]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/gpu/drm/i915/intel_display.c
drm/i915: Convert wait_for(I915_READ(reg)) to intel_wait_for_register()
[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
e0638cdf
PZ
533/**
534 * Returns whether any output on the specified pipe is of the specified type
535 */
4093561b 536bool intel_pipe_has_type(struct intel_crtc *crtc, enum intel_output_type type)
e0638cdf 537{
409ee761 538 struct drm_device *dev = crtc->base.dev;
e0638cdf
PZ
539 struct intel_encoder *encoder;
540
409ee761 541 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
e0638cdf
PZ
542 if (encoder->type == type)
543 return true;
544
545 return false;
546}
547
d0737e1d
ACO
548/**
549 * Returns whether any output on the specified pipe will have the specified
550 * type after a staged modeset is complete, i.e., the same as
551 * intel_pipe_has_type() but looking at encoder->new_crtc instead of
552 * encoder->crtc.
553 */
a93e255f
ACO
554static bool intel_pipe_will_have_type(const struct intel_crtc_state *crtc_state,
555 int type)
d0737e1d 556{
a93e255f 557 struct drm_atomic_state *state = crtc_state->base.state;
da3ced29 558 struct drm_connector *connector;
a93e255f 559 struct drm_connector_state *connector_state;
d0737e1d 560 struct intel_encoder *encoder;
a93e255f
ACO
561 int i, num_connectors = 0;
562
da3ced29 563 for_each_connector_in_state(state, connector, connector_state, i) {
a93e255f
ACO
564 if (connector_state->crtc != crtc_state->base.crtc)
565 continue;
566
567 num_connectors++;
d0737e1d 568
a93e255f
ACO
569 encoder = to_intel_encoder(connector_state->best_encoder);
570 if (encoder->type == type)
d0737e1d 571 return true;
a93e255f
ACO
572 }
573
574 WARN_ON(num_connectors == 0);
d0737e1d
ACO
575
576 return false;
577}
578
dccbea3b
ID
579/*
580 * Platform specific helpers to calculate the port PLL loopback- (clock.m),
581 * and post-divider (clock.p) values, pre- (clock.vco) and post-divided fast
582 * (clock.dot) clock rates. This fast dot clock is fed to the port's IO logic.
583 * The helpers' return value is the rate of the clock that is fed to the
584 * display engine's pipe which can be the above fast dot clock rate or a
585 * divided-down version of it.
586 */
f2b115e6 587/* m1 is reserved as 0 in Pineview, n is a ring counter */
9e2c8475 588static int pnv_calc_dpll_params(int refclk, struct dpll *clock)
79e53945 589{
2177832f
SL
590 clock->m = clock->m2 + 2;
591 clock->p = clock->p1 * clock->p2;
ed5ca77e 592 if (WARN_ON(clock->n == 0 || clock->p == 0))
dccbea3b 593 return 0;
fb03ac01
VS
594 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
595 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
dccbea3b
ID
596
597 return clock->dot;
2177832f
SL
598}
599
7429e9d4
DV
600static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
601{
602 return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
603}
604
9e2c8475 605static int i9xx_calc_dpll_params(int refclk, struct dpll *clock)
2177832f 606{
7429e9d4 607 clock->m = i9xx_dpll_compute_m(clock);
79e53945 608 clock->p = clock->p1 * clock->p2;
ed5ca77e 609 if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
dccbea3b 610 return 0;
fb03ac01
VS
611 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
612 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
dccbea3b
ID
613
614 return clock->dot;
79e53945
JB
615}
616
9e2c8475 617static int vlv_calc_dpll_params(int refclk, struct dpll *clock)
589eca67
ID
618{
619 clock->m = clock->m1 * clock->m2;
620 clock->p = clock->p1 * clock->p2;
621 if (WARN_ON(clock->n == 0 || clock->p == 0))
dccbea3b 622 return 0;
589eca67
ID
623 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
624 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
dccbea3b
ID
625
626 return clock->dot / 5;
589eca67
ID
627}
628
9e2c8475 629int chv_calc_dpll_params(int refclk, struct dpll *clock)
ef9348c8
CML
630{
631 clock->m = clock->m1 * clock->m2;
632 clock->p = clock->p1 * clock->p2;
633 if (WARN_ON(clock->n == 0 || clock->p == 0))
dccbea3b 634 return 0;
ef9348c8
CML
635 clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
636 clock->n << 22);
637 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
dccbea3b
ID
638
639 return clock->dot / 5;
ef9348c8
CML
640}
641
7c04d1d9 642#define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
79e53945
JB
643/**
644 * Returns whether the given set of divisors are valid for a given refclk with
645 * the given connectors.
646 */
647
1b894b59 648static bool intel_PLL_is_valid(struct drm_device *dev,
1b6f4958 649 const struct intel_limit *limit,
9e2c8475 650 const struct dpll *clock)
79e53945 651{
f01b7962
VS
652 if (clock->n < limit->n.min || limit->n.max < clock->n)
653 INTELPllInvalid("n out of range\n");
79e53945 654 if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
0206e353 655 INTELPllInvalid("p1 out of range\n");
79e53945 656 if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
0206e353 657 INTELPllInvalid("m2 out of range\n");
79e53945 658 if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
0206e353 659 INTELPllInvalid("m1 out of range\n");
f01b7962 660
666a4537
WB
661 if (!IS_PINEVIEW(dev) && !IS_VALLEYVIEW(dev) &&
662 !IS_CHERRYVIEW(dev) && !IS_BROXTON(dev))
f01b7962
VS
663 if (clock->m1 <= clock->m2)
664 INTELPllInvalid("m1 <= m2\n");
665
666a4537 666 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev) && !IS_BROXTON(dev)) {
f01b7962
VS
667 if (clock->p < limit->p.min || limit->p.max < clock->p)
668 INTELPllInvalid("p out of range\n");
669 if (clock->m < limit->m.min || limit->m.max < clock->m)
670 INTELPllInvalid("m out of range\n");
671 }
672
79e53945 673 if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
0206e353 674 INTELPllInvalid("vco out of range\n");
79e53945
JB
675 /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
676 * connector, etc., rather than just a single range.
677 */
678 if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
0206e353 679 INTELPllInvalid("dot out of range\n");
79e53945
JB
680
681 return true;
682}
683
3b1429d9 684static int
1b6f4958 685i9xx_select_p2_div(const struct intel_limit *limit,
3b1429d9
VS
686 const struct intel_crtc_state *crtc_state,
687 int target)
79e53945 688{
3b1429d9 689 struct drm_device *dev = crtc_state->base.crtc->dev;
79e53945 690
a93e255f 691 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
79e53945 692 /*
a210b028
DV
693 * For LVDS just rely on its current settings for dual-channel.
694 * We haven't figured out how to reliably set up different
695 * single/dual channel state, if we even can.
79e53945 696 */
1974cad0 697 if (intel_is_dual_link_lvds(dev))
3b1429d9 698 return limit->p2.p2_fast;
79e53945 699 else
3b1429d9 700 return limit->p2.p2_slow;
79e53945
JB
701 } else {
702 if (target < limit->p2.dot_limit)
3b1429d9 703 return limit->p2.p2_slow;
79e53945 704 else
3b1429d9 705 return limit->p2.p2_fast;
79e53945 706 }
3b1429d9
VS
707}
708
70e8aa21
ACO
709/*
710 * Returns a set of divisors for the desired target clock with the given
711 * refclk, or FALSE. The returned values represent the clock equation:
712 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
713 *
714 * Target and reference clocks are specified in kHz.
715 *
716 * If match_clock is provided, then best_clock P divider must match the P
717 * divider from @match_clock used for LVDS downclocking.
718 */
3b1429d9 719static bool
1b6f4958 720i9xx_find_best_dpll(const struct intel_limit *limit,
3b1429d9 721 struct intel_crtc_state *crtc_state,
9e2c8475
ACO
722 int target, int refclk, struct dpll *match_clock,
723 struct dpll *best_clock)
3b1429d9
VS
724{
725 struct drm_device *dev = crtc_state->base.crtc->dev;
9e2c8475 726 struct dpll clock;
3b1429d9 727 int err = target;
79e53945 728
0206e353 729 memset(best_clock, 0, sizeof(*best_clock));
79e53945 730
3b1429d9
VS
731 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
732
42158660
ZY
733 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
734 clock.m1++) {
735 for (clock.m2 = limit->m2.min;
736 clock.m2 <= limit->m2.max; clock.m2++) {
c0efc387 737 if (clock.m2 >= clock.m1)
42158660
ZY
738 break;
739 for (clock.n = limit->n.min;
740 clock.n <= limit->n.max; clock.n++) {
741 for (clock.p1 = limit->p1.min;
742 clock.p1 <= limit->p1.max; clock.p1++) {
79e53945
JB
743 int this_err;
744
dccbea3b 745 i9xx_calc_dpll_params(refclk, &clock);
ac58c3f0
DV
746 if (!intel_PLL_is_valid(dev, limit,
747 &clock))
748 continue;
749 if (match_clock &&
750 clock.p != match_clock->p)
751 continue;
752
753 this_err = abs(clock.dot - target);
754 if (this_err < err) {
755 *best_clock = clock;
756 err = this_err;
757 }
758 }
759 }
760 }
761 }
762
763 return (err != target);
764}
765
70e8aa21
ACO
766/*
767 * Returns a set of divisors for the desired target clock with the given
768 * refclk, or FALSE. The returned values represent the clock equation:
769 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
770 *
771 * Target and reference clocks are specified in kHz.
772 *
773 * If match_clock is provided, then best_clock P divider must match the P
774 * divider from @match_clock used for LVDS downclocking.
775 */
ac58c3f0 776static bool
1b6f4958 777pnv_find_best_dpll(const struct intel_limit *limit,
a93e255f 778 struct intel_crtc_state *crtc_state,
9e2c8475
ACO
779 int target, int refclk, struct dpll *match_clock,
780 struct dpll *best_clock)
79e53945 781{
3b1429d9 782 struct drm_device *dev = crtc_state->base.crtc->dev;
9e2c8475 783 struct dpll clock;
79e53945
JB
784 int err = target;
785
0206e353 786 memset(best_clock, 0, sizeof(*best_clock));
79e53945 787
3b1429d9
VS
788 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
789
42158660
ZY
790 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
791 clock.m1++) {
792 for (clock.m2 = limit->m2.min;
793 clock.m2 <= limit->m2.max; clock.m2++) {
42158660
ZY
794 for (clock.n = limit->n.min;
795 clock.n <= limit->n.max; clock.n++) {
796 for (clock.p1 = limit->p1.min;
797 clock.p1 <= limit->p1.max; clock.p1++) {
79e53945
JB
798 int this_err;
799
dccbea3b 800 pnv_calc_dpll_params(refclk, &clock);
1b894b59
CW
801 if (!intel_PLL_is_valid(dev, limit,
802 &clock))
79e53945 803 continue;
cec2f356
SP
804 if (match_clock &&
805 clock.p != match_clock->p)
806 continue;
79e53945
JB
807
808 this_err = abs(clock.dot - target);
809 if (this_err < err) {
810 *best_clock = clock;
811 err = this_err;
812 }
813 }
814 }
815 }
816 }
817
818 return (err != target);
819}
820
997c030c
ACO
821/*
822 * Returns a set of divisors for the desired target clock with the given
823 * refclk, or FALSE. The returned values represent the clock equation:
824 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
70e8aa21
ACO
825 *
826 * Target and reference clocks are specified in kHz.
827 *
828 * If match_clock is provided, then best_clock P divider must match the P
829 * divider from @match_clock used for LVDS downclocking.
997c030c 830 */
d4906093 831static bool
1b6f4958 832g4x_find_best_dpll(const struct intel_limit *limit,
a93e255f 833 struct intel_crtc_state *crtc_state,
9e2c8475
ACO
834 int target, int refclk, struct dpll *match_clock,
835 struct dpll *best_clock)
d4906093 836{
3b1429d9 837 struct drm_device *dev = crtc_state->base.crtc->dev;
9e2c8475 838 struct dpll clock;
d4906093 839 int max_n;
3b1429d9 840 bool found = false;
6ba770dc
AJ
841 /* approximately equals target * 0.00585 */
842 int err_most = (target >> 8) + (target >> 9);
d4906093
ML
843
844 memset(best_clock, 0, sizeof(*best_clock));
3b1429d9
VS
845
846 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
847
d4906093 848 max_n = limit->n.max;
f77f13e2 849 /* based on hardware requirement, prefer smaller n to precision */
d4906093 850 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
f77f13e2 851 /* based on hardware requirement, prefere larger m1,m2 */
d4906093
ML
852 for (clock.m1 = limit->m1.max;
853 clock.m1 >= limit->m1.min; clock.m1--) {
854 for (clock.m2 = limit->m2.max;
855 clock.m2 >= limit->m2.min; clock.m2--) {
856 for (clock.p1 = limit->p1.max;
857 clock.p1 >= limit->p1.min; clock.p1--) {
858 int this_err;
859
dccbea3b 860 i9xx_calc_dpll_params(refclk, &clock);
1b894b59
CW
861 if (!intel_PLL_is_valid(dev, limit,
862 &clock))
d4906093 863 continue;
1b894b59
CW
864
865 this_err = abs(clock.dot - target);
d4906093
ML
866 if (this_err < err_most) {
867 *best_clock = clock;
868 err_most = this_err;
869 max_n = clock.n;
870 found = true;
871 }
872 }
873 }
874 }
875 }
2c07245f
ZW
876 return found;
877}
878
d5dd62bd
ID
879/*
880 * Check if the calculated PLL configuration is more optimal compared to the
881 * best configuration and error found so far. Return the calculated error.
882 */
883static bool vlv_PLL_is_optimal(struct drm_device *dev, int target_freq,
9e2c8475
ACO
884 const struct dpll *calculated_clock,
885 const struct dpll *best_clock,
d5dd62bd
ID
886 unsigned int best_error_ppm,
887 unsigned int *error_ppm)
888{
9ca3ba01
ID
889 /*
890 * For CHV ignore the error and consider only the P value.
891 * Prefer a bigger P value based on HW requirements.
892 */
893 if (IS_CHERRYVIEW(dev)) {
894 *error_ppm = 0;
895
896 return calculated_clock->p > best_clock->p;
897 }
898
24be4e46
ID
899 if (WARN_ON_ONCE(!target_freq))
900 return false;
901
d5dd62bd
ID
902 *error_ppm = div_u64(1000000ULL *
903 abs(target_freq - calculated_clock->dot),
904 target_freq);
905 /*
906 * Prefer a better P value over a better (smaller) error if the error
907 * is small. Ensure this preference for future configurations too by
908 * setting the error to 0.
909 */
910 if (*error_ppm < 100 && calculated_clock->p > best_clock->p) {
911 *error_ppm = 0;
912
913 return true;
914 }
915
916 return *error_ppm + 10 < best_error_ppm;
917}
918
65b3d6a9
ACO
919/*
920 * Returns a set of divisors for the desired target clock with the given
921 * refclk, or FALSE. The returned values represent the clock equation:
922 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
923 */
a0c4da24 924static bool
1b6f4958 925vlv_find_best_dpll(const struct intel_limit *limit,
a93e255f 926 struct intel_crtc_state *crtc_state,
9e2c8475
ACO
927 int target, int refclk, struct dpll *match_clock,
928 struct dpll *best_clock)
a0c4da24 929{
a93e255f 930 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
a919ff14 931 struct drm_device *dev = crtc->base.dev;
9e2c8475 932 struct dpll clock;
69e4f900 933 unsigned int bestppm = 1000000;
27e639bf
VS
934 /* min update 19.2 MHz */
935 int max_n = min(limit->n.max, refclk / 19200);
49e497ef 936 bool found = false;
a0c4da24 937
6b4bf1c4
VS
938 target *= 5; /* fast clock */
939
940 memset(best_clock, 0, sizeof(*best_clock));
a0c4da24
JB
941
942 /* based on hardware requirement, prefer smaller n to precision */
27e639bf 943 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
811bbf05 944 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
889059d8 945 for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
c1a9ae43 946 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
6b4bf1c4 947 clock.p = clock.p1 * clock.p2;
a0c4da24 948 /* based on hardware requirement, prefer bigger m1,m2 values */
6b4bf1c4 949 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
d5dd62bd 950 unsigned int ppm;
69e4f900 951
6b4bf1c4
VS
952 clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
953 refclk * clock.m1);
954
dccbea3b 955 vlv_calc_dpll_params(refclk, &clock);
43b0ac53 956
f01b7962
VS
957 if (!intel_PLL_is_valid(dev, limit,
958 &clock))
43b0ac53
VS
959 continue;
960
d5dd62bd
ID
961 if (!vlv_PLL_is_optimal(dev, target,
962 &clock,
963 best_clock,
964 bestppm, &ppm))
965 continue;
6b4bf1c4 966
d5dd62bd
ID
967 *best_clock = clock;
968 bestppm = ppm;
969 found = true;
a0c4da24
JB
970 }
971 }
972 }
973 }
a0c4da24 974
49e497ef 975 return found;
a0c4da24 976}
a4fc5ed6 977
65b3d6a9
ACO
978/*
979 * Returns a set of divisors for the desired target clock with the given
980 * refclk, or FALSE. The returned values represent the clock equation:
981 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
982 */
ef9348c8 983static bool
1b6f4958 984chv_find_best_dpll(const struct intel_limit *limit,
a93e255f 985 struct intel_crtc_state *crtc_state,
9e2c8475
ACO
986 int target, int refclk, struct dpll *match_clock,
987 struct dpll *best_clock)
ef9348c8 988{
a93e255f 989 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
a919ff14 990 struct drm_device *dev = crtc->base.dev;
9ca3ba01 991 unsigned int best_error_ppm;
9e2c8475 992 struct dpll clock;
ef9348c8
CML
993 uint64_t m2;
994 int found = false;
995
996 memset(best_clock, 0, sizeof(*best_clock));
9ca3ba01 997 best_error_ppm = 1000000;
ef9348c8
CML
998
999 /*
1000 * Based on hardware doc, the n always set to 1, and m1 always
1001 * set to 2. If requires to support 200Mhz refclk, we need to
1002 * revisit this because n may not 1 anymore.
1003 */
1004 clock.n = 1, clock.m1 = 2;
1005 target *= 5; /* fast clock */
1006
1007 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
1008 for (clock.p2 = limit->p2.p2_fast;
1009 clock.p2 >= limit->p2.p2_slow;
1010 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
9ca3ba01 1011 unsigned int error_ppm;
ef9348c8
CML
1012
1013 clock.p = clock.p1 * clock.p2;
1014
1015 m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
1016 clock.n) << 22, refclk * clock.m1);
1017
1018 if (m2 > INT_MAX/clock.m1)
1019 continue;
1020
1021 clock.m2 = m2;
1022
dccbea3b 1023 chv_calc_dpll_params(refclk, &clock);
ef9348c8
CML
1024
1025 if (!intel_PLL_is_valid(dev, limit, &clock))
1026 continue;
1027
9ca3ba01
ID
1028 if (!vlv_PLL_is_optimal(dev, target, &clock, best_clock,
1029 best_error_ppm, &error_ppm))
1030 continue;
1031
1032 *best_clock = clock;
1033 best_error_ppm = error_ppm;
1034 found = true;
ef9348c8
CML
1035 }
1036 }
1037
1038 return found;
1039}
1040
5ab7b0b7 1041bool bxt_find_best_dpll(struct intel_crtc_state *crtc_state, int target_clock,
9e2c8475 1042 struct dpll *best_clock)
5ab7b0b7 1043{
65b3d6a9 1044 int refclk = 100000;
1b6f4958 1045 const struct intel_limit *limit = &intel_limits_bxt;
5ab7b0b7 1046
65b3d6a9 1047 return chv_find_best_dpll(limit, crtc_state,
5ab7b0b7
ID
1048 target_clock, refclk, NULL, best_clock);
1049}
1050
20ddf665
VS
1051bool intel_crtc_active(struct drm_crtc *crtc)
1052{
1053 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1054
1055 /* Be paranoid as we can arrive here with only partial
1056 * state retrieved from the hardware during setup.
1057 *
241bfc38 1058 * We can ditch the adjusted_mode.crtc_clock check as soon
20ddf665
VS
1059 * as Haswell has gained clock readout/fastboot support.
1060 *
66e514c1 1061 * We can ditch the crtc->primary->fb check as soon as we can
20ddf665 1062 * properly reconstruct framebuffers.
c3d1f436
MR
1063 *
1064 * FIXME: The intel_crtc->active here should be switched to
1065 * crtc->state->active once we have proper CRTC states wired up
1066 * for atomic.
20ddf665 1067 */
c3d1f436 1068 return intel_crtc->active && crtc->primary->state->fb &&
6e3c9717 1069 intel_crtc->config->base.adjusted_mode.crtc_clock;
20ddf665
VS
1070}
1071
a5c961d1
PZ
1072enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
1073 enum pipe pipe)
1074{
1075 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
1076 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1077
6e3c9717 1078 return intel_crtc->config->cpu_transcoder;
a5c961d1
PZ
1079}
1080
fbf49ea2
VS
1081static bool pipe_dsl_stopped(struct drm_device *dev, enum pipe pipe)
1082{
1083 struct drm_i915_private *dev_priv = dev->dev_private;
f0f59a00 1084 i915_reg_t reg = PIPEDSL(pipe);
fbf49ea2
VS
1085 u32 line1, line2;
1086 u32 line_mask;
1087
1088 if (IS_GEN2(dev))
1089 line_mask = DSL_LINEMASK_GEN2;
1090 else
1091 line_mask = DSL_LINEMASK_GEN3;
1092
1093 line1 = I915_READ(reg) & line_mask;
6adfb1ef 1094 msleep(5);
fbf49ea2
VS
1095 line2 = I915_READ(reg) & line_mask;
1096
1097 return line1 == line2;
1098}
1099
ab7ad7f6
KP
1100/*
1101 * intel_wait_for_pipe_off - wait for pipe to turn off
575f7ab7 1102 * @crtc: crtc whose pipe to wait for
9d0498a2
JB
1103 *
1104 * After disabling a pipe, we can't wait for vblank in the usual way,
1105 * spinning on the vblank interrupt status bit, since we won't actually
1106 * see an interrupt when the pipe is disabled.
1107 *
ab7ad7f6
KP
1108 * On Gen4 and above:
1109 * wait for the pipe register state bit to turn off
1110 *
1111 * Otherwise:
1112 * wait for the display line value to settle (it usually
1113 * ends up stopping at the start of the next frame).
58e10eb9 1114 *
9d0498a2 1115 */
575f7ab7 1116static void intel_wait_for_pipe_off(struct intel_crtc *crtc)
9d0498a2 1117{
575f7ab7 1118 struct drm_device *dev = crtc->base.dev;
9d0498a2 1119 struct drm_i915_private *dev_priv = dev->dev_private;
6e3c9717 1120 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
575f7ab7 1121 enum pipe pipe = crtc->pipe;
ab7ad7f6
KP
1122
1123 if (INTEL_INFO(dev)->gen >= 4) {
f0f59a00 1124 i915_reg_t reg = PIPECONF(cpu_transcoder);
ab7ad7f6
KP
1125
1126 /* Wait for the Pipe State to go off */
b8511f53
CW
1127 if (intel_wait_for_register(dev_priv,
1128 reg, I965_PIPECONF_ACTIVE, 0,
1129 100))
284637d9 1130 WARN(1, "pipe_off wait timed out\n");
ab7ad7f6 1131 } else {
ab7ad7f6 1132 /* Wait for the display line to settle */
fbf49ea2 1133 if (wait_for(pipe_dsl_stopped(dev, pipe), 100))
284637d9 1134 WARN(1, "pipe_off wait timed out\n");
ab7ad7f6 1135 }
79e53945
JB
1136}
1137
b24e7179 1138/* Only for pre-ILK configs */
55607e8a
DV
1139void assert_pll(struct drm_i915_private *dev_priv,
1140 enum pipe pipe, bool state)
b24e7179 1141{
b24e7179
JB
1142 u32 val;
1143 bool cur_state;
1144
649636ef 1145 val = I915_READ(DPLL(pipe));
b24e7179 1146 cur_state = !!(val & DPLL_VCO_ENABLE);
e2c719b7 1147 I915_STATE_WARN(cur_state != state,
b24e7179 1148 "PLL state assertion failure (expected %s, current %s)\n",
87ad3212 1149 onoff(state), onoff(cur_state));
b24e7179 1150}
b24e7179 1151
23538ef1 1152/* XXX: the dsi pll is shared between MIPI DSI ports */
8563b1e8 1153void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
23538ef1
JN
1154{
1155 u32 val;
1156 bool cur_state;
1157
a580516d 1158 mutex_lock(&dev_priv->sb_lock);
23538ef1 1159 val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
a580516d 1160 mutex_unlock(&dev_priv->sb_lock);
23538ef1
JN
1161
1162 cur_state = val & DSI_PLL_VCO_EN;
e2c719b7 1163 I915_STATE_WARN(cur_state != state,
23538ef1 1164 "DSI PLL state assertion failure (expected %s, current %s)\n",
87ad3212 1165 onoff(state), onoff(cur_state));
23538ef1 1166}
23538ef1 1167
040484af
JB
1168static void assert_fdi_tx(struct drm_i915_private *dev_priv,
1169 enum pipe pipe, bool state)
1170{
040484af 1171 bool cur_state;
ad80a810
PZ
1172 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1173 pipe);
040484af 1174
2d1fe073 1175 if (HAS_DDI(dev_priv)) {
affa9354 1176 /* DDI does not have a specific FDI_TX register */
649636ef 1177 u32 val = I915_READ(TRANS_DDI_FUNC_CTL(cpu_transcoder));
ad80a810 1178 cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
bf507ef7 1179 } else {
649636ef 1180 u32 val = I915_READ(FDI_TX_CTL(pipe));
bf507ef7
ED
1181 cur_state = !!(val & FDI_TX_ENABLE);
1182 }
e2c719b7 1183 I915_STATE_WARN(cur_state != state,
040484af 1184 "FDI TX state assertion failure (expected %s, current %s)\n",
87ad3212 1185 onoff(state), onoff(cur_state));
040484af
JB
1186}
1187#define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
1188#define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
1189
1190static void assert_fdi_rx(struct drm_i915_private *dev_priv,
1191 enum pipe pipe, bool state)
1192{
040484af
JB
1193 u32 val;
1194 bool cur_state;
1195
649636ef 1196 val = I915_READ(FDI_RX_CTL(pipe));
d63fa0dc 1197 cur_state = !!(val & FDI_RX_ENABLE);
e2c719b7 1198 I915_STATE_WARN(cur_state != state,
040484af 1199 "FDI RX state assertion failure (expected %s, current %s)\n",
87ad3212 1200 onoff(state), onoff(cur_state));
040484af
JB
1201}
1202#define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
1203#define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
1204
1205static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
1206 enum pipe pipe)
1207{
040484af
JB
1208 u32 val;
1209
1210 /* ILK FDI PLL is always enabled */
7e22dbbb 1211 if (IS_GEN5(dev_priv))
040484af
JB
1212 return;
1213
bf507ef7 1214 /* On Haswell, DDI ports are responsible for the FDI PLL setup */
2d1fe073 1215 if (HAS_DDI(dev_priv))
bf507ef7
ED
1216 return;
1217
649636ef 1218 val = I915_READ(FDI_TX_CTL(pipe));
e2c719b7 1219 I915_STATE_WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
040484af
JB
1220}
1221
55607e8a
DV
1222void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
1223 enum pipe pipe, bool state)
040484af 1224{
040484af 1225 u32 val;
55607e8a 1226 bool cur_state;
040484af 1227
649636ef 1228 val = I915_READ(FDI_RX_CTL(pipe));
55607e8a 1229 cur_state = !!(val & FDI_RX_PLL_ENABLE);
e2c719b7 1230 I915_STATE_WARN(cur_state != state,
55607e8a 1231 "FDI RX PLL assertion failure (expected %s, current %s)\n",
87ad3212 1232 onoff(state), onoff(cur_state));
040484af
JB
1233}
1234
b680c37a
DV
1235void assert_panel_unlocked(struct drm_i915_private *dev_priv,
1236 enum pipe pipe)
ea0760cf 1237{
bedd4dba 1238 struct drm_device *dev = dev_priv->dev;
f0f59a00 1239 i915_reg_t pp_reg;
ea0760cf
JB
1240 u32 val;
1241 enum pipe panel_pipe = PIPE_A;
0de3b485 1242 bool locked = true;
ea0760cf 1243
bedd4dba
JN
1244 if (WARN_ON(HAS_DDI(dev)))
1245 return;
1246
1247 if (HAS_PCH_SPLIT(dev)) {
1248 u32 port_sel;
1249
ea0760cf 1250 pp_reg = PCH_PP_CONTROL;
bedd4dba
JN
1251 port_sel = I915_READ(PCH_PP_ON_DELAYS) & PANEL_PORT_SELECT_MASK;
1252
1253 if (port_sel == PANEL_PORT_SELECT_LVDS &&
1254 I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
1255 panel_pipe = PIPE_B;
1256 /* XXX: else fix for eDP */
666a4537 1257 } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
bedd4dba
JN
1258 /* presumably write lock depends on pipe, not port select */
1259 pp_reg = VLV_PIPE_PP_CONTROL(pipe);
1260 panel_pipe = pipe;
ea0760cf
JB
1261 } else {
1262 pp_reg = PP_CONTROL;
bedd4dba
JN
1263 if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
1264 panel_pipe = PIPE_B;
ea0760cf
JB
1265 }
1266
1267 val = I915_READ(pp_reg);
1268 if (!(val & PANEL_POWER_ON) ||
ec49ba2d 1269 ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
ea0760cf
JB
1270 locked = false;
1271
e2c719b7 1272 I915_STATE_WARN(panel_pipe == pipe && locked,
ea0760cf 1273 "panel assertion failure, pipe %c regs locked\n",
9db4a9c7 1274 pipe_name(pipe));
ea0760cf
JB
1275}
1276
93ce0ba6
JN
1277static void assert_cursor(struct drm_i915_private *dev_priv,
1278 enum pipe pipe, bool state)
1279{
1280 struct drm_device *dev = dev_priv->dev;
1281 bool cur_state;
1282
d9d82081 1283 if (IS_845G(dev) || IS_I865G(dev))
0b87c24e 1284 cur_state = I915_READ(CURCNTR(PIPE_A)) & CURSOR_ENABLE;
d9d82081 1285 else
5efb3e28 1286 cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
93ce0ba6 1287
e2c719b7 1288 I915_STATE_WARN(cur_state != state,
93ce0ba6 1289 "cursor on pipe %c assertion failure (expected %s, current %s)\n",
87ad3212 1290 pipe_name(pipe), onoff(state), onoff(cur_state));
93ce0ba6
JN
1291}
1292#define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
1293#define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
1294
b840d907
JB
1295void assert_pipe(struct drm_i915_private *dev_priv,
1296 enum pipe pipe, bool state)
b24e7179 1297{
63d7bbe9 1298 bool cur_state;
702e7a56
PZ
1299 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1300 pipe);
4feed0eb 1301 enum intel_display_power_domain power_domain;
b24e7179 1302
b6b5d049
VS
1303 /* if we need the pipe quirk it must be always on */
1304 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1305 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
8e636784
DV
1306 state = true;
1307
4feed0eb
ID
1308 power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
1309 if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
649636ef 1310 u32 val = I915_READ(PIPECONF(cpu_transcoder));
69310161 1311 cur_state = !!(val & PIPECONF_ENABLE);
4feed0eb
ID
1312
1313 intel_display_power_put(dev_priv, power_domain);
1314 } else {
1315 cur_state = false;
69310161
PZ
1316 }
1317
e2c719b7 1318 I915_STATE_WARN(cur_state != state,
63d7bbe9 1319 "pipe %c assertion failure (expected %s, current %s)\n",
87ad3212 1320 pipe_name(pipe), onoff(state), onoff(cur_state));
b24e7179
JB
1321}
1322
931872fc
CW
1323static void assert_plane(struct drm_i915_private *dev_priv,
1324 enum plane plane, bool state)
b24e7179 1325{
b24e7179 1326 u32 val;
931872fc 1327 bool cur_state;
b24e7179 1328
649636ef 1329 val = I915_READ(DSPCNTR(plane));
931872fc 1330 cur_state = !!(val & DISPLAY_PLANE_ENABLE);
e2c719b7 1331 I915_STATE_WARN(cur_state != state,
931872fc 1332 "plane %c assertion failure (expected %s, current %s)\n",
87ad3212 1333 plane_name(plane), onoff(state), onoff(cur_state));
b24e7179
JB
1334}
1335
931872fc
CW
1336#define assert_plane_enabled(d, p) assert_plane(d, p, true)
1337#define assert_plane_disabled(d, p) assert_plane(d, p, false)
1338
b24e7179
JB
1339static void assert_planes_disabled(struct drm_i915_private *dev_priv,
1340 enum pipe pipe)
1341{
653e1026 1342 struct drm_device *dev = dev_priv->dev;
649636ef 1343 int i;
b24e7179 1344
653e1026
VS
1345 /* Primary planes are fixed to pipes on gen4+ */
1346 if (INTEL_INFO(dev)->gen >= 4) {
649636ef 1347 u32 val = I915_READ(DSPCNTR(pipe));
e2c719b7 1348 I915_STATE_WARN(val & DISPLAY_PLANE_ENABLE,
28c05794
AJ
1349 "plane %c assertion failure, should be disabled but not\n",
1350 plane_name(pipe));
19ec1358 1351 return;
28c05794 1352 }
19ec1358 1353
b24e7179 1354 /* Need to check both planes against the pipe */
055e393f 1355 for_each_pipe(dev_priv, i) {
649636ef
VS
1356 u32 val = I915_READ(DSPCNTR(i));
1357 enum pipe cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
b24e7179 1358 DISPPLANE_SEL_PIPE_SHIFT;
e2c719b7 1359 I915_STATE_WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
9db4a9c7
JB
1360 "plane %c assertion failure, should be off on pipe %c but is still active\n",
1361 plane_name(i), pipe_name(pipe));
b24e7179
JB
1362 }
1363}
1364
19332d7a
JB
1365static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
1366 enum pipe pipe)
1367{
20674eef 1368 struct drm_device *dev = dev_priv->dev;
649636ef 1369 int sprite;
19332d7a 1370
7feb8b88 1371 if (INTEL_INFO(dev)->gen >= 9) {
3bdcfc0c 1372 for_each_sprite(dev_priv, pipe, sprite) {
649636ef 1373 u32 val = I915_READ(PLANE_CTL(pipe, sprite));
e2c719b7 1374 I915_STATE_WARN(val & PLANE_CTL_ENABLE,
7feb8b88
DL
1375 "plane %d assertion failure, should be off on pipe %c but is still active\n",
1376 sprite, pipe_name(pipe));
1377 }
666a4537 1378 } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
3bdcfc0c 1379 for_each_sprite(dev_priv, pipe, sprite) {
649636ef 1380 u32 val = I915_READ(SPCNTR(pipe, sprite));
e2c719b7 1381 I915_STATE_WARN(val & SP_ENABLE,
20674eef 1382 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
1fe47785 1383 sprite_name(pipe, sprite), pipe_name(pipe));
20674eef
VS
1384 }
1385 } else if (INTEL_INFO(dev)->gen >= 7) {
649636ef 1386 u32 val = I915_READ(SPRCTL(pipe));
e2c719b7 1387 I915_STATE_WARN(val & SPRITE_ENABLE,
06da8da2 1388 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
20674eef
VS
1389 plane_name(pipe), pipe_name(pipe));
1390 } else if (INTEL_INFO(dev)->gen >= 5) {
649636ef 1391 u32 val = I915_READ(DVSCNTR(pipe));
e2c719b7 1392 I915_STATE_WARN(val & DVS_ENABLE,
06da8da2 1393 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
20674eef 1394 plane_name(pipe), pipe_name(pipe));
19332d7a
JB
1395 }
1396}
1397
08c71e5e
VS
1398static void assert_vblank_disabled(struct drm_crtc *crtc)
1399{
e2c719b7 1400 if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
08c71e5e
VS
1401 drm_crtc_vblank_put(crtc);
1402}
1403
7abd4b35
ACO
1404void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
1405 enum pipe pipe)
92f2584a 1406{
92f2584a
JB
1407 u32 val;
1408 bool enabled;
1409
649636ef 1410 val = I915_READ(PCH_TRANSCONF(pipe));
92f2584a 1411 enabled = !!(val & TRANS_ENABLE);
e2c719b7 1412 I915_STATE_WARN(enabled,
9db4a9c7
JB
1413 "transcoder assertion failed, should be off on pipe %c but is still active\n",
1414 pipe_name(pipe));
92f2584a
JB
1415}
1416
4e634389
KP
1417static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
1418 enum pipe pipe, u32 port_sel, u32 val)
f0575e92
KP
1419{
1420 if ((val & DP_PORT_EN) == 0)
1421 return false;
1422
2d1fe073 1423 if (HAS_PCH_CPT(dev_priv)) {
f0f59a00 1424 u32 trans_dp_ctl = I915_READ(TRANS_DP_CTL(pipe));
f0575e92
KP
1425 if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
1426 return false;
2d1fe073 1427 } else if (IS_CHERRYVIEW(dev_priv)) {
44f37d1f
CML
1428 if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
1429 return false;
f0575e92
KP
1430 } else {
1431 if ((val & DP_PIPE_MASK) != (pipe << 30))
1432 return false;
1433 }
1434 return true;
1435}
1436
1519b995
KP
1437static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
1438 enum pipe pipe, u32 val)
1439{
dc0fa718 1440 if ((val & SDVO_ENABLE) == 0)
1519b995
KP
1441 return false;
1442
2d1fe073 1443 if (HAS_PCH_CPT(dev_priv)) {
dc0fa718 1444 if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
1519b995 1445 return false;
2d1fe073 1446 } else if (IS_CHERRYVIEW(dev_priv)) {
44f37d1f
CML
1447 if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
1448 return false;
1519b995 1449 } else {
dc0fa718 1450 if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
1519b995
KP
1451 return false;
1452 }
1453 return true;
1454}
1455
1456static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
1457 enum pipe pipe, u32 val)
1458{
1459 if ((val & LVDS_PORT_EN) == 0)
1460 return false;
1461
2d1fe073 1462 if (HAS_PCH_CPT(dev_priv)) {
1519b995
KP
1463 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1464 return false;
1465 } else {
1466 if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
1467 return false;
1468 }
1469 return true;
1470}
1471
1472static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
1473 enum pipe pipe, u32 val)
1474{
1475 if ((val & ADPA_DAC_ENABLE) == 0)
1476 return false;
2d1fe073 1477 if (HAS_PCH_CPT(dev_priv)) {
1519b995
KP
1478 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1479 return false;
1480 } else {
1481 if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
1482 return false;
1483 }
1484 return true;
1485}
1486
291906f1 1487static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
f0f59a00
VS
1488 enum pipe pipe, i915_reg_t reg,
1489 u32 port_sel)
291906f1 1490{
47a05eca 1491 u32 val = I915_READ(reg);
e2c719b7 1492 I915_STATE_WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
291906f1 1493 "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
f0f59a00 1494 i915_mmio_reg_offset(reg), pipe_name(pipe));
de9a35ab 1495
2d1fe073 1496 I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && (val & DP_PORT_EN) == 0
75c5da27 1497 && (val & DP_PIPEB_SELECT),
de9a35ab 1498 "IBX PCH dp port still using transcoder B\n");
291906f1
JB
1499}
1500
1501static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
f0f59a00 1502 enum pipe pipe, i915_reg_t reg)
291906f1 1503{
47a05eca 1504 u32 val = I915_READ(reg);
e2c719b7 1505 I915_STATE_WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
23c99e77 1506 "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
f0f59a00 1507 i915_mmio_reg_offset(reg), pipe_name(pipe));
de9a35ab 1508
2d1fe073 1509 I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && (val & SDVO_ENABLE) == 0
75c5da27 1510 && (val & SDVO_PIPE_B_SELECT),
de9a35ab 1511 "IBX PCH hdmi port still using transcoder B\n");
291906f1
JB
1512}
1513
1514static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
1515 enum pipe pipe)
1516{
291906f1 1517 u32 val;
291906f1 1518
f0575e92
KP
1519 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
1520 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
1521 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
291906f1 1522
649636ef 1523 val = I915_READ(PCH_ADPA);
e2c719b7 1524 I915_STATE_WARN(adpa_pipe_enabled(dev_priv, pipe, val),
291906f1 1525 "PCH VGA enabled on transcoder %c, should be disabled\n",
9db4a9c7 1526 pipe_name(pipe));
291906f1 1527
649636ef 1528 val = I915_READ(PCH_LVDS);
e2c719b7 1529 I915_STATE_WARN(lvds_pipe_enabled(dev_priv, pipe, val),
291906f1 1530 "PCH LVDS enabled on transcoder %c, should be disabled\n",
9db4a9c7 1531 pipe_name(pipe));
291906f1 1532
e2debe91
PZ
1533 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
1534 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
1535 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
291906f1
JB
1536}
1537
cd2d34d9
VS
1538static void _vlv_enable_pll(struct intel_crtc *crtc,
1539 const struct intel_crtc_state *pipe_config)
1540{
1541 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1542 enum pipe pipe = crtc->pipe;
1543
1544 I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
1545 POSTING_READ(DPLL(pipe));
1546 udelay(150);
1547
1548 if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
1549 DRM_ERROR("DPLL %d failed to lock\n", pipe);
1550}
1551
d288f65f 1552static void vlv_enable_pll(struct intel_crtc *crtc,
5cec258b 1553 const struct intel_crtc_state *pipe_config)
87442f73 1554{
cd2d34d9 1555 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
8bd3f301 1556 enum pipe pipe = crtc->pipe;
87442f73 1557
8bd3f301 1558 assert_pipe_disabled(dev_priv, pipe);
87442f73 1559
87442f73 1560 /* PLL is protected by panel, make sure we can write it */
7d1a83cb 1561 assert_panel_unlocked(dev_priv, pipe);
87442f73 1562
cd2d34d9
VS
1563 if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
1564 _vlv_enable_pll(crtc, pipe_config);
426115cf 1565
8bd3f301
VS
1566 I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
1567 POSTING_READ(DPLL_MD(pipe));
87442f73
DV
1568}
1569
cd2d34d9
VS
1570
1571static void _chv_enable_pll(struct intel_crtc *crtc,
1572 const struct intel_crtc_state *pipe_config)
9d556c99 1573{
cd2d34d9 1574 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
8bd3f301 1575 enum pipe pipe = crtc->pipe;
9d556c99 1576 enum dpio_channel port = vlv_pipe_to_channel(pipe);
9d556c99
CML
1577 u32 tmp;
1578
a580516d 1579 mutex_lock(&dev_priv->sb_lock);
9d556c99
CML
1580
1581 /* Enable back the 10bit clock to display controller */
1582 tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1583 tmp |= DPIO_DCLKP_EN;
1584 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
1585
54433e91
VS
1586 mutex_unlock(&dev_priv->sb_lock);
1587
9d556c99
CML
1588 /*
1589 * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
1590 */
1591 udelay(1);
1592
1593 /* Enable PLL */
d288f65f 1594 I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
9d556c99
CML
1595
1596 /* Check PLL is locked */
a11b0703 1597 if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
9d556c99 1598 DRM_ERROR("PLL %d failed to lock\n", pipe);
cd2d34d9
VS
1599}
1600
1601static void chv_enable_pll(struct intel_crtc *crtc,
1602 const struct intel_crtc_state *pipe_config)
1603{
1604 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1605 enum pipe pipe = crtc->pipe;
1606
1607 assert_pipe_disabled(dev_priv, pipe);
1608
1609 /* PLL is protected by panel, make sure we can write it */
1610 assert_panel_unlocked(dev_priv, pipe);
1611
1612 if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
1613 _chv_enable_pll(crtc, pipe_config);
9d556c99 1614
c231775c
VS
1615 if (pipe != PIPE_A) {
1616 /*
1617 * WaPixelRepeatModeFixForC0:chv
1618 *
1619 * DPLLCMD is AWOL. Use chicken bits to propagate
1620 * the value from DPLLBMD to either pipe B or C.
1621 */
1622 I915_WRITE(CBR4_VLV, pipe == PIPE_B ? CBR_DPLLBMD_PIPE_B : CBR_DPLLBMD_PIPE_C);
1623 I915_WRITE(DPLL_MD(PIPE_B), pipe_config->dpll_hw_state.dpll_md);
1624 I915_WRITE(CBR4_VLV, 0);
1625 dev_priv->chv_dpll_md[pipe] = pipe_config->dpll_hw_state.dpll_md;
1626
1627 /*
1628 * DPLLB VGA mode also seems to cause problems.
1629 * We should always have it disabled.
1630 */
1631 WARN_ON((I915_READ(DPLL(PIPE_B)) & DPLL_VGA_MODE_DIS) == 0);
1632 } else {
1633 I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
1634 POSTING_READ(DPLL_MD(pipe));
1635 }
9d556c99
CML
1636}
1637
1c4e0274
VS
1638static int intel_num_dvo_pipes(struct drm_device *dev)
1639{
1640 struct intel_crtc *crtc;
1641 int count = 0;
1642
1643 for_each_intel_crtc(dev, crtc)
3538b9df 1644 count += crtc->base.state->active &&
409ee761 1645 intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO);
1c4e0274
VS
1646
1647 return count;
1648}
1649
66e3d5c0 1650static void i9xx_enable_pll(struct intel_crtc *crtc)
63d7bbe9 1651{
66e3d5c0
DV
1652 struct drm_device *dev = crtc->base.dev;
1653 struct drm_i915_private *dev_priv = dev->dev_private;
f0f59a00 1654 i915_reg_t reg = DPLL(crtc->pipe);
6e3c9717 1655 u32 dpll = crtc->config->dpll_hw_state.dpll;
63d7bbe9 1656
66e3d5c0 1657 assert_pipe_disabled(dev_priv, crtc->pipe);
58c6eaa2 1658
63d7bbe9 1659 /* PLL is protected by panel, make sure we can write it */
66e3d5c0
DV
1660 if (IS_MOBILE(dev) && !IS_I830(dev))
1661 assert_panel_unlocked(dev_priv, crtc->pipe);
63d7bbe9 1662
1c4e0274
VS
1663 /* Enable DVO 2x clock on both PLLs if necessary */
1664 if (IS_I830(dev) && intel_num_dvo_pipes(dev) > 0) {
1665 /*
1666 * It appears to be important that we don't enable this
1667 * for the current pipe before otherwise configuring the
1668 * PLL. No idea how this should be handled if multiple
1669 * DVO outputs are enabled simultaneosly.
1670 */
1671 dpll |= DPLL_DVO_2X_MODE;
1672 I915_WRITE(DPLL(!crtc->pipe),
1673 I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
1674 }
66e3d5c0 1675
c2b63374
VS
1676 /*
1677 * Apparently we need to have VGA mode enabled prior to changing
1678 * the P1/P2 dividers. Otherwise the DPLL will keep using the old
1679 * dividers, even though the register value does change.
1680 */
1681 I915_WRITE(reg, 0);
1682
8e7a65aa
VS
1683 I915_WRITE(reg, dpll);
1684
66e3d5c0
DV
1685 /* Wait for the clocks to stabilize. */
1686 POSTING_READ(reg);
1687 udelay(150);
1688
1689 if (INTEL_INFO(dev)->gen >= 4) {
1690 I915_WRITE(DPLL_MD(crtc->pipe),
6e3c9717 1691 crtc->config->dpll_hw_state.dpll_md);
66e3d5c0
DV
1692 } else {
1693 /* The pixel multiplier can only be updated once the
1694 * DPLL is enabled and the clocks are stable.
1695 *
1696 * So write it again.
1697 */
1698 I915_WRITE(reg, dpll);
1699 }
63d7bbe9
JB
1700
1701 /* We do this three times for luck */
66e3d5c0 1702 I915_WRITE(reg, dpll);
63d7bbe9
JB
1703 POSTING_READ(reg);
1704 udelay(150); /* wait for warmup */
66e3d5c0 1705 I915_WRITE(reg, dpll);
63d7bbe9
JB
1706 POSTING_READ(reg);
1707 udelay(150); /* wait for warmup */
66e3d5c0 1708 I915_WRITE(reg, dpll);
63d7bbe9
JB
1709 POSTING_READ(reg);
1710 udelay(150); /* wait for warmup */
1711}
1712
1713/**
50b44a44 1714 * i9xx_disable_pll - disable a PLL
63d7bbe9
JB
1715 * @dev_priv: i915 private structure
1716 * @pipe: pipe PLL to disable
1717 *
1718 * Disable the PLL for @pipe, making sure the pipe is off first.
1719 *
1720 * Note! This is for pre-ILK only.
1721 */
1c4e0274 1722static void i9xx_disable_pll(struct intel_crtc *crtc)
63d7bbe9 1723{
1c4e0274
VS
1724 struct drm_device *dev = crtc->base.dev;
1725 struct drm_i915_private *dev_priv = dev->dev_private;
1726 enum pipe pipe = crtc->pipe;
1727
1728 /* Disable DVO 2x clock on both PLLs if necessary */
1729 if (IS_I830(dev) &&
409ee761 1730 intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO) &&
3538b9df 1731 !intel_num_dvo_pipes(dev)) {
1c4e0274
VS
1732 I915_WRITE(DPLL(PIPE_B),
1733 I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
1734 I915_WRITE(DPLL(PIPE_A),
1735 I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
1736 }
1737
b6b5d049
VS
1738 /* Don't disable pipe or pipe PLLs if needed */
1739 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1740 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
63d7bbe9
JB
1741 return;
1742
1743 /* Make sure the pipe isn't still relying on us */
1744 assert_pipe_disabled(dev_priv, pipe);
1745
b8afb911 1746 I915_WRITE(DPLL(pipe), DPLL_VGA_MODE_DIS);
50b44a44 1747 POSTING_READ(DPLL(pipe));
63d7bbe9
JB
1748}
1749
f6071166
JB
1750static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1751{
b8afb911 1752 u32 val;
f6071166
JB
1753
1754 /* Make sure the pipe isn't still relying on us */
1755 assert_pipe_disabled(dev_priv, pipe);
1756
03ed5cbf
VS
1757 val = DPLL_INTEGRATED_REF_CLK_VLV |
1758 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
1759 if (pipe != PIPE_A)
1760 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
1761
f6071166
JB
1762 I915_WRITE(DPLL(pipe), val);
1763 POSTING_READ(DPLL(pipe));
076ed3b2
CML
1764}
1765
1766static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1767{
d752048d 1768 enum dpio_channel port = vlv_pipe_to_channel(pipe);
076ed3b2
CML
1769 u32 val;
1770
a11b0703
VS
1771 /* Make sure the pipe isn't still relying on us */
1772 assert_pipe_disabled(dev_priv, pipe);
076ed3b2 1773
60bfe44f
VS
1774 val = DPLL_SSC_REF_CLK_CHV |
1775 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
a11b0703
VS
1776 if (pipe != PIPE_A)
1777 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
03ed5cbf 1778
a11b0703
VS
1779 I915_WRITE(DPLL(pipe), val);
1780 POSTING_READ(DPLL(pipe));
d752048d 1781
a580516d 1782 mutex_lock(&dev_priv->sb_lock);
d752048d
VS
1783
1784 /* Disable 10bit clock to display controller */
1785 val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1786 val &= ~DPIO_DCLKP_EN;
1787 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
1788
a580516d 1789 mutex_unlock(&dev_priv->sb_lock);
f6071166
JB
1790}
1791
e4607fcf 1792void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
9b6de0a1
VS
1793 struct intel_digital_port *dport,
1794 unsigned int expected_mask)
89b667f8
JB
1795{
1796 u32 port_mask;
f0f59a00 1797 i915_reg_t dpll_reg;
89b667f8 1798
e4607fcf
CML
1799 switch (dport->port) {
1800 case PORT_B:
89b667f8 1801 port_mask = DPLL_PORTB_READY_MASK;
00fc31b7 1802 dpll_reg = DPLL(0);
e4607fcf
CML
1803 break;
1804 case PORT_C:
89b667f8 1805 port_mask = DPLL_PORTC_READY_MASK;
00fc31b7 1806 dpll_reg = DPLL(0);
9b6de0a1 1807 expected_mask <<= 4;
00fc31b7
CML
1808 break;
1809 case PORT_D:
1810 port_mask = DPLL_PORTD_READY_MASK;
1811 dpll_reg = DPIO_PHY_STATUS;
e4607fcf
CML
1812 break;
1813 default:
1814 BUG();
1815 }
89b667f8 1816
9b6de0a1
VS
1817 if (wait_for((I915_READ(dpll_reg) & port_mask) == expected_mask, 1000))
1818 WARN(1, "timed out waiting for port %c ready: got 0x%x, expected 0x%x\n",
1819 port_name(dport->port), I915_READ(dpll_reg) & port_mask, expected_mask);
89b667f8
JB
1820}
1821
b8a4f404
PZ
1822static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1823 enum pipe pipe)
040484af 1824{
23670b32 1825 struct drm_device *dev = dev_priv->dev;
7c26e5c6 1826 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
e2b78267 1827 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
f0f59a00
VS
1828 i915_reg_t reg;
1829 uint32_t val, pipeconf_val;
040484af 1830
040484af 1831 /* Make sure PCH DPLL is enabled */
8106ddbd 1832 assert_shared_dpll_enabled(dev_priv, intel_crtc->config->shared_dpll);
040484af
JB
1833
1834 /* FDI must be feeding us bits for PCH ports */
1835 assert_fdi_tx_enabled(dev_priv, pipe);
1836 assert_fdi_rx_enabled(dev_priv, pipe);
1837
23670b32
DV
1838 if (HAS_PCH_CPT(dev)) {
1839 /* Workaround: Set the timing override bit before enabling the
1840 * pch transcoder. */
1841 reg = TRANS_CHICKEN2(pipe);
1842 val = I915_READ(reg);
1843 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
1844 I915_WRITE(reg, val);
59c859d6 1845 }
23670b32 1846
ab9412ba 1847 reg = PCH_TRANSCONF(pipe);
040484af 1848 val = I915_READ(reg);
5f7f726d 1849 pipeconf_val = I915_READ(PIPECONF(pipe));
e9bcff5c 1850
2d1fe073 1851 if (HAS_PCH_IBX(dev_priv)) {
e9bcff5c 1852 /*
c5de7c6f
VS
1853 * Make the BPC in transcoder be consistent with
1854 * that in pipeconf reg. For HDMI we must use 8bpc
1855 * here for both 8bpc and 12bpc.
e9bcff5c 1856 */
dfd07d72 1857 val &= ~PIPECONF_BPC_MASK;
c5de7c6f
VS
1858 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_HDMI))
1859 val |= PIPECONF_8BPC;
1860 else
1861 val |= pipeconf_val & PIPECONF_BPC_MASK;
e9bcff5c 1862 }
5f7f726d
PZ
1863
1864 val &= ~TRANS_INTERLACE_MASK;
1865 if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
2d1fe073 1866 if (HAS_PCH_IBX(dev_priv) &&
409ee761 1867 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
7c26e5c6
PZ
1868 val |= TRANS_LEGACY_INTERLACED_ILK;
1869 else
1870 val |= TRANS_INTERLACED;
5f7f726d
PZ
1871 else
1872 val |= TRANS_PROGRESSIVE;
1873
040484af
JB
1874 I915_WRITE(reg, val | TRANS_ENABLE);
1875 if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
4bb6f1f3 1876 DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
040484af
JB
1877}
1878
8fb033d7 1879static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
937bb610 1880 enum transcoder cpu_transcoder)
040484af 1881{
8fb033d7 1882 u32 val, pipeconf_val;
8fb033d7 1883
8fb033d7 1884 /* FDI must be feeding us bits for PCH ports */
1a240d4d 1885 assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
937bb610 1886 assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
8fb033d7 1887
223a6fdf 1888 /* Workaround: set timing override bit. */
36c0d0cf 1889 val = I915_READ(TRANS_CHICKEN2(PIPE_A));
23670b32 1890 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
36c0d0cf 1891 I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
223a6fdf 1892
25f3ef11 1893 val = TRANS_ENABLE;
937bb610 1894 pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
8fb033d7 1895
9a76b1c6
PZ
1896 if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
1897 PIPECONF_INTERLACED_ILK)
a35f2679 1898 val |= TRANS_INTERLACED;
8fb033d7
PZ
1899 else
1900 val |= TRANS_PROGRESSIVE;
1901
ab9412ba
DV
1902 I915_WRITE(LPT_TRANSCONF, val);
1903 if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
937bb610 1904 DRM_ERROR("Failed to enable PCH transcoder\n");
8fb033d7
PZ
1905}
1906
b8a4f404
PZ
1907static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
1908 enum pipe pipe)
040484af 1909{
23670b32 1910 struct drm_device *dev = dev_priv->dev;
f0f59a00
VS
1911 i915_reg_t reg;
1912 uint32_t val;
040484af
JB
1913
1914 /* FDI relies on the transcoder */
1915 assert_fdi_tx_disabled(dev_priv, pipe);
1916 assert_fdi_rx_disabled(dev_priv, pipe);
1917
291906f1
JB
1918 /* Ports must be off as well */
1919 assert_pch_ports_disabled(dev_priv, pipe);
1920
ab9412ba 1921 reg = PCH_TRANSCONF(pipe);
040484af
JB
1922 val = I915_READ(reg);
1923 val &= ~TRANS_ENABLE;
1924 I915_WRITE(reg, val);
1925 /* wait for PCH transcoder off, transcoder state */
1926 if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
4bb6f1f3 1927 DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
23670b32 1928
c465613b 1929 if (HAS_PCH_CPT(dev)) {
23670b32
DV
1930 /* Workaround: Clear the timing override chicken bit again. */
1931 reg = TRANS_CHICKEN2(pipe);
1932 val = I915_READ(reg);
1933 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
1934 I915_WRITE(reg, val);
1935 }
040484af
JB
1936}
1937
ab4d966c 1938static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
8fb033d7 1939{
8fb033d7
PZ
1940 u32 val;
1941
ab9412ba 1942 val = I915_READ(LPT_TRANSCONF);
8fb033d7 1943 val &= ~TRANS_ENABLE;
ab9412ba 1944 I915_WRITE(LPT_TRANSCONF, val);
8fb033d7 1945 /* wait for PCH transcoder off, transcoder state */
ab9412ba 1946 if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
8a52fd9f 1947 DRM_ERROR("Failed to disable PCH transcoder\n");
223a6fdf
PZ
1948
1949 /* Workaround: clear timing override bit. */
36c0d0cf 1950 val = I915_READ(TRANS_CHICKEN2(PIPE_A));
23670b32 1951 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
36c0d0cf 1952 I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
040484af
JB
1953}
1954
b24e7179 1955/**
309cfea8 1956 * intel_enable_pipe - enable a pipe, asserting requirements
0372264a 1957 * @crtc: crtc responsible for the pipe
b24e7179 1958 *
0372264a 1959 * Enable @crtc's pipe, making sure that various hardware specific requirements
b24e7179 1960 * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
b24e7179 1961 */
e1fdc473 1962static void intel_enable_pipe(struct intel_crtc *crtc)
b24e7179 1963{
0372264a
PZ
1964 struct drm_device *dev = crtc->base.dev;
1965 struct drm_i915_private *dev_priv = dev->dev_private;
1966 enum pipe pipe = crtc->pipe;
1a70a728 1967 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
1a240d4d 1968 enum pipe pch_transcoder;
f0f59a00 1969 i915_reg_t reg;
b24e7179
JB
1970 u32 val;
1971
9e2ee2dd
VS
1972 DRM_DEBUG_KMS("enabling pipe %c\n", pipe_name(pipe));
1973
58c6eaa2 1974 assert_planes_disabled(dev_priv, pipe);
93ce0ba6 1975 assert_cursor_disabled(dev_priv, pipe);
58c6eaa2
DV
1976 assert_sprites_disabled(dev_priv, pipe);
1977
2d1fe073 1978 if (HAS_PCH_LPT(dev_priv))
cc391bbb
PZ
1979 pch_transcoder = TRANSCODER_A;
1980 else
1981 pch_transcoder = pipe;
1982
b24e7179
JB
1983 /*
1984 * A pipe without a PLL won't actually be able to drive bits from
1985 * a plane. On ILK+ the pipe PLLs are integrated, so we don't
1986 * need the check.
1987 */
2d1fe073 1988 if (HAS_GMCH_DISPLAY(dev_priv))
a65347ba 1989 if (crtc->config->has_dsi_encoder)
23538ef1
JN
1990 assert_dsi_pll_enabled(dev_priv);
1991 else
1992 assert_pll_enabled(dev_priv, pipe);
040484af 1993 else {
6e3c9717 1994 if (crtc->config->has_pch_encoder) {
040484af 1995 /* if driving the PCH, we need FDI enabled */
cc391bbb 1996 assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
1a240d4d
DV
1997 assert_fdi_tx_pll_enabled(dev_priv,
1998 (enum pipe) cpu_transcoder);
040484af
JB
1999 }
2000 /* FIXME: assert CPU port conditions for SNB+ */
2001 }
b24e7179 2002
702e7a56 2003 reg = PIPECONF(cpu_transcoder);
b24e7179 2004 val = I915_READ(reg);
7ad25d48 2005 if (val & PIPECONF_ENABLE) {
b6b5d049
VS
2006 WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
2007 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
00d70b15 2008 return;
7ad25d48 2009 }
00d70b15
CW
2010
2011 I915_WRITE(reg, val | PIPECONF_ENABLE);
851855d8 2012 POSTING_READ(reg);
b7792d8b
VS
2013
2014 /*
2015 * Until the pipe starts DSL will read as 0, which would cause
2016 * an apparent vblank timestamp jump, which messes up also the
2017 * frame count when it's derived from the timestamps. So let's
2018 * wait for the pipe to start properly before we call
2019 * drm_crtc_vblank_on()
2020 */
2021 if (dev->max_vblank_count == 0 &&
2022 wait_for(intel_get_crtc_scanline(crtc) != crtc->scanline_offset, 50))
2023 DRM_ERROR("pipe %c didn't start\n", pipe_name(pipe));
b24e7179
JB
2024}
2025
2026/**
309cfea8 2027 * intel_disable_pipe - disable a pipe, asserting requirements
575f7ab7 2028 * @crtc: crtc whose pipes is to be disabled
b24e7179 2029 *
575f7ab7
VS
2030 * Disable the pipe of @crtc, making sure that various hardware
2031 * specific requirements are met, if applicable, e.g. plane
2032 * disabled, panel fitter off, etc.
b24e7179
JB
2033 *
2034 * Will wait until the pipe has shut down before returning.
2035 */
575f7ab7 2036static void intel_disable_pipe(struct intel_crtc *crtc)
b24e7179 2037{
575f7ab7 2038 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
6e3c9717 2039 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
575f7ab7 2040 enum pipe pipe = crtc->pipe;
f0f59a00 2041 i915_reg_t reg;
b24e7179
JB
2042 u32 val;
2043
9e2ee2dd
VS
2044 DRM_DEBUG_KMS("disabling pipe %c\n", pipe_name(pipe));
2045
b24e7179
JB
2046 /*
2047 * Make sure planes won't keep trying to pump pixels to us,
2048 * or we might hang the display.
2049 */
2050 assert_planes_disabled(dev_priv, pipe);
93ce0ba6 2051 assert_cursor_disabled(dev_priv, pipe);
19332d7a 2052 assert_sprites_disabled(dev_priv, pipe);
b24e7179 2053
702e7a56 2054 reg = PIPECONF(cpu_transcoder);
b24e7179 2055 val = I915_READ(reg);
00d70b15
CW
2056 if ((val & PIPECONF_ENABLE) == 0)
2057 return;
2058
67adc644
VS
2059 /*
2060 * Double wide has implications for planes
2061 * so best keep it disabled when not needed.
2062 */
6e3c9717 2063 if (crtc->config->double_wide)
67adc644
VS
2064 val &= ~PIPECONF_DOUBLE_WIDE;
2065
2066 /* Don't disable pipe or pipe PLLs if needed */
b6b5d049
VS
2067 if (!(pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) &&
2068 !(pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
67adc644
VS
2069 val &= ~PIPECONF_ENABLE;
2070
2071 I915_WRITE(reg, val);
2072 if ((val & PIPECONF_ENABLE) == 0)
2073 intel_wait_for_pipe_off(crtc);
b24e7179
JB
2074}
2075
693db184
CW
2076static bool need_vtd_wa(struct drm_device *dev)
2077{
2078#ifdef CONFIG_INTEL_IOMMU
2079 if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
2080 return true;
2081#endif
2082 return false;
2083}
2084
832be82f
VS
2085static unsigned int intel_tile_size(const struct drm_i915_private *dev_priv)
2086{
2087 return IS_GEN2(dev_priv) ? 2048 : 4096;
2088}
2089
27ba3910
VS
2090static unsigned int intel_tile_width_bytes(const struct drm_i915_private *dev_priv,
2091 uint64_t fb_modifier, unsigned int cpp)
7b49f948
VS
2092{
2093 switch (fb_modifier) {
2094 case DRM_FORMAT_MOD_NONE:
2095 return cpp;
2096 case I915_FORMAT_MOD_X_TILED:
2097 if (IS_GEN2(dev_priv))
2098 return 128;
2099 else
2100 return 512;
2101 case I915_FORMAT_MOD_Y_TILED:
2102 if (IS_GEN2(dev_priv) || HAS_128_BYTE_Y_TILING(dev_priv))
2103 return 128;
2104 else
2105 return 512;
2106 case I915_FORMAT_MOD_Yf_TILED:
2107 switch (cpp) {
2108 case 1:
2109 return 64;
2110 case 2:
2111 case 4:
2112 return 128;
2113 case 8:
2114 case 16:
2115 return 256;
2116 default:
2117 MISSING_CASE(cpp);
2118 return cpp;
2119 }
2120 break;
2121 default:
2122 MISSING_CASE(fb_modifier);
2123 return cpp;
2124 }
2125}
2126
832be82f
VS
2127unsigned int intel_tile_height(const struct drm_i915_private *dev_priv,
2128 uint64_t fb_modifier, unsigned int cpp)
a57ce0b2 2129{
832be82f
VS
2130 if (fb_modifier == DRM_FORMAT_MOD_NONE)
2131 return 1;
2132 else
2133 return intel_tile_size(dev_priv) /
27ba3910 2134 intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
6761dd31
TU
2135}
2136
8d0deca8
VS
2137/* Return the tile dimensions in pixel units */
2138static void intel_tile_dims(const struct drm_i915_private *dev_priv,
2139 unsigned int *tile_width,
2140 unsigned int *tile_height,
2141 uint64_t fb_modifier,
2142 unsigned int cpp)
2143{
2144 unsigned int tile_width_bytes =
2145 intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
2146
2147 *tile_width = tile_width_bytes / cpp;
2148 *tile_height = intel_tile_size(dev_priv) / tile_width_bytes;
2149}
2150
6761dd31
TU
2151unsigned int
2152intel_fb_align_height(struct drm_device *dev, unsigned int height,
832be82f 2153 uint32_t pixel_format, uint64_t fb_modifier)
6761dd31 2154{
832be82f
VS
2155 unsigned int cpp = drm_format_plane_cpp(pixel_format, 0);
2156 unsigned int tile_height = intel_tile_height(to_i915(dev), fb_modifier, cpp);
2157
2158 return ALIGN(height, tile_height);
a57ce0b2
JB
2159}
2160
1663b9d6
VS
2161unsigned int intel_rotation_info_size(const struct intel_rotation_info *rot_info)
2162{
2163 unsigned int size = 0;
2164 int i;
2165
2166 for (i = 0 ; i < ARRAY_SIZE(rot_info->plane); i++)
2167 size += rot_info->plane[i].width * rot_info->plane[i].height;
2168
2169 return size;
2170}
2171
75c82a53 2172static void
3465c580
VS
2173intel_fill_fb_ggtt_view(struct i915_ggtt_view *view,
2174 const struct drm_framebuffer *fb,
2175 unsigned int rotation)
f64b98cd 2176{
2d7a215f
VS
2177 if (intel_rotation_90_or_270(rotation)) {
2178 *view = i915_ggtt_view_rotated;
2179 view->params.rotated = to_intel_framebuffer(fb)->rot_info;
2180 } else {
2181 *view = i915_ggtt_view_normal;
2182 }
2183}
50470bb0 2184
2d7a215f
VS
2185static void
2186intel_fill_fb_info(struct drm_i915_private *dev_priv,
2187 struct drm_framebuffer *fb)
2188{
2189 struct intel_rotation_info *info = &to_intel_framebuffer(fb)->rot_info;
2190 unsigned int tile_size, tile_width, tile_height, cpp;
50470bb0 2191
d9b3288e
VS
2192 tile_size = intel_tile_size(dev_priv);
2193
2194 cpp = drm_format_plane_cpp(fb->pixel_format, 0);
8d0deca8
VS
2195 intel_tile_dims(dev_priv, &tile_width, &tile_height,
2196 fb->modifier[0], cpp);
d9b3288e 2197
1663b9d6
VS
2198 info->plane[0].width = DIV_ROUND_UP(fb->pitches[0], tile_width * cpp);
2199 info->plane[0].height = DIV_ROUND_UP(fb->height, tile_height);
84fe03f7 2200
89e3e142 2201 if (info->pixel_format == DRM_FORMAT_NV12) {
832be82f 2202 cpp = drm_format_plane_cpp(fb->pixel_format, 1);
8d0deca8
VS
2203 intel_tile_dims(dev_priv, &tile_width, &tile_height,
2204 fb->modifier[1], cpp);
d9b3288e 2205
2d7a215f 2206 info->uv_offset = fb->offsets[1];
1663b9d6
VS
2207 info->plane[1].width = DIV_ROUND_UP(fb->pitches[1], tile_width * cpp);
2208 info->plane[1].height = DIV_ROUND_UP(fb->height / 2, tile_height);
89e3e142 2209 }
f64b98cd
TU
2210}
2211
603525d7 2212static unsigned int intel_linear_alignment(const struct drm_i915_private *dev_priv)
4e9a86b6
VS
2213{
2214 if (INTEL_INFO(dev_priv)->gen >= 9)
2215 return 256 * 1024;
985b8bb4 2216 else if (IS_BROADWATER(dev_priv) || IS_CRESTLINE(dev_priv) ||
666a4537 2217 IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
4e9a86b6
VS
2218 return 128 * 1024;
2219 else if (INTEL_INFO(dev_priv)->gen >= 4)
2220 return 4 * 1024;
2221 else
44c5905e 2222 return 0;
4e9a86b6
VS
2223}
2224
603525d7
VS
2225static unsigned int intel_surf_alignment(const struct drm_i915_private *dev_priv,
2226 uint64_t fb_modifier)
2227{
2228 switch (fb_modifier) {
2229 case DRM_FORMAT_MOD_NONE:
2230 return intel_linear_alignment(dev_priv);
2231 case I915_FORMAT_MOD_X_TILED:
2232 if (INTEL_INFO(dev_priv)->gen >= 9)
2233 return 256 * 1024;
2234 return 0;
2235 case I915_FORMAT_MOD_Y_TILED:
2236 case I915_FORMAT_MOD_Yf_TILED:
2237 return 1 * 1024 * 1024;
2238 default:
2239 MISSING_CASE(fb_modifier);
2240 return 0;
2241 }
2242}
2243
127bd2ac 2244int
3465c580
VS
2245intel_pin_and_fence_fb_obj(struct drm_framebuffer *fb,
2246 unsigned int rotation)
6b95a207 2247{
850c4cdc 2248 struct drm_device *dev = fb->dev;
ce453d81 2249 struct drm_i915_private *dev_priv = dev->dev_private;
850c4cdc 2250 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
f64b98cd 2251 struct i915_ggtt_view view;
6b95a207
KH
2252 u32 alignment;
2253 int ret;
2254
ebcdd39e
MR
2255 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
2256
603525d7 2257 alignment = intel_surf_alignment(dev_priv, fb->modifier[0]);
6b95a207 2258
3465c580 2259 intel_fill_fb_ggtt_view(&view, fb, rotation);
f64b98cd 2260
693db184
CW
2261 /* Note that the w/a also requires 64 PTE of padding following the
2262 * bo. We currently fill all unused PTE with the shadow page and so
2263 * we should always have valid PTE following the scanout preventing
2264 * the VT-d warning.
2265 */
2266 if (need_vtd_wa(dev) && alignment < 256 * 1024)
2267 alignment = 256 * 1024;
2268
d6dd6843
PZ
2269 /*
2270 * Global gtt pte registers are special registers which actually forward
2271 * writes to a chunk of system memory. Which means that there is no risk
2272 * that the register values disappear as soon as we call
2273 * intel_runtime_pm_put(), so it is correct to wrap only the
2274 * pin/unpin/fence and not more.
2275 */
2276 intel_runtime_pm_get(dev_priv);
2277
7580d774
ML
2278 ret = i915_gem_object_pin_to_display_plane(obj, alignment,
2279 &view);
48b956c5 2280 if (ret)
b26a6b35 2281 goto err_pm;
6b95a207
KH
2282
2283 /* Install a fence for tiled scan-out. Pre-i965 always needs a
2284 * fence, whereas 965+ only requires a fence if using
2285 * framebuffer compression. For simplicity, we always install
2286 * a fence as the cost is not that onerous.
2287 */
9807216f
VK
2288 if (view.type == I915_GGTT_VIEW_NORMAL) {
2289 ret = i915_gem_object_get_fence(obj);
2290 if (ret == -EDEADLK) {
2291 /*
2292 * -EDEADLK means there are no free fences
2293 * no pending flips.
2294 *
2295 * This is propagated to atomic, but it uses
2296 * -EDEADLK to force a locking recovery, so
2297 * change the returned error to -EBUSY.
2298 */
2299 ret = -EBUSY;
2300 goto err_unpin;
2301 } else if (ret)
2302 goto err_unpin;
1690e1eb 2303
9807216f
VK
2304 i915_gem_object_pin_fence(obj);
2305 }
6b95a207 2306
d6dd6843 2307 intel_runtime_pm_put(dev_priv);
6b95a207 2308 return 0;
48b956c5
CW
2309
2310err_unpin:
f64b98cd 2311 i915_gem_object_unpin_from_display_plane(obj, &view);
b26a6b35 2312err_pm:
d6dd6843 2313 intel_runtime_pm_put(dev_priv);
48b956c5 2314 return ret;
6b95a207
KH
2315}
2316
fb4b8ce1 2317void intel_unpin_fb_obj(struct drm_framebuffer *fb, unsigned int rotation)
1690e1eb 2318{
82bc3b2d 2319 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
f64b98cd 2320 struct i915_ggtt_view view;
82bc3b2d 2321
ebcdd39e
MR
2322 WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
2323
3465c580 2324 intel_fill_fb_ggtt_view(&view, fb, rotation);
f64b98cd 2325
9807216f
VK
2326 if (view.type == I915_GGTT_VIEW_NORMAL)
2327 i915_gem_object_unpin_fence(obj);
2328
f64b98cd 2329 i915_gem_object_unpin_from_display_plane(obj, &view);
1690e1eb
CW
2330}
2331
29cf9491
VS
2332/*
2333 * Adjust the tile offset by moving the difference into
2334 * the x/y offsets.
2335 *
2336 * Input tile dimensions and pitch must already be
2337 * rotated to match x and y, and in pixel units.
2338 */
2339static u32 intel_adjust_tile_offset(int *x, int *y,
2340 unsigned int tile_width,
2341 unsigned int tile_height,
2342 unsigned int tile_size,
2343 unsigned int pitch_tiles,
2344 u32 old_offset,
2345 u32 new_offset)
2346{
2347 unsigned int tiles;
2348
2349 WARN_ON(old_offset & (tile_size - 1));
2350 WARN_ON(new_offset & (tile_size - 1));
2351 WARN_ON(new_offset > old_offset);
2352
2353 tiles = (old_offset - new_offset) / tile_size;
2354
2355 *y += tiles / pitch_tiles * tile_height;
2356 *x += tiles % pitch_tiles * tile_width;
2357
2358 return new_offset;
2359}
2360
8d0deca8
VS
2361/*
2362 * Computes the linear offset to the base tile and adjusts
2363 * x, y. bytes per pixel is assumed to be a power-of-two.
2364 *
2365 * In the 90/270 rotated case, x and y are assumed
2366 * to be already rotated to match the rotated GTT view, and
2367 * pitch is the tile_height aligned framebuffer height.
2368 */
4f2d9934
VS
2369u32 intel_compute_tile_offset(int *x, int *y,
2370 const struct drm_framebuffer *fb, int plane,
8d0deca8
VS
2371 unsigned int pitch,
2372 unsigned int rotation)
c2c75131 2373{
4f2d9934
VS
2374 const struct drm_i915_private *dev_priv = to_i915(fb->dev);
2375 uint64_t fb_modifier = fb->modifier[plane];
2376 unsigned int cpp = drm_format_plane_cpp(fb->pixel_format, plane);
29cf9491
VS
2377 u32 offset, offset_aligned, alignment;
2378
2379 alignment = intel_surf_alignment(dev_priv, fb_modifier);
2380 if (alignment)
2381 alignment--;
2382
b5c65338 2383 if (fb_modifier != DRM_FORMAT_MOD_NONE) {
8d0deca8
VS
2384 unsigned int tile_size, tile_width, tile_height;
2385 unsigned int tile_rows, tiles, pitch_tiles;
c2c75131 2386
d843310d 2387 tile_size = intel_tile_size(dev_priv);
8d0deca8
VS
2388 intel_tile_dims(dev_priv, &tile_width, &tile_height,
2389 fb_modifier, cpp);
2390
2391 if (intel_rotation_90_or_270(rotation)) {
2392 pitch_tiles = pitch / tile_height;
2393 swap(tile_width, tile_height);
2394 } else {
2395 pitch_tiles = pitch / (tile_width * cpp);
2396 }
d843310d
VS
2397
2398 tile_rows = *y / tile_height;
2399 *y %= tile_height;
c2c75131 2400
8d0deca8
VS
2401 tiles = *x / tile_width;
2402 *x %= tile_width;
bc752862 2403
29cf9491
VS
2404 offset = (tile_rows * pitch_tiles + tiles) * tile_size;
2405 offset_aligned = offset & ~alignment;
bc752862 2406
29cf9491
VS
2407 intel_adjust_tile_offset(x, y, tile_width, tile_height,
2408 tile_size, pitch_tiles,
2409 offset, offset_aligned);
2410 } else {
bc752862 2411 offset = *y * pitch + *x * cpp;
29cf9491
VS
2412 offset_aligned = offset & ~alignment;
2413
4e9a86b6
VS
2414 *y = (offset & alignment) / pitch;
2415 *x = ((offset & alignment) - *y * pitch) / cpp;
bc752862 2416 }
29cf9491
VS
2417
2418 return offset_aligned;
c2c75131
DV
2419}
2420
b35d63fa 2421static int i9xx_format_to_fourcc(int format)
46f297fb
JB
2422{
2423 switch (format) {
2424 case DISPPLANE_8BPP:
2425 return DRM_FORMAT_C8;
2426 case DISPPLANE_BGRX555:
2427 return DRM_FORMAT_XRGB1555;
2428 case DISPPLANE_BGRX565:
2429 return DRM_FORMAT_RGB565;
2430 default:
2431 case DISPPLANE_BGRX888:
2432 return DRM_FORMAT_XRGB8888;
2433 case DISPPLANE_RGBX888:
2434 return DRM_FORMAT_XBGR8888;
2435 case DISPPLANE_BGRX101010:
2436 return DRM_FORMAT_XRGB2101010;
2437 case DISPPLANE_RGBX101010:
2438 return DRM_FORMAT_XBGR2101010;
2439 }
2440}
2441
bc8d7dff
DL
2442static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
2443{
2444 switch (format) {
2445 case PLANE_CTL_FORMAT_RGB_565:
2446 return DRM_FORMAT_RGB565;
2447 default:
2448 case PLANE_CTL_FORMAT_XRGB_8888:
2449 if (rgb_order) {
2450 if (alpha)
2451 return DRM_FORMAT_ABGR8888;
2452 else
2453 return DRM_FORMAT_XBGR8888;
2454 } else {
2455 if (alpha)
2456 return DRM_FORMAT_ARGB8888;
2457 else
2458 return DRM_FORMAT_XRGB8888;
2459 }
2460 case PLANE_CTL_FORMAT_XRGB_2101010:
2461 if (rgb_order)
2462 return DRM_FORMAT_XBGR2101010;
2463 else
2464 return DRM_FORMAT_XRGB2101010;
2465 }
2466}
2467
5724dbd1 2468static bool
f6936e29
DV
2469intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
2470 struct intel_initial_plane_config *plane_config)
46f297fb
JB
2471{
2472 struct drm_device *dev = crtc->base.dev;
3badb49f 2473 struct drm_i915_private *dev_priv = to_i915(dev);
72e96d64 2474 struct i915_ggtt *ggtt = &dev_priv->ggtt;
46f297fb
JB
2475 struct drm_i915_gem_object *obj = NULL;
2476 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
2d14030b 2477 struct drm_framebuffer *fb = &plane_config->fb->base;
f37b5c2b
DV
2478 u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
2479 u32 size_aligned = round_up(plane_config->base + plane_config->size,
2480 PAGE_SIZE);
2481
2482 size_aligned -= base_aligned;
46f297fb 2483
ff2652ea
CW
2484 if (plane_config->size == 0)
2485 return false;
2486
3badb49f
PZ
2487 /* If the FB is too big, just don't use it since fbdev is not very
2488 * important and we should probably use that space with FBC or other
2489 * features. */
72e96d64 2490 if (size_aligned * 2 > ggtt->stolen_usable_size)
3badb49f
PZ
2491 return false;
2492
12c83d99
TU
2493 mutex_lock(&dev->struct_mutex);
2494
f37b5c2b
DV
2495 obj = i915_gem_object_create_stolen_for_preallocated(dev,
2496 base_aligned,
2497 base_aligned,
2498 size_aligned);
12c83d99
TU
2499 if (!obj) {
2500 mutex_unlock(&dev->struct_mutex);
484b41dd 2501 return false;
12c83d99 2502 }
46f297fb 2503
49af449b
DL
2504 obj->tiling_mode = plane_config->tiling;
2505 if (obj->tiling_mode == I915_TILING_X)
6bf129df 2506 obj->stride = fb->pitches[0];
46f297fb 2507
6bf129df
DL
2508 mode_cmd.pixel_format = fb->pixel_format;
2509 mode_cmd.width = fb->width;
2510 mode_cmd.height = fb->height;
2511 mode_cmd.pitches[0] = fb->pitches[0];
18c5247e
DV
2512 mode_cmd.modifier[0] = fb->modifier[0];
2513 mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
46f297fb 2514
6bf129df 2515 if (intel_framebuffer_init(dev, to_intel_framebuffer(fb),
484b41dd 2516 &mode_cmd, obj)) {
46f297fb
JB
2517 DRM_DEBUG_KMS("intel fb init failed\n");
2518 goto out_unref_obj;
2519 }
12c83d99 2520
46f297fb 2521 mutex_unlock(&dev->struct_mutex);
484b41dd 2522
f6936e29 2523 DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
484b41dd 2524 return true;
46f297fb
JB
2525
2526out_unref_obj:
2527 drm_gem_object_unreference(&obj->base);
2528 mutex_unlock(&dev->struct_mutex);
484b41dd
JB
2529 return false;
2530}
2531
5a21b665
DV
2532/* Update plane->state->fb to match plane->fb after driver-internal updates */
2533static void
2534update_state_fb(struct drm_plane *plane)
2535{
2536 if (plane->fb == plane->state->fb)
2537 return;
2538
2539 if (plane->state->fb)
2540 drm_framebuffer_unreference(plane->state->fb);
2541 plane->state->fb = plane->fb;
2542 if (plane->state->fb)
2543 drm_framebuffer_reference(plane->state->fb);
2544}
2545
5724dbd1 2546static void
f6936e29
DV
2547intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
2548 struct intel_initial_plane_config *plane_config)
484b41dd
JB
2549{
2550 struct drm_device *dev = intel_crtc->base.dev;
d9ceb816 2551 struct drm_i915_private *dev_priv = dev->dev_private;
484b41dd
JB
2552 struct drm_crtc *c;
2553 struct intel_crtc *i;
2ff8fde1 2554 struct drm_i915_gem_object *obj;
88595ac9 2555 struct drm_plane *primary = intel_crtc->base.primary;
be5651f2 2556 struct drm_plane_state *plane_state = primary->state;
200757f5
MR
2557 struct drm_crtc_state *crtc_state = intel_crtc->base.state;
2558 struct intel_plane *intel_plane = to_intel_plane(primary);
0a8d8a86
MR
2559 struct intel_plane_state *intel_state =
2560 to_intel_plane_state(plane_state);
88595ac9 2561 struct drm_framebuffer *fb;
484b41dd 2562
2d14030b 2563 if (!plane_config->fb)
484b41dd
JB
2564 return;
2565
f6936e29 2566 if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
88595ac9
DV
2567 fb = &plane_config->fb->base;
2568 goto valid_fb;
f55548b5 2569 }
484b41dd 2570
2d14030b 2571 kfree(plane_config->fb);
484b41dd
JB
2572
2573 /*
2574 * Failed to alloc the obj, check to see if we should share
2575 * an fb with another CRTC instead
2576 */
70e1e0ec 2577 for_each_crtc(dev, c) {
484b41dd
JB
2578 i = to_intel_crtc(c);
2579
2580 if (c == &intel_crtc->base)
2581 continue;
2582
2ff8fde1
MR
2583 if (!i->active)
2584 continue;
2585
88595ac9
DV
2586 fb = c->primary->fb;
2587 if (!fb)
484b41dd
JB
2588 continue;
2589
88595ac9 2590 obj = intel_fb_obj(fb);
2ff8fde1 2591 if (i915_gem_obj_ggtt_offset(obj) == plane_config->base) {
88595ac9
DV
2592 drm_framebuffer_reference(fb);
2593 goto valid_fb;
484b41dd
JB
2594 }
2595 }
88595ac9 2596
200757f5
MR
2597 /*
2598 * We've failed to reconstruct the BIOS FB. Current display state
2599 * indicates that the primary plane is visible, but has a NULL FB,
2600 * which will lead to problems later if we don't fix it up. The
2601 * simplest solution is to just disable the primary plane now and
2602 * pretend the BIOS never had it enabled.
2603 */
2604 to_intel_plane_state(plane_state)->visible = false;
2605 crtc_state->plane_mask &= ~(1 << drm_plane_index(primary));
2622a081 2606 intel_pre_disable_primary_noatomic(&intel_crtc->base);
200757f5
MR
2607 intel_plane->disable_plane(primary, &intel_crtc->base);
2608
88595ac9
DV
2609 return;
2610
2611valid_fb:
f44e2659
VS
2612 plane_state->src_x = 0;
2613 plane_state->src_y = 0;
be5651f2
ML
2614 plane_state->src_w = fb->width << 16;
2615 plane_state->src_h = fb->height << 16;
2616
f44e2659
VS
2617 plane_state->crtc_x = 0;
2618 plane_state->crtc_y = 0;
be5651f2
ML
2619 plane_state->crtc_w = fb->width;
2620 plane_state->crtc_h = fb->height;
2621
0a8d8a86
MR
2622 intel_state->src.x1 = plane_state->src_x;
2623 intel_state->src.y1 = plane_state->src_y;
2624 intel_state->src.x2 = plane_state->src_x + plane_state->src_w;
2625 intel_state->src.y2 = plane_state->src_y + plane_state->src_h;
2626 intel_state->dst.x1 = plane_state->crtc_x;
2627 intel_state->dst.y1 = plane_state->crtc_y;
2628 intel_state->dst.x2 = plane_state->crtc_x + plane_state->crtc_w;
2629 intel_state->dst.y2 = plane_state->crtc_y + plane_state->crtc_h;
2630
88595ac9
DV
2631 obj = intel_fb_obj(fb);
2632 if (obj->tiling_mode != I915_TILING_NONE)
2633 dev_priv->preserve_bios_swizzle = true;
2634
be5651f2
ML
2635 drm_framebuffer_reference(fb);
2636 primary->fb = primary->state->fb = fb;
36750f28 2637 primary->crtc = primary->state->crtc = &intel_crtc->base;
36750f28 2638 intel_crtc->base.state->plane_mask |= (1 << drm_plane_index(primary));
a9ff8714 2639 obj->frontbuffer_bits |= to_intel_plane(primary)->frontbuffer_bit;
46f297fb
JB
2640}
2641
a8d201af
ML
2642static void i9xx_update_primary_plane(struct drm_plane *primary,
2643 const struct intel_crtc_state *crtc_state,
2644 const struct intel_plane_state *plane_state)
81255565 2645{
a8d201af 2646 struct drm_device *dev = primary->dev;
81255565 2647 struct drm_i915_private *dev_priv = dev->dev_private;
a8d201af
ML
2648 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
2649 struct drm_framebuffer *fb = plane_state->base.fb;
2650 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
81255565 2651 int plane = intel_crtc->plane;
54ea9da8 2652 u32 linear_offset;
81255565 2653 u32 dspcntr;
f0f59a00 2654 i915_reg_t reg = DSPCNTR(plane);
8d0deca8 2655 unsigned int rotation = plane_state->base.rotation;
ac484963 2656 int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
54ea9da8
VS
2657 int x = plane_state->src.x1 >> 16;
2658 int y = plane_state->src.y1 >> 16;
c9ba6fad 2659
f45651ba
VS
2660 dspcntr = DISPPLANE_GAMMA_ENABLE;
2661
fdd508a6 2662 dspcntr |= DISPLAY_PLANE_ENABLE;
f45651ba
VS
2663
2664 if (INTEL_INFO(dev)->gen < 4) {
2665 if (intel_crtc->pipe == PIPE_B)
2666 dspcntr |= DISPPLANE_SEL_PIPE_B;
2667
2668 /* pipesrc and dspsize control the size that is scaled from,
2669 * which should always be the user's requested size.
2670 */
2671 I915_WRITE(DSPSIZE(plane),
a8d201af
ML
2672 ((crtc_state->pipe_src_h - 1) << 16) |
2673 (crtc_state->pipe_src_w - 1));
f45651ba 2674 I915_WRITE(DSPPOS(plane), 0);
c14b0485
VS
2675 } else if (IS_CHERRYVIEW(dev) && plane == PLANE_B) {
2676 I915_WRITE(PRIMSIZE(plane),
a8d201af
ML
2677 ((crtc_state->pipe_src_h - 1) << 16) |
2678 (crtc_state->pipe_src_w - 1));
c14b0485
VS
2679 I915_WRITE(PRIMPOS(plane), 0);
2680 I915_WRITE(PRIMCNSTALPHA(plane), 0);
f45651ba 2681 }
81255565 2682
57779d06
VS
2683 switch (fb->pixel_format) {
2684 case DRM_FORMAT_C8:
81255565
JB
2685 dspcntr |= DISPPLANE_8BPP;
2686 break;
57779d06 2687 case DRM_FORMAT_XRGB1555:
57779d06 2688 dspcntr |= DISPPLANE_BGRX555;
81255565 2689 break;
57779d06
VS
2690 case DRM_FORMAT_RGB565:
2691 dspcntr |= DISPPLANE_BGRX565;
2692 break;
2693 case DRM_FORMAT_XRGB8888:
57779d06
VS
2694 dspcntr |= DISPPLANE_BGRX888;
2695 break;
2696 case DRM_FORMAT_XBGR8888:
57779d06
VS
2697 dspcntr |= DISPPLANE_RGBX888;
2698 break;
2699 case DRM_FORMAT_XRGB2101010:
57779d06
VS
2700 dspcntr |= DISPPLANE_BGRX101010;
2701 break;
2702 case DRM_FORMAT_XBGR2101010:
57779d06 2703 dspcntr |= DISPPLANE_RGBX101010;
81255565
JB
2704 break;
2705 default:
baba133a 2706 BUG();
81255565 2707 }
57779d06 2708
f45651ba
VS
2709 if (INTEL_INFO(dev)->gen >= 4 &&
2710 obj->tiling_mode != I915_TILING_NONE)
2711 dspcntr |= DISPPLANE_TILED;
81255565 2712
de1aa629
VS
2713 if (IS_G4X(dev))
2714 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
2715
ac484963 2716 linear_offset = y * fb->pitches[0] + x * cpp;
81255565 2717
c2c75131
DV
2718 if (INTEL_INFO(dev)->gen >= 4) {
2719 intel_crtc->dspaddr_offset =
4f2d9934 2720 intel_compute_tile_offset(&x, &y, fb, 0,
8d0deca8 2721 fb->pitches[0], rotation);
c2c75131
DV
2722 linear_offset -= intel_crtc->dspaddr_offset;
2723 } else {
e506a0c6 2724 intel_crtc->dspaddr_offset = linear_offset;
c2c75131 2725 }
e506a0c6 2726
8d0deca8 2727 if (rotation == BIT(DRM_ROTATE_180)) {
48404c1e
SJ
2728 dspcntr |= DISPPLANE_ROTATE_180;
2729
a8d201af
ML
2730 x += (crtc_state->pipe_src_w - 1);
2731 y += (crtc_state->pipe_src_h - 1);
48404c1e
SJ
2732
2733 /* Finding the last pixel of the last line of the display
2734 data and adding to linear_offset*/
2735 linear_offset +=
a8d201af 2736 (crtc_state->pipe_src_h - 1) * fb->pitches[0] +
ac484963 2737 (crtc_state->pipe_src_w - 1) * cpp;
48404c1e
SJ
2738 }
2739
2db3366b
PZ
2740 intel_crtc->adjusted_x = x;
2741 intel_crtc->adjusted_y = y;
2742
48404c1e
SJ
2743 I915_WRITE(reg, dspcntr);
2744
01f2c773 2745 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
a6c45cf0 2746 if (INTEL_INFO(dev)->gen >= 4) {
85ba7b7d
DV
2747 I915_WRITE(DSPSURF(plane),
2748 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
5eddb70b 2749 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
e506a0c6 2750 I915_WRITE(DSPLINOFF(plane), linear_offset);
5eddb70b 2751 } else
f343c5f6 2752 I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
5eddb70b 2753 POSTING_READ(reg);
17638cd6
JB
2754}
2755
a8d201af
ML
2756static void i9xx_disable_primary_plane(struct drm_plane *primary,
2757 struct drm_crtc *crtc)
17638cd6
JB
2758{
2759 struct drm_device *dev = crtc->dev;
2760 struct drm_i915_private *dev_priv = dev->dev_private;
2761 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
17638cd6 2762 int plane = intel_crtc->plane;
f45651ba 2763
a8d201af
ML
2764 I915_WRITE(DSPCNTR(plane), 0);
2765 if (INTEL_INFO(dev_priv)->gen >= 4)
fdd508a6 2766 I915_WRITE(DSPSURF(plane), 0);
a8d201af
ML
2767 else
2768 I915_WRITE(DSPADDR(plane), 0);
2769 POSTING_READ(DSPCNTR(plane));
2770}
c9ba6fad 2771
a8d201af
ML
2772static void ironlake_update_primary_plane(struct drm_plane *primary,
2773 const struct intel_crtc_state *crtc_state,
2774 const struct intel_plane_state *plane_state)
2775{
2776 struct drm_device *dev = primary->dev;
2777 struct drm_i915_private *dev_priv = dev->dev_private;
2778 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
2779 struct drm_framebuffer *fb = plane_state->base.fb;
2780 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
2781 int plane = intel_crtc->plane;
54ea9da8 2782 u32 linear_offset;
a8d201af
ML
2783 u32 dspcntr;
2784 i915_reg_t reg = DSPCNTR(plane);
8d0deca8 2785 unsigned int rotation = plane_state->base.rotation;
ac484963 2786 int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
a8d201af
ML
2787 int x = plane_state->src.x1 >> 16;
2788 int y = plane_state->src.y1 >> 16;
c9ba6fad 2789
f45651ba 2790 dspcntr = DISPPLANE_GAMMA_ENABLE;
fdd508a6 2791 dspcntr |= DISPLAY_PLANE_ENABLE;
f45651ba
VS
2792
2793 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
2794 dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
17638cd6 2795
57779d06
VS
2796 switch (fb->pixel_format) {
2797 case DRM_FORMAT_C8:
17638cd6
JB
2798 dspcntr |= DISPPLANE_8BPP;
2799 break;
57779d06
VS
2800 case DRM_FORMAT_RGB565:
2801 dspcntr |= DISPPLANE_BGRX565;
17638cd6 2802 break;
57779d06 2803 case DRM_FORMAT_XRGB8888:
57779d06
VS
2804 dspcntr |= DISPPLANE_BGRX888;
2805 break;
2806 case DRM_FORMAT_XBGR8888:
57779d06
VS
2807 dspcntr |= DISPPLANE_RGBX888;
2808 break;
2809 case DRM_FORMAT_XRGB2101010:
57779d06
VS
2810 dspcntr |= DISPPLANE_BGRX101010;
2811 break;
2812 case DRM_FORMAT_XBGR2101010:
57779d06 2813 dspcntr |= DISPPLANE_RGBX101010;
17638cd6
JB
2814 break;
2815 default:
baba133a 2816 BUG();
17638cd6
JB
2817 }
2818
2819 if (obj->tiling_mode != I915_TILING_NONE)
2820 dspcntr |= DISPPLANE_TILED;
17638cd6 2821
f45651ba 2822 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev))
1f5d76db 2823 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
17638cd6 2824
ac484963 2825 linear_offset = y * fb->pitches[0] + x * cpp;
c2c75131 2826 intel_crtc->dspaddr_offset =
4f2d9934 2827 intel_compute_tile_offset(&x, &y, fb, 0,
8d0deca8 2828 fb->pitches[0], rotation);
c2c75131 2829 linear_offset -= intel_crtc->dspaddr_offset;
8d0deca8 2830 if (rotation == BIT(DRM_ROTATE_180)) {
48404c1e
SJ
2831 dspcntr |= DISPPLANE_ROTATE_180;
2832
2833 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev)) {
a8d201af
ML
2834 x += (crtc_state->pipe_src_w - 1);
2835 y += (crtc_state->pipe_src_h - 1);
48404c1e
SJ
2836
2837 /* Finding the last pixel of the last line of the display
2838 data and adding to linear_offset*/
2839 linear_offset +=
a8d201af 2840 (crtc_state->pipe_src_h - 1) * fb->pitches[0] +
ac484963 2841 (crtc_state->pipe_src_w - 1) * cpp;
48404c1e
SJ
2842 }
2843 }
2844
2db3366b
PZ
2845 intel_crtc->adjusted_x = x;
2846 intel_crtc->adjusted_y = y;
2847
48404c1e 2848 I915_WRITE(reg, dspcntr);
17638cd6 2849
01f2c773 2850 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
85ba7b7d
DV
2851 I915_WRITE(DSPSURF(plane),
2852 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
b3dc685e 2853 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
bc1c91eb
DL
2854 I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
2855 } else {
2856 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2857 I915_WRITE(DSPLINOFF(plane), linear_offset);
2858 }
17638cd6 2859 POSTING_READ(reg);
17638cd6
JB
2860}
2861
7b49f948
VS
2862u32 intel_fb_stride_alignment(const struct drm_i915_private *dev_priv,
2863 uint64_t fb_modifier, uint32_t pixel_format)
b321803d 2864{
7b49f948 2865 if (fb_modifier == DRM_FORMAT_MOD_NONE) {
b321803d 2866 return 64;
7b49f948
VS
2867 } else {
2868 int cpp = drm_format_plane_cpp(pixel_format, 0);
2869
27ba3910 2870 return intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
b321803d
DL
2871 }
2872}
2873
44eb0cb9
MK
2874u32 intel_plane_obj_offset(struct intel_plane *intel_plane,
2875 struct drm_i915_gem_object *obj,
2876 unsigned int plane)
121920fa 2877{
ce7f1728 2878 struct i915_ggtt_view view;
dedf278c 2879 struct i915_vma *vma;
44eb0cb9 2880 u64 offset;
121920fa 2881
e7941294 2882 intel_fill_fb_ggtt_view(&view, intel_plane->base.state->fb,
3465c580 2883 intel_plane->base.state->rotation);
121920fa 2884
ce7f1728 2885 vma = i915_gem_obj_to_ggtt_view(obj, &view);
dedf278c 2886 if (WARN(!vma, "ggtt vma for display object not found! (view=%u)\n",
ce7f1728 2887 view.type))
dedf278c
TU
2888 return -1;
2889
44eb0cb9 2890 offset = vma->node.start;
dedf278c
TU
2891
2892 if (plane == 1) {
7723f47d 2893 offset += vma->ggtt_view.params.rotated.uv_start_page *
dedf278c
TU
2894 PAGE_SIZE;
2895 }
2896
44eb0cb9
MK
2897 WARN_ON(upper_32_bits(offset));
2898
2899 return lower_32_bits(offset);
121920fa
TU
2900}
2901
e435d6e5
ML
2902static void skl_detach_scaler(struct intel_crtc *intel_crtc, int id)
2903{
2904 struct drm_device *dev = intel_crtc->base.dev;
2905 struct drm_i915_private *dev_priv = dev->dev_private;
2906
2907 I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, id), 0);
2908 I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, id), 0);
2909 I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, id), 0);
e435d6e5
ML
2910}
2911
a1b2278e
CK
2912/*
2913 * This function detaches (aka. unbinds) unused scalers in hardware
2914 */
0583236e 2915static void skl_detach_scalers(struct intel_crtc *intel_crtc)
a1b2278e 2916{
a1b2278e
CK
2917 struct intel_crtc_scaler_state *scaler_state;
2918 int i;
2919
a1b2278e
CK
2920 scaler_state = &intel_crtc->config->scaler_state;
2921
2922 /* loop through and disable scalers that aren't in use */
2923 for (i = 0; i < intel_crtc->num_scalers; i++) {
e435d6e5
ML
2924 if (!scaler_state->scalers[i].in_use)
2925 skl_detach_scaler(intel_crtc, i);
a1b2278e
CK
2926 }
2927}
2928
6156a456 2929u32 skl_plane_ctl_format(uint32_t pixel_format)
70d21f0e 2930{
6156a456 2931 switch (pixel_format) {
d161cf7a 2932 case DRM_FORMAT_C8:
c34ce3d1 2933 return PLANE_CTL_FORMAT_INDEXED;
70d21f0e 2934 case DRM_FORMAT_RGB565:
c34ce3d1 2935 return PLANE_CTL_FORMAT_RGB_565;
70d21f0e 2936 case DRM_FORMAT_XBGR8888:
c34ce3d1 2937 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
6156a456 2938 case DRM_FORMAT_XRGB8888:
c34ce3d1 2939 return PLANE_CTL_FORMAT_XRGB_8888;
6156a456
CK
2940 /*
2941 * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
2942 * to be already pre-multiplied. We need to add a knob (or a different
2943 * DRM_FORMAT) for user-space to configure that.
2944 */
f75fb42a 2945 case DRM_FORMAT_ABGR8888:
c34ce3d1 2946 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX |
6156a456 2947 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
6156a456 2948 case DRM_FORMAT_ARGB8888:
c34ce3d1 2949 return PLANE_CTL_FORMAT_XRGB_8888 |
6156a456 2950 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
70d21f0e 2951 case DRM_FORMAT_XRGB2101010:
c34ce3d1 2952 return PLANE_CTL_FORMAT_XRGB_2101010;
70d21f0e 2953 case DRM_FORMAT_XBGR2101010:
c34ce3d1 2954 return PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
6156a456 2955 case DRM_FORMAT_YUYV:
c34ce3d1 2956 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
6156a456 2957 case DRM_FORMAT_YVYU:
c34ce3d1 2958 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
6156a456 2959 case DRM_FORMAT_UYVY:
c34ce3d1 2960 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
6156a456 2961 case DRM_FORMAT_VYUY:
c34ce3d1 2962 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
70d21f0e 2963 default:
4249eeef 2964 MISSING_CASE(pixel_format);
70d21f0e 2965 }
8cfcba41 2966
c34ce3d1 2967 return 0;
6156a456 2968}
70d21f0e 2969
6156a456
CK
2970u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
2971{
6156a456 2972 switch (fb_modifier) {
30af77c4 2973 case DRM_FORMAT_MOD_NONE:
70d21f0e 2974 break;
30af77c4 2975 case I915_FORMAT_MOD_X_TILED:
c34ce3d1 2976 return PLANE_CTL_TILED_X;
b321803d 2977 case I915_FORMAT_MOD_Y_TILED:
c34ce3d1 2978 return PLANE_CTL_TILED_Y;
b321803d 2979 case I915_FORMAT_MOD_Yf_TILED:
c34ce3d1 2980 return PLANE_CTL_TILED_YF;
70d21f0e 2981 default:
6156a456 2982 MISSING_CASE(fb_modifier);
70d21f0e 2983 }
8cfcba41 2984
c34ce3d1 2985 return 0;
6156a456 2986}
70d21f0e 2987
6156a456
CK
2988u32 skl_plane_ctl_rotation(unsigned int rotation)
2989{
3b7a5119 2990 switch (rotation) {
6156a456
CK
2991 case BIT(DRM_ROTATE_0):
2992 break;
1e8df167
SJ
2993 /*
2994 * DRM_ROTATE_ is counter clockwise to stay compatible with Xrandr
2995 * while i915 HW rotation is clockwise, thats why this swapping.
2996 */
3b7a5119 2997 case BIT(DRM_ROTATE_90):
1e8df167 2998 return PLANE_CTL_ROTATE_270;
3b7a5119 2999 case BIT(DRM_ROTATE_180):
c34ce3d1 3000 return PLANE_CTL_ROTATE_180;
3b7a5119 3001 case BIT(DRM_ROTATE_270):
1e8df167 3002 return PLANE_CTL_ROTATE_90;
6156a456
CK
3003 default:
3004 MISSING_CASE(rotation);
3005 }
3006
c34ce3d1 3007 return 0;
6156a456
CK
3008}
3009
a8d201af
ML
3010static void skylake_update_primary_plane(struct drm_plane *plane,
3011 const struct intel_crtc_state *crtc_state,
3012 const struct intel_plane_state *plane_state)
6156a456 3013{
a8d201af 3014 struct drm_device *dev = plane->dev;
6156a456 3015 struct drm_i915_private *dev_priv = dev->dev_private;
a8d201af
ML
3016 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
3017 struct drm_framebuffer *fb = plane_state->base.fb;
3018 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
6156a456
CK
3019 int pipe = intel_crtc->pipe;
3020 u32 plane_ctl, stride_div, stride;
3021 u32 tile_height, plane_offset, plane_size;
a8d201af 3022 unsigned int rotation = plane_state->base.rotation;
6156a456 3023 int x_offset, y_offset;
44eb0cb9 3024 u32 surf_addr;
a8d201af
ML
3025 int scaler_id = plane_state->scaler_id;
3026 int src_x = plane_state->src.x1 >> 16;
3027 int src_y = plane_state->src.y1 >> 16;
3028 int src_w = drm_rect_width(&plane_state->src) >> 16;
3029 int src_h = drm_rect_height(&plane_state->src) >> 16;
3030 int dst_x = plane_state->dst.x1;
3031 int dst_y = plane_state->dst.y1;
3032 int dst_w = drm_rect_width(&plane_state->dst);
3033 int dst_h = drm_rect_height(&plane_state->dst);
70d21f0e 3034
6156a456
CK
3035 plane_ctl = PLANE_CTL_ENABLE |
3036 PLANE_CTL_PIPE_GAMMA_ENABLE |
3037 PLANE_CTL_PIPE_CSC_ENABLE;
3038
3039 plane_ctl |= skl_plane_ctl_format(fb->pixel_format);
3040 plane_ctl |= skl_plane_ctl_tiling(fb->modifier[0]);
3041 plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
6156a456
CK
3042 plane_ctl |= skl_plane_ctl_rotation(rotation);
3043
7b49f948 3044 stride_div = intel_fb_stride_alignment(dev_priv, fb->modifier[0],
b321803d 3045 fb->pixel_format);
dedf278c 3046 surf_addr = intel_plane_obj_offset(to_intel_plane(plane), obj, 0);
3b7a5119 3047
a42e5a23
PZ
3048 WARN_ON(drm_rect_width(&plane_state->src) == 0);
3049
3b7a5119 3050 if (intel_rotation_90_or_270(rotation)) {
832be82f
VS
3051 int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
3052
3b7a5119 3053 /* stride = Surface height in tiles */
832be82f 3054 tile_height = intel_tile_height(dev_priv, fb->modifier[0], cpp);
3b7a5119 3055 stride = DIV_ROUND_UP(fb->height, tile_height);
a8d201af
ML
3056 x_offset = stride * tile_height - src_y - src_h;
3057 y_offset = src_x;
6156a456 3058 plane_size = (src_w - 1) << 16 | (src_h - 1);
3b7a5119
SJ
3059 } else {
3060 stride = fb->pitches[0] / stride_div;
a8d201af
ML
3061 x_offset = src_x;
3062 y_offset = src_y;
6156a456 3063 plane_size = (src_h - 1) << 16 | (src_w - 1);
3b7a5119
SJ
3064 }
3065 plane_offset = y_offset << 16 | x_offset;
b321803d 3066
2db3366b
PZ
3067 intel_crtc->adjusted_x = x_offset;
3068 intel_crtc->adjusted_y = y_offset;
3069
70d21f0e 3070 I915_WRITE(PLANE_CTL(pipe, 0), plane_ctl);
3b7a5119
SJ
3071 I915_WRITE(PLANE_OFFSET(pipe, 0), plane_offset);
3072 I915_WRITE(PLANE_SIZE(pipe, 0), plane_size);
3073 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
6156a456
CK
3074
3075 if (scaler_id >= 0) {
3076 uint32_t ps_ctrl = 0;
3077
3078 WARN_ON(!dst_w || !dst_h);
3079 ps_ctrl = PS_SCALER_EN | PS_PLANE_SEL(0) |
3080 crtc_state->scaler_state.scalers[scaler_id].mode;
3081 I915_WRITE(SKL_PS_CTRL(pipe, scaler_id), ps_ctrl);
3082 I915_WRITE(SKL_PS_PWR_GATE(pipe, scaler_id), 0);
3083 I915_WRITE(SKL_PS_WIN_POS(pipe, scaler_id), (dst_x << 16) | dst_y);
3084 I915_WRITE(SKL_PS_WIN_SZ(pipe, scaler_id), (dst_w << 16) | dst_h);
3085 I915_WRITE(PLANE_POS(pipe, 0), 0);
3086 } else {
3087 I915_WRITE(PLANE_POS(pipe, 0), (dst_y << 16) | dst_x);
3088 }
3089
121920fa 3090 I915_WRITE(PLANE_SURF(pipe, 0), surf_addr);
70d21f0e
DL
3091
3092 POSTING_READ(PLANE_SURF(pipe, 0));
3093}
3094
a8d201af
ML
3095static void skylake_disable_primary_plane(struct drm_plane *primary,
3096 struct drm_crtc *crtc)
17638cd6
JB
3097{
3098 struct drm_device *dev = crtc->dev;
3099 struct drm_i915_private *dev_priv = dev->dev_private;
a8d201af 3100 int pipe = to_intel_crtc(crtc)->pipe;
17638cd6 3101
a8d201af
ML
3102 I915_WRITE(PLANE_CTL(pipe, 0), 0);
3103 I915_WRITE(PLANE_SURF(pipe, 0), 0);
3104 POSTING_READ(PLANE_SURF(pipe, 0));
3105}
29b9bde6 3106
a8d201af
ML
3107/* Assume fb object is pinned & idle & fenced and just update base pointers */
3108static int
3109intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
3110 int x, int y, enum mode_set_atomic state)
3111{
3112 /* Support for kgdboc is disabled, this needs a major rework. */
3113 DRM_ERROR("legacy panic handler not supported any more.\n");
3114
3115 return -ENODEV;
81255565
JB
3116}
3117
5a21b665
DV
3118static void intel_complete_page_flips(struct drm_i915_private *dev_priv)
3119{
3120 struct intel_crtc *crtc;
3121
3122 for_each_intel_crtc(dev_priv->dev, crtc)
3123 intel_finish_page_flip_cs(dev_priv, crtc->pipe);
3124}
3125
7514747d
VS
3126static void intel_update_primary_planes(struct drm_device *dev)
3127{
7514747d 3128 struct drm_crtc *crtc;
96a02917 3129
70e1e0ec 3130 for_each_crtc(dev, crtc) {
11c22da6
ML
3131 struct intel_plane *plane = to_intel_plane(crtc->primary);
3132 struct intel_plane_state *plane_state;
96a02917 3133
11c22da6 3134 drm_modeset_lock_crtc(crtc, &plane->base);
11c22da6
ML
3135 plane_state = to_intel_plane_state(plane->base.state);
3136
a8d201af
ML
3137 if (plane_state->visible)
3138 plane->update_plane(&plane->base,
3139 to_intel_crtc_state(crtc->state),
3140 plane_state);
11c22da6
ML
3141
3142 drm_modeset_unlock_crtc(crtc);
96a02917
VS
3143 }
3144}
3145
c033666a 3146void intel_prepare_reset(struct drm_i915_private *dev_priv)
7514747d
VS
3147{
3148 /* no reset support for gen2 */
c033666a 3149 if (IS_GEN2(dev_priv))
7514747d
VS
3150 return;
3151
3152 /* reset doesn't touch the display */
c033666a 3153 if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
7514747d
VS
3154 return;
3155
c033666a 3156 drm_modeset_lock_all(dev_priv->dev);
f98ce92f
VS
3157 /*
3158 * Disabling the crtcs gracefully seems nicer. Also the
3159 * g33 docs say we should at least disable all the planes.
3160 */
c033666a 3161 intel_display_suspend(dev_priv->dev);
7514747d
VS
3162}
3163
c033666a 3164void intel_finish_reset(struct drm_i915_private *dev_priv)
7514747d 3165{
5a21b665
DV
3166 /*
3167 * Flips in the rings will be nuked by the reset,
3168 * so complete all pending flips so that user space
3169 * will get its events and not get stuck.
3170 */
3171 intel_complete_page_flips(dev_priv);
3172
7514747d 3173 /* no reset support for gen2 */
c033666a 3174 if (IS_GEN2(dev_priv))
7514747d
VS
3175 return;
3176
3177 /* reset doesn't touch the display */
c033666a 3178 if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv)) {
7514747d
VS
3179 /*
3180 * Flips in the rings have been nuked by the reset,
3181 * so update the base address of all primary
3182 * planes to the the last fb to make sure we're
3183 * showing the correct fb after a reset.
11c22da6
ML
3184 *
3185 * FIXME: Atomic will make this obsolete since we won't schedule
3186 * CS-based flips (which might get lost in gpu resets) any more.
7514747d 3187 */
c033666a 3188 intel_update_primary_planes(dev_priv->dev);
7514747d
VS
3189 return;
3190 }
3191
3192 /*
3193 * The display has been reset as well,
3194 * so need a full re-initialization.
3195 */
3196 intel_runtime_pm_disable_interrupts(dev_priv);
3197 intel_runtime_pm_enable_interrupts(dev_priv);
3198
c033666a 3199 intel_modeset_init_hw(dev_priv->dev);
7514747d
VS
3200
3201 spin_lock_irq(&dev_priv->irq_lock);
3202 if (dev_priv->display.hpd_irq_setup)
91d14251 3203 dev_priv->display.hpd_irq_setup(dev_priv);
7514747d
VS
3204 spin_unlock_irq(&dev_priv->irq_lock);
3205
c033666a 3206 intel_display_resume(dev_priv->dev);
7514747d
VS
3207
3208 intel_hpd_init(dev_priv);
3209
c033666a 3210 drm_modeset_unlock_all(dev_priv->dev);
7514747d
VS
3211}
3212
7d5e3799
CW
3213static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
3214{
5a21b665
DV
3215 struct drm_device *dev = crtc->dev;
3216 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3217 unsigned reset_counter;
3218 bool pending;
3219
3220 reset_counter = i915_reset_counter(&to_i915(dev)->gpu_error);
3221 if (intel_crtc->reset_counter != reset_counter)
3222 return false;
3223
3224 spin_lock_irq(&dev->event_lock);
3225 pending = to_intel_crtc(crtc)->flip_work != NULL;
3226 spin_unlock_irq(&dev->event_lock);
3227
3228 return pending;
7d5e3799
CW
3229}
3230
bfd16b2a
ML
3231static void intel_update_pipe_config(struct intel_crtc *crtc,
3232 struct intel_crtc_state *old_crtc_state)
e30e8f75
GP
3233{
3234 struct drm_device *dev = crtc->base.dev;
3235 struct drm_i915_private *dev_priv = dev->dev_private;
bfd16b2a
ML
3236 struct intel_crtc_state *pipe_config =
3237 to_intel_crtc_state(crtc->base.state);
e30e8f75 3238
bfd16b2a
ML
3239 /* drm_atomic_helper_update_legacy_modeset_state might not be called. */
3240 crtc->base.mode = crtc->base.state->mode;
3241
3242 DRM_DEBUG_KMS("Updating pipe size %ix%i -> %ix%i\n",
3243 old_crtc_state->pipe_src_w, old_crtc_state->pipe_src_h,
3244 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
e30e8f75
GP
3245
3246 /*
3247 * Update pipe size and adjust fitter if needed: the reason for this is
3248 * that in compute_mode_changes we check the native mode (not the pfit
3249 * mode) to see if we can flip rather than do a full mode set. In the
3250 * fastboot case, we'll flip, but if we don't update the pipesrc and
3251 * pfit state, we'll end up with a big fb scanned out into the wrong
3252 * sized surface.
e30e8f75
GP
3253 */
3254
e30e8f75 3255 I915_WRITE(PIPESRC(crtc->pipe),
bfd16b2a
ML
3256 ((pipe_config->pipe_src_w - 1) << 16) |
3257 (pipe_config->pipe_src_h - 1));
3258
3259 /* on skylake this is done by detaching scalers */
3260 if (INTEL_INFO(dev)->gen >= 9) {
3261 skl_detach_scalers(crtc);
3262
3263 if (pipe_config->pch_pfit.enabled)
3264 skylake_pfit_enable(crtc);
3265 } else if (HAS_PCH_SPLIT(dev)) {
3266 if (pipe_config->pch_pfit.enabled)
3267 ironlake_pfit_enable(crtc);
3268 else if (old_crtc_state->pch_pfit.enabled)
3269 ironlake_pfit_disable(crtc, true);
e30e8f75 3270 }
e30e8f75
GP
3271}
3272
5e84e1a4
ZW
3273static void intel_fdi_normal_train(struct drm_crtc *crtc)
3274{
3275 struct drm_device *dev = crtc->dev;
3276 struct drm_i915_private *dev_priv = dev->dev_private;
3277 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3278 int pipe = intel_crtc->pipe;
f0f59a00
VS
3279 i915_reg_t reg;
3280 u32 temp;
5e84e1a4
ZW
3281
3282 /* enable normal train */
3283 reg = FDI_TX_CTL(pipe);
3284 temp = I915_READ(reg);
61e499bf 3285 if (IS_IVYBRIDGE(dev)) {
357555c0
JB
3286 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3287 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
61e499bf
KP
3288 } else {
3289 temp &= ~FDI_LINK_TRAIN_NONE;
3290 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
357555c0 3291 }
5e84e1a4
ZW
3292 I915_WRITE(reg, temp);
3293
3294 reg = FDI_RX_CTL(pipe);
3295 temp = I915_READ(reg);
3296 if (HAS_PCH_CPT(dev)) {
3297 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3298 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
3299 } else {
3300 temp &= ~FDI_LINK_TRAIN_NONE;
3301 temp |= FDI_LINK_TRAIN_NONE;
3302 }
3303 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
3304
3305 /* wait one idle pattern time */
3306 POSTING_READ(reg);
3307 udelay(1000);
357555c0
JB
3308
3309 /* IVB wants error correction enabled */
3310 if (IS_IVYBRIDGE(dev))
3311 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
3312 FDI_FE_ERRC_ENABLE);
5e84e1a4
ZW
3313}
3314
8db9d77b
ZW
3315/* The FDI link training functions for ILK/Ibexpeak. */
3316static void ironlake_fdi_link_train(struct drm_crtc *crtc)
3317{
3318 struct drm_device *dev = crtc->dev;
3319 struct drm_i915_private *dev_priv = dev->dev_private;
3320 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3321 int pipe = intel_crtc->pipe;
f0f59a00
VS
3322 i915_reg_t reg;
3323 u32 temp, tries;
8db9d77b 3324
1c8562f6 3325 /* FDI needs bits from pipe first */
0fc932b8 3326 assert_pipe_enabled(dev_priv, pipe);
0fc932b8 3327
e1a44743
AJ
3328 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3329 for train result */
5eddb70b
CW
3330 reg = FDI_RX_IMR(pipe);
3331 temp = I915_READ(reg);
e1a44743
AJ
3332 temp &= ~FDI_RX_SYMBOL_LOCK;
3333 temp &= ~FDI_RX_BIT_LOCK;
5eddb70b
CW
3334 I915_WRITE(reg, temp);
3335 I915_READ(reg);
e1a44743
AJ
3336 udelay(150);
3337
8db9d77b 3338 /* enable CPU FDI TX and PCH FDI RX */
5eddb70b
CW
3339 reg = FDI_TX_CTL(pipe);
3340 temp = I915_READ(reg);
627eb5a3 3341 temp &= ~FDI_DP_PORT_WIDTH_MASK;
6e3c9717 3342 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
8db9d77b
ZW
3343 temp &= ~FDI_LINK_TRAIN_NONE;
3344 temp |= FDI_LINK_TRAIN_PATTERN_1;
5eddb70b 3345 I915_WRITE(reg, temp | FDI_TX_ENABLE);
8db9d77b 3346
5eddb70b
CW
3347 reg = FDI_RX_CTL(pipe);
3348 temp = I915_READ(reg);
8db9d77b
ZW
3349 temp &= ~FDI_LINK_TRAIN_NONE;
3350 temp |= FDI_LINK_TRAIN_PATTERN_1;
5eddb70b
CW
3351 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3352
3353 POSTING_READ(reg);
8db9d77b
ZW
3354 udelay(150);
3355
5b2adf89 3356 /* Ironlake workaround, enable clock pointer after FDI enable*/
8f5718a6
DV
3357 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
3358 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
3359 FDI_RX_PHASE_SYNC_POINTER_EN);
5b2adf89 3360
5eddb70b 3361 reg = FDI_RX_IIR(pipe);
e1a44743 3362 for (tries = 0; tries < 5; tries++) {
5eddb70b 3363 temp = I915_READ(reg);
8db9d77b
ZW
3364 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3365
3366 if ((temp & FDI_RX_BIT_LOCK)) {
3367 DRM_DEBUG_KMS("FDI train 1 done.\n");
5eddb70b 3368 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
8db9d77b
ZW
3369 break;
3370 }
8db9d77b 3371 }
e1a44743 3372 if (tries == 5)
5eddb70b 3373 DRM_ERROR("FDI train 1 fail!\n");
8db9d77b
ZW
3374
3375 /* Train 2 */
5eddb70b
CW
3376 reg = FDI_TX_CTL(pipe);
3377 temp = I915_READ(reg);
8db9d77b
ZW
3378 temp &= ~FDI_LINK_TRAIN_NONE;
3379 temp |= FDI_LINK_TRAIN_PATTERN_2;
5eddb70b 3380 I915_WRITE(reg, temp);
8db9d77b 3381
5eddb70b
CW
3382 reg = FDI_RX_CTL(pipe);
3383 temp = I915_READ(reg);
8db9d77b
ZW
3384 temp &= ~FDI_LINK_TRAIN_NONE;
3385 temp |= FDI_LINK_TRAIN_PATTERN_2;
5eddb70b 3386 I915_WRITE(reg, temp);
8db9d77b 3387
5eddb70b
CW
3388 POSTING_READ(reg);
3389 udelay(150);
8db9d77b 3390
5eddb70b 3391 reg = FDI_RX_IIR(pipe);
e1a44743 3392 for (tries = 0; tries < 5; tries++) {
5eddb70b 3393 temp = I915_READ(reg);
8db9d77b
ZW
3394 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3395
3396 if (temp & FDI_RX_SYMBOL_LOCK) {
5eddb70b 3397 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
8db9d77b
ZW
3398 DRM_DEBUG_KMS("FDI train 2 done.\n");
3399 break;
3400 }
8db9d77b 3401 }
e1a44743 3402 if (tries == 5)
5eddb70b 3403 DRM_ERROR("FDI train 2 fail!\n");
8db9d77b
ZW
3404
3405 DRM_DEBUG_KMS("FDI train done\n");
5c5313c8 3406
8db9d77b
ZW
3407}
3408
0206e353 3409static const int snb_b_fdi_train_param[] = {
8db9d77b
ZW
3410 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
3411 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
3412 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
3413 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
3414};
3415
3416/* The FDI link training functions for SNB/Cougarpoint. */
3417static void gen6_fdi_link_train(struct drm_crtc *crtc)
3418{
3419 struct drm_device *dev = crtc->dev;
3420 struct drm_i915_private *dev_priv = dev->dev_private;
3421 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3422 int pipe = intel_crtc->pipe;
f0f59a00
VS
3423 i915_reg_t reg;
3424 u32 temp, i, retry;
8db9d77b 3425
e1a44743
AJ
3426 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3427 for train result */
5eddb70b
CW
3428 reg = FDI_RX_IMR(pipe);
3429 temp = I915_READ(reg);
e1a44743
AJ
3430 temp &= ~FDI_RX_SYMBOL_LOCK;
3431 temp &= ~FDI_RX_BIT_LOCK;
5eddb70b
CW
3432 I915_WRITE(reg, temp);
3433
3434 POSTING_READ(reg);
e1a44743
AJ
3435 udelay(150);
3436
8db9d77b 3437 /* enable CPU FDI TX and PCH FDI RX */
5eddb70b
CW
3438 reg = FDI_TX_CTL(pipe);
3439 temp = I915_READ(reg);
627eb5a3 3440 temp &= ~FDI_DP_PORT_WIDTH_MASK;
6e3c9717 3441 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
8db9d77b
ZW
3442 temp &= ~FDI_LINK_TRAIN_NONE;
3443 temp |= FDI_LINK_TRAIN_PATTERN_1;
3444 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3445 /* SNB-B */
3446 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
5eddb70b 3447 I915_WRITE(reg, temp | FDI_TX_ENABLE);
8db9d77b 3448
d74cf324
DV
3449 I915_WRITE(FDI_RX_MISC(pipe),
3450 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3451
5eddb70b
CW
3452 reg = FDI_RX_CTL(pipe);
3453 temp = I915_READ(reg);
8db9d77b
ZW
3454 if (HAS_PCH_CPT(dev)) {
3455 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3456 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3457 } else {
3458 temp &= ~FDI_LINK_TRAIN_NONE;
3459 temp |= FDI_LINK_TRAIN_PATTERN_1;
3460 }
5eddb70b
CW
3461 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3462
3463 POSTING_READ(reg);
8db9d77b
ZW
3464 udelay(150);
3465
0206e353 3466 for (i = 0; i < 4; i++) {
5eddb70b
CW
3467 reg = FDI_TX_CTL(pipe);
3468 temp = I915_READ(reg);
8db9d77b
ZW
3469 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3470 temp |= snb_b_fdi_train_param[i];
5eddb70b
CW
3471 I915_WRITE(reg, temp);
3472
3473 POSTING_READ(reg);
8db9d77b
ZW
3474 udelay(500);
3475
fa37d39e
SP
3476 for (retry = 0; retry < 5; retry++) {
3477 reg = FDI_RX_IIR(pipe);
3478 temp = I915_READ(reg);
3479 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3480 if (temp & FDI_RX_BIT_LOCK) {
3481 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3482 DRM_DEBUG_KMS("FDI train 1 done.\n");
3483 break;
3484 }
3485 udelay(50);
8db9d77b 3486 }
fa37d39e
SP
3487 if (retry < 5)
3488 break;
8db9d77b
ZW
3489 }
3490 if (i == 4)
5eddb70b 3491 DRM_ERROR("FDI train 1 fail!\n");
8db9d77b
ZW
3492
3493 /* Train 2 */
5eddb70b
CW
3494 reg = FDI_TX_CTL(pipe);
3495 temp = I915_READ(reg);
8db9d77b
ZW
3496 temp &= ~FDI_LINK_TRAIN_NONE;
3497 temp |= FDI_LINK_TRAIN_PATTERN_2;
3498 if (IS_GEN6(dev)) {
3499 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3500 /* SNB-B */
3501 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
3502 }
5eddb70b 3503 I915_WRITE(reg, temp);
8db9d77b 3504
5eddb70b
CW
3505 reg = FDI_RX_CTL(pipe);
3506 temp = I915_READ(reg);
8db9d77b
ZW
3507 if (HAS_PCH_CPT(dev)) {
3508 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3509 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
3510 } else {
3511 temp &= ~FDI_LINK_TRAIN_NONE;
3512 temp |= FDI_LINK_TRAIN_PATTERN_2;
3513 }
5eddb70b
CW
3514 I915_WRITE(reg, temp);
3515
3516 POSTING_READ(reg);
8db9d77b
ZW
3517 udelay(150);
3518
0206e353 3519 for (i = 0; i < 4; i++) {
5eddb70b
CW
3520 reg = FDI_TX_CTL(pipe);
3521 temp = I915_READ(reg);
8db9d77b
ZW
3522 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3523 temp |= snb_b_fdi_train_param[i];
5eddb70b
CW
3524 I915_WRITE(reg, temp);
3525
3526 POSTING_READ(reg);
8db9d77b
ZW
3527 udelay(500);
3528
fa37d39e
SP
3529 for (retry = 0; retry < 5; retry++) {
3530 reg = FDI_RX_IIR(pipe);
3531 temp = I915_READ(reg);
3532 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3533 if (temp & FDI_RX_SYMBOL_LOCK) {
3534 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3535 DRM_DEBUG_KMS("FDI train 2 done.\n");
3536 break;
3537 }
3538 udelay(50);
8db9d77b 3539 }
fa37d39e
SP
3540 if (retry < 5)
3541 break;
8db9d77b
ZW
3542 }
3543 if (i == 4)
5eddb70b 3544 DRM_ERROR("FDI train 2 fail!\n");
8db9d77b
ZW
3545
3546 DRM_DEBUG_KMS("FDI train done.\n");
3547}
3548
357555c0
JB
3549/* Manual link training for Ivy Bridge A0 parts */
3550static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
3551{
3552 struct drm_device *dev = crtc->dev;
3553 struct drm_i915_private *dev_priv = dev->dev_private;
3554 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3555 int pipe = intel_crtc->pipe;
f0f59a00
VS
3556 i915_reg_t reg;
3557 u32 temp, i, j;
357555c0
JB
3558
3559 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3560 for train result */
3561 reg = FDI_RX_IMR(pipe);
3562 temp = I915_READ(reg);
3563 temp &= ~FDI_RX_SYMBOL_LOCK;
3564 temp &= ~FDI_RX_BIT_LOCK;
3565 I915_WRITE(reg, temp);
3566
3567 POSTING_READ(reg);
3568 udelay(150);
3569
01a415fd
DV
3570 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
3571 I915_READ(FDI_RX_IIR(pipe)));
3572
139ccd3f
JB
3573 /* Try each vswing and preemphasis setting twice before moving on */
3574 for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
3575 /* disable first in case we need to retry */
3576 reg = FDI_TX_CTL(pipe);
3577 temp = I915_READ(reg);
3578 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
3579 temp &= ~FDI_TX_ENABLE;
3580 I915_WRITE(reg, temp);
357555c0 3581
139ccd3f
JB
3582 reg = FDI_RX_CTL(pipe);
3583 temp = I915_READ(reg);
3584 temp &= ~FDI_LINK_TRAIN_AUTO;
3585 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3586 temp &= ~FDI_RX_ENABLE;
3587 I915_WRITE(reg, temp);
357555c0 3588
139ccd3f 3589 /* enable CPU FDI TX and PCH FDI RX */
357555c0
JB
3590 reg = FDI_TX_CTL(pipe);
3591 temp = I915_READ(reg);
139ccd3f 3592 temp &= ~FDI_DP_PORT_WIDTH_MASK;
6e3c9717 3593 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
139ccd3f 3594 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
357555c0 3595 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
139ccd3f
JB
3596 temp |= snb_b_fdi_train_param[j/2];
3597 temp |= FDI_COMPOSITE_SYNC;
3598 I915_WRITE(reg, temp | FDI_TX_ENABLE);
357555c0 3599
139ccd3f
JB
3600 I915_WRITE(FDI_RX_MISC(pipe),
3601 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
357555c0 3602
139ccd3f 3603 reg = FDI_RX_CTL(pipe);
357555c0 3604 temp = I915_READ(reg);
139ccd3f
JB
3605 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3606 temp |= FDI_COMPOSITE_SYNC;
3607 I915_WRITE(reg, temp | FDI_RX_ENABLE);
357555c0 3608
139ccd3f
JB
3609 POSTING_READ(reg);
3610 udelay(1); /* should be 0.5us */
357555c0 3611
139ccd3f
JB
3612 for (i = 0; i < 4; i++) {
3613 reg = FDI_RX_IIR(pipe);
3614 temp = I915_READ(reg);
3615 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
357555c0 3616
139ccd3f
JB
3617 if (temp & FDI_RX_BIT_LOCK ||
3618 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
3619 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3620 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
3621 i);
3622 break;
3623 }
3624 udelay(1); /* should be 0.5us */
3625 }
3626 if (i == 4) {
3627 DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
3628 continue;
3629 }
357555c0 3630
139ccd3f 3631 /* Train 2 */
357555c0
JB
3632 reg = FDI_TX_CTL(pipe);
3633 temp = I915_READ(reg);
139ccd3f
JB
3634 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3635 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
3636 I915_WRITE(reg, temp);
3637
3638 reg = FDI_RX_CTL(pipe);
3639 temp = I915_READ(reg);
3640 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3641 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
357555c0
JB
3642 I915_WRITE(reg, temp);
3643
3644 POSTING_READ(reg);
139ccd3f 3645 udelay(2); /* should be 1.5us */
357555c0 3646
139ccd3f
JB
3647 for (i = 0; i < 4; i++) {
3648 reg = FDI_RX_IIR(pipe);
3649 temp = I915_READ(reg);
3650 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
357555c0 3651
139ccd3f
JB
3652 if (temp & FDI_RX_SYMBOL_LOCK ||
3653 (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
3654 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3655 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
3656 i);
3657 goto train_done;
3658 }
3659 udelay(2); /* should be 1.5us */
357555c0 3660 }
139ccd3f
JB
3661 if (i == 4)
3662 DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
357555c0 3663 }
357555c0 3664
139ccd3f 3665train_done:
357555c0
JB
3666 DRM_DEBUG_KMS("FDI train done.\n");
3667}
3668
88cefb6c 3669static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
2c07245f 3670{
88cefb6c 3671 struct drm_device *dev = intel_crtc->base.dev;
2c07245f 3672 struct drm_i915_private *dev_priv = dev->dev_private;
2c07245f 3673 int pipe = intel_crtc->pipe;
f0f59a00
VS
3674 i915_reg_t reg;
3675 u32 temp;
c64e311e 3676
c98e9dcf 3677 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
5eddb70b
CW
3678 reg = FDI_RX_CTL(pipe);
3679 temp = I915_READ(reg);
627eb5a3 3680 temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
6e3c9717 3681 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
dfd07d72 3682 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
5eddb70b
CW
3683 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
3684
3685 POSTING_READ(reg);
c98e9dcf
JB
3686 udelay(200);
3687
3688 /* Switch from Rawclk to PCDclk */
5eddb70b
CW
3689 temp = I915_READ(reg);
3690 I915_WRITE(reg, temp | FDI_PCDCLK);
3691
3692 POSTING_READ(reg);
c98e9dcf
JB
3693 udelay(200);
3694
20749730
PZ
3695 /* Enable CPU FDI TX PLL, always on for Ironlake */
3696 reg = FDI_TX_CTL(pipe);
3697 temp = I915_READ(reg);
3698 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
3699 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
5eddb70b 3700
20749730
PZ
3701 POSTING_READ(reg);
3702 udelay(100);
6be4a607 3703 }
0e23b99d
JB
3704}
3705
88cefb6c
DV
3706static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
3707{
3708 struct drm_device *dev = intel_crtc->base.dev;
3709 struct drm_i915_private *dev_priv = dev->dev_private;
3710 int pipe = intel_crtc->pipe;
f0f59a00
VS
3711 i915_reg_t reg;
3712 u32 temp;
88cefb6c
DV
3713
3714 /* Switch from PCDclk to Rawclk */
3715 reg = FDI_RX_CTL(pipe);
3716 temp = I915_READ(reg);
3717 I915_WRITE(reg, temp & ~FDI_PCDCLK);
3718
3719 /* Disable CPU FDI TX PLL */
3720 reg = FDI_TX_CTL(pipe);
3721 temp = I915_READ(reg);
3722 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
3723
3724 POSTING_READ(reg);
3725 udelay(100);
3726
3727 reg = FDI_RX_CTL(pipe);
3728 temp = I915_READ(reg);
3729 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
3730
3731 /* Wait for the clocks to turn off. */
3732 POSTING_READ(reg);
3733 udelay(100);
3734}
3735
0fc932b8
JB
3736static void ironlake_fdi_disable(struct drm_crtc *crtc)
3737{
3738 struct drm_device *dev = crtc->dev;
3739 struct drm_i915_private *dev_priv = dev->dev_private;
3740 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3741 int pipe = intel_crtc->pipe;
f0f59a00
VS
3742 i915_reg_t reg;
3743 u32 temp;
0fc932b8
JB
3744
3745 /* disable CPU FDI tx and PCH FDI rx */
3746 reg = FDI_TX_CTL(pipe);
3747 temp = I915_READ(reg);
3748 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
3749 POSTING_READ(reg);
3750
3751 reg = FDI_RX_CTL(pipe);
3752 temp = I915_READ(reg);
3753 temp &= ~(0x7 << 16);
dfd07d72 3754 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
0fc932b8
JB
3755 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
3756
3757 POSTING_READ(reg);
3758 udelay(100);
3759
3760 /* Ironlake workaround, disable clock pointer after downing FDI */
eba905b2 3761 if (HAS_PCH_IBX(dev))
6f06ce18 3762 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
0fc932b8
JB
3763
3764 /* still set train pattern 1 */
3765 reg = FDI_TX_CTL(pipe);
3766 temp = I915_READ(reg);
3767 temp &= ~FDI_LINK_TRAIN_NONE;
3768 temp |= FDI_LINK_TRAIN_PATTERN_1;
3769 I915_WRITE(reg, temp);
3770
3771 reg = FDI_RX_CTL(pipe);
3772 temp = I915_READ(reg);
3773 if (HAS_PCH_CPT(dev)) {
3774 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3775 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3776 } else {
3777 temp &= ~FDI_LINK_TRAIN_NONE;
3778 temp |= FDI_LINK_TRAIN_PATTERN_1;
3779 }
3780 /* BPC in FDI rx is consistent with that in PIPECONF */
3781 temp &= ~(0x07 << 16);
dfd07d72 3782 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
0fc932b8
JB
3783 I915_WRITE(reg, temp);
3784
3785 POSTING_READ(reg);
3786 udelay(100);
3787}
3788
5dce5b93
CW
3789bool intel_has_pending_fb_unpin(struct drm_device *dev)
3790{
3791 struct intel_crtc *crtc;
3792
3793 /* Note that we don't need to be called with mode_config.lock here
3794 * as our list of CRTC objects is static for the lifetime of the
3795 * device and so cannot disappear as we iterate. Similarly, we can
3796 * happily treat the predicates as racy, atomic checks as userspace
3797 * cannot claim and pin a new fb without at least acquring the
3798 * struct_mutex and so serialising with us.
3799 */
d3fcc808 3800 for_each_intel_crtc(dev, crtc) {
5dce5b93
CW
3801 if (atomic_read(&crtc->unpin_work_count) == 0)
3802 continue;
3803
5a21b665 3804 if (crtc->flip_work)
5dce5b93
CW
3805 intel_wait_for_vblank(dev, crtc->pipe);
3806
3807 return true;
3808 }
3809
3810 return false;
3811}
3812
5a21b665 3813static void page_flip_completed(struct intel_crtc *intel_crtc)
d6bbafa1
CW
3814{
3815 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
5a21b665
DV
3816 struct intel_flip_work *work = intel_crtc->flip_work;
3817
3818 intel_crtc->flip_work = NULL;
d6bbafa1
CW
3819
3820 if (work->event)
560ce1dc 3821 drm_crtc_send_vblank_event(&intel_crtc->base, work->event);
d6bbafa1
CW
3822
3823 drm_crtc_vblank_put(&intel_crtc->base);
3824
5a21b665 3825 wake_up_all(&dev_priv->pending_flip_queue);
143f73b3 3826 queue_work(dev_priv->wq, &work->unpin_work);
5a21b665
DV
3827
3828 trace_i915_flip_complete(intel_crtc->plane,
3829 work->pending_flip_obj);
d6bbafa1
CW
3830}
3831
5008e874 3832static int intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
e6c3a2a6 3833{
0f91128d 3834 struct drm_device *dev = crtc->dev;
5bb61643 3835 struct drm_i915_private *dev_priv = dev->dev_private;
5008e874 3836 long ret;
e6c3a2a6 3837
2c10d571 3838 WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
5008e874
ML
3839
3840 ret = wait_event_interruptible_timeout(
3841 dev_priv->pending_flip_queue,
3842 !intel_crtc_has_pending_flip(crtc),
3843 60*HZ);
3844
3845 if (ret < 0)
3846 return ret;
3847
5a21b665
DV
3848 if (ret == 0) {
3849 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3850 struct intel_flip_work *work;
3851
3852 spin_lock_irq(&dev->event_lock);
3853 work = intel_crtc->flip_work;
3854 if (work && !is_mmio_work(work)) {
3855 WARN_ONCE(1, "Removing stuck page flip\n");
3856 page_flip_completed(intel_crtc);
3857 }
3858 spin_unlock_irq(&dev->event_lock);
3859 }
5bb61643 3860
5008e874 3861 return 0;
e6c3a2a6
CW
3862}
3863
060f02d8
VS
3864static void lpt_disable_iclkip(struct drm_i915_private *dev_priv)
3865{
3866 u32 temp;
3867
3868 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
3869
3870 mutex_lock(&dev_priv->sb_lock);
3871
3872 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
3873 temp |= SBI_SSCCTL_DISABLE;
3874 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
3875
3876 mutex_unlock(&dev_priv->sb_lock);
3877}
3878
e615efe4
ED
3879/* Program iCLKIP clock to the desired frequency */
3880static void lpt_program_iclkip(struct drm_crtc *crtc)
3881{
64b46a06 3882 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
6e3c9717 3883 int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
e615efe4
ED
3884 u32 divsel, phaseinc, auxdiv, phasedir = 0;
3885 u32 temp;
3886
060f02d8 3887 lpt_disable_iclkip(dev_priv);
e615efe4 3888
64b46a06
VS
3889 /* The iCLK virtual clock root frequency is in MHz,
3890 * but the adjusted_mode->crtc_clock in in KHz. To get the
3891 * divisors, it is necessary to divide one by another, so we
3892 * convert the virtual clock precision to KHz here for higher
3893 * precision.
3894 */
3895 for (auxdiv = 0; auxdiv < 2; auxdiv++) {
e615efe4
ED
3896 u32 iclk_virtual_root_freq = 172800 * 1000;
3897 u32 iclk_pi_range = 64;
64b46a06 3898 u32 desired_divisor;
e615efe4 3899
64b46a06
VS
3900 desired_divisor = DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
3901 clock << auxdiv);
3902 divsel = (desired_divisor / iclk_pi_range) - 2;
3903 phaseinc = desired_divisor % iclk_pi_range;
e615efe4 3904
64b46a06
VS
3905 /*
3906 * Near 20MHz is a corner case which is
3907 * out of range for the 7-bit divisor
3908 */
3909 if (divsel <= 0x7f)
3910 break;
e615efe4
ED
3911 }
3912
3913 /* This should not happen with any sane values */
3914 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
3915 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
3916 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
3917 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
3918
3919 DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
12d7ceed 3920 clock,
e615efe4
ED
3921 auxdiv,
3922 divsel,
3923 phasedir,
3924 phaseinc);
3925
060f02d8
VS
3926 mutex_lock(&dev_priv->sb_lock);
3927
e615efe4 3928 /* Program SSCDIVINTPHASE6 */
988d6ee8 3929 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
e615efe4
ED
3930 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
3931 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
3932 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
3933 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
3934 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
3935 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
988d6ee8 3936 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
e615efe4
ED
3937
3938 /* Program SSCAUXDIV */
988d6ee8 3939 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
e615efe4
ED
3940 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
3941 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
988d6ee8 3942 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
e615efe4
ED
3943
3944 /* Enable modulator and associated divider */
988d6ee8 3945 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
e615efe4 3946 temp &= ~SBI_SSCCTL_DISABLE;
988d6ee8 3947 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
e615efe4 3948
060f02d8
VS
3949 mutex_unlock(&dev_priv->sb_lock);
3950
e615efe4
ED
3951 /* Wait for initialization time */
3952 udelay(24);
3953
3954 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
3955}
3956
8802e5b6
VS
3957int lpt_get_iclkip(struct drm_i915_private *dev_priv)
3958{
3959 u32 divsel, phaseinc, auxdiv;
3960 u32 iclk_virtual_root_freq = 172800 * 1000;
3961 u32 iclk_pi_range = 64;
3962 u32 desired_divisor;
3963 u32 temp;
3964
3965 if ((I915_READ(PIXCLK_GATE) & PIXCLK_GATE_UNGATE) == 0)
3966 return 0;
3967
3968 mutex_lock(&dev_priv->sb_lock);
3969
3970 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
3971 if (temp & SBI_SSCCTL_DISABLE) {
3972 mutex_unlock(&dev_priv->sb_lock);
3973 return 0;
3974 }
3975
3976 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
3977 divsel = (temp & SBI_SSCDIVINTPHASE_DIVSEL_MASK) >>
3978 SBI_SSCDIVINTPHASE_DIVSEL_SHIFT;
3979 phaseinc = (temp & SBI_SSCDIVINTPHASE_INCVAL_MASK) >>
3980 SBI_SSCDIVINTPHASE_INCVAL_SHIFT;
3981
3982 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
3983 auxdiv = (temp & SBI_SSCAUXDIV_FINALDIV2SEL_MASK) >>
3984 SBI_SSCAUXDIV_FINALDIV2SEL_SHIFT;
3985
3986 mutex_unlock(&dev_priv->sb_lock);
3987
3988 desired_divisor = (divsel + 2) * iclk_pi_range + phaseinc;
3989
3990 return DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
3991 desired_divisor << auxdiv);
3992}
3993
275f01b2
DV
3994static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
3995 enum pipe pch_transcoder)
3996{
3997 struct drm_device *dev = crtc->base.dev;
3998 struct drm_i915_private *dev_priv = dev->dev_private;
6e3c9717 3999 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
275f01b2
DV
4000
4001 I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
4002 I915_READ(HTOTAL(cpu_transcoder)));
4003 I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
4004 I915_READ(HBLANK(cpu_transcoder)));
4005 I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
4006 I915_READ(HSYNC(cpu_transcoder)));
4007
4008 I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
4009 I915_READ(VTOTAL(cpu_transcoder)));
4010 I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
4011 I915_READ(VBLANK(cpu_transcoder)));
4012 I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
4013 I915_READ(VSYNC(cpu_transcoder)));
4014 I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
4015 I915_READ(VSYNCSHIFT(cpu_transcoder)));
4016}
4017
003632d9 4018static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
1fbc0d78
DV
4019{
4020 struct drm_i915_private *dev_priv = dev->dev_private;
4021 uint32_t temp;
4022
4023 temp = I915_READ(SOUTH_CHICKEN1);
003632d9 4024 if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
1fbc0d78
DV
4025 return;
4026
4027 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
4028 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
4029
003632d9
ACO
4030 temp &= ~FDI_BC_BIFURCATION_SELECT;
4031 if (enable)
4032 temp |= FDI_BC_BIFURCATION_SELECT;
4033
4034 DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
1fbc0d78
DV
4035 I915_WRITE(SOUTH_CHICKEN1, temp);
4036 POSTING_READ(SOUTH_CHICKEN1);
4037}
4038
4039static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
4040{
4041 struct drm_device *dev = intel_crtc->base.dev;
1fbc0d78
DV
4042
4043 switch (intel_crtc->pipe) {
4044 case PIPE_A:
4045 break;
4046 case PIPE_B:
6e3c9717 4047 if (intel_crtc->config->fdi_lanes > 2)
003632d9 4048 cpt_set_fdi_bc_bifurcation(dev, false);
1fbc0d78 4049 else
003632d9 4050 cpt_set_fdi_bc_bifurcation(dev, true);
1fbc0d78
DV
4051
4052 break;
4053 case PIPE_C:
003632d9 4054 cpt_set_fdi_bc_bifurcation(dev, true);
1fbc0d78
DV
4055
4056 break;
4057 default:
4058 BUG();
4059 }
4060}
4061
c48b5305
VS
4062/* Return which DP Port should be selected for Transcoder DP control */
4063static enum port
4064intel_trans_dp_port_sel(struct drm_crtc *crtc)
4065{
4066 struct drm_device *dev = crtc->dev;
4067 struct intel_encoder *encoder;
4068
4069 for_each_encoder_on_crtc(dev, crtc, encoder) {
4070 if (encoder->type == INTEL_OUTPUT_DISPLAYPORT ||
4071 encoder->type == INTEL_OUTPUT_EDP)
4072 return enc_to_dig_port(&encoder->base)->port;
4073 }
4074
4075 return -1;
4076}
4077
f67a559d
JB
4078/*
4079 * Enable PCH resources required for PCH ports:
4080 * - PCH PLLs
4081 * - FDI training & RX/TX
4082 * - update transcoder timings
4083 * - DP transcoding bits
4084 * - transcoder
4085 */
4086static void ironlake_pch_enable(struct drm_crtc *crtc)
0e23b99d
JB
4087{
4088 struct drm_device *dev = crtc->dev;
4089 struct drm_i915_private *dev_priv = dev->dev_private;
4090 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4091 int pipe = intel_crtc->pipe;
f0f59a00 4092 u32 temp;
2c07245f 4093
ab9412ba 4094 assert_pch_transcoder_disabled(dev_priv, pipe);
e7e164db 4095
1fbc0d78
DV
4096 if (IS_IVYBRIDGE(dev))
4097 ivybridge_update_fdi_bc_bifurcation(intel_crtc);
4098
cd986abb
DV
4099 /* Write the TU size bits before fdi link training, so that error
4100 * detection works. */
4101 I915_WRITE(FDI_RX_TUSIZE1(pipe),
4102 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
4103
c98e9dcf 4104 /* For PCH output, training FDI link */
674cf967 4105 dev_priv->display.fdi_link_train(crtc);
2c07245f 4106
3ad8a208
DV
4107 /* We need to program the right clock selection before writing the pixel
4108 * mutliplier into the DPLL. */
303b81e0 4109 if (HAS_PCH_CPT(dev)) {
ee7b9f93 4110 u32 sel;
4b645f14 4111
c98e9dcf 4112 temp = I915_READ(PCH_DPLL_SEL);
11887397
DV
4113 temp |= TRANS_DPLL_ENABLE(pipe);
4114 sel = TRANS_DPLLB_SEL(pipe);
8106ddbd
ACO
4115 if (intel_crtc->config->shared_dpll ==
4116 intel_get_shared_dpll_by_id(dev_priv, DPLL_ID_PCH_PLL_B))
ee7b9f93
JB
4117 temp |= sel;
4118 else
4119 temp &= ~sel;
c98e9dcf 4120 I915_WRITE(PCH_DPLL_SEL, temp);
c98e9dcf 4121 }
5eddb70b 4122
3ad8a208
DV
4123 /* XXX: pch pll's can be enabled any time before we enable the PCH
4124 * transcoder, and we actually should do this to not upset any PCH
4125 * transcoder that already use the clock when we share it.
4126 *
4127 * Note that enable_shared_dpll tries to do the right thing, but
4128 * get_shared_dpll unconditionally resets the pll - we need that to have
4129 * the right LVDS enable sequence. */
85b3894f 4130 intel_enable_shared_dpll(intel_crtc);
3ad8a208 4131
d9b6cb56
JB
4132 /* set transcoder timing, panel must allow it */
4133 assert_panel_unlocked(dev_priv, pipe);
275f01b2 4134 ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
8db9d77b 4135
303b81e0 4136 intel_fdi_normal_train(crtc);
5e84e1a4 4137
c98e9dcf 4138 /* For PCH DP, enable TRANS_DP_CTL */
6e3c9717 4139 if (HAS_PCH_CPT(dev) && intel_crtc->config->has_dp_encoder) {
9c4edaee
VS
4140 const struct drm_display_mode *adjusted_mode =
4141 &intel_crtc->config->base.adjusted_mode;
dfd07d72 4142 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
f0f59a00 4143 i915_reg_t reg = TRANS_DP_CTL(pipe);
5eddb70b
CW
4144 temp = I915_READ(reg);
4145 temp &= ~(TRANS_DP_PORT_SEL_MASK |
220cad3c
EA
4146 TRANS_DP_SYNC_MASK |
4147 TRANS_DP_BPC_MASK);
e3ef4479 4148 temp |= TRANS_DP_OUTPUT_ENABLE;
9325c9f0 4149 temp |= bpc << 9; /* same format but at 11:9 */
c98e9dcf 4150
9c4edaee 4151 if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
5eddb70b 4152 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
9c4edaee 4153 if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
5eddb70b 4154 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
c98e9dcf
JB
4155
4156 switch (intel_trans_dp_port_sel(crtc)) {
c48b5305 4157 case PORT_B:
5eddb70b 4158 temp |= TRANS_DP_PORT_SEL_B;
c98e9dcf 4159 break;
c48b5305 4160 case PORT_C:
5eddb70b 4161 temp |= TRANS_DP_PORT_SEL_C;
c98e9dcf 4162 break;
c48b5305 4163 case PORT_D:
5eddb70b 4164 temp |= TRANS_DP_PORT_SEL_D;
c98e9dcf
JB
4165 break;
4166 default:
e95d41e1 4167 BUG();
32f9d658 4168 }
2c07245f 4169
5eddb70b 4170 I915_WRITE(reg, temp);
6be4a607 4171 }
b52eb4dc 4172
b8a4f404 4173 ironlake_enable_pch_transcoder(dev_priv, pipe);
f67a559d
JB
4174}
4175
1507e5bd
PZ
4176static void lpt_pch_enable(struct drm_crtc *crtc)
4177{
4178 struct drm_device *dev = crtc->dev;
4179 struct drm_i915_private *dev_priv = dev->dev_private;
4180 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6e3c9717 4181 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
1507e5bd 4182
ab9412ba 4183 assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
1507e5bd 4184
8c52b5e8 4185 lpt_program_iclkip(crtc);
1507e5bd 4186
0540e488 4187 /* Set transcoder timing. */
275f01b2 4188 ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
1507e5bd 4189
937bb610 4190 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
f67a559d
JB
4191}
4192
a1520318 4193static void cpt_verify_modeset(struct drm_device *dev, int pipe)
d4270e57
JB
4194{
4195 struct drm_i915_private *dev_priv = dev->dev_private;
f0f59a00 4196 i915_reg_t dslreg = PIPEDSL(pipe);
d4270e57
JB
4197 u32 temp;
4198
4199 temp = I915_READ(dslreg);
4200 udelay(500);
4201 if (wait_for(I915_READ(dslreg) != temp, 5)) {
d4270e57 4202 if (wait_for(I915_READ(dslreg) != temp, 5))
84f44ce7 4203 DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
d4270e57
JB
4204 }
4205}
4206
86adf9d7
ML
4207static int
4208skl_update_scaler(struct intel_crtc_state *crtc_state, bool force_detach,
4209 unsigned scaler_user, int *scaler_id, unsigned int rotation,
4210 int src_w, int src_h, int dst_w, int dst_h)
a1b2278e 4211{
86adf9d7
ML
4212 struct intel_crtc_scaler_state *scaler_state =
4213 &crtc_state->scaler_state;
4214 struct intel_crtc *intel_crtc =
4215 to_intel_crtc(crtc_state->base.crtc);
a1b2278e 4216 int need_scaling;
6156a456
CK
4217
4218 need_scaling = intel_rotation_90_or_270(rotation) ?
4219 (src_h != dst_w || src_w != dst_h):
4220 (src_w != dst_w || src_h != dst_h);
a1b2278e
CK
4221
4222 /*
4223 * if plane is being disabled or scaler is no more required or force detach
4224 * - free scaler binded to this plane/crtc
4225 * - in order to do this, update crtc->scaler_usage
4226 *
4227 * Here scaler state in crtc_state is set free so that
4228 * scaler can be assigned to other user. Actual register
4229 * update to free the scaler is done in plane/panel-fit programming.
4230 * For this purpose crtc/plane_state->scaler_id isn't reset here.
4231 */
86adf9d7 4232 if (force_detach || !need_scaling) {
a1b2278e 4233 if (*scaler_id >= 0) {
86adf9d7 4234 scaler_state->scaler_users &= ~(1 << scaler_user);
a1b2278e
CK
4235 scaler_state->scalers[*scaler_id].in_use = 0;
4236
86adf9d7
ML
4237 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4238 "Staged freeing scaler id %d scaler_users = 0x%x\n",
4239 intel_crtc->pipe, scaler_user, *scaler_id,
a1b2278e
CK
4240 scaler_state->scaler_users);
4241 *scaler_id = -1;
4242 }
4243 return 0;
4244 }
4245
4246 /* range checks */
4247 if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
4248 dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
4249
4250 src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
4251 dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
86adf9d7 4252 DRM_DEBUG_KMS("scaler_user index %u.%u: src %ux%u dst %ux%u "
a1b2278e 4253 "size is out of scaler range\n",
86adf9d7 4254 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h);
a1b2278e
CK
4255 return -EINVAL;
4256 }
4257
86adf9d7
ML
4258 /* mark this plane as a scaler user in crtc_state */
4259 scaler_state->scaler_users |= (1 << scaler_user);
4260 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4261 "staged scaling request for %ux%u->%ux%u scaler_users = 0x%x\n",
4262 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h,
4263 scaler_state->scaler_users);
4264
4265 return 0;
4266}
4267
4268/**
4269 * skl_update_scaler_crtc - Stages update to scaler state for a given crtc.
4270 *
4271 * @state: crtc's scaler state
86adf9d7
ML
4272 *
4273 * Return
4274 * 0 - scaler_usage updated successfully
4275 * error - requested scaling cannot be supported or other error condition
4276 */
e435d6e5 4277int skl_update_scaler_crtc(struct intel_crtc_state *state)
86adf9d7
ML
4278{
4279 struct intel_crtc *intel_crtc = to_intel_crtc(state->base.crtc);
7c5f93b0 4280 const struct drm_display_mode *adjusted_mode = &state->base.adjusted_mode;
86adf9d7 4281
78108b7c
VS
4282 DRM_DEBUG_KMS("Updating scaler for [CRTC:%d:%s] scaler_user index %u.%u\n",
4283 intel_crtc->base.base.id, intel_crtc->base.name,
4284 intel_crtc->pipe, SKL_CRTC_INDEX);
86adf9d7 4285
e435d6e5 4286 return skl_update_scaler(state, !state->base.active, SKL_CRTC_INDEX,
fa5a7970 4287 &state->scaler_state.scaler_id, BIT(DRM_ROTATE_0),
86adf9d7 4288 state->pipe_src_w, state->pipe_src_h,
aad941d5 4289 adjusted_mode->crtc_hdisplay, adjusted_mode->crtc_vdisplay);
86adf9d7
ML
4290}
4291
4292/**
4293 * skl_update_scaler_plane - Stages update to scaler state for a given plane.
4294 *
4295 * @state: crtc's scaler state
86adf9d7
ML
4296 * @plane_state: atomic plane state to update
4297 *
4298 * Return
4299 * 0 - scaler_usage updated successfully
4300 * error - requested scaling cannot be supported or other error condition
4301 */
da20eabd
ML
4302static int skl_update_scaler_plane(struct intel_crtc_state *crtc_state,
4303 struct intel_plane_state *plane_state)
86adf9d7
ML
4304{
4305
4306 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
da20eabd
ML
4307 struct intel_plane *intel_plane =
4308 to_intel_plane(plane_state->base.plane);
86adf9d7
ML
4309 struct drm_framebuffer *fb = plane_state->base.fb;
4310 int ret;
4311
4312 bool force_detach = !fb || !plane_state->visible;
4313
72660ce0
VS
4314 DRM_DEBUG_KMS("Updating scaler for [PLANE:%d:%s] scaler_user index %u.%u\n",
4315 intel_plane->base.base.id, intel_plane->base.name,
4316 intel_crtc->pipe, drm_plane_index(&intel_plane->base));
86adf9d7
ML
4317
4318 ret = skl_update_scaler(crtc_state, force_detach,
4319 drm_plane_index(&intel_plane->base),
4320 &plane_state->scaler_id,
4321 plane_state->base.rotation,
4322 drm_rect_width(&plane_state->src) >> 16,
4323 drm_rect_height(&plane_state->src) >> 16,
4324 drm_rect_width(&plane_state->dst),
4325 drm_rect_height(&plane_state->dst));
4326
4327 if (ret || plane_state->scaler_id < 0)
4328 return ret;
4329
a1b2278e 4330 /* check colorkey */
818ed961 4331 if (plane_state->ckey.flags != I915_SET_COLORKEY_NONE) {
72660ce0
VS
4332 DRM_DEBUG_KMS("[PLANE:%d:%s] scaling with color key not allowed",
4333 intel_plane->base.base.id,
4334 intel_plane->base.name);
a1b2278e
CK
4335 return -EINVAL;
4336 }
4337
4338 /* Check src format */
86adf9d7
ML
4339 switch (fb->pixel_format) {
4340 case DRM_FORMAT_RGB565:
4341 case DRM_FORMAT_XBGR8888:
4342 case DRM_FORMAT_XRGB8888:
4343 case DRM_FORMAT_ABGR8888:
4344 case DRM_FORMAT_ARGB8888:
4345 case DRM_FORMAT_XRGB2101010:
4346 case DRM_FORMAT_XBGR2101010:
4347 case DRM_FORMAT_YUYV:
4348 case DRM_FORMAT_YVYU:
4349 case DRM_FORMAT_UYVY:
4350 case DRM_FORMAT_VYUY:
4351 break;
4352 default:
72660ce0
VS
4353 DRM_DEBUG_KMS("[PLANE:%d:%s] FB:%d unsupported scaling format 0x%x\n",
4354 intel_plane->base.base.id, intel_plane->base.name,
4355 fb->base.id, fb->pixel_format);
86adf9d7 4356 return -EINVAL;
a1b2278e
CK
4357 }
4358
a1b2278e
CK
4359 return 0;
4360}
4361
e435d6e5
ML
4362static void skylake_scaler_disable(struct intel_crtc *crtc)
4363{
4364 int i;
4365
4366 for (i = 0; i < crtc->num_scalers; i++)
4367 skl_detach_scaler(crtc, i);
4368}
4369
4370static void skylake_pfit_enable(struct intel_crtc *crtc)
bd2e244f
JB
4371{
4372 struct drm_device *dev = crtc->base.dev;
4373 struct drm_i915_private *dev_priv = dev->dev_private;
4374 int pipe = crtc->pipe;
a1b2278e
CK
4375 struct intel_crtc_scaler_state *scaler_state =
4376 &crtc->config->scaler_state;
4377
4378 DRM_DEBUG_KMS("for crtc_state = %p\n", crtc->config);
4379
6e3c9717 4380 if (crtc->config->pch_pfit.enabled) {
a1b2278e
CK
4381 int id;
4382
4383 if (WARN_ON(crtc->config->scaler_state.scaler_id < 0)) {
4384 DRM_ERROR("Requesting pfit without getting a scaler first\n");
4385 return;
4386 }
4387
4388 id = scaler_state->scaler_id;
4389 I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
4390 PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
4391 I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
4392 I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
4393
4394 DRM_DEBUG_KMS("for crtc_state = %p scaler_id = %d\n", crtc->config, id);
bd2e244f
JB
4395 }
4396}
4397
b074cec8
JB
4398static void ironlake_pfit_enable(struct intel_crtc *crtc)
4399{
4400 struct drm_device *dev = crtc->base.dev;
4401 struct drm_i915_private *dev_priv = dev->dev_private;
4402 int pipe = crtc->pipe;
4403
6e3c9717 4404 if (crtc->config->pch_pfit.enabled) {
b074cec8
JB
4405 /* Force use of hard-coded filter coefficients
4406 * as some pre-programmed values are broken,
4407 * e.g. x201.
4408 */
4409 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
4410 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
4411 PF_PIPE_SEL_IVB(pipe));
4412 else
4413 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
6e3c9717
ACO
4414 I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
4415 I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
d4270e57
JB
4416 }
4417}
4418
20bc8673 4419void hsw_enable_ips(struct intel_crtc *crtc)
d77e4531 4420{
cea165c3
VS
4421 struct drm_device *dev = crtc->base.dev;
4422 struct drm_i915_private *dev_priv = dev->dev_private;
d77e4531 4423
6e3c9717 4424 if (!crtc->config->ips_enabled)
d77e4531
PZ
4425 return;
4426
307e4498
ML
4427 /*
4428 * We can only enable IPS after we enable a plane and wait for a vblank
4429 * This function is called from post_plane_update, which is run after
4430 * a vblank wait.
4431 */
cea165c3 4432
d77e4531 4433 assert_plane_enabled(dev_priv, crtc->plane);
cea165c3 4434 if (IS_BROADWELL(dev)) {
2a114cc1
BW
4435 mutex_lock(&dev_priv->rps.hw_lock);
4436 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
4437 mutex_unlock(&dev_priv->rps.hw_lock);
4438 /* Quoting Art Runyan: "its not safe to expect any particular
4439 * value in IPS_CTL bit 31 after enabling IPS through the
e59150dc
JB
4440 * mailbox." Moreover, the mailbox may return a bogus state,
4441 * so we need to just enable it and continue on.
2a114cc1
BW
4442 */
4443 } else {
4444 I915_WRITE(IPS_CTL, IPS_ENABLE);
4445 /* The bit only becomes 1 in the next vblank, so this wait here
4446 * is essentially intel_wait_for_vblank. If we don't have this
4447 * and don't wait for vblanks until the end of crtc_enable, then
4448 * the HW state readout code will complain that the expected
4449 * IPS_CTL value is not the one we read. */
4450 if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
4451 DRM_ERROR("Timed out waiting for IPS enable\n");
4452 }
d77e4531
PZ
4453}
4454
20bc8673 4455void hsw_disable_ips(struct intel_crtc *crtc)
d77e4531
PZ
4456{
4457 struct drm_device *dev = crtc->base.dev;
4458 struct drm_i915_private *dev_priv = dev->dev_private;
4459
6e3c9717 4460 if (!crtc->config->ips_enabled)
d77e4531
PZ
4461 return;
4462
4463 assert_plane_enabled(dev_priv, crtc->plane);
23d0b130 4464 if (IS_BROADWELL(dev)) {
2a114cc1
BW
4465 mutex_lock(&dev_priv->rps.hw_lock);
4466 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
4467 mutex_unlock(&dev_priv->rps.hw_lock);
23d0b130
BW
4468 /* wait for pcode to finish disabling IPS, which may take up to 42ms */
4469 if (wait_for((I915_READ(IPS_CTL) & IPS_ENABLE) == 0, 42))
4470 DRM_ERROR("Timed out waiting for IPS disable\n");
e59150dc 4471 } else {
2a114cc1 4472 I915_WRITE(IPS_CTL, 0);
e59150dc
JB
4473 POSTING_READ(IPS_CTL);
4474 }
d77e4531
PZ
4475
4476 /* We need to wait for a vblank before we can disable the plane. */
4477 intel_wait_for_vblank(dev, crtc->pipe);
4478}
4479
7cac945f 4480static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
d3eedb1a 4481{
7cac945f 4482 if (intel_crtc->overlay) {
d3eedb1a
VS
4483 struct drm_device *dev = intel_crtc->base.dev;
4484 struct drm_i915_private *dev_priv = dev->dev_private;
4485
4486 mutex_lock(&dev->struct_mutex);
4487 dev_priv->mm.interruptible = false;
4488 (void) intel_overlay_switch_off(intel_crtc->overlay);
4489 dev_priv->mm.interruptible = true;
4490 mutex_unlock(&dev->struct_mutex);
4491 }
4492
4493 /* Let userspace switch the overlay on again. In most cases userspace
4494 * has to recompute where to put it anyway.
4495 */
4496}
4497
87d4300a
ML
4498/**
4499 * intel_post_enable_primary - Perform operations after enabling primary plane
4500 * @crtc: the CRTC whose primary plane was just enabled
4501 *
4502 * Performs potentially sleeping operations that must be done after the primary
4503 * plane is enabled, such as updating FBC and IPS. Note that this may be
4504 * called due to an explicit primary plane update, or due to an implicit
4505 * re-enable that is caused when a sprite plane is updated to no longer
4506 * completely hide the primary plane.
4507 */
4508static void
4509intel_post_enable_primary(struct drm_crtc *crtc)
a5c4d7bc
VS
4510{
4511 struct drm_device *dev = crtc->dev;
87d4300a 4512 struct drm_i915_private *dev_priv = dev->dev_private;
a5c4d7bc
VS
4513 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4514 int pipe = intel_crtc->pipe;
a5c4d7bc 4515
87d4300a
ML
4516 /*
4517 * FIXME IPS should be fine as long as one plane is
4518 * enabled, but in practice it seems to have problems
4519 * when going from primary only to sprite only and vice
4520 * versa.
4521 */
a5c4d7bc
VS
4522 hsw_enable_ips(intel_crtc);
4523
f99d7069 4524 /*
87d4300a
ML
4525 * Gen2 reports pipe underruns whenever all planes are disabled.
4526 * So don't enable underrun reporting before at least some planes
4527 * are enabled.
4528 * FIXME: Need to fix the logic to work when we turn off all planes
4529 * but leave the pipe running.
f99d7069 4530 */
87d4300a
ML
4531 if (IS_GEN2(dev))
4532 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4533
aca7b684
VS
4534 /* Underruns don't always raise interrupts, so check manually. */
4535 intel_check_cpu_fifo_underruns(dev_priv);
4536 intel_check_pch_fifo_underruns(dev_priv);
a5c4d7bc
VS
4537}
4538
2622a081 4539/* FIXME move all this to pre_plane_update() with proper state tracking */
87d4300a
ML
4540static void
4541intel_pre_disable_primary(struct drm_crtc *crtc)
a5c4d7bc
VS
4542{
4543 struct drm_device *dev = crtc->dev;
4544 struct drm_i915_private *dev_priv = dev->dev_private;
4545 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4546 int pipe = intel_crtc->pipe;
a5c4d7bc 4547
87d4300a
ML
4548 /*
4549 * Gen2 reports pipe underruns whenever all planes are disabled.
4550 * So diasble underrun reporting before all the planes get disabled.
4551 * FIXME: Need to fix the logic to work when we turn off all planes
4552 * but leave the pipe running.
4553 */
4554 if (IS_GEN2(dev))
4555 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
a5c4d7bc 4556
2622a081
VS
4557 /*
4558 * FIXME IPS should be fine as long as one plane is
4559 * enabled, but in practice it seems to have problems
4560 * when going from primary only to sprite only and vice
4561 * versa.
4562 */
4563 hsw_disable_ips(intel_crtc);
4564}
4565
4566/* FIXME get rid of this and use pre_plane_update */
4567static void
4568intel_pre_disable_primary_noatomic(struct drm_crtc *crtc)
4569{
4570 struct drm_device *dev = crtc->dev;
4571 struct drm_i915_private *dev_priv = dev->dev_private;
4572 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4573 int pipe = intel_crtc->pipe;
4574
4575 intel_pre_disable_primary(crtc);
4576
87d4300a
ML
4577 /*
4578 * Vblank time updates from the shadow to live plane control register
4579 * are blocked if the memory self-refresh mode is active at that
4580 * moment. So to make sure the plane gets truly disabled, disable
4581 * first the self-refresh mode. The self-refresh enable bit in turn
4582 * will be checked/applied by the HW only at the next frame start
4583 * event which is after the vblank start event, so we need to have a
4584 * wait-for-vblank between disabling the plane and the pipe.
4585 */
262cd2e1 4586 if (HAS_GMCH_DISPLAY(dev)) {
87d4300a 4587 intel_set_memory_cxsr(dev_priv, false);
262cd2e1
VS
4588 dev_priv->wm.vlv.cxsr = false;
4589 intel_wait_for_vblank(dev, pipe);
4590 }
87d4300a
ML
4591}
4592
5a21b665
DV
4593static void intel_post_plane_update(struct intel_crtc_state *old_crtc_state)
4594{
4595 struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
4596 struct drm_atomic_state *old_state = old_crtc_state->base.state;
4597 struct intel_crtc_state *pipe_config =
4598 to_intel_crtc_state(crtc->base.state);
4599 struct drm_device *dev = crtc->base.dev;
4600 struct drm_plane *primary = crtc->base.primary;
4601 struct drm_plane_state *old_pri_state =
4602 drm_atomic_get_existing_plane_state(old_state, primary);
4603
4604 intel_frontbuffer_flip(dev, pipe_config->fb_bits);
4605
4606 crtc->wm.cxsr_allowed = true;
4607
4608 if (pipe_config->update_wm_post && pipe_config->base.active)
4609 intel_update_watermarks(&crtc->base);
4610
4611 if (old_pri_state) {
4612 struct intel_plane_state *primary_state =
4613 to_intel_plane_state(primary->state);
4614 struct intel_plane_state *old_primary_state =
4615 to_intel_plane_state(old_pri_state);
4616
4617 intel_fbc_post_update(crtc);
4618
4619 if (primary_state->visible &&
4620 (needs_modeset(&pipe_config->base) ||
4621 !old_primary_state->visible))
4622 intel_post_enable_primary(&crtc->base);
4623 }
4624}
4625
5c74cd73 4626static void intel_pre_plane_update(struct intel_crtc_state *old_crtc_state)
ac21b225 4627{
5c74cd73 4628 struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
ac21b225 4629 struct drm_device *dev = crtc->base.dev;
eddfcbcd 4630 struct drm_i915_private *dev_priv = dev->dev_private;
ab1d3a0e
ML
4631 struct intel_crtc_state *pipe_config =
4632 to_intel_crtc_state(crtc->base.state);
5c74cd73
ML
4633 struct drm_atomic_state *old_state = old_crtc_state->base.state;
4634 struct drm_plane *primary = crtc->base.primary;
4635 struct drm_plane_state *old_pri_state =
4636 drm_atomic_get_existing_plane_state(old_state, primary);
4637 bool modeset = needs_modeset(&pipe_config->base);
ac21b225 4638
5c74cd73
ML
4639 if (old_pri_state) {
4640 struct intel_plane_state *primary_state =
4641 to_intel_plane_state(primary->state);
4642 struct intel_plane_state *old_primary_state =
4643 to_intel_plane_state(old_pri_state);
4644
faf68d92 4645 intel_fbc_pre_update(crtc, pipe_config, primary_state);
31ae71fc 4646
5c74cd73
ML
4647 if (old_primary_state->visible &&
4648 (modeset || !primary_state->visible))
4649 intel_pre_disable_primary(&crtc->base);
4650 }
852eb00d 4651
a4015f9a 4652 if (pipe_config->disable_cxsr && HAS_GMCH_DISPLAY(dev)) {
852eb00d 4653 crtc->wm.cxsr_allowed = false;
2dfd178d 4654
2622a081
VS
4655 /*
4656 * Vblank time updates from the shadow to live plane control register
4657 * are blocked if the memory self-refresh mode is active at that
4658 * moment. So to make sure the plane gets truly disabled, disable
4659 * first the self-refresh mode. The self-refresh enable bit in turn
4660 * will be checked/applied by the HW only at the next frame start
4661 * event which is after the vblank start event, so we need to have a
4662 * wait-for-vblank between disabling the plane and the pipe.
4663 */
4664 if (old_crtc_state->base.active) {
2dfd178d 4665 intel_set_memory_cxsr(dev_priv, false);
2622a081
VS
4666 dev_priv->wm.vlv.cxsr = false;
4667 intel_wait_for_vblank(dev, crtc->pipe);
4668 }
852eb00d 4669 }
92826fcd 4670
ed4a6a7c
MR
4671 /*
4672 * IVB workaround: must disable low power watermarks for at least
4673 * one frame before enabling scaling. LP watermarks can be re-enabled
4674 * when scaling is disabled.
4675 *
4676 * WaCxSRDisabledForSpriteScaling:ivb
4677 */
4678 if (pipe_config->disable_lp_wm) {
4679 ilk_disable_lp_wm(dev);
4680 intel_wait_for_vblank(dev, crtc->pipe);
4681 }
4682
4683 /*
4684 * If we're doing a modeset, we're done. No need to do any pre-vblank
4685 * watermark programming here.
4686 */
4687 if (needs_modeset(&pipe_config->base))
4688 return;
4689
4690 /*
4691 * For platforms that support atomic watermarks, program the
4692 * 'intermediate' watermarks immediately. On pre-gen9 platforms, these
4693 * will be the intermediate values that are safe for both pre- and
4694 * post- vblank; when vblank happens, the 'active' values will be set
4695 * to the final 'target' values and we'll do this again to get the
4696 * optimal watermarks. For gen9+ platforms, the values we program here
4697 * will be the final target values which will get automatically latched
4698 * at vblank time; no further programming will be necessary.
4699 *
4700 * If a platform hasn't been transitioned to atomic watermarks yet,
4701 * we'll continue to update watermarks the old way, if flags tell
4702 * us to.
4703 */
4704 if (dev_priv->display.initial_watermarks != NULL)
4705 dev_priv->display.initial_watermarks(pipe_config);
caed361d 4706 else if (pipe_config->update_wm_pre)
92826fcd 4707 intel_update_watermarks(&crtc->base);
ac21b225
ML
4708}
4709
d032ffa0 4710static void intel_crtc_disable_planes(struct drm_crtc *crtc, unsigned plane_mask)
87d4300a
ML
4711{
4712 struct drm_device *dev = crtc->dev;
4713 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
d032ffa0 4714 struct drm_plane *p;
87d4300a
ML
4715 int pipe = intel_crtc->pipe;
4716
7cac945f 4717 intel_crtc_dpms_overlay_disable(intel_crtc);
27321ae8 4718
d032ffa0
ML
4719 drm_for_each_plane_mask(p, dev, plane_mask)
4720 to_intel_plane(p)->disable_plane(p, crtc);
f98551ae 4721
f99d7069
DV
4722 /*
4723 * FIXME: Once we grow proper nuclear flip support out of this we need
4724 * to compute the mask of flip planes precisely. For the time being
4725 * consider this a flip to a NULL plane.
4726 */
4727 intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
a5c4d7bc
VS
4728}
4729
f67a559d
JB
4730static void ironlake_crtc_enable(struct drm_crtc *crtc)
4731{
4732 struct drm_device *dev = crtc->dev;
4733 struct drm_i915_private *dev_priv = dev->dev_private;
4734 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 4735 struct intel_encoder *encoder;
f67a559d 4736 int pipe = intel_crtc->pipe;
b95c5321
ML
4737 struct intel_crtc_state *pipe_config =
4738 to_intel_crtc_state(crtc->state);
f67a559d 4739
53d9f4e9 4740 if (WARN_ON(intel_crtc->active))
f67a559d
JB
4741 return;
4742
b2c0593a
VS
4743 /*
4744 * Sometimes spurious CPU pipe underruns happen during FDI
4745 * training, at least with VGA+HDMI cloning. Suppress them.
4746 *
4747 * On ILK we get an occasional spurious CPU pipe underruns
4748 * between eDP port A enable and vdd enable. Also PCH port
4749 * enable seems to result in the occasional CPU pipe underrun.
4750 *
4751 * Spurious PCH underruns also occur during PCH enabling.
4752 */
4753 if (intel_crtc->config->has_pch_encoder || IS_GEN5(dev_priv))
4754 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
81b088ca
VS
4755 if (intel_crtc->config->has_pch_encoder)
4756 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
4757
6e3c9717 4758 if (intel_crtc->config->has_pch_encoder)
b14b1055
DV
4759 intel_prepare_shared_dpll(intel_crtc);
4760
6e3c9717 4761 if (intel_crtc->config->has_dp_encoder)
fe3cd48d 4762 intel_dp_set_m_n(intel_crtc, M1_N1);
29407aab
DV
4763
4764 intel_set_pipe_timings(intel_crtc);
bc58be60 4765 intel_set_pipe_src_size(intel_crtc);
29407aab 4766
6e3c9717 4767 if (intel_crtc->config->has_pch_encoder) {
29407aab 4768 intel_cpu_transcoder_set_m_n(intel_crtc,
6e3c9717 4769 &intel_crtc->config->fdi_m_n, NULL);
29407aab
DV
4770 }
4771
4772 ironlake_set_pipeconf(crtc);
4773
f67a559d 4774 intel_crtc->active = true;
8664281b 4775
f6736a1a 4776 for_each_encoder_on_crtc(dev, crtc, encoder)
952735ee
DV
4777 if (encoder->pre_enable)
4778 encoder->pre_enable(encoder);
f67a559d 4779
6e3c9717 4780 if (intel_crtc->config->has_pch_encoder) {
fff367c7
DV
4781 /* Note: FDI PLL enabling _must_ be done before we enable the
4782 * cpu pipes, hence this is separate from all the other fdi/pch
4783 * enabling. */
88cefb6c 4784 ironlake_fdi_pll_enable(intel_crtc);
46b6f814
DV
4785 } else {
4786 assert_fdi_tx_disabled(dev_priv, pipe);
4787 assert_fdi_rx_disabled(dev_priv, pipe);
4788 }
f67a559d 4789
b074cec8 4790 ironlake_pfit_enable(intel_crtc);
f67a559d 4791
9c54c0dd
JB
4792 /*
4793 * On ILK+ LUT must be loaded before the pipe is running but with
4794 * clocks enabled
4795 */
b95c5321 4796 intel_color_load_luts(&pipe_config->base);
9c54c0dd 4797
1d5bf5d9
ID
4798 if (dev_priv->display.initial_watermarks != NULL)
4799 dev_priv->display.initial_watermarks(intel_crtc->config);
e1fdc473 4800 intel_enable_pipe(intel_crtc);
f67a559d 4801
6e3c9717 4802 if (intel_crtc->config->has_pch_encoder)
f67a559d 4803 ironlake_pch_enable(crtc);
c98e9dcf 4804
f9b61ff6
DV
4805 assert_vblank_disabled(crtc);
4806 drm_crtc_vblank_on(crtc);
4807
fa5c73b1
DV
4808 for_each_encoder_on_crtc(dev, crtc, encoder)
4809 encoder->enable(encoder);
61b77ddd
DV
4810
4811 if (HAS_PCH_CPT(dev))
a1520318 4812 cpt_verify_modeset(dev, intel_crtc->pipe);
37ca8d4c
VS
4813
4814 /* Must wait for vblank to avoid spurious PCH FIFO underruns */
4815 if (intel_crtc->config->has_pch_encoder)
4816 intel_wait_for_vblank(dev, pipe);
b2c0593a 4817 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
37ca8d4c 4818 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
6be4a607
JB
4819}
4820
42db64ef
PZ
4821/* IPS only exists on ULT machines and is tied to pipe A. */
4822static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
4823{
f5adf94e 4824 return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
42db64ef
PZ
4825}
4826
4f771f10
PZ
4827static void haswell_crtc_enable(struct drm_crtc *crtc)
4828{
4829 struct drm_device *dev = crtc->dev;
4830 struct drm_i915_private *dev_priv = dev->dev_private;
4831 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4832 struct intel_encoder *encoder;
99d736a2 4833 int pipe = intel_crtc->pipe, hsw_workaround_pipe;
4d1de975 4834 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
99d736a2
ML
4835 struct intel_crtc_state *pipe_config =
4836 to_intel_crtc_state(crtc->state);
4f771f10 4837
53d9f4e9 4838 if (WARN_ON(intel_crtc->active))
4f771f10
PZ
4839 return;
4840
81b088ca
VS
4841 if (intel_crtc->config->has_pch_encoder)
4842 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
4843 false);
4844
95a7a2ae
ID
4845 for_each_encoder_on_crtc(dev, crtc, encoder)
4846 if (encoder->pre_pll_enable)
4847 encoder->pre_pll_enable(encoder);
4848
8106ddbd 4849 if (intel_crtc->config->shared_dpll)
df8ad70c
DV
4850 intel_enable_shared_dpll(intel_crtc);
4851
6e3c9717 4852 if (intel_crtc->config->has_dp_encoder)
fe3cd48d 4853 intel_dp_set_m_n(intel_crtc, M1_N1);
229fca97 4854
4d1de975
JN
4855 if (!intel_crtc->config->has_dsi_encoder)
4856 intel_set_pipe_timings(intel_crtc);
4857
bc58be60 4858 intel_set_pipe_src_size(intel_crtc);
229fca97 4859
4d1de975
JN
4860 if (cpu_transcoder != TRANSCODER_EDP &&
4861 !transcoder_is_dsi(cpu_transcoder)) {
4862 I915_WRITE(PIPE_MULT(cpu_transcoder),
6e3c9717 4863 intel_crtc->config->pixel_multiplier - 1);
ebb69c95
CT
4864 }
4865
6e3c9717 4866 if (intel_crtc->config->has_pch_encoder) {
229fca97 4867 intel_cpu_transcoder_set_m_n(intel_crtc,
6e3c9717 4868 &intel_crtc->config->fdi_m_n, NULL);
229fca97
DV
4869 }
4870
4d1de975
JN
4871 if (!intel_crtc->config->has_dsi_encoder)
4872 haswell_set_pipeconf(crtc);
4873
391bf048 4874 haswell_set_pipemisc(crtc);
229fca97 4875
b95c5321 4876 intel_color_set_csc(&pipe_config->base);
229fca97 4877
4f771f10 4878 intel_crtc->active = true;
8664281b 4879
6b698516
DV
4880 if (intel_crtc->config->has_pch_encoder)
4881 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
4882 else
4883 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4884
7d4aefd0 4885 for_each_encoder_on_crtc(dev, crtc, encoder) {
4f771f10
PZ
4886 if (encoder->pre_enable)
4887 encoder->pre_enable(encoder);
7d4aefd0 4888 }
4f771f10 4889
d2d65408 4890 if (intel_crtc->config->has_pch_encoder)
4fe9467d 4891 dev_priv->display.fdi_link_train(crtc);
4fe9467d 4892
a65347ba 4893 if (!intel_crtc->config->has_dsi_encoder)
7d4aefd0 4894 intel_ddi_enable_pipe_clock(intel_crtc);
4f771f10 4895
1c132b44 4896 if (INTEL_INFO(dev)->gen >= 9)
e435d6e5 4897 skylake_pfit_enable(intel_crtc);
ff6d9f55 4898 else
1c132b44 4899 ironlake_pfit_enable(intel_crtc);
4f771f10
PZ
4900
4901 /*
4902 * On ILK+ LUT must be loaded before the pipe is running but with
4903 * clocks enabled
4904 */
b95c5321 4905 intel_color_load_luts(&pipe_config->base);
4f771f10 4906
1f544388 4907 intel_ddi_set_pipe_settings(crtc);
a65347ba 4908 if (!intel_crtc->config->has_dsi_encoder)
7d4aefd0 4909 intel_ddi_enable_transcoder_func(crtc);
4f771f10 4910
1d5bf5d9
ID
4911 if (dev_priv->display.initial_watermarks != NULL)
4912 dev_priv->display.initial_watermarks(pipe_config);
4913 else
4914 intel_update_watermarks(crtc);
4d1de975
JN
4915
4916 /* XXX: Do the pipe assertions at the right place for BXT DSI. */
4917 if (!intel_crtc->config->has_dsi_encoder)
4918 intel_enable_pipe(intel_crtc);
42db64ef 4919
6e3c9717 4920 if (intel_crtc->config->has_pch_encoder)
1507e5bd 4921 lpt_pch_enable(crtc);
4f771f10 4922
a65347ba 4923 if (intel_crtc->config->dp_encoder_is_mst)
0e32b39c
DA
4924 intel_ddi_set_vc_payload_alloc(crtc, true);
4925
f9b61ff6
DV
4926 assert_vblank_disabled(crtc);
4927 drm_crtc_vblank_on(crtc);
4928
8807e55b 4929 for_each_encoder_on_crtc(dev, crtc, encoder) {
4f771f10 4930 encoder->enable(encoder);
8807e55b
JN
4931 intel_opregion_notify_encoder(encoder, true);
4932 }
4f771f10 4933
6b698516
DV
4934 if (intel_crtc->config->has_pch_encoder) {
4935 intel_wait_for_vblank(dev, pipe);
4936 intel_wait_for_vblank(dev, pipe);
4937 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
d2d65408
VS
4938 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
4939 true);
6b698516 4940 }
d2d65408 4941
e4916946
PZ
4942 /* If we change the relative order between pipe/planes enabling, we need
4943 * to change the workaround. */
99d736a2
ML
4944 hsw_workaround_pipe = pipe_config->hsw_workaround_pipe;
4945 if (IS_HASWELL(dev) && hsw_workaround_pipe != INVALID_PIPE) {
4946 intel_wait_for_vblank(dev, hsw_workaround_pipe);
4947 intel_wait_for_vblank(dev, hsw_workaround_pipe);
4948 }
4f771f10
PZ
4949}
4950
bfd16b2a 4951static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force)
3f8dce3a
DV
4952{
4953 struct drm_device *dev = crtc->base.dev;
4954 struct drm_i915_private *dev_priv = dev->dev_private;
4955 int pipe = crtc->pipe;
4956
4957 /* To avoid upsetting the power well on haswell only disable the pfit if
4958 * it's in use. The hw state code will make sure we get this right. */
bfd16b2a 4959 if (force || crtc->config->pch_pfit.enabled) {
3f8dce3a
DV
4960 I915_WRITE(PF_CTL(pipe), 0);
4961 I915_WRITE(PF_WIN_POS(pipe), 0);
4962 I915_WRITE(PF_WIN_SZ(pipe), 0);
4963 }
4964}
4965
6be4a607
JB
4966static void ironlake_crtc_disable(struct drm_crtc *crtc)
4967{
4968 struct drm_device *dev = crtc->dev;
4969 struct drm_i915_private *dev_priv = dev->dev_private;
4970 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 4971 struct intel_encoder *encoder;
6be4a607 4972 int pipe = intel_crtc->pipe;
b52eb4dc 4973
b2c0593a
VS
4974 /*
4975 * Sometimes spurious CPU pipe underruns happen when the
4976 * pipe is already disabled, but FDI RX/TX is still enabled.
4977 * Happens at least with VGA+HDMI cloning. Suppress them.
4978 */
4979 if (intel_crtc->config->has_pch_encoder) {
4980 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
37ca8d4c 4981 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
b2c0593a 4982 }
37ca8d4c 4983
ea9d758d
DV
4984 for_each_encoder_on_crtc(dev, crtc, encoder)
4985 encoder->disable(encoder);
4986
f9b61ff6
DV
4987 drm_crtc_vblank_off(crtc);
4988 assert_vblank_disabled(crtc);
4989
575f7ab7 4990 intel_disable_pipe(intel_crtc);
32f9d658 4991
bfd16b2a 4992 ironlake_pfit_disable(intel_crtc, false);
2c07245f 4993
b2c0593a 4994 if (intel_crtc->config->has_pch_encoder)
5a74f70a
VS
4995 ironlake_fdi_disable(crtc);
4996
bf49ec8c
DV
4997 for_each_encoder_on_crtc(dev, crtc, encoder)
4998 if (encoder->post_disable)
4999 encoder->post_disable(encoder);
2c07245f 5000
6e3c9717 5001 if (intel_crtc->config->has_pch_encoder) {
d925c59a 5002 ironlake_disable_pch_transcoder(dev_priv, pipe);
6be4a607 5003
d925c59a 5004 if (HAS_PCH_CPT(dev)) {
f0f59a00
VS
5005 i915_reg_t reg;
5006 u32 temp;
5007
d925c59a
DV
5008 /* disable TRANS_DP_CTL */
5009 reg = TRANS_DP_CTL(pipe);
5010 temp = I915_READ(reg);
5011 temp &= ~(TRANS_DP_OUTPUT_ENABLE |
5012 TRANS_DP_PORT_SEL_MASK);
5013 temp |= TRANS_DP_PORT_SEL_NONE;
5014 I915_WRITE(reg, temp);
5015
5016 /* disable DPLL_SEL */
5017 temp = I915_READ(PCH_DPLL_SEL);
11887397 5018 temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
d925c59a 5019 I915_WRITE(PCH_DPLL_SEL, temp);
9db4a9c7 5020 }
e3421a18 5021
d925c59a
DV
5022 ironlake_fdi_pll_disable(intel_crtc);
5023 }
81b088ca 5024
b2c0593a 5025 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
81b088ca 5026 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
6be4a607 5027}
1b3c7a47 5028
4f771f10 5029static void haswell_crtc_disable(struct drm_crtc *crtc)
ee7b9f93 5030{
4f771f10
PZ
5031 struct drm_device *dev = crtc->dev;
5032 struct drm_i915_private *dev_priv = dev->dev_private;
ee7b9f93 5033 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4f771f10 5034 struct intel_encoder *encoder;
6e3c9717 5035 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
ee7b9f93 5036
d2d65408
VS
5037 if (intel_crtc->config->has_pch_encoder)
5038 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5039 false);
5040
8807e55b
JN
5041 for_each_encoder_on_crtc(dev, crtc, encoder) {
5042 intel_opregion_notify_encoder(encoder, false);
4f771f10 5043 encoder->disable(encoder);
8807e55b 5044 }
4f771f10 5045
f9b61ff6
DV
5046 drm_crtc_vblank_off(crtc);
5047 assert_vblank_disabled(crtc);
5048
4d1de975
JN
5049 /* XXX: Do the pipe assertions at the right place for BXT DSI. */
5050 if (!intel_crtc->config->has_dsi_encoder)
5051 intel_disable_pipe(intel_crtc);
4f771f10 5052
6e3c9717 5053 if (intel_crtc->config->dp_encoder_is_mst)
a4bf214f
VS
5054 intel_ddi_set_vc_payload_alloc(crtc, false);
5055
a65347ba 5056 if (!intel_crtc->config->has_dsi_encoder)
7d4aefd0 5057 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
4f771f10 5058
1c132b44 5059 if (INTEL_INFO(dev)->gen >= 9)
e435d6e5 5060 skylake_scaler_disable(intel_crtc);
ff6d9f55 5061 else
bfd16b2a 5062 ironlake_pfit_disable(intel_crtc, false);
4f771f10 5063
a65347ba 5064 if (!intel_crtc->config->has_dsi_encoder)
7d4aefd0 5065 intel_ddi_disable_pipe_clock(intel_crtc);
4f771f10 5066
97b040aa
ID
5067 for_each_encoder_on_crtc(dev, crtc, encoder)
5068 if (encoder->post_disable)
5069 encoder->post_disable(encoder);
81b088ca 5070
92966a37
VS
5071 if (intel_crtc->config->has_pch_encoder) {
5072 lpt_disable_pch_transcoder(dev_priv);
503a74e9 5073 lpt_disable_iclkip(dev_priv);
92966a37
VS
5074 intel_ddi_fdi_disable(crtc);
5075
81b088ca
VS
5076 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5077 true);
92966a37 5078 }
4f771f10
PZ
5079}
5080
2dd24552
JB
5081static void i9xx_pfit_enable(struct intel_crtc *crtc)
5082{
5083 struct drm_device *dev = crtc->base.dev;
5084 struct drm_i915_private *dev_priv = dev->dev_private;
6e3c9717 5085 struct intel_crtc_state *pipe_config = crtc->config;
2dd24552 5086
681a8504 5087 if (!pipe_config->gmch_pfit.control)
2dd24552
JB
5088 return;
5089
2dd24552 5090 /*
c0b03411
DV
5091 * The panel fitter should only be adjusted whilst the pipe is disabled,
5092 * according to register description and PRM.
2dd24552 5093 */
c0b03411
DV
5094 WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
5095 assert_pipe_disabled(dev_priv, crtc->pipe);
2dd24552 5096
b074cec8
JB
5097 I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
5098 I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
5a80c45c
DV
5099
5100 /* Border color in case we don't scale up to the full screen. Black by
5101 * default, change to something else for debugging. */
5102 I915_WRITE(BCLRPAT(crtc->pipe), 0);
2dd24552
JB
5103}
5104
d05410f9
DA
5105static enum intel_display_power_domain port_to_power_domain(enum port port)
5106{
5107 switch (port) {
5108 case PORT_A:
6331a704 5109 return POWER_DOMAIN_PORT_DDI_A_LANES;
d05410f9 5110 case PORT_B:
6331a704 5111 return POWER_DOMAIN_PORT_DDI_B_LANES;
d05410f9 5112 case PORT_C:
6331a704 5113 return POWER_DOMAIN_PORT_DDI_C_LANES;
d05410f9 5114 case PORT_D:
6331a704 5115 return POWER_DOMAIN_PORT_DDI_D_LANES;
d8e19f99 5116 case PORT_E:
6331a704 5117 return POWER_DOMAIN_PORT_DDI_E_LANES;
d05410f9 5118 default:
b9fec167 5119 MISSING_CASE(port);
d05410f9
DA
5120 return POWER_DOMAIN_PORT_OTHER;
5121 }
5122}
5123
25f78f58
VS
5124static enum intel_display_power_domain port_to_aux_power_domain(enum port port)
5125{
5126 switch (port) {
5127 case PORT_A:
5128 return POWER_DOMAIN_AUX_A;
5129 case PORT_B:
5130 return POWER_DOMAIN_AUX_B;
5131 case PORT_C:
5132 return POWER_DOMAIN_AUX_C;
5133 case PORT_D:
5134 return POWER_DOMAIN_AUX_D;
5135 case PORT_E:
5136 /* FIXME: Check VBT for actual wiring of PORT E */
5137 return POWER_DOMAIN_AUX_D;
5138 default:
b9fec167 5139 MISSING_CASE(port);
25f78f58
VS
5140 return POWER_DOMAIN_AUX_A;
5141 }
5142}
5143
319be8ae
ID
5144enum intel_display_power_domain
5145intel_display_port_power_domain(struct intel_encoder *intel_encoder)
5146{
5147 struct drm_device *dev = intel_encoder->base.dev;
5148 struct intel_digital_port *intel_dig_port;
5149
5150 switch (intel_encoder->type) {
5151 case INTEL_OUTPUT_UNKNOWN:
5152 /* Only DDI platforms should ever use this output type */
5153 WARN_ON_ONCE(!HAS_DDI(dev));
5154 case INTEL_OUTPUT_DISPLAYPORT:
5155 case INTEL_OUTPUT_HDMI:
5156 case INTEL_OUTPUT_EDP:
5157 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
d05410f9 5158 return port_to_power_domain(intel_dig_port->port);
0e32b39c
DA
5159 case INTEL_OUTPUT_DP_MST:
5160 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
5161 return port_to_power_domain(intel_dig_port->port);
319be8ae
ID
5162 case INTEL_OUTPUT_ANALOG:
5163 return POWER_DOMAIN_PORT_CRT;
5164 case INTEL_OUTPUT_DSI:
5165 return POWER_DOMAIN_PORT_DSI;
5166 default:
5167 return POWER_DOMAIN_PORT_OTHER;
5168 }
5169}
5170
25f78f58
VS
5171enum intel_display_power_domain
5172intel_display_port_aux_power_domain(struct intel_encoder *intel_encoder)
5173{
5174 struct drm_device *dev = intel_encoder->base.dev;
5175 struct intel_digital_port *intel_dig_port;
5176
5177 switch (intel_encoder->type) {
5178 case INTEL_OUTPUT_UNKNOWN:
651174a4
ID
5179 case INTEL_OUTPUT_HDMI:
5180 /*
5181 * Only DDI platforms should ever use these output types.
5182 * We can get here after the HDMI detect code has already set
5183 * the type of the shared encoder. Since we can't be sure
5184 * what's the status of the given connectors, play safe and
5185 * run the DP detection too.
5186 */
25f78f58
VS
5187 WARN_ON_ONCE(!HAS_DDI(dev));
5188 case INTEL_OUTPUT_DISPLAYPORT:
5189 case INTEL_OUTPUT_EDP:
5190 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
5191 return port_to_aux_power_domain(intel_dig_port->port);
5192 case INTEL_OUTPUT_DP_MST:
5193 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
5194 return port_to_aux_power_domain(intel_dig_port->port);
5195 default:
b9fec167 5196 MISSING_CASE(intel_encoder->type);
25f78f58
VS
5197 return POWER_DOMAIN_AUX_A;
5198 }
5199}
5200
74bff5f9
ML
5201static unsigned long get_crtc_power_domains(struct drm_crtc *crtc,
5202 struct intel_crtc_state *crtc_state)
77d22dca 5203{
319be8ae 5204 struct drm_device *dev = crtc->dev;
74bff5f9 5205 struct drm_encoder *encoder;
319be8ae
ID
5206 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5207 enum pipe pipe = intel_crtc->pipe;
77d22dca 5208 unsigned long mask;
74bff5f9 5209 enum transcoder transcoder = crtc_state->cpu_transcoder;
77d22dca 5210
74bff5f9 5211 if (!crtc_state->base.active)
292b990e
ML
5212 return 0;
5213
77d22dca
ID
5214 mask = BIT(POWER_DOMAIN_PIPE(pipe));
5215 mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
74bff5f9
ML
5216 if (crtc_state->pch_pfit.enabled ||
5217 crtc_state->pch_pfit.force_thru)
77d22dca
ID
5218 mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
5219
74bff5f9
ML
5220 drm_for_each_encoder_mask(encoder, dev, crtc_state->base.encoder_mask) {
5221 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
5222
319be8ae 5223 mask |= BIT(intel_display_port_power_domain(intel_encoder));
74bff5f9 5224 }
319be8ae 5225
15e7ec29
ML
5226 if (crtc_state->shared_dpll)
5227 mask |= BIT(POWER_DOMAIN_PLLS);
5228
77d22dca
ID
5229 return mask;
5230}
5231
74bff5f9
ML
5232static unsigned long
5233modeset_get_crtc_power_domains(struct drm_crtc *crtc,
5234 struct intel_crtc_state *crtc_state)
77d22dca 5235{
292b990e
ML
5236 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
5237 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5238 enum intel_display_power_domain domain;
5a21b665 5239 unsigned long domains, new_domains, old_domains;
77d22dca 5240
292b990e 5241 old_domains = intel_crtc->enabled_power_domains;
74bff5f9
ML
5242 intel_crtc->enabled_power_domains = new_domains =
5243 get_crtc_power_domains(crtc, crtc_state);
77d22dca 5244
5a21b665 5245 domains = new_domains & ~old_domains;
292b990e
ML
5246
5247 for_each_power_domain(domain, domains)
5248 intel_display_power_get(dev_priv, domain);
5249
5a21b665 5250 return old_domains & ~new_domains;
292b990e
ML
5251}
5252
5253static void modeset_put_power_domains(struct drm_i915_private *dev_priv,
5254 unsigned long domains)
5255{
5256 enum intel_display_power_domain domain;
5257
5258 for_each_power_domain(domain, domains)
5259 intel_display_power_put(dev_priv, domain);
5260}
77d22dca 5261
adafdc6f
MK
5262static int intel_compute_max_dotclk(struct drm_i915_private *dev_priv)
5263{
5264 int max_cdclk_freq = dev_priv->max_cdclk_freq;
5265
5266 if (INTEL_INFO(dev_priv)->gen >= 9 ||
5267 IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
5268 return max_cdclk_freq;
5269 else if (IS_CHERRYVIEW(dev_priv))
5270 return max_cdclk_freq*95/100;
5271 else if (INTEL_INFO(dev_priv)->gen < 4)
5272 return 2*max_cdclk_freq*90/100;
5273 else
5274 return max_cdclk_freq*90/100;
5275}
5276
b2045352
VS
5277static int skl_calc_cdclk(int max_pixclk, int vco);
5278
560a7ae4
DL
5279static void intel_update_max_cdclk(struct drm_device *dev)
5280{
5281 struct drm_i915_private *dev_priv = dev->dev_private;
5282
ef11bdb3 5283 if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) {
560a7ae4 5284 u32 limit = I915_READ(SKL_DFSM) & SKL_DFSM_CDCLK_LIMIT_MASK;
b2045352
VS
5285 int max_cdclk, vco;
5286
5287 vco = dev_priv->skl_preferred_vco_freq;
63911d72 5288 WARN_ON(vco != 8100000 && vco != 8640000);
560a7ae4 5289
b2045352
VS
5290 /*
5291 * Use the lower (vco 8640) cdclk values as a
5292 * first guess. skl_calc_cdclk() will correct it
5293 * if the preferred vco is 8100 instead.
5294 */
560a7ae4 5295 if (limit == SKL_DFSM_CDCLK_LIMIT_675)
487ed2e4 5296 max_cdclk = 617143;
560a7ae4 5297 else if (limit == SKL_DFSM_CDCLK_LIMIT_540)
b2045352 5298 max_cdclk = 540000;
560a7ae4 5299 else if (limit == SKL_DFSM_CDCLK_LIMIT_450)
b2045352 5300 max_cdclk = 432000;
560a7ae4 5301 else
487ed2e4 5302 max_cdclk = 308571;
b2045352
VS
5303
5304 dev_priv->max_cdclk_freq = skl_calc_cdclk(max_cdclk, vco);
281c114f
MR
5305 } else if (IS_BROXTON(dev)) {
5306 dev_priv->max_cdclk_freq = 624000;
560a7ae4
DL
5307 } else if (IS_BROADWELL(dev)) {
5308 /*
5309 * FIXME with extra cooling we can allow
5310 * 540 MHz for ULX and 675 Mhz for ULT.
5311 * How can we know if extra cooling is
5312 * available? PCI ID, VTB, something else?
5313 */
5314 if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
5315 dev_priv->max_cdclk_freq = 450000;
5316 else if (IS_BDW_ULX(dev))
5317 dev_priv->max_cdclk_freq = 450000;
5318 else if (IS_BDW_ULT(dev))
5319 dev_priv->max_cdclk_freq = 540000;
5320 else
5321 dev_priv->max_cdclk_freq = 675000;
0904deaf
MK
5322 } else if (IS_CHERRYVIEW(dev)) {
5323 dev_priv->max_cdclk_freq = 320000;
560a7ae4
DL
5324 } else if (IS_VALLEYVIEW(dev)) {
5325 dev_priv->max_cdclk_freq = 400000;
5326 } else {
5327 /* otherwise assume cdclk is fixed */
5328 dev_priv->max_cdclk_freq = dev_priv->cdclk_freq;
5329 }
5330
adafdc6f
MK
5331 dev_priv->max_dotclk_freq = intel_compute_max_dotclk(dev_priv);
5332
560a7ae4
DL
5333 DRM_DEBUG_DRIVER("Max CD clock rate: %d kHz\n",
5334 dev_priv->max_cdclk_freq);
adafdc6f
MK
5335
5336 DRM_DEBUG_DRIVER("Max dotclock rate: %d kHz\n",
5337 dev_priv->max_dotclk_freq);
560a7ae4
DL
5338}
5339
5340static void intel_update_cdclk(struct drm_device *dev)
5341{
5342 struct drm_i915_private *dev_priv = dev->dev_private;
5343
5344 dev_priv->cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
2f2a121a 5345
83d7c81f 5346 if (INTEL_GEN(dev_priv) >= 9)
709e05c3
VS
5347 DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz, VCO: %d kHz, ref: %d kHz\n",
5348 dev_priv->cdclk_freq, dev_priv->cdclk_pll.vco,
5349 dev_priv->cdclk_pll.ref);
2f2a121a
VS
5350 else
5351 DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
5352 dev_priv->cdclk_freq);
560a7ae4
DL
5353
5354 /*
b5d99ff9
VS
5355 * 9:0 CMBUS [sic] CDCLK frequency (cdfreq):
5356 * Programmng [sic] note: bit[9:2] should be programmed to the number
5357 * of cdclk that generates 4MHz reference clock freq which is used to
5358 * generate GMBus clock. This will vary with the cdclk freq.
560a7ae4 5359 */
b5d99ff9 5360 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
560a7ae4 5361 I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->cdclk_freq, 1000));
560a7ae4
DL
5362}
5363
92891e45
VS
5364/* convert from kHz to .1 fixpoint MHz with -1MHz offset */
5365static int skl_cdclk_decimal(int cdclk)
5366{
5367 return DIV_ROUND_CLOSEST(cdclk - 1000, 500);
5368}
5369
5f199dfa
VS
5370static int bxt_de_pll_vco(struct drm_i915_private *dev_priv, int cdclk)
5371{
5372 int ratio;
5373
5374 if (cdclk == dev_priv->cdclk_pll.ref)
5375 return 0;
5376
5377 switch (cdclk) {
5378 default:
5379 MISSING_CASE(cdclk);
5380 case 144000:
5381 case 288000:
5382 case 384000:
5383 case 576000:
5384 ratio = 60;
5385 break;
5386 case 624000:
5387 ratio = 65;
5388 break;
5389 }
5390
5391 return dev_priv->cdclk_pll.ref * ratio;
5392}
5393
2b73001e
VS
5394static void bxt_de_pll_disable(struct drm_i915_private *dev_priv)
5395{
5396 I915_WRITE(BXT_DE_PLL_ENABLE, 0);
5397
5398 /* Timeout 200us */
5399 if (wait_for((I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK) == 0, 1))
5400 DRM_ERROR("timeout waiting for DE PLL unlock\n");
83d7c81f
VS
5401
5402 dev_priv->cdclk_pll.vco = 0;
2b73001e
VS
5403}
5404
5f199dfa 5405static void bxt_de_pll_enable(struct drm_i915_private *dev_priv, int vco)
2b73001e 5406{
5f199dfa 5407 int ratio = DIV_ROUND_CLOSEST(vco, dev_priv->cdclk_pll.ref);
2b73001e
VS
5408 u32 val;
5409
5410 val = I915_READ(BXT_DE_PLL_CTL);
5411 val &= ~BXT_DE_PLL_RATIO_MASK;
5f199dfa 5412 val |= BXT_DE_PLL_RATIO(ratio);
2b73001e
VS
5413 I915_WRITE(BXT_DE_PLL_CTL, val);
5414
5415 I915_WRITE(BXT_DE_PLL_ENABLE, BXT_DE_PLL_PLL_ENABLE);
5416
5417 /* Timeout 200us */
5418 if (wait_for((I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK) != 0, 1))
5419 DRM_ERROR("timeout waiting for DE PLL lock\n");
83d7c81f 5420
5f199dfa 5421 dev_priv->cdclk_pll.vco = vco;
2b73001e
VS
5422}
5423
324513c0 5424static void bxt_set_cdclk(struct drm_i915_private *dev_priv, int cdclk)
f8437dd1 5425{
5f199dfa
VS
5426 u32 val, divider;
5427 int vco, ret;
f8437dd1 5428
5f199dfa
VS
5429 vco = bxt_de_pll_vco(dev_priv, cdclk);
5430
5431 DRM_DEBUG_DRIVER("Changing CDCLK to %d kHz (VCO %d kHz)\n", cdclk, vco);
5432
5433 /* cdclk = vco / 2 / div{1,1.5,2,4} */
5434 switch (DIV_ROUND_CLOSEST(vco, cdclk)) {
5435 case 8:
f8437dd1 5436 divider = BXT_CDCLK_CD2X_DIV_SEL_4;
f8437dd1 5437 break;
5f199dfa 5438 case 4:
f8437dd1 5439 divider = BXT_CDCLK_CD2X_DIV_SEL_2;
f8437dd1 5440 break;
5f199dfa 5441 case 3:
f8437dd1 5442 divider = BXT_CDCLK_CD2X_DIV_SEL_1_5;
f8437dd1 5443 break;
5f199dfa 5444 case 2:
f8437dd1 5445 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
f8437dd1
VK
5446 break;
5447 default:
5f199dfa
VS
5448 WARN_ON(cdclk != dev_priv->cdclk_pll.ref);
5449 WARN_ON(vco != 0);
f8437dd1 5450
5f199dfa
VS
5451 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
5452 break;
f8437dd1
VK
5453 }
5454
f8437dd1 5455 /* Inform power controller of upcoming frequency change */
5f199dfa 5456 mutex_lock(&dev_priv->rps.hw_lock);
f8437dd1
VK
5457 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
5458 0x80000000);
5459 mutex_unlock(&dev_priv->rps.hw_lock);
5460
5461 if (ret) {
5462 DRM_ERROR("PCode CDCLK freq change notify failed (err %d, freq %d)\n",
9ef56154 5463 ret, cdclk);
f8437dd1
VK
5464 return;
5465 }
5466
5f199dfa
VS
5467 if (dev_priv->cdclk_pll.vco != 0 &&
5468 dev_priv->cdclk_pll.vco != vco)
2b73001e 5469 bxt_de_pll_disable(dev_priv);
f8437dd1 5470
5f199dfa
VS
5471 if (dev_priv->cdclk_pll.vco != vco)
5472 bxt_de_pll_enable(dev_priv, vco);
f8437dd1 5473
5f199dfa
VS
5474 val = divider | skl_cdclk_decimal(cdclk);
5475 /*
5476 * FIXME if only the cd2x divider needs changing, it could be done
5477 * without shutting off the pipe (if only one pipe is active).
5478 */
5479 val |= BXT_CDCLK_CD2X_PIPE_NONE;
5480 /*
5481 * Disable SSA Precharge when CD clock frequency < 500 MHz,
5482 * enable otherwise.
5483 */
5484 if (cdclk >= 500000)
5485 val |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
5486 I915_WRITE(CDCLK_CTL, val);
f8437dd1
VK
5487
5488 mutex_lock(&dev_priv->rps.hw_lock);
5489 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
9ef56154 5490 DIV_ROUND_UP(cdclk, 25000));
f8437dd1
VK
5491 mutex_unlock(&dev_priv->rps.hw_lock);
5492
5493 if (ret) {
5494 DRM_ERROR("PCode CDCLK freq set failed, (err %d, freq %d)\n",
9ef56154 5495 ret, cdclk);
f8437dd1
VK
5496 return;
5497 }
5498
c6c4696f 5499 intel_update_cdclk(dev_priv->dev);
f8437dd1
VK
5500}
5501
d66a2194 5502static void bxt_sanitize_cdclk(struct drm_i915_private *dev_priv)
f8437dd1 5503{
d66a2194
ID
5504 u32 cdctl, expected;
5505
089c6fd5 5506 intel_update_cdclk(dev_priv->dev);
f8437dd1 5507
d66a2194
ID
5508 if (dev_priv->cdclk_pll.vco == 0 ||
5509 dev_priv->cdclk_freq == dev_priv->cdclk_pll.ref)
5510 goto sanitize;
5511
5512 /* DPLL okay; verify the cdclock
5513 *
5514 * Some BIOS versions leave an incorrect decimal frequency value and
5515 * set reserved MBZ bits in CDCLK_CTL at least during exiting from S4,
5516 * so sanitize this register.
5517 */
5518 cdctl = I915_READ(CDCLK_CTL);
5519 /*
5520 * Let's ignore the pipe field, since BIOS could have configured the
5521 * dividers both synching to an active pipe, or asynchronously
5522 * (PIPE_NONE).
5523 */
5524 cdctl &= ~BXT_CDCLK_CD2X_PIPE_NONE;
5525
5526 expected = (cdctl & BXT_CDCLK_CD2X_DIV_SEL_MASK) |
5527 skl_cdclk_decimal(dev_priv->cdclk_freq);
5528 /*
5529 * Disable SSA Precharge when CD clock frequency < 500 MHz,
5530 * enable otherwise.
5531 */
5532 if (dev_priv->cdclk_freq >= 500000)
5533 expected |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
5534
5535 if (cdctl == expected)
5536 /* All well; nothing to sanitize */
5537 return;
5538
5539sanitize:
5540 DRM_DEBUG_KMS("Sanitizing cdclk programmed by pre-os\n");
5541
5542 /* force cdclk programming */
5543 dev_priv->cdclk_freq = 0;
5544
5545 /* force full PLL disable + enable */
5546 dev_priv->cdclk_pll.vco = -1;
5547}
5548
324513c0 5549void bxt_init_cdclk(struct drm_i915_private *dev_priv)
d66a2194
ID
5550{
5551 bxt_sanitize_cdclk(dev_priv);
5552
5553 if (dev_priv->cdclk_freq != 0 && dev_priv->cdclk_pll.vco != 0)
089c6fd5 5554 return;
c2e001ef 5555
f8437dd1
VK
5556 /*
5557 * FIXME:
5558 * - The initial CDCLK needs to be read from VBT.
5559 * Need to make this change after VBT has changes for BXT.
f8437dd1 5560 */
324513c0 5561 bxt_set_cdclk(dev_priv, bxt_calc_cdclk(0));
f8437dd1
VK
5562}
5563
324513c0 5564void bxt_uninit_cdclk(struct drm_i915_private *dev_priv)
f8437dd1 5565{
324513c0 5566 bxt_set_cdclk(dev_priv, dev_priv->cdclk_pll.ref);
f8437dd1
VK
5567}
5568
a8ca4934
VS
5569static int skl_calc_cdclk(int max_pixclk, int vco)
5570{
63911d72 5571 if (vco == 8640000) {
a8ca4934 5572 if (max_pixclk > 540000)
487ed2e4 5573 return 617143;
a8ca4934
VS
5574 else if (max_pixclk > 432000)
5575 return 540000;
487ed2e4 5576 else if (max_pixclk > 308571)
a8ca4934
VS
5577 return 432000;
5578 else
487ed2e4 5579 return 308571;
a8ca4934 5580 } else {
a8ca4934
VS
5581 if (max_pixclk > 540000)
5582 return 675000;
5583 else if (max_pixclk > 450000)
5584 return 540000;
5585 else if (max_pixclk > 337500)
5586 return 450000;
5587 else
5588 return 337500;
5589 }
5590}
5591
ea61791e
VS
5592static void
5593skl_dpll0_update(struct drm_i915_private *dev_priv)
5d96d8af 5594{
ea61791e 5595 u32 val;
5d96d8af 5596
709e05c3 5597 dev_priv->cdclk_pll.ref = 24000;
1c3f7700 5598 dev_priv->cdclk_pll.vco = 0;
709e05c3 5599
ea61791e 5600 val = I915_READ(LCPLL1_CTL);
1c3f7700 5601 if ((val & LCPLL_PLL_ENABLE) == 0)
ea61791e 5602 return;
5d96d8af 5603
1c3f7700
ID
5604 if (WARN_ON((val & LCPLL_PLL_LOCK) == 0))
5605 return;
9f7eb31a 5606
ea61791e
VS
5607 val = I915_READ(DPLL_CTRL1);
5608
1c3f7700
ID
5609 if (WARN_ON((val & (DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) |
5610 DPLL_CTRL1_SSC(SKL_DPLL0) |
5611 DPLL_CTRL1_OVERRIDE(SKL_DPLL0))) !=
5612 DPLL_CTRL1_OVERRIDE(SKL_DPLL0)))
5613 return;
9f7eb31a 5614
ea61791e
VS
5615 switch (val & DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0)) {
5616 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810, SKL_DPLL0):
5617 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1350, SKL_DPLL0):
5618 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1620, SKL_DPLL0):
5619 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_2700, SKL_DPLL0):
63911d72 5620 dev_priv->cdclk_pll.vco = 8100000;
ea61791e
VS
5621 break;
5622 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080, SKL_DPLL0):
5623 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_2160, SKL_DPLL0):
63911d72 5624 dev_priv->cdclk_pll.vco = 8640000;
ea61791e
VS
5625 break;
5626 default:
5627 MISSING_CASE(val & DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
ea61791e
VS
5628 break;
5629 }
5d96d8af
DL
5630}
5631
b2045352
VS
5632void skl_set_preferred_cdclk_vco(struct drm_i915_private *dev_priv, int vco)
5633{
5634 bool changed = dev_priv->skl_preferred_vco_freq != vco;
5635
5636 dev_priv->skl_preferred_vco_freq = vco;
5637
5638 if (changed)
5639 intel_update_max_cdclk(dev_priv->dev);
5640}
5641
5d96d8af 5642static void
3861fc60 5643skl_dpll0_enable(struct drm_i915_private *dev_priv, int vco)
5d96d8af 5644{
a8ca4934 5645 int min_cdclk = skl_calc_cdclk(0, vco);
5d96d8af
DL
5646 u32 val;
5647
63911d72 5648 WARN_ON(vco != 8100000 && vco != 8640000);
b2045352 5649
5d96d8af 5650 /* select the minimum CDCLK before enabling DPLL 0 */
9ef56154 5651 val = CDCLK_FREQ_337_308 | skl_cdclk_decimal(min_cdclk);
5d96d8af
DL
5652 I915_WRITE(CDCLK_CTL, val);
5653 POSTING_READ(CDCLK_CTL);
5654
5655 /*
5656 * We always enable DPLL0 with the lowest link rate possible, but still
5657 * taking into account the VCO required to operate the eDP panel at the
5658 * desired frequency. The usual DP link rates operate with a VCO of
5659 * 8100 while the eDP 1.4 alternate link rates need a VCO of 8640.
5660 * The modeset code is responsible for the selection of the exact link
5661 * rate later on, with the constraint of choosing a frequency that
a8ca4934 5662 * works with vco.
5d96d8af
DL
5663 */
5664 val = I915_READ(DPLL_CTRL1);
5665
5666 val &= ~(DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) | DPLL_CTRL1_SSC(SKL_DPLL0) |
5667 DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
5668 val |= DPLL_CTRL1_OVERRIDE(SKL_DPLL0);
63911d72 5669 if (vco == 8640000)
5d96d8af
DL
5670 val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080,
5671 SKL_DPLL0);
5672 else
5673 val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810,
5674 SKL_DPLL0);
5675
5676 I915_WRITE(DPLL_CTRL1, val);
5677 POSTING_READ(DPLL_CTRL1);
5678
5679 I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) | LCPLL_PLL_ENABLE);
5680
5681 if (wait_for(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK, 5))
5682 DRM_ERROR("DPLL0 not locked\n");
1cd593e0 5683
63911d72 5684 dev_priv->cdclk_pll.vco = vco;
b2045352
VS
5685
5686 /* We'll want to keep using the current vco from now on. */
5687 skl_set_preferred_cdclk_vco(dev_priv, vco);
5d96d8af
DL
5688}
5689
430e05de
VS
5690static void
5691skl_dpll0_disable(struct drm_i915_private *dev_priv)
5692{
5693 I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) & ~LCPLL_PLL_ENABLE);
5694 if (wait_for(!(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK), 1))
5695 DRM_ERROR("Couldn't disable DPLL0\n");
1cd593e0 5696
63911d72 5697 dev_priv->cdclk_pll.vco = 0;
430e05de
VS
5698}
5699
5d96d8af
DL
5700static bool skl_cdclk_pcu_ready(struct drm_i915_private *dev_priv)
5701{
5702 int ret;
5703 u32 val;
5704
5705 /* inform PCU we want to change CDCLK */
5706 val = SKL_CDCLK_PREPARE_FOR_CHANGE;
5707 mutex_lock(&dev_priv->rps.hw_lock);
5708 ret = sandybridge_pcode_read(dev_priv, SKL_PCODE_CDCLK_CONTROL, &val);
5709 mutex_unlock(&dev_priv->rps.hw_lock);
5710
5711 return ret == 0 && (val & SKL_CDCLK_READY_FOR_CHANGE);
5712}
5713
5714static bool skl_cdclk_wait_for_pcu_ready(struct drm_i915_private *dev_priv)
5715{
5716 unsigned int i;
5717
5718 for (i = 0; i < 15; i++) {
5719 if (skl_cdclk_pcu_ready(dev_priv))
5720 return true;
5721 udelay(10);
5722 }
5723
5724 return false;
5725}
5726
1cd593e0 5727static void skl_set_cdclk(struct drm_i915_private *dev_priv, int cdclk, int vco)
5d96d8af 5728{
560a7ae4 5729 struct drm_device *dev = dev_priv->dev;
5d96d8af
DL
5730 u32 freq_select, pcu_ack;
5731
1cd593e0
VS
5732 WARN_ON((cdclk == 24000) != (vco == 0));
5733
63911d72 5734 DRM_DEBUG_DRIVER("Changing CDCLK to %d kHz (VCO %d kHz)\n", cdclk, vco);
5d96d8af
DL
5735
5736 if (!skl_cdclk_wait_for_pcu_ready(dev_priv)) {
5737 DRM_ERROR("failed to inform PCU about cdclk change\n");
5738 return;
5739 }
5740
5741 /* set CDCLK_CTL */
9ef56154 5742 switch (cdclk) {
5d96d8af
DL
5743 case 450000:
5744 case 432000:
5745 freq_select = CDCLK_FREQ_450_432;
5746 pcu_ack = 1;
5747 break;
5748 case 540000:
5749 freq_select = CDCLK_FREQ_540;
5750 pcu_ack = 2;
5751 break;
487ed2e4 5752 case 308571:
5d96d8af
DL
5753 case 337500:
5754 default:
5755 freq_select = CDCLK_FREQ_337_308;
5756 pcu_ack = 0;
5757 break;
487ed2e4 5758 case 617143:
5d96d8af
DL
5759 case 675000:
5760 freq_select = CDCLK_FREQ_675_617;
5761 pcu_ack = 3;
5762 break;
5763 }
5764
63911d72
VS
5765 if (dev_priv->cdclk_pll.vco != 0 &&
5766 dev_priv->cdclk_pll.vco != vco)
1cd593e0
VS
5767 skl_dpll0_disable(dev_priv);
5768
63911d72 5769 if (dev_priv->cdclk_pll.vco != vco)
1cd593e0
VS
5770 skl_dpll0_enable(dev_priv, vco);
5771
9ef56154 5772 I915_WRITE(CDCLK_CTL, freq_select | skl_cdclk_decimal(cdclk));
5d96d8af
DL
5773 POSTING_READ(CDCLK_CTL);
5774
5775 /* inform PCU of the change */
5776 mutex_lock(&dev_priv->rps.hw_lock);
5777 sandybridge_pcode_write(dev_priv, SKL_PCODE_CDCLK_CONTROL, pcu_ack);
5778 mutex_unlock(&dev_priv->rps.hw_lock);
560a7ae4
DL
5779
5780 intel_update_cdclk(dev);
5d96d8af
DL
5781}
5782
9f7eb31a
VS
5783static void skl_sanitize_cdclk(struct drm_i915_private *dev_priv);
5784
5d96d8af
DL
5785void skl_uninit_cdclk(struct drm_i915_private *dev_priv)
5786{
709e05c3 5787 skl_set_cdclk(dev_priv, dev_priv->cdclk_pll.ref, 0);
5d96d8af
DL
5788}
5789
5790void skl_init_cdclk(struct drm_i915_private *dev_priv)
5791{
9f7eb31a
VS
5792 int cdclk, vco;
5793
5794 skl_sanitize_cdclk(dev_priv);
5d96d8af 5795
63911d72 5796 if (dev_priv->cdclk_freq != 0 && dev_priv->cdclk_pll.vco != 0) {
9f7eb31a
VS
5797 /*
5798 * Use the current vco as our initial
5799 * guess as to what the preferred vco is.
5800 */
5801 if (dev_priv->skl_preferred_vco_freq == 0)
5802 skl_set_preferred_cdclk_vco(dev_priv,
63911d72 5803 dev_priv->cdclk_pll.vco);
70c2c184 5804 return;
1cd593e0 5805 }
5d96d8af 5806
70c2c184
VS
5807 vco = dev_priv->skl_preferred_vco_freq;
5808 if (vco == 0)
63911d72 5809 vco = 8100000;
70c2c184 5810 cdclk = skl_calc_cdclk(0, vco);
5d96d8af 5811
70c2c184 5812 skl_set_cdclk(dev_priv, cdclk, vco);
5d96d8af
DL
5813}
5814
9f7eb31a 5815static void skl_sanitize_cdclk(struct drm_i915_private *dev_priv)
c73666f3 5816{
09492498 5817 uint32_t cdctl, expected;
c73666f3 5818
f1b391a5
SK
5819 /*
5820 * check if the pre-os intialized the display
5821 * There is SWF18 scratchpad register defined which is set by the
5822 * pre-os which can be used by the OS drivers to check the status
5823 */
5824 if ((I915_READ(SWF_ILK(0x18)) & 0x00FFFFFF) == 0)
5825 goto sanitize;
5826
1c3f7700 5827 intel_update_cdclk(dev_priv->dev);
c73666f3 5828 /* Is PLL enabled and locked ? */
1c3f7700
ID
5829 if (dev_priv->cdclk_pll.vco == 0 ||
5830 dev_priv->cdclk_freq == dev_priv->cdclk_pll.ref)
c73666f3
SK
5831 goto sanitize;
5832
5833 /* DPLL okay; verify the cdclock
5834 *
5835 * Noticed in some instances that the freq selection is correct but
5836 * decimal part is programmed wrong from BIOS where pre-os does not
5837 * enable display. Verify the same as well.
5838 */
09492498
VS
5839 cdctl = I915_READ(CDCLK_CTL);
5840 expected = (cdctl & CDCLK_FREQ_SEL_MASK) |
5841 skl_cdclk_decimal(dev_priv->cdclk_freq);
5842 if (cdctl == expected)
c73666f3 5843 /* All well; nothing to sanitize */
9f7eb31a 5844 return;
c89e39f3 5845
9f7eb31a
VS
5846sanitize:
5847 DRM_DEBUG_KMS("Sanitizing cdclk programmed by pre-os\n");
c73666f3 5848
9f7eb31a
VS
5849 /* force cdclk programming */
5850 dev_priv->cdclk_freq = 0;
5851 /* force full PLL disable + enable */
63911d72 5852 dev_priv->cdclk_pll.vco = -1;
c73666f3
SK
5853}
5854
30a970c6
JB
5855/* Adjust CDclk dividers to allow high res or save power if possible */
5856static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
5857{
5858 struct drm_i915_private *dev_priv = dev->dev_private;
5859 u32 val, cmd;
5860
164dfd28
VK
5861 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5862 != dev_priv->cdclk_freq);
d60c4473 5863
dfcab17e 5864 if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
30a970c6 5865 cmd = 2;
dfcab17e 5866 else if (cdclk == 266667)
30a970c6
JB
5867 cmd = 1;
5868 else
5869 cmd = 0;
5870
5871 mutex_lock(&dev_priv->rps.hw_lock);
5872 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5873 val &= ~DSPFREQGUAR_MASK;
5874 val |= (cmd << DSPFREQGUAR_SHIFT);
5875 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5876 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5877 DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
5878 50)) {
5879 DRM_ERROR("timed out waiting for CDclk change\n");
5880 }
5881 mutex_unlock(&dev_priv->rps.hw_lock);
5882
54433e91
VS
5883 mutex_lock(&dev_priv->sb_lock);
5884
dfcab17e 5885 if (cdclk == 400000) {
6bcda4f0 5886 u32 divider;
30a970c6 5887
6bcda4f0 5888 divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
30a970c6 5889
30a970c6
JB
5890 /* adjust cdclk divider */
5891 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
87d5d259 5892 val &= ~CCK_FREQUENCY_VALUES;
30a970c6
JB
5893 val |= divider;
5894 vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
a877e801
VS
5895
5896 if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
87d5d259 5897 CCK_FREQUENCY_STATUS) == (divider << CCK_FREQUENCY_STATUS_SHIFT),
a877e801
VS
5898 50))
5899 DRM_ERROR("timed out waiting for CDclk change\n");
30a970c6
JB
5900 }
5901
30a970c6
JB
5902 /* adjust self-refresh exit latency value */
5903 val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
5904 val &= ~0x7f;
5905
5906 /*
5907 * For high bandwidth configs, we set a higher latency in the bunit
5908 * so that the core display fetch happens in time to avoid underruns.
5909 */
dfcab17e 5910 if (cdclk == 400000)
30a970c6
JB
5911 val |= 4500 / 250; /* 4.5 usec */
5912 else
5913 val |= 3000 / 250; /* 3.0 usec */
5914 vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
54433e91 5915
a580516d 5916 mutex_unlock(&dev_priv->sb_lock);
30a970c6 5917
b6283055 5918 intel_update_cdclk(dev);
30a970c6
JB
5919}
5920
383c5a6a
VS
5921static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
5922{
5923 struct drm_i915_private *dev_priv = dev->dev_private;
5924 u32 val, cmd;
5925
164dfd28
VK
5926 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5927 != dev_priv->cdclk_freq);
383c5a6a
VS
5928
5929 switch (cdclk) {
383c5a6a
VS
5930 case 333333:
5931 case 320000:
383c5a6a 5932 case 266667:
383c5a6a 5933 case 200000:
383c5a6a
VS
5934 break;
5935 default:
5f77eeb0 5936 MISSING_CASE(cdclk);
383c5a6a
VS
5937 return;
5938 }
5939
9d0d3fda
VS
5940 /*
5941 * Specs are full of misinformation, but testing on actual
5942 * hardware has shown that we just need to write the desired
5943 * CCK divider into the Punit register.
5944 */
5945 cmd = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
5946
383c5a6a
VS
5947 mutex_lock(&dev_priv->rps.hw_lock);
5948 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5949 val &= ~DSPFREQGUAR_MASK_CHV;
5950 val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
5951 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5952 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5953 DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
5954 50)) {
5955 DRM_ERROR("timed out waiting for CDclk change\n");
5956 }
5957 mutex_unlock(&dev_priv->rps.hw_lock);
5958
b6283055 5959 intel_update_cdclk(dev);
383c5a6a
VS
5960}
5961
30a970c6
JB
5962static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
5963 int max_pixclk)
5964{
6bcda4f0 5965 int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
6cca3195 5966 int limit = IS_CHERRYVIEW(dev_priv) ? 95 : 90;
29dc7ef3 5967
30a970c6
JB
5968 /*
5969 * Really only a few cases to deal with, as only 4 CDclks are supported:
5970 * 200MHz
5971 * 267MHz
29dc7ef3 5972 * 320/333MHz (depends on HPLL freq)
6cca3195
VS
5973 * 400MHz (VLV only)
5974 * So we check to see whether we're above 90% (VLV) or 95% (CHV)
5975 * of the lower bin and adjust if needed.
e37c67a1
VS
5976 *
5977 * We seem to get an unstable or solid color picture at 200MHz.
5978 * Not sure what's wrong. For now use 200MHz only when all pipes
5979 * are off.
30a970c6 5980 */
6cca3195
VS
5981 if (!IS_CHERRYVIEW(dev_priv) &&
5982 max_pixclk > freq_320*limit/100)
dfcab17e 5983 return 400000;
6cca3195 5984 else if (max_pixclk > 266667*limit/100)
29dc7ef3 5985 return freq_320;
e37c67a1 5986 else if (max_pixclk > 0)
dfcab17e 5987 return 266667;
e37c67a1
VS
5988 else
5989 return 200000;
30a970c6
JB
5990}
5991
324513c0 5992static int bxt_calc_cdclk(int max_pixclk)
f8437dd1 5993{
760e1477 5994 if (max_pixclk > 576000)
f8437dd1 5995 return 624000;
760e1477 5996 else if (max_pixclk > 384000)
f8437dd1 5997 return 576000;
760e1477 5998 else if (max_pixclk > 288000)
f8437dd1 5999 return 384000;
760e1477 6000 else if (max_pixclk > 144000)
f8437dd1
VK
6001 return 288000;
6002 else
6003 return 144000;
6004}
6005
e8788cbc 6006/* Compute the max pixel clock for new configuration. */
a821fc46
ACO
6007static int intel_mode_max_pixclk(struct drm_device *dev,
6008 struct drm_atomic_state *state)
30a970c6 6009{
565602d7
ML
6010 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
6011 struct drm_i915_private *dev_priv = dev->dev_private;
6012 struct drm_crtc *crtc;
6013 struct drm_crtc_state *crtc_state;
6014 unsigned max_pixclk = 0, i;
6015 enum pipe pipe;
30a970c6 6016
565602d7
ML
6017 memcpy(intel_state->min_pixclk, dev_priv->min_pixclk,
6018 sizeof(intel_state->min_pixclk));
304603f4 6019
565602d7
ML
6020 for_each_crtc_in_state(state, crtc, crtc_state, i) {
6021 int pixclk = 0;
6022
6023 if (crtc_state->enable)
6024 pixclk = crtc_state->adjusted_mode.crtc_clock;
304603f4 6025
565602d7 6026 intel_state->min_pixclk[i] = pixclk;
30a970c6
JB
6027 }
6028
565602d7
ML
6029 for_each_pipe(dev_priv, pipe)
6030 max_pixclk = max(intel_state->min_pixclk[pipe], max_pixclk);
6031
30a970c6
JB
6032 return max_pixclk;
6033}
6034
27c329ed 6035static int valleyview_modeset_calc_cdclk(struct drm_atomic_state *state)
30a970c6 6036{
27c329ed
ML
6037 struct drm_device *dev = state->dev;
6038 struct drm_i915_private *dev_priv = dev->dev_private;
6039 int max_pixclk = intel_mode_max_pixclk(dev, state);
1a617b77
ML
6040 struct intel_atomic_state *intel_state =
6041 to_intel_atomic_state(state);
30a970c6 6042
1a617b77 6043 intel_state->cdclk = intel_state->dev_cdclk =
27c329ed 6044 valleyview_calc_cdclk(dev_priv, max_pixclk);
0a9ab303 6045
1a617b77
ML
6046 if (!intel_state->active_crtcs)
6047 intel_state->dev_cdclk = valleyview_calc_cdclk(dev_priv, 0);
6048
27c329ed
ML
6049 return 0;
6050}
304603f4 6051
324513c0 6052static int bxt_modeset_calc_cdclk(struct drm_atomic_state *state)
27c329ed 6053{
4e5ca60f 6054 int max_pixclk = ilk_max_pixel_rate(state);
1a617b77
ML
6055 struct intel_atomic_state *intel_state =
6056 to_intel_atomic_state(state);
85a96e7a 6057
1a617b77 6058 intel_state->cdclk = intel_state->dev_cdclk =
324513c0 6059 bxt_calc_cdclk(max_pixclk);
85a96e7a 6060
1a617b77 6061 if (!intel_state->active_crtcs)
324513c0 6062 intel_state->dev_cdclk = bxt_calc_cdclk(0);
1a617b77 6063
27c329ed 6064 return 0;
30a970c6
JB
6065}
6066
1e69cd74
VS
6067static void vlv_program_pfi_credits(struct drm_i915_private *dev_priv)
6068{
6069 unsigned int credits, default_credits;
6070
6071 if (IS_CHERRYVIEW(dev_priv))
6072 default_credits = PFI_CREDIT(12);
6073 else
6074 default_credits = PFI_CREDIT(8);
6075
bfa7df01 6076 if (dev_priv->cdclk_freq >= dev_priv->czclk_freq) {
1e69cd74
VS
6077 /* CHV suggested value is 31 or 63 */
6078 if (IS_CHERRYVIEW(dev_priv))
fcc0008f 6079 credits = PFI_CREDIT_63;
1e69cd74
VS
6080 else
6081 credits = PFI_CREDIT(15);
6082 } else {
6083 credits = default_credits;
6084 }
6085
6086 /*
6087 * WA - write default credits before re-programming
6088 * FIXME: should we also set the resend bit here?
6089 */
6090 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
6091 default_credits);
6092
6093 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
6094 credits | PFI_CREDIT_RESEND);
6095
6096 /*
6097 * FIXME is this guaranteed to clear
6098 * immediately or should we poll for it?
6099 */
6100 WARN_ON(I915_READ(GCI_CONTROL) & PFI_CREDIT_RESEND);
6101}
6102
27c329ed 6103static void valleyview_modeset_commit_cdclk(struct drm_atomic_state *old_state)
30a970c6 6104{
a821fc46 6105 struct drm_device *dev = old_state->dev;
30a970c6 6106 struct drm_i915_private *dev_priv = dev->dev_private;
1a617b77
ML
6107 struct intel_atomic_state *old_intel_state =
6108 to_intel_atomic_state(old_state);
6109 unsigned req_cdclk = old_intel_state->dev_cdclk;
30a970c6 6110
27c329ed
ML
6111 /*
6112 * FIXME: We can end up here with all power domains off, yet
6113 * with a CDCLK frequency other than the minimum. To account
6114 * for this take the PIPE-A power domain, which covers the HW
6115 * blocks needed for the following programming. This can be
6116 * removed once it's guaranteed that we get here either with
6117 * the minimum CDCLK set, or the required power domains
6118 * enabled.
6119 */
6120 intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
738c05c0 6121
27c329ed
ML
6122 if (IS_CHERRYVIEW(dev))
6123 cherryview_set_cdclk(dev, req_cdclk);
6124 else
6125 valleyview_set_cdclk(dev, req_cdclk);
738c05c0 6126
27c329ed 6127 vlv_program_pfi_credits(dev_priv);
1e69cd74 6128
27c329ed 6129 intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
30a970c6
JB
6130}
6131
89b667f8
JB
6132static void valleyview_crtc_enable(struct drm_crtc *crtc)
6133{
6134 struct drm_device *dev = crtc->dev;
a72e4c9f 6135 struct drm_i915_private *dev_priv = to_i915(dev);
89b667f8
JB
6136 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6137 struct intel_encoder *encoder;
b95c5321
ML
6138 struct intel_crtc_state *pipe_config =
6139 to_intel_crtc_state(crtc->state);
89b667f8 6140 int pipe = intel_crtc->pipe;
89b667f8 6141
53d9f4e9 6142 if (WARN_ON(intel_crtc->active))
89b667f8
JB
6143 return;
6144
6e3c9717 6145 if (intel_crtc->config->has_dp_encoder)
fe3cd48d 6146 intel_dp_set_m_n(intel_crtc, M1_N1);
5b18e57c
DV
6147
6148 intel_set_pipe_timings(intel_crtc);
bc58be60 6149 intel_set_pipe_src_size(intel_crtc);
5b18e57c 6150
c14b0485
VS
6151 if (IS_CHERRYVIEW(dev) && pipe == PIPE_B) {
6152 struct drm_i915_private *dev_priv = dev->dev_private;
6153
6154 I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
6155 I915_WRITE(CHV_CANVAS(pipe), 0);
6156 }
6157
5b18e57c
DV
6158 i9xx_set_pipeconf(intel_crtc);
6159
89b667f8 6160 intel_crtc->active = true;
89b667f8 6161
a72e4c9f 6162 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4a3436e8 6163
89b667f8
JB
6164 for_each_encoder_on_crtc(dev, crtc, encoder)
6165 if (encoder->pre_pll_enable)
6166 encoder->pre_pll_enable(encoder);
6167
cd2d34d9
VS
6168 if (IS_CHERRYVIEW(dev)) {
6169 chv_prepare_pll(intel_crtc, intel_crtc->config);
6170 chv_enable_pll(intel_crtc, intel_crtc->config);
6171 } else {
6172 vlv_prepare_pll(intel_crtc, intel_crtc->config);
6173 vlv_enable_pll(intel_crtc, intel_crtc->config);
9d556c99 6174 }
89b667f8
JB
6175
6176 for_each_encoder_on_crtc(dev, crtc, encoder)
6177 if (encoder->pre_enable)
6178 encoder->pre_enable(encoder);
6179
2dd24552
JB
6180 i9xx_pfit_enable(intel_crtc);
6181
b95c5321 6182 intel_color_load_luts(&pipe_config->base);
63cbb074 6183
caed361d 6184 intel_update_watermarks(crtc);
e1fdc473 6185 intel_enable_pipe(intel_crtc);
be6a6f8e 6186
4b3a9526
VS
6187 assert_vblank_disabled(crtc);
6188 drm_crtc_vblank_on(crtc);
6189
f9b61ff6
DV
6190 for_each_encoder_on_crtc(dev, crtc, encoder)
6191 encoder->enable(encoder);
89b667f8
JB
6192}
6193
f13c2ef3
DV
6194static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
6195{
6196 struct drm_device *dev = crtc->base.dev;
6197 struct drm_i915_private *dev_priv = dev->dev_private;
6198
6e3c9717
ACO
6199 I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
6200 I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
f13c2ef3
DV
6201}
6202
0b8765c6 6203static void i9xx_crtc_enable(struct drm_crtc *crtc)
79e53945
JB
6204{
6205 struct drm_device *dev = crtc->dev;
a72e4c9f 6206 struct drm_i915_private *dev_priv = to_i915(dev);
79e53945 6207 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 6208 struct intel_encoder *encoder;
b95c5321
ML
6209 struct intel_crtc_state *pipe_config =
6210 to_intel_crtc_state(crtc->state);
cd2d34d9 6211 enum pipe pipe = intel_crtc->pipe;
79e53945 6212
53d9f4e9 6213 if (WARN_ON(intel_crtc->active))
f7abfe8b
CW
6214 return;
6215
f13c2ef3
DV
6216 i9xx_set_pll_dividers(intel_crtc);
6217
6e3c9717 6218 if (intel_crtc->config->has_dp_encoder)
fe3cd48d 6219 intel_dp_set_m_n(intel_crtc, M1_N1);
5b18e57c
DV
6220
6221 intel_set_pipe_timings(intel_crtc);
bc58be60 6222 intel_set_pipe_src_size(intel_crtc);
5b18e57c 6223
5b18e57c
DV
6224 i9xx_set_pipeconf(intel_crtc);
6225
f7abfe8b 6226 intel_crtc->active = true;
6b383a7f 6227
4a3436e8 6228 if (!IS_GEN2(dev))
a72e4c9f 6229 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4a3436e8 6230
9d6d9f19
MK
6231 for_each_encoder_on_crtc(dev, crtc, encoder)
6232 if (encoder->pre_enable)
6233 encoder->pre_enable(encoder);
6234
f6736a1a
DV
6235 i9xx_enable_pll(intel_crtc);
6236
2dd24552
JB
6237 i9xx_pfit_enable(intel_crtc);
6238
b95c5321 6239 intel_color_load_luts(&pipe_config->base);
63cbb074 6240
f37fcc2a 6241 intel_update_watermarks(crtc);
e1fdc473 6242 intel_enable_pipe(intel_crtc);
be6a6f8e 6243
4b3a9526
VS
6244 assert_vblank_disabled(crtc);
6245 drm_crtc_vblank_on(crtc);
6246
f9b61ff6
DV
6247 for_each_encoder_on_crtc(dev, crtc, encoder)
6248 encoder->enable(encoder);
0b8765c6 6249}
79e53945 6250
87476d63
DV
6251static void i9xx_pfit_disable(struct intel_crtc *crtc)
6252{
6253 struct drm_device *dev = crtc->base.dev;
6254 struct drm_i915_private *dev_priv = dev->dev_private;
87476d63 6255
6e3c9717 6256 if (!crtc->config->gmch_pfit.control)
328d8e82 6257 return;
87476d63 6258
328d8e82 6259 assert_pipe_disabled(dev_priv, crtc->pipe);
87476d63 6260
328d8e82
DV
6261 DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
6262 I915_READ(PFIT_CONTROL));
6263 I915_WRITE(PFIT_CONTROL, 0);
87476d63
DV
6264}
6265
0b8765c6
JB
6266static void i9xx_crtc_disable(struct drm_crtc *crtc)
6267{
6268 struct drm_device *dev = crtc->dev;
6269 struct drm_i915_private *dev_priv = dev->dev_private;
6270 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 6271 struct intel_encoder *encoder;
0b8765c6 6272 int pipe = intel_crtc->pipe;
ef9c3aee 6273
6304cd91
VS
6274 /*
6275 * On gen2 planes are double buffered but the pipe isn't, so we must
6276 * wait for planes to fully turn off before disabling the pipe.
6277 */
90e83e53
ACO
6278 if (IS_GEN2(dev))
6279 intel_wait_for_vblank(dev, pipe);
6304cd91 6280
4b3a9526
VS
6281 for_each_encoder_on_crtc(dev, crtc, encoder)
6282 encoder->disable(encoder);
6283
f9b61ff6
DV
6284 drm_crtc_vblank_off(crtc);
6285 assert_vblank_disabled(crtc);
6286
575f7ab7 6287 intel_disable_pipe(intel_crtc);
24a1f16d 6288
87476d63 6289 i9xx_pfit_disable(intel_crtc);
24a1f16d 6290
89b667f8
JB
6291 for_each_encoder_on_crtc(dev, crtc, encoder)
6292 if (encoder->post_disable)
6293 encoder->post_disable(encoder);
6294
a65347ba 6295 if (!intel_crtc->config->has_dsi_encoder) {
076ed3b2
CML
6296 if (IS_CHERRYVIEW(dev))
6297 chv_disable_pll(dev_priv, pipe);
6298 else if (IS_VALLEYVIEW(dev))
6299 vlv_disable_pll(dev_priv, pipe);
6300 else
1c4e0274 6301 i9xx_disable_pll(intel_crtc);
076ed3b2 6302 }
0b8765c6 6303
d6db995f
VS
6304 for_each_encoder_on_crtc(dev, crtc, encoder)
6305 if (encoder->post_pll_disable)
6306 encoder->post_pll_disable(encoder);
6307
4a3436e8 6308 if (!IS_GEN2(dev))
a72e4c9f 6309 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
0b8765c6
JB
6310}
6311
b17d48e2
ML
6312static void intel_crtc_disable_noatomic(struct drm_crtc *crtc)
6313{
842e0307 6314 struct intel_encoder *encoder;
b17d48e2
ML
6315 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6316 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
6317 enum intel_display_power_domain domain;
6318 unsigned long domains;
6319
6320 if (!intel_crtc->active)
6321 return;
6322
a539205a 6323 if (to_intel_plane_state(crtc->primary->state)->visible) {
5a21b665 6324 WARN_ON(intel_crtc->flip_work);
fc32b1fd 6325
2622a081 6326 intel_pre_disable_primary_noatomic(crtc);
54a41961
ML
6327
6328 intel_crtc_disable_planes(crtc, 1 << drm_plane_index(crtc->primary));
6329 to_intel_plane_state(crtc->primary->state)->visible = false;
a539205a
ML
6330 }
6331
b17d48e2 6332 dev_priv->display.crtc_disable(crtc);
842e0307 6333
78108b7c
VS
6334 DRM_DEBUG_KMS("[CRTC:%d:%s] hw state adjusted, was enabled, now disabled\n",
6335 crtc->base.id, crtc->name);
842e0307
ML
6336
6337 WARN_ON(drm_atomic_set_mode_for_crtc(crtc->state, NULL) < 0);
6338 crtc->state->active = false;
37d9078b 6339 intel_crtc->active = false;
842e0307
ML
6340 crtc->enabled = false;
6341 crtc->state->connector_mask = 0;
6342 crtc->state->encoder_mask = 0;
6343
6344 for_each_encoder_on_crtc(crtc->dev, crtc, encoder)
6345 encoder->base.crtc = NULL;
6346
58f9c0bc 6347 intel_fbc_disable(intel_crtc);
37d9078b 6348 intel_update_watermarks(crtc);
1f7457b1 6349 intel_disable_shared_dpll(intel_crtc);
b17d48e2
ML
6350
6351 domains = intel_crtc->enabled_power_domains;
6352 for_each_power_domain(domain, domains)
6353 intel_display_power_put(dev_priv, domain);
6354 intel_crtc->enabled_power_domains = 0;
565602d7
ML
6355
6356 dev_priv->active_crtcs &= ~(1 << intel_crtc->pipe);
6357 dev_priv->min_pixclk[intel_crtc->pipe] = 0;
b17d48e2
ML
6358}
6359
6b72d486
ML
6360/*
6361 * turn all crtc's off, but do not adjust state
6362 * This has to be paired with a call to intel_modeset_setup_hw_state.
6363 */
70e0bd74 6364int intel_display_suspend(struct drm_device *dev)
ee7b9f93 6365{
e2c8b870 6366 struct drm_i915_private *dev_priv = to_i915(dev);
70e0bd74 6367 struct drm_atomic_state *state;
e2c8b870 6368 int ret;
70e0bd74 6369
e2c8b870
ML
6370 state = drm_atomic_helper_suspend(dev);
6371 ret = PTR_ERR_OR_ZERO(state);
70e0bd74
ML
6372 if (ret)
6373 DRM_ERROR("Suspending crtc's failed with %i\n", ret);
e2c8b870
ML
6374 else
6375 dev_priv->modeset_restore_state = state;
70e0bd74 6376 return ret;
ee7b9f93
JB
6377}
6378
ea5b213a 6379void intel_encoder_destroy(struct drm_encoder *encoder)
7e7d76c3 6380{
4ef69c7a 6381 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
ea5b213a 6382
ea5b213a
CW
6383 drm_encoder_cleanup(encoder);
6384 kfree(intel_encoder);
7e7d76c3
JB
6385}
6386
0a91ca29
DV
6387/* Cross check the actual hw state with our own modeset state tracking (and it's
6388 * internal consistency). */
5a21b665 6389static void intel_connector_verify_state(struct intel_connector *connector)
79e53945 6390{
5a21b665 6391 struct drm_crtc *crtc = connector->base.state->crtc;
35dd3c64
ML
6392
6393 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
6394 connector->base.base.id,
6395 connector->base.name);
6396
0a91ca29 6397 if (connector->get_hw_state(connector)) {
e85376cb 6398 struct intel_encoder *encoder = connector->encoder;
5a21b665 6399 struct drm_connector_state *conn_state = connector->base.state;
0a91ca29 6400
35dd3c64
ML
6401 I915_STATE_WARN(!crtc,
6402 "connector enabled without attached crtc\n");
0a91ca29 6403
35dd3c64
ML
6404 if (!crtc)
6405 return;
6406
6407 I915_STATE_WARN(!crtc->state->active,
6408 "connector is active, but attached crtc isn't\n");
6409
e85376cb 6410 if (!encoder || encoder->type == INTEL_OUTPUT_DP_MST)
35dd3c64
ML
6411 return;
6412
e85376cb 6413 I915_STATE_WARN(conn_state->best_encoder != &encoder->base,
35dd3c64
ML
6414 "atomic encoder doesn't match attached encoder\n");
6415
e85376cb 6416 I915_STATE_WARN(conn_state->crtc != encoder->base.crtc,
35dd3c64
ML
6417 "attached encoder crtc differs from connector crtc\n");
6418 } else {
4d688a2a
ML
6419 I915_STATE_WARN(crtc && crtc->state->active,
6420 "attached crtc is active, but connector isn't\n");
5a21b665 6421 I915_STATE_WARN(!crtc && connector->base.state->best_encoder,
35dd3c64 6422 "best encoder set without crtc!\n");
0a91ca29 6423 }
79e53945
JB
6424}
6425
08d9bc92
ACO
6426int intel_connector_init(struct intel_connector *connector)
6427{
5350a031 6428 drm_atomic_helper_connector_reset(&connector->base);
08d9bc92 6429
5350a031 6430 if (!connector->base.state)
08d9bc92
ACO
6431 return -ENOMEM;
6432
08d9bc92
ACO
6433 return 0;
6434}
6435
6436struct intel_connector *intel_connector_alloc(void)
6437{
6438 struct intel_connector *connector;
6439
6440 connector = kzalloc(sizeof *connector, GFP_KERNEL);
6441 if (!connector)
6442 return NULL;
6443
6444 if (intel_connector_init(connector) < 0) {
6445 kfree(connector);
6446 return NULL;
6447 }
6448
6449 return connector;
6450}
6451
f0947c37
DV
6452/* Simple connector->get_hw_state implementation for encoders that support only
6453 * one connector and no cloning and hence the encoder state determines the state
6454 * of the connector. */
6455bool intel_connector_get_hw_state(struct intel_connector *connector)
ea5b213a 6456{
24929352 6457 enum pipe pipe = 0;
f0947c37 6458 struct intel_encoder *encoder = connector->encoder;
ea5b213a 6459
f0947c37 6460 return encoder->get_hw_state(encoder, &pipe);
ea5b213a
CW
6461}
6462
6d293983 6463static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
d272ddfa 6464{
6d293983
ACO
6465 if (crtc_state->base.enable && crtc_state->has_pch_encoder)
6466 return crtc_state->fdi_lanes;
d272ddfa
VS
6467
6468 return 0;
6469}
6470
6d293983 6471static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
5cec258b 6472 struct intel_crtc_state *pipe_config)
1857e1da 6473{
6d293983
ACO
6474 struct drm_atomic_state *state = pipe_config->base.state;
6475 struct intel_crtc *other_crtc;
6476 struct intel_crtc_state *other_crtc_state;
6477
1857e1da
DV
6478 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
6479 pipe_name(pipe), pipe_config->fdi_lanes);
6480 if (pipe_config->fdi_lanes > 4) {
6481 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
6482 pipe_name(pipe), pipe_config->fdi_lanes);
6d293983 6483 return -EINVAL;
1857e1da
DV
6484 }
6485
bafb6553 6486 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
1857e1da
DV
6487 if (pipe_config->fdi_lanes > 2) {
6488 DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
6489 pipe_config->fdi_lanes);
6d293983 6490 return -EINVAL;
1857e1da 6491 } else {
6d293983 6492 return 0;
1857e1da
DV
6493 }
6494 }
6495
6496 if (INTEL_INFO(dev)->num_pipes == 2)
6d293983 6497 return 0;
1857e1da
DV
6498
6499 /* Ivybridge 3 pipe is really complicated */
6500 switch (pipe) {
6501 case PIPE_A:
6d293983 6502 return 0;
1857e1da 6503 case PIPE_B:
6d293983
ACO
6504 if (pipe_config->fdi_lanes <= 2)
6505 return 0;
6506
6507 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_C));
6508 other_crtc_state =
6509 intel_atomic_get_crtc_state(state, other_crtc);
6510 if (IS_ERR(other_crtc_state))
6511 return PTR_ERR(other_crtc_state);
6512
6513 if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
1857e1da
DV
6514 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
6515 pipe_name(pipe), pipe_config->fdi_lanes);
6d293983 6516 return -EINVAL;
1857e1da 6517 }
6d293983 6518 return 0;
1857e1da 6519 case PIPE_C:
251cc67c
VS
6520 if (pipe_config->fdi_lanes > 2) {
6521 DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
6522 pipe_name(pipe), pipe_config->fdi_lanes);
6d293983 6523 return -EINVAL;
251cc67c 6524 }
6d293983
ACO
6525
6526 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_B));
6527 other_crtc_state =
6528 intel_atomic_get_crtc_state(state, other_crtc);
6529 if (IS_ERR(other_crtc_state))
6530 return PTR_ERR(other_crtc_state);
6531
6532 if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
1857e1da 6533 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
6d293983 6534 return -EINVAL;
1857e1da 6535 }
6d293983 6536 return 0;
1857e1da
DV
6537 default:
6538 BUG();
6539 }
6540}
6541
e29c22c0
DV
6542#define RETRY 1
6543static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
5cec258b 6544 struct intel_crtc_state *pipe_config)
877d48d5 6545{
1857e1da 6546 struct drm_device *dev = intel_crtc->base.dev;
7c5f93b0 6547 const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
6d293983
ACO
6548 int lane, link_bw, fdi_dotclock, ret;
6549 bool needs_recompute = false;
877d48d5 6550
e29c22c0 6551retry:
877d48d5
DV
6552 /* FDI is a binary signal running at ~2.7GHz, encoding
6553 * each output octet as 10 bits. The actual frequency
6554 * is stored as a divider into a 100MHz clock, and the
6555 * mode pixel clock is stored in units of 1KHz.
6556 * Hence the bw of each lane in terms of the mode signal
6557 * is:
6558 */
21a727b3 6559 link_bw = intel_fdi_link_freq(to_i915(dev), pipe_config);
877d48d5 6560
241bfc38 6561 fdi_dotclock = adjusted_mode->crtc_clock;
877d48d5 6562
2bd89a07 6563 lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
877d48d5
DV
6564 pipe_config->pipe_bpp);
6565
6566 pipe_config->fdi_lanes = lane;
6567
2bd89a07 6568 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
877d48d5 6569 link_bw, &pipe_config->fdi_m_n);
1857e1da 6570
e3b247da 6571 ret = ironlake_check_fdi_lanes(dev, intel_crtc->pipe, pipe_config);
6d293983 6572 if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
e29c22c0
DV
6573 pipe_config->pipe_bpp -= 2*3;
6574 DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
6575 pipe_config->pipe_bpp);
6576 needs_recompute = true;
6577 pipe_config->bw_constrained = true;
6578
6579 goto retry;
6580 }
6581
6582 if (needs_recompute)
6583 return RETRY;
6584
6d293983 6585 return ret;
877d48d5
DV
6586}
6587
8cfb3407
VS
6588static bool pipe_config_supports_ips(struct drm_i915_private *dev_priv,
6589 struct intel_crtc_state *pipe_config)
6590{
6591 if (pipe_config->pipe_bpp > 24)
6592 return false;
6593
6594 /* HSW can handle pixel rate up to cdclk? */
2d1fe073 6595 if (IS_HASWELL(dev_priv))
8cfb3407
VS
6596 return true;
6597
6598 /*
b432e5cf
VS
6599 * We compare against max which means we must take
6600 * the increased cdclk requirement into account when
6601 * calculating the new cdclk.
6602 *
6603 * Should measure whether using a lower cdclk w/o IPS
8cfb3407
VS
6604 */
6605 return ilk_pipe_pixel_rate(pipe_config) <=
6606 dev_priv->max_cdclk_freq * 95 / 100;
6607}
6608
42db64ef 6609static void hsw_compute_ips_config(struct intel_crtc *crtc,
5cec258b 6610 struct intel_crtc_state *pipe_config)
42db64ef 6611{
8cfb3407
VS
6612 struct drm_device *dev = crtc->base.dev;
6613 struct drm_i915_private *dev_priv = dev->dev_private;
6614
d330a953 6615 pipe_config->ips_enabled = i915.enable_ips &&
8cfb3407
VS
6616 hsw_crtc_supports_ips(crtc) &&
6617 pipe_config_supports_ips(dev_priv, pipe_config);
42db64ef
PZ
6618}
6619
39acb4aa
VS
6620static bool intel_crtc_supports_double_wide(const struct intel_crtc *crtc)
6621{
6622 const struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6623
6624 /* GDG double wide on either pipe, otherwise pipe A only */
6625 return INTEL_INFO(dev_priv)->gen < 4 &&
6626 (crtc->pipe == PIPE_A || IS_I915G(dev_priv));
6627}
6628
a43f6e0f 6629static int intel_crtc_compute_config(struct intel_crtc *crtc,
5cec258b 6630 struct intel_crtc_state *pipe_config)
79e53945 6631{
a43f6e0f 6632 struct drm_device *dev = crtc->base.dev;
8bd31e67 6633 struct drm_i915_private *dev_priv = dev->dev_private;
7c5f93b0 6634 const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
f3261156 6635 int clock_limit = dev_priv->max_dotclk_freq;
89749350 6636
cf532bb2 6637 if (INTEL_INFO(dev)->gen < 4) {
f3261156 6638 clock_limit = dev_priv->max_cdclk_freq * 9 / 10;
cf532bb2
VS
6639
6640 /*
39acb4aa 6641 * Enable double wide mode when the dot clock
cf532bb2 6642 * is > 90% of the (display) core speed.
cf532bb2 6643 */
39acb4aa
VS
6644 if (intel_crtc_supports_double_wide(crtc) &&
6645 adjusted_mode->crtc_clock > clock_limit) {
f3261156 6646 clock_limit = dev_priv->max_dotclk_freq;
cf532bb2 6647 pipe_config->double_wide = true;
ad3a4479 6648 }
f3261156 6649 }
ad3a4479 6650
f3261156
VS
6651 if (adjusted_mode->crtc_clock > clock_limit) {
6652 DRM_DEBUG_KMS("requested pixel clock (%d kHz) too high (max: %d kHz, double wide: %s)\n",
6653 adjusted_mode->crtc_clock, clock_limit,
6654 yesno(pipe_config->double_wide));
6655 return -EINVAL;
2c07245f 6656 }
89749350 6657
1d1d0e27
VS
6658 /*
6659 * Pipe horizontal size must be even in:
6660 * - DVO ganged mode
6661 * - LVDS dual channel mode
6662 * - Double wide pipe
6663 */
a93e255f 6664 if ((intel_pipe_will_have_type(pipe_config, INTEL_OUTPUT_LVDS) &&
1d1d0e27
VS
6665 intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
6666 pipe_config->pipe_src_w &= ~1;
6667
8693a824
DL
6668 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
6669 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
44f46b42
CW
6670 */
6671 if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
aad941d5 6672 adjusted_mode->crtc_hsync_start == adjusted_mode->crtc_hdisplay)
e29c22c0 6673 return -EINVAL;
44f46b42 6674
f5adf94e 6675 if (HAS_IPS(dev))
a43f6e0f
DV
6676 hsw_compute_ips_config(crtc, pipe_config);
6677
877d48d5 6678 if (pipe_config->has_pch_encoder)
a43f6e0f 6679 return ironlake_fdi_compute_config(crtc, pipe_config);
877d48d5 6680
cf5a15be 6681 return 0;
79e53945
JB
6682}
6683
1652d19e
VS
6684static int skylake_get_display_clock_speed(struct drm_device *dev)
6685{
6686 struct drm_i915_private *dev_priv = to_i915(dev);
ea61791e 6687 uint32_t cdctl;
1652d19e 6688
ea61791e 6689 skl_dpll0_update(dev_priv);
1652d19e 6690
63911d72 6691 if (dev_priv->cdclk_pll.vco == 0)
709e05c3 6692 return dev_priv->cdclk_pll.ref;
1652d19e 6693
ea61791e 6694 cdctl = I915_READ(CDCLK_CTL);
1652d19e 6695
63911d72 6696 if (dev_priv->cdclk_pll.vco == 8640000) {
1652d19e
VS
6697 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6698 case CDCLK_FREQ_450_432:
6699 return 432000;
6700 case CDCLK_FREQ_337_308:
487ed2e4 6701 return 308571;
ea61791e
VS
6702 case CDCLK_FREQ_540:
6703 return 540000;
1652d19e 6704 case CDCLK_FREQ_675_617:
487ed2e4 6705 return 617143;
1652d19e 6706 default:
ea61791e 6707 MISSING_CASE(cdctl & CDCLK_FREQ_SEL_MASK);
1652d19e
VS
6708 }
6709 } else {
1652d19e
VS
6710 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6711 case CDCLK_FREQ_450_432:
6712 return 450000;
6713 case CDCLK_FREQ_337_308:
6714 return 337500;
ea61791e
VS
6715 case CDCLK_FREQ_540:
6716 return 540000;
1652d19e
VS
6717 case CDCLK_FREQ_675_617:
6718 return 675000;
6719 default:
ea61791e 6720 MISSING_CASE(cdctl & CDCLK_FREQ_SEL_MASK);
1652d19e
VS
6721 }
6722 }
6723
709e05c3 6724 return dev_priv->cdclk_pll.ref;
1652d19e
VS
6725}
6726
83d7c81f
VS
6727static void bxt_de_pll_update(struct drm_i915_private *dev_priv)
6728{
6729 u32 val;
6730
6731 dev_priv->cdclk_pll.ref = 19200;
1c3f7700 6732 dev_priv->cdclk_pll.vco = 0;
83d7c81f
VS
6733
6734 val = I915_READ(BXT_DE_PLL_ENABLE);
1c3f7700 6735 if ((val & BXT_DE_PLL_PLL_ENABLE) == 0)
83d7c81f 6736 return;
83d7c81f 6737
1c3f7700
ID
6738 if (WARN_ON((val & BXT_DE_PLL_LOCK) == 0))
6739 return;
83d7c81f
VS
6740
6741 val = I915_READ(BXT_DE_PLL_CTL);
6742 dev_priv->cdclk_pll.vco = (val & BXT_DE_PLL_RATIO_MASK) *
6743 dev_priv->cdclk_pll.ref;
6744}
6745
acd3f3d3
BP
6746static int broxton_get_display_clock_speed(struct drm_device *dev)
6747{
6748 struct drm_i915_private *dev_priv = to_i915(dev);
f5986242
VS
6749 u32 divider;
6750 int div, vco;
acd3f3d3 6751
83d7c81f
VS
6752 bxt_de_pll_update(dev_priv);
6753
f5986242
VS
6754 vco = dev_priv->cdclk_pll.vco;
6755 if (vco == 0)
6756 return dev_priv->cdclk_pll.ref;
acd3f3d3 6757
f5986242 6758 divider = I915_READ(CDCLK_CTL) & BXT_CDCLK_CD2X_DIV_SEL_MASK;
acd3f3d3 6759
f5986242 6760 switch (divider) {
acd3f3d3 6761 case BXT_CDCLK_CD2X_DIV_SEL_1:
f5986242
VS
6762 div = 2;
6763 break;
acd3f3d3 6764 case BXT_CDCLK_CD2X_DIV_SEL_1_5:
f5986242
VS
6765 div = 3;
6766 break;
acd3f3d3 6767 case BXT_CDCLK_CD2X_DIV_SEL_2:
f5986242
VS
6768 div = 4;
6769 break;
acd3f3d3 6770 case BXT_CDCLK_CD2X_DIV_SEL_4:
f5986242
VS
6771 div = 8;
6772 break;
6773 default:
6774 MISSING_CASE(divider);
6775 return dev_priv->cdclk_pll.ref;
acd3f3d3
BP
6776 }
6777
f5986242 6778 return DIV_ROUND_CLOSEST(vco, div);
acd3f3d3
BP
6779}
6780
1652d19e
VS
6781static int broadwell_get_display_clock_speed(struct drm_device *dev)
6782{
6783 struct drm_i915_private *dev_priv = dev->dev_private;
6784 uint32_t lcpll = I915_READ(LCPLL_CTL);
6785 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6786
6787 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6788 return 800000;
6789 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6790 return 450000;
6791 else if (freq == LCPLL_CLK_FREQ_450)
6792 return 450000;
6793 else if (freq == LCPLL_CLK_FREQ_54O_BDW)
6794 return 540000;
6795 else if (freq == LCPLL_CLK_FREQ_337_5_BDW)
6796 return 337500;
6797 else
6798 return 675000;
6799}
6800
6801static int haswell_get_display_clock_speed(struct drm_device *dev)
6802{
6803 struct drm_i915_private *dev_priv = dev->dev_private;
6804 uint32_t lcpll = I915_READ(LCPLL_CTL);
6805 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6806
6807 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6808 return 800000;
6809 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6810 return 450000;
6811 else if (freq == LCPLL_CLK_FREQ_450)
6812 return 450000;
6813 else if (IS_HSW_ULT(dev))
6814 return 337500;
6815 else
6816 return 540000;
79e53945
JB
6817}
6818
25eb05fc
JB
6819static int valleyview_get_display_clock_speed(struct drm_device *dev)
6820{
bfa7df01
VS
6821 return vlv_get_cck_clock_hpll(to_i915(dev), "cdclk",
6822 CCK_DISPLAY_CLOCK_CONTROL);
25eb05fc
JB
6823}
6824
b37a6434
VS
6825static int ilk_get_display_clock_speed(struct drm_device *dev)
6826{
6827 return 450000;
6828}
6829
e70236a8
JB
6830static int i945_get_display_clock_speed(struct drm_device *dev)
6831{
6832 return 400000;
6833}
79e53945 6834
e70236a8 6835static int i915_get_display_clock_speed(struct drm_device *dev)
79e53945 6836{
e907f170 6837 return 333333;
e70236a8 6838}
79e53945 6839
e70236a8
JB
6840static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
6841{
6842 return 200000;
6843}
79e53945 6844
257a7ffc
DV
6845static int pnv_get_display_clock_speed(struct drm_device *dev)
6846{
6847 u16 gcfgc = 0;
6848
6849 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6850
6851 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6852 case GC_DISPLAY_CLOCK_267_MHZ_PNV:
e907f170 6853 return 266667;
257a7ffc 6854 case GC_DISPLAY_CLOCK_333_MHZ_PNV:
e907f170 6855 return 333333;
257a7ffc 6856 case GC_DISPLAY_CLOCK_444_MHZ_PNV:
e907f170 6857 return 444444;
257a7ffc
DV
6858 case GC_DISPLAY_CLOCK_200_MHZ_PNV:
6859 return 200000;
6860 default:
6861 DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
6862 case GC_DISPLAY_CLOCK_133_MHZ_PNV:
e907f170 6863 return 133333;
257a7ffc 6864 case GC_DISPLAY_CLOCK_167_MHZ_PNV:
e907f170 6865 return 166667;
257a7ffc
DV
6866 }
6867}
6868
e70236a8
JB
6869static int i915gm_get_display_clock_speed(struct drm_device *dev)
6870{
6871 u16 gcfgc = 0;
79e53945 6872
e70236a8
JB
6873 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6874
6875 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
e907f170 6876 return 133333;
e70236a8
JB
6877 else {
6878 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6879 case GC_DISPLAY_CLOCK_333_MHZ:
e907f170 6880 return 333333;
e70236a8
JB
6881 default:
6882 case GC_DISPLAY_CLOCK_190_200_MHZ:
6883 return 190000;
79e53945 6884 }
e70236a8
JB
6885 }
6886}
6887
6888static int i865_get_display_clock_speed(struct drm_device *dev)
6889{
e907f170 6890 return 266667;
e70236a8
JB
6891}
6892
1b1d2716 6893static int i85x_get_display_clock_speed(struct drm_device *dev)
e70236a8
JB
6894{
6895 u16 hpllcc = 0;
1b1d2716 6896
65cd2b3f
VS
6897 /*
6898 * 852GM/852GMV only supports 133 MHz and the HPLLCC
6899 * encoding is different :(
6900 * FIXME is this the right way to detect 852GM/852GMV?
6901 */
6902 if (dev->pdev->revision == 0x1)
6903 return 133333;
6904
1b1d2716
VS
6905 pci_bus_read_config_word(dev->pdev->bus,
6906 PCI_DEVFN(0, 3), HPLLCC, &hpllcc);
6907
e70236a8
JB
6908 /* Assume that the hardware is in the high speed state. This
6909 * should be the default.
6910 */
6911 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
6912 case GC_CLOCK_133_200:
1b1d2716 6913 case GC_CLOCK_133_200_2:
e70236a8
JB
6914 case GC_CLOCK_100_200:
6915 return 200000;
6916 case GC_CLOCK_166_250:
6917 return 250000;
6918 case GC_CLOCK_100_133:
e907f170 6919 return 133333;
1b1d2716
VS
6920 case GC_CLOCK_133_266:
6921 case GC_CLOCK_133_266_2:
6922 case GC_CLOCK_166_266:
6923 return 266667;
e70236a8 6924 }
79e53945 6925
e70236a8
JB
6926 /* Shouldn't happen */
6927 return 0;
6928}
79e53945 6929
e70236a8
JB
6930static int i830_get_display_clock_speed(struct drm_device *dev)
6931{
e907f170 6932 return 133333;
79e53945
JB
6933}
6934
34edce2f
VS
6935static unsigned int intel_hpll_vco(struct drm_device *dev)
6936{
6937 struct drm_i915_private *dev_priv = dev->dev_private;
6938 static const unsigned int blb_vco[8] = {
6939 [0] = 3200000,
6940 [1] = 4000000,
6941 [2] = 5333333,
6942 [3] = 4800000,
6943 [4] = 6400000,
6944 };
6945 static const unsigned int pnv_vco[8] = {
6946 [0] = 3200000,
6947 [1] = 4000000,
6948 [2] = 5333333,
6949 [3] = 4800000,
6950 [4] = 2666667,
6951 };
6952 static const unsigned int cl_vco[8] = {
6953 [0] = 3200000,
6954 [1] = 4000000,
6955 [2] = 5333333,
6956 [3] = 6400000,
6957 [4] = 3333333,
6958 [5] = 3566667,
6959 [6] = 4266667,
6960 };
6961 static const unsigned int elk_vco[8] = {
6962 [0] = 3200000,
6963 [1] = 4000000,
6964 [2] = 5333333,
6965 [3] = 4800000,
6966 };
6967 static const unsigned int ctg_vco[8] = {
6968 [0] = 3200000,
6969 [1] = 4000000,
6970 [2] = 5333333,
6971 [3] = 6400000,
6972 [4] = 2666667,
6973 [5] = 4266667,
6974 };
6975 const unsigned int *vco_table;
6976 unsigned int vco;
6977 uint8_t tmp = 0;
6978
6979 /* FIXME other chipsets? */
6980 if (IS_GM45(dev))
6981 vco_table = ctg_vco;
6982 else if (IS_G4X(dev))
6983 vco_table = elk_vco;
6984 else if (IS_CRESTLINE(dev))
6985 vco_table = cl_vco;
6986 else if (IS_PINEVIEW(dev))
6987 vco_table = pnv_vco;
6988 else if (IS_G33(dev))
6989 vco_table = blb_vco;
6990 else
6991 return 0;
6992
6993 tmp = I915_READ(IS_MOBILE(dev) ? HPLLVCO_MOBILE : HPLLVCO);
6994
6995 vco = vco_table[tmp & 0x7];
6996 if (vco == 0)
6997 DRM_ERROR("Bad HPLL VCO (HPLLVCO=0x%02x)\n", tmp);
6998 else
6999 DRM_DEBUG_KMS("HPLL VCO %u kHz\n", vco);
7000
7001 return vco;
7002}
7003
7004static int gm45_get_display_clock_speed(struct drm_device *dev)
7005{
7006 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
7007 uint16_t tmp = 0;
7008
7009 pci_read_config_word(dev->pdev, GCFGC, &tmp);
7010
7011 cdclk_sel = (tmp >> 12) & 0x1;
7012
7013 switch (vco) {
7014 case 2666667:
7015 case 4000000:
7016 case 5333333:
7017 return cdclk_sel ? 333333 : 222222;
7018 case 3200000:
7019 return cdclk_sel ? 320000 : 228571;
7020 default:
7021 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u, CFGC=0x%04x\n", vco, tmp);
7022 return 222222;
7023 }
7024}
7025
7026static int i965gm_get_display_clock_speed(struct drm_device *dev)
7027{
7028 static const uint8_t div_3200[] = { 16, 10, 8 };
7029 static const uint8_t div_4000[] = { 20, 12, 10 };
7030 static const uint8_t div_5333[] = { 24, 16, 14 };
7031 const uint8_t *div_table;
7032 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
7033 uint16_t tmp = 0;
7034
7035 pci_read_config_word(dev->pdev, GCFGC, &tmp);
7036
7037 cdclk_sel = ((tmp >> 8) & 0x1f) - 1;
7038
7039 if (cdclk_sel >= ARRAY_SIZE(div_3200))
7040 goto fail;
7041
7042 switch (vco) {
7043 case 3200000:
7044 div_table = div_3200;
7045 break;
7046 case 4000000:
7047 div_table = div_4000;
7048 break;
7049 case 5333333:
7050 div_table = div_5333;
7051 break;
7052 default:
7053 goto fail;
7054 }
7055
7056 return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
7057
caf4e252 7058fail:
34edce2f
VS
7059 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%04x\n", vco, tmp);
7060 return 200000;
7061}
7062
7063static int g33_get_display_clock_speed(struct drm_device *dev)
7064{
7065 static const uint8_t div_3200[] = { 12, 10, 8, 7, 5, 16 };
7066 static const uint8_t div_4000[] = { 14, 12, 10, 8, 6, 20 };
7067 static const uint8_t div_4800[] = { 20, 14, 12, 10, 8, 24 };
7068 static const uint8_t div_5333[] = { 20, 16, 12, 12, 8, 28 };
7069 const uint8_t *div_table;
7070 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
7071 uint16_t tmp = 0;
7072
7073 pci_read_config_word(dev->pdev, GCFGC, &tmp);
7074
7075 cdclk_sel = (tmp >> 4) & 0x7;
7076
7077 if (cdclk_sel >= ARRAY_SIZE(div_3200))
7078 goto fail;
7079
7080 switch (vco) {
7081 case 3200000:
7082 div_table = div_3200;
7083 break;
7084 case 4000000:
7085 div_table = div_4000;
7086 break;
7087 case 4800000:
7088 div_table = div_4800;
7089 break;
7090 case 5333333:
7091 div_table = div_5333;
7092 break;
7093 default:
7094 goto fail;
7095 }
7096
7097 return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
7098
caf4e252 7099fail:
34edce2f
VS
7100 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%08x\n", vco, tmp);
7101 return 190476;
7102}
7103
2c07245f 7104static void
a65851af 7105intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
2c07245f 7106{
a65851af
VS
7107 while (*num > DATA_LINK_M_N_MASK ||
7108 *den > DATA_LINK_M_N_MASK) {
2c07245f
ZW
7109 *num >>= 1;
7110 *den >>= 1;
7111 }
7112}
7113
a65851af
VS
7114static void compute_m_n(unsigned int m, unsigned int n,
7115 uint32_t *ret_m, uint32_t *ret_n)
7116{
7117 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
7118 *ret_m = div_u64((uint64_t) m * *ret_n, n);
7119 intel_reduce_m_n_ratio(ret_m, ret_n);
7120}
7121
e69d0bc1
DV
7122void
7123intel_link_compute_m_n(int bits_per_pixel, int nlanes,
7124 int pixel_clock, int link_clock,
7125 struct intel_link_m_n *m_n)
2c07245f 7126{
e69d0bc1 7127 m_n->tu = 64;
a65851af
VS
7128
7129 compute_m_n(bits_per_pixel * pixel_clock,
7130 link_clock * nlanes * 8,
7131 &m_n->gmch_m, &m_n->gmch_n);
7132
7133 compute_m_n(pixel_clock, link_clock,
7134 &m_n->link_m, &m_n->link_n);
2c07245f
ZW
7135}
7136
a7615030
CW
7137static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
7138{
d330a953
JN
7139 if (i915.panel_use_ssc >= 0)
7140 return i915.panel_use_ssc != 0;
41aa3448 7141 return dev_priv->vbt.lvds_use_ssc
435793df 7142 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
a7615030
CW
7143}
7144
7429e9d4 7145static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
c65d77d8 7146{
7df00d7a 7147 return (1 << dpll->n) << 16 | dpll->m2;
7429e9d4 7148}
f47709a9 7149
7429e9d4
DV
7150static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
7151{
7152 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
c65d77d8
JB
7153}
7154
f47709a9 7155static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
190f68c5 7156 struct intel_crtc_state *crtc_state,
9e2c8475 7157 struct dpll *reduced_clock)
a7516a05 7158{
f47709a9 7159 struct drm_device *dev = crtc->base.dev;
a7516a05
JB
7160 u32 fp, fp2 = 0;
7161
7162 if (IS_PINEVIEW(dev)) {
190f68c5 7163 fp = pnv_dpll_compute_fp(&crtc_state->dpll);
a7516a05 7164 if (reduced_clock)
7429e9d4 7165 fp2 = pnv_dpll_compute_fp(reduced_clock);
a7516a05 7166 } else {
190f68c5 7167 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
a7516a05 7168 if (reduced_clock)
7429e9d4 7169 fp2 = i9xx_dpll_compute_fp(reduced_clock);
a7516a05
JB
7170 }
7171
190f68c5 7172 crtc_state->dpll_hw_state.fp0 = fp;
a7516a05 7173
f47709a9 7174 crtc->lowfreq_avail = false;
a93e255f 7175 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
ab585dea 7176 reduced_clock) {
190f68c5 7177 crtc_state->dpll_hw_state.fp1 = fp2;
f47709a9 7178 crtc->lowfreq_avail = true;
a7516a05 7179 } else {
190f68c5 7180 crtc_state->dpll_hw_state.fp1 = fp;
a7516a05
JB
7181 }
7182}
7183
5e69f97f
CML
7184static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
7185 pipe)
89b667f8
JB
7186{
7187 u32 reg_val;
7188
7189 /*
7190 * PLLB opamp always calibrates to max value of 0x3f, force enable it
7191 * and set it to a reasonable value instead.
7192 */
ab3c759a 7193 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
89b667f8
JB
7194 reg_val &= 0xffffff00;
7195 reg_val |= 0x00000030;
ab3c759a 7196 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
89b667f8 7197
ab3c759a 7198 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
89b667f8
JB
7199 reg_val &= 0x8cffffff;
7200 reg_val = 0x8c000000;
ab3c759a 7201 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
89b667f8 7202
ab3c759a 7203 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
89b667f8 7204 reg_val &= 0xffffff00;
ab3c759a 7205 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
89b667f8 7206
ab3c759a 7207 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
89b667f8
JB
7208 reg_val &= 0x00ffffff;
7209 reg_val |= 0xb0000000;
ab3c759a 7210 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
89b667f8
JB
7211}
7212
b551842d
DV
7213static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
7214 struct intel_link_m_n *m_n)
7215{
7216 struct drm_device *dev = crtc->base.dev;
7217 struct drm_i915_private *dev_priv = dev->dev_private;
7218 int pipe = crtc->pipe;
7219
e3b95f1e
DV
7220 I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7221 I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
7222 I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
7223 I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
b551842d
DV
7224}
7225
7226static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
f769cd24
VK
7227 struct intel_link_m_n *m_n,
7228 struct intel_link_m_n *m2_n2)
b551842d
DV
7229{
7230 struct drm_device *dev = crtc->base.dev;
7231 struct drm_i915_private *dev_priv = dev->dev_private;
7232 int pipe = crtc->pipe;
6e3c9717 7233 enum transcoder transcoder = crtc->config->cpu_transcoder;
b551842d
DV
7234
7235 if (INTEL_INFO(dev)->gen >= 5) {
7236 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
7237 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
7238 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
7239 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
f769cd24
VK
7240 /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
7241 * for gen < 8) and if DRRS is supported (to make sure the
7242 * registers are not unnecessarily accessed).
7243 */
44395bfe 7244 if (m2_n2 && (IS_CHERRYVIEW(dev) || INTEL_INFO(dev)->gen < 8) &&
6e3c9717 7245 crtc->config->has_drrs) {
f769cd24
VK
7246 I915_WRITE(PIPE_DATA_M2(transcoder),
7247 TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
7248 I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
7249 I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
7250 I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
7251 }
b551842d 7252 } else {
e3b95f1e
DV
7253 I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7254 I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
7255 I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
7256 I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
b551842d
DV
7257 }
7258}
7259
fe3cd48d 7260void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
03afc4a2 7261{
fe3cd48d
R
7262 struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
7263
7264 if (m_n == M1_N1) {
7265 dp_m_n = &crtc->config->dp_m_n;
7266 dp_m2_n2 = &crtc->config->dp_m2_n2;
7267 } else if (m_n == M2_N2) {
7268
7269 /*
7270 * M2_N2 registers are not supported. Hence m2_n2 divider value
7271 * needs to be programmed into M1_N1.
7272 */
7273 dp_m_n = &crtc->config->dp_m2_n2;
7274 } else {
7275 DRM_ERROR("Unsupported divider value\n");
7276 return;
7277 }
7278
6e3c9717
ACO
7279 if (crtc->config->has_pch_encoder)
7280 intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
03afc4a2 7281 else
fe3cd48d 7282 intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
03afc4a2
DV
7283}
7284
251ac862
DV
7285static void vlv_compute_dpll(struct intel_crtc *crtc,
7286 struct intel_crtc_state *pipe_config)
bdd4b6a6 7287{
03ed5cbf 7288 pipe_config->dpll_hw_state.dpll = DPLL_INTEGRATED_REF_CLK_VLV |
cd2d34d9 7289 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
03ed5cbf
VS
7290 if (crtc->pipe != PIPE_A)
7291 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
bdd4b6a6 7292
cd2d34d9 7293 /* DPLL not used with DSI, but still need the rest set up */
187a1c07 7294 if (!pipe_config->has_dsi_encoder)
cd2d34d9
VS
7295 pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE |
7296 DPLL_EXT_BUFFER_ENABLE_VLV;
7297
03ed5cbf
VS
7298 pipe_config->dpll_hw_state.dpll_md =
7299 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
7300}
bdd4b6a6 7301
03ed5cbf
VS
7302static void chv_compute_dpll(struct intel_crtc *crtc,
7303 struct intel_crtc_state *pipe_config)
7304{
7305 pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLK_CHV |
cd2d34d9 7306 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
03ed5cbf
VS
7307 if (crtc->pipe != PIPE_A)
7308 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
7309
cd2d34d9 7310 /* DPLL not used with DSI, but still need the rest set up */
187a1c07 7311 if (!pipe_config->has_dsi_encoder)
cd2d34d9
VS
7312 pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE;
7313
03ed5cbf
VS
7314 pipe_config->dpll_hw_state.dpll_md =
7315 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
bdd4b6a6
DV
7316}
7317
d288f65f 7318static void vlv_prepare_pll(struct intel_crtc *crtc,
5cec258b 7319 const struct intel_crtc_state *pipe_config)
a0c4da24 7320{
f47709a9 7321 struct drm_device *dev = crtc->base.dev;
a0c4da24 7322 struct drm_i915_private *dev_priv = dev->dev_private;
cd2d34d9 7323 enum pipe pipe = crtc->pipe;
bdd4b6a6 7324 u32 mdiv;
a0c4da24 7325 u32 bestn, bestm1, bestm2, bestp1, bestp2;
bdd4b6a6 7326 u32 coreclk, reg_val;
a0c4da24 7327
cd2d34d9
VS
7328 /* Enable Refclk */
7329 I915_WRITE(DPLL(pipe),
7330 pipe_config->dpll_hw_state.dpll &
7331 ~(DPLL_VCO_ENABLE | DPLL_EXT_BUFFER_ENABLE_VLV));
7332
7333 /* No need to actually set up the DPLL with DSI */
7334 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
7335 return;
7336
a580516d 7337 mutex_lock(&dev_priv->sb_lock);
09153000 7338
d288f65f
VS
7339 bestn = pipe_config->dpll.n;
7340 bestm1 = pipe_config->dpll.m1;
7341 bestm2 = pipe_config->dpll.m2;
7342 bestp1 = pipe_config->dpll.p1;
7343 bestp2 = pipe_config->dpll.p2;
a0c4da24 7344
89b667f8
JB
7345 /* See eDP HDMI DPIO driver vbios notes doc */
7346
7347 /* PLL B needs special handling */
bdd4b6a6 7348 if (pipe == PIPE_B)
5e69f97f 7349 vlv_pllb_recal_opamp(dev_priv, pipe);
89b667f8
JB
7350
7351 /* Set up Tx target for periodic Rcomp update */
ab3c759a 7352 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
89b667f8
JB
7353
7354 /* Disable target IRef on PLL */
ab3c759a 7355 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
89b667f8 7356 reg_val &= 0x00ffffff;
ab3c759a 7357 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
89b667f8
JB
7358
7359 /* Disable fast lock */
ab3c759a 7360 vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
89b667f8
JB
7361
7362 /* Set idtafcrecal before PLL is enabled */
a0c4da24
JB
7363 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
7364 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
7365 mdiv |= ((bestn << DPIO_N_SHIFT));
a0c4da24 7366 mdiv |= (1 << DPIO_K_SHIFT);
7df5080b
JB
7367
7368 /*
7369 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
7370 * but we don't support that).
7371 * Note: don't use the DAC post divider as it seems unstable.
7372 */
7373 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
ab3c759a 7374 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
a0c4da24 7375
a0c4da24 7376 mdiv |= DPIO_ENABLE_CALIBRATION;
ab3c759a 7377 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
a0c4da24 7378
89b667f8 7379 /* Set HBR and RBR LPF coefficients */
d288f65f 7380 if (pipe_config->port_clock == 162000 ||
409ee761
ACO
7381 intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG) ||
7382 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
ab3c759a 7383 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
885b0120 7384 0x009f0003);
89b667f8 7385 else
ab3c759a 7386 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
89b667f8
JB
7387 0x00d0000f);
7388
681a8504 7389 if (pipe_config->has_dp_encoder) {
89b667f8 7390 /* Use SSC source */
bdd4b6a6 7391 if (pipe == PIPE_A)
ab3c759a 7392 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
7393 0x0df40000);
7394 else
ab3c759a 7395 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
7396 0x0df70000);
7397 } else { /* HDMI or VGA */
7398 /* Use bend source */
bdd4b6a6 7399 if (pipe == PIPE_A)
ab3c759a 7400 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
7401 0x0df70000);
7402 else
ab3c759a 7403 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
7404 0x0df40000);
7405 }
a0c4da24 7406
ab3c759a 7407 coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
89b667f8 7408 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
409ee761
ACO
7409 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
7410 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
89b667f8 7411 coreclk |= 0x01000000;
ab3c759a 7412 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
a0c4da24 7413
ab3c759a 7414 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
a580516d 7415 mutex_unlock(&dev_priv->sb_lock);
a0c4da24
JB
7416}
7417
d288f65f 7418static void chv_prepare_pll(struct intel_crtc *crtc,
5cec258b 7419 const struct intel_crtc_state *pipe_config)
9d556c99
CML
7420{
7421 struct drm_device *dev = crtc->base.dev;
7422 struct drm_i915_private *dev_priv = dev->dev_private;
cd2d34d9 7423 enum pipe pipe = crtc->pipe;
9d556c99 7424 enum dpio_channel port = vlv_pipe_to_channel(pipe);
9cbe40c1 7425 u32 loopfilter, tribuf_calcntr;
9d556c99 7426 u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
a945ce7e 7427 u32 dpio_val;
9cbe40c1 7428 int vco;
9d556c99 7429
cd2d34d9
VS
7430 /* Enable Refclk and SSC */
7431 I915_WRITE(DPLL(pipe),
7432 pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
7433
7434 /* No need to actually set up the DPLL with DSI */
7435 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
7436 return;
7437
d288f65f
VS
7438 bestn = pipe_config->dpll.n;
7439 bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
7440 bestm1 = pipe_config->dpll.m1;
7441 bestm2 = pipe_config->dpll.m2 >> 22;
7442 bestp1 = pipe_config->dpll.p1;
7443 bestp2 = pipe_config->dpll.p2;
9cbe40c1 7444 vco = pipe_config->dpll.vco;
a945ce7e 7445 dpio_val = 0;
9cbe40c1 7446 loopfilter = 0;
9d556c99 7447
a580516d 7448 mutex_lock(&dev_priv->sb_lock);
9d556c99 7449
9d556c99
CML
7450 /* p1 and p2 divider */
7451 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
7452 5 << DPIO_CHV_S1_DIV_SHIFT |
7453 bestp1 << DPIO_CHV_P1_DIV_SHIFT |
7454 bestp2 << DPIO_CHV_P2_DIV_SHIFT |
7455 1 << DPIO_CHV_K_DIV_SHIFT);
7456
7457 /* Feedback post-divider - m2 */
7458 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
7459
7460 /* Feedback refclk divider - n and m1 */
7461 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
7462 DPIO_CHV_M1_DIV_BY_2 |
7463 1 << DPIO_CHV_N_DIV_SHIFT);
7464
7465 /* M2 fraction division */
25a25dfc 7466 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
9d556c99
CML
7467
7468 /* M2 fraction division enable */
a945ce7e
VP
7469 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
7470 dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
7471 dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
7472 if (bestm2_frac)
7473 dpio_val |= DPIO_CHV_FRAC_DIV_EN;
7474 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
9d556c99 7475
de3a0fde
VP
7476 /* Program digital lock detect threshold */
7477 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
7478 dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
7479 DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
7480 dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
7481 if (!bestm2_frac)
7482 dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
7483 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
7484
9d556c99 7485 /* Loop filter */
9cbe40c1
VP
7486 if (vco == 5400000) {
7487 loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
7488 loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
7489 loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
7490 tribuf_calcntr = 0x9;
7491 } else if (vco <= 6200000) {
7492 loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
7493 loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
7494 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7495 tribuf_calcntr = 0x9;
7496 } else if (vco <= 6480000) {
7497 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7498 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7499 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7500 tribuf_calcntr = 0x8;
7501 } else {
7502 /* Not supported. Apply the same limits as in the max case */
7503 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7504 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7505 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7506 tribuf_calcntr = 0;
7507 }
9d556c99
CML
7508 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
7509
968040b2 7510 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
9cbe40c1
VP
7511 dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
7512 dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
7513 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
7514
9d556c99
CML
7515 /* AFC Recal */
7516 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
7517 vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
7518 DPIO_AFC_RECAL);
7519
a580516d 7520 mutex_unlock(&dev_priv->sb_lock);
9d556c99
CML
7521}
7522
d288f65f
VS
7523/**
7524 * vlv_force_pll_on - forcibly enable just the PLL
7525 * @dev_priv: i915 private structure
7526 * @pipe: pipe PLL to enable
7527 * @dpll: PLL configuration
7528 *
7529 * Enable the PLL for @pipe using the supplied @dpll config. To be used
7530 * in cases where we need the PLL enabled even when @pipe is not going to
7531 * be enabled.
7532 */
3f36b937
TU
7533int vlv_force_pll_on(struct drm_device *dev, enum pipe pipe,
7534 const struct dpll *dpll)
d288f65f
VS
7535{
7536 struct intel_crtc *crtc =
7537 to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
3f36b937
TU
7538 struct intel_crtc_state *pipe_config;
7539
7540 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
7541 if (!pipe_config)
7542 return -ENOMEM;
7543
7544 pipe_config->base.crtc = &crtc->base;
7545 pipe_config->pixel_multiplier = 1;
7546 pipe_config->dpll = *dpll;
d288f65f
VS
7547
7548 if (IS_CHERRYVIEW(dev)) {
3f36b937
TU
7549 chv_compute_dpll(crtc, pipe_config);
7550 chv_prepare_pll(crtc, pipe_config);
7551 chv_enable_pll(crtc, pipe_config);
d288f65f 7552 } else {
3f36b937
TU
7553 vlv_compute_dpll(crtc, pipe_config);
7554 vlv_prepare_pll(crtc, pipe_config);
7555 vlv_enable_pll(crtc, pipe_config);
d288f65f 7556 }
3f36b937
TU
7557
7558 kfree(pipe_config);
7559
7560 return 0;
d288f65f
VS
7561}
7562
7563/**
7564 * vlv_force_pll_off - forcibly disable just the PLL
7565 * @dev_priv: i915 private structure
7566 * @pipe: pipe PLL to disable
7567 *
7568 * Disable the PLL for @pipe. To be used in cases where we need
7569 * the PLL enabled even when @pipe is not going to be enabled.
7570 */
7571void vlv_force_pll_off(struct drm_device *dev, enum pipe pipe)
7572{
7573 if (IS_CHERRYVIEW(dev))
7574 chv_disable_pll(to_i915(dev), pipe);
7575 else
7576 vlv_disable_pll(to_i915(dev), pipe);
7577}
7578
251ac862
DV
7579static void i9xx_compute_dpll(struct intel_crtc *crtc,
7580 struct intel_crtc_state *crtc_state,
9e2c8475 7581 struct dpll *reduced_clock)
eb1cbe48 7582{
f47709a9 7583 struct drm_device *dev = crtc->base.dev;
eb1cbe48 7584 struct drm_i915_private *dev_priv = dev->dev_private;
eb1cbe48
DV
7585 u32 dpll;
7586 bool is_sdvo;
190f68c5 7587 struct dpll *clock = &crtc_state->dpll;
eb1cbe48 7588
190f68c5 7589 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
2a8f64ca 7590
a93e255f
ACO
7591 is_sdvo = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO) ||
7592 intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI);
eb1cbe48
DV
7593
7594 dpll = DPLL_VGA_MODE_DIS;
7595
a93e255f 7596 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
eb1cbe48
DV
7597 dpll |= DPLLB_MODE_LVDS;
7598 else
7599 dpll |= DPLLB_MODE_DAC_SERIAL;
6cc5f341 7600
ef1b460d 7601 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
190f68c5 7602 dpll |= (crtc_state->pixel_multiplier - 1)
198a037f 7603 << SDVO_MULTIPLIER_SHIFT_HIRES;
eb1cbe48 7604 }
198a037f
DV
7605
7606 if (is_sdvo)
4a33e48d 7607 dpll |= DPLL_SDVO_HIGH_SPEED;
198a037f 7608
190f68c5 7609 if (crtc_state->has_dp_encoder)
4a33e48d 7610 dpll |= DPLL_SDVO_HIGH_SPEED;
eb1cbe48
DV
7611
7612 /* compute bitmask from p1 value */
7613 if (IS_PINEVIEW(dev))
7614 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
7615 else {
7616 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7617 if (IS_G4X(dev) && reduced_clock)
7618 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
7619 }
7620 switch (clock->p2) {
7621 case 5:
7622 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
7623 break;
7624 case 7:
7625 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
7626 break;
7627 case 10:
7628 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
7629 break;
7630 case 14:
7631 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
7632 break;
7633 }
7634 if (INTEL_INFO(dev)->gen >= 4)
7635 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
7636
190f68c5 7637 if (crtc_state->sdvo_tv_clock)
eb1cbe48 7638 dpll |= PLL_REF_INPUT_TVCLKINBC;
a93e255f 7639 else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
ceb41007 7640 intel_panel_use_ssc(dev_priv))
eb1cbe48
DV
7641 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7642 else
7643 dpll |= PLL_REF_INPUT_DREFCLK;
7644
7645 dpll |= DPLL_VCO_ENABLE;
190f68c5 7646 crtc_state->dpll_hw_state.dpll = dpll;
8bcc2795 7647
eb1cbe48 7648 if (INTEL_INFO(dev)->gen >= 4) {
190f68c5 7649 u32 dpll_md = (crtc_state->pixel_multiplier - 1)
ef1b460d 7650 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
190f68c5 7651 crtc_state->dpll_hw_state.dpll_md = dpll_md;
eb1cbe48
DV
7652 }
7653}
7654
251ac862
DV
7655static void i8xx_compute_dpll(struct intel_crtc *crtc,
7656 struct intel_crtc_state *crtc_state,
9e2c8475 7657 struct dpll *reduced_clock)
eb1cbe48 7658{
f47709a9 7659 struct drm_device *dev = crtc->base.dev;
eb1cbe48 7660 struct drm_i915_private *dev_priv = dev->dev_private;
eb1cbe48 7661 u32 dpll;
190f68c5 7662 struct dpll *clock = &crtc_state->dpll;
eb1cbe48 7663
190f68c5 7664 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
2a8f64ca 7665
eb1cbe48
DV
7666 dpll = DPLL_VGA_MODE_DIS;
7667
a93e255f 7668 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
eb1cbe48
DV
7669 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7670 } else {
7671 if (clock->p1 == 2)
7672 dpll |= PLL_P1_DIVIDE_BY_TWO;
7673 else
7674 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7675 if (clock->p2 == 4)
7676 dpll |= PLL_P2_DIVIDE_BY_4;
7677 }
7678
a93e255f 7679 if (!IS_I830(dev) && intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
4a33e48d
DV
7680 dpll |= DPLL_DVO_2X_MODE;
7681
a93e255f 7682 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
ceb41007 7683 intel_panel_use_ssc(dev_priv))
eb1cbe48
DV
7684 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7685 else
7686 dpll |= PLL_REF_INPUT_DREFCLK;
7687
7688 dpll |= DPLL_VCO_ENABLE;
190f68c5 7689 crtc_state->dpll_hw_state.dpll = dpll;
eb1cbe48
DV
7690}
7691
8a654f3b 7692static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
b0e77b9c
PZ
7693{
7694 struct drm_device *dev = intel_crtc->base.dev;
7695 struct drm_i915_private *dev_priv = dev->dev_private;
7696 enum pipe pipe = intel_crtc->pipe;
6e3c9717 7697 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
7c5f93b0 7698 const struct drm_display_mode *adjusted_mode = &intel_crtc->config->base.adjusted_mode;
1caea6e9
VS
7699 uint32_t crtc_vtotal, crtc_vblank_end;
7700 int vsyncshift = 0;
4d8a62ea
DV
7701
7702 /* We need to be careful not to changed the adjusted mode, for otherwise
7703 * the hw state checker will get angry at the mismatch. */
7704 crtc_vtotal = adjusted_mode->crtc_vtotal;
7705 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
b0e77b9c 7706
609aeaca 7707 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
b0e77b9c 7708 /* the chip adds 2 halflines automatically */
4d8a62ea
DV
7709 crtc_vtotal -= 1;
7710 crtc_vblank_end -= 1;
609aeaca 7711
409ee761 7712 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
609aeaca
VS
7713 vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
7714 else
7715 vsyncshift = adjusted_mode->crtc_hsync_start -
7716 adjusted_mode->crtc_htotal / 2;
1caea6e9
VS
7717 if (vsyncshift < 0)
7718 vsyncshift += adjusted_mode->crtc_htotal;
b0e77b9c
PZ
7719 }
7720
7721 if (INTEL_INFO(dev)->gen > 3)
fe2b8f9d 7722 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
b0e77b9c 7723
fe2b8f9d 7724 I915_WRITE(HTOTAL(cpu_transcoder),
b0e77b9c
PZ
7725 (adjusted_mode->crtc_hdisplay - 1) |
7726 ((adjusted_mode->crtc_htotal - 1) << 16));
fe2b8f9d 7727 I915_WRITE(HBLANK(cpu_transcoder),
b0e77b9c
PZ
7728 (adjusted_mode->crtc_hblank_start - 1) |
7729 ((adjusted_mode->crtc_hblank_end - 1) << 16));
fe2b8f9d 7730 I915_WRITE(HSYNC(cpu_transcoder),
b0e77b9c
PZ
7731 (adjusted_mode->crtc_hsync_start - 1) |
7732 ((adjusted_mode->crtc_hsync_end - 1) << 16));
7733
fe2b8f9d 7734 I915_WRITE(VTOTAL(cpu_transcoder),
b0e77b9c 7735 (adjusted_mode->crtc_vdisplay - 1) |
4d8a62ea 7736 ((crtc_vtotal - 1) << 16));
fe2b8f9d 7737 I915_WRITE(VBLANK(cpu_transcoder),
b0e77b9c 7738 (adjusted_mode->crtc_vblank_start - 1) |
4d8a62ea 7739 ((crtc_vblank_end - 1) << 16));
fe2b8f9d 7740 I915_WRITE(VSYNC(cpu_transcoder),
b0e77b9c
PZ
7741 (adjusted_mode->crtc_vsync_start - 1) |
7742 ((adjusted_mode->crtc_vsync_end - 1) << 16));
7743
b5e508d4
PZ
7744 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
7745 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
7746 * documented on the DDI_FUNC_CTL register description, EDP Input Select
7747 * bits. */
7748 if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
7749 (pipe == PIPE_B || pipe == PIPE_C))
7750 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
7751
bc58be60
JN
7752}
7753
7754static void intel_set_pipe_src_size(struct intel_crtc *intel_crtc)
7755{
7756 struct drm_device *dev = intel_crtc->base.dev;
7757 struct drm_i915_private *dev_priv = dev->dev_private;
7758 enum pipe pipe = intel_crtc->pipe;
7759
b0e77b9c
PZ
7760 /* pipesrc controls the size that is scaled from, which should
7761 * always be the user's requested size.
7762 */
7763 I915_WRITE(PIPESRC(pipe),
6e3c9717
ACO
7764 ((intel_crtc->config->pipe_src_w - 1) << 16) |
7765 (intel_crtc->config->pipe_src_h - 1));
b0e77b9c
PZ
7766}
7767
1bd1bd80 7768static void intel_get_pipe_timings(struct intel_crtc *crtc,
5cec258b 7769 struct intel_crtc_state *pipe_config)
1bd1bd80
DV
7770{
7771 struct drm_device *dev = crtc->base.dev;
7772 struct drm_i915_private *dev_priv = dev->dev_private;
7773 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
7774 uint32_t tmp;
7775
7776 tmp = I915_READ(HTOTAL(cpu_transcoder));
2d112de7
ACO
7777 pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
7778 pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 7779 tmp = I915_READ(HBLANK(cpu_transcoder));
2d112de7
ACO
7780 pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
7781 pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 7782 tmp = I915_READ(HSYNC(cpu_transcoder));
2d112de7
ACO
7783 pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
7784 pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80
DV
7785
7786 tmp = I915_READ(VTOTAL(cpu_transcoder));
2d112de7
ACO
7787 pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
7788 pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 7789 tmp = I915_READ(VBLANK(cpu_transcoder));
2d112de7
ACO
7790 pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
7791 pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 7792 tmp = I915_READ(VSYNC(cpu_transcoder));
2d112de7
ACO
7793 pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
7794 pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80
DV
7795
7796 if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
2d112de7
ACO
7797 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
7798 pipe_config->base.adjusted_mode.crtc_vtotal += 1;
7799 pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
1bd1bd80 7800 }
bc58be60
JN
7801}
7802
7803static void intel_get_pipe_src_size(struct intel_crtc *crtc,
7804 struct intel_crtc_state *pipe_config)
7805{
7806 struct drm_device *dev = crtc->base.dev;
7807 struct drm_i915_private *dev_priv = dev->dev_private;
7808 u32 tmp;
1bd1bd80
DV
7809
7810 tmp = I915_READ(PIPESRC(crtc->pipe));
37327abd
VS
7811 pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
7812 pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
7813
2d112de7
ACO
7814 pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
7815 pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
1bd1bd80
DV
7816}
7817
f6a83288 7818void intel_mode_from_pipe_config(struct drm_display_mode *mode,
5cec258b 7819 struct intel_crtc_state *pipe_config)
babea61d 7820{
2d112de7
ACO
7821 mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
7822 mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
7823 mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
7824 mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
babea61d 7825
2d112de7
ACO
7826 mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
7827 mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
7828 mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
7829 mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
babea61d 7830
2d112de7 7831 mode->flags = pipe_config->base.adjusted_mode.flags;
cd13f5ab 7832 mode->type = DRM_MODE_TYPE_DRIVER;
babea61d 7833
2d112de7
ACO
7834 mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
7835 mode->flags |= pipe_config->base.adjusted_mode.flags;
cd13f5ab
ML
7836
7837 mode->hsync = drm_mode_hsync(mode);
7838 mode->vrefresh = drm_mode_vrefresh(mode);
7839 drm_mode_set_name(mode);
babea61d
JB
7840}
7841
84b046f3
DV
7842static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
7843{
7844 struct drm_device *dev = intel_crtc->base.dev;
7845 struct drm_i915_private *dev_priv = dev->dev_private;
7846 uint32_t pipeconf;
7847
9f11a9e4 7848 pipeconf = 0;
84b046f3 7849
b6b5d049
VS
7850 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
7851 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
7852 pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
67c72a12 7853
6e3c9717 7854 if (intel_crtc->config->double_wide)
cf532bb2 7855 pipeconf |= PIPECONF_DOUBLE_WIDE;
84b046f3 7856
ff9ce46e 7857 /* only g4x and later have fancy bpc/dither controls */
666a4537 7858 if (IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
ff9ce46e 7859 /* Bspec claims that we can't use dithering for 30bpp pipes. */
6e3c9717 7860 if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
ff9ce46e 7861 pipeconf |= PIPECONF_DITHER_EN |
84b046f3 7862 PIPECONF_DITHER_TYPE_SP;
84b046f3 7863
6e3c9717 7864 switch (intel_crtc->config->pipe_bpp) {
ff9ce46e
DV
7865 case 18:
7866 pipeconf |= PIPECONF_6BPC;
7867 break;
7868 case 24:
7869 pipeconf |= PIPECONF_8BPC;
7870 break;
7871 case 30:
7872 pipeconf |= PIPECONF_10BPC;
7873 break;
7874 default:
7875 /* Case prevented by intel_choose_pipe_bpp_dither. */
7876 BUG();
84b046f3
DV
7877 }
7878 }
7879
7880 if (HAS_PIPE_CXSR(dev)) {
7881 if (intel_crtc->lowfreq_avail) {
7882 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
7883 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
7884 } else {
7885 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
84b046f3
DV
7886 }
7887 }
7888
6e3c9717 7889 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
efc2cfff 7890 if (INTEL_INFO(dev)->gen < 4 ||
409ee761 7891 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
efc2cfff
VS
7892 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
7893 else
7894 pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
7895 } else
84b046f3
DV
7896 pipeconf |= PIPECONF_PROGRESSIVE;
7897
666a4537
WB
7898 if ((IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) &&
7899 intel_crtc->config->limited_color_range)
9f11a9e4 7900 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
9c8e09b7 7901
84b046f3
DV
7902 I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
7903 POSTING_READ(PIPECONF(intel_crtc->pipe));
7904}
7905
81c97f52
ACO
7906static int i8xx_crtc_compute_clock(struct intel_crtc *crtc,
7907 struct intel_crtc_state *crtc_state)
7908{
7909 struct drm_device *dev = crtc->base.dev;
7910 struct drm_i915_private *dev_priv = dev->dev_private;
1b6f4958 7911 const struct intel_limit *limit;
81c97f52
ACO
7912 int refclk = 48000;
7913
7914 memset(&crtc_state->dpll_hw_state, 0,
7915 sizeof(crtc_state->dpll_hw_state));
7916
7917 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
7918 if (intel_panel_use_ssc(dev_priv)) {
7919 refclk = dev_priv->vbt.lvds_ssc_freq;
7920 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
7921 }
7922
7923 limit = &intel_limits_i8xx_lvds;
7924 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO)) {
7925 limit = &intel_limits_i8xx_dvo;
7926 } else {
7927 limit = &intel_limits_i8xx_dac;
7928 }
7929
7930 if (!crtc_state->clock_set &&
7931 !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7932 refclk, NULL, &crtc_state->dpll)) {
7933 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7934 return -EINVAL;
7935 }
7936
7937 i8xx_compute_dpll(crtc, crtc_state, NULL);
7938
7939 return 0;
7940}
7941
19ec6693
ACO
7942static int g4x_crtc_compute_clock(struct intel_crtc *crtc,
7943 struct intel_crtc_state *crtc_state)
7944{
7945 struct drm_device *dev = crtc->base.dev;
7946 struct drm_i915_private *dev_priv = dev->dev_private;
1b6f4958 7947 const struct intel_limit *limit;
19ec6693
ACO
7948 int refclk = 96000;
7949
7950 memset(&crtc_state->dpll_hw_state, 0,
7951 sizeof(crtc_state->dpll_hw_state));
7952
7953 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
7954 if (intel_panel_use_ssc(dev_priv)) {
7955 refclk = dev_priv->vbt.lvds_ssc_freq;
7956 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
7957 }
7958
7959 if (intel_is_dual_link_lvds(dev))
7960 limit = &intel_limits_g4x_dual_channel_lvds;
7961 else
7962 limit = &intel_limits_g4x_single_channel_lvds;
7963 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI) ||
7964 intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
7965 limit = &intel_limits_g4x_hdmi;
7966 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO)) {
7967 limit = &intel_limits_g4x_sdvo;
7968 } else {
7969 /* The option is for other outputs */
7970 limit = &intel_limits_i9xx_sdvo;
7971 }
7972
7973 if (!crtc_state->clock_set &&
7974 !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7975 refclk, NULL, &crtc_state->dpll)) {
7976 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7977 return -EINVAL;
7978 }
7979
7980 i9xx_compute_dpll(crtc, crtc_state, NULL);
7981
7982 return 0;
7983}
7984
70e8aa21
ACO
7985static int pnv_crtc_compute_clock(struct intel_crtc *crtc,
7986 struct intel_crtc_state *crtc_state)
7987{
7988 struct drm_device *dev = crtc->base.dev;
7989 struct drm_i915_private *dev_priv = dev->dev_private;
1b6f4958 7990 const struct intel_limit *limit;
70e8aa21
ACO
7991 int refclk = 96000;
7992
7993 memset(&crtc_state->dpll_hw_state, 0,
7994 sizeof(crtc_state->dpll_hw_state));
7995
7996 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
7997 if (intel_panel_use_ssc(dev_priv)) {
7998 refclk = dev_priv->vbt.lvds_ssc_freq;
7999 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
8000 }
8001
8002 limit = &intel_limits_pineview_lvds;
8003 } else {
8004 limit = &intel_limits_pineview_sdvo;
8005 }
8006
8007 if (!crtc_state->clock_set &&
8008 !pnv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8009 refclk, NULL, &crtc_state->dpll)) {
8010 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8011 return -EINVAL;
8012 }
8013
8014 i9xx_compute_dpll(crtc, crtc_state, NULL);
8015
8016 return 0;
8017}
8018
190f68c5
ACO
8019static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
8020 struct intel_crtc_state *crtc_state)
79e53945 8021{
c7653199 8022 struct drm_device *dev = crtc->base.dev;
79e53945 8023 struct drm_i915_private *dev_priv = dev->dev_private;
1b6f4958 8024 const struct intel_limit *limit;
81c97f52 8025 int refclk = 96000;
79e53945 8026
dd3cd74a
ACO
8027 memset(&crtc_state->dpll_hw_state, 0,
8028 sizeof(crtc_state->dpll_hw_state));
8029
70e8aa21
ACO
8030 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
8031 if (intel_panel_use_ssc(dev_priv)) {
8032 refclk = dev_priv->vbt.lvds_ssc_freq;
8033 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
8034 }
43565a06 8035
70e8aa21
ACO
8036 limit = &intel_limits_i9xx_lvds;
8037 } else {
8038 limit = &intel_limits_i9xx_sdvo;
81c97f52 8039 }
79e53945 8040
70e8aa21
ACO
8041 if (!crtc_state->clock_set &&
8042 !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8043 refclk, NULL, &crtc_state->dpll)) {
8044 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8045 return -EINVAL;
f47709a9 8046 }
7026d4ac 8047
81c97f52 8048 i9xx_compute_dpll(crtc, crtc_state, NULL);
79e53945 8049
c8f7a0db 8050 return 0;
f564048e
EA
8051}
8052
65b3d6a9
ACO
8053static int chv_crtc_compute_clock(struct intel_crtc *crtc,
8054 struct intel_crtc_state *crtc_state)
8055{
8056 int refclk = 100000;
1b6f4958 8057 const struct intel_limit *limit = &intel_limits_chv;
65b3d6a9
ACO
8058
8059 memset(&crtc_state->dpll_hw_state, 0,
8060 sizeof(crtc_state->dpll_hw_state));
8061
65b3d6a9
ACO
8062 if (!crtc_state->clock_set &&
8063 !chv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8064 refclk, NULL, &crtc_state->dpll)) {
8065 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8066 return -EINVAL;
8067 }
8068
8069 chv_compute_dpll(crtc, crtc_state);
8070
8071 return 0;
8072}
8073
8074static int vlv_crtc_compute_clock(struct intel_crtc *crtc,
8075 struct intel_crtc_state *crtc_state)
8076{
8077 int refclk = 100000;
1b6f4958 8078 const struct intel_limit *limit = &intel_limits_vlv;
65b3d6a9
ACO
8079
8080 memset(&crtc_state->dpll_hw_state, 0,
8081 sizeof(crtc_state->dpll_hw_state));
8082
65b3d6a9
ACO
8083 if (!crtc_state->clock_set &&
8084 !vlv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8085 refclk, NULL, &crtc_state->dpll)) {
8086 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8087 return -EINVAL;
8088 }
8089
8090 vlv_compute_dpll(crtc, crtc_state);
8091
8092 return 0;
8093}
8094
2fa2fe9a 8095static void i9xx_get_pfit_config(struct intel_crtc *crtc,
5cec258b 8096 struct intel_crtc_state *pipe_config)
2fa2fe9a
DV
8097{
8098 struct drm_device *dev = crtc->base.dev;
8099 struct drm_i915_private *dev_priv = dev->dev_private;
8100 uint32_t tmp;
8101
dc9e7dec
VS
8102 if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
8103 return;
8104
2fa2fe9a 8105 tmp = I915_READ(PFIT_CONTROL);
06922821
DV
8106 if (!(tmp & PFIT_ENABLE))
8107 return;
2fa2fe9a 8108
06922821 8109 /* Check whether the pfit is attached to our pipe. */
2fa2fe9a
DV
8110 if (INTEL_INFO(dev)->gen < 4) {
8111 if (crtc->pipe != PIPE_B)
8112 return;
2fa2fe9a
DV
8113 } else {
8114 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
8115 return;
8116 }
8117
06922821 8118 pipe_config->gmch_pfit.control = tmp;
2fa2fe9a 8119 pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
2fa2fe9a
DV
8120}
8121
acbec814 8122static void vlv_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 8123 struct intel_crtc_state *pipe_config)
acbec814
JB
8124{
8125 struct drm_device *dev = crtc->base.dev;
8126 struct drm_i915_private *dev_priv = dev->dev_private;
8127 int pipe = pipe_config->cpu_transcoder;
9e2c8475 8128 struct dpll clock;
acbec814 8129 u32 mdiv;
662c6ecb 8130 int refclk = 100000;
acbec814 8131
b521973b
VS
8132 /* In case of DSI, DPLL will not be used */
8133 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
f573de5a
SK
8134 return;
8135
a580516d 8136 mutex_lock(&dev_priv->sb_lock);
ab3c759a 8137 mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
a580516d 8138 mutex_unlock(&dev_priv->sb_lock);
acbec814
JB
8139
8140 clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
8141 clock.m2 = mdiv & DPIO_M2DIV_MASK;
8142 clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
8143 clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
8144 clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
8145
dccbea3b 8146 pipe_config->port_clock = vlv_calc_dpll_params(refclk, &clock);
acbec814
JB
8147}
8148
5724dbd1
DL
8149static void
8150i9xx_get_initial_plane_config(struct intel_crtc *crtc,
8151 struct intel_initial_plane_config *plane_config)
1ad292b5
JB
8152{
8153 struct drm_device *dev = crtc->base.dev;
8154 struct drm_i915_private *dev_priv = dev->dev_private;
8155 u32 val, base, offset;
8156 int pipe = crtc->pipe, plane = crtc->plane;
8157 int fourcc, pixel_format;
6761dd31 8158 unsigned int aligned_height;
b113d5ee 8159 struct drm_framebuffer *fb;
1b842c89 8160 struct intel_framebuffer *intel_fb;
1ad292b5 8161
42a7b088
DL
8162 val = I915_READ(DSPCNTR(plane));
8163 if (!(val & DISPLAY_PLANE_ENABLE))
8164 return;
8165
d9806c9f 8166 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
1b842c89 8167 if (!intel_fb) {
1ad292b5
JB
8168 DRM_DEBUG_KMS("failed to alloc fb\n");
8169 return;
8170 }
8171
1b842c89
DL
8172 fb = &intel_fb->base;
8173
18c5247e
DV
8174 if (INTEL_INFO(dev)->gen >= 4) {
8175 if (val & DISPPLANE_TILED) {
49af449b 8176 plane_config->tiling = I915_TILING_X;
18c5247e
DV
8177 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
8178 }
8179 }
1ad292b5
JB
8180
8181 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
b35d63fa 8182 fourcc = i9xx_format_to_fourcc(pixel_format);
b113d5ee
DL
8183 fb->pixel_format = fourcc;
8184 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
1ad292b5
JB
8185
8186 if (INTEL_INFO(dev)->gen >= 4) {
49af449b 8187 if (plane_config->tiling)
1ad292b5
JB
8188 offset = I915_READ(DSPTILEOFF(plane));
8189 else
8190 offset = I915_READ(DSPLINOFF(plane));
8191 base = I915_READ(DSPSURF(plane)) & 0xfffff000;
8192 } else {
8193 base = I915_READ(DSPADDR(plane));
8194 }
8195 plane_config->base = base;
8196
8197 val = I915_READ(PIPESRC(pipe));
b113d5ee
DL
8198 fb->width = ((val >> 16) & 0xfff) + 1;
8199 fb->height = ((val >> 0) & 0xfff) + 1;
1ad292b5
JB
8200
8201 val = I915_READ(DSPSTRIDE(pipe));
b113d5ee 8202 fb->pitches[0] = val & 0xffffffc0;
1ad292b5 8203
b113d5ee 8204 aligned_height = intel_fb_align_height(dev, fb->height,
091df6cb
DV
8205 fb->pixel_format,
8206 fb->modifier[0]);
1ad292b5 8207
f37b5c2b 8208 plane_config->size = fb->pitches[0] * aligned_height;
1ad292b5 8209
2844a921
DL
8210 DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
8211 pipe_name(pipe), plane, fb->width, fb->height,
8212 fb->bits_per_pixel, base, fb->pitches[0],
8213 plane_config->size);
1ad292b5 8214
2d14030b 8215 plane_config->fb = intel_fb;
1ad292b5
JB
8216}
8217
70b23a98 8218static void chv_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 8219 struct intel_crtc_state *pipe_config)
70b23a98
VS
8220{
8221 struct drm_device *dev = crtc->base.dev;
8222 struct drm_i915_private *dev_priv = dev->dev_private;
8223 int pipe = pipe_config->cpu_transcoder;
8224 enum dpio_channel port = vlv_pipe_to_channel(pipe);
9e2c8475 8225 struct dpll clock;
0d7b6b11 8226 u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2, pll_dw3;
70b23a98
VS
8227 int refclk = 100000;
8228
b521973b
VS
8229 /* In case of DSI, DPLL will not be used */
8230 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
8231 return;
8232
a580516d 8233 mutex_lock(&dev_priv->sb_lock);
70b23a98
VS
8234 cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
8235 pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
8236 pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
8237 pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
0d7b6b11 8238 pll_dw3 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
a580516d 8239 mutex_unlock(&dev_priv->sb_lock);
70b23a98
VS
8240
8241 clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
0d7b6b11
ID
8242 clock.m2 = (pll_dw0 & 0xff) << 22;
8243 if (pll_dw3 & DPIO_CHV_FRAC_DIV_EN)
8244 clock.m2 |= pll_dw2 & 0x3fffff;
70b23a98
VS
8245 clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
8246 clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
8247 clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
8248
dccbea3b 8249 pipe_config->port_clock = chv_calc_dpll_params(refclk, &clock);
70b23a98
VS
8250}
8251
0e8ffe1b 8252static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
5cec258b 8253 struct intel_crtc_state *pipe_config)
0e8ffe1b
DV
8254{
8255 struct drm_device *dev = crtc->base.dev;
8256 struct drm_i915_private *dev_priv = dev->dev_private;
1729050e 8257 enum intel_display_power_domain power_domain;
0e8ffe1b 8258 uint32_t tmp;
1729050e 8259 bool ret;
0e8ffe1b 8260
1729050e
ID
8261 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
8262 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
b5482bd0
ID
8263 return false;
8264
e143a21c 8265 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
8106ddbd 8266 pipe_config->shared_dpll = NULL;
eccb140b 8267
1729050e
ID
8268 ret = false;
8269
0e8ffe1b
DV
8270 tmp = I915_READ(PIPECONF(crtc->pipe));
8271 if (!(tmp & PIPECONF_ENABLE))
1729050e 8272 goto out;
0e8ffe1b 8273
666a4537 8274 if (IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
42571aef
VS
8275 switch (tmp & PIPECONF_BPC_MASK) {
8276 case PIPECONF_6BPC:
8277 pipe_config->pipe_bpp = 18;
8278 break;
8279 case PIPECONF_8BPC:
8280 pipe_config->pipe_bpp = 24;
8281 break;
8282 case PIPECONF_10BPC:
8283 pipe_config->pipe_bpp = 30;
8284 break;
8285 default:
8286 break;
8287 }
8288 }
8289
666a4537
WB
8290 if ((IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) &&
8291 (tmp & PIPECONF_COLOR_RANGE_SELECT))
b5a9fa09
DV
8292 pipe_config->limited_color_range = true;
8293
282740f7
VS
8294 if (INTEL_INFO(dev)->gen < 4)
8295 pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
8296
1bd1bd80 8297 intel_get_pipe_timings(crtc, pipe_config);
bc58be60 8298 intel_get_pipe_src_size(crtc, pipe_config);
1bd1bd80 8299
2fa2fe9a
DV
8300 i9xx_get_pfit_config(crtc, pipe_config);
8301
6c49f241 8302 if (INTEL_INFO(dev)->gen >= 4) {
c231775c
VS
8303 /* No way to read it out on pipes B and C */
8304 if (IS_CHERRYVIEW(dev) && crtc->pipe != PIPE_A)
8305 tmp = dev_priv->chv_dpll_md[crtc->pipe];
8306 else
8307 tmp = I915_READ(DPLL_MD(crtc->pipe));
6c49f241
DV
8308 pipe_config->pixel_multiplier =
8309 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
8310 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
8bcc2795 8311 pipe_config->dpll_hw_state.dpll_md = tmp;
6c49f241
DV
8312 } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
8313 tmp = I915_READ(DPLL(crtc->pipe));
8314 pipe_config->pixel_multiplier =
8315 ((tmp & SDVO_MULTIPLIER_MASK)
8316 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
8317 } else {
8318 /* Note that on i915G/GM the pixel multiplier is in the sdvo
8319 * port and will be fixed up in the encoder->get_config
8320 * function. */
8321 pipe_config->pixel_multiplier = 1;
8322 }
8bcc2795 8323 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
666a4537 8324 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
1c4e0274
VS
8325 /*
8326 * DPLL_DVO_2X_MODE must be enabled for both DPLLs
8327 * on 830. Filter it out here so that we don't
8328 * report errors due to that.
8329 */
8330 if (IS_I830(dev))
8331 pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
8332
8bcc2795
DV
8333 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
8334 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
165e901c
VS
8335 } else {
8336 /* Mask out read-only status bits. */
8337 pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
8338 DPLL_PORTC_READY_MASK |
8339 DPLL_PORTB_READY_MASK);
8bcc2795 8340 }
6c49f241 8341
70b23a98
VS
8342 if (IS_CHERRYVIEW(dev))
8343 chv_crtc_clock_get(crtc, pipe_config);
8344 else if (IS_VALLEYVIEW(dev))
acbec814
JB
8345 vlv_crtc_clock_get(crtc, pipe_config);
8346 else
8347 i9xx_crtc_clock_get(crtc, pipe_config);
18442d08 8348
0f64614d
VS
8349 /*
8350 * Normally the dotclock is filled in by the encoder .get_config()
8351 * but in case the pipe is enabled w/o any ports we need a sane
8352 * default.
8353 */
8354 pipe_config->base.adjusted_mode.crtc_clock =
8355 pipe_config->port_clock / pipe_config->pixel_multiplier;
8356
1729050e
ID
8357 ret = true;
8358
8359out:
8360 intel_display_power_put(dev_priv, power_domain);
8361
8362 return ret;
0e8ffe1b
DV
8363}
8364
dde86e2d 8365static void ironlake_init_pch_refclk(struct drm_device *dev)
13d83a67
JB
8366{
8367 struct drm_i915_private *dev_priv = dev->dev_private;
13d83a67 8368 struct intel_encoder *encoder;
1c1a24d2 8369 int i;
74cfd7ac 8370 u32 val, final;
13d83a67 8371 bool has_lvds = false;
199e5d79 8372 bool has_cpu_edp = false;
199e5d79 8373 bool has_panel = false;
99eb6a01
KP
8374 bool has_ck505 = false;
8375 bool can_ssc = false;
1c1a24d2 8376 bool using_ssc_source = false;
13d83a67
JB
8377
8378 /* We need to take the global config into account */
b2784e15 8379 for_each_intel_encoder(dev, encoder) {
199e5d79
KP
8380 switch (encoder->type) {
8381 case INTEL_OUTPUT_LVDS:
8382 has_panel = true;
8383 has_lvds = true;
8384 break;
8385 case INTEL_OUTPUT_EDP:
8386 has_panel = true;
2de6905f 8387 if (enc_to_dig_port(&encoder->base)->port == PORT_A)
199e5d79
KP
8388 has_cpu_edp = true;
8389 break;
6847d71b
PZ
8390 default:
8391 break;
13d83a67
JB
8392 }
8393 }
8394
99eb6a01 8395 if (HAS_PCH_IBX(dev)) {
41aa3448 8396 has_ck505 = dev_priv->vbt.display_clock_mode;
99eb6a01
KP
8397 can_ssc = has_ck505;
8398 } else {
8399 has_ck505 = false;
8400 can_ssc = true;
8401 }
8402
1c1a24d2
L
8403 /* Check if any DPLLs are using the SSC source */
8404 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
8405 u32 temp = I915_READ(PCH_DPLL(i));
8406
8407 if (!(temp & DPLL_VCO_ENABLE))
8408 continue;
8409
8410 if ((temp & PLL_REF_INPUT_MASK) ==
8411 PLLB_REF_INPUT_SPREADSPECTRUMIN) {
8412 using_ssc_source = true;
8413 break;
8414 }
8415 }
8416
8417 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d using_ssc_source %d\n",
8418 has_panel, has_lvds, has_ck505, using_ssc_source);
13d83a67
JB
8419
8420 /* Ironlake: try to setup display ref clock before DPLL
8421 * enabling. This is only under driver's control after
8422 * PCH B stepping, previous chipset stepping should be
8423 * ignoring this setting.
8424 */
74cfd7ac
CW
8425 val = I915_READ(PCH_DREF_CONTROL);
8426
8427 /* As we must carefully and slowly disable/enable each source in turn,
8428 * compute the final state we want first and check if we need to
8429 * make any changes at all.
8430 */
8431 final = val;
8432 final &= ~DREF_NONSPREAD_SOURCE_MASK;
8433 if (has_ck505)
8434 final |= DREF_NONSPREAD_CK505_ENABLE;
8435 else
8436 final |= DREF_NONSPREAD_SOURCE_ENABLE;
8437
8c07eb68 8438 final &= ~DREF_SSC_SOURCE_MASK;
74cfd7ac 8439 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
8c07eb68 8440 final &= ~DREF_SSC1_ENABLE;
74cfd7ac
CW
8441
8442 if (has_panel) {
8443 final |= DREF_SSC_SOURCE_ENABLE;
8444
8445 if (intel_panel_use_ssc(dev_priv) && can_ssc)
8446 final |= DREF_SSC1_ENABLE;
8447
8448 if (has_cpu_edp) {
8449 if (intel_panel_use_ssc(dev_priv) && can_ssc)
8450 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
8451 else
8452 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
8453 } else
8454 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
1c1a24d2
L
8455 } else if (using_ssc_source) {
8456 final |= DREF_SSC_SOURCE_ENABLE;
8457 final |= DREF_SSC1_ENABLE;
74cfd7ac
CW
8458 }
8459
8460 if (final == val)
8461 return;
8462
13d83a67 8463 /* Always enable nonspread source */
74cfd7ac 8464 val &= ~DREF_NONSPREAD_SOURCE_MASK;
13d83a67 8465
99eb6a01 8466 if (has_ck505)
74cfd7ac 8467 val |= DREF_NONSPREAD_CK505_ENABLE;
99eb6a01 8468 else
74cfd7ac 8469 val |= DREF_NONSPREAD_SOURCE_ENABLE;
13d83a67 8470
199e5d79 8471 if (has_panel) {
74cfd7ac
CW
8472 val &= ~DREF_SSC_SOURCE_MASK;
8473 val |= DREF_SSC_SOURCE_ENABLE;
13d83a67 8474
199e5d79 8475 /* SSC must be turned on before enabling the CPU output */
99eb6a01 8476 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
199e5d79 8477 DRM_DEBUG_KMS("Using SSC on panel\n");
74cfd7ac 8478 val |= DREF_SSC1_ENABLE;
e77166b5 8479 } else
74cfd7ac 8480 val &= ~DREF_SSC1_ENABLE;
199e5d79
KP
8481
8482 /* Get SSC going before enabling the outputs */
74cfd7ac 8483 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
8484 POSTING_READ(PCH_DREF_CONTROL);
8485 udelay(200);
8486
74cfd7ac 8487 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
13d83a67
JB
8488
8489 /* Enable CPU source on CPU attached eDP */
199e5d79 8490 if (has_cpu_edp) {
99eb6a01 8491 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
199e5d79 8492 DRM_DEBUG_KMS("Using SSC on eDP\n");
74cfd7ac 8493 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
eba905b2 8494 } else
74cfd7ac 8495 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
199e5d79 8496 } else
74cfd7ac 8497 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
199e5d79 8498
74cfd7ac 8499 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
8500 POSTING_READ(PCH_DREF_CONTROL);
8501 udelay(200);
8502 } else {
1c1a24d2 8503 DRM_DEBUG_KMS("Disabling CPU source output\n");
199e5d79 8504
74cfd7ac 8505 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
199e5d79
KP
8506
8507 /* Turn off CPU output */
74cfd7ac 8508 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
199e5d79 8509
74cfd7ac 8510 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
8511 POSTING_READ(PCH_DREF_CONTROL);
8512 udelay(200);
8513
1c1a24d2
L
8514 if (!using_ssc_source) {
8515 DRM_DEBUG_KMS("Disabling SSC source\n");
199e5d79 8516
1c1a24d2
L
8517 /* Turn off the SSC source */
8518 val &= ~DREF_SSC_SOURCE_MASK;
8519 val |= DREF_SSC_SOURCE_DISABLE;
f165d283 8520
1c1a24d2
L
8521 /* Turn off SSC1 */
8522 val &= ~DREF_SSC1_ENABLE;
8523
8524 I915_WRITE(PCH_DREF_CONTROL, val);
8525 POSTING_READ(PCH_DREF_CONTROL);
8526 udelay(200);
8527 }
13d83a67 8528 }
74cfd7ac
CW
8529
8530 BUG_ON(val != final);
13d83a67
JB
8531}
8532
f31f2d55 8533static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
dde86e2d 8534{
f31f2d55 8535 uint32_t tmp;
dde86e2d 8536
0ff066a9
PZ
8537 tmp = I915_READ(SOUTH_CHICKEN2);
8538 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
8539 I915_WRITE(SOUTH_CHICKEN2, tmp);
dde86e2d 8540
cf3598c2
ID
8541 if (wait_for_us(I915_READ(SOUTH_CHICKEN2) &
8542 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
0ff066a9 8543 DRM_ERROR("FDI mPHY reset assert timeout\n");
dde86e2d 8544
0ff066a9
PZ
8545 tmp = I915_READ(SOUTH_CHICKEN2);
8546 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
8547 I915_WRITE(SOUTH_CHICKEN2, tmp);
dde86e2d 8548
cf3598c2
ID
8549 if (wait_for_us((I915_READ(SOUTH_CHICKEN2) &
8550 FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
0ff066a9 8551 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
f31f2d55
PZ
8552}
8553
8554/* WaMPhyProgramming:hsw */
8555static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
8556{
8557 uint32_t tmp;
dde86e2d
PZ
8558
8559 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
8560 tmp &= ~(0xFF << 24);
8561 tmp |= (0x12 << 24);
8562 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
8563
dde86e2d
PZ
8564 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
8565 tmp |= (1 << 11);
8566 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
8567
8568 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
8569 tmp |= (1 << 11);
8570 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
8571
dde86e2d
PZ
8572 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
8573 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
8574 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
8575
8576 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
8577 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
8578 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
8579
0ff066a9
PZ
8580 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
8581 tmp &= ~(7 << 13);
8582 tmp |= (5 << 13);
8583 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
dde86e2d 8584
0ff066a9
PZ
8585 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
8586 tmp &= ~(7 << 13);
8587 tmp |= (5 << 13);
8588 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
dde86e2d
PZ
8589
8590 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
8591 tmp &= ~0xFF;
8592 tmp |= 0x1C;
8593 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
8594
8595 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
8596 tmp &= ~0xFF;
8597 tmp |= 0x1C;
8598 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
8599
8600 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
8601 tmp &= ~(0xFF << 16);
8602 tmp |= (0x1C << 16);
8603 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
8604
8605 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
8606 tmp &= ~(0xFF << 16);
8607 tmp |= (0x1C << 16);
8608 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
8609
0ff066a9
PZ
8610 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
8611 tmp |= (1 << 27);
8612 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
dde86e2d 8613
0ff066a9
PZ
8614 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
8615 tmp |= (1 << 27);
8616 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
dde86e2d 8617
0ff066a9
PZ
8618 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
8619 tmp &= ~(0xF << 28);
8620 tmp |= (4 << 28);
8621 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
dde86e2d 8622
0ff066a9
PZ
8623 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
8624 tmp &= ~(0xF << 28);
8625 tmp |= (4 << 28);
8626 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
f31f2d55
PZ
8627}
8628
2fa86a1f
PZ
8629/* Implements 3 different sequences from BSpec chapter "Display iCLK
8630 * Programming" based on the parameters passed:
8631 * - Sequence to enable CLKOUT_DP
8632 * - Sequence to enable CLKOUT_DP without spread
8633 * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
8634 */
8635static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
8636 bool with_fdi)
f31f2d55
PZ
8637{
8638 struct drm_i915_private *dev_priv = dev->dev_private;
2fa86a1f
PZ
8639 uint32_t reg, tmp;
8640
8641 if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
8642 with_spread = true;
c2699524 8643 if (WARN(HAS_PCH_LPT_LP(dev) && with_fdi, "LP PCH doesn't have FDI\n"))
2fa86a1f 8644 with_fdi = false;
f31f2d55 8645
a580516d 8646 mutex_lock(&dev_priv->sb_lock);
f31f2d55
PZ
8647
8648 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8649 tmp &= ~SBI_SSCCTL_DISABLE;
8650 tmp |= SBI_SSCCTL_PATHALT;
8651 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8652
8653 udelay(24);
8654
2fa86a1f
PZ
8655 if (with_spread) {
8656 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8657 tmp &= ~SBI_SSCCTL_PATHALT;
8658 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
f31f2d55 8659
2fa86a1f
PZ
8660 if (with_fdi) {
8661 lpt_reset_fdi_mphy(dev_priv);
8662 lpt_program_fdi_mphy(dev_priv);
8663 }
8664 }
dde86e2d 8665
c2699524 8666 reg = HAS_PCH_LPT_LP(dev) ? SBI_GEN0 : SBI_DBUFF0;
2fa86a1f
PZ
8667 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8668 tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8669 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
c00db246 8670
a580516d 8671 mutex_unlock(&dev_priv->sb_lock);
dde86e2d
PZ
8672}
8673
47701c3b
PZ
8674/* Sequence to disable CLKOUT_DP */
8675static void lpt_disable_clkout_dp(struct drm_device *dev)
8676{
8677 struct drm_i915_private *dev_priv = dev->dev_private;
8678 uint32_t reg, tmp;
8679
a580516d 8680 mutex_lock(&dev_priv->sb_lock);
47701c3b 8681
c2699524 8682 reg = HAS_PCH_LPT_LP(dev) ? SBI_GEN0 : SBI_DBUFF0;
47701c3b
PZ
8683 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8684 tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8685 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
8686
8687 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8688 if (!(tmp & SBI_SSCCTL_DISABLE)) {
8689 if (!(tmp & SBI_SSCCTL_PATHALT)) {
8690 tmp |= SBI_SSCCTL_PATHALT;
8691 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8692 udelay(32);
8693 }
8694 tmp |= SBI_SSCCTL_DISABLE;
8695 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8696 }
8697
a580516d 8698 mutex_unlock(&dev_priv->sb_lock);
47701c3b
PZ
8699}
8700
f7be2c21
VS
8701#define BEND_IDX(steps) ((50 + (steps)) / 5)
8702
8703static const uint16_t sscdivintphase[] = {
8704 [BEND_IDX( 50)] = 0x3B23,
8705 [BEND_IDX( 45)] = 0x3B23,
8706 [BEND_IDX( 40)] = 0x3C23,
8707 [BEND_IDX( 35)] = 0x3C23,
8708 [BEND_IDX( 30)] = 0x3D23,
8709 [BEND_IDX( 25)] = 0x3D23,
8710 [BEND_IDX( 20)] = 0x3E23,
8711 [BEND_IDX( 15)] = 0x3E23,
8712 [BEND_IDX( 10)] = 0x3F23,
8713 [BEND_IDX( 5)] = 0x3F23,
8714 [BEND_IDX( 0)] = 0x0025,
8715 [BEND_IDX( -5)] = 0x0025,
8716 [BEND_IDX(-10)] = 0x0125,
8717 [BEND_IDX(-15)] = 0x0125,
8718 [BEND_IDX(-20)] = 0x0225,
8719 [BEND_IDX(-25)] = 0x0225,
8720 [BEND_IDX(-30)] = 0x0325,
8721 [BEND_IDX(-35)] = 0x0325,
8722 [BEND_IDX(-40)] = 0x0425,
8723 [BEND_IDX(-45)] = 0x0425,
8724 [BEND_IDX(-50)] = 0x0525,
8725};
8726
8727/*
8728 * Bend CLKOUT_DP
8729 * steps -50 to 50 inclusive, in steps of 5
8730 * < 0 slow down the clock, > 0 speed up the clock, 0 == no bend (135MHz)
8731 * change in clock period = -(steps / 10) * 5.787 ps
8732 */
8733static void lpt_bend_clkout_dp(struct drm_i915_private *dev_priv, int steps)
8734{
8735 uint32_t tmp;
8736 int idx = BEND_IDX(steps);
8737
8738 if (WARN_ON(steps % 5 != 0))
8739 return;
8740
8741 if (WARN_ON(idx >= ARRAY_SIZE(sscdivintphase)))
8742 return;
8743
8744 mutex_lock(&dev_priv->sb_lock);
8745
8746 if (steps % 10 != 0)
8747 tmp = 0xAAAAAAAB;
8748 else
8749 tmp = 0x00000000;
8750 intel_sbi_write(dev_priv, SBI_SSCDITHPHASE, tmp, SBI_ICLK);
8751
8752 tmp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE, SBI_ICLK);
8753 tmp &= 0xffff0000;
8754 tmp |= sscdivintphase[idx];
8755 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE, tmp, SBI_ICLK);
8756
8757 mutex_unlock(&dev_priv->sb_lock);
8758}
8759
8760#undef BEND_IDX
8761
bf8fa3d3
PZ
8762static void lpt_init_pch_refclk(struct drm_device *dev)
8763{
bf8fa3d3
PZ
8764 struct intel_encoder *encoder;
8765 bool has_vga = false;
8766
b2784e15 8767 for_each_intel_encoder(dev, encoder) {
bf8fa3d3
PZ
8768 switch (encoder->type) {
8769 case INTEL_OUTPUT_ANALOG:
8770 has_vga = true;
8771 break;
6847d71b
PZ
8772 default:
8773 break;
bf8fa3d3
PZ
8774 }
8775 }
8776
f7be2c21
VS
8777 if (has_vga) {
8778 lpt_bend_clkout_dp(to_i915(dev), 0);
47701c3b 8779 lpt_enable_clkout_dp(dev, true, true);
f7be2c21 8780 } else {
47701c3b 8781 lpt_disable_clkout_dp(dev);
f7be2c21 8782 }
bf8fa3d3
PZ
8783}
8784
dde86e2d
PZ
8785/*
8786 * Initialize reference clocks when the driver loads
8787 */
8788void intel_init_pch_refclk(struct drm_device *dev)
8789{
8790 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
8791 ironlake_init_pch_refclk(dev);
8792 else if (HAS_PCH_LPT(dev))
8793 lpt_init_pch_refclk(dev);
8794}
8795
6ff93609 8796static void ironlake_set_pipeconf(struct drm_crtc *crtc)
79e53945 8797{
c8203565 8798 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
79e53945
JB
8799 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8800 int pipe = intel_crtc->pipe;
c8203565
PZ
8801 uint32_t val;
8802
78114071 8803 val = 0;
c8203565 8804
6e3c9717 8805 switch (intel_crtc->config->pipe_bpp) {
c8203565 8806 case 18:
dfd07d72 8807 val |= PIPECONF_6BPC;
c8203565
PZ
8808 break;
8809 case 24:
dfd07d72 8810 val |= PIPECONF_8BPC;
c8203565
PZ
8811 break;
8812 case 30:
dfd07d72 8813 val |= PIPECONF_10BPC;
c8203565
PZ
8814 break;
8815 case 36:
dfd07d72 8816 val |= PIPECONF_12BPC;
c8203565
PZ
8817 break;
8818 default:
cc769b62
PZ
8819 /* Case prevented by intel_choose_pipe_bpp_dither. */
8820 BUG();
c8203565
PZ
8821 }
8822
6e3c9717 8823 if (intel_crtc->config->dither)
c8203565
PZ
8824 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8825
6e3c9717 8826 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
c8203565
PZ
8827 val |= PIPECONF_INTERLACED_ILK;
8828 else
8829 val |= PIPECONF_PROGRESSIVE;
8830
6e3c9717 8831 if (intel_crtc->config->limited_color_range)
3685a8f3 8832 val |= PIPECONF_COLOR_RANGE_SELECT;
3685a8f3 8833
c8203565
PZ
8834 I915_WRITE(PIPECONF(pipe), val);
8835 POSTING_READ(PIPECONF(pipe));
8836}
8837
6ff93609 8838static void haswell_set_pipeconf(struct drm_crtc *crtc)
ee2b0b38 8839{
391bf048 8840 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
ee2b0b38 8841 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6e3c9717 8842 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
391bf048 8843 u32 val = 0;
ee2b0b38 8844
391bf048 8845 if (IS_HASWELL(dev_priv) && intel_crtc->config->dither)
ee2b0b38
PZ
8846 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8847
6e3c9717 8848 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
ee2b0b38
PZ
8849 val |= PIPECONF_INTERLACED_ILK;
8850 else
8851 val |= PIPECONF_PROGRESSIVE;
8852
702e7a56
PZ
8853 I915_WRITE(PIPECONF(cpu_transcoder), val);
8854 POSTING_READ(PIPECONF(cpu_transcoder));
391bf048
JN
8855}
8856
391bf048
JN
8857static void haswell_set_pipemisc(struct drm_crtc *crtc)
8858{
8859 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
8860 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
756f85cf 8861
391bf048
JN
8862 if (IS_BROADWELL(dev_priv) || INTEL_INFO(dev_priv)->gen >= 9) {
8863 u32 val = 0;
756f85cf 8864
6e3c9717 8865 switch (intel_crtc->config->pipe_bpp) {
756f85cf
PZ
8866 case 18:
8867 val |= PIPEMISC_DITHER_6_BPC;
8868 break;
8869 case 24:
8870 val |= PIPEMISC_DITHER_8_BPC;
8871 break;
8872 case 30:
8873 val |= PIPEMISC_DITHER_10_BPC;
8874 break;
8875 case 36:
8876 val |= PIPEMISC_DITHER_12_BPC;
8877 break;
8878 default:
8879 /* Case prevented by pipe_config_set_bpp. */
8880 BUG();
8881 }
8882
6e3c9717 8883 if (intel_crtc->config->dither)
756f85cf
PZ
8884 val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
8885
391bf048 8886 I915_WRITE(PIPEMISC(intel_crtc->pipe), val);
756f85cf 8887 }
ee2b0b38
PZ
8888}
8889
d4b1931c
PZ
8890int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
8891{
8892 /*
8893 * Account for spread spectrum to avoid
8894 * oversubscribing the link. Max center spread
8895 * is 2.5%; use 5% for safety's sake.
8896 */
8897 u32 bps = target_clock * bpp * 21 / 20;
619d4d04 8898 return DIV_ROUND_UP(bps, link_bw * 8);
d4b1931c
PZ
8899}
8900
7429e9d4 8901static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
6cf86a5e 8902{
7429e9d4 8903 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
f48d8f23
PZ
8904}
8905
b75ca6f6
ACO
8906static void ironlake_compute_dpll(struct intel_crtc *intel_crtc,
8907 struct intel_crtc_state *crtc_state,
9e2c8475 8908 struct dpll *reduced_clock)
79e53945 8909{
de13a2e3 8910 struct drm_crtc *crtc = &intel_crtc->base;
79e53945
JB
8911 struct drm_device *dev = crtc->dev;
8912 struct drm_i915_private *dev_priv = dev->dev_private;
55bb9992 8913 struct drm_atomic_state *state = crtc_state->base.state;
da3ced29 8914 struct drm_connector *connector;
55bb9992
ACO
8915 struct drm_connector_state *connector_state;
8916 struct intel_encoder *encoder;
b75ca6f6 8917 u32 dpll, fp, fp2;
ceb41007 8918 int factor, i;
09ede541 8919 bool is_lvds = false, is_sdvo = false;
79e53945 8920
da3ced29 8921 for_each_connector_in_state(state, connector, connector_state, i) {
55bb9992
ACO
8922 if (connector_state->crtc != crtc_state->base.crtc)
8923 continue;
8924
8925 encoder = to_intel_encoder(connector_state->best_encoder);
8926
8927 switch (encoder->type) {
79e53945
JB
8928 case INTEL_OUTPUT_LVDS:
8929 is_lvds = true;
8930 break;
8931 case INTEL_OUTPUT_SDVO:
7d57382e 8932 case INTEL_OUTPUT_HDMI:
79e53945 8933 is_sdvo = true;
79e53945 8934 break;
6847d71b
PZ
8935 default:
8936 break;
79e53945
JB
8937 }
8938 }
79e53945 8939
c1858123 8940 /* Enable autotuning of the PLL clock (if permissible) */
8febb297
EA
8941 factor = 21;
8942 if (is_lvds) {
8943 if ((intel_panel_use_ssc(dev_priv) &&
e91e941b 8944 dev_priv->vbt.lvds_ssc_freq == 100000) ||
f0b44056 8945 (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
8febb297 8946 factor = 25;
190f68c5 8947 } else if (crtc_state->sdvo_tv_clock)
8febb297 8948 factor = 20;
c1858123 8949
b75ca6f6
ACO
8950 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
8951
190f68c5 8952 if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
b75ca6f6
ACO
8953 fp |= FP_CB_TUNE;
8954
8955 if (reduced_clock) {
8956 fp2 = i9xx_dpll_compute_fp(reduced_clock);
2c07245f 8957
b75ca6f6
ACO
8958 if (reduced_clock->m < factor * reduced_clock->n)
8959 fp2 |= FP_CB_TUNE;
8960 } else {
8961 fp2 = fp;
8962 }
9a7c7890 8963
5eddb70b 8964 dpll = 0;
2c07245f 8965
a07d6787
EA
8966 if (is_lvds)
8967 dpll |= DPLLB_MODE_LVDS;
8968 else
8969 dpll |= DPLLB_MODE_DAC_SERIAL;
198a037f 8970
190f68c5 8971 dpll |= (crtc_state->pixel_multiplier - 1)
ef1b460d 8972 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
198a037f
DV
8973
8974 if (is_sdvo)
4a33e48d 8975 dpll |= DPLL_SDVO_HIGH_SPEED;
190f68c5 8976 if (crtc_state->has_dp_encoder)
4a33e48d 8977 dpll |= DPLL_SDVO_HIGH_SPEED;
79e53945 8978
a07d6787 8979 /* compute bitmask from p1 value */
190f68c5 8980 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
a07d6787 8981 /* also FPA1 */
190f68c5 8982 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
a07d6787 8983
190f68c5 8984 switch (crtc_state->dpll.p2) {
a07d6787
EA
8985 case 5:
8986 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
8987 break;
8988 case 7:
8989 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
8990 break;
8991 case 10:
8992 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
8993 break;
8994 case 14:
8995 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
8996 break;
79e53945
JB
8997 }
8998
ceb41007 8999 if (is_lvds && intel_panel_use_ssc(dev_priv))
43565a06 9000 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
79e53945
JB
9001 else
9002 dpll |= PLL_REF_INPUT_DREFCLK;
9003
b75ca6f6
ACO
9004 dpll |= DPLL_VCO_ENABLE;
9005
9006 crtc_state->dpll_hw_state.dpll = dpll;
9007 crtc_state->dpll_hw_state.fp0 = fp;
9008 crtc_state->dpll_hw_state.fp1 = fp2;
de13a2e3
PZ
9009}
9010
190f68c5
ACO
9011static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
9012 struct intel_crtc_state *crtc_state)
de13a2e3 9013{
997c030c
ACO
9014 struct drm_device *dev = crtc->base.dev;
9015 struct drm_i915_private *dev_priv = dev->dev_private;
9e2c8475 9016 struct dpll reduced_clock;
7ed9f894 9017 bool has_reduced_clock = false;
e2b78267 9018 struct intel_shared_dpll *pll;
1b6f4958 9019 const struct intel_limit *limit;
997c030c 9020 int refclk = 120000;
de13a2e3 9021
dd3cd74a
ACO
9022 memset(&crtc_state->dpll_hw_state, 0,
9023 sizeof(crtc_state->dpll_hw_state));
9024
ded220e2
ACO
9025 crtc->lowfreq_avail = false;
9026
9027 /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
9028 if (!crtc_state->has_pch_encoder)
9029 return 0;
79e53945 9030
997c030c
ACO
9031 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
9032 if (intel_panel_use_ssc(dev_priv)) {
9033 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
9034 dev_priv->vbt.lvds_ssc_freq);
9035 refclk = dev_priv->vbt.lvds_ssc_freq;
9036 }
9037
9038 if (intel_is_dual_link_lvds(dev)) {
9039 if (refclk == 100000)
9040 limit = &intel_limits_ironlake_dual_lvds_100m;
9041 else
9042 limit = &intel_limits_ironlake_dual_lvds;
9043 } else {
9044 if (refclk == 100000)
9045 limit = &intel_limits_ironlake_single_lvds_100m;
9046 else
9047 limit = &intel_limits_ironlake_single_lvds;
9048 }
9049 } else {
9050 limit = &intel_limits_ironlake_dac;
9051 }
9052
364ee29d 9053 if (!crtc_state->clock_set &&
997c030c
ACO
9054 !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
9055 refclk, NULL, &crtc_state->dpll)) {
364ee29d
ACO
9056 DRM_ERROR("Couldn't find PLL settings for mode!\n");
9057 return -EINVAL;
f47709a9 9058 }
79e53945 9059
b75ca6f6
ACO
9060 ironlake_compute_dpll(crtc, crtc_state,
9061 has_reduced_clock ? &reduced_clock : NULL);
66e985c0 9062
ded220e2
ACO
9063 pll = intel_get_shared_dpll(crtc, crtc_state, NULL);
9064 if (pll == NULL) {
9065 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
9066 pipe_name(crtc->pipe));
9067 return -EINVAL;
3fb37703 9068 }
79e53945 9069
ded220e2
ACO
9070 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
9071 has_reduced_clock)
c7653199 9072 crtc->lowfreq_avail = true;
e2b78267 9073
c8f7a0db 9074 return 0;
79e53945
JB
9075}
9076
eb14cb74
VS
9077static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
9078 struct intel_link_m_n *m_n)
9079{
9080 struct drm_device *dev = crtc->base.dev;
9081 struct drm_i915_private *dev_priv = dev->dev_private;
9082 enum pipe pipe = crtc->pipe;
9083
9084 m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
9085 m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
9086 m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
9087 & ~TU_SIZE_MASK;
9088 m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
9089 m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
9090 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
9091}
9092
9093static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
9094 enum transcoder transcoder,
b95af8be
VK
9095 struct intel_link_m_n *m_n,
9096 struct intel_link_m_n *m2_n2)
72419203
DV
9097{
9098 struct drm_device *dev = crtc->base.dev;
9099 struct drm_i915_private *dev_priv = dev->dev_private;
eb14cb74 9100 enum pipe pipe = crtc->pipe;
72419203 9101
eb14cb74
VS
9102 if (INTEL_INFO(dev)->gen >= 5) {
9103 m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
9104 m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
9105 m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
9106 & ~TU_SIZE_MASK;
9107 m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
9108 m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
9109 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
b95af8be
VK
9110 /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
9111 * gen < 8) and if DRRS is supported (to make sure the
9112 * registers are not unnecessarily read).
9113 */
9114 if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
6e3c9717 9115 crtc->config->has_drrs) {
b95af8be
VK
9116 m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
9117 m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
9118 m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
9119 & ~TU_SIZE_MASK;
9120 m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
9121 m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
9122 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
9123 }
eb14cb74
VS
9124 } else {
9125 m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
9126 m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
9127 m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
9128 & ~TU_SIZE_MASK;
9129 m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
9130 m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
9131 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
9132 }
9133}
9134
9135void intel_dp_get_m_n(struct intel_crtc *crtc,
5cec258b 9136 struct intel_crtc_state *pipe_config)
eb14cb74 9137{
681a8504 9138 if (pipe_config->has_pch_encoder)
eb14cb74
VS
9139 intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
9140 else
9141 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
b95af8be
VK
9142 &pipe_config->dp_m_n,
9143 &pipe_config->dp_m2_n2);
eb14cb74 9144}
72419203 9145
eb14cb74 9146static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
5cec258b 9147 struct intel_crtc_state *pipe_config)
eb14cb74
VS
9148{
9149 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
b95af8be 9150 &pipe_config->fdi_m_n, NULL);
72419203
DV
9151}
9152
bd2e244f 9153static void skylake_get_pfit_config(struct intel_crtc *crtc,
5cec258b 9154 struct intel_crtc_state *pipe_config)
bd2e244f
JB
9155{
9156 struct drm_device *dev = crtc->base.dev;
9157 struct drm_i915_private *dev_priv = dev->dev_private;
a1b2278e
CK
9158 struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
9159 uint32_t ps_ctrl = 0;
9160 int id = -1;
9161 int i;
bd2e244f 9162
a1b2278e
CK
9163 /* find scaler attached to this pipe */
9164 for (i = 0; i < crtc->num_scalers; i++) {
9165 ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
9166 if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
9167 id = i;
9168 pipe_config->pch_pfit.enabled = true;
9169 pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
9170 pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
9171 break;
9172 }
9173 }
bd2e244f 9174
a1b2278e
CK
9175 scaler_state->scaler_id = id;
9176 if (id >= 0) {
9177 scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
9178 } else {
9179 scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
bd2e244f
JB
9180 }
9181}
9182
5724dbd1
DL
9183static void
9184skylake_get_initial_plane_config(struct intel_crtc *crtc,
9185 struct intel_initial_plane_config *plane_config)
bc8d7dff
DL
9186{
9187 struct drm_device *dev = crtc->base.dev;
9188 struct drm_i915_private *dev_priv = dev->dev_private;
40f46283 9189 u32 val, base, offset, stride_mult, tiling;
bc8d7dff
DL
9190 int pipe = crtc->pipe;
9191 int fourcc, pixel_format;
6761dd31 9192 unsigned int aligned_height;
bc8d7dff 9193 struct drm_framebuffer *fb;
1b842c89 9194 struct intel_framebuffer *intel_fb;
bc8d7dff 9195
d9806c9f 9196 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
1b842c89 9197 if (!intel_fb) {
bc8d7dff
DL
9198 DRM_DEBUG_KMS("failed to alloc fb\n");
9199 return;
9200 }
9201
1b842c89
DL
9202 fb = &intel_fb->base;
9203
bc8d7dff 9204 val = I915_READ(PLANE_CTL(pipe, 0));
42a7b088
DL
9205 if (!(val & PLANE_CTL_ENABLE))
9206 goto error;
9207
bc8d7dff
DL
9208 pixel_format = val & PLANE_CTL_FORMAT_MASK;
9209 fourcc = skl_format_to_fourcc(pixel_format,
9210 val & PLANE_CTL_ORDER_RGBX,
9211 val & PLANE_CTL_ALPHA_MASK);
9212 fb->pixel_format = fourcc;
9213 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
9214
40f46283
DL
9215 tiling = val & PLANE_CTL_TILED_MASK;
9216 switch (tiling) {
9217 case PLANE_CTL_TILED_LINEAR:
9218 fb->modifier[0] = DRM_FORMAT_MOD_NONE;
9219 break;
9220 case PLANE_CTL_TILED_X:
9221 plane_config->tiling = I915_TILING_X;
9222 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
9223 break;
9224 case PLANE_CTL_TILED_Y:
9225 fb->modifier[0] = I915_FORMAT_MOD_Y_TILED;
9226 break;
9227 case PLANE_CTL_TILED_YF:
9228 fb->modifier[0] = I915_FORMAT_MOD_Yf_TILED;
9229 break;
9230 default:
9231 MISSING_CASE(tiling);
9232 goto error;
9233 }
9234
bc8d7dff
DL
9235 base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
9236 plane_config->base = base;
9237
9238 offset = I915_READ(PLANE_OFFSET(pipe, 0));
9239
9240 val = I915_READ(PLANE_SIZE(pipe, 0));
9241 fb->height = ((val >> 16) & 0xfff) + 1;
9242 fb->width = ((val >> 0) & 0x1fff) + 1;
9243
9244 val = I915_READ(PLANE_STRIDE(pipe, 0));
7b49f948 9245 stride_mult = intel_fb_stride_alignment(dev_priv, fb->modifier[0],
40f46283 9246 fb->pixel_format);
bc8d7dff
DL
9247 fb->pitches[0] = (val & 0x3ff) * stride_mult;
9248
9249 aligned_height = intel_fb_align_height(dev, fb->height,
091df6cb
DV
9250 fb->pixel_format,
9251 fb->modifier[0]);
bc8d7dff 9252
f37b5c2b 9253 plane_config->size = fb->pitches[0] * aligned_height;
bc8d7dff
DL
9254
9255 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9256 pipe_name(pipe), fb->width, fb->height,
9257 fb->bits_per_pixel, base, fb->pitches[0],
9258 plane_config->size);
9259
2d14030b 9260 plane_config->fb = intel_fb;
bc8d7dff
DL
9261 return;
9262
9263error:
9264 kfree(fb);
9265}
9266
2fa2fe9a 9267static void ironlake_get_pfit_config(struct intel_crtc *crtc,
5cec258b 9268 struct intel_crtc_state *pipe_config)
2fa2fe9a
DV
9269{
9270 struct drm_device *dev = crtc->base.dev;
9271 struct drm_i915_private *dev_priv = dev->dev_private;
9272 uint32_t tmp;
9273
9274 tmp = I915_READ(PF_CTL(crtc->pipe));
9275
9276 if (tmp & PF_ENABLE) {
fd4daa9c 9277 pipe_config->pch_pfit.enabled = true;
2fa2fe9a
DV
9278 pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
9279 pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
cb8b2a30
DV
9280
9281 /* We currently do not free assignements of panel fitters on
9282 * ivb/hsw (since we don't use the higher upscaling modes which
9283 * differentiates them) so just WARN about this case for now. */
9284 if (IS_GEN7(dev)) {
9285 WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
9286 PF_PIPE_SEL_IVB(crtc->pipe));
9287 }
2fa2fe9a 9288 }
79e53945
JB
9289}
9290
5724dbd1
DL
9291static void
9292ironlake_get_initial_plane_config(struct intel_crtc *crtc,
9293 struct intel_initial_plane_config *plane_config)
4c6baa59
JB
9294{
9295 struct drm_device *dev = crtc->base.dev;
9296 struct drm_i915_private *dev_priv = dev->dev_private;
9297 u32 val, base, offset;
aeee5a49 9298 int pipe = crtc->pipe;
4c6baa59 9299 int fourcc, pixel_format;
6761dd31 9300 unsigned int aligned_height;
b113d5ee 9301 struct drm_framebuffer *fb;
1b842c89 9302 struct intel_framebuffer *intel_fb;
4c6baa59 9303
42a7b088
DL
9304 val = I915_READ(DSPCNTR(pipe));
9305 if (!(val & DISPLAY_PLANE_ENABLE))
9306 return;
9307
d9806c9f 9308 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
1b842c89 9309 if (!intel_fb) {
4c6baa59
JB
9310 DRM_DEBUG_KMS("failed to alloc fb\n");
9311 return;
9312 }
9313
1b842c89
DL
9314 fb = &intel_fb->base;
9315
18c5247e
DV
9316 if (INTEL_INFO(dev)->gen >= 4) {
9317 if (val & DISPPLANE_TILED) {
49af449b 9318 plane_config->tiling = I915_TILING_X;
18c5247e
DV
9319 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
9320 }
9321 }
4c6baa59
JB
9322
9323 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
b35d63fa 9324 fourcc = i9xx_format_to_fourcc(pixel_format);
b113d5ee
DL
9325 fb->pixel_format = fourcc;
9326 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
4c6baa59 9327
aeee5a49 9328 base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
4c6baa59 9329 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
aeee5a49 9330 offset = I915_READ(DSPOFFSET(pipe));
4c6baa59 9331 } else {
49af449b 9332 if (plane_config->tiling)
aeee5a49 9333 offset = I915_READ(DSPTILEOFF(pipe));
4c6baa59 9334 else
aeee5a49 9335 offset = I915_READ(DSPLINOFF(pipe));
4c6baa59
JB
9336 }
9337 plane_config->base = base;
9338
9339 val = I915_READ(PIPESRC(pipe));
b113d5ee
DL
9340 fb->width = ((val >> 16) & 0xfff) + 1;
9341 fb->height = ((val >> 0) & 0xfff) + 1;
4c6baa59
JB
9342
9343 val = I915_READ(DSPSTRIDE(pipe));
b113d5ee 9344 fb->pitches[0] = val & 0xffffffc0;
4c6baa59 9345
b113d5ee 9346 aligned_height = intel_fb_align_height(dev, fb->height,
091df6cb
DV
9347 fb->pixel_format,
9348 fb->modifier[0]);
4c6baa59 9349
f37b5c2b 9350 plane_config->size = fb->pitches[0] * aligned_height;
4c6baa59 9351
2844a921
DL
9352 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9353 pipe_name(pipe), fb->width, fb->height,
9354 fb->bits_per_pixel, base, fb->pitches[0],
9355 plane_config->size);
b113d5ee 9356
2d14030b 9357 plane_config->fb = intel_fb;
4c6baa59
JB
9358}
9359
0e8ffe1b 9360static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
5cec258b 9361 struct intel_crtc_state *pipe_config)
0e8ffe1b
DV
9362{
9363 struct drm_device *dev = crtc->base.dev;
9364 struct drm_i915_private *dev_priv = dev->dev_private;
1729050e 9365 enum intel_display_power_domain power_domain;
0e8ffe1b 9366 uint32_t tmp;
1729050e 9367 bool ret;
0e8ffe1b 9368
1729050e
ID
9369 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
9370 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
930e8c9e
PZ
9371 return false;
9372
e143a21c 9373 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
8106ddbd 9374 pipe_config->shared_dpll = NULL;
eccb140b 9375
1729050e 9376 ret = false;
0e8ffe1b
DV
9377 tmp = I915_READ(PIPECONF(crtc->pipe));
9378 if (!(tmp & PIPECONF_ENABLE))
1729050e 9379 goto out;
0e8ffe1b 9380
42571aef
VS
9381 switch (tmp & PIPECONF_BPC_MASK) {
9382 case PIPECONF_6BPC:
9383 pipe_config->pipe_bpp = 18;
9384 break;
9385 case PIPECONF_8BPC:
9386 pipe_config->pipe_bpp = 24;
9387 break;
9388 case PIPECONF_10BPC:
9389 pipe_config->pipe_bpp = 30;
9390 break;
9391 case PIPECONF_12BPC:
9392 pipe_config->pipe_bpp = 36;
9393 break;
9394 default:
9395 break;
9396 }
9397
b5a9fa09
DV
9398 if (tmp & PIPECONF_COLOR_RANGE_SELECT)
9399 pipe_config->limited_color_range = true;
9400
ab9412ba 9401 if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
66e985c0 9402 struct intel_shared_dpll *pll;
8106ddbd 9403 enum intel_dpll_id pll_id;
66e985c0 9404
88adfff1
DV
9405 pipe_config->has_pch_encoder = true;
9406
627eb5a3
DV
9407 tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
9408 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
9409 FDI_DP_PORT_WIDTH_SHIFT) + 1;
72419203
DV
9410
9411 ironlake_get_fdi_m_n_config(crtc, pipe_config);
6c49f241 9412
2d1fe073 9413 if (HAS_PCH_IBX(dev_priv)) {
d9a7bc67
ID
9414 /*
9415 * The pipe->pch transcoder and pch transcoder->pll
9416 * mapping is fixed.
9417 */
8106ddbd 9418 pll_id = (enum intel_dpll_id) crtc->pipe;
c0d43d62
DV
9419 } else {
9420 tmp = I915_READ(PCH_DPLL_SEL);
9421 if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
8106ddbd 9422 pll_id = DPLL_ID_PCH_PLL_B;
c0d43d62 9423 else
8106ddbd 9424 pll_id= DPLL_ID_PCH_PLL_A;
c0d43d62 9425 }
66e985c0 9426
8106ddbd
ACO
9427 pipe_config->shared_dpll =
9428 intel_get_shared_dpll_by_id(dev_priv, pll_id);
9429 pll = pipe_config->shared_dpll;
66e985c0 9430
2edd6443
ACO
9431 WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
9432 &pipe_config->dpll_hw_state));
c93f54cf
DV
9433
9434 tmp = pipe_config->dpll_hw_state.dpll;
9435 pipe_config->pixel_multiplier =
9436 ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
9437 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
18442d08
VS
9438
9439 ironlake_pch_clock_get(crtc, pipe_config);
6c49f241
DV
9440 } else {
9441 pipe_config->pixel_multiplier = 1;
627eb5a3
DV
9442 }
9443
1bd1bd80 9444 intel_get_pipe_timings(crtc, pipe_config);
bc58be60 9445 intel_get_pipe_src_size(crtc, pipe_config);
1bd1bd80 9446
2fa2fe9a
DV
9447 ironlake_get_pfit_config(crtc, pipe_config);
9448
1729050e
ID
9449 ret = true;
9450
9451out:
9452 intel_display_power_put(dev_priv, power_domain);
9453
9454 return ret;
0e8ffe1b
DV
9455}
9456
be256dc7
PZ
9457static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
9458{
9459 struct drm_device *dev = dev_priv->dev;
be256dc7 9460 struct intel_crtc *crtc;
be256dc7 9461
d3fcc808 9462 for_each_intel_crtc(dev, crtc)
e2c719b7 9463 I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
be256dc7
PZ
9464 pipe_name(crtc->pipe));
9465
e2c719b7
RC
9466 I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
9467 I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
01403de3
VS
9468 I915_STATE_WARN(I915_READ(WRPLL_CTL(0)) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
9469 I915_STATE_WARN(I915_READ(WRPLL_CTL(1)) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
e2c719b7
RC
9470 I915_STATE_WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
9471 I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
be256dc7 9472 "CPU PWM1 enabled\n");
c5107b87 9473 if (IS_HASWELL(dev))
e2c719b7 9474 I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
c5107b87 9475 "CPU PWM2 enabled\n");
e2c719b7 9476 I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
be256dc7 9477 "PCH PWM1 enabled\n");
e2c719b7 9478 I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
be256dc7 9479 "Utility pin enabled\n");
e2c719b7 9480 I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
be256dc7 9481
9926ada1
PZ
9482 /*
9483 * In theory we can still leave IRQs enabled, as long as only the HPD
9484 * interrupts remain enabled. We used to check for that, but since it's
9485 * gen-specific and since we only disable LCPLL after we fully disable
9486 * the interrupts, the check below should be enough.
9487 */
e2c719b7 9488 I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
be256dc7
PZ
9489}
9490
9ccd5aeb
PZ
9491static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
9492{
9493 struct drm_device *dev = dev_priv->dev;
9494
9495 if (IS_HASWELL(dev))
9496 return I915_READ(D_COMP_HSW);
9497 else
9498 return I915_READ(D_COMP_BDW);
9499}
9500
3c4c9b81
PZ
9501static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
9502{
9503 struct drm_device *dev = dev_priv->dev;
9504
9505 if (IS_HASWELL(dev)) {
9506 mutex_lock(&dev_priv->rps.hw_lock);
9507 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
9508 val))
f475dadf 9509 DRM_ERROR("Failed to write to D_COMP\n");
3c4c9b81
PZ
9510 mutex_unlock(&dev_priv->rps.hw_lock);
9511 } else {
9ccd5aeb
PZ
9512 I915_WRITE(D_COMP_BDW, val);
9513 POSTING_READ(D_COMP_BDW);
3c4c9b81 9514 }
be256dc7
PZ
9515}
9516
9517/*
9518 * This function implements pieces of two sequences from BSpec:
9519 * - Sequence for display software to disable LCPLL
9520 * - Sequence for display software to allow package C8+
9521 * The steps implemented here are just the steps that actually touch the LCPLL
9522 * register. Callers should take care of disabling all the display engine
9523 * functions, doing the mode unset, fixing interrupts, etc.
9524 */
6ff58d53
PZ
9525static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
9526 bool switch_to_fclk, bool allow_power_down)
be256dc7
PZ
9527{
9528 uint32_t val;
9529
9530 assert_can_disable_lcpll(dev_priv);
9531
9532 val = I915_READ(LCPLL_CTL);
9533
9534 if (switch_to_fclk) {
9535 val |= LCPLL_CD_SOURCE_FCLK;
9536 I915_WRITE(LCPLL_CTL, val);
9537
f53dd63f
ID
9538 if (wait_for_us(I915_READ(LCPLL_CTL) &
9539 LCPLL_CD_SOURCE_FCLK_DONE, 1))
be256dc7
PZ
9540 DRM_ERROR("Switching to FCLK failed\n");
9541
9542 val = I915_READ(LCPLL_CTL);
9543 }
9544
9545 val |= LCPLL_PLL_DISABLE;
9546 I915_WRITE(LCPLL_CTL, val);
9547 POSTING_READ(LCPLL_CTL);
9548
9549 if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
9550 DRM_ERROR("LCPLL still locked\n");
9551
9ccd5aeb 9552 val = hsw_read_dcomp(dev_priv);
be256dc7 9553 val |= D_COMP_COMP_DISABLE;
3c4c9b81 9554 hsw_write_dcomp(dev_priv, val);
be256dc7
PZ
9555 ndelay(100);
9556
9ccd5aeb
PZ
9557 if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
9558 1))
be256dc7
PZ
9559 DRM_ERROR("D_COMP RCOMP still in progress\n");
9560
9561 if (allow_power_down) {
9562 val = I915_READ(LCPLL_CTL);
9563 val |= LCPLL_POWER_DOWN_ALLOW;
9564 I915_WRITE(LCPLL_CTL, val);
9565 POSTING_READ(LCPLL_CTL);
9566 }
9567}
9568
9569/*
9570 * Fully restores LCPLL, disallowing power down and switching back to LCPLL
9571 * source.
9572 */
6ff58d53 9573static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
be256dc7
PZ
9574{
9575 uint32_t val;
9576
9577 val = I915_READ(LCPLL_CTL);
9578
9579 if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
9580 LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
9581 return;
9582
a8a8bd54
PZ
9583 /*
9584 * Make sure we're not on PC8 state before disabling PC8, otherwise
9585 * we'll hang the machine. To prevent PC8 state, just enable force_wake.
a8a8bd54 9586 */
59bad947 9587 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
215733fa 9588
be256dc7
PZ
9589 if (val & LCPLL_POWER_DOWN_ALLOW) {
9590 val &= ~LCPLL_POWER_DOWN_ALLOW;
9591 I915_WRITE(LCPLL_CTL, val);
35d8f2eb 9592 POSTING_READ(LCPLL_CTL);
be256dc7
PZ
9593 }
9594
9ccd5aeb 9595 val = hsw_read_dcomp(dev_priv);
be256dc7
PZ
9596 val |= D_COMP_COMP_FORCE;
9597 val &= ~D_COMP_COMP_DISABLE;
3c4c9b81 9598 hsw_write_dcomp(dev_priv, val);
be256dc7
PZ
9599
9600 val = I915_READ(LCPLL_CTL);
9601 val &= ~LCPLL_PLL_DISABLE;
9602 I915_WRITE(LCPLL_CTL, val);
9603
9604 if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
9605 DRM_ERROR("LCPLL not locked yet\n");
9606
9607 if (val & LCPLL_CD_SOURCE_FCLK) {
9608 val = I915_READ(LCPLL_CTL);
9609 val &= ~LCPLL_CD_SOURCE_FCLK;
9610 I915_WRITE(LCPLL_CTL, val);
9611
f53dd63f
ID
9612 if (wait_for_us((I915_READ(LCPLL_CTL) &
9613 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
be256dc7
PZ
9614 DRM_ERROR("Switching back to LCPLL failed\n");
9615 }
215733fa 9616
59bad947 9617 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
b6283055 9618 intel_update_cdclk(dev_priv->dev);
be256dc7
PZ
9619}
9620
765dab67
PZ
9621/*
9622 * Package states C8 and deeper are really deep PC states that can only be
9623 * reached when all the devices on the system allow it, so even if the graphics
9624 * device allows PC8+, it doesn't mean the system will actually get to these
9625 * states. Our driver only allows PC8+ when going into runtime PM.
9626 *
9627 * The requirements for PC8+ are that all the outputs are disabled, the power
9628 * well is disabled and most interrupts are disabled, and these are also
9629 * requirements for runtime PM. When these conditions are met, we manually do
9630 * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
9631 * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
9632 * hang the machine.
9633 *
9634 * When we really reach PC8 or deeper states (not just when we allow it) we lose
9635 * the state of some registers, so when we come back from PC8+ we need to
9636 * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
9637 * need to take care of the registers kept by RC6. Notice that this happens even
9638 * if we don't put the device in PCI D3 state (which is what currently happens
9639 * because of the runtime PM support).
9640 *
9641 * For more, read "Display Sequences for Package C8" on the hardware
9642 * documentation.
9643 */
a14cb6fc 9644void hsw_enable_pc8(struct drm_i915_private *dev_priv)
c67a470b 9645{
c67a470b
PZ
9646 struct drm_device *dev = dev_priv->dev;
9647 uint32_t val;
9648
c67a470b
PZ
9649 DRM_DEBUG_KMS("Enabling package C8+\n");
9650
c2699524 9651 if (HAS_PCH_LPT_LP(dev)) {
c67a470b
PZ
9652 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9653 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
9654 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9655 }
9656
9657 lpt_disable_clkout_dp(dev);
c67a470b
PZ
9658 hsw_disable_lcpll(dev_priv, true, true);
9659}
9660
a14cb6fc 9661void hsw_disable_pc8(struct drm_i915_private *dev_priv)
c67a470b
PZ
9662{
9663 struct drm_device *dev = dev_priv->dev;
9664 uint32_t val;
9665
c67a470b
PZ
9666 DRM_DEBUG_KMS("Disabling package C8+\n");
9667
9668 hsw_restore_lcpll(dev_priv);
c67a470b
PZ
9669 lpt_init_pch_refclk(dev);
9670
c2699524 9671 if (HAS_PCH_LPT_LP(dev)) {
c67a470b
PZ
9672 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9673 val |= PCH_LP_PARTITION_LEVEL_DISABLE;
9674 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9675 }
c67a470b
PZ
9676}
9677
324513c0 9678static void bxt_modeset_commit_cdclk(struct drm_atomic_state *old_state)
f8437dd1 9679{
a821fc46 9680 struct drm_device *dev = old_state->dev;
1a617b77
ML
9681 struct intel_atomic_state *old_intel_state =
9682 to_intel_atomic_state(old_state);
9683 unsigned int req_cdclk = old_intel_state->dev_cdclk;
f8437dd1 9684
324513c0 9685 bxt_set_cdclk(to_i915(dev), req_cdclk);
f8437dd1
VK
9686}
9687
b432e5cf 9688/* compute the max rate for new configuration */
27c329ed 9689static int ilk_max_pixel_rate(struct drm_atomic_state *state)
b432e5cf 9690{
565602d7
ML
9691 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
9692 struct drm_i915_private *dev_priv = state->dev->dev_private;
9693 struct drm_crtc *crtc;
9694 struct drm_crtc_state *cstate;
27c329ed 9695 struct intel_crtc_state *crtc_state;
565602d7
ML
9696 unsigned max_pixel_rate = 0, i;
9697 enum pipe pipe;
b432e5cf 9698
565602d7
ML
9699 memcpy(intel_state->min_pixclk, dev_priv->min_pixclk,
9700 sizeof(intel_state->min_pixclk));
27c329ed 9701
565602d7
ML
9702 for_each_crtc_in_state(state, crtc, cstate, i) {
9703 int pixel_rate;
27c329ed 9704
565602d7
ML
9705 crtc_state = to_intel_crtc_state(cstate);
9706 if (!crtc_state->base.enable) {
9707 intel_state->min_pixclk[i] = 0;
b432e5cf 9708 continue;
565602d7 9709 }
b432e5cf 9710
27c329ed 9711 pixel_rate = ilk_pipe_pixel_rate(crtc_state);
b432e5cf
VS
9712
9713 /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
565602d7 9714 if (IS_BROADWELL(dev_priv) && crtc_state->ips_enabled)
b432e5cf
VS
9715 pixel_rate = DIV_ROUND_UP(pixel_rate * 100, 95);
9716
565602d7 9717 intel_state->min_pixclk[i] = pixel_rate;
b432e5cf
VS
9718 }
9719
565602d7
ML
9720 for_each_pipe(dev_priv, pipe)
9721 max_pixel_rate = max(intel_state->min_pixclk[pipe], max_pixel_rate);
9722
b432e5cf
VS
9723 return max_pixel_rate;
9724}
9725
9726static void broadwell_set_cdclk(struct drm_device *dev, int cdclk)
9727{
9728 struct drm_i915_private *dev_priv = dev->dev_private;
9729 uint32_t val, data;
9730 int ret;
9731
9732 if (WARN((I915_READ(LCPLL_CTL) &
9733 (LCPLL_PLL_DISABLE | LCPLL_PLL_LOCK |
9734 LCPLL_CD_CLOCK_DISABLE | LCPLL_ROOT_CD_CLOCK_DISABLE |
9735 LCPLL_CD2X_CLOCK_DISABLE | LCPLL_POWER_DOWN_ALLOW |
9736 LCPLL_CD_SOURCE_FCLK)) != LCPLL_PLL_LOCK,
9737 "trying to change cdclk frequency with cdclk not enabled\n"))
9738 return;
9739
9740 mutex_lock(&dev_priv->rps.hw_lock);
9741 ret = sandybridge_pcode_write(dev_priv,
9742 BDW_PCODE_DISPLAY_FREQ_CHANGE_REQ, 0x0);
9743 mutex_unlock(&dev_priv->rps.hw_lock);
9744 if (ret) {
9745 DRM_ERROR("failed to inform pcode about cdclk change\n");
9746 return;
9747 }
9748
9749 val = I915_READ(LCPLL_CTL);
9750 val |= LCPLL_CD_SOURCE_FCLK;
9751 I915_WRITE(LCPLL_CTL, val);
9752
5ba00178
TU
9753 if (wait_for_us(I915_READ(LCPLL_CTL) &
9754 LCPLL_CD_SOURCE_FCLK_DONE, 1))
b432e5cf
VS
9755 DRM_ERROR("Switching to FCLK failed\n");
9756
9757 val = I915_READ(LCPLL_CTL);
9758 val &= ~LCPLL_CLK_FREQ_MASK;
9759
9760 switch (cdclk) {
9761 case 450000:
9762 val |= LCPLL_CLK_FREQ_450;
9763 data = 0;
9764 break;
9765 case 540000:
9766 val |= LCPLL_CLK_FREQ_54O_BDW;
9767 data = 1;
9768 break;
9769 case 337500:
9770 val |= LCPLL_CLK_FREQ_337_5_BDW;
9771 data = 2;
9772 break;
9773 case 675000:
9774 val |= LCPLL_CLK_FREQ_675_BDW;
9775 data = 3;
9776 break;
9777 default:
9778 WARN(1, "invalid cdclk frequency\n");
9779 return;
9780 }
9781
9782 I915_WRITE(LCPLL_CTL, val);
9783
9784 val = I915_READ(LCPLL_CTL);
9785 val &= ~LCPLL_CD_SOURCE_FCLK;
9786 I915_WRITE(LCPLL_CTL, val);
9787
5ba00178
TU
9788 if (wait_for_us((I915_READ(LCPLL_CTL) &
9789 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
b432e5cf
VS
9790 DRM_ERROR("Switching back to LCPLL failed\n");
9791
9792 mutex_lock(&dev_priv->rps.hw_lock);
9793 sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ, data);
9794 mutex_unlock(&dev_priv->rps.hw_lock);
9795
7f1052a8
VS
9796 I915_WRITE(CDCLK_FREQ, DIV_ROUND_CLOSEST(cdclk, 1000) - 1);
9797
b432e5cf
VS
9798 intel_update_cdclk(dev);
9799
9800 WARN(cdclk != dev_priv->cdclk_freq,
9801 "cdclk requested %d kHz but got %d kHz\n",
9802 cdclk, dev_priv->cdclk_freq);
9803}
9804
587c7914
VS
9805static int broadwell_calc_cdclk(int max_pixclk)
9806{
9807 if (max_pixclk > 540000)
9808 return 675000;
9809 else if (max_pixclk > 450000)
9810 return 540000;
9811 else if (max_pixclk > 337500)
9812 return 450000;
9813 else
9814 return 337500;
9815}
9816
27c329ed 9817static int broadwell_modeset_calc_cdclk(struct drm_atomic_state *state)
b432e5cf 9818{
27c329ed 9819 struct drm_i915_private *dev_priv = to_i915(state->dev);
1a617b77 9820 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
27c329ed 9821 int max_pixclk = ilk_max_pixel_rate(state);
b432e5cf
VS
9822 int cdclk;
9823
9824 /*
9825 * FIXME should also account for plane ratio
9826 * once 64bpp pixel formats are supported.
9827 */
587c7914 9828 cdclk = broadwell_calc_cdclk(max_pixclk);
b432e5cf 9829
b432e5cf 9830 if (cdclk > dev_priv->max_cdclk_freq) {
63ba534e
ML
9831 DRM_DEBUG_KMS("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
9832 cdclk, dev_priv->max_cdclk_freq);
9833 return -EINVAL;
b432e5cf
VS
9834 }
9835
1a617b77
ML
9836 intel_state->cdclk = intel_state->dev_cdclk = cdclk;
9837 if (!intel_state->active_crtcs)
587c7914 9838 intel_state->dev_cdclk = broadwell_calc_cdclk(0);
b432e5cf
VS
9839
9840 return 0;
9841}
9842
27c329ed 9843static void broadwell_modeset_commit_cdclk(struct drm_atomic_state *old_state)
b432e5cf 9844{
27c329ed 9845 struct drm_device *dev = old_state->dev;
1a617b77
ML
9846 struct intel_atomic_state *old_intel_state =
9847 to_intel_atomic_state(old_state);
9848 unsigned req_cdclk = old_intel_state->dev_cdclk;
b432e5cf 9849
27c329ed 9850 broadwell_set_cdclk(dev, req_cdclk);
b432e5cf
VS
9851}
9852
c89e39f3
CT
9853static int skl_modeset_calc_cdclk(struct drm_atomic_state *state)
9854{
9855 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
9856 struct drm_i915_private *dev_priv = to_i915(state->dev);
9857 const int max_pixclk = ilk_max_pixel_rate(state);
a8ca4934 9858 int vco = intel_state->cdclk_pll_vco;
c89e39f3
CT
9859 int cdclk;
9860
9861 /*
9862 * FIXME should also account for plane ratio
9863 * once 64bpp pixel formats are supported.
9864 */
a8ca4934 9865 cdclk = skl_calc_cdclk(max_pixclk, vco);
c89e39f3
CT
9866
9867 /*
9868 * FIXME move the cdclk caclulation to
9869 * compute_config() so we can fail gracegully.
9870 */
9871 if (cdclk > dev_priv->max_cdclk_freq) {
9872 DRM_ERROR("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
9873 cdclk, dev_priv->max_cdclk_freq);
9874 cdclk = dev_priv->max_cdclk_freq;
9875 }
9876
9877 intel_state->cdclk = intel_state->dev_cdclk = cdclk;
9878 if (!intel_state->active_crtcs)
a8ca4934 9879 intel_state->dev_cdclk = skl_calc_cdclk(0, vco);
c89e39f3
CT
9880
9881 return 0;
9882}
9883
9884static void skl_modeset_commit_cdclk(struct drm_atomic_state *old_state)
9885{
1cd593e0
VS
9886 struct drm_i915_private *dev_priv = to_i915(old_state->dev);
9887 struct intel_atomic_state *intel_state = to_intel_atomic_state(old_state);
9888 unsigned int req_cdclk = intel_state->dev_cdclk;
9889 unsigned int req_vco = intel_state->cdclk_pll_vco;
c89e39f3 9890
1cd593e0 9891 skl_set_cdclk(dev_priv, req_cdclk, req_vco);
c89e39f3
CT
9892}
9893
190f68c5
ACO
9894static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
9895 struct intel_crtc_state *crtc_state)
09b4ddf9 9896{
af3997b5
MK
9897 struct intel_encoder *intel_encoder =
9898 intel_ddi_get_crtc_new_encoder(crtc_state);
9899
9900 if (intel_encoder->type != INTEL_OUTPUT_DSI) {
9901 if (!intel_ddi_pll_select(crtc, crtc_state))
9902 return -EINVAL;
9903 }
716c2e55 9904
c7653199 9905 crtc->lowfreq_avail = false;
644cef34 9906
c8f7a0db 9907 return 0;
79e53945
JB
9908}
9909
3760b59c
S
9910static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
9911 enum port port,
9912 struct intel_crtc_state *pipe_config)
9913{
8106ddbd
ACO
9914 enum intel_dpll_id id;
9915
3760b59c
S
9916 switch (port) {
9917 case PORT_A:
9918 pipe_config->ddi_pll_sel = SKL_DPLL0;
08250c4b 9919 id = DPLL_ID_SKL_DPLL0;
3760b59c
S
9920 break;
9921 case PORT_B:
9922 pipe_config->ddi_pll_sel = SKL_DPLL1;
08250c4b 9923 id = DPLL_ID_SKL_DPLL1;
3760b59c
S
9924 break;
9925 case PORT_C:
9926 pipe_config->ddi_pll_sel = SKL_DPLL2;
08250c4b 9927 id = DPLL_ID_SKL_DPLL2;
3760b59c
S
9928 break;
9929 default:
9930 DRM_ERROR("Incorrect port type\n");
8106ddbd 9931 return;
3760b59c 9932 }
8106ddbd
ACO
9933
9934 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
3760b59c
S
9935}
9936
96b7dfb7
S
9937static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
9938 enum port port,
5cec258b 9939 struct intel_crtc_state *pipe_config)
96b7dfb7 9940{
8106ddbd 9941 enum intel_dpll_id id;
a3c988ea 9942 u32 temp;
96b7dfb7
S
9943
9944 temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
9945 pipe_config->ddi_pll_sel = temp >> (port * 3 + 1);
9946
9947 switch (pipe_config->ddi_pll_sel) {
3148ade7 9948 case SKL_DPLL0:
a3c988ea
ACO
9949 id = DPLL_ID_SKL_DPLL0;
9950 break;
96b7dfb7 9951 case SKL_DPLL1:
8106ddbd 9952 id = DPLL_ID_SKL_DPLL1;
96b7dfb7
S
9953 break;
9954 case SKL_DPLL2:
8106ddbd 9955 id = DPLL_ID_SKL_DPLL2;
96b7dfb7
S
9956 break;
9957 case SKL_DPLL3:
8106ddbd 9958 id = DPLL_ID_SKL_DPLL3;
96b7dfb7 9959 break;
8106ddbd
ACO
9960 default:
9961 MISSING_CASE(pipe_config->ddi_pll_sel);
9962 return;
96b7dfb7 9963 }
8106ddbd
ACO
9964
9965 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
96b7dfb7
S
9966}
9967
7d2c8175
DL
9968static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
9969 enum port port,
5cec258b 9970 struct intel_crtc_state *pipe_config)
7d2c8175 9971{
8106ddbd
ACO
9972 enum intel_dpll_id id;
9973
7d2c8175
DL
9974 pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
9975
9976 switch (pipe_config->ddi_pll_sel) {
9977 case PORT_CLK_SEL_WRPLL1:
8106ddbd 9978 id = DPLL_ID_WRPLL1;
7d2c8175
DL
9979 break;
9980 case PORT_CLK_SEL_WRPLL2:
8106ddbd 9981 id = DPLL_ID_WRPLL2;
7d2c8175 9982 break;
00490c22 9983 case PORT_CLK_SEL_SPLL:
8106ddbd 9984 id = DPLL_ID_SPLL;
79bd23da 9985 break;
9d16da65
ACO
9986 case PORT_CLK_SEL_LCPLL_810:
9987 id = DPLL_ID_LCPLL_810;
9988 break;
9989 case PORT_CLK_SEL_LCPLL_1350:
9990 id = DPLL_ID_LCPLL_1350;
9991 break;
9992 case PORT_CLK_SEL_LCPLL_2700:
9993 id = DPLL_ID_LCPLL_2700;
9994 break;
8106ddbd
ACO
9995 default:
9996 MISSING_CASE(pipe_config->ddi_pll_sel);
9997 /* fall through */
9998 case PORT_CLK_SEL_NONE:
8106ddbd 9999 return;
7d2c8175 10000 }
8106ddbd
ACO
10001
10002 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
7d2c8175
DL
10003}
10004
cf30429e
JN
10005static bool hsw_get_transcoder_state(struct intel_crtc *crtc,
10006 struct intel_crtc_state *pipe_config,
10007 unsigned long *power_domain_mask)
10008{
10009 struct drm_device *dev = crtc->base.dev;
10010 struct drm_i915_private *dev_priv = dev->dev_private;
10011 enum intel_display_power_domain power_domain;
10012 u32 tmp;
10013
d9a7bc67
ID
10014 /*
10015 * The pipe->transcoder mapping is fixed with the exception of the eDP
10016 * transcoder handled below.
10017 */
cf30429e
JN
10018 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
10019
10020 /*
10021 * XXX: Do intel_display_power_get_if_enabled before reading this (for
10022 * consistency and less surprising code; it's in always on power).
10023 */
10024 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
10025 if (tmp & TRANS_DDI_FUNC_ENABLE) {
10026 enum pipe trans_edp_pipe;
10027 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
10028 default:
10029 WARN(1, "unknown pipe linked to edp transcoder\n");
10030 case TRANS_DDI_EDP_INPUT_A_ONOFF:
10031 case TRANS_DDI_EDP_INPUT_A_ON:
10032 trans_edp_pipe = PIPE_A;
10033 break;
10034 case TRANS_DDI_EDP_INPUT_B_ONOFF:
10035 trans_edp_pipe = PIPE_B;
10036 break;
10037 case TRANS_DDI_EDP_INPUT_C_ONOFF:
10038 trans_edp_pipe = PIPE_C;
10039 break;
10040 }
10041
10042 if (trans_edp_pipe == crtc->pipe)
10043 pipe_config->cpu_transcoder = TRANSCODER_EDP;
10044 }
10045
10046 power_domain = POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder);
10047 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
10048 return false;
10049 *power_domain_mask |= BIT(power_domain);
10050
10051 tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
10052
10053 return tmp & PIPECONF_ENABLE;
10054}
10055
4d1de975
JN
10056static bool bxt_get_dsi_transcoder_state(struct intel_crtc *crtc,
10057 struct intel_crtc_state *pipe_config,
10058 unsigned long *power_domain_mask)
10059{
10060 struct drm_device *dev = crtc->base.dev;
10061 struct drm_i915_private *dev_priv = dev->dev_private;
10062 enum intel_display_power_domain power_domain;
10063 enum port port;
10064 enum transcoder cpu_transcoder;
10065 u32 tmp;
10066
10067 pipe_config->has_dsi_encoder = false;
10068
10069 for_each_port_masked(port, BIT(PORT_A) | BIT(PORT_C)) {
10070 if (port == PORT_A)
10071 cpu_transcoder = TRANSCODER_DSI_A;
10072 else
10073 cpu_transcoder = TRANSCODER_DSI_C;
10074
10075 power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
10076 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
10077 continue;
10078 *power_domain_mask |= BIT(power_domain);
10079
db18b6a6
ID
10080 /*
10081 * The PLL needs to be enabled with a valid divider
10082 * configuration, otherwise accessing DSI registers will hang
10083 * the machine. See BSpec North Display Engine
10084 * registers/MIPI[BXT]. We can break out here early, since we
10085 * need the same DSI PLL to be enabled for both DSI ports.
10086 */
10087 if (!intel_dsi_pll_is_enabled(dev_priv))
10088 break;
10089
4d1de975
JN
10090 /* XXX: this works for video mode only */
10091 tmp = I915_READ(BXT_MIPI_PORT_CTRL(port));
10092 if (!(tmp & DPI_ENABLE))
10093 continue;
10094
10095 tmp = I915_READ(MIPI_CTRL(port));
10096 if ((tmp & BXT_PIPE_SELECT_MASK) != BXT_PIPE_SELECT(crtc->pipe))
10097 continue;
10098
10099 pipe_config->cpu_transcoder = cpu_transcoder;
10100 pipe_config->has_dsi_encoder = true;
10101 break;
10102 }
10103
10104 return pipe_config->has_dsi_encoder;
10105}
10106
26804afd 10107static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
5cec258b 10108 struct intel_crtc_state *pipe_config)
26804afd
DV
10109{
10110 struct drm_device *dev = crtc->base.dev;
10111 struct drm_i915_private *dev_priv = dev->dev_private;
d452c5b6 10112 struct intel_shared_dpll *pll;
26804afd
DV
10113 enum port port;
10114 uint32_t tmp;
10115
10116 tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
10117
10118 port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
10119
ef11bdb3 10120 if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev))
96b7dfb7 10121 skylake_get_ddi_pll(dev_priv, port, pipe_config);
3760b59c
S
10122 else if (IS_BROXTON(dev))
10123 bxt_get_ddi_pll(dev_priv, port, pipe_config);
96b7dfb7
S
10124 else
10125 haswell_get_ddi_pll(dev_priv, port, pipe_config);
9cd86933 10126
8106ddbd
ACO
10127 pll = pipe_config->shared_dpll;
10128 if (pll) {
2edd6443
ACO
10129 WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
10130 &pipe_config->dpll_hw_state));
d452c5b6
DV
10131 }
10132
26804afd
DV
10133 /*
10134 * Haswell has only FDI/PCH transcoder A. It is which is connected to
10135 * DDI E. So just check whether this pipe is wired to DDI E and whether
10136 * the PCH transcoder is on.
10137 */
ca370455
DL
10138 if (INTEL_INFO(dev)->gen < 9 &&
10139 (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
26804afd
DV
10140 pipe_config->has_pch_encoder = true;
10141
10142 tmp = I915_READ(FDI_RX_CTL(PIPE_A));
10143 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
10144 FDI_DP_PORT_WIDTH_SHIFT) + 1;
10145
10146 ironlake_get_fdi_m_n_config(crtc, pipe_config);
10147 }
10148}
10149
0e8ffe1b 10150static bool haswell_get_pipe_config(struct intel_crtc *crtc,
5cec258b 10151 struct intel_crtc_state *pipe_config)
0e8ffe1b
DV
10152{
10153 struct drm_device *dev = crtc->base.dev;
10154 struct drm_i915_private *dev_priv = dev->dev_private;
1729050e
ID
10155 enum intel_display_power_domain power_domain;
10156 unsigned long power_domain_mask;
cf30429e 10157 bool active;
0e8ffe1b 10158
1729050e
ID
10159 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
10160 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
b5482bd0 10161 return false;
1729050e
ID
10162 power_domain_mask = BIT(power_domain);
10163
8106ddbd 10164 pipe_config->shared_dpll = NULL;
c0d43d62 10165
cf30429e 10166 active = hsw_get_transcoder_state(crtc, pipe_config, &power_domain_mask);
eccb140b 10167
4d1de975
JN
10168 if (IS_BROXTON(dev_priv)) {
10169 bxt_get_dsi_transcoder_state(crtc, pipe_config,
10170 &power_domain_mask);
10171 WARN_ON(active && pipe_config->has_dsi_encoder);
10172 if (pipe_config->has_dsi_encoder)
10173 active = true;
10174 }
10175
cf30429e 10176 if (!active)
1729050e 10177 goto out;
0e8ffe1b 10178
4d1de975
JN
10179 if (!pipe_config->has_dsi_encoder) {
10180 haswell_get_ddi_port_state(crtc, pipe_config);
10181 intel_get_pipe_timings(crtc, pipe_config);
10182 }
627eb5a3 10183
bc58be60 10184 intel_get_pipe_src_size(crtc, pipe_config);
1bd1bd80 10185
05dc698c
LL
10186 pipe_config->gamma_mode =
10187 I915_READ(GAMMA_MODE(crtc->pipe)) & GAMMA_MODE_MODE_MASK;
10188
a1b2278e
CK
10189 if (INTEL_INFO(dev)->gen >= 9) {
10190 skl_init_scalers(dev, crtc, pipe_config);
10191 }
10192
af99ceda
CK
10193 if (INTEL_INFO(dev)->gen >= 9) {
10194 pipe_config->scaler_state.scaler_id = -1;
10195 pipe_config->scaler_state.scaler_users &= ~(1 << SKL_CRTC_INDEX);
10196 }
10197
1729050e
ID
10198 power_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
10199 if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
10200 power_domain_mask |= BIT(power_domain);
1c132b44 10201 if (INTEL_INFO(dev)->gen >= 9)
bd2e244f 10202 skylake_get_pfit_config(crtc, pipe_config);
ff6d9f55 10203 else
1c132b44 10204 ironlake_get_pfit_config(crtc, pipe_config);
bd2e244f 10205 }
88adfff1 10206
e59150dc
JB
10207 if (IS_HASWELL(dev))
10208 pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
10209 (I915_READ(IPS_CTL) & IPS_ENABLE);
42db64ef 10210
4d1de975
JN
10211 if (pipe_config->cpu_transcoder != TRANSCODER_EDP &&
10212 !transcoder_is_dsi(pipe_config->cpu_transcoder)) {
ebb69c95
CT
10213 pipe_config->pixel_multiplier =
10214 I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
10215 } else {
10216 pipe_config->pixel_multiplier = 1;
10217 }
6c49f241 10218
1729050e
ID
10219out:
10220 for_each_power_domain(power_domain, power_domain_mask)
10221 intel_display_power_put(dev_priv, power_domain);
10222
cf30429e 10223 return active;
0e8ffe1b
DV
10224}
10225
55a08b3f
ML
10226static void i845_update_cursor(struct drm_crtc *crtc, u32 base,
10227 const struct intel_plane_state *plane_state)
560b85bb
CW
10228{
10229 struct drm_device *dev = crtc->dev;
10230 struct drm_i915_private *dev_priv = dev->dev_private;
10231 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
dc41c154 10232 uint32_t cntl = 0, size = 0;
560b85bb 10233
55a08b3f
ML
10234 if (plane_state && plane_state->visible) {
10235 unsigned int width = plane_state->base.crtc_w;
10236 unsigned int height = plane_state->base.crtc_h;
dc41c154
VS
10237 unsigned int stride = roundup_pow_of_two(width) * 4;
10238
10239 switch (stride) {
10240 default:
10241 WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
10242 width, stride);
10243 stride = 256;
10244 /* fallthrough */
10245 case 256:
10246 case 512:
10247 case 1024:
10248 case 2048:
10249 break;
4b0e333e
CW
10250 }
10251
dc41c154
VS
10252 cntl |= CURSOR_ENABLE |
10253 CURSOR_GAMMA_ENABLE |
10254 CURSOR_FORMAT_ARGB |
10255 CURSOR_STRIDE(stride);
10256
10257 size = (height << 12) | width;
4b0e333e 10258 }
560b85bb 10259
dc41c154
VS
10260 if (intel_crtc->cursor_cntl != 0 &&
10261 (intel_crtc->cursor_base != base ||
10262 intel_crtc->cursor_size != size ||
10263 intel_crtc->cursor_cntl != cntl)) {
10264 /* On these chipsets we can only modify the base/size/stride
10265 * whilst the cursor is disabled.
10266 */
0b87c24e
VS
10267 I915_WRITE(CURCNTR(PIPE_A), 0);
10268 POSTING_READ(CURCNTR(PIPE_A));
dc41c154 10269 intel_crtc->cursor_cntl = 0;
4b0e333e 10270 }
560b85bb 10271
99d1f387 10272 if (intel_crtc->cursor_base != base) {
0b87c24e 10273 I915_WRITE(CURBASE(PIPE_A), base);
99d1f387
VS
10274 intel_crtc->cursor_base = base;
10275 }
4726e0b0 10276
dc41c154
VS
10277 if (intel_crtc->cursor_size != size) {
10278 I915_WRITE(CURSIZE, size);
10279 intel_crtc->cursor_size = size;
4b0e333e 10280 }
560b85bb 10281
4b0e333e 10282 if (intel_crtc->cursor_cntl != cntl) {
0b87c24e
VS
10283 I915_WRITE(CURCNTR(PIPE_A), cntl);
10284 POSTING_READ(CURCNTR(PIPE_A));
4b0e333e 10285 intel_crtc->cursor_cntl = cntl;
560b85bb 10286 }
560b85bb
CW
10287}
10288
55a08b3f
ML
10289static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base,
10290 const struct intel_plane_state *plane_state)
65a21cd6
JB
10291{
10292 struct drm_device *dev = crtc->dev;
10293 struct drm_i915_private *dev_priv = dev->dev_private;
10294 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10295 int pipe = intel_crtc->pipe;
663f3122 10296 uint32_t cntl = 0;
4b0e333e 10297
55a08b3f 10298 if (plane_state && plane_state->visible) {
4b0e333e 10299 cntl = MCURSOR_GAMMA_ENABLE;
55a08b3f 10300 switch (plane_state->base.crtc_w) {
4726e0b0
SK
10301 case 64:
10302 cntl |= CURSOR_MODE_64_ARGB_AX;
10303 break;
10304 case 128:
10305 cntl |= CURSOR_MODE_128_ARGB_AX;
10306 break;
10307 case 256:
10308 cntl |= CURSOR_MODE_256_ARGB_AX;
10309 break;
10310 default:
55a08b3f 10311 MISSING_CASE(plane_state->base.crtc_w);
4726e0b0 10312 return;
65a21cd6 10313 }
4b0e333e 10314 cntl |= pipe << 28; /* Connect to correct pipe */
47bf17a7 10315
fc6f93bc 10316 if (HAS_DDI(dev))
47bf17a7 10317 cntl |= CURSOR_PIPE_CSC_ENABLE;
65a21cd6 10318
55a08b3f
ML
10319 if (plane_state->base.rotation == BIT(DRM_ROTATE_180))
10320 cntl |= CURSOR_ROTATE_180;
10321 }
4398ad45 10322
4b0e333e
CW
10323 if (intel_crtc->cursor_cntl != cntl) {
10324 I915_WRITE(CURCNTR(pipe), cntl);
10325 POSTING_READ(CURCNTR(pipe));
10326 intel_crtc->cursor_cntl = cntl;
65a21cd6 10327 }
4b0e333e 10328
65a21cd6 10329 /* and commit changes on next vblank */
5efb3e28
VS
10330 I915_WRITE(CURBASE(pipe), base);
10331 POSTING_READ(CURBASE(pipe));
99d1f387
VS
10332
10333 intel_crtc->cursor_base = base;
65a21cd6
JB
10334}
10335
cda4b7d3 10336/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
6b383a7f 10337static void intel_crtc_update_cursor(struct drm_crtc *crtc,
55a08b3f 10338 const struct intel_plane_state *plane_state)
cda4b7d3
CW
10339{
10340 struct drm_device *dev = crtc->dev;
10341 struct drm_i915_private *dev_priv = dev->dev_private;
10342 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10343 int pipe = intel_crtc->pipe;
55a08b3f
ML
10344 u32 base = intel_crtc->cursor_addr;
10345 u32 pos = 0;
cda4b7d3 10346
55a08b3f
ML
10347 if (plane_state) {
10348 int x = plane_state->base.crtc_x;
10349 int y = plane_state->base.crtc_y;
cda4b7d3 10350
55a08b3f
ML
10351 if (x < 0) {
10352 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
10353 x = -x;
10354 }
10355 pos |= x << CURSOR_X_SHIFT;
cda4b7d3 10356
55a08b3f
ML
10357 if (y < 0) {
10358 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
10359 y = -y;
10360 }
10361 pos |= y << CURSOR_Y_SHIFT;
10362
10363 /* ILK+ do this automagically */
10364 if (HAS_GMCH_DISPLAY(dev) &&
10365 plane_state->base.rotation == BIT(DRM_ROTATE_180)) {
10366 base += (plane_state->base.crtc_h *
10367 plane_state->base.crtc_w - 1) * 4;
10368 }
cda4b7d3 10369 }
cda4b7d3 10370
5efb3e28
VS
10371 I915_WRITE(CURPOS(pipe), pos);
10372
8ac54669 10373 if (IS_845G(dev) || IS_I865G(dev))
55a08b3f 10374 i845_update_cursor(crtc, base, plane_state);
5efb3e28 10375 else
55a08b3f 10376 i9xx_update_cursor(crtc, base, plane_state);
cda4b7d3
CW
10377}
10378
dc41c154
VS
10379static bool cursor_size_ok(struct drm_device *dev,
10380 uint32_t width, uint32_t height)
10381{
10382 if (width == 0 || height == 0)
10383 return false;
10384
10385 /*
10386 * 845g/865g are special in that they are only limited by
10387 * the width of their cursors, the height is arbitrary up to
10388 * the precision of the register. Everything else requires
10389 * square cursors, limited to a few power-of-two sizes.
10390 */
10391 if (IS_845G(dev) || IS_I865G(dev)) {
10392 if ((width & 63) != 0)
10393 return false;
10394
10395 if (width > (IS_845G(dev) ? 64 : 512))
10396 return false;
10397
10398 if (height > 1023)
10399 return false;
10400 } else {
10401 switch (width | height) {
10402 case 256:
10403 case 128:
10404 if (IS_GEN2(dev))
10405 return false;
10406 case 64:
10407 break;
10408 default:
10409 return false;
10410 }
10411 }
10412
10413 return true;
10414}
10415
79e53945
JB
10416/* VESA 640x480x72Hz mode to set on the pipe */
10417static struct drm_display_mode load_detect_mode = {
10418 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
10419 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
10420};
10421
a8bb6818
DV
10422struct drm_framebuffer *
10423__intel_framebuffer_create(struct drm_device *dev,
10424 struct drm_mode_fb_cmd2 *mode_cmd,
10425 struct drm_i915_gem_object *obj)
d2dff872
CW
10426{
10427 struct intel_framebuffer *intel_fb;
10428 int ret;
10429
10430 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
dcb1394e 10431 if (!intel_fb)
d2dff872 10432 return ERR_PTR(-ENOMEM);
d2dff872
CW
10433
10434 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
dd4916c5
DV
10435 if (ret)
10436 goto err;
d2dff872
CW
10437
10438 return &intel_fb->base;
dcb1394e 10439
dd4916c5 10440err:
dd4916c5 10441 kfree(intel_fb);
dd4916c5 10442 return ERR_PTR(ret);
d2dff872
CW
10443}
10444
b5ea642a 10445static struct drm_framebuffer *
a8bb6818
DV
10446intel_framebuffer_create(struct drm_device *dev,
10447 struct drm_mode_fb_cmd2 *mode_cmd,
10448 struct drm_i915_gem_object *obj)
10449{
10450 struct drm_framebuffer *fb;
10451 int ret;
10452
10453 ret = i915_mutex_lock_interruptible(dev);
10454 if (ret)
10455 return ERR_PTR(ret);
10456 fb = __intel_framebuffer_create(dev, mode_cmd, obj);
10457 mutex_unlock(&dev->struct_mutex);
10458
10459 return fb;
10460}
10461
d2dff872
CW
10462static u32
10463intel_framebuffer_pitch_for_width(int width, int bpp)
10464{
10465 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
10466 return ALIGN(pitch, 64);
10467}
10468
10469static u32
10470intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
10471{
10472 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
1267a26b 10473 return PAGE_ALIGN(pitch * mode->vdisplay);
d2dff872
CW
10474}
10475
10476static struct drm_framebuffer *
10477intel_framebuffer_create_for_mode(struct drm_device *dev,
10478 struct drm_display_mode *mode,
10479 int depth, int bpp)
10480{
dcb1394e 10481 struct drm_framebuffer *fb;
d2dff872 10482 struct drm_i915_gem_object *obj;
0fed39bd 10483 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
d2dff872 10484
d37cd8a8 10485 obj = i915_gem_object_create(dev,
d2dff872 10486 intel_framebuffer_size_for_mode(mode, bpp));
fe3db79b
CW
10487 if (IS_ERR(obj))
10488 return ERR_CAST(obj);
d2dff872
CW
10489
10490 mode_cmd.width = mode->hdisplay;
10491 mode_cmd.height = mode->vdisplay;
308e5bcb
JB
10492 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
10493 bpp);
5ca0c34a 10494 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
d2dff872 10495
dcb1394e
LW
10496 fb = intel_framebuffer_create(dev, &mode_cmd, obj);
10497 if (IS_ERR(fb))
10498 drm_gem_object_unreference_unlocked(&obj->base);
10499
10500 return fb;
d2dff872
CW
10501}
10502
10503static struct drm_framebuffer *
10504mode_fits_in_fbdev(struct drm_device *dev,
10505 struct drm_display_mode *mode)
10506{
0695726e 10507#ifdef CONFIG_DRM_FBDEV_EMULATION
d2dff872
CW
10508 struct drm_i915_private *dev_priv = dev->dev_private;
10509 struct drm_i915_gem_object *obj;
10510 struct drm_framebuffer *fb;
10511
4c0e5528 10512 if (!dev_priv->fbdev)
d2dff872
CW
10513 return NULL;
10514
4c0e5528 10515 if (!dev_priv->fbdev->fb)
d2dff872
CW
10516 return NULL;
10517
4c0e5528
DV
10518 obj = dev_priv->fbdev->fb->obj;
10519 BUG_ON(!obj);
10520
8bcd4553 10521 fb = &dev_priv->fbdev->fb->base;
01f2c773
VS
10522 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
10523 fb->bits_per_pixel))
d2dff872
CW
10524 return NULL;
10525
01f2c773 10526 if (obj->base.size < mode->vdisplay * fb->pitches[0])
d2dff872
CW
10527 return NULL;
10528
edde3617 10529 drm_framebuffer_reference(fb);
d2dff872 10530 return fb;
4520f53a
DV
10531#else
10532 return NULL;
10533#endif
d2dff872
CW
10534}
10535
d3a40d1b
ACO
10536static int intel_modeset_setup_plane_state(struct drm_atomic_state *state,
10537 struct drm_crtc *crtc,
10538 struct drm_display_mode *mode,
10539 struct drm_framebuffer *fb,
10540 int x, int y)
10541{
10542 struct drm_plane_state *plane_state;
10543 int hdisplay, vdisplay;
10544 int ret;
10545
10546 plane_state = drm_atomic_get_plane_state(state, crtc->primary);
10547 if (IS_ERR(plane_state))
10548 return PTR_ERR(plane_state);
10549
10550 if (mode)
10551 drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
10552 else
10553 hdisplay = vdisplay = 0;
10554
10555 ret = drm_atomic_set_crtc_for_plane(plane_state, fb ? crtc : NULL);
10556 if (ret)
10557 return ret;
10558 drm_atomic_set_fb_for_plane(plane_state, fb);
10559 plane_state->crtc_x = 0;
10560 plane_state->crtc_y = 0;
10561 plane_state->crtc_w = hdisplay;
10562 plane_state->crtc_h = vdisplay;
10563 plane_state->src_x = x << 16;
10564 plane_state->src_y = y << 16;
10565 plane_state->src_w = hdisplay << 16;
10566 plane_state->src_h = vdisplay << 16;
10567
10568 return 0;
10569}
10570
d2434ab7 10571bool intel_get_load_detect_pipe(struct drm_connector *connector,
7173188d 10572 struct drm_display_mode *mode,
51fd371b
RC
10573 struct intel_load_detect_pipe *old,
10574 struct drm_modeset_acquire_ctx *ctx)
79e53945
JB
10575{
10576 struct intel_crtc *intel_crtc;
d2434ab7
DV
10577 struct intel_encoder *intel_encoder =
10578 intel_attached_encoder(connector);
79e53945 10579 struct drm_crtc *possible_crtc;
4ef69c7a 10580 struct drm_encoder *encoder = &intel_encoder->base;
79e53945
JB
10581 struct drm_crtc *crtc = NULL;
10582 struct drm_device *dev = encoder->dev;
94352cf9 10583 struct drm_framebuffer *fb;
51fd371b 10584 struct drm_mode_config *config = &dev->mode_config;
edde3617 10585 struct drm_atomic_state *state = NULL, *restore_state = NULL;
944b0c76 10586 struct drm_connector_state *connector_state;
4be07317 10587 struct intel_crtc_state *crtc_state;
51fd371b 10588 int ret, i = -1;
79e53945 10589
d2dff872 10590 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
c23cc417 10591 connector->base.id, connector->name,
8e329a03 10592 encoder->base.id, encoder->name);
d2dff872 10593
edde3617
ML
10594 old->restore_state = NULL;
10595
51fd371b
RC
10596retry:
10597 ret = drm_modeset_lock(&config->connection_mutex, ctx);
10598 if (ret)
ad3c558f 10599 goto fail;
6e9f798d 10600
79e53945
JB
10601 /*
10602 * Algorithm gets a little messy:
7a5e4805 10603 *
79e53945
JB
10604 * - if the connector already has an assigned crtc, use it (but make
10605 * sure it's on first)
7a5e4805 10606 *
79e53945
JB
10607 * - try to find the first unused crtc that can drive this connector,
10608 * and use that if we find one
79e53945
JB
10609 */
10610
10611 /* See if we already have a CRTC for this connector */
edde3617
ML
10612 if (connector->state->crtc) {
10613 crtc = connector->state->crtc;
8261b191 10614
51fd371b 10615 ret = drm_modeset_lock(&crtc->mutex, ctx);
4d02e2de 10616 if (ret)
ad3c558f 10617 goto fail;
8261b191
CW
10618
10619 /* Make sure the crtc and connector are running */
edde3617 10620 goto found;
79e53945
JB
10621 }
10622
10623 /* Find an unused one (if possible) */
70e1e0ec 10624 for_each_crtc(dev, possible_crtc) {
79e53945
JB
10625 i++;
10626 if (!(encoder->possible_crtcs & (1 << i)))
10627 continue;
edde3617
ML
10628
10629 ret = drm_modeset_lock(&possible_crtc->mutex, ctx);
10630 if (ret)
10631 goto fail;
10632
10633 if (possible_crtc->state->enable) {
10634 drm_modeset_unlock(&possible_crtc->mutex);
a459249c 10635 continue;
edde3617 10636 }
a459249c
VS
10637
10638 crtc = possible_crtc;
10639 break;
79e53945
JB
10640 }
10641
10642 /*
10643 * If we didn't find an unused CRTC, don't use any.
10644 */
10645 if (!crtc) {
7173188d 10646 DRM_DEBUG_KMS("no pipe available for load-detect\n");
ad3c558f 10647 goto fail;
79e53945
JB
10648 }
10649
edde3617
ML
10650found:
10651 intel_crtc = to_intel_crtc(crtc);
10652
4d02e2de
DV
10653 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
10654 if (ret)
ad3c558f 10655 goto fail;
79e53945 10656
83a57153 10657 state = drm_atomic_state_alloc(dev);
edde3617
ML
10658 restore_state = drm_atomic_state_alloc(dev);
10659 if (!state || !restore_state) {
10660 ret = -ENOMEM;
10661 goto fail;
10662 }
83a57153
ACO
10663
10664 state->acquire_ctx = ctx;
edde3617 10665 restore_state->acquire_ctx = ctx;
83a57153 10666
944b0c76
ACO
10667 connector_state = drm_atomic_get_connector_state(state, connector);
10668 if (IS_ERR(connector_state)) {
10669 ret = PTR_ERR(connector_state);
10670 goto fail;
10671 }
10672
edde3617
ML
10673 ret = drm_atomic_set_crtc_for_connector(connector_state, crtc);
10674 if (ret)
10675 goto fail;
944b0c76 10676
4be07317
ACO
10677 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
10678 if (IS_ERR(crtc_state)) {
10679 ret = PTR_ERR(crtc_state);
10680 goto fail;
10681 }
10682
49d6fa21 10683 crtc_state->base.active = crtc_state->base.enable = true;
4be07317 10684
6492711d
CW
10685 if (!mode)
10686 mode = &load_detect_mode;
79e53945 10687
d2dff872
CW
10688 /* We need a framebuffer large enough to accommodate all accesses
10689 * that the plane may generate whilst we perform load detection.
10690 * We can not rely on the fbcon either being present (we get called
10691 * during its initialisation to detect all boot displays, or it may
10692 * not even exist) or that it is large enough to satisfy the
10693 * requested mode.
10694 */
94352cf9
DV
10695 fb = mode_fits_in_fbdev(dev, mode);
10696 if (fb == NULL) {
d2dff872 10697 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
94352cf9 10698 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
d2dff872
CW
10699 } else
10700 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
94352cf9 10701 if (IS_ERR(fb)) {
d2dff872 10702 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
412b61d8 10703 goto fail;
79e53945 10704 }
79e53945 10705
d3a40d1b
ACO
10706 ret = intel_modeset_setup_plane_state(state, crtc, mode, fb, 0, 0);
10707 if (ret)
10708 goto fail;
10709
edde3617
ML
10710 drm_framebuffer_unreference(fb);
10711
10712 ret = drm_atomic_set_mode_for_crtc(&crtc_state->base, mode);
10713 if (ret)
10714 goto fail;
10715
10716 ret = PTR_ERR_OR_ZERO(drm_atomic_get_connector_state(restore_state, connector));
10717 if (!ret)
10718 ret = PTR_ERR_OR_ZERO(drm_atomic_get_crtc_state(restore_state, crtc));
10719 if (!ret)
10720 ret = PTR_ERR_OR_ZERO(drm_atomic_get_plane_state(restore_state, crtc->primary));
10721 if (ret) {
10722 DRM_DEBUG_KMS("Failed to create a copy of old state to restore: %i\n", ret);
10723 goto fail;
10724 }
8c7b5ccb 10725
3ba86073
ML
10726 ret = drm_atomic_commit(state);
10727 if (ret) {
6492711d 10728 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
412b61d8 10729 goto fail;
79e53945 10730 }
edde3617
ML
10731
10732 old->restore_state = restore_state;
7173188d 10733
79e53945 10734 /* let the connector get through one full cycle before testing */
9d0498a2 10735 intel_wait_for_vblank(dev, intel_crtc->pipe);
7173188d 10736 return true;
412b61d8 10737
ad3c558f 10738fail:
e5d958ef 10739 drm_atomic_state_free(state);
edde3617
ML
10740 drm_atomic_state_free(restore_state);
10741 restore_state = state = NULL;
83a57153 10742
51fd371b
RC
10743 if (ret == -EDEADLK) {
10744 drm_modeset_backoff(ctx);
10745 goto retry;
10746 }
10747
412b61d8 10748 return false;
79e53945
JB
10749}
10750
d2434ab7 10751void intel_release_load_detect_pipe(struct drm_connector *connector,
49172fee
ACO
10752 struct intel_load_detect_pipe *old,
10753 struct drm_modeset_acquire_ctx *ctx)
79e53945 10754{
d2434ab7
DV
10755 struct intel_encoder *intel_encoder =
10756 intel_attached_encoder(connector);
4ef69c7a 10757 struct drm_encoder *encoder = &intel_encoder->base;
edde3617 10758 struct drm_atomic_state *state = old->restore_state;
d3a40d1b 10759 int ret;
79e53945 10760
d2dff872 10761 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
c23cc417 10762 connector->base.id, connector->name,
8e329a03 10763 encoder->base.id, encoder->name);
d2dff872 10764
edde3617 10765 if (!state)
0622a53c 10766 return;
79e53945 10767
edde3617
ML
10768 ret = drm_atomic_commit(state);
10769 if (ret) {
10770 DRM_DEBUG_KMS("Couldn't release load detect pipe: %i\n", ret);
10771 drm_atomic_state_free(state);
10772 }
79e53945
JB
10773}
10774
da4a1efa 10775static int i9xx_pll_refclk(struct drm_device *dev,
5cec258b 10776 const struct intel_crtc_state *pipe_config)
da4a1efa
VS
10777{
10778 struct drm_i915_private *dev_priv = dev->dev_private;
10779 u32 dpll = pipe_config->dpll_hw_state.dpll;
10780
10781 if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
e91e941b 10782 return dev_priv->vbt.lvds_ssc_freq;
da4a1efa
VS
10783 else if (HAS_PCH_SPLIT(dev))
10784 return 120000;
10785 else if (!IS_GEN2(dev))
10786 return 96000;
10787 else
10788 return 48000;
10789}
10790
79e53945 10791/* Returns the clock of the currently programmed mode of the given pipe. */
f1f644dc 10792static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 10793 struct intel_crtc_state *pipe_config)
79e53945 10794{
f1f644dc 10795 struct drm_device *dev = crtc->base.dev;
79e53945 10796 struct drm_i915_private *dev_priv = dev->dev_private;
f1f644dc 10797 int pipe = pipe_config->cpu_transcoder;
293623f7 10798 u32 dpll = pipe_config->dpll_hw_state.dpll;
79e53945 10799 u32 fp;
9e2c8475 10800 struct dpll clock;
dccbea3b 10801 int port_clock;
da4a1efa 10802 int refclk = i9xx_pll_refclk(dev, pipe_config);
79e53945
JB
10803
10804 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
293623f7 10805 fp = pipe_config->dpll_hw_state.fp0;
79e53945 10806 else
293623f7 10807 fp = pipe_config->dpll_hw_state.fp1;
79e53945
JB
10808
10809 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
f2b115e6
AJ
10810 if (IS_PINEVIEW(dev)) {
10811 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
10812 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
2177832f
SL
10813 } else {
10814 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
10815 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
10816 }
10817
a6c45cf0 10818 if (!IS_GEN2(dev)) {
f2b115e6
AJ
10819 if (IS_PINEVIEW(dev))
10820 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
10821 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
2177832f
SL
10822 else
10823 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
79e53945
JB
10824 DPLL_FPA01_P1_POST_DIV_SHIFT);
10825
10826 switch (dpll & DPLL_MODE_MASK) {
10827 case DPLLB_MODE_DAC_SERIAL:
10828 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
10829 5 : 10;
10830 break;
10831 case DPLLB_MODE_LVDS:
10832 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
10833 7 : 14;
10834 break;
10835 default:
28c97730 10836 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
79e53945 10837 "mode\n", (int)(dpll & DPLL_MODE_MASK));
f1f644dc 10838 return;
79e53945
JB
10839 }
10840
ac58c3f0 10841 if (IS_PINEVIEW(dev))
dccbea3b 10842 port_clock = pnv_calc_dpll_params(refclk, &clock);
ac58c3f0 10843 else
dccbea3b 10844 port_clock = i9xx_calc_dpll_params(refclk, &clock);
79e53945 10845 } else {
0fb58223 10846 u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
b1c560d1 10847 bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
79e53945
JB
10848
10849 if (is_lvds) {
10850 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
10851 DPLL_FPA01_P1_POST_DIV_SHIFT);
b1c560d1
VS
10852
10853 if (lvds & LVDS_CLKB_POWER_UP)
10854 clock.p2 = 7;
10855 else
10856 clock.p2 = 14;
79e53945
JB
10857 } else {
10858 if (dpll & PLL_P1_DIVIDE_BY_TWO)
10859 clock.p1 = 2;
10860 else {
10861 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
10862 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
10863 }
10864 if (dpll & PLL_P2_DIVIDE_BY_4)
10865 clock.p2 = 4;
10866 else
10867 clock.p2 = 2;
79e53945 10868 }
da4a1efa 10869
dccbea3b 10870 port_clock = i9xx_calc_dpll_params(refclk, &clock);
79e53945
JB
10871 }
10872
18442d08
VS
10873 /*
10874 * This value includes pixel_multiplier. We will use
241bfc38 10875 * port_clock to compute adjusted_mode.crtc_clock in the
18442d08
VS
10876 * encoder's get_config() function.
10877 */
dccbea3b 10878 pipe_config->port_clock = port_clock;
f1f644dc
JB
10879}
10880
6878da05
VS
10881int intel_dotclock_calculate(int link_freq,
10882 const struct intel_link_m_n *m_n)
f1f644dc 10883{
f1f644dc
JB
10884 /*
10885 * The calculation for the data clock is:
1041a02f 10886 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
f1f644dc 10887 * But we want to avoid losing precison if possible, so:
1041a02f 10888 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
f1f644dc
JB
10889 *
10890 * and the link clock is simpler:
1041a02f 10891 * link_clock = (m * link_clock) / n
f1f644dc
JB
10892 */
10893
6878da05
VS
10894 if (!m_n->link_n)
10895 return 0;
f1f644dc 10896
6878da05
VS
10897 return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
10898}
f1f644dc 10899
18442d08 10900static void ironlake_pch_clock_get(struct intel_crtc *crtc,
5cec258b 10901 struct intel_crtc_state *pipe_config)
6878da05 10902{
e3b247da 10903 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
79e53945 10904
18442d08
VS
10905 /* read out port_clock from the DPLL */
10906 i9xx_crtc_clock_get(crtc, pipe_config);
f1f644dc 10907
f1f644dc 10908 /*
e3b247da
VS
10909 * In case there is an active pipe without active ports,
10910 * we may need some idea for the dotclock anyway.
10911 * Calculate one based on the FDI configuration.
79e53945 10912 */
2d112de7 10913 pipe_config->base.adjusted_mode.crtc_clock =
21a727b3 10914 intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
18442d08 10915 &pipe_config->fdi_m_n);
79e53945
JB
10916}
10917
10918/** Returns the currently programmed mode of the given pipe. */
10919struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
10920 struct drm_crtc *crtc)
10921{
548f245b 10922 struct drm_i915_private *dev_priv = dev->dev_private;
79e53945 10923 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6e3c9717 10924 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
79e53945 10925 struct drm_display_mode *mode;
3f36b937 10926 struct intel_crtc_state *pipe_config;
fe2b8f9d
PZ
10927 int htot = I915_READ(HTOTAL(cpu_transcoder));
10928 int hsync = I915_READ(HSYNC(cpu_transcoder));
10929 int vtot = I915_READ(VTOTAL(cpu_transcoder));
10930 int vsync = I915_READ(VSYNC(cpu_transcoder));
293623f7 10931 enum pipe pipe = intel_crtc->pipe;
79e53945
JB
10932
10933 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
10934 if (!mode)
10935 return NULL;
10936
3f36b937
TU
10937 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
10938 if (!pipe_config) {
10939 kfree(mode);
10940 return NULL;
10941 }
10942
f1f644dc
JB
10943 /*
10944 * Construct a pipe_config sufficient for getting the clock info
10945 * back out of crtc_clock_get.
10946 *
10947 * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
10948 * to use a real value here instead.
10949 */
3f36b937
TU
10950 pipe_config->cpu_transcoder = (enum transcoder) pipe;
10951 pipe_config->pixel_multiplier = 1;
10952 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(pipe));
10953 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(pipe));
10954 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(pipe));
10955 i9xx_crtc_clock_get(intel_crtc, pipe_config);
10956
10957 mode->clock = pipe_config->port_clock / pipe_config->pixel_multiplier;
79e53945
JB
10958 mode->hdisplay = (htot & 0xffff) + 1;
10959 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
10960 mode->hsync_start = (hsync & 0xffff) + 1;
10961 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
10962 mode->vdisplay = (vtot & 0xffff) + 1;
10963 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
10964 mode->vsync_start = (vsync & 0xffff) + 1;
10965 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
10966
10967 drm_mode_set_name(mode);
79e53945 10968
3f36b937
TU
10969 kfree(pipe_config);
10970
79e53945
JB
10971 return mode;
10972}
10973
7d993739 10974void intel_mark_busy(struct drm_i915_private *dev_priv)
f047e395 10975{
f62a0076
CW
10976 if (dev_priv->mm.busy)
10977 return;
10978
43694d69 10979 intel_runtime_pm_get(dev_priv);
c67a470b 10980 i915_update_gfx_val(dev_priv);
7d993739 10981 if (INTEL_GEN(dev_priv) >= 6)
43cf3bf0 10982 gen6_rps_busy(dev_priv);
f62a0076 10983 dev_priv->mm.busy = true;
f047e395
CW
10984}
10985
7d993739 10986void intel_mark_idle(struct drm_i915_private *dev_priv)
652c393a 10987{
f62a0076
CW
10988 if (!dev_priv->mm.busy)
10989 return;
10990
10991 dev_priv->mm.busy = false;
10992
7d993739
TU
10993 if (INTEL_GEN(dev_priv) >= 6)
10994 gen6_rps_idle(dev_priv);
bb4cdd53 10995
43694d69 10996 intel_runtime_pm_put(dev_priv);
652c393a
JB
10997}
10998
79e53945
JB
10999static void intel_crtc_destroy(struct drm_crtc *crtc)
11000{
11001 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
67e77c5a 11002 struct drm_device *dev = crtc->dev;
51cbaf01 11003 struct intel_flip_work *work;
67e77c5a 11004
5e2d7afc 11005 spin_lock_irq(&dev->event_lock);
5a21b665
DV
11006 work = intel_crtc->flip_work;
11007 intel_crtc->flip_work = NULL;
11008 spin_unlock_irq(&dev->event_lock);
67e77c5a 11009
5a21b665 11010 if (work) {
51cbaf01
ML
11011 cancel_work_sync(&work->mmio_work);
11012 cancel_work_sync(&work->unpin_work);
5a21b665 11013 kfree(work);
67e77c5a 11014 }
79e53945
JB
11015
11016 drm_crtc_cleanup(crtc);
67e77c5a 11017
79e53945
JB
11018 kfree(intel_crtc);
11019}
11020
6b95a207
KH
11021static void intel_unpin_work_fn(struct work_struct *__work)
11022{
51cbaf01
ML
11023 struct intel_flip_work *work =
11024 container_of(__work, struct intel_flip_work, unpin_work);
5a21b665
DV
11025 struct intel_crtc *crtc = to_intel_crtc(work->crtc);
11026 struct drm_device *dev = crtc->base.dev;
11027 struct drm_plane *primary = crtc->base.primary;
03f476e1 11028
5a21b665
DV
11029 if (is_mmio_work(work))
11030 flush_work(&work->mmio_work);
03f476e1 11031
5a21b665
DV
11032 mutex_lock(&dev->struct_mutex);
11033 intel_unpin_fb_obj(work->old_fb, primary->state->rotation);
11034 drm_gem_object_unreference(&work->pending_flip_obj->base);
143f73b3 11035
5a21b665
DV
11036 if (work->flip_queued_req)
11037 i915_gem_request_assign(&work->flip_queued_req, NULL);
11038 mutex_unlock(&dev->struct_mutex);
143f73b3 11039
5a21b665
DV
11040 intel_frontbuffer_flip_complete(dev, to_intel_plane(primary)->frontbuffer_bit);
11041 intel_fbc_post_update(crtc);
11042 drm_framebuffer_unreference(work->old_fb);
143f73b3 11043
5a21b665
DV
11044 BUG_ON(atomic_read(&crtc->unpin_work_count) == 0);
11045 atomic_dec(&crtc->unpin_work_count);
a6747b73 11046
5a21b665
DV
11047 kfree(work);
11048}
d9e86c0e 11049
5a21b665
DV
11050/* Is 'a' after or equal to 'b'? */
11051static bool g4x_flip_count_after_eq(u32 a, u32 b)
11052{
11053 return !((a - b) & 0x80000000);
11054}
143f73b3 11055
5a21b665
DV
11056static bool __pageflip_finished_cs(struct intel_crtc *crtc,
11057 struct intel_flip_work *work)
11058{
11059 struct drm_device *dev = crtc->base.dev;
11060 struct drm_i915_private *dev_priv = dev->dev_private;
11061 unsigned reset_counter;
143f73b3 11062
5a21b665
DV
11063 reset_counter = i915_reset_counter(&dev_priv->gpu_error);
11064 if (crtc->reset_counter != reset_counter)
11065 return true;
143f73b3 11066
5a21b665
DV
11067 /*
11068 * The relevant registers doen't exist on pre-ctg.
11069 * As the flip done interrupt doesn't trigger for mmio
11070 * flips on gmch platforms, a flip count check isn't
11071 * really needed there. But since ctg has the registers,
11072 * include it in the check anyway.
11073 */
11074 if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
11075 return true;
b4a98e57 11076
5a21b665
DV
11077 /*
11078 * BDW signals flip done immediately if the plane
11079 * is disabled, even if the plane enable is already
11080 * armed to occur at the next vblank :(
11081 */
f99d7069 11082
5a21b665
DV
11083 /*
11084 * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
11085 * used the same base address. In that case the mmio flip might
11086 * have completed, but the CS hasn't even executed the flip yet.
11087 *
11088 * A flip count check isn't enough as the CS might have updated
11089 * the base address just after start of vblank, but before we
11090 * managed to process the interrupt. This means we'd complete the
11091 * CS flip too soon.
11092 *
11093 * Combining both checks should get us a good enough result. It may
11094 * still happen that the CS flip has been executed, but has not
11095 * yet actually completed. But in case the base address is the same
11096 * anyway, we don't really care.
11097 */
11098 return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
11099 crtc->flip_work->gtt_offset &&
11100 g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_G4X(crtc->pipe)),
11101 crtc->flip_work->flip_count);
11102}
b4a98e57 11103
5a21b665
DV
11104static bool
11105__pageflip_finished_mmio(struct intel_crtc *crtc,
11106 struct intel_flip_work *work)
11107{
11108 /*
11109 * MMIO work completes when vblank is different from
11110 * flip_queued_vblank.
11111 *
11112 * Reset counter value doesn't matter, this is handled by
11113 * i915_wait_request finishing early, so no need to handle
11114 * reset here.
11115 */
11116 return intel_crtc_get_vblank_counter(crtc) != work->flip_queued_vblank;
6b95a207
KH
11117}
11118
51cbaf01
ML
11119
11120static bool pageflip_finished(struct intel_crtc *crtc,
11121 struct intel_flip_work *work)
11122{
11123 if (!atomic_read(&work->pending))
11124 return false;
11125
11126 smp_rmb();
11127
5a21b665
DV
11128 if (is_mmio_work(work))
11129 return __pageflip_finished_mmio(crtc, work);
11130 else
11131 return __pageflip_finished_cs(crtc, work);
11132}
11133
11134void intel_finish_page_flip_cs(struct drm_i915_private *dev_priv, int pipe)
11135{
11136 struct drm_device *dev = dev_priv->dev;
11137 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
11138 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11139 struct intel_flip_work *work;
11140 unsigned long flags;
11141
11142 /* Ignore early vblank irqs */
11143 if (!crtc)
11144 return;
11145
51cbaf01 11146 /*
5a21b665
DV
11147 * This is called both by irq handlers and the reset code (to complete
11148 * lost pageflips) so needs the full irqsave spinlocks.
51cbaf01 11149 */
5a21b665
DV
11150 spin_lock_irqsave(&dev->event_lock, flags);
11151 work = intel_crtc->flip_work;
11152
11153 if (work != NULL &&
11154 !is_mmio_work(work) &&
11155 pageflip_finished(intel_crtc, work))
11156 page_flip_completed(intel_crtc);
11157
11158 spin_unlock_irqrestore(&dev->event_lock, flags);
75f7f3ec
VS
11159}
11160
51cbaf01 11161void intel_finish_page_flip_mmio(struct drm_i915_private *dev_priv, int pipe)
6b95a207 11162{
91d14251 11163 struct drm_device *dev = dev_priv->dev;
5251f04e
ML
11164 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
11165 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
51cbaf01 11166 struct intel_flip_work *work;
6b95a207
KH
11167 unsigned long flags;
11168
5251f04e
ML
11169 /* Ignore early vblank irqs */
11170 if (!crtc)
11171 return;
f326038a
DV
11172
11173 /*
11174 * This is called both by irq handlers and the reset code (to complete
11175 * lost pageflips) so needs the full irqsave spinlocks.
e7d841ca 11176 */
6b95a207 11177 spin_lock_irqsave(&dev->event_lock, flags);
5a21b665 11178 work = intel_crtc->flip_work;
5251f04e 11179
5a21b665
DV
11180 if (work != NULL &&
11181 is_mmio_work(work) &&
11182 pageflip_finished(intel_crtc, work))
11183 page_flip_completed(intel_crtc);
5251f04e 11184
6b95a207
KH
11185 spin_unlock_irqrestore(&dev->event_lock, flags);
11186}
11187
5a21b665
DV
11188static inline void intel_mark_page_flip_active(struct intel_crtc *crtc,
11189 struct intel_flip_work *work)
84c33a64 11190{
5a21b665 11191 work->flip_queued_vblank = intel_crtc_get_vblank_counter(crtc);
84c33a64 11192
5a21b665
DV
11193 /* Ensure that the work item is consistent when activating it ... */
11194 smp_mb__before_atomic();
11195 atomic_set(&work->pending, 1);
11196}
a6747b73 11197
5a21b665
DV
11198static int intel_gen2_queue_flip(struct drm_device *dev,
11199 struct drm_crtc *crtc,
11200 struct drm_framebuffer *fb,
11201 struct drm_i915_gem_object *obj,
11202 struct drm_i915_gem_request *req,
11203 uint32_t flags)
11204{
11205 struct intel_engine_cs *engine = req->engine;
11206 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11207 u32 flip_mask;
11208 int ret;
143f73b3 11209
5a21b665
DV
11210 ret = intel_ring_begin(req, 6);
11211 if (ret)
11212 return ret;
143f73b3 11213
5a21b665
DV
11214 /* Can't queue multiple flips, so wait for the previous
11215 * one to finish before executing the next.
11216 */
11217 if (intel_crtc->plane)
11218 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
11219 else
11220 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
11221 intel_ring_emit(engine, MI_WAIT_FOR_EVENT | flip_mask);
11222 intel_ring_emit(engine, MI_NOOP);
11223 intel_ring_emit(engine, MI_DISPLAY_FLIP |
11224 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
11225 intel_ring_emit(engine, fb->pitches[0]);
11226 intel_ring_emit(engine, intel_crtc->flip_work->gtt_offset);
11227 intel_ring_emit(engine, 0); /* aux display base address, unused */
143f73b3 11228
5a21b665
DV
11229 return 0;
11230}
84c33a64 11231
5a21b665
DV
11232static int intel_gen3_queue_flip(struct drm_device *dev,
11233 struct drm_crtc *crtc,
11234 struct drm_framebuffer *fb,
11235 struct drm_i915_gem_object *obj,
11236 struct drm_i915_gem_request *req,
11237 uint32_t flags)
11238{
11239 struct intel_engine_cs *engine = req->engine;
11240 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11241 u32 flip_mask;
11242 int ret;
d55dbd06 11243
5a21b665
DV
11244 ret = intel_ring_begin(req, 6);
11245 if (ret)
11246 return ret;
d55dbd06 11247
5a21b665
DV
11248 if (intel_crtc->plane)
11249 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
11250 else
11251 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
11252 intel_ring_emit(engine, MI_WAIT_FOR_EVENT | flip_mask);
11253 intel_ring_emit(engine, MI_NOOP);
11254 intel_ring_emit(engine, MI_DISPLAY_FLIP_I915 |
11255 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
11256 intel_ring_emit(engine, fb->pitches[0]);
11257 intel_ring_emit(engine, intel_crtc->flip_work->gtt_offset);
11258 intel_ring_emit(engine, MI_NOOP);
fd8e058a 11259
5a21b665
DV
11260 return 0;
11261}
84c33a64 11262
5a21b665
DV
11263static int intel_gen4_queue_flip(struct drm_device *dev,
11264 struct drm_crtc *crtc,
11265 struct drm_framebuffer *fb,
11266 struct drm_i915_gem_object *obj,
11267 struct drm_i915_gem_request *req,
11268 uint32_t flags)
11269{
11270 struct intel_engine_cs *engine = req->engine;
11271 struct drm_i915_private *dev_priv = dev->dev_private;
11272 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11273 uint32_t pf, pipesrc;
11274 int ret;
143f73b3 11275
5a21b665
DV
11276 ret = intel_ring_begin(req, 4);
11277 if (ret)
11278 return ret;
143f73b3 11279
5a21b665
DV
11280 /* i965+ uses the linear or tiled offsets from the
11281 * Display Registers (which do not change across a page-flip)
11282 * so we need only reprogram the base address.
11283 */
11284 intel_ring_emit(engine, MI_DISPLAY_FLIP |
11285 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
11286 intel_ring_emit(engine, fb->pitches[0]);
11287 intel_ring_emit(engine, intel_crtc->flip_work->gtt_offset |
11288 obj->tiling_mode);
11289
11290 /* XXX Enabling the panel-fitter across page-flip is so far
11291 * untested on non-native modes, so ignore it for now.
11292 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
11293 */
11294 pf = 0;
11295 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
11296 intel_ring_emit(engine, pf | pipesrc);
143f73b3 11297
5a21b665 11298 return 0;
8c9f3aaf
JB
11299}
11300
5a21b665
DV
11301static int intel_gen6_queue_flip(struct drm_device *dev,
11302 struct drm_crtc *crtc,
11303 struct drm_framebuffer *fb,
11304 struct drm_i915_gem_object *obj,
11305 struct drm_i915_gem_request *req,
11306 uint32_t flags)
da20eabd 11307{
5a21b665
DV
11308 struct intel_engine_cs *engine = req->engine;
11309 struct drm_i915_private *dev_priv = dev->dev_private;
11310 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11311 uint32_t pf, pipesrc;
11312 int ret;
d21fbe87 11313
5a21b665
DV
11314 ret = intel_ring_begin(req, 4);
11315 if (ret)
11316 return ret;
92826fcd 11317
5a21b665
DV
11318 intel_ring_emit(engine, MI_DISPLAY_FLIP |
11319 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
11320 intel_ring_emit(engine, fb->pitches[0] | obj->tiling_mode);
11321 intel_ring_emit(engine, intel_crtc->flip_work->gtt_offset);
92826fcd 11322
5a21b665
DV
11323 /* Contrary to the suggestions in the documentation,
11324 * "Enable Panel Fitter" does not seem to be required when page
11325 * flipping with a non-native mode, and worse causes a normal
11326 * modeset to fail.
11327 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
11328 */
11329 pf = 0;
11330 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
11331 intel_ring_emit(engine, pf | pipesrc);
7809e5ae 11332
5a21b665 11333 return 0;
7809e5ae
MR
11334}
11335
5a21b665
DV
11336static int intel_gen7_queue_flip(struct drm_device *dev,
11337 struct drm_crtc *crtc,
11338 struct drm_framebuffer *fb,
11339 struct drm_i915_gem_object *obj,
11340 struct drm_i915_gem_request *req,
11341 uint32_t flags)
d21fbe87 11342{
5a21b665
DV
11343 struct intel_engine_cs *engine = req->engine;
11344 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11345 uint32_t plane_bit = 0;
11346 int len, ret;
d21fbe87 11347
5a21b665
DV
11348 switch (intel_crtc->plane) {
11349 case PLANE_A:
11350 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
11351 break;
11352 case PLANE_B:
11353 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
11354 break;
11355 case PLANE_C:
11356 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
11357 break;
11358 default:
11359 WARN_ONCE(1, "unknown plane in flip command\n");
11360 return -ENODEV;
11361 }
11362
11363 len = 4;
11364 if (engine->id == RCS) {
11365 len += 6;
11366 /*
11367 * On Gen 8, SRM is now taking an extra dword to accommodate
11368 * 48bits addresses, and we need a NOOP for the batch size to
11369 * stay even.
11370 */
11371 if (IS_GEN8(dev))
11372 len += 2;
11373 }
11374
11375 /*
11376 * BSpec MI_DISPLAY_FLIP for IVB:
11377 * "The full packet must be contained within the same cache line."
11378 *
11379 * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
11380 * cacheline, if we ever start emitting more commands before
11381 * the MI_DISPLAY_FLIP we may need to first emit everything else,
11382 * then do the cacheline alignment, and finally emit the
11383 * MI_DISPLAY_FLIP.
11384 */
11385 ret = intel_ring_cacheline_align(req);
11386 if (ret)
11387 return ret;
11388
11389 ret = intel_ring_begin(req, len);
11390 if (ret)
11391 return ret;
11392
11393 /* Unmask the flip-done completion message. Note that the bspec says that
11394 * we should do this for both the BCS and RCS, and that we must not unmask
11395 * more than one flip event at any time (or ensure that one flip message
11396 * can be sent by waiting for flip-done prior to queueing new flips).
11397 * Experimentation says that BCS works despite DERRMR masking all
11398 * flip-done completion events and that unmasking all planes at once
11399 * for the RCS also doesn't appear to drop events. Setting the DERRMR
11400 * to zero does lead to lockups within MI_DISPLAY_FLIP.
11401 */
11402 if (engine->id == RCS) {
11403 intel_ring_emit(engine, MI_LOAD_REGISTER_IMM(1));
11404 intel_ring_emit_reg(engine, DERRMR);
11405 intel_ring_emit(engine, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
11406 DERRMR_PIPEB_PRI_FLIP_DONE |
11407 DERRMR_PIPEC_PRI_FLIP_DONE));
11408 if (IS_GEN8(dev))
11409 intel_ring_emit(engine, MI_STORE_REGISTER_MEM_GEN8 |
11410 MI_SRM_LRM_GLOBAL_GTT);
11411 else
11412 intel_ring_emit(engine, MI_STORE_REGISTER_MEM |
11413 MI_SRM_LRM_GLOBAL_GTT);
11414 intel_ring_emit_reg(engine, DERRMR);
11415 intel_ring_emit(engine, engine->scratch.gtt_offset + 256);
11416 if (IS_GEN8(dev)) {
11417 intel_ring_emit(engine, 0);
11418 intel_ring_emit(engine, MI_NOOP);
11419 }
11420 }
11421
11422 intel_ring_emit(engine, MI_DISPLAY_FLIP_I915 | plane_bit);
11423 intel_ring_emit(engine, (fb->pitches[0] | obj->tiling_mode));
11424 intel_ring_emit(engine, intel_crtc->flip_work->gtt_offset);
11425 intel_ring_emit(engine, (MI_NOOP));
11426
11427 return 0;
11428}
11429
11430static bool use_mmio_flip(struct intel_engine_cs *engine,
11431 struct drm_i915_gem_object *obj)
11432{
c37efb99
CW
11433 struct reservation_object *resv;
11434
5a21b665
DV
11435 /*
11436 * This is not being used for older platforms, because
11437 * non-availability of flip done interrupt forces us to use
11438 * CS flips. Older platforms derive flip done using some clever
11439 * tricks involving the flip_pending status bits and vblank irqs.
11440 * So using MMIO flips there would disrupt this mechanism.
11441 */
11442
11443 if (engine == NULL)
11444 return true;
11445
11446 if (INTEL_GEN(engine->i915) < 5)
11447 return false;
11448
11449 if (i915.use_mmio_flip < 0)
11450 return false;
11451 else if (i915.use_mmio_flip > 0)
11452 return true;
11453 else if (i915.enable_execlists)
11454 return true;
c37efb99
CW
11455
11456 resv = i915_gem_object_get_dmabuf_resv(obj);
11457 if (resv && !reservation_object_test_signaled_rcu(resv, false))
5a21b665 11458 return true;
c37efb99
CW
11459
11460 return engine != i915_gem_request_get_engine(obj->last_write_req);
5a21b665
DV
11461}
11462
11463static void skl_do_mmio_flip(struct intel_crtc *intel_crtc,
11464 unsigned int rotation,
11465 struct intel_flip_work *work)
11466{
11467 struct drm_device *dev = intel_crtc->base.dev;
11468 struct drm_i915_private *dev_priv = dev->dev_private;
11469 struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
11470 const enum pipe pipe = intel_crtc->pipe;
11471 u32 ctl, stride, tile_height;
11472
11473 ctl = I915_READ(PLANE_CTL(pipe, 0));
11474 ctl &= ~PLANE_CTL_TILED_MASK;
11475 switch (fb->modifier[0]) {
11476 case DRM_FORMAT_MOD_NONE:
11477 break;
11478 case I915_FORMAT_MOD_X_TILED:
11479 ctl |= PLANE_CTL_TILED_X;
11480 break;
11481 case I915_FORMAT_MOD_Y_TILED:
11482 ctl |= PLANE_CTL_TILED_Y;
11483 break;
11484 case I915_FORMAT_MOD_Yf_TILED:
11485 ctl |= PLANE_CTL_TILED_YF;
11486 break;
11487 default:
11488 MISSING_CASE(fb->modifier[0]);
11489 }
11490
11491 /*
11492 * The stride is either expressed as a multiple of 64 bytes chunks for
11493 * linear buffers or in number of tiles for tiled buffers.
11494 */
11495 if (intel_rotation_90_or_270(rotation)) {
11496 /* stride = Surface height in tiles */
11497 tile_height = intel_tile_height(dev_priv, fb->modifier[0], 0);
11498 stride = DIV_ROUND_UP(fb->height, tile_height);
11499 } else {
11500 stride = fb->pitches[0] /
11501 intel_fb_stride_alignment(dev_priv, fb->modifier[0],
11502 fb->pixel_format);
11503 }
11504
11505 /*
11506 * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
11507 * PLANE_SURF updates, the update is then guaranteed to be atomic.
11508 */
11509 I915_WRITE(PLANE_CTL(pipe, 0), ctl);
11510 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
11511
11512 I915_WRITE(PLANE_SURF(pipe, 0), work->gtt_offset);
11513 POSTING_READ(PLANE_SURF(pipe, 0));
11514}
11515
11516static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc,
11517 struct intel_flip_work *work)
11518{
11519 struct drm_device *dev = intel_crtc->base.dev;
11520 struct drm_i915_private *dev_priv = dev->dev_private;
11521 struct intel_framebuffer *intel_fb =
11522 to_intel_framebuffer(intel_crtc->base.primary->fb);
11523 struct drm_i915_gem_object *obj = intel_fb->obj;
11524 i915_reg_t reg = DSPCNTR(intel_crtc->plane);
11525 u32 dspcntr;
11526
11527 dspcntr = I915_READ(reg);
11528
11529 if (obj->tiling_mode != I915_TILING_NONE)
11530 dspcntr |= DISPPLANE_TILED;
11531 else
11532 dspcntr &= ~DISPPLANE_TILED;
11533
11534 I915_WRITE(reg, dspcntr);
11535
11536 I915_WRITE(DSPSURF(intel_crtc->plane), work->gtt_offset);
11537 POSTING_READ(DSPSURF(intel_crtc->plane));
11538}
11539
11540static void intel_mmio_flip_work_func(struct work_struct *w)
11541{
11542 struct intel_flip_work *work =
11543 container_of(w, struct intel_flip_work, mmio_work);
11544 struct intel_crtc *crtc = to_intel_crtc(work->crtc);
11545 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
11546 struct intel_framebuffer *intel_fb =
11547 to_intel_framebuffer(crtc->base.primary->fb);
11548 struct drm_i915_gem_object *obj = intel_fb->obj;
c37efb99 11549 struct reservation_object *resv;
5a21b665
DV
11550
11551 if (work->flip_queued_req)
11552 WARN_ON(__i915_wait_request(work->flip_queued_req,
11553 false, NULL,
11554 &dev_priv->rps.mmioflips));
11555
11556 /* For framebuffer backed by dmabuf, wait for fence */
c37efb99
CW
11557 resv = i915_gem_object_get_dmabuf_resv(obj);
11558 if (resv)
11559 WARN_ON(reservation_object_wait_timeout_rcu(resv, false, false,
5a21b665
DV
11560 MAX_SCHEDULE_TIMEOUT) < 0);
11561
11562 intel_pipe_update_start(crtc);
11563
11564 if (INTEL_GEN(dev_priv) >= 9)
11565 skl_do_mmio_flip(crtc, work->rotation, work);
11566 else
11567 /* use_mmio_flip() retricts MMIO flips to ilk+ */
11568 ilk_do_mmio_flip(crtc, work);
11569
11570 intel_pipe_update_end(crtc, work);
11571}
11572
11573static int intel_default_queue_flip(struct drm_device *dev,
11574 struct drm_crtc *crtc,
11575 struct drm_framebuffer *fb,
11576 struct drm_i915_gem_object *obj,
11577 struct drm_i915_gem_request *req,
11578 uint32_t flags)
11579{
11580 return -ENODEV;
11581}
11582
11583static bool __pageflip_stall_check_cs(struct drm_i915_private *dev_priv,
11584 struct intel_crtc *intel_crtc,
11585 struct intel_flip_work *work)
11586{
11587 u32 addr, vblank;
11588
11589 if (!atomic_read(&work->pending))
11590 return false;
11591
11592 smp_rmb();
11593
11594 vblank = intel_crtc_get_vblank_counter(intel_crtc);
11595 if (work->flip_ready_vblank == 0) {
11596 if (work->flip_queued_req &&
11597 !i915_gem_request_completed(work->flip_queued_req, true))
11598 return false;
11599
11600 work->flip_ready_vblank = vblank;
11601 }
11602
11603 if (vblank - work->flip_ready_vblank < 3)
11604 return false;
11605
11606 /* Potential stall - if we see that the flip has happened,
11607 * assume a missed interrupt. */
11608 if (INTEL_GEN(dev_priv) >= 4)
11609 addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
11610 else
11611 addr = I915_READ(DSPADDR(intel_crtc->plane));
11612
11613 /* There is a potential issue here with a false positive after a flip
11614 * to the same address. We could address this by checking for a
11615 * non-incrementing frame counter.
11616 */
11617 return addr == work->gtt_offset;
11618}
11619
11620void intel_check_page_flip(struct drm_i915_private *dev_priv, int pipe)
11621{
11622 struct drm_device *dev = dev_priv->dev;
11623 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
11624 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11625 struct intel_flip_work *work;
11626
11627 WARN_ON(!in_interrupt());
11628
11629 if (crtc == NULL)
11630 return;
11631
11632 spin_lock(&dev->event_lock);
11633 work = intel_crtc->flip_work;
11634
11635 if (work != NULL && !is_mmio_work(work) &&
11636 __pageflip_stall_check_cs(dev_priv, intel_crtc, work)) {
11637 WARN_ONCE(1,
11638 "Kicking stuck page flip: queued at %d, now %d\n",
11639 work->flip_queued_vblank, intel_crtc_get_vblank_counter(intel_crtc));
11640 page_flip_completed(intel_crtc);
11641 work = NULL;
11642 }
11643
11644 if (work != NULL && !is_mmio_work(work) &&
11645 intel_crtc_get_vblank_counter(intel_crtc) - work->flip_queued_vblank > 1)
11646 intel_queue_rps_boost_for_request(work->flip_queued_req);
11647 spin_unlock(&dev->event_lock);
11648}
11649
11650static int intel_crtc_page_flip(struct drm_crtc *crtc,
11651 struct drm_framebuffer *fb,
11652 struct drm_pending_vblank_event *event,
11653 uint32_t page_flip_flags)
11654{
11655 struct drm_device *dev = crtc->dev;
11656 struct drm_i915_private *dev_priv = dev->dev_private;
11657 struct drm_framebuffer *old_fb = crtc->primary->fb;
11658 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
11659 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11660 struct drm_plane *primary = crtc->primary;
11661 enum pipe pipe = intel_crtc->pipe;
11662 struct intel_flip_work *work;
11663 struct intel_engine_cs *engine;
11664 bool mmio_flip;
11665 struct drm_i915_gem_request *request = NULL;
11666 int ret;
11667
11668 /*
11669 * drm_mode_page_flip_ioctl() should already catch this, but double
11670 * check to be safe. In the future we may enable pageflipping from
11671 * a disabled primary plane.
11672 */
11673 if (WARN_ON(intel_fb_obj(old_fb) == NULL))
11674 return -EBUSY;
11675
11676 /* Can't change pixel format via MI display flips. */
11677 if (fb->pixel_format != crtc->primary->fb->pixel_format)
11678 return -EINVAL;
11679
11680 /*
11681 * TILEOFF/LINOFF registers can't be changed via MI display flips.
11682 * Note that pitch changes could also affect these register.
11683 */
11684 if (INTEL_INFO(dev)->gen > 3 &&
11685 (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
11686 fb->pitches[0] != crtc->primary->fb->pitches[0]))
11687 return -EINVAL;
11688
11689 if (i915_terminally_wedged(&dev_priv->gpu_error))
11690 goto out_hang;
11691
11692 work = kzalloc(sizeof(*work), GFP_KERNEL);
11693 if (work == NULL)
11694 return -ENOMEM;
11695
11696 work->event = event;
11697 work->crtc = crtc;
11698 work->old_fb = old_fb;
11699 INIT_WORK(&work->unpin_work, intel_unpin_work_fn);
11700
11701 ret = drm_crtc_vblank_get(crtc);
11702 if (ret)
11703 goto free_work;
11704
11705 /* We borrow the event spin lock for protecting flip_work */
11706 spin_lock_irq(&dev->event_lock);
11707 if (intel_crtc->flip_work) {
11708 /* Before declaring the flip queue wedged, check if
11709 * the hardware completed the operation behind our backs.
11710 */
11711 if (pageflip_finished(intel_crtc, intel_crtc->flip_work)) {
11712 DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
11713 page_flip_completed(intel_crtc);
11714 } else {
11715 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
11716 spin_unlock_irq(&dev->event_lock);
11717
11718 drm_crtc_vblank_put(crtc);
11719 kfree(work);
11720 return -EBUSY;
11721 }
11722 }
11723 intel_crtc->flip_work = work;
11724 spin_unlock_irq(&dev->event_lock);
11725
11726 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
11727 flush_workqueue(dev_priv->wq);
11728
11729 /* Reference the objects for the scheduled work. */
11730 drm_framebuffer_reference(work->old_fb);
11731 drm_gem_object_reference(&obj->base);
11732
11733 crtc->primary->fb = fb;
11734 update_state_fb(crtc->primary);
faf68d92
ML
11735
11736 intel_fbc_pre_update(intel_crtc, intel_crtc->config,
11737 to_intel_plane_state(primary->state));
5a21b665
DV
11738
11739 work->pending_flip_obj = obj;
11740
11741 ret = i915_mutex_lock_interruptible(dev);
11742 if (ret)
11743 goto cleanup;
11744
11745 intel_crtc->reset_counter = i915_reset_counter(&dev_priv->gpu_error);
11746 if (__i915_reset_in_progress_or_wedged(intel_crtc->reset_counter)) {
11747 ret = -EIO;
11748 goto cleanup;
11749 }
11750
11751 atomic_inc(&intel_crtc->unpin_work_count);
11752
11753 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
11754 work->flip_count = I915_READ(PIPE_FLIPCOUNT_G4X(pipe)) + 1;
11755
11756 if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
11757 engine = &dev_priv->engine[BCS];
11758 if (obj->tiling_mode != intel_fb_obj(work->old_fb)->tiling_mode)
11759 /* vlv: DISPLAY_FLIP fails to change tiling */
11760 engine = NULL;
11761 } else if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
11762 engine = &dev_priv->engine[BCS];
11763 } else if (INTEL_INFO(dev)->gen >= 7) {
11764 engine = i915_gem_request_get_engine(obj->last_write_req);
11765 if (engine == NULL || engine->id != RCS)
11766 engine = &dev_priv->engine[BCS];
11767 } else {
11768 engine = &dev_priv->engine[RCS];
11769 }
11770
11771 mmio_flip = use_mmio_flip(engine, obj);
11772
11773 /* When using CS flips, we want to emit semaphores between rings.
11774 * However, when using mmio flips we will create a task to do the
11775 * synchronisation, so all we want here is to pin the framebuffer
11776 * into the display plane and skip any waits.
11777 */
11778 if (!mmio_flip) {
11779 ret = i915_gem_object_sync(obj, engine, &request);
11780 if (!ret && !request) {
11781 request = i915_gem_request_alloc(engine, NULL);
11782 ret = PTR_ERR_OR_ZERO(request);
11783 }
11784
11785 if (ret)
11786 goto cleanup_pending;
11787 }
11788
11789 ret = intel_pin_and_fence_fb_obj(fb, primary->state->rotation);
11790 if (ret)
11791 goto cleanup_pending;
11792
11793 work->gtt_offset = intel_plane_obj_offset(to_intel_plane(primary),
11794 obj, 0);
11795 work->gtt_offset += intel_crtc->dspaddr_offset;
11796 work->rotation = crtc->primary->state->rotation;
11797
11798 if (mmio_flip) {
11799 INIT_WORK(&work->mmio_work, intel_mmio_flip_work_func);
11800
11801 i915_gem_request_assign(&work->flip_queued_req,
11802 obj->last_write_req);
11803
11804 schedule_work(&work->mmio_work);
11805 } else {
11806 i915_gem_request_assign(&work->flip_queued_req, request);
11807 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, request,
11808 page_flip_flags);
11809 if (ret)
11810 goto cleanup_unpin;
11811
11812 intel_mark_page_flip_active(intel_crtc, work);
11813
11814 i915_add_request_no_flush(request);
11815 }
11816
11817 i915_gem_track_fb(intel_fb_obj(old_fb), obj,
11818 to_intel_plane(primary)->frontbuffer_bit);
11819 mutex_unlock(&dev->struct_mutex);
11820
11821 intel_frontbuffer_flip_prepare(dev,
11822 to_intel_plane(primary)->frontbuffer_bit);
11823
11824 trace_i915_flip_request(intel_crtc->plane, obj);
11825
11826 return 0;
11827
11828cleanup_unpin:
11829 intel_unpin_fb_obj(fb, crtc->primary->state->rotation);
11830cleanup_pending:
11831 if (!IS_ERR_OR_NULL(request))
11832 i915_add_request_no_flush(request);
11833 atomic_dec(&intel_crtc->unpin_work_count);
11834 mutex_unlock(&dev->struct_mutex);
11835cleanup:
11836 crtc->primary->fb = old_fb;
11837 update_state_fb(crtc->primary);
11838
11839 drm_gem_object_unreference_unlocked(&obj->base);
11840 drm_framebuffer_unreference(work->old_fb);
11841
11842 spin_lock_irq(&dev->event_lock);
11843 intel_crtc->flip_work = NULL;
11844 spin_unlock_irq(&dev->event_lock);
11845
11846 drm_crtc_vblank_put(crtc);
11847free_work:
11848 kfree(work);
11849
11850 if (ret == -EIO) {
11851 struct drm_atomic_state *state;
11852 struct drm_plane_state *plane_state;
11853
11854out_hang:
11855 state = drm_atomic_state_alloc(dev);
11856 if (!state)
11857 return -ENOMEM;
11858 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
11859
11860retry:
11861 plane_state = drm_atomic_get_plane_state(state, primary);
11862 ret = PTR_ERR_OR_ZERO(plane_state);
11863 if (!ret) {
11864 drm_atomic_set_fb_for_plane(plane_state, fb);
11865
11866 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
11867 if (!ret)
11868 ret = drm_atomic_commit(state);
11869 }
11870
11871 if (ret == -EDEADLK) {
11872 drm_modeset_backoff(state->acquire_ctx);
11873 drm_atomic_state_clear(state);
11874 goto retry;
11875 }
11876
11877 if (ret)
11878 drm_atomic_state_free(state);
11879
11880 if (ret == 0 && event) {
11881 spin_lock_irq(&dev->event_lock);
11882 drm_crtc_send_vblank_event(crtc, event);
11883 spin_unlock_irq(&dev->event_lock);
11884 }
11885 }
11886 return ret;
11887}
11888
11889
11890/**
11891 * intel_wm_need_update - Check whether watermarks need updating
11892 * @plane: drm plane
11893 * @state: new plane state
11894 *
11895 * Check current plane state versus the new one to determine whether
11896 * watermarks need to be recalculated.
11897 *
11898 * Returns true or false.
11899 */
11900static bool intel_wm_need_update(struct drm_plane *plane,
11901 struct drm_plane_state *state)
11902{
11903 struct intel_plane_state *new = to_intel_plane_state(state);
11904 struct intel_plane_state *cur = to_intel_plane_state(plane->state);
11905
11906 /* Update watermarks on tiling or size changes. */
11907 if (new->visible != cur->visible)
11908 return true;
11909
11910 if (!cur->base.fb || !new->base.fb)
11911 return false;
11912
11913 if (cur->base.fb->modifier[0] != new->base.fb->modifier[0] ||
11914 cur->base.rotation != new->base.rotation ||
11915 drm_rect_width(&new->src) != drm_rect_width(&cur->src) ||
11916 drm_rect_height(&new->src) != drm_rect_height(&cur->src) ||
11917 drm_rect_width(&new->dst) != drm_rect_width(&cur->dst) ||
11918 drm_rect_height(&new->dst) != drm_rect_height(&cur->dst))
11919 return true;
11920
11921 return false;
11922}
11923
11924static bool needs_scaling(struct intel_plane_state *state)
11925{
11926 int src_w = drm_rect_width(&state->src) >> 16;
11927 int src_h = drm_rect_height(&state->src) >> 16;
11928 int dst_w = drm_rect_width(&state->dst);
11929 int dst_h = drm_rect_height(&state->dst);
11930
11931 return (src_w != dst_w || src_h != dst_h);
11932}
d21fbe87 11933
da20eabd
ML
11934int intel_plane_atomic_calc_changes(struct drm_crtc_state *crtc_state,
11935 struct drm_plane_state *plane_state)
11936{
ab1d3a0e 11937 struct intel_crtc_state *pipe_config = to_intel_crtc_state(crtc_state);
da20eabd
ML
11938 struct drm_crtc *crtc = crtc_state->crtc;
11939 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11940 struct drm_plane *plane = plane_state->plane;
11941 struct drm_device *dev = crtc->dev;
ed4a6a7c 11942 struct drm_i915_private *dev_priv = to_i915(dev);
da20eabd
ML
11943 struct intel_plane_state *old_plane_state =
11944 to_intel_plane_state(plane->state);
da20eabd
ML
11945 bool mode_changed = needs_modeset(crtc_state);
11946 bool was_crtc_enabled = crtc->state->active;
11947 bool is_crtc_enabled = crtc_state->active;
da20eabd
ML
11948 bool turn_off, turn_on, visible, was_visible;
11949 struct drm_framebuffer *fb = plane_state->fb;
78108b7c 11950 int ret;
da20eabd
ML
11951
11952 if (crtc_state && INTEL_INFO(dev)->gen >= 9 &&
11953 plane->type != DRM_PLANE_TYPE_CURSOR) {
11954 ret = skl_update_scaler_plane(
11955 to_intel_crtc_state(crtc_state),
11956 to_intel_plane_state(plane_state));
11957 if (ret)
11958 return ret;
11959 }
11960
da20eabd
ML
11961 was_visible = old_plane_state->visible;
11962 visible = to_intel_plane_state(plane_state)->visible;
11963
11964 if (!was_crtc_enabled && WARN_ON(was_visible))
11965 was_visible = false;
11966
35c08f43
ML
11967 /*
11968 * Visibility is calculated as if the crtc was on, but
11969 * after scaler setup everything depends on it being off
11970 * when the crtc isn't active.
f818ffea
VS
11971 *
11972 * FIXME this is wrong for watermarks. Watermarks should also
11973 * be computed as if the pipe would be active. Perhaps move
11974 * per-plane wm computation to the .check_plane() hook, and
11975 * only combine the results from all planes in the current place?
35c08f43
ML
11976 */
11977 if (!is_crtc_enabled)
11978 to_intel_plane_state(plane_state)->visible = visible = false;
da20eabd
ML
11979
11980 if (!was_visible && !visible)
11981 return 0;
11982
e8861675
ML
11983 if (fb != old_plane_state->base.fb)
11984 pipe_config->fb_changed = true;
11985
da20eabd
ML
11986 turn_off = was_visible && (!visible || mode_changed);
11987 turn_on = visible && (!was_visible || mode_changed);
11988
72660ce0 11989 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] has [PLANE:%d:%s] with fb %i\n",
78108b7c
VS
11990 intel_crtc->base.base.id,
11991 intel_crtc->base.name,
72660ce0
VS
11992 plane->base.id, plane->name,
11993 fb ? fb->base.id : -1);
da20eabd 11994
72660ce0
VS
11995 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] visible %i -> %i, off %i, on %i, ms %i\n",
11996 plane->base.id, plane->name,
11997 was_visible, visible,
da20eabd
ML
11998 turn_off, turn_on, mode_changed);
11999
caed361d
VS
12000 if (turn_on) {
12001 pipe_config->update_wm_pre = true;
12002
12003 /* must disable cxsr around plane enable/disable */
12004 if (plane->type != DRM_PLANE_TYPE_CURSOR)
12005 pipe_config->disable_cxsr = true;
12006 } else if (turn_off) {
12007 pipe_config->update_wm_post = true;
92826fcd 12008
852eb00d 12009 /* must disable cxsr around plane enable/disable */
e8861675 12010 if (plane->type != DRM_PLANE_TYPE_CURSOR)
ab1d3a0e 12011 pipe_config->disable_cxsr = true;
852eb00d 12012 } else if (intel_wm_need_update(plane, plane_state)) {
caed361d
VS
12013 /* FIXME bollocks */
12014 pipe_config->update_wm_pre = true;
12015 pipe_config->update_wm_post = true;
852eb00d 12016 }
da20eabd 12017
ed4a6a7c 12018 /* Pre-gen9 platforms need two-step watermark updates */
caed361d
VS
12019 if ((pipe_config->update_wm_pre || pipe_config->update_wm_post) &&
12020 INTEL_INFO(dev)->gen < 9 && dev_priv->display.optimize_watermarks)
ed4a6a7c
MR
12021 to_intel_crtc_state(crtc_state)->wm.need_postvbl_update = true;
12022
8be6ca85 12023 if (visible || was_visible)
cd202f69 12024 pipe_config->fb_bits |= to_intel_plane(plane)->frontbuffer_bit;
a9ff8714 12025
31ae71fc
ML
12026 /*
12027 * WaCxSRDisabledForSpriteScaling:ivb
12028 *
12029 * cstate->update_wm was already set above, so this flag will
12030 * take effect when we commit and program watermarks.
12031 */
12032 if (plane->type == DRM_PLANE_TYPE_OVERLAY && IS_IVYBRIDGE(dev) &&
12033 needs_scaling(to_intel_plane_state(plane_state)) &&
12034 !needs_scaling(old_plane_state))
12035 pipe_config->disable_lp_wm = true;
d21fbe87 12036
da20eabd
ML
12037 return 0;
12038}
12039
6d3a1ce7
ML
12040static bool encoders_cloneable(const struct intel_encoder *a,
12041 const struct intel_encoder *b)
12042{
12043 /* masks could be asymmetric, so check both ways */
12044 return a == b || (a->cloneable & (1 << b->type) &&
12045 b->cloneable & (1 << a->type));
12046}
12047
12048static bool check_single_encoder_cloning(struct drm_atomic_state *state,
12049 struct intel_crtc *crtc,
12050 struct intel_encoder *encoder)
12051{
12052 struct intel_encoder *source_encoder;
12053 struct drm_connector *connector;
12054 struct drm_connector_state *connector_state;
12055 int i;
12056
12057 for_each_connector_in_state(state, connector, connector_state, i) {
12058 if (connector_state->crtc != &crtc->base)
12059 continue;
12060
12061 source_encoder =
12062 to_intel_encoder(connector_state->best_encoder);
12063 if (!encoders_cloneable(encoder, source_encoder))
12064 return false;
12065 }
12066
12067 return true;
12068}
12069
12070static bool check_encoder_cloning(struct drm_atomic_state *state,
12071 struct intel_crtc *crtc)
12072{
12073 struct intel_encoder *encoder;
12074 struct drm_connector *connector;
12075 struct drm_connector_state *connector_state;
12076 int i;
12077
12078 for_each_connector_in_state(state, connector, connector_state, i) {
12079 if (connector_state->crtc != &crtc->base)
12080 continue;
12081
12082 encoder = to_intel_encoder(connector_state->best_encoder);
12083 if (!check_single_encoder_cloning(state, crtc, encoder))
12084 return false;
12085 }
12086
12087 return true;
12088}
12089
12090static int intel_crtc_atomic_check(struct drm_crtc *crtc,
12091 struct drm_crtc_state *crtc_state)
12092{
cf5a15be 12093 struct drm_device *dev = crtc->dev;
ad421372 12094 struct drm_i915_private *dev_priv = dev->dev_private;
6d3a1ce7 12095 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
cf5a15be
ML
12096 struct intel_crtc_state *pipe_config =
12097 to_intel_crtc_state(crtc_state);
6d3a1ce7 12098 struct drm_atomic_state *state = crtc_state->state;
4d20cd86 12099 int ret;
6d3a1ce7
ML
12100 bool mode_changed = needs_modeset(crtc_state);
12101
12102 if (mode_changed && !check_encoder_cloning(state, intel_crtc)) {
12103 DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
12104 return -EINVAL;
12105 }
12106
852eb00d 12107 if (mode_changed && !crtc_state->active)
caed361d 12108 pipe_config->update_wm_post = true;
eddfcbcd 12109
ad421372
ML
12110 if (mode_changed && crtc_state->enable &&
12111 dev_priv->display.crtc_compute_clock &&
8106ddbd 12112 !WARN_ON(pipe_config->shared_dpll)) {
ad421372
ML
12113 ret = dev_priv->display.crtc_compute_clock(intel_crtc,
12114 pipe_config);
12115 if (ret)
12116 return ret;
12117 }
12118
82cf435b
LL
12119 if (crtc_state->color_mgmt_changed) {
12120 ret = intel_color_check(crtc, crtc_state);
12121 if (ret)
12122 return ret;
12123 }
12124
e435d6e5 12125 ret = 0;
86c8bbbe 12126 if (dev_priv->display.compute_pipe_wm) {
e3bddded 12127 ret = dev_priv->display.compute_pipe_wm(pipe_config);
ed4a6a7c
MR
12128 if (ret) {
12129 DRM_DEBUG_KMS("Target pipe watermarks are invalid\n");
12130 return ret;
12131 }
12132 }
12133
12134 if (dev_priv->display.compute_intermediate_wm &&
12135 !to_intel_atomic_state(state)->skip_intermediate_wm) {
12136 if (WARN_ON(!dev_priv->display.compute_pipe_wm))
12137 return 0;
12138
12139 /*
12140 * Calculate 'intermediate' watermarks that satisfy both the
12141 * old state and the new state. We can program these
12142 * immediately.
12143 */
12144 ret = dev_priv->display.compute_intermediate_wm(crtc->dev,
12145 intel_crtc,
12146 pipe_config);
12147 if (ret) {
12148 DRM_DEBUG_KMS("No valid intermediate pipe watermarks are possible\n");
86c8bbbe 12149 return ret;
ed4a6a7c 12150 }
e3d5457c
VS
12151 } else if (dev_priv->display.compute_intermediate_wm) {
12152 if (HAS_PCH_SPLIT(dev_priv) && INTEL_GEN(dev_priv) < 9)
12153 pipe_config->wm.ilk.intermediate = pipe_config->wm.ilk.optimal;
86c8bbbe
MR
12154 }
12155
e435d6e5
ML
12156 if (INTEL_INFO(dev)->gen >= 9) {
12157 if (mode_changed)
12158 ret = skl_update_scaler_crtc(pipe_config);
12159
12160 if (!ret)
12161 ret = intel_atomic_setup_scalers(dev, intel_crtc,
12162 pipe_config);
12163 }
12164
12165 return ret;
6d3a1ce7
ML
12166}
12167
65b38e0d 12168static const struct drm_crtc_helper_funcs intel_helper_funcs = {
f6e5b160 12169 .mode_set_base_atomic = intel_pipe_set_base_atomic,
5a21b665
DV
12170 .atomic_begin = intel_begin_crtc_commit,
12171 .atomic_flush = intel_finish_crtc_commit,
6d3a1ce7 12172 .atomic_check = intel_crtc_atomic_check,
f6e5b160
CW
12173};
12174
d29b2f9d
ACO
12175static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
12176{
12177 struct intel_connector *connector;
12178
12179 for_each_intel_connector(dev, connector) {
8863dc7f
DV
12180 if (connector->base.state->crtc)
12181 drm_connector_unreference(&connector->base);
12182
d29b2f9d
ACO
12183 if (connector->base.encoder) {
12184 connector->base.state->best_encoder =
12185 connector->base.encoder;
12186 connector->base.state->crtc =
12187 connector->base.encoder->crtc;
8863dc7f
DV
12188
12189 drm_connector_reference(&connector->base);
d29b2f9d
ACO
12190 } else {
12191 connector->base.state->best_encoder = NULL;
12192 connector->base.state->crtc = NULL;
12193 }
12194 }
12195}
12196
050f7aeb 12197static void
eba905b2 12198connected_sink_compute_bpp(struct intel_connector *connector,
5cec258b 12199 struct intel_crtc_state *pipe_config)
050f7aeb
DV
12200{
12201 int bpp = pipe_config->pipe_bpp;
12202
12203 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
12204 connector->base.base.id,
c23cc417 12205 connector->base.name);
050f7aeb
DV
12206
12207 /* Don't use an invalid EDID bpc value */
12208 if (connector->base.display_info.bpc &&
12209 connector->base.display_info.bpc * 3 < bpp) {
12210 DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
12211 bpp, connector->base.display_info.bpc*3);
12212 pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
12213 }
12214
013dd9e0
JN
12215 /* Clamp bpp to default limit on screens without EDID 1.4 */
12216 if (connector->base.display_info.bpc == 0) {
12217 int type = connector->base.connector_type;
12218 int clamp_bpp = 24;
12219
12220 /* Fall back to 18 bpp when DP sink capability is unknown. */
12221 if (type == DRM_MODE_CONNECTOR_DisplayPort ||
12222 type == DRM_MODE_CONNECTOR_eDP)
12223 clamp_bpp = 18;
12224
12225 if (bpp > clamp_bpp) {
12226 DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of %d\n",
12227 bpp, clamp_bpp);
12228 pipe_config->pipe_bpp = clamp_bpp;
12229 }
050f7aeb
DV
12230 }
12231}
12232
4e53c2e0 12233static int
050f7aeb 12234compute_baseline_pipe_bpp(struct intel_crtc *crtc,
5cec258b 12235 struct intel_crtc_state *pipe_config)
4e53c2e0 12236{
050f7aeb 12237 struct drm_device *dev = crtc->base.dev;
1486017f 12238 struct drm_atomic_state *state;
da3ced29
ACO
12239 struct drm_connector *connector;
12240 struct drm_connector_state *connector_state;
1486017f 12241 int bpp, i;
4e53c2e0 12242
666a4537 12243 if ((IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)))
4e53c2e0 12244 bpp = 10*3;
d328c9d7
DV
12245 else if (INTEL_INFO(dev)->gen >= 5)
12246 bpp = 12*3;
12247 else
12248 bpp = 8*3;
12249
4e53c2e0 12250
4e53c2e0
DV
12251 pipe_config->pipe_bpp = bpp;
12252
1486017f
ACO
12253 state = pipe_config->base.state;
12254
4e53c2e0 12255 /* Clamp display bpp to EDID value */
da3ced29
ACO
12256 for_each_connector_in_state(state, connector, connector_state, i) {
12257 if (connector_state->crtc != &crtc->base)
4e53c2e0
DV
12258 continue;
12259
da3ced29
ACO
12260 connected_sink_compute_bpp(to_intel_connector(connector),
12261 pipe_config);
4e53c2e0
DV
12262 }
12263
12264 return bpp;
12265}
12266
644db711
DV
12267static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
12268{
12269 DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
12270 "type: 0x%x flags: 0x%x\n",
1342830c 12271 mode->crtc_clock,
644db711
DV
12272 mode->crtc_hdisplay, mode->crtc_hsync_start,
12273 mode->crtc_hsync_end, mode->crtc_htotal,
12274 mode->crtc_vdisplay, mode->crtc_vsync_start,
12275 mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
12276}
12277
c0b03411 12278static void intel_dump_pipe_config(struct intel_crtc *crtc,
5cec258b 12279 struct intel_crtc_state *pipe_config,
c0b03411
DV
12280 const char *context)
12281{
6a60cd87
CK
12282 struct drm_device *dev = crtc->base.dev;
12283 struct drm_plane *plane;
12284 struct intel_plane *intel_plane;
12285 struct intel_plane_state *state;
12286 struct drm_framebuffer *fb;
12287
78108b7c
VS
12288 DRM_DEBUG_KMS("[CRTC:%d:%s]%s config %p for pipe %c\n",
12289 crtc->base.base.id, crtc->base.name,
6a60cd87 12290 context, pipe_config, pipe_name(crtc->pipe));
c0b03411 12291
da205630 12292 DRM_DEBUG_KMS("cpu_transcoder: %s\n", transcoder_name(pipe_config->cpu_transcoder));
c0b03411
DV
12293 DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
12294 pipe_config->pipe_bpp, pipe_config->dither);
12295 DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
12296 pipe_config->has_pch_encoder,
12297 pipe_config->fdi_lanes,
12298 pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
12299 pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
12300 pipe_config->fdi_m_n.tu);
90a6b7b0 12301 DRM_DEBUG_KMS("dp: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
eb14cb74 12302 pipe_config->has_dp_encoder,
90a6b7b0 12303 pipe_config->lane_count,
eb14cb74
VS
12304 pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
12305 pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
12306 pipe_config->dp_m_n.tu);
b95af8be 12307
90a6b7b0 12308 DRM_DEBUG_KMS("dp: %i, lanes: %i, gmch_m2: %u, gmch_n2: %u, link_m2: %u, link_n2: %u, tu2: %u\n",
b95af8be 12309 pipe_config->has_dp_encoder,
90a6b7b0 12310 pipe_config->lane_count,
b95af8be
VK
12311 pipe_config->dp_m2_n2.gmch_m,
12312 pipe_config->dp_m2_n2.gmch_n,
12313 pipe_config->dp_m2_n2.link_m,
12314 pipe_config->dp_m2_n2.link_n,
12315 pipe_config->dp_m2_n2.tu);
12316
55072d19
DV
12317 DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
12318 pipe_config->has_audio,
12319 pipe_config->has_infoframe);
12320
c0b03411 12321 DRM_DEBUG_KMS("requested mode:\n");
2d112de7 12322 drm_mode_debug_printmodeline(&pipe_config->base.mode);
c0b03411 12323 DRM_DEBUG_KMS("adjusted mode:\n");
2d112de7
ACO
12324 drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
12325 intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
d71b8d4a 12326 DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
37327abd
VS
12327 DRM_DEBUG_KMS("pipe src size: %dx%d\n",
12328 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
0ec463d3
TU
12329 DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
12330 crtc->num_scalers,
12331 pipe_config->scaler_state.scaler_users,
12332 pipe_config->scaler_state.scaler_id);
c0b03411
DV
12333 DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
12334 pipe_config->gmch_pfit.control,
12335 pipe_config->gmch_pfit.pgm_ratios,
12336 pipe_config->gmch_pfit.lvds_border_bits);
fd4daa9c 12337 DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
c0b03411 12338 pipe_config->pch_pfit.pos,
fd4daa9c
CW
12339 pipe_config->pch_pfit.size,
12340 pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
42db64ef 12341 DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
cf532bb2 12342 DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
6a60cd87 12343
415ff0f6 12344 if (IS_BROXTON(dev)) {
05712c15 12345 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: ebb0: 0x%x, ebb4: 0x%x,"
415ff0f6 12346 "pll0: 0x%x, pll1: 0x%x, pll2: 0x%x, pll3: 0x%x, "
c8453338 12347 "pll6: 0x%x, pll8: 0x%x, pll9: 0x%x, pll10: 0x%x, pcsdw12: 0x%x\n",
415ff0f6
TU
12348 pipe_config->ddi_pll_sel,
12349 pipe_config->dpll_hw_state.ebb0,
05712c15 12350 pipe_config->dpll_hw_state.ebb4,
415ff0f6
TU
12351 pipe_config->dpll_hw_state.pll0,
12352 pipe_config->dpll_hw_state.pll1,
12353 pipe_config->dpll_hw_state.pll2,
12354 pipe_config->dpll_hw_state.pll3,
12355 pipe_config->dpll_hw_state.pll6,
12356 pipe_config->dpll_hw_state.pll8,
05712c15 12357 pipe_config->dpll_hw_state.pll9,
c8453338 12358 pipe_config->dpll_hw_state.pll10,
415ff0f6 12359 pipe_config->dpll_hw_state.pcsdw12);
ef11bdb3 12360 } else if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) {
415ff0f6
TU
12361 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: "
12362 "ctrl1: 0x%x, cfgcr1: 0x%x, cfgcr2: 0x%x\n",
12363 pipe_config->ddi_pll_sel,
12364 pipe_config->dpll_hw_state.ctrl1,
12365 pipe_config->dpll_hw_state.cfgcr1,
12366 pipe_config->dpll_hw_state.cfgcr2);
12367 } else if (HAS_DDI(dev)) {
1260f07e 12368 DRM_DEBUG_KMS("ddi_pll_sel: 0x%x; dpll_hw_state: wrpll: 0x%x spll: 0x%x\n",
415ff0f6 12369 pipe_config->ddi_pll_sel,
00490c22
ML
12370 pipe_config->dpll_hw_state.wrpll,
12371 pipe_config->dpll_hw_state.spll);
415ff0f6
TU
12372 } else {
12373 DRM_DEBUG_KMS("dpll_hw_state: dpll: 0x%x, dpll_md: 0x%x, "
12374 "fp0: 0x%x, fp1: 0x%x\n",
12375 pipe_config->dpll_hw_state.dpll,
12376 pipe_config->dpll_hw_state.dpll_md,
12377 pipe_config->dpll_hw_state.fp0,
12378 pipe_config->dpll_hw_state.fp1);
12379 }
12380
6a60cd87
CK
12381 DRM_DEBUG_KMS("planes on this crtc\n");
12382 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
12383 intel_plane = to_intel_plane(plane);
12384 if (intel_plane->pipe != crtc->pipe)
12385 continue;
12386
12387 state = to_intel_plane_state(plane->state);
12388 fb = state->base.fb;
12389 if (!fb) {
1d577e02
VS
12390 DRM_DEBUG_KMS("[PLANE:%d:%s] disabled, scaler_id = %d\n",
12391 plane->base.id, plane->name, state->scaler_id);
6a60cd87
CK
12392 continue;
12393 }
12394
1d577e02
VS
12395 DRM_DEBUG_KMS("[PLANE:%d:%s] enabled",
12396 plane->base.id, plane->name);
12397 DRM_DEBUG_KMS("\tFB:%d, fb = %ux%u format = %s",
12398 fb->base.id, fb->width, fb->height,
12399 drm_get_format_name(fb->pixel_format));
12400 DRM_DEBUG_KMS("\tscaler:%d src %dx%d+%d+%d dst %dx%d+%d+%d\n",
12401 state->scaler_id,
12402 state->src.x1 >> 16, state->src.y1 >> 16,
12403 drm_rect_width(&state->src) >> 16,
12404 drm_rect_height(&state->src) >> 16,
12405 state->dst.x1, state->dst.y1,
12406 drm_rect_width(&state->dst),
12407 drm_rect_height(&state->dst));
6a60cd87 12408 }
c0b03411
DV
12409}
12410
5448a00d 12411static bool check_digital_port_conflicts(struct drm_atomic_state *state)
00f0b378 12412{
5448a00d 12413 struct drm_device *dev = state->dev;
da3ced29 12414 struct drm_connector *connector;
00f0b378
VS
12415 unsigned int used_ports = 0;
12416
12417 /*
12418 * Walk the connector list instead of the encoder
12419 * list to detect the problem on ddi platforms
12420 * where there's just one encoder per digital port.
12421 */
0bff4858
VS
12422 drm_for_each_connector(connector, dev) {
12423 struct drm_connector_state *connector_state;
12424 struct intel_encoder *encoder;
12425
12426 connector_state = drm_atomic_get_existing_connector_state(state, connector);
12427 if (!connector_state)
12428 connector_state = connector->state;
12429
5448a00d 12430 if (!connector_state->best_encoder)
00f0b378
VS
12431 continue;
12432
5448a00d
ACO
12433 encoder = to_intel_encoder(connector_state->best_encoder);
12434
12435 WARN_ON(!connector_state->crtc);
00f0b378
VS
12436
12437 switch (encoder->type) {
12438 unsigned int port_mask;
12439 case INTEL_OUTPUT_UNKNOWN:
12440 if (WARN_ON(!HAS_DDI(dev)))
12441 break;
12442 case INTEL_OUTPUT_DISPLAYPORT:
12443 case INTEL_OUTPUT_HDMI:
12444 case INTEL_OUTPUT_EDP:
12445 port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
12446
12447 /* the same port mustn't appear more than once */
12448 if (used_ports & port_mask)
12449 return false;
12450
12451 used_ports |= port_mask;
12452 default:
12453 break;
12454 }
12455 }
12456
12457 return true;
12458}
12459
83a57153
ACO
12460static void
12461clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
12462{
12463 struct drm_crtc_state tmp_state;
663a3640 12464 struct intel_crtc_scaler_state scaler_state;
4978cc93 12465 struct intel_dpll_hw_state dpll_hw_state;
8106ddbd 12466 struct intel_shared_dpll *shared_dpll;
8504c74c 12467 uint32_t ddi_pll_sel;
c4e2d043 12468 bool force_thru;
83a57153 12469
7546a384
ACO
12470 /* FIXME: before the switch to atomic started, a new pipe_config was
12471 * kzalloc'd. Code that depends on any field being zero should be
12472 * fixed, so that the crtc_state can be safely duplicated. For now,
12473 * only fields that are know to not cause problems are preserved. */
12474
83a57153 12475 tmp_state = crtc_state->base;
663a3640 12476 scaler_state = crtc_state->scaler_state;
4978cc93
ACO
12477 shared_dpll = crtc_state->shared_dpll;
12478 dpll_hw_state = crtc_state->dpll_hw_state;
8504c74c 12479 ddi_pll_sel = crtc_state->ddi_pll_sel;
c4e2d043 12480 force_thru = crtc_state->pch_pfit.force_thru;
4978cc93 12481
83a57153 12482 memset(crtc_state, 0, sizeof *crtc_state);
4978cc93 12483
83a57153 12484 crtc_state->base = tmp_state;
663a3640 12485 crtc_state->scaler_state = scaler_state;
4978cc93
ACO
12486 crtc_state->shared_dpll = shared_dpll;
12487 crtc_state->dpll_hw_state = dpll_hw_state;
8504c74c 12488 crtc_state->ddi_pll_sel = ddi_pll_sel;
c4e2d043 12489 crtc_state->pch_pfit.force_thru = force_thru;
83a57153
ACO
12490}
12491
548ee15b 12492static int
b8cecdf5 12493intel_modeset_pipe_config(struct drm_crtc *crtc,
b359283a 12494 struct intel_crtc_state *pipe_config)
ee7b9f93 12495{
b359283a 12496 struct drm_atomic_state *state = pipe_config->base.state;
7758a113 12497 struct intel_encoder *encoder;
da3ced29 12498 struct drm_connector *connector;
0b901879 12499 struct drm_connector_state *connector_state;
d328c9d7 12500 int base_bpp, ret = -EINVAL;
0b901879 12501 int i;
e29c22c0 12502 bool retry = true;
ee7b9f93 12503
83a57153 12504 clear_intel_crtc_state(pipe_config);
7758a113 12505
e143a21c
DV
12506 pipe_config->cpu_transcoder =
12507 (enum transcoder) to_intel_crtc(crtc)->pipe;
b8cecdf5 12508
2960bc9c
ID
12509 /*
12510 * Sanitize sync polarity flags based on requested ones. If neither
12511 * positive or negative polarity is requested, treat this as meaning
12512 * negative polarity.
12513 */
2d112de7 12514 if (!(pipe_config->base.adjusted_mode.flags &
2960bc9c 12515 (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
2d112de7 12516 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
2960bc9c 12517
2d112de7 12518 if (!(pipe_config->base.adjusted_mode.flags &
2960bc9c 12519 (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
2d112de7 12520 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
2960bc9c 12521
d328c9d7
DV
12522 base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
12523 pipe_config);
12524 if (base_bpp < 0)
4e53c2e0
DV
12525 goto fail;
12526
e41a56be
VS
12527 /*
12528 * Determine the real pipe dimensions. Note that stereo modes can
12529 * increase the actual pipe size due to the frame doubling and
12530 * insertion of additional space for blanks between the frame. This
12531 * is stored in the crtc timings. We use the requested mode to do this
12532 * computation to clearly distinguish it from the adjusted mode, which
12533 * can be changed by the connectors in the below retry loop.
12534 */
2d112de7 12535 drm_crtc_get_hv_timing(&pipe_config->base.mode,
ecb7e16b
GP
12536 &pipe_config->pipe_src_w,
12537 &pipe_config->pipe_src_h);
e41a56be 12538
e29c22c0 12539encoder_retry:
ef1b460d 12540 /* Ensure the port clock defaults are reset when retrying. */
ff9a6750 12541 pipe_config->port_clock = 0;
ef1b460d 12542 pipe_config->pixel_multiplier = 1;
ff9a6750 12543
135c81b8 12544 /* Fill in default crtc timings, allow encoders to overwrite them. */
2d112de7
ACO
12545 drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
12546 CRTC_STEREO_DOUBLE);
135c81b8 12547
7758a113
DV
12548 /* Pass our mode to the connectors and the CRTC to give them a chance to
12549 * adjust it according to limitations or connector properties, and also
12550 * a chance to reject the mode entirely.
47f1c6c9 12551 */
da3ced29 12552 for_each_connector_in_state(state, connector, connector_state, i) {
0b901879 12553 if (connector_state->crtc != crtc)
7758a113 12554 continue;
7ae89233 12555
0b901879
ACO
12556 encoder = to_intel_encoder(connector_state->best_encoder);
12557
efea6e8e
DV
12558 if (!(encoder->compute_config(encoder, pipe_config))) {
12559 DRM_DEBUG_KMS("Encoder config failure\n");
7758a113
DV
12560 goto fail;
12561 }
ee7b9f93 12562 }
47f1c6c9 12563
ff9a6750
DV
12564 /* Set default port clock if not overwritten by the encoder. Needs to be
12565 * done afterwards in case the encoder adjusts the mode. */
12566 if (!pipe_config->port_clock)
2d112de7 12567 pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
241bfc38 12568 * pipe_config->pixel_multiplier;
ff9a6750 12569
a43f6e0f 12570 ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
e29c22c0 12571 if (ret < 0) {
7758a113
DV
12572 DRM_DEBUG_KMS("CRTC fixup failed\n");
12573 goto fail;
ee7b9f93 12574 }
e29c22c0
DV
12575
12576 if (ret == RETRY) {
12577 if (WARN(!retry, "loop in pipe configuration computation\n")) {
12578 ret = -EINVAL;
12579 goto fail;
12580 }
12581
12582 DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
12583 retry = false;
12584 goto encoder_retry;
12585 }
12586
e8fa4270
DV
12587 /* Dithering seems to not pass-through bits correctly when it should, so
12588 * only enable it on 6bpc panels. */
12589 pipe_config->dither = pipe_config->pipe_bpp == 6*3;
62f0ace5 12590 DRM_DEBUG_KMS("hw max bpp: %i, pipe bpp: %i, dithering: %i\n",
d328c9d7 12591 base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
4e53c2e0 12592
7758a113 12593fail:
548ee15b 12594 return ret;
ee7b9f93 12595}
47f1c6c9 12596
ea9d758d 12597static void
4740b0f2 12598intel_modeset_update_crtc_state(struct drm_atomic_state *state)
ea9d758d 12599{
0a9ab303
ACO
12600 struct drm_crtc *crtc;
12601 struct drm_crtc_state *crtc_state;
8a75d157 12602 int i;
ea9d758d 12603
7668851f 12604 /* Double check state. */
8a75d157 12605 for_each_crtc_in_state(state, crtc, crtc_state, i) {
3cb480bc 12606 to_intel_crtc(crtc)->config = to_intel_crtc_state(crtc->state);
fc467a22
ML
12607
12608 /* Update hwmode for vblank functions */
12609 if (crtc->state->active)
12610 crtc->hwmode = crtc->state->adjusted_mode;
12611 else
12612 crtc->hwmode.crtc_clock = 0;
61067a5e
ML
12613
12614 /*
12615 * Update legacy state to satisfy fbc code. This can
12616 * be removed when fbc uses the atomic state.
12617 */
12618 if (drm_atomic_get_existing_plane_state(state, crtc->primary)) {
12619 struct drm_plane_state *plane_state = crtc->primary->state;
12620
12621 crtc->primary->fb = plane_state->fb;
12622 crtc->x = plane_state->src_x >> 16;
12623 crtc->y = plane_state->src_y >> 16;
12624 }
ea9d758d 12625 }
ea9d758d
DV
12626}
12627
3bd26263 12628static bool intel_fuzzy_clock_check(int clock1, int clock2)
f1f644dc 12629{
3bd26263 12630 int diff;
f1f644dc
JB
12631
12632 if (clock1 == clock2)
12633 return true;
12634
12635 if (!clock1 || !clock2)
12636 return false;
12637
12638 diff = abs(clock1 - clock2);
12639
12640 if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
12641 return true;
12642
12643 return false;
12644}
12645
25c5b266
DV
12646#define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
12647 list_for_each_entry((intel_crtc), \
12648 &(dev)->mode_config.crtc_list, \
12649 base.head) \
95150bdf 12650 for_each_if (mask & (1 <<(intel_crtc)->pipe))
25c5b266 12651
cfb23ed6
ML
12652static bool
12653intel_compare_m_n(unsigned int m, unsigned int n,
12654 unsigned int m2, unsigned int n2,
12655 bool exact)
12656{
12657 if (m == m2 && n == n2)
12658 return true;
12659
12660 if (exact || !m || !n || !m2 || !n2)
12661 return false;
12662
12663 BUILD_BUG_ON(DATA_LINK_M_N_MASK > INT_MAX);
12664
31d10b57
ML
12665 if (n > n2) {
12666 while (n > n2) {
cfb23ed6
ML
12667 m2 <<= 1;
12668 n2 <<= 1;
12669 }
31d10b57
ML
12670 } else if (n < n2) {
12671 while (n < n2) {
cfb23ed6
ML
12672 m <<= 1;
12673 n <<= 1;
12674 }
12675 }
12676
31d10b57
ML
12677 if (n != n2)
12678 return false;
12679
12680 return intel_fuzzy_clock_check(m, m2);
cfb23ed6
ML
12681}
12682
12683static bool
12684intel_compare_link_m_n(const struct intel_link_m_n *m_n,
12685 struct intel_link_m_n *m2_n2,
12686 bool adjust)
12687{
12688 if (m_n->tu == m2_n2->tu &&
12689 intel_compare_m_n(m_n->gmch_m, m_n->gmch_n,
12690 m2_n2->gmch_m, m2_n2->gmch_n, !adjust) &&
12691 intel_compare_m_n(m_n->link_m, m_n->link_n,
12692 m2_n2->link_m, m2_n2->link_n, !adjust)) {
12693 if (adjust)
12694 *m2_n2 = *m_n;
12695
12696 return true;
12697 }
12698
12699 return false;
12700}
12701
0e8ffe1b 12702static bool
2fa2fe9a 12703intel_pipe_config_compare(struct drm_device *dev,
5cec258b 12704 struct intel_crtc_state *current_config,
cfb23ed6
ML
12705 struct intel_crtc_state *pipe_config,
12706 bool adjust)
0e8ffe1b 12707{
cfb23ed6
ML
12708 bool ret = true;
12709
12710#define INTEL_ERR_OR_DBG_KMS(fmt, ...) \
12711 do { \
12712 if (!adjust) \
12713 DRM_ERROR(fmt, ##__VA_ARGS__); \
12714 else \
12715 DRM_DEBUG_KMS(fmt, ##__VA_ARGS__); \
12716 } while (0)
12717
66e985c0
DV
12718#define PIPE_CONF_CHECK_X(name) \
12719 if (current_config->name != pipe_config->name) { \
cfb23ed6 12720 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
66e985c0
DV
12721 "(expected 0x%08x, found 0x%08x)\n", \
12722 current_config->name, \
12723 pipe_config->name); \
cfb23ed6 12724 ret = false; \
66e985c0
DV
12725 }
12726
08a24034
DV
12727#define PIPE_CONF_CHECK_I(name) \
12728 if (current_config->name != pipe_config->name) { \
cfb23ed6 12729 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
08a24034
DV
12730 "(expected %i, found %i)\n", \
12731 current_config->name, \
12732 pipe_config->name); \
cfb23ed6
ML
12733 ret = false; \
12734 }
12735
8106ddbd
ACO
12736#define PIPE_CONF_CHECK_P(name) \
12737 if (current_config->name != pipe_config->name) { \
12738 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
12739 "(expected %p, found %p)\n", \
12740 current_config->name, \
12741 pipe_config->name); \
12742 ret = false; \
12743 }
12744
cfb23ed6
ML
12745#define PIPE_CONF_CHECK_M_N(name) \
12746 if (!intel_compare_link_m_n(&current_config->name, \
12747 &pipe_config->name,\
12748 adjust)) { \
12749 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
12750 "(expected tu %i gmch %i/%i link %i/%i, " \
12751 "found tu %i, gmch %i/%i link %i/%i)\n", \
12752 current_config->name.tu, \
12753 current_config->name.gmch_m, \
12754 current_config->name.gmch_n, \
12755 current_config->name.link_m, \
12756 current_config->name.link_n, \
12757 pipe_config->name.tu, \
12758 pipe_config->name.gmch_m, \
12759 pipe_config->name.gmch_n, \
12760 pipe_config->name.link_m, \
12761 pipe_config->name.link_n); \
12762 ret = false; \
12763 }
12764
55c561a7
DV
12765/* This is required for BDW+ where there is only one set of registers for
12766 * switching between high and low RR.
12767 * This macro can be used whenever a comparison has to be made between one
12768 * hw state and multiple sw state variables.
12769 */
cfb23ed6
ML
12770#define PIPE_CONF_CHECK_M_N_ALT(name, alt_name) \
12771 if (!intel_compare_link_m_n(&current_config->name, \
12772 &pipe_config->name, adjust) && \
12773 !intel_compare_link_m_n(&current_config->alt_name, \
12774 &pipe_config->name, adjust)) { \
12775 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
12776 "(expected tu %i gmch %i/%i link %i/%i, " \
12777 "or tu %i gmch %i/%i link %i/%i, " \
12778 "found tu %i, gmch %i/%i link %i/%i)\n", \
12779 current_config->name.tu, \
12780 current_config->name.gmch_m, \
12781 current_config->name.gmch_n, \
12782 current_config->name.link_m, \
12783 current_config->name.link_n, \
12784 current_config->alt_name.tu, \
12785 current_config->alt_name.gmch_m, \
12786 current_config->alt_name.gmch_n, \
12787 current_config->alt_name.link_m, \
12788 current_config->alt_name.link_n, \
12789 pipe_config->name.tu, \
12790 pipe_config->name.gmch_m, \
12791 pipe_config->name.gmch_n, \
12792 pipe_config->name.link_m, \
12793 pipe_config->name.link_n); \
12794 ret = false; \
88adfff1
DV
12795 }
12796
1bd1bd80
DV
12797#define PIPE_CONF_CHECK_FLAGS(name, mask) \
12798 if ((current_config->name ^ pipe_config->name) & (mask)) { \
cfb23ed6 12799 INTEL_ERR_OR_DBG_KMS("mismatch in " #name "(" #mask ") " \
1bd1bd80
DV
12800 "(expected %i, found %i)\n", \
12801 current_config->name & (mask), \
12802 pipe_config->name & (mask)); \
cfb23ed6 12803 ret = false; \
1bd1bd80
DV
12804 }
12805
5e550656
VS
12806#define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
12807 if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
cfb23ed6 12808 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
5e550656
VS
12809 "(expected %i, found %i)\n", \
12810 current_config->name, \
12811 pipe_config->name); \
cfb23ed6 12812 ret = false; \
5e550656
VS
12813 }
12814
bb760063
DV
12815#define PIPE_CONF_QUIRK(quirk) \
12816 ((current_config->quirks | pipe_config->quirks) & (quirk))
12817
eccb140b
DV
12818 PIPE_CONF_CHECK_I(cpu_transcoder);
12819
08a24034
DV
12820 PIPE_CONF_CHECK_I(has_pch_encoder);
12821 PIPE_CONF_CHECK_I(fdi_lanes);
cfb23ed6 12822 PIPE_CONF_CHECK_M_N(fdi_m_n);
08a24034 12823
eb14cb74 12824 PIPE_CONF_CHECK_I(has_dp_encoder);
90a6b7b0 12825 PIPE_CONF_CHECK_I(lane_count);
95a7a2ae 12826 PIPE_CONF_CHECK_X(lane_lat_optim_mask);
b95af8be
VK
12827
12828 if (INTEL_INFO(dev)->gen < 8) {
cfb23ed6
ML
12829 PIPE_CONF_CHECK_M_N(dp_m_n);
12830
cfb23ed6
ML
12831 if (current_config->has_drrs)
12832 PIPE_CONF_CHECK_M_N(dp_m2_n2);
12833 } else
12834 PIPE_CONF_CHECK_M_N_ALT(dp_m_n, dp_m2_n2);
eb14cb74 12835
a65347ba
JN
12836 PIPE_CONF_CHECK_I(has_dsi_encoder);
12837
2d112de7
ACO
12838 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
12839 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
12840 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
12841 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
12842 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
12843 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
1bd1bd80 12844
2d112de7
ACO
12845 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
12846 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
12847 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
12848 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
12849 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
12850 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
1bd1bd80 12851
c93f54cf 12852 PIPE_CONF_CHECK_I(pixel_multiplier);
6897b4b5 12853 PIPE_CONF_CHECK_I(has_hdmi_sink);
b5a9fa09 12854 if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
666a4537 12855 IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev))
b5a9fa09 12856 PIPE_CONF_CHECK_I(limited_color_range);
e43823ec 12857 PIPE_CONF_CHECK_I(has_infoframe);
6c49f241 12858
9ed109a7
DV
12859 PIPE_CONF_CHECK_I(has_audio);
12860
2d112de7 12861 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
1bd1bd80
DV
12862 DRM_MODE_FLAG_INTERLACE);
12863
bb760063 12864 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
2d112de7 12865 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063 12866 DRM_MODE_FLAG_PHSYNC);
2d112de7 12867 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063 12868 DRM_MODE_FLAG_NHSYNC);
2d112de7 12869 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063 12870 DRM_MODE_FLAG_PVSYNC);
2d112de7 12871 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063
DV
12872 DRM_MODE_FLAG_NVSYNC);
12873 }
045ac3b5 12874
333b8ca8 12875 PIPE_CONF_CHECK_X(gmch_pfit.control);
e2ff2d4a
DV
12876 /* pfit ratios are autocomputed by the hw on gen4+ */
12877 if (INTEL_INFO(dev)->gen < 4)
7f7d8dd6 12878 PIPE_CONF_CHECK_X(gmch_pfit.pgm_ratios);
333b8ca8 12879 PIPE_CONF_CHECK_X(gmch_pfit.lvds_border_bits);
9953599b 12880
bfd16b2a
ML
12881 if (!adjust) {
12882 PIPE_CONF_CHECK_I(pipe_src_w);
12883 PIPE_CONF_CHECK_I(pipe_src_h);
12884
12885 PIPE_CONF_CHECK_I(pch_pfit.enabled);
12886 if (current_config->pch_pfit.enabled) {
12887 PIPE_CONF_CHECK_X(pch_pfit.pos);
12888 PIPE_CONF_CHECK_X(pch_pfit.size);
12889 }
2fa2fe9a 12890
7aefe2b5
ML
12891 PIPE_CONF_CHECK_I(scaler_state.scaler_id);
12892 }
a1b2278e 12893
e59150dc
JB
12894 /* BDW+ don't expose a synchronous way to read the state */
12895 if (IS_HASWELL(dev))
12896 PIPE_CONF_CHECK_I(ips_enabled);
42db64ef 12897
282740f7
VS
12898 PIPE_CONF_CHECK_I(double_wide);
12899
26804afd
DV
12900 PIPE_CONF_CHECK_X(ddi_pll_sel);
12901
8106ddbd 12902 PIPE_CONF_CHECK_P(shared_dpll);
66e985c0 12903 PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
8bcc2795 12904 PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
66e985c0
DV
12905 PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
12906 PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
d452c5b6 12907 PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
00490c22 12908 PIPE_CONF_CHECK_X(dpll_hw_state.spll);
3f4cd19f
DL
12909 PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
12910 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
12911 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
c0d43d62 12912
47eacbab
VS
12913 PIPE_CONF_CHECK_X(dsi_pll.ctrl);
12914 PIPE_CONF_CHECK_X(dsi_pll.div);
12915
42571aef
VS
12916 if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
12917 PIPE_CONF_CHECK_I(pipe_bpp);
12918
2d112de7 12919 PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
a9a7e98a 12920 PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
5e550656 12921
66e985c0 12922#undef PIPE_CONF_CHECK_X
08a24034 12923#undef PIPE_CONF_CHECK_I
8106ddbd 12924#undef PIPE_CONF_CHECK_P
1bd1bd80 12925#undef PIPE_CONF_CHECK_FLAGS
5e550656 12926#undef PIPE_CONF_CHECK_CLOCK_FUZZY
bb760063 12927#undef PIPE_CONF_QUIRK
cfb23ed6 12928#undef INTEL_ERR_OR_DBG_KMS
88adfff1 12929
cfb23ed6 12930 return ret;
0e8ffe1b
DV
12931}
12932
e3b247da
VS
12933static void intel_pipe_config_sanity_check(struct drm_i915_private *dev_priv,
12934 const struct intel_crtc_state *pipe_config)
12935{
12936 if (pipe_config->has_pch_encoder) {
21a727b3 12937 int fdi_dotclock = intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
e3b247da
VS
12938 &pipe_config->fdi_m_n);
12939 int dotclock = pipe_config->base.adjusted_mode.crtc_clock;
12940
12941 /*
12942 * FDI already provided one idea for the dotclock.
12943 * Yell if the encoder disagrees.
12944 */
12945 WARN(!intel_fuzzy_clock_check(fdi_dotclock, dotclock),
12946 "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
12947 fdi_dotclock, dotclock);
12948 }
12949}
12950
c0ead703
ML
12951static void verify_wm_state(struct drm_crtc *crtc,
12952 struct drm_crtc_state *new_state)
08db6652 12953{
e7c84544 12954 struct drm_device *dev = crtc->dev;
08db6652
DL
12955 struct drm_i915_private *dev_priv = dev->dev_private;
12956 struct skl_ddb_allocation hw_ddb, *sw_ddb;
e7c84544
ML
12957 struct skl_ddb_entry *hw_entry, *sw_entry;
12958 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
12959 const enum pipe pipe = intel_crtc->pipe;
08db6652
DL
12960 int plane;
12961
e7c84544 12962 if (INTEL_INFO(dev)->gen < 9 || !new_state->active)
08db6652
DL
12963 return;
12964
12965 skl_ddb_get_hw_state(dev_priv, &hw_ddb);
12966 sw_ddb = &dev_priv->wm.skl_hw.ddb;
12967
e7c84544
ML
12968 /* planes */
12969 for_each_plane(dev_priv, pipe, plane) {
12970 hw_entry = &hw_ddb.plane[pipe][plane];
12971 sw_entry = &sw_ddb->plane[pipe][plane];
08db6652 12972
e7c84544 12973 if (skl_ddb_entry_equal(hw_entry, sw_entry))
08db6652
DL
12974 continue;
12975
e7c84544
ML
12976 DRM_ERROR("mismatch in DDB state pipe %c plane %d "
12977 "(expected (%u,%u), found (%u,%u))\n",
12978 pipe_name(pipe), plane + 1,
12979 sw_entry->start, sw_entry->end,
12980 hw_entry->start, hw_entry->end);
12981 }
08db6652 12982
e7c84544
ML
12983 /* cursor */
12984 hw_entry = &hw_ddb.plane[pipe][PLANE_CURSOR];
12985 sw_entry = &sw_ddb->plane[pipe][PLANE_CURSOR];
08db6652 12986
e7c84544 12987 if (!skl_ddb_entry_equal(hw_entry, sw_entry)) {
08db6652
DL
12988 DRM_ERROR("mismatch in DDB state pipe %c cursor "
12989 "(expected (%u,%u), found (%u,%u))\n",
12990 pipe_name(pipe),
12991 sw_entry->start, sw_entry->end,
12992 hw_entry->start, hw_entry->end);
12993 }
12994}
12995
91d1b4bd 12996static void
c0ead703 12997verify_connector_state(struct drm_device *dev, struct drm_crtc *crtc)
8af6cf88 12998{
35dd3c64 12999 struct drm_connector *connector;
8af6cf88 13000
e7c84544 13001 drm_for_each_connector(connector, dev) {
35dd3c64
ML
13002 struct drm_encoder *encoder = connector->encoder;
13003 struct drm_connector_state *state = connector->state;
ad3c558f 13004
e7c84544
ML
13005 if (state->crtc != crtc)
13006 continue;
13007
5a21b665 13008 intel_connector_verify_state(to_intel_connector(connector));
8af6cf88 13009
ad3c558f 13010 I915_STATE_WARN(state->best_encoder != encoder,
35dd3c64 13011 "connector's atomic encoder doesn't match legacy encoder\n");
8af6cf88 13012 }
91d1b4bd
DV
13013}
13014
13015static void
c0ead703 13016verify_encoder_state(struct drm_device *dev)
91d1b4bd
DV
13017{
13018 struct intel_encoder *encoder;
13019 struct intel_connector *connector;
8af6cf88 13020
b2784e15 13021 for_each_intel_encoder(dev, encoder) {
8af6cf88 13022 bool enabled = false;
4d20cd86 13023 enum pipe pipe;
8af6cf88
DV
13024
13025 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
13026 encoder->base.base.id,
8e329a03 13027 encoder->base.name);
8af6cf88 13028
3a3371ff 13029 for_each_intel_connector(dev, connector) {
4d20cd86 13030 if (connector->base.state->best_encoder != &encoder->base)
8af6cf88
DV
13031 continue;
13032 enabled = true;
ad3c558f
ML
13033
13034 I915_STATE_WARN(connector->base.state->crtc !=
13035 encoder->base.crtc,
13036 "connector's crtc doesn't match encoder crtc\n");
8af6cf88 13037 }
0e32b39c 13038
e2c719b7 13039 I915_STATE_WARN(!!encoder->base.crtc != enabled,
8af6cf88
DV
13040 "encoder's enabled state mismatch "
13041 "(expected %i, found %i)\n",
13042 !!encoder->base.crtc, enabled);
7c60d198
ML
13043
13044 if (!encoder->base.crtc) {
4d20cd86 13045 bool active;
7c60d198 13046
4d20cd86
ML
13047 active = encoder->get_hw_state(encoder, &pipe);
13048 I915_STATE_WARN(active,
13049 "encoder detached but still enabled on pipe %c.\n",
13050 pipe_name(pipe));
7c60d198 13051 }
8af6cf88 13052 }
91d1b4bd
DV
13053}
13054
13055static void
c0ead703
ML
13056verify_crtc_state(struct drm_crtc *crtc,
13057 struct drm_crtc_state *old_crtc_state,
13058 struct drm_crtc_state *new_crtc_state)
91d1b4bd 13059{
e7c84544 13060 struct drm_device *dev = crtc->dev;
fbee40df 13061 struct drm_i915_private *dev_priv = dev->dev_private;
91d1b4bd 13062 struct intel_encoder *encoder;
e7c84544
ML
13063 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13064 struct intel_crtc_state *pipe_config, *sw_config;
13065 struct drm_atomic_state *old_state;
13066 bool active;
045ac3b5 13067
e7c84544 13068 old_state = old_crtc_state->state;
ec2dc6a0 13069 __drm_atomic_helper_crtc_destroy_state(old_crtc_state);
e7c84544
ML
13070 pipe_config = to_intel_crtc_state(old_crtc_state);
13071 memset(pipe_config, 0, sizeof(*pipe_config));
13072 pipe_config->base.crtc = crtc;
13073 pipe_config->base.state = old_state;
8af6cf88 13074
78108b7c 13075 DRM_DEBUG_KMS("[CRTC:%d:%s]\n", crtc->base.id, crtc->name);
8af6cf88 13076
e7c84544 13077 active = dev_priv->display.get_pipe_config(intel_crtc, pipe_config);
d62cf62a 13078
e7c84544
ML
13079 /* hw state is inconsistent with the pipe quirk */
13080 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
13081 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
13082 active = new_crtc_state->active;
6c49f241 13083
e7c84544
ML
13084 I915_STATE_WARN(new_crtc_state->active != active,
13085 "crtc active state doesn't match with hw state "
13086 "(expected %i, found %i)\n", new_crtc_state->active, active);
0e8ffe1b 13087
e7c84544
ML
13088 I915_STATE_WARN(intel_crtc->active != new_crtc_state->active,
13089 "transitional active state does not match atomic hw state "
13090 "(expected %i, found %i)\n", new_crtc_state->active, intel_crtc->active);
4d20cd86 13091
e7c84544
ML
13092 for_each_encoder_on_crtc(dev, crtc, encoder) {
13093 enum pipe pipe;
4d20cd86 13094
e7c84544
ML
13095 active = encoder->get_hw_state(encoder, &pipe);
13096 I915_STATE_WARN(active != new_crtc_state->active,
13097 "[ENCODER:%i] active %i with crtc active %i\n",
13098 encoder->base.base.id, active, new_crtc_state->active);
4d20cd86 13099
e7c84544
ML
13100 I915_STATE_WARN(active && intel_crtc->pipe != pipe,
13101 "Encoder connected to wrong pipe %c\n",
13102 pipe_name(pipe));
4d20cd86 13103
e7c84544
ML
13104 if (active)
13105 encoder->get_config(encoder, pipe_config);
13106 }
53d9f4e9 13107
e7c84544
ML
13108 if (!new_crtc_state->active)
13109 return;
cfb23ed6 13110
e7c84544 13111 intel_pipe_config_sanity_check(dev_priv, pipe_config);
e3b247da 13112
e7c84544
ML
13113 sw_config = to_intel_crtc_state(crtc->state);
13114 if (!intel_pipe_config_compare(dev, sw_config,
13115 pipe_config, false)) {
13116 I915_STATE_WARN(1, "pipe state doesn't match!\n");
13117 intel_dump_pipe_config(intel_crtc, pipe_config,
13118 "[hw state]");
13119 intel_dump_pipe_config(intel_crtc, sw_config,
13120 "[sw state]");
8af6cf88
DV
13121 }
13122}
13123
91d1b4bd 13124static void
c0ead703
ML
13125verify_single_dpll_state(struct drm_i915_private *dev_priv,
13126 struct intel_shared_dpll *pll,
13127 struct drm_crtc *crtc,
13128 struct drm_crtc_state *new_state)
91d1b4bd 13129{
91d1b4bd 13130 struct intel_dpll_hw_state dpll_hw_state;
e7c84544
ML
13131 unsigned crtc_mask;
13132 bool active;
5358901f 13133
e7c84544 13134 memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
5358901f 13135
e7c84544 13136 DRM_DEBUG_KMS("%s\n", pll->name);
5358901f 13137
e7c84544 13138 active = pll->funcs.get_hw_state(dev_priv, pll, &dpll_hw_state);
5358901f 13139
e7c84544
ML
13140 if (!(pll->flags & INTEL_DPLL_ALWAYS_ON)) {
13141 I915_STATE_WARN(!pll->on && pll->active_mask,
13142 "pll in active use but not on in sw tracking\n");
13143 I915_STATE_WARN(pll->on && !pll->active_mask,
13144 "pll is on but not used by any active crtc\n");
13145 I915_STATE_WARN(pll->on != active,
13146 "pll on state mismatch (expected %i, found %i)\n",
13147 pll->on, active);
13148 }
5358901f 13149
e7c84544 13150 if (!crtc) {
2dd66ebd 13151 I915_STATE_WARN(pll->active_mask & ~pll->config.crtc_mask,
e7c84544
ML
13152 "more active pll users than references: %x vs %x\n",
13153 pll->active_mask, pll->config.crtc_mask);
5358901f 13154
e7c84544
ML
13155 return;
13156 }
13157
13158 crtc_mask = 1 << drm_crtc_index(crtc);
13159
13160 if (new_state->active)
13161 I915_STATE_WARN(!(pll->active_mask & crtc_mask),
13162 "pll active mismatch (expected pipe %c in active mask 0x%02x)\n",
13163 pipe_name(drm_crtc_index(crtc)), pll->active_mask);
13164 else
13165 I915_STATE_WARN(pll->active_mask & crtc_mask,
13166 "pll active mismatch (didn't expect pipe %c in active mask 0x%02x)\n",
13167 pipe_name(drm_crtc_index(crtc)), pll->active_mask);
2dd66ebd 13168
e7c84544
ML
13169 I915_STATE_WARN(!(pll->config.crtc_mask & crtc_mask),
13170 "pll enabled crtcs mismatch (expected 0x%x in 0x%02x)\n",
13171 crtc_mask, pll->config.crtc_mask);
66e985c0 13172
e7c84544
ML
13173 I915_STATE_WARN(pll->on && memcmp(&pll->config.hw_state,
13174 &dpll_hw_state,
13175 sizeof(dpll_hw_state)),
13176 "pll hw state mismatch\n");
13177}
13178
13179static void
c0ead703
ML
13180verify_shared_dpll_state(struct drm_device *dev, struct drm_crtc *crtc,
13181 struct drm_crtc_state *old_crtc_state,
13182 struct drm_crtc_state *new_crtc_state)
e7c84544
ML
13183{
13184 struct drm_i915_private *dev_priv = dev->dev_private;
13185 struct intel_crtc_state *old_state = to_intel_crtc_state(old_crtc_state);
13186 struct intel_crtc_state *new_state = to_intel_crtc_state(new_crtc_state);
13187
13188 if (new_state->shared_dpll)
c0ead703 13189 verify_single_dpll_state(dev_priv, new_state->shared_dpll, crtc, new_crtc_state);
e7c84544
ML
13190
13191 if (old_state->shared_dpll &&
13192 old_state->shared_dpll != new_state->shared_dpll) {
13193 unsigned crtc_mask = 1 << drm_crtc_index(crtc);
13194 struct intel_shared_dpll *pll = old_state->shared_dpll;
13195
13196 I915_STATE_WARN(pll->active_mask & crtc_mask,
13197 "pll active mismatch (didn't expect pipe %c in active mask)\n",
13198 pipe_name(drm_crtc_index(crtc)));
13199 I915_STATE_WARN(pll->config.crtc_mask & crtc_mask,
13200 "pll enabled crtcs mismatch (found %x in enabled mask)\n",
13201 pipe_name(drm_crtc_index(crtc)));
5358901f 13202 }
8af6cf88
DV
13203}
13204
e7c84544 13205static void
c0ead703 13206intel_modeset_verify_crtc(struct drm_crtc *crtc,
e7c84544
ML
13207 struct drm_crtc_state *old_state,
13208 struct drm_crtc_state *new_state)
13209{
5a21b665
DV
13210 if (!needs_modeset(new_state) &&
13211 !to_intel_crtc_state(new_state)->update_pipe)
13212 return;
13213
c0ead703 13214 verify_wm_state(crtc, new_state);
5a21b665 13215 verify_connector_state(crtc->dev, crtc);
c0ead703
ML
13216 verify_crtc_state(crtc, old_state, new_state);
13217 verify_shared_dpll_state(crtc->dev, crtc, old_state, new_state);
e7c84544
ML
13218}
13219
13220static void
c0ead703 13221verify_disabled_dpll_state(struct drm_device *dev)
e7c84544
ML
13222{
13223 struct drm_i915_private *dev_priv = dev->dev_private;
13224 int i;
13225
13226 for (i = 0; i < dev_priv->num_shared_dpll; i++)
c0ead703 13227 verify_single_dpll_state(dev_priv, &dev_priv->shared_dplls[i], NULL, NULL);
e7c84544
ML
13228}
13229
13230static void
c0ead703 13231intel_modeset_verify_disabled(struct drm_device *dev)
e7c84544 13232{
c0ead703
ML
13233 verify_encoder_state(dev);
13234 verify_connector_state(dev, NULL);
13235 verify_disabled_dpll_state(dev);
e7c84544
ML
13236}
13237
80715b2f
VS
13238static void update_scanline_offset(struct intel_crtc *crtc)
13239{
13240 struct drm_device *dev = crtc->base.dev;
13241
13242 /*
13243 * The scanline counter increments at the leading edge of hsync.
13244 *
13245 * On most platforms it starts counting from vtotal-1 on the
13246 * first active line. That means the scanline counter value is
13247 * always one less than what we would expect. Ie. just after
13248 * start of vblank, which also occurs at start of hsync (on the
13249 * last active line), the scanline counter will read vblank_start-1.
13250 *
13251 * On gen2 the scanline counter starts counting from 1 instead
13252 * of vtotal-1, so we have to subtract one (or rather add vtotal-1
13253 * to keep the value positive), instead of adding one.
13254 *
13255 * On HSW+ the behaviour of the scanline counter depends on the output
13256 * type. For DP ports it behaves like most other platforms, but on HDMI
13257 * there's an extra 1 line difference. So we need to add two instead of
13258 * one to the value.
13259 */
13260 if (IS_GEN2(dev)) {
124abe07 13261 const struct drm_display_mode *adjusted_mode = &crtc->config->base.adjusted_mode;
80715b2f
VS
13262 int vtotal;
13263
124abe07
VS
13264 vtotal = adjusted_mode->crtc_vtotal;
13265 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
80715b2f
VS
13266 vtotal /= 2;
13267
13268 crtc->scanline_offset = vtotal - 1;
13269 } else if (HAS_DDI(dev) &&
409ee761 13270 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI)) {
80715b2f
VS
13271 crtc->scanline_offset = 2;
13272 } else
13273 crtc->scanline_offset = 1;
13274}
13275
ad421372 13276static void intel_modeset_clear_plls(struct drm_atomic_state *state)
ed6739ef 13277{
225da59b 13278 struct drm_device *dev = state->dev;
ed6739ef 13279 struct drm_i915_private *dev_priv = to_i915(dev);
ad421372 13280 struct intel_shared_dpll_config *shared_dpll = NULL;
0a9ab303
ACO
13281 struct drm_crtc *crtc;
13282 struct drm_crtc_state *crtc_state;
0a9ab303 13283 int i;
ed6739ef
ACO
13284
13285 if (!dev_priv->display.crtc_compute_clock)
ad421372 13286 return;
ed6739ef 13287
0a9ab303 13288 for_each_crtc_in_state(state, crtc, crtc_state, i) {
fb1a38a9 13289 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8106ddbd
ACO
13290 struct intel_shared_dpll *old_dpll =
13291 to_intel_crtc_state(crtc->state)->shared_dpll;
0a9ab303 13292
fb1a38a9 13293 if (!needs_modeset(crtc_state))
225da59b
ACO
13294 continue;
13295
8106ddbd 13296 to_intel_crtc_state(crtc_state)->shared_dpll = NULL;
fb1a38a9 13297
8106ddbd 13298 if (!old_dpll)
fb1a38a9 13299 continue;
0a9ab303 13300
ad421372
ML
13301 if (!shared_dpll)
13302 shared_dpll = intel_atomic_get_shared_dpll_state(state);
ed6739ef 13303
8106ddbd 13304 intel_shared_dpll_config_put(shared_dpll, old_dpll, intel_crtc);
ad421372 13305 }
ed6739ef
ACO
13306}
13307
99d736a2
ML
13308/*
13309 * This implements the workaround described in the "notes" section of the mode
13310 * set sequence documentation. When going from no pipes or single pipe to
13311 * multiple pipes, and planes are enabled after the pipe, we need to wait at
13312 * least 2 vblanks on the first pipe before enabling planes on the second pipe.
13313 */
13314static int haswell_mode_set_planes_workaround(struct drm_atomic_state *state)
13315{
13316 struct drm_crtc_state *crtc_state;
13317 struct intel_crtc *intel_crtc;
13318 struct drm_crtc *crtc;
13319 struct intel_crtc_state *first_crtc_state = NULL;
13320 struct intel_crtc_state *other_crtc_state = NULL;
13321 enum pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
13322 int i;
13323
13324 /* look at all crtc's that are going to be enabled in during modeset */
13325 for_each_crtc_in_state(state, crtc, crtc_state, i) {
13326 intel_crtc = to_intel_crtc(crtc);
13327
13328 if (!crtc_state->active || !needs_modeset(crtc_state))
13329 continue;
13330
13331 if (first_crtc_state) {
13332 other_crtc_state = to_intel_crtc_state(crtc_state);
13333 break;
13334 } else {
13335 first_crtc_state = to_intel_crtc_state(crtc_state);
13336 first_pipe = intel_crtc->pipe;
13337 }
13338 }
13339
13340 /* No workaround needed? */
13341 if (!first_crtc_state)
13342 return 0;
13343
13344 /* w/a possibly needed, check how many crtc's are already enabled. */
13345 for_each_intel_crtc(state->dev, intel_crtc) {
13346 struct intel_crtc_state *pipe_config;
13347
13348 pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
13349 if (IS_ERR(pipe_config))
13350 return PTR_ERR(pipe_config);
13351
13352 pipe_config->hsw_workaround_pipe = INVALID_PIPE;
13353
13354 if (!pipe_config->base.active ||
13355 needs_modeset(&pipe_config->base))
13356 continue;
13357
13358 /* 2 or more enabled crtcs means no need for w/a */
13359 if (enabled_pipe != INVALID_PIPE)
13360 return 0;
13361
13362 enabled_pipe = intel_crtc->pipe;
13363 }
13364
13365 if (enabled_pipe != INVALID_PIPE)
13366 first_crtc_state->hsw_workaround_pipe = enabled_pipe;
13367 else if (other_crtc_state)
13368 other_crtc_state->hsw_workaround_pipe = first_pipe;
13369
13370 return 0;
13371}
13372
27c329ed
ML
13373static int intel_modeset_all_pipes(struct drm_atomic_state *state)
13374{
13375 struct drm_crtc *crtc;
13376 struct drm_crtc_state *crtc_state;
13377 int ret = 0;
13378
13379 /* add all active pipes to the state */
13380 for_each_crtc(state->dev, crtc) {
13381 crtc_state = drm_atomic_get_crtc_state(state, crtc);
13382 if (IS_ERR(crtc_state))
13383 return PTR_ERR(crtc_state);
13384
13385 if (!crtc_state->active || needs_modeset(crtc_state))
13386 continue;
13387
13388 crtc_state->mode_changed = true;
13389
13390 ret = drm_atomic_add_affected_connectors(state, crtc);
13391 if (ret)
13392 break;
13393
13394 ret = drm_atomic_add_affected_planes(state, crtc);
13395 if (ret)
13396 break;
13397 }
13398
13399 return ret;
13400}
13401
c347a676 13402static int intel_modeset_checks(struct drm_atomic_state *state)
054518dd 13403{
565602d7
ML
13404 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
13405 struct drm_i915_private *dev_priv = state->dev->dev_private;
13406 struct drm_crtc *crtc;
13407 struct drm_crtc_state *crtc_state;
13408 int ret = 0, i;
054518dd 13409
b359283a
ML
13410 if (!check_digital_port_conflicts(state)) {
13411 DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
13412 return -EINVAL;
13413 }
13414
565602d7
ML
13415 intel_state->modeset = true;
13416 intel_state->active_crtcs = dev_priv->active_crtcs;
13417
13418 for_each_crtc_in_state(state, crtc, crtc_state, i) {
13419 if (crtc_state->active)
13420 intel_state->active_crtcs |= 1 << i;
13421 else
13422 intel_state->active_crtcs &= ~(1 << i);
8b4a7d05
MR
13423
13424 if (crtc_state->active != crtc->state->active)
13425 intel_state->active_pipe_changes |= drm_crtc_mask(crtc);
565602d7
ML
13426 }
13427
054518dd
ACO
13428 /*
13429 * See if the config requires any additional preparation, e.g.
13430 * to adjust global state with pipes off. We need to do this
13431 * here so we can get the modeset_pipe updated config for the new
13432 * mode set on this crtc. For other crtcs we need to use the
13433 * adjusted_mode bits in the crtc directly.
13434 */
27c329ed 13435 if (dev_priv->display.modeset_calc_cdclk) {
c89e39f3 13436 if (!intel_state->cdclk_pll_vco)
63911d72 13437 intel_state->cdclk_pll_vco = dev_priv->cdclk_pll.vco;
b2045352
VS
13438 if (!intel_state->cdclk_pll_vco)
13439 intel_state->cdclk_pll_vco = dev_priv->skl_preferred_vco_freq;
c89e39f3 13440
27c329ed 13441 ret = dev_priv->display.modeset_calc_cdclk(state);
c89e39f3
CT
13442 if (ret < 0)
13443 return ret;
27c329ed 13444
c89e39f3 13445 if (intel_state->dev_cdclk != dev_priv->cdclk_freq ||
63911d72 13446 intel_state->cdclk_pll_vco != dev_priv->cdclk_pll.vco)
27c329ed
ML
13447 ret = intel_modeset_all_pipes(state);
13448
13449 if (ret < 0)
054518dd 13450 return ret;
e8788cbc
ML
13451
13452 DRM_DEBUG_KMS("New cdclk calculated to be atomic %u, actual %u\n",
13453 intel_state->cdclk, intel_state->dev_cdclk);
27c329ed 13454 } else
1a617b77 13455 to_intel_atomic_state(state)->cdclk = dev_priv->atomic_cdclk_freq;
054518dd 13456
ad421372 13457 intel_modeset_clear_plls(state);
054518dd 13458
565602d7 13459 if (IS_HASWELL(dev_priv))
ad421372 13460 return haswell_mode_set_planes_workaround(state);
99d736a2 13461
ad421372 13462 return 0;
c347a676
ACO
13463}
13464
aa363136
MR
13465/*
13466 * Handle calculation of various watermark data at the end of the atomic check
13467 * phase. The code here should be run after the per-crtc and per-plane 'check'
13468 * handlers to ensure that all derived state has been updated.
13469 */
55994c2c 13470static int calc_watermark_data(struct drm_atomic_state *state)
aa363136
MR
13471{
13472 struct drm_device *dev = state->dev;
98d39494 13473 struct drm_i915_private *dev_priv = to_i915(dev);
98d39494
MR
13474
13475 /* Is there platform-specific watermark information to calculate? */
13476 if (dev_priv->display.compute_global_watermarks)
55994c2c
MR
13477 return dev_priv->display.compute_global_watermarks(state);
13478
13479 return 0;
aa363136
MR
13480}
13481
74c090b1
ML
13482/**
13483 * intel_atomic_check - validate state object
13484 * @dev: drm device
13485 * @state: state to validate
13486 */
13487static int intel_atomic_check(struct drm_device *dev,
13488 struct drm_atomic_state *state)
c347a676 13489{
dd8b3bdb 13490 struct drm_i915_private *dev_priv = to_i915(dev);
aa363136 13491 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
c347a676
ACO
13492 struct drm_crtc *crtc;
13493 struct drm_crtc_state *crtc_state;
13494 int ret, i;
61333b60 13495 bool any_ms = false;
c347a676 13496
74c090b1 13497 ret = drm_atomic_helper_check_modeset(dev, state);
054518dd
ACO
13498 if (ret)
13499 return ret;
13500
c347a676 13501 for_each_crtc_in_state(state, crtc, crtc_state, i) {
cfb23ed6
ML
13502 struct intel_crtc_state *pipe_config =
13503 to_intel_crtc_state(crtc_state);
1ed51de9
DV
13504
13505 /* Catch I915_MODE_FLAG_INHERITED */
13506 if (crtc_state->mode.private_flags != crtc->state->mode.private_flags)
13507 crtc_state->mode_changed = true;
cfb23ed6 13508
af4a879e 13509 if (!needs_modeset(crtc_state))
c347a676
ACO
13510 continue;
13511
af4a879e
DV
13512 if (!crtc_state->enable) {
13513 any_ms = true;
cfb23ed6 13514 continue;
af4a879e 13515 }
cfb23ed6 13516
26495481
DV
13517 /* FIXME: For only active_changed we shouldn't need to do any
13518 * state recomputation at all. */
13519
1ed51de9
DV
13520 ret = drm_atomic_add_affected_connectors(state, crtc);
13521 if (ret)
13522 return ret;
b359283a 13523
cfb23ed6 13524 ret = intel_modeset_pipe_config(crtc, pipe_config);
25aa1c39
ML
13525 if (ret) {
13526 intel_dump_pipe_config(to_intel_crtc(crtc),
13527 pipe_config, "[failed]");
c347a676 13528 return ret;
25aa1c39 13529 }
c347a676 13530
73831236 13531 if (i915.fastboot &&
dd8b3bdb 13532 intel_pipe_config_compare(dev,
cfb23ed6 13533 to_intel_crtc_state(crtc->state),
1ed51de9 13534 pipe_config, true)) {
26495481 13535 crtc_state->mode_changed = false;
bfd16b2a 13536 to_intel_crtc_state(crtc_state)->update_pipe = true;
26495481
DV
13537 }
13538
af4a879e 13539 if (needs_modeset(crtc_state))
26495481 13540 any_ms = true;
cfb23ed6 13541
af4a879e
DV
13542 ret = drm_atomic_add_affected_planes(state, crtc);
13543 if (ret)
13544 return ret;
61333b60 13545
26495481
DV
13546 intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
13547 needs_modeset(crtc_state) ?
13548 "[modeset]" : "[fastset]");
c347a676
ACO
13549 }
13550
61333b60
ML
13551 if (any_ms) {
13552 ret = intel_modeset_checks(state);
13553
13554 if (ret)
13555 return ret;
27c329ed 13556 } else
dd8b3bdb 13557 intel_state->cdclk = dev_priv->cdclk_freq;
76305b1a 13558
dd8b3bdb 13559 ret = drm_atomic_helper_check_planes(dev, state);
aa363136
MR
13560 if (ret)
13561 return ret;
13562
f51be2e0 13563 intel_fbc_choose_crtc(dev_priv, state);
55994c2c 13564 return calc_watermark_data(state);
054518dd
ACO
13565}
13566
5008e874
ML
13567static int intel_atomic_prepare_commit(struct drm_device *dev,
13568 struct drm_atomic_state *state,
81072bfd 13569 bool nonblock)
5008e874 13570{
7580d774
ML
13571 struct drm_i915_private *dev_priv = dev->dev_private;
13572 struct drm_plane_state *plane_state;
5008e874 13573 struct drm_crtc_state *crtc_state;
7580d774 13574 struct drm_plane *plane;
5008e874
ML
13575 struct drm_crtc *crtc;
13576 int i, ret;
13577
5a21b665
DV
13578 for_each_crtc_in_state(state, crtc, crtc_state, i) {
13579 if (state->legacy_cursor_update)
a6747b73
ML
13580 continue;
13581
5a21b665
DV
13582 ret = intel_crtc_wait_for_pending_flips(crtc);
13583 if (ret)
13584 return ret;
5008e874 13585
5a21b665
DV
13586 if (atomic_read(&to_intel_crtc(crtc)->unpin_work_count) >= 2)
13587 flush_workqueue(dev_priv->wq);
d55dbd06
ML
13588 }
13589
f935675f
ML
13590 ret = mutex_lock_interruptible(&dev->struct_mutex);
13591 if (ret)
13592 return ret;
13593
5008e874 13594 ret = drm_atomic_helper_prepare_planes(dev, state);
f7e5838b 13595 mutex_unlock(&dev->struct_mutex);
7580d774 13596
21daaeee 13597 if (!ret && !nonblock) {
7580d774
ML
13598 for_each_plane_in_state(state, plane, plane_state, i) {
13599 struct intel_plane_state *intel_plane_state =
13600 to_intel_plane_state(plane_state);
13601
13602 if (!intel_plane_state->wait_req)
13603 continue;
13604
13605 ret = __i915_wait_request(intel_plane_state->wait_req,
299259a3 13606 true, NULL, NULL);
f7e5838b 13607 if (ret) {
f4457ae7
CW
13608 /* Any hang should be swallowed by the wait */
13609 WARN_ON(ret == -EIO);
f7e5838b
CW
13610 mutex_lock(&dev->struct_mutex);
13611 drm_atomic_helper_cleanup_planes(dev, state);
13612 mutex_unlock(&dev->struct_mutex);
7580d774 13613 break;
f7e5838b 13614 }
7580d774 13615 }
7580d774 13616 }
5008e874
ML
13617
13618 return ret;
13619}
13620
a2991414
ML
13621u32 intel_crtc_get_vblank_counter(struct intel_crtc *crtc)
13622{
13623 struct drm_device *dev = crtc->base.dev;
13624
13625 if (!dev->max_vblank_count)
13626 return drm_accurate_vblank_count(&crtc->base);
13627
13628 return dev->driver->get_vblank_counter(dev, crtc->pipe);
13629}
13630
5a21b665
DV
13631static void intel_atomic_wait_for_vblanks(struct drm_device *dev,
13632 struct drm_i915_private *dev_priv,
13633 unsigned crtc_mask)
e8861675 13634{
5a21b665
DV
13635 unsigned last_vblank_count[I915_MAX_PIPES];
13636 enum pipe pipe;
13637 int ret;
e8861675 13638
5a21b665
DV
13639 if (!crtc_mask)
13640 return;
e8861675 13641
5a21b665
DV
13642 for_each_pipe(dev_priv, pipe) {
13643 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
e8861675 13644
5a21b665 13645 if (!((1 << pipe) & crtc_mask))
e8861675
ML
13646 continue;
13647
5a21b665
DV
13648 ret = drm_crtc_vblank_get(crtc);
13649 if (WARN_ON(ret != 0)) {
13650 crtc_mask &= ~(1 << pipe);
13651 continue;
e8861675
ML
13652 }
13653
5a21b665 13654 last_vblank_count[pipe] = drm_crtc_vblank_count(crtc);
e8861675
ML
13655 }
13656
5a21b665
DV
13657 for_each_pipe(dev_priv, pipe) {
13658 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
13659 long lret;
e8861675 13660
5a21b665
DV
13661 if (!((1 << pipe) & crtc_mask))
13662 continue;
d55dbd06 13663
5a21b665
DV
13664 lret = wait_event_timeout(dev->vblank[pipe].queue,
13665 last_vblank_count[pipe] !=
13666 drm_crtc_vblank_count(crtc),
13667 msecs_to_jiffies(50));
d55dbd06 13668
5a21b665 13669 WARN(!lret, "pipe %c vblank wait timed out\n", pipe_name(pipe));
d55dbd06 13670
5a21b665 13671 drm_crtc_vblank_put(crtc);
d55dbd06
ML
13672 }
13673}
13674
5a21b665 13675static bool needs_vblank_wait(struct intel_crtc_state *crtc_state)
a6747b73 13676{
5a21b665
DV
13677 /* fb updated, need to unpin old fb */
13678 if (crtc_state->fb_changed)
13679 return true;
a6747b73 13680
5a21b665
DV
13681 /* wm changes, need vblank before final wm's */
13682 if (crtc_state->update_wm_post)
13683 return true;
a6747b73 13684
5a21b665
DV
13685 /*
13686 * cxsr is re-enabled after vblank.
13687 * This is already handled by crtc_state->update_wm_post,
13688 * but added for clarity.
13689 */
13690 if (crtc_state->disable_cxsr)
13691 return true;
a6747b73 13692
5a21b665 13693 return false;
e8861675
ML
13694}
13695
94f05024 13696static void intel_atomic_commit_tail(struct drm_atomic_state *state)
a6778b3c 13697{
94f05024 13698 struct drm_device *dev = state->dev;
565602d7 13699 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
fbee40df 13700 struct drm_i915_private *dev_priv = dev->dev_private;
29ceb0e6 13701 struct drm_crtc_state *old_crtc_state;
7580d774 13702 struct drm_crtc *crtc;
5a21b665 13703 struct intel_crtc_state *intel_cstate;
94f05024
DV
13704 struct drm_plane *plane;
13705 struct drm_plane_state *plane_state;
5a21b665
DV
13706 bool hw_check = intel_state->modeset;
13707 unsigned long put_domains[I915_MAX_PIPES] = {};
13708 unsigned crtc_vblank_mask = 0;
94f05024 13709 int i, ret;
a6778b3c 13710
94f05024
DV
13711 for_each_plane_in_state(state, plane, plane_state, i) {
13712 struct intel_plane_state *intel_plane_state =
13713 to_intel_plane_state(plane_state);
ea0000f0 13714
94f05024
DV
13715 if (!intel_plane_state->wait_req)
13716 continue;
d4afb8cc 13717
94f05024
DV
13718 ret = __i915_wait_request(intel_plane_state->wait_req,
13719 true, NULL, NULL);
13720 /* EIO should be eaten, and we can't get interrupted in the
13721 * worker, and blocking commits have waited already. */
13722 WARN_ON(ret);
13723 }
1c5e19f8 13724
ea0000f0
DV
13725 drm_atomic_helper_wait_for_dependencies(state);
13726
565602d7
ML
13727 if (intel_state->modeset) {
13728 memcpy(dev_priv->min_pixclk, intel_state->min_pixclk,
13729 sizeof(intel_state->min_pixclk));
13730 dev_priv->active_crtcs = intel_state->active_crtcs;
1a617b77 13731 dev_priv->atomic_cdclk_freq = intel_state->cdclk;
5a21b665
DV
13732
13733 intel_display_power_get(dev_priv, POWER_DOMAIN_MODESET);
565602d7
ML
13734 }
13735
29ceb0e6 13736 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
a539205a
ML
13737 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13738
5a21b665
DV
13739 if (needs_modeset(crtc->state) ||
13740 to_intel_crtc_state(crtc->state)->update_pipe) {
13741 hw_check = true;
13742
13743 put_domains[to_intel_crtc(crtc)->pipe] =
13744 modeset_get_crtc_power_domains(crtc,
13745 to_intel_crtc_state(crtc->state));
13746 }
13747
61333b60
ML
13748 if (!needs_modeset(crtc->state))
13749 continue;
13750
29ceb0e6 13751 intel_pre_plane_update(to_intel_crtc_state(old_crtc_state));
460da916 13752
29ceb0e6
VS
13753 if (old_crtc_state->active) {
13754 intel_crtc_disable_planes(crtc, old_crtc_state->plane_mask);
a539205a 13755 dev_priv->display.crtc_disable(crtc);
eddfcbcd 13756 intel_crtc->active = false;
58f9c0bc 13757 intel_fbc_disable(intel_crtc);
eddfcbcd 13758 intel_disable_shared_dpll(intel_crtc);
9bbc8258
VS
13759
13760 /*
13761 * Underruns don't always raise
13762 * interrupts, so check manually.
13763 */
13764 intel_check_cpu_fifo_underruns(dev_priv);
13765 intel_check_pch_fifo_underruns(dev_priv);
b9001114
ML
13766
13767 if (!crtc->state->active)
13768 intel_update_watermarks(crtc);
a539205a 13769 }
b8cecdf5 13770 }
7758a113 13771
ea9d758d
DV
13772 /* Only after disabling all output pipelines that will be changed can we
13773 * update the the output configuration. */
4740b0f2 13774 intel_modeset_update_crtc_state(state);
f6e5b160 13775
565602d7 13776 if (intel_state->modeset) {
4740b0f2 13777 drm_atomic_helper_update_legacy_modeset_state(state->dev, state);
33c8df89
ML
13778
13779 if (dev_priv->display.modeset_commit_cdclk &&
c89e39f3 13780 (intel_state->dev_cdclk != dev_priv->cdclk_freq ||
63911d72 13781 intel_state->cdclk_pll_vco != dev_priv->cdclk_pll.vco))
33c8df89 13782 dev_priv->display.modeset_commit_cdclk(state);
f6d1973d 13783
c0ead703 13784 intel_modeset_verify_disabled(dev);
4740b0f2 13785 }
47fab737 13786
a6778b3c 13787 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
29ceb0e6 13788 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
f6ac4b2a
ML
13789 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13790 bool modeset = needs_modeset(crtc->state);
5a21b665
DV
13791 struct intel_crtc_state *pipe_config =
13792 to_intel_crtc_state(crtc->state);
9f836f90 13793
f6ac4b2a 13794 if (modeset && crtc->state->active) {
a539205a
ML
13795 update_scanline_offset(to_intel_crtc(crtc));
13796 dev_priv->display.crtc_enable(crtc);
13797 }
80715b2f 13798
1f7528c4
DV
13799 /* Complete events for now disable pipes here. */
13800 if (modeset && !crtc->state->active && crtc->state->event) {
13801 spin_lock_irq(&dev->event_lock);
13802 drm_crtc_send_vblank_event(crtc, crtc->state->event);
13803 spin_unlock_irq(&dev->event_lock);
13804
13805 crtc->state->event = NULL;
13806 }
13807
f6ac4b2a 13808 if (!modeset)
29ceb0e6 13809 intel_pre_plane_update(to_intel_crtc_state(old_crtc_state));
f6ac4b2a 13810
5a21b665
DV
13811 if (crtc->state->active &&
13812 drm_atomic_get_existing_plane_state(state, crtc->primary))
faf68d92 13813 intel_fbc_enable(intel_crtc, pipe_config, to_intel_plane_state(crtc->primary->state));
5a21b665 13814
1f7528c4 13815 if (crtc->state->active)
5a21b665 13816 drm_atomic_helper_commit_planes_on_crtc(old_crtc_state);
f6d1973d 13817
5a21b665
DV
13818 if (pipe_config->base.active && needs_vblank_wait(pipe_config))
13819 crtc_vblank_mask |= 1 << i;
177246a8
MR
13820 }
13821
94f05024
DV
13822 /* FIXME: We should call drm_atomic_helper_commit_hw_done() here
13823 * already, but still need the state for the delayed optimization. To
13824 * fix this:
13825 * - wrap the optimization/post_plane_update stuff into a per-crtc work.
13826 * - schedule that vblank worker _before_ calling hw_done
13827 * - at the start of commit_tail, cancel it _synchrously
13828 * - switch over to the vblank wait helper in the core after that since
13829 * we don't need out special handling any more.
13830 */
5a21b665
DV
13831 if (!state->legacy_cursor_update)
13832 intel_atomic_wait_for_vblanks(dev, dev_priv, crtc_vblank_mask);
13833
13834 /*
13835 * Now that the vblank has passed, we can go ahead and program the
13836 * optimal watermarks on platforms that need two-step watermark
13837 * programming.
13838 *
13839 * TODO: Move this (and other cleanup) to an async worker eventually.
13840 */
13841 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
13842 intel_cstate = to_intel_crtc_state(crtc->state);
13843
13844 if (dev_priv->display.optimize_watermarks)
13845 dev_priv->display.optimize_watermarks(intel_cstate);
13846 }
13847
13848 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
13849 intel_post_plane_update(to_intel_crtc_state(old_crtc_state));
13850
13851 if (put_domains[i])
13852 modeset_put_power_domains(dev_priv, put_domains[i]);
13853
13854 intel_modeset_verify_crtc(crtc, old_crtc_state, crtc->state);
13855 }
13856
94f05024
DV
13857 drm_atomic_helper_commit_hw_done(state);
13858
5a21b665
DV
13859 if (intel_state->modeset)
13860 intel_display_power_put(dev_priv, POWER_DOMAIN_MODESET);
13861
13862 mutex_lock(&dev->struct_mutex);
13863 drm_atomic_helper_cleanup_planes(dev, state);
13864 mutex_unlock(&dev->struct_mutex);
13865
ea0000f0
DV
13866 drm_atomic_helper_commit_cleanup_done(state);
13867
ee165b1a 13868 drm_atomic_state_free(state);
f30da187 13869
75714940
MK
13870 /* As one of the primary mmio accessors, KMS has a high likelihood
13871 * of triggering bugs in unclaimed access. After we finish
13872 * modesetting, see if an error has been flagged, and if so
13873 * enable debugging for the next modeset - and hope we catch
13874 * the culprit.
13875 *
13876 * XXX note that we assume display power is on at this point.
13877 * This might hold true now but we need to add pm helper to check
13878 * unclaimed only when the hardware is on, as atomic commits
13879 * can happen also when the device is completely off.
13880 */
13881 intel_uncore_arm_unclaimed_mmio_detection(dev_priv);
94f05024
DV
13882}
13883
13884static void intel_atomic_commit_work(struct work_struct *work)
13885{
13886 struct drm_atomic_state *state = container_of(work,
13887 struct drm_atomic_state,
13888 commit_work);
13889 intel_atomic_commit_tail(state);
13890}
13891
6c9c1b38
DV
13892static void intel_atomic_track_fbs(struct drm_atomic_state *state)
13893{
13894 struct drm_plane_state *old_plane_state;
13895 struct drm_plane *plane;
13896 struct drm_i915_gem_object *obj, *old_obj;
13897 struct intel_plane *intel_plane;
13898 int i;
13899
13900 mutex_lock(&state->dev->struct_mutex);
13901 for_each_plane_in_state(state, plane, old_plane_state, i) {
13902 obj = intel_fb_obj(plane->state->fb);
13903 old_obj = intel_fb_obj(old_plane_state->fb);
13904 intel_plane = to_intel_plane(plane);
13905
13906 i915_gem_track_fb(old_obj, obj, intel_plane->frontbuffer_bit);
13907 }
13908 mutex_unlock(&state->dev->struct_mutex);
13909}
13910
94f05024
DV
13911/**
13912 * intel_atomic_commit - commit validated state object
13913 * @dev: DRM device
13914 * @state: the top-level driver state object
13915 * @nonblock: nonblocking commit
13916 *
13917 * This function commits a top-level state object that has been validated
13918 * with drm_atomic_helper_check().
13919 *
13920 * FIXME: Atomic modeset support for i915 is not yet complete. At the moment
13921 * nonblocking commits are only safe for pure plane updates. Everything else
13922 * should work though.
13923 *
13924 * RETURNS
13925 * Zero for success or -errno.
13926 */
13927static int intel_atomic_commit(struct drm_device *dev,
13928 struct drm_atomic_state *state,
13929 bool nonblock)
13930{
13931 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
13932 struct drm_i915_private *dev_priv = dev->dev_private;
13933 int ret = 0;
13934
13935 if (intel_state->modeset && nonblock) {
13936 DRM_DEBUG_KMS("nonblocking commit for modeset not yet implemented.\n");
13937 return -EINVAL;
13938 }
13939
13940 ret = drm_atomic_helper_setup_commit(state, nonblock);
13941 if (ret)
13942 return ret;
13943
13944 INIT_WORK(&state->commit_work, intel_atomic_commit_work);
13945
13946 ret = intel_atomic_prepare_commit(dev, state, nonblock);
13947 if (ret) {
13948 DRM_DEBUG_ATOMIC("Preparing state failed with %i\n", ret);
13949 return ret;
13950 }
13951
13952 drm_atomic_helper_swap_state(state, true);
13953 dev_priv->wm.distrust_bios_wm = false;
13954 dev_priv->wm.skl_results = intel_state->wm_results;
13955 intel_shared_dpll_commit(state);
6c9c1b38 13956 intel_atomic_track_fbs(state);
94f05024
DV
13957
13958 if (nonblock)
13959 queue_work(system_unbound_wq, &state->commit_work);
13960 else
13961 intel_atomic_commit_tail(state);
75714940 13962
74c090b1 13963 return 0;
7f27126e
JB
13964}
13965
c0c36b94
CW
13966void intel_crtc_restore_mode(struct drm_crtc *crtc)
13967{
83a57153
ACO
13968 struct drm_device *dev = crtc->dev;
13969 struct drm_atomic_state *state;
e694eb02 13970 struct drm_crtc_state *crtc_state;
2bfb4627 13971 int ret;
83a57153
ACO
13972
13973 state = drm_atomic_state_alloc(dev);
13974 if (!state) {
78108b7c
VS
13975 DRM_DEBUG_KMS("[CRTC:%d:%s] crtc restore failed, out of memory",
13976 crtc->base.id, crtc->name);
83a57153
ACO
13977 return;
13978 }
13979
e694eb02 13980 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
83a57153 13981
e694eb02
ML
13982retry:
13983 crtc_state = drm_atomic_get_crtc_state(state, crtc);
13984 ret = PTR_ERR_OR_ZERO(crtc_state);
13985 if (!ret) {
13986 if (!crtc_state->active)
13987 goto out;
83a57153 13988
e694eb02 13989 crtc_state->mode_changed = true;
74c090b1 13990 ret = drm_atomic_commit(state);
83a57153
ACO
13991 }
13992
e694eb02
ML
13993 if (ret == -EDEADLK) {
13994 drm_atomic_state_clear(state);
13995 drm_modeset_backoff(state->acquire_ctx);
13996 goto retry;
4ed9fb37 13997 }
4be07317 13998
2bfb4627 13999 if (ret)
e694eb02 14000out:
2bfb4627 14001 drm_atomic_state_free(state);
c0c36b94
CW
14002}
14003
25c5b266
DV
14004#undef for_each_intel_crtc_masked
14005
f6e5b160 14006static const struct drm_crtc_funcs intel_crtc_funcs = {
82cf435b 14007 .gamma_set = drm_atomic_helper_legacy_gamma_set,
74c090b1 14008 .set_config = drm_atomic_helper_set_config,
82cf435b 14009 .set_property = drm_atomic_helper_crtc_set_property,
f6e5b160 14010 .destroy = intel_crtc_destroy,
527b6abe 14011 .page_flip = intel_crtc_page_flip,
1356837e
MR
14012 .atomic_duplicate_state = intel_crtc_duplicate_state,
14013 .atomic_destroy_state = intel_crtc_destroy_state,
f6e5b160
CW
14014};
14015
6beb8c23
MR
14016/**
14017 * intel_prepare_plane_fb - Prepare fb for usage on plane
14018 * @plane: drm plane to prepare for
14019 * @fb: framebuffer to prepare for presentation
14020 *
14021 * Prepares a framebuffer for usage on a display plane. Generally this
14022 * involves pinning the underlying object and updating the frontbuffer tracking
14023 * bits. Some older platforms need special physical address handling for
14024 * cursor planes.
14025 *
f935675f
ML
14026 * Must be called with struct_mutex held.
14027 *
6beb8c23
MR
14028 * Returns 0 on success, negative error code on failure.
14029 */
14030int
14031intel_prepare_plane_fb(struct drm_plane *plane,
d136dfee 14032 const struct drm_plane_state *new_state)
465c120c
MR
14033{
14034 struct drm_device *dev = plane->dev;
844f9111 14035 struct drm_framebuffer *fb = new_state->fb;
6beb8c23 14036 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
1ee49399 14037 struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->state->fb);
c37efb99 14038 struct reservation_object *resv;
6beb8c23 14039 int ret = 0;
465c120c 14040
1ee49399 14041 if (!obj && !old_obj)
465c120c
MR
14042 return 0;
14043
5008e874
ML
14044 if (old_obj) {
14045 struct drm_crtc_state *crtc_state =
14046 drm_atomic_get_existing_crtc_state(new_state->state, plane->state->crtc);
14047
14048 /* Big Hammer, we also need to ensure that any pending
14049 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
14050 * current scanout is retired before unpinning the old
14051 * framebuffer. Note that we rely on userspace rendering
14052 * into the buffer attached to the pipe they are waiting
14053 * on. If not, userspace generates a GPU hang with IPEHR
14054 * point to the MI_WAIT_FOR_EVENT.
14055 *
14056 * This should only fail upon a hung GPU, in which case we
14057 * can safely continue.
14058 */
14059 if (needs_modeset(crtc_state))
14060 ret = i915_gem_object_wait_rendering(old_obj, true);
f4457ae7
CW
14061 if (ret) {
14062 /* GPU hangs should have been swallowed by the wait */
14063 WARN_ON(ret == -EIO);
f935675f 14064 return ret;
f4457ae7 14065 }
5008e874
ML
14066 }
14067
c37efb99
CW
14068 if (!obj)
14069 return 0;
14070
5a21b665 14071 /* For framebuffer backed by dmabuf, wait for fence */
c37efb99
CW
14072 resv = i915_gem_object_get_dmabuf_resv(obj);
14073 if (resv) {
5a21b665
DV
14074 long lret;
14075
c37efb99 14076 lret = reservation_object_wait_timeout_rcu(resv, false, true,
5a21b665
DV
14077 MAX_SCHEDULE_TIMEOUT);
14078 if (lret == -ERESTARTSYS)
14079 return lret;
14080
14081 WARN(lret < 0, "waiting returns %li\n", lret);
14082 }
14083
c37efb99 14084 if (plane->type == DRM_PLANE_TYPE_CURSOR &&
6beb8c23
MR
14085 INTEL_INFO(dev)->cursor_needs_physical) {
14086 int align = IS_I830(dev) ? 16 * 1024 : 256;
14087 ret = i915_gem_object_attach_phys(obj, align);
14088 if (ret)
14089 DRM_DEBUG_KMS("failed to attach phys object\n");
14090 } else {
3465c580 14091 ret = intel_pin_and_fence_fb_obj(fb, new_state->rotation);
6beb8c23 14092 }
465c120c 14093
c37efb99 14094 if (ret == 0) {
6c9c1b38
DV
14095 struct intel_plane_state *plane_state =
14096 to_intel_plane_state(new_state);
7580d774 14097
6c9c1b38
DV
14098 i915_gem_request_assign(&plane_state->wait_req,
14099 obj->last_write_req);
7580d774 14100 }
fdd508a6 14101
6beb8c23
MR
14102 return ret;
14103}
14104
38f3ce3a
MR
14105/**
14106 * intel_cleanup_plane_fb - Cleans up an fb after plane use
14107 * @plane: drm plane to clean up for
14108 * @fb: old framebuffer that was on plane
14109 *
14110 * Cleans up a framebuffer that has just been removed from a plane.
f935675f
ML
14111 *
14112 * Must be called with struct_mutex held.
38f3ce3a
MR
14113 */
14114void
14115intel_cleanup_plane_fb(struct drm_plane *plane,
d136dfee 14116 const struct drm_plane_state *old_state)
38f3ce3a
MR
14117{
14118 struct drm_device *dev = plane->dev;
7580d774 14119 struct intel_plane_state *old_intel_state;
1ee49399
ML
14120 struct drm_i915_gem_object *old_obj = intel_fb_obj(old_state->fb);
14121 struct drm_i915_gem_object *obj = intel_fb_obj(plane->state->fb);
38f3ce3a 14122
7580d774
ML
14123 old_intel_state = to_intel_plane_state(old_state);
14124
1ee49399 14125 if (!obj && !old_obj)
38f3ce3a
MR
14126 return;
14127
1ee49399
ML
14128 if (old_obj && (plane->type != DRM_PLANE_TYPE_CURSOR ||
14129 !INTEL_INFO(dev)->cursor_needs_physical))
3465c580 14130 intel_unpin_fb_obj(old_state->fb, old_state->rotation);
1ee49399 14131
7580d774 14132 i915_gem_request_assign(&old_intel_state->wait_req, NULL);
465c120c
MR
14133}
14134
6156a456
CK
14135int
14136skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
14137{
14138 int max_scale;
14139 struct drm_device *dev;
14140 struct drm_i915_private *dev_priv;
14141 int crtc_clock, cdclk;
14142
bf8a0af0 14143 if (!intel_crtc || !crtc_state->base.enable)
6156a456
CK
14144 return DRM_PLANE_HELPER_NO_SCALING;
14145
14146 dev = intel_crtc->base.dev;
14147 dev_priv = dev->dev_private;
14148 crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
27c329ed 14149 cdclk = to_intel_atomic_state(crtc_state->base.state)->cdclk;
6156a456 14150
54bf1ce6 14151 if (WARN_ON_ONCE(!crtc_clock || cdclk < crtc_clock))
6156a456
CK
14152 return DRM_PLANE_HELPER_NO_SCALING;
14153
14154 /*
14155 * skl max scale is lower of:
14156 * close to 3 but not 3, -1 is for that purpose
14157 * or
14158 * cdclk/crtc_clock
14159 */
14160 max_scale = min((1 << 16) * 3 - 1, (1 << 8) * ((cdclk << 8) / crtc_clock));
14161
14162 return max_scale;
14163}
14164
465c120c 14165static int
3c692a41 14166intel_check_primary_plane(struct drm_plane *plane,
061e4b8d 14167 struct intel_crtc_state *crtc_state,
3c692a41
GP
14168 struct intel_plane_state *state)
14169{
2b875c22
MR
14170 struct drm_crtc *crtc = state->base.crtc;
14171 struct drm_framebuffer *fb = state->base.fb;
6156a456 14172 int min_scale = DRM_PLANE_HELPER_NO_SCALING;
061e4b8d
ML
14173 int max_scale = DRM_PLANE_HELPER_NO_SCALING;
14174 bool can_position = false;
465c120c 14175
693bdc28
VS
14176 if (INTEL_INFO(plane->dev)->gen >= 9) {
14177 /* use scaler when colorkey is not required */
14178 if (state->ckey.flags == I915_SET_COLORKEY_NONE) {
14179 min_scale = 1;
14180 max_scale = skl_max_scale(to_intel_crtc(crtc), crtc_state);
14181 }
d8106366 14182 can_position = true;
6156a456 14183 }
d8106366 14184
061e4b8d
ML
14185 return drm_plane_helper_check_update(plane, crtc, fb, &state->src,
14186 &state->dst, &state->clip,
9b8b013d 14187 state->base.rotation,
da20eabd
ML
14188 min_scale, max_scale,
14189 can_position, true,
14190 &state->visible);
14af293f
GP
14191}
14192
5a21b665
DV
14193static void intel_begin_crtc_commit(struct drm_crtc *crtc,
14194 struct drm_crtc_state *old_crtc_state)
14195{
14196 struct drm_device *dev = crtc->dev;
14197 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
14198 struct intel_crtc_state *old_intel_state =
14199 to_intel_crtc_state(old_crtc_state);
14200 bool modeset = needs_modeset(crtc->state);
14201
14202 /* Perform vblank evasion around commit operation */
14203 intel_pipe_update_start(intel_crtc);
14204
14205 if (modeset)
14206 return;
14207
14208 if (crtc->state->color_mgmt_changed || to_intel_crtc_state(crtc->state)->update_pipe) {
14209 intel_color_set_csc(crtc->state);
14210 intel_color_load_luts(crtc->state);
14211 }
14212
14213 if (to_intel_crtc_state(crtc->state)->update_pipe)
14214 intel_update_pipe_config(intel_crtc, old_intel_state);
14215 else if (INTEL_INFO(dev)->gen >= 9)
14216 skl_detach_scalers(intel_crtc);
14217}
14218
14219static void intel_finish_crtc_commit(struct drm_crtc *crtc,
14220 struct drm_crtc_state *old_crtc_state)
14221{
14222 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
14223
14224 intel_pipe_update_end(intel_crtc, NULL);
14225}
14226
cf4c7c12 14227/**
4a3b8769
MR
14228 * intel_plane_destroy - destroy a plane
14229 * @plane: plane to destroy
cf4c7c12 14230 *
4a3b8769
MR
14231 * Common destruction function for all types of planes (primary, cursor,
14232 * sprite).
cf4c7c12 14233 */
4a3b8769 14234void intel_plane_destroy(struct drm_plane *plane)
465c120c 14235{
69ae561f
VS
14236 if (!plane)
14237 return;
14238
465c120c 14239 drm_plane_cleanup(plane);
69ae561f 14240 kfree(to_intel_plane(plane));
465c120c
MR
14241}
14242
65a3fea0 14243const struct drm_plane_funcs intel_plane_funcs = {
70a101f8
MR
14244 .update_plane = drm_atomic_helper_update_plane,
14245 .disable_plane = drm_atomic_helper_disable_plane,
3d7d6510 14246 .destroy = intel_plane_destroy,
c196e1d6 14247 .set_property = drm_atomic_helper_plane_set_property,
a98b3431
MR
14248 .atomic_get_property = intel_plane_atomic_get_property,
14249 .atomic_set_property = intel_plane_atomic_set_property,
ea2c67bb
MR
14250 .atomic_duplicate_state = intel_plane_duplicate_state,
14251 .atomic_destroy_state = intel_plane_destroy_state,
14252
465c120c
MR
14253};
14254
14255static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
14256 int pipe)
14257{
fca0ce2a
VS
14258 struct intel_plane *primary = NULL;
14259 struct intel_plane_state *state = NULL;
465c120c 14260 const uint32_t *intel_primary_formats;
45e3743a 14261 unsigned int num_formats;
fca0ce2a 14262 int ret;
465c120c
MR
14263
14264 primary = kzalloc(sizeof(*primary), GFP_KERNEL);
fca0ce2a
VS
14265 if (!primary)
14266 goto fail;
465c120c 14267
8e7d688b 14268 state = intel_create_plane_state(&primary->base);
fca0ce2a
VS
14269 if (!state)
14270 goto fail;
8e7d688b 14271 primary->base.state = &state->base;
ea2c67bb 14272
465c120c
MR
14273 primary->can_scale = false;
14274 primary->max_downscale = 1;
6156a456
CK
14275 if (INTEL_INFO(dev)->gen >= 9) {
14276 primary->can_scale = true;
af99ceda 14277 state->scaler_id = -1;
6156a456 14278 }
465c120c
MR
14279 primary->pipe = pipe;
14280 primary->plane = pipe;
a9ff8714 14281 primary->frontbuffer_bit = INTEL_FRONTBUFFER_PRIMARY(pipe);
c59cb179 14282 primary->check_plane = intel_check_primary_plane;
465c120c
MR
14283 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
14284 primary->plane = !pipe;
14285
6c0fd451
DL
14286 if (INTEL_INFO(dev)->gen >= 9) {
14287 intel_primary_formats = skl_primary_formats;
14288 num_formats = ARRAY_SIZE(skl_primary_formats);
a8d201af
ML
14289
14290 primary->update_plane = skylake_update_primary_plane;
14291 primary->disable_plane = skylake_disable_primary_plane;
14292 } else if (HAS_PCH_SPLIT(dev)) {
14293 intel_primary_formats = i965_primary_formats;
14294 num_formats = ARRAY_SIZE(i965_primary_formats);
14295
14296 primary->update_plane = ironlake_update_primary_plane;
14297 primary->disable_plane = i9xx_disable_primary_plane;
6c0fd451 14298 } else if (INTEL_INFO(dev)->gen >= 4) {
568db4f2
DL
14299 intel_primary_formats = i965_primary_formats;
14300 num_formats = ARRAY_SIZE(i965_primary_formats);
a8d201af
ML
14301
14302 primary->update_plane = i9xx_update_primary_plane;
14303 primary->disable_plane = i9xx_disable_primary_plane;
6c0fd451
DL
14304 } else {
14305 intel_primary_formats = i8xx_primary_formats;
14306 num_formats = ARRAY_SIZE(i8xx_primary_formats);
a8d201af
ML
14307
14308 primary->update_plane = i9xx_update_primary_plane;
14309 primary->disable_plane = i9xx_disable_primary_plane;
465c120c
MR
14310 }
14311
38573dc1
VS
14312 if (INTEL_INFO(dev)->gen >= 9)
14313 ret = drm_universal_plane_init(dev, &primary->base, 0,
14314 &intel_plane_funcs,
14315 intel_primary_formats, num_formats,
14316 DRM_PLANE_TYPE_PRIMARY,
14317 "plane 1%c", pipe_name(pipe));
14318 else if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
14319 ret = drm_universal_plane_init(dev, &primary->base, 0,
14320 &intel_plane_funcs,
14321 intel_primary_formats, num_formats,
14322 DRM_PLANE_TYPE_PRIMARY,
14323 "primary %c", pipe_name(pipe));
14324 else
14325 ret = drm_universal_plane_init(dev, &primary->base, 0,
14326 &intel_plane_funcs,
14327 intel_primary_formats, num_formats,
14328 DRM_PLANE_TYPE_PRIMARY,
14329 "plane %c", plane_name(primary->plane));
fca0ce2a
VS
14330 if (ret)
14331 goto fail;
48404c1e 14332
3b7a5119
SJ
14333 if (INTEL_INFO(dev)->gen >= 4)
14334 intel_create_rotation_property(dev, primary);
48404c1e 14335
ea2c67bb
MR
14336 drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
14337
465c120c 14338 return &primary->base;
fca0ce2a
VS
14339
14340fail:
14341 kfree(state);
14342 kfree(primary);
14343
14344 return NULL;
465c120c
MR
14345}
14346
3b7a5119
SJ
14347void intel_create_rotation_property(struct drm_device *dev, struct intel_plane *plane)
14348{
14349 if (!dev->mode_config.rotation_property) {
14350 unsigned long flags = BIT(DRM_ROTATE_0) |
14351 BIT(DRM_ROTATE_180);
14352
14353 if (INTEL_INFO(dev)->gen >= 9)
14354 flags |= BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_270);
14355
14356 dev->mode_config.rotation_property =
14357 drm_mode_create_rotation_property(dev, flags);
14358 }
14359 if (dev->mode_config.rotation_property)
14360 drm_object_attach_property(&plane->base.base,
14361 dev->mode_config.rotation_property,
14362 plane->base.state->rotation);
14363}
14364
3d7d6510 14365static int
852e787c 14366intel_check_cursor_plane(struct drm_plane *plane,
061e4b8d 14367 struct intel_crtc_state *crtc_state,
852e787c 14368 struct intel_plane_state *state)
3d7d6510 14369{
061e4b8d 14370 struct drm_crtc *crtc = crtc_state->base.crtc;
2b875c22 14371 struct drm_framebuffer *fb = state->base.fb;
757f9a3e 14372 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
b29ec92c 14373 enum pipe pipe = to_intel_plane(plane)->pipe;
757f9a3e
GP
14374 unsigned stride;
14375 int ret;
3d7d6510 14376
061e4b8d
ML
14377 ret = drm_plane_helper_check_update(plane, crtc, fb, &state->src,
14378 &state->dst, &state->clip,
9b8b013d 14379 state->base.rotation,
3d7d6510
MR
14380 DRM_PLANE_HELPER_NO_SCALING,
14381 DRM_PLANE_HELPER_NO_SCALING,
852e787c 14382 true, true, &state->visible);
757f9a3e
GP
14383 if (ret)
14384 return ret;
14385
757f9a3e
GP
14386 /* if we want to turn off the cursor ignore width and height */
14387 if (!obj)
da20eabd 14388 return 0;
757f9a3e 14389
757f9a3e 14390 /* Check for which cursor types we support */
061e4b8d 14391 if (!cursor_size_ok(plane->dev, state->base.crtc_w, state->base.crtc_h)) {
ea2c67bb
MR
14392 DRM_DEBUG("Cursor dimension %dx%d not supported\n",
14393 state->base.crtc_w, state->base.crtc_h);
757f9a3e
GP
14394 return -EINVAL;
14395 }
14396
ea2c67bb
MR
14397 stride = roundup_pow_of_two(state->base.crtc_w) * 4;
14398 if (obj->base.size < stride * state->base.crtc_h) {
757f9a3e
GP
14399 DRM_DEBUG_KMS("buffer is too small\n");
14400 return -ENOMEM;
14401 }
14402
3a656b54 14403 if (fb->modifier[0] != DRM_FORMAT_MOD_NONE) {
757f9a3e 14404 DRM_DEBUG_KMS("cursor cannot be tiled\n");
da20eabd 14405 return -EINVAL;
32b7eeec
MR
14406 }
14407
b29ec92c
VS
14408 /*
14409 * There's something wrong with the cursor on CHV pipe C.
14410 * If it straddles the left edge of the screen then
14411 * moving it away from the edge or disabling it often
14412 * results in a pipe underrun, and often that can lead to
14413 * dead pipe (constant underrun reported, and it scans
14414 * out just a solid color). To recover from that, the
14415 * display power well must be turned off and on again.
14416 * Refuse the put the cursor into that compromised position.
14417 */
14418 if (IS_CHERRYVIEW(plane->dev) && pipe == PIPE_C &&
14419 state->visible && state->base.crtc_x < 0) {
14420 DRM_DEBUG_KMS("CHV cursor C not allowed to straddle the left screen edge\n");
14421 return -EINVAL;
14422 }
14423
da20eabd 14424 return 0;
852e787c 14425}
3d7d6510 14426
a8ad0d8e
ML
14427static void
14428intel_disable_cursor_plane(struct drm_plane *plane,
7fabf5ef 14429 struct drm_crtc *crtc)
a8ad0d8e 14430{
f2858021
ML
14431 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
14432
14433 intel_crtc->cursor_addr = 0;
55a08b3f 14434 intel_crtc_update_cursor(crtc, NULL);
a8ad0d8e
ML
14435}
14436
f4a2cf29 14437static void
55a08b3f
ML
14438intel_update_cursor_plane(struct drm_plane *plane,
14439 const struct intel_crtc_state *crtc_state,
14440 const struct intel_plane_state *state)
852e787c 14441{
55a08b3f
ML
14442 struct drm_crtc *crtc = crtc_state->base.crtc;
14443 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ea2c67bb 14444 struct drm_device *dev = plane->dev;
2b875c22 14445 struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
a912f12f 14446 uint32_t addr;
852e787c 14447
f4a2cf29 14448 if (!obj)
a912f12f 14449 addr = 0;
f4a2cf29 14450 else if (!INTEL_INFO(dev)->cursor_needs_physical)
a912f12f 14451 addr = i915_gem_obj_ggtt_offset(obj);
f4a2cf29 14452 else
a912f12f 14453 addr = obj->phys_handle->busaddr;
852e787c 14454
a912f12f 14455 intel_crtc->cursor_addr = addr;
55a08b3f 14456 intel_crtc_update_cursor(crtc, state);
852e787c
GP
14457}
14458
3d7d6510
MR
14459static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
14460 int pipe)
14461{
fca0ce2a
VS
14462 struct intel_plane *cursor = NULL;
14463 struct intel_plane_state *state = NULL;
14464 int ret;
3d7d6510
MR
14465
14466 cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
fca0ce2a
VS
14467 if (!cursor)
14468 goto fail;
3d7d6510 14469
8e7d688b 14470 state = intel_create_plane_state(&cursor->base);
fca0ce2a
VS
14471 if (!state)
14472 goto fail;
8e7d688b 14473 cursor->base.state = &state->base;
ea2c67bb 14474
3d7d6510
MR
14475 cursor->can_scale = false;
14476 cursor->max_downscale = 1;
14477 cursor->pipe = pipe;
14478 cursor->plane = pipe;
a9ff8714 14479 cursor->frontbuffer_bit = INTEL_FRONTBUFFER_CURSOR(pipe);
c59cb179 14480 cursor->check_plane = intel_check_cursor_plane;
55a08b3f 14481 cursor->update_plane = intel_update_cursor_plane;
a8ad0d8e 14482 cursor->disable_plane = intel_disable_cursor_plane;
3d7d6510 14483
fca0ce2a
VS
14484 ret = drm_universal_plane_init(dev, &cursor->base, 0,
14485 &intel_plane_funcs,
14486 intel_cursor_formats,
14487 ARRAY_SIZE(intel_cursor_formats),
38573dc1
VS
14488 DRM_PLANE_TYPE_CURSOR,
14489 "cursor %c", pipe_name(pipe));
fca0ce2a
VS
14490 if (ret)
14491 goto fail;
4398ad45
VS
14492
14493 if (INTEL_INFO(dev)->gen >= 4) {
14494 if (!dev->mode_config.rotation_property)
14495 dev->mode_config.rotation_property =
14496 drm_mode_create_rotation_property(dev,
14497 BIT(DRM_ROTATE_0) |
14498 BIT(DRM_ROTATE_180));
14499 if (dev->mode_config.rotation_property)
14500 drm_object_attach_property(&cursor->base.base,
14501 dev->mode_config.rotation_property,
8e7d688b 14502 state->base.rotation);
4398ad45
VS
14503 }
14504
af99ceda
CK
14505 if (INTEL_INFO(dev)->gen >=9)
14506 state->scaler_id = -1;
14507
ea2c67bb
MR
14508 drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
14509
3d7d6510 14510 return &cursor->base;
fca0ce2a
VS
14511
14512fail:
14513 kfree(state);
14514 kfree(cursor);
14515
14516 return NULL;
3d7d6510
MR
14517}
14518
549e2bfb
CK
14519static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
14520 struct intel_crtc_state *crtc_state)
14521{
14522 int i;
14523 struct intel_scaler *intel_scaler;
14524 struct intel_crtc_scaler_state *scaler_state = &crtc_state->scaler_state;
14525
14526 for (i = 0; i < intel_crtc->num_scalers; i++) {
14527 intel_scaler = &scaler_state->scalers[i];
14528 intel_scaler->in_use = 0;
549e2bfb
CK
14529 intel_scaler->mode = PS_SCALER_MODE_DYN;
14530 }
14531
14532 scaler_state->scaler_id = -1;
14533}
14534
b358d0a6 14535static void intel_crtc_init(struct drm_device *dev, int pipe)
79e53945 14536{
fbee40df 14537 struct drm_i915_private *dev_priv = dev->dev_private;
79e53945 14538 struct intel_crtc *intel_crtc;
f5de6e07 14539 struct intel_crtc_state *crtc_state = NULL;
3d7d6510
MR
14540 struct drm_plane *primary = NULL;
14541 struct drm_plane *cursor = NULL;
8563b1e8 14542 int ret;
79e53945 14543
955382f3 14544 intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
79e53945
JB
14545 if (intel_crtc == NULL)
14546 return;
14547
f5de6e07
ACO
14548 crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
14549 if (!crtc_state)
14550 goto fail;
550acefd
ACO
14551 intel_crtc->config = crtc_state;
14552 intel_crtc->base.state = &crtc_state->base;
07878248 14553 crtc_state->base.crtc = &intel_crtc->base;
f5de6e07 14554
549e2bfb
CK
14555 /* initialize shared scalers */
14556 if (INTEL_INFO(dev)->gen >= 9) {
14557 if (pipe == PIPE_C)
14558 intel_crtc->num_scalers = 1;
14559 else
14560 intel_crtc->num_scalers = SKL_NUM_SCALERS;
14561
14562 skl_init_scalers(dev, intel_crtc, crtc_state);
14563 }
14564
465c120c 14565 primary = intel_primary_plane_create(dev, pipe);
3d7d6510
MR
14566 if (!primary)
14567 goto fail;
14568
14569 cursor = intel_cursor_plane_create(dev, pipe);
14570 if (!cursor)
14571 goto fail;
14572
465c120c 14573 ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
4d5d72b7
VS
14574 cursor, &intel_crtc_funcs,
14575 "pipe %c", pipe_name(pipe));
3d7d6510
MR
14576 if (ret)
14577 goto fail;
79e53945 14578
1f1c2e24
VS
14579 /*
14580 * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
8c0f92e1 14581 * is hooked to pipe B. Hence we want plane A feeding pipe B.
1f1c2e24 14582 */
80824003
JB
14583 intel_crtc->pipe = pipe;
14584 intel_crtc->plane = pipe;
3a77c4c4 14585 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
28c97730 14586 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
e2e767ab 14587 intel_crtc->plane = !pipe;
80824003
JB
14588 }
14589
4b0e333e
CW
14590 intel_crtc->cursor_base = ~0;
14591 intel_crtc->cursor_cntl = ~0;
dc41c154 14592 intel_crtc->cursor_size = ~0;
8d7849db 14593
852eb00d
VS
14594 intel_crtc->wm.cxsr_allowed = true;
14595
22fd0fab
JB
14596 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
14597 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
14598 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
14599 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
14600
79e53945 14601 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
87b6b101 14602
8563b1e8
LL
14603 intel_color_init(&intel_crtc->base);
14604
87b6b101 14605 WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
3d7d6510
MR
14606 return;
14607
14608fail:
69ae561f
VS
14609 intel_plane_destroy(primary);
14610 intel_plane_destroy(cursor);
f5de6e07 14611 kfree(crtc_state);
3d7d6510 14612 kfree(intel_crtc);
79e53945
JB
14613}
14614
752aa88a
JB
14615enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
14616{
14617 struct drm_encoder *encoder = connector->base.encoder;
6e9f798d 14618 struct drm_device *dev = connector->base.dev;
752aa88a 14619
51fd371b 14620 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
752aa88a 14621
d3babd3f 14622 if (!encoder || WARN_ON(!encoder->crtc))
752aa88a
JB
14623 return INVALID_PIPE;
14624
14625 return to_intel_crtc(encoder->crtc)->pipe;
14626}
14627
08d7b3d1 14628int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
05394f39 14629 struct drm_file *file)
08d7b3d1 14630{
08d7b3d1 14631 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
7707e653 14632 struct drm_crtc *drmmode_crtc;
c05422d5 14633 struct intel_crtc *crtc;
08d7b3d1 14634
7707e653 14635 drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
71240ed2 14636 if (!drmmode_crtc)
3f2c2057 14637 return -ENOENT;
08d7b3d1 14638
7707e653 14639 crtc = to_intel_crtc(drmmode_crtc);
c05422d5 14640 pipe_from_crtc_id->pipe = crtc->pipe;
08d7b3d1 14641
c05422d5 14642 return 0;
08d7b3d1
CW
14643}
14644
66a9278e 14645static int intel_encoder_clones(struct intel_encoder *encoder)
79e53945 14646{
66a9278e
DV
14647 struct drm_device *dev = encoder->base.dev;
14648 struct intel_encoder *source_encoder;
79e53945 14649 int index_mask = 0;
79e53945
JB
14650 int entry = 0;
14651
b2784e15 14652 for_each_intel_encoder(dev, source_encoder) {
bc079e8b 14653 if (encoders_cloneable(encoder, source_encoder))
66a9278e
DV
14654 index_mask |= (1 << entry);
14655
79e53945
JB
14656 entry++;
14657 }
4ef69c7a 14658
79e53945
JB
14659 return index_mask;
14660}
14661
4d302442
CW
14662static bool has_edp_a(struct drm_device *dev)
14663{
14664 struct drm_i915_private *dev_priv = dev->dev_private;
14665
14666 if (!IS_MOBILE(dev))
14667 return false;
14668
14669 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
14670 return false;
14671
e3589908 14672 if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
4d302442
CW
14673 return false;
14674
14675 return true;
14676}
14677
84b4e042
JB
14678static bool intel_crt_present(struct drm_device *dev)
14679{
14680 struct drm_i915_private *dev_priv = dev->dev_private;
14681
884497ed
DL
14682 if (INTEL_INFO(dev)->gen >= 9)
14683 return false;
14684
cf404ce4 14685 if (IS_HSW_ULT(dev) || IS_BDW_ULT(dev))
84b4e042
JB
14686 return false;
14687
14688 if (IS_CHERRYVIEW(dev))
14689 return false;
14690
65e472e4
VS
14691 if (HAS_PCH_LPT_H(dev) && I915_READ(SFUSE_STRAP) & SFUSE_STRAP_CRT_DISABLED)
14692 return false;
14693
70ac54d0
VS
14694 /* DDI E can't be used if DDI A requires 4 lanes */
14695 if (HAS_DDI(dev) && I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_A_4_LANES)
14696 return false;
14697
e4abb733 14698 if (!dev_priv->vbt.int_crt_support)
84b4e042
JB
14699 return false;
14700
14701 return true;
14702}
14703
79e53945
JB
14704static void intel_setup_outputs(struct drm_device *dev)
14705{
725e30ad 14706 struct drm_i915_private *dev_priv = dev->dev_private;
4ef69c7a 14707 struct intel_encoder *encoder;
cb0953d7 14708 bool dpd_is_edp = false;
79e53945 14709
97a824e1
ID
14710 /*
14711 * intel_edp_init_connector() depends on this completing first, to
14712 * prevent the registeration of both eDP and LVDS and the incorrect
14713 * sharing of the PPS.
14714 */
c9093354 14715 intel_lvds_init(dev);
79e53945 14716
84b4e042 14717 if (intel_crt_present(dev))
79935fca 14718 intel_crt_init(dev);
cb0953d7 14719
c776eb2e
VK
14720 if (IS_BROXTON(dev)) {
14721 /*
14722 * FIXME: Broxton doesn't support port detection via the
14723 * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
14724 * detect the ports.
14725 */
14726 intel_ddi_init(dev, PORT_A);
14727 intel_ddi_init(dev, PORT_B);
14728 intel_ddi_init(dev, PORT_C);
c6c794a2
SS
14729
14730 intel_dsi_init(dev);
c776eb2e 14731 } else if (HAS_DDI(dev)) {
0e72a5b5
ED
14732 int found;
14733
de31facd
JB
14734 /*
14735 * Haswell uses DDI functions to detect digital outputs.
14736 * On SKL pre-D0 the strap isn't connected, so we assume
14737 * it's there.
14738 */
77179400 14739 found = I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_INIT_DISPLAY_DETECTED;
de31facd 14740 /* WaIgnoreDDIAStrap: skl */
ef11bdb3 14741 if (found || IS_SKYLAKE(dev) || IS_KABYLAKE(dev))
0e72a5b5
ED
14742 intel_ddi_init(dev, PORT_A);
14743
14744 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
14745 * register */
14746 found = I915_READ(SFUSE_STRAP);
14747
14748 if (found & SFUSE_STRAP_DDIB_DETECTED)
14749 intel_ddi_init(dev, PORT_B);
14750 if (found & SFUSE_STRAP_DDIC_DETECTED)
14751 intel_ddi_init(dev, PORT_C);
14752 if (found & SFUSE_STRAP_DDID_DETECTED)
14753 intel_ddi_init(dev, PORT_D);
2800e4c2
RV
14754 /*
14755 * On SKL we don't have a way to detect DDI-E so we rely on VBT.
14756 */
ef11bdb3 14757 if ((IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) &&
2800e4c2
RV
14758 (dev_priv->vbt.ddi_port_info[PORT_E].supports_dp ||
14759 dev_priv->vbt.ddi_port_info[PORT_E].supports_dvi ||
14760 dev_priv->vbt.ddi_port_info[PORT_E].supports_hdmi))
14761 intel_ddi_init(dev, PORT_E);
14762
0e72a5b5 14763 } else if (HAS_PCH_SPLIT(dev)) {
cb0953d7 14764 int found;
5d8a7752 14765 dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
270b3042
DV
14766
14767 if (has_edp_a(dev))
14768 intel_dp_init(dev, DP_A, PORT_A);
cb0953d7 14769
dc0fa718 14770 if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
461ed3ca 14771 /* PCH SDVOB multiplex with HDMIB */
2a5c0832 14772 found = intel_sdvo_init(dev, PCH_SDVOB, PORT_B);
30ad48b7 14773 if (!found)
e2debe91 14774 intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
5eb08b69 14775 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
ab9d7c30 14776 intel_dp_init(dev, PCH_DP_B, PORT_B);
30ad48b7
ZW
14777 }
14778
dc0fa718 14779 if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
e2debe91 14780 intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
30ad48b7 14781
dc0fa718 14782 if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
e2debe91 14783 intel_hdmi_init(dev, PCH_HDMID, PORT_D);
30ad48b7 14784
5eb08b69 14785 if (I915_READ(PCH_DP_C) & DP_DETECTED)
ab9d7c30 14786 intel_dp_init(dev, PCH_DP_C, PORT_C);
5eb08b69 14787
270b3042 14788 if (I915_READ(PCH_DP_D) & DP_DETECTED)
ab9d7c30 14789 intel_dp_init(dev, PCH_DP_D, PORT_D);
666a4537 14790 } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
22f35042 14791 bool has_edp, has_port;
457c52d8 14792
e17ac6db
VS
14793 /*
14794 * The DP_DETECTED bit is the latched state of the DDC
14795 * SDA pin at boot. However since eDP doesn't require DDC
14796 * (no way to plug in a DP->HDMI dongle) the DDC pins for
14797 * eDP ports may have been muxed to an alternate function.
14798 * Thus we can't rely on the DP_DETECTED bit alone to detect
14799 * eDP ports. Consult the VBT as well as DP_DETECTED to
14800 * detect eDP ports.
22f35042
VS
14801 *
14802 * Sadly the straps seem to be missing sometimes even for HDMI
14803 * ports (eg. on Voyo V3 - CHT x7-Z8700), so check both strap
14804 * and VBT for the presence of the port. Additionally we can't
14805 * trust the port type the VBT declares as we've seen at least
14806 * HDMI ports that the VBT claim are DP or eDP.
e17ac6db 14807 */
457c52d8 14808 has_edp = intel_dp_is_edp(dev, PORT_B);
22f35042
VS
14809 has_port = intel_bios_is_port_present(dev_priv, PORT_B);
14810 if (I915_READ(VLV_DP_B) & DP_DETECTED || has_port)
457c52d8 14811 has_edp &= intel_dp_init(dev, VLV_DP_B, PORT_B);
22f35042 14812 if ((I915_READ(VLV_HDMIB) & SDVO_DETECTED || has_port) && !has_edp)
e66eb81d 14813 intel_hdmi_init(dev, VLV_HDMIB, PORT_B);
585a94b8 14814
457c52d8 14815 has_edp = intel_dp_is_edp(dev, PORT_C);
22f35042
VS
14816 has_port = intel_bios_is_port_present(dev_priv, PORT_C);
14817 if (I915_READ(VLV_DP_C) & DP_DETECTED || has_port)
457c52d8 14818 has_edp &= intel_dp_init(dev, VLV_DP_C, PORT_C);
22f35042 14819 if ((I915_READ(VLV_HDMIC) & SDVO_DETECTED || has_port) && !has_edp)
e66eb81d 14820 intel_hdmi_init(dev, VLV_HDMIC, PORT_C);
19c03924 14821
9418c1f1 14822 if (IS_CHERRYVIEW(dev)) {
22f35042
VS
14823 /*
14824 * eDP not supported on port D,
14825 * so no need to worry about it
14826 */
14827 has_port = intel_bios_is_port_present(dev_priv, PORT_D);
14828 if (I915_READ(CHV_DP_D) & DP_DETECTED || has_port)
e66eb81d 14829 intel_dp_init(dev, CHV_DP_D, PORT_D);
22f35042
VS
14830 if (I915_READ(CHV_HDMID) & SDVO_DETECTED || has_port)
14831 intel_hdmi_init(dev, CHV_HDMID, PORT_D);
9418c1f1
VS
14832 }
14833
3cfca973 14834 intel_dsi_init(dev);
09da55dc 14835 } else if (!IS_GEN2(dev) && !IS_PINEVIEW(dev)) {
27185ae1 14836 bool found = false;
7d57382e 14837
e2debe91 14838 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
b01f2c3a 14839 DRM_DEBUG_KMS("probing SDVOB\n");
2a5c0832 14840 found = intel_sdvo_init(dev, GEN3_SDVOB, PORT_B);
3fec3d2f 14841 if (!found && IS_G4X(dev)) {
b01f2c3a 14842 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
e2debe91 14843 intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
b01f2c3a 14844 }
27185ae1 14845
3fec3d2f 14846 if (!found && IS_G4X(dev))
ab9d7c30 14847 intel_dp_init(dev, DP_B, PORT_B);
725e30ad 14848 }
13520b05
KH
14849
14850 /* Before G4X SDVOC doesn't have its own detect register */
13520b05 14851
e2debe91 14852 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
b01f2c3a 14853 DRM_DEBUG_KMS("probing SDVOC\n");
2a5c0832 14854 found = intel_sdvo_init(dev, GEN3_SDVOC, PORT_C);
b01f2c3a 14855 }
27185ae1 14856
e2debe91 14857 if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
27185ae1 14858
3fec3d2f 14859 if (IS_G4X(dev)) {
b01f2c3a 14860 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
e2debe91 14861 intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
b01f2c3a 14862 }
3fec3d2f 14863 if (IS_G4X(dev))
ab9d7c30 14864 intel_dp_init(dev, DP_C, PORT_C);
725e30ad 14865 }
27185ae1 14866
3fec3d2f 14867 if (IS_G4X(dev) &&
e7281eab 14868 (I915_READ(DP_D) & DP_DETECTED))
ab9d7c30 14869 intel_dp_init(dev, DP_D, PORT_D);
bad720ff 14870 } else if (IS_GEN2(dev))
79e53945
JB
14871 intel_dvo_init(dev);
14872
103a196f 14873 if (SUPPORTS_TV(dev))
79e53945
JB
14874 intel_tv_init(dev);
14875
0bc12bcb 14876 intel_psr_init(dev);
7c8f8a70 14877
b2784e15 14878 for_each_intel_encoder(dev, encoder) {
4ef69c7a
CW
14879 encoder->base.possible_crtcs = encoder->crtc_mask;
14880 encoder->base.possible_clones =
66a9278e 14881 intel_encoder_clones(encoder);
79e53945 14882 }
47356eb6 14883
dde86e2d 14884 intel_init_pch_refclk(dev);
270b3042
DV
14885
14886 drm_helper_move_panel_connectors_to_head(dev);
79e53945
JB
14887}
14888
14889static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
14890{
60a5ca01 14891 struct drm_device *dev = fb->dev;
79e53945 14892 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
79e53945 14893
ef2d633e 14894 drm_framebuffer_cleanup(fb);
60a5ca01 14895 mutex_lock(&dev->struct_mutex);
ef2d633e 14896 WARN_ON(!intel_fb->obj->framebuffer_references--);
60a5ca01
VS
14897 drm_gem_object_unreference(&intel_fb->obj->base);
14898 mutex_unlock(&dev->struct_mutex);
79e53945
JB
14899 kfree(intel_fb);
14900}
14901
14902static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
05394f39 14903 struct drm_file *file,
79e53945
JB
14904 unsigned int *handle)
14905{
14906 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
05394f39 14907 struct drm_i915_gem_object *obj = intel_fb->obj;
79e53945 14908
cc917ab4
CW
14909 if (obj->userptr.mm) {
14910 DRM_DEBUG("attempting to use a userptr for a framebuffer, denied\n");
14911 return -EINVAL;
14912 }
14913
05394f39 14914 return drm_gem_handle_create(file, &obj->base, handle);
79e53945
JB
14915}
14916
86c98588
RV
14917static int intel_user_framebuffer_dirty(struct drm_framebuffer *fb,
14918 struct drm_file *file,
14919 unsigned flags, unsigned color,
14920 struct drm_clip_rect *clips,
14921 unsigned num_clips)
14922{
14923 struct drm_device *dev = fb->dev;
14924 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
14925 struct drm_i915_gem_object *obj = intel_fb->obj;
14926
14927 mutex_lock(&dev->struct_mutex);
74b4ea1e 14928 intel_fb_obj_flush(obj, false, ORIGIN_DIRTYFB);
86c98588
RV
14929 mutex_unlock(&dev->struct_mutex);
14930
14931 return 0;
14932}
14933
79e53945
JB
14934static const struct drm_framebuffer_funcs intel_fb_funcs = {
14935 .destroy = intel_user_framebuffer_destroy,
14936 .create_handle = intel_user_framebuffer_create_handle,
86c98588 14937 .dirty = intel_user_framebuffer_dirty,
79e53945
JB
14938};
14939
b321803d
DL
14940static
14941u32 intel_fb_pitch_limit(struct drm_device *dev, uint64_t fb_modifier,
14942 uint32_t pixel_format)
14943{
14944 u32 gen = INTEL_INFO(dev)->gen;
14945
14946 if (gen >= 9) {
ac484963
VS
14947 int cpp = drm_format_plane_cpp(pixel_format, 0);
14948
b321803d
DL
14949 /* "The stride in bytes must not exceed the of the size of 8K
14950 * pixels and 32K bytes."
14951 */
ac484963 14952 return min(8192 * cpp, 32768);
666a4537 14953 } else if (gen >= 5 && !IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
b321803d
DL
14954 return 32*1024;
14955 } else if (gen >= 4) {
14956 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14957 return 16*1024;
14958 else
14959 return 32*1024;
14960 } else if (gen >= 3) {
14961 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14962 return 8*1024;
14963 else
14964 return 16*1024;
14965 } else {
14966 /* XXX DSPC is limited to 4k tiled */
14967 return 8*1024;
14968 }
14969}
14970
b5ea642a
DV
14971static int intel_framebuffer_init(struct drm_device *dev,
14972 struct intel_framebuffer *intel_fb,
14973 struct drm_mode_fb_cmd2 *mode_cmd,
14974 struct drm_i915_gem_object *obj)
79e53945 14975{
7b49f948 14976 struct drm_i915_private *dev_priv = to_i915(dev);
6761dd31 14977 unsigned int aligned_height;
79e53945 14978 int ret;
b321803d 14979 u32 pitch_limit, stride_alignment;
79e53945 14980
dd4916c5
DV
14981 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
14982
2a80eada
DV
14983 if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
14984 /* Enforce that fb modifier and tiling mode match, but only for
14985 * X-tiled. This is needed for FBC. */
14986 if (!!(obj->tiling_mode == I915_TILING_X) !=
14987 !!(mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED)) {
14988 DRM_DEBUG("tiling_mode doesn't match fb modifier\n");
14989 return -EINVAL;
14990 }
14991 } else {
14992 if (obj->tiling_mode == I915_TILING_X)
14993 mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
14994 else if (obj->tiling_mode == I915_TILING_Y) {
14995 DRM_DEBUG("No Y tiling for legacy addfb\n");
14996 return -EINVAL;
14997 }
14998 }
14999
9a8f0a12
TU
15000 /* Passed in modifier sanity checking. */
15001 switch (mode_cmd->modifier[0]) {
15002 case I915_FORMAT_MOD_Y_TILED:
15003 case I915_FORMAT_MOD_Yf_TILED:
15004 if (INTEL_INFO(dev)->gen < 9) {
15005 DRM_DEBUG("Unsupported tiling 0x%llx!\n",
15006 mode_cmd->modifier[0]);
15007 return -EINVAL;
15008 }
15009 case DRM_FORMAT_MOD_NONE:
15010 case I915_FORMAT_MOD_X_TILED:
15011 break;
15012 default:
c0f40428
JB
15013 DRM_DEBUG("Unsupported fb modifier 0x%llx!\n",
15014 mode_cmd->modifier[0]);
57cd6508 15015 return -EINVAL;
c16ed4be 15016 }
57cd6508 15017
7b49f948
VS
15018 stride_alignment = intel_fb_stride_alignment(dev_priv,
15019 mode_cmd->modifier[0],
b321803d
DL
15020 mode_cmd->pixel_format);
15021 if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
15022 DRM_DEBUG("pitch (%d) must be at least %u byte aligned\n",
15023 mode_cmd->pitches[0], stride_alignment);
57cd6508 15024 return -EINVAL;
c16ed4be 15025 }
57cd6508 15026
b321803d
DL
15027 pitch_limit = intel_fb_pitch_limit(dev, mode_cmd->modifier[0],
15028 mode_cmd->pixel_format);
a35cdaa0 15029 if (mode_cmd->pitches[0] > pitch_limit) {
b321803d
DL
15030 DRM_DEBUG("%s pitch (%u) must be at less than %d\n",
15031 mode_cmd->modifier[0] != DRM_FORMAT_MOD_NONE ?
2a80eada 15032 "tiled" : "linear",
a35cdaa0 15033 mode_cmd->pitches[0], pitch_limit);
5d7bd705 15034 return -EINVAL;
c16ed4be 15035 }
5d7bd705 15036
2a80eada 15037 if (mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED &&
c16ed4be
CW
15038 mode_cmd->pitches[0] != obj->stride) {
15039 DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
15040 mode_cmd->pitches[0], obj->stride);
5d7bd705 15041 return -EINVAL;
c16ed4be 15042 }
5d7bd705 15043
57779d06 15044 /* Reject formats not supported by any plane early. */
308e5bcb 15045 switch (mode_cmd->pixel_format) {
57779d06 15046 case DRM_FORMAT_C8:
04b3924d
VS
15047 case DRM_FORMAT_RGB565:
15048 case DRM_FORMAT_XRGB8888:
15049 case DRM_FORMAT_ARGB8888:
57779d06
VS
15050 break;
15051 case DRM_FORMAT_XRGB1555:
c16ed4be 15052 if (INTEL_INFO(dev)->gen > 3) {
4ee62c76
VS
15053 DRM_DEBUG("unsupported pixel format: %s\n",
15054 drm_get_format_name(mode_cmd->pixel_format));
57779d06 15055 return -EINVAL;
c16ed4be 15056 }
57779d06 15057 break;
57779d06 15058 case DRM_FORMAT_ABGR8888:
666a4537
WB
15059 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev) &&
15060 INTEL_INFO(dev)->gen < 9) {
6c0fd451
DL
15061 DRM_DEBUG("unsupported pixel format: %s\n",
15062 drm_get_format_name(mode_cmd->pixel_format));
15063 return -EINVAL;
15064 }
15065 break;
15066 case DRM_FORMAT_XBGR8888:
04b3924d 15067 case DRM_FORMAT_XRGB2101010:
57779d06 15068 case DRM_FORMAT_XBGR2101010:
c16ed4be 15069 if (INTEL_INFO(dev)->gen < 4) {
4ee62c76
VS
15070 DRM_DEBUG("unsupported pixel format: %s\n",
15071 drm_get_format_name(mode_cmd->pixel_format));
57779d06 15072 return -EINVAL;
c16ed4be 15073 }
b5626747 15074 break;
7531208b 15075 case DRM_FORMAT_ABGR2101010:
666a4537 15076 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
7531208b
DL
15077 DRM_DEBUG("unsupported pixel format: %s\n",
15078 drm_get_format_name(mode_cmd->pixel_format));
15079 return -EINVAL;
15080 }
15081 break;
04b3924d
VS
15082 case DRM_FORMAT_YUYV:
15083 case DRM_FORMAT_UYVY:
15084 case DRM_FORMAT_YVYU:
15085 case DRM_FORMAT_VYUY:
c16ed4be 15086 if (INTEL_INFO(dev)->gen < 5) {
4ee62c76
VS
15087 DRM_DEBUG("unsupported pixel format: %s\n",
15088 drm_get_format_name(mode_cmd->pixel_format));
57779d06 15089 return -EINVAL;
c16ed4be 15090 }
57cd6508
CW
15091 break;
15092 default:
4ee62c76
VS
15093 DRM_DEBUG("unsupported pixel format: %s\n",
15094 drm_get_format_name(mode_cmd->pixel_format));
57cd6508
CW
15095 return -EINVAL;
15096 }
15097
90f9a336
VS
15098 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
15099 if (mode_cmd->offsets[0] != 0)
15100 return -EINVAL;
15101
ec2c981e 15102 aligned_height = intel_fb_align_height(dev, mode_cmd->height,
091df6cb
DV
15103 mode_cmd->pixel_format,
15104 mode_cmd->modifier[0]);
53155c0a
DV
15105 /* FIXME drm helper for size checks (especially planar formats)? */
15106 if (obj->base.size < aligned_height * mode_cmd->pitches[0])
15107 return -EINVAL;
15108
c7d73f6a
DV
15109 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
15110 intel_fb->obj = obj;
15111
2d7a215f
VS
15112 intel_fill_fb_info(dev_priv, &intel_fb->base);
15113
79e53945
JB
15114 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
15115 if (ret) {
15116 DRM_ERROR("framebuffer init failed %d\n", ret);
15117 return ret;
15118 }
15119
0b05e1e0
VS
15120 intel_fb->obj->framebuffer_references++;
15121
79e53945
JB
15122 return 0;
15123}
15124
79e53945
JB
15125static struct drm_framebuffer *
15126intel_user_framebuffer_create(struct drm_device *dev,
15127 struct drm_file *filp,
1eb83451 15128 const struct drm_mode_fb_cmd2 *user_mode_cmd)
79e53945 15129{
dcb1394e 15130 struct drm_framebuffer *fb;
05394f39 15131 struct drm_i915_gem_object *obj;
76dc3769 15132 struct drm_mode_fb_cmd2 mode_cmd = *user_mode_cmd;
79e53945 15133
a8ad0bd8 15134 obj = to_intel_bo(drm_gem_object_lookup(filp, mode_cmd.handles[0]));
c8725226 15135 if (&obj->base == NULL)
cce13ff7 15136 return ERR_PTR(-ENOENT);
79e53945 15137
92907cbb 15138 fb = intel_framebuffer_create(dev, &mode_cmd, obj);
dcb1394e
LW
15139 if (IS_ERR(fb))
15140 drm_gem_object_unreference_unlocked(&obj->base);
15141
15142 return fb;
79e53945
JB
15143}
15144
0695726e 15145#ifndef CONFIG_DRM_FBDEV_EMULATION
0632fef6 15146static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
4520f53a
DV
15147{
15148}
15149#endif
15150
79e53945 15151static const struct drm_mode_config_funcs intel_mode_funcs = {
79e53945 15152 .fb_create = intel_user_framebuffer_create,
0632fef6 15153 .output_poll_changed = intel_fbdev_output_poll_changed,
5ee67f1c
MR
15154 .atomic_check = intel_atomic_check,
15155 .atomic_commit = intel_atomic_commit,
de419ab6
ML
15156 .atomic_state_alloc = intel_atomic_state_alloc,
15157 .atomic_state_clear = intel_atomic_state_clear,
79e53945
JB
15158};
15159
88212941
ID
15160/**
15161 * intel_init_display_hooks - initialize the display modesetting hooks
15162 * @dev_priv: device private
15163 */
15164void intel_init_display_hooks(struct drm_i915_private *dev_priv)
e70236a8 15165{
88212941 15166 if (INTEL_INFO(dev_priv)->gen >= 9) {
bc8d7dff 15167 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
5724dbd1
DL
15168 dev_priv->display.get_initial_plane_config =
15169 skylake_get_initial_plane_config;
bc8d7dff
DL
15170 dev_priv->display.crtc_compute_clock =
15171 haswell_crtc_compute_clock;
15172 dev_priv->display.crtc_enable = haswell_crtc_enable;
15173 dev_priv->display.crtc_disable = haswell_crtc_disable;
88212941 15174 } else if (HAS_DDI(dev_priv)) {
0e8ffe1b 15175 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
5724dbd1
DL
15176 dev_priv->display.get_initial_plane_config =
15177 ironlake_get_initial_plane_config;
797d0259
ACO
15178 dev_priv->display.crtc_compute_clock =
15179 haswell_crtc_compute_clock;
4f771f10
PZ
15180 dev_priv->display.crtc_enable = haswell_crtc_enable;
15181 dev_priv->display.crtc_disable = haswell_crtc_disable;
88212941 15182 } else if (HAS_PCH_SPLIT(dev_priv)) {
0e8ffe1b 15183 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
5724dbd1
DL
15184 dev_priv->display.get_initial_plane_config =
15185 ironlake_get_initial_plane_config;
3fb37703
ACO
15186 dev_priv->display.crtc_compute_clock =
15187 ironlake_crtc_compute_clock;
76e5a89c
DV
15188 dev_priv->display.crtc_enable = ironlake_crtc_enable;
15189 dev_priv->display.crtc_disable = ironlake_crtc_disable;
65b3d6a9 15190 } else if (IS_CHERRYVIEW(dev_priv)) {
89b667f8 15191 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
5724dbd1
DL
15192 dev_priv->display.get_initial_plane_config =
15193 i9xx_get_initial_plane_config;
65b3d6a9
ACO
15194 dev_priv->display.crtc_compute_clock = chv_crtc_compute_clock;
15195 dev_priv->display.crtc_enable = valleyview_crtc_enable;
15196 dev_priv->display.crtc_disable = i9xx_crtc_disable;
15197 } else if (IS_VALLEYVIEW(dev_priv)) {
15198 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
15199 dev_priv->display.get_initial_plane_config =
15200 i9xx_get_initial_plane_config;
15201 dev_priv->display.crtc_compute_clock = vlv_crtc_compute_clock;
89b667f8
JB
15202 dev_priv->display.crtc_enable = valleyview_crtc_enable;
15203 dev_priv->display.crtc_disable = i9xx_crtc_disable;
19ec6693
ACO
15204 } else if (IS_G4X(dev_priv)) {
15205 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
15206 dev_priv->display.get_initial_plane_config =
15207 i9xx_get_initial_plane_config;
15208 dev_priv->display.crtc_compute_clock = g4x_crtc_compute_clock;
15209 dev_priv->display.crtc_enable = i9xx_crtc_enable;
15210 dev_priv->display.crtc_disable = i9xx_crtc_disable;
70e8aa21
ACO
15211 } else if (IS_PINEVIEW(dev_priv)) {
15212 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
15213 dev_priv->display.get_initial_plane_config =
15214 i9xx_get_initial_plane_config;
15215 dev_priv->display.crtc_compute_clock = pnv_crtc_compute_clock;
15216 dev_priv->display.crtc_enable = i9xx_crtc_enable;
15217 dev_priv->display.crtc_disable = i9xx_crtc_disable;
81c97f52 15218 } else if (!IS_GEN2(dev_priv)) {
0e8ffe1b 15219 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
5724dbd1
DL
15220 dev_priv->display.get_initial_plane_config =
15221 i9xx_get_initial_plane_config;
d6dfee7a 15222 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
76e5a89c
DV
15223 dev_priv->display.crtc_enable = i9xx_crtc_enable;
15224 dev_priv->display.crtc_disable = i9xx_crtc_disable;
81c97f52
ACO
15225 } else {
15226 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
15227 dev_priv->display.get_initial_plane_config =
15228 i9xx_get_initial_plane_config;
15229 dev_priv->display.crtc_compute_clock = i8xx_crtc_compute_clock;
15230 dev_priv->display.crtc_enable = i9xx_crtc_enable;
15231 dev_priv->display.crtc_disable = i9xx_crtc_disable;
f564048e 15232 }
e70236a8 15233
e70236a8 15234 /* Returns the core display clock speed */
88212941 15235 if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv))
1652d19e
VS
15236 dev_priv->display.get_display_clock_speed =
15237 skylake_get_display_clock_speed;
88212941 15238 else if (IS_BROXTON(dev_priv))
acd3f3d3
BP
15239 dev_priv->display.get_display_clock_speed =
15240 broxton_get_display_clock_speed;
88212941 15241 else if (IS_BROADWELL(dev_priv))
1652d19e
VS
15242 dev_priv->display.get_display_clock_speed =
15243 broadwell_get_display_clock_speed;
88212941 15244 else if (IS_HASWELL(dev_priv))
1652d19e
VS
15245 dev_priv->display.get_display_clock_speed =
15246 haswell_get_display_clock_speed;
88212941 15247 else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
25eb05fc
JB
15248 dev_priv->display.get_display_clock_speed =
15249 valleyview_get_display_clock_speed;
88212941 15250 else if (IS_GEN5(dev_priv))
b37a6434
VS
15251 dev_priv->display.get_display_clock_speed =
15252 ilk_get_display_clock_speed;
88212941
ID
15253 else if (IS_I945G(dev_priv) || IS_BROADWATER(dev_priv) ||
15254 IS_GEN6(dev_priv) || IS_IVYBRIDGE(dev_priv))
e70236a8
JB
15255 dev_priv->display.get_display_clock_speed =
15256 i945_get_display_clock_speed;
88212941 15257 else if (IS_GM45(dev_priv))
34edce2f
VS
15258 dev_priv->display.get_display_clock_speed =
15259 gm45_get_display_clock_speed;
88212941 15260 else if (IS_CRESTLINE(dev_priv))
34edce2f
VS
15261 dev_priv->display.get_display_clock_speed =
15262 i965gm_get_display_clock_speed;
88212941 15263 else if (IS_PINEVIEW(dev_priv))
34edce2f
VS
15264 dev_priv->display.get_display_clock_speed =
15265 pnv_get_display_clock_speed;
88212941 15266 else if (IS_G33(dev_priv) || IS_G4X(dev_priv))
34edce2f
VS
15267 dev_priv->display.get_display_clock_speed =
15268 g33_get_display_clock_speed;
88212941 15269 else if (IS_I915G(dev_priv))
e70236a8
JB
15270 dev_priv->display.get_display_clock_speed =
15271 i915_get_display_clock_speed;
88212941 15272 else if (IS_I945GM(dev_priv) || IS_845G(dev_priv))
e70236a8
JB
15273 dev_priv->display.get_display_clock_speed =
15274 i9xx_misc_get_display_clock_speed;
88212941 15275 else if (IS_I915GM(dev_priv))
e70236a8
JB
15276 dev_priv->display.get_display_clock_speed =
15277 i915gm_get_display_clock_speed;
88212941 15278 else if (IS_I865G(dev_priv))
e70236a8
JB
15279 dev_priv->display.get_display_clock_speed =
15280 i865_get_display_clock_speed;
88212941 15281 else if (IS_I85X(dev_priv))
e70236a8 15282 dev_priv->display.get_display_clock_speed =
1b1d2716 15283 i85x_get_display_clock_speed;
623e01e5 15284 else { /* 830 */
88212941 15285 WARN(!IS_I830(dev_priv), "Unknown platform. Assuming 133 MHz CDCLK\n");
e70236a8
JB
15286 dev_priv->display.get_display_clock_speed =
15287 i830_get_display_clock_speed;
623e01e5 15288 }
e70236a8 15289
88212941 15290 if (IS_GEN5(dev_priv)) {
3bb11b53 15291 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
88212941 15292 } else if (IS_GEN6(dev_priv)) {
3bb11b53 15293 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
88212941 15294 } else if (IS_IVYBRIDGE(dev_priv)) {
3bb11b53
SJ
15295 /* FIXME: detect B0+ stepping and use auto training */
15296 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
88212941 15297 } else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
3bb11b53 15298 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
445e780b
VS
15299 }
15300
15301 if (IS_BROADWELL(dev_priv)) {
15302 dev_priv->display.modeset_commit_cdclk =
15303 broadwell_modeset_commit_cdclk;
15304 dev_priv->display.modeset_calc_cdclk =
15305 broadwell_modeset_calc_cdclk;
88212941 15306 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
27c329ed
ML
15307 dev_priv->display.modeset_commit_cdclk =
15308 valleyview_modeset_commit_cdclk;
15309 dev_priv->display.modeset_calc_cdclk =
15310 valleyview_modeset_calc_cdclk;
88212941 15311 } else if (IS_BROXTON(dev_priv)) {
27c329ed 15312 dev_priv->display.modeset_commit_cdclk =
324513c0 15313 bxt_modeset_commit_cdclk;
27c329ed 15314 dev_priv->display.modeset_calc_cdclk =
324513c0 15315 bxt_modeset_calc_cdclk;
c89e39f3
CT
15316 } else if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv)) {
15317 dev_priv->display.modeset_commit_cdclk =
15318 skl_modeset_commit_cdclk;
15319 dev_priv->display.modeset_calc_cdclk =
15320 skl_modeset_calc_cdclk;
e70236a8 15321 }
5a21b665
DV
15322
15323 switch (INTEL_INFO(dev_priv)->gen) {
15324 case 2:
15325 dev_priv->display.queue_flip = intel_gen2_queue_flip;
15326 break;
15327
15328 case 3:
15329 dev_priv->display.queue_flip = intel_gen3_queue_flip;
15330 break;
15331
15332 case 4:
15333 case 5:
15334 dev_priv->display.queue_flip = intel_gen4_queue_flip;
15335 break;
15336
15337 case 6:
15338 dev_priv->display.queue_flip = intel_gen6_queue_flip;
15339 break;
15340 case 7:
15341 case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
15342 dev_priv->display.queue_flip = intel_gen7_queue_flip;
15343 break;
15344 case 9:
15345 /* Drop through - unsupported since execlist only. */
15346 default:
15347 /* Default just returns -ENODEV to indicate unsupported */
15348 dev_priv->display.queue_flip = intel_default_queue_flip;
15349 }
e70236a8
JB
15350}
15351
b690e96c
JB
15352/*
15353 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
15354 * resume, or other times. This quirk makes sure that's the case for
15355 * affected systems.
15356 */
0206e353 15357static void quirk_pipea_force(struct drm_device *dev)
b690e96c
JB
15358{
15359 struct drm_i915_private *dev_priv = dev->dev_private;
15360
15361 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
bc0daf48 15362 DRM_INFO("applying pipe a force quirk\n");
b690e96c
JB
15363}
15364
b6b5d049
VS
15365static void quirk_pipeb_force(struct drm_device *dev)
15366{
15367 struct drm_i915_private *dev_priv = dev->dev_private;
15368
15369 dev_priv->quirks |= QUIRK_PIPEB_FORCE;
15370 DRM_INFO("applying pipe b force quirk\n");
15371}
15372
435793df
KP
15373/*
15374 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
15375 */
15376static void quirk_ssc_force_disable(struct drm_device *dev)
15377{
15378 struct drm_i915_private *dev_priv = dev->dev_private;
15379 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
bc0daf48 15380 DRM_INFO("applying lvds SSC disable quirk\n");
435793df
KP
15381}
15382
4dca20ef 15383/*
5a15ab5b
CE
15384 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
15385 * brightness value
4dca20ef
CE
15386 */
15387static void quirk_invert_brightness(struct drm_device *dev)
15388{
15389 struct drm_i915_private *dev_priv = dev->dev_private;
15390 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
bc0daf48 15391 DRM_INFO("applying inverted panel brightness quirk\n");
435793df
KP
15392}
15393
9c72cc6f
SD
15394/* Some VBT's incorrectly indicate no backlight is present */
15395static void quirk_backlight_present(struct drm_device *dev)
15396{
15397 struct drm_i915_private *dev_priv = dev->dev_private;
15398 dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
15399 DRM_INFO("applying backlight present quirk\n");
15400}
15401
b690e96c
JB
15402struct intel_quirk {
15403 int device;
15404 int subsystem_vendor;
15405 int subsystem_device;
15406 void (*hook)(struct drm_device *dev);
15407};
15408
5f85f176
EE
15409/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
15410struct intel_dmi_quirk {
15411 void (*hook)(struct drm_device *dev);
15412 const struct dmi_system_id (*dmi_id_list)[];
15413};
15414
15415static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
15416{
15417 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
15418 return 1;
15419}
15420
15421static const struct intel_dmi_quirk intel_dmi_quirks[] = {
15422 {
15423 .dmi_id_list = &(const struct dmi_system_id[]) {
15424 {
15425 .callback = intel_dmi_reverse_brightness,
15426 .ident = "NCR Corporation",
15427 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
15428 DMI_MATCH(DMI_PRODUCT_NAME, ""),
15429 },
15430 },
15431 { } /* terminating entry */
15432 },
15433 .hook = quirk_invert_brightness,
15434 },
15435};
15436
c43b5634 15437static struct intel_quirk intel_quirks[] = {
b690e96c
JB
15438 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
15439 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
15440
b690e96c
JB
15441 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
15442 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
15443
5f080c0f
VS
15444 /* 830 needs to leave pipe A & dpll A up */
15445 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
15446
b6b5d049
VS
15447 /* 830 needs to leave pipe B & dpll B up */
15448 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
15449
435793df
KP
15450 /* Lenovo U160 cannot use SSC on LVDS */
15451 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
070d329a
MAS
15452
15453 /* Sony Vaio Y cannot use SSC on LVDS */
15454 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
5a15ab5b 15455
be505f64
AH
15456 /* Acer Aspire 5734Z must invert backlight brightness */
15457 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
15458
15459 /* Acer/eMachines G725 */
15460 { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
15461
15462 /* Acer/eMachines e725 */
15463 { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
15464
15465 /* Acer/Packard Bell NCL20 */
15466 { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
15467
15468 /* Acer Aspire 4736Z */
15469 { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
0f540c3a
JN
15470
15471 /* Acer Aspire 5336 */
15472 { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
2e93a1aa
SD
15473
15474 /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
15475 { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
d4967d8c 15476
dfb3d47b
SD
15477 /* Acer C720 Chromebook (Core i3 4005U) */
15478 { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
15479
b2a9601c 15480 /* Apple Macbook 2,1 (Core 2 T7400) */
15481 { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
15482
1b9448b0
JN
15483 /* Apple Macbook 4,1 */
15484 { 0x2a02, 0x106b, 0x00a1, quirk_backlight_present },
15485
d4967d8c
SD
15486 /* Toshiba CB35 Chromebook (Celeron 2955U) */
15487 { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
724cb06f
SD
15488
15489 /* HP Chromebook 14 (Celeron 2955U) */
15490 { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
cf6f0af9
JN
15491
15492 /* Dell Chromebook 11 */
15493 { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
9be64eee
JN
15494
15495 /* Dell Chromebook 11 (2015 version) */
15496 { 0x0a16, 0x1028, 0x0a35, quirk_backlight_present },
b690e96c
JB
15497};
15498
15499static void intel_init_quirks(struct drm_device *dev)
15500{
15501 struct pci_dev *d = dev->pdev;
15502 int i;
15503
15504 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
15505 struct intel_quirk *q = &intel_quirks[i];
15506
15507 if (d->device == q->device &&
15508 (d->subsystem_vendor == q->subsystem_vendor ||
15509 q->subsystem_vendor == PCI_ANY_ID) &&
15510 (d->subsystem_device == q->subsystem_device ||
15511 q->subsystem_device == PCI_ANY_ID))
15512 q->hook(dev);
15513 }
5f85f176
EE
15514 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
15515 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
15516 intel_dmi_quirks[i].hook(dev);
15517 }
b690e96c
JB
15518}
15519
9cce37f4
JB
15520/* Disable the VGA plane that we never use */
15521static void i915_disable_vga(struct drm_device *dev)
15522{
15523 struct drm_i915_private *dev_priv = dev->dev_private;
15524 u8 sr1;
f0f59a00 15525 i915_reg_t vga_reg = i915_vgacntrl_reg(dev);
9cce37f4 15526
2b37c616 15527 /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
9cce37f4 15528 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
3fdcf431 15529 outb(SR01, VGA_SR_INDEX);
9cce37f4
JB
15530 sr1 = inb(VGA_SR_DATA);
15531 outb(sr1 | 1<<5, VGA_SR_DATA);
15532 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
15533 udelay(300);
15534
01f5a626 15535 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
9cce37f4
JB
15536 POSTING_READ(vga_reg);
15537}
15538
f817586c
DV
15539void intel_modeset_init_hw(struct drm_device *dev)
15540{
1a617b77
ML
15541 struct drm_i915_private *dev_priv = dev->dev_private;
15542
b6283055 15543 intel_update_cdclk(dev);
1a617b77
ML
15544
15545 dev_priv->atomic_cdclk_freq = dev_priv->cdclk_freq;
15546
f817586c 15547 intel_init_clock_gating(dev);
dc97997a 15548 intel_enable_gt_powersave(dev_priv);
f817586c
DV
15549}
15550
d93c0372
MR
15551/*
15552 * Calculate what we think the watermarks should be for the state we've read
15553 * out of the hardware and then immediately program those watermarks so that
15554 * we ensure the hardware settings match our internal state.
15555 *
15556 * We can calculate what we think WM's should be by creating a duplicate of the
15557 * current state (which was constructed during hardware readout) and running it
15558 * through the atomic check code to calculate new watermark values in the
15559 * state object.
15560 */
15561static void sanitize_watermarks(struct drm_device *dev)
15562{
15563 struct drm_i915_private *dev_priv = to_i915(dev);
15564 struct drm_atomic_state *state;
15565 struct drm_crtc *crtc;
15566 struct drm_crtc_state *cstate;
15567 struct drm_modeset_acquire_ctx ctx;
15568 int ret;
15569 int i;
15570
15571 /* Only supported on platforms that use atomic watermark design */
ed4a6a7c 15572 if (!dev_priv->display.optimize_watermarks)
d93c0372
MR
15573 return;
15574
15575 /*
15576 * We need to hold connection_mutex before calling duplicate_state so
15577 * that the connector loop is protected.
15578 */
15579 drm_modeset_acquire_init(&ctx, 0);
15580retry:
0cd1262d 15581 ret = drm_modeset_lock_all_ctx(dev, &ctx);
d93c0372
MR
15582 if (ret == -EDEADLK) {
15583 drm_modeset_backoff(&ctx);
15584 goto retry;
15585 } else if (WARN_ON(ret)) {
0cd1262d 15586 goto fail;
d93c0372
MR
15587 }
15588
15589 state = drm_atomic_helper_duplicate_state(dev, &ctx);
15590 if (WARN_ON(IS_ERR(state)))
0cd1262d 15591 goto fail;
d93c0372 15592
ed4a6a7c
MR
15593 /*
15594 * Hardware readout is the only time we don't want to calculate
15595 * intermediate watermarks (since we don't trust the current
15596 * watermarks).
15597 */
15598 to_intel_atomic_state(state)->skip_intermediate_wm = true;
15599
d93c0372
MR
15600 ret = intel_atomic_check(dev, state);
15601 if (ret) {
15602 /*
15603 * If we fail here, it means that the hardware appears to be
15604 * programmed in a way that shouldn't be possible, given our
15605 * understanding of watermark requirements. This might mean a
15606 * mistake in the hardware readout code or a mistake in the
15607 * watermark calculations for a given platform. Raise a WARN
15608 * so that this is noticeable.
15609 *
15610 * If this actually happens, we'll have to just leave the
15611 * BIOS-programmed watermarks untouched and hope for the best.
15612 */
15613 WARN(true, "Could not determine valid watermarks for inherited state\n");
0cd1262d 15614 goto fail;
d93c0372
MR
15615 }
15616
15617 /* Write calculated watermark values back */
d93c0372
MR
15618 for_each_crtc_in_state(state, crtc, cstate, i) {
15619 struct intel_crtc_state *cs = to_intel_crtc_state(cstate);
15620
ed4a6a7c
MR
15621 cs->wm.need_postvbl_update = true;
15622 dev_priv->display.optimize_watermarks(cs);
d93c0372
MR
15623 }
15624
15625 drm_atomic_state_free(state);
0cd1262d 15626fail:
d93c0372
MR
15627 drm_modeset_drop_locks(&ctx);
15628 drm_modeset_acquire_fini(&ctx);
15629}
15630
79e53945
JB
15631void intel_modeset_init(struct drm_device *dev)
15632{
72e96d64
JL
15633 struct drm_i915_private *dev_priv = to_i915(dev);
15634 struct i915_ggtt *ggtt = &dev_priv->ggtt;
1fe47785 15635 int sprite, ret;
8cc87b75 15636 enum pipe pipe;
46f297fb 15637 struct intel_crtc *crtc;
79e53945
JB
15638
15639 drm_mode_config_init(dev);
15640
15641 dev->mode_config.min_width = 0;
15642 dev->mode_config.min_height = 0;
15643
019d96cb
DA
15644 dev->mode_config.preferred_depth = 24;
15645 dev->mode_config.prefer_shadow = 1;
15646
25bab385
TU
15647 dev->mode_config.allow_fb_modifiers = true;
15648
e6ecefaa 15649 dev->mode_config.funcs = &intel_mode_funcs;
79e53945 15650
b690e96c
JB
15651 intel_init_quirks(dev);
15652
1fa61106
ED
15653 intel_init_pm(dev);
15654
e3c74757
BW
15655 if (INTEL_INFO(dev)->num_pipes == 0)
15656 return;
15657
69f92f67
LW
15658 /*
15659 * There may be no VBT; and if the BIOS enabled SSC we can
15660 * just keep using it to avoid unnecessary flicker. Whereas if the
15661 * BIOS isn't using it, don't assume it will work even if the VBT
15662 * indicates as much.
15663 */
15664 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)) {
15665 bool bios_lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
15666 DREF_SSC1_ENABLE);
15667
15668 if (dev_priv->vbt.lvds_use_ssc != bios_lvds_use_ssc) {
15669 DRM_DEBUG_KMS("SSC %sabled by BIOS, overriding VBT which says %sabled\n",
15670 bios_lvds_use_ssc ? "en" : "dis",
15671 dev_priv->vbt.lvds_use_ssc ? "en" : "dis");
15672 dev_priv->vbt.lvds_use_ssc = bios_lvds_use_ssc;
15673 }
15674 }
15675
a6c45cf0
CW
15676 if (IS_GEN2(dev)) {
15677 dev->mode_config.max_width = 2048;
15678 dev->mode_config.max_height = 2048;
15679 } else if (IS_GEN3(dev)) {
5e4d6fa7
KP
15680 dev->mode_config.max_width = 4096;
15681 dev->mode_config.max_height = 4096;
79e53945 15682 } else {
a6c45cf0
CW
15683 dev->mode_config.max_width = 8192;
15684 dev->mode_config.max_height = 8192;
79e53945 15685 }
068be561 15686
dc41c154
VS
15687 if (IS_845G(dev) || IS_I865G(dev)) {
15688 dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
15689 dev->mode_config.cursor_height = 1023;
15690 } else if (IS_GEN2(dev)) {
068be561
DL
15691 dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
15692 dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
15693 } else {
15694 dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
15695 dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
15696 }
15697
72e96d64 15698 dev->mode_config.fb_base = ggtt->mappable_base;
79e53945 15699
28c97730 15700 DRM_DEBUG_KMS("%d display pipe%s available.\n",
7eb552ae
BW
15701 INTEL_INFO(dev)->num_pipes,
15702 INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
79e53945 15703
055e393f 15704 for_each_pipe(dev_priv, pipe) {
8cc87b75 15705 intel_crtc_init(dev, pipe);
3bdcfc0c 15706 for_each_sprite(dev_priv, pipe, sprite) {
1fe47785 15707 ret = intel_plane_init(dev, pipe, sprite);
7f1f3851 15708 if (ret)
06da8da2 15709 DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
1fe47785 15710 pipe_name(pipe), sprite_name(pipe, sprite), ret);
7f1f3851 15711 }
79e53945
JB
15712 }
15713
bfa7df01
VS
15714 intel_update_czclk(dev_priv);
15715 intel_update_cdclk(dev);
15716
e72f9fbf 15717 intel_shared_dpll_init(dev);
ee7b9f93 15718
b2045352
VS
15719 if (dev_priv->max_cdclk_freq == 0)
15720 intel_update_max_cdclk(dev);
15721
9cce37f4
JB
15722 /* Just disable it once at startup */
15723 i915_disable_vga(dev);
79e53945 15724 intel_setup_outputs(dev);
11be49eb 15725
6e9f798d 15726 drm_modeset_lock_all(dev);
043e9bda 15727 intel_modeset_setup_hw_state(dev);
6e9f798d 15728 drm_modeset_unlock_all(dev);
46f297fb 15729
d3fcc808 15730 for_each_intel_crtc(dev, crtc) {
eeebeac5
ML
15731 struct intel_initial_plane_config plane_config = {};
15732
46f297fb
JB
15733 if (!crtc->active)
15734 continue;
15735
46f297fb 15736 /*
46f297fb
JB
15737 * Note that reserving the BIOS fb up front prevents us
15738 * from stuffing other stolen allocations like the ring
15739 * on top. This prevents some ugliness at boot time, and
15740 * can even allow for smooth boot transitions if the BIOS
15741 * fb is large enough for the active pipe configuration.
15742 */
eeebeac5
ML
15743 dev_priv->display.get_initial_plane_config(crtc,
15744 &plane_config);
15745
15746 /*
15747 * If the fb is shared between multiple heads, we'll
15748 * just get the first one.
15749 */
15750 intel_find_initial_plane_obj(crtc, &plane_config);
46f297fb 15751 }
d93c0372
MR
15752
15753 /*
15754 * Make sure hardware watermarks really match the state we read out.
15755 * Note that we need to do this after reconstructing the BIOS fb's
15756 * since the watermark calculation done here will use pstate->fb.
15757 */
15758 sanitize_watermarks(dev);
2c7111db
CW
15759}
15760
7fad798e
DV
15761static void intel_enable_pipe_a(struct drm_device *dev)
15762{
15763 struct intel_connector *connector;
15764 struct drm_connector *crt = NULL;
15765 struct intel_load_detect_pipe load_detect_temp;
208bf9fd 15766 struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
7fad798e
DV
15767
15768 /* We can't just switch on the pipe A, we need to set things up with a
15769 * proper mode and output configuration. As a gross hack, enable pipe A
15770 * by enabling the load detect pipe once. */
3a3371ff 15771 for_each_intel_connector(dev, connector) {
7fad798e
DV
15772 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
15773 crt = &connector->base;
15774 break;
15775 }
15776 }
15777
15778 if (!crt)
15779 return;
15780
208bf9fd 15781 if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
49172fee 15782 intel_release_load_detect_pipe(crt, &load_detect_temp, ctx);
7fad798e
DV
15783}
15784
fa555837
DV
15785static bool
15786intel_check_plane_mapping(struct intel_crtc *crtc)
15787{
7eb552ae
BW
15788 struct drm_device *dev = crtc->base.dev;
15789 struct drm_i915_private *dev_priv = dev->dev_private;
649636ef 15790 u32 val;
fa555837 15791
7eb552ae 15792 if (INTEL_INFO(dev)->num_pipes == 1)
fa555837
DV
15793 return true;
15794
649636ef 15795 val = I915_READ(DSPCNTR(!crtc->plane));
fa555837
DV
15796
15797 if ((val & DISPLAY_PLANE_ENABLE) &&
15798 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
15799 return false;
15800
15801 return true;
15802}
15803
02e93c35
VS
15804static bool intel_crtc_has_encoders(struct intel_crtc *crtc)
15805{
15806 struct drm_device *dev = crtc->base.dev;
15807 struct intel_encoder *encoder;
15808
15809 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
15810 return true;
15811
15812 return false;
15813}
15814
dd756198
VS
15815static bool intel_encoder_has_connectors(struct intel_encoder *encoder)
15816{
15817 struct drm_device *dev = encoder->base.dev;
15818 struct intel_connector *connector;
15819
15820 for_each_connector_on_encoder(dev, &encoder->base, connector)
15821 return true;
15822
15823 return false;
15824}
15825
24929352
DV
15826static void intel_sanitize_crtc(struct intel_crtc *crtc)
15827{
15828 struct drm_device *dev = crtc->base.dev;
15829 struct drm_i915_private *dev_priv = dev->dev_private;
4d1de975 15830 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
24929352 15831
24929352 15832 /* Clear any frame start delays used for debugging left by the BIOS */
4d1de975
JN
15833 if (!transcoder_is_dsi(cpu_transcoder)) {
15834 i915_reg_t reg = PIPECONF(cpu_transcoder);
15835
15836 I915_WRITE(reg,
15837 I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
15838 }
24929352 15839
d3eaf884 15840 /* restore vblank interrupts to correct state */
9625604c 15841 drm_crtc_vblank_reset(&crtc->base);
d297e103 15842 if (crtc->active) {
f9cd7b88
VS
15843 struct intel_plane *plane;
15844
9625604c 15845 drm_crtc_vblank_on(&crtc->base);
f9cd7b88
VS
15846
15847 /* Disable everything but the primary plane */
15848 for_each_intel_plane_on_crtc(dev, crtc, plane) {
15849 if (plane->base.type == DRM_PLANE_TYPE_PRIMARY)
15850 continue;
15851
15852 plane->disable_plane(&plane->base, &crtc->base);
15853 }
9625604c 15854 }
d3eaf884 15855
24929352 15856 /* We need to sanitize the plane -> pipe mapping first because this will
fa555837
DV
15857 * disable the crtc (and hence change the state) if it is wrong. Note
15858 * that gen4+ has a fixed plane -> pipe mapping. */
15859 if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
24929352
DV
15860 bool plane;
15861
78108b7c
VS
15862 DRM_DEBUG_KMS("[CRTC:%d:%s] wrong plane connection detected!\n",
15863 crtc->base.base.id, crtc->base.name);
24929352
DV
15864
15865 /* Pipe has the wrong plane attached and the plane is active.
15866 * Temporarily change the plane mapping and disable everything
15867 * ... */
15868 plane = crtc->plane;
b70709a6 15869 to_intel_plane_state(crtc->base.primary->state)->visible = true;
24929352 15870 crtc->plane = !plane;
b17d48e2 15871 intel_crtc_disable_noatomic(&crtc->base);
24929352 15872 crtc->plane = plane;
24929352 15873 }
24929352 15874
7fad798e
DV
15875 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
15876 crtc->pipe == PIPE_A && !crtc->active) {
15877 /* BIOS forgot to enable pipe A, this mostly happens after
15878 * resume. Force-enable the pipe to fix this, the update_dpms
15879 * call below we restore the pipe to the right state, but leave
15880 * the required bits on. */
15881 intel_enable_pipe_a(dev);
15882 }
15883
24929352
DV
15884 /* Adjust the state of the output pipe according to whether we
15885 * have active connectors/encoders. */
842e0307 15886 if (crtc->active && !intel_crtc_has_encoders(crtc))
b17d48e2 15887 intel_crtc_disable_noatomic(&crtc->base);
24929352 15888
a3ed6aad 15889 if (crtc->active || HAS_GMCH_DISPLAY(dev)) {
4cc31489
DV
15890 /*
15891 * We start out with underrun reporting disabled to avoid races.
15892 * For correct bookkeeping mark this on active crtcs.
15893 *
c5ab3bc0
DV
15894 * Also on gmch platforms we dont have any hardware bits to
15895 * disable the underrun reporting. Which means we need to start
15896 * out with underrun reporting disabled also on inactive pipes,
15897 * since otherwise we'll complain about the garbage we read when
15898 * e.g. coming up after runtime pm.
15899 *
4cc31489
DV
15900 * No protection against concurrent access is required - at
15901 * worst a fifo underrun happens which also sets this to false.
15902 */
15903 crtc->cpu_fifo_underrun_disabled = true;
15904 crtc->pch_fifo_underrun_disabled = true;
15905 }
24929352
DV
15906}
15907
15908static void intel_sanitize_encoder(struct intel_encoder *encoder)
15909{
15910 struct intel_connector *connector;
15911 struct drm_device *dev = encoder->base.dev;
15912
15913 /* We need to check both for a crtc link (meaning that the
15914 * encoder is active and trying to read from a pipe) and the
15915 * pipe itself being active. */
15916 bool has_active_crtc = encoder->base.crtc &&
15917 to_intel_crtc(encoder->base.crtc)->active;
15918
dd756198 15919 if (intel_encoder_has_connectors(encoder) && !has_active_crtc) {
24929352
DV
15920 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
15921 encoder->base.base.id,
8e329a03 15922 encoder->base.name);
24929352
DV
15923
15924 /* Connector is active, but has no active pipe. This is
15925 * fallout from our resume register restoring. Disable
15926 * the encoder manually again. */
15927 if (encoder->base.crtc) {
15928 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
15929 encoder->base.base.id,
8e329a03 15930 encoder->base.name);
24929352 15931 encoder->disable(encoder);
a62d1497
VS
15932 if (encoder->post_disable)
15933 encoder->post_disable(encoder);
24929352 15934 }
7f1950fb 15935 encoder->base.crtc = NULL;
24929352
DV
15936
15937 /* Inconsistent output/port/pipe state happens presumably due to
15938 * a bug in one of the get_hw_state functions. Or someplace else
15939 * in our code, like the register restore mess on resume. Clamp
15940 * things to off as a safer default. */
3a3371ff 15941 for_each_intel_connector(dev, connector) {
24929352
DV
15942 if (connector->encoder != encoder)
15943 continue;
7f1950fb
EE
15944 connector->base.dpms = DRM_MODE_DPMS_OFF;
15945 connector->base.encoder = NULL;
24929352
DV
15946 }
15947 }
15948 /* Enabled encoders without active connectors will be fixed in
15949 * the crtc fixup. */
15950}
15951
04098753 15952void i915_redisable_vga_power_on(struct drm_device *dev)
0fde901f
KM
15953{
15954 struct drm_i915_private *dev_priv = dev->dev_private;
f0f59a00 15955 i915_reg_t vga_reg = i915_vgacntrl_reg(dev);
0fde901f 15956
04098753
ID
15957 if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
15958 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
15959 i915_disable_vga(dev);
15960 }
15961}
15962
15963void i915_redisable_vga(struct drm_device *dev)
15964{
15965 struct drm_i915_private *dev_priv = dev->dev_private;
15966
8dc8a27c
PZ
15967 /* This function can be called both from intel_modeset_setup_hw_state or
15968 * at a very early point in our resume sequence, where the power well
15969 * structures are not yet restored. Since this function is at a very
15970 * paranoid "someone might have enabled VGA while we were not looking"
15971 * level, just check if the power well is enabled instead of trying to
15972 * follow the "don't touch the power well if we don't need it" policy
15973 * the rest of the driver uses. */
6392f847 15974 if (!intel_display_power_get_if_enabled(dev_priv, POWER_DOMAIN_VGA))
8dc8a27c
PZ
15975 return;
15976
04098753 15977 i915_redisable_vga_power_on(dev);
6392f847
ID
15978
15979 intel_display_power_put(dev_priv, POWER_DOMAIN_VGA);
0fde901f
KM
15980}
15981
f9cd7b88 15982static bool primary_get_hw_state(struct intel_plane *plane)
98ec7739 15983{
f9cd7b88 15984 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
98ec7739 15985
f9cd7b88 15986 return I915_READ(DSPCNTR(plane->plane)) & DISPLAY_PLANE_ENABLE;
d032ffa0
ML
15987}
15988
f9cd7b88
VS
15989/* FIXME read out full plane state for all planes */
15990static void readout_plane_state(struct intel_crtc *crtc)
d032ffa0 15991{
b26d3ea3 15992 struct drm_plane *primary = crtc->base.primary;
f9cd7b88 15993 struct intel_plane_state *plane_state =
b26d3ea3 15994 to_intel_plane_state(primary->state);
d032ffa0 15995
19b8d387 15996 plane_state->visible = crtc->active &&
b26d3ea3
ML
15997 primary_get_hw_state(to_intel_plane(primary));
15998
15999 if (plane_state->visible)
16000 crtc->base.state->plane_mask |= 1 << drm_plane_index(primary);
98ec7739
VS
16001}
16002
30e984df 16003static void intel_modeset_readout_hw_state(struct drm_device *dev)
24929352
DV
16004{
16005 struct drm_i915_private *dev_priv = dev->dev_private;
16006 enum pipe pipe;
24929352
DV
16007 struct intel_crtc *crtc;
16008 struct intel_encoder *encoder;
16009 struct intel_connector *connector;
5358901f 16010 int i;
24929352 16011
565602d7
ML
16012 dev_priv->active_crtcs = 0;
16013
d3fcc808 16014 for_each_intel_crtc(dev, crtc) {
565602d7
ML
16015 struct intel_crtc_state *crtc_state = crtc->config;
16016 int pixclk = 0;
3b117c8f 16017
ec2dc6a0 16018 __drm_atomic_helper_crtc_destroy_state(&crtc_state->base);
565602d7
ML
16019 memset(crtc_state, 0, sizeof(*crtc_state));
16020 crtc_state->base.crtc = &crtc->base;
24929352 16021
565602d7
ML
16022 crtc_state->base.active = crtc_state->base.enable =
16023 dev_priv->display.get_pipe_config(crtc, crtc_state);
16024
16025 crtc->base.enabled = crtc_state->base.enable;
16026 crtc->active = crtc_state->base.active;
16027
16028 if (crtc_state->base.active) {
16029 dev_priv->active_crtcs |= 1 << crtc->pipe;
16030
c89e39f3 16031 if (INTEL_GEN(dev_priv) >= 9 || IS_BROADWELL(dev_priv))
565602d7 16032 pixclk = ilk_pipe_pixel_rate(crtc_state);
9558d15d 16033 else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
565602d7
ML
16034 pixclk = crtc_state->base.adjusted_mode.crtc_clock;
16035 else
16036 WARN_ON(dev_priv->display.modeset_calc_cdclk);
9558d15d
VS
16037
16038 /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
16039 if (IS_BROADWELL(dev_priv) && crtc_state->ips_enabled)
16040 pixclk = DIV_ROUND_UP(pixclk * 100, 95);
565602d7
ML
16041 }
16042
16043 dev_priv->min_pixclk[crtc->pipe] = pixclk;
b70709a6 16044
f9cd7b88 16045 readout_plane_state(crtc);
24929352 16046
78108b7c
VS
16047 DRM_DEBUG_KMS("[CRTC:%d:%s] hw state readout: %s\n",
16048 crtc->base.base.id, crtc->base.name,
24929352
DV
16049 crtc->active ? "enabled" : "disabled");
16050 }
16051
5358901f
DV
16052 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
16053 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
16054
2edd6443
ACO
16055 pll->on = pll->funcs.get_hw_state(dev_priv, pll,
16056 &pll->config.hw_state);
3e369b76 16057 pll->config.crtc_mask = 0;
d3fcc808 16058 for_each_intel_crtc(dev, crtc) {
2dd66ebd 16059 if (crtc->active && crtc->config->shared_dpll == pll)
3e369b76 16060 pll->config.crtc_mask |= 1 << crtc->pipe;
5358901f 16061 }
2dd66ebd 16062 pll->active_mask = pll->config.crtc_mask;
5358901f 16063
1e6f2ddc 16064 DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
3e369b76 16065 pll->name, pll->config.crtc_mask, pll->on);
5358901f
DV
16066 }
16067
b2784e15 16068 for_each_intel_encoder(dev, encoder) {
24929352
DV
16069 pipe = 0;
16070
16071 if (encoder->get_hw_state(encoder, &pipe)) {
045ac3b5
JB
16072 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
16073 encoder->base.crtc = &crtc->base;
6e3c9717 16074 encoder->get_config(encoder, crtc->config);
24929352
DV
16075 } else {
16076 encoder->base.crtc = NULL;
16077 }
16078
6f2bcceb 16079 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
24929352 16080 encoder->base.base.id,
8e329a03 16081 encoder->base.name,
24929352 16082 encoder->base.crtc ? "enabled" : "disabled",
6f2bcceb 16083 pipe_name(pipe));
24929352
DV
16084 }
16085
3a3371ff 16086 for_each_intel_connector(dev, connector) {
24929352
DV
16087 if (connector->get_hw_state(connector)) {
16088 connector->base.dpms = DRM_MODE_DPMS_ON;
2aa974c9
ML
16089
16090 encoder = connector->encoder;
16091 connector->base.encoder = &encoder->base;
16092
16093 if (encoder->base.crtc &&
16094 encoder->base.crtc->state->active) {
16095 /*
16096 * This has to be done during hardware readout
16097 * because anything calling .crtc_disable may
16098 * rely on the connector_mask being accurate.
16099 */
16100 encoder->base.crtc->state->connector_mask |=
16101 1 << drm_connector_index(&connector->base);
e87a52b3
ML
16102 encoder->base.crtc->state->encoder_mask |=
16103 1 << drm_encoder_index(&encoder->base);
2aa974c9
ML
16104 }
16105
24929352
DV
16106 } else {
16107 connector->base.dpms = DRM_MODE_DPMS_OFF;
16108 connector->base.encoder = NULL;
16109 }
16110 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
16111 connector->base.base.id,
c23cc417 16112 connector->base.name,
24929352
DV
16113 connector->base.encoder ? "enabled" : "disabled");
16114 }
7f4c6284
VS
16115
16116 for_each_intel_crtc(dev, crtc) {
16117 crtc->base.hwmode = crtc->config->base.adjusted_mode;
16118
16119 memset(&crtc->base.mode, 0, sizeof(crtc->base.mode));
16120 if (crtc->base.state->active) {
16121 intel_mode_from_pipe_config(&crtc->base.mode, crtc->config);
16122 intel_mode_from_pipe_config(&crtc->base.state->adjusted_mode, crtc->config);
16123 WARN_ON(drm_atomic_set_mode_for_crtc(crtc->base.state, &crtc->base.mode));
16124
16125 /*
16126 * The initial mode needs to be set in order to keep
16127 * the atomic core happy. It wants a valid mode if the
16128 * crtc's enabled, so we do the above call.
16129 *
16130 * At this point some state updated by the connectors
16131 * in their ->detect() callback has not run yet, so
16132 * no recalculation can be done yet.
16133 *
16134 * Even if we could do a recalculation and modeset
16135 * right now it would cause a double modeset if
16136 * fbdev or userspace chooses a different initial mode.
16137 *
16138 * If that happens, someone indicated they wanted a
16139 * mode change, which means it's safe to do a full
16140 * recalculation.
16141 */
16142 crtc->base.state->mode.private_flags = I915_MODE_FLAG_INHERITED;
9eca6832
VS
16143
16144 drm_calc_timestamping_constants(&crtc->base, &crtc->base.hwmode);
16145 update_scanline_offset(crtc);
7f4c6284 16146 }
e3b247da
VS
16147
16148 intel_pipe_config_sanity_check(dev_priv, crtc->config);
7f4c6284 16149 }
30e984df
DV
16150}
16151
043e9bda
ML
16152/* Scan out the current hw modeset state,
16153 * and sanitizes it to the current state
16154 */
16155static void
16156intel_modeset_setup_hw_state(struct drm_device *dev)
30e984df
DV
16157{
16158 struct drm_i915_private *dev_priv = dev->dev_private;
16159 enum pipe pipe;
30e984df
DV
16160 struct intel_crtc *crtc;
16161 struct intel_encoder *encoder;
35c95375 16162 int i;
30e984df
DV
16163
16164 intel_modeset_readout_hw_state(dev);
24929352
DV
16165
16166 /* HW state is read out, now we need to sanitize this mess. */
b2784e15 16167 for_each_intel_encoder(dev, encoder) {
24929352
DV
16168 intel_sanitize_encoder(encoder);
16169 }
16170
055e393f 16171 for_each_pipe(dev_priv, pipe) {
24929352
DV
16172 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
16173 intel_sanitize_crtc(crtc);
6e3c9717
ACO
16174 intel_dump_pipe_config(crtc, crtc->config,
16175 "[setup_hw_state]");
24929352 16176 }
9a935856 16177
d29b2f9d
ACO
16178 intel_modeset_update_connector_atomic_state(dev);
16179
35c95375
DV
16180 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
16181 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
16182
2dd66ebd 16183 if (!pll->on || pll->active_mask)
35c95375
DV
16184 continue;
16185
16186 DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
16187
2edd6443 16188 pll->funcs.disable(dev_priv, pll);
35c95375
DV
16189 pll->on = false;
16190 }
16191
666a4537 16192 if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev))
6eb1a681
VS
16193 vlv_wm_get_hw_state(dev);
16194 else if (IS_GEN9(dev))
3078999f
PB
16195 skl_wm_get_hw_state(dev);
16196 else if (HAS_PCH_SPLIT(dev))
243e6a44 16197 ilk_wm_get_hw_state(dev);
292b990e
ML
16198
16199 for_each_intel_crtc(dev, crtc) {
16200 unsigned long put_domains;
16201
74bff5f9 16202 put_domains = modeset_get_crtc_power_domains(&crtc->base, crtc->config);
292b990e
ML
16203 if (WARN_ON(put_domains))
16204 modeset_put_power_domains(dev_priv, put_domains);
16205 }
16206 intel_display_set_init_power(dev_priv, false);
010cf73d
PZ
16207
16208 intel_fbc_init_pipe_state(dev_priv);
043e9bda 16209}
7d0bc1ea 16210
043e9bda
ML
16211void intel_display_resume(struct drm_device *dev)
16212{
e2c8b870
ML
16213 struct drm_i915_private *dev_priv = to_i915(dev);
16214 struct drm_atomic_state *state = dev_priv->modeset_restore_state;
16215 struct drm_modeset_acquire_ctx ctx;
043e9bda 16216 int ret;
e2c8b870 16217 bool setup = false;
f30da187 16218
e2c8b870 16219 dev_priv->modeset_restore_state = NULL;
043e9bda 16220
ea49c9ac
ML
16221 /*
16222 * This is a cludge because with real atomic modeset mode_config.mutex
16223 * won't be taken. Unfortunately some probed state like
16224 * audio_codec_enable is still protected by mode_config.mutex, so lock
16225 * it here for now.
16226 */
16227 mutex_lock(&dev->mode_config.mutex);
e2c8b870 16228 drm_modeset_acquire_init(&ctx, 0);
043e9bda 16229
e2c8b870
ML
16230retry:
16231 ret = drm_modeset_lock_all_ctx(dev, &ctx);
043e9bda 16232
e2c8b870
ML
16233 if (ret == 0 && !setup) {
16234 setup = true;
043e9bda 16235
e2c8b870
ML
16236 intel_modeset_setup_hw_state(dev);
16237 i915_redisable_vga(dev);
45e2b5f6 16238 }
8af6cf88 16239
e2c8b870
ML
16240 if (ret == 0 && state) {
16241 struct drm_crtc_state *crtc_state;
16242 struct drm_crtc *crtc;
16243 int i;
043e9bda 16244
e2c8b870
ML
16245 state->acquire_ctx = &ctx;
16246
e3d5457c
VS
16247 /* ignore any reset values/BIOS leftovers in the WM registers */
16248 to_intel_atomic_state(state)->skip_intermediate_wm = true;
16249
e2c8b870
ML
16250 for_each_crtc_in_state(state, crtc, crtc_state, i) {
16251 /*
16252 * Force recalculation even if we restore
16253 * current state. With fast modeset this may not result
16254 * in a modeset when the state is compatible.
16255 */
16256 crtc_state->mode_changed = true;
16257 }
16258
16259 ret = drm_atomic_commit(state);
043e9bda
ML
16260 }
16261
e2c8b870
ML
16262 if (ret == -EDEADLK) {
16263 drm_modeset_backoff(&ctx);
16264 goto retry;
16265 }
043e9bda 16266
e2c8b870
ML
16267 drm_modeset_drop_locks(&ctx);
16268 drm_modeset_acquire_fini(&ctx);
ea49c9ac 16269 mutex_unlock(&dev->mode_config.mutex);
043e9bda 16270
e2c8b870
ML
16271 if (ret) {
16272 DRM_ERROR("Restoring old state failed with %i\n", ret);
16273 drm_atomic_state_free(state);
16274 }
2c7111db
CW
16275}
16276
16277void intel_modeset_gem_init(struct drm_device *dev)
16278{
dc97997a 16279 struct drm_i915_private *dev_priv = to_i915(dev);
484b41dd 16280 struct drm_crtc *c;
2ff8fde1 16281 struct drm_i915_gem_object *obj;
e0d6149b 16282 int ret;
484b41dd 16283
dc97997a 16284 intel_init_gt_powersave(dev_priv);
ae48434c 16285
1833b134 16286 intel_modeset_init_hw(dev);
02e792fb 16287
1ee8da6d 16288 intel_setup_overlay(dev_priv);
484b41dd
JB
16289
16290 /*
16291 * Make sure any fbs we allocated at startup are properly
16292 * pinned & fenced. When we do the allocation it's too early
16293 * for this.
16294 */
70e1e0ec 16295 for_each_crtc(dev, c) {
2ff8fde1
MR
16296 obj = intel_fb_obj(c->primary->fb);
16297 if (obj == NULL)
484b41dd
JB
16298 continue;
16299
e0d6149b 16300 mutex_lock(&dev->struct_mutex);
3465c580
VS
16301 ret = intel_pin_and_fence_fb_obj(c->primary->fb,
16302 c->primary->state->rotation);
e0d6149b
TU
16303 mutex_unlock(&dev->struct_mutex);
16304 if (ret) {
484b41dd
JB
16305 DRM_ERROR("failed to pin boot fb on pipe %d\n",
16306 to_intel_crtc(c)->pipe);
66e514c1 16307 drm_framebuffer_unreference(c->primary->fb);
5a21b665 16308 c->primary->fb = NULL;
36750f28 16309 c->primary->crtc = c->primary->state->crtc = NULL;
5a21b665 16310 update_state_fb(c->primary);
36750f28 16311 c->state->plane_mask &= ~(1 << drm_plane_index(c->primary));
484b41dd
JB
16312 }
16313 }
1ebaa0b9
CW
16314}
16315
16316int intel_connector_register(struct drm_connector *connector)
16317{
16318 struct intel_connector *intel_connector = to_intel_connector(connector);
16319 int ret;
16320
16321 ret = intel_backlight_device_register(intel_connector);
16322 if (ret)
16323 goto err;
16324
16325 return 0;
0962c3c9 16326
1ebaa0b9
CW
16327err:
16328 return ret;
79e53945
JB
16329}
16330
c191eca1 16331void intel_connector_unregister(struct drm_connector *connector)
4932e2c3 16332{
e63d87c0 16333 struct intel_connector *intel_connector = to_intel_connector(connector);
4932e2c3 16334
e63d87c0 16335 intel_backlight_device_unregister(intel_connector);
4932e2c3 16336 intel_panel_destroy_backlight(connector);
4932e2c3
ID
16337}
16338
79e53945
JB
16339void intel_modeset_cleanup(struct drm_device *dev)
16340{
652c393a 16341 struct drm_i915_private *dev_priv = dev->dev_private;
652c393a 16342
dc97997a 16343 intel_disable_gt_powersave(dev_priv);
2eb5252e 16344
fd0c0642
DV
16345 /*
16346 * Interrupts and polling as the first thing to avoid creating havoc.
2eb5252e 16347 * Too much stuff here (turning of connectors, ...) would
fd0c0642
DV
16348 * experience fancy races otherwise.
16349 */
2aeb7d3a 16350 intel_irq_uninstall(dev_priv);
eb21b92b 16351
fd0c0642
DV
16352 /*
16353 * Due to the hpd irq storm handling the hotplug work can re-arm the
16354 * poll handlers. Hence disable polling after hpd handling is shut down.
16355 */
f87ea761 16356 drm_kms_helper_poll_fini(dev);
fd0c0642 16357
723bfd70
JB
16358 intel_unregister_dsm_handler();
16359
c937ab3e 16360 intel_fbc_global_disable(dev_priv);
69341a5e 16361
1630fe75
CW
16362 /* flush any delayed tasks or pending work */
16363 flush_scheduled_work();
16364
79e53945 16365 drm_mode_config_cleanup(dev);
4d7bb011 16366
1ee8da6d 16367 intel_cleanup_overlay(dev_priv);
ae48434c 16368
dc97997a 16369 intel_cleanup_gt_powersave(dev_priv);
f5949141
DV
16370
16371 intel_teardown_gmbus(dev);
79e53945
JB
16372}
16373
df0e9248
CW
16374void intel_connector_attach_encoder(struct intel_connector *connector,
16375 struct intel_encoder *encoder)
16376{
16377 connector->encoder = encoder;
16378 drm_mode_connector_attach_encoder(&connector->base,
16379 &encoder->base);
79e53945 16380}
28d52043
DA
16381
16382/*
16383 * set vga decode state - true == enable VGA decode
16384 */
16385int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
16386{
16387 struct drm_i915_private *dev_priv = dev->dev_private;
a885b3cc 16388 unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
28d52043
DA
16389 u16 gmch_ctrl;
16390
75fa041d
CW
16391 if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
16392 DRM_ERROR("failed to read control word\n");
16393 return -EIO;
16394 }
16395
c0cc8a55
CW
16396 if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
16397 return 0;
16398
28d52043
DA
16399 if (state)
16400 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
16401 else
16402 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
75fa041d
CW
16403
16404 if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
16405 DRM_ERROR("failed to write control word\n");
16406 return -EIO;
16407 }
16408
28d52043
DA
16409 return 0;
16410}
c4a1d9e4 16411
c4a1d9e4 16412struct intel_display_error_state {
ff57f1b0
PZ
16413
16414 u32 power_well_driver;
16415
63b66e5b
CW
16416 int num_transcoders;
16417
c4a1d9e4
CW
16418 struct intel_cursor_error_state {
16419 u32 control;
16420 u32 position;
16421 u32 base;
16422 u32 size;
52331309 16423 } cursor[I915_MAX_PIPES];
c4a1d9e4
CW
16424
16425 struct intel_pipe_error_state {
ddf9c536 16426 bool power_domain_on;
c4a1d9e4 16427 u32 source;
f301b1e1 16428 u32 stat;
52331309 16429 } pipe[I915_MAX_PIPES];
c4a1d9e4
CW
16430
16431 struct intel_plane_error_state {
16432 u32 control;
16433 u32 stride;
16434 u32 size;
16435 u32 pos;
16436 u32 addr;
16437 u32 surface;
16438 u32 tile_offset;
52331309 16439 } plane[I915_MAX_PIPES];
63b66e5b
CW
16440
16441 struct intel_transcoder_error_state {
ddf9c536 16442 bool power_domain_on;
63b66e5b
CW
16443 enum transcoder cpu_transcoder;
16444
16445 u32 conf;
16446
16447 u32 htotal;
16448 u32 hblank;
16449 u32 hsync;
16450 u32 vtotal;
16451 u32 vblank;
16452 u32 vsync;
16453 } transcoder[4];
c4a1d9e4
CW
16454};
16455
16456struct intel_display_error_state *
c033666a 16457intel_display_capture_error_state(struct drm_i915_private *dev_priv)
c4a1d9e4 16458{
c4a1d9e4 16459 struct intel_display_error_state *error;
63b66e5b
CW
16460 int transcoders[] = {
16461 TRANSCODER_A,
16462 TRANSCODER_B,
16463 TRANSCODER_C,
16464 TRANSCODER_EDP,
16465 };
c4a1d9e4
CW
16466 int i;
16467
c033666a 16468 if (INTEL_INFO(dev_priv)->num_pipes == 0)
63b66e5b
CW
16469 return NULL;
16470
9d1cb914 16471 error = kzalloc(sizeof(*error), GFP_ATOMIC);
c4a1d9e4
CW
16472 if (error == NULL)
16473 return NULL;
16474
c033666a 16475 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
ff57f1b0
PZ
16476 error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
16477
055e393f 16478 for_each_pipe(dev_priv, i) {
ddf9c536 16479 error->pipe[i].power_domain_on =
f458ebbc
DV
16480 __intel_display_power_is_enabled(dev_priv,
16481 POWER_DOMAIN_PIPE(i));
ddf9c536 16482 if (!error->pipe[i].power_domain_on)
9d1cb914
PZ
16483 continue;
16484
5efb3e28
VS
16485 error->cursor[i].control = I915_READ(CURCNTR(i));
16486 error->cursor[i].position = I915_READ(CURPOS(i));
16487 error->cursor[i].base = I915_READ(CURBASE(i));
c4a1d9e4
CW
16488
16489 error->plane[i].control = I915_READ(DSPCNTR(i));
16490 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
c033666a 16491 if (INTEL_GEN(dev_priv) <= 3) {
51889b35 16492 error->plane[i].size = I915_READ(DSPSIZE(i));
80ca378b
PZ
16493 error->plane[i].pos = I915_READ(DSPPOS(i));
16494 }
c033666a 16495 if (INTEL_GEN(dev_priv) <= 7 && !IS_HASWELL(dev_priv))
ca291363 16496 error->plane[i].addr = I915_READ(DSPADDR(i));
c033666a 16497 if (INTEL_GEN(dev_priv) >= 4) {
c4a1d9e4
CW
16498 error->plane[i].surface = I915_READ(DSPSURF(i));
16499 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
16500 }
16501
c4a1d9e4 16502 error->pipe[i].source = I915_READ(PIPESRC(i));
f301b1e1 16503
c033666a 16504 if (HAS_GMCH_DISPLAY(dev_priv))
f301b1e1 16505 error->pipe[i].stat = I915_READ(PIPESTAT(i));
63b66e5b
CW
16506 }
16507
4d1de975 16508 /* Note: this does not include DSI transcoders. */
c033666a 16509 error->num_transcoders = INTEL_INFO(dev_priv)->num_pipes;
2d1fe073 16510 if (HAS_DDI(dev_priv))
63b66e5b
CW
16511 error->num_transcoders++; /* Account for eDP. */
16512
16513 for (i = 0; i < error->num_transcoders; i++) {
16514 enum transcoder cpu_transcoder = transcoders[i];
16515
ddf9c536 16516 error->transcoder[i].power_domain_on =
f458ebbc 16517 __intel_display_power_is_enabled(dev_priv,
38cc1daf 16518 POWER_DOMAIN_TRANSCODER(cpu_transcoder));
ddf9c536 16519 if (!error->transcoder[i].power_domain_on)
9d1cb914
PZ
16520 continue;
16521
63b66e5b
CW
16522 error->transcoder[i].cpu_transcoder = cpu_transcoder;
16523
16524 error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
16525 error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
16526 error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
16527 error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
16528 error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
16529 error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
16530 error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
c4a1d9e4
CW
16531 }
16532
16533 return error;
16534}
16535
edc3d884
MK
16536#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
16537
c4a1d9e4 16538void
edc3d884 16539intel_display_print_error_state(struct drm_i915_error_state_buf *m,
c4a1d9e4
CW
16540 struct drm_device *dev,
16541 struct intel_display_error_state *error)
16542{
055e393f 16543 struct drm_i915_private *dev_priv = dev->dev_private;
c4a1d9e4
CW
16544 int i;
16545
63b66e5b
CW
16546 if (!error)
16547 return;
16548
edc3d884 16549 err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
190be112 16550 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
edc3d884 16551 err_printf(m, "PWR_WELL_CTL2: %08x\n",
ff57f1b0 16552 error->power_well_driver);
055e393f 16553 for_each_pipe(dev_priv, i) {
edc3d884 16554 err_printf(m, "Pipe [%d]:\n", i);
ddf9c536 16555 err_printf(m, " Power: %s\n",
87ad3212 16556 onoff(error->pipe[i].power_domain_on));
edc3d884 16557 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
f301b1e1 16558 err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
edc3d884
MK
16559
16560 err_printf(m, "Plane [%d]:\n", i);
16561 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
16562 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
80ca378b 16563 if (INTEL_INFO(dev)->gen <= 3) {
edc3d884
MK
16564 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
16565 err_printf(m, " POS: %08x\n", error->plane[i].pos);
80ca378b 16566 }
4b71a570 16567 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
edc3d884 16568 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
c4a1d9e4 16569 if (INTEL_INFO(dev)->gen >= 4) {
edc3d884
MK
16570 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
16571 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
c4a1d9e4
CW
16572 }
16573
edc3d884
MK
16574 err_printf(m, "Cursor [%d]:\n", i);
16575 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
16576 err_printf(m, " POS: %08x\n", error->cursor[i].position);
16577 err_printf(m, " BASE: %08x\n", error->cursor[i].base);
c4a1d9e4 16578 }
63b66e5b
CW
16579
16580 for (i = 0; i < error->num_transcoders; i++) {
da205630 16581 err_printf(m, "CPU transcoder: %s\n",
63b66e5b 16582 transcoder_name(error->transcoder[i].cpu_transcoder));
ddf9c536 16583 err_printf(m, " Power: %s\n",
87ad3212 16584 onoff(error->transcoder[i].power_domain_on));
63b66e5b
CW
16585 err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
16586 err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
16587 err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
16588 err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
16589 err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
16590 err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
16591 err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
16592 }
c4a1d9e4 16593}