]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/gpu/drm/i915/intel_display.c
drm/i915: Wrap drm_gem_object_reference in i915_gem_object_get
[mirror_ubuntu-bionic-kernel.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 <eric@anholt.net>
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"
760285e7 37#include <drm/i915_drm.h>
79e53945 38#include "i915_drv.h"
c37efb99 39#include "i915_gem_dmabuf.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
DV
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
DV
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
DV
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 *);
549e2bfb
CK
118static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
119 struct intel_crtc_state *crtc_state);
bfd16b2a
ML
120static void skylake_pfit_enable(struct intel_crtc *crtc);
121static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force);
122static void ironlake_pfit_enable(struct intel_crtc *crtc);
043e9bda 123static void intel_modeset_setup_hw_state(struct drm_device *dev);
2622a081 124static void intel_pre_disable_primary_noatomic(struct drm_crtc *crtc);
4e5ca60f 125static int ilk_max_pixel_rate(struct drm_atomic_state *state);
324513c0 126static int bxt_calc_cdclk(int max_pixclk);
e7457a9a 127
d4906093 128struct intel_limit {
4c5def93
ACO
129 struct {
130 int min, max;
131 } dot, vco, n, m, m1, m2, p, p1;
132
133 struct {
134 int dot_limit;
135 int p2_slow, p2_fast;
136 } p2;
d4906093 137};
79e53945 138
bfa7df01
VS
139/* returns HPLL frequency in kHz */
140static int valleyview_get_vco(struct drm_i915_private *dev_priv)
141{
142 int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
143
144 /* Obtain SKU information */
145 mutex_lock(&dev_priv->sb_lock);
146 hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
147 CCK_FUSE_HPLL_FREQ_MASK;
148 mutex_unlock(&dev_priv->sb_lock);
149
150 return vco_freq[hpll_freq] * 1000;
151}
152
c30fec65
VS
153int vlv_get_cck_clock(struct drm_i915_private *dev_priv,
154 const char *name, u32 reg, int ref_freq)
bfa7df01
VS
155{
156 u32 val;
157 int divider;
158
bfa7df01
VS
159 mutex_lock(&dev_priv->sb_lock);
160 val = vlv_cck_read(dev_priv, reg);
161 mutex_unlock(&dev_priv->sb_lock);
162
163 divider = val & CCK_FREQUENCY_VALUES;
164
165 WARN((val & CCK_FREQUENCY_STATUS) !=
166 (divider << CCK_FREQUENCY_STATUS_SHIFT),
167 "%s change in progress\n", name);
168
c30fec65
VS
169 return DIV_ROUND_CLOSEST(ref_freq << 1, divider + 1);
170}
171
172static int vlv_get_cck_clock_hpll(struct drm_i915_private *dev_priv,
173 const char *name, u32 reg)
174{
175 if (dev_priv->hpll_freq == 0)
176 dev_priv->hpll_freq = valleyview_get_vco(dev_priv);
177
178 return vlv_get_cck_clock(dev_priv, name, reg,
179 dev_priv->hpll_freq);
bfa7df01
VS
180}
181
e7dc33f3
VS
182static int
183intel_pch_rawclk(struct drm_i915_private *dev_priv)
d2acd215 184{
e7dc33f3
VS
185 return (I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK) * 1000;
186}
d2acd215 187
e7dc33f3
VS
188static int
189intel_vlv_hrawclk(struct drm_i915_private *dev_priv)
190{
19ab4ed3 191 /* RAWCLK_FREQ_VLV register updated from power well code */
35d38d1f
VS
192 return vlv_get_cck_clock_hpll(dev_priv, "hrawclk",
193 CCK_DISPLAY_REF_CLOCK_CONTROL);
d2acd215
DV
194}
195
e7dc33f3
VS
196static int
197intel_g4x_hrawclk(struct drm_i915_private *dev_priv)
79e50a4f 198{
79e50a4f
JN
199 uint32_t clkcfg;
200
e7dc33f3 201 /* hrawclock is 1/4 the FSB frequency */
79e50a4f
JN
202 clkcfg = I915_READ(CLKCFG);
203 switch (clkcfg & CLKCFG_FSB_MASK) {
204 case CLKCFG_FSB_400:
e7dc33f3 205 return 100000;
79e50a4f 206 case CLKCFG_FSB_533:
e7dc33f3 207 return 133333;
79e50a4f 208 case CLKCFG_FSB_667:
e7dc33f3 209 return 166667;
79e50a4f 210 case CLKCFG_FSB_800:
e7dc33f3 211 return 200000;
79e50a4f 212 case CLKCFG_FSB_1067:
e7dc33f3 213 return 266667;
79e50a4f 214 case CLKCFG_FSB_1333:
e7dc33f3 215 return 333333;
79e50a4f
JN
216 /* these two are just a guess; one of them might be right */
217 case CLKCFG_FSB_1600:
218 case CLKCFG_FSB_1600_ALT:
e7dc33f3 219 return 400000;
79e50a4f 220 default:
e7dc33f3 221 return 133333;
79e50a4f
JN
222 }
223}
224
19ab4ed3 225void intel_update_rawclk(struct drm_i915_private *dev_priv)
e7dc33f3
VS
226{
227 if (HAS_PCH_SPLIT(dev_priv))
228 dev_priv->rawclk_freq = intel_pch_rawclk(dev_priv);
229 else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
230 dev_priv->rawclk_freq = intel_vlv_hrawclk(dev_priv);
231 else if (IS_G4X(dev_priv) || IS_PINEVIEW(dev_priv))
232 dev_priv->rawclk_freq = intel_g4x_hrawclk(dev_priv);
233 else
234 return; /* no rawclk on other platforms, or no need to know it */
235
236 DRM_DEBUG_DRIVER("rawclk rate: %d kHz\n", dev_priv->rawclk_freq);
237}
238
bfa7df01
VS
239static void intel_update_czclk(struct drm_i915_private *dev_priv)
240{
666a4537 241 if (!(IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)))
bfa7df01
VS
242 return;
243
244 dev_priv->czclk_freq = vlv_get_cck_clock_hpll(dev_priv, "czclk",
245 CCK_CZ_CLOCK_CONTROL);
246
247 DRM_DEBUG_DRIVER("CZ clock rate: %d kHz\n", dev_priv->czclk_freq);
248}
249
021357ac 250static inline u32 /* units of 100MHz */
21a727b3
VS
251intel_fdi_link_freq(struct drm_i915_private *dev_priv,
252 const struct intel_crtc_state *pipe_config)
021357ac 253{
21a727b3
VS
254 if (HAS_DDI(dev_priv))
255 return pipe_config->port_clock; /* SPLL */
256 else if (IS_GEN5(dev_priv))
257 return ((I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2) * 10000;
e3b247da 258 else
21a727b3 259 return 270000;
021357ac
CW
260}
261
1b6f4958 262static const struct intel_limit intel_limits_i8xx_dac = {
0206e353 263 .dot = { .min = 25000, .max = 350000 },
9c333719 264 .vco = { .min = 908000, .max = 1512000 },
91dbe5fb 265 .n = { .min = 2, .max = 16 },
0206e353
AJ
266 .m = { .min = 96, .max = 140 },
267 .m1 = { .min = 18, .max = 26 },
268 .m2 = { .min = 6, .max = 16 },
269 .p = { .min = 4, .max = 128 },
270 .p1 = { .min = 2, .max = 33 },
273e27ca
EA
271 .p2 = { .dot_limit = 165000,
272 .p2_slow = 4, .p2_fast = 2 },
e4b36699
KP
273};
274
1b6f4958 275static const struct intel_limit intel_limits_i8xx_dvo = {
5d536e28 276 .dot = { .min = 25000, .max = 350000 },
9c333719 277 .vco = { .min = 908000, .max = 1512000 },
91dbe5fb 278 .n = { .min = 2, .max = 16 },
5d536e28
DV
279 .m = { .min = 96, .max = 140 },
280 .m1 = { .min = 18, .max = 26 },
281 .m2 = { .min = 6, .max = 16 },
282 .p = { .min = 4, .max = 128 },
283 .p1 = { .min = 2, .max = 33 },
284 .p2 = { .dot_limit = 165000,
285 .p2_slow = 4, .p2_fast = 4 },
286};
287
1b6f4958 288static const struct intel_limit intel_limits_i8xx_lvds = {
0206e353 289 .dot = { .min = 25000, .max = 350000 },
9c333719 290 .vco = { .min = 908000, .max = 1512000 },
91dbe5fb 291 .n = { .min = 2, .max = 16 },
0206e353
AJ
292 .m = { .min = 96, .max = 140 },
293 .m1 = { .min = 18, .max = 26 },
294 .m2 = { .min = 6, .max = 16 },
295 .p = { .min = 4, .max = 128 },
296 .p1 = { .min = 1, .max = 6 },
273e27ca
EA
297 .p2 = { .dot_limit = 165000,
298 .p2_slow = 14, .p2_fast = 7 },
e4b36699 299};
273e27ca 300
1b6f4958 301static const struct intel_limit intel_limits_i9xx_sdvo = {
0206e353
AJ
302 .dot = { .min = 20000, .max = 400000 },
303 .vco = { .min = 1400000, .max = 2800000 },
304 .n = { .min = 1, .max = 6 },
305 .m = { .min = 70, .max = 120 },
4f7dfb67
PJ
306 .m1 = { .min = 8, .max = 18 },
307 .m2 = { .min = 3, .max = 7 },
0206e353
AJ
308 .p = { .min = 5, .max = 80 },
309 .p1 = { .min = 1, .max = 8 },
273e27ca
EA
310 .p2 = { .dot_limit = 200000,
311 .p2_slow = 10, .p2_fast = 5 },
e4b36699
KP
312};
313
1b6f4958 314static const struct intel_limit intel_limits_i9xx_lvds = {
0206e353
AJ
315 .dot = { .min = 20000, .max = 400000 },
316 .vco = { .min = 1400000, .max = 2800000 },
317 .n = { .min = 1, .max = 6 },
318 .m = { .min = 70, .max = 120 },
53a7d2d1
PJ
319 .m1 = { .min = 8, .max = 18 },
320 .m2 = { .min = 3, .max = 7 },
0206e353
AJ
321 .p = { .min = 7, .max = 98 },
322 .p1 = { .min = 1, .max = 8 },
273e27ca
EA
323 .p2 = { .dot_limit = 112000,
324 .p2_slow = 14, .p2_fast = 7 },
e4b36699
KP
325};
326
273e27ca 327
1b6f4958 328static const struct intel_limit intel_limits_g4x_sdvo = {
273e27ca
EA
329 .dot = { .min = 25000, .max = 270000 },
330 .vco = { .min = 1750000, .max = 3500000},
331 .n = { .min = 1, .max = 4 },
332 .m = { .min = 104, .max = 138 },
333 .m1 = { .min = 17, .max = 23 },
334 .m2 = { .min = 5, .max = 11 },
335 .p = { .min = 10, .max = 30 },
336 .p1 = { .min = 1, .max = 3},
337 .p2 = { .dot_limit = 270000,
338 .p2_slow = 10,
339 .p2_fast = 10
044c7c41 340 },
e4b36699
KP
341};
342
1b6f4958 343static const struct intel_limit intel_limits_g4x_hdmi = {
273e27ca
EA
344 .dot = { .min = 22000, .max = 400000 },
345 .vco = { .min = 1750000, .max = 3500000},
346 .n = { .min = 1, .max = 4 },
347 .m = { .min = 104, .max = 138 },
348 .m1 = { .min = 16, .max = 23 },
349 .m2 = { .min = 5, .max = 11 },
350 .p = { .min = 5, .max = 80 },
351 .p1 = { .min = 1, .max = 8},
352 .p2 = { .dot_limit = 165000,
353 .p2_slow = 10, .p2_fast = 5 },
e4b36699
KP
354};
355
1b6f4958 356static const struct intel_limit intel_limits_g4x_single_channel_lvds = {
273e27ca
EA
357 .dot = { .min = 20000, .max = 115000 },
358 .vco = { .min = 1750000, .max = 3500000 },
359 .n = { .min = 1, .max = 3 },
360 .m = { .min = 104, .max = 138 },
361 .m1 = { .min = 17, .max = 23 },
362 .m2 = { .min = 5, .max = 11 },
363 .p = { .min = 28, .max = 112 },
364 .p1 = { .min = 2, .max = 8 },
365 .p2 = { .dot_limit = 0,
366 .p2_slow = 14, .p2_fast = 14
044c7c41 367 },
e4b36699
KP
368};
369
1b6f4958 370static const struct intel_limit intel_limits_g4x_dual_channel_lvds = {
273e27ca
EA
371 .dot = { .min = 80000, .max = 224000 },
372 .vco = { .min = 1750000, .max = 3500000 },
373 .n = { .min = 1, .max = 3 },
374 .m = { .min = 104, .max = 138 },
375 .m1 = { .min = 17, .max = 23 },
376 .m2 = { .min = 5, .max = 11 },
377 .p = { .min = 14, .max = 42 },
378 .p1 = { .min = 2, .max = 6 },
379 .p2 = { .dot_limit = 0,
380 .p2_slow = 7, .p2_fast = 7
044c7c41 381 },
e4b36699
KP
382};
383
1b6f4958 384static const struct intel_limit intel_limits_pineview_sdvo = {
0206e353
AJ
385 .dot = { .min = 20000, .max = 400000},
386 .vco = { .min = 1700000, .max = 3500000 },
273e27ca 387 /* Pineview's Ncounter is a ring counter */
0206e353
AJ
388 .n = { .min = 3, .max = 6 },
389 .m = { .min = 2, .max = 256 },
273e27ca 390 /* Pineview only has one combined m divider, which we treat as m2. */
0206e353
AJ
391 .m1 = { .min = 0, .max = 0 },
392 .m2 = { .min = 0, .max = 254 },
393 .p = { .min = 5, .max = 80 },
394 .p1 = { .min = 1, .max = 8 },
273e27ca
EA
395 .p2 = { .dot_limit = 200000,
396 .p2_slow = 10, .p2_fast = 5 },
e4b36699
KP
397};
398
1b6f4958 399static const struct intel_limit intel_limits_pineview_lvds = {
0206e353
AJ
400 .dot = { .min = 20000, .max = 400000 },
401 .vco = { .min = 1700000, .max = 3500000 },
402 .n = { .min = 3, .max = 6 },
403 .m = { .min = 2, .max = 256 },
404 .m1 = { .min = 0, .max = 0 },
405 .m2 = { .min = 0, .max = 254 },
406 .p = { .min = 7, .max = 112 },
407 .p1 = { .min = 1, .max = 8 },
273e27ca
EA
408 .p2 = { .dot_limit = 112000,
409 .p2_slow = 14, .p2_fast = 14 },
e4b36699
KP
410};
411
273e27ca
EA
412/* Ironlake / Sandybridge
413 *
414 * We calculate clock using (register_value + 2) for N/M1/M2, so here
415 * the range value for them is (actual_value - 2).
416 */
1b6f4958 417static const struct intel_limit intel_limits_ironlake_dac = {
273e27ca
EA
418 .dot = { .min = 25000, .max = 350000 },
419 .vco = { .min = 1760000, .max = 3510000 },
420 .n = { .min = 1, .max = 5 },
421 .m = { .min = 79, .max = 127 },
422 .m1 = { .min = 12, .max = 22 },
423 .m2 = { .min = 5, .max = 9 },
424 .p = { .min = 5, .max = 80 },
425 .p1 = { .min = 1, .max = 8 },
426 .p2 = { .dot_limit = 225000,
427 .p2_slow = 10, .p2_fast = 5 },
e4b36699
KP
428};
429
1b6f4958 430static const struct intel_limit intel_limits_ironlake_single_lvds = {
273e27ca
EA
431 .dot = { .min = 25000, .max = 350000 },
432 .vco = { .min = 1760000, .max = 3510000 },
433 .n = { .min = 1, .max = 3 },
434 .m = { .min = 79, .max = 118 },
435 .m1 = { .min = 12, .max = 22 },
436 .m2 = { .min = 5, .max = 9 },
437 .p = { .min = 28, .max = 112 },
438 .p1 = { .min = 2, .max = 8 },
439 .p2 = { .dot_limit = 225000,
440 .p2_slow = 14, .p2_fast = 14 },
b91ad0ec
ZW
441};
442
1b6f4958 443static const struct intel_limit intel_limits_ironlake_dual_lvds = {
273e27ca
EA
444 .dot = { .min = 25000, .max = 350000 },
445 .vco = { .min = 1760000, .max = 3510000 },
446 .n = { .min = 1, .max = 3 },
447 .m = { .min = 79, .max = 127 },
448 .m1 = { .min = 12, .max = 22 },
449 .m2 = { .min = 5, .max = 9 },
450 .p = { .min = 14, .max = 56 },
451 .p1 = { .min = 2, .max = 8 },
452 .p2 = { .dot_limit = 225000,
453 .p2_slow = 7, .p2_fast = 7 },
b91ad0ec
ZW
454};
455
273e27ca 456/* LVDS 100mhz refclk limits. */
1b6f4958 457static const struct intel_limit intel_limits_ironlake_single_lvds_100m = {
273e27ca
EA
458 .dot = { .min = 25000, .max = 350000 },
459 .vco = { .min = 1760000, .max = 3510000 },
460 .n = { .min = 1, .max = 2 },
461 .m = { .min = 79, .max = 126 },
462 .m1 = { .min = 12, .max = 22 },
463 .m2 = { .min = 5, .max = 9 },
464 .p = { .min = 28, .max = 112 },
0206e353 465 .p1 = { .min = 2, .max = 8 },
273e27ca
EA
466 .p2 = { .dot_limit = 225000,
467 .p2_slow = 14, .p2_fast = 14 },
b91ad0ec
ZW
468};
469
1b6f4958 470static const struct intel_limit intel_limits_ironlake_dual_lvds_100m = {
273e27ca
EA
471 .dot = { .min = 25000, .max = 350000 },
472 .vco = { .min = 1760000, .max = 3510000 },
473 .n = { .min = 1, .max = 3 },
474 .m = { .min = 79, .max = 126 },
475 .m1 = { .min = 12, .max = 22 },
476 .m2 = { .min = 5, .max = 9 },
477 .p = { .min = 14, .max = 42 },
0206e353 478 .p1 = { .min = 2, .max = 6 },
273e27ca
EA
479 .p2 = { .dot_limit = 225000,
480 .p2_slow = 7, .p2_fast = 7 },
4547668a
ZY
481};
482
1b6f4958 483static const struct intel_limit intel_limits_vlv = {
f01b7962
VS
484 /*
485 * These are the data rate limits (measured in fast clocks)
486 * since those are the strictest limits we have. The fast
487 * clock and actual rate limits are more relaxed, so checking
488 * them would make no difference.
489 */
490 .dot = { .min = 25000 * 5, .max = 270000 * 5 },
75e53986 491 .vco = { .min = 4000000, .max = 6000000 },
a0c4da24 492 .n = { .min = 1, .max = 7 },
a0c4da24
JB
493 .m1 = { .min = 2, .max = 3 },
494 .m2 = { .min = 11, .max = 156 },
b99ab663 495 .p1 = { .min = 2, .max = 3 },
5fdc9c49 496 .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
a0c4da24
JB
497};
498
1b6f4958 499static const struct intel_limit intel_limits_chv = {
ef9348c8
CML
500 /*
501 * These are the data rate limits (measured in fast clocks)
502 * since those are the strictest limits we have. The fast
503 * clock and actual rate limits are more relaxed, so checking
504 * them would make no difference.
505 */
506 .dot = { .min = 25000 * 5, .max = 540000 * 5},
17fe1021 507 .vco = { .min = 4800000, .max = 6480000 },
ef9348c8
CML
508 .n = { .min = 1, .max = 1 },
509 .m1 = { .min = 2, .max = 2 },
510 .m2 = { .min = 24 << 22, .max = 175 << 22 },
511 .p1 = { .min = 2, .max = 4 },
512 .p2 = { .p2_slow = 1, .p2_fast = 14 },
513};
514
1b6f4958 515static const struct intel_limit intel_limits_bxt = {
5ab7b0b7
ID
516 /* FIXME: find real dot limits */
517 .dot = { .min = 0, .max = INT_MAX },
e6292556 518 .vco = { .min = 4800000, .max = 6700000 },
5ab7b0b7
ID
519 .n = { .min = 1, .max = 1 },
520 .m1 = { .min = 2, .max = 2 },
521 /* FIXME: find real m2 limits */
522 .m2 = { .min = 2 << 22, .max = 255 << 22 },
523 .p1 = { .min = 2, .max = 4 },
524 .p2 = { .p2_slow = 1, .p2_fast = 20 },
525};
526
cdba954e
ACO
527static bool
528needs_modeset(struct drm_crtc_state *state)
529{
fc596660 530 return drm_atomic_crtc_needs_modeset(state);
cdba954e
ACO
531}
532
dccbea3b
ID
533/*
534 * Platform specific helpers to calculate the port PLL loopback- (clock.m),
535 * and post-divider (clock.p) values, pre- (clock.vco) and post-divided fast
536 * (clock.dot) clock rates. This fast dot clock is fed to the port's IO logic.
537 * The helpers' return value is the rate of the clock that is fed to the
538 * display engine's pipe which can be the above fast dot clock rate or a
539 * divided-down version of it.
540 */
f2b115e6 541/* m1 is reserved as 0 in Pineview, n is a ring counter */
9e2c8475 542static int pnv_calc_dpll_params(int refclk, struct dpll *clock)
79e53945 543{
2177832f
SL
544 clock->m = clock->m2 + 2;
545 clock->p = clock->p1 * clock->p2;
ed5ca77e 546 if (WARN_ON(clock->n == 0 || clock->p == 0))
dccbea3b 547 return 0;
fb03ac01
VS
548 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
549 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
dccbea3b
ID
550
551 return clock->dot;
2177832f
SL
552}
553
7429e9d4
DV
554static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
555{
556 return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
557}
558
9e2c8475 559static int i9xx_calc_dpll_params(int refclk, struct dpll *clock)
2177832f 560{
7429e9d4 561 clock->m = i9xx_dpll_compute_m(clock);
79e53945 562 clock->p = clock->p1 * clock->p2;
ed5ca77e 563 if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
dccbea3b 564 return 0;
fb03ac01
VS
565 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
566 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
dccbea3b
ID
567
568 return clock->dot;
79e53945
JB
569}
570
9e2c8475 571static int vlv_calc_dpll_params(int refclk, struct dpll *clock)
589eca67
ID
572{
573 clock->m = clock->m1 * clock->m2;
574 clock->p = clock->p1 * clock->p2;
575 if (WARN_ON(clock->n == 0 || clock->p == 0))
dccbea3b 576 return 0;
589eca67
ID
577 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
578 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
dccbea3b
ID
579
580 return clock->dot / 5;
589eca67
ID
581}
582
9e2c8475 583int chv_calc_dpll_params(int refclk, struct dpll *clock)
ef9348c8
CML
584{
585 clock->m = clock->m1 * clock->m2;
586 clock->p = clock->p1 * clock->p2;
587 if (WARN_ON(clock->n == 0 || clock->p == 0))
dccbea3b 588 return 0;
ef9348c8
CML
589 clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
590 clock->n << 22);
591 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
dccbea3b
ID
592
593 return clock->dot / 5;
ef9348c8
CML
594}
595
7c04d1d9 596#define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
79e53945
JB
597/**
598 * Returns whether the given set of divisors are valid for a given refclk with
599 * the given connectors.
600 */
601
1b894b59 602static bool intel_PLL_is_valid(struct drm_device *dev,
1b6f4958 603 const struct intel_limit *limit,
9e2c8475 604 const struct dpll *clock)
79e53945 605{
f01b7962
VS
606 if (clock->n < limit->n.min || limit->n.max < clock->n)
607 INTELPllInvalid("n out of range\n");
79e53945 608 if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
0206e353 609 INTELPllInvalid("p1 out of range\n");
79e53945 610 if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
0206e353 611 INTELPllInvalid("m2 out of range\n");
79e53945 612 if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
0206e353 613 INTELPllInvalid("m1 out of range\n");
f01b7962 614
666a4537
WB
615 if (!IS_PINEVIEW(dev) && !IS_VALLEYVIEW(dev) &&
616 !IS_CHERRYVIEW(dev) && !IS_BROXTON(dev))
f01b7962
VS
617 if (clock->m1 <= clock->m2)
618 INTELPllInvalid("m1 <= m2\n");
619
666a4537 620 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev) && !IS_BROXTON(dev)) {
f01b7962
VS
621 if (clock->p < limit->p.min || limit->p.max < clock->p)
622 INTELPllInvalid("p out of range\n");
623 if (clock->m < limit->m.min || limit->m.max < clock->m)
624 INTELPllInvalid("m out of range\n");
625 }
626
79e53945 627 if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
0206e353 628 INTELPllInvalid("vco out of range\n");
79e53945
JB
629 /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
630 * connector, etc., rather than just a single range.
631 */
632 if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
0206e353 633 INTELPllInvalid("dot out of range\n");
79e53945
JB
634
635 return true;
636}
637
3b1429d9 638static int
1b6f4958 639i9xx_select_p2_div(const struct intel_limit *limit,
3b1429d9
VS
640 const struct intel_crtc_state *crtc_state,
641 int target)
79e53945 642{
3b1429d9 643 struct drm_device *dev = crtc_state->base.crtc->dev;
79e53945 644
2d84d2b3 645 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
79e53945 646 /*
a210b028
DV
647 * For LVDS just rely on its current settings for dual-channel.
648 * We haven't figured out how to reliably set up different
649 * single/dual channel state, if we even can.
79e53945 650 */
1974cad0 651 if (intel_is_dual_link_lvds(dev))
3b1429d9 652 return limit->p2.p2_fast;
79e53945 653 else
3b1429d9 654 return limit->p2.p2_slow;
79e53945
JB
655 } else {
656 if (target < limit->p2.dot_limit)
3b1429d9 657 return limit->p2.p2_slow;
79e53945 658 else
3b1429d9 659 return limit->p2.p2_fast;
79e53945 660 }
3b1429d9
VS
661}
662
70e8aa21
ACO
663/*
664 * Returns a set of divisors for the desired target clock with the given
665 * refclk, or FALSE. The returned values represent the clock equation:
666 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
667 *
668 * Target and reference clocks are specified in kHz.
669 *
670 * If match_clock is provided, then best_clock P divider must match the P
671 * divider from @match_clock used for LVDS downclocking.
672 */
3b1429d9 673static bool
1b6f4958 674i9xx_find_best_dpll(const struct intel_limit *limit,
3b1429d9 675 struct intel_crtc_state *crtc_state,
9e2c8475
ACO
676 int target, int refclk, struct dpll *match_clock,
677 struct dpll *best_clock)
3b1429d9
VS
678{
679 struct drm_device *dev = crtc_state->base.crtc->dev;
9e2c8475 680 struct dpll clock;
3b1429d9 681 int err = target;
79e53945 682
0206e353 683 memset(best_clock, 0, sizeof(*best_clock));
79e53945 684
3b1429d9
VS
685 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
686
42158660
ZY
687 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
688 clock.m1++) {
689 for (clock.m2 = limit->m2.min;
690 clock.m2 <= limit->m2.max; clock.m2++) {
c0efc387 691 if (clock.m2 >= clock.m1)
42158660
ZY
692 break;
693 for (clock.n = limit->n.min;
694 clock.n <= limit->n.max; clock.n++) {
695 for (clock.p1 = limit->p1.min;
696 clock.p1 <= limit->p1.max; clock.p1++) {
79e53945
JB
697 int this_err;
698
dccbea3b 699 i9xx_calc_dpll_params(refclk, &clock);
ac58c3f0
DV
700 if (!intel_PLL_is_valid(dev, limit,
701 &clock))
702 continue;
703 if (match_clock &&
704 clock.p != match_clock->p)
705 continue;
706
707 this_err = abs(clock.dot - target);
708 if (this_err < err) {
709 *best_clock = clock;
710 err = this_err;
711 }
712 }
713 }
714 }
715 }
716
717 return (err != target);
718}
719
70e8aa21
ACO
720/*
721 * Returns a set of divisors for the desired target clock with the given
722 * refclk, or FALSE. The returned values represent the clock equation:
723 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
724 *
725 * Target and reference clocks are specified in kHz.
726 *
727 * If match_clock is provided, then best_clock P divider must match the P
728 * divider from @match_clock used for LVDS downclocking.
729 */
ac58c3f0 730static bool
1b6f4958 731pnv_find_best_dpll(const struct intel_limit *limit,
a93e255f 732 struct intel_crtc_state *crtc_state,
9e2c8475
ACO
733 int target, int refclk, struct dpll *match_clock,
734 struct dpll *best_clock)
79e53945 735{
3b1429d9 736 struct drm_device *dev = crtc_state->base.crtc->dev;
9e2c8475 737 struct dpll clock;
79e53945
JB
738 int err = target;
739
0206e353 740 memset(best_clock, 0, sizeof(*best_clock));
79e53945 741
3b1429d9
VS
742 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
743
42158660
ZY
744 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
745 clock.m1++) {
746 for (clock.m2 = limit->m2.min;
747 clock.m2 <= limit->m2.max; clock.m2++) {
42158660
ZY
748 for (clock.n = limit->n.min;
749 clock.n <= limit->n.max; clock.n++) {
750 for (clock.p1 = limit->p1.min;
751 clock.p1 <= limit->p1.max; clock.p1++) {
79e53945
JB
752 int this_err;
753
dccbea3b 754 pnv_calc_dpll_params(refclk, &clock);
1b894b59
CW
755 if (!intel_PLL_is_valid(dev, limit,
756 &clock))
79e53945 757 continue;
cec2f356
SP
758 if (match_clock &&
759 clock.p != match_clock->p)
760 continue;
79e53945
JB
761
762 this_err = abs(clock.dot - target);
763 if (this_err < err) {
764 *best_clock = clock;
765 err = this_err;
766 }
767 }
768 }
769 }
770 }
771
772 return (err != target);
773}
774
997c030c
ACO
775/*
776 * Returns a set of divisors for the desired target clock with the given
777 * refclk, or FALSE. The returned values represent the clock equation:
778 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
70e8aa21
ACO
779 *
780 * Target and reference clocks are specified in kHz.
781 *
782 * If match_clock is provided, then best_clock P divider must match the P
783 * divider from @match_clock used for LVDS downclocking.
997c030c 784 */
d4906093 785static bool
1b6f4958 786g4x_find_best_dpll(const struct intel_limit *limit,
a93e255f 787 struct intel_crtc_state *crtc_state,
9e2c8475
ACO
788 int target, int refclk, struct dpll *match_clock,
789 struct dpll *best_clock)
d4906093 790{
3b1429d9 791 struct drm_device *dev = crtc_state->base.crtc->dev;
9e2c8475 792 struct dpll clock;
d4906093 793 int max_n;
3b1429d9 794 bool found = false;
6ba770dc
AJ
795 /* approximately equals target * 0.00585 */
796 int err_most = (target >> 8) + (target >> 9);
d4906093
ML
797
798 memset(best_clock, 0, sizeof(*best_clock));
3b1429d9
VS
799
800 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
801
d4906093 802 max_n = limit->n.max;
f77f13e2 803 /* based on hardware requirement, prefer smaller n to precision */
d4906093 804 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
f77f13e2 805 /* based on hardware requirement, prefere larger m1,m2 */
d4906093
ML
806 for (clock.m1 = limit->m1.max;
807 clock.m1 >= limit->m1.min; clock.m1--) {
808 for (clock.m2 = limit->m2.max;
809 clock.m2 >= limit->m2.min; clock.m2--) {
810 for (clock.p1 = limit->p1.max;
811 clock.p1 >= limit->p1.min; clock.p1--) {
812 int this_err;
813
dccbea3b 814 i9xx_calc_dpll_params(refclk, &clock);
1b894b59
CW
815 if (!intel_PLL_is_valid(dev, limit,
816 &clock))
d4906093 817 continue;
1b894b59
CW
818
819 this_err = abs(clock.dot - target);
d4906093
ML
820 if (this_err < err_most) {
821 *best_clock = clock;
822 err_most = this_err;
823 max_n = clock.n;
824 found = true;
825 }
826 }
827 }
828 }
829 }
2c07245f
ZW
830 return found;
831}
832
d5dd62bd
ID
833/*
834 * Check if the calculated PLL configuration is more optimal compared to the
835 * best configuration and error found so far. Return the calculated error.
836 */
837static bool vlv_PLL_is_optimal(struct drm_device *dev, int target_freq,
9e2c8475
ACO
838 const struct dpll *calculated_clock,
839 const struct dpll *best_clock,
d5dd62bd
ID
840 unsigned int best_error_ppm,
841 unsigned int *error_ppm)
842{
9ca3ba01
ID
843 /*
844 * For CHV ignore the error and consider only the P value.
845 * Prefer a bigger P value based on HW requirements.
846 */
847 if (IS_CHERRYVIEW(dev)) {
848 *error_ppm = 0;
849
850 return calculated_clock->p > best_clock->p;
851 }
852
24be4e46
ID
853 if (WARN_ON_ONCE(!target_freq))
854 return false;
855
d5dd62bd
ID
856 *error_ppm = div_u64(1000000ULL *
857 abs(target_freq - calculated_clock->dot),
858 target_freq);
859 /*
860 * Prefer a better P value over a better (smaller) error if the error
861 * is small. Ensure this preference for future configurations too by
862 * setting the error to 0.
863 */
864 if (*error_ppm < 100 && calculated_clock->p > best_clock->p) {
865 *error_ppm = 0;
866
867 return true;
868 }
869
870 return *error_ppm + 10 < best_error_ppm;
871}
872
65b3d6a9
ACO
873/*
874 * Returns a set of divisors for the desired target clock with the given
875 * refclk, or FALSE. The returned values represent the clock equation:
876 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
877 */
a0c4da24 878static bool
1b6f4958 879vlv_find_best_dpll(const struct intel_limit *limit,
a93e255f 880 struct intel_crtc_state *crtc_state,
9e2c8475
ACO
881 int target, int refclk, struct dpll *match_clock,
882 struct dpll *best_clock)
a0c4da24 883{
a93e255f 884 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
a919ff14 885 struct drm_device *dev = crtc->base.dev;
9e2c8475 886 struct dpll clock;
69e4f900 887 unsigned int bestppm = 1000000;
27e639bf
VS
888 /* min update 19.2 MHz */
889 int max_n = min(limit->n.max, refclk / 19200);
49e497ef 890 bool found = false;
a0c4da24 891
6b4bf1c4
VS
892 target *= 5; /* fast clock */
893
894 memset(best_clock, 0, sizeof(*best_clock));
a0c4da24
JB
895
896 /* based on hardware requirement, prefer smaller n to precision */
27e639bf 897 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
811bbf05 898 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
889059d8 899 for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
c1a9ae43 900 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
6b4bf1c4 901 clock.p = clock.p1 * clock.p2;
a0c4da24 902 /* based on hardware requirement, prefer bigger m1,m2 values */
6b4bf1c4 903 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
d5dd62bd 904 unsigned int ppm;
69e4f900 905
6b4bf1c4
VS
906 clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
907 refclk * clock.m1);
908
dccbea3b 909 vlv_calc_dpll_params(refclk, &clock);
43b0ac53 910
f01b7962
VS
911 if (!intel_PLL_is_valid(dev, limit,
912 &clock))
43b0ac53
VS
913 continue;
914
d5dd62bd
ID
915 if (!vlv_PLL_is_optimal(dev, target,
916 &clock,
917 best_clock,
918 bestppm, &ppm))
919 continue;
6b4bf1c4 920
d5dd62bd
ID
921 *best_clock = clock;
922 bestppm = ppm;
923 found = true;
a0c4da24
JB
924 }
925 }
926 }
927 }
a0c4da24 928
49e497ef 929 return found;
a0c4da24 930}
a4fc5ed6 931
65b3d6a9
ACO
932/*
933 * Returns a set of divisors for the desired target clock with the given
934 * refclk, or FALSE. The returned values represent the clock equation:
935 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
936 */
ef9348c8 937static bool
1b6f4958 938chv_find_best_dpll(const struct intel_limit *limit,
a93e255f 939 struct intel_crtc_state *crtc_state,
9e2c8475
ACO
940 int target, int refclk, struct dpll *match_clock,
941 struct dpll *best_clock)
ef9348c8 942{
a93e255f 943 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
a919ff14 944 struct drm_device *dev = crtc->base.dev;
9ca3ba01 945 unsigned int best_error_ppm;
9e2c8475 946 struct dpll clock;
ef9348c8
CML
947 uint64_t m2;
948 int found = false;
949
950 memset(best_clock, 0, sizeof(*best_clock));
9ca3ba01 951 best_error_ppm = 1000000;
ef9348c8
CML
952
953 /*
954 * Based on hardware doc, the n always set to 1, and m1 always
955 * set to 2. If requires to support 200Mhz refclk, we need to
956 * revisit this because n may not 1 anymore.
957 */
958 clock.n = 1, clock.m1 = 2;
959 target *= 5; /* fast clock */
960
961 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
962 for (clock.p2 = limit->p2.p2_fast;
963 clock.p2 >= limit->p2.p2_slow;
964 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
9ca3ba01 965 unsigned int error_ppm;
ef9348c8
CML
966
967 clock.p = clock.p1 * clock.p2;
968
969 m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
970 clock.n) << 22, refclk * clock.m1);
971
972 if (m2 > INT_MAX/clock.m1)
973 continue;
974
975 clock.m2 = m2;
976
dccbea3b 977 chv_calc_dpll_params(refclk, &clock);
ef9348c8
CML
978
979 if (!intel_PLL_is_valid(dev, limit, &clock))
980 continue;
981
9ca3ba01
ID
982 if (!vlv_PLL_is_optimal(dev, target, &clock, best_clock,
983 best_error_ppm, &error_ppm))
984 continue;
985
986 *best_clock = clock;
987 best_error_ppm = error_ppm;
988 found = true;
ef9348c8
CML
989 }
990 }
991
992 return found;
993}
994
5ab7b0b7 995bool bxt_find_best_dpll(struct intel_crtc_state *crtc_state, int target_clock,
9e2c8475 996 struct dpll *best_clock)
5ab7b0b7 997{
65b3d6a9 998 int refclk = 100000;
1b6f4958 999 const struct intel_limit *limit = &intel_limits_bxt;
5ab7b0b7 1000
65b3d6a9 1001 return chv_find_best_dpll(limit, crtc_state,
5ab7b0b7
ID
1002 target_clock, refclk, NULL, best_clock);
1003}
1004
20ddf665
VS
1005bool intel_crtc_active(struct drm_crtc *crtc)
1006{
1007 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1008
1009 /* Be paranoid as we can arrive here with only partial
1010 * state retrieved from the hardware during setup.
1011 *
241bfc38 1012 * We can ditch the adjusted_mode.crtc_clock check as soon
20ddf665
VS
1013 * as Haswell has gained clock readout/fastboot support.
1014 *
66e514c1 1015 * We can ditch the crtc->primary->fb check as soon as we can
20ddf665 1016 * properly reconstruct framebuffers.
c3d1f436
MR
1017 *
1018 * FIXME: The intel_crtc->active here should be switched to
1019 * crtc->state->active once we have proper CRTC states wired up
1020 * for atomic.
20ddf665 1021 */
c3d1f436 1022 return intel_crtc->active && crtc->primary->state->fb &&
6e3c9717 1023 intel_crtc->config->base.adjusted_mode.crtc_clock;
20ddf665
VS
1024}
1025
a5c961d1
PZ
1026enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
1027 enum pipe pipe)
1028{
1029 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
1030 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1031
6e3c9717 1032 return intel_crtc->config->cpu_transcoder;
a5c961d1
PZ
1033}
1034
fbf49ea2
VS
1035static bool pipe_dsl_stopped(struct drm_device *dev, enum pipe pipe)
1036{
fac5e23e 1037 struct drm_i915_private *dev_priv = to_i915(dev);
f0f59a00 1038 i915_reg_t reg = PIPEDSL(pipe);
fbf49ea2
VS
1039 u32 line1, line2;
1040 u32 line_mask;
1041
1042 if (IS_GEN2(dev))
1043 line_mask = DSL_LINEMASK_GEN2;
1044 else
1045 line_mask = DSL_LINEMASK_GEN3;
1046
1047 line1 = I915_READ(reg) & line_mask;
6adfb1ef 1048 msleep(5);
fbf49ea2
VS
1049 line2 = I915_READ(reg) & line_mask;
1050
1051 return line1 == line2;
1052}
1053
ab7ad7f6
KP
1054/*
1055 * intel_wait_for_pipe_off - wait for pipe to turn off
575f7ab7 1056 * @crtc: crtc whose pipe to wait for
9d0498a2
JB
1057 *
1058 * After disabling a pipe, we can't wait for vblank in the usual way,
1059 * spinning on the vblank interrupt status bit, since we won't actually
1060 * see an interrupt when the pipe is disabled.
1061 *
ab7ad7f6
KP
1062 * On Gen4 and above:
1063 * wait for the pipe register state bit to turn off
1064 *
1065 * Otherwise:
1066 * wait for the display line value to settle (it usually
1067 * ends up stopping at the start of the next frame).
58e10eb9 1068 *
9d0498a2 1069 */
575f7ab7 1070static void intel_wait_for_pipe_off(struct intel_crtc *crtc)
9d0498a2 1071{
575f7ab7 1072 struct drm_device *dev = crtc->base.dev;
fac5e23e 1073 struct drm_i915_private *dev_priv = to_i915(dev);
6e3c9717 1074 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
575f7ab7 1075 enum pipe pipe = crtc->pipe;
ab7ad7f6
KP
1076
1077 if (INTEL_INFO(dev)->gen >= 4) {
f0f59a00 1078 i915_reg_t reg = PIPECONF(cpu_transcoder);
ab7ad7f6
KP
1079
1080 /* Wait for the Pipe State to go off */
b8511f53
CW
1081 if (intel_wait_for_register(dev_priv,
1082 reg, I965_PIPECONF_ACTIVE, 0,
1083 100))
284637d9 1084 WARN(1, "pipe_off wait timed out\n");
ab7ad7f6 1085 } else {
ab7ad7f6 1086 /* Wait for the display line to settle */
fbf49ea2 1087 if (wait_for(pipe_dsl_stopped(dev, pipe), 100))
284637d9 1088 WARN(1, "pipe_off wait timed out\n");
ab7ad7f6 1089 }
79e53945
JB
1090}
1091
b24e7179 1092/* Only for pre-ILK configs */
55607e8a
DV
1093void assert_pll(struct drm_i915_private *dev_priv,
1094 enum pipe pipe, bool state)
b24e7179 1095{
b24e7179
JB
1096 u32 val;
1097 bool cur_state;
1098
649636ef 1099 val = I915_READ(DPLL(pipe));
b24e7179 1100 cur_state = !!(val & DPLL_VCO_ENABLE);
e2c719b7 1101 I915_STATE_WARN(cur_state != state,
b24e7179 1102 "PLL state assertion failure (expected %s, current %s)\n",
87ad3212 1103 onoff(state), onoff(cur_state));
b24e7179 1104}
b24e7179 1105
23538ef1 1106/* XXX: the dsi pll is shared between MIPI DSI ports */
8563b1e8 1107void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
23538ef1
JN
1108{
1109 u32 val;
1110 bool cur_state;
1111
a580516d 1112 mutex_lock(&dev_priv->sb_lock);
23538ef1 1113 val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
a580516d 1114 mutex_unlock(&dev_priv->sb_lock);
23538ef1
JN
1115
1116 cur_state = val & DSI_PLL_VCO_EN;
e2c719b7 1117 I915_STATE_WARN(cur_state != state,
23538ef1 1118 "DSI PLL state assertion failure (expected %s, current %s)\n",
87ad3212 1119 onoff(state), onoff(cur_state));
23538ef1 1120}
23538ef1 1121
040484af
JB
1122static void assert_fdi_tx(struct drm_i915_private *dev_priv,
1123 enum pipe pipe, bool state)
1124{
040484af 1125 bool cur_state;
ad80a810
PZ
1126 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1127 pipe);
040484af 1128
2d1fe073 1129 if (HAS_DDI(dev_priv)) {
affa9354 1130 /* DDI does not have a specific FDI_TX register */
649636ef 1131 u32 val = I915_READ(TRANS_DDI_FUNC_CTL(cpu_transcoder));
ad80a810 1132 cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
bf507ef7 1133 } else {
649636ef 1134 u32 val = I915_READ(FDI_TX_CTL(pipe));
bf507ef7
ED
1135 cur_state = !!(val & FDI_TX_ENABLE);
1136 }
e2c719b7 1137 I915_STATE_WARN(cur_state != state,
040484af 1138 "FDI TX state assertion failure (expected %s, current %s)\n",
87ad3212 1139 onoff(state), onoff(cur_state));
040484af
JB
1140}
1141#define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
1142#define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
1143
1144static void assert_fdi_rx(struct drm_i915_private *dev_priv,
1145 enum pipe pipe, bool state)
1146{
040484af
JB
1147 u32 val;
1148 bool cur_state;
1149
649636ef 1150 val = I915_READ(FDI_RX_CTL(pipe));
d63fa0dc 1151 cur_state = !!(val & FDI_RX_ENABLE);
e2c719b7 1152 I915_STATE_WARN(cur_state != state,
040484af 1153 "FDI RX state assertion failure (expected %s, current %s)\n",
87ad3212 1154 onoff(state), onoff(cur_state));
040484af
JB
1155}
1156#define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
1157#define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
1158
1159static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
1160 enum pipe pipe)
1161{
040484af
JB
1162 u32 val;
1163
1164 /* ILK FDI PLL is always enabled */
7e22dbbb 1165 if (IS_GEN5(dev_priv))
040484af
JB
1166 return;
1167
bf507ef7 1168 /* On Haswell, DDI ports are responsible for the FDI PLL setup */
2d1fe073 1169 if (HAS_DDI(dev_priv))
bf507ef7
ED
1170 return;
1171
649636ef 1172 val = I915_READ(FDI_TX_CTL(pipe));
e2c719b7 1173 I915_STATE_WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
040484af
JB
1174}
1175
55607e8a
DV
1176void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
1177 enum pipe pipe, bool state)
040484af 1178{
040484af 1179 u32 val;
55607e8a 1180 bool cur_state;
040484af 1181
649636ef 1182 val = I915_READ(FDI_RX_CTL(pipe));
55607e8a 1183 cur_state = !!(val & FDI_RX_PLL_ENABLE);
e2c719b7 1184 I915_STATE_WARN(cur_state != state,
55607e8a 1185 "FDI RX PLL assertion failure (expected %s, current %s)\n",
87ad3212 1186 onoff(state), onoff(cur_state));
040484af
JB
1187}
1188
b680c37a
DV
1189void assert_panel_unlocked(struct drm_i915_private *dev_priv,
1190 enum pipe pipe)
ea0760cf 1191{
91c8a326 1192 struct drm_device *dev = &dev_priv->drm;
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
bedd4dba
JN
1198 if (WARN_ON(HAS_DDI(dev)))
1199 return;
1200
1201 if (HAS_PCH_SPLIT(dev)) {
1202 u32 port_sel;
1203
ea0760cf 1204 pp_reg = PCH_PP_CONTROL;
bedd4dba
JN
1205 port_sel = I915_READ(PCH_PP_ON_DELAYS) & PANEL_PORT_SELECT_MASK;
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 */
666a4537 1211 } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
bedd4dba
JN
1212 /* presumably write lock depends on pipe, not port select */
1213 pp_reg = VLV_PIPE_PP_CONTROL(pipe);
1214 panel_pipe = pipe;
ea0760cf
JB
1215 } else {
1216 pp_reg = PP_CONTROL;
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{
91c8a326 1234 struct drm_device *dev = &dev_priv->drm;
93ce0ba6
JN
1235 bool cur_state;
1236
d9d82081 1237 if (IS_845G(dev) || IS_I865G(dev))
0b87c24e 1238 cur_state = I915_READ(CURCNTR(PIPE_A)) & CURSOR_ENABLE;
d9d82081 1239 else
5efb3e28 1240 cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
93ce0ba6 1241
e2c719b7 1242 I915_STATE_WARN(cur_state != state,
93ce0ba6 1243 "cursor on pipe %c assertion failure (expected %s, current %s)\n",
87ad3212 1244 pipe_name(pipe), onoff(state), onoff(cur_state));
93ce0ba6
JN
1245}
1246#define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
1247#define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
1248
b840d907
JB
1249void assert_pipe(struct drm_i915_private *dev_priv,
1250 enum pipe pipe, bool state)
b24e7179 1251{
63d7bbe9 1252 bool cur_state;
702e7a56
PZ
1253 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1254 pipe);
4feed0eb 1255 enum intel_display_power_domain power_domain;
b24e7179 1256
b6b5d049
VS
1257 /* if we need the pipe quirk it must be always on */
1258 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1259 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
8e636784
DV
1260 state = true;
1261
4feed0eb
ID
1262 power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
1263 if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
649636ef 1264 u32 val = I915_READ(PIPECONF(cpu_transcoder));
69310161 1265 cur_state = !!(val & PIPECONF_ENABLE);
4feed0eb
ID
1266
1267 intel_display_power_put(dev_priv, power_domain);
1268 } else {
1269 cur_state = false;
69310161
PZ
1270 }
1271
e2c719b7 1272 I915_STATE_WARN(cur_state != state,
63d7bbe9 1273 "pipe %c assertion failure (expected %s, current %s)\n",
87ad3212 1274 pipe_name(pipe), onoff(state), onoff(cur_state));
b24e7179
JB
1275}
1276
931872fc
CW
1277static void assert_plane(struct drm_i915_private *dev_priv,
1278 enum plane plane, bool state)
b24e7179 1279{
b24e7179 1280 u32 val;
931872fc 1281 bool cur_state;
b24e7179 1282
649636ef 1283 val = I915_READ(DSPCNTR(plane));
931872fc 1284 cur_state = !!(val & DISPLAY_PLANE_ENABLE);
e2c719b7 1285 I915_STATE_WARN(cur_state != state,
931872fc 1286 "plane %c assertion failure (expected %s, current %s)\n",
87ad3212 1287 plane_name(plane), onoff(state), onoff(cur_state));
b24e7179
JB
1288}
1289
931872fc
CW
1290#define assert_plane_enabled(d, p) assert_plane(d, p, true)
1291#define assert_plane_disabled(d, p) assert_plane(d, p, false)
1292
b24e7179
JB
1293static void assert_planes_disabled(struct drm_i915_private *dev_priv,
1294 enum pipe pipe)
1295{
91c8a326 1296 struct drm_device *dev = &dev_priv->drm;
649636ef 1297 int i;
b24e7179 1298
653e1026
VS
1299 /* Primary planes are fixed to pipes on gen4+ */
1300 if (INTEL_INFO(dev)->gen >= 4) {
649636ef 1301 u32 val = I915_READ(DSPCNTR(pipe));
e2c719b7 1302 I915_STATE_WARN(val & DISPLAY_PLANE_ENABLE,
28c05794
AJ
1303 "plane %c assertion failure, should be disabled but not\n",
1304 plane_name(pipe));
19ec1358 1305 return;
28c05794 1306 }
19ec1358 1307
b24e7179 1308 /* Need to check both planes against the pipe */
055e393f 1309 for_each_pipe(dev_priv, i) {
649636ef
VS
1310 u32 val = I915_READ(DSPCNTR(i));
1311 enum pipe cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
b24e7179 1312 DISPPLANE_SEL_PIPE_SHIFT;
e2c719b7 1313 I915_STATE_WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
9db4a9c7
JB
1314 "plane %c assertion failure, should be off on pipe %c but is still active\n",
1315 plane_name(i), pipe_name(pipe));
b24e7179
JB
1316 }
1317}
1318
19332d7a
JB
1319static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
1320 enum pipe pipe)
1321{
91c8a326 1322 struct drm_device *dev = &dev_priv->drm;
649636ef 1323 int sprite;
19332d7a 1324
7feb8b88 1325 if (INTEL_INFO(dev)->gen >= 9) {
3bdcfc0c 1326 for_each_sprite(dev_priv, pipe, sprite) {
649636ef 1327 u32 val = I915_READ(PLANE_CTL(pipe, sprite));
e2c719b7 1328 I915_STATE_WARN(val & PLANE_CTL_ENABLE,
7feb8b88
DL
1329 "plane %d assertion failure, should be off on pipe %c but is still active\n",
1330 sprite, pipe_name(pipe));
1331 }
666a4537 1332 } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
3bdcfc0c 1333 for_each_sprite(dev_priv, pipe, sprite) {
649636ef 1334 u32 val = I915_READ(SPCNTR(pipe, sprite));
e2c719b7 1335 I915_STATE_WARN(val & SP_ENABLE,
20674eef 1336 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
1fe47785 1337 sprite_name(pipe, sprite), pipe_name(pipe));
20674eef
VS
1338 }
1339 } else if (INTEL_INFO(dev)->gen >= 7) {
649636ef 1340 u32 val = I915_READ(SPRCTL(pipe));
e2c719b7 1341 I915_STATE_WARN(val & SPRITE_ENABLE,
06da8da2 1342 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
20674eef
VS
1343 plane_name(pipe), pipe_name(pipe));
1344 } else if (INTEL_INFO(dev)->gen >= 5) {
649636ef 1345 u32 val = I915_READ(DVSCNTR(pipe));
e2c719b7 1346 I915_STATE_WARN(val & DVS_ENABLE,
06da8da2 1347 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
20674eef 1348 plane_name(pipe), pipe_name(pipe));
19332d7a
JB
1349 }
1350}
1351
08c71e5e
VS
1352static void assert_vblank_disabled(struct drm_crtc *crtc)
1353{
e2c719b7 1354 if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
08c71e5e
VS
1355 drm_crtc_vblank_put(crtc);
1356}
1357
7abd4b35
ACO
1358void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
1359 enum pipe pipe)
92f2584a 1360{
92f2584a
JB
1361 u32 val;
1362 bool enabled;
1363
649636ef 1364 val = I915_READ(PCH_TRANSCONF(pipe));
92f2584a 1365 enabled = !!(val & TRANS_ENABLE);
e2c719b7 1366 I915_STATE_WARN(enabled,
9db4a9c7
JB
1367 "transcoder assertion failed, should be off on pipe %c but is still active\n",
1368 pipe_name(pipe));
92f2584a
JB
1369}
1370
4e634389
KP
1371static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
1372 enum pipe pipe, u32 port_sel, u32 val)
f0575e92
KP
1373{
1374 if ((val & DP_PORT_EN) == 0)
1375 return false;
1376
2d1fe073 1377 if (HAS_PCH_CPT(dev_priv)) {
f0f59a00 1378 u32 trans_dp_ctl = I915_READ(TRANS_DP_CTL(pipe));
f0575e92
KP
1379 if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
1380 return false;
2d1fe073 1381 } else if (IS_CHERRYVIEW(dev_priv)) {
44f37d1f
CML
1382 if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
1383 return false;
f0575e92
KP
1384 } else {
1385 if ((val & DP_PIPE_MASK) != (pipe << 30))
1386 return false;
1387 }
1388 return true;
1389}
1390
1519b995
KP
1391static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
1392 enum pipe pipe, u32 val)
1393{
dc0fa718 1394 if ((val & SDVO_ENABLE) == 0)
1519b995
KP
1395 return false;
1396
2d1fe073 1397 if (HAS_PCH_CPT(dev_priv)) {
dc0fa718 1398 if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
1519b995 1399 return false;
2d1fe073 1400 } else if (IS_CHERRYVIEW(dev_priv)) {
44f37d1f
CML
1401 if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
1402 return false;
1519b995 1403 } else {
dc0fa718 1404 if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
1519b995
KP
1405 return false;
1406 }
1407 return true;
1408}
1409
1410static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
1411 enum pipe pipe, u32 val)
1412{
1413 if ((val & LVDS_PORT_EN) == 0)
1414 return false;
1415
2d1fe073 1416 if (HAS_PCH_CPT(dev_priv)) {
1519b995
KP
1417 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1418 return false;
1419 } else {
1420 if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
1421 return false;
1422 }
1423 return true;
1424}
1425
1426static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
1427 enum pipe pipe, u32 val)
1428{
1429 if ((val & ADPA_DAC_ENABLE) == 0)
1430 return false;
2d1fe073 1431 if (HAS_PCH_CPT(dev_priv)) {
1519b995
KP
1432 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1433 return false;
1434 } else {
1435 if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
1436 return false;
1437 }
1438 return true;
1439}
1440
291906f1 1441static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
f0f59a00
VS
1442 enum pipe pipe, i915_reg_t reg,
1443 u32 port_sel)
291906f1 1444{
47a05eca 1445 u32 val = I915_READ(reg);
e2c719b7 1446 I915_STATE_WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
291906f1 1447 "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
f0f59a00 1448 i915_mmio_reg_offset(reg), pipe_name(pipe));
de9a35ab 1449
2d1fe073 1450 I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && (val & DP_PORT_EN) == 0
75c5da27 1451 && (val & DP_PIPEB_SELECT),
de9a35ab 1452 "IBX PCH dp port still using transcoder B\n");
291906f1
JB
1453}
1454
1455static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
f0f59a00 1456 enum pipe pipe, i915_reg_t reg)
291906f1 1457{
47a05eca 1458 u32 val = I915_READ(reg);
e2c719b7 1459 I915_STATE_WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
23c99e77 1460 "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
f0f59a00 1461 i915_mmio_reg_offset(reg), pipe_name(pipe));
de9a35ab 1462
2d1fe073 1463 I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && (val & SDVO_ENABLE) == 0
75c5da27 1464 && (val & SDVO_PIPE_B_SELECT),
de9a35ab 1465 "IBX PCH hdmi port still using transcoder B\n");
291906f1
JB
1466}
1467
1468static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
1469 enum pipe pipe)
1470{
291906f1 1471 u32 val;
291906f1 1472
f0575e92
KP
1473 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
1474 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
1475 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
291906f1 1476
649636ef 1477 val = I915_READ(PCH_ADPA);
e2c719b7 1478 I915_STATE_WARN(adpa_pipe_enabled(dev_priv, pipe, val),
291906f1 1479 "PCH VGA enabled on transcoder %c, should be disabled\n",
9db4a9c7 1480 pipe_name(pipe));
291906f1 1481
649636ef 1482 val = I915_READ(PCH_LVDS);
e2c719b7 1483 I915_STATE_WARN(lvds_pipe_enabled(dev_priv, pipe, val),
291906f1 1484 "PCH LVDS enabled on transcoder %c, should be disabled\n",
9db4a9c7 1485 pipe_name(pipe));
291906f1 1486
e2debe91
PZ
1487 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
1488 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
1489 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
291906f1
JB
1490}
1491
cd2d34d9
VS
1492static void _vlv_enable_pll(struct intel_crtc *crtc,
1493 const struct intel_crtc_state *pipe_config)
1494{
1495 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1496 enum pipe pipe = crtc->pipe;
1497
1498 I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
1499 POSTING_READ(DPLL(pipe));
1500 udelay(150);
1501
2c30b43b
CW
1502 if (intel_wait_for_register(dev_priv,
1503 DPLL(pipe),
1504 DPLL_LOCK_VLV,
1505 DPLL_LOCK_VLV,
1506 1))
cd2d34d9
VS
1507 DRM_ERROR("DPLL %d failed to lock\n", pipe);
1508}
1509
d288f65f 1510static void vlv_enable_pll(struct intel_crtc *crtc,
5cec258b 1511 const struct intel_crtc_state *pipe_config)
87442f73 1512{
cd2d34d9 1513 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
8bd3f301 1514 enum pipe pipe = crtc->pipe;
87442f73 1515
8bd3f301 1516 assert_pipe_disabled(dev_priv, pipe);
87442f73 1517
87442f73 1518 /* PLL is protected by panel, make sure we can write it */
7d1a83cb 1519 assert_panel_unlocked(dev_priv, pipe);
87442f73 1520
cd2d34d9
VS
1521 if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
1522 _vlv_enable_pll(crtc, pipe_config);
426115cf 1523
8bd3f301
VS
1524 I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
1525 POSTING_READ(DPLL_MD(pipe));
87442f73
DV
1526}
1527
cd2d34d9
VS
1528
1529static void _chv_enable_pll(struct intel_crtc *crtc,
1530 const struct intel_crtc_state *pipe_config)
9d556c99 1531{
cd2d34d9 1532 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
8bd3f301 1533 enum pipe pipe = crtc->pipe;
9d556c99 1534 enum dpio_channel port = vlv_pipe_to_channel(pipe);
9d556c99
CML
1535 u32 tmp;
1536
a580516d 1537 mutex_lock(&dev_priv->sb_lock);
9d556c99
CML
1538
1539 /* Enable back the 10bit clock to display controller */
1540 tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1541 tmp |= DPIO_DCLKP_EN;
1542 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
1543
54433e91
VS
1544 mutex_unlock(&dev_priv->sb_lock);
1545
9d556c99
CML
1546 /*
1547 * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
1548 */
1549 udelay(1);
1550
1551 /* Enable PLL */
d288f65f 1552 I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
9d556c99
CML
1553
1554 /* Check PLL is locked */
6b18826a
CW
1555 if (intel_wait_for_register(dev_priv,
1556 DPLL(pipe), DPLL_LOCK_VLV, DPLL_LOCK_VLV,
1557 1))
9d556c99 1558 DRM_ERROR("PLL %d failed to lock\n", pipe);
cd2d34d9
VS
1559}
1560
1561static void chv_enable_pll(struct intel_crtc *crtc,
1562 const struct intel_crtc_state *pipe_config)
1563{
1564 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1565 enum pipe pipe = crtc->pipe;
1566
1567 assert_pipe_disabled(dev_priv, pipe);
1568
1569 /* PLL is protected by panel, make sure we can write it */
1570 assert_panel_unlocked(dev_priv, pipe);
1571
1572 if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
1573 _chv_enable_pll(crtc, pipe_config);
9d556c99 1574
c231775c
VS
1575 if (pipe != PIPE_A) {
1576 /*
1577 * WaPixelRepeatModeFixForC0:chv
1578 *
1579 * DPLLCMD is AWOL. Use chicken bits to propagate
1580 * the value from DPLLBMD to either pipe B or C.
1581 */
1582 I915_WRITE(CBR4_VLV, pipe == PIPE_B ? CBR_DPLLBMD_PIPE_B : CBR_DPLLBMD_PIPE_C);
1583 I915_WRITE(DPLL_MD(PIPE_B), pipe_config->dpll_hw_state.dpll_md);
1584 I915_WRITE(CBR4_VLV, 0);
1585 dev_priv->chv_dpll_md[pipe] = pipe_config->dpll_hw_state.dpll_md;
1586
1587 /*
1588 * DPLLB VGA mode also seems to cause problems.
1589 * We should always have it disabled.
1590 */
1591 WARN_ON((I915_READ(DPLL(PIPE_B)) & DPLL_VGA_MODE_DIS) == 0);
1592 } else {
1593 I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
1594 POSTING_READ(DPLL_MD(pipe));
1595 }
9d556c99
CML
1596}
1597
1c4e0274
VS
1598static int intel_num_dvo_pipes(struct drm_device *dev)
1599{
1600 struct intel_crtc *crtc;
1601 int count = 0;
1602
2d84d2b3 1603 for_each_intel_crtc(dev, crtc) {
3538b9df 1604 count += crtc->base.state->active &&
2d84d2b3
VS
1605 intel_crtc_has_type(crtc->config, INTEL_OUTPUT_DVO);
1606 }
1c4e0274
VS
1607
1608 return count;
1609}
1610
66e3d5c0 1611static void i9xx_enable_pll(struct intel_crtc *crtc)
63d7bbe9 1612{
66e3d5c0 1613 struct drm_device *dev = crtc->base.dev;
fac5e23e 1614 struct drm_i915_private *dev_priv = to_i915(dev);
f0f59a00 1615 i915_reg_t reg = DPLL(crtc->pipe);
6e3c9717 1616 u32 dpll = crtc->config->dpll_hw_state.dpll;
63d7bbe9 1617
66e3d5c0 1618 assert_pipe_disabled(dev_priv, crtc->pipe);
58c6eaa2 1619
63d7bbe9 1620 /* PLL is protected by panel, make sure we can write it */
66e3d5c0
DV
1621 if (IS_MOBILE(dev) && !IS_I830(dev))
1622 assert_panel_unlocked(dev_priv, crtc->pipe);
63d7bbe9 1623
1c4e0274
VS
1624 /* Enable DVO 2x clock on both PLLs if necessary */
1625 if (IS_I830(dev) && intel_num_dvo_pipes(dev) > 0) {
1626 /*
1627 * It appears to be important that we don't enable this
1628 * for the current pipe before otherwise configuring the
1629 * PLL. No idea how this should be handled if multiple
1630 * DVO outputs are enabled simultaneosly.
1631 */
1632 dpll |= DPLL_DVO_2X_MODE;
1633 I915_WRITE(DPLL(!crtc->pipe),
1634 I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
1635 }
66e3d5c0 1636
c2b63374
VS
1637 /*
1638 * Apparently we need to have VGA mode enabled prior to changing
1639 * the P1/P2 dividers. Otherwise the DPLL will keep using the old
1640 * dividers, even though the register value does change.
1641 */
1642 I915_WRITE(reg, 0);
1643
8e7a65aa
VS
1644 I915_WRITE(reg, dpll);
1645
66e3d5c0
DV
1646 /* Wait for the clocks to stabilize. */
1647 POSTING_READ(reg);
1648 udelay(150);
1649
1650 if (INTEL_INFO(dev)->gen >= 4) {
1651 I915_WRITE(DPLL_MD(crtc->pipe),
6e3c9717 1652 crtc->config->dpll_hw_state.dpll_md);
66e3d5c0
DV
1653 } else {
1654 /* The pixel multiplier can only be updated once the
1655 * DPLL is enabled and the clocks are stable.
1656 *
1657 * So write it again.
1658 */
1659 I915_WRITE(reg, dpll);
1660 }
63d7bbe9
JB
1661
1662 /* We do this three times for luck */
66e3d5c0 1663 I915_WRITE(reg, dpll);
63d7bbe9
JB
1664 POSTING_READ(reg);
1665 udelay(150); /* wait for warmup */
66e3d5c0 1666 I915_WRITE(reg, dpll);
63d7bbe9
JB
1667 POSTING_READ(reg);
1668 udelay(150); /* wait for warmup */
66e3d5c0 1669 I915_WRITE(reg, dpll);
63d7bbe9
JB
1670 POSTING_READ(reg);
1671 udelay(150); /* wait for warmup */
1672}
1673
1674/**
50b44a44 1675 * i9xx_disable_pll - disable a PLL
63d7bbe9
JB
1676 * @dev_priv: i915 private structure
1677 * @pipe: pipe PLL to disable
1678 *
1679 * Disable the PLL for @pipe, making sure the pipe is off first.
1680 *
1681 * Note! This is for pre-ILK only.
1682 */
1c4e0274 1683static void i9xx_disable_pll(struct intel_crtc *crtc)
63d7bbe9 1684{
1c4e0274 1685 struct drm_device *dev = crtc->base.dev;
fac5e23e 1686 struct drm_i915_private *dev_priv = to_i915(dev);
1c4e0274
VS
1687 enum pipe pipe = crtc->pipe;
1688
1689 /* Disable DVO 2x clock on both PLLs if necessary */
1690 if (IS_I830(dev) &&
2d84d2b3 1691 intel_crtc_has_type(crtc->config, INTEL_OUTPUT_DVO) &&
3538b9df 1692 !intel_num_dvo_pipes(dev)) {
1c4e0274
VS
1693 I915_WRITE(DPLL(PIPE_B),
1694 I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
1695 I915_WRITE(DPLL(PIPE_A),
1696 I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
1697 }
1698
b6b5d049
VS
1699 /* Don't disable pipe or pipe PLLs if needed */
1700 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1701 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
63d7bbe9
JB
1702 return;
1703
1704 /* Make sure the pipe isn't still relying on us */
1705 assert_pipe_disabled(dev_priv, pipe);
1706
b8afb911 1707 I915_WRITE(DPLL(pipe), DPLL_VGA_MODE_DIS);
50b44a44 1708 POSTING_READ(DPLL(pipe));
63d7bbe9
JB
1709}
1710
f6071166
JB
1711static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1712{
b8afb911 1713 u32 val;
f6071166
JB
1714
1715 /* Make sure the pipe isn't still relying on us */
1716 assert_pipe_disabled(dev_priv, pipe);
1717
03ed5cbf
VS
1718 val = DPLL_INTEGRATED_REF_CLK_VLV |
1719 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
1720 if (pipe != PIPE_A)
1721 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
1722
f6071166
JB
1723 I915_WRITE(DPLL(pipe), val);
1724 POSTING_READ(DPLL(pipe));
076ed3b2
CML
1725}
1726
1727static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1728{
d752048d 1729 enum dpio_channel port = vlv_pipe_to_channel(pipe);
076ed3b2
CML
1730 u32 val;
1731
a11b0703
VS
1732 /* Make sure the pipe isn't still relying on us */
1733 assert_pipe_disabled(dev_priv, pipe);
076ed3b2 1734
60bfe44f
VS
1735 val = DPLL_SSC_REF_CLK_CHV |
1736 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
a11b0703
VS
1737 if (pipe != PIPE_A)
1738 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
03ed5cbf 1739
a11b0703
VS
1740 I915_WRITE(DPLL(pipe), val);
1741 POSTING_READ(DPLL(pipe));
d752048d 1742
a580516d 1743 mutex_lock(&dev_priv->sb_lock);
d752048d
VS
1744
1745 /* Disable 10bit clock to display controller */
1746 val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1747 val &= ~DPIO_DCLKP_EN;
1748 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
1749
a580516d 1750 mutex_unlock(&dev_priv->sb_lock);
f6071166
JB
1751}
1752
e4607fcf 1753void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
9b6de0a1
VS
1754 struct intel_digital_port *dport,
1755 unsigned int expected_mask)
89b667f8
JB
1756{
1757 u32 port_mask;
f0f59a00 1758 i915_reg_t dpll_reg;
89b667f8 1759
e4607fcf
CML
1760 switch (dport->port) {
1761 case PORT_B:
89b667f8 1762 port_mask = DPLL_PORTB_READY_MASK;
00fc31b7 1763 dpll_reg = DPLL(0);
e4607fcf
CML
1764 break;
1765 case PORT_C:
89b667f8 1766 port_mask = DPLL_PORTC_READY_MASK;
00fc31b7 1767 dpll_reg = DPLL(0);
9b6de0a1 1768 expected_mask <<= 4;
00fc31b7
CML
1769 break;
1770 case PORT_D:
1771 port_mask = DPLL_PORTD_READY_MASK;
1772 dpll_reg = DPIO_PHY_STATUS;
e4607fcf
CML
1773 break;
1774 default:
1775 BUG();
1776 }
89b667f8 1777
370004d3
CW
1778 if (intel_wait_for_register(dev_priv,
1779 dpll_reg, port_mask, expected_mask,
1780 1000))
9b6de0a1
VS
1781 WARN(1, "timed out waiting for port %c ready: got 0x%x, expected 0x%x\n",
1782 port_name(dport->port), I915_READ(dpll_reg) & port_mask, expected_mask);
89b667f8
JB
1783}
1784
b8a4f404
PZ
1785static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1786 enum pipe pipe)
040484af 1787{
91c8a326 1788 struct drm_device *dev = &dev_priv->drm;
7c26e5c6 1789 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
e2b78267 1790 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
f0f59a00
VS
1791 i915_reg_t reg;
1792 uint32_t val, pipeconf_val;
040484af 1793
040484af 1794 /* Make sure PCH DPLL is enabled */
8106ddbd 1795 assert_shared_dpll_enabled(dev_priv, intel_crtc->config->shared_dpll);
040484af
JB
1796
1797 /* FDI must be feeding us bits for PCH ports */
1798 assert_fdi_tx_enabled(dev_priv, pipe);
1799 assert_fdi_rx_enabled(dev_priv, pipe);
1800
23670b32
DV
1801 if (HAS_PCH_CPT(dev)) {
1802 /* Workaround: Set the timing override bit before enabling the
1803 * pch transcoder. */
1804 reg = TRANS_CHICKEN2(pipe);
1805 val = I915_READ(reg);
1806 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
1807 I915_WRITE(reg, val);
59c859d6 1808 }
23670b32 1809
ab9412ba 1810 reg = PCH_TRANSCONF(pipe);
040484af 1811 val = I915_READ(reg);
5f7f726d 1812 pipeconf_val = I915_READ(PIPECONF(pipe));
e9bcff5c 1813
2d1fe073 1814 if (HAS_PCH_IBX(dev_priv)) {
e9bcff5c 1815 /*
c5de7c6f
VS
1816 * Make the BPC in transcoder be consistent with
1817 * that in pipeconf reg. For HDMI we must use 8bpc
1818 * here for both 8bpc and 12bpc.
e9bcff5c 1819 */
dfd07d72 1820 val &= ~PIPECONF_BPC_MASK;
2d84d2b3 1821 if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_HDMI))
c5de7c6f
VS
1822 val |= PIPECONF_8BPC;
1823 else
1824 val |= pipeconf_val & PIPECONF_BPC_MASK;
e9bcff5c 1825 }
5f7f726d
PZ
1826
1827 val &= ~TRANS_INTERLACE_MASK;
1828 if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
2d1fe073 1829 if (HAS_PCH_IBX(dev_priv) &&
2d84d2b3 1830 intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
7c26e5c6
PZ
1831 val |= TRANS_LEGACY_INTERLACED_ILK;
1832 else
1833 val |= TRANS_INTERLACED;
5f7f726d
PZ
1834 else
1835 val |= TRANS_PROGRESSIVE;
1836
040484af 1837 I915_WRITE(reg, val | TRANS_ENABLE);
650fbd84
CW
1838 if (intel_wait_for_register(dev_priv,
1839 reg, TRANS_STATE_ENABLE, TRANS_STATE_ENABLE,
1840 100))
4bb6f1f3 1841 DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
040484af
JB
1842}
1843
8fb033d7 1844static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
937bb610 1845 enum transcoder cpu_transcoder)
040484af 1846{
8fb033d7 1847 u32 val, pipeconf_val;
8fb033d7 1848
8fb033d7 1849 /* FDI must be feeding us bits for PCH ports */
1a240d4d 1850 assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
937bb610 1851 assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
8fb033d7 1852
223a6fdf 1853 /* Workaround: set timing override bit. */
36c0d0cf 1854 val = I915_READ(TRANS_CHICKEN2(PIPE_A));
23670b32 1855 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
36c0d0cf 1856 I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
223a6fdf 1857
25f3ef11 1858 val = TRANS_ENABLE;
937bb610 1859 pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
8fb033d7 1860
9a76b1c6
PZ
1861 if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
1862 PIPECONF_INTERLACED_ILK)
a35f2679 1863 val |= TRANS_INTERLACED;
8fb033d7
PZ
1864 else
1865 val |= TRANS_PROGRESSIVE;
1866
ab9412ba 1867 I915_WRITE(LPT_TRANSCONF, val);
d9f96244
CW
1868 if (intel_wait_for_register(dev_priv,
1869 LPT_TRANSCONF,
1870 TRANS_STATE_ENABLE,
1871 TRANS_STATE_ENABLE,
1872 100))
937bb610 1873 DRM_ERROR("Failed to enable PCH transcoder\n");
8fb033d7
PZ
1874}
1875
b8a4f404
PZ
1876static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
1877 enum pipe pipe)
040484af 1878{
91c8a326 1879 struct drm_device *dev = &dev_priv->drm;
f0f59a00
VS
1880 i915_reg_t reg;
1881 uint32_t val;
040484af
JB
1882
1883 /* FDI relies on the transcoder */
1884 assert_fdi_tx_disabled(dev_priv, pipe);
1885 assert_fdi_rx_disabled(dev_priv, pipe);
1886
291906f1
JB
1887 /* Ports must be off as well */
1888 assert_pch_ports_disabled(dev_priv, pipe);
1889
ab9412ba 1890 reg = PCH_TRANSCONF(pipe);
040484af
JB
1891 val = I915_READ(reg);
1892 val &= ~TRANS_ENABLE;
1893 I915_WRITE(reg, val);
1894 /* wait for PCH transcoder off, transcoder state */
a7d04662
CW
1895 if (intel_wait_for_register(dev_priv,
1896 reg, TRANS_STATE_ENABLE, 0,
1897 50))
4bb6f1f3 1898 DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
23670b32 1899
c465613b 1900 if (HAS_PCH_CPT(dev)) {
23670b32
DV
1901 /* Workaround: Clear the timing override chicken bit again. */
1902 reg = TRANS_CHICKEN2(pipe);
1903 val = I915_READ(reg);
1904 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
1905 I915_WRITE(reg, val);
1906 }
040484af
JB
1907}
1908
ab4d966c 1909static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
8fb033d7 1910{
8fb033d7
PZ
1911 u32 val;
1912
ab9412ba 1913 val = I915_READ(LPT_TRANSCONF);
8fb033d7 1914 val &= ~TRANS_ENABLE;
ab9412ba 1915 I915_WRITE(LPT_TRANSCONF, val);
8fb033d7 1916 /* wait for PCH transcoder off, transcoder state */
dfdb4749
CW
1917 if (intel_wait_for_register(dev_priv,
1918 LPT_TRANSCONF, TRANS_STATE_ENABLE, 0,
1919 50))
8a52fd9f 1920 DRM_ERROR("Failed to disable PCH transcoder\n");
223a6fdf
PZ
1921
1922 /* Workaround: clear timing override bit. */
36c0d0cf 1923 val = I915_READ(TRANS_CHICKEN2(PIPE_A));
23670b32 1924 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
36c0d0cf 1925 I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
040484af
JB
1926}
1927
b24e7179 1928/**
309cfea8 1929 * intel_enable_pipe - enable a pipe, asserting requirements
0372264a 1930 * @crtc: crtc responsible for the pipe
b24e7179 1931 *
0372264a 1932 * Enable @crtc's pipe, making sure that various hardware specific requirements
b24e7179 1933 * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
b24e7179 1934 */
e1fdc473 1935static void intel_enable_pipe(struct intel_crtc *crtc)
b24e7179 1936{
0372264a 1937 struct drm_device *dev = crtc->base.dev;
fac5e23e 1938 struct drm_i915_private *dev_priv = to_i915(dev);
0372264a 1939 enum pipe pipe = crtc->pipe;
1a70a728 1940 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
1a240d4d 1941 enum pipe pch_transcoder;
f0f59a00 1942 i915_reg_t reg;
b24e7179
JB
1943 u32 val;
1944
9e2ee2dd
VS
1945 DRM_DEBUG_KMS("enabling pipe %c\n", pipe_name(pipe));
1946
58c6eaa2 1947 assert_planes_disabled(dev_priv, pipe);
93ce0ba6 1948 assert_cursor_disabled(dev_priv, pipe);
58c6eaa2
DV
1949 assert_sprites_disabled(dev_priv, pipe);
1950
2d1fe073 1951 if (HAS_PCH_LPT(dev_priv))
cc391bbb
PZ
1952 pch_transcoder = TRANSCODER_A;
1953 else
1954 pch_transcoder = pipe;
1955
b24e7179
JB
1956 /*
1957 * A pipe without a PLL won't actually be able to drive bits from
1958 * a plane. On ILK+ the pipe PLLs are integrated, so we don't
1959 * need the check.
1960 */
2d1fe073 1961 if (HAS_GMCH_DISPLAY(dev_priv))
d7edc4e5 1962 if (intel_crtc_has_type(crtc->config, INTEL_OUTPUT_DSI))
23538ef1
JN
1963 assert_dsi_pll_enabled(dev_priv);
1964 else
1965 assert_pll_enabled(dev_priv, pipe);
040484af 1966 else {
6e3c9717 1967 if (crtc->config->has_pch_encoder) {
040484af 1968 /* if driving the PCH, we need FDI enabled */
cc391bbb 1969 assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
1a240d4d
DV
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{
2d7a215f
VS
2141 if (intel_rotation_90_or_270(rotation)) {
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
2d7a215f
VS
2149static void
2150intel_fill_fb_info(struct drm_i915_private *dev_priv,
2151 struct drm_framebuffer *fb)
2152{
2153 struct intel_rotation_info *info = &to_intel_framebuffer(fb)->rot_info;
2154 unsigned int tile_size, tile_width, tile_height, cpp;
50470bb0 2155
d9b3288e
VS
2156 tile_size = intel_tile_size(dev_priv);
2157
2158 cpp = drm_format_plane_cpp(fb->pixel_format, 0);
8d0deca8
VS
2159 intel_tile_dims(dev_priv, &tile_width, &tile_height,
2160 fb->modifier[0], cpp);
d9b3288e 2161
1663b9d6
VS
2162 info->plane[0].width = DIV_ROUND_UP(fb->pitches[0], tile_width * cpp);
2163 info->plane[0].height = DIV_ROUND_UP(fb->height, tile_height);
84fe03f7 2164
89e3e142 2165 if (info->pixel_format == DRM_FORMAT_NV12) {
832be82f 2166 cpp = drm_format_plane_cpp(fb->pixel_format, 1);
8d0deca8
VS
2167 intel_tile_dims(dev_priv, &tile_width, &tile_height,
2168 fb->modifier[1], cpp);
d9b3288e 2169
2d7a215f 2170 info->uv_offset = fb->offsets[1];
1663b9d6
VS
2171 info->plane[1].width = DIV_ROUND_UP(fb->pitches[1], tile_width * cpp);
2172 info->plane[1].height = DIV_ROUND_UP(fb->height / 2, tile_height);
89e3e142 2173 }
f64b98cd
TU
2174}
2175
603525d7 2176static unsigned int intel_linear_alignment(const struct drm_i915_private *dev_priv)
4e9a86b6
VS
2177{
2178 if (INTEL_INFO(dev_priv)->gen >= 9)
2179 return 256 * 1024;
985b8bb4 2180 else if (IS_BROADWATER(dev_priv) || IS_CRESTLINE(dev_priv) ||
666a4537 2181 IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
4e9a86b6
VS
2182 return 128 * 1024;
2183 else if (INTEL_INFO(dev_priv)->gen >= 4)
2184 return 4 * 1024;
2185 else
44c5905e 2186 return 0;
4e9a86b6
VS
2187}
2188
603525d7
VS
2189static unsigned int intel_surf_alignment(const struct drm_i915_private *dev_priv,
2190 uint64_t fb_modifier)
2191{
2192 switch (fb_modifier) {
2193 case DRM_FORMAT_MOD_NONE:
2194 return intel_linear_alignment(dev_priv);
2195 case I915_FORMAT_MOD_X_TILED:
2196 if (INTEL_INFO(dev_priv)->gen >= 9)
2197 return 256 * 1024;
2198 return 0;
2199 case I915_FORMAT_MOD_Y_TILED:
2200 case I915_FORMAT_MOD_Yf_TILED:
2201 return 1 * 1024 * 1024;
2202 default:
2203 MISSING_CASE(fb_modifier);
2204 return 0;
2205 }
2206}
2207
127bd2ac 2208int
3465c580
VS
2209intel_pin_and_fence_fb_obj(struct drm_framebuffer *fb,
2210 unsigned int rotation)
6b95a207 2211{
850c4cdc 2212 struct drm_device *dev = fb->dev;
fac5e23e 2213 struct drm_i915_private *dev_priv = to_i915(dev);
850c4cdc 2214 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
f64b98cd 2215 struct i915_ggtt_view view;
6b95a207
KH
2216 u32 alignment;
2217 int ret;
2218
ebcdd39e
MR
2219 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
2220
603525d7 2221 alignment = intel_surf_alignment(dev_priv, fb->modifier[0]);
6b95a207 2222
3465c580 2223 intel_fill_fb_ggtt_view(&view, fb, rotation);
f64b98cd 2224
693db184
CW
2225 /* Note that the w/a also requires 64 PTE of padding following the
2226 * bo. We currently fill all unused PTE with the shadow page and so
2227 * we should always have valid PTE following the scanout preventing
2228 * the VT-d warning.
2229 */
48f112fe 2230 if (intel_scanout_needs_vtd_wa(dev_priv) && alignment < 256 * 1024)
693db184
CW
2231 alignment = 256 * 1024;
2232
d6dd6843
PZ
2233 /*
2234 * Global gtt pte registers are special registers which actually forward
2235 * writes to a chunk of system memory. Which means that there is no risk
2236 * that the register values disappear as soon as we call
2237 * intel_runtime_pm_put(), so it is correct to wrap only the
2238 * pin/unpin/fence and not more.
2239 */
2240 intel_runtime_pm_get(dev_priv);
2241
7580d774
ML
2242 ret = i915_gem_object_pin_to_display_plane(obj, alignment,
2243 &view);
48b956c5 2244 if (ret)
b26a6b35 2245 goto err_pm;
6b95a207
KH
2246
2247 /* Install a fence for tiled scan-out. Pre-i965 always needs a
2248 * fence, whereas 965+ only requires a fence if using
2249 * framebuffer compression. For simplicity, we always install
2250 * a fence as the cost is not that onerous.
2251 */
9807216f
VK
2252 if (view.type == I915_GGTT_VIEW_NORMAL) {
2253 ret = i915_gem_object_get_fence(obj);
2254 if (ret == -EDEADLK) {
2255 /*
2256 * -EDEADLK means there are no free fences
2257 * no pending flips.
2258 *
2259 * This is propagated to atomic, but it uses
2260 * -EDEADLK to force a locking recovery, so
2261 * change the returned error to -EBUSY.
2262 */
2263 ret = -EBUSY;
2264 goto err_unpin;
2265 } else if (ret)
2266 goto err_unpin;
1690e1eb 2267
9807216f
VK
2268 i915_gem_object_pin_fence(obj);
2269 }
6b95a207 2270
d6dd6843 2271 intel_runtime_pm_put(dev_priv);
6b95a207 2272 return 0;
48b956c5
CW
2273
2274err_unpin:
f64b98cd 2275 i915_gem_object_unpin_from_display_plane(obj, &view);
b26a6b35 2276err_pm:
d6dd6843 2277 intel_runtime_pm_put(dev_priv);
48b956c5 2278 return ret;
6b95a207
KH
2279}
2280
fb4b8ce1 2281void intel_unpin_fb_obj(struct drm_framebuffer *fb, unsigned int rotation)
1690e1eb 2282{
82bc3b2d 2283 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
f64b98cd 2284 struct i915_ggtt_view view;
82bc3b2d 2285
ebcdd39e
MR
2286 WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
2287
3465c580 2288 intel_fill_fb_ggtt_view(&view, fb, rotation);
f64b98cd 2289
9807216f
VK
2290 if (view.type == I915_GGTT_VIEW_NORMAL)
2291 i915_gem_object_unpin_fence(obj);
2292
f64b98cd 2293 i915_gem_object_unpin_from_display_plane(obj, &view);
1690e1eb
CW
2294}
2295
29cf9491
VS
2296/*
2297 * Adjust the tile offset by moving the difference into
2298 * the x/y offsets.
2299 *
2300 * Input tile dimensions and pitch must already be
2301 * rotated to match x and y, and in pixel units.
2302 */
2303static u32 intel_adjust_tile_offset(int *x, int *y,
2304 unsigned int tile_width,
2305 unsigned int tile_height,
2306 unsigned int tile_size,
2307 unsigned int pitch_tiles,
2308 u32 old_offset,
2309 u32 new_offset)
2310{
2311 unsigned int tiles;
2312
2313 WARN_ON(old_offset & (tile_size - 1));
2314 WARN_ON(new_offset & (tile_size - 1));
2315 WARN_ON(new_offset > old_offset);
2316
2317 tiles = (old_offset - new_offset) / tile_size;
2318
2319 *y += tiles / pitch_tiles * tile_height;
2320 *x += tiles % pitch_tiles * tile_width;
2321
2322 return new_offset;
2323}
2324
8d0deca8
VS
2325/*
2326 * Computes the linear offset to the base tile and adjusts
2327 * x, y. bytes per pixel is assumed to be a power-of-two.
2328 *
2329 * In the 90/270 rotated case, x and y are assumed
2330 * to be already rotated to match the rotated GTT view, and
2331 * pitch is the tile_height aligned framebuffer height.
2332 */
4f2d9934
VS
2333u32 intel_compute_tile_offset(int *x, int *y,
2334 const struct drm_framebuffer *fb, int plane,
8d0deca8
VS
2335 unsigned int pitch,
2336 unsigned int rotation)
c2c75131 2337{
4f2d9934
VS
2338 const struct drm_i915_private *dev_priv = to_i915(fb->dev);
2339 uint64_t fb_modifier = fb->modifier[plane];
2340 unsigned int cpp = drm_format_plane_cpp(fb->pixel_format, plane);
29cf9491
VS
2341 u32 offset, offset_aligned, alignment;
2342
2343 alignment = intel_surf_alignment(dev_priv, fb_modifier);
2344 if (alignment)
2345 alignment--;
2346
b5c65338 2347 if (fb_modifier != DRM_FORMAT_MOD_NONE) {
8d0deca8
VS
2348 unsigned int tile_size, tile_width, tile_height;
2349 unsigned int tile_rows, tiles, pitch_tiles;
c2c75131 2350
d843310d 2351 tile_size = intel_tile_size(dev_priv);
8d0deca8
VS
2352 intel_tile_dims(dev_priv, &tile_width, &tile_height,
2353 fb_modifier, cpp);
2354
2355 if (intel_rotation_90_or_270(rotation)) {
2356 pitch_tiles = pitch / tile_height;
2357 swap(tile_width, tile_height);
2358 } else {
2359 pitch_tiles = pitch / (tile_width * cpp);
2360 }
d843310d
VS
2361
2362 tile_rows = *y / tile_height;
2363 *y %= tile_height;
c2c75131 2364
8d0deca8
VS
2365 tiles = *x / tile_width;
2366 *x %= tile_width;
bc752862 2367
29cf9491
VS
2368 offset = (tile_rows * pitch_tiles + tiles) * tile_size;
2369 offset_aligned = offset & ~alignment;
bc752862 2370
29cf9491
VS
2371 intel_adjust_tile_offset(x, y, tile_width, tile_height,
2372 tile_size, pitch_tiles,
2373 offset, offset_aligned);
2374 } else {
bc752862 2375 offset = *y * pitch + *x * cpp;
29cf9491
VS
2376 offset_aligned = offset & ~alignment;
2377
4e9a86b6
VS
2378 *y = (offset & alignment) / pitch;
2379 *x = ((offset & alignment) - *y * pitch) / cpp;
bc752862 2380 }
29cf9491
VS
2381
2382 return offset_aligned;
c2c75131
DV
2383}
2384
b35d63fa 2385static int i9xx_format_to_fourcc(int format)
46f297fb
JB
2386{
2387 switch (format) {
2388 case DISPPLANE_8BPP:
2389 return DRM_FORMAT_C8;
2390 case DISPPLANE_BGRX555:
2391 return DRM_FORMAT_XRGB1555;
2392 case DISPPLANE_BGRX565:
2393 return DRM_FORMAT_RGB565;
2394 default:
2395 case DISPPLANE_BGRX888:
2396 return DRM_FORMAT_XRGB8888;
2397 case DISPPLANE_RGBX888:
2398 return DRM_FORMAT_XBGR8888;
2399 case DISPPLANE_BGRX101010:
2400 return DRM_FORMAT_XRGB2101010;
2401 case DISPPLANE_RGBX101010:
2402 return DRM_FORMAT_XBGR2101010;
2403 }
2404}
2405
bc8d7dff
DL
2406static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
2407{
2408 switch (format) {
2409 case PLANE_CTL_FORMAT_RGB_565:
2410 return DRM_FORMAT_RGB565;
2411 default:
2412 case PLANE_CTL_FORMAT_XRGB_8888:
2413 if (rgb_order) {
2414 if (alpha)
2415 return DRM_FORMAT_ABGR8888;
2416 else
2417 return DRM_FORMAT_XBGR8888;
2418 } else {
2419 if (alpha)
2420 return DRM_FORMAT_ARGB8888;
2421 else
2422 return DRM_FORMAT_XRGB8888;
2423 }
2424 case PLANE_CTL_FORMAT_XRGB_2101010:
2425 if (rgb_order)
2426 return DRM_FORMAT_XBGR2101010;
2427 else
2428 return DRM_FORMAT_XRGB2101010;
2429 }
2430}
2431
5724dbd1 2432static bool
f6936e29
DV
2433intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
2434 struct intel_initial_plane_config *plane_config)
46f297fb
JB
2435{
2436 struct drm_device *dev = crtc->base.dev;
3badb49f 2437 struct drm_i915_private *dev_priv = to_i915(dev);
72e96d64 2438 struct i915_ggtt *ggtt = &dev_priv->ggtt;
46f297fb
JB
2439 struct drm_i915_gem_object *obj = NULL;
2440 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
2d14030b 2441 struct drm_framebuffer *fb = &plane_config->fb->base;
f37b5c2b
DV
2442 u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
2443 u32 size_aligned = round_up(plane_config->base + plane_config->size,
2444 PAGE_SIZE);
2445
2446 size_aligned -= base_aligned;
46f297fb 2447
ff2652ea
CW
2448 if (plane_config->size == 0)
2449 return false;
2450
3badb49f
PZ
2451 /* If the FB is too big, just don't use it since fbdev is not very
2452 * important and we should probably use that space with FBC or other
2453 * features. */
72e96d64 2454 if (size_aligned * 2 > ggtt->stolen_usable_size)
3badb49f
PZ
2455 return false;
2456
12c83d99
TU
2457 mutex_lock(&dev->struct_mutex);
2458
f37b5c2b
DV
2459 obj = i915_gem_object_create_stolen_for_preallocated(dev,
2460 base_aligned,
2461 base_aligned,
2462 size_aligned);
12c83d99
TU
2463 if (!obj) {
2464 mutex_unlock(&dev->struct_mutex);
484b41dd 2465 return false;
12c83d99 2466 }
46f297fb 2467
49af449b
DL
2468 obj->tiling_mode = plane_config->tiling;
2469 if (obj->tiling_mode == I915_TILING_X)
6bf129df 2470 obj->stride = fb->pitches[0];
46f297fb 2471
6bf129df
DL
2472 mode_cmd.pixel_format = fb->pixel_format;
2473 mode_cmd.width = fb->width;
2474 mode_cmd.height = fb->height;
2475 mode_cmd.pitches[0] = fb->pitches[0];
18c5247e
DV
2476 mode_cmd.modifier[0] = fb->modifier[0];
2477 mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
46f297fb 2478
6bf129df 2479 if (intel_framebuffer_init(dev, to_intel_framebuffer(fb),
484b41dd 2480 &mode_cmd, obj)) {
46f297fb
JB
2481 DRM_DEBUG_KMS("intel fb init failed\n");
2482 goto out_unref_obj;
2483 }
12c83d99 2484
46f297fb 2485 mutex_unlock(&dev->struct_mutex);
484b41dd 2486
f6936e29 2487 DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
484b41dd 2488 return true;
46f297fb
JB
2489
2490out_unref_obj:
2491 drm_gem_object_unreference(&obj->base);
2492 mutex_unlock(&dev->struct_mutex);
484b41dd
JB
2493 return false;
2494}
2495
5a21b665
DV
2496/* Update plane->state->fb to match plane->fb after driver-internal updates */
2497static void
2498update_state_fb(struct drm_plane *plane)
2499{
2500 if (plane->fb == plane->state->fb)
2501 return;
2502
2503 if (plane->state->fb)
2504 drm_framebuffer_unreference(plane->state->fb);
2505 plane->state->fb = plane->fb;
2506 if (plane->state->fb)
2507 drm_framebuffer_reference(plane->state->fb);
2508}
2509
5724dbd1 2510static void
f6936e29
DV
2511intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
2512 struct intel_initial_plane_config *plane_config)
484b41dd
JB
2513{
2514 struct drm_device *dev = intel_crtc->base.dev;
fac5e23e 2515 struct drm_i915_private *dev_priv = to_i915(dev);
484b41dd
JB
2516 struct drm_crtc *c;
2517 struct intel_crtc *i;
2ff8fde1 2518 struct drm_i915_gem_object *obj;
88595ac9 2519 struct drm_plane *primary = intel_crtc->base.primary;
be5651f2 2520 struct drm_plane_state *plane_state = primary->state;
200757f5
MR
2521 struct drm_crtc_state *crtc_state = intel_crtc->base.state;
2522 struct intel_plane *intel_plane = to_intel_plane(primary);
0a8d8a86
MR
2523 struct intel_plane_state *intel_state =
2524 to_intel_plane_state(plane_state);
88595ac9 2525 struct drm_framebuffer *fb;
484b41dd 2526
2d14030b 2527 if (!plane_config->fb)
484b41dd
JB
2528 return;
2529
f6936e29 2530 if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
88595ac9
DV
2531 fb = &plane_config->fb->base;
2532 goto valid_fb;
f55548b5 2533 }
484b41dd 2534
2d14030b 2535 kfree(plane_config->fb);
484b41dd
JB
2536
2537 /*
2538 * Failed to alloc the obj, check to see if we should share
2539 * an fb with another CRTC instead
2540 */
70e1e0ec 2541 for_each_crtc(dev, c) {
484b41dd
JB
2542 i = to_intel_crtc(c);
2543
2544 if (c == &intel_crtc->base)
2545 continue;
2546
2ff8fde1
MR
2547 if (!i->active)
2548 continue;
2549
88595ac9
DV
2550 fb = c->primary->fb;
2551 if (!fb)
484b41dd
JB
2552 continue;
2553
88595ac9 2554 obj = intel_fb_obj(fb);
2ff8fde1 2555 if (i915_gem_obj_ggtt_offset(obj) == plane_config->base) {
88595ac9
DV
2556 drm_framebuffer_reference(fb);
2557 goto valid_fb;
484b41dd
JB
2558 }
2559 }
88595ac9 2560
200757f5
MR
2561 /*
2562 * We've failed to reconstruct the BIOS FB. Current display state
2563 * indicates that the primary plane is visible, but has a NULL FB,
2564 * which will lead to problems later if we don't fix it up. The
2565 * simplest solution is to just disable the primary plane now and
2566 * pretend the BIOS never had it enabled.
2567 */
2568 to_intel_plane_state(plane_state)->visible = false;
2569 crtc_state->plane_mask &= ~(1 << drm_plane_index(primary));
2622a081 2570 intel_pre_disable_primary_noatomic(&intel_crtc->base);
200757f5
MR
2571 intel_plane->disable_plane(primary, &intel_crtc->base);
2572
88595ac9
DV
2573 return;
2574
2575valid_fb:
f44e2659
VS
2576 plane_state->src_x = 0;
2577 plane_state->src_y = 0;
be5651f2
ML
2578 plane_state->src_w = fb->width << 16;
2579 plane_state->src_h = fb->height << 16;
2580
f44e2659
VS
2581 plane_state->crtc_x = 0;
2582 plane_state->crtc_y = 0;
be5651f2
ML
2583 plane_state->crtc_w = fb->width;
2584 plane_state->crtc_h = fb->height;
2585
0a8d8a86
MR
2586 intel_state->src.x1 = plane_state->src_x;
2587 intel_state->src.y1 = plane_state->src_y;
2588 intel_state->src.x2 = plane_state->src_x + plane_state->src_w;
2589 intel_state->src.y2 = plane_state->src_y + plane_state->src_h;
2590 intel_state->dst.x1 = plane_state->crtc_x;
2591 intel_state->dst.y1 = plane_state->crtc_y;
2592 intel_state->dst.x2 = plane_state->crtc_x + plane_state->crtc_w;
2593 intel_state->dst.y2 = plane_state->crtc_y + plane_state->crtc_h;
2594
88595ac9
DV
2595 obj = intel_fb_obj(fb);
2596 if (obj->tiling_mode != I915_TILING_NONE)
2597 dev_priv->preserve_bios_swizzle = true;
2598
be5651f2
ML
2599 drm_framebuffer_reference(fb);
2600 primary->fb = primary->state->fb = fb;
36750f28 2601 primary->crtc = primary->state->crtc = &intel_crtc->base;
36750f28 2602 intel_crtc->base.state->plane_mask |= (1 << drm_plane_index(primary));
a9ff8714 2603 obj->frontbuffer_bits |= to_intel_plane(primary)->frontbuffer_bit;
46f297fb
JB
2604}
2605
a8d201af
ML
2606static void i9xx_update_primary_plane(struct drm_plane *primary,
2607 const struct intel_crtc_state *crtc_state,
2608 const struct intel_plane_state *plane_state)
81255565 2609{
a8d201af 2610 struct drm_device *dev = primary->dev;
fac5e23e 2611 struct drm_i915_private *dev_priv = to_i915(dev);
a8d201af
ML
2612 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
2613 struct drm_framebuffer *fb = plane_state->base.fb;
2614 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
81255565 2615 int plane = intel_crtc->plane;
54ea9da8 2616 u32 linear_offset;
81255565 2617 u32 dspcntr;
f0f59a00 2618 i915_reg_t reg = DSPCNTR(plane);
8d0deca8 2619 unsigned int rotation = plane_state->base.rotation;
ac484963 2620 int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
54ea9da8
VS
2621 int x = plane_state->src.x1 >> 16;
2622 int y = plane_state->src.y1 >> 16;
c9ba6fad 2623
f45651ba
VS
2624 dspcntr = DISPPLANE_GAMMA_ENABLE;
2625
fdd508a6 2626 dspcntr |= DISPLAY_PLANE_ENABLE;
f45651ba
VS
2627
2628 if (INTEL_INFO(dev)->gen < 4) {
2629 if (intel_crtc->pipe == PIPE_B)
2630 dspcntr |= DISPPLANE_SEL_PIPE_B;
2631
2632 /* pipesrc and dspsize control the size that is scaled from,
2633 * which should always be the user's requested size.
2634 */
2635 I915_WRITE(DSPSIZE(plane),
a8d201af
ML
2636 ((crtc_state->pipe_src_h - 1) << 16) |
2637 (crtc_state->pipe_src_w - 1));
f45651ba 2638 I915_WRITE(DSPPOS(plane), 0);
c14b0485
VS
2639 } else if (IS_CHERRYVIEW(dev) && plane == PLANE_B) {
2640 I915_WRITE(PRIMSIZE(plane),
a8d201af
ML
2641 ((crtc_state->pipe_src_h - 1) << 16) |
2642 (crtc_state->pipe_src_w - 1));
c14b0485
VS
2643 I915_WRITE(PRIMPOS(plane), 0);
2644 I915_WRITE(PRIMCNSTALPHA(plane), 0);
f45651ba 2645 }
81255565 2646
57779d06
VS
2647 switch (fb->pixel_format) {
2648 case DRM_FORMAT_C8:
81255565
JB
2649 dspcntr |= DISPPLANE_8BPP;
2650 break;
57779d06 2651 case DRM_FORMAT_XRGB1555:
57779d06 2652 dspcntr |= DISPPLANE_BGRX555;
81255565 2653 break;
57779d06
VS
2654 case DRM_FORMAT_RGB565:
2655 dspcntr |= DISPPLANE_BGRX565;
2656 break;
2657 case DRM_FORMAT_XRGB8888:
57779d06
VS
2658 dspcntr |= DISPPLANE_BGRX888;
2659 break;
2660 case DRM_FORMAT_XBGR8888:
57779d06
VS
2661 dspcntr |= DISPPLANE_RGBX888;
2662 break;
2663 case DRM_FORMAT_XRGB2101010:
57779d06
VS
2664 dspcntr |= DISPPLANE_BGRX101010;
2665 break;
2666 case DRM_FORMAT_XBGR2101010:
57779d06 2667 dspcntr |= DISPPLANE_RGBX101010;
81255565
JB
2668 break;
2669 default:
baba133a 2670 BUG();
81255565 2671 }
57779d06 2672
f45651ba
VS
2673 if (INTEL_INFO(dev)->gen >= 4 &&
2674 obj->tiling_mode != I915_TILING_NONE)
2675 dspcntr |= DISPPLANE_TILED;
81255565 2676
de1aa629
VS
2677 if (IS_G4X(dev))
2678 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
2679
ac484963 2680 linear_offset = y * fb->pitches[0] + x * cpp;
81255565 2681
c2c75131
DV
2682 if (INTEL_INFO(dev)->gen >= 4) {
2683 intel_crtc->dspaddr_offset =
4f2d9934 2684 intel_compute_tile_offset(&x, &y, fb, 0,
8d0deca8 2685 fb->pitches[0], rotation);
c2c75131
DV
2686 linear_offset -= intel_crtc->dspaddr_offset;
2687 } else {
e506a0c6 2688 intel_crtc->dspaddr_offset = linear_offset;
c2c75131 2689 }
e506a0c6 2690
8d0deca8 2691 if (rotation == BIT(DRM_ROTATE_180)) {
48404c1e
SJ
2692 dspcntr |= DISPPLANE_ROTATE_180;
2693
a8d201af
ML
2694 x += (crtc_state->pipe_src_w - 1);
2695 y += (crtc_state->pipe_src_h - 1);
48404c1e
SJ
2696
2697 /* Finding the last pixel of the last line of the display
2698 data and adding to linear_offset*/
2699 linear_offset +=
a8d201af 2700 (crtc_state->pipe_src_h - 1) * fb->pitches[0] +
ac484963 2701 (crtc_state->pipe_src_w - 1) * cpp;
48404c1e
SJ
2702 }
2703
2db3366b
PZ
2704 intel_crtc->adjusted_x = x;
2705 intel_crtc->adjusted_y = y;
2706
48404c1e
SJ
2707 I915_WRITE(reg, dspcntr);
2708
01f2c773 2709 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
a6c45cf0 2710 if (INTEL_INFO(dev)->gen >= 4) {
85ba7b7d
DV
2711 I915_WRITE(DSPSURF(plane),
2712 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
5eddb70b 2713 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
e506a0c6 2714 I915_WRITE(DSPLINOFF(plane), linear_offset);
5eddb70b 2715 } else
f343c5f6 2716 I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
5eddb70b 2717 POSTING_READ(reg);
17638cd6
JB
2718}
2719
a8d201af
ML
2720static void i9xx_disable_primary_plane(struct drm_plane *primary,
2721 struct drm_crtc *crtc)
17638cd6
JB
2722{
2723 struct drm_device *dev = crtc->dev;
fac5e23e 2724 struct drm_i915_private *dev_priv = to_i915(dev);
17638cd6 2725 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
17638cd6 2726 int plane = intel_crtc->plane;
f45651ba 2727
a8d201af
ML
2728 I915_WRITE(DSPCNTR(plane), 0);
2729 if (INTEL_INFO(dev_priv)->gen >= 4)
fdd508a6 2730 I915_WRITE(DSPSURF(plane), 0);
a8d201af
ML
2731 else
2732 I915_WRITE(DSPADDR(plane), 0);
2733 POSTING_READ(DSPCNTR(plane));
2734}
c9ba6fad 2735
a8d201af
ML
2736static void ironlake_update_primary_plane(struct drm_plane *primary,
2737 const struct intel_crtc_state *crtc_state,
2738 const struct intel_plane_state *plane_state)
2739{
2740 struct drm_device *dev = primary->dev;
fac5e23e 2741 struct drm_i915_private *dev_priv = to_i915(dev);
a8d201af
ML
2742 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
2743 struct drm_framebuffer *fb = plane_state->base.fb;
2744 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
2745 int plane = intel_crtc->plane;
54ea9da8 2746 u32 linear_offset;
a8d201af
ML
2747 u32 dspcntr;
2748 i915_reg_t reg = DSPCNTR(plane);
8d0deca8 2749 unsigned int rotation = plane_state->base.rotation;
ac484963 2750 int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
a8d201af
ML
2751 int x = plane_state->src.x1 >> 16;
2752 int y = plane_state->src.y1 >> 16;
c9ba6fad 2753
f45651ba 2754 dspcntr = DISPPLANE_GAMMA_ENABLE;
fdd508a6 2755 dspcntr |= DISPLAY_PLANE_ENABLE;
f45651ba
VS
2756
2757 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
2758 dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
17638cd6 2759
57779d06
VS
2760 switch (fb->pixel_format) {
2761 case DRM_FORMAT_C8:
17638cd6
JB
2762 dspcntr |= DISPPLANE_8BPP;
2763 break;
57779d06
VS
2764 case DRM_FORMAT_RGB565:
2765 dspcntr |= DISPPLANE_BGRX565;
17638cd6 2766 break;
57779d06 2767 case DRM_FORMAT_XRGB8888:
57779d06
VS
2768 dspcntr |= DISPPLANE_BGRX888;
2769 break;
2770 case DRM_FORMAT_XBGR8888:
57779d06
VS
2771 dspcntr |= DISPPLANE_RGBX888;
2772 break;
2773 case DRM_FORMAT_XRGB2101010:
57779d06
VS
2774 dspcntr |= DISPPLANE_BGRX101010;
2775 break;
2776 case DRM_FORMAT_XBGR2101010:
57779d06 2777 dspcntr |= DISPPLANE_RGBX101010;
17638cd6
JB
2778 break;
2779 default:
baba133a 2780 BUG();
17638cd6
JB
2781 }
2782
2783 if (obj->tiling_mode != I915_TILING_NONE)
2784 dspcntr |= DISPPLANE_TILED;
17638cd6 2785
f45651ba 2786 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev))
1f5d76db 2787 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
17638cd6 2788
ac484963 2789 linear_offset = y * fb->pitches[0] + x * cpp;
c2c75131 2790 intel_crtc->dspaddr_offset =
4f2d9934 2791 intel_compute_tile_offset(&x, &y, fb, 0,
8d0deca8 2792 fb->pitches[0], rotation);
c2c75131 2793 linear_offset -= intel_crtc->dspaddr_offset;
8d0deca8 2794 if (rotation == BIT(DRM_ROTATE_180)) {
48404c1e
SJ
2795 dspcntr |= DISPPLANE_ROTATE_180;
2796
2797 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev)) {
a8d201af
ML
2798 x += (crtc_state->pipe_src_w - 1);
2799 y += (crtc_state->pipe_src_h - 1);
48404c1e
SJ
2800
2801 /* Finding the last pixel of the last line of the display
2802 data and adding to linear_offset*/
2803 linear_offset +=
a8d201af 2804 (crtc_state->pipe_src_h - 1) * fb->pitches[0] +
ac484963 2805 (crtc_state->pipe_src_w - 1) * cpp;
48404c1e
SJ
2806 }
2807 }
2808
2db3366b
PZ
2809 intel_crtc->adjusted_x = x;
2810 intel_crtc->adjusted_y = y;
2811
48404c1e 2812 I915_WRITE(reg, dspcntr);
17638cd6 2813
01f2c773 2814 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
85ba7b7d
DV
2815 I915_WRITE(DSPSURF(plane),
2816 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
b3dc685e 2817 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
bc1c91eb
DL
2818 I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
2819 } else {
2820 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2821 I915_WRITE(DSPLINOFF(plane), linear_offset);
2822 }
17638cd6 2823 POSTING_READ(reg);
17638cd6
JB
2824}
2825
7b49f948
VS
2826u32 intel_fb_stride_alignment(const struct drm_i915_private *dev_priv,
2827 uint64_t fb_modifier, uint32_t pixel_format)
b321803d 2828{
7b49f948 2829 if (fb_modifier == DRM_FORMAT_MOD_NONE) {
b321803d 2830 return 64;
7b49f948
VS
2831 } else {
2832 int cpp = drm_format_plane_cpp(pixel_format, 0);
2833
27ba3910 2834 return intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
b321803d
DL
2835 }
2836}
2837
44eb0cb9
MK
2838u32 intel_plane_obj_offset(struct intel_plane *intel_plane,
2839 struct drm_i915_gem_object *obj,
2840 unsigned int plane)
121920fa 2841{
ce7f1728 2842 struct i915_ggtt_view view;
dedf278c 2843 struct i915_vma *vma;
44eb0cb9 2844 u64 offset;
121920fa 2845
e7941294 2846 intel_fill_fb_ggtt_view(&view, intel_plane->base.state->fb,
3465c580 2847 intel_plane->base.state->rotation);
121920fa 2848
ce7f1728 2849 vma = i915_gem_obj_to_ggtt_view(obj, &view);
dedf278c 2850 if (WARN(!vma, "ggtt vma for display object not found! (view=%u)\n",
ce7f1728 2851 view.type))
dedf278c
TU
2852 return -1;
2853
44eb0cb9 2854 offset = vma->node.start;
dedf278c
TU
2855
2856 if (plane == 1) {
7723f47d 2857 offset += vma->ggtt_view.params.rotated.uv_start_page *
dedf278c
TU
2858 PAGE_SIZE;
2859 }
2860
44eb0cb9
MK
2861 WARN_ON(upper_32_bits(offset));
2862
2863 return lower_32_bits(offset);
121920fa
TU
2864}
2865
e435d6e5
ML
2866static void skl_detach_scaler(struct intel_crtc *intel_crtc, int id)
2867{
2868 struct drm_device *dev = intel_crtc->base.dev;
fac5e23e 2869 struct drm_i915_private *dev_priv = to_i915(dev);
e435d6e5
ML
2870
2871 I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, id), 0);
2872 I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, id), 0);
2873 I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, id), 0);
e435d6e5
ML
2874}
2875
a1b2278e
CK
2876/*
2877 * This function detaches (aka. unbinds) unused scalers in hardware
2878 */
0583236e 2879static void skl_detach_scalers(struct intel_crtc *intel_crtc)
a1b2278e 2880{
a1b2278e
CK
2881 struct intel_crtc_scaler_state *scaler_state;
2882 int i;
2883
a1b2278e
CK
2884 scaler_state = &intel_crtc->config->scaler_state;
2885
2886 /* loop through and disable scalers that aren't in use */
2887 for (i = 0; i < intel_crtc->num_scalers; i++) {
e435d6e5
ML
2888 if (!scaler_state->scalers[i].in_use)
2889 skl_detach_scaler(intel_crtc, i);
a1b2278e
CK
2890 }
2891}
2892
6156a456 2893u32 skl_plane_ctl_format(uint32_t pixel_format)
70d21f0e 2894{
6156a456 2895 switch (pixel_format) {
d161cf7a 2896 case DRM_FORMAT_C8:
c34ce3d1 2897 return PLANE_CTL_FORMAT_INDEXED;
70d21f0e 2898 case DRM_FORMAT_RGB565:
c34ce3d1 2899 return PLANE_CTL_FORMAT_RGB_565;
70d21f0e 2900 case DRM_FORMAT_XBGR8888:
c34ce3d1 2901 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
6156a456 2902 case DRM_FORMAT_XRGB8888:
c34ce3d1 2903 return PLANE_CTL_FORMAT_XRGB_8888;
6156a456
CK
2904 /*
2905 * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
2906 * to be already pre-multiplied. We need to add a knob (or a different
2907 * DRM_FORMAT) for user-space to configure that.
2908 */
f75fb42a 2909 case DRM_FORMAT_ABGR8888:
c34ce3d1 2910 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX |
6156a456 2911 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
6156a456 2912 case DRM_FORMAT_ARGB8888:
c34ce3d1 2913 return PLANE_CTL_FORMAT_XRGB_8888 |
6156a456 2914 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
70d21f0e 2915 case DRM_FORMAT_XRGB2101010:
c34ce3d1 2916 return PLANE_CTL_FORMAT_XRGB_2101010;
70d21f0e 2917 case DRM_FORMAT_XBGR2101010:
c34ce3d1 2918 return PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
6156a456 2919 case DRM_FORMAT_YUYV:
c34ce3d1 2920 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
6156a456 2921 case DRM_FORMAT_YVYU:
c34ce3d1 2922 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
6156a456 2923 case DRM_FORMAT_UYVY:
c34ce3d1 2924 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
6156a456 2925 case DRM_FORMAT_VYUY:
c34ce3d1 2926 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
70d21f0e 2927 default:
4249eeef 2928 MISSING_CASE(pixel_format);
70d21f0e 2929 }
8cfcba41 2930
c34ce3d1 2931 return 0;
6156a456 2932}
70d21f0e 2933
6156a456
CK
2934u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
2935{
6156a456 2936 switch (fb_modifier) {
30af77c4 2937 case DRM_FORMAT_MOD_NONE:
70d21f0e 2938 break;
30af77c4 2939 case I915_FORMAT_MOD_X_TILED:
c34ce3d1 2940 return PLANE_CTL_TILED_X;
b321803d 2941 case I915_FORMAT_MOD_Y_TILED:
c34ce3d1 2942 return PLANE_CTL_TILED_Y;
b321803d 2943 case I915_FORMAT_MOD_Yf_TILED:
c34ce3d1 2944 return PLANE_CTL_TILED_YF;
70d21f0e 2945 default:
6156a456 2946 MISSING_CASE(fb_modifier);
70d21f0e 2947 }
8cfcba41 2948
c34ce3d1 2949 return 0;
6156a456 2950}
70d21f0e 2951
6156a456
CK
2952u32 skl_plane_ctl_rotation(unsigned int rotation)
2953{
3b7a5119 2954 switch (rotation) {
6156a456
CK
2955 case BIT(DRM_ROTATE_0):
2956 break;
1e8df167
SJ
2957 /*
2958 * DRM_ROTATE_ is counter clockwise to stay compatible with Xrandr
2959 * while i915 HW rotation is clockwise, thats why this swapping.
2960 */
3b7a5119 2961 case BIT(DRM_ROTATE_90):
1e8df167 2962 return PLANE_CTL_ROTATE_270;
3b7a5119 2963 case BIT(DRM_ROTATE_180):
c34ce3d1 2964 return PLANE_CTL_ROTATE_180;
3b7a5119 2965 case BIT(DRM_ROTATE_270):
1e8df167 2966 return PLANE_CTL_ROTATE_90;
6156a456
CK
2967 default:
2968 MISSING_CASE(rotation);
2969 }
2970
c34ce3d1 2971 return 0;
6156a456
CK
2972}
2973
a8d201af
ML
2974static void skylake_update_primary_plane(struct drm_plane *plane,
2975 const struct intel_crtc_state *crtc_state,
2976 const struct intel_plane_state *plane_state)
6156a456 2977{
a8d201af 2978 struct drm_device *dev = plane->dev;
fac5e23e 2979 struct drm_i915_private *dev_priv = to_i915(dev);
a8d201af
ML
2980 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
2981 struct drm_framebuffer *fb = plane_state->base.fb;
2982 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
6156a456
CK
2983 int pipe = intel_crtc->pipe;
2984 u32 plane_ctl, stride_div, stride;
2985 u32 tile_height, plane_offset, plane_size;
a8d201af 2986 unsigned int rotation = plane_state->base.rotation;
6156a456 2987 int x_offset, y_offset;
44eb0cb9 2988 u32 surf_addr;
a8d201af
ML
2989 int scaler_id = plane_state->scaler_id;
2990 int src_x = plane_state->src.x1 >> 16;
2991 int src_y = plane_state->src.y1 >> 16;
2992 int src_w = drm_rect_width(&plane_state->src) >> 16;
2993 int src_h = drm_rect_height(&plane_state->src) >> 16;
2994 int dst_x = plane_state->dst.x1;
2995 int dst_y = plane_state->dst.y1;
2996 int dst_w = drm_rect_width(&plane_state->dst);
2997 int dst_h = drm_rect_height(&plane_state->dst);
70d21f0e 2998
6156a456
CK
2999 plane_ctl = PLANE_CTL_ENABLE |
3000 PLANE_CTL_PIPE_GAMMA_ENABLE |
3001 PLANE_CTL_PIPE_CSC_ENABLE;
3002
3003 plane_ctl |= skl_plane_ctl_format(fb->pixel_format);
3004 plane_ctl |= skl_plane_ctl_tiling(fb->modifier[0]);
3005 plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
6156a456
CK
3006 plane_ctl |= skl_plane_ctl_rotation(rotation);
3007
7b49f948 3008 stride_div = intel_fb_stride_alignment(dev_priv, fb->modifier[0],
b321803d 3009 fb->pixel_format);
dedf278c 3010 surf_addr = intel_plane_obj_offset(to_intel_plane(plane), obj, 0);
3b7a5119 3011
a42e5a23
PZ
3012 WARN_ON(drm_rect_width(&plane_state->src) == 0);
3013
3b7a5119 3014 if (intel_rotation_90_or_270(rotation)) {
832be82f
VS
3015 int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
3016
3b7a5119 3017 /* stride = Surface height in tiles */
832be82f 3018 tile_height = intel_tile_height(dev_priv, fb->modifier[0], cpp);
3b7a5119 3019 stride = DIV_ROUND_UP(fb->height, tile_height);
a8d201af
ML
3020 x_offset = stride * tile_height - src_y - src_h;
3021 y_offset = src_x;
6156a456 3022 plane_size = (src_w - 1) << 16 | (src_h - 1);
3b7a5119
SJ
3023 } else {
3024 stride = fb->pitches[0] / stride_div;
a8d201af
ML
3025 x_offset = src_x;
3026 y_offset = src_y;
6156a456 3027 plane_size = (src_h - 1) << 16 | (src_w - 1);
3b7a5119
SJ
3028 }
3029 plane_offset = y_offset << 16 | x_offset;
b321803d 3030
2db3366b
PZ
3031 intel_crtc->adjusted_x = x_offset;
3032 intel_crtc->adjusted_y = y_offset;
3033
70d21f0e 3034 I915_WRITE(PLANE_CTL(pipe, 0), plane_ctl);
3b7a5119
SJ
3035 I915_WRITE(PLANE_OFFSET(pipe, 0), plane_offset);
3036 I915_WRITE(PLANE_SIZE(pipe, 0), plane_size);
3037 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
6156a456
CK
3038
3039 if (scaler_id >= 0) {
3040 uint32_t ps_ctrl = 0;
3041
3042 WARN_ON(!dst_w || !dst_h);
3043 ps_ctrl = PS_SCALER_EN | PS_PLANE_SEL(0) |
3044 crtc_state->scaler_state.scalers[scaler_id].mode;
3045 I915_WRITE(SKL_PS_CTRL(pipe, scaler_id), ps_ctrl);
3046 I915_WRITE(SKL_PS_PWR_GATE(pipe, scaler_id), 0);
3047 I915_WRITE(SKL_PS_WIN_POS(pipe, scaler_id), (dst_x << 16) | dst_y);
3048 I915_WRITE(SKL_PS_WIN_SZ(pipe, scaler_id), (dst_w << 16) | dst_h);
3049 I915_WRITE(PLANE_POS(pipe, 0), 0);
3050 } else {
3051 I915_WRITE(PLANE_POS(pipe, 0), (dst_y << 16) | dst_x);
3052 }
3053
121920fa 3054 I915_WRITE(PLANE_SURF(pipe, 0), surf_addr);
70d21f0e
DL
3055
3056 POSTING_READ(PLANE_SURF(pipe, 0));
3057}
3058
a8d201af
ML
3059static void skylake_disable_primary_plane(struct drm_plane *primary,
3060 struct drm_crtc *crtc)
17638cd6
JB
3061{
3062 struct drm_device *dev = crtc->dev;
fac5e23e 3063 struct drm_i915_private *dev_priv = to_i915(dev);
a8d201af 3064 int pipe = to_intel_crtc(crtc)->pipe;
17638cd6 3065
a8d201af
ML
3066 I915_WRITE(PLANE_CTL(pipe, 0), 0);
3067 I915_WRITE(PLANE_SURF(pipe, 0), 0);
3068 POSTING_READ(PLANE_SURF(pipe, 0));
3069}
29b9bde6 3070
a8d201af
ML
3071/* Assume fb object is pinned & idle & fenced and just update base pointers */
3072static int
3073intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
3074 int x, int y, enum mode_set_atomic state)
3075{
3076 /* Support for kgdboc is disabled, this needs a major rework. */
3077 DRM_ERROR("legacy panic handler not supported any more.\n");
3078
3079 return -ENODEV;
81255565
JB
3080}
3081
5a21b665
DV
3082static void intel_complete_page_flips(struct drm_i915_private *dev_priv)
3083{
3084 struct intel_crtc *crtc;
3085
91c8a326 3086 for_each_intel_crtc(&dev_priv->drm, crtc)
5a21b665
DV
3087 intel_finish_page_flip_cs(dev_priv, crtc->pipe);
3088}
3089
7514747d
VS
3090static void intel_update_primary_planes(struct drm_device *dev)
3091{
7514747d 3092 struct drm_crtc *crtc;
96a02917 3093
70e1e0ec 3094 for_each_crtc(dev, crtc) {
11c22da6
ML
3095 struct intel_plane *plane = to_intel_plane(crtc->primary);
3096 struct intel_plane_state *plane_state;
96a02917 3097
11c22da6 3098 drm_modeset_lock_crtc(crtc, &plane->base);
11c22da6
ML
3099 plane_state = to_intel_plane_state(plane->base.state);
3100
a8d201af
ML
3101 if (plane_state->visible)
3102 plane->update_plane(&plane->base,
3103 to_intel_crtc_state(crtc->state),
3104 plane_state);
11c22da6
ML
3105
3106 drm_modeset_unlock_crtc(crtc);
96a02917
VS
3107 }
3108}
3109
c033666a 3110void intel_prepare_reset(struct drm_i915_private *dev_priv)
7514747d
VS
3111{
3112 /* no reset support for gen2 */
c033666a 3113 if (IS_GEN2(dev_priv))
7514747d
VS
3114 return;
3115
3116 /* reset doesn't touch the display */
c033666a 3117 if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
7514747d
VS
3118 return;
3119
91c8a326 3120 drm_modeset_lock_all(&dev_priv->drm);
f98ce92f
VS
3121 /*
3122 * Disabling the crtcs gracefully seems nicer. Also the
3123 * g33 docs say we should at least disable all the planes.
3124 */
91c8a326 3125 intel_display_suspend(&dev_priv->drm);
7514747d
VS
3126}
3127
c033666a 3128void intel_finish_reset(struct drm_i915_private *dev_priv)
7514747d 3129{
5a21b665
DV
3130 /*
3131 * Flips in the rings will be nuked by the reset,
3132 * so complete all pending flips so that user space
3133 * will get its events and not get stuck.
3134 */
3135 intel_complete_page_flips(dev_priv);
3136
7514747d 3137 /* no reset support for gen2 */
c033666a 3138 if (IS_GEN2(dev_priv))
7514747d
VS
3139 return;
3140
3141 /* reset doesn't touch the display */
c033666a 3142 if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv)) {
7514747d
VS
3143 /*
3144 * Flips in the rings have been nuked by the reset,
3145 * so update the base address of all primary
3146 * planes to the the last fb to make sure we're
3147 * showing the correct fb after a reset.
11c22da6
ML
3148 *
3149 * FIXME: Atomic will make this obsolete since we won't schedule
3150 * CS-based flips (which might get lost in gpu resets) any more.
7514747d 3151 */
91c8a326 3152 intel_update_primary_planes(&dev_priv->drm);
7514747d
VS
3153 return;
3154 }
3155
3156 /*
3157 * The display has been reset as well,
3158 * so need a full re-initialization.
3159 */
3160 intel_runtime_pm_disable_interrupts(dev_priv);
3161 intel_runtime_pm_enable_interrupts(dev_priv);
3162
91c8a326 3163 intel_modeset_init_hw(&dev_priv->drm);
7514747d
VS
3164
3165 spin_lock_irq(&dev_priv->irq_lock);
3166 if (dev_priv->display.hpd_irq_setup)
91d14251 3167 dev_priv->display.hpd_irq_setup(dev_priv);
7514747d
VS
3168 spin_unlock_irq(&dev_priv->irq_lock);
3169
91c8a326 3170 intel_display_resume(&dev_priv->drm);
7514747d
VS
3171
3172 intel_hpd_init(dev_priv);
3173
91c8a326 3174 drm_modeset_unlock_all(&dev_priv->drm);
7514747d
VS
3175}
3176
7d5e3799
CW
3177static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
3178{
5a21b665
DV
3179 struct drm_device *dev = crtc->dev;
3180 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3181 unsigned reset_counter;
3182 bool pending;
3183
3184 reset_counter = i915_reset_counter(&to_i915(dev)->gpu_error);
3185 if (intel_crtc->reset_counter != reset_counter)
3186 return false;
3187
3188 spin_lock_irq(&dev->event_lock);
3189 pending = to_intel_crtc(crtc)->flip_work != NULL;
3190 spin_unlock_irq(&dev->event_lock);
3191
3192 return pending;
7d5e3799
CW
3193}
3194
bfd16b2a
ML
3195static void intel_update_pipe_config(struct intel_crtc *crtc,
3196 struct intel_crtc_state *old_crtc_state)
e30e8f75
GP
3197{
3198 struct drm_device *dev = crtc->base.dev;
fac5e23e 3199 struct drm_i915_private *dev_priv = to_i915(dev);
bfd16b2a
ML
3200 struct intel_crtc_state *pipe_config =
3201 to_intel_crtc_state(crtc->base.state);
e30e8f75 3202
bfd16b2a
ML
3203 /* drm_atomic_helper_update_legacy_modeset_state might not be called. */
3204 crtc->base.mode = crtc->base.state->mode;
3205
3206 DRM_DEBUG_KMS("Updating pipe size %ix%i -> %ix%i\n",
3207 old_crtc_state->pipe_src_w, old_crtc_state->pipe_src_h,
3208 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
e30e8f75
GP
3209
3210 /*
3211 * Update pipe size and adjust fitter if needed: the reason for this is
3212 * that in compute_mode_changes we check the native mode (not the pfit
3213 * mode) to see if we can flip rather than do a full mode set. In the
3214 * fastboot case, we'll flip, but if we don't update the pipesrc and
3215 * pfit state, we'll end up with a big fb scanned out into the wrong
3216 * sized surface.
e30e8f75
GP
3217 */
3218
e30e8f75 3219 I915_WRITE(PIPESRC(crtc->pipe),
bfd16b2a
ML
3220 ((pipe_config->pipe_src_w - 1) << 16) |
3221 (pipe_config->pipe_src_h - 1));
3222
3223 /* on skylake this is done by detaching scalers */
3224 if (INTEL_INFO(dev)->gen >= 9) {
3225 skl_detach_scalers(crtc);
3226
3227 if (pipe_config->pch_pfit.enabled)
3228 skylake_pfit_enable(crtc);
3229 } else if (HAS_PCH_SPLIT(dev)) {
3230 if (pipe_config->pch_pfit.enabled)
3231 ironlake_pfit_enable(crtc);
3232 else if (old_crtc_state->pch_pfit.enabled)
3233 ironlake_pfit_disable(crtc, true);
e30e8f75 3234 }
e30e8f75
GP
3235}
3236
5e84e1a4
ZW
3237static void intel_fdi_normal_train(struct drm_crtc *crtc)
3238{
3239 struct drm_device *dev = crtc->dev;
fac5e23e 3240 struct drm_i915_private *dev_priv = to_i915(dev);
5e84e1a4
ZW
3241 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3242 int pipe = intel_crtc->pipe;
f0f59a00
VS
3243 i915_reg_t reg;
3244 u32 temp;
5e84e1a4
ZW
3245
3246 /* enable normal train */
3247 reg = FDI_TX_CTL(pipe);
3248 temp = I915_READ(reg);
61e499bf 3249 if (IS_IVYBRIDGE(dev)) {
357555c0
JB
3250 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3251 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
61e499bf
KP
3252 } else {
3253 temp &= ~FDI_LINK_TRAIN_NONE;
3254 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
357555c0 3255 }
5e84e1a4
ZW
3256 I915_WRITE(reg, temp);
3257
3258 reg = FDI_RX_CTL(pipe);
3259 temp = I915_READ(reg);
3260 if (HAS_PCH_CPT(dev)) {
3261 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3262 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
3263 } else {
3264 temp &= ~FDI_LINK_TRAIN_NONE;
3265 temp |= FDI_LINK_TRAIN_NONE;
3266 }
3267 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
3268
3269 /* wait one idle pattern time */
3270 POSTING_READ(reg);
3271 udelay(1000);
357555c0
JB
3272
3273 /* IVB wants error correction enabled */
3274 if (IS_IVYBRIDGE(dev))
3275 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
3276 FDI_FE_ERRC_ENABLE);
5e84e1a4
ZW
3277}
3278
8db9d77b
ZW
3279/* The FDI link training functions for ILK/Ibexpeak. */
3280static void ironlake_fdi_link_train(struct drm_crtc *crtc)
3281{
3282 struct drm_device *dev = crtc->dev;
fac5e23e 3283 struct drm_i915_private *dev_priv = to_i915(dev);
8db9d77b
ZW
3284 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3285 int pipe = intel_crtc->pipe;
f0f59a00
VS
3286 i915_reg_t reg;
3287 u32 temp, tries;
8db9d77b 3288
1c8562f6 3289 /* FDI needs bits from pipe first */
0fc932b8 3290 assert_pipe_enabled(dev_priv, pipe);
0fc932b8 3291
e1a44743
AJ
3292 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3293 for train result */
5eddb70b
CW
3294 reg = FDI_RX_IMR(pipe);
3295 temp = I915_READ(reg);
e1a44743
AJ
3296 temp &= ~FDI_RX_SYMBOL_LOCK;
3297 temp &= ~FDI_RX_BIT_LOCK;
5eddb70b
CW
3298 I915_WRITE(reg, temp);
3299 I915_READ(reg);
e1a44743
AJ
3300 udelay(150);
3301
8db9d77b 3302 /* enable CPU FDI TX and PCH FDI RX */
5eddb70b
CW
3303 reg = FDI_TX_CTL(pipe);
3304 temp = I915_READ(reg);
627eb5a3 3305 temp &= ~FDI_DP_PORT_WIDTH_MASK;
6e3c9717 3306 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
8db9d77b
ZW
3307 temp &= ~FDI_LINK_TRAIN_NONE;
3308 temp |= FDI_LINK_TRAIN_PATTERN_1;
5eddb70b 3309 I915_WRITE(reg, temp | FDI_TX_ENABLE);
8db9d77b 3310
5eddb70b
CW
3311 reg = FDI_RX_CTL(pipe);
3312 temp = I915_READ(reg);
8db9d77b
ZW
3313 temp &= ~FDI_LINK_TRAIN_NONE;
3314 temp |= FDI_LINK_TRAIN_PATTERN_1;
5eddb70b
CW
3315 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3316
3317 POSTING_READ(reg);
8db9d77b
ZW
3318 udelay(150);
3319
5b2adf89 3320 /* Ironlake workaround, enable clock pointer after FDI enable*/
8f5718a6
DV
3321 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
3322 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
3323 FDI_RX_PHASE_SYNC_POINTER_EN);
5b2adf89 3324
5eddb70b 3325 reg = FDI_RX_IIR(pipe);
e1a44743 3326 for (tries = 0; tries < 5; tries++) {
5eddb70b 3327 temp = I915_READ(reg);
8db9d77b
ZW
3328 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3329
3330 if ((temp & FDI_RX_BIT_LOCK)) {
3331 DRM_DEBUG_KMS("FDI train 1 done.\n");
5eddb70b 3332 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
8db9d77b
ZW
3333 break;
3334 }
8db9d77b 3335 }
e1a44743 3336 if (tries == 5)
5eddb70b 3337 DRM_ERROR("FDI train 1 fail!\n");
8db9d77b
ZW
3338
3339 /* Train 2 */
5eddb70b
CW
3340 reg = FDI_TX_CTL(pipe);
3341 temp = I915_READ(reg);
8db9d77b
ZW
3342 temp &= ~FDI_LINK_TRAIN_NONE;
3343 temp |= FDI_LINK_TRAIN_PATTERN_2;
5eddb70b 3344 I915_WRITE(reg, temp);
8db9d77b 3345
5eddb70b
CW
3346 reg = FDI_RX_CTL(pipe);
3347 temp = I915_READ(reg);
8db9d77b
ZW
3348 temp &= ~FDI_LINK_TRAIN_NONE;
3349 temp |= FDI_LINK_TRAIN_PATTERN_2;
5eddb70b 3350 I915_WRITE(reg, temp);
8db9d77b 3351
5eddb70b
CW
3352 POSTING_READ(reg);
3353 udelay(150);
8db9d77b 3354
5eddb70b 3355 reg = FDI_RX_IIR(pipe);
e1a44743 3356 for (tries = 0; tries < 5; tries++) {
5eddb70b 3357 temp = I915_READ(reg);
8db9d77b
ZW
3358 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3359
3360 if (temp & FDI_RX_SYMBOL_LOCK) {
5eddb70b 3361 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
8db9d77b
ZW
3362 DRM_DEBUG_KMS("FDI train 2 done.\n");
3363 break;
3364 }
8db9d77b 3365 }
e1a44743 3366 if (tries == 5)
5eddb70b 3367 DRM_ERROR("FDI train 2 fail!\n");
8db9d77b
ZW
3368
3369 DRM_DEBUG_KMS("FDI train done\n");
5c5313c8 3370
8db9d77b
ZW
3371}
3372
0206e353 3373static const int snb_b_fdi_train_param[] = {
8db9d77b
ZW
3374 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
3375 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
3376 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
3377 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
3378};
3379
3380/* The FDI link training functions for SNB/Cougarpoint. */
3381static void gen6_fdi_link_train(struct drm_crtc *crtc)
3382{
3383 struct drm_device *dev = crtc->dev;
fac5e23e 3384 struct drm_i915_private *dev_priv = to_i915(dev);
8db9d77b
ZW
3385 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3386 int pipe = intel_crtc->pipe;
f0f59a00
VS
3387 i915_reg_t reg;
3388 u32 temp, i, retry;
8db9d77b 3389
e1a44743
AJ
3390 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3391 for train result */
5eddb70b
CW
3392 reg = FDI_RX_IMR(pipe);
3393 temp = I915_READ(reg);
e1a44743
AJ
3394 temp &= ~FDI_RX_SYMBOL_LOCK;
3395 temp &= ~FDI_RX_BIT_LOCK;
5eddb70b
CW
3396 I915_WRITE(reg, temp);
3397
3398 POSTING_READ(reg);
e1a44743
AJ
3399 udelay(150);
3400
8db9d77b 3401 /* enable CPU FDI TX and PCH FDI RX */
5eddb70b
CW
3402 reg = FDI_TX_CTL(pipe);
3403 temp = I915_READ(reg);
627eb5a3 3404 temp &= ~FDI_DP_PORT_WIDTH_MASK;
6e3c9717 3405 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
8db9d77b
ZW
3406 temp &= ~FDI_LINK_TRAIN_NONE;
3407 temp |= FDI_LINK_TRAIN_PATTERN_1;
3408 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3409 /* SNB-B */
3410 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
5eddb70b 3411 I915_WRITE(reg, temp | FDI_TX_ENABLE);
8db9d77b 3412
d74cf324
DV
3413 I915_WRITE(FDI_RX_MISC(pipe),
3414 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3415
5eddb70b
CW
3416 reg = FDI_RX_CTL(pipe);
3417 temp = I915_READ(reg);
8db9d77b
ZW
3418 if (HAS_PCH_CPT(dev)) {
3419 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3420 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3421 } else {
3422 temp &= ~FDI_LINK_TRAIN_NONE;
3423 temp |= FDI_LINK_TRAIN_PATTERN_1;
3424 }
5eddb70b
CW
3425 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3426
3427 POSTING_READ(reg);
8db9d77b
ZW
3428 udelay(150);
3429
0206e353 3430 for (i = 0; i < 4; i++) {
5eddb70b
CW
3431 reg = FDI_TX_CTL(pipe);
3432 temp = I915_READ(reg);
8db9d77b
ZW
3433 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3434 temp |= snb_b_fdi_train_param[i];
5eddb70b
CW
3435 I915_WRITE(reg, temp);
3436
3437 POSTING_READ(reg);
8db9d77b
ZW
3438 udelay(500);
3439
fa37d39e
SP
3440 for (retry = 0; retry < 5; retry++) {
3441 reg = FDI_RX_IIR(pipe);
3442 temp = I915_READ(reg);
3443 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3444 if (temp & FDI_RX_BIT_LOCK) {
3445 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3446 DRM_DEBUG_KMS("FDI train 1 done.\n");
3447 break;
3448 }
3449 udelay(50);
8db9d77b 3450 }
fa37d39e
SP
3451 if (retry < 5)
3452 break;
8db9d77b
ZW
3453 }
3454 if (i == 4)
5eddb70b 3455 DRM_ERROR("FDI train 1 fail!\n");
8db9d77b
ZW
3456
3457 /* Train 2 */
5eddb70b
CW
3458 reg = FDI_TX_CTL(pipe);
3459 temp = I915_READ(reg);
8db9d77b
ZW
3460 temp &= ~FDI_LINK_TRAIN_NONE;
3461 temp |= FDI_LINK_TRAIN_PATTERN_2;
3462 if (IS_GEN6(dev)) {
3463 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3464 /* SNB-B */
3465 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
3466 }
5eddb70b 3467 I915_WRITE(reg, temp);
8db9d77b 3468
5eddb70b
CW
3469 reg = FDI_RX_CTL(pipe);
3470 temp = I915_READ(reg);
8db9d77b
ZW
3471 if (HAS_PCH_CPT(dev)) {
3472 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3473 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
3474 } else {
3475 temp &= ~FDI_LINK_TRAIN_NONE;
3476 temp |= FDI_LINK_TRAIN_PATTERN_2;
3477 }
5eddb70b
CW
3478 I915_WRITE(reg, temp);
3479
3480 POSTING_READ(reg);
8db9d77b
ZW
3481 udelay(150);
3482
0206e353 3483 for (i = 0; i < 4; i++) {
5eddb70b
CW
3484 reg = FDI_TX_CTL(pipe);
3485 temp = I915_READ(reg);
8db9d77b
ZW
3486 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3487 temp |= snb_b_fdi_train_param[i];
5eddb70b
CW
3488 I915_WRITE(reg, temp);
3489
3490 POSTING_READ(reg);
8db9d77b
ZW
3491 udelay(500);
3492
fa37d39e
SP
3493 for (retry = 0; retry < 5; retry++) {
3494 reg = FDI_RX_IIR(pipe);
3495 temp = I915_READ(reg);
3496 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3497 if (temp & FDI_RX_SYMBOL_LOCK) {
3498 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3499 DRM_DEBUG_KMS("FDI train 2 done.\n");
3500 break;
3501 }
3502 udelay(50);
8db9d77b 3503 }
fa37d39e
SP
3504 if (retry < 5)
3505 break;
8db9d77b
ZW
3506 }
3507 if (i == 4)
5eddb70b 3508 DRM_ERROR("FDI train 2 fail!\n");
8db9d77b
ZW
3509
3510 DRM_DEBUG_KMS("FDI train done.\n");
3511}
3512
357555c0
JB
3513/* Manual link training for Ivy Bridge A0 parts */
3514static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
3515{
3516 struct drm_device *dev = crtc->dev;
fac5e23e 3517 struct drm_i915_private *dev_priv = to_i915(dev);
357555c0
JB
3518 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3519 int pipe = intel_crtc->pipe;
f0f59a00
VS
3520 i915_reg_t reg;
3521 u32 temp, i, j;
357555c0
JB
3522
3523 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3524 for train result */
3525 reg = FDI_RX_IMR(pipe);
3526 temp = I915_READ(reg);
3527 temp &= ~FDI_RX_SYMBOL_LOCK;
3528 temp &= ~FDI_RX_BIT_LOCK;
3529 I915_WRITE(reg, temp);
3530
3531 POSTING_READ(reg);
3532 udelay(150);
3533
01a415fd
DV
3534 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
3535 I915_READ(FDI_RX_IIR(pipe)));
3536
139ccd3f
JB
3537 /* Try each vswing and preemphasis setting twice before moving on */
3538 for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
3539 /* disable first in case we need to retry */
3540 reg = FDI_TX_CTL(pipe);
3541 temp = I915_READ(reg);
3542 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
3543 temp &= ~FDI_TX_ENABLE;
3544 I915_WRITE(reg, temp);
357555c0 3545
139ccd3f
JB
3546 reg = FDI_RX_CTL(pipe);
3547 temp = I915_READ(reg);
3548 temp &= ~FDI_LINK_TRAIN_AUTO;
3549 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3550 temp &= ~FDI_RX_ENABLE;
3551 I915_WRITE(reg, temp);
357555c0 3552
139ccd3f 3553 /* enable CPU FDI TX and PCH FDI RX */
357555c0
JB
3554 reg = FDI_TX_CTL(pipe);
3555 temp = I915_READ(reg);
139ccd3f 3556 temp &= ~FDI_DP_PORT_WIDTH_MASK;
6e3c9717 3557 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
139ccd3f 3558 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
357555c0 3559 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
139ccd3f
JB
3560 temp |= snb_b_fdi_train_param[j/2];
3561 temp |= FDI_COMPOSITE_SYNC;
3562 I915_WRITE(reg, temp | FDI_TX_ENABLE);
357555c0 3563
139ccd3f
JB
3564 I915_WRITE(FDI_RX_MISC(pipe),
3565 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
357555c0 3566
139ccd3f 3567 reg = FDI_RX_CTL(pipe);
357555c0 3568 temp = I915_READ(reg);
139ccd3f
JB
3569 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3570 temp |= FDI_COMPOSITE_SYNC;
3571 I915_WRITE(reg, temp | FDI_RX_ENABLE);
357555c0 3572
139ccd3f
JB
3573 POSTING_READ(reg);
3574 udelay(1); /* should be 0.5us */
357555c0 3575
139ccd3f
JB
3576 for (i = 0; i < 4; i++) {
3577 reg = FDI_RX_IIR(pipe);
3578 temp = I915_READ(reg);
3579 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
357555c0 3580
139ccd3f
JB
3581 if (temp & FDI_RX_BIT_LOCK ||
3582 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
3583 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3584 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
3585 i);
3586 break;
3587 }
3588 udelay(1); /* should be 0.5us */
3589 }
3590 if (i == 4) {
3591 DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
3592 continue;
3593 }
357555c0 3594
139ccd3f 3595 /* Train 2 */
357555c0
JB
3596 reg = FDI_TX_CTL(pipe);
3597 temp = I915_READ(reg);
139ccd3f
JB
3598 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3599 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
3600 I915_WRITE(reg, temp);
3601
3602 reg = FDI_RX_CTL(pipe);
3603 temp = I915_READ(reg);
3604 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3605 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
357555c0
JB
3606 I915_WRITE(reg, temp);
3607
3608 POSTING_READ(reg);
139ccd3f 3609 udelay(2); /* should be 1.5us */
357555c0 3610
139ccd3f
JB
3611 for (i = 0; i < 4; i++) {
3612 reg = FDI_RX_IIR(pipe);
3613 temp = I915_READ(reg);
3614 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
357555c0 3615
139ccd3f
JB
3616 if (temp & FDI_RX_SYMBOL_LOCK ||
3617 (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
3618 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3619 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
3620 i);
3621 goto train_done;
3622 }
3623 udelay(2); /* should be 1.5us */
357555c0 3624 }
139ccd3f
JB
3625 if (i == 4)
3626 DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
357555c0 3627 }
357555c0 3628
139ccd3f 3629train_done:
357555c0
JB
3630 DRM_DEBUG_KMS("FDI train done.\n");
3631}
3632
88cefb6c 3633static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
2c07245f 3634{
88cefb6c 3635 struct drm_device *dev = intel_crtc->base.dev;
fac5e23e 3636 struct drm_i915_private *dev_priv = to_i915(dev);
2c07245f 3637 int pipe = intel_crtc->pipe;
f0f59a00
VS
3638 i915_reg_t reg;
3639 u32 temp;
c64e311e 3640
c98e9dcf 3641 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
5eddb70b
CW
3642 reg = FDI_RX_CTL(pipe);
3643 temp = I915_READ(reg);
627eb5a3 3644 temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
6e3c9717 3645 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
dfd07d72 3646 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
5eddb70b
CW
3647 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
3648
3649 POSTING_READ(reg);
c98e9dcf
JB
3650 udelay(200);
3651
3652 /* Switch from Rawclk to PCDclk */
5eddb70b
CW
3653 temp = I915_READ(reg);
3654 I915_WRITE(reg, temp | FDI_PCDCLK);
3655
3656 POSTING_READ(reg);
c98e9dcf
JB
3657 udelay(200);
3658
20749730
PZ
3659 /* Enable CPU FDI TX PLL, always on for Ironlake */
3660 reg = FDI_TX_CTL(pipe);
3661 temp = I915_READ(reg);
3662 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
3663 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
5eddb70b 3664
20749730
PZ
3665 POSTING_READ(reg);
3666 udelay(100);
6be4a607 3667 }
0e23b99d
JB
3668}
3669
88cefb6c
DV
3670static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
3671{
3672 struct drm_device *dev = intel_crtc->base.dev;
fac5e23e 3673 struct drm_i915_private *dev_priv = to_i915(dev);
88cefb6c 3674 int pipe = intel_crtc->pipe;
f0f59a00
VS
3675 i915_reg_t reg;
3676 u32 temp;
88cefb6c
DV
3677
3678 /* Switch from PCDclk to Rawclk */
3679 reg = FDI_RX_CTL(pipe);
3680 temp = I915_READ(reg);
3681 I915_WRITE(reg, temp & ~FDI_PCDCLK);
3682
3683 /* Disable CPU FDI TX PLL */
3684 reg = FDI_TX_CTL(pipe);
3685 temp = I915_READ(reg);
3686 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
3687
3688 POSTING_READ(reg);
3689 udelay(100);
3690
3691 reg = FDI_RX_CTL(pipe);
3692 temp = I915_READ(reg);
3693 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
3694
3695 /* Wait for the clocks to turn off. */
3696 POSTING_READ(reg);
3697 udelay(100);
3698}
3699
0fc932b8
JB
3700static void ironlake_fdi_disable(struct drm_crtc *crtc)
3701{
3702 struct drm_device *dev = crtc->dev;
fac5e23e 3703 struct drm_i915_private *dev_priv = to_i915(dev);
0fc932b8
JB
3704 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3705 int pipe = intel_crtc->pipe;
f0f59a00
VS
3706 i915_reg_t reg;
3707 u32 temp;
0fc932b8
JB
3708
3709 /* disable CPU FDI tx and PCH FDI rx */
3710 reg = FDI_TX_CTL(pipe);
3711 temp = I915_READ(reg);
3712 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
3713 POSTING_READ(reg);
3714
3715 reg = FDI_RX_CTL(pipe);
3716 temp = I915_READ(reg);
3717 temp &= ~(0x7 << 16);
dfd07d72 3718 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
0fc932b8
JB
3719 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
3720
3721 POSTING_READ(reg);
3722 udelay(100);
3723
3724 /* Ironlake workaround, disable clock pointer after downing FDI */
eba905b2 3725 if (HAS_PCH_IBX(dev))
6f06ce18 3726 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
0fc932b8
JB
3727
3728 /* still set train pattern 1 */
3729 reg = FDI_TX_CTL(pipe);
3730 temp = I915_READ(reg);
3731 temp &= ~FDI_LINK_TRAIN_NONE;
3732 temp |= FDI_LINK_TRAIN_PATTERN_1;
3733 I915_WRITE(reg, temp);
3734
3735 reg = FDI_RX_CTL(pipe);
3736 temp = I915_READ(reg);
3737 if (HAS_PCH_CPT(dev)) {
3738 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3739 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3740 } else {
3741 temp &= ~FDI_LINK_TRAIN_NONE;
3742 temp |= FDI_LINK_TRAIN_PATTERN_1;
3743 }
3744 /* BPC in FDI rx is consistent with that in PIPECONF */
3745 temp &= ~(0x07 << 16);
dfd07d72 3746 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
0fc932b8
JB
3747 I915_WRITE(reg, temp);
3748
3749 POSTING_READ(reg);
3750 udelay(100);
3751}
3752
5dce5b93
CW
3753bool intel_has_pending_fb_unpin(struct drm_device *dev)
3754{
3755 struct intel_crtc *crtc;
3756
3757 /* Note that we don't need to be called with mode_config.lock here
3758 * as our list of CRTC objects is static for the lifetime of the
3759 * device and so cannot disappear as we iterate. Similarly, we can
3760 * happily treat the predicates as racy, atomic checks as userspace
3761 * cannot claim and pin a new fb without at least acquring the
3762 * struct_mutex and so serialising with us.
3763 */
d3fcc808 3764 for_each_intel_crtc(dev, crtc) {
5dce5b93
CW
3765 if (atomic_read(&crtc->unpin_work_count) == 0)
3766 continue;
3767
5a21b665 3768 if (crtc->flip_work)
5dce5b93
CW
3769 intel_wait_for_vblank(dev, crtc->pipe);
3770
3771 return true;
3772 }
3773
3774 return false;
3775}
3776
5a21b665 3777static void page_flip_completed(struct intel_crtc *intel_crtc)
d6bbafa1
CW
3778{
3779 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
5a21b665
DV
3780 struct intel_flip_work *work = intel_crtc->flip_work;
3781
3782 intel_crtc->flip_work = NULL;
d6bbafa1
CW
3783
3784 if (work->event)
560ce1dc 3785 drm_crtc_send_vblank_event(&intel_crtc->base, work->event);
d6bbafa1
CW
3786
3787 drm_crtc_vblank_put(&intel_crtc->base);
3788
5a21b665 3789 wake_up_all(&dev_priv->pending_flip_queue);
143f73b3 3790 queue_work(dev_priv->wq, &work->unpin_work);
5a21b665
DV
3791
3792 trace_i915_flip_complete(intel_crtc->plane,
3793 work->pending_flip_obj);
d6bbafa1
CW
3794}
3795
5008e874 3796static int intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
e6c3a2a6 3797{
0f91128d 3798 struct drm_device *dev = crtc->dev;
fac5e23e 3799 struct drm_i915_private *dev_priv = to_i915(dev);
5008e874 3800 long ret;
e6c3a2a6 3801
2c10d571 3802 WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
5008e874
ML
3803
3804 ret = wait_event_interruptible_timeout(
3805 dev_priv->pending_flip_queue,
3806 !intel_crtc_has_pending_flip(crtc),
3807 60*HZ);
3808
3809 if (ret < 0)
3810 return ret;
3811
5a21b665
DV
3812 if (ret == 0) {
3813 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3814 struct intel_flip_work *work;
3815
3816 spin_lock_irq(&dev->event_lock);
3817 work = intel_crtc->flip_work;
3818 if (work && !is_mmio_work(work)) {
3819 WARN_ONCE(1, "Removing stuck page flip\n");
3820 page_flip_completed(intel_crtc);
3821 }
3822 spin_unlock_irq(&dev->event_lock);
3823 }
5bb61643 3824
5008e874 3825 return 0;
e6c3a2a6
CW
3826}
3827
060f02d8
VS
3828static void lpt_disable_iclkip(struct drm_i915_private *dev_priv)
3829{
3830 u32 temp;
3831
3832 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
3833
3834 mutex_lock(&dev_priv->sb_lock);
3835
3836 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
3837 temp |= SBI_SSCCTL_DISABLE;
3838 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
3839
3840 mutex_unlock(&dev_priv->sb_lock);
3841}
3842
e615efe4
ED
3843/* Program iCLKIP clock to the desired frequency */
3844static void lpt_program_iclkip(struct drm_crtc *crtc)
3845{
64b46a06 3846 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
6e3c9717 3847 int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
e615efe4
ED
3848 u32 divsel, phaseinc, auxdiv, phasedir = 0;
3849 u32 temp;
3850
060f02d8 3851 lpt_disable_iclkip(dev_priv);
e615efe4 3852
64b46a06
VS
3853 /* The iCLK virtual clock root frequency is in MHz,
3854 * but the adjusted_mode->crtc_clock in in KHz. To get the
3855 * divisors, it is necessary to divide one by another, so we
3856 * convert the virtual clock precision to KHz here for higher
3857 * precision.
3858 */
3859 for (auxdiv = 0; auxdiv < 2; auxdiv++) {
e615efe4
ED
3860 u32 iclk_virtual_root_freq = 172800 * 1000;
3861 u32 iclk_pi_range = 64;
64b46a06 3862 u32 desired_divisor;
e615efe4 3863
64b46a06
VS
3864 desired_divisor = DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
3865 clock << auxdiv);
3866 divsel = (desired_divisor / iclk_pi_range) - 2;
3867 phaseinc = desired_divisor % iclk_pi_range;
e615efe4 3868
64b46a06
VS
3869 /*
3870 * Near 20MHz is a corner case which is
3871 * out of range for the 7-bit divisor
3872 */
3873 if (divsel <= 0x7f)
3874 break;
e615efe4
ED
3875 }
3876
3877 /* This should not happen with any sane values */
3878 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
3879 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
3880 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
3881 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
3882
3883 DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
12d7ceed 3884 clock,
e615efe4
ED
3885 auxdiv,
3886 divsel,
3887 phasedir,
3888 phaseinc);
3889
060f02d8
VS
3890 mutex_lock(&dev_priv->sb_lock);
3891
e615efe4 3892 /* Program SSCDIVINTPHASE6 */
988d6ee8 3893 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
e615efe4
ED
3894 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
3895 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
3896 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
3897 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
3898 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
3899 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
988d6ee8 3900 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
e615efe4
ED
3901
3902 /* Program SSCAUXDIV */
988d6ee8 3903 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
e615efe4
ED
3904 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
3905 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
988d6ee8 3906 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
e615efe4
ED
3907
3908 /* Enable modulator and associated divider */
988d6ee8 3909 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
e615efe4 3910 temp &= ~SBI_SSCCTL_DISABLE;
988d6ee8 3911 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
e615efe4 3912
060f02d8
VS
3913 mutex_unlock(&dev_priv->sb_lock);
3914
e615efe4
ED
3915 /* Wait for initialization time */
3916 udelay(24);
3917
3918 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
3919}
3920
8802e5b6
VS
3921int lpt_get_iclkip(struct drm_i915_private *dev_priv)
3922{
3923 u32 divsel, phaseinc, auxdiv;
3924 u32 iclk_virtual_root_freq = 172800 * 1000;
3925 u32 iclk_pi_range = 64;
3926 u32 desired_divisor;
3927 u32 temp;
3928
3929 if ((I915_READ(PIXCLK_GATE) & PIXCLK_GATE_UNGATE) == 0)
3930 return 0;
3931
3932 mutex_lock(&dev_priv->sb_lock);
3933
3934 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
3935 if (temp & SBI_SSCCTL_DISABLE) {
3936 mutex_unlock(&dev_priv->sb_lock);
3937 return 0;
3938 }
3939
3940 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
3941 divsel = (temp & SBI_SSCDIVINTPHASE_DIVSEL_MASK) >>
3942 SBI_SSCDIVINTPHASE_DIVSEL_SHIFT;
3943 phaseinc = (temp & SBI_SSCDIVINTPHASE_INCVAL_MASK) >>
3944 SBI_SSCDIVINTPHASE_INCVAL_SHIFT;
3945
3946 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
3947 auxdiv = (temp & SBI_SSCAUXDIV_FINALDIV2SEL_MASK) >>
3948 SBI_SSCAUXDIV_FINALDIV2SEL_SHIFT;
3949
3950 mutex_unlock(&dev_priv->sb_lock);
3951
3952 desired_divisor = (divsel + 2) * iclk_pi_range + phaseinc;
3953
3954 return DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
3955 desired_divisor << auxdiv);
3956}
3957
275f01b2
DV
3958static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
3959 enum pipe pch_transcoder)
3960{
3961 struct drm_device *dev = crtc->base.dev;
fac5e23e 3962 struct drm_i915_private *dev_priv = to_i915(dev);
6e3c9717 3963 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
275f01b2
DV
3964
3965 I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
3966 I915_READ(HTOTAL(cpu_transcoder)));
3967 I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
3968 I915_READ(HBLANK(cpu_transcoder)));
3969 I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
3970 I915_READ(HSYNC(cpu_transcoder)));
3971
3972 I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
3973 I915_READ(VTOTAL(cpu_transcoder)));
3974 I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
3975 I915_READ(VBLANK(cpu_transcoder)));
3976 I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
3977 I915_READ(VSYNC(cpu_transcoder)));
3978 I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
3979 I915_READ(VSYNCSHIFT(cpu_transcoder)));
3980}
3981
003632d9 3982static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
1fbc0d78 3983{
fac5e23e 3984 struct drm_i915_private *dev_priv = to_i915(dev);
1fbc0d78
DV
3985 uint32_t temp;
3986
3987 temp = I915_READ(SOUTH_CHICKEN1);
003632d9 3988 if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
1fbc0d78
DV
3989 return;
3990
3991 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
3992 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
3993
003632d9
ACO
3994 temp &= ~FDI_BC_BIFURCATION_SELECT;
3995 if (enable)
3996 temp |= FDI_BC_BIFURCATION_SELECT;
3997
3998 DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
1fbc0d78
DV
3999 I915_WRITE(SOUTH_CHICKEN1, temp);
4000 POSTING_READ(SOUTH_CHICKEN1);
4001}
4002
4003static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
4004{
4005 struct drm_device *dev = intel_crtc->base.dev;
1fbc0d78
DV
4006
4007 switch (intel_crtc->pipe) {
4008 case PIPE_A:
4009 break;
4010 case PIPE_B:
6e3c9717 4011 if (intel_crtc->config->fdi_lanes > 2)
003632d9 4012 cpt_set_fdi_bc_bifurcation(dev, false);
1fbc0d78 4013 else
003632d9 4014 cpt_set_fdi_bc_bifurcation(dev, true);
1fbc0d78
DV
4015
4016 break;
4017 case PIPE_C:
003632d9 4018 cpt_set_fdi_bc_bifurcation(dev, true);
1fbc0d78
DV
4019
4020 break;
4021 default:
4022 BUG();
4023 }
4024}
4025
c48b5305
VS
4026/* Return which DP Port should be selected for Transcoder DP control */
4027static enum port
4028intel_trans_dp_port_sel(struct drm_crtc *crtc)
4029{
4030 struct drm_device *dev = crtc->dev;
4031 struct intel_encoder *encoder;
4032
4033 for_each_encoder_on_crtc(dev, crtc, encoder) {
cca0502b 4034 if (encoder->type == INTEL_OUTPUT_DP ||
c48b5305
VS
4035 encoder->type == INTEL_OUTPUT_EDP)
4036 return enc_to_dig_port(&encoder->base)->port;
4037 }
4038
4039 return -1;
4040}
4041
f67a559d
JB
4042/*
4043 * Enable PCH resources required for PCH ports:
4044 * - PCH PLLs
4045 * - FDI training & RX/TX
4046 * - update transcoder timings
4047 * - DP transcoding bits
4048 * - transcoder
4049 */
4050static void ironlake_pch_enable(struct drm_crtc *crtc)
0e23b99d
JB
4051{
4052 struct drm_device *dev = crtc->dev;
fac5e23e 4053 struct drm_i915_private *dev_priv = to_i915(dev);
0e23b99d
JB
4054 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4055 int pipe = intel_crtc->pipe;
f0f59a00 4056 u32 temp;
2c07245f 4057
ab9412ba 4058 assert_pch_transcoder_disabled(dev_priv, pipe);
e7e164db 4059
1fbc0d78
DV
4060 if (IS_IVYBRIDGE(dev))
4061 ivybridge_update_fdi_bc_bifurcation(intel_crtc);
4062
cd986abb
DV
4063 /* Write the TU size bits before fdi link training, so that error
4064 * detection works. */
4065 I915_WRITE(FDI_RX_TUSIZE1(pipe),
4066 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
4067
c98e9dcf 4068 /* For PCH output, training FDI link */
674cf967 4069 dev_priv->display.fdi_link_train(crtc);
2c07245f 4070
3ad8a208
DV
4071 /* We need to program the right clock selection before writing the pixel
4072 * mutliplier into the DPLL. */
303b81e0 4073 if (HAS_PCH_CPT(dev)) {
ee7b9f93 4074 u32 sel;
4b645f14 4075
c98e9dcf 4076 temp = I915_READ(PCH_DPLL_SEL);
11887397
DV
4077 temp |= TRANS_DPLL_ENABLE(pipe);
4078 sel = TRANS_DPLLB_SEL(pipe);
8106ddbd
ACO
4079 if (intel_crtc->config->shared_dpll ==
4080 intel_get_shared_dpll_by_id(dev_priv, DPLL_ID_PCH_PLL_B))
ee7b9f93
JB
4081 temp |= sel;
4082 else
4083 temp &= ~sel;
c98e9dcf 4084 I915_WRITE(PCH_DPLL_SEL, temp);
c98e9dcf 4085 }
5eddb70b 4086
3ad8a208
DV
4087 /* XXX: pch pll's can be enabled any time before we enable the PCH
4088 * transcoder, and we actually should do this to not upset any PCH
4089 * transcoder that already use the clock when we share it.
4090 *
4091 * Note that enable_shared_dpll tries to do the right thing, but
4092 * get_shared_dpll unconditionally resets the pll - we need that to have
4093 * the right LVDS enable sequence. */
85b3894f 4094 intel_enable_shared_dpll(intel_crtc);
3ad8a208 4095
d9b6cb56
JB
4096 /* set transcoder timing, panel must allow it */
4097 assert_panel_unlocked(dev_priv, pipe);
275f01b2 4098 ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
8db9d77b 4099
303b81e0 4100 intel_fdi_normal_train(crtc);
5e84e1a4 4101
c98e9dcf 4102 /* For PCH DP, enable TRANS_DP_CTL */
37a5650b 4103 if (HAS_PCH_CPT(dev) && intel_crtc_has_dp_encoder(intel_crtc->config)) {
9c4edaee
VS
4104 const struct drm_display_mode *adjusted_mode =
4105 &intel_crtc->config->base.adjusted_mode;
dfd07d72 4106 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
f0f59a00 4107 i915_reg_t reg = TRANS_DP_CTL(pipe);
5eddb70b
CW
4108 temp = I915_READ(reg);
4109 temp &= ~(TRANS_DP_PORT_SEL_MASK |
220cad3c
EA
4110 TRANS_DP_SYNC_MASK |
4111 TRANS_DP_BPC_MASK);
e3ef4479 4112 temp |= TRANS_DP_OUTPUT_ENABLE;
9325c9f0 4113 temp |= bpc << 9; /* same format but at 11:9 */
c98e9dcf 4114
9c4edaee 4115 if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
5eddb70b 4116 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
9c4edaee 4117 if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
5eddb70b 4118 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
c98e9dcf
JB
4119
4120 switch (intel_trans_dp_port_sel(crtc)) {
c48b5305 4121 case PORT_B:
5eddb70b 4122 temp |= TRANS_DP_PORT_SEL_B;
c98e9dcf 4123 break;
c48b5305 4124 case PORT_C:
5eddb70b 4125 temp |= TRANS_DP_PORT_SEL_C;
c98e9dcf 4126 break;
c48b5305 4127 case PORT_D:
5eddb70b 4128 temp |= TRANS_DP_PORT_SEL_D;
c98e9dcf
JB
4129 break;
4130 default:
e95d41e1 4131 BUG();
32f9d658 4132 }
2c07245f 4133
5eddb70b 4134 I915_WRITE(reg, temp);
6be4a607 4135 }
b52eb4dc 4136
b8a4f404 4137 ironlake_enable_pch_transcoder(dev_priv, pipe);
f67a559d
JB
4138}
4139
1507e5bd
PZ
4140static void lpt_pch_enable(struct drm_crtc *crtc)
4141{
4142 struct drm_device *dev = crtc->dev;
fac5e23e 4143 struct drm_i915_private *dev_priv = to_i915(dev);
1507e5bd 4144 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6e3c9717 4145 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
1507e5bd 4146
ab9412ba 4147 assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
1507e5bd 4148
8c52b5e8 4149 lpt_program_iclkip(crtc);
1507e5bd 4150
0540e488 4151 /* Set transcoder timing. */
275f01b2 4152 ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
1507e5bd 4153
937bb610 4154 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
f67a559d
JB
4155}
4156
a1520318 4157static void cpt_verify_modeset(struct drm_device *dev, int pipe)
d4270e57 4158{
fac5e23e 4159 struct drm_i915_private *dev_priv = to_i915(dev);
f0f59a00 4160 i915_reg_t dslreg = PIPEDSL(pipe);
d4270e57
JB
4161 u32 temp;
4162
4163 temp = I915_READ(dslreg);
4164 udelay(500);
4165 if (wait_for(I915_READ(dslreg) != temp, 5)) {
d4270e57 4166 if (wait_for(I915_READ(dslreg) != temp, 5))
84f44ce7 4167 DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
d4270e57
JB
4168 }
4169}
4170
86adf9d7
ML
4171static int
4172skl_update_scaler(struct intel_crtc_state *crtc_state, bool force_detach,
4173 unsigned scaler_user, int *scaler_id, unsigned int rotation,
4174 int src_w, int src_h, int dst_w, int dst_h)
a1b2278e 4175{
86adf9d7
ML
4176 struct intel_crtc_scaler_state *scaler_state =
4177 &crtc_state->scaler_state;
4178 struct intel_crtc *intel_crtc =
4179 to_intel_crtc(crtc_state->base.crtc);
a1b2278e 4180 int need_scaling;
6156a456
CK
4181
4182 need_scaling = intel_rotation_90_or_270(rotation) ?
4183 (src_h != dst_w || src_w != dst_h):
4184 (src_w != dst_w || src_h != dst_h);
a1b2278e
CK
4185
4186 /*
4187 * if plane is being disabled or scaler is no more required or force detach
4188 * - free scaler binded to this plane/crtc
4189 * - in order to do this, update crtc->scaler_usage
4190 *
4191 * Here scaler state in crtc_state is set free so that
4192 * scaler can be assigned to other user. Actual register
4193 * update to free the scaler is done in plane/panel-fit programming.
4194 * For this purpose crtc/plane_state->scaler_id isn't reset here.
4195 */
86adf9d7 4196 if (force_detach || !need_scaling) {
a1b2278e 4197 if (*scaler_id >= 0) {
86adf9d7 4198 scaler_state->scaler_users &= ~(1 << scaler_user);
a1b2278e
CK
4199 scaler_state->scalers[*scaler_id].in_use = 0;
4200
86adf9d7
ML
4201 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4202 "Staged freeing scaler id %d scaler_users = 0x%x\n",
4203 intel_crtc->pipe, scaler_user, *scaler_id,
a1b2278e
CK
4204 scaler_state->scaler_users);
4205 *scaler_id = -1;
4206 }
4207 return 0;
4208 }
4209
4210 /* range checks */
4211 if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
4212 dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
4213
4214 src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
4215 dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
86adf9d7 4216 DRM_DEBUG_KMS("scaler_user index %u.%u: src %ux%u dst %ux%u "
a1b2278e 4217 "size is out of scaler range\n",
86adf9d7 4218 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h);
a1b2278e
CK
4219 return -EINVAL;
4220 }
4221
86adf9d7
ML
4222 /* mark this plane as a scaler user in crtc_state */
4223 scaler_state->scaler_users |= (1 << scaler_user);
4224 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4225 "staged scaling request for %ux%u->%ux%u scaler_users = 0x%x\n",
4226 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h,
4227 scaler_state->scaler_users);
4228
4229 return 0;
4230}
4231
4232/**
4233 * skl_update_scaler_crtc - Stages update to scaler state for a given crtc.
4234 *
4235 * @state: crtc's scaler state
86adf9d7
ML
4236 *
4237 * Return
4238 * 0 - scaler_usage updated successfully
4239 * error - requested scaling cannot be supported or other error condition
4240 */
e435d6e5 4241int skl_update_scaler_crtc(struct intel_crtc_state *state)
86adf9d7
ML
4242{
4243 struct intel_crtc *intel_crtc = to_intel_crtc(state->base.crtc);
7c5f93b0 4244 const struct drm_display_mode *adjusted_mode = &state->base.adjusted_mode;
86adf9d7 4245
78108b7c
VS
4246 DRM_DEBUG_KMS("Updating scaler for [CRTC:%d:%s] scaler_user index %u.%u\n",
4247 intel_crtc->base.base.id, intel_crtc->base.name,
4248 intel_crtc->pipe, SKL_CRTC_INDEX);
86adf9d7 4249
e435d6e5 4250 return skl_update_scaler(state, !state->base.active, SKL_CRTC_INDEX,
fa5a7970 4251 &state->scaler_state.scaler_id, BIT(DRM_ROTATE_0),
86adf9d7 4252 state->pipe_src_w, state->pipe_src_h,
aad941d5 4253 adjusted_mode->crtc_hdisplay, adjusted_mode->crtc_vdisplay);
86adf9d7
ML
4254}
4255
4256/**
4257 * skl_update_scaler_plane - Stages update to scaler state for a given plane.
4258 *
4259 * @state: crtc's scaler state
86adf9d7
ML
4260 * @plane_state: atomic plane state to update
4261 *
4262 * Return
4263 * 0 - scaler_usage updated successfully
4264 * error - requested scaling cannot be supported or other error condition
4265 */
da20eabd
ML
4266static int skl_update_scaler_plane(struct intel_crtc_state *crtc_state,
4267 struct intel_plane_state *plane_state)
86adf9d7
ML
4268{
4269
4270 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
da20eabd
ML
4271 struct intel_plane *intel_plane =
4272 to_intel_plane(plane_state->base.plane);
86adf9d7
ML
4273 struct drm_framebuffer *fb = plane_state->base.fb;
4274 int ret;
4275
4276 bool force_detach = !fb || !plane_state->visible;
4277
72660ce0
VS
4278 DRM_DEBUG_KMS("Updating scaler for [PLANE:%d:%s] scaler_user index %u.%u\n",
4279 intel_plane->base.base.id, intel_plane->base.name,
4280 intel_crtc->pipe, drm_plane_index(&intel_plane->base));
86adf9d7
ML
4281
4282 ret = skl_update_scaler(crtc_state, force_detach,
4283 drm_plane_index(&intel_plane->base),
4284 &plane_state->scaler_id,
4285 plane_state->base.rotation,
4286 drm_rect_width(&plane_state->src) >> 16,
4287 drm_rect_height(&plane_state->src) >> 16,
4288 drm_rect_width(&plane_state->dst),
4289 drm_rect_height(&plane_state->dst));
4290
4291 if (ret || plane_state->scaler_id < 0)
4292 return ret;
4293
a1b2278e 4294 /* check colorkey */
818ed961 4295 if (plane_state->ckey.flags != I915_SET_COLORKEY_NONE) {
72660ce0
VS
4296 DRM_DEBUG_KMS("[PLANE:%d:%s] scaling with color key not allowed",
4297 intel_plane->base.base.id,
4298 intel_plane->base.name);
a1b2278e
CK
4299 return -EINVAL;
4300 }
4301
4302 /* Check src format */
86adf9d7
ML
4303 switch (fb->pixel_format) {
4304 case DRM_FORMAT_RGB565:
4305 case DRM_FORMAT_XBGR8888:
4306 case DRM_FORMAT_XRGB8888:
4307 case DRM_FORMAT_ABGR8888:
4308 case DRM_FORMAT_ARGB8888:
4309 case DRM_FORMAT_XRGB2101010:
4310 case DRM_FORMAT_XBGR2101010:
4311 case DRM_FORMAT_YUYV:
4312 case DRM_FORMAT_YVYU:
4313 case DRM_FORMAT_UYVY:
4314 case DRM_FORMAT_VYUY:
4315 break;
4316 default:
72660ce0
VS
4317 DRM_DEBUG_KMS("[PLANE:%d:%s] FB:%d unsupported scaling format 0x%x\n",
4318 intel_plane->base.base.id, intel_plane->base.name,
4319 fb->base.id, fb->pixel_format);
86adf9d7 4320 return -EINVAL;
a1b2278e
CK
4321 }
4322
a1b2278e
CK
4323 return 0;
4324}
4325
e435d6e5
ML
4326static void skylake_scaler_disable(struct intel_crtc *crtc)
4327{
4328 int i;
4329
4330 for (i = 0; i < crtc->num_scalers; i++)
4331 skl_detach_scaler(crtc, i);
4332}
4333
4334static void skylake_pfit_enable(struct intel_crtc *crtc)
bd2e244f
JB
4335{
4336 struct drm_device *dev = crtc->base.dev;
fac5e23e 4337 struct drm_i915_private *dev_priv = to_i915(dev);
bd2e244f 4338 int pipe = crtc->pipe;
a1b2278e
CK
4339 struct intel_crtc_scaler_state *scaler_state =
4340 &crtc->config->scaler_state;
4341
4342 DRM_DEBUG_KMS("for crtc_state = %p\n", crtc->config);
4343
6e3c9717 4344 if (crtc->config->pch_pfit.enabled) {
a1b2278e
CK
4345 int id;
4346
4347 if (WARN_ON(crtc->config->scaler_state.scaler_id < 0)) {
4348 DRM_ERROR("Requesting pfit without getting a scaler first\n");
4349 return;
4350 }
4351
4352 id = scaler_state->scaler_id;
4353 I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
4354 PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
4355 I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
4356 I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
4357
4358 DRM_DEBUG_KMS("for crtc_state = %p scaler_id = %d\n", crtc->config, id);
bd2e244f
JB
4359 }
4360}
4361
b074cec8
JB
4362static void ironlake_pfit_enable(struct intel_crtc *crtc)
4363{
4364 struct drm_device *dev = crtc->base.dev;
fac5e23e 4365 struct drm_i915_private *dev_priv = to_i915(dev);
b074cec8
JB
4366 int pipe = crtc->pipe;
4367
6e3c9717 4368 if (crtc->config->pch_pfit.enabled) {
b074cec8
JB
4369 /* Force use of hard-coded filter coefficients
4370 * as some pre-programmed values are broken,
4371 * e.g. x201.
4372 */
4373 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
4374 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
4375 PF_PIPE_SEL_IVB(pipe));
4376 else
4377 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
6e3c9717
ACO
4378 I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
4379 I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
d4270e57
JB
4380 }
4381}
4382
20bc8673 4383void hsw_enable_ips(struct intel_crtc *crtc)
d77e4531 4384{
cea165c3 4385 struct drm_device *dev = crtc->base.dev;
fac5e23e 4386 struct drm_i915_private *dev_priv = to_i915(dev);
d77e4531 4387
6e3c9717 4388 if (!crtc->config->ips_enabled)
d77e4531
PZ
4389 return;
4390
307e4498
ML
4391 /*
4392 * We can only enable IPS after we enable a plane and wait for a vblank
4393 * This function is called from post_plane_update, which is run after
4394 * a vblank wait.
4395 */
cea165c3 4396
d77e4531 4397 assert_plane_enabled(dev_priv, crtc->plane);
cea165c3 4398 if (IS_BROADWELL(dev)) {
2a114cc1
BW
4399 mutex_lock(&dev_priv->rps.hw_lock);
4400 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
4401 mutex_unlock(&dev_priv->rps.hw_lock);
4402 /* Quoting Art Runyan: "its not safe to expect any particular
4403 * value in IPS_CTL bit 31 after enabling IPS through the
e59150dc
JB
4404 * mailbox." Moreover, the mailbox may return a bogus state,
4405 * so we need to just enable it and continue on.
2a114cc1
BW
4406 */
4407 } else {
4408 I915_WRITE(IPS_CTL, IPS_ENABLE);
4409 /* The bit only becomes 1 in the next vblank, so this wait here
4410 * is essentially intel_wait_for_vblank. If we don't have this
4411 * and don't wait for vblanks until the end of crtc_enable, then
4412 * the HW state readout code will complain that the expected
4413 * IPS_CTL value is not the one we read. */
2ec9ba3c
CW
4414 if (intel_wait_for_register(dev_priv,
4415 IPS_CTL, IPS_ENABLE, IPS_ENABLE,
4416 50))
2a114cc1
BW
4417 DRM_ERROR("Timed out waiting for IPS enable\n");
4418 }
d77e4531
PZ
4419}
4420
20bc8673 4421void hsw_disable_ips(struct intel_crtc *crtc)
d77e4531
PZ
4422{
4423 struct drm_device *dev = crtc->base.dev;
fac5e23e 4424 struct drm_i915_private *dev_priv = to_i915(dev);
d77e4531 4425
6e3c9717 4426 if (!crtc->config->ips_enabled)
d77e4531
PZ
4427 return;
4428
4429 assert_plane_enabled(dev_priv, crtc->plane);
23d0b130 4430 if (IS_BROADWELL(dev)) {
2a114cc1
BW
4431 mutex_lock(&dev_priv->rps.hw_lock);
4432 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
4433 mutex_unlock(&dev_priv->rps.hw_lock);
23d0b130 4434 /* wait for pcode to finish disabling IPS, which may take up to 42ms */
b85c1ecf
CW
4435 if (intel_wait_for_register(dev_priv,
4436 IPS_CTL, IPS_ENABLE, 0,
4437 42))
23d0b130 4438 DRM_ERROR("Timed out waiting for IPS disable\n");
e59150dc 4439 } else {
2a114cc1 4440 I915_WRITE(IPS_CTL, 0);
e59150dc
JB
4441 POSTING_READ(IPS_CTL);
4442 }
d77e4531
PZ
4443
4444 /* We need to wait for a vblank before we can disable the plane. */
4445 intel_wait_for_vblank(dev, crtc->pipe);
4446}
4447
7cac945f 4448static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
d3eedb1a 4449{
7cac945f 4450 if (intel_crtc->overlay) {
d3eedb1a 4451 struct drm_device *dev = intel_crtc->base.dev;
fac5e23e 4452 struct drm_i915_private *dev_priv = to_i915(dev);
d3eedb1a
VS
4453
4454 mutex_lock(&dev->struct_mutex);
4455 dev_priv->mm.interruptible = false;
4456 (void) intel_overlay_switch_off(intel_crtc->overlay);
4457 dev_priv->mm.interruptible = true;
4458 mutex_unlock(&dev->struct_mutex);
4459 }
4460
4461 /* Let userspace switch the overlay on again. In most cases userspace
4462 * has to recompute where to put it anyway.
4463 */
4464}
4465
87d4300a
ML
4466/**
4467 * intel_post_enable_primary - Perform operations after enabling primary plane
4468 * @crtc: the CRTC whose primary plane was just enabled
4469 *
4470 * Performs potentially sleeping operations that must be done after the primary
4471 * plane is enabled, such as updating FBC and IPS. Note that this may be
4472 * called due to an explicit primary plane update, or due to an implicit
4473 * re-enable that is caused when a sprite plane is updated to no longer
4474 * completely hide the primary plane.
4475 */
4476static void
4477intel_post_enable_primary(struct drm_crtc *crtc)
a5c4d7bc
VS
4478{
4479 struct drm_device *dev = crtc->dev;
fac5e23e 4480 struct drm_i915_private *dev_priv = to_i915(dev);
a5c4d7bc
VS
4481 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4482 int pipe = intel_crtc->pipe;
a5c4d7bc 4483
87d4300a
ML
4484 /*
4485 * FIXME IPS should be fine as long as one plane is
4486 * enabled, but in practice it seems to have problems
4487 * when going from primary only to sprite only and vice
4488 * versa.
4489 */
a5c4d7bc
VS
4490 hsw_enable_ips(intel_crtc);
4491
f99d7069 4492 /*
87d4300a
ML
4493 * Gen2 reports pipe underruns whenever all planes are disabled.
4494 * So don't enable underrun reporting before at least some planes
4495 * are enabled.
4496 * FIXME: Need to fix the logic to work when we turn off all planes
4497 * but leave the pipe running.
f99d7069 4498 */
87d4300a
ML
4499 if (IS_GEN2(dev))
4500 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4501
aca7b684
VS
4502 /* Underruns don't always raise interrupts, so check manually. */
4503 intel_check_cpu_fifo_underruns(dev_priv);
4504 intel_check_pch_fifo_underruns(dev_priv);
a5c4d7bc
VS
4505}
4506
2622a081 4507/* FIXME move all this to pre_plane_update() with proper state tracking */
87d4300a
ML
4508static void
4509intel_pre_disable_primary(struct drm_crtc *crtc)
a5c4d7bc
VS
4510{
4511 struct drm_device *dev = crtc->dev;
fac5e23e 4512 struct drm_i915_private *dev_priv = to_i915(dev);
a5c4d7bc
VS
4513 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4514 int pipe = intel_crtc->pipe;
a5c4d7bc 4515
87d4300a
ML
4516 /*
4517 * Gen2 reports pipe underruns whenever all planes are disabled.
4518 * So diasble underrun reporting before all the planes get disabled.
4519 * FIXME: Need to fix the logic to work when we turn off all planes
4520 * but leave the pipe running.
4521 */
4522 if (IS_GEN2(dev))
4523 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
a5c4d7bc 4524
2622a081
VS
4525 /*
4526 * FIXME IPS should be fine as long as one plane is
4527 * enabled, but in practice it seems to have problems
4528 * when going from primary only to sprite only and vice
4529 * versa.
4530 */
4531 hsw_disable_ips(intel_crtc);
4532}
4533
4534/* FIXME get rid of this and use pre_plane_update */
4535static void
4536intel_pre_disable_primary_noatomic(struct drm_crtc *crtc)
4537{
4538 struct drm_device *dev = crtc->dev;
fac5e23e 4539 struct drm_i915_private *dev_priv = to_i915(dev);
2622a081
VS
4540 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4541 int pipe = intel_crtc->pipe;
4542
4543 intel_pre_disable_primary(crtc);
4544
87d4300a
ML
4545 /*
4546 * Vblank time updates from the shadow to live plane control register
4547 * are blocked if the memory self-refresh mode is active at that
4548 * moment. So to make sure the plane gets truly disabled, disable
4549 * first the self-refresh mode. The self-refresh enable bit in turn
4550 * will be checked/applied by the HW only at the next frame start
4551 * event which is after the vblank start event, so we need to have a
4552 * wait-for-vblank between disabling the plane and the pipe.
4553 */
262cd2e1 4554 if (HAS_GMCH_DISPLAY(dev)) {
87d4300a 4555 intel_set_memory_cxsr(dev_priv, false);
262cd2e1
VS
4556 dev_priv->wm.vlv.cxsr = false;
4557 intel_wait_for_vblank(dev, pipe);
4558 }
87d4300a
ML
4559}
4560
5a21b665
DV
4561static void intel_post_plane_update(struct intel_crtc_state *old_crtc_state)
4562{
4563 struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
4564 struct drm_atomic_state *old_state = old_crtc_state->base.state;
4565 struct intel_crtc_state *pipe_config =
4566 to_intel_crtc_state(crtc->base.state);
4567 struct drm_device *dev = crtc->base.dev;
4568 struct drm_plane *primary = crtc->base.primary;
4569 struct drm_plane_state *old_pri_state =
4570 drm_atomic_get_existing_plane_state(old_state, primary);
4571
4572 intel_frontbuffer_flip(dev, pipe_config->fb_bits);
4573
4574 crtc->wm.cxsr_allowed = true;
4575
4576 if (pipe_config->update_wm_post && pipe_config->base.active)
4577 intel_update_watermarks(&crtc->base);
4578
4579 if (old_pri_state) {
4580 struct intel_plane_state *primary_state =
4581 to_intel_plane_state(primary->state);
4582 struct intel_plane_state *old_primary_state =
4583 to_intel_plane_state(old_pri_state);
4584
4585 intel_fbc_post_update(crtc);
4586
4587 if (primary_state->visible &&
4588 (needs_modeset(&pipe_config->base) ||
4589 !old_primary_state->visible))
4590 intel_post_enable_primary(&crtc->base);
4591 }
4592}
4593
5c74cd73 4594static void intel_pre_plane_update(struct intel_crtc_state *old_crtc_state)
ac21b225 4595{
5c74cd73 4596 struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
ac21b225 4597 struct drm_device *dev = crtc->base.dev;
fac5e23e 4598 struct drm_i915_private *dev_priv = to_i915(dev);
ab1d3a0e
ML
4599 struct intel_crtc_state *pipe_config =
4600 to_intel_crtc_state(crtc->base.state);
5c74cd73
ML
4601 struct drm_atomic_state *old_state = old_crtc_state->base.state;
4602 struct drm_plane *primary = crtc->base.primary;
4603 struct drm_plane_state *old_pri_state =
4604 drm_atomic_get_existing_plane_state(old_state, primary);
4605 bool modeset = needs_modeset(&pipe_config->base);
ac21b225 4606
5c74cd73
ML
4607 if (old_pri_state) {
4608 struct intel_plane_state *primary_state =
4609 to_intel_plane_state(primary->state);
4610 struct intel_plane_state *old_primary_state =
4611 to_intel_plane_state(old_pri_state);
4612
faf68d92 4613 intel_fbc_pre_update(crtc, pipe_config, primary_state);
31ae71fc 4614
5c74cd73
ML
4615 if (old_primary_state->visible &&
4616 (modeset || !primary_state->visible))
4617 intel_pre_disable_primary(&crtc->base);
4618 }
852eb00d 4619
a4015f9a 4620 if (pipe_config->disable_cxsr && HAS_GMCH_DISPLAY(dev)) {
852eb00d 4621 crtc->wm.cxsr_allowed = false;
2dfd178d 4622
2622a081
VS
4623 /*
4624 * Vblank time updates from the shadow to live plane control register
4625 * are blocked if the memory self-refresh mode is active at that
4626 * moment. So to make sure the plane gets truly disabled, disable
4627 * first the self-refresh mode. The self-refresh enable bit in turn
4628 * will be checked/applied by the HW only at the next frame start
4629 * event which is after the vblank start event, so we need to have a
4630 * wait-for-vblank between disabling the plane and the pipe.
4631 */
4632 if (old_crtc_state->base.active) {
2dfd178d 4633 intel_set_memory_cxsr(dev_priv, false);
2622a081
VS
4634 dev_priv->wm.vlv.cxsr = false;
4635 intel_wait_for_vblank(dev, crtc->pipe);
4636 }
852eb00d 4637 }
92826fcd 4638
ed4a6a7c
MR
4639 /*
4640 * IVB workaround: must disable low power watermarks for at least
4641 * one frame before enabling scaling. LP watermarks can be re-enabled
4642 * when scaling is disabled.
4643 *
4644 * WaCxSRDisabledForSpriteScaling:ivb
4645 */
4646 if (pipe_config->disable_lp_wm) {
4647 ilk_disable_lp_wm(dev);
4648 intel_wait_for_vblank(dev, crtc->pipe);
4649 }
4650
4651 /*
4652 * If we're doing a modeset, we're done. No need to do any pre-vblank
4653 * watermark programming here.
4654 */
4655 if (needs_modeset(&pipe_config->base))
4656 return;
4657
4658 /*
4659 * For platforms that support atomic watermarks, program the
4660 * 'intermediate' watermarks immediately. On pre-gen9 platforms, these
4661 * will be the intermediate values that are safe for both pre- and
4662 * post- vblank; when vblank happens, the 'active' values will be set
4663 * to the final 'target' values and we'll do this again to get the
4664 * optimal watermarks. For gen9+ platforms, the values we program here
4665 * will be the final target values which will get automatically latched
4666 * at vblank time; no further programming will be necessary.
4667 *
4668 * If a platform hasn't been transitioned to atomic watermarks yet,
4669 * we'll continue to update watermarks the old way, if flags tell
4670 * us to.
4671 */
4672 if (dev_priv->display.initial_watermarks != NULL)
4673 dev_priv->display.initial_watermarks(pipe_config);
caed361d 4674 else if (pipe_config->update_wm_pre)
92826fcd 4675 intel_update_watermarks(&crtc->base);
ac21b225
ML
4676}
4677
d032ffa0 4678static void intel_crtc_disable_planes(struct drm_crtc *crtc, unsigned plane_mask)
87d4300a
ML
4679{
4680 struct drm_device *dev = crtc->dev;
4681 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
d032ffa0 4682 struct drm_plane *p;
87d4300a
ML
4683 int pipe = intel_crtc->pipe;
4684
7cac945f 4685 intel_crtc_dpms_overlay_disable(intel_crtc);
27321ae8 4686
d032ffa0
ML
4687 drm_for_each_plane_mask(p, dev, plane_mask)
4688 to_intel_plane(p)->disable_plane(p, crtc);
f98551ae 4689
f99d7069
DV
4690 /*
4691 * FIXME: Once we grow proper nuclear flip support out of this we need
4692 * to compute the mask of flip planes precisely. For the time being
4693 * consider this a flip to a NULL plane.
4694 */
4695 intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
a5c4d7bc
VS
4696}
4697
f67a559d
JB
4698static void ironlake_crtc_enable(struct drm_crtc *crtc)
4699{
4700 struct drm_device *dev = crtc->dev;
fac5e23e 4701 struct drm_i915_private *dev_priv = to_i915(dev);
f67a559d 4702 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 4703 struct intel_encoder *encoder;
f67a559d 4704 int pipe = intel_crtc->pipe;
b95c5321
ML
4705 struct intel_crtc_state *pipe_config =
4706 to_intel_crtc_state(crtc->state);
f67a559d 4707
53d9f4e9 4708 if (WARN_ON(intel_crtc->active))
f67a559d
JB
4709 return;
4710
b2c0593a
VS
4711 /*
4712 * Sometimes spurious CPU pipe underruns happen during FDI
4713 * training, at least with VGA+HDMI cloning. Suppress them.
4714 *
4715 * On ILK we get an occasional spurious CPU pipe underruns
4716 * between eDP port A enable and vdd enable. Also PCH port
4717 * enable seems to result in the occasional CPU pipe underrun.
4718 *
4719 * Spurious PCH underruns also occur during PCH enabling.
4720 */
4721 if (intel_crtc->config->has_pch_encoder || IS_GEN5(dev_priv))
4722 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
81b088ca
VS
4723 if (intel_crtc->config->has_pch_encoder)
4724 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
4725
6e3c9717 4726 if (intel_crtc->config->has_pch_encoder)
b14b1055
DV
4727 intel_prepare_shared_dpll(intel_crtc);
4728
37a5650b 4729 if (intel_crtc_has_dp_encoder(intel_crtc->config))
fe3cd48d 4730 intel_dp_set_m_n(intel_crtc, M1_N1);
29407aab
DV
4731
4732 intel_set_pipe_timings(intel_crtc);
bc58be60 4733 intel_set_pipe_src_size(intel_crtc);
29407aab 4734
6e3c9717 4735 if (intel_crtc->config->has_pch_encoder) {
29407aab 4736 intel_cpu_transcoder_set_m_n(intel_crtc,
6e3c9717 4737 &intel_crtc->config->fdi_m_n, NULL);
29407aab
DV
4738 }
4739
4740 ironlake_set_pipeconf(crtc);
4741
f67a559d 4742 intel_crtc->active = true;
8664281b 4743
f6736a1a 4744 for_each_encoder_on_crtc(dev, crtc, encoder)
952735ee
DV
4745 if (encoder->pre_enable)
4746 encoder->pre_enable(encoder);
f67a559d 4747
6e3c9717 4748 if (intel_crtc->config->has_pch_encoder) {
fff367c7
DV
4749 /* Note: FDI PLL enabling _must_ be done before we enable the
4750 * cpu pipes, hence this is separate from all the other fdi/pch
4751 * enabling. */
88cefb6c 4752 ironlake_fdi_pll_enable(intel_crtc);
46b6f814
DV
4753 } else {
4754 assert_fdi_tx_disabled(dev_priv, pipe);
4755 assert_fdi_rx_disabled(dev_priv, pipe);
4756 }
f67a559d 4757
b074cec8 4758 ironlake_pfit_enable(intel_crtc);
f67a559d 4759
9c54c0dd
JB
4760 /*
4761 * On ILK+ LUT must be loaded before the pipe is running but with
4762 * clocks enabled
4763 */
b95c5321 4764 intel_color_load_luts(&pipe_config->base);
9c54c0dd 4765
1d5bf5d9
ID
4766 if (dev_priv->display.initial_watermarks != NULL)
4767 dev_priv->display.initial_watermarks(intel_crtc->config);
e1fdc473 4768 intel_enable_pipe(intel_crtc);
f67a559d 4769
6e3c9717 4770 if (intel_crtc->config->has_pch_encoder)
f67a559d 4771 ironlake_pch_enable(crtc);
c98e9dcf 4772
f9b61ff6
DV
4773 assert_vblank_disabled(crtc);
4774 drm_crtc_vblank_on(crtc);
4775
fa5c73b1
DV
4776 for_each_encoder_on_crtc(dev, crtc, encoder)
4777 encoder->enable(encoder);
61b77ddd
DV
4778
4779 if (HAS_PCH_CPT(dev))
a1520318 4780 cpt_verify_modeset(dev, intel_crtc->pipe);
37ca8d4c
VS
4781
4782 /* Must wait for vblank to avoid spurious PCH FIFO underruns */
4783 if (intel_crtc->config->has_pch_encoder)
4784 intel_wait_for_vblank(dev, pipe);
b2c0593a 4785 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
37ca8d4c 4786 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
6be4a607
JB
4787}
4788
42db64ef
PZ
4789/* IPS only exists on ULT machines and is tied to pipe A. */
4790static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
4791{
f5adf94e 4792 return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
42db64ef
PZ
4793}
4794
4f771f10
PZ
4795static void haswell_crtc_enable(struct drm_crtc *crtc)
4796{
4797 struct drm_device *dev = crtc->dev;
fac5e23e 4798 struct drm_i915_private *dev_priv = to_i915(dev);
4f771f10
PZ
4799 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4800 struct intel_encoder *encoder;
99d736a2 4801 int pipe = intel_crtc->pipe, hsw_workaround_pipe;
4d1de975 4802 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
99d736a2
ML
4803 struct intel_crtc_state *pipe_config =
4804 to_intel_crtc_state(crtc->state);
4f771f10 4805
53d9f4e9 4806 if (WARN_ON(intel_crtc->active))
4f771f10
PZ
4807 return;
4808
81b088ca
VS
4809 if (intel_crtc->config->has_pch_encoder)
4810 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
4811 false);
4812
95a7a2ae
ID
4813 for_each_encoder_on_crtc(dev, crtc, encoder)
4814 if (encoder->pre_pll_enable)
4815 encoder->pre_pll_enable(encoder);
4816
8106ddbd 4817 if (intel_crtc->config->shared_dpll)
df8ad70c
DV
4818 intel_enable_shared_dpll(intel_crtc);
4819
37a5650b 4820 if (intel_crtc_has_dp_encoder(intel_crtc->config))
fe3cd48d 4821 intel_dp_set_m_n(intel_crtc, M1_N1);
229fca97 4822
d7edc4e5 4823 if (!transcoder_is_dsi(cpu_transcoder))
4d1de975
JN
4824 intel_set_pipe_timings(intel_crtc);
4825
bc58be60 4826 intel_set_pipe_src_size(intel_crtc);
229fca97 4827
4d1de975
JN
4828 if (cpu_transcoder != TRANSCODER_EDP &&
4829 !transcoder_is_dsi(cpu_transcoder)) {
4830 I915_WRITE(PIPE_MULT(cpu_transcoder),
6e3c9717 4831 intel_crtc->config->pixel_multiplier - 1);
ebb69c95
CT
4832 }
4833
6e3c9717 4834 if (intel_crtc->config->has_pch_encoder) {
229fca97 4835 intel_cpu_transcoder_set_m_n(intel_crtc,
6e3c9717 4836 &intel_crtc->config->fdi_m_n, NULL);
229fca97
DV
4837 }
4838
d7edc4e5 4839 if (!transcoder_is_dsi(cpu_transcoder))
4d1de975
JN
4840 haswell_set_pipeconf(crtc);
4841
391bf048 4842 haswell_set_pipemisc(crtc);
229fca97 4843
b95c5321 4844 intel_color_set_csc(&pipe_config->base);
229fca97 4845
4f771f10 4846 intel_crtc->active = true;
8664281b 4847
6b698516
DV
4848 if (intel_crtc->config->has_pch_encoder)
4849 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
4850 else
4851 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4852
7d4aefd0 4853 for_each_encoder_on_crtc(dev, crtc, encoder) {
4f771f10
PZ
4854 if (encoder->pre_enable)
4855 encoder->pre_enable(encoder);
7d4aefd0 4856 }
4f771f10 4857
d2d65408 4858 if (intel_crtc->config->has_pch_encoder)
4fe9467d 4859 dev_priv->display.fdi_link_train(crtc);
4fe9467d 4860
d7edc4e5 4861 if (!transcoder_is_dsi(cpu_transcoder))
7d4aefd0 4862 intel_ddi_enable_pipe_clock(intel_crtc);
4f771f10 4863
1c132b44 4864 if (INTEL_INFO(dev)->gen >= 9)
e435d6e5 4865 skylake_pfit_enable(intel_crtc);
ff6d9f55 4866 else
1c132b44 4867 ironlake_pfit_enable(intel_crtc);
4f771f10
PZ
4868
4869 /*
4870 * On ILK+ LUT must be loaded before the pipe is running but with
4871 * clocks enabled
4872 */
b95c5321 4873 intel_color_load_luts(&pipe_config->base);
4f771f10 4874
1f544388 4875 intel_ddi_set_pipe_settings(crtc);
d7edc4e5 4876 if (!transcoder_is_dsi(cpu_transcoder))
7d4aefd0 4877 intel_ddi_enable_transcoder_func(crtc);
4f771f10 4878
1d5bf5d9
ID
4879 if (dev_priv->display.initial_watermarks != NULL)
4880 dev_priv->display.initial_watermarks(pipe_config);
4881 else
4882 intel_update_watermarks(crtc);
4d1de975
JN
4883
4884 /* XXX: Do the pipe assertions at the right place for BXT DSI. */
d7edc4e5 4885 if (!transcoder_is_dsi(cpu_transcoder))
4d1de975 4886 intel_enable_pipe(intel_crtc);
42db64ef 4887
6e3c9717 4888 if (intel_crtc->config->has_pch_encoder)
1507e5bd 4889 lpt_pch_enable(crtc);
4f771f10 4890
a65347ba 4891 if (intel_crtc->config->dp_encoder_is_mst)
0e32b39c
DA
4892 intel_ddi_set_vc_payload_alloc(crtc, true);
4893
f9b61ff6
DV
4894 assert_vblank_disabled(crtc);
4895 drm_crtc_vblank_on(crtc);
4896
8807e55b 4897 for_each_encoder_on_crtc(dev, crtc, encoder) {
4f771f10 4898 encoder->enable(encoder);
8807e55b
JN
4899 intel_opregion_notify_encoder(encoder, true);
4900 }
4f771f10 4901
6b698516
DV
4902 if (intel_crtc->config->has_pch_encoder) {
4903 intel_wait_for_vblank(dev, pipe);
4904 intel_wait_for_vblank(dev, pipe);
4905 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
d2d65408
VS
4906 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
4907 true);
6b698516 4908 }
d2d65408 4909
e4916946
PZ
4910 /* If we change the relative order between pipe/planes enabling, we need
4911 * to change the workaround. */
99d736a2
ML
4912 hsw_workaround_pipe = pipe_config->hsw_workaround_pipe;
4913 if (IS_HASWELL(dev) && hsw_workaround_pipe != INVALID_PIPE) {
4914 intel_wait_for_vblank(dev, hsw_workaround_pipe);
4915 intel_wait_for_vblank(dev, hsw_workaround_pipe);
4916 }
4f771f10
PZ
4917}
4918
bfd16b2a 4919static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force)
3f8dce3a
DV
4920{
4921 struct drm_device *dev = crtc->base.dev;
fac5e23e 4922 struct drm_i915_private *dev_priv = to_i915(dev);
3f8dce3a
DV
4923 int pipe = crtc->pipe;
4924
4925 /* To avoid upsetting the power well on haswell only disable the pfit if
4926 * it's in use. The hw state code will make sure we get this right. */
bfd16b2a 4927 if (force || crtc->config->pch_pfit.enabled) {
3f8dce3a
DV
4928 I915_WRITE(PF_CTL(pipe), 0);
4929 I915_WRITE(PF_WIN_POS(pipe), 0);
4930 I915_WRITE(PF_WIN_SZ(pipe), 0);
4931 }
4932}
4933
6be4a607
JB
4934static void ironlake_crtc_disable(struct drm_crtc *crtc)
4935{
4936 struct drm_device *dev = crtc->dev;
fac5e23e 4937 struct drm_i915_private *dev_priv = to_i915(dev);
6be4a607 4938 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 4939 struct intel_encoder *encoder;
6be4a607 4940 int pipe = intel_crtc->pipe;
b52eb4dc 4941
b2c0593a
VS
4942 /*
4943 * Sometimes spurious CPU pipe underruns happen when the
4944 * pipe is already disabled, but FDI RX/TX is still enabled.
4945 * Happens at least with VGA+HDMI cloning. Suppress them.
4946 */
4947 if (intel_crtc->config->has_pch_encoder) {
4948 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
37ca8d4c 4949 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
b2c0593a 4950 }
37ca8d4c 4951
ea9d758d
DV
4952 for_each_encoder_on_crtc(dev, crtc, encoder)
4953 encoder->disable(encoder);
4954
f9b61ff6
DV
4955 drm_crtc_vblank_off(crtc);
4956 assert_vblank_disabled(crtc);
4957
575f7ab7 4958 intel_disable_pipe(intel_crtc);
32f9d658 4959
bfd16b2a 4960 ironlake_pfit_disable(intel_crtc, false);
2c07245f 4961
b2c0593a 4962 if (intel_crtc->config->has_pch_encoder)
5a74f70a
VS
4963 ironlake_fdi_disable(crtc);
4964
bf49ec8c
DV
4965 for_each_encoder_on_crtc(dev, crtc, encoder)
4966 if (encoder->post_disable)
4967 encoder->post_disable(encoder);
2c07245f 4968
6e3c9717 4969 if (intel_crtc->config->has_pch_encoder) {
d925c59a 4970 ironlake_disable_pch_transcoder(dev_priv, pipe);
6be4a607 4971
d925c59a 4972 if (HAS_PCH_CPT(dev)) {
f0f59a00
VS
4973 i915_reg_t reg;
4974 u32 temp;
4975
d925c59a
DV
4976 /* disable TRANS_DP_CTL */
4977 reg = TRANS_DP_CTL(pipe);
4978 temp = I915_READ(reg);
4979 temp &= ~(TRANS_DP_OUTPUT_ENABLE |
4980 TRANS_DP_PORT_SEL_MASK);
4981 temp |= TRANS_DP_PORT_SEL_NONE;
4982 I915_WRITE(reg, temp);
4983
4984 /* disable DPLL_SEL */
4985 temp = I915_READ(PCH_DPLL_SEL);
11887397 4986 temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
d925c59a 4987 I915_WRITE(PCH_DPLL_SEL, temp);
9db4a9c7 4988 }
e3421a18 4989
d925c59a
DV
4990 ironlake_fdi_pll_disable(intel_crtc);
4991 }
81b088ca 4992
b2c0593a 4993 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
81b088ca 4994 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
6be4a607 4995}
1b3c7a47 4996
4f771f10 4997static void haswell_crtc_disable(struct drm_crtc *crtc)
ee7b9f93 4998{
4f771f10 4999 struct drm_device *dev = crtc->dev;
fac5e23e 5000 struct drm_i915_private *dev_priv = to_i915(dev);
ee7b9f93 5001 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4f771f10 5002 struct intel_encoder *encoder;
6e3c9717 5003 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
ee7b9f93 5004
d2d65408
VS
5005 if (intel_crtc->config->has_pch_encoder)
5006 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5007 false);
5008
8807e55b
JN
5009 for_each_encoder_on_crtc(dev, crtc, encoder) {
5010 intel_opregion_notify_encoder(encoder, false);
4f771f10 5011 encoder->disable(encoder);
8807e55b 5012 }
4f771f10 5013
f9b61ff6
DV
5014 drm_crtc_vblank_off(crtc);
5015 assert_vblank_disabled(crtc);
5016
4d1de975 5017 /* XXX: Do the pipe assertions at the right place for BXT DSI. */
d7edc4e5 5018 if (!transcoder_is_dsi(cpu_transcoder))
4d1de975 5019 intel_disable_pipe(intel_crtc);
4f771f10 5020
6e3c9717 5021 if (intel_crtc->config->dp_encoder_is_mst)
a4bf214f
VS
5022 intel_ddi_set_vc_payload_alloc(crtc, false);
5023
d7edc4e5 5024 if (!transcoder_is_dsi(cpu_transcoder))
7d4aefd0 5025 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
4f771f10 5026
1c132b44 5027 if (INTEL_INFO(dev)->gen >= 9)
e435d6e5 5028 skylake_scaler_disable(intel_crtc);
ff6d9f55 5029 else
bfd16b2a 5030 ironlake_pfit_disable(intel_crtc, false);
4f771f10 5031
d7edc4e5 5032 if (!transcoder_is_dsi(cpu_transcoder))
7d4aefd0 5033 intel_ddi_disable_pipe_clock(intel_crtc);
4f771f10 5034
97b040aa
ID
5035 for_each_encoder_on_crtc(dev, crtc, encoder)
5036 if (encoder->post_disable)
5037 encoder->post_disable(encoder);
81b088ca 5038
92966a37
VS
5039 if (intel_crtc->config->has_pch_encoder) {
5040 lpt_disable_pch_transcoder(dev_priv);
503a74e9 5041 lpt_disable_iclkip(dev_priv);
92966a37
VS
5042 intel_ddi_fdi_disable(crtc);
5043
81b088ca
VS
5044 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5045 true);
92966a37 5046 }
4f771f10
PZ
5047}
5048
2dd24552
JB
5049static void i9xx_pfit_enable(struct intel_crtc *crtc)
5050{
5051 struct drm_device *dev = crtc->base.dev;
fac5e23e 5052 struct drm_i915_private *dev_priv = to_i915(dev);
6e3c9717 5053 struct intel_crtc_state *pipe_config = crtc->config;
2dd24552 5054
681a8504 5055 if (!pipe_config->gmch_pfit.control)
2dd24552
JB
5056 return;
5057
2dd24552 5058 /*
c0b03411
DV
5059 * The panel fitter should only be adjusted whilst the pipe is disabled,
5060 * according to register description and PRM.
2dd24552 5061 */
c0b03411
DV
5062 WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
5063 assert_pipe_disabled(dev_priv, crtc->pipe);
2dd24552 5064
b074cec8
JB
5065 I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
5066 I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
5a80c45c
DV
5067
5068 /* Border color in case we don't scale up to the full screen. Black by
5069 * default, change to something else for debugging. */
5070 I915_WRITE(BCLRPAT(crtc->pipe), 0);
2dd24552
JB
5071}
5072
d05410f9
DA
5073static enum intel_display_power_domain port_to_power_domain(enum port port)
5074{
5075 switch (port) {
5076 case PORT_A:
6331a704 5077 return POWER_DOMAIN_PORT_DDI_A_LANES;
d05410f9 5078 case PORT_B:
6331a704 5079 return POWER_DOMAIN_PORT_DDI_B_LANES;
d05410f9 5080 case PORT_C:
6331a704 5081 return POWER_DOMAIN_PORT_DDI_C_LANES;
d05410f9 5082 case PORT_D:
6331a704 5083 return POWER_DOMAIN_PORT_DDI_D_LANES;
d8e19f99 5084 case PORT_E:
6331a704 5085 return POWER_DOMAIN_PORT_DDI_E_LANES;
d05410f9 5086 default:
b9fec167 5087 MISSING_CASE(port);
d05410f9
DA
5088 return POWER_DOMAIN_PORT_OTHER;
5089 }
5090}
5091
25f78f58
VS
5092static enum intel_display_power_domain port_to_aux_power_domain(enum port port)
5093{
5094 switch (port) {
5095 case PORT_A:
5096 return POWER_DOMAIN_AUX_A;
5097 case PORT_B:
5098 return POWER_DOMAIN_AUX_B;
5099 case PORT_C:
5100 return POWER_DOMAIN_AUX_C;
5101 case PORT_D:
5102 return POWER_DOMAIN_AUX_D;
5103 case PORT_E:
5104 /* FIXME: Check VBT for actual wiring of PORT E */
5105 return POWER_DOMAIN_AUX_D;
5106 default:
b9fec167 5107 MISSING_CASE(port);
25f78f58
VS
5108 return POWER_DOMAIN_AUX_A;
5109 }
5110}
5111
319be8ae
ID
5112enum intel_display_power_domain
5113intel_display_port_power_domain(struct intel_encoder *intel_encoder)
5114{
5115 struct drm_device *dev = intel_encoder->base.dev;
5116 struct intel_digital_port *intel_dig_port;
5117
5118 switch (intel_encoder->type) {
5119 case INTEL_OUTPUT_UNKNOWN:
5120 /* Only DDI platforms should ever use this output type */
5121 WARN_ON_ONCE(!HAS_DDI(dev));
cca0502b 5122 case INTEL_OUTPUT_DP:
319be8ae
ID
5123 case INTEL_OUTPUT_HDMI:
5124 case INTEL_OUTPUT_EDP:
5125 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
d05410f9 5126 return port_to_power_domain(intel_dig_port->port);
0e32b39c
DA
5127 case INTEL_OUTPUT_DP_MST:
5128 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
5129 return port_to_power_domain(intel_dig_port->port);
319be8ae
ID
5130 case INTEL_OUTPUT_ANALOG:
5131 return POWER_DOMAIN_PORT_CRT;
5132 case INTEL_OUTPUT_DSI:
5133 return POWER_DOMAIN_PORT_DSI;
5134 default:
5135 return POWER_DOMAIN_PORT_OTHER;
5136 }
5137}
5138
25f78f58
VS
5139enum intel_display_power_domain
5140intel_display_port_aux_power_domain(struct intel_encoder *intel_encoder)
5141{
5142 struct drm_device *dev = intel_encoder->base.dev;
5143 struct intel_digital_port *intel_dig_port;
5144
5145 switch (intel_encoder->type) {
5146 case INTEL_OUTPUT_UNKNOWN:
651174a4
ID
5147 case INTEL_OUTPUT_HDMI:
5148 /*
5149 * Only DDI platforms should ever use these output types.
5150 * We can get here after the HDMI detect code has already set
5151 * the type of the shared encoder. Since we can't be sure
5152 * what's the status of the given connectors, play safe and
5153 * run the DP detection too.
5154 */
25f78f58 5155 WARN_ON_ONCE(!HAS_DDI(dev));
cca0502b 5156 case INTEL_OUTPUT_DP:
25f78f58
VS
5157 case INTEL_OUTPUT_EDP:
5158 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
5159 return port_to_aux_power_domain(intel_dig_port->port);
5160 case INTEL_OUTPUT_DP_MST:
5161 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
5162 return port_to_aux_power_domain(intel_dig_port->port);
5163 default:
b9fec167 5164 MISSING_CASE(intel_encoder->type);
25f78f58
VS
5165 return POWER_DOMAIN_AUX_A;
5166 }
5167}
5168
74bff5f9
ML
5169static unsigned long get_crtc_power_domains(struct drm_crtc *crtc,
5170 struct intel_crtc_state *crtc_state)
77d22dca 5171{
319be8ae 5172 struct drm_device *dev = crtc->dev;
74bff5f9 5173 struct drm_encoder *encoder;
319be8ae
ID
5174 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5175 enum pipe pipe = intel_crtc->pipe;
77d22dca 5176 unsigned long mask;
74bff5f9 5177 enum transcoder transcoder = crtc_state->cpu_transcoder;
77d22dca 5178
74bff5f9 5179 if (!crtc_state->base.active)
292b990e
ML
5180 return 0;
5181
77d22dca
ID
5182 mask = BIT(POWER_DOMAIN_PIPE(pipe));
5183 mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
74bff5f9
ML
5184 if (crtc_state->pch_pfit.enabled ||
5185 crtc_state->pch_pfit.force_thru)
77d22dca
ID
5186 mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
5187
74bff5f9
ML
5188 drm_for_each_encoder_mask(encoder, dev, crtc_state->base.encoder_mask) {
5189 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
5190
319be8ae 5191 mask |= BIT(intel_display_port_power_domain(intel_encoder));
74bff5f9 5192 }
319be8ae 5193
15e7ec29
ML
5194 if (crtc_state->shared_dpll)
5195 mask |= BIT(POWER_DOMAIN_PLLS);
5196
77d22dca
ID
5197 return mask;
5198}
5199
74bff5f9
ML
5200static unsigned long
5201modeset_get_crtc_power_domains(struct drm_crtc *crtc,
5202 struct intel_crtc_state *crtc_state)
77d22dca 5203{
fac5e23e 5204 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
292b990e
ML
5205 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5206 enum intel_display_power_domain domain;
5a21b665 5207 unsigned long domains, new_domains, old_domains;
77d22dca 5208
292b990e 5209 old_domains = intel_crtc->enabled_power_domains;
74bff5f9
ML
5210 intel_crtc->enabled_power_domains = new_domains =
5211 get_crtc_power_domains(crtc, crtc_state);
77d22dca 5212
5a21b665 5213 domains = new_domains & ~old_domains;
292b990e
ML
5214
5215 for_each_power_domain(domain, domains)
5216 intel_display_power_get(dev_priv, domain);
5217
5a21b665 5218 return old_domains & ~new_domains;
292b990e
ML
5219}
5220
5221static void modeset_put_power_domains(struct drm_i915_private *dev_priv,
5222 unsigned long domains)
5223{
5224 enum intel_display_power_domain domain;
5225
5226 for_each_power_domain(domain, domains)
5227 intel_display_power_put(dev_priv, domain);
5228}
77d22dca 5229
adafdc6f
MK
5230static int intel_compute_max_dotclk(struct drm_i915_private *dev_priv)
5231{
5232 int max_cdclk_freq = dev_priv->max_cdclk_freq;
5233
5234 if (INTEL_INFO(dev_priv)->gen >= 9 ||
5235 IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
5236 return max_cdclk_freq;
5237 else if (IS_CHERRYVIEW(dev_priv))
5238 return max_cdclk_freq*95/100;
5239 else if (INTEL_INFO(dev_priv)->gen < 4)
5240 return 2*max_cdclk_freq*90/100;
5241 else
5242 return max_cdclk_freq*90/100;
5243}
5244
b2045352
VS
5245static int skl_calc_cdclk(int max_pixclk, int vco);
5246
560a7ae4
DL
5247static void intel_update_max_cdclk(struct drm_device *dev)
5248{
fac5e23e 5249 struct drm_i915_private *dev_priv = to_i915(dev);
560a7ae4 5250
ef11bdb3 5251 if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) {
560a7ae4 5252 u32 limit = I915_READ(SKL_DFSM) & SKL_DFSM_CDCLK_LIMIT_MASK;
b2045352
VS
5253 int max_cdclk, vco;
5254
5255 vco = dev_priv->skl_preferred_vco_freq;
63911d72 5256 WARN_ON(vco != 8100000 && vco != 8640000);
560a7ae4 5257
b2045352
VS
5258 /*
5259 * Use the lower (vco 8640) cdclk values as a
5260 * first guess. skl_calc_cdclk() will correct it
5261 * if the preferred vco is 8100 instead.
5262 */
560a7ae4 5263 if (limit == SKL_DFSM_CDCLK_LIMIT_675)
487ed2e4 5264 max_cdclk = 617143;
560a7ae4 5265 else if (limit == SKL_DFSM_CDCLK_LIMIT_540)
b2045352 5266 max_cdclk = 540000;
560a7ae4 5267 else if (limit == SKL_DFSM_CDCLK_LIMIT_450)
b2045352 5268 max_cdclk = 432000;
560a7ae4 5269 else
487ed2e4 5270 max_cdclk = 308571;
b2045352
VS
5271
5272 dev_priv->max_cdclk_freq = skl_calc_cdclk(max_cdclk, vco);
281c114f
MR
5273 } else if (IS_BROXTON(dev)) {
5274 dev_priv->max_cdclk_freq = 624000;
560a7ae4
DL
5275 } else if (IS_BROADWELL(dev)) {
5276 /*
5277 * FIXME with extra cooling we can allow
5278 * 540 MHz for ULX and 675 Mhz for ULT.
5279 * How can we know if extra cooling is
5280 * available? PCI ID, VTB, something else?
5281 */
5282 if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
5283 dev_priv->max_cdclk_freq = 450000;
5284 else if (IS_BDW_ULX(dev))
5285 dev_priv->max_cdclk_freq = 450000;
5286 else if (IS_BDW_ULT(dev))
5287 dev_priv->max_cdclk_freq = 540000;
5288 else
5289 dev_priv->max_cdclk_freq = 675000;
0904deaf
MK
5290 } else if (IS_CHERRYVIEW(dev)) {
5291 dev_priv->max_cdclk_freq = 320000;
560a7ae4
DL
5292 } else if (IS_VALLEYVIEW(dev)) {
5293 dev_priv->max_cdclk_freq = 400000;
5294 } else {
5295 /* otherwise assume cdclk is fixed */
5296 dev_priv->max_cdclk_freq = dev_priv->cdclk_freq;
5297 }
5298
adafdc6f
MK
5299 dev_priv->max_dotclk_freq = intel_compute_max_dotclk(dev_priv);
5300
560a7ae4
DL
5301 DRM_DEBUG_DRIVER("Max CD clock rate: %d kHz\n",
5302 dev_priv->max_cdclk_freq);
adafdc6f
MK
5303
5304 DRM_DEBUG_DRIVER("Max dotclock rate: %d kHz\n",
5305 dev_priv->max_dotclk_freq);
560a7ae4
DL
5306}
5307
5308static void intel_update_cdclk(struct drm_device *dev)
5309{
fac5e23e 5310 struct drm_i915_private *dev_priv = to_i915(dev);
560a7ae4
DL
5311
5312 dev_priv->cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
2f2a121a 5313
83d7c81f 5314 if (INTEL_GEN(dev_priv) >= 9)
709e05c3
VS
5315 DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz, VCO: %d kHz, ref: %d kHz\n",
5316 dev_priv->cdclk_freq, dev_priv->cdclk_pll.vco,
5317 dev_priv->cdclk_pll.ref);
2f2a121a
VS
5318 else
5319 DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
5320 dev_priv->cdclk_freq);
560a7ae4
DL
5321
5322 /*
b5d99ff9
VS
5323 * 9:0 CMBUS [sic] CDCLK frequency (cdfreq):
5324 * Programmng [sic] note: bit[9:2] should be programmed to the number
5325 * of cdclk that generates 4MHz reference clock freq which is used to
5326 * generate GMBus clock. This will vary with the cdclk freq.
560a7ae4 5327 */
b5d99ff9 5328 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
560a7ae4 5329 I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->cdclk_freq, 1000));
560a7ae4
DL
5330}
5331
92891e45
VS
5332/* convert from kHz to .1 fixpoint MHz with -1MHz offset */
5333static int skl_cdclk_decimal(int cdclk)
5334{
5335 return DIV_ROUND_CLOSEST(cdclk - 1000, 500);
5336}
5337
5f199dfa
VS
5338static int bxt_de_pll_vco(struct drm_i915_private *dev_priv, int cdclk)
5339{
5340 int ratio;
5341
5342 if (cdclk == dev_priv->cdclk_pll.ref)
5343 return 0;
5344
5345 switch (cdclk) {
5346 default:
5347 MISSING_CASE(cdclk);
5348 case 144000:
5349 case 288000:
5350 case 384000:
5351 case 576000:
5352 ratio = 60;
5353 break;
5354 case 624000:
5355 ratio = 65;
5356 break;
5357 }
5358
5359 return dev_priv->cdclk_pll.ref * ratio;
5360}
5361
2b73001e
VS
5362static void bxt_de_pll_disable(struct drm_i915_private *dev_priv)
5363{
5364 I915_WRITE(BXT_DE_PLL_ENABLE, 0);
5365
5366 /* Timeout 200us */
95cac283
CW
5367 if (intel_wait_for_register(dev_priv,
5368 BXT_DE_PLL_ENABLE, BXT_DE_PLL_LOCK, 0,
5369 1))
2b73001e 5370 DRM_ERROR("timeout waiting for DE PLL unlock\n");
83d7c81f
VS
5371
5372 dev_priv->cdclk_pll.vco = 0;
2b73001e
VS
5373}
5374
5f199dfa 5375static void bxt_de_pll_enable(struct drm_i915_private *dev_priv, int vco)
2b73001e 5376{
5f199dfa 5377 int ratio = DIV_ROUND_CLOSEST(vco, dev_priv->cdclk_pll.ref);
2b73001e
VS
5378 u32 val;
5379
5380 val = I915_READ(BXT_DE_PLL_CTL);
5381 val &= ~BXT_DE_PLL_RATIO_MASK;
5f199dfa 5382 val |= BXT_DE_PLL_RATIO(ratio);
2b73001e
VS
5383 I915_WRITE(BXT_DE_PLL_CTL, val);
5384
5385 I915_WRITE(BXT_DE_PLL_ENABLE, BXT_DE_PLL_PLL_ENABLE);
5386
5387 /* Timeout 200us */
e084e1b9
CW
5388 if (intel_wait_for_register(dev_priv,
5389 BXT_DE_PLL_ENABLE,
5390 BXT_DE_PLL_LOCK,
5391 BXT_DE_PLL_LOCK,
5392 1))
2b73001e 5393 DRM_ERROR("timeout waiting for DE PLL lock\n");
83d7c81f 5394
5f199dfa 5395 dev_priv->cdclk_pll.vco = vco;
2b73001e
VS
5396}
5397
324513c0 5398static void bxt_set_cdclk(struct drm_i915_private *dev_priv, int cdclk)
f8437dd1 5399{
5f199dfa
VS
5400 u32 val, divider;
5401 int vco, ret;
f8437dd1 5402
5f199dfa
VS
5403 vco = bxt_de_pll_vco(dev_priv, cdclk);
5404
5405 DRM_DEBUG_DRIVER("Changing CDCLK to %d kHz (VCO %d kHz)\n", cdclk, vco);
5406
5407 /* cdclk = vco / 2 / div{1,1.5,2,4} */
5408 switch (DIV_ROUND_CLOSEST(vco, cdclk)) {
5409 case 8:
f8437dd1 5410 divider = BXT_CDCLK_CD2X_DIV_SEL_4;
f8437dd1 5411 break;
5f199dfa 5412 case 4:
f8437dd1 5413 divider = BXT_CDCLK_CD2X_DIV_SEL_2;
f8437dd1 5414 break;
5f199dfa 5415 case 3:
f8437dd1 5416 divider = BXT_CDCLK_CD2X_DIV_SEL_1_5;
f8437dd1 5417 break;
5f199dfa 5418 case 2:
f8437dd1 5419 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
f8437dd1
VK
5420 break;
5421 default:
5f199dfa
VS
5422 WARN_ON(cdclk != dev_priv->cdclk_pll.ref);
5423 WARN_ON(vco != 0);
f8437dd1 5424
5f199dfa
VS
5425 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
5426 break;
f8437dd1
VK
5427 }
5428
f8437dd1 5429 /* Inform power controller of upcoming frequency change */
5f199dfa 5430 mutex_lock(&dev_priv->rps.hw_lock);
f8437dd1
VK
5431 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
5432 0x80000000);
5433 mutex_unlock(&dev_priv->rps.hw_lock);
5434
5435 if (ret) {
5436 DRM_ERROR("PCode CDCLK freq change notify failed (err %d, freq %d)\n",
9ef56154 5437 ret, cdclk);
f8437dd1
VK
5438 return;
5439 }
5440
5f199dfa
VS
5441 if (dev_priv->cdclk_pll.vco != 0 &&
5442 dev_priv->cdclk_pll.vco != vco)
2b73001e 5443 bxt_de_pll_disable(dev_priv);
f8437dd1 5444
5f199dfa
VS
5445 if (dev_priv->cdclk_pll.vco != vco)
5446 bxt_de_pll_enable(dev_priv, vco);
f8437dd1 5447
5f199dfa
VS
5448 val = divider | skl_cdclk_decimal(cdclk);
5449 /*
5450 * FIXME if only the cd2x divider needs changing, it could be done
5451 * without shutting off the pipe (if only one pipe is active).
5452 */
5453 val |= BXT_CDCLK_CD2X_PIPE_NONE;
5454 /*
5455 * Disable SSA Precharge when CD clock frequency < 500 MHz,
5456 * enable otherwise.
5457 */
5458 if (cdclk >= 500000)
5459 val |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
5460 I915_WRITE(CDCLK_CTL, val);
f8437dd1
VK
5461
5462 mutex_lock(&dev_priv->rps.hw_lock);
5463 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
9ef56154 5464 DIV_ROUND_UP(cdclk, 25000));
f8437dd1
VK
5465 mutex_unlock(&dev_priv->rps.hw_lock);
5466
5467 if (ret) {
5468 DRM_ERROR("PCode CDCLK freq set failed, (err %d, freq %d)\n",
9ef56154 5469 ret, cdclk);
f8437dd1
VK
5470 return;
5471 }
5472
91c8a326 5473 intel_update_cdclk(&dev_priv->drm);
f8437dd1
VK
5474}
5475
d66a2194 5476static void bxt_sanitize_cdclk(struct drm_i915_private *dev_priv)
f8437dd1 5477{
d66a2194
ID
5478 u32 cdctl, expected;
5479
91c8a326 5480 intel_update_cdclk(&dev_priv->drm);
f8437dd1 5481
d66a2194
ID
5482 if (dev_priv->cdclk_pll.vco == 0 ||
5483 dev_priv->cdclk_freq == dev_priv->cdclk_pll.ref)
5484 goto sanitize;
5485
5486 /* DPLL okay; verify the cdclock
5487 *
5488 * Some BIOS versions leave an incorrect decimal frequency value and
5489 * set reserved MBZ bits in CDCLK_CTL at least during exiting from S4,
5490 * so sanitize this register.
5491 */
5492 cdctl = I915_READ(CDCLK_CTL);
5493 /*
5494 * Let's ignore the pipe field, since BIOS could have configured the
5495 * dividers both synching to an active pipe, or asynchronously
5496 * (PIPE_NONE).
5497 */
5498 cdctl &= ~BXT_CDCLK_CD2X_PIPE_NONE;
5499
5500 expected = (cdctl & BXT_CDCLK_CD2X_DIV_SEL_MASK) |
5501 skl_cdclk_decimal(dev_priv->cdclk_freq);
5502 /*
5503 * Disable SSA Precharge when CD clock frequency < 500 MHz,
5504 * enable otherwise.
5505 */
5506 if (dev_priv->cdclk_freq >= 500000)
5507 expected |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
5508
5509 if (cdctl == expected)
5510 /* All well; nothing to sanitize */
5511 return;
5512
5513sanitize:
5514 DRM_DEBUG_KMS("Sanitizing cdclk programmed by pre-os\n");
5515
5516 /* force cdclk programming */
5517 dev_priv->cdclk_freq = 0;
5518
5519 /* force full PLL disable + enable */
5520 dev_priv->cdclk_pll.vco = -1;
5521}
5522
324513c0 5523void bxt_init_cdclk(struct drm_i915_private *dev_priv)
d66a2194
ID
5524{
5525 bxt_sanitize_cdclk(dev_priv);
5526
5527 if (dev_priv->cdclk_freq != 0 && dev_priv->cdclk_pll.vco != 0)
089c6fd5 5528 return;
c2e001ef 5529
f8437dd1
VK
5530 /*
5531 * FIXME:
5532 * - The initial CDCLK needs to be read from VBT.
5533 * Need to make this change after VBT has changes for BXT.
f8437dd1 5534 */
324513c0 5535 bxt_set_cdclk(dev_priv, bxt_calc_cdclk(0));
f8437dd1
VK
5536}
5537
324513c0 5538void bxt_uninit_cdclk(struct drm_i915_private *dev_priv)
f8437dd1 5539{
324513c0 5540 bxt_set_cdclk(dev_priv, dev_priv->cdclk_pll.ref);
f8437dd1
VK
5541}
5542
a8ca4934
VS
5543static int skl_calc_cdclk(int max_pixclk, int vco)
5544{
63911d72 5545 if (vco == 8640000) {
a8ca4934 5546 if (max_pixclk > 540000)
487ed2e4 5547 return 617143;
a8ca4934
VS
5548 else if (max_pixclk > 432000)
5549 return 540000;
487ed2e4 5550 else if (max_pixclk > 308571)
a8ca4934
VS
5551 return 432000;
5552 else
487ed2e4 5553 return 308571;
a8ca4934 5554 } else {
a8ca4934
VS
5555 if (max_pixclk > 540000)
5556 return 675000;
5557 else if (max_pixclk > 450000)
5558 return 540000;
5559 else if (max_pixclk > 337500)
5560 return 450000;
5561 else
5562 return 337500;
5563 }
5564}
5565
ea61791e
VS
5566static void
5567skl_dpll0_update(struct drm_i915_private *dev_priv)
5d96d8af 5568{
ea61791e 5569 u32 val;
5d96d8af 5570
709e05c3 5571 dev_priv->cdclk_pll.ref = 24000;
1c3f7700 5572 dev_priv->cdclk_pll.vco = 0;
709e05c3 5573
ea61791e 5574 val = I915_READ(LCPLL1_CTL);
1c3f7700 5575 if ((val & LCPLL_PLL_ENABLE) == 0)
ea61791e 5576 return;
5d96d8af 5577
1c3f7700
ID
5578 if (WARN_ON((val & LCPLL_PLL_LOCK) == 0))
5579 return;
9f7eb31a 5580
ea61791e
VS
5581 val = I915_READ(DPLL_CTRL1);
5582
1c3f7700
ID
5583 if (WARN_ON((val & (DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) |
5584 DPLL_CTRL1_SSC(SKL_DPLL0) |
5585 DPLL_CTRL1_OVERRIDE(SKL_DPLL0))) !=
5586 DPLL_CTRL1_OVERRIDE(SKL_DPLL0)))
5587 return;
9f7eb31a 5588
ea61791e
VS
5589 switch (val & DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0)) {
5590 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810, SKL_DPLL0):
5591 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1350, SKL_DPLL0):
5592 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1620, SKL_DPLL0):
5593 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_2700, SKL_DPLL0):
63911d72 5594 dev_priv->cdclk_pll.vco = 8100000;
ea61791e
VS
5595 break;
5596 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080, SKL_DPLL0):
5597 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_2160, SKL_DPLL0):
63911d72 5598 dev_priv->cdclk_pll.vco = 8640000;
ea61791e
VS
5599 break;
5600 default:
5601 MISSING_CASE(val & DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
ea61791e
VS
5602 break;
5603 }
5d96d8af
DL
5604}
5605
b2045352
VS
5606void skl_set_preferred_cdclk_vco(struct drm_i915_private *dev_priv, int vco)
5607{
5608 bool changed = dev_priv->skl_preferred_vco_freq != vco;
5609
5610 dev_priv->skl_preferred_vco_freq = vco;
5611
5612 if (changed)
91c8a326 5613 intel_update_max_cdclk(&dev_priv->drm);
b2045352
VS
5614}
5615
5d96d8af 5616static void
3861fc60 5617skl_dpll0_enable(struct drm_i915_private *dev_priv, int vco)
5d96d8af 5618{
a8ca4934 5619 int min_cdclk = skl_calc_cdclk(0, vco);
5d96d8af
DL
5620 u32 val;
5621
63911d72 5622 WARN_ON(vco != 8100000 && vco != 8640000);
b2045352 5623
5d96d8af 5624 /* select the minimum CDCLK before enabling DPLL 0 */
9ef56154 5625 val = CDCLK_FREQ_337_308 | skl_cdclk_decimal(min_cdclk);
5d96d8af
DL
5626 I915_WRITE(CDCLK_CTL, val);
5627 POSTING_READ(CDCLK_CTL);
5628
5629 /*
5630 * We always enable DPLL0 with the lowest link rate possible, but still
5631 * taking into account the VCO required to operate the eDP panel at the
5632 * desired frequency. The usual DP link rates operate with a VCO of
5633 * 8100 while the eDP 1.4 alternate link rates need a VCO of 8640.
5634 * The modeset code is responsible for the selection of the exact link
5635 * rate later on, with the constraint of choosing a frequency that
a8ca4934 5636 * works with vco.
5d96d8af
DL
5637 */
5638 val = I915_READ(DPLL_CTRL1);
5639
5640 val &= ~(DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) | DPLL_CTRL1_SSC(SKL_DPLL0) |
5641 DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
5642 val |= DPLL_CTRL1_OVERRIDE(SKL_DPLL0);
63911d72 5643 if (vco == 8640000)
5d96d8af
DL
5644 val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080,
5645 SKL_DPLL0);
5646 else
5647 val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810,
5648 SKL_DPLL0);
5649
5650 I915_WRITE(DPLL_CTRL1, val);
5651 POSTING_READ(DPLL_CTRL1);
5652
5653 I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) | LCPLL_PLL_ENABLE);
5654
e24ca054
CW
5655 if (intel_wait_for_register(dev_priv,
5656 LCPLL1_CTL, LCPLL_PLL_LOCK, LCPLL_PLL_LOCK,
5657 5))
5d96d8af 5658 DRM_ERROR("DPLL0 not locked\n");
1cd593e0 5659
63911d72 5660 dev_priv->cdclk_pll.vco = vco;
b2045352
VS
5661
5662 /* We'll want to keep using the current vco from now on. */
5663 skl_set_preferred_cdclk_vco(dev_priv, vco);
5d96d8af
DL
5664}
5665
430e05de
VS
5666static void
5667skl_dpll0_disable(struct drm_i915_private *dev_priv)
5668{
5669 I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) & ~LCPLL_PLL_ENABLE);
8ad32a05
CW
5670 if (intel_wait_for_register(dev_priv,
5671 LCPLL1_CTL, LCPLL_PLL_LOCK, 0,
5672 1))
430e05de 5673 DRM_ERROR("Couldn't disable DPLL0\n");
1cd593e0 5674
63911d72 5675 dev_priv->cdclk_pll.vco = 0;
430e05de
VS
5676}
5677
5d96d8af
DL
5678static bool skl_cdclk_pcu_ready(struct drm_i915_private *dev_priv)
5679{
5680 int ret;
5681 u32 val;
5682
5683 /* inform PCU we want to change CDCLK */
5684 val = SKL_CDCLK_PREPARE_FOR_CHANGE;
5685 mutex_lock(&dev_priv->rps.hw_lock);
5686 ret = sandybridge_pcode_read(dev_priv, SKL_PCODE_CDCLK_CONTROL, &val);
5687 mutex_unlock(&dev_priv->rps.hw_lock);
5688
5689 return ret == 0 && (val & SKL_CDCLK_READY_FOR_CHANGE);
5690}
5691
5692static bool skl_cdclk_wait_for_pcu_ready(struct drm_i915_private *dev_priv)
5693{
5694 unsigned int i;
5695
5696 for (i = 0; i < 15; i++) {
5697 if (skl_cdclk_pcu_ready(dev_priv))
5698 return true;
5699 udelay(10);
5700 }
5701
5702 return false;
5703}
5704
1cd593e0 5705static void skl_set_cdclk(struct drm_i915_private *dev_priv, int cdclk, int vco)
5d96d8af 5706{
91c8a326 5707 struct drm_device *dev = &dev_priv->drm;
5d96d8af
DL
5708 u32 freq_select, pcu_ack;
5709
1cd593e0
VS
5710 WARN_ON((cdclk == 24000) != (vco == 0));
5711
63911d72 5712 DRM_DEBUG_DRIVER("Changing CDCLK to %d kHz (VCO %d kHz)\n", cdclk, vco);
5d96d8af
DL
5713
5714 if (!skl_cdclk_wait_for_pcu_ready(dev_priv)) {
5715 DRM_ERROR("failed to inform PCU about cdclk change\n");
5716 return;
5717 }
5718
5719 /* set CDCLK_CTL */
9ef56154 5720 switch (cdclk) {
5d96d8af
DL
5721 case 450000:
5722 case 432000:
5723 freq_select = CDCLK_FREQ_450_432;
5724 pcu_ack = 1;
5725 break;
5726 case 540000:
5727 freq_select = CDCLK_FREQ_540;
5728 pcu_ack = 2;
5729 break;
487ed2e4 5730 case 308571:
5d96d8af
DL
5731 case 337500:
5732 default:
5733 freq_select = CDCLK_FREQ_337_308;
5734 pcu_ack = 0;
5735 break;
487ed2e4 5736 case 617143:
5d96d8af
DL
5737 case 675000:
5738 freq_select = CDCLK_FREQ_675_617;
5739 pcu_ack = 3;
5740 break;
5741 }
5742
63911d72
VS
5743 if (dev_priv->cdclk_pll.vco != 0 &&
5744 dev_priv->cdclk_pll.vco != vco)
1cd593e0
VS
5745 skl_dpll0_disable(dev_priv);
5746
63911d72 5747 if (dev_priv->cdclk_pll.vco != vco)
1cd593e0
VS
5748 skl_dpll0_enable(dev_priv, vco);
5749
9ef56154 5750 I915_WRITE(CDCLK_CTL, freq_select | skl_cdclk_decimal(cdclk));
5d96d8af
DL
5751 POSTING_READ(CDCLK_CTL);
5752
5753 /* inform PCU of the change */
5754 mutex_lock(&dev_priv->rps.hw_lock);
5755 sandybridge_pcode_write(dev_priv, SKL_PCODE_CDCLK_CONTROL, pcu_ack);
5756 mutex_unlock(&dev_priv->rps.hw_lock);
560a7ae4
DL
5757
5758 intel_update_cdclk(dev);
5d96d8af
DL
5759}
5760
9f7eb31a
VS
5761static void skl_sanitize_cdclk(struct drm_i915_private *dev_priv);
5762
5d96d8af
DL
5763void skl_uninit_cdclk(struct drm_i915_private *dev_priv)
5764{
709e05c3 5765 skl_set_cdclk(dev_priv, dev_priv->cdclk_pll.ref, 0);
5d96d8af
DL
5766}
5767
5768void skl_init_cdclk(struct drm_i915_private *dev_priv)
5769{
9f7eb31a
VS
5770 int cdclk, vco;
5771
5772 skl_sanitize_cdclk(dev_priv);
5d96d8af 5773
63911d72 5774 if (dev_priv->cdclk_freq != 0 && dev_priv->cdclk_pll.vco != 0) {
9f7eb31a
VS
5775 /*
5776 * Use the current vco as our initial
5777 * guess as to what the preferred vco is.
5778 */
5779 if (dev_priv->skl_preferred_vco_freq == 0)
5780 skl_set_preferred_cdclk_vco(dev_priv,
63911d72 5781 dev_priv->cdclk_pll.vco);
70c2c184 5782 return;
1cd593e0 5783 }
5d96d8af 5784
70c2c184
VS
5785 vco = dev_priv->skl_preferred_vco_freq;
5786 if (vco == 0)
63911d72 5787 vco = 8100000;
70c2c184 5788 cdclk = skl_calc_cdclk(0, vco);
5d96d8af 5789
70c2c184 5790 skl_set_cdclk(dev_priv, cdclk, vco);
5d96d8af
DL
5791}
5792
9f7eb31a 5793static void skl_sanitize_cdclk(struct drm_i915_private *dev_priv)
c73666f3 5794{
09492498 5795 uint32_t cdctl, expected;
c73666f3 5796
f1b391a5
SK
5797 /*
5798 * check if the pre-os intialized the display
5799 * There is SWF18 scratchpad register defined which is set by the
5800 * pre-os which can be used by the OS drivers to check the status
5801 */
5802 if ((I915_READ(SWF_ILK(0x18)) & 0x00FFFFFF) == 0)
5803 goto sanitize;
5804
91c8a326 5805 intel_update_cdclk(&dev_priv->drm);
c73666f3 5806 /* Is PLL enabled and locked ? */
1c3f7700
ID
5807 if (dev_priv->cdclk_pll.vco == 0 ||
5808 dev_priv->cdclk_freq == dev_priv->cdclk_pll.ref)
c73666f3
SK
5809 goto sanitize;
5810
5811 /* DPLL okay; verify the cdclock
5812 *
5813 * Noticed in some instances that the freq selection is correct but
5814 * decimal part is programmed wrong from BIOS where pre-os does not
5815 * enable display. Verify the same as well.
5816 */
09492498
VS
5817 cdctl = I915_READ(CDCLK_CTL);
5818 expected = (cdctl & CDCLK_FREQ_SEL_MASK) |
5819 skl_cdclk_decimal(dev_priv->cdclk_freq);
5820 if (cdctl == expected)
c73666f3 5821 /* All well; nothing to sanitize */
9f7eb31a 5822 return;
c89e39f3 5823
9f7eb31a
VS
5824sanitize:
5825 DRM_DEBUG_KMS("Sanitizing cdclk programmed by pre-os\n");
c73666f3 5826
9f7eb31a
VS
5827 /* force cdclk programming */
5828 dev_priv->cdclk_freq = 0;
5829 /* force full PLL disable + enable */
63911d72 5830 dev_priv->cdclk_pll.vco = -1;
c73666f3
SK
5831}
5832
30a970c6
JB
5833/* Adjust CDclk dividers to allow high res or save power if possible */
5834static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
5835{
fac5e23e 5836 struct drm_i915_private *dev_priv = to_i915(dev);
30a970c6
JB
5837 u32 val, cmd;
5838
164dfd28
VK
5839 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5840 != dev_priv->cdclk_freq);
d60c4473 5841
dfcab17e 5842 if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
30a970c6 5843 cmd = 2;
dfcab17e 5844 else if (cdclk == 266667)
30a970c6
JB
5845 cmd = 1;
5846 else
5847 cmd = 0;
5848
5849 mutex_lock(&dev_priv->rps.hw_lock);
5850 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5851 val &= ~DSPFREQGUAR_MASK;
5852 val |= (cmd << DSPFREQGUAR_SHIFT);
5853 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5854 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5855 DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
5856 50)) {
5857 DRM_ERROR("timed out waiting for CDclk change\n");
5858 }
5859 mutex_unlock(&dev_priv->rps.hw_lock);
5860
54433e91
VS
5861 mutex_lock(&dev_priv->sb_lock);
5862
dfcab17e 5863 if (cdclk == 400000) {
6bcda4f0 5864 u32 divider;
30a970c6 5865
6bcda4f0 5866 divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
30a970c6 5867
30a970c6
JB
5868 /* adjust cdclk divider */
5869 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
87d5d259 5870 val &= ~CCK_FREQUENCY_VALUES;
30a970c6
JB
5871 val |= divider;
5872 vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
a877e801
VS
5873
5874 if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
87d5d259 5875 CCK_FREQUENCY_STATUS) == (divider << CCK_FREQUENCY_STATUS_SHIFT),
a877e801
VS
5876 50))
5877 DRM_ERROR("timed out waiting for CDclk change\n");
30a970c6
JB
5878 }
5879
30a970c6
JB
5880 /* adjust self-refresh exit latency value */
5881 val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
5882 val &= ~0x7f;
5883
5884 /*
5885 * For high bandwidth configs, we set a higher latency in the bunit
5886 * so that the core display fetch happens in time to avoid underruns.
5887 */
dfcab17e 5888 if (cdclk == 400000)
30a970c6
JB
5889 val |= 4500 / 250; /* 4.5 usec */
5890 else
5891 val |= 3000 / 250; /* 3.0 usec */
5892 vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
54433e91 5893
a580516d 5894 mutex_unlock(&dev_priv->sb_lock);
30a970c6 5895
b6283055 5896 intel_update_cdclk(dev);
30a970c6
JB
5897}
5898
383c5a6a
VS
5899static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
5900{
fac5e23e 5901 struct drm_i915_private *dev_priv = to_i915(dev);
383c5a6a
VS
5902 u32 val, cmd;
5903
164dfd28
VK
5904 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5905 != dev_priv->cdclk_freq);
383c5a6a
VS
5906
5907 switch (cdclk) {
383c5a6a
VS
5908 case 333333:
5909 case 320000:
383c5a6a 5910 case 266667:
383c5a6a 5911 case 200000:
383c5a6a
VS
5912 break;
5913 default:
5f77eeb0 5914 MISSING_CASE(cdclk);
383c5a6a
VS
5915 return;
5916 }
5917
9d0d3fda
VS
5918 /*
5919 * Specs are full of misinformation, but testing on actual
5920 * hardware has shown that we just need to write the desired
5921 * CCK divider into the Punit register.
5922 */
5923 cmd = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
5924
383c5a6a
VS
5925 mutex_lock(&dev_priv->rps.hw_lock);
5926 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5927 val &= ~DSPFREQGUAR_MASK_CHV;
5928 val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
5929 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5930 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5931 DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
5932 50)) {
5933 DRM_ERROR("timed out waiting for CDclk change\n");
5934 }
5935 mutex_unlock(&dev_priv->rps.hw_lock);
5936
b6283055 5937 intel_update_cdclk(dev);
383c5a6a
VS
5938}
5939
30a970c6
JB
5940static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
5941 int max_pixclk)
5942{
6bcda4f0 5943 int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
6cca3195 5944 int limit = IS_CHERRYVIEW(dev_priv) ? 95 : 90;
29dc7ef3 5945
30a970c6
JB
5946 /*
5947 * Really only a few cases to deal with, as only 4 CDclks are supported:
5948 * 200MHz
5949 * 267MHz
29dc7ef3 5950 * 320/333MHz (depends on HPLL freq)
6cca3195
VS
5951 * 400MHz (VLV only)
5952 * So we check to see whether we're above 90% (VLV) or 95% (CHV)
5953 * of the lower bin and adjust if needed.
e37c67a1
VS
5954 *
5955 * We seem to get an unstable or solid color picture at 200MHz.
5956 * Not sure what's wrong. For now use 200MHz only when all pipes
5957 * are off.
30a970c6 5958 */
6cca3195
VS
5959 if (!IS_CHERRYVIEW(dev_priv) &&
5960 max_pixclk > freq_320*limit/100)
dfcab17e 5961 return 400000;
6cca3195 5962 else if (max_pixclk > 266667*limit/100)
29dc7ef3 5963 return freq_320;
e37c67a1 5964 else if (max_pixclk > 0)
dfcab17e 5965 return 266667;
e37c67a1
VS
5966 else
5967 return 200000;
30a970c6
JB
5968}
5969
324513c0 5970static int bxt_calc_cdclk(int max_pixclk)
f8437dd1 5971{
760e1477 5972 if (max_pixclk > 576000)
f8437dd1 5973 return 624000;
760e1477 5974 else if (max_pixclk > 384000)
f8437dd1 5975 return 576000;
760e1477 5976 else if (max_pixclk > 288000)
f8437dd1 5977 return 384000;
760e1477 5978 else if (max_pixclk > 144000)
f8437dd1
VK
5979 return 288000;
5980 else
5981 return 144000;
5982}
5983
e8788cbc 5984/* Compute the max pixel clock for new configuration. */
a821fc46
ACO
5985static int intel_mode_max_pixclk(struct drm_device *dev,
5986 struct drm_atomic_state *state)
30a970c6 5987{
565602d7 5988 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
fac5e23e 5989 struct drm_i915_private *dev_priv = to_i915(dev);
565602d7
ML
5990 struct drm_crtc *crtc;
5991 struct drm_crtc_state *crtc_state;
5992 unsigned max_pixclk = 0, i;
5993 enum pipe pipe;
30a970c6 5994
565602d7
ML
5995 memcpy(intel_state->min_pixclk, dev_priv->min_pixclk,
5996 sizeof(intel_state->min_pixclk));
304603f4 5997
565602d7
ML
5998 for_each_crtc_in_state(state, crtc, crtc_state, i) {
5999 int pixclk = 0;
6000
6001 if (crtc_state->enable)
6002 pixclk = crtc_state->adjusted_mode.crtc_clock;
304603f4 6003
565602d7 6004 intel_state->min_pixclk[i] = pixclk;
30a970c6
JB
6005 }
6006
565602d7
ML
6007 for_each_pipe(dev_priv, pipe)
6008 max_pixclk = max(intel_state->min_pixclk[pipe], max_pixclk);
6009
30a970c6
JB
6010 return max_pixclk;
6011}
6012
27c329ed 6013static int valleyview_modeset_calc_cdclk(struct drm_atomic_state *state)
30a970c6 6014{
27c329ed 6015 struct drm_device *dev = state->dev;
fac5e23e 6016 struct drm_i915_private *dev_priv = to_i915(dev);
27c329ed 6017 int max_pixclk = intel_mode_max_pixclk(dev, state);
1a617b77
ML
6018 struct intel_atomic_state *intel_state =
6019 to_intel_atomic_state(state);
30a970c6 6020
1a617b77 6021 intel_state->cdclk = intel_state->dev_cdclk =
27c329ed 6022 valleyview_calc_cdclk(dev_priv, max_pixclk);
0a9ab303 6023
1a617b77
ML
6024 if (!intel_state->active_crtcs)
6025 intel_state->dev_cdclk = valleyview_calc_cdclk(dev_priv, 0);
6026
27c329ed
ML
6027 return 0;
6028}
304603f4 6029
324513c0 6030static int bxt_modeset_calc_cdclk(struct drm_atomic_state *state)
27c329ed 6031{
4e5ca60f 6032 int max_pixclk = ilk_max_pixel_rate(state);
1a617b77
ML
6033 struct intel_atomic_state *intel_state =
6034 to_intel_atomic_state(state);
85a96e7a 6035
1a617b77 6036 intel_state->cdclk = intel_state->dev_cdclk =
324513c0 6037 bxt_calc_cdclk(max_pixclk);
85a96e7a 6038
1a617b77 6039 if (!intel_state->active_crtcs)
324513c0 6040 intel_state->dev_cdclk = bxt_calc_cdclk(0);
1a617b77 6041
27c329ed 6042 return 0;
30a970c6
JB
6043}
6044
1e69cd74
VS
6045static void vlv_program_pfi_credits(struct drm_i915_private *dev_priv)
6046{
6047 unsigned int credits, default_credits;
6048
6049 if (IS_CHERRYVIEW(dev_priv))
6050 default_credits = PFI_CREDIT(12);
6051 else
6052 default_credits = PFI_CREDIT(8);
6053
bfa7df01 6054 if (dev_priv->cdclk_freq >= dev_priv->czclk_freq) {
1e69cd74
VS
6055 /* CHV suggested value is 31 or 63 */
6056 if (IS_CHERRYVIEW(dev_priv))
fcc0008f 6057 credits = PFI_CREDIT_63;
1e69cd74
VS
6058 else
6059 credits = PFI_CREDIT(15);
6060 } else {
6061 credits = default_credits;
6062 }
6063
6064 /*
6065 * WA - write default credits before re-programming
6066 * FIXME: should we also set the resend bit here?
6067 */
6068 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
6069 default_credits);
6070
6071 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
6072 credits | PFI_CREDIT_RESEND);
6073
6074 /*
6075 * FIXME is this guaranteed to clear
6076 * immediately or should we poll for it?
6077 */
6078 WARN_ON(I915_READ(GCI_CONTROL) & PFI_CREDIT_RESEND);
6079}
6080
27c329ed 6081static void valleyview_modeset_commit_cdclk(struct drm_atomic_state *old_state)
30a970c6 6082{
a821fc46 6083 struct drm_device *dev = old_state->dev;
fac5e23e 6084 struct drm_i915_private *dev_priv = to_i915(dev);
1a617b77
ML
6085 struct intel_atomic_state *old_intel_state =
6086 to_intel_atomic_state(old_state);
6087 unsigned req_cdclk = old_intel_state->dev_cdclk;
30a970c6 6088
27c329ed
ML
6089 /*
6090 * FIXME: We can end up here with all power domains off, yet
6091 * with a CDCLK frequency other than the minimum. To account
6092 * for this take the PIPE-A power domain, which covers the HW
6093 * blocks needed for the following programming. This can be
6094 * removed once it's guaranteed that we get here either with
6095 * the minimum CDCLK set, or the required power domains
6096 * enabled.
6097 */
6098 intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
738c05c0 6099
27c329ed
ML
6100 if (IS_CHERRYVIEW(dev))
6101 cherryview_set_cdclk(dev, req_cdclk);
6102 else
6103 valleyview_set_cdclk(dev, req_cdclk);
738c05c0 6104
27c329ed 6105 vlv_program_pfi_credits(dev_priv);
1e69cd74 6106
27c329ed 6107 intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
30a970c6
JB
6108}
6109
89b667f8
JB
6110static void valleyview_crtc_enable(struct drm_crtc *crtc)
6111{
6112 struct drm_device *dev = crtc->dev;
a72e4c9f 6113 struct drm_i915_private *dev_priv = to_i915(dev);
89b667f8
JB
6114 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6115 struct intel_encoder *encoder;
b95c5321
ML
6116 struct intel_crtc_state *pipe_config =
6117 to_intel_crtc_state(crtc->state);
89b667f8 6118 int pipe = intel_crtc->pipe;
89b667f8 6119
53d9f4e9 6120 if (WARN_ON(intel_crtc->active))
89b667f8
JB
6121 return;
6122
37a5650b 6123 if (intel_crtc_has_dp_encoder(intel_crtc->config))
fe3cd48d 6124 intel_dp_set_m_n(intel_crtc, M1_N1);
5b18e57c
DV
6125
6126 intel_set_pipe_timings(intel_crtc);
bc58be60 6127 intel_set_pipe_src_size(intel_crtc);
5b18e57c 6128
c14b0485 6129 if (IS_CHERRYVIEW(dev) && pipe == PIPE_B) {
fac5e23e 6130 struct drm_i915_private *dev_priv = to_i915(dev);
c14b0485
VS
6131
6132 I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
6133 I915_WRITE(CHV_CANVAS(pipe), 0);
6134 }
6135
5b18e57c
DV
6136 i9xx_set_pipeconf(intel_crtc);
6137
89b667f8 6138 intel_crtc->active = true;
89b667f8 6139
a72e4c9f 6140 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4a3436e8 6141
89b667f8
JB
6142 for_each_encoder_on_crtc(dev, crtc, encoder)
6143 if (encoder->pre_pll_enable)
6144 encoder->pre_pll_enable(encoder);
6145
cd2d34d9
VS
6146 if (IS_CHERRYVIEW(dev)) {
6147 chv_prepare_pll(intel_crtc, intel_crtc->config);
6148 chv_enable_pll(intel_crtc, intel_crtc->config);
6149 } else {
6150 vlv_prepare_pll(intel_crtc, intel_crtc->config);
6151 vlv_enable_pll(intel_crtc, intel_crtc->config);
9d556c99 6152 }
89b667f8
JB
6153
6154 for_each_encoder_on_crtc(dev, crtc, encoder)
6155 if (encoder->pre_enable)
6156 encoder->pre_enable(encoder);
6157
2dd24552
JB
6158 i9xx_pfit_enable(intel_crtc);
6159
b95c5321 6160 intel_color_load_luts(&pipe_config->base);
63cbb074 6161
caed361d 6162 intel_update_watermarks(crtc);
e1fdc473 6163 intel_enable_pipe(intel_crtc);
be6a6f8e 6164
4b3a9526
VS
6165 assert_vblank_disabled(crtc);
6166 drm_crtc_vblank_on(crtc);
6167
f9b61ff6
DV
6168 for_each_encoder_on_crtc(dev, crtc, encoder)
6169 encoder->enable(encoder);
89b667f8
JB
6170}
6171
f13c2ef3
DV
6172static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
6173{
6174 struct drm_device *dev = crtc->base.dev;
fac5e23e 6175 struct drm_i915_private *dev_priv = to_i915(dev);
f13c2ef3 6176
6e3c9717
ACO
6177 I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
6178 I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
f13c2ef3
DV
6179}
6180
0b8765c6 6181static void i9xx_crtc_enable(struct drm_crtc *crtc)
79e53945
JB
6182{
6183 struct drm_device *dev = crtc->dev;
a72e4c9f 6184 struct drm_i915_private *dev_priv = to_i915(dev);
79e53945 6185 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 6186 struct intel_encoder *encoder;
b95c5321
ML
6187 struct intel_crtc_state *pipe_config =
6188 to_intel_crtc_state(crtc->state);
cd2d34d9 6189 enum pipe pipe = intel_crtc->pipe;
79e53945 6190
53d9f4e9 6191 if (WARN_ON(intel_crtc->active))
f7abfe8b
CW
6192 return;
6193
f13c2ef3
DV
6194 i9xx_set_pll_dividers(intel_crtc);
6195
37a5650b 6196 if (intel_crtc_has_dp_encoder(intel_crtc->config))
fe3cd48d 6197 intel_dp_set_m_n(intel_crtc, M1_N1);
5b18e57c
DV
6198
6199 intel_set_pipe_timings(intel_crtc);
bc58be60 6200 intel_set_pipe_src_size(intel_crtc);
5b18e57c 6201
5b18e57c
DV
6202 i9xx_set_pipeconf(intel_crtc);
6203
f7abfe8b 6204 intel_crtc->active = true;
6b383a7f 6205
4a3436e8 6206 if (!IS_GEN2(dev))
a72e4c9f 6207 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4a3436e8 6208
9d6d9f19
MK
6209 for_each_encoder_on_crtc(dev, crtc, encoder)
6210 if (encoder->pre_enable)
6211 encoder->pre_enable(encoder);
6212
f6736a1a
DV
6213 i9xx_enable_pll(intel_crtc);
6214
2dd24552
JB
6215 i9xx_pfit_enable(intel_crtc);
6216
b95c5321 6217 intel_color_load_luts(&pipe_config->base);
63cbb074 6218
f37fcc2a 6219 intel_update_watermarks(crtc);
e1fdc473 6220 intel_enable_pipe(intel_crtc);
be6a6f8e 6221
4b3a9526
VS
6222 assert_vblank_disabled(crtc);
6223 drm_crtc_vblank_on(crtc);
6224
f9b61ff6
DV
6225 for_each_encoder_on_crtc(dev, crtc, encoder)
6226 encoder->enable(encoder);
0b8765c6 6227}
79e53945 6228
87476d63
DV
6229static void i9xx_pfit_disable(struct intel_crtc *crtc)
6230{
6231 struct drm_device *dev = crtc->base.dev;
fac5e23e 6232 struct drm_i915_private *dev_priv = to_i915(dev);
87476d63 6233
6e3c9717 6234 if (!crtc->config->gmch_pfit.control)
328d8e82 6235 return;
87476d63 6236
328d8e82 6237 assert_pipe_disabled(dev_priv, crtc->pipe);
87476d63 6238
328d8e82
DV
6239 DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
6240 I915_READ(PFIT_CONTROL));
6241 I915_WRITE(PFIT_CONTROL, 0);
87476d63
DV
6242}
6243
0b8765c6
JB
6244static void i9xx_crtc_disable(struct drm_crtc *crtc)
6245{
6246 struct drm_device *dev = crtc->dev;
fac5e23e 6247 struct drm_i915_private *dev_priv = to_i915(dev);
0b8765c6 6248 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 6249 struct intel_encoder *encoder;
0b8765c6 6250 int pipe = intel_crtc->pipe;
ef9c3aee 6251
6304cd91
VS
6252 /*
6253 * On gen2 planes are double buffered but the pipe isn't, so we must
6254 * wait for planes to fully turn off before disabling the pipe.
6255 */
90e83e53
ACO
6256 if (IS_GEN2(dev))
6257 intel_wait_for_vblank(dev, pipe);
6304cd91 6258
4b3a9526
VS
6259 for_each_encoder_on_crtc(dev, crtc, encoder)
6260 encoder->disable(encoder);
6261
f9b61ff6
DV
6262 drm_crtc_vblank_off(crtc);
6263 assert_vblank_disabled(crtc);
6264
575f7ab7 6265 intel_disable_pipe(intel_crtc);
24a1f16d 6266
87476d63 6267 i9xx_pfit_disable(intel_crtc);
24a1f16d 6268
89b667f8
JB
6269 for_each_encoder_on_crtc(dev, crtc, encoder)
6270 if (encoder->post_disable)
6271 encoder->post_disable(encoder);
6272
d7edc4e5 6273 if (!intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_DSI)) {
076ed3b2
CML
6274 if (IS_CHERRYVIEW(dev))
6275 chv_disable_pll(dev_priv, pipe);
6276 else if (IS_VALLEYVIEW(dev))
6277 vlv_disable_pll(dev_priv, pipe);
6278 else
1c4e0274 6279 i9xx_disable_pll(intel_crtc);
076ed3b2 6280 }
0b8765c6 6281
d6db995f
VS
6282 for_each_encoder_on_crtc(dev, crtc, encoder)
6283 if (encoder->post_pll_disable)
6284 encoder->post_pll_disable(encoder);
6285
4a3436e8 6286 if (!IS_GEN2(dev))
a72e4c9f 6287 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
0b8765c6
JB
6288}
6289
b17d48e2
ML
6290static void intel_crtc_disable_noatomic(struct drm_crtc *crtc)
6291{
842e0307 6292 struct intel_encoder *encoder;
b17d48e2
ML
6293 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6294 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
6295 enum intel_display_power_domain domain;
6296 unsigned long domains;
6297
6298 if (!intel_crtc->active)
6299 return;
6300
a539205a 6301 if (to_intel_plane_state(crtc->primary->state)->visible) {
5a21b665 6302 WARN_ON(intel_crtc->flip_work);
fc32b1fd 6303
2622a081 6304 intel_pre_disable_primary_noatomic(crtc);
54a41961
ML
6305
6306 intel_crtc_disable_planes(crtc, 1 << drm_plane_index(crtc->primary));
6307 to_intel_plane_state(crtc->primary->state)->visible = false;
a539205a
ML
6308 }
6309
b17d48e2 6310 dev_priv->display.crtc_disable(crtc);
842e0307 6311
78108b7c
VS
6312 DRM_DEBUG_KMS("[CRTC:%d:%s] hw state adjusted, was enabled, now disabled\n",
6313 crtc->base.id, crtc->name);
842e0307
ML
6314
6315 WARN_ON(drm_atomic_set_mode_for_crtc(crtc->state, NULL) < 0);
6316 crtc->state->active = false;
37d9078b 6317 intel_crtc->active = false;
842e0307
ML
6318 crtc->enabled = false;
6319 crtc->state->connector_mask = 0;
6320 crtc->state->encoder_mask = 0;
6321
6322 for_each_encoder_on_crtc(crtc->dev, crtc, encoder)
6323 encoder->base.crtc = NULL;
6324
58f9c0bc 6325 intel_fbc_disable(intel_crtc);
37d9078b 6326 intel_update_watermarks(crtc);
1f7457b1 6327 intel_disable_shared_dpll(intel_crtc);
b17d48e2
ML
6328
6329 domains = intel_crtc->enabled_power_domains;
6330 for_each_power_domain(domain, domains)
6331 intel_display_power_put(dev_priv, domain);
6332 intel_crtc->enabled_power_domains = 0;
565602d7
ML
6333
6334 dev_priv->active_crtcs &= ~(1 << intel_crtc->pipe);
6335 dev_priv->min_pixclk[intel_crtc->pipe] = 0;
b17d48e2
ML
6336}
6337
6b72d486
ML
6338/*
6339 * turn all crtc's off, but do not adjust state
6340 * This has to be paired with a call to intel_modeset_setup_hw_state.
6341 */
70e0bd74 6342int intel_display_suspend(struct drm_device *dev)
ee7b9f93 6343{
e2c8b870 6344 struct drm_i915_private *dev_priv = to_i915(dev);
70e0bd74 6345 struct drm_atomic_state *state;
e2c8b870 6346 int ret;
70e0bd74 6347
e2c8b870
ML
6348 state = drm_atomic_helper_suspend(dev);
6349 ret = PTR_ERR_OR_ZERO(state);
70e0bd74
ML
6350 if (ret)
6351 DRM_ERROR("Suspending crtc's failed with %i\n", ret);
e2c8b870
ML
6352 else
6353 dev_priv->modeset_restore_state = state;
70e0bd74 6354 return ret;
ee7b9f93
JB
6355}
6356
ea5b213a 6357void intel_encoder_destroy(struct drm_encoder *encoder)
7e7d76c3 6358{
4ef69c7a 6359 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
ea5b213a 6360
ea5b213a
CW
6361 drm_encoder_cleanup(encoder);
6362 kfree(intel_encoder);
7e7d76c3
JB
6363}
6364
0a91ca29
DV
6365/* Cross check the actual hw state with our own modeset state tracking (and it's
6366 * internal consistency). */
5a21b665 6367static void intel_connector_verify_state(struct intel_connector *connector)
79e53945 6368{
5a21b665 6369 struct drm_crtc *crtc = connector->base.state->crtc;
35dd3c64
ML
6370
6371 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
6372 connector->base.base.id,
6373 connector->base.name);
6374
0a91ca29 6375 if (connector->get_hw_state(connector)) {
e85376cb 6376 struct intel_encoder *encoder = connector->encoder;
5a21b665 6377 struct drm_connector_state *conn_state = connector->base.state;
0a91ca29 6378
35dd3c64
ML
6379 I915_STATE_WARN(!crtc,
6380 "connector enabled without attached crtc\n");
0a91ca29 6381
35dd3c64
ML
6382 if (!crtc)
6383 return;
6384
6385 I915_STATE_WARN(!crtc->state->active,
6386 "connector is active, but attached crtc isn't\n");
6387
e85376cb 6388 if (!encoder || encoder->type == INTEL_OUTPUT_DP_MST)
35dd3c64
ML
6389 return;
6390
e85376cb 6391 I915_STATE_WARN(conn_state->best_encoder != &encoder->base,
35dd3c64
ML
6392 "atomic encoder doesn't match attached encoder\n");
6393
e85376cb 6394 I915_STATE_WARN(conn_state->crtc != encoder->base.crtc,
35dd3c64
ML
6395 "attached encoder crtc differs from connector crtc\n");
6396 } else {
4d688a2a
ML
6397 I915_STATE_WARN(crtc && crtc->state->active,
6398 "attached crtc is active, but connector isn't\n");
5a21b665 6399 I915_STATE_WARN(!crtc && connector->base.state->best_encoder,
35dd3c64 6400 "best encoder set without crtc!\n");
0a91ca29 6401 }
79e53945
JB
6402}
6403
08d9bc92
ACO
6404int intel_connector_init(struct intel_connector *connector)
6405{
5350a031 6406 drm_atomic_helper_connector_reset(&connector->base);
08d9bc92 6407
5350a031 6408 if (!connector->base.state)
08d9bc92
ACO
6409 return -ENOMEM;
6410
08d9bc92
ACO
6411 return 0;
6412}
6413
6414struct intel_connector *intel_connector_alloc(void)
6415{
6416 struct intel_connector *connector;
6417
6418 connector = kzalloc(sizeof *connector, GFP_KERNEL);
6419 if (!connector)
6420 return NULL;
6421
6422 if (intel_connector_init(connector) < 0) {
6423 kfree(connector);
6424 return NULL;
6425 }
6426
6427 return connector;
6428}
6429
f0947c37
DV
6430/* Simple connector->get_hw_state implementation for encoders that support only
6431 * one connector and no cloning and hence the encoder state determines the state
6432 * of the connector. */
6433bool intel_connector_get_hw_state(struct intel_connector *connector)
ea5b213a 6434{
24929352 6435 enum pipe pipe = 0;
f0947c37 6436 struct intel_encoder *encoder = connector->encoder;
ea5b213a 6437
f0947c37 6438 return encoder->get_hw_state(encoder, &pipe);
ea5b213a
CW
6439}
6440
6d293983 6441static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
d272ddfa 6442{
6d293983
ACO
6443 if (crtc_state->base.enable && crtc_state->has_pch_encoder)
6444 return crtc_state->fdi_lanes;
d272ddfa
VS
6445
6446 return 0;
6447}
6448
6d293983 6449static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
5cec258b 6450 struct intel_crtc_state *pipe_config)
1857e1da 6451{
6d293983
ACO
6452 struct drm_atomic_state *state = pipe_config->base.state;
6453 struct intel_crtc *other_crtc;
6454 struct intel_crtc_state *other_crtc_state;
6455
1857e1da
DV
6456 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
6457 pipe_name(pipe), pipe_config->fdi_lanes);
6458 if (pipe_config->fdi_lanes > 4) {
6459 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
6460 pipe_name(pipe), pipe_config->fdi_lanes);
6d293983 6461 return -EINVAL;
1857e1da
DV
6462 }
6463
bafb6553 6464 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
1857e1da
DV
6465 if (pipe_config->fdi_lanes > 2) {
6466 DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
6467 pipe_config->fdi_lanes);
6d293983 6468 return -EINVAL;
1857e1da 6469 } else {
6d293983 6470 return 0;
1857e1da
DV
6471 }
6472 }
6473
6474 if (INTEL_INFO(dev)->num_pipes == 2)
6d293983 6475 return 0;
1857e1da
DV
6476
6477 /* Ivybridge 3 pipe is really complicated */
6478 switch (pipe) {
6479 case PIPE_A:
6d293983 6480 return 0;
1857e1da 6481 case PIPE_B:
6d293983
ACO
6482 if (pipe_config->fdi_lanes <= 2)
6483 return 0;
6484
6485 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_C));
6486 other_crtc_state =
6487 intel_atomic_get_crtc_state(state, other_crtc);
6488 if (IS_ERR(other_crtc_state))
6489 return PTR_ERR(other_crtc_state);
6490
6491 if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
1857e1da
DV
6492 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
6493 pipe_name(pipe), pipe_config->fdi_lanes);
6d293983 6494 return -EINVAL;
1857e1da 6495 }
6d293983 6496 return 0;
1857e1da 6497 case PIPE_C:
251cc67c
VS
6498 if (pipe_config->fdi_lanes > 2) {
6499 DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
6500 pipe_name(pipe), pipe_config->fdi_lanes);
6d293983 6501 return -EINVAL;
251cc67c 6502 }
6d293983
ACO
6503
6504 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_B));
6505 other_crtc_state =
6506 intel_atomic_get_crtc_state(state, other_crtc);
6507 if (IS_ERR(other_crtc_state))
6508 return PTR_ERR(other_crtc_state);
6509
6510 if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
1857e1da 6511 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
6d293983 6512 return -EINVAL;
1857e1da 6513 }
6d293983 6514 return 0;
1857e1da
DV
6515 default:
6516 BUG();
6517 }
6518}
6519
e29c22c0
DV
6520#define RETRY 1
6521static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
5cec258b 6522 struct intel_crtc_state *pipe_config)
877d48d5 6523{
1857e1da 6524 struct drm_device *dev = intel_crtc->base.dev;
7c5f93b0 6525 const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
6d293983
ACO
6526 int lane, link_bw, fdi_dotclock, ret;
6527 bool needs_recompute = false;
877d48d5 6528
e29c22c0 6529retry:
877d48d5
DV
6530 /* FDI is a binary signal running at ~2.7GHz, encoding
6531 * each output octet as 10 bits. The actual frequency
6532 * is stored as a divider into a 100MHz clock, and the
6533 * mode pixel clock is stored in units of 1KHz.
6534 * Hence the bw of each lane in terms of the mode signal
6535 * is:
6536 */
21a727b3 6537 link_bw = intel_fdi_link_freq(to_i915(dev), pipe_config);
877d48d5 6538
241bfc38 6539 fdi_dotclock = adjusted_mode->crtc_clock;
877d48d5 6540
2bd89a07 6541 lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
877d48d5
DV
6542 pipe_config->pipe_bpp);
6543
6544 pipe_config->fdi_lanes = lane;
6545
2bd89a07 6546 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
877d48d5 6547 link_bw, &pipe_config->fdi_m_n);
1857e1da 6548
e3b247da 6549 ret = ironlake_check_fdi_lanes(dev, intel_crtc->pipe, pipe_config);
6d293983 6550 if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
e29c22c0
DV
6551 pipe_config->pipe_bpp -= 2*3;
6552 DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
6553 pipe_config->pipe_bpp);
6554 needs_recompute = true;
6555 pipe_config->bw_constrained = true;
6556
6557 goto retry;
6558 }
6559
6560 if (needs_recompute)
6561 return RETRY;
6562
6d293983 6563 return ret;
877d48d5
DV
6564}
6565
8cfb3407
VS
6566static bool pipe_config_supports_ips(struct drm_i915_private *dev_priv,
6567 struct intel_crtc_state *pipe_config)
6568{
6569 if (pipe_config->pipe_bpp > 24)
6570 return false;
6571
6572 /* HSW can handle pixel rate up to cdclk? */
2d1fe073 6573 if (IS_HASWELL(dev_priv))
8cfb3407
VS
6574 return true;
6575
6576 /*
b432e5cf
VS
6577 * We compare against max which means we must take
6578 * the increased cdclk requirement into account when
6579 * calculating the new cdclk.
6580 *
6581 * Should measure whether using a lower cdclk w/o IPS
8cfb3407
VS
6582 */
6583 return ilk_pipe_pixel_rate(pipe_config) <=
6584 dev_priv->max_cdclk_freq * 95 / 100;
6585}
6586
42db64ef 6587static void hsw_compute_ips_config(struct intel_crtc *crtc,
5cec258b 6588 struct intel_crtc_state *pipe_config)
42db64ef 6589{
8cfb3407 6590 struct drm_device *dev = crtc->base.dev;
fac5e23e 6591 struct drm_i915_private *dev_priv = to_i915(dev);
8cfb3407 6592
d330a953 6593 pipe_config->ips_enabled = i915.enable_ips &&
8cfb3407
VS
6594 hsw_crtc_supports_ips(crtc) &&
6595 pipe_config_supports_ips(dev_priv, pipe_config);
42db64ef
PZ
6596}
6597
39acb4aa
VS
6598static bool intel_crtc_supports_double_wide(const struct intel_crtc *crtc)
6599{
6600 const struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6601
6602 /* GDG double wide on either pipe, otherwise pipe A only */
6603 return INTEL_INFO(dev_priv)->gen < 4 &&
6604 (crtc->pipe == PIPE_A || IS_I915G(dev_priv));
6605}
6606
a43f6e0f 6607static int intel_crtc_compute_config(struct intel_crtc *crtc,
5cec258b 6608 struct intel_crtc_state *pipe_config)
79e53945 6609{
a43f6e0f 6610 struct drm_device *dev = crtc->base.dev;
fac5e23e 6611 struct drm_i915_private *dev_priv = to_i915(dev);
7c5f93b0 6612 const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
f3261156 6613 int clock_limit = dev_priv->max_dotclk_freq;
89749350 6614
cf532bb2 6615 if (INTEL_INFO(dev)->gen < 4) {
f3261156 6616 clock_limit = dev_priv->max_cdclk_freq * 9 / 10;
cf532bb2
VS
6617
6618 /*
39acb4aa 6619 * Enable double wide mode when the dot clock
cf532bb2 6620 * is > 90% of the (display) core speed.
cf532bb2 6621 */
39acb4aa
VS
6622 if (intel_crtc_supports_double_wide(crtc) &&
6623 adjusted_mode->crtc_clock > clock_limit) {
f3261156 6624 clock_limit = dev_priv->max_dotclk_freq;
cf532bb2 6625 pipe_config->double_wide = true;
ad3a4479 6626 }
f3261156 6627 }
ad3a4479 6628
f3261156
VS
6629 if (adjusted_mode->crtc_clock > clock_limit) {
6630 DRM_DEBUG_KMS("requested pixel clock (%d kHz) too high (max: %d kHz, double wide: %s)\n",
6631 adjusted_mode->crtc_clock, clock_limit,
6632 yesno(pipe_config->double_wide));
6633 return -EINVAL;
2c07245f 6634 }
89749350 6635
1d1d0e27
VS
6636 /*
6637 * Pipe horizontal size must be even in:
6638 * - DVO ganged mode
6639 * - LVDS dual channel mode
6640 * - Double wide pipe
6641 */
2d84d2b3 6642 if ((intel_crtc_has_type(pipe_config, INTEL_OUTPUT_LVDS) &&
1d1d0e27
VS
6643 intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
6644 pipe_config->pipe_src_w &= ~1;
6645
8693a824
DL
6646 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
6647 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
44f46b42
CW
6648 */
6649 if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
aad941d5 6650 adjusted_mode->crtc_hsync_start == adjusted_mode->crtc_hdisplay)
e29c22c0 6651 return -EINVAL;
44f46b42 6652
f5adf94e 6653 if (HAS_IPS(dev))
a43f6e0f
DV
6654 hsw_compute_ips_config(crtc, pipe_config);
6655
877d48d5 6656 if (pipe_config->has_pch_encoder)
a43f6e0f 6657 return ironlake_fdi_compute_config(crtc, pipe_config);
877d48d5 6658
cf5a15be 6659 return 0;
79e53945
JB
6660}
6661
1652d19e
VS
6662static int skylake_get_display_clock_speed(struct drm_device *dev)
6663{
6664 struct drm_i915_private *dev_priv = to_i915(dev);
ea61791e 6665 uint32_t cdctl;
1652d19e 6666
ea61791e 6667 skl_dpll0_update(dev_priv);
1652d19e 6668
63911d72 6669 if (dev_priv->cdclk_pll.vco == 0)
709e05c3 6670 return dev_priv->cdclk_pll.ref;
1652d19e 6671
ea61791e 6672 cdctl = I915_READ(CDCLK_CTL);
1652d19e 6673
63911d72 6674 if (dev_priv->cdclk_pll.vco == 8640000) {
1652d19e
VS
6675 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6676 case CDCLK_FREQ_450_432:
6677 return 432000;
6678 case CDCLK_FREQ_337_308:
487ed2e4 6679 return 308571;
ea61791e
VS
6680 case CDCLK_FREQ_540:
6681 return 540000;
1652d19e 6682 case CDCLK_FREQ_675_617:
487ed2e4 6683 return 617143;
1652d19e 6684 default:
ea61791e 6685 MISSING_CASE(cdctl & CDCLK_FREQ_SEL_MASK);
1652d19e
VS
6686 }
6687 } else {
1652d19e
VS
6688 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6689 case CDCLK_FREQ_450_432:
6690 return 450000;
6691 case CDCLK_FREQ_337_308:
6692 return 337500;
ea61791e
VS
6693 case CDCLK_FREQ_540:
6694 return 540000;
1652d19e
VS
6695 case CDCLK_FREQ_675_617:
6696 return 675000;
6697 default:
ea61791e 6698 MISSING_CASE(cdctl & CDCLK_FREQ_SEL_MASK);
1652d19e
VS
6699 }
6700 }
6701
709e05c3 6702 return dev_priv->cdclk_pll.ref;
1652d19e
VS
6703}
6704
83d7c81f
VS
6705static void bxt_de_pll_update(struct drm_i915_private *dev_priv)
6706{
6707 u32 val;
6708
6709 dev_priv->cdclk_pll.ref = 19200;
1c3f7700 6710 dev_priv->cdclk_pll.vco = 0;
83d7c81f
VS
6711
6712 val = I915_READ(BXT_DE_PLL_ENABLE);
1c3f7700 6713 if ((val & BXT_DE_PLL_PLL_ENABLE) == 0)
83d7c81f 6714 return;
83d7c81f 6715
1c3f7700
ID
6716 if (WARN_ON((val & BXT_DE_PLL_LOCK) == 0))
6717 return;
83d7c81f
VS
6718
6719 val = I915_READ(BXT_DE_PLL_CTL);
6720 dev_priv->cdclk_pll.vco = (val & BXT_DE_PLL_RATIO_MASK) *
6721 dev_priv->cdclk_pll.ref;
6722}
6723
acd3f3d3
BP
6724static int broxton_get_display_clock_speed(struct drm_device *dev)
6725{
6726 struct drm_i915_private *dev_priv = to_i915(dev);
f5986242
VS
6727 u32 divider;
6728 int div, vco;
acd3f3d3 6729
83d7c81f
VS
6730 bxt_de_pll_update(dev_priv);
6731
f5986242
VS
6732 vco = dev_priv->cdclk_pll.vco;
6733 if (vco == 0)
6734 return dev_priv->cdclk_pll.ref;
acd3f3d3 6735
f5986242 6736 divider = I915_READ(CDCLK_CTL) & BXT_CDCLK_CD2X_DIV_SEL_MASK;
acd3f3d3 6737
f5986242 6738 switch (divider) {
acd3f3d3 6739 case BXT_CDCLK_CD2X_DIV_SEL_1:
f5986242
VS
6740 div = 2;
6741 break;
acd3f3d3 6742 case BXT_CDCLK_CD2X_DIV_SEL_1_5:
f5986242
VS
6743 div = 3;
6744 break;
acd3f3d3 6745 case BXT_CDCLK_CD2X_DIV_SEL_2:
f5986242
VS
6746 div = 4;
6747 break;
acd3f3d3 6748 case BXT_CDCLK_CD2X_DIV_SEL_4:
f5986242
VS
6749 div = 8;
6750 break;
6751 default:
6752 MISSING_CASE(divider);
6753 return dev_priv->cdclk_pll.ref;
acd3f3d3
BP
6754 }
6755
f5986242 6756 return DIV_ROUND_CLOSEST(vco, div);
acd3f3d3
BP
6757}
6758
1652d19e
VS
6759static int broadwell_get_display_clock_speed(struct drm_device *dev)
6760{
fac5e23e 6761 struct drm_i915_private *dev_priv = to_i915(dev);
1652d19e
VS
6762 uint32_t lcpll = I915_READ(LCPLL_CTL);
6763 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6764
6765 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6766 return 800000;
6767 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6768 return 450000;
6769 else if (freq == LCPLL_CLK_FREQ_450)
6770 return 450000;
6771 else if (freq == LCPLL_CLK_FREQ_54O_BDW)
6772 return 540000;
6773 else if (freq == LCPLL_CLK_FREQ_337_5_BDW)
6774 return 337500;
6775 else
6776 return 675000;
6777}
6778
6779static int haswell_get_display_clock_speed(struct drm_device *dev)
6780{
fac5e23e 6781 struct drm_i915_private *dev_priv = to_i915(dev);
1652d19e
VS
6782 uint32_t lcpll = I915_READ(LCPLL_CTL);
6783 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6784
6785 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6786 return 800000;
6787 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6788 return 450000;
6789 else if (freq == LCPLL_CLK_FREQ_450)
6790 return 450000;
6791 else if (IS_HSW_ULT(dev))
6792 return 337500;
6793 else
6794 return 540000;
79e53945
JB
6795}
6796
25eb05fc
JB
6797static int valleyview_get_display_clock_speed(struct drm_device *dev)
6798{
bfa7df01
VS
6799 return vlv_get_cck_clock_hpll(to_i915(dev), "cdclk",
6800 CCK_DISPLAY_CLOCK_CONTROL);
25eb05fc
JB
6801}
6802
b37a6434
VS
6803static int ilk_get_display_clock_speed(struct drm_device *dev)
6804{
6805 return 450000;
6806}
6807
e70236a8
JB
6808static int i945_get_display_clock_speed(struct drm_device *dev)
6809{
6810 return 400000;
6811}
79e53945 6812
e70236a8 6813static int i915_get_display_clock_speed(struct drm_device *dev)
79e53945 6814{
e907f170 6815 return 333333;
e70236a8 6816}
79e53945 6817
e70236a8
JB
6818static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
6819{
6820 return 200000;
6821}
79e53945 6822
257a7ffc
DV
6823static int pnv_get_display_clock_speed(struct drm_device *dev)
6824{
6825 u16 gcfgc = 0;
6826
6827 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6828
6829 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6830 case GC_DISPLAY_CLOCK_267_MHZ_PNV:
e907f170 6831 return 266667;
257a7ffc 6832 case GC_DISPLAY_CLOCK_333_MHZ_PNV:
e907f170 6833 return 333333;
257a7ffc 6834 case GC_DISPLAY_CLOCK_444_MHZ_PNV:
e907f170 6835 return 444444;
257a7ffc
DV
6836 case GC_DISPLAY_CLOCK_200_MHZ_PNV:
6837 return 200000;
6838 default:
6839 DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
6840 case GC_DISPLAY_CLOCK_133_MHZ_PNV:
e907f170 6841 return 133333;
257a7ffc 6842 case GC_DISPLAY_CLOCK_167_MHZ_PNV:
e907f170 6843 return 166667;
257a7ffc
DV
6844 }
6845}
6846
e70236a8
JB
6847static int i915gm_get_display_clock_speed(struct drm_device *dev)
6848{
6849 u16 gcfgc = 0;
79e53945 6850
e70236a8
JB
6851 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6852
6853 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
e907f170 6854 return 133333;
e70236a8
JB
6855 else {
6856 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6857 case GC_DISPLAY_CLOCK_333_MHZ:
e907f170 6858 return 333333;
e70236a8
JB
6859 default:
6860 case GC_DISPLAY_CLOCK_190_200_MHZ:
6861 return 190000;
79e53945 6862 }
e70236a8
JB
6863 }
6864}
6865
6866static int i865_get_display_clock_speed(struct drm_device *dev)
6867{
e907f170 6868 return 266667;
e70236a8
JB
6869}
6870
1b1d2716 6871static int i85x_get_display_clock_speed(struct drm_device *dev)
e70236a8
JB
6872{
6873 u16 hpllcc = 0;
1b1d2716 6874
65cd2b3f
VS
6875 /*
6876 * 852GM/852GMV only supports 133 MHz and the HPLLCC
6877 * encoding is different :(
6878 * FIXME is this the right way to detect 852GM/852GMV?
6879 */
6880 if (dev->pdev->revision == 0x1)
6881 return 133333;
6882
1b1d2716
VS
6883 pci_bus_read_config_word(dev->pdev->bus,
6884 PCI_DEVFN(0, 3), HPLLCC, &hpllcc);
6885
e70236a8
JB
6886 /* Assume that the hardware is in the high speed state. This
6887 * should be the default.
6888 */
6889 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
6890 case GC_CLOCK_133_200:
1b1d2716 6891 case GC_CLOCK_133_200_2:
e70236a8
JB
6892 case GC_CLOCK_100_200:
6893 return 200000;
6894 case GC_CLOCK_166_250:
6895 return 250000;
6896 case GC_CLOCK_100_133:
e907f170 6897 return 133333;
1b1d2716
VS
6898 case GC_CLOCK_133_266:
6899 case GC_CLOCK_133_266_2:
6900 case GC_CLOCK_166_266:
6901 return 266667;
e70236a8 6902 }
79e53945 6903
e70236a8
JB
6904 /* Shouldn't happen */
6905 return 0;
6906}
79e53945 6907
e70236a8
JB
6908static int i830_get_display_clock_speed(struct drm_device *dev)
6909{
e907f170 6910 return 133333;
79e53945
JB
6911}
6912
34edce2f
VS
6913static unsigned int intel_hpll_vco(struct drm_device *dev)
6914{
fac5e23e 6915 struct drm_i915_private *dev_priv = to_i915(dev);
34edce2f
VS
6916 static const unsigned int blb_vco[8] = {
6917 [0] = 3200000,
6918 [1] = 4000000,
6919 [2] = 5333333,
6920 [3] = 4800000,
6921 [4] = 6400000,
6922 };
6923 static const unsigned int pnv_vco[8] = {
6924 [0] = 3200000,
6925 [1] = 4000000,
6926 [2] = 5333333,
6927 [3] = 4800000,
6928 [4] = 2666667,
6929 };
6930 static const unsigned int cl_vco[8] = {
6931 [0] = 3200000,
6932 [1] = 4000000,
6933 [2] = 5333333,
6934 [3] = 6400000,
6935 [4] = 3333333,
6936 [5] = 3566667,
6937 [6] = 4266667,
6938 };
6939 static const unsigned int elk_vco[8] = {
6940 [0] = 3200000,
6941 [1] = 4000000,
6942 [2] = 5333333,
6943 [3] = 4800000,
6944 };
6945 static const unsigned int ctg_vco[8] = {
6946 [0] = 3200000,
6947 [1] = 4000000,
6948 [2] = 5333333,
6949 [3] = 6400000,
6950 [4] = 2666667,
6951 [5] = 4266667,
6952 };
6953 const unsigned int *vco_table;
6954 unsigned int vco;
6955 uint8_t tmp = 0;
6956
6957 /* FIXME other chipsets? */
6958 if (IS_GM45(dev))
6959 vco_table = ctg_vco;
6960 else if (IS_G4X(dev))
6961 vco_table = elk_vco;
6962 else if (IS_CRESTLINE(dev))
6963 vco_table = cl_vco;
6964 else if (IS_PINEVIEW(dev))
6965 vco_table = pnv_vco;
6966 else if (IS_G33(dev))
6967 vco_table = blb_vco;
6968 else
6969 return 0;
6970
6971 tmp = I915_READ(IS_MOBILE(dev) ? HPLLVCO_MOBILE : HPLLVCO);
6972
6973 vco = vco_table[tmp & 0x7];
6974 if (vco == 0)
6975 DRM_ERROR("Bad HPLL VCO (HPLLVCO=0x%02x)\n", tmp);
6976 else
6977 DRM_DEBUG_KMS("HPLL VCO %u kHz\n", vco);
6978
6979 return vco;
6980}
6981
6982static int gm45_get_display_clock_speed(struct drm_device *dev)
6983{
6984 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
6985 uint16_t tmp = 0;
6986
6987 pci_read_config_word(dev->pdev, GCFGC, &tmp);
6988
6989 cdclk_sel = (tmp >> 12) & 0x1;
6990
6991 switch (vco) {
6992 case 2666667:
6993 case 4000000:
6994 case 5333333:
6995 return cdclk_sel ? 333333 : 222222;
6996 case 3200000:
6997 return cdclk_sel ? 320000 : 228571;
6998 default:
6999 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u, CFGC=0x%04x\n", vco, tmp);
7000 return 222222;
7001 }
7002}
7003
7004static int i965gm_get_display_clock_speed(struct drm_device *dev)
7005{
7006 static const uint8_t div_3200[] = { 16, 10, 8 };
7007 static const uint8_t div_4000[] = { 20, 12, 10 };
7008 static const uint8_t div_5333[] = { 24, 16, 14 };
7009 const uint8_t *div_table;
7010 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
7011 uint16_t tmp = 0;
7012
7013 pci_read_config_word(dev->pdev, GCFGC, &tmp);
7014
7015 cdclk_sel = ((tmp >> 8) & 0x1f) - 1;
7016
7017 if (cdclk_sel >= ARRAY_SIZE(div_3200))
7018 goto fail;
7019
7020 switch (vco) {
7021 case 3200000:
7022 div_table = div_3200;
7023 break;
7024 case 4000000:
7025 div_table = div_4000;
7026 break;
7027 case 5333333:
7028 div_table = div_5333;
7029 break;
7030 default:
7031 goto fail;
7032 }
7033
7034 return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
7035
caf4e252 7036fail:
34edce2f
VS
7037 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%04x\n", vco, tmp);
7038 return 200000;
7039}
7040
7041static int g33_get_display_clock_speed(struct drm_device *dev)
7042{
7043 static const uint8_t div_3200[] = { 12, 10, 8, 7, 5, 16 };
7044 static const uint8_t div_4000[] = { 14, 12, 10, 8, 6, 20 };
7045 static const uint8_t div_4800[] = { 20, 14, 12, 10, 8, 24 };
7046 static const uint8_t div_5333[] = { 20, 16, 12, 12, 8, 28 };
7047 const uint8_t *div_table;
7048 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
7049 uint16_t tmp = 0;
7050
7051 pci_read_config_word(dev->pdev, GCFGC, &tmp);
7052
7053 cdclk_sel = (tmp >> 4) & 0x7;
7054
7055 if (cdclk_sel >= ARRAY_SIZE(div_3200))
7056 goto fail;
7057
7058 switch (vco) {
7059 case 3200000:
7060 div_table = div_3200;
7061 break;
7062 case 4000000:
7063 div_table = div_4000;
7064 break;
7065 case 4800000:
7066 div_table = div_4800;
7067 break;
7068 case 5333333:
7069 div_table = div_5333;
7070 break;
7071 default:
7072 goto fail;
7073 }
7074
7075 return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
7076
caf4e252 7077fail:
34edce2f
VS
7078 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%08x\n", vco, tmp);
7079 return 190476;
7080}
7081
2c07245f 7082static void
a65851af 7083intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
2c07245f 7084{
a65851af
VS
7085 while (*num > DATA_LINK_M_N_MASK ||
7086 *den > DATA_LINK_M_N_MASK) {
2c07245f
ZW
7087 *num >>= 1;
7088 *den >>= 1;
7089 }
7090}
7091
a65851af
VS
7092static void compute_m_n(unsigned int m, unsigned int n,
7093 uint32_t *ret_m, uint32_t *ret_n)
7094{
7095 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
7096 *ret_m = div_u64((uint64_t) m * *ret_n, n);
7097 intel_reduce_m_n_ratio(ret_m, ret_n);
7098}
7099
e69d0bc1
DV
7100void
7101intel_link_compute_m_n(int bits_per_pixel, int nlanes,
7102 int pixel_clock, int link_clock,
7103 struct intel_link_m_n *m_n)
2c07245f 7104{
e69d0bc1 7105 m_n->tu = 64;
a65851af
VS
7106
7107 compute_m_n(bits_per_pixel * pixel_clock,
7108 link_clock * nlanes * 8,
7109 &m_n->gmch_m, &m_n->gmch_n);
7110
7111 compute_m_n(pixel_clock, link_clock,
7112 &m_n->link_m, &m_n->link_n);
2c07245f
ZW
7113}
7114
a7615030
CW
7115static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
7116{
d330a953
JN
7117 if (i915.panel_use_ssc >= 0)
7118 return i915.panel_use_ssc != 0;
41aa3448 7119 return dev_priv->vbt.lvds_use_ssc
435793df 7120 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
a7615030
CW
7121}
7122
7429e9d4 7123static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
c65d77d8 7124{
7df00d7a 7125 return (1 << dpll->n) << 16 | dpll->m2;
7429e9d4 7126}
f47709a9 7127
7429e9d4
DV
7128static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
7129{
7130 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
c65d77d8
JB
7131}
7132
f47709a9 7133static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
190f68c5 7134 struct intel_crtc_state *crtc_state,
9e2c8475 7135 struct dpll *reduced_clock)
a7516a05 7136{
f47709a9 7137 struct drm_device *dev = crtc->base.dev;
a7516a05
JB
7138 u32 fp, fp2 = 0;
7139
7140 if (IS_PINEVIEW(dev)) {
190f68c5 7141 fp = pnv_dpll_compute_fp(&crtc_state->dpll);
a7516a05 7142 if (reduced_clock)
7429e9d4 7143 fp2 = pnv_dpll_compute_fp(reduced_clock);
a7516a05 7144 } else {
190f68c5 7145 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
a7516a05 7146 if (reduced_clock)
7429e9d4 7147 fp2 = i9xx_dpll_compute_fp(reduced_clock);
a7516a05
JB
7148 }
7149
190f68c5 7150 crtc_state->dpll_hw_state.fp0 = fp;
a7516a05 7151
f47709a9 7152 crtc->lowfreq_avail = false;
2d84d2b3 7153 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
ab585dea 7154 reduced_clock) {
190f68c5 7155 crtc_state->dpll_hw_state.fp1 = fp2;
f47709a9 7156 crtc->lowfreq_avail = true;
a7516a05 7157 } else {
190f68c5 7158 crtc_state->dpll_hw_state.fp1 = fp;
a7516a05
JB
7159 }
7160}
7161
5e69f97f
CML
7162static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
7163 pipe)
89b667f8
JB
7164{
7165 u32 reg_val;
7166
7167 /*
7168 * PLLB opamp always calibrates to max value of 0x3f, force enable it
7169 * and set it to a reasonable value instead.
7170 */
ab3c759a 7171 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
89b667f8
JB
7172 reg_val &= 0xffffff00;
7173 reg_val |= 0x00000030;
ab3c759a 7174 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
89b667f8 7175
ab3c759a 7176 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
89b667f8
JB
7177 reg_val &= 0x8cffffff;
7178 reg_val = 0x8c000000;
ab3c759a 7179 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
89b667f8 7180
ab3c759a 7181 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
89b667f8 7182 reg_val &= 0xffffff00;
ab3c759a 7183 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
89b667f8 7184
ab3c759a 7185 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
89b667f8
JB
7186 reg_val &= 0x00ffffff;
7187 reg_val |= 0xb0000000;
ab3c759a 7188 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
89b667f8
JB
7189}
7190
b551842d
DV
7191static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
7192 struct intel_link_m_n *m_n)
7193{
7194 struct drm_device *dev = crtc->base.dev;
fac5e23e 7195 struct drm_i915_private *dev_priv = to_i915(dev);
b551842d
DV
7196 int pipe = crtc->pipe;
7197
e3b95f1e
DV
7198 I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7199 I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
7200 I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
7201 I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
b551842d
DV
7202}
7203
7204static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
f769cd24
VK
7205 struct intel_link_m_n *m_n,
7206 struct intel_link_m_n *m2_n2)
b551842d
DV
7207{
7208 struct drm_device *dev = crtc->base.dev;
fac5e23e 7209 struct drm_i915_private *dev_priv = to_i915(dev);
b551842d 7210 int pipe = crtc->pipe;
6e3c9717 7211 enum transcoder transcoder = crtc->config->cpu_transcoder;
b551842d
DV
7212
7213 if (INTEL_INFO(dev)->gen >= 5) {
7214 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
7215 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
7216 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
7217 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
f769cd24
VK
7218 /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
7219 * for gen < 8) and if DRRS is supported (to make sure the
7220 * registers are not unnecessarily accessed).
7221 */
44395bfe 7222 if (m2_n2 && (IS_CHERRYVIEW(dev) || INTEL_INFO(dev)->gen < 8) &&
6e3c9717 7223 crtc->config->has_drrs) {
f769cd24
VK
7224 I915_WRITE(PIPE_DATA_M2(transcoder),
7225 TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
7226 I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
7227 I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
7228 I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
7229 }
b551842d 7230 } else {
e3b95f1e
DV
7231 I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7232 I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
7233 I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
7234 I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
b551842d
DV
7235 }
7236}
7237
fe3cd48d 7238void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
03afc4a2 7239{
fe3cd48d
R
7240 struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
7241
7242 if (m_n == M1_N1) {
7243 dp_m_n = &crtc->config->dp_m_n;
7244 dp_m2_n2 = &crtc->config->dp_m2_n2;
7245 } else if (m_n == M2_N2) {
7246
7247 /*
7248 * M2_N2 registers are not supported. Hence m2_n2 divider value
7249 * needs to be programmed into M1_N1.
7250 */
7251 dp_m_n = &crtc->config->dp_m2_n2;
7252 } else {
7253 DRM_ERROR("Unsupported divider value\n");
7254 return;
7255 }
7256
6e3c9717
ACO
7257 if (crtc->config->has_pch_encoder)
7258 intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
03afc4a2 7259 else
fe3cd48d 7260 intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
03afc4a2
DV
7261}
7262
251ac862
DV
7263static void vlv_compute_dpll(struct intel_crtc *crtc,
7264 struct intel_crtc_state *pipe_config)
bdd4b6a6 7265{
03ed5cbf 7266 pipe_config->dpll_hw_state.dpll = DPLL_INTEGRATED_REF_CLK_VLV |
cd2d34d9 7267 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
03ed5cbf
VS
7268 if (crtc->pipe != PIPE_A)
7269 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
bdd4b6a6 7270
cd2d34d9 7271 /* DPLL not used with DSI, but still need the rest set up */
d7edc4e5 7272 if (!intel_crtc_has_type(pipe_config, INTEL_OUTPUT_DSI))
cd2d34d9
VS
7273 pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE |
7274 DPLL_EXT_BUFFER_ENABLE_VLV;
7275
03ed5cbf
VS
7276 pipe_config->dpll_hw_state.dpll_md =
7277 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
7278}
bdd4b6a6 7279
03ed5cbf
VS
7280static void chv_compute_dpll(struct intel_crtc *crtc,
7281 struct intel_crtc_state *pipe_config)
7282{
7283 pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLK_CHV |
cd2d34d9 7284 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
03ed5cbf
VS
7285 if (crtc->pipe != PIPE_A)
7286 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
7287
cd2d34d9 7288 /* DPLL not used with DSI, but still need the rest set up */
d7edc4e5 7289 if (!intel_crtc_has_type(pipe_config, INTEL_OUTPUT_DSI))
cd2d34d9
VS
7290 pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE;
7291
03ed5cbf
VS
7292 pipe_config->dpll_hw_state.dpll_md =
7293 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
bdd4b6a6
DV
7294}
7295
d288f65f 7296static void vlv_prepare_pll(struct intel_crtc *crtc,
5cec258b 7297 const struct intel_crtc_state *pipe_config)
a0c4da24 7298{
f47709a9 7299 struct drm_device *dev = crtc->base.dev;
fac5e23e 7300 struct drm_i915_private *dev_priv = to_i915(dev);
cd2d34d9 7301 enum pipe pipe = crtc->pipe;
bdd4b6a6 7302 u32 mdiv;
a0c4da24 7303 u32 bestn, bestm1, bestm2, bestp1, bestp2;
bdd4b6a6 7304 u32 coreclk, reg_val;
a0c4da24 7305
cd2d34d9
VS
7306 /* Enable Refclk */
7307 I915_WRITE(DPLL(pipe),
7308 pipe_config->dpll_hw_state.dpll &
7309 ~(DPLL_VCO_ENABLE | DPLL_EXT_BUFFER_ENABLE_VLV));
7310
7311 /* No need to actually set up the DPLL with DSI */
7312 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
7313 return;
7314
a580516d 7315 mutex_lock(&dev_priv->sb_lock);
09153000 7316
d288f65f
VS
7317 bestn = pipe_config->dpll.n;
7318 bestm1 = pipe_config->dpll.m1;
7319 bestm2 = pipe_config->dpll.m2;
7320 bestp1 = pipe_config->dpll.p1;
7321 bestp2 = pipe_config->dpll.p2;
a0c4da24 7322
89b667f8
JB
7323 /* See eDP HDMI DPIO driver vbios notes doc */
7324
7325 /* PLL B needs special handling */
bdd4b6a6 7326 if (pipe == PIPE_B)
5e69f97f 7327 vlv_pllb_recal_opamp(dev_priv, pipe);
89b667f8
JB
7328
7329 /* Set up Tx target for periodic Rcomp update */
ab3c759a 7330 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
89b667f8
JB
7331
7332 /* Disable target IRef on PLL */
ab3c759a 7333 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
89b667f8 7334 reg_val &= 0x00ffffff;
ab3c759a 7335 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
89b667f8
JB
7336
7337 /* Disable fast lock */
ab3c759a 7338 vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
89b667f8
JB
7339
7340 /* Set idtafcrecal before PLL is enabled */
a0c4da24
JB
7341 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
7342 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
7343 mdiv |= ((bestn << DPIO_N_SHIFT));
a0c4da24 7344 mdiv |= (1 << DPIO_K_SHIFT);
7df5080b
JB
7345
7346 /*
7347 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
7348 * but we don't support that).
7349 * Note: don't use the DAC post divider as it seems unstable.
7350 */
7351 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
ab3c759a 7352 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
a0c4da24 7353
a0c4da24 7354 mdiv |= DPIO_ENABLE_CALIBRATION;
ab3c759a 7355 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
a0c4da24 7356
89b667f8 7357 /* Set HBR and RBR LPF coefficients */
d288f65f 7358 if (pipe_config->port_clock == 162000 ||
2d84d2b3
VS
7359 intel_crtc_has_type(crtc->config, INTEL_OUTPUT_ANALOG) ||
7360 intel_crtc_has_type(crtc->config, INTEL_OUTPUT_HDMI))
ab3c759a 7361 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
885b0120 7362 0x009f0003);
89b667f8 7363 else
ab3c759a 7364 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
89b667f8
JB
7365 0x00d0000f);
7366
37a5650b 7367 if (intel_crtc_has_dp_encoder(pipe_config)) {
89b667f8 7368 /* Use SSC source */
bdd4b6a6 7369 if (pipe == PIPE_A)
ab3c759a 7370 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
7371 0x0df40000);
7372 else
ab3c759a 7373 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
7374 0x0df70000);
7375 } else { /* HDMI or VGA */
7376 /* Use bend source */
bdd4b6a6 7377 if (pipe == PIPE_A)
ab3c759a 7378 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
7379 0x0df70000);
7380 else
ab3c759a 7381 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
7382 0x0df40000);
7383 }
a0c4da24 7384
ab3c759a 7385 coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
89b667f8 7386 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
2210ce7f 7387 if (intel_crtc_has_dp_encoder(crtc->config))
89b667f8 7388 coreclk |= 0x01000000;
ab3c759a 7389 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
a0c4da24 7390
ab3c759a 7391 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
a580516d 7392 mutex_unlock(&dev_priv->sb_lock);
a0c4da24
JB
7393}
7394
d288f65f 7395static void chv_prepare_pll(struct intel_crtc *crtc,
5cec258b 7396 const struct intel_crtc_state *pipe_config)
9d556c99
CML
7397{
7398 struct drm_device *dev = crtc->base.dev;
fac5e23e 7399 struct drm_i915_private *dev_priv = to_i915(dev);
cd2d34d9 7400 enum pipe pipe = crtc->pipe;
9d556c99 7401 enum dpio_channel port = vlv_pipe_to_channel(pipe);
9cbe40c1 7402 u32 loopfilter, tribuf_calcntr;
9d556c99 7403 u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
a945ce7e 7404 u32 dpio_val;
9cbe40c1 7405 int vco;
9d556c99 7406
cd2d34d9
VS
7407 /* Enable Refclk and SSC */
7408 I915_WRITE(DPLL(pipe),
7409 pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
7410
7411 /* No need to actually set up the DPLL with DSI */
7412 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
7413 return;
7414
d288f65f
VS
7415 bestn = pipe_config->dpll.n;
7416 bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
7417 bestm1 = pipe_config->dpll.m1;
7418 bestm2 = pipe_config->dpll.m2 >> 22;
7419 bestp1 = pipe_config->dpll.p1;
7420 bestp2 = pipe_config->dpll.p2;
9cbe40c1 7421 vco = pipe_config->dpll.vco;
a945ce7e 7422 dpio_val = 0;
9cbe40c1 7423 loopfilter = 0;
9d556c99 7424
a580516d 7425 mutex_lock(&dev_priv->sb_lock);
9d556c99 7426
9d556c99
CML
7427 /* p1 and p2 divider */
7428 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
7429 5 << DPIO_CHV_S1_DIV_SHIFT |
7430 bestp1 << DPIO_CHV_P1_DIV_SHIFT |
7431 bestp2 << DPIO_CHV_P2_DIV_SHIFT |
7432 1 << DPIO_CHV_K_DIV_SHIFT);
7433
7434 /* Feedback post-divider - m2 */
7435 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
7436
7437 /* Feedback refclk divider - n and m1 */
7438 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
7439 DPIO_CHV_M1_DIV_BY_2 |
7440 1 << DPIO_CHV_N_DIV_SHIFT);
7441
7442 /* M2 fraction division */
25a25dfc 7443 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
9d556c99
CML
7444
7445 /* M2 fraction division enable */
a945ce7e
VP
7446 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
7447 dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
7448 dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
7449 if (bestm2_frac)
7450 dpio_val |= DPIO_CHV_FRAC_DIV_EN;
7451 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
9d556c99 7452
de3a0fde
VP
7453 /* Program digital lock detect threshold */
7454 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
7455 dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
7456 DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
7457 dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
7458 if (!bestm2_frac)
7459 dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
7460 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
7461
9d556c99 7462 /* Loop filter */
9cbe40c1
VP
7463 if (vco == 5400000) {
7464 loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
7465 loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
7466 loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
7467 tribuf_calcntr = 0x9;
7468 } else if (vco <= 6200000) {
7469 loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
7470 loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
7471 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7472 tribuf_calcntr = 0x9;
7473 } else if (vco <= 6480000) {
7474 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7475 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7476 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7477 tribuf_calcntr = 0x8;
7478 } else {
7479 /* Not supported. Apply the same limits as in the max case */
7480 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7481 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7482 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7483 tribuf_calcntr = 0;
7484 }
9d556c99
CML
7485 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
7486
968040b2 7487 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
9cbe40c1
VP
7488 dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
7489 dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
7490 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
7491
9d556c99
CML
7492 /* AFC Recal */
7493 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
7494 vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
7495 DPIO_AFC_RECAL);
7496
a580516d 7497 mutex_unlock(&dev_priv->sb_lock);
9d556c99
CML
7498}
7499
d288f65f
VS
7500/**
7501 * vlv_force_pll_on - forcibly enable just the PLL
7502 * @dev_priv: i915 private structure
7503 * @pipe: pipe PLL to enable
7504 * @dpll: PLL configuration
7505 *
7506 * Enable the PLL for @pipe using the supplied @dpll config. To be used
7507 * in cases where we need the PLL enabled even when @pipe is not going to
7508 * be enabled.
7509 */
3f36b937
TU
7510int vlv_force_pll_on(struct drm_device *dev, enum pipe pipe,
7511 const struct dpll *dpll)
d288f65f
VS
7512{
7513 struct intel_crtc *crtc =
7514 to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
3f36b937
TU
7515 struct intel_crtc_state *pipe_config;
7516
7517 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
7518 if (!pipe_config)
7519 return -ENOMEM;
7520
7521 pipe_config->base.crtc = &crtc->base;
7522 pipe_config->pixel_multiplier = 1;
7523 pipe_config->dpll = *dpll;
d288f65f
VS
7524
7525 if (IS_CHERRYVIEW(dev)) {
3f36b937
TU
7526 chv_compute_dpll(crtc, pipe_config);
7527 chv_prepare_pll(crtc, pipe_config);
7528 chv_enable_pll(crtc, pipe_config);
d288f65f 7529 } else {
3f36b937
TU
7530 vlv_compute_dpll(crtc, pipe_config);
7531 vlv_prepare_pll(crtc, pipe_config);
7532 vlv_enable_pll(crtc, pipe_config);
d288f65f 7533 }
3f36b937
TU
7534
7535 kfree(pipe_config);
7536
7537 return 0;
d288f65f
VS
7538}
7539
7540/**
7541 * vlv_force_pll_off - forcibly disable just the PLL
7542 * @dev_priv: i915 private structure
7543 * @pipe: pipe PLL to disable
7544 *
7545 * Disable the PLL for @pipe. To be used in cases where we need
7546 * the PLL enabled even when @pipe is not going to be enabled.
7547 */
7548void vlv_force_pll_off(struct drm_device *dev, enum pipe pipe)
7549{
7550 if (IS_CHERRYVIEW(dev))
7551 chv_disable_pll(to_i915(dev), pipe);
7552 else
7553 vlv_disable_pll(to_i915(dev), pipe);
7554}
7555
251ac862
DV
7556static void i9xx_compute_dpll(struct intel_crtc *crtc,
7557 struct intel_crtc_state *crtc_state,
9e2c8475 7558 struct dpll *reduced_clock)
eb1cbe48 7559{
f47709a9 7560 struct drm_device *dev = crtc->base.dev;
fac5e23e 7561 struct drm_i915_private *dev_priv = to_i915(dev);
eb1cbe48 7562 u32 dpll;
190f68c5 7563 struct dpll *clock = &crtc_state->dpll;
eb1cbe48 7564
190f68c5 7565 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
2a8f64ca 7566
eb1cbe48
DV
7567 dpll = DPLL_VGA_MODE_DIS;
7568
2d84d2b3 7569 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS))
eb1cbe48
DV
7570 dpll |= DPLLB_MODE_LVDS;
7571 else
7572 dpll |= DPLLB_MODE_DAC_SERIAL;
6cc5f341 7573
ef1b460d 7574 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
190f68c5 7575 dpll |= (crtc_state->pixel_multiplier - 1)
198a037f 7576 << SDVO_MULTIPLIER_SHIFT_HIRES;
eb1cbe48 7577 }
198a037f 7578
3d6e9ee0
VS
7579 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO) ||
7580 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
4a33e48d 7581 dpll |= DPLL_SDVO_HIGH_SPEED;
198a037f 7582
37a5650b 7583 if (intel_crtc_has_dp_encoder(crtc_state))
4a33e48d 7584 dpll |= DPLL_SDVO_HIGH_SPEED;
eb1cbe48
DV
7585
7586 /* compute bitmask from p1 value */
7587 if (IS_PINEVIEW(dev))
7588 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
7589 else {
7590 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7591 if (IS_G4X(dev) && reduced_clock)
7592 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
7593 }
7594 switch (clock->p2) {
7595 case 5:
7596 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
7597 break;
7598 case 7:
7599 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
7600 break;
7601 case 10:
7602 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
7603 break;
7604 case 14:
7605 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
7606 break;
7607 }
7608 if (INTEL_INFO(dev)->gen >= 4)
7609 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
7610
190f68c5 7611 if (crtc_state->sdvo_tv_clock)
eb1cbe48 7612 dpll |= PLL_REF_INPUT_TVCLKINBC;
2d84d2b3 7613 else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
ceb41007 7614 intel_panel_use_ssc(dev_priv))
eb1cbe48
DV
7615 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7616 else
7617 dpll |= PLL_REF_INPUT_DREFCLK;
7618
7619 dpll |= DPLL_VCO_ENABLE;
190f68c5 7620 crtc_state->dpll_hw_state.dpll = dpll;
8bcc2795 7621
eb1cbe48 7622 if (INTEL_INFO(dev)->gen >= 4) {
190f68c5 7623 u32 dpll_md = (crtc_state->pixel_multiplier - 1)
ef1b460d 7624 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
190f68c5 7625 crtc_state->dpll_hw_state.dpll_md = dpll_md;
eb1cbe48
DV
7626 }
7627}
7628
251ac862
DV
7629static void i8xx_compute_dpll(struct intel_crtc *crtc,
7630 struct intel_crtc_state *crtc_state,
9e2c8475 7631 struct dpll *reduced_clock)
eb1cbe48 7632{
f47709a9 7633 struct drm_device *dev = crtc->base.dev;
fac5e23e 7634 struct drm_i915_private *dev_priv = to_i915(dev);
eb1cbe48 7635 u32 dpll;
190f68c5 7636 struct dpll *clock = &crtc_state->dpll;
eb1cbe48 7637
190f68c5 7638 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
2a8f64ca 7639
eb1cbe48
DV
7640 dpll = DPLL_VGA_MODE_DIS;
7641
2d84d2b3 7642 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
eb1cbe48
DV
7643 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7644 } else {
7645 if (clock->p1 == 2)
7646 dpll |= PLL_P1_DIVIDE_BY_TWO;
7647 else
7648 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7649 if (clock->p2 == 4)
7650 dpll |= PLL_P2_DIVIDE_BY_4;
7651 }
7652
2d84d2b3 7653 if (!IS_I830(dev) && intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DVO))
4a33e48d
DV
7654 dpll |= DPLL_DVO_2X_MODE;
7655
2d84d2b3 7656 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
ceb41007 7657 intel_panel_use_ssc(dev_priv))
eb1cbe48
DV
7658 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7659 else
7660 dpll |= PLL_REF_INPUT_DREFCLK;
7661
7662 dpll |= DPLL_VCO_ENABLE;
190f68c5 7663 crtc_state->dpll_hw_state.dpll = dpll;
eb1cbe48
DV
7664}
7665
8a654f3b 7666static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
b0e77b9c
PZ
7667{
7668 struct drm_device *dev = intel_crtc->base.dev;
fac5e23e 7669 struct drm_i915_private *dev_priv = to_i915(dev);
b0e77b9c 7670 enum pipe pipe = intel_crtc->pipe;
6e3c9717 7671 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
7c5f93b0 7672 const struct drm_display_mode *adjusted_mode = &intel_crtc->config->base.adjusted_mode;
1caea6e9
VS
7673 uint32_t crtc_vtotal, crtc_vblank_end;
7674 int vsyncshift = 0;
4d8a62ea
DV
7675
7676 /* We need to be careful not to changed the adjusted mode, for otherwise
7677 * the hw state checker will get angry at the mismatch. */
7678 crtc_vtotal = adjusted_mode->crtc_vtotal;
7679 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
b0e77b9c 7680
609aeaca 7681 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
b0e77b9c 7682 /* the chip adds 2 halflines automatically */
4d8a62ea
DV
7683 crtc_vtotal -= 1;
7684 crtc_vblank_end -= 1;
609aeaca 7685
2d84d2b3 7686 if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
609aeaca
VS
7687 vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
7688 else
7689 vsyncshift = adjusted_mode->crtc_hsync_start -
7690 adjusted_mode->crtc_htotal / 2;
1caea6e9
VS
7691 if (vsyncshift < 0)
7692 vsyncshift += adjusted_mode->crtc_htotal;
b0e77b9c
PZ
7693 }
7694
7695 if (INTEL_INFO(dev)->gen > 3)
fe2b8f9d 7696 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
b0e77b9c 7697
fe2b8f9d 7698 I915_WRITE(HTOTAL(cpu_transcoder),
b0e77b9c
PZ
7699 (adjusted_mode->crtc_hdisplay - 1) |
7700 ((adjusted_mode->crtc_htotal - 1) << 16));
fe2b8f9d 7701 I915_WRITE(HBLANK(cpu_transcoder),
b0e77b9c
PZ
7702 (adjusted_mode->crtc_hblank_start - 1) |
7703 ((adjusted_mode->crtc_hblank_end - 1) << 16));
fe2b8f9d 7704 I915_WRITE(HSYNC(cpu_transcoder),
b0e77b9c
PZ
7705 (adjusted_mode->crtc_hsync_start - 1) |
7706 ((adjusted_mode->crtc_hsync_end - 1) << 16));
7707
fe2b8f9d 7708 I915_WRITE(VTOTAL(cpu_transcoder),
b0e77b9c 7709 (adjusted_mode->crtc_vdisplay - 1) |
4d8a62ea 7710 ((crtc_vtotal - 1) << 16));
fe2b8f9d 7711 I915_WRITE(VBLANK(cpu_transcoder),
b0e77b9c 7712 (adjusted_mode->crtc_vblank_start - 1) |
4d8a62ea 7713 ((crtc_vblank_end - 1) << 16));
fe2b8f9d 7714 I915_WRITE(VSYNC(cpu_transcoder),
b0e77b9c
PZ
7715 (adjusted_mode->crtc_vsync_start - 1) |
7716 ((adjusted_mode->crtc_vsync_end - 1) << 16));
7717
b5e508d4
PZ
7718 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
7719 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
7720 * documented on the DDI_FUNC_CTL register description, EDP Input Select
7721 * bits. */
7722 if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
7723 (pipe == PIPE_B || pipe == PIPE_C))
7724 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
7725
bc58be60
JN
7726}
7727
7728static void intel_set_pipe_src_size(struct intel_crtc *intel_crtc)
7729{
7730 struct drm_device *dev = intel_crtc->base.dev;
fac5e23e 7731 struct drm_i915_private *dev_priv = to_i915(dev);
bc58be60
JN
7732 enum pipe pipe = intel_crtc->pipe;
7733
b0e77b9c
PZ
7734 /* pipesrc controls the size that is scaled from, which should
7735 * always be the user's requested size.
7736 */
7737 I915_WRITE(PIPESRC(pipe),
6e3c9717
ACO
7738 ((intel_crtc->config->pipe_src_w - 1) << 16) |
7739 (intel_crtc->config->pipe_src_h - 1));
b0e77b9c
PZ
7740}
7741
1bd1bd80 7742static void intel_get_pipe_timings(struct intel_crtc *crtc,
5cec258b 7743 struct intel_crtc_state *pipe_config)
1bd1bd80
DV
7744{
7745 struct drm_device *dev = crtc->base.dev;
fac5e23e 7746 struct drm_i915_private *dev_priv = to_i915(dev);
1bd1bd80
DV
7747 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
7748 uint32_t tmp;
7749
7750 tmp = I915_READ(HTOTAL(cpu_transcoder));
2d112de7
ACO
7751 pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
7752 pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 7753 tmp = I915_READ(HBLANK(cpu_transcoder));
2d112de7
ACO
7754 pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
7755 pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 7756 tmp = I915_READ(HSYNC(cpu_transcoder));
2d112de7
ACO
7757 pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
7758 pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80
DV
7759
7760 tmp = I915_READ(VTOTAL(cpu_transcoder));
2d112de7
ACO
7761 pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
7762 pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 7763 tmp = I915_READ(VBLANK(cpu_transcoder));
2d112de7
ACO
7764 pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
7765 pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 7766 tmp = I915_READ(VSYNC(cpu_transcoder));
2d112de7
ACO
7767 pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
7768 pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80
DV
7769
7770 if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
2d112de7
ACO
7771 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
7772 pipe_config->base.adjusted_mode.crtc_vtotal += 1;
7773 pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
1bd1bd80 7774 }
bc58be60
JN
7775}
7776
7777static void intel_get_pipe_src_size(struct intel_crtc *crtc,
7778 struct intel_crtc_state *pipe_config)
7779{
7780 struct drm_device *dev = crtc->base.dev;
fac5e23e 7781 struct drm_i915_private *dev_priv = to_i915(dev);
bc58be60 7782 u32 tmp;
1bd1bd80
DV
7783
7784 tmp = I915_READ(PIPESRC(crtc->pipe));
37327abd
VS
7785 pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
7786 pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
7787
2d112de7
ACO
7788 pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
7789 pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
1bd1bd80
DV
7790}
7791
f6a83288 7792void intel_mode_from_pipe_config(struct drm_display_mode *mode,
5cec258b 7793 struct intel_crtc_state *pipe_config)
babea61d 7794{
2d112de7
ACO
7795 mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
7796 mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
7797 mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
7798 mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
babea61d 7799
2d112de7
ACO
7800 mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
7801 mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
7802 mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
7803 mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
babea61d 7804
2d112de7 7805 mode->flags = pipe_config->base.adjusted_mode.flags;
cd13f5ab 7806 mode->type = DRM_MODE_TYPE_DRIVER;
babea61d 7807
2d112de7
ACO
7808 mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
7809 mode->flags |= pipe_config->base.adjusted_mode.flags;
cd13f5ab
ML
7810
7811 mode->hsync = drm_mode_hsync(mode);
7812 mode->vrefresh = drm_mode_vrefresh(mode);
7813 drm_mode_set_name(mode);
babea61d
JB
7814}
7815
84b046f3
DV
7816static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
7817{
7818 struct drm_device *dev = intel_crtc->base.dev;
fac5e23e 7819 struct drm_i915_private *dev_priv = to_i915(dev);
84b046f3
DV
7820 uint32_t pipeconf;
7821
9f11a9e4 7822 pipeconf = 0;
84b046f3 7823
b6b5d049
VS
7824 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
7825 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
7826 pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
67c72a12 7827
6e3c9717 7828 if (intel_crtc->config->double_wide)
cf532bb2 7829 pipeconf |= PIPECONF_DOUBLE_WIDE;
84b046f3 7830
ff9ce46e 7831 /* only g4x and later have fancy bpc/dither controls */
666a4537 7832 if (IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
ff9ce46e 7833 /* Bspec claims that we can't use dithering for 30bpp pipes. */
6e3c9717 7834 if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
ff9ce46e 7835 pipeconf |= PIPECONF_DITHER_EN |
84b046f3 7836 PIPECONF_DITHER_TYPE_SP;
84b046f3 7837
6e3c9717 7838 switch (intel_crtc->config->pipe_bpp) {
ff9ce46e
DV
7839 case 18:
7840 pipeconf |= PIPECONF_6BPC;
7841 break;
7842 case 24:
7843 pipeconf |= PIPECONF_8BPC;
7844 break;
7845 case 30:
7846 pipeconf |= PIPECONF_10BPC;
7847 break;
7848 default:
7849 /* Case prevented by intel_choose_pipe_bpp_dither. */
7850 BUG();
84b046f3
DV
7851 }
7852 }
7853
7854 if (HAS_PIPE_CXSR(dev)) {
7855 if (intel_crtc->lowfreq_avail) {
7856 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
7857 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
7858 } else {
7859 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
84b046f3
DV
7860 }
7861 }
7862
6e3c9717 7863 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
efc2cfff 7864 if (INTEL_INFO(dev)->gen < 4 ||
2d84d2b3 7865 intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
efc2cfff
VS
7866 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
7867 else
7868 pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
7869 } else
84b046f3
DV
7870 pipeconf |= PIPECONF_PROGRESSIVE;
7871
666a4537
WB
7872 if ((IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) &&
7873 intel_crtc->config->limited_color_range)
9f11a9e4 7874 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
9c8e09b7 7875
84b046f3
DV
7876 I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
7877 POSTING_READ(PIPECONF(intel_crtc->pipe));
7878}
7879
81c97f52
ACO
7880static int i8xx_crtc_compute_clock(struct intel_crtc *crtc,
7881 struct intel_crtc_state *crtc_state)
7882{
7883 struct drm_device *dev = crtc->base.dev;
fac5e23e 7884 struct drm_i915_private *dev_priv = to_i915(dev);
1b6f4958 7885 const struct intel_limit *limit;
81c97f52
ACO
7886 int refclk = 48000;
7887
7888 memset(&crtc_state->dpll_hw_state, 0,
7889 sizeof(crtc_state->dpll_hw_state));
7890
2d84d2b3 7891 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
81c97f52
ACO
7892 if (intel_panel_use_ssc(dev_priv)) {
7893 refclk = dev_priv->vbt.lvds_ssc_freq;
7894 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
7895 }
7896
7897 limit = &intel_limits_i8xx_lvds;
2d84d2b3 7898 } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DVO)) {
81c97f52
ACO
7899 limit = &intel_limits_i8xx_dvo;
7900 } else {
7901 limit = &intel_limits_i8xx_dac;
7902 }
7903
7904 if (!crtc_state->clock_set &&
7905 !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7906 refclk, NULL, &crtc_state->dpll)) {
7907 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7908 return -EINVAL;
7909 }
7910
7911 i8xx_compute_dpll(crtc, crtc_state, NULL);
7912
7913 return 0;
7914}
7915
19ec6693
ACO
7916static int g4x_crtc_compute_clock(struct intel_crtc *crtc,
7917 struct intel_crtc_state *crtc_state)
7918{
7919 struct drm_device *dev = crtc->base.dev;
fac5e23e 7920 struct drm_i915_private *dev_priv = to_i915(dev);
1b6f4958 7921 const struct intel_limit *limit;
19ec6693
ACO
7922 int refclk = 96000;
7923
7924 memset(&crtc_state->dpll_hw_state, 0,
7925 sizeof(crtc_state->dpll_hw_state));
7926
2d84d2b3 7927 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
19ec6693
ACO
7928 if (intel_panel_use_ssc(dev_priv)) {
7929 refclk = dev_priv->vbt.lvds_ssc_freq;
7930 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
7931 }
7932
7933 if (intel_is_dual_link_lvds(dev))
7934 limit = &intel_limits_g4x_dual_channel_lvds;
7935 else
7936 limit = &intel_limits_g4x_single_channel_lvds;
2d84d2b3
VS
7937 } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI) ||
7938 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
19ec6693 7939 limit = &intel_limits_g4x_hdmi;
2d84d2b3 7940 } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO)) {
19ec6693
ACO
7941 limit = &intel_limits_g4x_sdvo;
7942 } else {
7943 /* The option is for other outputs */
7944 limit = &intel_limits_i9xx_sdvo;
7945 }
7946
7947 if (!crtc_state->clock_set &&
7948 !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7949 refclk, NULL, &crtc_state->dpll)) {
7950 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7951 return -EINVAL;
7952 }
7953
7954 i9xx_compute_dpll(crtc, crtc_state, NULL);
7955
7956 return 0;
7957}
7958
70e8aa21
ACO
7959static int pnv_crtc_compute_clock(struct intel_crtc *crtc,
7960 struct intel_crtc_state *crtc_state)
7961{
7962 struct drm_device *dev = crtc->base.dev;
fac5e23e 7963 struct drm_i915_private *dev_priv = to_i915(dev);
1b6f4958 7964 const struct intel_limit *limit;
70e8aa21
ACO
7965 int refclk = 96000;
7966
7967 memset(&crtc_state->dpll_hw_state, 0,
7968 sizeof(crtc_state->dpll_hw_state));
7969
2d84d2b3 7970 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
70e8aa21
ACO
7971 if (intel_panel_use_ssc(dev_priv)) {
7972 refclk = dev_priv->vbt.lvds_ssc_freq;
7973 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
7974 }
7975
7976 limit = &intel_limits_pineview_lvds;
7977 } else {
7978 limit = &intel_limits_pineview_sdvo;
7979 }
7980
7981 if (!crtc_state->clock_set &&
7982 !pnv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7983 refclk, NULL, &crtc_state->dpll)) {
7984 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7985 return -EINVAL;
7986 }
7987
7988 i9xx_compute_dpll(crtc, crtc_state, NULL);
7989
7990 return 0;
7991}
7992
190f68c5
ACO
7993static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
7994 struct intel_crtc_state *crtc_state)
79e53945 7995{
c7653199 7996 struct drm_device *dev = crtc->base.dev;
fac5e23e 7997 struct drm_i915_private *dev_priv = to_i915(dev);
1b6f4958 7998 const struct intel_limit *limit;
81c97f52 7999 int refclk = 96000;
79e53945 8000
dd3cd74a
ACO
8001 memset(&crtc_state->dpll_hw_state, 0,
8002 sizeof(crtc_state->dpll_hw_state));
8003
2d84d2b3 8004 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
70e8aa21
ACO
8005 if (intel_panel_use_ssc(dev_priv)) {
8006 refclk = dev_priv->vbt.lvds_ssc_freq;
8007 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
8008 }
43565a06 8009
70e8aa21
ACO
8010 limit = &intel_limits_i9xx_lvds;
8011 } else {
8012 limit = &intel_limits_i9xx_sdvo;
81c97f52 8013 }
79e53945 8014
70e8aa21
ACO
8015 if (!crtc_state->clock_set &&
8016 !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8017 refclk, NULL, &crtc_state->dpll)) {
8018 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8019 return -EINVAL;
f47709a9 8020 }
7026d4ac 8021
81c97f52 8022 i9xx_compute_dpll(crtc, crtc_state, NULL);
79e53945 8023
c8f7a0db 8024 return 0;
f564048e
EA
8025}
8026
65b3d6a9
ACO
8027static int chv_crtc_compute_clock(struct intel_crtc *crtc,
8028 struct intel_crtc_state *crtc_state)
8029{
8030 int refclk = 100000;
1b6f4958 8031 const struct intel_limit *limit = &intel_limits_chv;
65b3d6a9
ACO
8032
8033 memset(&crtc_state->dpll_hw_state, 0,
8034 sizeof(crtc_state->dpll_hw_state));
8035
65b3d6a9
ACO
8036 if (!crtc_state->clock_set &&
8037 !chv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8038 refclk, NULL, &crtc_state->dpll)) {
8039 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8040 return -EINVAL;
8041 }
8042
8043 chv_compute_dpll(crtc, crtc_state);
8044
8045 return 0;
8046}
8047
8048static int vlv_crtc_compute_clock(struct intel_crtc *crtc,
8049 struct intel_crtc_state *crtc_state)
8050{
8051 int refclk = 100000;
1b6f4958 8052 const struct intel_limit *limit = &intel_limits_vlv;
65b3d6a9
ACO
8053
8054 memset(&crtc_state->dpll_hw_state, 0,
8055 sizeof(crtc_state->dpll_hw_state));
8056
65b3d6a9
ACO
8057 if (!crtc_state->clock_set &&
8058 !vlv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8059 refclk, NULL, &crtc_state->dpll)) {
8060 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8061 return -EINVAL;
8062 }
8063
8064 vlv_compute_dpll(crtc, crtc_state);
8065
8066 return 0;
8067}
8068
2fa2fe9a 8069static void i9xx_get_pfit_config(struct intel_crtc *crtc,
5cec258b 8070 struct intel_crtc_state *pipe_config)
2fa2fe9a
DV
8071{
8072 struct drm_device *dev = crtc->base.dev;
fac5e23e 8073 struct drm_i915_private *dev_priv = to_i915(dev);
2fa2fe9a
DV
8074 uint32_t tmp;
8075
dc9e7dec
VS
8076 if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
8077 return;
8078
2fa2fe9a 8079 tmp = I915_READ(PFIT_CONTROL);
06922821
DV
8080 if (!(tmp & PFIT_ENABLE))
8081 return;
2fa2fe9a 8082
06922821 8083 /* Check whether the pfit is attached to our pipe. */
2fa2fe9a
DV
8084 if (INTEL_INFO(dev)->gen < 4) {
8085 if (crtc->pipe != PIPE_B)
8086 return;
2fa2fe9a
DV
8087 } else {
8088 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
8089 return;
8090 }
8091
06922821 8092 pipe_config->gmch_pfit.control = tmp;
2fa2fe9a 8093 pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
2fa2fe9a
DV
8094}
8095
acbec814 8096static void vlv_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 8097 struct intel_crtc_state *pipe_config)
acbec814
JB
8098{
8099 struct drm_device *dev = crtc->base.dev;
fac5e23e 8100 struct drm_i915_private *dev_priv = to_i915(dev);
acbec814 8101 int pipe = pipe_config->cpu_transcoder;
9e2c8475 8102 struct dpll clock;
acbec814 8103 u32 mdiv;
662c6ecb 8104 int refclk = 100000;
acbec814 8105
b521973b
VS
8106 /* In case of DSI, DPLL will not be used */
8107 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
f573de5a
SK
8108 return;
8109
a580516d 8110 mutex_lock(&dev_priv->sb_lock);
ab3c759a 8111 mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
a580516d 8112 mutex_unlock(&dev_priv->sb_lock);
acbec814
JB
8113
8114 clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
8115 clock.m2 = mdiv & DPIO_M2DIV_MASK;
8116 clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
8117 clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
8118 clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
8119
dccbea3b 8120 pipe_config->port_clock = vlv_calc_dpll_params(refclk, &clock);
acbec814
JB
8121}
8122
5724dbd1
DL
8123static void
8124i9xx_get_initial_plane_config(struct intel_crtc *crtc,
8125 struct intel_initial_plane_config *plane_config)
1ad292b5
JB
8126{
8127 struct drm_device *dev = crtc->base.dev;
fac5e23e 8128 struct drm_i915_private *dev_priv = to_i915(dev);
1ad292b5
JB
8129 u32 val, base, offset;
8130 int pipe = crtc->pipe, plane = crtc->plane;
8131 int fourcc, pixel_format;
6761dd31 8132 unsigned int aligned_height;
b113d5ee 8133 struct drm_framebuffer *fb;
1b842c89 8134 struct intel_framebuffer *intel_fb;
1ad292b5 8135
42a7b088
DL
8136 val = I915_READ(DSPCNTR(plane));
8137 if (!(val & DISPLAY_PLANE_ENABLE))
8138 return;
8139
d9806c9f 8140 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
1b842c89 8141 if (!intel_fb) {
1ad292b5
JB
8142 DRM_DEBUG_KMS("failed to alloc fb\n");
8143 return;
8144 }
8145
1b842c89
DL
8146 fb = &intel_fb->base;
8147
18c5247e
DV
8148 if (INTEL_INFO(dev)->gen >= 4) {
8149 if (val & DISPPLANE_TILED) {
49af449b 8150 plane_config->tiling = I915_TILING_X;
18c5247e
DV
8151 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
8152 }
8153 }
1ad292b5
JB
8154
8155 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
b35d63fa 8156 fourcc = i9xx_format_to_fourcc(pixel_format);
b113d5ee
DL
8157 fb->pixel_format = fourcc;
8158 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
1ad292b5
JB
8159
8160 if (INTEL_INFO(dev)->gen >= 4) {
49af449b 8161 if (plane_config->tiling)
1ad292b5
JB
8162 offset = I915_READ(DSPTILEOFF(plane));
8163 else
8164 offset = I915_READ(DSPLINOFF(plane));
8165 base = I915_READ(DSPSURF(plane)) & 0xfffff000;
8166 } else {
8167 base = I915_READ(DSPADDR(plane));
8168 }
8169 plane_config->base = base;
8170
8171 val = I915_READ(PIPESRC(pipe));
b113d5ee
DL
8172 fb->width = ((val >> 16) & 0xfff) + 1;
8173 fb->height = ((val >> 0) & 0xfff) + 1;
1ad292b5
JB
8174
8175 val = I915_READ(DSPSTRIDE(pipe));
b113d5ee 8176 fb->pitches[0] = val & 0xffffffc0;
1ad292b5 8177
b113d5ee 8178 aligned_height = intel_fb_align_height(dev, fb->height,
091df6cb
DV
8179 fb->pixel_format,
8180 fb->modifier[0]);
1ad292b5 8181
f37b5c2b 8182 plane_config->size = fb->pitches[0] * aligned_height;
1ad292b5 8183
2844a921
DL
8184 DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
8185 pipe_name(pipe), plane, fb->width, fb->height,
8186 fb->bits_per_pixel, base, fb->pitches[0],
8187 plane_config->size);
1ad292b5 8188
2d14030b 8189 plane_config->fb = intel_fb;
1ad292b5
JB
8190}
8191
70b23a98 8192static void chv_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 8193 struct intel_crtc_state *pipe_config)
70b23a98
VS
8194{
8195 struct drm_device *dev = crtc->base.dev;
fac5e23e 8196 struct drm_i915_private *dev_priv = to_i915(dev);
70b23a98
VS
8197 int pipe = pipe_config->cpu_transcoder;
8198 enum dpio_channel port = vlv_pipe_to_channel(pipe);
9e2c8475 8199 struct dpll clock;
0d7b6b11 8200 u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2, pll_dw3;
70b23a98
VS
8201 int refclk = 100000;
8202
b521973b
VS
8203 /* In case of DSI, DPLL will not be used */
8204 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
8205 return;
8206
a580516d 8207 mutex_lock(&dev_priv->sb_lock);
70b23a98
VS
8208 cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
8209 pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
8210 pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
8211 pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
0d7b6b11 8212 pll_dw3 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
a580516d 8213 mutex_unlock(&dev_priv->sb_lock);
70b23a98
VS
8214
8215 clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
0d7b6b11
ID
8216 clock.m2 = (pll_dw0 & 0xff) << 22;
8217 if (pll_dw3 & DPIO_CHV_FRAC_DIV_EN)
8218 clock.m2 |= pll_dw2 & 0x3fffff;
70b23a98
VS
8219 clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
8220 clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
8221 clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
8222
dccbea3b 8223 pipe_config->port_clock = chv_calc_dpll_params(refclk, &clock);
70b23a98
VS
8224}
8225
0e8ffe1b 8226static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
5cec258b 8227 struct intel_crtc_state *pipe_config)
0e8ffe1b
DV
8228{
8229 struct drm_device *dev = crtc->base.dev;
fac5e23e 8230 struct drm_i915_private *dev_priv = to_i915(dev);
1729050e 8231 enum intel_display_power_domain power_domain;
0e8ffe1b 8232 uint32_t tmp;
1729050e 8233 bool ret;
0e8ffe1b 8234
1729050e
ID
8235 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
8236 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
b5482bd0
ID
8237 return false;
8238
e143a21c 8239 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
8106ddbd 8240 pipe_config->shared_dpll = NULL;
eccb140b 8241
1729050e
ID
8242 ret = false;
8243
0e8ffe1b
DV
8244 tmp = I915_READ(PIPECONF(crtc->pipe));
8245 if (!(tmp & PIPECONF_ENABLE))
1729050e 8246 goto out;
0e8ffe1b 8247
666a4537 8248 if (IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
42571aef
VS
8249 switch (tmp & PIPECONF_BPC_MASK) {
8250 case PIPECONF_6BPC:
8251 pipe_config->pipe_bpp = 18;
8252 break;
8253 case PIPECONF_8BPC:
8254 pipe_config->pipe_bpp = 24;
8255 break;
8256 case PIPECONF_10BPC:
8257 pipe_config->pipe_bpp = 30;
8258 break;
8259 default:
8260 break;
8261 }
8262 }
8263
666a4537
WB
8264 if ((IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) &&
8265 (tmp & PIPECONF_COLOR_RANGE_SELECT))
b5a9fa09
DV
8266 pipe_config->limited_color_range = true;
8267
282740f7
VS
8268 if (INTEL_INFO(dev)->gen < 4)
8269 pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
8270
1bd1bd80 8271 intel_get_pipe_timings(crtc, pipe_config);
bc58be60 8272 intel_get_pipe_src_size(crtc, pipe_config);
1bd1bd80 8273
2fa2fe9a
DV
8274 i9xx_get_pfit_config(crtc, pipe_config);
8275
6c49f241 8276 if (INTEL_INFO(dev)->gen >= 4) {
c231775c
VS
8277 /* No way to read it out on pipes B and C */
8278 if (IS_CHERRYVIEW(dev) && crtc->pipe != PIPE_A)
8279 tmp = dev_priv->chv_dpll_md[crtc->pipe];
8280 else
8281 tmp = I915_READ(DPLL_MD(crtc->pipe));
6c49f241
DV
8282 pipe_config->pixel_multiplier =
8283 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
8284 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
8bcc2795 8285 pipe_config->dpll_hw_state.dpll_md = tmp;
6c49f241
DV
8286 } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
8287 tmp = I915_READ(DPLL(crtc->pipe));
8288 pipe_config->pixel_multiplier =
8289 ((tmp & SDVO_MULTIPLIER_MASK)
8290 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
8291 } else {
8292 /* Note that on i915G/GM the pixel multiplier is in the sdvo
8293 * port and will be fixed up in the encoder->get_config
8294 * function. */
8295 pipe_config->pixel_multiplier = 1;
8296 }
8bcc2795 8297 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
666a4537 8298 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
1c4e0274
VS
8299 /*
8300 * DPLL_DVO_2X_MODE must be enabled for both DPLLs
8301 * on 830. Filter it out here so that we don't
8302 * report errors due to that.
8303 */
8304 if (IS_I830(dev))
8305 pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
8306
8bcc2795
DV
8307 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
8308 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
165e901c
VS
8309 } else {
8310 /* Mask out read-only status bits. */
8311 pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
8312 DPLL_PORTC_READY_MASK |
8313 DPLL_PORTB_READY_MASK);
8bcc2795 8314 }
6c49f241 8315
70b23a98
VS
8316 if (IS_CHERRYVIEW(dev))
8317 chv_crtc_clock_get(crtc, pipe_config);
8318 else if (IS_VALLEYVIEW(dev))
acbec814
JB
8319 vlv_crtc_clock_get(crtc, pipe_config);
8320 else
8321 i9xx_crtc_clock_get(crtc, pipe_config);
18442d08 8322
0f64614d
VS
8323 /*
8324 * Normally the dotclock is filled in by the encoder .get_config()
8325 * but in case the pipe is enabled w/o any ports we need a sane
8326 * default.
8327 */
8328 pipe_config->base.adjusted_mode.crtc_clock =
8329 pipe_config->port_clock / pipe_config->pixel_multiplier;
8330
1729050e
ID
8331 ret = true;
8332
8333out:
8334 intel_display_power_put(dev_priv, power_domain);
8335
8336 return ret;
0e8ffe1b
DV
8337}
8338
dde86e2d 8339static void ironlake_init_pch_refclk(struct drm_device *dev)
13d83a67 8340{
fac5e23e 8341 struct drm_i915_private *dev_priv = to_i915(dev);
13d83a67 8342 struct intel_encoder *encoder;
1c1a24d2 8343 int i;
74cfd7ac 8344 u32 val, final;
13d83a67 8345 bool has_lvds = false;
199e5d79 8346 bool has_cpu_edp = false;
199e5d79 8347 bool has_panel = false;
99eb6a01
KP
8348 bool has_ck505 = false;
8349 bool can_ssc = false;
1c1a24d2 8350 bool using_ssc_source = false;
13d83a67
JB
8351
8352 /* We need to take the global config into account */
b2784e15 8353 for_each_intel_encoder(dev, encoder) {
199e5d79
KP
8354 switch (encoder->type) {
8355 case INTEL_OUTPUT_LVDS:
8356 has_panel = true;
8357 has_lvds = true;
8358 break;
8359 case INTEL_OUTPUT_EDP:
8360 has_panel = true;
2de6905f 8361 if (enc_to_dig_port(&encoder->base)->port == PORT_A)
199e5d79
KP
8362 has_cpu_edp = true;
8363 break;
6847d71b
PZ
8364 default:
8365 break;
13d83a67
JB
8366 }
8367 }
8368
99eb6a01 8369 if (HAS_PCH_IBX(dev)) {
41aa3448 8370 has_ck505 = dev_priv->vbt.display_clock_mode;
99eb6a01
KP
8371 can_ssc = has_ck505;
8372 } else {
8373 has_ck505 = false;
8374 can_ssc = true;
8375 }
8376
1c1a24d2
L
8377 /* Check if any DPLLs are using the SSC source */
8378 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
8379 u32 temp = I915_READ(PCH_DPLL(i));
8380
8381 if (!(temp & DPLL_VCO_ENABLE))
8382 continue;
8383
8384 if ((temp & PLL_REF_INPUT_MASK) ==
8385 PLLB_REF_INPUT_SPREADSPECTRUMIN) {
8386 using_ssc_source = true;
8387 break;
8388 }
8389 }
8390
8391 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d using_ssc_source %d\n",
8392 has_panel, has_lvds, has_ck505, using_ssc_source);
13d83a67
JB
8393
8394 /* Ironlake: try to setup display ref clock before DPLL
8395 * enabling. This is only under driver's control after
8396 * PCH B stepping, previous chipset stepping should be
8397 * ignoring this setting.
8398 */
74cfd7ac
CW
8399 val = I915_READ(PCH_DREF_CONTROL);
8400
8401 /* As we must carefully and slowly disable/enable each source in turn,
8402 * compute the final state we want first and check if we need to
8403 * make any changes at all.
8404 */
8405 final = val;
8406 final &= ~DREF_NONSPREAD_SOURCE_MASK;
8407 if (has_ck505)
8408 final |= DREF_NONSPREAD_CK505_ENABLE;
8409 else
8410 final |= DREF_NONSPREAD_SOURCE_ENABLE;
8411
8c07eb68 8412 final &= ~DREF_SSC_SOURCE_MASK;
74cfd7ac 8413 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
8c07eb68 8414 final &= ~DREF_SSC1_ENABLE;
74cfd7ac
CW
8415
8416 if (has_panel) {
8417 final |= DREF_SSC_SOURCE_ENABLE;
8418
8419 if (intel_panel_use_ssc(dev_priv) && can_ssc)
8420 final |= DREF_SSC1_ENABLE;
8421
8422 if (has_cpu_edp) {
8423 if (intel_panel_use_ssc(dev_priv) && can_ssc)
8424 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
8425 else
8426 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
8427 } else
8428 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
1c1a24d2
L
8429 } else if (using_ssc_source) {
8430 final |= DREF_SSC_SOURCE_ENABLE;
8431 final |= DREF_SSC1_ENABLE;
74cfd7ac
CW
8432 }
8433
8434 if (final == val)
8435 return;
8436
13d83a67 8437 /* Always enable nonspread source */
74cfd7ac 8438 val &= ~DREF_NONSPREAD_SOURCE_MASK;
13d83a67 8439
99eb6a01 8440 if (has_ck505)
74cfd7ac 8441 val |= DREF_NONSPREAD_CK505_ENABLE;
99eb6a01 8442 else
74cfd7ac 8443 val |= DREF_NONSPREAD_SOURCE_ENABLE;
13d83a67 8444
199e5d79 8445 if (has_panel) {
74cfd7ac
CW
8446 val &= ~DREF_SSC_SOURCE_MASK;
8447 val |= DREF_SSC_SOURCE_ENABLE;
13d83a67 8448
199e5d79 8449 /* SSC must be turned on before enabling the CPU output */
99eb6a01 8450 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
199e5d79 8451 DRM_DEBUG_KMS("Using SSC on panel\n");
74cfd7ac 8452 val |= DREF_SSC1_ENABLE;
e77166b5 8453 } else
74cfd7ac 8454 val &= ~DREF_SSC1_ENABLE;
199e5d79
KP
8455
8456 /* Get SSC going before enabling the outputs */
74cfd7ac 8457 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
8458 POSTING_READ(PCH_DREF_CONTROL);
8459 udelay(200);
8460
74cfd7ac 8461 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
13d83a67
JB
8462
8463 /* Enable CPU source on CPU attached eDP */
199e5d79 8464 if (has_cpu_edp) {
99eb6a01 8465 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
199e5d79 8466 DRM_DEBUG_KMS("Using SSC on eDP\n");
74cfd7ac 8467 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
eba905b2 8468 } else
74cfd7ac 8469 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
199e5d79 8470 } else
74cfd7ac 8471 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
199e5d79 8472
74cfd7ac 8473 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
8474 POSTING_READ(PCH_DREF_CONTROL);
8475 udelay(200);
8476 } else {
1c1a24d2 8477 DRM_DEBUG_KMS("Disabling CPU source output\n");
199e5d79 8478
74cfd7ac 8479 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
199e5d79
KP
8480
8481 /* Turn off CPU output */
74cfd7ac 8482 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
199e5d79 8483
74cfd7ac 8484 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
8485 POSTING_READ(PCH_DREF_CONTROL);
8486 udelay(200);
8487
1c1a24d2
L
8488 if (!using_ssc_source) {
8489 DRM_DEBUG_KMS("Disabling SSC source\n");
199e5d79 8490
1c1a24d2
L
8491 /* Turn off the SSC source */
8492 val &= ~DREF_SSC_SOURCE_MASK;
8493 val |= DREF_SSC_SOURCE_DISABLE;
f165d283 8494
1c1a24d2
L
8495 /* Turn off SSC1 */
8496 val &= ~DREF_SSC1_ENABLE;
8497
8498 I915_WRITE(PCH_DREF_CONTROL, val);
8499 POSTING_READ(PCH_DREF_CONTROL);
8500 udelay(200);
8501 }
13d83a67 8502 }
74cfd7ac
CW
8503
8504 BUG_ON(val != final);
13d83a67
JB
8505}
8506
f31f2d55 8507static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
dde86e2d 8508{
f31f2d55 8509 uint32_t tmp;
dde86e2d 8510
0ff066a9
PZ
8511 tmp = I915_READ(SOUTH_CHICKEN2);
8512 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
8513 I915_WRITE(SOUTH_CHICKEN2, tmp);
dde86e2d 8514
cf3598c2
ID
8515 if (wait_for_us(I915_READ(SOUTH_CHICKEN2) &
8516 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
0ff066a9 8517 DRM_ERROR("FDI mPHY reset assert timeout\n");
dde86e2d 8518
0ff066a9
PZ
8519 tmp = I915_READ(SOUTH_CHICKEN2);
8520 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
8521 I915_WRITE(SOUTH_CHICKEN2, tmp);
dde86e2d 8522
cf3598c2
ID
8523 if (wait_for_us((I915_READ(SOUTH_CHICKEN2) &
8524 FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
0ff066a9 8525 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
f31f2d55
PZ
8526}
8527
8528/* WaMPhyProgramming:hsw */
8529static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
8530{
8531 uint32_t tmp;
dde86e2d
PZ
8532
8533 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
8534 tmp &= ~(0xFF << 24);
8535 tmp |= (0x12 << 24);
8536 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
8537
dde86e2d
PZ
8538 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
8539 tmp |= (1 << 11);
8540 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
8541
8542 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
8543 tmp |= (1 << 11);
8544 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
8545
dde86e2d
PZ
8546 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
8547 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
8548 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
8549
8550 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
8551 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
8552 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
8553
0ff066a9
PZ
8554 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
8555 tmp &= ~(7 << 13);
8556 tmp |= (5 << 13);
8557 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
dde86e2d 8558
0ff066a9
PZ
8559 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
8560 tmp &= ~(7 << 13);
8561 tmp |= (5 << 13);
8562 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
dde86e2d
PZ
8563
8564 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
8565 tmp &= ~0xFF;
8566 tmp |= 0x1C;
8567 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
8568
8569 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
8570 tmp &= ~0xFF;
8571 tmp |= 0x1C;
8572 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
8573
8574 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
8575 tmp &= ~(0xFF << 16);
8576 tmp |= (0x1C << 16);
8577 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
8578
8579 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
8580 tmp &= ~(0xFF << 16);
8581 tmp |= (0x1C << 16);
8582 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
8583
0ff066a9
PZ
8584 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
8585 tmp |= (1 << 27);
8586 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
dde86e2d 8587
0ff066a9
PZ
8588 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
8589 tmp |= (1 << 27);
8590 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
dde86e2d 8591
0ff066a9
PZ
8592 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
8593 tmp &= ~(0xF << 28);
8594 tmp |= (4 << 28);
8595 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
dde86e2d 8596
0ff066a9
PZ
8597 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
8598 tmp &= ~(0xF << 28);
8599 tmp |= (4 << 28);
8600 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
f31f2d55
PZ
8601}
8602
2fa86a1f
PZ
8603/* Implements 3 different sequences from BSpec chapter "Display iCLK
8604 * Programming" based on the parameters passed:
8605 * - Sequence to enable CLKOUT_DP
8606 * - Sequence to enable CLKOUT_DP without spread
8607 * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
8608 */
8609static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
8610 bool with_fdi)
f31f2d55 8611{
fac5e23e 8612 struct drm_i915_private *dev_priv = to_i915(dev);
2fa86a1f
PZ
8613 uint32_t reg, tmp;
8614
8615 if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
8616 with_spread = true;
c2699524 8617 if (WARN(HAS_PCH_LPT_LP(dev) && with_fdi, "LP PCH doesn't have FDI\n"))
2fa86a1f 8618 with_fdi = false;
f31f2d55 8619
a580516d 8620 mutex_lock(&dev_priv->sb_lock);
f31f2d55
PZ
8621
8622 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8623 tmp &= ~SBI_SSCCTL_DISABLE;
8624 tmp |= SBI_SSCCTL_PATHALT;
8625 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8626
8627 udelay(24);
8628
2fa86a1f
PZ
8629 if (with_spread) {
8630 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8631 tmp &= ~SBI_SSCCTL_PATHALT;
8632 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
f31f2d55 8633
2fa86a1f
PZ
8634 if (with_fdi) {
8635 lpt_reset_fdi_mphy(dev_priv);
8636 lpt_program_fdi_mphy(dev_priv);
8637 }
8638 }
dde86e2d 8639
c2699524 8640 reg = HAS_PCH_LPT_LP(dev) ? SBI_GEN0 : SBI_DBUFF0;
2fa86a1f
PZ
8641 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8642 tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8643 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
c00db246 8644
a580516d 8645 mutex_unlock(&dev_priv->sb_lock);
dde86e2d
PZ
8646}
8647
47701c3b
PZ
8648/* Sequence to disable CLKOUT_DP */
8649static void lpt_disable_clkout_dp(struct drm_device *dev)
8650{
fac5e23e 8651 struct drm_i915_private *dev_priv = to_i915(dev);
47701c3b
PZ
8652 uint32_t reg, tmp;
8653
a580516d 8654 mutex_lock(&dev_priv->sb_lock);
47701c3b 8655
c2699524 8656 reg = HAS_PCH_LPT_LP(dev) ? SBI_GEN0 : SBI_DBUFF0;
47701c3b
PZ
8657 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8658 tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8659 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
8660
8661 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8662 if (!(tmp & SBI_SSCCTL_DISABLE)) {
8663 if (!(tmp & SBI_SSCCTL_PATHALT)) {
8664 tmp |= SBI_SSCCTL_PATHALT;
8665 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8666 udelay(32);
8667 }
8668 tmp |= SBI_SSCCTL_DISABLE;
8669 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8670 }
8671
a580516d 8672 mutex_unlock(&dev_priv->sb_lock);
47701c3b
PZ
8673}
8674
f7be2c21
VS
8675#define BEND_IDX(steps) ((50 + (steps)) / 5)
8676
8677static const uint16_t sscdivintphase[] = {
8678 [BEND_IDX( 50)] = 0x3B23,
8679 [BEND_IDX( 45)] = 0x3B23,
8680 [BEND_IDX( 40)] = 0x3C23,
8681 [BEND_IDX( 35)] = 0x3C23,
8682 [BEND_IDX( 30)] = 0x3D23,
8683 [BEND_IDX( 25)] = 0x3D23,
8684 [BEND_IDX( 20)] = 0x3E23,
8685 [BEND_IDX( 15)] = 0x3E23,
8686 [BEND_IDX( 10)] = 0x3F23,
8687 [BEND_IDX( 5)] = 0x3F23,
8688 [BEND_IDX( 0)] = 0x0025,
8689 [BEND_IDX( -5)] = 0x0025,
8690 [BEND_IDX(-10)] = 0x0125,
8691 [BEND_IDX(-15)] = 0x0125,
8692 [BEND_IDX(-20)] = 0x0225,
8693 [BEND_IDX(-25)] = 0x0225,
8694 [BEND_IDX(-30)] = 0x0325,
8695 [BEND_IDX(-35)] = 0x0325,
8696 [BEND_IDX(-40)] = 0x0425,
8697 [BEND_IDX(-45)] = 0x0425,
8698 [BEND_IDX(-50)] = 0x0525,
8699};
8700
8701/*
8702 * Bend CLKOUT_DP
8703 * steps -50 to 50 inclusive, in steps of 5
8704 * < 0 slow down the clock, > 0 speed up the clock, 0 == no bend (135MHz)
8705 * change in clock period = -(steps / 10) * 5.787 ps
8706 */
8707static void lpt_bend_clkout_dp(struct drm_i915_private *dev_priv, int steps)
8708{
8709 uint32_t tmp;
8710 int idx = BEND_IDX(steps);
8711
8712 if (WARN_ON(steps % 5 != 0))
8713 return;
8714
8715 if (WARN_ON(idx >= ARRAY_SIZE(sscdivintphase)))
8716 return;
8717
8718 mutex_lock(&dev_priv->sb_lock);
8719
8720 if (steps % 10 != 0)
8721 tmp = 0xAAAAAAAB;
8722 else
8723 tmp = 0x00000000;
8724 intel_sbi_write(dev_priv, SBI_SSCDITHPHASE, tmp, SBI_ICLK);
8725
8726 tmp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE, SBI_ICLK);
8727 tmp &= 0xffff0000;
8728 tmp |= sscdivintphase[idx];
8729 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE, tmp, SBI_ICLK);
8730
8731 mutex_unlock(&dev_priv->sb_lock);
8732}
8733
8734#undef BEND_IDX
8735
bf8fa3d3
PZ
8736static void lpt_init_pch_refclk(struct drm_device *dev)
8737{
bf8fa3d3
PZ
8738 struct intel_encoder *encoder;
8739 bool has_vga = false;
8740
b2784e15 8741 for_each_intel_encoder(dev, encoder) {
bf8fa3d3
PZ
8742 switch (encoder->type) {
8743 case INTEL_OUTPUT_ANALOG:
8744 has_vga = true;
8745 break;
6847d71b
PZ
8746 default:
8747 break;
bf8fa3d3
PZ
8748 }
8749 }
8750
f7be2c21
VS
8751 if (has_vga) {
8752 lpt_bend_clkout_dp(to_i915(dev), 0);
47701c3b 8753 lpt_enable_clkout_dp(dev, true, true);
f7be2c21 8754 } else {
47701c3b 8755 lpt_disable_clkout_dp(dev);
f7be2c21 8756 }
bf8fa3d3
PZ
8757}
8758
dde86e2d
PZ
8759/*
8760 * Initialize reference clocks when the driver loads
8761 */
8762void intel_init_pch_refclk(struct drm_device *dev)
8763{
8764 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
8765 ironlake_init_pch_refclk(dev);
8766 else if (HAS_PCH_LPT(dev))
8767 lpt_init_pch_refclk(dev);
8768}
8769
6ff93609 8770static void ironlake_set_pipeconf(struct drm_crtc *crtc)
79e53945 8771{
fac5e23e 8772 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
79e53945
JB
8773 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8774 int pipe = intel_crtc->pipe;
c8203565
PZ
8775 uint32_t val;
8776
78114071 8777 val = 0;
c8203565 8778
6e3c9717 8779 switch (intel_crtc->config->pipe_bpp) {
c8203565 8780 case 18:
dfd07d72 8781 val |= PIPECONF_6BPC;
c8203565
PZ
8782 break;
8783 case 24:
dfd07d72 8784 val |= PIPECONF_8BPC;
c8203565
PZ
8785 break;
8786 case 30:
dfd07d72 8787 val |= PIPECONF_10BPC;
c8203565
PZ
8788 break;
8789 case 36:
dfd07d72 8790 val |= PIPECONF_12BPC;
c8203565
PZ
8791 break;
8792 default:
cc769b62
PZ
8793 /* Case prevented by intel_choose_pipe_bpp_dither. */
8794 BUG();
c8203565
PZ
8795 }
8796
6e3c9717 8797 if (intel_crtc->config->dither)
c8203565
PZ
8798 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8799
6e3c9717 8800 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
c8203565
PZ
8801 val |= PIPECONF_INTERLACED_ILK;
8802 else
8803 val |= PIPECONF_PROGRESSIVE;
8804
6e3c9717 8805 if (intel_crtc->config->limited_color_range)
3685a8f3 8806 val |= PIPECONF_COLOR_RANGE_SELECT;
3685a8f3 8807
c8203565
PZ
8808 I915_WRITE(PIPECONF(pipe), val);
8809 POSTING_READ(PIPECONF(pipe));
8810}
8811
6ff93609 8812static void haswell_set_pipeconf(struct drm_crtc *crtc)
ee2b0b38 8813{
fac5e23e 8814 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
ee2b0b38 8815 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6e3c9717 8816 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
391bf048 8817 u32 val = 0;
ee2b0b38 8818
391bf048 8819 if (IS_HASWELL(dev_priv) && intel_crtc->config->dither)
ee2b0b38
PZ
8820 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8821
6e3c9717 8822 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
ee2b0b38
PZ
8823 val |= PIPECONF_INTERLACED_ILK;
8824 else
8825 val |= PIPECONF_PROGRESSIVE;
8826
702e7a56
PZ
8827 I915_WRITE(PIPECONF(cpu_transcoder), val);
8828 POSTING_READ(PIPECONF(cpu_transcoder));
391bf048
JN
8829}
8830
391bf048
JN
8831static void haswell_set_pipemisc(struct drm_crtc *crtc)
8832{
fac5e23e 8833 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
391bf048 8834 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
756f85cf 8835
391bf048
JN
8836 if (IS_BROADWELL(dev_priv) || INTEL_INFO(dev_priv)->gen >= 9) {
8837 u32 val = 0;
756f85cf 8838
6e3c9717 8839 switch (intel_crtc->config->pipe_bpp) {
756f85cf
PZ
8840 case 18:
8841 val |= PIPEMISC_DITHER_6_BPC;
8842 break;
8843 case 24:
8844 val |= PIPEMISC_DITHER_8_BPC;
8845 break;
8846 case 30:
8847 val |= PIPEMISC_DITHER_10_BPC;
8848 break;
8849 case 36:
8850 val |= PIPEMISC_DITHER_12_BPC;
8851 break;
8852 default:
8853 /* Case prevented by pipe_config_set_bpp. */
8854 BUG();
8855 }
8856
6e3c9717 8857 if (intel_crtc->config->dither)
756f85cf
PZ
8858 val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
8859
391bf048 8860 I915_WRITE(PIPEMISC(intel_crtc->pipe), val);
756f85cf 8861 }
ee2b0b38
PZ
8862}
8863
d4b1931c
PZ
8864int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
8865{
8866 /*
8867 * Account for spread spectrum to avoid
8868 * oversubscribing the link. Max center spread
8869 * is 2.5%; use 5% for safety's sake.
8870 */
8871 u32 bps = target_clock * bpp * 21 / 20;
619d4d04 8872 return DIV_ROUND_UP(bps, link_bw * 8);
d4b1931c
PZ
8873}
8874
7429e9d4 8875static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
6cf86a5e 8876{
7429e9d4 8877 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
f48d8f23
PZ
8878}
8879
b75ca6f6
ACO
8880static void ironlake_compute_dpll(struct intel_crtc *intel_crtc,
8881 struct intel_crtc_state *crtc_state,
9e2c8475 8882 struct dpll *reduced_clock)
79e53945 8883{
de13a2e3 8884 struct drm_crtc *crtc = &intel_crtc->base;
79e53945 8885 struct drm_device *dev = crtc->dev;
fac5e23e 8886 struct drm_i915_private *dev_priv = to_i915(dev);
b75ca6f6 8887 u32 dpll, fp, fp2;
3d6e9ee0 8888 int factor;
79e53945 8889
c1858123 8890 /* Enable autotuning of the PLL clock (if permissible) */
8febb297 8891 factor = 21;
3d6e9ee0 8892 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
8febb297 8893 if ((intel_panel_use_ssc(dev_priv) &&
e91e941b 8894 dev_priv->vbt.lvds_ssc_freq == 100000) ||
f0b44056 8895 (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
8febb297 8896 factor = 25;
190f68c5 8897 } else if (crtc_state->sdvo_tv_clock)
8febb297 8898 factor = 20;
c1858123 8899
b75ca6f6
ACO
8900 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
8901
190f68c5 8902 if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
b75ca6f6
ACO
8903 fp |= FP_CB_TUNE;
8904
8905 if (reduced_clock) {
8906 fp2 = i9xx_dpll_compute_fp(reduced_clock);
2c07245f 8907
b75ca6f6
ACO
8908 if (reduced_clock->m < factor * reduced_clock->n)
8909 fp2 |= FP_CB_TUNE;
8910 } else {
8911 fp2 = fp;
8912 }
9a7c7890 8913
5eddb70b 8914 dpll = 0;
2c07245f 8915
3d6e9ee0 8916 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS))
a07d6787
EA
8917 dpll |= DPLLB_MODE_LVDS;
8918 else
8919 dpll |= DPLLB_MODE_DAC_SERIAL;
198a037f 8920
190f68c5 8921 dpll |= (crtc_state->pixel_multiplier - 1)
ef1b460d 8922 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
198a037f 8923
3d6e9ee0
VS
8924 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO) ||
8925 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
4a33e48d 8926 dpll |= DPLL_SDVO_HIGH_SPEED;
3d6e9ee0 8927
37a5650b 8928 if (intel_crtc_has_dp_encoder(crtc_state))
4a33e48d 8929 dpll |= DPLL_SDVO_HIGH_SPEED;
79e53945 8930
a07d6787 8931 /* compute bitmask from p1 value */
190f68c5 8932 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
a07d6787 8933 /* also FPA1 */
190f68c5 8934 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
a07d6787 8935
190f68c5 8936 switch (crtc_state->dpll.p2) {
a07d6787
EA
8937 case 5:
8938 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
8939 break;
8940 case 7:
8941 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
8942 break;
8943 case 10:
8944 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
8945 break;
8946 case 14:
8947 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
8948 break;
79e53945
JB
8949 }
8950
3d6e9ee0
VS
8951 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
8952 intel_panel_use_ssc(dev_priv))
43565a06 8953 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
79e53945
JB
8954 else
8955 dpll |= PLL_REF_INPUT_DREFCLK;
8956
b75ca6f6
ACO
8957 dpll |= DPLL_VCO_ENABLE;
8958
8959 crtc_state->dpll_hw_state.dpll = dpll;
8960 crtc_state->dpll_hw_state.fp0 = fp;
8961 crtc_state->dpll_hw_state.fp1 = fp2;
de13a2e3
PZ
8962}
8963
190f68c5
ACO
8964static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
8965 struct intel_crtc_state *crtc_state)
de13a2e3 8966{
997c030c 8967 struct drm_device *dev = crtc->base.dev;
fac5e23e 8968 struct drm_i915_private *dev_priv = to_i915(dev);
9e2c8475 8969 struct dpll reduced_clock;
7ed9f894 8970 bool has_reduced_clock = false;
e2b78267 8971 struct intel_shared_dpll *pll;
1b6f4958 8972 const struct intel_limit *limit;
997c030c 8973 int refclk = 120000;
de13a2e3 8974
dd3cd74a
ACO
8975 memset(&crtc_state->dpll_hw_state, 0,
8976 sizeof(crtc_state->dpll_hw_state));
8977
ded220e2
ACO
8978 crtc->lowfreq_avail = false;
8979
8980 /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
8981 if (!crtc_state->has_pch_encoder)
8982 return 0;
79e53945 8983
2d84d2b3 8984 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
997c030c
ACO
8985 if (intel_panel_use_ssc(dev_priv)) {
8986 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
8987 dev_priv->vbt.lvds_ssc_freq);
8988 refclk = dev_priv->vbt.lvds_ssc_freq;
8989 }
8990
8991 if (intel_is_dual_link_lvds(dev)) {
8992 if (refclk == 100000)
8993 limit = &intel_limits_ironlake_dual_lvds_100m;
8994 else
8995 limit = &intel_limits_ironlake_dual_lvds;
8996 } else {
8997 if (refclk == 100000)
8998 limit = &intel_limits_ironlake_single_lvds_100m;
8999 else
9000 limit = &intel_limits_ironlake_single_lvds;
9001 }
9002 } else {
9003 limit = &intel_limits_ironlake_dac;
9004 }
9005
364ee29d 9006 if (!crtc_state->clock_set &&
997c030c
ACO
9007 !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
9008 refclk, NULL, &crtc_state->dpll)) {
364ee29d
ACO
9009 DRM_ERROR("Couldn't find PLL settings for mode!\n");
9010 return -EINVAL;
f47709a9 9011 }
79e53945 9012
b75ca6f6
ACO
9013 ironlake_compute_dpll(crtc, crtc_state,
9014 has_reduced_clock ? &reduced_clock : NULL);
66e985c0 9015
ded220e2
ACO
9016 pll = intel_get_shared_dpll(crtc, crtc_state, NULL);
9017 if (pll == NULL) {
9018 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
9019 pipe_name(crtc->pipe));
9020 return -EINVAL;
3fb37703 9021 }
79e53945 9022
2d84d2b3 9023 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
ded220e2 9024 has_reduced_clock)
c7653199 9025 crtc->lowfreq_avail = true;
e2b78267 9026
c8f7a0db 9027 return 0;
79e53945
JB
9028}
9029
eb14cb74
VS
9030static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
9031 struct intel_link_m_n *m_n)
9032{
9033 struct drm_device *dev = crtc->base.dev;
fac5e23e 9034 struct drm_i915_private *dev_priv = to_i915(dev);
eb14cb74
VS
9035 enum pipe pipe = crtc->pipe;
9036
9037 m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
9038 m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
9039 m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
9040 & ~TU_SIZE_MASK;
9041 m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
9042 m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
9043 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
9044}
9045
9046static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
9047 enum transcoder transcoder,
b95af8be
VK
9048 struct intel_link_m_n *m_n,
9049 struct intel_link_m_n *m2_n2)
72419203
DV
9050{
9051 struct drm_device *dev = crtc->base.dev;
fac5e23e 9052 struct drm_i915_private *dev_priv = to_i915(dev);
eb14cb74 9053 enum pipe pipe = crtc->pipe;
72419203 9054
eb14cb74
VS
9055 if (INTEL_INFO(dev)->gen >= 5) {
9056 m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
9057 m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
9058 m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
9059 & ~TU_SIZE_MASK;
9060 m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
9061 m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
9062 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
b95af8be
VK
9063 /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
9064 * gen < 8) and if DRRS is supported (to make sure the
9065 * registers are not unnecessarily read).
9066 */
9067 if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
6e3c9717 9068 crtc->config->has_drrs) {
b95af8be
VK
9069 m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
9070 m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
9071 m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
9072 & ~TU_SIZE_MASK;
9073 m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
9074 m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
9075 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
9076 }
eb14cb74
VS
9077 } else {
9078 m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
9079 m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
9080 m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
9081 & ~TU_SIZE_MASK;
9082 m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
9083 m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
9084 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
9085 }
9086}
9087
9088void intel_dp_get_m_n(struct intel_crtc *crtc,
5cec258b 9089 struct intel_crtc_state *pipe_config)
eb14cb74 9090{
681a8504 9091 if (pipe_config->has_pch_encoder)
eb14cb74
VS
9092 intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
9093 else
9094 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
b95af8be
VK
9095 &pipe_config->dp_m_n,
9096 &pipe_config->dp_m2_n2);
eb14cb74 9097}
72419203 9098
eb14cb74 9099static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
5cec258b 9100 struct intel_crtc_state *pipe_config)
eb14cb74
VS
9101{
9102 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
b95af8be 9103 &pipe_config->fdi_m_n, NULL);
72419203
DV
9104}
9105
bd2e244f 9106static void skylake_get_pfit_config(struct intel_crtc *crtc,
5cec258b 9107 struct intel_crtc_state *pipe_config)
bd2e244f
JB
9108{
9109 struct drm_device *dev = crtc->base.dev;
fac5e23e 9110 struct drm_i915_private *dev_priv = to_i915(dev);
a1b2278e
CK
9111 struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
9112 uint32_t ps_ctrl = 0;
9113 int id = -1;
9114 int i;
bd2e244f 9115
a1b2278e
CK
9116 /* find scaler attached to this pipe */
9117 for (i = 0; i < crtc->num_scalers; i++) {
9118 ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
9119 if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
9120 id = i;
9121 pipe_config->pch_pfit.enabled = true;
9122 pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
9123 pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
9124 break;
9125 }
9126 }
bd2e244f 9127
a1b2278e
CK
9128 scaler_state->scaler_id = id;
9129 if (id >= 0) {
9130 scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
9131 } else {
9132 scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
bd2e244f
JB
9133 }
9134}
9135
5724dbd1
DL
9136static void
9137skylake_get_initial_plane_config(struct intel_crtc *crtc,
9138 struct intel_initial_plane_config *plane_config)
bc8d7dff
DL
9139{
9140 struct drm_device *dev = crtc->base.dev;
fac5e23e 9141 struct drm_i915_private *dev_priv = to_i915(dev);
40f46283 9142 u32 val, base, offset, stride_mult, tiling;
bc8d7dff
DL
9143 int pipe = crtc->pipe;
9144 int fourcc, pixel_format;
6761dd31 9145 unsigned int aligned_height;
bc8d7dff 9146 struct drm_framebuffer *fb;
1b842c89 9147 struct intel_framebuffer *intel_fb;
bc8d7dff 9148
d9806c9f 9149 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
1b842c89 9150 if (!intel_fb) {
bc8d7dff
DL
9151 DRM_DEBUG_KMS("failed to alloc fb\n");
9152 return;
9153 }
9154
1b842c89
DL
9155 fb = &intel_fb->base;
9156
bc8d7dff 9157 val = I915_READ(PLANE_CTL(pipe, 0));
42a7b088
DL
9158 if (!(val & PLANE_CTL_ENABLE))
9159 goto error;
9160
bc8d7dff
DL
9161 pixel_format = val & PLANE_CTL_FORMAT_MASK;
9162 fourcc = skl_format_to_fourcc(pixel_format,
9163 val & PLANE_CTL_ORDER_RGBX,
9164 val & PLANE_CTL_ALPHA_MASK);
9165 fb->pixel_format = fourcc;
9166 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
9167
40f46283
DL
9168 tiling = val & PLANE_CTL_TILED_MASK;
9169 switch (tiling) {
9170 case PLANE_CTL_TILED_LINEAR:
9171 fb->modifier[0] = DRM_FORMAT_MOD_NONE;
9172 break;
9173 case PLANE_CTL_TILED_X:
9174 plane_config->tiling = I915_TILING_X;
9175 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
9176 break;
9177 case PLANE_CTL_TILED_Y:
9178 fb->modifier[0] = I915_FORMAT_MOD_Y_TILED;
9179 break;
9180 case PLANE_CTL_TILED_YF:
9181 fb->modifier[0] = I915_FORMAT_MOD_Yf_TILED;
9182 break;
9183 default:
9184 MISSING_CASE(tiling);
9185 goto error;
9186 }
9187
bc8d7dff
DL
9188 base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
9189 plane_config->base = base;
9190
9191 offset = I915_READ(PLANE_OFFSET(pipe, 0));
9192
9193 val = I915_READ(PLANE_SIZE(pipe, 0));
9194 fb->height = ((val >> 16) & 0xfff) + 1;
9195 fb->width = ((val >> 0) & 0x1fff) + 1;
9196
9197 val = I915_READ(PLANE_STRIDE(pipe, 0));
7b49f948 9198 stride_mult = intel_fb_stride_alignment(dev_priv, fb->modifier[0],
40f46283 9199 fb->pixel_format);
bc8d7dff
DL
9200 fb->pitches[0] = (val & 0x3ff) * stride_mult;
9201
9202 aligned_height = intel_fb_align_height(dev, fb->height,
091df6cb
DV
9203 fb->pixel_format,
9204 fb->modifier[0]);
bc8d7dff 9205
f37b5c2b 9206 plane_config->size = fb->pitches[0] * aligned_height;
bc8d7dff
DL
9207
9208 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9209 pipe_name(pipe), fb->width, fb->height,
9210 fb->bits_per_pixel, base, fb->pitches[0],
9211 plane_config->size);
9212
2d14030b 9213 plane_config->fb = intel_fb;
bc8d7dff
DL
9214 return;
9215
9216error:
9217 kfree(fb);
9218}
9219
2fa2fe9a 9220static void ironlake_get_pfit_config(struct intel_crtc *crtc,
5cec258b 9221 struct intel_crtc_state *pipe_config)
2fa2fe9a
DV
9222{
9223 struct drm_device *dev = crtc->base.dev;
fac5e23e 9224 struct drm_i915_private *dev_priv = to_i915(dev);
2fa2fe9a
DV
9225 uint32_t tmp;
9226
9227 tmp = I915_READ(PF_CTL(crtc->pipe));
9228
9229 if (tmp & PF_ENABLE) {
fd4daa9c 9230 pipe_config->pch_pfit.enabled = true;
2fa2fe9a
DV
9231 pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
9232 pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
cb8b2a30
DV
9233
9234 /* We currently do not free assignements of panel fitters on
9235 * ivb/hsw (since we don't use the higher upscaling modes which
9236 * differentiates them) so just WARN about this case for now. */
9237 if (IS_GEN7(dev)) {
9238 WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
9239 PF_PIPE_SEL_IVB(crtc->pipe));
9240 }
2fa2fe9a 9241 }
79e53945
JB
9242}
9243
5724dbd1
DL
9244static void
9245ironlake_get_initial_plane_config(struct intel_crtc *crtc,
9246 struct intel_initial_plane_config *plane_config)
4c6baa59
JB
9247{
9248 struct drm_device *dev = crtc->base.dev;
fac5e23e 9249 struct drm_i915_private *dev_priv = to_i915(dev);
4c6baa59 9250 u32 val, base, offset;
aeee5a49 9251 int pipe = crtc->pipe;
4c6baa59 9252 int fourcc, pixel_format;
6761dd31 9253 unsigned int aligned_height;
b113d5ee 9254 struct drm_framebuffer *fb;
1b842c89 9255 struct intel_framebuffer *intel_fb;
4c6baa59 9256
42a7b088
DL
9257 val = I915_READ(DSPCNTR(pipe));
9258 if (!(val & DISPLAY_PLANE_ENABLE))
9259 return;
9260
d9806c9f 9261 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
1b842c89 9262 if (!intel_fb) {
4c6baa59
JB
9263 DRM_DEBUG_KMS("failed to alloc fb\n");
9264 return;
9265 }
9266
1b842c89
DL
9267 fb = &intel_fb->base;
9268
18c5247e
DV
9269 if (INTEL_INFO(dev)->gen >= 4) {
9270 if (val & DISPPLANE_TILED) {
49af449b 9271 plane_config->tiling = I915_TILING_X;
18c5247e
DV
9272 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
9273 }
9274 }
4c6baa59
JB
9275
9276 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
b35d63fa 9277 fourcc = i9xx_format_to_fourcc(pixel_format);
b113d5ee
DL
9278 fb->pixel_format = fourcc;
9279 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
4c6baa59 9280
aeee5a49 9281 base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
4c6baa59 9282 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
aeee5a49 9283 offset = I915_READ(DSPOFFSET(pipe));
4c6baa59 9284 } else {
49af449b 9285 if (plane_config->tiling)
aeee5a49 9286 offset = I915_READ(DSPTILEOFF(pipe));
4c6baa59 9287 else
aeee5a49 9288 offset = I915_READ(DSPLINOFF(pipe));
4c6baa59
JB
9289 }
9290 plane_config->base = base;
9291
9292 val = I915_READ(PIPESRC(pipe));
b113d5ee
DL
9293 fb->width = ((val >> 16) & 0xfff) + 1;
9294 fb->height = ((val >> 0) & 0xfff) + 1;
4c6baa59
JB
9295
9296 val = I915_READ(DSPSTRIDE(pipe));
b113d5ee 9297 fb->pitches[0] = val & 0xffffffc0;
4c6baa59 9298
b113d5ee 9299 aligned_height = intel_fb_align_height(dev, fb->height,
091df6cb
DV
9300 fb->pixel_format,
9301 fb->modifier[0]);
4c6baa59 9302
f37b5c2b 9303 plane_config->size = fb->pitches[0] * aligned_height;
4c6baa59 9304
2844a921
DL
9305 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9306 pipe_name(pipe), fb->width, fb->height,
9307 fb->bits_per_pixel, base, fb->pitches[0],
9308 plane_config->size);
b113d5ee 9309
2d14030b 9310 plane_config->fb = intel_fb;
4c6baa59
JB
9311}
9312
0e8ffe1b 9313static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
5cec258b 9314 struct intel_crtc_state *pipe_config)
0e8ffe1b
DV
9315{
9316 struct drm_device *dev = crtc->base.dev;
fac5e23e 9317 struct drm_i915_private *dev_priv = to_i915(dev);
1729050e 9318 enum intel_display_power_domain power_domain;
0e8ffe1b 9319 uint32_t tmp;
1729050e 9320 bool ret;
0e8ffe1b 9321
1729050e
ID
9322 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
9323 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
930e8c9e
PZ
9324 return false;
9325
e143a21c 9326 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
8106ddbd 9327 pipe_config->shared_dpll = NULL;
eccb140b 9328
1729050e 9329 ret = false;
0e8ffe1b
DV
9330 tmp = I915_READ(PIPECONF(crtc->pipe));
9331 if (!(tmp & PIPECONF_ENABLE))
1729050e 9332 goto out;
0e8ffe1b 9333
42571aef
VS
9334 switch (tmp & PIPECONF_BPC_MASK) {
9335 case PIPECONF_6BPC:
9336 pipe_config->pipe_bpp = 18;
9337 break;
9338 case PIPECONF_8BPC:
9339 pipe_config->pipe_bpp = 24;
9340 break;
9341 case PIPECONF_10BPC:
9342 pipe_config->pipe_bpp = 30;
9343 break;
9344 case PIPECONF_12BPC:
9345 pipe_config->pipe_bpp = 36;
9346 break;
9347 default:
9348 break;
9349 }
9350
b5a9fa09
DV
9351 if (tmp & PIPECONF_COLOR_RANGE_SELECT)
9352 pipe_config->limited_color_range = true;
9353
ab9412ba 9354 if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
66e985c0 9355 struct intel_shared_dpll *pll;
8106ddbd 9356 enum intel_dpll_id pll_id;
66e985c0 9357
88adfff1
DV
9358 pipe_config->has_pch_encoder = true;
9359
627eb5a3
DV
9360 tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
9361 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
9362 FDI_DP_PORT_WIDTH_SHIFT) + 1;
72419203
DV
9363
9364 ironlake_get_fdi_m_n_config(crtc, pipe_config);
6c49f241 9365
2d1fe073 9366 if (HAS_PCH_IBX(dev_priv)) {
d9a7bc67
ID
9367 /*
9368 * The pipe->pch transcoder and pch transcoder->pll
9369 * mapping is fixed.
9370 */
8106ddbd 9371 pll_id = (enum intel_dpll_id) crtc->pipe;
c0d43d62
DV
9372 } else {
9373 tmp = I915_READ(PCH_DPLL_SEL);
9374 if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
8106ddbd 9375 pll_id = DPLL_ID_PCH_PLL_B;
c0d43d62 9376 else
8106ddbd 9377 pll_id= DPLL_ID_PCH_PLL_A;
c0d43d62 9378 }
66e985c0 9379
8106ddbd
ACO
9380 pipe_config->shared_dpll =
9381 intel_get_shared_dpll_by_id(dev_priv, pll_id);
9382 pll = pipe_config->shared_dpll;
66e985c0 9383
2edd6443
ACO
9384 WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
9385 &pipe_config->dpll_hw_state));
c93f54cf
DV
9386
9387 tmp = pipe_config->dpll_hw_state.dpll;
9388 pipe_config->pixel_multiplier =
9389 ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
9390 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
18442d08
VS
9391
9392 ironlake_pch_clock_get(crtc, pipe_config);
6c49f241
DV
9393 } else {
9394 pipe_config->pixel_multiplier = 1;
627eb5a3
DV
9395 }
9396
1bd1bd80 9397 intel_get_pipe_timings(crtc, pipe_config);
bc58be60 9398 intel_get_pipe_src_size(crtc, pipe_config);
1bd1bd80 9399
2fa2fe9a
DV
9400 ironlake_get_pfit_config(crtc, pipe_config);
9401
1729050e
ID
9402 ret = true;
9403
9404out:
9405 intel_display_power_put(dev_priv, power_domain);
9406
9407 return ret;
0e8ffe1b
DV
9408}
9409
be256dc7
PZ
9410static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
9411{
91c8a326 9412 struct drm_device *dev = &dev_priv->drm;
be256dc7 9413 struct intel_crtc *crtc;
be256dc7 9414
d3fcc808 9415 for_each_intel_crtc(dev, crtc)
e2c719b7 9416 I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
be256dc7
PZ
9417 pipe_name(crtc->pipe));
9418
e2c719b7
RC
9419 I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
9420 I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
01403de3
VS
9421 I915_STATE_WARN(I915_READ(WRPLL_CTL(0)) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
9422 I915_STATE_WARN(I915_READ(WRPLL_CTL(1)) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
e2c719b7
RC
9423 I915_STATE_WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
9424 I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
be256dc7 9425 "CPU PWM1 enabled\n");
c5107b87 9426 if (IS_HASWELL(dev))
e2c719b7 9427 I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
c5107b87 9428 "CPU PWM2 enabled\n");
e2c719b7 9429 I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
be256dc7 9430 "PCH PWM1 enabled\n");
e2c719b7 9431 I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
be256dc7 9432 "Utility pin enabled\n");
e2c719b7 9433 I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
be256dc7 9434
9926ada1
PZ
9435 /*
9436 * In theory we can still leave IRQs enabled, as long as only the HPD
9437 * interrupts remain enabled. We used to check for that, but since it's
9438 * gen-specific and since we only disable LCPLL after we fully disable
9439 * the interrupts, the check below should be enough.
9440 */
e2c719b7 9441 I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
be256dc7
PZ
9442}
9443
9ccd5aeb
PZ
9444static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
9445{
91c8a326 9446 struct drm_device *dev = &dev_priv->drm;
9ccd5aeb
PZ
9447
9448 if (IS_HASWELL(dev))
9449 return I915_READ(D_COMP_HSW);
9450 else
9451 return I915_READ(D_COMP_BDW);
9452}
9453
3c4c9b81
PZ
9454static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
9455{
91c8a326 9456 struct drm_device *dev = &dev_priv->drm;
3c4c9b81
PZ
9457
9458 if (IS_HASWELL(dev)) {
9459 mutex_lock(&dev_priv->rps.hw_lock);
9460 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
9461 val))
f475dadf 9462 DRM_ERROR("Failed to write to D_COMP\n");
3c4c9b81
PZ
9463 mutex_unlock(&dev_priv->rps.hw_lock);
9464 } else {
9ccd5aeb
PZ
9465 I915_WRITE(D_COMP_BDW, val);
9466 POSTING_READ(D_COMP_BDW);
3c4c9b81 9467 }
be256dc7
PZ
9468}
9469
9470/*
9471 * This function implements pieces of two sequences from BSpec:
9472 * - Sequence for display software to disable LCPLL
9473 * - Sequence for display software to allow package C8+
9474 * The steps implemented here are just the steps that actually touch the LCPLL
9475 * register. Callers should take care of disabling all the display engine
9476 * functions, doing the mode unset, fixing interrupts, etc.
9477 */
6ff58d53
PZ
9478static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
9479 bool switch_to_fclk, bool allow_power_down)
be256dc7
PZ
9480{
9481 uint32_t val;
9482
9483 assert_can_disable_lcpll(dev_priv);
9484
9485 val = I915_READ(LCPLL_CTL);
9486
9487 if (switch_to_fclk) {
9488 val |= LCPLL_CD_SOURCE_FCLK;
9489 I915_WRITE(LCPLL_CTL, val);
9490
f53dd63f
ID
9491 if (wait_for_us(I915_READ(LCPLL_CTL) &
9492 LCPLL_CD_SOURCE_FCLK_DONE, 1))
be256dc7
PZ
9493 DRM_ERROR("Switching to FCLK failed\n");
9494
9495 val = I915_READ(LCPLL_CTL);
9496 }
9497
9498 val |= LCPLL_PLL_DISABLE;
9499 I915_WRITE(LCPLL_CTL, val);
9500 POSTING_READ(LCPLL_CTL);
9501
24d8441d 9502 if (intel_wait_for_register(dev_priv, LCPLL_CTL, LCPLL_PLL_LOCK, 0, 1))
be256dc7
PZ
9503 DRM_ERROR("LCPLL still locked\n");
9504
9ccd5aeb 9505 val = hsw_read_dcomp(dev_priv);
be256dc7 9506 val |= D_COMP_COMP_DISABLE;
3c4c9b81 9507 hsw_write_dcomp(dev_priv, val);
be256dc7
PZ
9508 ndelay(100);
9509
9ccd5aeb
PZ
9510 if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
9511 1))
be256dc7
PZ
9512 DRM_ERROR("D_COMP RCOMP still in progress\n");
9513
9514 if (allow_power_down) {
9515 val = I915_READ(LCPLL_CTL);
9516 val |= LCPLL_POWER_DOWN_ALLOW;
9517 I915_WRITE(LCPLL_CTL, val);
9518 POSTING_READ(LCPLL_CTL);
9519 }
9520}
9521
9522/*
9523 * Fully restores LCPLL, disallowing power down and switching back to LCPLL
9524 * source.
9525 */
6ff58d53 9526static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
be256dc7
PZ
9527{
9528 uint32_t val;
9529
9530 val = I915_READ(LCPLL_CTL);
9531
9532 if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
9533 LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
9534 return;
9535
a8a8bd54
PZ
9536 /*
9537 * Make sure we're not on PC8 state before disabling PC8, otherwise
9538 * we'll hang the machine. To prevent PC8 state, just enable force_wake.
a8a8bd54 9539 */
59bad947 9540 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
215733fa 9541
be256dc7
PZ
9542 if (val & LCPLL_POWER_DOWN_ALLOW) {
9543 val &= ~LCPLL_POWER_DOWN_ALLOW;
9544 I915_WRITE(LCPLL_CTL, val);
35d8f2eb 9545 POSTING_READ(LCPLL_CTL);
be256dc7
PZ
9546 }
9547
9ccd5aeb 9548 val = hsw_read_dcomp(dev_priv);
be256dc7
PZ
9549 val |= D_COMP_COMP_FORCE;
9550 val &= ~D_COMP_COMP_DISABLE;
3c4c9b81 9551 hsw_write_dcomp(dev_priv, val);
be256dc7
PZ
9552
9553 val = I915_READ(LCPLL_CTL);
9554 val &= ~LCPLL_PLL_DISABLE;
9555 I915_WRITE(LCPLL_CTL, val);
9556
93220c08
CW
9557 if (intel_wait_for_register(dev_priv,
9558 LCPLL_CTL, LCPLL_PLL_LOCK, LCPLL_PLL_LOCK,
9559 5))
be256dc7
PZ
9560 DRM_ERROR("LCPLL not locked yet\n");
9561
9562 if (val & LCPLL_CD_SOURCE_FCLK) {
9563 val = I915_READ(LCPLL_CTL);
9564 val &= ~LCPLL_CD_SOURCE_FCLK;
9565 I915_WRITE(LCPLL_CTL, val);
9566
f53dd63f
ID
9567 if (wait_for_us((I915_READ(LCPLL_CTL) &
9568 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
be256dc7
PZ
9569 DRM_ERROR("Switching back to LCPLL failed\n");
9570 }
215733fa 9571
59bad947 9572 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
91c8a326 9573 intel_update_cdclk(&dev_priv->drm);
be256dc7
PZ
9574}
9575
765dab67
PZ
9576/*
9577 * Package states C8 and deeper are really deep PC states that can only be
9578 * reached when all the devices on the system allow it, so even if the graphics
9579 * device allows PC8+, it doesn't mean the system will actually get to these
9580 * states. Our driver only allows PC8+ when going into runtime PM.
9581 *
9582 * The requirements for PC8+ are that all the outputs are disabled, the power
9583 * well is disabled and most interrupts are disabled, and these are also
9584 * requirements for runtime PM. When these conditions are met, we manually do
9585 * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
9586 * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
9587 * hang the machine.
9588 *
9589 * When we really reach PC8 or deeper states (not just when we allow it) we lose
9590 * the state of some registers, so when we come back from PC8+ we need to
9591 * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
9592 * need to take care of the registers kept by RC6. Notice that this happens even
9593 * if we don't put the device in PCI D3 state (which is what currently happens
9594 * because of the runtime PM support).
9595 *
9596 * For more, read "Display Sequences for Package C8" on the hardware
9597 * documentation.
9598 */
a14cb6fc 9599void hsw_enable_pc8(struct drm_i915_private *dev_priv)
c67a470b 9600{
91c8a326 9601 struct drm_device *dev = &dev_priv->drm;
c67a470b
PZ
9602 uint32_t val;
9603
c67a470b
PZ
9604 DRM_DEBUG_KMS("Enabling package C8+\n");
9605
c2699524 9606 if (HAS_PCH_LPT_LP(dev)) {
c67a470b
PZ
9607 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9608 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
9609 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9610 }
9611
9612 lpt_disable_clkout_dp(dev);
c67a470b
PZ
9613 hsw_disable_lcpll(dev_priv, true, true);
9614}
9615
a14cb6fc 9616void hsw_disable_pc8(struct drm_i915_private *dev_priv)
c67a470b 9617{
91c8a326 9618 struct drm_device *dev = &dev_priv->drm;
c67a470b
PZ
9619 uint32_t val;
9620
c67a470b
PZ
9621 DRM_DEBUG_KMS("Disabling package C8+\n");
9622
9623 hsw_restore_lcpll(dev_priv);
c67a470b
PZ
9624 lpt_init_pch_refclk(dev);
9625
c2699524 9626 if (HAS_PCH_LPT_LP(dev)) {
c67a470b
PZ
9627 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9628 val |= PCH_LP_PARTITION_LEVEL_DISABLE;
9629 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9630 }
c67a470b
PZ
9631}
9632
324513c0 9633static void bxt_modeset_commit_cdclk(struct drm_atomic_state *old_state)
f8437dd1 9634{
a821fc46 9635 struct drm_device *dev = old_state->dev;
1a617b77
ML
9636 struct intel_atomic_state *old_intel_state =
9637 to_intel_atomic_state(old_state);
9638 unsigned int req_cdclk = old_intel_state->dev_cdclk;
f8437dd1 9639
324513c0 9640 bxt_set_cdclk(to_i915(dev), req_cdclk);
f8437dd1
VK
9641}
9642
b432e5cf 9643/* compute the max rate for new configuration */
27c329ed 9644static int ilk_max_pixel_rate(struct drm_atomic_state *state)
b432e5cf 9645{
565602d7 9646 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
fac5e23e 9647 struct drm_i915_private *dev_priv = to_i915(state->dev);
565602d7
ML
9648 struct drm_crtc *crtc;
9649 struct drm_crtc_state *cstate;
27c329ed 9650 struct intel_crtc_state *crtc_state;
565602d7
ML
9651 unsigned max_pixel_rate = 0, i;
9652 enum pipe pipe;
b432e5cf 9653
565602d7
ML
9654 memcpy(intel_state->min_pixclk, dev_priv->min_pixclk,
9655 sizeof(intel_state->min_pixclk));
27c329ed 9656
565602d7
ML
9657 for_each_crtc_in_state(state, crtc, cstate, i) {
9658 int pixel_rate;
27c329ed 9659
565602d7
ML
9660 crtc_state = to_intel_crtc_state(cstate);
9661 if (!crtc_state->base.enable) {
9662 intel_state->min_pixclk[i] = 0;
b432e5cf 9663 continue;
565602d7 9664 }
b432e5cf 9665
27c329ed 9666 pixel_rate = ilk_pipe_pixel_rate(crtc_state);
b432e5cf
VS
9667
9668 /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
565602d7 9669 if (IS_BROADWELL(dev_priv) && crtc_state->ips_enabled)
b432e5cf
VS
9670 pixel_rate = DIV_ROUND_UP(pixel_rate * 100, 95);
9671
565602d7 9672 intel_state->min_pixclk[i] = pixel_rate;
b432e5cf
VS
9673 }
9674
565602d7
ML
9675 for_each_pipe(dev_priv, pipe)
9676 max_pixel_rate = max(intel_state->min_pixclk[pipe], max_pixel_rate);
9677
b432e5cf
VS
9678 return max_pixel_rate;
9679}
9680
9681static void broadwell_set_cdclk(struct drm_device *dev, int cdclk)
9682{
fac5e23e 9683 struct drm_i915_private *dev_priv = to_i915(dev);
b432e5cf
VS
9684 uint32_t val, data;
9685 int ret;
9686
9687 if (WARN((I915_READ(LCPLL_CTL) &
9688 (LCPLL_PLL_DISABLE | LCPLL_PLL_LOCK |
9689 LCPLL_CD_CLOCK_DISABLE | LCPLL_ROOT_CD_CLOCK_DISABLE |
9690 LCPLL_CD2X_CLOCK_DISABLE | LCPLL_POWER_DOWN_ALLOW |
9691 LCPLL_CD_SOURCE_FCLK)) != LCPLL_PLL_LOCK,
9692 "trying to change cdclk frequency with cdclk not enabled\n"))
9693 return;
9694
9695 mutex_lock(&dev_priv->rps.hw_lock);
9696 ret = sandybridge_pcode_write(dev_priv,
9697 BDW_PCODE_DISPLAY_FREQ_CHANGE_REQ, 0x0);
9698 mutex_unlock(&dev_priv->rps.hw_lock);
9699 if (ret) {
9700 DRM_ERROR("failed to inform pcode about cdclk change\n");
9701 return;
9702 }
9703
9704 val = I915_READ(LCPLL_CTL);
9705 val |= LCPLL_CD_SOURCE_FCLK;
9706 I915_WRITE(LCPLL_CTL, val);
9707
5ba00178
TU
9708 if (wait_for_us(I915_READ(LCPLL_CTL) &
9709 LCPLL_CD_SOURCE_FCLK_DONE, 1))
b432e5cf
VS
9710 DRM_ERROR("Switching to FCLK failed\n");
9711
9712 val = I915_READ(LCPLL_CTL);
9713 val &= ~LCPLL_CLK_FREQ_MASK;
9714
9715 switch (cdclk) {
9716 case 450000:
9717 val |= LCPLL_CLK_FREQ_450;
9718 data = 0;
9719 break;
9720 case 540000:
9721 val |= LCPLL_CLK_FREQ_54O_BDW;
9722 data = 1;
9723 break;
9724 case 337500:
9725 val |= LCPLL_CLK_FREQ_337_5_BDW;
9726 data = 2;
9727 break;
9728 case 675000:
9729 val |= LCPLL_CLK_FREQ_675_BDW;
9730 data = 3;
9731 break;
9732 default:
9733 WARN(1, "invalid cdclk frequency\n");
9734 return;
9735 }
9736
9737 I915_WRITE(LCPLL_CTL, val);
9738
9739 val = I915_READ(LCPLL_CTL);
9740 val &= ~LCPLL_CD_SOURCE_FCLK;
9741 I915_WRITE(LCPLL_CTL, val);
9742
5ba00178
TU
9743 if (wait_for_us((I915_READ(LCPLL_CTL) &
9744 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
b432e5cf
VS
9745 DRM_ERROR("Switching back to LCPLL failed\n");
9746
9747 mutex_lock(&dev_priv->rps.hw_lock);
9748 sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ, data);
9749 mutex_unlock(&dev_priv->rps.hw_lock);
9750
7f1052a8
VS
9751 I915_WRITE(CDCLK_FREQ, DIV_ROUND_CLOSEST(cdclk, 1000) - 1);
9752
b432e5cf
VS
9753 intel_update_cdclk(dev);
9754
9755 WARN(cdclk != dev_priv->cdclk_freq,
9756 "cdclk requested %d kHz but got %d kHz\n",
9757 cdclk, dev_priv->cdclk_freq);
9758}
9759
587c7914
VS
9760static int broadwell_calc_cdclk(int max_pixclk)
9761{
9762 if (max_pixclk > 540000)
9763 return 675000;
9764 else if (max_pixclk > 450000)
9765 return 540000;
9766 else if (max_pixclk > 337500)
9767 return 450000;
9768 else
9769 return 337500;
9770}
9771
27c329ed 9772static int broadwell_modeset_calc_cdclk(struct drm_atomic_state *state)
b432e5cf 9773{
27c329ed 9774 struct drm_i915_private *dev_priv = to_i915(state->dev);
1a617b77 9775 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
27c329ed 9776 int max_pixclk = ilk_max_pixel_rate(state);
b432e5cf
VS
9777 int cdclk;
9778
9779 /*
9780 * FIXME should also account for plane ratio
9781 * once 64bpp pixel formats are supported.
9782 */
587c7914 9783 cdclk = broadwell_calc_cdclk(max_pixclk);
b432e5cf 9784
b432e5cf 9785 if (cdclk > dev_priv->max_cdclk_freq) {
63ba534e
ML
9786 DRM_DEBUG_KMS("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
9787 cdclk, dev_priv->max_cdclk_freq);
9788 return -EINVAL;
b432e5cf
VS
9789 }
9790
1a617b77
ML
9791 intel_state->cdclk = intel_state->dev_cdclk = cdclk;
9792 if (!intel_state->active_crtcs)
587c7914 9793 intel_state->dev_cdclk = broadwell_calc_cdclk(0);
b432e5cf
VS
9794
9795 return 0;
9796}
9797
27c329ed 9798static void broadwell_modeset_commit_cdclk(struct drm_atomic_state *old_state)
b432e5cf 9799{
27c329ed 9800 struct drm_device *dev = old_state->dev;
1a617b77
ML
9801 struct intel_atomic_state *old_intel_state =
9802 to_intel_atomic_state(old_state);
9803 unsigned req_cdclk = old_intel_state->dev_cdclk;
b432e5cf 9804
27c329ed 9805 broadwell_set_cdclk(dev, req_cdclk);
b432e5cf
VS
9806}
9807
c89e39f3
CT
9808static int skl_modeset_calc_cdclk(struct drm_atomic_state *state)
9809{
9810 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
9811 struct drm_i915_private *dev_priv = to_i915(state->dev);
9812 const int max_pixclk = ilk_max_pixel_rate(state);
a8ca4934 9813 int vco = intel_state->cdclk_pll_vco;
c89e39f3
CT
9814 int cdclk;
9815
9816 /*
9817 * FIXME should also account for plane ratio
9818 * once 64bpp pixel formats are supported.
9819 */
a8ca4934 9820 cdclk = skl_calc_cdclk(max_pixclk, vco);
c89e39f3
CT
9821
9822 /*
9823 * FIXME move the cdclk caclulation to
9824 * compute_config() so we can fail gracegully.
9825 */
9826 if (cdclk > dev_priv->max_cdclk_freq) {
9827 DRM_ERROR("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
9828 cdclk, dev_priv->max_cdclk_freq);
9829 cdclk = dev_priv->max_cdclk_freq;
9830 }
9831
9832 intel_state->cdclk = intel_state->dev_cdclk = cdclk;
9833 if (!intel_state->active_crtcs)
a8ca4934 9834 intel_state->dev_cdclk = skl_calc_cdclk(0, vco);
c89e39f3
CT
9835
9836 return 0;
9837}
9838
9839static void skl_modeset_commit_cdclk(struct drm_atomic_state *old_state)
9840{
1cd593e0
VS
9841 struct drm_i915_private *dev_priv = to_i915(old_state->dev);
9842 struct intel_atomic_state *intel_state = to_intel_atomic_state(old_state);
9843 unsigned int req_cdclk = intel_state->dev_cdclk;
9844 unsigned int req_vco = intel_state->cdclk_pll_vco;
c89e39f3 9845
1cd593e0 9846 skl_set_cdclk(dev_priv, req_cdclk, req_vco);
c89e39f3
CT
9847}
9848
190f68c5
ACO
9849static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
9850 struct intel_crtc_state *crtc_state)
09b4ddf9 9851{
d7edc4e5 9852 if (!intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DSI)) {
af3997b5
MK
9853 if (!intel_ddi_pll_select(crtc, crtc_state))
9854 return -EINVAL;
9855 }
716c2e55 9856
c7653199 9857 crtc->lowfreq_avail = false;
644cef34 9858
c8f7a0db 9859 return 0;
79e53945
JB
9860}
9861
3760b59c
S
9862static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
9863 enum port port,
9864 struct intel_crtc_state *pipe_config)
9865{
8106ddbd
ACO
9866 enum intel_dpll_id id;
9867
3760b59c
S
9868 switch (port) {
9869 case PORT_A:
9870 pipe_config->ddi_pll_sel = SKL_DPLL0;
08250c4b 9871 id = DPLL_ID_SKL_DPLL0;
3760b59c
S
9872 break;
9873 case PORT_B:
9874 pipe_config->ddi_pll_sel = SKL_DPLL1;
08250c4b 9875 id = DPLL_ID_SKL_DPLL1;
3760b59c
S
9876 break;
9877 case PORT_C:
9878 pipe_config->ddi_pll_sel = SKL_DPLL2;
08250c4b 9879 id = DPLL_ID_SKL_DPLL2;
3760b59c
S
9880 break;
9881 default:
9882 DRM_ERROR("Incorrect port type\n");
8106ddbd 9883 return;
3760b59c 9884 }
8106ddbd
ACO
9885
9886 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
3760b59c
S
9887}
9888
96b7dfb7
S
9889static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
9890 enum port port,
5cec258b 9891 struct intel_crtc_state *pipe_config)
96b7dfb7 9892{
8106ddbd 9893 enum intel_dpll_id id;
a3c988ea 9894 u32 temp;
96b7dfb7
S
9895
9896 temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
9897 pipe_config->ddi_pll_sel = temp >> (port * 3 + 1);
9898
9899 switch (pipe_config->ddi_pll_sel) {
3148ade7 9900 case SKL_DPLL0:
a3c988ea
ACO
9901 id = DPLL_ID_SKL_DPLL0;
9902 break;
96b7dfb7 9903 case SKL_DPLL1:
8106ddbd 9904 id = DPLL_ID_SKL_DPLL1;
96b7dfb7
S
9905 break;
9906 case SKL_DPLL2:
8106ddbd 9907 id = DPLL_ID_SKL_DPLL2;
96b7dfb7
S
9908 break;
9909 case SKL_DPLL3:
8106ddbd 9910 id = DPLL_ID_SKL_DPLL3;
96b7dfb7 9911 break;
8106ddbd
ACO
9912 default:
9913 MISSING_CASE(pipe_config->ddi_pll_sel);
9914 return;
96b7dfb7 9915 }
8106ddbd
ACO
9916
9917 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
96b7dfb7
S
9918}
9919
7d2c8175
DL
9920static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
9921 enum port port,
5cec258b 9922 struct intel_crtc_state *pipe_config)
7d2c8175 9923{
8106ddbd
ACO
9924 enum intel_dpll_id id;
9925
7d2c8175
DL
9926 pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
9927
9928 switch (pipe_config->ddi_pll_sel) {
9929 case PORT_CLK_SEL_WRPLL1:
8106ddbd 9930 id = DPLL_ID_WRPLL1;
7d2c8175
DL
9931 break;
9932 case PORT_CLK_SEL_WRPLL2:
8106ddbd 9933 id = DPLL_ID_WRPLL2;
7d2c8175 9934 break;
00490c22 9935 case PORT_CLK_SEL_SPLL:
8106ddbd 9936 id = DPLL_ID_SPLL;
79bd23da 9937 break;
9d16da65
ACO
9938 case PORT_CLK_SEL_LCPLL_810:
9939 id = DPLL_ID_LCPLL_810;
9940 break;
9941 case PORT_CLK_SEL_LCPLL_1350:
9942 id = DPLL_ID_LCPLL_1350;
9943 break;
9944 case PORT_CLK_SEL_LCPLL_2700:
9945 id = DPLL_ID_LCPLL_2700;
9946 break;
8106ddbd
ACO
9947 default:
9948 MISSING_CASE(pipe_config->ddi_pll_sel);
9949 /* fall through */
9950 case PORT_CLK_SEL_NONE:
8106ddbd 9951 return;
7d2c8175 9952 }
8106ddbd
ACO
9953
9954 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
7d2c8175
DL
9955}
9956
cf30429e
JN
9957static bool hsw_get_transcoder_state(struct intel_crtc *crtc,
9958 struct intel_crtc_state *pipe_config,
9959 unsigned long *power_domain_mask)
9960{
9961 struct drm_device *dev = crtc->base.dev;
fac5e23e 9962 struct drm_i915_private *dev_priv = to_i915(dev);
cf30429e
JN
9963 enum intel_display_power_domain power_domain;
9964 u32 tmp;
9965
d9a7bc67
ID
9966 /*
9967 * The pipe->transcoder mapping is fixed with the exception of the eDP
9968 * transcoder handled below.
9969 */
cf30429e
JN
9970 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
9971
9972 /*
9973 * XXX: Do intel_display_power_get_if_enabled before reading this (for
9974 * consistency and less surprising code; it's in always on power).
9975 */
9976 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
9977 if (tmp & TRANS_DDI_FUNC_ENABLE) {
9978 enum pipe trans_edp_pipe;
9979 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
9980 default:
9981 WARN(1, "unknown pipe linked to edp transcoder\n");
9982 case TRANS_DDI_EDP_INPUT_A_ONOFF:
9983 case TRANS_DDI_EDP_INPUT_A_ON:
9984 trans_edp_pipe = PIPE_A;
9985 break;
9986 case TRANS_DDI_EDP_INPUT_B_ONOFF:
9987 trans_edp_pipe = PIPE_B;
9988 break;
9989 case TRANS_DDI_EDP_INPUT_C_ONOFF:
9990 trans_edp_pipe = PIPE_C;
9991 break;
9992 }
9993
9994 if (trans_edp_pipe == crtc->pipe)
9995 pipe_config->cpu_transcoder = TRANSCODER_EDP;
9996 }
9997
9998 power_domain = POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder);
9999 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
10000 return false;
10001 *power_domain_mask |= BIT(power_domain);
10002
10003 tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
10004
10005 return tmp & PIPECONF_ENABLE;
10006}
10007
4d1de975
JN
10008static bool bxt_get_dsi_transcoder_state(struct intel_crtc *crtc,
10009 struct intel_crtc_state *pipe_config,
10010 unsigned long *power_domain_mask)
10011{
10012 struct drm_device *dev = crtc->base.dev;
fac5e23e 10013 struct drm_i915_private *dev_priv = to_i915(dev);
4d1de975
JN
10014 enum intel_display_power_domain power_domain;
10015 enum port port;
10016 enum transcoder cpu_transcoder;
10017 u32 tmp;
10018
4d1de975
JN
10019 for_each_port_masked(port, BIT(PORT_A) | BIT(PORT_C)) {
10020 if (port == PORT_A)
10021 cpu_transcoder = TRANSCODER_DSI_A;
10022 else
10023 cpu_transcoder = TRANSCODER_DSI_C;
10024
10025 power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
10026 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
10027 continue;
10028 *power_domain_mask |= BIT(power_domain);
10029
db18b6a6
ID
10030 /*
10031 * The PLL needs to be enabled with a valid divider
10032 * configuration, otherwise accessing DSI registers will hang
10033 * the machine. See BSpec North Display Engine
10034 * registers/MIPI[BXT]. We can break out here early, since we
10035 * need the same DSI PLL to be enabled for both DSI ports.
10036 */
10037 if (!intel_dsi_pll_is_enabled(dev_priv))
10038 break;
10039
4d1de975
JN
10040 /* XXX: this works for video mode only */
10041 tmp = I915_READ(BXT_MIPI_PORT_CTRL(port));
10042 if (!(tmp & DPI_ENABLE))
10043 continue;
10044
10045 tmp = I915_READ(MIPI_CTRL(port));
10046 if ((tmp & BXT_PIPE_SELECT_MASK) != BXT_PIPE_SELECT(crtc->pipe))
10047 continue;
10048
10049 pipe_config->cpu_transcoder = cpu_transcoder;
4d1de975
JN
10050 break;
10051 }
10052
d7edc4e5 10053 return transcoder_is_dsi(pipe_config->cpu_transcoder);
4d1de975
JN
10054}
10055
26804afd 10056static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
5cec258b 10057 struct intel_crtc_state *pipe_config)
26804afd
DV
10058{
10059 struct drm_device *dev = crtc->base.dev;
fac5e23e 10060 struct drm_i915_private *dev_priv = to_i915(dev);
d452c5b6 10061 struct intel_shared_dpll *pll;
26804afd
DV
10062 enum port port;
10063 uint32_t tmp;
10064
10065 tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
10066
10067 port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
10068
ef11bdb3 10069 if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev))
96b7dfb7 10070 skylake_get_ddi_pll(dev_priv, port, pipe_config);
3760b59c
S
10071 else if (IS_BROXTON(dev))
10072 bxt_get_ddi_pll(dev_priv, port, pipe_config);
96b7dfb7
S
10073 else
10074 haswell_get_ddi_pll(dev_priv, port, pipe_config);
9cd86933 10075
8106ddbd
ACO
10076 pll = pipe_config->shared_dpll;
10077 if (pll) {
2edd6443
ACO
10078 WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
10079 &pipe_config->dpll_hw_state));
d452c5b6
DV
10080 }
10081
26804afd
DV
10082 /*
10083 * Haswell has only FDI/PCH transcoder A. It is which is connected to
10084 * DDI E. So just check whether this pipe is wired to DDI E and whether
10085 * the PCH transcoder is on.
10086 */
ca370455
DL
10087 if (INTEL_INFO(dev)->gen < 9 &&
10088 (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
26804afd
DV
10089 pipe_config->has_pch_encoder = true;
10090
10091 tmp = I915_READ(FDI_RX_CTL(PIPE_A));
10092 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
10093 FDI_DP_PORT_WIDTH_SHIFT) + 1;
10094
10095 ironlake_get_fdi_m_n_config(crtc, pipe_config);
10096 }
10097}
10098
0e8ffe1b 10099static bool haswell_get_pipe_config(struct intel_crtc *crtc,
5cec258b 10100 struct intel_crtc_state *pipe_config)
0e8ffe1b
DV
10101{
10102 struct drm_device *dev = crtc->base.dev;
fac5e23e 10103 struct drm_i915_private *dev_priv = to_i915(dev);
1729050e
ID
10104 enum intel_display_power_domain power_domain;
10105 unsigned long power_domain_mask;
cf30429e 10106 bool active;
0e8ffe1b 10107
1729050e
ID
10108 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
10109 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
b5482bd0 10110 return false;
1729050e
ID
10111 power_domain_mask = BIT(power_domain);
10112
8106ddbd 10113 pipe_config->shared_dpll = NULL;
c0d43d62 10114
cf30429e 10115 active = hsw_get_transcoder_state(crtc, pipe_config, &power_domain_mask);
eccb140b 10116
d7edc4e5
VS
10117 if (IS_BROXTON(dev_priv) &&
10118 bxt_get_dsi_transcoder_state(crtc, pipe_config, &power_domain_mask)) {
10119 WARN_ON(active);
10120 active = true;
4d1de975
JN
10121 }
10122
cf30429e 10123 if (!active)
1729050e 10124 goto out;
0e8ffe1b 10125
d7edc4e5 10126 if (!transcoder_is_dsi(pipe_config->cpu_transcoder)) {
4d1de975
JN
10127 haswell_get_ddi_port_state(crtc, pipe_config);
10128 intel_get_pipe_timings(crtc, pipe_config);
10129 }
627eb5a3 10130
bc58be60 10131 intel_get_pipe_src_size(crtc, pipe_config);
1bd1bd80 10132
05dc698c
LL
10133 pipe_config->gamma_mode =
10134 I915_READ(GAMMA_MODE(crtc->pipe)) & GAMMA_MODE_MODE_MASK;
10135
a1b2278e
CK
10136 if (INTEL_INFO(dev)->gen >= 9) {
10137 skl_init_scalers(dev, crtc, pipe_config);
10138 }
10139
af99ceda
CK
10140 if (INTEL_INFO(dev)->gen >= 9) {
10141 pipe_config->scaler_state.scaler_id = -1;
10142 pipe_config->scaler_state.scaler_users &= ~(1 << SKL_CRTC_INDEX);
10143 }
10144
1729050e
ID
10145 power_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
10146 if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
10147 power_domain_mask |= BIT(power_domain);
1c132b44 10148 if (INTEL_INFO(dev)->gen >= 9)
bd2e244f 10149 skylake_get_pfit_config(crtc, pipe_config);
ff6d9f55 10150 else
1c132b44 10151 ironlake_get_pfit_config(crtc, pipe_config);
bd2e244f 10152 }
88adfff1 10153
e59150dc
JB
10154 if (IS_HASWELL(dev))
10155 pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
10156 (I915_READ(IPS_CTL) & IPS_ENABLE);
42db64ef 10157
4d1de975
JN
10158 if (pipe_config->cpu_transcoder != TRANSCODER_EDP &&
10159 !transcoder_is_dsi(pipe_config->cpu_transcoder)) {
ebb69c95
CT
10160 pipe_config->pixel_multiplier =
10161 I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
10162 } else {
10163 pipe_config->pixel_multiplier = 1;
10164 }
6c49f241 10165
1729050e
ID
10166out:
10167 for_each_power_domain(power_domain, power_domain_mask)
10168 intel_display_power_put(dev_priv, power_domain);
10169
cf30429e 10170 return active;
0e8ffe1b
DV
10171}
10172
55a08b3f
ML
10173static void i845_update_cursor(struct drm_crtc *crtc, u32 base,
10174 const struct intel_plane_state *plane_state)
560b85bb
CW
10175{
10176 struct drm_device *dev = crtc->dev;
fac5e23e 10177 struct drm_i915_private *dev_priv = to_i915(dev);
560b85bb 10178 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
dc41c154 10179 uint32_t cntl = 0, size = 0;
560b85bb 10180
55a08b3f
ML
10181 if (plane_state && plane_state->visible) {
10182 unsigned int width = plane_state->base.crtc_w;
10183 unsigned int height = plane_state->base.crtc_h;
dc41c154
VS
10184 unsigned int stride = roundup_pow_of_two(width) * 4;
10185
10186 switch (stride) {
10187 default:
10188 WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
10189 width, stride);
10190 stride = 256;
10191 /* fallthrough */
10192 case 256:
10193 case 512:
10194 case 1024:
10195 case 2048:
10196 break;
4b0e333e
CW
10197 }
10198
dc41c154
VS
10199 cntl |= CURSOR_ENABLE |
10200 CURSOR_GAMMA_ENABLE |
10201 CURSOR_FORMAT_ARGB |
10202 CURSOR_STRIDE(stride);
10203
10204 size = (height << 12) | width;
4b0e333e 10205 }
560b85bb 10206
dc41c154
VS
10207 if (intel_crtc->cursor_cntl != 0 &&
10208 (intel_crtc->cursor_base != base ||
10209 intel_crtc->cursor_size != size ||
10210 intel_crtc->cursor_cntl != cntl)) {
10211 /* On these chipsets we can only modify the base/size/stride
10212 * whilst the cursor is disabled.
10213 */
0b87c24e
VS
10214 I915_WRITE(CURCNTR(PIPE_A), 0);
10215 POSTING_READ(CURCNTR(PIPE_A));
dc41c154 10216 intel_crtc->cursor_cntl = 0;
4b0e333e 10217 }
560b85bb 10218
99d1f387 10219 if (intel_crtc->cursor_base != base) {
0b87c24e 10220 I915_WRITE(CURBASE(PIPE_A), base);
99d1f387
VS
10221 intel_crtc->cursor_base = base;
10222 }
4726e0b0 10223
dc41c154
VS
10224 if (intel_crtc->cursor_size != size) {
10225 I915_WRITE(CURSIZE, size);
10226 intel_crtc->cursor_size = size;
4b0e333e 10227 }
560b85bb 10228
4b0e333e 10229 if (intel_crtc->cursor_cntl != cntl) {
0b87c24e
VS
10230 I915_WRITE(CURCNTR(PIPE_A), cntl);
10231 POSTING_READ(CURCNTR(PIPE_A));
4b0e333e 10232 intel_crtc->cursor_cntl = cntl;
560b85bb 10233 }
560b85bb
CW
10234}
10235
55a08b3f
ML
10236static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base,
10237 const struct intel_plane_state *plane_state)
65a21cd6
JB
10238{
10239 struct drm_device *dev = crtc->dev;
fac5e23e 10240 struct drm_i915_private *dev_priv = to_i915(dev);
65a21cd6
JB
10241 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10242 int pipe = intel_crtc->pipe;
663f3122 10243 uint32_t cntl = 0;
4b0e333e 10244
55a08b3f 10245 if (plane_state && plane_state->visible) {
4b0e333e 10246 cntl = MCURSOR_GAMMA_ENABLE;
55a08b3f 10247 switch (plane_state->base.crtc_w) {
4726e0b0
SK
10248 case 64:
10249 cntl |= CURSOR_MODE_64_ARGB_AX;
10250 break;
10251 case 128:
10252 cntl |= CURSOR_MODE_128_ARGB_AX;
10253 break;
10254 case 256:
10255 cntl |= CURSOR_MODE_256_ARGB_AX;
10256 break;
10257 default:
55a08b3f 10258 MISSING_CASE(plane_state->base.crtc_w);
4726e0b0 10259 return;
65a21cd6 10260 }
4b0e333e 10261 cntl |= pipe << 28; /* Connect to correct pipe */
47bf17a7 10262
fc6f93bc 10263 if (HAS_DDI(dev))
47bf17a7 10264 cntl |= CURSOR_PIPE_CSC_ENABLE;
65a21cd6 10265
55a08b3f
ML
10266 if (plane_state->base.rotation == BIT(DRM_ROTATE_180))
10267 cntl |= CURSOR_ROTATE_180;
10268 }
4398ad45 10269
4b0e333e
CW
10270 if (intel_crtc->cursor_cntl != cntl) {
10271 I915_WRITE(CURCNTR(pipe), cntl);
10272 POSTING_READ(CURCNTR(pipe));
10273 intel_crtc->cursor_cntl = cntl;
65a21cd6 10274 }
4b0e333e 10275
65a21cd6 10276 /* and commit changes on next vblank */
5efb3e28
VS
10277 I915_WRITE(CURBASE(pipe), base);
10278 POSTING_READ(CURBASE(pipe));
99d1f387
VS
10279
10280 intel_crtc->cursor_base = base;
65a21cd6
JB
10281}
10282
cda4b7d3 10283/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
6b383a7f 10284static void intel_crtc_update_cursor(struct drm_crtc *crtc,
55a08b3f 10285 const struct intel_plane_state *plane_state)
cda4b7d3
CW
10286{
10287 struct drm_device *dev = crtc->dev;
fac5e23e 10288 struct drm_i915_private *dev_priv = to_i915(dev);
cda4b7d3
CW
10289 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10290 int pipe = intel_crtc->pipe;
55a08b3f
ML
10291 u32 base = intel_crtc->cursor_addr;
10292 u32 pos = 0;
cda4b7d3 10293
55a08b3f
ML
10294 if (plane_state) {
10295 int x = plane_state->base.crtc_x;
10296 int y = plane_state->base.crtc_y;
cda4b7d3 10297
55a08b3f
ML
10298 if (x < 0) {
10299 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
10300 x = -x;
10301 }
10302 pos |= x << CURSOR_X_SHIFT;
cda4b7d3 10303
55a08b3f
ML
10304 if (y < 0) {
10305 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
10306 y = -y;
10307 }
10308 pos |= y << CURSOR_Y_SHIFT;
10309
10310 /* ILK+ do this automagically */
10311 if (HAS_GMCH_DISPLAY(dev) &&
10312 plane_state->base.rotation == BIT(DRM_ROTATE_180)) {
10313 base += (plane_state->base.crtc_h *
10314 plane_state->base.crtc_w - 1) * 4;
10315 }
cda4b7d3 10316 }
cda4b7d3 10317
5efb3e28
VS
10318 I915_WRITE(CURPOS(pipe), pos);
10319
8ac54669 10320 if (IS_845G(dev) || IS_I865G(dev))
55a08b3f 10321 i845_update_cursor(crtc, base, plane_state);
5efb3e28 10322 else
55a08b3f 10323 i9xx_update_cursor(crtc, base, plane_state);
cda4b7d3
CW
10324}
10325
dc41c154
VS
10326static bool cursor_size_ok(struct drm_device *dev,
10327 uint32_t width, uint32_t height)
10328{
10329 if (width == 0 || height == 0)
10330 return false;
10331
10332 /*
10333 * 845g/865g are special in that they are only limited by
10334 * the width of their cursors, the height is arbitrary up to
10335 * the precision of the register. Everything else requires
10336 * square cursors, limited to a few power-of-two sizes.
10337 */
10338 if (IS_845G(dev) || IS_I865G(dev)) {
10339 if ((width & 63) != 0)
10340 return false;
10341
10342 if (width > (IS_845G(dev) ? 64 : 512))
10343 return false;
10344
10345 if (height > 1023)
10346 return false;
10347 } else {
10348 switch (width | height) {
10349 case 256:
10350 case 128:
10351 if (IS_GEN2(dev))
10352 return false;
10353 case 64:
10354 break;
10355 default:
10356 return false;
10357 }
10358 }
10359
10360 return true;
10361}
10362
79e53945
JB
10363/* VESA 640x480x72Hz mode to set on the pipe */
10364static struct drm_display_mode load_detect_mode = {
10365 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
10366 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
10367};
10368
a8bb6818
DV
10369struct drm_framebuffer *
10370__intel_framebuffer_create(struct drm_device *dev,
10371 struct drm_mode_fb_cmd2 *mode_cmd,
10372 struct drm_i915_gem_object *obj)
d2dff872
CW
10373{
10374 struct intel_framebuffer *intel_fb;
10375 int ret;
10376
10377 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
dcb1394e 10378 if (!intel_fb)
d2dff872 10379 return ERR_PTR(-ENOMEM);
d2dff872
CW
10380
10381 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
dd4916c5
DV
10382 if (ret)
10383 goto err;
d2dff872
CW
10384
10385 return &intel_fb->base;
dcb1394e 10386
dd4916c5 10387err:
dd4916c5 10388 kfree(intel_fb);
dd4916c5 10389 return ERR_PTR(ret);
d2dff872
CW
10390}
10391
b5ea642a 10392static struct drm_framebuffer *
a8bb6818
DV
10393intel_framebuffer_create(struct drm_device *dev,
10394 struct drm_mode_fb_cmd2 *mode_cmd,
10395 struct drm_i915_gem_object *obj)
10396{
10397 struct drm_framebuffer *fb;
10398 int ret;
10399
10400 ret = i915_mutex_lock_interruptible(dev);
10401 if (ret)
10402 return ERR_PTR(ret);
10403 fb = __intel_framebuffer_create(dev, mode_cmd, obj);
10404 mutex_unlock(&dev->struct_mutex);
10405
10406 return fb;
10407}
10408
d2dff872
CW
10409static u32
10410intel_framebuffer_pitch_for_width(int width, int bpp)
10411{
10412 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
10413 return ALIGN(pitch, 64);
10414}
10415
10416static u32
10417intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
10418{
10419 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
1267a26b 10420 return PAGE_ALIGN(pitch * mode->vdisplay);
d2dff872
CW
10421}
10422
10423static struct drm_framebuffer *
10424intel_framebuffer_create_for_mode(struct drm_device *dev,
10425 struct drm_display_mode *mode,
10426 int depth, int bpp)
10427{
dcb1394e 10428 struct drm_framebuffer *fb;
d2dff872 10429 struct drm_i915_gem_object *obj;
0fed39bd 10430 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
d2dff872 10431
d37cd8a8 10432 obj = i915_gem_object_create(dev,
d2dff872 10433 intel_framebuffer_size_for_mode(mode, bpp));
fe3db79b
CW
10434 if (IS_ERR(obj))
10435 return ERR_CAST(obj);
d2dff872
CW
10436
10437 mode_cmd.width = mode->hdisplay;
10438 mode_cmd.height = mode->vdisplay;
308e5bcb
JB
10439 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
10440 bpp);
5ca0c34a 10441 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
d2dff872 10442
dcb1394e
LW
10443 fb = intel_framebuffer_create(dev, &mode_cmd, obj);
10444 if (IS_ERR(fb))
10445 drm_gem_object_unreference_unlocked(&obj->base);
10446
10447 return fb;
d2dff872
CW
10448}
10449
10450static struct drm_framebuffer *
10451mode_fits_in_fbdev(struct drm_device *dev,
10452 struct drm_display_mode *mode)
10453{
0695726e 10454#ifdef CONFIG_DRM_FBDEV_EMULATION
fac5e23e 10455 struct drm_i915_private *dev_priv = to_i915(dev);
d2dff872
CW
10456 struct drm_i915_gem_object *obj;
10457 struct drm_framebuffer *fb;
10458
4c0e5528 10459 if (!dev_priv->fbdev)
d2dff872
CW
10460 return NULL;
10461
4c0e5528 10462 if (!dev_priv->fbdev->fb)
d2dff872
CW
10463 return NULL;
10464
4c0e5528
DV
10465 obj = dev_priv->fbdev->fb->obj;
10466 BUG_ON(!obj);
10467
8bcd4553 10468 fb = &dev_priv->fbdev->fb->base;
01f2c773
VS
10469 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
10470 fb->bits_per_pixel))
d2dff872
CW
10471 return NULL;
10472
01f2c773 10473 if (obj->base.size < mode->vdisplay * fb->pitches[0])
d2dff872
CW
10474 return NULL;
10475
edde3617 10476 drm_framebuffer_reference(fb);
d2dff872 10477 return fb;
4520f53a
DV
10478#else
10479 return NULL;
10480#endif
d2dff872
CW
10481}
10482
d3a40d1b
ACO
10483static int intel_modeset_setup_plane_state(struct drm_atomic_state *state,
10484 struct drm_crtc *crtc,
10485 struct drm_display_mode *mode,
10486 struct drm_framebuffer *fb,
10487 int x, int y)
10488{
10489 struct drm_plane_state *plane_state;
10490 int hdisplay, vdisplay;
10491 int ret;
10492
10493 plane_state = drm_atomic_get_plane_state(state, crtc->primary);
10494 if (IS_ERR(plane_state))
10495 return PTR_ERR(plane_state);
10496
10497 if (mode)
10498 drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
10499 else
10500 hdisplay = vdisplay = 0;
10501
10502 ret = drm_atomic_set_crtc_for_plane(plane_state, fb ? crtc : NULL);
10503 if (ret)
10504 return ret;
10505 drm_atomic_set_fb_for_plane(plane_state, fb);
10506 plane_state->crtc_x = 0;
10507 plane_state->crtc_y = 0;
10508 plane_state->crtc_w = hdisplay;
10509 plane_state->crtc_h = vdisplay;
10510 plane_state->src_x = x << 16;
10511 plane_state->src_y = y << 16;
10512 plane_state->src_w = hdisplay << 16;
10513 plane_state->src_h = vdisplay << 16;
10514
10515 return 0;
10516}
10517
d2434ab7 10518bool intel_get_load_detect_pipe(struct drm_connector *connector,
7173188d 10519 struct drm_display_mode *mode,
51fd371b
RC
10520 struct intel_load_detect_pipe *old,
10521 struct drm_modeset_acquire_ctx *ctx)
79e53945
JB
10522{
10523 struct intel_crtc *intel_crtc;
d2434ab7
DV
10524 struct intel_encoder *intel_encoder =
10525 intel_attached_encoder(connector);
79e53945 10526 struct drm_crtc *possible_crtc;
4ef69c7a 10527 struct drm_encoder *encoder = &intel_encoder->base;
79e53945
JB
10528 struct drm_crtc *crtc = NULL;
10529 struct drm_device *dev = encoder->dev;
94352cf9 10530 struct drm_framebuffer *fb;
51fd371b 10531 struct drm_mode_config *config = &dev->mode_config;
edde3617 10532 struct drm_atomic_state *state = NULL, *restore_state = NULL;
944b0c76 10533 struct drm_connector_state *connector_state;
4be07317 10534 struct intel_crtc_state *crtc_state;
51fd371b 10535 int ret, i = -1;
79e53945 10536
d2dff872 10537 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
c23cc417 10538 connector->base.id, connector->name,
8e329a03 10539 encoder->base.id, encoder->name);
d2dff872 10540
edde3617
ML
10541 old->restore_state = NULL;
10542
51fd371b
RC
10543retry:
10544 ret = drm_modeset_lock(&config->connection_mutex, ctx);
10545 if (ret)
ad3c558f 10546 goto fail;
6e9f798d 10547
79e53945
JB
10548 /*
10549 * Algorithm gets a little messy:
7a5e4805 10550 *
79e53945
JB
10551 * - if the connector already has an assigned crtc, use it (but make
10552 * sure it's on first)
7a5e4805 10553 *
79e53945
JB
10554 * - try to find the first unused crtc that can drive this connector,
10555 * and use that if we find one
79e53945
JB
10556 */
10557
10558 /* See if we already have a CRTC for this connector */
edde3617
ML
10559 if (connector->state->crtc) {
10560 crtc = connector->state->crtc;
8261b191 10561
51fd371b 10562 ret = drm_modeset_lock(&crtc->mutex, ctx);
4d02e2de 10563 if (ret)
ad3c558f 10564 goto fail;
8261b191
CW
10565
10566 /* Make sure the crtc and connector are running */
edde3617 10567 goto found;
79e53945
JB
10568 }
10569
10570 /* Find an unused one (if possible) */
70e1e0ec 10571 for_each_crtc(dev, possible_crtc) {
79e53945
JB
10572 i++;
10573 if (!(encoder->possible_crtcs & (1 << i)))
10574 continue;
edde3617
ML
10575
10576 ret = drm_modeset_lock(&possible_crtc->mutex, ctx);
10577 if (ret)
10578 goto fail;
10579
10580 if (possible_crtc->state->enable) {
10581 drm_modeset_unlock(&possible_crtc->mutex);
a459249c 10582 continue;
edde3617 10583 }
a459249c
VS
10584
10585 crtc = possible_crtc;
10586 break;
79e53945
JB
10587 }
10588
10589 /*
10590 * If we didn't find an unused CRTC, don't use any.
10591 */
10592 if (!crtc) {
7173188d 10593 DRM_DEBUG_KMS("no pipe available for load-detect\n");
ad3c558f 10594 goto fail;
79e53945
JB
10595 }
10596
edde3617
ML
10597found:
10598 intel_crtc = to_intel_crtc(crtc);
10599
4d02e2de
DV
10600 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
10601 if (ret)
ad3c558f 10602 goto fail;
79e53945 10603
83a57153 10604 state = drm_atomic_state_alloc(dev);
edde3617
ML
10605 restore_state = drm_atomic_state_alloc(dev);
10606 if (!state || !restore_state) {
10607 ret = -ENOMEM;
10608 goto fail;
10609 }
83a57153
ACO
10610
10611 state->acquire_ctx = ctx;
edde3617 10612 restore_state->acquire_ctx = ctx;
83a57153 10613
944b0c76
ACO
10614 connector_state = drm_atomic_get_connector_state(state, connector);
10615 if (IS_ERR(connector_state)) {
10616 ret = PTR_ERR(connector_state);
10617 goto fail;
10618 }
10619
edde3617
ML
10620 ret = drm_atomic_set_crtc_for_connector(connector_state, crtc);
10621 if (ret)
10622 goto fail;
944b0c76 10623
4be07317
ACO
10624 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
10625 if (IS_ERR(crtc_state)) {
10626 ret = PTR_ERR(crtc_state);
10627 goto fail;
10628 }
10629
49d6fa21 10630 crtc_state->base.active = crtc_state->base.enable = true;
4be07317 10631
6492711d
CW
10632 if (!mode)
10633 mode = &load_detect_mode;
79e53945 10634
d2dff872
CW
10635 /* We need a framebuffer large enough to accommodate all accesses
10636 * that the plane may generate whilst we perform load detection.
10637 * We can not rely on the fbcon either being present (we get called
10638 * during its initialisation to detect all boot displays, or it may
10639 * not even exist) or that it is large enough to satisfy the
10640 * requested mode.
10641 */
94352cf9
DV
10642 fb = mode_fits_in_fbdev(dev, mode);
10643 if (fb == NULL) {
d2dff872 10644 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
94352cf9 10645 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
d2dff872
CW
10646 } else
10647 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
94352cf9 10648 if (IS_ERR(fb)) {
d2dff872 10649 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
412b61d8 10650 goto fail;
79e53945 10651 }
79e53945 10652
d3a40d1b
ACO
10653 ret = intel_modeset_setup_plane_state(state, crtc, mode, fb, 0, 0);
10654 if (ret)
10655 goto fail;
10656
edde3617
ML
10657 drm_framebuffer_unreference(fb);
10658
10659 ret = drm_atomic_set_mode_for_crtc(&crtc_state->base, mode);
10660 if (ret)
10661 goto fail;
10662
10663 ret = PTR_ERR_OR_ZERO(drm_atomic_get_connector_state(restore_state, connector));
10664 if (!ret)
10665 ret = PTR_ERR_OR_ZERO(drm_atomic_get_crtc_state(restore_state, crtc));
10666 if (!ret)
10667 ret = PTR_ERR_OR_ZERO(drm_atomic_get_plane_state(restore_state, crtc->primary));
10668 if (ret) {
10669 DRM_DEBUG_KMS("Failed to create a copy of old state to restore: %i\n", ret);
10670 goto fail;
10671 }
8c7b5ccb 10672
3ba86073
ML
10673 ret = drm_atomic_commit(state);
10674 if (ret) {
6492711d 10675 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
412b61d8 10676 goto fail;
79e53945 10677 }
edde3617
ML
10678
10679 old->restore_state = restore_state;
7173188d 10680
79e53945 10681 /* let the connector get through one full cycle before testing */
9d0498a2 10682 intel_wait_for_vblank(dev, intel_crtc->pipe);
7173188d 10683 return true;
412b61d8 10684
ad3c558f 10685fail:
e5d958ef 10686 drm_atomic_state_free(state);
edde3617
ML
10687 drm_atomic_state_free(restore_state);
10688 restore_state = state = NULL;
83a57153 10689
51fd371b
RC
10690 if (ret == -EDEADLK) {
10691 drm_modeset_backoff(ctx);
10692 goto retry;
10693 }
10694
412b61d8 10695 return false;
79e53945
JB
10696}
10697
d2434ab7 10698void intel_release_load_detect_pipe(struct drm_connector *connector,
49172fee
ACO
10699 struct intel_load_detect_pipe *old,
10700 struct drm_modeset_acquire_ctx *ctx)
79e53945 10701{
d2434ab7
DV
10702 struct intel_encoder *intel_encoder =
10703 intel_attached_encoder(connector);
4ef69c7a 10704 struct drm_encoder *encoder = &intel_encoder->base;
edde3617 10705 struct drm_atomic_state *state = old->restore_state;
d3a40d1b 10706 int ret;
79e53945 10707
d2dff872 10708 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
c23cc417 10709 connector->base.id, connector->name,
8e329a03 10710 encoder->base.id, encoder->name);
d2dff872 10711
edde3617 10712 if (!state)
0622a53c 10713 return;
79e53945 10714
edde3617
ML
10715 ret = drm_atomic_commit(state);
10716 if (ret) {
10717 DRM_DEBUG_KMS("Couldn't release load detect pipe: %i\n", ret);
10718 drm_atomic_state_free(state);
10719 }
79e53945
JB
10720}
10721
da4a1efa 10722static int i9xx_pll_refclk(struct drm_device *dev,
5cec258b 10723 const struct intel_crtc_state *pipe_config)
da4a1efa 10724{
fac5e23e 10725 struct drm_i915_private *dev_priv = to_i915(dev);
da4a1efa
VS
10726 u32 dpll = pipe_config->dpll_hw_state.dpll;
10727
10728 if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
e91e941b 10729 return dev_priv->vbt.lvds_ssc_freq;
da4a1efa
VS
10730 else if (HAS_PCH_SPLIT(dev))
10731 return 120000;
10732 else if (!IS_GEN2(dev))
10733 return 96000;
10734 else
10735 return 48000;
10736}
10737
79e53945 10738/* Returns the clock of the currently programmed mode of the given pipe. */
f1f644dc 10739static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 10740 struct intel_crtc_state *pipe_config)
79e53945 10741{
f1f644dc 10742 struct drm_device *dev = crtc->base.dev;
fac5e23e 10743 struct drm_i915_private *dev_priv = to_i915(dev);
f1f644dc 10744 int pipe = pipe_config->cpu_transcoder;
293623f7 10745 u32 dpll = pipe_config->dpll_hw_state.dpll;
79e53945 10746 u32 fp;
9e2c8475 10747 struct dpll clock;
dccbea3b 10748 int port_clock;
da4a1efa 10749 int refclk = i9xx_pll_refclk(dev, pipe_config);
79e53945
JB
10750
10751 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
293623f7 10752 fp = pipe_config->dpll_hw_state.fp0;
79e53945 10753 else
293623f7 10754 fp = pipe_config->dpll_hw_state.fp1;
79e53945
JB
10755
10756 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
f2b115e6
AJ
10757 if (IS_PINEVIEW(dev)) {
10758 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
10759 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
2177832f
SL
10760 } else {
10761 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
10762 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
10763 }
10764
a6c45cf0 10765 if (!IS_GEN2(dev)) {
f2b115e6
AJ
10766 if (IS_PINEVIEW(dev))
10767 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
10768 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
2177832f
SL
10769 else
10770 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
79e53945
JB
10771 DPLL_FPA01_P1_POST_DIV_SHIFT);
10772
10773 switch (dpll & DPLL_MODE_MASK) {
10774 case DPLLB_MODE_DAC_SERIAL:
10775 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
10776 5 : 10;
10777 break;
10778 case DPLLB_MODE_LVDS:
10779 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
10780 7 : 14;
10781 break;
10782 default:
28c97730 10783 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
79e53945 10784 "mode\n", (int)(dpll & DPLL_MODE_MASK));
f1f644dc 10785 return;
79e53945
JB
10786 }
10787
ac58c3f0 10788 if (IS_PINEVIEW(dev))
dccbea3b 10789 port_clock = pnv_calc_dpll_params(refclk, &clock);
ac58c3f0 10790 else
dccbea3b 10791 port_clock = i9xx_calc_dpll_params(refclk, &clock);
79e53945 10792 } else {
0fb58223 10793 u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
b1c560d1 10794 bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
79e53945
JB
10795
10796 if (is_lvds) {
10797 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
10798 DPLL_FPA01_P1_POST_DIV_SHIFT);
b1c560d1
VS
10799
10800 if (lvds & LVDS_CLKB_POWER_UP)
10801 clock.p2 = 7;
10802 else
10803 clock.p2 = 14;
79e53945
JB
10804 } else {
10805 if (dpll & PLL_P1_DIVIDE_BY_TWO)
10806 clock.p1 = 2;
10807 else {
10808 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
10809 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
10810 }
10811 if (dpll & PLL_P2_DIVIDE_BY_4)
10812 clock.p2 = 4;
10813 else
10814 clock.p2 = 2;
79e53945 10815 }
da4a1efa 10816
dccbea3b 10817 port_clock = i9xx_calc_dpll_params(refclk, &clock);
79e53945
JB
10818 }
10819
18442d08
VS
10820 /*
10821 * This value includes pixel_multiplier. We will use
241bfc38 10822 * port_clock to compute adjusted_mode.crtc_clock in the
18442d08
VS
10823 * encoder's get_config() function.
10824 */
dccbea3b 10825 pipe_config->port_clock = port_clock;
f1f644dc
JB
10826}
10827
6878da05
VS
10828int intel_dotclock_calculate(int link_freq,
10829 const struct intel_link_m_n *m_n)
f1f644dc 10830{
f1f644dc
JB
10831 /*
10832 * The calculation for the data clock is:
1041a02f 10833 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
f1f644dc 10834 * But we want to avoid losing precison if possible, so:
1041a02f 10835 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
f1f644dc
JB
10836 *
10837 * and the link clock is simpler:
1041a02f 10838 * link_clock = (m * link_clock) / n
f1f644dc
JB
10839 */
10840
6878da05
VS
10841 if (!m_n->link_n)
10842 return 0;
f1f644dc 10843
6878da05
VS
10844 return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
10845}
f1f644dc 10846
18442d08 10847static void ironlake_pch_clock_get(struct intel_crtc *crtc,
5cec258b 10848 struct intel_crtc_state *pipe_config)
6878da05 10849{
e3b247da 10850 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
79e53945 10851
18442d08
VS
10852 /* read out port_clock from the DPLL */
10853 i9xx_crtc_clock_get(crtc, pipe_config);
f1f644dc 10854
f1f644dc 10855 /*
e3b247da
VS
10856 * In case there is an active pipe without active ports,
10857 * we may need some idea for the dotclock anyway.
10858 * Calculate one based on the FDI configuration.
79e53945 10859 */
2d112de7 10860 pipe_config->base.adjusted_mode.crtc_clock =
21a727b3 10861 intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
18442d08 10862 &pipe_config->fdi_m_n);
79e53945
JB
10863}
10864
10865/** Returns the currently programmed mode of the given pipe. */
10866struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
10867 struct drm_crtc *crtc)
10868{
fac5e23e 10869 struct drm_i915_private *dev_priv = to_i915(dev);
79e53945 10870 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6e3c9717 10871 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
79e53945 10872 struct drm_display_mode *mode;
3f36b937 10873 struct intel_crtc_state *pipe_config;
fe2b8f9d
PZ
10874 int htot = I915_READ(HTOTAL(cpu_transcoder));
10875 int hsync = I915_READ(HSYNC(cpu_transcoder));
10876 int vtot = I915_READ(VTOTAL(cpu_transcoder));
10877 int vsync = I915_READ(VSYNC(cpu_transcoder));
293623f7 10878 enum pipe pipe = intel_crtc->pipe;
79e53945
JB
10879
10880 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
10881 if (!mode)
10882 return NULL;
10883
3f36b937
TU
10884 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
10885 if (!pipe_config) {
10886 kfree(mode);
10887 return NULL;
10888 }
10889
f1f644dc
JB
10890 /*
10891 * Construct a pipe_config sufficient for getting the clock info
10892 * back out of crtc_clock_get.
10893 *
10894 * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
10895 * to use a real value here instead.
10896 */
3f36b937
TU
10897 pipe_config->cpu_transcoder = (enum transcoder) pipe;
10898 pipe_config->pixel_multiplier = 1;
10899 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(pipe));
10900 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(pipe));
10901 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(pipe));
10902 i9xx_crtc_clock_get(intel_crtc, pipe_config);
10903
10904 mode->clock = pipe_config->port_clock / pipe_config->pixel_multiplier;
79e53945
JB
10905 mode->hdisplay = (htot & 0xffff) + 1;
10906 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
10907 mode->hsync_start = (hsync & 0xffff) + 1;
10908 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
10909 mode->vdisplay = (vtot & 0xffff) + 1;
10910 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
10911 mode->vsync_start = (vsync & 0xffff) + 1;
10912 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
10913
10914 drm_mode_set_name(mode);
79e53945 10915
3f36b937
TU
10916 kfree(pipe_config);
10917
79e53945
JB
10918 return mode;
10919}
10920
10921static void intel_crtc_destroy(struct drm_crtc *crtc)
10922{
10923 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
67e77c5a 10924 struct drm_device *dev = crtc->dev;
51cbaf01 10925 struct intel_flip_work *work;
67e77c5a 10926
5e2d7afc 10927 spin_lock_irq(&dev->event_lock);
5a21b665
DV
10928 work = intel_crtc->flip_work;
10929 intel_crtc->flip_work = NULL;
10930 spin_unlock_irq(&dev->event_lock);
67e77c5a 10931
5a21b665 10932 if (work) {
51cbaf01
ML
10933 cancel_work_sync(&work->mmio_work);
10934 cancel_work_sync(&work->unpin_work);
5a21b665 10935 kfree(work);
67e77c5a 10936 }
79e53945
JB
10937
10938 drm_crtc_cleanup(crtc);
67e77c5a 10939
79e53945
JB
10940 kfree(intel_crtc);
10941}
10942
6b95a207
KH
10943static void intel_unpin_work_fn(struct work_struct *__work)
10944{
51cbaf01
ML
10945 struct intel_flip_work *work =
10946 container_of(__work, struct intel_flip_work, unpin_work);
5a21b665
DV
10947 struct intel_crtc *crtc = to_intel_crtc(work->crtc);
10948 struct drm_device *dev = crtc->base.dev;
10949 struct drm_plane *primary = crtc->base.primary;
03f476e1 10950
5a21b665
DV
10951 if (is_mmio_work(work))
10952 flush_work(&work->mmio_work);
03f476e1 10953
5a21b665
DV
10954 mutex_lock(&dev->struct_mutex);
10955 intel_unpin_fb_obj(work->old_fb, primary->state->rotation);
10956 drm_gem_object_unreference(&work->pending_flip_obj->base);
5a21b665 10957 mutex_unlock(&dev->struct_mutex);
143f73b3 10958
e8a261ea
CW
10959 i915_gem_request_put(work->flip_queued_req);
10960
5a21b665
DV
10961 intel_frontbuffer_flip_complete(dev, to_intel_plane(primary)->frontbuffer_bit);
10962 intel_fbc_post_update(crtc);
10963 drm_framebuffer_unreference(work->old_fb);
143f73b3 10964
5a21b665
DV
10965 BUG_ON(atomic_read(&crtc->unpin_work_count) == 0);
10966 atomic_dec(&crtc->unpin_work_count);
a6747b73 10967
5a21b665
DV
10968 kfree(work);
10969}
d9e86c0e 10970
5a21b665
DV
10971/* Is 'a' after or equal to 'b'? */
10972static bool g4x_flip_count_after_eq(u32 a, u32 b)
10973{
10974 return !((a - b) & 0x80000000);
10975}
143f73b3 10976
5a21b665
DV
10977static bool __pageflip_finished_cs(struct intel_crtc *crtc,
10978 struct intel_flip_work *work)
10979{
10980 struct drm_device *dev = crtc->base.dev;
fac5e23e 10981 struct drm_i915_private *dev_priv = to_i915(dev);
5a21b665 10982 unsigned reset_counter;
143f73b3 10983
5a21b665
DV
10984 reset_counter = i915_reset_counter(&dev_priv->gpu_error);
10985 if (crtc->reset_counter != reset_counter)
10986 return true;
143f73b3 10987
5a21b665
DV
10988 /*
10989 * The relevant registers doen't exist on pre-ctg.
10990 * As the flip done interrupt doesn't trigger for mmio
10991 * flips on gmch platforms, a flip count check isn't
10992 * really needed there. But since ctg has the registers,
10993 * include it in the check anyway.
10994 */
10995 if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
10996 return true;
b4a98e57 10997
5a21b665
DV
10998 /*
10999 * BDW signals flip done immediately if the plane
11000 * is disabled, even if the plane enable is already
11001 * armed to occur at the next vblank :(
11002 */
f99d7069 11003
5a21b665
DV
11004 /*
11005 * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
11006 * used the same base address. In that case the mmio flip might
11007 * have completed, but the CS hasn't even executed the flip yet.
11008 *
11009 * A flip count check isn't enough as the CS might have updated
11010 * the base address just after start of vblank, but before we
11011 * managed to process the interrupt. This means we'd complete the
11012 * CS flip too soon.
11013 *
11014 * Combining both checks should get us a good enough result. It may
11015 * still happen that the CS flip has been executed, but has not
11016 * yet actually completed. But in case the base address is the same
11017 * anyway, we don't really care.
11018 */
11019 return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
11020 crtc->flip_work->gtt_offset &&
11021 g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_G4X(crtc->pipe)),
11022 crtc->flip_work->flip_count);
11023}
b4a98e57 11024
5a21b665
DV
11025static bool
11026__pageflip_finished_mmio(struct intel_crtc *crtc,
11027 struct intel_flip_work *work)
11028{
11029 /*
11030 * MMIO work completes when vblank is different from
11031 * flip_queued_vblank.
11032 *
11033 * Reset counter value doesn't matter, this is handled by
11034 * i915_wait_request finishing early, so no need to handle
11035 * reset here.
11036 */
11037 return intel_crtc_get_vblank_counter(crtc) != work->flip_queued_vblank;
6b95a207
KH
11038}
11039
51cbaf01
ML
11040
11041static bool pageflip_finished(struct intel_crtc *crtc,
11042 struct intel_flip_work *work)
11043{
11044 if (!atomic_read(&work->pending))
11045 return false;
11046
11047 smp_rmb();
11048
5a21b665
DV
11049 if (is_mmio_work(work))
11050 return __pageflip_finished_mmio(crtc, work);
11051 else
11052 return __pageflip_finished_cs(crtc, work);
11053}
11054
11055void intel_finish_page_flip_cs(struct drm_i915_private *dev_priv, int pipe)
11056{
91c8a326 11057 struct drm_device *dev = &dev_priv->drm;
5a21b665
DV
11058 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
11059 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11060 struct intel_flip_work *work;
11061 unsigned long flags;
11062
11063 /* Ignore early vblank irqs */
11064 if (!crtc)
11065 return;
11066
51cbaf01 11067 /*
5a21b665
DV
11068 * This is called both by irq handlers and the reset code (to complete
11069 * lost pageflips) so needs the full irqsave spinlocks.
51cbaf01 11070 */
5a21b665
DV
11071 spin_lock_irqsave(&dev->event_lock, flags);
11072 work = intel_crtc->flip_work;
11073
11074 if (work != NULL &&
11075 !is_mmio_work(work) &&
11076 pageflip_finished(intel_crtc, work))
11077 page_flip_completed(intel_crtc);
11078
11079 spin_unlock_irqrestore(&dev->event_lock, flags);
75f7f3ec
VS
11080}
11081
51cbaf01 11082void intel_finish_page_flip_mmio(struct drm_i915_private *dev_priv, int pipe)
6b95a207 11083{
91c8a326 11084 struct drm_device *dev = &dev_priv->drm;
5251f04e
ML
11085 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
11086 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
51cbaf01 11087 struct intel_flip_work *work;
6b95a207
KH
11088 unsigned long flags;
11089
5251f04e
ML
11090 /* Ignore early vblank irqs */
11091 if (!crtc)
11092 return;
f326038a
DV
11093
11094 /*
11095 * This is called both by irq handlers and the reset code (to complete
11096 * lost pageflips) so needs the full irqsave spinlocks.
e7d841ca 11097 */
6b95a207 11098 spin_lock_irqsave(&dev->event_lock, flags);
5a21b665 11099 work = intel_crtc->flip_work;
5251f04e 11100
5a21b665
DV
11101 if (work != NULL &&
11102 is_mmio_work(work) &&
11103 pageflip_finished(intel_crtc, work))
11104 page_flip_completed(intel_crtc);
5251f04e 11105
6b95a207
KH
11106 spin_unlock_irqrestore(&dev->event_lock, flags);
11107}
11108
5a21b665
DV
11109static inline void intel_mark_page_flip_active(struct intel_crtc *crtc,
11110 struct intel_flip_work *work)
84c33a64 11111{
5a21b665 11112 work->flip_queued_vblank = intel_crtc_get_vblank_counter(crtc);
84c33a64 11113
5a21b665
DV
11114 /* Ensure that the work item is consistent when activating it ... */
11115 smp_mb__before_atomic();
11116 atomic_set(&work->pending, 1);
11117}
a6747b73 11118
5a21b665
DV
11119static int intel_gen2_queue_flip(struct drm_device *dev,
11120 struct drm_crtc *crtc,
11121 struct drm_framebuffer *fb,
11122 struct drm_i915_gem_object *obj,
11123 struct drm_i915_gem_request *req,
11124 uint32_t flags)
11125{
11126 struct intel_engine_cs *engine = req->engine;
11127 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11128 u32 flip_mask;
11129 int ret;
143f73b3 11130
5a21b665
DV
11131 ret = intel_ring_begin(req, 6);
11132 if (ret)
11133 return ret;
143f73b3 11134
5a21b665
DV
11135 /* Can't queue multiple flips, so wait for the previous
11136 * one to finish before executing the next.
11137 */
11138 if (intel_crtc->plane)
11139 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
11140 else
11141 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
11142 intel_ring_emit(engine, MI_WAIT_FOR_EVENT | flip_mask);
11143 intel_ring_emit(engine, MI_NOOP);
11144 intel_ring_emit(engine, MI_DISPLAY_FLIP |
11145 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
11146 intel_ring_emit(engine, fb->pitches[0]);
11147 intel_ring_emit(engine, intel_crtc->flip_work->gtt_offset);
11148 intel_ring_emit(engine, 0); /* aux display base address, unused */
143f73b3 11149
5a21b665
DV
11150 return 0;
11151}
84c33a64 11152
5a21b665
DV
11153static int intel_gen3_queue_flip(struct drm_device *dev,
11154 struct drm_crtc *crtc,
11155 struct drm_framebuffer *fb,
11156 struct drm_i915_gem_object *obj,
11157 struct drm_i915_gem_request *req,
11158 uint32_t flags)
11159{
11160 struct intel_engine_cs *engine = req->engine;
11161 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11162 u32 flip_mask;
11163 int ret;
d55dbd06 11164
5a21b665
DV
11165 ret = intel_ring_begin(req, 6);
11166 if (ret)
11167 return ret;
d55dbd06 11168
5a21b665
DV
11169 if (intel_crtc->plane)
11170 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
11171 else
11172 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
11173 intel_ring_emit(engine, MI_WAIT_FOR_EVENT | flip_mask);
11174 intel_ring_emit(engine, MI_NOOP);
11175 intel_ring_emit(engine, MI_DISPLAY_FLIP_I915 |
11176 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
11177 intel_ring_emit(engine, fb->pitches[0]);
11178 intel_ring_emit(engine, intel_crtc->flip_work->gtt_offset);
11179 intel_ring_emit(engine, MI_NOOP);
fd8e058a 11180
5a21b665
DV
11181 return 0;
11182}
84c33a64 11183
5a21b665
DV
11184static int intel_gen4_queue_flip(struct drm_device *dev,
11185 struct drm_crtc *crtc,
11186 struct drm_framebuffer *fb,
11187 struct drm_i915_gem_object *obj,
11188 struct drm_i915_gem_request *req,
11189 uint32_t flags)
11190{
11191 struct intel_engine_cs *engine = req->engine;
fac5e23e 11192 struct drm_i915_private *dev_priv = to_i915(dev);
5a21b665
DV
11193 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11194 uint32_t pf, pipesrc;
11195 int ret;
143f73b3 11196
5a21b665
DV
11197 ret = intel_ring_begin(req, 4);
11198 if (ret)
11199 return ret;
143f73b3 11200
5a21b665
DV
11201 /* i965+ uses the linear or tiled offsets from the
11202 * Display Registers (which do not change across a page-flip)
11203 * so we need only reprogram the base address.
11204 */
11205 intel_ring_emit(engine, MI_DISPLAY_FLIP |
11206 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
11207 intel_ring_emit(engine, fb->pitches[0]);
11208 intel_ring_emit(engine, intel_crtc->flip_work->gtt_offset |
11209 obj->tiling_mode);
11210
11211 /* XXX Enabling the panel-fitter across page-flip is so far
11212 * untested on non-native modes, so ignore it for now.
11213 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
11214 */
11215 pf = 0;
11216 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
11217 intel_ring_emit(engine, pf | pipesrc);
143f73b3 11218
5a21b665 11219 return 0;
8c9f3aaf
JB
11220}
11221
5a21b665
DV
11222static int intel_gen6_queue_flip(struct drm_device *dev,
11223 struct drm_crtc *crtc,
11224 struct drm_framebuffer *fb,
11225 struct drm_i915_gem_object *obj,
11226 struct drm_i915_gem_request *req,
11227 uint32_t flags)
da20eabd 11228{
5a21b665 11229 struct intel_engine_cs *engine = req->engine;
fac5e23e 11230 struct drm_i915_private *dev_priv = to_i915(dev);
5a21b665
DV
11231 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11232 uint32_t pf, pipesrc;
11233 int ret;
d21fbe87 11234
5a21b665
DV
11235 ret = intel_ring_begin(req, 4);
11236 if (ret)
11237 return ret;
92826fcd 11238
5a21b665
DV
11239 intel_ring_emit(engine, MI_DISPLAY_FLIP |
11240 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
11241 intel_ring_emit(engine, fb->pitches[0] | obj->tiling_mode);
11242 intel_ring_emit(engine, intel_crtc->flip_work->gtt_offset);
92826fcd 11243
5a21b665
DV
11244 /* Contrary to the suggestions in the documentation,
11245 * "Enable Panel Fitter" does not seem to be required when page
11246 * flipping with a non-native mode, and worse causes a normal
11247 * modeset to fail.
11248 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
11249 */
11250 pf = 0;
11251 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
11252 intel_ring_emit(engine, pf | pipesrc);
7809e5ae 11253
5a21b665 11254 return 0;
7809e5ae
MR
11255}
11256
5a21b665
DV
11257static int intel_gen7_queue_flip(struct drm_device *dev,
11258 struct drm_crtc *crtc,
11259 struct drm_framebuffer *fb,
11260 struct drm_i915_gem_object *obj,
11261 struct drm_i915_gem_request *req,
11262 uint32_t flags)
d21fbe87 11263{
5a21b665
DV
11264 struct intel_engine_cs *engine = req->engine;
11265 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11266 uint32_t plane_bit = 0;
11267 int len, ret;
d21fbe87 11268
5a21b665
DV
11269 switch (intel_crtc->plane) {
11270 case PLANE_A:
11271 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
11272 break;
11273 case PLANE_B:
11274 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
11275 break;
11276 case PLANE_C:
11277 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
11278 break;
11279 default:
11280 WARN_ONCE(1, "unknown plane in flip command\n");
11281 return -ENODEV;
11282 }
11283
11284 len = 4;
11285 if (engine->id == RCS) {
11286 len += 6;
11287 /*
11288 * On Gen 8, SRM is now taking an extra dword to accommodate
11289 * 48bits addresses, and we need a NOOP for the batch size to
11290 * stay even.
11291 */
11292 if (IS_GEN8(dev))
11293 len += 2;
11294 }
11295
11296 /*
11297 * BSpec MI_DISPLAY_FLIP for IVB:
11298 * "The full packet must be contained within the same cache line."
11299 *
11300 * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
11301 * cacheline, if we ever start emitting more commands before
11302 * the MI_DISPLAY_FLIP we may need to first emit everything else,
11303 * then do the cacheline alignment, and finally emit the
11304 * MI_DISPLAY_FLIP.
11305 */
11306 ret = intel_ring_cacheline_align(req);
11307 if (ret)
11308 return ret;
11309
11310 ret = intel_ring_begin(req, len);
11311 if (ret)
11312 return ret;
11313
11314 /* Unmask the flip-done completion message. Note that the bspec says that
11315 * we should do this for both the BCS and RCS, and that we must not unmask
11316 * more than one flip event at any time (or ensure that one flip message
11317 * can be sent by waiting for flip-done prior to queueing new flips).
11318 * Experimentation says that BCS works despite DERRMR masking all
11319 * flip-done completion events and that unmasking all planes at once
11320 * for the RCS also doesn't appear to drop events. Setting the DERRMR
11321 * to zero does lead to lockups within MI_DISPLAY_FLIP.
11322 */
11323 if (engine->id == RCS) {
11324 intel_ring_emit(engine, MI_LOAD_REGISTER_IMM(1));
11325 intel_ring_emit_reg(engine, DERRMR);
11326 intel_ring_emit(engine, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
11327 DERRMR_PIPEB_PRI_FLIP_DONE |
11328 DERRMR_PIPEC_PRI_FLIP_DONE));
11329 if (IS_GEN8(dev))
11330 intel_ring_emit(engine, MI_STORE_REGISTER_MEM_GEN8 |
11331 MI_SRM_LRM_GLOBAL_GTT);
11332 else
11333 intel_ring_emit(engine, MI_STORE_REGISTER_MEM |
11334 MI_SRM_LRM_GLOBAL_GTT);
11335 intel_ring_emit_reg(engine, DERRMR);
11336 intel_ring_emit(engine, engine->scratch.gtt_offset + 256);
11337 if (IS_GEN8(dev)) {
11338 intel_ring_emit(engine, 0);
11339 intel_ring_emit(engine, MI_NOOP);
11340 }
11341 }
11342
11343 intel_ring_emit(engine, MI_DISPLAY_FLIP_I915 | plane_bit);
11344 intel_ring_emit(engine, (fb->pitches[0] | obj->tiling_mode));
11345 intel_ring_emit(engine, intel_crtc->flip_work->gtt_offset);
11346 intel_ring_emit(engine, (MI_NOOP));
11347
11348 return 0;
11349}
11350
11351static bool use_mmio_flip(struct intel_engine_cs *engine,
11352 struct drm_i915_gem_object *obj)
11353{
c37efb99
CW
11354 struct reservation_object *resv;
11355
5a21b665
DV
11356 /*
11357 * This is not being used for older platforms, because
11358 * non-availability of flip done interrupt forces us to use
11359 * CS flips. Older platforms derive flip done using some clever
11360 * tricks involving the flip_pending status bits and vblank irqs.
11361 * So using MMIO flips there would disrupt this mechanism.
11362 */
11363
11364 if (engine == NULL)
11365 return true;
11366
11367 if (INTEL_GEN(engine->i915) < 5)
11368 return false;
11369
11370 if (i915.use_mmio_flip < 0)
11371 return false;
11372 else if (i915.use_mmio_flip > 0)
11373 return true;
11374 else if (i915.enable_execlists)
11375 return true;
c37efb99
CW
11376
11377 resv = i915_gem_object_get_dmabuf_resv(obj);
11378 if (resv && !reservation_object_test_signaled_rcu(resv, false))
5a21b665 11379 return true;
c37efb99
CW
11380
11381 return engine != i915_gem_request_get_engine(obj->last_write_req);
5a21b665
DV
11382}
11383
11384static void skl_do_mmio_flip(struct intel_crtc *intel_crtc,
11385 unsigned int rotation,
11386 struct intel_flip_work *work)
11387{
11388 struct drm_device *dev = intel_crtc->base.dev;
fac5e23e 11389 struct drm_i915_private *dev_priv = to_i915(dev);
5a21b665
DV
11390 struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
11391 const enum pipe pipe = intel_crtc->pipe;
11392 u32 ctl, stride, tile_height;
11393
11394 ctl = I915_READ(PLANE_CTL(pipe, 0));
11395 ctl &= ~PLANE_CTL_TILED_MASK;
11396 switch (fb->modifier[0]) {
11397 case DRM_FORMAT_MOD_NONE:
11398 break;
11399 case I915_FORMAT_MOD_X_TILED:
11400 ctl |= PLANE_CTL_TILED_X;
11401 break;
11402 case I915_FORMAT_MOD_Y_TILED:
11403 ctl |= PLANE_CTL_TILED_Y;
11404 break;
11405 case I915_FORMAT_MOD_Yf_TILED:
11406 ctl |= PLANE_CTL_TILED_YF;
11407 break;
11408 default:
11409 MISSING_CASE(fb->modifier[0]);
11410 }
11411
11412 /*
11413 * The stride is either expressed as a multiple of 64 bytes chunks for
11414 * linear buffers or in number of tiles for tiled buffers.
11415 */
11416 if (intel_rotation_90_or_270(rotation)) {
11417 /* stride = Surface height in tiles */
11418 tile_height = intel_tile_height(dev_priv, fb->modifier[0], 0);
11419 stride = DIV_ROUND_UP(fb->height, tile_height);
11420 } else {
11421 stride = fb->pitches[0] /
11422 intel_fb_stride_alignment(dev_priv, fb->modifier[0],
11423 fb->pixel_format);
11424 }
11425
11426 /*
11427 * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
11428 * PLANE_SURF updates, the update is then guaranteed to be atomic.
11429 */
11430 I915_WRITE(PLANE_CTL(pipe, 0), ctl);
11431 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
11432
11433 I915_WRITE(PLANE_SURF(pipe, 0), work->gtt_offset);
11434 POSTING_READ(PLANE_SURF(pipe, 0));
11435}
11436
11437static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc,
11438 struct intel_flip_work *work)
11439{
11440 struct drm_device *dev = intel_crtc->base.dev;
fac5e23e 11441 struct drm_i915_private *dev_priv = to_i915(dev);
5a21b665
DV
11442 struct intel_framebuffer *intel_fb =
11443 to_intel_framebuffer(intel_crtc->base.primary->fb);
11444 struct drm_i915_gem_object *obj = intel_fb->obj;
11445 i915_reg_t reg = DSPCNTR(intel_crtc->plane);
11446 u32 dspcntr;
11447
11448 dspcntr = I915_READ(reg);
11449
11450 if (obj->tiling_mode != I915_TILING_NONE)
11451 dspcntr |= DISPPLANE_TILED;
11452 else
11453 dspcntr &= ~DISPPLANE_TILED;
11454
11455 I915_WRITE(reg, dspcntr);
11456
11457 I915_WRITE(DSPSURF(intel_crtc->plane), work->gtt_offset);
11458 POSTING_READ(DSPSURF(intel_crtc->plane));
11459}
11460
11461static void intel_mmio_flip_work_func(struct work_struct *w)
11462{
11463 struct intel_flip_work *work =
11464 container_of(w, struct intel_flip_work, mmio_work);
11465 struct intel_crtc *crtc = to_intel_crtc(work->crtc);
11466 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
11467 struct intel_framebuffer *intel_fb =
11468 to_intel_framebuffer(crtc->base.primary->fb);
11469 struct drm_i915_gem_object *obj = intel_fb->obj;
c37efb99 11470 struct reservation_object *resv;
5a21b665
DV
11471
11472 if (work->flip_queued_req)
11473 WARN_ON(__i915_wait_request(work->flip_queued_req,
11474 false, NULL,
197be2ae 11475 NO_WAITBOOST));
5a21b665
DV
11476
11477 /* For framebuffer backed by dmabuf, wait for fence */
c37efb99
CW
11478 resv = i915_gem_object_get_dmabuf_resv(obj);
11479 if (resv)
11480 WARN_ON(reservation_object_wait_timeout_rcu(resv, false, false,
5a21b665
DV
11481 MAX_SCHEDULE_TIMEOUT) < 0);
11482
11483 intel_pipe_update_start(crtc);
11484
11485 if (INTEL_GEN(dev_priv) >= 9)
11486 skl_do_mmio_flip(crtc, work->rotation, work);
11487 else
11488 /* use_mmio_flip() retricts MMIO flips to ilk+ */
11489 ilk_do_mmio_flip(crtc, work);
11490
11491 intel_pipe_update_end(crtc, work);
11492}
11493
11494static int intel_default_queue_flip(struct drm_device *dev,
11495 struct drm_crtc *crtc,
11496 struct drm_framebuffer *fb,
11497 struct drm_i915_gem_object *obj,
11498 struct drm_i915_gem_request *req,
11499 uint32_t flags)
11500{
11501 return -ENODEV;
11502}
11503
11504static bool __pageflip_stall_check_cs(struct drm_i915_private *dev_priv,
11505 struct intel_crtc *intel_crtc,
11506 struct intel_flip_work *work)
11507{
11508 u32 addr, vblank;
11509
11510 if (!atomic_read(&work->pending))
11511 return false;
11512
11513 smp_rmb();
11514
11515 vblank = intel_crtc_get_vblank_counter(intel_crtc);
11516 if (work->flip_ready_vblank == 0) {
11517 if (work->flip_queued_req &&
f69a02c9 11518 !i915_gem_request_completed(work->flip_queued_req))
5a21b665
DV
11519 return false;
11520
11521 work->flip_ready_vblank = vblank;
11522 }
11523
11524 if (vblank - work->flip_ready_vblank < 3)
11525 return false;
11526
11527 /* Potential stall - if we see that the flip has happened,
11528 * assume a missed interrupt. */
11529 if (INTEL_GEN(dev_priv) >= 4)
11530 addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
11531 else
11532 addr = I915_READ(DSPADDR(intel_crtc->plane));
11533
11534 /* There is a potential issue here with a false positive after a flip
11535 * to the same address. We could address this by checking for a
11536 * non-incrementing frame counter.
11537 */
11538 return addr == work->gtt_offset;
11539}
11540
11541void intel_check_page_flip(struct drm_i915_private *dev_priv, int pipe)
11542{
91c8a326 11543 struct drm_device *dev = &dev_priv->drm;
5a21b665
DV
11544 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
11545 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11546 struct intel_flip_work *work;
11547
11548 WARN_ON(!in_interrupt());
11549
11550 if (crtc == NULL)
11551 return;
11552
11553 spin_lock(&dev->event_lock);
11554 work = intel_crtc->flip_work;
11555
11556 if (work != NULL && !is_mmio_work(work) &&
11557 __pageflip_stall_check_cs(dev_priv, intel_crtc, work)) {
11558 WARN_ONCE(1,
11559 "Kicking stuck page flip: queued at %d, now %d\n",
11560 work->flip_queued_vblank, intel_crtc_get_vblank_counter(intel_crtc));
11561 page_flip_completed(intel_crtc);
11562 work = NULL;
11563 }
11564
11565 if (work != NULL && !is_mmio_work(work) &&
11566 intel_crtc_get_vblank_counter(intel_crtc) - work->flip_queued_vblank > 1)
11567 intel_queue_rps_boost_for_request(work->flip_queued_req);
11568 spin_unlock(&dev->event_lock);
11569}
11570
11571static int intel_crtc_page_flip(struct drm_crtc *crtc,
11572 struct drm_framebuffer *fb,
11573 struct drm_pending_vblank_event *event,
11574 uint32_t page_flip_flags)
11575{
11576 struct drm_device *dev = crtc->dev;
fac5e23e 11577 struct drm_i915_private *dev_priv = to_i915(dev);
5a21b665
DV
11578 struct drm_framebuffer *old_fb = crtc->primary->fb;
11579 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
11580 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11581 struct drm_plane *primary = crtc->primary;
11582 enum pipe pipe = intel_crtc->pipe;
11583 struct intel_flip_work *work;
11584 struct intel_engine_cs *engine;
11585 bool mmio_flip;
11586 struct drm_i915_gem_request *request = NULL;
11587 int ret;
11588
11589 /*
11590 * drm_mode_page_flip_ioctl() should already catch this, but double
11591 * check to be safe. In the future we may enable pageflipping from
11592 * a disabled primary plane.
11593 */
11594 if (WARN_ON(intel_fb_obj(old_fb) == NULL))
11595 return -EBUSY;
11596
11597 /* Can't change pixel format via MI display flips. */
11598 if (fb->pixel_format != crtc->primary->fb->pixel_format)
11599 return -EINVAL;
11600
11601 /*
11602 * TILEOFF/LINOFF registers can't be changed via MI display flips.
11603 * Note that pitch changes could also affect these register.
11604 */
11605 if (INTEL_INFO(dev)->gen > 3 &&
11606 (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
11607 fb->pitches[0] != crtc->primary->fb->pitches[0]))
11608 return -EINVAL;
11609
11610 if (i915_terminally_wedged(&dev_priv->gpu_error))
11611 goto out_hang;
11612
11613 work = kzalloc(sizeof(*work), GFP_KERNEL);
11614 if (work == NULL)
11615 return -ENOMEM;
11616
11617 work->event = event;
11618 work->crtc = crtc;
11619 work->old_fb = old_fb;
11620 INIT_WORK(&work->unpin_work, intel_unpin_work_fn);
11621
11622 ret = drm_crtc_vblank_get(crtc);
11623 if (ret)
11624 goto free_work;
11625
11626 /* We borrow the event spin lock for protecting flip_work */
11627 spin_lock_irq(&dev->event_lock);
11628 if (intel_crtc->flip_work) {
11629 /* Before declaring the flip queue wedged, check if
11630 * the hardware completed the operation behind our backs.
11631 */
11632 if (pageflip_finished(intel_crtc, intel_crtc->flip_work)) {
11633 DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
11634 page_flip_completed(intel_crtc);
11635 } else {
11636 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
11637 spin_unlock_irq(&dev->event_lock);
11638
11639 drm_crtc_vblank_put(crtc);
11640 kfree(work);
11641 return -EBUSY;
11642 }
11643 }
11644 intel_crtc->flip_work = work;
11645 spin_unlock_irq(&dev->event_lock);
11646
11647 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
11648 flush_workqueue(dev_priv->wq);
11649
11650 /* Reference the objects for the scheduled work. */
11651 drm_framebuffer_reference(work->old_fb);
5a21b665
DV
11652
11653 crtc->primary->fb = fb;
11654 update_state_fb(crtc->primary);
faf68d92
ML
11655
11656 intel_fbc_pre_update(intel_crtc, intel_crtc->config,
11657 to_intel_plane_state(primary->state));
5a21b665 11658
25dc556a 11659 work->pending_flip_obj = i915_gem_object_get(obj);
5a21b665
DV
11660
11661 ret = i915_mutex_lock_interruptible(dev);
11662 if (ret)
11663 goto cleanup;
11664
11665 intel_crtc->reset_counter = i915_reset_counter(&dev_priv->gpu_error);
11666 if (__i915_reset_in_progress_or_wedged(intel_crtc->reset_counter)) {
11667 ret = -EIO;
11668 goto cleanup;
11669 }
11670
11671 atomic_inc(&intel_crtc->unpin_work_count);
11672
11673 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
11674 work->flip_count = I915_READ(PIPE_FLIPCOUNT_G4X(pipe)) + 1;
11675
11676 if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
11677 engine = &dev_priv->engine[BCS];
11678 if (obj->tiling_mode != intel_fb_obj(work->old_fb)->tiling_mode)
11679 /* vlv: DISPLAY_FLIP fails to change tiling */
11680 engine = NULL;
11681 } else if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
11682 engine = &dev_priv->engine[BCS];
11683 } else if (INTEL_INFO(dev)->gen >= 7) {
11684 engine = i915_gem_request_get_engine(obj->last_write_req);
11685 if (engine == NULL || engine->id != RCS)
11686 engine = &dev_priv->engine[BCS];
11687 } else {
11688 engine = &dev_priv->engine[RCS];
11689 }
11690
11691 mmio_flip = use_mmio_flip(engine, obj);
11692
11693 /* When using CS flips, we want to emit semaphores between rings.
11694 * However, when using mmio flips we will create a task to do the
11695 * synchronisation, so all we want here is to pin the framebuffer
11696 * into the display plane and skip any waits.
11697 */
11698 if (!mmio_flip) {
11699 ret = i915_gem_object_sync(obj, engine, &request);
11700 if (!ret && !request) {
11701 request = i915_gem_request_alloc(engine, NULL);
11702 ret = PTR_ERR_OR_ZERO(request);
11703 }
11704
11705 if (ret)
11706 goto cleanup_pending;
11707 }
11708
11709 ret = intel_pin_and_fence_fb_obj(fb, primary->state->rotation);
11710 if (ret)
11711 goto cleanup_pending;
11712
11713 work->gtt_offset = intel_plane_obj_offset(to_intel_plane(primary),
11714 obj, 0);
11715 work->gtt_offset += intel_crtc->dspaddr_offset;
11716 work->rotation = crtc->primary->state->rotation;
11717
11718 if (mmio_flip) {
11719 INIT_WORK(&work->mmio_work, intel_mmio_flip_work_func);
11720
11721 i915_gem_request_assign(&work->flip_queued_req,
11722 obj->last_write_req);
11723
11724 schedule_work(&work->mmio_work);
11725 } else {
11726 i915_gem_request_assign(&work->flip_queued_req, request);
11727 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, request,
11728 page_flip_flags);
11729 if (ret)
11730 goto cleanup_unpin;
11731
11732 intel_mark_page_flip_active(intel_crtc, work);
11733
11734 i915_add_request_no_flush(request);
11735 }
11736
11737 i915_gem_track_fb(intel_fb_obj(old_fb), obj,
11738 to_intel_plane(primary)->frontbuffer_bit);
11739 mutex_unlock(&dev->struct_mutex);
11740
11741 intel_frontbuffer_flip_prepare(dev,
11742 to_intel_plane(primary)->frontbuffer_bit);
11743
11744 trace_i915_flip_request(intel_crtc->plane, obj);
11745
11746 return 0;
11747
11748cleanup_unpin:
11749 intel_unpin_fb_obj(fb, crtc->primary->state->rotation);
11750cleanup_pending:
11751 if (!IS_ERR_OR_NULL(request))
11752 i915_add_request_no_flush(request);
11753 atomic_dec(&intel_crtc->unpin_work_count);
11754 mutex_unlock(&dev->struct_mutex);
11755cleanup:
11756 crtc->primary->fb = old_fb;
11757 update_state_fb(crtc->primary);
11758
11759 drm_gem_object_unreference_unlocked(&obj->base);
11760 drm_framebuffer_unreference(work->old_fb);
11761
11762 spin_lock_irq(&dev->event_lock);
11763 intel_crtc->flip_work = NULL;
11764 spin_unlock_irq(&dev->event_lock);
11765
11766 drm_crtc_vblank_put(crtc);
11767free_work:
11768 kfree(work);
11769
11770 if (ret == -EIO) {
11771 struct drm_atomic_state *state;
11772 struct drm_plane_state *plane_state;
11773
11774out_hang:
11775 state = drm_atomic_state_alloc(dev);
11776 if (!state)
11777 return -ENOMEM;
11778 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
11779
11780retry:
11781 plane_state = drm_atomic_get_plane_state(state, primary);
11782 ret = PTR_ERR_OR_ZERO(plane_state);
11783 if (!ret) {
11784 drm_atomic_set_fb_for_plane(plane_state, fb);
11785
11786 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
11787 if (!ret)
11788 ret = drm_atomic_commit(state);
11789 }
11790
11791 if (ret == -EDEADLK) {
11792 drm_modeset_backoff(state->acquire_ctx);
11793 drm_atomic_state_clear(state);
11794 goto retry;
11795 }
11796
11797 if (ret)
11798 drm_atomic_state_free(state);
11799
11800 if (ret == 0 && event) {
11801 spin_lock_irq(&dev->event_lock);
11802 drm_crtc_send_vblank_event(crtc, event);
11803 spin_unlock_irq(&dev->event_lock);
11804 }
11805 }
11806 return ret;
11807}
11808
11809
11810/**
11811 * intel_wm_need_update - Check whether watermarks need updating
11812 * @plane: drm plane
11813 * @state: new plane state
11814 *
11815 * Check current plane state versus the new one to determine whether
11816 * watermarks need to be recalculated.
11817 *
11818 * Returns true or false.
11819 */
11820static bool intel_wm_need_update(struct drm_plane *plane,
11821 struct drm_plane_state *state)
11822{
11823 struct intel_plane_state *new = to_intel_plane_state(state);
11824 struct intel_plane_state *cur = to_intel_plane_state(plane->state);
11825
11826 /* Update watermarks on tiling or size changes. */
11827 if (new->visible != cur->visible)
11828 return true;
11829
11830 if (!cur->base.fb || !new->base.fb)
11831 return false;
11832
11833 if (cur->base.fb->modifier[0] != new->base.fb->modifier[0] ||
11834 cur->base.rotation != new->base.rotation ||
11835 drm_rect_width(&new->src) != drm_rect_width(&cur->src) ||
11836 drm_rect_height(&new->src) != drm_rect_height(&cur->src) ||
11837 drm_rect_width(&new->dst) != drm_rect_width(&cur->dst) ||
11838 drm_rect_height(&new->dst) != drm_rect_height(&cur->dst))
11839 return true;
11840
11841 return false;
11842}
11843
11844static bool needs_scaling(struct intel_plane_state *state)
11845{
11846 int src_w = drm_rect_width(&state->src) >> 16;
11847 int src_h = drm_rect_height(&state->src) >> 16;
11848 int dst_w = drm_rect_width(&state->dst);
11849 int dst_h = drm_rect_height(&state->dst);
11850
11851 return (src_w != dst_w || src_h != dst_h);
11852}
d21fbe87 11853
da20eabd
ML
11854int intel_plane_atomic_calc_changes(struct drm_crtc_state *crtc_state,
11855 struct drm_plane_state *plane_state)
11856{
ab1d3a0e 11857 struct intel_crtc_state *pipe_config = to_intel_crtc_state(crtc_state);
da20eabd
ML
11858 struct drm_crtc *crtc = crtc_state->crtc;
11859 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11860 struct drm_plane *plane = plane_state->plane;
11861 struct drm_device *dev = crtc->dev;
ed4a6a7c 11862 struct drm_i915_private *dev_priv = to_i915(dev);
da20eabd
ML
11863 struct intel_plane_state *old_plane_state =
11864 to_intel_plane_state(plane->state);
da20eabd
ML
11865 bool mode_changed = needs_modeset(crtc_state);
11866 bool was_crtc_enabled = crtc->state->active;
11867 bool is_crtc_enabled = crtc_state->active;
da20eabd
ML
11868 bool turn_off, turn_on, visible, was_visible;
11869 struct drm_framebuffer *fb = plane_state->fb;
78108b7c 11870 int ret;
da20eabd 11871
84114990 11872 if (INTEL_GEN(dev) >= 9 && plane->type != DRM_PLANE_TYPE_CURSOR) {
da20eabd
ML
11873 ret = skl_update_scaler_plane(
11874 to_intel_crtc_state(crtc_state),
11875 to_intel_plane_state(plane_state));
11876 if (ret)
11877 return ret;
11878 }
11879
da20eabd
ML
11880 was_visible = old_plane_state->visible;
11881 visible = to_intel_plane_state(plane_state)->visible;
11882
11883 if (!was_crtc_enabled && WARN_ON(was_visible))
11884 was_visible = false;
11885
35c08f43
ML
11886 /*
11887 * Visibility is calculated as if the crtc was on, but
11888 * after scaler setup everything depends on it being off
11889 * when the crtc isn't active.
f818ffea
VS
11890 *
11891 * FIXME this is wrong for watermarks. Watermarks should also
11892 * be computed as if the pipe would be active. Perhaps move
11893 * per-plane wm computation to the .check_plane() hook, and
11894 * only combine the results from all planes in the current place?
35c08f43
ML
11895 */
11896 if (!is_crtc_enabled)
11897 to_intel_plane_state(plane_state)->visible = visible = false;
da20eabd
ML
11898
11899 if (!was_visible && !visible)
11900 return 0;
11901
e8861675
ML
11902 if (fb != old_plane_state->base.fb)
11903 pipe_config->fb_changed = true;
11904
da20eabd
ML
11905 turn_off = was_visible && (!visible || mode_changed);
11906 turn_on = visible && (!was_visible || mode_changed);
11907
72660ce0 11908 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] has [PLANE:%d:%s] with fb %i\n",
78108b7c
VS
11909 intel_crtc->base.base.id,
11910 intel_crtc->base.name,
72660ce0
VS
11911 plane->base.id, plane->name,
11912 fb ? fb->base.id : -1);
da20eabd 11913
72660ce0
VS
11914 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] visible %i -> %i, off %i, on %i, ms %i\n",
11915 plane->base.id, plane->name,
11916 was_visible, visible,
da20eabd
ML
11917 turn_off, turn_on, mode_changed);
11918
caed361d
VS
11919 if (turn_on) {
11920 pipe_config->update_wm_pre = true;
11921
11922 /* must disable cxsr around plane enable/disable */
11923 if (plane->type != DRM_PLANE_TYPE_CURSOR)
11924 pipe_config->disable_cxsr = true;
11925 } else if (turn_off) {
11926 pipe_config->update_wm_post = true;
92826fcd 11927
852eb00d 11928 /* must disable cxsr around plane enable/disable */
e8861675 11929 if (plane->type != DRM_PLANE_TYPE_CURSOR)
ab1d3a0e 11930 pipe_config->disable_cxsr = true;
852eb00d 11931 } else if (intel_wm_need_update(plane, plane_state)) {
caed361d
VS
11932 /* FIXME bollocks */
11933 pipe_config->update_wm_pre = true;
11934 pipe_config->update_wm_post = true;
852eb00d 11935 }
da20eabd 11936
ed4a6a7c 11937 /* Pre-gen9 platforms need two-step watermark updates */
caed361d
VS
11938 if ((pipe_config->update_wm_pre || pipe_config->update_wm_post) &&
11939 INTEL_INFO(dev)->gen < 9 && dev_priv->display.optimize_watermarks)
ed4a6a7c
MR
11940 to_intel_crtc_state(crtc_state)->wm.need_postvbl_update = true;
11941
8be6ca85 11942 if (visible || was_visible)
cd202f69 11943 pipe_config->fb_bits |= to_intel_plane(plane)->frontbuffer_bit;
a9ff8714 11944
31ae71fc
ML
11945 /*
11946 * WaCxSRDisabledForSpriteScaling:ivb
11947 *
11948 * cstate->update_wm was already set above, so this flag will
11949 * take effect when we commit and program watermarks.
11950 */
11951 if (plane->type == DRM_PLANE_TYPE_OVERLAY && IS_IVYBRIDGE(dev) &&
11952 needs_scaling(to_intel_plane_state(plane_state)) &&
11953 !needs_scaling(old_plane_state))
11954 pipe_config->disable_lp_wm = true;
d21fbe87 11955
da20eabd
ML
11956 return 0;
11957}
11958
6d3a1ce7
ML
11959static bool encoders_cloneable(const struct intel_encoder *a,
11960 const struct intel_encoder *b)
11961{
11962 /* masks could be asymmetric, so check both ways */
11963 return a == b || (a->cloneable & (1 << b->type) &&
11964 b->cloneable & (1 << a->type));
11965}
11966
11967static bool check_single_encoder_cloning(struct drm_atomic_state *state,
11968 struct intel_crtc *crtc,
11969 struct intel_encoder *encoder)
11970{
11971 struct intel_encoder *source_encoder;
11972 struct drm_connector *connector;
11973 struct drm_connector_state *connector_state;
11974 int i;
11975
11976 for_each_connector_in_state(state, connector, connector_state, i) {
11977 if (connector_state->crtc != &crtc->base)
11978 continue;
11979
11980 source_encoder =
11981 to_intel_encoder(connector_state->best_encoder);
11982 if (!encoders_cloneable(encoder, source_encoder))
11983 return false;
11984 }
11985
11986 return true;
11987}
11988
6d3a1ce7
ML
11989static int intel_crtc_atomic_check(struct drm_crtc *crtc,
11990 struct drm_crtc_state *crtc_state)
11991{
cf5a15be 11992 struct drm_device *dev = crtc->dev;
fac5e23e 11993 struct drm_i915_private *dev_priv = to_i915(dev);
6d3a1ce7 11994 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
cf5a15be
ML
11995 struct intel_crtc_state *pipe_config =
11996 to_intel_crtc_state(crtc_state);
6d3a1ce7 11997 struct drm_atomic_state *state = crtc_state->state;
4d20cd86 11998 int ret;
6d3a1ce7
ML
11999 bool mode_changed = needs_modeset(crtc_state);
12000
852eb00d 12001 if (mode_changed && !crtc_state->active)
caed361d 12002 pipe_config->update_wm_post = true;
eddfcbcd 12003
ad421372
ML
12004 if (mode_changed && crtc_state->enable &&
12005 dev_priv->display.crtc_compute_clock &&
8106ddbd 12006 !WARN_ON(pipe_config->shared_dpll)) {
ad421372
ML
12007 ret = dev_priv->display.crtc_compute_clock(intel_crtc,
12008 pipe_config);
12009 if (ret)
12010 return ret;
12011 }
12012
82cf435b
LL
12013 if (crtc_state->color_mgmt_changed) {
12014 ret = intel_color_check(crtc, crtc_state);
12015 if (ret)
12016 return ret;
e7852a4b
LL
12017
12018 /*
12019 * Changing color management on Intel hardware is
12020 * handled as part of planes update.
12021 */
12022 crtc_state->planes_changed = true;
82cf435b
LL
12023 }
12024
e435d6e5 12025 ret = 0;
86c8bbbe 12026 if (dev_priv->display.compute_pipe_wm) {
e3bddded 12027 ret = dev_priv->display.compute_pipe_wm(pipe_config);
ed4a6a7c
MR
12028 if (ret) {
12029 DRM_DEBUG_KMS("Target pipe watermarks are invalid\n");
12030 return ret;
12031 }
12032 }
12033
12034 if (dev_priv->display.compute_intermediate_wm &&
12035 !to_intel_atomic_state(state)->skip_intermediate_wm) {
12036 if (WARN_ON(!dev_priv->display.compute_pipe_wm))
12037 return 0;
12038
12039 /*
12040 * Calculate 'intermediate' watermarks that satisfy both the
12041 * old state and the new state. We can program these
12042 * immediately.
12043 */
12044 ret = dev_priv->display.compute_intermediate_wm(crtc->dev,
12045 intel_crtc,
12046 pipe_config);
12047 if (ret) {
12048 DRM_DEBUG_KMS("No valid intermediate pipe watermarks are possible\n");
86c8bbbe 12049 return ret;
ed4a6a7c 12050 }
e3d5457c
VS
12051 } else if (dev_priv->display.compute_intermediate_wm) {
12052 if (HAS_PCH_SPLIT(dev_priv) && INTEL_GEN(dev_priv) < 9)
12053 pipe_config->wm.ilk.intermediate = pipe_config->wm.ilk.optimal;
86c8bbbe
MR
12054 }
12055
e435d6e5
ML
12056 if (INTEL_INFO(dev)->gen >= 9) {
12057 if (mode_changed)
12058 ret = skl_update_scaler_crtc(pipe_config);
12059
12060 if (!ret)
12061 ret = intel_atomic_setup_scalers(dev, intel_crtc,
12062 pipe_config);
12063 }
12064
12065 return ret;
6d3a1ce7
ML
12066}
12067
65b38e0d 12068static const struct drm_crtc_helper_funcs intel_helper_funcs = {
f6e5b160 12069 .mode_set_base_atomic = intel_pipe_set_base_atomic,
5a21b665
DV
12070 .atomic_begin = intel_begin_crtc_commit,
12071 .atomic_flush = intel_finish_crtc_commit,
6d3a1ce7 12072 .atomic_check = intel_crtc_atomic_check,
f6e5b160
CW
12073};
12074
d29b2f9d
ACO
12075static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
12076{
12077 struct intel_connector *connector;
12078
12079 for_each_intel_connector(dev, connector) {
8863dc7f
DV
12080 if (connector->base.state->crtc)
12081 drm_connector_unreference(&connector->base);
12082
d29b2f9d
ACO
12083 if (connector->base.encoder) {
12084 connector->base.state->best_encoder =
12085 connector->base.encoder;
12086 connector->base.state->crtc =
12087 connector->base.encoder->crtc;
8863dc7f
DV
12088
12089 drm_connector_reference(&connector->base);
d29b2f9d
ACO
12090 } else {
12091 connector->base.state->best_encoder = NULL;
12092 connector->base.state->crtc = NULL;
12093 }
12094 }
12095}
12096
050f7aeb 12097static void
eba905b2 12098connected_sink_compute_bpp(struct intel_connector *connector,
5cec258b 12099 struct intel_crtc_state *pipe_config)
050f7aeb
DV
12100{
12101 int bpp = pipe_config->pipe_bpp;
12102
12103 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
12104 connector->base.base.id,
c23cc417 12105 connector->base.name);
050f7aeb
DV
12106
12107 /* Don't use an invalid EDID bpc value */
12108 if (connector->base.display_info.bpc &&
12109 connector->base.display_info.bpc * 3 < bpp) {
12110 DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
12111 bpp, connector->base.display_info.bpc*3);
12112 pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
12113 }
12114
013dd9e0
JN
12115 /* Clamp bpp to default limit on screens without EDID 1.4 */
12116 if (connector->base.display_info.bpc == 0) {
12117 int type = connector->base.connector_type;
12118 int clamp_bpp = 24;
12119
12120 /* Fall back to 18 bpp when DP sink capability is unknown. */
12121 if (type == DRM_MODE_CONNECTOR_DisplayPort ||
12122 type == DRM_MODE_CONNECTOR_eDP)
12123 clamp_bpp = 18;
12124
12125 if (bpp > clamp_bpp) {
12126 DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of %d\n",
12127 bpp, clamp_bpp);
12128 pipe_config->pipe_bpp = clamp_bpp;
12129 }
050f7aeb
DV
12130 }
12131}
12132
4e53c2e0 12133static int
050f7aeb 12134compute_baseline_pipe_bpp(struct intel_crtc *crtc,
5cec258b 12135 struct intel_crtc_state *pipe_config)
4e53c2e0 12136{
050f7aeb 12137 struct drm_device *dev = crtc->base.dev;
1486017f 12138 struct drm_atomic_state *state;
da3ced29
ACO
12139 struct drm_connector *connector;
12140 struct drm_connector_state *connector_state;
1486017f 12141 int bpp, i;
4e53c2e0 12142
666a4537 12143 if ((IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)))
4e53c2e0 12144 bpp = 10*3;
d328c9d7
DV
12145 else if (INTEL_INFO(dev)->gen >= 5)
12146 bpp = 12*3;
12147 else
12148 bpp = 8*3;
12149
4e53c2e0 12150
4e53c2e0
DV
12151 pipe_config->pipe_bpp = bpp;
12152
1486017f
ACO
12153 state = pipe_config->base.state;
12154
4e53c2e0 12155 /* Clamp display bpp to EDID value */
da3ced29
ACO
12156 for_each_connector_in_state(state, connector, connector_state, i) {
12157 if (connector_state->crtc != &crtc->base)
4e53c2e0
DV
12158 continue;
12159
da3ced29
ACO
12160 connected_sink_compute_bpp(to_intel_connector(connector),
12161 pipe_config);
4e53c2e0
DV
12162 }
12163
12164 return bpp;
12165}
12166
644db711
DV
12167static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
12168{
12169 DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
12170 "type: 0x%x flags: 0x%x\n",
1342830c 12171 mode->crtc_clock,
644db711
DV
12172 mode->crtc_hdisplay, mode->crtc_hsync_start,
12173 mode->crtc_hsync_end, mode->crtc_htotal,
12174 mode->crtc_vdisplay, mode->crtc_vsync_start,
12175 mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
12176}
12177
c0b03411 12178static void intel_dump_pipe_config(struct intel_crtc *crtc,
5cec258b 12179 struct intel_crtc_state *pipe_config,
c0b03411
DV
12180 const char *context)
12181{
6a60cd87
CK
12182 struct drm_device *dev = crtc->base.dev;
12183 struct drm_plane *plane;
12184 struct intel_plane *intel_plane;
12185 struct intel_plane_state *state;
12186 struct drm_framebuffer *fb;
12187
78108b7c
VS
12188 DRM_DEBUG_KMS("[CRTC:%d:%s]%s config %p for pipe %c\n",
12189 crtc->base.base.id, crtc->base.name,
6a60cd87 12190 context, pipe_config, pipe_name(crtc->pipe));
c0b03411 12191
da205630 12192 DRM_DEBUG_KMS("cpu_transcoder: %s\n", transcoder_name(pipe_config->cpu_transcoder));
c0b03411
DV
12193 DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
12194 pipe_config->pipe_bpp, pipe_config->dither);
12195 DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
12196 pipe_config->has_pch_encoder,
12197 pipe_config->fdi_lanes,
12198 pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
12199 pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
12200 pipe_config->fdi_m_n.tu);
90a6b7b0 12201 DRM_DEBUG_KMS("dp: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
37a5650b 12202 intel_crtc_has_dp_encoder(pipe_config),
90a6b7b0 12203 pipe_config->lane_count,
eb14cb74
VS
12204 pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
12205 pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
12206 pipe_config->dp_m_n.tu);
b95af8be 12207
90a6b7b0 12208 DRM_DEBUG_KMS("dp: %i, lanes: %i, gmch_m2: %u, gmch_n2: %u, link_m2: %u, link_n2: %u, tu2: %u\n",
37a5650b 12209 intel_crtc_has_dp_encoder(pipe_config),
90a6b7b0 12210 pipe_config->lane_count,
b95af8be
VK
12211 pipe_config->dp_m2_n2.gmch_m,
12212 pipe_config->dp_m2_n2.gmch_n,
12213 pipe_config->dp_m2_n2.link_m,
12214 pipe_config->dp_m2_n2.link_n,
12215 pipe_config->dp_m2_n2.tu);
12216
55072d19
DV
12217 DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
12218 pipe_config->has_audio,
12219 pipe_config->has_infoframe);
12220
c0b03411 12221 DRM_DEBUG_KMS("requested mode:\n");
2d112de7 12222 drm_mode_debug_printmodeline(&pipe_config->base.mode);
c0b03411 12223 DRM_DEBUG_KMS("adjusted mode:\n");
2d112de7
ACO
12224 drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
12225 intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
d71b8d4a 12226 DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
37327abd
VS
12227 DRM_DEBUG_KMS("pipe src size: %dx%d\n",
12228 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
0ec463d3
TU
12229 DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
12230 crtc->num_scalers,
12231 pipe_config->scaler_state.scaler_users,
12232 pipe_config->scaler_state.scaler_id);
c0b03411
DV
12233 DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
12234 pipe_config->gmch_pfit.control,
12235 pipe_config->gmch_pfit.pgm_ratios,
12236 pipe_config->gmch_pfit.lvds_border_bits);
fd4daa9c 12237 DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
c0b03411 12238 pipe_config->pch_pfit.pos,
fd4daa9c
CW
12239 pipe_config->pch_pfit.size,
12240 pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
42db64ef 12241 DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
cf532bb2 12242 DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
6a60cd87 12243
415ff0f6 12244 if (IS_BROXTON(dev)) {
05712c15 12245 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: ebb0: 0x%x, ebb4: 0x%x,"
415ff0f6 12246 "pll0: 0x%x, pll1: 0x%x, pll2: 0x%x, pll3: 0x%x, "
c8453338 12247 "pll6: 0x%x, pll8: 0x%x, pll9: 0x%x, pll10: 0x%x, pcsdw12: 0x%x\n",
415ff0f6
TU
12248 pipe_config->ddi_pll_sel,
12249 pipe_config->dpll_hw_state.ebb0,
05712c15 12250 pipe_config->dpll_hw_state.ebb4,
415ff0f6
TU
12251 pipe_config->dpll_hw_state.pll0,
12252 pipe_config->dpll_hw_state.pll1,
12253 pipe_config->dpll_hw_state.pll2,
12254 pipe_config->dpll_hw_state.pll3,
12255 pipe_config->dpll_hw_state.pll6,
12256 pipe_config->dpll_hw_state.pll8,
05712c15 12257 pipe_config->dpll_hw_state.pll9,
c8453338 12258 pipe_config->dpll_hw_state.pll10,
415ff0f6 12259 pipe_config->dpll_hw_state.pcsdw12);
ef11bdb3 12260 } else if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) {
415ff0f6
TU
12261 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: "
12262 "ctrl1: 0x%x, cfgcr1: 0x%x, cfgcr2: 0x%x\n",
12263 pipe_config->ddi_pll_sel,
12264 pipe_config->dpll_hw_state.ctrl1,
12265 pipe_config->dpll_hw_state.cfgcr1,
12266 pipe_config->dpll_hw_state.cfgcr2);
12267 } else if (HAS_DDI(dev)) {
1260f07e 12268 DRM_DEBUG_KMS("ddi_pll_sel: 0x%x; dpll_hw_state: wrpll: 0x%x spll: 0x%x\n",
415ff0f6 12269 pipe_config->ddi_pll_sel,
00490c22
ML
12270 pipe_config->dpll_hw_state.wrpll,
12271 pipe_config->dpll_hw_state.spll);
415ff0f6
TU
12272 } else {
12273 DRM_DEBUG_KMS("dpll_hw_state: dpll: 0x%x, dpll_md: 0x%x, "
12274 "fp0: 0x%x, fp1: 0x%x\n",
12275 pipe_config->dpll_hw_state.dpll,
12276 pipe_config->dpll_hw_state.dpll_md,
12277 pipe_config->dpll_hw_state.fp0,
12278 pipe_config->dpll_hw_state.fp1);
12279 }
12280
6a60cd87
CK
12281 DRM_DEBUG_KMS("planes on this crtc\n");
12282 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
12283 intel_plane = to_intel_plane(plane);
12284 if (intel_plane->pipe != crtc->pipe)
12285 continue;
12286
12287 state = to_intel_plane_state(plane->state);
12288 fb = state->base.fb;
12289 if (!fb) {
1d577e02
VS
12290 DRM_DEBUG_KMS("[PLANE:%d:%s] disabled, scaler_id = %d\n",
12291 plane->base.id, plane->name, state->scaler_id);
6a60cd87
CK
12292 continue;
12293 }
12294
1d577e02
VS
12295 DRM_DEBUG_KMS("[PLANE:%d:%s] enabled",
12296 plane->base.id, plane->name);
12297 DRM_DEBUG_KMS("\tFB:%d, fb = %ux%u format = %s",
12298 fb->base.id, fb->width, fb->height,
12299 drm_get_format_name(fb->pixel_format));
12300 DRM_DEBUG_KMS("\tscaler:%d src %dx%d+%d+%d dst %dx%d+%d+%d\n",
12301 state->scaler_id,
12302 state->src.x1 >> 16, state->src.y1 >> 16,
12303 drm_rect_width(&state->src) >> 16,
12304 drm_rect_height(&state->src) >> 16,
12305 state->dst.x1, state->dst.y1,
12306 drm_rect_width(&state->dst),
12307 drm_rect_height(&state->dst));
6a60cd87 12308 }
c0b03411
DV
12309}
12310
5448a00d 12311static bool check_digital_port_conflicts(struct drm_atomic_state *state)
00f0b378 12312{
5448a00d 12313 struct drm_device *dev = state->dev;
da3ced29 12314 struct drm_connector *connector;
00f0b378
VS
12315 unsigned int used_ports = 0;
12316
12317 /*
12318 * Walk the connector list instead of the encoder
12319 * list to detect the problem on ddi platforms
12320 * where there's just one encoder per digital port.
12321 */
0bff4858
VS
12322 drm_for_each_connector(connector, dev) {
12323 struct drm_connector_state *connector_state;
12324 struct intel_encoder *encoder;
12325
12326 connector_state = drm_atomic_get_existing_connector_state(state, connector);
12327 if (!connector_state)
12328 connector_state = connector->state;
12329
5448a00d 12330 if (!connector_state->best_encoder)
00f0b378
VS
12331 continue;
12332
5448a00d
ACO
12333 encoder = to_intel_encoder(connector_state->best_encoder);
12334
12335 WARN_ON(!connector_state->crtc);
00f0b378
VS
12336
12337 switch (encoder->type) {
12338 unsigned int port_mask;
12339 case INTEL_OUTPUT_UNKNOWN:
12340 if (WARN_ON(!HAS_DDI(dev)))
12341 break;
cca0502b 12342 case INTEL_OUTPUT_DP:
00f0b378
VS
12343 case INTEL_OUTPUT_HDMI:
12344 case INTEL_OUTPUT_EDP:
12345 port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
12346
12347 /* the same port mustn't appear more than once */
12348 if (used_ports & port_mask)
12349 return false;
12350
12351 used_ports |= port_mask;
12352 default:
12353 break;
12354 }
12355 }
12356
12357 return true;
12358}
12359
83a57153
ACO
12360static void
12361clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
12362{
12363 struct drm_crtc_state tmp_state;
663a3640 12364 struct intel_crtc_scaler_state scaler_state;
4978cc93 12365 struct intel_dpll_hw_state dpll_hw_state;
8106ddbd 12366 struct intel_shared_dpll *shared_dpll;
8504c74c 12367 uint32_t ddi_pll_sel;
c4e2d043 12368 bool force_thru;
83a57153 12369
7546a384
ACO
12370 /* FIXME: before the switch to atomic started, a new pipe_config was
12371 * kzalloc'd. Code that depends on any field being zero should be
12372 * fixed, so that the crtc_state can be safely duplicated. For now,
12373 * only fields that are know to not cause problems are preserved. */
12374
83a57153 12375 tmp_state = crtc_state->base;
663a3640 12376 scaler_state = crtc_state->scaler_state;
4978cc93
ACO
12377 shared_dpll = crtc_state->shared_dpll;
12378 dpll_hw_state = crtc_state->dpll_hw_state;
8504c74c 12379 ddi_pll_sel = crtc_state->ddi_pll_sel;
c4e2d043 12380 force_thru = crtc_state->pch_pfit.force_thru;
4978cc93 12381
83a57153 12382 memset(crtc_state, 0, sizeof *crtc_state);
4978cc93 12383
83a57153 12384 crtc_state->base = tmp_state;
663a3640 12385 crtc_state->scaler_state = scaler_state;
4978cc93
ACO
12386 crtc_state->shared_dpll = shared_dpll;
12387 crtc_state->dpll_hw_state = dpll_hw_state;
8504c74c 12388 crtc_state->ddi_pll_sel = ddi_pll_sel;
c4e2d043 12389 crtc_state->pch_pfit.force_thru = force_thru;
83a57153
ACO
12390}
12391
548ee15b 12392static int
b8cecdf5 12393intel_modeset_pipe_config(struct drm_crtc *crtc,
b359283a 12394 struct intel_crtc_state *pipe_config)
ee7b9f93 12395{
b359283a 12396 struct drm_atomic_state *state = pipe_config->base.state;
7758a113 12397 struct intel_encoder *encoder;
da3ced29 12398 struct drm_connector *connector;
0b901879 12399 struct drm_connector_state *connector_state;
d328c9d7 12400 int base_bpp, ret = -EINVAL;
0b901879 12401 int i;
e29c22c0 12402 bool retry = true;
ee7b9f93 12403
83a57153 12404 clear_intel_crtc_state(pipe_config);
7758a113 12405
e143a21c
DV
12406 pipe_config->cpu_transcoder =
12407 (enum transcoder) to_intel_crtc(crtc)->pipe;
b8cecdf5 12408
2960bc9c
ID
12409 /*
12410 * Sanitize sync polarity flags based on requested ones. If neither
12411 * positive or negative polarity is requested, treat this as meaning
12412 * negative polarity.
12413 */
2d112de7 12414 if (!(pipe_config->base.adjusted_mode.flags &
2960bc9c 12415 (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
2d112de7 12416 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
2960bc9c 12417
2d112de7 12418 if (!(pipe_config->base.adjusted_mode.flags &
2960bc9c 12419 (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
2d112de7 12420 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
2960bc9c 12421
d328c9d7
DV
12422 base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
12423 pipe_config);
12424 if (base_bpp < 0)
4e53c2e0
DV
12425 goto fail;
12426
e41a56be
VS
12427 /*
12428 * Determine the real pipe dimensions. Note that stereo modes can
12429 * increase the actual pipe size due to the frame doubling and
12430 * insertion of additional space for blanks between the frame. This
12431 * is stored in the crtc timings. We use the requested mode to do this
12432 * computation to clearly distinguish it from the adjusted mode, which
12433 * can be changed by the connectors in the below retry loop.
12434 */
2d112de7 12435 drm_crtc_get_hv_timing(&pipe_config->base.mode,
ecb7e16b
GP
12436 &pipe_config->pipe_src_w,
12437 &pipe_config->pipe_src_h);
e41a56be 12438
253c84c8
VS
12439 for_each_connector_in_state(state, connector, connector_state, i) {
12440 if (connector_state->crtc != crtc)
12441 continue;
12442
12443 encoder = to_intel_encoder(connector_state->best_encoder);
12444
e25148d0
VS
12445 if (!check_single_encoder_cloning(state, to_intel_crtc(crtc), encoder)) {
12446 DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
12447 goto fail;
12448 }
12449
253c84c8
VS
12450 /*
12451 * Determine output_types before calling the .compute_config()
12452 * hooks so that the hooks can use this information safely.
12453 */
12454 pipe_config->output_types |= 1 << encoder->type;
12455 }
12456
e29c22c0 12457encoder_retry:
ef1b460d 12458 /* Ensure the port clock defaults are reset when retrying. */
ff9a6750 12459 pipe_config->port_clock = 0;
ef1b460d 12460 pipe_config->pixel_multiplier = 1;
ff9a6750 12461
135c81b8 12462 /* Fill in default crtc timings, allow encoders to overwrite them. */
2d112de7
ACO
12463 drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
12464 CRTC_STEREO_DOUBLE);
135c81b8 12465
7758a113
DV
12466 /* Pass our mode to the connectors and the CRTC to give them a chance to
12467 * adjust it according to limitations or connector properties, and also
12468 * a chance to reject the mode entirely.
47f1c6c9 12469 */
da3ced29 12470 for_each_connector_in_state(state, connector, connector_state, i) {
0b901879 12471 if (connector_state->crtc != crtc)
7758a113 12472 continue;
7ae89233 12473
0b901879
ACO
12474 encoder = to_intel_encoder(connector_state->best_encoder);
12475
efea6e8e
DV
12476 if (!(encoder->compute_config(encoder, pipe_config))) {
12477 DRM_DEBUG_KMS("Encoder config failure\n");
7758a113
DV
12478 goto fail;
12479 }
ee7b9f93 12480 }
47f1c6c9 12481
ff9a6750
DV
12482 /* Set default port clock if not overwritten by the encoder. Needs to be
12483 * done afterwards in case the encoder adjusts the mode. */
12484 if (!pipe_config->port_clock)
2d112de7 12485 pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
241bfc38 12486 * pipe_config->pixel_multiplier;
ff9a6750 12487
a43f6e0f 12488 ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
e29c22c0 12489 if (ret < 0) {
7758a113
DV
12490 DRM_DEBUG_KMS("CRTC fixup failed\n");
12491 goto fail;
ee7b9f93 12492 }
e29c22c0
DV
12493
12494 if (ret == RETRY) {
12495 if (WARN(!retry, "loop in pipe configuration computation\n")) {
12496 ret = -EINVAL;
12497 goto fail;
12498 }
12499
12500 DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
12501 retry = false;
12502 goto encoder_retry;
12503 }
12504
e8fa4270
DV
12505 /* Dithering seems to not pass-through bits correctly when it should, so
12506 * only enable it on 6bpc panels. */
12507 pipe_config->dither = pipe_config->pipe_bpp == 6*3;
62f0ace5 12508 DRM_DEBUG_KMS("hw max bpp: %i, pipe bpp: %i, dithering: %i\n",
d328c9d7 12509 base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
4e53c2e0 12510
7758a113 12511fail:
548ee15b 12512 return ret;
ee7b9f93 12513}
47f1c6c9 12514
ea9d758d 12515static void
4740b0f2 12516intel_modeset_update_crtc_state(struct drm_atomic_state *state)
ea9d758d 12517{
0a9ab303
ACO
12518 struct drm_crtc *crtc;
12519 struct drm_crtc_state *crtc_state;
8a75d157 12520 int i;
ea9d758d 12521
7668851f 12522 /* Double check state. */
8a75d157 12523 for_each_crtc_in_state(state, crtc, crtc_state, i) {
3cb480bc 12524 to_intel_crtc(crtc)->config = to_intel_crtc_state(crtc->state);
fc467a22
ML
12525
12526 /* Update hwmode for vblank functions */
12527 if (crtc->state->active)
12528 crtc->hwmode = crtc->state->adjusted_mode;
12529 else
12530 crtc->hwmode.crtc_clock = 0;
61067a5e
ML
12531
12532 /*
12533 * Update legacy state to satisfy fbc code. This can
12534 * be removed when fbc uses the atomic state.
12535 */
12536 if (drm_atomic_get_existing_plane_state(state, crtc->primary)) {
12537 struct drm_plane_state *plane_state = crtc->primary->state;
12538
12539 crtc->primary->fb = plane_state->fb;
12540 crtc->x = plane_state->src_x >> 16;
12541 crtc->y = plane_state->src_y >> 16;
12542 }
ea9d758d 12543 }
ea9d758d
DV
12544}
12545
3bd26263 12546static bool intel_fuzzy_clock_check(int clock1, int clock2)
f1f644dc 12547{
3bd26263 12548 int diff;
f1f644dc
JB
12549
12550 if (clock1 == clock2)
12551 return true;
12552
12553 if (!clock1 || !clock2)
12554 return false;
12555
12556 diff = abs(clock1 - clock2);
12557
12558 if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
12559 return true;
12560
12561 return false;
12562}
12563
25c5b266
DV
12564#define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
12565 list_for_each_entry((intel_crtc), \
12566 &(dev)->mode_config.crtc_list, \
12567 base.head) \
95150bdf 12568 for_each_if (mask & (1 <<(intel_crtc)->pipe))
25c5b266 12569
cfb23ed6
ML
12570static bool
12571intel_compare_m_n(unsigned int m, unsigned int n,
12572 unsigned int m2, unsigned int n2,
12573 bool exact)
12574{
12575 if (m == m2 && n == n2)
12576 return true;
12577
12578 if (exact || !m || !n || !m2 || !n2)
12579 return false;
12580
12581 BUILD_BUG_ON(DATA_LINK_M_N_MASK > INT_MAX);
12582
31d10b57
ML
12583 if (n > n2) {
12584 while (n > n2) {
cfb23ed6
ML
12585 m2 <<= 1;
12586 n2 <<= 1;
12587 }
31d10b57
ML
12588 } else if (n < n2) {
12589 while (n < n2) {
cfb23ed6
ML
12590 m <<= 1;
12591 n <<= 1;
12592 }
12593 }
12594
31d10b57
ML
12595 if (n != n2)
12596 return false;
12597
12598 return intel_fuzzy_clock_check(m, m2);
cfb23ed6
ML
12599}
12600
12601static bool
12602intel_compare_link_m_n(const struct intel_link_m_n *m_n,
12603 struct intel_link_m_n *m2_n2,
12604 bool adjust)
12605{
12606 if (m_n->tu == m2_n2->tu &&
12607 intel_compare_m_n(m_n->gmch_m, m_n->gmch_n,
12608 m2_n2->gmch_m, m2_n2->gmch_n, !adjust) &&
12609 intel_compare_m_n(m_n->link_m, m_n->link_n,
12610 m2_n2->link_m, m2_n2->link_n, !adjust)) {
12611 if (adjust)
12612 *m2_n2 = *m_n;
12613
12614 return true;
12615 }
12616
12617 return false;
12618}
12619
0e8ffe1b 12620static bool
2fa2fe9a 12621intel_pipe_config_compare(struct drm_device *dev,
5cec258b 12622 struct intel_crtc_state *current_config,
cfb23ed6
ML
12623 struct intel_crtc_state *pipe_config,
12624 bool adjust)
0e8ffe1b 12625{
cfb23ed6
ML
12626 bool ret = true;
12627
12628#define INTEL_ERR_OR_DBG_KMS(fmt, ...) \
12629 do { \
12630 if (!adjust) \
12631 DRM_ERROR(fmt, ##__VA_ARGS__); \
12632 else \
12633 DRM_DEBUG_KMS(fmt, ##__VA_ARGS__); \
12634 } while (0)
12635
66e985c0
DV
12636#define PIPE_CONF_CHECK_X(name) \
12637 if (current_config->name != pipe_config->name) { \
cfb23ed6 12638 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
66e985c0
DV
12639 "(expected 0x%08x, found 0x%08x)\n", \
12640 current_config->name, \
12641 pipe_config->name); \
cfb23ed6 12642 ret = false; \
66e985c0
DV
12643 }
12644
08a24034
DV
12645#define PIPE_CONF_CHECK_I(name) \
12646 if (current_config->name != pipe_config->name) { \
cfb23ed6 12647 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
08a24034
DV
12648 "(expected %i, found %i)\n", \
12649 current_config->name, \
12650 pipe_config->name); \
cfb23ed6
ML
12651 ret = false; \
12652 }
12653
8106ddbd
ACO
12654#define PIPE_CONF_CHECK_P(name) \
12655 if (current_config->name != pipe_config->name) { \
12656 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
12657 "(expected %p, found %p)\n", \
12658 current_config->name, \
12659 pipe_config->name); \
12660 ret = false; \
12661 }
12662
cfb23ed6
ML
12663#define PIPE_CONF_CHECK_M_N(name) \
12664 if (!intel_compare_link_m_n(&current_config->name, \
12665 &pipe_config->name,\
12666 adjust)) { \
12667 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
12668 "(expected tu %i gmch %i/%i link %i/%i, " \
12669 "found tu %i, gmch %i/%i link %i/%i)\n", \
12670 current_config->name.tu, \
12671 current_config->name.gmch_m, \
12672 current_config->name.gmch_n, \
12673 current_config->name.link_m, \
12674 current_config->name.link_n, \
12675 pipe_config->name.tu, \
12676 pipe_config->name.gmch_m, \
12677 pipe_config->name.gmch_n, \
12678 pipe_config->name.link_m, \
12679 pipe_config->name.link_n); \
12680 ret = false; \
12681 }
12682
55c561a7
DV
12683/* This is required for BDW+ where there is only one set of registers for
12684 * switching between high and low RR.
12685 * This macro can be used whenever a comparison has to be made between one
12686 * hw state and multiple sw state variables.
12687 */
cfb23ed6
ML
12688#define PIPE_CONF_CHECK_M_N_ALT(name, alt_name) \
12689 if (!intel_compare_link_m_n(&current_config->name, \
12690 &pipe_config->name, adjust) && \
12691 !intel_compare_link_m_n(&current_config->alt_name, \
12692 &pipe_config->name, adjust)) { \
12693 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
12694 "(expected tu %i gmch %i/%i link %i/%i, " \
12695 "or tu %i gmch %i/%i link %i/%i, " \
12696 "found tu %i, gmch %i/%i link %i/%i)\n", \
12697 current_config->name.tu, \
12698 current_config->name.gmch_m, \
12699 current_config->name.gmch_n, \
12700 current_config->name.link_m, \
12701 current_config->name.link_n, \
12702 current_config->alt_name.tu, \
12703 current_config->alt_name.gmch_m, \
12704 current_config->alt_name.gmch_n, \
12705 current_config->alt_name.link_m, \
12706 current_config->alt_name.link_n, \
12707 pipe_config->name.tu, \
12708 pipe_config->name.gmch_m, \
12709 pipe_config->name.gmch_n, \
12710 pipe_config->name.link_m, \
12711 pipe_config->name.link_n); \
12712 ret = false; \
88adfff1
DV
12713 }
12714
1bd1bd80
DV
12715#define PIPE_CONF_CHECK_FLAGS(name, mask) \
12716 if ((current_config->name ^ pipe_config->name) & (mask)) { \
cfb23ed6 12717 INTEL_ERR_OR_DBG_KMS("mismatch in " #name "(" #mask ") " \
1bd1bd80
DV
12718 "(expected %i, found %i)\n", \
12719 current_config->name & (mask), \
12720 pipe_config->name & (mask)); \
cfb23ed6 12721 ret = false; \
1bd1bd80
DV
12722 }
12723
5e550656
VS
12724#define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
12725 if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
cfb23ed6 12726 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
5e550656
VS
12727 "(expected %i, found %i)\n", \
12728 current_config->name, \
12729 pipe_config->name); \
cfb23ed6 12730 ret = false; \
5e550656
VS
12731 }
12732
bb760063
DV
12733#define PIPE_CONF_QUIRK(quirk) \
12734 ((current_config->quirks | pipe_config->quirks) & (quirk))
12735
eccb140b
DV
12736 PIPE_CONF_CHECK_I(cpu_transcoder);
12737
08a24034
DV
12738 PIPE_CONF_CHECK_I(has_pch_encoder);
12739 PIPE_CONF_CHECK_I(fdi_lanes);
cfb23ed6 12740 PIPE_CONF_CHECK_M_N(fdi_m_n);
08a24034 12741
90a6b7b0 12742 PIPE_CONF_CHECK_I(lane_count);
95a7a2ae 12743 PIPE_CONF_CHECK_X(lane_lat_optim_mask);
b95af8be
VK
12744
12745 if (INTEL_INFO(dev)->gen < 8) {
cfb23ed6
ML
12746 PIPE_CONF_CHECK_M_N(dp_m_n);
12747
cfb23ed6
ML
12748 if (current_config->has_drrs)
12749 PIPE_CONF_CHECK_M_N(dp_m2_n2);
12750 } else
12751 PIPE_CONF_CHECK_M_N_ALT(dp_m_n, dp_m2_n2);
eb14cb74 12752
253c84c8 12753 PIPE_CONF_CHECK_X(output_types);
a65347ba 12754
2d112de7
ACO
12755 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
12756 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
12757 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
12758 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
12759 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
12760 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
1bd1bd80 12761
2d112de7
ACO
12762 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
12763 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
12764 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
12765 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
12766 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
12767 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
1bd1bd80 12768
c93f54cf 12769 PIPE_CONF_CHECK_I(pixel_multiplier);
6897b4b5 12770 PIPE_CONF_CHECK_I(has_hdmi_sink);
b5a9fa09 12771 if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
666a4537 12772 IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev))
b5a9fa09 12773 PIPE_CONF_CHECK_I(limited_color_range);
e43823ec 12774 PIPE_CONF_CHECK_I(has_infoframe);
6c49f241 12775
9ed109a7
DV
12776 PIPE_CONF_CHECK_I(has_audio);
12777
2d112de7 12778 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
1bd1bd80
DV
12779 DRM_MODE_FLAG_INTERLACE);
12780
bb760063 12781 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
2d112de7 12782 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063 12783 DRM_MODE_FLAG_PHSYNC);
2d112de7 12784 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063 12785 DRM_MODE_FLAG_NHSYNC);
2d112de7 12786 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063 12787 DRM_MODE_FLAG_PVSYNC);
2d112de7 12788 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063
DV
12789 DRM_MODE_FLAG_NVSYNC);
12790 }
045ac3b5 12791
333b8ca8 12792 PIPE_CONF_CHECK_X(gmch_pfit.control);
e2ff2d4a
DV
12793 /* pfit ratios are autocomputed by the hw on gen4+ */
12794 if (INTEL_INFO(dev)->gen < 4)
7f7d8dd6 12795 PIPE_CONF_CHECK_X(gmch_pfit.pgm_ratios);
333b8ca8 12796 PIPE_CONF_CHECK_X(gmch_pfit.lvds_border_bits);
9953599b 12797
bfd16b2a
ML
12798 if (!adjust) {
12799 PIPE_CONF_CHECK_I(pipe_src_w);
12800 PIPE_CONF_CHECK_I(pipe_src_h);
12801
12802 PIPE_CONF_CHECK_I(pch_pfit.enabled);
12803 if (current_config->pch_pfit.enabled) {
12804 PIPE_CONF_CHECK_X(pch_pfit.pos);
12805 PIPE_CONF_CHECK_X(pch_pfit.size);
12806 }
2fa2fe9a 12807
7aefe2b5
ML
12808 PIPE_CONF_CHECK_I(scaler_state.scaler_id);
12809 }
a1b2278e 12810
e59150dc
JB
12811 /* BDW+ don't expose a synchronous way to read the state */
12812 if (IS_HASWELL(dev))
12813 PIPE_CONF_CHECK_I(ips_enabled);
42db64ef 12814
282740f7
VS
12815 PIPE_CONF_CHECK_I(double_wide);
12816
26804afd
DV
12817 PIPE_CONF_CHECK_X(ddi_pll_sel);
12818
8106ddbd 12819 PIPE_CONF_CHECK_P(shared_dpll);
66e985c0 12820 PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
8bcc2795 12821 PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
66e985c0
DV
12822 PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
12823 PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
d452c5b6 12824 PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
00490c22 12825 PIPE_CONF_CHECK_X(dpll_hw_state.spll);
3f4cd19f
DL
12826 PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
12827 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
12828 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
c0d43d62 12829
47eacbab
VS
12830 PIPE_CONF_CHECK_X(dsi_pll.ctrl);
12831 PIPE_CONF_CHECK_X(dsi_pll.div);
12832
42571aef
VS
12833 if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
12834 PIPE_CONF_CHECK_I(pipe_bpp);
12835
2d112de7 12836 PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
a9a7e98a 12837 PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
5e550656 12838
66e985c0 12839#undef PIPE_CONF_CHECK_X
08a24034 12840#undef PIPE_CONF_CHECK_I
8106ddbd 12841#undef PIPE_CONF_CHECK_P
1bd1bd80 12842#undef PIPE_CONF_CHECK_FLAGS
5e550656 12843#undef PIPE_CONF_CHECK_CLOCK_FUZZY
bb760063 12844#undef PIPE_CONF_QUIRK
cfb23ed6 12845#undef INTEL_ERR_OR_DBG_KMS
88adfff1 12846
cfb23ed6 12847 return ret;
0e8ffe1b
DV
12848}
12849
e3b247da
VS
12850static void intel_pipe_config_sanity_check(struct drm_i915_private *dev_priv,
12851 const struct intel_crtc_state *pipe_config)
12852{
12853 if (pipe_config->has_pch_encoder) {
21a727b3 12854 int fdi_dotclock = intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
e3b247da
VS
12855 &pipe_config->fdi_m_n);
12856 int dotclock = pipe_config->base.adjusted_mode.crtc_clock;
12857
12858 /*
12859 * FDI already provided one idea for the dotclock.
12860 * Yell if the encoder disagrees.
12861 */
12862 WARN(!intel_fuzzy_clock_check(fdi_dotclock, dotclock),
12863 "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
12864 fdi_dotclock, dotclock);
12865 }
12866}
12867
c0ead703
ML
12868static void verify_wm_state(struct drm_crtc *crtc,
12869 struct drm_crtc_state *new_state)
08db6652 12870{
e7c84544 12871 struct drm_device *dev = crtc->dev;
fac5e23e 12872 struct drm_i915_private *dev_priv = to_i915(dev);
08db6652 12873 struct skl_ddb_allocation hw_ddb, *sw_ddb;
e7c84544
ML
12874 struct skl_ddb_entry *hw_entry, *sw_entry;
12875 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
12876 const enum pipe pipe = intel_crtc->pipe;
08db6652
DL
12877 int plane;
12878
e7c84544 12879 if (INTEL_INFO(dev)->gen < 9 || !new_state->active)
08db6652
DL
12880 return;
12881
12882 skl_ddb_get_hw_state(dev_priv, &hw_ddb);
12883 sw_ddb = &dev_priv->wm.skl_hw.ddb;
12884
e7c84544
ML
12885 /* planes */
12886 for_each_plane(dev_priv, pipe, plane) {
12887 hw_entry = &hw_ddb.plane[pipe][plane];
12888 sw_entry = &sw_ddb->plane[pipe][plane];
08db6652 12889
e7c84544 12890 if (skl_ddb_entry_equal(hw_entry, sw_entry))
08db6652
DL
12891 continue;
12892
e7c84544
ML
12893 DRM_ERROR("mismatch in DDB state pipe %c plane %d "
12894 "(expected (%u,%u), found (%u,%u))\n",
12895 pipe_name(pipe), plane + 1,
12896 sw_entry->start, sw_entry->end,
12897 hw_entry->start, hw_entry->end);
12898 }
08db6652 12899
e7c84544
ML
12900 /* cursor */
12901 hw_entry = &hw_ddb.plane[pipe][PLANE_CURSOR];
12902 sw_entry = &sw_ddb->plane[pipe][PLANE_CURSOR];
08db6652 12903
e7c84544 12904 if (!skl_ddb_entry_equal(hw_entry, sw_entry)) {
08db6652
DL
12905 DRM_ERROR("mismatch in DDB state pipe %c cursor "
12906 "(expected (%u,%u), found (%u,%u))\n",
12907 pipe_name(pipe),
12908 sw_entry->start, sw_entry->end,
12909 hw_entry->start, hw_entry->end);
12910 }
12911}
12912
91d1b4bd 12913static void
c0ead703 12914verify_connector_state(struct drm_device *dev, struct drm_crtc *crtc)
8af6cf88 12915{
35dd3c64 12916 struct drm_connector *connector;
8af6cf88 12917
e7c84544 12918 drm_for_each_connector(connector, dev) {
35dd3c64
ML
12919 struct drm_encoder *encoder = connector->encoder;
12920 struct drm_connector_state *state = connector->state;
ad3c558f 12921
e7c84544
ML
12922 if (state->crtc != crtc)
12923 continue;
12924
5a21b665 12925 intel_connector_verify_state(to_intel_connector(connector));
8af6cf88 12926
ad3c558f 12927 I915_STATE_WARN(state->best_encoder != encoder,
35dd3c64 12928 "connector's atomic encoder doesn't match legacy encoder\n");
8af6cf88 12929 }
91d1b4bd
DV
12930}
12931
12932static void
c0ead703 12933verify_encoder_state(struct drm_device *dev)
91d1b4bd
DV
12934{
12935 struct intel_encoder *encoder;
12936 struct intel_connector *connector;
8af6cf88 12937
b2784e15 12938 for_each_intel_encoder(dev, encoder) {
8af6cf88 12939 bool enabled = false;
4d20cd86 12940 enum pipe pipe;
8af6cf88
DV
12941
12942 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
12943 encoder->base.base.id,
8e329a03 12944 encoder->base.name);
8af6cf88 12945
3a3371ff 12946 for_each_intel_connector(dev, connector) {
4d20cd86 12947 if (connector->base.state->best_encoder != &encoder->base)
8af6cf88
DV
12948 continue;
12949 enabled = true;
ad3c558f
ML
12950
12951 I915_STATE_WARN(connector->base.state->crtc !=
12952 encoder->base.crtc,
12953 "connector's crtc doesn't match encoder crtc\n");
8af6cf88 12954 }
0e32b39c 12955
e2c719b7 12956 I915_STATE_WARN(!!encoder->base.crtc != enabled,
8af6cf88
DV
12957 "encoder's enabled state mismatch "
12958 "(expected %i, found %i)\n",
12959 !!encoder->base.crtc, enabled);
7c60d198
ML
12960
12961 if (!encoder->base.crtc) {
4d20cd86 12962 bool active;
7c60d198 12963
4d20cd86
ML
12964 active = encoder->get_hw_state(encoder, &pipe);
12965 I915_STATE_WARN(active,
12966 "encoder detached but still enabled on pipe %c.\n",
12967 pipe_name(pipe));
7c60d198 12968 }
8af6cf88 12969 }
91d1b4bd
DV
12970}
12971
12972static void
c0ead703
ML
12973verify_crtc_state(struct drm_crtc *crtc,
12974 struct drm_crtc_state *old_crtc_state,
12975 struct drm_crtc_state *new_crtc_state)
91d1b4bd 12976{
e7c84544 12977 struct drm_device *dev = crtc->dev;
fac5e23e 12978 struct drm_i915_private *dev_priv = to_i915(dev);
91d1b4bd 12979 struct intel_encoder *encoder;
e7c84544
ML
12980 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
12981 struct intel_crtc_state *pipe_config, *sw_config;
12982 struct drm_atomic_state *old_state;
12983 bool active;
045ac3b5 12984
e7c84544 12985 old_state = old_crtc_state->state;
ec2dc6a0 12986 __drm_atomic_helper_crtc_destroy_state(old_crtc_state);
e7c84544
ML
12987 pipe_config = to_intel_crtc_state(old_crtc_state);
12988 memset(pipe_config, 0, sizeof(*pipe_config));
12989 pipe_config->base.crtc = crtc;
12990 pipe_config->base.state = old_state;
8af6cf88 12991
78108b7c 12992 DRM_DEBUG_KMS("[CRTC:%d:%s]\n", crtc->base.id, crtc->name);
8af6cf88 12993
e7c84544 12994 active = dev_priv->display.get_pipe_config(intel_crtc, pipe_config);
d62cf62a 12995
e7c84544
ML
12996 /* hw state is inconsistent with the pipe quirk */
12997 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
12998 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
12999 active = new_crtc_state->active;
6c49f241 13000
e7c84544
ML
13001 I915_STATE_WARN(new_crtc_state->active != active,
13002 "crtc active state doesn't match with hw state "
13003 "(expected %i, found %i)\n", new_crtc_state->active, active);
0e8ffe1b 13004
e7c84544
ML
13005 I915_STATE_WARN(intel_crtc->active != new_crtc_state->active,
13006 "transitional active state does not match atomic hw state "
13007 "(expected %i, found %i)\n", new_crtc_state->active, intel_crtc->active);
4d20cd86 13008
e7c84544
ML
13009 for_each_encoder_on_crtc(dev, crtc, encoder) {
13010 enum pipe pipe;
4d20cd86 13011
e7c84544
ML
13012 active = encoder->get_hw_state(encoder, &pipe);
13013 I915_STATE_WARN(active != new_crtc_state->active,
13014 "[ENCODER:%i] active %i with crtc active %i\n",
13015 encoder->base.base.id, active, new_crtc_state->active);
4d20cd86 13016
e7c84544
ML
13017 I915_STATE_WARN(active && intel_crtc->pipe != pipe,
13018 "Encoder connected to wrong pipe %c\n",
13019 pipe_name(pipe));
4d20cd86 13020
253c84c8
VS
13021 if (active) {
13022 pipe_config->output_types |= 1 << encoder->type;
e7c84544 13023 encoder->get_config(encoder, pipe_config);
253c84c8 13024 }
e7c84544 13025 }
53d9f4e9 13026
e7c84544
ML
13027 if (!new_crtc_state->active)
13028 return;
cfb23ed6 13029
e7c84544 13030 intel_pipe_config_sanity_check(dev_priv, pipe_config);
e3b247da 13031
e7c84544
ML
13032 sw_config = to_intel_crtc_state(crtc->state);
13033 if (!intel_pipe_config_compare(dev, sw_config,
13034 pipe_config, false)) {
13035 I915_STATE_WARN(1, "pipe state doesn't match!\n");
13036 intel_dump_pipe_config(intel_crtc, pipe_config,
13037 "[hw state]");
13038 intel_dump_pipe_config(intel_crtc, sw_config,
13039 "[sw state]");
8af6cf88
DV
13040 }
13041}
13042
91d1b4bd 13043static void
c0ead703
ML
13044verify_single_dpll_state(struct drm_i915_private *dev_priv,
13045 struct intel_shared_dpll *pll,
13046 struct drm_crtc *crtc,
13047 struct drm_crtc_state *new_state)
91d1b4bd 13048{
91d1b4bd 13049 struct intel_dpll_hw_state dpll_hw_state;
e7c84544
ML
13050 unsigned crtc_mask;
13051 bool active;
5358901f 13052
e7c84544 13053 memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
5358901f 13054
e7c84544 13055 DRM_DEBUG_KMS("%s\n", pll->name);
5358901f 13056
e7c84544 13057 active = pll->funcs.get_hw_state(dev_priv, pll, &dpll_hw_state);
5358901f 13058
e7c84544
ML
13059 if (!(pll->flags & INTEL_DPLL_ALWAYS_ON)) {
13060 I915_STATE_WARN(!pll->on && pll->active_mask,
13061 "pll in active use but not on in sw tracking\n");
13062 I915_STATE_WARN(pll->on && !pll->active_mask,
13063 "pll is on but not used by any active crtc\n");
13064 I915_STATE_WARN(pll->on != active,
13065 "pll on state mismatch (expected %i, found %i)\n",
13066 pll->on, active);
13067 }
5358901f 13068
e7c84544 13069 if (!crtc) {
2dd66ebd 13070 I915_STATE_WARN(pll->active_mask & ~pll->config.crtc_mask,
e7c84544
ML
13071 "more active pll users than references: %x vs %x\n",
13072 pll->active_mask, pll->config.crtc_mask);
5358901f 13073
e7c84544
ML
13074 return;
13075 }
13076
13077 crtc_mask = 1 << drm_crtc_index(crtc);
13078
13079 if (new_state->active)
13080 I915_STATE_WARN(!(pll->active_mask & crtc_mask),
13081 "pll active mismatch (expected pipe %c in active mask 0x%02x)\n",
13082 pipe_name(drm_crtc_index(crtc)), pll->active_mask);
13083 else
13084 I915_STATE_WARN(pll->active_mask & crtc_mask,
13085 "pll active mismatch (didn't expect pipe %c in active mask 0x%02x)\n",
13086 pipe_name(drm_crtc_index(crtc)), pll->active_mask);
2dd66ebd 13087
e7c84544
ML
13088 I915_STATE_WARN(!(pll->config.crtc_mask & crtc_mask),
13089 "pll enabled crtcs mismatch (expected 0x%x in 0x%02x)\n",
13090 crtc_mask, pll->config.crtc_mask);
66e985c0 13091
e7c84544
ML
13092 I915_STATE_WARN(pll->on && memcmp(&pll->config.hw_state,
13093 &dpll_hw_state,
13094 sizeof(dpll_hw_state)),
13095 "pll hw state mismatch\n");
13096}
13097
13098static void
c0ead703
ML
13099verify_shared_dpll_state(struct drm_device *dev, struct drm_crtc *crtc,
13100 struct drm_crtc_state *old_crtc_state,
13101 struct drm_crtc_state *new_crtc_state)
e7c84544 13102{
fac5e23e 13103 struct drm_i915_private *dev_priv = to_i915(dev);
e7c84544
ML
13104 struct intel_crtc_state *old_state = to_intel_crtc_state(old_crtc_state);
13105 struct intel_crtc_state *new_state = to_intel_crtc_state(new_crtc_state);
13106
13107 if (new_state->shared_dpll)
c0ead703 13108 verify_single_dpll_state(dev_priv, new_state->shared_dpll, crtc, new_crtc_state);
e7c84544
ML
13109
13110 if (old_state->shared_dpll &&
13111 old_state->shared_dpll != new_state->shared_dpll) {
13112 unsigned crtc_mask = 1 << drm_crtc_index(crtc);
13113 struct intel_shared_dpll *pll = old_state->shared_dpll;
13114
13115 I915_STATE_WARN(pll->active_mask & crtc_mask,
13116 "pll active mismatch (didn't expect pipe %c in active mask)\n",
13117 pipe_name(drm_crtc_index(crtc)));
13118 I915_STATE_WARN(pll->config.crtc_mask & crtc_mask,
13119 "pll enabled crtcs mismatch (found %x in enabled mask)\n",
13120 pipe_name(drm_crtc_index(crtc)));
5358901f 13121 }
8af6cf88
DV
13122}
13123
e7c84544 13124static void
c0ead703 13125intel_modeset_verify_crtc(struct drm_crtc *crtc,
e7c84544
ML
13126 struct drm_crtc_state *old_state,
13127 struct drm_crtc_state *new_state)
13128{
5a21b665
DV
13129 if (!needs_modeset(new_state) &&
13130 !to_intel_crtc_state(new_state)->update_pipe)
13131 return;
13132
c0ead703 13133 verify_wm_state(crtc, new_state);
5a21b665 13134 verify_connector_state(crtc->dev, crtc);
c0ead703
ML
13135 verify_crtc_state(crtc, old_state, new_state);
13136 verify_shared_dpll_state(crtc->dev, crtc, old_state, new_state);
e7c84544
ML
13137}
13138
13139static void
c0ead703 13140verify_disabled_dpll_state(struct drm_device *dev)
e7c84544 13141{
fac5e23e 13142 struct drm_i915_private *dev_priv = to_i915(dev);
e7c84544
ML
13143 int i;
13144
13145 for (i = 0; i < dev_priv->num_shared_dpll; i++)
c0ead703 13146 verify_single_dpll_state(dev_priv, &dev_priv->shared_dplls[i], NULL, NULL);
e7c84544
ML
13147}
13148
13149static void
c0ead703 13150intel_modeset_verify_disabled(struct drm_device *dev)
e7c84544 13151{
c0ead703
ML
13152 verify_encoder_state(dev);
13153 verify_connector_state(dev, NULL);
13154 verify_disabled_dpll_state(dev);
e7c84544
ML
13155}
13156
80715b2f
VS
13157static void update_scanline_offset(struct intel_crtc *crtc)
13158{
13159 struct drm_device *dev = crtc->base.dev;
13160
13161 /*
13162 * The scanline counter increments at the leading edge of hsync.
13163 *
13164 * On most platforms it starts counting from vtotal-1 on the
13165 * first active line. That means the scanline counter value is
13166 * always one less than what we would expect. Ie. just after
13167 * start of vblank, which also occurs at start of hsync (on the
13168 * last active line), the scanline counter will read vblank_start-1.
13169 *
13170 * On gen2 the scanline counter starts counting from 1 instead
13171 * of vtotal-1, so we have to subtract one (or rather add vtotal-1
13172 * to keep the value positive), instead of adding one.
13173 *
13174 * On HSW+ the behaviour of the scanline counter depends on the output
13175 * type. For DP ports it behaves like most other platforms, but on HDMI
13176 * there's an extra 1 line difference. So we need to add two instead of
13177 * one to the value.
13178 */
13179 if (IS_GEN2(dev)) {
124abe07 13180 const struct drm_display_mode *adjusted_mode = &crtc->config->base.adjusted_mode;
80715b2f
VS
13181 int vtotal;
13182
124abe07
VS
13183 vtotal = adjusted_mode->crtc_vtotal;
13184 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
80715b2f
VS
13185 vtotal /= 2;
13186
13187 crtc->scanline_offset = vtotal - 1;
13188 } else if (HAS_DDI(dev) &&
2d84d2b3 13189 intel_crtc_has_type(crtc->config, INTEL_OUTPUT_HDMI)) {
80715b2f
VS
13190 crtc->scanline_offset = 2;
13191 } else
13192 crtc->scanline_offset = 1;
13193}
13194
ad421372 13195static void intel_modeset_clear_plls(struct drm_atomic_state *state)
ed6739ef 13196{
225da59b 13197 struct drm_device *dev = state->dev;
ed6739ef 13198 struct drm_i915_private *dev_priv = to_i915(dev);
ad421372 13199 struct intel_shared_dpll_config *shared_dpll = NULL;
0a9ab303
ACO
13200 struct drm_crtc *crtc;
13201 struct drm_crtc_state *crtc_state;
0a9ab303 13202 int i;
ed6739ef
ACO
13203
13204 if (!dev_priv->display.crtc_compute_clock)
ad421372 13205 return;
ed6739ef 13206
0a9ab303 13207 for_each_crtc_in_state(state, crtc, crtc_state, i) {
fb1a38a9 13208 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8106ddbd
ACO
13209 struct intel_shared_dpll *old_dpll =
13210 to_intel_crtc_state(crtc->state)->shared_dpll;
0a9ab303 13211
fb1a38a9 13212 if (!needs_modeset(crtc_state))
225da59b
ACO
13213 continue;
13214
8106ddbd 13215 to_intel_crtc_state(crtc_state)->shared_dpll = NULL;
fb1a38a9 13216
8106ddbd 13217 if (!old_dpll)
fb1a38a9 13218 continue;
0a9ab303 13219
ad421372
ML
13220 if (!shared_dpll)
13221 shared_dpll = intel_atomic_get_shared_dpll_state(state);
ed6739ef 13222
8106ddbd 13223 intel_shared_dpll_config_put(shared_dpll, old_dpll, intel_crtc);
ad421372 13224 }
ed6739ef
ACO
13225}
13226
99d736a2
ML
13227/*
13228 * This implements the workaround described in the "notes" section of the mode
13229 * set sequence documentation. When going from no pipes or single pipe to
13230 * multiple pipes, and planes are enabled after the pipe, we need to wait at
13231 * least 2 vblanks on the first pipe before enabling planes on the second pipe.
13232 */
13233static int haswell_mode_set_planes_workaround(struct drm_atomic_state *state)
13234{
13235 struct drm_crtc_state *crtc_state;
13236 struct intel_crtc *intel_crtc;
13237 struct drm_crtc *crtc;
13238 struct intel_crtc_state *first_crtc_state = NULL;
13239 struct intel_crtc_state *other_crtc_state = NULL;
13240 enum pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
13241 int i;
13242
13243 /* look at all crtc's that are going to be enabled in during modeset */
13244 for_each_crtc_in_state(state, crtc, crtc_state, i) {
13245 intel_crtc = to_intel_crtc(crtc);
13246
13247 if (!crtc_state->active || !needs_modeset(crtc_state))
13248 continue;
13249
13250 if (first_crtc_state) {
13251 other_crtc_state = to_intel_crtc_state(crtc_state);
13252 break;
13253 } else {
13254 first_crtc_state = to_intel_crtc_state(crtc_state);
13255 first_pipe = intel_crtc->pipe;
13256 }
13257 }
13258
13259 /* No workaround needed? */
13260 if (!first_crtc_state)
13261 return 0;
13262
13263 /* w/a possibly needed, check how many crtc's are already enabled. */
13264 for_each_intel_crtc(state->dev, intel_crtc) {
13265 struct intel_crtc_state *pipe_config;
13266
13267 pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
13268 if (IS_ERR(pipe_config))
13269 return PTR_ERR(pipe_config);
13270
13271 pipe_config->hsw_workaround_pipe = INVALID_PIPE;
13272
13273 if (!pipe_config->base.active ||
13274 needs_modeset(&pipe_config->base))
13275 continue;
13276
13277 /* 2 or more enabled crtcs means no need for w/a */
13278 if (enabled_pipe != INVALID_PIPE)
13279 return 0;
13280
13281 enabled_pipe = intel_crtc->pipe;
13282 }
13283
13284 if (enabled_pipe != INVALID_PIPE)
13285 first_crtc_state->hsw_workaround_pipe = enabled_pipe;
13286 else if (other_crtc_state)
13287 other_crtc_state->hsw_workaround_pipe = first_pipe;
13288
13289 return 0;
13290}
13291
27c329ed
ML
13292static int intel_modeset_all_pipes(struct drm_atomic_state *state)
13293{
13294 struct drm_crtc *crtc;
13295 struct drm_crtc_state *crtc_state;
13296 int ret = 0;
13297
13298 /* add all active pipes to the state */
13299 for_each_crtc(state->dev, crtc) {
13300 crtc_state = drm_atomic_get_crtc_state(state, crtc);
13301 if (IS_ERR(crtc_state))
13302 return PTR_ERR(crtc_state);
13303
13304 if (!crtc_state->active || needs_modeset(crtc_state))
13305 continue;
13306
13307 crtc_state->mode_changed = true;
13308
13309 ret = drm_atomic_add_affected_connectors(state, crtc);
13310 if (ret)
13311 break;
13312
13313 ret = drm_atomic_add_affected_planes(state, crtc);
13314 if (ret)
13315 break;
13316 }
13317
13318 return ret;
13319}
13320
c347a676 13321static int intel_modeset_checks(struct drm_atomic_state *state)
054518dd 13322{
565602d7 13323 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
fac5e23e 13324 struct drm_i915_private *dev_priv = to_i915(state->dev);
565602d7
ML
13325 struct drm_crtc *crtc;
13326 struct drm_crtc_state *crtc_state;
13327 int ret = 0, i;
054518dd 13328
b359283a
ML
13329 if (!check_digital_port_conflicts(state)) {
13330 DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
13331 return -EINVAL;
13332 }
13333
565602d7
ML
13334 intel_state->modeset = true;
13335 intel_state->active_crtcs = dev_priv->active_crtcs;
13336
13337 for_each_crtc_in_state(state, crtc, crtc_state, i) {
13338 if (crtc_state->active)
13339 intel_state->active_crtcs |= 1 << i;
13340 else
13341 intel_state->active_crtcs &= ~(1 << i);
8b4a7d05
MR
13342
13343 if (crtc_state->active != crtc->state->active)
13344 intel_state->active_pipe_changes |= drm_crtc_mask(crtc);
565602d7
ML
13345 }
13346
054518dd
ACO
13347 /*
13348 * See if the config requires any additional preparation, e.g.
13349 * to adjust global state with pipes off. We need to do this
13350 * here so we can get the modeset_pipe updated config for the new
13351 * mode set on this crtc. For other crtcs we need to use the
13352 * adjusted_mode bits in the crtc directly.
13353 */
27c329ed 13354 if (dev_priv->display.modeset_calc_cdclk) {
c89e39f3 13355 if (!intel_state->cdclk_pll_vco)
63911d72 13356 intel_state->cdclk_pll_vco = dev_priv->cdclk_pll.vco;
b2045352
VS
13357 if (!intel_state->cdclk_pll_vco)
13358 intel_state->cdclk_pll_vco = dev_priv->skl_preferred_vco_freq;
c89e39f3 13359
27c329ed 13360 ret = dev_priv->display.modeset_calc_cdclk(state);
c89e39f3
CT
13361 if (ret < 0)
13362 return ret;
27c329ed 13363
c89e39f3 13364 if (intel_state->dev_cdclk != dev_priv->cdclk_freq ||
63911d72 13365 intel_state->cdclk_pll_vco != dev_priv->cdclk_pll.vco)
27c329ed
ML
13366 ret = intel_modeset_all_pipes(state);
13367
13368 if (ret < 0)
054518dd 13369 return ret;
e8788cbc
ML
13370
13371 DRM_DEBUG_KMS("New cdclk calculated to be atomic %u, actual %u\n",
13372 intel_state->cdclk, intel_state->dev_cdclk);
27c329ed 13373 } else
1a617b77 13374 to_intel_atomic_state(state)->cdclk = dev_priv->atomic_cdclk_freq;
054518dd 13375
ad421372 13376 intel_modeset_clear_plls(state);
054518dd 13377
565602d7 13378 if (IS_HASWELL(dev_priv))
ad421372 13379 return haswell_mode_set_planes_workaround(state);
99d736a2 13380
ad421372 13381 return 0;
c347a676
ACO
13382}
13383
aa363136
MR
13384/*
13385 * Handle calculation of various watermark data at the end of the atomic check
13386 * phase. The code here should be run after the per-crtc and per-plane 'check'
13387 * handlers to ensure that all derived state has been updated.
13388 */
55994c2c 13389static int calc_watermark_data(struct drm_atomic_state *state)
aa363136
MR
13390{
13391 struct drm_device *dev = state->dev;
98d39494 13392 struct drm_i915_private *dev_priv = to_i915(dev);
98d39494
MR
13393
13394 /* Is there platform-specific watermark information to calculate? */
13395 if (dev_priv->display.compute_global_watermarks)
55994c2c
MR
13396 return dev_priv->display.compute_global_watermarks(state);
13397
13398 return 0;
aa363136
MR
13399}
13400
74c090b1
ML
13401/**
13402 * intel_atomic_check - validate state object
13403 * @dev: drm device
13404 * @state: state to validate
13405 */
13406static int intel_atomic_check(struct drm_device *dev,
13407 struct drm_atomic_state *state)
c347a676 13408{
dd8b3bdb 13409 struct drm_i915_private *dev_priv = to_i915(dev);
aa363136 13410 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
c347a676
ACO
13411 struct drm_crtc *crtc;
13412 struct drm_crtc_state *crtc_state;
13413 int ret, i;
61333b60 13414 bool any_ms = false;
c347a676 13415
74c090b1 13416 ret = drm_atomic_helper_check_modeset(dev, state);
054518dd
ACO
13417 if (ret)
13418 return ret;
13419
c347a676 13420 for_each_crtc_in_state(state, crtc, crtc_state, i) {
cfb23ed6
ML
13421 struct intel_crtc_state *pipe_config =
13422 to_intel_crtc_state(crtc_state);
1ed51de9
DV
13423
13424 /* Catch I915_MODE_FLAG_INHERITED */
13425 if (crtc_state->mode.private_flags != crtc->state->mode.private_flags)
13426 crtc_state->mode_changed = true;
cfb23ed6 13427
af4a879e 13428 if (!needs_modeset(crtc_state))
c347a676
ACO
13429 continue;
13430
af4a879e
DV
13431 if (!crtc_state->enable) {
13432 any_ms = true;
cfb23ed6 13433 continue;
af4a879e 13434 }
cfb23ed6 13435
26495481
DV
13436 /* FIXME: For only active_changed we shouldn't need to do any
13437 * state recomputation at all. */
13438
1ed51de9
DV
13439 ret = drm_atomic_add_affected_connectors(state, crtc);
13440 if (ret)
13441 return ret;
b359283a 13442
cfb23ed6 13443 ret = intel_modeset_pipe_config(crtc, pipe_config);
25aa1c39
ML
13444 if (ret) {
13445 intel_dump_pipe_config(to_intel_crtc(crtc),
13446 pipe_config, "[failed]");
c347a676 13447 return ret;
25aa1c39 13448 }
c347a676 13449
73831236 13450 if (i915.fastboot &&
dd8b3bdb 13451 intel_pipe_config_compare(dev,
cfb23ed6 13452 to_intel_crtc_state(crtc->state),
1ed51de9 13453 pipe_config, true)) {
26495481 13454 crtc_state->mode_changed = false;
bfd16b2a 13455 to_intel_crtc_state(crtc_state)->update_pipe = true;
26495481
DV
13456 }
13457
af4a879e 13458 if (needs_modeset(crtc_state))
26495481 13459 any_ms = true;
cfb23ed6 13460
af4a879e
DV
13461 ret = drm_atomic_add_affected_planes(state, crtc);
13462 if (ret)
13463 return ret;
61333b60 13464
26495481
DV
13465 intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
13466 needs_modeset(crtc_state) ?
13467 "[modeset]" : "[fastset]");
c347a676
ACO
13468 }
13469
61333b60
ML
13470 if (any_ms) {
13471 ret = intel_modeset_checks(state);
13472
13473 if (ret)
13474 return ret;
27c329ed 13475 } else
dd8b3bdb 13476 intel_state->cdclk = dev_priv->cdclk_freq;
76305b1a 13477
dd8b3bdb 13478 ret = drm_atomic_helper_check_planes(dev, state);
aa363136
MR
13479 if (ret)
13480 return ret;
13481
f51be2e0 13482 intel_fbc_choose_crtc(dev_priv, state);
55994c2c 13483 return calc_watermark_data(state);
054518dd
ACO
13484}
13485
5008e874
ML
13486static int intel_atomic_prepare_commit(struct drm_device *dev,
13487 struct drm_atomic_state *state,
81072bfd 13488 bool nonblock)
5008e874 13489{
fac5e23e 13490 struct drm_i915_private *dev_priv = to_i915(dev);
7580d774 13491 struct drm_plane_state *plane_state;
5008e874 13492 struct drm_crtc_state *crtc_state;
7580d774 13493 struct drm_plane *plane;
5008e874
ML
13494 struct drm_crtc *crtc;
13495 int i, ret;
13496
5a21b665
DV
13497 for_each_crtc_in_state(state, crtc, crtc_state, i) {
13498 if (state->legacy_cursor_update)
a6747b73
ML
13499 continue;
13500
5a21b665
DV
13501 ret = intel_crtc_wait_for_pending_flips(crtc);
13502 if (ret)
13503 return ret;
5008e874 13504
5a21b665
DV
13505 if (atomic_read(&to_intel_crtc(crtc)->unpin_work_count) >= 2)
13506 flush_workqueue(dev_priv->wq);
d55dbd06
ML
13507 }
13508
f935675f
ML
13509 ret = mutex_lock_interruptible(&dev->struct_mutex);
13510 if (ret)
13511 return ret;
13512
5008e874 13513 ret = drm_atomic_helper_prepare_planes(dev, state);
f7e5838b 13514 mutex_unlock(&dev->struct_mutex);
7580d774 13515
21daaeee 13516 if (!ret && !nonblock) {
7580d774
ML
13517 for_each_plane_in_state(state, plane, plane_state, i) {
13518 struct intel_plane_state *intel_plane_state =
13519 to_intel_plane_state(plane_state);
13520
13521 if (!intel_plane_state->wait_req)
13522 continue;
13523
13524 ret = __i915_wait_request(intel_plane_state->wait_req,
299259a3 13525 true, NULL, NULL);
f7e5838b 13526 if (ret) {
f4457ae7
CW
13527 /* Any hang should be swallowed by the wait */
13528 WARN_ON(ret == -EIO);
f7e5838b
CW
13529 mutex_lock(&dev->struct_mutex);
13530 drm_atomic_helper_cleanup_planes(dev, state);
13531 mutex_unlock(&dev->struct_mutex);
7580d774 13532 break;
f7e5838b 13533 }
7580d774 13534 }
7580d774 13535 }
5008e874
ML
13536
13537 return ret;
13538}
13539
a2991414
ML
13540u32 intel_crtc_get_vblank_counter(struct intel_crtc *crtc)
13541{
13542 struct drm_device *dev = crtc->base.dev;
13543
13544 if (!dev->max_vblank_count)
13545 return drm_accurate_vblank_count(&crtc->base);
13546
13547 return dev->driver->get_vblank_counter(dev, crtc->pipe);
13548}
13549
5a21b665
DV
13550static void intel_atomic_wait_for_vblanks(struct drm_device *dev,
13551 struct drm_i915_private *dev_priv,
13552 unsigned crtc_mask)
e8861675 13553{
5a21b665
DV
13554 unsigned last_vblank_count[I915_MAX_PIPES];
13555 enum pipe pipe;
13556 int ret;
e8861675 13557
5a21b665
DV
13558 if (!crtc_mask)
13559 return;
e8861675 13560
5a21b665
DV
13561 for_each_pipe(dev_priv, pipe) {
13562 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
e8861675 13563
5a21b665 13564 if (!((1 << pipe) & crtc_mask))
e8861675
ML
13565 continue;
13566
5a21b665
DV
13567 ret = drm_crtc_vblank_get(crtc);
13568 if (WARN_ON(ret != 0)) {
13569 crtc_mask &= ~(1 << pipe);
13570 continue;
e8861675
ML
13571 }
13572
5a21b665 13573 last_vblank_count[pipe] = drm_crtc_vblank_count(crtc);
e8861675
ML
13574 }
13575
5a21b665
DV
13576 for_each_pipe(dev_priv, pipe) {
13577 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
13578 long lret;
e8861675 13579
5a21b665
DV
13580 if (!((1 << pipe) & crtc_mask))
13581 continue;
d55dbd06 13582
5a21b665
DV
13583 lret = wait_event_timeout(dev->vblank[pipe].queue,
13584 last_vblank_count[pipe] !=
13585 drm_crtc_vblank_count(crtc),
13586 msecs_to_jiffies(50));
d55dbd06 13587
5a21b665 13588 WARN(!lret, "pipe %c vblank wait timed out\n", pipe_name(pipe));
d55dbd06 13589
5a21b665 13590 drm_crtc_vblank_put(crtc);
d55dbd06
ML
13591 }
13592}
13593
5a21b665 13594static bool needs_vblank_wait(struct intel_crtc_state *crtc_state)
a6747b73 13595{
5a21b665
DV
13596 /* fb updated, need to unpin old fb */
13597 if (crtc_state->fb_changed)
13598 return true;
a6747b73 13599
5a21b665
DV
13600 /* wm changes, need vblank before final wm's */
13601 if (crtc_state->update_wm_post)
13602 return true;
a6747b73 13603
5a21b665
DV
13604 /*
13605 * cxsr is re-enabled after vblank.
13606 * This is already handled by crtc_state->update_wm_post,
13607 * but added for clarity.
13608 */
13609 if (crtc_state->disable_cxsr)
13610 return true;
a6747b73 13611
5a21b665 13612 return false;
e8861675
ML
13613}
13614
94f05024 13615static void intel_atomic_commit_tail(struct drm_atomic_state *state)
a6778b3c 13616{
94f05024 13617 struct drm_device *dev = state->dev;
565602d7 13618 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
fac5e23e 13619 struct drm_i915_private *dev_priv = to_i915(dev);
29ceb0e6 13620 struct drm_crtc_state *old_crtc_state;
7580d774 13621 struct drm_crtc *crtc;
5a21b665 13622 struct intel_crtc_state *intel_cstate;
94f05024
DV
13623 struct drm_plane *plane;
13624 struct drm_plane_state *plane_state;
5a21b665
DV
13625 bool hw_check = intel_state->modeset;
13626 unsigned long put_domains[I915_MAX_PIPES] = {};
13627 unsigned crtc_vblank_mask = 0;
94f05024 13628 int i, ret;
a6778b3c 13629
94f05024
DV
13630 for_each_plane_in_state(state, plane, plane_state, i) {
13631 struct intel_plane_state *intel_plane_state =
13632 to_intel_plane_state(plane_state);
ea0000f0 13633
94f05024
DV
13634 if (!intel_plane_state->wait_req)
13635 continue;
d4afb8cc 13636
94f05024
DV
13637 ret = __i915_wait_request(intel_plane_state->wait_req,
13638 true, NULL, NULL);
13639 /* EIO should be eaten, and we can't get interrupted in the
13640 * worker, and blocking commits have waited already. */
13641 WARN_ON(ret);
13642 }
1c5e19f8 13643
ea0000f0
DV
13644 drm_atomic_helper_wait_for_dependencies(state);
13645
565602d7
ML
13646 if (intel_state->modeset) {
13647 memcpy(dev_priv->min_pixclk, intel_state->min_pixclk,
13648 sizeof(intel_state->min_pixclk));
13649 dev_priv->active_crtcs = intel_state->active_crtcs;
1a617b77 13650 dev_priv->atomic_cdclk_freq = intel_state->cdclk;
5a21b665
DV
13651
13652 intel_display_power_get(dev_priv, POWER_DOMAIN_MODESET);
565602d7
ML
13653 }
13654
29ceb0e6 13655 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
a539205a
ML
13656 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13657
5a21b665
DV
13658 if (needs_modeset(crtc->state) ||
13659 to_intel_crtc_state(crtc->state)->update_pipe) {
13660 hw_check = true;
13661
13662 put_domains[to_intel_crtc(crtc)->pipe] =
13663 modeset_get_crtc_power_domains(crtc,
13664 to_intel_crtc_state(crtc->state));
13665 }
13666
61333b60
ML
13667 if (!needs_modeset(crtc->state))
13668 continue;
13669
29ceb0e6 13670 intel_pre_plane_update(to_intel_crtc_state(old_crtc_state));
460da916 13671
29ceb0e6
VS
13672 if (old_crtc_state->active) {
13673 intel_crtc_disable_planes(crtc, old_crtc_state->plane_mask);
a539205a 13674 dev_priv->display.crtc_disable(crtc);
eddfcbcd 13675 intel_crtc->active = false;
58f9c0bc 13676 intel_fbc_disable(intel_crtc);
eddfcbcd 13677 intel_disable_shared_dpll(intel_crtc);
9bbc8258
VS
13678
13679 /*
13680 * Underruns don't always raise
13681 * interrupts, so check manually.
13682 */
13683 intel_check_cpu_fifo_underruns(dev_priv);
13684 intel_check_pch_fifo_underruns(dev_priv);
b9001114
ML
13685
13686 if (!crtc->state->active)
13687 intel_update_watermarks(crtc);
a539205a 13688 }
b8cecdf5 13689 }
7758a113 13690
ea9d758d
DV
13691 /* Only after disabling all output pipelines that will be changed can we
13692 * update the the output configuration. */
4740b0f2 13693 intel_modeset_update_crtc_state(state);
f6e5b160 13694
565602d7 13695 if (intel_state->modeset) {
4740b0f2 13696 drm_atomic_helper_update_legacy_modeset_state(state->dev, state);
33c8df89
ML
13697
13698 if (dev_priv->display.modeset_commit_cdclk &&
c89e39f3 13699 (intel_state->dev_cdclk != dev_priv->cdclk_freq ||
63911d72 13700 intel_state->cdclk_pll_vco != dev_priv->cdclk_pll.vco))
33c8df89 13701 dev_priv->display.modeset_commit_cdclk(state);
f6d1973d 13702
c0ead703 13703 intel_modeset_verify_disabled(dev);
4740b0f2 13704 }
47fab737 13705
a6778b3c 13706 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
29ceb0e6 13707 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
f6ac4b2a
ML
13708 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13709 bool modeset = needs_modeset(crtc->state);
5a21b665
DV
13710 struct intel_crtc_state *pipe_config =
13711 to_intel_crtc_state(crtc->state);
9f836f90 13712
f6ac4b2a 13713 if (modeset && crtc->state->active) {
a539205a
ML
13714 update_scanline_offset(to_intel_crtc(crtc));
13715 dev_priv->display.crtc_enable(crtc);
13716 }
80715b2f 13717
1f7528c4
DV
13718 /* Complete events for now disable pipes here. */
13719 if (modeset && !crtc->state->active && crtc->state->event) {
13720 spin_lock_irq(&dev->event_lock);
13721 drm_crtc_send_vblank_event(crtc, crtc->state->event);
13722 spin_unlock_irq(&dev->event_lock);
13723
13724 crtc->state->event = NULL;
13725 }
13726
f6ac4b2a 13727 if (!modeset)
29ceb0e6 13728 intel_pre_plane_update(to_intel_crtc_state(old_crtc_state));
f6ac4b2a 13729
5a21b665
DV
13730 if (crtc->state->active &&
13731 drm_atomic_get_existing_plane_state(state, crtc->primary))
faf68d92 13732 intel_fbc_enable(intel_crtc, pipe_config, to_intel_plane_state(crtc->primary->state));
5a21b665 13733
1f7528c4 13734 if (crtc->state->active)
5a21b665 13735 drm_atomic_helper_commit_planes_on_crtc(old_crtc_state);
f6d1973d 13736
5a21b665
DV
13737 if (pipe_config->base.active && needs_vblank_wait(pipe_config))
13738 crtc_vblank_mask |= 1 << i;
177246a8
MR
13739 }
13740
94f05024
DV
13741 /* FIXME: We should call drm_atomic_helper_commit_hw_done() here
13742 * already, but still need the state for the delayed optimization. To
13743 * fix this:
13744 * - wrap the optimization/post_plane_update stuff into a per-crtc work.
13745 * - schedule that vblank worker _before_ calling hw_done
13746 * - at the start of commit_tail, cancel it _synchrously
13747 * - switch over to the vblank wait helper in the core after that since
13748 * we don't need out special handling any more.
13749 */
5a21b665
DV
13750 if (!state->legacy_cursor_update)
13751 intel_atomic_wait_for_vblanks(dev, dev_priv, crtc_vblank_mask);
13752
13753 /*
13754 * Now that the vblank has passed, we can go ahead and program the
13755 * optimal watermarks on platforms that need two-step watermark
13756 * programming.
13757 *
13758 * TODO: Move this (and other cleanup) to an async worker eventually.
13759 */
13760 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
13761 intel_cstate = to_intel_crtc_state(crtc->state);
13762
13763 if (dev_priv->display.optimize_watermarks)
13764 dev_priv->display.optimize_watermarks(intel_cstate);
13765 }
13766
13767 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
13768 intel_post_plane_update(to_intel_crtc_state(old_crtc_state));
13769
13770 if (put_domains[i])
13771 modeset_put_power_domains(dev_priv, put_domains[i]);
13772
13773 intel_modeset_verify_crtc(crtc, old_crtc_state, crtc->state);
13774 }
13775
94f05024
DV
13776 drm_atomic_helper_commit_hw_done(state);
13777
5a21b665
DV
13778 if (intel_state->modeset)
13779 intel_display_power_put(dev_priv, POWER_DOMAIN_MODESET);
13780
13781 mutex_lock(&dev->struct_mutex);
13782 drm_atomic_helper_cleanup_planes(dev, state);
13783 mutex_unlock(&dev->struct_mutex);
13784
ea0000f0
DV
13785 drm_atomic_helper_commit_cleanup_done(state);
13786
ee165b1a 13787 drm_atomic_state_free(state);
f30da187 13788
75714940
MK
13789 /* As one of the primary mmio accessors, KMS has a high likelihood
13790 * of triggering bugs in unclaimed access. After we finish
13791 * modesetting, see if an error has been flagged, and if so
13792 * enable debugging for the next modeset - and hope we catch
13793 * the culprit.
13794 *
13795 * XXX note that we assume display power is on at this point.
13796 * This might hold true now but we need to add pm helper to check
13797 * unclaimed only when the hardware is on, as atomic commits
13798 * can happen also when the device is completely off.
13799 */
13800 intel_uncore_arm_unclaimed_mmio_detection(dev_priv);
94f05024
DV
13801}
13802
13803static void intel_atomic_commit_work(struct work_struct *work)
13804{
13805 struct drm_atomic_state *state = container_of(work,
13806 struct drm_atomic_state,
13807 commit_work);
13808 intel_atomic_commit_tail(state);
13809}
13810
6c9c1b38
DV
13811static void intel_atomic_track_fbs(struct drm_atomic_state *state)
13812{
13813 struct drm_plane_state *old_plane_state;
13814 struct drm_plane *plane;
13815 struct drm_i915_gem_object *obj, *old_obj;
13816 struct intel_plane *intel_plane;
13817 int i;
13818
13819 mutex_lock(&state->dev->struct_mutex);
13820 for_each_plane_in_state(state, plane, old_plane_state, i) {
13821 obj = intel_fb_obj(plane->state->fb);
13822 old_obj = intel_fb_obj(old_plane_state->fb);
13823 intel_plane = to_intel_plane(plane);
13824
13825 i915_gem_track_fb(old_obj, obj, intel_plane->frontbuffer_bit);
13826 }
13827 mutex_unlock(&state->dev->struct_mutex);
13828}
13829
94f05024
DV
13830/**
13831 * intel_atomic_commit - commit validated state object
13832 * @dev: DRM device
13833 * @state: the top-level driver state object
13834 * @nonblock: nonblocking commit
13835 *
13836 * This function commits a top-level state object that has been validated
13837 * with drm_atomic_helper_check().
13838 *
13839 * FIXME: Atomic modeset support for i915 is not yet complete. At the moment
13840 * nonblocking commits are only safe for pure plane updates. Everything else
13841 * should work though.
13842 *
13843 * RETURNS
13844 * Zero for success or -errno.
13845 */
13846static int intel_atomic_commit(struct drm_device *dev,
13847 struct drm_atomic_state *state,
13848 bool nonblock)
13849{
13850 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
fac5e23e 13851 struct drm_i915_private *dev_priv = to_i915(dev);
94f05024
DV
13852 int ret = 0;
13853
13854 if (intel_state->modeset && nonblock) {
13855 DRM_DEBUG_KMS("nonblocking commit for modeset not yet implemented.\n");
13856 return -EINVAL;
13857 }
13858
13859 ret = drm_atomic_helper_setup_commit(state, nonblock);
13860 if (ret)
13861 return ret;
13862
13863 INIT_WORK(&state->commit_work, intel_atomic_commit_work);
13864
13865 ret = intel_atomic_prepare_commit(dev, state, nonblock);
13866 if (ret) {
13867 DRM_DEBUG_ATOMIC("Preparing state failed with %i\n", ret);
13868 return ret;
13869 }
13870
13871 drm_atomic_helper_swap_state(state, true);
13872 dev_priv->wm.distrust_bios_wm = false;
13873 dev_priv->wm.skl_results = intel_state->wm_results;
13874 intel_shared_dpll_commit(state);
6c9c1b38 13875 intel_atomic_track_fbs(state);
94f05024
DV
13876
13877 if (nonblock)
13878 queue_work(system_unbound_wq, &state->commit_work);
13879 else
13880 intel_atomic_commit_tail(state);
75714940 13881
74c090b1 13882 return 0;
7f27126e
JB
13883}
13884
c0c36b94
CW
13885void intel_crtc_restore_mode(struct drm_crtc *crtc)
13886{
83a57153
ACO
13887 struct drm_device *dev = crtc->dev;
13888 struct drm_atomic_state *state;
e694eb02 13889 struct drm_crtc_state *crtc_state;
2bfb4627 13890 int ret;
83a57153
ACO
13891
13892 state = drm_atomic_state_alloc(dev);
13893 if (!state) {
78108b7c
VS
13894 DRM_DEBUG_KMS("[CRTC:%d:%s] crtc restore failed, out of memory",
13895 crtc->base.id, crtc->name);
83a57153
ACO
13896 return;
13897 }
13898
e694eb02 13899 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
83a57153 13900
e694eb02
ML
13901retry:
13902 crtc_state = drm_atomic_get_crtc_state(state, crtc);
13903 ret = PTR_ERR_OR_ZERO(crtc_state);
13904 if (!ret) {
13905 if (!crtc_state->active)
13906 goto out;
83a57153 13907
e694eb02 13908 crtc_state->mode_changed = true;
74c090b1 13909 ret = drm_atomic_commit(state);
83a57153
ACO
13910 }
13911
e694eb02
ML
13912 if (ret == -EDEADLK) {
13913 drm_atomic_state_clear(state);
13914 drm_modeset_backoff(state->acquire_ctx);
13915 goto retry;
4ed9fb37 13916 }
4be07317 13917
2bfb4627 13918 if (ret)
e694eb02 13919out:
2bfb4627 13920 drm_atomic_state_free(state);
c0c36b94
CW
13921}
13922
25c5b266
DV
13923#undef for_each_intel_crtc_masked
13924
a8784875
BP
13925/*
13926 * FIXME: Remove this once i915 is fully DRIVER_ATOMIC by calling
13927 * drm_atomic_helper_legacy_gamma_set() directly.
13928 */
13929static int intel_atomic_legacy_gamma_set(struct drm_crtc *crtc,
13930 u16 *red, u16 *green, u16 *blue,
13931 uint32_t size)
13932{
13933 struct drm_device *dev = crtc->dev;
13934 struct drm_mode_config *config = &dev->mode_config;
13935 struct drm_crtc_state *state;
13936 int ret;
13937
13938 ret = drm_atomic_helper_legacy_gamma_set(crtc, red, green, blue, size);
13939 if (ret)
13940 return ret;
13941
13942 /*
13943 * Make sure we update the legacy properties so this works when
13944 * atomic is not enabled.
13945 */
13946
13947 state = crtc->state;
13948
13949 drm_object_property_set_value(&crtc->base,
13950 config->degamma_lut_property,
13951 (state->degamma_lut) ?
13952 state->degamma_lut->base.id : 0);
13953
13954 drm_object_property_set_value(&crtc->base,
13955 config->ctm_property,
13956 (state->ctm) ?
13957 state->ctm->base.id : 0);
13958
13959 drm_object_property_set_value(&crtc->base,
13960 config->gamma_lut_property,
13961 (state->gamma_lut) ?
13962 state->gamma_lut->base.id : 0);
13963
13964 return 0;
13965}
13966
f6e5b160 13967static const struct drm_crtc_funcs intel_crtc_funcs = {
a8784875 13968 .gamma_set = intel_atomic_legacy_gamma_set,
74c090b1 13969 .set_config = drm_atomic_helper_set_config,
82cf435b 13970 .set_property = drm_atomic_helper_crtc_set_property,
f6e5b160 13971 .destroy = intel_crtc_destroy,
527b6abe 13972 .page_flip = intel_crtc_page_flip,
1356837e
MR
13973 .atomic_duplicate_state = intel_crtc_duplicate_state,
13974 .atomic_destroy_state = intel_crtc_destroy_state,
f6e5b160
CW
13975};
13976
6beb8c23
MR
13977/**
13978 * intel_prepare_plane_fb - Prepare fb for usage on plane
13979 * @plane: drm plane to prepare for
13980 * @fb: framebuffer to prepare for presentation
13981 *
13982 * Prepares a framebuffer for usage on a display plane. Generally this
13983 * involves pinning the underlying object and updating the frontbuffer tracking
13984 * bits. Some older platforms need special physical address handling for
13985 * cursor planes.
13986 *
f935675f
ML
13987 * Must be called with struct_mutex held.
13988 *
6beb8c23
MR
13989 * Returns 0 on success, negative error code on failure.
13990 */
13991int
13992intel_prepare_plane_fb(struct drm_plane *plane,
d136dfee 13993 const struct drm_plane_state *new_state)
465c120c
MR
13994{
13995 struct drm_device *dev = plane->dev;
844f9111 13996 struct drm_framebuffer *fb = new_state->fb;
6beb8c23 13997 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
1ee49399 13998 struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->state->fb);
c37efb99 13999 struct reservation_object *resv;
6beb8c23 14000 int ret = 0;
465c120c 14001
1ee49399 14002 if (!obj && !old_obj)
465c120c
MR
14003 return 0;
14004
5008e874
ML
14005 if (old_obj) {
14006 struct drm_crtc_state *crtc_state =
14007 drm_atomic_get_existing_crtc_state(new_state->state, plane->state->crtc);
14008
14009 /* Big Hammer, we also need to ensure that any pending
14010 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
14011 * current scanout is retired before unpinning the old
14012 * framebuffer. Note that we rely on userspace rendering
14013 * into the buffer attached to the pipe they are waiting
14014 * on. If not, userspace generates a GPU hang with IPEHR
14015 * point to the MI_WAIT_FOR_EVENT.
14016 *
14017 * This should only fail upon a hung GPU, in which case we
14018 * can safely continue.
14019 */
14020 if (needs_modeset(crtc_state))
14021 ret = i915_gem_object_wait_rendering(old_obj, true);
f4457ae7
CW
14022 if (ret) {
14023 /* GPU hangs should have been swallowed by the wait */
14024 WARN_ON(ret == -EIO);
f935675f 14025 return ret;
f4457ae7 14026 }
5008e874
ML
14027 }
14028
c37efb99
CW
14029 if (!obj)
14030 return 0;
14031
5a21b665 14032 /* For framebuffer backed by dmabuf, wait for fence */
c37efb99
CW
14033 resv = i915_gem_object_get_dmabuf_resv(obj);
14034 if (resv) {
5a21b665
DV
14035 long lret;
14036
c37efb99 14037 lret = reservation_object_wait_timeout_rcu(resv, false, true,
5a21b665
DV
14038 MAX_SCHEDULE_TIMEOUT);
14039 if (lret == -ERESTARTSYS)
14040 return lret;
14041
14042 WARN(lret < 0, "waiting returns %li\n", lret);
14043 }
14044
c37efb99 14045 if (plane->type == DRM_PLANE_TYPE_CURSOR &&
6beb8c23
MR
14046 INTEL_INFO(dev)->cursor_needs_physical) {
14047 int align = IS_I830(dev) ? 16 * 1024 : 256;
14048 ret = i915_gem_object_attach_phys(obj, align);
14049 if (ret)
14050 DRM_DEBUG_KMS("failed to attach phys object\n");
14051 } else {
3465c580 14052 ret = intel_pin_and_fence_fb_obj(fb, new_state->rotation);
6beb8c23 14053 }
465c120c 14054
c37efb99 14055 if (ret == 0) {
6c9c1b38
DV
14056 struct intel_plane_state *plane_state =
14057 to_intel_plane_state(new_state);
7580d774 14058
6c9c1b38
DV
14059 i915_gem_request_assign(&plane_state->wait_req,
14060 obj->last_write_req);
7580d774 14061 }
fdd508a6 14062
6beb8c23
MR
14063 return ret;
14064}
14065
38f3ce3a
MR
14066/**
14067 * intel_cleanup_plane_fb - Cleans up an fb after plane use
14068 * @plane: drm plane to clean up for
14069 * @fb: old framebuffer that was on plane
14070 *
14071 * Cleans up a framebuffer that has just been removed from a plane.
f935675f
ML
14072 *
14073 * Must be called with struct_mutex held.
38f3ce3a
MR
14074 */
14075void
14076intel_cleanup_plane_fb(struct drm_plane *plane,
d136dfee 14077 const struct drm_plane_state *old_state)
38f3ce3a
MR
14078{
14079 struct drm_device *dev = plane->dev;
7580d774 14080 struct intel_plane_state *old_intel_state;
1ee49399
ML
14081 struct drm_i915_gem_object *old_obj = intel_fb_obj(old_state->fb);
14082 struct drm_i915_gem_object *obj = intel_fb_obj(plane->state->fb);
38f3ce3a 14083
7580d774
ML
14084 old_intel_state = to_intel_plane_state(old_state);
14085
1ee49399 14086 if (!obj && !old_obj)
38f3ce3a
MR
14087 return;
14088
1ee49399
ML
14089 if (old_obj && (plane->type != DRM_PLANE_TYPE_CURSOR ||
14090 !INTEL_INFO(dev)->cursor_needs_physical))
3465c580 14091 intel_unpin_fb_obj(old_state->fb, old_state->rotation);
1ee49399 14092
7580d774 14093 i915_gem_request_assign(&old_intel_state->wait_req, NULL);
465c120c
MR
14094}
14095
6156a456
CK
14096int
14097skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
14098{
14099 int max_scale;
6156a456
CK
14100 int crtc_clock, cdclk;
14101
bf8a0af0 14102 if (!intel_crtc || !crtc_state->base.enable)
6156a456
CK
14103 return DRM_PLANE_HELPER_NO_SCALING;
14104
6156a456 14105 crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
27c329ed 14106 cdclk = to_intel_atomic_state(crtc_state->base.state)->cdclk;
6156a456 14107
54bf1ce6 14108 if (WARN_ON_ONCE(!crtc_clock || cdclk < crtc_clock))
6156a456
CK
14109 return DRM_PLANE_HELPER_NO_SCALING;
14110
14111 /*
14112 * skl max scale is lower of:
14113 * close to 3 but not 3, -1 is for that purpose
14114 * or
14115 * cdclk/crtc_clock
14116 */
14117 max_scale = min((1 << 16) * 3 - 1, (1 << 8) * ((cdclk << 8) / crtc_clock));
14118
14119 return max_scale;
14120}
14121
465c120c 14122static int
3c692a41 14123intel_check_primary_plane(struct drm_plane *plane,
061e4b8d 14124 struct intel_crtc_state *crtc_state,
3c692a41
GP
14125 struct intel_plane_state *state)
14126{
2b875c22
MR
14127 struct drm_crtc *crtc = state->base.crtc;
14128 struct drm_framebuffer *fb = state->base.fb;
6156a456 14129 int min_scale = DRM_PLANE_HELPER_NO_SCALING;
061e4b8d
ML
14130 int max_scale = DRM_PLANE_HELPER_NO_SCALING;
14131 bool can_position = false;
465c120c 14132
693bdc28
VS
14133 if (INTEL_INFO(plane->dev)->gen >= 9) {
14134 /* use scaler when colorkey is not required */
14135 if (state->ckey.flags == I915_SET_COLORKEY_NONE) {
14136 min_scale = 1;
14137 max_scale = skl_max_scale(to_intel_crtc(crtc), crtc_state);
14138 }
d8106366 14139 can_position = true;
6156a456 14140 }
d8106366 14141
061e4b8d
ML
14142 return drm_plane_helper_check_update(plane, crtc, fb, &state->src,
14143 &state->dst, &state->clip,
9b8b013d 14144 state->base.rotation,
da20eabd
ML
14145 min_scale, max_scale,
14146 can_position, true,
14147 &state->visible);
14af293f
GP
14148}
14149
5a21b665
DV
14150static void intel_begin_crtc_commit(struct drm_crtc *crtc,
14151 struct drm_crtc_state *old_crtc_state)
14152{
14153 struct drm_device *dev = crtc->dev;
14154 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
14155 struct intel_crtc_state *old_intel_state =
14156 to_intel_crtc_state(old_crtc_state);
14157 bool modeset = needs_modeset(crtc->state);
14158
14159 /* Perform vblank evasion around commit operation */
14160 intel_pipe_update_start(intel_crtc);
14161
14162 if (modeset)
14163 return;
14164
14165 if (crtc->state->color_mgmt_changed || to_intel_crtc_state(crtc->state)->update_pipe) {
14166 intel_color_set_csc(crtc->state);
14167 intel_color_load_luts(crtc->state);
14168 }
14169
14170 if (to_intel_crtc_state(crtc->state)->update_pipe)
14171 intel_update_pipe_config(intel_crtc, old_intel_state);
14172 else if (INTEL_INFO(dev)->gen >= 9)
14173 skl_detach_scalers(intel_crtc);
14174}
14175
14176static void intel_finish_crtc_commit(struct drm_crtc *crtc,
14177 struct drm_crtc_state *old_crtc_state)
14178{
14179 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
14180
14181 intel_pipe_update_end(intel_crtc, NULL);
14182}
14183
cf4c7c12 14184/**
4a3b8769
MR
14185 * intel_plane_destroy - destroy a plane
14186 * @plane: plane to destroy
cf4c7c12 14187 *
4a3b8769
MR
14188 * Common destruction function for all types of planes (primary, cursor,
14189 * sprite).
cf4c7c12 14190 */
4a3b8769 14191void intel_plane_destroy(struct drm_plane *plane)
465c120c 14192{
69ae561f
VS
14193 if (!plane)
14194 return;
14195
465c120c 14196 drm_plane_cleanup(plane);
69ae561f 14197 kfree(to_intel_plane(plane));
465c120c
MR
14198}
14199
65a3fea0 14200const struct drm_plane_funcs intel_plane_funcs = {
70a101f8
MR
14201 .update_plane = drm_atomic_helper_update_plane,
14202 .disable_plane = drm_atomic_helper_disable_plane,
3d7d6510 14203 .destroy = intel_plane_destroy,
c196e1d6 14204 .set_property = drm_atomic_helper_plane_set_property,
a98b3431
MR
14205 .atomic_get_property = intel_plane_atomic_get_property,
14206 .atomic_set_property = intel_plane_atomic_set_property,
ea2c67bb
MR
14207 .atomic_duplicate_state = intel_plane_duplicate_state,
14208 .atomic_destroy_state = intel_plane_destroy_state,
14209
465c120c
MR
14210};
14211
14212static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
14213 int pipe)
14214{
fca0ce2a
VS
14215 struct intel_plane *primary = NULL;
14216 struct intel_plane_state *state = NULL;
465c120c 14217 const uint32_t *intel_primary_formats;
45e3743a 14218 unsigned int num_formats;
fca0ce2a 14219 int ret;
465c120c
MR
14220
14221 primary = kzalloc(sizeof(*primary), GFP_KERNEL);
fca0ce2a
VS
14222 if (!primary)
14223 goto fail;
465c120c 14224
8e7d688b 14225 state = intel_create_plane_state(&primary->base);
fca0ce2a
VS
14226 if (!state)
14227 goto fail;
8e7d688b 14228 primary->base.state = &state->base;
ea2c67bb 14229
465c120c
MR
14230 primary->can_scale = false;
14231 primary->max_downscale = 1;
6156a456
CK
14232 if (INTEL_INFO(dev)->gen >= 9) {
14233 primary->can_scale = true;
af99ceda 14234 state->scaler_id = -1;
6156a456 14235 }
465c120c
MR
14236 primary->pipe = pipe;
14237 primary->plane = pipe;
a9ff8714 14238 primary->frontbuffer_bit = INTEL_FRONTBUFFER_PRIMARY(pipe);
c59cb179 14239 primary->check_plane = intel_check_primary_plane;
465c120c
MR
14240 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
14241 primary->plane = !pipe;
14242
6c0fd451
DL
14243 if (INTEL_INFO(dev)->gen >= 9) {
14244 intel_primary_formats = skl_primary_formats;
14245 num_formats = ARRAY_SIZE(skl_primary_formats);
a8d201af
ML
14246
14247 primary->update_plane = skylake_update_primary_plane;
14248 primary->disable_plane = skylake_disable_primary_plane;
14249 } else if (HAS_PCH_SPLIT(dev)) {
14250 intel_primary_formats = i965_primary_formats;
14251 num_formats = ARRAY_SIZE(i965_primary_formats);
14252
14253 primary->update_plane = ironlake_update_primary_plane;
14254 primary->disable_plane = i9xx_disable_primary_plane;
6c0fd451 14255 } else if (INTEL_INFO(dev)->gen >= 4) {
568db4f2
DL
14256 intel_primary_formats = i965_primary_formats;
14257 num_formats = ARRAY_SIZE(i965_primary_formats);
a8d201af
ML
14258
14259 primary->update_plane = i9xx_update_primary_plane;
14260 primary->disable_plane = i9xx_disable_primary_plane;
6c0fd451
DL
14261 } else {
14262 intel_primary_formats = i8xx_primary_formats;
14263 num_formats = ARRAY_SIZE(i8xx_primary_formats);
a8d201af
ML
14264
14265 primary->update_plane = i9xx_update_primary_plane;
14266 primary->disable_plane = i9xx_disable_primary_plane;
465c120c
MR
14267 }
14268
38573dc1
VS
14269 if (INTEL_INFO(dev)->gen >= 9)
14270 ret = drm_universal_plane_init(dev, &primary->base, 0,
14271 &intel_plane_funcs,
14272 intel_primary_formats, num_formats,
14273 DRM_PLANE_TYPE_PRIMARY,
14274 "plane 1%c", pipe_name(pipe));
14275 else if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
14276 ret = drm_universal_plane_init(dev, &primary->base, 0,
14277 &intel_plane_funcs,
14278 intel_primary_formats, num_formats,
14279 DRM_PLANE_TYPE_PRIMARY,
14280 "primary %c", pipe_name(pipe));
14281 else
14282 ret = drm_universal_plane_init(dev, &primary->base, 0,
14283 &intel_plane_funcs,
14284 intel_primary_formats, num_formats,
14285 DRM_PLANE_TYPE_PRIMARY,
14286 "plane %c", plane_name(primary->plane));
fca0ce2a
VS
14287 if (ret)
14288 goto fail;
48404c1e 14289
3b7a5119
SJ
14290 if (INTEL_INFO(dev)->gen >= 4)
14291 intel_create_rotation_property(dev, primary);
48404c1e 14292
ea2c67bb
MR
14293 drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
14294
465c120c 14295 return &primary->base;
fca0ce2a
VS
14296
14297fail:
14298 kfree(state);
14299 kfree(primary);
14300
14301 return NULL;
465c120c
MR
14302}
14303
3b7a5119
SJ
14304void intel_create_rotation_property(struct drm_device *dev, struct intel_plane *plane)
14305{
14306 if (!dev->mode_config.rotation_property) {
14307 unsigned long flags = BIT(DRM_ROTATE_0) |
14308 BIT(DRM_ROTATE_180);
14309
14310 if (INTEL_INFO(dev)->gen >= 9)
14311 flags |= BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_270);
14312
14313 dev->mode_config.rotation_property =
14314 drm_mode_create_rotation_property(dev, flags);
14315 }
14316 if (dev->mode_config.rotation_property)
14317 drm_object_attach_property(&plane->base.base,
14318 dev->mode_config.rotation_property,
14319 plane->base.state->rotation);
14320}
14321
3d7d6510 14322static int
852e787c 14323intel_check_cursor_plane(struct drm_plane *plane,
061e4b8d 14324 struct intel_crtc_state *crtc_state,
852e787c 14325 struct intel_plane_state *state)
3d7d6510 14326{
061e4b8d 14327 struct drm_crtc *crtc = crtc_state->base.crtc;
2b875c22 14328 struct drm_framebuffer *fb = state->base.fb;
757f9a3e 14329 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
b29ec92c 14330 enum pipe pipe = to_intel_plane(plane)->pipe;
757f9a3e
GP
14331 unsigned stride;
14332 int ret;
3d7d6510 14333
061e4b8d
ML
14334 ret = drm_plane_helper_check_update(plane, crtc, fb, &state->src,
14335 &state->dst, &state->clip,
9b8b013d 14336 state->base.rotation,
3d7d6510
MR
14337 DRM_PLANE_HELPER_NO_SCALING,
14338 DRM_PLANE_HELPER_NO_SCALING,
852e787c 14339 true, true, &state->visible);
757f9a3e
GP
14340 if (ret)
14341 return ret;
14342
757f9a3e
GP
14343 /* if we want to turn off the cursor ignore width and height */
14344 if (!obj)
da20eabd 14345 return 0;
757f9a3e 14346
757f9a3e 14347 /* Check for which cursor types we support */
061e4b8d 14348 if (!cursor_size_ok(plane->dev, state->base.crtc_w, state->base.crtc_h)) {
ea2c67bb
MR
14349 DRM_DEBUG("Cursor dimension %dx%d not supported\n",
14350 state->base.crtc_w, state->base.crtc_h);
757f9a3e
GP
14351 return -EINVAL;
14352 }
14353
ea2c67bb
MR
14354 stride = roundup_pow_of_two(state->base.crtc_w) * 4;
14355 if (obj->base.size < stride * state->base.crtc_h) {
757f9a3e
GP
14356 DRM_DEBUG_KMS("buffer is too small\n");
14357 return -ENOMEM;
14358 }
14359
3a656b54 14360 if (fb->modifier[0] != DRM_FORMAT_MOD_NONE) {
757f9a3e 14361 DRM_DEBUG_KMS("cursor cannot be tiled\n");
da20eabd 14362 return -EINVAL;
32b7eeec
MR
14363 }
14364
b29ec92c
VS
14365 /*
14366 * There's something wrong with the cursor on CHV pipe C.
14367 * If it straddles the left edge of the screen then
14368 * moving it away from the edge or disabling it often
14369 * results in a pipe underrun, and often that can lead to
14370 * dead pipe (constant underrun reported, and it scans
14371 * out just a solid color). To recover from that, the
14372 * display power well must be turned off and on again.
14373 * Refuse the put the cursor into that compromised position.
14374 */
14375 if (IS_CHERRYVIEW(plane->dev) && pipe == PIPE_C &&
14376 state->visible && state->base.crtc_x < 0) {
14377 DRM_DEBUG_KMS("CHV cursor C not allowed to straddle the left screen edge\n");
14378 return -EINVAL;
14379 }
14380
da20eabd 14381 return 0;
852e787c 14382}
3d7d6510 14383
a8ad0d8e
ML
14384static void
14385intel_disable_cursor_plane(struct drm_plane *plane,
7fabf5ef 14386 struct drm_crtc *crtc)
a8ad0d8e 14387{
f2858021
ML
14388 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
14389
14390 intel_crtc->cursor_addr = 0;
55a08b3f 14391 intel_crtc_update_cursor(crtc, NULL);
a8ad0d8e
ML
14392}
14393
f4a2cf29 14394static void
55a08b3f
ML
14395intel_update_cursor_plane(struct drm_plane *plane,
14396 const struct intel_crtc_state *crtc_state,
14397 const struct intel_plane_state *state)
852e787c 14398{
55a08b3f
ML
14399 struct drm_crtc *crtc = crtc_state->base.crtc;
14400 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ea2c67bb 14401 struct drm_device *dev = plane->dev;
2b875c22 14402 struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
a912f12f 14403 uint32_t addr;
852e787c 14404
f4a2cf29 14405 if (!obj)
a912f12f 14406 addr = 0;
f4a2cf29 14407 else if (!INTEL_INFO(dev)->cursor_needs_physical)
a912f12f 14408 addr = i915_gem_obj_ggtt_offset(obj);
f4a2cf29 14409 else
a912f12f 14410 addr = obj->phys_handle->busaddr;
852e787c 14411
a912f12f 14412 intel_crtc->cursor_addr = addr;
55a08b3f 14413 intel_crtc_update_cursor(crtc, state);
852e787c
GP
14414}
14415
3d7d6510
MR
14416static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
14417 int pipe)
14418{
fca0ce2a
VS
14419 struct intel_plane *cursor = NULL;
14420 struct intel_plane_state *state = NULL;
14421 int ret;
3d7d6510
MR
14422
14423 cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
fca0ce2a
VS
14424 if (!cursor)
14425 goto fail;
3d7d6510 14426
8e7d688b 14427 state = intel_create_plane_state(&cursor->base);
fca0ce2a
VS
14428 if (!state)
14429 goto fail;
8e7d688b 14430 cursor->base.state = &state->base;
ea2c67bb 14431
3d7d6510
MR
14432 cursor->can_scale = false;
14433 cursor->max_downscale = 1;
14434 cursor->pipe = pipe;
14435 cursor->plane = pipe;
a9ff8714 14436 cursor->frontbuffer_bit = INTEL_FRONTBUFFER_CURSOR(pipe);
c59cb179 14437 cursor->check_plane = intel_check_cursor_plane;
55a08b3f 14438 cursor->update_plane = intel_update_cursor_plane;
a8ad0d8e 14439 cursor->disable_plane = intel_disable_cursor_plane;
3d7d6510 14440
fca0ce2a
VS
14441 ret = drm_universal_plane_init(dev, &cursor->base, 0,
14442 &intel_plane_funcs,
14443 intel_cursor_formats,
14444 ARRAY_SIZE(intel_cursor_formats),
38573dc1
VS
14445 DRM_PLANE_TYPE_CURSOR,
14446 "cursor %c", pipe_name(pipe));
fca0ce2a
VS
14447 if (ret)
14448 goto fail;
4398ad45
VS
14449
14450 if (INTEL_INFO(dev)->gen >= 4) {
14451 if (!dev->mode_config.rotation_property)
14452 dev->mode_config.rotation_property =
14453 drm_mode_create_rotation_property(dev,
14454 BIT(DRM_ROTATE_0) |
14455 BIT(DRM_ROTATE_180));
14456 if (dev->mode_config.rotation_property)
14457 drm_object_attach_property(&cursor->base.base,
14458 dev->mode_config.rotation_property,
8e7d688b 14459 state->base.rotation);
4398ad45
VS
14460 }
14461
af99ceda
CK
14462 if (INTEL_INFO(dev)->gen >=9)
14463 state->scaler_id = -1;
14464
ea2c67bb
MR
14465 drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
14466
3d7d6510 14467 return &cursor->base;
fca0ce2a
VS
14468
14469fail:
14470 kfree(state);
14471 kfree(cursor);
14472
14473 return NULL;
3d7d6510
MR
14474}
14475
549e2bfb
CK
14476static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
14477 struct intel_crtc_state *crtc_state)
14478{
14479 int i;
14480 struct intel_scaler *intel_scaler;
14481 struct intel_crtc_scaler_state *scaler_state = &crtc_state->scaler_state;
14482
14483 for (i = 0; i < intel_crtc->num_scalers; i++) {
14484 intel_scaler = &scaler_state->scalers[i];
14485 intel_scaler->in_use = 0;
549e2bfb
CK
14486 intel_scaler->mode = PS_SCALER_MODE_DYN;
14487 }
14488
14489 scaler_state->scaler_id = -1;
14490}
14491
b358d0a6 14492static void intel_crtc_init(struct drm_device *dev, int pipe)
79e53945 14493{
fac5e23e 14494 struct drm_i915_private *dev_priv = to_i915(dev);
79e53945 14495 struct intel_crtc *intel_crtc;
f5de6e07 14496 struct intel_crtc_state *crtc_state = NULL;
3d7d6510
MR
14497 struct drm_plane *primary = NULL;
14498 struct drm_plane *cursor = NULL;
8563b1e8 14499 int ret;
79e53945 14500
955382f3 14501 intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
79e53945
JB
14502 if (intel_crtc == NULL)
14503 return;
14504
f5de6e07
ACO
14505 crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
14506 if (!crtc_state)
14507 goto fail;
550acefd
ACO
14508 intel_crtc->config = crtc_state;
14509 intel_crtc->base.state = &crtc_state->base;
07878248 14510 crtc_state->base.crtc = &intel_crtc->base;
f5de6e07 14511
549e2bfb
CK
14512 /* initialize shared scalers */
14513 if (INTEL_INFO(dev)->gen >= 9) {
14514 if (pipe == PIPE_C)
14515 intel_crtc->num_scalers = 1;
14516 else
14517 intel_crtc->num_scalers = SKL_NUM_SCALERS;
14518
14519 skl_init_scalers(dev, intel_crtc, crtc_state);
14520 }
14521
465c120c 14522 primary = intel_primary_plane_create(dev, pipe);
3d7d6510
MR
14523 if (!primary)
14524 goto fail;
14525
14526 cursor = intel_cursor_plane_create(dev, pipe);
14527 if (!cursor)
14528 goto fail;
14529
465c120c 14530 ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
4d5d72b7
VS
14531 cursor, &intel_crtc_funcs,
14532 "pipe %c", pipe_name(pipe));
3d7d6510
MR
14533 if (ret)
14534 goto fail;
79e53945 14535
1f1c2e24
VS
14536 /*
14537 * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
8c0f92e1 14538 * is hooked to pipe B. Hence we want plane A feeding pipe B.
1f1c2e24 14539 */
80824003
JB
14540 intel_crtc->pipe = pipe;
14541 intel_crtc->plane = pipe;
3a77c4c4 14542 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
28c97730 14543 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
e2e767ab 14544 intel_crtc->plane = !pipe;
80824003
JB
14545 }
14546
4b0e333e
CW
14547 intel_crtc->cursor_base = ~0;
14548 intel_crtc->cursor_cntl = ~0;
dc41c154 14549 intel_crtc->cursor_size = ~0;
8d7849db 14550
852eb00d
VS
14551 intel_crtc->wm.cxsr_allowed = true;
14552
22fd0fab
JB
14553 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
14554 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
14555 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
14556 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
14557
79e53945 14558 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
87b6b101 14559
8563b1e8
LL
14560 intel_color_init(&intel_crtc->base);
14561
87b6b101 14562 WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
3d7d6510
MR
14563 return;
14564
14565fail:
69ae561f
VS
14566 intel_plane_destroy(primary);
14567 intel_plane_destroy(cursor);
f5de6e07 14568 kfree(crtc_state);
3d7d6510 14569 kfree(intel_crtc);
79e53945
JB
14570}
14571
752aa88a
JB
14572enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
14573{
14574 struct drm_encoder *encoder = connector->base.encoder;
6e9f798d 14575 struct drm_device *dev = connector->base.dev;
752aa88a 14576
51fd371b 14577 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
752aa88a 14578
d3babd3f 14579 if (!encoder || WARN_ON(!encoder->crtc))
752aa88a
JB
14580 return INVALID_PIPE;
14581
14582 return to_intel_crtc(encoder->crtc)->pipe;
14583}
14584
08d7b3d1 14585int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
05394f39 14586 struct drm_file *file)
08d7b3d1 14587{
08d7b3d1 14588 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
7707e653 14589 struct drm_crtc *drmmode_crtc;
c05422d5 14590 struct intel_crtc *crtc;
08d7b3d1 14591
7707e653 14592 drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
71240ed2 14593 if (!drmmode_crtc)
3f2c2057 14594 return -ENOENT;
08d7b3d1 14595
7707e653 14596 crtc = to_intel_crtc(drmmode_crtc);
c05422d5 14597 pipe_from_crtc_id->pipe = crtc->pipe;
08d7b3d1 14598
c05422d5 14599 return 0;
08d7b3d1
CW
14600}
14601
66a9278e 14602static int intel_encoder_clones(struct intel_encoder *encoder)
79e53945 14603{
66a9278e
DV
14604 struct drm_device *dev = encoder->base.dev;
14605 struct intel_encoder *source_encoder;
79e53945 14606 int index_mask = 0;
79e53945
JB
14607 int entry = 0;
14608
b2784e15 14609 for_each_intel_encoder(dev, source_encoder) {
bc079e8b 14610 if (encoders_cloneable(encoder, source_encoder))
66a9278e
DV
14611 index_mask |= (1 << entry);
14612
79e53945
JB
14613 entry++;
14614 }
4ef69c7a 14615
79e53945
JB
14616 return index_mask;
14617}
14618
4d302442
CW
14619static bool has_edp_a(struct drm_device *dev)
14620{
fac5e23e 14621 struct drm_i915_private *dev_priv = to_i915(dev);
4d302442
CW
14622
14623 if (!IS_MOBILE(dev))
14624 return false;
14625
14626 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
14627 return false;
14628
e3589908 14629 if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
4d302442
CW
14630 return false;
14631
14632 return true;
14633}
14634
84b4e042
JB
14635static bool intel_crt_present(struct drm_device *dev)
14636{
fac5e23e 14637 struct drm_i915_private *dev_priv = to_i915(dev);
84b4e042 14638
884497ed
DL
14639 if (INTEL_INFO(dev)->gen >= 9)
14640 return false;
14641
cf404ce4 14642 if (IS_HSW_ULT(dev) || IS_BDW_ULT(dev))
84b4e042
JB
14643 return false;
14644
14645 if (IS_CHERRYVIEW(dev))
14646 return false;
14647
65e472e4
VS
14648 if (HAS_PCH_LPT_H(dev) && I915_READ(SFUSE_STRAP) & SFUSE_STRAP_CRT_DISABLED)
14649 return false;
14650
70ac54d0
VS
14651 /* DDI E can't be used if DDI A requires 4 lanes */
14652 if (HAS_DDI(dev) && I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_A_4_LANES)
14653 return false;
14654
e4abb733 14655 if (!dev_priv->vbt.int_crt_support)
84b4e042
JB
14656 return false;
14657
14658 return true;
14659}
14660
79e53945
JB
14661static void intel_setup_outputs(struct drm_device *dev)
14662{
fac5e23e 14663 struct drm_i915_private *dev_priv = to_i915(dev);
4ef69c7a 14664 struct intel_encoder *encoder;
cb0953d7 14665 bool dpd_is_edp = false;
79e53945 14666
97a824e1
ID
14667 /*
14668 * intel_edp_init_connector() depends on this completing first, to
14669 * prevent the registeration of both eDP and LVDS and the incorrect
14670 * sharing of the PPS.
14671 */
c9093354 14672 intel_lvds_init(dev);
79e53945 14673
84b4e042 14674 if (intel_crt_present(dev))
79935fca 14675 intel_crt_init(dev);
cb0953d7 14676
c776eb2e
VK
14677 if (IS_BROXTON(dev)) {
14678 /*
14679 * FIXME: Broxton doesn't support port detection via the
14680 * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
14681 * detect the ports.
14682 */
14683 intel_ddi_init(dev, PORT_A);
14684 intel_ddi_init(dev, PORT_B);
14685 intel_ddi_init(dev, PORT_C);
c6c794a2
SS
14686
14687 intel_dsi_init(dev);
c776eb2e 14688 } else if (HAS_DDI(dev)) {
0e72a5b5
ED
14689 int found;
14690
de31facd
JB
14691 /*
14692 * Haswell uses DDI functions to detect digital outputs.
14693 * On SKL pre-D0 the strap isn't connected, so we assume
14694 * it's there.
14695 */
77179400 14696 found = I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_INIT_DISPLAY_DETECTED;
de31facd 14697 /* WaIgnoreDDIAStrap: skl */
ef11bdb3 14698 if (found || IS_SKYLAKE(dev) || IS_KABYLAKE(dev))
0e72a5b5
ED
14699 intel_ddi_init(dev, PORT_A);
14700
14701 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
14702 * register */
14703 found = I915_READ(SFUSE_STRAP);
14704
14705 if (found & SFUSE_STRAP_DDIB_DETECTED)
14706 intel_ddi_init(dev, PORT_B);
14707 if (found & SFUSE_STRAP_DDIC_DETECTED)
14708 intel_ddi_init(dev, PORT_C);
14709 if (found & SFUSE_STRAP_DDID_DETECTED)
14710 intel_ddi_init(dev, PORT_D);
2800e4c2
RV
14711 /*
14712 * On SKL we don't have a way to detect DDI-E so we rely on VBT.
14713 */
ef11bdb3 14714 if ((IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) &&
2800e4c2
RV
14715 (dev_priv->vbt.ddi_port_info[PORT_E].supports_dp ||
14716 dev_priv->vbt.ddi_port_info[PORT_E].supports_dvi ||
14717 dev_priv->vbt.ddi_port_info[PORT_E].supports_hdmi))
14718 intel_ddi_init(dev, PORT_E);
14719
0e72a5b5 14720 } else if (HAS_PCH_SPLIT(dev)) {
cb0953d7 14721 int found;
5d8a7752 14722 dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
270b3042
DV
14723
14724 if (has_edp_a(dev))
14725 intel_dp_init(dev, DP_A, PORT_A);
cb0953d7 14726
dc0fa718 14727 if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
461ed3ca 14728 /* PCH SDVOB multiplex with HDMIB */
2a5c0832 14729 found = intel_sdvo_init(dev, PCH_SDVOB, PORT_B);
30ad48b7 14730 if (!found)
e2debe91 14731 intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
5eb08b69 14732 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
ab9d7c30 14733 intel_dp_init(dev, PCH_DP_B, PORT_B);
30ad48b7
ZW
14734 }
14735
dc0fa718 14736 if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
e2debe91 14737 intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
30ad48b7 14738
dc0fa718 14739 if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
e2debe91 14740 intel_hdmi_init(dev, PCH_HDMID, PORT_D);
30ad48b7 14741
5eb08b69 14742 if (I915_READ(PCH_DP_C) & DP_DETECTED)
ab9d7c30 14743 intel_dp_init(dev, PCH_DP_C, PORT_C);
5eb08b69 14744
270b3042 14745 if (I915_READ(PCH_DP_D) & DP_DETECTED)
ab9d7c30 14746 intel_dp_init(dev, PCH_DP_D, PORT_D);
666a4537 14747 } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
22f35042 14748 bool has_edp, has_port;
457c52d8 14749
e17ac6db
VS
14750 /*
14751 * The DP_DETECTED bit is the latched state of the DDC
14752 * SDA pin at boot. However since eDP doesn't require DDC
14753 * (no way to plug in a DP->HDMI dongle) the DDC pins for
14754 * eDP ports may have been muxed to an alternate function.
14755 * Thus we can't rely on the DP_DETECTED bit alone to detect
14756 * eDP ports. Consult the VBT as well as DP_DETECTED to
14757 * detect eDP ports.
22f35042
VS
14758 *
14759 * Sadly the straps seem to be missing sometimes even for HDMI
14760 * ports (eg. on Voyo V3 - CHT x7-Z8700), so check both strap
14761 * and VBT for the presence of the port. Additionally we can't
14762 * trust the port type the VBT declares as we've seen at least
14763 * HDMI ports that the VBT claim are DP or eDP.
e17ac6db 14764 */
457c52d8 14765 has_edp = intel_dp_is_edp(dev, PORT_B);
22f35042
VS
14766 has_port = intel_bios_is_port_present(dev_priv, PORT_B);
14767 if (I915_READ(VLV_DP_B) & DP_DETECTED || has_port)
457c52d8 14768 has_edp &= intel_dp_init(dev, VLV_DP_B, PORT_B);
22f35042 14769 if ((I915_READ(VLV_HDMIB) & SDVO_DETECTED || has_port) && !has_edp)
e66eb81d 14770 intel_hdmi_init(dev, VLV_HDMIB, PORT_B);
585a94b8 14771
457c52d8 14772 has_edp = intel_dp_is_edp(dev, PORT_C);
22f35042
VS
14773 has_port = intel_bios_is_port_present(dev_priv, PORT_C);
14774 if (I915_READ(VLV_DP_C) & DP_DETECTED || has_port)
457c52d8 14775 has_edp &= intel_dp_init(dev, VLV_DP_C, PORT_C);
22f35042 14776 if ((I915_READ(VLV_HDMIC) & SDVO_DETECTED || has_port) && !has_edp)
e66eb81d 14777 intel_hdmi_init(dev, VLV_HDMIC, PORT_C);
19c03924 14778
9418c1f1 14779 if (IS_CHERRYVIEW(dev)) {
22f35042
VS
14780 /*
14781 * eDP not supported on port D,
14782 * so no need to worry about it
14783 */
14784 has_port = intel_bios_is_port_present(dev_priv, PORT_D);
14785 if (I915_READ(CHV_DP_D) & DP_DETECTED || has_port)
e66eb81d 14786 intel_dp_init(dev, CHV_DP_D, PORT_D);
22f35042
VS
14787 if (I915_READ(CHV_HDMID) & SDVO_DETECTED || has_port)
14788 intel_hdmi_init(dev, CHV_HDMID, PORT_D);
9418c1f1
VS
14789 }
14790
3cfca973 14791 intel_dsi_init(dev);
09da55dc 14792 } else if (!IS_GEN2(dev) && !IS_PINEVIEW(dev)) {
27185ae1 14793 bool found = false;
7d57382e 14794
e2debe91 14795 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
b01f2c3a 14796 DRM_DEBUG_KMS("probing SDVOB\n");
2a5c0832 14797 found = intel_sdvo_init(dev, GEN3_SDVOB, PORT_B);
3fec3d2f 14798 if (!found && IS_G4X(dev)) {
b01f2c3a 14799 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
e2debe91 14800 intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
b01f2c3a 14801 }
27185ae1 14802
3fec3d2f 14803 if (!found && IS_G4X(dev))
ab9d7c30 14804 intel_dp_init(dev, DP_B, PORT_B);
725e30ad 14805 }
13520b05
KH
14806
14807 /* Before G4X SDVOC doesn't have its own detect register */
13520b05 14808
e2debe91 14809 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
b01f2c3a 14810 DRM_DEBUG_KMS("probing SDVOC\n");
2a5c0832 14811 found = intel_sdvo_init(dev, GEN3_SDVOC, PORT_C);
b01f2c3a 14812 }
27185ae1 14813
e2debe91 14814 if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
27185ae1 14815
3fec3d2f 14816 if (IS_G4X(dev)) {
b01f2c3a 14817 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
e2debe91 14818 intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
b01f2c3a 14819 }
3fec3d2f 14820 if (IS_G4X(dev))
ab9d7c30 14821 intel_dp_init(dev, DP_C, PORT_C);
725e30ad 14822 }
27185ae1 14823
3fec3d2f 14824 if (IS_G4X(dev) &&
e7281eab 14825 (I915_READ(DP_D) & DP_DETECTED))
ab9d7c30 14826 intel_dp_init(dev, DP_D, PORT_D);
bad720ff 14827 } else if (IS_GEN2(dev))
79e53945
JB
14828 intel_dvo_init(dev);
14829
103a196f 14830 if (SUPPORTS_TV(dev))
79e53945
JB
14831 intel_tv_init(dev);
14832
0bc12bcb 14833 intel_psr_init(dev);
7c8f8a70 14834
b2784e15 14835 for_each_intel_encoder(dev, encoder) {
4ef69c7a
CW
14836 encoder->base.possible_crtcs = encoder->crtc_mask;
14837 encoder->base.possible_clones =
66a9278e 14838 intel_encoder_clones(encoder);
79e53945 14839 }
47356eb6 14840
dde86e2d 14841 intel_init_pch_refclk(dev);
270b3042
DV
14842
14843 drm_helper_move_panel_connectors_to_head(dev);
79e53945
JB
14844}
14845
14846static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
14847{
60a5ca01 14848 struct drm_device *dev = fb->dev;
79e53945 14849 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
79e53945 14850
ef2d633e 14851 drm_framebuffer_cleanup(fb);
60a5ca01 14852 mutex_lock(&dev->struct_mutex);
ef2d633e 14853 WARN_ON(!intel_fb->obj->framebuffer_references--);
60a5ca01
VS
14854 drm_gem_object_unreference(&intel_fb->obj->base);
14855 mutex_unlock(&dev->struct_mutex);
79e53945
JB
14856 kfree(intel_fb);
14857}
14858
14859static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
05394f39 14860 struct drm_file *file,
79e53945
JB
14861 unsigned int *handle)
14862{
14863 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
05394f39 14864 struct drm_i915_gem_object *obj = intel_fb->obj;
79e53945 14865
cc917ab4
CW
14866 if (obj->userptr.mm) {
14867 DRM_DEBUG("attempting to use a userptr for a framebuffer, denied\n");
14868 return -EINVAL;
14869 }
14870
05394f39 14871 return drm_gem_handle_create(file, &obj->base, handle);
79e53945
JB
14872}
14873
86c98588
RV
14874static int intel_user_framebuffer_dirty(struct drm_framebuffer *fb,
14875 struct drm_file *file,
14876 unsigned flags, unsigned color,
14877 struct drm_clip_rect *clips,
14878 unsigned num_clips)
14879{
14880 struct drm_device *dev = fb->dev;
14881 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
14882 struct drm_i915_gem_object *obj = intel_fb->obj;
14883
14884 mutex_lock(&dev->struct_mutex);
74b4ea1e 14885 intel_fb_obj_flush(obj, false, ORIGIN_DIRTYFB);
86c98588
RV
14886 mutex_unlock(&dev->struct_mutex);
14887
14888 return 0;
14889}
14890
79e53945
JB
14891static const struct drm_framebuffer_funcs intel_fb_funcs = {
14892 .destroy = intel_user_framebuffer_destroy,
14893 .create_handle = intel_user_framebuffer_create_handle,
86c98588 14894 .dirty = intel_user_framebuffer_dirty,
79e53945
JB
14895};
14896
b321803d
DL
14897static
14898u32 intel_fb_pitch_limit(struct drm_device *dev, uint64_t fb_modifier,
14899 uint32_t pixel_format)
14900{
14901 u32 gen = INTEL_INFO(dev)->gen;
14902
14903 if (gen >= 9) {
ac484963
VS
14904 int cpp = drm_format_plane_cpp(pixel_format, 0);
14905
b321803d
DL
14906 /* "The stride in bytes must not exceed the of the size of 8K
14907 * pixels and 32K bytes."
14908 */
ac484963 14909 return min(8192 * cpp, 32768);
666a4537 14910 } else if (gen >= 5 && !IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
b321803d
DL
14911 return 32*1024;
14912 } else if (gen >= 4) {
14913 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14914 return 16*1024;
14915 else
14916 return 32*1024;
14917 } else if (gen >= 3) {
14918 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14919 return 8*1024;
14920 else
14921 return 16*1024;
14922 } else {
14923 /* XXX DSPC is limited to 4k tiled */
14924 return 8*1024;
14925 }
14926}
14927
b5ea642a
DV
14928static int intel_framebuffer_init(struct drm_device *dev,
14929 struct intel_framebuffer *intel_fb,
14930 struct drm_mode_fb_cmd2 *mode_cmd,
14931 struct drm_i915_gem_object *obj)
79e53945 14932{
7b49f948 14933 struct drm_i915_private *dev_priv = to_i915(dev);
6761dd31 14934 unsigned int aligned_height;
79e53945 14935 int ret;
b321803d 14936 u32 pitch_limit, stride_alignment;
79e53945 14937
dd4916c5
DV
14938 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
14939
2a80eada
DV
14940 if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
14941 /* Enforce that fb modifier and tiling mode match, but only for
14942 * X-tiled. This is needed for FBC. */
14943 if (!!(obj->tiling_mode == I915_TILING_X) !=
14944 !!(mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED)) {
14945 DRM_DEBUG("tiling_mode doesn't match fb modifier\n");
14946 return -EINVAL;
14947 }
14948 } else {
14949 if (obj->tiling_mode == I915_TILING_X)
14950 mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
14951 else if (obj->tiling_mode == I915_TILING_Y) {
14952 DRM_DEBUG("No Y tiling for legacy addfb\n");
14953 return -EINVAL;
14954 }
14955 }
14956
9a8f0a12
TU
14957 /* Passed in modifier sanity checking. */
14958 switch (mode_cmd->modifier[0]) {
14959 case I915_FORMAT_MOD_Y_TILED:
14960 case I915_FORMAT_MOD_Yf_TILED:
14961 if (INTEL_INFO(dev)->gen < 9) {
14962 DRM_DEBUG("Unsupported tiling 0x%llx!\n",
14963 mode_cmd->modifier[0]);
14964 return -EINVAL;
14965 }
14966 case DRM_FORMAT_MOD_NONE:
14967 case I915_FORMAT_MOD_X_TILED:
14968 break;
14969 default:
c0f40428
JB
14970 DRM_DEBUG("Unsupported fb modifier 0x%llx!\n",
14971 mode_cmd->modifier[0]);
57cd6508 14972 return -EINVAL;
c16ed4be 14973 }
57cd6508 14974
7b49f948
VS
14975 stride_alignment = intel_fb_stride_alignment(dev_priv,
14976 mode_cmd->modifier[0],
b321803d
DL
14977 mode_cmd->pixel_format);
14978 if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
14979 DRM_DEBUG("pitch (%d) must be at least %u byte aligned\n",
14980 mode_cmd->pitches[0], stride_alignment);
57cd6508 14981 return -EINVAL;
c16ed4be 14982 }
57cd6508 14983
b321803d
DL
14984 pitch_limit = intel_fb_pitch_limit(dev, mode_cmd->modifier[0],
14985 mode_cmd->pixel_format);
a35cdaa0 14986 if (mode_cmd->pitches[0] > pitch_limit) {
b321803d
DL
14987 DRM_DEBUG("%s pitch (%u) must be at less than %d\n",
14988 mode_cmd->modifier[0] != DRM_FORMAT_MOD_NONE ?
2a80eada 14989 "tiled" : "linear",
a35cdaa0 14990 mode_cmd->pitches[0], pitch_limit);
5d7bd705 14991 return -EINVAL;
c16ed4be 14992 }
5d7bd705 14993
2a80eada 14994 if (mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED &&
c16ed4be
CW
14995 mode_cmd->pitches[0] != obj->stride) {
14996 DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
14997 mode_cmd->pitches[0], obj->stride);
5d7bd705 14998 return -EINVAL;
c16ed4be 14999 }
5d7bd705 15000
57779d06 15001 /* Reject formats not supported by any plane early. */
308e5bcb 15002 switch (mode_cmd->pixel_format) {
57779d06 15003 case DRM_FORMAT_C8:
04b3924d
VS
15004 case DRM_FORMAT_RGB565:
15005 case DRM_FORMAT_XRGB8888:
15006 case DRM_FORMAT_ARGB8888:
57779d06
VS
15007 break;
15008 case DRM_FORMAT_XRGB1555:
c16ed4be 15009 if (INTEL_INFO(dev)->gen > 3) {
4ee62c76
VS
15010 DRM_DEBUG("unsupported pixel format: %s\n",
15011 drm_get_format_name(mode_cmd->pixel_format));
57779d06 15012 return -EINVAL;
c16ed4be 15013 }
57779d06 15014 break;
57779d06 15015 case DRM_FORMAT_ABGR8888:
666a4537
WB
15016 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev) &&
15017 INTEL_INFO(dev)->gen < 9) {
6c0fd451
DL
15018 DRM_DEBUG("unsupported pixel format: %s\n",
15019 drm_get_format_name(mode_cmd->pixel_format));
15020 return -EINVAL;
15021 }
15022 break;
15023 case DRM_FORMAT_XBGR8888:
04b3924d 15024 case DRM_FORMAT_XRGB2101010:
57779d06 15025 case DRM_FORMAT_XBGR2101010:
c16ed4be 15026 if (INTEL_INFO(dev)->gen < 4) {
4ee62c76
VS
15027 DRM_DEBUG("unsupported pixel format: %s\n",
15028 drm_get_format_name(mode_cmd->pixel_format));
57779d06 15029 return -EINVAL;
c16ed4be 15030 }
b5626747 15031 break;
7531208b 15032 case DRM_FORMAT_ABGR2101010:
666a4537 15033 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
7531208b
DL
15034 DRM_DEBUG("unsupported pixel format: %s\n",
15035 drm_get_format_name(mode_cmd->pixel_format));
15036 return -EINVAL;
15037 }
15038 break;
04b3924d
VS
15039 case DRM_FORMAT_YUYV:
15040 case DRM_FORMAT_UYVY:
15041 case DRM_FORMAT_YVYU:
15042 case DRM_FORMAT_VYUY:
c16ed4be 15043 if (INTEL_INFO(dev)->gen < 5) {
4ee62c76
VS
15044 DRM_DEBUG("unsupported pixel format: %s\n",
15045 drm_get_format_name(mode_cmd->pixel_format));
57779d06 15046 return -EINVAL;
c16ed4be 15047 }
57cd6508
CW
15048 break;
15049 default:
4ee62c76
VS
15050 DRM_DEBUG("unsupported pixel format: %s\n",
15051 drm_get_format_name(mode_cmd->pixel_format));
57cd6508
CW
15052 return -EINVAL;
15053 }
15054
90f9a336
VS
15055 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
15056 if (mode_cmd->offsets[0] != 0)
15057 return -EINVAL;
15058
ec2c981e 15059 aligned_height = intel_fb_align_height(dev, mode_cmd->height,
091df6cb
DV
15060 mode_cmd->pixel_format,
15061 mode_cmd->modifier[0]);
53155c0a
DV
15062 /* FIXME drm helper for size checks (especially planar formats)? */
15063 if (obj->base.size < aligned_height * mode_cmd->pitches[0])
15064 return -EINVAL;
15065
c7d73f6a
DV
15066 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
15067 intel_fb->obj = obj;
15068
2d7a215f
VS
15069 intel_fill_fb_info(dev_priv, &intel_fb->base);
15070
79e53945
JB
15071 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
15072 if (ret) {
15073 DRM_ERROR("framebuffer init failed %d\n", ret);
15074 return ret;
15075 }
15076
0b05e1e0
VS
15077 intel_fb->obj->framebuffer_references++;
15078
79e53945
JB
15079 return 0;
15080}
15081
79e53945
JB
15082static struct drm_framebuffer *
15083intel_user_framebuffer_create(struct drm_device *dev,
15084 struct drm_file *filp,
1eb83451 15085 const struct drm_mode_fb_cmd2 *user_mode_cmd)
79e53945 15086{
dcb1394e 15087 struct drm_framebuffer *fb;
05394f39 15088 struct drm_i915_gem_object *obj;
76dc3769 15089 struct drm_mode_fb_cmd2 mode_cmd = *user_mode_cmd;
79e53945 15090
03ac0642
CW
15091 obj = i915_gem_object_lookup(filp, mode_cmd.handles[0]);
15092 if (!obj)
cce13ff7 15093 return ERR_PTR(-ENOENT);
79e53945 15094
92907cbb 15095 fb = intel_framebuffer_create(dev, &mode_cmd, obj);
dcb1394e
LW
15096 if (IS_ERR(fb))
15097 drm_gem_object_unreference_unlocked(&obj->base);
15098
15099 return fb;
79e53945
JB
15100}
15101
0695726e 15102#ifndef CONFIG_DRM_FBDEV_EMULATION
0632fef6 15103static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
4520f53a
DV
15104{
15105}
15106#endif
15107
79e53945 15108static const struct drm_mode_config_funcs intel_mode_funcs = {
79e53945 15109 .fb_create = intel_user_framebuffer_create,
0632fef6 15110 .output_poll_changed = intel_fbdev_output_poll_changed,
5ee67f1c
MR
15111 .atomic_check = intel_atomic_check,
15112 .atomic_commit = intel_atomic_commit,
de419ab6
ML
15113 .atomic_state_alloc = intel_atomic_state_alloc,
15114 .atomic_state_clear = intel_atomic_state_clear,
79e53945
JB
15115};
15116
88212941
ID
15117/**
15118 * intel_init_display_hooks - initialize the display modesetting hooks
15119 * @dev_priv: device private
15120 */
15121void intel_init_display_hooks(struct drm_i915_private *dev_priv)
e70236a8 15122{
88212941 15123 if (INTEL_INFO(dev_priv)->gen >= 9) {
bc8d7dff 15124 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
5724dbd1
DL
15125 dev_priv->display.get_initial_plane_config =
15126 skylake_get_initial_plane_config;
bc8d7dff
DL
15127 dev_priv->display.crtc_compute_clock =
15128 haswell_crtc_compute_clock;
15129 dev_priv->display.crtc_enable = haswell_crtc_enable;
15130 dev_priv->display.crtc_disable = haswell_crtc_disable;
88212941 15131 } else if (HAS_DDI(dev_priv)) {
0e8ffe1b 15132 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
5724dbd1
DL
15133 dev_priv->display.get_initial_plane_config =
15134 ironlake_get_initial_plane_config;
797d0259
ACO
15135 dev_priv->display.crtc_compute_clock =
15136 haswell_crtc_compute_clock;
4f771f10
PZ
15137 dev_priv->display.crtc_enable = haswell_crtc_enable;
15138 dev_priv->display.crtc_disable = haswell_crtc_disable;
88212941 15139 } else if (HAS_PCH_SPLIT(dev_priv)) {
0e8ffe1b 15140 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
5724dbd1
DL
15141 dev_priv->display.get_initial_plane_config =
15142 ironlake_get_initial_plane_config;
3fb37703
ACO
15143 dev_priv->display.crtc_compute_clock =
15144 ironlake_crtc_compute_clock;
76e5a89c
DV
15145 dev_priv->display.crtc_enable = ironlake_crtc_enable;
15146 dev_priv->display.crtc_disable = ironlake_crtc_disable;
65b3d6a9 15147 } else if (IS_CHERRYVIEW(dev_priv)) {
89b667f8 15148 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
5724dbd1
DL
15149 dev_priv->display.get_initial_plane_config =
15150 i9xx_get_initial_plane_config;
65b3d6a9
ACO
15151 dev_priv->display.crtc_compute_clock = chv_crtc_compute_clock;
15152 dev_priv->display.crtc_enable = valleyview_crtc_enable;
15153 dev_priv->display.crtc_disable = i9xx_crtc_disable;
15154 } else if (IS_VALLEYVIEW(dev_priv)) {
15155 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
15156 dev_priv->display.get_initial_plane_config =
15157 i9xx_get_initial_plane_config;
15158 dev_priv->display.crtc_compute_clock = vlv_crtc_compute_clock;
89b667f8
JB
15159 dev_priv->display.crtc_enable = valleyview_crtc_enable;
15160 dev_priv->display.crtc_disable = i9xx_crtc_disable;
19ec6693
ACO
15161 } else if (IS_G4X(dev_priv)) {
15162 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
15163 dev_priv->display.get_initial_plane_config =
15164 i9xx_get_initial_plane_config;
15165 dev_priv->display.crtc_compute_clock = g4x_crtc_compute_clock;
15166 dev_priv->display.crtc_enable = i9xx_crtc_enable;
15167 dev_priv->display.crtc_disable = i9xx_crtc_disable;
70e8aa21
ACO
15168 } else if (IS_PINEVIEW(dev_priv)) {
15169 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
15170 dev_priv->display.get_initial_plane_config =
15171 i9xx_get_initial_plane_config;
15172 dev_priv->display.crtc_compute_clock = pnv_crtc_compute_clock;
15173 dev_priv->display.crtc_enable = i9xx_crtc_enable;
15174 dev_priv->display.crtc_disable = i9xx_crtc_disable;
81c97f52 15175 } else if (!IS_GEN2(dev_priv)) {
0e8ffe1b 15176 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
5724dbd1
DL
15177 dev_priv->display.get_initial_plane_config =
15178 i9xx_get_initial_plane_config;
d6dfee7a 15179 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
76e5a89c
DV
15180 dev_priv->display.crtc_enable = i9xx_crtc_enable;
15181 dev_priv->display.crtc_disable = i9xx_crtc_disable;
81c97f52
ACO
15182 } else {
15183 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
15184 dev_priv->display.get_initial_plane_config =
15185 i9xx_get_initial_plane_config;
15186 dev_priv->display.crtc_compute_clock = i8xx_crtc_compute_clock;
15187 dev_priv->display.crtc_enable = i9xx_crtc_enable;
15188 dev_priv->display.crtc_disable = i9xx_crtc_disable;
f564048e 15189 }
e70236a8 15190
e70236a8 15191 /* Returns the core display clock speed */
88212941 15192 if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv))
1652d19e
VS
15193 dev_priv->display.get_display_clock_speed =
15194 skylake_get_display_clock_speed;
88212941 15195 else if (IS_BROXTON(dev_priv))
acd3f3d3
BP
15196 dev_priv->display.get_display_clock_speed =
15197 broxton_get_display_clock_speed;
88212941 15198 else if (IS_BROADWELL(dev_priv))
1652d19e
VS
15199 dev_priv->display.get_display_clock_speed =
15200 broadwell_get_display_clock_speed;
88212941 15201 else if (IS_HASWELL(dev_priv))
1652d19e
VS
15202 dev_priv->display.get_display_clock_speed =
15203 haswell_get_display_clock_speed;
88212941 15204 else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
25eb05fc
JB
15205 dev_priv->display.get_display_clock_speed =
15206 valleyview_get_display_clock_speed;
88212941 15207 else if (IS_GEN5(dev_priv))
b37a6434
VS
15208 dev_priv->display.get_display_clock_speed =
15209 ilk_get_display_clock_speed;
88212941
ID
15210 else if (IS_I945G(dev_priv) || IS_BROADWATER(dev_priv) ||
15211 IS_GEN6(dev_priv) || IS_IVYBRIDGE(dev_priv))
e70236a8
JB
15212 dev_priv->display.get_display_clock_speed =
15213 i945_get_display_clock_speed;
88212941 15214 else if (IS_GM45(dev_priv))
34edce2f
VS
15215 dev_priv->display.get_display_clock_speed =
15216 gm45_get_display_clock_speed;
88212941 15217 else if (IS_CRESTLINE(dev_priv))
34edce2f
VS
15218 dev_priv->display.get_display_clock_speed =
15219 i965gm_get_display_clock_speed;
88212941 15220 else if (IS_PINEVIEW(dev_priv))
34edce2f
VS
15221 dev_priv->display.get_display_clock_speed =
15222 pnv_get_display_clock_speed;
88212941 15223 else if (IS_G33(dev_priv) || IS_G4X(dev_priv))
34edce2f
VS
15224 dev_priv->display.get_display_clock_speed =
15225 g33_get_display_clock_speed;
88212941 15226 else if (IS_I915G(dev_priv))
e70236a8
JB
15227 dev_priv->display.get_display_clock_speed =
15228 i915_get_display_clock_speed;
88212941 15229 else if (IS_I945GM(dev_priv) || IS_845G(dev_priv))
e70236a8
JB
15230 dev_priv->display.get_display_clock_speed =
15231 i9xx_misc_get_display_clock_speed;
88212941 15232 else if (IS_I915GM(dev_priv))
e70236a8
JB
15233 dev_priv->display.get_display_clock_speed =
15234 i915gm_get_display_clock_speed;
88212941 15235 else if (IS_I865G(dev_priv))
e70236a8
JB
15236 dev_priv->display.get_display_clock_speed =
15237 i865_get_display_clock_speed;
88212941 15238 else if (IS_I85X(dev_priv))
e70236a8 15239 dev_priv->display.get_display_clock_speed =
1b1d2716 15240 i85x_get_display_clock_speed;
623e01e5 15241 else { /* 830 */
88212941 15242 WARN(!IS_I830(dev_priv), "Unknown platform. Assuming 133 MHz CDCLK\n");
e70236a8
JB
15243 dev_priv->display.get_display_clock_speed =
15244 i830_get_display_clock_speed;
623e01e5 15245 }
e70236a8 15246
88212941 15247 if (IS_GEN5(dev_priv)) {
3bb11b53 15248 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
88212941 15249 } else if (IS_GEN6(dev_priv)) {
3bb11b53 15250 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
88212941 15251 } else if (IS_IVYBRIDGE(dev_priv)) {
3bb11b53
SJ
15252 /* FIXME: detect B0+ stepping and use auto training */
15253 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
88212941 15254 } else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
3bb11b53 15255 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
445e780b
VS
15256 }
15257
15258 if (IS_BROADWELL(dev_priv)) {
15259 dev_priv->display.modeset_commit_cdclk =
15260 broadwell_modeset_commit_cdclk;
15261 dev_priv->display.modeset_calc_cdclk =
15262 broadwell_modeset_calc_cdclk;
88212941 15263 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
27c329ed
ML
15264 dev_priv->display.modeset_commit_cdclk =
15265 valleyview_modeset_commit_cdclk;
15266 dev_priv->display.modeset_calc_cdclk =
15267 valleyview_modeset_calc_cdclk;
88212941 15268 } else if (IS_BROXTON(dev_priv)) {
27c329ed 15269 dev_priv->display.modeset_commit_cdclk =
324513c0 15270 bxt_modeset_commit_cdclk;
27c329ed 15271 dev_priv->display.modeset_calc_cdclk =
324513c0 15272 bxt_modeset_calc_cdclk;
c89e39f3
CT
15273 } else if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv)) {
15274 dev_priv->display.modeset_commit_cdclk =
15275 skl_modeset_commit_cdclk;
15276 dev_priv->display.modeset_calc_cdclk =
15277 skl_modeset_calc_cdclk;
e70236a8 15278 }
5a21b665
DV
15279
15280 switch (INTEL_INFO(dev_priv)->gen) {
15281 case 2:
15282 dev_priv->display.queue_flip = intel_gen2_queue_flip;
15283 break;
15284
15285 case 3:
15286 dev_priv->display.queue_flip = intel_gen3_queue_flip;
15287 break;
15288
15289 case 4:
15290 case 5:
15291 dev_priv->display.queue_flip = intel_gen4_queue_flip;
15292 break;
15293
15294 case 6:
15295 dev_priv->display.queue_flip = intel_gen6_queue_flip;
15296 break;
15297 case 7:
15298 case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
15299 dev_priv->display.queue_flip = intel_gen7_queue_flip;
15300 break;
15301 case 9:
15302 /* Drop through - unsupported since execlist only. */
15303 default:
15304 /* Default just returns -ENODEV to indicate unsupported */
15305 dev_priv->display.queue_flip = intel_default_queue_flip;
15306 }
e70236a8
JB
15307}
15308
b690e96c
JB
15309/*
15310 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
15311 * resume, or other times. This quirk makes sure that's the case for
15312 * affected systems.
15313 */
0206e353 15314static void quirk_pipea_force(struct drm_device *dev)
b690e96c 15315{
fac5e23e 15316 struct drm_i915_private *dev_priv = to_i915(dev);
b690e96c
JB
15317
15318 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
bc0daf48 15319 DRM_INFO("applying pipe a force quirk\n");
b690e96c
JB
15320}
15321
b6b5d049
VS
15322static void quirk_pipeb_force(struct drm_device *dev)
15323{
fac5e23e 15324 struct drm_i915_private *dev_priv = to_i915(dev);
b6b5d049
VS
15325
15326 dev_priv->quirks |= QUIRK_PIPEB_FORCE;
15327 DRM_INFO("applying pipe b force quirk\n");
15328}
15329
435793df
KP
15330/*
15331 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
15332 */
15333static void quirk_ssc_force_disable(struct drm_device *dev)
15334{
fac5e23e 15335 struct drm_i915_private *dev_priv = to_i915(dev);
435793df 15336 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
bc0daf48 15337 DRM_INFO("applying lvds SSC disable quirk\n");
435793df
KP
15338}
15339
4dca20ef 15340/*
5a15ab5b
CE
15341 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
15342 * brightness value
4dca20ef
CE
15343 */
15344static void quirk_invert_brightness(struct drm_device *dev)
15345{
fac5e23e 15346 struct drm_i915_private *dev_priv = to_i915(dev);
4dca20ef 15347 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
bc0daf48 15348 DRM_INFO("applying inverted panel brightness quirk\n");
435793df
KP
15349}
15350
9c72cc6f
SD
15351/* Some VBT's incorrectly indicate no backlight is present */
15352static void quirk_backlight_present(struct drm_device *dev)
15353{
fac5e23e 15354 struct drm_i915_private *dev_priv = to_i915(dev);
9c72cc6f
SD
15355 dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
15356 DRM_INFO("applying backlight present quirk\n");
15357}
15358
b690e96c
JB
15359struct intel_quirk {
15360 int device;
15361 int subsystem_vendor;
15362 int subsystem_device;
15363 void (*hook)(struct drm_device *dev);
15364};
15365
5f85f176
EE
15366/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
15367struct intel_dmi_quirk {
15368 void (*hook)(struct drm_device *dev);
15369 const struct dmi_system_id (*dmi_id_list)[];
15370};
15371
15372static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
15373{
15374 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
15375 return 1;
15376}
15377
15378static const struct intel_dmi_quirk intel_dmi_quirks[] = {
15379 {
15380 .dmi_id_list = &(const struct dmi_system_id[]) {
15381 {
15382 .callback = intel_dmi_reverse_brightness,
15383 .ident = "NCR Corporation",
15384 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
15385 DMI_MATCH(DMI_PRODUCT_NAME, ""),
15386 },
15387 },
15388 { } /* terminating entry */
15389 },
15390 .hook = quirk_invert_brightness,
15391 },
15392};
15393
c43b5634 15394static struct intel_quirk intel_quirks[] = {
b690e96c
JB
15395 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
15396 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
15397
b690e96c
JB
15398 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
15399 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
15400
5f080c0f
VS
15401 /* 830 needs to leave pipe A & dpll A up */
15402 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
15403
b6b5d049
VS
15404 /* 830 needs to leave pipe B & dpll B up */
15405 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
15406
435793df
KP
15407 /* Lenovo U160 cannot use SSC on LVDS */
15408 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
070d329a
MAS
15409
15410 /* Sony Vaio Y cannot use SSC on LVDS */
15411 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
5a15ab5b 15412
be505f64
AH
15413 /* Acer Aspire 5734Z must invert backlight brightness */
15414 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
15415
15416 /* Acer/eMachines G725 */
15417 { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
15418
15419 /* Acer/eMachines e725 */
15420 { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
15421
15422 /* Acer/Packard Bell NCL20 */
15423 { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
15424
15425 /* Acer Aspire 4736Z */
15426 { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
0f540c3a
JN
15427
15428 /* Acer Aspire 5336 */
15429 { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
2e93a1aa
SD
15430
15431 /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
15432 { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
d4967d8c 15433
dfb3d47b
SD
15434 /* Acer C720 Chromebook (Core i3 4005U) */
15435 { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
15436
b2a9601c 15437 /* Apple Macbook 2,1 (Core 2 T7400) */
15438 { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
15439
1b9448b0
JN
15440 /* Apple Macbook 4,1 */
15441 { 0x2a02, 0x106b, 0x00a1, quirk_backlight_present },
15442
d4967d8c
SD
15443 /* Toshiba CB35 Chromebook (Celeron 2955U) */
15444 { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
724cb06f
SD
15445
15446 /* HP Chromebook 14 (Celeron 2955U) */
15447 { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
cf6f0af9
JN
15448
15449 /* Dell Chromebook 11 */
15450 { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
9be64eee
JN
15451
15452 /* Dell Chromebook 11 (2015 version) */
15453 { 0x0a16, 0x1028, 0x0a35, quirk_backlight_present },
b690e96c
JB
15454};
15455
15456static void intel_init_quirks(struct drm_device *dev)
15457{
15458 struct pci_dev *d = dev->pdev;
15459 int i;
15460
15461 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
15462 struct intel_quirk *q = &intel_quirks[i];
15463
15464 if (d->device == q->device &&
15465 (d->subsystem_vendor == q->subsystem_vendor ||
15466 q->subsystem_vendor == PCI_ANY_ID) &&
15467 (d->subsystem_device == q->subsystem_device ||
15468 q->subsystem_device == PCI_ANY_ID))
15469 q->hook(dev);
15470 }
5f85f176
EE
15471 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
15472 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
15473 intel_dmi_quirks[i].hook(dev);
15474 }
b690e96c
JB
15475}
15476
9cce37f4
JB
15477/* Disable the VGA plane that we never use */
15478static void i915_disable_vga(struct drm_device *dev)
15479{
fac5e23e 15480 struct drm_i915_private *dev_priv = to_i915(dev);
9cce37f4 15481 u8 sr1;
f0f59a00 15482 i915_reg_t vga_reg = i915_vgacntrl_reg(dev);
9cce37f4 15483
2b37c616 15484 /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
9cce37f4 15485 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
3fdcf431 15486 outb(SR01, VGA_SR_INDEX);
9cce37f4
JB
15487 sr1 = inb(VGA_SR_DATA);
15488 outb(sr1 | 1<<5, VGA_SR_DATA);
15489 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
15490 udelay(300);
15491
01f5a626 15492 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
9cce37f4
JB
15493 POSTING_READ(vga_reg);
15494}
15495
f817586c
DV
15496void intel_modeset_init_hw(struct drm_device *dev)
15497{
fac5e23e 15498 struct drm_i915_private *dev_priv = to_i915(dev);
1a617b77 15499
b6283055 15500 intel_update_cdclk(dev);
1a617b77
ML
15501
15502 dev_priv->atomic_cdclk_freq = dev_priv->cdclk_freq;
15503
f817586c 15504 intel_init_clock_gating(dev);
f817586c
DV
15505}
15506
d93c0372
MR
15507/*
15508 * Calculate what we think the watermarks should be for the state we've read
15509 * out of the hardware and then immediately program those watermarks so that
15510 * we ensure the hardware settings match our internal state.
15511 *
15512 * We can calculate what we think WM's should be by creating a duplicate of the
15513 * current state (which was constructed during hardware readout) and running it
15514 * through the atomic check code to calculate new watermark values in the
15515 * state object.
15516 */
15517static void sanitize_watermarks(struct drm_device *dev)
15518{
15519 struct drm_i915_private *dev_priv = to_i915(dev);
15520 struct drm_atomic_state *state;
15521 struct drm_crtc *crtc;
15522 struct drm_crtc_state *cstate;
15523 struct drm_modeset_acquire_ctx ctx;
15524 int ret;
15525 int i;
15526
15527 /* Only supported on platforms that use atomic watermark design */
ed4a6a7c 15528 if (!dev_priv->display.optimize_watermarks)
d93c0372
MR
15529 return;
15530
15531 /*
15532 * We need to hold connection_mutex before calling duplicate_state so
15533 * that the connector loop is protected.
15534 */
15535 drm_modeset_acquire_init(&ctx, 0);
15536retry:
0cd1262d 15537 ret = drm_modeset_lock_all_ctx(dev, &ctx);
d93c0372
MR
15538 if (ret == -EDEADLK) {
15539 drm_modeset_backoff(&ctx);
15540 goto retry;
15541 } else if (WARN_ON(ret)) {
0cd1262d 15542 goto fail;
d93c0372
MR
15543 }
15544
15545 state = drm_atomic_helper_duplicate_state(dev, &ctx);
15546 if (WARN_ON(IS_ERR(state)))
0cd1262d 15547 goto fail;
d93c0372 15548
ed4a6a7c
MR
15549 /*
15550 * Hardware readout is the only time we don't want to calculate
15551 * intermediate watermarks (since we don't trust the current
15552 * watermarks).
15553 */
15554 to_intel_atomic_state(state)->skip_intermediate_wm = true;
15555
d93c0372
MR
15556 ret = intel_atomic_check(dev, state);
15557 if (ret) {
15558 /*
15559 * If we fail here, it means that the hardware appears to be
15560 * programmed in a way that shouldn't be possible, given our
15561 * understanding of watermark requirements. This might mean a
15562 * mistake in the hardware readout code or a mistake in the
15563 * watermark calculations for a given platform. Raise a WARN
15564 * so that this is noticeable.
15565 *
15566 * If this actually happens, we'll have to just leave the
15567 * BIOS-programmed watermarks untouched and hope for the best.
15568 */
15569 WARN(true, "Could not determine valid watermarks for inherited state\n");
0cd1262d 15570 goto fail;
d93c0372
MR
15571 }
15572
15573 /* Write calculated watermark values back */
d93c0372
MR
15574 for_each_crtc_in_state(state, crtc, cstate, i) {
15575 struct intel_crtc_state *cs = to_intel_crtc_state(cstate);
15576
ed4a6a7c
MR
15577 cs->wm.need_postvbl_update = true;
15578 dev_priv->display.optimize_watermarks(cs);
d93c0372
MR
15579 }
15580
15581 drm_atomic_state_free(state);
0cd1262d 15582fail:
d93c0372
MR
15583 drm_modeset_drop_locks(&ctx);
15584 drm_modeset_acquire_fini(&ctx);
15585}
15586
79e53945
JB
15587void intel_modeset_init(struct drm_device *dev)
15588{
72e96d64
JL
15589 struct drm_i915_private *dev_priv = to_i915(dev);
15590 struct i915_ggtt *ggtt = &dev_priv->ggtt;
1fe47785 15591 int sprite, ret;
8cc87b75 15592 enum pipe pipe;
46f297fb 15593 struct intel_crtc *crtc;
79e53945
JB
15594
15595 drm_mode_config_init(dev);
15596
15597 dev->mode_config.min_width = 0;
15598 dev->mode_config.min_height = 0;
15599
019d96cb
DA
15600 dev->mode_config.preferred_depth = 24;
15601 dev->mode_config.prefer_shadow = 1;
15602
25bab385
TU
15603 dev->mode_config.allow_fb_modifiers = true;
15604
e6ecefaa 15605 dev->mode_config.funcs = &intel_mode_funcs;
79e53945 15606
b690e96c
JB
15607 intel_init_quirks(dev);
15608
1fa61106
ED
15609 intel_init_pm(dev);
15610
e3c74757
BW
15611 if (INTEL_INFO(dev)->num_pipes == 0)
15612 return;
15613
69f92f67
LW
15614 /*
15615 * There may be no VBT; and if the BIOS enabled SSC we can
15616 * just keep using it to avoid unnecessary flicker. Whereas if the
15617 * BIOS isn't using it, don't assume it will work even if the VBT
15618 * indicates as much.
15619 */
15620 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)) {
15621 bool bios_lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
15622 DREF_SSC1_ENABLE);
15623
15624 if (dev_priv->vbt.lvds_use_ssc != bios_lvds_use_ssc) {
15625 DRM_DEBUG_KMS("SSC %sabled by BIOS, overriding VBT which says %sabled\n",
15626 bios_lvds_use_ssc ? "en" : "dis",
15627 dev_priv->vbt.lvds_use_ssc ? "en" : "dis");
15628 dev_priv->vbt.lvds_use_ssc = bios_lvds_use_ssc;
15629 }
15630 }
15631
a6c45cf0
CW
15632 if (IS_GEN2(dev)) {
15633 dev->mode_config.max_width = 2048;
15634 dev->mode_config.max_height = 2048;
15635 } else if (IS_GEN3(dev)) {
5e4d6fa7
KP
15636 dev->mode_config.max_width = 4096;
15637 dev->mode_config.max_height = 4096;
79e53945 15638 } else {
a6c45cf0
CW
15639 dev->mode_config.max_width = 8192;
15640 dev->mode_config.max_height = 8192;
79e53945 15641 }
068be561 15642
dc41c154
VS
15643 if (IS_845G(dev) || IS_I865G(dev)) {
15644 dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
15645 dev->mode_config.cursor_height = 1023;
15646 } else if (IS_GEN2(dev)) {
068be561
DL
15647 dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
15648 dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
15649 } else {
15650 dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
15651 dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
15652 }
15653
72e96d64 15654 dev->mode_config.fb_base = ggtt->mappable_base;
79e53945 15655
28c97730 15656 DRM_DEBUG_KMS("%d display pipe%s available.\n",
7eb552ae
BW
15657 INTEL_INFO(dev)->num_pipes,
15658 INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
79e53945 15659
055e393f 15660 for_each_pipe(dev_priv, pipe) {
8cc87b75 15661 intel_crtc_init(dev, pipe);
3bdcfc0c 15662 for_each_sprite(dev_priv, pipe, sprite) {
1fe47785 15663 ret = intel_plane_init(dev, pipe, sprite);
7f1f3851 15664 if (ret)
06da8da2 15665 DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
1fe47785 15666 pipe_name(pipe), sprite_name(pipe, sprite), ret);
7f1f3851 15667 }
79e53945
JB
15668 }
15669
bfa7df01
VS
15670 intel_update_czclk(dev_priv);
15671 intel_update_cdclk(dev);
15672
e72f9fbf 15673 intel_shared_dpll_init(dev);
ee7b9f93 15674
b2045352
VS
15675 if (dev_priv->max_cdclk_freq == 0)
15676 intel_update_max_cdclk(dev);
15677
9cce37f4
JB
15678 /* Just disable it once at startup */
15679 i915_disable_vga(dev);
79e53945 15680 intel_setup_outputs(dev);
11be49eb 15681
6e9f798d 15682 drm_modeset_lock_all(dev);
043e9bda 15683 intel_modeset_setup_hw_state(dev);
6e9f798d 15684 drm_modeset_unlock_all(dev);
46f297fb 15685
d3fcc808 15686 for_each_intel_crtc(dev, crtc) {
eeebeac5
ML
15687 struct intel_initial_plane_config plane_config = {};
15688
46f297fb
JB
15689 if (!crtc->active)
15690 continue;
15691
46f297fb 15692 /*
46f297fb
JB
15693 * Note that reserving the BIOS fb up front prevents us
15694 * from stuffing other stolen allocations like the ring
15695 * on top. This prevents some ugliness at boot time, and
15696 * can even allow for smooth boot transitions if the BIOS
15697 * fb is large enough for the active pipe configuration.
15698 */
eeebeac5
ML
15699 dev_priv->display.get_initial_plane_config(crtc,
15700 &plane_config);
15701
15702 /*
15703 * If the fb is shared between multiple heads, we'll
15704 * just get the first one.
15705 */
15706 intel_find_initial_plane_obj(crtc, &plane_config);
46f297fb 15707 }
d93c0372
MR
15708
15709 /*
15710 * Make sure hardware watermarks really match the state we read out.
15711 * Note that we need to do this after reconstructing the BIOS fb's
15712 * since the watermark calculation done here will use pstate->fb.
15713 */
15714 sanitize_watermarks(dev);
2c7111db
CW
15715}
15716
7fad798e
DV
15717static void intel_enable_pipe_a(struct drm_device *dev)
15718{
15719 struct intel_connector *connector;
15720 struct drm_connector *crt = NULL;
15721 struct intel_load_detect_pipe load_detect_temp;
208bf9fd 15722 struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
7fad798e
DV
15723
15724 /* We can't just switch on the pipe A, we need to set things up with a
15725 * proper mode and output configuration. As a gross hack, enable pipe A
15726 * by enabling the load detect pipe once. */
3a3371ff 15727 for_each_intel_connector(dev, connector) {
7fad798e
DV
15728 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
15729 crt = &connector->base;
15730 break;
15731 }
15732 }
15733
15734 if (!crt)
15735 return;
15736
208bf9fd 15737 if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
49172fee 15738 intel_release_load_detect_pipe(crt, &load_detect_temp, ctx);
7fad798e
DV
15739}
15740
fa555837
DV
15741static bool
15742intel_check_plane_mapping(struct intel_crtc *crtc)
15743{
7eb552ae 15744 struct drm_device *dev = crtc->base.dev;
fac5e23e 15745 struct drm_i915_private *dev_priv = to_i915(dev);
649636ef 15746 u32 val;
fa555837 15747
7eb552ae 15748 if (INTEL_INFO(dev)->num_pipes == 1)
fa555837
DV
15749 return true;
15750
649636ef 15751 val = I915_READ(DSPCNTR(!crtc->plane));
fa555837
DV
15752
15753 if ((val & DISPLAY_PLANE_ENABLE) &&
15754 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
15755 return false;
15756
15757 return true;
15758}
15759
02e93c35
VS
15760static bool intel_crtc_has_encoders(struct intel_crtc *crtc)
15761{
15762 struct drm_device *dev = crtc->base.dev;
15763 struct intel_encoder *encoder;
15764
15765 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
15766 return true;
15767
15768 return false;
15769}
15770
dd756198
VS
15771static bool intel_encoder_has_connectors(struct intel_encoder *encoder)
15772{
15773 struct drm_device *dev = encoder->base.dev;
15774 struct intel_connector *connector;
15775
15776 for_each_connector_on_encoder(dev, &encoder->base, connector)
15777 return true;
15778
15779 return false;
15780}
15781
24929352
DV
15782static void intel_sanitize_crtc(struct intel_crtc *crtc)
15783{
15784 struct drm_device *dev = crtc->base.dev;
fac5e23e 15785 struct drm_i915_private *dev_priv = to_i915(dev);
4d1de975 15786 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
24929352 15787
24929352 15788 /* Clear any frame start delays used for debugging left by the BIOS */
4d1de975
JN
15789 if (!transcoder_is_dsi(cpu_transcoder)) {
15790 i915_reg_t reg = PIPECONF(cpu_transcoder);
15791
15792 I915_WRITE(reg,
15793 I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
15794 }
24929352 15795
d3eaf884 15796 /* restore vblank interrupts to correct state */
9625604c 15797 drm_crtc_vblank_reset(&crtc->base);
d297e103 15798 if (crtc->active) {
f9cd7b88
VS
15799 struct intel_plane *plane;
15800
9625604c 15801 drm_crtc_vblank_on(&crtc->base);
f9cd7b88
VS
15802
15803 /* Disable everything but the primary plane */
15804 for_each_intel_plane_on_crtc(dev, crtc, plane) {
15805 if (plane->base.type == DRM_PLANE_TYPE_PRIMARY)
15806 continue;
15807
15808 plane->disable_plane(&plane->base, &crtc->base);
15809 }
9625604c 15810 }
d3eaf884 15811
24929352 15812 /* We need to sanitize the plane -> pipe mapping first because this will
fa555837
DV
15813 * disable the crtc (and hence change the state) if it is wrong. Note
15814 * that gen4+ has a fixed plane -> pipe mapping. */
15815 if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
24929352
DV
15816 bool plane;
15817
78108b7c
VS
15818 DRM_DEBUG_KMS("[CRTC:%d:%s] wrong plane connection detected!\n",
15819 crtc->base.base.id, crtc->base.name);
24929352
DV
15820
15821 /* Pipe has the wrong plane attached and the plane is active.
15822 * Temporarily change the plane mapping and disable everything
15823 * ... */
15824 plane = crtc->plane;
b70709a6 15825 to_intel_plane_state(crtc->base.primary->state)->visible = true;
24929352 15826 crtc->plane = !plane;
b17d48e2 15827 intel_crtc_disable_noatomic(&crtc->base);
24929352 15828 crtc->plane = plane;
24929352 15829 }
24929352 15830
7fad798e
DV
15831 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
15832 crtc->pipe == PIPE_A && !crtc->active) {
15833 /* BIOS forgot to enable pipe A, this mostly happens after
15834 * resume. Force-enable the pipe to fix this, the update_dpms
15835 * call below we restore the pipe to the right state, but leave
15836 * the required bits on. */
15837 intel_enable_pipe_a(dev);
15838 }
15839
24929352
DV
15840 /* Adjust the state of the output pipe according to whether we
15841 * have active connectors/encoders. */
842e0307 15842 if (crtc->active && !intel_crtc_has_encoders(crtc))
b17d48e2 15843 intel_crtc_disable_noatomic(&crtc->base);
24929352 15844
a3ed6aad 15845 if (crtc->active || HAS_GMCH_DISPLAY(dev)) {
4cc31489
DV
15846 /*
15847 * We start out with underrun reporting disabled to avoid races.
15848 * For correct bookkeeping mark this on active crtcs.
15849 *
c5ab3bc0
DV
15850 * Also on gmch platforms we dont have any hardware bits to
15851 * disable the underrun reporting. Which means we need to start
15852 * out with underrun reporting disabled also on inactive pipes,
15853 * since otherwise we'll complain about the garbage we read when
15854 * e.g. coming up after runtime pm.
15855 *
4cc31489
DV
15856 * No protection against concurrent access is required - at
15857 * worst a fifo underrun happens which also sets this to false.
15858 */
15859 crtc->cpu_fifo_underrun_disabled = true;
15860 crtc->pch_fifo_underrun_disabled = true;
15861 }
24929352
DV
15862}
15863
15864static void intel_sanitize_encoder(struct intel_encoder *encoder)
15865{
15866 struct intel_connector *connector;
15867 struct drm_device *dev = encoder->base.dev;
15868
15869 /* We need to check both for a crtc link (meaning that the
15870 * encoder is active and trying to read from a pipe) and the
15871 * pipe itself being active. */
15872 bool has_active_crtc = encoder->base.crtc &&
15873 to_intel_crtc(encoder->base.crtc)->active;
15874
dd756198 15875 if (intel_encoder_has_connectors(encoder) && !has_active_crtc) {
24929352
DV
15876 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
15877 encoder->base.base.id,
8e329a03 15878 encoder->base.name);
24929352
DV
15879
15880 /* Connector is active, but has no active pipe. This is
15881 * fallout from our resume register restoring. Disable
15882 * the encoder manually again. */
15883 if (encoder->base.crtc) {
15884 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
15885 encoder->base.base.id,
8e329a03 15886 encoder->base.name);
24929352 15887 encoder->disable(encoder);
a62d1497
VS
15888 if (encoder->post_disable)
15889 encoder->post_disable(encoder);
24929352 15890 }
7f1950fb 15891 encoder->base.crtc = NULL;
24929352
DV
15892
15893 /* Inconsistent output/port/pipe state happens presumably due to
15894 * a bug in one of the get_hw_state functions. Or someplace else
15895 * in our code, like the register restore mess on resume. Clamp
15896 * things to off as a safer default. */
3a3371ff 15897 for_each_intel_connector(dev, connector) {
24929352
DV
15898 if (connector->encoder != encoder)
15899 continue;
7f1950fb
EE
15900 connector->base.dpms = DRM_MODE_DPMS_OFF;
15901 connector->base.encoder = NULL;
24929352
DV
15902 }
15903 }
15904 /* Enabled encoders without active connectors will be fixed in
15905 * the crtc fixup. */
15906}
15907
04098753 15908void i915_redisable_vga_power_on(struct drm_device *dev)
0fde901f 15909{
fac5e23e 15910 struct drm_i915_private *dev_priv = to_i915(dev);
f0f59a00 15911 i915_reg_t vga_reg = i915_vgacntrl_reg(dev);
0fde901f 15912
04098753
ID
15913 if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
15914 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
15915 i915_disable_vga(dev);
15916 }
15917}
15918
15919void i915_redisable_vga(struct drm_device *dev)
15920{
fac5e23e 15921 struct drm_i915_private *dev_priv = to_i915(dev);
04098753 15922
8dc8a27c
PZ
15923 /* This function can be called both from intel_modeset_setup_hw_state or
15924 * at a very early point in our resume sequence, where the power well
15925 * structures are not yet restored. Since this function is at a very
15926 * paranoid "someone might have enabled VGA while we were not looking"
15927 * level, just check if the power well is enabled instead of trying to
15928 * follow the "don't touch the power well if we don't need it" policy
15929 * the rest of the driver uses. */
6392f847 15930 if (!intel_display_power_get_if_enabled(dev_priv, POWER_DOMAIN_VGA))
8dc8a27c
PZ
15931 return;
15932
04098753 15933 i915_redisable_vga_power_on(dev);
6392f847
ID
15934
15935 intel_display_power_put(dev_priv, POWER_DOMAIN_VGA);
0fde901f
KM
15936}
15937
f9cd7b88 15938static bool primary_get_hw_state(struct intel_plane *plane)
98ec7739 15939{
f9cd7b88 15940 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
98ec7739 15941
f9cd7b88 15942 return I915_READ(DSPCNTR(plane->plane)) & DISPLAY_PLANE_ENABLE;
d032ffa0
ML
15943}
15944
f9cd7b88
VS
15945/* FIXME read out full plane state for all planes */
15946static void readout_plane_state(struct intel_crtc *crtc)
d032ffa0 15947{
b26d3ea3 15948 struct drm_plane *primary = crtc->base.primary;
f9cd7b88 15949 struct intel_plane_state *plane_state =
b26d3ea3 15950 to_intel_plane_state(primary->state);
d032ffa0 15951
19b8d387 15952 plane_state->visible = crtc->active &&
b26d3ea3
ML
15953 primary_get_hw_state(to_intel_plane(primary));
15954
15955 if (plane_state->visible)
15956 crtc->base.state->plane_mask |= 1 << drm_plane_index(primary);
98ec7739
VS
15957}
15958
30e984df 15959static void intel_modeset_readout_hw_state(struct drm_device *dev)
24929352 15960{
fac5e23e 15961 struct drm_i915_private *dev_priv = to_i915(dev);
24929352 15962 enum pipe pipe;
24929352
DV
15963 struct intel_crtc *crtc;
15964 struct intel_encoder *encoder;
15965 struct intel_connector *connector;
5358901f 15966 int i;
24929352 15967
565602d7
ML
15968 dev_priv->active_crtcs = 0;
15969
d3fcc808 15970 for_each_intel_crtc(dev, crtc) {
565602d7
ML
15971 struct intel_crtc_state *crtc_state = crtc->config;
15972 int pixclk = 0;
3b117c8f 15973
ec2dc6a0 15974 __drm_atomic_helper_crtc_destroy_state(&crtc_state->base);
565602d7
ML
15975 memset(crtc_state, 0, sizeof(*crtc_state));
15976 crtc_state->base.crtc = &crtc->base;
24929352 15977
565602d7
ML
15978 crtc_state->base.active = crtc_state->base.enable =
15979 dev_priv->display.get_pipe_config(crtc, crtc_state);
15980
15981 crtc->base.enabled = crtc_state->base.enable;
15982 crtc->active = crtc_state->base.active;
15983
15984 if (crtc_state->base.active) {
15985 dev_priv->active_crtcs |= 1 << crtc->pipe;
15986
c89e39f3 15987 if (INTEL_GEN(dev_priv) >= 9 || IS_BROADWELL(dev_priv))
565602d7 15988 pixclk = ilk_pipe_pixel_rate(crtc_state);
9558d15d 15989 else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
565602d7
ML
15990 pixclk = crtc_state->base.adjusted_mode.crtc_clock;
15991 else
15992 WARN_ON(dev_priv->display.modeset_calc_cdclk);
9558d15d
VS
15993
15994 /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
15995 if (IS_BROADWELL(dev_priv) && crtc_state->ips_enabled)
15996 pixclk = DIV_ROUND_UP(pixclk * 100, 95);
565602d7
ML
15997 }
15998
15999 dev_priv->min_pixclk[crtc->pipe] = pixclk;
b70709a6 16000
f9cd7b88 16001 readout_plane_state(crtc);
24929352 16002
78108b7c
VS
16003 DRM_DEBUG_KMS("[CRTC:%d:%s] hw state readout: %s\n",
16004 crtc->base.base.id, crtc->base.name,
24929352
DV
16005 crtc->active ? "enabled" : "disabled");
16006 }
16007
5358901f
DV
16008 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
16009 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
16010
2edd6443
ACO
16011 pll->on = pll->funcs.get_hw_state(dev_priv, pll,
16012 &pll->config.hw_state);
3e369b76 16013 pll->config.crtc_mask = 0;
d3fcc808 16014 for_each_intel_crtc(dev, crtc) {
2dd66ebd 16015 if (crtc->active && crtc->config->shared_dpll == pll)
3e369b76 16016 pll->config.crtc_mask |= 1 << crtc->pipe;
5358901f 16017 }
2dd66ebd 16018 pll->active_mask = pll->config.crtc_mask;
5358901f 16019
1e6f2ddc 16020 DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
3e369b76 16021 pll->name, pll->config.crtc_mask, pll->on);
5358901f
DV
16022 }
16023
b2784e15 16024 for_each_intel_encoder(dev, encoder) {
24929352
DV
16025 pipe = 0;
16026
16027 if (encoder->get_hw_state(encoder, &pipe)) {
045ac3b5
JB
16028 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
16029 encoder->base.crtc = &crtc->base;
253c84c8 16030 crtc->config->output_types |= 1 << encoder->type;
6e3c9717 16031 encoder->get_config(encoder, crtc->config);
24929352
DV
16032 } else {
16033 encoder->base.crtc = NULL;
16034 }
16035
6f2bcceb 16036 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
24929352 16037 encoder->base.base.id,
8e329a03 16038 encoder->base.name,
24929352 16039 encoder->base.crtc ? "enabled" : "disabled",
6f2bcceb 16040 pipe_name(pipe));
24929352
DV
16041 }
16042
3a3371ff 16043 for_each_intel_connector(dev, connector) {
24929352
DV
16044 if (connector->get_hw_state(connector)) {
16045 connector->base.dpms = DRM_MODE_DPMS_ON;
2aa974c9
ML
16046
16047 encoder = connector->encoder;
16048 connector->base.encoder = &encoder->base;
16049
16050 if (encoder->base.crtc &&
16051 encoder->base.crtc->state->active) {
16052 /*
16053 * This has to be done during hardware readout
16054 * because anything calling .crtc_disable may
16055 * rely on the connector_mask being accurate.
16056 */
16057 encoder->base.crtc->state->connector_mask |=
16058 1 << drm_connector_index(&connector->base);
e87a52b3
ML
16059 encoder->base.crtc->state->encoder_mask |=
16060 1 << drm_encoder_index(&encoder->base);
2aa974c9
ML
16061 }
16062
24929352
DV
16063 } else {
16064 connector->base.dpms = DRM_MODE_DPMS_OFF;
16065 connector->base.encoder = NULL;
16066 }
16067 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
16068 connector->base.base.id,
c23cc417 16069 connector->base.name,
24929352
DV
16070 connector->base.encoder ? "enabled" : "disabled");
16071 }
7f4c6284
VS
16072
16073 for_each_intel_crtc(dev, crtc) {
16074 crtc->base.hwmode = crtc->config->base.adjusted_mode;
16075
16076 memset(&crtc->base.mode, 0, sizeof(crtc->base.mode));
16077 if (crtc->base.state->active) {
16078 intel_mode_from_pipe_config(&crtc->base.mode, crtc->config);
16079 intel_mode_from_pipe_config(&crtc->base.state->adjusted_mode, crtc->config);
16080 WARN_ON(drm_atomic_set_mode_for_crtc(crtc->base.state, &crtc->base.mode));
16081
16082 /*
16083 * The initial mode needs to be set in order to keep
16084 * the atomic core happy. It wants a valid mode if the
16085 * crtc's enabled, so we do the above call.
16086 *
16087 * At this point some state updated by the connectors
16088 * in their ->detect() callback has not run yet, so
16089 * no recalculation can be done yet.
16090 *
16091 * Even if we could do a recalculation and modeset
16092 * right now it would cause a double modeset if
16093 * fbdev or userspace chooses a different initial mode.
16094 *
16095 * If that happens, someone indicated they wanted a
16096 * mode change, which means it's safe to do a full
16097 * recalculation.
16098 */
16099 crtc->base.state->mode.private_flags = I915_MODE_FLAG_INHERITED;
9eca6832
VS
16100
16101 drm_calc_timestamping_constants(&crtc->base, &crtc->base.hwmode);
16102 update_scanline_offset(crtc);
7f4c6284 16103 }
e3b247da
VS
16104
16105 intel_pipe_config_sanity_check(dev_priv, crtc->config);
7f4c6284 16106 }
30e984df
DV
16107}
16108
043e9bda
ML
16109/* Scan out the current hw modeset state,
16110 * and sanitizes it to the current state
16111 */
16112static void
16113intel_modeset_setup_hw_state(struct drm_device *dev)
30e984df 16114{
fac5e23e 16115 struct drm_i915_private *dev_priv = to_i915(dev);
30e984df 16116 enum pipe pipe;
30e984df
DV
16117 struct intel_crtc *crtc;
16118 struct intel_encoder *encoder;
35c95375 16119 int i;
30e984df
DV
16120
16121 intel_modeset_readout_hw_state(dev);
24929352
DV
16122
16123 /* HW state is read out, now we need to sanitize this mess. */
b2784e15 16124 for_each_intel_encoder(dev, encoder) {
24929352
DV
16125 intel_sanitize_encoder(encoder);
16126 }
16127
055e393f 16128 for_each_pipe(dev_priv, pipe) {
24929352
DV
16129 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
16130 intel_sanitize_crtc(crtc);
6e3c9717
ACO
16131 intel_dump_pipe_config(crtc, crtc->config,
16132 "[setup_hw_state]");
24929352 16133 }
9a935856 16134
d29b2f9d
ACO
16135 intel_modeset_update_connector_atomic_state(dev);
16136
35c95375
DV
16137 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
16138 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
16139
2dd66ebd 16140 if (!pll->on || pll->active_mask)
35c95375
DV
16141 continue;
16142
16143 DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
16144
2edd6443 16145 pll->funcs.disable(dev_priv, pll);
35c95375
DV
16146 pll->on = false;
16147 }
16148
666a4537 16149 if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev))
6eb1a681
VS
16150 vlv_wm_get_hw_state(dev);
16151 else if (IS_GEN9(dev))
3078999f
PB
16152 skl_wm_get_hw_state(dev);
16153 else if (HAS_PCH_SPLIT(dev))
243e6a44 16154 ilk_wm_get_hw_state(dev);
292b990e
ML
16155
16156 for_each_intel_crtc(dev, crtc) {
16157 unsigned long put_domains;
16158
74bff5f9 16159 put_domains = modeset_get_crtc_power_domains(&crtc->base, crtc->config);
292b990e
ML
16160 if (WARN_ON(put_domains))
16161 modeset_put_power_domains(dev_priv, put_domains);
16162 }
16163 intel_display_set_init_power(dev_priv, false);
010cf73d
PZ
16164
16165 intel_fbc_init_pipe_state(dev_priv);
043e9bda 16166}
7d0bc1ea 16167
043e9bda
ML
16168void intel_display_resume(struct drm_device *dev)
16169{
e2c8b870
ML
16170 struct drm_i915_private *dev_priv = to_i915(dev);
16171 struct drm_atomic_state *state = dev_priv->modeset_restore_state;
16172 struct drm_modeset_acquire_ctx ctx;
043e9bda 16173 int ret;
e2c8b870 16174 bool setup = false;
f30da187 16175
e2c8b870 16176 dev_priv->modeset_restore_state = NULL;
043e9bda 16177
ea49c9ac
ML
16178 /*
16179 * This is a cludge because with real atomic modeset mode_config.mutex
16180 * won't be taken. Unfortunately some probed state like
16181 * audio_codec_enable is still protected by mode_config.mutex, so lock
16182 * it here for now.
16183 */
16184 mutex_lock(&dev->mode_config.mutex);
e2c8b870 16185 drm_modeset_acquire_init(&ctx, 0);
043e9bda 16186
e2c8b870
ML
16187retry:
16188 ret = drm_modeset_lock_all_ctx(dev, &ctx);
043e9bda 16189
e2c8b870
ML
16190 if (ret == 0 && !setup) {
16191 setup = true;
043e9bda 16192
e2c8b870
ML
16193 intel_modeset_setup_hw_state(dev);
16194 i915_redisable_vga(dev);
45e2b5f6 16195 }
8af6cf88 16196
e2c8b870
ML
16197 if (ret == 0 && state) {
16198 struct drm_crtc_state *crtc_state;
16199 struct drm_crtc *crtc;
16200 int i;
043e9bda 16201
e2c8b870
ML
16202 state->acquire_ctx = &ctx;
16203
e3d5457c
VS
16204 /* ignore any reset values/BIOS leftovers in the WM registers */
16205 to_intel_atomic_state(state)->skip_intermediate_wm = true;
16206
e2c8b870
ML
16207 for_each_crtc_in_state(state, crtc, crtc_state, i) {
16208 /*
16209 * Force recalculation even if we restore
16210 * current state. With fast modeset this may not result
16211 * in a modeset when the state is compatible.
16212 */
16213 crtc_state->mode_changed = true;
16214 }
16215
16216 ret = drm_atomic_commit(state);
043e9bda
ML
16217 }
16218
e2c8b870
ML
16219 if (ret == -EDEADLK) {
16220 drm_modeset_backoff(&ctx);
16221 goto retry;
16222 }
043e9bda 16223
e2c8b870
ML
16224 drm_modeset_drop_locks(&ctx);
16225 drm_modeset_acquire_fini(&ctx);
ea49c9ac 16226 mutex_unlock(&dev->mode_config.mutex);
043e9bda 16227
e2c8b870
ML
16228 if (ret) {
16229 DRM_ERROR("Restoring old state failed with %i\n", ret);
16230 drm_atomic_state_free(state);
16231 }
2c7111db
CW
16232}
16233
16234void intel_modeset_gem_init(struct drm_device *dev)
16235{
dc97997a 16236 struct drm_i915_private *dev_priv = to_i915(dev);
484b41dd 16237 struct drm_crtc *c;
2ff8fde1 16238 struct drm_i915_gem_object *obj;
e0d6149b 16239 int ret;
484b41dd 16240
dc97997a 16241 intel_init_gt_powersave(dev_priv);
ae48434c 16242
1833b134 16243 intel_modeset_init_hw(dev);
02e792fb 16244
1ee8da6d 16245 intel_setup_overlay(dev_priv);
484b41dd
JB
16246
16247 /*
16248 * Make sure any fbs we allocated at startup are properly
16249 * pinned & fenced. When we do the allocation it's too early
16250 * for this.
16251 */
70e1e0ec 16252 for_each_crtc(dev, c) {
2ff8fde1
MR
16253 obj = intel_fb_obj(c->primary->fb);
16254 if (obj == NULL)
484b41dd
JB
16255 continue;
16256
e0d6149b 16257 mutex_lock(&dev->struct_mutex);
3465c580
VS
16258 ret = intel_pin_and_fence_fb_obj(c->primary->fb,
16259 c->primary->state->rotation);
e0d6149b
TU
16260 mutex_unlock(&dev->struct_mutex);
16261 if (ret) {
484b41dd
JB
16262 DRM_ERROR("failed to pin boot fb on pipe %d\n",
16263 to_intel_crtc(c)->pipe);
66e514c1 16264 drm_framebuffer_unreference(c->primary->fb);
5a21b665 16265 c->primary->fb = NULL;
36750f28 16266 c->primary->crtc = c->primary->state->crtc = NULL;
5a21b665 16267 update_state_fb(c->primary);
36750f28 16268 c->state->plane_mask &= ~(1 << drm_plane_index(c->primary));
484b41dd
JB
16269 }
16270 }
1ebaa0b9
CW
16271}
16272
16273int intel_connector_register(struct drm_connector *connector)
16274{
16275 struct intel_connector *intel_connector = to_intel_connector(connector);
16276 int ret;
16277
16278 ret = intel_backlight_device_register(intel_connector);
16279 if (ret)
16280 goto err;
16281
16282 return 0;
0962c3c9 16283
1ebaa0b9
CW
16284err:
16285 return ret;
79e53945
JB
16286}
16287
c191eca1 16288void intel_connector_unregister(struct drm_connector *connector)
4932e2c3 16289{
e63d87c0 16290 struct intel_connector *intel_connector = to_intel_connector(connector);
4932e2c3 16291
e63d87c0 16292 intel_backlight_device_unregister(intel_connector);
4932e2c3 16293 intel_panel_destroy_backlight(connector);
4932e2c3
ID
16294}
16295
79e53945
JB
16296void intel_modeset_cleanup(struct drm_device *dev)
16297{
fac5e23e 16298 struct drm_i915_private *dev_priv = to_i915(dev);
652c393a 16299
dc97997a 16300 intel_disable_gt_powersave(dev_priv);
2eb5252e 16301
fd0c0642
DV
16302 /*
16303 * Interrupts and polling as the first thing to avoid creating havoc.
2eb5252e 16304 * Too much stuff here (turning of connectors, ...) would
fd0c0642
DV
16305 * experience fancy races otherwise.
16306 */
2aeb7d3a 16307 intel_irq_uninstall(dev_priv);
eb21b92b 16308
fd0c0642
DV
16309 /*
16310 * Due to the hpd irq storm handling the hotplug work can re-arm the
16311 * poll handlers. Hence disable polling after hpd handling is shut down.
16312 */
f87ea761 16313 drm_kms_helper_poll_fini(dev);
fd0c0642 16314
723bfd70
JB
16315 intel_unregister_dsm_handler();
16316
c937ab3e 16317 intel_fbc_global_disable(dev_priv);
69341a5e 16318
1630fe75
CW
16319 /* flush any delayed tasks or pending work */
16320 flush_scheduled_work();
16321
79e53945 16322 drm_mode_config_cleanup(dev);
4d7bb011 16323
1ee8da6d 16324 intel_cleanup_overlay(dev_priv);
ae48434c 16325
dc97997a 16326 intel_cleanup_gt_powersave(dev_priv);
f5949141
DV
16327
16328 intel_teardown_gmbus(dev);
79e53945
JB
16329}
16330
df0e9248
CW
16331void intel_connector_attach_encoder(struct intel_connector *connector,
16332 struct intel_encoder *encoder)
16333{
16334 connector->encoder = encoder;
16335 drm_mode_connector_attach_encoder(&connector->base,
16336 &encoder->base);
79e53945 16337}
28d52043
DA
16338
16339/*
16340 * set vga decode state - true == enable VGA decode
16341 */
16342int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
16343{
fac5e23e 16344 struct drm_i915_private *dev_priv = to_i915(dev);
a885b3cc 16345 unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
28d52043
DA
16346 u16 gmch_ctrl;
16347
75fa041d
CW
16348 if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
16349 DRM_ERROR("failed to read control word\n");
16350 return -EIO;
16351 }
16352
c0cc8a55
CW
16353 if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
16354 return 0;
16355
28d52043
DA
16356 if (state)
16357 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
16358 else
16359 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
75fa041d
CW
16360
16361 if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
16362 DRM_ERROR("failed to write control word\n");
16363 return -EIO;
16364 }
16365
28d52043
DA
16366 return 0;
16367}
c4a1d9e4 16368
c4a1d9e4 16369struct intel_display_error_state {
ff57f1b0
PZ
16370
16371 u32 power_well_driver;
16372
63b66e5b
CW
16373 int num_transcoders;
16374
c4a1d9e4
CW
16375 struct intel_cursor_error_state {
16376 u32 control;
16377 u32 position;
16378 u32 base;
16379 u32 size;
52331309 16380 } cursor[I915_MAX_PIPES];
c4a1d9e4
CW
16381
16382 struct intel_pipe_error_state {
ddf9c536 16383 bool power_domain_on;
c4a1d9e4 16384 u32 source;
f301b1e1 16385 u32 stat;
52331309 16386 } pipe[I915_MAX_PIPES];
c4a1d9e4
CW
16387
16388 struct intel_plane_error_state {
16389 u32 control;
16390 u32 stride;
16391 u32 size;
16392 u32 pos;
16393 u32 addr;
16394 u32 surface;
16395 u32 tile_offset;
52331309 16396 } plane[I915_MAX_PIPES];
63b66e5b
CW
16397
16398 struct intel_transcoder_error_state {
ddf9c536 16399 bool power_domain_on;
63b66e5b
CW
16400 enum transcoder cpu_transcoder;
16401
16402 u32 conf;
16403
16404 u32 htotal;
16405 u32 hblank;
16406 u32 hsync;
16407 u32 vtotal;
16408 u32 vblank;
16409 u32 vsync;
16410 } transcoder[4];
c4a1d9e4
CW
16411};
16412
16413struct intel_display_error_state *
c033666a 16414intel_display_capture_error_state(struct drm_i915_private *dev_priv)
c4a1d9e4 16415{
c4a1d9e4 16416 struct intel_display_error_state *error;
63b66e5b
CW
16417 int transcoders[] = {
16418 TRANSCODER_A,
16419 TRANSCODER_B,
16420 TRANSCODER_C,
16421 TRANSCODER_EDP,
16422 };
c4a1d9e4
CW
16423 int i;
16424
c033666a 16425 if (INTEL_INFO(dev_priv)->num_pipes == 0)
63b66e5b
CW
16426 return NULL;
16427
9d1cb914 16428 error = kzalloc(sizeof(*error), GFP_ATOMIC);
c4a1d9e4
CW
16429 if (error == NULL)
16430 return NULL;
16431
c033666a 16432 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
ff57f1b0
PZ
16433 error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
16434
055e393f 16435 for_each_pipe(dev_priv, i) {
ddf9c536 16436 error->pipe[i].power_domain_on =
f458ebbc
DV
16437 __intel_display_power_is_enabled(dev_priv,
16438 POWER_DOMAIN_PIPE(i));
ddf9c536 16439 if (!error->pipe[i].power_domain_on)
9d1cb914
PZ
16440 continue;
16441
5efb3e28
VS
16442 error->cursor[i].control = I915_READ(CURCNTR(i));
16443 error->cursor[i].position = I915_READ(CURPOS(i));
16444 error->cursor[i].base = I915_READ(CURBASE(i));
c4a1d9e4
CW
16445
16446 error->plane[i].control = I915_READ(DSPCNTR(i));
16447 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
c033666a 16448 if (INTEL_GEN(dev_priv) <= 3) {
51889b35 16449 error->plane[i].size = I915_READ(DSPSIZE(i));
80ca378b
PZ
16450 error->plane[i].pos = I915_READ(DSPPOS(i));
16451 }
c033666a 16452 if (INTEL_GEN(dev_priv) <= 7 && !IS_HASWELL(dev_priv))
ca291363 16453 error->plane[i].addr = I915_READ(DSPADDR(i));
c033666a 16454 if (INTEL_GEN(dev_priv) >= 4) {
c4a1d9e4
CW
16455 error->plane[i].surface = I915_READ(DSPSURF(i));
16456 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
16457 }
16458
c4a1d9e4 16459 error->pipe[i].source = I915_READ(PIPESRC(i));
f301b1e1 16460
c033666a 16461 if (HAS_GMCH_DISPLAY(dev_priv))
f301b1e1 16462 error->pipe[i].stat = I915_READ(PIPESTAT(i));
63b66e5b
CW
16463 }
16464
4d1de975 16465 /* Note: this does not include DSI transcoders. */
c033666a 16466 error->num_transcoders = INTEL_INFO(dev_priv)->num_pipes;
2d1fe073 16467 if (HAS_DDI(dev_priv))
63b66e5b
CW
16468 error->num_transcoders++; /* Account for eDP. */
16469
16470 for (i = 0; i < error->num_transcoders; i++) {
16471 enum transcoder cpu_transcoder = transcoders[i];
16472
ddf9c536 16473 error->transcoder[i].power_domain_on =
f458ebbc 16474 __intel_display_power_is_enabled(dev_priv,
38cc1daf 16475 POWER_DOMAIN_TRANSCODER(cpu_transcoder));
ddf9c536 16476 if (!error->transcoder[i].power_domain_on)
9d1cb914
PZ
16477 continue;
16478
63b66e5b
CW
16479 error->transcoder[i].cpu_transcoder = cpu_transcoder;
16480
16481 error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
16482 error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
16483 error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
16484 error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
16485 error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
16486 error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
16487 error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
c4a1d9e4
CW
16488 }
16489
16490 return error;
16491}
16492
edc3d884
MK
16493#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
16494
c4a1d9e4 16495void
edc3d884 16496intel_display_print_error_state(struct drm_i915_error_state_buf *m,
c4a1d9e4
CW
16497 struct drm_device *dev,
16498 struct intel_display_error_state *error)
16499{
fac5e23e 16500 struct drm_i915_private *dev_priv = to_i915(dev);
c4a1d9e4
CW
16501 int i;
16502
63b66e5b
CW
16503 if (!error)
16504 return;
16505
edc3d884 16506 err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
190be112 16507 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
edc3d884 16508 err_printf(m, "PWR_WELL_CTL2: %08x\n",
ff57f1b0 16509 error->power_well_driver);
055e393f 16510 for_each_pipe(dev_priv, i) {
edc3d884 16511 err_printf(m, "Pipe [%d]:\n", i);
ddf9c536 16512 err_printf(m, " Power: %s\n",
87ad3212 16513 onoff(error->pipe[i].power_domain_on));
edc3d884 16514 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
f301b1e1 16515 err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
edc3d884
MK
16516
16517 err_printf(m, "Plane [%d]:\n", i);
16518 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
16519 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
80ca378b 16520 if (INTEL_INFO(dev)->gen <= 3) {
edc3d884
MK
16521 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
16522 err_printf(m, " POS: %08x\n", error->plane[i].pos);
80ca378b 16523 }
4b71a570 16524 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
edc3d884 16525 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
c4a1d9e4 16526 if (INTEL_INFO(dev)->gen >= 4) {
edc3d884
MK
16527 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
16528 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
c4a1d9e4
CW
16529 }
16530
edc3d884
MK
16531 err_printf(m, "Cursor [%d]:\n", i);
16532 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
16533 err_printf(m, " POS: %08x\n", error->cursor[i].position);
16534 err_printf(m, " BASE: %08x\n", error->cursor[i].base);
c4a1d9e4 16535 }
63b66e5b
CW
16536
16537 for (i = 0; i < error->num_transcoders; i++) {
da205630 16538 err_printf(m, "CPU transcoder: %s\n",
63b66e5b 16539 transcoder_name(error->transcoder[i].cpu_transcoder));
ddf9c536 16540 err_printf(m, " Power: %s\n",
87ad3212 16541 onoff(error->transcoder[i].power_domain_on));
63b66e5b
CW
16542 err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
16543 err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
16544 err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
16545 err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
16546 err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
16547 err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
16548 err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
16549 }
c4a1d9e4 16550}