]> 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 1912 I915_WRITE(LPT_TRANSCONF, val);
d9f96244
CW
1913 if (intel_wait_for_register(dev_priv,
1914 LPT_TRANSCONF,
1915 TRANS_STATE_ENABLE,
1916 TRANS_STATE_ENABLE,
1917 100))
937bb610 1918 DRM_ERROR("Failed to enable PCH transcoder\n");
8fb033d7
PZ
1919}
1920
b8a4f404
PZ
1921static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
1922 enum pipe pipe)
040484af 1923{
23670b32 1924 struct drm_device *dev = dev_priv->dev;
f0f59a00
VS
1925 i915_reg_t reg;
1926 uint32_t val;
040484af
JB
1927
1928 /* FDI relies on the transcoder */
1929 assert_fdi_tx_disabled(dev_priv, pipe);
1930 assert_fdi_rx_disabled(dev_priv, pipe);
1931
291906f1
JB
1932 /* Ports must be off as well */
1933 assert_pch_ports_disabled(dev_priv, pipe);
1934
ab9412ba 1935 reg = PCH_TRANSCONF(pipe);
040484af
JB
1936 val = I915_READ(reg);
1937 val &= ~TRANS_ENABLE;
1938 I915_WRITE(reg, val);
1939 /* wait for PCH transcoder off, transcoder state */
a7d04662
CW
1940 if (intel_wait_for_register(dev_priv,
1941 reg, TRANS_STATE_ENABLE, 0,
1942 50))
4bb6f1f3 1943 DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
23670b32 1944
c465613b 1945 if (HAS_PCH_CPT(dev)) {
23670b32
DV
1946 /* Workaround: Clear the timing override chicken bit again. */
1947 reg = TRANS_CHICKEN2(pipe);
1948 val = I915_READ(reg);
1949 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
1950 I915_WRITE(reg, val);
1951 }
040484af
JB
1952}
1953
ab4d966c 1954static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
8fb033d7 1955{
8fb033d7
PZ
1956 u32 val;
1957
ab9412ba 1958 val = I915_READ(LPT_TRANSCONF);
8fb033d7 1959 val &= ~TRANS_ENABLE;
ab9412ba 1960 I915_WRITE(LPT_TRANSCONF, val);
8fb033d7 1961 /* wait for PCH transcoder off, transcoder state */
dfdb4749
CW
1962 if (intel_wait_for_register(dev_priv,
1963 LPT_TRANSCONF, TRANS_STATE_ENABLE, 0,
1964 50))
8a52fd9f 1965 DRM_ERROR("Failed to disable PCH transcoder\n");
223a6fdf
PZ
1966
1967 /* Workaround: clear timing override bit. */
36c0d0cf 1968 val = I915_READ(TRANS_CHICKEN2(PIPE_A));
23670b32 1969 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
36c0d0cf 1970 I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
040484af
JB
1971}
1972
b24e7179 1973/**
309cfea8 1974 * intel_enable_pipe - enable a pipe, asserting requirements
0372264a 1975 * @crtc: crtc responsible for the pipe
b24e7179 1976 *
0372264a 1977 * Enable @crtc's pipe, making sure that various hardware specific requirements
b24e7179 1978 * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
b24e7179 1979 */
e1fdc473 1980static void intel_enable_pipe(struct intel_crtc *crtc)
b24e7179 1981{
0372264a
PZ
1982 struct drm_device *dev = crtc->base.dev;
1983 struct drm_i915_private *dev_priv = dev->dev_private;
1984 enum pipe pipe = crtc->pipe;
1a70a728 1985 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
1a240d4d 1986 enum pipe pch_transcoder;
f0f59a00 1987 i915_reg_t reg;
b24e7179
JB
1988 u32 val;
1989
9e2ee2dd
VS
1990 DRM_DEBUG_KMS("enabling pipe %c\n", pipe_name(pipe));
1991
58c6eaa2 1992 assert_planes_disabled(dev_priv, pipe);
93ce0ba6 1993 assert_cursor_disabled(dev_priv, pipe);
58c6eaa2
DV
1994 assert_sprites_disabled(dev_priv, pipe);
1995
2d1fe073 1996 if (HAS_PCH_LPT(dev_priv))
cc391bbb
PZ
1997 pch_transcoder = TRANSCODER_A;
1998 else
1999 pch_transcoder = pipe;
2000
b24e7179
JB
2001 /*
2002 * A pipe without a PLL won't actually be able to drive bits from
2003 * a plane. On ILK+ the pipe PLLs are integrated, so we don't
2004 * need the check.
2005 */
2d1fe073 2006 if (HAS_GMCH_DISPLAY(dev_priv))
a65347ba 2007 if (crtc->config->has_dsi_encoder)
23538ef1
JN
2008 assert_dsi_pll_enabled(dev_priv);
2009 else
2010 assert_pll_enabled(dev_priv, pipe);
040484af 2011 else {
6e3c9717 2012 if (crtc->config->has_pch_encoder) {
040484af 2013 /* if driving the PCH, we need FDI enabled */
cc391bbb 2014 assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
1a240d4d
DV
2015 assert_fdi_tx_pll_enabled(dev_priv,
2016 (enum pipe) cpu_transcoder);
040484af
JB
2017 }
2018 /* FIXME: assert CPU port conditions for SNB+ */
2019 }
b24e7179 2020
702e7a56 2021 reg = PIPECONF(cpu_transcoder);
b24e7179 2022 val = I915_READ(reg);
7ad25d48 2023 if (val & PIPECONF_ENABLE) {
b6b5d049
VS
2024 WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
2025 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
00d70b15 2026 return;
7ad25d48 2027 }
00d70b15
CW
2028
2029 I915_WRITE(reg, val | PIPECONF_ENABLE);
851855d8 2030 POSTING_READ(reg);
b7792d8b
VS
2031
2032 /*
2033 * Until the pipe starts DSL will read as 0, which would cause
2034 * an apparent vblank timestamp jump, which messes up also the
2035 * frame count when it's derived from the timestamps. So let's
2036 * wait for the pipe to start properly before we call
2037 * drm_crtc_vblank_on()
2038 */
2039 if (dev->max_vblank_count == 0 &&
2040 wait_for(intel_get_crtc_scanline(crtc) != crtc->scanline_offset, 50))
2041 DRM_ERROR("pipe %c didn't start\n", pipe_name(pipe));
b24e7179
JB
2042}
2043
2044/**
309cfea8 2045 * intel_disable_pipe - disable a pipe, asserting requirements
575f7ab7 2046 * @crtc: crtc whose pipes is to be disabled
b24e7179 2047 *
575f7ab7
VS
2048 * Disable the pipe of @crtc, making sure that various hardware
2049 * specific requirements are met, if applicable, e.g. plane
2050 * disabled, panel fitter off, etc.
b24e7179
JB
2051 *
2052 * Will wait until the pipe has shut down before returning.
2053 */
575f7ab7 2054static void intel_disable_pipe(struct intel_crtc *crtc)
b24e7179 2055{
575f7ab7 2056 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
6e3c9717 2057 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
575f7ab7 2058 enum pipe pipe = crtc->pipe;
f0f59a00 2059 i915_reg_t reg;
b24e7179
JB
2060 u32 val;
2061
9e2ee2dd
VS
2062 DRM_DEBUG_KMS("disabling pipe %c\n", pipe_name(pipe));
2063
b24e7179
JB
2064 /*
2065 * Make sure planes won't keep trying to pump pixels to us,
2066 * or we might hang the display.
2067 */
2068 assert_planes_disabled(dev_priv, pipe);
93ce0ba6 2069 assert_cursor_disabled(dev_priv, pipe);
19332d7a 2070 assert_sprites_disabled(dev_priv, pipe);
b24e7179 2071
702e7a56 2072 reg = PIPECONF(cpu_transcoder);
b24e7179 2073 val = I915_READ(reg);
00d70b15
CW
2074 if ((val & PIPECONF_ENABLE) == 0)
2075 return;
2076
67adc644
VS
2077 /*
2078 * Double wide has implications for planes
2079 * so best keep it disabled when not needed.
2080 */
6e3c9717 2081 if (crtc->config->double_wide)
67adc644
VS
2082 val &= ~PIPECONF_DOUBLE_WIDE;
2083
2084 /* Don't disable pipe or pipe PLLs if needed */
b6b5d049
VS
2085 if (!(pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) &&
2086 !(pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
67adc644
VS
2087 val &= ~PIPECONF_ENABLE;
2088
2089 I915_WRITE(reg, val);
2090 if ((val & PIPECONF_ENABLE) == 0)
2091 intel_wait_for_pipe_off(crtc);
b24e7179
JB
2092}
2093
693db184
CW
2094static bool need_vtd_wa(struct drm_device *dev)
2095{
2096#ifdef CONFIG_INTEL_IOMMU
2097 if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
2098 return true;
2099#endif
2100 return false;
2101}
2102
832be82f
VS
2103static unsigned int intel_tile_size(const struct drm_i915_private *dev_priv)
2104{
2105 return IS_GEN2(dev_priv) ? 2048 : 4096;
2106}
2107
27ba3910
VS
2108static unsigned int intel_tile_width_bytes(const struct drm_i915_private *dev_priv,
2109 uint64_t fb_modifier, unsigned int cpp)
7b49f948
VS
2110{
2111 switch (fb_modifier) {
2112 case DRM_FORMAT_MOD_NONE:
2113 return cpp;
2114 case I915_FORMAT_MOD_X_TILED:
2115 if (IS_GEN2(dev_priv))
2116 return 128;
2117 else
2118 return 512;
2119 case I915_FORMAT_MOD_Y_TILED:
2120 if (IS_GEN2(dev_priv) || HAS_128_BYTE_Y_TILING(dev_priv))
2121 return 128;
2122 else
2123 return 512;
2124 case I915_FORMAT_MOD_Yf_TILED:
2125 switch (cpp) {
2126 case 1:
2127 return 64;
2128 case 2:
2129 case 4:
2130 return 128;
2131 case 8:
2132 case 16:
2133 return 256;
2134 default:
2135 MISSING_CASE(cpp);
2136 return cpp;
2137 }
2138 break;
2139 default:
2140 MISSING_CASE(fb_modifier);
2141 return cpp;
2142 }
2143}
2144
832be82f
VS
2145unsigned int intel_tile_height(const struct drm_i915_private *dev_priv,
2146 uint64_t fb_modifier, unsigned int cpp)
a57ce0b2 2147{
832be82f
VS
2148 if (fb_modifier == DRM_FORMAT_MOD_NONE)
2149 return 1;
2150 else
2151 return intel_tile_size(dev_priv) /
27ba3910 2152 intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
6761dd31
TU
2153}
2154
8d0deca8
VS
2155/* Return the tile dimensions in pixel units */
2156static void intel_tile_dims(const struct drm_i915_private *dev_priv,
2157 unsigned int *tile_width,
2158 unsigned int *tile_height,
2159 uint64_t fb_modifier,
2160 unsigned int cpp)
2161{
2162 unsigned int tile_width_bytes =
2163 intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
2164
2165 *tile_width = tile_width_bytes / cpp;
2166 *tile_height = intel_tile_size(dev_priv) / tile_width_bytes;
2167}
2168
6761dd31
TU
2169unsigned int
2170intel_fb_align_height(struct drm_device *dev, unsigned int height,
832be82f 2171 uint32_t pixel_format, uint64_t fb_modifier)
6761dd31 2172{
832be82f
VS
2173 unsigned int cpp = drm_format_plane_cpp(pixel_format, 0);
2174 unsigned int tile_height = intel_tile_height(to_i915(dev), fb_modifier, cpp);
2175
2176 return ALIGN(height, tile_height);
a57ce0b2
JB
2177}
2178
1663b9d6
VS
2179unsigned int intel_rotation_info_size(const struct intel_rotation_info *rot_info)
2180{
2181 unsigned int size = 0;
2182 int i;
2183
2184 for (i = 0 ; i < ARRAY_SIZE(rot_info->plane); i++)
2185 size += rot_info->plane[i].width * rot_info->plane[i].height;
2186
2187 return size;
2188}
2189
75c82a53 2190static void
3465c580
VS
2191intel_fill_fb_ggtt_view(struct i915_ggtt_view *view,
2192 const struct drm_framebuffer *fb,
2193 unsigned int rotation)
f64b98cd 2194{
2d7a215f
VS
2195 if (intel_rotation_90_or_270(rotation)) {
2196 *view = i915_ggtt_view_rotated;
2197 view->params.rotated = to_intel_framebuffer(fb)->rot_info;
2198 } else {
2199 *view = i915_ggtt_view_normal;
2200 }
2201}
50470bb0 2202
2d7a215f
VS
2203static void
2204intel_fill_fb_info(struct drm_i915_private *dev_priv,
2205 struct drm_framebuffer *fb)
2206{
2207 struct intel_rotation_info *info = &to_intel_framebuffer(fb)->rot_info;
2208 unsigned int tile_size, tile_width, tile_height, cpp;
50470bb0 2209
d9b3288e
VS
2210 tile_size = intel_tile_size(dev_priv);
2211
2212 cpp = drm_format_plane_cpp(fb->pixel_format, 0);
8d0deca8
VS
2213 intel_tile_dims(dev_priv, &tile_width, &tile_height,
2214 fb->modifier[0], cpp);
d9b3288e 2215
1663b9d6
VS
2216 info->plane[0].width = DIV_ROUND_UP(fb->pitches[0], tile_width * cpp);
2217 info->plane[0].height = DIV_ROUND_UP(fb->height, tile_height);
84fe03f7 2218
89e3e142 2219 if (info->pixel_format == DRM_FORMAT_NV12) {
832be82f 2220 cpp = drm_format_plane_cpp(fb->pixel_format, 1);
8d0deca8
VS
2221 intel_tile_dims(dev_priv, &tile_width, &tile_height,
2222 fb->modifier[1], cpp);
d9b3288e 2223
2d7a215f 2224 info->uv_offset = fb->offsets[1];
1663b9d6
VS
2225 info->plane[1].width = DIV_ROUND_UP(fb->pitches[1], tile_width * cpp);
2226 info->plane[1].height = DIV_ROUND_UP(fb->height / 2, tile_height);
89e3e142 2227 }
f64b98cd
TU
2228}
2229
603525d7 2230static unsigned int intel_linear_alignment(const struct drm_i915_private *dev_priv)
4e9a86b6
VS
2231{
2232 if (INTEL_INFO(dev_priv)->gen >= 9)
2233 return 256 * 1024;
985b8bb4 2234 else if (IS_BROADWATER(dev_priv) || IS_CRESTLINE(dev_priv) ||
666a4537 2235 IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
4e9a86b6
VS
2236 return 128 * 1024;
2237 else if (INTEL_INFO(dev_priv)->gen >= 4)
2238 return 4 * 1024;
2239 else
44c5905e 2240 return 0;
4e9a86b6
VS
2241}
2242
603525d7
VS
2243static unsigned int intel_surf_alignment(const struct drm_i915_private *dev_priv,
2244 uint64_t fb_modifier)
2245{
2246 switch (fb_modifier) {
2247 case DRM_FORMAT_MOD_NONE:
2248 return intel_linear_alignment(dev_priv);
2249 case I915_FORMAT_MOD_X_TILED:
2250 if (INTEL_INFO(dev_priv)->gen >= 9)
2251 return 256 * 1024;
2252 return 0;
2253 case I915_FORMAT_MOD_Y_TILED:
2254 case I915_FORMAT_MOD_Yf_TILED:
2255 return 1 * 1024 * 1024;
2256 default:
2257 MISSING_CASE(fb_modifier);
2258 return 0;
2259 }
2260}
2261
127bd2ac 2262int
3465c580
VS
2263intel_pin_and_fence_fb_obj(struct drm_framebuffer *fb,
2264 unsigned int rotation)
6b95a207 2265{
850c4cdc 2266 struct drm_device *dev = fb->dev;
ce453d81 2267 struct drm_i915_private *dev_priv = dev->dev_private;
850c4cdc 2268 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
f64b98cd 2269 struct i915_ggtt_view view;
6b95a207
KH
2270 u32 alignment;
2271 int ret;
2272
ebcdd39e
MR
2273 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
2274
603525d7 2275 alignment = intel_surf_alignment(dev_priv, fb->modifier[0]);
6b95a207 2276
3465c580 2277 intel_fill_fb_ggtt_view(&view, fb, rotation);
f64b98cd 2278
693db184
CW
2279 /* Note that the w/a also requires 64 PTE of padding following the
2280 * bo. We currently fill all unused PTE with the shadow page and so
2281 * we should always have valid PTE following the scanout preventing
2282 * the VT-d warning.
2283 */
2284 if (need_vtd_wa(dev) && alignment < 256 * 1024)
2285 alignment = 256 * 1024;
2286
d6dd6843
PZ
2287 /*
2288 * Global gtt pte registers are special registers which actually forward
2289 * writes to a chunk of system memory. Which means that there is no risk
2290 * that the register values disappear as soon as we call
2291 * intel_runtime_pm_put(), so it is correct to wrap only the
2292 * pin/unpin/fence and not more.
2293 */
2294 intel_runtime_pm_get(dev_priv);
2295
7580d774
ML
2296 ret = i915_gem_object_pin_to_display_plane(obj, alignment,
2297 &view);
48b956c5 2298 if (ret)
b26a6b35 2299 goto err_pm;
6b95a207
KH
2300
2301 /* Install a fence for tiled scan-out. Pre-i965 always needs a
2302 * fence, whereas 965+ only requires a fence if using
2303 * framebuffer compression. For simplicity, we always install
2304 * a fence as the cost is not that onerous.
2305 */
9807216f
VK
2306 if (view.type == I915_GGTT_VIEW_NORMAL) {
2307 ret = i915_gem_object_get_fence(obj);
2308 if (ret == -EDEADLK) {
2309 /*
2310 * -EDEADLK means there are no free fences
2311 * no pending flips.
2312 *
2313 * This is propagated to atomic, but it uses
2314 * -EDEADLK to force a locking recovery, so
2315 * change the returned error to -EBUSY.
2316 */
2317 ret = -EBUSY;
2318 goto err_unpin;
2319 } else if (ret)
2320 goto err_unpin;
1690e1eb 2321
9807216f
VK
2322 i915_gem_object_pin_fence(obj);
2323 }
6b95a207 2324
d6dd6843 2325 intel_runtime_pm_put(dev_priv);
6b95a207 2326 return 0;
48b956c5
CW
2327
2328err_unpin:
f64b98cd 2329 i915_gem_object_unpin_from_display_plane(obj, &view);
b26a6b35 2330err_pm:
d6dd6843 2331 intel_runtime_pm_put(dev_priv);
48b956c5 2332 return ret;
6b95a207
KH
2333}
2334
fb4b8ce1 2335void intel_unpin_fb_obj(struct drm_framebuffer *fb, unsigned int rotation)
1690e1eb 2336{
82bc3b2d 2337 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
f64b98cd 2338 struct i915_ggtt_view view;
82bc3b2d 2339
ebcdd39e
MR
2340 WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
2341
3465c580 2342 intel_fill_fb_ggtt_view(&view, fb, rotation);
f64b98cd 2343
9807216f
VK
2344 if (view.type == I915_GGTT_VIEW_NORMAL)
2345 i915_gem_object_unpin_fence(obj);
2346
f64b98cd 2347 i915_gem_object_unpin_from_display_plane(obj, &view);
1690e1eb
CW
2348}
2349
29cf9491
VS
2350/*
2351 * Adjust the tile offset by moving the difference into
2352 * the x/y offsets.
2353 *
2354 * Input tile dimensions and pitch must already be
2355 * rotated to match x and y, and in pixel units.
2356 */
2357static u32 intel_adjust_tile_offset(int *x, int *y,
2358 unsigned int tile_width,
2359 unsigned int tile_height,
2360 unsigned int tile_size,
2361 unsigned int pitch_tiles,
2362 u32 old_offset,
2363 u32 new_offset)
2364{
2365 unsigned int tiles;
2366
2367 WARN_ON(old_offset & (tile_size - 1));
2368 WARN_ON(new_offset & (tile_size - 1));
2369 WARN_ON(new_offset > old_offset);
2370
2371 tiles = (old_offset - new_offset) / tile_size;
2372
2373 *y += tiles / pitch_tiles * tile_height;
2374 *x += tiles % pitch_tiles * tile_width;
2375
2376 return new_offset;
2377}
2378
8d0deca8
VS
2379/*
2380 * Computes the linear offset to the base tile and adjusts
2381 * x, y. bytes per pixel is assumed to be a power-of-two.
2382 *
2383 * In the 90/270 rotated case, x and y are assumed
2384 * to be already rotated to match the rotated GTT view, and
2385 * pitch is the tile_height aligned framebuffer height.
2386 */
4f2d9934
VS
2387u32 intel_compute_tile_offset(int *x, int *y,
2388 const struct drm_framebuffer *fb, int plane,
8d0deca8
VS
2389 unsigned int pitch,
2390 unsigned int rotation)
c2c75131 2391{
4f2d9934
VS
2392 const struct drm_i915_private *dev_priv = to_i915(fb->dev);
2393 uint64_t fb_modifier = fb->modifier[plane];
2394 unsigned int cpp = drm_format_plane_cpp(fb->pixel_format, plane);
29cf9491
VS
2395 u32 offset, offset_aligned, alignment;
2396
2397 alignment = intel_surf_alignment(dev_priv, fb_modifier);
2398 if (alignment)
2399 alignment--;
2400
b5c65338 2401 if (fb_modifier != DRM_FORMAT_MOD_NONE) {
8d0deca8
VS
2402 unsigned int tile_size, tile_width, tile_height;
2403 unsigned int tile_rows, tiles, pitch_tiles;
c2c75131 2404
d843310d 2405 tile_size = intel_tile_size(dev_priv);
8d0deca8
VS
2406 intel_tile_dims(dev_priv, &tile_width, &tile_height,
2407 fb_modifier, cpp);
2408
2409 if (intel_rotation_90_or_270(rotation)) {
2410 pitch_tiles = pitch / tile_height;
2411 swap(tile_width, tile_height);
2412 } else {
2413 pitch_tiles = pitch / (tile_width * cpp);
2414 }
d843310d
VS
2415
2416 tile_rows = *y / tile_height;
2417 *y %= tile_height;
c2c75131 2418
8d0deca8
VS
2419 tiles = *x / tile_width;
2420 *x %= tile_width;
bc752862 2421
29cf9491
VS
2422 offset = (tile_rows * pitch_tiles + tiles) * tile_size;
2423 offset_aligned = offset & ~alignment;
bc752862 2424
29cf9491
VS
2425 intel_adjust_tile_offset(x, y, tile_width, tile_height,
2426 tile_size, pitch_tiles,
2427 offset, offset_aligned);
2428 } else {
bc752862 2429 offset = *y * pitch + *x * cpp;
29cf9491
VS
2430 offset_aligned = offset & ~alignment;
2431
4e9a86b6
VS
2432 *y = (offset & alignment) / pitch;
2433 *x = ((offset & alignment) - *y * pitch) / cpp;
bc752862 2434 }
29cf9491
VS
2435
2436 return offset_aligned;
c2c75131
DV
2437}
2438
b35d63fa 2439static int i9xx_format_to_fourcc(int format)
46f297fb
JB
2440{
2441 switch (format) {
2442 case DISPPLANE_8BPP:
2443 return DRM_FORMAT_C8;
2444 case DISPPLANE_BGRX555:
2445 return DRM_FORMAT_XRGB1555;
2446 case DISPPLANE_BGRX565:
2447 return DRM_FORMAT_RGB565;
2448 default:
2449 case DISPPLANE_BGRX888:
2450 return DRM_FORMAT_XRGB8888;
2451 case DISPPLANE_RGBX888:
2452 return DRM_FORMAT_XBGR8888;
2453 case DISPPLANE_BGRX101010:
2454 return DRM_FORMAT_XRGB2101010;
2455 case DISPPLANE_RGBX101010:
2456 return DRM_FORMAT_XBGR2101010;
2457 }
2458}
2459
bc8d7dff
DL
2460static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
2461{
2462 switch (format) {
2463 case PLANE_CTL_FORMAT_RGB_565:
2464 return DRM_FORMAT_RGB565;
2465 default:
2466 case PLANE_CTL_FORMAT_XRGB_8888:
2467 if (rgb_order) {
2468 if (alpha)
2469 return DRM_FORMAT_ABGR8888;
2470 else
2471 return DRM_FORMAT_XBGR8888;
2472 } else {
2473 if (alpha)
2474 return DRM_FORMAT_ARGB8888;
2475 else
2476 return DRM_FORMAT_XRGB8888;
2477 }
2478 case PLANE_CTL_FORMAT_XRGB_2101010:
2479 if (rgb_order)
2480 return DRM_FORMAT_XBGR2101010;
2481 else
2482 return DRM_FORMAT_XRGB2101010;
2483 }
2484}
2485
5724dbd1 2486static bool
f6936e29
DV
2487intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
2488 struct intel_initial_plane_config *plane_config)
46f297fb
JB
2489{
2490 struct drm_device *dev = crtc->base.dev;
3badb49f 2491 struct drm_i915_private *dev_priv = to_i915(dev);
72e96d64 2492 struct i915_ggtt *ggtt = &dev_priv->ggtt;
46f297fb
JB
2493 struct drm_i915_gem_object *obj = NULL;
2494 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
2d14030b 2495 struct drm_framebuffer *fb = &plane_config->fb->base;
f37b5c2b
DV
2496 u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
2497 u32 size_aligned = round_up(plane_config->base + plane_config->size,
2498 PAGE_SIZE);
2499
2500 size_aligned -= base_aligned;
46f297fb 2501
ff2652ea
CW
2502 if (plane_config->size == 0)
2503 return false;
2504
3badb49f
PZ
2505 /* If the FB is too big, just don't use it since fbdev is not very
2506 * important and we should probably use that space with FBC or other
2507 * features. */
72e96d64 2508 if (size_aligned * 2 > ggtt->stolen_usable_size)
3badb49f
PZ
2509 return false;
2510
12c83d99
TU
2511 mutex_lock(&dev->struct_mutex);
2512
f37b5c2b
DV
2513 obj = i915_gem_object_create_stolen_for_preallocated(dev,
2514 base_aligned,
2515 base_aligned,
2516 size_aligned);
12c83d99
TU
2517 if (!obj) {
2518 mutex_unlock(&dev->struct_mutex);
484b41dd 2519 return false;
12c83d99 2520 }
46f297fb 2521
49af449b
DL
2522 obj->tiling_mode = plane_config->tiling;
2523 if (obj->tiling_mode == I915_TILING_X)
6bf129df 2524 obj->stride = fb->pitches[0];
46f297fb 2525
6bf129df
DL
2526 mode_cmd.pixel_format = fb->pixel_format;
2527 mode_cmd.width = fb->width;
2528 mode_cmd.height = fb->height;
2529 mode_cmd.pitches[0] = fb->pitches[0];
18c5247e
DV
2530 mode_cmd.modifier[0] = fb->modifier[0];
2531 mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
46f297fb 2532
6bf129df 2533 if (intel_framebuffer_init(dev, to_intel_framebuffer(fb),
484b41dd 2534 &mode_cmd, obj)) {
46f297fb
JB
2535 DRM_DEBUG_KMS("intel fb init failed\n");
2536 goto out_unref_obj;
2537 }
12c83d99 2538
46f297fb 2539 mutex_unlock(&dev->struct_mutex);
484b41dd 2540
f6936e29 2541 DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
484b41dd 2542 return true;
46f297fb
JB
2543
2544out_unref_obj:
2545 drm_gem_object_unreference(&obj->base);
2546 mutex_unlock(&dev->struct_mutex);
484b41dd
JB
2547 return false;
2548}
2549
5a21b665
DV
2550/* Update plane->state->fb to match plane->fb after driver-internal updates */
2551static void
2552update_state_fb(struct drm_plane *plane)
2553{
2554 if (plane->fb == plane->state->fb)
2555 return;
2556
2557 if (plane->state->fb)
2558 drm_framebuffer_unreference(plane->state->fb);
2559 plane->state->fb = plane->fb;
2560 if (plane->state->fb)
2561 drm_framebuffer_reference(plane->state->fb);
2562}
2563
5724dbd1 2564static void
f6936e29
DV
2565intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
2566 struct intel_initial_plane_config *plane_config)
484b41dd
JB
2567{
2568 struct drm_device *dev = intel_crtc->base.dev;
d9ceb816 2569 struct drm_i915_private *dev_priv = dev->dev_private;
484b41dd
JB
2570 struct drm_crtc *c;
2571 struct intel_crtc *i;
2ff8fde1 2572 struct drm_i915_gem_object *obj;
88595ac9 2573 struct drm_plane *primary = intel_crtc->base.primary;
be5651f2 2574 struct drm_plane_state *plane_state = primary->state;
200757f5
MR
2575 struct drm_crtc_state *crtc_state = intel_crtc->base.state;
2576 struct intel_plane *intel_plane = to_intel_plane(primary);
0a8d8a86
MR
2577 struct intel_plane_state *intel_state =
2578 to_intel_plane_state(plane_state);
88595ac9 2579 struct drm_framebuffer *fb;
484b41dd 2580
2d14030b 2581 if (!plane_config->fb)
484b41dd
JB
2582 return;
2583
f6936e29 2584 if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
88595ac9
DV
2585 fb = &plane_config->fb->base;
2586 goto valid_fb;
f55548b5 2587 }
484b41dd 2588
2d14030b 2589 kfree(plane_config->fb);
484b41dd
JB
2590
2591 /*
2592 * Failed to alloc the obj, check to see if we should share
2593 * an fb with another CRTC instead
2594 */
70e1e0ec 2595 for_each_crtc(dev, c) {
484b41dd
JB
2596 i = to_intel_crtc(c);
2597
2598 if (c == &intel_crtc->base)
2599 continue;
2600
2ff8fde1
MR
2601 if (!i->active)
2602 continue;
2603
88595ac9
DV
2604 fb = c->primary->fb;
2605 if (!fb)
484b41dd
JB
2606 continue;
2607
88595ac9 2608 obj = intel_fb_obj(fb);
2ff8fde1 2609 if (i915_gem_obj_ggtt_offset(obj) == plane_config->base) {
88595ac9
DV
2610 drm_framebuffer_reference(fb);
2611 goto valid_fb;
484b41dd
JB
2612 }
2613 }
88595ac9 2614
200757f5
MR
2615 /*
2616 * We've failed to reconstruct the BIOS FB. Current display state
2617 * indicates that the primary plane is visible, but has a NULL FB,
2618 * which will lead to problems later if we don't fix it up. The
2619 * simplest solution is to just disable the primary plane now and
2620 * pretend the BIOS never had it enabled.
2621 */
2622 to_intel_plane_state(plane_state)->visible = false;
2623 crtc_state->plane_mask &= ~(1 << drm_plane_index(primary));
2622a081 2624 intel_pre_disable_primary_noatomic(&intel_crtc->base);
200757f5
MR
2625 intel_plane->disable_plane(primary, &intel_crtc->base);
2626
88595ac9
DV
2627 return;
2628
2629valid_fb:
f44e2659
VS
2630 plane_state->src_x = 0;
2631 plane_state->src_y = 0;
be5651f2
ML
2632 plane_state->src_w = fb->width << 16;
2633 plane_state->src_h = fb->height << 16;
2634
f44e2659
VS
2635 plane_state->crtc_x = 0;
2636 plane_state->crtc_y = 0;
be5651f2
ML
2637 plane_state->crtc_w = fb->width;
2638 plane_state->crtc_h = fb->height;
2639
0a8d8a86
MR
2640 intel_state->src.x1 = plane_state->src_x;
2641 intel_state->src.y1 = plane_state->src_y;
2642 intel_state->src.x2 = plane_state->src_x + plane_state->src_w;
2643 intel_state->src.y2 = plane_state->src_y + plane_state->src_h;
2644 intel_state->dst.x1 = plane_state->crtc_x;
2645 intel_state->dst.y1 = plane_state->crtc_y;
2646 intel_state->dst.x2 = plane_state->crtc_x + plane_state->crtc_w;
2647 intel_state->dst.y2 = plane_state->crtc_y + plane_state->crtc_h;
2648
88595ac9
DV
2649 obj = intel_fb_obj(fb);
2650 if (obj->tiling_mode != I915_TILING_NONE)
2651 dev_priv->preserve_bios_swizzle = true;
2652
be5651f2
ML
2653 drm_framebuffer_reference(fb);
2654 primary->fb = primary->state->fb = fb;
36750f28 2655 primary->crtc = primary->state->crtc = &intel_crtc->base;
36750f28 2656 intel_crtc->base.state->plane_mask |= (1 << drm_plane_index(primary));
a9ff8714 2657 obj->frontbuffer_bits |= to_intel_plane(primary)->frontbuffer_bit;
46f297fb
JB
2658}
2659
a8d201af
ML
2660static void i9xx_update_primary_plane(struct drm_plane *primary,
2661 const struct intel_crtc_state *crtc_state,
2662 const struct intel_plane_state *plane_state)
81255565 2663{
a8d201af 2664 struct drm_device *dev = primary->dev;
81255565 2665 struct drm_i915_private *dev_priv = dev->dev_private;
a8d201af
ML
2666 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
2667 struct drm_framebuffer *fb = plane_state->base.fb;
2668 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
81255565 2669 int plane = intel_crtc->plane;
54ea9da8 2670 u32 linear_offset;
81255565 2671 u32 dspcntr;
f0f59a00 2672 i915_reg_t reg = DSPCNTR(plane);
8d0deca8 2673 unsigned int rotation = plane_state->base.rotation;
ac484963 2674 int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
54ea9da8
VS
2675 int x = plane_state->src.x1 >> 16;
2676 int y = plane_state->src.y1 >> 16;
c9ba6fad 2677
f45651ba
VS
2678 dspcntr = DISPPLANE_GAMMA_ENABLE;
2679
fdd508a6 2680 dspcntr |= DISPLAY_PLANE_ENABLE;
f45651ba
VS
2681
2682 if (INTEL_INFO(dev)->gen < 4) {
2683 if (intel_crtc->pipe == PIPE_B)
2684 dspcntr |= DISPPLANE_SEL_PIPE_B;
2685
2686 /* pipesrc and dspsize control the size that is scaled from,
2687 * which should always be the user's requested size.
2688 */
2689 I915_WRITE(DSPSIZE(plane),
a8d201af
ML
2690 ((crtc_state->pipe_src_h - 1) << 16) |
2691 (crtc_state->pipe_src_w - 1));
f45651ba 2692 I915_WRITE(DSPPOS(plane), 0);
c14b0485
VS
2693 } else if (IS_CHERRYVIEW(dev) && plane == PLANE_B) {
2694 I915_WRITE(PRIMSIZE(plane),
a8d201af
ML
2695 ((crtc_state->pipe_src_h - 1) << 16) |
2696 (crtc_state->pipe_src_w - 1));
c14b0485
VS
2697 I915_WRITE(PRIMPOS(plane), 0);
2698 I915_WRITE(PRIMCNSTALPHA(plane), 0);
f45651ba 2699 }
81255565 2700
57779d06
VS
2701 switch (fb->pixel_format) {
2702 case DRM_FORMAT_C8:
81255565
JB
2703 dspcntr |= DISPPLANE_8BPP;
2704 break;
57779d06 2705 case DRM_FORMAT_XRGB1555:
57779d06 2706 dspcntr |= DISPPLANE_BGRX555;
81255565 2707 break;
57779d06
VS
2708 case DRM_FORMAT_RGB565:
2709 dspcntr |= DISPPLANE_BGRX565;
2710 break;
2711 case DRM_FORMAT_XRGB8888:
57779d06
VS
2712 dspcntr |= DISPPLANE_BGRX888;
2713 break;
2714 case DRM_FORMAT_XBGR8888:
57779d06
VS
2715 dspcntr |= DISPPLANE_RGBX888;
2716 break;
2717 case DRM_FORMAT_XRGB2101010:
57779d06
VS
2718 dspcntr |= DISPPLANE_BGRX101010;
2719 break;
2720 case DRM_FORMAT_XBGR2101010:
57779d06 2721 dspcntr |= DISPPLANE_RGBX101010;
81255565
JB
2722 break;
2723 default:
baba133a 2724 BUG();
81255565 2725 }
57779d06 2726
f45651ba
VS
2727 if (INTEL_INFO(dev)->gen >= 4 &&
2728 obj->tiling_mode != I915_TILING_NONE)
2729 dspcntr |= DISPPLANE_TILED;
81255565 2730
de1aa629
VS
2731 if (IS_G4X(dev))
2732 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
2733
ac484963 2734 linear_offset = y * fb->pitches[0] + x * cpp;
81255565 2735
c2c75131
DV
2736 if (INTEL_INFO(dev)->gen >= 4) {
2737 intel_crtc->dspaddr_offset =
4f2d9934 2738 intel_compute_tile_offset(&x, &y, fb, 0,
8d0deca8 2739 fb->pitches[0], rotation);
c2c75131
DV
2740 linear_offset -= intel_crtc->dspaddr_offset;
2741 } else {
e506a0c6 2742 intel_crtc->dspaddr_offset = linear_offset;
c2c75131 2743 }
e506a0c6 2744
8d0deca8 2745 if (rotation == BIT(DRM_ROTATE_180)) {
48404c1e
SJ
2746 dspcntr |= DISPPLANE_ROTATE_180;
2747
a8d201af
ML
2748 x += (crtc_state->pipe_src_w - 1);
2749 y += (crtc_state->pipe_src_h - 1);
48404c1e
SJ
2750
2751 /* Finding the last pixel of the last line of the display
2752 data and adding to linear_offset*/
2753 linear_offset +=
a8d201af 2754 (crtc_state->pipe_src_h - 1) * fb->pitches[0] +
ac484963 2755 (crtc_state->pipe_src_w - 1) * cpp;
48404c1e
SJ
2756 }
2757
2db3366b
PZ
2758 intel_crtc->adjusted_x = x;
2759 intel_crtc->adjusted_y = y;
2760
48404c1e
SJ
2761 I915_WRITE(reg, dspcntr);
2762
01f2c773 2763 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
a6c45cf0 2764 if (INTEL_INFO(dev)->gen >= 4) {
85ba7b7d
DV
2765 I915_WRITE(DSPSURF(plane),
2766 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
5eddb70b 2767 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
e506a0c6 2768 I915_WRITE(DSPLINOFF(plane), linear_offset);
5eddb70b 2769 } else
f343c5f6 2770 I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
5eddb70b 2771 POSTING_READ(reg);
17638cd6
JB
2772}
2773
a8d201af
ML
2774static void i9xx_disable_primary_plane(struct drm_plane *primary,
2775 struct drm_crtc *crtc)
17638cd6
JB
2776{
2777 struct drm_device *dev = crtc->dev;
2778 struct drm_i915_private *dev_priv = dev->dev_private;
2779 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
17638cd6 2780 int plane = intel_crtc->plane;
f45651ba 2781
a8d201af
ML
2782 I915_WRITE(DSPCNTR(plane), 0);
2783 if (INTEL_INFO(dev_priv)->gen >= 4)
fdd508a6 2784 I915_WRITE(DSPSURF(plane), 0);
a8d201af
ML
2785 else
2786 I915_WRITE(DSPADDR(plane), 0);
2787 POSTING_READ(DSPCNTR(plane));
2788}
c9ba6fad 2789
a8d201af
ML
2790static void ironlake_update_primary_plane(struct drm_plane *primary,
2791 const struct intel_crtc_state *crtc_state,
2792 const struct intel_plane_state *plane_state)
2793{
2794 struct drm_device *dev = primary->dev;
2795 struct drm_i915_private *dev_priv = dev->dev_private;
2796 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
2797 struct drm_framebuffer *fb = plane_state->base.fb;
2798 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
2799 int plane = intel_crtc->plane;
54ea9da8 2800 u32 linear_offset;
a8d201af
ML
2801 u32 dspcntr;
2802 i915_reg_t reg = DSPCNTR(plane);
8d0deca8 2803 unsigned int rotation = plane_state->base.rotation;
ac484963 2804 int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
a8d201af
ML
2805 int x = plane_state->src.x1 >> 16;
2806 int y = plane_state->src.y1 >> 16;
c9ba6fad 2807
f45651ba 2808 dspcntr = DISPPLANE_GAMMA_ENABLE;
fdd508a6 2809 dspcntr |= DISPLAY_PLANE_ENABLE;
f45651ba
VS
2810
2811 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
2812 dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
17638cd6 2813
57779d06
VS
2814 switch (fb->pixel_format) {
2815 case DRM_FORMAT_C8:
17638cd6
JB
2816 dspcntr |= DISPPLANE_8BPP;
2817 break;
57779d06
VS
2818 case DRM_FORMAT_RGB565:
2819 dspcntr |= DISPPLANE_BGRX565;
17638cd6 2820 break;
57779d06 2821 case DRM_FORMAT_XRGB8888:
57779d06
VS
2822 dspcntr |= DISPPLANE_BGRX888;
2823 break;
2824 case DRM_FORMAT_XBGR8888:
57779d06
VS
2825 dspcntr |= DISPPLANE_RGBX888;
2826 break;
2827 case DRM_FORMAT_XRGB2101010:
57779d06
VS
2828 dspcntr |= DISPPLANE_BGRX101010;
2829 break;
2830 case DRM_FORMAT_XBGR2101010:
57779d06 2831 dspcntr |= DISPPLANE_RGBX101010;
17638cd6
JB
2832 break;
2833 default:
baba133a 2834 BUG();
17638cd6
JB
2835 }
2836
2837 if (obj->tiling_mode != I915_TILING_NONE)
2838 dspcntr |= DISPPLANE_TILED;
17638cd6 2839
f45651ba 2840 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev))
1f5d76db 2841 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
17638cd6 2842
ac484963 2843 linear_offset = y * fb->pitches[0] + x * cpp;
c2c75131 2844 intel_crtc->dspaddr_offset =
4f2d9934 2845 intel_compute_tile_offset(&x, &y, fb, 0,
8d0deca8 2846 fb->pitches[0], rotation);
c2c75131 2847 linear_offset -= intel_crtc->dspaddr_offset;
8d0deca8 2848 if (rotation == BIT(DRM_ROTATE_180)) {
48404c1e
SJ
2849 dspcntr |= DISPPLANE_ROTATE_180;
2850
2851 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev)) {
a8d201af
ML
2852 x += (crtc_state->pipe_src_w - 1);
2853 y += (crtc_state->pipe_src_h - 1);
48404c1e
SJ
2854
2855 /* Finding the last pixel of the last line of the display
2856 data and adding to linear_offset*/
2857 linear_offset +=
a8d201af 2858 (crtc_state->pipe_src_h - 1) * fb->pitches[0] +
ac484963 2859 (crtc_state->pipe_src_w - 1) * cpp;
48404c1e
SJ
2860 }
2861 }
2862
2db3366b
PZ
2863 intel_crtc->adjusted_x = x;
2864 intel_crtc->adjusted_y = y;
2865
48404c1e 2866 I915_WRITE(reg, dspcntr);
17638cd6 2867
01f2c773 2868 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
85ba7b7d
DV
2869 I915_WRITE(DSPSURF(plane),
2870 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
b3dc685e 2871 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
bc1c91eb
DL
2872 I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
2873 } else {
2874 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2875 I915_WRITE(DSPLINOFF(plane), linear_offset);
2876 }
17638cd6 2877 POSTING_READ(reg);
17638cd6
JB
2878}
2879
7b49f948
VS
2880u32 intel_fb_stride_alignment(const struct drm_i915_private *dev_priv,
2881 uint64_t fb_modifier, uint32_t pixel_format)
b321803d 2882{
7b49f948 2883 if (fb_modifier == DRM_FORMAT_MOD_NONE) {
b321803d 2884 return 64;
7b49f948
VS
2885 } else {
2886 int cpp = drm_format_plane_cpp(pixel_format, 0);
2887
27ba3910 2888 return intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
b321803d
DL
2889 }
2890}
2891
44eb0cb9
MK
2892u32 intel_plane_obj_offset(struct intel_plane *intel_plane,
2893 struct drm_i915_gem_object *obj,
2894 unsigned int plane)
121920fa 2895{
ce7f1728 2896 struct i915_ggtt_view view;
dedf278c 2897 struct i915_vma *vma;
44eb0cb9 2898 u64 offset;
121920fa 2899
e7941294 2900 intel_fill_fb_ggtt_view(&view, intel_plane->base.state->fb,
3465c580 2901 intel_plane->base.state->rotation);
121920fa 2902
ce7f1728 2903 vma = i915_gem_obj_to_ggtt_view(obj, &view);
dedf278c 2904 if (WARN(!vma, "ggtt vma for display object not found! (view=%u)\n",
ce7f1728 2905 view.type))
dedf278c
TU
2906 return -1;
2907
44eb0cb9 2908 offset = vma->node.start;
dedf278c
TU
2909
2910 if (plane == 1) {
7723f47d 2911 offset += vma->ggtt_view.params.rotated.uv_start_page *
dedf278c
TU
2912 PAGE_SIZE;
2913 }
2914
44eb0cb9
MK
2915 WARN_ON(upper_32_bits(offset));
2916
2917 return lower_32_bits(offset);
121920fa
TU
2918}
2919
e435d6e5
ML
2920static void skl_detach_scaler(struct intel_crtc *intel_crtc, int id)
2921{
2922 struct drm_device *dev = intel_crtc->base.dev;
2923 struct drm_i915_private *dev_priv = dev->dev_private;
2924
2925 I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, id), 0);
2926 I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, id), 0);
2927 I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, id), 0);
e435d6e5
ML
2928}
2929
a1b2278e
CK
2930/*
2931 * This function detaches (aka. unbinds) unused scalers in hardware
2932 */
0583236e 2933static void skl_detach_scalers(struct intel_crtc *intel_crtc)
a1b2278e 2934{
a1b2278e
CK
2935 struct intel_crtc_scaler_state *scaler_state;
2936 int i;
2937
a1b2278e
CK
2938 scaler_state = &intel_crtc->config->scaler_state;
2939
2940 /* loop through and disable scalers that aren't in use */
2941 for (i = 0; i < intel_crtc->num_scalers; i++) {
e435d6e5
ML
2942 if (!scaler_state->scalers[i].in_use)
2943 skl_detach_scaler(intel_crtc, i);
a1b2278e
CK
2944 }
2945}
2946
6156a456 2947u32 skl_plane_ctl_format(uint32_t pixel_format)
70d21f0e 2948{
6156a456 2949 switch (pixel_format) {
d161cf7a 2950 case DRM_FORMAT_C8:
c34ce3d1 2951 return PLANE_CTL_FORMAT_INDEXED;
70d21f0e 2952 case DRM_FORMAT_RGB565:
c34ce3d1 2953 return PLANE_CTL_FORMAT_RGB_565;
70d21f0e 2954 case DRM_FORMAT_XBGR8888:
c34ce3d1 2955 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
6156a456 2956 case DRM_FORMAT_XRGB8888:
c34ce3d1 2957 return PLANE_CTL_FORMAT_XRGB_8888;
6156a456
CK
2958 /*
2959 * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
2960 * to be already pre-multiplied. We need to add a knob (or a different
2961 * DRM_FORMAT) for user-space to configure that.
2962 */
f75fb42a 2963 case DRM_FORMAT_ABGR8888:
c34ce3d1 2964 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX |
6156a456 2965 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
6156a456 2966 case DRM_FORMAT_ARGB8888:
c34ce3d1 2967 return PLANE_CTL_FORMAT_XRGB_8888 |
6156a456 2968 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
70d21f0e 2969 case DRM_FORMAT_XRGB2101010:
c34ce3d1 2970 return PLANE_CTL_FORMAT_XRGB_2101010;
70d21f0e 2971 case DRM_FORMAT_XBGR2101010:
c34ce3d1 2972 return PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
6156a456 2973 case DRM_FORMAT_YUYV:
c34ce3d1 2974 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
6156a456 2975 case DRM_FORMAT_YVYU:
c34ce3d1 2976 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
6156a456 2977 case DRM_FORMAT_UYVY:
c34ce3d1 2978 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
6156a456 2979 case DRM_FORMAT_VYUY:
c34ce3d1 2980 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
70d21f0e 2981 default:
4249eeef 2982 MISSING_CASE(pixel_format);
70d21f0e 2983 }
8cfcba41 2984
c34ce3d1 2985 return 0;
6156a456 2986}
70d21f0e 2987
6156a456
CK
2988u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
2989{
6156a456 2990 switch (fb_modifier) {
30af77c4 2991 case DRM_FORMAT_MOD_NONE:
70d21f0e 2992 break;
30af77c4 2993 case I915_FORMAT_MOD_X_TILED:
c34ce3d1 2994 return PLANE_CTL_TILED_X;
b321803d 2995 case I915_FORMAT_MOD_Y_TILED:
c34ce3d1 2996 return PLANE_CTL_TILED_Y;
b321803d 2997 case I915_FORMAT_MOD_Yf_TILED:
c34ce3d1 2998 return PLANE_CTL_TILED_YF;
70d21f0e 2999 default:
6156a456 3000 MISSING_CASE(fb_modifier);
70d21f0e 3001 }
8cfcba41 3002
c34ce3d1 3003 return 0;
6156a456 3004}
70d21f0e 3005
6156a456
CK
3006u32 skl_plane_ctl_rotation(unsigned int rotation)
3007{
3b7a5119 3008 switch (rotation) {
6156a456
CK
3009 case BIT(DRM_ROTATE_0):
3010 break;
1e8df167
SJ
3011 /*
3012 * DRM_ROTATE_ is counter clockwise to stay compatible with Xrandr
3013 * while i915 HW rotation is clockwise, thats why this swapping.
3014 */
3b7a5119 3015 case BIT(DRM_ROTATE_90):
1e8df167 3016 return PLANE_CTL_ROTATE_270;
3b7a5119 3017 case BIT(DRM_ROTATE_180):
c34ce3d1 3018 return PLANE_CTL_ROTATE_180;
3b7a5119 3019 case BIT(DRM_ROTATE_270):
1e8df167 3020 return PLANE_CTL_ROTATE_90;
6156a456
CK
3021 default:
3022 MISSING_CASE(rotation);
3023 }
3024
c34ce3d1 3025 return 0;
6156a456
CK
3026}
3027
a8d201af
ML
3028static void skylake_update_primary_plane(struct drm_plane *plane,
3029 const struct intel_crtc_state *crtc_state,
3030 const struct intel_plane_state *plane_state)
6156a456 3031{
a8d201af 3032 struct drm_device *dev = plane->dev;
6156a456 3033 struct drm_i915_private *dev_priv = dev->dev_private;
a8d201af
ML
3034 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
3035 struct drm_framebuffer *fb = plane_state->base.fb;
3036 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
6156a456
CK
3037 int pipe = intel_crtc->pipe;
3038 u32 plane_ctl, stride_div, stride;
3039 u32 tile_height, plane_offset, plane_size;
a8d201af 3040 unsigned int rotation = plane_state->base.rotation;
6156a456 3041 int x_offset, y_offset;
44eb0cb9 3042 u32 surf_addr;
a8d201af
ML
3043 int scaler_id = plane_state->scaler_id;
3044 int src_x = plane_state->src.x1 >> 16;
3045 int src_y = plane_state->src.y1 >> 16;
3046 int src_w = drm_rect_width(&plane_state->src) >> 16;
3047 int src_h = drm_rect_height(&plane_state->src) >> 16;
3048 int dst_x = plane_state->dst.x1;
3049 int dst_y = plane_state->dst.y1;
3050 int dst_w = drm_rect_width(&plane_state->dst);
3051 int dst_h = drm_rect_height(&plane_state->dst);
70d21f0e 3052
6156a456
CK
3053 plane_ctl = PLANE_CTL_ENABLE |
3054 PLANE_CTL_PIPE_GAMMA_ENABLE |
3055 PLANE_CTL_PIPE_CSC_ENABLE;
3056
3057 plane_ctl |= skl_plane_ctl_format(fb->pixel_format);
3058 plane_ctl |= skl_plane_ctl_tiling(fb->modifier[0]);
3059 plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
6156a456
CK
3060 plane_ctl |= skl_plane_ctl_rotation(rotation);
3061
7b49f948 3062 stride_div = intel_fb_stride_alignment(dev_priv, fb->modifier[0],
b321803d 3063 fb->pixel_format);
dedf278c 3064 surf_addr = intel_plane_obj_offset(to_intel_plane(plane), obj, 0);
3b7a5119 3065
a42e5a23
PZ
3066 WARN_ON(drm_rect_width(&plane_state->src) == 0);
3067
3b7a5119 3068 if (intel_rotation_90_or_270(rotation)) {
832be82f
VS
3069 int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
3070
3b7a5119 3071 /* stride = Surface height in tiles */
832be82f 3072 tile_height = intel_tile_height(dev_priv, fb->modifier[0], cpp);
3b7a5119 3073 stride = DIV_ROUND_UP(fb->height, tile_height);
a8d201af
ML
3074 x_offset = stride * tile_height - src_y - src_h;
3075 y_offset = src_x;
6156a456 3076 plane_size = (src_w - 1) << 16 | (src_h - 1);
3b7a5119
SJ
3077 } else {
3078 stride = fb->pitches[0] / stride_div;
a8d201af
ML
3079 x_offset = src_x;
3080 y_offset = src_y;
6156a456 3081 plane_size = (src_h - 1) << 16 | (src_w - 1);
3b7a5119
SJ
3082 }
3083 plane_offset = y_offset << 16 | x_offset;
b321803d 3084
2db3366b
PZ
3085 intel_crtc->adjusted_x = x_offset;
3086 intel_crtc->adjusted_y = y_offset;
3087
70d21f0e 3088 I915_WRITE(PLANE_CTL(pipe, 0), plane_ctl);
3b7a5119
SJ
3089 I915_WRITE(PLANE_OFFSET(pipe, 0), plane_offset);
3090 I915_WRITE(PLANE_SIZE(pipe, 0), plane_size);
3091 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
6156a456
CK
3092
3093 if (scaler_id >= 0) {
3094 uint32_t ps_ctrl = 0;
3095
3096 WARN_ON(!dst_w || !dst_h);
3097 ps_ctrl = PS_SCALER_EN | PS_PLANE_SEL(0) |
3098 crtc_state->scaler_state.scalers[scaler_id].mode;
3099 I915_WRITE(SKL_PS_CTRL(pipe, scaler_id), ps_ctrl);
3100 I915_WRITE(SKL_PS_PWR_GATE(pipe, scaler_id), 0);
3101 I915_WRITE(SKL_PS_WIN_POS(pipe, scaler_id), (dst_x << 16) | dst_y);
3102 I915_WRITE(SKL_PS_WIN_SZ(pipe, scaler_id), (dst_w << 16) | dst_h);
3103 I915_WRITE(PLANE_POS(pipe, 0), 0);
3104 } else {
3105 I915_WRITE(PLANE_POS(pipe, 0), (dst_y << 16) | dst_x);
3106 }
3107
121920fa 3108 I915_WRITE(PLANE_SURF(pipe, 0), surf_addr);
70d21f0e
DL
3109
3110 POSTING_READ(PLANE_SURF(pipe, 0));
3111}
3112
a8d201af
ML
3113static void skylake_disable_primary_plane(struct drm_plane *primary,
3114 struct drm_crtc *crtc)
17638cd6
JB
3115{
3116 struct drm_device *dev = crtc->dev;
3117 struct drm_i915_private *dev_priv = dev->dev_private;
a8d201af 3118 int pipe = to_intel_crtc(crtc)->pipe;
17638cd6 3119
a8d201af
ML
3120 I915_WRITE(PLANE_CTL(pipe, 0), 0);
3121 I915_WRITE(PLANE_SURF(pipe, 0), 0);
3122 POSTING_READ(PLANE_SURF(pipe, 0));
3123}
29b9bde6 3124
a8d201af
ML
3125/* Assume fb object is pinned & idle & fenced and just update base pointers */
3126static int
3127intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
3128 int x, int y, enum mode_set_atomic state)
3129{
3130 /* Support for kgdboc is disabled, this needs a major rework. */
3131 DRM_ERROR("legacy panic handler not supported any more.\n");
3132
3133 return -ENODEV;
81255565
JB
3134}
3135
5a21b665
DV
3136static void intel_complete_page_flips(struct drm_i915_private *dev_priv)
3137{
3138 struct intel_crtc *crtc;
3139
3140 for_each_intel_crtc(dev_priv->dev, crtc)
3141 intel_finish_page_flip_cs(dev_priv, crtc->pipe);
3142}
3143
7514747d
VS
3144static void intel_update_primary_planes(struct drm_device *dev)
3145{
7514747d 3146 struct drm_crtc *crtc;
96a02917 3147
70e1e0ec 3148 for_each_crtc(dev, crtc) {
11c22da6
ML
3149 struct intel_plane *plane = to_intel_plane(crtc->primary);
3150 struct intel_plane_state *plane_state;
96a02917 3151
11c22da6 3152 drm_modeset_lock_crtc(crtc, &plane->base);
11c22da6
ML
3153 plane_state = to_intel_plane_state(plane->base.state);
3154
a8d201af
ML
3155 if (plane_state->visible)
3156 plane->update_plane(&plane->base,
3157 to_intel_crtc_state(crtc->state),
3158 plane_state);
11c22da6
ML
3159
3160 drm_modeset_unlock_crtc(crtc);
96a02917
VS
3161 }
3162}
3163
c033666a 3164void intel_prepare_reset(struct drm_i915_private *dev_priv)
7514747d
VS
3165{
3166 /* no reset support for gen2 */
c033666a 3167 if (IS_GEN2(dev_priv))
7514747d
VS
3168 return;
3169
3170 /* reset doesn't touch the display */
c033666a 3171 if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
7514747d
VS
3172 return;
3173
c033666a 3174 drm_modeset_lock_all(dev_priv->dev);
f98ce92f
VS
3175 /*
3176 * Disabling the crtcs gracefully seems nicer. Also the
3177 * g33 docs say we should at least disable all the planes.
3178 */
c033666a 3179 intel_display_suspend(dev_priv->dev);
7514747d
VS
3180}
3181
c033666a 3182void intel_finish_reset(struct drm_i915_private *dev_priv)
7514747d 3183{
5a21b665
DV
3184 /*
3185 * Flips in the rings will be nuked by the reset,
3186 * so complete all pending flips so that user space
3187 * will get its events and not get stuck.
3188 */
3189 intel_complete_page_flips(dev_priv);
3190
7514747d 3191 /* no reset support for gen2 */
c033666a 3192 if (IS_GEN2(dev_priv))
7514747d
VS
3193 return;
3194
3195 /* reset doesn't touch the display */
c033666a 3196 if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv)) {
7514747d
VS
3197 /*
3198 * Flips in the rings have been nuked by the reset,
3199 * so update the base address of all primary
3200 * planes to the the last fb to make sure we're
3201 * showing the correct fb after a reset.
11c22da6
ML
3202 *
3203 * FIXME: Atomic will make this obsolete since we won't schedule
3204 * CS-based flips (which might get lost in gpu resets) any more.
7514747d 3205 */
c033666a 3206 intel_update_primary_planes(dev_priv->dev);
7514747d
VS
3207 return;
3208 }
3209
3210 /*
3211 * The display has been reset as well,
3212 * so need a full re-initialization.
3213 */
3214 intel_runtime_pm_disable_interrupts(dev_priv);
3215 intel_runtime_pm_enable_interrupts(dev_priv);
3216
c033666a 3217 intel_modeset_init_hw(dev_priv->dev);
7514747d
VS
3218
3219 spin_lock_irq(&dev_priv->irq_lock);
3220 if (dev_priv->display.hpd_irq_setup)
91d14251 3221 dev_priv->display.hpd_irq_setup(dev_priv);
7514747d
VS
3222 spin_unlock_irq(&dev_priv->irq_lock);
3223
c033666a 3224 intel_display_resume(dev_priv->dev);
7514747d
VS
3225
3226 intel_hpd_init(dev_priv);
3227
c033666a 3228 drm_modeset_unlock_all(dev_priv->dev);
7514747d
VS
3229}
3230
7d5e3799
CW
3231static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
3232{
5a21b665
DV
3233 struct drm_device *dev = crtc->dev;
3234 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3235 unsigned reset_counter;
3236 bool pending;
3237
3238 reset_counter = i915_reset_counter(&to_i915(dev)->gpu_error);
3239 if (intel_crtc->reset_counter != reset_counter)
3240 return false;
3241
3242 spin_lock_irq(&dev->event_lock);
3243 pending = to_intel_crtc(crtc)->flip_work != NULL;
3244 spin_unlock_irq(&dev->event_lock);
3245
3246 return pending;
7d5e3799
CW
3247}
3248
bfd16b2a
ML
3249static void intel_update_pipe_config(struct intel_crtc *crtc,
3250 struct intel_crtc_state *old_crtc_state)
e30e8f75
GP
3251{
3252 struct drm_device *dev = crtc->base.dev;
3253 struct drm_i915_private *dev_priv = dev->dev_private;
bfd16b2a
ML
3254 struct intel_crtc_state *pipe_config =
3255 to_intel_crtc_state(crtc->base.state);
e30e8f75 3256
bfd16b2a
ML
3257 /* drm_atomic_helper_update_legacy_modeset_state might not be called. */
3258 crtc->base.mode = crtc->base.state->mode;
3259
3260 DRM_DEBUG_KMS("Updating pipe size %ix%i -> %ix%i\n",
3261 old_crtc_state->pipe_src_w, old_crtc_state->pipe_src_h,
3262 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
e30e8f75
GP
3263
3264 /*
3265 * Update pipe size and adjust fitter if needed: the reason for this is
3266 * that in compute_mode_changes we check the native mode (not the pfit
3267 * mode) to see if we can flip rather than do a full mode set. In the
3268 * fastboot case, we'll flip, but if we don't update the pipesrc and
3269 * pfit state, we'll end up with a big fb scanned out into the wrong
3270 * sized surface.
e30e8f75
GP
3271 */
3272
e30e8f75 3273 I915_WRITE(PIPESRC(crtc->pipe),
bfd16b2a
ML
3274 ((pipe_config->pipe_src_w - 1) << 16) |
3275 (pipe_config->pipe_src_h - 1));
3276
3277 /* on skylake this is done by detaching scalers */
3278 if (INTEL_INFO(dev)->gen >= 9) {
3279 skl_detach_scalers(crtc);
3280
3281 if (pipe_config->pch_pfit.enabled)
3282 skylake_pfit_enable(crtc);
3283 } else if (HAS_PCH_SPLIT(dev)) {
3284 if (pipe_config->pch_pfit.enabled)
3285 ironlake_pfit_enable(crtc);
3286 else if (old_crtc_state->pch_pfit.enabled)
3287 ironlake_pfit_disable(crtc, true);
e30e8f75 3288 }
e30e8f75
GP
3289}
3290
5e84e1a4
ZW
3291static void intel_fdi_normal_train(struct drm_crtc *crtc)
3292{
3293 struct drm_device *dev = crtc->dev;
3294 struct drm_i915_private *dev_priv = dev->dev_private;
3295 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3296 int pipe = intel_crtc->pipe;
f0f59a00
VS
3297 i915_reg_t reg;
3298 u32 temp;
5e84e1a4
ZW
3299
3300 /* enable normal train */
3301 reg = FDI_TX_CTL(pipe);
3302 temp = I915_READ(reg);
61e499bf 3303 if (IS_IVYBRIDGE(dev)) {
357555c0
JB
3304 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3305 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
61e499bf
KP
3306 } else {
3307 temp &= ~FDI_LINK_TRAIN_NONE;
3308 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
357555c0 3309 }
5e84e1a4
ZW
3310 I915_WRITE(reg, temp);
3311
3312 reg = FDI_RX_CTL(pipe);
3313 temp = I915_READ(reg);
3314 if (HAS_PCH_CPT(dev)) {
3315 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3316 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
3317 } else {
3318 temp &= ~FDI_LINK_TRAIN_NONE;
3319 temp |= FDI_LINK_TRAIN_NONE;
3320 }
3321 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
3322
3323 /* wait one idle pattern time */
3324 POSTING_READ(reg);
3325 udelay(1000);
357555c0
JB
3326
3327 /* IVB wants error correction enabled */
3328 if (IS_IVYBRIDGE(dev))
3329 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
3330 FDI_FE_ERRC_ENABLE);
5e84e1a4
ZW
3331}
3332
8db9d77b
ZW
3333/* The FDI link training functions for ILK/Ibexpeak. */
3334static void ironlake_fdi_link_train(struct drm_crtc *crtc)
3335{
3336 struct drm_device *dev = crtc->dev;
3337 struct drm_i915_private *dev_priv = dev->dev_private;
3338 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3339 int pipe = intel_crtc->pipe;
f0f59a00
VS
3340 i915_reg_t reg;
3341 u32 temp, tries;
8db9d77b 3342
1c8562f6 3343 /* FDI needs bits from pipe first */
0fc932b8 3344 assert_pipe_enabled(dev_priv, pipe);
0fc932b8 3345
e1a44743
AJ
3346 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3347 for train result */
5eddb70b
CW
3348 reg = FDI_RX_IMR(pipe);
3349 temp = I915_READ(reg);
e1a44743
AJ
3350 temp &= ~FDI_RX_SYMBOL_LOCK;
3351 temp &= ~FDI_RX_BIT_LOCK;
5eddb70b
CW
3352 I915_WRITE(reg, temp);
3353 I915_READ(reg);
e1a44743
AJ
3354 udelay(150);
3355
8db9d77b 3356 /* enable CPU FDI TX and PCH FDI RX */
5eddb70b
CW
3357 reg = FDI_TX_CTL(pipe);
3358 temp = I915_READ(reg);
627eb5a3 3359 temp &= ~FDI_DP_PORT_WIDTH_MASK;
6e3c9717 3360 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
8db9d77b
ZW
3361 temp &= ~FDI_LINK_TRAIN_NONE;
3362 temp |= FDI_LINK_TRAIN_PATTERN_1;
5eddb70b 3363 I915_WRITE(reg, temp | FDI_TX_ENABLE);
8db9d77b 3364
5eddb70b
CW
3365 reg = FDI_RX_CTL(pipe);
3366 temp = I915_READ(reg);
8db9d77b
ZW
3367 temp &= ~FDI_LINK_TRAIN_NONE;
3368 temp |= FDI_LINK_TRAIN_PATTERN_1;
5eddb70b
CW
3369 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3370
3371 POSTING_READ(reg);
8db9d77b
ZW
3372 udelay(150);
3373
5b2adf89 3374 /* Ironlake workaround, enable clock pointer after FDI enable*/
8f5718a6
DV
3375 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
3376 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
3377 FDI_RX_PHASE_SYNC_POINTER_EN);
5b2adf89 3378
5eddb70b 3379 reg = FDI_RX_IIR(pipe);
e1a44743 3380 for (tries = 0; tries < 5; tries++) {
5eddb70b 3381 temp = I915_READ(reg);
8db9d77b
ZW
3382 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3383
3384 if ((temp & FDI_RX_BIT_LOCK)) {
3385 DRM_DEBUG_KMS("FDI train 1 done.\n");
5eddb70b 3386 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
8db9d77b
ZW
3387 break;
3388 }
8db9d77b 3389 }
e1a44743 3390 if (tries == 5)
5eddb70b 3391 DRM_ERROR("FDI train 1 fail!\n");
8db9d77b
ZW
3392
3393 /* Train 2 */
5eddb70b
CW
3394 reg = FDI_TX_CTL(pipe);
3395 temp = I915_READ(reg);
8db9d77b
ZW
3396 temp &= ~FDI_LINK_TRAIN_NONE;
3397 temp |= FDI_LINK_TRAIN_PATTERN_2;
5eddb70b 3398 I915_WRITE(reg, temp);
8db9d77b 3399
5eddb70b
CW
3400 reg = FDI_RX_CTL(pipe);
3401 temp = I915_READ(reg);
8db9d77b
ZW
3402 temp &= ~FDI_LINK_TRAIN_NONE;
3403 temp |= FDI_LINK_TRAIN_PATTERN_2;
5eddb70b 3404 I915_WRITE(reg, temp);
8db9d77b 3405
5eddb70b
CW
3406 POSTING_READ(reg);
3407 udelay(150);
8db9d77b 3408
5eddb70b 3409 reg = FDI_RX_IIR(pipe);
e1a44743 3410 for (tries = 0; tries < 5; tries++) {
5eddb70b 3411 temp = I915_READ(reg);
8db9d77b
ZW
3412 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3413
3414 if (temp & FDI_RX_SYMBOL_LOCK) {
5eddb70b 3415 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
8db9d77b
ZW
3416 DRM_DEBUG_KMS("FDI train 2 done.\n");
3417 break;
3418 }
8db9d77b 3419 }
e1a44743 3420 if (tries == 5)
5eddb70b 3421 DRM_ERROR("FDI train 2 fail!\n");
8db9d77b
ZW
3422
3423 DRM_DEBUG_KMS("FDI train done\n");
5c5313c8 3424
8db9d77b
ZW
3425}
3426
0206e353 3427static const int snb_b_fdi_train_param[] = {
8db9d77b
ZW
3428 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
3429 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
3430 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
3431 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
3432};
3433
3434/* The FDI link training functions for SNB/Cougarpoint. */
3435static void gen6_fdi_link_train(struct drm_crtc *crtc)
3436{
3437 struct drm_device *dev = crtc->dev;
3438 struct drm_i915_private *dev_priv = dev->dev_private;
3439 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3440 int pipe = intel_crtc->pipe;
f0f59a00
VS
3441 i915_reg_t reg;
3442 u32 temp, i, retry;
8db9d77b 3443
e1a44743
AJ
3444 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3445 for train result */
5eddb70b
CW
3446 reg = FDI_RX_IMR(pipe);
3447 temp = I915_READ(reg);
e1a44743
AJ
3448 temp &= ~FDI_RX_SYMBOL_LOCK;
3449 temp &= ~FDI_RX_BIT_LOCK;
5eddb70b
CW
3450 I915_WRITE(reg, temp);
3451
3452 POSTING_READ(reg);
e1a44743
AJ
3453 udelay(150);
3454
8db9d77b 3455 /* enable CPU FDI TX and PCH FDI RX */
5eddb70b
CW
3456 reg = FDI_TX_CTL(pipe);
3457 temp = I915_READ(reg);
627eb5a3 3458 temp &= ~FDI_DP_PORT_WIDTH_MASK;
6e3c9717 3459 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
8db9d77b
ZW
3460 temp &= ~FDI_LINK_TRAIN_NONE;
3461 temp |= FDI_LINK_TRAIN_PATTERN_1;
3462 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3463 /* SNB-B */
3464 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
5eddb70b 3465 I915_WRITE(reg, temp | FDI_TX_ENABLE);
8db9d77b 3466
d74cf324
DV
3467 I915_WRITE(FDI_RX_MISC(pipe),
3468 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3469
5eddb70b
CW
3470 reg = FDI_RX_CTL(pipe);
3471 temp = I915_READ(reg);
8db9d77b
ZW
3472 if (HAS_PCH_CPT(dev)) {
3473 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3474 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3475 } else {
3476 temp &= ~FDI_LINK_TRAIN_NONE;
3477 temp |= FDI_LINK_TRAIN_PATTERN_1;
3478 }
5eddb70b
CW
3479 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3480
3481 POSTING_READ(reg);
8db9d77b
ZW
3482 udelay(150);
3483
0206e353 3484 for (i = 0; i < 4; i++) {
5eddb70b
CW
3485 reg = FDI_TX_CTL(pipe);
3486 temp = I915_READ(reg);
8db9d77b
ZW
3487 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3488 temp |= snb_b_fdi_train_param[i];
5eddb70b
CW
3489 I915_WRITE(reg, temp);
3490
3491 POSTING_READ(reg);
8db9d77b
ZW
3492 udelay(500);
3493
fa37d39e
SP
3494 for (retry = 0; retry < 5; retry++) {
3495 reg = FDI_RX_IIR(pipe);
3496 temp = I915_READ(reg);
3497 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3498 if (temp & FDI_RX_BIT_LOCK) {
3499 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3500 DRM_DEBUG_KMS("FDI train 1 done.\n");
3501 break;
3502 }
3503 udelay(50);
8db9d77b 3504 }
fa37d39e
SP
3505 if (retry < 5)
3506 break;
8db9d77b
ZW
3507 }
3508 if (i == 4)
5eddb70b 3509 DRM_ERROR("FDI train 1 fail!\n");
8db9d77b
ZW
3510
3511 /* Train 2 */
5eddb70b
CW
3512 reg = FDI_TX_CTL(pipe);
3513 temp = I915_READ(reg);
8db9d77b
ZW
3514 temp &= ~FDI_LINK_TRAIN_NONE;
3515 temp |= FDI_LINK_TRAIN_PATTERN_2;
3516 if (IS_GEN6(dev)) {
3517 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3518 /* SNB-B */
3519 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
3520 }
5eddb70b 3521 I915_WRITE(reg, temp);
8db9d77b 3522
5eddb70b
CW
3523 reg = FDI_RX_CTL(pipe);
3524 temp = I915_READ(reg);
8db9d77b
ZW
3525 if (HAS_PCH_CPT(dev)) {
3526 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3527 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
3528 } else {
3529 temp &= ~FDI_LINK_TRAIN_NONE;
3530 temp |= FDI_LINK_TRAIN_PATTERN_2;
3531 }
5eddb70b
CW
3532 I915_WRITE(reg, temp);
3533
3534 POSTING_READ(reg);
8db9d77b
ZW
3535 udelay(150);
3536
0206e353 3537 for (i = 0; i < 4; i++) {
5eddb70b
CW
3538 reg = FDI_TX_CTL(pipe);
3539 temp = I915_READ(reg);
8db9d77b
ZW
3540 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3541 temp |= snb_b_fdi_train_param[i];
5eddb70b
CW
3542 I915_WRITE(reg, temp);
3543
3544 POSTING_READ(reg);
8db9d77b
ZW
3545 udelay(500);
3546
fa37d39e
SP
3547 for (retry = 0; retry < 5; retry++) {
3548 reg = FDI_RX_IIR(pipe);
3549 temp = I915_READ(reg);
3550 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3551 if (temp & FDI_RX_SYMBOL_LOCK) {
3552 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3553 DRM_DEBUG_KMS("FDI train 2 done.\n");
3554 break;
3555 }
3556 udelay(50);
8db9d77b 3557 }
fa37d39e
SP
3558 if (retry < 5)
3559 break;
8db9d77b
ZW
3560 }
3561 if (i == 4)
5eddb70b 3562 DRM_ERROR("FDI train 2 fail!\n");
8db9d77b
ZW
3563
3564 DRM_DEBUG_KMS("FDI train done.\n");
3565}
3566
357555c0
JB
3567/* Manual link training for Ivy Bridge A0 parts */
3568static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
3569{
3570 struct drm_device *dev = crtc->dev;
3571 struct drm_i915_private *dev_priv = dev->dev_private;
3572 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3573 int pipe = intel_crtc->pipe;
f0f59a00
VS
3574 i915_reg_t reg;
3575 u32 temp, i, j;
357555c0
JB
3576
3577 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3578 for train result */
3579 reg = FDI_RX_IMR(pipe);
3580 temp = I915_READ(reg);
3581 temp &= ~FDI_RX_SYMBOL_LOCK;
3582 temp &= ~FDI_RX_BIT_LOCK;
3583 I915_WRITE(reg, temp);
3584
3585 POSTING_READ(reg);
3586 udelay(150);
3587
01a415fd
DV
3588 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
3589 I915_READ(FDI_RX_IIR(pipe)));
3590
139ccd3f
JB
3591 /* Try each vswing and preemphasis setting twice before moving on */
3592 for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
3593 /* disable first in case we need to retry */
3594 reg = FDI_TX_CTL(pipe);
3595 temp = I915_READ(reg);
3596 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
3597 temp &= ~FDI_TX_ENABLE;
3598 I915_WRITE(reg, temp);
357555c0 3599
139ccd3f
JB
3600 reg = FDI_RX_CTL(pipe);
3601 temp = I915_READ(reg);
3602 temp &= ~FDI_LINK_TRAIN_AUTO;
3603 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3604 temp &= ~FDI_RX_ENABLE;
3605 I915_WRITE(reg, temp);
357555c0 3606
139ccd3f 3607 /* enable CPU FDI TX and PCH FDI RX */
357555c0
JB
3608 reg = FDI_TX_CTL(pipe);
3609 temp = I915_READ(reg);
139ccd3f 3610 temp &= ~FDI_DP_PORT_WIDTH_MASK;
6e3c9717 3611 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
139ccd3f 3612 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
357555c0 3613 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
139ccd3f
JB
3614 temp |= snb_b_fdi_train_param[j/2];
3615 temp |= FDI_COMPOSITE_SYNC;
3616 I915_WRITE(reg, temp | FDI_TX_ENABLE);
357555c0 3617
139ccd3f
JB
3618 I915_WRITE(FDI_RX_MISC(pipe),
3619 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
357555c0 3620
139ccd3f 3621 reg = FDI_RX_CTL(pipe);
357555c0 3622 temp = I915_READ(reg);
139ccd3f
JB
3623 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3624 temp |= FDI_COMPOSITE_SYNC;
3625 I915_WRITE(reg, temp | FDI_RX_ENABLE);
357555c0 3626
139ccd3f
JB
3627 POSTING_READ(reg);
3628 udelay(1); /* should be 0.5us */
357555c0 3629
139ccd3f
JB
3630 for (i = 0; i < 4; i++) {
3631 reg = FDI_RX_IIR(pipe);
3632 temp = I915_READ(reg);
3633 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
357555c0 3634
139ccd3f
JB
3635 if (temp & FDI_RX_BIT_LOCK ||
3636 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
3637 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3638 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
3639 i);
3640 break;
3641 }
3642 udelay(1); /* should be 0.5us */
3643 }
3644 if (i == 4) {
3645 DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
3646 continue;
3647 }
357555c0 3648
139ccd3f 3649 /* Train 2 */
357555c0
JB
3650 reg = FDI_TX_CTL(pipe);
3651 temp = I915_READ(reg);
139ccd3f
JB
3652 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3653 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
3654 I915_WRITE(reg, temp);
3655
3656 reg = FDI_RX_CTL(pipe);
3657 temp = I915_READ(reg);
3658 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3659 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
357555c0
JB
3660 I915_WRITE(reg, temp);
3661
3662 POSTING_READ(reg);
139ccd3f 3663 udelay(2); /* should be 1.5us */
357555c0 3664
139ccd3f
JB
3665 for (i = 0; i < 4; i++) {
3666 reg = FDI_RX_IIR(pipe);
3667 temp = I915_READ(reg);
3668 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
357555c0 3669
139ccd3f
JB
3670 if (temp & FDI_RX_SYMBOL_LOCK ||
3671 (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
3672 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3673 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
3674 i);
3675 goto train_done;
3676 }
3677 udelay(2); /* should be 1.5us */
357555c0 3678 }
139ccd3f
JB
3679 if (i == 4)
3680 DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
357555c0 3681 }
357555c0 3682
139ccd3f 3683train_done:
357555c0
JB
3684 DRM_DEBUG_KMS("FDI train done.\n");
3685}
3686
88cefb6c 3687static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
2c07245f 3688{
88cefb6c 3689 struct drm_device *dev = intel_crtc->base.dev;
2c07245f 3690 struct drm_i915_private *dev_priv = dev->dev_private;
2c07245f 3691 int pipe = intel_crtc->pipe;
f0f59a00
VS
3692 i915_reg_t reg;
3693 u32 temp;
c64e311e 3694
c98e9dcf 3695 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
5eddb70b
CW
3696 reg = FDI_RX_CTL(pipe);
3697 temp = I915_READ(reg);
627eb5a3 3698 temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
6e3c9717 3699 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
dfd07d72 3700 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
5eddb70b
CW
3701 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
3702
3703 POSTING_READ(reg);
c98e9dcf
JB
3704 udelay(200);
3705
3706 /* Switch from Rawclk to PCDclk */
5eddb70b
CW
3707 temp = I915_READ(reg);
3708 I915_WRITE(reg, temp | FDI_PCDCLK);
3709
3710 POSTING_READ(reg);
c98e9dcf
JB
3711 udelay(200);
3712
20749730
PZ
3713 /* Enable CPU FDI TX PLL, always on for Ironlake */
3714 reg = FDI_TX_CTL(pipe);
3715 temp = I915_READ(reg);
3716 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
3717 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
5eddb70b 3718
20749730
PZ
3719 POSTING_READ(reg);
3720 udelay(100);
6be4a607 3721 }
0e23b99d
JB
3722}
3723
88cefb6c
DV
3724static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
3725{
3726 struct drm_device *dev = intel_crtc->base.dev;
3727 struct drm_i915_private *dev_priv = dev->dev_private;
3728 int pipe = intel_crtc->pipe;
f0f59a00
VS
3729 i915_reg_t reg;
3730 u32 temp;
88cefb6c
DV
3731
3732 /* Switch from PCDclk to Rawclk */
3733 reg = FDI_RX_CTL(pipe);
3734 temp = I915_READ(reg);
3735 I915_WRITE(reg, temp & ~FDI_PCDCLK);
3736
3737 /* Disable CPU FDI TX PLL */
3738 reg = FDI_TX_CTL(pipe);
3739 temp = I915_READ(reg);
3740 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
3741
3742 POSTING_READ(reg);
3743 udelay(100);
3744
3745 reg = FDI_RX_CTL(pipe);
3746 temp = I915_READ(reg);
3747 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
3748
3749 /* Wait for the clocks to turn off. */
3750 POSTING_READ(reg);
3751 udelay(100);
3752}
3753
0fc932b8
JB
3754static void ironlake_fdi_disable(struct drm_crtc *crtc)
3755{
3756 struct drm_device *dev = crtc->dev;
3757 struct drm_i915_private *dev_priv = dev->dev_private;
3758 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3759 int pipe = intel_crtc->pipe;
f0f59a00
VS
3760 i915_reg_t reg;
3761 u32 temp;
0fc932b8
JB
3762
3763 /* disable CPU FDI tx and PCH FDI rx */
3764 reg = FDI_TX_CTL(pipe);
3765 temp = I915_READ(reg);
3766 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
3767 POSTING_READ(reg);
3768
3769 reg = FDI_RX_CTL(pipe);
3770 temp = I915_READ(reg);
3771 temp &= ~(0x7 << 16);
dfd07d72 3772 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
0fc932b8
JB
3773 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
3774
3775 POSTING_READ(reg);
3776 udelay(100);
3777
3778 /* Ironlake workaround, disable clock pointer after downing FDI */
eba905b2 3779 if (HAS_PCH_IBX(dev))
6f06ce18 3780 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
0fc932b8
JB
3781
3782 /* still set train pattern 1 */
3783 reg = FDI_TX_CTL(pipe);
3784 temp = I915_READ(reg);
3785 temp &= ~FDI_LINK_TRAIN_NONE;
3786 temp |= FDI_LINK_TRAIN_PATTERN_1;
3787 I915_WRITE(reg, temp);
3788
3789 reg = FDI_RX_CTL(pipe);
3790 temp = I915_READ(reg);
3791 if (HAS_PCH_CPT(dev)) {
3792 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3793 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3794 } else {
3795 temp &= ~FDI_LINK_TRAIN_NONE;
3796 temp |= FDI_LINK_TRAIN_PATTERN_1;
3797 }
3798 /* BPC in FDI rx is consistent with that in PIPECONF */
3799 temp &= ~(0x07 << 16);
dfd07d72 3800 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
0fc932b8
JB
3801 I915_WRITE(reg, temp);
3802
3803 POSTING_READ(reg);
3804 udelay(100);
3805}
3806
5dce5b93
CW
3807bool intel_has_pending_fb_unpin(struct drm_device *dev)
3808{
3809 struct intel_crtc *crtc;
3810
3811 /* Note that we don't need to be called with mode_config.lock here
3812 * as our list of CRTC objects is static for the lifetime of the
3813 * device and so cannot disappear as we iterate. Similarly, we can
3814 * happily treat the predicates as racy, atomic checks as userspace
3815 * cannot claim and pin a new fb without at least acquring the
3816 * struct_mutex and so serialising with us.
3817 */
d3fcc808 3818 for_each_intel_crtc(dev, crtc) {
5dce5b93
CW
3819 if (atomic_read(&crtc->unpin_work_count) == 0)
3820 continue;
3821
5a21b665 3822 if (crtc->flip_work)
5dce5b93
CW
3823 intel_wait_for_vblank(dev, crtc->pipe);
3824
3825 return true;
3826 }
3827
3828 return false;
3829}
3830
5a21b665 3831static void page_flip_completed(struct intel_crtc *intel_crtc)
d6bbafa1
CW
3832{
3833 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
5a21b665
DV
3834 struct intel_flip_work *work = intel_crtc->flip_work;
3835
3836 intel_crtc->flip_work = NULL;
d6bbafa1
CW
3837
3838 if (work->event)
560ce1dc 3839 drm_crtc_send_vblank_event(&intel_crtc->base, work->event);
d6bbafa1
CW
3840
3841 drm_crtc_vblank_put(&intel_crtc->base);
3842
5a21b665 3843 wake_up_all(&dev_priv->pending_flip_queue);
143f73b3 3844 queue_work(dev_priv->wq, &work->unpin_work);
5a21b665
DV
3845
3846 trace_i915_flip_complete(intel_crtc->plane,
3847 work->pending_flip_obj);
d6bbafa1
CW
3848}
3849
5008e874 3850static int intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
e6c3a2a6 3851{
0f91128d 3852 struct drm_device *dev = crtc->dev;
5bb61643 3853 struct drm_i915_private *dev_priv = dev->dev_private;
5008e874 3854 long ret;
e6c3a2a6 3855
2c10d571 3856 WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
5008e874
ML
3857
3858 ret = wait_event_interruptible_timeout(
3859 dev_priv->pending_flip_queue,
3860 !intel_crtc_has_pending_flip(crtc),
3861 60*HZ);
3862
3863 if (ret < 0)
3864 return ret;
3865
5a21b665
DV
3866 if (ret == 0) {
3867 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3868 struct intel_flip_work *work;
3869
3870 spin_lock_irq(&dev->event_lock);
3871 work = intel_crtc->flip_work;
3872 if (work && !is_mmio_work(work)) {
3873 WARN_ONCE(1, "Removing stuck page flip\n");
3874 page_flip_completed(intel_crtc);
3875 }
3876 spin_unlock_irq(&dev->event_lock);
3877 }
5bb61643 3878
5008e874 3879 return 0;
e6c3a2a6
CW
3880}
3881
060f02d8
VS
3882static void lpt_disable_iclkip(struct drm_i915_private *dev_priv)
3883{
3884 u32 temp;
3885
3886 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
3887
3888 mutex_lock(&dev_priv->sb_lock);
3889
3890 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
3891 temp |= SBI_SSCCTL_DISABLE;
3892 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
3893
3894 mutex_unlock(&dev_priv->sb_lock);
3895}
3896
e615efe4
ED
3897/* Program iCLKIP clock to the desired frequency */
3898static void lpt_program_iclkip(struct drm_crtc *crtc)
3899{
64b46a06 3900 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
6e3c9717 3901 int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
e615efe4
ED
3902 u32 divsel, phaseinc, auxdiv, phasedir = 0;
3903 u32 temp;
3904
060f02d8 3905 lpt_disable_iclkip(dev_priv);
e615efe4 3906
64b46a06
VS
3907 /* The iCLK virtual clock root frequency is in MHz,
3908 * but the adjusted_mode->crtc_clock in in KHz. To get the
3909 * divisors, it is necessary to divide one by another, so we
3910 * convert the virtual clock precision to KHz here for higher
3911 * precision.
3912 */
3913 for (auxdiv = 0; auxdiv < 2; auxdiv++) {
e615efe4
ED
3914 u32 iclk_virtual_root_freq = 172800 * 1000;
3915 u32 iclk_pi_range = 64;
64b46a06 3916 u32 desired_divisor;
e615efe4 3917
64b46a06
VS
3918 desired_divisor = DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
3919 clock << auxdiv);
3920 divsel = (desired_divisor / iclk_pi_range) - 2;
3921 phaseinc = desired_divisor % iclk_pi_range;
e615efe4 3922
64b46a06
VS
3923 /*
3924 * Near 20MHz is a corner case which is
3925 * out of range for the 7-bit divisor
3926 */
3927 if (divsel <= 0x7f)
3928 break;
e615efe4
ED
3929 }
3930
3931 /* This should not happen with any sane values */
3932 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
3933 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
3934 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
3935 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
3936
3937 DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
12d7ceed 3938 clock,
e615efe4
ED
3939 auxdiv,
3940 divsel,
3941 phasedir,
3942 phaseinc);
3943
060f02d8
VS
3944 mutex_lock(&dev_priv->sb_lock);
3945
e615efe4 3946 /* Program SSCDIVINTPHASE6 */
988d6ee8 3947 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
e615efe4
ED
3948 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
3949 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
3950 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
3951 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
3952 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
3953 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
988d6ee8 3954 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
e615efe4
ED
3955
3956 /* Program SSCAUXDIV */
988d6ee8 3957 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
e615efe4
ED
3958 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
3959 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
988d6ee8 3960 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
e615efe4
ED
3961
3962 /* Enable modulator and associated divider */
988d6ee8 3963 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
e615efe4 3964 temp &= ~SBI_SSCCTL_DISABLE;
988d6ee8 3965 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
e615efe4 3966
060f02d8
VS
3967 mutex_unlock(&dev_priv->sb_lock);
3968
e615efe4
ED
3969 /* Wait for initialization time */
3970 udelay(24);
3971
3972 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
3973}
3974
8802e5b6
VS
3975int lpt_get_iclkip(struct drm_i915_private *dev_priv)
3976{
3977 u32 divsel, phaseinc, auxdiv;
3978 u32 iclk_virtual_root_freq = 172800 * 1000;
3979 u32 iclk_pi_range = 64;
3980 u32 desired_divisor;
3981 u32 temp;
3982
3983 if ((I915_READ(PIXCLK_GATE) & PIXCLK_GATE_UNGATE) == 0)
3984 return 0;
3985
3986 mutex_lock(&dev_priv->sb_lock);
3987
3988 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
3989 if (temp & SBI_SSCCTL_DISABLE) {
3990 mutex_unlock(&dev_priv->sb_lock);
3991 return 0;
3992 }
3993
3994 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
3995 divsel = (temp & SBI_SSCDIVINTPHASE_DIVSEL_MASK) >>
3996 SBI_SSCDIVINTPHASE_DIVSEL_SHIFT;
3997 phaseinc = (temp & SBI_SSCDIVINTPHASE_INCVAL_MASK) >>
3998 SBI_SSCDIVINTPHASE_INCVAL_SHIFT;
3999
4000 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
4001 auxdiv = (temp & SBI_SSCAUXDIV_FINALDIV2SEL_MASK) >>
4002 SBI_SSCAUXDIV_FINALDIV2SEL_SHIFT;
4003
4004 mutex_unlock(&dev_priv->sb_lock);
4005
4006 desired_divisor = (divsel + 2) * iclk_pi_range + phaseinc;
4007
4008 return DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
4009 desired_divisor << auxdiv);
4010}
4011
275f01b2
DV
4012static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
4013 enum pipe pch_transcoder)
4014{
4015 struct drm_device *dev = crtc->base.dev;
4016 struct drm_i915_private *dev_priv = dev->dev_private;
6e3c9717 4017 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
275f01b2
DV
4018
4019 I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
4020 I915_READ(HTOTAL(cpu_transcoder)));
4021 I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
4022 I915_READ(HBLANK(cpu_transcoder)));
4023 I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
4024 I915_READ(HSYNC(cpu_transcoder)));
4025
4026 I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
4027 I915_READ(VTOTAL(cpu_transcoder)));
4028 I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
4029 I915_READ(VBLANK(cpu_transcoder)));
4030 I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
4031 I915_READ(VSYNC(cpu_transcoder)));
4032 I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
4033 I915_READ(VSYNCSHIFT(cpu_transcoder)));
4034}
4035
003632d9 4036static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
1fbc0d78
DV
4037{
4038 struct drm_i915_private *dev_priv = dev->dev_private;
4039 uint32_t temp;
4040
4041 temp = I915_READ(SOUTH_CHICKEN1);
003632d9 4042 if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
1fbc0d78
DV
4043 return;
4044
4045 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
4046 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
4047
003632d9
ACO
4048 temp &= ~FDI_BC_BIFURCATION_SELECT;
4049 if (enable)
4050 temp |= FDI_BC_BIFURCATION_SELECT;
4051
4052 DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
1fbc0d78
DV
4053 I915_WRITE(SOUTH_CHICKEN1, temp);
4054 POSTING_READ(SOUTH_CHICKEN1);
4055}
4056
4057static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
4058{
4059 struct drm_device *dev = intel_crtc->base.dev;
1fbc0d78
DV
4060
4061 switch (intel_crtc->pipe) {
4062 case PIPE_A:
4063 break;
4064 case PIPE_B:
6e3c9717 4065 if (intel_crtc->config->fdi_lanes > 2)
003632d9 4066 cpt_set_fdi_bc_bifurcation(dev, false);
1fbc0d78 4067 else
003632d9 4068 cpt_set_fdi_bc_bifurcation(dev, true);
1fbc0d78
DV
4069
4070 break;
4071 case PIPE_C:
003632d9 4072 cpt_set_fdi_bc_bifurcation(dev, true);
1fbc0d78
DV
4073
4074 break;
4075 default:
4076 BUG();
4077 }
4078}
4079
c48b5305
VS
4080/* Return which DP Port should be selected for Transcoder DP control */
4081static enum port
4082intel_trans_dp_port_sel(struct drm_crtc *crtc)
4083{
4084 struct drm_device *dev = crtc->dev;
4085 struct intel_encoder *encoder;
4086
4087 for_each_encoder_on_crtc(dev, crtc, encoder) {
4088 if (encoder->type == INTEL_OUTPUT_DISPLAYPORT ||
4089 encoder->type == INTEL_OUTPUT_EDP)
4090 return enc_to_dig_port(&encoder->base)->port;
4091 }
4092
4093 return -1;
4094}
4095
f67a559d
JB
4096/*
4097 * Enable PCH resources required for PCH ports:
4098 * - PCH PLLs
4099 * - FDI training & RX/TX
4100 * - update transcoder timings
4101 * - DP transcoding bits
4102 * - transcoder
4103 */
4104static void ironlake_pch_enable(struct drm_crtc *crtc)
0e23b99d
JB
4105{
4106 struct drm_device *dev = crtc->dev;
4107 struct drm_i915_private *dev_priv = dev->dev_private;
4108 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4109 int pipe = intel_crtc->pipe;
f0f59a00 4110 u32 temp;
2c07245f 4111
ab9412ba 4112 assert_pch_transcoder_disabled(dev_priv, pipe);
e7e164db 4113
1fbc0d78
DV
4114 if (IS_IVYBRIDGE(dev))
4115 ivybridge_update_fdi_bc_bifurcation(intel_crtc);
4116
cd986abb
DV
4117 /* Write the TU size bits before fdi link training, so that error
4118 * detection works. */
4119 I915_WRITE(FDI_RX_TUSIZE1(pipe),
4120 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
4121
c98e9dcf 4122 /* For PCH output, training FDI link */
674cf967 4123 dev_priv->display.fdi_link_train(crtc);
2c07245f 4124
3ad8a208
DV
4125 /* We need to program the right clock selection before writing the pixel
4126 * mutliplier into the DPLL. */
303b81e0 4127 if (HAS_PCH_CPT(dev)) {
ee7b9f93 4128 u32 sel;
4b645f14 4129
c98e9dcf 4130 temp = I915_READ(PCH_DPLL_SEL);
11887397
DV
4131 temp |= TRANS_DPLL_ENABLE(pipe);
4132 sel = TRANS_DPLLB_SEL(pipe);
8106ddbd
ACO
4133 if (intel_crtc->config->shared_dpll ==
4134 intel_get_shared_dpll_by_id(dev_priv, DPLL_ID_PCH_PLL_B))
ee7b9f93
JB
4135 temp |= sel;
4136 else
4137 temp &= ~sel;
c98e9dcf 4138 I915_WRITE(PCH_DPLL_SEL, temp);
c98e9dcf 4139 }
5eddb70b 4140
3ad8a208
DV
4141 /* XXX: pch pll's can be enabled any time before we enable the PCH
4142 * transcoder, and we actually should do this to not upset any PCH
4143 * transcoder that already use the clock when we share it.
4144 *
4145 * Note that enable_shared_dpll tries to do the right thing, but
4146 * get_shared_dpll unconditionally resets the pll - we need that to have
4147 * the right LVDS enable sequence. */
85b3894f 4148 intel_enable_shared_dpll(intel_crtc);
3ad8a208 4149
d9b6cb56
JB
4150 /* set transcoder timing, panel must allow it */
4151 assert_panel_unlocked(dev_priv, pipe);
275f01b2 4152 ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
8db9d77b 4153
303b81e0 4154 intel_fdi_normal_train(crtc);
5e84e1a4 4155
c98e9dcf 4156 /* For PCH DP, enable TRANS_DP_CTL */
6e3c9717 4157 if (HAS_PCH_CPT(dev) && intel_crtc->config->has_dp_encoder) {
9c4edaee
VS
4158 const struct drm_display_mode *adjusted_mode =
4159 &intel_crtc->config->base.adjusted_mode;
dfd07d72 4160 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
f0f59a00 4161 i915_reg_t reg = TRANS_DP_CTL(pipe);
5eddb70b
CW
4162 temp = I915_READ(reg);
4163 temp &= ~(TRANS_DP_PORT_SEL_MASK |
220cad3c
EA
4164 TRANS_DP_SYNC_MASK |
4165 TRANS_DP_BPC_MASK);
e3ef4479 4166 temp |= TRANS_DP_OUTPUT_ENABLE;
9325c9f0 4167 temp |= bpc << 9; /* same format but at 11:9 */
c98e9dcf 4168
9c4edaee 4169 if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
5eddb70b 4170 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
9c4edaee 4171 if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
5eddb70b 4172 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
c98e9dcf
JB
4173
4174 switch (intel_trans_dp_port_sel(crtc)) {
c48b5305 4175 case PORT_B:
5eddb70b 4176 temp |= TRANS_DP_PORT_SEL_B;
c98e9dcf 4177 break;
c48b5305 4178 case PORT_C:
5eddb70b 4179 temp |= TRANS_DP_PORT_SEL_C;
c98e9dcf 4180 break;
c48b5305 4181 case PORT_D:
5eddb70b 4182 temp |= TRANS_DP_PORT_SEL_D;
c98e9dcf
JB
4183 break;
4184 default:
e95d41e1 4185 BUG();
32f9d658 4186 }
2c07245f 4187
5eddb70b 4188 I915_WRITE(reg, temp);
6be4a607 4189 }
b52eb4dc 4190
b8a4f404 4191 ironlake_enable_pch_transcoder(dev_priv, pipe);
f67a559d
JB
4192}
4193
1507e5bd
PZ
4194static void lpt_pch_enable(struct drm_crtc *crtc)
4195{
4196 struct drm_device *dev = crtc->dev;
4197 struct drm_i915_private *dev_priv = dev->dev_private;
4198 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6e3c9717 4199 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
1507e5bd 4200
ab9412ba 4201 assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
1507e5bd 4202
8c52b5e8 4203 lpt_program_iclkip(crtc);
1507e5bd 4204
0540e488 4205 /* Set transcoder timing. */
275f01b2 4206 ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
1507e5bd 4207
937bb610 4208 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
f67a559d
JB
4209}
4210
a1520318 4211static void cpt_verify_modeset(struct drm_device *dev, int pipe)
d4270e57
JB
4212{
4213 struct drm_i915_private *dev_priv = dev->dev_private;
f0f59a00 4214 i915_reg_t dslreg = PIPEDSL(pipe);
d4270e57
JB
4215 u32 temp;
4216
4217 temp = I915_READ(dslreg);
4218 udelay(500);
4219 if (wait_for(I915_READ(dslreg) != temp, 5)) {
d4270e57 4220 if (wait_for(I915_READ(dslreg) != temp, 5))
84f44ce7 4221 DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
d4270e57
JB
4222 }
4223}
4224
86adf9d7
ML
4225static int
4226skl_update_scaler(struct intel_crtc_state *crtc_state, bool force_detach,
4227 unsigned scaler_user, int *scaler_id, unsigned int rotation,
4228 int src_w, int src_h, int dst_w, int dst_h)
a1b2278e 4229{
86adf9d7
ML
4230 struct intel_crtc_scaler_state *scaler_state =
4231 &crtc_state->scaler_state;
4232 struct intel_crtc *intel_crtc =
4233 to_intel_crtc(crtc_state->base.crtc);
a1b2278e 4234 int need_scaling;
6156a456
CK
4235
4236 need_scaling = intel_rotation_90_or_270(rotation) ?
4237 (src_h != dst_w || src_w != dst_h):
4238 (src_w != dst_w || src_h != dst_h);
a1b2278e
CK
4239
4240 /*
4241 * if plane is being disabled or scaler is no more required or force detach
4242 * - free scaler binded to this plane/crtc
4243 * - in order to do this, update crtc->scaler_usage
4244 *
4245 * Here scaler state in crtc_state is set free so that
4246 * scaler can be assigned to other user. Actual register
4247 * update to free the scaler is done in plane/panel-fit programming.
4248 * For this purpose crtc/plane_state->scaler_id isn't reset here.
4249 */
86adf9d7 4250 if (force_detach || !need_scaling) {
a1b2278e 4251 if (*scaler_id >= 0) {
86adf9d7 4252 scaler_state->scaler_users &= ~(1 << scaler_user);
a1b2278e
CK
4253 scaler_state->scalers[*scaler_id].in_use = 0;
4254
86adf9d7
ML
4255 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4256 "Staged freeing scaler id %d scaler_users = 0x%x\n",
4257 intel_crtc->pipe, scaler_user, *scaler_id,
a1b2278e
CK
4258 scaler_state->scaler_users);
4259 *scaler_id = -1;
4260 }
4261 return 0;
4262 }
4263
4264 /* range checks */
4265 if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
4266 dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
4267
4268 src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
4269 dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
86adf9d7 4270 DRM_DEBUG_KMS("scaler_user index %u.%u: src %ux%u dst %ux%u "
a1b2278e 4271 "size is out of scaler range\n",
86adf9d7 4272 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h);
a1b2278e
CK
4273 return -EINVAL;
4274 }
4275
86adf9d7
ML
4276 /* mark this plane as a scaler user in crtc_state */
4277 scaler_state->scaler_users |= (1 << scaler_user);
4278 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4279 "staged scaling request for %ux%u->%ux%u scaler_users = 0x%x\n",
4280 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h,
4281 scaler_state->scaler_users);
4282
4283 return 0;
4284}
4285
4286/**
4287 * skl_update_scaler_crtc - Stages update to scaler state for a given crtc.
4288 *
4289 * @state: crtc's scaler state
86adf9d7
ML
4290 *
4291 * Return
4292 * 0 - scaler_usage updated successfully
4293 * error - requested scaling cannot be supported or other error condition
4294 */
e435d6e5 4295int skl_update_scaler_crtc(struct intel_crtc_state *state)
86adf9d7
ML
4296{
4297 struct intel_crtc *intel_crtc = to_intel_crtc(state->base.crtc);
7c5f93b0 4298 const struct drm_display_mode *adjusted_mode = &state->base.adjusted_mode;
86adf9d7 4299
78108b7c
VS
4300 DRM_DEBUG_KMS("Updating scaler for [CRTC:%d:%s] scaler_user index %u.%u\n",
4301 intel_crtc->base.base.id, intel_crtc->base.name,
4302 intel_crtc->pipe, SKL_CRTC_INDEX);
86adf9d7 4303
e435d6e5 4304 return skl_update_scaler(state, !state->base.active, SKL_CRTC_INDEX,
fa5a7970 4305 &state->scaler_state.scaler_id, BIT(DRM_ROTATE_0),
86adf9d7 4306 state->pipe_src_w, state->pipe_src_h,
aad941d5 4307 adjusted_mode->crtc_hdisplay, adjusted_mode->crtc_vdisplay);
86adf9d7
ML
4308}
4309
4310/**
4311 * skl_update_scaler_plane - Stages update to scaler state for a given plane.
4312 *
4313 * @state: crtc's scaler state
86adf9d7
ML
4314 * @plane_state: atomic plane state to update
4315 *
4316 * Return
4317 * 0 - scaler_usage updated successfully
4318 * error - requested scaling cannot be supported or other error condition
4319 */
da20eabd
ML
4320static int skl_update_scaler_plane(struct intel_crtc_state *crtc_state,
4321 struct intel_plane_state *plane_state)
86adf9d7
ML
4322{
4323
4324 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
da20eabd
ML
4325 struct intel_plane *intel_plane =
4326 to_intel_plane(plane_state->base.plane);
86adf9d7
ML
4327 struct drm_framebuffer *fb = plane_state->base.fb;
4328 int ret;
4329
4330 bool force_detach = !fb || !plane_state->visible;
4331
72660ce0
VS
4332 DRM_DEBUG_KMS("Updating scaler for [PLANE:%d:%s] scaler_user index %u.%u\n",
4333 intel_plane->base.base.id, intel_plane->base.name,
4334 intel_crtc->pipe, drm_plane_index(&intel_plane->base));
86adf9d7
ML
4335
4336 ret = skl_update_scaler(crtc_state, force_detach,
4337 drm_plane_index(&intel_plane->base),
4338 &plane_state->scaler_id,
4339 plane_state->base.rotation,
4340 drm_rect_width(&plane_state->src) >> 16,
4341 drm_rect_height(&plane_state->src) >> 16,
4342 drm_rect_width(&plane_state->dst),
4343 drm_rect_height(&plane_state->dst));
4344
4345 if (ret || plane_state->scaler_id < 0)
4346 return ret;
4347
a1b2278e 4348 /* check colorkey */
818ed961 4349 if (plane_state->ckey.flags != I915_SET_COLORKEY_NONE) {
72660ce0
VS
4350 DRM_DEBUG_KMS("[PLANE:%d:%s] scaling with color key not allowed",
4351 intel_plane->base.base.id,
4352 intel_plane->base.name);
a1b2278e
CK
4353 return -EINVAL;
4354 }
4355
4356 /* Check src format */
86adf9d7
ML
4357 switch (fb->pixel_format) {
4358 case DRM_FORMAT_RGB565:
4359 case DRM_FORMAT_XBGR8888:
4360 case DRM_FORMAT_XRGB8888:
4361 case DRM_FORMAT_ABGR8888:
4362 case DRM_FORMAT_ARGB8888:
4363 case DRM_FORMAT_XRGB2101010:
4364 case DRM_FORMAT_XBGR2101010:
4365 case DRM_FORMAT_YUYV:
4366 case DRM_FORMAT_YVYU:
4367 case DRM_FORMAT_UYVY:
4368 case DRM_FORMAT_VYUY:
4369 break;
4370 default:
72660ce0
VS
4371 DRM_DEBUG_KMS("[PLANE:%d:%s] FB:%d unsupported scaling format 0x%x\n",
4372 intel_plane->base.base.id, intel_plane->base.name,
4373 fb->base.id, fb->pixel_format);
86adf9d7 4374 return -EINVAL;
a1b2278e
CK
4375 }
4376
a1b2278e
CK
4377 return 0;
4378}
4379
e435d6e5
ML
4380static void skylake_scaler_disable(struct intel_crtc *crtc)
4381{
4382 int i;
4383
4384 for (i = 0; i < crtc->num_scalers; i++)
4385 skl_detach_scaler(crtc, i);
4386}
4387
4388static void skylake_pfit_enable(struct intel_crtc *crtc)
bd2e244f
JB
4389{
4390 struct drm_device *dev = crtc->base.dev;
4391 struct drm_i915_private *dev_priv = dev->dev_private;
4392 int pipe = crtc->pipe;
a1b2278e
CK
4393 struct intel_crtc_scaler_state *scaler_state =
4394 &crtc->config->scaler_state;
4395
4396 DRM_DEBUG_KMS("for crtc_state = %p\n", crtc->config);
4397
6e3c9717 4398 if (crtc->config->pch_pfit.enabled) {
a1b2278e
CK
4399 int id;
4400
4401 if (WARN_ON(crtc->config->scaler_state.scaler_id < 0)) {
4402 DRM_ERROR("Requesting pfit without getting a scaler first\n");
4403 return;
4404 }
4405
4406 id = scaler_state->scaler_id;
4407 I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
4408 PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
4409 I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
4410 I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
4411
4412 DRM_DEBUG_KMS("for crtc_state = %p scaler_id = %d\n", crtc->config, id);
bd2e244f
JB
4413 }
4414}
4415
b074cec8
JB
4416static void ironlake_pfit_enable(struct intel_crtc *crtc)
4417{
4418 struct drm_device *dev = crtc->base.dev;
4419 struct drm_i915_private *dev_priv = dev->dev_private;
4420 int pipe = crtc->pipe;
4421
6e3c9717 4422 if (crtc->config->pch_pfit.enabled) {
b074cec8
JB
4423 /* Force use of hard-coded filter coefficients
4424 * as some pre-programmed values are broken,
4425 * e.g. x201.
4426 */
4427 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
4428 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
4429 PF_PIPE_SEL_IVB(pipe));
4430 else
4431 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
6e3c9717
ACO
4432 I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
4433 I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
d4270e57
JB
4434 }
4435}
4436
20bc8673 4437void hsw_enable_ips(struct intel_crtc *crtc)
d77e4531 4438{
cea165c3
VS
4439 struct drm_device *dev = crtc->base.dev;
4440 struct drm_i915_private *dev_priv = dev->dev_private;
d77e4531 4441
6e3c9717 4442 if (!crtc->config->ips_enabled)
d77e4531
PZ
4443 return;
4444
307e4498
ML
4445 /*
4446 * We can only enable IPS after we enable a plane and wait for a vblank
4447 * This function is called from post_plane_update, which is run after
4448 * a vblank wait.
4449 */
cea165c3 4450
d77e4531 4451 assert_plane_enabled(dev_priv, crtc->plane);
cea165c3 4452 if (IS_BROADWELL(dev)) {
2a114cc1
BW
4453 mutex_lock(&dev_priv->rps.hw_lock);
4454 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
4455 mutex_unlock(&dev_priv->rps.hw_lock);
4456 /* Quoting Art Runyan: "its not safe to expect any particular
4457 * value in IPS_CTL bit 31 after enabling IPS through the
e59150dc
JB
4458 * mailbox." Moreover, the mailbox may return a bogus state,
4459 * so we need to just enable it and continue on.
2a114cc1
BW
4460 */
4461 } else {
4462 I915_WRITE(IPS_CTL, IPS_ENABLE);
4463 /* The bit only becomes 1 in the next vblank, so this wait here
4464 * is essentially intel_wait_for_vblank. If we don't have this
4465 * and don't wait for vblanks until the end of crtc_enable, then
4466 * the HW state readout code will complain that the expected
4467 * IPS_CTL value is not the one we read. */
2ec9ba3c
CW
4468 if (intel_wait_for_register(dev_priv,
4469 IPS_CTL, IPS_ENABLE, IPS_ENABLE,
4470 50))
2a114cc1
BW
4471 DRM_ERROR("Timed out waiting for IPS enable\n");
4472 }
d77e4531
PZ
4473}
4474
20bc8673 4475void hsw_disable_ips(struct intel_crtc *crtc)
d77e4531
PZ
4476{
4477 struct drm_device *dev = crtc->base.dev;
4478 struct drm_i915_private *dev_priv = dev->dev_private;
4479
6e3c9717 4480 if (!crtc->config->ips_enabled)
d77e4531
PZ
4481 return;
4482
4483 assert_plane_enabled(dev_priv, crtc->plane);
23d0b130 4484 if (IS_BROADWELL(dev)) {
2a114cc1
BW
4485 mutex_lock(&dev_priv->rps.hw_lock);
4486 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
4487 mutex_unlock(&dev_priv->rps.hw_lock);
23d0b130 4488 /* wait for pcode to finish disabling IPS, which may take up to 42ms */
b85c1ecf
CW
4489 if (intel_wait_for_register(dev_priv,
4490 IPS_CTL, IPS_ENABLE, 0,
4491 42))
23d0b130 4492 DRM_ERROR("Timed out waiting for IPS disable\n");
e59150dc 4493 } else {
2a114cc1 4494 I915_WRITE(IPS_CTL, 0);
e59150dc
JB
4495 POSTING_READ(IPS_CTL);
4496 }
d77e4531
PZ
4497
4498 /* We need to wait for a vblank before we can disable the plane. */
4499 intel_wait_for_vblank(dev, crtc->pipe);
4500}
4501
7cac945f 4502static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
d3eedb1a 4503{
7cac945f 4504 if (intel_crtc->overlay) {
d3eedb1a
VS
4505 struct drm_device *dev = intel_crtc->base.dev;
4506 struct drm_i915_private *dev_priv = dev->dev_private;
4507
4508 mutex_lock(&dev->struct_mutex);
4509 dev_priv->mm.interruptible = false;
4510 (void) intel_overlay_switch_off(intel_crtc->overlay);
4511 dev_priv->mm.interruptible = true;
4512 mutex_unlock(&dev->struct_mutex);
4513 }
4514
4515 /* Let userspace switch the overlay on again. In most cases userspace
4516 * has to recompute where to put it anyway.
4517 */
4518}
4519
87d4300a
ML
4520/**
4521 * intel_post_enable_primary - Perform operations after enabling primary plane
4522 * @crtc: the CRTC whose primary plane was just enabled
4523 *
4524 * Performs potentially sleeping operations that must be done after the primary
4525 * plane is enabled, such as updating FBC and IPS. Note that this may be
4526 * called due to an explicit primary plane update, or due to an implicit
4527 * re-enable that is caused when a sprite plane is updated to no longer
4528 * completely hide the primary plane.
4529 */
4530static void
4531intel_post_enable_primary(struct drm_crtc *crtc)
a5c4d7bc
VS
4532{
4533 struct drm_device *dev = crtc->dev;
87d4300a 4534 struct drm_i915_private *dev_priv = dev->dev_private;
a5c4d7bc
VS
4535 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4536 int pipe = intel_crtc->pipe;
a5c4d7bc 4537
87d4300a
ML
4538 /*
4539 * FIXME IPS should be fine as long as one plane is
4540 * enabled, but in practice it seems to have problems
4541 * when going from primary only to sprite only and vice
4542 * versa.
4543 */
a5c4d7bc
VS
4544 hsw_enable_ips(intel_crtc);
4545
f99d7069 4546 /*
87d4300a
ML
4547 * Gen2 reports pipe underruns whenever all planes are disabled.
4548 * So don't enable underrun reporting before at least some planes
4549 * are enabled.
4550 * FIXME: Need to fix the logic to work when we turn off all planes
4551 * but leave the pipe running.
f99d7069 4552 */
87d4300a
ML
4553 if (IS_GEN2(dev))
4554 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4555
aca7b684
VS
4556 /* Underruns don't always raise interrupts, so check manually. */
4557 intel_check_cpu_fifo_underruns(dev_priv);
4558 intel_check_pch_fifo_underruns(dev_priv);
a5c4d7bc
VS
4559}
4560
2622a081 4561/* FIXME move all this to pre_plane_update() with proper state tracking */
87d4300a
ML
4562static void
4563intel_pre_disable_primary(struct drm_crtc *crtc)
a5c4d7bc
VS
4564{
4565 struct drm_device *dev = crtc->dev;
4566 struct drm_i915_private *dev_priv = dev->dev_private;
4567 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4568 int pipe = intel_crtc->pipe;
a5c4d7bc 4569
87d4300a
ML
4570 /*
4571 * Gen2 reports pipe underruns whenever all planes are disabled.
4572 * So diasble underrun reporting before all the planes get disabled.
4573 * FIXME: Need to fix the logic to work when we turn off all planes
4574 * but leave the pipe running.
4575 */
4576 if (IS_GEN2(dev))
4577 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
a5c4d7bc 4578
2622a081
VS
4579 /*
4580 * FIXME IPS should be fine as long as one plane is
4581 * enabled, but in practice it seems to have problems
4582 * when going from primary only to sprite only and vice
4583 * versa.
4584 */
4585 hsw_disable_ips(intel_crtc);
4586}
4587
4588/* FIXME get rid of this and use pre_plane_update */
4589static void
4590intel_pre_disable_primary_noatomic(struct drm_crtc *crtc)
4591{
4592 struct drm_device *dev = crtc->dev;
4593 struct drm_i915_private *dev_priv = dev->dev_private;
4594 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4595 int pipe = intel_crtc->pipe;
4596
4597 intel_pre_disable_primary(crtc);
4598
87d4300a
ML
4599 /*
4600 * Vblank time updates from the shadow to live plane control register
4601 * are blocked if the memory self-refresh mode is active at that
4602 * moment. So to make sure the plane gets truly disabled, disable
4603 * first the self-refresh mode. The self-refresh enable bit in turn
4604 * will be checked/applied by the HW only at the next frame start
4605 * event which is after the vblank start event, so we need to have a
4606 * wait-for-vblank between disabling the plane and the pipe.
4607 */
262cd2e1 4608 if (HAS_GMCH_DISPLAY(dev)) {
87d4300a 4609 intel_set_memory_cxsr(dev_priv, false);
262cd2e1
VS
4610 dev_priv->wm.vlv.cxsr = false;
4611 intel_wait_for_vblank(dev, pipe);
4612 }
87d4300a
ML
4613}
4614
5a21b665
DV
4615static void intel_post_plane_update(struct intel_crtc_state *old_crtc_state)
4616{
4617 struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
4618 struct drm_atomic_state *old_state = old_crtc_state->base.state;
4619 struct intel_crtc_state *pipe_config =
4620 to_intel_crtc_state(crtc->base.state);
4621 struct drm_device *dev = crtc->base.dev;
4622 struct drm_plane *primary = crtc->base.primary;
4623 struct drm_plane_state *old_pri_state =
4624 drm_atomic_get_existing_plane_state(old_state, primary);
4625
4626 intel_frontbuffer_flip(dev, pipe_config->fb_bits);
4627
4628 crtc->wm.cxsr_allowed = true;
4629
4630 if (pipe_config->update_wm_post && pipe_config->base.active)
4631 intel_update_watermarks(&crtc->base);
4632
4633 if (old_pri_state) {
4634 struct intel_plane_state *primary_state =
4635 to_intel_plane_state(primary->state);
4636 struct intel_plane_state *old_primary_state =
4637 to_intel_plane_state(old_pri_state);
4638
4639 intel_fbc_post_update(crtc);
4640
4641 if (primary_state->visible &&
4642 (needs_modeset(&pipe_config->base) ||
4643 !old_primary_state->visible))
4644 intel_post_enable_primary(&crtc->base);
4645 }
4646}
4647
5c74cd73 4648static void intel_pre_plane_update(struct intel_crtc_state *old_crtc_state)
ac21b225 4649{
5c74cd73 4650 struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
ac21b225 4651 struct drm_device *dev = crtc->base.dev;
eddfcbcd 4652 struct drm_i915_private *dev_priv = dev->dev_private;
ab1d3a0e
ML
4653 struct intel_crtc_state *pipe_config =
4654 to_intel_crtc_state(crtc->base.state);
5c74cd73
ML
4655 struct drm_atomic_state *old_state = old_crtc_state->base.state;
4656 struct drm_plane *primary = crtc->base.primary;
4657 struct drm_plane_state *old_pri_state =
4658 drm_atomic_get_existing_plane_state(old_state, primary);
4659 bool modeset = needs_modeset(&pipe_config->base);
ac21b225 4660
5c74cd73
ML
4661 if (old_pri_state) {
4662 struct intel_plane_state *primary_state =
4663 to_intel_plane_state(primary->state);
4664 struct intel_plane_state *old_primary_state =
4665 to_intel_plane_state(old_pri_state);
4666
faf68d92 4667 intel_fbc_pre_update(crtc, pipe_config, primary_state);
31ae71fc 4668
5c74cd73
ML
4669 if (old_primary_state->visible &&
4670 (modeset || !primary_state->visible))
4671 intel_pre_disable_primary(&crtc->base);
4672 }
852eb00d 4673
a4015f9a 4674 if (pipe_config->disable_cxsr && HAS_GMCH_DISPLAY(dev)) {
852eb00d 4675 crtc->wm.cxsr_allowed = false;
2dfd178d 4676
2622a081
VS
4677 /*
4678 * Vblank time updates from the shadow to live plane control register
4679 * are blocked if the memory self-refresh mode is active at that
4680 * moment. So to make sure the plane gets truly disabled, disable
4681 * first the self-refresh mode. The self-refresh enable bit in turn
4682 * will be checked/applied by the HW only at the next frame start
4683 * event which is after the vblank start event, so we need to have a
4684 * wait-for-vblank between disabling the plane and the pipe.
4685 */
4686 if (old_crtc_state->base.active) {
2dfd178d 4687 intel_set_memory_cxsr(dev_priv, false);
2622a081
VS
4688 dev_priv->wm.vlv.cxsr = false;
4689 intel_wait_for_vblank(dev, crtc->pipe);
4690 }
852eb00d 4691 }
92826fcd 4692
ed4a6a7c
MR
4693 /*
4694 * IVB workaround: must disable low power watermarks for at least
4695 * one frame before enabling scaling. LP watermarks can be re-enabled
4696 * when scaling is disabled.
4697 *
4698 * WaCxSRDisabledForSpriteScaling:ivb
4699 */
4700 if (pipe_config->disable_lp_wm) {
4701 ilk_disable_lp_wm(dev);
4702 intel_wait_for_vblank(dev, crtc->pipe);
4703 }
4704
4705 /*
4706 * If we're doing a modeset, we're done. No need to do any pre-vblank
4707 * watermark programming here.
4708 */
4709 if (needs_modeset(&pipe_config->base))
4710 return;
4711
4712 /*
4713 * For platforms that support atomic watermarks, program the
4714 * 'intermediate' watermarks immediately. On pre-gen9 platforms, these
4715 * will be the intermediate values that are safe for both pre- and
4716 * post- vblank; when vblank happens, the 'active' values will be set
4717 * to the final 'target' values and we'll do this again to get the
4718 * optimal watermarks. For gen9+ platforms, the values we program here
4719 * will be the final target values which will get automatically latched
4720 * at vblank time; no further programming will be necessary.
4721 *
4722 * If a platform hasn't been transitioned to atomic watermarks yet,
4723 * we'll continue to update watermarks the old way, if flags tell
4724 * us to.
4725 */
4726 if (dev_priv->display.initial_watermarks != NULL)
4727 dev_priv->display.initial_watermarks(pipe_config);
caed361d 4728 else if (pipe_config->update_wm_pre)
92826fcd 4729 intel_update_watermarks(&crtc->base);
ac21b225
ML
4730}
4731
d032ffa0 4732static void intel_crtc_disable_planes(struct drm_crtc *crtc, unsigned plane_mask)
87d4300a
ML
4733{
4734 struct drm_device *dev = crtc->dev;
4735 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
d032ffa0 4736 struct drm_plane *p;
87d4300a
ML
4737 int pipe = intel_crtc->pipe;
4738
7cac945f 4739 intel_crtc_dpms_overlay_disable(intel_crtc);
27321ae8 4740
d032ffa0
ML
4741 drm_for_each_plane_mask(p, dev, plane_mask)
4742 to_intel_plane(p)->disable_plane(p, crtc);
f98551ae 4743
f99d7069
DV
4744 /*
4745 * FIXME: Once we grow proper nuclear flip support out of this we need
4746 * to compute the mask of flip planes precisely. For the time being
4747 * consider this a flip to a NULL plane.
4748 */
4749 intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
a5c4d7bc
VS
4750}
4751
f67a559d
JB
4752static void ironlake_crtc_enable(struct drm_crtc *crtc)
4753{
4754 struct drm_device *dev = crtc->dev;
4755 struct drm_i915_private *dev_priv = dev->dev_private;
4756 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 4757 struct intel_encoder *encoder;
f67a559d 4758 int pipe = intel_crtc->pipe;
b95c5321
ML
4759 struct intel_crtc_state *pipe_config =
4760 to_intel_crtc_state(crtc->state);
f67a559d 4761
53d9f4e9 4762 if (WARN_ON(intel_crtc->active))
f67a559d
JB
4763 return;
4764
b2c0593a
VS
4765 /*
4766 * Sometimes spurious CPU pipe underruns happen during FDI
4767 * training, at least with VGA+HDMI cloning. Suppress them.
4768 *
4769 * On ILK we get an occasional spurious CPU pipe underruns
4770 * between eDP port A enable and vdd enable. Also PCH port
4771 * enable seems to result in the occasional CPU pipe underrun.
4772 *
4773 * Spurious PCH underruns also occur during PCH enabling.
4774 */
4775 if (intel_crtc->config->has_pch_encoder || IS_GEN5(dev_priv))
4776 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
81b088ca
VS
4777 if (intel_crtc->config->has_pch_encoder)
4778 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
4779
6e3c9717 4780 if (intel_crtc->config->has_pch_encoder)
b14b1055
DV
4781 intel_prepare_shared_dpll(intel_crtc);
4782
6e3c9717 4783 if (intel_crtc->config->has_dp_encoder)
fe3cd48d 4784 intel_dp_set_m_n(intel_crtc, M1_N1);
29407aab
DV
4785
4786 intel_set_pipe_timings(intel_crtc);
bc58be60 4787 intel_set_pipe_src_size(intel_crtc);
29407aab 4788
6e3c9717 4789 if (intel_crtc->config->has_pch_encoder) {
29407aab 4790 intel_cpu_transcoder_set_m_n(intel_crtc,
6e3c9717 4791 &intel_crtc->config->fdi_m_n, NULL);
29407aab
DV
4792 }
4793
4794 ironlake_set_pipeconf(crtc);
4795
f67a559d 4796 intel_crtc->active = true;
8664281b 4797
f6736a1a 4798 for_each_encoder_on_crtc(dev, crtc, encoder)
952735ee
DV
4799 if (encoder->pre_enable)
4800 encoder->pre_enable(encoder);
f67a559d 4801
6e3c9717 4802 if (intel_crtc->config->has_pch_encoder) {
fff367c7
DV
4803 /* Note: FDI PLL enabling _must_ be done before we enable the
4804 * cpu pipes, hence this is separate from all the other fdi/pch
4805 * enabling. */
88cefb6c 4806 ironlake_fdi_pll_enable(intel_crtc);
46b6f814
DV
4807 } else {
4808 assert_fdi_tx_disabled(dev_priv, pipe);
4809 assert_fdi_rx_disabled(dev_priv, pipe);
4810 }
f67a559d 4811
b074cec8 4812 ironlake_pfit_enable(intel_crtc);
f67a559d 4813
9c54c0dd
JB
4814 /*
4815 * On ILK+ LUT must be loaded before the pipe is running but with
4816 * clocks enabled
4817 */
b95c5321 4818 intel_color_load_luts(&pipe_config->base);
9c54c0dd 4819
1d5bf5d9
ID
4820 if (dev_priv->display.initial_watermarks != NULL)
4821 dev_priv->display.initial_watermarks(intel_crtc->config);
e1fdc473 4822 intel_enable_pipe(intel_crtc);
f67a559d 4823
6e3c9717 4824 if (intel_crtc->config->has_pch_encoder)
f67a559d 4825 ironlake_pch_enable(crtc);
c98e9dcf 4826
f9b61ff6
DV
4827 assert_vblank_disabled(crtc);
4828 drm_crtc_vblank_on(crtc);
4829
fa5c73b1
DV
4830 for_each_encoder_on_crtc(dev, crtc, encoder)
4831 encoder->enable(encoder);
61b77ddd
DV
4832
4833 if (HAS_PCH_CPT(dev))
a1520318 4834 cpt_verify_modeset(dev, intel_crtc->pipe);
37ca8d4c
VS
4835
4836 /* Must wait for vblank to avoid spurious PCH FIFO underruns */
4837 if (intel_crtc->config->has_pch_encoder)
4838 intel_wait_for_vblank(dev, pipe);
b2c0593a 4839 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
37ca8d4c 4840 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
6be4a607
JB
4841}
4842
42db64ef
PZ
4843/* IPS only exists on ULT machines and is tied to pipe A. */
4844static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
4845{
f5adf94e 4846 return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
42db64ef
PZ
4847}
4848
4f771f10
PZ
4849static void haswell_crtc_enable(struct drm_crtc *crtc)
4850{
4851 struct drm_device *dev = crtc->dev;
4852 struct drm_i915_private *dev_priv = dev->dev_private;
4853 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4854 struct intel_encoder *encoder;
99d736a2 4855 int pipe = intel_crtc->pipe, hsw_workaround_pipe;
4d1de975 4856 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
99d736a2
ML
4857 struct intel_crtc_state *pipe_config =
4858 to_intel_crtc_state(crtc->state);
4f771f10 4859
53d9f4e9 4860 if (WARN_ON(intel_crtc->active))
4f771f10
PZ
4861 return;
4862
81b088ca
VS
4863 if (intel_crtc->config->has_pch_encoder)
4864 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
4865 false);
4866
95a7a2ae
ID
4867 for_each_encoder_on_crtc(dev, crtc, encoder)
4868 if (encoder->pre_pll_enable)
4869 encoder->pre_pll_enable(encoder);
4870
8106ddbd 4871 if (intel_crtc->config->shared_dpll)
df8ad70c
DV
4872 intel_enable_shared_dpll(intel_crtc);
4873
6e3c9717 4874 if (intel_crtc->config->has_dp_encoder)
fe3cd48d 4875 intel_dp_set_m_n(intel_crtc, M1_N1);
229fca97 4876
4d1de975
JN
4877 if (!intel_crtc->config->has_dsi_encoder)
4878 intel_set_pipe_timings(intel_crtc);
4879
bc58be60 4880 intel_set_pipe_src_size(intel_crtc);
229fca97 4881
4d1de975
JN
4882 if (cpu_transcoder != TRANSCODER_EDP &&
4883 !transcoder_is_dsi(cpu_transcoder)) {
4884 I915_WRITE(PIPE_MULT(cpu_transcoder),
6e3c9717 4885 intel_crtc->config->pixel_multiplier - 1);
ebb69c95
CT
4886 }
4887
6e3c9717 4888 if (intel_crtc->config->has_pch_encoder) {
229fca97 4889 intel_cpu_transcoder_set_m_n(intel_crtc,
6e3c9717 4890 &intel_crtc->config->fdi_m_n, NULL);
229fca97
DV
4891 }
4892
4d1de975
JN
4893 if (!intel_crtc->config->has_dsi_encoder)
4894 haswell_set_pipeconf(crtc);
4895
391bf048 4896 haswell_set_pipemisc(crtc);
229fca97 4897
b95c5321 4898 intel_color_set_csc(&pipe_config->base);
229fca97 4899
4f771f10 4900 intel_crtc->active = true;
8664281b 4901
6b698516
DV
4902 if (intel_crtc->config->has_pch_encoder)
4903 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
4904 else
4905 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4906
7d4aefd0 4907 for_each_encoder_on_crtc(dev, crtc, encoder) {
4f771f10
PZ
4908 if (encoder->pre_enable)
4909 encoder->pre_enable(encoder);
7d4aefd0 4910 }
4f771f10 4911
d2d65408 4912 if (intel_crtc->config->has_pch_encoder)
4fe9467d 4913 dev_priv->display.fdi_link_train(crtc);
4fe9467d 4914
a65347ba 4915 if (!intel_crtc->config->has_dsi_encoder)
7d4aefd0 4916 intel_ddi_enable_pipe_clock(intel_crtc);
4f771f10 4917
1c132b44 4918 if (INTEL_INFO(dev)->gen >= 9)
e435d6e5 4919 skylake_pfit_enable(intel_crtc);
ff6d9f55 4920 else
1c132b44 4921 ironlake_pfit_enable(intel_crtc);
4f771f10
PZ
4922
4923 /*
4924 * On ILK+ LUT must be loaded before the pipe is running but with
4925 * clocks enabled
4926 */
b95c5321 4927 intel_color_load_luts(&pipe_config->base);
4f771f10 4928
1f544388 4929 intel_ddi_set_pipe_settings(crtc);
a65347ba 4930 if (!intel_crtc->config->has_dsi_encoder)
7d4aefd0 4931 intel_ddi_enable_transcoder_func(crtc);
4f771f10 4932
1d5bf5d9
ID
4933 if (dev_priv->display.initial_watermarks != NULL)
4934 dev_priv->display.initial_watermarks(pipe_config);
4935 else
4936 intel_update_watermarks(crtc);
4d1de975
JN
4937
4938 /* XXX: Do the pipe assertions at the right place for BXT DSI. */
4939 if (!intel_crtc->config->has_dsi_encoder)
4940 intel_enable_pipe(intel_crtc);
42db64ef 4941
6e3c9717 4942 if (intel_crtc->config->has_pch_encoder)
1507e5bd 4943 lpt_pch_enable(crtc);
4f771f10 4944
a65347ba 4945 if (intel_crtc->config->dp_encoder_is_mst)
0e32b39c
DA
4946 intel_ddi_set_vc_payload_alloc(crtc, true);
4947
f9b61ff6
DV
4948 assert_vblank_disabled(crtc);
4949 drm_crtc_vblank_on(crtc);
4950
8807e55b 4951 for_each_encoder_on_crtc(dev, crtc, encoder) {
4f771f10 4952 encoder->enable(encoder);
8807e55b
JN
4953 intel_opregion_notify_encoder(encoder, true);
4954 }
4f771f10 4955
6b698516
DV
4956 if (intel_crtc->config->has_pch_encoder) {
4957 intel_wait_for_vblank(dev, pipe);
4958 intel_wait_for_vblank(dev, pipe);
4959 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
d2d65408
VS
4960 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
4961 true);
6b698516 4962 }
d2d65408 4963
e4916946
PZ
4964 /* If we change the relative order between pipe/planes enabling, we need
4965 * to change the workaround. */
99d736a2
ML
4966 hsw_workaround_pipe = pipe_config->hsw_workaround_pipe;
4967 if (IS_HASWELL(dev) && hsw_workaround_pipe != INVALID_PIPE) {
4968 intel_wait_for_vblank(dev, hsw_workaround_pipe);
4969 intel_wait_for_vblank(dev, hsw_workaround_pipe);
4970 }
4f771f10
PZ
4971}
4972
bfd16b2a 4973static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force)
3f8dce3a
DV
4974{
4975 struct drm_device *dev = crtc->base.dev;
4976 struct drm_i915_private *dev_priv = dev->dev_private;
4977 int pipe = crtc->pipe;
4978
4979 /* To avoid upsetting the power well on haswell only disable the pfit if
4980 * it's in use. The hw state code will make sure we get this right. */
bfd16b2a 4981 if (force || crtc->config->pch_pfit.enabled) {
3f8dce3a
DV
4982 I915_WRITE(PF_CTL(pipe), 0);
4983 I915_WRITE(PF_WIN_POS(pipe), 0);
4984 I915_WRITE(PF_WIN_SZ(pipe), 0);
4985 }
4986}
4987
6be4a607
JB
4988static void ironlake_crtc_disable(struct drm_crtc *crtc)
4989{
4990 struct drm_device *dev = crtc->dev;
4991 struct drm_i915_private *dev_priv = dev->dev_private;
4992 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 4993 struct intel_encoder *encoder;
6be4a607 4994 int pipe = intel_crtc->pipe;
b52eb4dc 4995
b2c0593a
VS
4996 /*
4997 * Sometimes spurious CPU pipe underruns happen when the
4998 * pipe is already disabled, but FDI RX/TX is still enabled.
4999 * Happens at least with VGA+HDMI cloning. Suppress them.
5000 */
5001 if (intel_crtc->config->has_pch_encoder) {
5002 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
37ca8d4c 5003 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
b2c0593a 5004 }
37ca8d4c 5005
ea9d758d
DV
5006 for_each_encoder_on_crtc(dev, crtc, encoder)
5007 encoder->disable(encoder);
5008
f9b61ff6
DV
5009 drm_crtc_vblank_off(crtc);
5010 assert_vblank_disabled(crtc);
5011
575f7ab7 5012 intel_disable_pipe(intel_crtc);
32f9d658 5013
bfd16b2a 5014 ironlake_pfit_disable(intel_crtc, false);
2c07245f 5015
b2c0593a 5016 if (intel_crtc->config->has_pch_encoder)
5a74f70a
VS
5017 ironlake_fdi_disable(crtc);
5018
bf49ec8c
DV
5019 for_each_encoder_on_crtc(dev, crtc, encoder)
5020 if (encoder->post_disable)
5021 encoder->post_disable(encoder);
2c07245f 5022
6e3c9717 5023 if (intel_crtc->config->has_pch_encoder) {
d925c59a 5024 ironlake_disable_pch_transcoder(dev_priv, pipe);
6be4a607 5025
d925c59a 5026 if (HAS_PCH_CPT(dev)) {
f0f59a00
VS
5027 i915_reg_t reg;
5028 u32 temp;
5029
d925c59a
DV
5030 /* disable TRANS_DP_CTL */
5031 reg = TRANS_DP_CTL(pipe);
5032 temp = I915_READ(reg);
5033 temp &= ~(TRANS_DP_OUTPUT_ENABLE |
5034 TRANS_DP_PORT_SEL_MASK);
5035 temp |= TRANS_DP_PORT_SEL_NONE;
5036 I915_WRITE(reg, temp);
5037
5038 /* disable DPLL_SEL */
5039 temp = I915_READ(PCH_DPLL_SEL);
11887397 5040 temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
d925c59a 5041 I915_WRITE(PCH_DPLL_SEL, temp);
9db4a9c7 5042 }
e3421a18 5043
d925c59a
DV
5044 ironlake_fdi_pll_disable(intel_crtc);
5045 }
81b088ca 5046
b2c0593a 5047 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
81b088ca 5048 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
6be4a607 5049}
1b3c7a47 5050
4f771f10 5051static void haswell_crtc_disable(struct drm_crtc *crtc)
ee7b9f93 5052{
4f771f10
PZ
5053 struct drm_device *dev = crtc->dev;
5054 struct drm_i915_private *dev_priv = dev->dev_private;
ee7b9f93 5055 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4f771f10 5056 struct intel_encoder *encoder;
6e3c9717 5057 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
ee7b9f93 5058
d2d65408
VS
5059 if (intel_crtc->config->has_pch_encoder)
5060 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5061 false);
5062
8807e55b
JN
5063 for_each_encoder_on_crtc(dev, crtc, encoder) {
5064 intel_opregion_notify_encoder(encoder, false);
4f771f10 5065 encoder->disable(encoder);
8807e55b 5066 }
4f771f10 5067
f9b61ff6
DV
5068 drm_crtc_vblank_off(crtc);
5069 assert_vblank_disabled(crtc);
5070
4d1de975
JN
5071 /* XXX: Do the pipe assertions at the right place for BXT DSI. */
5072 if (!intel_crtc->config->has_dsi_encoder)
5073 intel_disable_pipe(intel_crtc);
4f771f10 5074
6e3c9717 5075 if (intel_crtc->config->dp_encoder_is_mst)
a4bf214f
VS
5076 intel_ddi_set_vc_payload_alloc(crtc, false);
5077
a65347ba 5078 if (!intel_crtc->config->has_dsi_encoder)
7d4aefd0 5079 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
4f771f10 5080
1c132b44 5081 if (INTEL_INFO(dev)->gen >= 9)
e435d6e5 5082 skylake_scaler_disable(intel_crtc);
ff6d9f55 5083 else
bfd16b2a 5084 ironlake_pfit_disable(intel_crtc, false);
4f771f10 5085
a65347ba 5086 if (!intel_crtc->config->has_dsi_encoder)
7d4aefd0 5087 intel_ddi_disable_pipe_clock(intel_crtc);
4f771f10 5088
97b040aa
ID
5089 for_each_encoder_on_crtc(dev, crtc, encoder)
5090 if (encoder->post_disable)
5091 encoder->post_disable(encoder);
81b088ca 5092
92966a37
VS
5093 if (intel_crtc->config->has_pch_encoder) {
5094 lpt_disable_pch_transcoder(dev_priv);
503a74e9 5095 lpt_disable_iclkip(dev_priv);
92966a37
VS
5096 intel_ddi_fdi_disable(crtc);
5097
81b088ca
VS
5098 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5099 true);
92966a37 5100 }
4f771f10
PZ
5101}
5102
2dd24552
JB
5103static void i9xx_pfit_enable(struct intel_crtc *crtc)
5104{
5105 struct drm_device *dev = crtc->base.dev;
5106 struct drm_i915_private *dev_priv = dev->dev_private;
6e3c9717 5107 struct intel_crtc_state *pipe_config = crtc->config;
2dd24552 5108
681a8504 5109 if (!pipe_config->gmch_pfit.control)
2dd24552
JB
5110 return;
5111
2dd24552 5112 /*
c0b03411
DV
5113 * The panel fitter should only be adjusted whilst the pipe is disabled,
5114 * according to register description and PRM.
2dd24552 5115 */
c0b03411
DV
5116 WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
5117 assert_pipe_disabled(dev_priv, crtc->pipe);
2dd24552 5118
b074cec8
JB
5119 I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
5120 I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
5a80c45c
DV
5121
5122 /* Border color in case we don't scale up to the full screen. Black by
5123 * default, change to something else for debugging. */
5124 I915_WRITE(BCLRPAT(crtc->pipe), 0);
2dd24552
JB
5125}
5126
d05410f9
DA
5127static enum intel_display_power_domain port_to_power_domain(enum port port)
5128{
5129 switch (port) {
5130 case PORT_A:
6331a704 5131 return POWER_DOMAIN_PORT_DDI_A_LANES;
d05410f9 5132 case PORT_B:
6331a704 5133 return POWER_DOMAIN_PORT_DDI_B_LANES;
d05410f9 5134 case PORT_C:
6331a704 5135 return POWER_DOMAIN_PORT_DDI_C_LANES;
d05410f9 5136 case PORT_D:
6331a704 5137 return POWER_DOMAIN_PORT_DDI_D_LANES;
d8e19f99 5138 case PORT_E:
6331a704 5139 return POWER_DOMAIN_PORT_DDI_E_LANES;
d05410f9 5140 default:
b9fec167 5141 MISSING_CASE(port);
d05410f9
DA
5142 return POWER_DOMAIN_PORT_OTHER;
5143 }
5144}
5145
25f78f58
VS
5146static enum intel_display_power_domain port_to_aux_power_domain(enum port port)
5147{
5148 switch (port) {
5149 case PORT_A:
5150 return POWER_DOMAIN_AUX_A;
5151 case PORT_B:
5152 return POWER_DOMAIN_AUX_B;
5153 case PORT_C:
5154 return POWER_DOMAIN_AUX_C;
5155 case PORT_D:
5156 return POWER_DOMAIN_AUX_D;
5157 case PORT_E:
5158 /* FIXME: Check VBT for actual wiring of PORT E */
5159 return POWER_DOMAIN_AUX_D;
5160 default:
b9fec167 5161 MISSING_CASE(port);
25f78f58
VS
5162 return POWER_DOMAIN_AUX_A;
5163 }
5164}
5165
319be8ae
ID
5166enum intel_display_power_domain
5167intel_display_port_power_domain(struct intel_encoder *intel_encoder)
5168{
5169 struct drm_device *dev = intel_encoder->base.dev;
5170 struct intel_digital_port *intel_dig_port;
5171
5172 switch (intel_encoder->type) {
5173 case INTEL_OUTPUT_UNKNOWN:
5174 /* Only DDI platforms should ever use this output type */
5175 WARN_ON_ONCE(!HAS_DDI(dev));
5176 case INTEL_OUTPUT_DISPLAYPORT:
5177 case INTEL_OUTPUT_HDMI:
5178 case INTEL_OUTPUT_EDP:
5179 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
d05410f9 5180 return port_to_power_domain(intel_dig_port->port);
0e32b39c
DA
5181 case INTEL_OUTPUT_DP_MST:
5182 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
5183 return port_to_power_domain(intel_dig_port->port);
319be8ae
ID
5184 case INTEL_OUTPUT_ANALOG:
5185 return POWER_DOMAIN_PORT_CRT;
5186 case INTEL_OUTPUT_DSI:
5187 return POWER_DOMAIN_PORT_DSI;
5188 default:
5189 return POWER_DOMAIN_PORT_OTHER;
5190 }
5191}
5192
25f78f58
VS
5193enum intel_display_power_domain
5194intel_display_port_aux_power_domain(struct intel_encoder *intel_encoder)
5195{
5196 struct drm_device *dev = intel_encoder->base.dev;
5197 struct intel_digital_port *intel_dig_port;
5198
5199 switch (intel_encoder->type) {
5200 case INTEL_OUTPUT_UNKNOWN:
651174a4
ID
5201 case INTEL_OUTPUT_HDMI:
5202 /*
5203 * Only DDI platforms should ever use these output types.
5204 * We can get here after the HDMI detect code has already set
5205 * the type of the shared encoder. Since we can't be sure
5206 * what's the status of the given connectors, play safe and
5207 * run the DP detection too.
5208 */
25f78f58
VS
5209 WARN_ON_ONCE(!HAS_DDI(dev));
5210 case INTEL_OUTPUT_DISPLAYPORT:
5211 case INTEL_OUTPUT_EDP:
5212 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
5213 return port_to_aux_power_domain(intel_dig_port->port);
5214 case INTEL_OUTPUT_DP_MST:
5215 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
5216 return port_to_aux_power_domain(intel_dig_port->port);
5217 default:
b9fec167 5218 MISSING_CASE(intel_encoder->type);
25f78f58
VS
5219 return POWER_DOMAIN_AUX_A;
5220 }
5221}
5222
74bff5f9
ML
5223static unsigned long get_crtc_power_domains(struct drm_crtc *crtc,
5224 struct intel_crtc_state *crtc_state)
77d22dca 5225{
319be8ae 5226 struct drm_device *dev = crtc->dev;
74bff5f9 5227 struct drm_encoder *encoder;
319be8ae
ID
5228 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5229 enum pipe pipe = intel_crtc->pipe;
77d22dca 5230 unsigned long mask;
74bff5f9 5231 enum transcoder transcoder = crtc_state->cpu_transcoder;
77d22dca 5232
74bff5f9 5233 if (!crtc_state->base.active)
292b990e
ML
5234 return 0;
5235
77d22dca
ID
5236 mask = BIT(POWER_DOMAIN_PIPE(pipe));
5237 mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
74bff5f9
ML
5238 if (crtc_state->pch_pfit.enabled ||
5239 crtc_state->pch_pfit.force_thru)
77d22dca
ID
5240 mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
5241
74bff5f9
ML
5242 drm_for_each_encoder_mask(encoder, dev, crtc_state->base.encoder_mask) {
5243 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
5244
319be8ae 5245 mask |= BIT(intel_display_port_power_domain(intel_encoder));
74bff5f9 5246 }
319be8ae 5247
15e7ec29
ML
5248 if (crtc_state->shared_dpll)
5249 mask |= BIT(POWER_DOMAIN_PLLS);
5250
77d22dca
ID
5251 return mask;
5252}
5253
74bff5f9
ML
5254static unsigned long
5255modeset_get_crtc_power_domains(struct drm_crtc *crtc,
5256 struct intel_crtc_state *crtc_state)
77d22dca 5257{
292b990e
ML
5258 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
5259 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5260 enum intel_display_power_domain domain;
5a21b665 5261 unsigned long domains, new_domains, old_domains;
77d22dca 5262
292b990e 5263 old_domains = intel_crtc->enabled_power_domains;
74bff5f9
ML
5264 intel_crtc->enabled_power_domains = new_domains =
5265 get_crtc_power_domains(crtc, crtc_state);
77d22dca 5266
5a21b665 5267 domains = new_domains & ~old_domains;
292b990e
ML
5268
5269 for_each_power_domain(domain, domains)
5270 intel_display_power_get(dev_priv, domain);
5271
5a21b665 5272 return old_domains & ~new_domains;
292b990e
ML
5273}
5274
5275static void modeset_put_power_domains(struct drm_i915_private *dev_priv,
5276 unsigned long domains)
5277{
5278 enum intel_display_power_domain domain;
5279
5280 for_each_power_domain(domain, domains)
5281 intel_display_power_put(dev_priv, domain);
5282}
77d22dca 5283
adafdc6f
MK
5284static int intel_compute_max_dotclk(struct drm_i915_private *dev_priv)
5285{
5286 int max_cdclk_freq = dev_priv->max_cdclk_freq;
5287
5288 if (INTEL_INFO(dev_priv)->gen >= 9 ||
5289 IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
5290 return max_cdclk_freq;
5291 else if (IS_CHERRYVIEW(dev_priv))
5292 return max_cdclk_freq*95/100;
5293 else if (INTEL_INFO(dev_priv)->gen < 4)
5294 return 2*max_cdclk_freq*90/100;
5295 else
5296 return max_cdclk_freq*90/100;
5297}
5298
b2045352
VS
5299static int skl_calc_cdclk(int max_pixclk, int vco);
5300
560a7ae4
DL
5301static void intel_update_max_cdclk(struct drm_device *dev)
5302{
5303 struct drm_i915_private *dev_priv = dev->dev_private;
5304
ef11bdb3 5305 if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) {
560a7ae4 5306 u32 limit = I915_READ(SKL_DFSM) & SKL_DFSM_CDCLK_LIMIT_MASK;
b2045352
VS
5307 int max_cdclk, vco;
5308
5309 vco = dev_priv->skl_preferred_vco_freq;
63911d72 5310 WARN_ON(vco != 8100000 && vco != 8640000);
560a7ae4 5311
b2045352
VS
5312 /*
5313 * Use the lower (vco 8640) cdclk values as a
5314 * first guess. skl_calc_cdclk() will correct it
5315 * if the preferred vco is 8100 instead.
5316 */
560a7ae4 5317 if (limit == SKL_DFSM_CDCLK_LIMIT_675)
487ed2e4 5318 max_cdclk = 617143;
560a7ae4 5319 else if (limit == SKL_DFSM_CDCLK_LIMIT_540)
b2045352 5320 max_cdclk = 540000;
560a7ae4 5321 else if (limit == SKL_DFSM_CDCLK_LIMIT_450)
b2045352 5322 max_cdclk = 432000;
560a7ae4 5323 else
487ed2e4 5324 max_cdclk = 308571;
b2045352
VS
5325
5326 dev_priv->max_cdclk_freq = skl_calc_cdclk(max_cdclk, vco);
281c114f
MR
5327 } else if (IS_BROXTON(dev)) {
5328 dev_priv->max_cdclk_freq = 624000;
560a7ae4
DL
5329 } else if (IS_BROADWELL(dev)) {
5330 /*
5331 * FIXME with extra cooling we can allow
5332 * 540 MHz for ULX and 675 Mhz for ULT.
5333 * How can we know if extra cooling is
5334 * available? PCI ID, VTB, something else?
5335 */
5336 if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
5337 dev_priv->max_cdclk_freq = 450000;
5338 else if (IS_BDW_ULX(dev))
5339 dev_priv->max_cdclk_freq = 450000;
5340 else if (IS_BDW_ULT(dev))
5341 dev_priv->max_cdclk_freq = 540000;
5342 else
5343 dev_priv->max_cdclk_freq = 675000;
0904deaf
MK
5344 } else if (IS_CHERRYVIEW(dev)) {
5345 dev_priv->max_cdclk_freq = 320000;
560a7ae4
DL
5346 } else if (IS_VALLEYVIEW(dev)) {
5347 dev_priv->max_cdclk_freq = 400000;
5348 } else {
5349 /* otherwise assume cdclk is fixed */
5350 dev_priv->max_cdclk_freq = dev_priv->cdclk_freq;
5351 }
5352
adafdc6f
MK
5353 dev_priv->max_dotclk_freq = intel_compute_max_dotclk(dev_priv);
5354
560a7ae4
DL
5355 DRM_DEBUG_DRIVER("Max CD clock rate: %d kHz\n",
5356 dev_priv->max_cdclk_freq);
adafdc6f
MK
5357
5358 DRM_DEBUG_DRIVER("Max dotclock rate: %d kHz\n",
5359 dev_priv->max_dotclk_freq);
560a7ae4
DL
5360}
5361
5362static void intel_update_cdclk(struct drm_device *dev)
5363{
5364 struct drm_i915_private *dev_priv = dev->dev_private;
5365
5366 dev_priv->cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
2f2a121a 5367
83d7c81f 5368 if (INTEL_GEN(dev_priv) >= 9)
709e05c3
VS
5369 DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz, VCO: %d kHz, ref: %d kHz\n",
5370 dev_priv->cdclk_freq, dev_priv->cdclk_pll.vco,
5371 dev_priv->cdclk_pll.ref);
2f2a121a
VS
5372 else
5373 DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
5374 dev_priv->cdclk_freq);
560a7ae4
DL
5375
5376 /*
b5d99ff9
VS
5377 * 9:0 CMBUS [sic] CDCLK frequency (cdfreq):
5378 * Programmng [sic] note: bit[9:2] should be programmed to the number
5379 * of cdclk that generates 4MHz reference clock freq which is used to
5380 * generate GMBus clock. This will vary with the cdclk freq.
560a7ae4 5381 */
b5d99ff9 5382 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
560a7ae4 5383 I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->cdclk_freq, 1000));
560a7ae4
DL
5384}
5385
92891e45
VS
5386/* convert from kHz to .1 fixpoint MHz with -1MHz offset */
5387static int skl_cdclk_decimal(int cdclk)
5388{
5389 return DIV_ROUND_CLOSEST(cdclk - 1000, 500);
5390}
5391
5f199dfa
VS
5392static int bxt_de_pll_vco(struct drm_i915_private *dev_priv, int cdclk)
5393{
5394 int ratio;
5395
5396 if (cdclk == dev_priv->cdclk_pll.ref)
5397 return 0;
5398
5399 switch (cdclk) {
5400 default:
5401 MISSING_CASE(cdclk);
5402 case 144000:
5403 case 288000:
5404 case 384000:
5405 case 576000:
5406 ratio = 60;
5407 break;
5408 case 624000:
5409 ratio = 65;
5410 break;
5411 }
5412
5413 return dev_priv->cdclk_pll.ref * ratio;
5414}
5415
2b73001e
VS
5416static void bxt_de_pll_disable(struct drm_i915_private *dev_priv)
5417{
5418 I915_WRITE(BXT_DE_PLL_ENABLE, 0);
5419
5420 /* Timeout 200us */
95cac283
CW
5421 if (intel_wait_for_register(dev_priv,
5422 BXT_DE_PLL_ENABLE, BXT_DE_PLL_LOCK, 0,
5423 1))
2b73001e 5424 DRM_ERROR("timeout waiting for DE PLL unlock\n");
83d7c81f
VS
5425
5426 dev_priv->cdclk_pll.vco = 0;
2b73001e
VS
5427}
5428
5f199dfa 5429static void bxt_de_pll_enable(struct drm_i915_private *dev_priv, int vco)
2b73001e 5430{
5f199dfa 5431 int ratio = DIV_ROUND_CLOSEST(vco, dev_priv->cdclk_pll.ref);
2b73001e
VS
5432 u32 val;
5433
5434 val = I915_READ(BXT_DE_PLL_CTL);
5435 val &= ~BXT_DE_PLL_RATIO_MASK;
5f199dfa 5436 val |= BXT_DE_PLL_RATIO(ratio);
2b73001e
VS
5437 I915_WRITE(BXT_DE_PLL_CTL, val);
5438
5439 I915_WRITE(BXT_DE_PLL_ENABLE, BXT_DE_PLL_PLL_ENABLE);
5440
5441 /* Timeout 200us */
e084e1b9
CW
5442 if (intel_wait_for_register(dev_priv,
5443 BXT_DE_PLL_ENABLE,
5444 BXT_DE_PLL_LOCK,
5445 BXT_DE_PLL_LOCK,
5446 1))
2b73001e 5447 DRM_ERROR("timeout waiting for DE PLL lock\n");
83d7c81f 5448
5f199dfa 5449 dev_priv->cdclk_pll.vco = vco;
2b73001e
VS
5450}
5451
324513c0 5452static void bxt_set_cdclk(struct drm_i915_private *dev_priv, int cdclk)
f8437dd1 5453{
5f199dfa
VS
5454 u32 val, divider;
5455 int vco, ret;
f8437dd1 5456
5f199dfa
VS
5457 vco = bxt_de_pll_vco(dev_priv, cdclk);
5458
5459 DRM_DEBUG_DRIVER("Changing CDCLK to %d kHz (VCO %d kHz)\n", cdclk, vco);
5460
5461 /* cdclk = vco / 2 / div{1,1.5,2,4} */
5462 switch (DIV_ROUND_CLOSEST(vco, cdclk)) {
5463 case 8:
f8437dd1 5464 divider = BXT_CDCLK_CD2X_DIV_SEL_4;
f8437dd1 5465 break;
5f199dfa 5466 case 4:
f8437dd1 5467 divider = BXT_CDCLK_CD2X_DIV_SEL_2;
f8437dd1 5468 break;
5f199dfa 5469 case 3:
f8437dd1 5470 divider = BXT_CDCLK_CD2X_DIV_SEL_1_5;
f8437dd1 5471 break;
5f199dfa 5472 case 2:
f8437dd1 5473 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
f8437dd1
VK
5474 break;
5475 default:
5f199dfa
VS
5476 WARN_ON(cdclk != dev_priv->cdclk_pll.ref);
5477 WARN_ON(vco != 0);
f8437dd1 5478
5f199dfa
VS
5479 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
5480 break;
f8437dd1
VK
5481 }
5482
f8437dd1 5483 /* Inform power controller of upcoming frequency change */
5f199dfa 5484 mutex_lock(&dev_priv->rps.hw_lock);
f8437dd1
VK
5485 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
5486 0x80000000);
5487 mutex_unlock(&dev_priv->rps.hw_lock);
5488
5489 if (ret) {
5490 DRM_ERROR("PCode CDCLK freq change notify failed (err %d, freq %d)\n",
9ef56154 5491 ret, cdclk);
f8437dd1
VK
5492 return;
5493 }
5494
5f199dfa
VS
5495 if (dev_priv->cdclk_pll.vco != 0 &&
5496 dev_priv->cdclk_pll.vco != vco)
2b73001e 5497 bxt_de_pll_disable(dev_priv);
f8437dd1 5498
5f199dfa
VS
5499 if (dev_priv->cdclk_pll.vco != vco)
5500 bxt_de_pll_enable(dev_priv, vco);
f8437dd1 5501
5f199dfa
VS
5502 val = divider | skl_cdclk_decimal(cdclk);
5503 /*
5504 * FIXME if only the cd2x divider needs changing, it could be done
5505 * without shutting off the pipe (if only one pipe is active).
5506 */
5507 val |= BXT_CDCLK_CD2X_PIPE_NONE;
5508 /*
5509 * Disable SSA Precharge when CD clock frequency < 500 MHz,
5510 * enable otherwise.
5511 */
5512 if (cdclk >= 500000)
5513 val |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
5514 I915_WRITE(CDCLK_CTL, val);
f8437dd1
VK
5515
5516 mutex_lock(&dev_priv->rps.hw_lock);
5517 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
9ef56154 5518 DIV_ROUND_UP(cdclk, 25000));
f8437dd1
VK
5519 mutex_unlock(&dev_priv->rps.hw_lock);
5520
5521 if (ret) {
5522 DRM_ERROR("PCode CDCLK freq set failed, (err %d, freq %d)\n",
9ef56154 5523 ret, cdclk);
f8437dd1
VK
5524 return;
5525 }
5526
c6c4696f 5527 intel_update_cdclk(dev_priv->dev);
f8437dd1
VK
5528}
5529
d66a2194 5530static void bxt_sanitize_cdclk(struct drm_i915_private *dev_priv)
f8437dd1 5531{
d66a2194
ID
5532 u32 cdctl, expected;
5533
089c6fd5 5534 intel_update_cdclk(dev_priv->dev);
f8437dd1 5535
d66a2194
ID
5536 if (dev_priv->cdclk_pll.vco == 0 ||
5537 dev_priv->cdclk_freq == dev_priv->cdclk_pll.ref)
5538 goto sanitize;
5539
5540 /* DPLL okay; verify the cdclock
5541 *
5542 * Some BIOS versions leave an incorrect decimal frequency value and
5543 * set reserved MBZ bits in CDCLK_CTL at least during exiting from S4,
5544 * so sanitize this register.
5545 */
5546 cdctl = I915_READ(CDCLK_CTL);
5547 /*
5548 * Let's ignore the pipe field, since BIOS could have configured the
5549 * dividers both synching to an active pipe, or asynchronously
5550 * (PIPE_NONE).
5551 */
5552 cdctl &= ~BXT_CDCLK_CD2X_PIPE_NONE;
5553
5554 expected = (cdctl & BXT_CDCLK_CD2X_DIV_SEL_MASK) |
5555 skl_cdclk_decimal(dev_priv->cdclk_freq);
5556 /*
5557 * Disable SSA Precharge when CD clock frequency < 500 MHz,
5558 * enable otherwise.
5559 */
5560 if (dev_priv->cdclk_freq >= 500000)
5561 expected |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
5562
5563 if (cdctl == expected)
5564 /* All well; nothing to sanitize */
5565 return;
5566
5567sanitize:
5568 DRM_DEBUG_KMS("Sanitizing cdclk programmed by pre-os\n");
5569
5570 /* force cdclk programming */
5571 dev_priv->cdclk_freq = 0;
5572
5573 /* force full PLL disable + enable */
5574 dev_priv->cdclk_pll.vco = -1;
5575}
5576
324513c0 5577void bxt_init_cdclk(struct drm_i915_private *dev_priv)
d66a2194
ID
5578{
5579 bxt_sanitize_cdclk(dev_priv);
5580
5581 if (dev_priv->cdclk_freq != 0 && dev_priv->cdclk_pll.vco != 0)
089c6fd5 5582 return;
c2e001ef 5583
f8437dd1
VK
5584 /*
5585 * FIXME:
5586 * - The initial CDCLK needs to be read from VBT.
5587 * Need to make this change after VBT has changes for BXT.
f8437dd1 5588 */
324513c0 5589 bxt_set_cdclk(dev_priv, bxt_calc_cdclk(0));
f8437dd1
VK
5590}
5591
324513c0 5592void bxt_uninit_cdclk(struct drm_i915_private *dev_priv)
f8437dd1 5593{
324513c0 5594 bxt_set_cdclk(dev_priv, dev_priv->cdclk_pll.ref);
f8437dd1
VK
5595}
5596
a8ca4934
VS
5597static int skl_calc_cdclk(int max_pixclk, int vco)
5598{
63911d72 5599 if (vco == 8640000) {
a8ca4934 5600 if (max_pixclk > 540000)
487ed2e4 5601 return 617143;
a8ca4934
VS
5602 else if (max_pixclk > 432000)
5603 return 540000;
487ed2e4 5604 else if (max_pixclk > 308571)
a8ca4934
VS
5605 return 432000;
5606 else
487ed2e4 5607 return 308571;
a8ca4934 5608 } else {
a8ca4934
VS
5609 if (max_pixclk > 540000)
5610 return 675000;
5611 else if (max_pixclk > 450000)
5612 return 540000;
5613 else if (max_pixclk > 337500)
5614 return 450000;
5615 else
5616 return 337500;
5617 }
5618}
5619
ea61791e
VS
5620static void
5621skl_dpll0_update(struct drm_i915_private *dev_priv)
5d96d8af 5622{
ea61791e 5623 u32 val;
5d96d8af 5624
709e05c3 5625 dev_priv->cdclk_pll.ref = 24000;
1c3f7700 5626 dev_priv->cdclk_pll.vco = 0;
709e05c3 5627
ea61791e 5628 val = I915_READ(LCPLL1_CTL);
1c3f7700 5629 if ((val & LCPLL_PLL_ENABLE) == 0)
ea61791e 5630 return;
5d96d8af 5631
1c3f7700
ID
5632 if (WARN_ON((val & LCPLL_PLL_LOCK) == 0))
5633 return;
9f7eb31a 5634
ea61791e
VS
5635 val = I915_READ(DPLL_CTRL1);
5636
1c3f7700
ID
5637 if (WARN_ON((val & (DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) |
5638 DPLL_CTRL1_SSC(SKL_DPLL0) |
5639 DPLL_CTRL1_OVERRIDE(SKL_DPLL0))) !=
5640 DPLL_CTRL1_OVERRIDE(SKL_DPLL0)))
5641 return;
9f7eb31a 5642
ea61791e
VS
5643 switch (val & DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0)) {
5644 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810, SKL_DPLL0):
5645 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1350, SKL_DPLL0):
5646 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1620, SKL_DPLL0):
5647 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_2700, SKL_DPLL0):
63911d72 5648 dev_priv->cdclk_pll.vco = 8100000;
ea61791e
VS
5649 break;
5650 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080, SKL_DPLL0):
5651 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_2160, SKL_DPLL0):
63911d72 5652 dev_priv->cdclk_pll.vco = 8640000;
ea61791e
VS
5653 break;
5654 default:
5655 MISSING_CASE(val & DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
ea61791e
VS
5656 break;
5657 }
5d96d8af
DL
5658}
5659
b2045352
VS
5660void skl_set_preferred_cdclk_vco(struct drm_i915_private *dev_priv, int vco)
5661{
5662 bool changed = dev_priv->skl_preferred_vco_freq != vco;
5663
5664 dev_priv->skl_preferred_vco_freq = vco;
5665
5666 if (changed)
5667 intel_update_max_cdclk(dev_priv->dev);
5668}
5669
5d96d8af 5670static void
3861fc60 5671skl_dpll0_enable(struct drm_i915_private *dev_priv, int vco)
5d96d8af 5672{
a8ca4934 5673 int min_cdclk = skl_calc_cdclk(0, vco);
5d96d8af
DL
5674 u32 val;
5675
63911d72 5676 WARN_ON(vco != 8100000 && vco != 8640000);
b2045352 5677
5d96d8af 5678 /* select the minimum CDCLK before enabling DPLL 0 */
9ef56154 5679 val = CDCLK_FREQ_337_308 | skl_cdclk_decimal(min_cdclk);
5d96d8af
DL
5680 I915_WRITE(CDCLK_CTL, val);
5681 POSTING_READ(CDCLK_CTL);
5682
5683 /*
5684 * We always enable DPLL0 with the lowest link rate possible, but still
5685 * taking into account the VCO required to operate the eDP panel at the
5686 * desired frequency. The usual DP link rates operate with a VCO of
5687 * 8100 while the eDP 1.4 alternate link rates need a VCO of 8640.
5688 * The modeset code is responsible for the selection of the exact link
5689 * rate later on, with the constraint of choosing a frequency that
a8ca4934 5690 * works with vco.
5d96d8af
DL
5691 */
5692 val = I915_READ(DPLL_CTRL1);
5693
5694 val &= ~(DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) | DPLL_CTRL1_SSC(SKL_DPLL0) |
5695 DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
5696 val |= DPLL_CTRL1_OVERRIDE(SKL_DPLL0);
63911d72 5697 if (vco == 8640000)
5d96d8af
DL
5698 val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080,
5699 SKL_DPLL0);
5700 else
5701 val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810,
5702 SKL_DPLL0);
5703
5704 I915_WRITE(DPLL_CTRL1, val);
5705 POSTING_READ(DPLL_CTRL1);
5706
5707 I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) | LCPLL_PLL_ENABLE);
5708
5709 if (wait_for(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK, 5))
5710 DRM_ERROR("DPLL0 not locked\n");
1cd593e0 5711
63911d72 5712 dev_priv->cdclk_pll.vco = vco;
b2045352
VS
5713
5714 /* We'll want to keep using the current vco from now on. */
5715 skl_set_preferred_cdclk_vco(dev_priv, vco);
5d96d8af
DL
5716}
5717
430e05de
VS
5718static void
5719skl_dpll0_disable(struct drm_i915_private *dev_priv)
5720{
5721 I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) & ~LCPLL_PLL_ENABLE);
5722 if (wait_for(!(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK), 1))
5723 DRM_ERROR("Couldn't disable DPLL0\n");
1cd593e0 5724
63911d72 5725 dev_priv->cdclk_pll.vco = 0;
430e05de
VS
5726}
5727
5d96d8af
DL
5728static bool skl_cdclk_pcu_ready(struct drm_i915_private *dev_priv)
5729{
5730 int ret;
5731 u32 val;
5732
5733 /* inform PCU we want to change CDCLK */
5734 val = SKL_CDCLK_PREPARE_FOR_CHANGE;
5735 mutex_lock(&dev_priv->rps.hw_lock);
5736 ret = sandybridge_pcode_read(dev_priv, SKL_PCODE_CDCLK_CONTROL, &val);
5737 mutex_unlock(&dev_priv->rps.hw_lock);
5738
5739 return ret == 0 && (val & SKL_CDCLK_READY_FOR_CHANGE);
5740}
5741
5742static bool skl_cdclk_wait_for_pcu_ready(struct drm_i915_private *dev_priv)
5743{
5744 unsigned int i;
5745
5746 for (i = 0; i < 15; i++) {
5747 if (skl_cdclk_pcu_ready(dev_priv))
5748 return true;
5749 udelay(10);
5750 }
5751
5752 return false;
5753}
5754
1cd593e0 5755static void skl_set_cdclk(struct drm_i915_private *dev_priv, int cdclk, int vco)
5d96d8af 5756{
560a7ae4 5757 struct drm_device *dev = dev_priv->dev;
5d96d8af
DL
5758 u32 freq_select, pcu_ack;
5759
1cd593e0
VS
5760 WARN_ON((cdclk == 24000) != (vco == 0));
5761
63911d72 5762 DRM_DEBUG_DRIVER("Changing CDCLK to %d kHz (VCO %d kHz)\n", cdclk, vco);
5d96d8af
DL
5763
5764 if (!skl_cdclk_wait_for_pcu_ready(dev_priv)) {
5765 DRM_ERROR("failed to inform PCU about cdclk change\n");
5766 return;
5767 }
5768
5769 /* set CDCLK_CTL */
9ef56154 5770 switch (cdclk) {
5d96d8af
DL
5771 case 450000:
5772 case 432000:
5773 freq_select = CDCLK_FREQ_450_432;
5774 pcu_ack = 1;
5775 break;
5776 case 540000:
5777 freq_select = CDCLK_FREQ_540;
5778 pcu_ack = 2;
5779 break;
487ed2e4 5780 case 308571:
5d96d8af
DL
5781 case 337500:
5782 default:
5783 freq_select = CDCLK_FREQ_337_308;
5784 pcu_ack = 0;
5785 break;
487ed2e4 5786 case 617143:
5d96d8af
DL
5787 case 675000:
5788 freq_select = CDCLK_FREQ_675_617;
5789 pcu_ack = 3;
5790 break;
5791 }
5792
63911d72
VS
5793 if (dev_priv->cdclk_pll.vco != 0 &&
5794 dev_priv->cdclk_pll.vco != vco)
1cd593e0
VS
5795 skl_dpll0_disable(dev_priv);
5796
63911d72 5797 if (dev_priv->cdclk_pll.vco != vco)
1cd593e0
VS
5798 skl_dpll0_enable(dev_priv, vco);
5799
9ef56154 5800 I915_WRITE(CDCLK_CTL, freq_select | skl_cdclk_decimal(cdclk));
5d96d8af
DL
5801 POSTING_READ(CDCLK_CTL);
5802
5803 /* inform PCU of the change */
5804 mutex_lock(&dev_priv->rps.hw_lock);
5805 sandybridge_pcode_write(dev_priv, SKL_PCODE_CDCLK_CONTROL, pcu_ack);
5806 mutex_unlock(&dev_priv->rps.hw_lock);
560a7ae4
DL
5807
5808 intel_update_cdclk(dev);
5d96d8af
DL
5809}
5810
9f7eb31a
VS
5811static void skl_sanitize_cdclk(struct drm_i915_private *dev_priv);
5812
5d96d8af
DL
5813void skl_uninit_cdclk(struct drm_i915_private *dev_priv)
5814{
709e05c3 5815 skl_set_cdclk(dev_priv, dev_priv->cdclk_pll.ref, 0);
5d96d8af
DL
5816}
5817
5818void skl_init_cdclk(struct drm_i915_private *dev_priv)
5819{
9f7eb31a
VS
5820 int cdclk, vco;
5821
5822 skl_sanitize_cdclk(dev_priv);
5d96d8af 5823
63911d72 5824 if (dev_priv->cdclk_freq != 0 && dev_priv->cdclk_pll.vco != 0) {
9f7eb31a
VS
5825 /*
5826 * Use the current vco as our initial
5827 * guess as to what the preferred vco is.
5828 */
5829 if (dev_priv->skl_preferred_vco_freq == 0)
5830 skl_set_preferred_cdclk_vco(dev_priv,
63911d72 5831 dev_priv->cdclk_pll.vco);
70c2c184 5832 return;
1cd593e0 5833 }
5d96d8af 5834
70c2c184
VS
5835 vco = dev_priv->skl_preferred_vco_freq;
5836 if (vco == 0)
63911d72 5837 vco = 8100000;
70c2c184 5838 cdclk = skl_calc_cdclk(0, vco);
5d96d8af 5839
70c2c184 5840 skl_set_cdclk(dev_priv, cdclk, vco);
5d96d8af
DL
5841}
5842
9f7eb31a 5843static void skl_sanitize_cdclk(struct drm_i915_private *dev_priv)
c73666f3 5844{
09492498 5845 uint32_t cdctl, expected;
c73666f3 5846
f1b391a5
SK
5847 /*
5848 * check if the pre-os intialized the display
5849 * There is SWF18 scratchpad register defined which is set by the
5850 * pre-os which can be used by the OS drivers to check the status
5851 */
5852 if ((I915_READ(SWF_ILK(0x18)) & 0x00FFFFFF) == 0)
5853 goto sanitize;
5854
1c3f7700 5855 intel_update_cdclk(dev_priv->dev);
c73666f3 5856 /* Is PLL enabled and locked ? */
1c3f7700
ID
5857 if (dev_priv->cdclk_pll.vco == 0 ||
5858 dev_priv->cdclk_freq == dev_priv->cdclk_pll.ref)
c73666f3
SK
5859 goto sanitize;
5860
5861 /* DPLL okay; verify the cdclock
5862 *
5863 * Noticed in some instances that the freq selection is correct but
5864 * decimal part is programmed wrong from BIOS where pre-os does not
5865 * enable display. Verify the same as well.
5866 */
09492498
VS
5867 cdctl = I915_READ(CDCLK_CTL);
5868 expected = (cdctl & CDCLK_FREQ_SEL_MASK) |
5869 skl_cdclk_decimal(dev_priv->cdclk_freq);
5870 if (cdctl == expected)
c73666f3 5871 /* All well; nothing to sanitize */
9f7eb31a 5872 return;
c89e39f3 5873
9f7eb31a
VS
5874sanitize:
5875 DRM_DEBUG_KMS("Sanitizing cdclk programmed by pre-os\n");
c73666f3 5876
9f7eb31a
VS
5877 /* force cdclk programming */
5878 dev_priv->cdclk_freq = 0;
5879 /* force full PLL disable + enable */
63911d72 5880 dev_priv->cdclk_pll.vco = -1;
c73666f3
SK
5881}
5882
30a970c6
JB
5883/* Adjust CDclk dividers to allow high res or save power if possible */
5884static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
5885{
5886 struct drm_i915_private *dev_priv = dev->dev_private;
5887 u32 val, cmd;
5888
164dfd28
VK
5889 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5890 != dev_priv->cdclk_freq);
d60c4473 5891
dfcab17e 5892 if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
30a970c6 5893 cmd = 2;
dfcab17e 5894 else if (cdclk == 266667)
30a970c6
JB
5895 cmd = 1;
5896 else
5897 cmd = 0;
5898
5899 mutex_lock(&dev_priv->rps.hw_lock);
5900 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5901 val &= ~DSPFREQGUAR_MASK;
5902 val |= (cmd << DSPFREQGUAR_SHIFT);
5903 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5904 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5905 DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
5906 50)) {
5907 DRM_ERROR("timed out waiting for CDclk change\n");
5908 }
5909 mutex_unlock(&dev_priv->rps.hw_lock);
5910
54433e91
VS
5911 mutex_lock(&dev_priv->sb_lock);
5912
dfcab17e 5913 if (cdclk == 400000) {
6bcda4f0 5914 u32 divider;
30a970c6 5915
6bcda4f0 5916 divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
30a970c6 5917
30a970c6
JB
5918 /* adjust cdclk divider */
5919 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
87d5d259 5920 val &= ~CCK_FREQUENCY_VALUES;
30a970c6
JB
5921 val |= divider;
5922 vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
a877e801
VS
5923
5924 if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
87d5d259 5925 CCK_FREQUENCY_STATUS) == (divider << CCK_FREQUENCY_STATUS_SHIFT),
a877e801
VS
5926 50))
5927 DRM_ERROR("timed out waiting for CDclk change\n");
30a970c6
JB
5928 }
5929
30a970c6
JB
5930 /* adjust self-refresh exit latency value */
5931 val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
5932 val &= ~0x7f;
5933
5934 /*
5935 * For high bandwidth configs, we set a higher latency in the bunit
5936 * so that the core display fetch happens in time to avoid underruns.
5937 */
dfcab17e 5938 if (cdclk == 400000)
30a970c6
JB
5939 val |= 4500 / 250; /* 4.5 usec */
5940 else
5941 val |= 3000 / 250; /* 3.0 usec */
5942 vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
54433e91 5943
a580516d 5944 mutex_unlock(&dev_priv->sb_lock);
30a970c6 5945
b6283055 5946 intel_update_cdclk(dev);
30a970c6
JB
5947}
5948
383c5a6a
VS
5949static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
5950{
5951 struct drm_i915_private *dev_priv = dev->dev_private;
5952 u32 val, cmd;
5953
164dfd28
VK
5954 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5955 != dev_priv->cdclk_freq);
383c5a6a
VS
5956
5957 switch (cdclk) {
383c5a6a
VS
5958 case 333333:
5959 case 320000:
383c5a6a 5960 case 266667:
383c5a6a 5961 case 200000:
383c5a6a
VS
5962 break;
5963 default:
5f77eeb0 5964 MISSING_CASE(cdclk);
383c5a6a
VS
5965 return;
5966 }
5967
9d0d3fda
VS
5968 /*
5969 * Specs are full of misinformation, but testing on actual
5970 * hardware has shown that we just need to write the desired
5971 * CCK divider into the Punit register.
5972 */
5973 cmd = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
5974
383c5a6a
VS
5975 mutex_lock(&dev_priv->rps.hw_lock);
5976 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5977 val &= ~DSPFREQGUAR_MASK_CHV;
5978 val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
5979 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5980 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5981 DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
5982 50)) {
5983 DRM_ERROR("timed out waiting for CDclk change\n");
5984 }
5985 mutex_unlock(&dev_priv->rps.hw_lock);
5986
b6283055 5987 intel_update_cdclk(dev);
383c5a6a
VS
5988}
5989
30a970c6
JB
5990static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
5991 int max_pixclk)
5992{
6bcda4f0 5993 int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
6cca3195 5994 int limit = IS_CHERRYVIEW(dev_priv) ? 95 : 90;
29dc7ef3 5995
30a970c6
JB
5996 /*
5997 * Really only a few cases to deal with, as only 4 CDclks are supported:
5998 * 200MHz
5999 * 267MHz
29dc7ef3 6000 * 320/333MHz (depends on HPLL freq)
6cca3195
VS
6001 * 400MHz (VLV only)
6002 * So we check to see whether we're above 90% (VLV) or 95% (CHV)
6003 * of the lower bin and adjust if needed.
e37c67a1
VS
6004 *
6005 * We seem to get an unstable or solid color picture at 200MHz.
6006 * Not sure what's wrong. For now use 200MHz only when all pipes
6007 * are off.
30a970c6 6008 */
6cca3195
VS
6009 if (!IS_CHERRYVIEW(dev_priv) &&
6010 max_pixclk > freq_320*limit/100)
dfcab17e 6011 return 400000;
6cca3195 6012 else if (max_pixclk > 266667*limit/100)
29dc7ef3 6013 return freq_320;
e37c67a1 6014 else if (max_pixclk > 0)
dfcab17e 6015 return 266667;
e37c67a1
VS
6016 else
6017 return 200000;
30a970c6
JB
6018}
6019
324513c0 6020static int bxt_calc_cdclk(int max_pixclk)
f8437dd1 6021{
760e1477 6022 if (max_pixclk > 576000)
f8437dd1 6023 return 624000;
760e1477 6024 else if (max_pixclk > 384000)
f8437dd1 6025 return 576000;
760e1477 6026 else if (max_pixclk > 288000)
f8437dd1 6027 return 384000;
760e1477 6028 else if (max_pixclk > 144000)
f8437dd1
VK
6029 return 288000;
6030 else
6031 return 144000;
6032}
6033
e8788cbc 6034/* Compute the max pixel clock for new configuration. */
a821fc46
ACO
6035static int intel_mode_max_pixclk(struct drm_device *dev,
6036 struct drm_atomic_state *state)
30a970c6 6037{
565602d7
ML
6038 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
6039 struct drm_i915_private *dev_priv = dev->dev_private;
6040 struct drm_crtc *crtc;
6041 struct drm_crtc_state *crtc_state;
6042 unsigned max_pixclk = 0, i;
6043 enum pipe pipe;
30a970c6 6044
565602d7
ML
6045 memcpy(intel_state->min_pixclk, dev_priv->min_pixclk,
6046 sizeof(intel_state->min_pixclk));
304603f4 6047
565602d7
ML
6048 for_each_crtc_in_state(state, crtc, crtc_state, i) {
6049 int pixclk = 0;
6050
6051 if (crtc_state->enable)
6052 pixclk = crtc_state->adjusted_mode.crtc_clock;
304603f4 6053
565602d7 6054 intel_state->min_pixclk[i] = pixclk;
30a970c6
JB
6055 }
6056
565602d7
ML
6057 for_each_pipe(dev_priv, pipe)
6058 max_pixclk = max(intel_state->min_pixclk[pipe], max_pixclk);
6059
30a970c6
JB
6060 return max_pixclk;
6061}
6062
27c329ed 6063static int valleyview_modeset_calc_cdclk(struct drm_atomic_state *state)
30a970c6 6064{
27c329ed
ML
6065 struct drm_device *dev = state->dev;
6066 struct drm_i915_private *dev_priv = dev->dev_private;
6067 int max_pixclk = intel_mode_max_pixclk(dev, state);
1a617b77
ML
6068 struct intel_atomic_state *intel_state =
6069 to_intel_atomic_state(state);
30a970c6 6070
1a617b77 6071 intel_state->cdclk = intel_state->dev_cdclk =
27c329ed 6072 valleyview_calc_cdclk(dev_priv, max_pixclk);
0a9ab303 6073
1a617b77
ML
6074 if (!intel_state->active_crtcs)
6075 intel_state->dev_cdclk = valleyview_calc_cdclk(dev_priv, 0);
6076
27c329ed
ML
6077 return 0;
6078}
304603f4 6079
324513c0 6080static int bxt_modeset_calc_cdclk(struct drm_atomic_state *state)
27c329ed 6081{
4e5ca60f 6082 int max_pixclk = ilk_max_pixel_rate(state);
1a617b77
ML
6083 struct intel_atomic_state *intel_state =
6084 to_intel_atomic_state(state);
85a96e7a 6085
1a617b77 6086 intel_state->cdclk = intel_state->dev_cdclk =
324513c0 6087 bxt_calc_cdclk(max_pixclk);
85a96e7a 6088
1a617b77 6089 if (!intel_state->active_crtcs)
324513c0 6090 intel_state->dev_cdclk = bxt_calc_cdclk(0);
1a617b77 6091
27c329ed 6092 return 0;
30a970c6
JB
6093}
6094
1e69cd74
VS
6095static void vlv_program_pfi_credits(struct drm_i915_private *dev_priv)
6096{
6097 unsigned int credits, default_credits;
6098
6099 if (IS_CHERRYVIEW(dev_priv))
6100 default_credits = PFI_CREDIT(12);
6101 else
6102 default_credits = PFI_CREDIT(8);
6103
bfa7df01 6104 if (dev_priv->cdclk_freq >= dev_priv->czclk_freq) {
1e69cd74
VS
6105 /* CHV suggested value is 31 or 63 */
6106 if (IS_CHERRYVIEW(dev_priv))
fcc0008f 6107 credits = PFI_CREDIT_63;
1e69cd74
VS
6108 else
6109 credits = PFI_CREDIT(15);
6110 } else {
6111 credits = default_credits;
6112 }
6113
6114 /*
6115 * WA - write default credits before re-programming
6116 * FIXME: should we also set the resend bit here?
6117 */
6118 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
6119 default_credits);
6120
6121 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
6122 credits | PFI_CREDIT_RESEND);
6123
6124 /*
6125 * FIXME is this guaranteed to clear
6126 * immediately or should we poll for it?
6127 */
6128 WARN_ON(I915_READ(GCI_CONTROL) & PFI_CREDIT_RESEND);
6129}
6130
27c329ed 6131static void valleyview_modeset_commit_cdclk(struct drm_atomic_state *old_state)
30a970c6 6132{
a821fc46 6133 struct drm_device *dev = old_state->dev;
30a970c6 6134 struct drm_i915_private *dev_priv = dev->dev_private;
1a617b77
ML
6135 struct intel_atomic_state *old_intel_state =
6136 to_intel_atomic_state(old_state);
6137 unsigned req_cdclk = old_intel_state->dev_cdclk;
30a970c6 6138
27c329ed
ML
6139 /*
6140 * FIXME: We can end up here with all power domains off, yet
6141 * with a CDCLK frequency other than the minimum. To account
6142 * for this take the PIPE-A power domain, which covers the HW
6143 * blocks needed for the following programming. This can be
6144 * removed once it's guaranteed that we get here either with
6145 * the minimum CDCLK set, or the required power domains
6146 * enabled.
6147 */
6148 intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
738c05c0 6149
27c329ed
ML
6150 if (IS_CHERRYVIEW(dev))
6151 cherryview_set_cdclk(dev, req_cdclk);
6152 else
6153 valleyview_set_cdclk(dev, req_cdclk);
738c05c0 6154
27c329ed 6155 vlv_program_pfi_credits(dev_priv);
1e69cd74 6156
27c329ed 6157 intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
30a970c6
JB
6158}
6159
89b667f8
JB
6160static void valleyview_crtc_enable(struct drm_crtc *crtc)
6161{
6162 struct drm_device *dev = crtc->dev;
a72e4c9f 6163 struct drm_i915_private *dev_priv = to_i915(dev);
89b667f8
JB
6164 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6165 struct intel_encoder *encoder;
b95c5321
ML
6166 struct intel_crtc_state *pipe_config =
6167 to_intel_crtc_state(crtc->state);
89b667f8 6168 int pipe = intel_crtc->pipe;
89b667f8 6169
53d9f4e9 6170 if (WARN_ON(intel_crtc->active))
89b667f8
JB
6171 return;
6172
6e3c9717 6173 if (intel_crtc->config->has_dp_encoder)
fe3cd48d 6174 intel_dp_set_m_n(intel_crtc, M1_N1);
5b18e57c
DV
6175
6176 intel_set_pipe_timings(intel_crtc);
bc58be60 6177 intel_set_pipe_src_size(intel_crtc);
5b18e57c 6178
c14b0485
VS
6179 if (IS_CHERRYVIEW(dev) && pipe == PIPE_B) {
6180 struct drm_i915_private *dev_priv = dev->dev_private;
6181
6182 I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
6183 I915_WRITE(CHV_CANVAS(pipe), 0);
6184 }
6185
5b18e57c
DV
6186 i9xx_set_pipeconf(intel_crtc);
6187
89b667f8 6188 intel_crtc->active = true;
89b667f8 6189
a72e4c9f 6190 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4a3436e8 6191
89b667f8
JB
6192 for_each_encoder_on_crtc(dev, crtc, encoder)
6193 if (encoder->pre_pll_enable)
6194 encoder->pre_pll_enable(encoder);
6195
cd2d34d9
VS
6196 if (IS_CHERRYVIEW(dev)) {
6197 chv_prepare_pll(intel_crtc, intel_crtc->config);
6198 chv_enable_pll(intel_crtc, intel_crtc->config);
6199 } else {
6200 vlv_prepare_pll(intel_crtc, intel_crtc->config);
6201 vlv_enable_pll(intel_crtc, intel_crtc->config);
9d556c99 6202 }
89b667f8
JB
6203
6204 for_each_encoder_on_crtc(dev, crtc, encoder)
6205 if (encoder->pre_enable)
6206 encoder->pre_enable(encoder);
6207
2dd24552
JB
6208 i9xx_pfit_enable(intel_crtc);
6209
b95c5321 6210 intel_color_load_luts(&pipe_config->base);
63cbb074 6211
caed361d 6212 intel_update_watermarks(crtc);
e1fdc473 6213 intel_enable_pipe(intel_crtc);
be6a6f8e 6214
4b3a9526
VS
6215 assert_vblank_disabled(crtc);
6216 drm_crtc_vblank_on(crtc);
6217
f9b61ff6
DV
6218 for_each_encoder_on_crtc(dev, crtc, encoder)
6219 encoder->enable(encoder);
89b667f8
JB
6220}
6221
f13c2ef3
DV
6222static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
6223{
6224 struct drm_device *dev = crtc->base.dev;
6225 struct drm_i915_private *dev_priv = dev->dev_private;
6226
6e3c9717
ACO
6227 I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
6228 I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
f13c2ef3
DV
6229}
6230
0b8765c6 6231static void i9xx_crtc_enable(struct drm_crtc *crtc)
79e53945
JB
6232{
6233 struct drm_device *dev = crtc->dev;
a72e4c9f 6234 struct drm_i915_private *dev_priv = to_i915(dev);
79e53945 6235 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 6236 struct intel_encoder *encoder;
b95c5321
ML
6237 struct intel_crtc_state *pipe_config =
6238 to_intel_crtc_state(crtc->state);
cd2d34d9 6239 enum pipe pipe = intel_crtc->pipe;
79e53945 6240
53d9f4e9 6241 if (WARN_ON(intel_crtc->active))
f7abfe8b
CW
6242 return;
6243
f13c2ef3
DV
6244 i9xx_set_pll_dividers(intel_crtc);
6245
6e3c9717 6246 if (intel_crtc->config->has_dp_encoder)
fe3cd48d 6247 intel_dp_set_m_n(intel_crtc, M1_N1);
5b18e57c
DV
6248
6249 intel_set_pipe_timings(intel_crtc);
bc58be60 6250 intel_set_pipe_src_size(intel_crtc);
5b18e57c 6251
5b18e57c
DV
6252 i9xx_set_pipeconf(intel_crtc);
6253
f7abfe8b 6254 intel_crtc->active = true;
6b383a7f 6255
4a3436e8 6256 if (!IS_GEN2(dev))
a72e4c9f 6257 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4a3436e8 6258
9d6d9f19
MK
6259 for_each_encoder_on_crtc(dev, crtc, encoder)
6260 if (encoder->pre_enable)
6261 encoder->pre_enable(encoder);
6262
f6736a1a
DV
6263 i9xx_enable_pll(intel_crtc);
6264
2dd24552
JB
6265 i9xx_pfit_enable(intel_crtc);
6266
b95c5321 6267 intel_color_load_luts(&pipe_config->base);
63cbb074 6268
f37fcc2a 6269 intel_update_watermarks(crtc);
e1fdc473 6270 intel_enable_pipe(intel_crtc);
be6a6f8e 6271
4b3a9526
VS
6272 assert_vblank_disabled(crtc);
6273 drm_crtc_vblank_on(crtc);
6274
f9b61ff6
DV
6275 for_each_encoder_on_crtc(dev, crtc, encoder)
6276 encoder->enable(encoder);
0b8765c6 6277}
79e53945 6278
87476d63
DV
6279static void i9xx_pfit_disable(struct intel_crtc *crtc)
6280{
6281 struct drm_device *dev = crtc->base.dev;
6282 struct drm_i915_private *dev_priv = dev->dev_private;
87476d63 6283
6e3c9717 6284 if (!crtc->config->gmch_pfit.control)
328d8e82 6285 return;
87476d63 6286
328d8e82 6287 assert_pipe_disabled(dev_priv, crtc->pipe);
87476d63 6288
328d8e82
DV
6289 DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
6290 I915_READ(PFIT_CONTROL));
6291 I915_WRITE(PFIT_CONTROL, 0);
87476d63
DV
6292}
6293
0b8765c6
JB
6294static void i9xx_crtc_disable(struct drm_crtc *crtc)
6295{
6296 struct drm_device *dev = crtc->dev;
6297 struct drm_i915_private *dev_priv = dev->dev_private;
6298 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 6299 struct intel_encoder *encoder;
0b8765c6 6300 int pipe = intel_crtc->pipe;
ef9c3aee 6301
6304cd91
VS
6302 /*
6303 * On gen2 planes are double buffered but the pipe isn't, so we must
6304 * wait for planes to fully turn off before disabling the pipe.
6305 */
90e83e53
ACO
6306 if (IS_GEN2(dev))
6307 intel_wait_for_vblank(dev, pipe);
6304cd91 6308
4b3a9526
VS
6309 for_each_encoder_on_crtc(dev, crtc, encoder)
6310 encoder->disable(encoder);
6311
f9b61ff6
DV
6312 drm_crtc_vblank_off(crtc);
6313 assert_vblank_disabled(crtc);
6314
575f7ab7 6315 intel_disable_pipe(intel_crtc);
24a1f16d 6316
87476d63 6317 i9xx_pfit_disable(intel_crtc);
24a1f16d 6318
89b667f8
JB
6319 for_each_encoder_on_crtc(dev, crtc, encoder)
6320 if (encoder->post_disable)
6321 encoder->post_disable(encoder);
6322
a65347ba 6323 if (!intel_crtc->config->has_dsi_encoder) {
076ed3b2
CML
6324 if (IS_CHERRYVIEW(dev))
6325 chv_disable_pll(dev_priv, pipe);
6326 else if (IS_VALLEYVIEW(dev))
6327 vlv_disable_pll(dev_priv, pipe);
6328 else
1c4e0274 6329 i9xx_disable_pll(intel_crtc);
076ed3b2 6330 }
0b8765c6 6331
d6db995f
VS
6332 for_each_encoder_on_crtc(dev, crtc, encoder)
6333 if (encoder->post_pll_disable)
6334 encoder->post_pll_disable(encoder);
6335
4a3436e8 6336 if (!IS_GEN2(dev))
a72e4c9f 6337 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
0b8765c6
JB
6338}
6339
b17d48e2
ML
6340static void intel_crtc_disable_noatomic(struct drm_crtc *crtc)
6341{
842e0307 6342 struct intel_encoder *encoder;
b17d48e2
ML
6343 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6344 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
6345 enum intel_display_power_domain domain;
6346 unsigned long domains;
6347
6348 if (!intel_crtc->active)
6349 return;
6350
a539205a 6351 if (to_intel_plane_state(crtc->primary->state)->visible) {
5a21b665 6352 WARN_ON(intel_crtc->flip_work);
fc32b1fd 6353
2622a081 6354 intel_pre_disable_primary_noatomic(crtc);
54a41961
ML
6355
6356 intel_crtc_disable_planes(crtc, 1 << drm_plane_index(crtc->primary));
6357 to_intel_plane_state(crtc->primary->state)->visible = false;
a539205a
ML
6358 }
6359
b17d48e2 6360 dev_priv->display.crtc_disable(crtc);
842e0307 6361
78108b7c
VS
6362 DRM_DEBUG_KMS("[CRTC:%d:%s] hw state adjusted, was enabled, now disabled\n",
6363 crtc->base.id, crtc->name);
842e0307
ML
6364
6365 WARN_ON(drm_atomic_set_mode_for_crtc(crtc->state, NULL) < 0);
6366 crtc->state->active = false;
37d9078b 6367 intel_crtc->active = false;
842e0307
ML
6368 crtc->enabled = false;
6369 crtc->state->connector_mask = 0;
6370 crtc->state->encoder_mask = 0;
6371
6372 for_each_encoder_on_crtc(crtc->dev, crtc, encoder)
6373 encoder->base.crtc = NULL;
6374
58f9c0bc 6375 intel_fbc_disable(intel_crtc);
37d9078b 6376 intel_update_watermarks(crtc);
1f7457b1 6377 intel_disable_shared_dpll(intel_crtc);
b17d48e2
ML
6378
6379 domains = intel_crtc->enabled_power_domains;
6380 for_each_power_domain(domain, domains)
6381 intel_display_power_put(dev_priv, domain);
6382 intel_crtc->enabled_power_domains = 0;
565602d7
ML
6383
6384 dev_priv->active_crtcs &= ~(1 << intel_crtc->pipe);
6385 dev_priv->min_pixclk[intel_crtc->pipe] = 0;
b17d48e2
ML
6386}
6387
6b72d486
ML
6388/*
6389 * turn all crtc's off, but do not adjust state
6390 * This has to be paired with a call to intel_modeset_setup_hw_state.
6391 */
70e0bd74 6392int intel_display_suspend(struct drm_device *dev)
ee7b9f93 6393{
e2c8b870 6394 struct drm_i915_private *dev_priv = to_i915(dev);
70e0bd74 6395 struct drm_atomic_state *state;
e2c8b870 6396 int ret;
70e0bd74 6397
e2c8b870
ML
6398 state = drm_atomic_helper_suspend(dev);
6399 ret = PTR_ERR_OR_ZERO(state);
70e0bd74
ML
6400 if (ret)
6401 DRM_ERROR("Suspending crtc's failed with %i\n", ret);
e2c8b870
ML
6402 else
6403 dev_priv->modeset_restore_state = state;
70e0bd74 6404 return ret;
ee7b9f93
JB
6405}
6406
ea5b213a 6407void intel_encoder_destroy(struct drm_encoder *encoder)
7e7d76c3 6408{
4ef69c7a 6409 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
ea5b213a 6410
ea5b213a
CW
6411 drm_encoder_cleanup(encoder);
6412 kfree(intel_encoder);
7e7d76c3
JB
6413}
6414
0a91ca29
DV
6415/* Cross check the actual hw state with our own modeset state tracking (and it's
6416 * internal consistency). */
5a21b665 6417static void intel_connector_verify_state(struct intel_connector *connector)
79e53945 6418{
5a21b665 6419 struct drm_crtc *crtc = connector->base.state->crtc;
35dd3c64
ML
6420
6421 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
6422 connector->base.base.id,
6423 connector->base.name);
6424
0a91ca29 6425 if (connector->get_hw_state(connector)) {
e85376cb 6426 struct intel_encoder *encoder = connector->encoder;
5a21b665 6427 struct drm_connector_state *conn_state = connector->base.state;
0a91ca29 6428
35dd3c64
ML
6429 I915_STATE_WARN(!crtc,
6430 "connector enabled without attached crtc\n");
0a91ca29 6431
35dd3c64
ML
6432 if (!crtc)
6433 return;
6434
6435 I915_STATE_WARN(!crtc->state->active,
6436 "connector is active, but attached crtc isn't\n");
6437
e85376cb 6438 if (!encoder || encoder->type == INTEL_OUTPUT_DP_MST)
35dd3c64
ML
6439 return;
6440
e85376cb 6441 I915_STATE_WARN(conn_state->best_encoder != &encoder->base,
35dd3c64
ML
6442 "atomic encoder doesn't match attached encoder\n");
6443
e85376cb 6444 I915_STATE_WARN(conn_state->crtc != encoder->base.crtc,
35dd3c64
ML
6445 "attached encoder crtc differs from connector crtc\n");
6446 } else {
4d688a2a
ML
6447 I915_STATE_WARN(crtc && crtc->state->active,
6448 "attached crtc is active, but connector isn't\n");
5a21b665 6449 I915_STATE_WARN(!crtc && connector->base.state->best_encoder,
35dd3c64 6450 "best encoder set without crtc!\n");
0a91ca29 6451 }
79e53945
JB
6452}
6453
08d9bc92
ACO
6454int intel_connector_init(struct intel_connector *connector)
6455{
5350a031 6456 drm_atomic_helper_connector_reset(&connector->base);
08d9bc92 6457
5350a031 6458 if (!connector->base.state)
08d9bc92
ACO
6459 return -ENOMEM;
6460
08d9bc92
ACO
6461 return 0;
6462}
6463
6464struct intel_connector *intel_connector_alloc(void)
6465{
6466 struct intel_connector *connector;
6467
6468 connector = kzalloc(sizeof *connector, GFP_KERNEL);
6469 if (!connector)
6470 return NULL;
6471
6472 if (intel_connector_init(connector) < 0) {
6473 kfree(connector);
6474 return NULL;
6475 }
6476
6477 return connector;
6478}
6479
f0947c37
DV
6480/* Simple connector->get_hw_state implementation for encoders that support only
6481 * one connector and no cloning and hence the encoder state determines the state
6482 * of the connector. */
6483bool intel_connector_get_hw_state(struct intel_connector *connector)
ea5b213a 6484{
24929352 6485 enum pipe pipe = 0;
f0947c37 6486 struct intel_encoder *encoder = connector->encoder;
ea5b213a 6487
f0947c37 6488 return encoder->get_hw_state(encoder, &pipe);
ea5b213a
CW
6489}
6490
6d293983 6491static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
d272ddfa 6492{
6d293983
ACO
6493 if (crtc_state->base.enable && crtc_state->has_pch_encoder)
6494 return crtc_state->fdi_lanes;
d272ddfa
VS
6495
6496 return 0;
6497}
6498
6d293983 6499static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
5cec258b 6500 struct intel_crtc_state *pipe_config)
1857e1da 6501{
6d293983
ACO
6502 struct drm_atomic_state *state = pipe_config->base.state;
6503 struct intel_crtc *other_crtc;
6504 struct intel_crtc_state *other_crtc_state;
6505
1857e1da
DV
6506 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
6507 pipe_name(pipe), pipe_config->fdi_lanes);
6508 if (pipe_config->fdi_lanes > 4) {
6509 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
6510 pipe_name(pipe), pipe_config->fdi_lanes);
6d293983 6511 return -EINVAL;
1857e1da
DV
6512 }
6513
bafb6553 6514 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
1857e1da
DV
6515 if (pipe_config->fdi_lanes > 2) {
6516 DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
6517 pipe_config->fdi_lanes);
6d293983 6518 return -EINVAL;
1857e1da 6519 } else {
6d293983 6520 return 0;
1857e1da
DV
6521 }
6522 }
6523
6524 if (INTEL_INFO(dev)->num_pipes == 2)
6d293983 6525 return 0;
1857e1da
DV
6526
6527 /* Ivybridge 3 pipe is really complicated */
6528 switch (pipe) {
6529 case PIPE_A:
6d293983 6530 return 0;
1857e1da 6531 case PIPE_B:
6d293983
ACO
6532 if (pipe_config->fdi_lanes <= 2)
6533 return 0;
6534
6535 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_C));
6536 other_crtc_state =
6537 intel_atomic_get_crtc_state(state, other_crtc);
6538 if (IS_ERR(other_crtc_state))
6539 return PTR_ERR(other_crtc_state);
6540
6541 if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
1857e1da
DV
6542 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
6543 pipe_name(pipe), pipe_config->fdi_lanes);
6d293983 6544 return -EINVAL;
1857e1da 6545 }
6d293983 6546 return 0;
1857e1da 6547 case PIPE_C:
251cc67c
VS
6548 if (pipe_config->fdi_lanes > 2) {
6549 DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
6550 pipe_name(pipe), pipe_config->fdi_lanes);
6d293983 6551 return -EINVAL;
251cc67c 6552 }
6d293983
ACO
6553
6554 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_B));
6555 other_crtc_state =
6556 intel_atomic_get_crtc_state(state, other_crtc);
6557 if (IS_ERR(other_crtc_state))
6558 return PTR_ERR(other_crtc_state);
6559
6560 if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
1857e1da 6561 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
6d293983 6562 return -EINVAL;
1857e1da 6563 }
6d293983 6564 return 0;
1857e1da
DV
6565 default:
6566 BUG();
6567 }
6568}
6569
e29c22c0
DV
6570#define RETRY 1
6571static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
5cec258b 6572 struct intel_crtc_state *pipe_config)
877d48d5 6573{
1857e1da 6574 struct drm_device *dev = intel_crtc->base.dev;
7c5f93b0 6575 const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
6d293983
ACO
6576 int lane, link_bw, fdi_dotclock, ret;
6577 bool needs_recompute = false;
877d48d5 6578
e29c22c0 6579retry:
877d48d5
DV
6580 /* FDI is a binary signal running at ~2.7GHz, encoding
6581 * each output octet as 10 bits. The actual frequency
6582 * is stored as a divider into a 100MHz clock, and the
6583 * mode pixel clock is stored in units of 1KHz.
6584 * Hence the bw of each lane in terms of the mode signal
6585 * is:
6586 */
21a727b3 6587 link_bw = intel_fdi_link_freq(to_i915(dev), pipe_config);
877d48d5 6588
241bfc38 6589 fdi_dotclock = adjusted_mode->crtc_clock;
877d48d5 6590
2bd89a07 6591 lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
877d48d5
DV
6592 pipe_config->pipe_bpp);
6593
6594 pipe_config->fdi_lanes = lane;
6595
2bd89a07 6596 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
877d48d5 6597 link_bw, &pipe_config->fdi_m_n);
1857e1da 6598
e3b247da 6599 ret = ironlake_check_fdi_lanes(dev, intel_crtc->pipe, pipe_config);
6d293983 6600 if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
e29c22c0
DV
6601 pipe_config->pipe_bpp -= 2*3;
6602 DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
6603 pipe_config->pipe_bpp);
6604 needs_recompute = true;
6605 pipe_config->bw_constrained = true;
6606
6607 goto retry;
6608 }
6609
6610 if (needs_recompute)
6611 return RETRY;
6612
6d293983 6613 return ret;
877d48d5
DV
6614}
6615
8cfb3407
VS
6616static bool pipe_config_supports_ips(struct drm_i915_private *dev_priv,
6617 struct intel_crtc_state *pipe_config)
6618{
6619 if (pipe_config->pipe_bpp > 24)
6620 return false;
6621
6622 /* HSW can handle pixel rate up to cdclk? */
2d1fe073 6623 if (IS_HASWELL(dev_priv))
8cfb3407
VS
6624 return true;
6625
6626 /*
b432e5cf
VS
6627 * We compare against max which means we must take
6628 * the increased cdclk requirement into account when
6629 * calculating the new cdclk.
6630 *
6631 * Should measure whether using a lower cdclk w/o IPS
8cfb3407
VS
6632 */
6633 return ilk_pipe_pixel_rate(pipe_config) <=
6634 dev_priv->max_cdclk_freq * 95 / 100;
6635}
6636
42db64ef 6637static void hsw_compute_ips_config(struct intel_crtc *crtc,
5cec258b 6638 struct intel_crtc_state *pipe_config)
42db64ef 6639{
8cfb3407
VS
6640 struct drm_device *dev = crtc->base.dev;
6641 struct drm_i915_private *dev_priv = dev->dev_private;
6642
d330a953 6643 pipe_config->ips_enabled = i915.enable_ips &&
8cfb3407
VS
6644 hsw_crtc_supports_ips(crtc) &&
6645 pipe_config_supports_ips(dev_priv, pipe_config);
42db64ef
PZ
6646}
6647
39acb4aa
VS
6648static bool intel_crtc_supports_double_wide(const struct intel_crtc *crtc)
6649{
6650 const struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6651
6652 /* GDG double wide on either pipe, otherwise pipe A only */
6653 return INTEL_INFO(dev_priv)->gen < 4 &&
6654 (crtc->pipe == PIPE_A || IS_I915G(dev_priv));
6655}
6656
a43f6e0f 6657static int intel_crtc_compute_config(struct intel_crtc *crtc,
5cec258b 6658 struct intel_crtc_state *pipe_config)
79e53945 6659{
a43f6e0f 6660 struct drm_device *dev = crtc->base.dev;
8bd31e67 6661 struct drm_i915_private *dev_priv = dev->dev_private;
7c5f93b0 6662 const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
f3261156 6663 int clock_limit = dev_priv->max_dotclk_freq;
89749350 6664
cf532bb2 6665 if (INTEL_INFO(dev)->gen < 4) {
f3261156 6666 clock_limit = dev_priv->max_cdclk_freq * 9 / 10;
cf532bb2
VS
6667
6668 /*
39acb4aa 6669 * Enable double wide mode when the dot clock
cf532bb2 6670 * is > 90% of the (display) core speed.
cf532bb2 6671 */
39acb4aa
VS
6672 if (intel_crtc_supports_double_wide(crtc) &&
6673 adjusted_mode->crtc_clock > clock_limit) {
f3261156 6674 clock_limit = dev_priv->max_dotclk_freq;
cf532bb2 6675 pipe_config->double_wide = true;
ad3a4479 6676 }
f3261156 6677 }
ad3a4479 6678
f3261156
VS
6679 if (adjusted_mode->crtc_clock > clock_limit) {
6680 DRM_DEBUG_KMS("requested pixel clock (%d kHz) too high (max: %d kHz, double wide: %s)\n",
6681 adjusted_mode->crtc_clock, clock_limit,
6682 yesno(pipe_config->double_wide));
6683 return -EINVAL;
2c07245f 6684 }
89749350 6685
1d1d0e27
VS
6686 /*
6687 * Pipe horizontal size must be even in:
6688 * - DVO ganged mode
6689 * - LVDS dual channel mode
6690 * - Double wide pipe
6691 */
a93e255f 6692 if ((intel_pipe_will_have_type(pipe_config, INTEL_OUTPUT_LVDS) &&
1d1d0e27
VS
6693 intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
6694 pipe_config->pipe_src_w &= ~1;
6695
8693a824
DL
6696 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
6697 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
44f46b42
CW
6698 */
6699 if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
aad941d5 6700 adjusted_mode->crtc_hsync_start == adjusted_mode->crtc_hdisplay)
e29c22c0 6701 return -EINVAL;
44f46b42 6702
f5adf94e 6703 if (HAS_IPS(dev))
a43f6e0f
DV
6704 hsw_compute_ips_config(crtc, pipe_config);
6705
877d48d5 6706 if (pipe_config->has_pch_encoder)
a43f6e0f 6707 return ironlake_fdi_compute_config(crtc, pipe_config);
877d48d5 6708
cf5a15be 6709 return 0;
79e53945
JB
6710}
6711
1652d19e
VS
6712static int skylake_get_display_clock_speed(struct drm_device *dev)
6713{
6714 struct drm_i915_private *dev_priv = to_i915(dev);
ea61791e 6715 uint32_t cdctl;
1652d19e 6716
ea61791e 6717 skl_dpll0_update(dev_priv);
1652d19e 6718
63911d72 6719 if (dev_priv->cdclk_pll.vco == 0)
709e05c3 6720 return dev_priv->cdclk_pll.ref;
1652d19e 6721
ea61791e 6722 cdctl = I915_READ(CDCLK_CTL);
1652d19e 6723
63911d72 6724 if (dev_priv->cdclk_pll.vco == 8640000) {
1652d19e
VS
6725 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6726 case CDCLK_FREQ_450_432:
6727 return 432000;
6728 case CDCLK_FREQ_337_308:
487ed2e4 6729 return 308571;
ea61791e
VS
6730 case CDCLK_FREQ_540:
6731 return 540000;
1652d19e 6732 case CDCLK_FREQ_675_617:
487ed2e4 6733 return 617143;
1652d19e 6734 default:
ea61791e 6735 MISSING_CASE(cdctl & CDCLK_FREQ_SEL_MASK);
1652d19e
VS
6736 }
6737 } else {
1652d19e
VS
6738 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6739 case CDCLK_FREQ_450_432:
6740 return 450000;
6741 case CDCLK_FREQ_337_308:
6742 return 337500;
ea61791e
VS
6743 case CDCLK_FREQ_540:
6744 return 540000;
1652d19e
VS
6745 case CDCLK_FREQ_675_617:
6746 return 675000;
6747 default:
ea61791e 6748 MISSING_CASE(cdctl & CDCLK_FREQ_SEL_MASK);
1652d19e
VS
6749 }
6750 }
6751
709e05c3 6752 return dev_priv->cdclk_pll.ref;
1652d19e
VS
6753}
6754
83d7c81f
VS
6755static void bxt_de_pll_update(struct drm_i915_private *dev_priv)
6756{
6757 u32 val;
6758
6759 dev_priv->cdclk_pll.ref = 19200;
1c3f7700 6760 dev_priv->cdclk_pll.vco = 0;
83d7c81f
VS
6761
6762 val = I915_READ(BXT_DE_PLL_ENABLE);
1c3f7700 6763 if ((val & BXT_DE_PLL_PLL_ENABLE) == 0)
83d7c81f 6764 return;
83d7c81f 6765
1c3f7700
ID
6766 if (WARN_ON((val & BXT_DE_PLL_LOCK) == 0))
6767 return;
83d7c81f
VS
6768
6769 val = I915_READ(BXT_DE_PLL_CTL);
6770 dev_priv->cdclk_pll.vco = (val & BXT_DE_PLL_RATIO_MASK) *
6771 dev_priv->cdclk_pll.ref;
6772}
6773
acd3f3d3
BP
6774static int broxton_get_display_clock_speed(struct drm_device *dev)
6775{
6776 struct drm_i915_private *dev_priv = to_i915(dev);
f5986242
VS
6777 u32 divider;
6778 int div, vco;
acd3f3d3 6779
83d7c81f
VS
6780 bxt_de_pll_update(dev_priv);
6781
f5986242
VS
6782 vco = dev_priv->cdclk_pll.vco;
6783 if (vco == 0)
6784 return dev_priv->cdclk_pll.ref;
acd3f3d3 6785
f5986242 6786 divider = I915_READ(CDCLK_CTL) & BXT_CDCLK_CD2X_DIV_SEL_MASK;
acd3f3d3 6787
f5986242 6788 switch (divider) {
acd3f3d3 6789 case BXT_CDCLK_CD2X_DIV_SEL_1:
f5986242
VS
6790 div = 2;
6791 break;
acd3f3d3 6792 case BXT_CDCLK_CD2X_DIV_SEL_1_5:
f5986242
VS
6793 div = 3;
6794 break;
acd3f3d3 6795 case BXT_CDCLK_CD2X_DIV_SEL_2:
f5986242
VS
6796 div = 4;
6797 break;
acd3f3d3 6798 case BXT_CDCLK_CD2X_DIV_SEL_4:
f5986242
VS
6799 div = 8;
6800 break;
6801 default:
6802 MISSING_CASE(divider);
6803 return dev_priv->cdclk_pll.ref;
acd3f3d3
BP
6804 }
6805
f5986242 6806 return DIV_ROUND_CLOSEST(vco, div);
acd3f3d3
BP
6807}
6808
1652d19e
VS
6809static int broadwell_get_display_clock_speed(struct drm_device *dev)
6810{
6811 struct drm_i915_private *dev_priv = dev->dev_private;
6812 uint32_t lcpll = I915_READ(LCPLL_CTL);
6813 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6814
6815 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6816 return 800000;
6817 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6818 return 450000;
6819 else if (freq == LCPLL_CLK_FREQ_450)
6820 return 450000;
6821 else if (freq == LCPLL_CLK_FREQ_54O_BDW)
6822 return 540000;
6823 else if (freq == LCPLL_CLK_FREQ_337_5_BDW)
6824 return 337500;
6825 else
6826 return 675000;
6827}
6828
6829static int haswell_get_display_clock_speed(struct drm_device *dev)
6830{
6831 struct drm_i915_private *dev_priv = dev->dev_private;
6832 uint32_t lcpll = I915_READ(LCPLL_CTL);
6833 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6834
6835 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6836 return 800000;
6837 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6838 return 450000;
6839 else if (freq == LCPLL_CLK_FREQ_450)
6840 return 450000;
6841 else if (IS_HSW_ULT(dev))
6842 return 337500;
6843 else
6844 return 540000;
79e53945
JB
6845}
6846
25eb05fc
JB
6847static int valleyview_get_display_clock_speed(struct drm_device *dev)
6848{
bfa7df01
VS
6849 return vlv_get_cck_clock_hpll(to_i915(dev), "cdclk",
6850 CCK_DISPLAY_CLOCK_CONTROL);
25eb05fc
JB
6851}
6852
b37a6434
VS
6853static int ilk_get_display_clock_speed(struct drm_device *dev)
6854{
6855 return 450000;
6856}
6857
e70236a8
JB
6858static int i945_get_display_clock_speed(struct drm_device *dev)
6859{
6860 return 400000;
6861}
79e53945 6862
e70236a8 6863static int i915_get_display_clock_speed(struct drm_device *dev)
79e53945 6864{
e907f170 6865 return 333333;
e70236a8 6866}
79e53945 6867
e70236a8
JB
6868static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
6869{
6870 return 200000;
6871}
79e53945 6872
257a7ffc
DV
6873static int pnv_get_display_clock_speed(struct drm_device *dev)
6874{
6875 u16 gcfgc = 0;
6876
6877 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6878
6879 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6880 case GC_DISPLAY_CLOCK_267_MHZ_PNV:
e907f170 6881 return 266667;
257a7ffc 6882 case GC_DISPLAY_CLOCK_333_MHZ_PNV:
e907f170 6883 return 333333;
257a7ffc 6884 case GC_DISPLAY_CLOCK_444_MHZ_PNV:
e907f170 6885 return 444444;
257a7ffc
DV
6886 case GC_DISPLAY_CLOCK_200_MHZ_PNV:
6887 return 200000;
6888 default:
6889 DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
6890 case GC_DISPLAY_CLOCK_133_MHZ_PNV:
e907f170 6891 return 133333;
257a7ffc 6892 case GC_DISPLAY_CLOCK_167_MHZ_PNV:
e907f170 6893 return 166667;
257a7ffc
DV
6894 }
6895}
6896
e70236a8
JB
6897static int i915gm_get_display_clock_speed(struct drm_device *dev)
6898{
6899 u16 gcfgc = 0;
79e53945 6900
e70236a8
JB
6901 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6902
6903 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
e907f170 6904 return 133333;
e70236a8
JB
6905 else {
6906 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6907 case GC_DISPLAY_CLOCK_333_MHZ:
e907f170 6908 return 333333;
e70236a8
JB
6909 default:
6910 case GC_DISPLAY_CLOCK_190_200_MHZ:
6911 return 190000;
79e53945 6912 }
e70236a8
JB
6913 }
6914}
6915
6916static int i865_get_display_clock_speed(struct drm_device *dev)
6917{
e907f170 6918 return 266667;
e70236a8
JB
6919}
6920
1b1d2716 6921static int i85x_get_display_clock_speed(struct drm_device *dev)
e70236a8
JB
6922{
6923 u16 hpllcc = 0;
1b1d2716 6924
65cd2b3f
VS
6925 /*
6926 * 852GM/852GMV only supports 133 MHz and the HPLLCC
6927 * encoding is different :(
6928 * FIXME is this the right way to detect 852GM/852GMV?
6929 */
6930 if (dev->pdev->revision == 0x1)
6931 return 133333;
6932
1b1d2716
VS
6933 pci_bus_read_config_word(dev->pdev->bus,
6934 PCI_DEVFN(0, 3), HPLLCC, &hpllcc);
6935
e70236a8
JB
6936 /* Assume that the hardware is in the high speed state. This
6937 * should be the default.
6938 */
6939 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
6940 case GC_CLOCK_133_200:
1b1d2716 6941 case GC_CLOCK_133_200_2:
e70236a8
JB
6942 case GC_CLOCK_100_200:
6943 return 200000;
6944 case GC_CLOCK_166_250:
6945 return 250000;
6946 case GC_CLOCK_100_133:
e907f170 6947 return 133333;
1b1d2716
VS
6948 case GC_CLOCK_133_266:
6949 case GC_CLOCK_133_266_2:
6950 case GC_CLOCK_166_266:
6951 return 266667;
e70236a8 6952 }
79e53945 6953
e70236a8
JB
6954 /* Shouldn't happen */
6955 return 0;
6956}
79e53945 6957
e70236a8
JB
6958static int i830_get_display_clock_speed(struct drm_device *dev)
6959{
e907f170 6960 return 133333;
79e53945
JB
6961}
6962
34edce2f
VS
6963static unsigned int intel_hpll_vco(struct drm_device *dev)
6964{
6965 struct drm_i915_private *dev_priv = dev->dev_private;
6966 static const unsigned int blb_vco[8] = {
6967 [0] = 3200000,
6968 [1] = 4000000,
6969 [2] = 5333333,
6970 [3] = 4800000,
6971 [4] = 6400000,
6972 };
6973 static const unsigned int pnv_vco[8] = {
6974 [0] = 3200000,
6975 [1] = 4000000,
6976 [2] = 5333333,
6977 [3] = 4800000,
6978 [4] = 2666667,
6979 };
6980 static const unsigned int cl_vco[8] = {
6981 [0] = 3200000,
6982 [1] = 4000000,
6983 [2] = 5333333,
6984 [3] = 6400000,
6985 [4] = 3333333,
6986 [5] = 3566667,
6987 [6] = 4266667,
6988 };
6989 static const unsigned int elk_vco[8] = {
6990 [0] = 3200000,
6991 [1] = 4000000,
6992 [2] = 5333333,
6993 [3] = 4800000,
6994 };
6995 static const unsigned int ctg_vco[8] = {
6996 [0] = 3200000,
6997 [1] = 4000000,
6998 [2] = 5333333,
6999 [3] = 6400000,
7000 [4] = 2666667,
7001 [5] = 4266667,
7002 };
7003 const unsigned int *vco_table;
7004 unsigned int vco;
7005 uint8_t tmp = 0;
7006
7007 /* FIXME other chipsets? */
7008 if (IS_GM45(dev))
7009 vco_table = ctg_vco;
7010 else if (IS_G4X(dev))
7011 vco_table = elk_vco;
7012 else if (IS_CRESTLINE(dev))
7013 vco_table = cl_vco;
7014 else if (IS_PINEVIEW(dev))
7015 vco_table = pnv_vco;
7016 else if (IS_G33(dev))
7017 vco_table = blb_vco;
7018 else
7019 return 0;
7020
7021 tmp = I915_READ(IS_MOBILE(dev) ? HPLLVCO_MOBILE : HPLLVCO);
7022
7023 vco = vco_table[tmp & 0x7];
7024 if (vco == 0)
7025 DRM_ERROR("Bad HPLL VCO (HPLLVCO=0x%02x)\n", tmp);
7026 else
7027 DRM_DEBUG_KMS("HPLL VCO %u kHz\n", vco);
7028
7029 return vco;
7030}
7031
7032static int gm45_get_display_clock_speed(struct drm_device *dev)
7033{
7034 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
7035 uint16_t tmp = 0;
7036
7037 pci_read_config_word(dev->pdev, GCFGC, &tmp);
7038
7039 cdclk_sel = (tmp >> 12) & 0x1;
7040
7041 switch (vco) {
7042 case 2666667:
7043 case 4000000:
7044 case 5333333:
7045 return cdclk_sel ? 333333 : 222222;
7046 case 3200000:
7047 return cdclk_sel ? 320000 : 228571;
7048 default:
7049 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u, CFGC=0x%04x\n", vco, tmp);
7050 return 222222;
7051 }
7052}
7053
7054static int i965gm_get_display_clock_speed(struct drm_device *dev)
7055{
7056 static const uint8_t div_3200[] = { 16, 10, 8 };
7057 static const uint8_t div_4000[] = { 20, 12, 10 };
7058 static const uint8_t div_5333[] = { 24, 16, 14 };
7059 const uint8_t *div_table;
7060 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
7061 uint16_t tmp = 0;
7062
7063 pci_read_config_word(dev->pdev, GCFGC, &tmp);
7064
7065 cdclk_sel = ((tmp >> 8) & 0x1f) - 1;
7066
7067 if (cdclk_sel >= ARRAY_SIZE(div_3200))
7068 goto fail;
7069
7070 switch (vco) {
7071 case 3200000:
7072 div_table = div_3200;
7073 break;
7074 case 4000000:
7075 div_table = div_4000;
7076 break;
7077 case 5333333:
7078 div_table = div_5333;
7079 break;
7080 default:
7081 goto fail;
7082 }
7083
7084 return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
7085
caf4e252 7086fail:
34edce2f
VS
7087 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%04x\n", vco, tmp);
7088 return 200000;
7089}
7090
7091static int g33_get_display_clock_speed(struct drm_device *dev)
7092{
7093 static const uint8_t div_3200[] = { 12, 10, 8, 7, 5, 16 };
7094 static const uint8_t div_4000[] = { 14, 12, 10, 8, 6, 20 };
7095 static const uint8_t div_4800[] = { 20, 14, 12, 10, 8, 24 };
7096 static const uint8_t div_5333[] = { 20, 16, 12, 12, 8, 28 };
7097 const uint8_t *div_table;
7098 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
7099 uint16_t tmp = 0;
7100
7101 pci_read_config_word(dev->pdev, GCFGC, &tmp);
7102
7103 cdclk_sel = (tmp >> 4) & 0x7;
7104
7105 if (cdclk_sel >= ARRAY_SIZE(div_3200))
7106 goto fail;
7107
7108 switch (vco) {
7109 case 3200000:
7110 div_table = div_3200;
7111 break;
7112 case 4000000:
7113 div_table = div_4000;
7114 break;
7115 case 4800000:
7116 div_table = div_4800;
7117 break;
7118 case 5333333:
7119 div_table = div_5333;
7120 break;
7121 default:
7122 goto fail;
7123 }
7124
7125 return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
7126
caf4e252 7127fail:
34edce2f
VS
7128 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%08x\n", vco, tmp);
7129 return 190476;
7130}
7131
2c07245f 7132static void
a65851af 7133intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
2c07245f 7134{
a65851af
VS
7135 while (*num > DATA_LINK_M_N_MASK ||
7136 *den > DATA_LINK_M_N_MASK) {
2c07245f
ZW
7137 *num >>= 1;
7138 *den >>= 1;
7139 }
7140}
7141
a65851af
VS
7142static void compute_m_n(unsigned int m, unsigned int n,
7143 uint32_t *ret_m, uint32_t *ret_n)
7144{
7145 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
7146 *ret_m = div_u64((uint64_t) m * *ret_n, n);
7147 intel_reduce_m_n_ratio(ret_m, ret_n);
7148}
7149
e69d0bc1
DV
7150void
7151intel_link_compute_m_n(int bits_per_pixel, int nlanes,
7152 int pixel_clock, int link_clock,
7153 struct intel_link_m_n *m_n)
2c07245f 7154{
e69d0bc1 7155 m_n->tu = 64;
a65851af
VS
7156
7157 compute_m_n(bits_per_pixel * pixel_clock,
7158 link_clock * nlanes * 8,
7159 &m_n->gmch_m, &m_n->gmch_n);
7160
7161 compute_m_n(pixel_clock, link_clock,
7162 &m_n->link_m, &m_n->link_n);
2c07245f
ZW
7163}
7164
a7615030
CW
7165static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
7166{
d330a953
JN
7167 if (i915.panel_use_ssc >= 0)
7168 return i915.panel_use_ssc != 0;
41aa3448 7169 return dev_priv->vbt.lvds_use_ssc
435793df 7170 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
a7615030
CW
7171}
7172
7429e9d4 7173static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
c65d77d8 7174{
7df00d7a 7175 return (1 << dpll->n) << 16 | dpll->m2;
7429e9d4 7176}
f47709a9 7177
7429e9d4
DV
7178static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
7179{
7180 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
c65d77d8
JB
7181}
7182
f47709a9 7183static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
190f68c5 7184 struct intel_crtc_state *crtc_state,
9e2c8475 7185 struct dpll *reduced_clock)
a7516a05 7186{
f47709a9 7187 struct drm_device *dev = crtc->base.dev;
a7516a05
JB
7188 u32 fp, fp2 = 0;
7189
7190 if (IS_PINEVIEW(dev)) {
190f68c5 7191 fp = pnv_dpll_compute_fp(&crtc_state->dpll);
a7516a05 7192 if (reduced_clock)
7429e9d4 7193 fp2 = pnv_dpll_compute_fp(reduced_clock);
a7516a05 7194 } else {
190f68c5 7195 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
a7516a05 7196 if (reduced_clock)
7429e9d4 7197 fp2 = i9xx_dpll_compute_fp(reduced_clock);
a7516a05
JB
7198 }
7199
190f68c5 7200 crtc_state->dpll_hw_state.fp0 = fp;
a7516a05 7201
f47709a9 7202 crtc->lowfreq_avail = false;
a93e255f 7203 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
ab585dea 7204 reduced_clock) {
190f68c5 7205 crtc_state->dpll_hw_state.fp1 = fp2;
f47709a9 7206 crtc->lowfreq_avail = true;
a7516a05 7207 } else {
190f68c5 7208 crtc_state->dpll_hw_state.fp1 = fp;
a7516a05
JB
7209 }
7210}
7211
5e69f97f
CML
7212static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
7213 pipe)
89b667f8
JB
7214{
7215 u32 reg_val;
7216
7217 /*
7218 * PLLB opamp always calibrates to max value of 0x3f, force enable it
7219 * and set it to a reasonable value instead.
7220 */
ab3c759a 7221 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
89b667f8
JB
7222 reg_val &= 0xffffff00;
7223 reg_val |= 0x00000030;
ab3c759a 7224 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
89b667f8 7225
ab3c759a 7226 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
89b667f8
JB
7227 reg_val &= 0x8cffffff;
7228 reg_val = 0x8c000000;
ab3c759a 7229 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
89b667f8 7230
ab3c759a 7231 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
89b667f8 7232 reg_val &= 0xffffff00;
ab3c759a 7233 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
89b667f8 7234
ab3c759a 7235 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
89b667f8
JB
7236 reg_val &= 0x00ffffff;
7237 reg_val |= 0xb0000000;
ab3c759a 7238 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
89b667f8
JB
7239}
7240
b551842d
DV
7241static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
7242 struct intel_link_m_n *m_n)
7243{
7244 struct drm_device *dev = crtc->base.dev;
7245 struct drm_i915_private *dev_priv = dev->dev_private;
7246 int pipe = crtc->pipe;
7247
e3b95f1e
DV
7248 I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7249 I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
7250 I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
7251 I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
b551842d
DV
7252}
7253
7254static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
f769cd24
VK
7255 struct intel_link_m_n *m_n,
7256 struct intel_link_m_n *m2_n2)
b551842d
DV
7257{
7258 struct drm_device *dev = crtc->base.dev;
7259 struct drm_i915_private *dev_priv = dev->dev_private;
7260 int pipe = crtc->pipe;
6e3c9717 7261 enum transcoder transcoder = crtc->config->cpu_transcoder;
b551842d
DV
7262
7263 if (INTEL_INFO(dev)->gen >= 5) {
7264 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
7265 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
7266 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
7267 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
f769cd24
VK
7268 /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
7269 * for gen < 8) and if DRRS is supported (to make sure the
7270 * registers are not unnecessarily accessed).
7271 */
44395bfe 7272 if (m2_n2 && (IS_CHERRYVIEW(dev) || INTEL_INFO(dev)->gen < 8) &&
6e3c9717 7273 crtc->config->has_drrs) {
f769cd24
VK
7274 I915_WRITE(PIPE_DATA_M2(transcoder),
7275 TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
7276 I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
7277 I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
7278 I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
7279 }
b551842d 7280 } else {
e3b95f1e
DV
7281 I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7282 I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
7283 I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
7284 I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
b551842d
DV
7285 }
7286}
7287
fe3cd48d 7288void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
03afc4a2 7289{
fe3cd48d
R
7290 struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
7291
7292 if (m_n == M1_N1) {
7293 dp_m_n = &crtc->config->dp_m_n;
7294 dp_m2_n2 = &crtc->config->dp_m2_n2;
7295 } else if (m_n == M2_N2) {
7296
7297 /*
7298 * M2_N2 registers are not supported. Hence m2_n2 divider value
7299 * needs to be programmed into M1_N1.
7300 */
7301 dp_m_n = &crtc->config->dp_m2_n2;
7302 } else {
7303 DRM_ERROR("Unsupported divider value\n");
7304 return;
7305 }
7306
6e3c9717
ACO
7307 if (crtc->config->has_pch_encoder)
7308 intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
03afc4a2 7309 else
fe3cd48d 7310 intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
03afc4a2
DV
7311}
7312
251ac862
DV
7313static void vlv_compute_dpll(struct intel_crtc *crtc,
7314 struct intel_crtc_state *pipe_config)
bdd4b6a6 7315{
03ed5cbf 7316 pipe_config->dpll_hw_state.dpll = DPLL_INTEGRATED_REF_CLK_VLV |
cd2d34d9 7317 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
03ed5cbf
VS
7318 if (crtc->pipe != PIPE_A)
7319 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
bdd4b6a6 7320
cd2d34d9 7321 /* DPLL not used with DSI, but still need the rest set up */
187a1c07 7322 if (!pipe_config->has_dsi_encoder)
cd2d34d9
VS
7323 pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE |
7324 DPLL_EXT_BUFFER_ENABLE_VLV;
7325
03ed5cbf
VS
7326 pipe_config->dpll_hw_state.dpll_md =
7327 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
7328}
bdd4b6a6 7329
03ed5cbf
VS
7330static void chv_compute_dpll(struct intel_crtc *crtc,
7331 struct intel_crtc_state *pipe_config)
7332{
7333 pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLK_CHV |
cd2d34d9 7334 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
03ed5cbf
VS
7335 if (crtc->pipe != PIPE_A)
7336 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
7337
cd2d34d9 7338 /* DPLL not used with DSI, but still need the rest set up */
187a1c07 7339 if (!pipe_config->has_dsi_encoder)
cd2d34d9
VS
7340 pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE;
7341
03ed5cbf
VS
7342 pipe_config->dpll_hw_state.dpll_md =
7343 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
bdd4b6a6
DV
7344}
7345
d288f65f 7346static void vlv_prepare_pll(struct intel_crtc *crtc,
5cec258b 7347 const struct intel_crtc_state *pipe_config)
a0c4da24 7348{
f47709a9 7349 struct drm_device *dev = crtc->base.dev;
a0c4da24 7350 struct drm_i915_private *dev_priv = dev->dev_private;
cd2d34d9 7351 enum pipe pipe = crtc->pipe;
bdd4b6a6 7352 u32 mdiv;
a0c4da24 7353 u32 bestn, bestm1, bestm2, bestp1, bestp2;
bdd4b6a6 7354 u32 coreclk, reg_val;
a0c4da24 7355
cd2d34d9
VS
7356 /* Enable Refclk */
7357 I915_WRITE(DPLL(pipe),
7358 pipe_config->dpll_hw_state.dpll &
7359 ~(DPLL_VCO_ENABLE | DPLL_EXT_BUFFER_ENABLE_VLV));
7360
7361 /* No need to actually set up the DPLL with DSI */
7362 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
7363 return;
7364
a580516d 7365 mutex_lock(&dev_priv->sb_lock);
09153000 7366
d288f65f
VS
7367 bestn = pipe_config->dpll.n;
7368 bestm1 = pipe_config->dpll.m1;
7369 bestm2 = pipe_config->dpll.m2;
7370 bestp1 = pipe_config->dpll.p1;
7371 bestp2 = pipe_config->dpll.p2;
a0c4da24 7372
89b667f8
JB
7373 /* See eDP HDMI DPIO driver vbios notes doc */
7374
7375 /* PLL B needs special handling */
bdd4b6a6 7376 if (pipe == PIPE_B)
5e69f97f 7377 vlv_pllb_recal_opamp(dev_priv, pipe);
89b667f8
JB
7378
7379 /* Set up Tx target for periodic Rcomp update */
ab3c759a 7380 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
89b667f8
JB
7381
7382 /* Disable target IRef on PLL */
ab3c759a 7383 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
89b667f8 7384 reg_val &= 0x00ffffff;
ab3c759a 7385 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
89b667f8
JB
7386
7387 /* Disable fast lock */
ab3c759a 7388 vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
89b667f8
JB
7389
7390 /* Set idtafcrecal before PLL is enabled */
a0c4da24
JB
7391 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
7392 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
7393 mdiv |= ((bestn << DPIO_N_SHIFT));
a0c4da24 7394 mdiv |= (1 << DPIO_K_SHIFT);
7df5080b
JB
7395
7396 /*
7397 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
7398 * but we don't support that).
7399 * Note: don't use the DAC post divider as it seems unstable.
7400 */
7401 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
ab3c759a 7402 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
a0c4da24 7403
a0c4da24 7404 mdiv |= DPIO_ENABLE_CALIBRATION;
ab3c759a 7405 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
a0c4da24 7406
89b667f8 7407 /* Set HBR and RBR LPF coefficients */
d288f65f 7408 if (pipe_config->port_clock == 162000 ||
409ee761
ACO
7409 intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG) ||
7410 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
ab3c759a 7411 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
885b0120 7412 0x009f0003);
89b667f8 7413 else
ab3c759a 7414 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
89b667f8
JB
7415 0x00d0000f);
7416
681a8504 7417 if (pipe_config->has_dp_encoder) {
89b667f8 7418 /* Use SSC source */
bdd4b6a6 7419 if (pipe == PIPE_A)
ab3c759a 7420 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
7421 0x0df40000);
7422 else
ab3c759a 7423 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
7424 0x0df70000);
7425 } else { /* HDMI or VGA */
7426 /* Use bend source */
bdd4b6a6 7427 if (pipe == PIPE_A)
ab3c759a 7428 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
7429 0x0df70000);
7430 else
ab3c759a 7431 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
7432 0x0df40000);
7433 }
a0c4da24 7434
ab3c759a 7435 coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
89b667f8 7436 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
409ee761
ACO
7437 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
7438 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
89b667f8 7439 coreclk |= 0x01000000;
ab3c759a 7440 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
a0c4da24 7441
ab3c759a 7442 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
a580516d 7443 mutex_unlock(&dev_priv->sb_lock);
a0c4da24
JB
7444}
7445
d288f65f 7446static void chv_prepare_pll(struct intel_crtc *crtc,
5cec258b 7447 const struct intel_crtc_state *pipe_config)
9d556c99
CML
7448{
7449 struct drm_device *dev = crtc->base.dev;
7450 struct drm_i915_private *dev_priv = dev->dev_private;
cd2d34d9 7451 enum pipe pipe = crtc->pipe;
9d556c99 7452 enum dpio_channel port = vlv_pipe_to_channel(pipe);
9cbe40c1 7453 u32 loopfilter, tribuf_calcntr;
9d556c99 7454 u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
a945ce7e 7455 u32 dpio_val;
9cbe40c1 7456 int vco;
9d556c99 7457
cd2d34d9
VS
7458 /* Enable Refclk and SSC */
7459 I915_WRITE(DPLL(pipe),
7460 pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
7461
7462 /* No need to actually set up the DPLL with DSI */
7463 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
7464 return;
7465
d288f65f
VS
7466 bestn = pipe_config->dpll.n;
7467 bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
7468 bestm1 = pipe_config->dpll.m1;
7469 bestm2 = pipe_config->dpll.m2 >> 22;
7470 bestp1 = pipe_config->dpll.p1;
7471 bestp2 = pipe_config->dpll.p2;
9cbe40c1 7472 vco = pipe_config->dpll.vco;
a945ce7e 7473 dpio_val = 0;
9cbe40c1 7474 loopfilter = 0;
9d556c99 7475
a580516d 7476 mutex_lock(&dev_priv->sb_lock);
9d556c99 7477
9d556c99
CML
7478 /* p1 and p2 divider */
7479 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
7480 5 << DPIO_CHV_S1_DIV_SHIFT |
7481 bestp1 << DPIO_CHV_P1_DIV_SHIFT |
7482 bestp2 << DPIO_CHV_P2_DIV_SHIFT |
7483 1 << DPIO_CHV_K_DIV_SHIFT);
7484
7485 /* Feedback post-divider - m2 */
7486 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
7487
7488 /* Feedback refclk divider - n and m1 */
7489 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
7490 DPIO_CHV_M1_DIV_BY_2 |
7491 1 << DPIO_CHV_N_DIV_SHIFT);
7492
7493 /* M2 fraction division */
25a25dfc 7494 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
9d556c99
CML
7495
7496 /* M2 fraction division enable */
a945ce7e
VP
7497 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
7498 dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
7499 dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
7500 if (bestm2_frac)
7501 dpio_val |= DPIO_CHV_FRAC_DIV_EN;
7502 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
9d556c99 7503
de3a0fde
VP
7504 /* Program digital lock detect threshold */
7505 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
7506 dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
7507 DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
7508 dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
7509 if (!bestm2_frac)
7510 dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
7511 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
7512
9d556c99 7513 /* Loop filter */
9cbe40c1
VP
7514 if (vco == 5400000) {
7515 loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
7516 loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
7517 loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
7518 tribuf_calcntr = 0x9;
7519 } else if (vco <= 6200000) {
7520 loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
7521 loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
7522 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7523 tribuf_calcntr = 0x9;
7524 } else if (vco <= 6480000) {
7525 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7526 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7527 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7528 tribuf_calcntr = 0x8;
7529 } else {
7530 /* Not supported. Apply the same limits as in the max case */
7531 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7532 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7533 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7534 tribuf_calcntr = 0;
7535 }
9d556c99
CML
7536 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
7537
968040b2 7538 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
9cbe40c1
VP
7539 dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
7540 dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
7541 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
7542
9d556c99
CML
7543 /* AFC Recal */
7544 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
7545 vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
7546 DPIO_AFC_RECAL);
7547
a580516d 7548 mutex_unlock(&dev_priv->sb_lock);
9d556c99
CML
7549}
7550
d288f65f
VS
7551/**
7552 * vlv_force_pll_on - forcibly enable just the PLL
7553 * @dev_priv: i915 private structure
7554 * @pipe: pipe PLL to enable
7555 * @dpll: PLL configuration
7556 *
7557 * Enable the PLL for @pipe using the supplied @dpll config. To be used
7558 * in cases where we need the PLL enabled even when @pipe is not going to
7559 * be enabled.
7560 */
3f36b937
TU
7561int vlv_force_pll_on(struct drm_device *dev, enum pipe pipe,
7562 const struct dpll *dpll)
d288f65f
VS
7563{
7564 struct intel_crtc *crtc =
7565 to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
3f36b937
TU
7566 struct intel_crtc_state *pipe_config;
7567
7568 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
7569 if (!pipe_config)
7570 return -ENOMEM;
7571
7572 pipe_config->base.crtc = &crtc->base;
7573 pipe_config->pixel_multiplier = 1;
7574 pipe_config->dpll = *dpll;
d288f65f
VS
7575
7576 if (IS_CHERRYVIEW(dev)) {
3f36b937
TU
7577 chv_compute_dpll(crtc, pipe_config);
7578 chv_prepare_pll(crtc, pipe_config);
7579 chv_enable_pll(crtc, pipe_config);
d288f65f 7580 } else {
3f36b937
TU
7581 vlv_compute_dpll(crtc, pipe_config);
7582 vlv_prepare_pll(crtc, pipe_config);
7583 vlv_enable_pll(crtc, pipe_config);
d288f65f 7584 }
3f36b937
TU
7585
7586 kfree(pipe_config);
7587
7588 return 0;
d288f65f
VS
7589}
7590
7591/**
7592 * vlv_force_pll_off - forcibly disable just the PLL
7593 * @dev_priv: i915 private structure
7594 * @pipe: pipe PLL to disable
7595 *
7596 * Disable the PLL for @pipe. To be used in cases where we need
7597 * the PLL enabled even when @pipe is not going to be enabled.
7598 */
7599void vlv_force_pll_off(struct drm_device *dev, enum pipe pipe)
7600{
7601 if (IS_CHERRYVIEW(dev))
7602 chv_disable_pll(to_i915(dev), pipe);
7603 else
7604 vlv_disable_pll(to_i915(dev), pipe);
7605}
7606
251ac862
DV
7607static void i9xx_compute_dpll(struct intel_crtc *crtc,
7608 struct intel_crtc_state *crtc_state,
9e2c8475 7609 struct dpll *reduced_clock)
eb1cbe48 7610{
f47709a9 7611 struct drm_device *dev = crtc->base.dev;
eb1cbe48 7612 struct drm_i915_private *dev_priv = dev->dev_private;
eb1cbe48
DV
7613 u32 dpll;
7614 bool is_sdvo;
190f68c5 7615 struct dpll *clock = &crtc_state->dpll;
eb1cbe48 7616
190f68c5 7617 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
2a8f64ca 7618
a93e255f
ACO
7619 is_sdvo = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO) ||
7620 intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI);
eb1cbe48
DV
7621
7622 dpll = DPLL_VGA_MODE_DIS;
7623
a93e255f 7624 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
eb1cbe48
DV
7625 dpll |= DPLLB_MODE_LVDS;
7626 else
7627 dpll |= DPLLB_MODE_DAC_SERIAL;
6cc5f341 7628
ef1b460d 7629 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
190f68c5 7630 dpll |= (crtc_state->pixel_multiplier - 1)
198a037f 7631 << SDVO_MULTIPLIER_SHIFT_HIRES;
eb1cbe48 7632 }
198a037f
DV
7633
7634 if (is_sdvo)
4a33e48d 7635 dpll |= DPLL_SDVO_HIGH_SPEED;
198a037f 7636
190f68c5 7637 if (crtc_state->has_dp_encoder)
4a33e48d 7638 dpll |= DPLL_SDVO_HIGH_SPEED;
eb1cbe48
DV
7639
7640 /* compute bitmask from p1 value */
7641 if (IS_PINEVIEW(dev))
7642 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
7643 else {
7644 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7645 if (IS_G4X(dev) && reduced_clock)
7646 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
7647 }
7648 switch (clock->p2) {
7649 case 5:
7650 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
7651 break;
7652 case 7:
7653 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
7654 break;
7655 case 10:
7656 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
7657 break;
7658 case 14:
7659 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
7660 break;
7661 }
7662 if (INTEL_INFO(dev)->gen >= 4)
7663 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
7664
190f68c5 7665 if (crtc_state->sdvo_tv_clock)
eb1cbe48 7666 dpll |= PLL_REF_INPUT_TVCLKINBC;
a93e255f 7667 else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
ceb41007 7668 intel_panel_use_ssc(dev_priv))
eb1cbe48
DV
7669 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7670 else
7671 dpll |= PLL_REF_INPUT_DREFCLK;
7672
7673 dpll |= DPLL_VCO_ENABLE;
190f68c5 7674 crtc_state->dpll_hw_state.dpll = dpll;
8bcc2795 7675
eb1cbe48 7676 if (INTEL_INFO(dev)->gen >= 4) {
190f68c5 7677 u32 dpll_md = (crtc_state->pixel_multiplier - 1)
ef1b460d 7678 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
190f68c5 7679 crtc_state->dpll_hw_state.dpll_md = dpll_md;
eb1cbe48
DV
7680 }
7681}
7682
251ac862
DV
7683static void i8xx_compute_dpll(struct intel_crtc *crtc,
7684 struct intel_crtc_state *crtc_state,
9e2c8475 7685 struct dpll *reduced_clock)
eb1cbe48 7686{
f47709a9 7687 struct drm_device *dev = crtc->base.dev;
eb1cbe48 7688 struct drm_i915_private *dev_priv = dev->dev_private;
eb1cbe48 7689 u32 dpll;
190f68c5 7690 struct dpll *clock = &crtc_state->dpll;
eb1cbe48 7691
190f68c5 7692 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
2a8f64ca 7693
eb1cbe48
DV
7694 dpll = DPLL_VGA_MODE_DIS;
7695
a93e255f 7696 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
eb1cbe48
DV
7697 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7698 } else {
7699 if (clock->p1 == 2)
7700 dpll |= PLL_P1_DIVIDE_BY_TWO;
7701 else
7702 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7703 if (clock->p2 == 4)
7704 dpll |= PLL_P2_DIVIDE_BY_4;
7705 }
7706
a93e255f 7707 if (!IS_I830(dev) && intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
4a33e48d
DV
7708 dpll |= DPLL_DVO_2X_MODE;
7709
a93e255f 7710 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
ceb41007 7711 intel_panel_use_ssc(dev_priv))
eb1cbe48
DV
7712 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7713 else
7714 dpll |= PLL_REF_INPUT_DREFCLK;
7715
7716 dpll |= DPLL_VCO_ENABLE;
190f68c5 7717 crtc_state->dpll_hw_state.dpll = dpll;
eb1cbe48
DV
7718}
7719
8a654f3b 7720static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
b0e77b9c
PZ
7721{
7722 struct drm_device *dev = intel_crtc->base.dev;
7723 struct drm_i915_private *dev_priv = dev->dev_private;
7724 enum pipe pipe = intel_crtc->pipe;
6e3c9717 7725 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
7c5f93b0 7726 const struct drm_display_mode *adjusted_mode = &intel_crtc->config->base.adjusted_mode;
1caea6e9
VS
7727 uint32_t crtc_vtotal, crtc_vblank_end;
7728 int vsyncshift = 0;
4d8a62ea
DV
7729
7730 /* We need to be careful not to changed the adjusted mode, for otherwise
7731 * the hw state checker will get angry at the mismatch. */
7732 crtc_vtotal = adjusted_mode->crtc_vtotal;
7733 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
b0e77b9c 7734
609aeaca 7735 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
b0e77b9c 7736 /* the chip adds 2 halflines automatically */
4d8a62ea
DV
7737 crtc_vtotal -= 1;
7738 crtc_vblank_end -= 1;
609aeaca 7739
409ee761 7740 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
609aeaca
VS
7741 vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
7742 else
7743 vsyncshift = adjusted_mode->crtc_hsync_start -
7744 adjusted_mode->crtc_htotal / 2;
1caea6e9
VS
7745 if (vsyncshift < 0)
7746 vsyncshift += adjusted_mode->crtc_htotal;
b0e77b9c
PZ
7747 }
7748
7749 if (INTEL_INFO(dev)->gen > 3)
fe2b8f9d 7750 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
b0e77b9c 7751
fe2b8f9d 7752 I915_WRITE(HTOTAL(cpu_transcoder),
b0e77b9c
PZ
7753 (adjusted_mode->crtc_hdisplay - 1) |
7754 ((adjusted_mode->crtc_htotal - 1) << 16));
fe2b8f9d 7755 I915_WRITE(HBLANK(cpu_transcoder),
b0e77b9c
PZ
7756 (adjusted_mode->crtc_hblank_start - 1) |
7757 ((adjusted_mode->crtc_hblank_end - 1) << 16));
fe2b8f9d 7758 I915_WRITE(HSYNC(cpu_transcoder),
b0e77b9c
PZ
7759 (adjusted_mode->crtc_hsync_start - 1) |
7760 ((adjusted_mode->crtc_hsync_end - 1) << 16));
7761
fe2b8f9d 7762 I915_WRITE(VTOTAL(cpu_transcoder),
b0e77b9c 7763 (adjusted_mode->crtc_vdisplay - 1) |
4d8a62ea 7764 ((crtc_vtotal - 1) << 16));
fe2b8f9d 7765 I915_WRITE(VBLANK(cpu_transcoder),
b0e77b9c 7766 (adjusted_mode->crtc_vblank_start - 1) |
4d8a62ea 7767 ((crtc_vblank_end - 1) << 16));
fe2b8f9d 7768 I915_WRITE(VSYNC(cpu_transcoder),
b0e77b9c
PZ
7769 (adjusted_mode->crtc_vsync_start - 1) |
7770 ((adjusted_mode->crtc_vsync_end - 1) << 16));
7771
b5e508d4
PZ
7772 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
7773 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
7774 * documented on the DDI_FUNC_CTL register description, EDP Input Select
7775 * bits. */
7776 if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
7777 (pipe == PIPE_B || pipe == PIPE_C))
7778 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
7779
bc58be60
JN
7780}
7781
7782static void intel_set_pipe_src_size(struct intel_crtc *intel_crtc)
7783{
7784 struct drm_device *dev = intel_crtc->base.dev;
7785 struct drm_i915_private *dev_priv = dev->dev_private;
7786 enum pipe pipe = intel_crtc->pipe;
7787
b0e77b9c
PZ
7788 /* pipesrc controls the size that is scaled from, which should
7789 * always be the user's requested size.
7790 */
7791 I915_WRITE(PIPESRC(pipe),
6e3c9717
ACO
7792 ((intel_crtc->config->pipe_src_w - 1) << 16) |
7793 (intel_crtc->config->pipe_src_h - 1));
b0e77b9c
PZ
7794}
7795
1bd1bd80 7796static void intel_get_pipe_timings(struct intel_crtc *crtc,
5cec258b 7797 struct intel_crtc_state *pipe_config)
1bd1bd80
DV
7798{
7799 struct drm_device *dev = crtc->base.dev;
7800 struct drm_i915_private *dev_priv = dev->dev_private;
7801 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
7802 uint32_t tmp;
7803
7804 tmp = I915_READ(HTOTAL(cpu_transcoder));
2d112de7
ACO
7805 pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
7806 pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 7807 tmp = I915_READ(HBLANK(cpu_transcoder));
2d112de7
ACO
7808 pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
7809 pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 7810 tmp = I915_READ(HSYNC(cpu_transcoder));
2d112de7
ACO
7811 pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
7812 pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80
DV
7813
7814 tmp = I915_READ(VTOTAL(cpu_transcoder));
2d112de7
ACO
7815 pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
7816 pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 7817 tmp = I915_READ(VBLANK(cpu_transcoder));
2d112de7
ACO
7818 pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
7819 pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 7820 tmp = I915_READ(VSYNC(cpu_transcoder));
2d112de7
ACO
7821 pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
7822 pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80
DV
7823
7824 if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
2d112de7
ACO
7825 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
7826 pipe_config->base.adjusted_mode.crtc_vtotal += 1;
7827 pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
1bd1bd80 7828 }
bc58be60
JN
7829}
7830
7831static void intel_get_pipe_src_size(struct intel_crtc *crtc,
7832 struct intel_crtc_state *pipe_config)
7833{
7834 struct drm_device *dev = crtc->base.dev;
7835 struct drm_i915_private *dev_priv = dev->dev_private;
7836 u32 tmp;
1bd1bd80
DV
7837
7838 tmp = I915_READ(PIPESRC(crtc->pipe));
37327abd
VS
7839 pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
7840 pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
7841
2d112de7
ACO
7842 pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
7843 pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
1bd1bd80
DV
7844}
7845
f6a83288 7846void intel_mode_from_pipe_config(struct drm_display_mode *mode,
5cec258b 7847 struct intel_crtc_state *pipe_config)
babea61d 7848{
2d112de7
ACO
7849 mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
7850 mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
7851 mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
7852 mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
babea61d 7853
2d112de7
ACO
7854 mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
7855 mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
7856 mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
7857 mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
babea61d 7858
2d112de7 7859 mode->flags = pipe_config->base.adjusted_mode.flags;
cd13f5ab 7860 mode->type = DRM_MODE_TYPE_DRIVER;
babea61d 7861
2d112de7
ACO
7862 mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
7863 mode->flags |= pipe_config->base.adjusted_mode.flags;
cd13f5ab
ML
7864
7865 mode->hsync = drm_mode_hsync(mode);
7866 mode->vrefresh = drm_mode_vrefresh(mode);
7867 drm_mode_set_name(mode);
babea61d
JB
7868}
7869
84b046f3
DV
7870static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
7871{
7872 struct drm_device *dev = intel_crtc->base.dev;
7873 struct drm_i915_private *dev_priv = dev->dev_private;
7874 uint32_t pipeconf;
7875
9f11a9e4 7876 pipeconf = 0;
84b046f3 7877
b6b5d049
VS
7878 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
7879 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
7880 pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
67c72a12 7881
6e3c9717 7882 if (intel_crtc->config->double_wide)
cf532bb2 7883 pipeconf |= PIPECONF_DOUBLE_WIDE;
84b046f3 7884
ff9ce46e 7885 /* only g4x and later have fancy bpc/dither controls */
666a4537 7886 if (IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
ff9ce46e 7887 /* Bspec claims that we can't use dithering for 30bpp pipes. */
6e3c9717 7888 if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
ff9ce46e 7889 pipeconf |= PIPECONF_DITHER_EN |
84b046f3 7890 PIPECONF_DITHER_TYPE_SP;
84b046f3 7891
6e3c9717 7892 switch (intel_crtc->config->pipe_bpp) {
ff9ce46e
DV
7893 case 18:
7894 pipeconf |= PIPECONF_6BPC;
7895 break;
7896 case 24:
7897 pipeconf |= PIPECONF_8BPC;
7898 break;
7899 case 30:
7900 pipeconf |= PIPECONF_10BPC;
7901 break;
7902 default:
7903 /* Case prevented by intel_choose_pipe_bpp_dither. */
7904 BUG();
84b046f3
DV
7905 }
7906 }
7907
7908 if (HAS_PIPE_CXSR(dev)) {
7909 if (intel_crtc->lowfreq_avail) {
7910 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
7911 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
7912 } else {
7913 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
84b046f3
DV
7914 }
7915 }
7916
6e3c9717 7917 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
efc2cfff 7918 if (INTEL_INFO(dev)->gen < 4 ||
409ee761 7919 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
efc2cfff
VS
7920 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
7921 else
7922 pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
7923 } else
84b046f3
DV
7924 pipeconf |= PIPECONF_PROGRESSIVE;
7925
666a4537
WB
7926 if ((IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) &&
7927 intel_crtc->config->limited_color_range)
9f11a9e4 7928 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
9c8e09b7 7929
84b046f3
DV
7930 I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
7931 POSTING_READ(PIPECONF(intel_crtc->pipe));
7932}
7933
81c97f52
ACO
7934static int i8xx_crtc_compute_clock(struct intel_crtc *crtc,
7935 struct intel_crtc_state *crtc_state)
7936{
7937 struct drm_device *dev = crtc->base.dev;
7938 struct drm_i915_private *dev_priv = dev->dev_private;
1b6f4958 7939 const struct intel_limit *limit;
81c97f52
ACO
7940 int refclk = 48000;
7941
7942 memset(&crtc_state->dpll_hw_state, 0,
7943 sizeof(crtc_state->dpll_hw_state));
7944
7945 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
7946 if (intel_panel_use_ssc(dev_priv)) {
7947 refclk = dev_priv->vbt.lvds_ssc_freq;
7948 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
7949 }
7950
7951 limit = &intel_limits_i8xx_lvds;
7952 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO)) {
7953 limit = &intel_limits_i8xx_dvo;
7954 } else {
7955 limit = &intel_limits_i8xx_dac;
7956 }
7957
7958 if (!crtc_state->clock_set &&
7959 !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7960 refclk, NULL, &crtc_state->dpll)) {
7961 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7962 return -EINVAL;
7963 }
7964
7965 i8xx_compute_dpll(crtc, crtc_state, NULL);
7966
7967 return 0;
7968}
7969
19ec6693
ACO
7970static int g4x_crtc_compute_clock(struct intel_crtc *crtc,
7971 struct intel_crtc_state *crtc_state)
7972{
7973 struct drm_device *dev = crtc->base.dev;
7974 struct drm_i915_private *dev_priv = dev->dev_private;
1b6f4958 7975 const struct intel_limit *limit;
19ec6693
ACO
7976 int refclk = 96000;
7977
7978 memset(&crtc_state->dpll_hw_state, 0,
7979 sizeof(crtc_state->dpll_hw_state));
7980
7981 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
7982 if (intel_panel_use_ssc(dev_priv)) {
7983 refclk = dev_priv->vbt.lvds_ssc_freq;
7984 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
7985 }
7986
7987 if (intel_is_dual_link_lvds(dev))
7988 limit = &intel_limits_g4x_dual_channel_lvds;
7989 else
7990 limit = &intel_limits_g4x_single_channel_lvds;
7991 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI) ||
7992 intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
7993 limit = &intel_limits_g4x_hdmi;
7994 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO)) {
7995 limit = &intel_limits_g4x_sdvo;
7996 } else {
7997 /* The option is for other outputs */
7998 limit = &intel_limits_i9xx_sdvo;
7999 }
8000
8001 if (!crtc_state->clock_set &&
8002 !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8003 refclk, NULL, &crtc_state->dpll)) {
8004 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8005 return -EINVAL;
8006 }
8007
8008 i9xx_compute_dpll(crtc, crtc_state, NULL);
8009
8010 return 0;
8011}
8012
70e8aa21
ACO
8013static int pnv_crtc_compute_clock(struct intel_crtc *crtc,
8014 struct intel_crtc_state *crtc_state)
8015{
8016 struct drm_device *dev = crtc->base.dev;
8017 struct drm_i915_private *dev_priv = dev->dev_private;
1b6f4958 8018 const struct intel_limit *limit;
70e8aa21
ACO
8019 int refclk = 96000;
8020
8021 memset(&crtc_state->dpll_hw_state, 0,
8022 sizeof(crtc_state->dpll_hw_state));
8023
8024 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
8025 if (intel_panel_use_ssc(dev_priv)) {
8026 refclk = dev_priv->vbt.lvds_ssc_freq;
8027 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
8028 }
8029
8030 limit = &intel_limits_pineview_lvds;
8031 } else {
8032 limit = &intel_limits_pineview_sdvo;
8033 }
8034
8035 if (!crtc_state->clock_set &&
8036 !pnv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8037 refclk, NULL, &crtc_state->dpll)) {
8038 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8039 return -EINVAL;
8040 }
8041
8042 i9xx_compute_dpll(crtc, crtc_state, NULL);
8043
8044 return 0;
8045}
8046
190f68c5
ACO
8047static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
8048 struct intel_crtc_state *crtc_state)
79e53945 8049{
c7653199 8050 struct drm_device *dev = crtc->base.dev;
79e53945 8051 struct drm_i915_private *dev_priv = dev->dev_private;
1b6f4958 8052 const struct intel_limit *limit;
81c97f52 8053 int refclk = 96000;
79e53945 8054
dd3cd74a
ACO
8055 memset(&crtc_state->dpll_hw_state, 0,
8056 sizeof(crtc_state->dpll_hw_state));
8057
70e8aa21
ACO
8058 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
8059 if (intel_panel_use_ssc(dev_priv)) {
8060 refclk = dev_priv->vbt.lvds_ssc_freq;
8061 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
8062 }
43565a06 8063
70e8aa21
ACO
8064 limit = &intel_limits_i9xx_lvds;
8065 } else {
8066 limit = &intel_limits_i9xx_sdvo;
81c97f52 8067 }
79e53945 8068
70e8aa21
ACO
8069 if (!crtc_state->clock_set &&
8070 !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8071 refclk, NULL, &crtc_state->dpll)) {
8072 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8073 return -EINVAL;
f47709a9 8074 }
7026d4ac 8075
81c97f52 8076 i9xx_compute_dpll(crtc, crtc_state, NULL);
79e53945 8077
c8f7a0db 8078 return 0;
f564048e
EA
8079}
8080
65b3d6a9
ACO
8081static int chv_crtc_compute_clock(struct intel_crtc *crtc,
8082 struct intel_crtc_state *crtc_state)
8083{
8084 int refclk = 100000;
1b6f4958 8085 const struct intel_limit *limit = &intel_limits_chv;
65b3d6a9
ACO
8086
8087 memset(&crtc_state->dpll_hw_state, 0,
8088 sizeof(crtc_state->dpll_hw_state));
8089
65b3d6a9
ACO
8090 if (!crtc_state->clock_set &&
8091 !chv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8092 refclk, NULL, &crtc_state->dpll)) {
8093 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8094 return -EINVAL;
8095 }
8096
8097 chv_compute_dpll(crtc, crtc_state);
8098
8099 return 0;
8100}
8101
8102static int vlv_crtc_compute_clock(struct intel_crtc *crtc,
8103 struct intel_crtc_state *crtc_state)
8104{
8105 int refclk = 100000;
1b6f4958 8106 const struct intel_limit *limit = &intel_limits_vlv;
65b3d6a9
ACO
8107
8108 memset(&crtc_state->dpll_hw_state, 0,
8109 sizeof(crtc_state->dpll_hw_state));
8110
65b3d6a9
ACO
8111 if (!crtc_state->clock_set &&
8112 !vlv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8113 refclk, NULL, &crtc_state->dpll)) {
8114 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8115 return -EINVAL;
8116 }
8117
8118 vlv_compute_dpll(crtc, crtc_state);
8119
8120 return 0;
8121}
8122
2fa2fe9a 8123static void i9xx_get_pfit_config(struct intel_crtc *crtc,
5cec258b 8124 struct intel_crtc_state *pipe_config)
2fa2fe9a
DV
8125{
8126 struct drm_device *dev = crtc->base.dev;
8127 struct drm_i915_private *dev_priv = dev->dev_private;
8128 uint32_t tmp;
8129
dc9e7dec
VS
8130 if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
8131 return;
8132
2fa2fe9a 8133 tmp = I915_READ(PFIT_CONTROL);
06922821
DV
8134 if (!(tmp & PFIT_ENABLE))
8135 return;
2fa2fe9a 8136
06922821 8137 /* Check whether the pfit is attached to our pipe. */
2fa2fe9a
DV
8138 if (INTEL_INFO(dev)->gen < 4) {
8139 if (crtc->pipe != PIPE_B)
8140 return;
2fa2fe9a
DV
8141 } else {
8142 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
8143 return;
8144 }
8145
06922821 8146 pipe_config->gmch_pfit.control = tmp;
2fa2fe9a 8147 pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
2fa2fe9a
DV
8148}
8149
acbec814 8150static void vlv_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 8151 struct intel_crtc_state *pipe_config)
acbec814
JB
8152{
8153 struct drm_device *dev = crtc->base.dev;
8154 struct drm_i915_private *dev_priv = dev->dev_private;
8155 int pipe = pipe_config->cpu_transcoder;
9e2c8475 8156 struct dpll clock;
acbec814 8157 u32 mdiv;
662c6ecb 8158 int refclk = 100000;
acbec814 8159
b521973b
VS
8160 /* In case of DSI, DPLL will not be used */
8161 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
f573de5a
SK
8162 return;
8163
a580516d 8164 mutex_lock(&dev_priv->sb_lock);
ab3c759a 8165 mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
a580516d 8166 mutex_unlock(&dev_priv->sb_lock);
acbec814
JB
8167
8168 clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
8169 clock.m2 = mdiv & DPIO_M2DIV_MASK;
8170 clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
8171 clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
8172 clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
8173
dccbea3b 8174 pipe_config->port_clock = vlv_calc_dpll_params(refclk, &clock);
acbec814
JB
8175}
8176
5724dbd1
DL
8177static void
8178i9xx_get_initial_plane_config(struct intel_crtc *crtc,
8179 struct intel_initial_plane_config *plane_config)
1ad292b5
JB
8180{
8181 struct drm_device *dev = crtc->base.dev;
8182 struct drm_i915_private *dev_priv = dev->dev_private;
8183 u32 val, base, offset;
8184 int pipe = crtc->pipe, plane = crtc->plane;
8185 int fourcc, pixel_format;
6761dd31 8186 unsigned int aligned_height;
b113d5ee 8187 struct drm_framebuffer *fb;
1b842c89 8188 struct intel_framebuffer *intel_fb;
1ad292b5 8189
42a7b088
DL
8190 val = I915_READ(DSPCNTR(plane));
8191 if (!(val & DISPLAY_PLANE_ENABLE))
8192 return;
8193
d9806c9f 8194 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
1b842c89 8195 if (!intel_fb) {
1ad292b5
JB
8196 DRM_DEBUG_KMS("failed to alloc fb\n");
8197 return;
8198 }
8199
1b842c89
DL
8200 fb = &intel_fb->base;
8201
18c5247e
DV
8202 if (INTEL_INFO(dev)->gen >= 4) {
8203 if (val & DISPPLANE_TILED) {
49af449b 8204 plane_config->tiling = I915_TILING_X;
18c5247e
DV
8205 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
8206 }
8207 }
1ad292b5
JB
8208
8209 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
b35d63fa 8210 fourcc = i9xx_format_to_fourcc(pixel_format);
b113d5ee
DL
8211 fb->pixel_format = fourcc;
8212 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
1ad292b5
JB
8213
8214 if (INTEL_INFO(dev)->gen >= 4) {
49af449b 8215 if (plane_config->tiling)
1ad292b5
JB
8216 offset = I915_READ(DSPTILEOFF(plane));
8217 else
8218 offset = I915_READ(DSPLINOFF(plane));
8219 base = I915_READ(DSPSURF(plane)) & 0xfffff000;
8220 } else {
8221 base = I915_READ(DSPADDR(plane));
8222 }
8223 plane_config->base = base;
8224
8225 val = I915_READ(PIPESRC(pipe));
b113d5ee
DL
8226 fb->width = ((val >> 16) & 0xfff) + 1;
8227 fb->height = ((val >> 0) & 0xfff) + 1;
1ad292b5
JB
8228
8229 val = I915_READ(DSPSTRIDE(pipe));
b113d5ee 8230 fb->pitches[0] = val & 0xffffffc0;
1ad292b5 8231
b113d5ee 8232 aligned_height = intel_fb_align_height(dev, fb->height,
091df6cb
DV
8233 fb->pixel_format,
8234 fb->modifier[0]);
1ad292b5 8235
f37b5c2b 8236 plane_config->size = fb->pitches[0] * aligned_height;
1ad292b5 8237
2844a921
DL
8238 DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
8239 pipe_name(pipe), plane, fb->width, fb->height,
8240 fb->bits_per_pixel, base, fb->pitches[0],
8241 plane_config->size);
1ad292b5 8242
2d14030b 8243 plane_config->fb = intel_fb;
1ad292b5
JB
8244}
8245
70b23a98 8246static void chv_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 8247 struct intel_crtc_state *pipe_config)
70b23a98
VS
8248{
8249 struct drm_device *dev = crtc->base.dev;
8250 struct drm_i915_private *dev_priv = dev->dev_private;
8251 int pipe = pipe_config->cpu_transcoder;
8252 enum dpio_channel port = vlv_pipe_to_channel(pipe);
9e2c8475 8253 struct dpll clock;
0d7b6b11 8254 u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2, pll_dw3;
70b23a98
VS
8255 int refclk = 100000;
8256
b521973b
VS
8257 /* In case of DSI, DPLL will not be used */
8258 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
8259 return;
8260
a580516d 8261 mutex_lock(&dev_priv->sb_lock);
70b23a98
VS
8262 cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
8263 pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
8264 pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
8265 pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
0d7b6b11 8266 pll_dw3 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
a580516d 8267 mutex_unlock(&dev_priv->sb_lock);
70b23a98
VS
8268
8269 clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
0d7b6b11
ID
8270 clock.m2 = (pll_dw0 & 0xff) << 22;
8271 if (pll_dw3 & DPIO_CHV_FRAC_DIV_EN)
8272 clock.m2 |= pll_dw2 & 0x3fffff;
70b23a98
VS
8273 clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
8274 clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
8275 clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
8276
dccbea3b 8277 pipe_config->port_clock = chv_calc_dpll_params(refclk, &clock);
70b23a98
VS
8278}
8279
0e8ffe1b 8280static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
5cec258b 8281 struct intel_crtc_state *pipe_config)
0e8ffe1b
DV
8282{
8283 struct drm_device *dev = crtc->base.dev;
8284 struct drm_i915_private *dev_priv = dev->dev_private;
1729050e 8285 enum intel_display_power_domain power_domain;
0e8ffe1b 8286 uint32_t tmp;
1729050e 8287 bool ret;
0e8ffe1b 8288
1729050e
ID
8289 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
8290 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
b5482bd0
ID
8291 return false;
8292
e143a21c 8293 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
8106ddbd 8294 pipe_config->shared_dpll = NULL;
eccb140b 8295
1729050e
ID
8296 ret = false;
8297
0e8ffe1b
DV
8298 tmp = I915_READ(PIPECONF(crtc->pipe));
8299 if (!(tmp & PIPECONF_ENABLE))
1729050e 8300 goto out;
0e8ffe1b 8301
666a4537 8302 if (IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
42571aef
VS
8303 switch (tmp & PIPECONF_BPC_MASK) {
8304 case PIPECONF_6BPC:
8305 pipe_config->pipe_bpp = 18;
8306 break;
8307 case PIPECONF_8BPC:
8308 pipe_config->pipe_bpp = 24;
8309 break;
8310 case PIPECONF_10BPC:
8311 pipe_config->pipe_bpp = 30;
8312 break;
8313 default:
8314 break;
8315 }
8316 }
8317
666a4537
WB
8318 if ((IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) &&
8319 (tmp & PIPECONF_COLOR_RANGE_SELECT))
b5a9fa09
DV
8320 pipe_config->limited_color_range = true;
8321
282740f7
VS
8322 if (INTEL_INFO(dev)->gen < 4)
8323 pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
8324
1bd1bd80 8325 intel_get_pipe_timings(crtc, pipe_config);
bc58be60 8326 intel_get_pipe_src_size(crtc, pipe_config);
1bd1bd80 8327
2fa2fe9a
DV
8328 i9xx_get_pfit_config(crtc, pipe_config);
8329
6c49f241 8330 if (INTEL_INFO(dev)->gen >= 4) {
c231775c
VS
8331 /* No way to read it out on pipes B and C */
8332 if (IS_CHERRYVIEW(dev) && crtc->pipe != PIPE_A)
8333 tmp = dev_priv->chv_dpll_md[crtc->pipe];
8334 else
8335 tmp = I915_READ(DPLL_MD(crtc->pipe));
6c49f241
DV
8336 pipe_config->pixel_multiplier =
8337 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
8338 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
8bcc2795 8339 pipe_config->dpll_hw_state.dpll_md = tmp;
6c49f241
DV
8340 } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
8341 tmp = I915_READ(DPLL(crtc->pipe));
8342 pipe_config->pixel_multiplier =
8343 ((tmp & SDVO_MULTIPLIER_MASK)
8344 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
8345 } else {
8346 /* Note that on i915G/GM the pixel multiplier is in the sdvo
8347 * port and will be fixed up in the encoder->get_config
8348 * function. */
8349 pipe_config->pixel_multiplier = 1;
8350 }
8bcc2795 8351 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
666a4537 8352 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
1c4e0274
VS
8353 /*
8354 * DPLL_DVO_2X_MODE must be enabled for both DPLLs
8355 * on 830. Filter it out here so that we don't
8356 * report errors due to that.
8357 */
8358 if (IS_I830(dev))
8359 pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
8360
8bcc2795
DV
8361 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
8362 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
165e901c
VS
8363 } else {
8364 /* Mask out read-only status bits. */
8365 pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
8366 DPLL_PORTC_READY_MASK |
8367 DPLL_PORTB_READY_MASK);
8bcc2795 8368 }
6c49f241 8369
70b23a98
VS
8370 if (IS_CHERRYVIEW(dev))
8371 chv_crtc_clock_get(crtc, pipe_config);
8372 else if (IS_VALLEYVIEW(dev))
acbec814
JB
8373 vlv_crtc_clock_get(crtc, pipe_config);
8374 else
8375 i9xx_crtc_clock_get(crtc, pipe_config);
18442d08 8376
0f64614d
VS
8377 /*
8378 * Normally the dotclock is filled in by the encoder .get_config()
8379 * but in case the pipe is enabled w/o any ports we need a sane
8380 * default.
8381 */
8382 pipe_config->base.adjusted_mode.crtc_clock =
8383 pipe_config->port_clock / pipe_config->pixel_multiplier;
8384
1729050e
ID
8385 ret = true;
8386
8387out:
8388 intel_display_power_put(dev_priv, power_domain);
8389
8390 return ret;
0e8ffe1b
DV
8391}
8392
dde86e2d 8393static void ironlake_init_pch_refclk(struct drm_device *dev)
13d83a67
JB
8394{
8395 struct drm_i915_private *dev_priv = dev->dev_private;
13d83a67 8396 struct intel_encoder *encoder;
1c1a24d2 8397 int i;
74cfd7ac 8398 u32 val, final;
13d83a67 8399 bool has_lvds = false;
199e5d79 8400 bool has_cpu_edp = false;
199e5d79 8401 bool has_panel = false;
99eb6a01
KP
8402 bool has_ck505 = false;
8403 bool can_ssc = false;
1c1a24d2 8404 bool using_ssc_source = false;
13d83a67
JB
8405
8406 /* We need to take the global config into account */
b2784e15 8407 for_each_intel_encoder(dev, encoder) {
199e5d79
KP
8408 switch (encoder->type) {
8409 case INTEL_OUTPUT_LVDS:
8410 has_panel = true;
8411 has_lvds = true;
8412 break;
8413 case INTEL_OUTPUT_EDP:
8414 has_panel = true;
2de6905f 8415 if (enc_to_dig_port(&encoder->base)->port == PORT_A)
199e5d79
KP
8416 has_cpu_edp = true;
8417 break;
6847d71b
PZ
8418 default:
8419 break;
13d83a67
JB
8420 }
8421 }
8422
99eb6a01 8423 if (HAS_PCH_IBX(dev)) {
41aa3448 8424 has_ck505 = dev_priv->vbt.display_clock_mode;
99eb6a01
KP
8425 can_ssc = has_ck505;
8426 } else {
8427 has_ck505 = false;
8428 can_ssc = true;
8429 }
8430
1c1a24d2
L
8431 /* Check if any DPLLs are using the SSC source */
8432 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
8433 u32 temp = I915_READ(PCH_DPLL(i));
8434
8435 if (!(temp & DPLL_VCO_ENABLE))
8436 continue;
8437
8438 if ((temp & PLL_REF_INPUT_MASK) ==
8439 PLLB_REF_INPUT_SPREADSPECTRUMIN) {
8440 using_ssc_source = true;
8441 break;
8442 }
8443 }
8444
8445 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d using_ssc_source %d\n",
8446 has_panel, has_lvds, has_ck505, using_ssc_source);
13d83a67
JB
8447
8448 /* Ironlake: try to setup display ref clock before DPLL
8449 * enabling. This is only under driver's control after
8450 * PCH B stepping, previous chipset stepping should be
8451 * ignoring this setting.
8452 */
74cfd7ac
CW
8453 val = I915_READ(PCH_DREF_CONTROL);
8454
8455 /* As we must carefully and slowly disable/enable each source in turn,
8456 * compute the final state we want first and check if we need to
8457 * make any changes at all.
8458 */
8459 final = val;
8460 final &= ~DREF_NONSPREAD_SOURCE_MASK;
8461 if (has_ck505)
8462 final |= DREF_NONSPREAD_CK505_ENABLE;
8463 else
8464 final |= DREF_NONSPREAD_SOURCE_ENABLE;
8465
8c07eb68 8466 final &= ~DREF_SSC_SOURCE_MASK;
74cfd7ac 8467 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
8c07eb68 8468 final &= ~DREF_SSC1_ENABLE;
74cfd7ac
CW
8469
8470 if (has_panel) {
8471 final |= DREF_SSC_SOURCE_ENABLE;
8472
8473 if (intel_panel_use_ssc(dev_priv) && can_ssc)
8474 final |= DREF_SSC1_ENABLE;
8475
8476 if (has_cpu_edp) {
8477 if (intel_panel_use_ssc(dev_priv) && can_ssc)
8478 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
8479 else
8480 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
8481 } else
8482 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
1c1a24d2
L
8483 } else if (using_ssc_source) {
8484 final |= DREF_SSC_SOURCE_ENABLE;
8485 final |= DREF_SSC1_ENABLE;
74cfd7ac
CW
8486 }
8487
8488 if (final == val)
8489 return;
8490
13d83a67 8491 /* Always enable nonspread source */
74cfd7ac 8492 val &= ~DREF_NONSPREAD_SOURCE_MASK;
13d83a67 8493
99eb6a01 8494 if (has_ck505)
74cfd7ac 8495 val |= DREF_NONSPREAD_CK505_ENABLE;
99eb6a01 8496 else
74cfd7ac 8497 val |= DREF_NONSPREAD_SOURCE_ENABLE;
13d83a67 8498
199e5d79 8499 if (has_panel) {
74cfd7ac
CW
8500 val &= ~DREF_SSC_SOURCE_MASK;
8501 val |= DREF_SSC_SOURCE_ENABLE;
13d83a67 8502
199e5d79 8503 /* SSC must be turned on before enabling the CPU output */
99eb6a01 8504 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
199e5d79 8505 DRM_DEBUG_KMS("Using SSC on panel\n");
74cfd7ac 8506 val |= DREF_SSC1_ENABLE;
e77166b5 8507 } else
74cfd7ac 8508 val &= ~DREF_SSC1_ENABLE;
199e5d79
KP
8509
8510 /* Get SSC going before enabling the outputs */
74cfd7ac 8511 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
8512 POSTING_READ(PCH_DREF_CONTROL);
8513 udelay(200);
8514
74cfd7ac 8515 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
13d83a67
JB
8516
8517 /* Enable CPU source on CPU attached eDP */
199e5d79 8518 if (has_cpu_edp) {
99eb6a01 8519 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
199e5d79 8520 DRM_DEBUG_KMS("Using SSC on eDP\n");
74cfd7ac 8521 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
eba905b2 8522 } else
74cfd7ac 8523 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
199e5d79 8524 } else
74cfd7ac 8525 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
199e5d79 8526
74cfd7ac 8527 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
8528 POSTING_READ(PCH_DREF_CONTROL);
8529 udelay(200);
8530 } else {
1c1a24d2 8531 DRM_DEBUG_KMS("Disabling CPU source output\n");
199e5d79 8532
74cfd7ac 8533 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
199e5d79
KP
8534
8535 /* Turn off CPU output */
74cfd7ac 8536 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
199e5d79 8537
74cfd7ac 8538 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
8539 POSTING_READ(PCH_DREF_CONTROL);
8540 udelay(200);
8541
1c1a24d2
L
8542 if (!using_ssc_source) {
8543 DRM_DEBUG_KMS("Disabling SSC source\n");
199e5d79 8544
1c1a24d2
L
8545 /* Turn off the SSC source */
8546 val &= ~DREF_SSC_SOURCE_MASK;
8547 val |= DREF_SSC_SOURCE_DISABLE;
f165d283 8548
1c1a24d2
L
8549 /* Turn off SSC1 */
8550 val &= ~DREF_SSC1_ENABLE;
8551
8552 I915_WRITE(PCH_DREF_CONTROL, val);
8553 POSTING_READ(PCH_DREF_CONTROL);
8554 udelay(200);
8555 }
13d83a67 8556 }
74cfd7ac
CW
8557
8558 BUG_ON(val != final);
13d83a67
JB
8559}
8560
f31f2d55 8561static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
dde86e2d 8562{
f31f2d55 8563 uint32_t tmp;
dde86e2d 8564
0ff066a9
PZ
8565 tmp = I915_READ(SOUTH_CHICKEN2);
8566 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
8567 I915_WRITE(SOUTH_CHICKEN2, tmp);
dde86e2d 8568
cf3598c2
ID
8569 if (wait_for_us(I915_READ(SOUTH_CHICKEN2) &
8570 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
0ff066a9 8571 DRM_ERROR("FDI mPHY reset assert timeout\n");
dde86e2d 8572
0ff066a9
PZ
8573 tmp = I915_READ(SOUTH_CHICKEN2);
8574 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
8575 I915_WRITE(SOUTH_CHICKEN2, tmp);
dde86e2d 8576
cf3598c2
ID
8577 if (wait_for_us((I915_READ(SOUTH_CHICKEN2) &
8578 FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
0ff066a9 8579 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
f31f2d55
PZ
8580}
8581
8582/* WaMPhyProgramming:hsw */
8583static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
8584{
8585 uint32_t tmp;
dde86e2d
PZ
8586
8587 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
8588 tmp &= ~(0xFF << 24);
8589 tmp |= (0x12 << 24);
8590 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
8591
dde86e2d
PZ
8592 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
8593 tmp |= (1 << 11);
8594 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
8595
8596 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
8597 tmp |= (1 << 11);
8598 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
8599
dde86e2d
PZ
8600 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
8601 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
8602 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
8603
8604 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
8605 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
8606 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
8607
0ff066a9
PZ
8608 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
8609 tmp &= ~(7 << 13);
8610 tmp |= (5 << 13);
8611 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
dde86e2d 8612
0ff066a9
PZ
8613 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
8614 tmp &= ~(7 << 13);
8615 tmp |= (5 << 13);
8616 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
dde86e2d
PZ
8617
8618 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
8619 tmp &= ~0xFF;
8620 tmp |= 0x1C;
8621 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
8622
8623 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
8624 tmp &= ~0xFF;
8625 tmp |= 0x1C;
8626 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
8627
8628 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
8629 tmp &= ~(0xFF << 16);
8630 tmp |= (0x1C << 16);
8631 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
8632
8633 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
8634 tmp &= ~(0xFF << 16);
8635 tmp |= (0x1C << 16);
8636 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
8637
0ff066a9
PZ
8638 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
8639 tmp |= (1 << 27);
8640 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
dde86e2d 8641
0ff066a9
PZ
8642 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
8643 tmp |= (1 << 27);
8644 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
dde86e2d 8645
0ff066a9
PZ
8646 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
8647 tmp &= ~(0xF << 28);
8648 tmp |= (4 << 28);
8649 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
dde86e2d 8650
0ff066a9
PZ
8651 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
8652 tmp &= ~(0xF << 28);
8653 tmp |= (4 << 28);
8654 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
f31f2d55
PZ
8655}
8656
2fa86a1f
PZ
8657/* Implements 3 different sequences from BSpec chapter "Display iCLK
8658 * Programming" based on the parameters passed:
8659 * - Sequence to enable CLKOUT_DP
8660 * - Sequence to enable CLKOUT_DP without spread
8661 * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
8662 */
8663static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
8664 bool with_fdi)
f31f2d55
PZ
8665{
8666 struct drm_i915_private *dev_priv = dev->dev_private;
2fa86a1f
PZ
8667 uint32_t reg, tmp;
8668
8669 if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
8670 with_spread = true;
c2699524 8671 if (WARN(HAS_PCH_LPT_LP(dev) && with_fdi, "LP PCH doesn't have FDI\n"))
2fa86a1f 8672 with_fdi = false;
f31f2d55 8673
a580516d 8674 mutex_lock(&dev_priv->sb_lock);
f31f2d55
PZ
8675
8676 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8677 tmp &= ~SBI_SSCCTL_DISABLE;
8678 tmp |= SBI_SSCCTL_PATHALT;
8679 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8680
8681 udelay(24);
8682
2fa86a1f
PZ
8683 if (with_spread) {
8684 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8685 tmp &= ~SBI_SSCCTL_PATHALT;
8686 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
f31f2d55 8687
2fa86a1f
PZ
8688 if (with_fdi) {
8689 lpt_reset_fdi_mphy(dev_priv);
8690 lpt_program_fdi_mphy(dev_priv);
8691 }
8692 }
dde86e2d 8693
c2699524 8694 reg = HAS_PCH_LPT_LP(dev) ? SBI_GEN0 : SBI_DBUFF0;
2fa86a1f
PZ
8695 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8696 tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8697 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
c00db246 8698
a580516d 8699 mutex_unlock(&dev_priv->sb_lock);
dde86e2d
PZ
8700}
8701
47701c3b
PZ
8702/* Sequence to disable CLKOUT_DP */
8703static void lpt_disable_clkout_dp(struct drm_device *dev)
8704{
8705 struct drm_i915_private *dev_priv = dev->dev_private;
8706 uint32_t reg, tmp;
8707
a580516d 8708 mutex_lock(&dev_priv->sb_lock);
47701c3b 8709
c2699524 8710 reg = HAS_PCH_LPT_LP(dev) ? SBI_GEN0 : SBI_DBUFF0;
47701c3b
PZ
8711 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8712 tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8713 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
8714
8715 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8716 if (!(tmp & SBI_SSCCTL_DISABLE)) {
8717 if (!(tmp & SBI_SSCCTL_PATHALT)) {
8718 tmp |= SBI_SSCCTL_PATHALT;
8719 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8720 udelay(32);
8721 }
8722 tmp |= SBI_SSCCTL_DISABLE;
8723 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8724 }
8725
a580516d 8726 mutex_unlock(&dev_priv->sb_lock);
47701c3b
PZ
8727}
8728
f7be2c21
VS
8729#define BEND_IDX(steps) ((50 + (steps)) / 5)
8730
8731static const uint16_t sscdivintphase[] = {
8732 [BEND_IDX( 50)] = 0x3B23,
8733 [BEND_IDX( 45)] = 0x3B23,
8734 [BEND_IDX( 40)] = 0x3C23,
8735 [BEND_IDX( 35)] = 0x3C23,
8736 [BEND_IDX( 30)] = 0x3D23,
8737 [BEND_IDX( 25)] = 0x3D23,
8738 [BEND_IDX( 20)] = 0x3E23,
8739 [BEND_IDX( 15)] = 0x3E23,
8740 [BEND_IDX( 10)] = 0x3F23,
8741 [BEND_IDX( 5)] = 0x3F23,
8742 [BEND_IDX( 0)] = 0x0025,
8743 [BEND_IDX( -5)] = 0x0025,
8744 [BEND_IDX(-10)] = 0x0125,
8745 [BEND_IDX(-15)] = 0x0125,
8746 [BEND_IDX(-20)] = 0x0225,
8747 [BEND_IDX(-25)] = 0x0225,
8748 [BEND_IDX(-30)] = 0x0325,
8749 [BEND_IDX(-35)] = 0x0325,
8750 [BEND_IDX(-40)] = 0x0425,
8751 [BEND_IDX(-45)] = 0x0425,
8752 [BEND_IDX(-50)] = 0x0525,
8753};
8754
8755/*
8756 * Bend CLKOUT_DP
8757 * steps -50 to 50 inclusive, in steps of 5
8758 * < 0 slow down the clock, > 0 speed up the clock, 0 == no bend (135MHz)
8759 * change in clock period = -(steps / 10) * 5.787 ps
8760 */
8761static void lpt_bend_clkout_dp(struct drm_i915_private *dev_priv, int steps)
8762{
8763 uint32_t tmp;
8764 int idx = BEND_IDX(steps);
8765
8766 if (WARN_ON(steps % 5 != 0))
8767 return;
8768
8769 if (WARN_ON(idx >= ARRAY_SIZE(sscdivintphase)))
8770 return;
8771
8772 mutex_lock(&dev_priv->sb_lock);
8773
8774 if (steps % 10 != 0)
8775 tmp = 0xAAAAAAAB;
8776 else
8777 tmp = 0x00000000;
8778 intel_sbi_write(dev_priv, SBI_SSCDITHPHASE, tmp, SBI_ICLK);
8779
8780 tmp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE, SBI_ICLK);
8781 tmp &= 0xffff0000;
8782 tmp |= sscdivintphase[idx];
8783 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE, tmp, SBI_ICLK);
8784
8785 mutex_unlock(&dev_priv->sb_lock);
8786}
8787
8788#undef BEND_IDX
8789
bf8fa3d3
PZ
8790static void lpt_init_pch_refclk(struct drm_device *dev)
8791{
bf8fa3d3
PZ
8792 struct intel_encoder *encoder;
8793 bool has_vga = false;
8794
b2784e15 8795 for_each_intel_encoder(dev, encoder) {
bf8fa3d3
PZ
8796 switch (encoder->type) {
8797 case INTEL_OUTPUT_ANALOG:
8798 has_vga = true;
8799 break;
6847d71b
PZ
8800 default:
8801 break;
bf8fa3d3
PZ
8802 }
8803 }
8804
f7be2c21
VS
8805 if (has_vga) {
8806 lpt_bend_clkout_dp(to_i915(dev), 0);
47701c3b 8807 lpt_enable_clkout_dp(dev, true, true);
f7be2c21 8808 } else {
47701c3b 8809 lpt_disable_clkout_dp(dev);
f7be2c21 8810 }
bf8fa3d3
PZ
8811}
8812
dde86e2d
PZ
8813/*
8814 * Initialize reference clocks when the driver loads
8815 */
8816void intel_init_pch_refclk(struct drm_device *dev)
8817{
8818 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
8819 ironlake_init_pch_refclk(dev);
8820 else if (HAS_PCH_LPT(dev))
8821 lpt_init_pch_refclk(dev);
8822}
8823
6ff93609 8824static void ironlake_set_pipeconf(struct drm_crtc *crtc)
79e53945 8825{
c8203565 8826 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
79e53945
JB
8827 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8828 int pipe = intel_crtc->pipe;
c8203565
PZ
8829 uint32_t val;
8830
78114071 8831 val = 0;
c8203565 8832
6e3c9717 8833 switch (intel_crtc->config->pipe_bpp) {
c8203565 8834 case 18:
dfd07d72 8835 val |= PIPECONF_6BPC;
c8203565
PZ
8836 break;
8837 case 24:
dfd07d72 8838 val |= PIPECONF_8BPC;
c8203565
PZ
8839 break;
8840 case 30:
dfd07d72 8841 val |= PIPECONF_10BPC;
c8203565
PZ
8842 break;
8843 case 36:
dfd07d72 8844 val |= PIPECONF_12BPC;
c8203565
PZ
8845 break;
8846 default:
cc769b62
PZ
8847 /* Case prevented by intel_choose_pipe_bpp_dither. */
8848 BUG();
c8203565
PZ
8849 }
8850
6e3c9717 8851 if (intel_crtc->config->dither)
c8203565
PZ
8852 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8853
6e3c9717 8854 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
c8203565
PZ
8855 val |= PIPECONF_INTERLACED_ILK;
8856 else
8857 val |= PIPECONF_PROGRESSIVE;
8858
6e3c9717 8859 if (intel_crtc->config->limited_color_range)
3685a8f3 8860 val |= PIPECONF_COLOR_RANGE_SELECT;
3685a8f3 8861
c8203565
PZ
8862 I915_WRITE(PIPECONF(pipe), val);
8863 POSTING_READ(PIPECONF(pipe));
8864}
8865
6ff93609 8866static void haswell_set_pipeconf(struct drm_crtc *crtc)
ee2b0b38 8867{
391bf048 8868 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
ee2b0b38 8869 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6e3c9717 8870 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
391bf048 8871 u32 val = 0;
ee2b0b38 8872
391bf048 8873 if (IS_HASWELL(dev_priv) && intel_crtc->config->dither)
ee2b0b38
PZ
8874 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8875
6e3c9717 8876 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
ee2b0b38
PZ
8877 val |= PIPECONF_INTERLACED_ILK;
8878 else
8879 val |= PIPECONF_PROGRESSIVE;
8880
702e7a56
PZ
8881 I915_WRITE(PIPECONF(cpu_transcoder), val);
8882 POSTING_READ(PIPECONF(cpu_transcoder));
391bf048
JN
8883}
8884
391bf048
JN
8885static void haswell_set_pipemisc(struct drm_crtc *crtc)
8886{
8887 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
8888 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
756f85cf 8889
391bf048
JN
8890 if (IS_BROADWELL(dev_priv) || INTEL_INFO(dev_priv)->gen >= 9) {
8891 u32 val = 0;
756f85cf 8892
6e3c9717 8893 switch (intel_crtc->config->pipe_bpp) {
756f85cf
PZ
8894 case 18:
8895 val |= PIPEMISC_DITHER_6_BPC;
8896 break;
8897 case 24:
8898 val |= PIPEMISC_DITHER_8_BPC;
8899 break;
8900 case 30:
8901 val |= PIPEMISC_DITHER_10_BPC;
8902 break;
8903 case 36:
8904 val |= PIPEMISC_DITHER_12_BPC;
8905 break;
8906 default:
8907 /* Case prevented by pipe_config_set_bpp. */
8908 BUG();
8909 }
8910
6e3c9717 8911 if (intel_crtc->config->dither)
756f85cf
PZ
8912 val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
8913
391bf048 8914 I915_WRITE(PIPEMISC(intel_crtc->pipe), val);
756f85cf 8915 }
ee2b0b38
PZ
8916}
8917
d4b1931c
PZ
8918int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
8919{
8920 /*
8921 * Account for spread spectrum to avoid
8922 * oversubscribing the link. Max center spread
8923 * is 2.5%; use 5% for safety's sake.
8924 */
8925 u32 bps = target_clock * bpp * 21 / 20;
619d4d04 8926 return DIV_ROUND_UP(bps, link_bw * 8);
d4b1931c
PZ
8927}
8928
7429e9d4 8929static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
6cf86a5e 8930{
7429e9d4 8931 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
f48d8f23
PZ
8932}
8933
b75ca6f6
ACO
8934static void ironlake_compute_dpll(struct intel_crtc *intel_crtc,
8935 struct intel_crtc_state *crtc_state,
9e2c8475 8936 struct dpll *reduced_clock)
79e53945 8937{
de13a2e3 8938 struct drm_crtc *crtc = &intel_crtc->base;
79e53945
JB
8939 struct drm_device *dev = crtc->dev;
8940 struct drm_i915_private *dev_priv = dev->dev_private;
55bb9992 8941 struct drm_atomic_state *state = crtc_state->base.state;
da3ced29 8942 struct drm_connector *connector;
55bb9992
ACO
8943 struct drm_connector_state *connector_state;
8944 struct intel_encoder *encoder;
b75ca6f6 8945 u32 dpll, fp, fp2;
ceb41007 8946 int factor, i;
09ede541 8947 bool is_lvds = false, is_sdvo = false;
79e53945 8948
da3ced29 8949 for_each_connector_in_state(state, connector, connector_state, i) {
55bb9992
ACO
8950 if (connector_state->crtc != crtc_state->base.crtc)
8951 continue;
8952
8953 encoder = to_intel_encoder(connector_state->best_encoder);
8954
8955 switch (encoder->type) {
79e53945
JB
8956 case INTEL_OUTPUT_LVDS:
8957 is_lvds = true;
8958 break;
8959 case INTEL_OUTPUT_SDVO:
7d57382e 8960 case INTEL_OUTPUT_HDMI:
79e53945 8961 is_sdvo = true;
79e53945 8962 break;
6847d71b
PZ
8963 default:
8964 break;
79e53945
JB
8965 }
8966 }
79e53945 8967
c1858123 8968 /* Enable autotuning of the PLL clock (if permissible) */
8febb297
EA
8969 factor = 21;
8970 if (is_lvds) {
8971 if ((intel_panel_use_ssc(dev_priv) &&
e91e941b 8972 dev_priv->vbt.lvds_ssc_freq == 100000) ||
f0b44056 8973 (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
8febb297 8974 factor = 25;
190f68c5 8975 } else if (crtc_state->sdvo_tv_clock)
8febb297 8976 factor = 20;
c1858123 8977
b75ca6f6
ACO
8978 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
8979
190f68c5 8980 if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
b75ca6f6
ACO
8981 fp |= FP_CB_TUNE;
8982
8983 if (reduced_clock) {
8984 fp2 = i9xx_dpll_compute_fp(reduced_clock);
2c07245f 8985
b75ca6f6
ACO
8986 if (reduced_clock->m < factor * reduced_clock->n)
8987 fp2 |= FP_CB_TUNE;
8988 } else {
8989 fp2 = fp;
8990 }
9a7c7890 8991
5eddb70b 8992 dpll = 0;
2c07245f 8993
a07d6787
EA
8994 if (is_lvds)
8995 dpll |= DPLLB_MODE_LVDS;
8996 else
8997 dpll |= DPLLB_MODE_DAC_SERIAL;
198a037f 8998
190f68c5 8999 dpll |= (crtc_state->pixel_multiplier - 1)
ef1b460d 9000 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
198a037f
DV
9001
9002 if (is_sdvo)
4a33e48d 9003 dpll |= DPLL_SDVO_HIGH_SPEED;
190f68c5 9004 if (crtc_state->has_dp_encoder)
4a33e48d 9005 dpll |= DPLL_SDVO_HIGH_SPEED;
79e53945 9006
a07d6787 9007 /* compute bitmask from p1 value */
190f68c5 9008 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
a07d6787 9009 /* also FPA1 */
190f68c5 9010 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
a07d6787 9011
190f68c5 9012 switch (crtc_state->dpll.p2) {
a07d6787
EA
9013 case 5:
9014 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
9015 break;
9016 case 7:
9017 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
9018 break;
9019 case 10:
9020 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
9021 break;
9022 case 14:
9023 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
9024 break;
79e53945
JB
9025 }
9026
ceb41007 9027 if (is_lvds && intel_panel_use_ssc(dev_priv))
43565a06 9028 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
79e53945
JB
9029 else
9030 dpll |= PLL_REF_INPUT_DREFCLK;
9031
b75ca6f6
ACO
9032 dpll |= DPLL_VCO_ENABLE;
9033
9034 crtc_state->dpll_hw_state.dpll = dpll;
9035 crtc_state->dpll_hw_state.fp0 = fp;
9036 crtc_state->dpll_hw_state.fp1 = fp2;
de13a2e3
PZ
9037}
9038
190f68c5
ACO
9039static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
9040 struct intel_crtc_state *crtc_state)
de13a2e3 9041{
997c030c
ACO
9042 struct drm_device *dev = crtc->base.dev;
9043 struct drm_i915_private *dev_priv = dev->dev_private;
9e2c8475 9044 struct dpll reduced_clock;
7ed9f894 9045 bool has_reduced_clock = false;
e2b78267 9046 struct intel_shared_dpll *pll;
1b6f4958 9047 const struct intel_limit *limit;
997c030c 9048 int refclk = 120000;
de13a2e3 9049
dd3cd74a
ACO
9050 memset(&crtc_state->dpll_hw_state, 0,
9051 sizeof(crtc_state->dpll_hw_state));
9052
ded220e2
ACO
9053 crtc->lowfreq_avail = false;
9054
9055 /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
9056 if (!crtc_state->has_pch_encoder)
9057 return 0;
79e53945 9058
997c030c
ACO
9059 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
9060 if (intel_panel_use_ssc(dev_priv)) {
9061 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
9062 dev_priv->vbt.lvds_ssc_freq);
9063 refclk = dev_priv->vbt.lvds_ssc_freq;
9064 }
9065
9066 if (intel_is_dual_link_lvds(dev)) {
9067 if (refclk == 100000)
9068 limit = &intel_limits_ironlake_dual_lvds_100m;
9069 else
9070 limit = &intel_limits_ironlake_dual_lvds;
9071 } else {
9072 if (refclk == 100000)
9073 limit = &intel_limits_ironlake_single_lvds_100m;
9074 else
9075 limit = &intel_limits_ironlake_single_lvds;
9076 }
9077 } else {
9078 limit = &intel_limits_ironlake_dac;
9079 }
9080
364ee29d 9081 if (!crtc_state->clock_set &&
997c030c
ACO
9082 !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
9083 refclk, NULL, &crtc_state->dpll)) {
364ee29d
ACO
9084 DRM_ERROR("Couldn't find PLL settings for mode!\n");
9085 return -EINVAL;
f47709a9 9086 }
79e53945 9087
b75ca6f6
ACO
9088 ironlake_compute_dpll(crtc, crtc_state,
9089 has_reduced_clock ? &reduced_clock : NULL);
66e985c0 9090
ded220e2
ACO
9091 pll = intel_get_shared_dpll(crtc, crtc_state, NULL);
9092 if (pll == NULL) {
9093 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
9094 pipe_name(crtc->pipe));
9095 return -EINVAL;
3fb37703 9096 }
79e53945 9097
ded220e2
ACO
9098 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
9099 has_reduced_clock)
c7653199 9100 crtc->lowfreq_avail = true;
e2b78267 9101
c8f7a0db 9102 return 0;
79e53945
JB
9103}
9104
eb14cb74
VS
9105static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
9106 struct intel_link_m_n *m_n)
9107{
9108 struct drm_device *dev = crtc->base.dev;
9109 struct drm_i915_private *dev_priv = dev->dev_private;
9110 enum pipe pipe = crtc->pipe;
9111
9112 m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
9113 m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
9114 m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
9115 & ~TU_SIZE_MASK;
9116 m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
9117 m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
9118 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
9119}
9120
9121static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
9122 enum transcoder transcoder,
b95af8be
VK
9123 struct intel_link_m_n *m_n,
9124 struct intel_link_m_n *m2_n2)
72419203
DV
9125{
9126 struct drm_device *dev = crtc->base.dev;
9127 struct drm_i915_private *dev_priv = dev->dev_private;
eb14cb74 9128 enum pipe pipe = crtc->pipe;
72419203 9129
eb14cb74
VS
9130 if (INTEL_INFO(dev)->gen >= 5) {
9131 m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
9132 m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
9133 m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
9134 & ~TU_SIZE_MASK;
9135 m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
9136 m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
9137 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
b95af8be
VK
9138 /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
9139 * gen < 8) and if DRRS is supported (to make sure the
9140 * registers are not unnecessarily read).
9141 */
9142 if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
6e3c9717 9143 crtc->config->has_drrs) {
b95af8be
VK
9144 m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
9145 m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
9146 m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
9147 & ~TU_SIZE_MASK;
9148 m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
9149 m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
9150 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
9151 }
eb14cb74
VS
9152 } else {
9153 m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
9154 m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
9155 m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
9156 & ~TU_SIZE_MASK;
9157 m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
9158 m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
9159 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
9160 }
9161}
9162
9163void intel_dp_get_m_n(struct intel_crtc *crtc,
5cec258b 9164 struct intel_crtc_state *pipe_config)
eb14cb74 9165{
681a8504 9166 if (pipe_config->has_pch_encoder)
eb14cb74
VS
9167 intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
9168 else
9169 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
b95af8be
VK
9170 &pipe_config->dp_m_n,
9171 &pipe_config->dp_m2_n2);
eb14cb74 9172}
72419203 9173
eb14cb74 9174static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
5cec258b 9175 struct intel_crtc_state *pipe_config)
eb14cb74
VS
9176{
9177 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
b95af8be 9178 &pipe_config->fdi_m_n, NULL);
72419203
DV
9179}
9180
bd2e244f 9181static void skylake_get_pfit_config(struct intel_crtc *crtc,
5cec258b 9182 struct intel_crtc_state *pipe_config)
bd2e244f
JB
9183{
9184 struct drm_device *dev = crtc->base.dev;
9185 struct drm_i915_private *dev_priv = dev->dev_private;
a1b2278e
CK
9186 struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
9187 uint32_t ps_ctrl = 0;
9188 int id = -1;
9189 int i;
bd2e244f 9190
a1b2278e
CK
9191 /* find scaler attached to this pipe */
9192 for (i = 0; i < crtc->num_scalers; i++) {
9193 ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
9194 if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
9195 id = i;
9196 pipe_config->pch_pfit.enabled = true;
9197 pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
9198 pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
9199 break;
9200 }
9201 }
bd2e244f 9202
a1b2278e
CK
9203 scaler_state->scaler_id = id;
9204 if (id >= 0) {
9205 scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
9206 } else {
9207 scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
bd2e244f
JB
9208 }
9209}
9210
5724dbd1
DL
9211static void
9212skylake_get_initial_plane_config(struct intel_crtc *crtc,
9213 struct intel_initial_plane_config *plane_config)
bc8d7dff
DL
9214{
9215 struct drm_device *dev = crtc->base.dev;
9216 struct drm_i915_private *dev_priv = dev->dev_private;
40f46283 9217 u32 val, base, offset, stride_mult, tiling;
bc8d7dff
DL
9218 int pipe = crtc->pipe;
9219 int fourcc, pixel_format;
6761dd31 9220 unsigned int aligned_height;
bc8d7dff 9221 struct drm_framebuffer *fb;
1b842c89 9222 struct intel_framebuffer *intel_fb;
bc8d7dff 9223
d9806c9f 9224 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
1b842c89 9225 if (!intel_fb) {
bc8d7dff
DL
9226 DRM_DEBUG_KMS("failed to alloc fb\n");
9227 return;
9228 }
9229
1b842c89
DL
9230 fb = &intel_fb->base;
9231
bc8d7dff 9232 val = I915_READ(PLANE_CTL(pipe, 0));
42a7b088
DL
9233 if (!(val & PLANE_CTL_ENABLE))
9234 goto error;
9235
bc8d7dff
DL
9236 pixel_format = val & PLANE_CTL_FORMAT_MASK;
9237 fourcc = skl_format_to_fourcc(pixel_format,
9238 val & PLANE_CTL_ORDER_RGBX,
9239 val & PLANE_CTL_ALPHA_MASK);
9240 fb->pixel_format = fourcc;
9241 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
9242
40f46283
DL
9243 tiling = val & PLANE_CTL_TILED_MASK;
9244 switch (tiling) {
9245 case PLANE_CTL_TILED_LINEAR:
9246 fb->modifier[0] = DRM_FORMAT_MOD_NONE;
9247 break;
9248 case PLANE_CTL_TILED_X:
9249 plane_config->tiling = I915_TILING_X;
9250 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
9251 break;
9252 case PLANE_CTL_TILED_Y:
9253 fb->modifier[0] = I915_FORMAT_MOD_Y_TILED;
9254 break;
9255 case PLANE_CTL_TILED_YF:
9256 fb->modifier[0] = I915_FORMAT_MOD_Yf_TILED;
9257 break;
9258 default:
9259 MISSING_CASE(tiling);
9260 goto error;
9261 }
9262
bc8d7dff
DL
9263 base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
9264 plane_config->base = base;
9265
9266 offset = I915_READ(PLANE_OFFSET(pipe, 0));
9267
9268 val = I915_READ(PLANE_SIZE(pipe, 0));
9269 fb->height = ((val >> 16) & 0xfff) + 1;
9270 fb->width = ((val >> 0) & 0x1fff) + 1;
9271
9272 val = I915_READ(PLANE_STRIDE(pipe, 0));
7b49f948 9273 stride_mult = intel_fb_stride_alignment(dev_priv, fb->modifier[0],
40f46283 9274 fb->pixel_format);
bc8d7dff
DL
9275 fb->pitches[0] = (val & 0x3ff) * stride_mult;
9276
9277 aligned_height = intel_fb_align_height(dev, fb->height,
091df6cb
DV
9278 fb->pixel_format,
9279 fb->modifier[0]);
bc8d7dff 9280
f37b5c2b 9281 plane_config->size = fb->pitches[0] * aligned_height;
bc8d7dff
DL
9282
9283 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9284 pipe_name(pipe), fb->width, fb->height,
9285 fb->bits_per_pixel, base, fb->pitches[0],
9286 plane_config->size);
9287
2d14030b 9288 plane_config->fb = intel_fb;
bc8d7dff
DL
9289 return;
9290
9291error:
9292 kfree(fb);
9293}
9294
2fa2fe9a 9295static void ironlake_get_pfit_config(struct intel_crtc *crtc,
5cec258b 9296 struct intel_crtc_state *pipe_config)
2fa2fe9a
DV
9297{
9298 struct drm_device *dev = crtc->base.dev;
9299 struct drm_i915_private *dev_priv = dev->dev_private;
9300 uint32_t tmp;
9301
9302 tmp = I915_READ(PF_CTL(crtc->pipe));
9303
9304 if (tmp & PF_ENABLE) {
fd4daa9c 9305 pipe_config->pch_pfit.enabled = true;
2fa2fe9a
DV
9306 pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
9307 pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
cb8b2a30
DV
9308
9309 /* We currently do not free assignements of panel fitters on
9310 * ivb/hsw (since we don't use the higher upscaling modes which
9311 * differentiates them) so just WARN about this case for now. */
9312 if (IS_GEN7(dev)) {
9313 WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
9314 PF_PIPE_SEL_IVB(crtc->pipe));
9315 }
2fa2fe9a 9316 }
79e53945
JB
9317}
9318
5724dbd1
DL
9319static void
9320ironlake_get_initial_plane_config(struct intel_crtc *crtc,
9321 struct intel_initial_plane_config *plane_config)
4c6baa59
JB
9322{
9323 struct drm_device *dev = crtc->base.dev;
9324 struct drm_i915_private *dev_priv = dev->dev_private;
9325 u32 val, base, offset;
aeee5a49 9326 int pipe = crtc->pipe;
4c6baa59 9327 int fourcc, pixel_format;
6761dd31 9328 unsigned int aligned_height;
b113d5ee 9329 struct drm_framebuffer *fb;
1b842c89 9330 struct intel_framebuffer *intel_fb;
4c6baa59 9331
42a7b088
DL
9332 val = I915_READ(DSPCNTR(pipe));
9333 if (!(val & DISPLAY_PLANE_ENABLE))
9334 return;
9335
d9806c9f 9336 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
1b842c89 9337 if (!intel_fb) {
4c6baa59
JB
9338 DRM_DEBUG_KMS("failed to alloc fb\n");
9339 return;
9340 }
9341
1b842c89
DL
9342 fb = &intel_fb->base;
9343
18c5247e
DV
9344 if (INTEL_INFO(dev)->gen >= 4) {
9345 if (val & DISPPLANE_TILED) {
49af449b 9346 plane_config->tiling = I915_TILING_X;
18c5247e
DV
9347 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
9348 }
9349 }
4c6baa59
JB
9350
9351 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
b35d63fa 9352 fourcc = i9xx_format_to_fourcc(pixel_format);
b113d5ee
DL
9353 fb->pixel_format = fourcc;
9354 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
4c6baa59 9355
aeee5a49 9356 base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
4c6baa59 9357 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
aeee5a49 9358 offset = I915_READ(DSPOFFSET(pipe));
4c6baa59 9359 } else {
49af449b 9360 if (plane_config->tiling)
aeee5a49 9361 offset = I915_READ(DSPTILEOFF(pipe));
4c6baa59 9362 else
aeee5a49 9363 offset = I915_READ(DSPLINOFF(pipe));
4c6baa59
JB
9364 }
9365 plane_config->base = base;
9366
9367 val = I915_READ(PIPESRC(pipe));
b113d5ee
DL
9368 fb->width = ((val >> 16) & 0xfff) + 1;
9369 fb->height = ((val >> 0) & 0xfff) + 1;
4c6baa59
JB
9370
9371 val = I915_READ(DSPSTRIDE(pipe));
b113d5ee 9372 fb->pitches[0] = val & 0xffffffc0;
4c6baa59 9373
b113d5ee 9374 aligned_height = intel_fb_align_height(dev, fb->height,
091df6cb
DV
9375 fb->pixel_format,
9376 fb->modifier[0]);
4c6baa59 9377
f37b5c2b 9378 plane_config->size = fb->pitches[0] * aligned_height;
4c6baa59 9379
2844a921
DL
9380 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9381 pipe_name(pipe), fb->width, fb->height,
9382 fb->bits_per_pixel, base, fb->pitches[0],
9383 plane_config->size);
b113d5ee 9384
2d14030b 9385 plane_config->fb = intel_fb;
4c6baa59
JB
9386}
9387
0e8ffe1b 9388static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
5cec258b 9389 struct intel_crtc_state *pipe_config)
0e8ffe1b
DV
9390{
9391 struct drm_device *dev = crtc->base.dev;
9392 struct drm_i915_private *dev_priv = dev->dev_private;
1729050e 9393 enum intel_display_power_domain power_domain;
0e8ffe1b 9394 uint32_t tmp;
1729050e 9395 bool ret;
0e8ffe1b 9396
1729050e
ID
9397 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
9398 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
930e8c9e
PZ
9399 return false;
9400
e143a21c 9401 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
8106ddbd 9402 pipe_config->shared_dpll = NULL;
eccb140b 9403
1729050e 9404 ret = false;
0e8ffe1b
DV
9405 tmp = I915_READ(PIPECONF(crtc->pipe));
9406 if (!(tmp & PIPECONF_ENABLE))
1729050e 9407 goto out;
0e8ffe1b 9408
42571aef
VS
9409 switch (tmp & PIPECONF_BPC_MASK) {
9410 case PIPECONF_6BPC:
9411 pipe_config->pipe_bpp = 18;
9412 break;
9413 case PIPECONF_8BPC:
9414 pipe_config->pipe_bpp = 24;
9415 break;
9416 case PIPECONF_10BPC:
9417 pipe_config->pipe_bpp = 30;
9418 break;
9419 case PIPECONF_12BPC:
9420 pipe_config->pipe_bpp = 36;
9421 break;
9422 default:
9423 break;
9424 }
9425
b5a9fa09
DV
9426 if (tmp & PIPECONF_COLOR_RANGE_SELECT)
9427 pipe_config->limited_color_range = true;
9428
ab9412ba 9429 if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
66e985c0 9430 struct intel_shared_dpll *pll;
8106ddbd 9431 enum intel_dpll_id pll_id;
66e985c0 9432
88adfff1
DV
9433 pipe_config->has_pch_encoder = true;
9434
627eb5a3
DV
9435 tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
9436 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
9437 FDI_DP_PORT_WIDTH_SHIFT) + 1;
72419203
DV
9438
9439 ironlake_get_fdi_m_n_config(crtc, pipe_config);
6c49f241 9440
2d1fe073 9441 if (HAS_PCH_IBX(dev_priv)) {
d9a7bc67
ID
9442 /*
9443 * The pipe->pch transcoder and pch transcoder->pll
9444 * mapping is fixed.
9445 */
8106ddbd 9446 pll_id = (enum intel_dpll_id) crtc->pipe;
c0d43d62
DV
9447 } else {
9448 tmp = I915_READ(PCH_DPLL_SEL);
9449 if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
8106ddbd 9450 pll_id = DPLL_ID_PCH_PLL_B;
c0d43d62 9451 else
8106ddbd 9452 pll_id= DPLL_ID_PCH_PLL_A;
c0d43d62 9453 }
66e985c0 9454
8106ddbd
ACO
9455 pipe_config->shared_dpll =
9456 intel_get_shared_dpll_by_id(dev_priv, pll_id);
9457 pll = pipe_config->shared_dpll;
66e985c0 9458
2edd6443
ACO
9459 WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
9460 &pipe_config->dpll_hw_state));
c93f54cf
DV
9461
9462 tmp = pipe_config->dpll_hw_state.dpll;
9463 pipe_config->pixel_multiplier =
9464 ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
9465 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
18442d08
VS
9466
9467 ironlake_pch_clock_get(crtc, pipe_config);
6c49f241
DV
9468 } else {
9469 pipe_config->pixel_multiplier = 1;
627eb5a3
DV
9470 }
9471
1bd1bd80 9472 intel_get_pipe_timings(crtc, pipe_config);
bc58be60 9473 intel_get_pipe_src_size(crtc, pipe_config);
1bd1bd80 9474
2fa2fe9a
DV
9475 ironlake_get_pfit_config(crtc, pipe_config);
9476
1729050e
ID
9477 ret = true;
9478
9479out:
9480 intel_display_power_put(dev_priv, power_domain);
9481
9482 return ret;
0e8ffe1b
DV
9483}
9484
be256dc7
PZ
9485static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
9486{
9487 struct drm_device *dev = dev_priv->dev;
be256dc7 9488 struct intel_crtc *crtc;
be256dc7 9489
d3fcc808 9490 for_each_intel_crtc(dev, crtc)
e2c719b7 9491 I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
be256dc7
PZ
9492 pipe_name(crtc->pipe));
9493
e2c719b7
RC
9494 I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
9495 I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
01403de3
VS
9496 I915_STATE_WARN(I915_READ(WRPLL_CTL(0)) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
9497 I915_STATE_WARN(I915_READ(WRPLL_CTL(1)) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
e2c719b7
RC
9498 I915_STATE_WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
9499 I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
be256dc7 9500 "CPU PWM1 enabled\n");
c5107b87 9501 if (IS_HASWELL(dev))
e2c719b7 9502 I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
c5107b87 9503 "CPU PWM2 enabled\n");
e2c719b7 9504 I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
be256dc7 9505 "PCH PWM1 enabled\n");
e2c719b7 9506 I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
be256dc7 9507 "Utility pin enabled\n");
e2c719b7 9508 I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
be256dc7 9509
9926ada1
PZ
9510 /*
9511 * In theory we can still leave IRQs enabled, as long as only the HPD
9512 * interrupts remain enabled. We used to check for that, but since it's
9513 * gen-specific and since we only disable LCPLL after we fully disable
9514 * the interrupts, the check below should be enough.
9515 */
e2c719b7 9516 I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
be256dc7
PZ
9517}
9518
9ccd5aeb
PZ
9519static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
9520{
9521 struct drm_device *dev = dev_priv->dev;
9522
9523 if (IS_HASWELL(dev))
9524 return I915_READ(D_COMP_HSW);
9525 else
9526 return I915_READ(D_COMP_BDW);
9527}
9528
3c4c9b81
PZ
9529static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
9530{
9531 struct drm_device *dev = dev_priv->dev;
9532
9533 if (IS_HASWELL(dev)) {
9534 mutex_lock(&dev_priv->rps.hw_lock);
9535 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
9536 val))
f475dadf 9537 DRM_ERROR("Failed to write to D_COMP\n");
3c4c9b81
PZ
9538 mutex_unlock(&dev_priv->rps.hw_lock);
9539 } else {
9ccd5aeb
PZ
9540 I915_WRITE(D_COMP_BDW, val);
9541 POSTING_READ(D_COMP_BDW);
3c4c9b81 9542 }
be256dc7
PZ
9543}
9544
9545/*
9546 * This function implements pieces of two sequences from BSpec:
9547 * - Sequence for display software to disable LCPLL
9548 * - Sequence for display software to allow package C8+
9549 * The steps implemented here are just the steps that actually touch the LCPLL
9550 * register. Callers should take care of disabling all the display engine
9551 * functions, doing the mode unset, fixing interrupts, etc.
9552 */
6ff58d53
PZ
9553static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
9554 bool switch_to_fclk, bool allow_power_down)
be256dc7
PZ
9555{
9556 uint32_t val;
9557
9558 assert_can_disable_lcpll(dev_priv);
9559
9560 val = I915_READ(LCPLL_CTL);
9561
9562 if (switch_to_fclk) {
9563 val |= LCPLL_CD_SOURCE_FCLK;
9564 I915_WRITE(LCPLL_CTL, val);
9565
f53dd63f
ID
9566 if (wait_for_us(I915_READ(LCPLL_CTL) &
9567 LCPLL_CD_SOURCE_FCLK_DONE, 1))
be256dc7
PZ
9568 DRM_ERROR("Switching to FCLK failed\n");
9569
9570 val = I915_READ(LCPLL_CTL);
9571 }
9572
9573 val |= LCPLL_PLL_DISABLE;
9574 I915_WRITE(LCPLL_CTL, val);
9575 POSTING_READ(LCPLL_CTL);
9576
9577 if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
9578 DRM_ERROR("LCPLL still locked\n");
9579
9ccd5aeb 9580 val = hsw_read_dcomp(dev_priv);
be256dc7 9581 val |= D_COMP_COMP_DISABLE;
3c4c9b81 9582 hsw_write_dcomp(dev_priv, val);
be256dc7
PZ
9583 ndelay(100);
9584
9ccd5aeb
PZ
9585 if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
9586 1))
be256dc7
PZ
9587 DRM_ERROR("D_COMP RCOMP still in progress\n");
9588
9589 if (allow_power_down) {
9590 val = I915_READ(LCPLL_CTL);
9591 val |= LCPLL_POWER_DOWN_ALLOW;
9592 I915_WRITE(LCPLL_CTL, val);
9593 POSTING_READ(LCPLL_CTL);
9594 }
9595}
9596
9597/*
9598 * Fully restores LCPLL, disallowing power down and switching back to LCPLL
9599 * source.
9600 */
6ff58d53 9601static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
be256dc7
PZ
9602{
9603 uint32_t val;
9604
9605 val = I915_READ(LCPLL_CTL);
9606
9607 if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
9608 LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
9609 return;
9610
a8a8bd54
PZ
9611 /*
9612 * Make sure we're not on PC8 state before disabling PC8, otherwise
9613 * we'll hang the machine. To prevent PC8 state, just enable force_wake.
a8a8bd54 9614 */
59bad947 9615 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
215733fa 9616
be256dc7
PZ
9617 if (val & LCPLL_POWER_DOWN_ALLOW) {
9618 val &= ~LCPLL_POWER_DOWN_ALLOW;
9619 I915_WRITE(LCPLL_CTL, val);
35d8f2eb 9620 POSTING_READ(LCPLL_CTL);
be256dc7
PZ
9621 }
9622
9ccd5aeb 9623 val = hsw_read_dcomp(dev_priv);
be256dc7
PZ
9624 val |= D_COMP_COMP_FORCE;
9625 val &= ~D_COMP_COMP_DISABLE;
3c4c9b81 9626 hsw_write_dcomp(dev_priv, val);
be256dc7
PZ
9627
9628 val = I915_READ(LCPLL_CTL);
9629 val &= ~LCPLL_PLL_DISABLE;
9630 I915_WRITE(LCPLL_CTL, val);
9631
9632 if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
9633 DRM_ERROR("LCPLL not locked yet\n");
9634
9635 if (val & LCPLL_CD_SOURCE_FCLK) {
9636 val = I915_READ(LCPLL_CTL);
9637 val &= ~LCPLL_CD_SOURCE_FCLK;
9638 I915_WRITE(LCPLL_CTL, val);
9639
f53dd63f
ID
9640 if (wait_for_us((I915_READ(LCPLL_CTL) &
9641 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
be256dc7
PZ
9642 DRM_ERROR("Switching back to LCPLL failed\n");
9643 }
215733fa 9644
59bad947 9645 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
b6283055 9646 intel_update_cdclk(dev_priv->dev);
be256dc7
PZ
9647}
9648
765dab67
PZ
9649/*
9650 * Package states C8 and deeper are really deep PC states that can only be
9651 * reached when all the devices on the system allow it, so even if the graphics
9652 * device allows PC8+, it doesn't mean the system will actually get to these
9653 * states. Our driver only allows PC8+ when going into runtime PM.
9654 *
9655 * The requirements for PC8+ are that all the outputs are disabled, the power
9656 * well is disabled and most interrupts are disabled, and these are also
9657 * requirements for runtime PM. When these conditions are met, we manually do
9658 * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
9659 * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
9660 * hang the machine.
9661 *
9662 * When we really reach PC8 or deeper states (not just when we allow it) we lose
9663 * the state of some registers, so when we come back from PC8+ we need to
9664 * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
9665 * need to take care of the registers kept by RC6. Notice that this happens even
9666 * if we don't put the device in PCI D3 state (which is what currently happens
9667 * because of the runtime PM support).
9668 *
9669 * For more, read "Display Sequences for Package C8" on the hardware
9670 * documentation.
9671 */
a14cb6fc 9672void hsw_enable_pc8(struct drm_i915_private *dev_priv)
c67a470b 9673{
c67a470b
PZ
9674 struct drm_device *dev = dev_priv->dev;
9675 uint32_t val;
9676
c67a470b
PZ
9677 DRM_DEBUG_KMS("Enabling package C8+\n");
9678
c2699524 9679 if (HAS_PCH_LPT_LP(dev)) {
c67a470b
PZ
9680 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9681 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
9682 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9683 }
9684
9685 lpt_disable_clkout_dp(dev);
c67a470b
PZ
9686 hsw_disable_lcpll(dev_priv, true, true);
9687}
9688
a14cb6fc 9689void hsw_disable_pc8(struct drm_i915_private *dev_priv)
c67a470b
PZ
9690{
9691 struct drm_device *dev = dev_priv->dev;
9692 uint32_t val;
9693
c67a470b
PZ
9694 DRM_DEBUG_KMS("Disabling package C8+\n");
9695
9696 hsw_restore_lcpll(dev_priv);
c67a470b
PZ
9697 lpt_init_pch_refclk(dev);
9698
c2699524 9699 if (HAS_PCH_LPT_LP(dev)) {
c67a470b
PZ
9700 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9701 val |= PCH_LP_PARTITION_LEVEL_DISABLE;
9702 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9703 }
c67a470b
PZ
9704}
9705
324513c0 9706static void bxt_modeset_commit_cdclk(struct drm_atomic_state *old_state)
f8437dd1 9707{
a821fc46 9708 struct drm_device *dev = old_state->dev;
1a617b77
ML
9709 struct intel_atomic_state *old_intel_state =
9710 to_intel_atomic_state(old_state);
9711 unsigned int req_cdclk = old_intel_state->dev_cdclk;
f8437dd1 9712
324513c0 9713 bxt_set_cdclk(to_i915(dev), req_cdclk);
f8437dd1
VK
9714}
9715
b432e5cf 9716/* compute the max rate for new configuration */
27c329ed 9717static int ilk_max_pixel_rate(struct drm_atomic_state *state)
b432e5cf 9718{
565602d7
ML
9719 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
9720 struct drm_i915_private *dev_priv = state->dev->dev_private;
9721 struct drm_crtc *crtc;
9722 struct drm_crtc_state *cstate;
27c329ed 9723 struct intel_crtc_state *crtc_state;
565602d7
ML
9724 unsigned max_pixel_rate = 0, i;
9725 enum pipe pipe;
b432e5cf 9726
565602d7
ML
9727 memcpy(intel_state->min_pixclk, dev_priv->min_pixclk,
9728 sizeof(intel_state->min_pixclk));
27c329ed 9729
565602d7
ML
9730 for_each_crtc_in_state(state, crtc, cstate, i) {
9731 int pixel_rate;
27c329ed 9732
565602d7
ML
9733 crtc_state = to_intel_crtc_state(cstate);
9734 if (!crtc_state->base.enable) {
9735 intel_state->min_pixclk[i] = 0;
b432e5cf 9736 continue;
565602d7 9737 }
b432e5cf 9738
27c329ed 9739 pixel_rate = ilk_pipe_pixel_rate(crtc_state);
b432e5cf
VS
9740
9741 /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
565602d7 9742 if (IS_BROADWELL(dev_priv) && crtc_state->ips_enabled)
b432e5cf
VS
9743 pixel_rate = DIV_ROUND_UP(pixel_rate * 100, 95);
9744
565602d7 9745 intel_state->min_pixclk[i] = pixel_rate;
b432e5cf
VS
9746 }
9747
565602d7
ML
9748 for_each_pipe(dev_priv, pipe)
9749 max_pixel_rate = max(intel_state->min_pixclk[pipe], max_pixel_rate);
9750
b432e5cf
VS
9751 return max_pixel_rate;
9752}
9753
9754static void broadwell_set_cdclk(struct drm_device *dev, int cdclk)
9755{
9756 struct drm_i915_private *dev_priv = dev->dev_private;
9757 uint32_t val, data;
9758 int ret;
9759
9760 if (WARN((I915_READ(LCPLL_CTL) &
9761 (LCPLL_PLL_DISABLE | LCPLL_PLL_LOCK |
9762 LCPLL_CD_CLOCK_DISABLE | LCPLL_ROOT_CD_CLOCK_DISABLE |
9763 LCPLL_CD2X_CLOCK_DISABLE | LCPLL_POWER_DOWN_ALLOW |
9764 LCPLL_CD_SOURCE_FCLK)) != LCPLL_PLL_LOCK,
9765 "trying to change cdclk frequency with cdclk not enabled\n"))
9766 return;
9767
9768 mutex_lock(&dev_priv->rps.hw_lock);
9769 ret = sandybridge_pcode_write(dev_priv,
9770 BDW_PCODE_DISPLAY_FREQ_CHANGE_REQ, 0x0);
9771 mutex_unlock(&dev_priv->rps.hw_lock);
9772 if (ret) {
9773 DRM_ERROR("failed to inform pcode about cdclk change\n");
9774 return;
9775 }
9776
9777 val = I915_READ(LCPLL_CTL);
9778 val |= LCPLL_CD_SOURCE_FCLK;
9779 I915_WRITE(LCPLL_CTL, val);
9780
5ba00178
TU
9781 if (wait_for_us(I915_READ(LCPLL_CTL) &
9782 LCPLL_CD_SOURCE_FCLK_DONE, 1))
b432e5cf
VS
9783 DRM_ERROR("Switching to FCLK failed\n");
9784
9785 val = I915_READ(LCPLL_CTL);
9786 val &= ~LCPLL_CLK_FREQ_MASK;
9787
9788 switch (cdclk) {
9789 case 450000:
9790 val |= LCPLL_CLK_FREQ_450;
9791 data = 0;
9792 break;
9793 case 540000:
9794 val |= LCPLL_CLK_FREQ_54O_BDW;
9795 data = 1;
9796 break;
9797 case 337500:
9798 val |= LCPLL_CLK_FREQ_337_5_BDW;
9799 data = 2;
9800 break;
9801 case 675000:
9802 val |= LCPLL_CLK_FREQ_675_BDW;
9803 data = 3;
9804 break;
9805 default:
9806 WARN(1, "invalid cdclk frequency\n");
9807 return;
9808 }
9809
9810 I915_WRITE(LCPLL_CTL, val);
9811
9812 val = I915_READ(LCPLL_CTL);
9813 val &= ~LCPLL_CD_SOURCE_FCLK;
9814 I915_WRITE(LCPLL_CTL, val);
9815
5ba00178
TU
9816 if (wait_for_us((I915_READ(LCPLL_CTL) &
9817 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
b432e5cf
VS
9818 DRM_ERROR("Switching back to LCPLL failed\n");
9819
9820 mutex_lock(&dev_priv->rps.hw_lock);
9821 sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ, data);
9822 mutex_unlock(&dev_priv->rps.hw_lock);
9823
7f1052a8
VS
9824 I915_WRITE(CDCLK_FREQ, DIV_ROUND_CLOSEST(cdclk, 1000) - 1);
9825
b432e5cf
VS
9826 intel_update_cdclk(dev);
9827
9828 WARN(cdclk != dev_priv->cdclk_freq,
9829 "cdclk requested %d kHz but got %d kHz\n",
9830 cdclk, dev_priv->cdclk_freq);
9831}
9832
587c7914
VS
9833static int broadwell_calc_cdclk(int max_pixclk)
9834{
9835 if (max_pixclk > 540000)
9836 return 675000;
9837 else if (max_pixclk > 450000)
9838 return 540000;
9839 else if (max_pixclk > 337500)
9840 return 450000;
9841 else
9842 return 337500;
9843}
9844
27c329ed 9845static int broadwell_modeset_calc_cdclk(struct drm_atomic_state *state)
b432e5cf 9846{
27c329ed 9847 struct drm_i915_private *dev_priv = to_i915(state->dev);
1a617b77 9848 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
27c329ed 9849 int max_pixclk = ilk_max_pixel_rate(state);
b432e5cf
VS
9850 int cdclk;
9851
9852 /*
9853 * FIXME should also account for plane ratio
9854 * once 64bpp pixel formats are supported.
9855 */
587c7914 9856 cdclk = broadwell_calc_cdclk(max_pixclk);
b432e5cf 9857
b432e5cf 9858 if (cdclk > dev_priv->max_cdclk_freq) {
63ba534e
ML
9859 DRM_DEBUG_KMS("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
9860 cdclk, dev_priv->max_cdclk_freq);
9861 return -EINVAL;
b432e5cf
VS
9862 }
9863
1a617b77
ML
9864 intel_state->cdclk = intel_state->dev_cdclk = cdclk;
9865 if (!intel_state->active_crtcs)
587c7914 9866 intel_state->dev_cdclk = broadwell_calc_cdclk(0);
b432e5cf
VS
9867
9868 return 0;
9869}
9870
27c329ed 9871static void broadwell_modeset_commit_cdclk(struct drm_atomic_state *old_state)
b432e5cf 9872{
27c329ed 9873 struct drm_device *dev = old_state->dev;
1a617b77
ML
9874 struct intel_atomic_state *old_intel_state =
9875 to_intel_atomic_state(old_state);
9876 unsigned req_cdclk = old_intel_state->dev_cdclk;
b432e5cf 9877
27c329ed 9878 broadwell_set_cdclk(dev, req_cdclk);
b432e5cf
VS
9879}
9880
c89e39f3
CT
9881static int skl_modeset_calc_cdclk(struct drm_atomic_state *state)
9882{
9883 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
9884 struct drm_i915_private *dev_priv = to_i915(state->dev);
9885 const int max_pixclk = ilk_max_pixel_rate(state);
a8ca4934 9886 int vco = intel_state->cdclk_pll_vco;
c89e39f3
CT
9887 int cdclk;
9888
9889 /*
9890 * FIXME should also account for plane ratio
9891 * once 64bpp pixel formats are supported.
9892 */
a8ca4934 9893 cdclk = skl_calc_cdclk(max_pixclk, vco);
c89e39f3
CT
9894
9895 /*
9896 * FIXME move the cdclk caclulation to
9897 * compute_config() so we can fail gracegully.
9898 */
9899 if (cdclk > dev_priv->max_cdclk_freq) {
9900 DRM_ERROR("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
9901 cdclk, dev_priv->max_cdclk_freq);
9902 cdclk = dev_priv->max_cdclk_freq;
9903 }
9904
9905 intel_state->cdclk = intel_state->dev_cdclk = cdclk;
9906 if (!intel_state->active_crtcs)
a8ca4934 9907 intel_state->dev_cdclk = skl_calc_cdclk(0, vco);
c89e39f3
CT
9908
9909 return 0;
9910}
9911
9912static void skl_modeset_commit_cdclk(struct drm_atomic_state *old_state)
9913{
1cd593e0
VS
9914 struct drm_i915_private *dev_priv = to_i915(old_state->dev);
9915 struct intel_atomic_state *intel_state = to_intel_atomic_state(old_state);
9916 unsigned int req_cdclk = intel_state->dev_cdclk;
9917 unsigned int req_vco = intel_state->cdclk_pll_vco;
c89e39f3 9918
1cd593e0 9919 skl_set_cdclk(dev_priv, req_cdclk, req_vco);
c89e39f3
CT
9920}
9921
190f68c5
ACO
9922static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
9923 struct intel_crtc_state *crtc_state)
09b4ddf9 9924{
af3997b5
MK
9925 struct intel_encoder *intel_encoder =
9926 intel_ddi_get_crtc_new_encoder(crtc_state);
9927
9928 if (intel_encoder->type != INTEL_OUTPUT_DSI) {
9929 if (!intel_ddi_pll_select(crtc, crtc_state))
9930 return -EINVAL;
9931 }
716c2e55 9932
c7653199 9933 crtc->lowfreq_avail = false;
644cef34 9934
c8f7a0db 9935 return 0;
79e53945
JB
9936}
9937
3760b59c
S
9938static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
9939 enum port port,
9940 struct intel_crtc_state *pipe_config)
9941{
8106ddbd
ACO
9942 enum intel_dpll_id id;
9943
3760b59c
S
9944 switch (port) {
9945 case PORT_A:
9946 pipe_config->ddi_pll_sel = SKL_DPLL0;
08250c4b 9947 id = DPLL_ID_SKL_DPLL0;
3760b59c
S
9948 break;
9949 case PORT_B:
9950 pipe_config->ddi_pll_sel = SKL_DPLL1;
08250c4b 9951 id = DPLL_ID_SKL_DPLL1;
3760b59c
S
9952 break;
9953 case PORT_C:
9954 pipe_config->ddi_pll_sel = SKL_DPLL2;
08250c4b 9955 id = DPLL_ID_SKL_DPLL2;
3760b59c
S
9956 break;
9957 default:
9958 DRM_ERROR("Incorrect port type\n");
8106ddbd 9959 return;
3760b59c 9960 }
8106ddbd
ACO
9961
9962 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
3760b59c
S
9963}
9964
96b7dfb7
S
9965static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
9966 enum port port,
5cec258b 9967 struct intel_crtc_state *pipe_config)
96b7dfb7 9968{
8106ddbd 9969 enum intel_dpll_id id;
a3c988ea 9970 u32 temp;
96b7dfb7
S
9971
9972 temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
9973 pipe_config->ddi_pll_sel = temp >> (port * 3 + 1);
9974
9975 switch (pipe_config->ddi_pll_sel) {
3148ade7 9976 case SKL_DPLL0:
a3c988ea
ACO
9977 id = DPLL_ID_SKL_DPLL0;
9978 break;
96b7dfb7 9979 case SKL_DPLL1:
8106ddbd 9980 id = DPLL_ID_SKL_DPLL1;
96b7dfb7
S
9981 break;
9982 case SKL_DPLL2:
8106ddbd 9983 id = DPLL_ID_SKL_DPLL2;
96b7dfb7
S
9984 break;
9985 case SKL_DPLL3:
8106ddbd 9986 id = DPLL_ID_SKL_DPLL3;
96b7dfb7 9987 break;
8106ddbd
ACO
9988 default:
9989 MISSING_CASE(pipe_config->ddi_pll_sel);
9990 return;
96b7dfb7 9991 }
8106ddbd
ACO
9992
9993 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
96b7dfb7
S
9994}
9995
7d2c8175
DL
9996static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
9997 enum port port,
5cec258b 9998 struct intel_crtc_state *pipe_config)
7d2c8175 9999{
8106ddbd
ACO
10000 enum intel_dpll_id id;
10001
7d2c8175
DL
10002 pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
10003
10004 switch (pipe_config->ddi_pll_sel) {
10005 case PORT_CLK_SEL_WRPLL1:
8106ddbd 10006 id = DPLL_ID_WRPLL1;
7d2c8175
DL
10007 break;
10008 case PORT_CLK_SEL_WRPLL2:
8106ddbd 10009 id = DPLL_ID_WRPLL2;
7d2c8175 10010 break;
00490c22 10011 case PORT_CLK_SEL_SPLL:
8106ddbd 10012 id = DPLL_ID_SPLL;
79bd23da 10013 break;
9d16da65
ACO
10014 case PORT_CLK_SEL_LCPLL_810:
10015 id = DPLL_ID_LCPLL_810;
10016 break;
10017 case PORT_CLK_SEL_LCPLL_1350:
10018 id = DPLL_ID_LCPLL_1350;
10019 break;
10020 case PORT_CLK_SEL_LCPLL_2700:
10021 id = DPLL_ID_LCPLL_2700;
10022 break;
8106ddbd
ACO
10023 default:
10024 MISSING_CASE(pipe_config->ddi_pll_sel);
10025 /* fall through */
10026 case PORT_CLK_SEL_NONE:
8106ddbd 10027 return;
7d2c8175 10028 }
8106ddbd
ACO
10029
10030 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
7d2c8175
DL
10031}
10032
cf30429e
JN
10033static bool hsw_get_transcoder_state(struct intel_crtc *crtc,
10034 struct intel_crtc_state *pipe_config,
10035 unsigned long *power_domain_mask)
10036{
10037 struct drm_device *dev = crtc->base.dev;
10038 struct drm_i915_private *dev_priv = dev->dev_private;
10039 enum intel_display_power_domain power_domain;
10040 u32 tmp;
10041
d9a7bc67
ID
10042 /*
10043 * The pipe->transcoder mapping is fixed with the exception of the eDP
10044 * transcoder handled below.
10045 */
cf30429e
JN
10046 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
10047
10048 /*
10049 * XXX: Do intel_display_power_get_if_enabled before reading this (for
10050 * consistency and less surprising code; it's in always on power).
10051 */
10052 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
10053 if (tmp & TRANS_DDI_FUNC_ENABLE) {
10054 enum pipe trans_edp_pipe;
10055 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
10056 default:
10057 WARN(1, "unknown pipe linked to edp transcoder\n");
10058 case TRANS_DDI_EDP_INPUT_A_ONOFF:
10059 case TRANS_DDI_EDP_INPUT_A_ON:
10060 trans_edp_pipe = PIPE_A;
10061 break;
10062 case TRANS_DDI_EDP_INPUT_B_ONOFF:
10063 trans_edp_pipe = PIPE_B;
10064 break;
10065 case TRANS_DDI_EDP_INPUT_C_ONOFF:
10066 trans_edp_pipe = PIPE_C;
10067 break;
10068 }
10069
10070 if (trans_edp_pipe == crtc->pipe)
10071 pipe_config->cpu_transcoder = TRANSCODER_EDP;
10072 }
10073
10074 power_domain = POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder);
10075 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
10076 return false;
10077 *power_domain_mask |= BIT(power_domain);
10078
10079 tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
10080
10081 return tmp & PIPECONF_ENABLE;
10082}
10083
4d1de975
JN
10084static bool bxt_get_dsi_transcoder_state(struct intel_crtc *crtc,
10085 struct intel_crtc_state *pipe_config,
10086 unsigned long *power_domain_mask)
10087{
10088 struct drm_device *dev = crtc->base.dev;
10089 struct drm_i915_private *dev_priv = dev->dev_private;
10090 enum intel_display_power_domain power_domain;
10091 enum port port;
10092 enum transcoder cpu_transcoder;
10093 u32 tmp;
10094
10095 pipe_config->has_dsi_encoder = false;
10096
10097 for_each_port_masked(port, BIT(PORT_A) | BIT(PORT_C)) {
10098 if (port == PORT_A)
10099 cpu_transcoder = TRANSCODER_DSI_A;
10100 else
10101 cpu_transcoder = TRANSCODER_DSI_C;
10102
10103 power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
10104 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
10105 continue;
10106 *power_domain_mask |= BIT(power_domain);
10107
db18b6a6
ID
10108 /*
10109 * The PLL needs to be enabled with a valid divider
10110 * configuration, otherwise accessing DSI registers will hang
10111 * the machine. See BSpec North Display Engine
10112 * registers/MIPI[BXT]. We can break out here early, since we
10113 * need the same DSI PLL to be enabled for both DSI ports.
10114 */
10115 if (!intel_dsi_pll_is_enabled(dev_priv))
10116 break;
10117
4d1de975
JN
10118 /* XXX: this works for video mode only */
10119 tmp = I915_READ(BXT_MIPI_PORT_CTRL(port));
10120 if (!(tmp & DPI_ENABLE))
10121 continue;
10122
10123 tmp = I915_READ(MIPI_CTRL(port));
10124 if ((tmp & BXT_PIPE_SELECT_MASK) != BXT_PIPE_SELECT(crtc->pipe))
10125 continue;
10126
10127 pipe_config->cpu_transcoder = cpu_transcoder;
10128 pipe_config->has_dsi_encoder = true;
10129 break;
10130 }
10131
10132 return pipe_config->has_dsi_encoder;
10133}
10134
26804afd 10135static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
5cec258b 10136 struct intel_crtc_state *pipe_config)
26804afd
DV
10137{
10138 struct drm_device *dev = crtc->base.dev;
10139 struct drm_i915_private *dev_priv = dev->dev_private;
d452c5b6 10140 struct intel_shared_dpll *pll;
26804afd
DV
10141 enum port port;
10142 uint32_t tmp;
10143
10144 tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
10145
10146 port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
10147
ef11bdb3 10148 if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev))
96b7dfb7 10149 skylake_get_ddi_pll(dev_priv, port, pipe_config);
3760b59c
S
10150 else if (IS_BROXTON(dev))
10151 bxt_get_ddi_pll(dev_priv, port, pipe_config);
96b7dfb7
S
10152 else
10153 haswell_get_ddi_pll(dev_priv, port, pipe_config);
9cd86933 10154
8106ddbd
ACO
10155 pll = pipe_config->shared_dpll;
10156 if (pll) {
2edd6443
ACO
10157 WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
10158 &pipe_config->dpll_hw_state));
d452c5b6
DV
10159 }
10160
26804afd
DV
10161 /*
10162 * Haswell has only FDI/PCH transcoder A. It is which is connected to
10163 * DDI E. So just check whether this pipe is wired to DDI E and whether
10164 * the PCH transcoder is on.
10165 */
ca370455
DL
10166 if (INTEL_INFO(dev)->gen < 9 &&
10167 (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
26804afd
DV
10168 pipe_config->has_pch_encoder = true;
10169
10170 tmp = I915_READ(FDI_RX_CTL(PIPE_A));
10171 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
10172 FDI_DP_PORT_WIDTH_SHIFT) + 1;
10173
10174 ironlake_get_fdi_m_n_config(crtc, pipe_config);
10175 }
10176}
10177
0e8ffe1b 10178static bool haswell_get_pipe_config(struct intel_crtc *crtc,
5cec258b 10179 struct intel_crtc_state *pipe_config)
0e8ffe1b
DV
10180{
10181 struct drm_device *dev = crtc->base.dev;
10182 struct drm_i915_private *dev_priv = dev->dev_private;
1729050e
ID
10183 enum intel_display_power_domain power_domain;
10184 unsigned long power_domain_mask;
cf30429e 10185 bool active;
0e8ffe1b 10186
1729050e
ID
10187 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
10188 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
b5482bd0 10189 return false;
1729050e
ID
10190 power_domain_mask = BIT(power_domain);
10191
8106ddbd 10192 pipe_config->shared_dpll = NULL;
c0d43d62 10193
cf30429e 10194 active = hsw_get_transcoder_state(crtc, pipe_config, &power_domain_mask);
eccb140b 10195
4d1de975
JN
10196 if (IS_BROXTON(dev_priv)) {
10197 bxt_get_dsi_transcoder_state(crtc, pipe_config,
10198 &power_domain_mask);
10199 WARN_ON(active && pipe_config->has_dsi_encoder);
10200 if (pipe_config->has_dsi_encoder)
10201 active = true;
10202 }
10203
cf30429e 10204 if (!active)
1729050e 10205 goto out;
0e8ffe1b 10206
4d1de975
JN
10207 if (!pipe_config->has_dsi_encoder) {
10208 haswell_get_ddi_port_state(crtc, pipe_config);
10209 intel_get_pipe_timings(crtc, pipe_config);
10210 }
627eb5a3 10211
bc58be60 10212 intel_get_pipe_src_size(crtc, pipe_config);
1bd1bd80 10213
05dc698c
LL
10214 pipe_config->gamma_mode =
10215 I915_READ(GAMMA_MODE(crtc->pipe)) & GAMMA_MODE_MODE_MASK;
10216
a1b2278e
CK
10217 if (INTEL_INFO(dev)->gen >= 9) {
10218 skl_init_scalers(dev, crtc, pipe_config);
10219 }
10220
af99ceda
CK
10221 if (INTEL_INFO(dev)->gen >= 9) {
10222 pipe_config->scaler_state.scaler_id = -1;
10223 pipe_config->scaler_state.scaler_users &= ~(1 << SKL_CRTC_INDEX);
10224 }
10225
1729050e
ID
10226 power_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
10227 if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
10228 power_domain_mask |= BIT(power_domain);
1c132b44 10229 if (INTEL_INFO(dev)->gen >= 9)
bd2e244f 10230 skylake_get_pfit_config(crtc, pipe_config);
ff6d9f55 10231 else
1c132b44 10232 ironlake_get_pfit_config(crtc, pipe_config);
bd2e244f 10233 }
88adfff1 10234
e59150dc
JB
10235 if (IS_HASWELL(dev))
10236 pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
10237 (I915_READ(IPS_CTL) & IPS_ENABLE);
42db64ef 10238
4d1de975
JN
10239 if (pipe_config->cpu_transcoder != TRANSCODER_EDP &&
10240 !transcoder_is_dsi(pipe_config->cpu_transcoder)) {
ebb69c95
CT
10241 pipe_config->pixel_multiplier =
10242 I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
10243 } else {
10244 pipe_config->pixel_multiplier = 1;
10245 }
6c49f241 10246
1729050e
ID
10247out:
10248 for_each_power_domain(power_domain, power_domain_mask)
10249 intel_display_power_put(dev_priv, power_domain);
10250
cf30429e 10251 return active;
0e8ffe1b
DV
10252}
10253
55a08b3f
ML
10254static void i845_update_cursor(struct drm_crtc *crtc, u32 base,
10255 const struct intel_plane_state *plane_state)
560b85bb
CW
10256{
10257 struct drm_device *dev = crtc->dev;
10258 struct drm_i915_private *dev_priv = dev->dev_private;
10259 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
dc41c154 10260 uint32_t cntl = 0, size = 0;
560b85bb 10261
55a08b3f
ML
10262 if (plane_state && plane_state->visible) {
10263 unsigned int width = plane_state->base.crtc_w;
10264 unsigned int height = plane_state->base.crtc_h;
dc41c154
VS
10265 unsigned int stride = roundup_pow_of_two(width) * 4;
10266
10267 switch (stride) {
10268 default:
10269 WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
10270 width, stride);
10271 stride = 256;
10272 /* fallthrough */
10273 case 256:
10274 case 512:
10275 case 1024:
10276 case 2048:
10277 break;
4b0e333e
CW
10278 }
10279
dc41c154
VS
10280 cntl |= CURSOR_ENABLE |
10281 CURSOR_GAMMA_ENABLE |
10282 CURSOR_FORMAT_ARGB |
10283 CURSOR_STRIDE(stride);
10284
10285 size = (height << 12) | width;
4b0e333e 10286 }
560b85bb 10287
dc41c154
VS
10288 if (intel_crtc->cursor_cntl != 0 &&
10289 (intel_crtc->cursor_base != base ||
10290 intel_crtc->cursor_size != size ||
10291 intel_crtc->cursor_cntl != cntl)) {
10292 /* On these chipsets we can only modify the base/size/stride
10293 * whilst the cursor is disabled.
10294 */
0b87c24e
VS
10295 I915_WRITE(CURCNTR(PIPE_A), 0);
10296 POSTING_READ(CURCNTR(PIPE_A));
dc41c154 10297 intel_crtc->cursor_cntl = 0;
4b0e333e 10298 }
560b85bb 10299
99d1f387 10300 if (intel_crtc->cursor_base != base) {
0b87c24e 10301 I915_WRITE(CURBASE(PIPE_A), base);
99d1f387
VS
10302 intel_crtc->cursor_base = base;
10303 }
4726e0b0 10304
dc41c154
VS
10305 if (intel_crtc->cursor_size != size) {
10306 I915_WRITE(CURSIZE, size);
10307 intel_crtc->cursor_size = size;
4b0e333e 10308 }
560b85bb 10309
4b0e333e 10310 if (intel_crtc->cursor_cntl != cntl) {
0b87c24e
VS
10311 I915_WRITE(CURCNTR(PIPE_A), cntl);
10312 POSTING_READ(CURCNTR(PIPE_A));
4b0e333e 10313 intel_crtc->cursor_cntl = cntl;
560b85bb 10314 }
560b85bb
CW
10315}
10316
55a08b3f
ML
10317static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base,
10318 const struct intel_plane_state *plane_state)
65a21cd6
JB
10319{
10320 struct drm_device *dev = crtc->dev;
10321 struct drm_i915_private *dev_priv = dev->dev_private;
10322 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10323 int pipe = intel_crtc->pipe;
663f3122 10324 uint32_t cntl = 0;
4b0e333e 10325
55a08b3f 10326 if (plane_state && plane_state->visible) {
4b0e333e 10327 cntl = MCURSOR_GAMMA_ENABLE;
55a08b3f 10328 switch (plane_state->base.crtc_w) {
4726e0b0
SK
10329 case 64:
10330 cntl |= CURSOR_MODE_64_ARGB_AX;
10331 break;
10332 case 128:
10333 cntl |= CURSOR_MODE_128_ARGB_AX;
10334 break;
10335 case 256:
10336 cntl |= CURSOR_MODE_256_ARGB_AX;
10337 break;
10338 default:
55a08b3f 10339 MISSING_CASE(plane_state->base.crtc_w);
4726e0b0 10340 return;
65a21cd6 10341 }
4b0e333e 10342 cntl |= pipe << 28; /* Connect to correct pipe */
47bf17a7 10343
fc6f93bc 10344 if (HAS_DDI(dev))
47bf17a7 10345 cntl |= CURSOR_PIPE_CSC_ENABLE;
65a21cd6 10346
55a08b3f
ML
10347 if (plane_state->base.rotation == BIT(DRM_ROTATE_180))
10348 cntl |= CURSOR_ROTATE_180;
10349 }
4398ad45 10350
4b0e333e
CW
10351 if (intel_crtc->cursor_cntl != cntl) {
10352 I915_WRITE(CURCNTR(pipe), cntl);
10353 POSTING_READ(CURCNTR(pipe));
10354 intel_crtc->cursor_cntl = cntl;
65a21cd6 10355 }
4b0e333e 10356
65a21cd6 10357 /* and commit changes on next vblank */
5efb3e28
VS
10358 I915_WRITE(CURBASE(pipe), base);
10359 POSTING_READ(CURBASE(pipe));
99d1f387
VS
10360
10361 intel_crtc->cursor_base = base;
65a21cd6
JB
10362}
10363
cda4b7d3 10364/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
6b383a7f 10365static void intel_crtc_update_cursor(struct drm_crtc *crtc,
55a08b3f 10366 const struct intel_plane_state *plane_state)
cda4b7d3
CW
10367{
10368 struct drm_device *dev = crtc->dev;
10369 struct drm_i915_private *dev_priv = dev->dev_private;
10370 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10371 int pipe = intel_crtc->pipe;
55a08b3f
ML
10372 u32 base = intel_crtc->cursor_addr;
10373 u32 pos = 0;
cda4b7d3 10374
55a08b3f
ML
10375 if (plane_state) {
10376 int x = plane_state->base.crtc_x;
10377 int y = plane_state->base.crtc_y;
cda4b7d3 10378
55a08b3f
ML
10379 if (x < 0) {
10380 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
10381 x = -x;
10382 }
10383 pos |= x << CURSOR_X_SHIFT;
cda4b7d3 10384
55a08b3f
ML
10385 if (y < 0) {
10386 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
10387 y = -y;
10388 }
10389 pos |= y << CURSOR_Y_SHIFT;
10390
10391 /* ILK+ do this automagically */
10392 if (HAS_GMCH_DISPLAY(dev) &&
10393 plane_state->base.rotation == BIT(DRM_ROTATE_180)) {
10394 base += (plane_state->base.crtc_h *
10395 plane_state->base.crtc_w - 1) * 4;
10396 }
cda4b7d3 10397 }
cda4b7d3 10398
5efb3e28
VS
10399 I915_WRITE(CURPOS(pipe), pos);
10400
8ac54669 10401 if (IS_845G(dev) || IS_I865G(dev))
55a08b3f 10402 i845_update_cursor(crtc, base, plane_state);
5efb3e28 10403 else
55a08b3f 10404 i9xx_update_cursor(crtc, base, plane_state);
cda4b7d3
CW
10405}
10406
dc41c154
VS
10407static bool cursor_size_ok(struct drm_device *dev,
10408 uint32_t width, uint32_t height)
10409{
10410 if (width == 0 || height == 0)
10411 return false;
10412
10413 /*
10414 * 845g/865g are special in that they are only limited by
10415 * the width of their cursors, the height is arbitrary up to
10416 * the precision of the register. Everything else requires
10417 * square cursors, limited to a few power-of-two sizes.
10418 */
10419 if (IS_845G(dev) || IS_I865G(dev)) {
10420 if ((width & 63) != 0)
10421 return false;
10422
10423 if (width > (IS_845G(dev) ? 64 : 512))
10424 return false;
10425
10426 if (height > 1023)
10427 return false;
10428 } else {
10429 switch (width | height) {
10430 case 256:
10431 case 128:
10432 if (IS_GEN2(dev))
10433 return false;
10434 case 64:
10435 break;
10436 default:
10437 return false;
10438 }
10439 }
10440
10441 return true;
10442}
10443
79e53945
JB
10444/* VESA 640x480x72Hz mode to set on the pipe */
10445static struct drm_display_mode load_detect_mode = {
10446 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
10447 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
10448};
10449
a8bb6818
DV
10450struct drm_framebuffer *
10451__intel_framebuffer_create(struct drm_device *dev,
10452 struct drm_mode_fb_cmd2 *mode_cmd,
10453 struct drm_i915_gem_object *obj)
d2dff872
CW
10454{
10455 struct intel_framebuffer *intel_fb;
10456 int ret;
10457
10458 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
dcb1394e 10459 if (!intel_fb)
d2dff872 10460 return ERR_PTR(-ENOMEM);
d2dff872
CW
10461
10462 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
dd4916c5
DV
10463 if (ret)
10464 goto err;
d2dff872
CW
10465
10466 return &intel_fb->base;
dcb1394e 10467
dd4916c5 10468err:
dd4916c5 10469 kfree(intel_fb);
dd4916c5 10470 return ERR_PTR(ret);
d2dff872
CW
10471}
10472
b5ea642a 10473static struct drm_framebuffer *
a8bb6818
DV
10474intel_framebuffer_create(struct drm_device *dev,
10475 struct drm_mode_fb_cmd2 *mode_cmd,
10476 struct drm_i915_gem_object *obj)
10477{
10478 struct drm_framebuffer *fb;
10479 int ret;
10480
10481 ret = i915_mutex_lock_interruptible(dev);
10482 if (ret)
10483 return ERR_PTR(ret);
10484 fb = __intel_framebuffer_create(dev, mode_cmd, obj);
10485 mutex_unlock(&dev->struct_mutex);
10486
10487 return fb;
10488}
10489
d2dff872
CW
10490static u32
10491intel_framebuffer_pitch_for_width(int width, int bpp)
10492{
10493 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
10494 return ALIGN(pitch, 64);
10495}
10496
10497static u32
10498intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
10499{
10500 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
1267a26b 10501 return PAGE_ALIGN(pitch * mode->vdisplay);
d2dff872
CW
10502}
10503
10504static struct drm_framebuffer *
10505intel_framebuffer_create_for_mode(struct drm_device *dev,
10506 struct drm_display_mode *mode,
10507 int depth, int bpp)
10508{
dcb1394e 10509 struct drm_framebuffer *fb;
d2dff872 10510 struct drm_i915_gem_object *obj;
0fed39bd 10511 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
d2dff872 10512
d37cd8a8 10513 obj = i915_gem_object_create(dev,
d2dff872 10514 intel_framebuffer_size_for_mode(mode, bpp));
fe3db79b
CW
10515 if (IS_ERR(obj))
10516 return ERR_CAST(obj);
d2dff872
CW
10517
10518 mode_cmd.width = mode->hdisplay;
10519 mode_cmd.height = mode->vdisplay;
308e5bcb
JB
10520 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
10521 bpp);
5ca0c34a 10522 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
d2dff872 10523
dcb1394e
LW
10524 fb = intel_framebuffer_create(dev, &mode_cmd, obj);
10525 if (IS_ERR(fb))
10526 drm_gem_object_unreference_unlocked(&obj->base);
10527
10528 return fb;
d2dff872
CW
10529}
10530
10531static struct drm_framebuffer *
10532mode_fits_in_fbdev(struct drm_device *dev,
10533 struct drm_display_mode *mode)
10534{
0695726e 10535#ifdef CONFIG_DRM_FBDEV_EMULATION
d2dff872
CW
10536 struct drm_i915_private *dev_priv = dev->dev_private;
10537 struct drm_i915_gem_object *obj;
10538 struct drm_framebuffer *fb;
10539
4c0e5528 10540 if (!dev_priv->fbdev)
d2dff872
CW
10541 return NULL;
10542
4c0e5528 10543 if (!dev_priv->fbdev->fb)
d2dff872
CW
10544 return NULL;
10545
4c0e5528
DV
10546 obj = dev_priv->fbdev->fb->obj;
10547 BUG_ON(!obj);
10548
8bcd4553 10549 fb = &dev_priv->fbdev->fb->base;
01f2c773
VS
10550 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
10551 fb->bits_per_pixel))
d2dff872
CW
10552 return NULL;
10553
01f2c773 10554 if (obj->base.size < mode->vdisplay * fb->pitches[0])
d2dff872
CW
10555 return NULL;
10556
edde3617 10557 drm_framebuffer_reference(fb);
d2dff872 10558 return fb;
4520f53a
DV
10559#else
10560 return NULL;
10561#endif
d2dff872
CW
10562}
10563
d3a40d1b
ACO
10564static int intel_modeset_setup_plane_state(struct drm_atomic_state *state,
10565 struct drm_crtc *crtc,
10566 struct drm_display_mode *mode,
10567 struct drm_framebuffer *fb,
10568 int x, int y)
10569{
10570 struct drm_plane_state *plane_state;
10571 int hdisplay, vdisplay;
10572 int ret;
10573
10574 plane_state = drm_atomic_get_plane_state(state, crtc->primary);
10575 if (IS_ERR(plane_state))
10576 return PTR_ERR(plane_state);
10577
10578 if (mode)
10579 drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
10580 else
10581 hdisplay = vdisplay = 0;
10582
10583 ret = drm_atomic_set_crtc_for_plane(plane_state, fb ? crtc : NULL);
10584 if (ret)
10585 return ret;
10586 drm_atomic_set_fb_for_plane(plane_state, fb);
10587 plane_state->crtc_x = 0;
10588 plane_state->crtc_y = 0;
10589 plane_state->crtc_w = hdisplay;
10590 plane_state->crtc_h = vdisplay;
10591 plane_state->src_x = x << 16;
10592 plane_state->src_y = y << 16;
10593 plane_state->src_w = hdisplay << 16;
10594 plane_state->src_h = vdisplay << 16;
10595
10596 return 0;
10597}
10598
d2434ab7 10599bool intel_get_load_detect_pipe(struct drm_connector *connector,
7173188d 10600 struct drm_display_mode *mode,
51fd371b
RC
10601 struct intel_load_detect_pipe *old,
10602 struct drm_modeset_acquire_ctx *ctx)
79e53945
JB
10603{
10604 struct intel_crtc *intel_crtc;
d2434ab7
DV
10605 struct intel_encoder *intel_encoder =
10606 intel_attached_encoder(connector);
79e53945 10607 struct drm_crtc *possible_crtc;
4ef69c7a 10608 struct drm_encoder *encoder = &intel_encoder->base;
79e53945
JB
10609 struct drm_crtc *crtc = NULL;
10610 struct drm_device *dev = encoder->dev;
94352cf9 10611 struct drm_framebuffer *fb;
51fd371b 10612 struct drm_mode_config *config = &dev->mode_config;
edde3617 10613 struct drm_atomic_state *state = NULL, *restore_state = NULL;
944b0c76 10614 struct drm_connector_state *connector_state;
4be07317 10615 struct intel_crtc_state *crtc_state;
51fd371b 10616 int ret, i = -1;
79e53945 10617
d2dff872 10618 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
c23cc417 10619 connector->base.id, connector->name,
8e329a03 10620 encoder->base.id, encoder->name);
d2dff872 10621
edde3617
ML
10622 old->restore_state = NULL;
10623
51fd371b
RC
10624retry:
10625 ret = drm_modeset_lock(&config->connection_mutex, ctx);
10626 if (ret)
ad3c558f 10627 goto fail;
6e9f798d 10628
79e53945
JB
10629 /*
10630 * Algorithm gets a little messy:
7a5e4805 10631 *
79e53945
JB
10632 * - if the connector already has an assigned crtc, use it (but make
10633 * sure it's on first)
7a5e4805 10634 *
79e53945
JB
10635 * - try to find the first unused crtc that can drive this connector,
10636 * and use that if we find one
79e53945
JB
10637 */
10638
10639 /* See if we already have a CRTC for this connector */
edde3617
ML
10640 if (connector->state->crtc) {
10641 crtc = connector->state->crtc;
8261b191 10642
51fd371b 10643 ret = drm_modeset_lock(&crtc->mutex, ctx);
4d02e2de 10644 if (ret)
ad3c558f 10645 goto fail;
8261b191
CW
10646
10647 /* Make sure the crtc and connector are running */
edde3617 10648 goto found;
79e53945
JB
10649 }
10650
10651 /* Find an unused one (if possible) */
70e1e0ec 10652 for_each_crtc(dev, possible_crtc) {
79e53945
JB
10653 i++;
10654 if (!(encoder->possible_crtcs & (1 << i)))
10655 continue;
edde3617
ML
10656
10657 ret = drm_modeset_lock(&possible_crtc->mutex, ctx);
10658 if (ret)
10659 goto fail;
10660
10661 if (possible_crtc->state->enable) {
10662 drm_modeset_unlock(&possible_crtc->mutex);
a459249c 10663 continue;
edde3617 10664 }
a459249c
VS
10665
10666 crtc = possible_crtc;
10667 break;
79e53945
JB
10668 }
10669
10670 /*
10671 * If we didn't find an unused CRTC, don't use any.
10672 */
10673 if (!crtc) {
7173188d 10674 DRM_DEBUG_KMS("no pipe available for load-detect\n");
ad3c558f 10675 goto fail;
79e53945
JB
10676 }
10677
edde3617
ML
10678found:
10679 intel_crtc = to_intel_crtc(crtc);
10680
4d02e2de
DV
10681 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
10682 if (ret)
ad3c558f 10683 goto fail;
79e53945 10684
83a57153 10685 state = drm_atomic_state_alloc(dev);
edde3617
ML
10686 restore_state = drm_atomic_state_alloc(dev);
10687 if (!state || !restore_state) {
10688 ret = -ENOMEM;
10689 goto fail;
10690 }
83a57153
ACO
10691
10692 state->acquire_ctx = ctx;
edde3617 10693 restore_state->acquire_ctx = ctx;
83a57153 10694
944b0c76
ACO
10695 connector_state = drm_atomic_get_connector_state(state, connector);
10696 if (IS_ERR(connector_state)) {
10697 ret = PTR_ERR(connector_state);
10698 goto fail;
10699 }
10700
edde3617
ML
10701 ret = drm_atomic_set_crtc_for_connector(connector_state, crtc);
10702 if (ret)
10703 goto fail;
944b0c76 10704
4be07317
ACO
10705 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
10706 if (IS_ERR(crtc_state)) {
10707 ret = PTR_ERR(crtc_state);
10708 goto fail;
10709 }
10710
49d6fa21 10711 crtc_state->base.active = crtc_state->base.enable = true;
4be07317 10712
6492711d
CW
10713 if (!mode)
10714 mode = &load_detect_mode;
79e53945 10715
d2dff872
CW
10716 /* We need a framebuffer large enough to accommodate all accesses
10717 * that the plane may generate whilst we perform load detection.
10718 * We can not rely on the fbcon either being present (we get called
10719 * during its initialisation to detect all boot displays, or it may
10720 * not even exist) or that it is large enough to satisfy the
10721 * requested mode.
10722 */
94352cf9
DV
10723 fb = mode_fits_in_fbdev(dev, mode);
10724 if (fb == NULL) {
d2dff872 10725 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
94352cf9 10726 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
d2dff872
CW
10727 } else
10728 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
94352cf9 10729 if (IS_ERR(fb)) {
d2dff872 10730 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
412b61d8 10731 goto fail;
79e53945 10732 }
79e53945 10733
d3a40d1b
ACO
10734 ret = intel_modeset_setup_plane_state(state, crtc, mode, fb, 0, 0);
10735 if (ret)
10736 goto fail;
10737
edde3617
ML
10738 drm_framebuffer_unreference(fb);
10739
10740 ret = drm_atomic_set_mode_for_crtc(&crtc_state->base, mode);
10741 if (ret)
10742 goto fail;
10743
10744 ret = PTR_ERR_OR_ZERO(drm_atomic_get_connector_state(restore_state, connector));
10745 if (!ret)
10746 ret = PTR_ERR_OR_ZERO(drm_atomic_get_crtc_state(restore_state, crtc));
10747 if (!ret)
10748 ret = PTR_ERR_OR_ZERO(drm_atomic_get_plane_state(restore_state, crtc->primary));
10749 if (ret) {
10750 DRM_DEBUG_KMS("Failed to create a copy of old state to restore: %i\n", ret);
10751 goto fail;
10752 }
8c7b5ccb 10753
3ba86073
ML
10754 ret = drm_atomic_commit(state);
10755 if (ret) {
6492711d 10756 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
412b61d8 10757 goto fail;
79e53945 10758 }
edde3617
ML
10759
10760 old->restore_state = restore_state;
7173188d 10761
79e53945 10762 /* let the connector get through one full cycle before testing */
9d0498a2 10763 intel_wait_for_vblank(dev, intel_crtc->pipe);
7173188d 10764 return true;
412b61d8 10765
ad3c558f 10766fail:
e5d958ef 10767 drm_atomic_state_free(state);
edde3617
ML
10768 drm_atomic_state_free(restore_state);
10769 restore_state = state = NULL;
83a57153 10770
51fd371b
RC
10771 if (ret == -EDEADLK) {
10772 drm_modeset_backoff(ctx);
10773 goto retry;
10774 }
10775
412b61d8 10776 return false;
79e53945
JB
10777}
10778
d2434ab7 10779void intel_release_load_detect_pipe(struct drm_connector *connector,
49172fee
ACO
10780 struct intel_load_detect_pipe *old,
10781 struct drm_modeset_acquire_ctx *ctx)
79e53945 10782{
d2434ab7
DV
10783 struct intel_encoder *intel_encoder =
10784 intel_attached_encoder(connector);
4ef69c7a 10785 struct drm_encoder *encoder = &intel_encoder->base;
edde3617 10786 struct drm_atomic_state *state = old->restore_state;
d3a40d1b 10787 int ret;
79e53945 10788
d2dff872 10789 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
c23cc417 10790 connector->base.id, connector->name,
8e329a03 10791 encoder->base.id, encoder->name);
d2dff872 10792
edde3617 10793 if (!state)
0622a53c 10794 return;
79e53945 10795
edde3617
ML
10796 ret = drm_atomic_commit(state);
10797 if (ret) {
10798 DRM_DEBUG_KMS("Couldn't release load detect pipe: %i\n", ret);
10799 drm_atomic_state_free(state);
10800 }
79e53945
JB
10801}
10802
da4a1efa 10803static int i9xx_pll_refclk(struct drm_device *dev,
5cec258b 10804 const struct intel_crtc_state *pipe_config)
da4a1efa
VS
10805{
10806 struct drm_i915_private *dev_priv = dev->dev_private;
10807 u32 dpll = pipe_config->dpll_hw_state.dpll;
10808
10809 if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
e91e941b 10810 return dev_priv->vbt.lvds_ssc_freq;
da4a1efa
VS
10811 else if (HAS_PCH_SPLIT(dev))
10812 return 120000;
10813 else if (!IS_GEN2(dev))
10814 return 96000;
10815 else
10816 return 48000;
10817}
10818
79e53945 10819/* Returns the clock of the currently programmed mode of the given pipe. */
f1f644dc 10820static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 10821 struct intel_crtc_state *pipe_config)
79e53945 10822{
f1f644dc 10823 struct drm_device *dev = crtc->base.dev;
79e53945 10824 struct drm_i915_private *dev_priv = dev->dev_private;
f1f644dc 10825 int pipe = pipe_config->cpu_transcoder;
293623f7 10826 u32 dpll = pipe_config->dpll_hw_state.dpll;
79e53945 10827 u32 fp;
9e2c8475 10828 struct dpll clock;
dccbea3b 10829 int port_clock;
da4a1efa 10830 int refclk = i9xx_pll_refclk(dev, pipe_config);
79e53945
JB
10831
10832 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
293623f7 10833 fp = pipe_config->dpll_hw_state.fp0;
79e53945 10834 else
293623f7 10835 fp = pipe_config->dpll_hw_state.fp1;
79e53945
JB
10836
10837 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
f2b115e6
AJ
10838 if (IS_PINEVIEW(dev)) {
10839 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
10840 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
2177832f
SL
10841 } else {
10842 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
10843 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
10844 }
10845
a6c45cf0 10846 if (!IS_GEN2(dev)) {
f2b115e6
AJ
10847 if (IS_PINEVIEW(dev))
10848 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
10849 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
2177832f
SL
10850 else
10851 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
79e53945
JB
10852 DPLL_FPA01_P1_POST_DIV_SHIFT);
10853
10854 switch (dpll & DPLL_MODE_MASK) {
10855 case DPLLB_MODE_DAC_SERIAL:
10856 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
10857 5 : 10;
10858 break;
10859 case DPLLB_MODE_LVDS:
10860 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
10861 7 : 14;
10862 break;
10863 default:
28c97730 10864 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
79e53945 10865 "mode\n", (int)(dpll & DPLL_MODE_MASK));
f1f644dc 10866 return;
79e53945
JB
10867 }
10868
ac58c3f0 10869 if (IS_PINEVIEW(dev))
dccbea3b 10870 port_clock = pnv_calc_dpll_params(refclk, &clock);
ac58c3f0 10871 else
dccbea3b 10872 port_clock = i9xx_calc_dpll_params(refclk, &clock);
79e53945 10873 } else {
0fb58223 10874 u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
b1c560d1 10875 bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
79e53945
JB
10876
10877 if (is_lvds) {
10878 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
10879 DPLL_FPA01_P1_POST_DIV_SHIFT);
b1c560d1
VS
10880
10881 if (lvds & LVDS_CLKB_POWER_UP)
10882 clock.p2 = 7;
10883 else
10884 clock.p2 = 14;
79e53945
JB
10885 } else {
10886 if (dpll & PLL_P1_DIVIDE_BY_TWO)
10887 clock.p1 = 2;
10888 else {
10889 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
10890 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
10891 }
10892 if (dpll & PLL_P2_DIVIDE_BY_4)
10893 clock.p2 = 4;
10894 else
10895 clock.p2 = 2;
79e53945 10896 }
da4a1efa 10897
dccbea3b 10898 port_clock = i9xx_calc_dpll_params(refclk, &clock);
79e53945
JB
10899 }
10900
18442d08
VS
10901 /*
10902 * This value includes pixel_multiplier. We will use
241bfc38 10903 * port_clock to compute adjusted_mode.crtc_clock in the
18442d08
VS
10904 * encoder's get_config() function.
10905 */
dccbea3b 10906 pipe_config->port_clock = port_clock;
f1f644dc
JB
10907}
10908
6878da05
VS
10909int intel_dotclock_calculate(int link_freq,
10910 const struct intel_link_m_n *m_n)
f1f644dc 10911{
f1f644dc
JB
10912 /*
10913 * The calculation for the data clock is:
1041a02f 10914 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
f1f644dc 10915 * But we want to avoid losing precison if possible, so:
1041a02f 10916 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
f1f644dc
JB
10917 *
10918 * and the link clock is simpler:
1041a02f 10919 * link_clock = (m * link_clock) / n
f1f644dc
JB
10920 */
10921
6878da05
VS
10922 if (!m_n->link_n)
10923 return 0;
f1f644dc 10924
6878da05
VS
10925 return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
10926}
f1f644dc 10927
18442d08 10928static void ironlake_pch_clock_get(struct intel_crtc *crtc,
5cec258b 10929 struct intel_crtc_state *pipe_config)
6878da05 10930{
e3b247da 10931 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
79e53945 10932
18442d08
VS
10933 /* read out port_clock from the DPLL */
10934 i9xx_crtc_clock_get(crtc, pipe_config);
f1f644dc 10935
f1f644dc 10936 /*
e3b247da
VS
10937 * In case there is an active pipe without active ports,
10938 * we may need some idea for the dotclock anyway.
10939 * Calculate one based on the FDI configuration.
79e53945 10940 */
2d112de7 10941 pipe_config->base.adjusted_mode.crtc_clock =
21a727b3 10942 intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
18442d08 10943 &pipe_config->fdi_m_n);
79e53945
JB
10944}
10945
10946/** Returns the currently programmed mode of the given pipe. */
10947struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
10948 struct drm_crtc *crtc)
10949{
548f245b 10950 struct drm_i915_private *dev_priv = dev->dev_private;
79e53945 10951 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6e3c9717 10952 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
79e53945 10953 struct drm_display_mode *mode;
3f36b937 10954 struct intel_crtc_state *pipe_config;
fe2b8f9d
PZ
10955 int htot = I915_READ(HTOTAL(cpu_transcoder));
10956 int hsync = I915_READ(HSYNC(cpu_transcoder));
10957 int vtot = I915_READ(VTOTAL(cpu_transcoder));
10958 int vsync = I915_READ(VSYNC(cpu_transcoder));
293623f7 10959 enum pipe pipe = intel_crtc->pipe;
79e53945
JB
10960
10961 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
10962 if (!mode)
10963 return NULL;
10964
3f36b937
TU
10965 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
10966 if (!pipe_config) {
10967 kfree(mode);
10968 return NULL;
10969 }
10970
f1f644dc
JB
10971 /*
10972 * Construct a pipe_config sufficient for getting the clock info
10973 * back out of crtc_clock_get.
10974 *
10975 * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
10976 * to use a real value here instead.
10977 */
3f36b937
TU
10978 pipe_config->cpu_transcoder = (enum transcoder) pipe;
10979 pipe_config->pixel_multiplier = 1;
10980 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(pipe));
10981 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(pipe));
10982 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(pipe));
10983 i9xx_crtc_clock_get(intel_crtc, pipe_config);
10984
10985 mode->clock = pipe_config->port_clock / pipe_config->pixel_multiplier;
79e53945
JB
10986 mode->hdisplay = (htot & 0xffff) + 1;
10987 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
10988 mode->hsync_start = (hsync & 0xffff) + 1;
10989 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
10990 mode->vdisplay = (vtot & 0xffff) + 1;
10991 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
10992 mode->vsync_start = (vsync & 0xffff) + 1;
10993 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
10994
10995 drm_mode_set_name(mode);
79e53945 10996
3f36b937
TU
10997 kfree(pipe_config);
10998
79e53945
JB
10999 return mode;
11000}
11001
7d993739 11002void intel_mark_busy(struct drm_i915_private *dev_priv)
f047e395 11003{
f62a0076
CW
11004 if (dev_priv->mm.busy)
11005 return;
11006
43694d69 11007 intel_runtime_pm_get(dev_priv);
c67a470b 11008 i915_update_gfx_val(dev_priv);
7d993739 11009 if (INTEL_GEN(dev_priv) >= 6)
43cf3bf0 11010 gen6_rps_busy(dev_priv);
f62a0076 11011 dev_priv->mm.busy = true;
f047e395
CW
11012}
11013
7d993739 11014void intel_mark_idle(struct drm_i915_private *dev_priv)
652c393a 11015{
f62a0076
CW
11016 if (!dev_priv->mm.busy)
11017 return;
11018
11019 dev_priv->mm.busy = false;
11020
7d993739
TU
11021 if (INTEL_GEN(dev_priv) >= 6)
11022 gen6_rps_idle(dev_priv);
bb4cdd53 11023
43694d69 11024 intel_runtime_pm_put(dev_priv);
652c393a
JB
11025}
11026
79e53945
JB
11027static void intel_crtc_destroy(struct drm_crtc *crtc)
11028{
11029 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
67e77c5a 11030 struct drm_device *dev = crtc->dev;
51cbaf01 11031 struct intel_flip_work *work;
67e77c5a 11032
5e2d7afc 11033 spin_lock_irq(&dev->event_lock);
5a21b665
DV
11034 work = intel_crtc->flip_work;
11035 intel_crtc->flip_work = NULL;
11036 spin_unlock_irq(&dev->event_lock);
67e77c5a 11037
5a21b665 11038 if (work) {
51cbaf01
ML
11039 cancel_work_sync(&work->mmio_work);
11040 cancel_work_sync(&work->unpin_work);
5a21b665 11041 kfree(work);
67e77c5a 11042 }
79e53945
JB
11043
11044 drm_crtc_cleanup(crtc);
67e77c5a 11045
79e53945
JB
11046 kfree(intel_crtc);
11047}
11048
6b95a207
KH
11049static void intel_unpin_work_fn(struct work_struct *__work)
11050{
51cbaf01
ML
11051 struct intel_flip_work *work =
11052 container_of(__work, struct intel_flip_work, unpin_work);
5a21b665
DV
11053 struct intel_crtc *crtc = to_intel_crtc(work->crtc);
11054 struct drm_device *dev = crtc->base.dev;
11055 struct drm_plane *primary = crtc->base.primary;
03f476e1 11056
5a21b665
DV
11057 if (is_mmio_work(work))
11058 flush_work(&work->mmio_work);
03f476e1 11059
5a21b665
DV
11060 mutex_lock(&dev->struct_mutex);
11061 intel_unpin_fb_obj(work->old_fb, primary->state->rotation);
11062 drm_gem_object_unreference(&work->pending_flip_obj->base);
143f73b3 11063
5a21b665
DV
11064 if (work->flip_queued_req)
11065 i915_gem_request_assign(&work->flip_queued_req, NULL);
11066 mutex_unlock(&dev->struct_mutex);
143f73b3 11067
5a21b665
DV
11068 intel_frontbuffer_flip_complete(dev, to_intel_plane(primary)->frontbuffer_bit);
11069 intel_fbc_post_update(crtc);
11070 drm_framebuffer_unreference(work->old_fb);
143f73b3 11071
5a21b665
DV
11072 BUG_ON(atomic_read(&crtc->unpin_work_count) == 0);
11073 atomic_dec(&crtc->unpin_work_count);
a6747b73 11074
5a21b665
DV
11075 kfree(work);
11076}
d9e86c0e 11077
5a21b665
DV
11078/* Is 'a' after or equal to 'b'? */
11079static bool g4x_flip_count_after_eq(u32 a, u32 b)
11080{
11081 return !((a - b) & 0x80000000);
11082}
143f73b3 11083
5a21b665
DV
11084static bool __pageflip_finished_cs(struct intel_crtc *crtc,
11085 struct intel_flip_work *work)
11086{
11087 struct drm_device *dev = crtc->base.dev;
11088 struct drm_i915_private *dev_priv = dev->dev_private;
11089 unsigned reset_counter;
143f73b3 11090
5a21b665
DV
11091 reset_counter = i915_reset_counter(&dev_priv->gpu_error);
11092 if (crtc->reset_counter != reset_counter)
11093 return true;
143f73b3 11094
5a21b665
DV
11095 /*
11096 * The relevant registers doen't exist on pre-ctg.
11097 * As the flip done interrupt doesn't trigger for mmio
11098 * flips on gmch platforms, a flip count check isn't
11099 * really needed there. But since ctg has the registers,
11100 * include it in the check anyway.
11101 */
11102 if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
11103 return true;
b4a98e57 11104
5a21b665
DV
11105 /*
11106 * BDW signals flip done immediately if the plane
11107 * is disabled, even if the plane enable is already
11108 * armed to occur at the next vblank :(
11109 */
f99d7069 11110
5a21b665
DV
11111 /*
11112 * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
11113 * used the same base address. In that case the mmio flip might
11114 * have completed, but the CS hasn't even executed the flip yet.
11115 *
11116 * A flip count check isn't enough as the CS might have updated
11117 * the base address just after start of vblank, but before we
11118 * managed to process the interrupt. This means we'd complete the
11119 * CS flip too soon.
11120 *
11121 * Combining both checks should get us a good enough result. It may
11122 * still happen that the CS flip has been executed, but has not
11123 * yet actually completed. But in case the base address is the same
11124 * anyway, we don't really care.
11125 */
11126 return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
11127 crtc->flip_work->gtt_offset &&
11128 g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_G4X(crtc->pipe)),
11129 crtc->flip_work->flip_count);
11130}
b4a98e57 11131
5a21b665
DV
11132static bool
11133__pageflip_finished_mmio(struct intel_crtc *crtc,
11134 struct intel_flip_work *work)
11135{
11136 /*
11137 * MMIO work completes when vblank is different from
11138 * flip_queued_vblank.
11139 *
11140 * Reset counter value doesn't matter, this is handled by
11141 * i915_wait_request finishing early, so no need to handle
11142 * reset here.
11143 */
11144 return intel_crtc_get_vblank_counter(crtc) != work->flip_queued_vblank;
6b95a207
KH
11145}
11146
51cbaf01
ML
11147
11148static bool pageflip_finished(struct intel_crtc *crtc,
11149 struct intel_flip_work *work)
11150{
11151 if (!atomic_read(&work->pending))
11152 return false;
11153
11154 smp_rmb();
11155
5a21b665
DV
11156 if (is_mmio_work(work))
11157 return __pageflip_finished_mmio(crtc, work);
11158 else
11159 return __pageflip_finished_cs(crtc, work);
11160}
11161
11162void intel_finish_page_flip_cs(struct drm_i915_private *dev_priv, int pipe)
11163{
11164 struct drm_device *dev = dev_priv->dev;
11165 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
11166 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11167 struct intel_flip_work *work;
11168 unsigned long flags;
11169
11170 /* Ignore early vblank irqs */
11171 if (!crtc)
11172 return;
11173
51cbaf01 11174 /*
5a21b665
DV
11175 * This is called both by irq handlers and the reset code (to complete
11176 * lost pageflips) so needs the full irqsave spinlocks.
51cbaf01 11177 */
5a21b665
DV
11178 spin_lock_irqsave(&dev->event_lock, flags);
11179 work = intel_crtc->flip_work;
11180
11181 if (work != NULL &&
11182 !is_mmio_work(work) &&
11183 pageflip_finished(intel_crtc, work))
11184 page_flip_completed(intel_crtc);
11185
11186 spin_unlock_irqrestore(&dev->event_lock, flags);
75f7f3ec
VS
11187}
11188
51cbaf01 11189void intel_finish_page_flip_mmio(struct drm_i915_private *dev_priv, int pipe)
6b95a207 11190{
91d14251 11191 struct drm_device *dev = dev_priv->dev;
5251f04e
ML
11192 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
11193 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
51cbaf01 11194 struct intel_flip_work *work;
6b95a207
KH
11195 unsigned long flags;
11196
5251f04e
ML
11197 /* Ignore early vblank irqs */
11198 if (!crtc)
11199 return;
f326038a
DV
11200
11201 /*
11202 * This is called both by irq handlers and the reset code (to complete
11203 * lost pageflips) so needs the full irqsave spinlocks.
e7d841ca 11204 */
6b95a207 11205 spin_lock_irqsave(&dev->event_lock, flags);
5a21b665 11206 work = intel_crtc->flip_work;
5251f04e 11207
5a21b665
DV
11208 if (work != NULL &&
11209 is_mmio_work(work) &&
11210 pageflip_finished(intel_crtc, work))
11211 page_flip_completed(intel_crtc);
5251f04e 11212
6b95a207
KH
11213 spin_unlock_irqrestore(&dev->event_lock, flags);
11214}
11215
5a21b665
DV
11216static inline void intel_mark_page_flip_active(struct intel_crtc *crtc,
11217 struct intel_flip_work *work)
84c33a64 11218{
5a21b665 11219 work->flip_queued_vblank = intel_crtc_get_vblank_counter(crtc);
84c33a64 11220
5a21b665
DV
11221 /* Ensure that the work item is consistent when activating it ... */
11222 smp_mb__before_atomic();
11223 atomic_set(&work->pending, 1);
11224}
a6747b73 11225
5a21b665
DV
11226static int intel_gen2_queue_flip(struct drm_device *dev,
11227 struct drm_crtc *crtc,
11228 struct drm_framebuffer *fb,
11229 struct drm_i915_gem_object *obj,
11230 struct drm_i915_gem_request *req,
11231 uint32_t flags)
11232{
11233 struct intel_engine_cs *engine = req->engine;
11234 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11235 u32 flip_mask;
11236 int ret;
143f73b3 11237
5a21b665
DV
11238 ret = intel_ring_begin(req, 6);
11239 if (ret)
11240 return ret;
143f73b3 11241
5a21b665
DV
11242 /* Can't queue multiple flips, so wait for the previous
11243 * one to finish before executing the next.
11244 */
11245 if (intel_crtc->plane)
11246 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
11247 else
11248 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
11249 intel_ring_emit(engine, MI_WAIT_FOR_EVENT | flip_mask);
11250 intel_ring_emit(engine, MI_NOOP);
11251 intel_ring_emit(engine, MI_DISPLAY_FLIP |
11252 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
11253 intel_ring_emit(engine, fb->pitches[0]);
11254 intel_ring_emit(engine, intel_crtc->flip_work->gtt_offset);
11255 intel_ring_emit(engine, 0); /* aux display base address, unused */
143f73b3 11256
5a21b665
DV
11257 return 0;
11258}
84c33a64 11259
5a21b665
DV
11260static int intel_gen3_queue_flip(struct drm_device *dev,
11261 struct drm_crtc *crtc,
11262 struct drm_framebuffer *fb,
11263 struct drm_i915_gem_object *obj,
11264 struct drm_i915_gem_request *req,
11265 uint32_t flags)
11266{
11267 struct intel_engine_cs *engine = req->engine;
11268 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11269 u32 flip_mask;
11270 int ret;
d55dbd06 11271
5a21b665
DV
11272 ret = intel_ring_begin(req, 6);
11273 if (ret)
11274 return ret;
d55dbd06 11275
5a21b665
DV
11276 if (intel_crtc->plane)
11277 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
11278 else
11279 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
11280 intel_ring_emit(engine, MI_WAIT_FOR_EVENT | flip_mask);
11281 intel_ring_emit(engine, MI_NOOP);
11282 intel_ring_emit(engine, MI_DISPLAY_FLIP_I915 |
11283 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
11284 intel_ring_emit(engine, fb->pitches[0]);
11285 intel_ring_emit(engine, intel_crtc->flip_work->gtt_offset);
11286 intel_ring_emit(engine, MI_NOOP);
fd8e058a 11287
5a21b665
DV
11288 return 0;
11289}
84c33a64 11290
5a21b665
DV
11291static int intel_gen4_queue_flip(struct drm_device *dev,
11292 struct drm_crtc *crtc,
11293 struct drm_framebuffer *fb,
11294 struct drm_i915_gem_object *obj,
11295 struct drm_i915_gem_request *req,
11296 uint32_t flags)
11297{
11298 struct intel_engine_cs *engine = req->engine;
11299 struct drm_i915_private *dev_priv = dev->dev_private;
11300 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11301 uint32_t pf, pipesrc;
11302 int ret;
143f73b3 11303
5a21b665
DV
11304 ret = intel_ring_begin(req, 4);
11305 if (ret)
11306 return ret;
143f73b3 11307
5a21b665
DV
11308 /* i965+ uses the linear or tiled offsets from the
11309 * Display Registers (which do not change across a page-flip)
11310 * so we need only reprogram the base address.
11311 */
11312 intel_ring_emit(engine, MI_DISPLAY_FLIP |
11313 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
11314 intel_ring_emit(engine, fb->pitches[0]);
11315 intel_ring_emit(engine, intel_crtc->flip_work->gtt_offset |
11316 obj->tiling_mode);
11317
11318 /* XXX Enabling the panel-fitter across page-flip is so far
11319 * untested on non-native modes, so ignore it for now.
11320 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
11321 */
11322 pf = 0;
11323 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
11324 intel_ring_emit(engine, pf | pipesrc);
143f73b3 11325
5a21b665 11326 return 0;
8c9f3aaf
JB
11327}
11328
5a21b665
DV
11329static int intel_gen6_queue_flip(struct drm_device *dev,
11330 struct drm_crtc *crtc,
11331 struct drm_framebuffer *fb,
11332 struct drm_i915_gem_object *obj,
11333 struct drm_i915_gem_request *req,
11334 uint32_t flags)
da20eabd 11335{
5a21b665
DV
11336 struct intel_engine_cs *engine = req->engine;
11337 struct drm_i915_private *dev_priv = dev->dev_private;
11338 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11339 uint32_t pf, pipesrc;
11340 int ret;
d21fbe87 11341
5a21b665
DV
11342 ret = intel_ring_begin(req, 4);
11343 if (ret)
11344 return ret;
92826fcd 11345
5a21b665
DV
11346 intel_ring_emit(engine, MI_DISPLAY_FLIP |
11347 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
11348 intel_ring_emit(engine, fb->pitches[0] | obj->tiling_mode);
11349 intel_ring_emit(engine, intel_crtc->flip_work->gtt_offset);
92826fcd 11350
5a21b665
DV
11351 /* Contrary to the suggestions in the documentation,
11352 * "Enable Panel Fitter" does not seem to be required when page
11353 * flipping with a non-native mode, and worse causes a normal
11354 * modeset to fail.
11355 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
11356 */
11357 pf = 0;
11358 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
11359 intel_ring_emit(engine, pf | pipesrc);
7809e5ae 11360
5a21b665 11361 return 0;
7809e5ae
MR
11362}
11363
5a21b665
DV
11364static int intel_gen7_queue_flip(struct drm_device *dev,
11365 struct drm_crtc *crtc,
11366 struct drm_framebuffer *fb,
11367 struct drm_i915_gem_object *obj,
11368 struct drm_i915_gem_request *req,
11369 uint32_t flags)
d21fbe87 11370{
5a21b665
DV
11371 struct intel_engine_cs *engine = req->engine;
11372 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11373 uint32_t plane_bit = 0;
11374 int len, ret;
d21fbe87 11375
5a21b665
DV
11376 switch (intel_crtc->plane) {
11377 case PLANE_A:
11378 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
11379 break;
11380 case PLANE_B:
11381 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
11382 break;
11383 case PLANE_C:
11384 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
11385 break;
11386 default:
11387 WARN_ONCE(1, "unknown plane in flip command\n");
11388 return -ENODEV;
11389 }
11390
11391 len = 4;
11392 if (engine->id == RCS) {
11393 len += 6;
11394 /*
11395 * On Gen 8, SRM is now taking an extra dword to accommodate
11396 * 48bits addresses, and we need a NOOP for the batch size to
11397 * stay even.
11398 */
11399 if (IS_GEN8(dev))
11400 len += 2;
11401 }
11402
11403 /*
11404 * BSpec MI_DISPLAY_FLIP for IVB:
11405 * "The full packet must be contained within the same cache line."
11406 *
11407 * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
11408 * cacheline, if we ever start emitting more commands before
11409 * the MI_DISPLAY_FLIP we may need to first emit everything else,
11410 * then do the cacheline alignment, and finally emit the
11411 * MI_DISPLAY_FLIP.
11412 */
11413 ret = intel_ring_cacheline_align(req);
11414 if (ret)
11415 return ret;
11416
11417 ret = intel_ring_begin(req, len);
11418 if (ret)
11419 return ret;
11420
11421 /* Unmask the flip-done completion message. Note that the bspec says that
11422 * we should do this for both the BCS and RCS, and that we must not unmask
11423 * more than one flip event at any time (or ensure that one flip message
11424 * can be sent by waiting for flip-done prior to queueing new flips).
11425 * Experimentation says that BCS works despite DERRMR masking all
11426 * flip-done completion events and that unmasking all planes at once
11427 * for the RCS also doesn't appear to drop events. Setting the DERRMR
11428 * to zero does lead to lockups within MI_DISPLAY_FLIP.
11429 */
11430 if (engine->id == RCS) {
11431 intel_ring_emit(engine, MI_LOAD_REGISTER_IMM(1));
11432 intel_ring_emit_reg(engine, DERRMR);
11433 intel_ring_emit(engine, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
11434 DERRMR_PIPEB_PRI_FLIP_DONE |
11435 DERRMR_PIPEC_PRI_FLIP_DONE));
11436 if (IS_GEN8(dev))
11437 intel_ring_emit(engine, MI_STORE_REGISTER_MEM_GEN8 |
11438 MI_SRM_LRM_GLOBAL_GTT);
11439 else
11440 intel_ring_emit(engine, MI_STORE_REGISTER_MEM |
11441 MI_SRM_LRM_GLOBAL_GTT);
11442 intel_ring_emit_reg(engine, DERRMR);
11443 intel_ring_emit(engine, engine->scratch.gtt_offset + 256);
11444 if (IS_GEN8(dev)) {
11445 intel_ring_emit(engine, 0);
11446 intel_ring_emit(engine, MI_NOOP);
11447 }
11448 }
11449
11450 intel_ring_emit(engine, MI_DISPLAY_FLIP_I915 | plane_bit);
11451 intel_ring_emit(engine, (fb->pitches[0] | obj->tiling_mode));
11452 intel_ring_emit(engine, intel_crtc->flip_work->gtt_offset);
11453 intel_ring_emit(engine, (MI_NOOP));
11454
11455 return 0;
11456}
11457
11458static bool use_mmio_flip(struct intel_engine_cs *engine,
11459 struct drm_i915_gem_object *obj)
11460{
c37efb99
CW
11461 struct reservation_object *resv;
11462
5a21b665
DV
11463 /*
11464 * This is not being used for older platforms, because
11465 * non-availability of flip done interrupt forces us to use
11466 * CS flips. Older platforms derive flip done using some clever
11467 * tricks involving the flip_pending status bits and vblank irqs.
11468 * So using MMIO flips there would disrupt this mechanism.
11469 */
11470
11471 if (engine == NULL)
11472 return true;
11473
11474 if (INTEL_GEN(engine->i915) < 5)
11475 return false;
11476
11477 if (i915.use_mmio_flip < 0)
11478 return false;
11479 else if (i915.use_mmio_flip > 0)
11480 return true;
11481 else if (i915.enable_execlists)
11482 return true;
c37efb99
CW
11483
11484 resv = i915_gem_object_get_dmabuf_resv(obj);
11485 if (resv && !reservation_object_test_signaled_rcu(resv, false))
5a21b665 11486 return true;
c37efb99
CW
11487
11488 return engine != i915_gem_request_get_engine(obj->last_write_req);
5a21b665
DV
11489}
11490
11491static void skl_do_mmio_flip(struct intel_crtc *intel_crtc,
11492 unsigned int rotation,
11493 struct intel_flip_work *work)
11494{
11495 struct drm_device *dev = intel_crtc->base.dev;
11496 struct drm_i915_private *dev_priv = dev->dev_private;
11497 struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
11498 const enum pipe pipe = intel_crtc->pipe;
11499 u32 ctl, stride, tile_height;
11500
11501 ctl = I915_READ(PLANE_CTL(pipe, 0));
11502 ctl &= ~PLANE_CTL_TILED_MASK;
11503 switch (fb->modifier[0]) {
11504 case DRM_FORMAT_MOD_NONE:
11505 break;
11506 case I915_FORMAT_MOD_X_TILED:
11507 ctl |= PLANE_CTL_TILED_X;
11508 break;
11509 case I915_FORMAT_MOD_Y_TILED:
11510 ctl |= PLANE_CTL_TILED_Y;
11511 break;
11512 case I915_FORMAT_MOD_Yf_TILED:
11513 ctl |= PLANE_CTL_TILED_YF;
11514 break;
11515 default:
11516 MISSING_CASE(fb->modifier[0]);
11517 }
11518
11519 /*
11520 * The stride is either expressed as a multiple of 64 bytes chunks for
11521 * linear buffers or in number of tiles for tiled buffers.
11522 */
11523 if (intel_rotation_90_or_270(rotation)) {
11524 /* stride = Surface height in tiles */
11525 tile_height = intel_tile_height(dev_priv, fb->modifier[0], 0);
11526 stride = DIV_ROUND_UP(fb->height, tile_height);
11527 } else {
11528 stride = fb->pitches[0] /
11529 intel_fb_stride_alignment(dev_priv, fb->modifier[0],
11530 fb->pixel_format);
11531 }
11532
11533 /*
11534 * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
11535 * PLANE_SURF updates, the update is then guaranteed to be atomic.
11536 */
11537 I915_WRITE(PLANE_CTL(pipe, 0), ctl);
11538 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
11539
11540 I915_WRITE(PLANE_SURF(pipe, 0), work->gtt_offset);
11541 POSTING_READ(PLANE_SURF(pipe, 0));
11542}
11543
11544static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc,
11545 struct intel_flip_work *work)
11546{
11547 struct drm_device *dev = intel_crtc->base.dev;
11548 struct drm_i915_private *dev_priv = dev->dev_private;
11549 struct intel_framebuffer *intel_fb =
11550 to_intel_framebuffer(intel_crtc->base.primary->fb);
11551 struct drm_i915_gem_object *obj = intel_fb->obj;
11552 i915_reg_t reg = DSPCNTR(intel_crtc->plane);
11553 u32 dspcntr;
11554
11555 dspcntr = I915_READ(reg);
11556
11557 if (obj->tiling_mode != I915_TILING_NONE)
11558 dspcntr |= DISPPLANE_TILED;
11559 else
11560 dspcntr &= ~DISPPLANE_TILED;
11561
11562 I915_WRITE(reg, dspcntr);
11563
11564 I915_WRITE(DSPSURF(intel_crtc->plane), work->gtt_offset);
11565 POSTING_READ(DSPSURF(intel_crtc->plane));
11566}
11567
11568static void intel_mmio_flip_work_func(struct work_struct *w)
11569{
11570 struct intel_flip_work *work =
11571 container_of(w, struct intel_flip_work, mmio_work);
11572 struct intel_crtc *crtc = to_intel_crtc(work->crtc);
11573 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
11574 struct intel_framebuffer *intel_fb =
11575 to_intel_framebuffer(crtc->base.primary->fb);
11576 struct drm_i915_gem_object *obj = intel_fb->obj;
c37efb99 11577 struct reservation_object *resv;
5a21b665
DV
11578
11579 if (work->flip_queued_req)
11580 WARN_ON(__i915_wait_request(work->flip_queued_req,
11581 false, NULL,
11582 &dev_priv->rps.mmioflips));
11583
11584 /* For framebuffer backed by dmabuf, wait for fence */
c37efb99
CW
11585 resv = i915_gem_object_get_dmabuf_resv(obj);
11586 if (resv)
11587 WARN_ON(reservation_object_wait_timeout_rcu(resv, false, false,
5a21b665
DV
11588 MAX_SCHEDULE_TIMEOUT) < 0);
11589
11590 intel_pipe_update_start(crtc);
11591
11592 if (INTEL_GEN(dev_priv) >= 9)
11593 skl_do_mmio_flip(crtc, work->rotation, work);
11594 else
11595 /* use_mmio_flip() retricts MMIO flips to ilk+ */
11596 ilk_do_mmio_flip(crtc, work);
11597
11598 intel_pipe_update_end(crtc, work);
11599}
11600
11601static int intel_default_queue_flip(struct drm_device *dev,
11602 struct drm_crtc *crtc,
11603 struct drm_framebuffer *fb,
11604 struct drm_i915_gem_object *obj,
11605 struct drm_i915_gem_request *req,
11606 uint32_t flags)
11607{
11608 return -ENODEV;
11609}
11610
11611static bool __pageflip_stall_check_cs(struct drm_i915_private *dev_priv,
11612 struct intel_crtc *intel_crtc,
11613 struct intel_flip_work *work)
11614{
11615 u32 addr, vblank;
11616
11617 if (!atomic_read(&work->pending))
11618 return false;
11619
11620 smp_rmb();
11621
11622 vblank = intel_crtc_get_vblank_counter(intel_crtc);
11623 if (work->flip_ready_vblank == 0) {
11624 if (work->flip_queued_req &&
11625 !i915_gem_request_completed(work->flip_queued_req, true))
11626 return false;
11627
11628 work->flip_ready_vblank = vblank;
11629 }
11630
11631 if (vblank - work->flip_ready_vblank < 3)
11632 return false;
11633
11634 /* Potential stall - if we see that the flip has happened,
11635 * assume a missed interrupt. */
11636 if (INTEL_GEN(dev_priv) >= 4)
11637 addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
11638 else
11639 addr = I915_READ(DSPADDR(intel_crtc->plane));
11640
11641 /* There is a potential issue here with a false positive after a flip
11642 * to the same address. We could address this by checking for a
11643 * non-incrementing frame counter.
11644 */
11645 return addr == work->gtt_offset;
11646}
11647
11648void intel_check_page_flip(struct drm_i915_private *dev_priv, int pipe)
11649{
11650 struct drm_device *dev = dev_priv->dev;
11651 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
11652 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11653 struct intel_flip_work *work;
11654
11655 WARN_ON(!in_interrupt());
11656
11657 if (crtc == NULL)
11658 return;
11659
11660 spin_lock(&dev->event_lock);
11661 work = intel_crtc->flip_work;
11662
11663 if (work != NULL && !is_mmio_work(work) &&
11664 __pageflip_stall_check_cs(dev_priv, intel_crtc, work)) {
11665 WARN_ONCE(1,
11666 "Kicking stuck page flip: queued at %d, now %d\n",
11667 work->flip_queued_vblank, intel_crtc_get_vblank_counter(intel_crtc));
11668 page_flip_completed(intel_crtc);
11669 work = NULL;
11670 }
11671
11672 if (work != NULL && !is_mmio_work(work) &&
11673 intel_crtc_get_vblank_counter(intel_crtc) - work->flip_queued_vblank > 1)
11674 intel_queue_rps_boost_for_request(work->flip_queued_req);
11675 spin_unlock(&dev->event_lock);
11676}
11677
11678static int intel_crtc_page_flip(struct drm_crtc *crtc,
11679 struct drm_framebuffer *fb,
11680 struct drm_pending_vblank_event *event,
11681 uint32_t page_flip_flags)
11682{
11683 struct drm_device *dev = crtc->dev;
11684 struct drm_i915_private *dev_priv = dev->dev_private;
11685 struct drm_framebuffer *old_fb = crtc->primary->fb;
11686 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
11687 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11688 struct drm_plane *primary = crtc->primary;
11689 enum pipe pipe = intel_crtc->pipe;
11690 struct intel_flip_work *work;
11691 struct intel_engine_cs *engine;
11692 bool mmio_flip;
11693 struct drm_i915_gem_request *request = NULL;
11694 int ret;
11695
11696 /*
11697 * drm_mode_page_flip_ioctl() should already catch this, but double
11698 * check to be safe. In the future we may enable pageflipping from
11699 * a disabled primary plane.
11700 */
11701 if (WARN_ON(intel_fb_obj(old_fb) == NULL))
11702 return -EBUSY;
11703
11704 /* Can't change pixel format via MI display flips. */
11705 if (fb->pixel_format != crtc->primary->fb->pixel_format)
11706 return -EINVAL;
11707
11708 /*
11709 * TILEOFF/LINOFF registers can't be changed via MI display flips.
11710 * Note that pitch changes could also affect these register.
11711 */
11712 if (INTEL_INFO(dev)->gen > 3 &&
11713 (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
11714 fb->pitches[0] != crtc->primary->fb->pitches[0]))
11715 return -EINVAL;
11716
11717 if (i915_terminally_wedged(&dev_priv->gpu_error))
11718 goto out_hang;
11719
11720 work = kzalloc(sizeof(*work), GFP_KERNEL);
11721 if (work == NULL)
11722 return -ENOMEM;
11723
11724 work->event = event;
11725 work->crtc = crtc;
11726 work->old_fb = old_fb;
11727 INIT_WORK(&work->unpin_work, intel_unpin_work_fn);
11728
11729 ret = drm_crtc_vblank_get(crtc);
11730 if (ret)
11731 goto free_work;
11732
11733 /* We borrow the event spin lock for protecting flip_work */
11734 spin_lock_irq(&dev->event_lock);
11735 if (intel_crtc->flip_work) {
11736 /* Before declaring the flip queue wedged, check if
11737 * the hardware completed the operation behind our backs.
11738 */
11739 if (pageflip_finished(intel_crtc, intel_crtc->flip_work)) {
11740 DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
11741 page_flip_completed(intel_crtc);
11742 } else {
11743 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
11744 spin_unlock_irq(&dev->event_lock);
11745
11746 drm_crtc_vblank_put(crtc);
11747 kfree(work);
11748 return -EBUSY;
11749 }
11750 }
11751 intel_crtc->flip_work = work;
11752 spin_unlock_irq(&dev->event_lock);
11753
11754 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
11755 flush_workqueue(dev_priv->wq);
11756
11757 /* Reference the objects for the scheduled work. */
11758 drm_framebuffer_reference(work->old_fb);
11759 drm_gem_object_reference(&obj->base);
11760
11761 crtc->primary->fb = fb;
11762 update_state_fb(crtc->primary);
faf68d92
ML
11763
11764 intel_fbc_pre_update(intel_crtc, intel_crtc->config,
11765 to_intel_plane_state(primary->state));
5a21b665
DV
11766
11767 work->pending_flip_obj = obj;
11768
11769 ret = i915_mutex_lock_interruptible(dev);
11770 if (ret)
11771 goto cleanup;
11772
11773 intel_crtc->reset_counter = i915_reset_counter(&dev_priv->gpu_error);
11774 if (__i915_reset_in_progress_or_wedged(intel_crtc->reset_counter)) {
11775 ret = -EIO;
11776 goto cleanup;
11777 }
11778
11779 atomic_inc(&intel_crtc->unpin_work_count);
11780
11781 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
11782 work->flip_count = I915_READ(PIPE_FLIPCOUNT_G4X(pipe)) + 1;
11783
11784 if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
11785 engine = &dev_priv->engine[BCS];
11786 if (obj->tiling_mode != intel_fb_obj(work->old_fb)->tiling_mode)
11787 /* vlv: DISPLAY_FLIP fails to change tiling */
11788 engine = NULL;
11789 } else if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
11790 engine = &dev_priv->engine[BCS];
11791 } else if (INTEL_INFO(dev)->gen >= 7) {
11792 engine = i915_gem_request_get_engine(obj->last_write_req);
11793 if (engine == NULL || engine->id != RCS)
11794 engine = &dev_priv->engine[BCS];
11795 } else {
11796 engine = &dev_priv->engine[RCS];
11797 }
11798
11799 mmio_flip = use_mmio_flip(engine, obj);
11800
11801 /* When using CS flips, we want to emit semaphores between rings.
11802 * However, when using mmio flips we will create a task to do the
11803 * synchronisation, so all we want here is to pin the framebuffer
11804 * into the display plane and skip any waits.
11805 */
11806 if (!mmio_flip) {
11807 ret = i915_gem_object_sync(obj, engine, &request);
11808 if (!ret && !request) {
11809 request = i915_gem_request_alloc(engine, NULL);
11810 ret = PTR_ERR_OR_ZERO(request);
11811 }
11812
11813 if (ret)
11814 goto cleanup_pending;
11815 }
11816
11817 ret = intel_pin_and_fence_fb_obj(fb, primary->state->rotation);
11818 if (ret)
11819 goto cleanup_pending;
11820
11821 work->gtt_offset = intel_plane_obj_offset(to_intel_plane(primary),
11822 obj, 0);
11823 work->gtt_offset += intel_crtc->dspaddr_offset;
11824 work->rotation = crtc->primary->state->rotation;
11825
11826 if (mmio_flip) {
11827 INIT_WORK(&work->mmio_work, intel_mmio_flip_work_func);
11828
11829 i915_gem_request_assign(&work->flip_queued_req,
11830 obj->last_write_req);
11831
11832 schedule_work(&work->mmio_work);
11833 } else {
11834 i915_gem_request_assign(&work->flip_queued_req, request);
11835 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, request,
11836 page_flip_flags);
11837 if (ret)
11838 goto cleanup_unpin;
11839
11840 intel_mark_page_flip_active(intel_crtc, work);
11841
11842 i915_add_request_no_flush(request);
11843 }
11844
11845 i915_gem_track_fb(intel_fb_obj(old_fb), obj,
11846 to_intel_plane(primary)->frontbuffer_bit);
11847 mutex_unlock(&dev->struct_mutex);
11848
11849 intel_frontbuffer_flip_prepare(dev,
11850 to_intel_plane(primary)->frontbuffer_bit);
11851
11852 trace_i915_flip_request(intel_crtc->plane, obj);
11853
11854 return 0;
11855
11856cleanup_unpin:
11857 intel_unpin_fb_obj(fb, crtc->primary->state->rotation);
11858cleanup_pending:
11859 if (!IS_ERR_OR_NULL(request))
11860 i915_add_request_no_flush(request);
11861 atomic_dec(&intel_crtc->unpin_work_count);
11862 mutex_unlock(&dev->struct_mutex);
11863cleanup:
11864 crtc->primary->fb = old_fb;
11865 update_state_fb(crtc->primary);
11866
11867 drm_gem_object_unreference_unlocked(&obj->base);
11868 drm_framebuffer_unreference(work->old_fb);
11869
11870 spin_lock_irq(&dev->event_lock);
11871 intel_crtc->flip_work = NULL;
11872 spin_unlock_irq(&dev->event_lock);
11873
11874 drm_crtc_vblank_put(crtc);
11875free_work:
11876 kfree(work);
11877
11878 if (ret == -EIO) {
11879 struct drm_atomic_state *state;
11880 struct drm_plane_state *plane_state;
11881
11882out_hang:
11883 state = drm_atomic_state_alloc(dev);
11884 if (!state)
11885 return -ENOMEM;
11886 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
11887
11888retry:
11889 plane_state = drm_atomic_get_plane_state(state, primary);
11890 ret = PTR_ERR_OR_ZERO(plane_state);
11891 if (!ret) {
11892 drm_atomic_set_fb_for_plane(plane_state, fb);
11893
11894 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
11895 if (!ret)
11896 ret = drm_atomic_commit(state);
11897 }
11898
11899 if (ret == -EDEADLK) {
11900 drm_modeset_backoff(state->acquire_ctx);
11901 drm_atomic_state_clear(state);
11902 goto retry;
11903 }
11904
11905 if (ret)
11906 drm_atomic_state_free(state);
11907
11908 if (ret == 0 && event) {
11909 spin_lock_irq(&dev->event_lock);
11910 drm_crtc_send_vblank_event(crtc, event);
11911 spin_unlock_irq(&dev->event_lock);
11912 }
11913 }
11914 return ret;
11915}
11916
11917
11918/**
11919 * intel_wm_need_update - Check whether watermarks need updating
11920 * @plane: drm plane
11921 * @state: new plane state
11922 *
11923 * Check current plane state versus the new one to determine whether
11924 * watermarks need to be recalculated.
11925 *
11926 * Returns true or false.
11927 */
11928static bool intel_wm_need_update(struct drm_plane *plane,
11929 struct drm_plane_state *state)
11930{
11931 struct intel_plane_state *new = to_intel_plane_state(state);
11932 struct intel_plane_state *cur = to_intel_plane_state(plane->state);
11933
11934 /* Update watermarks on tiling or size changes. */
11935 if (new->visible != cur->visible)
11936 return true;
11937
11938 if (!cur->base.fb || !new->base.fb)
11939 return false;
11940
11941 if (cur->base.fb->modifier[0] != new->base.fb->modifier[0] ||
11942 cur->base.rotation != new->base.rotation ||
11943 drm_rect_width(&new->src) != drm_rect_width(&cur->src) ||
11944 drm_rect_height(&new->src) != drm_rect_height(&cur->src) ||
11945 drm_rect_width(&new->dst) != drm_rect_width(&cur->dst) ||
11946 drm_rect_height(&new->dst) != drm_rect_height(&cur->dst))
11947 return true;
11948
11949 return false;
11950}
11951
11952static bool needs_scaling(struct intel_plane_state *state)
11953{
11954 int src_w = drm_rect_width(&state->src) >> 16;
11955 int src_h = drm_rect_height(&state->src) >> 16;
11956 int dst_w = drm_rect_width(&state->dst);
11957 int dst_h = drm_rect_height(&state->dst);
11958
11959 return (src_w != dst_w || src_h != dst_h);
11960}
d21fbe87 11961
da20eabd
ML
11962int intel_plane_atomic_calc_changes(struct drm_crtc_state *crtc_state,
11963 struct drm_plane_state *plane_state)
11964{
ab1d3a0e 11965 struct intel_crtc_state *pipe_config = to_intel_crtc_state(crtc_state);
da20eabd
ML
11966 struct drm_crtc *crtc = crtc_state->crtc;
11967 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11968 struct drm_plane *plane = plane_state->plane;
11969 struct drm_device *dev = crtc->dev;
ed4a6a7c 11970 struct drm_i915_private *dev_priv = to_i915(dev);
da20eabd
ML
11971 struct intel_plane_state *old_plane_state =
11972 to_intel_plane_state(plane->state);
da20eabd
ML
11973 bool mode_changed = needs_modeset(crtc_state);
11974 bool was_crtc_enabled = crtc->state->active;
11975 bool is_crtc_enabled = crtc_state->active;
da20eabd
ML
11976 bool turn_off, turn_on, visible, was_visible;
11977 struct drm_framebuffer *fb = plane_state->fb;
78108b7c 11978 int ret;
da20eabd
ML
11979
11980 if (crtc_state && INTEL_INFO(dev)->gen >= 9 &&
11981 plane->type != DRM_PLANE_TYPE_CURSOR) {
11982 ret = skl_update_scaler_plane(
11983 to_intel_crtc_state(crtc_state),
11984 to_intel_plane_state(plane_state));
11985 if (ret)
11986 return ret;
11987 }
11988
da20eabd
ML
11989 was_visible = old_plane_state->visible;
11990 visible = to_intel_plane_state(plane_state)->visible;
11991
11992 if (!was_crtc_enabled && WARN_ON(was_visible))
11993 was_visible = false;
11994
35c08f43
ML
11995 /*
11996 * Visibility is calculated as if the crtc was on, but
11997 * after scaler setup everything depends on it being off
11998 * when the crtc isn't active.
f818ffea
VS
11999 *
12000 * FIXME this is wrong for watermarks. Watermarks should also
12001 * be computed as if the pipe would be active. Perhaps move
12002 * per-plane wm computation to the .check_plane() hook, and
12003 * only combine the results from all planes in the current place?
35c08f43
ML
12004 */
12005 if (!is_crtc_enabled)
12006 to_intel_plane_state(plane_state)->visible = visible = false;
da20eabd
ML
12007
12008 if (!was_visible && !visible)
12009 return 0;
12010
e8861675
ML
12011 if (fb != old_plane_state->base.fb)
12012 pipe_config->fb_changed = true;
12013
da20eabd
ML
12014 turn_off = was_visible && (!visible || mode_changed);
12015 turn_on = visible && (!was_visible || mode_changed);
12016
72660ce0 12017 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] has [PLANE:%d:%s] with fb %i\n",
78108b7c
VS
12018 intel_crtc->base.base.id,
12019 intel_crtc->base.name,
72660ce0
VS
12020 plane->base.id, plane->name,
12021 fb ? fb->base.id : -1);
da20eabd 12022
72660ce0
VS
12023 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] visible %i -> %i, off %i, on %i, ms %i\n",
12024 plane->base.id, plane->name,
12025 was_visible, visible,
da20eabd
ML
12026 turn_off, turn_on, mode_changed);
12027
caed361d
VS
12028 if (turn_on) {
12029 pipe_config->update_wm_pre = true;
12030
12031 /* must disable cxsr around plane enable/disable */
12032 if (plane->type != DRM_PLANE_TYPE_CURSOR)
12033 pipe_config->disable_cxsr = true;
12034 } else if (turn_off) {
12035 pipe_config->update_wm_post = true;
92826fcd 12036
852eb00d 12037 /* must disable cxsr around plane enable/disable */
e8861675 12038 if (plane->type != DRM_PLANE_TYPE_CURSOR)
ab1d3a0e 12039 pipe_config->disable_cxsr = true;
852eb00d 12040 } else if (intel_wm_need_update(plane, plane_state)) {
caed361d
VS
12041 /* FIXME bollocks */
12042 pipe_config->update_wm_pre = true;
12043 pipe_config->update_wm_post = true;
852eb00d 12044 }
da20eabd 12045
ed4a6a7c 12046 /* Pre-gen9 platforms need two-step watermark updates */
caed361d
VS
12047 if ((pipe_config->update_wm_pre || pipe_config->update_wm_post) &&
12048 INTEL_INFO(dev)->gen < 9 && dev_priv->display.optimize_watermarks)
ed4a6a7c
MR
12049 to_intel_crtc_state(crtc_state)->wm.need_postvbl_update = true;
12050
8be6ca85 12051 if (visible || was_visible)
cd202f69 12052 pipe_config->fb_bits |= to_intel_plane(plane)->frontbuffer_bit;
a9ff8714 12053
31ae71fc
ML
12054 /*
12055 * WaCxSRDisabledForSpriteScaling:ivb
12056 *
12057 * cstate->update_wm was already set above, so this flag will
12058 * take effect when we commit and program watermarks.
12059 */
12060 if (plane->type == DRM_PLANE_TYPE_OVERLAY && IS_IVYBRIDGE(dev) &&
12061 needs_scaling(to_intel_plane_state(plane_state)) &&
12062 !needs_scaling(old_plane_state))
12063 pipe_config->disable_lp_wm = true;
d21fbe87 12064
da20eabd
ML
12065 return 0;
12066}
12067
6d3a1ce7
ML
12068static bool encoders_cloneable(const struct intel_encoder *a,
12069 const struct intel_encoder *b)
12070{
12071 /* masks could be asymmetric, so check both ways */
12072 return a == b || (a->cloneable & (1 << b->type) &&
12073 b->cloneable & (1 << a->type));
12074}
12075
12076static bool check_single_encoder_cloning(struct drm_atomic_state *state,
12077 struct intel_crtc *crtc,
12078 struct intel_encoder *encoder)
12079{
12080 struct intel_encoder *source_encoder;
12081 struct drm_connector *connector;
12082 struct drm_connector_state *connector_state;
12083 int i;
12084
12085 for_each_connector_in_state(state, connector, connector_state, i) {
12086 if (connector_state->crtc != &crtc->base)
12087 continue;
12088
12089 source_encoder =
12090 to_intel_encoder(connector_state->best_encoder);
12091 if (!encoders_cloneable(encoder, source_encoder))
12092 return false;
12093 }
12094
12095 return true;
12096}
12097
12098static bool check_encoder_cloning(struct drm_atomic_state *state,
12099 struct intel_crtc *crtc)
12100{
12101 struct intel_encoder *encoder;
12102 struct drm_connector *connector;
12103 struct drm_connector_state *connector_state;
12104 int i;
12105
12106 for_each_connector_in_state(state, connector, connector_state, i) {
12107 if (connector_state->crtc != &crtc->base)
12108 continue;
12109
12110 encoder = to_intel_encoder(connector_state->best_encoder);
12111 if (!check_single_encoder_cloning(state, crtc, encoder))
12112 return false;
12113 }
12114
12115 return true;
12116}
12117
12118static int intel_crtc_atomic_check(struct drm_crtc *crtc,
12119 struct drm_crtc_state *crtc_state)
12120{
cf5a15be 12121 struct drm_device *dev = crtc->dev;
ad421372 12122 struct drm_i915_private *dev_priv = dev->dev_private;
6d3a1ce7 12123 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
cf5a15be
ML
12124 struct intel_crtc_state *pipe_config =
12125 to_intel_crtc_state(crtc_state);
6d3a1ce7 12126 struct drm_atomic_state *state = crtc_state->state;
4d20cd86 12127 int ret;
6d3a1ce7
ML
12128 bool mode_changed = needs_modeset(crtc_state);
12129
12130 if (mode_changed && !check_encoder_cloning(state, intel_crtc)) {
12131 DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
12132 return -EINVAL;
12133 }
12134
852eb00d 12135 if (mode_changed && !crtc_state->active)
caed361d 12136 pipe_config->update_wm_post = true;
eddfcbcd 12137
ad421372
ML
12138 if (mode_changed && crtc_state->enable &&
12139 dev_priv->display.crtc_compute_clock &&
8106ddbd 12140 !WARN_ON(pipe_config->shared_dpll)) {
ad421372
ML
12141 ret = dev_priv->display.crtc_compute_clock(intel_crtc,
12142 pipe_config);
12143 if (ret)
12144 return ret;
12145 }
12146
82cf435b
LL
12147 if (crtc_state->color_mgmt_changed) {
12148 ret = intel_color_check(crtc, crtc_state);
12149 if (ret)
12150 return ret;
12151 }
12152
e435d6e5 12153 ret = 0;
86c8bbbe 12154 if (dev_priv->display.compute_pipe_wm) {
e3bddded 12155 ret = dev_priv->display.compute_pipe_wm(pipe_config);
ed4a6a7c
MR
12156 if (ret) {
12157 DRM_DEBUG_KMS("Target pipe watermarks are invalid\n");
12158 return ret;
12159 }
12160 }
12161
12162 if (dev_priv->display.compute_intermediate_wm &&
12163 !to_intel_atomic_state(state)->skip_intermediate_wm) {
12164 if (WARN_ON(!dev_priv->display.compute_pipe_wm))
12165 return 0;
12166
12167 /*
12168 * Calculate 'intermediate' watermarks that satisfy both the
12169 * old state and the new state. We can program these
12170 * immediately.
12171 */
12172 ret = dev_priv->display.compute_intermediate_wm(crtc->dev,
12173 intel_crtc,
12174 pipe_config);
12175 if (ret) {
12176 DRM_DEBUG_KMS("No valid intermediate pipe watermarks are possible\n");
86c8bbbe 12177 return ret;
ed4a6a7c 12178 }
e3d5457c
VS
12179 } else if (dev_priv->display.compute_intermediate_wm) {
12180 if (HAS_PCH_SPLIT(dev_priv) && INTEL_GEN(dev_priv) < 9)
12181 pipe_config->wm.ilk.intermediate = pipe_config->wm.ilk.optimal;
86c8bbbe
MR
12182 }
12183
e435d6e5
ML
12184 if (INTEL_INFO(dev)->gen >= 9) {
12185 if (mode_changed)
12186 ret = skl_update_scaler_crtc(pipe_config);
12187
12188 if (!ret)
12189 ret = intel_atomic_setup_scalers(dev, intel_crtc,
12190 pipe_config);
12191 }
12192
12193 return ret;
6d3a1ce7
ML
12194}
12195
65b38e0d 12196static const struct drm_crtc_helper_funcs intel_helper_funcs = {
f6e5b160 12197 .mode_set_base_atomic = intel_pipe_set_base_atomic,
5a21b665
DV
12198 .atomic_begin = intel_begin_crtc_commit,
12199 .atomic_flush = intel_finish_crtc_commit,
6d3a1ce7 12200 .atomic_check = intel_crtc_atomic_check,
f6e5b160
CW
12201};
12202
d29b2f9d
ACO
12203static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
12204{
12205 struct intel_connector *connector;
12206
12207 for_each_intel_connector(dev, connector) {
8863dc7f
DV
12208 if (connector->base.state->crtc)
12209 drm_connector_unreference(&connector->base);
12210
d29b2f9d
ACO
12211 if (connector->base.encoder) {
12212 connector->base.state->best_encoder =
12213 connector->base.encoder;
12214 connector->base.state->crtc =
12215 connector->base.encoder->crtc;
8863dc7f
DV
12216
12217 drm_connector_reference(&connector->base);
d29b2f9d
ACO
12218 } else {
12219 connector->base.state->best_encoder = NULL;
12220 connector->base.state->crtc = NULL;
12221 }
12222 }
12223}
12224
050f7aeb 12225static void
eba905b2 12226connected_sink_compute_bpp(struct intel_connector *connector,
5cec258b 12227 struct intel_crtc_state *pipe_config)
050f7aeb
DV
12228{
12229 int bpp = pipe_config->pipe_bpp;
12230
12231 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
12232 connector->base.base.id,
c23cc417 12233 connector->base.name);
050f7aeb
DV
12234
12235 /* Don't use an invalid EDID bpc value */
12236 if (connector->base.display_info.bpc &&
12237 connector->base.display_info.bpc * 3 < bpp) {
12238 DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
12239 bpp, connector->base.display_info.bpc*3);
12240 pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
12241 }
12242
013dd9e0
JN
12243 /* Clamp bpp to default limit on screens without EDID 1.4 */
12244 if (connector->base.display_info.bpc == 0) {
12245 int type = connector->base.connector_type;
12246 int clamp_bpp = 24;
12247
12248 /* Fall back to 18 bpp when DP sink capability is unknown. */
12249 if (type == DRM_MODE_CONNECTOR_DisplayPort ||
12250 type == DRM_MODE_CONNECTOR_eDP)
12251 clamp_bpp = 18;
12252
12253 if (bpp > clamp_bpp) {
12254 DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of %d\n",
12255 bpp, clamp_bpp);
12256 pipe_config->pipe_bpp = clamp_bpp;
12257 }
050f7aeb
DV
12258 }
12259}
12260
4e53c2e0 12261static int
050f7aeb 12262compute_baseline_pipe_bpp(struct intel_crtc *crtc,
5cec258b 12263 struct intel_crtc_state *pipe_config)
4e53c2e0 12264{
050f7aeb 12265 struct drm_device *dev = crtc->base.dev;
1486017f 12266 struct drm_atomic_state *state;
da3ced29
ACO
12267 struct drm_connector *connector;
12268 struct drm_connector_state *connector_state;
1486017f 12269 int bpp, i;
4e53c2e0 12270
666a4537 12271 if ((IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)))
4e53c2e0 12272 bpp = 10*3;
d328c9d7
DV
12273 else if (INTEL_INFO(dev)->gen >= 5)
12274 bpp = 12*3;
12275 else
12276 bpp = 8*3;
12277
4e53c2e0 12278
4e53c2e0
DV
12279 pipe_config->pipe_bpp = bpp;
12280
1486017f
ACO
12281 state = pipe_config->base.state;
12282
4e53c2e0 12283 /* Clamp display bpp to EDID value */
da3ced29
ACO
12284 for_each_connector_in_state(state, connector, connector_state, i) {
12285 if (connector_state->crtc != &crtc->base)
4e53c2e0
DV
12286 continue;
12287
da3ced29
ACO
12288 connected_sink_compute_bpp(to_intel_connector(connector),
12289 pipe_config);
4e53c2e0
DV
12290 }
12291
12292 return bpp;
12293}
12294
644db711
DV
12295static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
12296{
12297 DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
12298 "type: 0x%x flags: 0x%x\n",
1342830c 12299 mode->crtc_clock,
644db711
DV
12300 mode->crtc_hdisplay, mode->crtc_hsync_start,
12301 mode->crtc_hsync_end, mode->crtc_htotal,
12302 mode->crtc_vdisplay, mode->crtc_vsync_start,
12303 mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
12304}
12305
c0b03411 12306static void intel_dump_pipe_config(struct intel_crtc *crtc,
5cec258b 12307 struct intel_crtc_state *pipe_config,
c0b03411
DV
12308 const char *context)
12309{
6a60cd87
CK
12310 struct drm_device *dev = crtc->base.dev;
12311 struct drm_plane *plane;
12312 struct intel_plane *intel_plane;
12313 struct intel_plane_state *state;
12314 struct drm_framebuffer *fb;
12315
78108b7c
VS
12316 DRM_DEBUG_KMS("[CRTC:%d:%s]%s config %p for pipe %c\n",
12317 crtc->base.base.id, crtc->base.name,
6a60cd87 12318 context, pipe_config, pipe_name(crtc->pipe));
c0b03411 12319
da205630 12320 DRM_DEBUG_KMS("cpu_transcoder: %s\n", transcoder_name(pipe_config->cpu_transcoder));
c0b03411
DV
12321 DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
12322 pipe_config->pipe_bpp, pipe_config->dither);
12323 DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
12324 pipe_config->has_pch_encoder,
12325 pipe_config->fdi_lanes,
12326 pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
12327 pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
12328 pipe_config->fdi_m_n.tu);
90a6b7b0 12329 DRM_DEBUG_KMS("dp: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
eb14cb74 12330 pipe_config->has_dp_encoder,
90a6b7b0 12331 pipe_config->lane_count,
eb14cb74
VS
12332 pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
12333 pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
12334 pipe_config->dp_m_n.tu);
b95af8be 12335
90a6b7b0 12336 DRM_DEBUG_KMS("dp: %i, lanes: %i, gmch_m2: %u, gmch_n2: %u, link_m2: %u, link_n2: %u, tu2: %u\n",
b95af8be 12337 pipe_config->has_dp_encoder,
90a6b7b0 12338 pipe_config->lane_count,
b95af8be
VK
12339 pipe_config->dp_m2_n2.gmch_m,
12340 pipe_config->dp_m2_n2.gmch_n,
12341 pipe_config->dp_m2_n2.link_m,
12342 pipe_config->dp_m2_n2.link_n,
12343 pipe_config->dp_m2_n2.tu);
12344
55072d19
DV
12345 DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
12346 pipe_config->has_audio,
12347 pipe_config->has_infoframe);
12348
c0b03411 12349 DRM_DEBUG_KMS("requested mode:\n");
2d112de7 12350 drm_mode_debug_printmodeline(&pipe_config->base.mode);
c0b03411 12351 DRM_DEBUG_KMS("adjusted mode:\n");
2d112de7
ACO
12352 drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
12353 intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
d71b8d4a 12354 DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
37327abd
VS
12355 DRM_DEBUG_KMS("pipe src size: %dx%d\n",
12356 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
0ec463d3
TU
12357 DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
12358 crtc->num_scalers,
12359 pipe_config->scaler_state.scaler_users,
12360 pipe_config->scaler_state.scaler_id);
c0b03411
DV
12361 DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
12362 pipe_config->gmch_pfit.control,
12363 pipe_config->gmch_pfit.pgm_ratios,
12364 pipe_config->gmch_pfit.lvds_border_bits);
fd4daa9c 12365 DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
c0b03411 12366 pipe_config->pch_pfit.pos,
fd4daa9c
CW
12367 pipe_config->pch_pfit.size,
12368 pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
42db64ef 12369 DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
cf532bb2 12370 DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
6a60cd87 12371
415ff0f6 12372 if (IS_BROXTON(dev)) {
05712c15 12373 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: ebb0: 0x%x, ebb4: 0x%x,"
415ff0f6 12374 "pll0: 0x%x, pll1: 0x%x, pll2: 0x%x, pll3: 0x%x, "
c8453338 12375 "pll6: 0x%x, pll8: 0x%x, pll9: 0x%x, pll10: 0x%x, pcsdw12: 0x%x\n",
415ff0f6
TU
12376 pipe_config->ddi_pll_sel,
12377 pipe_config->dpll_hw_state.ebb0,
05712c15 12378 pipe_config->dpll_hw_state.ebb4,
415ff0f6
TU
12379 pipe_config->dpll_hw_state.pll0,
12380 pipe_config->dpll_hw_state.pll1,
12381 pipe_config->dpll_hw_state.pll2,
12382 pipe_config->dpll_hw_state.pll3,
12383 pipe_config->dpll_hw_state.pll6,
12384 pipe_config->dpll_hw_state.pll8,
05712c15 12385 pipe_config->dpll_hw_state.pll9,
c8453338 12386 pipe_config->dpll_hw_state.pll10,
415ff0f6 12387 pipe_config->dpll_hw_state.pcsdw12);
ef11bdb3 12388 } else if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) {
415ff0f6
TU
12389 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: "
12390 "ctrl1: 0x%x, cfgcr1: 0x%x, cfgcr2: 0x%x\n",
12391 pipe_config->ddi_pll_sel,
12392 pipe_config->dpll_hw_state.ctrl1,
12393 pipe_config->dpll_hw_state.cfgcr1,
12394 pipe_config->dpll_hw_state.cfgcr2);
12395 } else if (HAS_DDI(dev)) {
1260f07e 12396 DRM_DEBUG_KMS("ddi_pll_sel: 0x%x; dpll_hw_state: wrpll: 0x%x spll: 0x%x\n",
415ff0f6 12397 pipe_config->ddi_pll_sel,
00490c22
ML
12398 pipe_config->dpll_hw_state.wrpll,
12399 pipe_config->dpll_hw_state.spll);
415ff0f6
TU
12400 } else {
12401 DRM_DEBUG_KMS("dpll_hw_state: dpll: 0x%x, dpll_md: 0x%x, "
12402 "fp0: 0x%x, fp1: 0x%x\n",
12403 pipe_config->dpll_hw_state.dpll,
12404 pipe_config->dpll_hw_state.dpll_md,
12405 pipe_config->dpll_hw_state.fp0,
12406 pipe_config->dpll_hw_state.fp1);
12407 }
12408
6a60cd87
CK
12409 DRM_DEBUG_KMS("planes on this crtc\n");
12410 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
12411 intel_plane = to_intel_plane(plane);
12412 if (intel_plane->pipe != crtc->pipe)
12413 continue;
12414
12415 state = to_intel_plane_state(plane->state);
12416 fb = state->base.fb;
12417 if (!fb) {
1d577e02
VS
12418 DRM_DEBUG_KMS("[PLANE:%d:%s] disabled, scaler_id = %d\n",
12419 plane->base.id, plane->name, state->scaler_id);
6a60cd87
CK
12420 continue;
12421 }
12422
1d577e02
VS
12423 DRM_DEBUG_KMS("[PLANE:%d:%s] enabled",
12424 plane->base.id, plane->name);
12425 DRM_DEBUG_KMS("\tFB:%d, fb = %ux%u format = %s",
12426 fb->base.id, fb->width, fb->height,
12427 drm_get_format_name(fb->pixel_format));
12428 DRM_DEBUG_KMS("\tscaler:%d src %dx%d+%d+%d dst %dx%d+%d+%d\n",
12429 state->scaler_id,
12430 state->src.x1 >> 16, state->src.y1 >> 16,
12431 drm_rect_width(&state->src) >> 16,
12432 drm_rect_height(&state->src) >> 16,
12433 state->dst.x1, state->dst.y1,
12434 drm_rect_width(&state->dst),
12435 drm_rect_height(&state->dst));
6a60cd87 12436 }
c0b03411
DV
12437}
12438
5448a00d 12439static bool check_digital_port_conflicts(struct drm_atomic_state *state)
00f0b378 12440{
5448a00d 12441 struct drm_device *dev = state->dev;
da3ced29 12442 struct drm_connector *connector;
00f0b378
VS
12443 unsigned int used_ports = 0;
12444
12445 /*
12446 * Walk the connector list instead of the encoder
12447 * list to detect the problem on ddi platforms
12448 * where there's just one encoder per digital port.
12449 */
0bff4858
VS
12450 drm_for_each_connector(connector, dev) {
12451 struct drm_connector_state *connector_state;
12452 struct intel_encoder *encoder;
12453
12454 connector_state = drm_atomic_get_existing_connector_state(state, connector);
12455 if (!connector_state)
12456 connector_state = connector->state;
12457
5448a00d 12458 if (!connector_state->best_encoder)
00f0b378
VS
12459 continue;
12460
5448a00d
ACO
12461 encoder = to_intel_encoder(connector_state->best_encoder);
12462
12463 WARN_ON(!connector_state->crtc);
00f0b378
VS
12464
12465 switch (encoder->type) {
12466 unsigned int port_mask;
12467 case INTEL_OUTPUT_UNKNOWN:
12468 if (WARN_ON(!HAS_DDI(dev)))
12469 break;
12470 case INTEL_OUTPUT_DISPLAYPORT:
12471 case INTEL_OUTPUT_HDMI:
12472 case INTEL_OUTPUT_EDP:
12473 port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
12474
12475 /* the same port mustn't appear more than once */
12476 if (used_ports & port_mask)
12477 return false;
12478
12479 used_ports |= port_mask;
12480 default:
12481 break;
12482 }
12483 }
12484
12485 return true;
12486}
12487
83a57153
ACO
12488static void
12489clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
12490{
12491 struct drm_crtc_state tmp_state;
663a3640 12492 struct intel_crtc_scaler_state scaler_state;
4978cc93 12493 struct intel_dpll_hw_state dpll_hw_state;
8106ddbd 12494 struct intel_shared_dpll *shared_dpll;
8504c74c 12495 uint32_t ddi_pll_sel;
c4e2d043 12496 bool force_thru;
83a57153 12497
7546a384
ACO
12498 /* FIXME: before the switch to atomic started, a new pipe_config was
12499 * kzalloc'd. Code that depends on any field being zero should be
12500 * fixed, so that the crtc_state can be safely duplicated. For now,
12501 * only fields that are know to not cause problems are preserved. */
12502
83a57153 12503 tmp_state = crtc_state->base;
663a3640 12504 scaler_state = crtc_state->scaler_state;
4978cc93
ACO
12505 shared_dpll = crtc_state->shared_dpll;
12506 dpll_hw_state = crtc_state->dpll_hw_state;
8504c74c 12507 ddi_pll_sel = crtc_state->ddi_pll_sel;
c4e2d043 12508 force_thru = crtc_state->pch_pfit.force_thru;
4978cc93 12509
83a57153 12510 memset(crtc_state, 0, sizeof *crtc_state);
4978cc93 12511
83a57153 12512 crtc_state->base = tmp_state;
663a3640 12513 crtc_state->scaler_state = scaler_state;
4978cc93
ACO
12514 crtc_state->shared_dpll = shared_dpll;
12515 crtc_state->dpll_hw_state = dpll_hw_state;
8504c74c 12516 crtc_state->ddi_pll_sel = ddi_pll_sel;
c4e2d043 12517 crtc_state->pch_pfit.force_thru = force_thru;
83a57153
ACO
12518}
12519
548ee15b 12520static int
b8cecdf5 12521intel_modeset_pipe_config(struct drm_crtc *crtc,
b359283a 12522 struct intel_crtc_state *pipe_config)
ee7b9f93 12523{
b359283a 12524 struct drm_atomic_state *state = pipe_config->base.state;
7758a113 12525 struct intel_encoder *encoder;
da3ced29 12526 struct drm_connector *connector;
0b901879 12527 struct drm_connector_state *connector_state;
d328c9d7 12528 int base_bpp, ret = -EINVAL;
0b901879 12529 int i;
e29c22c0 12530 bool retry = true;
ee7b9f93 12531
83a57153 12532 clear_intel_crtc_state(pipe_config);
7758a113 12533
e143a21c
DV
12534 pipe_config->cpu_transcoder =
12535 (enum transcoder) to_intel_crtc(crtc)->pipe;
b8cecdf5 12536
2960bc9c
ID
12537 /*
12538 * Sanitize sync polarity flags based on requested ones. If neither
12539 * positive or negative polarity is requested, treat this as meaning
12540 * negative polarity.
12541 */
2d112de7 12542 if (!(pipe_config->base.adjusted_mode.flags &
2960bc9c 12543 (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
2d112de7 12544 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
2960bc9c 12545
2d112de7 12546 if (!(pipe_config->base.adjusted_mode.flags &
2960bc9c 12547 (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
2d112de7 12548 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
2960bc9c 12549
d328c9d7
DV
12550 base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
12551 pipe_config);
12552 if (base_bpp < 0)
4e53c2e0
DV
12553 goto fail;
12554
e41a56be
VS
12555 /*
12556 * Determine the real pipe dimensions. Note that stereo modes can
12557 * increase the actual pipe size due to the frame doubling and
12558 * insertion of additional space for blanks between the frame. This
12559 * is stored in the crtc timings. We use the requested mode to do this
12560 * computation to clearly distinguish it from the adjusted mode, which
12561 * can be changed by the connectors in the below retry loop.
12562 */
2d112de7 12563 drm_crtc_get_hv_timing(&pipe_config->base.mode,
ecb7e16b
GP
12564 &pipe_config->pipe_src_w,
12565 &pipe_config->pipe_src_h);
e41a56be 12566
e29c22c0 12567encoder_retry:
ef1b460d 12568 /* Ensure the port clock defaults are reset when retrying. */
ff9a6750 12569 pipe_config->port_clock = 0;
ef1b460d 12570 pipe_config->pixel_multiplier = 1;
ff9a6750 12571
135c81b8 12572 /* Fill in default crtc timings, allow encoders to overwrite them. */
2d112de7
ACO
12573 drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
12574 CRTC_STEREO_DOUBLE);
135c81b8 12575
7758a113
DV
12576 /* Pass our mode to the connectors and the CRTC to give them a chance to
12577 * adjust it according to limitations or connector properties, and also
12578 * a chance to reject the mode entirely.
47f1c6c9 12579 */
da3ced29 12580 for_each_connector_in_state(state, connector, connector_state, i) {
0b901879 12581 if (connector_state->crtc != crtc)
7758a113 12582 continue;
7ae89233 12583
0b901879
ACO
12584 encoder = to_intel_encoder(connector_state->best_encoder);
12585
efea6e8e
DV
12586 if (!(encoder->compute_config(encoder, pipe_config))) {
12587 DRM_DEBUG_KMS("Encoder config failure\n");
7758a113
DV
12588 goto fail;
12589 }
ee7b9f93 12590 }
47f1c6c9 12591
ff9a6750
DV
12592 /* Set default port clock if not overwritten by the encoder. Needs to be
12593 * done afterwards in case the encoder adjusts the mode. */
12594 if (!pipe_config->port_clock)
2d112de7 12595 pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
241bfc38 12596 * pipe_config->pixel_multiplier;
ff9a6750 12597
a43f6e0f 12598 ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
e29c22c0 12599 if (ret < 0) {
7758a113
DV
12600 DRM_DEBUG_KMS("CRTC fixup failed\n");
12601 goto fail;
ee7b9f93 12602 }
e29c22c0
DV
12603
12604 if (ret == RETRY) {
12605 if (WARN(!retry, "loop in pipe configuration computation\n")) {
12606 ret = -EINVAL;
12607 goto fail;
12608 }
12609
12610 DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
12611 retry = false;
12612 goto encoder_retry;
12613 }
12614
e8fa4270
DV
12615 /* Dithering seems to not pass-through bits correctly when it should, so
12616 * only enable it on 6bpc panels. */
12617 pipe_config->dither = pipe_config->pipe_bpp == 6*3;
62f0ace5 12618 DRM_DEBUG_KMS("hw max bpp: %i, pipe bpp: %i, dithering: %i\n",
d328c9d7 12619 base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
4e53c2e0 12620
7758a113 12621fail:
548ee15b 12622 return ret;
ee7b9f93 12623}
47f1c6c9 12624
ea9d758d 12625static void
4740b0f2 12626intel_modeset_update_crtc_state(struct drm_atomic_state *state)
ea9d758d 12627{
0a9ab303
ACO
12628 struct drm_crtc *crtc;
12629 struct drm_crtc_state *crtc_state;
8a75d157 12630 int i;
ea9d758d 12631
7668851f 12632 /* Double check state. */
8a75d157 12633 for_each_crtc_in_state(state, crtc, crtc_state, i) {
3cb480bc 12634 to_intel_crtc(crtc)->config = to_intel_crtc_state(crtc->state);
fc467a22
ML
12635
12636 /* Update hwmode for vblank functions */
12637 if (crtc->state->active)
12638 crtc->hwmode = crtc->state->adjusted_mode;
12639 else
12640 crtc->hwmode.crtc_clock = 0;
61067a5e
ML
12641
12642 /*
12643 * Update legacy state to satisfy fbc code. This can
12644 * be removed when fbc uses the atomic state.
12645 */
12646 if (drm_atomic_get_existing_plane_state(state, crtc->primary)) {
12647 struct drm_plane_state *plane_state = crtc->primary->state;
12648
12649 crtc->primary->fb = plane_state->fb;
12650 crtc->x = plane_state->src_x >> 16;
12651 crtc->y = plane_state->src_y >> 16;
12652 }
ea9d758d 12653 }
ea9d758d
DV
12654}
12655
3bd26263 12656static bool intel_fuzzy_clock_check(int clock1, int clock2)
f1f644dc 12657{
3bd26263 12658 int diff;
f1f644dc
JB
12659
12660 if (clock1 == clock2)
12661 return true;
12662
12663 if (!clock1 || !clock2)
12664 return false;
12665
12666 diff = abs(clock1 - clock2);
12667
12668 if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
12669 return true;
12670
12671 return false;
12672}
12673
25c5b266
DV
12674#define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
12675 list_for_each_entry((intel_crtc), \
12676 &(dev)->mode_config.crtc_list, \
12677 base.head) \
95150bdf 12678 for_each_if (mask & (1 <<(intel_crtc)->pipe))
25c5b266 12679
cfb23ed6
ML
12680static bool
12681intel_compare_m_n(unsigned int m, unsigned int n,
12682 unsigned int m2, unsigned int n2,
12683 bool exact)
12684{
12685 if (m == m2 && n == n2)
12686 return true;
12687
12688 if (exact || !m || !n || !m2 || !n2)
12689 return false;
12690
12691 BUILD_BUG_ON(DATA_LINK_M_N_MASK > INT_MAX);
12692
31d10b57
ML
12693 if (n > n2) {
12694 while (n > n2) {
cfb23ed6
ML
12695 m2 <<= 1;
12696 n2 <<= 1;
12697 }
31d10b57
ML
12698 } else if (n < n2) {
12699 while (n < n2) {
cfb23ed6
ML
12700 m <<= 1;
12701 n <<= 1;
12702 }
12703 }
12704
31d10b57
ML
12705 if (n != n2)
12706 return false;
12707
12708 return intel_fuzzy_clock_check(m, m2);
cfb23ed6
ML
12709}
12710
12711static bool
12712intel_compare_link_m_n(const struct intel_link_m_n *m_n,
12713 struct intel_link_m_n *m2_n2,
12714 bool adjust)
12715{
12716 if (m_n->tu == m2_n2->tu &&
12717 intel_compare_m_n(m_n->gmch_m, m_n->gmch_n,
12718 m2_n2->gmch_m, m2_n2->gmch_n, !adjust) &&
12719 intel_compare_m_n(m_n->link_m, m_n->link_n,
12720 m2_n2->link_m, m2_n2->link_n, !adjust)) {
12721 if (adjust)
12722 *m2_n2 = *m_n;
12723
12724 return true;
12725 }
12726
12727 return false;
12728}
12729
0e8ffe1b 12730static bool
2fa2fe9a 12731intel_pipe_config_compare(struct drm_device *dev,
5cec258b 12732 struct intel_crtc_state *current_config,
cfb23ed6
ML
12733 struct intel_crtc_state *pipe_config,
12734 bool adjust)
0e8ffe1b 12735{
cfb23ed6
ML
12736 bool ret = true;
12737
12738#define INTEL_ERR_OR_DBG_KMS(fmt, ...) \
12739 do { \
12740 if (!adjust) \
12741 DRM_ERROR(fmt, ##__VA_ARGS__); \
12742 else \
12743 DRM_DEBUG_KMS(fmt, ##__VA_ARGS__); \
12744 } while (0)
12745
66e985c0
DV
12746#define PIPE_CONF_CHECK_X(name) \
12747 if (current_config->name != pipe_config->name) { \
cfb23ed6 12748 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
66e985c0
DV
12749 "(expected 0x%08x, found 0x%08x)\n", \
12750 current_config->name, \
12751 pipe_config->name); \
cfb23ed6 12752 ret = false; \
66e985c0
DV
12753 }
12754
08a24034
DV
12755#define PIPE_CONF_CHECK_I(name) \
12756 if (current_config->name != pipe_config->name) { \
cfb23ed6 12757 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
08a24034
DV
12758 "(expected %i, found %i)\n", \
12759 current_config->name, \
12760 pipe_config->name); \
cfb23ed6
ML
12761 ret = false; \
12762 }
12763
8106ddbd
ACO
12764#define PIPE_CONF_CHECK_P(name) \
12765 if (current_config->name != pipe_config->name) { \
12766 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
12767 "(expected %p, found %p)\n", \
12768 current_config->name, \
12769 pipe_config->name); \
12770 ret = false; \
12771 }
12772
cfb23ed6
ML
12773#define PIPE_CONF_CHECK_M_N(name) \
12774 if (!intel_compare_link_m_n(&current_config->name, \
12775 &pipe_config->name,\
12776 adjust)) { \
12777 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
12778 "(expected tu %i gmch %i/%i link %i/%i, " \
12779 "found tu %i, gmch %i/%i link %i/%i)\n", \
12780 current_config->name.tu, \
12781 current_config->name.gmch_m, \
12782 current_config->name.gmch_n, \
12783 current_config->name.link_m, \
12784 current_config->name.link_n, \
12785 pipe_config->name.tu, \
12786 pipe_config->name.gmch_m, \
12787 pipe_config->name.gmch_n, \
12788 pipe_config->name.link_m, \
12789 pipe_config->name.link_n); \
12790 ret = false; \
12791 }
12792
55c561a7
DV
12793/* This is required for BDW+ where there is only one set of registers for
12794 * switching between high and low RR.
12795 * This macro can be used whenever a comparison has to be made between one
12796 * hw state and multiple sw state variables.
12797 */
cfb23ed6
ML
12798#define PIPE_CONF_CHECK_M_N_ALT(name, alt_name) \
12799 if (!intel_compare_link_m_n(&current_config->name, \
12800 &pipe_config->name, adjust) && \
12801 !intel_compare_link_m_n(&current_config->alt_name, \
12802 &pipe_config->name, adjust)) { \
12803 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
12804 "(expected tu %i gmch %i/%i link %i/%i, " \
12805 "or tu %i gmch %i/%i link %i/%i, " \
12806 "found tu %i, gmch %i/%i link %i/%i)\n", \
12807 current_config->name.tu, \
12808 current_config->name.gmch_m, \
12809 current_config->name.gmch_n, \
12810 current_config->name.link_m, \
12811 current_config->name.link_n, \
12812 current_config->alt_name.tu, \
12813 current_config->alt_name.gmch_m, \
12814 current_config->alt_name.gmch_n, \
12815 current_config->alt_name.link_m, \
12816 current_config->alt_name.link_n, \
12817 pipe_config->name.tu, \
12818 pipe_config->name.gmch_m, \
12819 pipe_config->name.gmch_n, \
12820 pipe_config->name.link_m, \
12821 pipe_config->name.link_n); \
12822 ret = false; \
88adfff1
DV
12823 }
12824
1bd1bd80
DV
12825#define PIPE_CONF_CHECK_FLAGS(name, mask) \
12826 if ((current_config->name ^ pipe_config->name) & (mask)) { \
cfb23ed6 12827 INTEL_ERR_OR_DBG_KMS("mismatch in " #name "(" #mask ") " \
1bd1bd80
DV
12828 "(expected %i, found %i)\n", \
12829 current_config->name & (mask), \
12830 pipe_config->name & (mask)); \
cfb23ed6 12831 ret = false; \
1bd1bd80
DV
12832 }
12833
5e550656
VS
12834#define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
12835 if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
cfb23ed6 12836 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
5e550656
VS
12837 "(expected %i, found %i)\n", \
12838 current_config->name, \
12839 pipe_config->name); \
cfb23ed6 12840 ret = false; \
5e550656
VS
12841 }
12842
bb760063
DV
12843#define PIPE_CONF_QUIRK(quirk) \
12844 ((current_config->quirks | pipe_config->quirks) & (quirk))
12845
eccb140b
DV
12846 PIPE_CONF_CHECK_I(cpu_transcoder);
12847
08a24034
DV
12848 PIPE_CONF_CHECK_I(has_pch_encoder);
12849 PIPE_CONF_CHECK_I(fdi_lanes);
cfb23ed6 12850 PIPE_CONF_CHECK_M_N(fdi_m_n);
08a24034 12851
eb14cb74 12852 PIPE_CONF_CHECK_I(has_dp_encoder);
90a6b7b0 12853 PIPE_CONF_CHECK_I(lane_count);
95a7a2ae 12854 PIPE_CONF_CHECK_X(lane_lat_optim_mask);
b95af8be
VK
12855
12856 if (INTEL_INFO(dev)->gen < 8) {
cfb23ed6
ML
12857 PIPE_CONF_CHECK_M_N(dp_m_n);
12858
cfb23ed6
ML
12859 if (current_config->has_drrs)
12860 PIPE_CONF_CHECK_M_N(dp_m2_n2);
12861 } else
12862 PIPE_CONF_CHECK_M_N_ALT(dp_m_n, dp_m2_n2);
eb14cb74 12863
a65347ba
JN
12864 PIPE_CONF_CHECK_I(has_dsi_encoder);
12865
2d112de7
ACO
12866 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
12867 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
12868 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
12869 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
12870 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
12871 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
1bd1bd80 12872
2d112de7
ACO
12873 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
12874 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
12875 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
12876 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
12877 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
12878 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
1bd1bd80 12879
c93f54cf 12880 PIPE_CONF_CHECK_I(pixel_multiplier);
6897b4b5 12881 PIPE_CONF_CHECK_I(has_hdmi_sink);
b5a9fa09 12882 if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
666a4537 12883 IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev))
b5a9fa09 12884 PIPE_CONF_CHECK_I(limited_color_range);
e43823ec 12885 PIPE_CONF_CHECK_I(has_infoframe);
6c49f241 12886
9ed109a7
DV
12887 PIPE_CONF_CHECK_I(has_audio);
12888
2d112de7 12889 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
1bd1bd80
DV
12890 DRM_MODE_FLAG_INTERLACE);
12891
bb760063 12892 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
2d112de7 12893 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063 12894 DRM_MODE_FLAG_PHSYNC);
2d112de7 12895 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063 12896 DRM_MODE_FLAG_NHSYNC);
2d112de7 12897 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063 12898 DRM_MODE_FLAG_PVSYNC);
2d112de7 12899 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063
DV
12900 DRM_MODE_FLAG_NVSYNC);
12901 }
045ac3b5 12902
333b8ca8 12903 PIPE_CONF_CHECK_X(gmch_pfit.control);
e2ff2d4a
DV
12904 /* pfit ratios are autocomputed by the hw on gen4+ */
12905 if (INTEL_INFO(dev)->gen < 4)
7f7d8dd6 12906 PIPE_CONF_CHECK_X(gmch_pfit.pgm_ratios);
333b8ca8 12907 PIPE_CONF_CHECK_X(gmch_pfit.lvds_border_bits);
9953599b 12908
bfd16b2a
ML
12909 if (!adjust) {
12910 PIPE_CONF_CHECK_I(pipe_src_w);
12911 PIPE_CONF_CHECK_I(pipe_src_h);
12912
12913 PIPE_CONF_CHECK_I(pch_pfit.enabled);
12914 if (current_config->pch_pfit.enabled) {
12915 PIPE_CONF_CHECK_X(pch_pfit.pos);
12916 PIPE_CONF_CHECK_X(pch_pfit.size);
12917 }
2fa2fe9a 12918
7aefe2b5
ML
12919 PIPE_CONF_CHECK_I(scaler_state.scaler_id);
12920 }
a1b2278e 12921
e59150dc
JB
12922 /* BDW+ don't expose a synchronous way to read the state */
12923 if (IS_HASWELL(dev))
12924 PIPE_CONF_CHECK_I(ips_enabled);
42db64ef 12925
282740f7
VS
12926 PIPE_CONF_CHECK_I(double_wide);
12927
26804afd
DV
12928 PIPE_CONF_CHECK_X(ddi_pll_sel);
12929
8106ddbd 12930 PIPE_CONF_CHECK_P(shared_dpll);
66e985c0 12931 PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
8bcc2795 12932 PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
66e985c0
DV
12933 PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
12934 PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
d452c5b6 12935 PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
00490c22 12936 PIPE_CONF_CHECK_X(dpll_hw_state.spll);
3f4cd19f
DL
12937 PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
12938 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
12939 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
c0d43d62 12940
47eacbab
VS
12941 PIPE_CONF_CHECK_X(dsi_pll.ctrl);
12942 PIPE_CONF_CHECK_X(dsi_pll.div);
12943
42571aef
VS
12944 if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
12945 PIPE_CONF_CHECK_I(pipe_bpp);
12946
2d112de7 12947 PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
a9a7e98a 12948 PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
5e550656 12949
66e985c0 12950#undef PIPE_CONF_CHECK_X
08a24034 12951#undef PIPE_CONF_CHECK_I
8106ddbd 12952#undef PIPE_CONF_CHECK_P
1bd1bd80 12953#undef PIPE_CONF_CHECK_FLAGS
5e550656 12954#undef PIPE_CONF_CHECK_CLOCK_FUZZY
bb760063 12955#undef PIPE_CONF_QUIRK
cfb23ed6 12956#undef INTEL_ERR_OR_DBG_KMS
88adfff1 12957
cfb23ed6 12958 return ret;
0e8ffe1b
DV
12959}
12960
e3b247da
VS
12961static void intel_pipe_config_sanity_check(struct drm_i915_private *dev_priv,
12962 const struct intel_crtc_state *pipe_config)
12963{
12964 if (pipe_config->has_pch_encoder) {
21a727b3 12965 int fdi_dotclock = intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
e3b247da
VS
12966 &pipe_config->fdi_m_n);
12967 int dotclock = pipe_config->base.adjusted_mode.crtc_clock;
12968
12969 /*
12970 * FDI already provided one idea for the dotclock.
12971 * Yell if the encoder disagrees.
12972 */
12973 WARN(!intel_fuzzy_clock_check(fdi_dotclock, dotclock),
12974 "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
12975 fdi_dotclock, dotclock);
12976 }
12977}
12978
c0ead703
ML
12979static void verify_wm_state(struct drm_crtc *crtc,
12980 struct drm_crtc_state *new_state)
08db6652 12981{
e7c84544 12982 struct drm_device *dev = crtc->dev;
08db6652
DL
12983 struct drm_i915_private *dev_priv = dev->dev_private;
12984 struct skl_ddb_allocation hw_ddb, *sw_ddb;
e7c84544
ML
12985 struct skl_ddb_entry *hw_entry, *sw_entry;
12986 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
12987 const enum pipe pipe = intel_crtc->pipe;
08db6652
DL
12988 int plane;
12989
e7c84544 12990 if (INTEL_INFO(dev)->gen < 9 || !new_state->active)
08db6652
DL
12991 return;
12992
12993 skl_ddb_get_hw_state(dev_priv, &hw_ddb);
12994 sw_ddb = &dev_priv->wm.skl_hw.ddb;
12995
e7c84544
ML
12996 /* planes */
12997 for_each_plane(dev_priv, pipe, plane) {
12998 hw_entry = &hw_ddb.plane[pipe][plane];
12999 sw_entry = &sw_ddb->plane[pipe][plane];
08db6652 13000
e7c84544 13001 if (skl_ddb_entry_equal(hw_entry, sw_entry))
08db6652
DL
13002 continue;
13003
e7c84544
ML
13004 DRM_ERROR("mismatch in DDB state pipe %c plane %d "
13005 "(expected (%u,%u), found (%u,%u))\n",
13006 pipe_name(pipe), plane + 1,
13007 sw_entry->start, sw_entry->end,
13008 hw_entry->start, hw_entry->end);
13009 }
08db6652 13010
e7c84544
ML
13011 /* cursor */
13012 hw_entry = &hw_ddb.plane[pipe][PLANE_CURSOR];
13013 sw_entry = &sw_ddb->plane[pipe][PLANE_CURSOR];
08db6652 13014
e7c84544 13015 if (!skl_ddb_entry_equal(hw_entry, sw_entry)) {
08db6652
DL
13016 DRM_ERROR("mismatch in DDB state pipe %c cursor "
13017 "(expected (%u,%u), found (%u,%u))\n",
13018 pipe_name(pipe),
13019 sw_entry->start, sw_entry->end,
13020 hw_entry->start, hw_entry->end);
13021 }
13022}
13023
91d1b4bd 13024static void
c0ead703 13025verify_connector_state(struct drm_device *dev, struct drm_crtc *crtc)
8af6cf88 13026{
35dd3c64 13027 struct drm_connector *connector;
8af6cf88 13028
e7c84544 13029 drm_for_each_connector(connector, dev) {
35dd3c64
ML
13030 struct drm_encoder *encoder = connector->encoder;
13031 struct drm_connector_state *state = connector->state;
ad3c558f 13032
e7c84544
ML
13033 if (state->crtc != crtc)
13034 continue;
13035
5a21b665 13036 intel_connector_verify_state(to_intel_connector(connector));
8af6cf88 13037
ad3c558f 13038 I915_STATE_WARN(state->best_encoder != encoder,
35dd3c64 13039 "connector's atomic encoder doesn't match legacy encoder\n");
8af6cf88 13040 }
91d1b4bd
DV
13041}
13042
13043static void
c0ead703 13044verify_encoder_state(struct drm_device *dev)
91d1b4bd
DV
13045{
13046 struct intel_encoder *encoder;
13047 struct intel_connector *connector;
8af6cf88 13048
b2784e15 13049 for_each_intel_encoder(dev, encoder) {
8af6cf88 13050 bool enabled = false;
4d20cd86 13051 enum pipe pipe;
8af6cf88
DV
13052
13053 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
13054 encoder->base.base.id,
8e329a03 13055 encoder->base.name);
8af6cf88 13056
3a3371ff 13057 for_each_intel_connector(dev, connector) {
4d20cd86 13058 if (connector->base.state->best_encoder != &encoder->base)
8af6cf88
DV
13059 continue;
13060 enabled = true;
ad3c558f
ML
13061
13062 I915_STATE_WARN(connector->base.state->crtc !=
13063 encoder->base.crtc,
13064 "connector's crtc doesn't match encoder crtc\n");
8af6cf88 13065 }
0e32b39c 13066
e2c719b7 13067 I915_STATE_WARN(!!encoder->base.crtc != enabled,
8af6cf88
DV
13068 "encoder's enabled state mismatch "
13069 "(expected %i, found %i)\n",
13070 !!encoder->base.crtc, enabled);
7c60d198
ML
13071
13072 if (!encoder->base.crtc) {
4d20cd86 13073 bool active;
7c60d198 13074
4d20cd86
ML
13075 active = encoder->get_hw_state(encoder, &pipe);
13076 I915_STATE_WARN(active,
13077 "encoder detached but still enabled on pipe %c.\n",
13078 pipe_name(pipe));
7c60d198 13079 }
8af6cf88 13080 }
91d1b4bd
DV
13081}
13082
13083static void
c0ead703
ML
13084verify_crtc_state(struct drm_crtc *crtc,
13085 struct drm_crtc_state *old_crtc_state,
13086 struct drm_crtc_state *new_crtc_state)
91d1b4bd 13087{
e7c84544 13088 struct drm_device *dev = crtc->dev;
fbee40df 13089 struct drm_i915_private *dev_priv = dev->dev_private;
91d1b4bd 13090 struct intel_encoder *encoder;
e7c84544
ML
13091 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13092 struct intel_crtc_state *pipe_config, *sw_config;
13093 struct drm_atomic_state *old_state;
13094 bool active;
045ac3b5 13095
e7c84544 13096 old_state = old_crtc_state->state;
ec2dc6a0 13097 __drm_atomic_helper_crtc_destroy_state(old_crtc_state);
e7c84544
ML
13098 pipe_config = to_intel_crtc_state(old_crtc_state);
13099 memset(pipe_config, 0, sizeof(*pipe_config));
13100 pipe_config->base.crtc = crtc;
13101 pipe_config->base.state = old_state;
8af6cf88 13102
78108b7c 13103 DRM_DEBUG_KMS("[CRTC:%d:%s]\n", crtc->base.id, crtc->name);
8af6cf88 13104
e7c84544 13105 active = dev_priv->display.get_pipe_config(intel_crtc, pipe_config);
d62cf62a 13106
e7c84544
ML
13107 /* hw state is inconsistent with the pipe quirk */
13108 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
13109 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
13110 active = new_crtc_state->active;
6c49f241 13111
e7c84544
ML
13112 I915_STATE_WARN(new_crtc_state->active != active,
13113 "crtc active state doesn't match with hw state "
13114 "(expected %i, found %i)\n", new_crtc_state->active, active);
0e8ffe1b 13115
e7c84544
ML
13116 I915_STATE_WARN(intel_crtc->active != new_crtc_state->active,
13117 "transitional active state does not match atomic hw state "
13118 "(expected %i, found %i)\n", new_crtc_state->active, intel_crtc->active);
4d20cd86 13119
e7c84544
ML
13120 for_each_encoder_on_crtc(dev, crtc, encoder) {
13121 enum pipe pipe;
4d20cd86 13122
e7c84544
ML
13123 active = encoder->get_hw_state(encoder, &pipe);
13124 I915_STATE_WARN(active != new_crtc_state->active,
13125 "[ENCODER:%i] active %i with crtc active %i\n",
13126 encoder->base.base.id, active, new_crtc_state->active);
4d20cd86 13127
e7c84544
ML
13128 I915_STATE_WARN(active && intel_crtc->pipe != pipe,
13129 "Encoder connected to wrong pipe %c\n",
13130 pipe_name(pipe));
4d20cd86 13131
e7c84544
ML
13132 if (active)
13133 encoder->get_config(encoder, pipe_config);
13134 }
53d9f4e9 13135
e7c84544
ML
13136 if (!new_crtc_state->active)
13137 return;
cfb23ed6 13138
e7c84544 13139 intel_pipe_config_sanity_check(dev_priv, pipe_config);
e3b247da 13140
e7c84544
ML
13141 sw_config = to_intel_crtc_state(crtc->state);
13142 if (!intel_pipe_config_compare(dev, sw_config,
13143 pipe_config, false)) {
13144 I915_STATE_WARN(1, "pipe state doesn't match!\n");
13145 intel_dump_pipe_config(intel_crtc, pipe_config,
13146 "[hw state]");
13147 intel_dump_pipe_config(intel_crtc, sw_config,
13148 "[sw state]");
8af6cf88
DV
13149 }
13150}
13151
91d1b4bd 13152static void
c0ead703
ML
13153verify_single_dpll_state(struct drm_i915_private *dev_priv,
13154 struct intel_shared_dpll *pll,
13155 struct drm_crtc *crtc,
13156 struct drm_crtc_state *new_state)
91d1b4bd 13157{
91d1b4bd 13158 struct intel_dpll_hw_state dpll_hw_state;
e7c84544
ML
13159 unsigned crtc_mask;
13160 bool active;
5358901f 13161
e7c84544 13162 memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
5358901f 13163
e7c84544 13164 DRM_DEBUG_KMS("%s\n", pll->name);
5358901f 13165
e7c84544 13166 active = pll->funcs.get_hw_state(dev_priv, pll, &dpll_hw_state);
5358901f 13167
e7c84544
ML
13168 if (!(pll->flags & INTEL_DPLL_ALWAYS_ON)) {
13169 I915_STATE_WARN(!pll->on && pll->active_mask,
13170 "pll in active use but not on in sw tracking\n");
13171 I915_STATE_WARN(pll->on && !pll->active_mask,
13172 "pll is on but not used by any active crtc\n");
13173 I915_STATE_WARN(pll->on != active,
13174 "pll on state mismatch (expected %i, found %i)\n",
13175 pll->on, active);
13176 }
5358901f 13177
e7c84544 13178 if (!crtc) {
2dd66ebd 13179 I915_STATE_WARN(pll->active_mask & ~pll->config.crtc_mask,
e7c84544
ML
13180 "more active pll users than references: %x vs %x\n",
13181 pll->active_mask, pll->config.crtc_mask);
5358901f 13182
e7c84544
ML
13183 return;
13184 }
13185
13186 crtc_mask = 1 << drm_crtc_index(crtc);
13187
13188 if (new_state->active)
13189 I915_STATE_WARN(!(pll->active_mask & crtc_mask),
13190 "pll active mismatch (expected pipe %c in active mask 0x%02x)\n",
13191 pipe_name(drm_crtc_index(crtc)), pll->active_mask);
13192 else
13193 I915_STATE_WARN(pll->active_mask & crtc_mask,
13194 "pll active mismatch (didn't expect pipe %c in active mask 0x%02x)\n",
13195 pipe_name(drm_crtc_index(crtc)), pll->active_mask);
2dd66ebd 13196
e7c84544
ML
13197 I915_STATE_WARN(!(pll->config.crtc_mask & crtc_mask),
13198 "pll enabled crtcs mismatch (expected 0x%x in 0x%02x)\n",
13199 crtc_mask, pll->config.crtc_mask);
66e985c0 13200
e7c84544
ML
13201 I915_STATE_WARN(pll->on && memcmp(&pll->config.hw_state,
13202 &dpll_hw_state,
13203 sizeof(dpll_hw_state)),
13204 "pll hw state mismatch\n");
13205}
13206
13207static void
c0ead703
ML
13208verify_shared_dpll_state(struct drm_device *dev, struct drm_crtc *crtc,
13209 struct drm_crtc_state *old_crtc_state,
13210 struct drm_crtc_state *new_crtc_state)
e7c84544
ML
13211{
13212 struct drm_i915_private *dev_priv = dev->dev_private;
13213 struct intel_crtc_state *old_state = to_intel_crtc_state(old_crtc_state);
13214 struct intel_crtc_state *new_state = to_intel_crtc_state(new_crtc_state);
13215
13216 if (new_state->shared_dpll)
c0ead703 13217 verify_single_dpll_state(dev_priv, new_state->shared_dpll, crtc, new_crtc_state);
e7c84544
ML
13218
13219 if (old_state->shared_dpll &&
13220 old_state->shared_dpll != new_state->shared_dpll) {
13221 unsigned crtc_mask = 1 << drm_crtc_index(crtc);
13222 struct intel_shared_dpll *pll = old_state->shared_dpll;
13223
13224 I915_STATE_WARN(pll->active_mask & crtc_mask,
13225 "pll active mismatch (didn't expect pipe %c in active mask)\n",
13226 pipe_name(drm_crtc_index(crtc)));
13227 I915_STATE_WARN(pll->config.crtc_mask & crtc_mask,
13228 "pll enabled crtcs mismatch (found %x in enabled mask)\n",
13229 pipe_name(drm_crtc_index(crtc)));
5358901f 13230 }
8af6cf88
DV
13231}
13232
e7c84544 13233static void
c0ead703 13234intel_modeset_verify_crtc(struct drm_crtc *crtc,
e7c84544
ML
13235 struct drm_crtc_state *old_state,
13236 struct drm_crtc_state *new_state)
13237{
5a21b665
DV
13238 if (!needs_modeset(new_state) &&
13239 !to_intel_crtc_state(new_state)->update_pipe)
13240 return;
13241
c0ead703 13242 verify_wm_state(crtc, new_state);
5a21b665 13243 verify_connector_state(crtc->dev, crtc);
c0ead703
ML
13244 verify_crtc_state(crtc, old_state, new_state);
13245 verify_shared_dpll_state(crtc->dev, crtc, old_state, new_state);
e7c84544
ML
13246}
13247
13248static void
c0ead703 13249verify_disabled_dpll_state(struct drm_device *dev)
e7c84544
ML
13250{
13251 struct drm_i915_private *dev_priv = dev->dev_private;
13252 int i;
13253
13254 for (i = 0; i < dev_priv->num_shared_dpll; i++)
c0ead703 13255 verify_single_dpll_state(dev_priv, &dev_priv->shared_dplls[i], NULL, NULL);
e7c84544
ML
13256}
13257
13258static void
c0ead703 13259intel_modeset_verify_disabled(struct drm_device *dev)
e7c84544 13260{
c0ead703
ML
13261 verify_encoder_state(dev);
13262 verify_connector_state(dev, NULL);
13263 verify_disabled_dpll_state(dev);
e7c84544
ML
13264}
13265
80715b2f
VS
13266static void update_scanline_offset(struct intel_crtc *crtc)
13267{
13268 struct drm_device *dev = crtc->base.dev;
13269
13270 /*
13271 * The scanline counter increments at the leading edge of hsync.
13272 *
13273 * On most platforms it starts counting from vtotal-1 on the
13274 * first active line. That means the scanline counter value is
13275 * always one less than what we would expect. Ie. just after
13276 * start of vblank, which also occurs at start of hsync (on the
13277 * last active line), the scanline counter will read vblank_start-1.
13278 *
13279 * On gen2 the scanline counter starts counting from 1 instead
13280 * of vtotal-1, so we have to subtract one (or rather add vtotal-1
13281 * to keep the value positive), instead of adding one.
13282 *
13283 * On HSW+ the behaviour of the scanline counter depends on the output
13284 * type. For DP ports it behaves like most other platforms, but on HDMI
13285 * there's an extra 1 line difference. So we need to add two instead of
13286 * one to the value.
13287 */
13288 if (IS_GEN2(dev)) {
124abe07 13289 const struct drm_display_mode *adjusted_mode = &crtc->config->base.adjusted_mode;
80715b2f
VS
13290 int vtotal;
13291
124abe07
VS
13292 vtotal = adjusted_mode->crtc_vtotal;
13293 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
80715b2f
VS
13294 vtotal /= 2;
13295
13296 crtc->scanline_offset = vtotal - 1;
13297 } else if (HAS_DDI(dev) &&
409ee761 13298 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI)) {
80715b2f
VS
13299 crtc->scanline_offset = 2;
13300 } else
13301 crtc->scanline_offset = 1;
13302}
13303
ad421372 13304static void intel_modeset_clear_plls(struct drm_atomic_state *state)
ed6739ef 13305{
225da59b 13306 struct drm_device *dev = state->dev;
ed6739ef 13307 struct drm_i915_private *dev_priv = to_i915(dev);
ad421372 13308 struct intel_shared_dpll_config *shared_dpll = NULL;
0a9ab303
ACO
13309 struct drm_crtc *crtc;
13310 struct drm_crtc_state *crtc_state;
0a9ab303 13311 int i;
ed6739ef
ACO
13312
13313 if (!dev_priv->display.crtc_compute_clock)
ad421372 13314 return;
ed6739ef 13315
0a9ab303 13316 for_each_crtc_in_state(state, crtc, crtc_state, i) {
fb1a38a9 13317 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8106ddbd
ACO
13318 struct intel_shared_dpll *old_dpll =
13319 to_intel_crtc_state(crtc->state)->shared_dpll;
0a9ab303 13320
fb1a38a9 13321 if (!needs_modeset(crtc_state))
225da59b
ACO
13322 continue;
13323
8106ddbd 13324 to_intel_crtc_state(crtc_state)->shared_dpll = NULL;
fb1a38a9 13325
8106ddbd 13326 if (!old_dpll)
fb1a38a9 13327 continue;
0a9ab303 13328
ad421372
ML
13329 if (!shared_dpll)
13330 shared_dpll = intel_atomic_get_shared_dpll_state(state);
ed6739ef 13331
8106ddbd 13332 intel_shared_dpll_config_put(shared_dpll, old_dpll, intel_crtc);
ad421372 13333 }
ed6739ef
ACO
13334}
13335
99d736a2
ML
13336/*
13337 * This implements the workaround described in the "notes" section of the mode
13338 * set sequence documentation. When going from no pipes or single pipe to
13339 * multiple pipes, and planes are enabled after the pipe, we need to wait at
13340 * least 2 vblanks on the first pipe before enabling planes on the second pipe.
13341 */
13342static int haswell_mode_set_planes_workaround(struct drm_atomic_state *state)
13343{
13344 struct drm_crtc_state *crtc_state;
13345 struct intel_crtc *intel_crtc;
13346 struct drm_crtc *crtc;
13347 struct intel_crtc_state *first_crtc_state = NULL;
13348 struct intel_crtc_state *other_crtc_state = NULL;
13349 enum pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
13350 int i;
13351
13352 /* look at all crtc's that are going to be enabled in during modeset */
13353 for_each_crtc_in_state(state, crtc, crtc_state, i) {
13354 intel_crtc = to_intel_crtc(crtc);
13355
13356 if (!crtc_state->active || !needs_modeset(crtc_state))
13357 continue;
13358
13359 if (first_crtc_state) {
13360 other_crtc_state = to_intel_crtc_state(crtc_state);
13361 break;
13362 } else {
13363 first_crtc_state = to_intel_crtc_state(crtc_state);
13364 first_pipe = intel_crtc->pipe;
13365 }
13366 }
13367
13368 /* No workaround needed? */
13369 if (!first_crtc_state)
13370 return 0;
13371
13372 /* w/a possibly needed, check how many crtc's are already enabled. */
13373 for_each_intel_crtc(state->dev, intel_crtc) {
13374 struct intel_crtc_state *pipe_config;
13375
13376 pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
13377 if (IS_ERR(pipe_config))
13378 return PTR_ERR(pipe_config);
13379
13380 pipe_config->hsw_workaround_pipe = INVALID_PIPE;
13381
13382 if (!pipe_config->base.active ||
13383 needs_modeset(&pipe_config->base))
13384 continue;
13385
13386 /* 2 or more enabled crtcs means no need for w/a */
13387 if (enabled_pipe != INVALID_PIPE)
13388 return 0;
13389
13390 enabled_pipe = intel_crtc->pipe;
13391 }
13392
13393 if (enabled_pipe != INVALID_PIPE)
13394 first_crtc_state->hsw_workaround_pipe = enabled_pipe;
13395 else if (other_crtc_state)
13396 other_crtc_state->hsw_workaround_pipe = first_pipe;
13397
13398 return 0;
13399}
13400
27c329ed
ML
13401static int intel_modeset_all_pipes(struct drm_atomic_state *state)
13402{
13403 struct drm_crtc *crtc;
13404 struct drm_crtc_state *crtc_state;
13405 int ret = 0;
13406
13407 /* add all active pipes to the state */
13408 for_each_crtc(state->dev, crtc) {
13409 crtc_state = drm_atomic_get_crtc_state(state, crtc);
13410 if (IS_ERR(crtc_state))
13411 return PTR_ERR(crtc_state);
13412
13413 if (!crtc_state->active || needs_modeset(crtc_state))
13414 continue;
13415
13416 crtc_state->mode_changed = true;
13417
13418 ret = drm_atomic_add_affected_connectors(state, crtc);
13419 if (ret)
13420 break;
13421
13422 ret = drm_atomic_add_affected_planes(state, crtc);
13423 if (ret)
13424 break;
13425 }
13426
13427 return ret;
13428}
13429
c347a676 13430static int intel_modeset_checks(struct drm_atomic_state *state)
054518dd 13431{
565602d7
ML
13432 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
13433 struct drm_i915_private *dev_priv = state->dev->dev_private;
13434 struct drm_crtc *crtc;
13435 struct drm_crtc_state *crtc_state;
13436 int ret = 0, i;
054518dd 13437
b359283a
ML
13438 if (!check_digital_port_conflicts(state)) {
13439 DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
13440 return -EINVAL;
13441 }
13442
565602d7
ML
13443 intel_state->modeset = true;
13444 intel_state->active_crtcs = dev_priv->active_crtcs;
13445
13446 for_each_crtc_in_state(state, crtc, crtc_state, i) {
13447 if (crtc_state->active)
13448 intel_state->active_crtcs |= 1 << i;
13449 else
13450 intel_state->active_crtcs &= ~(1 << i);
8b4a7d05
MR
13451
13452 if (crtc_state->active != crtc->state->active)
13453 intel_state->active_pipe_changes |= drm_crtc_mask(crtc);
565602d7
ML
13454 }
13455
054518dd
ACO
13456 /*
13457 * See if the config requires any additional preparation, e.g.
13458 * to adjust global state with pipes off. We need to do this
13459 * here so we can get the modeset_pipe updated config for the new
13460 * mode set on this crtc. For other crtcs we need to use the
13461 * adjusted_mode bits in the crtc directly.
13462 */
27c329ed 13463 if (dev_priv->display.modeset_calc_cdclk) {
c89e39f3 13464 if (!intel_state->cdclk_pll_vco)
63911d72 13465 intel_state->cdclk_pll_vco = dev_priv->cdclk_pll.vco;
b2045352
VS
13466 if (!intel_state->cdclk_pll_vco)
13467 intel_state->cdclk_pll_vco = dev_priv->skl_preferred_vco_freq;
c89e39f3 13468
27c329ed 13469 ret = dev_priv->display.modeset_calc_cdclk(state);
c89e39f3
CT
13470 if (ret < 0)
13471 return ret;
27c329ed 13472
c89e39f3 13473 if (intel_state->dev_cdclk != dev_priv->cdclk_freq ||
63911d72 13474 intel_state->cdclk_pll_vco != dev_priv->cdclk_pll.vco)
27c329ed
ML
13475 ret = intel_modeset_all_pipes(state);
13476
13477 if (ret < 0)
054518dd 13478 return ret;
e8788cbc
ML
13479
13480 DRM_DEBUG_KMS("New cdclk calculated to be atomic %u, actual %u\n",
13481 intel_state->cdclk, intel_state->dev_cdclk);
27c329ed 13482 } else
1a617b77 13483 to_intel_atomic_state(state)->cdclk = dev_priv->atomic_cdclk_freq;
054518dd 13484
ad421372 13485 intel_modeset_clear_plls(state);
054518dd 13486
565602d7 13487 if (IS_HASWELL(dev_priv))
ad421372 13488 return haswell_mode_set_planes_workaround(state);
99d736a2 13489
ad421372 13490 return 0;
c347a676
ACO
13491}
13492
aa363136
MR
13493/*
13494 * Handle calculation of various watermark data at the end of the atomic check
13495 * phase. The code here should be run after the per-crtc and per-plane 'check'
13496 * handlers to ensure that all derived state has been updated.
13497 */
55994c2c 13498static int calc_watermark_data(struct drm_atomic_state *state)
aa363136
MR
13499{
13500 struct drm_device *dev = state->dev;
98d39494 13501 struct drm_i915_private *dev_priv = to_i915(dev);
98d39494
MR
13502
13503 /* Is there platform-specific watermark information to calculate? */
13504 if (dev_priv->display.compute_global_watermarks)
55994c2c
MR
13505 return dev_priv->display.compute_global_watermarks(state);
13506
13507 return 0;
aa363136
MR
13508}
13509
74c090b1
ML
13510/**
13511 * intel_atomic_check - validate state object
13512 * @dev: drm device
13513 * @state: state to validate
13514 */
13515static int intel_atomic_check(struct drm_device *dev,
13516 struct drm_atomic_state *state)
c347a676 13517{
dd8b3bdb 13518 struct drm_i915_private *dev_priv = to_i915(dev);
aa363136 13519 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
c347a676
ACO
13520 struct drm_crtc *crtc;
13521 struct drm_crtc_state *crtc_state;
13522 int ret, i;
61333b60 13523 bool any_ms = false;
c347a676 13524
74c090b1 13525 ret = drm_atomic_helper_check_modeset(dev, state);
054518dd
ACO
13526 if (ret)
13527 return ret;
13528
c347a676 13529 for_each_crtc_in_state(state, crtc, crtc_state, i) {
cfb23ed6
ML
13530 struct intel_crtc_state *pipe_config =
13531 to_intel_crtc_state(crtc_state);
1ed51de9
DV
13532
13533 /* Catch I915_MODE_FLAG_INHERITED */
13534 if (crtc_state->mode.private_flags != crtc->state->mode.private_flags)
13535 crtc_state->mode_changed = true;
cfb23ed6 13536
af4a879e 13537 if (!needs_modeset(crtc_state))
c347a676
ACO
13538 continue;
13539
af4a879e
DV
13540 if (!crtc_state->enable) {
13541 any_ms = true;
cfb23ed6 13542 continue;
af4a879e 13543 }
cfb23ed6 13544
26495481
DV
13545 /* FIXME: For only active_changed we shouldn't need to do any
13546 * state recomputation at all. */
13547
1ed51de9
DV
13548 ret = drm_atomic_add_affected_connectors(state, crtc);
13549 if (ret)
13550 return ret;
b359283a 13551
cfb23ed6 13552 ret = intel_modeset_pipe_config(crtc, pipe_config);
25aa1c39
ML
13553 if (ret) {
13554 intel_dump_pipe_config(to_intel_crtc(crtc),
13555 pipe_config, "[failed]");
c347a676 13556 return ret;
25aa1c39 13557 }
c347a676 13558
73831236 13559 if (i915.fastboot &&
dd8b3bdb 13560 intel_pipe_config_compare(dev,
cfb23ed6 13561 to_intel_crtc_state(crtc->state),
1ed51de9 13562 pipe_config, true)) {
26495481 13563 crtc_state->mode_changed = false;
bfd16b2a 13564 to_intel_crtc_state(crtc_state)->update_pipe = true;
26495481
DV
13565 }
13566
af4a879e 13567 if (needs_modeset(crtc_state))
26495481 13568 any_ms = true;
cfb23ed6 13569
af4a879e
DV
13570 ret = drm_atomic_add_affected_planes(state, crtc);
13571 if (ret)
13572 return ret;
61333b60 13573
26495481
DV
13574 intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
13575 needs_modeset(crtc_state) ?
13576 "[modeset]" : "[fastset]");
c347a676
ACO
13577 }
13578
61333b60
ML
13579 if (any_ms) {
13580 ret = intel_modeset_checks(state);
13581
13582 if (ret)
13583 return ret;
27c329ed 13584 } else
dd8b3bdb 13585 intel_state->cdclk = dev_priv->cdclk_freq;
76305b1a 13586
dd8b3bdb 13587 ret = drm_atomic_helper_check_planes(dev, state);
aa363136
MR
13588 if (ret)
13589 return ret;
13590
f51be2e0 13591 intel_fbc_choose_crtc(dev_priv, state);
55994c2c 13592 return calc_watermark_data(state);
054518dd
ACO
13593}
13594
5008e874
ML
13595static int intel_atomic_prepare_commit(struct drm_device *dev,
13596 struct drm_atomic_state *state,
81072bfd 13597 bool nonblock)
5008e874 13598{
7580d774
ML
13599 struct drm_i915_private *dev_priv = dev->dev_private;
13600 struct drm_plane_state *plane_state;
5008e874 13601 struct drm_crtc_state *crtc_state;
7580d774 13602 struct drm_plane *plane;
5008e874
ML
13603 struct drm_crtc *crtc;
13604 int i, ret;
13605
5a21b665
DV
13606 for_each_crtc_in_state(state, crtc, crtc_state, i) {
13607 if (state->legacy_cursor_update)
a6747b73
ML
13608 continue;
13609
5a21b665
DV
13610 ret = intel_crtc_wait_for_pending_flips(crtc);
13611 if (ret)
13612 return ret;
5008e874 13613
5a21b665
DV
13614 if (atomic_read(&to_intel_crtc(crtc)->unpin_work_count) >= 2)
13615 flush_workqueue(dev_priv->wq);
d55dbd06
ML
13616 }
13617
f935675f
ML
13618 ret = mutex_lock_interruptible(&dev->struct_mutex);
13619 if (ret)
13620 return ret;
13621
5008e874 13622 ret = drm_atomic_helper_prepare_planes(dev, state);
f7e5838b 13623 mutex_unlock(&dev->struct_mutex);
7580d774 13624
21daaeee 13625 if (!ret && !nonblock) {
7580d774
ML
13626 for_each_plane_in_state(state, plane, plane_state, i) {
13627 struct intel_plane_state *intel_plane_state =
13628 to_intel_plane_state(plane_state);
13629
13630 if (!intel_plane_state->wait_req)
13631 continue;
13632
13633 ret = __i915_wait_request(intel_plane_state->wait_req,
299259a3 13634 true, NULL, NULL);
f7e5838b 13635 if (ret) {
f4457ae7
CW
13636 /* Any hang should be swallowed by the wait */
13637 WARN_ON(ret == -EIO);
f7e5838b
CW
13638 mutex_lock(&dev->struct_mutex);
13639 drm_atomic_helper_cleanup_planes(dev, state);
13640 mutex_unlock(&dev->struct_mutex);
7580d774 13641 break;
f7e5838b 13642 }
7580d774 13643 }
7580d774 13644 }
5008e874
ML
13645
13646 return ret;
13647}
13648
a2991414
ML
13649u32 intel_crtc_get_vblank_counter(struct intel_crtc *crtc)
13650{
13651 struct drm_device *dev = crtc->base.dev;
13652
13653 if (!dev->max_vblank_count)
13654 return drm_accurate_vblank_count(&crtc->base);
13655
13656 return dev->driver->get_vblank_counter(dev, crtc->pipe);
13657}
13658
5a21b665
DV
13659static void intel_atomic_wait_for_vblanks(struct drm_device *dev,
13660 struct drm_i915_private *dev_priv,
13661 unsigned crtc_mask)
e8861675 13662{
5a21b665
DV
13663 unsigned last_vblank_count[I915_MAX_PIPES];
13664 enum pipe pipe;
13665 int ret;
e8861675 13666
5a21b665
DV
13667 if (!crtc_mask)
13668 return;
e8861675 13669
5a21b665
DV
13670 for_each_pipe(dev_priv, pipe) {
13671 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
e8861675 13672
5a21b665 13673 if (!((1 << pipe) & crtc_mask))
e8861675
ML
13674 continue;
13675
5a21b665
DV
13676 ret = drm_crtc_vblank_get(crtc);
13677 if (WARN_ON(ret != 0)) {
13678 crtc_mask &= ~(1 << pipe);
13679 continue;
e8861675
ML
13680 }
13681
5a21b665 13682 last_vblank_count[pipe] = drm_crtc_vblank_count(crtc);
e8861675
ML
13683 }
13684
5a21b665
DV
13685 for_each_pipe(dev_priv, pipe) {
13686 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
13687 long lret;
e8861675 13688
5a21b665
DV
13689 if (!((1 << pipe) & crtc_mask))
13690 continue;
d55dbd06 13691
5a21b665
DV
13692 lret = wait_event_timeout(dev->vblank[pipe].queue,
13693 last_vblank_count[pipe] !=
13694 drm_crtc_vblank_count(crtc),
13695 msecs_to_jiffies(50));
d55dbd06 13696
5a21b665 13697 WARN(!lret, "pipe %c vblank wait timed out\n", pipe_name(pipe));
d55dbd06 13698
5a21b665 13699 drm_crtc_vblank_put(crtc);
d55dbd06
ML
13700 }
13701}
13702
5a21b665 13703static bool needs_vblank_wait(struct intel_crtc_state *crtc_state)
a6747b73 13704{
5a21b665
DV
13705 /* fb updated, need to unpin old fb */
13706 if (crtc_state->fb_changed)
13707 return true;
a6747b73 13708
5a21b665
DV
13709 /* wm changes, need vblank before final wm's */
13710 if (crtc_state->update_wm_post)
13711 return true;
a6747b73 13712
5a21b665
DV
13713 /*
13714 * cxsr is re-enabled after vblank.
13715 * This is already handled by crtc_state->update_wm_post,
13716 * but added for clarity.
13717 */
13718 if (crtc_state->disable_cxsr)
13719 return true;
a6747b73 13720
5a21b665 13721 return false;
e8861675
ML
13722}
13723
94f05024 13724static void intel_atomic_commit_tail(struct drm_atomic_state *state)
a6778b3c 13725{
94f05024 13726 struct drm_device *dev = state->dev;
565602d7 13727 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
fbee40df 13728 struct drm_i915_private *dev_priv = dev->dev_private;
29ceb0e6 13729 struct drm_crtc_state *old_crtc_state;
7580d774 13730 struct drm_crtc *crtc;
5a21b665 13731 struct intel_crtc_state *intel_cstate;
94f05024
DV
13732 struct drm_plane *plane;
13733 struct drm_plane_state *plane_state;
5a21b665
DV
13734 bool hw_check = intel_state->modeset;
13735 unsigned long put_domains[I915_MAX_PIPES] = {};
13736 unsigned crtc_vblank_mask = 0;
94f05024 13737 int i, ret;
a6778b3c 13738
94f05024
DV
13739 for_each_plane_in_state(state, plane, plane_state, i) {
13740 struct intel_plane_state *intel_plane_state =
13741 to_intel_plane_state(plane_state);
ea0000f0 13742
94f05024
DV
13743 if (!intel_plane_state->wait_req)
13744 continue;
d4afb8cc 13745
94f05024
DV
13746 ret = __i915_wait_request(intel_plane_state->wait_req,
13747 true, NULL, NULL);
13748 /* EIO should be eaten, and we can't get interrupted in the
13749 * worker, and blocking commits have waited already. */
13750 WARN_ON(ret);
13751 }
1c5e19f8 13752
ea0000f0
DV
13753 drm_atomic_helper_wait_for_dependencies(state);
13754
565602d7
ML
13755 if (intel_state->modeset) {
13756 memcpy(dev_priv->min_pixclk, intel_state->min_pixclk,
13757 sizeof(intel_state->min_pixclk));
13758 dev_priv->active_crtcs = intel_state->active_crtcs;
1a617b77 13759 dev_priv->atomic_cdclk_freq = intel_state->cdclk;
5a21b665
DV
13760
13761 intel_display_power_get(dev_priv, POWER_DOMAIN_MODESET);
565602d7
ML
13762 }
13763
29ceb0e6 13764 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
a539205a
ML
13765 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13766
5a21b665
DV
13767 if (needs_modeset(crtc->state) ||
13768 to_intel_crtc_state(crtc->state)->update_pipe) {
13769 hw_check = true;
13770
13771 put_domains[to_intel_crtc(crtc)->pipe] =
13772 modeset_get_crtc_power_domains(crtc,
13773 to_intel_crtc_state(crtc->state));
13774 }
13775
61333b60
ML
13776 if (!needs_modeset(crtc->state))
13777 continue;
13778
29ceb0e6 13779 intel_pre_plane_update(to_intel_crtc_state(old_crtc_state));
460da916 13780
29ceb0e6
VS
13781 if (old_crtc_state->active) {
13782 intel_crtc_disable_planes(crtc, old_crtc_state->plane_mask);
a539205a 13783 dev_priv->display.crtc_disable(crtc);
eddfcbcd 13784 intel_crtc->active = false;
58f9c0bc 13785 intel_fbc_disable(intel_crtc);
eddfcbcd 13786 intel_disable_shared_dpll(intel_crtc);
9bbc8258
VS
13787
13788 /*
13789 * Underruns don't always raise
13790 * interrupts, so check manually.
13791 */
13792 intel_check_cpu_fifo_underruns(dev_priv);
13793 intel_check_pch_fifo_underruns(dev_priv);
b9001114
ML
13794
13795 if (!crtc->state->active)
13796 intel_update_watermarks(crtc);
a539205a 13797 }
b8cecdf5 13798 }
7758a113 13799
ea9d758d
DV
13800 /* Only after disabling all output pipelines that will be changed can we
13801 * update the the output configuration. */
4740b0f2 13802 intel_modeset_update_crtc_state(state);
f6e5b160 13803
565602d7 13804 if (intel_state->modeset) {
4740b0f2 13805 drm_atomic_helper_update_legacy_modeset_state(state->dev, state);
33c8df89
ML
13806
13807 if (dev_priv->display.modeset_commit_cdclk &&
c89e39f3 13808 (intel_state->dev_cdclk != dev_priv->cdclk_freq ||
63911d72 13809 intel_state->cdclk_pll_vco != dev_priv->cdclk_pll.vco))
33c8df89 13810 dev_priv->display.modeset_commit_cdclk(state);
f6d1973d 13811
c0ead703 13812 intel_modeset_verify_disabled(dev);
4740b0f2 13813 }
47fab737 13814
a6778b3c 13815 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
29ceb0e6 13816 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
f6ac4b2a
ML
13817 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13818 bool modeset = needs_modeset(crtc->state);
5a21b665
DV
13819 struct intel_crtc_state *pipe_config =
13820 to_intel_crtc_state(crtc->state);
9f836f90 13821
f6ac4b2a 13822 if (modeset && crtc->state->active) {
a539205a
ML
13823 update_scanline_offset(to_intel_crtc(crtc));
13824 dev_priv->display.crtc_enable(crtc);
13825 }
80715b2f 13826
1f7528c4
DV
13827 /* Complete events for now disable pipes here. */
13828 if (modeset && !crtc->state->active && crtc->state->event) {
13829 spin_lock_irq(&dev->event_lock);
13830 drm_crtc_send_vblank_event(crtc, crtc->state->event);
13831 spin_unlock_irq(&dev->event_lock);
13832
13833 crtc->state->event = NULL;
13834 }
13835
f6ac4b2a 13836 if (!modeset)
29ceb0e6 13837 intel_pre_plane_update(to_intel_crtc_state(old_crtc_state));
f6ac4b2a 13838
5a21b665
DV
13839 if (crtc->state->active &&
13840 drm_atomic_get_existing_plane_state(state, crtc->primary))
faf68d92 13841 intel_fbc_enable(intel_crtc, pipe_config, to_intel_plane_state(crtc->primary->state));
5a21b665 13842
1f7528c4 13843 if (crtc->state->active)
5a21b665 13844 drm_atomic_helper_commit_planes_on_crtc(old_crtc_state);
f6d1973d 13845
5a21b665
DV
13846 if (pipe_config->base.active && needs_vblank_wait(pipe_config))
13847 crtc_vblank_mask |= 1 << i;
177246a8
MR
13848 }
13849
94f05024
DV
13850 /* FIXME: We should call drm_atomic_helper_commit_hw_done() here
13851 * already, but still need the state for the delayed optimization. To
13852 * fix this:
13853 * - wrap the optimization/post_plane_update stuff into a per-crtc work.
13854 * - schedule that vblank worker _before_ calling hw_done
13855 * - at the start of commit_tail, cancel it _synchrously
13856 * - switch over to the vblank wait helper in the core after that since
13857 * we don't need out special handling any more.
13858 */
5a21b665
DV
13859 if (!state->legacy_cursor_update)
13860 intel_atomic_wait_for_vblanks(dev, dev_priv, crtc_vblank_mask);
13861
13862 /*
13863 * Now that the vblank has passed, we can go ahead and program the
13864 * optimal watermarks on platforms that need two-step watermark
13865 * programming.
13866 *
13867 * TODO: Move this (and other cleanup) to an async worker eventually.
13868 */
13869 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
13870 intel_cstate = to_intel_crtc_state(crtc->state);
13871
13872 if (dev_priv->display.optimize_watermarks)
13873 dev_priv->display.optimize_watermarks(intel_cstate);
13874 }
13875
13876 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
13877 intel_post_plane_update(to_intel_crtc_state(old_crtc_state));
13878
13879 if (put_domains[i])
13880 modeset_put_power_domains(dev_priv, put_domains[i]);
13881
13882 intel_modeset_verify_crtc(crtc, old_crtc_state, crtc->state);
13883 }
13884
94f05024
DV
13885 drm_atomic_helper_commit_hw_done(state);
13886
5a21b665
DV
13887 if (intel_state->modeset)
13888 intel_display_power_put(dev_priv, POWER_DOMAIN_MODESET);
13889
13890 mutex_lock(&dev->struct_mutex);
13891 drm_atomic_helper_cleanup_planes(dev, state);
13892 mutex_unlock(&dev->struct_mutex);
13893
ea0000f0
DV
13894 drm_atomic_helper_commit_cleanup_done(state);
13895
ee165b1a 13896 drm_atomic_state_free(state);
f30da187 13897
75714940
MK
13898 /* As one of the primary mmio accessors, KMS has a high likelihood
13899 * of triggering bugs in unclaimed access. After we finish
13900 * modesetting, see if an error has been flagged, and if so
13901 * enable debugging for the next modeset - and hope we catch
13902 * the culprit.
13903 *
13904 * XXX note that we assume display power is on at this point.
13905 * This might hold true now but we need to add pm helper to check
13906 * unclaimed only when the hardware is on, as atomic commits
13907 * can happen also when the device is completely off.
13908 */
13909 intel_uncore_arm_unclaimed_mmio_detection(dev_priv);
94f05024
DV
13910}
13911
13912static void intel_atomic_commit_work(struct work_struct *work)
13913{
13914 struct drm_atomic_state *state = container_of(work,
13915 struct drm_atomic_state,
13916 commit_work);
13917 intel_atomic_commit_tail(state);
13918}
13919
6c9c1b38
DV
13920static void intel_atomic_track_fbs(struct drm_atomic_state *state)
13921{
13922 struct drm_plane_state *old_plane_state;
13923 struct drm_plane *plane;
13924 struct drm_i915_gem_object *obj, *old_obj;
13925 struct intel_plane *intel_plane;
13926 int i;
13927
13928 mutex_lock(&state->dev->struct_mutex);
13929 for_each_plane_in_state(state, plane, old_plane_state, i) {
13930 obj = intel_fb_obj(plane->state->fb);
13931 old_obj = intel_fb_obj(old_plane_state->fb);
13932 intel_plane = to_intel_plane(plane);
13933
13934 i915_gem_track_fb(old_obj, obj, intel_plane->frontbuffer_bit);
13935 }
13936 mutex_unlock(&state->dev->struct_mutex);
13937}
13938
94f05024
DV
13939/**
13940 * intel_atomic_commit - commit validated state object
13941 * @dev: DRM device
13942 * @state: the top-level driver state object
13943 * @nonblock: nonblocking commit
13944 *
13945 * This function commits a top-level state object that has been validated
13946 * with drm_atomic_helper_check().
13947 *
13948 * FIXME: Atomic modeset support for i915 is not yet complete. At the moment
13949 * nonblocking commits are only safe for pure plane updates. Everything else
13950 * should work though.
13951 *
13952 * RETURNS
13953 * Zero for success or -errno.
13954 */
13955static int intel_atomic_commit(struct drm_device *dev,
13956 struct drm_atomic_state *state,
13957 bool nonblock)
13958{
13959 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
13960 struct drm_i915_private *dev_priv = dev->dev_private;
13961 int ret = 0;
13962
13963 if (intel_state->modeset && nonblock) {
13964 DRM_DEBUG_KMS("nonblocking commit for modeset not yet implemented.\n");
13965 return -EINVAL;
13966 }
13967
13968 ret = drm_atomic_helper_setup_commit(state, nonblock);
13969 if (ret)
13970 return ret;
13971
13972 INIT_WORK(&state->commit_work, intel_atomic_commit_work);
13973
13974 ret = intel_atomic_prepare_commit(dev, state, nonblock);
13975 if (ret) {
13976 DRM_DEBUG_ATOMIC("Preparing state failed with %i\n", ret);
13977 return ret;
13978 }
13979
13980 drm_atomic_helper_swap_state(state, true);
13981 dev_priv->wm.distrust_bios_wm = false;
13982 dev_priv->wm.skl_results = intel_state->wm_results;
13983 intel_shared_dpll_commit(state);
6c9c1b38 13984 intel_atomic_track_fbs(state);
94f05024
DV
13985
13986 if (nonblock)
13987 queue_work(system_unbound_wq, &state->commit_work);
13988 else
13989 intel_atomic_commit_tail(state);
75714940 13990
74c090b1 13991 return 0;
7f27126e
JB
13992}
13993
c0c36b94
CW
13994void intel_crtc_restore_mode(struct drm_crtc *crtc)
13995{
83a57153
ACO
13996 struct drm_device *dev = crtc->dev;
13997 struct drm_atomic_state *state;
e694eb02 13998 struct drm_crtc_state *crtc_state;
2bfb4627 13999 int ret;
83a57153
ACO
14000
14001 state = drm_atomic_state_alloc(dev);
14002 if (!state) {
78108b7c
VS
14003 DRM_DEBUG_KMS("[CRTC:%d:%s] crtc restore failed, out of memory",
14004 crtc->base.id, crtc->name);
83a57153
ACO
14005 return;
14006 }
14007
e694eb02 14008 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
83a57153 14009
e694eb02
ML
14010retry:
14011 crtc_state = drm_atomic_get_crtc_state(state, crtc);
14012 ret = PTR_ERR_OR_ZERO(crtc_state);
14013 if (!ret) {
14014 if (!crtc_state->active)
14015 goto out;
83a57153 14016
e694eb02 14017 crtc_state->mode_changed = true;
74c090b1 14018 ret = drm_atomic_commit(state);
83a57153
ACO
14019 }
14020
e694eb02
ML
14021 if (ret == -EDEADLK) {
14022 drm_atomic_state_clear(state);
14023 drm_modeset_backoff(state->acquire_ctx);
14024 goto retry;
4ed9fb37 14025 }
4be07317 14026
2bfb4627 14027 if (ret)
e694eb02 14028out:
2bfb4627 14029 drm_atomic_state_free(state);
c0c36b94
CW
14030}
14031
25c5b266
DV
14032#undef for_each_intel_crtc_masked
14033
f6e5b160 14034static const struct drm_crtc_funcs intel_crtc_funcs = {
82cf435b 14035 .gamma_set = drm_atomic_helper_legacy_gamma_set,
74c090b1 14036 .set_config = drm_atomic_helper_set_config,
82cf435b 14037 .set_property = drm_atomic_helper_crtc_set_property,
f6e5b160 14038 .destroy = intel_crtc_destroy,
527b6abe 14039 .page_flip = intel_crtc_page_flip,
1356837e
MR
14040 .atomic_duplicate_state = intel_crtc_duplicate_state,
14041 .atomic_destroy_state = intel_crtc_destroy_state,
f6e5b160
CW
14042};
14043
6beb8c23
MR
14044/**
14045 * intel_prepare_plane_fb - Prepare fb for usage on plane
14046 * @plane: drm plane to prepare for
14047 * @fb: framebuffer to prepare for presentation
14048 *
14049 * Prepares a framebuffer for usage on a display plane. Generally this
14050 * involves pinning the underlying object and updating the frontbuffer tracking
14051 * bits. Some older platforms need special physical address handling for
14052 * cursor planes.
14053 *
f935675f
ML
14054 * Must be called with struct_mutex held.
14055 *
6beb8c23
MR
14056 * Returns 0 on success, negative error code on failure.
14057 */
14058int
14059intel_prepare_plane_fb(struct drm_plane *plane,
d136dfee 14060 const struct drm_plane_state *new_state)
465c120c
MR
14061{
14062 struct drm_device *dev = plane->dev;
844f9111 14063 struct drm_framebuffer *fb = new_state->fb;
6beb8c23 14064 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
1ee49399 14065 struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->state->fb);
c37efb99 14066 struct reservation_object *resv;
6beb8c23 14067 int ret = 0;
465c120c 14068
1ee49399 14069 if (!obj && !old_obj)
465c120c
MR
14070 return 0;
14071
5008e874
ML
14072 if (old_obj) {
14073 struct drm_crtc_state *crtc_state =
14074 drm_atomic_get_existing_crtc_state(new_state->state, plane->state->crtc);
14075
14076 /* Big Hammer, we also need to ensure that any pending
14077 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
14078 * current scanout is retired before unpinning the old
14079 * framebuffer. Note that we rely on userspace rendering
14080 * into the buffer attached to the pipe they are waiting
14081 * on. If not, userspace generates a GPU hang with IPEHR
14082 * point to the MI_WAIT_FOR_EVENT.
14083 *
14084 * This should only fail upon a hung GPU, in which case we
14085 * can safely continue.
14086 */
14087 if (needs_modeset(crtc_state))
14088 ret = i915_gem_object_wait_rendering(old_obj, true);
f4457ae7
CW
14089 if (ret) {
14090 /* GPU hangs should have been swallowed by the wait */
14091 WARN_ON(ret == -EIO);
f935675f 14092 return ret;
f4457ae7 14093 }
5008e874
ML
14094 }
14095
c37efb99
CW
14096 if (!obj)
14097 return 0;
14098
5a21b665 14099 /* For framebuffer backed by dmabuf, wait for fence */
c37efb99
CW
14100 resv = i915_gem_object_get_dmabuf_resv(obj);
14101 if (resv) {
5a21b665
DV
14102 long lret;
14103
c37efb99 14104 lret = reservation_object_wait_timeout_rcu(resv, false, true,
5a21b665
DV
14105 MAX_SCHEDULE_TIMEOUT);
14106 if (lret == -ERESTARTSYS)
14107 return lret;
14108
14109 WARN(lret < 0, "waiting returns %li\n", lret);
14110 }
14111
c37efb99 14112 if (plane->type == DRM_PLANE_TYPE_CURSOR &&
6beb8c23
MR
14113 INTEL_INFO(dev)->cursor_needs_physical) {
14114 int align = IS_I830(dev) ? 16 * 1024 : 256;
14115 ret = i915_gem_object_attach_phys(obj, align);
14116 if (ret)
14117 DRM_DEBUG_KMS("failed to attach phys object\n");
14118 } else {
3465c580 14119 ret = intel_pin_and_fence_fb_obj(fb, new_state->rotation);
6beb8c23 14120 }
465c120c 14121
c37efb99 14122 if (ret == 0) {
6c9c1b38
DV
14123 struct intel_plane_state *plane_state =
14124 to_intel_plane_state(new_state);
7580d774 14125
6c9c1b38
DV
14126 i915_gem_request_assign(&plane_state->wait_req,
14127 obj->last_write_req);
7580d774 14128 }
fdd508a6 14129
6beb8c23
MR
14130 return ret;
14131}
14132
38f3ce3a
MR
14133/**
14134 * intel_cleanup_plane_fb - Cleans up an fb after plane use
14135 * @plane: drm plane to clean up for
14136 * @fb: old framebuffer that was on plane
14137 *
14138 * Cleans up a framebuffer that has just been removed from a plane.
f935675f
ML
14139 *
14140 * Must be called with struct_mutex held.
38f3ce3a
MR
14141 */
14142void
14143intel_cleanup_plane_fb(struct drm_plane *plane,
d136dfee 14144 const struct drm_plane_state *old_state)
38f3ce3a
MR
14145{
14146 struct drm_device *dev = plane->dev;
7580d774 14147 struct intel_plane_state *old_intel_state;
1ee49399
ML
14148 struct drm_i915_gem_object *old_obj = intel_fb_obj(old_state->fb);
14149 struct drm_i915_gem_object *obj = intel_fb_obj(plane->state->fb);
38f3ce3a 14150
7580d774
ML
14151 old_intel_state = to_intel_plane_state(old_state);
14152
1ee49399 14153 if (!obj && !old_obj)
38f3ce3a
MR
14154 return;
14155
1ee49399
ML
14156 if (old_obj && (plane->type != DRM_PLANE_TYPE_CURSOR ||
14157 !INTEL_INFO(dev)->cursor_needs_physical))
3465c580 14158 intel_unpin_fb_obj(old_state->fb, old_state->rotation);
1ee49399 14159
7580d774 14160 i915_gem_request_assign(&old_intel_state->wait_req, NULL);
465c120c
MR
14161}
14162
6156a456
CK
14163int
14164skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
14165{
14166 int max_scale;
14167 struct drm_device *dev;
14168 struct drm_i915_private *dev_priv;
14169 int crtc_clock, cdclk;
14170
bf8a0af0 14171 if (!intel_crtc || !crtc_state->base.enable)
6156a456
CK
14172 return DRM_PLANE_HELPER_NO_SCALING;
14173
14174 dev = intel_crtc->base.dev;
14175 dev_priv = dev->dev_private;
14176 crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
27c329ed 14177 cdclk = to_intel_atomic_state(crtc_state->base.state)->cdclk;
6156a456 14178
54bf1ce6 14179 if (WARN_ON_ONCE(!crtc_clock || cdclk < crtc_clock))
6156a456
CK
14180 return DRM_PLANE_HELPER_NO_SCALING;
14181
14182 /*
14183 * skl max scale is lower of:
14184 * close to 3 but not 3, -1 is for that purpose
14185 * or
14186 * cdclk/crtc_clock
14187 */
14188 max_scale = min((1 << 16) * 3 - 1, (1 << 8) * ((cdclk << 8) / crtc_clock));
14189
14190 return max_scale;
14191}
14192
465c120c 14193static int
3c692a41 14194intel_check_primary_plane(struct drm_plane *plane,
061e4b8d 14195 struct intel_crtc_state *crtc_state,
3c692a41
GP
14196 struct intel_plane_state *state)
14197{
2b875c22
MR
14198 struct drm_crtc *crtc = state->base.crtc;
14199 struct drm_framebuffer *fb = state->base.fb;
6156a456 14200 int min_scale = DRM_PLANE_HELPER_NO_SCALING;
061e4b8d
ML
14201 int max_scale = DRM_PLANE_HELPER_NO_SCALING;
14202 bool can_position = false;
465c120c 14203
693bdc28
VS
14204 if (INTEL_INFO(plane->dev)->gen >= 9) {
14205 /* use scaler when colorkey is not required */
14206 if (state->ckey.flags == I915_SET_COLORKEY_NONE) {
14207 min_scale = 1;
14208 max_scale = skl_max_scale(to_intel_crtc(crtc), crtc_state);
14209 }
d8106366 14210 can_position = true;
6156a456 14211 }
d8106366 14212
061e4b8d
ML
14213 return drm_plane_helper_check_update(plane, crtc, fb, &state->src,
14214 &state->dst, &state->clip,
9b8b013d 14215 state->base.rotation,
da20eabd
ML
14216 min_scale, max_scale,
14217 can_position, true,
14218 &state->visible);
14af293f
GP
14219}
14220
5a21b665
DV
14221static void intel_begin_crtc_commit(struct drm_crtc *crtc,
14222 struct drm_crtc_state *old_crtc_state)
14223{
14224 struct drm_device *dev = crtc->dev;
14225 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
14226 struct intel_crtc_state *old_intel_state =
14227 to_intel_crtc_state(old_crtc_state);
14228 bool modeset = needs_modeset(crtc->state);
14229
14230 /* Perform vblank evasion around commit operation */
14231 intel_pipe_update_start(intel_crtc);
14232
14233 if (modeset)
14234 return;
14235
14236 if (crtc->state->color_mgmt_changed || to_intel_crtc_state(crtc->state)->update_pipe) {
14237 intel_color_set_csc(crtc->state);
14238 intel_color_load_luts(crtc->state);
14239 }
14240
14241 if (to_intel_crtc_state(crtc->state)->update_pipe)
14242 intel_update_pipe_config(intel_crtc, old_intel_state);
14243 else if (INTEL_INFO(dev)->gen >= 9)
14244 skl_detach_scalers(intel_crtc);
14245}
14246
14247static void intel_finish_crtc_commit(struct drm_crtc *crtc,
14248 struct drm_crtc_state *old_crtc_state)
14249{
14250 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
14251
14252 intel_pipe_update_end(intel_crtc, NULL);
14253}
14254
cf4c7c12 14255/**
4a3b8769
MR
14256 * intel_plane_destroy - destroy a plane
14257 * @plane: plane to destroy
cf4c7c12 14258 *
4a3b8769
MR
14259 * Common destruction function for all types of planes (primary, cursor,
14260 * sprite).
cf4c7c12 14261 */
4a3b8769 14262void intel_plane_destroy(struct drm_plane *plane)
465c120c 14263{
69ae561f
VS
14264 if (!plane)
14265 return;
14266
465c120c 14267 drm_plane_cleanup(plane);
69ae561f 14268 kfree(to_intel_plane(plane));
465c120c
MR
14269}
14270
65a3fea0 14271const struct drm_plane_funcs intel_plane_funcs = {
70a101f8
MR
14272 .update_plane = drm_atomic_helper_update_plane,
14273 .disable_plane = drm_atomic_helper_disable_plane,
3d7d6510 14274 .destroy = intel_plane_destroy,
c196e1d6 14275 .set_property = drm_atomic_helper_plane_set_property,
a98b3431
MR
14276 .atomic_get_property = intel_plane_atomic_get_property,
14277 .atomic_set_property = intel_plane_atomic_set_property,
ea2c67bb
MR
14278 .atomic_duplicate_state = intel_plane_duplicate_state,
14279 .atomic_destroy_state = intel_plane_destroy_state,
14280
465c120c
MR
14281};
14282
14283static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
14284 int pipe)
14285{
fca0ce2a
VS
14286 struct intel_plane *primary = NULL;
14287 struct intel_plane_state *state = NULL;
465c120c 14288 const uint32_t *intel_primary_formats;
45e3743a 14289 unsigned int num_formats;
fca0ce2a 14290 int ret;
465c120c
MR
14291
14292 primary = kzalloc(sizeof(*primary), GFP_KERNEL);
fca0ce2a
VS
14293 if (!primary)
14294 goto fail;
465c120c 14295
8e7d688b 14296 state = intel_create_plane_state(&primary->base);
fca0ce2a
VS
14297 if (!state)
14298 goto fail;
8e7d688b 14299 primary->base.state = &state->base;
ea2c67bb 14300
465c120c
MR
14301 primary->can_scale = false;
14302 primary->max_downscale = 1;
6156a456
CK
14303 if (INTEL_INFO(dev)->gen >= 9) {
14304 primary->can_scale = true;
af99ceda 14305 state->scaler_id = -1;
6156a456 14306 }
465c120c
MR
14307 primary->pipe = pipe;
14308 primary->plane = pipe;
a9ff8714 14309 primary->frontbuffer_bit = INTEL_FRONTBUFFER_PRIMARY(pipe);
c59cb179 14310 primary->check_plane = intel_check_primary_plane;
465c120c
MR
14311 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
14312 primary->plane = !pipe;
14313
6c0fd451
DL
14314 if (INTEL_INFO(dev)->gen >= 9) {
14315 intel_primary_formats = skl_primary_formats;
14316 num_formats = ARRAY_SIZE(skl_primary_formats);
a8d201af
ML
14317
14318 primary->update_plane = skylake_update_primary_plane;
14319 primary->disable_plane = skylake_disable_primary_plane;
14320 } else if (HAS_PCH_SPLIT(dev)) {
14321 intel_primary_formats = i965_primary_formats;
14322 num_formats = ARRAY_SIZE(i965_primary_formats);
14323
14324 primary->update_plane = ironlake_update_primary_plane;
14325 primary->disable_plane = i9xx_disable_primary_plane;
6c0fd451 14326 } else if (INTEL_INFO(dev)->gen >= 4) {
568db4f2
DL
14327 intel_primary_formats = i965_primary_formats;
14328 num_formats = ARRAY_SIZE(i965_primary_formats);
a8d201af
ML
14329
14330 primary->update_plane = i9xx_update_primary_plane;
14331 primary->disable_plane = i9xx_disable_primary_plane;
6c0fd451
DL
14332 } else {
14333 intel_primary_formats = i8xx_primary_formats;
14334 num_formats = ARRAY_SIZE(i8xx_primary_formats);
a8d201af
ML
14335
14336 primary->update_plane = i9xx_update_primary_plane;
14337 primary->disable_plane = i9xx_disable_primary_plane;
465c120c
MR
14338 }
14339
38573dc1
VS
14340 if (INTEL_INFO(dev)->gen >= 9)
14341 ret = drm_universal_plane_init(dev, &primary->base, 0,
14342 &intel_plane_funcs,
14343 intel_primary_formats, num_formats,
14344 DRM_PLANE_TYPE_PRIMARY,
14345 "plane 1%c", pipe_name(pipe));
14346 else if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
14347 ret = drm_universal_plane_init(dev, &primary->base, 0,
14348 &intel_plane_funcs,
14349 intel_primary_formats, num_formats,
14350 DRM_PLANE_TYPE_PRIMARY,
14351 "primary %c", pipe_name(pipe));
14352 else
14353 ret = drm_universal_plane_init(dev, &primary->base, 0,
14354 &intel_plane_funcs,
14355 intel_primary_formats, num_formats,
14356 DRM_PLANE_TYPE_PRIMARY,
14357 "plane %c", plane_name(primary->plane));
fca0ce2a
VS
14358 if (ret)
14359 goto fail;
48404c1e 14360
3b7a5119
SJ
14361 if (INTEL_INFO(dev)->gen >= 4)
14362 intel_create_rotation_property(dev, primary);
48404c1e 14363
ea2c67bb
MR
14364 drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
14365
465c120c 14366 return &primary->base;
fca0ce2a
VS
14367
14368fail:
14369 kfree(state);
14370 kfree(primary);
14371
14372 return NULL;
465c120c
MR
14373}
14374
3b7a5119
SJ
14375void intel_create_rotation_property(struct drm_device *dev, struct intel_plane *plane)
14376{
14377 if (!dev->mode_config.rotation_property) {
14378 unsigned long flags = BIT(DRM_ROTATE_0) |
14379 BIT(DRM_ROTATE_180);
14380
14381 if (INTEL_INFO(dev)->gen >= 9)
14382 flags |= BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_270);
14383
14384 dev->mode_config.rotation_property =
14385 drm_mode_create_rotation_property(dev, flags);
14386 }
14387 if (dev->mode_config.rotation_property)
14388 drm_object_attach_property(&plane->base.base,
14389 dev->mode_config.rotation_property,
14390 plane->base.state->rotation);
14391}
14392
3d7d6510 14393static int
852e787c 14394intel_check_cursor_plane(struct drm_plane *plane,
061e4b8d 14395 struct intel_crtc_state *crtc_state,
852e787c 14396 struct intel_plane_state *state)
3d7d6510 14397{
061e4b8d 14398 struct drm_crtc *crtc = crtc_state->base.crtc;
2b875c22 14399 struct drm_framebuffer *fb = state->base.fb;
757f9a3e 14400 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
b29ec92c 14401 enum pipe pipe = to_intel_plane(plane)->pipe;
757f9a3e
GP
14402 unsigned stride;
14403 int ret;
3d7d6510 14404
061e4b8d
ML
14405 ret = drm_plane_helper_check_update(plane, crtc, fb, &state->src,
14406 &state->dst, &state->clip,
9b8b013d 14407 state->base.rotation,
3d7d6510
MR
14408 DRM_PLANE_HELPER_NO_SCALING,
14409 DRM_PLANE_HELPER_NO_SCALING,
852e787c 14410 true, true, &state->visible);
757f9a3e
GP
14411 if (ret)
14412 return ret;
14413
757f9a3e
GP
14414 /* if we want to turn off the cursor ignore width and height */
14415 if (!obj)
da20eabd 14416 return 0;
757f9a3e 14417
757f9a3e 14418 /* Check for which cursor types we support */
061e4b8d 14419 if (!cursor_size_ok(plane->dev, state->base.crtc_w, state->base.crtc_h)) {
ea2c67bb
MR
14420 DRM_DEBUG("Cursor dimension %dx%d not supported\n",
14421 state->base.crtc_w, state->base.crtc_h);
757f9a3e
GP
14422 return -EINVAL;
14423 }
14424
ea2c67bb
MR
14425 stride = roundup_pow_of_two(state->base.crtc_w) * 4;
14426 if (obj->base.size < stride * state->base.crtc_h) {
757f9a3e
GP
14427 DRM_DEBUG_KMS("buffer is too small\n");
14428 return -ENOMEM;
14429 }
14430
3a656b54 14431 if (fb->modifier[0] != DRM_FORMAT_MOD_NONE) {
757f9a3e 14432 DRM_DEBUG_KMS("cursor cannot be tiled\n");
da20eabd 14433 return -EINVAL;
32b7eeec
MR
14434 }
14435
b29ec92c
VS
14436 /*
14437 * There's something wrong with the cursor on CHV pipe C.
14438 * If it straddles the left edge of the screen then
14439 * moving it away from the edge or disabling it often
14440 * results in a pipe underrun, and often that can lead to
14441 * dead pipe (constant underrun reported, and it scans
14442 * out just a solid color). To recover from that, the
14443 * display power well must be turned off and on again.
14444 * Refuse the put the cursor into that compromised position.
14445 */
14446 if (IS_CHERRYVIEW(plane->dev) && pipe == PIPE_C &&
14447 state->visible && state->base.crtc_x < 0) {
14448 DRM_DEBUG_KMS("CHV cursor C not allowed to straddle the left screen edge\n");
14449 return -EINVAL;
14450 }
14451
da20eabd 14452 return 0;
852e787c 14453}
3d7d6510 14454
a8ad0d8e
ML
14455static void
14456intel_disable_cursor_plane(struct drm_plane *plane,
7fabf5ef 14457 struct drm_crtc *crtc)
a8ad0d8e 14458{
f2858021
ML
14459 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
14460
14461 intel_crtc->cursor_addr = 0;
55a08b3f 14462 intel_crtc_update_cursor(crtc, NULL);
a8ad0d8e
ML
14463}
14464
f4a2cf29 14465static void
55a08b3f
ML
14466intel_update_cursor_plane(struct drm_plane *plane,
14467 const struct intel_crtc_state *crtc_state,
14468 const struct intel_plane_state *state)
852e787c 14469{
55a08b3f
ML
14470 struct drm_crtc *crtc = crtc_state->base.crtc;
14471 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ea2c67bb 14472 struct drm_device *dev = plane->dev;
2b875c22 14473 struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
a912f12f 14474 uint32_t addr;
852e787c 14475
f4a2cf29 14476 if (!obj)
a912f12f 14477 addr = 0;
f4a2cf29 14478 else if (!INTEL_INFO(dev)->cursor_needs_physical)
a912f12f 14479 addr = i915_gem_obj_ggtt_offset(obj);
f4a2cf29 14480 else
a912f12f 14481 addr = obj->phys_handle->busaddr;
852e787c 14482
a912f12f 14483 intel_crtc->cursor_addr = addr;
55a08b3f 14484 intel_crtc_update_cursor(crtc, state);
852e787c
GP
14485}
14486
3d7d6510
MR
14487static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
14488 int pipe)
14489{
fca0ce2a
VS
14490 struct intel_plane *cursor = NULL;
14491 struct intel_plane_state *state = NULL;
14492 int ret;
3d7d6510
MR
14493
14494 cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
fca0ce2a
VS
14495 if (!cursor)
14496 goto fail;
3d7d6510 14497
8e7d688b 14498 state = intel_create_plane_state(&cursor->base);
fca0ce2a
VS
14499 if (!state)
14500 goto fail;
8e7d688b 14501 cursor->base.state = &state->base;
ea2c67bb 14502
3d7d6510
MR
14503 cursor->can_scale = false;
14504 cursor->max_downscale = 1;
14505 cursor->pipe = pipe;
14506 cursor->plane = pipe;
a9ff8714 14507 cursor->frontbuffer_bit = INTEL_FRONTBUFFER_CURSOR(pipe);
c59cb179 14508 cursor->check_plane = intel_check_cursor_plane;
55a08b3f 14509 cursor->update_plane = intel_update_cursor_plane;
a8ad0d8e 14510 cursor->disable_plane = intel_disable_cursor_plane;
3d7d6510 14511
fca0ce2a
VS
14512 ret = drm_universal_plane_init(dev, &cursor->base, 0,
14513 &intel_plane_funcs,
14514 intel_cursor_formats,
14515 ARRAY_SIZE(intel_cursor_formats),
38573dc1
VS
14516 DRM_PLANE_TYPE_CURSOR,
14517 "cursor %c", pipe_name(pipe));
fca0ce2a
VS
14518 if (ret)
14519 goto fail;
4398ad45
VS
14520
14521 if (INTEL_INFO(dev)->gen >= 4) {
14522 if (!dev->mode_config.rotation_property)
14523 dev->mode_config.rotation_property =
14524 drm_mode_create_rotation_property(dev,
14525 BIT(DRM_ROTATE_0) |
14526 BIT(DRM_ROTATE_180));
14527 if (dev->mode_config.rotation_property)
14528 drm_object_attach_property(&cursor->base.base,
14529 dev->mode_config.rotation_property,
8e7d688b 14530 state->base.rotation);
4398ad45
VS
14531 }
14532
af99ceda
CK
14533 if (INTEL_INFO(dev)->gen >=9)
14534 state->scaler_id = -1;
14535
ea2c67bb
MR
14536 drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
14537
3d7d6510 14538 return &cursor->base;
fca0ce2a
VS
14539
14540fail:
14541 kfree(state);
14542 kfree(cursor);
14543
14544 return NULL;
3d7d6510
MR
14545}
14546
549e2bfb
CK
14547static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
14548 struct intel_crtc_state *crtc_state)
14549{
14550 int i;
14551 struct intel_scaler *intel_scaler;
14552 struct intel_crtc_scaler_state *scaler_state = &crtc_state->scaler_state;
14553
14554 for (i = 0; i < intel_crtc->num_scalers; i++) {
14555 intel_scaler = &scaler_state->scalers[i];
14556 intel_scaler->in_use = 0;
549e2bfb
CK
14557 intel_scaler->mode = PS_SCALER_MODE_DYN;
14558 }
14559
14560 scaler_state->scaler_id = -1;
14561}
14562
b358d0a6 14563static void intel_crtc_init(struct drm_device *dev, int pipe)
79e53945 14564{
fbee40df 14565 struct drm_i915_private *dev_priv = dev->dev_private;
79e53945 14566 struct intel_crtc *intel_crtc;
f5de6e07 14567 struct intel_crtc_state *crtc_state = NULL;
3d7d6510
MR
14568 struct drm_plane *primary = NULL;
14569 struct drm_plane *cursor = NULL;
8563b1e8 14570 int ret;
79e53945 14571
955382f3 14572 intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
79e53945
JB
14573 if (intel_crtc == NULL)
14574 return;
14575
f5de6e07
ACO
14576 crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
14577 if (!crtc_state)
14578 goto fail;
550acefd
ACO
14579 intel_crtc->config = crtc_state;
14580 intel_crtc->base.state = &crtc_state->base;
07878248 14581 crtc_state->base.crtc = &intel_crtc->base;
f5de6e07 14582
549e2bfb
CK
14583 /* initialize shared scalers */
14584 if (INTEL_INFO(dev)->gen >= 9) {
14585 if (pipe == PIPE_C)
14586 intel_crtc->num_scalers = 1;
14587 else
14588 intel_crtc->num_scalers = SKL_NUM_SCALERS;
14589
14590 skl_init_scalers(dev, intel_crtc, crtc_state);
14591 }
14592
465c120c 14593 primary = intel_primary_plane_create(dev, pipe);
3d7d6510
MR
14594 if (!primary)
14595 goto fail;
14596
14597 cursor = intel_cursor_plane_create(dev, pipe);
14598 if (!cursor)
14599 goto fail;
14600
465c120c 14601 ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
4d5d72b7
VS
14602 cursor, &intel_crtc_funcs,
14603 "pipe %c", pipe_name(pipe));
3d7d6510
MR
14604 if (ret)
14605 goto fail;
79e53945 14606
1f1c2e24
VS
14607 /*
14608 * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
8c0f92e1 14609 * is hooked to pipe B. Hence we want plane A feeding pipe B.
1f1c2e24 14610 */
80824003
JB
14611 intel_crtc->pipe = pipe;
14612 intel_crtc->plane = pipe;
3a77c4c4 14613 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
28c97730 14614 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
e2e767ab 14615 intel_crtc->plane = !pipe;
80824003
JB
14616 }
14617
4b0e333e
CW
14618 intel_crtc->cursor_base = ~0;
14619 intel_crtc->cursor_cntl = ~0;
dc41c154 14620 intel_crtc->cursor_size = ~0;
8d7849db 14621
852eb00d
VS
14622 intel_crtc->wm.cxsr_allowed = true;
14623
22fd0fab
JB
14624 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
14625 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
14626 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
14627 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
14628
79e53945 14629 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
87b6b101 14630
8563b1e8
LL
14631 intel_color_init(&intel_crtc->base);
14632
87b6b101 14633 WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
3d7d6510
MR
14634 return;
14635
14636fail:
69ae561f
VS
14637 intel_plane_destroy(primary);
14638 intel_plane_destroy(cursor);
f5de6e07 14639 kfree(crtc_state);
3d7d6510 14640 kfree(intel_crtc);
79e53945
JB
14641}
14642
752aa88a
JB
14643enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
14644{
14645 struct drm_encoder *encoder = connector->base.encoder;
6e9f798d 14646 struct drm_device *dev = connector->base.dev;
752aa88a 14647
51fd371b 14648 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
752aa88a 14649
d3babd3f 14650 if (!encoder || WARN_ON(!encoder->crtc))
752aa88a
JB
14651 return INVALID_PIPE;
14652
14653 return to_intel_crtc(encoder->crtc)->pipe;
14654}
14655
08d7b3d1 14656int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
05394f39 14657 struct drm_file *file)
08d7b3d1 14658{
08d7b3d1 14659 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
7707e653 14660 struct drm_crtc *drmmode_crtc;
c05422d5 14661 struct intel_crtc *crtc;
08d7b3d1 14662
7707e653 14663 drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
71240ed2 14664 if (!drmmode_crtc)
3f2c2057 14665 return -ENOENT;
08d7b3d1 14666
7707e653 14667 crtc = to_intel_crtc(drmmode_crtc);
c05422d5 14668 pipe_from_crtc_id->pipe = crtc->pipe;
08d7b3d1 14669
c05422d5 14670 return 0;
08d7b3d1
CW
14671}
14672
66a9278e 14673static int intel_encoder_clones(struct intel_encoder *encoder)
79e53945 14674{
66a9278e
DV
14675 struct drm_device *dev = encoder->base.dev;
14676 struct intel_encoder *source_encoder;
79e53945 14677 int index_mask = 0;
79e53945
JB
14678 int entry = 0;
14679
b2784e15 14680 for_each_intel_encoder(dev, source_encoder) {
bc079e8b 14681 if (encoders_cloneable(encoder, source_encoder))
66a9278e
DV
14682 index_mask |= (1 << entry);
14683
79e53945
JB
14684 entry++;
14685 }
4ef69c7a 14686
79e53945
JB
14687 return index_mask;
14688}
14689
4d302442
CW
14690static bool has_edp_a(struct drm_device *dev)
14691{
14692 struct drm_i915_private *dev_priv = dev->dev_private;
14693
14694 if (!IS_MOBILE(dev))
14695 return false;
14696
14697 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
14698 return false;
14699
e3589908 14700 if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
4d302442
CW
14701 return false;
14702
14703 return true;
14704}
14705
84b4e042
JB
14706static bool intel_crt_present(struct drm_device *dev)
14707{
14708 struct drm_i915_private *dev_priv = dev->dev_private;
14709
884497ed
DL
14710 if (INTEL_INFO(dev)->gen >= 9)
14711 return false;
14712
cf404ce4 14713 if (IS_HSW_ULT(dev) || IS_BDW_ULT(dev))
84b4e042
JB
14714 return false;
14715
14716 if (IS_CHERRYVIEW(dev))
14717 return false;
14718
65e472e4
VS
14719 if (HAS_PCH_LPT_H(dev) && I915_READ(SFUSE_STRAP) & SFUSE_STRAP_CRT_DISABLED)
14720 return false;
14721
70ac54d0
VS
14722 /* DDI E can't be used if DDI A requires 4 lanes */
14723 if (HAS_DDI(dev) && I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_A_4_LANES)
14724 return false;
14725
e4abb733 14726 if (!dev_priv->vbt.int_crt_support)
84b4e042
JB
14727 return false;
14728
14729 return true;
14730}
14731
79e53945
JB
14732static void intel_setup_outputs(struct drm_device *dev)
14733{
725e30ad 14734 struct drm_i915_private *dev_priv = dev->dev_private;
4ef69c7a 14735 struct intel_encoder *encoder;
cb0953d7 14736 bool dpd_is_edp = false;
79e53945 14737
97a824e1
ID
14738 /*
14739 * intel_edp_init_connector() depends on this completing first, to
14740 * prevent the registeration of both eDP and LVDS and the incorrect
14741 * sharing of the PPS.
14742 */
c9093354 14743 intel_lvds_init(dev);
79e53945 14744
84b4e042 14745 if (intel_crt_present(dev))
79935fca 14746 intel_crt_init(dev);
cb0953d7 14747
c776eb2e
VK
14748 if (IS_BROXTON(dev)) {
14749 /*
14750 * FIXME: Broxton doesn't support port detection via the
14751 * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
14752 * detect the ports.
14753 */
14754 intel_ddi_init(dev, PORT_A);
14755 intel_ddi_init(dev, PORT_B);
14756 intel_ddi_init(dev, PORT_C);
c6c794a2
SS
14757
14758 intel_dsi_init(dev);
c776eb2e 14759 } else if (HAS_DDI(dev)) {
0e72a5b5
ED
14760 int found;
14761
de31facd
JB
14762 /*
14763 * Haswell uses DDI functions to detect digital outputs.
14764 * On SKL pre-D0 the strap isn't connected, so we assume
14765 * it's there.
14766 */
77179400 14767 found = I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_INIT_DISPLAY_DETECTED;
de31facd 14768 /* WaIgnoreDDIAStrap: skl */
ef11bdb3 14769 if (found || IS_SKYLAKE(dev) || IS_KABYLAKE(dev))
0e72a5b5
ED
14770 intel_ddi_init(dev, PORT_A);
14771
14772 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
14773 * register */
14774 found = I915_READ(SFUSE_STRAP);
14775
14776 if (found & SFUSE_STRAP_DDIB_DETECTED)
14777 intel_ddi_init(dev, PORT_B);
14778 if (found & SFUSE_STRAP_DDIC_DETECTED)
14779 intel_ddi_init(dev, PORT_C);
14780 if (found & SFUSE_STRAP_DDID_DETECTED)
14781 intel_ddi_init(dev, PORT_D);
2800e4c2
RV
14782 /*
14783 * On SKL we don't have a way to detect DDI-E so we rely on VBT.
14784 */
ef11bdb3 14785 if ((IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) &&
2800e4c2
RV
14786 (dev_priv->vbt.ddi_port_info[PORT_E].supports_dp ||
14787 dev_priv->vbt.ddi_port_info[PORT_E].supports_dvi ||
14788 dev_priv->vbt.ddi_port_info[PORT_E].supports_hdmi))
14789 intel_ddi_init(dev, PORT_E);
14790
0e72a5b5 14791 } else if (HAS_PCH_SPLIT(dev)) {
cb0953d7 14792 int found;
5d8a7752 14793 dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
270b3042
DV
14794
14795 if (has_edp_a(dev))
14796 intel_dp_init(dev, DP_A, PORT_A);
cb0953d7 14797
dc0fa718 14798 if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
461ed3ca 14799 /* PCH SDVOB multiplex with HDMIB */
2a5c0832 14800 found = intel_sdvo_init(dev, PCH_SDVOB, PORT_B);
30ad48b7 14801 if (!found)
e2debe91 14802 intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
5eb08b69 14803 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
ab9d7c30 14804 intel_dp_init(dev, PCH_DP_B, PORT_B);
30ad48b7
ZW
14805 }
14806
dc0fa718 14807 if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
e2debe91 14808 intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
30ad48b7 14809
dc0fa718 14810 if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
e2debe91 14811 intel_hdmi_init(dev, PCH_HDMID, PORT_D);
30ad48b7 14812
5eb08b69 14813 if (I915_READ(PCH_DP_C) & DP_DETECTED)
ab9d7c30 14814 intel_dp_init(dev, PCH_DP_C, PORT_C);
5eb08b69 14815
270b3042 14816 if (I915_READ(PCH_DP_D) & DP_DETECTED)
ab9d7c30 14817 intel_dp_init(dev, PCH_DP_D, PORT_D);
666a4537 14818 } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
22f35042 14819 bool has_edp, has_port;
457c52d8 14820
e17ac6db
VS
14821 /*
14822 * The DP_DETECTED bit is the latched state of the DDC
14823 * SDA pin at boot. However since eDP doesn't require DDC
14824 * (no way to plug in a DP->HDMI dongle) the DDC pins for
14825 * eDP ports may have been muxed to an alternate function.
14826 * Thus we can't rely on the DP_DETECTED bit alone to detect
14827 * eDP ports. Consult the VBT as well as DP_DETECTED to
14828 * detect eDP ports.
22f35042
VS
14829 *
14830 * Sadly the straps seem to be missing sometimes even for HDMI
14831 * ports (eg. on Voyo V3 - CHT x7-Z8700), so check both strap
14832 * and VBT for the presence of the port. Additionally we can't
14833 * trust the port type the VBT declares as we've seen at least
14834 * HDMI ports that the VBT claim are DP or eDP.
e17ac6db 14835 */
457c52d8 14836 has_edp = intel_dp_is_edp(dev, PORT_B);
22f35042
VS
14837 has_port = intel_bios_is_port_present(dev_priv, PORT_B);
14838 if (I915_READ(VLV_DP_B) & DP_DETECTED || has_port)
457c52d8 14839 has_edp &= intel_dp_init(dev, VLV_DP_B, PORT_B);
22f35042 14840 if ((I915_READ(VLV_HDMIB) & SDVO_DETECTED || has_port) && !has_edp)
e66eb81d 14841 intel_hdmi_init(dev, VLV_HDMIB, PORT_B);
585a94b8 14842
457c52d8 14843 has_edp = intel_dp_is_edp(dev, PORT_C);
22f35042
VS
14844 has_port = intel_bios_is_port_present(dev_priv, PORT_C);
14845 if (I915_READ(VLV_DP_C) & DP_DETECTED || has_port)
457c52d8 14846 has_edp &= intel_dp_init(dev, VLV_DP_C, PORT_C);
22f35042 14847 if ((I915_READ(VLV_HDMIC) & SDVO_DETECTED || has_port) && !has_edp)
e66eb81d 14848 intel_hdmi_init(dev, VLV_HDMIC, PORT_C);
19c03924 14849
9418c1f1 14850 if (IS_CHERRYVIEW(dev)) {
22f35042
VS
14851 /*
14852 * eDP not supported on port D,
14853 * so no need to worry about it
14854 */
14855 has_port = intel_bios_is_port_present(dev_priv, PORT_D);
14856 if (I915_READ(CHV_DP_D) & DP_DETECTED || has_port)
e66eb81d 14857 intel_dp_init(dev, CHV_DP_D, PORT_D);
22f35042
VS
14858 if (I915_READ(CHV_HDMID) & SDVO_DETECTED || has_port)
14859 intel_hdmi_init(dev, CHV_HDMID, PORT_D);
9418c1f1
VS
14860 }
14861
3cfca973 14862 intel_dsi_init(dev);
09da55dc 14863 } else if (!IS_GEN2(dev) && !IS_PINEVIEW(dev)) {
27185ae1 14864 bool found = false;
7d57382e 14865
e2debe91 14866 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
b01f2c3a 14867 DRM_DEBUG_KMS("probing SDVOB\n");
2a5c0832 14868 found = intel_sdvo_init(dev, GEN3_SDVOB, PORT_B);
3fec3d2f 14869 if (!found && IS_G4X(dev)) {
b01f2c3a 14870 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
e2debe91 14871 intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
b01f2c3a 14872 }
27185ae1 14873
3fec3d2f 14874 if (!found && IS_G4X(dev))
ab9d7c30 14875 intel_dp_init(dev, DP_B, PORT_B);
725e30ad 14876 }
13520b05
KH
14877
14878 /* Before G4X SDVOC doesn't have its own detect register */
13520b05 14879
e2debe91 14880 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
b01f2c3a 14881 DRM_DEBUG_KMS("probing SDVOC\n");
2a5c0832 14882 found = intel_sdvo_init(dev, GEN3_SDVOC, PORT_C);
b01f2c3a 14883 }
27185ae1 14884
e2debe91 14885 if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
27185ae1 14886
3fec3d2f 14887 if (IS_G4X(dev)) {
b01f2c3a 14888 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
e2debe91 14889 intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
b01f2c3a 14890 }
3fec3d2f 14891 if (IS_G4X(dev))
ab9d7c30 14892 intel_dp_init(dev, DP_C, PORT_C);
725e30ad 14893 }
27185ae1 14894
3fec3d2f 14895 if (IS_G4X(dev) &&
e7281eab 14896 (I915_READ(DP_D) & DP_DETECTED))
ab9d7c30 14897 intel_dp_init(dev, DP_D, PORT_D);
bad720ff 14898 } else if (IS_GEN2(dev))
79e53945
JB
14899 intel_dvo_init(dev);
14900
103a196f 14901 if (SUPPORTS_TV(dev))
79e53945
JB
14902 intel_tv_init(dev);
14903
0bc12bcb 14904 intel_psr_init(dev);
7c8f8a70 14905
b2784e15 14906 for_each_intel_encoder(dev, encoder) {
4ef69c7a
CW
14907 encoder->base.possible_crtcs = encoder->crtc_mask;
14908 encoder->base.possible_clones =
66a9278e 14909 intel_encoder_clones(encoder);
79e53945 14910 }
47356eb6 14911
dde86e2d 14912 intel_init_pch_refclk(dev);
270b3042
DV
14913
14914 drm_helper_move_panel_connectors_to_head(dev);
79e53945
JB
14915}
14916
14917static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
14918{
60a5ca01 14919 struct drm_device *dev = fb->dev;
79e53945 14920 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
79e53945 14921
ef2d633e 14922 drm_framebuffer_cleanup(fb);
60a5ca01 14923 mutex_lock(&dev->struct_mutex);
ef2d633e 14924 WARN_ON(!intel_fb->obj->framebuffer_references--);
60a5ca01
VS
14925 drm_gem_object_unreference(&intel_fb->obj->base);
14926 mutex_unlock(&dev->struct_mutex);
79e53945
JB
14927 kfree(intel_fb);
14928}
14929
14930static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
05394f39 14931 struct drm_file *file,
79e53945
JB
14932 unsigned int *handle)
14933{
14934 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
05394f39 14935 struct drm_i915_gem_object *obj = intel_fb->obj;
79e53945 14936
cc917ab4
CW
14937 if (obj->userptr.mm) {
14938 DRM_DEBUG("attempting to use a userptr for a framebuffer, denied\n");
14939 return -EINVAL;
14940 }
14941
05394f39 14942 return drm_gem_handle_create(file, &obj->base, handle);
79e53945
JB
14943}
14944
86c98588
RV
14945static int intel_user_framebuffer_dirty(struct drm_framebuffer *fb,
14946 struct drm_file *file,
14947 unsigned flags, unsigned color,
14948 struct drm_clip_rect *clips,
14949 unsigned num_clips)
14950{
14951 struct drm_device *dev = fb->dev;
14952 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
14953 struct drm_i915_gem_object *obj = intel_fb->obj;
14954
14955 mutex_lock(&dev->struct_mutex);
74b4ea1e 14956 intel_fb_obj_flush(obj, false, ORIGIN_DIRTYFB);
86c98588
RV
14957 mutex_unlock(&dev->struct_mutex);
14958
14959 return 0;
14960}
14961
79e53945
JB
14962static const struct drm_framebuffer_funcs intel_fb_funcs = {
14963 .destroy = intel_user_framebuffer_destroy,
14964 .create_handle = intel_user_framebuffer_create_handle,
86c98588 14965 .dirty = intel_user_framebuffer_dirty,
79e53945
JB
14966};
14967
b321803d
DL
14968static
14969u32 intel_fb_pitch_limit(struct drm_device *dev, uint64_t fb_modifier,
14970 uint32_t pixel_format)
14971{
14972 u32 gen = INTEL_INFO(dev)->gen;
14973
14974 if (gen >= 9) {
ac484963
VS
14975 int cpp = drm_format_plane_cpp(pixel_format, 0);
14976
b321803d
DL
14977 /* "The stride in bytes must not exceed the of the size of 8K
14978 * pixels and 32K bytes."
14979 */
ac484963 14980 return min(8192 * cpp, 32768);
666a4537 14981 } else if (gen >= 5 && !IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
b321803d
DL
14982 return 32*1024;
14983 } else if (gen >= 4) {
14984 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14985 return 16*1024;
14986 else
14987 return 32*1024;
14988 } else if (gen >= 3) {
14989 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14990 return 8*1024;
14991 else
14992 return 16*1024;
14993 } else {
14994 /* XXX DSPC is limited to 4k tiled */
14995 return 8*1024;
14996 }
14997}
14998
b5ea642a
DV
14999static int intel_framebuffer_init(struct drm_device *dev,
15000 struct intel_framebuffer *intel_fb,
15001 struct drm_mode_fb_cmd2 *mode_cmd,
15002 struct drm_i915_gem_object *obj)
79e53945 15003{
7b49f948 15004 struct drm_i915_private *dev_priv = to_i915(dev);
6761dd31 15005 unsigned int aligned_height;
79e53945 15006 int ret;
b321803d 15007 u32 pitch_limit, stride_alignment;
79e53945 15008
dd4916c5
DV
15009 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
15010
2a80eada
DV
15011 if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
15012 /* Enforce that fb modifier and tiling mode match, but only for
15013 * X-tiled. This is needed for FBC. */
15014 if (!!(obj->tiling_mode == I915_TILING_X) !=
15015 !!(mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED)) {
15016 DRM_DEBUG("tiling_mode doesn't match fb modifier\n");
15017 return -EINVAL;
15018 }
15019 } else {
15020 if (obj->tiling_mode == I915_TILING_X)
15021 mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
15022 else if (obj->tiling_mode == I915_TILING_Y) {
15023 DRM_DEBUG("No Y tiling for legacy addfb\n");
15024 return -EINVAL;
15025 }
15026 }
15027
9a8f0a12
TU
15028 /* Passed in modifier sanity checking. */
15029 switch (mode_cmd->modifier[0]) {
15030 case I915_FORMAT_MOD_Y_TILED:
15031 case I915_FORMAT_MOD_Yf_TILED:
15032 if (INTEL_INFO(dev)->gen < 9) {
15033 DRM_DEBUG("Unsupported tiling 0x%llx!\n",
15034 mode_cmd->modifier[0]);
15035 return -EINVAL;
15036 }
15037 case DRM_FORMAT_MOD_NONE:
15038 case I915_FORMAT_MOD_X_TILED:
15039 break;
15040 default:
c0f40428
JB
15041 DRM_DEBUG("Unsupported fb modifier 0x%llx!\n",
15042 mode_cmd->modifier[0]);
57cd6508 15043 return -EINVAL;
c16ed4be 15044 }
57cd6508 15045
7b49f948
VS
15046 stride_alignment = intel_fb_stride_alignment(dev_priv,
15047 mode_cmd->modifier[0],
b321803d
DL
15048 mode_cmd->pixel_format);
15049 if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
15050 DRM_DEBUG("pitch (%d) must be at least %u byte aligned\n",
15051 mode_cmd->pitches[0], stride_alignment);
57cd6508 15052 return -EINVAL;
c16ed4be 15053 }
57cd6508 15054
b321803d
DL
15055 pitch_limit = intel_fb_pitch_limit(dev, mode_cmd->modifier[0],
15056 mode_cmd->pixel_format);
a35cdaa0 15057 if (mode_cmd->pitches[0] > pitch_limit) {
b321803d
DL
15058 DRM_DEBUG("%s pitch (%u) must be at less than %d\n",
15059 mode_cmd->modifier[0] != DRM_FORMAT_MOD_NONE ?
2a80eada 15060 "tiled" : "linear",
a35cdaa0 15061 mode_cmd->pitches[0], pitch_limit);
5d7bd705 15062 return -EINVAL;
c16ed4be 15063 }
5d7bd705 15064
2a80eada 15065 if (mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED &&
c16ed4be
CW
15066 mode_cmd->pitches[0] != obj->stride) {
15067 DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
15068 mode_cmd->pitches[0], obj->stride);
5d7bd705 15069 return -EINVAL;
c16ed4be 15070 }
5d7bd705 15071
57779d06 15072 /* Reject formats not supported by any plane early. */
308e5bcb 15073 switch (mode_cmd->pixel_format) {
57779d06 15074 case DRM_FORMAT_C8:
04b3924d
VS
15075 case DRM_FORMAT_RGB565:
15076 case DRM_FORMAT_XRGB8888:
15077 case DRM_FORMAT_ARGB8888:
57779d06
VS
15078 break;
15079 case DRM_FORMAT_XRGB1555:
c16ed4be 15080 if (INTEL_INFO(dev)->gen > 3) {
4ee62c76
VS
15081 DRM_DEBUG("unsupported pixel format: %s\n",
15082 drm_get_format_name(mode_cmd->pixel_format));
57779d06 15083 return -EINVAL;
c16ed4be 15084 }
57779d06 15085 break;
57779d06 15086 case DRM_FORMAT_ABGR8888:
666a4537
WB
15087 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev) &&
15088 INTEL_INFO(dev)->gen < 9) {
6c0fd451
DL
15089 DRM_DEBUG("unsupported pixel format: %s\n",
15090 drm_get_format_name(mode_cmd->pixel_format));
15091 return -EINVAL;
15092 }
15093 break;
15094 case DRM_FORMAT_XBGR8888:
04b3924d 15095 case DRM_FORMAT_XRGB2101010:
57779d06 15096 case DRM_FORMAT_XBGR2101010:
c16ed4be 15097 if (INTEL_INFO(dev)->gen < 4) {
4ee62c76
VS
15098 DRM_DEBUG("unsupported pixel format: %s\n",
15099 drm_get_format_name(mode_cmd->pixel_format));
57779d06 15100 return -EINVAL;
c16ed4be 15101 }
b5626747 15102 break;
7531208b 15103 case DRM_FORMAT_ABGR2101010:
666a4537 15104 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
7531208b
DL
15105 DRM_DEBUG("unsupported pixel format: %s\n",
15106 drm_get_format_name(mode_cmd->pixel_format));
15107 return -EINVAL;
15108 }
15109 break;
04b3924d
VS
15110 case DRM_FORMAT_YUYV:
15111 case DRM_FORMAT_UYVY:
15112 case DRM_FORMAT_YVYU:
15113 case DRM_FORMAT_VYUY:
c16ed4be 15114 if (INTEL_INFO(dev)->gen < 5) {
4ee62c76
VS
15115 DRM_DEBUG("unsupported pixel format: %s\n",
15116 drm_get_format_name(mode_cmd->pixel_format));
57779d06 15117 return -EINVAL;
c16ed4be 15118 }
57cd6508
CW
15119 break;
15120 default:
4ee62c76
VS
15121 DRM_DEBUG("unsupported pixel format: %s\n",
15122 drm_get_format_name(mode_cmd->pixel_format));
57cd6508
CW
15123 return -EINVAL;
15124 }
15125
90f9a336
VS
15126 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
15127 if (mode_cmd->offsets[0] != 0)
15128 return -EINVAL;
15129
ec2c981e 15130 aligned_height = intel_fb_align_height(dev, mode_cmd->height,
091df6cb
DV
15131 mode_cmd->pixel_format,
15132 mode_cmd->modifier[0]);
53155c0a
DV
15133 /* FIXME drm helper for size checks (especially planar formats)? */
15134 if (obj->base.size < aligned_height * mode_cmd->pitches[0])
15135 return -EINVAL;
15136
c7d73f6a
DV
15137 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
15138 intel_fb->obj = obj;
15139
2d7a215f
VS
15140 intel_fill_fb_info(dev_priv, &intel_fb->base);
15141
79e53945
JB
15142 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
15143 if (ret) {
15144 DRM_ERROR("framebuffer init failed %d\n", ret);
15145 return ret;
15146 }
15147
0b05e1e0
VS
15148 intel_fb->obj->framebuffer_references++;
15149
79e53945
JB
15150 return 0;
15151}
15152
79e53945
JB
15153static struct drm_framebuffer *
15154intel_user_framebuffer_create(struct drm_device *dev,
15155 struct drm_file *filp,
1eb83451 15156 const struct drm_mode_fb_cmd2 *user_mode_cmd)
79e53945 15157{
dcb1394e 15158 struct drm_framebuffer *fb;
05394f39 15159 struct drm_i915_gem_object *obj;
76dc3769 15160 struct drm_mode_fb_cmd2 mode_cmd = *user_mode_cmd;
79e53945 15161
a8ad0bd8 15162 obj = to_intel_bo(drm_gem_object_lookup(filp, mode_cmd.handles[0]));
c8725226 15163 if (&obj->base == NULL)
cce13ff7 15164 return ERR_PTR(-ENOENT);
79e53945 15165
92907cbb 15166 fb = intel_framebuffer_create(dev, &mode_cmd, obj);
dcb1394e
LW
15167 if (IS_ERR(fb))
15168 drm_gem_object_unreference_unlocked(&obj->base);
15169
15170 return fb;
79e53945
JB
15171}
15172
0695726e 15173#ifndef CONFIG_DRM_FBDEV_EMULATION
0632fef6 15174static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
4520f53a
DV
15175{
15176}
15177#endif
15178
79e53945 15179static const struct drm_mode_config_funcs intel_mode_funcs = {
79e53945 15180 .fb_create = intel_user_framebuffer_create,
0632fef6 15181 .output_poll_changed = intel_fbdev_output_poll_changed,
5ee67f1c
MR
15182 .atomic_check = intel_atomic_check,
15183 .atomic_commit = intel_atomic_commit,
de419ab6
ML
15184 .atomic_state_alloc = intel_atomic_state_alloc,
15185 .atomic_state_clear = intel_atomic_state_clear,
79e53945
JB
15186};
15187
88212941
ID
15188/**
15189 * intel_init_display_hooks - initialize the display modesetting hooks
15190 * @dev_priv: device private
15191 */
15192void intel_init_display_hooks(struct drm_i915_private *dev_priv)
e70236a8 15193{
88212941 15194 if (INTEL_INFO(dev_priv)->gen >= 9) {
bc8d7dff 15195 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
5724dbd1
DL
15196 dev_priv->display.get_initial_plane_config =
15197 skylake_get_initial_plane_config;
bc8d7dff
DL
15198 dev_priv->display.crtc_compute_clock =
15199 haswell_crtc_compute_clock;
15200 dev_priv->display.crtc_enable = haswell_crtc_enable;
15201 dev_priv->display.crtc_disable = haswell_crtc_disable;
88212941 15202 } else if (HAS_DDI(dev_priv)) {
0e8ffe1b 15203 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
5724dbd1
DL
15204 dev_priv->display.get_initial_plane_config =
15205 ironlake_get_initial_plane_config;
797d0259
ACO
15206 dev_priv->display.crtc_compute_clock =
15207 haswell_crtc_compute_clock;
4f771f10
PZ
15208 dev_priv->display.crtc_enable = haswell_crtc_enable;
15209 dev_priv->display.crtc_disable = haswell_crtc_disable;
88212941 15210 } else if (HAS_PCH_SPLIT(dev_priv)) {
0e8ffe1b 15211 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
5724dbd1
DL
15212 dev_priv->display.get_initial_plane_config =
15213 ironlake_get_initial_plane_config;
3fb37703
ACO
15214 dev_priv->display.crtc_compute_clock =
15215 ironlake_crtc_compute_clock;
76e5a89c
DV
15216 dev_priv->display.crtc_enable = ironlake_crtc_enable;
15217 dev_priv->display.crtc_disable = ironlake_crtc_disable;
65b3d6a9 15218 } else if (IS_CHERRYVIEW(dev_priv)) {
89b667f8 15219 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
5724dbd1
DL
15220 dev_priv->display.get_initial_plane_config =
15221 i9xx_get_initial_plane_config;
65b3d6a9
ACO
15222 dev_priv->display.crtc_compute_clock = chv_crtc_compute_clock;
15223 dev_priv->display.crtc_enable = valleyview_crtc_enable;
15224 dev_priv->display.crtc_disable = i9xx_crtc_disable;
15225 } else if (IS_VALLEYVIEW(dev_priv)) {
15226 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
15227 dev_priv->display.get_initial_plane_config =
15228 i9xx_get_initial_plane_config;
15229 dev_priv->display.crtc_compute_clock = vlv_crtc_compute_clock;
89b667f8
JB
15230 dev_priv->display.crtc_enable = valleyview_crtc_enable;
15231 dev_priv->display.crtc_disable = i9xx_crtc_disable;
19ec6693
ACO
15232 } else if (IS_G4X(dev_priv)) {
15233 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
15234 dev_priv->display.get_initial_plane_config =
15235 i9xx_get_initial_plane_config;
15236 dev_priv->display.crtc_compute_clock = g4x_crtc_compute_clock;
15237 dev_priv->display.crtc_enable = i9xx_crtc_enable;
15238 dev_priv->display.crtc_disable = i9xx_crtc_disable;
70e8aa21
ACO
15239 } else if (IS_PINEVIEW(dev_priv)) {
15240 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
15241 dev_priv->display.get_initial_plane_config =
15242 i9xx_get_initial_plane_config;
15243 dev_priv->display.crtc_compute_clock = pnv_crtc_compute_clock;
15244 dev_priv->display.crtc_enable = i9xx_crtc_enable;
15245 dev_priv->display.crtc_disable = i9xx_crtc_disable;
81c97f52 15246 } else if (!IS_GEN2(dev_priv)) {
0e8ffe1b 15247 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
5724dbd1
DL
15248 dev_priv->display.get_initial_plane_config =
15249 i9xx_get_initial_plane_config;
d6dfee7a 15250 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
76e5a89c
DV
15251 dev_priv->display.crtc_enable = i9xx_crtc_enable;
15252 dev_priv->display.crtc_disable = i9xx_crtc_disable;
81c97f52
ACO
15253 } else {
15254 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
15255 dev_priv->display.get_initial_plane_config =
15256 i9xx_get_initial_plane_config;
15257 dev_priv->display.crtc_compute_clock = i8xx_crtc_compute_clock;
15258 dev_priv->display.crtc_enable = i9xx_crtc_enable;
15259 dev_priv->display.crtc_disable = i9xx_crtc_disable;
f564048e 15260 }
e70236a8 15261
e70236a8 15262 /* Returns the core display clock speed */
88212941 15263 if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv))
1652d19e
VS
15264 dev_priv->display.get_display_clock_speed =
15265 skylake_get_display_clock_speed;
88212941 15266 else if (IS_BROXTON(dev_priv))
acd3f3d3
BP
15267 dev_priv->display.get_display_clock_speed =
15268 broxton_get_display_clock_speed;
88212941 15269 else if (IS_BROADWELL(dev_priv))
1652d19e
VS
15270 dev_priv->display.get_display_clock_speed =
15271 broadwell_get_display_clock_speed;
88212941 15272 else if (IS_HASWELL(dev_priv))
1652d19e
VS
15273 dev_priv->display.get_display_clock_speed =
15274 haswell_get_display_clock_speed;
88212941 15275 else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
25eb05fc
JB
15276 dev_priv->display.get_display_clock_speed =
15277 valleyview_get_display_clock_speed;
88212941 15278 else if (IS_GEN5(dev_priv))
b37a6434
VS
15279 dev_priv->display.get_display_clock_speed =
15280 ilk_get_display_clock_speed;
88212941
ID
15281 else if (IS_I945G(dev_priv) || IS_BROADWATER(dev_priv) ||
15282 IS_GEN6(dev_priv) || IS_IVYBRIDGE(dev_priv))
e70236a8
JB
15283 dev_priv->display.get_display_clock_speed =
15284 i945_get_display_clock_speed;
88212941 15285 else if (IS_GM45(dev_priv))
34edce2f
VS
15286 dev_priv->display.get_display_clock_speed =
15287 gm45_get_display_clock_speed;
88212941 15288 else if (IS_CRESTLINE(dev_priv))
34edce2f
VS
15289 dev_priv->display.get_display_clock_speed =
15290 i965gm_get_display_clock_speed;
88212941 15291 else if (IS_PINEVIEW(dev_priv))
34edce2f
VS
15292 dev_priv->display.get_display_clock_speed =
15293 pnv_get_display_clock_speed;
88212941 15294 else if (IS_G33(dev_priv) || IS_G4X(dev_priv))
34edce2f
VS
15295 dev_priv->display.get_display_clock_speed =
15296 g33_get_display_clock_speed;
88212941 15297 else if (IS_I915G(dev_priv))
e70236a8
JB
15298 dev_priv->display.get_display_clock_speed =
15299 i915_get_display_clock_speed;
88212941 15300 else if (IS_I945GM(dev_priv) || IS_845G(dev_priv))
e70236a8
JB
15301 dev_priv->display.get_display_clock_speed =
15302 i9xx_misc_get_display_clock_speed;
88212941 15303 else if (IS_I915GM(dev_priv))
e70236a8
JB
15304 dev_priv->display.get_display_clock_speed =
15305 i915gm_get_display_clock_speed;
88212941 15306 else if (IS_I865G(dev_priv))
e70236a8
JB
15307 dev_priv->display.get_display_clock_speed =
15308 i865_get_display_clock_speed;
88212941 15309 else if (IS_I85X(dev_priv))
e70236a8 15310 dev_priv->display.get_display_clock_speed =
1b1d2716 15311 i85x_get_display_clock_speed;
623e01e5 15312 else { /* 830 */
88212941 15313 WARN(!IS_I830(dev_priv), "Unknown platform. Assuming 133 MHz CDCLK\n");
e70236a8
JB
15314 dev_priv->display.get_display_clock_speed =
15315 i830_get_display_clock_speed;
623e01e5 15316 }
e70236a8 15317
88212941 15318 if (IS_GEN5(dev_priv)) {
3bb11b53 15319 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
88212941 15320 } else if (IS_GEN6(dev_priv)) {
3bb11b53 15321 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
88212941 15322 } else if (IS_IVYBRIDGE(dev_priv)) {
3bb11b53
SJ
15323 /* FIXME: detect B0+ stepping and use auto training */
15324 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
88212941 15325 } else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
3bb11b53 15326 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
445e780b
VS
15327 }
15328
15329 if (IS_BROADWELL(dev_priv)) {
15330 dev_priv->display.modeset_commit_cdclk =
15331 broadwell_modeset_commit_cdclk;
15332 dev_priv->display.modeset_calc_cdclk =
15333 broadwell_modeset_calc_cdclk;
88212941 15334 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
27c329ed
ML
15335 dev_priv->display.modeset_commit_cdclk =
15336 valleyview_modeset_commit_cdclk;
15337 dev_priv->display.modeset_calc_cdclk =
15338 valleyview_modeset_calc_cdclk;
88212941 15339 } else if (IS_BROXTON(dev_priv)) {
27c329ed 15340 dev_priv->display.modeset_commit_cdclk =
324513c0 15341 bxt_modeset_commit_cdclk;
27c329ed 15342 dev_priv->display.modeset_calc_cdclk =
324513c0 15343 bxt_modeset_calc_cdclk;
c89e39f3
CT
15344 } else if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv)) {
15345 dev_priv->display.modeset_commit_cdclk =
15346 skl_modeset_commit_cdclk;
15347 dev_priv->display.modeset_calc_cdclk =
15348 skl_modeset_calc_cdclk;
e70236a8 15349 }
5a21b665
DV
15350
15351 switch (INTEL_INFO(dev_priv)->gen) {
15352 case 2:
15353 dev_priv->display.queue_flip = intel_gen2_queue_flip;
15354 break;
15355
15356 case 3:
15357 dev_priv->display.queue_flip = intel_gen3_queue_flip;
15358 break;
15359
15360 case 4:
15361 case 5:
15362 dev_priv->display.queue_flip = intel_gen4_queue_flip;
15363 break;
15364
15365 case 6:
15366 dev_priv->display.queue_flip = intel_gen6_queue_flip;
15367 break;
15368 case 7:
15369 case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
15370 dev_priv->display.queue_flip = intel_gen7_queue_flip;
15371 break;
15372 case 9:
15373 /* Drop through - unsupported since execlist only. */
15374 default:
15375 /* Default just returns -ENODEV to indicate unsupported */
15376 dev_priv->display.queue_flip = intel_default_queue_flip;
15377 }
e70236a8
JB
15378}
15379
b690e96c
JB
15380/*
15381 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
15382 * resume, or other times. This quirk makes sure that's the case for
15383 * affected systems.
15384 */
0206e353 15385static void quirk_pipea_force(struct drm_device *dev)
b690e96c
JB
15386{
15387 struct drm_i915_private *dev_priv = dev->dev_private;
15388
15389 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
bc0daf48 15390 DRM_INFO("applying pipe a force quirk\n");
b690e96c
JB
15391}
15392
b6b5d049
VS
15393static void quirk_pipeb_force(struct drm_device *dev)
15394{
15395 struct drm_i915_private *dev_priv = dev->dev_private;
15396
15397 dev_priv->quirks |= QUIRK_PIPEB_FORCE;
15398 DRM_INFO("applying pipe b force quirk\n");
15399}
15400
435793df
KP
15401/*
15402 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
15403 */
15404static void quirk_ssc_force_disable(struct drm_device *dev)
15405{
15406 struct drm_i915_private *dev_priv = dev->dev_private;
15407 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
bc0daf48 15408 DRM_INFO("applying lvds SSC disable quirk\n");
435793df
KP
15409}
15410
4dca20ef 15411/*
5a15ab5b
CE
15412 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
15413 * brightness value
4dca20ef
CE
15414 */
15415static void quirk_invert_brightness(struct drm_device *dev)
15416{
15417 struct drm_i915_private *dev_priv = dev->dev_private;
15418 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
bc0daf48 15419 DRM_INFO("applying inverted panel brightness quirk\n");
435793df
KP
15420}
15421
9c72cc6f
SD
15422/* Some VBT's incorrectly indicate no backlight is present */
15423static void quirk_backlight_present(struct drm_device *dev)
15424{
15425 struct drm_i915_private *dev_priv = dev->dev_private;
15426 dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
15427 DRM_INFO("applying backlight present quirk\n");
15428}
15429
b690e96c
JB
15430struct intel_quirk {
15431 int device;
15432 int subsystem_vendor;
15433 int subsystem_device;
15434 void (*hook)(struct drm_device *dev);
15435};
15436
5f85f176
EE
15437/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
15438struct intel_dmi_quirk {
15439 void (*hook)(struct drm_device *dev);
15440 const struct dmi_system_id (*dmi_id_list)[];
15441};
15442
15443static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
15444{
15445 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
15446 return 1;
15447}
15448
15449static const struct intel_dmi_quirk intel_dmi_quirks[] = {
15450 {
15451 .dmi_id_list = &(const struct dmi_system_id[]) {
15452 {
15453 .callback = intel_dmi_reverse_brightness,
15454 .ident = "NCR Corporation",
15455 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
15456 DMI_MATCH(DMI_PRODUCT_NAME, ""),
15457 },
15458 },
15459 { } /* terminating entry */
15460 },
15461 .hook = quirk_invert_brightness,
15462 },
15463};
15464
c43b5634 15465static struct intel_quirk intel_quirks[] = {
b690e96c
JB
15466 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
15467 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
15468
b690e96c
JB
15469 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
15470 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
15471
5f080c0f
VS
15472 /* 830 needs to leave pipe A & dpll A up */
15473 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
15474
b6b5d049
VS
15475 /* 830 needs to leave pipe B & dpll B up */
15476 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
15477
435793df
KP
15478 /* Lenovo U160 cannot use SSC on LVDS */
15479 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
070d329a
MAS
15480
15481 /* Sony Vaio Y cannot use SSC on LVDS */
15482 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
5a15ab5b 15483
be505f64
AH
15484 /* Acer Aspire 5734Z must invert backlight brightness */
15485 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
15486
15487 /* Acer/eMachines G725 */
15488 { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
15489
15490 /* Acer/eMachines e725 */
15491 { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
15492
15493 /* Acer/Packard Bell NCL20 */
15494 { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
15495
15496 /* Acer Aspire 4736Z */
15497 { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
0f540c3a
JN
15498
15499 /* Acer Aspire 5336 */
15500 { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
2e93a1aa
SD
15501
15502 /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
15503 { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
d4967d8c 15504
dfb3d47b
SD
15505 /* Acer C720 Chromebook (Core i3 4005U) */
15506 { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
15507
b2a9601c 15508 /* Apple Macbook 2,1 (Core 2 T7400) */
15509 { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
15510
1b9448b0
JN
15511 /* Apple Macbook 4,1 */
15512 { 0x2a02, 0x106b, 0x00a1, quirk_backlight_present },
15513
d4967d8c
SD
15514 /* Toshiba CB35 Chromebook (Celeron 2955U) */
15515 { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
724cb06f
SD
15516
15517 /* HP Chromebook 14 (Celeron 2955U) */
15518 { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
cf6f0af9
JN
15519
15520 /* Dell Chromebook 11 */
15521 { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
9be64eee
JN
15522
15523 /* Dell Chromebook 11 (2015 version) */
15524 { 0x0a16, 0x1028, 0x0a35, quirk_backlight_present },
b690e96c
JB
15525};
15526
15527static void intel_init_quirks(struct drm_device *dev)
15528{
15529 struct pci_dev *d = dev->pdev;
15530 int i;
15531
15532 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
15533 struct intel_quirk *q = &intel_quirks[i];
15534
15535 if (d->device == q->device &&
15536 (d->subsystem_vendor == q->subsystem_vendor ||
15537 q->subsystem_vendor == PCI_ANY_ID) &&
15538 (d->subsystem_device == q->subsystem_device ||
15539 q->subsystem_device == PCI_ANY_ID))
15540 q->hook(dev);
15541 }
5f85f176
EE
15542 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
15543 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
15544 intel_dmi_quirks[i].hook(dev);
15545 }
b690e96c
JB
15546}
15547
9cce37f4
JB
15548/* Disable the VGA plane that we never use */
15549static void i915_disable_vga(struct drm_device *dev)
15550{
15551 struct drm_i915_private *dev_priv = dev->dev_private;
15552 u8 sr1;
f0f59a00 15553 i915_reg_t vga_reg = i915_vgacntrl_reg(dev);
9cce37f4 15554
2b37c616 15555 /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
9cce37f4 15556 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
3fdcf431 15557 outb(SR01, VGA_SR_INDEX);
9cce37f4
JB
15558 sr1 = inb(VGA_SR_DATA);
15559 outb(sr1 | 1<<5, VGA_SR_DATA);
15560 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
15561 udelay(300);
15562
01f5a626 15563 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
9cce37f4
JB
15564 POSTING_READ(vga_reg);
15565}
15566
f817586c
DV
15567void intel_modeset_init_hw(struct drm_device *dev)
15568{
1a617b77
ML
15569 struct drm_i915_private *dev_priv = dev->dev_private;
15570
b6283055 15571 intel_update_cdclk(dev);
1a617b77
ML
15572
15573 dev_priv->atomic_cdclk_freq = dev_priv->cdclk_freq;
15574
f817586c 15575 intel_init_clock_gating(dev);
dc97997a 15576 intel_enable_gt_powersave(dev_priv);
f817586c
DV
15577}
15578
d93c0372
MR
15579/*
15580 * Calculate what we think the watermarks should be for the state we've read
15581 * out of the hardware and then immediately program those watermarks so that
15582 * we ensure the hardware settings match our internal state.
15583 *
15584 * We can calculate what we think WM's should be by creating a duplicate of the
15585 * current state (which was constructed during hardware readout) and running it
15586 * through the atomic check code to calculate new watermark values in the
15587 * state object.
15588 */
15589static void sanitize_watermarks(struct drm_device *dev)
15590{
15591 struct drm_i915_private *dev_priv = to_i915(dev);
15592 struct drm_atomic_state *state;
15593 struct drm_crtc *crtc;
15594 struct drm_crtc_state *cstate;
15595 struct drm_modeset_acquire_ctx ctx;
15596 int ret;
15597 int i;
15598
15599 /* Only supported on platforms that use atomic watermark design */
ed4a6a7c 15600 if (!dev_priv->display.optimize_watermarks)
d93c0372
MR
15601 return;
15602
15603 /*
15604 * We need to hold connection_mutex before calling duplicate_state so
15605 * that the connector loop is protected.
15606 */
15607 drm_modeset_acquire_init(&ctx, 0);
15608retry:
0cd1262d 15609 ret = drm_modeset_lock_all_ctx(dev, &ctx);
d93c0372
MR
15610 if (ret == -EDEADLK) {
15611 drm_modeset_backoff(&ctx);
15612 goto retry;
15613 } else if (WARN_ON(ret)) {
0cd1262d 15614 goto fail;
d93c0372
MR
15615 }
15616
15617 state = drm_atomic_helper_duplicate_state(dev, &ctx);
15618 if (WARN_ON(IS_ERR(state)))
0cd1262d 15619 goto fail;
d93c0372 15620
ed4a6a7c
MR
15621 /*
15622 * Hardware readout is the only time we don't want to calculate
15623 * intermediate watermarks (since we don't trust the current
15624 * watermarks).
15625 */
15626 to_intel_atomic_state(state)->skip_intermediate_wm = true;
15627
d93c0372
MR
15628 ret = intel_atomic_check(dev, state);
15629 if (ret) {
15630 /*
15631 * If we fail here, it means that the hardware appears to be
15632 * programmed in a way that shouldn't be possible, given our
15633 * understanding of watermark requirements. This might mean a
15634 * mistake in the hardware readout code or a mistake in the
15635 * watermark calculations for a given platform. Raise a WARN
15636 * so that this is noticeable.
15637 *
15638 * If this actually happens, we'll have to just leave the
15639 * BIOS-programmed watermarks untouched and hope for the best.
15640 */
15641 WARN(true, "Could not determine valid watermarks for inherited state\n");
0cd1262d 15642 goto fail;
d93c0372
MR
15643 }
15644
15645 /* Write calculated watermark values back */
d93c0372
MR
15646 for_each_crtc_in_state(state, crtc, cstate, i) {
15647 struct intel_crtc_state *cs = to_intel_crtc_state(cstate);
15648
ed4a6a7c
MR
15649 cs->wm.need_postvbl_update = true;
15650 dev_priv->display.optimize_watermarks(cs);
d93c0372
MR
15651 }
15652
15653 drm_atomic_state_free(state);
0cd1262d 15654fail:
d93c0372
MR
15655 drm_modeset_drop_locks(&ctx);
15656 drm_modeset_acquire_fini(&ctx);
15657}
15658
79e53945
JB
15659void intel_modeset_init(struct drm_device *dev)
15660{
72e96d64
JL
15661 struct drm_i915_private *dev_priv = to_i915(dev);
15662 struct i915_ggtt *ggtt = &dev_priv->ggtt;
1fe47785 15663 int sprite, ret;
8cc87b75 15664 enum pipe pipe;
46f297fb 15665 struct intel_crtc *crtc;
79e53945
JB
15666
15667 drm_mode_config_init(dev);
15668
15669 dev->mode_config.min_width = 0;
15670 dev->mode_config.min_height = 0;
15671
019d96cb
DA
15672 dev->mode_config.preferred_depth = 24;
15673 dev->mode_config.prefer_shadow = 1;
15674
25bab385
TU
15675 dev->mode_config.allow_fb_modifiers = true;
15676
e6ecefaa 15677 dev->mode_config.funcs = &intel_mode_funcs;
79e53945 15678
b690e96c
JB
15679 intel_init_quirks(dev);
15680
1fa61106
ED
15681 intel_init_pm(dev);
15682
e3c74757
BW
15683 if (INTEL_INFO(dev)->num_pipes == 0)
15684 return;
15685
69f92f67
LW
15686 /*
15687 * There may be no VBT; and if the BIOS enabled SSC we can
15688 * just keep using it to avoid unnecessary flicker. Whereas if the
15689 * BIOS isn't using it, don't assume it will work even if the VBT
15690 * indicates as much.
15691 */
15692 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)) {
15693 bool bios_lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
15694 DREF_SSC1_ENABLE);
15695
15696 if (dev_priv->vbt.lvds_use_ssc != bios_lvds_use_ssc) {
15697 DRM_DEBUG_KMS("SSC %sabled by BIOS, overriding VBT which says %sabled\n",
15698 bios_lvds_use_ssc ? "en" : "dis",
15699 dev_priv->vbt.lvds_use_ssc ? "en" : "dis");
15700 dev_priv->vbt.lvds_use_ssc = bios_lvds_use_ssc;
15701 }
15702 }
15703
a6c45cf0
CW
15704 if (IS_GEN2(dev)) {
15705 dev->mode_config.max_width = 2048;
15706 dev->mode_config.max_height = 2048;
15707 } else if (IS_GEN3(dev)) {
5e4d6fa7
KP
15708 dev->mode_config.max_width = 4096;
15709 dev->mode_config.max_height = 4096;
79e53945 15710 } else {
a6c45cf0
CW
15711 dev->mode_config.max_width = 8192;
15712 dev->mode_config.max_height = 8192;
79e53945 15713 }
068be561 15714
dc41c154
VS
15715 if (IS_845G(dev) || IS_I865G(dev)) {
15716 dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
15717 dev->mode_config.cursor_height = 1023;
15718 } else if (IS_GEN2(dev)) {
068be561
DL
15719 dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
15720 dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
15721 } else {
15722 dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
15723 dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
15724 }
15725
72e96d64 15726 dev->mode_config.fb_base = ggtt->mappable_base;
79e53945 15727
28c97730 15728 DRM_DEBUG_KMS("%d display pipe%s available.\n",
7eb552ae
BW
15729 INTEL_INFO(dev)->num_pipes,
15730 INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
79e53945 15731
055e393f 15732 for_each_pipe(dev_priv, pipe) {
8cc87b75 15733 intel_crtc_init(dev, pipe);
3bdcfc0c 15734 for_each_sprite(dev_priv, pipe, sprite) {
1fe47785 15735 ret = intel_plane_init(dev, pipe, sprite);
7f1f3851 15736 if (ret)
06da8da2 15737 DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
1fe47785 15738 pipe_name(pipe), sprite_name(pipe, sprite), ret);
7f1f3851 15739 }
79e53945
JB
15740 }
15741
bfa7df01
VS
15742 intel_update_czclk(dev_priv);
15743 intel_update_cdclk(dev);
15744
e72f9fbf 15745 intel_shared_dpll_init(dev);
ee7b9f93 15746
b2045352
VS
15747 if (dev_priv->max_cdclk_freq == 0)
15748 intel_update_max_cdclk(dev);
15749
9cce37f4
JB
15750 /* Just disable it once at startup */
15751 i915_disable_vga(dev);
79e53945 15752 intel_setup_outputs(dev);
11be49eb 15753
6e9f798d 15754 drm_modeset_lock_all(dev);
043e9bda 15755 intel_modeset_setup_hw_state(dev);
6e9f798d 15756 drm_modeset_unlock_all(dev);
46f297fb 15757
d3fcc808 15758 for_each_intel_crtc(dev, crtc) {
eeebeac5
ML
15759 struct intel_initial_plane_config plane_config = {};
15760
46f297fb
JB
15761 if (!crtc->active)
15762 continue;
15763
46f297fb 15764 /*
46f297fb
JB
15765 * Note that reserving the BIOS fb up front prevents us
15766 * from stuffing other stolen allocations like the ring
15767 * on top. This prevents some ugliness at boot time, and
15768 * can even allow for smooth boot transitions if the BIOS
15769 * fb is large enough for the active pipe configuration.
15770 */
eeebeac5
ML
15771 dev_priv->display.get_initial_plane_config(crtc,
15772 &plane_config);
15773
15774 /*
15775 * If the fb is shared between multiple heads, we'll
15776 * just get the first one.
15777 */
15778 intel_find_initial_plane_obj(crtc, &plane_config);
46f297fb 15779 }
d93c0372
MR
15780
15781 /*
15782 * Make sure hardware watermarks really match the state we read out.
15783 * Note that we need to do this after reconstructing the BIOS fb's
15784 * since the watermark calculation done here will use pstate->fb.
15785 */
15786 sanitize_watermarks(dev);
2c7111db
CW
15787}
15788
7fad798e
DV
15789static void intel_enable_pipe_a(struct drm_device *dev)
15790{
15791 struct intel_connector *connector;
15792 struct drm_connector *crt = NULL;
15793 struct intel_load_detect_pipe load_detect_temp;
208bf9fd 15794 struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
7fad798e
DV
15795
15796 /* We can't just switch on the pipe A, we need to set things up with a
15797 * proper mode and output configuration. As a gross hack, enable pipe A
15798 * by enabling the load detect pipe once. */
3a3371ff 15799 for_each_intel_connector(dev, connector) {
7fad798e
DV
15800 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
15801 crt = &connector->base;
15802 break;
15803 }
15804 }
15805
15806 if (!crt)
15807 return;
15808
208bf9fd 15809 if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
49172fee 15810 intel_release_load_detect_pipe(crt, &load_detect_temp, ctx);
7fad798e
DV
15811}
15812
fa555837
DV
15813static bool
15814intel_check_plane_mapping(struct intel_crtc *crtc)
15815{
7eb552ae
BW
15816 struct drm_device *dev = crtc->base.dev;
15817 struct drm_i915_private *dev_priv = dev->dev_private;
649636ef 15818 u32 val;
fa555837 15819
7eb552ae 15820 if (INTEL_INFO(dev)->num_pipes == 1)
fa555837
DV
15821 return true;
15822
649636ef 15823 val = I915_READ(DSPCNTR(!crtc->plane));
fa555837
DV
15824
15825 if ((val & DISPLAY_PLANE_ENABLE) &&
15826 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
15827 return false;
15828
15829 return true;
15830}
15831
02e93c35
VS
15832static bool intel_crtc_has_encoders(struct intel_crtc *crtc)
15833{
15834 struct drm_device *dev = crtc->base.dev;
15835 struct intel_encoder *encoder;
15836
15837 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
15838 return true;
15839
15840 return false;
15841}
15842
dd756198
VS
15843static bool intel_encoder_has_connectors(struct intel_encoder *encoder)
15844{
15845 struct drm_device *dev = encoder->base.dev;
15846 struct intel_connector *connector;
15847
15848 for_each_connector_on_encoder(dev, &encoder->base, connector)
15849 return true;
15850
15851 return false;
15852}
15853
24929352
DV
15854static void intel_sanitize_crtc(struct intel_crtc *crtc)
15855{
15856 struct drm_device *dev = crtc->base.dev;
15857 struct drm_i915_private *dev_priv = dev->dev_private;
4d1de975 15858 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
24929352 15859
24929352 15860 /* Clear any frame start delays used for debugging left by the BIOS */
4d1de975
JN
15861 if (!transcoder_is_dsi(cpu_transcoder)) {
15862 i915_reg_t reg = PIPECONF(cpu_transcoder);
15863
15864 I915_WRITE(reg,
15865 I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
15866 }
24929352 15867
d3eaf884 15868 /* restore vblank interrupts to correct state */
9625604c 15869 drm_crtc_vblank_reset(&crtc->base);
d297e103 15870 if (crtc->active) {
f9cd7b88
VS
15871 struct intel_plane *plane;
15872
9625604c 15873 drm_crtc_vblank_on(&crtc->base);
f9cd7b88
VS
15874
15875 /* Disable everything but the primary plane */
15876 for_each_intel_plane_on_crtc(dev, crtc, plane) {
15877 if (plane->base.type == DRM_PLANE_TYPE_PRIMARY)
15878 continue;
15879
15880 plane->disable_plane(&plane->base, &crtc->base);
15881 }
9625604c 15882 }
d3eaf884 15883
24929352 15884 /* We need to sanitize the plane -> pipe mapping first because this will
fa555837
DV
15885 * disable the crtc (and hence change the state) if it is wrong. Note
15886 * that gen4+ has a fixed plane -> pipe mapping. */
15887 if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
24929352
DV
15888 bool plane;
15889
78108b7c
VS
15890 DRM_DEBUG_KMS("[CRTC:%d:%s] wrong plane connection detected!\n",
15891 crtc->base.base.id, crtc->base.name);
24929352
DV
15892
15893 /* Pipe has the wrong plane attached and the plane is active.
15894 * Temporarily change the plane mapping and disable everything
15895 * ... */
15896 plane = crtc->plane;
b70709a6 15897 to_intel_plane_state(crtc->base.primary->state)->visible = true;
24929352 15898 crtc->plane = !plane;
b17d48e2 15899 intel_crtc_disable_noatomic(&crtc->base);
24929352 15900 crtc->plane = plane;
24929352 15901 }
24929352 15902
7fad798e
DV
15903 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
15904 crtc->pipe == PIPE_A && !crtc->active) {
15905 /* BIOS forgot to enable pipe A, this mostly happens after
15906 * resume. Force-enable the pipe to fix this, the update_dpms
15907 * call below we restore the pipe to the right state, but leave
15908 * the required bits on. */
15909 intel_enable_pipe_a(dev);
15910 }
15911
24929352
DV
15912 /* Adjust the state of the output pipe according to whether we
15913 * have active connectors/encoders. */
842e0307 15914 if (crtc->active && !intel_crtc_has_encoders(crtc))
b17d48e2 15915 intel_crtc_disable_noatomic(&crtc->base);
24929352 15916
a3ed6aad 15917 if (crtc->active || HAS_GMCH_DISPLAY(dev)) {
4cc31489
DV
15918 /*
15919 * We start out with underrun reporting disabled to avoid races.
15920 * For correct bookkeeping mark this on active crtcs.
15921 *
c5ab3bc0
DV
15922 * Also on gmch platforms we dont have any hardware bits to
15923 * disable the underrun reporting. Which means we need to start
15924 * out with underrun reporting disabled also on inactive pipes,
15925 * since otherwise we'll complain about the garbage we read when
15926 * e.g. coming up after runtime pm.
15927 *
4cc31489
DV
15928 * No protection against concurrent access is required - at
15929 * worst a fifo underrun happens which also sets this to false.
15930 */
15931 crtc->cpu_fifo_underrun_disabled = true;
15932 crtc->pch_fifo_underrun_disabled = true;
15933 }
24929352
DV
15934}
15935
15936static void intel_sanitize_encoder(struct intel_encoder *encoder)
15937{
15938 struct intel_connector *connector;
15939 struct drm_device *dev = encoder->base.dev;
15940
15941 /* We need to check both for a crtc link (meaning that the
15942 * encoder is active and trying to read from a pipe) and the
15943 * pipe itself being active. */
15944 bool has_active_crtc = encoder->base.crtc &&
15945 to_intel_crtc(encoder->base.crtc)->active;
15946
dd756198 15947 if (intel_encoder_has_connectors(encoder) && !has_active_crtc) {
24929352
DV
15948 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
15949 encoder->base.base.id,
8e329a03 15950 encoder->base.name);
24929352
DV
15951
15952 /* Connector is active, but has no active pipe. This is
15953 * fallout from our resume register restoring. Disable
15954 * the encoder manually again. */
15955 if (encoder->base.crtc) {
15956 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
15957 encoder->base.base.id,
8e329a03 15958 encoder->base.name);
24929352 15959 encoder->disable(encoder);
a62d1497
VS
15960 if (encoder->post_disable)
15961 encoder->post_disable(encoder);
24929352 15962 }
7f1950fb 15963 encoder->base.crtc = NULL;
24929352
DV
15964
15965 /* Inconsistent output/port/pipe state happens presumably due to
15966 * a bug in one of the get_hw_state functions. Or someplace else
15967 * in our code, like the register restore mess on resume. Clamp
15968 * things to off as a safer default. */
3a3371ff 15969 for_each_intel_connector(dev, connector) {
24929352
DV
15970 if (connector->encoder != encoder)
15971 continue;
7f1950fb
EE
15972 connector->base.dpms = DRM_MODE_DPMS_OFF;
15973 connector->base.encoder = NULL;
24929352
DV
15974 }
15975 }
15976 /* Enabled encoders without active connectors will be fixed in
15977 * the crtc fixup. */
15978}
15979
04098753 15980void i915_redisable_vga_power_on(struct drm_device *dev)
0fde901f
KM
15981{
15982 struct drm_i915_private *dev_priv = dev->dev_private;
f0f59a00 15983 i915_reg_t vga_reg = i915_vgacntrl_reg(dev);
0fde901f 15984
04098753
ID
15985 if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
15986 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
15987 i915_disable_vga(dev);
15988 }
15989}
15990
15991void i915_redisable_vga(struct drm_device *dev)
15992{
15993 struct drm_i915_private *dev_priv = dev->dev_private;
15994
8dc8a27c
PZ
15995 /* This function can be called both from intel_modeset_setup_hw_state or
15996 * at a very early point in our resume sequence, where the power well
15997 * structures are not yet restored. Since this function is at a very
15998 * paranoid "someone might have enabled VGA while we were not looking"
15999 * level, just check if the power well is enabled instead of trying to
16000 * follow the "don't touch the power well if we don't need it" policy
16001 * the rest of the driver uses. */
6392f847 16002 if (!intel_display_power_get_if_enabled(dev_priv, POWER_DOMAIN_VGA))
8dc8a27c
PZ
16003 return;
16004
04098753 16005 i915_redisable_vga_power_on(dev);
6392f847
ID
16006
16007 intel_display_power_put(dev_priv, POWER_DOMAIN_VGA);
0fde901f
KM
16008}
16009
f9cd7b88 16010static bool primary_get_hw_state(struct intel_plane *plane)
98ec7739 16011{
f9cd7b88 16012 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
98ec7739 16013
f9cd7b88 16014 return I915_READ(DSPCNTR(plane->plane)) & DISPLAY_PLANE_ENABLE;
d032ffa0
ML
16015}
16016
f9cd7b88
VS
16017/* FIXME read out full plane state for all planes */
16018static void readout_plane_state(struct intel_crtc *crtc)
d032ffa0 16019{
b26d3ea3 16020 struct drm_plane *primary = crtc->base.primary;
f9cd7b88 16021 struct intel_plane_state *plane_state =
b26d3ea3 16022 to_intel_plane_state(primary->state);
d032ffa0 16023
19b8d387 16024 plane_state->visible = crtc->active &&
b26d3ea3
ML
16025 primary_get_hw_state(to_intel_plane(primary));
16026
16027 if (plane_state->visible)
16028 crtc->base.state->plane_mask |= 1 << drm_plane_index(primary);
98ec7739
VS
16029}
16030
30e984df 16031static void intel_modeset_readout_hw_state(struct drm_device *dev)
24929352
DV
16032{
16033 struct drm_i915_private *dev_priv = dev->dev_private;
16034 enum pipe pipe;
24929352
DV
16035 struct intel_crtc *crtc;
16036 struct intel_encoder *encoder;
16037 struct intel_connector *connector;
5358901f 16038 int i;
24929352 16039
565602d7
ML
16040 dev_priv->active_crtcs = 0;
16041
d3fcc808 16042 for_each_intel_crtc(dev, crtc) {
565602d7
ML
16043 struct intel_crtc_state *crtc_state = crtc->config;
16044 int pixclk = 0;
3b117c8f 16045
ec2dc6a0 16046 __drm_atomic_helper_crtc_destroy_state(&crtc_state->base);
565602d7
ML
16047 memset(crtc_state, 0, sizeof(*crtc_state));
16048 crtc_state->base.crtc = &crtc->base;
24929352 16049
565602d7
ML
16050 crtc_state->base.active = crtc_state->base.enable =
16051 dev_priv->display.get_pipe_config(crtc, crtc_state);
16052
16053 crtc->base.enabled = crtc_state->base.enable;
16054 crtc->active = crtc_state->base.active;
16055
16056 if (crtc_state->base.active) {
16057 dev_priv->active_crtcs |= 1 << crtc->pipe;
16058
c89e39f3 16059 if (INTEL_GEN(dev_priv) >= 9 || IS_BROADWELL(dev_priv))
565602d7 16060 pixclk = ilk_pipe_pixel_rate(crtc_state);
9558d15d 16061 else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
565602d7
ML
16062 pixclk = crtc_state->base.adjusted_mode.crtc_clock;
16063 else
16064 WARN_ON(dev_priv->display.modeset_calc_cdclk);
9558d15d
VS
16065
16066 /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
16067 if (IS_BROADWELL(dev_priv) && crtc_state->ips_enabled)
16068 pixclk = DIV_ROUND_UP(pixclk * 100, 95);
565602d7
ML
16069 }
16070
16071 dev_priv->min_pixclk[crtc->pipe] = pixclk;
b70709a6 16072
f9cd7b88 16073 readout_plane_state(crtc);
24929352 16074
78108b7c
VS
16075 DRM_DEBUG_KMS("[CRTC:%d:%s] hw state readout: %s\n",
16076 crtc->base.base.id, crtc->base.name,
24929352
DV
16077 crtc->active ? "enabled" : "disabled");
16078 }
16079
5358901f
DV
16080 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
16081 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
16082
2edd6443
ACO
16083 pll->on = pll->funcs.get_hw_state(dev_priv, pll,
16084 &pll->config.hw_state);
3e369b76 16085 pll->config.crtc_mask = 0;
d3fcc808 16086 for_each_intel_crtc(dev, crtc) {
2dd66ebd 16087 if (crtc->active && crtc->config->shared_dpll == pll)
3e369b76 16088 pll->config.crtc_mask |= 1 << crtc->pipe;
5358901f 16089 }
2dd66ebd 16090 pll->active_mask = pll->config.crtc_mask;
5358901f 16091
1e6f2ddc 16092 DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
3e369b76 16093 pll->name, pll->config.crtc_mask, pll->on);
5358901f
DV
16094 }
16095
b2784e15 16096 for_each_intel_encoder(dev, encoder) {
24929352
DV
16097 pipe = 0;
16098
16099 if (encoder->get_hw_state(encoder, &pipe)) {
045ac3b5
JB
16100 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
16101 encoder->base.crtc = &crtc->base;
6e3c9717 16102 encoder->get_config(encoder, crtc->config);
24929352
DV
16103 } else {
16104 encoder->base.crtc = NULL;
16105 }
16106
6f2bcceb 16107 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
24929352 16108 encoder->base.base.id,
8e329a03 16109 encoder->base.name,
24929352 16110 encoder->base.crtc ? "enabled" : "disabled",
6f2bcceb 16111 pipe_name(pipe));
24929352
DV
16112 }
16113
3a3371ff 16114 for_each_intel_connector(dev, connector) {
24929352
DV
16115 if (connector->get_hw_state(connector)) {
16116 connector->base.dpms = DRM_MODE_DPMS_ON;
2aa974c9
ML
16117
16118 encoder = connector->encoder;
16119 connector->base.encoder = &encoder->base;
16120
16121 if (encoder->base.crtc &&
16122 encoder->base.crtc->state->active) {
16123 /*
16124 * This has to be done during hardware readout
16125 * because anything calling .crtc_disable may
16126 * rely on the connector_mask being accurate.
16127 */
16128 encoder->base.crtc->state->connector_mask |=
16129 1 << drm_connector_index(&connector->base);
e87a52b3
ML
16130 encoder->base.crtc->state->encoder_mask |=
16131 1 << drm_encoder_index(&encoder->base);
2aa974c9
ML
16132 }
16133
24929352
DV
16134 } else {
16135 connector->base.dpms = DRM_MODE_DPMS_OFF;
16136 connector->base.encoder = NULL;
16137 }
16138 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
16139 connector->base.base.id,
c23cc417 16140 connector->base.name,
24929352
DV
16141 connector->base.encoder ? "enabled" : "disabled");
16142 }
7f4c6284
VS
16143
16144 for_each_intel_crtc(dev, crtc) {
16145 crtc->base.hwmode = crtc->config->base.adjusted_mode;
16146
16147 memset(&crtc->base.mode, 0, sizeof(crtc->base.mode));
16148 if (crtc->base.state->active) {
16149 intel_mode_from_pipe_config(&crtc->base.mode, crtc->config);
16150 intel_mode_from_pipe_config(&crtc->base.state->adjusted_mode, crtc->config);
16151 WARN_ON(drm_atomic_set_mode_for_crtc(crtc->base.state, &crtc->base.mode));
16152
16153 /*
16154 * The initial mode needs to be set in order to keep
16155 * the atomic core happy. It wants a valid mode if the
16156 * crtc's enabled, so we do the above call.
16157 *
16158 * At this point some state updated by the connectors
16159 * in their ->detect() callback has not run yet, so
16160 * no recalculation can be done yet.
16161 *
16162 * Even if we could do a recalculation and modeset
16163 * right now it would cause a double modeset if
16164 * fbdev or userspace chooses a different initial mode.
16165 *
16166 * If that happens, someone indicated they wanted a
16167 * mode change, which means it's safe to do a full
16168 * recalculation.
16169 */
16170 crtc->base.state->mode.private_flags = I915_MODE_FLAG_INHERITED;
9eca6832
VS
16171
16172 drm_calc_timestamping_constants(&crtc->base, &crtc->base.hwmode);
16173 update_scanline_offset(crtc);
7f4c6284 16174 }
e3b247da
VS
16175
16176 intel_pipe_config_sanity_check(dev_priv, crtc->config);
7f4c6284 16177 }
30e984df
DV
16178}
16179
043e9bda
ML
16180/* Scan out the current hw modeset state,
16181 * and sanitizes it to the current state
16182 */
16183static void
16184intel_modeset_setup_hw_state(struct drm_device *dev)
30e984df
DV
16185{
16186 struct drm_i915_private *dev_priv = dev->dev_private;
16187 enum pipe pipe;
30e984df
DV
16188 struct intel_crtc *crtc;
16189 struct intel_encoder *encoder;
35c95375 16190 int i;
30e984df
DV
16191
16192 intel_modeset_readout_hw_state(dev);
24929352
DV
16193
16194 /* HW state is read out, now we need to sanitize this mess. */
b2784e15 16195 for_each_intel_encoder(dev, encoder) {
24929352
DV
16196 intel_sanitize_encoder(encoder);
16197 }
16198
055e393f 16199 for_each_pipe(dev_priv, pipe) {
24929352
DV
16200 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
16201 intel_sanitize_crtc(crtc);
6e3c9717
ACO
16202 intel_dump_pipe_config(crtc, crtc->config,
16203 "[setup_hw_state]");
24929352 16204 }
9a935856 16205
d29b2f9d
ACO
16206 intel_modeset_update_connector_atomic_state(dev);
16207
35c95375
DV
16208 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
16209 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
16210
2dd66ebd 16211 if (!pll->on || pll->active_mask)
35c95375
DV
16212 continue;
16213
16214 DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
16215
2edd6443 16216 pll->funcs.disable(dev_priv, pll);
35c95375
DV
16217 pll->on = false;
16218 }
16219
666a4537 16220 if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev))
6eb1a681
VS
16221 vlv_wm_get_hw_state(dev);
16222 else if (IS_GEN9(dev))
3078999f
PB
16223 skl_wm_get_hw_state(dev);
16224 else if (HAS_PCH_SPLIT(dev))
243e6a44 16225 ilk_wm_get_hw_state(dev);
292b990e
ML
16226
16227 for_each_intel_crtc(dev, crtc) {
16228 unsigned long put_domains;
16229
74bff5f9 16230 put_domains = modeset_get_crtc_power_domains(&crtc->base, crtc->config);
292b990e
ML
16231 if (WARN_ON(put_domains))
16232 modeset_put_power_domains(dev_priv, put_domains);
16233 }
16234 intel_display_set_init_power(dev_priv, false);
010cf73d
PZ
16235
16236 intel_fbc_init_pipe_state(dev_priv);
043e9bda 16237}
7d0bc1ea 16238
043e9bda
ML
16239void intel_display_resume(struct drm_device *dev)
16240{
e2c8b870
ML
16241 struct drm_i915_private *dev_priv = to_i915(dev);
16242 struct drm_atomic_state *state = dev_priv->modeset_restore_state;
16243 struct drm_modeset_acquire_ctx ctx;
043e9bda 16244 int ret;
e2c8b870 16245 bool setup = false;
f30da187 16246
e2c8b870 16247 dev_priv->modeset_restore_state = NULL;
043e9bda 16248
ea49c9ac
ML
16249 /*
16250 * This is a cludge because with real atomic modeset mode_config.mutex
16251 * won't be taken. Unfortunately some probed state like
16252 * audio_codec_enable is still protected by mode_config.mutex, so lock
16253 * it here for now.
16254 */
16255 mutex_lock(&dev->mode_config.mutex);
e2c8b870 16256 drm_modeset_acquire_init(&ctx, 0);
043e9bda 16257
e2c8b870
ML
16258retry:
16259 ret = drm_modeset_lock_all_ctx(dev, &ctx);
043e9bda 16260
e2c8b870
ML
16261 if (ret == 0 && !setup) {
16262 setup = true;
043e9bda 16263
e2c8b870
ML
16264 intel_modeset_setup_hw_state(dev);
16265 i915_redisable_vga(dev);
45e2b5f6 16266 }
8af6cf88 16267
e2c8b870
ML
16268 if (ret == 0 && state) {
16269 struct drm_crtc_state *crtc_state;
16270 struct drm_crtc *crtc;
16271 int i;
043e9bda 16272
e2c8b870
ML
16273 state->acquire_ctx = &ctx;
16274
e3d5457c
VS
16275 /* ignore any reset values/BIOS leftovers in the WM registers */
16276 to_intel_atomic_state(state)->skip_intermediate_wm = true;
16277
e2c8b870
ML
16278 for_each_crtc_in_state(state, crtc, crtc_state, i) {
16279 /*
16280 * Force recalculation even if we restore
16281 * current state. With fast modeset this may not result
16282 * in a modeset when the state is compatible.
16283 */
16284 crtc_state->mode_changed = true;
16285 }
16286
16287 ret = drm_atomic_commit(state);
043e9bda
ML
16288 }
16289
e2c8b870
ML
16290 if (ret == -EDEADLK) {
16291 drm_modeset_backoff(&ctx);
16292 goto retry;
16293 }
043e9bda 16294
e2c8b870
ML
16295 drm_modeset_drop_locks(&ctx);
16296 drm_modeset_acquire_fini(&ctx);
ea49c9ac 16297 mutex_unlock(&dev->mode_config.mutex);
043e9bda 16298
e2c8b870
ML
16299 if (ret) {
16300 DRM_ERROR("Restoring old state failed with %i\n", ret);
16301 drm_atomic_state_free(state);
16302 }
2c7111db
CW
16303}
16304
16305void intel_modeset_gem_init(struct drm_device *dev)
16306{
dc97997a 16307 struct drm_i915_private *dev_priv = to_i915(dev);
484b41dd 16308 struct drm_crtc *c;
2ff8fde1 16309 struct drm_i915_gem_object *obj;
e0d6149b 16310 int ret;
484b41dd 16311
dc97997a 16312 intel_init_gt_powersave(dev_priv);
ae48434c 16313
1833b134 16314 intel_modeset_init_hw(dev);
02e792fb 16315
1ee8da6d 16316 intel_setup_overlay(dev_priv);
484b41dd
JB
16317
16318 /*
16319 * Make sure any fbs we allocated at startup are properly
16320 * pinned & fenced. When we do the allocation it's too early
16321 * for this.
16322 */
70e1e0ec 16323 for_each_crtc(dev, c) {
2ff8fde1
MR
16324 obj = intel_fb_obj(c->primary->fb);
16325 if (obj == NULL)
484b41dd
JB
16326 continue;
16327
e0d6149b 16328 mutex_lock(&dev->struct_mutex);
3465c580
VS
16329 ret = intel_pin_and_fence_fb_obj(c->primary->fb,
16330 c->primary->state->rotation);
e0d6149b
TU
16331 mutex_unlock(&dev->struct_mutex);
16332 if (ret) {
484b41dd
JB
16333 DRM_ERROR("failed to pin boot fb on pipe %d\n",
16334 to_intel_crtc(c)->pipe);
66e514c1 16335 drm_framebuffer_unreference(c->primary->fb);
5a21b665 16336 c->primary->fb = NULL;
36750f28 16337 c->primary->crtc = c->primary->state->crtc = NULL;
5a21b665 16338 update_state_fb(c->primary);
36750f28 16339 c->state->plane_mask &= ~(1 << drm_plane_index(c->primary));
484b41dd
JB
16340 }
16341 }
1ebaa0b9
CW
16342}
16343
16344int intel_connector_register(struct drm_connector *connector)
16345{
16346 struct intel_connector *intel_connector = to_intel_connector(connector);
16347 int ret;
16348
16349 ret = intel_backlight_device_register(intel_connector);
16350 if (ret)
16351 goto err;
16352
16353 return 0;
0962c3c9 16354
1ebaa0b9
CW
16355err:
16356 return ret;
79e53945
JB
16357}
16358
c191eca1 16359void intel_connector_unregister(struct drm_connector *connector)
4932e2c3 16360{
e63d87c0 16361 struct intel_connector *intel_connector = to_intel_connector(connector);
4932e2c3 16362
e63d87c0 16363 intel_backlight_device_unregister(intel_connector);
4932e2c3 16364 intel_panel_destroy_backlight(connector);
4932e2c3
ID
16365}
16366
79e53945
JB
16367void intel_modeset_cleanup(struct drm_device *dev)
16368{
652c393a 16369 struct drm_i915_private *dev_priv = dev->dev_private;
652c393a 16370
dc97997a 16371 intel_disable_gt_powersave(dev_priv);
2eb5252e 16372
fd0c0642
DV
16373 /*
16374 * Interrupts and polling as the first thing to avoid creating havoc.
2eb5252e 16375 * Too much stuff here (turning of connectors, ...) would
fd0c0642
DV
16376 * experience fancy races otherwise.
16377 */
2aeb7d3a 16378 intel_irq_uninstall(dev_priv);
eb21b92b 16379
fd0c0642
DV
16380 /*
16381 * Due to the hpd irq storm handling the hotplug work can re-arm the
16382 * poll handlers. Hence disable polling after hpd handling is shut down.
16383 */
f87ea761 16384 drm_kms_helper_poll_fini(dev);
fd0c0642 16385
723bfd70
JB
16386 intel_unregister_dsm_handler();
16387
c937ab3e 16388 intel_fbc_global_disable(dev_priv);
69341a5e 16389
1630fe75
CW
16390 /* flush any delayed tasks or pending work */
16391 flush_scheduled_work();
16392
79e53945 16393 drm_mode_config_cleanup(dev);
4d7bb011 16394
1ee8da6d 16395 intel_cleanup_overlay(dev_priv);
ae48434c 16396
dc97997a 16397 intel_cleanup_gt_powersave(dev_priv);
f5949141
DV
16398
16399 intel_teardown_gmbus(dev);
79e53945
JB
16400}
16401
df0e9248
CW
16402void intel_connector_attach_encoder(struct intel_connector *connector,
16403 struct intel_encoder *encoder)
16404{
16405 connector->encoder = encoder;
16406 drm_mode_connector_attach_encoder(&connector->base,
16407 &encoder->base);
79e53945 16408}
28d52043
DA
16409
16410/*
16411 * set vga decode state - true == enable VGA decode
16412 */
16413int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
16414{
16415 struct drm_i915_private *dev_priv = dev->dev_private;
a885b3cc 16416 unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
28d52043
DA
16417 u16 gmch_ctrl;
16418
75fa041d
CW
16419 if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
16420 DRM_ERROR("failed to read control word\n");
16421 return -EIO;
16422 }
16423
c0cc8a55
CW
16424 if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
16425 return 0;
16426
28d52043
DA
16427 if (state)
16428 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
16429 else
16430 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
75fa041d
CW
16431
16432 if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
16433 DRM_ERROR("failed to write control word\n");
16434 return -EIO;
16435 }
16436
28d52043
DA
16437 return 0;
16438}
c4a1d9e4 16439
c4a1d9e4 16440struct intel_display_error_state {
ff57f1b0
PZ
16441
16442 u32 power_well_driver;
16443
63b66e5b
CW
16444 int num_transcoders;
16445
c4a1d9e4
CW
16446 struct intel_cursor_error_state {
16447 u32 control;
16448 u32 position;
16449 u32 base;
16450 u32 size;
52331309 16451 } cursor[I915_MAX_PIPES];
c4a1d9e4
CW
16452
16453 struct intel_pipe_error_state {
ddf9c536 16454 bool power_domain_on;
c4a1d9e4 16455 u32 source;
f301b1e1 16456 u32 stat;
52331309 16457 } pipe[I915_MAX_PIPES];
c4a1d9e4
CW
16458
16459 struct intel_plane_error_state {
16460 u32 control;
16461 u32 stride;
16462 u32 size;
16463 u32 pos;
16464 u32 addr;
16465 u32 surface;
16466 u32 tile_offset;
52331309 16467 } plane[I915_MAX_PIPES];
63b66e5b
CW
16468
16469 struct intel_transcoder_error_state {
ddf9c536 16470 bool power_domain_on;
63b66e5b
CW
16471 enum transcoder cpu_transcoder;
16472
16473 u32 conf;
16474
16475 u32 htotal;
16476 u32 hblank;
16477 u32 hsync;
16478 u32 vtotal;
16479 u32 vblank;
16480 u32 vsync;
16481 } transcoder[4];
c4a1d9e4
CW
16482};
16483
16484struct intel_display_error_state *
c033666a 16485intel_display_capture_error_state(struct drm_i915_private *dev_priv)
c4a1d9e4 16486{
c4a1d9e4 16487 struct intel_display_error_state *error;
63b66e5b
CW
16488 int transcoders[] = {
16489 TRANSCODER_A,
16490 TRANSCODER_B,
16491 TRANSCODER_C,
16492 TRANSCODER_EDP,
16493 };
c4a1d9e4
CW
16494 int i;
16495
c033666a 16496 if (INTEL_INFO(dev_priv)->num_pipes == 0)
63b66e5b
CW
16497 return NULL;
16498
9d1cb914 16499 error = kzalloc(sizeof(*error), GFP_ATOMIC);
c4a1d9e4
CW
16500 if (error == NULL)
16501 return NULL;
16502
c033666a 16503 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
ff57f1b0
PZ
16504 error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
16505
055e393f 16506 for_each_pipe(dev_priv, i) {
ddf9c536 16507 error->pipe[i].power_domain_on =
f458ebbc
DV
16508 __intel_display_power_is_enabled(dev_priv,
16509 POWER_DOMAIN_PIPE(i));
ddf9c536 16510 if (!error->pipe[i].power_domain_on)
9d1cb914
PZ
16511 continue;
16512
5efb3e28
VS
16513 error->cursor[i].control = I915_READ(CURCNTR(i));
16514 error->cursor[i].position = I915_READ(CURPOS(i));
16515 error->cursor[i].base = I915_READ(CURBASE(i));
c4a1d9e4
CW
16516
16517 error->plane[i].control = I915_READ(DSPCNTR(i));
16518 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
c033666a 16519 if (INTEL_GEN(dev_priv) <= 3) {
51889b35 16520 error->plane[i].size = I915_READ(DSPSIZE(i));
80ca378b
PZ
16521 error->plane[i].pos = I915_READ(DSPPOS(i));
16522 }
c033666a 16523 if (INTEL_GEN(dev_priv) <= 7 && !IS_HASWELL(dev_priv))
ca291363 16524 error->plane[i].addr = I915_READ(DSPADDR(i));
c033666a 16525 if (INTEL_GEN(dev_priv) >= 4) {
c4a1d9e4
CW
16526 error->plane[i].surface = I915_READ(DSPSURF(i));
16527 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
16528 }
16529
c4a1d9e4 16530 error->pipe[i].source = I915_READ(PIPESRC(i));
f301b1e1 16531
c033666a 16532 if (HAS_GMCH_DISPLAY(dev_priv))
f301b1e1 16533 error->pipe[i].stat = I915_READ(PIPESTAT(i));
63b66e5b
CW
16534 }
16535
4d1de975 16536 /* Note: this does not include DSI transcoders. */
c033666a 16537 error->num_transcoders = INTEL_INFO(dev_priv)->num_pipes;
2d1fe073 16538 if (HAS_DDI(dev_priv))
63b66e5b
CW
16539 error->num_transcoders++; /* Account for eDP. */
16540
16541 for (i = 0; i < error->num_transcoders; i++) {
16542 enum transcoder cpu_transcoder = transcoders[i];
16543
ddf9c536 16544 error->transcoder[i].power_domain_on =
f458ebbc 16545 __intel_display_power_is_enabled(dev_priv,
38cc1daf 16546 POWER_DOMAIN_TRANSCODER(cpu_transcoder));
ddf9c536 16547 if (!error->transcoder[i].power_domain_on)
9d1cb914
PZ
16548 continue;
16549
63b66e5b
CW
16550 error->transcoder[i].cpu_transcoder = cpu_transcoder;
16551
16552 error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
16553 error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
16554 error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
16555 error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
16556 error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
16557 error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
16558 error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
c4a1d9e4
CW
16559 }
16560
16561 return error;
16562}
16563
edc3d884
MK
16564#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
16565
c4a1d9e4 16566void
edc3d884 16567intel_display_print_error_state(struct drm_i915_error_state_buf *m,
c4a1d9e4
CW
16568 struct drm_device *dev,
16569 struct intel_display_error_state *error)
16570{
055e393f 16571 struct drm_i915_private *dev_priv = dev->dev_private;
c4a1d9e4
CW
16572 int i;
16573
63b66e5b
CW
16574 if (!error)
16575 return;
16576
edc3d884 16577 err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
190be112 16578 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
edc3d884 16579 err_printf(m, "PWR_WELL_CTL2: %08x\n",
ff57f1b0 16580 error->power_well_driver);
055e393f 16581 for_each_pipe(dev_priv, i) {
edc3d884 16582 err_printf(m, "Pipe [%d]:\n", i);
ddf9c536 16583 err_printf(m, " Power: %s\n",
87ad3212 16584 onoff(error->pipe[i].power_domain_on));
edc3d884 16585 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
f301b1e1 16586 err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
edc3d884
MK
16587
16588 err_printf(m, "Plane [%d]:\n", i);
16589 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
16590 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
80ca378b 16591 if (INTEL_INFO(dev)->gen <= 3) {
edc3d884
MK
16592 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
16593 err_printf(m, " POS: %08x\n", error->plane[i].pos);
80ca378b 16594 }
4b71a570 16595 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
edc3d884 16596 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
c4a1d9e4 16597 if (INTEL_INFO(dev)->gen >= 4) {
edc3d884
MK
16598 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
16599 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
c4a1d9e4
CW
16600 }
16601
edc3d884
MK
16602 err_printf(m, "Cursor [%d]:\n", i);
16603 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
16604 err_printf(m, " POS: %08x\n", error->cursor[i].position);
16605 err_printf(m, " BASE: %08x\n", error->cursor[i].base);
c4a1d9e4 16606 }
63b66e5b
CW
16607
16608 for (i = 0; i < error->num_transcoders; i++) {
da205630 16609 err_printf(m, "CPU transcoder: %s\n",
63b66e5b 16610 transcoder_name(error->transcoder[i].cpu_transcoder));
ddf9c536 16611 err_printf(m, " Power: %s\n",
87ad3212 16612 onoff(error->transcoder[i].power_domain_on));
63b66e5b
CW
16613 err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
16614 err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
16615 err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
16616 err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
16617 err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
16618 err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
16619 err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
16620 }
c4a1d9e4 16621}