]> 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
2c30b43b
CW
1548 if (intel_wait_for_register(dev_priv,
1549 DPLL(pipe),
1550 DPLL_LOCK_VLV,
1551 DPLL_LOCK_VLV,
1552 1))
cd2d34d9
VS
1553 DRM_ERROR("DPLL %d failed to lock\n", pipe);
1554}
1555
d288f65f 1556static void vlv_enable_pll(struct intel_crtc *crtc,
5cec258b 1557 const struct intel_crtc_state *pipe_config)
87442f73 1558{
cd2d34d9 1559 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
8bd3f301 1560 enum pipe pipe = crtc->pipe;
87442f73 1561
8bd3f301 1562 assert_pipe_disabled(dev_priv, pipe);
87442f73 1563
87442f73 1564 /* PLL is protected by panel, make sure we can write it */
7d1a83cb 1565 assert_panel_unlocked(dev_priv, pipe);
87442f73 1566
cd2d34d9
VS
1567 if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
1568 _vlv_enable_pll(crtc, pipe_config);
426115cf 1569
8bd3f301
VS
1570 I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
1571 POSTING_READ(DPLL_MD(pipe));
87442f73
DV
1572}
1573
cd2d34d9
VS
1574
1575static void _chv_enable_pll(struct intel_crtc *crtc,
1576 const struct intel_crtc_state *pipe_config)
9d556c99 1577{
cd2d34d9 1578 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
8bd3f301 1579 enum pipe pipe = crtc->pipe;
9d556c99 1580 enum dpio_channel port = vlv_pipe_to_channel(pipe);
9d556c99
CML
1581 u32 tmp;
1582
a580516d 1583 mutex_lock(&dev_priv->sb_lock);
9d556c99
CML
1584
1585 /* Enable back the 10bit clock to display controller */
1586 tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1587 tmp |= DPIO_DCLKP_EN;
1588 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
1589
54433e91
VS
1590 mutex_unlock(&dev_priv->sb_lock);
1591
9d556c99
CML
1592 /*
1593 * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
1594 */
1595 udelay(1);
1596
1597 /* Enable PLL */
d288f65f 1598 I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
9d556c99
CML
1599
1600 /* Check PLL is locked */
6b18826a
CW
1601 if (intel_wait_for_register(dev_priv,
1602 DPLL(pipe), DPLL_LOCK_VLV, DPLL_LOCK_VLV,
1603 1))
9d556c99 1604 DRM_ERROR("PLL %d failed to lock\n", pipe);
cd2d34d9
VS
1605}
1606
1607static void chv_enable_pll(struct intel_crtc *crtc,
1608 const struct intel_crtc_state *pipe_config)
1609{
1610 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1611 enum pipe pipe = crtc->pipe;
1612
1613 assert_pipe_disabled(dev_priv, pipe);
1614
1615 /* PLL is protected by panel, make sure we can write it */
1616 assert_panel_unlocked(dev_priv, pipe);
1617
1618 if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
1619 _chv_enable_pll(crtc, pipe_config);
9d556c99 1620
c231775c
VS
1621 if (pipe != PIPE_A) {
1622 /*
1623 * WaPixelRepeatModeFixForC0:chv
1624 *
1625 * DPLLCMD is AWOL. Use chicken bits to propagate
1626 * the value from DPLLBMD to either pipe B or C.
1627 */
1628 I915_WRITE(CBR4_VLV, pipe == PIPE_B ? CBR_DPLLBMD_PIPE_B : CBR_DPLLBMD_PIPE_C);
1629 I915_WRITE(DPLL_MD(PIPE_B), pipe_config->dpll_hw_state.dpll_md);
1630 I915_WRITE(CBR4_VLV, 0);
1631 dev_priv->chv_dpll_md[pipe] = pipe_config->dpll_hw_state.dpll_md;
1632
1633 /*
1634 * DPLLB VGA mode also seems to cause problems.
1635 * We should always have it disabled.
1636 */
1637 WARN_ON((I915_READ(DPLL(PIPE_B)) & DPLL_VGA_MODE_DIS) == 0);
1638 } else {
1639 I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
1640 POSTING_READ(DPLL_MD(pipe));
1641 }
9d556c99
CML
1642}
1643
1c4e0274
VS
1644static int intel_num_dvo_pipes(struct drm_device *dev)
1645{
1646 struct intel_crtc *crtc;
1647 int count = 0;
1648
1649 for_each_intel_crtc(dev, crtc)
3538b9df 1650 count += crtc->base.state->active &&
409ee761 1651 intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO);
1c4e0274
VS
1652
1653 return count;
1654}
1655
66e3d5c0 1656static void i9xx_enable_pll(struct intel_crtc *crtc)
63d7bbe9 1657{
66e3d5c0
DV
1658 struct drm_device *dev = crtc->base.dev;
1659 struct drm_i915_private *dev_priv = dev->dev_private;
f0f59a00 1660 i915_reg_t reg = DPLL(crtc->pipe);
6e3c9717 1661 u32 dpll = crtc->config->dpll_hw_state.dpll;
63d7bbe9 1662
66e3d5c0 1663 assert_pipe_disabled(dev_priv, crtc->pipe);
58c6eaa2 1664
63d7bbe9 1665 /* PLL is protected by panel, make sure we can write it */
66e3d5c0
DV
1666 if (IS_MOBILE(dev) && !IS_I830(dev))
1667 assert_panel_unlocked(dev_priv, crtc->pipe);
63d7bbe9 1668
1c4e0274
VS
1669 /* Enable DVO 2x clock on both PLLs if necessary */
1670 if (IS_I830(dev) && intel_num_dvo_pipes(dev) > 0) {
1671 /*
1672 * It appears to be important that we don't enable this
1673 * for the current pipe before otherwise configuring the
1674 * PLL. No idea how this should be handled if multiple
1675 * DVO outputs are enabled simultaneosly.
1676 */
1677 dpll |= DPLL_DVO_2X_MODE;
1678 I915_WRITE(DPLL(!crtc->pipe),
1679 I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
1680 }
66e3d5c0 1681
c2b63374
VS
1682 /*
1683 * Apparently we need to have VGA mode enabled prior to changing
1684 * the P1/P2 dividers. Otherwise the DPLL will keep using the old
1685 * dividers, even though the register value does change.
1686 */
1687 I915_WRITE(reg, 0);
1688
8e7a65aa
VS
1689 I915_WRITE(reg, dpll);
1690
66e3d5c0
DV
1691 /* Wait for the clocks to stabilize. */
1692 POSTING_READ(reg);
1693 udelay(150);
1694
1695 if (INTEL_INFO(dev)->gen >= 4) {
1696 I915_WRITE(DPLL_MD(crtc->pipe),
6e3c9717 1697 crtc->config->dpll_hw_state.dpll_md);
66e3d5c0
DV
1698 } else {
1699 /* The pixel multiplier can only be updated once the
1700 * DPLL is enabled and the clocks are stable.
1701 *
1702 * So write it again.
1703 */
1704 I915_WRITE(reg, dpll);
1705 }
63d7bbe9
JB
1706
1707 /* We do this three times for luck */
66e3d5c0 1708 I915_WRITE(reg, dpll);
63d7bbe9
JB
1709 POSTING_READ(reg);
1710 udelay(150); /* wait for warmup */
66e3d5c0 1711 I915_WRITE(reg, dpll);
63d7bbe9
JB
1712 POSTING_READ(reg);
1713 udelay(150); /* wait for warmup */
66e3d5c0 1714 I915_WRITE(reg, dpll);
63d7bbe9
JB
1715 POSTING_READ(reg);
1716 udelay(150); /* wait for warmup */
1717}
1718
1719/**
50b44a44 1720 * i9xx_disable_pll - disable a PLL
63d7bbe9
JB
1721 * @dev_priv: i915 private structure
1722 * @pipe: pipe PLL to disable
1723 *
1724 * Disable the PLL for @pipe, making sure the pipe is off first.
1725 *
1726 * Note! This is for pre-ILK only.
1727 */
1c4e0274 1728static void i9xx_disable_pll(struct intel_crtc *crtc)
63d7bbe9 1729{
1c4e0274
VS
1730 struct drm_device *dev = crtc->base.dev;
1731 struct drm_i915_private *dev_priv = dev->dev_private;
1732 enum pipe pipe = crtc->pipe;
1733
1734 /* Disable DVO 2x clock on both PLLs if necessary */
1735 if (IS_I830(dev) &&
409ee761 1736 intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO) &&
3538b9df 1737 !intel_num_dvo_pipes(dev)) {
1c4e0274
VS
1738 I915_WRITE(DPLL(PIPE_B),
1739 I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
1740 I915_WRITE(DPLL(PIPE_A),
1741 I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
1742 }
1743
b6b5d049
VS
1744 /* Don't disable pipe or pipe PLLs if needed */
1745 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1746 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
63d7bbe9
JB
1747 return;
1748
1749 /* Make sure the pipe isn't still relying on us */
1750 assert_pipe_disabled(dev_priv, pipe);
1751
b8afb911 1752 I915_WRITE(DPLL(pipe), DPLL_VGA_MODE_DIS);
50b44a44 1753 POSTING_READ(DPLL(pipe));
63d7bbe9
JB
1754}
1755
f6071166
JB
1756static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1757{
b8afb911 1758 u32 val;
f6071166
JB
1759
1760 /* Make sure the pipe isn't still relying on us */
1761 assert_pipe_disabled(dev_priv, pipe);
1762
03ed5cbf
VS
1763 val = DPLL_INTEGRATED_REF_CLK_VLV |
1764 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
1765 if (pipe != PIPE_A)
1766 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
1767
f6071166
JB
1768 I915_WRITE(DPLL(pipe), val);
1769 POSTING_READ(DPLL(pipe));
076ed3b2
CML
1770}
1771
1772static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1773{
d752048d 1774 enum dpio_channel port = vlv_pipe_to_channel(pipe);
076ed3b2
CML
1775 u32 val;
1776
a11b0703
VS
1777 /* Make sure the pipe isn't still relying on us */
1778 assert_pipe_disabled(dev_priv, pipe);
076ed3b2 1779
60bfe44f
VS
1780 val = DPLL_SSC_REF_CLK_CHV |
1781 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
a11b0703
VS
1782 if (pipe != PIPE_A)
1783 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
03ed5cbf 1784
a11b0703
VS
1785 I915_WRITE(DPLL(pipe), val);
1786 POSTING_READ(DPLL(pipe));
d752048d 1787
a580516d 1788 mutex_lock(&dev_priv->sb_lock);
d752048d
VS
1789
1790 /* Disable 10bit clock to display controller */
1791 val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1792 val &= ~DPIO_DCLKP_EN;
1793 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
1794
a580516d 1795 mutex_unlock(&dev_priv->sb_lock);
f6071166
JB
1796}
1797
e4607fcf 1798void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
9b6de0a1
VS
1799 struct intel_digital_port *dport,
1800 unsigned int expected_mask)
89b667f8
JB
1801{
1802 u32 port_mask;
f0f59a00 1803 i915_reg_t dpll_reg;
89b667f8 1804
e4607fcf
CML
1805 switch (dport->port) {
1806 case PORT_B:
89b667f8 1807 port_mask = DPLL_PORTB_READY_MASK;
00fc31b7 1808 dpll_reg = DPLL(0);
e4607fcf
CML
1809 break;
1810 case PORT_C:
89b667f8 1811 port_mask = DPLL_PORTC_READY_MASK;
00fc31b7 1812 dpll_reg = DPLL(0);
9b6de0a1 1813 expected_mask <<= 4;
00fc31b7
CML
1814 break;
1815 case PORT_D:
1816 port_mask = DPLL_PORTD_READY_MASK;
1817 dpll_reg = DPIO_PHY_STATUS;
e4607fcf
CML
1818 break;
1819 default:
1820 BUG();
1821 }
89b667f8 1822
370004d3
CW
1823 if (intel_wait_for_register(dev_priv,
1824 dpll_reg, port_mask, expected_mask,
1825 1000))
9b6de0a1
VS
1826 WARN(1, "timed out waiting for port %c ready: got 0x%x, expected 0x%x\n",
1827 port_name(dport->port), I915_READ(dpll_reg) & port_mask, expected_mask);
89b667f8
JB
1828}
1829
b8a4f404
PZ
1830static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1831 enum pipe pipe)
040484af 1832{
23670b32 1833 struct drm_device *dev = dev_priv->dev;
7c26e5c6 1834 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
e2b78267 1835 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
f0f59a00
VS
1836 i915_reg_t reg;
1837 uint32_t val, pipeconf_val;
040484af 1838
040484af 1839 /* Make sure PCH DPLL is enabled */
8106ddbd 1840 assert_shared_dpll_enabled(dev_priv, intel_crtc->config->shared_dpll);
040484af
JB
1841
1842 /* FDI must be feeding us bits for PCH ports */
1843 assert_fdi_tx_enabled(dev_priv, pipe);
1844 assert_fdi_rx_enabled(dev_priv, pipe);
1845
23670b32
DV
1846 if (HAS_PCH_CPT(dev)) {
1847 /* Workaround: Set the timing override bit before enabling the
1848 * pch transcoder. */
1849 reg = TRANS_CHICKEN2(pipe);
1850 val = I915_READ(reg);
1851 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
1852 I915_WRITE(reg, val);
59c859d6 1853 }
23670b32 1854
ab9412ba 1855 reg = PCH_TRANSCONF(pipe);
040484af 1856 val = I915_READ(reg);
5f7f726d 1857 pipeconf_val = I915_READ(PIPECONF(pipe));
e9bcff5c 1858
2d1fe073 1859 if (HAS_PCH_IBX(dev_priv)) {
e9bcff5c 1860 /*
c5de7c6f
VS
1861 * Make the BPC in transcoder be consistent with
1862 * that in pipeconf reg. For HDMI we must use 8bpc
1863 * here for both 8bpc and 12bpc.
e9bcff5c 1864 */
dfd07d72 1865 val &= ~PIPECONF_BPC_MASK;
c5de7c6f
VS
1866 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_HDMI))
1867 val |= PIPECONF_8BPC;
1868 else
1869 val |= pipeconf_val & PIPECONF_BPC_MASK;
e9bcff5c 1870 }
5f7f726d
PZ
1871
1872 val &= ~TRANS_INTERLACE_MASK;
1873 if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
2d1fe073 1874 if (HAS_PCH_IBX(dev_priv) &&
409ee761 1875 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
7c26e5c6
PZ
1876 val |= TRANS_LEGACY_INTERLACED_ILK;
1877 else
1878 val |= TRANS_INTERLACED;
5f7f726d
PZ
1879 else
1880 val |= TRANS_PROGRESSIVE;
1881
040484af 1882 I915_WRITE(reg, val | TRANS_ENABLE);
650fbd84
CW
1883 if (intel_wait_for_register(dev_priv,
1884 reg, TRANS_STATE_ENABLE, TRANS_STATE_ENABLE,
1885 100))
4bb6f1f3 1886 DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
040484af
JB
1887}
1888
8fb033d7 1889static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
937bb610 1890 enum transcoder cpu_transcoder)
040484af 1891{
8fb033d7 1892 u32 val, pipeconf_val;
8fb033d7 1893
8fb033d7 1894 /* FDI must be feeding us bits for PCH ports */
1a240d4d 1895 assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
937bb610 1896 assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
8fb033d7 1897
223a6fdf 1898 /* Workaround: set timing override bit. */
36c0d0cf 1899 val = I915_READ(TRANS_CHICKEN2(PIPE_A));
23670b32 1900 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
36c0d0cf 1901 I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
223a6fdf 1902
25f3ef11 1903 val = TRANS_ENABLE;
937bb610 1904 pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
8fb033d7 1905
9a76b1c6
PZ
1906 if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
1907 PIPECONF_INTERLACED_ILK)
a35f2679 1908 val |= TRANS_INTERLACED;
8fb033d7
PZ
1909 else
1910 val |= TRANS_PROGRESSIVE;
1911
ab9412ba
DV
1912 I915_WRITE(LPT_TRANSCONF, val);
1913 if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
937bb610 1914 DRM_ERROR("Failed to enable PCH transcoder\n");
8fb033d7
PZ
1915}
1916
b8a4f404
PZ
1917static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
1918 enum pipe pipe)
040484af 1919{
23670b32 1920 struct drm_device *dev = dev_priv->dev;
f0f59a00
VS
1921 i915_reg_t reg;
1922 uint32_t val;
040484af
JB
1923
1924 /* FDI relies on the transcoder */
1925 assert_fdi_tx_disabled(dev_priv, pipe);
1926 assert_fdi_rx_disabled(dev_priv, pipe);
1927
291906f1
JB
1928 /* Ports must be off as well */
1929 assert_pch_ports_disabled(dev_priv, pipe);
1930
ab9412ba 1931 reg = PCH_TRANSCONF(pipe);
040484af
JB
1932 val = I915_READ(reg);
1933 val &= ~TRANS_ENABLE;
1934 I915_WRITE(reg, val);
1935 /* wait for PCH transcoder off, transcoder state */
1936 if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
4bb6f1f3 1937 DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
23670b32 1938
c465613b 1939 if (HAS_PCH_CPT(dev)) {
23670b32
DV
1940 /* Workaround: Clear the timing override chicken bit again. */
1941 reg = TRANS_CHICKEN2(pipe);
1942 val = I915_READ(reg);
1943 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
1944 I915_WRITE(reg, val);
1945 }
040484af
JB
1946}
1947
ab4d966c 1948static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
8fb033d7 1949{
8fb033d7
PZ
1950 u32 val;
1951
ab9412ba 1952 val = I915_READ(LPT_TRANSCONF);
8fb033d7 1953 val &= ~TRANS_ENABLE;
ab9412ba 1954 I915_WRITE(LPT_TRANSCONF, val);
8fb033d7 1955 /* wait for PCH transcoder off, transcoder state */
ab9412ba 1956 if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
8a52fd9f 1957 DRM_ERROR("Failed to disable PCH transcoder\n");
223a6fdf
PZ
1958
1959 /* Workaround: clear timing override bit. */
36c0d0cf 1960 val = I915_READ(TRANS_CHICKEN2(PIPE_A));
23670b32 1961 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
36c0d0cf 1962 I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
040484af
JB
1963}
1964
b24e7179 1965/**
309cfea8 1966 * intel_enable_pipe - enable a pipe, asserting requirements
0372264a 1967 * @crtc: crtc responsible for the pipe
b24e7179 1968 *
0372264a 1969 * Enable @crtc's pipe, making sure that various hardware specific requirements
b24e7179 1970 * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
b24e7179 1971 */
e1fdc473 1972static void intel_enable_pipe(struct intel_crtc *crtc)
b24e7179 1973{
0372264a
PZ
1974 struct drm_device *dev = crtc->base.dev;
1975 struct drm_i915_private *dev_priv = dev->dev_private;
1976 enum pipe pipe = crtc->pipe;
1a70a728 1977 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
1a240d4d 1978 enum pipe pch_transcoder;
f0f59a00 1979 i915_reg_t reg;
b24e7179
JB
1980 u32 val;
1981
9e2ee2dd
VS
1982 DRM_DEBUG_KMS("enabling pipe %c\n", pipe_name(pipe));
1983
58c6eaa2 1984 assert_planes_disabled(dev_priv, pipe);
93ce0ba6 1985 assert_cursor_disabled(dev_priv, pipe);
58c6eaa2
DV
1986 assert_sprites_disabled(dev_priv, pipe);
1987
2d1fe073 1988 if (HAS_PCH_LPT(dev_priv))
cc391bbb
PZ
1989 pch_transcoder = TRANSCODER_A;
1990 else
1991 pch_transcoder = pipe;
1992
b24e7179
JB
1993 /*
1994 * A pipe without a PLL won't actually be able to drive bits from
1995 * a plane. On ILK+ the pipe PLLs are integrated, so we don't
1996 * need the check.
1997 */
2d1fe073 1998 if (HAS_GMCH_DISPLAY(dev_priv))
a65347ba 1999 if (crtc->config->has_dsi_encoder)
23538ef1
JN
2000 assert_dsi_pll_enabled(dev_priv);
2001 else
2002 assert_pll_enabled(dev_priv, pipe);
040484af 2003 else {
6e3c9717 2004 if (crtc->config->has_pch_encoder) {
040484af 2005 /* if driving the PCH, we need FDI enabled */
cc391bbb 2006 assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
1a240d4d
DV
2007 assert_fdi_tx_pll_enabled(dev_priv,
2008 (enum pipe) cpu_transcoder);
040484af
JB
2009 }
2010 /* FIXME: assert CPU port conditions for SNB+ */
2011 }
b24e7179 2012
702e7a56 2013 reg = PIPECONF(cpu_transcoder);
b24e7179 2014 val = I915_READ(reg);
7ad25d48 2015 if (val & PIPECONF_ENABLE) {
b6b5d049
VS
2016 WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
2017 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
00d70b15 2018 return;
7ad25d48 2019 }
00d70b15
CW
2020
2021 I915_WRITE(reg, val | PIPECONF_ENABLE);
851855d8 2022 POSTING_READ(reg);
b7792d8b
VS
2023
2024 /*
2025 * Until the pipe starts DSL will read as 0, which would cause
2026 * an apparent vblank timestamp jump, which messes up also the
2027 * frame count when it's derived from the timestamps. So let's
2028 * wait for the pipe to start properly before we call
2029 * drm_crtc_vblank_on()
2030 */
2031 if (dev->max_vblank_count == 0 &&
2032 wait_for(intel_get_crtc_scanline(crtc) != crtc->scanline_offset, 50))
2033 DRM_ERROR("pipe %c didn't start\n", pipe_name(pipe));
b24e7179
JB
2034}
2035
2036/**
309cfea8 2037 * intel_disable_pipe - disable a pipe, asserting requirements
575f7ab7 2038 * @crtc: crtc whose pipes is to be disabled
b24e7179 2039 *
575f7ab7
VS
2040 * Disable the pipe of @crtc, making sure that various hardware
2041 * specific requirements are met, if applicable, e.g. plane
2042 * disabled, panel fitter off, etc.
b24e7179
JB
2043 *
2044 * Will wait until the pipe has shut down before returning.
2045 */
575f7ab7 2046static void intel_disable_pipe(struct intel_crtc *crtc)
b24e7179 2047{
575f7ab7 2048 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
6e3c9717 2049 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
575f7ab7 2050 enum pipe pipe = crtc->pipe;
f0f59a00 2051 i915_reg_t reg;
b24e7179
JB
2052 u32 val;
2053
9e2ee2dd
VS
2054 DRM_DEBUG_KMS("disabling pipe %c\n", pipe_name(pipe));
2055
b24e7179
JB
2056 /*
2057 * Make sure planes won't keep trying to pump pixels to us,
2058 * or we might hang the display.
2059 */
2060 assert_planes_disabled(dev_priv, pipe);
93ce0ba6 2061 assert_cursor_disabled(dev_priv, pipe);
19332d7a 2062 assert_sprites_disabled(dev_priv, pipe);
b24e7179 2063
702e7a56 2064 reg = PIPECONF(cpu_transcoder);
b24e7179 2065 val = I915_READ(reg);
00d70b15
CW
2066 if ((val & PIPECONF_ENABLE) == 0)
2067 return;
2068
67adc644
VS
2069 /*
2070 * Double wide has implications for planes
2071 * so best keep it disabled when not needed.
2072 */
6e3c9717 2073 if (crtc->config->double_wide)
67adc644
VS
2074 val &= ~PIPECONF_DOUBLE_WIDE;
2075
2076 /* Don't disable pipe or pipe PLLs if needed */
b6b5d049
VS
2077 if (!(pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) &&
2078 !(pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
67adc644
VS
2079 val &= ~PIPECONF_ENABLE;
2080
2081 I915_WRITE(reg, val);
2082 if ((val & PIPECONF_ENABLE) == 0)
2083 intel_wait_for_pipe_off(crtc);
b24e7179
JB
2084}
2085
693db184
CW
2086static bool need_vtd_wa(struct drm_device *dev)
2087{
2088#ifdef CONFIG_INTEL_IOMMU
2089 if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
2090 return true;
2091#endif
2092 return false;
2093}
2094
832be82f
VS
2095static unsigned int intel_tile_size(const struct drm_i915_private *dev_priv)
2096{
2097 return IS_GEN2(dev_priv) ? 2048 : 4096;
2098}
2099
27ba3910
VS
2100static unsigned int intel_tile_width_bytes(const struct drm_i915_private *dev_priv,
2101 uint64_t fb_modifier, unsigned int cpp)
7b49f948
VS
2102{
2103 switch (fb_modifier) {
2104 case DRM_FORMAT_MOD_NONE:
2105 return cpp;
2106 case I915_FORMAT_MOD_X_TILED:
2107 if (IS_GEN2(dev_priv))
2108 return 128;
2109 else
2110 return 512;
2111 case I915_FORMAT_MOD_Y_TILED:
2112 if (IS_GEN2(dev_priv) || HAS_128_BYTE_Y_TILING(dev_priv))
2113 return 128;
2114 else
2115 return 512;
2116 case I915_FORMAT_MOD_Yf_TILED:
2117 switch (cpp) {
2118 case 1:
2119 return 64;
2120 case 2:
2121 case 4:
2122 return 128;
2123 case 8:
2124 case 16:
2125 return 256;
2126 default:
2127 MISSING_CASE(cpp);
2128 return cpp;
2129 }
2130 break;
2131 default:
2132 MISSING_CASE(fb_modifier);
2133 return cpp;
2134 }
2135}
2136
832be82f
VS
2137unsigned int intel_tile_height(const struct drm_i915_private *dev_priv,
2138 uint64_t fb_modifier, unsigned int cpp)
a57ce0b2 2139{
832be82f
VS
2140 if (fb_modifier == DRM_FORMAT_MOD_NONE)
2141 return 1;
2142 else
2143 return intel_tile_size(dev_priv) /
27ba3910 2144 intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
6761dd31
TU
2145}
2146
8d0deca8
VS
2147/* Return the tile dimensions in pixel units */
2148static void intel_tile_dims(const struct drm_i915_private *dev_priv,
2149 unsigned int *tile_width,
2150 unsigned int *tile_height,
2151 uint64_t fb_modifier,
2152 unsigned int cpp)
2153{
2154 unsigned int tile_width_bytes =
2155 intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
2156
2157 *tile_width = tile_width_bytes / cpp;
2158 *tile_height = intel_tile_size(dev_priv) / tile_width_bytes;
2159}
2160
6761dd31
TU
2161unsigned int
2162intel_fb_align_height(struct drm_device *dev, unsigned int height,
832be82f 2163 uint32_t pixel_format, uint64_t fb_modifier)
6761dd31 2164{
832be82f
VS
2165 unsigned int cpp = drm_format_plane_cpp(pixel_format, 0);
2166 unsigned int tile_height = intel_tile_height(to_i915(dev), fb_modifier, cpp);
2167
2168 return ALIGN(height, tile_height);
a57ce0b2
JB
2169}
2170
1663b9d6
VS
2171unsigned int intel_rotation_info_size(const struct intel_rotation_info *rot_info)
2172{
2173 unsigned int size = 0;
2174 int i;
2175
2176 for (i = 0 ; i < ARRAY_SIZE(rot_info->plane); i++)
2177 size += rot_info->plane[i].width * rot_info->plane[i].height;
2178
2179 return size;
2180}
2181
75c82a53 2182static void
3465c580
VS
2183intel_fill_fb_ggtt_view(struct i915_ggtt_view *view,
2184 const struct drm_framebuffer *fb,
2185 unsigned int rotation)
f64b98cd 2186{
2d7a215f
VS
2187 if (intel_rotation_90_or_270(rotation)) {
2188 *view = i915_ggtt_view_rotated;
2189 view->params.rotated = to_intel_framebuffer(fb)->rot_info;
2190 } else {
2191 *view = i915_ggtt_view_normal;
2192 }
2193}
50470bb0 2194
2d7a215f
VS
2195static void
2196intel_fill_fb_info(struct drm_i915_private *dev_priv,
2197 struct drm_framebuffer *fb)
2198{
2199 struct intel_rotation_info *info = &to_intel_framebuffer(fb)->rot_info;
2200 unsigned int tile_size, tile_width, tile_height, cpp;
50470bb0 2201
d9b3288e
VS
2202 tile_size = intel_tile_size(dev_priv);
2203
2204 cpp = drm_format_plane_cpp(fb->pixel_format, 0);
8d0deca8
VS
2205 intel_tile_dims(dev_priv, &tile_width, &tile_height,
2206 fb->modifier[0], cpp);
d9b3288e 2207
1663b9d6
VS
2208 info->plane[0].width = DIV_ROUND_UP(fb->pitches[0], tile_width * cpp);
2209 info->plane[0].height = DIV_ROUND_UP(fb->height, tile_height);
84fe03f7 2210
89e3e142 2211 if (info->pixel_format == DRM_FORMAT_NV12) {
832be82f 2212 cpp = drm_format_plane_cpp(fb->pixel_format, 1);
8d0deca8
VS
2213 intel_tile_dims(dev_priv, &tile_width, &tile_height,
2214 fb->modifier[1], cpp);
d9b3288e 2215
2d7a215f 2216 info->uv_offset = fb->offsets[1];
1663b9d6
VS
2217 info->plane[1].width = DIV_ROUND_UP(fb->pitches[1], tile_width * cpp);
2218 info->plane[1].height = DIV_ROUND_UP(fb->height / 2, tile_height);
89e3e142 2219 }
f64b98cd
TU
2220}
2221
603525d7 2222static unsigned int intel_linear_alignment(const struct drm_i915_private *dev_priv)
4e9a86b6
VS
2223{
2224 if (INTEL_INFO(dev_priv)->gen >= 9)
2225 return 256 * 1024;
985b8bb4 2226 else if (IS_BROADWATER(dev_priv) || IS_CRESTLINE(dev_priv) ||
666a4537 2227 IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
4e9a86b6
VS
2228 return 128 * 1024;
2229 else if (INTEL_INFO(dev_priv)->gen >= 4)
2230 return 4 * 1024;
2231 else
44c5905e 2232 return 0;
4e9a86b6
VS
2233}
2234
603525d7
VS
2235static unsigned int intel_surf_alignment(const struct drm_i915_private *dev_priv,
2236 uint64_t fb_modifier)
2237{
2238 switch (fb_modifier) {
2239 case DRM_FORMAT_MOD_NONE:
2240 return intel_linear_alignment(dev_priv);
2241 case I915_FORMAT_MOD_X_TILED:
2242 if (INTEL_INFO(dev_priv)->gen >= 9)
2243 return 256 * 1024;
2244 return 0;
2245 case I915_FORMAT_MOD_Y_TILED:
2246 case I915_FORMAT_MOD_Yf_TILED:
2247 return 1 * 1024 * 1024;
2248 default:
2249 MISSING_CASE(fb_modifier);
2250 return 0;
2251 }
2252}
2253
127bd2ac 2254int
3465c580
VS
2255intel_pin_and_fence_fb_obj(struct drm_framebuffer *fb,
2256 unsigned int rotation)
6b95a207 2257{
850c4cdc 2258 struct drm_device *dev = fb->dev;
ce453d81 2259 struct drm_i915_private *dev_priv = dev->dev_private;
850c4cdc 2260 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
f64b98cd 2261 struct i915_ggtt_view view;
6b95a207
KH
2262 u32 alignment;
2263 int ret;
2264
ebcdd39e
MR
2265 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
2266
603525d7 2267 alignment = intel_surf_alignment(dev_priv, fb->modifier[0]);
6b95a207 2268
3465c580 2269 intel_fill_fb_ggtt_view(&view, fb, rotation);
f64b98cd 2270
693db184
CW
2271 /* Note that the w/a also requires 64 PTE of padding following the
2272 * bo. We currently fill all unused PTE with the shadow page and so
2273 * we should always have valid PTE following the scanout preventing
2274 * the VT-d warning.
2275 */
2276 if (need_vtd_wa(dev) && alignment < 256 * 1024)
2277 alignment = 256 * 1024;
2278
d6dd6843
PZ
2279 /*
2280 * Global gtt pte registers are special registers which actually forward
2281 * writes to a chunk of system memory. Which means that there is no risk
2282 * that the register values disappear as soon as we call
2283 * intel_runtime_pm_put(), so it is correct to wrap only the
2284 * pin/unpin/fence and not more.
2285 */
2286 intel_runtime_pm_get(dev_priv);
2287
7580d774
ML
2288 ret = i915_gem_object_pin_to_display_plane(obj, alignment,
2289 &view);
48b956c5 2290 if (ret)
b26a6b35 2291 goto err_pm;
6b95a207
KH
2292
2293 /* Install a fence for tiled scan-out. Pre-i965 always needs a
2294 * fence, whereas 965+ only requires a fence if using
2295 * framebuffer compression. For simplicity, we always install
2296 * a fence as the cost is not that onerous.
2297 */
9807216f
VK
2298 if (view.type == I915_GGTT_VIEW_NORMAL) {
2299 ret = i915_gem_object_get_fence(obj);
2300 if (ret == -EDEADLK) {
2301 /*
2302 * -EDEADLK means there are no free fences
2303 * no pending flips.
2304 *
2305 * This is propagated to atomic, but it uses
2306 * -EDEADLK to force a locking recovery, so
2307 * change the returned error to -EBUSY.
2308 */
2309 ret = -EBUSY;
2310 goto err_unpin;
2311 } else if (ret)
2312 goto err_unpin;
1690e1eb 2313
9807216f
VK
2314 i915_gem_object_pin_fence(obj);
2315 }
6b95a207 2316
d6dd6843 2317 intel_runtime_pm_put(dev_priv);
6b95a207 2318 return 0;
48b956c5
CW
2319
2320err_unpin:
f64b98cd 2321 i915_gem_object_unpin_from_display_plane(obj, &view);
b26a6b35 2322err_pm:
d6dd6843 2323 intel_runtime_pm_put(dev_priv);
48b956c5 2324 return ret;
6b95a207
KH
2325}
2326
fb4b8ce1 2327void intel_unpin_fb_obj(struct drm_framebuffer *fb, unsigned int rotation)
1690e1eb 2328{
82bc3b2d 2329 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
f64b98cd 2330 struct i915_ggtt_view view;
82bc3b2d 2331
ebcdd39e
MR
2332 WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
2333
3465c580 2334 intel_fill_fb_ggtt_view(&view, fb, rotation);
f64b98cd 2335
9807216f
VK
2336 if (view.type == I915_GGTT_VIEW_NORMAL)
2337 i915_gem_object_unpin_fence(obj);
2338
f64b98cd 2339 i915_gem_object_unpin_from_display_plane(obj, &view);
1690e1eb
CW
2340}
2341
29cf9491
VS
2342/*
2343 * Adjust the tile offset by moving the difference into
2344 * the x/y offsets.
2345 *
2346 * Input tile dimensions and pitch must already be
2347 * rotated to match x and y, and in pixel units.
2348 */
2349static u32 intel_adjust_tile_offset(int *x, int *y,
2350 unsigned int tile_width,
2351 unsigned int tile_height,
2352 unsigned int tile_size,
2353 unsigned int pitch_tiles,
2354 u32 old_offset,
2355 u32 new_offset)
2356{
2357 unsigned int tiles;
2358
2359 WARN_ON(old_offset & (tile_size - 1));
2360 WARN_ON(new_offset & (tile_size - 1));
2361 WARN_ON(new_offset > old_offset);
2362
2363 tiles = (old_offset - new_offset) / tile_size;
2364
2365 *y += tiles / pitch_tiles * tile_height;
2366 *x += tiles % pitch_tiles * tile_width;
2367
2368 return new_offset;
2369}
2370
8d0deca8
VS
2371/*
2372 * Computes the linear offset to the base tile and adjusts
2373 * x, y. bytes per pixel is assumed to be a power-of-two.
2374 *
2375 * In the 90/270 rotated case, x and y are assumed
2376 * to be already rotated to match the rotated GTT view, and
2377 * pitch is the tile_height aligned framebuffer height.
2378 */
4f2d9934
VS
2379u32 intel_compute_tile_offset(int *x, int *y,
2380 const struct drm_framebuffer *fb, int plane,
8d0deca8
VS
2381 unsigned int pitch,
2382 unsigned int rotation)
c2c75131 2383{
4f2d9934
VS
2384 const struct drm_i915_private *dev_priv = to_i915(fb->dev);
2385 uint64_t fb_modifier = fb->modifier[plane];
2386 unsigned int cpp = drm_format_plane_cpp(fb->pixel_format, plane);
29cf9491
VS
2387 u32 offset, offset_aligned, alignment;
2388
2389 alignment = intel_surf_alignment(dev_priv, fb_modifier);
2390 if (alignment)
2391 alignment--;
2392
b5c65338 2393 if (fb_modifier != DRM_FORMAT_MOD_NONE) {
8d0deca8
VS
2394 unsigned int tile_size, tile_width, tile_height;
2395 unsigned int tile_rows, tiles, pitch_tiles;
c2c75131 2396
d843310d 2397 tile_size = intel_tile_size(dev_priv);
8d0deca8
VS
2398 intel_tile_dims(dev_priv, &tile_width, &tile_height,
2399 fb_modifier, cpp);
2400
2401 if (intel_rotation_90_or_270(rotation)) {
2402 pitch_tiles = pitch / tile_height;
2403 swap(tile_width, tile_height);
2404 } else {
2405 pitch_tiles = pitch / (tile_width * cpp);
2406 }
d843310d
VS
2407
2408 tile_rows = *y / tile_height;
2409 *y %= tile_height;
c2c75131 2410
8d0deca8
VS
2411 tiles = *x / tile_width;
2412 *x %= tile_width;
bc752862 2413
29cf9491
VS
2414 offset = (tile_rows * pitch_tiles + tiles) * tile_size;
2415 offset_aligned = offset & ~alignment;
bc752862 2416
29cf9491
VS
2417 intel_adjust_tile_offset(x, y, tile_width, tile_height,
2418 tile_size, pitch_tiles,
2419 offset, offset_aligned);
2420 } else {
bc752862 2421 offset = *y * pitch + *x * cpp;
29cf9491
VS
2422 offset_aligned = offset & ~alignment;
2423
4e9a86b6
VS
2424 *y = (offset & alignment) / pitch;
2425 *x = ((offset & alignment) - *y * pitch) / cpp;
bc752862 2426 }
29cf9491
VS
2427
2428 return offset_aligned;
c2c75131
DV
2429}
2430
b35d63fa 2431static int i9xx_format_to_fourcc(int format)
46f297fb
JB
2432{
2433 switch (format) {
2434 case DISPPLANE_8BPP:
2435 return DRM_FORMAT_C8;
2436 case DISPPLANE_BGRX555:
2437 return DRM_FORMAT_XRGB1555;
2438 case DISPPLANE_BGRX565:
2439 return DRM_FORMAT_RGB565;
2440 default:
2441 case DISPPLANE_BGRX888:
2442 return DRM_FORMAT_XRGB8888;
2443 case DISPPLANE_RGBX888:
2444 return DRM_FORMAT_XBGR8888;
2445 case DISPPLANE_BGRX101010:
2446 return DRM_FORMAT_XRGB2101010;
2447 case DISPPLANE_RGBX101010:
2448 return DRM_FORMAT_XBGR2101010;
2449 }
2450}
2451
bc8d7dff
DL
2452static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
2453{
2454 switch (format) {
2455 case PLANE_CTL_FORMAT_RGB_565:
2456 return DRM_FORMAT_RGB565;
2457 default:
2458 case PLANE_CTL_FORMAT_XRGB_8888:
2459 if (rgb_order) {
2460 if (alpha)
2461 return DRM_FORMAT_ABGR8888;
2462 else
2463 return DRM_FORMAT_XBGR8888;
2464 } else {
2465 if (alpha)
2466 return DRM_FORMAT_ARGB8888;
2467 else
2468 return DRM_FORMAT_XRGB8888;
2469 }
2470 case PLANE_CTL_FORMAT_XRGB_2101010:
2471 if (rgb_order)
2472 return DRM_FORMAT_XBGR2101010;
2473 else
2474 return DRM_FORMAT_XRGB2101010;
2475 }
2476}
2477
5724dbd1 2478static bool
f6936e29
DV
2479intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
2480 struct intel_initial_plane_config *plane_config)
46f297fb
JB
2481{
2482 struct drm_device *dev = crtc->base.dev;
3badb49f 2483 struct drm_i915_private *dev_priv = to_i915(dev);
72e96d64 2484 struct i915_ggtt *ggtt = &dev_priv->ggtt;
46f297fb
JB
2485 struct drm_i915_gem_object *obj = NULL;
2486 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
2d14030b 2487 struct drm_framebuffer *fb = &plane_config->fb->base;
f37b5c2b
DV
2488 u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
2489 u32 size_aligned = round_up(plane_config->base + plane_config->size,
2490 PAGE_SIZE);
2491
2492 size_aligned -= base_aligned;
46f297fb 2493
ff2652ea
CW
2494 if (plane_config->size == 0)
2495 return false;
2496
3badb49f
PZ
2497 /* If the FB is too big, just don't use it since fbdev is not very
2498 * important and we should probably use that space with FBC or other
2499 * features. */
72e96d64 2500 if (size_aligned * 2 > ggtt->stolen_usable_size)
3badb49f
PZ
2501 return false;
2502
12c83d99
TU
2503 mutex_lock(&dev->struct_mutex);
2504
f37b5c2b
DV
2505 obj = i915_gem_object_create_stolen_for_preallocated(dev,
2506 base_aligned,
2507 base_aligned,
2508 size_aligned);
12c83d99
TU
2509 if (!obj) {
2510 mutex_unlock(&dev->struct_mutex);
484b41dd 2511 return false;
12c83d99 2512 }
46f297fb 2513
49af449b
DL
2514 obj->tiling_mode = plane_config->tiling;
2515 if (obj->tiling_mode == I915_TILING_X)
6bf129df 2516 obj->stride = fb->pitches[0];
46f297fb 2517
6bf129df
DL
2518 mode_cmd.pixel_format = fb->pixel_format;
2519 mode_cmd.width = fb->width;
2520 mode_cmd.height = fb->height;
2521 mode_cmd.pitches[0] = fb->pitches[0];
18c5247e
DV
2522 mode_cmd.modifier[0] = fb->modifier[0];
2523 mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
46f297fb 2524
6bf129df 2525 if (intel_framebuffer_init(dev, to_intel_framebuffer(fb),
484b41dd 2526 &mode_cmd, obj)) {
46f297fb
JB
2527 DRM_DEBUG_KMS("intel fb init failed\n");
2528 goto out_unref_obj;
2529 }
12c83d99 2530
46f297fb 2531 mutex_unlock(&dev->struct_mutex);
484b41dd 2532
f6936e29 2533 DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
484b41dd 2534 return true;
46f297fb
JB
2535
2536out_unref_obj:
2537 drm_gem_object_unreference(&obj->base);
2538 mutex_unlock(&dev->struct_mutex);
484b41dd
JB
2539 return false;
2540}
2541
5a21b665
DV
2542/* Update plane->state->fb to match plane->fb after driver-internal updates */
2543static void
2544update_state_fb(struct drm_plane *plane)
2545{
2546 if (plane->fb == plane->state->fb)
2547 return;
2548
2549 if (plane->state->fb)
2550 drm_framebuffer_unreference(plane->state->fb);
2551 plane->state->fb = plane->fb;
2552 if (plane->state->fb)
2553 drm_framebuffer_reference(plane->state->fb);
2554}
2555
5724dbd1 2556static void
f6936e29
DV
2557intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
2558 struct intel_initial_plane_config *plane_config)
484b41dd
JB
2559{
2560 struct drm_device *dev = intel_crtc->base.dev;
d9ceb816 2561 struct drm_i915_private *dev_priv = dev->dev_private;
484b41dd
JB
2562 struct drm_crtc *c;
2563 struct intel_crtc *i;
2ff8fde1 2564 struct drm_i915_gem_object *obj;
88595ac9 2565 struct drm_plane *primary = intel_crtc->base.primary;
be5651f2 2566 struct drm_plane_state *plane_state = primary->state;
200757f5
MR
2567 struct drm_crtc_state *crtc_state = intel_crtc->base.state;
2568 struct intel_plane *intel_plane = to_intel_plane(primary);
0a8d8a86
MR
2569 struct intel_plane_state *intel_state =
2570 to_intel_plane_state(plane_state);
88595ac9 2571 struct drm_framebuffer *fb;
484b41dd 2572
2d14030b 2573 if (!plane_config->fb)
484b41dd
JB
2574 return;
2575
f6936e29 2576 if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
88595ac9
DV
2577 fb = &plane_config->fb->base;
2578 goto valid_fb;
f55548b5 2579 }
484b41dd 2580
2d14030b 2581 kfree(plane_config->fb);
484b41dd
JB
2582
2583 /*
2584 * Failed to alloc the obj, check to see if we should share
2585 * an fb with another CRTC instead
2586 */
70e1e0ec 2587 for_each_crtc(dev, c) {
484b41dd
JB
2588 i = to_intel_crtc(c);
2589
2590 if (c == &intel_crtc->base)
2591 continue;
2592
2ff8fde1
MR
2593 if (!i->active)
2594 continue;
2595
88595ac9
DV
2596 fb = c->primary->fb;
2597 if (!fb)
484b41dd
JB
2598 continue;
2599
88595ac9 2600 obj = intel_fb_obj(fb);
2ff8fde1 2601 if (i915_gem_obj_ggtt_offset(obj) == plane_config->base) {
88595ac9
DV
2602 drm_framebuffer_reference(fb);
2603 goto valid_fb;
484b41dd
JB
2604 }
2605 }
88595ac9 2606
200757f5
MR
2607 /*
2608 * We've failed to reconstruct the BIOS FB. Current display state
2609 * indicates that the primary plane is visible, but has a NULL FB,
2610 * which will lead to problems later if we don't fix it up. The
2611 * simplest solution is to just disable the primary plane now and
2612 * pretend the BIOS never had it enabled.
2613 */
2614 to_intel_plane_state(plane_state)->visible = false;
2615 crtc_state->plane_mask &= ~(1 << drm_plane_index(primary));
2622a081 2616 intel_pre_disable_primary_noatomic(&intel_crtc->base);
200757f5
MR
2617 intel_plane->disable_plane(primary, &intel_crtc->base);
2618
88595ac9
DV
2619 return;
2620
2621valid_fb:
f44e2659
VS
2622 plane_state->src_x = 0;
2623 plane_state->src_y = 0;
be5651f2
ML
2624 plane_state->src_w = fb->width << 16;
2625 plane_state->src_h = fb->height << 16;
2626
f44e2659
VS
2627 plane_state->crtc_x = 0;
2628 plane_state->crtc_y = 0;
be5651f2
ML
2629 plane_state->crtc_w = fb->width;
2630 plane_state->crtc_h = fb->height;
2631
0a8d8a86
MR
2632 intel_state->src.x1 = plane_state->src_x;
2633 intel_state->src.y1 = plane_state->src_y;
2634 intel_state->src.x2 = plane_state->src_x + plane_state->src_w;
2635 intel_state->src.y2 = plane_state->src_y + plane_state->src_h;
2636 intel_state->dst.x1 = plane_state->crtc_x;
2637 intel_state->dst.y1 = plane_state->crtc_y;
2638 intel_state->dst.x2 = plane_state->crtc_x + plane_state->crtc_w;
2639 intel_state->dst.y2 = plane_state->crtc_y + plane_state->crtc_h;
2640
88595ac9
DV
2641 obj = intel_fb_obj(fb);
2642 if (obj->tiling_mode != I915_TILING_NONE)
2643 dev_priv->preserve_bios_swizzle = true;
2644
be5651f2
ML
2645 drm_framebuffer_reference(fb);
2646 primary->fb = primary->state->fb = fb;
36750f28 2647 primary->crtc = primary->state->crtc = &intel_crtc->base;
36750f28 2648 intel_crtc->base.state->plane_mask |= (1 << drm_plane_index(primary));
a9ff8714 2649 obj->frontbuffer_bits |= to_intel_plane(primary)->frontbuffer_bit;
46f297fb
JB
2650}
2651
a8d201af
ML
2652static void i9xx_update_primary_plane(struct drm_plane *primary,
2653 const struct intel_crtc_state *crtc_state,
2654 const struct intel_plane_state *plane_state)
81255565 2655{
a8d201af 2656 struct drm_device *dev = primary->dev;
81255565 2657 struct drm_i915_private *dev_priv = dev->dev_private;
a8d201af
ML
2658 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
2659 struct drm_framebuffer *fb = plane_state->base.fb;
2660 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
81255565 2661 int plane = intel_crtc->plane;
54ea9da8 2662 u32 linear_offset;
81255565 2663 u32 dspcntr;
f0f59a00 2664 i915_reg_t reg = DSPCNTR(plane);
8d0deca8 2665 unsigned int rotation = plane_state->base.rotation;
ac484963 2666 int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
54ea9da8
VS
2667 int x = plane_state->src.x1 >> 16;
2668 int y = plane_state->src.y1 >> 16;
c9ba6fad 2669
f45651ba
VS
2670 dspcntr = DISPPLANE_GAMMA_ENABLE;
2671
fdd508a6 2672 dspcntr |= DISPLAY_PLANE_ENABLE;
f45651ba
VS
2673
2674 if (INTEL_INFO(dev)->gen < 4) {
2675 if (intel_crtc->pipe == PIPE_B)
2676 dspcntr |= DISPPLANE_SEL_PIPE_B;
2677
2678 /* pipesrc and dspsize control the size that is scaled from,
2679 * which should always be the user's requested size.
2680 */
2681 I915_WRITE(DSPSIZE(plane),
a8d201af
ML
2682 ((crtc_state->pipe_src_h - 1) << 16) |
2683 (crtc_state->pipe_src_w - 1));
f45651ba 2684 I915_WRITE(DSPPOS(plane), 0);
c14b0485
VS
2685 } else if (IS_CHERRYVIEW(dev) && plane == PLANE_B) {
2686 I915_WRITE(PRIMSIZE(plane),
a8d201af
ML
2687 ((crtc_state->pipe_src_h - 1) << 16) |
2688 (crtc_state->pipe_src_w - 1));
c14b0485
VS
2689 I915_WRITE(PRIMPOS(plane), 0);
2690 I915_WRITE(PRIMCNSTALPHA(plane), 0);
f45651ba 2691 }
81255565 2692
57779d06
VS
2693 switch (fb->pixel_format) {
2694 case DRM_FORMAT_C8:
81255565
JB
2695 dspcntr |= DISPPLANE_8BPP;
2696 break;
57779d06 2697 case DRM_FORMAT_XRGB1555:
57779d06 2698 dspcntr |= DISPPLANE_BGRX555;
81255565 2699 break;
57779d06
VS
2700 case DRM_FORMAT_RGB565:
2701 dspcntr |= DISPPLANE_BGRX565;
2702 break;
2703 case DRM_FORMAT_XRGB8888:
57779d06
VS
2704 dspcntr |= DISPPLANE_BGRX888;
2705 break;
2706 case DRM_FORMAT_XBGR8888:
57779d06
VS
2707 dspcntr |= DISPPLANE_RGBX888;
2708 break;
2709 case DRM_FORMAT_XRGB2101010:
57779d06
VS
2710 dspcntr |= DISPPLANE_BGRX101010;
2711 break;
2712 case DRM_FORMAT_XBGR2101010:
57779d06 2713 dspcntr |= DISPPLANE_RGBX101010;
81255565
JB
2714 break;
2715 default:
baba133a 2716 BUG();
81255565 2717 }
57779d06 2718
f45651ba
VS
2719 if (INTEL_INFO(dev)->gen >= 4 &&
2720 obj->tiling_mode != I915_TILING_NONE)
2721 dspcntr |= DISPPLANE_TILED;
81255565 2722
de1aa629
VS
2723 if (IS_G4X(dev))
2724 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
2725
ac484963 2726 linear_offset = y * fb->pitches[0] + x * cpp;
81255565 2727
c2c75131
DV
2728 if (INTEL_INFO(dev)->gen >= 4) {
2729 intel_crtc->dspaddr_offset =
4f2d9934 2730 intel_compute_tile_offset(&x, &y, fb, 0,
8d0deca8 2731 fb->pitches[0], rotation);
c2c75131
DV
2732 linear_offset -= intel_crtc->dspaddr_offset;
2733 } else {
e506a0c6 2734 intel_crtc->dspaddr_offset = linear_offset;
c2c75131 2735 }
e506a0c6 2736
8d0deca8 2737 if (rotation == BIT(DRM_ROTATE_180)) {
48404c1e
SJ
2738 dspcntr |= DISPPLANE_ROTATE_180;
2739
a8d201af
ML
2740 x += (crtc_state->pipe_src_w - 1);
2741 y += (crtc_state->pipe_src_h - 1);
48404c1e
SJ
2742
2743 /* Finding the last pixel of the last line of the display
2744 data and adding to linear_offset*/
2745 linear_offset +=
a8d201af 2746 (crtc_state->pipe_src_h - 1) * fb->pitches[0] +
ac484963 2747 (crtc_state->pipe_src_w - 1) * cpp;
48404c1e
SJ
2748 }
2749
2db3366b
PZ
2750 intel_crtc->adjusted_x = x;
2751 intel_crtc->adjusted_y = y;
2752
48404c1e
SJ
2753 I915_WRITE(reg, dspcntr);
2754
01f2c773 2755 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
a6c45cf0 2756 if (INTEL_INFO(dev)->gen >= 4) {
85ba7b7d
DV
2757 I915_WRITE(DSPSURF(plane),
2758 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
5eddb70b 2759 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
e506a0c6 2760 I915_WRITE(DSPLINOFF(plane), linear_offset);
5eddb70b 2761 } else
f343c5f6 2762 I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
5eddb70b 2763 POSTING_READ(reg);
17638cd6
JB
2764}
2765
a8d201af
ML
2766static void i9xx_disable_primary_plane(struct drm_plane *primary,
2767 struct drm_crtc *crtc)
17638cd6
JB
2768{
2769 struct drm_device *dev = crtc->dev;
2770 struct drm_i915_private *dev_priv = dev->dev_private;
2771 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
17638cd6 2772 int plane = intel_crtc->plane;
f45651ba 2773
a8d201af
ML
2774 I915_WRITE(DSPCNTR(plane), 0);
2775 if (INTEL_INFO(dev_priv)->gen >= 4)
fdd508a6 2776 I915_WRITE(DSPSURF(plane), 0);
a8d201af
ML
2777 else
2778 I915_WRITE(DSPADDR(plane), 0);
2779 POSTING_READ(DSPCNTR(plane));
2780}
c9ba6fad 2781
a8d201af
ML
2782static void ironlake_update_primary_plane(struct drm_plane *primary,
2783 const struct intel_crtc_state *crtc_state,
2784 const struct intel_plane_state *plane_state)
2785{
2786 struct drm_device *dev = primary->dev;
2787 struct drm_i915_private *dev_priv = dev->dev_private;
2788 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
2789 struct drm_framebuffer *fb = plane_state->base.fb;
2790 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
2791 int plane = intel_crtc->plane;
54ea9da8 2792 u32 linear_offset;
a8d201af
ML
2793 u32 dspcntr;
2794 i915_reg_t reg = DSPCNTR(plane);
8d0deca8 2795 unsigned int rotation = plane_state->base.rotation;
ac484963 2796 int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
a8d201af
ML
2797 int x = plane_state->src.x1 >> 16;
2798 int y = plane_state->src.y1 >> 16;
c9ba6fad 2799
f45651ba 2800 dspcntr = DISPPLANE_GAMMA_ENABLE;
fdd508a6 2801 dspcntr |= DISPLAY_PLANE_ENABLE;
f45651ba
VS
2802
2803 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
2804 dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
17638cd6 2805
57779d06
VS
2806 switch (fb->pixel_format) {
2807 case DRM_FORMAT_C8:
17638cd6
JB
2808 dspcntr |= DISPPLANE_8BPP;
2809 break;
57779d06
VS
2810 case DRM_FORMAT_RGB565:
2811 dspcntr |= DISPPLANE_BGRX565;
17638cd6 2812 break;
57779d06 2813 case DRM_FORMAT_XRGB8888:
57779d06
VS
2814 dspcntr |= DISPPLANE_BGRX888;
2815 break;
2816 case DRM_FORMAT_XBGR8888:
57779d06
VS
2817 dspcntr |= DISPPLANE_RGBX888;
2818 break;
2819 case DRM_FORMAT_XRGB2101010:
57779d06
VS
2820 dspcntr |= DISPPLANE_BGRX101010;
2821 break;
2822 case DRM_FORMAT_XBGR2101010:
57779d06 2823 dspcntr |= DISPPLANE_RGBX101010;
17638cd6
JB
2824 break;
2825 default:
baba133a 2826 BUG();
17638cd6
JB
2827 }
2828
2829 if (obj->tiling_mode != I915_TILING_NONE)
2830 dspcntr |= DISPPLANE_TILED;
17638cd6 2831
f45651ba 2832 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev))
1f5d76db 2833 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
17638cd6 2834
ac484963 2835 linear_offset = y * fb->pitches[0] + x * cpp;
c2c75131 2836 intel_crtc->dspaddr_offset =
4f2d9934 2837 intel_compute_tile_offset(&x, &y, fb, 0,
8d0deca8 2838 fb->pitches[0], rotation);
c2c75131 2839 linear_offset -= intel_crtc->dspaddr_offset;
8d0deca8 2840 if (rotation == BIT(DRM_ROTATE_180)) {
48404c1e
SJ
2841 dspcntr |= DISPPLANE_ROTATE_180;
2842
2843 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev)) {
a8d201af
ML
2844 x += (crtc_state->pipe_src_w - 1);
2845 y += (crtc_state->pipe_src_h - 1);
48404c1e
SJ
2846
2847 /* Finding the last pixel of the last line of the display
2848 data and adding to linear_offset*/
2849 linear_offset +=
a8d201af 2850 (crtc_state->pipe_src_h - 1) * fb->pitches[0] +
ac484963 2851 (crtc_state->pipe_src_w - 1) * cpp;
48404c1e
SJ
2852 }
2853 }
2854
2db3366b
PZ
2855 intel_crtc->adjusted_x = x;
2856 intel_crtc->adjusted_y = y;
2857
48404c1e 2858 I915_WRITE(reg, dspcntr);
17638cd6 2859
01f2c773 2860 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
85ba7b7d
DV
2861 I915_WRITE(DSPSURF(plane),
2862 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
b3dc685e 2863 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
bc1c91eb
DL
2864 I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
2865 } else {
2866 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2867 I915_WRITE(DSPLINOFF(plane), linear_offset);
2868 }
17638cd6 2869 POSTING_READ(reg);
17638cd6
JB
2870}
2871
7b49f948
VS
2872u32 intel_fb_stride_alignment(const struct drm_i915_private *dev_priv,
2873 uint64_t fb_modifier, uint32_t pixel_format)
b321803d 2874{
7b49f948 2875 if (fb_modifier == DRM_FORMAT_MOD_NONE) {
b321803d 2876 return 64;
7b49f948
VS
2877 } else {
2878 int cpp = drm_format_plane_cpp(pixel_format, 0);
2879
27ba3910 2880 return intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
b321803d
DL
2881 }
2882}
2883
44eb0cb9
MK
2884u32 intel_plane_obj_offset(struct intel_plane *intel_plane,
2885 struct drm_i915_gem_object *obj,
2886 unsigned int plane)
121920fa 2887{
ce7f1728 2888 struct i915_ggtt_view view;
dedf278c 2889 struct i915_vma *vma;
44eb0cb9 2890 u64 offset;
121920fa 2891
e7941294 2892 intel_fill_fb_ggtt_view(&view, intel_plane->base.state->fb,
3465c580 2893 intel_plane->base.state->rotation);
121920fa 2894
ce7f1728 2895 vma = i915_gem_obj_to_ggtt_view(obj, &view);
dedf278c 2896 if (WARN(!vma, "ggtt vma for display object not found! (view=%u)\n",
ce7f1728 2897 view.type))
dedf278c
TU
2898 return -1;
2899
44eb0cb9 2900 offset = vma->node.start;
dedf278c
TU
2901
2902 if (plane == 1) {
7723f47d 2903 offset += vma->ggtt_view.params.rotated.uv_start_page *
dedf278c
TU
2904 PAGE_SIZE;
2905 }
2906
44eb0cb9
MK
2907 WARN_ON(upper_32_bits(offset));
2908
2909 return lower_32_bits(offset);
121920fa
TU
2910}
2911
e435d6e5
ML
2912static void skl_detach_scaler(struct intel_crtc *intel_crtc, int id)
2913{
2914 struct drm_device *dev = intel_crtc->base.dev;
2915 struct drm_i915_private *dev_priv = dev->dev_private;
2916
2917 I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, id), 0);
2918 I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, id), 0);
2919 I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, id), 0);
e435d6e5
ML
2920}
2921
a1b2278e
CK
2922/*
2923 * This function detaches (aka. unbinds) unused scalers in hardware
2924 */
0583236e 2925static void skl_detach_scalers(struct intel_crtc *intel_crtc)
a1b2278e 2926{
a1b2278e
CK
2927 struct intel_crtc_scaler_state *scaler_state;
2928 int i;
2929
a1b2278e
CK
2930 scaler_state = &intel_crtc->config->scaler_state;
2931
2932 /* loop through and disable scalers that aren't in use */
2933 for (i = 0; i < intel_crtc->num_scalers; i++) {
e435d6e5
ML
2934 if (!scaler_state->scalers[i].in_use)
2935 skl_detach_scaler(intel_crtc, i);
a1b2278e
CK
2936 }
2937}
2938
6156a456 2939u32 skl_plane_ctl_format(uint32_t pixel_format)
70d21f0e 2940{
6156a456 2941 switch (pixel_format) {
d161cf7a 2942 case DRM_FORMAT_C8:
c34ce3d1 2943 return PLANE_CTL_FORMAT_INDEXED;
70d21f0e 2944 case DRM_FORMAT_RGB565:
c34ce3d1 2945 return PLANE_CTL_FORMAT_RGB_565;
70d21f0e 2946 case DRM_FORMAT_XBGR8888:
c34ce3d1 2947 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
6156a456 2948 case DRM_FORMAT_XRGB8888:
c34ce3d1 2949 return PLANE_CTL_FORMAT_XRGB_8888;
6156a456
CK
2950 /*
2951 * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
2952 * to be already pre-multiplied. We need to add a knob (or a different
2953 * DRM_FORMAT) for user-space to configure that.
2954 */
f75fb42a 2955 case DRM_FORMAT_ABGR8888:
c34ce3d1 2956 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX |
6156a456 2957 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
6156a456 2958 case DRM_FORMAT_ARGB8888:
c34ce3d1 2959 return PLANE_CTL_FORMAT_XRGB_8888 |
6156a456 2960 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
70d21f0e 2961 case DRM_FORMAT_XRGB2101010:
c34ce3d1 2962 return PLANE_CTL_FORMAT_XRGB_2101010;
70d21f0e 2963 case DRM_FORMAT_XBGR2101010:
c34ce3d1 2964 return PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
6156a456 2965 case DRM_FORMAT_YUYV:
c34ce3d1 2966 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
6156a456 2967 case DRM_FORMAT_YVYU:
c34ce3d1 2968 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
6156a456 2969 case DRM_FORMAT_UYVY:
c34ce3d1 2970 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
6156a456 2971 case DRM_FORMAT_VYUY:
c34ce3d1 2972 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
70d21f0e 2973 default:
4249eeef 2974 MISSING_CASE(pixel_format);
70d21f0e 2975 }
8cfcba41 2976
c34ce3d1 2977 return 0;
6156a456 2978}
70d21f0e 2979
6156a456
CK
2980u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
2981{
6156a456 2982 switch (fb_modifier) {
30af77c4 2983 case DRM_FORMAT_MOD_NONE:
70d21f0e 2984 break;
30af77c4 2985 case I915_FORMAT_MOD_X_TILED:
c34ce3d1 2986 return PLANE_CTL_TILED_X;
b321803d 2987 case I915_FORMAT_MOD_Y_TILED:
c34ce3d1 2988 return PLANE_CTL_TILED_Y;
b321803d 2989 case I915_FORMAT_MOD_Yf_TILED:
c34ce3d1 2990 return PLANE_CTL_TILED_YF;
70d21f0e 2991 default:
6156a456 2992 MISSING_CASE(fb_modifier);
70d21f0e 2993 }
8cfcba41 2994
c34ce3d1 2995 return 0;
6156a456 2996}
70d21f0e 2997
6156a456
CK
2998u32 skl_plane_ctl_rotation(unsigned int rotation)
2999{
3b7a5119 3000 switch (rotation) {
6156a456
CK
3001 case BIT(DRM_ROTATE_0):
3002 break;
1e8df167
SJ
3003 /*
3004 * DRM_ROTATE_ is counter clockwise to stay compatible with Xrandr
3005 * while i915 HW rotation is clockwise, thats why this swapping.
3006 */
3b7a5119 3007 case BIT(DRM_ROTATE_90):
1e8df167 3008 return PLANE_CTL_ROTATE_270;
3b7a5119 3009 case BIT(DRM_ROTATE_180):
c34ce3d1 3010 return PLANE_CTL_ROTATE_180;
3b7a5119 3011 case BIT(DRM_ROTATE_270):
1e8df167 3012 return PLANE_CTL_ROTATE_90;
6156a456
CK
3013 default:
3014 MISSING_CASE(rotation);
3015 }
3016
c34ce3d1 3017 return 0;
6156a456
CK
3018}
3019
a8d201af
ML
3020static void skylake_update_primary_plane(struct drm_plane *plane,
3021 const struct intel_crtc_state *crtc_state,
3022 const struct intel_plane_state *plane_state)
6156a456 3023{
a8d201af 3024 struct drm_device *dev = plane->dev;
6156a456 3025 struct drm_i915_private *dev_priv = dev->dev_private;
a8d201af
ML
3026 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
3027 struct drm_framebuffer *fb = plane_state->base.fb;
3028 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
6156a456
CK
3029 int pipe = intel_crtc->pipe;
3030 u32 plane_ctl, stride_div, stride;
3031 u32 tile_height, plane_offset, plane_size;
a8d201af 3032 unsigned int rotation = plane_state->base.rotation;
6156a456 3033 int x_offset, y_offset;
44eb0cb9 3034 u32 surf_addr;
a8d201af
ML
3035 int scaler_id = plane_state->scaler_id;
3036 int src_x = plane_state->src.x1 >> 16;
3037 int src_y = plane_state->src.y1 >> 16;
3038 int src_w = drm_rect_width(&plane_state->src) >> 16;
3039 int src_h = drm_rect_height(&plane_state->src) >> 16;
3040 int dst_x = plane_state->dst.x1;
3041 int dst_y = plane_state->dst.y1;
3042 int dst_w = drm_rect_width(&plane_state->dst);
3043 int dst_h = drm_rect_height(&plane_state->dst);
70d21f0e 3044
6156a456
CK
3045 plane_ctl = PLANE_CTL_ENABLE |
3046 PLANE_CTL_PIPE_GAMMA_ENABLE |
3047 PLANE_CTL_PIPE_CSC_ENABLE;
3048
3049 plane_ctl |= skl_plane_ctl_format(fb->pixel_format);
3050 plane_ctl |= skl_plane_ctl_tiling(fb->modifier[0]);
3051 plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
6156a456
CK
3052 plane_ctl |= skl_plane_ctl_rotation(rotation);
3053
7b49f948 3054 stride_div = intel_fb_stride_alignment(dev_priv, fb->modifier[0],
b321803d 3055 fb->pixel_format);
dedf278c 3056 surf_addr = intel_plane_obj_offset(to_intel_plane(plane), obj, 0);
3b7a5119 3057
a42e5a23
PZ
3058 WARN_ON(drm_rect_width(&plane_state->src) == 0);
3059
3b7a5119 3060 if (intel_rotation_90_or_270(rotation)) {
832be82f
VS
3061 int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
3062
3b7a5119 3063 /* stride = Surface height in tiles */
832be82f 3064 tile_height = intel_tile_height(dev_priv, fb->modifier[0], cpp);
3b7a5119 3065 stride = DIV_ROUND_UP(fb->height, tile_height);
a8d201af
ML
3066 x_offset = stride * tile_height - src_y - src_h;
3067 y_offset = src_x;
6156a456 3068 plane_size = (src_w - 1) << 16 | (src_h - 1);
3b7a5119
SJ
3069 } else {
3070 stride = fb->pitches[0] / stride_div;
a8d201af
ML
3071 x_offset = src_x;
3072 y_offset = src_y;
6156a456 3073 plane_size = (src_h - 1) << 16 | (src_w - 1);
3b7a5119
SJ
3074 }
3075 plane_offset = y_offset << 16 | x_offset;
b321803d 3076
2db3366b
PZ
3077 intel_crtc->adjusted_x = x_offset;
3078 intel_crtc->adjusted_y = y_offset;
3079
70d21f0e 3080 I915_WRITE(PLANE_CTL(pipe, 0), plane_ctl);
3b7a5119
SJ
3081 I915_WRITE(PLANE_OFFSET(pipe, 0), plane_offset);
3082 I915_WRITE(PLANE_SIZE(pipe, 0), plane_size);
3083 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
6156a456
CK
3084
3085 if (scaler_id >= 0) {
3086 uint32_t ps_ctrl = 0;
3087
3088 WARN_ON(!dst_w || !dst_h);
3089 ps_ctrl = PS_SCALER_EN | PS_PLANE_SEL(0) |
3090 crtc_state->scaler_state.scalers[scaler_id].mode;
3091 I915_WRITE(SKL_PS_CTRL(pipe, scaler_id), ps_ctrl);
3092 I915_WRITE(SKL_PS_PWR_GATE(pipe, scaler_id), 0);
3093 I915_WRITE(SKL_PS_WIN_POS(pipe, scaler_id), (dst_x << 16) | dst_y);
3094 I915_WRITE(SKL_PS_WIN_SZ(pipe, scaler_id), (dst_w << 16) | dst_h);
3095 I915_WRITE(PLANE_POS(pipe, 0), 0);
3096 } else {
3097 I915_WRITE(PLANE_POS(pipe, 0), (dst_y << 16) | dst_x);
3098 }
3099
121920fa 3100 I915_WRITE(PLANE_SURF(pipe, 0), surf_addr);
70d21f0e
DL
3101
3102 POSTING_READ(PLANE_SURF(pipe, 0));
3103}
3104
a8d201af
ML
3105static void skylake_disable_primary_plane(struct drm_plane *primary,
3106 struct drm_crtc *crtc)
17638cd6
JB
3107{
3108 struct drm_device *dev = crtc->dev;
3109 struct drm_i915_private *dev_priv = dev->dev_private;
a8d201af 3110 int pipe = to_intel_crtc(crtc)->pipe;
17638cd6 3111
a8d201af
ML
3112 I915_WRITE(PLANE_CTL(pipe, 0), 0);
3113 I915_WRITE(PLANE_SURF(pipe, 0), 0);
3114 POSTING_READ(PLANE_SURF(pipe, 0));
3115}
29b9bde6 3116
a8d201af
ML
3117/* Assume fb object is pinned & idle & fenced and just update base pointers */
3118static int
3119intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
3120 int x, int y, enum mode_set_atomic state)
3121{
3122 /* Support for kgdboc is disabled, this needs a major rework. */
3123 DRM_ERROR("legacy panic handler not supported any more.\n");
3124
3125 return -ENODEV;
81255565
JB
3126}
3127
5a21b665
DV
3128static void intel_complete_page_flips(struct drm_i915_private *dev_priv)
3129{
3130 struct intel_crtc *crtc;
3131
3132 for_each_intel_crtc(dev_priv->dev, crtc)
3133 intel_finish_page_flip_cs(dev_priv, crtc->pipe);
3134}
3135
7514747d
VS
3136static void intel_update_primary_planes(struct drm_device *dev)
3137{
7514747d 3138 struct drm_crtc *crtc;
96a02917 3139
70e1e0ec 3140 for_each_crtc(dev, crtc) {
11c22da6
ML
3141 struct intel_plane *plane = to_intel_plane(crtc->primary);
3142 struct intel_plane_state *plane_state;
96a02917 3143
11c22da6 3144 drm_modeset_lock_crtc(crtc, &plane->base);
11c22da6
ML
3145 plane_state = to_intel_plane_state(plane->base.state);
3146
a8d201af
ML
3147 if (plane_state->visible)
3148 plane->update_plane(&plane->base,
3149 to_intel_crtc_state(crtc->state),
3150 plane_state);
11c22da6
ML
3151
3152 drm_modeset_unlock_crtc(crtc);
96a02917
VS
3153 }
3154}
3155
c033666a 3156void intel_prepare_reset(struct drm_i915_private *dev_priv)
7514747d
VS
3157{
3158 /* no reset support for gen2 */
c033666a 3159 if (IS_GEN2(dev_priv))
7514747d
VS
3160 return;
3161
3162 /* reset doesn't touch the display */
c033666a 3163 if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
7514747d
VS
3164 return;
3165
c033666a 3166 drm_modeset_lock_all(dev_priv->dev);
f98ce92f
VS
3167 /*
3168 * Disabling the crtcs gracefully seems nicer. Also the
3169 * g33 docs say we should at least disable all the planes.
3170 */
c033666a 3171 intel_display_suspend(dev_priv->dev);
7514747d
VS
3172}
3173
c033666a 3174void intel_finish_reset(struct drm_i915_private *dev_priv)
7514747d 3175{
5a21b665
DV
3176 /*
3177 * Flips in the rings will be nuked by the reset,
3178 * so complete all pending flips so that user space
3179 * will get its events and not get stuck.
3180 */
3181 intel_complete_page_flips(dev_priv);
3182
7514747d 3183 /* no reset support for gen2 */
c033666a 3184 if (IS_GEN2(dev_priv))
7514747d
VS
3185 return;
3186
3187 /* reset doesn't touch the display */
c033666a 3188 if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv)) {
7514747d
VS
3189 /*
3190 * Flips in the rings have been nuked by the reset,
3191 * so update the base address of all primary
3192 * planes to the the last fb to make sure we're
3193 * showing the correct fb after a reset.
11c22da6
ML
3194 *
3195 * FIXME: Atomic will make this obsolete since we won't schedule
3196 * CS-based flips (which might get lost in gpu resets) any more.
7514747d 3197 */
c033666a 3198 intel_update_primary_planes(dev_priv->dev);
7514747d
VS
3199 return;
3200 }
3201
3202 /*
3203 * The display has been reset as well,
3204 * so need a full re-initialization.
3205 */
3206 intel_runtime_pm_disable_interrupts(dev_priv);
3207 intel_runtime_pm_enable_interrupts(dev_priv);
3208
c033666a 3209 intel_modeset_init_hw(dev_priv->dev);
7514747d
VS
3210
3211 spin_lock_irq(&dev_priv->irq_lock);
3212 if (dev_priv->display.hpd_irq_setup)
91d14251 3213 dev_priv->display.hpd_irq_setup(dev_priv);
7514747d
VS
3214 spin_unlock_irq(&dev_priv->irq_lock);
3215
c033666a 3216 intel_display_resume(dev_priv->dev);
7514747d
VS
3217
3218 intel_hpd_init(dev_priv);
3219
c033666a 3220 drm_modeset_unlock_all(dev_priv->dev);
7514747d
VS
3221}
3222
7d5e3799
CW
3223static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
3224{
5a21b665
DV
3225 struct drm_device *dev = crtc->dev;
3226 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3227 unsigned reset_counter;
3228 bool pending;
3229
3230 reset_counter = i915_reset_counter(&to_i915(dev)->gpu_error);
3231 if (intel_crtc->reset_counter != reset_counter)
3232 return false;
3233
3234 spin_lock_irq(&dev->event_lock);
3235 pending = to_intel_crtc(crtc)->flip_work != NULL;
3236 spin_unlock_irq(&dev->event_lock);
3237
3238 return pending;
7d5e3799
CW
3239}
3240
bfd16b2a
ML
3241static void intel_update_pipe_config(struct intel_crtc *crtc,
3242 struct intel_crtc_state *old_crtc_state)
e30e8f75
GP
3243{
3244 struct drm_device *dev = crtc->base.dev;
3245 struct drm_i915_private *dev_priv = dev->dev_private;
bfd16b2a
ML
3246 struct intel_crtc_state *pipe_config =
3247 to_intel_crtc_state(crtc->base.state);
e30e8f75 3248
bfd16b2a
ML
3249 /* drm_atomic_helper_update_legacy_modeset_state might not be called. */
3250 crtc->base.mode = crtc->base.state->mode;
3251
3252 DRM_DEBUG_KMS("Updating pipe size %ix%i -> %ix%i\n",
3253 old_crtc_state->pipe_src_w, old_crtc_state->pipe_src_h,
3254 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
e30e8f75
GP
3255
3256 /*
3257 * Update pipe size and adjust fitter if needed: the reason for this is
3258 * that in compute_mode_changes we check the native mode (not the pfit
3259 * mode) to see if we can flip rather than do a full mode set. In the
3260 * fastboot case, we'll flip, but if we don't update the pipesrc and
3261 * pfit state, we'll end up with a big fb scanned out into the wrong
3262 * sized surface.
e30e8f75
GP
3263 */
3264
e30e8f75 3265 I915_WRITE(PIPESRC(crtc->pipe),
bfd16b2a
ML
3266 ((pipe_config->pipe_src_w - 1) << 16) |
3267 (pipe_config->pipe_src_h - 1));
3268
3269 /* on skylake this is done by detaching scalers */
3270 if (INTEL_INFO(dev)->gen >= 9) {
3271 skl_detach_scalers(crtc);
3272
3273 if (pipe_config->pch_pfit.enabled)
3274 skylake_pfit_enable(crtc);
3275 } else if (HAS_PCH_SPLIT(dev)) {
3276 if (pipe_config->pch_pfit.enabled)
3277 ironlake_pfit_enable(crtc);
3278 else if (old_crtc_state->pch_pfit.enabled)
3279 ironlake_pfit_disable(crtc, true);
e30e8f75 3280 }
e30e8f75
GP
3281}
3282
5e84e1a4
ZW
3283static void intel_fdi_normal_train(struct drm_crtc *crtc)
3284{
3285 struct drm_device *dev = crtc->dev;
3286 struct drm_i915_private *dev_priv = dev->dev_private;
3287 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3288 int pipe = intel_crtc->pipe;
f0f59a00
VS
3289 i915_reg_t reg;
3290 u32 temp;
5e84e1a4
ZW
3291
3292 /* enable normal train */
3293 reg = FDI_TX_CTL(pipe);
3294 temp = I915_READ(reg);
61e499bf 3295 if (IS_IVYBRIDGE(dev)) {
357555c0
JB
3296 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3297 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
61e499bf
KP
3298 } else {
3299 temp &= ~FDI_LINK_TRAIN_NONE;
3300 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
357555c0 3301 }
5e84e1a4
ZW
3302 I915_WRITE(reg, temp);
3303
3304 reg = FDI_RX_CTL(pipe);
3305 temp = I915_READ(reg);
3306 if (HAS_PCH_CPT(dev)) {
3307 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3308 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
3309 } else {
3310 temp &= ~FDI_LINK_TRAIN_NONE;
3311 temp |= FDI_LINK_TRAIN_NONE;
3312 }
3313 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
3314
3315 /* wait one idle pattern time */
3316 POSTING_READ(reg);
3317 udelay(1000);
357555c0
JB
3318
3319 /* IVB wants error correction enabled */
3320 if (IS_IVYBRIDGE(dev))
3321 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
3322 FDI_FE_ERRC_ENABLE);
5e84e1a4
ZW
3323}
3324
8db9d77b
ZW
3325/* The FDI link training functions for ILK/Ibexpeak. */
3326static void ironlake_fdi_link_train(struct drm_crtc *crtc)
3327{
3328 struct drm_device *dev = crtc->dev;
3329 struct drm_i915_private *dev_priv = dev->dev_private;
3330 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3331 int pipe = intel_crtc->pipe;
f0f59a00
VS
3332 i915_reg_t reg;
3333 u32 temp, tries;
8db9d77b 3334
1c8562f6 3335 /* FDI needs bits from pipe first */
0fc932b8 3336 assert_pipe_enabled(dev_priv, pipe);
0fc932b8 3337
e1a44743
AJ
3338 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3339 for train result */
5eddb70b
CW
3340 reg = FDI_RX_IMR(pipe);
3341 temp = I915_READ(reg);
e1a44743
AJ
3342 temp &= ~FDI_RX_SYMBOL_LOCK;
3343 temp &= ~FDI_RX_BIT_LOCK;
5eddb70b
CW
3344 I915_WRITE(reg, temp);
3345 I915_READ(reg);
e1a44743
AJ
3346 udelay(150);
3347
8db9d77b 3348 /* enable CPU FDI TX and PCH FDI RX */
5eddb70b
CW
3349 reg = FDI_TX_CTL(pipe);
3350 temp = I915_READ(reg);
627eb5a3 3351 temp &= ~FDI_DP_PORT_WIDTH_MASK;
6e3c9717 3352 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
8db9d77b
ZW
3353 temp &= ~FDI_LINK_TRAIN_NONE;
3354 temp |= FDI_LINK_TRAIN_PATTERN_1;
5eddb70b 3355 I915_WRITE(reg, temp | FDI_TX_ENABLE);
8db9d77b 3356
5eddb70b
CW
3357 reg = FDI_RX_CTL(pipe);
3358 temp = I915_READ(reg);
8db9d77b
ZW
3359 temp &= ~FDI_LINK_TRAIN_NONE;
3360 temp |= FDI_LINK_TRAIN_PATTERN_1;
5eddb70b
CW
3361 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3362
3363 POSTING_READ(reg);
8db9d77b
ZW
3364 udelay(150);
3365
5b2adf89 3366 /* Ironlake workaround, enable clock pointer after FDI enable*/
8f5718a6
DV
3367 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
3368 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
3369 FDI_RX_PHASE_SYNC_POINTER_EN);
5b2adf89 3370
5eddb70b 3371 reg = FDI_RX_IIR(pipe);
e1a44743 3372 for (tries = 0; tries < 5; tries++) {
5eddb70b 3373 temp = I915_READ(reg);
8db9d77b
ZW
3374 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3375
3376 if ((temp & FDI_RX_BIT_LOCK)) {
3377 DRM_DEBUG_KMS("FDI train 1 done.\n");
5eddb70b 3378 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
8db9d77b
ZW
3379 break;
3380 }
8db9d77b 3381 }
e1a44743 3382 if (tries == 5)
5eddb70b 3383 DRM_ERROR("FDI train 1 fail!\n");
8db9d77b
ZW
3384
3385 /* Train 2 */
5eddb70b
CW
3386 reg = FDI_TX_CTL(pipe);
3387 temp = I915_READ(reg);
8db9d77b
ZW
3388 temp &= ~FDI_LINK_TRAIN_NONE;
3389 temp |= FDI_LINK_TRAIN_PATTERN_2;
5eddb70b 3390 I915_WRITE(reg, temp);
8db9d77b 3391
5eddb70b
CW
3392 reg = FDI_RX_CTL(pipe);
3393 temp = I915_READ(reg);
8db9d77b
ZW
3394 temp &= ~FDI_LINK_TRAIN_NONE;
3395 temp |= FDI_LINK_TRAIN_PATTERN_2;
5eddb70b 3396 I915_WRITE(reg, temp);
8db9d77b 3397
5eddb70b
CW
3398 POSTING_READ(reg);
3399 udelay(150);
8db9d77b 3400
5eddb70b 3401 reg = FDI_RX_IIR(pipe);
e1a44743 3402 for (tries = 0; tries < 5; tries++) {
5eddb70b 3403 temp = I915_READ(reg);
8db9d77b
ZW
3404 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3405
3406 if (temp & FDI_RX_SYMBOL_LOCK) {
5eddb70b 3407 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
8db9d77b
ZW
3408 DRM_DEBUG_KMS("FDI train 2 done.\n");
3409 break;
3410 }
8db9d77b 3411 }
e1a44743 3412 if (tries == 5)
5eddb70b 3413 DRM_ERROR("FDI train 2 fail!\n");
8db9d77b
ZW
3414
3415 DRM_DEBUG_KMS("FDI train done\n");
5c5313c8 3416
8db9d77b
ZW
3417}
3418
0206e353 3419static const int snb_b_fdi_train_param[] = {
8db9d77b
ZW
3420 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
3421 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
3422 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
3423 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
3424};
3425
3426/* The FDI link training functions for SNB/Cougarpoint. */
3427static void gen6_fdi_link_train(struct drm_crtc *crtc)
3428{
3429 struct drm_device *dev = crtc->dev;
3430 struct drm_i915_private *dev_priv = dev->dev_private;
3431 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3432 int pipe = intel_crtc->pipe;
f0f59a00
VS
3433 i915_reg_t reg;
3434 u32 temp, i, retry;
8db9d77b 3435
e1a44743
AJ
3436 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3437 for train result */
5eddb70b
CW
3438 reg = FDI_RX_IMR(pipe);
3439 temp = I915_READ(reg);
e1a44743
AJ
3440 temp &= ~FDI_RX_SYMBOL_LOCK;
3441 temp &= ~FDI_RX_BIT_LOCK;
5eddb70b
CW
3442 I915_WRITE(reg, temp);
3443
3444 POSTING_READ(reg);
e1a44743
AJ
3445 udelay(150);
3446
8db9d77b 3447 /* enable CPU FDI TX and PCH FDI RX */
5eddb70b
CW
3448 reg = FDI_TX_CTL(pipe);
3449 temp = I915_READ(reg);
627eb5a3 3450 temp &= ~FDI_DP_PORT_WIDTH_MASK;
6e3c9717 3451 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
8db9d77b
ZW
3452 temp &= ~FDI_LINK_TRAIN_NONE;
3453 temp |= FDI_LINK_TRAIN_PATTERN_1;
3454 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3455 /* SNB-B */
3456 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
5eddb70b 3457 I915_WRITE(reg, temp | FDI_TX_ENABLE);
8db9d77b 3458
d74cf324
DV
3459 I915_WRITE(FDI_RX_MISC(pipe),
3460 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3461
5eddb70b
CW
3462 reg = FDI_RX_CTL(pipe);
3463 temp = I915_READ(reg);
8db9d77b
ZW
3464 if (HAS_PCH_CPT(dev)) {
3465 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3466 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3467 } else {
3468 temp &= ~FDI_LINK_TRAIN_NONE;
3469 temp |= FDI_LINK_TRAIN_PATTERN_1;
3470 }
5eddb70b
CW
3471 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3472
3473 POSTING_READ(reg);
8db9d77b
ZW
3474 udelay(150);
3475
0206e353 3476 for (i = 0; i < 4; i++) {
5eddb70b
CW
3477 reg = FDI_TX_CTL(pipe);
3478 temp = I915_READ(reg);
8db9d77b
ZW
3479 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3480 temp |= snb_b_fdi_train_param[i];
5eddb70b
CW
3481 I915_WRITE(reg, temp);
3482
3483 POSTING_READ(reg);
8db9d77b
ZW
3484 udelay(500);
3485
fa37d39e
SP
3486 for (retry = 0; retry < 5; retry++) {
3487 reg = FDI_RX_IIR(pipe);
3488 temp = I915_READ(reg);
3489 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3490 if (temp & FDI_RX_BIT_LOCK) {
3491 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3492 DRM_DEBUG_KMS("FDI train 1 done.\n");
3493 break;
3494 }
3495 udelay(50);
8db9d77b 3496 }
fa37d39e
SP
3497 if (retry < 5)
3498 break;
8db9d77b
ZW
3499 }
3500 if (i == 4)
5eddb70b 3501 DRM_ERROR("FDI train 1 fail!\n");
8db9d77b
ZW
3502
3503 /* Train 2 */
5eddb70b
CW
3504 reg = FDI_TX_CTL(pipe);
3505 temp = I915_READ(reg);
8db9d77b
ZW
3506 temp &= ~FDI_LINK_TRAIN_NONE;
3507 temp |= FDI_LINK_TRAIN_PATTERN_2;
3508 if (IS_GEN6(dev)) {
3509 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3510 /* SNB-B */
3511 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
3512 }
5eddb70b 3513 I915_WRITE(reg, temp);
8db9d77b 3514
5eddb70b
CW
3515 reg = FDI_RX_CTL(pipe);
3516 temp = I915_READ(reg);
8db9d77b
ZW
3517 if (HAS_PCH_CPT(dev)) {
3518 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3519 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
3520 } else {
3521 temp &= ~FDI_LINK_TRAIN_NONE;
3522 temp |= FDI_LINK_TRAIN_PATTERN_2;
3523 }
5eddb70b
CW
3524 I915_WRITE(reg, temp);
3525
3526 POSTING_READ(reg);
8db9d77b
ZW
3527 udelay(150);
3528
0206e353 3529 for (i = 0; i < 4; i++) {
5eddb70b
CW
3530 reg = FDI_TX_CTL(pipe);
3531 temp = I915_READ(reg);
8db9d77b
ZW
3532 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3533 temp |= snb_b_fdi_train_param[i];
5eddb70b
CW
3534 I915_WRITE(reg, temp);
3535
3536 POSTING_READ(reg);
8db9d77b
ZW
3537 udelay(500);
3538
fa37d39e
SP
3539 for (retry = 0; retry < 5; retry++) {
3540 reg = FDI_RX_IIR(pipe);
3541 temp = I915_READ(reg);
3542 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3543 if (temp & FDI_RX_SYMBOL_LOCK) {
3544 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3545 DRM_DEBUG_KMS("FDI train 2 done.\n");
3546 break;
3547 }
3548 udelay(50);
8db9d77b 3549 }
fa37d39e
SP
3550 if (retry < 5)
3551 break;
8db9d77b
ZW
3552 }
3553 if (i == 4)
5eddb70b 3554 DRM_ERROR("FDI train 2 fail!\n");
8db9d77b
ZW
3555
3556 DRM_DEBUG_KMS("FDI train done.\n");
3557}
3558
357555c0
JB
3559/* Manual link training for Ivy Bridge A0 parts */
3560static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
3561{
3562 struct drm_device *dev = crtc->dev;
3563 struct drm_i915_private *dev_priv = dev->dev_private;
3564 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3565 int pipe = intel_crtc->pipe;
f0f59a00
VS
3566 i915_reg_t reg;
3567 u32 temp, i, j;
357555c0
JB
3568
3569 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3570 for train result */
3571 reg = FDI_RX_IMR(pipe);
3572 temp = I915_READ(reg);
3573 temp &= ~FDI_RX_SYMBOL_LOCK;
3574 temp &= ~FDI_RX_BIT_LOCK;
3575 I915_WRITE(reg, temp);
3576
3577 POSTING_READ(reg);
3578 udelay(150);
3579
01a415fd
DV
3580 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
3581 I915_READ(FDI_RX_IIR(pipe)));
3582
139ccd3f
JB
3583 /* Try each vswing and preemphasis setting twice before moving on */
3584 for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
3585 /* disable first in case we need to retry */
3586 reg = FDI_TX_CTL(pipe);
3587 temp = I915_READ(reg);
3588 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
3589 temp &= ~FDI_TX_ENABLE;
3590 I915_WRITE(reg, temp);
357555c0 3591
139ccd3f
JB
3592 reg = FDI_RX_CTL(pipe);
3593 temp = I915_READ(reg);
3594 temp &= ~FDI_LINK_TRAIN_AUTO;
3595 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3596 temp &= ~FDI_RX_ENABLE;
3597 I915_WRITE(reg, temp);
357555c0 3598
139ccd3f 3599 /* enable CPU FDI TX and PCH FDI RX */
357555c0
JB
3600 reg = FDI_TX_CTL(pipe);
3601 temp = I915_READ(reg);
139ccd3f 3602 temp &= ~FDI_DP_PORT_WIDTH_MASK;
6e3c9717 3603 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
139ccd3f 3604 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
357555c0 3605 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
139ccd3f
JB
3606 temp |= snb_b_fdi_train_param[j/2];
3607 temp |= FDI_COMPOSITE_SYNC;
3608 I915_WRITE(reg, temp | FDI_TX_ENABLE);
357555c0 3609
139ccd3f
JB
3610 I915_WRITE(FDI_RX_MISC(pipe),
3611 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
357555c0 3612
139ccd3f 3613 reg = FDI_RX_CTL(pipe);
357555c0 3614 temp = I915_READ(reg);
139ccd3f
JB
3615 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3616 temp |= FDI_COMPOSITE_SYNC;
3617 I915_WRITE(reg, temp | FDI_RX_ENABLE);
357555c0 3618
139ccd3f
JB
3619 POSTING_READ(reg);
3620 udelay(1); /* should be 0.5us */
357555c0 3621
139ccd3f
JB
3622 for (i = 0; i < 4; i++) {
3623 reg = FDI_RX_IIR(pipe);
3624 temp = I915_READ(reg);
3625 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
357555c0 3626
139ccd3f
JB
3627 if (temp & FDI_RX_BIT_LOCK ||
3628 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
3629 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3630 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
3631 i);
3632 break;
3633 }
3634 udelay(1); /* should be 0.5us */
3635 }
3636 if (i == 4) {
3637 DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
3638 continue;
3639 }
357555c0 3640
139ccd3f 3641 /* Train 2 */
357555c0
JB
3642 reg = FDI_TX_CTL(pipe);
3643 temp = I915_READ(reg);
139ccd3f
JB
3644 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3645 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
3646 I915_WRITE(reg, temp);
3647
3648 reg = FDI_RX_CTL(pipe);
3649 temp = I915_READ(reg);
3650 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3651 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
357555c0
JB
3652 I915_WRITE(reg, temp);
3653
3654 POSTING_READ(reg);
139ccd3f 3655 udelay(2); /* should be 1.5us */
357555c0 3656
139ccd3f
JB
3657 for (i = 0; i < 4; i++) {
3658 reg = FDI_RX_IIR(pipe);
3659 temp = I915_READ(reg);
3660 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
357555c0 3661
139ccd3f
JB
3662 if (temp & FDI_RX_SYMBOL_LOCK ||
3663 (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
3664 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3665 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
3666 i);
3667 goto train_done;
3668 }
3669 udelay(2); /* should be 1.5us */
357555c0 3670 }
139ccd3f
JB
3671 if (i == 4)
3672 DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
357555c0 3673 }
357555c0 3674
139ccd3f 3675train_done:
357555c0
JB
3676 DRM_DEBUG_KMS("FDI train done.\n");
3677}
3678
88cefb6c 3679static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
2c07245f 3680{
88cefb6c 3681 struct drm_device *dev = intel_crtc->base.dev;
2c07245f 3682 struct drm_i915_private *dev_priv = dev->dev_private;
2c07245f 3683 int pipe = intel_crtc->pipe;
f0f59a00
VS
3684 i915_reg_t reg;
3685 u32 temp;
c64e311e 3686
c98e9dcf 3687 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
5eddb70b
CW
3688 reg = FDI_RX_CTL(pipe);
3689 temp = I915_READ(reg);
627eb5a3 3690 temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
6e3c9717 3691 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
dfd07d72 3692 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
5eddb70b
CW
3693 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
3694
3695 POSTING_READ(reg);
c98e9dcf
JB
3696 udelay(200);
3697
3698 /* Switch from Rawclk to PCDclk */
5eddb70b
CW
3699 temp = I915_READ(reg);
3700 I915_WRITE(reg, temp | FDI_PCDCLK);
3701
3702 POSTING_READ(reg);
c98e9dcf
JB
3703 udelay(200);
3704
20749730
PZ
3705 /* Enable CPU FDI TX PLL, always on for Ironlake */
3706 reg = FDI_TX_CTL(pipe);
3707 temp = I915_READ(reg);
3708 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
3709 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
5eddb70b 3710
20749730
PZ
3711 POSTING_READ(reg);
3712 udelay(100);
6be4a607 3713 }
0e23b99d
JB
3714}
3715
88cefb6c
DV
3716static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
3717{
3718 struct drm_device *dev = intel_crtc->base.dev;
3719 struct drm_i915_private *dev_priv = dev->dev_private;
3720 int pipe = intel_crtc->pipe;
f0f59a00
VS
3721 i915_reg_t reg;
3722 u32 temp;
88cefb6c
DV
3723
3724 /* Switch from PCDclk to Rawclk */
3725 reg = FDI_RX_CTL(pipe);
3726 temp = I915_READ(reg);
3727 I915_WRITE(reg, temp & ~FDI_PCDCLK);
3728
3729 /* Disable CPU FDI TX PLL */
3730 reg = FDI_TX_CTL(pipe);
3731 temp = I915_READ(reg);
3732 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
3733
3734 POSTING_READ(reg);
3735 udelay(100);
3736
3737 reg = FDI_RX_CTL(pipe);
3738 temp = I915_READ(reg);
3739 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
3740
3741 /* Wait for the clocks to turn off. */
3742 POSTING_READ(reg);
3743 udelay(100);
3744}
3745
0fc932b8
JB
3746static void ironlake_fdi_disable(struct drm_crtc *crtc)
3747{
3748 struct drm_device *dev = crtc->dev;
3749 struct drm_i915_private *dev_priv = dev->dev_private;
3750 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3751 int pipe = intel_crtc->pipe;
f0f59a00
VS
3752 i915_reg_t reg;
3753 u32 temp;
0fc932b8
JB
3754
3755 /* disable CPU FDI tx and PCH FDI rx */
3756 reg = FDI_TX_CTL(pipe);
3757 temp = I915_READ(reg);
3758 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
3759 POSTING_READ(reg);
3760
3761 reg = FDI_RX_CTL(pipe);
3762 temp = I915_READ(reg);
3763 temp &= ~(0x7 << 16);
dfd07d72 3764 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
0fc932b8
JB
3765 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
3766
3767 POSTING_READ(reg);
3768 udelay(100);
3769
3770 /* Ironlake workaround, disable clock pointer after downing FDI */
eba905b2 3771 if (HAS_PCH_IBX(dev))
6f06ce18 3772 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
0fc932b8
JB
3773
3774 /* still set train pattern 1 */
3775 reg = FDI_TX_CTL(pipe);
3776 temp = I915_READ(reg);
3777 temp &= ~FDI_LINK_TRAIN_NONE;
3778 temp |= FDI_LINK_TRAIN_PATTERN_1;
3779 I915_WRITE(reg, temp);
3780
3781 reg = FDI_RX_CTL(pipe);
3782 temp = I915_READ(reg);
3783 if (HAS_PCH_CPT(dev)) {
3784 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3785 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3786 } else {
3787 temp &= ~FDI_LINK_TRAIN_NONE;
3788 temp |= FDI_LINK_TRAIN_PATTERN_1;
3789 }
3790 /* BPC in FDI rx is consistent with that in PIPECONF */
3791 temp &= ~(0x07 << 16);
dfd07d72 3792 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
0fc932b8
JB
3793 I915_WRITE(reg, temp);
3794
3795 POSTING_READ(reg);
3796 udelay(100);
3797}
3798
5dce5b93
CW
3799bool intel_has_pending_fb_unpin(struct drm_device *dev)
3800{
3801 struct intel_crtc *crtc;
3802
3803 /* Note that we don't need to be called with mode_config.lock here
3804 * as our list of CRTC objects is static for the lifetime of the
3805 * device and so cannot disappear as we iterate. Similarly, we can
3806 * happily treat the predicates as racy, atomic checks as userspace
3807 * cannot claim and pin a new fb without at least acquring the
3808 * struct_mutex and so serialising with us.
3809 */
d3fcc808 3810 for_each_intel_crtc(dev, crtc) {
5dce5b93
CW
3811 if (atomic_read(&crtc->unpin_work_count) == 0)
3812 continue;
3813
5a21b665 3814 if (crtc->flip_work)
5dce5b93
CW
3815 intel_wait_for_vblank(dev, crtc->pipe);
3816
3817 return true;
3818 }
3819
3820 return false;
3821}
3822
5a21b665 3823static void page_flip_completed(struct intel_crtc *intel_crtc)
d6bbafa1
CW
3824{
3825 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
5a21b665
DV
3826 struct intel_flip_work *work = intel_crtc->flip_work;
3827
3828 intel_crtc->flip_work = NULL;
d6bbafa1
CW
3829
3830 if (work->event)
560ce1dc 3831 drm_crtc_send_vblank_event(&intel_crtc->base, work->event);
d6bbafa1
CW
3832
3833 drm_crtc_vblank_put(&intel_crtc->base);
3834
5a21b665 3835 wake_up_all(&dev_priv->pending_flip_queue);
143f73b3 3836 queue_work(dev_priv->wq, &work->unpin_work);
5a21b665
DV
3837
3838 trace_i915_flip_complete(intel_crtc->plane,
3839 work->pending_flip_obj);
d6bbafa1
CW
3840}
3841
5008e874 3842static int intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
e6c3a2a6 3843{
0f91128d 3844 struct drm_device *dev = crtc->dev;
5bb61643 3845 struct drm_i915_private *dev_priv = dev->dev_private;
5008e874 3846 long ret;
e6c3a2a6 3847
2c10d571 3848 WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
5008e874
ML
3849
3850 ret = wait_event_interruptible_timeout(
3851 dev_priv->pending_flip_queue,
3852 !intel_crtc_has_pending_flip(crtc),
3853 60*HZ);
3854
3855 if (ret < 0)
3856 return ret;
3857
5a21b665
DV
3858 if (ret == 0) {
3859 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3860 struct intel_flip_work *work;
3861
3862 spin_lock_irq(&dev->event_lock);
3863 work = intel_crtc->flip_work;
3864 if (work && !is_mmio_work(work)) {
3865 WARN_ONCE(1, "Removing stuck page flip\n");
3866 page_flip_completed(intel_crtc);
3867 }
3868 spin_unlock_irq(&dev->event_lock);
3869 }
5bb61643 3870
5008e874 3871 return 0;
e6c3a2a6
CW
3872}
3873
060f02d8
VS
3874static void lpt_disable_iclkip(struct drm_i915_private *dev_priv)
3875{
3876 u32 temp;
3877
3878 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
3879
3880 mutex_lock(&dev_priv->sb_lock);
3881
3882 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
3883 temp |= SBI_SSCCTL_DISABLE;
3884 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
3885
3886 mutex_unlock(&dev_priv->sb_lock);
3887}
3888
e615efe4
ED
3889/* Program iCLKIP clock to the desired frequency */
3890static void lpt_program_iclkip(struct drm_crtc *crtc)
3891{
64b46a06 3892 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
6e3c9717 3893 int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
e615efe4
ED
3894 u32 divsel, phaseinc, auxdiv, phasedir = 0;
3895 u32 temp;
3896
060f02d8 3897 lpt_disable_iclkip(dev_priv);
e615efe4 3898
64b46a06
VS
3899 /* The iCLK virtual clock root frequency is in MHz,
3900 * but the adjusted_mode->crtc_clock in in KHz. To get the
3901 * divisors, it is necessary to divide one by another, so we
3902 * convert the virtual clock precision to KHz here for higher
3903 * precision.
3904 */
3905 for (auxdiv = 0; auxdiv < 2; auxdiv++) {
e615efe4
ED
3906 u32 iclk_virtual_root_freq = 172800 * 1000;
3907 u32 iclk_pi_range = 64;
64b46a06 3908 u32 desired_divisor;
e615efe4 3909
64b46a06
VS
3910 desired_divisor = DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
3911 clock << auxdiv);
3912 divsel = (desired_divisor / iclk_pi_range) - 2;
3913 phaseinc = desired_divisor % iclk_pi_range;
e615efe4 3914
64b46a06
VS
3915 /*
3916 * Near 20MHz is a corner case which is
3917 * out of range for the 7-bit divisor
3918 */
3919 if (divsel <= 0x7f)
3920 break;
e615efe4
ED
3921 }
3922
3923 /* This should not happen with any sane values */
3924 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
3925 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
3926 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
3927 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
3928
3929 DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
12d7ceed 3930 clock,
e615efe4
ED
3931 auxdiv,
3932 divsel,
3933 phasedir,
3934 phaseinc);
3935
060f02d8
VS
3936 mutex_lock(&dev_priv->sb_lock);
3937
e615efe4 3938 /* Program SSCDIVINTPHASE6 */
988d6ee8 3939 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
e615efe4
ED
3940 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
3941 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
3942 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
3943 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
3944 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
3945 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
988d6ee8 3946 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
e615efe4
ED
3947
3948 /* Program SSCAUXDIV */
988d6ee8 3949 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
e615efe4
ED
3950 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
3951 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
988d6ee8 3952 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
e615efe4
ED
3953
3954 /* Enable modulator and associated divider */
988d6ee8 3955 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
e615efe4 3956 temp &= ~SBI_SSCCTL_DISABLE;
988d6ee8 3957 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
e615efe4 3958
060f02d8
VS
3959 mutex_unlock(&dev_priv->sb_lock);
3960
e615efe4
ED
3961 /* Wait for initialization time */
3962 udelay(24);
3963
3964 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
3965}
3966
8802e5b6
VS
3967int lpt_get_iclkip(struct drm_i915_private *dev_priv)
3968{
3969 u32 divsel, phaseinc, auxdiv;
3970 u32 iclk_virtual_root_freq = 172800 * 1000;
3971 u32 iclk_pi_range = 64;
3972 u32 desired_divisor;
3973 u32 temp;
3974
3975 if ((I915_READ(PIXCLK_GATE) & PIXCLK_GATE_UNGATE) == 0)
3976 return 0;
3977
3978 mutex_lock(&dev_priv->sb_lock);
3979
3980 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
3981 if (temp & SBI_SSCCTL_DISABLE) {
3982 mutex_unlock(&dev_priv->sb_lock);
3983 return 0;
3984 }
3985
3986 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
3987 divsel = (temp & SBI_SSCDIVINTPHASE_DIVSEL_MASK) >>
3988 SBI_SSCDIVINTPHASE_DIVSEL_SHIFT;
3989 phaseinc = (temp & SBI_SSCDIVINTPHASE_INCVAL_MASK) >>
3990 SBI_SSCDIVINTPHASE_INCVAL_SHIFT;
3991
3992 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
3993 auxdiv = (temp & SBI_SSCAUXDIV_FINALDIV2SEL_MASK) >>
3994 SBI_SSCAUXDIV_FINALDIV2SEL_SHIFT;
3995
3996 mutex_unlock(&dev_priv->sb_lock);
3997
3998 desired_divisor = (divsel + 2) * iclk_pi_range + phaseinc;
3999
4000 return DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
4001 desired_divisor << auxdiv);
4002}
4003
275f01b2
DV
4004static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
4005 enum pipe pch_transcoder)
4006{
4007 struct drm_device *dev = crtc->base.dev;
4008 struct drm_i915_private *dev_priv = dev->dev_private;
6e3c9717 4009 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
275f01b2
DV
4010
4011 I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
4012 I915_READ(HTOTAL(cpu_transcoder)));
4013 I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
4014 I915_READ(HBLANK(cpu_transcoder)));
4015 I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
4016 I915_READ(HSYNC(cpu_transcoder)));
4017
4018 I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
4019 I915_READ(VTOTAL(cpu_transcoder)));
4020 I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
4021 I915_READ(VBLANK(cpu_transcoder)));
4022 I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
4023 I915_READ(VSYNC(cpu_transcoder)));
4024 I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
4025 I915_READ(VSYNCSHIFT(cpu_transcoder)));
4026}
4027
003632d9 4028static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
1fbc0d78
DV
4029{
4030 struct drm_i915_private *dev_priv = dev->dev_private;
4031 uint32_t temp;
4032
4033 temp = I915_READ(SOUTH_CHICKEN1);
003632d9 4034 if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
1fbc0d78
DV
4035 return;
4036
4037 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
4038 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
4039
003632d9
ACO
4040 temp &= ~FDI_BC_BIFURCATION_SELECT;
4041 if (enable)
4042 temp |= FDI_BC_BIFURCATION_SELECT;
4043
4044 DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
1fbc0d78
DV
4045 I915_WRITE(SOUTH_CHICKEN1, temp);
4046 POSTING_READ(SOUTH_CHICKEN1);
4047}
4048
4049static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
4050{
4051 struct drm_device *dev = intel_crtc->base.dev;
1fbc0d78
DV
4052
4053 switch (intel_crtc->pipe) {
4054 case PIPE_A:
4055 break;
4056 case PIPE_B:
6e3c9717 4057 if (intel_crtc->config->fdi_lanes > 2)
003632d9 4058 cpt_set_fdi_bc_bifurcation(dev, false);
1fbc0d78 4059 else
003632d9 4060 cpt_set_fdi_bc_bifurcation(dev, true);
1fbc0d78
DV
4061
4062 break;
4063 case PIPE_C:
003632d9 4064 cpt_set_fdi_bc_bifurcation(dev, true);
1fbc0d78
DV
4065
4066 break;
4067 default:
4068 BUG();
4069 }
4070}
4071
c48b5305
VS
4072/* Return which DP Port should be selected for Transcoder DP control */
4073static enum port
4074intel_trans_dp_port_sel(struct drm_crtc *crtc)
4075{
4076 struct drm_device *dev = crtc->dev;
4077 struct intel_encoder *encoder;
4078
4079 for_each_encoder_on_crtc(dev, crtc, encoder) {
4080 if (encoder->type == INTEL_OUTPUT_DISPLAYPORT ||
4081 encoder->type == INTEL_OUTPUT_EDP)
4082 return enc_to_dig_port(&encoder->base)->port;
4083 }
4084
4085 return -1;
4086}
4087
f67a559d
JB
4088/*
4089 * Enable PCH resources required for PCH ports:
4090 * - PCH PLLs
4091 * - FDI training & RX/TX
4092 * - update transcoder timings
4093 * - DP transcoding bits
4094 * - transcoder
4095 */
4096static void ironlake_pch_enable(struct drm_crtc *crtc)
0e23b99d
JB
4097{
4098 struct drm_device *dev = crtc->dev;
4099 struct drm_i915_private *dev_priv = dev->dev_private;
4100 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4101 int pipe = intel_crtc->pipe;
f0f59a00 4102 u32 temp;
2c07245f 4103
ab9412ba 4104 assert_pch_transcoder_disabled(dev_priv, pipe);
e7e164db 4105
1fbc0d78
DV
4106 if (IS_IVYBRIDGE(dev))
4107 ivybridge_update_fdi_bc_bifurcation(intel_crtc);
4108
cd986abb
DV
4109 /* Write the TU size bits before fdi link training, so that error
4110 * detection works. */
4111 I915_WRITE(FDI_RX_TUSIZE1(pipe),
4112 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
4113
c98e9dcf 4114 /* For PCH output, training FDI link */
674cf967 4115 dev_priv->display.fdi_link_train(crtc);
2c07245f 4116
3ad8a208
DV
4117 /* We need to program the right clock selection before writing the pixel
4118 * mutliplier into the DPLL. */
303b81e0 4119 if (HAS_PCH_CPT(dev)) {
ee7b9f93 4120 u32 sel;
4b645f14 4121
c98e9dcf 4122 temp = I915_READ(PCH_DPLL_SEL);
11887397
DV
4123 temp |= TRANS_DPLL_ENABLE(pipe);
4124 sel = TRANS_DPLLB_SEL(pipe);
8106ddbd
ACO
4125 if (intel_crtc->config->shared_dpll ==
4126 intel_get_shared_dpll_by_id(dev_priv, DPLL_ID_PCH_PLL_B))
ee7b9f93
JB
4127 temp |= sel;
4128 else
4129 temp &= ~sel;
c98e9dcf 4130 I915_WRITE(PCH_DPLL_SEL, temp);
c98e9dcf 4131 }
5eddb70b 4132
3ad8a208
DV
4133 /* XXX: pch pll's can be enabled any time before we enable the PCH
4134 * transcoder, and we actually should do this to not upset any PCH
4135 * transcoder that already use the clock when we share it.
4136 *
4137 * Note that enable_shared_dpll tries to do the right thing, but
4138 * get_shared_dpll unconditionally resets the pll - we need that to have
4139 * the right LVDS enable sequence. */
85b3894f 4140 intel_enable_shared_dpll(intel_crtc);
3ad8a208 4141
d9b6cb56
JB
4142 /* set transcoder timing, panel must allow it */
4143 assert_panel_unlocked(dev_priv, pipe);
275f01b2 4144 ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
8db9d77b 4145
303b81e0 4146 intel_fdi_normal_train(crtc);
5e84e1a4 4147
c98e9dcf 4148 /* For PCH DP, enable TRANS_DP_CTL */
6e3c9717 4149 if (HAS_PCH_CPT(dev) && intel_crtc->config->has_dp_encoder) {
9c4edaee
VS
4150 const struct drm_display_mode *adjusted_mode =
4151 &intel_crtc->config->base.adjusted_mode;
dfd07d72 4152 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
f0f59a00 4153 i915_reg_t reg = TRANS_DP_CTL(pipe);
5eddb70b
CW
4154 temp = I915_READ(reg);
4155 temp &= ~(TRANS_DP_PORT_SEL_MASK |
220cad3c
EA
4156 TRANS_DP_SYNC_MASK |
4157 TRANS_DP_BPC_MASK);
e3ef4479 4158 temp |= TRANS_DP_OUTPUT_ENABLE;
9325c9f0 4159 temp |= bpc << 9; /* same format but at 11:9 */
c98e9dcf 4160
9c4edaee 4161 if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
5eddb70b 4162 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
9c4edaee 4163 if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
5eddb70b 4164 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
c98e9dcf
JB
4165
4166 switch (intel_trans_dp_port_sel(crtc)) {
c48b5305 4167 case PORT_B:
5eddb70b 4168 temp |= TRANS_DP_PORT_SEL_B;
c98e9dcf 4169 break;
c48b5305 4170 case PORT_C:
5eddb70b 4171 temp |= TRANS_DP_PORT_SEL_C;
c98e9dcf 4172 break;
c48b5305 4173 case PORT_D:
5eddb70b 4174 temp |= TRANS_DP_PORT_SEL_D;
c98e9dcf
JB
4175 break;
4176 default:
e95d41e1 4177 BUG();
32f9d658 4178 }
2c07245f 4179
5eddb70b 4180 I915_WRITE(reg, temp);
6be4a607 4181 }
b52eb4dc 4182
b8a4f404 4183 ironlake_enable_pch_transcoder(dev_priv, pipe);
f67a559d
JB
4184}
4185
1507e5bd
PZ
4186static void lpt_pch_enable(struct drm_crtc *crtc)
4187{
4188 struct drm_device *dev = crtc->dev;
4189 struct drm_i915_private *dev_priv = dev->dev_private;
4190 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6e3c9717 4191 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
1507e5bd 4192
ab9412ba 4193 assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
1507e5bd 4194
8c52b5e8 4195 lpt_program_iclkip(crtc);
1507e5bd 4196
0540e488 4197 /* Set transcoder timing. */
275f01b2 4198 ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
1507e5bd 4199
937bb610 4200 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
f67a559d
JB
4201}
4202
a1520318 4203static void cpt_verify_modeset(struct drm_device *dev, int pipe)
d4270e57
JB
4204{
4205 struct drm_i915_private *dev_priv = dev->dev_private;
f0f59a00 4206 i915_reg_t dslreg = PIPEDSL(pipe);
d4270e57
JB
4207 u32 temp;
4208
4209 temp = I915_READ(dslreg);
4210 udelay(500);
4211 if (wait_for(I915_READ(dslreg) != temp, 5)) {
d4270e57 4212 if (wait_for(I915_READ(dslreg) != temp, 5))
84f44ce7 4213 DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
d4270e57
JB
4214 }
4215}
4216
86adf9d7
ML
4217static int
4218skl_update_scaler(struct intel_crtc_state *crtc_state, bool force_detach,
4219 unsigned scaler_user, int *scaler_id, unsigned int rotation,
4220 int src_w, int src_h, int dst_w, int dst_h)
a1b2278e 4221{
86adf9d7
ML
4222 struct intel_crtc_scaler_state *scaler_state =
4223 &crtc_state->scaler_state;
4224 struct intel_crtc *intel_crtc =
4225 to_intel_crtc(crtc_state->base.crtc);
a1b2278e 4226 int need_scaling;
6156a456
CK
4227
4228 need_scaling = intel_rotation_90_or_270(rotation) ?
4229 (src_h != dst_w || src_w != dst_h):
4230 (src_w != dst_w || src_h != dst_h);
a1b2278e
CK
4231
4232 /*
4233 * if plane is being disabled or scaler is no more required or force detach
4234 * - free scaler binded to this plane/crtc
4235 * - in order to do this, update crtc->scaler_usage
4236 *
4237 * Here scaler state in crtc_state is set free so that
4238 * scaler can be assigned to other user. Actual register
4239 * update to free the scaler is done in plane/panel-fit programming.
4240 * For this purpose crtc/plane_state->scaler_id isn't reset here.
4241 */
86adf9d7 4242 if (force_detach || !need_scaling) {
a1b2278e 4243 if (*scaler_id >= 0) {
86adf9d7 4244 scaler_state->scaler_users &= ~(1 << scaler_user);
a1b2278e
CK
4245 scaler_state->scalers[*scaler_id].in_use = 0;
4246
86adf9d7
ML
4247 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4248 "Staged freeing scaler id %d scaler_users = 0x%x\n",
4249 intel_crtc->pipe, scaler_user, *scaler_id,
a1b2278e
CK
4250 scaler_state->scaler_users);
4251 *scaler_id = -1;
4252 }
4253 return 0;
4254 }
4255
4256 /* range checks */
4257 if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
4258 dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
4259
4260 src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
4261 dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
86adf9d7 4262 DRM_DEBUG_KMS("scaler_user index %u.%u: src %ux%u dst %ux%u "
a1b2278e 4263 "size is out of scaler range\n",
86adf9d7 4264 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h);
a1b2278e
CK
4265 return -EINVAL;
4266 }
4267
86adf9d7
ML
4268 /* mark this plane as a scaler user in crtc_state */
4269 scaler_state->scaler_users |= (1 << scaler_user);
4270 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4271 "staged scaling request for %ux%u->%ux%u scaler_users = 0x%x\n",
4272 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h,
4273 scaler_state->scaler_users);
4274
4275 return 0;
4276}
4277
4278/**
4279 * skl_update_scaler_crtc - Stages update to scaler state for a given crtc.
4280 *
4281 * @state: crtc's scaler state
86adf9d7
ML
4282 *
4283 * Return
4284 * 0 - scaler_usage updated successfully
4285 * error - requested scaling cannot be supported or other error condition
4286 */
e435d6e5 4287int skl_update_scaler_crtc(struct intel_crtc_state *state)
86adf9d7
ML
4288{
4289 struct intel_crtc *intel_crtc = to_intel_crtc(state->base.crtc);
7c5f93b0 4290 const struct drm_display_mode *adjusted_mode = &state->base.adjusted_mode;
86adf9d7 4291
78108b7c
VS
4292 DRM_DEBUG_KMS("Updating scaler for [CRTC:%d:%s] scaler_user index %u.%u\n",
4293 intel_crtc->base.base.id, intel_crtc->base.name,
4294 intel_crtc->pipe, SKL_CRTC_INDEX);
86adf9d7 4295
e435d6e5 4296 return skl_update_scaler(state, !state->base.active, SKL_CRTC_INDEX,
fa5a7970 4297 &state->scaler_state.scaler_id, BIT(DRM_ROTATE_0),
86adf9d7 4298 state->pipe_src_w, state->pipe_src_h,
aad941d5 4299 adjusted_mode->crtc_hdisplay, adjusted_mode->crtc_vdisplay);
86adf9d7
ML
4300}
4301
4302/**
4303 * skl_update_scaler_plane - Stages update to scaler state for a given plane.
4304 *
4305 * @state: crtc's scaler state
86adf9d7
ML
4306 * @plane_state: atomic plane state to update
4307 *
4308 * Return
4309 * 0 - scaler_usage updated successfully
4310 * error - requested scaling cannot be supported or other error condition
4311 */
da20eabd
ML
4312static int skl_update_scaler_plane(struct intel_crtc_state *crtc_state,
4313 struct intel_plane_state *plane_state)
86adf9d7
ML
4314{
4315
4316 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
da20eabd
ML
4317 struct intel_plane *intel_plane =
4318 to_intel_plane(plane_state->base.plane);
86adf9d7
ML
4319 struct drm_framebuffer *fb = plane_state->base.fb;
4320 int ret;
4321
4322 bool force_detach = !fb || !plane_state->visible;
4323
72660ce0
VS
4324 DRM_DEBUG_KMS("Updating scaler for [PLANE:%d:%s] scaler_user index %u.%u\n",
4325 intel_plane->base.base.id, intel_plane->base.name,
4326 intel_crtc->pipe, drm_plane_index(&intel_plane->base));
86adf9d7
ML
4327
4328 ret = skl_update_scaler(crtc_state, force_detach,
4329 drm_plane_index(&intel_plane->base),
4330 &plane_state->scaler_id,
4331 plane_state->base.rotation,
4332 drm_rect_width(&plane_state->src) >> 16,
4333 drm_rect_height(&plane_state->src) >> 16,
4334 drm_rect_width(&plane_state->dst),
4335 drm_rect_height(&plane_state->dst));
4336
4337 if (ret || plane_state->scaler_id < 0)
4338 return ret;
4339
a1b2278e 4340 /* check colorkey */
818ed961 4341 if (plane_state->ckey.flags != I915_SET_COLORKEY_NONE) {
72660ce0
VS
4342 DRM_DEBUG_KMS("[PLANE:%d:%s] scaling with color key not allowed",
4343 intel_plane->base.base.id,
4344 intel_plane->base.name);
a1b2278e
CK
4345 return -EINVAL;
4346 }
4347
4348 /* Check src format */
86adf9d7
ML
4349 switch (fb->pixel_format) {
4350 case DRM_FORMAT_RGB565:
4351 case DRM_FORMAT_XBGR8888:
4352 case DRM_FORMAT_XRGB8888:
4353 case DRM_FORMAT_ABGR8888:
4354 case DRM_FORMAT_ARGB8888:
4355 case DRM_FORMAT_XRGB2101010:
4356 case DRM_FORMAT_XBGR2101010:
4357 case DRM_FORMAT_YUYV:
4358 case DRM_FORMAT_YVYU:
4359 case DRM_FORMAT_UYVY:
4360 case DRM_FORMAT_VYUY:
4361 break;
4362 default:
72660ce0
VS
4363 DRM_DEBUG_KMS("[PLANE:%d:%s] FB:%d unsupported scaling format 0x%x\n",
4364 intel_plane->base.base.id, intel_plane->base.name,
4365 fb->base.id, fb->pixel_format);
86adf9d7 4366 return -EINVAL;
a1b2278e
CK
4367 }
4368
a1b2278e
CK
4369 return 0;
4370}
4371
e435d6e5
ML
4372static void skylake_scaler_disable(struct intel_crtc *crtc)
4373{
4374 int i;
4375
4376 for (i = 0; i < crtc->num_scalers; i++)
4377 skl_detach_scaler(crtc, i);
4378}
4379
4380static void skylake_pfit_enable(struct intel_crtc *crtc)
bd2e244f
JB
4381{
4382 struct drm_device *dev = crtc->base.dev;
4383 struct drm_i915_private *dev_priv = dev->dev_private;
4384 int pipe = crtc->pipe;
a1b2278e
CK
4385 struct intel_crtc_scaler_state *scaler_state =
4386 &crtc->config->scaler_state;
4387
4388 DRM_DEBUG_KMS("for crtc_state = %p\n", crtc->config);
4389
6e3c9717 4390 if (crtc->config->pch_pfit.enabled) {
a1b2278e
CK
4391 int id;
4392
4393 if (WARN_ON(crtc->config->scaler_state.scaler_id < 0)) {
4394 DRM_ERROR("Requesting pfit without getting a scaler first\n");
4395 return;
4396 }
4397
4398 id = scaler_state->scaler_id;
4399 I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
4400 PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
4401 I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
4402 I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
4403
4404 DRM_DEBUG_KMS("for crtc_state = %p scaler_id = %d\n", crtc->config, id);
bd2e244f
JB
4405 }
4406}
4407
b074cec8
JB
4408static void ironlake_pfit_enable(struct intel_crtc *crtc)
4409{
4410 struct drm_device *dev = crtc->base.dev;
4411 struct drm_i915_private *dev_priv = dev->dev_private;
4412 int pipe = crtc->pipe;
4413
6e3c9717 4414 if (crtc->config->pch_pfit.enabled) {
b074cec8
JB
4415 /* Force use of hard-coded filter coefficients
4416 * as some pre-programmed values are broken,
4417 * e.g. x201.
4418 */
4419 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
4420 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
4421 PF_PIPE_SEL_IVB(pipe));
4422 else
4423 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
6e3c9717
ACO
4424 I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
4425 I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
d4270e57
JB
4426 }
4427}
4428
20bc8673 4429void hsw_enable_ips(struct intel_crtc *crtc)
d77e4531 4430{
cea165c3
VS
4431 struct drm_device *dev = crtc->base.dev;
4432 struct drm_i915_private *dev_priv = dev->dev_private;
d77e4531 4433
6e3c9717 4434 if (!crtc->config->ips_enabled)
d77e4531
PZ
4435 return;
4436
307e4498
ML
4437 /*
4438 * We can only enable IPS after we enable a plane and wait for a vblank
4439 * This function is called from post_plane_update, which is run after
4440 * a vblank wait.
4441 */
cea165c3 4442
d77e4531 4443 assert_plane_enabled(dev_priv, crtc->plane);
cea165c3 4444 if (IS_BROADWELL(dev)) {
2a114cc1
BW
4445 mutex_lock(&dev_priv->rps.hw_lock);
4446 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
4447 mutex_unlock(&dev_priv->rps.hw_lock);
4448 /* Quoting Art Runyan: "its not safe to expect any particular
4449 * value in IPS_CTL bit 31 after enabling IPS through the
e59150dc
JB
4450 * mailbox." Moreover, the mailbox may return a bogus state,
4451 * so we need to just enable it and continue on.
2a114cc1
BW
4452 */
4453 } else {
4454 I915_WRITE(IPS_CTL, IPS_ENABLE);
4455 /* The bit only becomes 1 in the next vblank, so this wait here
4456 * is essentially intel_wait_for_vblank. If we don't have this
4457 * and don't wait for vblanks until the end of crtc_enable, then
4458 * the HW state readout code will complain that the expected
4459 * IPS_CTL value is not the one we read. */
4460 if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
4461 DRM_ERROR("Timed out waiting for IPS enable\n");
4462 }
d77e4531
PZ
4463}
4464
20bc8673 4465void hsw_disable_ips(struct intel_crtc *crtc)
d77e4531
PZ
4466{
4467 struct drm_device *dev = crtc->base.dev;
4468 struct drm_i915_private *dev_priv = dev->dev_private;
4469
6e3c9717 4470 if (!crtc->config->ips_enabled)
d77e4531
PZ
4471 return;
4472
4473 assert_plane_enabled(dev_priv, crtc->plane);
23d0b130 4474 if (IS_BROADWELL(dev)) {
2a114cc1
BW
4475 mutex_lock(&dev_priv->rps.hw_lock);
4476 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
4477 mutex_unlock(&dev_priv->rps.hw_lock);
23d0b130
BW
4478 /* wait for pcode to finish disabling IPS, which may take up to 42ms */
4479 if (wait_for((I915_READ(IPS_CTL) & IPS_ENABLE) == 0, 42))
4480 DRM_ERROR("Timed out waiting for IPS disable\n");
e59150dc 4481 } else {
2a114cc1 4482 I915_WRITE(IPS_CTL, 0);
e59150dc
JB
4483 POSTING_READ(IPS_CTL);
4484 }
d77e4531
PZ
4485
4486 /* We need to wait for a vblank before we can disable the plane. */
4487 intel_wait_for_vblank(dev, crtc->pipe);
4488}
4489
7cac945f 4490static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
d3eedb1a 4491{
7cac945f 4492 if (intel_crtc->overlay) {
d3eedb1a
VS
4493 struct drm_device *dev = intel_crtc->base.dev;
4494 struct drm_i915_private *dev_priv = dev->dev_private;
4495
4496 mutex_lock(&dev->struct_mutex);
4497 dev_priv->mm.interruptible = false;
4498 (void) intel_overlay_switch_off(intel_crtc->overlay);
4499 dev_priv->mm.interruptible = true;
4500 mutex_unlock(&dev->struct_mutex);
4501 }
4502
4503 /* Let userspace switch the overlay on again. In most cases userspace
4504 * has to recompute where to put it anyway.
4505 */
4506}
4507
87d4300a
ML
4508/**
4509 * intel_post_enable_primary - Perform operations after enabling primary plane
4510 * @crtc: the CRTC whose primary plane was just enabled
4511 *
4512 * Performs potentially sleeping operations that must be done after the primary
4513 * plane is enabled, such as updating FBC and IPS. Note that this may be
4514 * called due to an explicit primary plane update, or due to an implicit
4515 * re-enable that is caused when a sprite plane is updated to no longer
4516 * completely hide the primary plane.
4517 */
4518static void
4519intel_post_enable_primary(struct drm_crtc *crtc)
a5c4d7bc
VS
4520{
4521 struct drm_device *dev = crtc->dev;
87d4300a 4522 struct drm_i915_private *dev_priv = dev->dev_private;
a5c4d7bc
VS
4523 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4524 int pipe = intel_crtc->pipe;
a5c4d7bc 4525
87d4300a
ML
4526 /*
4527 * FIXME IPS should be fine as long as one plane is
4528 * enabled, but in practice it seems to have problems
4529 * when going from primary only to sprite only and vice
4530 * versa.
4531 */
a5c4d7bc
VS
4532 hsw_enable_ips(intel_crtc);
4533
f99d7069 4534 /*
87d4300a
ML
4535 * Gen2 reports pipe underruns whenever all planes are disabled.
4536 * So don't enable underrun reporting before at least some planes
4537 * are enabled.
4538 * FIXME: Need to fix the logic to work when we turn off all planes
4539 * but leave the pipe running.
f99d7069 4540 */
87d4300a
ML
4541 if (IS_GEN2(dev))
4542 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4543
aca7b684
VS
4544 /* Underruns don't always raise interrupts, so check manually. */
4545 intel_check_cpu_fifo_underruns(dev_priv);
4546 intel_check_pch_fifo_underruns(dev_priv);
a5c4d7bc
VS
4547}
4548
2622a081 4549/* FIXME move all this to pre_plane_update() with proper state tracking */
87d4300a
ML
4550static void
4551intel_pre_disable_primary(struct drm_crtc *crtc)
a5c4d7bc
VS
4552{
4553 struct drm_device *dev = crtc->dev;
4554 struct drm_i915_private *dev_priv = dev->dev_private;
4555 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4556 int pipe = intel_crtc->pipe;
a5c4d7bc 4557
87d4300a
ML
4558 /*
4559 * Gen2 reports pipe underruns whenever all planes are disabled.
4560 * So diasble underrun reporting before all the planes get disabled.
4561 * FIXME: Need to fix the logic to work when we turn off all planes
4562 * but leave the pipe running.
4563 */
4564 if (IS_GEN2(dev))
4565 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
a5c4d7bc 4566
2622a081
VS
4567 /*
4568 * FIXME IPS should be fine as long as one plane is
4569 * enabled, but in practice it seems to have problems
4570 * when going from primary only to sprite only and vice
4571 * versa.
4572 */
4573 hsw_disable_ips(intel_crtc);
4574}
4575
4576/* FIXME get rid of this and use pre_plane_update */
4577static void
4578intel_pre_disable_primary_noatomic(struct drm_crtc *crtc)
4579{
4580 struct drm_device *dev = crtc->dev;
4581 struct drm_i915_private *dev_priv = dev->dev_private;
4582 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4583 int pipe = intel_crtc->pipe;
4584
4585 intel_pre_disable_primary(crtc);
4586
87d4300a
ML
4587 /*
4588 * Vblank time updates from the shadow to live plane control register
4589 * are blocked if the memory self-refresh mode is active at that
4590 * moment. So to make sure the plane gets truly disabled, disable
4591 * first the self-refresh mode. The self-refresh enable bit in turn
4592 * will be checked/applied by the HW only at the next frame start
4593 * event which is after the vblank start event, so we need to have a
4594 * wait-for-vblank between disabling the plane and the pipe.
4595 */
262cd2e1 4596 if (HAS_GMCH_DISPLAY(dev)) {
87d4300a 4597 intel_set_memory_cxsr(dev_priv, false);
262cd2e1
VS
4598 dev_priv->wm.vlv.cxsr = false;
4599 intel_wait_for_vblank(dev, pipe);
4600 }
87d4300a
ML
4601}
4602
5a21b665
DV
4603static void intel_post_plane_update(struct intel_crtc_state *old_crtc_state)
4604{
4605 struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
4606 struct drm_atomic_state *old_state = old_crtc_state->base.state;
4607 struct intel_crtc_state *pipe_config =
4608 to_intel_crtc_state(crtc->base.state);
4609 struct drm_device *dev = crtc->base.dev;
4610 struct drm_plane *primary = crtc->base.primary;
4611 struct drm_plane_state *old_pri_state =
4612 drm_atomic_get_existing_plane_state(old_state, primary);
4613
4614 intel_frontbuffer_flip(dev, pipe_config->fb_bits);
4615
4616 crtc->wm.cxsr_allowed = true;
4617
4618 if (pipe_config->update_wm_post && pipe_config->base.active)
4619 intel_update_watermarks(&crtc->base);
4620
4621 if (old_pri_state) {
4622 struct intel_plane_state *primary_state =
4623 to_intel_plane_state(primary->state);
4624 struct intel_plane_state *old_primary_state =
4625 to_intel_plane_state(old_pri_state);
4626
4627 intel_fbc_post_update(crtc);
4628
4629 if (primary_state->visible &&
4630 (needs_modeset(&pipe_config->base) ||
4631 !old_primary_state->visible))
4632 intel_post_enable_primary(&crtc->base);
4633 }
4634}
4635
5c74cd73 4636static void intel_pre_plane_update(struct intel_crtc_state *old_crtc_state)
ac21b225 4637{
5c74cd73 4638 struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
ac21b225 4639 struct drm_device *dev = crtc->base.dev;
eddfcbcd 4640 struct drm_i915_private *dev_priv = dev->dev_private;
ab1d3a0e
ML
4641 struct intel_crtc_state *pipe_config =
4642 to_intel_crtc_state(crtc->base.state);
5c74cd73
ML
4643 struct drm_atomic_state *old_state = old_crtc_state->base.state;
4644 struct drm_plane *primary = crtc->base.primary;
4645 struct drm_plane_state *old_pri_state =
4646 drm_atomic_get_existing_plane_state(old_state, primary);
4647 bool modeset = needs_modeset(&pipe_config->base);
ac21b225 4648
5c74cd73
ML
4649 if (old_pri_state) {
4650 struct intel_plane_state *primary_state =
4651 to_intel_plane_state(primary->state);
4652 struct intel_plane_state *old_primary_state =
4653 to_intel_plane_state(old_pri_state);
4654
faf68d92 4655 intel_fbc_pre_update(crtc, pipe_config, primary_state);
31ae71fc 4656
5c74cd73
ML
4657 if (old_primary_state->visible &&
4658 (modeset || !primary_state->visible))
4659 intel_pre_disable_primary(&crtc->base);
4660 }
852eb00d 4661
a4015f9a 4662 if (pipe_config->disable_cxsr && HAS_GMCH_DISPLAY(dev)) {
852eb00d 4663 crtc->wm.cxsr_allowed = false;
2dfd178d 4664
2622a081
VS
4665 /*
4666 * Vblank time updates from the shadow to live plane control register
4667 * are blocked if the memory self-refresh mode is active at that
4668 * moment. So to make sure the plane gets truly disabled, disable
4669 * first the self-refresh mode. The self-refresh enable bit in turn
4670 * will be checked/applied by the HW only at the next frame start
4671 * event which is after the vblank start event, so we need to have a
4672 * wait-for-vblank between disabling the plane and the pipe.
4673 */
4674 if (old_crtc_state->base.active) {
2dfd178d 4675 intel_set_memory_cxsr(dev_priv, false);
2622a081
VS
4676 dev_priv->wm.vlv.cxsr = false;
4677 intel_wait_for_vblank(dev, crtc->pipe);
4678 }
852eb00d 4679 }
92826fcd 4680
ed4a6a7c
MR
4681 /*
4682 * IVB workaround: must disable low power watermarks for at least
4683 * one frame before enabling scaling. LP watermarks can be re-enabled
4684 * when scaling is disabled.
4685 *
4686 * WaCxSRDisabledForSpriteScaling:ivb
4687 */
4688 if (pipe_config->disable_lp_wm) {
4689 ilk_disable_lp_wm(dev);
4690 intel_wait_for_vblank(dev, crtc->pipe);
4691 }
4692
4693 /*
4694 * If we're doing a modeset, we're done. No need to do any pre-vblank
4695 * watermark programming here.
4696 */
4697 if (needs_modeset(&pipe_config->base))
4698 return;
4699
4700 /*
4701 * For platforms that support atomic watermarks, program the
4702 * 'intermediate' watermarks immediately. On pre-gen9 platforms, these
4703 * will be the intermediate values that are safe for both pre- and
4704 * post- vblank; when vblank happens, the 'active' values will be set
4705 * to the final 'target' values and we'll do this again to get the
4706 * optimal watermarks. For gen9+ platforms, the values we program here
4707 * will be the final target values which will get automatically latched
4708 * at vblank time; no further programming will be necessary.
4709 *
4710 * If a platform hasn't been transitioned to atomic watermarks yet,
4711 * we'll continue to update watermarks the old way, if flags tell
4712 * us to.
4713 */
4714 if (dev_priv->display.initial_watermarks != NULL)
4715 dev_priv->display.initial_watermarks(pipe_config);
caed361d 4716 else if (pipe_config->update_wm_pre)
92826fcd 4717 intel_update_watermarks(&crtc->base);
ac21b225
ML
4718}
4719
d032ffa0 4720static void intel_crtc_disable_planes(struct drm_crtc *crtc, unsigned plane_mask)
87d4300a
ML
4721{
4722 struct drm_device *dev = crtc->dev;
4723 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
d032ffa0 4724 struct drm_plane *p;
87d4300a
ML
4725 int pipe = intel_crtc->pipe;
4726
7cac945f 4727 intel_crtc_dpms_overlay_disable(intel_crtc);
27321ae8 4728
d032ffa0
ML
4729 drm_for_each_plane_mask(p, dev, plane_mask)
4730 to_intel_plane(p)->disable_plane(p, crtc);
f98551ae 4731
f99d7069
DV
4732 /*
4733 * FIXME: Once we grow proper nuclear flip support out of this we need
4734 * to compute the mask of flip planes precisely. For the time being
4735 * consider this a flip to a NULL plane.
4736 */
4737 intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
a5c4d7bc
VS
4738}
4739
f67a559d
JB
4740static void ironlake_crtc_enable(struct drm_crtc *crtc)
4741{
4742 struct drm_device *dev = crtc->dev;
4743 struct drm_i915_private *dev_priv = dev->dev_private;
4744 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 4745 struct intel_encoder *encoder;
f67a559d 4746 int pipe = intel_crtc->pipe;
b95c5321
ML
4747 struct intel_crtc_state *pipe_config =
4748 to_intel_crtc_state(crtc->state);
f67a559d 4749
53d9f4e9 4750 if (WARN_ON(intel_crtc->active))
f67a559d
JB
4751 return;
4752
b2c0593a
VS
4753 /*
4754 * Sometimes spurious CPU pipe underruns happen during FDI
4755 * training, at least with VGA+HDMI cloning. Suppress them.
4756 *
4757 * On ILK we get an occasional spurious CPU pipe underruns
4758 * between eDP port A enable and vdd enable. Also PCH port
4759 * enable seems to result in the occasional CPU pipe underrun.
4760 *
4761 * Spurious PCH underruns also occur during PCH enabling.
4762 */
4763 if (intel_crtc->config->has_pch_encoder || IS_GEN5(dev_priv))
4764 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
81b088ca
VS
4765 if (intel_crtc->config->has_pch_encoder)
4766 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
4767
6e3c9717 4768 if (intel_crtc->config->has_pch_encoder)
b14b1055
DV
4769 intel_prepare_shared_dpll(intel_crtc);
4770
6e3c9717 4771 if (intel_crtc->config->has_dp_encoder)
fe3cd48d 4772 intel_dp_set_m_n(intel_crtc, M1_N1);
29407aab
DV
4773
4774 intel_set_pipe_timings(intel_crtc);
bc58be60 4775 intel_set_pipe_src_size(intel_crtc);
29407aab 4776
6e3c9717 4777 if (intel_crtc->config->has_pch_encoder) {
29407aab 4778 intel_cpu_transcoder_set_m_n(intel_crtc,
6e3c9717 4779 &intel_crtc->config->fdi_m_n, NULL);
29407aab
DV
4780 }
4781
4782 ironlake_set_pipeconf(crtc);
4783
f67a559d 4784 intel_crtc->active = true;
8664281b 4785
f6736a1a 4786 for_each_encoder_on_crtc(dev, crtc, encoder)
952735ee
DV
4787 if (encoder->pre_enable)
4788 encoder->pre_enable(encoder);
f67a559d 4789
6e3c9717 4790 if (intel_crtc->config->has_pch_encoder) {
fff367c7
DV
4791 /* Note: FDI PLL enabling _must_ be done before we enable the
4792 * cpu pipes, hence this is separate from all the other fdi/pch
4793 * enabling. */
88cefb6c 4794 ironlake_fdi_pll_enable(intel_crtc);
46b6f814
DV
4795 } else {
4796 assert_fdi_tx_disabled(dev_priv, pipe);
4797 assert_fdi_rx_disabled(dev_priv, pipe);
4798 }
f67a559d 4799
b074cec8 4800 ironlake_pfit_enable(intel_crtc);
f67a559d 4801
9c54c0dd
JB
4802 /*
4803 * On ILK+ LUT must be loaded before the pipe is running but with
4804 * clocks enabled
4805 */
b95c5321 4806 intel_color_load_luts(&pipe_config->base);
9c54c0dd 4807
1d5bf5d9
ID
4808 if (dev_priv->display.initial_watermarks != NULL)
4809 dev_priv->display.initial_watermarks(intel_crtc->config);
e1fdc473 4810 intel_enable_pipe(intel_crtc);
f67a559d 4811
6e3c9717 4812 if (intel_crtc->config->has_pch_encoder)
f67a559d 4813 ironlake_pch_enable(crtc);
c98e9dcf 4814
f9b61ff6
DV
4815 assert_vblank_disabled(crtc);
4816 drm_crtc_vblank_on(crtc);
4817
fa5c73b1
DV
4818 for_each_encoder_on_crtc(dev, crtc, encoder)
4819 encoder->enable(encoder);
61b77ddd
DV
4820
4821 if (HAS_PCH_CPT(dev))
a1520318 4822 cpt_verify_modeset(dev, intel_crtc->pipe);
37ca8d4c
VS
4823
4824 /* Must wait for vblank to avoid spurious PCH FIFO underruns */
4825 if (intel_crtc->config->has_pch_encoder)
4826 intel_wait_for_vblank(dev, pipe);
b2c0593a 4827 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
37ca8d4c 4828 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
6be4a607
JB
4829}
4830
42db64ef
PZ
4831/* IPS only exists on ULT machines and is tied to pipe A. */
4832static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
4833{
f5adf94e 4834 return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
42db64ef
PZ
4835}
4836
4f771f10
PZ
4837static void haswell_crtc_enable(struct drm_crtc *crtc)
4838{
4839 struct drm_device *dev = crtc->dev;
4840 struct drm_i915_private *dev_priv = dev->dev_private;
4841 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4842 struct intel_encoder *encoder;
99d736a2 4843 int pipe = intel_crtc->pipe, hsw_workaround_pipe;
4d1de975 4844 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
99d736a2
ML
4845 struct intel_crtc_state *pipe_config =
4846 to_intel_crtc_state(crtc->state);
4f771f10 4847
53d9f4e9 4848 if (WARN_ON(intel_crtc->active))
4f771f10
PZ
4849 return;
4850
81b088ca
VS
4851 if (intel_crtc->config->has_pch_encoder)
4852 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
4853 false);
4854
95a7a2ae
ID
4855 for_each_encoder_on_crtc(dev, crtc, encoder)
4856 if (encoder->pre_pll_enable)
4857 encoder->pre_pll_enable(encoder);
4858
8106ddbd 4859 if (intel_crtc->config->shared_dpll)
df8ad70c
DV
4860 intel_enable_shared_dpll(intel_crtc);
4861
6e3c9717 4862 if (intel_crtc->config->has_dp_encoder)
fe3cd48d 4863 intel_dp_set_m_n(intel_crtc, M1_N1);
229fca97 4864
4d1de975
JN
4865 if (!intel_crtc->config->has_dsi_encoder)
4866 intel_set_pipe_timings(intel_crtc);
4867
bc58be60 4868 intel_set_pipe_src_size(intel_crtc);
229fca97 4869
4d1de975
JN
4870 if (cpu_transcoder != TRANSCODER_EDP &&
4871 !transcoder_is_dsi(cpu_transcoder)) {
4872 I915_WRITE(PIPE_MULT(cpu_transcoder),
6e3c9717 4873 intel_crtc->config->pixel_multiplier - 1);
ebb69c95
CT
4874 }
4875
6e3c9717 4876 if (intel_crtc->config->has_pch_encoder) {
229fca97 4877 intel_cpu_transcoder_set_m_n(intel_crtc,
6e3c9717 4878 &intel_crtc->config->fdi_m_n, NULL);
229fca97
DV
4879 }
4880
4d1de975
JN
4881 if (!intel_crtc->config->has_dsi_encoder)
4882 haswell_set_pipeconf(crtc);
4883
391bf048 4884 haswell_set_pipemisc(crtc);
229fca97 4885
b95c5321 4886 intel_color_set_csc(&pipe_config->base);
229fca97 4887
4f771f10 4888 intel_crtc->active = true;
8664281b 4889
6b698516
DV
4890 if (intel_crtc->config->has_pch_encoder)
4891 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
4892 else
4893 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4894
7d4aefd0 4895 for_each_encoder_on_crtc(dev, crtc, encoder) {
4f771f10
PZ
4896 if (encoder->pre_enable)
4897 encoder->pre_enable(encoder);
7d4aefd0 4898 }
4f771f10 4899
d2d65408 4900 if (intel_crtc->config->has_pch_encoder)
4fe9467d 4901 dev_priv->display.fdi_link_train(crtc);
4fe9467d 4902
a65347ba 4903 if (!intel_crtc->config->has_dsi_encoder)
7d4aefd0 4904 intel_ddi_enable_pipe_clock(intel_crtc);
4f771f10 4905
1c132b44 4906 if (INTEL_INFO(dev)->gen >= 9)
e435d6e5 4907 skylake_pfit_enable(intel_crtc);
ff6d9f55 4908 else
1c132b44 4909 ironlake_pfit_enable(intel_crtc);
4f771f10
PZ
4910
4911 /*
4912 * On ILK+ LUT must be loaded before the pipe is running but with
4913 * clocks enabled
4914 */
b95c5321 4915 intel_color_load_luts(&pipe_config->base);
4f771f10 4916
1f544388 4917 intel_ddi_set_pipe_settings(crtc);
a65347ba 4918 if (!intel_crtc->config->has_dsi_encoder)
7d4aefd0 4919 intel_ddi_enable_transcoder_func(crtc);
4f771f10 4920
1d5bf5d9
ID
4921 if (dev_priv->display.initial_watermarks != NULL)
4922 dev_priv->display.initial_watermarks(pipe_config);
4923 else
4924 intel_update_watermarks(crtc);
4d1de975
JN
4925
4926 /* XXX: Do the pipe assertions at the right place for BXT DSI. */
4927 if (!intel_crtc->config->has_dsi_encoder)
4928 intel_enable_pipe(intel_crtc);
42db64ef 4929
6e3c9717 4930 if (intel_crtc->config->has_pch_encoder)
1507e5bd 4931 lpt_pch_enable(crtc);
4f771f10 4932
a65347ba 4933 if (intel_crtc->config->dp_encoder_is_mst)
0e32b39c
DA
4934 intel_ddi_set_vc_payload_alloc(crtc, true);
4935
f9b61ff6
DV
4936 assert_vblank_disabled(crtc);
4937 drm_crtc_vblank_on(crtc);
4938
8807e55b 4939 for_each_encoder_on_crtc(dev, crtc, encoder) {
4f771f10 4940 encoder->enable(encoder);
8807e55b
JN
4941 intel_opregion_notify_encoder(encoder, true);
4942 }
4f771f10 4943
6b698516
DV
4944 if (intel_crtc->config->has_pch_encoder) {
4945 intel_wait_for_vblank(dev, pipe);
4946 intel_wait_for_vblank(dev, pipe);
4947 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
d2d65408
VS
4948 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
4949 true);
6b698516 4950 }
d2d65408 4951
e4916946
PZ
4952 /* If we change the relative order between pipe/planes enabling, we need
4953 * to change the workaround. */
99d736a2
ML
4954 hsw_workaround_pipe = pipe_config->hsw_workaround_pipe;
4955 if (IS_HASWELL(dev) && hsw_workaround_pipe != INVALID_PIPE) {
4956 intel_wait_for_vblank(dev, hsw_workaround_pipe);
4957 intel_wait_for_vblank(dev, hsw_workaround_pipe);
4958 }
4f771f10
PZ
4959}
4960
bfd16b2a 4961static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force)
3f8dce3a
DV
4962{
4963 struct drm_device *dev = crtc->base.dev;
4964 struct drm_i915_private *dev_priv = dev->dev_private;
4965 int pipe = crtc->pipe;
4966
4967 /* To avoid upsetting the power well on haswell only disable the pfit if
4968 * it's in use. The hw state code will make sure we get this right. */
bfd16b2a 4969 if (force || crtc->config->pch_pfit.enabled) {
3f8dce3a
DV
4970 I915_WRITE(PF_CTL(pipe), 0);
4971 I915_WRITE(PF_WIN_POS(pipe), 0);
4972 I915_WRITE(PF_WIN_SZ(pipe), 0);
4973 }
4974}
4975
6be4a607
JB
4976static void ironlake_crtc_disable(struct drm_crtc *crtc)
4977{
4978 struct drm_device *dev = crtc->dev;
4979 struct drm_i915_private *dev_priv = dev->dev_private;
4980 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 4981 struct intel_encoder *encoder;
6be4a607 4982 int pipe = intel_crtc->pipe;
b52eb4dc 4983
b2c0593a
VS
4984 /*
4985 * Sometimes spurious CPU pipe underruns happen when the
4986 * pipe is already disabled, but FDI RX/TX is still enabled.
4987 * Happens at least with VGA+HDMI cloning. Suppress them.
4988 */
4989 if (intel_crtc->config->has_pch_encoder) {
4990 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
37ca8d4c 4991 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
b2c0593a 4992 }
37ca8d4c 4993
ea9d758d
DV
4994 for_each_encoder_on_crtc(dev, crtc, encoder)
4995 encoder->disable(encoder);
4996
f9b61ff6
DV
4997 drm_crtc_vblank_off(crtc);
4998 assert_vblank_disabled(crtc);
4999
575f7ab7 5000 intel_disable_pipe(intel_crtc);
32f9d658 5001
bfd16b2a 5002 ironlake_pfit_disable(intel_crtc, false);
2c07245f 5003
b2c0593a 5004 if (intel_crtc->config->has_pch_encoder)
5a74f70a
VS
5005 ironlake_fdi_disable(crtc);
5006
bf49ec8c
DV
5007 for_each_encoder_on_crtc(dev, crtc, encoder)
5008 if (encoder->post_disable)
5009 encoder->post_disable(encoder);
2c07245f 5010
6e3c9717 5011 if (intel_crtc->config->has_pch_encoder) {
d925c59a 5012 ironlake_disable_pch_transcoder(dev_priv, pipe);
6be4a607 5013
d925c59a 5014 if (HAS_PCH_CPT(dev)) {
f0f59a00
VS
5015 i915_reg_t reg;
5016 u32 temp;
5017
d925c59a
DV
5018 /* disable TRANS_DP_CTL */
5019 reg = TRANS_DP_CTL(pipe);
5020 temp = I915_READ(reg);
5021 temp &= ~(TRANS_DP_OUTPUT_ENABLE |
5022 TRANS_DP_PORT_SEL_MASK);
5023 temp |= TRANS_DP_PORT_SEL_NONE;
5024 I915_WRITE(reg, temp);
5025
5026 /* disable DPLL_SEL */
5027 temp = I915_READ(PCH_DPLL_SEL);
11887397 5028 temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
d925c59a 5029 I915_WRITE(PCH_DPLL_SEL, temp);
9db4a9c7 5030 }
e3421a18 5031
d925c59a
DV
5032 ironlake_fdi_pll_disable(intel_crtc);
5033 }
81b088ca 5034
b2c0593a 5035 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
81b088ca 5036 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
6be4a607 5037}
1b3c7a47 5038
4f771f10 5039static void haswell_crtc_disable(struct drm_crtc *crtc)
ee7b9f93 5040{
4f771f10
PZ
5041 struct drm_device *dev = crtc->dev;
5042 struct drm_i915_private *dev_priv = dev->dev_private;
ee7b9f93 5043 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4f771f10 5044 struct intel_encoder *encoder;
6e3c9717 5045 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
ee7b9f93 5046
d2d65408
VS
5047 if (intel_crtc->config->has_pch_encoder)
5048 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5049 false);
5050
8807e55b
JN
5051 for_each_encoder_on_crtc(dev, crtc, encoder) {
5052 intel_opregion_notify_encoder(encoder, false);
4f771f10 5053 encoder->disable(encoder);
8807e55b 5054 }
4f771f10 5055
f9b61ff6
DV
5056 drm_crtc_vblank_off(crtc);
5057 assert_vblank_disabled(crtc);
5058
4d1de975
JN
5059 /* XXX: Do the pipe assertions at the right place for BXT DSI. */
5060 if (!intel_crtc->config->has_dsi_encoder)
5061 intel_disable_pipe(intel_crtc);
4f771f10 5062
6e3c9717 5063 if (intel_crtc->config->dp_encoder_is_mst)
a4bf214f
VS
5064 intel_ddi_set_vc_payload_alloc(crtc, false);
5065
a65347ba 5066 if (!intel_crtc->config->has_dsi_encoder)
7d4aefd0 5067 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
4f771f10 5068
1c132b44 5069 if (INTEL_INFO(dev)->gen >= 9)
e435d6e5 5070 skylake_scaler_disable(intel_crtc);
ff6d9f55 5071 else
bfd16b2a 5072 ironlake_pfit_disable(intel_crtc, false);
4f771f10 5073
a65347ba 5074 if (!intel_crtc->config->has_dsi_encoder)
7d4aefd0 5075 intel_ddi_disable_pipe_clock(intel_crtc);
4f771f10 5076
97b040aa
ID
5077 for_each_encoder_on_crtc(dev, crtc, encoder)
5078 if (encoder->post_disable)
5079 encoder->post_disable(encoder);
81b088ca 5080
92966a37
VS
5081 if (intel_crtc->config->has_pch_encoder) {
5082 lpt_disable_pch_transcoder(dev_priv);
503a74e9 5083 lpt_disable_iclkip(dev_priv);
92966a37
VS
5084 intel_ddi_fdi_disable(crtc);
5085
81b088ca
VS
5086 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5087 true);
92966a37 5088 }
4f771f10
PZ
5089}
5090
2dd24552
JB
5091static void i9xx_pfit_enable(struct intel_crtc *crtc)
5092{
5093 struct drm_device *dev = crtc->base.dev;
5094 struct drm_i915_private *dev_priv = dev->dev_private;
6e3c9717 5095 struct intel_crtc_state *pipe_config = crtc->config;
2dd24552 5096
681a8504 5097 if (!pipe_config->gmch_pfit.control)
2dd24552
JB
5098 return;
5099
2dd24552 5100 /*
c0b03411
DV
5101 * The panel fitter should only be adjusted whilst the pipe is disabled,
5102 * according to register description and PRM.
2dd24552 5103 */
c0b03411
DV
5104 WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
5105 assert_pipe_disabled(dev_priv, crtc->pipe);
2dd24552 5106
b074cec8
JB
5107 I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
5108 I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
5a80c45c
DV
5109
5110 /* Border color in case we don't scale up to the full screen. Black by
5111 * default, change to something else for debugging. */
5112 I915_WRITE(BCLRPAT(crtc->pipe), 0);
2dd24552
JB
5113}
5114
d05410f9
DA
5115static enum intel_display_power_domain port_to_power_domain(enum port port)
5116{
5117 switch (port) {
5118 case PORT_A:
6331a704 5119 return POWER_DOMAIN_PORT_DDI_A_LANES;
d05410f9 5120 case PORT_B:
6331a704 5121 return POWER_DOMAIN_PORT_DDI_B_LANES;
d05410f9 5122 case PORT_C:
6331a704 5123 return POWER_DOMAIN_PORT_DDI_C_LANES;
d05410f9 5124 case PORT_D:
6331a704 5125 return POWER_DOMAIN_PORT_DDI_D_LANES;
d8e19f99 5126 case PORT_E:
6331a704 5127 return POWER_DOMAIN_PORT_DDI_E_LANES;
d05410f9 5128 default:
b9fec167 5129 MISSING_CASE(port);
d05410f9
DA
5130 return POWER_DOMAIN_PORT_OTHER;
5131 }
5132}
5133
25f78f58
VS
5134static enum intel_display_power_domain port_to_aux_power_domain(enum port port)
5135{
5136 switch (port) {
5137 case PORT_A:
5138 return POWER_DOMAIN_AUX_A;
5139 case PORT_B:
5140 return POWER_DOMAIN_AUX_B;
5141 case PORT_C:
5142 return POWER_DOMAIN_AUX_C;
5143 case PORT_D:
5144 return POWER_DOMAIN_AUX_D;
5145 case PORT_E:
5146 /* FIXME: Check VBT for actual wiring of PORT E */
5147 return POWER_DOMAIN_AUX_D;
5148 default:
b9fec167 5149 MISSING_CASE(port);
25f78f58
VS
5150 return POWER_DOMAIN_AUX_A;
5151 }
5152}
5153
319be8ae
ID
5154enum intel_display_power_domain
5155intel_display_port_power_domain(struct intel_encoder *intel_encoder)
5156{
5157 struct drm_device *dev = intel_encoder->base.dev;
5158 struct intel_digital_port *intel_dig_port;
5159
5160 switch (intel_encoder->type) {
5161 case INTEL_OUTPUT_UNKNOWN:
5162 /* Only DDI platforms should ever use this output type */
5163 WARN_ON_ONCE(!HAS_DDI(dev));
5164 case INTEL_OUTPUT_DISPLAYPORT:
5165 case INTEL_OUTPUT_HDMI:
5166 case INTEL_OUTPUT_EDP:
5167 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
d05410f9 5168 return port_to_power_domain(intel_dig_port->port);
0e32b39c
DA
5169 case INTEL_OUTPUT_DP_MST:
5170 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
5171 return port_to_power_domain(intel_dig_port->port);
319be8ae
ID
5172 case INTEL_OUTPUT_ANALOG:
5173 return POWER_DOMAIN_PORT_CRT;
5174 case INTEL_OUTPUT_DSI:
5175 return POWER_DOMAIN_PORT_DSI;
5176 default:
5177 return POWER_DOMAIN_PORT_OTHER;
5178 }
5179}
5180
25f78f58
VS
5181enum intel_display_power_domain
5182intel_display_port_aux_power_domain(struct intel_encoder *intel_encoder)
5183{
5184 struct drm_device *dev = intel_encoder->base.dev;
5185 struct intel_digital_port *intel_dig_port;
5186
5187 switch (intel_encoder->type) {
5188 case INTEL_OUTPUT_UNKNOWN:
651174a4
ID
5189 case INTEL_OUTPUT_HDMI:
5190 /*
5191 * Only DDI platforms should ever use these output types.
5192 * We can get here after the HDMI detect code has already set
5193 * the type of the shared encoder. Since we can't be sure
5194 * what's the status of the given connectors, play safe and
5195 * run the DP detection too.
5196 */
25f78f58
VS
5197 WARN_ON_ONCE(!HAS_DDI(dev));
5198 case INTEL_OUTPUT_DISPLAYPORT:
5199 case INTEL_OUTPUT_EDP:
5200 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
5201 return port_to_aux_power_domain(intel_dig_port->port);
5202 case INTEL_OUTPUT_DP_MST:
5203 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
5204 return port_to_aux_power_domain(intel_dig_port->port);
5205 default:
b9fec167 5206 MISSING_CASE(intel_encoder->type);
25f78f58
VS
5207 return POWER_DOMAIN_AUX_A;
5208 }
5209}
5210
74bff5f9
ML
5211static unsigned long get_crtc_power_domains(struct drm_crtc *crtc,
5212 struct intel_crtc_state *crtc_state)
77d22dca 5213{
319be8ae 5214 struct drm_device *dev = crtc->dev;
74bff5f9 5215 struct drm_encoder *encoder;
319be8ae
ID
5216 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5217 enum pipe pipe = intel_crtc->pipe;
77d22dca 5218 unsigned long mask;
74bff5f9 5219 enum transcoder transcoder = crtc_state->cpu_transcoder;
77d22dca 5220
74bff5f9 5221 if (!crtc_state->base.active)
292b990e
ML
5222 return 0;
5223
77d22dca
ID
5224 mask = BIT(POWER_DOMAIN_PIPE(pipe));
5225 mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
74bff5f9
ML
5226 if (crtc_state->pch_pfit.enabled ||
5227 crtc_state->pch_pfit.force_thru)
77d22dca
ID
5228 mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
5229
74bff5f9
ML
5230 drm_for_each_encoder_mask(encoder, dev, crtc_state->base.encoder_mask) {
5231 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
5232
319be8ae 5233 mask |= BIT(intel_display_port_power_domain(intel_encoder));
74bff5f9 5234 }
319be8ae 5235
15e7ec29
ML
5236 if (crtc_state->shared_dpll)
5237 mask |= BIT(POWER_DOMAIN_PLLS);
5238
77d22dca
ID
5239 return mask;
5240}
5241
74bff5f9
ML
5242static unsigned long
5243modeset_get_crtc_power_domains(struct drm_crtc *crtc,
5244 struct intel_crtc_state *crtc_state)
77d22dca 5245{
292b990e
ML
5246 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
5247 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5248 enum intel_display_power_domain domain;
5a21b665 5249 unsigned long domains, new_domains, old_domains;
77d22dca 5250
292b990e 5251 old_domains = intel_crtc->enabled_power_domains;
74bff5f9
ML
5252 intel_crtc->enabled_power_domains = new_domains =
5253 get_crtc_power_domains(crtc, crtc_state);
77d22dca 5254
5a21b665 5255 domains = new_domains & ~old_domains;
292b990e
ML
5256
5257 for_each_power_domain(domain, domains)
5258 intel_display_power_get(dev_priv, domain);
5259
5a21b665 5260 return old_domains & ~new_domains;
292b990e
ML
5261}
5262
5263static void modeset_put_power_domains(struct drm_i915_private *dev_priv,
5264 unsigned long domains)
5265{
5266 enum intel_display_power_domain domain;
5267
5268 for_each_power_domain(domain, domains)
5269 intel_display_power_put(dev_priv, domain);
5270}
77d22dca 5271
adafdc6f
MK
5272static int intel_compute_max_dotclk(struct drm_i915_private *dev_priv)
5273{
5274 int max_cdclk_freq = dev_priv->max_cdclk_freq;
5275
5276 if (INTEL_INFO(dev_priv)->gen >= 9 ||
5277 IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
5278 return max_cdclk_freq;
5279 else if (IS_CHERRYVIEW(dev_priv))
5280 return max_cdclk_freq*95/100;
5281 else if (INTEL_INFO(dev_priv)->gen < 4)
5282 return 2*max_cdclk_freq*90/100;
5283 else
5284 return max_cdclk_freq*90/100;
5285}
5286
b2045352
VS
5287static int skl_calc_cdclk(int max_pixclk, int vco);
5288
560a7ae4
DL
5289static void intel_update_max_cdclk(struct drm_device *dev)
5290{
5291 struct drm_i915_private *dev_priv = dev->dev_private;
5292
ef11bdb3 5293 if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) {
560a7ae4 5294 u32 limit = I915_READ(SKL_DFSM) & SKL_DFSM_CDCLK_LIMIT_MASK;
b2045352
VS
5295 int max_cdclk, vco;
5296
5297 vco = dev_priv->skl_preferred_vco_freq;
63911d72 5298 WARN_ON(vco != 8100000 && vco != 8640000);
560a7ae4 5299
b2045352
VS
5300 /*
5301 * Use the lower (vco 8640) cdclk values as a
5302 * first guess. skl_calc_cdclk() will correct it
5303 * if the preferred vco is 8100 instead.
5304 */
560a7ae4 5305 if (limit == SKL_DFSM_CDCLK_LIMIT_675)
487ed2e4 5306 max_cdclk = 617143;
560a7ae4 5307 else if (limit == SKL_DFSM_CDCLK_LIMIT_540)
b2045352 5308 max_cdclk = 540000;
560a7ae4 5309 else if (limit == SKL_DFSM_CDCLK_LIMIT_450)
b2045352 5310 max_cdclk = 432000;
560a7ae4 5311 else
487ed2e4 5312 max_cdclk = 308571;
b2045352
VS
5313
5314 dev_priv->max_cdclk_freq = skl_calc_cdclk(max_cdclk, vco);
281c114f
MR
5315 } else if (IS_BROXTON(dev)) {
5316 dev_priv->max_cdclk_freq = 624000;
560a7ae4
DL
5317 } else if (IS_BROADWELL(dev)) {
5318 /*
5319 * FIXME with extra cooling we can allow
5320 * 540 MHz for ULX and 675 Mhz for ULT.
5321 * How can we know if extra cooling is
5322 * available? PCI ID, VTB, something else?
5323 */
5324 if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
5325 dev_priv->max_cdclk_freq = 450000;
5326 else if (IS_BDW_ULX(dev))
5327 dev_priv->max_cdclk_freq = 450000;
5328 else if (IS_BDW_ULT(dev))
5329 dev_priv->max_cdclk_freq = 540000;
5330 else
5331 dev_priv->max_cdclk_freq = 675000;
0904deaf
MK
5332 } else if (IS_CHERRYVIEW(dev)) {
5333 dev_priv->max_cdclk_freq = 320000;
560a7ae4
DL
5334 } else if (IS_VALLEYVIEW(dev)) {
5335 dev_priv->max_cdclk_freq = 400000;
5336 } else {
5337 /* otherwise assume cdclk is fixed */
5338 dev_priv->max_cdclk_freq = dev_priv->cdclk_freq;
5339 }
5340
adafdc6f
MK
5341 dev_priv->max_dotclk_freq = intel_compute_max_dotclk(dev_priv);
5342
560a7ae4
DL
5343 DRM_DEBUG_DRIVER("Max CD clock rate: %d kHz\n",
5344 dev_priv->max_cdclk_freq);
adafdc6f
MK
5345
5346 DRM_DEBUG_DRIVER("Max dotclock rate: %d kHz\n",
5347 dev_priv->max_dotclk_freq);
560a7ae4
DL
5348}
5349
5350static void intel_update_cdclk(struct drm_device *dev)
5351{
5352 struct drm_i915_private *dev_priv = dev->dev_private;
5353
5354 dev_priv->cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
2f2a121a 5355
83d7c81f 5356 if (INTEL_GEN(dev_priv) >= 9)
709e05c3
VS
5357 DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz, VCO: %d kHz, ref: %d kHz\n",
5358 dev_priv->cdclk_freq, dev_priv->cdclk_pll.vco,
5359 dev_priv->cdclk_pll.ref);
2f2a121a
VS
5360 else
5361 DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
5362 dev_priv->cdclk_freq);
560a7ae4
DL
5363
5364 /*
b5d99ff9
VS
5365 * 9:0 CMBUS [sic] CDCLK frequency (cdfreq):
5366 * Programmng [sic] note: bit[9:2] should be programmed to the number
5367 * of cdclk that generates 4MHz reference clock freq which is used to
5368 * generate GMBus clock. This will vary with the cdclk freq.
560a7ae4 5369 */
b5d99ff9 5370 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
560a7ae4 5371 I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->cdclk_freq, 1000));
560a7ae4
DL
5372}
5373
92891e45
VS
5374/* convert from kHz to .1 fixpoint MHz with -1MHz offset */
5375static int skl_cdclk_decimal(int cdclk)
5376{
5377 return DIV_ROUND_CLOSEST(cdclk - 1000, 500);
5378}
5379
5f199dfa
VS
5380static int bxt_de_pll_vco(struct drm_i915_private *dev_priv, int cdclk)
5381{
5382 int ratio;
5383
5384 if (cdclk == dev_priv->cdclk_pll.ref)
5385 return 0;
5386
5387 switch (cdclk) {
5388 default:
5389 MISSING_CASE(cdclk);
5390 case 144000:
5391 case 288000:
5392 case 384000:
5393 case 576000:
5394 ratio = 60;
5395 break;
5396 case 624000:
5397 ratio = 65;
5398 break;
5399 }
5400
5401 return dev_priv->cdclk_pll.ref * ratio;
5402}
5403
2b73001e
VS
5404static void bxt_de_pll_disable(struct drm_i915_private *dev_priv)
5405{
5406 I915_WRITE(BXT_DE_PLL_ENABLE, 0);
5407
5408 /* Timeout 200us */
5409 if (wait_for((I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK) == 0, 1))
5410 DRM_ERROR("timeout waiting for DE PLL unlock\n");
83d7c81f
VS
5411
5412 dev_priv->cdclk_pll.vco = 0;
2b73001e
VS
5413}
5414
5f199dfa 5415static void bxt_de_pll_enable(struct drm_i915_private *dev_priv, int vco)
2b73001e 5416{
5f199dfa 5417 int ratio = DIV_ROUND_CLOSEST(vco, dev_priv->cdclk_pll.ref);
2b73001e
VS
5418 u32 val;
5419
5420 val = I915_READ(BXT_DE_PLL_CTL);
5421 val &= ~BXT_DE_PLL_RATIO_MASK;
5f199dfa 5422 val |= BXT_DE_PLL_RATIO(ratio);
2b73001e
VS
5423 I915_WRITE(BXT_DE_PLL_CTL, val);
5424
5425 I915_WRITE(BXT_DE_PLL_ENABLE, BXT_DE_PLL_PLL_ENABLE);
5426
5427 /* Timeout 200us */
5428 if (wait_for((I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK) != 0, 1))
5429 DRM_ERROR("timeout waiting for DE PLL lock\n");
83d7c81f 5430
5f199dfa 5431 dev_priv->cdclk_pll.vco = vco;
2b73001e
VS
5432}
5433
324513c0 5434static void bxt_set_cdclk(struct drm_i915_private *dev_priv, int cdclk)
f8437dd1 5435{
5f199dfa
VS
5436 u32 val, divider;
5437 int vco, ret;
f8437dd1 5438
5f199dfa
VS
5439 vco = bxt_de_pll_vco(dev_priv, cdclk);
5440
5441 DRM_DEBUG_DRIVER("Changing CDCLK to %d kHz (VCO %d kHz)\n", cdclk, vco);
5442
5443 /* cdclk = vco / 2 / div{1,1.5,2,4} */
5444 switch (DIV_ROUND_CLOSEST(vco, cdclk)) {
5445 case 8:
f8437dd1 5446 divider = BXT_CDCLK_CD2X_DIV_SEL_4;
f8437dd1 5447 break;
5f199dfa 5448 case 4:
f8437dd1 5449 divider = BXT_CDCLK_CD2X_DIV_SEL_2;
f8437dd1 5450 break;
5f199dfa 5451 case 3:
f8437dd1 5452 divider = BXT_CDCLK_CD2X_DIV_SEL_1_5;
f8437dd1 5453 break;
5f199dfa 5454 case 2:
f8437dd1 5455 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
f8437dd1
VK
5456 break;
5457 default:
5f199dfa
VS
5458 WARN_ON(cdclk != dev_priv->cdclk_pll.ref);
5459 WARN_ON(vco != 0);
f8437dd1 5460
5f199dfa
VS
5461 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
5462 break;
f8437dd1
VK
5463 }
5464
f8437dd1 5465 /* Inform power controller of upcoming frequency change */
5f199dfa 5466 mutex_lock(&dev_priv->rps.hw_lock);
f8437dd1
VK
5467 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
5468 0x80000000);
5469 mutex_unlock(&dev_priv->rps.hw_lock);
5470
5471 if (ret) {
5472 DRM_ERROR("PCode CDCLK freq change notify failed (err %d, freq %d)\n",
9ef56154 5473 ret, cdclk);
f8437dd1
VK
5474 return;
5475 }
5476
5f199dfa
VS
5477 if (dev_priv->cdclk_pll.vco != 0 &&
5478 dev_priv->cdclk_pll.vco != vco)
2b73001e 5479 bxt_de_pll_disable(dev_priv);
f8437dd1 5480
5f199dfa
VS
5481 if (dev_priv->cdclk_pll.vco != vco)
5482 bxt_de_pll_enable(dev_priv, vco);
f8437dd1 5483
5f199dfa
VS
5484 val = divider | skl_cdclk_decimal(cdclk);
5485 /*
5486 * FIXME if only the cd2x divider needs changing, it could be done
5487 * without shutting off the pipe (if only one pipe is active).
5488 */
5489 val |= BXT_CDCLK_CD2X_PIPE_NONE;
5490 /*
5491 * Disable SSA Precharge when CD clock frequency < 500 MHz,
5492 * enable otherwise.
5493 */
5494 if (cdclk >= 500000)
5495 val |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
5496 I915_WRITE(CDCLK_CTL, val);
f8437dd1
VK
5497
5498 mutex_lock(&dev_priv->rps.hw_lock);
5499 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
9ef56154 5500 DIV_ROUND_UP(cdclk, 25000));
f8437dd1
VK
5501 mutex_unlock(&dev_priv->rps.hw_lock);
5502
5503 if (ret) {
5504 DRM_ERROR("PCode CDCLK freq set failed, (err %d, freq %d)\n",
9ef56154 5505 ret, cdclk);
f8437dd1
VK
5506 return;
5507 }
5508
c6c4696f 5509 intel_update_cdclk(dev_priv->dev);
f8437dd1
VK
5510}
5511
d66a2194 5512static void bxt_sanitize_cdclk(struct drm_i915_private *dev_priv)
f8437dd1 5513{
d66a2194
ID
5514 u32 cdctl, expected;
5515
089c6fd5 5516 intel_update_cdclk(dev_priv->dev);
f8437dd1 5517
d66a2194
ID
5518 if (dev_priv->cdclk_pll.vco == 0 ||
5519 dev_priv->cdclk_freq == dev_priv->cdclk_pll.ref)
5520 goto sanitize;
5521
5522 /* DPLL okay; verify the cdclock
5523 *
5524 * Some BIOS versions leave an incorrect decimal frequency value and
5525 * set reserved MBZ bits in CDCLK_CTL at least during exiting from S4,
5526 * so sanitize this register.
5527 */
5528 cdctl = I915_READ(CDCLK_CTL);
5529 /*
5530 * Let's ignore the pipe field, since BIOS could have configured the
5531 * dividers both synching to an active pipe, or asynchronously
5532 * (PIPE_NONE).
5533 */
5534 cdctl &= ~BXT_CDCLK_CD2X_PIPE_NONE;
5535
5536 expected = (cdctl & BXT_CDCLK_CD2X_DIV_SEL_MASK) |
5537 skl_cdclk_decimal(dev_priv->cdclk_freq);
5538 /*
5539 * Disable SSA Precharge when CD clock frequency < 500 MHz,
5540 * enable otherwise.
5541 */
5542 if (dev_priv->cdclk_freq >= 500000)
5543 expected |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
5544
5545 if (cdctl == expected)
5546 /* All well; nothing to sanitize */
5547 return;
5548
5549sanitize:
5550 DRM_DEBUG_KMS("Sanitizing cdclk programmed by pre-os\n");
5551
5552 /* force cdclk programming */
5553 dev_priv->cdclk_freq = 0;
5554
5555 /* force full PLL disable + enable */
5556 dev_priv->cdclk_pll.vco = -1;
5557}
5558
324513c0 5559void bxt_init_cdclk(struct drm_i915_private *dev_priv)
d66a2194
ID
5560{
5561 bxt_sanitize_cdclk(dev_priv);
5562
5563 if (dev_priv->cdclk_freq != 0 && dev_priv->cdclk_pll.vco != 0)
089c6fd5 5564 return;
c2e001ef 5565
f8437dd1
VK
5566 /*
5567 * FIXME:
5568 * - The initial CDCLK needs to be read from VBT.
5569 * Need to make this change after VBT has changes for BXT.
f8437dd1 5570 */
324513c0 5571 bxt_set_cdclk(dev_priv, bxt_calc_cdclk(0));
f8437dd1
VK
5572}
5573
324513c0 5574void bxt_uninit_cdclk(struct drm_i915_private *dev_priv)
f8437dd1 5575{
324513c0 5576 bxt_set_cdclk(dev_priv, dev_priv->cdclk_pll.ref);
f8437dd1
VK
5577}
5578
a8ca4934
VS
5579static int skl_calc_cdclk(int max_pixclk, int vco)
5580{
63911d72 5581 if (vco == 8640000) {
a8ca4934 5582 if (max_pixclk > 540000)
487ed2e4 5583 return 617143;
a8ca4934
VS
5584 else if (max_pixclk > 432000)
5585 return 540000;
487ed2e4 5586 else if (max_pixclk > 308571)
a8ca4934
VS
5587 return 432000;
5588 else
487ed2e4 5589 return 308571;
a8ca4934 5590 } else {
a8ca4934
VS
5591 if (max_pixclk > 540000)
5592 return 675000;
5593 else if (max_pixclk > 450000)
5594 return 540000;
5595 else if (max_pixclk > 337500)
5596 return 450000;
5597 else
5598 return 337500;
5599 }
5600}
5601
ea61791e
VS
5602static void
5603skl_dpll0_update(struct drm_i915_private *dev_priv)
5d96d8af 5604{
ea61791e 5605 u32 val;
5d96d8af 5606
709e05c3 5607 dev_priv->cdclk_pll.ref = 24000;
1c3f7700 5608 dev_priv->cdclk_pll.vco = 0;
709e05c3 5609
ea61791e 5610 val = I915_READ(LCPLL1_CTL);
1c3f7700 5611 if ((val & LCPLL_PLL_ENABLE) == 0)
ea61791e 5612 return;
5d96d8af 5613
1c3f7700
ID
5614 if (WARN_ON((val & LCPLL_PLL_LOCK) == 0))
5615 return;
9f7eb31a 5616
ea61791e
VS
5617 val = I915_READ(DPLL_CTRL1);
5618
1c3f7700
ID
5619 if (WARN_ON((val & (DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) |
5620 DPLL_CTRL1_SSC(SKL_DPLL0) |
5621 DPLL_CTRL1_OVERRIDE(SKL_DPLL0))) !=
5622 DPLL_CTRL1_OVERRIDE(SKL_DPLL0)))
5623 return;
9f7eb31a 5624
ea61791e
VS
5625 switch (val & DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0)) {
5626 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810, SKL_DPLL0):
5627 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1350, SKL_DPLL0):
5628 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1620, SKL_DPLL0):
5629 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_2700, SKL_DPLL0):
63911d72 5630 dev_priv->cdclk_pll.vco = 8100000;
ea61791e
VS
5631 break;
5632 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080, SKL_DPLL0):
5633 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_2160, SKL_DPLL0):
63911d72 5634 dev_priv->cdclk_pll.vco = 8640000;
ea61791e
VS
5635 break;
5636 default:
5637 MISSING_CASE(val & DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
ea61791e
VS
5638 break;
5639 }
5d96d8af
DL
5640}
5641
b2045352
VS
5642void skl_set_preferred_cdclk_vco(struct drm_i915_private *dev_priv, int vco)
5643{
5644 bool changed = dev_priv->skl_preferred_vco_freq != vco;
5645
5646 dev_priv->skl_preferred_vco_freq = vco;
5647
5648 if (changed)
5649 intel_update_max_cdclk(dev_priv->dev);
5650}
5651
5d96d8af 5652static void
3861fc60 5653skl_dpll0_enable(struct drm_i915_private *dev_priv, int vco)
5d96d8af 5654{
a8ca4934 5655 int min_cdclk = skl_calc_cdclk(0, vco);
5d96d8af
DL
5656 u32 val;
5657
63911d72 5658 WARN_ON(vco != 8100000 && vco != 8640000);
b2045352 5659
5d96d8af 5660 /* select the minimum CDCLK before enabling DPLL 0 */
9ef56154 5661 val = CDCLK_FREQ_337_308 | skl_cdclk_decimal(min_cdclk);
5d96d8af
DL
5662 I915_WRITE(CDCLK_CTL, val);
5663 POSTING_READ(CDCLK_CTL);
5664
5665 /*
5666 * We always enable DPLL0 with the lowest link rate possible, but still
5667 * taking into account the VCO required to operate the eDP panel at the
5668 * desired frequency. The usual DP link rates operate with a VCO of
5669 * 8100 while the eDP 1.4 alternate link rates need a VCO of 8640.
5670 * The modeset code is responsible for the selection of the exact link
5671 * rate later on, with the constraint of choosing a frequency that
a8ca4934 5672 * works with vco.
5d96d8af
DL
5673 */
5674 val = I915_READ(DPLL_CTRL1);
5675
5676 val &= ~(DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) | DPLL_CTRL1_SSC(SKL_DPLL0) |
5677 DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
5678 val |= DPLL_CTRL1_OVERRIDE(SKL_DPLL0);
63911d72 5679 if (vco == 8640000)
5d96d8af
DL
5680 val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080,
5681 SKL_DPLL0);
5682 else
5683 val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810,
5684 SKL_DPLL0);
5685
5686 I915_WRITE(DPLL_CTRL1, val);
5687 POSTING_READ(DPLL_CTRL1);
5688
5689 I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) | LCPLL_PLL_ENABLE);
5690
5691 if (wait_for(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK, 5))
5692 DRM_ERROR("DPLL0 not locked\n");
1cd593e0 5693
63911d72 5694 dev_priv->cdclk_pll.vco = vco;
b2045352
VS
5695
5696 /* We'll want to keep using the current vco from now on. */
5697 skl_set_preferred_cdclk_vco(dev_priv, vco);
5d96d8af
DL
5698}
5699
430e05de
VS
5700static void
5701skl_dpll0_disable(struct drm_i915_private *dev_priv)
5702{
5703 I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) & ~LCPLL_PLL_ENABLE);
5704 if (wait_for(!(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK), 1))
5705 DRM_ERROR("Couldn't disable DPLL0\n");
1cd593e0 5706
63911d72 5707 dev_priv->cdclk_pll.vco = 0;
430e05de
VS
5708}
5709
5d96d8af
DL
5710static bool skl_cdclk_pcu_ready(struct drm_i915_private *dev_priv)
5711{
5712 int ret;
5713 u32 val;
5714
5715 /* inform PCU we want to change CDCLK */
5716 val = SKL_CDCLK_PREPARE_FOR_CHANGE;
5717 mutex_lock(&dev_priv->rps.hw_lock);
5718 ret = sandybridge_pcode_read(dev_priv, SKL_PCODE_CDCLK_CONTROL, &val);
5719 mutex_unlock(&dev_priv->rps.hw_lock);
5720
5721 return ret == 0 && (val & SKL_CDCLK_READY_FOR_CHANGE);
5722}
5723
5724static bool skl_cdclk_wait_for_pcu_ready(struct drm_i915_private *dev_priv)
5725{
5726 unsigned int i;
5727
5728 for (i = 0; i < 15; i++) {
5729 if (skl_cdclk_pcu_ready(dev_priv))
5730 return true;
5731 udelay(10);
5732 }
5733
5734 return false;
5735}
5736
1cd593e0 5737static void skl_set_cdclk(struct drm_i915_private *dev_priv, int cdclk, int vco)
5d96d8af 5738{
560a7ae4 5739 struct drm_device *dev = dev_priv->dev;
5d96d8af
DL
5740 u32 freq_select, pcu_ack;
5741
1cd593e0
VS
5742 WARN_ON((cdclk == 24000) != (vco == 0));
5743
63911d72 5744 DRM_DEBUG_DRIVER("Changing CDCLK to %d kHz (VCO %d kHz)\n", cdclk, vco);
5d96d8af
DL
5745
5746 if (!skl_cdclk_wait_for_pcu_ready(dev_priv)) {
5747 DRM_ERROR("failed to inform PCU about cdclk change\n");
5748 return;
5749 }
5750
5751 /* set CDCLK_CTL */
9ef56154 5752 switch (cdclk) {
5d96d8af
DL
5753 case 450000:
5754 case 432000:
5755 freq_select = CDCLK_FREQ_450_432;
5756 pcu_ack = 1;
5757 break;
5758 case 540000:
5759 freq_select = CDCLK_FREQ_540;
5760 pcu_ack = 2;
5761 break;
487ed2e4 5762 case 308571:
5d96d8af
DL
5763 case 337500:
5764 default:
5765 freq_select = CDCLK_FREQ_337_308;
5766 pcu_ack = 0;
5767 break;
487ed2e4 5768 case 617143:
5d96d8af
DL
5769 case 675000:
5770 freq_select = CDCLK_FREQ_675_617;
5771 pcu_ack = 3;
5772 break;
5773 }
5774
63911d72
VS
5775 if (dev_priv->cdclk_pll.vco != 0 &&
5776 dev_priv->cdclk_pll.vco != vco)
1cd593e0
VS
5777 skl_dpll0_disable(dev_priv);
5778
63911d72 5779 if (dev_priv->cdclk_pll.vco != vco)
1cd593e0
VS
5780 skl_dpll0_enable(dev_priv, vco);
5781
9ef56154 5782 I915_WRITE(CDCLK_CTL, freq_select | skl_cdclk_decimal(cdclk));
5d96d8af
DL
5783 POSTING_READ(CDCLK_CTL);
5784
5785 /* inform PCU of the change */
5786 mutex_lock(&dev_priv->rps.hw_lock);
5787 sandybridge_pcode_write(dev_priv, SKL_PCODE_CDCLK_CONTROL, pcu_ack);
5788 mutex_unlock(&dev_priv->rps.hw_lock);
560a7ae4
DL
5789
5790 intel_update_cdclk(dev);
5d96d8af
DL
5791}
5792
9f7eb31a
VS
5793static void skl_sanitize_cdclk(struct drm_i915_private *dev_priv);
5794
5d96d8af
DL
5795void skl_uninit_cdclk(struct drm_i915_private *dev_priv)
5796{
709e05c3 5797 skl_set_cdclk(dev_priv, dev_priv->cdclk_pll.ref, 0);
5d96d8af
DL
5798}
5799
5800void skl_init_cdclk(struct drm_i915_private *dev_priv)
5801{
9f7eb31a
VS
5802 int cdclk, vco;
5803
5804 skl_sanitize_cdclk(dev_priv);
5d96d8af 5805
63911d72 5806 if (dev_priv->cdclk_freq != 0 && dev_priv->cdclk_pll.vco != 0) {
9f7eb31a
VS
5807 /*
5808 * Use the current vco as our initial
5809 * guess as to what the preferred vco is.
5810 */
5811 if (dev_priv->skl_preferred_vco_freq == 0)
5812 skl_set_preferred_cdclk_vco(dev_priv,
63911d72 5813 dev_priv->cdclk_pll.vco);
70c2c184 5814 return;
1cd593e0 5815 }
5d96d8af 5816
70c2c184
VS
5817 vco = dev_priv->skl_preferred_vco_freq;
5818 if (vco == 0)
63911d72 5819 vco = 8100000;
70c2c184 5820 cdclk = skl_calc_cdclk(0, vco);
5d96d8af 5821
70c2c184 5822 skl_set_cdclk(dev_priv, cdclk, vco);
5d96d8af
DL
5823}
5824
9f7eb31a 5825static void skl_sanitize_cdclk(struct drm_i915_private *dev_priv)
c73666f3 5826{
09492498 5827 uint32_t cdctl, expected;
c73666f3 5828
f1b391a5
SK
5829 /*
5830 * check if the pre-os intialized the display
5831 * There is SWF18 scratchpad register defined which is set by the
5832 * pre-os which can be used by the OS drivers to check the status
5833 */
5834 if ((I915_READ(SWF_ILK(0x18)) & 0x00FFFFFF) == 0)
5835 goto sanitize;
5836
1c3f7700 5837 intel_update_cdclk(dev_priv->dev);
c73666f3 5838 /* Is PLL enabled and locked ? */
1c3f7700
ID
5839 if (dev_priv->cdclk_pll.vco == 0 ||
5840 dev_priv->cdclk_freq == dev_priv->cdclk_pll.ref)
c73666f3
SK
5841 goto sanitize;
5842
5843 /* DPLL okay; verify the cdclock
5844 *
5845 * Noticed in some instances that the freq selection is correct but
5846 * decimal part is programmed wrong from BIOS where pre-os does not
5847 * enable display. Verify the same as well.
5848 */
09492498
VS
5849 cdctl = I915_READ(CDCLK_CTL);
5850 expected = (cdctl & CDCLK_FREQ_SEL_MASK) |
5851 skl_cdclk_decimal(dev_priv->cdclk_freq);
5852 if (cdctl == expected)
c73666f3 5853 /* All well; nothing to sanitize */
9f7eb31a 5854 return;
c89e39f3 5855
9f7eb31a
VS
5856sanitize:
5857 DRM_DEBUG_KMS("Sanitizing cdclk programmed by pre-os\n");
c73666f3 5858
9f7eb31a
VS
5859 /* force cdclk programming */
5860 dev_priv->cdclk_freq = 0;
5861 /* force full PLL disable + enable */
63911d72 5862 dev_priv->cdclk_pll.vco = -1;
c73666f3
SK
5863}
5864
30a970c6
JB
5865/* Adjust CDclk dividers to allow high res or save power if possible */
5866static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
5867{
5868 struct drm_i915_private *dev_priv = dev->dev_private;
5869 u32 val, cmd;
5870
164dfd28
VK
5871 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5872 != dev_priv->cdclk_freq);
d60c4473 5873
dfcab17e 5874 if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
30a970c6 5875 cmd = 2;
dfcab17e 5876 else if (cdclk == 266667)
30a970c6
JB
5877 cmd = 1;
5878 else
5879 cmd = 0;
5880
5881 mutex_lock(&dev_priv->rps.hw_lock);
5882 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5883 val &= ~DSPFREQGUAR_MASK;
5884 val |= (cmd << DSPFREQGUAR_SHIFT);
5885 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5886 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5887 DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
5888 50)) {
5889 DRM_ERROR("timed out waiting for CDclk change\n");
5890 }
5891 mutex_unlock(&dev_priv->rps.hw_lock);
5892
54433e91
VS
5893 mutex_lock(&dev_priv->sb_lock);
5894
dfcab17e 5895 if (cdclk == 400000) {
6bcda4f0 5896 u32 divider;
30a970c6 5897
6bcda4f0 5898 divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
30a970c6 5899
30a970c6
JB
5900 /* adjust cdclk divider */
5901 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
87d5d259 5902 val &= ~CCK_FREQUENCY_VALUES;
30a970c6
JB
5903 val |= divider;
5904 vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
a877e801
VS
5905
5906 if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
87d5d259 5907 CCK_FREQUENCY_STATUS) == (divider << CCK_FREQUENCY_STATUS_SHIFT),
a877e801
VS
5908 50))
5909 DRM_ERROR("timed out waiting for CDclk change\n");
30a970c6
JB
5910 }
5911
30a970c6
JB
5912 /* adjust self-refresh exit latency value */
5913 val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
5914 val &= ~0x7f;
5915
5916 /*
5917 * For high bandwidth configs, we set a higher latency in the bunit
5918 * so that the core display fetch happens in time to avoid underruns.
5919 */
dfcab17e 5920 if (cdclk == 400000)
30a970c6
JB
5921 val |= 4500 / 250; /* 4.5 usec */
5922 else
5923 val |= 3000 / 250; /* 3.0 usec */
5924 vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
54433e91 5925
a580516d 5926 mutex_unlock(&dev_priv->sb_lock);
30a970c6 5927
b6283055 5928 intel_update_cdclk(dev);
30a970c6
JB
5929}
5930
383c5a6a
VS
5931static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
5932{
5933 struct drm_i915_private *dev_priv = dev->dev_private;
5934 u32 val, cmd;
5935
164dfd28
VK
5936 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5937 != dev_priv->cdclk_freq);
383c5a6a
VS
5938
5939 switch (cdclk) {
383c5a6a
VS
5940 case 333333:
5941 case 320000:
383c5a6a 5942 case 266667:
383c5a6a 5943 case 200000:
383c5a6a
VS
5944 break;
5945 default:
5f77eeb0 5946 MISSING_CASE(cdclk);
383c5a6a
VS
5947 return;
5948 }
5949
9d0d3fda
VS
5950 /*
5951 * Specs are full of misinformation, but testing on actual
5952 * hardware has shown that we just need to write the desired
5953 * CCK divider into the Punit register.
5954 */
5955 cmd = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
5956
383c5a6a
VS
5957 mutex_lock(&dev_priv->rps.hw_lock);
5958 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5959 val &= ~DSPFREQGUAR_MASK_CHV;
5960 val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
5961 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5962 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5963 DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
5964 50)) {
5965 DRM_ERROR("timed out waiting for CDclk change\n");
5966 }
5967 mutex_unlock(&dev_priv->rps.hw_lock);
5968
b6283055 5969 intel_update_cdclk(dev);
383c5a6a
VS
5970}
5971
30a970c6
JB
5972static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
5973 int max_pixclk)
5974{
6bcda4f0 5975 int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
6cca3195 5976 int limit = IS_CHERRYVIEW(dev_priv) ? 95 : 90;
29dc7ef3 5977
30a970c6
JB
5978 /*
5979 * Really only a few cases to deal with, as only 4 CDclks are supported:
5980 * 200MHz
5981 * 267MHz
29dc7ef3 5982 * 320/333MHz (depends on HPLL freq)
6cca3195
VS
5983 * 400MHz (VLV only)
5984 * So we check to see whether we're above 90% (VLV) or 95% (CHV)
5985 * of the lower bin and adjust if needed.
e37c67a1
VS
5986 *
5987 * We seem to get an unstable or solid color picture at 200MHz.
5988 * Not sure what's wrong. For now use 200MHz only when all pipes
5989 * are off.
30a970c6 5990 */
6cca3195
VS
5991 if (!IS_CHERRYVIEW(dev_priv) &&
5992 max_pixclk > freq_320*limit/100)
dfcab17e 5993 return 400000;
6cca3195 5994 else if (max_pixclk > 266667*limit/100)
29dc7ef3 5995 return freq_320;
e37c67a1 5996 else if (max_pixclk > 0)
dfcab17e 5997 return 266667;
e37c67a1
VS
5998 else
5999 return 200000;
30a970c6
JB
6000}
6001
324513c0 6002static int bxt_calc_cdclk(int max_pixclk)
f8437dd1 6003{
760e1477 6004 if (max_pixclk > 576000)
f8437dd1 6005 return 624000;
760e1477 6006 else if (max_pixclk > 384000)
f8437dd1 6007 return 576000;
760e1477 6008 else if (max_pixclk > 288000)
f8437dd1 6009 return 384000;
760e1477 6010 else if (max_pixclk > 144000)
f8437dd1
VK
6011 return 288000;
6012 else
6013 return 144000;
6014}
6015
e8788cbc 6016/* Compute the max pixel clock for new configuration. */
a821fc46
ACO
6017static int intel_mode_max_pixclk(struct drm_device *dev,
6018 struct drm_atomic_state *state)
30a970c6 6019{
565602d7
ML
6020 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
6021 struct drm_i915_private *dev_priv = dev->dev_private;
6022 struct drm_crtc *crtc;
6023 struct drm_crtc_state *crtc_state;
6024 unsigned max_pixclk = 0, i;
6025 enum pipe pipe;
30a970c6 6026
565602d7
ML
6027 memcpy(intel_state->min_pixclk, dev_priv->min_pixclk,
6028 sizeof(intel_state->min_pixclk));
304603f4 6029
565602d7
ML
6030 for_each_crtc_in_state(state, crtc, crtc_state, i) {
6031 int pixclk = 0;
6032
6033 if (crtc_state->enable)
6034 pixclk = crtc_state->adjusted_mode.crtc_clock;
304603f4 6035
565602d7 6036 intel_state->min_pixclk[i] = pixclk;
30a970c6
JB
6037 }
6038
565602d7
ML
6039 for_each_pipe(dev_priv, pipe)
6040 max_pixclk = max(intel_state->min_pixclk[pipe], max_pixclk);
6041
30a970c6
JB
6042 return max_pixclk;
6043}
6044
27c329ed 6045static int valleyview_modeset_calc_cdclk(struct drm_atomic_state *state)
30a970c6 6046{
27c329ed
ML
6047 struct drm_device *dev = state->dev;
6048 struct drm_i915_private *dev_priv = dev->dev_private;
6049 int max_pixclk = intel_mode_max_pixclk(dev, state);
1a617b77
ML
6050 struct intel_atomic_state *intel_state =
6051 to_intel_atomic_state(state);
30a970c6 6052
1a617b77 6053 intel_state->cdclk = intel_state->dev_cdclk =
27c329ed 6054 valleyview_calc_cdclk(dev_priv, max_pixclk);
0a9ab303 6055
1a617b77
ML
6056 if (!intel_state->active_crtcs)
6057 intel_state->dev_cdclk = valleyview_calc_cdclk(dev_priv, 0);
6058
27c329ed
ML
6059 return 0;
6060}
304603f4 6061
324513c0 6062static int bxt_modeset_calc_cdclk(struct drm_atomic_state *state)
27c329ed 6063{
4e5ca60f 6064 int max_pixclk = ilk_max_pixel_rate(state);
1a617b77
ML
6065 struct intel_atomic_state *intel_state =
6066 to_intel_atomic_state(state);
85a96e7a 6067
1a617b77 6068 intel_state->cdclk = intel_state->dev_cdclk =
324513c0 6069 bxt_calc_cdclk(max_pixclk);
85a96e7a 6070
1a617b77 6071 if (!intel_state->active_crtcs)
324513c0 6072 intel_state->dev_cdclk = bxt_calc_cdclk(0);
1a617b77 6073
27c329ed 6074 return 0;
30a970c6
JB
6075}
6076
1e69cd74
VS
6077static void vlv_program_pfi_credits(struct drm_i915_private *dev_priv)
6078{
6079 unsigned int credits, default_credits;
6080
6081 if (IS_CHERRYVIEW(dev_priv))
6082 default_credits = PFI_CREDIT(12);
6083 else
6084 default_credits = PFI_CREDIT(8);
6085
bfa7df01 6086 if (dev_priv->cdclk_freq >= dev_priv->czclk_freq) {
1e69cd74
VS
6087 /* CHV suggested value is 31 or 63 */
6088 if (IS_CHERRYVIEW(dev_priv))
fcc0008f 6089 credits = PFI_CREDIT_63;
1e69cd74
VS
6090 else
6091 credits = PFI_CREDIT(15);
6092 } else {
6093 credits = default_credits;
6094 }
6095
6096 /*
6097 * WA - write default credits before re-programming
6098 * FIXME: should we also set the resend bit here?
6099 */
6100 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
6101 default_credits);
6102
6103 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
6104 credits | PFI_CREDIT_RESEND);
6105
6106 /*
6107 * FIXME is this guaranteed to clear
6108 * immediately or should we poll for it?
6109 */
6110 WARN_ON(I915_READ(GCI_CONTROL) & PFI_CREDIT_RESEND);
6111}
6112
27c329ed 6113static void valleyview_modeset_commit_cdclk(struct drm_atomic_state *old_state)
30a970c6 6114{
a821fc46 6115 struct drm_device *dev = old_state->dev;
30a970c6 6116 struct drm_i915_private *dev_priv = dev->dev_private;
1a617b77
ML
6117 struct intel_atomic_state *old_intel_state =
6118 to_intel_atomic_state(old_state);
6119 unsigned req_cdclk = old_intel_state->dev_cdclk;
30a970c6 6120
27c329ed
ML
6121 /*
6122 * FIXME: We can end up here with all power domains off, yet
6123 * with a CDCLK frequency other than the minimum. To account
6124 * for this take the PIPE-A power domain, which covers the HW
6125 * blocks needed for the following programming. This can be
6126 * removed once it's guaranteed that we get here either with
6127 * the minimum CDCLK set, or the required power domains
6128 * enabled.
6129 */
6130 intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
738c05c0 6131
27c329ed
ML
6132 if (IS_CHERRYVIEW(dev))
6133 cherryview_set_cdclk(dev, req_cdclk);
6134 else
6135 valleyview_set_cdclk(dev, req_cdclk);
738c05c0 6136
27c329ed 6137 vlv_program_pfi_credits(dev_priv);
1e69cd74 6138
27c329ed 6139 intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
30a970c6
JB
6140}
6141
89b667f8
JB
6142static void valleyview_crtc_enable(struct drm_crtc *crtc)
6143{
6144 struct drm_device *dev = crtc->dev;
a72e4c9f 6145 struct drm_i915_private *dev_priv = to_i915(dev);
89b667f8
JB
6146 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6147 struct intel_encoder *encoder;
b95c5321
ML
6148 struct intel_crtc_state *pipe_config =
6149 to_intel_crtc_state(crtc->state);
89b667f8 6150 int pipe = intel_crtc->pipe;
89b667f8 6151
53d9f4e9 6152 if (WARN_ON(intel_crtc->active))
89b667f8
JB
6153 return;
6154
6e3c9717 6155 if (intel_crtc->config->has_dp_encoder)
fe3cd48d 6156 intel_dp_set_m_n(intel_crtc, M1_N1);
5b18e57c
DV
6157
6158 intel_set_pipe_timings(intel_crtc);
bc58be60 6159 intel_set_pipe_src_size(intel_crtc);
5b18e57c 6160
c14b0485
VS
6161 if (IS_CHERRYVIEW(dev) && pipe == PIPE_B) {
6162 struct drm_i915_private *dev_priv = dev->dev_private;
6163
6164 I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
6165 I915_WRITE(CHV_CANVAS(pipe), 0);
6166 }
6167
5b18e57c
DV
6168 i9xx_set_pipeconf(intel_crtc);
6169
89b667f8 6170 intel_crtc->active = true;
89b667f8 6171
a72e4c9f 6172 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4a3436e8 6173
89b667f8
JB
6174 for_each_encoder_on_crtc(dev, crtc, encoder)
6175 if (encoder->pre_pll_enable)
6176 encoder->pre_pll_enable(encoder);
6177
cd2d34d9
VS
6178 if (IS_CHERRYVIEW(dev)) {
6179 chv_prepare_pll(intel_crtc, intel_crtc->config);
6180 chv_enable_pll(intel_crtc, intel_crtc->config);
6181 } else {
6182 vlv_prepare_pll(intel_crtc, intel_crtc->config);
6183 vlv_enable_pll(intel_crtc, intel_crtc->config);
9d556c99 6184 }
89b667f8
JB
6185
6186 for_each_encoder_on_crtc(dev, crtc, encoder)
6187 if (encoder->pre_enable)
6188 encoder->pre_enable(encoder);
6189
2dd24552
JB
6190 i9xx_pfit_enable(intel_crtc);
6191
b95c5321 6192 intel_color_load_luts(&pipe_config->base);
63cbb074 6193
caed361d 6194 intel_update_watermarks(crtc);
e1fdc473 6195 intel_enable_pipe(intel_crtc);
be6a6f8e 6196
4b3a9526
VS
6197 assert_vblank_disabled(crtc);
6198 drm_crtc_vblank_on(crtc);
6199
f9b61ff6
DV
6200 for_each_encoder_on_crtc(dev, crtc, encoder)
6201 encoder->enable(encoder);
89b667f8
JB
6202}
6203
f13c2ef3
DV
6204static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
6205{
6206 struct drm_device *dev = crtc->base.dev;
6207 struct drm_i915_private *dev_priv = dev->dev_private;
6208
6e3c9717
ACO
6209 I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
6210 I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
f13c2ef3
DV
6211}
6212
0b8765c6 6213static void i9xx_crtc_enable(struct drm_crtc *crtc)
79e53945
JB
6214{
6215 struct drm_device *dev = crtc->dev;
a72e4c9f 6216 struct drm_i915_private *dev_priv = to_i915(dev);
79e53945 6217 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 6218 struct intel_encoder *encoder;
b95c5321
ML
6219 struct intel_crtc_state *pipe_config =
6220 to_intel_crtc_state(crtc->state);
cd2d34d9 6221 enum pipe pipe = intel_crtc->pipe;
79e53945 6222
53d9f4e9 6223 if (WARN_ON(intel_crtc->active))
f7abfe8b
CW
6224 return;
6225
f13c2ef3
DV
6226 i9xx_set_pll_dividers(intel_crtc);
6227
6e3c9717 6228 if (intel_crtc->config->has_dp_encoder)
fe3cd48d 6229 intel_dp_set_m_n(intel_crtc, M1_N1);
5b18e57c
DV
6230
6231 intel_set_pipe_timings(intel_crtc);
bc58be60 6232 intel_set_pipe_src_size(intel_crtc);
5b18e57c 6233
5b18e57c
DV
6234 i9xx_set_pipeconf(intel_crtc);
6235
f7abfe8b 6236 intel_crtc->active = true;
6b383a7f 6237
4a3436e8 6238 if (!IS_GEN2(dev))
a72e4c9f 6239 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4a3436e8 6240
9d6d9f19
MK
6241 for_each_encoder_on_crtc(dev, crtc, encoder)
6242 if (encoder->pre_enable)
6243 encoder->pre_enable(encoder);
6244
f6736a1a
DV
6245 i9xx_enable_pll(intel_crtc);
6246
2dd24552
JB
6247 i9xx_pfit_enable(intel_crtc);
6248
b95c5321 6249 intel_color_load_luts(&pipe_config->base);
63cbb074 6250
f37fcc2a 6251 intel_update_watermarks(crtc);
e1fdc473 6252 intel_enable_pipe(intel_crtc);
be6a6f8e 6253
4b3a9526
VS
6254 assert_vblank_disabled(crtc);
6255 drm_crtc_vblank_on(crtc);
6256
f9b61ff6
DV
6257 for_each_encoder_on_crtc(dev, crtc, encoder)
6258 encoder->enable(encoder);
0b8765c6 6259}
79e53945 6260
87476d63
DV
6261static void i9xx_pfit_disable(struct intel_crtc *crtc)
6262{
6263 struct drm_device *dev = crtc->base.dev;
6264 struct drm_i915_private *dev_priv = dev->dev_private;
87476d63 6265
6e3c9717 6266 if (!crtc->config->gmch_pfit.control)
328d8e82 6267 return;
87476d63 6268
328d8e82 6269 assert_pipe_disabled(dev_priv, crtc->pipe);
87476d63 6270
328d8e82
DV
6271 DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
6272 I915_READ(PFIT_CONTROL));
6273 I915_WRITE(PFIT_CONTROL, 0);
87476d63
DV
6274}
6275
0b8765c6
JB
6276static void i9xx_crtc_disable(struct drm_crtc *crtc)
6277{
6278 struct drm_device *dev = crtc->dev;
6279 struct drm_i915_private *dev_priv = dev->dev_private;
6280 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 6281 struct intel_encoder *encoder;
0b8765c6 6282 int pipe = intel_crtc->pipe;
ef9c3aee 6283
6304cd91
VS
6284 /*
6285 * On gen2 planes are double buffered but the pipe isn't, so we must
6286 * wait for planes to fully turn off before disabling the pipe.
6287 */
90e83e53
ACO
6288 if (IS_GEN2(dev))
6289 intel_wait_for_vblank(dev, pipe);
6304cd91 6290
4b3a9526
VS
6291 for_each_encoder_on_crtc(dev, crtc, encoder)
6292 encoder->disable(encoder);
6293
f9b61ff6
DV
6294 drm_crtc_vblank_off(crtc);
6295 assert_vblank_disabled(crtc);
6296
575f7ab7 6297 intel_disable_pipe(intel_crtc);
24a1f16d 6298
87476d63 6299 i9xx_pfit_disable(intel_crtc);
24a1f16d 6300
89b667f8
JB
6301 for_each_encoder_on_crtc(dev, crtc, encoder)
6302 if (encoder->post_disable)
6303 encoder->post_disable(encoder);
6304
a65347ba 6305 if (!intel_crtc->config->has_dsi_encoder) {
076ed3b2
CML
6306 if (IS_CHERRYVIEW(dev))
6307 chv_disable_pll(dev_priv, pipe);
6308 else if (IS_VALLEYVIEW(dev))
6309 vlv_disable_pll(dev_priv, pipe);
6310 else
1c4e0274 6311 i9xx_disable_pll(intel_crtc);
076ed3b2 6312 }
0b8765c6 6313
d6db995f
VS
6314 for_each_encoder_on_crtc(dev, crtc, encoder)
6315 if (encoder->post_pll_disable)
6316 encoder->post_pll_disable(encoder);
6317
4a3436e8 6318 if (!IS_GEN2(dev))
a72e4c9f 6319 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
0b8765c6
JB
6320}
6321
b17d48e2
ML
6322static void intel_crtc_disable_noatomic(struct drm_crtc *crtc)
6323{
842e0307 6324 struct intel_encoder *encoder;
b17d48e2
ML
6325 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6326 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
6327 enum intel_display_power_domain domain;
6328 unsigned long domains;
6329
6330 if (!intel_crtc->active)
6331 return;
6332
a539205a 6333 if (to_intel_plane_state(crtc->primary->state)->visible) {
5a21b665 6334 WARN_ON(intel_crtc->flip_work);
fc32b1fd 6335
2622a081 6336 intel_pre_disable_primary_noatomic(crtc);
54a41961
ML
6337
6338 intel_crtc_disable_planes(crtc, 1 << drm_plane_index(crtc->primary));
6339 to_intel_plane_state(crtc->primary->state)->visible = false;
a539205a
ML
6340 }
6341
b17d48e2 6342 dev_priv->display.crtc_disable(crtc);
842e0307 6343
78108b7c
VS
6344 DRM_DEBUG_KMS("[CRTC:%d:%s] hw state adjusted, was enabled, now disabled\n",
6345 crtc->base.id, crtc->name);
842e0307
ML
6346
6347 WARN_ON(drm_atomic_set_mode_for_crtc(crtc->state, NULL) < 0);
6348 crtc->state->active = false;
37d9078b 6349 intel_crtc->active = false;
842e0307
ML
6350 crtc->enabled = false;
6351 crtc->state->connector_mask = 0;
6352 crtc->state->encoder_mask = 0;
6353
6354 for_each_encoder_on_crtc(crtc->dev, crtc, encoder)
6355 encoder->base.crtc = NULL;
6356
58f9c0bc 6357 intel_fbc_disable(intel_crtc);
37d9078b 6358 intel_update_watermarks(crtc);
1f7457b1 6359 intel_disable_shared_dpll(intel_crtc);
b17d48e2
ML
6360
6361 domains = intel_crtc->enabled_power_domains;
6362 for_each_power_domain(domain, domains)
6363 intel_display_power_put(dev_priv, domain);
6364 intel_crtc->enabled_power_domains = 0;
565602d7
ML
6365
6366 dev_priv->active_crtcs &= ~(1 << intel_crtc->pipe);
6367 dev_priv->min_pixclk[intel_crtc->pipe] = 0;
b17d48e2
ML
6368}
6369
6b72d486
ML
6370/*
6371 * turn all crtc's off, but do not adjust state
6372 * This has to be paired with a call to intel_modeset_setup_hw_state.
6373 */
70e0bd74 6374int intel_display_suspend(struct drm_device *dev)
ee7b9f93 6375{
e2c8b870 6376 struct drm_i915_private *dev_priv = to_i915(dev);
70e0bd74 6377 struct drm_atomic_state *state;
e2c8b870 6378 int ret;
70e0bd74 6379
e2c8b870
ML
6380 state = drm_atomic_helper_suspend(dev);
6381 ret = PTR_ERR_OR_ZERO(state);
70e0bd74
ML
6382 if (ret)
6383 DRM_ERROR("Suspending crtc's failed with %i\n", ret);
e2c8b870
ML
6384 else
6385 dev_priv->modeset_restore_state = state;
70e0bd74 6386 return ret;
ee7b9f93
JB
6387}
6388
ea5b213a 6389void intel_encoder_destroy(struct drm_encoder *encoder)
7e7d76c3 6390{
4ef69c7a 6391 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
ea5b213a 6392
ea5b213a
CW
6393 drm_encoder_cleanup(encoder);
6394 kfree(intel_encoder);
7e7d76c3
JB
6395}
6396
0a91ca29
DV
6397/* Cross check the actual hw state with our own modeset state tracking (and it's
6398 * internal consistency). */
5a21b665 6399static void intel_connector_verify_state(struct intel_connector *connector)
79e53945 6400{
5a21b665 6401 struct drm_crtc *crtc = connector->base.state->crtc;
35dd3c64
ML
6402
6403 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
6404 connector->base.base.id,
6405 connector->base.name);
6406
0a91ca29 6407 if (connector->get_hw_state(connector)) {
e85376cb 6408 struct intel_encoder *encoder = connector->encoder;
5a21b665 6409 struct drm_connector_state *conn_state = connector->base.state;
0a91ca29 6410
35dd3c64
ML
6411 I915_STATE_WARN(!crtc,
6412 "connector enabled without attached crtc\n");
0a91ca29 6413
35dd3c64
ML
6414 if (!crtc)
6415 return;
6416
6417 I915_STATE_WARN(!crtc->state->active,
6418 "connector is active, but attached crtc isn't\n");
6419
e85376cb 6420 if (!encoder || encoder->type == INTEL_OUTPUT_DP_MST)
35dd3c64
ML
6421 return;
6422
e85376cb 6423 I915_STATE_WARN(conn_state->best_encoder != &encoder->base,
35dd3c64
ML
6424 "atomic encoder doesn't match attached encoder\n");
6425
e85376cb 6426 I915_STATE_WARN(conn_state->crtc != encoder->base.crtc,
35dd3c64
ML
6427 "attached encoder crtc differs from connector crtc\n");
6428 } else {
4d688a2a
ML
6429 I915_STATE_WARN(crtc && crtc->state->active,
6430 "attached crtc is active, but connector isn't\n");
5a21b665 6431 I915_STATE_WARN(!crtc && connector->base.state->best_encoder,
35dd3c64 6432 "best encoder set without crtc!\n");
0a91ca29 6433 }
79e53945
JB
6434}
6435
08d9bc92
ACO
6436int intel_connector_init(struct intel_connector *connector)
6437{
5350a031 6438 drm_atomic_helper_connector_reset(&connector->base);
08d9bc92 6439
5350a031 6440 if (!connector->base.state)
08d9bc92
ACO
6441 return -ENOMEM;
6442
08d9bc92
ACO
6443 return 0;
6444}
6445
6446struct intel_connector *intel_connector_alloc(void)
6447{
6448 struct intel_connector *connector;
6449
6450 connector = kzalloc(sizeof *connector, GFP_KERNEL);
6451 if (!connector)
6452 return NULL;
6453
6454 if (intel_connector_init(connector) < 0) {
6455 kfree(connector);
6456 return NULL;
6457 }
6458
6459 return connector;
6460}
6461
f0947c37
DV
6462/* Simple connector->get_hw_state implementation for encoders that support only
6463 * one connector and no cloning and hence the encoder state determines the state
6464 * of the connector. */
6465bool intel_connector_get_hw_state(struct intel_connector *connector)
ea5b213a 6466{
24929352 6467 enum pipe pipe = 0;
f0947c37 6468 struct intel_encoder *encoder = connector->encoder;
ea5b213a 6469
f0947c37 6470 return encoder->get_hw_state(encoder, &pipe);
ea5b213a
CW
6471}
6472
6d293983 6473static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
d272ddfa 6474{
6d293983
ACO
6475 if (crtc_state->base.enable && crtc_state->has_pch_encoder)
6476 return crtc_state->fdi_lanes;
d272ddfa
VS
6477
6478 return 0;
6479}
6480
6d293983 6481static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
5cec258b 6482 struct intel_crtc_state *pipe_config)
1857e1da 6483{
6d293983
ACO
6484 struct drm_atomic_state *state = pipe_config->base.state;
6485 struct intel_crtc *other_crtc;
6486 struct intel_crtc_state *other_crtc_state;
6487
1857e1da
DV
6488 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
6489 pipe_name(pipe), pipe_config->fdi_lanes);
6490 if (pipe_config->fdi_lanes > 4) {
6491 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
6492 pipe_name(pipe), pipe_config->fdi_lanes);
6d293983 6493 return -EINVAL;
1857e1da
DV
6494 }
6495
bafb6553 6496 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
1857e1da
DV
6497 if (pipe_config->fdi_lanes > 2) {
6498 DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
6499 pipe_config->fdi_lanes);
6d293983 6500 return -EINVAL;
1857e1da 6501 } else {
6d293983 6502 return 0;
1857e1da
DV
6503 }
6504 }
6505
6506 if (INTEL_INFO(dev)->num_pipes == 2)
6d293983 6507 return 0;
1857e1da
DV
6508
6509 /* Ivybridge 3 pipe is really complicated */
6510 switch (pipe) {
6511 case PIPE_A:
6d293983 6512 return 0;
1857e1da 6513 case PIPE_B:
6d293983
ACO
6514 if (pipe_config->fdi_lanes <= 2)
6515 return 0;
6516
6517 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_C));
6518 other_crtc_state =
6519 intel_atomic_get_crtc_state(state, other_crtc);
6520 if (IS_ERR(other_crtc_state))
6521 return PTR_ERR(other_crtc_state);
6522
6523 if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
1857e1da
DV
6524 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
6525 pipe_name(pipe), pipe_config->fdi_lanes);
6d293983 6526 return -EINVAL;
1857e1da 6527 }
6d293983 6528 return 0;
1857e1da 6529 case PIPE_C:
251cc67c
VS
6530 if (pipe_config->fdi_lanes > 2) {
6531 DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
6532 pipe_name(pipe), pipe_config->fdi_lanes);
6d293983 6533 return -EINVAL;
251cc67c 6534 }
6d293983
ACO
6535
6536 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_B));
6537 other_crtc_state =
6538 intel_atomic_get_crtc_state(state, other_crtc);
6539 if (IS_ERR(other_crtc_state))
6540 return PTR_ERR(other_crtc_state);
6541
6542 if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
1857e1da 6543 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
6d293983 6544 return -EINVAL;
1857e1da 6545 }
6d293983 6546 return 0;
1857e1da
DV
6547 default:
6548 BUG();
6549 }
6550}
6551
e29c22c0
DV
6552#define RETRY 1
6553static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
5cec258b 6554 struct intel_crtc_state *pipe_config)
877d48d5 6555{
1857e1da 6556 struct drm_device *dev = intel_crtc->base.dev;
7c5f93b0 6557 const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
6d293983
ACO
6558 int lane, link_bw, fdi_dotclock, ret;
6559 bool needs_recompute = false;
877d48d5 6560
e29c22c0 6561retry:
877d48d5
DV
6562 /* FDI is a binary signal running at ~2.7GHz, encoding
6563 * each output octet as 10 bits. The actual frequency
6564 * is stored as a divider into a 100MHz clock, and the
6565 * mode pixel clock is stored in units of 1KHz.
6566 * Hence the bw of each lane in terms of the mode signal
6567 * is:
6568 */
21a727b3 6569 link_bw = intel_fdi_link_freq(to_i915(dev), pipe_config);
877d48d5 6570
241bfc38 6571 fdi_dotclock = adjusted_mode->crtc_clock;
877d48d5 6572
2bd89a07 6573 lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
877d48d5
DV
6574 pipe_config->pipe_bpp);
6575
6576 pipe_config->fdi_lanes = lane;
6577
2bd89a07 6578 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
877d48d5 6579 link_bw, &pipe_config->fdi_m_n);
1857e1da 6580
e3b247da 6581 ret = ironlake_check_fdi_lanes(dev, intel_crtc->pipe, pipe_config);
6d293983 6582 if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
e29c22c0
DV
6583 pipe_config->pipe_bpp -= 2*3;
6584 DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
6585 pipe_config->pipe_bpp);
6586 needs_recompute = true;
6587 pipe_config->bw_constrained = true;
6588
6589 goto retry;
6590 }
6591
6592 if (needs_recompute)
6593 return RETRY;
6594
6d293983 6595 return ret;
877d48d5
DV
6596}
6597
8cfb3407
VS
6598static bool pipe_config_supports_ips(struct drm_i915_private *dev_priv,
6599 struct intel_crtc_state *pipe_config)
6600{
6601 if (pipe_config->pipe_bpp > 24)
6602 return false;
6603
6604 /* HSW can handle pixel rate up to cdclk? */
2d1fe073 6605 if (IS_HASWELL(dev_priv))
8cfb3407
VS
6606 return true;
6607
6608 /*
b432e5cf
VS
6609 * We compare against max which means we must take
6610 * the increased cdclk requirement into account when
6611 * calculating the new cdclk.
6612 *
6613 * Should measure whether using a lower cdclk w/o IPS
8cfb3407
VS
6614 */
6615 return ilk_pipe_pixel_rate(pipe_config) <=
6616 dev_priv->max_cdclk_freq * 95 / 100;
6617}
6618
42db64ef 6619static void hsw_compute_ips_config(struct intel_crtc *crtc,
5cec258b 6620 struct intel_crtc_state *pipe_config)
42db64ef 6621{
8cfb3407
VS
6622 struct drm_device *dev = crtc->base.dev;
6623 struct drm_i915_private *dev_priv = dev->dev_private;
6624
d330a953 6625 pipe_config->ips_enabled = i915.enable_ips &&
8cfb3407
VS
6626 hsw_crtc_supports_ips(crtc) &&
6627 pipe_config_supports_ips(dev_priv, pipe_config);
42db64ef
PZ
6628}
6629
39acb4aa
VS
6630static bool intel_crtc_supports_double_wide(const struct intel_crtc *crtc)
6631{
6632 const struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6633
6634 /* GDG double wide on either pipe, otherwise pipe A only */
6635 return INTEL_INFO(dev_priv)->gen < 4 &&
6636 (crtc->pipe == PIPE_A || IS_I915G(dev_priv));
6637}
6638
a43f6e0f 6639static int intel_crtc_compute_config(struct intel_crtc *crtc,
5cec258b 6640 struct intel_crtc_state *pipe_config)
79e53945 6641{
a43f6e0f 6642 struct drm_device *dev = crtc->base.dev;
8bd31e67 6643 struct drm_i915_private *dev_priv = dev->dev_private;
7c5f93b0 6644 const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
f3261156 6645 int clock_limit = dev_priv->max_dotclk_freq;
89749350 6646
cf532bb2 6647 if (INTEL_INFO(dev)->gen < 4) {
f3261156 6648 clock_limit = dev_priv->max_cdclk_freq * 9 / 10;
cf532bb2
VS
6649
6650 /*
39acb4aa 6651 * Enable double wide mode when the dot clock
cf532bb2 6652 * is > 90% of the (display) core speed.
cf532bb2 6653 */
39acb4aa
VS
6654 if (intel_crtc_supports_double_wide(crtc) &&
6655 adjusted_mode->crtc_clock > clock_limit) {
f3261156 6656 clock_limit = dev_priv->max_dotclk_freq;
cf532bb2 6657 pipe_config->double_wide = true;
ad3a4479 6658 }
f3261156 6659 }
ad3a4479 6660
f3261156
VS
6661 if (adjusted_mode->crtc_clock > clock_limit) {
6662 DRM_DEBUG_KMS("requested pixel clock (%d kHz) too high (max: %d kHz, double wide: %s)\n",
6663 adjusted_mode->crtc_clock, clock_limit,
6664 yesno(pipe_config->double_wide));
6665 return -EINVAL;
2c07245f 6666 }
89749350 6667
1d1d0e27
VS
6668 /*
6669 * Pipe horizontal size must be even in:
6670 * - DVO ganged mode
6671 * - LVDS dual channel mode
6672 * - Double wide pipe
6673 */
a93e255f 6674 if ((intel_pipe_will_have_type(pipe_config, INTEL_OUTPUT_LVDS) &&
1d1d0e27
VS
6675 intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
6676 pipe_config->pipe_src_w &= ~1;
6677
8693a824
DL
6678 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
6679 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
44f46b42
CW
6680 */
6681 if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
aad941d5 6682 adjusted_mode->crtc_hsync_start == adjusted_mode->crtc_hdisplay)
e29c22c0 6683 return -EINVAL;
44f46b42 6684
f5adf94e 6685 if (HAS_IPS(dev))
a43f6e0f
DV
6686 hsw_compute_ips_config(crtc, pipe_config);
6687
877d48d5 6688 if (pipe_config->has_pch_encoder)
a43f6e0f 6689 return ironlake_fdi_compute_config(crtc, pipe_config);
877d48d5 6690
cf5a15be 6691 return 0;
79e53945
JB
6692}
6693
1652d19e
VS
6694static int skylake_get_display_clock_speed(struct drm_device *dev)
6695{
6696 struct drm_i915_private *dev_priv = to_i915(dev);
ea61791e 6697 uint32_t cdctl;
1652d19e 6698
ea61791e 6699 skl_dpll0_update(dev_priv);
1652d19e 6700
63911d72 6701 if (dev_priv->cdclk_pll.vco == 0)
709e05c3 6702 return dev_priv->cdclk_pll.ref;
1652d19e 6703
ea61791e 6704 cdctl = I915_READ(CDCLK_CTL);
1652d19e 6705
63911d72 6706 if (dev_priv->cdclk_pll.vco == 8640000) {
1652d19e
VS
6707 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6708 case CDCLK_FREQ_450_432:
6709 return 432000;
6710 case CDCLK_FREQ_337_308:
487ed2e4 6711 return 308571;
ea61791e
VS
6712 case CDCLK_FREQ_540:
6713 return 540000;
1652d19e 6714 case CDCLK_FREQ_675_617:
487ed2e4 6715 return 617143;
1652d19e 6716 default:
ea61791e 6717 MISSING_CASE(cdctl & CDCLK_FREQ_SEL_MASK);
1652d19e
VS
6718 }
6719 } else {
1652d19e
VS
6720 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6721 case CDCLK_FREQ_450_432:
6722 return 450000;
6723 case CDCLK_FREQ_337_308:
6724 return 337500;
ea61791e
VS
6725 case CDCLK_FREQ_540:
6726 return 540000;
1652d19e
VS
6727 case CDCLK_FREQ_675_617:
6728 return 675000;
6729 default:
ea61791e 6730 MISSING_CASE(cdctl & CDCLK_FREQ_SEL_MASK);
1652d19e
VS
6731 }
6732 }
6733
709e05c3 6734 return dev_priv->cdclk_pll.ref;
1652d19e
VS
6735}
6736
83d7c81f
VS
6737static void bxt_de_pll_update(struct drm_i915_private *dev_priv)
6738{
6739 u32 val;
6740
6741 dev_priv->cdclk_pll.ref = 19200;
1c3f7700 6742 dev_priv->cdclk_pll.vco = 0;
83d7c81f
VS
6743
6744 val = I915_READ(BXT_DE_PLL_ENABLE);
1c3f7700 6745 if ((val & BXT_DE_PLL_PLL_ENABLE) == 0)
83d7c81f 6746 return;
83d7c81f 6747
1c3f7700
ID
6748 if (WARN_ON((val & BXT_DE_PLL_LOCK) == 0))
6749 return;
83d7c81f
VS
6750
6751 val = I915_READ(BXT_DE_PLL_CTL);
6752 dev_priv->cdclk_pll.vco = (val & BXT_DE_PLL_RATIO_MASK) *
6753 dev_priv->cdclk_pll.ref;
6754}
6755
acd3f3d3
BP
6756static int broxton_get_display_clock_speed(struct drm_device *dev)
6757{
6758 struct drm_i915_private *dev_priv = to_i915(dev);
f5986242
VS
6759 u32 divider;
6760 int div, vco;
acd3f3d3 6761
83d7c81f
VS
6762 bxt_de_pll_update(dev_priv);
6763
f5986242
VS
6764 vco = dev_priv->cdclk_pll.vco;
6765 if (vco == 0)
6766 return dev_priv->cdclk_pll.ref;
acd3f3d3 6767
f5986242 6768 divider = I915_READ(CDCLK_CTL) & BXT_CDCLK_CD2X_DIV_SEL_MASK;
acd3f3d3 6769
f5986242 6770 switch (divider) {
acd3f3d3 6771 case BXT_CDCLK_CD2X_DIV_SEL_1:
f5986242
VS
6772 div = 2;
6773 break;
acd3f3d3 6774 case BXT_CDCLK_CD2X_DIV_SEL_1_5:
f5986242
VS
6775 div = 3;
6776 break;
acd3f3d3 6777 case BXT_CDCLK_CD2X_DIV_SEL_2:
f5986242
VS
6778 div = 4;
6779 break;
acd3f3d3 6780 case BXT_CDCLK_CD2X_DIV_SEL_4:
f5986242
VS
6781 div = 8;
6782 break;
6783 default:
6784 MISSING_CASE(divider);
6785 return dev_priv->cdclk_pll.ref;
acd3f3d3
BP
6786 }
6787
f5986242 6788 return DIV_ROUND_CLOSEST(vco, div);
acd3f3d3
BP
6789}
6790
1652d19e
VS
6791static int broadwell_get_display_clock_speed(struct drm_device *dev)
6792{
6793 struct drm_i915_private *dev_priv = dev->dev_private;
6794 uint32_t lcpll = I915_READ(LCPLL_CTL);
6795 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6796
6797 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6798 return 800000;
6799 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6800 return 450000;
6801 else if (freq == LCPLL_CLK_FREQ_450)
6802 return 450000;
6803 else if (freq == LCPLL_CLK_FREQ_54O_BDW)
6804 return 540000;
6805 else if (freq == LCPLL_CLK_FREQ_337_5_BDW)
6806 return 337500;
6807 else
6808 return 675000;
6809}
6810
6811static int haswell_get_display_clock_speed(struct drm_device *dev)
6812{
6813 struct drm_i915_private *dev_priv = dev->dev_private;
6814 uint32_t lcpll = I915_READ(LCPLL_CTL);
6815 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6816
6817 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6818 return 800000;
6819 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6820 return 450000;
6821 else if (freq == LCPLL_CLK_FREQ_450)
6822 return 450000;
6823 else if (IS_HSW_ULT(dev))
6824 return 337500;
6825 else
6826 return 540000;
79e53945
JB
6827}
6828
25eb05fc
JB
6829static int valleyview_get_display_clock_speed(struct drm_device *dev)
6830{
bfa7df01
VS
6831 return vlv_get_cck_clock_hpll(to_i915(dev), "cdclk",
6832 CCK_DISPLAY_CLOCK_CONTROL);
25eb05fc
JB
6833}
6834
b37a6434
VS
6835static int ilk_get_display_clock_speed(struct drm_device *dev)
6836{
6837 return 450000;
6838}
6839
e70236a8
JB
6840static int i945_get_display_clock_speed(struct drm_device *dev)
6841{
6842 return 400000;
6843}
79e53945 6844
e70236a8 6845static int i915_get_display_clock_speed(struct drm_device *dev)
79e53945 6846{
e907f170 6847 return 333333;
e70236a8 6848}
79e53945 6849
e70236a8
JB
6850static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
6851{
6852 return 200000;
6853}
79e53945 6854
257a7ffc
DV
6855static int pnv_get_display_clock_speed(struct drm_device *dev)
6856{
6857 u16 gcfgc = 0;
6858
6859 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6860
6861 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6862 case GC_DISPLAY_CLOCK_267_MHZ_PNV:
e907f170 6863 return 266667;
257a7ffc 6864 case GC_DISPLAY_CLOCK_333_MHZ_PNV:
e907f170 6865 return 333333;
257a7ffc 6866 case GC_DISPLAY_CLOCK_444_MHZ_PNV:
e907f170 6867 return 444444;
257a7ffc
DV
6868 case GC_DISPLAY_CLOCK_200_MHZ_PNV:
6869 return 200000;
6870 default:
6871 DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
6872 case GC_DISPLAY_CLOCK_133_MHZ_PNV:
e907f170 6873 return 133333;
257a7ffc 6874 case GC_DISPLAY_CLOCK_167_MHZ_PNV:
e907f170 6875 return 166667;
257a7ffc
DV
6876 }
6877}
6878
e70236a8
JB
6879static int i915gm_get_display_clock_speed(struct drm_device *dev)
6880{
6881 u16 gcfgc = 0;
79e53945 6882
e70236a8
JB
6883 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6884
6885 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
e907f170 6886 return 133333;
e70236a8
JB
6887 else {
6888 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6889 case GC_DISPLAY_CLOCK_333_MHZ:
e907f170 6890 return 333333;
e70236a8
JB
6891 default:
6892 case GC_DISPLAY_CLOCK_190_200_MHZ:
6893 return 190000;
79e53945 6894 }
e70236a8
JB
6895 }
6896}
6897
6898static int i865_get_display_clock_speed(struct drm_device *dev)
6899{
e907f170 6900 return 266667;
e70236a8
JB
6901}
6902
1b1d2716 6903static int i85x_get_display_clock_speed(struct drm_device *dev)
e70236a8
JB
6904{
6905 u16 hpllcc = 0;
1b1d2716 6906
65cd2b3f
VS
6907 /*
6908 * 852GM/852GMV only supports 133 MHz and the HPLLCC
6909 * encoding is different :(
6910 * FIXME is this the right way to detect 852GM/852GMV?
6911 */
6912 if (dev->pdev->revision == 0x1)
6913 return 133333;
6914
1b1d2716
VS
6915 pci_bus_read_config_word(dev->pdev->bus,
6916 PCI_DEVFN(0, 3), HPLLCC, &hpllcc);
6917
e70236a8
JB
6918 /* Assume that the hardware is in the high speed state. This
6919 * should be the default.
6920 */
6921 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
6922 case GC_CLOCK_133_200:
1b1d2716 6923 case GC_CLOCK_133_200_2:
e70236a8
JB
6924 case GC_CLOCK_100_200:
6925 return 200000;
6926 case GC_CLOCK_166_250:
6927 return 250000;
6928 case GC_CLOCK_100_133:
e907f170 6929 return 133333;
1b1d2716
VS
6930 case GC_CLOCK_133_266:
6931 case GC_CLOCK_133_266_2:
6932 case GC_CLOCK_166_266:
6933 return 266667;
e70236a8 6934 }
79e53945 6935
e70236a8
JB
6936 /* Shouldn't happen */
6937 return 0;
6938}
79e53945 6939
e70236a8
JB
6940static int i830_get_display_clock_speed(struct drm_device *dev)
6941{
e907f170 6942 return 133333;
79e53945
JB
6943}
6944
34edce2f
VS
6945static unsigned int intel_hpll_vco(struct drm_device *dev)
6946{
6947 struct drm_i915_private *dev_priv = dev->dev_private;
6948 static const unsigned int blb_vco[8] = {
6949 [0] = 3200000,
6950 [1] = 4000000,
6951 [2] = 5333333,
6952 [3] = 4800000,
6953 [4] = 6400000,
6954 };
6955 static const unsigned int pnv_vco[8] = {
6956 [0] = 3200000,
6957 [1] = 4000000,
6958 [2] = 5333333,
6959 [3] = 4800000,
6960 [4] = 2666667,
6961 };
6962 static const unsigned int cl_vco[8] = {
6963 [0] = 3200000,
6964 [1] = 4000000,
6965 [2] = 5333333,
6966 [3] = 6400000,
6967 [4] = 3333333,
6968 [5] = 3566667,
6969 [6] = 4266667,
6970 };
6971 static const unsigned int elk_vco[8] = {
6972 [0] = 3200000,
6973 [1] = 4000000,
6974 [2] = 5333333,
6975 [3] = 4800000,
6976 };
6977 static const unsigned int ctg_vco[8] = {
6978 [0] = 3200000,
6979 [1] = 4000000,
6980 [2] = 5333333,
6981 [3] = 6400000,
6982 [4] = 2666667,
6983 [5] = 4266667,
6984 };
6985 const unsigned int *vco_table;
6986 unsigned int vco;
6987 uint8_t tmp = 0;
6988
6989 /* FIXME other chipsets? */
6990 if (IS_GM45(dev))
6991 vco_table = ctg_vco;
6992 else if (IS_G4X(dev))
6993 vco_table = elk_vco;
6994 else if (IS_CRESTLINE(dev))
6995 vco_table = cl_vco;
6996 else if (IS_PINEVIEW(dev))
6997 vco_table = pnv_vco;
6998 else if (IS_G33(dev))
6999 vco_table = blb_vco;
7000 else
7001 return 0;
7002
7003 tmp = I915_READ(IS_MOBILE(dev) ? HPLLVCO_MOBILE : HPLLVCO);
7004
7005 vco = vco_table[tmp & 0x7];
7006 if (vco == 0)
7007 DRM_ERROR("Bad HPLL VCO (HPLLVCO=0x%02x)\n", tmp);
7008 else
7009 DRM_DEBUG_KMS("HPLL VCO %u kHz\n", vco);
7010
7011 return vco;
7012}
7013
7014static int gm45_get_display_clock_speed(struct drm_device *dev)
7015{
7016 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
7017 uint16_t tmp = 0;
7018
7019 pci_read_config_word(dev->pdev, GCFGC, &tmp);
7020
7021 cdclk_sel = (tmp >> 12) & 0x1;
7022
7023 switch (vco) {
7024 case 2666667:
7025 case 4000000:
7026 case 5333333:
7027 return cdclk_sel ? 333333 : 222222;
7028 case 3200000:
7029 return cdclk_sel ? 320000 : 228571;
7030 default:
7031 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u, CFGC=0x%04x\n", vco, tmp);
7032 return 222222;
7033 }
7034}
7035
7036static int i965gm_get_display_clock_speed(struct drm_device *dev)
7037{
7038 static const uint8_t div_3200[] = { 16, 10, 8 };
7039 static const uint8_t div_4000[] = { 20, 12, 10 };
7040 static const uint8_t div_5333[] = { 24, 16, 14 };
7041 const uint8_t *div_table;
7042 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
7043 uint16_t tmp = 0;
7044
7045 pci_read_config_word(dev->pdev, GCFGC, &tmp);
7046
7047 cdclk_sel = ((tmp >> 8) & 0x1f) - 1;
7048
7049 if (cdclk_sel >= ARRAY_SIZE(div_3200))
7050 goto fail;
7051
7052 switch (vco) {
7053 case 3200000:
7054 div_table = div_3200;
7055 break;
7056 case 4000000:
7057 div_table = div_4000;
7058 break;
7059 case 5333333:
7060 div_table = div_5333;
7061 break;
7062 default:
7063 goto fail;
7064 }
7065
7066 return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
7067
caf4e252 7068fail:
34edce2f
VS
7069 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%04x\n", vco, tmp);
7070 return 200000;
7071}
7072
7073static int g33_get_display_clock_speed(struct drm_device *dev)
7074{
7075 static const uint8_t div_3200[] = { 12, 10, 8, 7, 5, 16 };
7076 static const uint8_t div_4000[] = { 14, 12, 10, 8, 6, 20 };
7077 static const uint8_t div_4800[] = { 20, 14, 12, 10, 8, 24 };
7078 static const uint8_t div_5333[] = { 20, 16, 12, 12, 8, 28 };
7079 const uint8_t *div_table;
7080 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
7081 uint16_t tmp = 0;
7082
7083 pci_read_config_word(dev->pdev, GCFGC, &tmp);
7084
7085 cdclk_sel = (tmp >> 4) & 0x7;
7086
7087 if (cdclk_sel >= ARRAY_SIZE(div_3200))
7088 goto fail;
7089
7090 switch (vco) {
7091 case 3200000:
7092 div_table = div_3200;
7093 break;
7094 case 4000000:
7095 div_table = div_4000;
7096 break;
7097 case 4800000:
7098 div_table = div_4800;
7099 break;
7100 case 5333333:
7101 div_table = div_5333;
7102 break;
7103 default:
7104 goto fail;
7105 }
7106
7107 return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
7108
caf4e252 7109fail:
34edce2f
VS
7110 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%08x\n", vco, tmp);
7111 return 190476;
7112}
7113
2c07245f 7114static void
a65851af 7115intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
2c07245f 7116{
a65851af
VS
7117 while (*num > DATA_LINK_M_N_MASK ||
7118 *den > DATA_LINK_M_N_MASK) {
2c07245f
ZW
7119 *num >>= 1;
7120 *den >>= 1;
7121 }
7122}
7123
a65851af
VS
7124static void compute_m_n(unsigned int m, unsigned int n,
7125 uint32_t *ret_m, uint32_t *ret_n)
7126{
7127 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
7128 *ret_m = div_u64((uint64_t) m * *ret_n, n);
7129 intel_reduce_m_n_ratio(ret_m, ret_n);
7130}
7131
e69d0bc1
DV
7132void
7133intel_link_compute_m_n(int bits_per_pixel, int nlanes,
7134 int pixel_clock, int link_clock,
7135 struct intel_link_m_n *m_n)
2c07245f 7136{
e69d0bc1 7137 m_n->tu = 64;
a65851af
VS
7138
7139 compute_m_n(bits_per_pixel * pixel_clock,
7140 link_clock * nlanes * 8,
7141 &m_n->gmch_m, &m_n->gmch_n);
7142
7143 compute_m_n(pixel_clock, link_clock,
7144 &m_n->link_m, &m_n->link_n);
2c07245f
ZW
7145}
7146
a7615030
CW
7147static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
7148{
d330a953
JN
7149 if (i915.panel_use_ssc >= 0)
7150 return i915.panel_use_ssc != 0;
41aa3448 7151 return dev_priv->vbt.lvds_use_ssc
435793df 7152 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
a7615030
CW
7153}
7154
7429e9d4 7155static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
c65d77d8 7156{
7df00d7a 7157 return (1 << dpll->n) << 16 | dpll->m2;
7429e9d4 7158}
f47709a9 7159
7429e9d4
DV
7160static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
7161{
7162 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
c65d77d8
JB
7163}
7164
f47709a9 7165static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
190f68c5 7166 struct intel_crtc_state *crtc_state,
9e2c8475 7167 struct dpll *reduced_clock)
a7516a05 7168{
f47709a9 7169 struct drm_device *dev = crtc->base.dev;
a7516a05
JB
7170 u32 fp, fp2 = 0;
7171
7172 if (IS_PINEVIEW(dev)) {
190f68c5 7173 fp = pnv_dpll_compute_fp(&crtc_state->dpll);
a7516a05 7174 if (reduced_clock)
7429e9d4 7175 fp2 = pnv_dpll_compute_fp(reduced_clock);
a7516a05 7176 } else {
190f68c5 7177 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
a7516a05 7178 if (reduced_clock)
7429e9d4 7179 fp2 = i9xx_dpll_compute_fp(reduced_clock);
a7516a05
JB
7180 }
7181
190f68c5 7182 crtc_state->dpll_hw_state.fp0 = fp;
a7516a05 7183
f47709a9 7184 crtc->lowfreq_avail = false;
a93e255f 7185 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
ab585dea 7186 reduced_clock) {
190f68c5 7187 crtc_state->dpll_hw_state.fp1 = fp2;
f47709a9 7188 crtc->lowfreq_avail = true;
a7516a05 7189 } else {
190f68c5 7190 crtc_state->dpll_hw_state.fp1 = fp;
a7516a05
JB
7191 }
7192}
7193
5e69f97f
CML
7194static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
7195 pipe)
89b667f8
JB
7196{
7197 u32 reg_val;
7198
7199 /*
7200 * PLLB opamp always calibrates to max value of 0x3f, force enable it
7201 * and set it to a reasonable value instead.
7202 */
ab3c759a 7203 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
89b667f8
JB
7204 reg_val &= 0xffffff00;
7205 reg_val |= 0x00000030;
ab3c759a 7206 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
89b667f8 7207
ab3c759a 7208 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
89b667f8
JB
7209 reg_val &= 0x8cffffff;
7210 reg_val = 0x8c000000;
ab3c759a 7211 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
89b667f8 7212
ab3c759a 7213 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
89b667f8 7214 reg_val &= 0xffffff00;
ab3c759a 7215 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
89b667f8 7216
ab3c759a 7217 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
89b667f8
JB
7218 reg_val &= 0x00ffffff;
7219 reg_val |= 0xb0000000;
ab3c759a 7220 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
89b667f8
JB
7221}
7222
b551842d
DV
7223static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
7224 struct intel_link_m_n *m_n)
7225{
7226 struct drm_device *dev = crtc->base.dev;
7227 struct drm_i915_private *dev_priv = dev->dev_private;
7228 int pipe = crtc->pipe;
7229
e3b95f1e
DV
7230 I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7231 I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
7232 I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
7233 I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
b551842d
DV
7234}
7235
7236static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
f769cd24
VK
7237 struct intel_link_m_n *m_n,
7238 struct intel_link_m_n *m2_n2)
b551842d
DV
7239{
7240 struct drm_device *dev = crtc->base.dev;
7241 struct drm_i915_private *dev_priv = dev->dev_private;
7242 int pipe = crtc->pipe;
6e3c9717 7243 enum transcoder transcoder = crtc->config->cpu_transcoder;
b551842d
DV
7244
7245 if (INTEL_INFO(dev)->gen >= 5) {
7246 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
7247 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
7248 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
7249 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
f769cd24
VK
7250 /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
7251 * for gen < 8) and if DRRS is supported (to make sure the
7252 * registers are not unnecessarily accessed).
7253 */
44395bfe 7254 if (m2_n2 && (IS_CHERRYVIEW(dev) || INTEL_INFO(dev)->gen < 8) &&
6e3c9717 7255 crtc->config->has_drrs) {
f769cd24
VK
7256 I915_WRITE(PIPE_DATA_M2(transcoder),
7257 TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
7258 I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
7259 I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
7260 I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
7261 }
b551842d 7262 } else {
e3b95f1e
DV
7263 I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7264 I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
7265 I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
7266 I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
b551842d
DV
7267 }
7268}
7269
fe3cd48d 7270void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
03afc4a2 7271{
fe3cd48d
R
7272 struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
7273
7274 if (m_n == M1_N1) {
7275 dp_m_n = &crtc->config->dp_m_n;
7276 dp_m2_n2 = &crtc->config->dp_m2_n2;
7277 } else if (m_n == M2_N2) {
7278
7279 /*
7280 * M2_N2 registers are not supported. Hence m2_n2 divider value
7281 * needs to be programmed into M1_N1.
7282 */
7283 dp_m_n = &crtc->config->dp_m2_n2;
7284 } else {
7285 DRM_ERROR("Unsupported divider value\n");
7286 return;
7287 }
7288
6e3c9717
ACO
7289 if (crtc->config->has_pch_encoder)
7290 intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
03afc4a2 7291 else
fe3cd48d 7292 intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
03afc4a2
DV
7293}
7294
251ac862
DV
7295static void vlv_compute_dpll(struct intel_crtc *crtc,
7296 struct intel_crtc_state *pipe_config)
bdd4b6a6 7297{
03ed5cbf 7298 pipe_config->dpll_hw_state.dpll = DPLL_INTEGRATED_REF_CLK_VLV |
cd2d34d9 7299 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
03ed5cbf
VS
7300 if (crtc->pipe != PIPE_A)
7301 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
bdd4b6a6 7302
cd2d34d9 7303 /* DPLL not used with DSI, but still need the rest set up */
187a1c07 7304 if (!pipe_config->has_dsi_encoder)
cd2d34d9
VS
7305 pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE |
7306 DPLL_EXT_BUFFER_ENABLE_VLV;
7307
03ed5cbf
VS
7308 pipe_config->dpll_hw_state.dpll_md =
7309 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
7310}
bdd4b6a6 7311
03ed5cbf
VS
7312static void chv_compute_dpll(struct intel_crtc *crtc,
7313 struct intel_crtc_state *pipe_config)
7314{
7315 pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLK_CHV |
cd2d34d9 7316 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
03ed5cbf
VS
7317 if (crtc->pipe != PIPE_A)
7318 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
7319
cd2d34d9 7320 /* DPLL not used with DSI, but still need the rest set up */
187a1c07 7321 if (!pipe_config->has_dsi_encoder)
cd2d34d9
VS
7322 pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE;
7323
03ed5cbf
VS
7324 pipe_config->dpll_hw_state.dpll_md =
7325 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
bdd4b6a6
DV
7326}
7327
d288f65f 7328static void vlv_prepare_pll(struct intel_crtc *crtc,
5cec258b 7329 const struct intel_crtc_state *pipe_config)
a0c4da24 7330{
f47709a9 7331 struct drm_device *dev = crtc->base.dev;
a0c4da24 7332 struct drm_i915_private *dev_priv = dev->dev_private;
cd2d34d9 7333 enum pipe pipe = crtc->pipe;
bdd4b6a6 7334 u32 mdiv;
a0c4da24 7335 u32 bestn, bestm1, bestm2, bestp1, bestp2;
bdd4b6a6 7336 u32 coreclk, reg_val;
a0c4da24 7337
cd2d34d9
VS
7338 /* Enable Refclk */
7339 I915_WRITE(DPLL(pipe),
7340 pipe_config->dpll_hw_state.dpll &
7341 ~(DPLL_VCO_ENABLE | DPLL_EXT_BUFFER_ENABLE_VLV));
7342
7343 /* No need to actually set up the DPLL with DSI */
7344 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
7345 return;
7346
a580516d 7347 mutex_lock(&dev_priv->sb_lock);
09153000 7348
d288f65f
VS
7349 bestn = pipe_config->dpll.n;
7350 bestm1 = pipe_config->dpll.m1;
7351 bestm2 = pipe_config->dpll.m2;
7352 bestp1 = pipe_config->dpll.p1;
7353 bestp2 = pipe_config->dpll.p2;
a0c4da24 7354
89b667f8
JB
7355 /* See eDP HDMI DPIO driver vbios notes doc */
7356
7357 /* PLL B needs special handling */
bdd4b6a6 7358 if (pipe == PIPE_B)
5e69f97f 7359 vlv_pllb_recal_opamp(dev_priv, pipe);
89b667f8
JB
7360
7361 /* Set up Tx target for periodic Rcomp update */
ab3c759a 7362 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
89b667f8
JB
7363
7364 /* Disable target IRef on PLL */
ab3c759a 7365 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
89b667f8 7366 reg_val &= 0x00ffffff;
ab3c759a 7367 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
89b667f8
JB
7368
7369 /* Disable fast lock */
ab3c759a 7370 vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
89b667f8
JB
7371
7372 /* Set idtafcrecal before PLL is enabled */
a0c4da24
JB
7373 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
7374 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
7375 mdiv |= ((bestn << DPIO_N_SHIFT));
a0c4da24 7376 mdiv |= (1 << DPIO_K_SHIFT);
7df5080b
JB
7377
7378 /*
7379 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
7380 * but we don't support that).
7381 * Note: don't use the DAC post divider as it seems unstable.
7382 */
7383 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
ab3c759a 7384 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
a0c4da24 7385
a0c4da24 7386 mdiv |= DPIO_ENABLE_CALIBRATION;
ab3c759a 7387 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
a0c4da24 7388
89b667f8 7389 /* Set HBR and RBR LPF coefficients */
d288f65f 7390 if (pipe_config->port_clock == 162000 ||
409ee761
ACO
7391 intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG) ||
7392 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
ab3c759a 7393 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
885b0120 7394 0x009f0003);
89b667f8 7395 else
ab3c759a 7396 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
89b667f8
JB
7397 0x00d0000f);
7398
681a8504 7399 if (pipe_config->has_dp_encoder) {
89b667f8 7400 /* Use SSC source */
bdd4b6a6 7401 if (pipe == PIPE_A)
ab3c759a 7402 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
7403 0x0df40000);
7404 else
ab3c759a 7405 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
7406 0x0df70000);
7407 } else { /* HDMI or VGA */
7408 /* Use bend source */
bdd4b6a6 7409 if (pipe == PIPE_A)
ab3c759a 7410 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
7411 0x0df70000);
7412 else
ab3c759a 7413 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
7414 0x0df40000);
7415 }
a0c4da24 7416
ab3c759a 7417 coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
89b667f8 7418 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
409ee761
ACO
7419 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
7420 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
89b667f8 7421 coreclk |= 0x01000000;
ab3c759a 7422 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
a0c4da24 7423
ab3c759a 7424 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
a580516d 7425 mutex_unlock(&dev_priv->sb_lock);
a0c4da24
JB
7426}
7427
d288f65f 7428static void chv_prepare_pll(struct intel_crtc *crtc,
5cec258b 7429 const struct intel_crtc_state *pipe_config)
9d556c99
CML
7430{
7431 struct drm_device *dev = crtc->base.dev;
7432 struct drm_i915_private *dev_priv = dev->dev_private;
cd2d34d9 7433 enum pipe pipe = crtc->pipe;
9d556c99 7434 enum dpio_channel port = vlv_pipe_to_channel(pipe);
9cbe40c1 7435 u32 loopfilter, tribuf_calcntr;
9d556c99 7436 u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
a945ce7e 7437 u32 dpio_val;
9cbe40c1 7438 int vco;
9d556c99 7439
cd2d34d9
VS
7440 /* Enable Refclk and SSC */
7441 I915_WRITE(DPLL(pipe),
7442 pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
7443
7444 /* No need to actually set up the DPLL with DSI */
7445 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
7446 return;
7447
d288f65f
VS
7448 bestn = pipe_config->dpll.n;
7449 bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
7450 bestm1 = pipe_config->dpll.m1;
7451 bestm2 = pipe_config->dpll.m2 >> 22;
7452 bestp1 = pipe_config->dpll.p1;
7453 bestp2 = pipe_config->dpll.p2;
9cbe40c1 7454 vco = pipe_config->dpll.vco;
a945ce7e 7455 dpio_val = 0;
9cbe40c1 7456 loopfilter = 0;
9d556c99 7457
a580516d 7458 mutex_lock(&dev_priv->sb_lock);
9d556c99 7459
9d556c99
CML
7460 /* p1 and p2 divider */
7461 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
7462 5 << DPIO_CHV_S1_DIV_SHIFT |
7463 bestp1 << DPIO_CHV_P1_DIV_SHIFT |
7464 bestp2 << DPIO_CHV_P2_DIV_SHIFT |
7465 1 << DPIO_CHV_K_DIV_SHIFT);
7466
7467 /* Feedback post-divider - m2 */
7468 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
7469
7470 /* Feedback refclk divider - n and m1 */
7471 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
7472 DPIO_CHV_M1_DIV_BY_2 |
7473 1 << DPIO_CHV_N_DIV_SHIFT);
7474
7475 /* M2 fraction division */
25a25dfc 7476 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
9d556c99
CML
7477
7478 /* M2 fraction division enable */
a945ce7e
VP
7479 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
7480 dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
7481 dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
7482 if (bestm2_frac)
7483 dpio_val |= DPIO_CHV_FRAC_DIV_EN;
7484 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
9d556c99 7485
de3a0fde
VP
7486 /* Program digital lock detect threshold */
7487 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
7488 dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
7489 DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
7490 dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
7491 if (!bestm2_frac)
7492 dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
7493 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
7494
9d556c99 7495 /* Loop filter */
9cbe40c1
VP
7496 if (vco == 5400000) {
7497 loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
7498 loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
7499 loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
7500 tribuf_calcntr = 0x9;
7501 } else if (vco <= 6200000) {
7502 loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
7503 loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
7504 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7505 tribuf_calcntr = 0x9;
7506 } else if (vco <= 6480000) {
7507 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7508 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7509 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7510 tribuf_calcntr = 0x8;
7511 } else {
7512 /* Not supported. Apply the same limits as in the max case */
7513 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7514 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7515 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7516 tribuf_calcntr = 0;
7517 }
9d556c99
CML
7518 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
7519
968040b2 7520 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
9cbe40c1
VP
7521 dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
7522 dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
7523 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
7524
9d556c99
CML
7525 /* AFC Recal */
7526 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
7527 vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
7528 DPIO_AFC_RECAL);
7529
a580516d 7530 mutex_unlock(&dev_priv->sb_lock);
9d556c99
CML
7531}
7532
d288f65f
VS
7533/**
7534 * vlv_force_pll_on - forcibly enable just the PLL
7535 * @dev_priv: i915 private structure
7536 * @pipe: pipe PLL to enable
7537 * @dpll: PLL configuration
7538 *
7539 * Enable the PLL for @pipe using the supplied @dpll config. To be used
7540 * in cases where we need the PLL enabled even when @pipe is not going to
7541 * be enabled.
7542 */
3f36b937
TU
7543int vlv_force_pll_on(struct drm_device *dev, enum pipe pipe,
7544 const struct dpll *dpll)
d288f65f
VS
7545{
7546 struct intel_crtc *crtc =
7547 to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
3f36b937
TU
7548 struct intel_crtc_state *pipe_config;
7549
7550 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
7551 if (!pipe_config)
7552 return -ENOMEM;
7553
7554 pipe_config->base.crtc = &crtc->base;
7555 pipe_config->pixel_multiplier = 1;
7556 pipe_config->dpll = *dpll;
d288f65f
VS
7557
7558 if (IS_CHERRYVIEW(dev)) {
3f36b937
TU
7559 chv_compute_dpll(crtc, pipe_config);
7560 chv_prepare_pll(crtc, pipe_config);
7561 chv_enable_pll(crtc, pipe_config);
d288f65f 7562 } else {
3f36b937
TU
7563 vlv_compute_dpll(crtc, pipe_config);
7564 vlv_prepare_pll(crtc, pipe_config);
7565 vlv_enable_pll(crtc, pipe_config);
d288f65f 7566 }
3f36b937
TU
7567
7568 kfree(pipe_config);
7569
7570 return 0;
d288f65f
VS
7571}
7572
7573/**
7574 * vlv_force_pll_off - forcibly disable just the PLL
7575 * @dev_priv: i915 private structure
7576 * @pipe: pipe PLL to disable
7577 *
7578 * Disable the PLL for @pipe. To be used in cases where we need
7579 * the PLL enabled even when @pipe is not going to be enabled.
7580 */
7581void vlv_force_pll_off(struct drm_device *dev, enum pipe pipe)
7582{
7583 if (IS_CHERRYVIEW(dev))
7584 chv_disable_pll(to_i915(dev), pipe);
7585 else
7586 vlv_disable_pll(to_i915(dev), pipe);
7587}
7588
251ac862
DV
7589static void i9xx_compute_dpll(struct intel_crtc *crtc,
7590 struct intel_crtc_state *crtc_state,
9e2c8475 7591 struct dpll *reduced_clock)
eb1cbe48 7592{
f47709a9 7593 struct drm_device *dev = crtc->base.dev;
eb1cbe48 7594 struct drm_i915_private *dev_priv = dev->dev_private;
eb1cbe48
DV
7595 u32 dpll;
7596 bool is_sdvo;
190f68c5 7597 struct dpll *clock = &crtc_state->dpll;
eb1cbe48 7598
190f68c5 7599 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
2a8f64ca 7600
a93e255f
ACO
7601 is_sdvo = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO) ||
7602 intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI);
eb1cbe48
DV
7603
7604 dpll = DPLL_VGA_MODE_DIS;
7605
a93e255f 7606 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
eb1cbe48
DV
7607 dpll |= DPLLB_MODE_LVDS;
7608 else
7609 dpll |= DPLLB_MODE_DAC_SERIAL;
6cc5f341 7610
ef1b460d 7611 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
190f68c5 7612 dpll |= (crtc_state->pixel_multiplier - 1)
198a037f 7613 << SDVO_MULTIPLIER_SHIFT_HIRES;
eb1cbe48 7614 }
198a037f
DV
7615
7616 if (is_sdvo)
4a33e48d 7617 dpll |= DPLL_SDVO_HIGH_SPEED;
198a037f 7618
190f68c5 7619 if (crtc_state->has_dp_encoder)
4a33e48d 7620 dpll |= DPLL_SDVO_HIGH_SPEED;
eb1cbe48
DV
7621
7622 /* compute bitmask from p1 value */
7623 if (IS_PINEVIEW(dev))
7624 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
7625 else {
7626 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7627 if (IS_G4X(dev) && reduced_clock)
7628 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
7629 }
7630 switch (clock->p2) {
7631 case 5:
7632 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
7633 break;
7634 case 7:
7635 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
7636 break;
7637 case 10:
7638 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
7639 break;
7640 case 14:
7641 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
7642 break;
7643 }
7644 if (INTEL_INFO(dev)->gen >= 4)
7645 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
7646
190f68c5 7647 if (crtc_state->sdvo_tv_clock)
eb1cbe48 7648 dpll |= PLL_REF_INPUT_TVCLKINBC;
a93e255f 7649 else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
ceb41007 7650 intel_panel_use_ssc(dev_priv))
eb1cbe48
DV
7651 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7652 else
7653 dpll |= PLL_REF_INPUT_DREFCLK;
7654
7655 dpll |= DPLL_VCO_ENABLE;
190f68c5 7656 crtc_state->dpll_hw_state.dpll = dpll;
8bcc2795 7657
eb1cbe48 7658 if (INTEL_INFO(dev)->gen >= 4) {
190f68c5 7659 u32 dpll_md = (crtc_state->pixel_multiplier - 1)
ef1b460d 7660 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
190f68c5 7661 crtc_state->dpll_hw_state.dpll_md = dpll_md;
eb1cbe48
DV
7662 }
7663}
7664
251ac862
DV
7665static void i8xx_compute_dpll(struct intel_crtc *crtc,
7666 struct intel_crtc_state *crtc_state,
9e2c8475 7667 struct dpll *reduced_clock)
eb1cbe48 7668{
f47709a9 7669 struct drm_device *dev = crtc->base.dev;
eb1cbe48 7670 struct drm_i915_private *dev_priv = dev->dev_private;
eb1cbe48 7671 u32 dpll;
190f68c5 7672 struct dpll *clock = &crtc_state->dpll;
eb1cbe48 7673
190f68c5 7674 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
2a8f64ca 7675
eb1cbe48
DV
7676 dpll = DPLL_VGA_MODE_DIS;
7677
a93e255f 7678 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
eb1cbe48
DV
7679 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7680 } else {
7681 if (clock->p1 == 2)
7682 dpll |= PLL_P1_DIVIDE_BY_TWO;
7683 else
7684 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7685 if (clock->p2 == 4)
7686 dpll |= PLL_P2_DIVIDE_BY_4;
7687 }
7688
a93e255f 7689 if (!IS_I830(dev) && intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
4a33e48d
DV
7690 dpll |= DPLL_DVO_2X_MODE;
7691
a93e255f 7692 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
ceb41007 7693 intel_panel_use_ssc(dev_priv))
eb1cbe48
DV
7694 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7695 else
7696 dpll |= PLL_REF_INPUT_DREFCLK;
7697
7698 dpll |= DPLL_VCO_ENABLE;
190f68c5 7699 crtc_state->dpll_hw_state.dpll = dpll;
eb1cbe48
DV
7700}
7701
8a654f3b 7702static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
b0e77b9c
PZ
7703{
7704 struct drm_device *dev = intel_crtc->base.dev;
7705 struct drm_i915_private *dev_priv = dev->dev_private;
7706 enum pipe pipe = intel_crtc->pipe;
6e3c9717 7707 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
7c5f93b0 7708 const struct drm_display_mode *adjusted_mode = &intel_crtc->config->base.adjusted_mode;
1caea6e9
VS
7709 uint32_t crtc_vtotal, crtc_vblank_end;
7710 int vsyncshift = 0;
4d8a62ea
DV
7711
7712 /* We need to be careful not to changed the adjusted mode, for otherwise
7713 * the hw state checker will get angry at the mismatch. */
7714 crtc_vtotal = adjusted_mode->crtc_vtotal;
7715 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
b0e77b9c 7716
609aeaca 7717 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
b0e77b9c 7718 /* the chip adds 2 halflines automatically */
4d8a62ea
DV
7719 crtc_vtotal -= 1;
7720 crtc_vblank_end -= 1;
609aeaca 7721
409ee761 7722 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
609aeaca
VS
7723 vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
7724 else
7725 vsyncshift = adjusted_mode->crtc_hsync_start -
7726 adjusted_mode->crtc_htotal / 2;
1caea6e9
VS
7727 if (vsyncshift < 0)
7728 vsyncshift += adjusted_mode->crtc_htotal;
b0e77b9c
PZ
7729 }
7730
7731 if (INTEL_INFO(dev)->gen > 3)
fe2b8f9d 7732 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
b0e77b9c 7733
fe2b8f9d 7734 I915_WRITE(HTOTAL(cpu_transcoder),
b0e77b9c
PZ
7735 (adjusted_mode->crtc_hdisplay - 1) |
7736 ((adjusted_mode->crtc_htotal - 1) << 16));
fe2b8f9d 7737 I915_WRITE(HBLANK(cpu_transcoder),
b0e77b9c
PZ
7738 (adjusted_mode->crtc_hblank_start - 1) |
7739 ((adjusted_mode->crtc_hblank_end - 1) << 16));
fe2b8f9d 7740 I915_WRITE(HSYNC(cpu_transcoder),
b0e77b9c
PZ
7741 (adjusted_mode->crtc_hsync_start - 1) |
7742 ((adjusted_mode->crtc_hsync_end - 1) << 16));
7743
fe2b8f9d 7744 I915_WRITE(VTOTAL(cpu_transcoder),
b0e77b9c 7745 (adjusted_mode->crtc_vdisplay - 1) |
4d8a62ea 7746 ((crtc_vtotal - 1) << 16));
fe2b8f9d 7747 I915_WRITE(VBLANK(cpu_transcoder),
b0e77b9c 7748 (adjusted_mode->crtc_vblank_start - 1) |
4d8a62ea 7749 ((crtc_vblank_end - 1) << 16));
fe2b8f9d 7750 I915_WRITE(VSYNC(cpu_transcoder),
b0e77b9c
PZ
7751 (adjusted_mode->crtc_vsync_start - 1) |
7752 ((adjusted_mode->crtc_vsync_end - 1) << 16));
7753
b5e508d4
PZ
7754 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
7755 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
7756 * documented on the DDI_FUNC_CTL register description, EDP Input Select
7757 * bits. */
7758 if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
7759 (pipe == PIPE_B || pipe == PIPE_C))
7760 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
7761
bc58be60
JN
7762}
7763
7764static void intel_set_pipe_src_size(struct intel_crtc *intel_crtc)
7765{
7766 struct drm_device *dev = intel_crtc->base.dev;
7767 struct drm_i915_private *dev_priv = dev->dev_private;
7768 enum pipe pipe = intel_crtc->pipe;
7769
b0e77b9c
PZ
7770 /* pipesrc controls the size that is scaled from, which should
7771 * always be the user's requested size.
7772 */
7773 I915_WRITE(PIPESRC(pipe),
6e3c9717
ACO
7774 ((intel_crtc->config->pipe_src_w - 1) << 16) |
7775 (intel_crtc->config->pipe_src_h - 1));
b0e77b9c
PZ
7776}
7777
1bd1bd80 7778static void intel_get_pipe_timings(struct intel_crtc *crtc,
5cec258b 7779 struct intel_crtc_state *pipe_config)
1bd1bd80
DV
7780{
7781 struct drm_device *dev = crtc->base.dev;
7782 struct drm_i915_private *dev_priv = dev->dev_private;
7783 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
7784 uint32_t tmp;
7785
7786 tmp = I915_READ(HTOTAL(cpu_transcoder));
2d112de7
ACO
7787 pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
7788 pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 7789 tmp = I915_READ(HBLANK(cpu_transcoder));
2d112de7
ACO
7790 pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
7791 pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 7792 tmp = I915_READ(HSYNC(cpu_transcoder));
2d112de7
ACO
7793 pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
7794 pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80
DV
7795
7796 tmp = I915_READ(VTOTAL(cpu_transcoder));
2d112de7
ACO
7797 pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
7798 pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 7799 tmp = I915_READ(VBLANK(cpu_transcoder));
2d112de7
ACO
7800 pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
7801 pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 7802 tmp = I915_READ(VSYNC(cpu_transcoder));
2d112de7
ACO
7803 pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
7804 pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80
DV
7805
7806 if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
2d112de7
ACO
7807 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
7808 pipe_config->base.adjusted_mode.crtc_vtotal += 1;
7809 pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
1bd1bd80 7810 }
bc58be60
JN
7811}
7812
7813static void intel_get_pipe_src_size(struct intel_crtc *crtc,
7814 struct intel_crtc_state *pipe_config)
7815{
7816 struct drm_device *dev = crtc->base.dev;
7817 struct drm_i915_private *dev_priv = dev->dev_private;
7818 u32 tmp;
1bd1bd80
DV
7819
7820 tmp = I915_READ(PIPESRC(crtc->pipe));
37327abd
VS
7821 pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
7822 pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
7823
2d112de7
ACO
7824 pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
7825 pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
1bd1bd80
DV
7826}
7827
f6a83288 7828void intel_mode_from_pipe_config(struct drm_display_mode *mode,
5cec258b 7829 struct intel_crtc_state *pipe_config)
babea61d 7830{
2d112de7
ACO
7831 mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
7832 mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
7833 mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
7834 mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
babea61d 7835
2d112de7
ACO
7836 mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
7837 mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
7838 mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
7839 mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
babea61d 7840
2d112de7 7841 mode->flags = pipe_config->base.adjusted_mode.flags;
cd13f5ab 7842 mode->type = DRM_MODE_TYPE_DRIVER;
babea61d 7843
2d112de7
ACO
7844 mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
7845 mode->flags |= pipe_config->base.adjusted_mode.flags;
cd13f5ab
ML
7846
7847 mode->hsync = drm_mode_hsync(mode);
7848 mode->vrefresh = drm_mode_vrefresh(mode);
7849 drm_mode_set_name(mode);
babea61d
JB
7850}
7851
84b046f3
DV
7852static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
7853{
7854 struct drm_device *dev = intel_crtc->base.dev;
7855 struct drm_i915_private *dev_priv = dev->dev_private;
7856 uint32_t pipeconf;
7857
9f11a9e4 7858 pipeconf = 0;
84b046f3 7859
b6b5d049
VS
7860 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
7861 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
7862 pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
67c72a12 7863
6e3c9717 7864 if (intel_crtc->config->double_wide)
cf532bb2 7865 pipeconf |= PIPECONF_DOUBLE_WIDE;
84b046f3 7866
ff9ce46e 7867 /* only g4x and later have fancy bpc/dither controls */
666a4537 7868 if (IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
ff9ce46e 7869 /* Bspec claims that we can't use dithering for 30bpp pipes. */
6e3c9717 7870 if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
ff9ce46e 7871 pipeconf |= PIPECONF_DITHER_EN |
84b046f3 7872 PIPECONF_DITHER_TYPE_SP;
84b046f3 7873
6e3c9717 7874 switch (intel_crtc->config->pipe_bpp) {
ff9ce46e
DV
7875 case 18:
7876 pipeconf |= PIPECONF_6BPC;
7877 break;
7878 case 24:
7879 pipeconf |= PIPECONF_8BPC;
7880 break;
7881 case 30:
7882 pipeconf |= PIPECONF_10BPC;
7883 break;
7884 default:
7885 /* Case prevented by intel_choose_pipe_bpp_dither. */
7886 BUG();
84b046f3
DV
7887 }
7888 }
7889
7890 if (HAS_PIPE_CXSR(dev)) {
7891 if (intel_crtc->lowfreq_avail) {
7892 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
7893 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
7894 } else {
7895 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
84b046f3
DV
7896 }
7897 }
7898
6e3c9717 7899 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
efc2cfff 7900 if (INTEL_INFO(dev)->gen < 4 ||
409ee761 7901 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
efc2cfff
VS
7902 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
7903 else
7904 pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
7905 } else
84b046f3
DV
7906 pipeconf |= PIPECONF_PROGRESSIVE;
7907
666a4537
WB
7908 if ((IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) &&
7909 intel_crtc->config->limited_color_range)
9f11a9e4 7910 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
9c8e09b7 7911
84b046f3
DV
7912 I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
7913 POSTING_READ(PIPECONF(intel_crtc->pipe));
7914}
7915
81c97f52
ACO
7916static int i8xx_crtc_compute_clock(struct intel_crtc *crtc,
7917 struct intel_crtc_state *crtc_state)
7918{
7919 struct drm_device *dev = crtc->base.dev;
7920 struct drm_i915_private *dev_priv = dev->dev_private;
1b6f4958 7921 const struct intel_limit *limit;
81c97f52
ACO
7922 int refclk = 48000;
7923
7924 memset(&crtc_state->dpll_hw_state, 0,
7925 sizeof(crtc_state->dpll_hw_state));
7926
7927 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
7928 if (intel_panel_use_ssc(dev_priv)) {
7929 refclk = dev_priv->vbt.lvds_ssc_freq;
7930 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
7931 }
7932
7933 limit = &intel_limits_i8xx_lvds;
7934 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO)) {
7935 limit = &intel_limits_i8xx_dvo;
7936 } else {
7937 limit = &intel_limits_i8xx_dac;
7938 }
7939
7940 if (!crtc_state->clock_set &&
7941 !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7942 refclk, NULL, &crtc_state->dpll)) {
7943 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7944 return -EINVAL;
7945 }
7946
7947 i8xx_compute_dpll(crtc, crtc_state, NULL);
7948
7949 return 0;
7950}
7951
19ec6693
ACO
7952static int g4x_crtc_compute_clock(struct intel_crtc *crtc,
7953 struct intel_crtc_state *crtc_state)
7954{
7955 struct drm_device *dev = crtc->base.dev;
7956 struct drm_i915_private *dev_priv = dev->dev_private;
1b6f4958 7957 const struct intel_limit *limit;
19ec6693
ACO
7958 int refclk = 96000;
7959
7960 memset(&crtc_state->dpll_hw_state, 0,
7961 sizeof(crtc_state->dpll_hw_state));
7962
7963 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
7964 if (intel_panel_use_ssc(dev_priv)) {
7965 refclk = dev_priv->vbt.lvds_ssc_freq;
7966 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
7967 }
7968
7969 if (intel_is_dual_link_lvds(dev))
7970 limit = &intel_limits_g4x_dual_channel_lvds;
7971 else
7972 limit = &intel_limits_g4x_single_channel_lvds;
7973 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI) ||
7974 intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
7975 limit = &intel_limits_g4x_hdmi;
7976 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO)) {
7977 limit = &intel_limits_g4x_sdvo;
7978 } else {
7979 /* The option is for other outputs */
7980 limit = &intel_limits_i9xx_sdvo;
7981 }
7982
7983 if (!crtc_state->clock_set &&
7984 !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7985 refclk, NULL, &crtc_state->dpll)) {
7986 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7987 return -EINVAL;
7988 }
7989
7990 i9xx_compute_dpll(crtc, crtc_state, NULL);
7991
7992 return 0;
7993}
7994
70e8aa21
ACO
7995static int pnv_crtc_compute_clock(struct intel_crtc *crtc,
7996 struct intel_crtc_state *crtc_state)
7997{
7998 struct drm_device *dev = crtc->base.dev;
7999 struct drm_i915_private *dev_priv = dev->dev_private;
1b6f4958 8000 const struct intel_limit *limit;
70e8aa21
ACO
8001 int refclk = 96000;
8002
8003 memset(&crtc_state->dpll_hw_state, 0,
8004 sizeof(crtc_state->dpll_hw_state));
8005
8006 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
8007 if (intel_panel_use_ssc(dev_priv)) {
8008 refclk = dev_priv->vbt.lvds_ssc_freq;
8009 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
8010 }
8011
8012 limit = &intel_limits_pineview_lvds;
8013 } else {
8014 limit = &intel_limits_pineview_sdvo;
8015 }
8016
8017 if (!crtc_state->clock_set &&
8018 !pnv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8019 refclk, NULL, &crtc_state->dpll)) {
8020 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8021 return -EINVAL;
8022 }
8023
8024 i9xx_compute_dpll(crtc, crtc_state, NULL);
8025
8026 return 0;
8027}
8028
190f68c5
ACO
8029static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
8030 struct intel_crtc_state *crtc_state)
79e53945 8031{
c7653199 8032 struct drm_device *dev = crtc->base.dev;
79e53945 8033 struct drm_i915_private *dev_priv = dev->dev_private;
1b6f4958 8034 const struct intel_limit *limit;
81c97f52 8035 int refclk = 96000;
79e53945 8036
dd3cd74a
ACO
8037 memset(&crtc_state->dpll_hw_state, 0,
8038 sizeof(crtc_state->dpll_hw_state));
8039
70e8aa21
ACO
8040 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
8041 if (intel_panel_use_ssc(dev_priv)) {
8042 refclk = dev_priv->vbt.lvds_ssc_freq;
8043 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
8044 }
43565a06 8045
70e8aa21
ACO
8046 limit = &intel_limits_i9xx_lvds;
8047 } else {
8048 limit = &intel_limits_i9xx_sdvo;
81c97f52 8049 }
79e53945 8050
70e8aa21
ACO
8051 if (!crtc_state->clock_set &&
8052 !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8053 refclk, NULL, &crtc_state->dpll)) {
8054 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8055 return -EINVAL;
f47709a9 8056 }
7026d4ac 8057
81c97f52 8058 i9xx_compute_dpll(crtc, crtc_state, NULL);
79e53945 8059
c8f7a0db 8060 return 0;
f564048e
EA
8061}
8062
65b3d6a9
ACO
8063static int chv_crtc_compute_clock(struct intel_crtc *crtc,
8064 struct intel_crtc_state *crtc_state)
8065{
8066 int refclk = 100000;
1b6f4958 8067 const struct intel_limit *limit = &intel_limits_chv;
65b3d6a9
ACO
8068
8069 memset(&crtc_state->dpll_hw_state, 0,
8070 sizeof(crtc_state->dpll_hw_state));
8071
65b3d6a9
ACO
8072 if (!crtc_state->clock_set &&
8073 !chv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8074 refclk, NULL, &crtc_state->dpll)) {
8075 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8076 return -EINVAL;
8077 }
8078
8079 chv_compute_dpll(crtc, crtc_state);
8080
8081 return 0;
8082}
8083
8084static int vlv_crtc_compute_clock(struct intel_crtc *crtc,
8085 struct intel_crtc_state *crtc_state)
8086{
8087 int refclk = 100000;
1b6f4958 8088 const struct intel_limit *limit = &intel_limits_vlv;
65b3d6a9
ACO
8089
8090 memset(&crtc_state->dpll_hw_state, 0,
8091 sizeof(crtc_state->dpll_hw_state));
8092
65b3d6a9
ACO
8093 if (!crtc_state->clock_set &&
8094 !vlv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8095 refclk, NULL, &crtc_state->dpll)) {
8096 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8097 return -EINVAL;
8098 }
8099
8100 vlv_compute_dpll(crtc, crtc_state);
8101
8102 return 0;
8103}
8104
2fa2fe9a 8105static void i9xx_get_pfit_config(struct intel_crtc *crtc,
5cec258b 8106 struct intel_crtc_state *pipe_config)
2fa2fe9a
DV
8107{
8108 struct drm_device *dev = crtc->base.dev;
8109 struct drm_i915_private *dev_priv = dev->dev_private;
8110 uint32_t tmp;
8111
dc9e7dec
VS
8112 if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
8113 return;
8114
2fa2fe9a 8115 tmp = I915_READ(PFIT_CONTROL);
06922821
DV
8116 if (!(tmp & PFIT_ENABLE))
8117 return;
2fa2fe9a 8118
06922821 8119 /* Check whether the pfit is attached to our pipe. */
2fa2fe9a
DV
8120 if (INTEL_INFO(dev)->gen < 4) {
8121 if (crtc->pipe != PIPE_B)
8122 return;
2fa2fe9a
DV
8123 } else {
8124 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
8125 return;
8126 }
8127
06922821 8128 pipe_config->gmch_pfit.control = tmp;
2fa2fe9a 8129 pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
2fa2fe9a
DV
8130}
8131
acbec814 8132static void vlv_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 8133 struct intel_crtc_state *pipe_config)
acbec814
JB
8134{
8135 struct drm_device *dev = crtc->base.dev;
8136 struct drm_i915_private *dev_priv = dev->dev_private;
8137 int pipe = pipe_config->cpu_transcoder;
9e2c8475 8138 struct dpll clock;
acbec814 8139 u32 mdiv;
662c6ecb 8140 int refclk = 100000;
acbec814 8141
b521973b
VS
8142 /* In case of DSI, DPLL will not be used */
8143 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
f573de5a
SK
8144 return;
8145
a580516d 8146 mutex_lock(&dev_priv->sb_lock);
ab3c759a 8147 mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
a580516d 8148 mutex_unlock(&dev_priv->sb_lock);
acbec814
JB
8149
8150 clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
8151 clock.m2 = mdiv & DPIO_M2DIV_MASK;
8152 clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
8153 clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
8154 clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
8155
dccbea3b 8156 pipe_config->port_clock = vlv_calc_dpll_params(refclk, &clock);
acbec814
JB
8157}
8158
5724dbd1
DL
8159static void
8160i9xx_get_initial_plane_config(struct intel_crtc *crtc,
8161 struct intel_initial_plane_config *plane_config)
1ad292b5
JB
8162{
8163 struct drm_device *dev = crtc->base.dev;
8164 struct drm_i915_private *dev_priv = dev->dev_private;
8165 u32 val, base, offset;
8166 int pipe = crtc->pipe, plane = crtc->plane;
8167 int fourcc, pixel_format;
6761dd31 8168 unsigned int aligned_height;
b113d5ee 8169 struct drm_framebuffer *fb;
1b842c89 8170 struct intel_framebuffer *intel_fb;
1ad292b5 8171
42a7b088
DL
8172 val = I915_READ(DSPCNTR(plane));
8173 if (!(val & DISPLAY_PLANE_ENABLE))
8174 return;
8175
d9806c9f 8176 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
1b842c89 8177 if (!intel_fb) {
1ad292b5
JB
8178 DRM_DEBUG_KMS("failed to alloc fb\n");
8179 return;
8180 }
8181
1b842c89
DL
8182 fb = &intel_fb->base;
8183
18c5247e
DV
8184 if (INTEL_INFO(dev)->gen >= 4) {
8185 if (val & DISPPLANE_TILED) {
49af449b 8186 plane_config->tiling = I915_TILING_X;
18c5247e
DV
8187 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
8188 }
8189 }
1ad292b5
JB
8190
8191 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
b35d63fa 8192 fourcc = i9xx_format_to_fourcc(pixel_format);
b113d5ee
DL
8193 fb->pixel_format = fourcc;
8194 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
1ad292b5
JB
8195
8196 if (INTEL_INFO(dev)->gen >= 4) {
49af449b 8197 if (plane_config->tiling)
1ad292b5
JB
8198 offset = I915_READ(DSPTILEOFF(plane));
8199 else
8200 offset = I915_READ(DSPLINOFF(plane));
8201 base = I915_READ(DSPSURF(plane)) & 0xfffff000;
8202 } else {
8203 base = I915_READ(DSPADDR(plane));
8204 }
8205 plane_config->base = base;
8206
8207 val = I915_READ(PIPESRC(pipe));
b113d5ee
DL
8208 fb->width = ((val >> 16) & 0xfff) + 1;
8209 fb->height = ((val >> 0) & 0xfff) + 1;
1ad292b5
JB
8210
8211 val = I915_READ(DSPSTRIDE(pipe));
b113d5ee 8212 fb->pitches[0] = val & 0xffffffc0;
1ad292b5 8213
b113d5ee 8214 aligned_height = intel_fb_align_height(dev, fb->height,
091df6cb
DV
8215 fb->pixel_format,
8216 fb->modifier[0]);
1ad292b5 8217
f37b5c2b 8218 plane_config->size = fb->pitches[0] * aligned_height;
1ad292b5 8219
2844a921
DL
8220 DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
8221 pipe_name(pipe), plane, fb->width, fb->height,
8222 fb->bits_per_pixel, base, fb->pitches[0],
8223 plane_config->size);
1ad292b5 8224
2d14030b 8225 plane_config->fb = intel_fb;
1ad292b5
JB
8226}
8227
70b23a98 8228static void chv_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 8229 struct intel_crtc_state *pipe_config)
70b23a98
VS
8230{
8231 struct drm_device *dev = crtc->base.dev;
8232 struct drm_i915_private *dev_priv = dev->dev_private;
8233 int pipe = pipe_config->cpu_transcoder;
8234 enum dpio_channel port = vlv_pipe_to_channel(pipe);
9e2c8475 8235 struct dpll clock;
0d7b6b11 8236 u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2, pll_dw3;
70b23a98
VS
8237 int refclk = 100000;
8238
b521973b
VS
8239 /* In case of DSI, DPLL will not be used */
8240 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
8241 return;
8242
a580516d 8243 mutex_lock(&dev_priv->sb_lock);
70b23a98
VS
8244 cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
8245 pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
8246 pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
8247 pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
0d7b6b11 8248 pll_dw3 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
a580516d 8249 mutex_unlock(&dev_priv->sb_lock);
70b23a98
VS
8250
8251 clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
0d7b6b11
ID
8252 clock.m2 = (pll_dw0 & 0xff) << 22;
8253 if (pll_dw3 & DPIO_CHV_FRAC_DIV_EN)
8254 clock.m2 |= pll_dw2 & 0x3fffff;
70b23a98
VS
8255 clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
8256 clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
8257 clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
8258
dccbea3b 8259 pipe_config->port_clock = chv_calc_dpll_params(refclk, &clock);
70b23a98
VS
8260}
8261
0e8ffe1b 8262static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
5cec258b 8263 struct intel_crtc_state *pipe_config)
0e8ffe1b
DV
8264{
8265 struct drm_device *dev = crtc->base.dev;
8266 struct drm_i915_private *dev_priv = dev->dev_private;
1729050e 8267 enum intel_display_power_domain power_domain;
0e8ffe1b 8268 uint32_t tmp;
1729050e 8269 bool ret;
0e8ffe1b 8270
1729050e
ID
8271 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
8272 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
b5482bd0
ID
8273 return false;
8274
e143a21c 8275 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
8106ddbd 8276 pipe_config->shared_dpll = NULL;
eccb140b 8277
1729050e
ID
8278 ret = false;
8279
0e8ffe1b
DV
8280 tmp = I915_READ(PIPECONF(crtc->pipe));
8281 if (!(tmp & PIPECONF_ENABLE))
1729050e 8282 goto out;
0e8ffe1b 8283
666a4537 8284 if (IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
42571aef
VS
8285 switch (tmp & PIPECONF_BPC_MASK) {
8286 case PIPECONF_6BPC:
8287 pipe_config->pipe_bpp = 18;
8288 break;
8289 case PIPECONF_8BPC:
8290 pipe_config->pipe_bpp = 24;
8291 break;
8292 case PIPECONF_10BPC:
8293 pipe_config->pipe_bpp = 30;
8294 break;
8295 default:
8296 break;
8297 }
8298 }
8299
666a4537
WB
8300 if ((IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) &&
8301 (tmp & PIPECONF_COLOR_RANGE_SELECT))
b5a9fa09
DV
8302 pipe_config->limited_color_range = true;
8303
282740f7
VS
8304 if (INTEL_INFO(dev)->gen < 4)
8305 pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
8306
1bd1bd80 8307 intel_get_pipe_timings(crtc, pipe_config);
bc58be60 8308 intel_get_pipe_src_size(crtc, pipe_config);
1bd1bd80 8309
2fa2fe9a
DV
8310 i9xx_get_pfit_config(crtc, pipe_config);
8311
6c49f241 8312 if (INTEL_INFO(dev)->gen >= 4) {
c231775c
VS
8313 /* No way to read it out on pipes B and C */
8314 if (IS_CHERRYVIEW(dev) && crtc->pipe != PIPE_A)
8315 tmp = dev_priv->chv_dpll_md[crtc->pipe];
8316 else
8317 tmp = I915_READ(DPLL_MD(crtc->pipe));
6c49f241
DV
8318 pipe_config->pixel_multiplier =
8319 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
8320 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
8bcc2795 8321 pipe_config->dpll_hw_state.dpll_md = tmp;
6c49f241
DV
8322 } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
8323 tmp = I915_READ(DPLL(crtc->pipe));
8324 pipe_config->pixel_multiplier =
8325 ((tmp & SDVO_MULTIPLIER_MASK)
8326 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
8327 } else {
8328 /* Note that on i915G/GM the pixel multiplier is in the sdvo
8329 * port and will be fixed up in the encoder->get_config
8330 * function. */
8331 pipe_config->pixel_multiplier = 1;
8332 }
8bcc2795 8333 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
666a4537 8334 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
1c4e0274
VS
8335 /*
8336 * DPLL_DVO_2X_MODE must be enabled for both DPLLs
8337 * on 830. Filter it out here so that we don't
8338 * report errors due to that.
8339 */
8340 if (IS_I830(dev))
8341 pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
8342
8bcc2795
DV
8343 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
8344 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
165e901c
VS
8345 } else {
8346 /* Mask out read-only status bits. */
8347 pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
8348 DPLL_PORTC_READY_MASK |
8349 DPLL_PORTB_READY_MASK);
8bcc2795 8350 }
6c49f241 8351
70b23a98
VS
8352 if (IS_CHERRYVIEW(dev))
8353 chv_crtc_clock_get(crtc, pipe_config);
8354 else if (IS_VALLEYVIEW(dev))
acbec814
JB
8355 vlv_crtc_clock_get(crtc, pipe_config);
8356 else
8357 i9xx_crtc_clock_get(crtc, pipe_config);
18442d08 8358
0f64614d
VS
8359 /*
8360 * Normally the dotclock is filled in by the encoder .get_config()
8361 * but in case the pipe is enabled w/o any ports we need a sane
8362 * default.
8363 */
8364 pipe_config->base.adjusted_mode.crtc_clock =
8365 pipe_config->port_clock / pipe_config->pixel_multiplier;
8366
1729050e
ID
8367 ret = true;
8368
8369out:
8370 intel_display_power_put(dev_priv, power_domain);
8371
8372 return ret;
0e8ffe1b
DV
8373}
8374
dde86e2d 8375static void ironlake_init_pch_refclk(struct drm_device *dev)
13d83a67
JB
8376{
8377 struct drm_i915_private *dev_priv = dev->dev_private;
13d83a67 8378 struct intel_encoder *encoder;
1c1a24d2 8379 int i;
74cfd7ac 8380 u32 val, final;
13d83a67 8381 bool has_lvds = false;
199e5d79 8382 bool has_cpu_edp = false;
199e5d79 8383 bool has_panel = false;
99eb6a01
KP
8384 bool has_ck505 = false;
8385 bool can_ssc = false;
1c1a24d2 8386 bool using_ssc_source = false;
13d83a67
JB
8387
8388 /* We need to take the global config into account */
b2784e15 8389 for_each_intel_encoder(dev, encoder) {
199e5d79
KP
8390 switch (encoder->type) {
8391 case INTEL_OUTPUT_LVDS:
8392 has_panel = true;
8393 has_lvds = true;
8394 break;
8395 case INTEL_OUTPUT_EDP:
8396 has_panel = true;
2de6905f 8397 if (enc_to_dig_port(&encoder->base)->port == PORT_A)
199e5d79
KP
8398 has_cpu_edp = true;
8399 break;
6847d71b
PZ
8400 default:
8401 break;
13d83a67
JB
8402 }
8403 }
8404
99eb6a01 8405 if (HAS_PCH_IBX(dev)) {
41aa3448 8406 has_ck505 = dev_priv->vbt.display_clock_mode;
99eb6a01
KP
8407 can_ssc = has_ck505;
8408 } else {
8409 has_ck505 = false;
8410 can_ssc = true;
8411 }
8412
1c1a24d2
L
8413 /* Check if any DPLLs are using the SSC source */
8414 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
8415 u32 temp = I915_READ(PCH_DPLL(i));
8416
8417 if (!(temp & DPLL_VCO_ENABLE))
8418 continue;
8419
8420 if ((temp & PLL_REF_INPUT_MASK) ==
8421 PLLB_REF_INPUT_SPREADSPECTRUMIN) {
8422 using_ssc_source = true;
8423 break;
8424 }
8425 }
8426
8427 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d using_ssc_source %d\n",
8428 has_panel, has_lvds, has_ck505, using_ssc_source);
13d83a67
JB
8429
8430 /* Ironlake: try to setup display ref clock before DPLL
8431 * enabling. This is only under driver's control after
8432 * PCH B stepping, previous chipset stepping should be
8433 * ignoring this setting.
8434 */
74cfd7ac
CW
8435 val = I915_READ(PCH_DREF_CONTROL);
8436
8437 /* As we must carefully and slowly disable/enable each source in turn,
8438 * compute the final state we want first and check if we need to
8439 * make any changes at all.
8440 */
8441 final = val;
8442 final &= ~DREF_NONSPREAD_SOURCE_MASK;
8443 if (has_ck505)
8444 final |= DREF_NONSPREAD_CK505_ENABLE;
8445 else
8446 final |= DREF_NONSPREAD_SOURCE_ENABLE;
8447
8c07eb68 8448 final &= ~DREF_SSC_SOURCE_MASK;
74cfd7ac 8449 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
8c07eb68 8450 final &= ~DREF_SSC1_ENABLE;
74cfd7ac
CW
8451
8452 if (has_panel) {
8453 final |= DREF_SSC_SOURCE_ENABLE;
8454
8455 if (intel_panel_use_ssc(dev_priv) && can_ssc)
8456 final |= DREF_SSC1_ENABLE;
8457
8458 if (has_cpu_edp) {
8459 if (intel_panel_use_ssc(dev_priv) && can_ssc)
8460 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
8461 else
8462 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
8463 } else
8464 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
1c1a24d2
L
8465 } else if (using_ssc_source) {
8466 final |= DREF_SSC_SOURCE_ENABLE;
8467 final |= DREF_SSC1_ENABLE;
74cfd7ac
CW
8468 }
8469
8470 if (final == val)
8471 return;
8472
13d83a67 8473 /* Always enable nonspread source */
74cfd7ac 8474 val &= ~DREF_NONSPREAD_SOURCE_MASK;
13d83a67 8475
99eb6a01 8476 if (has_ck505)
74cfd7ac 8477 val |= DREF_NONSPREAD_CK505_ENABLE;
99eb6a01 8478 else
74cfd7ac 8479 val |= DREF_NONSPREAD_SOURCE_ENABLE;
13d83a67 8480
199e5d79 8481 if (has_panel) {
74cfd7ac
CW
8482 val &= ~DREF_SSC_SOURCE_MASK;
8483 val |= DREF_SSC_SOURCE_ENABLE;
13d83a67 8484
199e5d79 8485 /* SSC must be turned on before enabling the CPU output */
99eb6a01 8486 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
199e5d79 8487 DRM_DEBUG_KMS("Using SSC on panel\n");
74cfd7ac 8488 val |= DREF_SSC1_ENABLE;
e77166b5 8489 } else
74cfd7ac 8490 val &= ~DREF_SSC1_ENABLE;
199e5d79
KP
8491
8492 /* Get SSC going before enabling the outputs */
74cfd7ac 8493 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
8494 POSTING_READ(PCH_DREF_CONTROL);
8495 udelay(200);
8496
74cfd7ac 8497 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
13d83a67
JB
8498
8499 /* Enable CPU source on CPU attached eDP */
199e5d79 8500 if (has_cpu_edp) {
99eb6a01 8501 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
199e5d79 8502 DRM_DEBUG_KMS("Using SSC on eDP\n");
74cfd7ac 8503 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
eba905b2 8504 } else
74cfd7ac 8505 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
199e5d79 8506 } else
74cfd7ac 8507 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
199e5d79 8508
74cfd7ac 8509 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
8510 POSTING_READ(PCH_DREF_CONTROL);
8511 udelay(200);
8512 } else {
1c1a24d2 8513 DRM_DEBUG_KMS("Disabling CPU source output\n");
199e5d79 8514
74cfd7ac 8515 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
199e5d79
KP
8516
8517 /* Turn off CPU output */
74cfd7ac 8518 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
199e5d79 8519
74cfd7ac 8520 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
8521 POSTING_READ(PCH_DREF_CONTROL);
8522 udelay(200);
8523
1c1a24d2
L
8524 if (!using_ssc_source) {
8525 DRM_DEBUG_KMS("Disabling SSC source\n");
199e5d79 8526
1c1a24d2
L
8527 /* Turn off the SSC source */
8528 val &= ~DREF_SSC_SOURCE_MASK;
8529 val |= DREF_SSC_SOURCE_DISABLE;
f165d283 8530
1c1a24d2
L
8531 /* Turn off SSC1 */
8532 val &= ~DREF_SSC1_ENABLE;
8533
8534 I915_WRITE(PCH_DREF_CONTROL, val);
8535 POSTING_READ(PCH_DREF_CONTROL);
8536 udelay(200);
8537 }
13d83a67 8538 }
74cfd7ac
CW
8539
8540 BUG_ON(val != final);
13d83a67
JB
8541}
8542
f31f2d55 8543static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
dde86e2d 8544{
f31f2d55 8545 uint32_t tmp;
dde86e2d 8546
0ff066a9
PZ
8547 tmp = I915_READ(SOUTH_CHICKEN2);
8548 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
8549 I915_WRITE(SOUTH_CHICKEN2, tmp);
dde86e2d 8550
cf3598c2
ID
8551 if (wait_for_us(I915_READ(SOUTH_CHICKEN2) &
8552 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
0ff066a9 8553 DRM_ERROR("FDI mPHY reset assert timeout\n");
dde86e2d 8554
0ff066a9
PZ
8555 tmp = I915_READ(SOUTH_CHICKEN2);
8556 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
8557 I915_WRITE(SOUTH_CHICKEN2, tmp);
dde86e2d 8558
cf3598c2
ID
8559 if (wait_for_us((I915_READ(SOUTH_CHICKEN2) &
8560 FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
0ff066a9 8561 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
f31f2d55
PZ
8562}
8563
8564/* WaMPhyProgramming:hsw */
8565static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
8566{
8567 uint32_t tmp;
dde86e2d
PZ
8568
8569 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
8570 tmp &= ~(0xFF << 24);
8571 tmp |= (0x12 << 24);
8572 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
8573
dde86e2d
PZ
8574 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
8575 tmp |= (1 << 11);
8576 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
8577
8578 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
8579 tmp |= (1 << 11);
8580 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
8581
dde86e2d
PZ
8582 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
8583 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
8584 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
8585
8586 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
8587 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
8588 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
8589
0ff066a9
PZ
8590 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
8591 tmp &= ~(7 << 13);
8592 tmp |= (5 << 13);
8593 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
dde86e2d 8594
0ff066a9
PZ
8595 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
8596 tmp &= ~(7 << 13);
8597 tmp |= (5 << 13);
8598 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
dde86e2d
PZ
8599
8600 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
8601 tmp &= ~0xFF;
8602 tmp |= 0x1C;
8603 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
8604
8605 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
8606 tmp &= ~0xFF;
8607 tmp |= 0x1C;
8608 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
8609
8610 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
8611 tmp &= ~(0xFF << 16);
8612 tmp |= (0x1C << 16);
8613 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
8614
8615 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
8616 tmp &= ~(0xFF << 16);
8617 tmp |= (0x1C << 16);
8618 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
8619
0ff066a9
PZ
8620 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
8621 tmp |= (1 << 27);
8622 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
dde86e2d 8623
0ff066a9
PZ
8624 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
8625 tmp |= (1 << 27);
8626 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
dde86e2d 8627
0ff066a9
PZ
8628 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
8629 tmp &= ~(0xF << 28);
8630 tmp |= (4 << 28);
8631 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
dde86e2d 8632
0ff066a9
PZ
8633 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
8634 tmp &= ~(0xF << 28);
8635 tmp |= (4 << 28);
8636 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
f31f2d55
PZ
8637}
8638
2fa86a1f
PZ
8639/* Implements 3 different sequences from BSpec chapter "Display iCLK
8640 * Programming" based on the parameters passed:
8641 * - Sequence to enable CLKOUT_DP
8642 * - Sequence to enable CLKOUT_DP without spread
8643 * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
8644 */
8645static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
8646 bool with_fdi)
f31f2d55
PZ
8647{
8648 struct drm_i915_private *dev_priv = dev->dev_private;
2fa86a1f
PZ
8649 uint32_t reg, tmp;
8650
8651 if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
8652 with_spread = true;
c2699524 8653 if (WARN(HAS_PCH_LPT_LP(dev) && with_fdi, "LP PCH doesn't have FDI\n"))
2fa86a1f 8654 with_fdi = false;
f31f2d55 8655
a580516d 8656 mutex_lock(&dev_priv->sb_lock);
f31f2d55
PZ
8657
8658 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8659 tmp &= ~SBI_SSCCTL_DISABLE;
8660 tmp |= SBI_SSCCTL_PATHALT;
8661 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8662
8663 udelay(24);
8664
2fa86a1f
PZ
8665 if (with_spread) {
8666 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8667 tmp &= ~SBI_SSCCTL_PATHALT;
8668 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
f31f2d55 8669
2fa86a1f
PZ
8670 if (with_fdi) {
8671 lpt_reset_fdi_mphy(dev_priv);
8672 lpt_program_fdi_mphy(dev_priv);
8673 }
8674 }
dde86e2d 8675
c2699524 8676 reg = HAS_PCH_LPT_LP(dev) ? SBI_GEN0 : SBI_DBUFF0;
2fa86a1f
PZ
8677 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8678 tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8679 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
c00db246 8680
a580516d 8681 mutex_unlock(&dev_priv->sb_lock);
dde86e2d
PZ
8682}
8683
47701c3b
PZ
8684/* Sequence to disable CLKOUT_DP */
8685static void lpt_disable_clkout_dp(struct drm_device *dev)
8686{
8687 struct drm_i915_private *dev_priv = dev->dev_private;
8688 uint32_t reg, tmp;
8689
a580516d 8690 mutex_lock(&dev_priv->sb_lock);
47701c3b 8691
c2699524 8692 reg = HAS_PCH_LPT_LP(dev) ? SBI_GEN0 : SBI_DBUFF0;
47701c3b
PZ
8693 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8694 tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8695 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
8696
8697 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8698 if (!(tmp & SBI_SSCCTL_DISABLE)) {
8699 if (!(tmp & SBI_SSCCTL_PATHALT)) {
8700 tmp |= SBI_SSCCTL_PATHALT;
8701 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8702 udelay(32);
8703 }
8704 tmp |= SBI_SSCCTL_DISABLE;
8705 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8706 }
8707
a580516d 8708 mutex_unlock(&dev_priv->sb_lock);
47701c3b
PZ
8709}
8710
f7be2c21
VS
8711#define BEND_IDX(steps) ((50 + (steps)) / 5)
8712
8713static const uint16_t sscdivintphase[] = {
8714 [BEND_IDX( 50)] = 0x3B23,
8715 [BEND_IDX( 45)] = 0x3B23,
8716 [BEND_IDX( 40)] = 0x3C23,
8717 [BEND_IDX( 35)] = 0x3C23,
8718 [BEND_IDX( 30)] = 0x3D23,
8719 [BEND_IDX( 25)] = 0x3D23,
8720 [BEND_IDX( 20)] = 0x3E23,
8721 [BEND_IDX( 15)] = 0x3E23,
8722 [BEND_IDX( 10)] = 0x3F23,
8723 [BEND_IDX( 5)] = 0x3F23,
8724 [BEND_IDX( 0)] = 0x0025,
8725 [BEND_IDX( -5)] = 0x0025,
8726 [BEND_IDX(-10)] = 0x0125,
8727 [BEND_IDX(-15)] = 0x0125,
8728 [BEND_IDX(-20)] = 0x0225,
8729 [BEND_IDX(-25)] = 0x0225,
8730 [BEND_IDX(-30)] = 0x0325,
8731 [BEND_IDX(-35)] = 0x0325,
8732 [BEND_IDX(-40)] = 0x0425,
8733 [BEND_IDX(-45)] = 0x0425,
8734 [BEND_IDX(-50)] = 0x0525,
8735};
8736
8737/*
8738 * Bend CLKOUT_DP
8739 * steps -50 to 50 inclusive, in steps of 5
8740 * < 0 slow down the clock, > 0 speed up the clock, 0 == no bend (135MHz)
8741 * change in clock period = -(steps / 10) * 5.787 ps
8742 */
8743static void lpt_bend_clkout_dp(struct drm_i915_private *dev_priv, int steps)
8744{
8745 uint32_t tmp;
8746 int idx = BEND_IDX(steps);
8747
8748 if (WARN_ON(steps % 5 != 0))
8749 return;
8750
8751 if (WARN_ON(idx >= ARRAY_SIZE(sscdivintphase)))
8752 return;
8753
8754 mutex_lock(&dev_priv->sb_lock);
8755
8756 if (steps % 10 != 0)
8757 tmp = 0xAAAAAAAB;
8758 else
8759 tmp = 0x00000000;
8760 intel_sbi_write(dev_priv, SBI_SSCDITHPHASE, tmp, SBI_ICLK);
8761
8762 tmp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE, SBI_ICLK);
8763 tmp &= 0xffff0000;
8764 tmp |= sscdivintphase[idx];
8765 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE, tmp, SBI_ICLK);
8766
8767 mutex_unlock(&dev_priv->sb_lock);
8768}
8769
8770#undef BEND_IDX
8771
bf8fa3d3
PZ
8772static void lpt_init_pch_refclk(struct drm_device *dev)
8773{
bf8fa3d3
PZ
8774 struct intel_encoder *encoder;
8775 bool has_vga = false;
8776
b2784e15 8777 for_each_intel_encoder(dev, encoder) {
bf8fa3d3
PZ
8778 switch (encoder->type) {
8779 case INTEL_OUTPUT_ANALOG:
8780 has_vga = true;
8781 break;
6847d71b
PZ
8782 default:
8783 break;
bf8fa3d3
PZ
8784 }
8785 }
8786
f7be2c21
VS
8787 if (has_vga) {
8788 lpt_bend_clkout_dp(to_i915(dev), 0);
47701c3b 8789 lpt_enable_clkout_dp(dev, true, true);
f7be2c21 8790 } else {
47701c3b 8791 lpt_disable_clkout_dp(dev);
f7be2c21 8792 }
bf8fa3d3
PZ
8793}
8794
dde86e2d
PZ
8795/*
8796 * Initialize reference clocks when the driver loads
8797 */
8798void intel_init_pch_refclk(struct drm_device *dev)
8799{
8800 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
8801 ironlake_init_pch_refclk(dev);
8802 else if (HAS_PCH_LPT(dev))
8803 lpt_init_pch_refclk(dev);
8804}
8805
6ff93609 8806static void ironlake_set_pipeconf(struct drm_crtc *crtc)
79e53945 8807{
c8203565 8808 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
79e53945
JB
8809 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8810 int pipe = intel_crtc->pipe;
c8203565
PZ
8811 uint32_t val;
8812
78114071 8813 val = 0;
c8203565 8814
6e3c9717 8815 switch (intel_crtc->config->pipe_bpp) {
c8203565 8816 case 18:
dfd07d72 8817 val |= PIPECONF_6BPC;
c8203565
PZ
8818 break;
8819 case 24:
dfd07d72 8820 val |= PIPECONF_8BPC;
c8203565
PZ
8821 break;
8822 case 30:
dfd07d72 8823 val |= PIPECONF_10BPC;
c8203565
PZ
8824 break;
8825 case 36:
dfd07d72 8826 val |= PIPECONF_12BPC;
c8203565
PZ
8827 break;
8828 default:
cc769b62
PZ
8829 /* Case prevented by intel_choose_pipe_bpp_dither. */
8830 BUG();
c8203565
PZ
8831 }
8832
6e3c9717 8833 if (intel_crtc->config->dither)
c8203565
PZ
8834 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8835
6e3c9717 8836 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
c8203565
PZ
8837 val |= PIPECONF_INTERLACED_ILK;
8838 else
8839 val |= PIPECONF_PROGRESSIVE;
8840
6e3c9717 8841 if (intel_crtc->config->limited_color_range)
3685a8f3 8842 val |= PIPECONF_COLOR_RANGE_SELECT;
3685a8f3 8843
c8203565
PZ
8844 I915_WRITE(PIPECONF(pipe), val);
8845 POSTING_READ(PIPECONF(pipe));
8846}
8847
6ff93609 8848static void haswell_set_pipeconf(struct drm_crtc *crtc)
ee2b0b38 8849{
391bf048 8850 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
ee2b0b38 8851 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6e3c9717 8852 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
391bf048 8853 u32 val = 0;
ee2b0b38 8854
391bf048 8855 if (IS_HASWELL(dev_priv) && intel_crtc->config->dither)
ee2b0b38
PZ
8856 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8857
6e3c9717 8858 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
ee2b0b38
PZ
8859 val |= PIPECONF_INTERLACED_ILK;
8860 else
8861 val |= PIPECONF_PROGRESSIVE;
8862
702e7a56
PZ
8863 I915_WRITE(PIPECONF(cpu_transcoder), val);
8864 POSTING_READ(PIPECONF(cpu_transcoder));
391bf048
JN
8865}
8866
391bf048
JN
8867static void haswell_set_pipemisc(struct drm_crtc *crtc)
8868{
8869 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
8870 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
756f85cf 8871
391bf048
JN
8872 if (IS_BROADWELL(dev_priv) || INTEL_INFO(dev_priv)->gen >= 9) {
8873 u32 val = 0;
756f85cf 8874
6e3c9717 8875 switch (intel_crtc->config->pipe_bpp) {
756f85cf
PZ
8876 case 18:
8877 val |= PIPEMISC_DITHER_6_BPC;
8878 break;
8879 case 24:
8880 val |= PIPEMISC_DITHER_8_BPC;
8881 break;
8882 case 30:
8883 val |= PIPEMISC_DITHER_10_BPC;
8884 break;
8885 case 36:
8886 val |= PIPEMISC_DITHER_12_BPC;
8887 break;
8888 default:
8889 /* Case prevented by pipe_config_set_bpp. */
8890 BUG();
8891 }
8892
6e3c9717 8893 if (intel_crtc->config->dither)
756f85cf
PZ
8894 val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
8895
391bf048 8896 I915_WRITE(PIPEMISC(intel_crtc->pipe), val);
756f85cf 8897 }
ee2b0b38
PZ
8898}
8899
d4b1931c
PZ
8900int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
8901{
8902 /*
8903 * Account for spread spectrum to avoid
8904 * oversubscribing the link. Max center spread
8905 * is 2.5%; use 5% for safety's sake.
8906 */
8907 u32 bps = target_clock * bpp * 21 / 20;
619d4d04 8908 return DIV_ROUND_UP(bps, link_bw * 8);
d4b1931c
PZ
8909}
8910
7429e9d4 8911static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
6cf86a5e 8912{
7429e9d4 8913 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
f48d8f23
PZ
8914}
8915
b75ca6f6
ACO
8916static void ironlake_compute_dpll(struct intel_crtc *intel_crtc,
8917 struct intel_crtc_state *crtc_state,
9e2c8475 8918 struct dpll *reduced_clock)
79e53945 8919{
de13a2e3 8920 struct drm_crtc *crtc = &intel_crtc->base;
79e53945
JB
8921 struct drm_device *dev = crtc->dev;
8922 struct drm_i915_private *dev_priv = dev->dev_private;
55bb9992 8923 struct drm_atomic_state *state = crtc_state->base.state;
da3ced29 8924 struct drm_connector *connector;
55bb9992
ACO
8925 struct drm_connector_state *connector_state;
8926 struct intel_encoder *encoder;
b75ca6f6 8927 u32 dpll, fp, fp2;
ceb41007 8928 int factor, i;
09ede541 8929 bool is_lvds = false, is_sdvo = false;
79e53945 8930
da3ced29 8931 for_each_connector_in_state(state, connector, connector_state, i) {
55bb9992
ACO
8932 if (connector_state->crtc != crtc_state->base.crtc)
8933 continue;
8934
8935 encoder = to_intel_encoder(connector_state->best_encoder);
8936
8937 switch (encoder->type) {
79e53945
JB
8938 case INTEL_OUTPUT_LVDS:
8939 is_lvds = true;
8940 break;
8941 case INTEL_OUTPUT_SDVO:
7d57382e 8942 case INTEL_OUTPUT_HDMI:
79e53945 8943 is_sdvo = true;
79e53945 8944 break;
6847d71b
PZ
8945 default:
8946 break;
79e53945
JB
8947 }
8948 }
79e53945 8949
c1858123 8950 /* Enable autotuning of the PLL clock (if permissible) */
8febb297
EA
8951 factor = 21;
8952 if (is_lvds) {
8953 if ((intel_panel_use_ssc(dev_priv) &&
e91e941b 8954 dev_priv->vbt.lvds_ssc_freq == 100000) ||
f0b44056 8955 (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
8febb297 8956 factor = 25;
190f68c5 8957 } else if (crtc_state->sdvo_tv_clock)
8febb297 8958 factor = 20;
c1858123 8959
b75ca6f6
ACO
8960 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
8961
190f68c5 8962 if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
b75ca6f6
ACO
8963 fp |= FP_CB_TUNE;
8964
8965 if (reduced_clock) {
8966 fp2 = i9xx_dpll_compute_fp(reduced_clock);
2c07245f 8967
b75ca6f6
ACO
8968 if (reduced_clock->m < factor * reduced_clock->n)
8969 fp2 |= FP_CB_TUNE;
8970 } else {
8971 fp2 = fp;
8972 }
9a7c7890 8973
5eddb70b 8974 dpll = 0;
2c07245f 8975
a07d6787
EA
8976 if (is_lvds)
8977 dpll |= DPLLB_MODE_LVDS;
8978 else
8979 dpll |= DPLLB_MODE_DAC_SERIAL;
198a037f 8980
190f68c5 8981 dpll |= (crtc_state->pixel_multiplier - 1)
ef1b460d 8982 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
198a037f
DV
8983
8984 if (is_sdvo)
4a33e48d 8985 dpll |= DPLL_SDVO_HIGH_SPEED;
190f68c5 8986 if (crtc_state->has_dp_encoder)
4a33e48d 8987 dpll |= DPLL_SDVO_HIGH_SPEED;
79e53945 8988
a07d6787 8989 /* compute bitmask from p1 value */
190f68c5 8990 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
a07d6787 8991 /* also FPA1 */
190f68c5 8992 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
a07d6787 8993
190f68c5 8994 switch (crtc_state->dpll.p2) {
a07d6787
EA
8995 case 5:
8996 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
8997 break;
8998 case 7:
8999 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
9000 break;
9001 case 10:
9002 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
9003 break;
9004 case 14:
9005 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
9006 break;
79e53945
JB
9007 }
9008
ceb41007 9009 if (is_lvds && intel_panel_use_ssc(dev_priv))
43565a06 9010 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
79e53945
JB
9011 else
9012 dpll |= PLL_REF_INPUT_DREFCLK;
9013
b75ca6f6
ACO
9014 dpll |= DPLL_VCO_ENABLE;
9015
9016 crtc_state->dpll_hw_state.dpll = dpll;
9017 crtc_state->dpll_hw_state.fp0 = fp;
9018 crtc_state->dpll_hw_state.fp1 = fp2;
de13a2e3
PZ
9019}
9020
190f68c5
ACO
9021static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
9022 struct intel_crtc_state *crtc_state)
de13a2e3 9023{
997c030c
ACO
9024 struct drm_device *dev = crtc->base.dev;
9025 struct drm_i915_private *dev_priv = dev->dev_private;
9e2c8475 9026 struct dpll reduced_clock;
7ed9f894 9027 bool has_reduced_clock = false;
e2b78267 9028 struct intel_shared_dpll *pll;
1b6f4958 9029 const struct intel_limit *limit;
997c030c 9030 int refclk = 120000;
de13a2e3 9031
dd3cd74a
ACO
9032 memset(&crtc_state->dpll_hw_state, 0,
9033 sizeof(crtc_state->dpll_hw_state));
9034
ded220e2
ACO
9035 crtc->lowfreq_avail = false;
9036
9037 /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
9038 if (!crtc_state->has_pch_encoder)
9039 return 0;
79e53945 9040
997c030c
ACO
9041 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
9042 if (intel_panel_use_ssc(dev_priv)) {
9043 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
9044 dev_priv->vbt.lvds_ssc_freq);
9045 refclk = dev_priv->vbt.lvds_ssc_freq;
9046 }
9047
9048 if (intel_is_dual_link_lvds(dev)) {
9049 if (refclk == 100000)
9050 limit = &intel_limits_ironlake_dual_lvds_100m;
9051 else
9052 limit = &intel_limits_ironlake_dual_lvds;
9053 } else {
9054 if (refclk == 100000)
9055 limit = &intel_limits_ironlake_single_lvds_100m;
9056 else
9057 limit = &intel_limits_ironlake_single_lvds;
9058 }
9059 } else {
9060 limit = &intel_limits_ironlake_dac;
9061 }
9062
364ee29d 9063 if (!crtc_state->clock_set &&
997c030c
ACO
9064 !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
9065 refclk, NULL, &crtc_state->dpll)) {
364ee29d
ACO
9066 DRM_ERROR("Couldn't find PLL settings for mode!\n");
9067 return -EINVAL;
f47709a9 9068 }
79e53945 9069
b75ca6f6
ACO
9070 ironlake_compute_dpll(crtc, crtc_state,
9071 has_reduced_clock ? &reduced_clock : NULL);
66e985c0 9072
ded220e2
ACO
9073 pll = intel_get_shared_dpll(crtc, crtc_state, NULL);
9074 if (pll == NULL) {
9075 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
9076 pipe_name(crtc->pipe));
9077 return -EINVAL;
3fb37703 9078 }
79e53945 9079
ded220e2
ACO
9080 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
9081 has_reduced_clock)
c7653199 9082 crtc->lowfreq_avail = true;
e2b78267 9083
c8f7a0db 9084 return 0;
79e53945
JB
9085}
9086
eb14cb74
VS
9087static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
9088 struct intel_link_m_n *m_n)
9089{
9090 struct drm_device *dev = crtc->base.dev;
9091 struct drm_i915_private *dev_priv = dev->dev_private;
9092 enum pipe pipe = crtc->pipe;
9093
9094 m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
9095 m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
9096 m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
9097 & ~TU_SIZE_MASK;
9098 m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
9099 m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
9100 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
9101}
9102
9103static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
9104 enum transcoder transcoder,
b95af8be
VK
9105 struct intel_link_m_n *m_n,
9106 struct intel_link_m_n *m2_n2)
72419203
DV
9107{
9108 struct drm_device *dev = crtc->base.dev;
9109 struct drm_i915_private *dev_priv = dev->dev_private;
eb14cb74 9110 enum pipe pipe = crtc->pipe;
72419203 9111
eb14cb74
VS
9112 if (INTEL_INFO(dev)->gen >= 5) {
9113 m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
9114 m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
9115 m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
9116 & ~TU_SIZE_MASK;
9117 m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
9118 m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
9119 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
b95af8be
VK
9120 /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
9121 * gen < 8) and if DRRS is supported (to make sure the
9122 * registers are not unnecessarily read).
9123 */
9124 if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
6e3c9717 9125 crtc->config->has_drrs) {
b95af8be
VK
9126 m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
9127 m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
9128 m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
9129 & ~TU_SIZE_MASK;
9130 m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
9131 m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
9132 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
9133 }
eb14cb74
VS
9134 } else {
9135 m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
9136 m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
9137 m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
9138 & ~TU_SIZE_MASK;
9139 m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
9140 m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
9141 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
9142 }
9143}
9144
9145void intel_dp_get_m_n(struct intel_crtc *crtc,
5cec258b 9146 struct intel_crtc_state *pipe_config)
eb14cb74 9147{
681a8504 9148 if (pipe_config->has_pch_encoder)
eb14cb74
VS
9149 intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
9150 else
9151 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
b95af8be
VK
9152 &pipe_config->dp_m_n,
9153 &pipe_config->dp_m2_n2);
eb14cb74 9154}
72419203 9155
eb14cb74 9156static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
5cec258b 9157 struct intel_crtc_state *pipe_config)
eb14cb74
VS
9158{
9159 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
b95af8be 9160 &pipe_config->fdi_m_n, NULL);
72419203
DV
9161}
9162
bd2e244f 9163static void skylake_get_pfit_config(struct intel_crtc *crtc,
5cec258b 9164 struct intel_crtc_state *pipe_config)
bd2e244f
JB
9165{
9166 struct drm_device *dev = crtc->base.dev;
9167 struct drm_i915_private *dev_priv = dev->dev_private;
a1b2278e
CK
9168 struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
9169 uint32_t ps_ctrl = 0;
9170 int id = -1;
9171 int i;
bd2e244f 9172
a1b2278e
CK
9173 /* find scaler attached to this pipe */
9174 for (i = 0; i < crtc->num_scalers; i++) {
9175 ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
9176 if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
9177 id = i;
9178 pipe_config->pch_pfit.enabled = true;
9179 pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
9180 pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
9181 break;
9182 }
9183 }
bd2e244f 9184
a1b2278e
CK
9185 scaler_state->scaler_id = id;
9186 if (id >= 0) {
9187 scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
9188 } else {
9189 scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
bd2e244f
JB
9190 }
9191}
9192
5724dbd1
DL
9193static void
9194skylake_get_initial_plane_config(struct intel_crtc *crtc,
9195 struct intel_initial_plane_config *plane_config)
bc8d7dff
DL
9196{
9197 struct drm_device *dev = crtc->base.dev;
9198 struct drm_i915_private *dev_priv = dev->dev_private;
40f46283 9199 u32 val, base, offset, stride_mult, tiling;
bc8d7dff
DL
9200 int pipe = crtc->pipe;
9201 int fourcc, pixel_format;
6761dd31 9202 unsigned int aligned_height;
bc8d7dff 9203 struct drm_framebuffer *fb;
1b842c89 9204 struct intel_framebuffer *intel_fb;
bc8d7dff 9205
d9806c9f 9206 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
1b842c89 9207 if (!intel_fb) {
bc8d7dff
DL
9208 DRM_DEBUG_KMS("failed to alloc fb\n");
9209 return;
9210 }
9211
1b842c89
DL
9212 fb = &intel_fb->base;
9213
bc8d7dff 9214 val = I915_READ(PLANE_CTL(pipe, 0));
42a7b088
DL
9215 if (!(val & PLANE_CTL_ENABLE))
9216 goto error;
9217
bc8d7dff
DL
9218 pixel_format = val & PLANE_CTL_FORMAT_MASK;
9219 fourcc = skl_format_to_fourcc(pixel_format,
9220 val & PLANE_CTL_ORDER_RGBX,
9221 val & PLANE_CTL_ALPHA_MASK);
9222 fb->pixel_format = fourcc;
9223 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
9224
40f46283
DL
9225 tiling = val & PLANE_CTL_TILED_MASK;
9226 switch (tiling) {
9227 case PLANE_CTL_TILED_LINEAR:
9228 fb->modifier[0] = DRM_FORMAT_MOD_NONE;
9229 break;
9230 case PLANE_CTL_TILED_X:
9231 plane_config->tiling = I915_TILING_X;
9232 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
9233 break;
9234 case PLANE_CTL_TILED_Y:
9235 fb->modifier[0] = I915_FORMAT_MOD_Y_TILED;
9236 break;
9237 case PLANE_CTL_TILED_YF:
9238 fb->modifier[0] = I915_FORMAT_MOD_Yf_TILED;
9239 break;
9240 default:
9241 MISSING_CASE(tiling);
9242 goto error;
9243 }
9244
bc8d7dff
DL
9245 base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
9246 plane_config->base = base;
9247
9248 offset = I915_READ(PLANE_OFFSET(pipe, 0));
9249
9250 val = I915_READ(PLANE_SIZE(pipe, 0));
9251 fb->height = ((val >> 16) & 0xfff) + 1;
9252 fb->width = ((val >> 0) & 0x1fff) + 1;
9253
9254 val = I915_READ(PLANE_STRIDE(pipe, 0));
7b49f948 9255 stride_mult = intel_fb_stride_alignment(dev_priv, fb->modifier[0],
40f46283 9256 fb->pixel_format);
bc8d7dff
DL
9257 fb->pitches[0] = (val & 0x3ff) * stride_mult;
9258
9259 aligned_height = intel_fb_align_height(dev, fb->height,
091df6cb
DV
9260 fb->pixel_format,
9261 fb->modifier[0]);
bc8d7dff 9262
f37b5c2b 9263 plane_config->size = fb->pitches[0] * aligned_height;
bc8d7dff
DL
9264
9265 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9266 pipe_name(pipe), fb->width, fb->height,
9267 fb->bits_per_pixel, base, fb->pitches[0],
9268 plane_config->size);
9269
2d14030b 9270 plane_config->fb = intel_fb;
bc8d7dff
DL
9271 return;
9272
9273error:
9274 kfree(fb);
9275}
9276
2fa2fe9a 9277static void ironlake_get_pfit_config(struct intel_crtc *crtc,
5cec258b 9278 struct intel_crtc_state *pipe_config)
2fa2fe9a
DV
9279{
9280 struct drm_device *dev = crtc->base.dev;
9281 struct drm_i915_private *dev_priv = dev->dev_private;
9282 uint32_t tmp;
9283
9284 tmp = I915_READ(PF_CTL(crtc->pipe));
9285
9286 if (tmp & PF_ENABLE) {
fd4daa9c 9287 pipe_config->pch_pfit.enabled = true;
2fa2fe9a
DV
9288 pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
9289 pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
cb8b2a30
DV
9290
9291 /* We currently do not free assignements of panel fitters on
9292 * ivb/hsw (since we don't use the higher upscaling modes which
9293 * differentiates them) so just WARN about this case for now. */
9294 if (IS_GEN7(dev)) {
9295 WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
9296 PF_PIPE_SEL_IVB(crtc->pipe));
9297 }
2fa2fe9a 9298 }
79e53945
JB
9299}
9300
5724dbd1
DL
9301static void
9302ironlake_get_initial_plane_config(struct intel_crtc *crtc,
9303 struct intel_initial_plane_config *plane_config)
4c6baa59
JB
9304{
9305 struct drm_device *dev = crtc->base.dev;
9306 struct drm_i915_private *dev_priv = dev->dev_private;
9307 u32 val, base, offset;
aeee5a49 9308 int pipe = crtc->pipe;
4c6baa59 9309 int fourcc, pixel_format;
6761dd31 9310 unsigned int aligned_height;
b113d5ee 9311 struct drm_framebuffer *fb;
1b842c89 9312 struct intel_framebuffer *intel_fb;
4c6baa59 9313
42a7b088
DL
9314 val = I915_READ(DSPCNTR(pipe));
9315 if (!(val & DISPLAY_PLANE_ENABLE))
9316 return;
9317
d9806c9f 9318 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
1b842c89 9319 if (!intel_fb) {
4c6baa59
JB
9320 DRM_DEBUG_KMS("failed to alloc fb\n");
9321 return;
9322 }
9323
1b842c89
DL
9324 fb = &intel_fb->base;
9325
18c5247e
DV
9326 if (INTEL_INFO(dev)->gen >= 4) {
9327 if (val & DISPPLANE_TILED) {
49af449b 9328 plane_config->tiling = I915_TILING_X;
18c5247e
DV
9329 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
9330 }
9331 }
4c6baa59
JB
9332
9333 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
b35d63fa 9334 fourcc = i9xx_format_to_fourcc(pixel_format);
b113d5ee
DL
9335 fb->pixel_format = fourcc;
9336 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
4c6baa59 9337
aeee5a49 9338 base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
4c6baa59 9339 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
aeee5a49 9340 offset = I915_READ(DSPOFFSET(pipe));
4c6baa59 9341 } else {
49af449b 9342 if (plane_config->tiling)
aeee5a49 9343 offset = I915_READ(DSPTILEOFF(pipe));
4c6baa59 9344 else
aeee5a49 9345 offset = I915_READ(DSPLINOFF(pipe));
4c6baa59
JB
9346 }
9347 plane_config->base = base;
9348
9349 val = I915_READ(PIPESRC(pipe));
b113d5ee
DL
9350 fb->width = ((val >> 16) & 0xfff) + 1;
9351 fb->height = ((val >> 0) & 0xfff) + 1;
4c6baa59
JB
9352
9353 val = I915_READ(DSPSTRIDE(pipe));
b113d5ee 9354 fb->pitches[0] = val & 0xffffffc0;
4c6baa59 9355
b113d5ee 9356 aligned_height = intel_fb_align_height(dev, fb->height,
091df6cb
DV
9357 fb->pixel_format,
9358 fb->modifier[0]);
4c6baa59 9359
f37b5c2b 9360 plane_config->size = fb->pitches[0] * aligned_height;
4c6baa59 9361
2844a921
DL
9362 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9363 pipe_name(pipe), fb->width, fb->height,
9364 fb->bits_per_pixel, base, fb->pitches[0],
9365 plane_config->size);
b113d5ee 9366
2d14030b 9367 plane_config->fb = intel_fb;
4c6baa59
JB
9368}
9369
0e8ffe1b 9370static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
5cec258b 9371 struct intel_crtc_state *pipe_config)
0e8ffe1b
DV
9372{
9373 struct drm_device *dev = crtc->base.dev;
9374 struct drm_i915_private *dev_priv = dev->dev_private;
1729050e 9375 enum intel_display_power_domain power_domain;
0e8ffe1b 9376 uint32_t tmp;
1729050e 9377 bool ret;
0e8ffe1b 9378
1729050e
ID
9379 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
9380 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
930e8c9e
PZ
9381 return false;
9382
e143a21c 9383 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
8106ddbd 9384 pipe_config->shared_dpll = NULL;
eccb140b 9385
1729050e 9386 ret = false;
0e8ffe1b
DV
9387 tmp = I915_READ(PIPECONF(crtc->pipe));
9388 if (!(tmp & PIPECONF_ENABLE))
1729050e 9389 goto out;
0e8ffe1b 9390
42571aef
VS
9391 switch (tmp & PIPECONF_BPC_MASK) {
9392 case PIPECONF_6BPC:
9393 pipe_config->pipe_bpp = 18;
9394 break;
9395 case PIPECONF_8BPC:
9396 pipe_config->pipe_bpp = 24;
9397 break;
9398 case PIPECONF_10BPC:
9399 pipe_config->pipe_bpp = 30;
9400 break;
9401 case PIPECONF_12BPC:
9402 pipe_config->pipe_bpp = 36;
9403 break;
9404 default:
9405 break;
9406 }
9407
b5a9fa09
DV
9408 if (tmp & PIPECONF_COLOR_RANGE_SELECT)
9409 pipe_config->limited_color_range = true;
9410
ab9412ba 9411 if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
66e985c0 9412 struct intel_shared_dpll *pll;
8106ddbd 9413 enum intel_dpll_id pll_id;
66e985c0 9414
88adfff1
DV
9415 pipe_config->has_pch_encoder = true;
9416
627eb5a3
DV
9417 tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
9418 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
9419 FDI_DP_PORT_WIDTH_SHIFT) + 1;
72419203
DV
9420
9421 ironlake_get_fdi_m_n_config(crtc, pipe_config);
6c49f241 9422
2d1fe073 9423 if (HAS_PCH_IBX(dev_priv)) {
d9a7bc67
ID
9424 /*
9425 * The pipe->pch transcoder and pch transcoder->pll
9426 * mapping is fixed.
9427 */
8106ddbd 9428 pll_id = (enum intel_dpll_id) crtc->pipe;
c0d43d62
DV
9429 } else {
9430 tmp = I915_READ(PCH_DPLL_SEL);
9431 if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
8106ddbd 9432 pll_id = DPLL_ID_PCH_PLL_B;
c0d43d62 9433 else
8106ddbd 9434 pll_id= DPLL_ID_PCH_PLL_A;
c0d43d62 9435 }
66e985c0 9436
8106ddbd
ACO
9437 pipe_config->shared_dpll =
9438 intel_get_shared_dpll_by_id(dev_priv, pll_id);
9439 pll = pipe_config->shared_dpll;
66e985c0 9440
2edd6443
ACO
9441 WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
9442 &pipe_config->dpll_hw_state));
c93f54cf
DV
9443
9444 tmp = pipe_config->dpll_hw_state.dpll;
9445 pipe_config->pixel_multiplier =
9446 ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
9447 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
18442d08
VS
9448
9449 ironlake_pch_clock_get(crtc, pipe_config);
6c49f241
DV
9450 } else {
9451 pipe_config->pixel_multiplier = 1;
627eb5a3
DV
9452 }
9453
1bd1bd80 9454 intel_get_pipe_timings(crtc, pipe_config);
bc58be60 9455 intel_get_pipe_src_size(crtc, pipe_config);
1bd1bd80 9456
2fa2fe9a
DV
9457 ironlake_get_pfit_config(crtc, pipe_config);
9458
1729050e
ID
9459 ret = true;
9460
9461out:
9462 intel_display_power_put(dev_priv, power_domain);
9463
9464 return ret;
0e8ffe1b
DV
9465}
9466
be256dc7
PZ
9467static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
9468{
9469 struct drm_device *dev = dev_priv->dev;
be256dc7 9470 struct intel_crtc *crtc;
be256dc7 9471
d3fcc808 9472 for_each_intel_crtc(dev, crtc)
e2c719b7 9473 I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
be256dc7
PZ
9474 pipe_name(crtc->pipe));
9475
e2c719b7
RC
9476 I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
9477 I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
01403de3
VS
9478 I915_STATE_WARN(I915_READ(WRPLL_CTL(0)) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
9479 I915_STATE_WARN(I915_READ(WRPLL_CTL(1)) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
e2c719b7
RC
9480 I915_STATE_WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
9481 I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
be256dc7 9482 "CPU PWM1 enabled\n");
c5107b87 9483 if (IS_HASWELL(dev))
e2c719b7 9484 I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
c5107b87 9485 "CPU PWM2 enabled\n");
e2c719b7 9486 I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
be256dc7 9487 "PCH PWM1 enabled\n");
e2c719b7 9488 I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
be256dc7 9489 "Utility pin enabled\n");
e2c719b7 9490 I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
be256dc7 9491
9926ada1
PZ
9492 /*
9493 * In theory we can still leave IRQs enabled, as long as only the HPD
9494 * interrupts remain enabled. We used to check for that, but since it's
9495 * gen-specific and since we only disable LCPLL after we fully disable
9496 * the interrupts, the check below should be enough.
9497 */
e2c719b7 9498 I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
be256dc7
PZ
9499}
9500
9ccd5aeb
PZ
9501static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
9502{
9503 struct drm_device *dev = dev_priv->dev;
9504
9505 if (IS_HASWELL(dev))
9506 return I915_READ(D_COMP_HSW);
9507 else
9508 return I915_READ(D_COMP_BDW);
9509}
9510
3c4c9b81
PZ
9511static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
9512{
9513 struct drm_device *dev = dev_priv->dev;
9514
9515 if (IS_HASWELL(dev)) {
9516 mutex_lock(&dev_priv->rps.hw_lock);
9517 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
9518 val))
f475dadf 9519 DRM_ERROR("Failed to write to D_COMP\n");
3c4c9b81
PZ
9520 mutex_unlock(&dev_priv->rps.hw_lock);
9521 } else {
9ccd5aeb
PZ
9522 I915_WRITE(D_COMP_BDW, val);
9523 POSTING_READ(D_COMP_BDW);
3c4c9b81 9524 }
be256dc7
PZ
9525}
9526
9527/*
9528 * This function implements pieces of two sequences from BSpec:
9529 * - Sequence for display software to disable LCPLL
9530 * - Sequence for display software to allow package C8+
9531 * The steps implemented here are just the steps that actually touch the LCPLL
9532 * register. Callers should take care of disabling all the display engine
9533 * functions, doing the mode unset, fixing interrupts, etc.
9534 */
6ff58d53
PZ
9535static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
9536 bool switch_to_fclk, bool allow_power_down)
be256dc7
PZ
9537{
9538 uint32_t val;
9539
9540 assert_can_disable_lcpll(dev_priv);
9541
9542 val = I915_READ(LCPLL_CTL);
9543
9544 if (switch_to_fclk) {
9545 val |= LCPLL_CD_SOURCE_FCLK;
9546 I915_WRITE(LCPLL_CTL, val);
9547
f53dd63f
ID
9548 if (wait_for_us(I915_READ(LCPLL_CTL) &
9549 LCPLL_CD_SOURCE_FCLK_DONE, 1))
be256dc7
PZ
9550 DRM_ERROR("Switching to FCLK failed\n");
9551
9552 val = I915_READ(LCPLL_CTL);
9553 }
9554
9555 val |= LCPLL_PLL_DISABLE;
9556 I915_WRITE(LCPLL_CTL, val);
9557 POSTING_READ(LCPLL_CTL);
9558
9559 if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
9560 DRM_ERROR("LCPLL still locked\n");
9561
9ccd5aeb 9562 val = hsw_read_dcomp(dev_priv);
be256dc7 9563 val |= D_COMP_COMP_DISABLE;
3c4c9b81 9564 hsw_write_dcomp(dev_priv, val);
be256dc7
PZ
9565 ndelay(100);
9566
9ccd5aeb
PZ
9567 if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
9568 1))
be256dc7
PZ
9569 DRM_ERROR("D_COMP RCOMP still in progress\n");
9570
9571 if (allow_power_down) {
9572 val = I915_READ(LCPLL_CTL);
9573 val |= LCPLL_POWER_DOWN_ALLOW;
9574 I915_WRITE(LCPLL_CTL, val);
9575 POSTING_READ(LCPLL_CTL);
9576 }
9577}
9578
9579/*
9580 * Fully restores LCPLL, disallowing power down and switching back to LCPLL
9581 * source.
9582 */
6ff58d53 9583static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
be256dc7
PZ
9584{
9585 uint32_t val;
9586
9587 val = I915_READ(LCPLL_CTL);
9588
9589 if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
9590 LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
9591 return;
9592
a8a8bd54
PZ
9593 /*
9594 * Make sure we're not on PC8 state before disabling PC8, otherwise
9595 * we'll hang the machine. To prevent PC8 state, just enable force_wake.
a8a8bd54 9596 */
59bad947 9597 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
215733fa 9598
be256dc7
PZ
9599 if (val & LCPLL_POWER_DOWN_ALLOW) {
9600 val &= ~LCPLL_POWER_DOWN_ALLOW;
9601 I915_WRITE(LCPLL_CTL, val);
35d8f2eb 9602 POSTING_READ(LCPLL_CTL);
be256dc7
PZ
9603 }
9604
9ccd5aeb 9605 val = hsw_read_dcomp(dev_priv);
be256dc7
PZ
9606 val |= D_COMP_COMP_FORCE;
9607 val &= ~D_COMP_COMP_DISABLE;
3c4c9b81 9608 hsw_write_dcomp(dev_priv, val);
be256dc7
PZ
9609
9610 val = I915_READ(LCPLL_CTL);
9611 val &= ~LCPLL_PLL_DISABLE;
9612 I915_WRITE(LCPLL_CTL, val);
9613
9614 if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
9615 DRM_ERROR("LCPLL not locked yet\n");
9616
9617 if (val & LCPLL_CD_SOURCE_FCLK) {
9618 val = I915_READ(LCPLL_CTL);
9619 val &= ~LCPLL_CD_SOURCE_FCLK;
9620 I915_WRITE(LCPLL_CTL, val);
9621
f53dd63f
ID
9622 if (wait_for_us((I915_READ(LCPLL_CTL) &
9623 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
be256dc7
PZ
9624 DRM_ERROR("Switching back to LCPLL failed\n");
9625 }
215733fa 9626
59bad947 9627 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
b6283055 9628 intel_update_cdclk(dev_priv->dev);
be256dc7
PZ
9629}
9630
765dab67
PZ
9631/*
9632 * Package states C8 and deeper are really deep PC states that can only be
9633 * reached when all the devices on the system allow it, so even if the graphics
9634 * device allows PC8+, it doesn't mean the system will actually get to these
9635 * states. Our driver only allows PC8+ when going into runtime PM.
9636 *
9637 * The requirements for PC8+ are that all the outputs are disabled, the power
9638 * well is disabled and most interrupts are disabled, and these are also
9639 * requirements for runtime PM. When these conditions are met, we manually do
9640 * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
9641 * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
9642 * hang the machine.
9643 *
9644 * When we really reach PC8 or deeper states (not just when we allow it) we lose
9645 * the state of some registers, so when we come back from PC8+ we need to
9646 * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
9647 * need to take care of the registers kept by RC6. Notice that this happens even
9648 * if we don't put the device in PCI D3 state (which is what currently happens
9649 * because of the runtime PM support).
9650 *
9651 * For more, read "Display Sequences for Package C8" on the hardware
9652 * documentation.
9653 */
a14cb6fc 9654void hsw_enable_pc8(struct drm_i915_private *dev_priv)
c67a470b 9655{
c67a470b
PZ
9656 struct drm_device *dev = dev_priv->dev;
9657 uint32_t val;
9658
c67a470b
PZ
9659 DRM_DEBUG_KMS("Enabling package C8+\n");
9660
c2699524 9661 if (HAS_PCH_LPT_LP(dev)) {
c67a470b
PZ
9662 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9663 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
9664 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9665 }
9666
9667 lpt_disable_clkout_dp(dev);
c67a470b
PZ
9668 hsw_disable_lcpll(dev_priv, true, true);
9669}
9670
a14cb6fc 9671void hsw_disable_pc8(struct drm_i915_private *dev_priv)
c67a470b
PZ
9672{
9673 struct drm_device *dev = dev_priv->dev;
9674 uint32_t val;
9675
c67a470b
PZ
9676 DRM_DEBUG_KMS("Disabling package C8+\n");
9677
9678 hsw_restore_lcpll(dev_priv);
c67a470b
PZ
9679 lpt_init_pch_refclk(dev);
9680
c2699524 9681 if (HAS_PCH_LPT_LP(dev)) {
c67a470b
PZ
9682 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9683 val |= PCH_LP_PARTITION_LEVEL_DISABLE;
9684 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9685 }
c67a470b
PZ
9686}
9687
324513c0 9688static void bxt_modeset_commit_cdclk(struct drm_atomic_state *old_state)
f8437dd1 9689{
a821fc46 9690 struct drm_device *dev = old_state->dev;
1a617b77
ML
9691 struct intel_atomic_state *old_intel_state =
9692 to_intel_atomic_state(old_state);
9693 unsigned int req_cdclk = old_intel_state->dev_cdclk;
f8437dd1 9694
324513c0 9695 bxt_set_cdclk(to_i915(dev), req_cdclk);
f8437dd1
VK
9696}
9697
b432e5cf 9698/* compute the max rate for new configuration */
27c329ed 9699static int ilk_max_pixel_rate(struct drm_atomic_state *state)
b432e5cf 9700{
565602d7
ML
9701 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
9702 struct drm_i915_private *dev_priv = state->dev->dev_private;
9703 struct drm_crtc *crtc;
9704 struct drm_crtc_state *cstate;
27c329ed 9705 struct intel_crtc_state *crtc_state;
565602d7
ML
9706 unsigned max_pixel_rate = 0, i;
9707 enum pipe pipe;
b432e5cf 9708
565602d7
ML
9709 memcpy(intel_state->min_pixclk, dev_priv->min_pixclk,
9710 sizeof(intel_state->min_pixclk));
27c329ed 9711
565602d7
ML
9712 for_each_crtc_in_state(state, crtc, cstate, i) {
9713 int pixel_rate;
27c329ed 9714
565602d7
ML
9715 crtc_state = to_intel_crtc_state(cstate);
9716 if (!crtc_state->base.enable) {
9717 intel_state->min_pixclk[i] = 0;
b432e5cf 9718 continue;
565602d7 9719 }
b432e5cf 9720
27c329ed 9721 pixel_rate = ilk_pipe_pixel_rate(crtc_state);
b432e5cf
VS
9722
9723 /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
565602d7 9724 if (IS_BROADWELL(dev_priv) && crtc_state->ips_enabled)
b432e5cf
VS
9725 pixel_rate = DIV_ROUND_UP(pixel_rate * 100, 95);
9726
565602d7 9727 intel_state->min_pixclk[i] = pixel_rate;
b432e5cf
VS
9728 }
9729
565602d7
ML
9730 for_each_pipe(dev_priv, pipe)
9731 max_pixel_rate = max(intel_state->min_pixclk[pipe], max_pixel_rate);
9732
b432e5cf
VS
9733 return max_pixel_rate;
9734}
9735
9736static void broadwell_set_cdclk(struct drm_device *dev, int cdclk)
9737{
9738 struct drm_i915_private *dev_priv = dev->dev_private;
9739 uint32_t val, data;
9740 int ret;
9741
9742 if (WARN((I915_READ(LCPLL_CTL) &
9743 (LCPLL_PLL_DISABLE | LCPLL_PLL_LOCK |
9744 LCPLL_CD_CLOCK_DISABLE | LCPLL_ROOT_CD_CLOCK_DISABLE |
9745 LCPLL_CD2X_CLOCK_DISABLE | LCPLL_POWER_DOWN_ALLOW |
9746 LCPLL_CD_SOURCE_FCLK)) != LCPLL_PLL_LOCK,
9747 "trying to change cdclk frequency with cdclk not enabled\n"))
9748 return;
9749
9750 mutex_lock(&dev_priv->rps.hw_lock);
9751 ret = sandybridge_pcode_write(dev_priv,
9752 BDW_PCODE_DISPLAY_FREQ_CHANGE_REQ, 0x0);
9753 mutex_unlock(&dev_priv->rps.hw_lock);
9754 if (ret) {
9755 DRM_ERROR("failed to inform pcode about cdclk change\n");
9756 return;
9757 }
9758
9759 val = I915_READ(LCPLL_CTL);
9760 val |= LCPLL_CD_SOURCE_FCLK;
9761 I915_WRITE(LCPLL_CTL, val);
9762
5ba00178
TU
9763 if (wait_for_us(I915_READ(LCPLL_CTL) &
9764 LCPLL_CD_SOURCE_FCLK_DONE, 1))
b432e5cf
VS
9765 DRM_ERROR("Switching to FCLK failed\n");
9766
9767 val = I915_READ(LCPLL_CTL);
9768 val &= ~LCPLL_CLK_FREQ_MASK;
9769
9770 switch (cdclk) {
9771 case 450000:
9772 val |= LCPLL_CLK_FREQ_450;
9773 data = 0;
9774 break;
9775 case 540000:
9776 val |= LCPLL_CLK_FREQ_54O_BDW;
9777 data = 1;
9778 break;
9779 case 337500:
9780 val |= LCPLL_CLK_FREQ_337_5_BDW;
9781 data = 2;
9782 break;
9783 case 675000:
9784 val |= LCPLL_CLK_FREQ_675_BDW;
9785 data = 3;
9786 break;
9787 default:
9788 WARN(1, "invalid cdclk frequency\n");
9789 return;
9790 }
9791
9792 I915_WRITE(LCPLL_CTL, val);
9793
9794 val = I915_READ(LCPLL_CTL);
9795 val &= ~LCPLL_CD_SOURCE_FCLK;
9796 I915_WRITE(LCPLL_CTL, val);
9797
5ba00178
TU
9798 if (wait_for_us((I915_READ(LCPLL_CTL) &
9799 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
b432e5cf
VS
9800 DRM_ERROR("Switching back to LCPLL failed\n");
9801
9802 mutex_lock(&dev_priv->rps.hw_lock);
9803 sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ, data);
9804 mutex_unlock(&dev_priv->rps.hw_lock);
9805
7f1052a8
VS
9806 I915_WRITE(CDCLK_FREQ, DIV_ROUND_CLOSEST(cdclk, 1000) - 1);
9807
b432e5cf
VS
9808 intel_update_cdclk(dev);
9809
9810 WARN(cdclk != dev_priv->cdclk_freq,
9811 "cdclk requested %d kHz but got %d kHz\n",
9812 cdclk, dev_priv->cdclk_freq);
9813}
9814
587c7914
VS
9815static int broadwell_calc_cdclk(int max_pixclk)
9816{
9817 if (max_pixclk > 540000)
9818 return 675000;
9819 else if (max_pixclk > 450000)
9820 return 540000;
9821 else if (max_pixclk > 337500)
9822 return 450000;
9823 else
9824 return 337500;
9825}
9826
27c329ed 9827static int broadwell_modeset_calc_cdclk(struct drm_atomic_state *state)
b432e5cf 9828{
27c329ed 9829 struct drm_i915_private *dev_priv = to_i915(state->dev);
1a617b77 9830 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
27c329ed 9831 int max_pixclk = ilk_max_pixel_rate(state);
b432e5cf
VS
9832 int cdclk;
9833
9834 /*
9835 * FIXME should also account for plane ratio
9836 * once 64bpp pixel formats are supported.
9837 */
587c7914 9838 cdclk = broadwell_calc_cdclk(max_pixclk);
b432e5cf 9839
b432e5cf 9840 if (cdclk > dev_priv->max_cdclk_freq) {
63ba534e
ML
9841 DRM_DEBUG_KMS("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
9842 cdclk, dev_priv->max_cdclk_freq);
9843 return -EINVAL;
b432e5cf
VS
9844 }
9845
1a617b77
ML
9846 intel_state->cdclk = intel_state->dev_cdclk = cdclk;
9847 if (!intel_state->active_crtcs)
587c7914 9848 intel_state->dev_cdclk = broadwell_calc_cdclk(0);
b432e5cf
VS
9849
9850 return 0;
9851}
9852
27c329ed 9853static void broadwell_modeset_commit_cdclk(struct drm_atomic_state *old_state)
b432e5cf 9854{
27c329ed 9855 struct drm_device *dev = old_state->dev;
1a617b77
ML
9856 struct intel_atomic_state *old_intel_state =
9857 to_intel_atomic_state(old_state);
9858 unsigned req_cdclk = old_intel_state->dev_cdclk;
b432e5cf 9859
27c329ed 9860 broadwell_set_cdclk(dev, req_cdclk);
b432e5cf
VS
9861}
9862
c89e39f3
CT
9863static int skl_modeset_calc_cdclk(struct drm_atomic_state *state)
9864{
9865 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
9866 struct drm_i915_private *dev_priv = to_i915(state->dev);
9867 const int max_pixclk = ilk_max_pixel_rate(state);
a8ca4934 9868 int vco = intel_state->cdclk_pll_vco;
c89e39f3
CT
9869 int cdclk;
9870
9871 /*
9872 * FIXME should also account for plane ratio
9873 * once 64bpp pixel formats are supported.
9874 */
a8ca4934 9875 cdclk = skl_calc_cdclk(max_pixclk, vco);
c89e39f3
CT
9876
9877 /*
9878 * FIXME move the cdclk caclulation to
9879 * compute_config() so we can fail gracegully.
9880 */
9881 if (cdclk > dev_priv->max_cdclk_freq) {
9882 DRM_ERROR("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
9883 cdclk, dev_priv->max_cdclk_freq);
9884 cdclk = dev_priv->max_cdclk_freq;
9885 }
9886
9887 intel_state->cdclk = intel_state->dev_cdclk = cdclk;
9888 if (!intel_state->active_crtcs)
a8ca4934 9889 intel_state->dev_cdclk = skl_calc_cdclk(0, vco);
c89e39f3
CT
9890
9891 return 0;
9892}
9893
9894static void skl_modeset_commit_cdclk(struct drm_atomic_state *old_state)
9895{
1cd593e0
VS
9896 struct drm_i915_private *dev_priv = to_i915(old_state->dev);
9897 struct intel_atomic_state *intel_state = to_intel_atomic_state(old_state);
9898 unsigned int req_cdclk = intel_state->dev_cdclk;
9899 unsigned int req_vco = intel_state->cdclk_pll_vco;
c89e39f3 9900
1cd593e0 9901 skl_set_cdclk(dev_priv, req_cdclk, req_vco);
c89e39f3
CT
9902}
9903
190f68c5
ACO
9904static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
9905 struct intel_crtc_state *crtc_state)
09b4ddf9 9906{
af3997b5
MK
9907 struct intel_encoder *intel_encoder =
9908 intel_ddi_get_crtc_new_encoder(crtc_state);
9909
9910 if (intel_encoder->type != INTEL_OUTPUT_DSI) {
9911 if (!intel_ddi_pll_select(crtc, crtc_state))
9912 return -EINVAL;
9913 }
716c2e55 9914
c7653199 9915 crtc->lowfreq_avail = false;
644cef34 9916
c8f7a0db 9917 return 0;
79e53945
JB
9918}
9919
3760b59c
S
9920static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
9921 enum port port,
9922 struct intel_crtc_state *pipe_config)
9923{
8106ddbd
ACO
9924 enum intel_dpll_id id;
9925
3760b59c
S
9926 switch (port) {
9927 case PORT_A:
9928 pipe_config->ddi_pll_sel = SKL_DPLL0;
08250c4b 9929 id = DPLL_ID_SKL_DPLL0;
3760b59c
S
9930 break;
9931 case PORT_B:
9932 pipe_config->ddi_pll_sel = SKL_DPLL1;
08250c4b 9933 id = DPLL_ID_SKL_DPLL1;
3760b59c
S
9934 break;
9935 case PORT_C:
9936 pipe_config->ddi_pll_sel = SKL_DPLL2;
08250c4b 9937 id = DPLL_ID_SKL_DPLL2;
3760b59c
S
9938 break;
9939 default:
9940 DRM_ERROR("Incorrect port type\n");
8106ddbd 9941 return;
3760b59c 9942 }
8106ddbd
ACO
9943
9944 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
3760b59c
S
9945}
9946
96b7dfb7
S
9947static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
9948 enum port port,
5cec258b 9949 struct intel_crtc_state *pipe_config)
96b7dfb7 9950{
8106ddbd 9951 enum intel_dpll_id id;
a3c988ea 9952 u32 temp;
96b7dfb7
S
9953
9954 temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
9955 pipe_config->ddi_pll_sel = temp >> (port * 3 + 1);
9956
9957 switch (pipe_config->ddi_pll_sel) {
3148ade7 9958 case SKL_DPLL0:
a3c988ea
ACO
9959 id = DPLL_ID_SKL_DPLL0;
9960 break;
96b7dfb7 9961 case SKL_DPLL1:
8106ddbd 9962 id = DPLL_ID_SKL_DPLL1;
96b7dfb7
S
9963 break;
9964 case SKL_DPLL2:
8106ddbd 9965 id = DPLL_ID_SKL_DPLL2;
96b7dfb7
S
9966 break;
9967 case SKL_DPLL3:
8106ddbd 9968 id = DPLL_ID_SKL_DPLL3;
96b7dfb7 9969 break;
8106ddbd
ACO
9970 default:
9971 MISSING_CASE(pipe_config->ddi_pll_sel);
9972 return;
96b7dfb7 9973 }
8106ddbd
ACO
9974
9975 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
96b7dfb7
S
9976}
9977
7d2c8175
DL
9978static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
9979 enum port port,
5cec258b 9980 struct intel_crtc_state *pipe_config)
7d2c8175 9981{
8106ddbd
ACO
9982 enum intel_dpll_id id;
9983
7d2c8175
DL
9984 pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
9985
9986 switch (pipe_config->ddi_pll_sel) {
9987 case PORT_CLK_SEL_WRPLL1:
8106ddbd 9988 id = DPLL_ID_WRPLL1;
7d2c8175
DL
9989 break;
9990 case PORT_CLK_SEL_WRPLL2:
8106ddbd 9991 id = DPLL_ID_WRPLL2;
7d2c8175 9992 break;
00490c22 9993 case PORT_CLK_SEL_SPLL:
8106ddbd 9994 id = DPLL_ID_SPLL;
79bd23da 9995 break;
9d16da65
ACO
9996 case PORT_CLK_SEL_LCPLL_810:
9997 id = DPLL_ID_LCPLL_810;
9998 break;
9999 case PORT_CLK_SEL_LCPLL_1350:
10000 id = DPLL_ID_LCPLL_1350;
10001 break;
10002 case PORT_CLK_SEL_LCPLL_2700:
10003 id = DPLL_ID_LCPLL_2700;
10004 break;
8106ddbd
ACO
10005 default:
10006 MISSING_CASE(pipe_config->ddi_pll_sel);
10007 /* fall through */
10008 case PORT_CLK_SEL_NONE:
8106ddbd 10009 return;
7d2c8175 10010 }
8106ddbd
ACO
10011
10012 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
7d2c8175
DL
10013}
10014
cf30429e
JN
10015static bool hsw_get_transcoder_state(struct intel_crtc *crtc,
10016 struct intel_crtc_state *pipe_config,
10017 unsigned long *power_domain_mask)
10018{
10019 struct drm_device *dev = crtc->base.dev;
10020 struct drm_i915_private *dev_priv = dev->dev_private;
10021 enum intel_display_power_domain power_domain;
10022 u32 tmp;
10023
d9a7bc67
ID
10024 /*
10025 * The pipe->transcoder mapping is fixed with the exception of the eDP
10026 * transcoder handled below.
10027 */
cf30429e
JN
10028 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
10029
10030 /*
10031 * XXX: Do intel_display_power_get_if_enabled before reading this (for
10032 * consistency and less surprising code; it's in always on power).
10033 */
10034 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
10035 if (tmp & TRANS_DDI_FUNC_ENABLE) {
10036 enum pipe trans_edp_pipe;
10037 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
10038 default:
10039 WARN(1, "unknown pipe linked to edp transcoder\n");
10040 case TRANS_DDI_EDP_INPUT_A_ONOFF:
10041 case TRANS_DDI_EDP_INPUT_A_ON:
10042 trans_edp_pipe = PIPE_A;
10043 break;
10044 case TRANS_DDI_EDP_INPUT_B_ONOFF:
10045 trans_edp_pipe = PIPE_B;
10046 break;
10047 case TRANS_DDI_EDP_INPUT_C_ONOFF:
10048 trans_edp_pipe = PIPE_C;
10049 break;
10050 }
10051
10052 if (trans_edp_pipe == crtc->pipe)
10053 pipe_config->cpu_transcoder = TRANSCODER_EDP;
10054 }
10055
10056 power_domain = POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder);
10057 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
10058 return false;
10059 *power_domain_mask |= BIT(power_domain);
10060
10061 tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
10062
10063 return tmp & PIPECONF_ENABLE;
10064}
10065
4d1de975
JN
10066static bool bxt_get_dsi_transcoder_state(struct intel_crtc *crtc,
10067 struct intel_crtc_state *pipe_config,
10068 unsigned long *power_domain_mask)
10069{
10070 struct drm_device *dev = crtc->base.dev;
10071 struct drm_i915_private *dev_priv = dev->dev_private;
10072 enum intel_display_power_domain power_domain;
10073 enum port port;
10074 enum transcoder cpu_transcoder;
10075 u32 tmp;
10076
10077 pipe_config->has_dsi_encoder = false;
10078
10079 for_each_port_masked(port, BIT(PORT_A) | BIT(PORT_C)) {
10080 if (port == PORT_A)
10081 cpu_transcoder = TRANSCODER_DSI_A;
10082 else
10083 cpu_transcoder = TRANSCODER_DSI_C;
10084
10085 power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
10086 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
10087 continue;
10088 *power_domain_mask |= BIT(power_domain);
10089
db18b6a6
ID
10090 /*
10091 * The PLL needs to be enabled with a valid divider
10092 * configuration, otherwise accessing DSI registers will hang
10093 * the machine. See BSpec North Display Engine
10094 * registers/MIPI[BXT]. We can break out here early, since we
10095 * need the same DSI PLL to be enabled for both DSI ports.
10096 */
10097 if (!intel_dsi_pll_is_enabled(dev_priv))
10098 break;
10099
4d1de975
JN
10100 /* XXX: this works for video mode only */
10101 tmp = I915_READ(BXT_MIPI_PORT_CTRL(port));
10102 if (!(tmp & DPI_ENABLE))
10103 continue;
10104
10105 tmp = I915_READ(MIPI_CTRL(port));
10106 if ((tmp & BXT_PIPE_SELECT_MASK) != BXT_PIPE_SELECT(crtc->pipe))
10107 continue;
10108
10109 pipe_config->cpu_transcoder = cpu_transcoder;
10110 pipe_config->has_dsi_encoder = true;
10111 break;
10112 }
10113
10114 return pipe_config->has_dsi_encoder;
10115}
10116
26804afd 10117static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
5cec258b 10118 struct intel_crtc_state *pipe_config)
26804afd
DV
10119{
10120 struct drm_device *dev = crtc->base.dev;
10121 struct drm_i915_private *dev_priv = dev->dev_private;
d452c5b6 10122 struct intel_shared_dpll *pll;
26804afd
DV
10123 enum port port;
10124 uint32_t tmp;
10125
10126 tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
10127
10128 port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
10129
ef11bdb3 10130 if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev))
96b7dfb7 10131 skylake_get_ddi_pll(dev_priv, port, pipe_config);
3760b59c
S
10132 else if (IS_BROXTON(dev))
10133 bxt_get_ddi_pll(dev_priv, port, pipe_config);
96b7dfb7
S
10134 else
10135 haswell_get_ddi_pll(dev_priv, port, pipe_config);
9cd86933 10136
8106ddbd
ACO
10137 pll = pipe_config->shared_dpll;
10138 if (pll) {
2edd6443
ACO
10139 WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
10140 &pipe_config->dpll_hw_state));
d452c5b6
DV
10141 }
10142
26804afd
DV
10143 /*
10144 * Haswell has only FDI/PCH transcoder A. It is which is connected to
10145 * DDI E. So just check whether this pipe is wired to DDI E and whether
10146 * the PCH transcoder is on.
10147 */
ca370455
DL
10148 if (INTEL_INFO(dev)->gen < 9 &&
10149 (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
26804afd
DV
10150 pipe_config->has_pch_encoder = true;
10151
10152 tmp = I915_READ(FDI_RX_CTL(PIPE_A));
10153 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
10154 FDI_DP_PORT_WIDTH_SHIFT) + 1;
10155
10156 ironlake_get_fdi_m_n_config(crtc, pipe_config);
10157 }
10158}
10159
0e8ffe1b 10160static bool haswell_get_pipe_config(struct intel_crtc *crtc,
5cec258b 10161 struct intel_crtc_state *pipe_config)
0e8ffe1b
DV
10162{
10163 struct drm_device *dev = crtc->base.dev;
10164 struct drm_i915_private *dev_priv = dev->dev_private;
1729050e
ID
10165 enum intel_display_power_domain power_domain;
10166 unsigned long power_domain_mask;
cf30429e 10167 bool active;
0e8ffe1b 10168
1729050e
ID
10169 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
10170 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
b5482bd0 10171 return false;
1729050e
ID
10172 power_domain_mask = BIT(power_domain);
10173
8106ddbd 10174 pipe_config->shared_dpll = NULL;
c0d43d62 10175
cf30429e 10176 active = hsw_get_transcoder_state(crtc, pipe_config, &power_domain_mask);
eccb140b 10177
4d1de975
JN
10178 if (IS_BROXTON(dev_priv)) {
10179 bxt_get_dsi_transcoder_state(crtc, pipe_config,
10180 &power_domain_mask);
10181 WARN_ON(active && pipe_config->has_dsi_encoder);
10182 if (pipe_config->has_dsi_encoder)
10183 active = true;
10184 }
10185
cf30429e 10186 if (!active)
1729050e 10187 goto out;
0e8ffe1b 10188
4d1de975
JN
10189 if (!pipe_config->has_dsi_encoder) {
10190 haswell_get_ddi_port_state(crtc, pipe_config);
10191 intel_get_pipe_timings(crtc, pipe_config);
10192 }
627eb5a3 10193
bc58be60 10194 intel_get_pipe_src_size(crtc, pipe_config);
1bd1bd80 10195
05dc698c
LL
10196 pipe_config->gamma_mode =
10197 I915_READ(GAMMA_MODE(crtc->pipe)) & GAMMA_MODE_MODE_MASK;
10198
a1b2278e
CK
10199 if (INTEL_INFO(dev)->gen >= 9) {
10200 skl_init_scalers(dev, crtc, pipe_config);
10201 }
10202
af99ceda
CK
10203 if (INTEL_INFO(dev)->gen >= 9) {
10204 pipe_config->scaler_state.scaler_id = -1;
10205 pipe_config->scaler_state.scaler_users &= ~(1 << SKL_CRTC_INDEX);
10206 }
10207
1729050e
ID
10208 power_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
10209 if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
10210 power_domain_mask |= BIT(power_domain);
1c132b44 10211 if (INTEL_INFO(dev)->gen >= 9)
bd2e244f 10212 skylake_get_pfit_config(crtc, pipe_config);
ff6d9f55 10213 else
1c132b44 10214 ironlake_get_pfit_config(crtc, pipe_config);
bd2e244f 10215 }
88adfff1 10216
e59150dc
JB
10217 if (IS_HASWELL(dev))
10218 pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
10219 (I915_READ(IPS_CTL) & IPS_ENABLE);
42db64ef 10220
4d1de975
JN
10221 if (pipe_config->cpu_transcoder != TRANSCODER_EDP &&
10222 !transcoder_is_dsi(pipe_config->cpu_transcoder)) {
ebb69c95
CT
10223 pipe_config->pixel_multiplier =
10224 I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
10225 } else {
10226 pipe_config->pixel_multiplier = 1;
10227 }
6c49f241 10228
1729050e
ID
10229out:
10230 for_each_power_domain(power_domain, power_domain_mask)
10231 intel_display_power_put(dev_priv, power_domain);
10232
cf30429e 10233 return active;
0e8ffe1b
DV
10234}
10235
55a08b3f
ML
10236static void i845_update_cursor(struct drm_crtc *crtc, u32 base,
10237 const struct intel_plane_state *plane_state)
560b85bb
CW
10238{
10239 struct drm_device *dev = crtc->dev;
10240 struct drm_i915_private *dev_priv = dev->dev_private;
10241 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
dc41c154 10242 uint32_t cntl = 0, size = 0;
560b85bb 10243
55a08b3f
ML
10244 if (plane_state && plane_state->visible) {
10245 unsigned int width = plane_state->base.crtc_w;
10246 unsigned int height = plane_state->base.crtc_h;
dc41c154
VS
10247 unsigned int stride = roundup_pow_of_two(width) * 4;
10248
10249 switch (stride) {
10250 default:
10251 WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
10252 width, stride);
10253 stride = 256;
10254 /* fallthrough */
10255 case 256:
10256 case 512:
10257 case 1024:
10258 case 2048:
10259 break;
4b0e333e
CW
10260 }
10261
dc41c154
VS
10262 cntl |= CURSOR_ENABLE |
10263 CURSOR_GAMMA_ENABLE |
10264 CURSOR_FORMAT_ARGB |
10265 CURSOR_STRIDE(stride);
10266
10267 size = (height << 12) | width;
4b0e333e 10268 }
560b85bb 10269
dc41c154
VS
10270 if (intel_crtc->cursor_cntl != 0 &&
10271 (intel_crtc->cursor_base != base ||
10272 intel_crtc->cursor_size != size ||
10273 intel_crtc->cursor_cntl != cntl)) {
10274 /* On these chipsets we can only modify the base/size/stride
10275 * whilst the cursor is disabled.
10276 */
0b87c24e
VS
10277 I915_WRITE(CURCNTR(PIPE_A), 0);
10278 POSTING_READ(CURCNTR(PIPE_A));
dc41c154 10279 intel_crtc->cursor_cntl = 0;
4b0e333e 10280 }
560b85bb 10281
99d1f387 10282 if (intel_crtc->cursor_base != base) {
0b87c24e 10283 I915_WRITE(CURBASE(PIPE_A), base);
99d1f387
VS
10284 intel_crtc->cursor_base = base;
10285 }
4726e0b0 10286
dc41c154
VS
10287 if (intel_crtc->cursor_size != size) {
10288 I915_WRITE(CURSIZE, size);
10289 intel_crtc->cursor_size = size;
4b0e333e 10290 }
560b85bb 10291
4b0e333e 10292 if (intel_crtc->cursor_cntl != cntl) {
0b87c24e
VS
10293 I915_WRITE(CURCNTR(PIPE_A), cntl);
10294 POSTING_READ(CURCNTR(PIPE_A));
4b0e333e 10295 intel_crtc->cursor_cntl = cntl;
560b85bb 10296 }
560b85bb
CW
10297}
10298
55a08b3f
ML
10299static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base,
10300 const struct intel_plane_state *plane_state)
65a21cd6
JB
10301{
10302 struct drm_device *dev = crtc->dev;
10303 struct drm_i915_private *dev_priv = dev->dev_private;
10304 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10305 int pipe = intel_crtc->pipe;
663f3122 10306 uint32_t cntl = 0;
4b0e333e 10307
55a08b3f 10308 if (plane_state && plane_state->visible) {
4b0e333e 10309 cntl = MCURSOR_GAMMA_ENABLE;
55a08b3f 10310 switch (plane_state->base.crtc_w) {
4726e0b0
SK
10311 case 64:
10312 cntl |= CURSOR_MODE_64_ARGB_AX;
10313 break;
10314 case 128:
10315 cntl |= CURSOR_MODE_128_ARGB_AX;
10316 break;
10317 case 256:
10318 cntl |= CURSOR_MODE_256_ARGB_AX;
10319 break;
10320 default:
55a08b3f 10321 MISSING_CASE(plane_state->base.crtc_w);
4726e0b0 10322 return;
65a21cd6 10323 }
4b0e333e 10324 cntl |= pipe << 28; /* Connect to correct pipe */
47bf17a7 10325
fc6f93bc 10326 if (HAS_DDI(dev))
47bf17a7 10327 cntl |= CURSOR_PIPE_CSC_ENABLE;
65a21cd6 10328
55a08b3f
ML
10329 if (plane_state->base.rotation == BIT(DRM_ROTATE_180))
10330 cntl |= CURSOR_ROTATE_180;
10331 }
4398ad45 10332
4b0e333e
CW
10333 if (intel_crtc->cursor_cntl != cntl) {
10334 I915_WRITE(CURCNTR(pipe), cntl);
10335 POSTING_READ(CURCNTR(pipe));
10336 intel_crtc->cursor_cntl = cntl;
65a21cd6 10337 }
4b0e333e 10338
65a21cd6 10339 /* and commit changes on next vblank */
5efb3e28
VS
10340 I915_WRITE(CURBASE(pipe), base);
10341 POSTING_READ(CURBASE(pipe));
99d1f387
VS
10342
10343 intel_crtc->cursor_base = base;
65a21cd6
JB
10344}
10345
cda4b7d3 10346/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
6b383a7f 10347static void intel_crtc_update_cursor(struct drm_crtc *crtc,
55a08b3f 10348 const struct intel_plane_state *plane_state)
cda4b7d3
CW
10349{
10350 struct drm_device *dev = crtc->dev;
10351 struct drm_i915_private *dev_priv = dev->dev_private;
10352 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10353 int pipe = intel_crtc->pipe;
55a08b3f
ML
10354 u32 base = intel_crtc->cursor_addr;
10355 u32 pos = 0;
cda4b7d3 10356
55a08b3f
ML
10357 if (plane_state) {
10358 int x = plane_state->base.crtc_x;
10359 int y = plane_state->base.crtc_y;
cda4b7d3 10360
55a08b3f
ML
10361 if (x < 0) {
10362 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
10363 x = -x;
10364 }
10365 pos |= x << CURSOR_X_SHIFT;
cda4b7d3 10366
55a08b3f
ML
10367 if (y < 0) {
10368 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
10369 y = -y;
10370 }
10371 pos |= y << CURSOR_Y_SHIFT;
10372
10373 /* ILK+ do this automagically */
10374 if (HAS_GMCH_DISPLAY(dev) &&
10375 plane_state->base.rotation == BIT(DRM_ROTATE_180)) {
10376 base += (plane_state->base.crtc_h *
10377 plane_state->base.crtc_w - 1) * 4;
10378 }
cda4b7d3 10379 }
cda4b7d3 10380
5efb3e28
VS
10381 I915_WRITE(CURPOS(pipe), pos);
10382
8ac54669 10383 if (IS_845G(dev) || IS_I865G(dev))
55a08b3f 10384 i845_update_cursor(crtc, base, plane_state);
5efb3e28 10385 else
55a08b3f 10386 i9xx_update_cursor(crtc, base, plane_state);
cda4b7d3
CW
10387}
10388
dc41c154
VS
10389static bool cursor_size_ok(struct drm_device *dev,
10390 uint32_t width, uint32_t height)
10391{
10392 if (width == 0 || height == 0)
10393 return false;
10394
10395 /*
10396 * 845g/865g are special in that they are only limited by
10397 * the width of their cursors, the height is arbitrary up to
10398 * the precision of the register. Everything else requires
10399 * square cursors, limited to a few power-of-two sizes.
10400 */
10401 if (IS_845G(dev) || IS_I865G(dev)) {
10402 if ((width & 63) != 0)
10403 return false;
10404
10405 if (width > (IS_845G(dev) ? 64 : 512))
10406 return false;
10407
10408 if (height > 1023)
10409 return false;
10410 } else {
10411 switch (width | height) {
10412 case 256:
10413 case 128:
10414 if (IS_GEN2(dev))
10415 return false;
10416 case 64:
10417 break;
10418 default:
10419 return false;
10420 }
10421 }
10422
10423 return true;
10424}
10425
79e53945
JB
10426/* VESA 640x480x72Hz mode to set on the pipe */
10427static struct drm_display_mode load_detect_mode = {
10428 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
10429 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
10430};
10431
a8bb6818
DV
10432struct drm_framebuffer *
10433__intel_framebuffer_create(struct drm_device *dev,
10434 struct drm_mode_fb_cmd2 *mode_cmd,
10435 struct drm_i915_gem_object *obj)
d2dff872
CW
10436{
10437 struct intel_framebuffer *intel_fb;
10438 int ret;
10439
10440 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
dcb1394e 10441 if (!intel_fb)
d2dff872 10442 return ERR_PTR(-ENOMEM);
d2dff872
CW
10443
10444 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
dd4916c5
DV
10445 if (ret)
10446 goto err;
d2dff872
CW
10447
10448 return &intel_fb->base;
dcb1394e 10449
dd4916c5 10450err:
dd4916c5 10451 kfree(intel_fb);
dd4916c5 10452 return ERR_PTR(ret);
d2dff872
CW
10453}
10454
b5ea642a 10455static struct drm_framebuffer *
a8bb6818
DV
10456intel_framebuffer_create(struct drm_device *dev,
10457 struct drm_mode_fb_cmd2 *mode_cmd,
10458 struct drm_i915_gem_object *obj)
10459{
10460 struct drm_framebuffer *fb;
10461 int ret;
10462
10463 ret = i915_mutex_lock_interruptible(dev);
10464 if (ret)
10465 return ERR_PTR(ret);
10466 fb = __intel_framebuffer_create(dev, mode_cmd, obj);
10467 mutex_unlock(&dev->struct_mutex);
10468
10469 return fb;
10470}
10471
d2dff872
CW
10472static u32
10473intel_framebuffer_pitch_for_width(int width, int bpp)
10474{
10475 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
10476 return ALIGN(pitch, 64);
10477}
10478
10479static u32
10480intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
10481{
10482 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
1267a26b 10483 return PAGE_ALIGN(pitch * mode->vdisplay);
d2dff872
CW
10484}
10485
10486static struct drm_framebuffer *
10487intel_framebuffer_create_for_mode(struct drm_device *dev,
10488 struct drm_display_mode *mode,
10489 int depth, int bpp)
10490{
dcb1394e 10491 struct drm_framebuffer *fb;
d2dff872 10492 struct drm_i915_gem_object *obj;
0fed39bd 10493 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
d2dff872 10494
d37cd8a8 10495 obj = i915_gem_object_create(dev,
d2dff872 10496 intel_framebuffer_size_for_mode(mode, bpp));
fe3db79b
CW
10497 if (IS_ERR(obj))
10498 return ERR_CAST(obj);
d2dff872
CW
10499
10500 mode_cmd.width = mode->hdisplay;
10501 mode_cmd.height = mode->vdisplay;
308e5bcb
JB
10502 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
10503 bpp);
5ca0c34a 10504 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
d2dff872 10505
dcb1394e
LW
10506 fb = intel_framebuffer_create(dev, &mode_cmd, obj);
10507 if (IS_ERR(fb))
10508 drm_gem_object_unreference_unlocked(&obj->base);
10509
10510 return fb;
d2dff872
CW
10511}
10512
10513static struct drm_framebuffer *
10514mode_fits_in_fbdev(struct drm_device *dev,
10515 struct drm_display_mode *mode)
10516{
0695726e 10517#ifdef CONFIG_DRM_FBDEV_EMULATION
d2dff872
CW
10518 struct drm_i915_private *dev_priv = dev->dev_private;
10519 struct drm_i915_gem_object *obj;
10520 struct drm_framebuffer *fb;
10521
4c0e5528 10522 if (!dev_priv->fbdev)
d2dff872
CW
10523 return NULL;
10524
4c0e5528 10525 if (!dev_priv->fbdev->fb)
d2dff872
CW
10526 return NULL;
10527
4c0e5528
DV
10528 obj = dev_priv->fbdev->fb->obj;
10529 BUG_ON(!obj);
10530
8bcd4553 10531 fb = &dev_priv->fbdev->fb->base;
01f2c773
VS
10532 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
10533 fb->bits_per_pixel))
d2dff872
CW
10534 return NULL;
10535
01f2c773 10536 if (obj->base.size < mode->vdisplay * fb->pitches[0])
d2dff872
CW
10537 return NULL;
10538
edde3617 10539 drm_framebuffer_reference(fb);
d2dff872 10540 return fb;
4520f53a
DV
10541#else
10542 return NULL;
10543#endif
d2dff872
CW
10544}
10545
d3a40d1b
ACO
10546static int intel_modeset_setup_plane_state(struct drm_atomic_state *state,
10547 struct drm_crtc *crtc,
10548 struct drm_display_mode *mode,
10549 struct drm_framebuffer *fb,
10550 int x, int y)
10551{
10552 struct drm_plane_state *plane_state;
10553 int hdisplay, vdisplay;
10554 int ret;
10555
10556 plane_state = drm_atomic_get_plane_state(state, crtc->primary);
10557 if (IS_ERR(plane_state))
10558 return PTR_ERR(plane_state);
10559
10560 if (mode)
10561 drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
10562 else
10563 hdisplay = vdisplay = 0;
10564
10565 ret = drm_atomic_set_crtc_for_plane(plane_state, fb ? crtc : NULL);
10566 if (ret)
10567 return ret;
10568 drm_atomic_set_fb_for_plane(plane_state, fb);
10569 plane_state->crtc_x = 0;
10570 plane_state->crtc_y = 0;
10571 plane_state->crtc_w = hdisplay;
10572 plane_state->crtc_h = vdisplay;
10573 plane_state->src_x = x << 16;
10574 plane_state->src_y = y << 16;
10575 plane_state->src_w = hdisplay << 16;
10576 plane_state->src_h = vdisplay << 16;
10577
10578 return 0;
10579}
10580
d2434ab7 10581bool intel_get_load_detect_pipe(struct drm_connector *connector,
7173188d 10582 struct drm_display_mode *mode,
51fd371b
RC
10583 struct intel_load_detect_pipe *old,
10584 struct drm_modeset_acquire_ctx *ctx)
79e53945
JB
10585{
10586 struct intel_crtc *intel_crtc;
d2434ab7
DV
10587 struct intel_encoder *intel_encoder =
10588 intel_attached_encoder(connector);
79e53945 10589 struct drm_crtc *possible_crtc;
4ef69c7a 10590 struct drm_encoder *encoder = &intel_encoder->base;
79e53945
JB
10591 struct drm_crtc *crtc = NULL;
10592 struct drm_device *dev = encoder->dev;
94352cf9 10593 struct drm_framebuffer *fb;
51fd371b 10594 struct drm_mode_config *config = &dev->mode_config;
edde3617 10595 struct drm_atomic_state *state = NULL, *restore_state = NULL;
944b0c76 10596 struct drm_connector_state *connector_state;
4be07317 10597 struct intel_crtc_state *crtc_state;
51fd371b 10598 int ret, i = -1;
79e53945 10599
d2dff872 10600 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
c23cc417 10601 connector->base.id, connector->name,
8e329a03 10602 encoder->base.id, encoder->name);
d2dff872 10603
edde3617
ML
10604 old->restore_state = NULL;
10605
51fd371b
RC
10606retry:
10607 ret = drm_modeset_lock(&config->connection_mutex, ctx);
10608 if (ret)
ad3c558f 10609 goto fail;
6e9f798d 10610
79e53945
JB
10611 /*
10612 * Algorithm gets a little messy:
7a5e4805 10613 *
79e53945
JB
10614 * - if the connector already has an assigned crtc, use it (but make
10615 * sure it's on first)
7a5e4805 10616 *
79e53945
JB
10617 * - try to find the first unused crtc that can drive this connector,
10618 * and use that if we find one
79e53945
JB
10619 */
10620
10621 /* See if we already have a CRTC for this connector */
edde3617
ML
10622 if (connector->state->crtc) {
10623 crtc = connector->state->crtc;
8261b191 10624
51fd371b 10625 ret = drm_modeset_lock(&crtc->mutex, ctx);
4d02e2de 10626 if (ret)
ad3c558f 10627 goto fail;
8261b191
CW
10628
10629 /* Make sure the crtc and connector are running */
edde3617 10630 goto found;
79e53945
JB
10631 }
10632
10633 /* Find an unused one (if possible) */
70e1e0ec 10634 for_each_crtc(dev, possible_crtc) {
79e53945
JB
10635 i++;
10636 if (!(encoder->possible_crtcs & (1 << i)))
10637 continue;
edde3617
ML
10638
10639 ret = drm_modeset_lock(&possible_crtc->mutex, ctx);
10640 if (ret)
10641 goto fail;
10642
10643 if (possible_crtc->state->enable) {
10644 drm_modeset_unlock(&possible_crtc->mutex);
a459249c 10645 continue;
edde3617 10646 }
a459249c
VS
10647
10648 crtc = possible_crtc;
10649 break;
79e53945
JB
10650 }
10651
10652 /*
10653 * If we didn't find an unused CRTC, don't use any.
10654 */
10655 if (!crtc) {
7173188d 10656 DRM_DEBUG_KMS("no pipe available for load-detect\n");
ad3c558f 10657 goto fail;
79e53945
JB
10658 }
10659
edde3617
ML
10660found:
10661 intel_crtc = to_intel_crtc(crtc);
10662
4d02e2de
DV
10663 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
10664 if (ret)
ad3c558f 10665 goto fail;
79e53945 10666
83a57153 10667 state = drm_atomic_state_alloc(dev);
edde3617
ML
10668 restore_state = drm_atomic_state_alloc(dev);
10669 if (!state || !restore_state) {
10670 ret = -ENOMEM;
10671 goto fail;
10672 }
83a57153
ACO
10673
10674 state->acquire_ctx = ctx;
edde3617 10675 restore_state->acquire_ctx = ctx;
83a57153 10676
944b0c76
ACO
10677 connector_state = drm_atomic_get_connector_state(state, connector);
10678 if (IS_ERR(connector_state)) {
10679 ret = PTR_ERR(connector_state);
10680 goto fail;
10681 }
10682
edde3617
ML
10683 ret = drm_atomic_set_crtc_for_connector(connector_state, crtc);
10684 if (ret)
10685 goto fail;
944b0c76 10686
4be07317
ACO
10687 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
10688 if (IS_ERR(crtc_state)) {
10689 ret = PTR_ERR(crtc_state);
10690 goto fail;
10691 }
10692
49d6fa21 10693 crtc_state->base.active = crtc_state->base.enable = true;
4be07317 10694
6492711d
CW
10695 if (!mode)
10696 mode = &load_detect_mode;
79e53945 10697
d2dff872
CW
10698 /* We need a framebuffer large enough to accommodate all accesses
10699 * that the plane may generate whilst we perform load detection.
10700 * We can not rely on the fbcon either being present (we get called
10701 * during its initialisation to detect all boot displays, or it may
10702 * not even exist) or that it is large enough to satisfy the
10703 * requested mode.
10704 */
94352cf9
DV
10705 fb = mode_fits_in_fbdev(dev, mode);
10706 if (fb == NULL) {
d2dff872 10707 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
94352cf9 10708 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
d2dff872
CW
10709 } else
10710 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
94352cf9 10711 if (IS_ERR(fb)) {
d2dff872 10712 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
412b61d8 10713 goto fail;
79e53945 10714 }
79e53945 10715
d3a40d1b
ACO
10716 ret = intel_modeset_setup_plane_state(state, crtc, mode, fb, 0, 0);
10717 if (ret)
10718 goto fail;
10719
edde3617
ML
10720 drm_framebuffer_unreference(fb);
10721
10722 ret = drm_atomic_set_mode_for_crtc(&crtc_state->base, mode);
10723 if (ret)
10724 goto fail;
10725
10726 ret = PTR_ERR_OR_ZERO(drm_atomic_get_connector_state(restore_state, connector));
10727 if (!ret)
10728 ret = PTR_ERR_OR_ZERO(drm_atomic_get_crtc_state(restore_state, crtc));
10729 if (!ret)
10730 ret = PTR_ERR_OR_ZERO(drm_atomic_get_plane_state(restore_state, crtc->primary));
10731 if (ret) {
10732 DRM_DEBUG_KMS("Failed to create a copy of old state to restore: %i\n", ret);
10733 goto fail;
10734 }
8c7b5ccb 10735
3ba86073
ML
10736 ret = drm_atomic_commit(state);
10737 if (ret) {
6492711d 10738 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
412b61d8 10739 goto fail;
79e53945 10740 }
edde3617
ML
10741
10742 old->restore_state = restore_state;
7173188d 10743
79e53945 10744 /* let the connector get through one full cycle before testing */
9d0498a2 10745 intel_wait_for_vblank(dev, intel_crtc->pipe);
7173188d 10746 return true;
412b61d8 10747
ad3c558f 10748fail:
e5d958ef 10749 drm_atomic_state_free(state);
edde3617
ML
10750 drm_atomic_state_free(restore_state);
10751 restore_state = state = NULL;
83a57153 10752
51fd371b
RC
10753 if (ret == -EDEADLK) {
10754 drm_modeset_backoff(ctx);
10755 goto retry;
10756 }
10757
412b61d8 10758 return false;
79e53945
JB
10759}
10760
d2434ab7 10761void intel_release_load_detect_pipe(struct drm_connector *connector,
49172fee
ACO
10762 struct intel_load_detect_pipe *old,
10763 struct drm_modeset_acquire_ctx *ctx)
79e53945 10764{
d2434ab7
DV
10765 struct intel_encoder *intel_encoder =
10766 intel_attached_encoder(connector);
4ef69c7a 10767 struct drm_encoder *encoder = &intel_encoder->base;
edde3617 10768 struct drm_atomic_state *state = old->restore_state;
d3a40d1b 10769 int ret;
79e53945 10770
d2dff872 10771 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
c23cc417 10772 connector->base.id, connector->name,
8e329a03 10773 encoder->base.id, encoder->name);
d2dff872 10774
edde3617 10775 if (!state)
0622a53c 10776 return;
79e53945 10777
edde3617
ML
10778 ret = drm_atomic_commit(state);
10779 if (ret) {
10780 DRM_DEBUG_KMS("Couldn't release load detect pipe: %i\n", ret);
10781 drm_atomic_state_free(state);
10782 }
79e53945
JB
10783}
10784
da4a1efa 10785static int i9xx_pll_refclk(struct drm_device *dev,
5cec258b 10786 const struct intel_crtc_state *pipe_config)
da4a1efa
VS
10787{
10788 struct drm_i915_private *dev_priv = dev->dev_private;
10789 u32 dpll = pipe_config->dpll_hw_state.dpll;
10790
10791 if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
e91e941b 10792 return dev_priv->vbt.lvds_ssc_freq;
da4a1efa
VS
10793 else if (HAS_PCH_SPLIT(dev))
10794 return 120000;
10795 else if (!IS_GEN2(dev))
10796 return 96000;
10797 else
10798 return 48000;
10799}
10800
79e53945 10801/* Returns the clock of the currently programmed mode of the given pipe. */
f1f644dc 10802static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 10803 struct intel_crtc_state *pipe_config)
79e53945 10804{
f1f644dc 10805 struct drm_device *dev = crtc->base.dev;
79e53945 10806 struct drm_i915_private *dev_priv = dev->dev_private;
f1f644dc 10807 int pipe = pipe_config->cpu_transcoder;
293623f7 10808 u32 dpll = pipe_config->dpll_hw_state.dpll;
79e53945 10809 u32 fp;
9e2c8475 10810 struct dpll clock;
dccbea3b 10811 int port_clock;
da4a1efa 10812 int refclk = i9xx_pll_refclk(dev, pipe_config);
79e53945
JB
10813
10814 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
293623f7 10815 fp = pipe_config->dpll_hw_state.fp0;
79e53945 10816 else
293623f7 10817 fp = pipe_config->dpll_hw_state.fp1;
79e53945
JB
10818
10819 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
f2b115e6
AJ
10820 if (IS_PINEVIEW(dev)) {
10821 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
10822 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
2177832f
SL
10823 } else {
10824 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
10825 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
10826 }
10827
a6c45cf0 10828 if (!IS_GEN2(dev)) {
f2b115e6
AJ
10829 if (IS_PINEVIEW(dev))
10830 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
10831 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
2177832f
SL
10832 else
10833 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
79e53945
JB
10834 DPLL_FPA01_P1_POST_DIV_SHIFT);
10835
10836 switch (dpll & DPLL_MODE_MASK) {
10837 case DPLLB_MODE_DAC_SERIAL:
10838 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
10839 5 : 10;
10840 break;
10841 case DPLLB_MODE_LVDS:
10842 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
10843 7 : 14;
10844 break;
10845 default:
28c97730 10846 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
79e53945 10847 "mode\n", (int)(dpll & DPLL_MODE_MASK));
f1f644dc 10848 return;
79e53945
JB
10849 }
10850
ac58c3f0 10851 if (IS_PINEVIEW(dev))
dccbea3b 10852 port_clock = pnv_calc_dpll_params(refclk, &clock);
ac58c3f0 10853 else
dccbea3b 10854 port_clock = i9xx_calc_dpll_params(refclk, &clock);
79e53945 10855 } else {
0fb58223 10856 u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
b1c560d1 10857 bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
79e53945
JB
10858
10859 if (is_lvds) {
10860 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
10861 DPLL_FPA01_P1_POST_DIV_SHIFT);
b1c560d1
VS
10862
10863 if (lvds & LVDS_CLKB_POWER_UP)
10864 clock.p2 = 7;
10865 else
10866 clock.p2 = 14;
79e53945
JB
10867 } else {
10868 if (dpll & PLL_P1_DIVIDE_BY_TWO)
10869 clock.p1 = 2;
10870 else {
10871 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
10872 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
10873 }
10874 if (dpll & PLL_P2_DIVIDE_BY_4)
10875 clock.p2 = 4;
10876 else
10877 clock.p2 = 2;
79e53945 10878 }
da4a1efa 10879
dccbea3b 10880 port_clock = i9xx_calc_dpll_params(refclk, &clock);
79e53945
JB
10881 }
10882
18442d08
VS
10883 /*
10884 * This value includes pixel_multiplier. We will use
241bfc38 10885 * port_clock to compute adjusted_mode.crtc_clock in the
18442d08
VS
10886 * encoder's get_config() function.
10887 */
dccbea3b 10888 pipe_config->port_clock = port_clock;
f1f644dc
JB
10889}
10890
6878da05
VS
10891int intel_dotclock_calculate(int link_freq,
10892 const struct intel_link_m_n *m_n)
f1f644dc 10893{
f1f644dc
JB
10894 /*
10895 * The calculation for the data clock is:
1041a02f 10896 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
f1f644dc 10897 * But we want to avoid losing precison if possible, so:
1041a02f 10898 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
f1f644dc
JB
10899 *
10900 * and the link clock is simpler:
1041a02f 10901 * link_clock = (m * link_clock) / n
f1f644dc
JB
10902 */
10903
6878da05
VS
10904 if (!m_n->link_n)
10905 return 0;
f1f644dc 10906
6878da05
VS
10907 return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
10908}
f1f644dc 10909
18442d08 10910static void ironlake_pch_clock_get(struct intel_crtc *crtc,
5cec258b 10911 struct intel_crtc_state *pipe_config)
6878da05 10912{
e3b247da 10913 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
79e53945 10914
18442d08
VS
10915 /* read out port_clock from the DPLL */
10916 i9xx_crtc_clock_get(crtc, pipe_config);
f1f644dc 10917
f1f644dc 10918 /*
e3b247da
VS
10919 * In case there is an active pipe without active ports,
10920 * we may need some idea for the dotclock anyway.
10921 * Calculate one based on the FDI configuration.
79e53945 10922 */
2d112de7 10923 pipe_config->base.adjusted_mode.crtc_clock =
21a727b3 10924 intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
18442d08 10925 &pipe_config->fdi_m_n);
79e53945
JB
10926}
10927
10928/** Returns the currently programmed mode of the given pipe. */
10929struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
10930 struct drm_crtc *crtc)
10931{
548f245b 10932 struct drm_i915_private *dev_priv = dev->dev_private;
79e53945 10933 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6e3c9717 10934 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
79e53945 10935 struct drm_display_mode *mode;
3f36b937 10936 struct intel_crtc_state *pipe_config;
fe2b8f9d
PZ
10937 int htot = I915_READ(HTOTAL(cpu_transcoder));
10938 int hsync = I915_READ(HSYNC(cpu_transcoder));
10939 int vtot = I915_READ(VTOTAL(cpu_transcoder));
10940 int vsync = I915_READ(VSYNC(cpu_transcoder));
293623f7 10941 enum pipe pipe = intel_crtc->pipe;
79e53945
JB
10942
10943 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
10944 if (!mode)
10945 return NULL;
10946
3f36b937
TU
10947 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
10948 if (!pipe_config) {
10949 kfree(mode);
10950 return NULL;
10951 }
10952
f1f644dc
JB
10953 /*
10954 * Construct a pipe_config sufficient for getting the clock info
10955 * back out of crtc_clock_get.
10956 *
10957 * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
10958 * to use a real value here instead.
10959 */
3f36b937
TU
10960 pipe_config->cpu_transcoder = (enum transcoder) pipe;
10961 pipe_config->pixel_multiplier = 1;
10962 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(pipe));
10963 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(pipe));
10964 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(pipe));
10965 i9xx_crtc_clock_get(intel_crtc, pipe_config);
10966
10967 mode->clock = pipe_config->port_clock / pipe_config->pixel_multiplier;
79e53945
JB
10968 mode->hdisplay = (htot & 0xffff) + 1;
10969 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
10970 mode->hsync_start = (hsync & 0xffff) + 1;
10971 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
10972 mode->vdisplay = (vtot & 0xffff) + 1;
10973 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
10974 mode->vsync_start = (vsync & 0xffff) + 1;
10975 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
10976
10977 drm_mode_set_name(mode);
79e53945 10978
3f36b937
TU
10979 kfree(pipe_config);
10980
79e53945
JB
10981 return mode;
10982}
10983
7d993739 10984void intel_mark_busy(struct drm_i915_private *dev_priv)
f047e395 10985{
f62a0076
CW
10986 if (dev_priv->mm.busy)
10987 return;
10988
43694d69 10989 intel_runtime_pm_get(dev_priv);
c67a470b 10990 i915_update_gfx_val(dev_priv);
7d993739 10991 if (INTEL_GEN(dev_priv) >= 6)
43cf3bf0 10992 gen6_rps_busy(dev_priv);
f62a0076 10993 dev_priv->mm.busy = true;
f047e395
CW
10994}
10995
7d993739 10996void intel_mark_idle(struct drm_i915_private *dev_priv)
652c393a 10997{
f62a0076
CW
10998 if (!dev_priv->mm.busy)
10999 return;
11000
11001 dev_priv->mm.busy = false;
11002
7d993739
TU
11003 if (INTEL_GEN(dev_priv) >= 6)
11004 gen6_rps_idle(dev_priv);
bb4cdd53 11005
43694d69 11006 intel_runtime_pm_put(dev_priv);
652c393a
JB
11007}
11008
79e53945
JB
11009static void intel_crtc_destroy(struct drm_crtc *crtc)
11010{
11011 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
67e77c5a 11012 struct drm_device *dev = crtc->dev;
51cbaf01 11013 struct intel_flip_work *work;
67e77c5a 11014
5e2d7afc 11015 spin_lock_irq(&dev->event_lock);
5a21b665
DV
11016 work = intel_crtc->flip_work;
11017 intel_crtc->flip_work = NULL;
11018 spin_unlock_irq(&dev->event_lock);
67e77c5a 11019
5a21b665 11020 if (work) {
51cbaf01
ML
11021 cancel_work_sync(&work->mmio_work);
11022 cancel_work_sync(&work->unpin_work);
5a21b665 11023 kfree(work);
67e77c5a 11024 }
79e53945
JB
11025
11026 drm_crtc_cleanup(crtc);
67e77c5a 11027
79e53945
JB
11028 kfree(intel_crtc);
11029}
11030
6b95a207
KH
11031static void intel_unpin_work_fn(struct work_struct *__work)
11032{
51cbaf01
ML
11033 struct intel_flip_work *work =
11034 container_of(__work, struct intel_flip_work, unpin_work);
5a21b665
DV
11035 struct intel_crtc *crtc = to_intel_crtc(work->crtc);
11036 struct drm_device *dev = crtc->base.dev;
11037 struct drm_plane *primary = crtc->base.primary;
03f476e1 11038
5a21b665
DV
11039 if (is_mmio_work(work))
11040 flush_work(&work->mmio_work);
03f476e1 11041
5a21b665
DV
11042 mutex_lock(&dev->struct_mutex);
11043 intel_unpin_fb_obj(work->old_fb, primary->state->rotation);
11044 drm_gem_object_unreference(&work->pending_flip_obj->base);
143f73b3 11045
5a21b665
DV
11046 if (work->flip_queued_req)
11047 i915_gem_request_assign(&work->flip_queued_req, NULL);
11048 mutex_unlock(&dev->struct_mutex);
143f73b3 11049
5a21b665
DV
11050 intel_frontbuffer_flip_complete(dev, to_intel_plane(primary)->frontbuffer_bit);
11051 intel_fbc_post_update(crtc);
11052 drm_framebuffer_unreference(work->old_fb);
143f73b3 11053
5a21b665
DV
11054 BUG_ON(atomic_read(&crtc->unpin_work_count) == 0);
11055 atomic_dec(&crtc->unpin_work_count);
a6747b73 11056
5a21b665
DV
11057 kfree(work);
11058}
d9e86c0e 11059
5a21b665
DV
11060/* Is 'a' after or equal to 'b'? */
11061static bool g4x_flip_count_after_eq(u32 a, u32 b)
11062{
11063 return !((a - b) & 0x80000000);
11064}
143f73b3 11065
5a21b665
DV
11066static bool __pageflip_finished_cs(struct intel_crtc *crtc,
11067 struct intel_flip_work *work)
11068{
11069 struct drm_device *dev = crtc->base.dev;
11070 struct drm_i915_private *dev_priv = dev->dev_private;
11071 unsigned reset_counter;
143f73b3 11072
5a21b665
DV
11073 reset_counter = i915_reset_counter(&dev_priv->gpu_error);
11074 if (crtc->reset_counter != reset_counter)
11075 return true;
143f73b3 11076
5a21b665
DV
11077 /*
11078 * The relevant registers doen't exist on pre-ctg.
11079 * As the flip done interrupt doesn't trigger for mmio
11080 * flips on gmch platforms, a flip count check isn't
11081 * really needed there. But since ctg has the registers,
11082 * include it in the check anyway.
11083 */
11084 if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
11085 return true;
b4a98e57 11086
5a21b665
DV
11087 /*
11088 * BDW signals flip done immediately if the plane
11089 * is disabled, even if the plane enable is already
11090 * armed to occur at the next vblank :(
11091 */
f99d7069 11092
5a21b665
DV
11093 /*
11094 * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
11095 * used the same base address. In that case the mmio flip might
11096 * have completed, but the CS hasn't even executed the flip yet.
11097 *
11098 * A flip count check isn't enough as the CS might have updated
11099 * the base address just after start of vblank, but before we
11100 * managed to process the interrupt. This means we'd complete the
11101 * CS flip too soon.
11102 *
11103 * Combining both checks should get us a good enough result. It may
11104 * still happen that the CS flip has been executed, but has not
11105 * yet actually completed. But in case the base address is the same
11106 * anyway, we don't really care.
11107 */
11108 return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
11109 crtc->flip_work->gtt_offset &&
11110 g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_G4X(crtc->pipe)),
11111 crtc->flip_work->flip_count);
11112}
b4a98e57 11113
5a21b665
DV
11114static bool
11115__pageflip_finished_mmio(struct intel_crtc *crtc,
11116 struct intel_flip_work *work)
11117{
11118 /*
11119 * MMIO work completes when vblank is different from
11120 * flip_queued_vblank.
11121 *
11122 * Reset counter value doesn't matter, this is handled by
11123 * i915_wait_request finishing early, so no need to handle
11124 * reset here.
11125 */
11126 return intel_crtc_get_vblank_counter(crtc) != work->flip_queued_vblank;
6b95a207
KH
11127}
11128
51cbaf01
ML
11129
11130static bool pageflip_finished(struct intel_crtc *crtc,
11131 struct intel_flip_work *work)
11132{
11133 if (!atomic_read(&work->pending))
11134 return false;
11135
11136 smp_rmb();
11137
5a21b665
DV
11138 if (is_mmio_work(work))
11139 return __pageflip_finished_mmio(crtc, work);
11140 else
11141 return __pageflip_finished_cs(crtc, work);
11142}
11143
11144void intel_finish_page_flip_cs(struct drm_i915_private *dev_priv, int pipe)
11145{
11146 struct drm_device *dev = dev_priv->dev;
11147 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
11148 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11149 struct intel_flip_work *work;
11150 unsigned long flags;
11151
11152 /* Ignore early vblank irqs */
11153 if (!crtc)
11154 return;
11155
51cbaf01 11156 /*
5a21b665
DV
11157 * This is called both by irq handlers and the reset code (to complete
11158 * lost pageflips) so needs the full irqsave spinlocks.
51cbaf01 11159 */
5a21b665
DV
11160 spin_lock_irqsave(&dev->event_lock, flags);
11161 work = intel_crtc->flip_work;
11162
11163 if (work != NULL &&
11164 !is_mmio_work(work) &&
11165 pageflip_finished(intel_crtc, work))
11166 page_flip_completed(intel_crtc);
11167
11168 spin_unlock_irqrestore(&dev->event_lock, flags);
75f7f3ec
VS
11169}
11170
51cbaf01 11171void intel_finish_page_flip_mmio(struct drm_i915_private *dev_priv, int pipe)
6b95a207 11172{
91d14251 11173 struct drm_device *dev = dev_priv->dev;
5251f04e
ML
11174 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
11175 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
51cbaf01 11176 struct intel_flip_work *work;
6b95a207
KH
11177 unsigned long flags;
11178
5251f04e
ML
11179 /* Ignore early vblank irqs */
11180 if (!crtc)
11181 return;
f326038a
DV
11182
11183 /*
11184 * This is called both by irq handlers and the reset code (to complete
11185 * lost pageflips) so needs the full irqsave spinlocks.
e7d841ca 11186 */
6b95a207 11187 spin_lock_irqsave(&dev->event_lock, flags);
5a21b665 11188 work = intel_crtc->flip_work;
5251f04e 11189
5a21b665
DV
11190 if (work != NULL &&
11191 is_mmio_work(work) &&
11192 pageflip_finished(intel_crtc, work))
11193 page_flip_completed(intel_crtc);
5251f04e 11194
6b95a207
KH
11195 spin_unlock_irqrestore(&dev->event_lock, flags);
11196}
11197
5a21b665
DV
11198static inline void intel_mark_page_flip_active(struct intel_crtc *crtc,
11199 struct intel_flip_work *work)
84c33a64 11200{
5a21b665 11201 work->flip_queued_vblank = intel_crtc_get_vblank_counter(crtc);
84c33a64 11202
5a21b665
DV
11203 /* Ensure that the work item is consistent when activating it ... */
11204 smp_mb__before_atomic();
11205 atomic_set(&work->pending, 1);
11206}
a6747b73 11207
5a21b665
DV
11208static int intel_gen2_queue_flip(struct drm_device *dev,
11209 struct drm_crtc *crtc,
11210 struct drm_framebuffer *fb,
11211 struct drm_i915_gem_object *obj,
11212 struct drm_i915_gem_request *req,
11213 uint32_t flags)
11214{
11215 struct intel_engine_cs *engine = req->engine;
11216 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11217 u32 flip_mask;
11218 int ret;
143f73b3 11219
5a21b665
DV
11220 ret = intel_ring_begin(req, 6);
11221 if (ret)
11222 return ret;
143f73b3 11223
5a21b665
DV
11224 /* Can't queue multiple flips, so wait for the previous
11225 * one to finish before executing the next.
11226 */
11227 if (intel_crtc->plane)
11228 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
11229 else
11230 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
11231 intel_ring_emit(engine, MI_WAIT_FOR_EVENT | flip_mask);
11232 intel_ring_emit(engine, MI_NOOP);
11233 intel_ring_emit(engine, MI_DISPLAY_FLIP |
11234 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
11235 intel_ring_emit(engine, fb->pitches[0]);
11236 intel_ring_emit(engine, intel_crtc->flip_work->gtt_offset);
11237 intel_ring_emit(engine, 0); /* aux display base address, unused */
143f73b3 11238
5a21b665
DV
11239 return 0;
11240}
84c33a64 11241
5a21b665
DV
11242static int intel_gen3_queue_flip(struct drm_device *dev,
11243 struct drm_crtc *crtc,
11244 struct drm_framebuffer *fb,
11245 struct drm_i915_gem_object *obj,
11246 struct drm_i915_gem_request *req,
11247 uint32_t flags)
11248{
11249 struct intel_engine_cs *engine = req->engine;
11250 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11251 u32 flip_mask;
11252 int ret;
d55dbd06 11253
5a21b665
DV
11254 ret = intel_ring_begin(req, 6);
11255 if (ret)
11256 return ret;
d55dbd06 11257
5a21b665
DV
11258 if (intel_crtc->plane)
11259 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
11260 else
11261 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
11262 intel_ring_emit(engine, MI_WAIT_FOR_EVENT | flip_mask);
11263 intel_ring_emit(engine, MI_NOOP);
11264 intel_ring_emit(engine, MI_DISPLAY_FLIP_I915 |
11265 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
11266 intel_ring_emit(engine, fb->pitches[0]);
11267 intel_ring_emit(engine, intel_crtc->flip_work->gtt_offset);
11268 intel_ring_emit(engine, MI_NOOP);
fd8e058a 11269
5a21b665
DV
11270 return 0;
11271}
84c33a64 11272
5a21b665
DV
11273static int intel_gen4_queue_flip(struct drm_device *dev,
11274 struct drm_crtc *crtc,
11275 struct drm_framebuffer *fb,
11276 struct drm_i915_gem_object *obj,
11277 struct drm_i915_gem_request *req,
11278 uint32_t flags)
11279{
11280 struct intel_engine_cs *engine = req->engine;
11281 struct drm_i915_private *dev_priv = dev->dev_private;
11282 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11283 uint32_t pf, pipesrc;
11284 int ret;
143f73b3 11285
5a21b665
DV
11286 ret = intel_ring_begin(req, 4);
11287 if (ret)
11288 return ret;
143f73b3 11289
5a21b665
DV
11290 /* i965+ uses the linear or tiled offsets from the
11291 * Display Registers (which do not change across a page-flip)
11292 * so we need only reprogram the base address.
11293 */
11294 intel_ring_emit(engine, MI_DISPLAY_FLIP |
11295 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
11296 intel_ring_emit(engine, fb->pitches[0]);
11297 intel_ring_emit(engine, intel_crtc->flip_work->gtt_offset |
11298 obj->tiling_mode);
11299
11300 /* XXX Enabling the panel-fitter across page-flip is so far
11301 * untested on non-native modes, so ignore it for now.
11302 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
11303 */
11304 pf = 0;
11305 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
11306 intel_ring_emit(engine, pf | pipesrc);
143f73b3 11307
5a21b665 11308 return 0;
8c9f3aaf
JB
11309}
11310
5a21b665
DV
11311static int intel_gen6_queue_flip(struct drm_device *dev,
11312 struct drm_crtc *crtc,
11313 struct drm_framebuffer *fb,
11314 struct drm_i915_gem_object *obj,
11315 struct drm_i915_gem_request *req,
11316 uint32_t flags)
da20eabd 11317{
5a21b665
DV
11318 struct intel_engine_cs *engine = req->engine;
11319 struct drm_i915_private *dev_priv = dev->dev_private;
11320 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11321 uint32_t pf, pipesrc;
11322 int ret;
d21fbe87 11323
5a21b665
DV
11324 ret = intel_ring_begin(req, 4);
11325 if (ret)
11326 return ret;
92826fcd 11327
5a21b665
DV
11328 intel_ring_emit(engine, MI_DISPLAY_FLIP |
11329 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
11330 intel_ring_emit(engine, fb->pitches[0] | obj->tiling_mode);
11331 intel_ring_emit(engine, intel_crtc->flip_work->gtt_offset);
92826fcd 11332
5a21b665
DV
11333 /* Contrary to the suggestions in the documentation,
11334 * "Enable Panel Fitter" does not seem to be required when page
11335 * flipping with a non-native mode, and worse causes a normal
11336 * modeset to fail.
11337 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
11338 */
11339 pf = 0;
11340 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
11341 intel_ring_emit(engine, pf | pipesrc);
7809e5ae 11342
5a21b665 11343 return 0;
7809e5ae
MR
11344}
11345
5a21b665
DV
11346static int intel_gen7_queue_flip(struct drm_device *dev,
11347 struct drm_crtc *crtc,
11348 struct drm_framebuffer *fb,
11349 struct drm_i915_gem_object *obj,
11350 struct drm_i915_gem_request *req,
11351 uint32_t flags)
d21fbe87 11352{
5a21b665
DV
11353 struct intel_engine_cs *engine = req->engine;
11354 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11355 uint32_t plane_bit = 0;
11356 int len, ret;
d21fbe87 11357
5a21b665
DV
11358 switch (intel_crtc->plane) {
11359 case PLANE_A:
11360 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
11361 break;
11362 case PLANE_B:
11363 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
11364 break;
11365 case PLANE_C:
11366 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
11367 break;
11368 default:
11369 WARN_ONCE(1, "unknown plane in flip command\n");
11370 return -ENODEV;
11371 }
11372
11373 len = 4;
11374 if (engine->id == RCS) {
11375 len += 6;
11376 /*
11377 * On Gen 8, SRM is now taking an extra dword to accommodate
11378 * 48bits addresses, and we need a NOOP for the batch size to
11379 * stay even.
11380 */
11381 if (IS_GEN8(dev))
11382 len += 2;
11383 }
11384
11385 /*
11386 * BSpec MI_DISPLAY_FLIP for IVB:
11387 * "The full packet must be contained within the same cache line."
11388 *
11389 * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
11390 * cacheline, if we ever start emitting more commands before
11391 * the MI_DISPLAY_FLIP we may need to first emit everything else,
11392 * then do the cacheline alignment, and finally emit the
11393 * MI_DISPLAY_FLIP.
11394 */
11395 ret = intel_ring_cacheline_align(req);
11396 if (ret)
11397 return ret;
11398
11399 ret = intel_ring_begin(req, len);
11400 if (ret)
11401 return ret;
11402
11403 /* Unmask the flip-done completion message. Note that the bspec says that
11404 * we should do this for both the BCS and RCS, and that we must not unmask
11405 * more than one flip event at any time (or ensure that one flip message
11406 * can be sent by waiting for flip-done prior to queueing new flips).
11407 * Experimentation says that BCS works despite DERRMR masking all
11408 * flip-done completion events and that unmasking all planes at once
11409 * for the RCS also doesn't appear to drop events. Setting the DERRMR
11410 * to zero does lead to lockups within MI_DISPLAY_FLIP.
11411 */
11412 if (engine->id == RCS) {
11413 intel_ring_emit(engine, MI_LOAD_REGISTER_IMM(1));
11414 intel_ring_emit_reg(engine, DERRMR);
11415 intel_ring_emit(engine, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
11416 DERRMR_PIPEB_PRI_FLIP_DONE |
11417 DERRMR_PIPEC_PRI_FLIP_DONE));
11418 if (IS_GEN8(dev))
11419 intel_ring_emit(engine, MI_STORE_REGISTER_MEM_GEN8 |
11420 MI_SRM_LRM_GLOBAL_GTT);
11421 else
11422 intel_ring_emit(engine, MI_STORE_REGISTER_MEM |
11423 MI_SRM_LRM_GLOBAL_GTT);
11424 intel_ring_emit_reg(engine, DERRMR);
11425 intel_ring_emit(engine, engine->scratch.gtt_offset + 256);
11426 if (IS_GEN8(dev)) {
11427 intel_ring_emit(engine, 0);
11428 intel_ring_emit(engine, MI_NOOP);
11429 }
11430 }
11431
11432 intel_ring_emit(engine, MI_DISPLAY_FLIP_I915 | plane_bit);
11433 intel_ring_emit(engine, (fb->pitches[0] | obj->tiling_mode));
11434 intel_ring_emit(engine, intel_crtc->flip_work->gtt_offset);
11435 intel_ring_emit(engine, (MI_NOOP));
11436
11437 return 0;
11438}
11439
11440static bool use_mmio_flip(struct intel_engine_cs *engine,
11441 struct drm_i915_gem_object *obj)
11442{
c37efb99
CW
11443 struct reservation_object *resv;
11444
5a21b665
DV
11445 /*
11446 * This is not being used for older platforms, because
11447 * non-availability of flip done interrupt forces us to use
11448 * CS flips. Older platforms derive flip done using some clever
11449 * tricks involving the flip_pending status bits and vblank irqs.
11450 * So using MMIO flips there would disrupt this mechanism.
11451 */
11452
11453 if (engine == NULL)
11454 return true;
11455
11456 if (INTEL_GEN(engine->i915) < 5)
11457 return false;
11458
11459 if (i915.use_mmio_flip < 0)
11460 return false;
11461 else if (i915.use_mmio_flip > 0)
11462 return true;
11463 else if (i915.enable_execlists)
11464 return true;
c37efb99
CW
11465
11466 resv = i915_gem_object_get_dmabuf_resv(obj);
11467 if (resv && !reservation_object_test_signaled_rcu(resv, false))
5a21b665 11468 return true;
c37efb99
CW
11469
11470 return engine != i915_gem_request_get_engine(obj->last_write_req);
5a21b665
DV
11471}
11472
11473static void skl_do_mmio_flip(struct intel_crtc *intel_crtc,
11474 unsigned int rotation,
11475 struct intel_flip_work *work)
11476{
11477 struct drm_device *dev = intel_crtc->base.dev;
11478 struct drm_i915_private *dev_priv = dev->dev_private;
11479 struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
11480 const enum pipe pipe = intel_crtc->pipe;
11481 u32 ctl, stride, tile_height;
11482
11483 ctl = I915_READ(PLANE_CTL(pipe, 0));
11484 ctl &= ~PLANE_CTL_TILED_MASK;
11485 switch (fb->modifier[0]) {
11486 case DRM_FORMAT_MOD_NONE:
11487 break;
11488 case I915_FORMAT_MOD_X_TILED:
11489 ctl |= PLANE_CTL_TILED_X;
11490 break;
11491 case I915_FORMAT_MOD_Y_TILED:
11492 ctl |= PLANE_CTL_TILED_Y;
11493 break;
11494 case I915_FORMAT_MOD_Yf_TILED:
11495 ctl |= PLANE_CTL_TILED_YF;
11496 break;
11497 default:
11498 MISSING_CASE(fb->modifier[0]);
11499 }
11500
11501 /*
11502 * The stride is either expressed as a multiple of 64 bytes chunks for
11503 * linear buffers or in number of tiles for tiled buffers.
11504 */
11505 if (intel_rotation_90_or_270(rotation)) {
11506 /* stride = Surface height in tiles */
11507 tile_height = intel_tile_height(dev_priv, fb->modifier[0], 0);
11508 stride = DIV_ROUND_UP(fb->height, tile_height);
11509 } else {
11510 stride = fb->pitches[0] /
11511 intel_fb_stride_alignment(dev_priv, fb->modifier[0],
11512 fb->pixel_format);
11513 }
11514
11515 /*
11516 * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
11517 * PLANE_SURF updates, the update is then guaranteed to be atomic.
11518 */
11519 I915_WRITE(PLANE_CTL(pipe, 0), ctl);
11520 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
11521
11522 I915_WRITE(PLANE_SURF(pipe, 0), work->gtt_offset);
11523 POSTING_READ(PLANE_SURF(pipe, 0));
11524}
11525
11526static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc,
11527 struct intel_flip_work *work)
11528{
11529 struct drm_device *dev = intel_crtc->base.dev;
11530 struct drm_i915_private *dev_priv = dev->dev_private;
11531 struct intel_framebuffer *intel_fb =
11532 to_intel_framebuffer(intel_crtc->base.primary->fb);
11533 struct drm_i915_gem_object *obj = intel_fb->obj;
11534 i915_reg_t reg = DSPCNTR(intel_crtc->plane);
11535 u32 dspcntr;
11536
11537 dspcntr = I915_READ(reg);
11538
11539 if (obj->tiling_mode != I915_TILING_NONE)
11540 dspcntr |= DISPPLANE_TILED;
11541 else
11542 dspcntr &= ~DISPPLANE_TILED;
11543
11544 I915_WRITE(reg, dspcntr);
11545
11546 I915_WRITE(DSPSURF(intel_crtc->plane), work->gtt_offset);
11547 POSTING_READ(DSPSURF(intel_crtc->plane));
11548}
11549
11550static void intel_mmio_flip_work_func(struct work_struct *w)
11551{
11552 struct intel_flip_work *work =
11553 container_of(w, struct intel_flip_work, mmio_work);
11554 struct intel_crtc *crtc = to_intel_crtc(work->crtc);
11555 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
11556 struct intel_framebuffer *intel_fb =
11557 to_intel_framebuffer(crtc->base.primary->fb);
11558 struct drm_i915_gem_object *obj = intel_fb->obj;
c37efb99 11559 struct reservation_object *resv;
5a21b665
DV
11560
11561 if (work->flip_queued_req)
11562 WARN_ON(__i915_wait_request(work->flip_queued_req,
11563 false, NULL,
11564 &dev_priv->rps.mmioflips));
11565
11566 /* For framebuffer backed by dmabuf, wait for fence */
c37efb99
CW
11567 resv = i915_gem_object_get_dmabuf_resv(obj);
11568 if (resv)
11569 WARN_ON(reservation_object_wait_timeout_rcu(resv, false, false,
5a21b665
DV
11570 MAX_SCHEDULE_TIMEOUT) < 0);
11571
11572 intel_pipe_update_start(crtc);
11573
11574 if (INTEL_GEN(dev_priv) >= 9)
11575 skl_do_mmio_flip(crtc, work->rotation, work);
11576 else
11577 /* use_mmio_flip() retricts MMIO flips to ilk+ */
11578 ilk_do_mmio_flip(crtc, work);
11579
11580 intel_pipe_update_end(crtc, work);
11581}
11582
11583static int intel_default_queue_flip(struct drm_device *dev,
11584 struct drm_crtc *crtc,
11585 struct drm_framebuffer *fb,
11586 struct drm_i915_gem_object *obj,
11587 struct drm_i915_gem_request *req,
11588 uint32_t flags)
11589{
11590 return -ENODEV;
11591}
11592
11593static bool __pageflip_stall_check_cs(struct drm_i915_private *dev_priv,
11594 struct intel_crtc *intel_crtc,
11595 struct intel_flip_work *work)
11596{
11597 u32 addr, vblank;
11598
11599 if (!atomic_read(&work->pending))
11600 return false;
11601
11602 smp_rmb();
11603
11604 vblank = intel_crtc_get_vblank_counter(intel_crtc);
11605 if (work->flip_ready_vblank == 0) {
11606 if (work->flip_queued_req &&
11607 !i915_gem_request_completed(work->flip_queued_req, true))
11608 return false;
11609
11610 work->flip_ready_vblank = vblank;
11611 }
11612
11613 if (vblank - work->flip_ready_vblank < 3)
11614 return false;
11615
11616 /* Potential stall - if we see that the flip has happened,
11617 * assume a missed interrupt. */
11618 if (INTEL_GEN(dev_priv) >= 4)
11619 addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
11620 else
11621 addr = I915_READ(DSPADDR(intel_crtc->plane));
11622
11623 /* There is a potential issue here with a false positive after a flip
11624 * to the same address. We could address this by checking for a
11625 * non-incrementing frame counter.
11626 */
11627 return addr == work->gtt_offset;
11628}
11629
11630void intel_check_page_flip(struct drm_i915_private *dev_priv, int pipe)
11631{
11632 struct drm_device *dev = dev_priv->dev;
11633 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
11634 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11635 struct intel_flip_work *work;
11636
11637 WARN_ON(!in_interrupt());
11638
11639 if (crtc == NULL)
11640 return;
11641
11642 spin_lock(&dev->event_lock);
11643 work = intel_crtc->flip_work;
11644
11645 if (work != NULL && !is_mmio_work(work) &&
11646 __pageflip_stall_check_cs(dev_priv, intel_crtc, work)) {
11647 WARN_ONCE(1,
11648 "Kicking stuck page flip: queued at %d, now %d\n",
11649 work->flip_queued_vblank, intel_crtc_get_vblank_counter(intel_crtc));
11650 page_flip_completed(intel_crtc);
11651 work = NULL;
11652 }
11653
11654 if (work != NULL && !is_mmio_work(work) &&
11655 intel_crtc_get_vblank_counter(intel_crtc) - work->flip_queued_vblank > 1)
11656 intel_queue_rps_boost_for_request(work->flip_queued_req);
11657 spin_unlock(&dev->event_lock);
11658}
11659
11660static int intel_crtc_page_flip(struct drm_crtc *crtc,
11661 struct drm_framebuffer *fb,
11662 struct drm_pending_vblank_event *event,
11663 uint32_t page_flip_flags)
11664{
11665 struct drm_device *dev = crtc->dev;
11666 struct drm_i915_private *dev_priv = dev->dev_private;
11667 struct drm_framebuffer *old_fb = crtc->primary->fb;
11668 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
11669 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11670 struct drm_plane *primary = crtc->primary;
11671 enum pipe pipe = intel_crtc->pipe;
11672 struct intel_flip_work *work;
11673 struct intel_engine_cs *engine;
11674 bool mmio_flip;
11675 struct drm_i915_gem_request *request = NULL;
11676 int ret;
11677
11678 /*
11679 * drm_mode_page_flip_ioctl() should already catch this, but double
11680 * check to be safe. In the future we may enable pageflipping from
11681 * a disabled primary plane.
11682 */
11683 if (WARN_ON(intel_fb_obj(old_fb) == NULL))
11684 return -EBUSY;
11685
11686 /* Can't change pixel format via MI display flips. */
11687 if (fb->pixel_format != crtc->primary->fb->pixel_format)
11688 return -EINVAL;
11689
11690 /*
11691 * TILEOFF/LINOFF registers can't be changed via MI display flips.
11692 * Note that pitch changes could also affect these register.
11693 */
11694 if (INTEL_INFO(dev)->gen > 3 &&
11695 (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
11696 fb->pitches[0] != crtc->primary->fb->pitches[0]))
11697 return -EINVAL;
11698
11699 if (i915_terminally_wedged(&dev_priv->gpu_error))
11700 goto out_hang;
11701
11702 work = kzalloc(sizeof(*work), GFP_KERNEL);
11703 if (work == NULL)
11704 return -ENOMEM;
11705
11706 work->event = event;
11707 work->crtc = crtc;
11708 work->old_fb = old_fb;
11709 INIT_WORK(&work->unpin_work, intel_unpin_work_fn);
11710
11711 ret = drm_crtc_vblank_get(crtc);
11712 if (ret)
11713 goto free_work;
11714
11715 /* We borrow the event spin lock for protecting flip_work */
11716 spin_lock_irq(&dev->event_lock);
11717 if (intel_crtc->flip_work) {
11718 /* Before declaring the flip queue wedged, check if
11719 * the hardware completed the operation behind our backs.
11720 */
11721 if (pageflip_finished(intel_crtc, intel_crtc->flip_work)) {
11722 DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
11723 page_flip_completed(intel_crtc);
11724 } else {
11725 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
11726 spin_unlock_irq(&dev->event_lock);
11727
11728 drm_crtc_vblank_put(crtc);
11729 kfree(work);
11730 return -EBUSY;
11731 }
11732 }
11733 intel_crtc->flip_work = work;
11734 spin_unlock_irq(&dev->event_lock);
11735
11736 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
11737 flush_workqueue(dev_priv->wq);
11738
11739 /* Reference the objects for the scheduled work. */
11740 drm_framebuffer_reference(work->old_fb);
11741 drm_gem_object_reference(&obj->base);
11742
11743 crtc->primary->fb = fb;
11744 update_state_fb(crtc->primary);
faf68d92
ML
11745
11746 intel_fbc_pre_update(intel_crtc, intel_crtc->config,
11747 to_intel_plane_state(primary->state));
5a21b665
DV
11748
11749 work->pending_flip_obj = obj;
11750
11751 ret = i915_mutex_lock_interruptible(dev);
11752 if (ret)
11753 goto cleanup;
11754
11755 intel_crtc->reset_counter = i915_reset_counter(&dev_priv->gpu_error);
11756 if (__i915_reset_in_progress_or_wedged(intel_crtc->reset_counter)) {
11757 ret = -EIO;
11758 goto cleanup;
11759 }
11760
11761 atomic_inc(&intel_crtc->unpin_work_count);
11762
11763 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
11764 work->flip_count = I915_READ(PIPE_FLIPCOUNT_G4X(pipe)) + 1;
11765
11766 if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
11767 engine = &dev_priv->engine[BCS];
11768 if (obj->tiling_mode != intel_fb_obj(work->old_fb)->tiling_mode)
11769 /* vlv: DISPLAY_FLIP fails to change tiling */
11770 engine = NULL;
11771 } else if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
11772 engine = &dev_priv->engine[BCS];
11773 } else if (INTEL_INFO(dev)->gen >= 7) {
11774 engine = i915_gem_request_get_engine(obj->last_write_req);
11775 if (engine == NULL || engine->id != RCS)
11776 engine = &dev_priv->engine[BCS];
11777 } else {
11778 engine = &dev_priv->engine[RCS];
11779 }
11780
11781 mmio_flip = use_mmio_flip(engine, obj);
11782
11783 /* When using CS flips, we want to emit semaphores between rings.
11784 * However, when using mmio flips we will create a task to do the
11785 * synchronisation, so all we want here is to pin the framebuffer
11786 * into the display plane and skip any waits.
11787 */
11788 if (!mmio_flip) {
11789 ret = i915_gem_object_sync(obj, engine, &request);
11790 if (!ret && !request) {
11791 request = i915_gem_request_alloc(engine, NULL);
11792 ret = PTR_ERR_OR_ZERO(request);
11793 }
11794
11795 if (ret)
11796 goto cleanup_pending;
11797 }
11798
11799 ret = intel_pin_and_fence_fb_obj(fb, primary->state->rotation);
11800 if (ret)
11801 goto cleanup_pending;
11802
11803 work->gtt_offset = intel_plane_obj_offset(to_intel_plane(primary),
11804 obj, 0);
11805 work->gtt_offset += intel_crtc->dspaddr_offset;
11806 work->rotation = crtc->primary->state->rotation;
11807
11808 if (mmio_flip) {
11809 INIT_WORK(&work->mmio_work, intel_mmio_flip_work_func);
11810
11811 i915_gem_request_assign(&work->flip_queued_req,
11812 obj->last_write_req);
11813
11814 schedule_work(&work->mmio_work);
11815 } else {
11816 i915_gem_request_assign(&work->flip_queued_req, request);
11817 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, request,
11818 page_flip_flags);
11819 if (ret)
11820 goto cleanup_unpin;
11821
11822 intel_mark_page_flip_active(intel_crtc, work);
11823
11824 i915_add_request_no_flush(request);
11825 }
11826
11827 i915_gem_track_fb(intel_fb_obj(old_fb), obj,
11828 to_intel_plane(primary)->frontbuffer_bit);
11829 mutex_unlock(&dev->struct_mutex);
11830
11831 intel_frontbuffer_flip_prepare(dev,
11832 to_intel_plane(primary)->frontbuffer_bit);
11833
11834 trace_i915_flip_request(intel_crtc->plane, obj);
11835
11836 return 0;
11837
11838cleanup_unpin:
11839 intel_unpin_fb_obj(fb, crtc->primary->state->rotation);
11840cleanup_pending:
11841 if (!IS_ERR_OR_NULL(request))
11842 i915_add_request_no_flush(request);
11843 atomic_dec(&intel_crtc->unpin_work_count);
11844 mutex_unlock(&dev->struct_mutex);
11845cleanup:
11846 crtc->primary->fb = old_fb;
11847 update_state_fb(crtc->primary);
11848
11849 drm_gem_object_unreference_unlocked(&obj->base);
11850 drm_framebuffer_unreference(work->old_fb);
11851
11852 spin_lock_irq(&dev->event_lock);
11853 intel_crtc->flip_work = NULL;
11854 spin_unlock_irq(&dev->event_lock);
11855
11856 drm_crtc_vblank_put(crtc);
11857free_work:
11858 kfree(work);
11859
11860 if (ret == -EIO) {
11861 struct drm_atomic_state *state;
11862 struct drm_plane_state *plane_state;
11863
11864out_hang:
11865 state = drm_atomic_state_alloc(dev);
11866 if (!state)
11867 return -ENOMEM;
11868 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
11869
11870retry:
11871 plane_state = drm_atomic_get_plane_state(state, primary);
11872 ret = PTR_ERR_OR_ZERO(plane_state);
11873 if (!ret) {
11874 drm_atomic_set_fb_for_plane(plane_state, fb);
11875
11876 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
11877 if (!ret)
11878 ret = drm_atomic_commit(state);
11879 }
11880
11881 if (ret == -EDEADLK) {
11882 drm_modeset_backoff(state->acquire_ctx);
11883 drm_atomic_state_clear(state);
11884 goto retry;
11885 }
11886
11887 if (ret)
11888 drm_atomic_state_free(state);
11889
11890 if (ret == 0 && event) {
11891 spin_lock_irq(&dev->event_lock);
11892 drm_crtc_send_vblank_event(crtc, event);
11893 spin_unlock_irq(&dev->event_lock);
11894 }
11895 }
11896 return ret;
11897}
11898
11899
11900/**
11901 * intel_wm_need_update - Check whether watermarks need updating
11902 * @plane: drm plane
11903 * @state: new plane state
11904 *
11905 * Check current plane state versus the new one to determine whether
11906 * watermarks need to be recalculated.
11907 *
11908 * Returns true or false.
11909 */
11910static bool intel_wm_need_update(struct drm_plane *plane,
11911 struct drm_plane_state *state)
11912{
11913 struct intel_plane_state *new = to_intel_plane_state(state);
11914 struct intel_plane_state *cur = to_intel_plane_state(plane->state);
11915
11916 /* Update watermarks on tiling or size changes. */
11917 if (new->visible != cur->visible)
11918 return true;
11919
11920 if (!cur->base.fb || !new->base.fb)
11921 return false;
11922
11923 if (cur->base.fb->modifier[0] != new->base.fb->modifier[0] ||
11924 cur->base.rotation != new->base.rotation ||
11925 drm_rect_width(&new->src) != drm_rect_width(&cur->src) ||
11926 drm_rect_height(&new->src) != drm_rect_height(&cur->src) ||
11927 drm_rect_width(&new->dst) != drm_rect_width(&cur->dst) ||
11928 drm_rect_height(&new->dst) != drm_rect_height(&cur->dst))
11929 return true;
11930
11931 return false;
11932}
11933
11934static bool needs_scaling(struct intel_plane_state *state)
11935{
11936 int src_w = drm_rect_width(&state->src) >> 16;
11937 int src_h = drm_rect_height(&state->src) >> 16;
11938 int dst_w = drm_rect_width(&state->dst);
11939 int dst_h = drm_rect_height(&state->dst);
11940
11941 return (src_w != dst_w || src_h != dst_h);
11942}
d21fbe87 11943
da20eabd
ML
11944int intel_plane_atomic_calc_changes(struct drm_crtc_state *crtc_state,
11945 struct drm_plane_state *plane_state)
11946{
ab1d3a0e 11947 struct intel_crtc_state *pipe_config = to_intel_crtc_state(crtc_state);
da20eabd
ML
11948 struct drm_crtc *crtc = crtc_state->crtc;
11949 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11950 struct drm_plane *plane = plane_state->plane;
11951 struct drm_device *dev = crtc->dev;
ed4a6a7c 11952 struct drm_i915_private *dev_priv = to_i915(dev);
da20eabd
ML
11953 struct intel_plane_state *old_plane_state =
11954 to_intel_plane_state(plane->state);
da20eabd
ML
11955 bool mode_changed = needs_modeset(crtc_state);
11956 bool was_crtc_enabled = crtc->state->active;
11957 bool is_crtc_enabled = crtc_state->active;
da20eabd
ML
11958 bool turn_off, turn_on, visible, was_visible;
11959 struct drm_framebuffer *fb = plane_state->fb;
78108b7c 11960 int ret;
da20eabd
ML
11961
11962 if (crtc_state && INTEL_INFO(dev)->gen >= 9 &&
11963 plane->type != DRM_PLANE_TYPE_CURSOR) {
11964 ret = skl_update_scaler_plane(
11965 to_intel_crtc_state(crtc_state),
11966 to_intel_plane_state(plane_state));
11967 if (ret)
11968 return ret;
11969 }
11970
da20eabd
ML
11971 was_visible = old_plane_state->visible;
11972 visible = to_intel_plane_state(plane_state)->visible;
11973
11974 if (!was_crtc_enabled && WARN_ON(was_visible))
11975 was_visible = false;
11976
35c08f43
ML
11977 /*
11978 * Visibility is calculated as if the crtc was on, but
11979 * after scaler setup everything depends on it being off
11980 * when the crtc isn't active.
f818ffea
VS
11981 *
11982 * FIXME this is wrong for watermarks. Watermarks should also
11983 * be computed as if the pipe would be active. Perhaps move
11984 * per-plane wm computation to the .check_plane() hook, and
11985 * only combine the results from all planes in the current place?
35c08f43
ML
11986 */
11987 if (!is_crtc_enabled)
11988 to_intel_plane_state(plane_state)->visible = visible = false;
da20eabd
ML
11989
11990 if (!was_visible && !visible)
11991 return 0;
11992
e8861675
ML
11993 if (fb != old_plane_state->base.fb)
11994 pipe_config->fb_changed = true;
11995
da20eabd
ML
11996 turn_off = was_visible && (!visible || mode_changed);
11997 turn_on = visible && (!was_visible || mode_changed);
11998
72660ce0 11999 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] has [PLANE:%d:%s] with fb %i\n",
78108b7c
VS
12000 intel_crtc->base.base.id,
12001 intel_crtc->base.name,
72660ce0
VS
12002 plane->base.id, plane->name,
12003 fb ? fb->base.id : -1);
da20eabd 12004
72660ce0
VS
12005 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] visible %i -> %i, off %i, on %i, ms %i\n",
12006 plane->base.id, plane->name,
12007 was_visible, visible,
da20eabd
ML
12008 turn_off, turn_on, mode_changed);
12009
caed361d
VS
12010 if (turn_on) {
12011 pipe_config->update_wm_pre = true;
12012
12013 /* must disable cxsr around plane enable/disable */
12014 if (plane->type != DRM_PLANE_TYPE_CURSOR)
12015 pipe_config->disable_cxsr = true;
12016 } else if (turn_off) {
12017 pipe_config->update_wm_post = true;
92826fcd 12018
852eb00d 12019 /* must disable cxsr around plane enable/disable */
e8861675 12020 if (plane->type != DRM_PLANE_TYPE_CURSOR)
ab1d3a0e 12021 pipe_config->disable_cxsr = true;
852eb00d 12022 } else if (intel_wm_need_update(plane, plane_state)) {
caed361d
VS
12023 /* FIXME bollocks */
12024 pipe_config->update_wm_pre = true;
12025 pipe_config->update_wm_post = true;
852eb00d 12026 }
da20eabd 12027
ed4a6a7c 12028 /* Pre-gen9 platforms need two-step watermark updates */
caed361d
VS
12029 if ((pipe_config->update_wm_pre || pipe_config->update_wm_post) &&
12030 INTEL_INFO(dev)->gen < 9 && dev_priv->display.optimize_watermarks)
ed4a6a7c
MR
12031 to_intel_crtc_state(crtc_state)->wm.need_postvbl_update = true;
12032
8be6ca85 12033 if (visible || was_visible)
cd202f69 12034 pipe_config->fb_bits |= to_intel_plane(plane)->frontbuffer_bit;
a9ff8714 12035
31ae71fc
ML
12036 /*
12037 * WaCxSRDisabledForSpriteScaling:ivb
12038 *
12039 * cstate->update_wm was already set above, so this flag will
12040 * take effect when we commit and program watermarks.
12041 */
12042 if (plane->type == DRM_PLANE_TYPE_OVERLAY && IS_IVYBRIDGE(dev) &&
12043 needs_scaling(to_intel_plane_state(plane_state)) &&
12044 !needs_scaling(old_plane_state))
12045 pipe_config->disable_lp_wm = true;
d21fbe87 12046
da20eabd
ML
12047 return 0;
12048}
12049
6d3a1ce7
ML
12050static bool encoders_cloneable(const struct intel_encoder *a,
12051 const struct intel_encoder *b)
12052{
12053 /* masks could be asymmetric, so check both ways */
12054 return a == b || (a->cloneable & (1 << b->type) &&
12055 b->cloneable & (1 << a->type));
12056}
12057
12058static bool check_single_encoder_cloning(struct drm_atomic_state *state,
12059 struct intel_crtc *crtc,
12060 struct intel_encoder *encoder)
12061{
12062 struct intel_encoder *source_encoder;
12063 struct drm_connector *connector;
12064 struct drm_connector_state *connector_state;
12065 int i;
12066
12067 for_each_connector_in_state(state, connector, connector_state, i) {
12068 if (connector_state->crtc != &crtc->base)
12069 continue;
12070
12071 source_encoder =
12072 to_intel_encoder(connector_state->best_encoder);
12073 if (!encoders_cloneable(encoder, source_encoder))
12074 return false;
12075 }
12076
12077 return true;
12078}
12079
12080static bool check_encoder_cloning(struct drm_atomic_state *state,
12081 struct intel_crtc *crtc)
12082{
12083 struct intel_encoder *encoder;
12084 struct drm_connector *connector;
12085 struct drm_connector_state *connector_state;
12086 int i;
12087
12088 for_each_connector_in_state(state, connector, connector_state, i) {
12089 if (connector_state->crtc != &crtc->base)
12090 continue;
12091
12092 encoder = to_intel_encoder(connector_state->best_encoder);
12093 if (!check_single_encoder_cloning(state, crtc, encoder))
12094 return false;
12095 }
12096
12097 return true;
12098}
12099
12100static int intel_crtc_atomic_check(struct drm_crtc *crtc,
12101 struct drm_crtc_state *crtc_state)
12102{
cf5a15be 12103 struct drm_device *dev = crtc->dev;
ad421372 12104 struct drm_i915_private *dev_priv = dev->dev_private;
6d3a1ce7 12105 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
cf5a15be
ML
12106 struct intel_crtc_state *pipe_config =
12107 to_intel_crtc_state(crtc_state);
6d3a1ce7 12108 struct drm_atomic_state *state = crtc_state->state;
4d20cd86 12109 int ret;
6d3a1ce7
ML
12110 bool mode_changed = needs_modeset(crtc_state);
12111
12112 if (mode_changed && !check_encoder_cloning(state, intel_crtc)) {
12113 DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
12114 return -EINVAL;
12115 }
12116
852eb00d 12117 if (mode_changed && !crtc_state->active)
caed361d 12118 pipe_config->update_wm_post = true;
eddfcbcd 12119
ad421372
ML
12120 if (mode_changed && crtc_state->enable &&
12121 dev_priv->display.crtc_compute_clock &&
8106ddbd 12122 !WARN_ON(pipe_config->shared_dpll)) {
ad421372
ML
12123 ret = dev_priv->display.crtc_compute_clock(intel_crtc,
12124 pipe_config);
12125 if (ret)
12126 return ret;
12127 }
12128
82cf435b
LL
12129 if (crtc_state->color_mgmt_changed) {
12130 ret = intel_color_check(crtc, crtc_state);
12131 if (ret)
12132 return ret;
12133 }
12134
e435d6e5 12135 ret = 0;
86c8bbbe 12136 if (dev_priv->display.compute_pipe_wm) {
e3bddded 12137 ret = dev_priv->display.compute_pipe_wm(pipe_config);
ed4a6a7c
MR
12138 if (ret) {
12139 DRM_DEBUG_KMS("Target pipe watermarks are invalid\n");
12140 return ret;
12141 }
12142 }
12143
12144 if (dev_priv->display.compute_intermediate_wm &&
12145 !to_intel_atomic_state(state)->skip_intermediate_wm) {
12146 if (WARN_ON(!dev_priv->display.compute_pipe_wm))
12147 return 0;
12148
12149 /*
12150 * Calculate 'intermediate' watermarks that satisfy both the
12151 * old state and the new state. We can program these
12152 * immediately.
12153 */
12154 ret = dev_priv->display.compute_intermediate_wm(crtc->dev,
12155 intel_crtc,
12156 pipe_config);
12157 if (ret) {
12158 DRM_DEBUG_KMS("No valid intermediate pipe watermarks are possible\n");
86c8bbbe 12159 return ret;
ed4a6a7c 12160 }
e3d5457c
VS
12161 } else if (dev_priv->display.compute_intermediate_wm) {
12162 if (HAS_PCH_SPLIT(dev_priv) && INTEL_GEN(dev_priv) < 9)
12163 pipe_config->wm.ilk.intermediate = pipe_config->wm.ilk.optimal;
86c8bbbe
MR
12164 }
12165
e435d6e5
ML
12166 if (INTEL_INFO(dev)->gen >= 9) {
12167 if (mode_changed)
12168 ret = skl_update_scaler_crtc(pipe_config);
12169
12170 if (!ret)
12171 ret = intel_atomic_setup_scalers(dev, intel_crtc,
12172 pipe_config);
12173 }
12174
12175 return ret;
6d3a1ce7
ML
12176}
12177
65b38e0d 12178static const struct drm_crtc_helper_funcs intel_helper_funcs = {
f6e5b160 12179 .mode_set_base_atomic = intel_pipe_set_base_atomic,
5a21b665
DV
12180 .atomic_begin = intel_begin_crtc_commit,
12181 .atomic_flush = intel_finish_crtc_commit,
6d3a1ce7 12182 .atomic_check = intel_crtc_atomic_check,
f6e5b160
CW
12183};
12184
d29b2f9d
ACO
12185static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
12186{
12187 struct intel_connector *connector;
12188
12189 for_each_intel_connector(dev, connector) {
8863dc7f
DV
12190 if (connector->base.state->crtc)
12191 drm_connector_unreference(&connector->base);
12192
d29b2f9d
ACO
12193 if (connector->base.encoder) {
12194 connector->base.state->best_encoder =
12195 connector->base.encoder;
12196 connector->base.state->crtc =
12197 connector->base.encoder->crtc;
8863dc7f
DV
12198
12199 drm_connector_reference(&connector->base);
d29b2f9d
ACO
12200 } else {
12201 connector->base.state->best_encoder = NULL;
12202 connector->base.state->crtc = NULL;
12203 }
12204 }
12205}
12206
050f7aeb 12207static void
eba905b2 12208connected_sink_compute_bpp(struct intel_connector *connector,
5cec258b 12209 struct intel_crtc_state *pipe_config)
050f7aeb
DV
12210{
12211 int bpp = pipe_config->pipe_bpp;
12212
12213 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
12214 connector->base.base.id,
c23cc417 12215 connector->base.name);
050f7aeb
DV
12216
12217 /* Don't use an invalid EDID bpc value */
12218 if (connector->base.display_info.bpc &&
12219 connector->base.display_info.bpc * 3 < bpp) {
12220 DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
12221 bpp, connector->base.display_info.bpc*3);
12222 pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
12223 }
12224
013dd9e0
JN
12225 /* Clamp bpp to default limit on screens without EDID 1.4 */
12226 if (connector->base.display_info.bpc == 0) {
12227 int type = connector->base.connector_type;
12228 int clamp_bpp = 24;
12229
12230 /* Fall back to 18 bpp when DP sink capability is unknown. */
12231 if (type == DRM_MODE_CONNECTOR_DisplayPort ||
12232 type == DRM_MODE_CONNECTOR_eDP)
12233 clamp_bpp = 18;
12234
12235 if (bpp > clamp_bpp) {
12236 DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of %d\n",
12237 bpp, clamp_bpp);
12238 pipe_config->pipe_bpp = clamp_bpp;
12239 }
050f7aeb
DV
12240 }
12241}
12242
4e53c2e0 12243static int
050f7aeb 12244compute_baseline_pipe_bpp(struct intel_crtc *crtc,
5cec258b 12245 struct intel_crtc_state *pipe_config)
4e53c2e0 12246{
050f7aeb 12247 struct drm_device *dev = crtc->base.dev;
1486017f 12248 struct drm_atomic_state *state;
da3ced29
ACO
12249 struct drm_connector *connector;
12250 struct drm_connector_state *connector_state;
1486017f 12251 int bpp, i;
4e53c2e0 12252
666a4537 12253 if ((IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)))
4e53c2e0 12254 bpp = 10*3;
d328c9d7
DV
12255 else if (INTEL_INFO(dev)->gen >= 5)
12256 bpp = 12*3;
12257 else
12258 bpp = 8*3;
12259
4e53c2e0 12260
4e53c2e0
DV
12261 pipe_config->pipe_bpp = bpp;
12262
1486017f
ACO
12263 state = pipe_config->base.state;
12264
4e53c2e0 12265 /* Clamp display bpp to EDID value */
da3ced29
ACO
12266 for_each_connector_in_state(state, connector, connector_state, i) {
12267 if (connector_state->crtc != &crtc->base)
4e53c2e0
DV
12268 continue;
12269
da3ced29
ACO
12270 connected_sink_compute_bpp(to_intel_connector(connector),
12271 pipe_config);
4e53c2e0
DV
12272 }
12273
12274 return bpp;
12275}
12276
644db711
DV
12277static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
12278{
12279 DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
12280 "type: 0x%x flags: 0x%x\n",
1342830c 12281 mode->crtc_clock,
644db711
DV
12282 mode->crtc_hdisplay, mode->crtc_hsync_start,
12283 mode->crtc_hsync_end, mode->crtc_htotal,
12284 mode->crtc_vdisplay, mode->crtc_vsync_start,
12285 mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
12286}
12287
c0b03411 12288static void intel_dump_pipe_config(struct intel_crtc *crtc,
5cec258b 12289 struct intel_crtc_state *pipe_config,
c0b03411
DV
12290 const char *context)
12291{
6a60cd87
CK
12292 struct drm_device *dev = crtc->base.dev;
12293 struct drm_plane *plane;
12294 struct intel_plane *intel_plane;
12295 struct intel_plane_state *state;
12296 struct drm_framebuffer *fb;
12297
78108b7c
VS
12298 DRM_DEBUG_KMS("[CRTC:%d:%s]%s config %p for pipe %c\n",
12299 crtc->base.base.id, crtc->base.name,
6a60cd87 12300 context, pipe_config, pipe_name(crtc->pipe));
c0b03411 12301
da205630 12302 DRM_DEBUG_KMS("cpu_transcoder: %s\n", transcoder_name(pipe_config->cpu_transcoder));
c0b03411
DV
12303 DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
12304 pipe_config->pipe_bpp, pipe_config->dither);
12305 DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
12306 pipe_config->has_pch_encoder,
12307 pipe_config->fdi_lanes,
12308 pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
12309 pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
12310 pipe_config->fdi_m_n.tu);
90a6b7b0 12311 DRM_DEBUG_KMS("dp: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
eb14cb74 12312 pipe_config->has_dp_encoder,
90a6b7b0 12313 pipe_config->lane_count,
eb14cb74
VS
12314 pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
12315 pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
12316 pipe_config->dp_m_n.tu);
b95af8be 12317
90a6b7b0 12318 DRM_DEBUG_KMS("dp: %i, lanes: %i, gmch_m2: %u, gmch_n2: %u, link_m2: %u, link_n2: %u, tu2: %u\n",
b95af8be 12319 pipe_config->has_dp_encoder,
90a6b7b0 12320 pipe_config->lane_count,
b95af8be
VK
12321 pipe_config->dp_m2_n2.gmch_m,
12322 pipe_config->dp_m2_n2.gmch_n,
12323 pipe_config->dp_m2_n2.link_m,
12324 pipe_config->dp_m2_n2.link_n,
12325 pipe_config->dp_m2_n2.tu);
12326
55072d19
DV
12327 DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
12328 pipe_config->has_audio,
12329 pipe_config->has_infoframe);
12330
c0b03411 12331 DRM_DEBUG_KMS("requested mode:\n");
2d112de7 12332 drm_mode_debug_printmodeline(&pipe_config->base.mode);
c0b03411 12333 DRM_DEBUG_KMS("adjusted mode:\n");
2d112de7
ACO
12334 drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
12335 intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
d71b8d4a 12336 DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
37327abd
VS
12337 DRM_DEBUG_KMS("pipe src size: %dx%d\n",
12338 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
0ec463d3
TU
12339 DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
12340 crtc->num_scalers,
12341 pipe_config->scaler_state.scaler_users,
12342 pipe_config->scaler_state.scaler_id);
c0b03411
DV
12343 DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
12344 pipe_config->gmch_pfit.control,
12345 pipe_config->gmch_pfit.pgm_ratios,
12346 pipe_config->gmch_pfit.lvds_border_bits);
fd4daa9c 12347 DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
c0b03411 12348 pipe_config->pch_pfit.pos,
fd4daa9c
CW
12349 pipe_config->pch_pfit.size,
12350 pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
42db64ef 12351 DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
cf532bb2 12352 DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
6a60cd87 12353
415ff0f6 12354 if (IS_BROXTON(dev)) {
05712c15 12355 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: ebb0: 0x%x, ebb4: 0x%x,"
415ff0f6 12356 "pll0: 0x%x, pll1: 0x%x, pll2: 0x%x, pll3: 0x%x, "
c8453338 12357 "pll6: 0x%x, pll8: 0x%x, pll9: 0x%x, pll10: 0x%x, pcsdw12: 0x%x\n",
415ff0f6
TU
12358 pipe_config->ddi_pll_sel,
12359 pipe_config->dpll_hw_state.ebb0,
05712c15 12360 pipe_config->dpll_hw_state.ebb4,
415ff0f6
TU
12361 pipe_config->dpll_hw_state.pll0,
12362 pipe_config->dpll_hw_state.pll1,
12363 pipe_config->dpll_hw_state.pll2,
12364 pipe_config->dpll_hw_state.pll3,
12365 pipe_config->dpll_hw_state.pll6,
12366 pipe_config->dpll_hw_state.pll8,
05712c15 12367 pipe_config->dpll_hw_state.pll9,
c8453338 12368 pipe_config->dpll_hw_state.pll10,
415ff0f6 12369 pipe_config->dpll_hw_state.pcsdw12);
ef11bdb3 12370 } else if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) {
415ff0f6
TU
12371 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: "
12372 "ctrl1: 0x%x, cfgcr1: 0x%x, cfgcr2: 0x%x\n",
12373 pipe_config->ddi_pll_sel,
12374 pipe_config->dpll_hw_state.ctrl1,
12375 pipe_config->dpll_hw_state.cfgcr1,
12376 pipe_config->dpll_hw_state.cfgcr2);
12377 } else if (HAS_DDI(dev)) {
1260f07e 12378 DRM_DEBUG_KMS("ddi_pll_sel: 0x%x; dpll_hw_state: wrpll: 0x%x spll: 0x%x\n",
415ff0f6 12379 pipe_config->ddi_pll_sel,
00490c22
ML
12380 pipe_config->dpll_hw_state.wrpll,
12381 pipe_config->dpll_hw_state.spll);
415ff0f6
TU
12382 } else {
12383 DRM_DEBUG_KMS("dpll_hw_state: dpll: 0x%x, dpll_md: 0x%x, "
12384 "fp0: 0x%x, fp1: 0x%x\n",
12385 pipe_config->dpll_hw_state.dpll,
12386 pipe_config->dpll_hw_state.dpll_md,
12387 pipe_config->dpll_hw_state.fp0,
12388 pipe_config->dpll_hw_state.fp1);
12389 }
12390
6a60cd87
CK
12391 DRM_DEBUG_KMS("planes on this crtc\n");
12392 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
12393 intel_plane = to_intel_plane(plane);
12394 if (intel_plane->pipe != crtc->pipe)
12395 continue;
12396
12397 state = to_intel_plane_state(plane->state);
12398 fb = state->base.fb;
12399 if (!fb) {
1d577e02
VS
12400 DRM_DEBUG_KMS("[PLANE:%d:%s] disabled, scaler_id = %d\n",
12401 plane->base.id, plane->name, state->scaler_id);
6a60cd87
CK
12402 continue;
12403 }
12404
1d577e02
VS
12405 DRM_DEBUG_KMS("[PLANE:%d:%s] enabled",
12406 plane->base.id, plane->name);
12407 DRM_DEBUG_KMS("\tFB:%d, fb = %ux%u format = %s",
12408 fb->base.id, fb->width, fb->height,
12409 drm_get_format_name(fb->pixel_format));
12410 DRM_DEBUG_KMS("\tscaler:%d src %dx%d+%d+%d dst %dx%d+%d+%d\n",
12411 state->scaler_id,
12412 state->src.x1 >> 16, state->src.y1 >> 16,
12413 drm_rect_width(&state->src) >> 16,
12414 drm_rect_height(&state->src) >> 16,
12415 state->dst.x1, state->dst.y1,
12416 drm_rect_width(&state->dst),
12417 drm_rect_height(&state->dst));
6a60cd87 12418 }
c0b03411
DV
12419}
12420
5448a00d 12421static bool check_digital_port_conflicts(struct drm_atomic_state *state)
00f0b378 12422{
5448a00d 12423 struct drm_device *dev = state->dev;
da3ced29 12424 struct drm_connector *connector;
00f0b378
VS
12425 unsigned int used_ports = 0;
12426
12427 /*
12428 * Walk the connector list instead of the encoder
12429 * list to detect the problem on ddi platforms
12430 * where there's just one encoder per digital port.
12431 */
0bff4858
VS
12432 drm_for_each_connector(connector, dev) {
12433 struct drm_connector_state *connector_state;
12434 struct intel_encoder *encoder;
12435
12436 connector_state = drm_atomic_get_existing_connector_state(state, connector);
12437 if (!connector_state)
12438 connector_state = connector->state;
12439
5448a00d 12440 if (!connector_state->best_encoder)
00f0b378
VS
12441 continue;
12442
5448a00d
ACO
12443 encoder = to_intel_encoder(connector_state->best_encoder);
12444
12445 WARN_ON(!connector_state->crtc);
00f0b378
VS
12446
12447 switch (encoder->type) {
12448 unsigned int port_mask;
12449 case INTEL_OUTPUT_UNKNOWN:
12450 if (WARN_ON(!HAS_DDI(dev)))
12451 break;
12452 case INTEL_OUTPUT_DISPLAYPORT:
12453 case INTEL_OUTPUT_HDMI:
12454 case INTEL_OUTPUT_EDP:
12455 port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
12456
12457 /* the same port mustn't appear more than once */
12458 if (used_ports & port_mask)
12459 return false;
12460
12461 used_ports |= port_mask;
12462 default:
12463 break;
12464 }
12465 }
12466
12467 return true;
12468}
12469
83a57153
ACO
12470static void
12471clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
12472{
12473 struct drm_crtc_state tmp_state;
663a3640 12474 struct intel_crtc_scaler_state scaler_state;
4978cc93 12475 struct intel_dpll_hw_state dpll_hw_state;
8106ddbd 12476 struct intel_shared_dpll *shared_dpll;
8504c74c 12477 uint32_t ddi_pll_sel;
c4e2d043 12478 bool force_thru;
83a57153 12479
7546a384
ACO
12480 /* FIXME: before the switch to atomic started, a new pipe_config was
12481 * kzalloc'd. Code that depends on any field being zero should be
12482 * fixed, so that the crtc_state can be safely duplicated. For now,
12483 * only fields that are know to not cause problems are preserved. */
12484
83a57153 12485 tmp_state = crtc_state->base;
663a3640 12486 scaler_state = crtc_state->scaler_state;
4978cc93
ACO
12487 shared_dpll = crtc_state->shared_dpll;
12488 dpll_hw_state = crtc_state->dpll_hw_state;
8504c74c 12489 ddi_pll_sel = crtc_state->ddi_pll_sel;
c4e2d043 12490 force_thru = crtc_state->pch_pfit.force_thru;
4978cc93 12491
83a57153 12492 memset(crtc_state, 0, sizeof *crtc_state);
4978cc93 12493
83a57153 12494 crtc_state->base = tmp_state;
663a3640 12495 crtc_state->scaler_state = scaler_state;
4978cc93
ACO
12496 crtc_state->shared_dpll = shared_dpll;
12497 crtc_state->dpll_hw_state = dpll_hw_state;
8504c74c 12498 crtc_state->ddi_pll_sel = ddi_pll_sel;
c4e2d043 12499 crtc_state->pch_pfit.force_thru = force_thru;
83a57153
ACO
12500}
12501
548ee15b 12502static int
b8cecdf5 12503intel_modeset_pipe_config(struct drm_crtc *crtc,
b359283a 12504 struct intel_crtc_state *pipe_config)
ee7b9f93 12505{
b359283a 12506 struct drm_atomic_state *state = pipe_config->base.state;
7758a113 12507 struct intel_encoder *encoder;
da3ced29 12508 struct drm_connector *connector;
0b901879 12509 struct drm_connector_state *connector_state;
d328c9d7 12510 int base_bpp, ret = -EINVAL;
0b901879 12511 int i;
e29c22c0 12512 bool retry = true;
ee7b9f93 12513
83a57153 12514 clear_intel_crtc_state(pipe_config);
7758a113 12515
e143a21c
DV
12516 pipe_config->cpu_transcoder =
12517 (enum transcoder) to_intel_crtc(crtc)->pipe;
b8cecdf5 12518
2960bc9c
ID
12519 /*
12520 * Sanitize sync polarity flags based on requested ones. If neither
12521 * positive or negative polarity is requested, treat this as meaning
12522 * negative polarity.
12523 */
2d112de7 12524 if (!(pipe_config->base.adjusted_mode.flags &
2960bc9c 12525 (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
2d112de7 12526 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
2960bc9c 12527
2d112de7 12528 if (!(pipe_config->base.adjusted_mode.flags &
2960bc9c 12529 (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
2d112de7 12530 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
2960bc9c 12531
d328c9d7
DV
12532 base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
12533 pipe_config);
12534 if (base_bpp < 0)
4e53c2e0
DV
12535 goto fail;
12536
e41a56be
VS
12537 /*
12538 * Determine the real pipe dimensions. Note that stereo modes can
12539 * increase the actual pipe size due to the frame doubling and
12540 * insertion of additional space for blanks between the frame. This
12541 * is stored in the crtc timings. We use the requested mode to do this
12542 * computation to clearly distinguish it from the adjusted mode, which
12543 * can be changed by the connectors in the below retry loop.
12544 */
2d112de7 12545 drm_crtc_get_hv_timing(&pipe_config->base.mode,
ecb7e16b
GP
12546 &pipe_config->pipe_src_w,
12547 &pipe_config->pipe_src_h);
e41a56be 12548
e29c22c0 12549encoder_retry:
ef1b460d 12550 /* Ensure the port clock defaults are reset when retrying. */
ff9a6750 12551 pipe_config->port_clock = 0;
ef1b460d 12552 pipe_config->pixel_multiplier = 1;
ff9a6750 12553
135c81b8 12554 /* Fill in default crtc timings, allow encoders to overwrite them. */
2d112de7
ACO
12555 drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
12556 CRTC_STEREO_DOUBLE);
135c81b8 12557
7758a113
DV
12558 /* Pass our mode to the connectors and the CRTC to give them a chance to
12559 * adjust it according to limitations or connector properties, and also
12560 * a chance to reject the mode entirely.
47f1c6c9 12561 */
da3ced29 12562 for_each_connector_in_state(state, connector, connector_state, i) {
0b901879 12563 if (connector_state->crtc != crtc)
7758a113 12564 continue;
7ae89233 12565
0b901879
ACO
12566 encoder = to_intel_encoder(connector_state->best_encoder);
12567
efea6e8e
DV
12568 if (!(encoder->compute_config(encoder, pipe_config))) {
12569 DRM_DEBUG_KMS("Encoder config failure\n");
7758a113
DV
12570 goto fail;
12571 }
ee7b9f93 12572 }
47f1c6c9 12573
ff9a6750
DV
12574 /* Set default port clock if not overwritten by the encoder. Needs to be
12575 * done afterwards in case the encoder adjusts the mode. */
12576 if (!pipe_config->port_clock)
2d112de7 12577 pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
241bfc38 12578 * pipe_config->pixel_multiplier;
ff9a6750 12579
a43f6e0f 12580 ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
e29c22c0 12581 if (ret < 0) {
7758a113
DV
12582 DRM_DEBUG_KMS("CRTC fixup failed\n");
12583 goto fail;
ee7b9f93 12584 }
e29c22c0
DV
12585
12586 if (ret == RETRY) {
12587 if (WARN(!retry, "loop in pipe configuration computation\n")) {
12588 ret = -EINVAL;
12589 goto fail;
12590 }
12591
12592 DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
12593 retry = false;
12594 goto encoder_retry;
12595 }
12596
e8fa4270
DV
12597 /* Dithering seems to not pass-through bits correctly when it should, so
12598 * only enable it on 6bpc panels. */
12599 pipe_config->dither = pipe_config->pipe_bpp == 6*3;
62f0ace5 12600 DRM_DEBUG_KMS("hw max bpp: %i, pipe bpp: %i, dithering: %i\n",
d328c9d7 12601 base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
4e53c2e0 12602
7758a113 12603fail:
548ee15b 12604 return ret;
ee7b9f93 12605}
47f1c6c9 12606
ea9d758d 12607static void
4740b0f2 12608intel_modeset_update_crtc_state(struct drm_atomic_state *state)
ea9d758d 12609{
0a9ab303
ACO
12610 struct drm_crtc *crtc;
12611 struct drm_crtc_state *crtc_state;
8a75d157 12612 int i;
ea9d758d 12613
7668851f 12614 /* Double check state. */
8a75d157 12615 for_each_crtc_in_state(state, crtc, crtc_state, i) {
3cb480bc 12616 to_intel_crtc(crtc)->config = to_intel_crtc_state(crtc->state);
fc467a22
ML
12617
12618 /* Update hwmode for vblank functions */
12619 if (crtc->state->active)
12620 crtc->hwmode = crtc->state->adjusted_mode;
12621 else
12622 crtc->hwmode.crtc_clock = 0;
61067a5e
ML
12623
12624 /*
12625 * Update legacy state to satisfy fbc code. This can
12626 * be removed when fbc uses the atomic state.
12627 */
12628 if (drm_atomic_get_existing_plane_state(state, crtc->primary)) {
12629 struct drm_plane_state *plane_state = crtc->primary->state;
12630
12631 crtc->primary->fb = plane_state->fb;
12632 crtc->x = plane_state->src_x >> 16;
12633 crtc->y = plane_state->src_y >> 16;
12634 }
ea9d758d 12635 }
ea9d758d
DV
12636}
12637
3bd26263 12638static bool intel_fuzzy_clock_check(int clock1, int clock2)
f1f644dc 12639{
3bd26263 12640 int diff;
f1f644dc
JB
12641
12642 if (clock1 == clock2)
12643 return true;
12644
12645 if (!clock1 || !clock2)
12646 return false;
12647
12648 diff = abs(clock1 - clock2);
12649
12650 if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
12651 return true;
12652
12653 return false;
12654}
12655
25c5b266
DV
12656#define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
12657 list_for_each_entry((intel_crtc), \
12658 &(dev)->mode_config.crtc_list, \
12659 base.head) \
95150bdf 12660 for_each_if (mask & (1 <<(intel_crtc)->pipe))
25c5b266 12661
cfb23ed6
ML
12662static bool
12663intel_compare_m_n(unsigned int m, unsigned int n,
12664 unsigned int m2, unsigned int n2,
12665 bool exact)
12666{
12667 if (m == m2 && n == n2)
12668 return true;
12669
12670 if (exact || !m || !n || !m2 || !n2)
12671 return false;
12672
12673 BUILD_BUG_ON(DATA_LINK_M_N_MASK > INT_MAX);
12674
31d10b57
ML
12675 if (n > n2) {
12676 while (n > n2) {
cfb23ed6
ML
12677 m2 <<= 1;
12678 n2 <<= 1;
12679 }
31d10b57
ML
12680 } else if (n < n2) {
12681 while (n < n2) {
cfb23ed6
ML
12682 m <<= 1;
12683 n <<= 1;
12684 }
12685 }
12686
31d10b57
ML
12687 if (n != n2)
12688 return false;
12689
12690 return intel_fuzzy_clock_check(m, m2);
cfb23ed6
ML
12691}
12692
12693static bool
12694intel_compare_link_m_n(const struct intel_link_m_n *m_n,
12695 struct intel_link_m_n *m2_n2,
12696 bool adjust)
12697{
12698 if (m_n->tu == m2_n2->tu &&
12699 intel_compare_m_n(m_n->gmch_m, m_n->gmch_n,
12700 m2_n2->gmch_m, m2_n2->gmch_n, !adjust) &&
12701 intel_compare_m_n(m_n->link_m, m_n->link_n,
12702 m2_n2->link_m, m2_n2->link_n, !adjust)) {
12703 if (adjust)
12704 *m2_n2 = *m_n;
12705
12706 return true;
12707 }
12708
12709 return false;
12710}
12711
0e8ffe1b 12712static bool
2fa2fe9a 12713intel_pipe_config_compare(struct drm_device *dev,
5cec258b 12714 struct intel_crtc_state *current_config,
cfb23ed6
ML
12715 struct intel_crtc_state *pipe_config,
12716 bool adjust)
0e8ffe1b 12717{
cfb23ed6
ML
12718 bool ret = true;
12719
12720#define INTEL_ERR_OR_DBG_KMS(fmt, ...) \
12721 do { \
12722 if (!adjust) \
12723 DRM_ERROR(fmt, ##__VA_ARGS__); \
12724 else \
12725 DRM_DEBUG_KMS(fmt, ##__VA_ARGS__); \
12726 } while (0)
12727
66e985c0
DV
12728#define PIPE_CONF_CHECK_X(name) \
12729 if (current_config->name != pipe_config->name) { \
cfb23ed6 12730 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
66e985c0
DV
12731 "(expected 0x%08x, found 0x%08x)\n", \
12732 current_config->name, \
12733 pipe_config->name); \
cfb23ed6 12734 ret = false; \
66e985c0
DV
12735 }
12736
08a24034
DV
12737#define PIPE_CONF_CHECK_I(name) \
12738 if (current_config->name != pipe_config->name) { \
cfb23ed6 12739 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
08a24034
DV
12740 "(expected %i, found %i)\n", \
12741 current_config->name, \
12742 pipe_config->name); \
cfb23ed6
ML
12743 ret = false; \
12744 }
12745
8106ddbd
ACO
12746#define PIPE_CONF_CHECK_P(name) \
12747 if (current_config->name != pipe_config->name) { \
12748 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
12749 "(expected %p, found %p)\n", \
12750 current_config->name, \
12751 pipe_config->name); \
12752 ret = false; \
12753 }
12754
cfb23ed6
ML
12755#define PIPE_CONF_CHECK_M_N(name) \
12756 if (!intel_compare_link_m_n(&current_config->name, \
12757 &pipe_config->name,\
12758 adjust)) { \
12759 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
12760 "(expected tu %i gmch %i/%i link %i/%i, " \
12761 "found tu %i, gmch %i/%i link %i/%i)\n", \
12762 current_config->name.tu, \
12763 current_config->name.gmch_m, \
12764 current_config->name.gmch_n, \
12765 current_config->name.link_m, \
12766 current_config->name.link_n, \
12767 pipe_config->name.tu, \
12768 pipe_config->name.gmch_m, \
12769 pipe_config->name.gmch_n, \
12770 pipe_config->name.link_m, \
12771 pipe_config->name.link_n); \
12772 ret = false; \
12773 }
12774
55c561a7
DV
12775/* This is required for BDW+ where there is only one set of registers for
12776 * switching between high and low RR.
12777 * This macro can be used whenever a comparison has to be made between one
12778 * hw state and multiple sw state variables.
12779 */
cfb23ed6
ML
12780#define PIPE_CONF_CHECK_M_N_ALT(name, alt_name) \
12781 if (!intel_compare_link_m_n(&current_config->name, \
12782 &pipe_config->name, adjust) && \
12783 !intel_compare_link_m_n(&current_config->alt_name, \
12784 &pipe_config->name, adjust)) { \
12785 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
12786 "(expected tu %i gmch %i/%i link %i/%i, " \
12787 "or tu %i gmch %i/%i link %i/%i, " \
12788 "found tu %i, gmch %i/%i link %i/%i)\n", \
12789 current_config->name.tu, \
12790 current_config->name.gmch_m, \
12791 current_config->name.gmch_n, \
12792 current_config->name.link_m, \
12793 current_config->name.link_n, \
12794 current_config->alt_name.tu, \
12795 current_config->alt_name.gmch_m, \
12796 current_config->alt_name.gmch_n, \
12797 current_config->alt_name.link_m, \
12798 current_config->alt_name.link_n, \
12799 pipe_config->name.tu, \
12800 pipe_config->name.gmch_m, \
12801 pipe_config->name.gmch_n, \
12802 pipe_config->name.link_m, \
12803 pipe_config->name.link_n); \
12804 ret = false; \
88adfff1
DV
12805 }
12806
1bd1bd80
DV
12807#define PIPE_CONF_CHECK_FLAGS(name, mask) \
12808 if ((current_config->name ^ pipe_config->name) & (mask)) { \
cfb23ed6 12809 INTEL_ERR_OR_DBG_KMS("mismatch in " #name "(" #mask ") " \
1bd1bd80
DV
12810 "(expected %i, found %i)\n", \
12811 current_config->name & (mask), \
12812 pipe_config->name & (mask)); \
cfb23ed6 12813 ret = false; \
1bd1bd80
DV
12814 }
12815
5e550656
VS
12816#define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
12817 if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
cfb23ed6 12818 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
5e550656
VS
12819 "(expected %i, found %i)\n", \
12820 current_config->name, \
12821 pipe_config->name); \
cfb23ed6 12822 ret = false; \
5e550656
VS
12823 }
12824
bb760063
DV
12825#define PIPE_CONF_QUIRK(quirk) \
12826 ((current_config->quirks | pipe_config->quirks) & (quirk))
12827
eccb140b
DV
12828 PIPE_CONF_CHECK_I(cpu_transcoder);
12829
08a24034
DV
12830 PIPE_CONF_CHECK_I(has_pch_encoder);
12831 PIPE_CONF_CHECK_I(fdi_lanes);
cfb23ed6 12832 PIPE_CONF_CHECK_M_N(fdi_m_n);
08a24034 12833
eb14cb74 12834 PIPE_CONF_CHECK_I(has_dp_encoder);
90a6b7b0 12835 PIPE_CONF_CHECK_I(lane_count);
95a7a2ae 12836 PIPE_CONF_CHECK_X(lane_lat_optim_mask);
b95af8be
VK
12837
12838 if (INTEL_INFO(dev)->gen < 8) {
cfb23ed6
ML
12839 PIPE_CONF_CHECK_M_N(dp_m_n);
12840
cfb23ed6
ML
12841 if (current_config->has_drrs)
12842 PIPE_CONF_CHECK_M_N(dp_m2_n2);
12843 } else
12844 PIPE_CONF_CHECK_M_N_ALT(dp_m_n, dp_m2_n2);
eb14cb74 12845
a65347ba
JN
12846 PIPE_CONF_CHECK_I(has_dsi_encoder);
12847
2d112de7
ACO
12848 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
12849 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
12850 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
12851 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
12852 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
12853 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
1bd1bd80 12854
2d112de7
ACO
12855 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
12856 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
12857 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
12858 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
12859 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
12860 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
1bd1bd80 12861
c93f54cf 12862 PIPE_CONF_CHECK_I(pixel_multiplier);
6897b4b5 12863 PIPE_CONF_CHECK_I(has_hdmi_sink);
b5a9fa09 12864 if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
666a4537 12865 IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev))
b5a9fa09 12866 PIPE_CONF_CHECK_I(limited_color_range);
e43823ec 12867 PIPE_CONF_CHECK_I(has_infoframe);
6c49f241 12868
9ed109a7
DV
12869 PIPE_CONF_CHECK_I(has_audio);
12870
2d112de7 12871 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
1bd1bd80
DV
12872 DRM_MODE_FLAG_INTERLACE);
12873
bb760063 12874 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
2d112de7 12875 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063 12876 DRM_MODE_FLAG_PHSYNC);
2d112de7 12877 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063 12878 DRM_MODE_FLAG_NHSYNC);
2d112de7 12879 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063 12880 DRM_MODE_FLAG_PVSYNC);
2d112de7 12881 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063
DV
12882 DRM_MODE_FLAG_NVSYNC);
12883 }
045ac3b5 12884
333b8ca8 12885 PIPE_CONF_CHECK_X(gmch_pfit.control);
e2ff2d4a
DV
12886 /* pfit ratios are autocomputed by the hw on gen4+ */
12887 if (INTEL_INFO(dev)->gen < 4)
7f7d8dd6 12888 PIPE_CONF_CHECK_X(gmch_pfit.pgm_ratios);
333b8ca8 12889 PIPE_CONF_CHECK_X(gmch_pfit.lvds_border_bits);
9953599b 12890
bfd16b2a
ML
12891 if (!adjust) {
12892 PIPE_CONF_CHECK_I(pipe_src_w);
12893 PIPE_CONF_CHECK_I(pipe_src_h);
12894
12895 PIPE_CONF_CHECK_I(pch_pfit.enabled);
12896 if (current_config->pch_pfit.enabled) {
12897 PIPE_CONF_CHECK_X(pch_pfit.pos);
12898 PIPE_CONF_CHECK_X(pch_pfit.size);
12899 }
2fa2fe9a 12900
7aefe2b5
ML
12901 PIPE_CONF_CHECK_I(scaler_state.scaler_id);
12902 }
a1b2278e 12903
e59150dc
JB
12904 /* BDW+ don't expose a synchronous way to read the state */
12905 if (IS_HASWELL(dev))
12906 PIPE_CONF_CHECK_I(ips_enabled);
42db64ef 12907
282740f7
VS
12908 PIPE_CONF_CHECK_I(double_wide);
12909
26804afd
DV
12910 PIPE_CONF_CHECK_X(ddi_pll_sel);
12911
8106ddbd 12912 PIPE_CONF_CHECK_P(shared_dpll);
66e985c0 12913 PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
8bcc2795 12914 PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
66e985c0
DV
12915 PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
12916 PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
d452c5b6 12917 PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
00490c22 12918 PIPE_CONF_CHECK_X(dpll_hw_state.spll);
3f4cd19f
DL
12919 PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
12920 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
12921 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
c0d43d62 12922
47eacbab
VS
12923 PIPE_CONF_CHECK_X(dsi_pll.ctrl);
12924 PIPE_CONF_CHECK_X(dsi_pll.div);
12925
42571aef
VS
12926 if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
12927 PIPE_CONF_CHECK_I(pipe_bpp);
12928
2d112de7 12929 PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
a9a7e98a 12930 PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
5e550656 12931
66e985c0 12932#undef PIPE_CONF_CHECK_X
08a24034 12933#undef PIPE_CONF_CHECK_I
8106ddbd 12934#undef PIPE_CONF_CHECK_P
1bd1bd80 12935#undef PIPE_CONF_CHECK_FLAGS
5e550656 12936#undef PIPE_CONF_CHECK_CLOCK_FUZZY
bb760063 12937#undef PIPE_CONF_QUIRK
cfb23ed6 12938#undef INTEL_ERR_OR_DBG_KMS
88adfff1 12939
cfb23ed6 12940 return ret;
0e8ffe1b
DV
12941}
12942
e3b247da
VS
12943static void intel_pipe_config_sanity_check(struct drm_i915_private *dev_priv,
12944 const struct intel_crtc_state *pipe_config)
12945{
12946 if (pipe_config->has_pch_encoder) {
21a727b3 12947 int fdi_dotclock = intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
e3b247da
VS
12948 &pipe_config->fdi_m_n);
12949 int dotclock = pipe_config->base.adjusted_mode.crtc_clock;
12950
12951 /*
12952 * FDI already provided one idea for the dotclock.
12953 * Yell if the encoder disagrees.
12954 */
12955 WARN(!intel_fuzzy_clock_check(fdi_dotclock, dotclock),
12956 "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
12957 fdi_dotclock, dotclock);
12958 }
12959}
12960
c0ead703
ML
12961static void verify_wm_state(struct drm_crtc *crtc,
12962 struct drm_crtc_state *new_state)
08db6652 12963{
e7c84544 12964 struct drm_device *dev = crtc->dev;
08db6652
DL
12965 struct drm_i915_private *dev_priv = dev->dev_private;
12966 struct skl_ddb_allocation hw_ddb, *sw_ddb;
e7c84544
ML
12967 struct skl_ddb_entry *hw_entry, *sw_entry;
12968 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
12969 const enum pipe pipe = intel_crtc->pipe;
08db6652
DL
12970 int plane;
12971
e7c84544 12972 if (INTEL_INFO(dev)->gen < 9 || !new_state->active)
08db6652
DL
12973 return;
12974
12975 skl_ddb_get_hw_state(dev_priv, &hw_ddb);
12976 sw_ddb = &dev_priv->wm.skl_hw.ddb;
12977
e7c84544
ML
12978 /* planes */
12979 for_each_plane(dev_priv, pipe, plane) {
12980 hw_entry = &hw_ddb.plane[pipe][plane];
12981 sw_entry = &sw_ddb->plane[pipe][plane];
08db6652 12982
e7c84544 12983 if (skl_ddb_entry_equal(hw_entry, sw_entry))
08db6652
DL
12984 continue;
12985
e7c84544
ML
12986 DRM_ERROR("mismatch in DDB state pipe %c plane %d "
12987 "(expected (%u,%u), found (%u,%u))\n",
12988 pipe_name(pipe), plane + 1,
12989 sw_entry->start, sw_entry->end,
12990 hw_entry->start, hw_entry->end);
12991 }
08db6652 12992
e7c84544
ML
12993 /* cursor */
12994 hw_entry = &hw_ddb.plane[pipe][PLANE_CURSOR];
12995 sw_entry = &sw_ddb->plane[pipe][PLANE_CURSOR];
08db6652 12996
e7c84544 12997 if (!skl_ddb_entry_equal(hw_entry, sw_entry)) {
08db6652
DL
12998 DRM_ERROR("mismatch in DDB state pipe %c cursor "
12999 "(expected (%u,%u), found (%u,%u))\n",
13000 pipe_name(pipe),
13001 sw_entry->start, sw_entry->end,
13002 hw_entry->start, hw_entry->end);
13003 }
13004}
13005
91d1b4bd 13006static void
c0ead703 13007verify_connector_state(struct drm_device *dev, struct drm_crtc *crtc)
8af6cf88 13008{
35dd3c64 13009 struct drm_connector *connector;
8af6cf88 13010
e7c84544 13011 drm_for_each_connector(connector, dev) {
35dd3c64
ML
13012 struct drm_encoder *encoder = connector->encoder;
13013 struct drm_connector_state *state = connector->state;
ad3c558f 13014
e7c84544
ML
13015 if (state->crtc != crtc)
13016 continue;
13017
5a21b665 13018 intel_connector_verify_state(to_intel_connector(connector));
8af6cf88 13019
ad3c558f 13020 I915_STATE_WARN(state->best_encoder != encoder,
35dd3c64 13021 "connector's atomic encoder doesn't match legacy encoder\n");
8af6cf88 13022 }
91d1b4bd
DV
13023}
13024
13025static void
c0ead703 13026verify_encoder_state(struct drm_device *dev)
91d1b4bd
DV
13027{
13028 struct intel_encoder *encoder;
13029 struct intel_connector *connector;
8af6cf88 13030
b2784e15 13031 for_each_intel_encoder(dev, encoder) {
8af6cf88 13032 bool enabled = false;
4d20cd86 13033 enum pipe pipe;
8af6cf88
DV
13034
13035 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
13036 encoder->base.base.id,
8e329a03 13037 encoder->base.name);
8af6cf88 13038
3a3371ff 13039 for_each_intel_connector(dev, connector) {
4d20cd86 13040 if (connector->base.state->best_encoder != &encoder->base)
8af6cf88
DV
13041 continue;
13042 enabled = true;
ad3c558f
ML
13043
13044 I915_STATE_WARN(connector->base.state->crtc !=
13045 encoder->base.crtc,
13046 "connector's crtc doesn't match encoder crtc\n");
8af6cf88 13047 }
0e32b39c 13048
e2c719b7 13049 I915_STATE_WARN(!!encoder->base.crtc != enabled,
8af6cf88
DV
13050 "encoder's enabled state mismatch "
13051 "(expected %i, found %i)\n",
13052 !!encoder->base.crtc, enabled);
7c60d198
ML
13053
13054 if (!encoder->base.crtc) {
4d20cd86 13055 bool active;
7c60d198 13056
4d20cd86
ML
13057 active = encoder->get_hw_state(encoder, &pipe);
13058 I915_STATE_WARN(active,
13059 "encoder detached but still enabled on pipe %c.\n",
13060 pipe_name(pipe));
7c60d198 13061 }
8af6cf88 13062 }
91d1b4bd
DV
13063}
13064
13065static void
c0ead703
ML
13066verify_crtc_state(struct drm_crtc *crtc,
13067 struct drm_crtc_state *old_crtc_state,
13068 struct drm_crtc_state *new_crtc_state)
91d1b4bd 13069{
e7c84544 13070 struct drm_device *dev = crtc->dev;
fbee40df 13071 struct drm_i915_private *dev_priv = dev->dev_private;
91d1b4bd 13072 struct intel_encoder *encoder;
e7c84544
ML
13073 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13074 struct intel_crtc_state *pipe_config, *sw_config;
13075 struct drm_atomic_state *old_state;
13076 bool active;
045ac3b5 13077
e7c84544 13078 old_state = old_crtc_state->state;
ec2dc6a0 13079 __drm_atomic_helper_crtc_destroy_state(old_crtc_state);
e7c84544
ML
13080 pipe_config = to_intel_crtc_state(old_crtc_state);
13081 memset(pipe_config, 0, sizeof(*pipe_config));
13082 pipe_config->base.crtc = crtc;
13083 pipe_config->base.state = old_state;
8af6cf88 13084
78108b7c 13085 DRM_DEBUG_KMS("[CRTC:%d:%s]\n", crtc->base.id, crtc->name);
8af6cf88 13086
e7c84544 13087 active = dev_priv->display.get_pipe_config(intel_crtc, pipe_config);
d62cf62a 13088
e7c84544
ML
13089 /* hw state is inconsistent with the pipe quirk */
13090 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
13091 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
13092 active = new_crtc_state->active;
6c49f241 13093
e7c84544
ML
13094 I915_STATE_WARN(new_crtc_state->active != active,
13095 "crtc active state doesn't match with hw state "
13096 "(expected %i, found %i)\n", new_crtc_state->active, active);
0e8ffe1b 13097
e7c84544
ML
13098 I915_STATE_WARN(intel_crtc->active != new_crtc_state->active,
13099 "transitional active state does not match atomic hw state "
13100 "(expected %i, found %i)\n", new_crtc_state->active, intel_crtc->active);
4d20cd86 13101
e7c84544
ML
13102 for_each_encoder_on_crtc(dev, crtc, encoder) {
13103 enum pipe pipe;
4d20cd86 13104
e7c84544
ML
13105 active = encoder->get_hw_state(encoder, &pipe);
13106 I915_STATE_WARN(active != new_crtc_state->active,
13107 "[ENCODER:%i] active %i with crtc active %i\n",
13108 encoder->base.base.id, active, new_crtc_state->active);
4d20cd86 13109
e7c84544
ML
13110 I915_STATE_WARN(active && intel_crtc->pipe != pipe,
13111 "Encoder connected to wrong pipe %c\n",
13112 pipe_name(pipe));
4d20cd86 13113
e7c84544
ML
13114 if (active)
13115 encoder->get_config(encoder, pipe_config);
13116 }
53d9f4e9 13117
e7c84544
ML
13118 if (!new_crtc_state->active)
13119 return;
cfb23ed6 13120
e7c84544 13121 intel_pipe_config_sanity_check(dev_priv, pipe_config);
e3b247da 13122
e7c84544
ML
13123 sw_config = to_intel_crtc_state(crtc->state);
13124 if (!intel_pipe_config_compare(dev, sw_config,
13125 pipe_config, false)) {
13126 I915_STATE_WARN(1, "pipe state doesn't match!\n");
13127 intel_dump_pipe_config(intel_crtc, pipe_config,
13128 "[hw state]");
13129 intel_dump_pipe_config(intel_crtc, sw_config,
13130 "[sw state]");
8af6cf88
DV
13131 }
13132}
13133
91d1b4bd 13134static void
c0ead703
ML
13135verify_single_dpll_state(struct drm_i915_private *dev_priv,
13136 struct intel_shared_dpll *pll,
13137 struct drm_crtc *crtc,
13138 struct drm_crtc_state *new_state)
91d1b4bd 13139{
91d1b4bd 13140 struct intel_dpll_hw_state dpll_hw_state;
e7c84544
ML
13141 unsigned crtc_mask;
13142 bool active;
5358901f 13143
e7c84544 13144 memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
5358901f 13145
e7c84544 13146 DRM_DEBUG_KMS("%s\n", pll->name);
5358901f 13147
e7c84544 13148 active = pll->funcs.get_hw_state(dev_priv, pll, &dpll_hw_state);
5358901f 13149
e7c84544
ML
13150 if (!(pll->flags & INTEL_DPLL_ALWAYS_ON)) {
13151 I915_STATE_WARN(!pll->on && pll->active_mask,
13152 "pll in active use but not on in sw tracking\n");
13153 I915_STATE_WARN(pll->on && !pll->active_mask,
13154 "pll is on but not used by any active crtc\n");
13155 I915_STATE_WARN(pll->on != active,
13156 "pll on state mismatch (expected %i, found %i)\n",
13157 pll->on, active);
13158 }
5358901f 13159
e7c84544 13160 if (!crtc) {
2dd66ebd 13161 I915_STATE_WARN(pll->active_mask & ~pll->config.crtc_mask,
e7c84544
ML
13162 "more active pll users than references: %x vs %x\n",
13163 pll->active_mask, pll->config.crtc_mask);
5358901f 13164
e7c84544
ML
13165 return;
13166 }
13167
13168 crtc_mask = 1 << drm_crtc_index(crtc);
13169
13170 if (new_state->active)
13171 I915_STATE_WARN(!(pll->active_mask & crtc_mask),
13172 "pll active mismatch (expected pipe %c in active mask 0x%02x)\n",
13173 pipe_name(drm_crtc_index(crtc)), pll->active_mask);
13174 else
13175 I915_STATE_WARN(pll->active_mask & crtc_mask,
13176 "pll active mismatch (didn't expect pipe %c in active mask 0x%02x)\n",
13177 pipe_name(drm_crtc_index(crtc)), pll->active_mask);
2dd66ebd 13178
e7c84544
ML
13179 I915_STATE_WARN(!(pll->config.crtc_mask & crtc_mask),
13180 "pll enabled crtcs mismatch (expected 0x%x in 0x%02x)\n",
13181 crtc_mask, pll->config.crtc_mask);
66e985c0 13182
e7c84544
ML
13183 I915_STATE_WARN(pll->on && memcmp(&pll->config.hw_state,
13184 &dpll_hw_state,
13185 sizeof(dpll_hw_state)),
13186 "pll hw state mismatch\n");
13187}
13188
13189static void
c0ead703
ML
13190verify_shared_dpll_state(struct drm_device *dev, struct drm_crtc *crtc,
13191 struct drm_crtc_state *old_crtc_state,
13192 struct drm_crtc_state *new_crtc_state)
e7c84544
ML
13193{
13194 struct drm_i915_private *dev_priv = dev->dev_private;
13195 struct intel_crtc_state *old_state = to_intel_crtc_state(old_crtc_state);
13196 struct intel_crtc_state *new_state = to_intel_crtc_state(new_crtc_state);
13197
13198 if (new_state->shared_dpll)
c0ead703 13199 verify_single_dpll_state(dev_priv, new_state->shared_dpll, crtc, new_crtc_state);
e7c84544
ML
13200
13201 if (old_state->shared_dpll &&
13202 old_state->shared_dpll != new_state->shared_dpll) {
13203 unsigned crtc_mask = 1 << drm_crtc_index(crtc);
13204 struct intel_shared_dpll *pll = old_state->shared_dpll;
13205
13206 I915_STATE_WARN(pll->active_mask & crtc_mask,
13207 "pll active mismatch (didn't expect pipe %c in active mask)\n",
13208 pipe_name(drm_crtc_index(crtc)));
13209 I915_STATE_WARN(pll->config.crtc_mask & crtc_mask,
13210 "pll enabled crtcs mismatch (found %x in enabled mask)\n",
13211 pipe_name(drm_crtc_index(crtc)));
5358901f 13212 }
8af6cf88
DV
13213}
13214
e7c84544 13215static void
c0ead703 13216intel_modeset_verify_crtc(struct drm_crtc *crtc,
e7c84544
ML
13217 struct drm_crtc_state *old_state,
13218 struct drm_crtc_state *new_state)
13219{
5a21b665
DV
13220 if (!needs_modeset(new_state) &&
13221 !to_intel_crtc_state(new_state)->update_pipe)
13222 return;
13223
c0ead703 13224 verify_wm_state(crtc, new_state);
5a21b665 13225 verify_connector_state(crtc->dev, crtc);
c0ead703
ML
13226 verify_crtc_state(crtc, old_state, new_state);
13227 verify_shared_dpll_state(crtc->dev, crtc, old_state, new_state);
e7c84544
ML
13228}
13229
13230static void
c0ead703 13231verify_disabled_dpll_state(struct drm_device *dev)
e7c84544
ML
13232{
13233 struct drm_i915_private *dev_priv = dev->dev_private;
13234 int i;
13235
13236 for (i = 0; i < dev_priv->num_shared_dpll; i++)
c0ead703 13237 verify_single_dpll_state(dev_priv, &dev_priv->shared_dplls[i], NULL, NULL);
e7c84544
ML
13238}
13239
13240static void
c0ead703 13241intel_modeset_verify_disabled(struct drm_device *dev)
e7c84544 13242{
c0ead703
ML
13243 verify_encoder_state(dev);
13244 verify_connector_state(dev, NULL);
13245 verify_disabled_dpll_state(dev);
e7c84544
ML
13246}
13247
80715b2f
VS
13248static void update_scanline_offset(struct intel_crtc *crtc)
13249{
13250 struct drm_device *dev = crtc->base.dev;
13251
13252 /*
13253 * The scanline counter increments at the leading edge of hsync.
13254 *
13255 * On most platforms it starts counting from vtotal-1 on the
13256 * first active line. That means the scanline counter value is
13257 * always one less than what we would expect. Ie. just after
13258 * start of vblank, which also occurs at start of hsync (on the
13259 * last active line), the scanline counter will read vblank_start-1.
13260 *
13261 * On gen2 the scanline counter starts counting from 1 instead
13262 * of vtotal-1, so we have to subtract one (or rather add vtotal-1
13263 * to keep the value positive), instead of adding one.
13264 *
13265 * On HSW+ the behaviour of the scanline counter depends on the output
13266 * type. For DP ports it behaves like most other platforms, but on HDMI
13267 * there's an extra 1 line difference. So we need to add two instead of
13268 * one to the value.
13269 */
13270 if (IS_GEN2(dev)) {
124abe07 13271 const struct drm_display_mode *adjusted_mode = &crtc->config->base.adjusted_mode;
80715b2f
VS
13272 int vtotal;
13273
124abe07
VS
13274 vtotal = adjusted_mode->crtc_vtotal;
13275 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
80715b2f
VS
13276 vtotal /= 2;
13277
13278 crtc->scanline_offset = vtotal - 1;
13279 } else if (HAS_DDI(dev) &&
409ee761 13280 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI)) {
80715b2f
VS
13281 crtc->scanline_offset = 2;
13282 } else
13283 crtc->scanline_offset = 1;
13284}
13285
ad421372 13286static void intel_modeset_clear_plls(struct drm_atomic_state *state)
ed6739ef 13287{
225da59b 13288 struct drm_device *dev = state->dev;
ed6739ef 13289 struct drm_i915_private *dev_priv = to_i915(dev);
ad421372 13290 struct intel_shared_dpll_config *shared_dpll = NULL;
0a9ab303
ACO
13291 struct drm_crtc *crtc;
13292 struct drm_crtc_state *crtc_state;
0a9ab303 13293 int i;
ed6739ef
ACO
13294
13295 if (!dev_priv->display.crtc_compute_clock)
ad421372 13296 return;
ed6739ef 13297
0a9ab303 13298 for_each_crtc_in_state(state, crtc, crtc_state, i) {
fb1a38a9 13299 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8106ddbd
ACO
13300 struct intel_shared_dpll *old_dpll =
13301 to_intel_crtc_state(crtc->state)->shared_dpll;
0a9ab303 13302
fb1a38a9 13303 if (!needs_modeset(crtc_state))
225da59b
ACO
13304 continue;
13305
8106ddbd 13306 to_intel_crtc_state(crtc_state)->shared_dpll = NULL;
fb1a38a9 13307
8106ddbd 13308 if (!old_dpll)
fb1a38a9 13309 continue;
0a9ab303 13310
ad421372
ML
13311 if (!shared_dpll)
13312 shared_dpll = intel_atomic_get_shared_dpll_state(state);
ed6739ef 13313
8106ddbd 13314 intel_shared_dpll_config_put(shared_dpll, old_dpll, intel_crtc);
ad421372 13315 }
ed6739ef
ACO
13316}
13317
99d736a2
ML
13318/*
13319 * This implements the workaround described in the "notes" section of the mode
13320 * set sequence documentation. When going from no pipes or single pipe to
13321 * multiple pipes, and planes are enabled after the pipe, we need to wait at
13322 * least 2 vblanks on the first pipe before enabling planes on the second pipe.
13323 */
13324static int haswell_mode_set_planes_workaround(struct drm_atomic_state *state)
13325{
13326 struct drm_crtc_state *crtc_state;
13327 struct intel_crtc *intel_crtc;
13328 struct drm_crtc *crtc;
13329 struct intel_crtc_state *first_crtc_state = NULL;
13330 struct intel_crtc_state *other_crtc_state = NULL;
13331 enum pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
13332 int i;
13333
13334 /* look at all crtc's that are going to be enabled in during modeset */
13335 for_each_crtc_in_state(state, crtc, crtc_state, i) {
13336 intel_crtc = to_intel_crtc(crtc);
13337
13338 if (!crtc_state->active || !needs_modeset(crtc_state))
13339 continue;
13340
13341 if (first_crtc_state) {
13342 other_crtc_state = to_intel_crtc_state(crtc_state);
13343 break;
13344 } else {
13345 first_crtc_state = to_intel_crtc_state(crtc_state);
13346 first_pipe = intel_crtc->pipe;
13347 }
13348 }
13349
13350 /* No workaround needed? */
13351 if (!first_crtc_state)
13352 return 0;
13353
13354 /* w/a possibly needed, check how many crtc's are already enabled. */
13355 for_each_intel_crtc(state->dev, intel_crtc) {
13356 struct intel_crtc_state *pipe_config;
13357
13358 pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
13359 if (IS_ERR(pipe_config))
13360 return PTR_ERR(pipe_config);
13361
13362 pipe_config->hsw_workaround_pipe = INVALID_PIPE;
13363
13364 if (!pipe_config->base.active ||
13365 needs_modeset(&pipe_config->base))
13366 continue;
13367
13368 /* 2 or more enabled crtcs means no need for w/a */
13369 if (enabled_pipe != INVALID_PIPE)
13370 return 0;
13371
13372 enabled_pipe = intel_crtc->pipe;
13373 }
13374
13375 if (enabled_pipe != INVALID_PIPE)
13376 first_crtc_state->hsw_workaround_pipe = enabled_pipe;
13377 else if (other_crtc_state)
13378 other_crtc_state->hsw_workaround_pipe = first_pipe;
13379
13380 return 0;
13381}
13382
27c329ed
ML
13383static int intel_modeset_all_pipes(struct drm_atomic_state *state)
13384{
13385 struct drm_crtc *crtc;
13386 struct drm_crtc_state *crtc_state;
13387 int ret = 0;
13388
13389 /* add all active pipes to the state */
13390 for_each_crtc(state->dev, crtc) {
13391 crtc_state = drm_atomic_get_crtc_state(state, crtc);
13392 if (IS_ERR(crtc_state))
13393 return PTR_ERR(crtc_state);
13394
13395 if (!crtc_state->active || needs_modeset(crtc_state))
13396 continue;
13397
13398 crtc_state->mode_changed = true;
13399
13400 ret = drm_atomic_add_affected_connectors(state, crtc);
13401 if (ret)
13402 break;
13403
13404 ret = drm_atomic_add_affected_planes(state, crtc);
13405 if (ret)
13406 break;
13407 }
13408
13409 return ret;
13410}
13411
c347a676 13412static int intel_modeset_checks(struct drm_atomic_state *state)
054518dd 13413{
565602d7
ML
13414 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
13415 struct drm_i915_private *dev_priv = state->dev->dev_private;
13416 struct drm_crtc *crtc;
13417 struct drm_crtc_state *crtc_state;
13418 int ret = 0, i;
054518dd 13419
b359283a
ML
13420 if (!check_digital_port_conflicts(state)) {
13421 DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
13422 return -EINVAL;
13423 }
13424
565602d7
ML
13425 intel_state->modeset = true;
13426 intel_state->active_crtcs = dev_priv->active_crtcs;
13427
13428 for_each_crtc_in_state(state, crtc, crtc_state, i) {
13429 if (crtc_state->active)
13430 intel_state->active_crtcs |= 1 << i;
13431 else
13432 intel_state->active_crtcs &= ~(1 << i);
8b4a7d05
MR
13433
13434 if (crtc_state->active != crtc->state->active)
13435 intel_state->active_pipe_changes |= drm_crtc_mask(crtc);
565602d7
ML
13436 }
13437
054518dd
ACO
13438 /*
13439 * See if the config requires any additional preparation, e.g.
13440 * to adjust global state with pipes off. We need to do this
13441 * here so we can get the modeset_pipe updated config for the new
13442 * mode set on this crtc. For other crtcs we need to use the
13443 * adjusted_mode bits in the crtc directly.
13444 */
27c329ed 13445 if (dev_priv->display.modeset_calc_cdclk) {
c89e39f3 13446 if (!intel_state->cdclk_pll_vco)
63911d72 13447 intel_state->cdclk_pll_vco = dev_priv->cdclk_pll.vco;
b2045352
VS
13448 if (!intel_state->cdclk_pll_vco)
13449 intel_state->cdclk_pll_vco = dev_priv->skl_preferred_vco_freq;
c89e39f3 13450
27c329ed 13451 ret = dev_priv->display.modeset_calc_cdclk(state);
c89e39f3
CT
13452 if (ret < 0)
13453 return ret;
27c329ed 13454
c89e39f3 13455 if (intel_state->dev_cdclk != dev_priv->cdclk_freq ||
63911d72 13456 intel_state->cdclk_pll_vco != dev_priv->cdclk_pll.vco)
27c329ed
ML
13457 ret = intel_modeset_all_pipes(state);
13458
13459 if (ret < 0)
054518dd 13460 return ret;
e8788cbc
ML
13461
13462 DRM_DEBUG_KMS("New cdclk calculated to be atomic %u, actual %u\n",
13463 intel_state->cdclk, intel_state->dev_cdclk);
27c329ed 13464 } else
1a617b77 13465 to_intel_atomic_state(state)->cdclk = dev_priv->atomic_cdclk_freq;
054518dd 13466
ad421372 13467 intel_modeset_clear_plls(state);
054518dd 13468
565602d7 13469 if (IS_HASWELL(dev_priv))
ad421372 13470 return haswell_mode_set_planes_workaround(state);
99d736a2 13471
ad421372 13472 return 0;
c347a676
ACO
13473}
13474
aa363136
MR
13475/*
13476 * Handle calculation of various watermark data at the end of the atomic check
13477 * phase. The code here should be run after the per-crtc and per-plane 'check'
13478 * handlers to ensure that all derived state has been updated.
13479 */
55994c2c 13480static int calc_watermark_data(struct drm_atomic_state *state)
aa363136
MR
13481{
13482 struct drm_device *dev = state->dev;
98d39494 13483 struct drm_i915_private *dev_priv = to_i915(dev);
98d39494
MR
13484
13485 /* Is there platform-specific watermark information to calculate? */
13486 if (dev_priv->display.compute_global_watermarks)
55994c2c
MR
13487 return dev_priv->display.compute_global_watermarks(state);
13488
13489 return 0;
aa363136
MR
13490}
13491
74c090b1
ML
13492/**
13493 * intel_atomic_check - validate state object
13494 * @dev: drm device
13495 * @state: state to validate
13496 */
13497static int intel_atomic_check(struct drm_device *dev,
13498 struct drm_atomic_state *state)
c347a676 13499{
dd8b3bdb 13500 struct drm_i915_private *dev_priv = to_i915(dev);
aa363136 13501 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
c347a676
ACO
13502 struct drm_crtc *crtc;
13503 struct drm_crtc_state *crtc_state;
13504 int ret, i;
61333b60 13505 bool any_ms = false;
c347a676 13506
74c090b1 13507 ret = drm_atomic_helper_check_modeset(dev, state);
054518dd
ACO
13508 if (ret)
13509 return ret;
13510
c347a676 13511 for_each_crtc_in_state(state, crtc, crtc_state, i) {
cfb23ed6
ML
13512 struct intel_crtc_state *pipe_config =
13513 to_intel_crtc_state(crtc_state);
1ed51de9
DV
13514
13515 /* Catch I915_MODE_FLAG_INHERITED */
13516 if (crtc_state->mode.private_flags != crtc->state->mode.private_flags)
13517 crtc_state->mode_changed = true;
cfb23ed6 13518
af4a879e 13519 if (!needs_modeset(crtc_state))
c347a676
ACO
13520 continue;
13521
af4a879e
DV
13522 if (!crtc_state->enable) {
13523 any_ms = true;
cfb23ed6 13524 continue;
af4a879e 13525 }
cfb23ed6 13526
26495481
DV
13527 /* FIXME: For only active_changed we shouldn't need to do any
13528 * state recomputation at all. */
13529
1ed51de9
DV
13530 ret = drm_atomic_add_affected_connectors(state, crtc);
13531 if (ret)
13532 return ret;
b359283a 13533
cfb23ed6 13534 ret = intel_modeset_pipe_config(crtc, pipe_config);
25aa1c39
ML
13535 if (ret) {
13536 intel_dump_pipe_config(to_intel_crtc(crtc),
13537 pipe_config, "[failed]");
c347a676 13538 return ret;
25aa1c39 13539 }
c347a676 13540
73831236 13541 if (i915.fastboot &&
dd8b3bdb 13542 intel_pipe_config_compare(dev,
cfb23ed6 13543 to_intel_crtc_state(crtc->state),
1ed51de9 13544 pipe_config, true)) {
26495481 13545 crtc_state->mode_changed = false;
bfd16b2a 13546 to_intel_crtc_state(crtc_state)->update_pipe = true;
26495481
DV
13547 }
13548
af4a879e 13549 if (needs_modeset(crtc_state))
26495481 13550 any_ms = true;
cfb23ed6 13551
af4a879e
DV
13552 ret = drm_atomic_add_affected_planes(state, crtc);
13553 if (ret)
13554 return ret;
61333b60 13555
26495481
DV
13556 intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
13557 needs_modeset(crtc_state) ?
13558 "[modeset]" : "[fastset]");
c347a676
ACO
13559 }
13560
61333b60
ML
13561 if (any_ms) {
13562 ret = intel_modeset_checks(state);
13563
13564 if (ret)
13565 return ret;
27c329ed 13566 } else
dd8b3bdb 13567 intel_state->cdclk = dev_priv->cdclk_freq;
76305b1a 13568
dd8b3bdb 13569 ret = drm_atomic_helper_check_planes(dev, state);
aa363136
MR
13570 if (ret)
13571 return ret;
13572
f51be2e0 13573 intel_fbc_choose_crtc(dev_priv, state);
55994c2c 13574 return calc_watermark_data(state);
054518dd
ACO
13575}
13576
5008e874
ML
13577static int intel_atomic_prepare_commit(struct drm_device *dev,
13578 struct drm_atomic_state *state,
81072bfd 13579 bool nonblock)
5008e874 13580{
7580d774
ML
13581 struct drm_i915_private *dev_priv = dev->dev_private;
13582 struct drm_plane_state *plane_state;
5008e874 13583 struct drm_crtc_state *crtc_state;
7580d774 13584 struct drm_plane *plane;
5008e874
ML
13585 struct drm_crtc *crtc;
13586 int i, ret;
13587
5a21b665
DV
13588 for_each_crtc_in_state(state, crtc, crtc_state, i) {
13589 if (state->legacy_cursor_update)
a6747b73
ML
13590 continue;
13591
5a21b665
DV
13592 ret = intel_crtc_wait_for_pending_flips(crtc);
13593 if (ret)
13594 return ret;
5008e874 13595
5a21b665
DV
13596 if (atomic_read(&to_intel_crtc(crtc)->unpin_work_count) >= 2)
13597 flush_workqueue(dev_priv->wq);
d55dbd06
ML
13598 }
13599
f935675f
ML
13600 ret = mutex_lock_interruptible(&dev->struct_mutex);
13601 if (ret)
13602 return ret;
13603
5008e874 13604 ret = drm_atomic_helper_prepare_planes(dev, state);
f7e5838b 13605 mutex_unlock(&dev->struct_mutex);
7580d774 13606
21daaeee 13607 if (!ret && !nonblock) {
7580d774
ML
13608 for_each_plane_in_state(state, plane, plane_state, i) {
13609 struct intel_plane_state *intel_plane_state =
13610 to_intel_plane_state(plane_state);
13611
13612 if (!intel_plane_state->wait_req)
13613 continue;
13614
13615 ret = __i915_wait_request(intel_plane_state->wait_req,
299259a3 13616 true, NULL, NULL);
f7e5838b 13617 if (ret) {
f4457ae7
CW
13618 /* Any hang should be swallowed by the wait */
13619 WARN_ON(ret == -EIO);
f7e5838b
CW
13620 mutex_lock(&dev->struct_mutex);
13621 drm_atomic_helper_cleanup_planes(dev, state);
13622 mutex_unlock(&dev->struct_mutex);
7580d774 13623 break;
f7e5838b 13624 }
7580d774 13625 }
7580d774 13626 }
5008e874
ML
13627
13628 return ret;
13629}
13630
a2991414
ML
13631u32 intel_crtc_get_vblank_counter(struct intel_crtc *crtc)
13632{
13633 struct drm_device *dev = crtc->base.dev;
13634
13635 if (!dev->max_vblank_count)
13636 return drm_accurate_vblank_count(&crtc->base);
13637
13638 return dev->driver->get_vblank_counter(dev, crtc->pipe);
13639}
13640
5a21b665
DV
13641static void intel_atomic_wait_for_vblanks(struct drm_device *dev,
13642 struct drm_i915_private *dev_priv,
13643 unsigned crtc_mask)
e8861675 13644{
5a21b665
DV
13645 unsigned last_vblank_count[I915_MAX_PIPES];
13646 enum pipe pipe;
13647 int ret;
e8861675 13648
5a21b665
DV
13649 if (!crtc_mask)
13650 return;
e8861675 13651
5a21b665
DV
13652 for_each_pipe(dev_priv, pipe) {
13653 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
e8861675 13654
5a21b665 13655 if (!((1 << pipe) & crtc_mask))
e8861675
ML
13656 continue;
13657
5a21b665
DV
13658 ret = drm_crtc_vblank_get(crtc);
13659 if (WARN_ON(ret != 0)) {
13660 crtc_mask &= ~(1 << pipe);
13661 continue;
e8861675
ML
13662 }
13663
5a21b665 13664 last_vblank_count[pipe] = drm_crtc_vblank_count(crtc);
e8861675
ML
13665 }
13666
5a21b665
DV
13667 for_each_pipe(dev_priv, pipe) {
13668 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
13669 long lret;
e8861675 13670
5a21b665
DV
13671 if (!((1 << pipe) & crtc_mask))
13672 continue;
d55dbd06 13673
5a21b665
DV
13674 lret = wait_event_timeout(dev->vblank[pipe].queue,
13675 last_vblank_count[pipe] !=
13676 drm_crtc_vblank_count(crtc),
13677 msecs_to_jiffies(50));
d55dbd06 13678
5a21b665 13679 WARN(!lret, "pipe %c vblank wait timed out\n", pipe_name(pipe));
d55dbd06 13680
5a21b665 13681 drm_crtc_vblank_put(crtc);
d55dbd06
ML
13682 }
13683}
13684
5a21b665 13685static bool needs_vblank_wait(struct intel_crtc_state *crtc_state)
a6747b73 13686{
5a21b665
DV
13687 /* fb updated, need to unpin old fb */
13688 if (crtc_state->fb_changed)
13689 return true;
a6747b73 13690
5a21b665
DV
13691 /* wm changes, need vblank before final wm's */
13692 if (crtc_state->update_wm_post)
13693 return true;
a6747b73 13694
5a21b665
DV
13695 /*
13696 * cxsr is re-enabled after vblank.
13697 * This is already handled by crtc_state->update_wm_post,
13698 * but added for clarity.
13699 */
13700 if (crtc_state->disable_cxsr)
13701 return true;
a6747b73 13702
5a21b665 13703 return false;
e8861675
ML
13704}
13705
94f05024 13706static void intel_atomic_commit_tail(struct drm_atomic_state *state)
a6778b3c 13707{
94f05024 13708 struct drm_device *dev = state->dev;
565602d7 13709 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
fbee40df 13710 struct drm_i915_private *dev_priv = dev->dev_private;
29ceb0e6 13711 struct drm_crtc_state *old_crtc_state;
7580d774 13712 struct drm_crtc *crtc;
5a21b665 13713 struct intel_crtc_state *intel_cstate;
94f05024
DV
13714 struct drm_plane *plane;
13715 struct drm_plane_state *plane_state;
5a21b665
DV
13716 bool hw_check = intel_state->modeset;
13717 unsigned long put_domains[I915_MAX_PIPES] = {};
13718 unsigned crtc_vblank_mask = 0;
94f05024 13719 int i, ret;
a6778b3c 13720
94f05024
DV
13721 for_each_plane_in_state(state, plane, plane_state, i) {
13722 struct intel_plane_state *intel_plane_state =
13723 to_intel_plane_state(plane_state);
ea0000f0 13724
94f05024
DV
13725 if (!intel_plane_state->wait_req)
13726 continue;
d4afb8cc 13727
94f05024
DV
13728 ret = __i915_wait_request(intel_plane_state->wait_req,
13729 true, NULL, NULL);
13730 /* EIO should be eaten, and we can't get interrupted in the
13731 * worker, and blocking commits have waited already. */
13732 WARN_ON(ret);
13733 }
1c5e19f8 13734
ea0000f0
DV
13735 drm_atomic_helper_wait_for_dependencies(state);
13736
565602d7
ML
13737 if (intel_state->modeset) {
13738 memcpy(dev_priv->min_pixclk, intel_state->min_pixclk,
13739 sizeof(intel_state->min_pixclk));
13740 dev_priv->active_crtcs = intel_state->active_crtcs;
1a617b77 13741 dev_priv->atomic_cdclk_freq = intel_state->cdclk;
5a21b665
DV
13742
13743 intel_display_power_get(dev_priv, POWER_DOMAIN_MODESET);
565602d7
ML
13744 }
13745
29ceb0e6 13746 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
a539205a
ML
13747 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13748
5a21b665
DV
13749 if (needs_modeset(crtc->state) ||
13750 to_intel_crtc_state(crtc->state)->update_pipe) {
13751 hw_check = true;
13752
13753 put_domains[to_intel_crtc(crtc)->pipe] =
13754 modeset_get_crtc_power_domains(crtc,
13755 to_intel_crtc_state(crtc->state));
13756 }
13757
61333b60
ML
13758 if (!needs_modeset(crtc->state))
13759 continue;
13760
29ceb0e6 13761 intel_pre_plane_update(to_intel_crtc_state(old_crtc_state));
460da916 13762
29ceb0e6
VS
13763 if (old_crtc_state->active) {
13764 intel_crtc_disable_planes(crtc, old_crtc_state->plane_mask);
a539205a 13765 dev_priv->display.crtc_disable(crtc);
eddfcbcd 13766 intel_crtc->active = false;
58f9c0bc 13767 intel_fbc_disable(intel_crtc);
eddfcbcd 13768 intel_disable_shared_dpll(intel_crtc);
9bbc8258
VS
13769
13770 /*
13771 * Underruns don't always raise
13772 * interrupts, so check manually.
13773 */
13774 intel_check_cpu_fifo_underruns(dev_priv);
13775 intel_check_pch_fifo_underruns(dev_priv);
b9001114
ML
13776
13777 if (!crtc->state->active)
13778 intel_update_watermarks(crtc);
a539205a 13779 }
b8cecdf5 13780 }
7758a113 13781
ea9d758d
DV
13782 /* Only after disabling all output pipelines that will be changed can we
13783 * update the the output configuration. */
4740b0f2 13784 intel_modeset_update_crtc_state(state);
f6e5b160 13785
565602d7 13786 if (intel_state->modeset) {
4740b0f2 13787 drm_atomic_helper_update_legacy_modeset_state(state->dev, state);
33c8df89
ML
13788
13789 if (dev_priv->display.modeset_commit_cdclk &&
c89e39f3 13790 (intel_state->dev_cdclk != dev_priv->cdclk_freq ||
63911d72 13791 intel_state->cdclk_pll_vco != dev_priv->cdclk_pll.vco))
33c8df89 13792 dev_priv->display.modeset_commit_cdclk(state);
f6d1973d 13793
c0ead703 13794 intel_modeset_verify_disabled(dev);
4740b0f2 13795 }
47fab737 13796
a6778b3c 13797 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
29ceb0e6 13798 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
f6ac4b2a
ML
13799 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13800 bool modeset = needs_modeset(crtc->state);
5a21b665
DV
13801 struct intel_crtc_state *pipe_config =
13802 to_intel_crtc_state(crtc->state);
9f836f90 13803
f6ac4b2a 13804 if (modeset && crtc->state->active) {
a539205a
ML
13805 update_scanline_offset(to_intel_crtc(crtc));
13806 dev_priv->display.crtc_enable(crtc);
13807 }
80715b2f 13808
1f7528c4
DV
13809 /* Complete events for now disable pipes here. */
13810 if (modeset && !crtc->state->active && crtc->state->event) {
13811 spin_lock_irq(&dev->event_lock);
13812 drm_crtc_send_vblank_event(crtc, crtc->state->event);
13813 spin_unlock_irq(&dev->event_lock);
13814
13815 crtc->state->event = NULL;
13816 }
13817
f6ac4b2a 13818 if (!modeset)
29ceb0e6 13819 intel_pre_plane_update(to_intel_crtc_state(old_crtc_state));
f6ac4b2a 13820
5a21b665
DV
13821 if (crtc->state->active &&
13822 drm_atomic_get_existing_plane_state(state, crtc->primary))
faf68d92 13823 intel_fbc_enable(intel_crtc, pipe_config, to_intel_plane_state(crtc->primary->state));
5a21b665 13824
1f7528c4 13825 if (crtc->state->active)
5a21b665 13826 drm_atomic_helper_commit_planes_on_crtc(old_crtc_state);
f6d1973d 13827
5a21b665
DV
13828 if (pipe_config->base.active && needs_vblank_wait(pipe_config))
13829 crtc_vblank_mask |= 1 << i;
177246a8
MR
13830 }
13831
94f05024
DV
13832 /* FIXME: We should call drm_atomic_helper_commit_hw_done() here
13833 * already, but still need the state for the delayed optimization. To
13834 * fix this:
13835 * - wrap the optimization/post_plane_update stuff into a per-crtc work.
13836 * - schedule that vblank worker _before_ calling hw_done
13837 * - at the start of commit_tail, cancel it _synchrously
13838 * - switch over to the vblank wait helper in the core after that since
13839 * we don't need out special handling any more.
13840 */
5a21b665
DV
13841 if (!state->legacy_cursor_update)
13842 intel_atomic_wait_for_vblanks(dev, dev_priv, crtc_vblank_mask);
13843
13844 /*
13845 * Now that the vblank has passed, we can go ahead and program the
13846 * optimal watermarks on platforms that need two-step watermark
13847 * programming.
13848 *
13849 * TODO: Move this (and other cleanup) to an async worker eventually.
13850 */
13851 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
13852 intel_cstate = to_intel_crtc_state(crtc->state);
13853
13854 if (dev_priv->display.optimize_watermarks)
13855 dev_priv->display.optimize_watermarks(intel_cstate);
13856 }
13857
13858 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
13859 intel_post_plane_update(to_intel_crtc_state(old_crtc_state));
13860
13861 if (put_domains[i])
13862 modeset_put_power_domains(dev_priv, put_domains[i]);
13863
13864 intel_modeset_verify_crtc(crtc, old_crtc_state, crtc->state);
13865 }
13866
94f05024
DV
13867 drm_atomic_helper_commit_hw_done(state);
13868
5a21b665
DV
13869 if (intel_state->modeset)
13870 intel_display_power_put(dev_priv, POWER_DOMAIN_MODESET);
13871
13872 mutex_lock(&dev->struct_mutex);
13873 drm_atomic_helper_cleanup_planes(dev, state);
13874 mutex_unlock(&dev->struct_mutex);
13875
ea0000f0
DV
13876 drm_atomic_helper_commit_cleanup_done(state);
13877
ee165b1a 13878 drm_atomic_state_free(state);
f30da187 13879
75714940
MK
13880 /* As one of the primary mmio accessors, KMS has a high likelihood
13881 * of triggering bugs in unclaimed access. After we finish
13882 * modesetting, see if an error has been flagged, and if so
13883 * enable debugging for the next modeset - and hope we catch
13884 * the culprit.
13885 *
13886 * XXX note that we assume display power is on at this point.
13887 * This might hold true now but we need to add pm helper to check
13888 * unclaimed only when the hardware is on, as atomic commits
13889 * can happen also when the device is completely off.
13890 */
13891 intel_uncore_arm_unclaimed_mmio_detection(dev_priv);
94f05024
DV
13892}
13893
13894static void intel_atomic_commit_work(struct work_struct *work)
13895{
13896 struct drm_atomic_state *state = container_of(work,
13897 struct drm_atomic_state,
13898 commit_work);
13899 intel_atomic_commit_tail(state);
13900}
13901
6c9c1b38
DV
13902static void intel_atomic_track_fbs(struct drm_atomic_state *state)
13903{
13904 struct drm_plane_state *old_plane_state;
13905 struct drm_plane *plane;
13906 struct drm_i915_gem_object *obj, *old_obj;
13907 struct intel_plane *intel_plane;
13908 int i;
13909
13910 mutex_lock(&state->dev->struct_mutex);
13911 for_each_plane_in_state(state, plane, old_plane_state, i) {
13912 obj = intel_fb_obj(plane->state->fb);
13913 old_obj = intel_fb_obj(old_plane_state->fb);
13914 intel_plane = to_intel_plane(plane);
13915
13916 i915_gem_track_fb(old_obj, obj, intel_plane->frontbuffer_bit);
13917 }
13918 mutex_unlock(&state->dev->struct_mutex);
13919}
13920
94f05024
DV
13921/**
13922 * intel_atomic_commit - commit validated state object
13923 * @dev: DRM device
13924 * @state: the top-level driver state object
13925 * @nonblock: nonblocking commit
13926 *
13927 * This function commits a top-level state object that has been validated
13928 * with drm_atomic_helper_check().
13929 *
13930 * FIXME: Atomic modeset support for i915 is not yet complete. At the moment
13931 * nonblocking commits are only safe for pure plane updates. Everything else
13932 * should work though.
13933 *
13934 * RETURNS
13935 * Zero for success or -errno.
13936 */
13937static int intel_atomic_commit(struct drm_device *dev,
13938 struct drm_atomic_state *state,
13939 bool nonblock)
13940{
13941 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
13942 struct drm_i915_private *dev_priv = dev->dev_private;
13943 int ret = 0;
13944
13945 if (intel_state->modeset && nonblock) {
13946 DRM_DEBUG_KMS("nonblocking commit for modeset not yet implemented.\n");
13947 return -EINVAL;
13948 }
13949
13950 ret = drm_atomic_helper_setup_commit(state, nonblock);
13951 if (ret)
13952 return ret;
13953
13954 INIT_WORK(&state->commit_work, intel_atomic_commit_work);
13955
13956 ret = intel_atomic_prepare_commit(dev, state, nonblock);
13957 if (ret) {
13958 DRM_DEBUG_ATOMIC("Preparing state failed with %i\n", ret);
13959 return ret;
13960 }
13961
13962 drm_atomic_helper_swap_state(state, true);
13963 dev_priv->wm.distrust_bios_wm = false;
13964 dev_priv->wm.skl_results = intel_state->wm_results;
13965 intel_shared_dpll_commit(state);
6c9c1b38 13966 intel_atomic_track_fbs(state);
94f05024
DV
13967
13968 if (nonblock)
13969 queue_work(system_unbound_wq, &state->commit_work);
13970 else
13971 intel_atomic_commit_tail(state);
75714940 13972
74c090b1 13973 return 0;
7f27126e
JB
13974}
13975
c0c36b94
CW
13976void intel_crtc_restore_mode(struct drm_crtc *crtc)
13977{
83a57153
ACO
13978 struct drm_device *dev = crtc->dev;
13979 struct drm_atomic_state *state;
e694eb02 13980 struct drm_crtc_state *crtc_state;
2bfb4627 13981 int ret;
83a57153
ACO
13982
13983 state = drm_atomic_state_alloc(dev);
13984 if (!state) {
78108b7c
VS
13985 DRM_DEBUG_KMS("[CRTC:%d:%s] crtc restore failed, out of memory",
13986 crtc->base.id, crtc->name);
83a57153
ACO
13987 return;
13988 }
13989
e694eb02 13990 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
83a57153 13991
e694eb02
ML
13992retry:
13993 crtc_state = drm_atomic_get_crtc_state(state, crtc);
13994 ret = PTR_ERR_OR_ZERO(crtc_state);
13995 if (!ret) {
13996 if (!crtc_state->active)
13997 goto out;
83a57153 13998
e694eb02 13999 crtc_state->mode_changed = true;
74c090b1 14000 ret = drm_atomic_commit(state);
83a57153
ACO
14001 }
14002
e694eb02
ML
14003 if (ret == -EDEADLK) {
14004 drm_atomic_state_clear(state);
14005 drm_modeset_backoff(state->acquire_ctx);
14006 goto retry;
4ed9fb37 14007 }
4be07317 14008
2bfb4627 14009 if (ret)
e694eb02 14010out:
2bfb4627 14011 drm_atomic_state_free(state);
c0c36b94
CW
14012}
14013
25c5b266
DV
14014#undef for_each_intel_crtc_masked
14015
f6e5b160 14016static const struct drm_crtc_funcs intel_crtc_funcs = {
82cf435b 14017 .gamma_set = drm_atomic_helper_legacy_gamma_set,
74c090b1 14018 .set_config = drm_atomic_helper_set_config,
82cf435b 14019 .set_property = drm_atomic_helper_crtc_set_property,
f6e5b160 14020 .destroy = intel_crtc_destroy,
527b6abe 14021 .page_flip = intel_crtc_page_flip,
1356837e
MR
14022 .atomic_duplicate_state = intel_crtc_duplicate_state,
14023 .atomic_destroy_state = intel_crtc_destroy_state,
f6e5b160
CW
14024};
14025
6beb8c23
MR
14026/**
14027 * intel_prepare_plane_fb - Prepare fb for usage on plane
14028 * @plane: drm plane to prepare for
14029 * @fb: framebuffer to prepare for presentation
14030 *
14031 * Prepares a framebuffer for usage on a display plane. Generally this
14032 * involves pinning the underlying object and updating the frontbuffer tracking
14033 * bits. Some older platforms need special physical address handling for
14034 * cursor planes.
14035 *
f935675f
ML
14036 * Must be called with struct_mutex held.
14037 *
6beb8c23
MR
14038 * Returns 0 on success, negative error code on failure.
14039 */
14040int
14041intel_prepare_plane_fb(struct drm_plane *plane,
d136dfee 14042 const struct drm_plane_state *new_state)
465c120c
MR
14043{
14044 struct drm_device *dev = plane->dev;
844f9111 14045 struct drm_framebuffer *fb = new_state->fb;
6beb8c23 14046 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
1ee49399 14047 struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->state->fb);
c37efb99 14048 struct reservation_object *resv;
6beb8c23 14049 int ret = 0;
465c120c 14050
1ee49399 14051 if (!obj && !old_obj)
465c120c
MR
14052 return 0;
14053
5008e874
ML
14054 if (old_obj) {
14055 struct drm_crtc_state *crtc_state =
14056 drm_atomic_get_existing_crtc_state(new_state->state, plane->state->crtc);
14057
14058 /* Big Hammer, we also need to ensure that any pending
14059 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
14060 * current scanout is retired before unpinning the old
14061 * framebuffer. Note that we rely on userspace rendering
14062 * into the buffer attached to the pipe they are waiting
14063 * on. If not, userspace generates a GPU hang with IPEHR
14064 * point to the MI_WAIT_FOR_EVENT.
14065 *
14066 * This should only fail upon a hung GPU, in which case we
14067 * can safely continue.
14068 */
14069 if (needs_modeset(crtc_state))
14070 ret = i915_gem_object_wait_rendering(old_obj, true);
f4457ae7
CW
14071 if (ret) {
14072 /* GPU hangs should have been swallowed by the wait */
14073 WARN_ON(ret == -EIO);
f935675f 14074 return ret;
f4457ae7 14075 }
5008e874
ML
14076 }
14077
c37efb99
CW
14078 if (!obj)
14079 return 0;
14080
5a21b665 14081 /* For framebuffer backed by dmabuf, wait for fence */
c37efb99
CW
14082 resv = i915_gem_object_get_dmabuf_resv(obj);
14083 if (resv) {
5a21b665
DV
14084 long lret;
14085
c37efb99 14086 lret = reservation_object_wait_timeout_rcu(resv, false, true,
5a21b665
DV
14087 MAX_SCHEDULE_TIMEOUT);
14088 if (lret == -ERESTARTSYS)
14089 return lret;
14090
14091 WARN(lret < 0, "waiting returns %li\n", lret);
14092 }
14093
c37efb99 14094 if (plane->type == DRM_PLANE_TYPE_CURSOR &&
6beb8c23
MR
14095 INTEL_INFO(dev)->cursor_needs_physical) {
14096 int align = IS_I830(dev) ? 16 * 1024 : 256;
14097 ret = i915_gem_object_attach_phys(obj, align);
14098 if (ret)
14099 DRM_DEBUG_KMS("failed to attach phys object\n");
14100 } else {
3465c580 14101 ret = intel_pin_and_fence_fb_obj(fb, new_state->rotation);
6beb8c23 14102 }
465c120c 14103
c37efb99 14104 if (ret == 0) {
6c9c1b38
DV
14105 struct intel_plane_state *plane_state =
14106 to_intel_plane_state(new_state);
7580d774 14107
6c9c1b38
DV
14108 i915_gem_request_assign(&plane_state->wait_req,
14109 obj->last_write_req);
7580d774 14110 }
fdd508a6 14111
6beb8c23
MR
14112 return ret;
14113}
14114
38f3ce3a
MR
14115/**
14116 * intel_cleanup_plane_fb - Cleans up an fb after plane use
14117 * @plane: drm plane to clean up for
14118 * @fb: old framebuffer that was on plane
14119 *
14120 * Cleans up a framebuffer that has just been removed from a plane.
f935675f
ML
14121 *
14122 * Must be called with struct_mutex held.
38f3ce3a
MR
14123 */
14124void
14125intel_cleanup_plane_fb(struct drm_plane *plane,
d136dfee 14126 const struct drm_plane_state *old_state)
38f3ce3a
MR
14127{
14128 struct drm_device *dev = plane->dev;
7580d774 14129 struct intel_plane_state *old_intel_state;
1ee49399
ML
14130 struct drm_i915_gem_object *old_obj = intel_fb_obj(old_state->fb);
14131 struct drm_i915_gem_object *obj = intel_fb_obj(plane->state->fb);
38f3ce3a 14132
7580d774
ML
14133 old_intel_state = to_intel_plane_state(old_state);
14134
1ee49399 14135 if (!obj && !old_obj)
38f3ce3a
MR
14136 return;
14137
1ee49399
ML
14138 if (old_obj && (plane->type != DRM_PLANE_TYPE_CURSOR ||
14139 !INTEL_INFO(dev)->cursor_needs_physical))
3465c580 14140 intel_unpin_fb_obj(old_state->fb, old_state->rotation);
1ee49399 14141
7580d774 14142 i915_gem_request_assign(&old_intel_state->wait_req, NULL);
465c120c
MR
14143}
14144
6156a456
CK
14145int
14146skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
14147{
14148 int max_scale;
14149 struct drm_device *dev;
14150 struct drm_i915_private *dev_priv;
14151 int crtc_clock, cdclk;
14152
bf8a0af0 14153 if (!intel_crtc || !crtc_state->base.enable)
6156a456
CK
14154 return DRM_PLANE_HELPER_NO_SCALING;
14155
14156 dev = intel_crtc->base.dev;
14157 dev_priv = dev->dev_private;
14158 crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
27c329ed 14159 cdclk = to_intel_atomic_state(crtc_state->base.state)->cdclk;
6156a456 14160
54bf1ce6 14161 if (WARN_ON_ONCE(!crtc_clock || cdclk < crtc_clock))
6156a456
CK
14162 return DRM_PLANE_HELPER_NO_SCALING;
14163
14164 /*
14165 * skl max scale is lower of:
14166 * close to 3 but not 3, -1 is for that purpose
14167 * or
14168 * cdclk/crtc_clock
14169 */
14170 max_scale = min((1 << 16) * 3 - 1, (1 << 8) * ((cdclk << 8) / crtc_clock));
14171
14172 return max_scale;
14173}
14174
465c120c 14175static int
3c692a41 14176intel_check_primary_plane(struct drm_plane *plane,
061e4b8d 14177 struct intel_crtc_state *crtc_state,
3c692a41
GP
14178 struct intel_plane_state *state)
14179{
2b875c22
MR
14180 struct drm_crtc *crtc = state->base.crtc;
14181 struct drm_framebuffer *fb = state->base.fb;
6156a456 14182 int min_scale = DRM_PLANE_HELPER_NO_SCALING;
061e4b8d
ML
14183 int max_scale = DRM_PLANE_HELPER_NO_SCALING;
14184 bool can_position = false;
465c120c 14185
693bdc28
VS
14186 if (INTEL_INFO(plane->dev)->gen >= 9) {
14187 /* use scaler when colorkey is not required */
14188 if (state->ckey.flags == I915_SET_COLORKEY_NONE) {
14189 min_scale = 1;
14190 max_scale = skl_max_scale(to_intel_crtc(crtc), crtc_state);
14191 }
d8106366 14192 can_position = true;
6156a456 14193 }
d8106366 14194
061e4b8d
ML
14195 return drm_plane_helper_check_update(plane, crtc, fb, &state->src,
14196 &state->dst, &state->clip,
9b8b013d 14197 state->base.rotation,
da20eabd
ML
14198 min_scale, max_scale,
14199 can_position, true,
14200 &state->visible);
14af293f
GP
14201}
14202
5a21b665
DV
14203static void intel_begin_crtc_commit(struct drm_crtc *crtc,
14204 struct drm_crtc_state *old_crtc_state)
14205{
14206 struct drm_device *dev = crtc->dev;
14207 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
14208 struct intel_crtc_state *old_intel_state =
14209 to_intel_crtc_state(old_crtc_state);
14210 bool modeset = needs_modeset(crtc->state);
14211
14212 /* Perform vblank evasion around commit operation */
14213 intel_pipe_update_start(intel_crtc);
14214
14215 if (modeset)
14216 return;
14217
14218 if (crtc->state->color_mgmt_changed || to_intel_crtc_state(crtc->state)->update_pipe) {
14219 intel_color_set_csc(crtc->state);
14220 intel_color_load_luts(crtc->state);
14221 }
14222
14223 if (to_intel_crtc_state(crtc->state)->update_pipe)
14224 intel_update_pipe_config(intel_crtc, old_intel_state);
14225 else if (INTEL_INFO(dev)->gen >= 9)
14226 skl_detach_scalers(intel_crtc);
14227}
14228
14229static void intel_finish_crtc_commit(struct drm_crtc *crtc,
14230 struct drm_crtc_state *old_crtc_state)
14231{
14232 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
14233
14234 intel_pipe_update_end(intel_crtc, NULL);
14235}
14236
cf4c7c12 14237/**
4a3b8769
MR
14238 * intel_plane_destroy - destroy a plane
14239 * @plane: plane to destroy
cf4c7c12 14240 *
4a3b8769
MR
14241 * Common destruction function for all types of planes (primary, cursor,
14242 * sprite).
cf4c7c12 14243 */
4a3b8769 14244void intel_plane_destroy(struct drm_plane *plane)
465c120c 14245{
69ae561f
VS
14246 if (!plane)
14247 return;
14248
465c120c 14249 drm_plane_cleanup(plane);
69ae561f 14250 kfree(to_intel_plane(plane));
465c120c
MR
14251}
14252
65a3fea0 14253const struct drm_plane_funcs intel_plane_funcs = {
70a101f8
MR
14254 .update_plane = drm_atomic_helper_update_plane,
14255 .disable_plane = drm_atomic_helper_disable_plane,
3d7d6510 14256 .destroy = intel_plane_destroy,
c196e1d6 14257 .set_property = drm_atomic_helper_plane_set_property,
a98b3431
MR
14258 .atomic_get_property = intel_plane_atomic_get_property,
14259 .atomic_set_property = intel_plane_atomic_set_property,
ea2c67bb
MR
14260 .atomic_duplicate_state = intel_plane_duplicate_state,
14261 .atomic_destroy_state = intel_plane_destroy_state,
14262
465c120c
MR
14263};
14264
14265static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
14266 int pipe)
14267{
fca0ce2a
VS
14268 struct intel_plane *primary = NULL;
14269 struct intel_plane_state *state = NULL;
465c120c 14270 const uint32_t *intel_primary_formats;
45e3743a 14271 unsigned int num_formats;
fca0ce2a 14272 int ret;
465c120c
MR
14273
14274 primary = kzalloc(sizeof(*primary), GFP_KERNEL);
fca0ce2a
VS
14275 if (!primary)
14276 goto fail;
465c120c 14277
8e7d688b 14278 state = intel_create_plane_state(&primary->base);
fca0ce2a
VS
14279 if (!state)
14280 goto fail;
8e7d688b 14281 primary->base.state = &state->base;
ea2c67bb 14282
465c120c
MR
14283 primary->can_scale = false;
14284 primary->max_downscale = 1;
6156a456
CK
14285 if (INTEL_INFO(dev)->gen >= 9) {
14286 primary->can_scale = true;
af99ceda 14287 state->scaler_id = -1;
6156a456 14288 }
465c120c
MR
14289 primary->pipe = pipe;
14290 primary->plane = pipe;
a9ff8714 14291 primary->frontbuffer_bit = INTEL_FRONTBUFFER_PRIMARY(pipe);
c59cb179 14292 primary->check_plane = intel_check_primary_plane;
465c120c
MR
14293 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
14294 primary->plane = !pipe;
14295
6c0fd451
DL
14296 if (INTEL_INFO(dev)->gen >= 9) {
14297 intel_primary_formats = skl_primary_formats;
14298 num_formats = ARRAY_SIZE(skl_primary_formats);
a8d201af
ML
14299
14300 primary->update_plane = skylake_update_primary_plane;
14301 primary->disable_plane = skylake_disable_primary_plane;
14302 } else if (HAS_PCH_SPLIT(dev)) {
14303 intel_primary_formats = i965_primary_formats;
14304 num_formats = ARRAY_SIZE(i965_primary_formats);
14305
14306 primary->update_plane = ironlake_update_primary_plane;
14307 primary->disable_plane = i9xx_disable_primary_plane;
6c0fd451 14308 } else if (INTEL_INFO(dev)->gen >= 4) {
568db4f2
DL
14309 intel_primary_formats = i965_primary_formats;
14310 num_formats = ARRAY_SIZE(i965_primary_formats);
a8d201af
ML
14311
14312 primary->update_plane = i9xx_update_primary_plane;
14313 primary->disable_plane = i9xx_disable_primary_plane;
6c0fd451
DL
14314 } else {
14315 intel_primary_formats = i8xx_primary_formats;
14316 num_formats = ARRAY_SIZE(i8xx_primary_formats);
a8d201af
ML
14317
14318 primary->update_plane = i9xx_update_primary_plane;
14319 primary->disable_plane = i9xx_disable_primary_plane;
465c120c
MR
14320 }
14321
38573dc1
VS
14322 if (INTEL_INFO(dev)->gen >= 9)
14323 ret = drm_universal_plane_init(dev, &primary->base, 0,
14324 &intel_plane_funcs,
14325 intel_primary_formats, num_formats,
14326 DRM_PLANE_TYPE_PRIMARY,
14327 "plane 1%c", pipe_name(pipe));
14328 else if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
14329 ret = drm_universal_plane_init(dev, &primary->base, 0,
14330 &intel_plane_funcs,
14331 intel_primary_formats, num_formats,
14332 DRM_PLANE_TYPE_PRIMARY,
14333 "primary %c", pipe_name(pipe));
14334 else
14335 ret = drm_universal_plane_init(dev, &primary->base, 0,
14336 &intel_plane_funcs,
14337 intel_primary_formats, num_formats,
14338 DRM_PLANE_TYPE_PRIMARY,
14339 "plane %c", plane_name(primary->plane));
fca0ce2a
VS
14340 if (ret)
14341 goto fail;
48404c1e 14342
3b7a5119
SJ
14343 if (INTEL_INFO(dev)->gen >= 4)
14344 intel_create_rotation_property(dev, primary);
48404c1e 14345
ea2c67bb
MR
14346 drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
14347
465c120c 14348 return &primary->base;
fca0ce2a
VS
14349
14350fail:
14351 kfree(state);
14352 kfree(primary);
14353
14354 return NULL;
465c120c
MR
14355}
14356
3b7a5119
SJ
14357void intel_create_rotation_property(struct drm_device *dev, struct intel_plane *plane)
14358{
14359 if (!dev->mode_config.rotation_property) {
14360 unsigned long flags = BIT(DRM_ROTATE_0) |
14361 BIT(DRM_ROTATE_180);
14362
14363 if (INTEL_INFO(dev)->gen >= 9)
14364 flags |= BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_270);
14365
14366 dev->mode_config.rotation_property =
14367 drm_mode_create_rotation_property(dev, flags);
14368 }
14369 if (dev->mode_config.rotation_property)
14370 drm_object_attach_property(&plane->base.base,
14371 dev->mode_config.rotation_property,
14372 plane->base.state->rotation);
14373}
14374
3d7d6510 14375static int
852e787c 14376intel_check_cursor_plane(struct drm_plane *plane,
061e4b8d 14377 struct intel_crtc_state *crtc_state,
852e787c 14378 struct intel_plane_state *state)
3d7d6510 14379{
061e4b8d 14380 struct drm_crtc *crtc = crtc_state->base.crtc;
2b875c22 14381 struct drm_framebuffer *fb = state->base.fb;
757f9a3e 14382 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
b29ec92c 14383 enum pipe pipe = to_intel_plane(plane)->pipe;
757f9a3e
GP
14384 unsigned stride;
14385 int ret;
3d7d6510 14386
061e4b8d
ML
14387 ret = drm_plane_helper_check_update(plane, crtc, fb, &state->src,
14388 &state->dst, &state->clip,
9b8b013d 14389 state->base.rotation,
3d7d6510
MR
14390 DRM_PLANE_HELPER_NO_SCALING,
14391 DRM_PLANE_HELPER_NO_SCALING,
852e787c 14392 true, true, &state->visible);
757f9a3e
GP
14393 if (ret)
14394 return ret;
14395
757f9a3e
GP
14396 /* if we want to turn off the cursor ignore width and height */
14397 if (!obj)
da20eabd 14398 return 0;
757f9a3e 14399
757f9a3e 14400 /* Check for which cursor types we support */
061e4b8d 14401 if (!cursor_size_ok(plane->dev, state->base.crtc_w, state->base.crtc_h)) {
ea2c67bb
MR
14402 DRM_DEBUG("Cursor dimension %dx%d not supported\n",
14403 state->base.crtc_w, state->base.crtc_h);
757f9a3e
GP
14404 return -EINVAL;
14405 }
14406
ea2c67bb
MR
14407 stride = roundup_pow_of_two(state->base.crtc_w) * 4;
14408 if (obj->base.size < stride * state->base.crtc_h) {
757f9a3e
GP
14409 DRM_DEBUG_KMS("buffer is too small\n");
14410 return -ENOMEM;
14411 }
14412
3a656b54 14413 if (fb->modifier[0] != DRM_FORMAT_MOD_NONE) {
757f9a3e 14414 DRM_DEBUG_KMS("cursor cannot be tiled\n");
da20eabd 14415 return -EINVAL;
32b7eeec
MR
14416 }
14417
b29ec92c
VS
14418 /*
14419 * There's something wrong with the cursor on CHV pipe C.
14420 * If it straddles the left edge of the screen then
14421 * moving it away from the edge or disabling it often
14422 * results in a pipe underrun, and often that can lead to
14423 * dead pipe (constant underrun reported, and it scans
14424 * out just a solid color). To recover from that, the
14425 * display power well must be turned off and on again.
14426 * Refuse the put the cursor into that compromised position.
14427 */
14428 if (IS_CHERRYVIEW(plane->dev) && pipe == PIPE_C &&
14429 state->visible && state->base.crtc_x < 0) {
14430 DRM_DEBUG_KMS("CHV cursor C not allowed to straddle the left screen edge\n");
14431 return -EINVAL;
14432 }
14433
da20eabd 14434 return 0;
852e787c 14435}
3d7d6510 14436
a8ad0d8e
ML
14437static void
14438intel_disable_cursor_plane(struct drm_plane *plane,
7fabf5ef 14439 struct drm_crtc *crtc)
a8ad0d8e 14440{
f2858021
ML
14441 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
14442
14443 intel_crtc->cursor_addr = 0;
55a08b3f 14444 intel_crtc_update_cursor(crtc, NULL);
a8ad0d8e
ML
14445}
14446
f4a2cf29 14447static void
55a08b3f
ML
14448intel_update_cursor_plane(struct drm_plane *plane,
14449 const struct intel_crtc_state *crtc_state,
14450 const struct intel_plane_state *state)
852e787c 14451{
55a08b3f
ML
14452 struct drm_crtc *crtc = crtc_state->base.crtc;
14453 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ea2c67bb 14454 struct drm_device *dev = plane->dev;
2b875c22 14455 struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
a912f12f 14456 uint32_t addr;
852e787c 14457
f4a2cf29 14458 if (!obj)
a912f12f 14459 addr = 0;
f4a2cf29 14460 else if (!INTEL_INFO(dev)->cursor_needs_physical)
a912f12f 14461 addr = i915_gem_obj_ggtt_offset(obj);
f4a2cf29 14462 else
a912f12f 14463 addr = obj->phys_handle->busaddr;
852e787c 14464
a912f12f 14465 intel_crtc->cursor_addr = addr;
55a08b3f 14466 intel_crtc_update_cursor(crtc, state);
852e787c
GP
14467}
14468
3d7d6510
MR
14469static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
14470 int pipe)
14471{
fca0ce2a
VS
14472 struct intel_plane *cursor = NULL;
14473 struct intel_plane_state *state = NULL;
14474 int ret;
3d7d6510
MR
14475
14476 cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
fca0ce2a
VS
14477 if (!cursor)
14478 goto fail;
3d7d6510 14479
8e7d688b 14480 state = intel_create_plane_state(&cursor->base);
fca0ce2a
VS
14481 if (!state)
14482 goto fail;
8e7d688b 14483 cursor->base.state = &state->base;
ea2c67bb 14484
3d7d6510
MR
14485 cursor->can_scale = false;
14486 cursor->max_downscale = 1;
14487 cursor->pipe = pipe;
14488 cursor->plane = pipe;
a9ff8714 14489 cursor->frontbuffer_bit = INTEL_FRONTBUFFER_CURSOR(pipe);
c59cb179 14490 cursor->check_plane = intel_check_cursor_plane;
55a08b3f 14491 cursor->update_plane = intel_update_cursor_plane;
a8ad0d8e 14492 cursor->disable_plane = intel_disable_cursor_plane;
3d7d6510 14493
fca0ce2a
VS
14494 ret = drm_universal_plane_init(dev, &cursor->base, 0,
14495 &intel_plane_funcs,
14496 intel_cursor_formats,
14497 ARRAY_SIZE(intel_cursor_formats),
38573dc1
VS
14498 DRM_PLANE_TYPE_CURSOR,
14499 "cursor %c", pipe_name(pipe));
fca0ce2a
VS
14500 if (ret)
14501 goto fail;
4398ad45
VS
14502
14503 if (INTEL_INFO(dev)->gen >= 4) {
14504 if (!dev->mode_config.rotation_property)
14505 dev->mode_config.rotation_property =
14506 drm_mode_create_rotation_property(dev,
14507 BIT(DRM_ROTATE_0) |
14508 BIT(DRM_ROTATE_180));
14509 if (dev->mode_config.rotation_property)
14510 drm_object_attach_property(&cursor->base.base,
14511 dev->mode_config.rotation_property,
8e7d688b 14512 state->base.rotation);
4398ad45
VS
14513 }
14514
af99ceda
CK
14515 if (INTEL_INFO(dev)->gen >=9)
14516 state->scaler_id = -1;
14517
ea2c67bb
MR
14518 drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
14519
3d7d6510 14520 return &cursor->base;
fca0ce2a
VS
14521
14522fail:
14523 kfree(state);
14524 kfree(cursor);
14525
14526 return NULL;
3d7d6510
MR
14527}
14528
549e2bfb
CK
14529static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
14530 struct intel_crtc_state *crtc_state)
14531{
14532 int i;
14533 struct intel_scaler *intel_scaler;
14534 struct intel_crtc_scaler_state *scaler_state = &crtc_state->scaler_state;
14535
14536 for (i = 0; i < intel_crtc->num_scalers; i++) {
14537 intel_scaler = &scaler_state->scalers[i];
14538 intel_scaler->in_use = 0;
549e2bfb
CK
14539 intel_scaler->mode = PS_SCALER_MODE_DYN;
14540 }
14541
14542 scaler_state->scaler_id = -1;
14543}
14544
b358d0a6 14545static void intel_crtc_init(struct drm_device *dev, int pipe)
79e53945 14546{
fbee40df 14547 struct drm_i915_private *dev_priv = dev->dev_private;
79e53945 14548 struct intel_crtc *intel_crtc;
f5de6e07 14549 struct intel_crtc_state *crtc_state = NULL;
3d7d6510
MR
14550 struct drm_plane *primary = NULL;
14551 struct drm_plane *cursor = NULL;
8563b1e8 14552 int ret;
79e53945 14553
955382f3 14554 intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
79e53945
JB
14555 if (intel_crtc == NULL)
14556 return;
14557
f5de6e07
ACO
14558 crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
14559 if (!crtc_state)
14560 goto fail;
550acefd
ACO
14561 intel_crtc->config = crtc_state;
14562 intel_crtc->base.state = &crtc_state->base;
07878248 14563 crtc_state->base.crtc = &intel_crtc->base;
f5de6e07 14564
549e2bfb
CK
14565 /* initialize shared scalers */
14566 if (INTEL_INFO(dev)->gen >= 9) {
14567 if (pipe == PIPE_C)
14568 intel_crtc->num_scalers = 1;
14569 else
14570 intel_crtc->num_scalers = SKL_NUM_SCALERS;
14571
14572 skl_init_scalers(dev, intel_crtc, crtc_state);
14573 }
14574
465c120c 14575 primary = intel_primary_plane_create(dev, pipe);
3d7d6510
MR
14576 if (!primary)
14577 goto fail;
14578
14579 cursor = intel_cursor_plane_create(dev, pipe);
14580 if (!cursor)
14581 goto fail;
14582
465c120c 14583 ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
4d5d72b7
VS
14584 cursor, &intel_crtc_funcs,
14585 "pipe %c", pipe_name(pipe));
3d7d6510
MR
14586 if (ret)
14587 goto fail;
79e53945 14588
1f1c2e24
VS
14589 /*
14590 * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
8c0f92e1 14591 * is hooked to pipe B. Hence we want plane A feeding pipe B.
1f1c2e24 14592 */
80824003
JB
14593 intel_crtc->pipe = pipe;
14594 intel_crtc->plane = pipe;
3a77c4c4 14595 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
28c97730 14596 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
e2e767ab 14597 intel_crtc->plane = !pipe;
80824003
JB
14598 }
14599
4b0e333e
CW
14600 intel_crtc->cursor_base = ~0;
14601 intel_crtc->cursor_cntl = ~0;
dc41c154 14602 intel_crtc->cursor_size = ~0;
8d7849db 14603
852eb00d
VS
14604 intel_crtc->wm.cxsr_allowed = true;
14605
22fd0fab
JB
14606 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
14607 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
14608 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
14609 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
14610
79e53945 14611 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
87b6b101 14612
8563b1e8
LL
14613 intel_color_init(&intel_crtc->base);
14614
87b6b101 14615 WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
3d7d6510
MR
14616 return;
14617
14618fail:
69ae561f
VS
14619 intel_plane_destroy(primary);
14620 intel_plane_destroy(cursor);
f5de6e07 14621 kfree(crtc_state);
3d7d6510 14622 kfree(intel_crtc);
79e53945
JB
14623}
14624
752aa88a
JB
14625enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
14626{
14627 struct drm_encoder *encoder = connector->base.encoder;
6e9f798d 14628 struct drm_device *dev = connector->base.dev;
752aa88a 14629
51fd371b 14630 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
752aa88a 14631
d3babd3f 14632 if (!encoder || WARN_ON(!encoder->crtc))
752aa88a
JB
14633 return INVALID_PIPE;
14634
14635 return to_intel_crtc(encoder->crtc)->pipe;
14636}
14637
08d7b3d1 14638int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
05394f39 14639 struct drm_file *file)
08d7b3d1 14640{
08d7b3d1 14641 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
7707e653 14642 struct drm_crtc *drmmode_crtc;
c05422d5 14643 struct intel_crtc *crtc;
08d7b3d1 14644
7707e653 14645 drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
71240ed2 14646 if (!drmmode_crtc)
3f2c2057 14647 return -ENOENT;
08d7b3d1 14648
7707e653 14649 crtc = to_intel_crtc(drmmode_crtc);
c05422d5 14650 pipe_from_crtc_id->pipe = crtc->pipe;
08d7b3d1 14651
c05422d5 14652 return 0;
08d7b3d1
CW
14653}
14654
66a9278e 14655static int intel_encoder_clones(struct intel_encoder *encoder)
79e53945 14656{
66a9278e
DV
14657 struct drm_device *dev = encoder->base.dev;
14658 struct intel_encoder *source_encoder;
79e53945 14659 int index_mask = 0;
79e53945
JB
14660 int entry = 0;
14661
b2784e15 14662 for_each_intel_encoder(dev, source_encoder) {
bc079e8b 14663 if (encoders_cloneable(encoder, source_encoder))
66a9278e
DV
14664 index_mask |= (1 << entry);
14665
79e53945
JB
14666 entry++;
14667 }
4ef69c7a 14668
79e53945
JB
14669 return index_mask;
14670}
14671
4d302442
CW
14672static bool has_edp_a(struct drm_device *dev)
14673{
14674 struct drm_i915_private *dev_priv = dev->dev_private;
14675
14676 if (!IS_MOBILE(dev))
14677 return false;
14678
14679 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
14680 return false;
14681
e3589908 14682 if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
4d302442
CW
14683 return false;
14684
14685 return true;
14686}
14687
84b4e042
JB
14688static bool intel_crt_present(struct drm_device *dev)
14689{
14690 struct drm_i915_private *dev_priv = dev->dev_private;
14691
884497ed
DL
14692 if (INTEL_INFO(dev)->gen >= 9)
14693 return false;
14694
cf404ce4 14695 if (IS_HSW_ULT(dev) || IS_BDW_ULT(dev))
84b4e042
JB
14696 return false;
14697
14698 if (IS_CHERRYVIEW(dev))
14699 return false;
14700
65e472e4
VS
14701 if (HAS_PCH_LPT_H(dev) && I915_READ(SFUSE_STRAP) & SFUSE_STRAP_CRT_DISABLED)
14702 return false;
14703
70ac54d0
VS
14704 /* DDI E can't be used if DDI A requires 4 lanes */
14705 if (HAS_DDI(dev) && I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_A_4_LANES)
14706 return false;
14707
e4abb733 14708 if (!dev_priv->vbt.int_crt_support)
84b4e042
JB
14709 return false;
14710
14711 return true;
14712}
14713
79e53945
JB
14714static void intel_setup_outputs(struct drm_device *dev)
14715{
725e30ad 14716 struct drm_i915_private *dev_priv = dev->dev_private;
4ef69c7a 14717 struct intel_encoder *encoder;
cb0953d7 14718 bool dpd_is_edp = false;
79e53945 14719
97a824e1
ID
14720 /*
14721 * intel_edp_init_connector() depends on this completing first, to
14722 * prevent the registeration of both eDP and LVDS and the incorrect
14723 * sharing of the PPS.
14724 */
c9093354 14725 intel_lvds_init(dev);
79e53945 14726
84b4e042 14727 if (intel_crt_present(dev))
79935fca 14728 intel_crt_init(dev);
cb0953d7 14729
c776eb2e
VK
14730 if (IS_BROXTON(dev)) {
14731 /*
14732 * FIXME: Broxton doesn't support port detection via the
14733 * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
14734 * detect the ports.
14735 */
14736 intel_ddi_init(dev, PORT_A);
14737 intel_ddi_init(dev, PORT_B);
14738 intel_ddi_init(dev, PORT_C);
c6c794a2
SS
14739
14740 intel_dsi_init(dev);
c776eb2e 14741 } else if (HAS_DDI(dev)) {
0e72a5b5
ED
14742 int found;
14743
de31facd
JB
14744 /*
14745 * Haswell uses DDI functions to detect digital outputs.
14746 * On SKL pre-D0 the strap isn't connected, so we assume
14747 * it's there.
14748 */
77179400 14749 found = I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_INIT_DISPLAY_DETECTED;
de31facd 14750 /* WaIgnoreDDIAStrap: skl */
ef11bdb3 14751 if (found || IS_SKYLAKE(dev) || IS_KABYLAKE(dev))
0e72a5b5
ED
14752 intel_ddi_init(dev, PORT_A);
14753
14754 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
14755 * register */
14756 found = I915_READ(SFUSE_STRAP);
14757
14758 if (found & SFUSE_STRAP_DDIB_DETECTED)
14759 intel_ddi_init(dev, PORT_B);
14760 if (found & SFUSE_STRAP_DDIC_DETECTED)
14761 intel_ddi_init(dev, PORT_C);
14762 if (found & SFUSE_STRAP_DDID_DETECTED)
14763 intel_ddi_init(dev, PORT_D);
2800e4c2
RV
14764 /*
14765 * On SKL we don't have a way to detect DDI-E so we rely on VBT.
14766 */
ef11bdb3 14767 if ((IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) &&
2800e4c2
RV
14768 (dev_priv->vbt.ddi_port_info[PORT_E].supports_dp ||
14769 dev_priv->vbt.ddi_port_info[PORT_E].supports_dvi ||
14770 dev_priv->vbt.ddi_port_info[PORT_E].supports_hdmi))
14771 intel_ddi_init(dev, PORT_E);
14772
0e72a5b5 14773 } else if (HAS_PCH_SPLIT(dev)) {
cb0953d7 14774 int found;
5d8a7752 14775 dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
270b3042
DV
14776
14777 if (has_edp_a(dev))
14778 intel_dp_init(dev, DP_A, PORT_A);
cb0953d7 14779
dc0fa718 14780 if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
461ed3ca 14781 /* PCH SDVOB multiplex with HDMIB */
2a5c0832 14782 found = intel_sdvo_init(dev, PCH_SDVOB, PORT_B);
30ad48b7 14783 if (!found)
e2debe91 14784 intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
5eb08b69 14785 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
ab9d7c30 14786 intel_dp_init(dev, PCH_DP_B, PORT_B);
30ad48b7
ZW
14787 }
14788
dc0fa718 14789 if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
e2debe91 14790 intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
30ad48b7 14791
dc0fa718 14792 if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
e2debe91 14793 intel_hdmi_init(dev, PCH_HDMID, PORT_D);
30ad48b7 14794
5eb08b69 14795 if (I915_READ(PCH_DP_C) & DP_DETECTED)
ab9d7c30 14796 intel_dp_init(dev, PCH_DP_C, PORT_C);
5eb08b69 14797
270b3042 14798 if (I915_READ(PCH_DP_D) & DP_DETECTED)
ab9d7c30 14799 intel_dp_init(dev, PCH_DP_D, PORT_D);
666a4537 14800 } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
22f35042 14801 bool has_edp, has_port;
457c52d8 14802
e17ac6db
VS
14803 /*
14804 * The DP_DETECTED bit is the latched state of the DDC
14805 * SDA pin at boot. However since eDP doesn't require DDC
14806 * (no way to plug in a DP->HDMI dongle) the DDC pins for
14807 * eDP ports may have been muxed to an alternate function.
14808 * Thus we can't rely on the DP_DETECTED bit alone to detect
14809 * eDP ports. Consult the VBT as well as DP_DETECTED to
14810 * detect eDP ports.
22f35042
VS
14811 *
14812 * Sadly the straps seem to be missing sometimes even for HDMI
14813 * ports (eg. on Voyo V3 - CHT x7-Z8700), so check both strap
14814 * and VBT for the presence of the port. Additionally we can't
14815 * trust the port type the VBT declares as we've seen at least
14816 * HDMI ports that the VBT claim are DP or eDP.
e17ac6db 14817 */
457c52d8 14818 has_edp = intel_dp_is_edp(dev, PORT_B);
22f35042
VS
14819 has_port = intel_bios_is_port_present(dev_priv, PORT_B);
14820 if (I915_READ(VLV_DP_B) & DP_DETECTED || has_port)
457c52d8 14821 has_edp &= intel_dp_init(dev, VLV_DP_B, PORT_B);
22f35042 14822 if ((I915_READ(VLV_HDMIB) & SDVO_DETECTED || has_port) && !has_edp)
e66eb81d 14823 intel_hdmi_init(dev, VLV_HDMIB, PORT_B);
585a94b8 14824
457c52d8 14825 has_edp = intel_dp_is_edp(dev, PORT_C);
22f35042
VS
14826 has_port = intel_bios_is_port_present(dev_priv, PORT_C);
14827 if (I915_READ(VLV_DP_C) & DP_DETECTED || has_port)
457c52d8 14828 has_edp &= intel_dp_init(dev, VLV_DP_C, PORT_C);
22f35042 14829 if ((I915_READ(VLV_HDMIC) & SDVO_DETECTED || has_port) && !has_edp)
e66eb81d 14830 intel_hdmi_init(dev, VLV_HDMIC, PORT_C);
19c03924 14831
9418c1f1 14832 if (IS_CHERRYVIEW(dev)) {
22f35042
VS
14833 /*
14834 * eDP not supported on port D,
14835 * so no need to worry about it
14836 */
14837 has_port = intel_bios_is_port_present(dev_priv, PORT_D);
14838 if (I915_READ(CHV_DP_D) & DP_DETECTED || has_port)
e66eb81d 14839 intel_dp_init(dev, CHV_DP_D, PORT_D);
22f35042
VS
14840 if (I915_READ(CHV_HDMID) & SDVO_DETECTED || has_port)
14841 intel_hdmi_init(dev, CHV_HDMID, PORT_D);
9418c1f1
VS
14842 }
14843
3cfca973 14844 intel_dsi_init(dev);
09da55dc 14845 } else if (!IS_GEN2(dev) && !IS_PINEVIEW(dev)) {
27185ae1 14846 bool found = false;
7d57382e 14847
e2debe91 14848 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
b01f2c3a 14849 DRM_DEBUG_KMS("probing SDVOB\n");
2a5c0832 14850 found = intel_sdvo_init(dev, GEN3_SDVOB, PORT_B);
3fec3d2f 14851 if (!found && IS_G4X(dev)) {
b01f2c3a 14852 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
e2debe91 14853 intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
b01f2c3a 14854 }
27185ae1 14855
3fec3d2f 14856 if (!found && IS_G4X(dev))
ab9d7c30 14857 intel_dp_init(dev, DP_B, PORT_B);
725e30ad 14858 }
13520b05
KH
14859
14860 /* Before G4X SDVOC doesn't have its own detect register */
13520b05 14861
e2debe91 14862 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
b01f2c3a 14863 DRM_DEBUG_KMS("probing SDVOC\n");
2a5c0832 14864 found = intel_sdvo_init(dev, GEN3_SDVOC, PORT_C);
b01f2c3a 14865 }
27185ae1 14866
e2debe91 14867 if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
27185ae1 14868
3fec3d2f 14869 if (IS_G4X(dev)) {
b01f2c3a 14870 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
e2debe91 14871 intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
b01f2c3a 14872 }
3fec3d2f 14873 if (IS_G4X(dev))
ab9d7c30 14874 intel_dp_init(dev, DP_C, PORT_C);
725e30ad 14875 }
27185ae1 14876
3fec3d2f 14877 if (IS_G4X(dev) &&
e7281eab 14878 (I915_READ(DP_D) & DP_DETECTED))
ab9d7c30 14879 intel_dp_init(dev, DP_D, PORT_D);
bad720ff 14880 } else if (IS_GEN2(dev))
79e53945
JB
14881 intel_dvo_init(dev);
14882
103a196f 14883 if (SUPPORTS_TV(dev))
79e53945
JB
14884 intel_tv_init(dev);
14885
0bc12bcb 14886 intel_psr_init(dev);
7c8f8a70 14887
b2784e15 14888 for_each_intel_encoder(dev, encoder) {
4ef69c7a
CW
14889 encoder->base.possible_crtcs = encoder->crtc_mask;
14890 encoder->base.possible_clones =
66a9278e 14891 intel_encoder_clones(encoder);
79e53945 14892 }
47356eb6 14893
dde86e2d 14894 intel_init_pch_refclk(dev);
270b3042
DV
14895
14896 drm_helper_move_panel_connectors_to_head(dev);
79e53945
JB
14897}
14898
14899static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
14900{
60a5ca01 14901 struct drm_device *dev = fb->dev;
79e53945 14902 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
79e53945 14903
ef2d633e 14904 drm_framebuffer_cleanup(fb);
60a5ca01 14905 mutex_lock(&dev->struct_mutex);
ef2d633e 14906 WARN_ON(!intel_fb->obj->framebuffer_references--);
60a5ca01
VS
14907 drm_gem_object_unreference(&intel_fb->obj->base);
14908 mutex_unlock(&dev->struct_mutex);
79e53945
JB
14909 kfree(intel_fb);
14910}
14911
14912static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
05394f39 14913 struct drm_file *file,
79e53945
JB
14914 unsigned int *handle)
14915{
14916 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
05394f39 14917 struct drm_i915_gem_object *obj = intel_fb->obj;
79e53945 14918
cc917ab4
CW
14919 if (obj->userptr.mm) {
14920 DRM_DEBUG("attempting to use a userptr for a framebuffer, denied\n");
14921 return -EINVAL;
14922 }
14923
05394f39 14924 return drm_gem_handle_create(file, &obj->base, handle);
79e53945
JB
14925}
14926
86c98588
RV
14927static int intel_user_framebuffer_dirty(struct drm_framebuffer *fb,
14928 struct drm_file *file,
14929 unsigned flags, unsigned color,
14930 struct drm_clip_rect *clips,
14931 unsigned num_clips)
14932{
14933 struct drm_device *dev = fb->dev;
14934 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
14935 struct drm_i915_gem_object *obj = intel_fb->obj;
14936
14937 mutex_lock(&dev->struct_mutex);
74b4ea1e 14938 intel_fb_obj_flush(obj, false, ORIGIN_DIRTYFB);
86c98588
RV
14939 mutex_unlock(&dev->struct_mutex);
14940
14941 return 0;
14942}
14943
79e53945
JB
14944static const struct drm_framebuffer_funcs intel_fb_funcs = {
14945 .destroy = intel_user_framebuffer_destroy,
14946 .create_handle = intel_user_framebuffer_create_handle,
86c98588 14947 .dirty = intel_user_framebuffer_dirty,
79e53945
JB
14948};
14949
b321803d
DL
14950static
14951u32 intel_fb_pitch_limit(struct drm_device *dev, uint64_t fb_modifier,
14952 uint32_t pixel_format)
14953{
14954 u32 gen = INTEL_INFO(dev)->gen;
14955
14956 if (gen >= 9) {
ac484963
VS
14957 int cpp = drm_format_plane_cpp(pixel_format, 0);
14958
b321803d
DL
14959 /* "The stride in bytes must not exceed the of the size of 8K
14960 * pixels and 32K bytes."
14961 */
ac484963 14962 return min(8192 * cpp, 32768);
666a4537 14963 } else if (gen >= 5 && !IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
b321803d
DL
14964 return 32*1024;
14965 } else if (gen >= 4) {
14966 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14967 return 16*1024;
14968 else
14969 return 32*1024;
14970 } else if (gen >= 3) {
14971 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14972 return 8*1024;
14973 else
14974 return 16*1024;
14975 } else {
14976 /* XXX DSPC is limited to 4k tiled */
14977 return 8*1024;
14978 }
14979}
14980
b5ea642a
DV
14981static int intel_framebuffer_init(struct drm_device *dev,
14982 struct intel_framebuffer *intel_fb,
14983 struct drm_mode_fb_cmd2 *mode_cmd,
14984 struct drm_i915_gem_object *obj)
79e53945 14985{
7b49f948 14986 struct drm_i915_private *dev_priv = to_i915(dev);
6761dd31 14987 unsigned int aligned_height;
79e53945 14988 int ret;
b321803d 14989 u32 pitch_limit, stride_alignment;
79e53945 14990
dd4916c5
DV
14991 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
14992
2a80eada
DV
14993 if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
14994 /* Enforce that fb modifier and tiling mode match, but only for
14995 * X-tiled. This is needed for FBC. */
14996 if (!!(obj->tiling_mode == I915_TILING_X) !=
14997 !!(mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED)) {
14998 DRM_DEBUG("tiling_mode doesn't match fb modifier\n");
14999 return -EINVAL;
15000 }
15001 } else {
15002 if (obj->tiling_mode == I915_TILING_X)
15003 mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
15004 else if (obj->tiling_mode == I915_TILING_Y) {
15005 DRM_DEBUG("No Y tiling for legacy addfb\n");
15006 return -EINVAL;
15007 }
15008 }
15009
9a8f0a12
TU
15010 /* Passed in modifier sanity checking. */
15011 switch (mode_cmd->modifier[0]) {
15012 case I915_FORMAT_MOD_Y_TILED:
15013 case I915_FORMAT_MOD_Yf_TILED:
15014 if (INTEL_INFO(dev)->gen < 9) {
15015 DRM_DEBUG("Unsupported tiling 0x%llx!\n",
15016 mode_cmd->modifier[0]);
15017 return -EINVAL;
15018 }
15019 case DRM_FORMAT_MOD_NONE:
15020 case I915_FORMAT_MOD_X_TILED:
15021 break;
15022 default:
c0f40428
JB
15023 DRM_DEBUG("Unsupported fb modifier 0x%llx!\n",
15024 mode_cmd->modifier[0]);
57cd6508 15025 return -EINVAL;
c16ed4be 15026 }
57cd6508 15027
7b49f948
VS
15028 stride_alignment = intel_fb_stride_alignment(dev_priv,
15029 mode_cmd->modifier[0],
b321803d
DL
15030 mode_cmd->pixel_format);
15031 if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
15032 DRM_DEBUG("pitch (%d) must be at least %u byte aligned\n",
15033 mode_cmd->pitches[0], stride_alignment);
57cd6508 15034 return -EINVAL;
c16ed4be 15035 }
57cd6508 15036
b321803d
DL
15037 pitch_limit = intel_fb_pitch_limit(dev, mode_cmd->modifier[0],
15038 mode_cmd->pixel_format);
a35cdaa0 15039 if (mode_cmd->pitches[0] > pitch_limit) {
b321803d
DL
15040 DRM_DEBUG("%s pitch (%u) must be at less than %d\n",
15041 mode_cmd->modifier[0] != DRM_FORMAT_MOD_NONE ?
2a80eada 15042 "tiled" : "linear",
a35cdaa0 15043 mode_cmd->pitches[0], pitch_limit);
5d7bd705 15044 return -EINVAL;
c16ed4be 15045 }
5d7bd705 15046
2a80eada 15047 if (mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED &&
c16ed4be
CW
15048 mode_cmd->pitches[0] != obj->stride) {
15049 DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
15050 mode_cmd->pitches[0], obj->stride);
5d7bd705 15051 return -EINVAL;
c16ed4be 15052 }
5d7bd705 15053
57779d06 15054 /* Reject formats not supported by any plane early. */
308e5bcb 15055 switch (mode_cmd->pixel_format) {
57779d06 15056 case DRM_FORMAT_C8:
04b3924d
VS
15057 case DRM_FORMAT_RGB565:
15058 case DRM_FORMAT_XRGB8888:
15059 case DRM_FORMAT_ARGB8888:
57779d06
VS
15060 break;
15061 case DRM_FORMAT_XRGB1555:
c16ed4be 15062 if (INTEL_INFO(dev)->gen > 3) {
4ee62c76
VS
15063 DRM_DEBUG("unsupported pixel format: %s\n",
15064 drm_get_format_name(mode_cmd->pixel_format));
57779d06 15065 return -EINVAL;
c16ed4be 15066 }
57779d06 15067 break;
57779d06 15068 case DRM_FORMAT_ABGR8888:
666a4537
WB
15069 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev) &&
15070 INTEL_INFO(dev)->gen < 9) {
6c0fd451
DL
15071 DRM_DEBUG("unsupported pixel format: %s\n",
15072 drm_get_format_name(mode_cmd->pixel_format));
15073 return -EINVAL;
15074 }
15075 break;
15076 case DRM_FORMAT_XBGR8888:
04b3924d 15077 case DRM_FORMAT_XRGB2101010:
57779d06 15078 case DRM_FORMAT_XBGR2101010:
c16ed4be 15079 if (INTEL_INFO(dev)->gen < 4) {
4ee62c76
VS
15080 DRM_DEBUG("unsupported pixel format: %s\n",
15081 drm_get_format_name(mode_cmd->pixel_format));
57779d06 15082 return -EINVAL;
c16ed4be 15083 }
b5626747 15084 break;
7531208b 15085 case DRM_FORMAT_ABGR2101010:
666a4537 15086 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
7531208b
DL
15087 DRM_DEBUG("unsupported pixel format: %s\n",
15088 drm_get_format_name(mode_cmd->pixel_format));
15089 return -EINVAL;
15090 }
15091 break;
04b3924d
VS
15092 case DRM_FORMAT_YUYV:
15093 case DRM_FORMAT_UYVY:
15094 case DRM_FORMAT_YVYU:
15095 case DRM_FORMAT_VYUY:
c16ed4be 15096 if (INTEL_INFO(dev)->gen < 5) {
4ee62c76
VS
15097 DRM_DEBUG("unsupported pixel format: %s\n",
15098 drm_get_format_name(mode_cmd->pixel_format));
57779d06 15099 return -EINVAL;
c16ed4be 15100 }
57cd6508
CW
15101 break;
15102 default:
4ee62c76
VS
15103 DRM_DEBUG("unsupported pixel format: %s\n",
15104 drm_get_format_name(mode_cmd->pixel_format));
57cd6508
CW
15105 return -EINVAL;
15106 }
15107
90f9a336
VS
15108 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
15109 if (mode_cmd->offsets[0] != 0)
15110 return -EINVAL;
15111
ec2c981e 15112 aligned_height = intel_fb_align_height(dev, mode_cmd->height,
091df6cb
DV
15113 mode_cmd->pixel_format,
15114 mode_cmd->modifier[0]);
53155c0a
DV
15115 /* FIXME drm helper for size checks (especially planar formats)? */
15116 if (obj->base.size < aligned_height * mode_cmd->pitches[0])
15117 return -EINVAL;
15118
c7d73f6a
DV
15119 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
15120 intel_fb->obj = obj;
15121
2d7a215f
VS
15122 intel_fill_fb_info(dev_priv, &intel_fb->base);
15123
79e53945
JB
15124 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
15125 if (ret) {
15126 DRM_ERROR("framebuffer init failed %d\n", ret);
15127 return ret;
15128 }
15129
0b05e1e0
VS
15130 intel_fb->obj->framebuffer_references++;
15131
79e53945
JB
15132 return 0;
15133}
15134
79e53945
JB
15135static struct drm_framebuffer *
15136intel_user_framebuffer_create(struct drm_device *dev,
15137 struct drm_file *filp,
1eb83451 15138 const struct drm_mode_fb_cmd2 *user_mode_cmd)
79e53945 15139{
dcb1394e 15140 struct drm_framebuffer *fb;
05394f39 15141 struct drm_i915_gem_object *obj;
76dc3769 15142 struct drm_mode_fb_cmd2 mode_cmd = *user_mode_cmd;
79e53945 15143
a8ad0bd8 15144 obj = to_intel_bo(drm_gem_object_lookup(filp, mode_cmd.handles[0]));
c8725226 15145 if (&obj->base == NULL)
cce13ff7 15146 return ERR_PTR(-ENOENT);
79e53945 15147
92907cbb 15148 fb = intel_framebuffer_create(dev, &mode_cmd, obj);
dcb1394e
LW
15149 if (IS_ERR(fb))
15150 drm_gem_object_unreference_unlocked(&obj->base);
15151
15152 return fb;
79e53945
JB
15153}
15154
0695726e 15155#ifndef CONFIG_DRM_FBDEV_EMULATION
0632fef6 15156static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
4520f53a
DV
15157{
15158}
15159#endif
15160
79e53945 15161static const struct drm_mode_config_funcs intel_mode_funcs = {
79e53945 15162 .fb_create = intel_user_framebuffer_create,
0632fef6 15163 .output_poll_changed = intel_fbdev_output_poll_changed,
5ee67f1c
MR
15164 .atomic_check = intel_atomic_check,
15165 .atomic_commit = intel_atomic_commit,
de419ab6
ML
15166 .atomic_state_alloc = intel_atomic_state_alloc,
15167 .atomic_state_clear = intel_atomic_state_clear,
79e53945
JB
15168};
15169
88212941
ID
15170/**
15171 * intel_init_display_hooks - initialize the display modesetting hooks
15172 * @dev_priv: device private
15173 */
15174void intel_init_display_hooks(struct drm_i915_private *dev_priv)
e70236a8 15175{
88212941 15176 if (INTEL_INFO(dev_priv)->gen >= 9) {
bc8d7dff 15177 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
5724dbd1
DL
15178 dev_priv->display.get_initial_plane_config =
15179 skylake_get_initial_plane_config;
bc8d7dff
DL
15180 dev_priv->display.crtc_compute_clock =
15181 haswell_crtc_compute_clock;
15182 dev_priv->display.crtc_enable = haswell_crtc_enable;
15183 dev_priv->display.crtc_disable = haswell_crtc_disable;
88212941 15184 } else if (HAS_DDI(dev_priv)) {
0e8ffe1b 15185 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
5724dbd1
DL
15186 dev_priv->display.get_initial_plane_config =
15187 ironlake_get_initial_plane_config;
797d0259
ACO
15188 dev_priv->display.crtc_compute_clock =
15189 haswell_crtc_compute_clock;
4f771f10
PZ
15190 dev_priv->display.crtc_enable = haswell_crtc_enable;
15191 dev_priv->display.crtc_disable = haswell_crtc_disable;
88212941 15192 } else if (HAS_PCH_SPLIT(dev_priv)) {
0e8ffe1b 15193 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
5724dbd1
DL
15194 dev_priv->display.get_initial_plane_config =
15195 ironlake_get_initial_plane_config;
3fb37703
ACO
15196 dev_priv->display.crtc_compute_clock =
15197 ironlake_crtc_compute_clock;
76e5a89c
DV
15198 dev_priv->display.crtc_enable = ironlake_crtc_enable;
15199 dev_priv->display.crtc_disable = ironlake_crtc_disable;
65b3d6a9 15200 } else if (IS_CHERRYVIEW(dev_priv)) {
89b667f8 15201 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
5724dbd1
DL
15202 dev_priv->display.get_initial_plane_config =
15203 i9xx_get_initial_plane_config;
65b3d6a9
ACO
15204 dev_priv->display.crtc_compute_clock = chv_crtc_compute_clock;
15205 dev_priv->display.crtc_enable = valleyview_crtc_enable;
15206 dev_priv->display.crtc_disable = i9xx_crtc_disable;
15207 } else if (IS_VALLEYVIEW(dev_priv)) {
15208 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
15209 dev_priv->display.get_initial_plane_config =
15210 i9xx_get_initial_plane_config;
15211 dev_priv->display.crtc_compute_clock = vlv_crtc_compute_clock;
89b667f8
JB
15212 dev_priv->display.crtc_enable = valleyview_crtc_enable;
15213 dev_priv->display.crtc_disable = i9xx_crtc_disable;
19ec6693
ACO
15214 } else if (IS_G4X(dev_priv)) {
15215 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
15216 dev_priv->display.get_initial_plane_config =
15217 i9xx_get_initial_plane_config;
15218 dev_priv->display.crtc_compute_clock = g4x_crtc_compute_clock;
15219 dev_priv->display.crtc_enable = i9xx_crtc_enable;
15220 dev_priv->display.crtc_disable = i9xx_crtc_disable;
70e8aa21
ACO
15221 } else if (IS_PINEVIEW(dev_priv)) {
15222 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
15223 dev_priv->display.get_initial_plane_config =
15224 i9xx_get_initial_plane_config;
15225 dev_priv->display.crtc_compute_clock = pnv_crtc_compute_clock;
15226 dev_priv->display.crtc_enable = i9xx_crtc_enable;
15227 dev_priv->display.crtc_disable = i9xx_crtc_disable;
81c97f52 15228 } else if (!IS_GEN2(dev_priv)) {
0e8ffe1b 15229 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
5724dbd1
DL
15230 dev_priv->display.get_initial_plane_config =
15231 i9xx_get_initial_plane_config;
d6dfee7a 15232 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
76e5a89c
DV
15233 dev_priv->display.crtc_enable = i9xx_crtc_enable;
15234 dev_priv->display.crtc_disable = i9xx_crtc_disable;
81c97f52
ACO
15235 } else {
15236 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
15237 dev_priv->display.get_initial_plane_config =
15238 i9xx_get_initial_plane_config;
15239 dev_priv->display.crtc_compute_clock = i8xx_crtc_compute_clock;
15240 dev_priv->display.crtc_enable = i9xx_crtc_enable;
15241 dev_priv->display.crtc_disable = i9xx_crtc_disable;
f564048e 15242 }
e70236a8 15243
e70236a8 15244 /* Returns the core display clock speed */
88212941 15245 if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv))
1652d19e
VS
15246 dev_priv->display.get_display_clock_speed =
15247 skylake_get_display_clock_speed;
88212941 15248 else if (IS_BROXTON(dev_priv))
acd3f3d3
BP
15249 dev_priv->display.get_display_clock_speed =
15250 broxton_get_display_clock_speed;
88212941 15251 else if (IS_BROADWELL(dev_priv))
1652d19e
VS
15252 dev_priv->display.get_display_clock_speed =
15253 broadwell_get_display_clock_speed;
88212941 15254 else if (IS_HASWELL(dev_priv))
1652d19e
VS
15255 dev_priv->display.get_display_clock_speed =
15256 haswell_get_display_clock_speed;
88212941 15257 else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
25eb05fc
JB
15258 dev_priv->display.get_display_clock_speed =
15259 valleyview_get_display_clock_speed;
88212941 15260 else if (IS_GEN5(dev_priv))
b37a6434
VS
15261 dev_priv->display.get_display_clock_speed =
15262 ilk_get_display_clock_speed;
88212941
ID
15263 else if (IS_I945G(dev_priv) || IS_BROADWATER(dev_priv) ||
15264 IS_GEN6(dev_priv) || IS_IVYBRIDGE(dev_priv))
e70236a8
JB
15265 dev_priv->display.get_display_clock_speed =
15266 i945_get_display_clock_speed;
88212941 15267 else if (IS_GM45(dev_priv))
34edce2f
VS
15268 dev_priv->display.get_display_clock_speed =
15269 gm45_get_display_clock_speed;
88212941 15270 else if (IS_CRESTLINE(dev_priv))
34edce2f
VS
15271 dev_priv->display.get_display_clock_speed =
15272 i965gm_get_display_clock_speed;
88212941 15273 else if (IS_PINEVIEW(dev_priv))
34edce2f
VS
15274 dev_priv->display.get_display_clock_speed =
15275 pnv_get_display_clock_speed;
88212941 15276 else if (IS_G33(dev_priv) || IS_G4X(dev_priv))
34edce2f
VS
15277 dev_priv->display.get_display_clock_speed =
15278 g33_get_display_clock_speed;
88212941 15279 else if (IS_I915G(dev_priv))
e70236a8
JB
15280 dev_priv->display.get_display_clock_speed =
15281 i915_get_display_clock_speed;
88212941 15282 else if (IS_I945GM(dev_priv) || IS_845G(dev_priv))
e70236a8
JB
15283 dev_priv->display.get_display_clock_speed =
15284 i9xx_misc_get_display_clock_speed;
88212941 15285 else if (IS_I915GM(dev_priv))
e70236a8
JB
15286 dev_priv->display.get_display_clock_speed =
15287 i915gm_get_display_clock_speed;
88212941 15288 else if (IS_I865G(dev_priv))
e70236a8
JB
15289 dev_priv->display.get_display_clock_speed =
15290 i865_get_display_clock_speed;
88212941 15291 else if (IS_I85X(dev_priv))
e70236a8 15292 dev_priv->display.get_display_clock_speed =
1b1d2716 15293 i85x_get_display_clock_speed;
623e01e5 15294 else { /* 830 */
88212941 15295 WARN(!IS_I830(dev_priv), "Unknown platform. Assuming 133 MHz CDCLK\n");
e70236a8
JB
15296 dev_priv->display.get_display_clock_speed =
15297 i830_get_display_clock_speed;
623e01e5 15298 }
e70236a8 15299
88212941 15300 if (IS_GEN5(dev_priv)) {
3bb11b53 15301 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
88212941 15302 } else if (IS_GEN6(dev_priv)) {
3bb11b53 15303 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
88212941 15304 } else if (IS_IVYBRIDGE(dev_priv)) {
3bb11b53
SJ
15305 /* FIXME: detect B0+ stepping and use auto training */
15306 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
88212941 15307 } else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
3bb11b53 15308 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
445e780b
VS
15309 }
15310
15311 if (IS_BROADWELL(dev_priv)) {
15312 dev_priv->display.modeset_commit_cdclk =
15313 broadwell_modeset_commit_cdclk;
15314 dev_priv->display.modeset_calc_cdclk =
15315 broadwell_modeset_calc_cdclk;
88212941 15316 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
27c329ed
ML
15317 dev_priv->display.modeset_commit_cdclk =
15318 valleyview_modeset_commit_cdclk;
15319 dev_priv->display.modeset_calc_cdclk =
15320 valleyview_modeset_calc_cdclk;
88212941 15321 } else if (IS_BROXTON(dev_priv)) {
27c329ed 15322 dev_priv->display.modeset_commit_cdclk =
324513c0 15323 bxt_modeset_commit_cdclk;
27c329ed 15324 dev_priv->display.modeset_calc_cdclk =
324513c0 15325 bxt_modeset_calc_cdclk;
c89e39f3
CT
15326 } else if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv)) {
15327 dev_priv->display.modeset_commit_cdclk =
15328 skl_modeset_commit_cdclk;
15329 dev_priv->display.modeset_calc_cdclk =
15330 skl_modeset_calc_cdclk;
e70236a8 15331 }
5a21b665
DV
15332
15333 switch (INTEL_INFO(dev_priv)->gen) {
15334 case 2:
15335 dev_priv->display.queue_flip = intel_gen2_queue_flip;
15336 break;
15337
15338 case 3:
15339 dev_priv->display.queue_flip = intel_gen3_queue_flip;
15340 break;
15341
15342 case 4:
15343 case 5:
15344 dev_priv->display.queue_flip = intel_gen4_queue_flip;
15345 break;
15346
15347 case 6:
15348 dev_priv->display.queue_flip = intel_gen6_queue_flip;
15349 break;
15350 case 7:
15351 case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
15352 dev_priv->display.queue_flip = intel_gen7_queue_flip;
15353 break;
15354 case 9:
15355 /* Drop through - unsupported since execlist only. */
15356 default:
15357 /* Default just returns -ENODEV to indicate unsupported */
15358 dev_priv->display.queue_flip = intel_default_queue_flip;
15359 }
e70236a8
JB
15360}
15361
b690e96c
JB
15362/*
15363 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
15364 * resume, or other times. This quirk makes sure that's the case for
15365 * affected systems.
15366 */
0206e353 15367static void quirk_pipea_force(struct drm_device *dev)
b690e96c
JB
15368{
15369 struct drm_i915_private *dev_priv = dev->dev_private;
15370
15371 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
bc0daf48 15372 DRM_INFO("applying pipe a force quirk\n");
b690e96c
JB
15373}
15374
b6b5d049
VS
15375static void quirk_pipeb_force(struct drm_device *dev)
15376{
15377 struct drm_i915_private *dev_priv = dev->dev_private;
15378
15379 dev_priv->quirks |= QUIRK_PIPEB_FORCE;
15380 DRM_INFO("applying pipe b force quirk\n");
15381}
15382
435793df
KP
15383/*
15384 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
15385 */
15386static void quirk_ssc_force_disable(struct drm_device *dev)
15387{
15388 struct drm_i915_private *dev_priv = dev->dev_private;
15389 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
bc0daf48 15390 DRM_INFO("applying lvds SSC disable quirk\n");
435793df
KP
15391}
15392
4dca20ef 15393/*
5a15ab5b
CE
15394 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
15395 * brightness value
4dca20ef
CE
15396 */
15397static void quirk_invert_brightness(struct drm_device *dev)
15398{
15399 struct drm_i915_private *dev_priv = dev->dev_private;
15400 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
bc0daf48 15401 DRM_INFO("applying inverted panel brightness quirk\n");
435793df
KP
15402}
15403
9c72cc6f
SD
15404/* Some VBT's incorrectly indicate no backlight is present */
15405static void quirk_backlight_present(struct drm_device *dev)
15406{
15407 struct drm_i915_private *dev_priv = dev->dev_private;
15408 dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
15409 DRM_INFO("applying backlight present quirk\n");
15410}
15411
b690e96c
JB
15412struct intel_quirk {
15413 int device;
15414 int subsystem_vendor;
15415 int subsystem_device;
15416 void (*hook)(struct drm_device *dev);
15417};
15418
5f85f176
EE
15419/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
15420struct intel_dmi_quirk {
15421 void (*hook)(struct drm_device *dev);
15422 const struct dmi_system_id (*dmi_id_list)[];
15423};
15424
15425static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
15426{
15427 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
15428 return 1;
15429}
15430
15431static const struct intel_dmi_quirk intel_dmi_quirks[] = {
15432 {
15433 .dmi_id_list = &(const struct dmi_system_id[]) {
15434 {
15435 .callback = intel_dmi_reverse_brightness,
15436 .ident = "NCR Corporation",
15437 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
15438 DMI_MATCH(DMI_PRODUCT_NAME, ""),
15439 },
15440 },
15441 { } /* terminating entry */
15442 },
15443 .hook = quirk_invert_brightness,
15444 },
15445};
15446
c43b5634 15447static struct intel_quirk intel_quirks[] = {
b690e96c
JB
15448 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
15449 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
15450
b690e96c
JB
15451 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
15452 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
15453
5f080c0f
VS
15454 /* 830 needs to leave pipe A & dpll A up */
15455 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
15456
b6b5d049
VS
15457 /* 830 needs to leave pipe B & dpll B up */
15458 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
15459
435793df
KP
15460 /* Lenovo U160 cannot use SSC on LVDS */
15461 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
070d329a
MAS
15462
15463 /* Sony Vaio Y cannot use SSC on LVDS */
15464 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
5a15ab5b 15465
be505f64
AH
15466 /* Acer Aspire 5734Z must invert backlight brightness */
15467 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
15468
15469 /* Acer/eMachines G725 */
15470 { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
15471
15472 /* Acer/eMachines e725 */
15473 { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
15474
15475 /* Acer/Packard Bell NCL20 */
15476 { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
15477
15478 /* Acer Aspire 4736Z */
15479 { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
0f540c3a
JN
15480
15481 /* Acer Aspire 5336 */
15482 { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
2e93a1aa
SD
15483
15484 /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
15485 { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
d4967d8c 15486
dfb3d47b
SD
15487 /* Acer C720 Chromebook (Core i3 4005U) */
15488 { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
15489
b2a9601c 15490 /* Apple Macbook 2,1 (Core 2 T7400) */
15491 { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
15492
1b9448b0
JN
15493 /* Apple Macbook 4,1 */
15494 { 0x2a02, 0x106b, 0x00a1, quirk_backlight_present },
15495
d4967d8c
SD
15496 /* Toshiba CB35 Chromebook (Celeron 2955U) */
15497 { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
724cb06f
SD
15498
15499 /* HP Chromebook 14 (Celeron 2955U) */
15500 { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
cf6f0af9
JN
15501
15502 /* Dell Chromebook 11 */
15503 { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
9be64eee
JN
15504
15505 /* Dell Chromebook 11 (2015 version) */
15506 { 0x0a16, 0x1028, 0x0a35, quirk_backlight_present },
b690e96c
JB
15507};
15508
15509static void intel_init_quirks(struct drm_device *dev)
15510{
15511 struct pci_dev *d = dev->pdev;
15512 int i;
15513
15514 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
15515 struct intel_quirk *q = &intel_quirks[i];
15516
15517 if (d->device == q->device &&
15518 (d->subsystem_vendor == q->subsystem_vendor ||
15519 q->subsystem_vendor == PCI_ANY_ID) &&
15520 (d->subsystem_device == q->subsystem_device ||
15521 q->subsystem_device == PCI_ANY_ID))
15522 q->hook(dev);
15523 }
5f85f176
EE
15524 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
15525 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
15526 intel_dmi_quirks[i].hook(dev);
15527 }
b690e96c
JB
15528}
15529
9cce37f4
JB
15530/* Disable the VGA plane that we never use */
15531static void i915_disable_vga(struct drm_device *dev)
15532{
15533 struct drm_i915_private *dev_priv = dev->dev_private;
15534 u8 sr1;
f0f59a00 15535 i915_reg_t vga_reg = i915_vgacntrl_reg(dev);
9cce37f4 15536
2b37c616 15537 /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
9cce37f4 15538 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
3fdcf431 15539 outb(SR01, VGA_SR_INDEX);
9cce37f4
JB
15540 sr1 = inb(VGA_SR_DATA);
15541 outb(sr1 | 1<<5, VGA_SR_DATA);
15542 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
15543 udelay(300);
15544
01f5a626 15545 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
9cce37f4
JB
15546 POSTING_READ(vga_reg);
15547}
15548
f817586c
DV
15549void intel_modeset_init_hw(struct drm_device *dev)
15550{
1a617b77
ML
15551 struct drm_i915_private *dev_priv = dev->dev_private;
15552
b6283055 15553 intel_update_cdclk(dev);
1a617b77
ML
15554
15555 dev_priv->atomic_cdclk_freq = dev_priv->cdclk_freq;
15556
f817586c 15557 intel_init_clock_gating(dev);
dc97997a 15558 intel_enable_gt_powersave(dev_priv);
f817586c
DV
15559}
15560
d93c0372
MR
15561/*
15562 * Calculate what we think the watermarks should be for the state we've read
15563 * out of the hardware and then immediately program those watermarks so that
15564 * we ensure the hardware settings match our internal state.
15565 *
15566 * We can calculate what we think WM's should be by creating a duplicate of the
15567 * current state (which was constructed during hardware readout) and running it
15568 * through the atomic check code to calculate new watermark values in the
15569 * state object.
15570 */
15571static void sanitize_watermarks(struct drm_device *dev)
15572{
15573 struct drm_i915_private *dev_priv = to_i915(dev);
15574 struct drm_atomic_state *state;
15575 struct drm_crtc *crtc;
15576 struct drm_crtc_state *cstate;
15577 struct drm_modeset_acquire_ctx ctx;
15578 int ret;
15579 int i;
15580
15581 /* Only supported on platforms that use atomic watermark design */
ed4a6a7c 15582 if (!dev_priv->display.optimize_watermarks)
d93c0372
MR
15583 return;
15584
15585 /*
15586 * We need to hold connection_mutex before calling duplicate_state so
15587 * that the connector loop is protected.
15588 */
15589 drm_modeset_acquire_init(&ctx, 0);
15590retry:
0cd1262d 15591 ret = drm_modeset_lock_all_ctx(dev, &ctx);
d93c0372
MR
15592 if (ret == -EDEADLK) {
15593 drm_modeset_backoff(&ctx);
15594 goto retry;
15595 } else if (WARN_ON(ret)) {
0cd1262d 15596 goto fail;
d93c0372
MR
15597 }
15598
15599 state = drm_atomic_helper_duplicate_state(dev, &ctx);
15600 if (WARN_ON(IS_ERR(state)))
0cd1262d 15601 goto fail;
d93c0372 15602
ed4a6a7c
MR
15603 /*
15604 * Hardware readout is the only time we don't want to calculate
15605 * intermediate watermarks (since we don't trust the current
15606 * watermarks).
15607 */
15608 to_intel_atomic_state(state)->skip_intermediate_wm = true;
15609
d93c0372
MR
15610 ret = intel_atomic_check(dev, state);
15611 if (ret) {
15612 /*
15613 * If we fail here, it means that the hardware appears to be
15614 * programmed in a way that shouldn't be possible, given our
15615 * understanding of watermark requirements. This might mean a
15616 * mistake in the hardware readout code or a mistake in the
15617 * watermark calculations for a given platform. Raise a WARN
15618 * so that this is noticeable.
15619 *
15620 * If this actually happens, we'll have to just leave the
15621 * BIOS-programmed watermarks untouched and hope for the best.
15622 */
15623 WARN(true, "Could not determine valid watermarks for inherited state\n");
0cd1262d 15624 goto fail;
d93c0372
MR
15625 }
15626
15627 /* Write calculated watermark values back */
d93c0372
MR
15628 for_each_crtc_in_state(state, crtc, cstate, i) {
15629 struct intel_crtc_state *cs = to_intel_crtc_state(cstate);
15630
ed4a6a7c
MR
15631 cs->wm.need_postvbl_update = true;
15632 dev_priv->display.optimize_watermarks(cs);
d93c0372
MR
15633 }
15634
15635 drm_atomic_state_free(state);
0cd1262d 15636fail:
d93c0372
MR
15637 drm_modeset_drop_locks(&ctx);
15638 drm_modeset_acquire_fini(&ctx);
15639}
15640
79e53945
JB
15641void intel_modeset_init(struct drm_device *dev)
15642{
72e96d64
JL
15643 struct drm_i915_private *dev_priv = to_i915(dev);
15644 struct i915_ggtt *ggtt = &dev_priv->ggtt;
1fe47785 15645 int sprite, ret;
8cc87b75 15646 enum pipe pipe;
46f297fb 15647 struct intel_crtc *crtc;
79e53945
JB
15648
15649 drm_mode_config_init(dev);
15650
15651 dev->mode_config.min_width = 0;
15652 dev->mode_config.min_height = 0;
15653
019d96cb
DA
15654 dev->mode_config.preferred_depth = 24;
15655 dev->mode_config.prefer_shadow = 1;
15656
25bab385
TU
15657 dev->mode_config.allow_fb_modifiers = true;
15658
e6ecefaa 15659 dev->mode_config.funcs = &intel_mode_funcs;
79e53945 15660
b690e96c
JB
15661 intel_init_quirks(dev);
15662
1fa61106
ED
15663 intel_init_pm(dev);
15664
e3c74757
BW
15665 if (INTEL_INFO(dev)->num_pipes == 0)
15666 return;
15667
69f92f67
LW
15668 /*
15669 * There may be no VBT; and if the BIOS enabled SSC we can
15670 * just keep using it to avoid unnecessary flicker. Whereas if the
15671 * BIOS isn't using it, don't assume it will work even if the VBT
15672 * indicates as much.
15673 */
15674 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)) {
15675 bool bios_lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
15676 DREF_SSC1_ENABLE);
15677
15678 if (dev_priv->vbt.lvds_use_ssc != bios_lvds_use_ssc) {
15679 DRM_DEBUG_KMS("SSC %sabled by BIOS, overriding VBT which says %sabled\n",
15680 bios_lvds_use_ssc ? "en" : "dis",
15681 dev_priv->vbt.lvds_use_ssc ? "en" : "dis");
15682 dev_priv->vbt.lvds_use_ssc = bios_lvds_use_ssc;
15683 }
15684 }
15685
a6c45cf0
CW
15686 if (IS_GEN2(dev)) {
15687 dev->mode_config.max_width = 2048;
15688 dev->mode_config.max_height = 2048;
15689 } else if (IS_GEN3(dev)) {
5e4d6fa7
KP
15690 dev->mode_config.max_width = 4096;
15691 dev->mode_config.max_height = 4096;
79e53945 15692 } else {
a6c45cf0
CW
15693 dev->mode_config.max_width = 8192;
15694 dev->mode_config.max_height = 8192;
79e53945 15695 }
068be561 15696
dc41c154
VS
15697 if (IS_845G(dev) || IS_I865G(dev)) {
15698 dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
15699 dev->mode_config.cursor_height = 1023;
15700 } else if (IS_GEN2(dev)) {
068be561
DL
15701 dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
15702 dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
15703 } else {
15704 dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
15705 dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
15706 }
15707
72e96d64 15708 dev->mode_config.fb_base = ggtt->mappable_base;
79e53945 15709
28c97730 15710 DRM_DEBUG_KMS("%d display pipe%s available.\n",
7eb552ae
BW
15711 INTEL_INFO(dev)->num_pipes,
15712 INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
79e53945 15713
055e393f 15714 for_each_pipe(dev_priv, pipe) {
8cc87b75 15715 intel_crtc_init(dev, pipe);
3bdcfc0c 15716 for_each_sprite(dev_priv, pipe, sprite) {
1fe47785 15717 ret = intel_plane_init(dev, pipe, sprite);
7f1f3851 15718 if (ret)
06da8da2 15719 DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
1fe47785 15720 pipe_name(pipe), sprite_name(pipe, sprite), ret);
7f1f3851 15721 }
79e53945
JB
15722 }
15723
bfa7df01
VS
15724 intel_update_czclk(dev_priv);
15725 intel_update_cdclk(dev);
15726
e72f9fbf 15727 intel_shared_dpll_init(dev);
ee7b9f93 15728
b2045352
VS
15729 if (dev_priv->max_cdclk_freq == 0)
15730 intel_update_max_cdclk(dev);
15731
9cce37f4
JB
15732 /* Just disable it once at startup */
15733 i915_disable_vga(dev);
79e53945 15734 intel_setup_outputs(dev);
11be49eb 15735
6e9f798d 15736 drm_modeset_lock_all(dev);
043e9bda 15737 intel_modeset_setup_hw_state(dev);
6e9f798d 15738 drm_modeset_unlock_all(dev);
46f297fb 15739
d3fcc808 15740 for_each_intel_crtc(dev, crtc) {
eeebeac5
ML
15741 struct intel_initial_plane_config plane_config = {};
15742
46f297fb
JB
15743 if (!crtc->active)
15744 continue;
15745
46f297fb 15746 /*
46f297fb
JB
15747 * Note that reserving the BIOS fb up front prevents us
15748 * from stuffing other stolen allocations like the ring
15749 * on top. This prevents some ugliness at boot time, and
15750 * can even allow for smooth boot transitions if the BIOS
15751 * fb is large enough for the active pipe configuration.
15752 */
eeebeac5
ML
15753 dev_priv->display.get_initial_plane_config(crtc,
15754 &plane_config);
15755
15756 /*
15757 * If the fb is shared between multiple heads, we'll
15758 * just get the first one.
15759 */
15760 intel_find_initial_plane_obj(crtc, &plane_config);
46f297fb 15761 }
d93c0372
MR
15762
15763 /*
15764 * Make sure hardware watermarks really match the state we read out.
15765 * Note that we need to do this after reconstructing the BIOS fb's
15766 * since the watermark calculation done here will use pstate->fb.
15767 */
15768 sanitize_watermarks(dev);
2c7111db
CW
15769}
15770
7fad798e
DV
15771static void intel_enable_pipe_a(struct drm_device *dev)
15772{
15773 struct intel_connector *connector;
15774 struct drm_connector *crt = NULL;
15775 struct intel_load_detect_pipe load_detect_temp;
208bf9fd 15776 struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
7fad798e
DV
15777
15778 /* We can't just switch on the pipe A, we need to set things up with a
15779 * proper mode and output configuration. As a gross hack, enable pipe A
15780 * by enabling the load detect pipe once. */
3a3371ff 15781 for_each_intel_connector(dev, connector) {
7fad798e
DV
15782 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
15783 crt = &connector->base;
15784 break;
15785 }
15786 }
15787
15788 if (!crt)
15789 return;
15790
208bf9fd 15791 if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
49172fee 15792 intel_release_load_detect_pipe(crt, &load_detect_temp, ctx);
7fad798e
DV
15793}
15794
fa555837
DV
15795static bool
15796intel_check_plane_mapping(struct intel_crtc *crtc)
15797{
7eb552ae
BW
15798 struct drm_device *dev = crtc->base.dev;
15799 struct drm_i915_private *dev_priv = dev->dev_private;
649636ef 15800 u32 val;
fa555837 15801
7eb552ae 15802 if (INTEL_INFO(dev)->num_pipes == 1)
fa555837
DV
15803 return true;
15804
649636ef 15805 val = I915_READ(DSPCNTR(!crtc->plane));
fa555837
DV
15806
15807 if ((val & DISPLAY_PLANE_ENABLE) &&
15808 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
15809 return false;
15810
15811 return true;
15812}
15813
02e93c35
VS
15814static bool intel_crtc_has_encoders(struct intel_crtc *crtc)
15815{
15816 struct drm_device *dev = crtc->base.dev;
15817 struct intel_encoder *encoder;
15818
15819 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
15820 return true;
15821
15822 return false;
15823}
15824
dd756198
VS
15825static bool intel_encoder_has_connectors(struct intel_encoder *encoder)
15826{
15827 struct drm_device *dev = encoder->base.dev;
15828 struct intel_connector *connector;
15829
15830 for_each_connector_on_encoder(dev, &encoder->base, connector)
15831 return true;
15832
15833 return false;
15834}
15835
24929352
DV
15836static void intel_sanitize_crtc(struct intel_crtc *crtc)
15837{
15838 struct drm_device *dev = crtc->base.dev;
15839 struct drm_i915_private *dev_priv = dev->dev_private;
4d1de975 15840 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
24929352 15841
24929352 15842 /* Clear any frame start delays used for debugging left by the BIOS */
4d1de975
JN
15843 if (!transcoder_is_dsi(cpu_transcoder)) {
15844 i915_reg_t reg = PIPECONF(cpu_transcoder);
15845
15846 I915_WRITE(reg,
15847 I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
15848 }
24929352 15849
d3eaf884 15850 /* restore vblank interrupts to correct state */
9625604c 15851 drm_crtc_vblank_reset(&crtc->base);
d297e103 15852 if (crtc->active) {
f9cd7b88
VS
15853 struct intel_plane *plane;
15854
9625604c 15855 drm_crtc_vblank_on(&crtc->base);
f9cd7b88
VS
15856
15857 /* Disable everything but the primary plane */
15858 for_each_intel_plane_on_crtc(dev, crtc, plane) {
15859 if (plane->base.type == DRM_PLANE_TYPE_PRIMARY)
15860 continue;
15861
15862 plane->disable_plane(&plane->base, &crtc->base);
15863 }
9625604c 15864 }
d3eaf884 15865
24929352 15866 /* We need to sanitize the plane -> pipe mapping first because this will
fa555837
DV
15867 * disable the crtc (and hence change the state) if it is wrong. Note
15868 * that gen4+ has a fixed plane -> pipe mapping. */
15869 if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
24929352
DV
15870 bool plane;
15871
78108b7c
VS
15872 DRM_DEBUG_KMS("[CRTC:%d:%s] wrong plane connection detected!\n",
15873 crtc->base.base.id, crtc->base.name);
24929352
DV
15874
15875 /* Pipe has the wrong plane attached and the plane is active.
15876 * Temporarily change the plane mapping and disable everything
15877 * ... */
15878 plane = crtc->plane;
b70709a6 15879 to_intel_plane_state(crtc->base.primary->state)->visible = true;
24929352 15880 crtc->plane = !plane;
b17d48e2 15881 intel_crtc_disable_noatomic(&crtc->base);
24929352 15882 crtc->plane = plane;
24929352 15883 }
24929352 15884
7fad798e
DV
15885 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
15886 crtc->pipe == PIPE_A && !crtc->active) {
15887 /* BIOS forgot to enable pipe A, this mostly happens after
15888 * resume. Force-enable the pipe to fix this, the update_dpms
15889 * call below we restore the pipe to the right state, but leave
15890 * the required bits on. */
15891 intel_enable_pipe_a(dev);
15892 }
15893
24929352
DV
15894 /* Adjust the state of the output pipe according to whether we
15895 * have active connectors/encoders. */
842e0307 15896 if (crtc->active && !intel_crtc_has_encoders(crtc))
b17d48e2 15897 intel_crtc_disable_noatomic(&crtc->base);
24929352 15898
a3ed6aad 15899 if (crtc->active || HAS_GMCH_DISPLAY(dev)) {
4cc31489
DV
15900 /*
15901 * We start out with underrun reporting disabled to avoid races.
15902 * For correct bookkeeping mark this on active crtcs.
15903 *
c5ab3bc0
DV
15904 * Also on gmch platforms we dont have any hardware bits to
15905 * disable the underrun reporting. Which means we need to start
15906 * out with underrun reporting disabled also on inactive pipes,
15907 * since otherwise we'll complain about the garbage we read when
15908 * e.g. coming up after runtime pm.
15909 *
4cc31489
DV
15910 * No protection against concurrent access is required - at
15911 * worst a fifo underrun happens which also sets this to false.
15912 */
15913 crtc->cpu_fifo_underrun_disabled = true;
15914 crtc->pch_fifo_underrun_disabled = true;
15915 }
24929352
DV
15916}
15917
15918static void intel_sanitize_encoder(struct intel_encoder *encoder)
15919{
15920 struct intel_connector *connector;
15921 struct drm_device *dev = encoder->base.dev;
15922
15923 /* We need to check both for a crtc link (meaning that the
15924 * encoder is active and trying to read from a pipe) and the
15925 * pipe itself being active. */
15926 bool has_active_crtc = encoder->base.crtc &&
15927 to_intel_crtc(encoder->base.crtc)->active;
15928
dd756198 15929 if (intel_encoder_has_connectors(encoder) && !has_active_crtc) {
24929352
DV
15930 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
15931 encoder->base.base.id,
8e329a03 15932 encoder->base.name);
24929352
DV
15933
15934 /* Connector is active, but has no active pipe. This is
15935 * fallout from our resume register restoring. Disable
15936 * the encoder manually again. */
15937 if (encoder->base.crtc) {
15938 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
15939 encoder->base.base.id,
8e329a03 15940 encoder->base.name);
24929352 15941 encoder->disable(encoder);
a62d1497
VS
15942 if (encoder->post_disable)
15943 encoder->post_disable(encoder);
24929352 15944 }
7f1950fb 15945 encoder->base.crtc = NULL;
24929352
DV
15946
15947 /* Inconsistent output/port/pipe state happens presumably due to
15948 * a bug in one of the get_hw_state functions. Or someplace else
15949 * in our code, like the register restore mess on resume. Clamp
15950 * things to off as a safer default. */
3a3371ff 15951 for_each_intel_connector(dev, connector) {
24929352
DV
15952 if (connector->encoder != encoder)
15953 continue;
7f1950fb
EE
15954 connector->base.dpms = DRM_MODE_DPMS_OFF;
15955 connector->base.encoder = NULL;
24929352
DV
15956 }
15957 }
15958 /* Enabled encoders without active connectors will be fixed in
15959 * the crtc fixup. */
15960}
15961
04098753 15962void i915_redisable_vga_power_on(struct drm_device *dev)
0fde901f
KM
15963{
15964 struct drm_i915_private *dev_priv = dev->dev_private;
f0f59a00 15965 i915_reg_t vga_reg = i915_vgacntrl_reg(dev);
0fde901f 15966
04098753
ID
15967 if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
15968 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
15969 i915_disable_vga(dev);
15970 }
15971}
15972
15973void i915_redisable_vga(struct drm_device *dev)
15974{
15975 struct drm_i915_private *dev_priv = dev->dev_private;
15976
8dc8a27c
PZ
15977 /* This function can be called both from intel_modeset_setup_hw_state or
15978 * at a very early point in our resume sequence, where the power well
15979 * structures are not yet restored. Since this function is at a very
15980 * paranoid "someone might have enabled VGA while we were not looking"
15981 * level, just check if the power well is enabled instead of trying to
15982 * follow the "don't touch the power well if we don't need it" policy
15983 * the rest of the driver uses. */
6392f847 15984 if (!intel_display_power_get_if_enabled(dev_priv, POWER_DOMAIN_VGA))
8dc8a27c
PZ
15985 return;
15986
04098753 15987 i915_redisable_vga_power_on(dev);
6392f847
ID
15988
15989 intel_display_power_put(dev_priv, POWER_DOMAIN_VGA);
0fde901f
KM
15990}
15991
f9cd7b88 15992static bool primary_get_hw_state(struct intel_plane *plane)
98ec7739 15993{
f9cd7b88 15994 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
98ec7739 15995
f9cd7b88 15996 return I915_READ(DSPCNTR(plane->plane)) & DISPLAY_PLANE_ENABLE;
d032ffa0
ML
15997}
15998
f9cd7b88
VS
15999/* FIXME read out full plane state for all planes */
16000static void readout_plane_state(struct intel_crtc *crtc)
d032ffa0 16001{
b26d3ea3 16002 struct drm_plane *primary = crtc->base.primary;
f9cd7b88 16003 struct intel_plane_state *plane_state =
b26d3ea3 16004 to_intel_plane_state(primary->state);
d032ffa0 16005
19b8d387 16006 plane_state->visible = crtc->active &&
b26d3ea3
ML
16007 primary_get_hw_state(to_intel_plane(primary));
16008
16009 if (plane_state->visible)
16010 crtc->base.state->plane_mask |= 1 << drm_plane_index(primary);
98ec7739
VS
16011}
16012
30e984df 16013static void intel_modeset_readout_hw_state(struct drm_device *dev)
24929352
DV
16014{
16015 struct drm_i915_private *dev_priv = dev->dev_private;
16016 enum pipe pipe;
24929352
DV
16017 struct intel_crtc *crtc;
16018 struct intel_encoder *encoder;
16019 struct intel_connector *connector;
5358901f 16020 int i;
24929352 16021
565602d7
ML
16022 dev_priv->active_crtcs = 0;
16023
d3fcc808 16024 for_each_intel_crtc(dev, crtc) {
565602d7
ML
16025 struct intel_crtc_state *crtc_state = crtc->config;
16026 int pixclk = 0;
3b117c8f 16027
ec2dc6a0 16028 __drm_atomic_helper_crtc_destroy_state(&crtc_state->base);
565602d7
ML
16029 memset(crtc_state, 0, sizeof(*crtc_state));
16030 crtc_state->base.crtc = &crtc->base;
24929352 16031
565602d7
ML
16032 crtc_state->base.active = crtc_state->base.enable =
16033 dev_priv->display.get_pipe_config(crtc, crtc_state);
16034
16035 crtc->base.enabled = crtc_state->base.enable;
16036 crtc->active = crtc_state->base.active;
16037
16038 if (crtc_state->base.active) {
16039 dev_priv->active_crtcs |= 1 << crtc->pipe;
16040
c89e39f3 16041 if (INTEL_GEN(dev_priv) >= 9 || IS_BROADWELL(dev_priv))
565602d7 16042 pixclk = ilk_pipe_pixel_rate(crtc_state);
9558d15d 16043 else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
565602d7
ML
16044 pixclk = crtc_state->base.adjusted_mode.crtc_clock;
16045 else
16046 WARN_ON(dev_priv->display.modeset_calc_cdclk);
9558d15d
VS
16047
16048 /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
16049 if (IS_BROADWELL(dev_priv) && crtc_state->ips_enabled)
16050 pixclk = DIV_ROUND_UP(pixclk * 100, 95);
565602d7
ML
16051 }
16052
16053 dev_priv->min_pixclk[crtc->pipe] = pixclk;
b70709a6 16054
f9cd7b88 16055 readout_plane_state(crtc);
24929352 16056
78108b7c
VS
16057 DRM_DEBUG_KMS("[CRTC:%d:%s] hw state readout: %s\n",
16058 crtc->base.base.id, crtc->base.name,
24929352
DV
16059 crtc->active ? "enabled" : "disabled");
16060 }
16061
5358901f
DV
16062 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
16063 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
16064
2edd6443
ACO
16065 pll->on = pll->funcs.get_hw_state(dev_priv, pll,
16066 &pll->config.hw_state);
3e369b76 16067 pll->config.crtc_mask = 0;
d3fcc808 16068 for_each_intel_crtc(dev, crtc) {
2dd66ebd 16069 if (crtc->active && crtc->config->shared_dpll == pll)
3e369b76 16070 pll->config.crtc_mask |= 1 << crtc->pipe;
5358901f 16071 }
2dd66ebd 16072 pll->active_mask = pll->config.crtc_mask;
5358901f 16073
1e6f2ddc 16074 DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
3e369b76 16075 pll->name, pll->config.crtc_mask, pll->on);
5358901f
DV
16076 }
16077
b2784e15 16078 for_each_intel_encoder(dev, encoder) {
24929352
DV
16079 pipe = 0;
16080
16081 if (encoder->get_hw_state(encoder, &pipe)) {
045ac3b5
JB
16082 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
16083 encoder->base.crtc = &crtc->base;
6e3c9717 16084 encoder->get_config(encoder, crtc->config);
24929352
DV
16085 } else {
16086 encoder->base.crtc = NULL;
16087 }
16088
6f2bcceb 16089 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
24929352 16090 encoder->base.base.id,
8e329a03 16091 encoder->base.name,
24929352 16092 encoder->base.crtc ? "enabled" : "disabled",
6f2bcceb 16093 pipe_name(pipe));
24929352
DV
16094 }
16095
3a3371ff 16096 for_each_intel_connector(dev, connector) {
24929352
DV
16097 if (connector->get_hw_state(connector)) {
16098 connector->base.dpms = DRM_MODE_DPMS_ON;
2aa974c9
ML
16099
16100 encoder = connector->encoder;
16101 connector->base.encoder = &encoder->base;
16102
16103 if (encoder->base.crtc &&
16104 encoder->base.crtc->state->active) {
16105 /*
16106 * This has to be done during hardware readout
16107 * because anything calling .crtc_disable may
16108 * rely on the connector_mask being accurate.
16109 */
16110 encoder->base.crtc->state->connector_mask |=
16111 1 << drm_connector_index(&connector->base);
e87a52b3
ML
16112 encoder->base.crtc->state->encoder_mask |=
16113 1 << drm_encoder_index(&encoder->base);
2aa974c9
ML
16114 }
16115
24929352
DV
16116 } else {
16117 connector->base.dpms = DRM_MODE_DPMS_OFF;
16118 connector->base.encoder = NULL;
16119 }
16120 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
16121 connector->base.base.id,
c23cc417 16122 connector->base.name,
24929352
DV
16123 connector->base.encoder ? "enabled" : "disabled");
16124 }
7f4c6284
VS
16125
16126 for_each_intel_crtc(dev, crtc) {
16127 crtc->base.hwmode = crtc->config->base.adjusted_mode;
16128
16129 memset(&crtc->base.mode, 0, sizeof(crtc->base.mode));
16130 if (crtc->base.state->active) {
16131 intel_mode_from_pipe_config(&crtc->base.mode, crtc->config);
16132 intel_mode_from_pipe_config(&crtc->base.state->adjusted_mode, crtc->config);
16133 WARN_ON(drm_atomic_set_mode_for_crtc(crtc->base.state, &crtc->base.mode));
16134
16135 /*
16136 * The initial mode needs to be set in order to keep
16137 * the atomic core happy. It wants a valid mode if the
16138 * crtc's enabled, so we do the above call.
16139 *
16140 * At this point some state updated by the connectors
16141 * in their ->detect() callback has not run yet, so
16142 * no recalculation can be done yet.
16143 *
16144 * Even if we could do a recalculation and modeset
16145 * right now it would cause a double modeset if
16146 * fbdev or userspace chooses a different initial mode.
16147 *
16148 * If that happens, someone indicated they wanted a
16149 * mode change, which means it's safe to do a full
16150 * recalculation.
16151 */
16152 crtc->base.state->mode.private_flags = I915_MODE_FLAG_INHERITED;
9eca6832
VS
16153
16154 drm_calc_timestamping_constants(&crtc->base, &crtc->base.hwmode);
16155 update_scanline_offset(crtc);
7f4c6284 16156 }
e3b247da
VS
16157
16158 intel_pipe_config_sanity_check(dev_priv, crtc->config);
7f4c6284 16159 }
30e984df
DV
16160}
16161
043e9bda
ML
16162/* Scan out the current hw modeset state,
16163 * and sanitizes it to the current state
16164 */
16165static void
16166intel_modeset_setup_hw_state(struct drm_device *dev)
30e984df
DV
16167{
16168 struct drm_i915_private *dev_priv = dev->dev_private;
16169 enum pipe pipe;
30e984df
DV
16170 struct intel_crtc *crtc;
16171 struct intel_encoder *encoder;
35c95375 16172 int i;
30e984df
DV
16173
16174 intel_modeset_readout_hw_state(dev);
24929352
DV
16175
16176 /* HW state is read out, now we need to sanitize this mess. */
b2784e15 16177 for_each_intel_encoder(dev, encoder) {
24929352
DV
16178 intel_sanitize_encoder(encoder);
16179 }
16180
055e393f 16181 for_each_pipe(dev_priv, pipe) {
24929352
DV
16182 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
16183 intel_sanitize_crtc(crtc);
6e3c9717
ACO
16184 intel_dump_pipe_config(crtc, crtc->config,
16185 "[setup_hw_state]");
24929352 16186 }
9a935856 16187
d29b2f9d
ACO
16188 intel_modeset_update_connector_atomic_state(dev);
16189
35c95375
DV
16190 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
16191 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
16192
2dd66ebd 16193 if (!pll->on || pll->active_mask)
35c95375
DV
16194 continue;
16195
16196 DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
16197
2edd6443 16198 pll->funcs.disable(dev_priv, pll);
35c95375
DV
16199 pll->on = false;
16200 }
16201
666a4537 16202 if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev))
6eb1a681
VS
16203 vlv_wm_get_hw_state(dev);
16204 else if (IS_GEN9(dev))
3078999f
PB
16205 skl_wm_get_hw_state(dev);
16206 else if (HAS_PCH_SPLIT(dev))
243e6a44 16207 ilk_wm_get_hw_state(dev);
292b990e
ML
16208
16209 for_each_intel_crtc(dev, crtc) {
16210 unsigned long put_domains;
16211
74bff5f9 16212 put_domains = modeset_get_crtc_power_domains(&crtc->base, crtc->config);
292b990e
ML
16213 if (WARN_ON(put_domains))
16214 modeset_put_power_domains(dev_priv, put_domains);
16215 }
16216 intel_display_set_init_power(dev_priv, false);
010cf73d
PZ
16217
16218 intel_fbc_init_pipe_state(dev_priv);
043e9bda 16219}
7d0bc1ea 16220
043e9bda
ML
16221void intel_display_resume(struct drm_device *dev)
16222{
e2c8b870
ML
16223 struct drm_i915_private *dev_priv = to_i915(dev);
16224 struct drm_atomic_state *state = dev_priv->modeset_restore_state;
16225 struct drm_modeset_acquire_ctx ctx;
043e9bda 16226 int ret;
e2c8b870 16227 bool setup = false;
f30da187 16228
e2c8b870 16229 dev_priv->modeset_restore_state = NULL;
043e9bda 16230
ea49c9ac
ML
16231 /*
16232 * This is a cludge because with real atomic modeset mode_config.mutex
16233 * won't be taken. Unfortunately some probed state like
16234 * audio_codec_enable is still protected by mode_config.mutex, so lock
16235 * it here for now.
16236 */
16237 mutex_lock(&dev->mode_config.mutex);
e2c8b870 16238 drm_modeset_acquire_init(&ctx, 0);
043e9bda 16239
e2c8b870
ML
16240retry:
16241 ret = drm_modeset_lock_all_ctx(dev, &ctx);
043e9bda 16242
e2c8b870
ML
16243 if (ret == 0 && !setup) {
16244 setup = true;
043e9bda 16245
e2c8b870
ML
16246 intel_modeset_setup_hw_state(dev);
16247 i915_redisable_vga(dev);
45e2b5f6 16248 }
8af6cf88 16249
e2c8b870
ML
16250 if (ret == 0 && state) {
16251 struct drm_crtc_state *crtc_state;
16252 struct drm_crtc *crtc;
16253 int i;
043e9bda 16254
e2c8b870
ML
16255 state->acquire_ctx = &ctx;
16256
e3d5457c
VS
16257 /* ignore any reset values/BIOS leftovers in the WM registers */
16258 to_intel_atomic_state(state)->skip_intermediate_wm = true;
16259
e2c8b870
ML
16260 for_each_crtc_in_state(state, crtc, crtc_state, i) {
16261 /*
16262 * Force recalculation even if we restore
16263 * current state. With fast modeset this may not result
16264 * in a modeset when the state is compatible.
16265 */
16266 crtc_state->mode_changed = true;
16267 }
16268
16269 ret = drm_atomic_commit(state);
043e9bda
ML
16270 }
16271
e2c8b870
ML
16272 if (ret == -EDEADLK) {
16273 drm_modeset_backoff(&ctx);
16274 goto retry;
16275 }
043e9bda 16276
e2c8b870
ML
16277 drm_modeset_drop_locks(&ctx);
16278 drm_modeset_acquire_fini(&ctx);
ea49c9ac 16279 mutex_unlock(&dev->mode_config.mutex);
043e9bda 16280
e2c8b870
ML
16281 if (ret) {
16282 DRM_ERROR("Restoring old state failed with %i\n", ret);
16283 drm_atomic_state_free(state);
16284 }
2c7111db
CW
16285}
16286
16287void intel_modeset_gem_init(struct drm_device *dev)
16288{
dc97997a 16289 struct drm_i915_private *dev_priv = to_i915(dev);
484b41dd 16290 struct drm_crtc *c;
2ff8fde1 16291 struct drm_i915_gem_object *obj;
e0d6149b 16292 int ret;
484b41dd 16293
dc97997a 16294 intel_init_gt_powersave(dev_priv);
ae48434c 16295
1833b134 16296 intel_modeset_init_hw(dev);
02e792fb 16297
1ee8da6d 16298 intel_setup_overlay(dev_priv);
484b41dd
JB
16299
16300 /*
16301 * Make sure any fbs we allocated at startup are properly
16302 * pinned & fenced. When we do the allocation it's too early
16303 * for this.
16304 */
70e1e0ec 16305 for_each_crtc(dev, c) {
2ff8fde1
MR
16306 obj = intel_fb_obj(c->primary->fb);
16307 if (obj == NULL)
484b41dd
JB
16308 continue;
16309
e0d6149b 16310 mutex_lock(&dev->struct_mutex);
3465c580
VS
16311 ret = intel_pin_and_fence_fb_obj(c->primary->fb,
16312 c->primary->state->rotation);
e0d6149b
TU
16313 mutex_unlock(&dev->struct_mutex);
16314 if (ret) {
484b41dd
JB
16315 DRM_ERROR("failed to pin boot fb on pipe %d\n",
16316 to_intel_crtc(c)->pipe);
66e514c1 16317 drm_framebuffer_unreference(c->primary->fb);
5a21b665 16318 c->primary->fb = NULL;
36750f28 16319 c->primary->crtc = c->primary->state->crtc = NULL;
5a21b665 16320 update_state_fb(c->primary);
36750f28 16321 c->state->plane_mask &= ~(1 << drm_plane_index(c->primary));
484b41dd
JB
16322 }
16323 }
1ebaa0b9
CW
16324}
16325
16326int intel_connector_register(struct drm_connector *connector)
16327{
16328 struct intel_connector *intel_connector = to_intel_connector(connector);
16329 int ret;
16330
16331 ret = intel_backlight_device_register(intel_connector);
16332 if (ret)
16333 goto err;
16334
16335 return 0;
0962c3c9 16336
1ebaa0b9
CW
16337err:
16338 return ret;
79e53945
JB
16339}
16340
c191eca1 16341void intel_connector_unregister(struct drm_connector *connector)
4932e2c3 16342{
e63d87c0 16343 struct intel_connector *intel_connector = to_intel_connector(connector);
4932e2c3 16344
e63d87c0 16345 intel_backlight_device_unregister(intel_connector);
4932e2c3 16346 intel_panel_destroy_backlight(connector);
4932e2c3
ID
16347}
16348
79e53945
JB
16349void intel_modeset_cleanup(struct drm_device *dev)
16350{
652c393a 16351 struct drm_i915_private *dev_priv = dev->dev_private;
652c393a 16352
dc97997a 16353 intel_disable_gt_powersave(dev_priv);
2eb5252e 16354
fd0c0642
DV
16355 /*
16356 * Interrupts and polling as the first thing to avoid creating havoc.
2eb5252e 16357 * Too much stuff here (turning of connectors, ...) would
fd0c0642
DV
16358 * experience fancy races otherwise.
16359 */
2aeb7d3a 16360 intel_irq_uninstall(dev_priv);
eb21b92b 16361
fd0c0642
DV
16362 /*
16363 * Due to the hpd irq storm handling the hotplug work can re-arm the
16364 * poll handlers. Hence disable polling after hpd handling is shut down.
16365 */
f87ea761 16366 drm_kms_helper_poll_fini(dev);
fd0c0642 16367
723bfd70
JB
16368 intel_unregister_dsm_handler();
16369
c937ab3e 16370 intel_fbc_global_disable(dev_priv);
69341a5e 16371
1630fe75
CW
16372 /* flush any delayed tasks or pending work */
16373 flush_scheduled_work();
16374
79e53945 16375 drm_mode_config_cleanup(dev);
4d7bb011 16376
1ee8da6d 16377 intel_cleanup_overlay(dev_priv);
ae48434c 16378
dc97997a 16379 intel_cleanup_gt_powersave(dev_priv);
f5949141
DV
16380
16381 intel_teardown_gmbus(dev);
79e53945
JB
16382}
16383
df0e9248
CW
16384void intel_connector_attach_encoder(struct intel_connector *connector,
16385 struct intel_encoder *encoder)
16386{
16387 connector->encoder = encoder;
16388 drm_mode_connector_attach_encoder(&connector->base,
16389 &encoder->base);
79e53945 16390}
28d52043
DA
16391
16392/*
16393 * set vga decode state - true == enable VGA decode
16394 */
16395int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
16396{
16397 struct drm_i915_private *dev_priv = dev->dev_private;
a885b3cc 16398 unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
28d52043
DA
16399 u16 gmch_ctrl;
16400
75fa041d
CW
16401 if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
16402 DRM_ERROR("failed to read control word\n");
16403 return -EIO;
16404 }
16405
c0cc8a55
CW
16406 if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
16407 return 0;
16408
28d52043
DA
16409 if (state)
16410 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
16411 else
16412 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
75fa041d
CW
16413
16414 if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
16415 DRM_ERROR("failed to write control word\n");
16416 return -EIO;
16417 }
16418
28d52043
DA
16419 return 0;
16420}
c4a1d9e4 16421
c4a1d9e4 16422struct intel_display_error_state {
ff57f1b0
PZ
16423
16424 u32 power_well_driver;
16425
63b66e5b
CW
16426 int num_transcoders;
16427
c4a1d9e4
CW
16428 struct intel_cursor_error_state {
16429 u32 control;
16430 u32 position;
16431 u32 base;
16432 u32 size;
52331309 16433 } cursor[I915_MAX_PIPES];
c4a1d9e4
CW
16434
16435 struct intel_pipe_error_state {
ddf9c536 16436 bool power_domain_on;
c4a1d9e4 16437 u32 source;
f301b1e1 16438 u32 stat;
52331309 16439 } pipe[I915_MAX_PIPES];
c4a1d9e4
CW
16440
16441 struct intel_plane_error_state {
16442 u32 control;
16443 u32 stride;
16444 u32 size;
16445 u32 pos;
16446 u32 addr;
16447 u32 surface;
16448 u32 tile_offset;
52331309 16449 } plane[I915_MAX_PIPES];
63b66e5b
CW
16450
16451 struct intel_transcoder_error_state {
ddf9c536 16452 bool power_domain_on;
63b66e5b
CW
16453 enum transcoder cpu_transcoder;
16454
16455 u32 conf;
16456
16457 u32 htotal;
16458 u32 hblank;
16459 u32 hsync;
16460 u32 vtotal;
16461 u32 vblank;
16462 u32 vsync;
16463 } transcoder[4];
c4a1d9e4
CW
16464};
16465
16466struct intel_display_error_state *
c033666a 16467intel_display_capture_error_state(struct drm_i915_private *dev_priv)
c4a1d9e4 16468{
c4a1d9e4 16469 struct intel_display_error_state *error;
63b66e5b
CW
16470 int transcoders[] = {
16471 TRANSCODER_A,
16472 TRANSCODER_B,
16473 TRANSCODER_C,
16474 TRANSCODER_EDP,
16475 };
c4a1d9e4
CW
16476 int i;
16477
c033666a 16478 if (INTEL_INFO(dev_priv)->num_pipes == 0)
63b66e5b
CW
16479 return NULL;
16480
9d1cb914 16481 error = kzalloc(sizeof(*error), GFP_ATOMIC);
c4a1d9e4
CW
16482 if (error == NULL)
16483 return NULL;
16484
c033666a 16485 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
ff57f1b0
PZ
16486 error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
16487
055e393f 16488 for_each_pipe(dev_priv, i) {
ddf9c536 16489 error->pipe[i].power_domain_on =
f458ebbc
DV
16490 __intel_display_power_is_enabled(dev_priv,
16491 POWER_DOMAIN_PIPE(i));
ddf9c536 16492 if (!error->pipe[i].power_domain_on)
9d1cb914
PZ
16493 continue;
16494
5efb3e28
VS
16495 error->cursor[i].control = I915_READ(CURCNTR(i));
16496 error->cursor[i].position = I915_READ(CURPOS(i));
16497 error->cursor[i].base = I915_READ(CURBASE(i));
c4a1d9e4
CW
16498
16499 error->plane[i].control = I915_READ(DSPCNTR(i));
16500 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
c033666a 16501 if (INTEL_GEN(dev_priv) <= 3) {
51889b35 16502 error->plane[i].size = I915_READ(DSPSIZE(i));
80ca378b
PZ
16503 error->plane[i].pos = I915_READ(DSPPOS(i));
16504 }
c033666a 16505 if (INTEL_GEN(dev_priv) <= 7 && !IS_HASWELL(dev_priv))
ca291363 16506 error->plane[i].addr = I915_READ(DSPADDR(i));
c033666a 16507 if (INTEL_GEN(dev_priv) >= 4) {
c4a1d9e4
CW
16508 error->plane[i].surface = I915_READ(DSPSURF(i));
16509 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
16510 }
16511
c4a1d9e4 16512 error->pipe[i].source = I915_READ(PIPESRC(i));
f301b1e1 16513
c033666a 16514 if (HAS_GMCH_DISPLAY(dev_priv))
f301b1e1 16515 error->pipe[i].stat = I915_READ(PIPESTAT(i));
63b66e5b
CW
16516 }
16517
4d1de975 16518 /* Note: this does not include DSI transcoders. */
c033666a 16519 error->num_transcoders = INTEL_INFO(dev_priv)->num_pipes;
2d1fe073 16520 if (HAS_DDI(dev_priv))
63b66e5b
CW
16521 error->num_transcoders++; /* Account for eDP. */
16522
16523 for (i = 0; i < error->num_transcoders; i++) {
16524 enum transcoder cpu_transcoder = transcoders[i];
16525
ddf9c536 16526 error->transcoder[i].power_domain_on =
f458ebbc 16527 __intel_display_power_is_enabled(dev_priv,
38cc1daf 16528 POWER_DOMAIN_TRANSCODER(cpu_transcoder));
ddf9c536 16529 if (!error->transcoder[i].power_domain_on)
9d1cb914
PZ
16530 continue;
16531
63b66e5b
CW
16532 error->transcoder[i].cpu_transcoder = cpu_transcoder;
16533
16534 error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
16535 error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
16536 error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
16537 error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
16538 error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
16539 error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
16540 error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
c4a1d9e4
CW
16541 }
16542
16543 return error;
16544}
16545
edc3d884
MK
16546#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
16547
c4a1d9e4 16548void
edc3d884 16549intel_display_print_error_state(struct drm_i915_error_state_buf *m,
c4a1d9e4
CW
16550 struct drm_device *dev,
16551 struct intel_display_error_state *error)
16552{
055e393f 16553 struct drm_i915_private *dev_priv = dev->dev_private;
c4a1d9e4
CW
16554 int i;
16555
63b66e5b
CW
16556 if (!error)
16557 return;
16558
edc3d884 16559 err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
190be112 16560 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
edc3d884 16561 err_printf(m, "PWR_WELL_CTL2: %08x\n",
ff57f1b0 16562 error->power_well_driver);
055e393f 16563 for_each_pipe(dev_priv, i) {
edc3d884 16564 err_printf(m, "Pipe [%d]:\n", i);
ddf9c536 16565 err_printf(m, " Power: %s\n",
87ad3212 16566 onoff(error->pipe[i].power_domain_on));
edc3d884 16567 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
f301b1e1 16568 err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
edc3d884
MK
16569
16570 err_printf(m, "Plane [%d]:\n", i);
16571 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
16572 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
80ca378b 16573 if (INTEL_INFO(dev)->gen <= 3) {
edc3d884
MK
16574 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
16575 err_printf(m, " POS: %08x\n", error->plane[i].pos);
80ca378b 16576 }
4b71a570 16577 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
edc3d884 16578 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
c4a1d9e4 16579 if (INTEL_INFO(dev)->gen >= 4) {
edc3d884
MK
16580 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
16581 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
c4a1d9e4
CW
16582 }
16583
edc3d884
MK
16584 err_printf(m, "Cursor [%d]:\n", i);
16585 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
16586 err_printf(m, " POS: %08x\n", error->cursor[i].position);
16587 err_printf(m, " BASE: %08x\n", error->cursor[i].base);
c4a1d9e4 16588 }
63b66e5b
CW
16589
16590 for (i = 0; i < error->num_transcoders; i++) {
da205630 16591 err_printf(m, "CPU transcoder: %s\n",
63b66e5b 16592 transcoder_name(error->transcoder[i].cpu_transcoder));
ddf9c536 16593 err_printf(m, " Power: %s\n",
87ad3212 16594 onoff(error->transcoder[i].power_domain_on));
63b66e5b
CW
16595 err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
16596 err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
16597 err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
16598 err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
16599 err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
16600 err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
16601 err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
16602 }
c4a1d9e4 16603}