]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/gpu/drm/i915/intel_display.c
drm/i915: Use crtc->name in debug messages
[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"
db18b6a6 39#include "intel_dsi.h"
e5510fac 40#include "i915_trace.h"
319c1d42 41#include <drm/drm_atomic.h>
c196e1d6 42#include <drm/drm_atomic_helper.h>
760285e7
DH
43#include <drm/drm_dp_helper.h>
44#include <drm/drm_crtc_helper.h>
465c120c
MR
45#include <drm/drm_plane_helper.h>
46#include <drm/drm_rect.h>
c0f372b3 47#include <linux/dma_remapping.h>
fd8e058a
AG
48#include <linux/reservation.h>
49#include <linux/dma-buf.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);
d1b32c32 126static int broxton_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 */
58e10eb9
CW
1127 if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
1128 100))
284637d9 1129 WARN(1, "pipe_off wait timed out\n");
ab7ad7f6 1130 } else {
ab7ad7f6 1131 /* Wait for the display line to settle */
fbf49ea2 1132 if (wait_for(pipe_dsl_stopped(dev, pipe), 100))
284637d9 1133 WARN(1, "pipe_off wait timed out\n");
ab7ad7f6 1134 }
79e53945
JB
1135}
1136
b24e7179 1137/* Only for pre-ILK configs */
55607e8a
DV
1138void assert_pll(struct drm_i915_private *dev_priv,
1139 enum pipe pipe, bool state)
b24e7179 1140{
b24e7179
JB
1141 u32 val;
1142 bool cur_state;
1143
649636ef 1144 val = I915_READ(DPLL(pipe));
b24e7179 1145 cur_state = !!(val & DPLL_VCO_ENABLE);
e2c719b7 1146 I915_STATE_WARN(cur_state != state,
b24e7179 1147 "PLL state assertion failure (expected %s, current %s)\n",
87ad3212 1148 onoff(state), onoff(cur_state));
b24e7179 1149}
b24e7179 1150
23538ef1 1151/* XXX: the dsi pll is shared between MIPI DSI ports */
8563b1e8 1152void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
23538ef1
JN
1153{
1154 u32 val;
1155 bool cur_state;
1156
a580516d 1157 mutex_lock(&dev_priv->sb_lock);
23538ef1 1158 val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
a580516d 1159 mutex_unlock(&dev_priv->sb_lock);
23538ef1
JN
1160
1161 cur_state = val & DSI_PLL_VCO_EN;
e2c719b7 1162 I915_STATE_WARN(cur_state != state,
23538ef1 1163 "DSI PLL state assertion failure (expected %s, current %s)\n",
87ad3212 1164 onoff(state), onoff(cur_state));
23538ef1 1165}
23538ef1 1166
040484af
JB
1167static void assert_fdi_tx(struct drm_i915_private *dev_priv,
1168 enum pipe pipe, bool state)
1169{
040484af 1170 bool cur_state;
ad80a810
PZ
1171 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1172 pipe);
040484af 1173
2d1fe073 1174 if (HAS_DDI(dev_priv)) {
affa9354 1175 /* DDI does not have a specific FDI_TX register */
649636ef 1176 u32 val = I915_READ(TRANS_DDI_FUNC_CTL(cpu_transcoder));
ad80a810 1177 cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
bf507ef7 1178 } else {
649636ef 1179 u32 val = I915_READ(FDI_TX_CTL(pipe));
bf507ef7
ED
1180 cur_state = !!(val & FDI_TX_ENABLE);
1181 }
e2c719b7 1182 I915_STATE_WARN(cur_state != state,
040484af 1183 "FDI TX state assertion failure (expected %s, current %s)\n",
87ad3212 1184 onoff(state), onoff(cur_state));
040484af
JB
1185}
1186#define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
1187#define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
1188
1189static void assert_fdi_rx(struct drm_i915_private *dev_priv,
1190 enum pipe pipe, bool state)
1191{
040484af
JB
1192 u32 val;
1193 bool cur_state;
1194
649636ef 1195 val = I915_READ(FDI_RX_CTL(pipe));
d63fa0dc 1196 cur_state = !!(val & FDI_RX_ENABLE);
e2c719b7 1197 I915_STATE_WARN(cur_state != state,
040484af 1198 "FDI RX state assertion failure (expected %s, current %s)\n",
87ad3212 1199 onoff(state), onoff(cur_state));
040484af
JB
1200}
1201#define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
1202#define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
1203
1204static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
1205 enum pipe pipe)
1206{
040484af
JB
1207 u32 val;
1208
1209 /* ILK FDI PLL is always enabled */
7e22dbbb 1210 if (IS_GEN5(dev_priv))
040484af
JB
1211 return;
1212
bf507ef7 1213 /* On Haswell, DDI ports are responsible for the FDI PLL setup */
2d1fe073 1214 if (HAS_DDI(dev_priv))
bf507ef7
ED
1215 return;
1216
649636ef 1217 val = I915_READ(FDI_TX_CTL(pipe));
e2c719b7 1218 I915_STATE_WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
040484af
JB
1219}
1220
55607e8a
DV
1221void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
1222 enum pipe pipe, bool state)
040484af 1223{
040484af 1224 u32 val;
55607e8a 1225 bool cur_state;
040484af 1226
649636ef 1227 val = I915_READ(FDI_RX_CTL(pipe));
55607e8a 1228 cur_state = !!(val & FDI_RX_PLL_ENABLE);
e2c719b7 1229 I915_STATE_WARN(cur_state != state,
55607e8a 1230 "FDI RX PLL assertion failure (expected %s, current %s)\n",
87ad3212 1231 onoff(state), onoff(cur_state));
040484af
JB
1232}
1233
b680c37a
DV
1234void assert_panel_unlocked(struct drm_i915_private *dev_priv,
1235 enum pipe pipe)
ea0760cf 1236{
bedd4dba 1237 struct drm_device *dev = dev_priv->dev;
f0f59a00 1238 i915_reg_t pp_reg;
ea0760cf
JB
1239 u32 val;
1240 enum pipe panel_pipe = PIPE_A;
0de3b485 1241 bool locked = true;
ea0760cf 1242
bedd4dba
JN
1243 if (WARN_ON(HAS_DDI(dev)))
1244 return;
1245
1246 if (HAS_PCH_SPLIT(dev)) {
1247 u32 port_sel;
1248
ea0760cf 1249 pp_reg = PCH_PP_CONTROL;
bedd4dba
JN
1250 port_sel = I915_READ(PCH_PP_ON_DELAYS) & PANEL_PORT_SELECT_MASK;
1251
1252 if (port_sel == PANEL_PORT_SELECT_LVDS &&
1253 I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
1254 panel_pipe = PIPE_B;
1255 /* XXX: else fix for eDP */
666a4537 1256 } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
bedd4dba
JN
1257 /* presumably write lock depends on pipe, not port select */
1258 pp_reg = VLV_PIPE_PP_CONTROL(pipe);
1259 panel_pipe = pipe;
ea0760cf
JB
1260 } else {
1261 pp_reg = PP_CONTROL;
bedd4dba
JN
1262 if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
1263 panel_pipe = PIPE_B;
ea0760cf
JB
1264 }
1265
1266 val = I915_READ(pp_reg);
1267 if (!(val & PANEL_POWER_ON) ||
ec49ba2d 1268 ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
ea0760cf
JB
1269 locked = false;
1270
e2c719b7 1271 I915_STATE_WARN(panel_pipe == pipe && locked,
ea0760cf 1272 "panel assertion failure, pipe %c regs locked\n",
9db4a9c7 1273 pipe_name(pipe));
ea0760cf
JB
1274}
1275
93ce0ba6
JN
1276static void assert_cursor(struct drm_i915_private *dev_priv,
1277 enum pipe pipe, bool state)
1278{
1279 struct drm_device *dev = dev_priv->dev;
1280 bool cur_state;
1281
d9d82081 1282 if (IS_845G(dev) || IS_I865G(dev))
0b87c24e 1283 cur_state = I915_READ(CURCNTR(PIPE_A)) & CURSOR_ENABLE;
d9d82081 1284 else
5efb3e28 1285 cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
93ce0ba6 1286
e2c719b7 1287 I915_STATE_WARN(cur_state != state,
93ce0ba6 1288 "cursor on pipe %c assertion failure (expected %s, current %s)\n",
87ad3212 1289 pipe_name(pipe), onoff(state), onoff(cur_state));
93ce0ba6
JN
1290}
1291#define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
1292#define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
1293
b840d907
JB
1294void assert_pipe(struct drm_i915_private *dev_priv,
1295 enum pipe pipe, bool state)
b24e7179 1296{
63d7bbe9 1297 bool cur_state;
702e7a56
PZ
1298 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1299 pipe);
4feed0eb 1300 enum intel_display_power_domain power_domain;
b24e7179 1301
b6b5d049
VS
1302 /* if we need the pipe quirk it must be always on */
1303 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1304 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
8e636784
DV
1305 state = true;
1306
4feed0eb
ID
1307 power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
1308 if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
649636ef 1309 u32 val = I915_READ(PIPECONF(cpu_transcoder));
69310161 1310 cur_state = !!(val & PIPECONF_ENABLE);
4feed0eb
ID
1311
1312 intel_display_power_put(dev_priv, power_domain);
1313 } else {
1314 cur_state = false;
69310161
PZ
1315 }
1316
e2c719b7 1317 I915_STATE_WARN(cur_state != state,
63d7bbe9 1318 "pipe %c assertion failure (expected %s, current %s)\n",
87ad3212 1319 pipe_name(pipe), onoff(state), onoff(cur_state));
b24e7179
JB
1320}
1321
931872fc
CW
1322static void assert_plane(struct drm_i915_private *dev_priv,
1323 enum plane plane, bool state)
b24e7179 1324{
b24e7179 1325 u32 val;
931872fc 1326 bool cur_state;
b24e7179 1327
649636ef 1328 val = I915_READ(DSPCNTR(plane));
931872fc 1329 cur_state = !!(val & DISPLAY_PLANE_ENABLE);
e2c719b7 1330 I915_STATE_WARN(cur_state != state,
931872fc 1331 "plane %c assertion failure (expected %s, current %s)\n",
87ad3212 1332 plane_name(plane), onoff(state), onoff(cur_state));
b24e7179
JB
1333}
1334
931872fc
CW
1335#define assert_plane_enabled(d, p) assert_plane(d, p, true)
1336#define assert_plane_disabled(d, p) assert_plane(d, p, false)
1337
b24e7179
JB
1338static void assert_planes_disabled(struct drm_i915_private *dev_priv,
1339 enum pipe pipe)
1340{
653e1026 1341 struct drm_device *dev = dev_priv->dev;
649636ef 1342 int i;
b24e7179 1343
653e1026
VS
1344 /* Primary planes are fixed to pipes on gen4+ */
1345 if (INTEL_INFO(dev)->gen >= 4) {
649636ef 1346 u32 val = I915_READ(DSPCNTR(pipe));
e2c719b7 1347 I915_STATE_WARN(val & DISPLAY_PLANE_ENABLE,
28c05794
AJ
1348 "plane %c assertion failure, should be disabled but not\n",
1349 plane_name(pipe));
19ec1358 1350 return;
28c05794 1351 }
19ec1358 1352
b24e7179 1353 /* Need to check both planes against the pipe */
055e393f 1354 for_each_pipe(dev_priv, i) {
649636ef
VS
1355 u32 val = I915_READ(DSPCNTR(i));
1356 enum pipe cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
b24e7179 1357 DISPPLANE_SEL_PIPE_SHIFT;
e2c719b7 1358 I915_STATE_WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
9db4a9c7
JB
1359 "plane %c assertion failure, should be off on pipe %c but is still active\n",
1360 plane_name(i), pipe_name(pipe));
b24e7179
JB
1361 }
1362}
1363
19332d7a
JB
1364static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
1365 enum pipe pipe)
1366{
20674eef 1367 struct drm_device *dev = dev_priv->dev;
649636ef 1368 int sprite;
19332d7a 1369
7feb8b88 1370 if (INTEL_INFO(dev)->gen >= 9) {
3bdcfc0c 1371 for_each_sprite(dev_priv, pipe, sprite) {
649636ef 1372 u32 val = I915_READ(PLANE_CTL(pipe, sprite));
e2c719b7 1373 I915_STATE_WARN(val & PLANE_CTL_ENABLE,
7feb8b88
DL
1374 "plane %d assertion failure, should be off on pipe %c but is still active\n",
1375 sprite, pipe_name(pipe));
1376 }
666a4537 1377 } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
3bdcfc0c 1378 for_each_sprite(dev_priv, pipe, sprite) {
649636ef 1379 u32 val = I915_READ(SPCNTR(pipe, sprite));
e2c719b7 1380 I915_STATE_WARN(val & SP_ENABLE,
20674eef 1381 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
1fe47785 1382 sprite_name(pipe, sprite), pipe_name(pipe));
20674eef
VS
1383 }
1384 } else if (INTEL_INFO(dev)->gen >= 7) {
649636ef 1385 u32 val = I915_READ(SPRCTL(pipe));
e2c719b7 1386 I915_STATE_WARN(val & SPRITE_ENABLE,
06da8da2 1387 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
20674eef
VS
1388 plane_name(pipe), pipe_name(pipe));
1389 } else if (INTEL_INFO(dev)->gen >= 5) {
649636ef 1390 u32 val = I915_READ(DVSCNTR(pipe));
e2c719b7 1391 I915_STATE_WARN(val & DVS_ENABLE,
06da8da2 1392 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
20674eef 1393 plane_name(pipe), pipe_name(pipe));
19332d7a
JB
1394 }
1395}
1396
08c71e5e
VS
1397static void assert_vblank_disabled(struct drm_crtc *crtc)
1398{
e2c719b7 1399 if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
08c71e5e
VS
1400 drm_crtc_vblank_put(crtc);
1401}
1402
7abd4b35
ACO
1403void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
1404 enum pipe pipe)
92f2584a 1405{
92f2584a
JB
1406 u32 val;
1407 bool enabled;
1408
649636ef 1409 val = I915_READ(PCH_TRANSCONF(pipe));
92f2584a 1410 enabled = !!(val & TRANS_ENABLE);
e2c719b7 1411 I915_STATE_WARN(enabled,
9db4a9c7
JB
1412 "transcoder assertion failed, should be off on pipe %c but is still active\n",
1413 pipe_name(pipe));
92f2584a
JB
1414}
1415
4e634389
KP
1416static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
1417 enum pipe pipe, u32 port_sel, u32 val)
f0575e92
KP
1418{
1419 if ((val & DP_PORT_EN) == 0)
1420 return false;
1421
2d1fe073 1422 if (HAS_PCH_CPT(dev_priv)) {
f0f59a00 1423 u32 trans_dp_ctl = I915_READ(TRANS_DP_CTL(pipe));
f0575e92
KP
1424 if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
1425 return false;
2d1fe073 1426 } else if (IS_CHERRYVIEW(dev_priv)) {
44f37d1f
CML
1427 if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
1428 return false;
f0575e92
KP
1429 } else {
1430 if ((val & DP_PIPE_MASK) != (pipe << 30))
1431 return false;
1432 }
1433 return true;
1434}
1435
1519b995
KP
1436static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
1437 enum pipe pipe, u32 val)
1438{
dc0fa718 1439 if ((val & SDVO_ENABLE) == 0)
1519b995
KP
1440 return false;
1441
2d1fe073 1442 if (HAS_PCH_CPT(dev_priv)) {
dc0fa718 1443 if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
1519b995 1444 return false;
2d1fe073 1445 } else if (IS_CHERRYVIEW(dev_priv)) {
44f37d1f
CML
1446 if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
1447 return false;
1519b995 1448 } else {
dc0fa718 1449 if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
1519b995
KP
1450 return false;
1451 }
1452 return true;
1453}
1454
1455static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
1456 enum pipe pipe, u32 val)
1457{
1458 if ((val & LVDS_PORT_EN) == 0)
1459 return false;
1460
2d1fe073 1461 if (HAS_PCH_CPT(dev_priv)) {
1519b995
KP
1462 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1463 return false;
1464 } else {
1465 if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
1466 return false;
1467 }
1468 return true;
1469}
1470
1471static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
1472 enum pipe pipe, u32 val)
1473{
1474 if ((val & ADPA_DAC_ENABLE) == 0)
1475 return false;
2d1fe073 1476 if (HAS_PCH_CPT(dev_priv)) {
1519b995
KP
1477 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1478 return false;
1479 } else {
1480 if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
1481 return false;
1482 }
1483 return true;
1484}
1485
291906f1 1486static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
f0f59a00
VS
1487 enum pipe pipe, i915_reg_t reg,
1488 u32 port_sel)
291906f1 1489{
47a05eca 1490 u32 val = I915_READ(reg);
e2c719b7 1491 I915_STATE_WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
291906f1 1492 "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
f0f59a00 1493 i915_mmio_reg_offset(reg), pipe_name(pipe));
de9a35ab 1494
2d1fe073 1495 I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && (val & DP_PORT_EN) == 0
75c5da27 1496 && (val & DP_PIPEB_SELECT),
de9a35ab 1497 "IBX PCH dp port still using transcoder B\n");
291906f1
JB
1498}
1499
1500static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
f0f59a00 1501 enum pipe pipe, i915_reg_t reg)
291906f1 1502{
47a05eca 1503 u32 val = I915_READ(reg);
e2c719b7 1504 I915_STATE_WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
23c99e77 1505 "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
f0f59a00 1506 i915_mmio_reg_offset(reg), pipe_name(pipe));
de9a35ab 1507
2d1fe073 1508 I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && (val & SDVO_ENABLE) == 0
75c5da27 1509 && (val & SDVO_PIPE_B_SELECT),
de9a35ab 1510 "IBX PCH hdmi port still using transcoder B\n");
291906f1
JB
1511}
1512
1513static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
1514 enum pipe pipe)
1515{
291906f1 1516 u32 val;
291906f1 1517
f0575e92
KP
1518 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
1519 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
1520 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
291906f1 1521
649636ef 1522 val = I915_READ(PCH_ADPA);
e2c719b7 1523 I915_STATE_WARN(adpa_pipe_enabled(dev_priv, pipe, val),
291906f1 1524 "PCH VGA enabled on transcoder %c, should be disabled\n",
9db4a9c7 1525 pipe_name(pipe));
291906f1 1526
649636ef 1527 val = I915_READ(PCH_LVDS);
e2c719b7 1528 I915_STATE_WARN(lvds_pipe_enabled(dev_priv, pipe, val),
291906f1 1529 "PCH LVDS enabled on transcoder %c, should be disabled\n",
9db4a9c7 1530 pipe_name(pipe));
291906f1 1531
e2debe91
PZ
1532 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
1533 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
1534 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
291906f1
JB
1535}
1536
cd2d34d9
VS
1537static void _vlv_enable_pll(struct intel_crtc *crtc,
1538 const struct intel_crtc_state *pipe_config)
1539{
1540 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1541 enum pipe pipe = crtc->pipe;
1542
1543 I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
1544 POSTING_READ(DPLL(pipe));
1545 udelay(150);
1546
1547 if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
1548 DRM_ERROR("DPLL %d failed to lock\n", pipe);
1549}
1550
d288f65f 1551static void vlv_enable_pll(struct intel_crtc *crtc,
5cec258b 1552 const struct intel_crtc_state *pipe_config)
87442f73 1553{
cd2d34d9 1554 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
8bd3f301 1555 enum pipe pipe = crtc->pipe;
87442f73 1556
8bd3f301 1557 assert_pipe_disabled(dev_priv, pipe);
87442f73 1558
87442f73 1559 /* PLL is protected by panel, make sure we can write it */
7d1a83cb 1560 assert_panel_unlocked(dev_priv, pipe);
87442f73 1561
cd2d34d9
VS
1562 if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
1563 _vlv_enable_pll(crtc, pipe_config);
426115cf 1564
8bd3f301
VS
1565 I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
1566 POSTING_READ(DPLL_MD(pipe));
87442f73
DV
1567}
1568
cd2d34d9
VS
1569
1570static void _chv_enable_pll(struct intel_crtc *crtc,
1571 const struct intel_crtc_state *pipe_config)
9d556c99 1572{
cd2d34d9 1573 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
8bd3f301 1574 enum pipe pipe = crtc->pipe;
9d556c99 1575 enum dpio_channel port = vlv_pipe_to_channel(pipe);
9d556c99
CML
1576 u32 tmp;
1577
a580516d 1578 mutex_lock(&dev_priv->sb_lock);
9d556c99
CML
1579
1580 /* Enable back the 10bit clock to display controller */
1581 tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1582 tmp |= DPIO_DCLKP_EN;
1583 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
1584
54433e91
VS
1585 mutex_unlock(&dev_priv->sb_lock);
1586
9d556c99
CML
1587 /*
1588 * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
1589 */
1590 udelay(1);
1591
1592 /* Enable PLL */
d288f65f 1593 I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
9d556c99
CML
1594
1595 /* Check PLL is locked */
a11b0703 1596 if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
9d556c99 1597 DRM_ERROR("PLL %d failed to lock\n", pipe);
cd2d34d9
VS
1598}
1599
1600static void chv_enable_pll(struct intel_crtc *crtc,
1601 const struct intel_crtc_state *pipe_config)
1602{
1603 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1604 enum pipe pipe = crtc->pipe;
1605
1606 assert_pipe_disabled(dev_priv, pipe);
1607
1608 /* PLL is protected by panel, make sure we can write it */
1609 assert_panel_unlocked(dev_priv, pipe);
1610
1611 if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
1612 _chv_enable_pll(crtc, pipe_config);
9d556c99 1613
c231775c
VS
1614 if (pipe != PIPE_A) {
1615 /*
1616 * WaPixelRepeatModeFixForC0:chv
1617 *
1618 * DPLLCMD is AWOL. Use chicken bits to propagate
1619 * the value from DPLLBMD to either pipe B or C.
1620 */
1621 I915_WRITE(CBR4_VLV, pipe == PIPE_B ? CBR_DPLLBMD_PIPE_B : CBR_DPLLBMD_PIPE_C);
1622 I915_WRITE(DPLL_MD(PIPE_B), pipe_config->dpll_hw_state.dpll_md);
1623 I915_WRITE(CBR4_VLV, 0);
1624 dev_priv->chv_dpll_md[pipe] = pipe_config->dpll_hw_state.dpll_md;
1625
1626 /*
1627 * DPLLB VGA mode also seems to cause problems.
1628 * We should always have it disabled.
1629 */
1630 WARN_ON((I915_READ(DPLL(PIPE_B)) & DPLL_VGA_MODE_DIS) == 0);
1631 } else {
1632 I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
1633 POSTING_READ(DPLL_MD(pipe));
1634 }
9d556c99
CML
1635}
1636
1c4e0274
VS
1637static int intel_num_dvo_pipes(struct drm_device *dev)
1638{
1639 struct intel_crtc *crtc;
1640 int count = 0;
1641
1642 for_each_intel_crtc(dev, crtc)
3538b9df 1643 count += crtc->base.state->active &&
409ee761 1644 intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO);
1c4e0274
VS
1645
1646 return count;
1647}
1648
66e3d5c0 1649static void i9xx_enable_pll(struct intel_crtc *crtc)
63d7bbe9 1650{
66e3d5c0
DV
1651 struct drm_device *dev = crtc->base.dev;
1652 struct drm_i915_private *dev_priv = dev->dev_private;
f0f59a00 1653 i915_reg_t reg = DPLL(crtc->pipe);
6e3c9717 1654 u32 dpll = crtc->config->dpll_hw_state.dpll;
63d7bbe9 1655
66e3d5c0 1656 assert_pipe_disabled(dev_priv, crtc->pipe);
58c6eaa2 1657
63d7bbe9 1658 /* PLL is protected by panel, make sure we can write it */
66e3d5c0
DV
1659 if (IS_MOBILE(dev) && !IS_I830(dev))
1660 assert_panel_unlocked(dev_priv, crtc->pipe);
63d7bbe9 1661
1c4e0274
VS
1662 /* Enable DVO 2x clock on both PLLs if necessary */
1663 if (IS_I830(dev) && intel_num_dvo_pipes(dev) > 0) {
1664 /*
1665 * It appears to be important that we don't enable this
1666 * for the current pipe before otherwise configuring the
1667 * PLL. No idea how this should be handled if multiple
1668 * DVO outputs are enabled simultaneosly.
1669 */
1670 dpll |= DPLL_DVO_2X_MODE;
1671 I915_WRITE(DPLL(!crtc->pipe),
1672 I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
1673 }
66e3d5c0 1674
c2b63374
VS
1675 /*
1676 * Apparently we need to have VGA mode enabled prior to changing
1677 * the P1/P2 dividers. Otherwise the DPLL will keep using the old
1678 * dividers, even though the register value does change.
1679 */
1680 I915_WRITE(reg, 0);
1681
8e7a65aa
VS
1682 I915_WRITE(reg, dpll);
1683
66e3d5c0
DV
1684 /* Wait for the clocks to stabilize. */
1685 POSTING_READ(reg);
1686 udelay(150);
1687
1688 if (INTEL_INFO(dev)->gen >= 4) {
1689 I915_WRITE(DPLL_MD(crtc->pipe),
6e3c9717 1690 crtc->config->dpll_hw_state.dpll_md);
66e3d5c0
DV
1691 } else {
1692 /* The pixel multiplier can only be updated once the
1693 * DPLL is enabled and the clocks are stable.
1694 *
1695 * So write it again.
1696 */
1697 I915_WRITE(reg, dpll);
1698 }
63d7bbe9
JB
1699
1700 /* We do this three times for luck */
66e3d5c0 1701 I915_WRITE(reg, dpll);
63d7bbe9
JB
1702 POSTING_READ(reg);
1703 udelay(150); /* wait for warmup */
66e3d5c0 1704 I915_WRITE(reg, dpll);
63d7bbe9
JB
1705 POSTING_READ(reg);
1706 udelay(150); /* wait for warmup */
66e3d5c0 1707 I915_WRITE(reg, dpll);
63d7bbe9
JB
1708 POSTING_READ(reg);
1709 udelay(150); /* wait for warmup */
1710}
1711
1712/**
50b44a44 1713 * i9xx_disable_pll - disable a PLL
63d7bbe9
JB
1714 * @dev_priv: i915 private structure
1715 * @pipe: pipe PLL to disable
1716 *
1717 * Disable the PLL for @pipe, making sure the pipe is off first.
1718 *
1719 * Note! This is for pre-ILK only.
1720 */
1c4e0274 1721static void i9xx_disable_pll(struct intel_crtc *crtc)
63d7bbe9 1722{
1c4e0274
VS
1723 struct drm_device *dev = crtc->base.dev;
1724 struct drm_i915_private *dev_priv = dev->dev_private;
1725 enum pipe pipe = crtc->pipe;
1726
1727 /* Disable DVO 2x clock on both PLLs if necessary */
1728 if (IS_I830(dev) &&
409ee761 1729 intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO) &&
3538b9df 1730 !intel_num_dvo_pipes(dev)) {
1c4e0274
VS
1731 I915_WRITE(DPLL(PIPE_B),
1732 I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
1733 I915_WRITE(DPLL(PIPE_A),
1734 I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
1735 }
1736
b6b5d049
VS
1737 /* Don't disable pipe or pipe PLLs if needed */
1738 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1739 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
63d7bbe9
JB
1740 return;
1741
1742 /* Make sure the pipe isn't still relying on us */
1743 assert_pipe_disabled(dev_priv, pipe);
1744
b8afb911 1745 I915_WRITE(DPLL(pipe), DPLL_VGA_MODE_DIS);
50b44a44 1746 POSTING_READ(DPLL(pipe));
63d7bbe9
JB
1747}
1748
f6071166
JB
1749static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1750{
b8afb911 1751 u32 val;
f6071166
JB
1752
1753 /* Make sure the pipe isn't still relying on us */
1754 assert_pipe_disabled(dev_priv, pipe);
1755
03ed5cbf
VS
1756 val = DPLL_INTEGRATED_REF_CLK_VLV |
1757 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
1758 if (pipe != PIPE_A)
1759 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
1760
f6071166
JB
1761 I915_WRITE(DPLL(pipe), val);
1762 POSTING_READ(DPLL(pipe));
076ed3b2
CML
1763}
1764
1765static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1766{
d752048d 1767 enum dpio_channel port = vlv_pipe_to_channel(pipe);
076ed3b2
CML
1768 u32 val;
1769
a11b0703
VS
1770 /* Make sure the pipe isn't still relying on us */
1771 assert_pipe_disabled(dev_priv, pipe);
076ed3b2 1772
60bfe44f
VS
1773 val = DPLL_SSC_REF_CLK_CHV |
1774 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
a11b0703
VS
1775 if (pipe != PIPE_A)
1776 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
03ed5cbf 1777
a11b0703
VS
1778 I915_WRITE(DPLL(pipe), val);
1779 POSTING_READ(DPLL(pipe));
d752048d 1780
a580516d 1781 mutex_lock(&dev_priv->sb_lock);
d752048d
VS
1782
1783 /* Disable 10bit clock to display controller */
1784 val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1785 val &= ~DPIO_DCLKP_EN;
1786 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
1787
a580516d 1788 mutex_unlock(&dev_priv->sb_lock);
f6071166
JB
1789}
1790
e4607fcf 1791void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
9b6de0a1
VS
1792 struct intel_digital_port *dport,
1793 unsigned int expected_mask)
89b667f8
JB
1794{
1795 u32 port_mask;
f0f59a00 1796 i915_reg_t dpll_reg;
89b667f8 1797
e4607fcf
CML
1798 switch (dport->port) {
1799 case PORT_B:
89b667f8 1800 port_mask = DPLL_PORTB_READY_MASK;
00fc31b7 1801 dpll_reg = DPLL(0);
e4607fcf
CML
1802 break;
1803 case PORT_C:
89b667f8 1804 port_mask = DPLL_PORTC_READY_MASK;
00fc31b7 1805 dpll_reg = DPLL(0);
9b6de0a1 1806 expected_mask <<= 4;
00fc31b7
CML
1807 break;
1808 case PORT_D:
1809 port_mask = DPLL_PORTD_READY_MASK;
1810 dpll_reg = DPIO_PHY_STATUS;
e4607fcf
CML
1811 break;
1812 default:
1813 BUG();
1814 }
89b667f8 1815
9b6de0a1
VS
1816 if (wait_for((I915_READ(dpll_reg) & port_mask) == expected_mask, 1000))
1817 WARN(1, "timed out waiting for port %c ready: got 0x%x, expected 0x%x\n",
1818 port_name(dport->port), I915_READ(dpll_reg) & port_mask, expected_mask);
89b667f8
JB
1819}
1820
b8a4f404
PZ
1821static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1822 enum pipe pipe)
040484af 1823{
23670b32 1824 struct drm_device *dev = dev_priv->dev;
7c26e5c6 1825 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
e2b78267 1826 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
f0f59a00
VS
1827 i915_reg_t reg;
1828 uint32_t val, pipeconf_val;
040484af 1829
040484af 1830 /* Make sure PCH DPLL is enabled */
8106ddbd 1831 assert_shared_dpll_enabled(dev_priv, intel_crtc->config->shared_dpll);
040484af
JB
1832
1833 /* FDI must be feeding us bits for PCH ports */
1834 assert_fdi_tx_enabled(dev_priv, pipe);
1835 assert_fdi_rx_enabled(dev_priv, pipe);
1836
23670b32
DV
1837 if (HAS_PCH_CPT(dev)) {
1838 /* Workaround: Set the timing override bit before enabling the
1839 * pch transcoder. */
1840 reg = TRANS_CHICKEN2(pipe);
1841 val = I915_READ(reg);
1842 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
1843 I915_WRITE(reg, val);
59c859d6 1844 }
23670b32 1845
ab9412ba 1846 reg = PCH_TRANSCONF(pipe);
040484af 1847 val = I915_READ(reg);
5f7f726d 1848 pipeconf_val = I915_READ(PIPECONF(pipe));
e9bcff5c 1849
2d1fe073 1850 if (HAS_PCH_IBX(dev_priv)) {
e9bcff5c 1851 /*
c5de7c6f
VS
1852 * Make the BPC in transcoder be consistent with
1853 * that in pipeconf reg. For HDMI we must use 8bpc
1854 * here for both 8bpc and 12bpc.
e9bcff5c 1855 */
dfd07d72 1856 val &= ~PIPECONF_BPC_MASK;
c5de7c6f
VS
1857 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_HDMI))
1858 val |= PIPECONF_8BPC;
1859 else
1860 val |= pipeconf_val & PIPECONF_BPC_MASK;
e9bcff5c 1861 }
5f7f726d
PZ
1862
1863 val &= ~TRANS_INTERLACE_MASK;
1864 if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
2d1fe073 1865 if (HAS_PCH_IBX(dev_priv) &&
409ee761 1866 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
7c26e5c6
PZ
1867 val |= TRANS_LEGACY_INTERLACED_ILK;
1868 else
1869 val |= TRANS_INTERLACED;
5f7f726d
PZ
1870 else
1871 val |= TRANS_PROGRESSIVE;
1872
040484af
JB
1873 I915_WRITE(reg, val | TRANS_ENABLE);
1874 if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
4bb6f1f3 1875 DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
040484af
JB
1876}
1877
8fb033d7 1878static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
937bb610 1879 enum transcoder cpu_transcoder)
040484af 1880{
8fb033d7 1881 u32 val, pipeconf_val;
8fb033d7 1882
8fb033d7 1883 /* FDI must be feeding us bits for PCH ports */
1a240d4d 1884 assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
937bb610 1885 assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
8fb033d7 1886
223a6fdf 1887 /* Workaround: set timing override bit. */
36c0d0cf 1888 val = I915_READ(TRANS_CHICKEN2(PIPE_A));
23670b32 1889 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
36c0d0cf 1890 I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
223a6fdf 1891
25f3ef11 1892 val = TRANS_ENABLE;
937bb610 1893 pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
8fb033d7 1894
9a76b1c6
PZ
1895 if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
1896 PIPECONF_INTERLACED_ILK)
a35f2679 1897 val |= TRANS_INTERLACED;
8fb033d7
PZ
1898 else
1899 val |= TRANS_PROGRESSIVE;
1900
ab9412ba
DV
1901 I915_WRITE(LPT_TRANSCONF, val);
1902 if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
937bb610 1903 DRM_ERROR("Failed to enable PCH transcoder\n");
8fb033d7
PZ
1904}
1905
b8a4f404
PZ
1906static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
1907 enum pipe pipe)
040484af 1908{
23670b32 1909 struct drm_device *dev = dev_priv->dev;
f0f59a00
VS
1910 i915_reg_t reg;
1911 uint32_t val;
040484af
JB
1912
1913 /* FDI relies on the transcoder */
1914 assert_fdi_tx_disabled(dev_priv, pipe);
1915 assert_fdi_rx_disabled(dev_priv, pipe);
1916
291906f1
JB
1917 /* Ports must be off as well */
1918 assert_pch_ports_disabled(dev_priv, pipe);
1919
ab9412ba 1920 reg = PCH_TRANSCONF(pipe);
040484af
JB
1921 val = I915_READ(reg);
1922 val &= ~TRANS_ENABLE;
1923 I915_WRITE(reg, val);
1924 /* wait for PCH transcoder off, transcoder state */
1925 if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
4bb6f1f3 1926 DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
23670b32 1927
c465613b 1928 if (HAS_PCH_CPT(dev)) {
23670b32
DV
1929 /* Workaround: Clear the timing override chicken bit again. */
1930 reg = TRANS_CHICKEN2(pipe);
1931 val = I915_READ(reg);
1932 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
1933 I915_WRITE(reg, val);
1934 }
040484af
JB
1935}
1936
ab4d966c 1937static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
8fb033d7 1938{
8fb033d7
PZ
1939 u32 val;
1940
ab9412ba 1941 val = I915_READ(LPT_TRANSCONF);
8fb033d7 1942 val &= ~TRANS_ENABLE;
ab9412ba 1943 I915_WRITE(LPT_TRANSCONF, val);
8fb033d7 1944 /* wait for PCH transcoder off, transcoder state */
ab9412ba 1945 if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
8a52fd9f 1946 DRM_ERROR("Failed to disable PCH transcoder\n");
223a6fdf
PZ
1947
1948 /* Workaround: clear timing override bit. */
36c0d0cf 1949 val = I915_READ(TRANS_CHICKEN2(PIPE_A));
23670b32 1950 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
36c0d0cf 1951 I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
040484af
JB
1952}
1953
b24e7179 1954/**
309cfea8 1955 * intel_enable_pipe - enable a pipe, asserting requirements
0372264a 1956 * @crtc: crtc responsible for the pipe
b24e7179 1957 *
0372264a 1958 * Enable @crtc's pipe, making sure that various hardware specific requirements
b24e7179 1959 * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
b24e7179 1960 */
e1fdc473 1961static void intel_enable_pipe(struct intel_crtc *crtc)
b24e7179 1962{
0372264a
PZ
1963 struct drm_device *dev = crtc->base.dev;
1964 struct drm_i915_private *dev_priv = dev->dev_private;
1965 enum pipe pipe = crtc->pipe;
1a70a728 1966 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
1a240d4d 1967 enum pipe pch_transcoder;
f0f59a00 1968 i915_reg_t reg;
b24e7179
JB
1969 u32 val;
1970
9e2ee2dd
VS
1971 DRM_DEBUG_KMS("enabling pipe %c\n", pipe_name(pipe));
1972
58c6eaa2 1973 assert_planes_disabled(dev_priv, pipe);
93ce0ba6 1974 assert_cursor_disabled(dev_priv, pipe);
58c6eaa2
DV
1975 assert_sprites_disabled(dev_priv, pipe);
1976
2d1fe073 1977 if (HAS_PCH_LPT(dev_priv))
cc391bbb
PZ
1978 pch_transcoder = TRANSCODER_A;
1979 else
1980 pch_transcoder = pipe;
1981
b24e7179
JB
1982 /*
1983 * A pipe without a PLL won't actually be able to drive bits from
1984 * a plane. On ILK+ the pipe PLLs are integrated, so we don't
1985 * need the check.
1986 */
2d1fe073 1987 if (HAS_GMCH_DISPLAY(dev_priv))
a65347ba 1988 if (crtc->config->has_dsi_encoder)
23538ef1
JN
1989 assert_dsi_pll_enabled(dev_priv);
1990 else
1991 assert_pll_enabled(dev_priv, pipe);
040484af 1992 else {
6e3c9717 1993 if (crtc->config->has_pch_encoder) {
040484af 1994 /* if driving the PCH, we need FDI enabled */
cc391bbb 1995 assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
1a240d4d
DV
1996 assert_fdi_tx_pll_enabled(dev_priv,
1997 (enum pipe) cpu_transcoder);
040484af
JB
1998 }
1999 /* FIXME: assert CPU port conditions for SNB+ */
2000 }
b24e7179 2001
702e7a56 2002 reg = PIPECONF(cpu_transcoder);
b24e7179 2003 val = I915_READ(reg);
7ad25d48 2004 if (val & PIPECONF_ENABLE) {
b6b5d049
VS
2005 WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
2006 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
00d70b15 2007 return;
7ad25d48 2008 }
00d70b15
CW
2009
2010 I915_WRITE(reg, val | PIPECONF_ENABLE);
851855d8 2011 POSTING_READ(reg);
b7792d8b
VS
2012
2013 /*
2014 * Until the pipe starts DSL will read as 0, which would cause
2015 * an apparent vblank timestamp jump, which messes up also the
2016 * frame count when it's derived from the timestamps. So let's
2017 * wait for the pipe to start properly before we call
2018 * drm_crtc_vblank_on()
2019 */
2020 if (dev->max_vblank_count == 0 &&
2021 wait_for(intel_get_crtc_scanline(crtc) != crtc->scanline_offset, 50))
2022 DRM_ERROR("pipe %c didn't start\n", pipe_name(pipe));
b24e7179
JB
2023}
2024
2025/**
309cfea8 2026 * intel_disable_pipe - disable a pipe, asserting requirements
575f7ab7 2027 * @crtc: crtc whose pipes is to be disabled
b24e7179 2028 *
575f7ab7
VS
2029 * Disable the pipe of @crtc, making sure that various hardware
2030 * specific requirements are met, if applicable, e.g. plane
2031 * disabled, panel fitter off, etc.
b24e7179
JB
2032 *
2033 * Will wait until the pipe has shut down before returning.
2034 */
575f7ab7 2035static void intel_disable_pipe(struct intel_crtc *crtc)
b24e7179 2036{
575f7ab7 2037 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
6e3c9717 2038 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
575f7ab7 2039 enum pipe pipe = crtc->pipe;
f0f59a00 2040 i915_reg_t reg;
b24e7179
JB
2041 u32 val;
2042
9e2ee2dd
VS
2043 DRM_DEBUG_KMS("disabling pipe %c\n", pipe_name(pipe));
2044
b24e7179
JB
2045 /*
2046 * Make sure planes won't keep trying to pump pixels to us,
2047 * or we might hang the display.
2048 */
2049 assert_planes_disabled(dev_priv, pipe);
93ce0ba6 2050 assert_cursor_disabled(dev_priv, pipe);
19332d7a 2051 assert_sprites_disabled(dev_priv, pipe);
b24e7179 2052
702e7a56 2053 reg = PIPECONF(cpu_transcoder);
b24e7179 2054 val = I915_READ(reg);
00d70b15
CW
2055 if ((val & PIPECONF_ENABLE) == 0)
2056 return;
2057
67adc644
VS
2058 /*
2059 * Double wide has implications for planes
2060 * so best keep it disabled when not needed.
2061 */
6e3c9717 2062 if (crtc->config->double_wide)
67adc644
VS
2063 val &= ~PIPECONF_DOUBLE_WIDE;
2064
2065 /* Don't disable pipe or pipe PLLs if needed */
b6b5d049
VS
2066 if (!(pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) &&
2067 !(pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
67adc644
VS
2068 val &= ~PIPECONF_ENABLE;
2069
2070 I915_WRITE(reg, val);
2071 if ((val & PIPECONF_ENABLE) == 0)
2072 intel_wait_for_pipe_off(crtc);
b24e7179
JB
2073}
2074
693db184
CW
2075static bool need_vtd_wa(struct drm_device *dev)
2076{
2077#ifdef CONFIG_INTEL_IOMMU
2078 if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
2079 return true;
2080#endif
2081 return false;
2082}
2083
832be82f
VS
2084static unsigned int intel_tile_size(const struct drm_i915_private *dev_priv)
2085{
2086 return IS_GEN2(dev_priv) ? 2048 : 4096;
2087}
2088
27ba3910
VS
2089static unsigned int intel_tile_width_bytes(const struct drm_i915_private *dev_priv,
2090 uint64_t fb_modifier, unsigned int cpp)
7b49f948
VS
2091{
2092 switch (fb_modifier) {
2093 case DRM_FORMAT_MOD_NONE:
2094 return cpp;
2095 case I915_FORMAT_MOD_X_TILED:
2096 if (IS_GEN2(dev_priv))
2097 return 128;
2098 else
2099 return 512;
2100 case I915_FORMAT_MOD_Y_TILED:
2101 if (IS_GEN2(dev_priv) || HAS_128_BYTE_Y_TILING(dev_priv))
2102 return 128;
2103 else
2104 return 512;
2105 case I915_FORMAT_MOD_Yf_TILED:
2106 switch (cpp) {
2107 case 1:
2108 return 64;
2109 case 2:
2110 case 4:
2111 return 128;
2112 case 8:
2113 case 16:
2114 return 256;
2115 default:
2116 MISSING_CASE(cpp);
2117 return cpp;
2118 }
2119 break;
2120 default:
2121 MISSING_CASE(fb_modifier);
2122 return cpp;
2123 }
2124}
2125
832be82f
VS
2126unsigned int intel_tile_height(const struct drm_i915_private *dev_priv,
2127 uint64_t fb_modifier, unsigned int cpp)
a57ce0b2 2128{
832be82f
VS
2129 if (fb_modifier == DRM_FORMAT_MOD_NONE)
2130 return 1;
2131 else
2132 return intel_tile_size(dev_priv) /
27ba3910 2133 intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
6761dd31
TU
2134}
2135
8d0deca8
VS
2136/* Return the tile dimensions in pixel units */
2137static void intel_tile_dims(const struct drm_i915_private *dev_priv,
2138 unsigned int *tile_width,
2139 unsigned int *tile_height,
2140 uint64_t fb_modifier,
2141 unsigned int cpp)
2142{
2143 unsigned int tile_width_bytes =
2144 intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
2145
2146 *tile_width = tile_width_bytes / cpp;
2147 *tile_height = intel_tile_size(dev_priv) / tile_width_bytes;
2148}
2149
6761dd31
TU
2150unsigned int
2151intel_fb_align_height(struct drm_device *dev, unsigned int height,
832be82f 2152 uint32_t pixel_format, uint64_t fb_modifier)
6761dd31 2153{
832be82f
VS
2154 unsigned int cpp = drm_format_plane_cpp(pixel_format, 0);
2155 unsigned int tile_height = intel_tile_height(to_i915(dev), fb_modifier, cpp);
2156
2157 return ALIGN(height, tile_height);
a57ce0b2
JB
2158}
2159
1663b9d6
VS
2160unsigned int intel_rotation_info_size(const struct intel_rotation_info *rot_info)
2161{
2162 unsigned int size = 0;
2163 int i;
2164
2165 for (i = 0 ; i < ARRAY_SIZE(rot_info->plane); i++)
2166 size += rot_info->plane[i].width * rot_info->plane[i].height;
2167
2168 return size;
2169}
2170
75c82a53 2171static void
3465c580
VS
2172intel_fill_fb_ggtt_view(struct i915_ggtt_view *view,
2173 const struct drm_framebuffer *fb,
2174 unsigned int rotation)
f64b98cd 2175{
2d7a215f
VS
2176 if (intel_rotation_90_or_270(rotation)) {
2177 *view = i915_ggtt_view_rotated;
2178 view->params.rotated = to_intel_framebuffer(fb)->rot_info;
2179 } else {
2180 *view = i915_ggtt_view_normal;
2181 }
2182}
50470bb0 2183
2d7a215f
VS
2184static void
2185intel_fill_fb_info(struct drm_i915_private *dev_priv,
2186 struct drm_framebuffer *fb)
2187{
2188 struct intel_rotation_info *info = &to_intel_framebuffer(fb)->rot_info;
2189 unsigned int tile_size, tile_width, tile_height, cpp;
50470bb0 2190
d9b3288e
VS
2191 tile_size = intel_tile_size(dev_priv);
2192
2193 cpp = drm_format_plane_cpp(fb->pixel_format, 0);
8d0deca8
VS
2194 intel_tile_dims(dev_priv, &tile_width, &tile_height,
2195 fb->modifier[0], cpp);
d9b3288e 2196
1663b9d6
VS
2197 info->plane[0].width = DIV_ROUND_UP(fb->pitches[0], tile_width * cpp);
2198 info->plane[0].height = DIV_ROUND_UP(fb->height, tile_height);
84fe03f7 2199
89e3e142 2200 if (info->pixel_format == DRM_FORMAT_NV12) {
832be82f 2201 cpp = drm_format_plane_cpp(fb->pixel_format, 1);
8d0deca8
VS
2202 intel_tile_dims(dev_priv, &tile_width, &tile_height,
2203 fb->modifier[1], cpp);
d9b3288e 2204
2d7a215f 2205 info->uv_offset = fb->offsets[1];
1663b9d6
VS
2206 info->plane[1].width = DIV_ROUND_UP(fb->pitches[1], tile_width * cpp);
2207 info->plane[1].height = DIV_ROUND_UP(fb->height / 2, tile_height);
89e3e142 2208 }
f64b98cd
TU
2209}
2210
603525d7 2211static unsigned int intel_linear_alignment(const struct drm_i915_private *dev_priv)
4e9a86b6
VS
2212{
2213 if (INTEL_INFO(dev_priv)->gen >= 9)
2214 return 256 * 1024;
985b8bb4 2215 else if (IS_BROADWATER(dev_priv) || IS_CRESTLINE(dev_priv) ||
666a4537 2216 IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
4e9a86b6
VS
2217 return 128 * 1024;
2218 else if (INTEL_INFO(dev_priv)->gen >= 4)
2219 return 4 * 1024;
2220 else
44c5905e 2221 return 0;
4e9a86b6
VS
2222}
2223
603525d7
VS
2224static unsigned int intel_surf_alignment(const struct drm_i915_private *dev_priv,
2225 uint64_t fb_modifier)
2226{
2227 switch (fb_modifier) {
2228 case DRM_FORMAT_MOD_NONE:
2229 return intel_linear_alignment(dev_priv);
2230 case I915_FORMAT_MOD_X_TILED:
2231 if (INTEL_INFO(dev_priv)->gen >= 9)
2232 return 256 * 1024;
2233 return 0;
2234 case I915_FORMAT_MOD_Y_TILED:
2235 case I915_FORMAT_MOD_Yf_TILED:
2236 return 1 * 1024 * 1024;
2237 default:
2238 MISSING_CASE(fb_modifier);
2239 return 0;
2240 }
2241}
2242
127bd2ac 2243int
3465c580
VS
2244intel_pin_and_fence_fb_obj(struct drm_framebuffer *fb,
2245 unsigned int rotation)
6b95a207 2246{
850c4cdc 2247 struct drm_device *dev = fb->dev;
ce453d81 2248 struct drm_i915_private *dev_priv = dev->dev_private;
850c4cdc 2249 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
f64b98cd 2250 struct i915_ggtt_view view;
6b95a207
KH
2251 u32 alignment;
2252 int ret;
2253
ebcdd39e
MR
2254 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
2255
603525d7 2256 alignment = intel_surf_alignment(dev_priv, fb->modifier[0]);
6b95a207 2257
3465c580 2258 intel_fill_fb_ggtt_view(&view, fb, rotation);
f64b98cd 2259
693db184
CW
2260 /* Note that the w/a also requires 64 PTE of padding following the
2261 * bo. We currently fill all unused PTE with the shadow page and so
2262 * we should always have valid PTE following the scanout preventing
2263 * the VT-d warning.
2264 */
2265 if (need_vtd_wa(dev) && alignment < 256 * 1024)
2266 alignment = 256 * 1024;
2267
d6dd6843
PZ
2268 /*
2269 * Global gtt pte registers are special registers which actually forward
2270 * writes to a chunk of system memory. Which means that there is no risk
2271 * that the register values disappear as soon as we call
2272 * intel_runtime_pm_put(), so it is correct to wrap only the
2273 * pin/unpin/fence and not more.
2274 */
2275 intel_runtime_pm_get(dev_priv);
2276
7580d774
ML
2277 ret = i915_gem_object_pin_to_display_plane(obj, alignment,
2278 &view);
48b956c5 2279 if (ret)
b26a6b35 2280 goto err_pm;
6b95a207
KH
2281
2282 /* Install a fence for tiled scan-out. Pre-i965 always needs a
2283 * fence, whereas 965+ only requires a fence if using
2284 * framebuffer compression. For simplicity, we always install
2285 * a fence as the cost is not that onerous.
2286 */
9807216f
VK
2287 if (view.type == I915_GGTT_VIEW_NORMAL) {
2288 ret = i915_gem_object_get_fence(obj);
2289 if (ret == -EDEADLK) {
2290 /*
2291 * -EDEADLK means there are no free fences
2292 * no pending flips.
2293 *
2294 * This is propagated to atomic, but it uses
2295 * -EDEADLK to force a locking recovery, so
2296 * change the returned error to -EBUSY.
2297 */
2298 ret = -EBUSY;
2299 goto err_unpin;
2300 } else if (ret)
2301 goto err_unpin;
1690e1eb 2302
9807216f
VK
2303 i915_gem_object_pin_fence(obj);
2304 }
6b95a207 2305
d6dd6843 2306 intel_runtime_pm_put(dev_priv);
6b95a207 2307 return 0;
48b956c5
CW
2308
2309err_unpin:
f64b98cd 2310 i915_gem_object_unpin_from_display_plane(obj, &view);
b26a6b35 2311err_pm:
d6dd6843 2312 intel_runtime_pm_put(dev_priv);
48b956c5 2313 return ret;
6b95a207
KH
2314}
2315
fb4b8ce1 2316void intel_unpin_fb_obj(struct drm_framebuffer *fb, unsigned int rotation)
1690e1eb 2317{
82bc3b2d 2318 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
f64b98cd 2319 struct i915_ggtt_view view;
82bc3b2d 2320
ebcdd39e
MR
2321 WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
2322
3465c580 2323 intel_fill_fb_ggtt_view(&view, fb, rotation);
f64b98cd 2324
9807216f
VK
2325 if (view.type == I915_GGTT_VIEW_NORMAL)
2326 i915_gem_object_unpin_fence(obj);
2327
f64b98cd 2328 i915_gem_object_unpin_from_display_plane(obj, &view);
1690e1eb
CW
2329}
2330
29cf9491
VS
2331/*
2332 * Adjust the tile offset by moving the difference into
2333 * the x/y offsets.
2334 *
2335 * Input tile dimensions and pitch must already be
2336 * rotated to match x and y, and in pixel units.
2337 */
2338static u32 intel_adjust_tile_offset(int *x, int *y,
2339 unsigned int tile_width,
2340 unsigned int tile_height,
2341 unsigned int tile_size,
2342 unsigned int pitch_tiles,
2343 u32 old_offset,
2344 u32 new_offset)
2345{
2346 unsigned int tiles;
2347
2348 WARN_ON(old_offset & (tile_size - 1));
2349 WARN_ON(new_offset & (tile_size - 1));
2350 WARN_ON(new_offset > old_offset);
2351
2352 tiles = (old_offset - new_offset) / tile_size;
2353
2354 *y += tiles / pitch_tiles * tile_height;
2355 *x += tiles % pitch_tiles * tile_width;
2356
2357 return new_offset;
2358}
2359
8d0deca8
VS
2360/*
2361 * Computes the linear offset to the base tile and adjusts
2362 * x, y. bytes per pixel is assumed to be a power-of-two.
2363 *
2364 * In the 90/270 rotated case, x and y are assumed
2365 * to be already rotated to match the rotated GTT view, and
2366 * pitch is the tile_height aligned framebuffer height.
2367 */
4f2d9934
VS
2368u32 intel_compute_tile_offset(int *x, int *y,
2369 const struct drm_framebuffer *fb, int plane,
8d0deca8
VS
2370 unsigned int pitch,
2371 unsigned int rotation)
c2c75131 2372{
4f2d9934
VS
2373 const struct drm_i915_private *dev_priv = to_i915(fb->dev);
2374 uint64_t fb_modifier = fb->modifier[plane];
2375 unsigned int cpp = drm_format_plane_cpp(fb->pixel_format, plane);
29cf9491
VS
2376 u32 offset, offset_aligned, alignment;
2377
2378 alignment = intel_surf_alignment(dev_priv, fb_modifier);
2379 if (alignment)
2380 alignment--;
2381
b5c65338 2382 if (fb_modifier != DRM_FORMAT_MOD_NONE) {
8d0deca8
VS
2383 unsigned int tile_size, tile_width, tile_height;
2384 unsigned int tile_rows, tiles, pitch_tiles;
c2c75131 2385
d843310d 2386 tile_size = intel_tile_size(dev_priv);
8d0deca8
VS
2387 intel_tile_dims(dev_priv, &tile_width, &tile_height,
2388 fb_modifier, cpp);
2389
2390 if (intel_rotation_90_or_270(rotation)) {
2391 pitch_tiles = pitch / tile_height;
2392 swap(tile_width, tile_height);
2393 } else {
2394 pitch_tiles = pitch / (tile_width * cpp);
2395 }
d843310d
VS
2396
2397 tile_rows = *y / tile_height;
2398 *y %= tile_height;
c2c75131 2399
8d0deca8
VS
2400 tiles = *x / tile_width;
2401 *x %= tile_width;
bc752862 2402
29cf9491
VS
2403 offset = (tile_rows * pitch_tiles + tiles) * tile_size;
2404 offset_aligned = offset & ~alignment;
bc752862 2405
29cf9491
VS
2406 intel_adjust_tile_offset(x, y, tile_width, tile_height,
2407 tile_size, pitch_tiles,
2408 offset, offset_aligned);
2409 } else {
bc752862 2410 offset = *y * pitch + *x * cpp;
29cf9491
VS
2411 offset_aligned = offset & ~alignment;
2412
4e9a86b6
VS
2413 *y = (offset & alignment) / pitch;
2414 *x = ((offset & alignment) - *y * pitch) / cpp;
bc752862 2415 }
29cf9491
VS
2416
2417 return offset_aligned;
c2c75131
DV
2418}
2419
b35d63fa 2420static int i9xx_format_to_fourcc(int format)
46f297fb
JB
2421{
2422 switch (format) {
2423 case DISPPLANE_8BPP:
2424 return DRM_FORMAT_C8;
2425 case DISPPLANE_BGRX555:
2426 return DRM_FORMAT_XRGB1555;
2427 case DISPPLANE_BGRX565:
2428 return DRM_FORMAT_RGB565;
2429 default:
2430 case DISPPLANE_BGRX888:
2431 return DRM_FORMAT_XRGB8888;
2432 case DISPPLANE_RGBX888:
2433 return DRM_FORMAT_XBGR8888;
2434 case DISPPLANE_BGRX101010:
2435 return DRM_FORMAT_XRGB2101010;
2436 case DISPPLANE_RGBX101010:
2437 return DRM_FORMAT_XBGR2101010;
2438 }
2439}
2440
bc8d7dff
DL
2441static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
2442{
2443 switch (format) {
2444 case PLANE_CTL_FORMAT_RGB_565:
2445 return DRM_FORMAT_RGB565;
2446 default:
2447 case PLANE_CTL_FORMAT_XRGB_8888:
2448 if (rgb_order) {
2449 if (alpha)
2450 return DRM_FORMAT_ABGR8888;
2451 else
2452 return DRM_FORMAT_XBGR8888;
2453 } else {
2454 if (alpha)
2455 return DRM_FORMAT_ARGB8888;
2456 else
2457 return DRM_FORMAT_XRGB8888;
2458 }
2459 case PLANE_CTL_FORMAT_XRGB_2101010:
2460 if (rgb_order)
2461 return DRM_FORMAT_XBGR2101010;
2462 else
2463 return DRM_FORMAT_XRGB2101010;
2464 }
2465}
2466
5724dbd1 2467static bool
f6936e29
DV
2468intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
2469 struct intel_initial_plane_config *plane_config)
46f297fb
JB
2470{
2471 struct drm_device *dev = crtc->base.dev;
3badb49f 2472 struct drm_i915_private *dev_priv = to_i915(dev);
72e96d64 2473 struct i915_ggtt *ggtt = &dev_priv->ggtt;
46f297fb
JB
2474 struct drm_i915_gem_object *obj = NULL;
2475 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
2d14030b 2476 struct drm_framebuffer *fb = &plane_config->fb->base;
f37b5c2b
DV
2477 u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
2478 u32 size_aligned = round_up(plane_config->base + plane_config->size,
2479 PAGE_SIZE);
2480
2481 size_aligned -= base_aligned;
46f297fb 2482
ff2652ea
CW
2483 if (plane_config->size == 0)
2484 return false;
2485
3badb49f
PZ
2486 /* If the FB is too big, just don't use it since fbdev is not very
2487 * important and we should probably use that space with FBC or other
2488 * features. */
72e96d64 2489 if (size_aligned * 2 > ggtt->stolen_usable_size)
3badb49f
PZ
2490 return false;
2491
12c83d99
TU
2492 mutex_lock(&dev->struct_mutex);
2493
f37b5c2b
DV
2494 obj = i915_gem_object_create_stolen_for_preallocated(dev,
2495 base_aligned,
2496 base_aligned,
2497 size_aligned);
12c83d99
TU
2498 if (!obj) {
2499 mutex_unlock(&dev->struct_mutex);
484b41dd 2500 return false;
12c83d99 2501 }
46f297fb 2502
49af449b
DL
2503 obj->tiling_mode = plane_config->tiling;
2504 if (obj->tiling_mode == I915_TILING_X)
6bf129df 2505 obj->stride = fb->pitches[0];
46f297fb 2506
6bf129df
DL
2507 mode_cmd.pixel_format = fb->pixel_format;
2508 mode_cmd.width = fb->width;
2509 mode_cmd.height = fb->height;
2510 mode_cmd.pitches[0] = fb->pitches[0];
18c5247e
DV
2511 mode_cmd.modifier[0] = fb->modifier[0];
2512 mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
46f297fb 2513
6bf129df 2514 if (intel_framebuffer_init(dev, to_intel_framebuffer(fb),
484b41dd 2515 &mode_cmd, obj)) {
46f297fb
JB
2516 DRM_DEBUG_KMS("intel fb init failed\n");
2517 goto out_unref_obj;
2518 }
12c83d99 2519
46f297fb 2520 mutex_unlock(&dev->struct_mutex);
484b41dd 2521
f6936e29 2522 DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
484b41dd 2523 return true;
46f297fb
JB
2524
2525out_unref_obj:
2526 drm_gem_object_unreference(&obj->base);
2527 mutex_unlock(&dev->struct_mutex);
484b41dd
JB
2528 return false;
2529}
2530
5a21b665
DV
2531/* Update plane->state->fb to match plane->fb after driver-internal updates */
2532static void
2533update_state_fb(struct drm_plane *plane)
2534{
2535 if (plane->fb == plane->state->fb)
2536 return;
2537
2538 if (plane->state->fb)
2539 drm_framebuffer_unreference(plane->state->fb);
2540 plane->state->fb = plane->fb;
2541 if (plane->state->fb)
2542 drm_framebuffer_reference(plane->state->fb);
2543}
2544
5724dbd1 2545static void
f6936e29
DV
2546intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
2547 struct intel_initial_plane_config *plane_config)
484b41dd
JB
2548{
2549 struct drm_device *dev = intel_crtc->base.dev;
d9ceb816 2550 struct drm_i915_private *dev_priv = dev->dev_private;
484b41dd
JB
2551 struct drm_crtc *c;
2552 struct intel_crtc *i;
2ff8fde1 2553 struct drm_i915_gem_object *obj;
88595ac9 2554 struct drm_plane *primary = intel_crtc->base.primary;
be5651f2 2555 struct drm_plane_state *plane_state = primary->state;
200757f5
MR
2556 struct drm_crtc_state *crtc_state = intel_crtc->base.state;
2557 struct intel_plane *intel_plane = to_intel_plane(primary);
0a8d8a86
MR
2558 struct intel_plane_state *intel_state =
2559 to_intel_plane_state(plane_state);
88595ac9 2560 struct drm_framebuffer *fb;
484b41dd 2561
2d14030b 2562 if (!plane_config->fb)
484b41dd
JB
2563 return;
2564
f6936e29 2565 if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
88595ac9
DV
2566 fb = &plane_config->fb->base;
2567 goto valid_fb;
f55548b5 2568 }
484b41dd 2569
2d14030b 2570 kfree(plane_config->fb);
484b41dd
JB
2571
2572 /*
2573 * Failed to alloc the obj, check to see if we should share
2574 * an fb with another CRTC instead
2575 */
70e1e0ec 2576 for_each_crtc(dev, c) {
484b41dd
JB
2577 i = to_intel_crtc(c);
2578
2579 if (c == &intel_crtc->base)
2580 continue;
2581
2ff8fde1
MR
2582 if (!i->active)
2583 continue;
2584
88595ac9
DV
2585 fb = c->primary->fb;
2586 if (!fb)
484b41dd
JB
2587 continue;
2588
88595ac9 2589 obj = intel_fb_obj(fb);
2ff8fde1 2590 if (i915_gem_obj_ggtt_offset(obj) == plane_config->base) {
88595ac9
DV
2591 drm_framebuffer_reference(fb);
2592 goto valid_fb;
484b41dd
JB
2593 }
2594 }
88595ac9 2595
200757f5
MR
2596 /*
2597 * We've failed to reconstruct the BIOS FB. Current display state
2598 * indicates that the primary plane is visible, but has a NULL FB,
2599 * which will lead to problems later if we don't fix it up. The
2600 * simplest solution is to just disable the primary plane now and
2601 * pretend the BIOS never had it enabled.
2602 */
2603 to_intel_plane_state(plane_state)->visible = false;
2604 crtc_state->plane_mask &= ~(1 << drm_plane_index(primary));
2622a081 2605 intel_pre_disable_primary_noatomic(&intel_crtc->base);
200757f5
MR
2606 intel_plane->disable_plane(primary, &intel_crtc->base);
2607
88595ac9
DV
2608 return;
2609
2610valid_fb:
f44e2659
VS
2611 plane_state->src_x = 0;
2612 plane_state->src_y = 0;
be5651f2
ML
2613 plane_state->src_w = fb->width << 16;
2614 plane_state->src_h = fb->height << 16;
2615
f44e2659
VS
2616 plane_state->crtc_x = 0;
2617 plane_state->crtc_y = 0;
be5651f2
ML
2618 plane_state->crtc_w = fb->width;
2619 plane_state->crtc_h = fb->height;
2620
0a8d8a86
MR
2621 intel_state->src.x1 = plane_state->src_x;
2622 intel_state->src.y1 = plane_state->src_y;
2623 intel_state->src.x2 = plane_state->src_x + plane_state->src_w;
2624 intel_state->src.y2 = plane_state->src_y + plane_state->src_h;
2625 intel_state->dst.x1 = plane_state->crtc_x;
2626 intel_state->dst.y1 = plane_state->crtc_y;
2627 intel_state->dst.x2 = plane_state->crtc_x + plane_state->crtc_w;
2628 intel_state->dst.y2 = plane_state->crtc_y + plane_state->crtc_h;
2629
88595ac9
DV
2630 obj = intel_fb_obj(fb);
2631 if (obj->tiling_mode != I915_TILING_NONE)
2632 dev_priv->preserve_bios_swizzle = true;
2633
be5651f2
ML
2634 drm_framebuffer_reference(fb);
2635 primary->fb = primary->state->fb = fb;
36750f28 2636 primary->crtc = primary->state->crtc = &intel_crtc->base;
36750f28 2637 intel_crtc->base.state->plane_mask |= (1 << drm_plane_index(primary));
a9ff8714 2638 obj->frontbuffer_bits |= to_intel_plane(primary)->frontbuffer_bit;
46f297fb
JB
2639}
2640
a8d201af
ML
2641static void i9xx_update_primary_plane(struct drm_plane *primary,
2642 const struct intel_crtc_state *crtc_state,
2643 const struct intel_plane_state *plane_state)
81255565 2644{
a8d201af 2645 struct drm_device *dev = primary->dev;
81255565 2646 struct drm_i915_private *dev_priv = dev->dev_private;
a8d201af
ML
2647 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
2648 struct drm_framebuffer *fb = plane_state->base.fb;
2649 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
81255565 2650 int plane = intel_crtc->plane;
54ea9da8 2651 u32 linear_offset;
81255565 2652 u32 dspcntr;
f0f59a00 2653 i915_reg_t reg = DSPCNTR(plane);
8d0deca8 2654 unsigned int rotation = plane_state->base.rotation;
ac484963 2655 int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
54ea9da8
VS
2656 int x = plane_state->src.x1 >> 16;
2657 int y = plane_state->src.y1 >> 16;
c9ba6fad 2658
f45651ba
VS
2659 dspcntr = DISPPLANE_GAMMA_ENABLE;
2660
fdd508a6 2661 dspcntr |= DISPLAY_PLANE_ENABLE;
f45651ba
VS
2662
2663 if (INTEL_INFO(dev)->gen < 4) {
2664 if (intel_crtc->pipe == PIPE_B)
2665 dspcntr |= DISPPLANE_SEL_PIPE_B;
2666
2667 /* pipesrc and dspsize control the size that is scaled from,
2668 * which should always be the user's requested size.
2669 */
2670 I915_WRITE(DSPSIZE(plane),
a8d201af
ML
2671 ((crtc_state->pipe_src_h - 1) << 16) |
2672 (crtc_state->pipe_src_w - 1));
f45651ba 2673 I915_WRITE(DSPPOS(plane), 0);
c14b0485
VS
2674 } else if (IS_CHERRYVIEW(dev) && plane == PLANE_B) {
2675 I915_WRITE(PRIMSIZE(plane),
a8d201af
ML
2676 ((crtc_state->pipe_src_h - 1) << 16) |
2677 (crtc_state->pipe_src_w - 1));
c14b0485
VS
2678 I915_WRITE(PRIMPOS(plane), 0);
2679 I915_WRITE(PRIMCNSTALPHA(plane), 0);
f45651ba 2680 }
81255565 2681
57779d06
VS
2682 switch (fb->pixel_format) {
2683 case DRM_FORMAT_C8:
81255565
JB
2684 dspcntr |= DISPPLANE_8BPP;
2685 break;
57779d06 2686 case DRM_FORMAT_XRGB1555:
57779d06 2687 dspcntr |= DISPPLANE_BGRX555;
81255565 2688 break;
57779d06
VS
2689 case DRM_FORMAT_RGB565:
2690 dspcntr |= DISPPLANE_BGRX565;
2691 break;
2692 case DRM_FORMAT_XRGB8888:
57779d06
VS
2693 dspcntr |= DISPPLANE_BGRX888;
2694 break;
2695 case DRM_FORMAT_XBGR8888:
57779d06
VS
2696 dspcntr |= DISPPLANE_RGBX888;
2697 break;
2698 case DRM_FORMAT_XRGB2101010:
57779d06
VS
2699 dspcntr |= DISPPLANE_BGRX101010;
2700 break;
2701 case DRM_FORMAT_XBGR2101010:
57779d06 2702 dspcntr |= DISPPLANE_RGBX101010;
81255565
JB
2703 break;
2704 default:
baba133a 2705 BUG();
81255565 2706 }
57779d06 2707
f45651ba
VS
2708 if (INTEL_INFO(dev)->gen >= 4 &&
2709 obj->tiling_mode != I915_TILING_NONE)
2710 dspcntr |= DISPPLANE_TILED;
81255565 2711
de1aa629
VS
2712 if (IS_G4X(dev))
2713 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
2714
ac484963 2715 linear_offset = y * fb->pitches[0] + x * cpp;
81255565 2716
c2c75131
DV
2717 if (INTEL_INFO(dev)->gen >= 4) {
2718 intel_crtc->dspaddr_offset =
4f2d9934 2719 intel_compute_tile_offset(&x, &y, fb, 0,
8d0deca8 2720 fb->pitches[0], rotation);
c2c75131
DV
2721 linear_offset -= intel_crtc->dspaddr_offset;
2722 } else {
e506a0c6 2723 intel_crtc->dspaddr_offset = linear_offset;
c2c75131 2724 }
e506a0c6 2725
8d0deca8 2726 if (rotation == BIT(DRM_ROTATE_180)) {
48404c1e
SJ
2727 dspcntr |= DISPPLANE_ROTATE_180;
2728
a8d201af
ML
2729 x += (crtc_state->pipe_src_w - 1);
2730 y += (crtc_state->pipe_src_h - 1);
48404c1e
SJ
2731
2732 /* Finding the last pixel of the last line of the display
2733 data and adding to linear_offset*/
2734 linear_offset +=
a8d201af 2735 (crtc_state->pipe_src_h - 1) * fb->pitches[0] +
ac484963 2736 (crtc_state->pipe_src_w - 1) * cpp;
48404c1e
SJ
2737 }
2738
2db3366b
PZ
2739 intel_crtc->adjusted_x = x;
2740 intel_crtc->adjusted_y = y;
2741
48404c1e
SJ
2742 I915_WRITE(reg, dspcntr);
2743
01f2c773 2744 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
a6c45cf0 2745 if (INTEL_INFO(dev)->gen >= 4) {
85ba7b7d
DV
2746 I915_WRITE(DSPSURF(plane),
2747 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
5eddb70b 2748 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
e506a0c6 2749 I915_WRITE(DSPLINOFF(plane), linear_offset);
5eddb70b 2750 } else
f343c5f6 2751 I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
5eddb70b 2752 POSTING_READ(reg);
17638cd6
JB
2753}
2754
a8d201af
ML
2755static void i9xx_disable_primary_plane(struct drm_plane *primary,
2756 struct drm_crtc *crtc)
17638cd6
JB
2757{
2758 struct drm_device *dev = crtc->dev;
2759 struct drm_i915_private *dev_priv = dev->dev_private;
2760 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
17638cd6 2761 int plane = intel_crtc->plane;
f45651ba 2762
a8d201af
ML
2763 I915_WRITE(DSPCNTR(plane), 0);
2764 if (INTEL_INFO(dev_priv)->gen >= 4)
fdd508a6 2765 I915_WRITE(DSPSURF(plane), 0);
a8d201af
ML
2766 else
2767 I915_WRITE(DSPADDR(plane), 0);
2768 POSTING_READ(DSPCNTR(plane));
2769}
c9ba6fad 2770
a8d201af
ML
2771static void ironlake_update_primary_plane(struct drm_plane *primary,
2772 const struct intel_crtc_state *crtc_state,
2773 const struct intel_plane_state *plane_state)
2774{
2775 struct drm_device *dev = primary->dev;
2776 struct drm_i915_private *dev_priv = dev->dev_private;
2777 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
2778 struct drm_framebuffer *fb = plane_state->base.fb;
2779 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
2780 int plane = intel_crtc->plane;
54ea9da8 2781 u32 linear_offset;
a8d201af
ML
2782 u32 dspcntr;
2783 i915_reg_t reg = DSPCNTR(plane);
8d0deca8 2784 unsigned int rotation = plane_state->base.rotation;
ac484963 2785 int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
a8d201af
ML
2786 int x = plane_state->src.x1 >> 16;
2787 int y = plane_state->src.y1 >> 16;
c9ba6fad 2788
f45651ba 2789 dspcntr = DISPPLANE_GAMMA_ENABLE;
fdd508a6 2790 dspcntr |= DISPLAY_PLANE_ENABLE;
f45651ba
VS
2791
2792 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
2793 dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
17638cd6 2794
57779d06
VS
2795 switch (fb->pixel_format) {
2796 case DRM_FORMAT_C8:
17638cd6
JB
2797 dspcntr |= DISPPLANE_8BPP;
2798 break;
57779d06
VS
2799 case DRM_FORMAT_RGB565:
2800 dspcntr |= DISPPLANE_BGRX565;
17638cd6 2801 break;
57779d06 2802 case DRM_FORMAT_XRGB8888:
57779d06
VS
2803 dspcntr |= DISPPLANE_BGRX888;
2804 break;
2805 case DRM_FORMAT_XBGR8888:
57779d06
VS
2806 dspcntr |= DISPPLANE_RGBX888;
2807 break;
2808 case DRM_FORMAT_XRGB2101010:
57779d06
VS
2809 dspcntr |= DISPPLANE_BGRX101010;
2810 break;
2811 case DRM_FORMAT_XBGR2101010:
57779d06 2812 dspcntr |= DISPPLANE_RGBX101010;
17638cd6
JB
2813 break;
2814 default:
baba133a 2815 BUG();
17638cd6
JB
2816 }
2817
2818 if (obj->tiling_mode != I915_TILING_NONE)
2819 dspcntr |= DISPPLANE_TILED;
17638cd6 2820
f45651ba 2821 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev))
1f5d76db 2822 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
17638cd6 2823
ac484963 2824 linear_offset = y * fb->pitches[0] + x * cpp;
c2c75131 2825 intel_crtc->dspaddr_offset =
4f2d9934 2826 intel_compute_tile_offset(&x, &y, fb, 0,
8d0deca8 2827 fb->pitches[0], rotation);
c2c75131 2828 linear_offset -= intel_crtc->dspaddr_offset;
8d0deca8 2829 if (rotation == BIT(DRM_ROTATE_180)) {
48404c1e
SJ
2830 dspcntr |= DISPPLANE_ROTATE_180;
2831
2832 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev)) {
a8d201af
ML
2833 x += (crtc_state->pipe_src_w - 1);
2834 y += (crtc_state->pipe_src_h - 1);
48404c1e
SJ
2835
2836 /* Finding the last pixel of the last line of the display
2837 data and adding to linear_offset*/
2838 linear_offset +=
a8d201af 2839 (crtc_state->pipe_src_h - 1) * fb->pitches[0] +
ac484963 2840 (crtc_state->pipe_src_w - 1) * cpp;
48404c1e
SJ
2841 }
2842 }
2843
2db3366b
PZ
2844 intel_crtc->adjusted_x = x;
2845 intel_crtc->adjusted_y = y;
2846
48404c1e 2847 I915_WRITE(reg, dspcntr);
17638cd6 2848
01f2c773 2849 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
85ba7b7d
DV
2850 I915_WRITE(DSPSURF(plane),
2851 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
b3dc685e 2852 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
bc1c91eb
DL
2853 I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
2854 } else {
2855 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2856 I915_WRITE(DSPLINOFF(plane), linear_offset);
2857 }
17638cd6 2858 POSTING_READ(reg);
17638cd6
JB
2859}
2860
7b49f948
VS
2861u32 intel_fb_stride_alignment(const struct drm_i915_private *dev_priv,
2862 uint64_t fb_modifier, uint32_t pixel_format)
b321803d 2863{
7b49f948 2864 if (fb_modifier == DRM_FORMAT_MOD_NONE) {
b321803d 2865 return 64;
7b49f948
VS
2866 } else {
2867 int cpp = drm_format_plane_cpp(pixel_format, 0);
2868
27ba3910 2869 return intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
b321803d
DL
2870 }
2871}
2872
44eb0cb9
MK
2873u32 intel_plane_obj_offset(struct intel_plane *intel_plane,
2874 struct drm_i915_gem_object *obj,
2875 unsigned int plane)
121920fa 2876{
ce7f1728 2877 struct i915_ggtt_view view;
dedf278c 2878 struct i915_vma *vma;
44eb0cb9 2879 u64 offset;
121920fa 2880
e7941294 2881 intel_fill_fb_ggtt_view(&view, intel_plane->base.state->fb,
3465c580 2882 intel_plane->base.state->rotation);
121920fa 2883
ce7f1728 2884 vma = i915_gem_obj_to_ggtt_view(obj, &view);
dedf278c 2885 if (WARN(!vma, "ggtt vma for display object not found! (view=%u)\n",
ce7f1728 2886 view.type))
dedf278c
TU
2887 return -1;
2888
44eb0cb9 2889 offset = vma->node.start;
dedf278c
TU
2890
2891 if (plane == 1) {
7723f47d 2892 offset += vma->ggtt_view.params.rotated.uv_start_page *
dedf278c
TU
2893 PAGE_SIZE;
2894 }
2895
44eb0cb9
MK
2896 WARN_ON(upper_32_bits(offset));
2897
2898 return lower_32_bits(offset);
121920fa
TU
2899}
2900
e435d6e5
ML
2901static void skl_detach_scaler(struct intel_crtc *intel_crtc, int id)
2902{
2903 struct drm_device *dev = intel_crtc->base.dev;
2904 struct drm_i915_private *dev_priv = dev->dev_private;
2905
2906 I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, id), 0);
2907 I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, id), 0);
2908 I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, id), 0);
e435d6e5
ML
2909}
2910
a1b2278e
CK
2911/*
2912 * This function detaches (aka. unbinds) unused scalers in hardware
2913 */
0583236e 2914static void skl_detach_scalers(struct intel_crtc *intel_crtc)
a1b2278e 2915{
a1b2278e
CK
2916 struct intel_crtc_scaler_state *scaler_state;
2917 int i;
2918
a1b2278e
CK
2919 scaler_state = &intel_crtc->config->scaler_state;
2920
2921 /* loop through and disable scalers that aren't in use */
2922 for (i = 0; i < intel_crtc->num_scalers; i++) {
e435d6e5
ML
2923 if (!scaler_state->scalers[i].in_use)
2924 skl_detach_scaler(intel_crtc, i);
a1b2278e
CK
2925 }
2926}
2927
6156a456 2928u32 skl_plane_ctl_format(uint32_t pixel_format)
70d21f0e 2929{
6156a456 2930 switch (pixel_format) {
d161cf7a 2931 case DRM_FORMAT_C8:
c34ce3d1 2932 return PLANE_CTL_FORMAT_INDEXED;
70d21f0e 2933 case DRM_FORMAT_RGB565:
c34ce3d1 2934 return PLANE_CTL_FORMAT_RGB_565;
70d21f0e 2935 case DRM_FORMAT_XBGR8888:
c34ce3d1 2936 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
6156a456 2937 case DRM_FORMAT_XRGB8888:
c34ce3d1 2938 return PLANE_CTL_FORMAT_XRGB_8888;
6156a456
CK
2939 /*
2940 * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
2941 * to be already pre-multiplied. We need to add a knob (or a different
2942 * DRM_FORMAT) for user-space to configure that.
2943 */
f75fb42a 2944 case DRM_FORMAT_ABGR8888:
c34ce3d1 2945 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX |
6156a456 2946 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
6156a456 2947 case DRM_FORMAT_ARGB8888:
c34ce3d1 2948 return PLANE_CTL_FORMAT_XRGB_8888 |
6156a456 2949 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
70d21f0e 2950 case DRM_FORMAT_XRGB2101010:
c34ce3d1 2951 return PLANE_CTL_FORMAT_XRGB_2101010;
70d21f0e 2952 case DRM_FORMAT_XBGR2101010:
c34ce3d1 2953 return PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
6156a456 2954 case DRM_FORMAT_YUYV:
c34ce3d1 2955 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
6156a456 2956 case DRM_FORMAT_YVYU:
c34ce3d1 2957 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
6156a456 2958 case DRM_FORMAT_UYVY:
c34ce3d1 2959 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
6156a456 2960 case DRM_FORMAT_VYUY:
c34ce3d1 2961 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
70d21f0e 2962 default:
4249eeef 2963 MISSING_CASE(pixel_format);
70d21f0e 2964 }
8cfcba41 2965
c34ce3d1 2966 return 0;
6156a456 2967}
70d21f0e 2968
6156a456
CK
2969u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
2970{
6156a456 2971 switch (fb_modifier) {
30af77c4 2972 case DRM_FORMAT_MOD_NONE:
70d21f0e 2973 break;
30af77c4 2974 case I915_FORMAT_MOD_X_TILED:
c34ce3d1 2975 return PLANE_CTL_TILED_X;
b321803d 2976 case I915_FORMAT_MOD_Y_TILED:
c34ce3d1 2977 return PLANE_CTL_TILED_Y;
b321803d 2978 case I915_FORMAT_MOD_Yf_TILED:
c34ce3d1 2979 return PLANE_CTL_TILED_YF;
70d21f0e 2980 default:
6156a456 2981 MISSING_CASE(fb_modifier);
70d21f0e 2982 }
8cfcba41 2983
c34ce3d1 2984 return 0;
6156a456 2985}
70d21f0e 2986
6156a456
CK
2987u32 skl_plane_ctl_rotation(unsigned int rotation)
2988{
3b7a5119 2989 switch (rotation) {
6156a456
CK
2990 case BIT(DRM_ROTATE_0):
2991 break;
1e8df167
SJ
2992 /*
2993 * DRM_ROTATE_ is counter clockwise to stay compatible with Xrandr
2994 * while i915 HW rotation is clockwise, thats why this swapping.
2995 */
3b7a5119 2996 case BIT(DRM_ROTATE_90):
1e8df167 2997 return PLANE_CTL_ROTATE_270;
3b7a5119 2998 case BIT(DRM_ROTATE_180):
c34ce3d1 2999 return PLANE_CTL_ROTATE_180;
3b7a5119 3000 case BIT(DRM_ROTATE_270):
1e8df167 3001 return PLANE_CTL_ROTATE_90;
6156a456
CK
3002 default:
3003 MISSING_CASE(rotation);
3004 }
3005
c34ce3d1 3006 return 0;
6156a456
CK
3007}
3008
a8d201af
ML
3009static void skylake_update_primary_plane(struct drm_plane *plane,
3010 const struct intel_crtc_state *crtc_state,
3011 const struct intel_plane_state *plane_state)
6156a456 3012{
a8d201af 3013 struct drm_device *dev = plane->dev;
6156a456 3014 struct drm_i915_private *dev_priv = dev->dev_private;
a8d201af
ML
3015 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
3016 struct drm_framebuffer *fb = plane_state->base.fb;
3017 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
6156a456
CK
3018 int pipe = intel_crtc->pipe;
3019 u32 plane_ctl, stride_div, stride;
3020 u32 tile_height, plane_offset, plane_size;
a8d201af 3021 unsigned int rotation = plane_state->base.rotation;
6156a456 3022 int x_offset, y_offset;
44eb0cb9 3023 u32 surf_addr;
a8d201af
ML
3024 int scaler_id = plane_state->scaler_id;
3025 int src_x = plane_state->src.x1 >> 16;
3026 int src_y = plane_state->src.y1 >> 16;
3027 int src_w = drm_rect_width(&plane_state->src) >> 16;
3028 int src_h = drm_rect_height(&plane_state->src) >> 16;
3029 int dst_x = plane_state->dst.x1;
3030 int dst_y = plane_state->dst.y1;
3031 int dst_w = drm_rect_width(&plane_state->dst);
3032 int dst_h = drm_rect_height(&plane_state->dst);
70d21f0e 3033
6156a456
CK
3034 plane_ctl = PLANE_CTL_ENABLE |
3035 PLANE_CTL_PIPE_GAMMA_ENABLE |
3036 PLANE_CTL_PIPE_CSC_ENABLE;
3037
3038 plane_ctl |= skl_plane_ctl_format(fb->pixel_format);
3039 plane_ctl |= skl_plane_ctl_tiling(fb->modifier[0]);
3040 plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
6156a456
CK
3041 plane_ctl |= skl_plane_ctl_rotation(rotation);
3042
7b49f948 3043 stride_div = intel_fb_stride_alignment(dev_priv, fb->modifier[0],
b321803d 3044 fb->pixel_format);
dedf278c 3045 surf_addr = intel_plane_obj_offset(to_intel_plane(plane), obj, 0);
3b7a5119 3046
a42e5a23
PZ
3047 WARN_ON(drm_rect_width(&plane_state->src) == 0);
3048
3b7a5119 3049 if (intel_rotation_90_or_270(rotation)) {
832be82f
VS
3050 int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
3051
3b7a5119 3052 /* stride = Surface height in tiles */
832be82f 3053 tile_height = intel_tile_height(dev_priv, fb->modifier[0], cpp);
3b7a5119 3054 stride = DIV_ROUND_UP(fb->height, tile_height);
a8d201af
ML
3055 x_offset = stride * tile_height - src_y - src_h;
3056 y_offset = src_x;
6156a456 3057 plane_size = (src_w - 1) << 16 | (src_h - 1);
3b7a5119
SJ
3058 } else {
3059 stride = fb->pitches[0] / stride_div;
a8d201af
ML
3060 x_offset = src_x;
3061 y_offset = src_y;
6156a456 3062 plane_size = (src_h - 1) << 16 | (src_w - 1);
3b7a5119
SJ
3063 }
3064 plane_offset = y_offset << 16 | x_offset;
b321803d 3065
2db3366b
PZ
3066 intel_crtc->adjusted_x = x_offset;
3067 intel_crtc->adjusted_y = y_offset;
3068
70d21f0e 3069 I915_WRITE(PLANE_CTL(pipe, 0), plane_ctl);
3b7a5119
SJ
3070 I915_WRITE(PLANE_OFFSET(pipe, 0), plane_offset);
3071 I915_WRITE(PLANE_SIZE(pipe, 0), plane_size);
3072 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
6156a456
CK
3073
3074 if (scaler_id >= 0) {
3075 uint32_t ps_ctrl = 0;
3076
3077 WARN_ON(!dst_w || !dst_h);
3078 ps_ctrl = PS_SCALER_EN | PS_PLANE_SEL(0) |
3079 crtc_state->scaler_state.scalers[scaler_id].mode;
3080 I915_WRITE(SKL_PS_CTRL(pipe, scaler_id), ps_ctrl);
3081 I915_WRITE(SKL_PS_PWR_GATE(pipe, scaler_id), 0);
3082 I915_WRITE(SKL_PS_WIN_POS(pipe, scaler_id), (dst_x << 16) | dst_y);
3083 I915_WRITE(SKL_PS_WIN_SZ(pipe, scaler_id), (dst_w << 16) | dst_h);
3084 I915_WRITE(PLANE_POS(pipe, 0), 0);
3085 } else {
3086 I915_WRITE(PLANE_POS(pipe, 0), (dst_y << 16) | dst_x);
3087 }
3088
121920fa 3089 I915_WRITE(PLANE_SURF(pipe, 0), surf_addr);
70d21f0e
DL
3090
3091 POSTING_READ(PLANE_SURF(pipe, 0));
3092}
3093
a8d201af
ML
3094static void skylake_disable_primary_plane(struct drm_plane *primary,
3095 struct drm_crtc *crtc)
17638cd6
JB
3096{
3097 struct drm_device *dev = crtc->dev;
3098 struct drm_i915_private *dev_priv = dev->dev_private;
a8d201af 3099 int pipe = to_intel_crtc(crtc)->pipe;
17638cd6 3100
a8d201af
ML
3101 I915_WRITE(PLANE_CTL(pipe, 0), 0);
3102 I915_WRITE(PLANE_SURF(pipe, 0), 0);
3103 POSTING_READ(PLANE_SURF(pipe, 0));
3104}
29b9bde6 3105
a8d201af
ML
3106/* Assume fb object is pinned & idle & fenced and just update base pointers */
3107static int
3108intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
3109 int x, int y, enum mode_set_atomic state)
3110{
3111 /* Support for kgdboc is disabled, this needs a major rework. */
3112 DRM_ERROR("legacy panic handler not supported any more.\n");
3113
3114 return -ENODEV;
81255565
JB
3115}
3116
5a21b665
DV
3117static void intel_complete_page_flips(struct drm_i915_private *dev_priv)
3118{
3119 struct intel_crtc *crtc;
3120
3121 for_each_intel_crtc(dev_priv->dev, crtc)
3122 intel_finish_page_flip_cs(dev_priv, crtc->pipe);
3123}
3124
7514747d
VS
3125static void intel_update_primary_planes(struct drm_device *dev)
3126{
7514747d 3127 struct drm_crtc *crtc;
96a02917 3128
70e1e0ec 3129 for_each_crtc(dev, crtc) {
11c22da6
ML
3130 struct intel_plane *plane = to_intel_plane(crtc->primary);
3131 struct intel_plane_state *plane_state;
96a02917 3132
11c22da6 3133 drm_modeset_lock_crtc(crtc, &plane->base);
11c22da6
ML
3134 plane_state = to_intel_plane_state(plane->base.state);
3135
a8d201af
ML
3136 if (plane_state->visible)
3137 plane->update_plane(&plane->base,
3138 to_intel_crtc_state(crtc->state),
3139 plane_state);
11c22da6
ML
3140
3141 drm_modeset_unlock_crtc(crtc);
96a02917
VS
3142 }
3143}
3144
c033666a 3145void intel_prepare_reset(struct drm_i915_private *dev_priv)
7514747d
VS
3146{
3147 /* no reset support for gen2 */
c033666a 3148 if (IS_GEN2(dev_priv))
7514747d
VS
3149 return;
3150
3151 /* reset doesn't touch the display */
c033666a 3152 if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
7514747d
VS
3153 return;
3154
c033666a 3155 drm_modeset_lock_all(dev_priv->dev);
f98ce92f
VS
3156 /*
3157 * Disabling the crtcs gracefully seems nicer. Also the
3158 * g33 docs say we should at least disable all the planes.
3159 */
c033666a 3160 intel_display_suspend(dev_priv->dev);
7514747d
VS
3161}
3162
c033666a 3163void intel_finish_reset(struct drm_i915_private *dev_priv)
7514747d 3164{
5a21b665
DV
3165 /*
3166 * Flips in the rings will be nuked by the reset,
3167 * so complete all pending flips so that user space
3168 * will get its events and not get stuck.
3169 */
3170 intel_complete_page_flips(dev_priv);
3171
7514747d 3172 /* no reset support for gen2 */
c033666a 3173 if (IS_GEN2(dev_priv))
7514747d
VS
3174 return;
3175
3176 /* reset doesn't touch the display */
c033666a 3177 if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv)) {
7514747d
VS
3178 /*
3179 * Flips in the rings have been nuked by the reset,
3180 * so update the base address of all primary
3181 * planes to the the last fb to make sure we're
3182 * showing the correct fb after a reset.
11c22da6
ML
3183 *
3184 * FIXME: Atomic will make this obsolete since we won't schedule
3185 * CS-based flips (which might get lost in gpu resets) any more.
7514747d 3186 */
c033666a 3187 intel_update_primary_planes(dev_priv->dev);
7514747d
VS
3188 return;
3189 }
3190
3191 /*
3192 * The display has been reset as well,
3193 * so need a full re-initialization.
3194 */
3195 intel_runtime_pm_disable_interrupts(dev_priv);
3196 intel_runtime_pm_enable_interrupts(dev_priv);
3197
c033666a 3198 intel_modeset_init_hw(dev_priv->dev);
7514747d
VS
3199
3200 spin_lock_irq(&dev_priv->irq_lock);
3201 if (dev_priv->display.hpd_irq_setup)
91d14251 3202 dev_priv->display.hpd_irq_setup(dev_priv);
7514747d
VS
3203 spin_unlock_irq(&dev_priv->irq_lock);
3204
c033666a 3205 intel_display_resume(dev_priv->dev);
7514747d
VS
3206
3207 intel_hpd_init(dev_priv);
3208
c033666a 3209 drm_modeset_unlock_all(dev_priv->dev);
7514747d
VS
3210}
3211
7d5e3799
CW
3212static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
3213{
5a21b665
DV
3214 struct drm_device *dev = crtc->dev;
3215 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3216 unsigned reset_counter;
3217 bool pending;
3218
3219 reset_counter = i915_reset_counter(&to_i915(dev)->gpu_error);
3220 if (intel_crtc->reset_counter != reset_counter)
3221 return false;
3222
3223 spin_lock_irq(&dev->event_lock);
3224 pending = to_intel_crtc(crtc)->flip_work != NULL;
3225 spin_unlock_irq(&dev->event_lock);
3226
3227 return pending;
7d5e3799
CW
3228}
3229
bfd16b2a
ML
3230static void intel_update_pipe_config(struct intel_crtc *crtc,
3231 struct intel_crtc_state *old_crtc_state)
e30e8f75
GP
3232{
3233 struct drm_device *dev = crtc->base.dev;
3234 struct drm_i915_private *dev_priv = dev->dev_private;
bfd16b2a
ML
3235 struct intel_crtc_state *pipe_config =
3236 to_intel_crtc_state(crtc->base.state);
e30e8f75 3237
bfd16b2a
ML
3238 /* drm_atomic_helper_update_legacy_modeset_state might not be called. */
3239 crtc->base.mode = crtc->base.state->mode;
3240
3241 DRM_DEBUG_KMS("Updating pipe size %ix%i -> %ix%i\n",
3242 old_crtc_state->pipe_src_w, old_crtc_state->pipe_src_h,
3243 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
e30e8f75
GP
3244
3245 /*
3246 * Update pipe size and adjust fitter if needed: the reason for this is
3247 * that in compute_mode_changes we check the native mode (not the pfit
3248 * mode) to see if we can flip rather than do a full mode set. In the
3249 * fastboot case, we'll flip, but if we don't update the pipesrc and
3250 * pfit state, we'll end up with a big fb scanned out into the wrong
3251 * sized surface.
e30e8f75
GP
3252 */
3253
e30e8f75 3254 I915_WRITE(PIPESRC(crtc->pipe),
bfd16b2a
ML
3255 ((pipe_config->pipe_src_w - 1) << 16) |
3256 (pipe_config->pipe_src_h - 1));
3257
3258 /* on skylake this is done by detaching scalers */
3259 if (INTEL_INFO(dev)->gen >= 9) {
3260 skl_detach_scalers(crtc);
3261
3262 if (pipe_config->pch_pfit.enabled)
3263 skylake_pfit_enable(crtc);
3264 } else if (HAS_PCH_SPLIT(dev)) {
3265 if (pipe_config->pch_pfit.enabled)
3266 ironlake_pfit_enable(crtc);
3267 else if (old_crtc_state->pch_pfit.enabled)
3268 ironlake_pfit_disable(crtc, true);
e30e8f75 3269 }
e30e8f75
GP
3270}
3271
5e84e1a4
ZW
3272static void intel_fdi_normal_train(struct drm_crtc *crtc)
3273{
3274 struct drm_device *dev = crtc->dev;
3275 struct drm_i915_private *dev_priv = dev->dev_private;
3276 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3277 int pipe = intel_crtc->pipe;
f0f59a00
VS
3278 i915_reg_t reg;
3279 u32 temp;
5e84e1a4
ZW
3280
3281 /* enable normal train */
3282 reg = FDI_TX_CTL(pipe);
3283 temp = I915_READ(reg);
61e499bf 3284 if (IS_IVYBRIDGE(dev)) {
357555c0
JB
3285 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3286 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
61e499bf
KP
3287 } else {
3288 temp &= ~FDI_LINK_TRAIN_NONE;
3289 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
357555c0 3290 }
5e84e1a4
ZW
3291 I915_WRITE(reg, temp);
3292
3293 reg = FDI_RX_CTL(pipe);
3294 temp = I915_READ(reg);
3295 if (HAS_PCH_CPT(dev)) {
3296 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3297 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
3298 } else {
3299 temp &= ~FDI_LINK_TRAIN_NONE;
3300 temp |= FDI_LINK_TRAIN_NONE;
3301 }
3302 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
3303
3304 /* wait one idle pattern time */
3305 POSTING_READ(reg);
3306 udelay(1000);
357555c0
JB
3307
3308 /* IVB wants error correction enabled */
3309 if (IS_IVYBRIDGE(dev))
3310 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
3311 FDI_FE_ERRC_ENABLE);
5e84e1a4
ZW
3312}
3313
8db9d77b
ZW
3314/* The FDI link training functions for ILK/Ibexpeak. */
3315static void ironlake_fdi_link_train(struct drm_crtc *crtc)
3316{
3317 struct drm_device *dev = crtc->dev;
3318 struct drm_i915_private *dev_priv = dev->dev_private;
3319 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3320 int pipe = intel_crtc->pipe;
f0f59a00
VS
3321 i915_reg_t reg;
3322 u32 temp, tries;
8db9d77b 3323
1c8562f6 3324 /* FDI needs bits from pipe first */
0fc932b8 3325 assert_pipe_enabled(dev_priv, pipe);
0fc932b8 3326
e1a44743
AJ
3327 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3328 for train result */
5eddb70b
CW
3329 reg = FDI_RX_IMR(pipe);
3330 temp = I915_READ(reg);
e1a44743
AJ
3331 temp &= ~FDI_RX_SYMBOL_LOCK;
3332 temp &= ~FDI_RX_BIT_LOCK;
5eddb70b
CW
3333 I915_WRITE(reg, temp);
3334 I915_READ(reg);
e1a44743
AJ
3335 udelay(150);
3336
8db9d77b 3337 /* enable CPU FDI TX and PCH FDI RX */
5eddb70b
CW
3338 reg = FDI_TX_CTL(pipe);
3339 temp = I915_READ(reg);
627eb5a3 3340 temp &= ~FDI_DP_PORT_WIDTH_MASK;
6e3c9717 3341 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
8db9d77b
ZW
3342 temp &= ~FDI_LINK_TRAIN_NONE;
3343 temp |= FDI_LINK_TRAIN_PATTERN_1;
5eddb70b 3344 I915_WRITE(reg, temp | FDI_TX_ENABLE);
8db9d77b 3345
5eddb70b
CW
3346 reg = FDI_RX_CTL(pipe);
3347 temp = I915_READ(reg);
8db9d77b
ZW
3348 temp &= ~FDI_LINK_TRAIN_NONE;
3349 temp |= FDI_LINK_TRAIN_PATTERN_1;
5eddb70b
CW
3350 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3351
3352 POSTING_READ(reg);
8db9d77b
ZW
3353 udelay(150);
3354
5b2adf89 3355 /* Ironlake workaround, enable clock pointer after FDI enable*/
8f5718a6
DV
3356 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
3357 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
3358 FDI_RX_PHASE_SYNC_POINTER_EN);
5b2adf89 3359
5eddb70b 3360 reg = FDI_RX_IIR(pipe);
e1a44743 3361 for (tries = 0; tries < 5; tries++) {
5eddb70b 3362 temp = I915_READ(reg);
8db9d77b
ZW
3363 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3364
3365 if ((temp & FDI_RX_BIT_LOCK)) {
3366 DRM_DEBUG_KMS("FDI train 1 done.\n");
5eddb70b 3367 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
8db9d77b
ZW
3368 break;
3369 }
8db9d77b 3370 }
e1a44743 3371 if (tries == 5)
5eddb70b 3372 DRM_ERROR("FDI train 1 fail!\n");
8db9d77b
ZW
3373
3374 /* Train 2 */
5eddb70b
CW
3375 reg = FDI_TX_CTL(pipe);
3376 temp = I915_READ(reg);
8db9d77b
ZW
3377 temp &= ~FDI_LINK_TRAIN_NONE;
3378 temp |= FDI_LINK_TRAIN_PATTERN_2;
5eddb70b 3379 I915_WRITE(reg, temp);
8db9d77b 3380
5eddb70b
CW
3381 reg = FDI_RX_CTL(pipe);
3382 temp = I915_READ(reg);
8db9d77b
ZW
3383 temp &= ~FDI_LINK_TRAIN_NONE;
3384 temp |= FDI_LINK_TRAIN_PATTERN_2;
5eddb70b 3385 I915_WRITE(reg, temp);
8db9d77b 3386
5eddb70b
CW
3387 POSTING_READ(reg);
3388 udelay(150);
8db9d77b 3389
5eddb70b 3390 reg = FDI_RX_IIR(pipe);
e1a44743 3391 for (tries = 0; tries < 5; tries++) {
5eddb70b 3392 temp = I915_READ(reg);
8db9d77b
ZW
3393 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3394
3395 if (temp & FDI_RX_SYMBOL_LOCK) {
5eddb70b 3396 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
8db9d77b
ZW
3397 DRM_DEBUG_KMS("FDI train 2 done.\n");
3398 break;
3399 }
8db9d77b 3400 }
e1a44743 3401 if (tries == 5)
5eddb70b 3402 DRM_ERROR("FDI train 2 fail!\n");
8db9d77b
ZW
3403
3404 DRM_DEBUG_KMS("FDI train done\n");
5c5313c8 3405
8db9d77b
ZW
3406}
3407
0206e353 3408static const int snb_b_fdi_train_param[] = {
8db9d77b
ZW
3409 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
3410 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
3411 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
3412 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
3413};
3414
3415/* The FDI link training functions for SNB/Cougarpoint. */
3416static void gen6_fdi_link_train(struct drm_crtc *crtc)
3417{
3418 struct drm_device *dev = crtc->dev;
3419 struct drm_i915_private *dev_priv = dev->dev_private;
3420 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3421 int pipe = intel_crtc->pipe;
f0f59a00
VS
3422 i915_reg_t reg;
3423 u32 temp, i, retry;
8db9d77b 3424
e1a44743
AJ
3425 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3426 for train result */
5eddb70b
CW
3427 reg = FDI_RX_IMR(pipe);
3428 temp = I915_READ(reg);
e1a44743
AJ
3429 temp &= ~FDI_RX_SYMBOL_LOCK;
3430 temp &= ~FDI_RX_BIT_LOCK;
5eddb70b
CW
3431 I915_WRITE(reg, temp);
3432
3433 POSTING_READ(reg);
e1a44743
AJ
3434 udelay(150);
3435
8db9d77b 3436 /* enable CPU FDI TX and PCH FDI RX */
5eddb70b
CW
3437 reg = FDI_TX_CTL(pipe);
3438 temp = I915_READ(reg);
627eb5a3 3439 temp &= ~FDI_DP_PORT_WIDTH_MASK;
6e3c9717 3440 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
8db9d77b
ZW
3441 temp &= ~FDI_LINK_TRAIN_NONE;
3442 temp |= FDI_LINK_TRAIN_PATTERN_1;
3443 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3444 /* SNB-B */
3445 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
5eddb70b 3446 I915_WRITE(reg, temp | FDI_TX_ENABLE);
8db9d77b 3447
d74cf324
DV
3448 I915_WRITE(FDI_RX_MISC(pipe),
3449 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3450
5eddb70b
CW
3451 reg = FDI_RX_CTL(pipe);
3452 temp = I915_READ(reg);
8db9d77b
ZW
3453 if (HAS_PCH_CPT(dev)) {
3454 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3455 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3456 } else {
3457 temp &= ~FDI_LINK_TRAIN_NONE;
3458 temp |= FDI_LINK_TRAIN_PATTERN_1;
3459 }
5eddb70b
CW
3460 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3461
3462 POSTING_READ(reg);
8db9d77b
ZW
3463 udelay(150);
3464
0206e353 3465 for (i = 0; i < 4; i++) {
5eddb70b
CW
3466 reg = FDI_TX_CTL(pipe);
3467 temp = I915_READ(reg);
8db9d77b
ZW
3468 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3469 temp |= snb_b_fdi_train_param[i];
5eddb70b
CW
3470 I915_WRITE(reg, temp);
3471
3472 POSTING_READ(reg);
8db9d77b
ZW
3473 udelay(500);
3474
fa37d39e
SP
3475 for (retry = 0; retry < 5; retry++) {
3476 reg = FDI_RX_IIR(pipe);
3477 temp = I915_READ(reg);
3478 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3479 if (temp & FDI_RX_BIT_LOCK) {
3480 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3481 DRM_DEBUG_KMS("FDI train 1 done.\n");
3482 break;
3483 }
3484 udelay(50);
8db9d77b 3485 }
fa37d39e
SP
3486 if (retry < 5)
3487 break;
8db9d77b
ZW
3488 }
3489 if (i == 4)
5eddb70b 3490 DRM_ERROR("FDI train 1 fail!\n");
8db9d77b
ZW
3491
3492 /* Train 2 */
5eddb70b
CW
3493 reg = FDI_TX_CTL(pipe);
3494 temp = I915_READ(reg);
8db9d77b
ZW
3495 temp &= ~FDI_LINK_TRAIN_NONE;
3496 temp |= FDI_LINK_TRAIN_PATTERN_2;
3497 if (IS_GEN6(dev)) {
3498 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3499 /* SNB-B */
3500 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
3501 }
5eddb70b 3502 I915_WRITE(reg, temp);
8db9d77b 3503
5eddb70b
CW
3504 reg = FDI_RX_CTL(pipe);
3505 temp = I915_READ(reg);
8db9d77b
ZW
3506 if (HAS_PCH_CPT(dev)) {
3507 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3508 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
3509 } else {
3510 temp &= ~FDI_LINK_TRAIN_NONE;
3511 temp |= FDI_LINK_TRAIN_PATTERN_2;
3512 }
5eddb70b
CW
3513 I915_WRITE(reg, temp);
3514
3515 POSTING_READ(reg);
8db9d77b
ZW
3516 udelay(150);
3517
0206e353 3518 for (i = 0; i < 4; i++) {
5eddb70b
CW
3519 reg = FDI_TX_CTL(pipe);
3520 temp = I915_READ(reg);
8db9d77b
ZW
3521 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3522 temp |= snb_b_fdi_train_param[i];
5eddb70b
CW
3523 I915_WRITE(reg, temp);
3524
3525 POSTING_READ(reg);
8db9d77b
ZW
3526 udelay(500);
3527
fa37d39e
SP
3528 for (retry = 0; retry < 5; retry++) {
3529 reg = FDI_RX_IIR(pipe);
3530 temp = I915_READ(reg);
3531 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3532 if (temp & FDI_RX_SYMBOL_LOCK) {
3533 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3534 DRM_DEBUG_KMS("FDI train 2 done.\n");
3535 break;
3536 }
3537 udelay(50);
8db9d77b 3538 }
fa37d39e
SP
3539 if (retry < 5)
3540 break;
8db9d77b
ZW
3541 }
3542 if (i == 4)
5eddb70b 3543 DRM_ERROR("FDI train 2 fail!\n");
8db9d77b
ZW
3544
3545 DRM_DEBUG_KMS("FDI train done.\n");
3546}
3547
357555c0
JB
3548/* Manual link training for Ivy Bridge A0 parts */
3549static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
3550{
3551 struct drm_device *dev = crtc->dev;
3552 struct drm_i915_private *dev_priv = dev->dev_private;
3553 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3554 int pipe = intel_crtc->pipe;
f0f59a00
VS
3555 i915_reg_t reg;
3556 u32 temp, i, j;
357555c0
JB
3557
3558 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3559 for train result */
3560 reg = FDI_RX_IMR(pipe);
3561 temp = I915_READ(reg);
3562 temp &= ~FDI_RX_SYMBOL_LOCK;
3563 temp &= ~FDI_RX_BIT_LOCK;
3564 I915_WRITE(reg, temp);
3565
3566 POSTING_READ(reg);
3567 udelay(150);
3568
01a415fd
DV
3569 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
3570 I915_READ(FDI_RX_IIR(pipe)));
3571
139ccd3f
JB
3572 /* Try each vswing and preemphasis setting twice before moving on */
3573 for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
3574 /* disable first in case we need to retry */
3575 reg = FDI_TX_CTL(pipe);
3576 temp = I915_READ(reg);
3577 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
3578 temp &= ~FDI_TX_ENABLE;
3579 I915_WRITE(reg, temp);
357555c0 3580
139ccd3f
JB
3581 reg = FDI_RX_CTL(pipe);
3582 temp = I915_READ(reg);
3583 temp &= ~FDI_LINK_TRAIN_AUTO;
3584 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3585 temp &= ~FDI_RX_ENABLE;
3586 I915_WRITE(reg, temp);
357555c0 3587
139ccd3f 3588 /* enable CPU FDI TX and PCH FDI RX */
357555c0
JB
3589 reg = FDI_TX_CTL(pipe);
3590 temp = I915_READ(reg);
139ccd3f 3591 temp &= ~FDI_DP_PORT_WIDTH_MASK;
6e3c9717 3592 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
139ccd3f 3593 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
357555c0 3594 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
139ccd3f
JB
3595 temp |= snb_b_fdi_train_param[j/2];
3596 temp |= FDI_COMPOSITE_SYNC;
3597 I915_WRITE(reg, temp | FDI_TX_ENABLE);
357555c0 3598
139ccd3f
JB
3599 I915_WRITE(FDI_RX_MISC(pipe),
3600 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
357555c0 3601
139ccd3f 3602 reg = FDI_RX_CTL(pipe);
357555c0 3603 temp = I915_READ(reg);
139ccd3f
JB
3604 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3605 temp |= FDI_COMPOSITE_SYNC;
3606 I915_WRITE(reg, temp | FDI_RX_ENABLE);
357555c0 3607
139ccd3f
JB
3608 POSTING_READ(reg);
3609 udelay(1); /* should be 0.5us */
357555c0 3610
139ccd3f
JB
3611 for (i = 0; i < 4; i++) {
3612 reg = FDI_RX_IIR(pipe);
3613 temp = I915_READ(reg);
3614 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
357555c0 3615
139ccd3f
JB
3616 if (temp & FDI_RX_BIT_LOCK ||
3617 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
3618 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3619 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
3620 i);
3621 break;
3622 }
3623 udelay(1); /* should be 0.5us */
3624 }
3625 if (i == 4) {
3626 DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
3627 continue;
3628 }
357555c0 3629
139ccd3f 3630 /* Train 2 */
357555c0
JB
3631 reg = FDI_TX_CTL(pipe);
3632 temp = I915_READ(reg);
139ccd3f
JB
3633 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3634 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
3635 I915_WRITE(reg, temp);
3636
3637 reg = FDI_RX_CTL(pipe);
3638 temp = I915_READ(reg);
3639 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3640 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
357555c0
JB
3641 I915_WRITE(reg, temp);
3642
3643 POSTING_READ(reg);
139ccd3f 3644 udelay(2); /* should be 1.5us */
357555c0 3645
139ccd3f
JB
3646 for (i = 0; i < 4; i++) {
3647 reg = FDI_RX_IIR(pipe);
3648 temp = I915_READ(reg);
3649 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
357555c0 3650
139ccd3f
JB
3651 if (temp & FDI_RX_SYMBOL_LOCK ||
3652 (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
3653 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3654 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
3655 i);
3656 goto train_done;
3657 }
3658 udelay(2); /* should be 1.5us */
357555c0 3659 }
139ccd3f
JB
3660 if (i == 4)
3661 DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
357555c0 3662 }
357555c0 3663
139ccd3f 3664train_done:
357555c0
JB
3665 DRM_DEBUG_KMS("FDI train done.\n");
3666}
3667
88cefb6c 3668static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
2c07245f 3669{
88cefb6c 3670 struct drm_device *dev = intel_crtc->base.dev;
2c07245f 3671 struct drm_i915_private *dev_priv = dev->dev_private;
2c07245f 3672 int pipe = intel_crtc->pipe;
f0f59a00
VS
3673 i915_reg_t reg;
3674 u32 temp;
c64e311e 3675
c98e9dcf 3676 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
5eddb70b
CW
3677 reg = FDI_RX_CTL(pipe);
3678 temp = I915_READ(reg);
627eb5a3 3679 temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
6e3c9717 3680 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
dfd07d72 3681 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
5eddb70b
CW
3682 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
3683
3684 POSTING_READ(reg);
c98e9dcf
JB
3685 udelay(200);
3686
3687 /* Switch from Rawclk to PCDclk */
5eddb70b
CW
3688 temp = I915_READ(reg);
3689 I915_WRITE(reg, temp | FDI_PCDCLK);
3690
3691 POSTING_READ(reg);
c98e9dcf
JB
3692 udelay(200);
3693
20749730
PZ
3694 /* Enable CPU FDI TX PLL, always on for Ironlake */
3695 reg = FDI_TX_CTL(pipe);
3696 temp = I915_READ(reg);
3697 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
3698 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
5eddb70b 3699
20749730
PZ
3700 POSTING_READ(reg);
3701 udelay(100);
6be4a607 3702 }
0e23b99d
JB
3703}
3704
88cefb6c
DV
3705static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
3706{
3707 struct drm_device *dev = intel_crtc->base.dev;
3708 struct drm_i915_private *dev_priv = dev->dev_private;
3709 int pipe = intel_crtc->pipe;
f0f59a00
VS
3710 i915_reg_t reg;
3711 u32 temp;
88cefb6c
DV
3712
3713 /* Switch from PCDclk to Rawclk */
3714 reg = FDI_RX_CTL(pipe);
3715 temp = I915_READ(reg);
3716 I915_WRITE(reg, temp & ~FDI_PCDCLK);
3717
3718 /* Disable CPU FDI TX PLL */
3719 reg = FDI_TX_CTL(pipe);
3720 temp = I915_READ(reg);
3721 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
3722
3723 POSTING_READ(reg);
3724 udelay(100);
3725
3726 reg = FDI_RX_CTL(pipe);
3727 temp = I915_READ(reg);
3728 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
3729
3730 /* Wait for the clocks to turn off. */
3731 POSTING_READ(reg);
3732 udelay(100);
3733}
3734
0fc932b8
JB
3735static void ironlake_fdi_disable(struct drm_crtc *crtc)
3736{
3737 struct drm_device *dev = crtc->dev;
3738 struct drm_i915_private *dev_priv = dev->dev_private;
3739 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3740 int pipe = intel_crtc->pipe;
f0f59a00
VS
3741 i915_reg_t reg;
3742 u32 temp;
0fc932b8
JB
3743
3744 /* disable CPU FDI tx and PCH FDI rx */
3745 reg = FDI_TX_CTL(pipe);
3746 temp = I915_READ(reg);
3747 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
3748 POSTING_READ(reg);
3749
3750 reg = FDI_RX_CTL(pipe);
3751 temp = I915_READ(reg);
3752 temp &= ~(0x7 << 16);
dfd07d72 3753 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
0fc932b8
JB
3754 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
3755
3756 POSTING_READ(reg);
3757 udelay(100);
3758
3759 /* Ironlake workaround, disable clock pointer after downing FDI */
eba905b2 3760 if (HAS_PCH_IBX(dev))
6f06ce18 3761 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
0fc932b8
JB
3762
3763 /* still set train pattern 1 */
3764 reg = FDI_TX_CTL(pipe);
3765 temp = I915_READ(reg);
3766 temp &= ~FDI_LINK_TRAIN_NONE;
3767 temp |= FDI_LINK_TRAIN_PATTERN_1;
3768 I915_WRITE(reg, temp);
3769
3770 reg = FDI_RX_CTL(pipe);
3771 temp = I915_READ(reg);
3772 if (HAS_PCH_CPT(dev)) {
3773 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3774 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3775 } else {
3776 temp &= ~FDI_LINK_TRAIN_NONE;
3777 temp |= FDI_LINK_TRAIN_PATTERN_1;
3778 }
3779 /* BPC in FDI rx is consistent with that in PIPECONF */
3780 temp &= ~(0x07 << 16);
dfd07d72 3781 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
0fc932b8
JB
3782 I915_WRITE(reg, temp);
3783
3784 POSTING_READ(reg);
3785 udelay(100);
3786}
3787
5dce5b93
CW
3788bool intel_has_pending_fb_unpin(struct drm_device *dev)
3789{
3790 struct intel_crtc *crtc;
3791
3792 /* Note that we don't need to be called with mode_config.lock here
3793 * as our list of CRTC objects is static for the lifetime of the
3794 * device and so cannot disappear as we iterate. Similarly, we can
3795 * happily treat the predicates as racy, atomic checks as userspace
3796 * cannot claim and pin a new fb without at least acquring the
3797 * struct_mutex and so serialising with us.
3798 */
d3fcc808 3799 for_each_intel_crtc(dev, crtc) {
5dce5b93
CW
3800 if (atomic_read(&crtc->unpin_work_count) == 0)
3801 continue;
3802
5a21b665 3803 if (crtc->flip_work)
5dce5b93
CW
3804 intel_wait_for_vblank(dev, crtc->pipe);
3805
3806 return true;
3807 }
3808
3809 return false;
3810}
3811
5a21b665 3812static void page_flip_completed(struct intel_crtc *intel_crtc)
d6bbafa1
CW
3813{
3814 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
5a21b665
DV
3815 struct intel_flip_work *work = intel_crtc->flip_work;
3816
3817 intel_crtc->flip_work = NULL;
d6bbafa1
CW
3818
3819 if (work->event)
560ce1dc 3820 drm_crtc_send_vblank_event(&intel_crtc->base, work->event);
d6bbafa1
CW
3821
3822 drm_crtc_vblank_put(&intel_crtc->base);
3823
5a21b665 3824 wake_up_all(&dev_priv->pending_flip_queue);
143f73b3 3825 queue_work(dev_priv->wq, &work->unpin_work);
5a21b665
DV
3826
3827 trace_i915_flip_complete(intel_crtc->plane,
3828 work->pending_flip_obj);
d6bbafa1
CW
3829}
3830
5008e874 3831static int intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
e6c3a2a6 3832{
0f91128d 3833 struct drm_device *dev = crtc->dev;
5bb61643 3834 struct drm_i915_private *dev_priv = dev->dev_private;
5008e874 3835 long ret;
e6c3a2a6 3836
2c10d571 3837 WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
5008e874
ML
3838
3839 ret = wait_event_interruptible_timeout(
3840 dev_priv->pending_flip_queue,
3841 !intel_crtc_has_pending_flip(crtc),
3842 60*HZ);
3843
3844 if (ret < 0)
3845 return ret;
3846
5a21b665
DV
3847 if (ret == 0) {
3848 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3849 struct intel_flip_work *work;
3850
3851 spin_lock_irq(&dev->event_lock);
3852 work = intel_crtc->flip_work;
3853 if (work && !is_mmio_work(work)) {
3854 WARN_ONCE(1, "Removing stuck page flip\n");
3855 page_flip_completed(intel_crtc);
3856 }
3857 spin_unlock_irq(&dev->event_lock);
3858 }
5bb61643 3859
5008e874 3860 return 0;
e6c3a2a6
CW
3861}
3862
060f02d8
VS
3863static void lpt_disable_iclkip(struct drm_i915_private *dev_priv)
3864{
3865 u32 temp;
3866
3867 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
3868
3869 mutex_lock(&dev_priv->sb_lock);
3870
3871 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
3872 temp |= SBI_SSCCTL_DISABLE;
3873 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
3874
3875 mutex_unlock(&dev_priv->sb_lock);
3876}
3877
e615efe4
ED
3878/* Program iCLKIP clock to the desired frequency */
3879static void lpt_program_iclkip(struct drm_crtc *crtc)
3880{
64b46a06 3881 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
6e3c9717 3882 int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
e615efe4
ED
3883 u32 divsel, phaseinc, auxdiv, phasedir = 0;
3884 u32 temp;
3885
060f02d8 3886 lpt_disable_iclkip(dev_priv);
e615efe4 3887
64b46a06
VS
3888 /* The iCLK virtual clock root frequency is in MHz,
3889 * but the adjusted_mode->crtc_clock in in KHz. To get the
3890 * divisors, it is necessary to divide one by another, so we
3891 * convert the virtual clock precision to KHz here for higher
3892 * precision.
3893 */
3894 for (auxdiv = 0; auxdiv < 2; auxdiv++) {
e615efe4
ED
3895 u32 iclk_virtual_root_freq = 172800 * 1000;
3896 u32 iclk_pi_range = 64;
64b46a06 3897 u32 desired_divisor;
e615efe4 3898
64b46a06
VS
3899 desired_divisor = DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
3900 clock << auxdiv);
3901 divsel = (desired_divisor / iclk_pi_range) - 2;
3902 phaseinc = desired_divisor % iclk_pi_range;
e615efe4 3903
64b46a06
VS
3904 /*
3905 * Near 20MHz is a corner case which is
3906 * out of range for the 7-bit divisor
3907 */
3908 if (divsel <= 0x7f)
3909 break;
e615efe4
ED
3910 }
3911
3912 /* This should not happen with any sane values */
3913 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
3914 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
3915 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
3916 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
3917
3918 DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
12d7ceed 3919 clock,
e615efe4
ED
3920 auxdiv,
3921 divsel,
3922 phasedir,
3923 phaseinc);
3924
060f02d8
VS
3925 mutex_lock(&dev_priv->sb_lock);
3926
e615efe4 3927 /* Program SSCDIVINTPHASE6 */
988d6ee8 3928 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
e615efe4
ED
3929 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
3930 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
3931 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
3932 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
3933 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
3934 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
988d6ee8 3935 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
e615efe4
ED
3936
3937 /* Program SSCAUXDIV */
988d6ee8 3938 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
e615efe4
ED
3939 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
3940 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
988d6ee8 3941 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
e615efe4
ED
3942
3943 /* Enable modulator and associated divider */
988d6ee8 3944 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
e615efe4 3945 temp &= ~SBI_SSCCTL_DISABLE;
988d6ee8 3946 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
e615efe4 3947
060f02d8
VS
3948 mutex_unlock(&dev_priv->sb_lock);
3949
e615efe4
ED
3950 /* Wait for initialization time */
3951 udelay(24);
3952
3953 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
3954}
3955
8802e5b6
VS
3956int lpt_get_iclkip(struct drm_i915_private *dev_priv)
3957{
3958 u32 divsel, phaseinc, auxdiv;
3959 u32 iclk_virtual_root_freq = 172800 * 1000;
3960 u32 iclk_pi_range = 64;
3961 u32 desired_divisor;
3962 u32 temp;
3963
3964 if ((I915_READ(PIXCLK_GATE) & PIXCLK_GATE_UNGATE) == 0)
3965 return 0;
3966
3967 mutex_lock(&dev_priv->sb_lock);
3968
3969 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
3970 if (temp & SBI_SSCCTL_DISABLE) {
3971 mutex_unlock(&dev_priv->sb_lock);
3972 return 0;
3973 }
3974
3975 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
3976 divsel = (temp & SBI_SSCDIVINTPHASE_DIVSEL_MASK) >>
3977 SBI_SSCDIVINTPHASE_DIVSEL_SHIFT;
3978 phaseinc = (temp & SBI_SSCDIVINTPHASE_INCVAL_MASK) >>
3979 SBI_SSCDIVINTPHASE_INCVAL_SHIFT;
3980
3981 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
3982 auxdiv = (temp & SBI_SSCAUXDIV_FINALDIV2SEL_MASK) >>
3983 SBI_SSCAUXDIV_FINALDIV2SEL_SHIFT;
3984
3985 mutex_unlock(&dev_priv->sb_lock);
3986
3987 desired_divisor = (divsel + 2) * iclk_pi_range + phaseinc;
3988
3989 return DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
3990 desired_divisor << auxdiv);
3991}
3992
275f01b2
DV
3993static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
3994 enum pipe pch_transcoder)
3995{
3996 struct drm_device *dev = crtc->base.dev;
3997 struct drm_i915_private *dev_priv = dev->dev_private;
6e3c9717 3998 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
275f01b2
DV
3999
4000 I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
4001 I915_READ(HTOTAL(cpu_transcoder)));
4002 I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
4003 I915_READ(HBLANK(cpu_transcoder)));
4004 I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
4005 I915_READ(HSYNC(cpu_transcoder)));
4006
4007 I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
4008 I915_READ(VTOTAL(cpu_transcoder)));
4009 I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
4010 I915_READ(VBLANK(cpu_transcoder)));
4011 I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
4012 I915_READ(VSYNC(cpu_transcoder)));
4013 I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
4014 I915_READ(VSYNCSHIFT(cpu_transcoder)));
4015}
4016
003632d9 4017static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
1fbc0d78
DV
4018{
4019 struct drm_i915_private *dev_priv = dev->dev_private;
4020 uint32_t temp;
4021
4022 temp = I915_READ(SOUTH_CHICKEN1);
003632d9 4023 if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
1fbc0d78
DV
4024 return;
4025
4026 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
4027 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
4028
003632d9
ACO
4029 temp &= ~FDI_BC_BIFURCATION_SELECT;
4030 if (enable)
4031 temp |= FDI_BC_BIFURCATION_SELECT;
4032
4033 DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
1fbc0d78
DV
4034 I915_WRITE(SOUTH_CHICKEN1, temp);
4035 POSTING_READ(SOUTH_CHICKEN1);
4036}
4037
4038static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
4039{
4040 struct drm_device *dev = intel_crtc->base.dev;
1fbc0d78
DV
4041
4042 switch (intel_crtc->pipe) {
4043 case PIPE_A:
4044 break;
4045 case PIPE_B:
6e3c9717 4046 if (intel_crtc->config->fdi_lanes > 2)
003632d9 4047 cpt_set_fdi_bc_bifurcation(dev, false);
1fbc0d78 4048 else
003632d9 4049 cpt_set_fdi_bc_bifurcation(dev, true);
1fbc0d78
DV
4050
4051 break;
4052 case PIPE_C:
003632d9 4053 cpt_set_fdi_bc_bifurcation(dev, true);
1fbc0d78
DV
4054
4055 break;
4056 default:
4057 BUG();
4058 }
4059}
4060
c48b5305
VS
4061/* Return which DP Port should be selected for Transcoder DP control */
4062static enum port
4063intel_trans_dp_port_sel(struct drm_crtc *crtc)
4064{
4065 struct drm_device *dev = crtc->dev;
4066 struct intel_encoder *encoder;
4067
4068 for_each_encoder_on_crtc(dev, crtc, encoder) {
4069 if (encoder->type == INTEL_OUTPUT_DISPLAYPORT ||
4070 encoder->type == INTEL_OUTPUT_EDP)
4071 return enc_to_dig_port(&encoder->base)->port;
4072 }
4073
4074 return -1;
4075}
4076
f67a559d
JB
4077/*
4078 * Enable PCH resources required for PCH ports:
4079 * - PCH PLLs
4080 * - FDI training & RX/TX
4081 * - update transcoder timings
4082 * - DP transcoding bits
4083 * - transcoder
4084 */
4085static void ironlake_pch_enable(struct drm_crtc *crtc)
0e23b99d
JB
4086{
4087 struct drm_device *dev = crtc->dev;
4088 struct drm_i915_private *dev_priv = dev->dev_private;
4089 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4090 int pipe = intel_crtc->pipe;
f0f59a00 4091 u32 temp;
2c07245f 4092
ab9412ba 4093 assert_pch_transcoder_disabled(dev_priv, pipe);
e7e164db 4094
1fbc0d78
DV
4095 if (IS_IVYBRIDGE(dev))
4096 ivybridge_update_fdi_bc_bifurcation(intel_crtc);
4097
cd986abb
DV
4098 /* Write the TU size bits before fdi link training, so that error
4099 * detection works. */
4100 I915_WRITE(FDI_RX_TUSIZE1(pipe),
4101 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
4102
c98e9dcf 4103 /* For PCH output, training FDI link */
674cf967 4104 dev_priv->display.fdi_link_train(crtc);
2c07245f 4105
3ad8a208
DV
4106 /* We need to program the right clock selection before writing the pixel
4107 * mutliplier into the DPLL. */
303b81e0 4108 if (HAS_PCH_CPT(dev)) {
ee7b9f93 4109 u32 sel;
4b645f14 4110
c98e9dcf 4111 temp = I915_READ(PCH_DPLL_SEL);
11887397
DV
4112 temp |= TRANS_DPLL_ENABLE(pipe);
4113 sel = TRANS_DPLLB_SEL(pipe);
8106ddbd
ACO
4114 if (intel_crtc->config->shared_dpll ==
4115 intel_get_shared_dpll_by_id(dev_priv, DPLL_ID_PCH_PLL_B))
ee7b9f93
JB
4116 temp |= sel;
4117 else
4118 temp &= ~sel;
c98e9dcf 4119 I915_WRITE(PCH_DPLL_SEL, temp);
c98e9dcf 4120 }
5eddb70b 4121
3ad8a208
DV
4122 /* XXX: pch pll's can be enabled any time before we enable the PCH
4123 * transcoder, and we actually should do this to not upset any PCH
4124 * transcoder that already use the clock when we share it.
4125 *
4126 * Note that enable_shared_dpll tries to do the right thing, but
4127 * get_shared_dpll unconditionally resets the pll - we need that to have
4128 * the right LVDS enable sequence. */
85b3894f 4129 intel_enable_shared_dpll(intel_crtc);
3ad8a208 4130
d9b6cb56
JB
4131 /* set transcoder timing, panel must allow it */
4132 assert_panel_unlocked(dev_priv, pipe);
275f01b2 4133 ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
8db9d77b 4134
303b81e0 4135 intel_fdi_normal_train(crtc);
5e84e1a4 4136
c98e9dcf 4137 /* For PCH DP, enable TRANS_DP_CTL */
6e3c9717 4138 if (HAS_PCH_CPT(dev) && intel_crtc->config->has_dp_encoder) {
9c4edaee
VS
4139 const struct drm_display_mode *adjusted_mode =
4140 &intel_crtc->config->base.adjusted_mode;
dfd07d72 4141 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
f0f59a00 4142 i915_reg_t reg = TRANS_DP_CTL(pipe);
5eddb70b
CW
4143 temp = I915_READ(reg);
4144 temp &= ~(TRANS_DP_PORT_SEL_MASK |
220cad3c
EA
4145 TRANS_DP_SYNC_MASK |
4146 TRANS_DP_BPC_MASK);
e3ef4479 4147 temp |= TRANS_DP_OUTPUT_ENABLE;
9325c9f0 4148 temp |= bpc << 9; /* same format but at 11:9 */
c98e9dcf 4149
9c4edaee 4150 if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
5eddb70b 4151 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
9c4edaee 4152 if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
5eddb70b 4153 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
c98e9dcf
JB
4154
4155 switch (intel_trans_dp_port_sel(crtc)) {
c48b5305 4156 case PORT_B:
5eddb70b 4157 temp |= TRANS_DP_PORT_SEL_B;
c98e9dcf 4158 break;
c48b5305 4159 case PORT_C:
5eddb70b 4160 temp |= TRANS_DP_PORT_SEL_C;
c98e9dcf 4161 break;
c48b5305 4162 case PORT_D:
5eddb70b 4163 temp |= TRANS_DP_PORT_SEL_D;
c98e9dcf
JB
4164 break;
4165 default:
e95d41e1 4166 BUG();
32f9d658 4167 }
2c07245f 4168
5eddb70b 4169 I915_WRITE(reg, temp);
6be4a607 4170 }
b52eb4dc 4171
b8a4f404 4172 ironlake_enable_pch_transcoder(dev_priv, pipe);
f67a559d
JB
4173}
4174
1507e5bd
PZ
4175static void lpt_pch_enable(struct drm_crtc *crtc)
4176{
4177 struct drm_device *dev = crtc->dev;
4178 struct drm_i915_private *dev_priv = dev->dev_private;
4179 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6e3c9717 4180 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
1507e5bd 4181
ab9412ba 4182 assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
1507e5bd 4183
8c52b5e8 4184 lpt_program_iclkip(crtc);
1507e5bd 4185
0540e488 4186 /* Set transcoder timing. */
275f01b2 4187 ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
1507e5bd 4188
937bb610 4189 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
f67a559d
JB
4190}
4191
a1520318 4192static void cpt_verify_modeset(struct drm_device *dev, int pipe)
d4270e57
JB
4193{
4194 struct drm_i915_private *dev_priv = dev->dev_private;
f0f59a00 4195 i915_reg_t dslreg = PIPEDSL(pipe);
d4270e57
JB
4196 u32 temp;
4197
4198 temp = I915_READ(dslreg);
4199 udelay(500);
4200 if (wait_for(I915_READ(dslreg) != temp, 5)) {
d4270e57 4201 if (wait_for(I915_READ(dslreg) != temp, 5))
84f44ce7 4202 DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
d4270e57
JB
4203 }
4204}
4205
86adf9d7
ML
4206static int
4207skl_update_scaler(struct intel_crtc_state *crtc_state, bool force_detach,
4208 unsigned scaler_user, int *scaler_id, unsigned int rotation,
4209 int src_w, int src_h, int dst_w, int dst_h)
a1b2278e 4210{
86adf9d7
ML
4211 struct intel_crtc_scaler_state *scaler_state =
4212 &crtc_state->scaler_state;
4213 struct intel_crtc *intel_crtc =
4214 to_intel_crtc(crtc_state->base.crtc);
a1b2278e 4215 int need_scaling;
6156a456
CK
4216
4217 need_scaling = intel_rotation_90_or_270(rotation) ?
4218 (src_h != dst_w || src_w != dst_h):
4219 (src_w != dst_w || src_h != dst_h);
a1b2278e
CK
4220
4221 /*
4222 * if plane is being disabled or scaler is no more required or force detach
4223 * - free scaler binded to this plane/crtc
4224 * - in order to do this, update crtc->scaler_usage
4225 *
4226 * Here scaler state in crtc_state is set free so that
4227 * scaler can be assigned to other user. Actual register
4228 * update to free the scaler is done in plane/panel-fit programming.
4229 * For this purpose crtc/plane_state->scaler_id isn't reset here.
4230 */
86adf9d7 4231 if (force_detach || !need_scaling) {
a1b2278e 4232 if (*scaler_id >= 0) {
86adf9d7 4233 scaler_state->scaler_users &= ~(1 << scaler_user);
a1b2278e
CK
4234 scaler_state->scalers[*scaler_id].in_use = 0;
4235
86adf9d7
ML
4236 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4237 "Staged freeing scaler id %d scaler_users = 0x%x\n",
4238 intel_crtc->pipe, scaler_user, *scaler_id,
a1b2278e
CK
4239 scaler_state->scaler_users);
4240 *scaler_id = -1;
4241 }
4242 return 0;
4243 }
4244
4245 /* range checks */
4246 if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
4247 dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
4248
4249 src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
4250 dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
86adf9d7 4251 DRM_DEBUG_KMS("scaler_user index %u.%u: src %ux%u dst %ux%u "
a1b2278e 4252 "size is out of scaler range\n",
86adf9d7 4253 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h);
a1b2278e
CK
4254 return -EINVAL;
4255 }
4256
86adf9d7
ML
4257 /* mark this plane as a scaler user in crtc_state */
4258 scaler_state->scaler_users |= (1 << scaler_user);
4259 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4260 "staged scaling request for %ux%u->%ux%u scaler_users = 0x%x\n",
4261 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h,
4262 scaler_state->scaler_users);
4263
4264 return 0;
4265}
4266
4267/**
4268 * skl_update_scaler_crtc - Stages update to scaler state for a given crtc.
4269 *
4270 * @state: crtc's scaler state
86adf9d7
ML
4271 *
4272 * Return
4273 * 0 - scaler_usage updated successfully
4274 * error - requested scaling cannot be supported or other error condition
4275 */
e435d6e5 4276int skl_update_scaler_crtc(struct intel_crtc_state *state)
86adf9d7
ML
4277{
4278 struct intel_crtc *intel_crtc = to_intel_crtc(state->base.crtc);
7c5f93b0 4279 const struct drm_display_mode *adjusted_mode = &state->base.adjusted_mode;
86adf9d7 4280
78108b7c
VS
4281 DRM_DEBUG_KMS("Updating scaler for [CRTC:%d:%s] scaler_user index %u.%u\n",
4282 intel_crtc->base.base.id, intel_crtc->base.name,
4283 intel_crtc->pipe, SKL_CRTC_INDEX);
86adf9d7 4284
e435d6e5 4285 return skl_update_scaler(state, !state->base.active, SKL_CRTC_INDEX,
fa5a7970 4286 &state->scaler_state.scaler_id, BIT(DRM_ROTATE_0),
86adf9d7 4287 state->pipe_src_w, state->pipe_src_h,
aad941d5 4288 adjusted_mode->crtc_hdisplay, adjusted_mode->crtc_vdisplay);
86adf9d7
ML
4289}
4290
4291/**
4292 * skl_update_scaler_plane - Stages update to scaler state for a given plane.
4293 *
4294 * @state: crtc's scaler state
86adf9d7
ML
4295 * @plane_state: atomic plane state to update
4296 *
4297 * Return
4298 * 0 - scaler_usage updated successfully
4299 * error - requested scaling cannot be supported or other error condition
4300 */
da20eabd
ML
4301static int skl_update_scaler_plane(struct intel_crtc_state *crtc_state,
4302 struct intel_plane_state *plane_state)
86adf9d7
ML
4303{
4304
4305 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
da20eabd
ML
4306 struct intel_plane *intel_plane =
4307 to_intel_plane(plane_state->base.plane);
86adf9d7
ML
4308 struct drm_framebuffer *fb = plane_state->base.fb;
4309 int ret;
4310
4311 bool force_detach = !fb || !plane_state->visible;
4312
4313 DRM_DEBUG_KMS("Updating scaler for [PLANE:%d] scaler_user index %u.%u\n",
4314 intel_plane->base.base.id, intel_crtc->pipe,
4315 drm_plane_index(&intel_plane->base));
4316
4317 ret = skl_update_scaler(crtc_state, force_detach,
4318 drm_plane_index(&intel_plane->base),
4319 &plane_state->scaler_id,
4320 plane_state->base.rotation,
4321 drm_rect_width(&plane_state->src) >> 16,
4322 drm_rect_height(&plane_state->src) >> 16,
4323 drm_rect_width(&plane_state->dst),
4324 drm_rect_height(&plane_state->dst));
4325
4326 if (ret || plane_state->scaler_id < 0)
4327 return ret;
4328
a1b2278e 4329 /* check colorkey */
818ed961 4330 if (plane_state->ckey.flags != I915_SET_COLORKEY_NONE) {
86adf9d7 4331 DRM_DEBUG_KMS("[PLANE:%d] scaling with color key not allowed",
818ed961 4332 intel_plane->base.base.id);
a1b2278e
CK
4333 return -EINVAL;
4334 }
4335
4336 /* Check src format */
86adf9d7
ML
4337 switch (fb->pixel_format) {
4338 case DRM_FORMAT_RGB565:
4339 case DRM_FORMAT_XBGR8888:
4340 case DRM_FORMAT_XRGB8888:
4341 case DRM_FORMAT_ABGR8888:
4342 case DRM_FORMAT_ARGB8888:
4343 case DRM_FORMAT_XRGB2101010:
4344 case DRM_FORMAT_XBGR2101010:
4345 case DRM_FORMAT_YUYV:
4346 case DRM_FORMAT_YVYU:
4347 case DRM_FORMAT_UYVY:
4348 case DRM_FORMAT_VYUY:
4349 break;
4350 default:
4351 DRM_DEBUG_KMS("[PLANE:%d] FB:%d unsupported scaling format 0x%x\n",
4352 intel_plane->base.base.id, fb->base.id, fb->pixel_format);
4353 return -EINVAL;
a1b2278e
CK
4354 }
4355
a1b2278e
CK
4356 return 0;
4357}
4358
e435d6e5
ML
4359static void skylake_scaler_disable(struct intel_crtc *crtc)
4360{
4361 int i;
4362
4363 for (i = 0; i < crtc->num_scalers; i++)
4364 skl_detach_scaler(crtc, i);
4365}
4366
4367static void skylake_pfit_enable(struct intel_crtc *crtc)
bd2e244f
JB
4368{
4369 struct drm_device *dev = crtc->base.dev;
4370 struct drm_i915_private *dev_priv = dev->dev_private;
4371 int pipe = crtc->pipe;
a1b2278e
CK
4372 struct intel_crtc_scaler_state *scaler_state =
4373 &crtc->config->scaler_state;
4374
4375 DRM_DEBUG_KMS("for crtc_state = %p\n", crtc->config);
4376
6e3c9717 4377 if (crtc->config->pch_pfit.enabled) {
a1b2278e
CK
4378 int id;
4379
4380 if (WARN_ON(crtc->config->scaler_state.scaler_id < 0)) {
4381 DRM_ERROR("Requesting pfit without getting a scaler first\n");
4382 return;
4383 }
4384
4385 id = scaler_state->scaler_id;
4386 I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
4387 PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
4388 I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
4389 I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
4390
4391 DRM_DEBUG_KMS("for crtc_state = %p scaler_id = %d\n", crtc->config, id);
bd2e244f
JB
4392 }
4393}
4394
b074cec8
JB
4395static void ironlake_pfit_enable(struct intel_crtc *crtc)
4396{
4397 struct drm_device *dev = crtc->base.dev;
4398 struct drm_i915_private *dev_priv = dev->dev_private;
4399 int pipe = crtc->pipe;
4400
6e3c9717 4401 if (crtc->config->pch_pfit.enabled) {
b074cec8
JB
4402 /* Force use of hard-coded filter coefficients
4403 * as some pre-programmed values are broken,
4404 * e.g. x201.
4405 */
4406 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
4407 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
4408 PF_PIPE_SEL_IVB(pipe));
4409 else
4410 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
6e3c9717
ACO
4411 I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
4412 I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
d4270e57
JB
4413 }
4414}
4415
20bc8673 4416void hsw_enable_ips(struct intel_crtc *crtc)
d77e4531 4417{
cea165c3
VS
4418 struct drm_device *dev = crtc->base.dev;
4419 struct drm_i915_private *dev_priv = dev->dev_private;
d77e4531 4420
6e3c9717 4421 if (!crtc->config->ips_enabled)
d77e4531
PZ
4422 return;
4423
307e4498
ML
4424 /*
4425 * We can only enable IPS after we enable a plane and wait for a vblank
4426 * This function is called from post_plane_update, which is run after
4427 * a vblank wait.
4428 */
cea165c3 4429
d77e4531 4430 assert_plane_enabled(dev_priv, crtc->plane);
cea165c3 4431 if (IS_BROADWELL(dev)) {
2a114cc1
BW
4432 mutex_lock(&dev_priv->rps.hw_lock);
4433 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
4434 mutex_unlock(&dev_priv->rps.hw_lock);
4435 /* Quoting Art Runyan: "its not safe to expect any particular
4436 * value in IPS_CTL bit 31 after enabling IPS through the
e59150dc
JB
4437 * mailbox." Moreover, the mailbox may return a bogus state,
4438 * so we need to just enable it and continue on.
2a114cc1
BW
4439 */
4440 } else {
4441 I915_WRITE(IPS_CTL, IPS_ENABLE);
4442 /* The bit only becomes 1 in the next vblank, so this wait here
4443 * is essentially intel_wait_for_vblank. If we don't have this
4444 * and don't wait for vblanks until the end of crtc_enable, then
4445 * the HW state readout code will complain that the expected
4446 * IPS_CTL value is not the one we read. */
4447 if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
4448 DRM_ERROR("Timed out waiting for IPS enable\n");
4449 }
d77e4531
PZ
4450}
4451
20bc8673 4452void hsw_disable_ips(struct intel_crtc *crtc)
d77e4531
PZ
4453{
4454 struct drm_device *dev = crtc->base.dev;
4455 struct drm_i915_private *dev_priv = dev->dev_private;
4456
6e3c9717 4457 if (!crtc->config->ips_enabled)
d77e4531
PZ
4458 return;
4459
4460 assert_plane_enabled(dev_priv, crtc->plane);
23d0b130 4461 if (IS_BROADWELL(dev)) {
2a114cc1
BW
4462 mutex_lock(&dev_priv->rps.hw_lock);
4463 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
4464 mutex_unlock(&dev_priv->rps.hw_lock);
23d0b130
BW
4465 /* wait for pcode to finish disabling IPS, which may take up to 42ms */
4466 if (wait_for((I915_READ(IPS_CTL) & IPS_ENABLE) == 0, 42))
4467 DRM_ERROR("Timed out waiting for IPS disable\n");
e59150dc 4468 } else {
2a114cc1 4469 I915_WRITE(IPS_CTL, 0);
e59150dc
JB
4470 POSTING_READ(IPS_CTL);
4471 }
d77e4531
PZ
4472
4473 /* We need to wait for a vblank before we can disable the plane. */
4474 intel_wait_for_vblank(dev, crtc->pipe);
4475}
4476
7cac945f 4477static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
d3eedb1a 4478{
7cac945f 4479 if (intel_crtc->overlay) {
d3eedb1a
VS
4480 struct drm_device *dev = intel_crtc->base.dev;
4481 struct drm_i915_private *dev_priv = dev->dev_private;
4482
4483 mutex_lock(&dev->struct_mutex);
4484 dev_priv->mm.interruptible = false;
4485 (void) intel_overlay_switch_off(intel_crtc->overlay);
4486 dev_priv->mm.interruptible = true;
4487 mutex_unlock(&dev->struct_mutex);
4488 }
4489
4490 /* Let userspace switch the overlay on again. In most cases userspace
4491 * has to recompute where to put it anyway.
4492 */
4493}
4494
87d4300a
ML
4495/**
4496 * intel_post_enable_primary - Perform operations after enabling primary plane
4497 * @crtc: the CRTC whose primary plane was just enabled
4498 *
4499 * Performs potentially sleeping operations that must be done after the primary
4500 * plane is enabled, such as updating FBC and IPS. Note that this may be
4501 * called due to an explicit primary plane update, or due to an implicit
4502 * re-enable that is caused when a sprite plane is updated to no longer
4503 * completely hide the primary plane.
4504 */
4505static void
4506intel_post_enable_primary(struct drm_crtc *crtc)
a5c4d7bc
VS
4507{
4508 struct drm_device *dev = crtc->dev;
87d4300a 4509 struct drm_i915_private *dev_priv = dev->dev_private;
a5c4d7bc
VS
4510 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4511 int pipe = intel_crtc->pipe;
a5c4d7bc 4512
87d4300a
ML
4513 /*
4514 * FIXME IPS should be fine as long as one plane is
4515 * enabled, but in practice it seems to have problems
4516 * when going from primary only to sprite only and vice
4517 * versa.
4518 */
a5c4d7bc
VS
4519 hsw_enable_ips(intel_crtc);
4520
f99d7069 4521 /*
87d4300a
ML
4522 * Gen2 reports pipe underruns whenever all planes are disabled.
4523 * So don't enable underrun reporting before at least some planes
4524 * are enabled.
4525 * FIXME: Need to fix the logic to work when we turn off all planes
4526 * but leave the pipe running.
f99d7069 4527 */
87d4300a
ML
4528 if (IS_GEN2(dev))
4529 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4530
aca7b684
VS
4531 /* Underruns don't always raise interrupts, so check manually. */
4532 intel_check_cpu_fifo_underruns(dev_priv);
4533 intel_check_pch_fifo_underruns(dev_priv);
a5c4d7bc
VS
4534}
4535
2622a081 4536/* FIXME move all this to pre_plane_update() with proper state tracking */
87d4300a
ML
4537static void
4538intel_pre_disable_primary(struct drm_crtc *crtc)
a5c4d7bc
VS
4539{
4540 struct drm_device *dev = crtc->dev;
4541 struct drm_i915_private *dev_priv = dev->dev_private;
4542 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4543 int pipe = intel_crtc->pipe;
a5c4d7bc 4544
87d4300a
ML
4545 /*
4546 * Gen2 reports pipe underruns whenever all planes are disabled.
4547 * So diasble underrun reporting before all the planes get disabled.
4548 * FIXME: Need to fix the logic to work when we turn off all planes
4549 * but leave the pipe running.
4550 */
4551 if (IS_GEN2(dev))
4552 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
a5c4d7bc 4553
2622a081
VS
4554 /*
4555 * FIXME IPS should be fine as long as one plane is
4556 * enabled, but in practice it seems to have problems
4557 * when going from primary only to sprite only and vice
4558 * versa.
4559 */
4560 hsw_disable_ips(intel_crtc);
4561}
4562
4563/* FIXME get rid of this and use pre_plane_update */
4564static void
4565intel_pre_disable_primary_noatomic(struct drm_crtc *crtc)
4566{
4567 struct drm_device *dev = crtc->dev;
4568 struct drm_i915_private *dev_priv = dev->dev_private;
4569 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4570 int pipe = intel_crtc->pipe;
4571
4572 intel_pre_disable_primary(crtc);
4573
87d4300a
ML
4574 /*
4575 * Vblank time updates from the shadow to live plane control register
4576 * are blocked if the memory self-refresh mode is active at that
4577 * moment. So to make sure the plane gets truly disabled, disable
4578 * first the self-refresh mode. The self-refresh enable bit in turn
4579 * will be checked/applied by the HW only at the next frame start
4580 * event which is after the vblank start event, so we need to have a
4581 * wait-for-vblank between disabling the plane and the pipe.
4582 */
262cd2e1 4583 if (HAS_GMCH_DISPLAY(dev)) {
87d4300a 4584 intel_set_memory_cxsr(dev_priv, false);
262cd2e1
VS
4585 dev_priv->wm.vlv.cxsr = false;
4586 intel_wait_for_vblank(dev, pipe);
4587 }
87d4300a
ML
4588}
4589
5a21b665
DV
4590static void intel_post_plane_update(struct intel_crtc_state *old_crtc_state)
4591{
4592 struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
4593 struct drm_atomic_state *old_state = old_crtc_state->base.state;
4594 struct intel_crtc_state *pipe_config =
4595 to_intel_crtc_state(crtc->base.state);
4596 struct drm_device *dev = crtc->base.dev;
4597 struct drm_plane *primary = crtc->base.primary;
4598 struct drm_plane_state *old_pri_state =
4599 drm_atomic_get_existing_plane_state(old_state, primary);
4600
4601 intel_frontbuffer_flip(dev, pipe_config->fb_bits);
4602
4603 crtc->wm.cxsr_allowed = true;
4604
4605 if (pipe_config->update_wm_post && pipe_config->base.active)
4606 intel_update_watermarks(&crtc->base);
4607
4608 if (old_pri_state) {
4609 struct intel_plane_state *primary_state =
4610 to_intel_plane_state(primary->state);
4611 struct intel_plane_state *old_primary_state =
4612 to_intel_plane_state(old_pri_state);
4613
4614 intel_fbc_post_update(crtc);
4615
4616 if (primary_state->visible &&
4617 (needs_modeset(&pipe_config->base) ||
4618 !old_primary_state->visible))
4619 intel_post_enable_primary(&crtc->base);
4620 }
4621}
4622
5c74cd73 4623static void intel_pre_plane_update(struct intel_crtc_state *old_crtc_state)
ac21b225 4624{
5c74cd73 4625 struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
ac21b225 4626 struct drm_device *dev = crtc->base.dev;
eddfcbcd 4627 struct drm_i915_private *dev_priv = dev->dev_private;
ab1d3a0e
ML
4628 struct intel_crtc_state *pipe_config =
4629 to_intel_crtc_state(crtc->base.state);
5c74cd73
ML
4630 struct drm_atomic_state *old_state = old_crtc_state->base.state;
4631 struct drm_plane *primary = crtc->base.primary;
4632 struct drm_plane_state *old_pri_state =
4633 drm_atomic_get_existing_plane_state(old_state, primary);
4634 bool modeset = needs_modeset(&pipe_config->base);
ac21b225 4635
5c74cd73
ML
4636 if (old_pri_state) {
4637 struct intel_plane_state *primary_state =
4638 to_intel_plane_state(primary->state);
4639 struct intel_plane_state *old_primary_state =
4640 to_intel_plane_state(old_pri_state);
4641
5a21b665 4642 intel_fbc_pre_update(crtc);
31ae71fc 4643
5c74cd73
ML
4644 if (old_primary_state->visible &&
4645 (modeset || !primary_state->visible))
4646 intel_pre_disable_primary(&crtc->base);
4647 }
852eb00d 4648
ab1d3a0e 4649 if (pipe_config->disable_cxsr) {
852eb00d 4650 crtc->wm.cxsr_allowed = false;
2dfd178d 4651
2622a081
VS
4652 /*
4653 * Vblank time updates from the shadow to live plane control register
4654 * are blocked if the memory self-refresh mode is active at that
4655 * moment. So to make sure the plane gets truly disabled, disable
4656 * first the self-refresh mode. The self-refresh enable bit in turn
4657 * will be checked/applied by the HW only at the next frame start
4658 * event which is after the vblank start event, so we need to have a
4659 * wait-for-vblank between disabling the plane and the pipe.
4660 */
4661 if (old_crtc_state->base.active) {
2dfd178d 4662 intel_set_memory_cxsr(dev_priv, false);
2622a081
VS
4663 dev_priv->wm.vlv.cxsr = false;
4664 intel_wait_for_vblank(dev, crtc->pipe);
4665 }
852eb00d 4666 }
92826fcd 4667
ed4a6a7c
MR
4668 /*
4669 * IVB workaround: must disable low power watermarks for at least
4670 * one frame before enabling scaling. LP watermarks can be re-enabled
4671 * when scaling is disabled.
4672 *
4673 * WaCxSRDisabledForSpriteScaling:ivb
4674 */
4675 if (pipe_config->disable_lp_wm) {
4676 ilk_disable_lp_wm(dev);
4677 intel_wait_for_vblank(dev, crtc->pipe);
4678 }
4679
4680 /*
4681 * If we're doing a modeset, we're done. No need to do any pre-vblank
4682 * watermark programming here.
4683 */
4684 if (needs_modeset(&pipe_config->base))
4685 return;
4686
4687 /*
4688 * For platforms that support atomic watermarks, program the
4689 * 'intermediate' watermarks immediately. On pre-gen9 platforms, these
4690 * will be the intermediate values that are safe for both pre- and
4691 * post- vblank; when vblank happens, the 'active' values will be set
4692 * to the final 'target' values and we'll do this again to get the
4693 * optimal watermarks. For gen9+ platforms, the values we program here
4694 * will be the final target values which will get automatically latched
4695 * at vblank time; no further programming will be necessary.
4696 *
4697 * If a platform hasn't been transitioned to atomic watermarks yet,
4698 * we'll continue to update watermarks the old way, if flags tell
4699 * us to.
4700 */
4701 if (dev_priv->display.initial_watermarks != NULL)
4702 dev_priv->display.initial_watermarks(pipe_config);
caed361d 4703 else if (pipe_config->update_wm_pre)
92826fcd 4704 intel_update_watermarks(&crtc->base);
ac21b225
ML
4705}
4706
d032ffa0 4707static void intel_crtc_disable_planes(struct drm_crtc *crtc, unsigned plane_mask)
87d4300a
ML
4708{
4709 struct drm_device *dev = crtc->dev;
4710 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
d032ffa0 4711 struct drm_plane *p;
87d4300a
ML
4712 int pipe = intel_crtc->pipe;
4713
7cac945f 4714 intel_crtc_dpms_overlay_disable(intel_crtc);
27321ae8 4715
d032ffa0
ML
4716 drm_for_each_plane_mask(p, dev, plane_mask)
4717 to_intel_plane(p)->disable_plane(p, crtc);
f98551ae 4718
f99d7069
DV
4719 /*
4720 * FIXME: Once we grow proper nuclear flip support out of this we need
4721 * to compute the mask of flip planes precisely. For the time being
4722 * consider this a flip to a NULL plane.
4723 */
4724 intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
a5c4d7bc
VS
4725}
4726
f67a559d
JB
4727static void ironlake_crtc_enable(struct drm_crtc *crtc)
4728{
4729 struct drm_device *dev = crtc->dev;
4730 struct drm_i915_private *dev_priv = dev->dev_private;
4731 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 4732 struct intel_encoder *encoder;
f67a559d 4733 int pipe = intel_crtc->pipe;
b95c5321
ML
4734 struct intel_crtc_state *pipe_config =
4735 to_intel_crtc_state(crtc->state);
f67a559d 4736
53d9f4e9 4737 if (WARN_ON(intel_crtc->active))
f67a559d
JB
4738 return;
4739
b2c0593a
VS
4740 /*
4741 * Sometimes spurious CPU pipe underruns happen during FDI
4742 * training, at least with VGA+HDMI cloning. Suppress them.
4743 *
4744 * On ILK we get an occasional spurious CPU pipe underruns
4745 * between eDP port A enable and vdd enable. Also PCH port
4746 * enable seems to result in the occasional CPU pipe underrun.
4747 *
4748 * Spurious PCH underruns also occur during PCH enabling.
4749 */
4750 if (intel_crtc->config->has_pch_encoder || IS_GEN5(dev_priv))
4751 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
81b088ca
VS
4752 if (intel_crtc->config->has_pch_encoder)
4753 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
4754
6e3c9717 4755 if (intel_crtc->config->has_pch_encoder)
b14b1055
DV
4756 intel_prepare_shared_dpll(intel_crtc);
4757
6e3c9717 4758 if (intel_crtc->config->has_dp_encoder)
fe3cd48d 4759 intel_dp_set_m_n(intel_crtc, M1_N1);
29407aab
DV
4760
4761 intel_set_pipe_timings(intel_crtc);
bc58be60 4762 intel_set_pipe_src_size(intel_crtc);
29407aab 4763
6e3c9717 4764 if (intel_crtc->config->has_pch_encoder) {
29407aab 4765 intel_cpu_transcoder_set_m_n(intel_crtc,
6e3c9717 4766 &intel_crtc->config->fdi_m_n, NULL);
29407aab
DV
4767 }
4768
4769 ironlake_set_pipeconf(crtc);
4770
f67a559d 4771 intel_crtc->active = true;
8664281b 4772
f6736a1a 4773 for_each_encoder_on_crtc(dev, crtc, encoder)
952735ee
DV
4774 if (encoder->pre_enable)
4775 encoder->pre_enable(encoder);
f67a559d 4776
6e3c9717 4777 if (intel_crtc->config->has_pch_encoder) {
fff367c7
DV
4778 /* Note: FDI PLL enabling _must_ be done before we enable the
4779 * cpu pipes, hence this is separate from all the other fdi/pch
4780 * enabling. */
88cefb6c 4781 ironlake_fdi_pll_enable(intel_crtc);
46b6f814
DV
4782 } else {
4783 assert_fdi_tx_disabled(dev_priv, pipe);
4784 assert_fdi_rx_disabled(dev_priv, pipe);
4785 }
f67a559d 4786
b074cec8 4787 ironlake_pfit_enable(intel_crtc);
f67a559d 4788
9c54c0dd
JB
4789 /*
4790 * On ILK+ LUT must be loaded before the pipe is running but with
4791 * clocks enabled
4792 */
b95c5321 4793 intel_color_load_luts(&pipe_config->base);
9c54c0dd 4794
1d5bf5d9
ID
4795 if (dev_priv->display.initial_watermarks != NULL)
4796 dev_priv->display.initial_watermarks(intel_crtc->config);
e1fdc473 4797 intel_enable_pipe(intel_crtc);
f67a559d 4798
6e3c9717 4799 if (intel_crtc->config->has_pch_encoder)
f67a559d 4800 ironlake_pch_enable(crtc);
c98e9dcf 4801
f9b61ff6
DV
4802 assert_vblank_disabled(crtc);
4803 drm_crtc_vblank_on(crtc);
4804
fa5c73b1
DV
4805 for_each_encoder_on_crtc(dev, crtc, encoder)
4806 encoder->enable(encoder);
61b77ddd
DV
4807
4808 if (HAS_PCH_CPT(dev))
a1520318 4809 cpt_verify_modeset(dev, intel_crtc->pipe);
37ca8d4c
VS
4810
4811 /* Must wait for vblank to avoid spurious PCH FIFO underruns */
4812 if (intel_crtc->config->has_pch_encoder)
4813 intel_wait_for_vblank(dev, pipe);
b2c0593a 4814 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
37ca8d4c 4815 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
6be4a607
JB
4816}
4817
42db64ef
PZ
4818/* IPS only exists on ULT machines and is tied to pipe A. */
4819static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
4820{
f5adf94e 4821 return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
42db64ef
PZ
4822}
4823
4f771f10
PZ
4824static void haswell_crtc_enable(struct drm_crtc *crtc)
4825{
4826 struct drm_device *dev = crtc->dev;
4827 struct drm_i915_private *dev_priv = dev->dev_private;
4828 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4829 struct intel_encoder *encoder;
99d736a2 4830 int pipe = intel_crtc->pipe, hsw_workaround_pipe;
4d1de975 4831 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
99d736a2
ML
4832 struct intel_crtc_state *pipe_config =
4833 to_intel_crtc_state(crtc->state);
4f771f10 4834
53d9f4e9 4835 if (WARN_ON(intel_crtc->active))
4f771f10
PZ
4836 return;
4837
81b088ca
VS
4838 if (intel_crtc->config->has_pch_encoder)
4839 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
4840 false);
4841
8106ddbd 4842 if (intel_crtc->config->shared_dpll)
df8ad70c
DV
4843 intel_enable_shared_dpll(intel_crtc);
4844
6e3c9717 4845 if (intel_crtc->config->has_dp_encoder)
fe3cd48d 4846 intel_dp_set_m_n(intel_crtc, M1_N1);
229fca97 4847
4d1de975
JN
4848 if (!intel_crtc->config->has_dsi_encoder)
4849 intel_set_pipe_timings(intel_crtc);
4850
bc58be60 4851 intel_set_pipe_src_size(intel_crtc);
229fca97 4852
4d1de975
JN
4853 if (cpu_transcoder != TRANSCODER_EDP &&
4854 !transcoder_is_dsi(cpu_transcoder)) {
4855 I915_WRITE(PIPE_MULT(cpu_transcoder),
6e3c9717 4856 intel_crtc->config->pixel_multiplier - 1);
ebb69c95
CT
4857 }
4858
6e3c9717 4859 if (intel_crtc->config->has_pch_encoder) {
229fca97 4860 intel_cpu_transcoder_set_m_n(intel_crtc,
6e3c9717 4861 &intel_crtc->config->fdi_m_n, NULL);
229fca97
DV
4862 }
4863
4d1de975
JN
4864 if (!intel_crtc->config->has_dsi_encoder)
4865 haswell_set_pipeconf(crtc);
4866
391bf048 4867 haswell_set_pipemisc(crtc);
229fca97 4868
b95c5321 4869 intel_color_set_csc(&pipe_config->base);
229fca97 4870
4f771f10 4871 intel_crtc->active = true;
8664281b 4872
6b698516
DV
4873 if (intel_crtc->config->has_pch_encoder)
4874 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
4875 else
4876 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4877
7d4aefd0 4878 for_each_encoder_on_crtc(dev, crtc, encoder) {
4f771f10
PZ
4879 if (encoder->pre_enable)
4880 encoder->pre_enable(encoder);
7d4aefd0 4881 }
4f771f10 4882
d2d65408 4883 if (intel_crtc->config->has_pch_encoder)
4fe9467d 4884 dev_priv->display.fdi_link_train(crtc);
4fe9467d 4885
a65347ba 4886 if (!intel_crtc->config->has_dsi_encoder)
7d4aefd0 4887 intel_ddi_enable_pipe_clock(intel_crtc);
4f771f10 4888
1c132b44 4889 if (INTEL_INFO(dev)->gen >= 9)
e435d6e5 4890 skylake_pfit_enable(intel_crtc);
ff6d9f55 4891 else
1c132b44 4892 ironlake_pfit_enable(intel_crtc);
4f771f10
PZ
4893
4894 /*
4895 * On ILK+ LUT must be loaded before the pipe is running but with
4896 * clocks enabled
4897 */
b95c5321 4898 intel_color_load_luts(&pipe_config->base);
4f771f10 4899
1f544388 4900 intel_ddi_set_pipe_settings(crtc);
a65347ba 4901 if (!intel_crtc->config->has_dsi_encoder)
7d4aefd0 4902 intel_ddi_enable_transcoder_func(crtc);
4f771f10 4903
1d5bf5d9
ID
4904 if (dev_priv->display.initial_watermarks != NULL)
4905 dev_priv->display.initial_watermarks(pipe_config);
4906 else
4907 intel_update_watermarks(crtc);
4d1de975
JN
4908
4909 /* XXX: Do the pipe assertions at the right place for BXT DSI. */
4910 if (!intel_crtc->config->has_dsi_encoder)
4911 intel_enable_pipe(intel_crtc);
42db64ef 4912
6e3c9717 4913 if (intel_crtc->config->has_pch_encoder)
1507e5bd 4914 lpt_pch_enable(crtc);
4f771f10 4915
a65347ba 4916 if (intel_crtc->config->dp_encoder_is_mst)
0e32b39c
DA
4917 intel_ddi_set_vc_payload_alloc(crtc, true);
4918
f9b61ff6
DV
4919 assert_vblank_disabled(crtc);
4920 drm_crtc_vblank_on(crtc);
4921
8807e55b 4922 for_each_encoder_on_crtc(dev, crtc, encoder) {
4f771f10 4923 encoder->enable(encoder);
8807e55b
JN
4924 intel_opregion_notify_encoder(encoder, true);
4925 }
4f771f10 4926
6b698516
DV
4927 if (intel_crtc->config->has_pch_encoder) {
4928 intel_wait_for_vblank(dev, pipe);
4929 intel_wait_for_vblank(dev, pipe);
4930 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
d2d65408
VS
4931 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
4932 true);
6b698516 4933 }
d2d65408 4934
e4916946
PZ
4935 /* If we change the relative order between pipe/planes enabling, we need
4936 * to change the workaround. */
99d736a2
ML
4937 hsw_workaround_pipe = pipe_config->hsw_workaround_pipe;
4938 if (IS_HASWELL(dev) && hsw_workaround_pipe != INVALID_PIPE) {
4939 intel_wait_for_vblank(dev, hsw_workaround_pipe);
4940 intel_wait_for_vblank(dev, hsw_workaround_pipe);
4941 }
4f771f10
PZ
4942}
4943
bfd16b2a 4944static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force)
3f8dce3a
DV
4945{
4946 struct drm_device *dev = crtc->base.dev;
4947 struct drm_i915_private *dev_priv = dev->dev_private;
4948 int pipe = crtc->pipe;
4949
4950 /* To avoid upsetting the power well on haswell only disable the pfit if
4951 * it's in use. The hw state code will make sure we get this right. */
bfd16b2a 4952 if (force || crtc->config->pch_pfit.enabled) {
3f8dce3a
DV
4953 I915_WRITE(PF_CTL(pipe), 0);
4954 I915_WRITE(PF_WIN_POS(pipe), 0);
4955 I915_WRITE(PF_WIN_SZ(pipe), 0);
4956 }
4957}
4958
6be4a607
JB
4959static void ironlake_crtc_disable(struct drm_crtc *crtc)
4960{
4961 struct drm_device *dev = crtc->dev;
4962 struct drm_i915_private *dev_priv = dev->dev_private;
4963 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 4964 struct intel_encoder *encoder;
6be4a607 4965 int pipe = intel_crtc->pipe;
b52eb4dc 4966
b2c0593a
VS
4967 /*
4968 * Sometimes spurious CPU pipe underruns happen when the
4969 * pipe is already disabled, but FDI RX/TX is still enabled.
4970 * Happens at least with VGA+HDMI cloning. Suppress them.
4971 */
4972 if (intel_crtc->config->has_pch_encoder) {
4973 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
37ca8d4c 4974 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
b2c0593a 4975 }
37ca8d4c 4976
ea9d758d
DV
4977 for_each_encoder_on_crtc(dev, crtc, encoder)
4978 encoder->disable(encoder);
4979
f9b61ff6
DV
4980 drm_crtc_vblank_off(crtc);
4981 assert_vblank_disabled(crtc);
4982
575f7ab7 4983 intel_disable_pipe(intel_crtc);
32f9d658 4984
bfd16b2a 4985 ironlake_pfit_disable(intel_crtc, false);
2c07245f 4986
b2c0593a 4987 if (intel_crtc->config->has_pch_encoder)
5a74f70a
VS
4988 ironlake_fdi_disable(crtc);
4989
bf49ec8c
DV
4990 for_each_encoder_on_crtc(dev, crtc, encoder)
4991 if (encoder->post_disable)
4992 encoder->post_disable(encoder);
2c07245f 4993
6e3c9717 4994 if (intel_crtc->config->has_pch_encoder) {
d925c59a 4995 ironlake_disable_pch_transcoder(dev_priv, pipe);
6be4a607 4996
d925c59a 4997 if (HAS_PCH_CPT(dev)) {
f0f59a00
VS
4998 i915_reg_t reg;
4999 u32 temp;
5000
d925c59a
DV
5001 /* disable TRANS_DP_CTL */
5002 reg = TRANS_DP_CTL(pipe);
5003 temp = I915_READ(reg);
5004 temp &= ~(TRANS_DP_OUTPUT_ENABLE |
5005 TRANS_DP_PORT_SEL_MASK);
5006 temp |= TRANS_DP_PORT_SEL_NONE;
5007 I915_WRITE(reg, temp);
5008
5009 /* disable DPLL_SEL */
5010 temp = I915_READ(PCH_DPLL_SEL);
11887397 5011 temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
d925c59a 5012 I915_WRITE(PCH_DPLL_SEL, temp);
9db4a9c7 5013 }
e3421a18 5014
d925c59a
DV
5015 ironlake_fdi_pll_disable(intel_crtc);
5016 }
81b088ca 5017
b2c0593a 5018 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
81b088ca 5019 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
6be4a607 5020}
1b3c7a47 5021
4f771f10 5022static void haswell_crtc_disable(struct drm_crtc *crtc)
ee7b9f93 5023{
4f771f10
PZ
5024 struct drm_device *dev = crtc->dev;
5025 struct drm_i915_private *dev_priv = dev->dev_private;
ee7b9f93 5026 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4f771f10 5027 struct intel_encoder *encoder;
6e3c9717 5028 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
ee7b9f93 5029
d2d65408
VS
5030 if (intel_crtc->config->has_pch_encoder)
5031 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5032 false);
5033
8807e55b
JN
5034 for_each_encoder_on_crtc(dev, crtc, encoder) {
5035 intel_opregion_notify_encoder(encoder, false);
4f771f10 5036 encoder->disable(encoder);
8807e55b 5037 }
4f771f10 5038
f9b61ff6
DV
5039 drm_crtc_vblank_off(crtc);
5040 assert_vblank_disabled(crtc);
5041
4d1de975
JN
5042 /* XXX: Do the pipe assertions at the right place for BXT DSI. */
5043 if (!intel_crtc->config->has_dsi_encoder)
5044 intel_disable_pipe(intel_crtc);
4f771f10 5045
6e3c9717 5046 if (intel_crtc->config->dp_encoder_is_mst)
a4bf214f
VS
5047 intel_ddi_set_vc_payload_alloc(crtc, false);
5048
a65347ba 5049 if (!intel_crtc->config->has_dsi_encoder)
7d4aefd0 5050 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
4f771f10 5051
1c132b44 5052 if (INTEL_INFO(dev)->gen >= 9)
e435d6e5 5053 skylake_scaler_disable(intel_crtc);
ff6d9f55 5054 else
bfd16b2a 5055 ironlake_pfit_disable(intel_crtc, false);
4f771f10 5056
a65347ba 5057 if (!intel_crtc->config->has_dsi_encoder)
7d4aefd0 5058 intel_ddi_disable_pipe_clock(intel_crtc);
4f771f10 5059
97b040aa
ID
5060 for_each_encoder_on_crtc(dev, crtc, encoder)
5061 if (encoder->post_disable)
5062 encoder->post_disable(encoder);
81b088ca 5063
92966a37
VS
5064 if (intel_crtc->config->has_pch_encoder) {
5065 lpt_disable_pch_transcoder(dev_priv);
503a74e9 5066 lpt_disable_iclkip(dev_priv);
92966a37
VS
5067 intel_ddi_fdi_disable(crtc);
5068
81b088ca
VS
5069 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5070 true);
92966a37 5071 }
4f771f10
PZ
5072}
5073
2dd24552
JB
5074static void i9xx_pfit_enable(struct intel_crtc *crtc)
5075{
5076 struct drm_device *dev = crtc->base.dev;
5077 struct drm_i915_private *dev_priv = dev->dev_private;
6e3c9717 5078 struct intel_crtc_state *pipe_config = crtc->config;
2dd24552 5079
681a8504 5080 if (!pipe_config->gmch_pfit.control)
2dd24552
JB
5081 return;
5082
2dd24552 5083 /*
c0b03411
DV
5084 * The panel fitter should only be adjusted whilst the pipe is disabled,
5085 * according to register description and PRM.
2dd24552 5086 */
c0b03411
DV
5087 WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
5088 assert_pipe_disabled(dev_priv, crtc->pipe);
2dd24552 5089
b074cec8
JB
5090 I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
5091 I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
5a80c45c
DV
5092
5093 /* Border color in case we don't scale up to the full screen. Black by
5094 * default, change to something else for debugging. */
5095 I915_WRITE(BCLRPAT(crtc->pipe), 0);
2dd24552
JB
5096}
5097
d05410f9
DA
5098static enum intel_display_power_domain port_to_power_domain(enum port port)
5099{
5100 switch (port) {
5101 case PORT_A:
6331a704 5102 return POWER_DOMAIN_PORT_DDI_A_LANES;
d05410f9 5103 case PORT_B:
6331a704 5104 return POWER_DOMAIN_PORT_DDI_B_LANES;
d05410f9 5105 case PORT_C:
6331a704 5106 return POWER_DOMAIN_PORT_DDI_C_LANES;
d05410f9 5107 case PORT_D:
6331a704 5108 return POWER_DOMAIN_PORT_DDI_D_LANES;
d8e19f99 5109 case PORT_E:
6331a704 5110 return POWER_DOMAIN_PORT_DDI_E_LANES;
d05410f9 5111 default:
b9fec167 5112 MISSING_CASE(port);
d05410f9
DA
5113 return POWER_DOMAIN_PORT_OTHER;
5114 }
5115}
5116
25f78f58
VS
5117static enum intel_display_power_domain port_to_aux_power_domain(enum port port)
5118{
5119 switch (port) {
5120 case PORT_A:
5121 return POWER_DOMAIN_AUX_A;
5122 case PORT_B:
5123 return POWER_DOMAIN_AUX_B;
5124 case PORT_C:
5125 return POWER_DOMAIN_AUX_C;
5126 case PORT_D:
5127 return POWER_DOMAIN_AUX_D;
5128 case PORT_E:
5129 /* FIXME: Check VBT for actual wiring of PORT E */
5130 return POWER_DOMAIN_AUX_D;
5131 default:
b9fec167 5132 MISSING_CASE(port);
25f78f58
VS
5133 return POWER_DOMAIN_AUX_A;
5134 }
5135}
5136
319be8ae
ID
5137enum intel_display_power_domain
5138intel_display_port_power_domain(struct intel_encoder *intel_encoder)
5139{
5140 struct drm_device *dev = intel_encoder->base.dev;
5141 struct intel_digital_port *intel_dig_port;
5142
5143 switch (intel_encoder->type) {
5144 case INTEL_OUTPUT_UNKNOWN:
5145 /* Only DDI platforms should ever use this output type */
5146 WARN_ON_ONCE(!HAS_DDI(dev));
5147 case INTEL_OUTPUT_DISPLAYPORT:
5148 case INTEL_OUTPUT_HDMI:
5149 case INTEL_OUTPUT_EDP:
5150 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
d05410f9 5151 return port_to_power_domain(intel_dig_port->port);
0e32b39c
DA
5152 case INTEL_OUTPUT_DP_MST:
5153 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
5154 return port_to_power_domain(intel_dig_port->port);
319be8ae
ID
5155 case INTEL_OUTPUT_ANALOG:
5156 return POWER_DOMAIN_PORT_CRT;
5157 case INTEL_OUTPUT_DSI:
5158 return POWER_DOMAIN_PORT_DSI;
5159 default:
5160 return POWER_DOMAIN_PORT_OTHER;
5161 }
5162}
5163
25f78f58
VS
5164enum intel_display_power_domain
5165intel_display_port_aux_power_domain(struct intel_encoder *intel_encoder)
5166{
5167 struct drm_device *dev = intel_encoder->base.dev;
5168 struct intel_digital_port *intel_dig_port;
5169
5170 switch (intel_encoder->type) {
5171 case INTEL_OUTPUT_UNKNOWN:
651174a4
ID
5172 case INTEL_OUTPUT_HDMI:
5173 /*
5174 * Only DDI platforms should ever use these output types.
5175 * We can get here after the HDMI detect code has already set
5176 * the type of the shared encoder. Since we can't be sure
5177 * what's the status of the given connectors, play safe and
5178 * run the DP detection too.
5179 */
25f78f58
VS
5180 WARN_ON_ONCE(!HAS_DDI(dev));
5181 case INTEL_OUTPUT_DISPLAYPORT:
5182 case INTEL_OUTPUT_EDP:
5183 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
5184 return port_to_aux_power_domain(intel_dig_port->port);
5185 case INTEL_OUTPUT_DP_MST:
5186 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
5187 return port_to_aux_power_domain(intel_dig_port->port);
5188 default:
b9fec167 5189 MISSING_CASE(intel_encoder->type);
25f78f58
VS
5190 return POWER_DOMAIN_AUX_A;
5191 }
5192}
5193
74bff5f9
ML
5194static unsigned long get_crtc_power_domains(struct drm_crtc *crtc,
5195 struct intel_crtc_state *crtc_state)
77d22dca 5196{
319be8ae 5197 struct drm_device *dev = crtc->dev;
74bff5f9 5198 struct drm_encoder *encoder;
319be8ae
ID
5199 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5200 enum pipe pipe = intel_crtc->pipe;
77d22dca 5201 unsigned long mask;
74bff5f9 5202 enum transcoder transcoder = crtc_state->cpu_transcoder;
77d22dca 5203
74bff5f9 5204 if (!crtc_state->base.active)
292b990e
ML
5205 return 0;
5206
77d22dca
ID
5207 mask = BIT(POWER_DOMAIN_PIPE(pipe));
5208 mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
74bff5f9
ML
5209 if (crtc_state->pch_pfit.enabled ||
5210 crtc_state->pch_pfit.force_thru)
77d22dca
ID
5211 mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
5212
74bff5f9
ML
5213 drm_for_each_encoder_mask(encoder, dev, crtc_state->base.encoder_mask) {
5214 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
5215
319be8ae 5216 mask |= BIT(intel_display_port_power_domain(intel_encoder));
74bff5f9 5217 }
319be8ae 5218
15e7ec29
ML
5219 if (crtc_state->shared_dpll)
5220 mask |= BIT(POWER_DOMAIN_PLLS);
5221
77d22dca
ID
5222 return mask;
5223}
5224
74bff5f9
ML
5225static unsigned long
5226modeset_get_crtc_power_domains(struct drm_crtc *crtc,
5227 struct intel_crtc_state *crtc_state)
77d22dca 5228{
292b990e
ML
5229 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
5230 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5231 enum intel_display_power_domain domain;
5a21b665 5232 unsigned long domains, new_domains, old_domains;
77d22dca 5233
292b990e 5234 old_domains = intel_crtc->enabled_power_domains;
74bff5f9
ML
5235 intel_crtc->enabled_power_domains = new_domains =
5236 get_crtc_power_domains(crtc, crtc_state);
77d22dca 5237
5a21b665 5238 domains = new_domains & ~old_domains;
292b990e
ML
5239
5240 for_each_power_domain(domain, domains)
5241 intel_display_power_get(dev_priv, domain);
5242
5a21b665 5243 return old_domains & ~new_domains;
292b990e
ML
5244}
5245
5246static void modeset_put_power_domains(struct drm_i915_private *dev_priv,
5247 unsigned long domains)
5248{
5249 enum intel_display_power_domain domain;
5250
5251 for_each_power_domain(domain, domains)
5252 intel_display_power_put(dev_priv, domain);
5253}
77d22dca 5254
adafdc6f
MK
5255static int intel_compute_max_dotclk(struct drm_i915_private *dev_priv)
5256{
5257 int max_cdclk_freq = dev_priv->max_cdclk_freq;
5258
5259 if (INTEL_INFO(dev_priv)->gen >= 9 ||
5260 IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
5261 return max_cdclk_freq;
5262 else if (IS_CHERRYVIEW(dev_priv))
5263 return max_cdclk_freq*95/100;
5264 else if (INTEL_INFO(dev_priv)->gen < 4)
5265 return 2*max_cdclk_freq*90/100;
5266 else
5267 return max_cdclk_freq*90/100;
5268}
5269
b2045352
VS
5270static int skl_calc_cdclk(int max_pixclk, int vco);
5271
560a7ae4
DL
5272static void intel_update_max_cdclk(struct drm_device *dev)
5273{
5274 struct drm_i915_private *dev_priv = dev->dev_private;
5275
ef11bdb3 5276 if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) {
560a7ae4 5277 u32 limit = I915_READ(SKL_DFSM) & SKL_DFSM_CDCLK_LIMIT_MASK;
b2045352
VS
5278 int max_cdclk, vco;
5279
5280 vco = dev_priv->skl_preferred_vco_freq;
63911d72 5281 WARN_ON(vco != 8100000 && vco != 8640000);
560a7ae4 5282
b2045352
VS
5283 /*
5284 * Use the lower (vco 8640) cdclk values as a
5285 * first guess. skl_calc_cdclk() will correct it
5286 * if the preferred vco is 8100 instead.
5287 */
560a7ae4 5288 if (limit == SKL_DFSM_CDCLK_LIMIT_675)
487ed2e4 5289 max_cdclk = 617143;
560a7ae4 5290 else if (limit == SKL_DFSM_CDCLK_LIMIT_540)
b2045352 5291 max_cdclk = 540000;
560a7ae4 5292 else if (limit == SKL_DFSM_CDCLK_LIMIT_450)
b2045352 5293 max_cdclk = 432000;
560a7ae4 5294 else
487ed2e4 5295 max_cdclk = 308571;
b2045352
VS
5296
5297 dev_priv->max_cdclk_freq = skl_calc_cdclk(max_cdclk, vco);
281c114f
MR
5298 } else if (IS_BROXTON(dev)) {
5299 dev_priv->max_cdclk_freq = 624000;
560a7ae4
DL
5300 } else if (IS_BROADWELL(dev)) {
5301 /*
5302 * FIXME with extra cooling we can allow
5303 * 540 MHz for ULX and 675 Mhz for ULT.
5304 * How can we know if extra cooling is
5305 * available? PCI ID, VTB, something else?
5306 */
5307 if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
5308 dev_priv->max_cdclk_freq = 450000;
5309 else if (IS_BDW_ULX(dev))
5310 dev_priv->max_cdclk_freq = 450000;
5311 else if (IS_BDW_ULT(dev))
5312 dev_priv->max_cdclk_freq = 540000;
5313 else
5314 dev_priv->max_cdclk_freq = 675000;
0904deaf
MK
5315 } else if (IS_CHERRYVIEW(dev)) {
5316 dev_priv->max_cdclk_freq = 320000;
560a7ae4
DL
5317 } else if (IS_VALLEYVIEW(dev)) {
5318 dev_priv->max_cdclk_freq = 400000;
5319 } else {
5320 /* otherwise assume cdclk is fixed */
5321 dev_priv->max_cdclk_freq = dev_priv->cdclk_freq;
5322 }
5323
adafdc6f
MK
5324 dev_priv->max_dotclk_freq = intel_compute_max_dotclk(dev_priv);
5325
560a7ae4
DL
5326 DRM_DEBUG_DRIVER("Max CD clock rate: %d kHz\n",
5327 dev_priv->max_cdclk_freq);
adafdc6f
MK
5328
5329 DRM_DEBUG_DRIVER("Max dotclock rate: %d kHz\n",
5330 dev_priv->max_dotclk_freq);
560a7ae4
DL
5331}
5332
5333static void intel_update_cdclk(struct drm_device *dev)
5334{
5335 struct drm_i915_private *dev_priv = dev->dev_private;
5336
5337 dev_priv->cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
2f2a121a 5338
83d7c81f 5339 if (INTEL_GEN(dev_priv) >= 9)
709e05c3
VS
5340 DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz, VCO: %d kHz, ref: %d kHz\n",
5341 dev_priv->cdclk_freq, dev_priv->cdclk_pll.vco,
5342 dev_priv->cdclk_pll.ref);
2f2a121a
VS
5343 else
5344 DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
5345 dev_priv->cdclk_freq);
560a7ae4
DL
5346
5347 /*
b5d99ff9
VS
5348 * 9:0 CMBUS [sic] CDCLK frequency (cdfreq):
5349 * Programmng [sic] note: bit[9:2] should be programmed to the number
5350 * of cdclk that generates 4MHz reference clock freq which is used to
5351 * generate GMBus clock. This will vary with the cdclk freq.
560a7ae4 5352 */
b5d99ff9 5353 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
560a7ae4 5354 I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->cdclk_freq, 1000));
560a7ae4
DL
5355}
5356
92891e45
VS
5357/* convert from kHz to .1 fixpoint MHz with -1MHz offset */
5358static int skl_cdclk_decimal(int cdclk)
5359{
5360 return DIV_ROUND_CLOSEST(cdclk - 1000, 500);
5361}
5362
5f199dfa
VS
5363static int bxt_de_pll_vco(struct drm_i915_private *dev_priv, int cdclk)
5364{
5365 int ratio;
5366
5367 if (cdclk == dev_priv->cdclk_pll.ref)
5368 return 0;
5369
5370 switch (cdclk) {
5371 default:
5372 MISSING_CASE(cdclk);
5373 case 144000:
5374 case 288000:
5375 case 384000:
5376 case 576000:
5377 ratio = 60;
5378 break;
5379 case 624000:
5380 ratio = 65;
5381 break;
5382 }
5383
5384 return dev_priv->cdclk_pll.ref * ratio;
5385}
5386
2b73001e
VS
5387static void bxt_de_pll_disable(struct drm_i915_private *dev_priv)
5388{
5389 I915_WRITE(BXT_DE_PLL_ENABLE, 0);
5390
5391 /* Timeout 200us */
5392 if (wait_for((I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK) == 0, 1))
5393 DRM_ERROR("timeout waiting for DE PLL unlock\n");
83d7c81f
VS
5394
5395 dev_priv->cdclk_pll.vco = 0;
2b73001e
VS
5396}
5397
5f199dfa 5398static void bxt_de_pll_enable(struct drm_i915_private *dev_priv, int vco)
2b73001e 5399{
5f199dfa 5400 int ratio = DIV_ROUND_CLOSEST(vco, dev_priv->cdclk_pll.ref);
2b73001e
VS
5401 u32 val;
5402
5403 val = I915_READ(BXT_DE_PLL_CTL);
5404 val &= ~BXT_DE_PLL_RATIO_MASK;
5f199dfa 5405 val |= BXT_DE_PLL_RATIO(ratio);
2b73001e
VS
5406 I915_WRITE(BXT_DE_PLL_CTL, val);
5407
5408 I915_WRITE(BXT_DE_PLL_ENABLE, BXT_DE_PLL_PLL_ENABLE);
5409
5410 /* Timeout 200us */
5411 if (wait_for((I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK) != 0, 1))
5412 DRM_ERROR("timeout waiting for DE PLL lock\n");
83d7c81f 5413
5f199dfa 5414 dev_priv->cdclk_pll.vco = vco;
2b73001e
VS
5415}
5416
9ef56154 5417static void broxton_set_cdclk(struct drm_i915_private *dev_priv, int cdclk)
f8437dd1 5418{
5f199dfa
VS
5419 u32 val, divider;
5420 int vco, ret;
f8437dd1 5421
5f199dfa
VS
5422 vco = bxt_de_pll_vco(dev_priv, cdclk);
5423
5424 DRM_DEBUG_DRIVER("Changing CDCLK to %d kHz (VCO %d kHz)\n", cdclk, vco);
5425
5426 /* cdclk = vco / 2 / div{1,1.5,2,4} */
5427 switch (DIV_ROUND_CLOSEST(vco, cdclk)) {
5428 case 8:
f8437dd1 5429 divider = BXT_CDCLK_CD2X_DIV_SEL_4;
f8437dd1 5430 break;
5f199dfa 5431 case 4:
f8437dd1 5432 divider = BXT_CDCLK_CD2X_DIV_SEL_2;
f8437dd1 5433 break;
5f199dfa 5434 case 3:
f8437dd1 5435 divider = BXT_CDCLK_CD2X_DIV_SEL_1_5;
f8437dd1 5436 break;
5f199dfa 5437 case 2:
f8437dd1 5438 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
f8437dd1
VK
5439 break;
5440 default:
5f199dfa
VS
5441 WARN_ON(cdclk != dev_priv->cdclk_pll.ref);
5442 WARN_ON(vco != 0);
f8437dd1 5443
5f199dfa
VS
5444 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
5445 break;
f8437dd1
VK
5446 }
5447
f8437dd1 5448 /* Inform power controller of upcoming frequency change */
5f199dfa 5449 mutex_lock(&dev_priv->rps.hw_lock);
f8437dd1
VK
5450 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
5451 0x80000000);
5452 mutex_unlock(&dev_priv->rps.hw_lock);
5453
5454 if (ret) {
5455 DRM_ERROR("PCode CDCLK freq change notify failed (err %d, freq %d)\n",
9ef56154 5456 ret, cdclk);
f8437dd1
VK
5457 return;
5458 }
5459
5f199dfa
VS
5460 if (dev_priv->cdclk_pll.vco != 0 &&
5461 dev_priv->cdclk_pll.vco != vco)
2b73001e 5462 bxt_de_pll_disable(dev_priv);
f8437dd1 5463
5f199dfa
VS
5464 if (dev_priv->cdclk_pll.vco != vco)
5465 bxt_de_pll_enable(dev_priv, vco);
f8437dd1 5466
5f199dfa
VS
5467 val = divider | skl_cdclk_decimal(cdclk);
5468 /*
5469 * FIXME if only the cd2x divider needs changing, it could be done
5470 * without shutting off the pipe (if only one pipe is active).
5471 */
5472 val |= BXT_CDCLK_CD2X_PIPE_NONE;
5473 /*
5474 * Disable SSA Precharge when CD clock frequency < 500 MHz,
5475 * enable otherwise.
5476 */
5477 if (cdclk >= 500000)
5478 val |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
5479 I915_WRITE(CDCLK_CTL, val);
f8437dd1
VK
5480
5481 mutex_lock(&dev_priv->rps.hw_lock);
5482 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
9ef56154 5483 DIV_ROUND_UP(cdclk, 25000));
f8437dd1
VK
5484 mutex_unlock(&dev_priv->rps.hw_lock);
5485
5486 if (ret) {
5487 DRM_ERROR("PCode CDCLK freq set failed, (err %d, freq %d)\n",
9ef56154 5488 ret, cdclk);
f8437dd1
VK
5489 return;
5490 }
5491
c6c4696f 5492 intel_update_cdclk(dev_priv->dev);
f8437dd1
VK
5493}
5494
d66a2194 5495static void bxt_sanitize_cdclk(struct drm_i915_private *dev_priv)
f8437dd1 5496{
d66a2194
ID
5497 u32 cdctl, expected;
5498
089c6fd5 5499 intel_update_cdclk(dev_priv->dev);
f8437dd1 5500
d66a2194
ID
5501 if (dev_priv->cdclk_pll.vco == 0 ||
5502 dev_priv->cdclk_freq == dev_priv->cdclk_pll.ref)
5503 goto sanitize;
5504
5505 /* DPLL okay; verify the cdclock
5506 *
5507 * Some BIOS versions leave an incorrect decimal frequency value and
5508 * set reserved MBZ bits in CDCLK_CTL at least during exiting from S4,
5509 * so sanitize this register.
5510 */
5511 cdctl = I915_READ(CDCLK_CTL);
5512 /*
5513 * Let's ignore the pipe field, since BIOS could have configured the
5514 * dividers both synching to an active pipe, or asynchronously
5515 * (PIPE_NONE).
5516 */
5517 cdctl &= ~BXT_CDCLK_CD2X_PIPE_NONE;
5518
5519 expected = (cdctl & BXT_CDCLK_CD2X_DIV_SEL_MASK) |
5520 skl_cdclk_decimal(dev_priv->cdclk_freq);
5521 /*
5522 * Disable SSA Precharge when CD clock frequency < 500 MHz,
5523 * enable otherwise.
5524 */
5525 if (dev_priv->cdclk_freq >= 500000)
5526 expected |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
5527
5528 if (cdctl == expected)
5529 /* All well; nothing to sanitize */
5530 return;
5531
5532sanitize:
5533 DRM_DEBUG_KMS("Sanitizing cdclk programmed by pre-os\n");
5534
5535 /* force cdclk programming */
5536 dev_priv->cdclk_freq = 0;
5537
5538 /* force full PLL disable + enable */
5539 dev_priv->cdclk_pll.vco = -1;
5540}
5541
5542void broxton_init_cdclk(struct drm_i915_private *dev_priv)
5543{
5544 bxt_sanitize_cdclk(dev_priv);
5545
5546 if (dev_priv->cdclk_freq != 0 && dev_priv->cdclk_pll.vco != 0)
089c6fd5 5547 return;
c2e001ef 5548
f8437dd1
VK
5549 /*
5550 * FIXME:
5551 * - The initial CDCLK needs to be read from VBT.
5552 * Need to make this change after VBT has changes for BXT.
f8437dd1 5553 */
d1b32c32 5554 broxton_set_cdclk(dev_priv, broxton_calc_cdclk(0));
f8437dd1
VK
5555}
5556
c6c4696f 5557void broxton_uninit_cdclk(struct drm_i915_private *dev_priv)
f8437dd1 5558{
5f199dfa 5559 broxton_set_cdclk(dev_priv, dev_priv->cdclk_pll.ref);
f8437dd1
VK
5560}
5561
a8ca4934
VS
5562static int skl_calc_cdclk(int max_pixclk, int vco)
5563{
63911d72 5564 if (vco == 8640000) {
a8ca4934 5565 if (max_pixclk > 540000)
487ed2e4 5566 return 617143;
a8ca4934
VS
5567 else if (max_pixclk > 432000)
5568 return 540000;
487ed2e4 5569 else if (max_pixclk > 308571)
a8ca4934
VS
5570 return 432000;
5571 else
487ed2e4 5572 return 308571;
a8ca4934 5573 } else {
a8ca4934
VS
5574 if (max_pixclk > 540000)
5575 return 675000;
5576 else if (max_pixclk > 450000)
5577 return 540000;
5578 else if (max_pixclk > 337500)
5579 return 450000;
5580 else
5581 return 337500;
5582 }
5583}
5584
ea61791e
VS
5585static void
5586skl_dpll0_update(struct drm_i915_private *dev_priv)
5d96d8af 5587{
ea61791e 5588 u32 val;
5d96d8af 5589
709e05c3 5590 dev_priv->cdclk_pll.ref = 24000;
1c3f7700 5591 dev_priv->cdclk_pll.vco = 0;
709e05c3 5592
ea61791e 5593 val = I915_READ(LCPLL1_CTL);
1c3f7700 5594 if ((val & LCPLL_PLL_ENABLE) == 0)
ea61791e 5595 return;
5d96d8af 5596
1c3f7700
ID
5597 if (WARN_ON((val & LCPLL_PLL_LOCK) == 0))
5598 return;
9f7eb31a 5599
ea61791e
VS
5600 val = I915_READ(DPLL_CTRL1);
5601
1c3f7700
ID
5602 if (WARN_ON((val & (DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) |
5603 DPLL_CTRL1_SSC(SKL_DPLL0) |
5604 DPLL_CTRL1_OVERRIDE(SKL_DPLL0))) !=
5605 DPLL_CTRL1_OVERRIDE(SKL_DPLL0)))
5606 return;
9f7eb31a 5607
ea61791e
VS
5608 switch (val & DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0)) {
5609 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810, SKL_DPLL0):
5610 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1350, SKL_DPLL0):
5611 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1620, SKL_DPLL0):
5612 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_2700, SKL_DPLL0):
63911d72 5613 dev_priv->cdclk_pll.vco = 8100000;
ea61791e
VS
5614 break;
5615 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080, SKL_DPLL0):
5616 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_2160, SKL_DPLL0):
63911d72 5617 dev_priv->cdclk_pll.vco = 8640000;
ea61791e
VS
5618 break;
5619 default:
5620 MISSING_CASE(val & DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
ea61791e
VS
5621 break;
5622 }
5d96d8af
DL
5623}
5624
b2045352
VS
5625void skl_set_preferred_cdclk_vco(struct drm_i915_private *dev_priv, int vco)
5626{
5627 bool changed = dev_priv->skl_preferred_vco_freq != vco;
5628
5629 dev_priv->skl_preferred_vco_freq = vco;
5630
5631 if (changed)
5632 intel_update_max_cdclk(dev_priv->dev);
5633}
5634
5d96d8af 5635static void
3861fc60 5636skl_dpll0_enable(struct drm_i915_private *dev_priv, int vco)
5d96d8af 5637{
a8ca4934 5638 int min_cdclk = skl_calc_cdclk(0, vco);
5d96d8af
DL
5639 u32 val;
5640
63911d72 5641 WARN_ON(vco != 8100000 && vco != 8640000);
b2045352 5642
5d96d8af 5643 /* select the minimum CDCLK before enabling DPLL 0 */
9ef56154 5644 val = CDCLK_FREQ_337_308 | skl_cdclk_decimal(min_cdclk);
5d96d8af
DL
5645 I915_WRITE(CDCLK_CTL, val);
5646 POSTING_READ(CDCLK_CTL);
5647
5648 /*
5649 * We always enable DPLL0 with the lowest link rate possible, but still
5650 * taking into account the VCO required to operate the eDP panel at the
5651 * desired frequency. The usual DP link rates operate with a VCO of
5652 * 8100 while the eDP 1.4 alternate link rates need a VCO of 8640.
5653 * The modeset code is responsible for the selection of the exact link
5654 * rate later on, with the constraint of choosing a frequency that
a8ca4934 5655 * works with vco.
5d96d8af
DL
5656 */
5657 val = I915_READ(DPLL_CTRL1);
5658
5659 val &= ~(DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) | DPLL_CTRL1_SSC(SKL_DPLL0) |
5660 DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
5661 val |= DPLL_CTRL1_OVERRIDE(SKL_DPLL0);
63911d72 5662 if (vco == 8640000)
5d96d8af
DL
5663 val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080,
5664 SKL_DPLL0);
5665 else
5666 val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810,
5667 SKL_DPLL0);
5668
5669 I915_WRITE(DPLL_CTRL1, val);
5670 POSTING_READ(DPLL_CTRL1);
5671
5672 I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) | LCPLL_PLL_ENABLE);
5673
5674 if (wait_for(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK, 5))
5675 DRM_ERROR("DPLL0 not locked\n");
1cd593e0 5676
63911d72 5677 dev_priv->cdclk_pll.vco = vco;
b2045352
VS
5678
5679 /* We'll want to keep using the current vco from now on. */
5680 skl_set_preferred_cdclk_vco(dev_priv, vco);
5d96d8af
DL
5681}
5682
430e05de
VS
5683static void
5684skl_dpll0_disable(struct drm_i915_private *dev_priv)
5685{
5686 I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) & ~LCPLL_PLL_ENABLE);
5687 if (wait_for(!(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK), 1))
5688 DRM_ERROR("Couldn't disable DPLL0\n");
1cd593e0 5689
63911d72 5690 dev_priv->cdclk_pll.vco = 0;
430e05de
VS
5691}
5692
5d96d8af
DL
5693static bool skl_cdclk_pcu_ready(struct drm_i915_private *dev_priv)
5694{
5695 int ret;
5696 u32 val;
5697
5698 /* inform PCU we want to change CDCLK */
5699 val = SKL_CDCLK_PREPARE_FOR_CHANGE;
5700 mutex_lock(&dev_priv->rps.hw_lock);
5701 ret = sandybridge_pcode_read(dev_priv, SKL_PCODE_CDCLK_CONTROL, &val);
5702 mutex_unlock(&dev_priv->rps.hw_lock);
5703
5704 return ret == 0 && (val & SKL_CDCLK_READY_FOR_CHANGE);
5705}
5706
5707static bool skl_cdclk_wait_for_pcu_ready(struct drm_i915_private *dev_priv)
5708{
5709 unsigned int i;
5710
5711 for (i = 0; i < 15; i++) {
5712 if (skl_cdclk_pcu_ready(dev_priv))
5713 return true;
5714 udelay(10);
5715 }
5716
5717 return false;
5718}
5719
1cd593e0 5720static void skl_set_cdclk(struct drm_i915_private *dev_priv, int cdclk, int vco)
5d96d8af 5721{
560a7ae4 5722 struct drm_device *dev = dev_priv->dev;
5d96d8af
DL
5723 u32 freq_select, pcu_ack;
5724
1cd593e0
VS
5725 WARN_ON((cdclk == 24000) != (vco == 0));
5726
63911d72 5727 DRM_DEBUG_DRIVER("Changing CDCLK to %d kHz (VCO %d kHz)\n", cdclk, vco);
5d96d8af
DL
5728
5729 if (!skl_cdclk_wait_for_pcu_ready(dev_priv)) {
5730 DRM_ERROR("failed to inform PCU about cdclk change\n");
5731 return;
5732 }
5733
5734 /* set CDCLK_CTL */
9ef56154 5735 switch (cdclk) {
5d96d8af
DL
5736 case 450000:
5737 case 432000:
5738 freq_select = CDCLK_FREQ_450_432;
5739 pcu_ack = 1;
5740 break;
5741 case 540000:
5742 freq_select = CDCLK_FREQ_540;
5743 pcu_ack = 2;
5744 break;
487ed2e4 5745 case 308571:
5d96d8af
DL
5746 case 337500:
5747 default:
5748 freq_select = CDCLK_FREQ_337_308;
5749 pcu_ack = 0;
5750 break;
487ed2e4 5751 case 617143:
5d96d8af
DL
5752 case 675000:
5753 freq_select = CDCLK_FREQ_675_617;
5754 pcu_ack = 3;
5755 break;
5756 }
5757
63911d72
VS
5758 if (dev_priv->cdclk_pll.vco != 0 &&
5759 dev_priv->cdclk_pll.vco != vco)
1cd593e0
VS
5760 skl_dpll0_disable(dev_priv);
5761
63911d72 5762 if (dev_priv->cdclk_pll.vco != vco)
1cd593e0
VS
5763 skl_dpll0_enable(dev_priv, vco);
5764
9ef56154 5765 I915_WRITE(CDCLK_CTL, freq_select | skl_cdclk_decimal(cdclk));
5d96d8af
DL
5766 POSTING_READ(CDCLK_CTL);
5767
5768 /* inform PCU of the change */
5769 mutex_lock(&dev_priv->rps.hw_lock);
5770 sandybridge_pcode_write(dev_priv, SKL_PCODE_CDCLK_CONTROL, pcu_ack);
5771 mutex_unlock(&dev_priv->rps.hw_lock);
560a7ae4
DL
5772
5773 intel_update_cdclk(dev);
5d96d8af
DL
5774}
5775
9f7eb31a
VS
5776static void skl_sanitize_cdclk(struct drm_i915_private *dev_priv);
5777
5d96d8af
DL
5778void skl_uninit_cdclk(struct drm_i915_private *dev_priv)
5779{
709e05c3 5780 skl_set_cdclk(dev_priv, dev_priv->cdclk_pll.ref, 0);
5d96d8af
DL
5781}
5782
5783void skl_init_cdclk(struct drm_i915_private *dev_priv)
5784{
9f7eb31a
VS
5785 int cdclk, vco;
5786
5787 skl_sanitize_cdclk(dev_priv);
5d96d8af 5788
63911d72 5789 if (dev_priv->cdclk_freq != 0 && dev_priv->cdclk_pll.vco != 0) {
9f7eb31a
VS
5790 /*
5791 * Use the current vco as our initial
5792 * guess as to what the preferred vco is.
5793 */
5794 if (dev_priv->skl_preferred_vco_freq == 0)
5795 skl_set_preferred_cdclk_vco(dev_priv,
63911d72 5796 dev_priv->cdclk_pll.vco);
70c2c184 5797 return;
1cd593e0 5798 }
5d96d8af 5799
70c2c184
VS
5800 vco = dev_priv->skl_preferred_vco_freq;
5801 if (vco == 0)
63911d72 5802 vco = 8100000;
70c2c184 5803 cdclk = skl_calc_cdclk(0, vco);
5d96d8af 5804
70c2c184 5805 skl_set_cdclk(dev_priv, cdclk, vco);
5d96d8af
DL
5806}
5807
9f7eb31a 5808static void skl_sanitize_cdclk(struct drm_i915_private *dev_priv)
c73666f3 5809{
09492498 5810 uint32_t cdctl, expected;
c73666f3 5811
f1b391a5
SK
5812 /*
5813 * check if the pre-os intialized the display
5814 * There is SWF18 scratchpad register defined which is set by the
5815 * pre-os which can be used by the OS drivers to check the status
5816 */
5817 if ((I915_READ(SWF_ILK(0x18)) & 0x00FFFFFF) == 0)
5818 goto sanitize;
5819
1c3f7700 5820 intel_update_cdclk(dev_priv->dev);
c73666f3 5821 /* Is PLL enabled and locked ? */
1c3f7700
ID
5822 if (dev_priv->cdclk_pll.vco == 0 ||
5823 dev_priv->cdclk_freq == dev_priv->cdclk_pll.ref)
c73666f3
SK
5824 goto sanitize;
5825
5826 /* DPLL okay; verify the cdclock
5827 *
5828 * Noticed in some instances that the freq selection is correct but
5829 * decimal part is programmed wrong from BIOS where pre-os does not
5830 * enable display. Verify the same as well.
5831 */
09492498
VS
5832 cdctl = I915_READ(CDCLK_CTL);
5833 expected = (cdctl & CDCLK_FREQ_SEL_MASK) |
5834 skl_cdclk_decimal(dev_priv->cdclk_freq);
5835 if (cdctl == expected)
c73666f3 5836 /* All well; nothing to sanitize */
9f7eb31a 5837 return;
c89e39f3 5838
9f7eb31a
VS
5839sanitize:
5840 DRM_DEBUG_KMS("Sanitizing cdclk programmed by pre-os\n");
c73666f3 5841
9f7eb31a
VS
5842 /* force cdclk programming */
5843 dev_priv->cdclk_freq = 0;
5844 /* force full PLL disable + enable */
63911d72 5845 dev_priv->cdclk_pll.vco = -1;
c73666f3
SK
5846}
5847
30a970c6
JB
5848/* Adjust CDclk dividers to allow high res or save power if possible */
5849static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
5850{
5851 struct drm_i915_private *dev_priv = dev->dev_private;
5852 u32 val, cmd;
5853
164dfd28
VK
5854 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5855 != dev_priv->cdclk_freq);
d60c4473 5856
dfcab17e 5857 if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
30a970c6 5858 cmd = 2;
dfcab17e 5859 else if (cdclk == 266667)
30a970c6
JB
5860 cmd = 1;
5861 else
5862 cmd = 0;
5863
5864 mutex_lock(&dev_priv->rps.hw_lock);
5865 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5866 val &= ~DSPFREQGUAR_MASK;
5867 val |= (cmd << DSPFREQGUAR_SHIFT);
5868 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5869 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5870 DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
5871 50)) {
5872 DRM_ERROR("timed out waiting for CDclk change\n");
5873 }
5874 mutex_unlock(&dev_priv->rps.hw_lock);
5875
54433e91
VS
5876 mutex_lock(&dev_priv->sb_lock);
5877
dfcab17e 5878 if (cdclk == 400000) {
6bcda4f0 5879 u32 divider;
30a970c6 5880
6bcda4f0 5881 divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
30a970c6 5882
30a970c6
JB
5883 /* adjust cdclk divider */
5884 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
87d5d259 5885 val &= ~CCK_FREQUENCY_VALUES;
30a970c6
JB
5886 val |= divider;
5887 vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
a877e801
VS
5888
5889 if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
87d5d259 5890 CCK_FREQUENCY_STATUS) == (divider << CCK_FREQUENCY_STATUS_SHIFT),
a877e801
VS
5891 50))
5892 DRM_ERROR("timed out waiting for CDclk change\n");
30a970c6
JB
5893 }
5894
30a970c6
JB
5895 /* adjust self-refresh exit latency value */
5896 val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
5897 val &= ~0x7f;
5898
5899 /*
5900 * For high bandwidth configs, we set a higher latency in the bunit
5901 * so that the core display fetch happens in time to avoid underruns.
5902 */
dfcab17e 5903 if (cdclk == 400000)
30a970c6
JB
5904 val |= 4500 / 250; /* 4.5 usec */
5905 else
5906 val |= 3000 / 250; /* 3.0 usec */
5907 vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
54433e91 5908
a580516d 5909 mutex_unlock(&dev_priv->sb_lock);
30a970c6 5910
b6283055 5911 intel_update_cdclk(dev);
30a970c6
JB
5912}
5913
383c5a6a
VS
5914static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
5915{
5916 struct drm_i915_private *dev_priv = dev->dev_private;
5917 u32 val, cmd;
5918
164dfd28
VK
5919 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5920 != dev_priv->cdclk_freq);
383c5a6a
VS
5921
5922 switch (cdclk) {
383c5a6a
VS
5923 case 333333:
5924 case 320000:
383c5a6a 5925 case 266667:
383c5a6a 5926 case 200000:
383c5a6a
VS
5927 break;
5928 default:
5f77eeb0 5929 MISSING_CASE(cdclk);
383c5a6a
VS
5930 return;
5931 }
5932
9d0d3fda
VS
5933 /*
5934 * Specs are full of misinformation, but testing on actual
5935 * hardware has shown that we just need to write the desired
5936 * CCK divider into the Punit register.
5937 */
5938 cmd = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
5939
383c5a6a
VS
5940 mutex_lock(&dev_priv->rps.hw_lock);
5941 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5942 val &= ~DSPFREQGUAR_MASK_CHV;
5943 val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
5944 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5945 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5946 DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
5947 50)) {
5948 DRM_ERROR("timed out waiting for CDclk change\n");
5949 }
5950 mutex_unlock(&dev_priv->rps.hw_lock);
5951
b6283055 5952 intel_update_cdclk(dev);
383c5a6a
VS
5953}
5954
30a970c6
JB
5955static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
5956 int max_pixclk)
5957{
6bcda4f0 5958 int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
6cca3195 5959 int limit = IS_CHERRYVIEW(dev_priv) ? 95 : 90;
29dc7ef3 5960
30a970c6
JB
5961 /*
5962 * Really only a few cases to deal with, as only 4 CDclks are supported:
5963 * 200MHz
5964 * 267MHz
29dc7ef3 5965 * 320/333MHz (depends on HPLL freq)
6cca3195
VS
5966 * 400MHz (VLV only)
5967 * So we check to see whether we're above 90% (VLV) or 95% (CHV)
5968 * of the lower bin and adjust if needed.
e37c67a1
VS
5969 *
5970 * We seem to get an unstable or solid color picture at 200MHz.
5971 * Not sure what's wrong. For now use 200MHz only when all pipes
5972 * are off.
30a970c6 5973 */
6cca3195
VS
5974 if (!IS_CHERRYVIEW(dev_priv) &&
5975 max_pixclk > freq_320*limit/100)
dfcab17e 5976 return 400000;
6cca3195 5977 else if (max_pixclk > 266667*limit/100)
29dc7ef3 5978 return freq_320;
e37c67a1 5979 else if (max_pixclk > 0)
dfcab17e 5980 return 266667;
e37c67a1
VS
5981 else
5982 return 200000;
30a970c6
JB
5983}
5984
c44deb6c 5985static int broxton_calc_cdclk(int max_pixclk)
f8437dd1 5986{
760e1477 5987 if (max_pixclk > 576000)
f8437dd1 5988 return 624000;
760e1477 5989 else if (max_pixclk > 384000)
f8437dd1 5990 return 576000;
760e1477 5991 else if (max_pixclk > 288000)
f8437dd1 5992 return 384000;
760e1477 5993 else if (max_pixclk > 144000)
f8437dd1
VK
5994 return 288000;
5995 else
5996 return 144000;
5997}
5998
e8788cbc 5999/* Compute the max pixel clock for new configuration. */
a821fc46
ACO
6000static int intel_mode_max_pixclk(struct drm_device *dev,
6001 struct drm_atomic_state *state)
30a970c6 6002{
565602d7
ML
6003 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
6004 struct drm_i915_private *dev_priv = dev->dev_private;
6005 struct drm_crtc *crtc;
6006 struct drm_crtc_state *crtc_state;
6007 unsigned max_pixclk = 0, i;
6008 enum pipe pipe;
30a970c6 6009
565602d7
ML
6010 memcpy(intel_state->min_pixclk, dev_priv->min_pixclk,
6011 sizeof(intel_state->min_pixclk));
304603f4 6012
565602d7
ML
6013 for_each_crtc_in_state(state, crtc, crtc_state, i) {
6014 int pixclk = 0;
6015
6016 if (crtc_state->enable)
6017 pixclk = crtc_state->adjusted_mode.crtc_clock;
304603f4 6018
565602d7 6019 intel_state->min_pixclk[i] = pixclk;
30a970c6
JB
6020 }
6021
565602d7
ML
6022 for_each_pipe(dev_priv, pipe)
6023 max_pixclk = max(intel_state->min_pixclk[pipe], max_pixclk);
6024
30a970c6
JB
6025 return max_pixclk;
6026}
6027
27c329ed 6028static int valleyview_modeset_calc_cdclk(struct drm_atomic_state *state)
30a970c6 6029{
27c329ed
ML
6030 struct drm_device *dev = state->dev;
6031 struct drm_i915_private *dev_priv = dev->dev_private;
6032 int max_pixclk = intel_mode_max_pixclk(dev, state);
1a617b77
ML
6033 struct intel_atomic_state *intel_state =
6034 to_intel_atomic_state(state);
30a970c6 6035
1a617b77 6036 intel_state->cdclk = intel_state->dev_cdclk =
27c329ed 6037 valleyview_calc_cdclk(dev_priv, max_pixclk);
0a9ab303 6038
1a617b77
ML
6039 if (!intel_state->active_crtcs)
6040 intel_state->dev_cdclk = valleyview_calc_cdclk(dev_priv, 0);
6041
27c329ed
ML
6042 return 0;
6043}
304603f4 6044
27c329ed
ML
6045static int broxton_modeset_calc_cdclk(struct drm_atomic_state *state)
6046{
4e5ca60f 6047 int max_pixclk = ilk_max_pixel_rate(state);
1a617b77
ML
6048 struct intel_atomic_state *intel_state =
6049 to_intel_atomic_state(state);
85a96e7a 6050
1a617b77 6051 intel_state->cdclk = intel_state->dev_cdclk =
c44deb6c 6052 broxton_calc_cdclk(max_pixclk);
85a96e7a 6053
1a617b77 6054 if (!intel_state->active_crtcs)
c44deb6c 6055 intel_state->dev_cdclk = broxton_calc_cdclk(0);
1a617b77 6056
27c329ed 6057 return 0;
30a970c6
JB
6058}
6059
1e69cd74
VS
6060static void vlv_program_pfi_credits(struct drm_i915_private *dev_priv)
6061{
6062 unsigned int credits, default_credits;
6063
6064 if (IS_CHERRYVIEW(dev_priv))
6065 default_credits = PFI_CREDIT(12);
6066 else
6067 default_credits = PFI_CREDIT(8);
6068
bfa7df01 6069 if (dev_priv->cdclk_freq >= dev_priv->czclk_freq) {
1e69cd74
VS
6070 /* CHV suggested value is 31 or 63 */
6071 if (IS_CHERRYVIEW(dev_priv))
fcc0008f 6072 credits = PFI_CREDIT_63;
1e69cd74
VS
6073 else
6074 credits = PFI_CREDIT(15);
6075 } else {
6076 credits = default_credits;
6077 }
6078
6079 /*
6080 * WA - write default credits before re-programming
6081 * FIXME: should we also set the resend bit here?
6082 */
6083 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
6084 default_credits);
6085
6086 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
6087 credits | PFI_CREDIT_RESEND);
6088
6089 /*
6090 * FIXME is this guaranteed to clear
6091 * immediately or should we poll for it?
6092 */
6093 WARN_ON(I915_READ(GCI_CONTROL) & PFI_CREDIT_RESEND);
6094}
6095
27c329ed 6096static void valleyview_modeset_commit_cdclk(struct drm_atomic_state *old_state)
30a970c6 6097{
a821fc46 6098 struct drm_device *dev = old_state->dev;
30a970c6 6099 struct drm_i915_private *dev_priv = dev->dev_private;
1a617b77
ML
6100 struct intel_atomic_state *old_intel_state =
6101 to_intel_atomic_state(old_state);
6102 unsigned req_cdclk = old_intel_state->dev_cdclk;
30a970c6 6103
27c329ed
ML
6104 /*
6105 * FIXME: We can end up here with all power domains off, yet
6106 * with a CDCLK frequency other than the minimum. To account
6107 * for this take the PIPE-A power domain, which covers the HW
6108 * blocks needed for the following programming. This can be
6109 * removed once it's guaranteed that we get here either with
6110 * the minimum CDCLK set, or the required power domains
6111 * enabled.
6112 */
6113 intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
738c05c0 6114
27c329ed
ML
6115 if (IS_CHERRYVIEW(dev))
6116 cherryview_set_cdclk(dev, req_cdclk);
6117 else
6118 valleyview_set_cdclk(dev, req_cdclk);
738c05c0 6119
27c329ed 6120 vlv_program_pfi_credits(dev_priv);
1e69cd74 6121
27c329ed 6122 intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
30a970c6
JB
6123}
6124
89b667f8
JB
6125static void valleyview_crtc_enable(struct drm_crtc *crtc)
6126{
6127 struct drm_device *dev = crtc->dev;
a72e4c9f 6128 struct drm_i915_private *dev_priv = to_i915(dev);
89b667f8
JB
6129 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6130 struct intel_encoder *encoder;
b95c5321
ML
6131 struct intel_crtc_state *pipe_config =
6132 to_intel_crtc_state(crtc->state);
89b667f8 6133 int pipe = intel_crtc->pipe;
89b667f8 6134
53d9f4e9 6135 if (WARN_ON(intel_crtc->active))
89b667f8
JB
6136 return;
6137
6e3c9717 6138 if (intel_crtc->config->has_dp_encoder)
fe3cd48d 6139 intel_dp_set_m_n(intel_crtc, M1_N1);
5b18e57c
DV
6140
6141 intel_set_pipe_timings(intel_crtc);
bc58be60 6142 intel_set_pipe_src_size(intel_crtc);
5b18e57c 6143
c14b0485
VS
6144 if (IS_CHERRYVIEW(dev) && pipe == PIPE_B) {
6145 struct drm_i915_private *dev_priv = dev->dev_private;
6146
6147 I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
6148 I915_WRITE(CHV_CANVAS(pipe), 0);
6149 }
6150
5b18e57c
DV
6151 i9xx_set_pipeconf(intel_crtc);
6152
89b667f8 6153 intel_crtc->active = true;
89b667f8 6154
a72e4c9f 6155 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4a3436e8 6156
89b667f8
JB
6157 for_each_encoder_on_crtc(dev, crtc, encoder)
6158 if (encoder->pre_pll_enable)
6159 encoder->pre_pll_enable(encoder);
6160
cd2d34d9
VS
6161 if (IS_CHERRYVIEW(dev)) {
6162 chv_prepare_pll(intel_crtc, intel_crtc->config);
6163 chv_enable_pll(intel_crtc, intel_crtc->config);
6164 } else {
6165 vlv_prepare_pll(intel_crtc, intel_crtc->config);
6166 vlv_enable_pll(intel_crtc, intel_crtc->config);
9d556c99 6167 }
89b667f8
JB
6168
6169 for_each_encoder_on_crtc(dev, crtc, encoder)
6170 if (encoder->pre_enable)
6171 encoder->pre_enable(encoder);
6172
2dd24552
JB
6173 i9xx_pfit_enable(intel_crtc);
6174
b95c5321 6175 intel_color_load_luts(&pipe_config->base);
63cbb074 6176
caed361d 6177 intel_update_watermarks(crtc);
e1fdc473 6178 intel_enable_pipe(intel_crtc);
be6a6f8e 6179
4b3a9526
VS
6180 assert_vblank_disabled(crtc);
6181 drm_crtc_vblank_on(crtc);
6182
f9b61ff6
DV
6183 for_each_encoder_on_crtc(dev, crtc, encoder)
6184 encoder->enable(encoder);
89b667f8
JB
6185}
6186
f13c2ef3
DV
6187static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
6188{
6189 struct drm_device *dev = crtc->base.dev;
6190 struct drm_i915_private *dev_priv = dev->dev_private;
6191
6e3c9717
ACO
6192 I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
6193 I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
f13c2ef3
DV
6194}
6195
0b8765c6 6196static void i9xx_crtc_enable(struct drm_crtc *crtc)
79e53945
JB
6197{
6198 struct drm_device *dev = crtc->dev;
a72e4c9f 6199 struct drm_i915_private *dev_priv = to_i915(dev);
79e53945 6200 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 6201 struct intel_encoder *encoder;
b95c5321
ML
6202 struct intel_crtc_state *pipe_config =
6203 to_intel_crtc_state(crtc->state);
cd2d34d9 6204 enum pipe pipe = intel_crtc->pipe;
79e53945 6205
53d9f4e9 6206 if (WARN_ON(intel_crtc->active))
f7abfe8b
CW
6207 return;
6208
f13c2ef3
DV
6209 i9xx_set_pll_dividers(intel_crtc);
6210
6e3c9717 6211 if (intel_crtc->config->has_dp_encoder)
fe3cd48d 6212 intel_dp_set_m_n(intel_crtc, M1_N1);
5b18e57c
DV
6213
6214 intel_set_pipe_timings(intel_crtc);
bc58be60 6215 intel_set_pipe_src_size(intel_crtc);
5b18e57c 6216
5b18e57c
DV
6217 i9xx_set_pipeconf(intel_crtc);
6218
f7abfe8b 6219 intel_crtc->active = true;
6b383a7f 6220
4a3436e8 6221 if (!IS_GEN2(dev))
a72e4c9f 6222 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4a3436e8 6223
9d6d9f19
MK
6224 for_each_encoder_on_crtc(dev, crtc, encoder)
6225 if (encoder->pre_enable)
6226 encoder->pre_enable(encoder);
6227
f6736a1a
DV
6228 i9xx_enable_pll(intel_crtc);
6229
2dd24552
JB
6230 i9xx_pfit_enable(intel_crtc);
6231
b95c5321 6232 intel_color_load_luts(&pipe_config->base);
63cbb074 6233
f37fcc2a 6234 intel_update_watermarks(crtc);
e1fdc473 6235 intel_enable_pipe(intel_crtc);
be6a6f8e 6236
4b3a9526
VS
6237 assert_vblank_disabled(crtc);
6238 drm_crtc_vblank_on(crtc);
6239
f9b61ff6
DV
6240 for_each_encoder_on_crtc(dev, crtc, encoder)
6241 encoder->enable(encoder);
0b8765c6 6242}
79e53945 6243
87476d63
DV
6244static void i9xx_pfit_disable(struct intel_crtc *crtc)
6245{
6246 struct drm_device *dev = crtc->base.dev;
6247 struct drm_i915_private *dev_priv = dev->dev_private;
87476d63 6248
6e3c9717 6249 if (!crtc->config->gmch_pfit.control)
328d8e82 6250 return;
87476d63 6251
328d8e82 6252 assert_pipe_disabled(dev_priv, crtc->pipe);
87476d63 6253
328d8e82
DV
6254 DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
6255 I915_READ(PFIT_CONTROL));
6256 I915_WRITE(PFIT_CONTROL, 0);
87476d63
DV
6257}
6258
0b8765c6
JB
6259static void i9xx_crtc_disable(struct drm_crtc *crtc)
6260{
6261 struct drm_device *dev = crtc->dev;
6262 struct drm_i915_private *dev_priv = dev->dev_private;
6263 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 6264 struct intel_encoder *encoder;
0b8765c6 6265 int pipe = intel_crtc->pipe;
ef9c3aee 6266
6304cd91
VS
6267 /*
6268 * On gen2 planes are double buffered but the pipe isn't, so we must
6269 * wait for planes to fully turn off before disabling the pipe.
6270 */
90e83e53
ACO
6271 if (IS_GEN2(dev))
6272 intel_wait_for_vblank(dev, pipe);
6304cd91 6273
4b3a9526
VS
6274 for_each_encoder_on_crtc(dev, crtc, encoder)
6275 encoder->disable(encoder);
6276
f9b61ff6
DV
6277 drm_crtc_vblank_off(crtc);
6278 assert_vblank_disabled(crtc);
6279
575f7ab7 6280 intel_disable_pipe(intel_crtc);
24a1f16d 6281
87476d63 6282 i9xx_pfit_disable(intel_crtc);
24a1f16d 6283
89b667f8
JB
6284 for_each_encoder_on_crtc(dev, crtc, encoder)
6285 if (encoder->post_disable)
6286 encoder->post_disable(encoder);
6287
a65347ba 6288 if (!intel_crtc->config->has_dsi_encoder) {
076ed3b2
CML
6289 if (IS_CHERRYVIEW(dev))
6290 chv_disable_pll(dev_priv, pipe);
6291 else if (IS_VALLEYVIEW(dev))
6292 vlv_disable_pll(dev_priv, pipe);
6293 else
1c4e0274 6294 i9xx_disable_pll(intel_crtc);
076ed3b2 6295 }
0b8765c6 6296
d6db995f
VS
6297 for_each_encoder_on_crtc(dev, crtc, encoder)
6298 if (encoder->post_pll_disable)
6299 encoder->post_pll_disable(encoder);
6300
4a3436e8 6301 if (!IS_GEN2(dev))
a72e4c9f 6302 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
0b8765c6
JB
6303}
6304
b17d48e2
ML
6305static void intel_crtc_disable_noatomic(struct drm_crtc *crtc)
6306{
842e0307 6307 struct intel_encoder *encoder;
b17d48e2
ML
6308 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6309 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
6310 enum intel_display_power_domain domain;
6311 unsigned long domains;
6312
6313 if (!intel_crtc->active)
6314 return;
6315
a539205a 6316 if (to_intel_plane_state(crtc->primary->state)->visible) {
5a21b665 6317 WARN_ON(intel_crtc->flip_work);
fc32b1fd 6318
2622a081 6319 intel_pre_disable_primary_noatomic(crtc);
54a41961
ML
6320
6321 intel_crtc_disable_planes(crtc, 1 << drm_plane_index(crtc->primary));
6322 to_intel_plane_state(crtc->primary->state)->visible = false;
a539205a
ML
6323 }
6324
b17d48e2 6325 dev_priv->display.crtc_disable(crtc);
842e0307 6326
78108b7c
VS
6327 DRM_DEBUG_KMS("[CRTC:%d:%s] hw state adjusted, was enabled, now disabled\n",
6328 crtc->base.id, crtc->name);
842e0307
ML
6329
6330 WARN_ON(drm_atomic_set_mode_for_crtc(crtc->state, NULL) < 0);
6331 crtc->state->active = false;
37d9078b 6332 intel_crtc->active = false;
842e0307
ML
6333 crtc->enabled = false;
6334 crtc->state->connector_mask = 0;
6335 crtc->state->encoder_mask = 0;
6336
6337 for_each_encoder_on_crtc(crtc->dev, crtc, encoder)
6338 encoder->base.crtc = NULL;
6339
58f9c0bc 6340 intel_fbc_disable(intel_crtc);
37d9078b 6341 intel_update_watermarks(crtc);
1f7457b1 6342 intel_disable_shared_dpll(intel_crtc);
b17d48e2
ML
6343
6344 domains = intel_crtc->enabled_power_domains;
6345 for_each_power_domain(domain, domains)
6346 intel_display_power_put(dev_priv, domain);
6347 intel_crtc->enabled_power_domains = 0;
565602d7
ML
6348
6349 dev_priv->active_crtcs &= ~(1 << intel_crtc->pipe);
6350 dev_priv->min_pixclk[intel_crtc->pipe] = 0;
b17d48e2
ML
6351}
6352
6b72d486
ML
6353/*
6354 * turn all crtc's off, but do not adjust state
6355 * This has to be paired with a call to intel_modeset_setup_hw_state.
6356 */
70e0bd74 6357int intel_display_suspend(struct drm_device *dev)
ee7b9f93 6358{
e2c8b870 6359 struct drm_i915_private *dev_priv = to_i915(dev);
70e0bd74 6360 struct drm_atomic_state *state;
e2c8b870 6361 int ret;
70e0bd74 6362
e2c8b870
ML
6363 state = drm_atomic_helper_suspend(dev);
6364 ret = PTR_ERR_OR_ZERO(state);
70e0bd74
ML
6365 if (ret)
6366 DRM_ERROR("Suspending crtc's failed with %i\n", ret);
e2c8b870
ML
6367 else
6368 dev_priv->modeset_restore_state = state;
70e0bd74 6369 return ret;
ee7b9f93
JB
6370}
6371
ea5b213a 6372void intel_encoder_destroy(struct drm_encoder *encoder)
7e7d76c3 6373{
4ef69c7a 6374 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
ea5b213a 6375
ea5b213a
CW
6376 drm_encoder_cleanup(encoder);
6377 kfree(intel_encoder);
7e7d76c3
JB
6378}
6379
0a91ca29
DV
6380/* Cross check the actual hw state with our own modeset state tracking (and it's
6381 * internal consistency). */
5a21b665 6382static void intel_connector_verify_state(struct intel_connector *connector)
79e53945 6383{
5a21b665 6384 struct drm_crtc *crtc = connector->base.state->crtc;
35dd3c64
ML
6385
6386 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
6387 connector->base.base.id,
6388 connector->base.name);
6389
0a91ca29 6390 if (connector->get_hw_state(connector)) {
e85376cb 6391 struct intel_encoder *encoder = connector->encoder;
5a21b665 6392 struct drm_connector_state *conn_state = connector->base.state;
0a91ca29 6393
35dd3c64
ML
6394 I915_STATE_WARN(!crtc,
6395 "connector enabled without attached crtc\n");
0a91ca29 6396
35dd3c64
ML
6397 if (!crtc)
6398 return;
6399
6400 I915_STATE_WARN(!crtc->state->active,
6401 "connector is active, but attached crtc isn't\n");
6402
e85376cb 6403 if (!encoder || encoder->type == INTEL_OUTPUT_DP_MST)
35dd3c64
ML
6404 return;
6405
e85376cb 6406 I915_STATE_WARN(conn_state->best_encoder != &encoder->base,
35dd3c64
ML
6407 "atomic encoder doesn't match attached encoder\n");
6408
e85376cb 6409 I915_STATE_WARN(conn_state->crtc != encoder->base.crtc,
35dd3c64
ML
6410 "attached encoder crtc differs from connector crtc\n");
6411 } else {
4d688a2a
ML
6412 I915_STATE_WARN(crtc && crtc->state->active,
6413 "attached crtc is active, but connector isn't\n");
5a21b665 6414 I915_STATE_WARN(!crtc && connector->base.state->best_encoder,
35dd3c64 6415 "best encoder set without crtc!\n");
0a91ca29 6416 }
79e53945
JB
6417}
6418
08d9bc92
ACO
6419int intel_connector_init(struct intel_connector *connector)
6420{
5350a031 6421 drm_atomic_helper_connector_reset(&connector->base);
08d9bc92 6422
5350a031 6423 if (!connector->base.state)
08d9bc92
ACO
6424 return -ENOMEM;
6425
08d9bc92
ACO
6426 return 0;
6427}
6428
6429struct intel_connector *intel_connector_alloc(void)
6430{
6431 struct intel_connector *connector;
6432
6433 connector = kzalloc(sizeof *connector, GFP_KERNEL);
6434 if (!connector)
6435 return NULL;
6436
6437 if (intel_connector_init(connector) < 0) {
6438 kfree(connector);
6439 return NULL;
6440 }
6441
6442 return connector;
6443}
6444
f0947c37
DV
6445/* Simple connector->get_hw_state implementation for encoders that support only
6446 * one connector and no cloning and hence the encoder state determines the state
6447 * of the connector. */
6448bool intel_connector_get_hw_state(struct intel_connector *connector)
ea5b213a 6449{
24929352 6450 enum pipe pipe = 0;
f0947c37 6451 struct intel_encoder *encoder = connector->encoder;
ea5b213a 6452
f0947c37 6453 return encoder->get_hw_state(encoder, &pipe);
ea5b213a
CW
6454}
6455
6d293983 6456static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
d272ddfa 6457{
6d293983
ACO
6458 if (crtc_state->base.enable && crtc_state->has_pch_encoder)
6459 return crtc_state->fdi_lanes;
d272ddfa
VS
6460
6461 return 0;
6462}
6463
6d293983 6464static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
5cec258b 6465 struct intel_crtc_state *pipe_config)
1857e1da 6466{
6d293983
ACO
6467 struct drm_atomic_state *state = pipe_config->base.state;
6468 struct intel_crtc *other_crtc;
6469 struct intel_crtc_state *other_crtc_state;
6470
1857e1da
DV
6471 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
6472 pipe_name(pipe), pipe_config->fdi_lanes);
6473 if (pipe_config->fdi_lanes > 4) {
6474 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
6475 pipe_name(pipe), pipe_config->fdi_lanes);
6d293983 6476 return -EINVAL;
1857e1da
DV
6477 }
6478
bafb6553 6479 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
1857e1da
DV
6480 if (pipe_config->fdi_lanes > 2) {
6481 DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
6482 pipe_config->fdi_lanes);
6d293983 6483 return -EINVAL;
1857e1da 6484 } else {
6d293983 6485 return 0;
1857e1da
DV
6486 }
6487 }
6488
6489 if (INTEL_INFO(dev)->num_pipes == 2)
6d293983 6490 return 0;
1857e1da
DV
6491
6492 /* Ivybridge 3 pipe is really complicated */
6493 switch (pipe) {
6494 case PIPE_A:
6d293983 6495 return 0;
1857e1da 6496 case PIPE_B:
6d293983
ACO
6497 if (pipe_config->fdi_lanes <= 2)
6498 return 0;
6499
6500 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_C));
6501 other_crtc_state =
6502 intel_atomic_get_crtc_state(state, other_crtc);
6503 if (IS_ERR(other_crtc_state))
6504 return PTR_ERR(other_crtc_state);
6505
6506 if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
1857e1da
DV
6507 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
6508 pipe_name(pipe), pipe_config->fdi_lanes);
6d293983 6509 return -EINVAL;
1857e1da 6510 }
6d293983 6511 return 0;
1857e1da 6512 case PIPE_C:
251cc67c
VS
6513 if (pipe_config->fdi_lanes > 2) {
6514 DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
6515 pipe_name(pipe), pipe_config->fdi_lanes);
6d293983 6516 return -EINVAL;
251cc67c 6517 }
6d293983
ACO
6518
6519 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_B));
6520 other_crtc_state =
6521 intel_atomic_get_crtc_state(state, other_crtc);
6522 if (IS_ERR(other_crtc_state))
6523 return PTR_ERR(other_crtc_state);
6524
6525 if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
1857e1da 6526 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
6d293983 6527 return -EINVAL;
1857e1da 6528 }
6d293983 6529 return 0;
1857e1da
DV
6530 default:
6531 BUG();
6532 }
6533}
6534
e29c22c0
DV
6535#define RETRY 1
6536static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
5cec258b 6537 struct intel_crtc_state *pipe_config)
877d48d5 6538{
1857e1da 6539 struct drm_device *dev = intel_crtc->base.dev;
7c5f93b0 6540 const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
6d293983
ACO
6541 int lane, link_bw, fdi_dotclock, ret;
6542 bool needs_recompute = false;
877d48d5 6543
e29c22c0 6544retry:
877d48d5
DV
6545 /* FDI is a binary signal running at ~2.7GHz, encoding
6546 * each output octet as 10 bits. The actual frequency
6547 * is stored as a divider into a 100MHz clock, and the
6548 * mode pixel clock is stored in units of 1KHz.
6549 * Hence the bw of each lane in terms of the mode signal
6550 * is:
6551 */
21a727b3 6552 link_bw = intel_fdi_link_freq(to_i915(dev), pipe_config);
877d48d5 6553
241bfc38 6554 fdi_dotclock = adjusted_mode->crtc_clock;
877d48d5 6555
2bd89a07 6556 lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
877d48d5
DV
6557 pipe_config->pipe_bpp);
6558
6559 pipe_config->fdi_lanes = lane;
6560
2bd89a07 6561 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
877d48d5 6562 link_bw, &pipe_config->fdi_m_n);
1857e1da 6563
e3b247da 6564 ret = ironlake_check_fdi_lanes(dev, intel_crtc->pipe, pipe_config);
6d293983 6565 if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
e29c22c0
DV
6566 pipe_config->pipe_bpp -= 2*3;
6567 DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
6568 pipe_config->pipe_bpp);
6569 needs_recompute = true;
6570 pipe_config->bw_constrained = true;
6571
6572 goto retry;
6573 }
6574
6575 if (needs_recompute)
6576 return RETRY;
6577
6d293983 6578 return ret;
877d48d5
DV
6579}
6580
8cfb3407
VS
6581static bool pipe_config_supports_ips(struct drm_i915_private *dev_priv,
6582 struct intel_crtc_state *pipe_config)
6583{
6584 if (pipe_config->pipe_bpp > 24)
6585 return false;
6586
6587 /* HSW can handle pixel rate up to cdclk? */
2d1fe073 6588 if (IS_HASWELL(dev_priv))
8cfb3407
VS
6589 return true;
6590
6591 /*
b432e5cf
VS
6592 * We compare against max which means we must take
6593 * the increased cdclk requirement into account when
6594 * calculating the new cdclk.
6595 *
6596 * Should measure whether using a lower cdclk w/o IPS
8cfb3407
VS
6597 */
6598 return ilk_pipe_pixel_rate(pipe_config) <=
6599 dev_priv->max_cdclk_freq * 95 / 100;
6600}
6601
42db64ef 6602static void hsw_compute_ips_config(struct intel_crtc *crtc,
5cec258b 6603 struct intel_crtc_state *pipe_config)
42db64ef 6604{
8cfb3407
VS
6605 struct drm_device *dev = crtc->base.dev;
6606 struct drm_i915_private *dev_priv = dev->dev_private;
6607
d330a953 6608 pipe_config->ips_enabled = i915.enable_ips &&
8cfb3407
VS
6609 hsw_crtc_supports_ips(crtc) &&
6610 pipe_config_supports_ips(dev_priv, pipe_config);
42db64ef
PZ
6611}
6612
39acb4aa
VS
6613static bool intel_crtc_supports_double_wide(const struct intel_crtc *crtc)
6614{
6615 const struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6616
6617 /* GDG double wide on either pipe, otherwise pipe A only */
6618 return INTEL_INFO(dev_priv)->gen < 4 &&
6619 (crtc->pipe == PIPE_A || IS_I915G(dev_priv));
6620}
6621
a43f6e0f 6622static int intel_crtc_compute_config(struct intel_crtc *crtc,
5cec258b 6623 struct intel_crtc_state *pipe_config)
79e53945 6624{
a43f6e0f 6625 struct drm_device *dev = crtc->base.dev;
8bd31e67 6626 struct drm_i915_private *dev_priv = dev->dev_private;
7c5f93b0 6627 const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
f3261156 6628 int clock_limit = dev_priv->max_dotclk_freq;
89749350 6629
cf532bb2 6630 if (INTEL_INFO(dev)->gen < 4) {
f3261156 6631 clock_limit = dev_priv->max_cdclk_freq * 9 / 10;
cf532bb2
VS
6632
6633 /*
39acb4aa 6634 * Enable double wide mode when the dot clock
cf532bb2 6635 * is > 90% of the (display) core speed.
cf532bb2 6636 */
39acb4aa
VS
6637 if (intel_crtc_supports_double_wide(crtc) &&
6638 adjusted_mode->crtc_clock > clock_limit) {
f3261156 6639 clock_limit = dev_priv->max_dotclk_freq;
cf532bb2 6640 pipe_config->double_wide = true;
ad3a4479 6641 }
f3261156 6642 }
ad3a4479 6643
f3261156
VS
6644 if (adjusted_mode->crtc_clock > clock_limit) {
6645 DRM_DEBUG_KMS("requested pixel clock (%d kHz) too high (max: %d kHz, double wide: %s)\n",
6646 adjusted_mode->crtc_clock, clock_limit,
6647 yesno(pipe_config->double_wide));
6648 return -EINVAL;
2c07245f 6649 }
89749350 6650
1d1d0e27
VS
6651 /*
6652 * Pipe horizontal size must be even in:
6653 * - DVO ganged mode
6654 * - LVDS dual channel mode
6655 * - Double wide pipe
6656 */
a93e255f 6657 if ((intel_pipe_will_have_type(pipe_config, INTEL_OUTPUT_LVDS) &&
1d1d0e27
VS
6658 intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
6659 pipe_config->pipe_src_w &= ~1;
6660
8693a824
DL
6661 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
6662 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
44f46b42
CW
6663 */
6664 if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
aad941d5 6665 adjusted_mode->crtc_hsync_start == adjusted_mode->crtc_hdisplay)
e29c22c0 6666 return -EINVAL;
44f46b42 6667
f5adf94e 6668 if (HAS_IPS(dev))
a43f6e0f
DV
6669 hsw_compute_ips_config(crtc, pipe_config);
6670
877d48d5 6671 if (pipe_config->has_pch_encoder)
a43f6e0f 6672 return ironlake_fdi_compute_config(crtc, pipe_config);
877d48d5 6673
cf5a15be 6674 return 0;
79e53945
JB
6675}
6676
1652d19e
VS
6677static int skylake_get_display_clock_speed(struct drm_device *dev)
6678{
6679 struct drm_i915_private *dev_priv = to_i915(dev);
ea61791e 6680 uint32_t cdctl;
1652d19e 6681
ea61791e 6682 skl_dpll0_update(dev_priv);
1652d19e 6683
63911d72 6684 if (dev_priv->cdclk_pll.vco == 0)
709e05c3 6685 return dev_priv->cdclk_pll.ref;
1652d19e 6686
ea61791e 6687 cdctl = I915_READ(CDCLK_CTL);
1652d19e 6688
63911d72 6689 if (dev_priv->cdclk_pll.vco == 8640000) {
1652d19e
VS
6690 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6691 case CDCLK_FREQ_450_432:
6692 return 432000;
6693 case CDCLK_FREQ_337_308:
487ed2e4 6694 return 308571;
ea61791e
VS
6695 case CDCLK_FREQ_540:
6696 return 540000;
1652d19e 6697 case CDCLK_FREQ_675_617:
487ed2e4 6698 return 617143;
1652d19e 6699 default:
ea61791e 6700 MISSING_CASE(cdctl & CDCLK_FREQ_SEL_MASK);
1652d19e
VS
6701 }
6702 } else {
1652d19e
VS
6703 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6704 case CDCLK_FREQ_450_432:
6705 return 450000;
6706 case CDCLK_FREQ_337_308:
6707 return 337500;
ea61791e
VS
6708 case CDCLK_FREQ_540:
6709 return 540000;
1652d19e
VS
6710 case CDCLK_FREQ_675_617:
6711 return 675000;
6712 default:
ea61791e 6713 MISSING_CASE(cdctl & CDCLK_FREQ_SEL_MASK);
1652d19e
VS
6714 }
6715 }
6716
709e05c3 6717 return dev_priv->cdclk_pll.ref;
1652d19e
VS
6718}
6719
83d7c81f
VS
6720static void bxt_de_pll_update(struct drm_i915_private *dev_priv)
6721{
6722 u32 val;
6723
6724 dev_priv->cdclk_pll.ref = 19200;
1c3f7700 6725 dev_priv->cdclk_pll.vco = 0;
83d7c81f
VS
6726
6727 val = I915_READ(BXT_DE_PLL_ENABLE);
1c3f7700 6728 if ((val & BXT_DE_PLL_PLL_ENABLE) == 0)
83d7c81f 6729 return;
83d7c81f 6730
1c3f7700
ID
6731 if (WARN_ON((val & BXT_DE_PLL_LOCK) == 0))
6732 return;
83d7c81f
VS
6733
6734 val = I915_READ(BXT_DE_PLL_CTL);
6735 dev_priv->cdclk_pll.vco = (val & BXT_DE_PLL_RATIO_MASK) *
6736 dev_priv->cdclk_pll.ref;
6737}
6738
acd3f3d3
BP
6739static int broxton_get_display_clock_speed(struct drm_device *dev)
6740{
6741 struct drm_i915_private *dev_priv = to_i915(dev);
f5986242
VS
6742 u32 divider;
6743 int div, vco;
acd3f3d3 6744
83d7c81f
VS
6745 bxt_de_pll_update(dev_priv);
6746
f5986242
VS
6747 vco = dev_priv->cdclk_pll.vco;
6748 if (vco == 0)
6749 return dev_priv->cdclk_pll.ref;
acd3f3d3 6750
f5986242 6751 divider = I915_READ(CDCLK_CTL) & BXT_CDCLK_CD2X_DIV_SEL_MASK;
acd3f3d3 6752
f5986242 6753 switch (divider) {
acd3f3d3 6754 case BXT_CDCLK_CD2X_DIV_SEL_1:
f5986242
VS
6755 div = 2;
6756 break;
acd3f3d3 6757 case BXT_CDCLK_CD2X_DIV_SEL_1_5:
f5986242
VS
6758 div = 3;
6759 break;
acd3f3d3 6760 case BXT_CDCLK_CD2X_DIV_SEL_2:
f5986242
VS
6761 div = 4;
6762 break;
acd3f3d3 6763 case BXT_CDCLK_CD2X_DIV_SEL_4:
f5986242
VS
6764 div = 8;
6765 break;
6766 default:
6767 MISSING_CASE(divider);
6768 return dev_priv->cdclk_pll.ref;
acd3f3d3
BP
6769 }
6770
f5986242 6771 return DIV_ROUND_CLOSEST(vco, div);
acd3f3d3
BP
6772}
6773
1652d19e
VS
6774static int broadwell_get_display_clock_speed(struct drm_device *dev)
6775{
6776 struct drm_i915_private *dev_priv = dev->dev_private;
6777 uint32_t lcpll = I915_READ(LCPLL_CTL);
6778 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6779
6780 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6781 return 800000;
6782 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6783 return 450000;
6784 else if (freq == LCPLL_CLK_FREQ_450)
6785 return 450000;
6786 else if (freq == LCPLL_CLK_FREQ_54O_BDW)
6787 return 540000;
6788 else if (freq == LCPLL_CLK_FREQ_337_5_BDW)
6789 return 337500;
6790 else
6791 return 675000;
6792}
6793
6794static int haswell_get_display_clock_speed(struct drm_device *dev)
6795{
6796 struct drm_i915_private *dev_priv = dev->dev_private;
6797 uint32_t lcpll = I915_READ(LCPLL_CTL);
6798 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6799
6800 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6801 return 800000;
6802 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6803 return 450000;
6804 else if (freq == LCPLL_CLK_FREQ_450)
6805 return 450000;
6806 else if (IS_HSW_ULT(dev))
6807 return 337500;
6808 else
6809 return 540000;
79e53945
JB
6810}
6811
25eb05fc
JB
6812static int valleyview_get_display_clock_speed(struct drm_device *dev)
6813{
bfa7df01
VS
6814 return vlv_get_cck_clock_hpll(to_i915(dev), "cdclk",
6815 CCK_DISPLAY_CLOCK_CONTROL);
25eb05fc
JB
6816}
6817
b37a6434
VS
6818static int ilk_get_display_clock_speed(struct drm_device *dev)
6819{
6820 return 450000;
6821}
6822
e70236a8
JB
6823static int i945_get_display_clock_speed(struct drm_device *dev)
6824{
6825 return 400000;
6826}
79e53945 6827
e70236a8 6828static int i915_get_display_clock_speed(struct drm_device *dev)
79e53945 6829{
e907f170 6830 return 333333;
e70236a8 6831}
79e53945 6832
e70236a8
JB
6833static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
6834{
6835 return 200000;
6836}
79e53945 6837
257a7ffc
DV
6838static int pnv_get_display_clock_speed(struct drm_device *dev)
6839{
6840 u16 gcfgc = 0;
6841
6842 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6843
6844 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6845 case GC_DISPLAY_CLOCK_267_MHZ_PNV:
e907f170 6846 return 266667;
257a7ffc 6847 case GC_DISPLAY_CLOCK_333_MHZ_PNV:
e907f170 6848 return 333333;
257a7ffc 6849 case GC_DISPLAY_CLOCK_444_MHZ_PNV:
e907f170 6850 return 444444;
257a7ffc
DV
6851 case GC_DISPLAY_CLOCK_200_MHZ_PNV:
6852 return 200000;
6853 default:
6854 DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
6855 case GC_DISPLAY_CLOCK_133_MHZ_PNV:
e907f170 6856 return 133333;
257a7ffc 6857 case GC_DISPLAY_CLOCK_167_MHZ_PNV:
e907f170 6858 return 166667;
257a7ffc
DV
6859 }
6860}
6861
e70236a8
JB
6862static int i915gm_get_display_clock_speed(struct drm_device *dev)
6863{
6864 u16 gcfgc = 0;
79e53945 6865
e70236a8
JB
6866 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6867
6868 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
e907f170 6869 return 133333;
e70236a8
JB
6870 else {
6871 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6872 case GC_DISPLAY_CLOCK_333_MHZ:
e907f170 6873 return 333333;
e70236a8
JB
6874 default:
6875 case GC_DISPLAY_CLOCK_190_200_MHZ:
6876 return 190000;
79e53945 6877 }
e70236a8
JB
6878 }
6879}
6880
6881static int i865_get_display_clock_speed(struct drm_device *dev)
6882{
e907f170 6883 return 266667;
e70236a8
JB
6884}
6885
1b1d2716 6886static int i85x_get_display_clock_speed(struct drm_device *dev)
e70236a8
JB
6887{
6888 u16 hpllcc = 0;
1b1d2716 6889
65cd2b3f
VS
6890 /*
6891 * 852GM/852GMV only supports 133 MHz and the HPLLCC
6892 * encoding is different :(
6893 * FIXME is this the right way to detect 852GM/852GMV?
6894 */
6895 if (dev->pdev->revision == 0x1)
6896 return 133333;
6897
1b1d2716
VS
6898 pci_bus_read_config_word(dev->pdev->bus,
6899 PCI_DEVFN(0, 3), HPLLCC, &hpllcc);
6900
e70236a8
JB
6901 /* Assume that the hardware is in the high speed state. This
6902 * should be the default.
6903 */
6904 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
6905 case GC_CLOCK_133_200:
1b1d2716 6906 case GC_CLOCK_133_200_2:
e70236a8
JB
6907 case GC_CLOCK_100_200:
6908 return 200000;
6909 case GC_CLOCK_166_250:
6910 return 250000;
6911 case GC_CLOCK_100_133:
e907f170 6912 return 133333;
1b1d2716
VS
6913 case GC_CLOCK_133_266:
6914 case GC_CLOCK_133_266_2:
6915 case GC_CLOCK_166_266:
6916 return 266667;
e70236a8 6917 }
79e53945 6918
e70236a8
JB
6919 /* Shouldn't happen */
6920 return 0;
6921}
79e53945 6922
e70236a8
JB
6923static int i830_get_display_clock_speed(struct drm_device *dev)
6924{
e907f170 6925 return 133333;
79e53945
JB
6926}
6927
34edce2f
VS
6928static unsigned int intel_hpll_vco(struct drm_device *dev)
6929{
6930 struct drm_i915_private *dev_priv = dev->dev_private;
6931 static const unsigned int blb_vco[8] = {
6932 [0] = 3200000,
6933 [1] = 4000000,
6934 [2] = 5333333,
6935 [3] = 4800000,
6936 [4] = 6400000,
6937 };
6938 static const unsigned int pnv_vco[8] = {
6939 [0] = 3200000,
6940 [1] = 4000000,
6941 [2] = 5333333,
6942 [3] = 4800000,
6943 [4] = 2666667,
6944 };
6945 static const unsigned int cl_vco[8] = {
6946 [0] = 3200000,
6947 [1] = 4000000,
6948 [2] = 5333333,
6949 [3] = 6400000,
6950 [4] = 3333333,
6951 [5] = 3566667,
6952 [6] = 4266667,
6953 };
6954 static const unsigned int elk_vco[8] = {
6955 [0] = 3200000,
6956 [1] = 4000000,
6957 [2] = 5333333,
6958 [3] = 4800000,
6959 };
6960 static const unsigned int ctg_vco[8] = {
6961 [0] = 3200000,
6962 [1] = 4000000,
6963 [2] = 5333333,
6964 [3] = 6400000,
6965 [4] = 2666667,
6966 [5] = 4266667,
6967 };
6968 const unsigned int *vco_table;
6969 unsigned int vco;
6970 uint8_t tmp = 0;
6971
6972 /* FIXME other chipsets? */
6973 if (IS_GM45(dev))
6974 vco_table = ctg_vco;
6975 else if (IS_G4X(dev))
6976 vco_table = elk_vco;
6977 else if (IS_CRESTLINE(dev))
6978 vco_table = cl_vco;
6979 else if (IS_PINEVIEW(dev))
6980 vco_table = pnv_vco;
6981 else if (IS_G33(dev))
6982 vco_table = blb_vco;
6983 else
6984 return 0;
6985
6986 tmp = I915_READ(IS_MOBILE(dev) ? HPLLVCO_MOBILE : HPLLVCO);
6987
6988 vco = vco_table[tmp & 0x7];
6989 if (vco == 0)
6990 DRM_ERROR("Bad HPLL VCO (HPLLVCO=0x%02x)\n", tmp);
6991 else
6992 DRM_DEBUG_KMS("HPLL VCO %u kHz\n", vco);
6993
6994 return vco;
6995}
6996
6997static int gm45_get_display_clock_speed(struct drm_device *dev)
6998{
6999 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
7000 uint16_t tmp = 0;
7001
7002 pci_read_config_word(dev->pdev, GCFGC, &tmp);
7003
7004 cdclk_sel = (tmp >> 12) & 0x1;
7005
7006 switch (vco) {
7007 case 2666667:
7008 case 4000000:
7009 case 5333333:
7010 return cdclk_sel ? 333333 : 222222;
7011 case 3200000:
7012 return cdclk_sel ? 320000 : 228571;
7013 default:
7014 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u, CFGC=0x%04x\n", vco, tmp);
7015 return 222222;
7016 }
7017}
7018
7019static int i965gm_get_display_clock_speed(struct drm_device *dev)
7020{
7021 static const uint8_t div_3200[] = { 16, 10, 8 };
7022 static const uint8_t div_4000[] = { 20, 12, 10 };
7023 static const uint8_t div_5333[] = { 24, 16, 14 };
7024 const uint8_t *div_table;
7025 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
7026 uint16_t tmp = 0;
7027
7028 pci_read_config_word(dev->pdev, GCFGC, &tmp);
7029
7030 cdclk_sel = ((tmp >> 8) & 0x1f) - 1;
7031
7032 if (cdclk_sel >= ARRAY_SIZE(div_3200))
7033 goto fail;
7034
7035 switch (vco) {
7036 case 3200000:
7037 div_table = div_3200;
7038 break;
7039 case 4000000:
7040 div_table = div_4000;
7041 break;
7042 case 5333333:
7043 div_table = div_5333;
7044 break;
7045 default:
7046 goto fail;
7047 }
7048
7049 return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
7050
caf4e252 7051fail:
34edce2f
VS
7052 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%04x\n", vco, tmp);
7053 return 200000;
7054}
7055
7056static int g33_get_display_clock_speed(struct drm_device *dev)
7057{
7058 static const uint8_t div_3200[] = { 12, 10, 8, 7, 5, 16 };
7059 static const uint8_t div_4000[] = { 14, 12, 10, 8, 6, 20 };
7060 static const uint8_t div_4800[] = { 20, 14, 12, 10, 8, 24 };
7061 static const uint8_t div_5333[] = { 20, 16, 12, 12, 8, 28 };
7062 const uint8_t *div_table;
7063 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
7064 uint16_t tmp = 0;
7065
7066 pci_read_config_word(dev->pdev, GCFGC, &tmp);
7067
7068 cdclk_sel = (tmp >> 4) & 0x7;
7069
7070 if (cdclk_sel >= ARRAY_SIZE(div_3200))
7071 goto fail;
7072
7073 switch (vco) {
7074 case 3200000:
7075 div_table = div_3200;
7076 break;
7077 case 4000000:
7078 div_table = div_4000;
7079 break;
7080 case 4800000:
7081 div_table = div_4800;
7082 break;
7083 case 5333333:
7084 div_table = div_5333;
7085 break;
7086 default:
7087 goto fail;
7088 }
7089
7090 return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
7091
caf4e252 7092fail:
34edce2f
VS
7093 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%08x\n", vco, tmp);
7094 return 190476;
7095}
7096
2c07245f 7097static void
a65851af 7098intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
2c07245f 7099{
a65851af
VS
7100 while (*num > DATA_LINK_M_N_MASK ||
7101 *den > DATA_LINK_M_N_MASK) {
2c07245f
ZW
7102 *num >>= 1;
7103 *den >>= 1;
7104 }
7105}
7106
a65851af
VS
7107static void compute_m_n(unsigned int m, unsigned int n,
7108 uint32_t *ret_m, uint32_t *ret_n)
7109{
7110 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
7111 *ret_m = div_u64((uint64_t) m * *ret_n, n);
7112 intel_reduce_m_n_ratio(ret_m, ret_n);
7113}
7114
e69d0bc1
DV
7115void
7116intel_link_compute_m_n(int bits_per_pixel, int nlanes,
7117 int pixel_clock, int link_clock,
7118 struct intel_link_m_n *m_n)
2c07245f 7119{
e69d0bc1 7120 m_n->tu = 64;
a65851af
VS
7121
7122 compute_m_n(bits_per_pixel * pixel_clock,
7123 link_clock * nlanes * 8,
7124 &m_n->gmch_m, &m_n->gmch_n);
7125
7126 compute_m_n(pixel_clock, link_clock,
7127 &m_n->link_m, &m_n->link_n);
2c07245f
ZW
7128}
7129
a7615030
CW
7130static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
7131{
d330a953
JN
7132 if (i915.panel_use_ssc >= 0)
7133 return i915.panel_use_ssc != 0;
41aa3448 7134 return dev_priv->vbt.lvds_use_ssc
435793df 7135 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
a7615030
CW
7136}
7137
7429e9d4 7138static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
c65d77d8 7139{
7df00d7a 7140 return (1 << dpll->n) << 16 | dpll->m2;
7429e9d4 7141}
f47709a9 7142
7429e9d4
DV
7143static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
7144{
7145 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
c65d77d8
JB
7146}
7147
f47709a9 7148static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
190f68c5 7149 struct intel_crtc_state *crtc_state,
9e2c8475 7150 struct dpll *reduced_clock)
a7516a05 7151{
f47709a9 7152 struct drm_device *dev = crtc->base.dev;
a7516a05
JB
7153 u32 fp, fp2 = 0;
7154
7155 if (IS_PINEVIEW(dev)) {
190f68c5 7156 fp = pnv_dpll_compute_fp(&crtc_state->dpll);
a7516a05 7157 if (reduced_clock)
7429e9d4 7158 fp2 = pnv_dpll_compute_fp(reduced_clock);
a7516a05 7159 } else {
190f68c5 7160 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
a7516a05 7161 if (reduced_clock)
7429e9d4 7162 fp2 = i9xx_dpll_compute_fp(reduced_clock);
a7516a05
JB
7163 }
7164
190f68c5 7165 crtc_state->dpll_hw_state.fp0 = fp;
a7516a05 7166
f47709a9 7167 crtc->lowfreq_avail = false;
a93e255f 7168 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
ab585dea 7169 reduced_clock) {
190f68c5 7170 crtc_state->dpll_hw_state.fp1 = fp2;
f47709a9 7171 crtc->lowfreq_avail = true;
a7516a05 7172 } else {
190f68c5 7173 crtc_state->dpll_hw_state.fp1 = fp;
a7516a05
JB
7174 }
7175}
7176
5e69f97f
CML
7177static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
7178 pipe)
89b667f8
JB
7179{
7180 u32 reg_val;
7181
7182 /*
7183 * PLLB opamp always calibrates to max value of 0x3f, force enable it
7184 * and set it to a reasonable value instead.
7185 */
ab3c759a 7186 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
89b667f8
JB
7187 reg_val &= 0xffffff00;
7188 reg_val |= 0x00000030;
ab3c759a 7189 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
89b667f8 7190
ab3c759a 7191 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
89b667f8
JB
7192 reg_val &= 0x8cffffff;
7193 reg_val = 0x8c000000;
ab3c759a 7194 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
89b667f8 7195
ab3c759a 7196 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
89b667f8 7197 reg_val &= 0xffffff00;
ab3c759a 7198 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
89b667f8 7199
ab3c759a 7200 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
89b667f8
JB
7201 reg_val &= 0x00ffffff;
7202 reg_val |= 0xb0000000;
ab3c759a 7203 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
89b667f8
JB
7204}
7205
b551842d
DV
7206static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
7207 struct intel_link_m_n *m_n)
7208{
7209 struct drm_device *dev = crtc->base.dev;
7210 struct drm_i915_private *dev_priv = dev->dev_private;
7211 int pipe = crtc->pipe;
7212
e3b95f1e
DV
7213 I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7214 I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
7215 I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
7216 I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
b551842d
DV
7217}
7218
7219static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
f769cd24
VK
7220 struct intel_link_m_n *m_n,
7221 struct intel_link_m_n *m2_n2)
b551842d
DV
7222{
7223 struct drm_device *dev = crtc->base.dev;
7224 struct drm_i915_private *dev_priv = dev->dev_private;
7225 int pipe = crtc->pipe;
6e3c9717 7226 enum transcoder transcoder = crtc->config->cpu_transcoder;
b551842d
DV
7227
7228 if (INTEL_INFO(dev)->gen >= 5) {
7229 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
7230 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
7231 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
7232 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
f769cd24
VK
7233 /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
7234 * for gen < 8) and if DRRS is supported (to make sure the
7235 * registers are not unnecessarily accessed).
7236 */
44395bfe 7237 if (m2_n2 && (IS_CHERRYVIEW(dev) || INTEL_INFO(dev)->gen < 8) &&
6e3c9717 7238 crtc->config->has_drrs) {
f769cd24
VK
7239 I915_WRITE(PIPE_DATA_M2(transcoder),
7240 TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
7241 I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
7242 I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
7243 I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
7244 }
b551842d 7245 } else {
e3b95f1e
DV
7246 I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7247 I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
7248 I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
7249 I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
b551842d
DV
7250 }
7251}
7252
fe3cd48d 7253void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
03afc4a2 7254{
fe3cd48d
R
7255 struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
7256
7257 if (m_n == M1_N1) {
7258 dp_m_n = &crtc->config->dp_m_n;
7259 dp_m2_n2 = &crtc->config->dp_m2_n2;
7260 } else if (m_n == M2_N2) {
7261
7262 /*
7263 * M2_N2 registers are not supported. Hence m2_n2 divider value
7264 * needs to be programmed into M1_N1.
7265 */
7266 dp_m_n = &crtc->config->dp_m2_n2;
7267 } else {
7268 DRM_ERROR("Unsupported divider value\n");
7269 return;
7270 }
7271
6e3c9717
ACO
7272 if (crtc->config->has_pch_encoder)
7273 intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
03afc4a2 7274 else
fe3cd48d 7275 intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
03afc4a2
DV
7276}
7277
251ac862
DV
7278static void vlv_compute_dpll(struct intel_crtc *crtc,
7279 struct intel_crtc_state *pipe_config)
bdd4b6a6 7280{
03ed5cbf 7281 pipe_config->dpll_hw_state.dpll = DPLL_INTEGRATED_REF_CLK_VLV |
cd2d34d9 7282 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
03ed5cbf
VS
7283 if (crtc->pipe != PIPE_A)
7284 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
bdd4b6a6 7285
cd2d34d9 7286 /* DPLL not used with DSI, but still need the rest set up */
187a1c07 7287 if (!pipe_config->has_dsi_encoder)
cd2d34d9
VS
7288 pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE |
7289 DPLL_EXT_BUFFER_ENABLE_VLV;
7290
03ed5cbf
VS
7291 pipe_config->dpll_hw_state.dpll_md =
7292 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
7293}
bdd4b6a6 7294
03ed5cbf
VS
7295static void chv_compute_dpll(struct intel_crtc *crtc,
7296 struct intel_crtc_state *pipe_config)
7297{
7298 pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLK_CHV |
cd2d34d9 7299 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
03ed5cbf
VS
7300 if (crtc->pipe != PIPE_A)
7301 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
7302
cd2d34d9 7303 /* DPLL not used with DSI, but still need the rest set up */
187a1c07 7304 if (!pipe_config->has_dsi_encoder)
cd2d34d9
VS
7305 pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE;
7306
03ed5cbf
VS
7307 pipe_config->dpll_hw_state.dpll_md =
7308 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
bdd4b6a6
DV
7309}
7310
d288f65f 7311static void vlv_prepare_pll(struct intel_crtc *crtc,
5cec258b 7312 const struct intel_crtc_state *pipe_config)
a0c4da24 7313{
f47709a9 7314 struct drm_device *dev = crtc->base.dev;
a0c4da24 7315 struct drm_i915_private *dev_priv = dev->dev_private;
cd2d34d9 7316 enum pipe pipe = crtc->pipe;
bdd4b6a6 7317 u32 mdiv;
a0c4da24 7318 u32 bestn, bestm1, bestm2, bestp1, bestp2;
bdd4b6a6 7319 u32 coreclk, reg_val;
a0c4da24 7320
cd2d34d9
VS
7321 /* Enable Refclk */
7322 I915_WRITE(DPLL(pipe),
7323 pipe_config->dpll_hw_state.dpll &
7324 ~(DPLL_VCO_ENABLE | DPLL_EXT_BUFFER_ENABLE_VLV));
7325
7326 /* No need to actually set up the DPLL with DSI */
7327 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
7328 return;
7329
a580516d 7330 mutex_lock(&dev_priv->sb_lock);
09153000 7331
d288f65f
VS
7332 bestn = pipe_config->dpll.n;
7333 bestm1 = pipe_config->dpll.m1;
7334 bestm2 = pipe_config->dpll.m2;
7335 bestp1 = pipe_config->dpll.p1;
7336 bestp2 = pipe_config->dpll.p2;
a0c4da24 7337
89b667f8
JB
7338 /* See eDP HDMI DPIO driver vbios notes doc */
7339
7340 /* PLL B needs special handling */
bdd4b6a6 7341 if (pipe == PIPE_B)
5e69f97f 7342 vlv_pllb_recal_opamp(dev_priv, pipe);
89b667f8
JB
7343
7344 /* Set up Tx target for periodic Rcomp update */
ab3c759a 7345 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
89b667f8
JB
7346
7347 /* Disable target IRef on PLL */
ab3c759a 7348 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
89b667f8 7349 reg_val &= 0x00ffffff;
ab3c759a 7350 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
89b667f8
JB
7351
7352 /* Disable fast lock */
ab3c759a 7353 vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
89b667f8
JB
7354
7355 /* Set idtafcrecal before PLL is enabled */
a0c4da24
JB
7356 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
7357 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
7358 mdiv |= ((bestn << DPIO_N_SHIFT));
a0c4da24 7359 mdiv |= (1 << DPIO_K_SHIFT);
7df5080b
JB
7360
7361 /*
7362 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
7363 * but we don't support that).
7364 * Note: don't use the DAC post divider as it seems unstable.
7365 */
7366 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
ab3c759a 7367 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
a0c4da24 7368
a0c4da24 7369 mdiv |= DPIO_ENABLE_CALIBRATION;
ab3c759a 7370 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
a0c4da24 7371
89b667f8 7372 /* Set HBR and RBR LPF coefficients */
d288f65f 7373 if (pipe_config->port_clock == 162000 ||
409ee761
ACO
7374 intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG) ||
7375 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
ab3c759a 7376 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
885b0120 7377 0x009f0003);
89b667f8 7378 else
ab3c759a 7379 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
89b667f8
JB
7380 0x00d0000f);
7381
681a8504 7382 if (pipe_config->has_dp_encoder) {
89b667f8 7383 /* Use SSC source */
bdd4b6a6 7384 if (pipe == PIPE_A)
ab3c759a 7385 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
7386 0x0df40000);
7387 else
ab3c759a 7388 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
7389 0x0df70000);
7390 } else { /* HDMI or VGA */
7391 /* Use bend source */
bdd4b6a6 7392 if (pipe == PIPE_A)
ab3c759a 7393 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
7394 0x0df70000);
7395 else
ab3c759a 7396 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
7397 0x0df40000);
7398 }
a0c4da24 7399
ab3c759a 7400 coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
89b667f8 7401 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
409ee761
ACO
7402 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
7403 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
89b667f8 7404 coreclk |= 0x01000000;
ab3c759a 7405 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
a0c4da24 7406
ab3c759a 7407 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
a580516d 7408 mutex_unlock(&dev_priv->sb_lock);
a0c4da24
JB
7409}
7410
d288f65f 7411static void chv_prepare_pll(struct intel_crtc *crtc,
5cec258b 7412 const struct intel_crtc_state *pipe_config)
9d556c99
CML
7413{
7414 struct drm_device *dev = crtc->base.dev;
7415 struct drm_i915_private *dev_priv = dev->dev_private;
cd2d34d9 7416 enum pipe pipe = crtc->pipe;
9d556c99 7417 enum dpio_channel port = vlv_pipe_to_channel(pipe);
9cbe40c1 7418 u32 loopfilter, tribuf_calcntr;
9d556c99 7419 u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
a945ce7e 7420 u32 dpio_val;
9cbe40c1 7421 int vco;
9d556c99 7422
cd2d34d9
VS
7423 /* Enable Refclk and SSC */
7424 I915_WRITE(DPLL(pipe),
7425 pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
7426
7427 /* No need to actually set up the DPLL with DSI */
7428 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
7429 return;
7430
d288f65f
VS
7431 bestn = pipe_config->dpll.n;
7432 bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
7433 bestm1 = pipe_config->dpll.m1;
7434 bestm2 = pipe_config->dpll.m2 >> 22;
7435 bestp1 = pipe_config->dpll.p1;
7436 bestp2 = pipe_config->dpll.p2;
9cbe40c1 7437 vco = pipe_config->dpll.vco;
a945ce7e 7438 dpio_val = 0;
9cbe40c1 7439 loopfilter = 0;
9d556c99 7440
a580516d 7441 mutex_lock(&dev_priv->sb_lock);
9d556c99 7442
9d556c99
CML
7443 /* p1 and p2 divider */
7444 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
7445 5 << DPIO_CHV_S1_DIV_SHIFT |
7446 bestp1 << DPIO_CHV_P1_DIV_SHIFT |
7447 bestp2 << DPIO_CHV_P2_DIV_SHIFT |
7448 1 << DPIO_CHV_K_DIV_SHIFT);
7449
7450 /* Feedback post-divider - m2 */
7451 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
7452
7453 /* Feedback refclk divider - n and m1 */
7454 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
7455 DPIO_CHV_M1_DIV_BY_2 |
7456 1 << DPIO_CHV_N_DIV_SHIFT);
7457
7458 /* M2 fraction division */
25a25dfc 7459 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
9d556c99
CML
7460
7461 /* M2 fraction division enable */
a945ce7e
VP
7462 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
7463 dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
7464 dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
7465 if (bestm2_frac)
7466 dpio_val |= DPIO_CHV_FRAC_DIV_EN;
7467 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
9d556c99 7468
de3a0fde
VP
7469 /* Program digital lock detect threshold */
7470 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
7471 dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
7472 DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
7473 dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
7474 if (!bestm2_frac)
7475 dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
7476 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
7477
9d556c99 7478 /* Loop filter */
9cbe40c1
VP
7479 if (vco == 5400000) {
7480 loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
7481 loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
7482 loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
7483 tribuf_calcntr = 0x9;
7484 } else if (vco <= 6200000) {
7485 loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
7486 loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
7487 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7488 tribuf_calcntr = 0x9;
7489 } else if (vco <= 6480000) {
7490 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7491 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7492 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7493 tribuf_calcntr = 0x8;
7494 } else {
7495 /* Not supported. Apply the same limits as in the max case */
7496 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7497 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7498 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7499 tribuf_calcntr = 0;
7500 }
9d556c99
CML
7501 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
7502
968040b2 7503 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
9cbe40c1
VP
7504 dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
7505 dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
7506 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
7507
9d556c99
CML
7508 /* AFC Recal */
7509 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
7510 vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
7511 DPIO_AFC_RECAL);
7512
a580516d 7513 mutex_unlock(&dev_priv->sb_lock);
9d556c99
CML
7514}
7515
d288f65f
VS
7516/**
7517 * vlv_force_pll_on - forcibly enable just the PLL
7518 * @dev_priv: i915 private structure
7519 * @pipe: pipe PLL to enable
7520 * @dpll: PLL configuration
7521 *
7522 * Enable the PLL for @pipe using the supplied @dpll config. To be used
7523 * in cases where we need the PLL enabled even when @pipe is not going to
7524 * be enabled.
7525 */
3f36b937
TU
7526int vlv_force_pll_on(struct drm_device *dev, enum pipe pipe,
7527 const struct dpll *dpll)
d288f65f
VS
7528{
7529 struct intel_crtc *crtc =
7530 to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
3f36b937
TU
7531 struct intel_crtc_state *pipe_config;
7532
7533 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
7534 if (!pipe_config)
7535 return -ENOMEM;
7536
7537 pipe_config->base.crtc = &crtc->base;
7538 pipe_config->pixel_multiplier = 1;
7539 pipe_config->dpll = *dpll;
d288f65f
VS
7540
7541 if (IS_CHERRYVIEW(dev)) {
3f36b937
TU
7542 chv_compute_dpll(crtc, pipe_config);
7543 chv_prepare_pll(crtc, pipe_config);
7544 chv_enable_pll(crtc, pipe_config);
d288f65f 7545 } else {
3f36b937
TU
7546 vlv_compute_dpll(crtc, pipe_config);
7547 vlv_prepare_pll(crtc, pipe_config);
7548 vlv_enable_pll(crtc, pipe_config);
d288f65f 7549 }
3f36b937
TU
7550
7551 kfree(pipe_config);
7552
7553 return 0;
d288f65f
VS
7554}
7555
7556/**
7557 * vlv_force_pll_off - forcibly disable just the PLL
7558 * @dev_priv: i915 private structure
7559 * @pipe: pipe PLL to disable
7560 *
7561 * Disable the PLL for @pipe. To be used in cases where we need
7562 * the PLL enabled even when @pipe is not going to be enabled.
7563 */
7564void vlv_force_pll_off(struct drm_device *dev, enum pipe pipe)
7565{
7566 if (IS_CHERRYVIEW(dev))
7567 chv_disable_pll(to_i915(dev), pipe);
7568 else
7569 vlv_disable_pll(to_i915(dev), pipe);
7570}
7571
251ac862
DV
7572static void i9xx_compute_dpll(struct intel_crtc *crtc,
7573 struct intel_crtc_state *crtc_state,
9e2c8475 7574 struct dpll *reduced_clock)
eb1cbe48 7575{
f47709a9 7576 struct drm_device *dev = crtc->base.dev;
eb1cbe48 7577 struct drm_i915_private *dev_priv = dev->dev_private;
eb1cbe48
DV
7578 u32 dpll;
7579 bool is_sdvo;
190f68c5 7580 struct dpll *clock = &crtc_state->dpll;
eb1cbe48 7581
190f68c5 7582 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
2a8f64ca 7583
a93e255f
ACO
7584 is_sdvo = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO) ||
7585 intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI);
eb1cbe48
DV
7586
7587 dpll = DPLL_VGA_MODE_DIS;
7588
a93e255f 7589 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
eb1cbe48
DV
7590 dpll |= DPLLB_MODE_LVDS;
7591 else
7592 dpll |= DPLLB_MODE_DAC_SERIAL;
6cc5f341 7593
ef1b460d 7594 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
190f68c5 7595 dpll |= (crtc_state->pixel_multiplier - 1)
198a037f 7596 << SDVO_MULTIPLIER_SHIFT_HIRES;
eb1cbe48 7597 }
198a037f
DV
7598
7599 if (is_sdvo)
4a33e48d 7600 dpll |= DPLL_SDVO_HIGH_SPEED;
198a037f 7601
190f68c5 7602 if (crtc_state->has_dp_encoder)
4a33e48d 7603 dpll |= DPLL_SDVO_HIGH_SPEED;
eb1cbe48
DV
7604
7605 /* compute bitmask from p1 value */
7606 if (IS_PINEVIEW(dev))
7607 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
7608 else {
7609 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7610 if (IS_G4X(dev) && reduced_clock)
7611 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
7612 }
7613 switch (clock->p2) {
7614 case 5:
7615 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
7616 break;
7617 case 7:
7618 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
7619 break;
7620 case 10:
7621 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
7622 break;
7623 case 14:
7624 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
7625 break;
7626 }
7627 if (INTEL_INFO(dev)->gen >= 4)
7628 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
7629
190f68c5 7630 if (crtc_state->sdvo_tv_clock)
eb1cbe48 7631 dpll |= PLL_REF_INPUT_TVCLKINBC;
a93e255f 7632 else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
ceb41007 7633 intel_panel_use_ssc(dev_priv))
eb1cbe48
DV
7634 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7635 else
7636 dpll |= PLL_REF_INPUT_DREFCLK;
7637
7638 dpll |= DPLL_VCO_ENABLE;
190f68c5 7639 crtc_state->dpll_hw_state.dpll = dpll;
8bcc2795 7640
eb1cbe48 7641 if (INTEL_INFO(dev)->gen >= 4) {
190f68c5 7642 u32 dpll_md = (crtc_state->pixel_multiplier - 1)
ef1b460d 7643 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
190f68c5 7644 crtc_state->dpll_hw_state.dpll_md = dpll_md;
eb1cbe48
DV
7645 }
7646}
7647
251ac862
DV
7648static void i8xx_compute_dpll(struct intel_crtc *crtc,
7649 struct intel_crtc_state *crtc_state,
9e2c8475 7650 struct dpll *reduced_clock)
eb1cbe48 7651{
f47709a9 7652 struct drm_device *dev = crtc->base.dev;
eb1cbe48 7653 struct drm_i915_private *dev_priv = dev->dev_private;
eb1cbe48 7654 u32 dpll;
190f68c5 7655 struct dpll *clock = &crtc_state->dpll;
eb1cbe48 7656
190f68c5 7657 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
2a8f64ca 7658
eb1cbe48
DV
7659 dpll = DPLL_VGA_MODE_DIS;
7660
a93e255f 7661 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
eb1cbe48
DV
7662 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7663 } else {
7664 if (clock->p1 == 2)
7665 dpll |= PLL_P1_DIVIDE_BY_TWO;
7666 else
7667 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7668 if (clock->p2 == 4)
7669 dpll |= PLL_P2_DIVIDE_BY_4;
7670 }
7671
a93e255f 7672 if (!IS_I830(dev) && intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
4a33e48d
DV
7673 dpll |= DPLL_DVO_2X_MODE;
7674
a93e255f 7675 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
ceb41007 7676 intel_panel_use_ssc(dev_priv))
eb1cbe48
DV
7677 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7678 else
7679 dpll |= PLL_REF_INPUT_DREFCLK;
7680
7681 dpll |= DPLL_VCO_ENABLE;
190f68c5 7682 crtc_state->dpll_hw_state.dpll = dpll;
eb1cbe48
DV
7683}
7684
8a654f3b 7685static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
b0e77b9c
PZ
7686{
7687 struct drm_device *dev = intel_crtc->base.dev;
7688 struct drm_i915_private *dev_priv = dev->dev_private;
7689 enum pipe pipe = intel_crtc->pipe;
6e3c9717 7690 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
7c5f93b0 7691 const struct drm_display_mode *adjusted_mode = &intel_crtc->config->base.adjusted_mode;
1caea6e9
VS
7692 uint32_t crtc_vtotal, crtc_vblank_end;
7693 int vsyncshift = 0;
4d8a62ea
DV
7694
7695 /* We need to be careful not to changed the adjusted mode, for otherwise
7696 * the hw state checker will get angry at the mismatch. */
7697 crtc_vtotal = adjusted_mode->crtc_vtotal;
7698 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
b0e77b9c 7699
609aeaca 7700 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
b0e77b9c 7701 /* the chip adds 2 halflines automatically */
4d8a62ea
DV
7702 crtc_vtotal -= 1;
7703 crtc_vblank_end -= 1;
609aeaca 7704
409ee761 7705 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
609aeaca
VS
7706 vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
7707 else
7708 vsyncshift = adjusted_mode->crtc_hsync_start -
7709 adjusted_mode->crtc_htotal / 2;
1caea6e9
VS
7710 if (vsyncshift < 0)
7711 vsyncshift += adjusted_mode->crtc_htotal;
b0e77b9c
PZ
7712 }
7713
7714 if (INTEL_INFO(dev)->gen > 3)
fe2b8f9d 7715 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
b0e77b9c 7716
fe2b8f9d 7717 I915_WRITE(HTOTAL(cpu_transcoder),
b0e77b9c
PZ
7718 (adjusted_mode->crtc_hdisplay - 1) |
7719 ((adjusted_mode->crtc_htotal - 1) << 16));
fe2b8f9d 7720 I915_WRITE(HBLANK(cpu_transcoder),
b0e77b9c
PZ
7721 (adjusted_mode->crtc_hblank_start - 1) |
7722 ((adjusted_mode->crtc_hblank_end - 1) << 16));
fe2b8f9d 7723 I915_WRITE(HSYNC(cpu_transcoder),
b0e77b9c
PZ
7724 (adjusted_mode->crtc_hsync_start - 1) |
7725 ((adjusted_mode->crtc_hsync_end - 1) << 16));
7726
fe2b8f9d 7727 I915_WRITE(VTOTAL(cpu_transcoder),
b0e77b9c 7728 (adjusted_mode->crtc_vdisplay - 1) |
4d8a62ea 7729 ((crtc_vtotal - 1) << 16));
fe2b8f9d 7730 I915_WRITE(VBLANK(cpu_transcoder),
b0e77b9c 7731 (adjusted_mode->crtc_vblank_start - 1) |
4d8a62ea 7732 ((crtc_vblank_end - 1) << 16));
fe2b8f9d 7733 I915_WRITE(VSYNC(cpu_transcoder),
b0e77b9c
PZ
7734 (adjusted_mode->crtc_vsync_start - 1) |
7735 ((adjusted_mode->crtc_vsync_end - 1) << 16));
7736
b5e508d4
PZ
7737 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
7738 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
7739 * documented on the DDI_FUNC_CTL register description, EDP Input Select
7740 * bits. */
7741 if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
7742 (pipe == PIPE_B || pipe == PIPE_C))
7743 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
7744
bc58be60
JN
7745}
7746
7747static void intel_set_pipe_src_size(struct intel_crtc *intel_crtc)
7748{
7749 struct drm_device *dev = intel_crtc->base.dev;
7750 struct drm_i915_private *dev_priv = dev->dev_private;
7751 enum pipe pipe = intel_crtc->pipe;
7752
b0e77b9c
PZ
7753 /* pipesrc controls the size that is scaled from, which should
7754 * always be the user's requested size.
7755 */
7756 I915_WRITE(PIPESRC(pipe),
6e3c9717
ACO
7757 ((intel_crtc->config->pipe_src_w - 1) << 16) |
7758 (intel_crtc->config->pipe_src_h - 1));
b0e77b9c
PZ
7759}
7760
1bd1bd80 7761static void intel_get_pipe_timings(struct intel_crtc *crtc,
5cec258b 7762 struct intel_crtc_state *pipe_config)
1bd1bd80
DV
7763{
7764 struct drm_device *dev = crtc->base.dev;
7765 struct drm_i915_private *dev_priv = dev->dev_private;
7766 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
7767 uint32_t tmp;
7768
7769 tmp = I915_READ(HTOTAL(cpu_transcoder));
2d112de7
ACO
7770 pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
7771 pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 7772 tmp = I915_READ(HBLANK(cpu_transcoder));
2d112de7
ACO
7773 pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
7774 pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 7775 tmp = I915_READ(HSYNC(cpu_transcoder));
2d112de7
ACO
7776 pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
7777 pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80
DV
7778
7779 tmp = I915_READ(VTOTAL(cpu_transcoder));
2d112de7
ACO
7780 pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
7781 pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 7782 tmp = I915_READ(VBLANK(cpu_transcoder));
2d112de7
ACO
7783 pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
7784 pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 7785 tmp = I915_READ(VSYNC(cpu_transcoder));
2d112de7
ACO
7786 pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
7787 pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80
DV
7788
7789 if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
2d112de7
ACO
7790 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
7791 pipe_config->base.adjusted_mode.crtc_vtotal += 1;
7792 pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
1bd1bd80 7793 }
bc58be60
JN
7794}
7795
7796static void intel_get_pipe_src_size(struct intel_crtc *crtc,
7797 struct intel_crtc_state *pipe_config)
7798{
7799 struct drm_device *dev = crtc->base.dev;
7800 struct drm_i915_private *dev_priv = dev->dev_private;
7801 u32 tmp;
1bd1bd80
DV
7802
7803 tmp = I915_READ(PIPESRC(crtc->pipe));
37327abd
VS
7804 pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
7805 pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
7806
2d112de7
ACO
7807 pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
7808 pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
1bd1bd80
DV
7809}
7810
f6a83288 7811void intel_mode_from_pipe_config(struct drm_display_mode *mode,
5cec258b 7812 struct intel_crtc_state *pipe_config)
babea61d 7813{
2d112de7
ACO
7814 mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
7815 mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
7816 mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
7817 mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
babea61d 7818
2d112de7
ACO
7819 mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
7820 mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
7821 mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
7822 mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
babea61d 7823
2d112de7 7824 mode->flags = pipe_config->base.adjusted_mode.flags;
cd13f5ab 7825 mode->type = DRM_MODE_TYPE_DRIVER;
babea61d 7826
2d112de7
ACO
7827 mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
7828 mode->flags |= pipe_config->base.adjusted_mode.flags;
cd13f5ab
ML
7829
7830 mode->hsync = drm_mode_hsync(mode);
7831 mode->vrefresh = drm_mode_vrefresh(mode);
7832 drm_mode_set_name(mode);
babea61d
JB
7833}
7834
84b046f3
DV
7835static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
7836{
7837 struct drm_device *dev = intel_crtc->base.dev;
7838 struct drm_i915_private *dev_priv = dev->dev_private;
7839 uint32_t pipeconf;
7840
9f11a9e4 7841 pipeconf = 0;
84b046f3 7842
b6b5d049
VS
7843 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
7844 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
7845 pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
67c72a12 7846
6e3c9717 7847 if (intel_crtc->config->double_wide)
cf532bb2 7848 pipeconf |= PIPECONF_DOUBLE_WIDE;
84b046f3 7849
ff9ce46e 7850 /* only g4x and later have fancy bpc/dither controls */
666a4537 7851 if (IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
ff9ce46e 7852 /* Bspec claims that we can't use dithering for 30bpp pipes. */
6e3c9717 7853 if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
ff9ce46e 7854 pipeconf |= PIPECONF_DITHER_EN |
84b046f3 7855 PIPECONF_DITHER_TYPE_SP;
84b046f3 7856
6e3c9717 7857 switch (intel_crtc->config->pipe_bpp) {
ff9ce46e
DV
7858 case 18:
7859 pipeconf |= PIPECONF_6BPC;
7860 break;
7861 case 24:
7862 pipeconf |= PIPECONF_8BPC;
7863 break;
7864 case 30:
7865 pipeconf |= PIPECONF_10BPC;
7866 break;
7867 default:
7868 /* Case prevented by intel_choose_pipe_bpp_dither. */
7869 BUG();
84b046f3
DV
7870 }
7871 }
7872
7873 if (HAS_PIPE_CXSR(dev)) {
7874 if (intel_crtc->lowfreq_avail) {
7875 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
7876 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
7877 } else {
7878 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
84b046f3
DV
7879 }
7880 }
7881
6e3c9717 7882 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
efc2cfff 7883 if (INTEL_INFO(dev)->gen < 4 ||
409ee761 7884 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
efc2cfff
VS
7885 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
7886 else
7887 pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
7888 } else
84b046f3
DV
7889 pipeconf |= PIPECONF_PROGRESSIVE;
7890
666a4537
WB
7891 if ((IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) &&
7892 intel_crtc->config->limited_color_range)
9f11a9e4 7893 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
9c8e09b7 7894
84b046f3
DV
7895 I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
7896 POSTING_READ(PIPECONF(intel_crtc->pipe));
7897}
7898
81c97f52
ACO
7899static int i8xx_crtc_compute_clock(struct intel_crtc *crtc,
7900 struct intel_crtc_state *crtc_state)
7901{
7902 struct drm_device *dev = crtc->base.dev;
7903 struct drm_i915_private *dev_priv = dev->dev_private;
1b6f4958 7904 const struct intel_limit *limit;
81c97f52
ACO
7905 int refclk = 48000;
7906
7907 memset(&crtc_state->dpll_hw_state, 0,
7908 sizeof(crtc_state->dpll_hw_state));
7909
7910 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
7911 if (intel_panel_use_ssc(dev_priv)) {
7912 refclk = dev_priv->vbt.lvds_ssc_freq;
7913 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
7914 }
7915
7916 limit = &intel_limits_i8xx_lvds;
7917 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO)) {
7918 limit = &intel_limits_i8xx_dvo;
7919 } else {
7920 limit = &intel_limits_i8xx_dac;
7921 }
7922
7923 if (!crtc_state->clock_set &&
7924 !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7925 refclk, NULL, &crtc_state->dpll)) {
7926 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7927 return -EINVAL;
7928 }
7929
7930 i8xx_compute_dpll(crtc, crtc_state, NULL);
7931
7932 return 0;
7933}
7934
19ec6693
ACO
7935static int g4x_crtc_compute_clock(struct intel_crtc *crtc,
7936 struct intel_crtc_state *crtc_state)
7937{
7938 struct drm_device *dev = crtc->base.dev;
7939 struct drm_i915_private *dev_priv = dev->dev_private;
1b6f4958 7940 const struct intel_limit *limit;
19ec6693
ACO
7941 int refclk = 96000;
7942
7943 memset(&crtc_state->dpll_hw_state, 0,
7944 sizeof(crtc_state->dpll_hw_state));
7945
7946 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
7947 if (intel_panel_use_ssc(dev_priv)) {
7948 refclk = dev_priv->vbt.lvds_ssc_freq;
7949 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
7950 }
7951
7952 if (intel_is_dual_link_lvds(dev))
7953 limit = &intel_limits_g4x_dual_channel_lvds;
7954 else
7955 limit = &intel_limits_g4x_single_channel_lvds;
7956 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI) ||
7957 intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
7958 limit = &intel_limits_g4x_hdmi;
7959 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO)) {
7960 limit = &intel_limits_g4x_sdvo;
7961 } else {
7962 /* The option is for other outputs */
7963 limit = &intel_limits_i9xx_sdvo;
7964 }
7965
7966 if (!crtc_state->clock_set &&
7967 !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7968 refclk, NULL, &crtc_state->dpll)) {
7969 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7970 return -EINVAL;
7971 }
7972
7973 i9xx_compute_dpll(crtc, crtc_state, NULL);
7974
7975 return 0;
7976}
7977
70e8aa21
ACO
7978static int pnv_crtc_compute_clock(struct intel_crtc *crtc,
7979 struct intel_crtc_state *crtc_state)
7980{
7981 struct drm_device *dev = crtc->base.dev;
7982 struct drm_i915_private *dev_priv = dev->dev_private;
1b6f4958 7983 const struct intel_limit *limit;
70e8aa21
ACO
7984 int refclk = 96000;
7985
7986 memset(&crtc_state->dpll_hw_state, 0,
7987 sizeof(crtc_state->dpll_hw_state));
7988
7989 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
7990 if (intel_panel_use_ssc(dev_priv)) {
7991 refclk = dev_priv->vbt.lvds_ssc_freq;
7992 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
7993 }
7994
7995 limit = &intel_limits_pineview_lvds;
7996 } else {
7997 limit = &intel_limits_pineview_sdvo;
7998 }
7999
8000 if (!crtc_state->clock_set &&
8001 !pnv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8002 refclk, NULL, &crtc_state->dpll)) {
8003 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8004 return -EINVAL;
8005 }
8006
8007 i9xx_compute_dpll(crtc, crtc_state, NULL);
8008
8009 return 0;
8010}
8011
190f68c5
ACO
8012static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
8013 struct intel_crtc_state *crtc_state)
79e53945 8014{
c7653199 8015 struct drm_device *dev = crtc->base.dev;
79e53945 8016 struct drm_i915_private *dev_priv = dev->dev_private;
1b6f4958 8017 const struct intel_limit *limit;
81c97f52 8018 int refclk = 96000;
79e53945 8019
dd3cd74a
ACO
8020 memset(&crtc_state->dpll_hw_state, 0,
8021 sizeof(crtc_state->dpll_hw_state));
8022
70e8aa21
ACO
8023 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
8024 if (intel_panel_use_ssc(dev_priv)) {
8025 refclk = dev_priv->vbt.lvds_ssc_freq;
8026 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
8027 }
43565a06 8028
70e8aa21
ACO
8029 limit = &intel_limits_i9xx_lvds;
8030 } else {
8031 limit = &intel_limits_i9xx_sdvo;
81c97f52 8032 }
79e53945 8033
70e8aa21
ACO
8034 if (!crtc_state->clock_set &&
8035 !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8036 refclk, NULL, &crtc_state->dpll)) {
8037 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8038 return -EINVAL;
f47709a9 8039 }
7026d4ac 8040
81c97f52 8041 i9xx_compute_dpll(crtc, crtc_state, NULL);
79e53945 8042
c8f7a0db 8043 return 0;
f564048e
EA
8044}
8045
65b3d6a9
ACO
8046static int chv_crtc_compute_clock(struct intel_crtc *crtc,
8047 struct intel_crtc_state *crtc_state)
8048{
8049 int refclk = 100000;
1b6f4958 8050 const struct intel_limit *limit = &intel_limits_chv;
65b3d6a9
ACO
8051
8052 memset(&crtc_state->dpll_hw_state, 0,
8053 sizeof(crtc_state->dpll_hw_state));
8054
65b3d6a9
ACO
8055 if (!crtc_state->clock_set &&
8056 !chv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8057 refclk, NULL, &crtc_state->dpll)) {
8058 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8059 return -EINVAL;
8060 }
8061
8062 chv_compute_dpll(crtc, crtc_state);
8063
8064 return 0;
8065}
8066
8067static int vlv_crtc_compute_clock(struct intel_crtc *crtc,
8068 struct intel_crtc_state *crtc_state)
8069{
8070 int refclk = 100000;
1b6f4958 8071 const struct intel_limit *limit = &intel_limits_vlv;
65b3d6a9
ACO
8072
8073 memset(&crtc_state->dpll_hw_state, 0,
8074 sizeof(crtc_state->dpll_hw_state));
8075
65b3d6a9
ACO
8076 if (!crtc_state->clock_set &&
8077 !vlv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8078 refclk, NULL, &crtc_state->dpll)) {
8079 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8080 return -EINVAL;
8081 }
8082
8083 vlv_compute_dpll(crtc, crtc_state);
8084
8085 return 0;
8086}
8087
2fa2fe9a 8088static void i9xx_get_pfit_config(struct intel_crtc *crtc,
5cec258b 8089 struct intel_crtc_state *pipe_config)
2fa2fe9a
DV
8090{
8091 struct drm_device *dev = crtc->base.dev;
8092 struct drm_i915_private *dev_priv = dev->dev_private;
8093 uint32_t tmp;
8094
dc9e7dec
VS
8095 if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
8096 return;
8097
2fa2fe9a 8098 tmp = I915_READ(PFIT_CONTROL);
06922821
DV
8099 if (!(tmp & PFIT_ENABLE))
8100 return;
2fa2fe9a 8101
06922821 8102 /* Check whether the pfit is attached to our pipe. */
2fa2fe9a
DV
8103 if (INTEL_INFO(dev)->gen < 4) {
8104 if (crtc->pipe != PIPE_B)
8105 return;
2fa2fe9a
DV
8106 } else {
8107 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
8108 return;
8109 }
8110
06922821 8111 pipe_config->gmch_pfit.control = tmp;
2fa2fe9a 8112 pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
2fa2fe9a
DV
8113}
8114
acbec814 8115static void vlv_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 8116 struct intel_crtc_state *pipe_config)
acbec814
JB
8117{
8118 struct drm_device *dev = crtc->base.dev;
8119 struct drm_i915_private *dev_priv = dev->dev_private;
8120 int pipe = pipe_config->cpu_transcoder;
9e2c8475 8121 struct dpll clock;
acbec814 8122 u32 mdiv;
662c6ecb 8123 int refclk = 100000;
acbec814 8124
b521973b
VS
8125 /* In case of DSI, DPLL will not be used */
8126 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
f573de5a
SK
8127 return;
8128
a580516d 8129 mutex_lock(&dev_priv->sb_lock);
ab3c759a 8130 mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
a580516d 8131 mutex_unlock(&dev_priv->sb_lock);
acbec814
JB
8132
8133 clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
8134 clock.m2 = mdiv & DPIO_M2DIV_MASK;
8135 clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
8136 clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
8137 clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
8138
dccbea3b 8139 pipe_config->port_clock = vlv_calc_dpll_params(refclk, &clock);
acbec814
JB
8140}
8141
5724dbd1
DL
8142static void
8143i9xx_get_initial_plane_config(struct intel_crtc *crtc,
8144 struct intel_initial_plane_config *plane_config)
1ad292b5
JB
8145{
8146 struct drm_device *dev = crtc->base.dev;
8147 struct drm_i915_private *dev_priv = dev->dev_private;
8148 u32 val, base, offset;
8149 int pipe = crtc->pipe, plane = crtc->plane;
8150 int fourcc, pixel_format;
6761dd31 8151 unsigned int aligned_height;
b113d5ee 8152 struct drm_framebuffer *fb;
1b842c89 8153 struct intel_framebuffer *intel_fb;
1ad292b5 8154
42a7b088
DL
8155 val = I915_READ(DSPCNTR(plane));
8156 if (!(val & DISPLAY_PLANE_ENABLE))
8157 return;
8158
d9806c9f 8159 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
1b842c89 8160 if (!intel_fb) {
1ad292b5
JB
8161 DRM_DEBUG_KMS("failed to alloc fb\n");
8162 return;
8163 }
8164
1b842c89
DL
8165 fb = &intel_fb->base;
8166
18c5247e
DV
8167 if (INTEL_INFO(dev)->gen >= 4) {
8168 if (val & DISPPLANE_TILED) {
49af449b 8169 plane_config->tiling = I915_TILING_X;
18c5247e
DV
8170 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
8171 }
8172 }
1ad292b5
JB
8173
8174 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
b35d63fa 8175 fourcc = i9xx_format_to_fourcc(pixel_format);
b113d5ee
DL
8176 fb->pixel_format = fourcc;
8177 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
1ad292b5
JB
8178
8179 if (INTEL_INFO(dev)->gen >= 4) {
49af449b 8180 if (plane_config->tiling)
1ad292b5
JB
8181 offset = I915_READ(DSPTILEOFF(plane));
8182 else
8183 offset = I915_READ(DSPLINOFF(plane));
8184 base = I915_READ(DSPSURF(plane)) & 0xfffff000;
8185 } else {
8186 base = I915_READ(DSPADDR(plane));
8187 }
8188 plane_config->base = base;
8189
8190 val = I915_READ(PIPESRC(pipe));
b113d5ee
DL
8191 fb->width = ((val >> 16) & 0xfff) + 1;
8192 fb->height = ((val >> 0) & 0xfff) + 1;
1ad292b5
JB
8193
8194 val = I915_READ(DSPSTRIDE(pipe));
b113d5ee 8195 fb->pitches[0] = val & 0xffffffc0;
1ad292b5 8196
b113d5ee 8197 aligned_height = intel_fb_align_height(dev, fb->height,
091df6cb
DV
8198 fb->pixel_format,
8199 fb->modifier[0]);
1ad292b5 8200
f37b5c2b 8201 plane_config->size = fb->pitches[0] * aligned_height;
1ad292b5 8202
2844a921
DL
8203 DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
8204 pipe_name(pipe), plane, fb->width, fb->height,
8205 fb->bits_per_pixel, base, fb->pitches[0],
8206 plane_config->size);
1ad292b5 8207
2d14030b 8208 plane_config->fb = intel_fb;
1ad292b5
JB
8209}
8210
70b23a98 8211static void chv_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 8212 struct intel_crtc_state *pipe_config)
70b23a98
VS
8213{
8214 struct drm_device *dev = crtc->base.dev;
8215 struct drm_i915_private *dev_priv = dev->dev_private;
8216 int pipe = pipe_config->cpu_transcoder;
8217 enum dpio_channel port = vlv_pipe_to_channel(pipe);
9e2c8475 8218 struct dpll clock;
0d7b6b11 8219 u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2, pll_dw3;
70b23a98
VS
8220 int refclk = 100000;
8221
b521973b
VS
8222 /* In case of DSI, DPLL will not be used */
8223 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
8224 return;
8225
a580516d 8226 mutex_lock(&dev_priv->sb_lock);
70b23a98
VS
8227 cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
8228 pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
8229 pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
8230 pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
0d7b6b11 8231 pll_dw3 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
a580516d 8232 mutex_unlock(&dev_priv->sb_lock);
70b23a98
VS
8233
8234 clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
0d7b6b11
ID
8235 clock.m2 = (pll_dw0 & 0xff) << 22;
8236 if (pll_dw3 & DPIO_CHV_FRAC_DIV_EN)
8237 clock.m2 |= pll_dw2 & 0x3fffff;
70b23a98
VS
8238 clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
8239 clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
8240 clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
8241
dccbea3b 8242 pipe_config->port_clock = chv_calc_dpll_params(refclk, &clock);
70b23a98
VS
8243}
8244
0e8ffe1b 8245static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
5cec258b 8246 struct intel_crtc_state *pipe_config)
0e8ffe1b
DV
8247{
8248 struct drm_device *dev = crtc->base.dev;
8249 struct drm_i915_private *dev_priv = dev->dev_private;
1729050e 8250 enum intel_display_power_domain power_domain;
0e8ffe1b 8251 uint32_t tmp;
1729050e 8252 bool ret;
0e8ffe1b 8253
1729050e
ID
8254 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
8255 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
b5482bd0
ID
8256 return false;
8257
e143a21c 8258 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
8106ddbd 8259 pipe_config->shared_dpll = NULL;
eccb140b 8260
1729050e
ID
8261 ret = false;
8262
0e8ffe1b
DV
8263 tmp = I915_READ(PIPECONF(crtc->pipe));
8264 if (!(tmp & PIPECONF_ENABLE))
1729050e 8265 goto out;
0e8ffe1b 8266
666a4537 8267 if (IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
42571aef
VS
8268 switch (tmp & PIPECONF_BPC_MASK) {
8269 case PIPECONF_6BPC:
8270 pipe_config->pipe_bpp = 18;
8271 break;
8272 case PIPECONF_8BPC:
8273 pipe_config->pipe_bpp = 24;
8274 break;
8275 case PIPECONF_10BPC:
8276 pipe_config->pipe_bpp = 30;
8277 break;
8278 default:
8279 break;
8280 }
8281 }
8282
666a4537
WB
8283 if ((IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) &&
8284 (tmp & PIPECONF_COLOR_RANGE_SELECT))
b5a9fa09
DV
8285 pipe_config->limited_color_range = true;
8286
282740f7
VS
8287 if (INTEL_INFO(dev)->gen < 4)
8288 pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
8289
1bd1bd80 8290 intel_get_pipe_timings(crtc, pipe_config);
bc58be60 8291 intel_get_pipe_src_size(crtc, pipe_config);
1bd1bd80 8292
2fa2fe9a
DV
8293 i9xx_get_pfit_config(crtc, pipe_config);
8294
6c49f241 8295 if (INTEL_INFO(dev)->gen >= 4) {
c231775c
VS
8296 /* No way to read it out on pipes B and C */
8297 if (IS_CHERRYVIEW(dev) && crtc->pipe != PIPE_A)
8298 tmp = dev_priv->chv_dpll_md[crtc->pipe];
8299 else
8300 tmp = I915_READ(DPLL_MD(crtc->pipe));
6c49f241
DV
8301 pipe_config->pixel_multiplier =
8302 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
8303 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
8bcc2795 8304 pipe_config->dpll_hw_state.dpll_md = tmp;
6c49f241
DV
8305 } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
8306 tmp = I915_READ(DPLL(crtc->pipe));
8307 pipe_config->pixel_multiplier =
8308 ((tmp & SDVO_MULTIPLIER_MASK)
8309 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
8310 } else {
8311 /* Note that on i915G/GM the pixel multiplier is in the sdvo
8312 * port and will be fixed up in the encoder->get_config
8313 * function. */
8314 pipe_config->pixel_multiplier = 1;
8315 }
8bcc2795 8316 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
666a4537 8317 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
1c4e0274
VS
8318 /*
8319 * DPLL_DVO_2X_MODE must be enabled for both DPLLs
8320 * on 830. Filter it out here so that we don't
8321 * report errors due to that.
8322 */
8323 if (IS_I830(dev))
8324 pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
8325
8bcc2795
DV
8326 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
8327 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
165e901c
VS
8328 } else {
8329 /* Mask out read-only status bits. */
8330 pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
8331 DPLL_PORTC_READY_MASK |
8332 DPLL_PORTB_READY_MASK);
8bcc2795 8333 }
6c49f241 8334
70b23a98
VS
8335 if (IS_CHERRYVIEW(dev))
8336 chv_crtc_clock_get(crtc, pipe_config);
8337 else if (IS_VALLEYVIEW(dev))
acbec814
JB
8338 vlv_crtc_clock_get(crtc, pipe_config);
8339 else
8340 i9xx_crtc_clock_get(crtc, pipe_config);
18442d08 8341
0f64614d
VS
8342 /*
8343 * Normally the dotclock is filled in by the encoder .get_config()
8344 * but in case the pipe is enabled w/o any ports we need a sane
8345 * default.
8346 */
8347 pipe_config->base.adjusted_mode.crtc_clock =
8348 pipe_config->port_clock / pipe_config->pixel_multiplier;
8349
1729050e
ID
8350 ret = true;
8351
8352out:
8353 intel_display_power_put(dev_priv, power_domain);
8354
8355 return ret;
0e8ffe1b
DV
8356}
8357
dde86e2d 8358static void ironlake_init_pch_refclk(struct drm_device *dev)
13d83a67
JB
8359{
8360 struct drm_i915_private *dev_priv = dev->dev_private;
13d83a67 8361 struct intel_encoder *encoder;
f165d283 8362 int i;
74cfd7ac 8363 u32 val, final;
13d83a67 8364 bool has_lvds = false;
199e5d79 8365 bool has_cpu_edp = false;
199e5d79 8366 bool has_panel = false;
99eb6a01
KP
8367 bool has_ck505 = false;
8368 bool can_ssc = false;
f165d283 8369 bool using_ssc_source = false;
13d83a67
JB
8370
8371 /* We need to take the global config into account */
b2784e15 8372 for_each_intel_encoder(dev, encoder) {
199e5d79
KP
8373 switch (encoder->type) {
8374 case INTEL_OUTPUT_LVDS:
8375 has_panel = true;
8376 has_lvds = true;
8377 break;
8378 case INTEL_OUTPUT_EDP:
8379 has_panel = true;
2de6905f 8380 if (enc_to_dig_port(&encoder->base)->port == PORT_A)
199e5d79
KP
8381 has_cpu_edp = true;
8382 break;
6847d71b
PZ
8383 default:
8384 break;
13d83a67
JB
8385 }
8386 }
8387
99eb6a01 8388 if (HAS_PCH_IBX(dev)) {
41aa3448 8389 has_ck505 = dev_priv->vbt.display_clock_mode;
99eb6a01
KP
8390 can_ssc = has_ck505;
8391 } else {
8392 has_ck505 = false;
8393 can_ssc = true;
8394 }
8395
f165d283
L
8396 /* Check if any DPLLs are using the SSC source */
8397 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
8398 u32 temp = I915_READ(PCH_DPLL(i));
8399
8400 if (!(temp & DPLL_VCO_ENABLE))
8401 continue;
8402
8403 if ((temp & PLL_REF_INPUT_MASK) ==
8404 PLLB_REF_INPUT_SPREADSPECTRUMIN) {
8405 using_ssc_source = true;
8406 break;
8407 }
8408 }
8409
8410 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d using_ssc_source %d\n",
8411 has_panel, has_lvds, has_ck505, using_ssc_source);
13d83a67
JB
8412
8413 /* Ironlake: try to setup display ref clock before DPLL
8414 * enabling. This is only under driver's control after
8415 * PCH B stepping, previous chipset stepping should be
8416 * ignoring this setting.
8417 */
74cfd7ac
CW
8418 val = I915_READ(PCH_DREF_CONTROL);
8419
8420 /* As we must carefully and slowly disable/enable each source in turn,
8421 * compute the final state we want first and check if we need to
8422 * make any changes at all.
8423 */
8424 final = val;
8425 final &= ~DREF_NONSPREAD_SOURCE_MASK;
8426 if (has_ck505)
8427 final |= DREF_NONSPREAD_CK505_ENABLE;
8428 else
8429 final |= DREF_NONSPREAD_SOURCE_ENABLE;
8430
74cfd7ac 8431 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
f165d283
L
8432
8433 if (!using_ssc_source) {
8434 final &= ~DREF_SSC_SOURCE_MASK;
8435 final &= ~DREF_SSC1_ENABLE;
8436 }
74cfd7ac
CW
8437
8438 if (has_panel) {
8439 final |= DREF_SSC_SOURCE_ENABLE;
8440
8441 if (intel_panel_use_ssc(dev_priv) && can_ssc)
8442 final |= DREF_SSC1_ENABLE;
8443
8444 if (has_cpu_edp) {
8445 if (intel_panel_use_ssc(dev_priv) && can_ssc)
8446 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
8447 else
8448 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
8449 } else
8450 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
8451 } else {
8452 final |= DREF_SSC_SOURCE_DISABLE;
8453 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
8454 }
8455
8456 if (final == val)
8457 return;
8458
13d83a67 8459 /* Always enable nonspread source */
74cfd7ac 8460 val &= ~DREF_NONSPREAD_SOURCE_MASK;
13d83a67 8461
99eb6a01 8462 if (has_ck505)
74cfd7ac 8463 val |= DREF_NONSPREAD_CK505_ENABLE;
99eb6a01 8464 else
74cfd7ac 8465 val |= DREF_NONSPREAD_SOURCE_ENABLE;
13d83a67 8466
199e5d79 8467 if (has_panel) {
74cfd7ac
CW
8468 val &= ~DREF_SSC_SOURCE_MASK;
8469 val |= DREF_SSC_SOURCE_ENABLE;
13d83a67 8470
199e5d79 8471 /* SSC must be turned on before enabling the CPU output */
99eb6a01 8472 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
199e5d79 8473 DRM_DEBUG_KMS("Using SSC on panel\n");
74cfd7ac 8474 val |= DREF_SSC1_ENABLE;
e77166b5 8475 } else
74cfd7ac 8476 val &= ~DREF_SSC1_ENABLE;
199e5d79
KP
8477
8478 /* Get SSC going before enabling the outputs */
74cfd7ac 8479 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
8480 POSTING_READ(PCH_DREF_CONTROL);
8481 udelay(200);
8482
74cfd7ac 8483 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
13d83a67
JB
8484
8485 /* Enable CPU source on CPU attached eDP */
199e5d79 8486 if (has_cpu_edp) {
99eb6a01 8487 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
199e5d79 8488 DRM_DEBUG_KMS("Using SSC on eDP\n");
74cfd7ac 8489 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
eba905b2 8490 } else
74cfd7ac 8491 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
199e5d79 8492 } else
74cfd7ac 8493 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
199e5d79 8494
74cfd7ac 8495 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
8496 POSTING_READ(PCH_DREF_CONTROL);
8497 udelay(200);
8498 } else {
f165d283 8499 DRM_DEBUG_KMS("Disabling CPU source output\n");
199e5d79 8500
74cfd7ac 8501 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
199e5d79
KP
8502
8503 /* Turn off CPU output */
74cfd7ac 8504 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
199e5d79 8505
74cfd7ac 8506 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
8507 POSTING_READ(PCH_DREF_CONTROL);
8508 udelay(200);
8509
f165d283
L
8510 if (!using_ssc_source) {
8511 DRM_DEBUG_KMS("Disabling SSC source\n");
199e5d79 8512
f165d283
L
8513 /* Turn off the SSC source */
8514 val &= ~DREF_SSC_SOURCE_MASK;
8515 val |= DREF_SSC_SOURCE_DISABLE;
199e5d79 8516
f165d283
L
8517 /* Turn off SSC1 */
8518 val &= ~DREF_SSC1_ENABLE;
8519
8520 I915_WRITE(PCH_DREF_CONTROL, val);
8521 POSTING_READ(PCH_DREF_CONTROL);
8522 udelay(200);
8523 }
13d83a67 8524 }
74cfd7ac
CW
8525
8526 BUG_ON(val != final);
13d83a67
JB
8527}
8528
f31f2d55 8529static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
dde86e2d 8530{
f31f2d55 8531 uint32_t tmp;
dde86e2d 8532
0ff066a9
PZ
8533 tmp = I915_READ(SOUTH_CHICKEN2);
8534 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
8535 I915_WRITE(SOUTH_CHICKEN2, tmp);
dde86e2d 8536
0ff066a9
PZ
8537 if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
8538 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
8539 DRM_ERROR("FDI mPHY reset assert timeout\n");
dde86e2d 8540
0ff066a9
PZ
8541 tmp = I915_READ(SOUTH_CHICKEN2);
8542 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
8543 I915_WRITE(SOUTH_CHICKEN2, tmp);
dde86e2d 8544
0ff066a9
PZ
8545 if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
8546 FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
8547 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
f31f2d55
PZ
8548}
8549
8550/* WaMPhyProgramming:hsw */
8551static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
8552{
8553 uint32_t tmp;
dde86e2d
PZ
8554
8555 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
8556 tmp &= ~(0xFF << 24);
8557 tmp |= (0x12 << 24);
8558 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
8559
dde86e2d
PZ
8560 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
8561 tmp |= (1 << 11);
8562 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
8563
8564 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
8565 tmp |= (1 << 11);
8566 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
8567
dde86e2d
PZ
8568 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
8569 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
8570 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
8571
8572 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
8573 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
8574 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
8575
0ff066a9
PZ
8576 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
8577 tmp &= ~(7 << 13);
8578 tmp |= (5 << 13);
8579 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
dde86e2d 8580
0ff066a9
PZ
8581 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
8582 tmp &= ~(7 << 13);
8583 tmp |= (5 << 13);
8584 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
dde86e2d
PZ
8585
8586 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
8587 tmp &= ~0xFF;
8588 tmp |= 0x1C;
8589 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
8590
8591 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
8592 tmp &= ~0xFF;
8593 tmp |= 0x1C;
8594 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
8595
8596 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
8597 tmp &= ~(0xFF << 16);
8598 tmp |= (0x1C << 16);
8599 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
8600
8601 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
8602 tmp &= ~(0xFF << 16);
8603 tmp |= (0x1C << 16);
8604 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
8605
0ff066a9
PZ
8606 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
8607 tmp |= (1 << 27);
8608 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
dde86e2d 8609
0ff066a9
PZ
8610 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
8611 tmp |= (1 << 27);
8612 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
dde86e2d 8613
0ff066a9
PZ
8614 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
8615 tmp &= ~(0xF << 28);
8616 tmp |= (4 << 28);
8617 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
dde86e2d 8618
0ff066a9
PZ
8619 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
8620 tmp &= ~(0xF << 28);
8621 tmp |= (4 << 28);
8622 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
f31f2d55
PZ
8623}
8624
2fa86a1f
PZ
8625/* Implements 3 different sequences from BSpec chapter "Display iCLK
8626 * Programming" based on the parameters passed:
8627 * - Sequence to enable CLKOUT_DP
8628 * - Sequence to enable CLKOUT_DP without spread
8629 * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
8630 */
8631static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
8632 bool with_fdi)
f31f2d55
PZ
8633{
8634 struct drm_i915_private *dev_priv = dev->dev_private;
2fa86a1f
PZ
8635 uint32_t reg, tmp;
8636
8637 if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
8638 with_spread = true;
c2699524 8639 if (WARN(HAS_PCH_LPT_LP(dev) && with_fdi, "LP PCH doesn't have FDI\n"))
2fa86a1f 8640 with_fdi = false;
f31f2d55 8641
a580516d 8642 mutex_lock(&dev_priv->sb_lock);
f31f2d55
PZ
8643
8644 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8645 tmp &= ~SBI_SSCCTL_DISABLE;
8646 tmp |= SBI_SSCCTL_PATHALT;
8647 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8648
8649 udelay(24);
8650
2fa86a1f
PZ
8651 if (with_spread) {
8652 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8653 tmp &= ~SBI_SSCCTL_PATHALT;
8654 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
f31f2d55 8655
2fa86a1f
PZ
8656 if (with_fdi) {
8657 lpt_reset_fdi_mphy(dev_priv);
8658 lpt_program_fdi_mphy(dev_priv);
8659 }
8660 }
dde86e2d 8661
c2699524 8662 reg = HAS_PCH_LPT_LP(dev) ? SBI_GEN0 : SBI_DBUFF0;
2fa86a1f
PZ
8663 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8664 tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8665 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
c00db246 8666
a580516d 8667 mutex_unlock(&dev_priv->sb_lock);
dde86e2d
PZ
8668}
8669
47701c3b
PZ
8670/* Sequence to disable CLKOUT_DP */
8671static void lpt_disable_clkout_dp(struct drm_device *dev)
8672{
8673 struct drm_i915_private *dev_priv = dev->dev_private;
8674 uint32_t reg, tmp;
8675
a580516d 8676 mutex_lock(&dev_priv->sb_lock);
47701c3b 8677
c2699524 8678 reg = HAS_PCH_LPT_LP(dev) ? SBI_GEN0 : SBI_DBUFF0;
47701c3b
PZ
8679 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8680 tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8681 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
8682
8683 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8684 if (!(tmp & SBI_SSCCTL_DISABLE)) {
8685 if (!(tmp & SBI_SSCCTL_PATHALT)) {
8686 tmp |= SBI_SSCCTL_PATHALT;
8687 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8688 udelay(32);
8689 }
8690 tmp |= SBI_SSCCTL_DISABLE;
8691 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8692 }
8693
a580516d 8694 mutex_unlock(&dev_priv->sb_lock);
47701c3b
PZ
8695}
8696
f7be2c21
VS
8697#define BEND_IDX(steps) ((50 + (steps)) / 5)
8698
8699static const uint16_t sscdivintphase[] = {
8700 [BEND_IDX( 50)] = 0x3B23,
8701 [BEND_IDX( 45)] = 0x3B23,
8702 [BEND_IDX( 40)] = 0x3C23,
8703 [BEND_IDX( 35)] = 0x3C23,
8704 [BEND_IDX( 30)] = 0x3D23,
8705 [BEND_IDX( 25)] = 0x3D23,
8706 [BEND_IDX( 20)] = 0x3E23,
8707 [BEND_IDX( 15)] = 0x3E23,
8708 [BEND_IDX( 10)] = 0x3F23,
8709 [BEND_IDX( 5)] = 0x3F23,
8710 [BEND_IDX( 0)] = 0x0025,
8711 [BEND_IDX( -5)] = 0x0025,
8712 [BEND_IDX(-10)] = 0x0125,
8713 [BEND_IDX(-15)] = 0x0125,
8714 [BEND_IDX(-20)] = 0x0225,
8715 [BEND_IDX(-25)] = 0x0225,
8716 [BEND_IDX(-30)] = 0x0325,
8717 [BEND_IDX(-35)] = 0x0325,
8718 [BEND_IDX(-40)] = 0x0425,
8719 [BEND_IDX(-45)] = 0x0425,
8720 [BEND_IDX(-50)] = 0x0525,
8721};
8722
8723/*
8724 * Bend CLKOUT_DP
8725 * steps -50 to 50 inclusive, in steps of 5
8726 * < 0 slow down the clock, > 0 speed up the clock, 0 == no bend (135MHz)
8727 * change in clock period = -(steps / 10) * 5.787 ps
8728 */
8729static void lpt_bend_clkout_dp(struct drm_i915_private *dev_priv, int steps)
8730{
8731 uint32_t tmp;
8732 int idx = BEND_IDX(steps);
8733
8734 if (WARN_ON(steps % 5 != 0))
8735 return;
8736
8737 if (WARN_ON(idx >= ARRAY_SIZE(sscdivintphase)))
8738 return;
8739
8740 mutex_lock(&dev_priv->sb_lock);
8741
8742 if (steps % 10 != 0)
8743 tmp = 0xAAAAAAAB;
8744 else
8745 tmp = 0x00000000;
8746 intel_sbi_write(dev_priv, SBI_SSCDITHPHASE, tmp, SBI_ICLK);
8747
8748 tmp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE, SBI_ICLK);
8749 tmp &= 0xffff0000;
8750 tmp |= sscdivintphase[idx];
8751 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE, tmp, SBI_ICLK);
8752
8753 mutex_unlock(&dev_priv->sb_lock);
8754}
8755
8756#undef BEND_IDX
8757
bf8fa3d3
PZ
8758static void lpt_init_pch_refclk(struct drm_device *dev)
8759{
bf8fa3d3
PZ
8760 struct intel_encoder *encoder;
8761 bool has_vga = false;
8762
b2784e15 8763 for_each_intel_encoder(dev, encoder) {
bf8fa3d3
PZ
8764 switch (encoder->type) {
8765 case INTEL_OUTPUT_ANALOG:
8766 has_vga = true;
8767 break;
6847d71b
PZ
8768 default:
8769 break;
bf8fa3d3
PZ
8770 }
8771 }
8772
f7be2c21
VS
8773 if (has_vga) {
8774 lpt_bend_clkout_dp(to_i915(dev), 0);
47701c3b 8775 lpt_enable_clkout_dp(dev, true, true);
f7be2c21 8776 } else {
47701c3b 8777 lpt_disable_clkout_dp(dev);
f7be2c21 8778 }
bf8fa3d3
PZ
8779}
8780
dde86e2d
PZ
8781/*
8782 * Initialize reference clocks when the driver loads
8783 */
8784void intel_init_pch_refclk(struct drm_device *dev)
8785{
8786 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
8787 ironlake_init_pch_refclk(dev);
8788 else if (HAS_PCH_LPT(dev))
8789 lpt_init_pch_refclk(dev);
8790}
8791
6ff93609 8792static void ironlake_set_pipeconf(struct drm_crtc *crtc)
79e53945 8793{
c8203565 8794 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
79e53945
JB
8795 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8796 int pipe = intel_crtc->pipe;
c8203565
PZ
8797 uint32_t val;
8798
78114071 8799 val = 0;
c8203565 8800
6e3c9717 8801 switch (intel_crtc->config->pipe_bpp) {
c8203565 8802 case 18:
dfd07d72 8803 val |= PIPECONF_6BPC;
c8203565
PZ
8804 break;
8805 case 24:
dfd07d72 8806 val |= PIPECONF_8BPC;
c8203565
PZ
8807 break;
8808 case 30:
dfd07d72 8809 val |= PIPECONF_10BPC;
c8203565
PZ
8810 break;
8811 case 36:
dfd07d72 8812 val |= PIPECONF_12BPC;
c8203565
PZ
8813 break;
8814 default:
cc769b62
PZ
8815 /* Case prevented by intel_choose_pipe_bpp_dither. */
8816 BUG();
c8203565
PZ
8817 }
8818
6e3c9717 8819 if (intel_crtc->config->dither)
c8203565
PZ
8820 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8821
6e3c9717 8822 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
c8203565
PZ
8823 val |= PIPECONF_INTERLACED_ILK;
8824 else
8825 val |= PIPECONF_PROGRESSIVE;
8826
6e3c9717 8827 if (intel_crtc->config->limited_color_range)
3685a8f3 8828 val |= PIPECONF_COLOR_RANGE_SELECT;
3685a8f3 8829
c8203565
PZ
8830 I915_WRITE(PIPECONF(pipe), val);
8831 POSTING_READ(PIPECONF(pipe));
8832}
8833
6ff93609 8834static void haswell_set_pipeconf(struct drm_crtc *crtc)
ee2b0b38 8835{
391bf048 8836 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
ee2b0b38 8837 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6e3c9717 8838 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
391bf048 8839 u32 val = 0;
ee2b0b38 8840
391bf048 8841 if (IS_HASWELL(dev_priv) && intel_crtc->config->dither)
ee2b0b38
PZ
8842 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8843
6e3c9717 8844 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
ee2b0b38
PZ
8845 val |= PIPECONF_INTERLACED_ILK;
8846 else
8847 val |= PIPECONF_PROGRESSIVE;
8848
702e7a56
PZ
8849 I915_WRITE(PIPECONF(cpu_transcoder), val);
8850 POSTING_READ(PIPECONF(cpu_transcoder));
391bf048
JN
8851}
8852
391bf048
JN
8853static void haswell_set_pipemisc(struct drm_crtc *crtc)
8854{
8855 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
8856 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
756f85cf 8857
391bf048
JN
8858 if (IS_BROADWELL(dev_priv) || INTEL_INFO(dev_priv)->gen >= 9) {
8859 u32 val = 0;
756f85cf 8860
6e3c9717 8861 switch (intel_crtc->config->pipe_bpp) {
756f85cf
PZ
8862 case 18:
8863 val |= PIPEMISC_DITHER_6_BPC;
8864 break;
8865 case 24:
8866 val |= PIPEMISC_DITHER_8_BPC;
8867 break;
8868 case 30:
8869 val |= PIPEMISC_DITHER_10_BPC;
8870 break;
8871 case 36:
8872 val |= PIPEMISC_DITHER_12_BPC;
8873 break;
8874 default:
8875 /* Case prevented by pipe_config_set_bpp. */
8876 BUG();
8877 }
8878
6e3c9717 8879 if (intel_crtc->config->dither)
756f85cf
PZ
8880 val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
8881
391bf048 8882 I915_WRITE(PIPEMISC(intel_crtc->pipe), val);
756f85cf 8883 }
ee2b0b38
PZ
8884}
8885
d4b1931c
PZ
8886int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
8887{
8888 /*
8889 * Account for spread spectrum to avoid
8890 * oversubscribing the link. Max center spread
8891 * is 2.5%; use 5% for safety's sake.
8892 */
8893 u32 bps = target_clock * bpp * 21 / 20;
619d4d04 8894 return DIV_ROUND_UP(bps, link_bw * 8);
d4b1931c
PZ
8895}
8896
7429e9d4 8897static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
6cf86a5e 8898{
7429e9d4 8899 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
f48d8f23
PZ
8900}
8901
b75ca6f6
ACO
8902static void ironlake_compute_dpll(struct intel_crtc *intel_crtc,
8903 struct intel_crtc_state *crtc_state,
9e2c8475 8904 struct dpll *reduced_clock)
79e53945 8905{
de13a2e3 8906 struct drm_crtc *crtc = &intel_crtc->base;
79e53945
JB
8907 struct drm_device *dev = crtc->dev;
8908 struct drm_i915_private *dev_priv = dev->dev_private;
55bb9992 8909 struct drm_atomic_state *state = crtc_state->base.state;
da3ced29 8910 struct drm_connector *connector;
55bb9992
ACO
8911 struct drm_connector_state *connector_state;
8912 struct intel_encoder *encoder;
b75ca6f6 8913 u32 dpll, fp, fp2;
ceb41007 8914 int factor, i;
09ede541 8915 bool is_lvds = false, is_sdvo = false;
79e53945 8916
da3ced29 8917 for_each_connector_in_state(state, connector, connector_state, i) {
55bb9992
ACO
8918 if (connector_state->crtc != crtc_state->base.crtc)
8919 continue;
8920
8921 encoder = to_intel_encoder(connector_state->best_encoder);
8922
8923 switch (encoder->type) {
79e53945
JB
8924 case INTEL_OUTPUT_LVDS:
8925 is_lvds = true;
8926 break;
8927 case INTEL_OUTPUT_SDVO:
7d57382e 8928 case INTEL_OUTPUT_HDMI:
79e53945 8929 is_sdvo = true;
79e53945 8930 break;
6847d71b
PZ
8931 default:
8932 break;
79e53945
JB
8933 }
8934 }
79e53945 8935
c1858123 8936 /* Enable autotuning of the PLL clock (if permissible) */
8febb297
EA
8937 factor = 21;
8938 if (is_lvds) {
8939 if ((intel_panel_use_ssc(dev_priv) &&
e91e941b 8940 dev_priv->vbt.lvds_ssc_freq == 100000) ||
f0b44056 8941 (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
8febb297 8942 factor = 25;
190f68c5 8943 } else if (crtc_state->sdvo_tv_clock)
8febb297 8944 factor = 20;
c1858123 8945
b75ca6f6
ACO
8946 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
8947
190f68c5 8948 if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
b75ca6f6
ACO
8949 fp |= FP_CB_TUNE;
8950
8951 if (reduced_clock) {
8952 fp2 = i9xx_dpll_compute_fp(reduced_clock);
2c07245f 8953
b75ca6f6
ACO
8954 if (reduced_clock->m < factor * reduced_clock->n)
8955 fp2 |= FP_CB_TUNE;
8956 } else {
8957 fp2 = fp;
8958 }
9a7c7890 8959
5eddb70b 8960 dpll = 0;
2c07245f 8961
a07d6787
EA
8962 if (is_lvds)
8963 dpll |= DPLLB_MODE_LVDS;
8964 else
8965 dpll |= DPLLB_MODE_DAC_SERIAL;
198a037f 8966
190f68c5 8967 dpll |= (crtc_state->pixel_multiplier - 1)
ef1b460d 8968 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
198a037f
DV
8969
8970 if (is_sdvo)
4a33e48d 8971 dpll |= DPLL_SDVO_HIGH_SPEED;
190f68c5 8972 if (crtc_state->has_dp_encoder)
4a33e48d 8973 dpll |= DPLL_SDVO_HIGH_SPEED;
79e53945 8974
a07d6787 8975 /* compute bitmask from p1 value */
190f68c5 8976 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
a07d6787 8977 /* also FPA1 */
190f68c5 8978 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
a07d6787 8979
190f68c5 8980 switch (crtc_state->dpll.p2) {
a07d6787
EA
8981 case 5:
8982 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
8983 break;
8984 case 7:
8985 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
8986 break;
8987 case 10:
8988 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
8989 break;
8990 case 14:
8991 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
8992 break;
79e53945
JB
8993 }
8994
ceb41007 8995 if (is_lvds && intel_panel_use_ssc(dev_priv))
43565a06 8996 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
79e53945
JB
8997 else
8998 dpll |= PLL_REF_INPUT_DREFCLK;
8999
b75ca6f6
ACO
9000 dpll |= DPLL_VCO_ENABLE;
9001
9002 crtc_state->dpll_hw_state.dpll = dpll;
9003 crtc_state->dpll_hw_state.fp0 = fp;
9004 crtc_state->dpll_hw_state.fp1 = fp2;
de13a2e3
PZ
9005}
9006
190f68c5
ACO
9007static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
9008 struct intel_crtc_state *crtc_state)
de13a2e3 9009{
997c030c
ACO
9010 struct drm_device *dev = crtc->base.dev;
9011 struct drm_i915_private *dev_priv = dev->dev_private;
9e2c8475 9012 struct dpll reduced_clock;
7ed9f894 9013 bool has_reduced_clock = false;
e2b78267 9014 struct intel_shared_dpll *pll;
1b6f4958 9015 const struct intel_limit *limit;
997c030c 9016 int refclk = 120000;
de13a2e3 9017
dd3cd74a
ACO
9018 memset(&crtc_state->dpll_hw_state, 0,
9019 sizeof(crtc_state->dpll_hw_state));
9020
ded220e2
ACO
9021 crtc->lowfreq_avail = false;
9022
9023 /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
9024 if (!crtc_state->has_pch_encoder)
9025 return 0;
79e53945 9026
997c030c
ACO
9027 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
9028 if (intel_panel_use_ssc(dev_priv)) {
9029 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
9030 dev_priv->vbt.lvds_ssc_freq);
9031 refclk = dev_priv->vbt.lvds_ssc_freq;
9032 }
9033
9034 if (intel_is_dual_link_lvds(dev)) {
9035 if (refclk == 100000)
9036 limit = &intel_limits_ironlake_dual_lvds_100m;
9037 else
9038 limit = &intel_limits_ironlake_dual_lvds;
9039 } else {
9040 if (refclk == 100000)
9041 limit = &intel_limits_ironlake_single_lvds_100m;
9042 else
9043 limit = &intel_limits_ironlake_single_lvds;
9044 }
9045 } else {
9046 limit = &intel_limits_ironlake_dac;
9047 }
9048
364ee29d 9049 if (!crtc_state->clock_set &&
997c030c
ACO
9050 !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
9051 refclk, NULL, &crtc_state->dpll)) {
364ee29d
ACO
9052 DRM_ERROR("Couldn't find PLL settings for mode!\n");
9053 return -EINVAL;
f47709a9 9054 }
79e53945 9055
b75ca6f6
ACO
9056 ironlake_compute_dpll(crtc, crtc_state,
9057 has_reduced_clock ? &reduced_clock : NULL);
66e985c0 9058
ded220e2
ACO
9059 pll = intel_get_shared_dpll(crtc, crtc_state, NULL);
9060 if (pll == NULL) {
9061 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
9062 pipe_name(crtc->pipe));
9063 return -EINVAL;
3fb37703 9064 }
79e53945 9065
ded220e2
ACO
9066 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
9067 has_reduced_clock)
c7653199 9068 crtc->lowfreq_avail = true;
e2b78267 9069
c8f7a0db 9070 return 0;
79e53945
JB
9071}
9072
eb14cb74
VS
9073static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
9074 struct intel_link_m_n *m_n)
9075{
9076 struct drm_device *dev = crtc->base.dev;
9077 struct drm_i915_private *dev_priv = dev->dev_private;
9078 enum pipe pipe = crtc->pipe;
9079
9080 m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
9081 m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
9082 m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
9083 & ~TU_SIZE_MASK;
9084 m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
9085 m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
9086 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
9087}
9088
9089static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
9090 enum transcoder transcoder,
b95af8be
VK
9091 struct intel_link_m_n *m_n,
9092 struct intel_link_m_n *m2_n2)
72419203
DV
9093{
9094 struct drm_device *dev = crtc->base.dev;
9095 struct drm_i915_private *dev_priv = dev->dev_private;
eb14cb74 9096 enum pipe pipe = crtc->pipe;
72419203 9097
eb14cb74
VS
9098 if (INTEL_INFO(dev)->gen >= 5) {
9099 m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
9100 m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
9101 m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
9102 & ~TU_SIZE_MASK;
9103 m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
9104 m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
9105 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
b95af8be
VK
9106 /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
9107 * gen < 8) and if DRRS is supported (to make sure the
9108 * registers are not unnecessarily read).
9109 */
9110 if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
6e3c9717 9111 crtc->config->has_drrs) {
b95af8be
VK
9112 m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
9113 m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
9114 m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
9115 & ~TU_SIZE_MASK;
9116 m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
9117 m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
9118 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
9119 }
eb14cb74
VS
9120 } else {
9121 m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
9122 m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
9123 m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
9124 & ~TU_SIZE_MASK;
9125 m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
9126 m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
9127 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
9128 }
9129}
9130
9131void intel_dp_get_m_n(struct intel_crtc *crtc,
5cec258b 9132 struct intel_crtc_state *pipe_config)
eb14cb74 9133{
681a8504 9134 if (pipe_config->has_pch_encoder)
eb14cb74
VS
9135 intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
9136 else
9137 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
b95af8be
VK
9138 &pipe_config->dp_m_n,
9139 &pipe_config->dp_m2_n2);
eb14cb74 9140}
72419203 9141
eb14cb74 9142static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
5cec258b 9143 struct intel_crtc_state *pipe_config)
eb14cb74
VS
9144{
9145 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
b95af8be 9146 &pipe_config->fdi_m_n, NULL);
72419203
DV
9147}
9148
bd2e244f 9149static void skylake_get_pfit_config(struct intel_crtc *crtc,
5cec258b 9150 struct intel_crtc_state *pipe_config)
bd2e244f
JB
9151{
9152 struct drm_device *dev = crtc->base.dev;
9153 struct drm_i915_private *dev_priv = dev->dev_private;
a1b2278e
CK
9154 struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
9155 uint32_t ps_ctrl = 0;
9156 int id = -1;
9157 int i;
bd2e244f 9158
a1b2278e
CK
9159 /* find scaler attached to this pipe */
9160 for (i = 0; i < crtc->num_scalers; i++) {
9161 ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
9162 if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
9163 id = i;
9164 pipe_config->pch_pfit.enabled = true;
9165 pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
9166 pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
9167 break;
9168 }
9169 }
bd2e244f 9170
a1b2278e
CK
9171 scaler_state->scaler_id = id;
9172 if (id >= 0) {
9173 scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
9174 } else {
9175 scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
bd2e244f
JB
9176 }
9177}
9178
5724dbd1
DL
9179static void
9180skylake_get_initial_plane_config(struct intel_crtc *crtc,
9181 struct intel_initial_plane_config *plane_config)
bc8d7dff
DL
9182{
9183 struct drm_device *dev = crtc->base.dev;
9184 struct drm_i915_private *dev_priv = dev->dev_private;
40f46283 9185 u32 val, base, offset, stride_mult, tiling;
bc8d7dff
DL
9186 int pipe = crtc->pipe;
9187 int fourcc, pixel_format;
6761dd31 9188 unsigned int aligned_height;
bc8d7dff 9189 struct drm_framebuffer *fb;
1b842c89 9190 struct intel_framebuffer *intel_fb;
bc8d7dff 9191
d9806c9f 9192 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
1b842c89 9193 if (!intel_fb) {
bc8d7dff
DL
9194 DRM_DEBUG_KMS("failed to alloc fb\n");
9195 return;
9196 }
9197
1b842c89
DL
9198 fb = &intel_fb->base;
9199
bc8d7dff 9200 val = I915_READ(PLANE_CTL(pipe, 0));
42a7b088
DL
9201 if (!(val & PLANE_CTL_ENABLE))
9202 goto error;
9203
bc8d7dff
DL
9204 pixel_format = val & PLANE_CTL_FORMAT_MASK;
9205 fourcc = skl_format_to_fourcc(pixel_format,
9206 val & PLANE_CTL_ORDER_RGBX,
9207 val & PLANE_CTL_ALPHA_MASK);
9208 fb->pixel_format = fourcc;
9209 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
9210
40f46283
DL
9211 tiling = val & PLANE_CTL_TILED_MASK;
9212 switch (tiling) {
9213 case PLANE_CTL_TILED_LINEAR:
9214 fb->modifier[0] = DRM_FORMAT_MOD_NONE;
9215 break;
9216 case PLANE_CTL_TILED_X:
9217 plane_config->tiling = I915_TILING_X;
9218 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
9219 break;
9220 case PLANE_CTL_TILED_Y:
9221 fb->modifier[0] = I915_FORMAT_MOD_Y_TILED;
9222 break;
9223 case PLANE_CTL_TILED_YF:
9224 fb->modifier[0] = I915_FORMAT_MOD_Yf_TILED;
9225 break;
9226 default:
9227 MISSING_CASE(tiling);
9228 goto error;
9229 }
9230
bc8d7dff
DL
9231 base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
9232 plane_config->base = base;
9233
9234 offset = I915_READ(PLANE_OFFSET(pipe, 0));
9235
9236 val = I915_READ(PLANE_SIZE(pipe, 0));
9237 fb->height = ((val >> 16) & 0xfff) + 1;
9238 fb->width = ((val >> 0) & 0x1fff) + 1;
9239
9240 val = I915_READ(PLANE_STRIDE(pipe, 0));
7b49f948 9241 stride_mult = intel_fb_stride_alignment(dev_priv, fb->modifier[0],
40f46283 9242 fb->pixel_format);
bc8d7dff
DL
9243 fb->pitches[0] = (val & 0x3ff) * stride_mult;
9244
9245 aligned_height = intel_fb_align_height(dev, fb->height,
091df6cb
DV
9246 fb->pixel_format,
9247 fb->modifier[0]);
bc8d7dff 9248
f37b5c2b 9249 plane_config->size = fb->pitches[0] * aligned_height;
bc8d7dff
DL
9250
9251 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9252 pipe_name(pipe), fb->width, fb->height,
9253 fb->bits_per_pixel, base, fb->pitches[0],
9254 plane_config->size);
9255
2d14030b 9256 plane_config->fb = intel_fb;
bc8d7dff
DL
9257 return;
9258
9259error:
9260 kfree(fb);
9261}
9262
2fa2fe9a 9263static void ironlake_get_pfit_config(struct intel_crtc *crtc,
5cec258b 9264 struct intel_crtc_state *pipe_config)
2fa2fe9a
DV
9265{
9266 struct drm_device *dev = crtc->base.dev;
9267 struct drm_i915_private *dev_priv = dev->dev_private;
9268 uint32_t tmp;
9269
9270 tmp = I915_READ(PF_CTL(crtc->pipe));
9271
9272 if (tmp & PF_ENABLE) {
fd4daa9c 9273 pipe_config->pch_pfit.enabled = true;
2fa2fe9a
DV
9274 pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
9275 pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
cb8b2a30
DV
9276
9277 /* We currently do not free assignements of panel fitters on
9278 * ivb/hsw (since we don't use the higher upscaling modes which
9279 * differentiates them) so just WARN about this case for now. */
9280 if (IS_GEN7(dev)) {
9281 WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
9282 PF_PIPE_SEL_IVB(crtc->pipe));
9283 }
2fa2fe9a 9284 }
79e53945
JB
9285}
9286
5724dbd1
DL
9287static void
9288ironlake_get_initial_plane_config(struct intel_crtc *crtc,
9289 struct intel_initial_plane_config *plane_config)
4c6baa59
JB
9290{
9291 struct drm_device *dev = crtc->base.dev;
9292 struct drm_i915_private *dev_priv = dev->dev_private;
9293 u32 val, base, offset;
aeee5a49 9294 int pipe = crtc->pipe;
4c6baa59 9295 int fourcc, pixel_format;
6761dd31 9296 unsigned int aligned_height;
b113d5ee 9297 struct drm_framebuffer *fb;
1b842c89 9298 struct intel_framebuffer *intel_fb;
4c6baa59 9299
42a7b088
DL
9300 val = I915_READ(DSPCNTR(pipe));
9301 if (!(val & DISPLAY_PLANE_ENABLE))
9302 return;
9303
d9806c9f 9304 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
1b842c89 9305 if (!intel_fb) {
4c6baa59
JB
9306 DRM_DEBUG_KMS("failed to alloc fb\n");
9307 return;
9308 }
9309
1b842c89
DL
9310 fb = &intel_fb->base;
9311
18c5247e
DV
9312 if (INTEL_INFO(dev)->gen >= 4) {
9313 if (val & DISPPLANE_TILED) {
49af449b 9314 plane_config->tiling = I915_TILING_X;
18c5247e
DV
9315 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
9316 }
9317 }
4c6baa59
JB
9318
9319 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
b35d63fa 9320 fourcc = i9xx_format_to_fourcc(pixel_format);
b113d5ee
DL
9321 fb->pixel_format = fourcc;
9322 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
4c6baa59 9323
aeee5a49 9324 base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
4c6baa59 9325 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
aeee5a49 9326 offset = I915_READ(DSPOFFSET(pipe));
4c6baa59 9327 } else {
49af449b 9328 if (plane_config->tiling)
aeee5a49 9329 offset = I915_READ(DSPTILEOFF(pipe));
4c6baa59 9330 else
aeee5a49 9331 offset = I915_READ(DSPLINOFF(pipe));
4c6baa59
JB
9332 }
9333 plane_config->base = base;
9334
9335 val = I915_READ(PIPESRC(pipe));
b113d5ee
DL
9336 fb->width = ((val >> 16) & 0xfff) + 1;
9337 fb->height = ((val >> 0) & 0xfff) + 1;
4c6baa59
JB
9338
9339 val = I915_READ(DSPSTRIDE(pipe));
b113d5ee 9340 fb->pitches[0] = val & 0xffffffc0;
4c6baa59 9341
b113d5ee 9342 aligned_height = intel_fb_align_height(dev, fb->height,
091df6cb
DV
9343 fb->pixel_format,
9344 fb->modifier[0]);
4c6baa59 9345
f37b5c2b 9346 plane_config->size = fb->pitches[0] * aligned_height;
4c6baa59 9347
2844a921
DL
9348 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9349 pipe_name(pipe), fb->width, fb->height,
9350 fb->bits_per_pixel, base, fb->pitches[0],
9351 plane_config->size);
b113d5ee 9352
2d14030b 9353 plane_config->fb = intel_fb;
4c6baa59
JB
9354}
9355
0e8ffe1b 9356static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
5cec258b 9357 struct intel_crtc_state *pipe_config)
0e8ffe1b
DV
9358{
9359 struct drm_device *dev = crtc->base.dev;
9360 struct drm_i915_private *dev_priv = dev->dev_private;
1729050e 9361 enum intel_display_power_domain power_domain;
0e8ffe1b 9362 uint32_t tmp;
1729050e 9363 bool ret;
0e8ffe1b 9364
1729050e
ID
9365 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
9366 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
930e8c9e
PZ
9367 return false;
9368
e143a21c 9369 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
8106ddbd 9370 pipe_config->shared_dpll = NULL;
eccb140b 9371
1729050e 9372 ret = false;
0e8ffe1b
DV
9373 tmp = I915_READ(PIPECONF(crtc->pipe));
9374 if (!(tmp & PIPECONF_ENABLE))
1729050e 9375 goto out;
0e8ffe1b 9376
42571aef
VS
9377 switch (tmp & PIPECONF_BPC_MASK) {
9378 case PIPECONF_6BPC:
9379 pipe_config->pipe_bpp = 18;
9380 break;
9381 case PIPECONF_8BPC:
9382 pipe_config->pipe_bpp = 24;
9383 break;
9384 case PIPECONF_10BPC:
9385 pipe_config->pipe_bpp = 30;
9386 break;
9387 case PIPECONF_12BPC:
9388 pipe_config->pipe_bpp = 36;
9389 break;
9390 default:
9391 break;
9392 }
9393
b5a9fa09
DV
9394 if (tmp & PIPECONF_COLOR_RANGE_SELECT)
9395 pipe_config->limited_color_range = true;
9396
ab9412ba 9397 if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
66e985c0 9398 struct intel_shared_dpll *pll;
8106ddbd 9399 enum intel_dpll_id pll_id;
66e985c0 9400
88adfff1
DV
9401 pipe_config->has_pch_encoder = true;
9402
627eb5a3
DV
9403 tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
9404 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
9405 FDI_DP_PORT_WIDTH_SHIFT) + 1;
72419203
DV
9406
9407 ironlake_get_fdi_m_n_config(crtc, pipe_config);
6c49f241 9408
2d1fe073 9409 if (HAS_PCH_IBX(dev_priv)) {
d9a7bc67
ID
9410 /*
9411 * The pipe->pch transcoder and pch transcoder->pll
9412 * mapping is fixed.
9413 */
8106ddbd 9414 pll_id = (enum intel_dpll_id) crtc->pipe;
c0d43d62
DV
9415 } else {
9416 tmp = I915_READ(PCH_DPLL_SEL);
9417 if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
8106ddbd 9418 pll_id = DPLL_ID_PCH_PLL_B;
c0d43d62 9419 else
8106ddbd 9420 pll_id= DPLL_ID_PCH_PLL_A;
c0d43d62 9421 }
66e985c0 9422
8106ddbd
ACO
9423 pipe_config->shared_dpll =
9424 intel_get_shared_dpll_by_id(dev_priv, pll_id);
9425 pll = pipe_config->shared_dpll;
66e985c0 9426
2edd6443
ACO
9427 WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
9428 &pipe_config->dpll_hw_state));
c93f54cf
DV
9429
9430 tmp = pipe_config->dpll_hw_state.dpll;
9431 pipe_config->pixel_multiplier =
9432 ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
9433 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
18442d08
VS
9434
9435 ironlake_pch_clock_get(crtc, pipe_config);
6c49f241
DV
9436 } else {
9437 pipe_config->pixel_multiplier = 1;
627eb5a3
DV
9438 }
9439
1bd1bd80 9440 intel_get_pipe_timings(crtc, pipe_config);
bc58be60 9441 intel_get_pipe_src_size(crtc, pipe_config);
1bd1bd80 9442
2fa2fe9a
DV
9443 ironlake_get_pfit_config(crtc, pipe_config);
9444
1729050e
ID
9445 ret = true;
9446
9447out:
9448 intel_display_power_put(dev_priv, power_domain);
9449
9450 return ret;
0e8ffe1b
DV
9451}
9452
be256dc7
PZ
9453static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
9454{
9455 struct drm_device *dev = dev_priv->dev;
be256dc7 9456 struct intel_crtc *crtc;
be256dc7 9457
d3fcc808 9458 for_each_intel_crtc(dev, crtc)
e2c719b7 9459 I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
be256dc7
PZ
9460 pipe_name(crtc->pipe));
9461
e2c719b7
RC
9462 I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
9463 I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
01403de3
VS
9464 I915_STATE_WARN(I915_READ(WRPLL_CTL(0)) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
9465 I915_STATE_WARN(I915_READ(WRPLL_CTL(1)) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
e2c719b7
RC
9466 I915_STATE_WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
9467 I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
be256dc7 9468 "CPU PWM1 enabled\n");
c5107b87 9469 if (IS_HASWELL(dev))
e2c719b7 9470 I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
c5107b87 9471 "CPU PWM2 enabled\n");
e2c719b7 9472 I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
be256dc7 9473 "PCH PWM1 enabled\n");
e2c719b7 9474 I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
be256dc7 9475 "Utility pin enabled\n");
e2c719b7 9476 I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
be256dc7 9477
9926ada1
PZ
9478 /*
9479 * In theory we can still leave IRQs enabled, as long as only the HPD
9480 * interrupts remain enabled. We used to check for that, but since it's
9481 * gen-specific and since we only disable LCPLL after we fully disable
9482 * the interrupts, the check below should be enough.
9483 */
e2c719b7 9484 I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
be256dc7
PZ
9485}
9486
9ccd5aeb
PZ
9487static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
9488{
9489 struct drm_device *dev = dev_priv->dev;
9490
9491 if (IS_HASWELL(dev))
9492 return I915_READ(D_COMP_HSW);
9493 else
9494 return I915_READ(D_COMP_BDW);
9495}
9496
3c4c9b81
PZ
9497static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
9498{
9499 struct drm_device *dev = dev_priv->dev;
9500
9501 if (IS_HASWELL(dev)) {
9502 mutex_lock(&dev_priv->rps.hw_lock);
9503 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
9504 val))
f475dadf 9505 DRM_ERROR("Failed to write to D_COMP\n");
3c4c9b81
PZ
9506 mutex_unlock(&dev_priv->rps.hw_lock);
9507 } else {
9ccd5aeb
PZ
9508 I915_WRITE(D_COMP_BDW, val);
9509 POSTING_READ(D_COMP_BDW);
3c4c9b81 9510 }
be256dc7
PZ
9511}
9512
9513/*
9514 * This function implements pieces of two sequences from BSpec:
9515 * - Sequence for display software to disable LCPLL
9516 * - Sequence for display software to allow package C8+
9517 * The steps implemented here are just the steps that actually touch the LCPLL
9518 * register. Callers should take care of disabling all the display engine
9519 * functions, doing the mode unset, fixing interrupts, etc.
9520 */
6ff58d53
PZ
9521static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
9522 bool switch_to_fclk, bool allow_power_down)
be256dc7
PZ
9523{
9524 uint32_t val;
9525
9526 assert_can_disable_lcpll(dev_priv);
9527
9528 val = I915_READ(LCPLL_CTL);
9529
9530 if (switch_to_fclk) {
9531 val |= LCPLL_CD_SOURCE_FCLK;
9532 I915_WRITE(LCPLL_CTL, val);
9533
9534 if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
9535 LCPLL_CD_SOURCE_FCLK_DONE, 1))
9536 DRM_ERROR("Switching to FCLK failed\n");
9537
9538 val = I915_READ(LCPLL_CTL);
9539 }
9540
9541 val |= LCPLL_PLL_DISABLE;
9542 I915_WRITE(LCPLL_CTL, val);
9543 POSTING_READ(LCPLL_CTL);
9544
9545 if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
9546 DRM_ERROR("LCPLL still locked\n");
9547
9ccd5aeb 9548 val = hsw_read_dcomp(dev_priv);
be256dc7 9549 val |= D_COMP_COMP_DISABLE;
3c4c9b81 9550 hsw_write_dcomp(dev_priv, val);
be256dc7
PZ
9551 ndelay(100);
9552
9ccd5aeb
PZ
9553 if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
9554 1))
be256dc7
PZ
9555 DRM_ERROR("D_COMP RCOMP still in progress\n");
9556
9557 if (allow_power_down) {
9558 val = I915_READ(LCPLL_CTL);
9559 val |= LCPLL_POWER_DOWN_ALLOW;
9560 I915_WRITE(LCPLL_CTL, val);
9561 POSTING_READ(LCPLL_CTL);
9562 }
9563}
9564
9565/*
9566 * Fully restores LCPLL, disallowing power down and switching back to LCPLL
9567 * source.
9568 */
6ff58d53 9569static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
be256dc7
PZ
9570{
9571 uint32_t val;
9572
9573 val = I915_READ(LCPLL_CTL);
9574
9575 if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
9576 LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
9577 return;
9578
a8a8bd54
PZ
9579 /*
9580 * Make sure we're not on PC8 state before disabling PC8, otherwise
9581 * we'll hang the machine. To prevent PC8 state, just enable force_wake.
a8a8bd54 9582 */
59bad947 9583 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
215733fa 9584
be256dc7
PZ
9585 if (val & LCPLL_POWER_DOWN_ALLOW) {
9586 val &= ~LCPLL_POWER_DOWN_ALLOW;
9587 I915_WRITE(LCPLL_CTL, val);
35d8f2eb 9588 POSTING_READ(LCPLL_CTL);
be256dc7
PZ
9589 }
9590
9ccd5aeb 9591 val = hsw_read_dcomp(dev_priv);
be256dc7
PZ
9592 val |= D_COMP_COMP_FORCE;
9593 val &= ~D_COMP_COMP_DISABLE;
3c4c9b81 9594 hsw_write_dcomp(dev_priv, val);
be256dc7
PZ
9595
9596 val = I915_READ(LCPLL_CTL);
9597 val &= ~LCPLL_PLL_DISABLE;
9598 I915_WRITE(LCPLL_CTL, val);
9599
9600 if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
9601 DRM_ERROR("LCPLL not locked yet\n");
9602
9603 if (val & LCPLL_CD_SOURCE_FCLK) {
9604 val = I915_READ(LCPLL_CTL);
9605 val &= ~LCPLL_CD_SOURCE_FCLK;
9606 I915_WRITE(LCPLL_CTL, val);
9607
9608 if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
9609 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
9610 DRM_ERROR("Switching back to LCPLL failed\n");
9611 }
215733fa 9612
59bad947 9613 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
b6283055 9614 intel_update_cdclk(dev_priv->dev);
be256dc7
PZ
9615}
9616
765dab67
PZ
9617/*
9618 * Package states C8 and deeper are really deep PC states that can only be
9619 * reached when all the devices on the system allow it, so even if the graphics
9620 * device allows PC8+, it doesn't mean the system will actually get to these
9621 * states. Our driver only allows PC8+ when going into runtime PM.
9622 *
9623 * The requirements for PC8+ are that all the outputs are disabled, the power
9624 * well is disabled and most interrupts are disabled, and these are also
9625 * requirements for runtime PM. When these conditions are met, we manually do
9626 * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
9627 * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
9628 * hang the machine.
9629 *
9630 * When we really reach PC8 or deeper states (not just when we allow it) we lose
9631 * the state of some registers, so when we come back from PC8+ we need to
9632 * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
9633 * need to take care of the registers kept by RC6. Notice that this happens even
9634 * if we don't put the device in PCI D3 state (which is what currently happens
9635 * because of the runtime PM support).
9636 *
9637 * For more, read "Display Sequences for Package C8" on the hardware
9638 * documentation.
9639 */
a14cb6fc 9640void hsw_enable_pc8(struct drm_i915_private *dev_priv)
c67a470b 9641{
c67a470b
PZ
9642 struct drm_device *dev = dev_priv->dev;
9643 uint32_t val;
9644
c67a470b
PZ
9645 DRM_DEBUG_KMS("Enabling package C8+\n");
9646
c2699524 9647 if (HAS_PCH_LPT_LP(dev)) {
c67a470b
PZ
9648 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9649 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
9650 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9651 }
9652
9653 lpt_disable_clkout_dp(dev);
c67a470b
PZ
9654 hsw_disable_lcpll(dev_priv, true, true);
9655}
9656
a14cb6fc 9657void hsw_disable_pc8(struct drm_i915_private *dev_priv)
c67a470b
PZ
9658{
9659 struct drm_device *dev = dev_priv->dev;
9660 uint32_t val;
9661
c67a470b
PZ
9662 DRM_DEBUG_KMS("Disabling package C8+\n");
9663
9664 hsw_restore_lcpll(dev_priv);
c67a470b
PZ
9665 lpt_init_pch_refclk(dev);
9666
c2699524 9667 if (HAS_PCH_LPT_LP(dev)) {
c67a470b
PZ
9668 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9669 val |= PCH_LP_PARTITION_LEVEL_DISABLE;
9670 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9671 }
c67a470b
PZ
9672}
9673
27c329ed 9674static void broxton_modeset_commit_cdclk(struct drm_atomic_state *old_state)
f8437dd1 9675{
a821fc46 9676 struct drm_device *dev = old_state->dev;
1a617b77
ML
9677 struct intel_atomic_state *old_intel_state =
9678 to_intel_atomic_state(old_state);
9679 unsigned int req_cdclk = old_intel_state->dev_cdclk;
f8437dd1 9680
c6c4696f 9681 broxton_set_cdclk(to_i915(dev), req_cdclk);
f8437dd1
VK
9682}
9683
b432e5cf 9684/* compute the max rate for new configuration */
27c329ed 9685static int ilk_max_pixel_rate(struct drm_atomic_state *state)
b432e5cf 9686{
565602d7
ML
9687 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
9688 struct drm_i915_private *dev_priv = state->dev->dev_private;
9689 struct drm_crtc *crtc;
9690 struct drm_crtc_state *cstate;
27c329ed 9691 struct intel_crtc_state *crtc_state;
565602d7
ML
9692 unsigned max_pixel_rate = 0, i;
9693 enum pipe pipe;
b432e5cf 9694
565602d7
ML
9695 memcpy(intel_state->min_pixclk, dev_priv->min_pixclk,
9696 sizeof(intel_state->min_pixclk));
27c329ed 9697
565602d7
ML
9698 for_each_crtc_in_state(state, crtc, cstate, i) {
9699 int pixel_rate;
27c329ed 9700
565602d7
ML
9701 crtc_state = to_intel_crtc_state(cstate);
9702 if (!crtc_state->base.enable) {
9703 intel_state->min_pixclk[i] = 0;
b432e5cf 9704 continue;
565602d7 9705 }
b432e5cf 9706
27c329ed 9707 pixel_rate = ilk_pipe_pixel_rate(crtc_state);
b432e5cf
VS
9708
9709 /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
565602d7 9710 if (IS_BROADWELL(dev_priv) && crtc_state->ips_enabled)
b432e5cf
VS
9711 pixel_rate = DIV_ROUND_UP(pixel_rate * 100, 95);
9712
565602d7 9713 intel_state->min_pixclk[i] = pixel_rate;
b432e5cf
VS
9714 }
9715
565602d7
ML
9716 for_each_pipe(dev_priv, pipe)
9717 max_pixel_rate = max(intel_state->min_pixclk[pipe], max_pixel_rate);
9718
b432e5cf
VS
9719 return max_pixel_rate;
9720}
9721
9722static void broadwell_set_cdclk(struct drm_device *dev, int cdclk)
9723{
9724 struct drm_i915_private *dev_priv = dev->dev_private;
9725 uint32_t val, data;
9726 int ret;
9727
9728 if (WARN((I915_READ(LCPLL_CTL) &
9729 (LCPLL_PLL_DISABLE | LCPLL_PLL_LOCK |
9730 LCPLL_CD_CLOCK_DISABLE | LCPLL_ROOT_CD_CLOCK_DISABLE |
9731 LCPLL_CD2X_CLOCK_DISABLE | LCPLL_POWER_DOWN_ALLOW |
9732 LCPLL_CD_SOURCE_FCLK)) != LCPLL_PLL_LOCK,
9733 "trying to change cdclk frequency with cdclk not enabled\n"))
9734 return;
9735
9736 mutex_lock(&dev_priv->rps.hw_lock);
9737 ret = sandybridge_pcode_write(dev_priv,
9738 BDW_PCODE_DISPLAY_FREQ_CHANGE_REQ, 0x0);
9739 mutex_unlock(&dev_priv->rps.hw_lock);
9740 if (ret) {
9741 DRM_ERROR("failed to inform pcode about cdclk change\n");
9742 return;
9743 }
9744
9745 val = I915_READ(LCPLL_CTL);
9746 val |= LCPLL_CD_SOURCE_FCLK;
9747 I915_WRITE(LCPLL_CTL, val);
9748
5ba00178
TU
9749 if (wait_for_us(I915_READ(LCPLL_CTL) &
9750 LCPLL_CD_SOURCE_FCLK_DONE, 1))
b432e5cf
VS
9751 DRM_ERROR("Switching to FCLK failed\n");
9752
9753 val = I915_READ(LCPLL_CTL);
9754 val &= ~LCPLL_CLK_FREQ_MASK;
9755
9756 switch (cdclk) {
9757 case 450000:
9758 val |= LCPLL_CLK_FREQ_450;
9759 data = 0;
9760 break;
9761 case 540000:
9762 val |= LCPLL_CLK_FREQ_54O_BDW;
9763 data = 1;
9764 break;
9765 case 337500:
9766 val |= LCPLL_CLK_FREQ_337_5_BDW;
9767 data = 2;
9768 break;
9769 case 675000:
9770 val |= LCPLL_CLK_FREQ_675_BDW;
9771 data = 3;
9772 break;
9773 default:
9774 WARN(1, "invalid cdclk frequency\n");
9775 return;
9776 }
9777
9778 I915_WRITE(LCPLL_CTL, val);
9779
9780 val = I915_READ(LCPLL_CTL);
9781 val &= ~LCPLL_CD_SOURCE_FCLK;
9782 I915_WRITE(LCPLL_CTL, val);
9783
5ba00178
TU
9784 if (wait_for_us((I915_READ(LCPLL_CTL) &
9785 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
b432e5cf
VS
9786 DRM_ERROR("Switching back to LCPLL failed\n");
9787
9788 mutex_lock(&dev_priv->rps.hw_lock);
9789 sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ, data);
9790 mutex_unlock(&dev_priv->rps.hw_lock);
9791
7f1052a8
VS
9792 I915_WRITE(CDCLK_FREQ, DIV_ROUND_CLOSEST(cdclk, 1000) - 1);
9793
b432e5cf
VS
9794 intel_update_cdclk(dev);
9795
9796 WARN(cdclk != dev_priv->cdclk_freq,
9797 "cdclk requested %d kHz but got %d kHz\n",
9798 cdclk, dev_priv->cdclk_freq);
9799}
9800
587c7914
VS
9801static int broadwell_calc_cdclk(int max_pixclk)
9802{
9803 if (max_pixclk > 540000)
9804 return 675000;
9805 else if (max_pixclk > 450000)
9806 return 540000;
9807 else if (max_pixclk > 337500)
9808 return 450000;
9809 else
9810 return 337500;
9811}
9812
27c329ed 9813static int broadwell_modeset_calc_cdclk(struct drm_atomic_state *state)
b432e5cf 9814{
27c329ed 9815 struct drm_i915_private *dev_priv = to_i915(state->dev);
1a617b77 9816 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
27c329ed 9817 int max_pixclk = ilk_max_pixel_rate(state);
b432e5cf
VS
9818 int cdclk;
9819
9820 /*
9821 * FIXME should also account for plane ratio
9822 * once 64bpp pixel formats are supported.
9823 */
587c7914 9824 cdclk = broadwell_calc_cdclk(max_pixclk);
b432e5cf 9825
b432e5cf 9826 if (cdclk > dev_priv->max_cdclk_freq) {
63ba534e
ML
9827 DRM_DEBUG_KMS("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
9828 cdclk, dev_priv->max_cdclk_freq);
9829 return -EINVAL;
b432e5cf
VS
9830 }
9831
1a617b77
ML
9832 intel_state->cdclk = intel_state->dev_cdclk = cdclk;
9833 if (!intel_state->active_crtcs)
587c7914 9834 intel_state->dev_cdclk = broadwell_calc_cdclk(0);
b432e5cf
VS
9835
9836 return 0;
9837}
9838
27c329ed 9839static void broadwell_modeset_commit_cdclk(struct drm_atomic_state *old_state)
b432e5cf 9840{
27c329ed 9841 struct drm_device *dev = old_state->dev;
1a617b77
ML
9842 struct intel_atomic_state *old_intel_state =
9843 to_intel_atomic_state(old_state);
9844 unsigned req_cdclk = old_intel_state->dev_cdclk;
b432e5cf 9845
27c329ed 9846 broadwell_set_cdclk(dev, req_cdclk);
b432e5cf
VS
9847}
9848
c89e39f3
CT
9849static int skl_modeset_calc_cdclk(struct drm_atomic_state *state)
9850{
9851 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
9852 struct drm_i915_private *dev_priv = to_i915(state->dev);
9853 const int max_pixclk = ilk_max_pixel_rate(state);
a8ca4934 9854 int vco = intel_state->cdclk_pll_vco;
c89e39f3
CT
9855 int cdclk;
9856
9857 /*
9858 * FIXME should also account for plane ratio
9859 * once 64bpp pixel formats are supported.
9860 */
a8ca4934 9861 cdclk = skl_calc_cdclk(max_pixclk, vco);
c89e39f3
CT
9862
9863 /*
9864 * FIXME move the cdclk caclulation to
9865 * compute_config() so we can fail gracegully.
9866 */
9867 if (cdclk > dev_priv->max_cdclk_freq) {
9868 DRM_ERROR("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
9869 cdclk, dev_priv->max_cdclk_freq);
9870 cdclk = dev_priv->max_cdclk_freq;
9871 }
9872
9873 intel_state->cdclk = intel_state->dev_cdclk = cdclk;
9874 if (!intel_state->active_crtcs)
a8ca4934 9875 intel_state->dev_cdclk = skl_calc_cdclk(0, vco);
c89e39f3
CT
9876
9877 return 0;
9878}
9879
9880static void skl_modeset_commit_cdclk(struct drm_atomic_state *old_state)
9881{
1cd593e0
VS
9882 struct drm_i915_private *dev_priv = to_i915(old_state->dev);
9883 struct intel_atomic_state *intel_state = to_intel_atomic_state(old_state);
9884 unsigned int req_cdclk = intel_state->dev_cdclk;
9885 unsigned int req_vco = intel_state->cdclk_pll_vco;
c89e39f3 9886
1cd593e0 9887 skl_set_cdclk(dev_priv, req_cdclk, req_vco);
c89e39f3
CT
9888}
9889
190f68c5
ACO
9890static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
9891 struct intel_crtc_state *crtc_state)
09b4ddf9 9892{
af3997b5
MK
9893 struct intel_encoder *intel_encoder =
9894 intel_ddi_get_crtc_new_encoder(crtc_state);
9895
9896 if (intel_encoder->type != INTEL_OUTPUT_DSI) {
9897 if (!intel_ddi_pll_select(crtc, crtc_state))
9898 return -EINVAL;
9899 }
716c2e55 9900
c7653199 9901 crtc->lowfreq_avail = false;
644cef34 9902
c8f7a0db 9903 return 0;
79e53945
JB
9904}
9905
3760b59c
S
9906static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
9907 enum port port,
9908 struct intel_crtc_state *pipe_config)
9909{
8106ddbd
ACO
9910 enum intel_dpll_id id;
9911
3760b59c
S
9912 switch (port) {
9913 case PORT_A:
9914 pipe_config->ddi_pll_sel = SKL_DPLL0;
08250c4b 9915 id = DPLL_ID_SKL_DPLL0;
3760b59c
S
9916 break;
9917 case PORT_B:
9918 pipe_config->ddi_pll_sel = SKL_DPLL1;
08250c4b 9919 id = DPLL_ID_SKL_DPLL1;
3760b59c
S
9920 break;
9921 case PORT_C:
9922 pipe_config->ddi_pll_sel = SKL_DPLL2;
08250c4b 9923 id = DPLL_ID_SKL_DPLL2;
3760b59c
S
9924 break;
9925 default:
9926 DRM_ERROR("Incorrect port type\n");
8106ddbd 9927 return;
3760b59c 9928 }
8106ddbd
ACO
9929
9930 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
3760b59c
S
9931}
9932
96b7dfb7
S
9933static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
9934 enum port port,
5cec258b 9935 struct intel_crtc_state *pipe_config)
96b7dfb7 9936{
8106ddbd 9937 enum intel_dpll_id id;
a3c988ea 9938 u32 temp;
96b7dfb7
S
9939
9940 temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
9941 pipe_config->ddi_pll_sel = temp >> (port * 3 + 1);
9942
9943 switch (pipe_config->ddi_pll_sel) {
3148ade7 9944 case SKL_DPLL0:
a3c988ea
ACO
9945 id = DPLL_ID_SKL_DPLL0;
9946 break;
96b7dfb7 9947 case SKL_DPLL1:
8106ddbd 9948 id = DPLL_ID_SKL_DPLL1;
96b7dfb7
S
9949 break;
9950 case SKL_DPLL2:
8106ddbd 9951 id = DPLL_ID_SKL_DPLL2;
96b7dfb7
S
9952 break;
9953 case SKL_DPLL3:
8106ddbd 9954 id = DPLL_ID_SKL_DPLL3;
96b7dfb7 9955 break;
8106ddbd
ACO
9956 default:
9957 MISSING_CASE(pipe_config->ddi_pll_sel);
9958 return;
96b7dfb7 9959 }
8106ddbd
ACO
9960
9961 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
96b7dfb7
S
9962}
9963
7d2c8175
DL
9964static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
9965 enum port port,
5cec258b 9966 struct intel_crtc_state *pipe_config)
7d2c8175 9967{
8106ddbd
ACO
9968 enum intel_dpll_id id;
9969
7d2c8175
DL
9970 pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
9971
9972 switch (pipe_config->ddi_pll_sel) {
9973 case PORT_CLK_SEL_WRPLL1:
8106ddbd 9974 id = DPLL_ID_WRPLL1;
7d2c8175
DL
9975 break;
9976 case PORT_CLK_SEL_WRPLL2:
8106ddbd 9977 id = DPLL_ID_WRPLL2;
7d2c8175 9978 break;
00490c22 9979 case PORT_CLK_SEL_SPLL:
8106ddbd 9980 id = DPLL_ID_SPLL;
79bd23da 9981 break;
9d16da65
ACO
9982 case PORT_CLK_SEL_LCPLL_810:
9983 id = DPLL_ID_LCPLL_810;
9984 break;
9985 case PORT_CLK_SEL_LCPLL_1350:
9986 id = DPLL_ID_LCPLL_1350;
9987 break;
9988 case PORT_CLK_SEL_LCPLL_2700:
9989 id = DPLL_ID_LCPLL_2700;
9990 break;
8106ddbd
ACO
9991 default:
9992 MISSING_CASE(pipe_config->ddi_pll_sel);
9993 /* fall through */
9994 case PORT_CLK_SEL_NONE:
8106ddbd 9995 return;
7d2c8175 9996 }
8106ddbd
ACO
9997
9998 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
7d2c8175
DL
9999}
10000
cf30429e
JN
10001static bool hsw_get_transcoder_state(struct intel_crtc *crtc,
10002 struct intel_crtc_state *pipe_config,
10003 unsigned long *power_domain_mask)
10004{
10005 struct drm_device *dev = crtc->base.dev;
10006 struct drm_i915_private *dev_priv = dev->dev_private;
10007 enum intel_display_power_domain power_domain;
10008 u32 tmp;
10009
d9a7bc67
ID
10010 /*
10011 * The pipe->transcoder mapping is fixed with the exception of the eDP
10012 * transcoder handled below.
10013 */
cf30429e
JN
10014 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
10015
10016 /*
10017 * XXX: Do intel_display_power_get_if_enabled before reading this (for
10018 * consistency and less surprising code; it's in always on power).
10019 */
10020 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
10021 if (tmp & TRANS_DDI_FUNC_ENABLE) {
10022 enum pipe trans_edp_pipe;
10023 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
10024 default:
10025 WARN(1, "unknown pipe linked to edp transcoder\n");
10026 case TRANS_DDI_EDP_INPUT_A_ONOFF:
10027 case TRANS_DDI_EDP_INPUT_A_ON:
10028 trans_edp_pipe = PIPE_A;
10029 break;
10030 case TRANS_DDI_EDP_INPUT_B_ONOFF:
10031 trans_edp_pipe = PIPE_B;
10032 break;
10033 case TRANS_DDI_EDP_INPUT_C_ONOFF:
10034 trans_edp_pipe = PIPE_C;
10035 break;
10036 }
10037
10038 if (trans_edp_pipe == crtc->pipe)
10039 pipe_config->cpu_transcoder = TRANSCODER_EDP;
10040 }
10041
10042 power_domain = POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder);
10043 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
10044 return false;
10045 *power_domain_mask |= BIT(power_domain);
10046
10047 tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
10048
10049 return tmp & PIPECONF_ENABLE;
10050}
10051
4d1de975
JN
10052static bool bxt_get_dsi_transcoder_state(struct intel_crtc *crtc,
10053 struct intel_crtc_state *pipe_config,
10054 unsigned long *power_domain_mask)
10055{
10056 struct drm_device *dev = crtc->base.dev;
10057 struct drm_i915_private *dev_priv = dev->dev_private;
10058 enum intel_display_power_domain power_domain;
10059 enum port port;
10060 enum transcoder cpu_transcoder;
10061 u32 tmp;
10062
10063 pipe_config->has_dsi_encoder = false;
10064
10065 for_each_port_masked(port, BIT(PORT_A) | BIT(PORT_C)) {
10066 if (port == PORT_A)
10067 cpu_transcoder = TRANSCODER_DSI_A;
10068 else
10069 cpu_transcoder = TRANSCODER_DSI_C;
10070
10071 power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
10072 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
10073 continue;
10074 *power_domain_mask |= BIT(power_domain);
10075
db18b6a6
ID
10076 /*
10077 * The PLL needs to be enabled with a valid divider
10078 * configuration, otherwise accessing DSI registers will hang
10079 * the machine. See BSpec North Display Engine
10080 * registers/MIPI[BXT]. We can break out here early, since we
10081 * need the same DSI PLL to be enabled for both DSI ports.
10082 */
10083 if (!intel_dsi_pll_is_enabled(dev_priv))
10084 break;
10085
4d1de975
JN
10086 /* XXX: this works for video mode only */
10087 tmp = I915_READ(BXT_MIPI_PORT_CTRL(port));
10088 if (!(tmp & DPI_ENABLE))
10089 continue;
10090
10091 tmp = I915_READ(MIPI_CTRL(port));
10092 if ((tmp & BXT_PIPE_SELECT_MASK) != BXT_PIPE_SELECT(crtc->pipe))
10093 continue;
10094
10095 pipe_config->cpu_transcoder = cpu_transcoder;
10096 pipe_config->has_dsi_encoder = true;
10097 break;
10098 }
10099
10100 return pipe_config->has_dsi_encoder;
10101}
10102
26804afd 10103static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
5cec258b 10104 struct intel_crtc_state *pipe_config)
26804afd
DV
10105{
10106 struct drm_device *dev = crtc->base.dev;
10107 struct drm_i915_private *dev_priv = dev->dev_private;
d452c5b6 10108 struct intel_shared_dpll *pll;
26804afd
DV
10109 enum port port;
10110 uint32_t tmp;
10111
10112 tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
10113
10114 port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
10115
ef11bdb3 10116 if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev))
96b7dfb7 10117 skylake_get_ddi_pll(dev_priv, port, pipe_config);
3760b59c
S
10118 else if (IS_BROXTON(dev))
10119 bxt_get_ddi_pll(dev_priv, port, pipe_config);
96b7dfb7
S
10120 else
10121 haswell_get_ddi_pll(dev_priv, port, pipe_config);
9cd86933 10122
8106ddbd
ACO
10123 pll = pipe_config->shared_dpll;
10124 if (pll) {
2edd6443
ACO
10125 WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
10126 &pipe_config->dpll_hw_state));
d452c5b6
DV
10127 }
10128
26804afd
DV
10129 /*
10130 * Haswell has only FDI/PCH transcoder A. It is which is connected to
10131 * DDI E. So just check whether this pipe is wired to DDI E and whether
10132 * the PCH transcoder is on.
10133 */
ca370455
DL
10134 if (INTEL_INFO(dev)->gen < 9 &&
10135 (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
26804afd
DV
10136 pipe_config->has_pch_encoder = true;
10137
10138 tmp = I915_READ(FDI_RX_CTL(PIPE_A));
10139 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
10140 FDI_DP_PORT_WIDTH_SHIFT) + 1;
10141
10142 ironlake_get_fdi_m_n_config(crtc, pipe_config);
10143 }
10144}
10145
0e8ffe1b 10146static bool haswell_get_pipe_config(struct intel_crtc *crtc,
5cec258b 10147 struct intel_crtc_state *pipe_config)
0e8ffe1b
DV
10148{
10149 struct drm_device *dev = crtc->base.dev;
10150 struct drm_i915_private *dev_priv = dev->dev_private;
1729050e
ID
10151 enum intel_display_power_domain power_domain;
10152 unsigned long power_domain_mask;
cf30429e 10153 bool active;
0e8ffe1b 10154
1729050e
ID
10155 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
10156 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
b5482bd0 10157 return false;
1729050e
ID
10158 power_domain_mask = BIT(power_domain);
10159
8106ddbd 10160 pipe_config->shared_dpll = NULL;
c0d43d62 10161
cf30429e 10162 active = hsw_get_transcoder_state(crtc, pipe_config, &power_domain_mask);
eccb140b 10163
4d1de975
JN
10164 if (IS_BROXTON(dev_priv)) {
10165 bxt_get_dsi_transcoder_state(crtc, pipe_config,
10166 &power_domain_mask);
10167 WARN_ON(active && pipe_config->has_dsi_encoder);
10168 if (pipe_config->has_dsi_encoder)
10169 active = true;
10170 }
10171
cf30429e 10172 if (!active)
1729050e 10173 goto out;
0e8ffe1b 10174
4d1de975
JN
10175 if (!pipe_config->has_dsi_encoder) {
10176 haswell_get_ddi_port_state(crtc, pipe_config);
10177 intel_get_pipe_timings(crtc, pipe_config);
10178 }
627eb5a3 10179
bc58be60 10180 intel_get_pipe_src_size(crtc, pipe_config);
1bd1bd80 10181
05dc698c
LL
10182 pipe_config->gamma_mode =
10183 I915_READ(GAMMA_MODE(crtc->pipe)) & GAMMA_MODE_MODE_MASK;
10184
a1b2278e
CK
10185 if (INTEL_INFO(dev)->gen >= 9) {
10186 skl_init_scalers(dev, crtc, pipe_config);
10187 }
10188
af99ceda
CK
10189 if (INTEL_INFO(dev)->gen >= 9) {
10190 pipe_config->scaler_state.scaler_id = -1;
10191 pipe_config->scaler_state.scaler_users &= ~(1 << SKL_CRTC_INDEX);
10192 }
10193
1729050e
ID
10194 power_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
10195 if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
10196 power_domain_mask |= BIT(power_domain);
1c132b44 10197 if (INTEL_INFO(dev)->gen >= 9)
bd2e244f 10198 skylake_get_pfit_config(crtc, pipe_config);
ff6d9f55 10199 else
1c132b44 10200 ironlake_get_pfit_config(crtc, pipe_config);
bd2e244f 10201 }
88adfff1 10202
e59150dc
JB
10203 if (IS_HASWELL(dev))
10204 pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
10205 (I915_READ(IPS_CTL) & IPS_ENABLE);
42db64ef 10206
4d1de975
JN
10207 if (pipe_config->cpu_transcoder != TRANSCODER_EDP &&
10208 !transcoder_is_dsi(pipe_config->cpu_transcoder)) {
ebb69c95
CT
10209 pipe_config->pixel_multiplier =
10210 I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
10211 } else {
10212 pipe_config->pixel_multiplier = 1;
10213 }
6c49f241 10214
1729050e
ID
10215out:
10216 for_each_power_domain(power_domain, power_domain_mask)
10217 intel_display_power_put(dev_priv, power_domain);
10218
cf30429e 10219 return active;
0e8ffe1b
DV
10220}
10221
55a08b3f
ML
10222static void i845_update_cursor(struct drm_crtc *crtc, u32 base,
10223 const struct intel_plane_state *plane_state)
560b85bb
CW
10224{
10225 struct drm_device *dev = crtc->dev;
10226 struct drm_i915_private *dev_priv = dev->dev_private;
10227 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
dc41c154 10228 uint32_t cntl = 0, size = 0;
560b85bb 10229
55a08b3f
ML
10230 if (plane_state && plane_state->visible) {
10231 unsigned int width = plane_state->base.crtc_w;
10232 unsigned int height = plane_state->base.crtc_h;
dc41c154
VS
10233 unsigned int stride = roundup_pow_of_two(width) * 4;
10234
10235 switch (stride) {
10236 default:
10237 WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
10238 width, stride);
10239 stride = 256;
10240 /* fallthrough */
10241 case 256:
10242 case 512:
10243 case 1024:
10244 case 2048:
10245 break;
4b0e333e
CW
10246 }
10247
dc41c154
VS
10248 cntl |= CURSOR_ENABLE |
10249 CURSOR_GAMMA_ENABLE |
10250 CURSOR_FORMAT_ARGB |
10251 CURSOR_STRIDE(stride);
10252
10253 size = (height << 12) | width;
4b0e333e 10254 }
560b85bb 10255
dc41c154
VS
10256 if (intel_crtc->cursor_cntl != 0 &&
10257 (intel_crtc->cursor_base != base ||
10258 intel_crtc->cursor_size != size ||
10259 intel_crtc->cursor_cntl != cntl)) {
10260 /* On these chipsets we can only modify the base/size/stride
10261 * whilst the cursor is disabled.
10262 */
0b87c24e
VS
10263 I915_WRITE(CURCNTR(PIPE_A), 0);
10264 POSTING_READ(CURCNTR(PIPE_A));
dc41c154 10265 intel_crtc->cursor_cntl = 0;
4b0e333e 10266 }
560b85bb 10267
99d1f387 10268 if (intel_crtc->cursor_base != base) {
0b87c24e 10269 I915_WRITE(CURBASE(PIPE_A), base);
99d1f387
VS
10270 intel_crtc->cursor_base = base;
10271 }
4726e0b0 10272
dc41c154
VS
10273 if (intel_crtc->cursor_size != size) {
10274 I915_WRITE(CURSIZE, size);
10275 intel_crtc->cursor_size = size;
4b0e333e 10276 }
560b85bb 10277
4b0e333e 10278 if (intel_crtc->cursor_cntl != cntl) {
0b87c24e
VS
10279 I915_WRITE(CURCNTR(PIPE_A), cntl);
10280 POSTING_READ(CURCNTR(PIPE_A));
4b0e333e 10281 intel_crtc->cursor_cntl = cntl;
560b85bb 10282 }
560b85bb
CW
10283}
10284
55a08b3f
ML
10285static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base,
10286 const struct intel_plane_state *plane_state)
65a21cd6
JB
10287{
10288 struct drm_device *dev = crtc->dev;
10289 struct drm_i915_private *dev_priv = dev->dev_private;
10290 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10291 int pipe = intel_crtc->pipe;
663f3122 10292 uint32_t cntl = 0;
4b0e333e 10293
55a08b3f 10294 if (plane_state && plane_state->visible) {
4b0e333e 10295 cntl = MCURSOR_GAMMA_ENABLE;
55a08b3f 10296 switch (plane_state->base.crtc_w) {
4726e0b0
SK
10297 case 64:
10298 cntl |= CURSOR_MODE_64_ARGB_AX;
10299 break;
10300 case 128:
10301 cntl |= CURSOR_MODE_128_ARGB_AX;
10302 break;
10303 case 256:
10304 cntl |= CURSOR_MODE_256_ARGB_AX;
10305 break;
10306 default:
55a08b3f 10307 MISSING_CASE(plane_state->base.crtc_w);
4726e0b0 10308 return;
65a21cd6 10309 }
4b0e333e 10310 cntl |= pipe << 28; /* Connect to correct pipe */
47bf17a7 10311
fc6f93bc 10312 if (HAS_DDI(dev))
47bf17a7 10313 cntl |= CURSOR_PIPE_CSC_ENABLE;
65a21cd6 10314
55a08b3f
ML
10315 if (plane_state->base.rotation == BIT(DRM_ROTATE_180))
10316 cntl |= CURSOR_ROTATE_180;
10317 }
4398ad45 10318
4b0e333e
CW
10319 if (intel_crtc->cursor_cntl != cntl) {
10320 I915_WRITE(CURCNTR(pipe), cntl);
10321 POSTING_READ(CURCNTR(pipe));
10322 intel_crtc->cursor_cntl = cntl;
65a21cd6 10323 }
4b0e333e 10324
65a21cd6 10325 /* and commit changes on next vblank */
5efb3e28
VS
10326 I915_WRITE(CURBASE(pipe), base);
10327 POSTING_READ(CURBASE(pipe));
99d1f387
VS
10328
10329 intel_crtc->cursor_base = base;
65a21cd6
JB
10330}
10331
cda4b7d3 10332/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
6b383a7f 10333static void intel_crtc_update_cursor(struct drm_crtc *crtc,
55a08b3f 10334 const struct intel_plane_state *plane_state)
cda4b7d3
CW
10335{
10336 struct drm_device *dev = crtc->dev;
10337 struct drm_i915_private *dev_priv = dev->dev_private;
10338 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10339 int pipe = intel_crtc->pipe;
55a08b3f
ML
10340 u32 base = intel_crtc->cursor_addr;
10341 u32 pos = 0;
cda4b7d3 10342
55a08b3f
ML
10343 if (plane_state) {
10344 int x = plane_state->base.crtc_x;
10345 int y = plane_state->base.crtc_y;
cda4b7d3 10346
55a08b3f
ML
10347 if (x < 0) {
10348 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
10349 x = -x;
10350 }
10351 pos |= x << CURSOR_X_SHIFT;
cda4b7d3 10352
55a08b3f
ML
10353 if (y < 0) {
10354 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
10355 y = -y;
10356 }
10357 pos |= y << CURSOR_Y_SHIFT;
10358
10359 /* ILK+ do this automagically */
10360 if (HAS_GMCH_DISPLAY(dev) &&
10361 plane_state->base.rotation == BIT(DRM_ROTATE_180)) {
10362 base += (plane_state->base.crtc_h *
10363 plane_state->base.crtc_w - 1) * 4;
10364 }
cda4b7d3 10365 }
cda4b7d3 10366
5efb3e28
VS
10367 I915_WRITE(CURPOS(pipe), pos);
10368
8ac54669 10369 if (IS_845G(dev) || IS_I865G(dev))
55a08b3f 10370 i845_update_cursor(crtc, base, plane_state);
5efb3e28 10371 else
55a08b3f 10372 i9xx_update_cursor(crtc, base, plane_state);
cda4b7d3
CW
10373}
10374
dc41c154
VS
10375static bool cursor_size_ok(struct drm_device *dev,
10376 uint32_t width, uint32_t height)
10377{
10378 if (width == 0 || height == 0)
10379 return false;
10380
10381 /*
10382 * 845g/865g are special in that they are only limited by
10383 * the width of their cursors, the height is arbitrary up to
10384 * the precision of the register. Everything else requires
10385 * square cursors, limited to a few power-of-two sizes.
10386 */
10387 if (IS_845G(dev) || IS_I865G(dev)) {
10388 if ((width & 63) != 0)
10389 return false;
10390
10391 if (width > (IS_845G(dev) ? 64 : 512))
10392 return false;
10393
10394 if (height > 1023)
10395 return false;
10396 } else {
10397 switch (width | height) {
10398 case 256:
10399 case 128:
10400 if (IS_GEN2(dev))
10401 return false;
10402 case 64:
10403 break;
10404 default:
10405 return false;
10406 }
10407 }
10408
10409 return true;
10410}
10411
79e53945
JB
10412/* VESA 640x480x72Hz mode to set on the pipe */
10413static struct drm_display_mode load_detect_mode = {
10414 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
10415 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
10416};
10417
a8bb6818
DV
10418struct drm_framebuffer *
10419__intel_framebuffer_create(struct drm_device *dev,
10420 struct drm_mode_fb_cmd2 *mode_cmd,
10421 struct drm_i915_gem_object *obj)
d2dff872
CW
10422{
10423 struct intel_framebuffer *intel_fb;
10424 int ret;
10425
10426 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
dcb1394e 10427 if (!intel_fb)
d2dff872 10428 return ERR_PTR(-ENOMEM);
d2dff872
CW
10429
10430 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
dd4916c5
DV
10431 if (ret)
10432 goto err;
d2dff872
CW
10433
10434 return &intel_fb->base;
dcb1394e 10435
dd4916c5 10436err:
dd4916c5 10437 kfree(intel_fb);
dd4916c5 10438 return ERR_PTR(ret);
d2dff872
CW
10439}
10440
b5ea642a 10441static struct drm_framebuffer *
a8bb6818
DV
10442intel_framebuffer_create(struct drm_device *dev,
10443 struct drm_mode_fb_cmd2 *mode_cmd,
10444 struct drm_i915_gem_object *obj)
10445{
10446 struct drm_framebuffer *fb;
10447 int ret;
10448
10449 ret = i915_mutex_lock_interruptible(dev);
10450 if (ret)
10451 return ERR_PTR(ret);
10452 fb = __intel_framebuffer_create(dev, mode_cmd, obj);
10453 mutex_unlock(&dev->struct_mutex);
10454
10455 return fb;
10456}
10457
d2dff872
CW
10458static u32
10459intel_framebuffer_pitch_for_width(int width, int bpp)
10460{
10461 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
10462 return ALIGN(pitch, 64);
10463}
10464
10465static u32
10466intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
10467{
10468 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
1267a26b 10469 return PAGE_ALIGN(pitch * mode->vdisplay);
d2dff872
CW
10470}
10471
10472static struct drm_framebuffer *
10473intel_framebuffer_create_for_mode(struct drm_device *dev,
10474 struct drm_display_mode *mode,
10475 int depth, int bpp)
10476{
dcb1394e 10477 struct drm_framebuffer *fb;
d2dff872 10478 struct drm_i915_gem_object *obj;
0fed39bd 10479 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
d2dff872 10480
d37cd8a8 10481 obj = i915_gem_object_create(dev,
d2dff872 10482 intel_framebuffer_size_for_mode(mode, bpp));
fe3db79b
CW
10483 if (IS_ERR(obj))
10484 return ERR_CAST(obj);
d2dff872
CW
10485
10486 mode_cmd.width = mode->hdisplay;
10487 mode_cmd.height = mode->vdisplay;
308e5bcb
JB
10488 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
10489 bpp);
5ca0c34a 10490 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
d2dff872 10491
dcb1394e
LW
10492 fb = intel_framebuffer_create(dev, &mode_cmd, obj);
10493 if (IS_ERR(fb))
10494 drm_gem_object_unreference_unlocked(&obj->base);
10495
10496 return fb;
d2dff872
CW
10497}
10498
10499static struct drm_framebuffer *
10500mode_fits_in_fbdev(struct drm_device *dev,
10501 struct drm_display_mode *mode)
10502{
0695726e 10503#ifdef CONFIG_DRM_FBDEV_EMULATION
d2dff872
CW
10504 struct drm_i915_private *dev_priv = dev->dev_private;
10505 struct drm_i915_gem_object *obj;
10506 struct drm_framebuffer *fb;
10507
4c0e5528 10508 if (!dev_priv->fbdev)
d2dff872
CW
10509 return NULL;
10510
4c0e5528 10511 if (!dev_priv->fbdev->fb)
d2dff872
CW
10512 return NULL;
10513
4c0e5528
DV
10514 obj = dev_priv->fbdev->fb->obj;
10515 BUG_ON(!obj);
10516
8bcd4553 10517 fb = &dev_priv->fbdev->fb->base;
01f2c773
VS
10518 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
10519 fb->bits_per_pixel))
d2dff872
CW
10520 return NULL;
10521
01f2c773 10522 if (obj->base.size < mode->vdisplay * fb->pitches[0])
d2dff872
CW
10523 return NULL;
10524
edde3617 10525 drm_framebuffer_reference(fb);
d2dff872 10526 return fb;
4520f53a
DV
10527#else
10528 return NULL;
10529#endif
d2dff872
CW
10530}
10531
d3a40d1b
ACO
10532static int intel_modeset_setup_plane_state(struct drm_atomic_state *state,
10533 struct drm_crtc *crtc,
10534 struct drm_display_mode *mode,
10535 struct drm_framebuffer *fb,
10536 int x, int y)
10537{
10538 struct drm_plane_state *plane_state;
10539 int hdisplay, vdisplay;
10540 int ret;
10541
10542 plane_state = drm_atomic_get_plane_state(state, crtc->primary);
10543 if (IS_ERR(plane_state))
10544 return PTR_ERR(plane_state);
10545
10546 if (mode)
10547 drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
10548 else
10549 hdisplay = vdisplay = 0;
10550
10551 ret = drm_atomic_set_crtc_for_plane(plane_state, fb ? crtc : NULL);
10552 if (ret)
10553 return ret;
10554 drm_atomic_set_fb_for_plane(plane_state, fb);
10555 plane_state->crtc_x = 0;
10556 plane_state->crtc_y = 0;
10557 plane_state->crtc_w = hdisplay;
10558 plane_state->crtc_h = vdisplay;
10559 plane_state->src_x = x << 16;
10560 plane_state->src_y = y << 16;
10561 plane_state->src_w = hdisplay << 16;
10562 plane_state->src_h = vdisplay << 16;
10563
10564 return 0;
10565}
10566
d2434ab7 10567bool intel_get_load_detect_pipe(struct drm_connector *connector,
7173188d 10568 struct drm_display_mode *mode,
51fd371b
RC
10569 struct intel_load_detect_pipe *old,
10570 struct drm_modeset_acquire_ctx *ctx)
79e53945
JB
10571{
10572 struct intel_crtc *intel_crtc;
d2434ab7
DV
10573 struct intel_encoder *intel_encoder =
10574 intel_attached_encoder(connector);
79e53945 10575 struct drm_crtc *possible_crtc;
4ef69c7a 10576 struct drm_encoder *encoder = &intel_encoder->base;
79e53945
JB
10577 struct drm_crtc *crtc = NULL;
10578 struct drm_device *dev = encoder->dev;
94352cf9 10579 struct drm_framebuffer *fb;
51fd371b 10580 struct drm_mode_config *config = &dev->mode_config;
edde3617 10581 struct drm_atomic_state *state = NULL, *restore_state = NULL;
944b0c76 10582 struct drm_connector_state *connector_state;
4be07317 10583 struct intel_crtc_state *crtc_state;
51fd371b 10584 int ret, i = -1;
79e53945 10585
d2dff872 10586 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
c23cc417 10587 connector->base.id, connector->name,
8e329a03 10588 encoder->base.id, encoder->name);
d2dff872 10589
edde3617
ML
10590 old->restore_state = NULL;
10591
51fd371b
RC
10592retry:
10593 ret = drm_modeset_lock(&config->connection_mutex, ctx);
10594 if (ret)
ad3c558f 10595 goto fail;
6e9f798d 10596
79e53945
JB
10597 /*
10598 * Algorithm gets a little messy:
7a5e4805 10599 *
79e53945
JB
10600 * - if the connector already has an assigned crtc, use it (but make
10601 * sure it's on first)
7a5e4805 10602 *
79e53945
JB
10603 * - try to find the first unused crtc that can drive this connector,
10604 * and use that if we find one
79e53945
JB
10605 */
10606
10607 /* See if we already have a CRTC for this connector */
edde3617
ML
10608 if (connector->state->crtc) {
10609 crtc = connector->state->crtc;
8261b191 10610
51fd371b 10611 ret = drm_modeset_lock(&crtc->mutex, ctx);
4d02e2de 10612 if (ret)
ad3c558f 10613 goto fail;
8261b191
CW
10614
10615 /* Make sure the crtc and connector are running */
edde3617 10616 goto found;
79e53945
JB
10617 }
10618
10619 /* Find an unused one (if possible) */
70e1e0ec 10620 for_each_crtc(dev, possible_crtc) {
79e53945
JB
10621 i++;
10622 if (!(encoder->possible_crtcs & (1 << i)))
10623 continue;
edde3617
ML
10624
10625 ret = drm_modeset_lock(&possible_crtc->mutex, ctx);
10626 if (ret)
10627 goto fail;
10628
10629 if (possible_crtc->state->enable) {
10630 drm_modeset_unlock(&possible_crtc->mutex);
a459249c 10631 continue;
edde3617 10632 }
a459249c
VS
10633
10634 crtc = possible_crtc;
10635 break;
79e53945
JB
10636 }
10637
10638 /*
10639 * If we didn't find an unused CRTC, don't use any.
10640 */
10641 if (!crtc) {
7173188d 10642 DRM_DEBUG_KMS("no pipe available for load-detect\n");
ad3c558f 10643 goto fail;
79e53945
JB
10644 }
10645
edde3617
ML
10646found:
10647 intel_crtc = to_intel_crtc(crtc);
10648
4d02e2de
DV
10649 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
10650 if (ret)
ad3c558f 10651 goto fail;
79e53945 10652
83a57153 10653 state = drm_atomic_state_alloc(dev);
edde3617
ML
10654 restore_state = drm_atomic_state_alloc(dev);
10655 if (!state || !restore_state) {
10656 ret = -ENOMEM;
10657 goto fail;
10658 }
83a57153
ACO
10659
10660 state->acquire_ctx = ctx;
edde3617 10661 restore_state->acquire_ctx = ctx;
83a57153 10662
944b0c76
ACO
10663 connector_state = drm_atomic_get_connector_state(state, connector);
10664 if (IS_ERR(connector_state)) {
10665 ret = PTR_ERR(connector_state);
10666 goto fail;
10667 }
10668
edde3617
ML
10669 ret = drm_atomic_set_crtc_for_connector(connector_state, crtc);
10670 if (ret)
10671 goto fail;
944b0c76 10672
4be07317
ACO
10673 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
10674 if (IS_ERR(crtc_state)) {
10675 ret = PTR_ERR(crtc_state);
10676 goto fail;
10677 }
10678
49d6fa21 10679 crtc_state->base.active = crtc_state->base.enable = true;
4be07317 10680
6492711d
CW
10681 if (!mode)
10682 mode = &load_detect_mode;
79e53945 10683
d2dff872
CW
10684 /* We need a framebuffer large enough to accommodate all accesses
10685 * that the plane may generate whilst we perform load detection.
10686 * We can not rely on the fbcon either being present (we get called
10687 * during its initialisation to detect all boot displays, or it may
10688 * not even exist) or that it is large enough to satisfy the
10689 * requested mode.
10690 */
94352cf9
DV
10691 fb = mode_fits_in_fbdev(dev, mode);
10692 if (fb == NULL) {
d2dff872 10693 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
94352cf9 10694 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
d2dff872
CW
10695 } else
10696 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
94352cf9 10697 if (IS_ERR(fb)) {
d2dff872 10698 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
412b61d8 10699 goto fail;
79e53945 10700 }
79e53945 10701
d3a40d1b
ACO
10702 ret = intel_modeset_setup_plane_state(state, crtc, mode, fb, 0, 0);
10703 if (ret)
10704 goto fail;
10705
edde3617
ML
10706 drm_framebuffer_unreference(fb);
10707
10708 ret = drm_atomic_set_mode_for_crtc(&crtc_state->base, mode);
10709 if (ret)
10710 goto fail;
10711
10712 ret = PTR_ERR_OR_ZERO(drm_atomic_get_connector_state(restore_state, connector));
10713 if (!ret)
10714 ret = PTR_ERR_OR_ZERO(drm_atomic_get_crtc_state(restore_state, crtc));
10715 if (!ret)
10716 ret = PTR_ERR_OR_ZERO(drm_atomic_get_plane_state(restore_state, crtc->primary));
10717 if (ret) {
10718 DRM_DEBUG_KMS("Failed to create a copy of old state to restore: %i\n", ret);
10719 goto fail;
10720 }
8c7b5ccb 10721
3ba86073
ML
10722 ret = drm_atomic_commit(state);
10723 if (ret) {
6492711d 10724 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
412b61d8 10725 goto fail;
79e53945 10726 }
edde3617
ML
10727
10728 old->restore_state = restore_state;
7173188d 10729
79e53945 10730 /* let the connector get through one full cycle before testing */
9d0498a2 10731 intel_wait_for_vblank(dev, intel_crtc->pipe);
7173188d 10732 return true;
412b61d8 10733
ad3c558f 10734fail:
e5d958ef 10735 drm_atomic_state_free(state);
edde3617
ML
10736 drm_atomic_state_free(restore_state);
10737 restore_state = state = NULL;
83a57153 10738
51fd371b
RC
10739 if (ret == -EDEADLK) {
10740 drm_modeset_backoff(ctx);
10741 goto retry;
10742 }
10743
412b61d8 10744 return false;
79e53945
JB
10745}
10746
d2434ab7 10747void intel_release_load_detect_pipe(struct drm_connector *connector,
49172fee
ACO
10748 struct intel_load_detect_pipe *old,
10749 struct drm_modeset_acquire_ctx *ctx)
79e53945 10750{
d2434ab7
DV
10751 struct intel_encoder *intel_encoder =
10752 intel_attached_encoder(connector);
4ef69c7a 10753 struct drm_encoder *encoder = &intel_encoder->base;
edde3617 10754 struct drm_atomic_state *state = old->restore_state;
d3a40d1b 10755 int ret;
79e53945 10756
d2dff872 10757 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
c23cc417 10758 connector->base.id, connector->name,
8e329a03 10759 encoder->base.id, encoder->name);
d2dff872 10760
edde3617 10761 if (!state)
0622a53c 10762 return;
79e53945 10763
edde3617
ML
10764 ret = drm_atomic_commit(state);
10765 if (ret) {
10766 DRM_DEBUG_KMS("Couldn't release load detect pipe: %i\n", ret);
10767 drm_atomic_state_free(state);
10768 }
79e53945
JB
10769}
10770
da4a1efa 10771static int i9xx_pll_refclk(struct drm_device *dev,
5cec258b 10772 const struct intel_crtc_state *pipe_config)
da4a1efa
VS
10773{
10774 struct drm_i915_private *dev_priv = dev->dev_private;
10775 u32 dpll = pipe_config->dpll_hw_state.dpll;
10776
10777 if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
e91e941b 10778 return dev_priv->vbt.lvds_ssc_freq;
da4a1efa
VS
10779 else if (HAS_PCH_SPLIT(dev))
10780 return 120000;
10781 else if (!IS_GEN2(dev))
10782 return 96000;
10783 else
10784 return 48000;
10785}
10786
79e53945 10787/* Returns the clock of the currently programmed mode of the given pipe. */
f1f644dc 10788static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 10789 struct intel_crtc_state *pipe_config)
79e53945 10790{
f1f644dc 10791 struct drm_device *dev = crtc->base.dev;
79e53945 10792 struct drm_i915_private *dev_priv = dev->dev_private;
f1f644dc 10793 int pipe = pipe_config->cpu_transcoder;
293623f7 10794 u32 dpll = pipe_config->dpll_hw_state.dpll;
79e53945 10795 u32 fp;
9e2c8475 10796 struct dpll clock;
dccbea3b 10797 int port_clock;
da4a1efa 10798 int refclk = i9xx_pll_refclk(dev, pipe_config);
79e53945
JB
10799
10800 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
293623f7 10801 fp = pipe_config->dpll_hw_state.fp0;
79e53945 10802 else
293623f7 10803 fp = pipe_config->dpll_hw_state.fp1;
79e53945
JB
10804
10805 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
f2b115e6
AJ
10806 if (IS_PINEVIEW(dev)) {
10807 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
10808 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
2177832f
SL
10809 } else {
10810 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
10811 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
10812 }
10813
a6c45cf0 10814 if (!IS_GEN2(dev)) {
f2b115e6
AJ
10815 if (IS_PINEVIEW(dev))
10816 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
10817 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
2177832f
SL
10818 else
10819 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
79e53945
JB
10820 DPLL_FPA01_P1_POST_DIV_SHIFT);
10821
10822 switch (dpll & DPLL_MODE_MASK) {
10823 case DPLLB_MODE_DAC_SERIAL:
10824 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
10825 5 : 10;
10826 break;
10827 case DPLLB_MODE_LVDS:
10828 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
10829 7 : 14;
10830 break;
10831 default:
28c97730 10832 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
79e53945 10833 "mode\n", (int)(dpll & DPLL_MODE_MASK));
f1f644dc 10834 return;
79e53945
JB
10835 }
10836
ac58c3f0 10837 if (IS_PINEVIEW(dev))
dccbea3b 10838 port_clock = pnv_calc_dpll_params(refclk, &clock);
ac58c3f0 10839 else
dccbea3b 10840 port_clock = i9xx_calc_dpll_params(refclk, &clock);
79e53945 10841 } else {
0fb58223 10842 u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
b1c560d1 10843 bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
79e53945
JB
10844
10845 if (is_lvds) {
10846 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
10847 DPLL_FPA01_P1_POST_DIV_SHIFT);
b1c560d1
VS
10848
10849 if (lvds & LVDS_CLKB_POWER_UP)
10850 clock.p2 = 7;
10851 else
10852 clock.p2 = 14;
79e53945
JB
10853 } else {
10854 if (dpll & PLL_P1_DIVIDE_BY_TWO)
10855 clock.p1 = 2;
10856 else {
10857 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
10858 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
10859 }
10860 if (dpll & PLL_P2_DIVIDE_BY_4)
10861 clock.p2 = 4;
10862 else
10863 clock.p2 = 2;
79e53945 10864 }
da4a1efa 10865
dccbea3b 10866 port_clock = i9xx_calc_dpll_params(refclk, &clock);
79e53945
JB
10867 }
10868
18442d08
VS
10869 /*
10870 * This value includes pixel_multiplier. We will use
241bfc38 10871 * port_clock to compute adjusted_mode.crtc_clock in the
18442d08
VS
10872 * encoder's get_config() function.
10873 */
dccbea3b 10874 pipe_config->port_clock = port_clock;
f1f644dc
JB
10875}
10876
6878da05
VS
10877int intel_dotclock_calculate(int link_freq,
10878 const struct intel_link_m_n *m_n)
f1f644dc 10879{
f1f644dc
JB
10880 /*
10881 * The calculation for the data clock is:
1041a02f 10882 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
f1f644dc 10883 * But we want to avoid losing precison if possible, so:
1041a02f 10884 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
f1f644dc
JB
10885 *
10886 * and the link clock is simpler:
1041a02f 10887 * link_clock = (m * link_clock) / n
f1f644dc
JB
10888 */
10889
6878da05
VS
10890 if (!m_n->link_n)
10891 return 0;
f1f644dc 10892
6878da05
VS
10893 return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
10894}
f1f644dc 10895
18442d08 10896static void ironlake_pch_clock_get(struct intel_crtc *crtc,
5cec258b 10897 struct intel_crtc_state *pipe_config)
6878da05 10898{
e3b247da 10899 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
79e53945 10900
18442d08
VS
10901 /* read out port_clock from the DPLL */
10902 i9xx_crtc_clock_get(crtc, pipe_config);
f1f644dc 10903
f1f644dc 10904 /*
e3b247da
VS
10905 * In case there is an active pipe without active ports,
10906 * we may need some idea for the dotclock anyway.
10907 * Calculate one based on the FDI configuration.
79e53945 10908 */
2d112de7 10909 pipe_config->base.adjusted_mode.crtc_clock =
21a727b3 10910 intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
18442d08 10911 &pipe_config->fdi_m_n);
79e53945
JB
10912}
10913
10914/** Returns the currently programmed mode of the given pipe. */
10915struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
10916 struct drm_crtc *crtc)
10917{
548f245b 10918 struct drm_i915_private *dev_priv = dev->dev_private;
79e53945 10919 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6e3c9717 10920 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
79e53945 10921 struct drm_display_mode *mode;
3f36b937 10922 struct intel_crtc_state *pipe_config;
fe2b8f9d
PZ
10923 int htot = I915_READ(HTOTAL(cpu_transcoder));
10924 int hsync = I915_READ(HSYNC(cpu_transcoder));
10925 int vtot = I915_READ(VTOTAL(cpu_transcoder));
10926 int vsync = I915_READ(VSYNC(cpu_transcoder));
293623f7 10927 enum pipe pipe = intel_crtc->pipe;
79e53945
JB
10928
10929 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
10930 if (!mode)
10931 return NULL;
10932
3f36b937
TU
10933 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
10934 if (!pipe_config) {
10935 kfree(mode);
10936 return NULL;
10937 }
10938
f1f644dc
JB
10939 /*
10940 * Construct a pipe_config sufficient for getting the clock info
10941 * back out of crtc_clock_get.
10942 *
10943 * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
10944 * to use a real value here instead.
10945 */
3f36b937
TU
10946 pipe_config->cpu_transcoder = (enum transcoder) pipe;
10947 pipe_config->pixel_multiplier = 1;
10948 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(pipe));
10949 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(pipe));
10950 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(pipe));
10951 i9xx_crtc_clock_get(intel_crtc, pipe_config);
10952
10953 mode->clock = pipe_config->port_clock / pipe_config->pixel_multiplier;
79e53945
JB
10954 mode->hdisplay = (htot & 0xffff) + 1;
10955 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
10956 mode->hsync_start = (hsync & 0xffff) + 1;
10957 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
10958 mode->vdisplay = (vtot & 0xffff) + 1;
10959 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
10960 mode->vsync_start = (vsync & 0xffff) + 1;
10961 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
10962
10963 drm_mode_set_name(mode);
79e53945 10964
3f36b937
TU
10965 kfree(pipe_config);
10966
79e53945
JB
10967 return mode;
10968}
10969
7d993739 10970void intel_mark_busy(struct drm_i915_private *dev_priv)
f047e395 10971{
f62a0076
CW
10972 if (dev_priv->mm.busy)
10973 return;
10974
43694d69 10975 intel_runtime_pm_get(dev_priv);
c67a470b 10976 i915_update_gfx_val(dev_priv);
7d993739 10977 if (INTEL_GEN(dev_priv) >= 6)
43cf3bf0 10978 gen6_rps_busy(dev_priv);
f62a0076 10979 dev_priv->mm.busy = true;
f047e395
CW
10980}
10981
7d993739 10982void intel_mark_idle(struct drm_i915_private *dev_priv)
652c393a 10983{
f62a0076
CW
10984 if (!dev_priv->mm.busy)
10985 return;
10986
10987 dev_priv->mm.busy = false;
10988
7d993739
TU
10989 if (INTEL_GEN(dev_priv) >= 6)
10990 gen6_rps_idle(dev_priv);
bb4cdd53 10991
43694d69 10992 intel_runtime_pm_put(dev_priv);
652c393a
JB
10993}
10994
79e53945
JB
10995static void intel_crtc_destroy(struct drm_crtc *crtc)
10996{
10997 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
67e77c5a 10998 struct drm_device *dev = crtc->dev;
51cbaf01 10999 struct intel_flip_work *work;
67e77c5a 11000
5e2d7afc 11001 spin_lock_irq(&dev->event_lock);
5a21b665
DV
11002 work = intel_crtc->flip_work;
11003 intel_crtc->flip_work = NULL;
11004 spin_unlock_irq(&dev->event_lock);
67e77c5a 11005
5a21b665 11006 if (work) {
51cbaf01
ML
11007 cancel_work_sync(&work->mmio_work);
11008 cancel_work_sync(&work->unpin_work);
5a21b665 11009 kfree(work);
67e77c5a 11010 }
79e53945
JB
11011
11012 drm_crtc_cleanup(crtc);
67e77c5a 11013
79e53945
JB
11014 kfree(intel_crtc);
11015}
11016
6b95a207
KH
11017static void intel_unpin_work_fn(struct work_struct *__work)
11018{
51cbaf01
ML
11019 struct intel_flip_work *work =
11020 container_of(__work, struct intel_flip_work, unpin_work);
5a21b665
DV
11021 struct intel_crtc *crtc = to_intel_crtc(work->crtc);
11022 struct drm_device *dev = crtc->base.dev;
11023 struct drm_plane *primary = crtc->base.primary;
03f476e1 11024
5a21b665
DV
11025 if (is_mmio_work(work))
11026 flush_work(&work->mmio_work);
03f476e1 11027
5a21b665
DV
11028 mutex_lock(&dev->struct_mutex);
11029 intel_unpin_fb_obj(work->old_fb, primary->state->rotation);
11030 drm_gem_object_unreference(&work->pending_flip_obj->base);
143f73b3 11031
5a21b665
DV
11032 if (work->flip_queued_req)
11033 i915_gem_request_assign(&work->flip_queued_req, NULL);
11034 mutex_unlock(&dev->struct_mutex);
143f73b3 11035
5a21b665
DV
11036 intel_frontbuffer_flip_complete(dev, to_intel_plane(primary)->frontbuffer_bit);
11037 intel_fbc_post_update(crtc);
11038 drm_framebuffer_unreference(work->old_fb);
143f73b3 11039
5a21b665
DV
11040 BUG_ON(atomic_read(&crtc->unpin_work_count) == 0);
11041 atomic_dec(&crtc->unpin_work_count);
a6747b73 11042
5a21b665
DV
11043 kfree(work);
11044}
d9e86c0e 11045
5a21b665
DV
11046/* Is 'a' after or equal to 'b'? */
11047static bool g4x_flip_count_after_eq(u32 a, u32 b)
11048{
11049 return !((a - b) & 0x80000000);
11050}
143f73b3 11051
5a21b665
DV
11052static bool __pageflip_finished_cs(struct intel_crtc *crtc,
11053 struct intel_flip_work *work)
11054{
11055 struct drm_device *dev = crtc->base.dev;
11056 struct drm_i915_private *dev_priv = dev->dev_private;
11057 unsigned reset_counter;
143f73b3 11058
5a21b665
DV
11059 reset_counter = i915_reset_counter(&dev_priv->gpu_error);
11060 if (crtc->reset_counter != reset_counter)
11061 return true;
143f73b3 11062
5a21b665
DV
11063 /*
11064 * The relevant registers doen't exist on pre-ctg.
11065 * As the flip done interrupt doesn't trigger for mmio
11066 * flips on gmch platforms, a flip count check isn't
11067 * really needed there. But since ctg has the registers,
11068 * include it in the check anyway.
11069 */
11070 if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
11071 return true;
b4a98e57 11072
5a21b665
DV
11073 /*
11074 * BDW signals flip done immediately if the plane
11075 * is disabled, even if the plane enable is already
11076 * armed to occur at the next vblank :(
11077 */
f99d7069 11078
5a21b665
DV
11079 /*
11080 * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
11081 * used the same base address. In that case the mmio flip might
11082 * have completed, but the CS hasn't even executed the flip yet.
11083 *
11084 * A flip count check isn't enough as the CS might have updated
11085 * the base address just after start of vblank, but before we
11086 * managed to process the interrupt. This means we'd complete the
11087 * CS flip too soon.
11088 *
11089 * Combining both checks should get us a good enough result. It may
11090 * still happen that the CS flip has been executed, but has not
11091 * yet actually completed. But in case the base address is the same
11092 * anyway, we don't really care.
11093 */
11094 return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
11095 crtc->flip_work->gtt_offset &&
11096 g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_G4X(crtc->pipe)),
11097 crtc->flip_work->flip_count);
11098}
b4a98e57 11099
5a21b665
DV
11100static bool
11101__pageflip_finished_mmio(struct intel_crtc *crtc,
11102 struct intel_flip_work *work)
11103{
11104 /*
11105 * MMIO work completes when vblank is different from
11106 * flip_queued_vblank.
11107 *
11108 * Reset counter value doesn't matter, this is handled by
11109 * i915_wait_request finishing early, so no need to handle
11110 * reset here.
11111 */
11112 return intel_crtc_get_vblank_counter(crtc) != work->flip_queued_vblank;
6b95a207
KH
11113}
11114
51cbaf01
ML
11115
11116static bool pageflip_finished(struct intel_crtc *crtc,
11117 struct intel_flip_work *work)
11118{
11119 if (!atomic_read(&work->pending))
11120 return false;
11121
11122 smp_rmb();
11123
5a21b665
DV
11124 if (is_mmio_work(work))
11125 return __pageflip_finished_mmio(crtc, work);
11126 else
11127 return __pageflip_finished_cs(crtc, work);
11128}
11129
11130void intel_finish_page_flip_cs(struct drm_i915_private *dev_priv, int pipe)
11131{
11132 struct drm_device *dev = dev_priv->dev;
11133 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
11134 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11135 struct intel_flip_work *work;
11136 unsigned long flags;
11137
11138 /* Ignore early vblank irqs */
11139 if (!crtc)
11140 return;
11141
51cbaf01 11142 /*
5a21b665
DV
11143 * This is called both by irq handlers and the reset code (to complete
11144 * lost pageflips) so needs the full irqsave spinlocks.
51cbaf01 11145 */
5a21b665
DV
11146 spin_lock_irqsave(&dev->event_lock, flags);
11147 work = intel_crtc->flip_work;
11148
11149 if (work != NULL &&
11150 !is_mmio_work(work) &&
11151 pageflip_finished(intel_crtc, work))
11152 page_flip_completed(intel_crtc);
11153
11154 spin_unlock_irqrestore(&dev->event_lock, flags);
75f7f3ec
VS
11155}
11156
51cbaf01 11157void intel_finish_page_flip_mmio(struct drm_i915_private *dev_priv, int pipe)
6b95a207 11158{
91d14251 11159 struct drm_device *dev = dev_priv->dev;
5251f04e
ML
11160 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
11161 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
51cbaf01 11162 struct intel_flip_work *work;
6b95a207
KH
11163 unsigned long flags;
11164
5251f04e
ML
11165 /* Ignore early vblank irqs */
11166 if (!crtc)
11167 return;
f326038a
DV
11168
11169 /*
11170 * This is called both by irq handlers and the reset code (to complete
11171 * lost pageflips) so needs the full irqsave spinlocks.
e7d841ca 11172 */
6b95a207 11173 spin_lock_irqsave(&dev->event_lock, flags);
5a21b665 11174 work = intel_crtc->flip_work;
5251f04e 11175
5a21b665
DV
11176 if (work != NULL &&
11177 is_mmio_work(work) &&
11178 pageflip_finished(intel_crtc, work))
11179 page_flip_completed(intel_crtc);
5251f04e 11180
6b95a207
KH
11181 spin_unlock_irqrestore(&dev->event_lock, flags);
11182}
11183
5a21b665
DV
11184static inline void intel_mark_page_flip_active(struct intel_crtc *crtc,
11185 struct intel_flip_work *work)
84c33a64 11186{
5a21b665 11187 work->flip_queued_vblank = intel_crtc_get_vblank_counter(crtc);
84c33a64 11188
5a21b665
DV
11189 /* Ensure that the work item is consistent when activating it ... */
11190 smp_mb__before_atomic();
11191 atomic_set(&work->pending, 1);
11192}
a6747b73 11193
5a21b665
DV
11194static int intel_gen2_queue_flip(struct drm_device *dev,
11195 struct drm_crtc *crtc,
11196 struct drm_framebuffer *fb,
11197 struct drm_i915_gem_object *obj,
11198 struct drm_i915_gem_request *req,
11199 uint32_t flags)
11200{
11201 struct intel_engine_cs *engine = req->engine;
11202 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11203 u32 flip_mask;
11204 int ret;
143f73b3 11205
5a21b665
DV
11206 ret = intel_ring_begin(req, 6);
11207 if (ret)
11208 return ret;
143f73b3 11209
5a21b665
DV
11210 /* Can't queue multiple flips, so wait for the previous
11211 * one to finish before executing the next.
11212 */
11213 if (intel_crtc->plane)
11214 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
11215 else
11216 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
11217 intel_ring_emit(engine, MI_WAIT_FOR_EVENT | flip_mask);
11218 intel_ring_emit(engine, MI_NOOP);
11219 intel_ring_emit(engine, MI_DISPLAY_FLIP |
11220 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
11221 intel_ring_emit(engine, fb->pitches[0]);
11222 intel_ring_emit(engine, intel_crtc->flip_work->gtt_offset);
11223 intel_ring_emit(engine, 0); /* aux display base address, unused */
143f73b3 11224
5a21b665
DV
11225 return 0;
11226}
84c33a64 11227
5a21b665
DV
11228static int intel_gen3_queue_flip(struct drm_device *dev,
11229 struct drm_crtc *crtc,
11230 struct drm_framebuffer *fb,
11231 struct drm_i915_gem_object *obj,
11232 struct drm_i915_gem_request *req,
11233 uint32_t flags)
11234{
11235 struct intel_engine_cs *engine = req->engine;
11236 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11237 u32 flip_mask;
11238 int ret;
d55dbd06 11239
5a21b665
DV
11240 ret = intel_ring_begin(req, 6);
11241 if (ret)
11242 return ret;
d55dbd06 11243
5a21b665
DV
11244 if (intel_crtc->plane)
11245 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
11246 else
11247 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
11248 intel_ring_emit(engine, MI_WAIT_FOR_EVENT | flip_mask);
11249 intel_ring_emit(engine, MI_NOOP);
11250 intel_ring_emit(engine, MI_DISPLAY_FLIP_I915 |
11251 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
11252 intel_ring_emit(engine, fb->pitches[0]);
11253 intel_ring_emit(engine, intel_crtc->flip_work->gtt_offset);
11254 intel_ring_emit(engine, MI_NOOP);
fd8e058a 11255
5a21b665
DV
11256 return 0;
11257}
84c33a64 11258
5a21b665
DV
11259static int intel_gen4_queue_flip(struct drm_device *dev,
11260 struct drm_crtc *crtc,
11261 struct drm_framebuffer *fb,
11262 struct drm_i915_gem_object *obj,
11263 struct drm_i915_gem_request *req,
11264 uint32_t flags)
11265{
11266 struct intel_engine_cs *engine = req->engine;
11267 struct drm_i915_private *dev_priv = dev->dev_private;
11268 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11269 uint32_t pf, pipesrc;
11270 int ret;
143f73b3 11271
5a21b665
DV
11272 ret = intel_ring_begin(req, 4);
11273 if (ret)
11274 return ret;
143f73b3 11275
5a21b665
DV
11276 /* i965+ uses the linear or tiled offsets from the
11277 * Display Registers (which do not change across a page-flip)
11278 * so we need only reprogram the base address.
11279 */
11280 intel_ring_emit(engine, MI_DISPLAY_FLIP |
11281 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
11282 intel_ring_emit(engine, fb->pitches[0]);
11283 intel_ring_emit(engine, intel_crtc->flip_work->gtt_offset |
11284 obj->tiling_mode);
11285
11286 /* XXX Enabling the panel-fitter across page-flip is so far
11287 * untested on non-native modes, so ignore it for now.
11288 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
11289 */
11290 pf = 0;
11291 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
11292 intel_ring_emit(engine, pf | pipesrc);
143f73b3 11293
5a21b665 11294 return 0;
8c9f3aaf
JB
11295}
11296
5a21b665
DV
11297static int intel_gen6_queue_flip(struct drm_device *dev,
11298 struct drm_crtc *crtc,
11299 struct drm_framebuffer *fb,
11300 struct drm_i915_gem_object *obj,
11301 struct drm_i915_gem_request *req,
11302 uint32_t flags)
da20eabd 11303{
5a21b665
DV
11304 struct intel_engine_cs *engine = req->engine;
11305 struct drm_i915_private *dev_priv = dev->dev_private;
11306 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11307 uint32_t pf, pipesrc;
11308 int ret;
d21fbe87 11309
5a21b665
DV
11310 ret = intel_ring_begin(req, 4);
11311 if (ret)
11312 return ret;
92826fcd 11313
5a21b665
DV
11314 intel_ring_emit(engine, MI_DISPLAY_FLIP |
11315 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
11316 intel_ring_emit(engine, fb->pitches[0] | obj->tiling_mode);
11317 intel_ring_emit(engine, intel_crtc->flip_work->gtt_offset);
92826fcd 11318
5a21b665
DV
11319 /* Contrary to the suggestions in the documentation,
11320 * "Enable Panel Fitter" does not seem to be required when page
11321 * flipping with a non-native mode, and worse causes a normal
11322 * modeset to fail.
11323 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
11324 */
11325 pf = 0;
11326 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
11327 intel_ring_emit(engine, pf | pipesrc);
7809e5ae 11328
5a21b665 11329 return 0;
7809e5ae
MR
11330}
11331
5a21b665
DV
11332static int intel_gen7_queue_flip(struct drm_device *dev,
11333 struct drm_crtc *crtc,
11334 struct drm_framebuffer *fb,
11335 struct drm_i915_gem_object *obj,
11336 struct drm_i915_gem_request *req,
11337 uint32_t flags)
d21fbe87 11338{
5a21b665
DV
11339 struct intel_engine_cs *engine = req->engine;
11340 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11341 uint32_t plane_bit = 0;
11342 int len, ret;
d21fbe87 11343
5a21b665
DV
11344 switch (intel_crtc->plane) {
11345 case PLANE_A:
11346 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
11347 break;
11348 case PLANE_B:
11349 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
11350 break;
11351 case PLANE_C:
11352 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
11353 break;
11354 default:
11355 WARN_ONCE(1, "unknown plane in flip command\n");
11356 return -ENODEV;
11357 }
11358
11359 len = 4;
11360 if (engine->id == RCS) {
11361 len += 6;
11362 /*
11363 * On Gen 8, SRM is now taking an extra dword to accommodate
11364 * 48bits addresses, and we need a NOOP for the batch size to
11365 * stay even.
11366 */
11367 if (IS_GEN8(dev))
11368 len += 2;
11369 }
11370
11371 /*
11372 * BSpec MI_DISPLAY_FLIP for IVB:
11373 * "The full packet must be contained within the same cache line."
11374 *
11375 * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
11376 * cacheline, if we ever start emitting more commands before
11377 * the MI_DISPLAY_FLIP we may need to first emit everything else,
11378 * then do the cacheline alignment, and finally emit the
11379 * MI_DISPLAY_FLIP.
11380 */
11381 ret = intel_ring_cacheline_align(req);
11382 if (ret)
11383 return ret;
11384
11385 ret = intel_ring_begin(req, len);
11386 if (ret)
11387 return ret;
11388
11389 /* Unmask the flip-done completion message. Note that the bspec says that
11390 * we should do this for both the BCS and RCS, and that we must not unmask
11391 * more than one flip event at any time (or ensure that one flip message
11392 * can be sent by waiting for flip-done prior to queueing new flips).
11393 * Experimentation says that BCS works despite DERRMR masking all
11394 * flip-done completion events and that unmasking all planes at once
11395 * for the RCS also doesn't appear to drop events. Setting the DERRMR
11396 * to zero does lead to lockups within MI_DISPLAY_FLIP.
11397 */
11398 if (engine->id == RCS) {
11399 intel_ring_emit(engine, MI_LOAD_REGISTER_IMM(1));
11400 intel_ring_emit_reg(engine, DERRMR);
11401 intel_ring_emit(engine, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
11402 DERRMR_PIPEB_PRI_FLIP_DONE |
11403 DERRMR_PIPEC_PRI_FLIP_DONE));
11404 if (IS_GEN8(dev))
11405 intel_ring_emit(engine, MI_STORE_REGISTER_MEM_GEN8 |
11406 MI_SRM_LRM_GLOBAL_GTT);
11407 else
11408 intel_ring_emit(engine, MI_STORE_REGISTER_MEM |
11409 MI_SRM_LRM_GLOBAL_GTT);
11410 intel_ring_emit_reg(engine, DERRMR);
11411 intel_ring_emit(engine, engine->scratch.gtt_offset + 256);
11412 if (IS_GEN8(dev)) {
11413 intel_ring_emit(engine, 0);
11414 intel_ring_emit(engine, MI_NOOP);
11415 }
11416 }
11417
11418 intel_ring_emit(engine, MI_DISPLAY_FLIP_I915 | plane_bit);
11419 intel_ring_emit(engine, (fb->pitches[0] | obj->tiling_mode));
11420 intel_ring_emit(engine, intel_crtc->flip_work->gtt_offset);
11421 intel_ring_emit(engine, (MI_NOOP));
11422
11423 return 0;
11424}
11425
11426static bool use_mmio_flip(struct intel_engine_cs *engine,
11427 struct drm_i915_gem_object *obj)
11428{
11429 /*
11430 * This is not being used for older platforms, because
11431 * non-availability of flip done interrupt forces us to use
11432 * CS flips. Older platforms derive flip done using some clever
11433 * tricks involving the flip_pending status bits and vblank irqs.
11434 * So using MMIO flips there would disrupt this mechanism.
11435 */
11436
11437 if (engine == NULL)
11438 return true;
11439
11440 if (INTEL_GEN(engine->i915) < 5)
11441 return false;
11442
11443 if (i915.use_mmio_flip < 0)
11444 return false;
11445 else if (i915.use_mmio_flip > 0)
11446 return true;
11447 else if (i915.enable_execlists)
11448 return true;
11449 else if (obj->base.dma_buf &&
11450 !reservation_object_test_signaled_rcu(obj->base.dma_buf->resv,
11451 false))
11452 return true;
11453 else
11454 return engine != i915_gem_request_get_engine(obj->last_write_req);
11455}
11456
11457static void skl_do_mmio_flip(struct intel_crtc *intel_crtc,
11458 unsigned int rotation,
11459 struct intel_flip_work *work)
11460{
11461 struct drm_device *dev = intel_crtc->base.dev;
11462 struct drm_i915_private *dev_priv = dev->dev_private;
11463 struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
11464 const enum pipe pipe = intel_crtc->pipe;
11465 u32 ctl, stride, tile_height;
11466
11467 ctl = I915_READ(PLANE_CTL(pipe, 0));
11468 ctl &= ~PLANE_CTL_TILED_MASK;
11469 switch (fb->modifier[0]) {
11470 case DRM_FORMAT_MOD_NONE:
11471 break;
11472 case I915_FORMAT_MOD_X_TILED:
11473 ctl |= PLANE_CTL_TILED_X;
11474 break;
11475 case I915_FORMAT_MOD_Y_TILED:
11476 ctl |= PLANE_CTL_TILED_Y;
11477 break;
11478 case I915_FORMAT_MOD_Yf_TILED:
11479 ctl |= PLANE_CTL_TILED_YF;
11480 break;
11481 default:
11482 MISSING_CASE(fb->modifier[0]);
11483 }
11484
11485 /*
11486 * The stride is either expressed as a multiple of 64 bytes chunks for
11487 * linear buffers or in number of tiles for tiled buffers.
11488 */
11489 if (intel_rotation_90_or_270(rotation)) {
11490 /* stride = Surface height in tiles */
11491 tile_height = intel_tile_height(dev_priv, fb->modifier[0], 0);
11492 stride = DIV_ROUND_UP(fb->height, tile_height);
11493 } else {
11494 stride = fb->pitches[0] /
11495 intel_fb_stride_alignment(dev_priv, fb->modifier[0],
11496 fb->pixel_format);
11497 }
11498
11499 /*
11500 * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
11501 * PLANE_SURF updates, the update is then guaranteed to be atomic.
11502 */
11503 I915_WRITE(PLANE_CTL(pipe, 0), ctl);
11504 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
11505
11506 I915_WRITE(PLANE_SURF(pipe, 0), work->gtt_offset);
11507 POSTING_READ(PLANE_SURF(pipe, 0));
11508}
11509
11510static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc,
11511 struct intel_flip_work *work)
11512{
11513 struct drm_device *dev = intel_crtc->base.dev;
11514 struct drm_i915_private *dev_priv = dev->dev_private;
11515 struct intel_framebuffer *intel_fb =
11516 to_intel_framebuffer(intel_crtc->base.primary->fb);
11517 struct drm_i915_gem_object *obj = intel_fb->obj;
11518 i915_reg_t reg = DSPCNTR(intel_crtc->plane);
11519 u32 dspcntr;
11520
11521 dspcntr = I915_READ(reg);
11522
11523 if (obj->tiling_mode != I915_TILING_NONE)
11524 dspcntr |= DISPPLANE_TILED;
11525 else
11526 dspcntr &= ~DISPPLANE_TILED;
11527
11528 I915_WRITE(reg, dspcntr);
11529
11530 I915_WRITE(DSPSURF(intel_crtc->plane), work->gtt_offset);
11531 POSTING_READ(DSPSURF(intel_crtc->plane));
11532}
11533
11534static void intel_mmio_flip_work_func(struct work_struct *w)
11535{
11536 struct intel_flip_work *work =
11537 container_of(w, struct intel_flip_work, mmio_work);
11538 struct intel_crtc *crtc = to_intel_crtc(work->crtc);
11539 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
11540 struct intel_framebuffer *intel_fb =
11541 to_intel_framebuffer(crtc->base.primary->fb);
11542 struct drm_i915_gem_object *obj = intel_fb->obj;
11543
11544 if (work->flip_queued_req)
11545 WARN_ON(__i915_wait_request(work->flip_queued_req,
11546 false, NULL,
11547 &dev_priv->rps.mmioflips));
11548
11549 /* For framebuffer backed by dmabuf, wait for fence */
11550 if (obj->base.dma_buf)
11551 WARN_ON(reservation_object_wait_timeout_rcu(obj->base.dma_buf->resv,
11552 false, false,
11553 MAX_SCHEDULE_TIMEOUT) < 0);
11554
11555 intel_pipe_update_start(crtc);
11556
11557 if (INTEL_GEN(dev_priv) >= 9)
11558 skl_do_mmio_flip(crtc, work->rotation, work);
11559 else
11560 /* use_mmio_flip() retricts MMIO flips to ilk+ */
11561 ilk_do_mmio_flip(crtc, work);
11562
11563 intel_pipe_update_end(crtc, work);
11564}
11565
11566static int intel_default_queue_flip(struct drm_device *dev,
11567 struct drm_crtc *crtc,
11568 struct drm_framebuffer *fb,
11569 struct drm_i915_gem_object *obj,
11570 struct drm_i915_gem_request *req,
11571 uint32_t flags)
11572{
11573 return -ENODEV;
11574}
11575
11576static bool __pageflip_stall_check_cs(struct drm_i915_private *dev_priv,
11577 struct intel_crtc *intel_crtc,
11578 struct intel_flip_work *work)
11579{
11580 u32 addr, vblank;
11581
11582 if (!atomic_read(&work->pending))
11583 return false;
11584
11585 smp_rmb();
11586
11587 vblank = intel_crtc_get_vblank_counter(intel_crtc);
11588 if (work->flip_ready_vblank == 0) {
11589 if (work->flip_queued_req &&
11590 !i915_gem_request_completed(work->flip_queued_req, true))
11591 return false;
11592
11593 work->flip_ready_vblank = vblank;
11594 }
11595
11596 if (vblank - work->flip_ready_vblank < 3)
11597 return false;
11598
11599 /* Potential stall - if we see that the flip has happened,
11600 * assume a missed interrupt. */
11601 if (INTEL_GEN(dev_priv) >= 4)
11602 addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
11603 else
11604 addr = I915_READ(DSPADDR(intel_crtc->plane));
11605
11606 /* There is a potential issue here with a false positive after a flip
11607 * to the same address. We could address this by checking for a
11608 * non-incrementing frame counter.
11609 */
11610 return addr == work->gtt_offset;
11611}
11612
11613void intel_check_page_flip(struct drm_i915_private *dev_priv, int pipe)
11614{
11615 struct drm_device *dev = dev_priv->dev;
11616 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
11617 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11618 struct intel_flip_work *work;
11619
11620 WARN_ON(!in_interrupt());
11621
11622 if (crtc == NULL)
11623 return;
11624
11625 spin_lock(&dev->event_lock);
11626 work = intel_crtc->flip_work;
11627
11628 if (work != NULL && !is_mmio_work(work) &&
11629 __pageflip_stall_check_cs(dev_priv, intel_crtc, work)) {
11630 WARN_ONCE(1,
11631 "Kicking stuck page flip: queued at %d, now %d\n",
11632 work->flip_queued_vblank, intel_crtc_get_vblank_counter(intel_crtc));
11633 page_flip_completed(intel_crtc);
11634 work = NULL;
11635 }
11636
11637 if (work != NULL && !is_mmio_work(work) &&
11638 intel_crtc_get_vblank_counter(intel_crtc) - work->flip_queued_vblank > 1)
11639 intel_queue_rps_boost_for_request(work->flip_queued_req);
11640 spin_unlock(&dev->event_lock);
11641}
11642
11643static int intel_crtc_page_flip(struct drm_crtc *crtc,
11644 struct drm_framebuffer *fb,
11645 struct drm_pending_vblank_event *event,
11646 uint32_t page_flip_flags)
11647{
11648 struct drm_device *dev = crtc->dev;
11649 struct drm_i915_private *dev_priv = dev->dev_private;
11650 struct drm_framebuffer *old_fb = crtc->primary->fb;
11651 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
11652 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11653 struct drm_plane *primary = crtc->primary;
11654 enum pipe pipe = intel_crtc->pipe;
11655 struct intel_flip_work *work;
11656 struct intel_engine_cs *engine;
11657 bool mmio_flip;
11658 struct drm_i915_gem_request *request = NULL;
11659 int ret;
11660
11661 /*
11662 * drm_mode_page_flip_ioctl() should already catch this, but double
11663 * check to be safe. In the future we may enable pageflipping from
11664 * a disabled primary plane.
11665 */
11666 if (WARN_ON(intel_fb_obj(old_fb) == NULL))
11667 return -EBUSY;
11668
11669 /* Can't change pixel format via MI display flips. */
11670 if (fb->pixel_format != crtc->primary->fb->pixel_format)
11671 return -EINVAL;
11672
11673 /*
11674 * TILEOFF/LINOFF registers can't be changed via MI display flips.
11675 * Note that pitch changes could also affect these register.
11676 */
11677 if (INTEL_INFO(dev)->gen > 3 &&
11678 (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
11679 fb->pitches[0] != crtc->primary->fb->pitches[0]))
11680 return -EINVAL;
11681
11682 if (i915_terminally_wedged(&dev_priv->gpu_error))
11683 goto out_hang;
11684
11685 work = kzalloc(sizeof(*work), GFP_KERNEL);
11686 if (work == NULL)
11687 return -ENOMEM;
11688
11689 work->event = event;
11690 work->crtc = crtc;
11691 work->old_fb = old_fb;
11692 INIT_WORK(&work->unpin_work, intel_unpin_work_fn);
11693
11694 ret = drm_crtc_vblank_get(crtc);
11695 if (ret)
11696 goto free_work;
11697
11698 /* We borrow the event spin lock for protecting flip_work */
11699 spin_lock_irq(&dev->event_lock);
11700 if (intel_crtc->flip_work) {
11701 /* Before declaring the flip queue wedged, check if
11702 * the hardware completed the operation behind our backs.
11703 */
11704 if (pageflip_finished(intel_crtc, intel_crtc->flip_work)) {
11705 DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
11706 page_flip_completed(intel_crtc);
11707 } else {
11708 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
11709 spin_unlock_irq(&dev->event_lock);
11710
11711 drm_crtc_vblank_put(crtc);
11712 kfree(work);
11713 return -EBUSY;
11714 }
11715 }
11716 intel_crtc->flip_work = work;
11717 spin_unlock_irq(&dev->event_lock);
11718
11719 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
11720 flush_workqueue(dev_priv->wq);
11721
11722 /* Reference the objects for the scheduled work. */
11723 drm_framebuffer_reference(work->old_fb);
11724 drm_gem_object_reference(&obj->base);
11725
11726 crtc->primary->fb = fb;
11727 update_state_fb(crtc->primary);
11728 intel_fbc_pre_update(intel_crtc);
11729
11730 work->pending_flip_obj = obj;
11731
11732 ret = i915_mutex_lock_interruptible(dev);
11733 if (ret)
11734 goto cleanup;
11735
11736 intel_crtc->reset_counter = i915_reset_counter(&dev_priv->gpu_error);
11737 if (__i915_reset_in_progress_or_wedged(intel_crtc->reset_counter)) {
11738 ret = -EIO;
11739 goto cleanup;
11740 }
11741
11742 atomic_inc(&intel_crtc->unpin_work_count);
11743
11744 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
11745 work->flip_count = I915_READ(PIPE_FLIPCOUNT_G4X(pipe)) + 1;
11746
11747 if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
11748 engine = &dev_priv->engine[BCS];
11749 if (obj->tiling_mode != intel_fb_obj(work->old_fb)->tiling_mode)
11750 /* vlv: DISPLAY_FLIP fails to change tiling */
11751 engine = NULL;
11752 } else if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
11753 engine = &dev_priv->engine[BCS];
11754 } else if (INTEL_INFO(dev)->gen >= 7) {
11755 engine = i915_gem_request_get_engine(obj->last_write_req);
11756 if (engine == NULL || engine->id != RCS)
11757 engine = &dev_priv->engine[BCS];
11758 } else {
11759 engine = &dev_priv->engine[RCS];
11760 }
11761
11762 mmio_flip = use_mmio_flip(engine, obj);
11763
11764 /* When using CS flips, we want to emit semaphores between rings.
11765 * However, when using mmio flips we will create a task to do the
11766 * synchronisation, so all we want here is to pin the framebuffer
11767 * into the display plane and skip any waits.
11768 */
11769 if (!mmio_flip) {
11770 ret = i915_gem_object_sync(obj, engine, &request);
11771 if (!ret && !request) {
11772 request = i915_gem_request_alloc(engine, NULL);
11773 ret = PTR_ERR_OR_ZERO(request);
11774 }
11775
11776 if (ret)
11777 goto cleanup_pending;
11778 }
11779
11780 ret = intel_pin_and_fence_fb_obj(fb, primary->state->rotation);
11781 if (ret)
11782 goto cleanup_pending;
11783
11784 work->gtt_offset = intel_plane_obj_offset(to_intel_plane(primary),
11785 obj, 0);
11786 work->gtt_offset += intel_crtc->dspaddr_offset;
11787 work->rotation = crtc->primary->state->rotation;
11788
11789 if (mmio_flip) {
11790 INIT_WORK(&work->mmio_work, intel_mmio_flip_work_func);
11791
11792 i915_gem_request_assign(&work->flip_queued_req,
11793 obj->last_write_req);
11794
11795 schedule_work(&work->mmio_work);
11796 } else {
11797 i915_gem_request_assign(&work->flip_queued_req, request);
11798 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, request,
11799 page_flip_flags);
11800 if (ret)
11801 goto cleanup_unpin;
11802
11803 intel_mark_page_flip_active(intel_crtc, work);
11804
11805 i915_add_request_no_flush(request);
11806 }
11807
11808 i915_gem_track_fb(intel_fb_obj(old_fb), obj,
11809 to_intel_plane(primary)->frontbuffer_bit);
11810 mutex_unlock(&dev->struct_mutex);
11811
11812 intel_frontbuffer_flip_prepare(dev,
11813 to_intel_plane(primary)->frontbuffer_bit);
11814
11815 trace_i915_flip_request(intel_crtc->plane, obj);
11816
11817 return 0;
11818
11819cleanup_unpin:
11820 intel_unpin_fb_obj(fb, crtc->primary->state->rotation);
11821cleanup_pending:
11822 if (!IS_ERR_OR_NULL(request))
11823 i915_add_request_no_flush(request);
11824 atomic_dec(&intel_crtc->unpin_work_count);
11825 mutex_unlock(&dev->struct_mutex);
11826cleanup:
11827 crtc->primary->fb = old_fb;
11828 update_state_fb(crtc->primary);
11829
11830 drm_gem_object_unreference_unlocked(&obj->base);
11831 drm_framebuffer_unreference(work->old_fb);
11832
11833 spin_lock_irq(&dev->event_lock);
11834 intel_crtc->flip_work = NULL;
11835 spin_unlock_irq(&dev->event_lock);
11836
11837 drm_crtc_vblank_put(crtc);
11838free_work:
11839 kfree(work);
11840
11841 if (ret == -EIO) {
11842 struct drm_atomic_state *state;
11843 struct drm_plane_state *plane_state;
11844
11845out_hang:
11846 state = drm_atomic_state_alloc(dev);
11847 if (!state)
11848 return -ENOMEM;
11849 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
11850
11851retry:
11852 plane_state = drm_atomic_get_plane_state(state, primary);
11853 ret = PTR_ERR_OR_ZERO(plane_state);
11854 if (!ret) {
11855 drm_atomic_set_fb_for_plane(plane_state, fb);
11856
11857 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
11858 if (!ret)
11859 ret = drm_atomic_commit(state);
11860 }
11861
11862 if (ret == -EDEADLK) {
11863 drm_modeset_backoff(state->acquire_ctx);
11864 drm_atomic_state_clear(state);
11865 goto retry;
11866 }
11867
11868 if (ret)
11869 drm_atomic_state_free(state);
11870
11871 if (ret == 0 && event) {
11872 spin_lock_irq(&dev->event_lock);
11873 drm_crtc_send_vblank_event(crtc, event);
11874 spin_unlock_irq(&dev->event_lock);
11875 }
11876 }
11877 return ret;
11878}
11879
11880
11881/**
11882 * intel_wm_need_update - Check whether watermarks need updating
11883 * @plane: drm plane
11884 * @state: new plane state
11885 *
11886 * Check current plane state versus the new one to determine whether
11887 * watermarks need to be recalculated.
11888 *
11889 * Returns true or false.
11890 */
11891static bool intel_wm_need_update(struct drm_plane *plane,
11892 struct drm_plane_state *state)
11893{
11894 struct intel_plane_state *new = to_intel_plane_state(state);
11895 struct intel_plane_state *cur = to_intel_plane_state(plane->state);
11896
11897 /* Update watermarks on tiling or size changes. */
11898 if (new->visible != cur->visible)
11899 return true;
11900
11901 if (!cur->base.fb || !new->base.fb)
11902 return false;
11903
11904 if (cur->base.fb->modifier[0] != new->base.fb->modifier[0] ||
11905 cur->base.rotation != new->base.rotation ||
11906 drm_rect_width(&new->src) != drm_rect_width(&cur->src) ||
11907 drm_rect_height(&new->src) != drm_rect_height(&cur->src) ||
11908 drm_rect_width(&new->dst) != drm_rect_width(&cur->dst) ||
11909 drm_rect_height(&new->dst) != drm_rect_height(&cur->dst))
11910 return true;
11911
11912 return false;
11913}
11914
11915static bool needs_scaling(struct intel_plane_state *state)
11916{
11917 int src_w = drm_rect_width(&state->src) >> 16;
11918 int src_h = drm_rect_height(&state->src) >> 16;
11919 int dst_w = drm_rect_width(&state->dst);
11920 int dst_h = drm_rect_height(&state->dst);
11921
11922 return (src_w != dst_w || src_h != dst_h);
11923}
d21fbe87 11924
da20eabd
ML
11925int intel_plane_atomic_calc_changes(struct drm_crtc_state *crtc_state,
11926 struct drm_plane_state *plane_state)
11927{
ab1d3a0e 11928 struct intel_crtc_state *pipe_config = to_intel_crtc_state(crtc_state);
da20eabd
ML
11929 struct drm_crtc *crtc = crtc_state->crtc;
11930 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11931 struct drm_plane *plane = plane_state->plane;
11932 struct drm_device *dev = crtc->dev;
ed4a6a7c 11933 struct drm_i915_private *dev_priv = to_i915(dev);
da20eabd
ML
11934 struct intel_plane_state *old_plane_state =
11935 to_intel_plane_state(plane->state);
da20eabd
ML
11936 bool mode_changed = needs_modeset(crtc_state);
11937 bool was_crtc_enabled = crtc->state->active;
11938 bool is_crtc_enabled = crtc_state->active;
da20eabd
ML
11939 bool turn_off, turn_on, visible, was_visible;
11940 struct drm_framebuffer *fb = plane_state->fb;
78108b7c 11941 int ret;
da20eabd
ML
11942
11943 if (crtc_state && INTEL_INFO(dev)->gen >= 9 &&
11944 plane->type != DRM_PLANE_TYPE_CURSOR) {
11945 ret = skl_update_scaler_plane(
11946 to_intel_crtc_state(crtc_state),
11947 to_intel_plane_state(plane_state));
11948 if (ret)
11949 return ret;
11950 }
11951
da20eabd
ML
11952 was_visible = old_plane_state->visible;
11953 visible = to_intel_plane_state(plane_state)->visible;
11954
11955 if (!was_crtc_enabled && WARN_ON(was_visible))
11956 was_visible = false;
11957
35c08f43
ML
11958 /*
11959 * Visibility is calculated as if the crtc was on, but
11960 * after scaler setup everything depends on it being off
11961 * when the crtc isn't active.
f818ffea
VS
11962 *
11963 * FIXME this is wrong for watermarks. Watermarks should also
11964 * be computed as if the pipe would be active. Perhaps move
11965 * per-plane wm computation to the .check_plane() hook, and
11966 * only combine the results from all planes in the current place?
35c08f43
ML
11967 */
11968 if (!is_crtc_enabled)
11969 to_intel_plane_state(plane_state)->visible = visible = false;
da20eabd
ML
11970
11971 if (!was_visible && !visible)
11972 return 0;
11973
e8861675
ML
11974 if (fb != old_plane_state->base.fb)
11975 pipe_config->fb_changed = true;
11976
da20eabd
ML
11977 turn_off = was_visible && (!visible || mode_changed);
11978 turn_on = visible && (!was_visible || mode_changed);
11979
78108b7c
VS
11980 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] has [PLANE:%i] with fb %i\n",
11981 intel_crtc->base.base.id,
11982 intel_crtc->base.name,
da20eabd
ML
11983 plane->base.id, fb ? fb->base.id : -1);
11984
11985 DRM_DEBUG_ATOMIC("[PLANE:%i] visible %i -> %i, off %i, on %i, ms %i\n",
11986 plane->base.id, was_visible, visible,
11987 turn_off, turn_on, mode_changed);
11988
caed361d
VS
11989 if (turn_on) {
11990 pipe_config->update_wm_pre = true;
11991
11992 /* must disable cxsr around plane enable/disable */
11993 if (plane->type != DRM_PLANE_TYPE_CURSOR)
11994 pipe_config->disable_cxsr = true;
11995 } else if (turn_off) {
11996 pipe_config->update_wm_post = true;
92826fcd 11997
852eb00d 11998 /* must disable cxsr around plane enable/disable */
e8861675 11999 if (plane->type != DRM_PLANE_TYPE_CURSOR)
ab1d3a0e 12000 pipe_config->disable_cxsr = true;
852eb00d 12001 } else if (intel_wm_need_update(plane, plane_state)) {
caed361d
VS
12002 /* FIXME bollocks */
12003 pipe_config->update_wm_pre = true;
12004 pipe_config->update_wm_post = true;
852eb00d 12005 }
da20eabd 12006
ed4a6a7c 12007 /* Pre-gen9 platforms need two-step watermark updates */
caed361d
VS
12008 if ((pipe_config->update_wm_pre || pipe_config->update_wm_post) &&
12009 INTEL_INFO(dev)->gen < 9 && dev_priv->display.optimize_watermarks)
ed4a6a7c
MR
12010 to_intel_crtc_state(crtc_state)->wm.need_postvbl_update = true;
12011
8be6ca85 12012 if (visible || was_visible)
cd202f69 12013 pipe_config->fb_bits |= to_intel_plane(plane)->frontbuffer_bit;
a9ff8714 12014
31ae71fc
ML
12015 /*
12016 * WaCxSRDisabledForSpriteScaling:ivb
12017 *
12018 * cstate->update_wm was already set above, so this flag will
12019 * take effect when we commit and program watermarks.
12020 */
12021 if (plane->type == DRM_PLANE_TYPE_OVERLAY && IS_IVYBRIDGE(dev) &&
12022 needs_scaling(to_intel_plane_state(plane_state)) &&
12023 !needs_scaling(old_plane_state))
12024 pipe_config->disable_lp_wm = true;
d21fbe87 12025
da20eabd
ML
12026 return 0;
12027}
12028
6d3a1ce7
ML
12029static bool encoders_cloneable(const struct intel_encoder *a,
12030 const struct intel_encoder *b)
12031{
12032 /* masks could be asymmetric, so check both ways */
12033 return a == b || (a->cloneable & (1 << b->type) &&
12034 b->cloneable & (1 << a->type));
12035}
12036
12037static bool check_single_encoder_cloning(struct drm_atomic_state *state,
12038 struct intel_crtc *crtc,
12039 struct intel_encoder *encoder)
12040{
12041 struct intel_encoder *source_encoder;
12042 struct drm_connector *connector;
12043 struct drm_connector_state *connector_state;
12044 int i;
12045
12046 for_each_connector_in_state(state, connector, connector_state, i) {
12047 if (connector_state->crtc != &crtc->base)
12048 continue;
12049
12050 source_encoder =
12051 to_intel_encoder(connector_state->best_encoder);
12052 if (!encoders_cloneable(encoder, source_encoder))
12053 return false;
12054 }
12055
12056 return true;
12057}
12058
12059static bool check_encoder_cloning(struct drm_atomic_state *state,
12060 struct intel_crtc *crtc)
12061{
12062 struct intel_encoder *encoder;
12063 struct drm_connector *connector;
12064 struct drm_connector_state *connector_state;
12065 int i;
12066
12067 for_each_connector_in_state(state, connector, connector_state, i) {
12068 if (connector_state->crtc != &crtc->base)
12069 continue;
12070
12071 encoder = to_intel_encoder(connector_state->best_encoder);
12072 if (!check_single_encoder_cloning(state, crtc, encoder))
12073 return false;
12074 }
12075
12076 return true;
12077}
12078
12079static int intel_crtc_atomic_check(struct drm_crtc *crtc,
12080 struct drm_crtc_state *crtc_state)
12081{
cf5a15be 12082 struct drm_device *dev = crtc->dev;
ad421372 12083 struct drm_i915_private *dev_priv = dev->dev_private;
6d3a1ce7 12084 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
cf5a15be
ML
12085 struct intel_crtc_state *pipe_config =
12086 to_intel_crtc_state(crtc_state);
6d3a1ce7 12087 struct drm_atomic_state *state = crtc_state->state;
4d20cd86 12088 int ret;
6d3a1ce7
ML
12089 bool mode_changed = needs_modeset(crtc_state);
12090
12091 if (mode_changed && !check_encoder_cloning(state, intel_crtc)) {
12092 DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
12093 return -EINVAL;
12094 }
12095
852eb00d 12096 if (mode_changed && !crtc_state->active)
caed361d 12097 pipe_config->update_wm_post = true;
eddfcbcd 12098
ad421372
ML
12099 if (mode_changed && crtc_state->enable &&
12100 dev_priv->display.crtc_compute_clock &&
8106ddbd 12101 !WARN_ON(pipe_config->shared_dpll)) {
ad421372
ML
12102 ret = dev_priv->display.crtc_compute_clock(intel_crtc,
12103 pipe_config);
12104 if (ret)
12105 return ret;
12106 }
12107
82cf435b
LL
12108 if (crtc_state->color_mgmt_changed) {
12109 ret = intel_color_check(crtc, crtc_state);
12110 if (ret)
12111 return ret;
12112 }
12113
e435d6e5 12114 ret = 0;
86c8bbbe 12115 if (dev_priv->display.compute_pipe_wm) {
e3bddded 12116 ret = dev_priv->display.compute_pipe_wm(pipe_config);
ed4a6a7c
MR
12117 if (ret) {
12118 DRM_DEBUG_KMS("Target pipe watermarks are invalid\n");
12119 return ret;
12120 }
12121 }
12122
12123 if (dev_priv->display.compute_intermediate_wm &&
12124 !to_intel_atomic_state(state)->skip_intermediate_wm) {
12125 if (WARN_ON(!dev_priv->display.compute_pipe_wm))
12126 return 0;
12127
12128 /*
12129 * Calculate 'intermediate' watermarks that satisfy both the
12130 * old state and the new state. We can program these
12131 * immediately.
12132 */
12133 ret = dev_priv->display.compute_intermediate_wm(crtc->dev,
12134 intel_crtc,
12135 pipe_config);
12136 if (ret) {
12137 DRM_DEBUG_KMS("No valid intermediate pipe watermarks are possible\n");
86c8bbbe 12138 return ret;
ed4a6a7c 12139 }
e3d5457c
VS
12140 } else if (dev_priv->display.compute_intermediate_wm) {
12141 if (HAS_PCH_SPLIT(dev_priv) && INTEL_GEN(dev_priv) < 9)
12142 pipe_config->wm.ilk.intermediate = pipe_config->wm.ilk.optimal;
86c8bbbe
MR
12143 }
12144
e435d6e5
ML
12145 if (INTEL_INFO(dev)->gen >= 9) {
12146 if (mode_changed)
12147 ret = skl_update_scaler_crtc(pipe_config);
12148
12149 if (!ret)
12150 ret = intel_atomic_setup_scalers(dev, intel_crtc,
12151 pipe_config);
12152 }
12153
12154 return ret;
6d3a1ce7
ML
12155}
12156
65b38e0d 12157static const struct drm_crtc_helper_funcs intel_helper_funcs = {
f6e5b160 12158 .mode_set_base_atomic = intel_pipe_set_base_atomic,
5a21b665
DV
12159 .atomic_begin = intel_begin_crtc_commit,
12160 .atomic_flush = intel_finish_crtc_commit,
6d3a1ce7 12161 .atomic_check = intel_crtc_atomic_check,
f6e5b160
CW
12162};
12163
d29b2f9d
ACO
12164static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
12165{
12166 struct intel_connector *connector;
12167
12168 for_each_intel_connector(dev, connector) {
8863dc7f
DV
12169 if (connector->base.state->crtc)
12170 drm_connector_unreference(&connector->base);
12171
d29b2f9d
ACO
12172 if (connector->base.encoder) {
12173 connector->base.state->best_encoder =
12174 connector->base.encoder;
12175 connector->base.state->crtc =
12176 connector->base.encoder->crtc;
8863dc7f
DV
12177
12178 drm_connector_reference(&connector->base);
d29b2f9d
ACO
12179 } else {
12180 connector->base.state->best_encoder = NULL;
12181 connector->base.state->crtc = NULL;
12182 }
12183 }
12184}
12185
050f7aeb 12186static void
eba905b2 12187connected_sink_compute_bpp(struct intel_connector *connector,
5cec258b 12188 struct intel_crtc_state *pipe_config)
050f7aeb
DV
12189{
12190 int bpp = pipe_config->pipe_bpp;
12191
12192 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
12193 connector->base.base.id,
c23cc417 12194 connector->base.name);
050f7aeb
DV
12195
12196 /* Don't use an invalid EDID bpc value */
12197 if (connector->base.display_info.bpc &&
12198 connector->base.display_info.bpc * 3 < bpp) {
12199 DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
12200 bpp, connector->base.display_info.bpc*3);
12201 pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
12202 }
12203
013dd9e0
JN
12204 /* Clamp bpp to default limit on screens without EDID 1.4 */
12205 if (connector->base.display_info.bpc == 0) {
12206 int type = connector->base.connector_type;
12207 int clamp_bpp = 24;
12208
12209 /* Fall back to 18 bpp when DP sink capability is unknown. */
12210 if (type == DRM_MODE_CONNECTOR_DisplayPort ||
12211 type == DRM_MODE_CONNECTOR_eDP)
12212 clamp_bpp = 18;
12213
12214 if (bpp > clamp_bpp) {
12215 DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of %d\n",
12216 bpp, clamp_bpp);
12217 pipe_config->pipe_bpp = clamp_bpp;
12218 }
050f7aeb
DV
12219 }
12220}
12221
4e53c2e0 12222static int
050f7aeb 12223compute_baseline_pipe_bpp(struct intel_crtc *crtc,
5cec258b 12224 struct intel_crtc_state *pipe_config)
4e53c2e0 12225{
050f7aeb 12226 struct drm_device *dev = crtc->base.dev;
1486017f 12227 struct drm_atomic_state *state;
da3ced29
ACO
12228 struct drm_connector *connector;
12229 struct drm_connector_state *connector_state;
1486017f 12230 int bpp, i;
4e53c2e0 12231
666a4537 12232 if ((IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)))
4e53c2e0 12233 bpp = 10*3;
d328c9d7
DV
12234 else if (INTEL_INFO(dev)->gen >= 5)
12235 bpp = 12*3;
12236 else
12237 bpp = 8*3;
12238
4e53c2e0 12239
4e53c2e0
DV
12240 pipe_config->pipe_bpp = bpp;
12241
1486017f
ACO
12242 state = pipe_config->base.state;
12243
4e53c2e0 12244 /* Clamp display bpp to EDID value */
da3ced29
ACO
12245 for_each_connector_in_state(state, connector, connector_state, i) {
12246 if (connector_state->crtc != &crtc->base)
4e53c2e0
DV
12247 continue;
12248
da3ced29
ACO
12249 connected_sink_compute_bpp(to_intel_connector(connector),
12250 pipe_config);
4e53c2e0
DV
12251 }
12252
12253 return bpp;
12254}
12255
644db711
DV
12256static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
12257{
12258 DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
12259 "type: 0x%x flags: 0x%x\n",
1342830c 12260 mode->crtc_clock,
644db711
DV
12261 mode->crtc_hdisplay, mode->crtc_hsync_start,
12262 mode->crtc_hsync_end, mode->crtc_htotal,
12263 mode->crtc_vdisplay, mode->crtc_vsync_start,
12264 mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
12265}
12266
c0b03411 12267static void intel_dump_pipe_config(struct intel_crtc *crtc,
5cec258b 12268 struct intel_crtc_state *pipe_config,
c0b03411
DV
12269 const char *context)
12270{
6a60cd87
CK
12271 struct drm_device *dev = crtc->base.dev;
12272 struct drm_plane *plane;
12273 struct intel_plane *intel_plane;
12274 struct intel_plane_state *state;
12275 struct drm_framebuffer *fb;
12276
78108b7c
VS
12277 DRM_DEBUG_KMS("[CRTC:%d:%s]%s config %p for pipe %c\n",
12278 crtc->base.base.id, crtc->base.name,
6a60cd87 12279 context, pipe_config, pipe_name(crtc->pipe));
c0b03411 12280
da205630 12281 DRM_DEBUG_KMS("cpu_transcoder: %s\n", transcoder_name(pipe_config->cpu_transcoder));
c0b03411
DV
12282 DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
12283 pipe_config->pipe_bpp, pipe_config->dither);
12284 DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
12285 pipe_config->has_pch_encoder,
12286 pipe_config->fdi_lanes,
12287 pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
12288 pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
12289 pipe_config->fdi_m_n.tu);
90a6b7b0 12290 DRM_DEBUG_KMS("dp: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
eb14cb74 12291 pipe_config->has_dp_encoder,
90a6b7b0 12292 pipe_config->lane_count,
eb14cb74
VS
12293 pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
12294 pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
12295 pipe_config->dp_m_n.tu);
b95af8be 12296
90a6b7b0 12297 DRM_DEBUG_KMS("dp: %i, lanes: %i, gmch_m2: %u, gmch_n2: %u, link_m2: %u, link_n2: %u, tu2: %u\n",
b95af8be 12298 pipe_config->has_dp_encoder,
90a6b7b0 12299 pipe_config->lane_count,
b95af8be
VK
12300 pipe_config->dp_m2_n2.gmch_m,
12301 pipe_config->dp_m2_n2.gmch_n,
12302 pipe_config->dp_m2_n2.link_m,
12303 pipe_config->dp_m2_n2.link_n,
12304 pipe_config->dp_m2_n2.tu);
12305
55072d19
DV
12306 DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
12307 pipe_config->has_audio,
12308 pipe_config->has_infoframe);
12309
c0b03411 12310 DRM_DEBUG_KMS("requested mode:\n");
2d112de7 12311 drm_mode_debug_printmodeline(&pipe_config->base.mode);
c0b03411 12312 DRM_DEBUG_KMS("adjusted mode:\n");
2d112de7
ACO
12313 drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
12314 intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
d71b8d4a 12315 DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
37327abd
VS
12316 DRM_DEBUG_KMS("pipe src size: %dx%d\n",
12317 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
0ec463d3
TU
12318 DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
12319 crtc->num_scalers,
12320 pipe_config->scaler_state.scaler_users,
12321 pipe_config->scaler_state.scaler_id);
c0b03411
DV
12322 DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
12323 pipe_config->gmch_pfit.control,
12324 pipe_config->gmch_pfit.pgm_ratios,
12325 pipe_config->gmch_pfit.lvds_border_bits);
fd4daa9c 12326 DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
c0b03411 12327 pipe_config->pch_pfit.pos,
fd4daa9c
CW
12328 pipe_config->pch_pfit.size,
12329 pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
42db64ef 12330 DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
cf532bb2 12331 DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
6a60cd87 12332
415ff0f6 12333 if (IS_BROXTON(dev)) {
05712c15 12334 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: ebb0: 0x%x, ebb4: 0x%x,"
415ff0f6 12335 "pll0: 0x%x, pll1: 0x%x, pll2: 0x%x, pll3: 0x%x, "
c8453338 12336 "pll6: 0x%x, pll8: 0x%x, pll9: 0x%x, pll10: 0x%x, pcsdw12: 0x%x\n",
415ff0f6
TU
12337 pipe_config->ddi_pll_sel,
12338 pipe_config->dpll_hw_state.ebb0,
05712c15 12339 pipe_config->dpll_hw_state.ebb4,
415ff0f6
TU
12340 pipe_config->dpll_hw_state.pll0,
12341 pipe_config->dpll_hw_state.pll1,
12342 pipe_config->dpll_hw_state.pll2,
12343 pipe_config->dpll_hw_state.pll3,
12344 pipe_config->dpll_hw_state.pll6,
12345 pipe_config->dpll_hw_state.pll8,
05712c15 12346 pipe_config->dpll_hw_state.pll9,
c8453338 12347 pipe_config->dpll_hw_state.pll10,
415ff0f6 12348 pipe_config->dpll_hw_state.pcsdw12);
ef11bdb3 12349 } else if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) {
415ff0f6
TU
12350 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: "
12351 "ctrl1: 0x%x, cfgcr1: 0x%x, cfgcr2: 0x%x\n",
12352 pipe_config->ddi_pll_sel,
12353 pipe_config->dpll_hw_state.ctrl1,
12354 pipe_config->dpll_hw_state.cfgcr1,
12355 pipe_config->dpll_hw_state.cfgcr2);
12356 } else if (HAS_DDI(dev)) {
1260f07e 12357 DRM_DEBUG_KMS("ddi_pll_sel: 0x%x; dpll_hw_state: wrpll: 0x%x spll: 0x%x\n",
415ff0f6 12358 pipe_config->ddi_pll_sel,
00490c22
ML
12359 pipe_config->dpll_hw_state.wrpll,
12360 pipe_config->dpll_hw_state.spll);
415ff0f6
TU
12361 } else {
12362 DRM_DEBUG_KMS("dpll_hw_state: dpll: 0x%x, dpll_md: 0x%x, "
12363 "fp0: 0x%x, fp1: 0x%x\n",
12364 pipe_config->dpll_hw_state.dpll,
12365 pipe_config->dpll_hw_state.dpll_md,
12366 pipe_config->dpll_hw_state.fp0,
12367 pipe_config->dpll_hw_state.fp1);
12368 }
12369
6a60cd87
CK
12370 DRM_DEBUG_KMS("planes on this crtc\n");
12371 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
12372 intel_plane = to_intel_plane(plane);
12373 if (intel_plane->pipe != crtc->pipe)
12374 continue;
12375
12376 state = to_intel_plane_state(plane->state);
12377 fb = state->base.fb;
12378 if (!fb) {
12379 DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d "
12380 "disabled, scaler_id = %d\n",
12381 plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
12382 plane->base.id, intel_plane->pipe,
12383 (crtc->base.primary == plane) ? 0 : intel_plane->plane + 1,
12384 drm_plane_index(plane), state->scaler_id);
12385 continue;
12386 }
12387
12388 DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d enabled",
12389 plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
12390 plane->base.id, intel_plane->pipe,
12391 crtc->base.primary == plane ? 0 : intel_plane->plane + 1,
12392 drm_plane_index(plane));
12393 DRM_DEBUG_KMS("\tFB:%d, fb = %ux%u format = 0x%x",
12394 fb->base.id, fb->width, fb->height, fb->pixel_format);
12395 DRM_DEBUG_KMS("\tscaler:%d src (%u, %u) %ux%u dst (%u, %u) %ux%u\n",
12396 state->scaler_id,
12397 state->src.x1 >> 16, state->src.y1 >> 16,
12398 drm_rect_width(&state->src) >> 16,
12399 drm_rect_height(&state->src) >> 16,
12400 state->dst.x1, state->dst.y1,
12401 drm_rect_width(&state->dst), drm_rect_height(&state->dst));
12402 }
c0b03411
DV
12403}
12404
5448a00d 12405static bool check_digital_port_conflicts(struct drm_atomic_state *state)
00f0b378 12406{
5448a00d 12407 struct drm_device *dev = state->dev;
da3ced29 12408 struct drm_connector *connector;
00f0b378
VS
12409 unsigned int used_ports = 0;
12410
12411 /*
12412 * Walk the connector list instead of the encoder
12413 * list to detect the problem on ddi platforms
12414 * where there's just one encoder per digital port.
12415 */
0bff4858
VS
12416 drm_for_each_connector(connector, dev) {
12417 struct drm_connector_state *connector_state;
12418 struct intel_encoder *encoder;
12419
12420 connector_state = drm_atomic_get_existing_connector_state(state, connector);
12421 if (!connector_state)
12422 connector_state = connector->state;
12423
5448a00d 12424 if (!connector_state->best_encoder)
00f0b378
VS
12425 continue;
12426
5448a00d
ACO
12427 encoder = to_intel_encoder(connector_state->best_encoder);
12428
12429 WARN_ON(!connector_state->crtc);
00f0b378
VS
12430
12431 switch (encoder->type) {
12432 unsigned int port_mask;
12433 case INTEL_OUTPUT_UNKNOWN:
12434 if (WARN_ON(!HAS_DDI(dev)))
12435 break;
12436 case INTEL_OUTPUT_DISPLAYPORT:
12437 case INTEL_OUTPUT_HDMI:
12438 case INTEL_OUTPUT_EDP:
12439 port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
12440
12441 /* the same port mustn't appear more than once */
12442 if (used_ports & port_mask)
12443 return false;
12444
12445 used_ports |= port_mask;
12446 default:
12447 break;
12448 }
12449 }
12450
12451 return true;
12452}
12453
83a57153
ACO
12454static void
12455clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
12456{
12457 struct drm_crtc_state tmp_state;
663a3640 12458 struct intel_crtc_scaler_state scaler_state;
4978cc93 12459 struct intel_dpll_hw_state dpll_hw_state;
8106ddbd 12460 struct intel_shared_dpll *shared_dpll;
8504c74c 12461 uint32_t ddi_pll_sel;
c4e2d043 12462 bool force_thru;
83a57153 12463
7546a384
ACO
12464 /* FIXME: before the switch to atomic started, a new pipe_config was
12465 * kzalloc'd. Code that depends on any field being zero should be
12466 * fixed, so that the crtc_state can be safely duplicated. For now,
12467 * only fields that are know to not cause problems are preserved. */
12468
83a57153 12469 tmp_state = crtc_state->base;
663a3640 12470 scaler_state = crtc_state->scaler_state;
4978cc93
ACO
12471 shared_dpll = crtc_state->shared_dpll;
12472 dpll_hw_state = crtc_state->dpll_hw_state;
8504c74c 12473 ddi_pll_sel = crtc_state->ddi_pll_sel;
c4e2d043 12474 force_thru = crtc_state->pch_pfit.force_thru;
4978cc93 12475
83a57153 12476 memset(crtc_state, 0, sizeof *crtc_state);
4978cc93 12477
83a57153 12478 crtc_state->base = tmp_state;
663a3640 12479 crtc_state->scaler_state = scaler_state;
4978cc93
ACO
12480 crtc_state->shared_dpll = shared_dpll;
12481 crtc_state->dpll_hw_state = dpll_hw_state;
8504c74c 12482 crtc_state->ddi_pll_sel = ddi_pll_sel;
c4e2d043 12483 crtc_state->pch_pfit.force_thru = force_thru;
83a57153
ACO
12484}
12485
548ee15b 12486static int
b8cecdf5 12487intel_modeset_pipe_config(struct drm_crtc *crtc,
b359283a 12488 struct intel_crtc_state *pipe_config)
ee7b9f93 12489{
b359283a 12490 struct drm_atomic_state *state = pipe_config->base.state;
7758a113 12491 struct intel_encoder *encoder;
da3ced29 12492 struct drm_connector *connector;
0b901879 12493 struct drm_connector_state *connector_state;
d328c9d7 12494 int base_bpp, ret = -EINVAL;
0b901879 12495 int i;
e29c22c0 12496 bool retry = true;
ee7b9f93 12497
83a57153 12498 clear_intel_crtc_state(pipe_config);
7758a113 12499
e143a21c
DV
12500 pipe_config->cpu_transcoder =
12501 (enum transcoder) to_intel_crtc(crtc)->pipe;
b8cecdf5 12502
2960bc9c
ID
12503 /*
12504 * Sanitize sync polarity flags based on requested ones. If neither
12505 * positive or negative polarity is requested, treat this as meaning
12506 * negative polarity.
12507 */
2d112de7 12508 if (!(pipe_config->base.adjusted_mode.flags &
2960bc9c 12509 (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
2d112de7 12510 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
2960bc9c 12511
2d112de7 12512 if (!(pipe_config->base.adjusted_mode.flags &
2960bc9c 12513 (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
2d112de7 12514 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
2960bc9c 12515
d328c9d7
DV
12516 base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
12517 pipe_config);
12518 if (base_bpp < 0)
4e53c2e0
DV
12519 goto fail;
12520
e41a56be
VS
12521 /*
12522 * Determine the real pipe dimensions. Note that stereo modes can
12523 * increase the actual pipe size due to the frame doubling and
12524 * insertion of additional space for blanks between the frame. This
12525 * is stored in the crtc timings. We use the requested mode to do this
12526 * computation to clearly distinguish it from the adjusted mode, which
12527 * can be changed by the connectors in the below retry loop.
12528 */
2d112de7 12529 drm_crtc_get_hv_timing(&pipe_config->base.mode,
ecb7e16b
GP
12530 &pipe_config->pipe_src_w,
12531 &pipe_config->pipe_src_h);
e41a56be 12532
e29c22c0 12533encoder_retry:
ef1b460d 12534 /* Ensure the port clock defaults are reset when retrying. */
ff9a6750 12535 pipe_config->port_clock = 0;
ef1b460d 12536 pipe_config->pixel_multiplier = 1;
ff9a6750 12537
135c81b8 12538 /* Fill in default crtc timings, allow encoders to overwrite them. */
2d112de7
ACO
12539 drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
12540 CRTC_STEREO_DOUBLE);
135c81b8 12541
7758a113
DV
12542 /* Pass our mode to the connectors and the CRTC to give them a chance to
12543 * adjust it according to limitations or connector properties, and also
12544 * a chance to reject the mode entirely.
47f1c6c9 12545 */
da3ced29 12546 for_each_connector_in_state(state, connector, connector_state, i) {
0b901879 12547 if (connector_state->crtc != crtc)
7758a113 12548 continue;
7ae89233 12549
0b901879
ACO
12550 encoder = to_intel_encoder(connector_state->best_encoder);
12551
efea6e8e
DV
12552 if (!(encoder->compute_config(encoder, pipe_config))) {
12553 DRM_DEBUG_KMS("Encoder config failure\n");
7758a113
DV
12554 goto fail;
12555 }
ee7b9f93 12556 }
47f1c6c9 12557
ff9a6750
DV
12558 /* Set default port clock if not overwritten by the encoder. Needs to be
12559 * done afterwards in case the encoder adjusts the mode. */
12560 if (!pipe_config->port_clock)
2d112de7 12561 pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
241bfc38 12562 * pipe_config->pixel_multiplier;
ff9a6750 12563
a43f6e0f 12564 ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
e29c22c0 12565 if (ret < 0) {
7758a113
DV
12566 DRM_DEBUG_KMS("CRTC fixup failed\n");
12567 goto fail;
ee7b9f93 12568 }
e29c22c0
DV
12569
12570 if (ret == RETRY) {
12571 if (WARN(!retry, "loop in pipe configuration computation\n")) {
12572 ret = -EINVAL;
12573 goto fail;
12574 }
12575
12576 DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
12577 retry = false;
12578 goto encoder_retry;
12579 }
12580
e8fa4270
DV
12581 /* Dithering seems to not pass-through bits correctly when it should, so
12582 * only enable it on 6bpc panels. */
12583 pipe_config->dither = pipe_config->pipe_bpp == 6*3;
62f0ace5 12584 DRM_DEBUG_KMS("hw max bpp: %i, pipe bpp: %i, dithering: %i\n",
d328c9d7 12585 base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
4e53c2e0 12586
7758a113 12587fail:
548ee15b 12588 return ret;
ee7b9f93 12589}
47f1c6c9 12590
ea9d758d 12591static void
4740b0f2 12592intel_modeset_update_crtc_state(struct drm_atomic_state *state)
ea9d758d 12593{
0a9ab303
ACO
12594 struct drm_crtc *crtc;
12595 struct drm_crtc_state *crtc_state;
8a75d157 12596 int i;
ea9d758d 12597
7668851f 12598 /* Double check state. */
8a75d157 12599 for_each_crtc_in_state(state, crtc, crtc_state, i) {
3cb480bc 12600 to_intel_crtc(crtc)->config = to_intel_crtc_state(crtc->state);
fc467a22
ML
12601
12602 /* Update hwmode for vblank functions */
12603 if (crtc->state->active)
12604 crtc->hwmode = crtc->state->adjusted_mode;
12605 else
12606 crtc->hwmode.crtc_clock = 0;
61067a5e
ML
12607
12608 /*
12609 * Update legacy state to satisfy fbc code. This can
12610 * be removed when fbc uses the atomic state.
12611 */
12612 if (drm_atomic_get_existing_plane_state(state, crtc->primary)) {
12613 struct drm_plane_state *plane_state = crtc->primary->state;
12614
12615 crtc->primary->fb = plane_state->fb;
12616 crtc->x = plane_state->src_x >> 16;
12617 crtc->y = plane_state->src_y >> 16;
12618 }
ea9d758d 12619 }
ea9d758d
DV
12620}
12621
3bd26263 12622static bool intel_fuzzy_clock_check(int clock1, int clock2)
f1f644dc 12623{
3bd26263 12624 int diff;
f1f644dc
JB
12625
12626 if (clock1 == clock2)
12627 return true;
12628
12629 if (!clock1 || !clock2)
12630 return false;
12631
12632 diff = abs(clock1 - clock2);
12633
12634 if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
12635 return true;
12636
12637 return false;
12638}
12639
25c5b266
DV
12640#define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
12641 list_for_each_entry((intel_crtc), \
12642 &(dev)->mode_config.crtc_list, \
12643 base.head) \
95150bdf 12644 for_each_if (mask & (1 <<(intel_crtc)->pipe))
25c5b266 12645
cfb23ed6
ML
12646static bool
12647intel_compare_m_n(unsigned int m, unsigned int n,
12648 unsigned int m2, unsigned int n2,
12649 bool exact)
12650{
12651 if (m == m2 && n == n2)
12652 return true;
12653
12654 if (exact || !m || !n || !m2 || !n2)
12655 return false;
12656
12657 BUILD_BUG_ON(DATA_LINK_M_N_MASK > INT_MAX);
12658
31d10b57
ML
12659 if (n > n2) {
12660 while (n > n2) {
cfb23ed6
ML
12661 m2 <<= 1;
12662 n2 <<= 1;
12663 }
31d10b57
ML
12664 } else if (n < n2) {
12665 while (n < n2) {
cfb23ed6
ML
12666 m <<= 1;
12667 n <<= 1;
12668 }
12669 }
12670
31d10b57
ML
12671 if (n != n2)
12672 return false;
12673
12674 return intel_fuzzy_clock_check(m, m2);
cfb23ed6
ML
12675}
12676
12677static bool
12678intel_compare_link_m_n(const struct intel_link_m_n *m_n,
12679 struct intel_link_m_n *m2_n2,
12680 bool adjust)
12681{
12682 if (m_n->tu == m2_n2->tu &&
12683 intel_compare_m_n(m_n->gmch_m, m_n->gmch_n,
12684 m2_n2->gmch_m, m2_n2->gmch_n, !adjust) &&
12685 intel_compare_m_n(m_n->link_m, m_n->link_n,
12686 m2_n2->link_m, m2_n2->link_n, !adjust)) {
12687 if (adjust)
12688 *m2_n2 = *m_n;
12689
12690 return true;
12691 }
12692
12693 return false;
12694}
12695
0e8ffe1b 12696static bool
2fa2fe9a 12697intel_pipe_config_compare(struct drm_device *dev,
5cec258b 12698 struct intel_crtc_state *current_config,
cfb23ed6
ML
12699 struct intel_crtc_state *pipe_config,
12700 bool adjust)
0e8ffe1b 12701{
cfb23ed6
ML
12702 bool ret = true;
12703
12704#define INTEL_ERR_OR_DBG_KMS(fmt, ...) \
12705 do { \
12706 if (!adjust) \
12707 DRM_ERROR(fmt, ##__VA_ARGS__); \
12708 else \
12709 DRM_DEBUG_KMS(fmt, ##__VA_ARGS__); \
12710 } while (0)
12711
66e985c0
DV
12712#define PIPE_CONF_CHECK_X(name) \
12713 if (current_config->name != pipe_config->name) { \
cfb23ed6 12714 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
66e985c0
DV
12715 "(expected 0x%08x, found 0x%08x)\n", \
12716 current_config->name, \
12717 pipe_config->name); \
cfb23ed6 12718 ret = false; \
66e985c0
DV
12719 }
12720
08a24034
DV
12721#define PIPE_CONF_CHECK_I(name) \
12722 if (current_config->name != pipe_config->name) { \
cfb23ed6 12723 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
08a24034
DV
12724 "(expected %i, found %i)\n", \
12725 current_config->name, \
12726 pipe_config->name); \
cfb23ed6
ML
12727 ret = false; \
12728 }
12729
8106ddbd
ACO
12730#define PIPE_CONF_CHECK_P(name) \
12731 if (current_config->name != pipe_config->name) { \
12732 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
12733 "(expected %p, found %p)\n", \
12734 current_config->name, \
12735 pipe_config->name); \
12736 ret = false; \
12737 }
12738
cfb23ed6
ML
12739#define PIPE_CONF_CHECK_M_N(name) \
12740 if (!intel_compare_link_m_n(&current_config->name, \
12741 &pipe_config->name,\
12742 adjust)) { \
12743 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
12744 "(expected tu %i gmch %i/%i link %i/%i, " \
12745 "found tu %i, gmch %i/%i link %i/%i)\n", \
12746 current_config->name.tu, \
12747 current_config->name.gmch_m, \
12748 current_config->name.gmch_n, \
12749 current_config->name.link_m, \
12750 current_config->name.link_n, \
12751 pipe_config->name.tu, \
12752 pipe_config->name.gmch_m, \
12753 pipe_config->name.gmch_n, \
12754 pipe_config->name.link_m, \
12755 pipe_config->name.link_n); \
12756 ret = false; \
12757 }
12758
55c561a7
DV
12759/* This is required for BDW+ where there is only one set of registers for
12760 * switching between high and low RR.
12761 * This macro can be used whenever a comparison has to be made between one
12762 * hw state and multiple sw state variables.
12763 */
cfb23ed6
ML
12764#define PIPE_CONF_CHECK_M_N_ALT(name, alt_name) \
12765 if (!intel_compare_link_m_n(&current_config->name, \
12766 &pipe_config->name, adjust) && \
12767 !intel_compare_link_m_n(&current_config->alt_name, \
12768 &pipe_config->name, adjust)) { \
12769 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
12770 "(expected tu %i gmch %i/%i link %i/%i, " \
12771 "or tu %i gmch %i/%i link %i/%i, " \
12772 "found tu %i, gmch %i/%i link %i/%i)\n", \
12773 current_config->name.tu, \
12774 current_config->name.gmch_m, \
12775 current_config->name.gmch_n, \
12776 current_config->name.link_m, \
12777 current_config->name.link_n, \
12778 current_config->alt_name.tu, \
12779 current_config->alt_name.gmch_m, \
12780 current_config->alt_name.gmch_n, \
12781 current_config->alt_name.link_m, \
12782 current_config->alt_name.link_n, \
12783 pipe_config->name.tu, \
12784 pipe_config->name.gmch_m, \
12785 pipe_config->name.gmch_n, \
12786 pipe_config->name.link_m, \
12787 pipe_config->name.link_n); \
12788 ret = false; \
88adfff1
DV
12789 }
12790
1bd1bd80
DV
12791#define PIPE_CONF_CHECK_FLAGS(name, mask) \
12792 if ((current_config->name ^ pipe_config->name) & (mask)) { \
cfb23ed6 12793 INTEL_ERR_OR_DBG_KMS("mismatch in " #name "(" #mask ") " \
1bd1bd80
DV
12794 "(expected %i, found %i)\n", \
12795 current_config->name & (mask), \
12796 pipe_config->name & (mask)); \
cfb23ed6 12797 ret = false; \
1bd1bd80
DV
12798 }
12799
5e550656
VS
12800#define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
12801 if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
cfb23ed6 12802 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
5e550656
VS
12803 "(expected %i, found %i)\n", \
12804 current_config->name, \
12805 pipe_config->name); \
cfb23ed6 12806 ret = false; \
5e550656
VS
12807 }
12808
bb760063
DV
12809#define PIPE_CONF_QUIRK(quirk) \
12810 ((current_config->quirks | pipe_config->quirks) & (quirk))
12811
eccb140b
DV
12812 PIPE_CONF_CHECK_I(cpu_transcoder);
12813
08a24034
DV
12814 PIPE_CONF_CHECK_I(has_pch_encoder);
12815 PIPE_CONF_CHECK_I(fdi_lanes);
cfb23ed6 12816 PIPE_CONF_CHECK_M_N(fdi_m_n);
08a24034 12817
eb14cb74 12818 PIPE_CONF_CHECK_I(has_dp_encoder);
90a6b7b0 12819 PIPE_CONF_CHECK_I(lane_count);
b95af8be
VK
12820
12821 if (INTEL_INFO(dev)->gen < 8) {
cfb23ed6
ML
12822 PIPE_CONF_CHECK_M_N(dp_m_n);
12823
cfb23ed6
ML
12824 if (current_config->has_drrs)
12825 PIPE_CONF_CHECK_M_N(dp_m2_n2);
12826 } else
12827 PIPE_CONF_CHECK_M_N_ALT(dp_m_n, dp_m2_n2);
eb14cb74 12828
a65347ba
JN
12829 PIPE_CONF_CHECK_I(has_dsi_encoder);
12830
2d112de7
ACO
12831 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
12832 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
12833 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
12834 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
12835 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
12836 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
1bd1bd80 12837
2d112de7
ACO
12838 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
12839 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
12840 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
12841 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
12842 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
12843 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
1bd1bd80 12844
c93f54cf 12845 PIPE_CONF_CHECK_I(pixel_multiplier);
6897b4b5 12846 PIPE_CONF_CHECK_I(has_hdmi_sink);
b5a9fa09 12847 if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
666a4537 12848 IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev))
b5a9fa09 12849 PIPE_CONF_CHECK_I(limited_color_range);
e43823ec 12850 PIPE_CONF_CHECK_I(has_infoframe);
6c49f241 12851
9ed109a7
DV
12852 PIPE_CONF_CHECK_I(has_audio);
12853
2d112de7 12854 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
1bd1bd80
DV
12855 DRM_MODE_FLAG_INTERLACE);
12856
bb760063 12857 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
2d112de7 12858 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063 12859 DRM_MODE_FLAG_PHSYNC);
2d112de7 12860 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063 12861 DRM_MODE_FLAG_NHSYNC);
2d112de7 12862 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063 12863 DRM_MODE_FLAG_PVSYNC);
2d112de7 12864 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063
DV
12865 DRM_MODE_FLAG_NVSYNC);
12866 }
045ac3b5 12867
333b8ca8 12868 PIPE_CONF_CHECK_X(gmch_pfit.control);
e2ff2d4a
DV
12869 /* pfit ratios are autocomputed by the hw on gen4+ */
12870 if (INTEL_INFO(dev)->gen < 4)
7f7d8dd6 12871 PIPE_CONF_CHECK_X(gmch_pfit.pgm_ratios);
333b8ca8 12872 PIPE_CONF_CHECK_X(gmch_pfit.lvds_border_bits);
9953599b 12873
bfd16b2a
ML
12874 if (!adjust) {
12875 PIPE_CONF_CHECK_I(pipe_src_w);
12876 PIPE_CONF_CHECK_I(pipe_src_h);
12877
12878 PIPE_CONF_CHECK_I(pch_pfit.enabled);
12879 if (current_config->pch_pfit.enabled) {
12880 PIPE_CONF_CHECK_X(pch_pfit.pos);
12881 PIPE_CONF_CHECK_X(pch_pfit.size);
12882 }
2fa2fe9a 12883
7aefe2b5
ML
12884 PIPE_CONF_CHECK_I(scaler_state.scaler_id);
12885 }
a1b2278e 12886
e59150dc
JB
12887 /* BDW+ don't expose a synchronous way to read the state */
12888 if (IS_HASWELL(dev))
12889 PIPE_CONF_CHECK_I(ips_enabled);
42db64ef 12890
282740f7
VS
12891 PIPE_CONF_CHECK_I(double_wide);
12892
26804afd
DV
12893 PIPE_CONF_CHECK_X(ddi_pll_sel);
12894
8106ddbd 12895 PIPE_CONF_CHECK_P(shared_dpll);
66e985c0 12896 PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
8bcc2795 12897 PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
66e985c0
DV
12898 PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
12899 PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
d452c5b6 12900 PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
00490c22 12901 PIPE_CONF_CHECK_X(dpll_hw_state.spll);
3f4cd19f
DL
12902 PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
12903 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
12904 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
c0d43d62 12905
47eacbab
VS
12906 PIPE_CONF_CHECK_X(dsi_pll.ctrl);
12907 PIPE_CONF_CHECK_X(dsi_pll.div);
12908
42571aef
VS
12909 if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
12910 PIPE_CONF_CHECK_I(pipe_bpp);
12911
2d112de7 12912 PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
a9a7e98a 12913 PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
5e550656 12914
66e985c0 12915#undef PIPE_CONF_CHECK_X
08a24034 12916#undef PIPE_CONF_CHECK_I
8106ddbd 12917#undef PIPE_CONF_CHECK_P
1bd1bd80 12918#undef PIPE_CONF_CHECK_FLAGS
5e550656 12919#undef PIPE_CONF_CHECK_CLOCK_FUZZY
bb760063 12920#undef PIPE_CONF_QUIRK
cfb23ed6 12921#undef INTEL_ERR_OR_DBG_KMS
88adfff1 12922
cfb23ed6 12923 return ret;
0e8ffe1b
DV
12924}
12925
e3b247da
VS
12926static void intel_pipe_config_sanity_check(struct drm_i915_private *dev_priv,
12927 const struct intel_crtc_state *pipe_config)
12928{
12929 if (pipe_config->has_pch_encoder) {
21a727b3 12930 int fdi_dotclock = intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
e3b247da
VS
12931 &pipe_config->fdi_m_n);
12932 int dotclock = pipe_config->base.adjusted_mode.crtc_clock;
12933
12934 /*
12935 * FDI already provided one idea for the dotclock.
12936 * Yell if the encoder disagrees.
12937 */
12938 WARN(!intel_fuzzy_clock_check(fdi_dotclock, dotclock),
12939 "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
12940 fdi_dotclock, dotclock);
12941 }
12942}
12943
c0ead703
ML
12944static void verify_wm_state(struct drm_crtc *crtc,
12945 struct drm_crtc_state *new_state)
08db6652 12946{
e7c84544 12947 struct drm_device *dev = crtc->dev;
08db6652
DL
12948 struct drm_i915_private *dev_priv = dev->dev_private;
12949 struct skl_ddb_allocation hw_ddb, *sw_ddb;
e7c84544
ML
12950 struct skl_ddb_entry *hw_entry, *sw_entry;
12951 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
12952 const enum pipe pipe = intel_crtc->pipe;
08db6652
DL
12953 int plane;
12954
e7c84544 12955 if (INTEL_INFO(dev)->gen < 9 || !new_state->active)
08db6652
DL
12956 return;
12957
12958 skl_ddb_get_hw_state(dev_priv, &hw_ddb);
12959 sw_ddb = &dev_priv->wm.skl_hw.ddb;
12960
e7c84544
ML
12961 /* planes */
12962 for_each_plane(dev_priv, pipe, plane) {
12963 hw_entry = &hw_ddb.plane[pipe][plane];
12964 sw_entry = &sw_ddb->plane[pipe][plane];
08db6652 12965
e7c84544 12966 if (skl_ddb_entry_equal(hw_entry, sw_entry))
08db6652
DL
12967 continue;
12968
e7c84544
ML
12969 DRM_ERROR("mismatch in DDB state pipe %c plane %d "
12970 "(expected (%u,%u), found (%u,%u))\n",
12971 pipe_name(pipe), plane + 1,
12972 sw_entry->start, sw_entry->end,
12973 hw_entry->start, hw_entry->end);
12974 }
08db6652 12975
e7c84544
ML
12976 /* cursor */
12977 hw_entry = &hw_ddb.plane[pipe][PLANE_CURSOR];
12978 sw_entry = &sw_ddb->plane[pipe][PLANE_CURSOR];
08db6652 12979
e7c84544 12980 if (!skl_ddb_entry_equal(hw_entry, sw_entry)) {
08db6652
DL
12981 DRM_ERROR("mismatch in DDB state pipe %c cursor "
12982 "(expected (%u,%u), found (%u,%u))\n",
12983 pipe_name(pipe),
12984 sw_entry->start, sw_entry->end,
12985 hw_entry->start, hw_entry->end);
12986 }
12987}
12988
91d1b4bd 12989static void
c0ead703 12990verify_connector_state(struct drm_device *dev, struct drm_crtc *crtc)
8af6cf88 12991{
35dd3c64 12992 struct drm_connector *connector;
8af6cf88 12993
e7c84544 12994 drm_for_each_connector(connector, dev) {
35dd3c64
ML
12995 struct drm_encoder *encoder = connector->encoder;
12996 struct drm_connector_state *state = connector->state;
ad3c558f 12997
e7c84544
ML
12998 if (state->crtc != crtc)
12999 continue;
13000
5a21b665 13001 intel_connector_verify_state(to_intel_connector(connector));
8af6cf88 13002
ad3c558f 13003 I915_STATE_WARN(state->best_encoder != encoder,
35dd3c64 13004 "connector's atomic encoder doesn't match legacy encoder\n");
8af6cf88 13005 }
91d1b4bd
DV
13006}
13007
13008static void
c0ead703 13009verify_encoder_state(struct drm_device *dev)
91d1b4bd
DV
13010{
13011 struct intel_encoder *encoder;
13012 struct intel_connector *connector;
8af6cf88 13013
b2784e15 13014 for_each_intel_encoder(dev, encoder) {
8af6cf88 13015 bool enabled = false;
4d20cd86 13016 enum pipe pipe;
8af6cf88
DV
13017
13018 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
13019 encoder->base.base.id,
8e329a03 13020 encoder->base.name);
8af6cf88 13021
3a3371ff 13022 for_each_intel_connector(dev, connector) {
4d20cd86 13023 if (connector->base.state->best_encoder != &encoder->base)
8af6cf88
DV
13024 continue;
13025 enabled = true;
ad3c558f
ML
13026
13027 I915_STATE_WARN(connector->base.state->crtc !=
13028 encoder->base.crtc,
13029 "connector's crtc doesn't match encoder crtc\n");
8af6cf88 13030 }
0e32b39c 13031
e2c719b7 13032 I915_STATE_WARN(!!encoder->base.crtc != enabled,
8af6cf88
DV
13033 "encoder's enabled state mismatch "
13034 "(expected %i, found %i)\n",
13035 !!encoder->base.crtc, enabled);
7c60d198
ML
13036
13037 if (!encoder->base.crtc) {
4d20cd86 13038 bool active;
7c60d198 13039
4d20cd86
ML
13040 active = encoder->get_hw_state(encoder, &pipe);
13041 I915_STATE_WARN(active,
13042 "encoder detached but still enabled on pipe %c.\n",
13043 pipe_name(pipe));
7c60d198 13044 }
8af6cf88 13045 }
91d1b4bd
DV
13046}
13047
13048static void
c0ead703
ML
13049verify_crtc_state(struct drm_crtc *crtc,
13050 struct drm_crtc_state *old_crtc_state,
13051 struct drm_crtc_state *new_crtc_state)
91d1b4bd 13052{
e7c84544 13053 struct drm_device *dev = crtc->dev;
fbee40df 13054 struct drm_i915_private *dev_priv = dev->dev_private;
91d1b4bd 13055 struct intel_encoder *encoder;
e7c84544
ML
13056 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13057 struct intel_crtc_state *pipe_config, *sw_config;
13058 struct drm_atomic_state *old_state;
13059 bool active;
045ac3b5 13060
e7c84544
ML
13061 old_state = old_crtc_state->state;
13062 __drm_atomic_helper_crtc_destroy_state(crtc, old_crtc_state);
13063 pipe_config = to_intel_crtc_state(old_crtc_state);
13064 memset(pipe_config, 0, sizeof(*pipe_config));
13065 pipe_config->base.crtc = crtc;
13066 pipe_config->base.state = old_state;
8af6cf88 13067
78108b7c 13068 DRM_DEBUG_KMS("[CRTC:%d:%s]\n", crtc->base.id, crtc->name);
8af6cf88 13069
e7c84544 13070 active = dev_priv->display.get_pipe_config(intel_crtc, pipe_config);
d62cf62a 13071
e7c84544
ML
13072 /* hw state is inconsistent with the pipe quirk */
13073 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
13074 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
13075 active = new_crtc_state->active;
6c49f241 13076
e7c84544
ML
13077 I915_STATE_WARN(new_crtc_state->active != active,
13078 "crtc active state doesn't match with hw state "
13079 "(expected %i, found %i)\n", new_crtc_state->active, active);
0e8ffe1b 13080
e7c84544
ML
13081 I915_STATE_WARN(intel_crtc->active != new_crtc_state->active,
13082 "transitional active state does not match atomic hw state "
13083 "(expected %i, found %i)\n", new_crtc_state->active, intel_crtc->active);
4d20cd86 13084
e7c84544
ML
13085 for_each_encoder_on_crtc(dev, crtc, encoder) {
13086 enum pipe pipe;
4d20cd86 13087
e7c84544
ML
13088 active = encoder->get_hw_state(encoder, &pipe);
13089 I915_STATE_WARN(active != new_crtc_state->active,
13090 "[ENCODER:%i] active %i with crtc active %i\n",
13091 encoder->base.base.id, active, new_crtc_state->active);
4d20cd86 13092
e7c84544
ML
13093 I915_STATE_WARN(active && intel_crtc->pipe != pipe,
13094 "Encoder connected to wrong pipe %c\n",
13095 pipe_name(pipe));
4d20cd86 13096
e7c84544
ML
13097 if (active)
13098 encoder->get_config(encoder, pipe_config);
13099 }
53d9f4e9 13100
e7c84544
ML
13101 if (!new_crtc_state->active)
13102 return;
cfb23ed6 13103
e7c84544 13104 intel_pipe_config_sanity_check(dev_priv, pipe_config);
e3b247da 13105
e7c84544
ML
13106 sw_config = to_intel_crtc_state(crtc->state);
13107 if (!intel_pipe_config_compare(dev, sw_config,
13108 pipe_config, false)) {
13109 I915_STATE_WARN(1, "pipe state doesn't match!\n");
13110 intel_dump_pipe_config(intel_crtc, pipe_config,
13111 "[hw state]");
13112 intel_dump_pipe_config(intel_crtc, sw_config,
13113 "[sw state]");
8af6cf88
DV
13114 }
13115}
13116
91d1b4bd 13117static void
c0ead703
ML
13118verify_single_dpll_state(struct drm_i915_private *dev_priv,
13119 struct intel_shared_dpll *pll,
13120 struct drm_crtc *crtc,
13121 struct drm_crtc_state *new_state)
91d1b4bd 13122{
91d1b4bd 13123 struct intel_dpll_hw_state dpll_hw_state;
e7c84544
ML
13124 unsigned crtc_mask;
13125 bool active;
5358901f 13126
e7c84544 13127 memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
5358901f 13128
e7c84544 13129 DRM_DEBUG_KMS("%s\n", pll->name);
5358901f 13130
e7c84544 13131 active = pll->funcs.get_hw_state(dev_priv, pll, &dpll_hw_state);
5358901f 13132
e7c84544
ML
13133 if (!(pll->flags & INTEL_DPLL_ALWAYS_ON)) {
13134 I915_STATE_WARN(!pll->on && pll->active_mask,
13135 "pll in active use but not on in sw tracking\n");
13136 I915_STATE_WARN(pll->on && !pll->active_mask,
13137 "pll is on but not used by any active crtc\n");
13138 I915_STATE_WARN(pll->on != active,
13139 "pll on state mismatch (expected %i, found %i)\n",
13140 pll->on, active);
13141 }
5358901f 13142
e7c84544 13143 if (!crtc) {
2dd66ebd 13144 I915_STATE_WARN(pll->active_mask & ~pll->config.crtc_mask,
e7c84544
ML
13145 "more active pll users than references: %x vs %x\n",
13146 pll->active_mask, pll->config.crtc_mask);
5358901f 13147
e7c84544
ML
13148 return;
13149 }
13150
13151 crtc_mask = 1 << drm_crtc_index(crtc);
13152
13153 if (new_state->active)
13154 I915_STATE_WARN(!(pll->active_mask & crtc_mask),
13155 "pll active mismatch (expected pipe %c in active mask 0x%02x)\n",
13156 pipe_name(drm_crtc_index(crtc)), pll->active_mask);
13157 else
13158 I915_STATE_WARN(pll->active_mask & crtc_mask,
13159 "pll active mismatch (didn't expect pipe %c in active mask 0x%02x)\n",
13160 pipe_name(drm_crtc_index(crtc)), pll->active_mask);
2dd66ebd 13161
e7c84544
ML
13162 I915_STATE_WARN(!(pll->config.crtc_mask & crtc_mask),
13163 "pll enabled crtcs mismatch (expected 0x%x in 0x%02x)\n",
13164 crtc_mask, pll->config.crtc_mask);
66e985c0 13165
e7c84544
ML
13166 I915_STATE_WARN(pll->on && memcmp(&pll->config.hw_state,
13167 &dpll_hw_state,
13168 sizeof(dpll_hw_state)),
13169 "pll hw state mismatch\n");
13170}
13171
13172static void
c0ead703
ML
13173verify_shared_dpll_state(struct drm_device *dev, struct drm_crtc *crtc,
13174 struct drm_crtc_state *old_crtc_state,
13175 struct drm_crtc_state *new_crtc_state)
e7c84544
ML
13176{
13177 struct drm_i915_private *dev_priv = dev->dev_private;
13178 struct intel_crtc_state *old_state = to_intel_crtc_state(old_crtc_state);
13179 struct intel_crtc_state *new_state = to_intel_crtc_state(new_crtc_state);
13180
13181 if (new_state->shared_dpll)
c0ead703 13182 verify_single_dpll_state(dev_priv, new_state->shared_dpll, crtc, new_crtc_state);
e7c84544
ML
13183
13184 if (old_state->shared_dpll &&
13185 old_state->shared_dpll != new_state->shared_dpll) {
13186 unsigned crtc_mask = 1 << drm_crtc_index(crtc);
13187 struct intel_shared_dpll *pll = old_state->shared_dpll;
13188
13189 I915_STATE_WARN(pll->active_mask & crtc_mask,
13190 "pll active mismatch (didn't expect pipe %c in active mask)\n",
13191 pipe_name(drm_crtc_index(crtc)));
13192 I915_STATE_WARN(pll->config.crtc_mask & crtc_mask,
13193 "pll enabled crtcs mismatch (found %x in enabled mask)\n",
13194 pipe_name(drm_crtc_index(crtc)));
5358901f 13195 }
8af6cf88
DV
13196}
13197
e7c84544 13198static void
c0ead703 13199intel_modeset_verify_crtc(struct drm_crtc *crtc,
e7c84544
ML
13200 struct drm_crtc_state *old_state,
13201 struct drm_crtc_state *new_state)
13202{
5a21b665
DV
13203 if (!needs_modeset(new_state) &&
13204 !to_intel_crtc_state(new_state)->update_pipe)
13205 return;
13206
c0ead703 13207 verify_wm_state(crtc, new_state);
5a21b665 13208 verify_connector_state(crtc->dev, crtc);
c0ead703
ML
13209 verify_crtc_state(crtc, old_state, new_state);
13210 verify_shared_dpll_state(crtc->dev, crtc, old_state, new_state);
e7c84544
ML
13211}
13212
13213static void
c0ead703 13214verify_disabled_dpll_state(struct drm_device *dev)
e7c84544
ML
13215{
13216 struct drm_i915_private *dev_priv = dev->dev_private;
13217 int i;
13218
13219 for (i = 0; i < dev_priv->num_shared_dpll; i++)
c0ead703 13220 verify_single_dpll_state(dev_priv, &dev_priv->shared_dplls[i], NULL, NULL);
e7c84544
ML
13221}
13222
13223static void
c0ead703 13224intel_modeset_verify_disabled(struct drm_device *dev)
e7c84544 13225{
c0ead703
ML
13226 verify_encoder_state(dev);
13227 verify_connector_state(dev, NULL);
13228 verify_disabled_dpll_state(dev);
e7c84544
ML
13229}
13230
80715b2f
VS
13231static void update_scanline_offset(struct intel_crtc *crtc)
13232{
13233 struct drm_device *dev = crtc->base.dev;
13234
13235 /*
13236 * The scanline counter increments at the leading edge of hsync.
13237 *
13238 * On most platforms it starts counting from vtotal-1 on the
13239 * first active line. That means the scanline counter value is
13240 * always one less than what we would expect. Ie. just after
13241 * start of vblank, which also occurs at start of hsync (on the
13242 * last active line), the scanline counter will read vblank_start-1.
13243 *
13244 * On gen2 the scanline counter starts counting from 1 instead
13245 * of vtotal-1, so we have to subtract one (or rather add vtotal-1
13246 * to keep the value positive), instead of adding one.
13247 *
13248 * On HSW+ the behaviour of the scanline counter depends on the output
13249 * type. For DP ports it behaves like most other platforms, but on HDMI
13250 * there's an extra 1 line difference. So we need to add two instead of
13251 * one to the value.
13252 */
13253 if (IS_GEN2(dev)) {
124abe07 13254 const struct drm_display_mode *adjusted_mode = &crtc->config->base.adjusted_mode;
80715b2f
VS
13255 int vtotal;
13256
124abe07
VS
13257 vtotal = adjusted_mode->crtc_vtotal;
13258 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
80715b2f
VS
13259 vtotal /= 2;
13260
13261 crtc->scanline_offset = vtotal - 1;
13262 } else if (HAS_DDI(dev) &&
409ee761 13263 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI)) {
80715b2f
VS
13264 crtc->scanline_offset = 2;
13265 } else
13266 crtc->scanline_offset = 1;
13267}
13268
ad421372 13269static void intel_modeset_clear_plls(struct drm_atomic_state *state)
ed6739ef 13270{
225da59b 13271 struct drm_device *dev = state->dev;
ed6739ef 13272 struct drm_i915_private *dev_priv = to_i915(dev);
ad421372 13273 struct intel_shared_dpll_config *shared_dpll = NULL;
0a9ab303
ACO
13274 struct drm_crtc *crtc;
13275 struct drm_crtc_state *crtc_state;
0a9ab303 13276 int i;
ed6739ef
ACO
13277
13278 if (!dev_priv->display.crtc_compute_clock)
ad421372 13279 return;
ed6739ef 13280
0a9ab303 13281 for_each_crtc_in_state(state, crtc, crtc_state, i) {
fb1a38a9 13282 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8106ddbd
ACO
13283 struct intel_shared_dpll *old_dpll =
13284 to_intel_crtc_state(crtc->state)->shared_dpll;
0a9ab303 13285
fb1a38a9 13286 if (!needs_modeset(crtc_state))
225da59b
ACO
13287 continue;
13288
8106ddbd 13289 to_intel_crtc_state(crtc_state)->shared_dpll = NULL;
fb1a38a9 13290
8106ddbd 13291 if (!old_dpll)
fb1a38a9 13292 continue;
0a9ab303 13293
ad421372
ML
13294 if (!shared_dpll)
13295 shared_dpll = intel_atomic_get_shared_dpll_state(state);
ed6739ef 13296
8106ddbd 13297 intel_shared_dpll_config_put(shared_dpll, old_dpll, intel_crtc);
ad421372 13298 }
ed6739ef
ACO
13299}
13300
99d736a2
ML
13301/*
13302 * This implements the workaround described in the "notes" section of the mode
13303 * set sequence documentation. When going from no pipes or single pipe to
13304 * multiple pipes, and planes are enabled after the pipe, we need to wait at
13305 * least 2 vblanks on the first pipe before enabling planes on the second pipe.
13306 */
13307static int haswell_mode_set_planes_workaround(struct drm_atomic_state *state)
13308{
13309 struct drm_crtc_state *crtc_state;
13310 struct intel_crtc *intel_crtc;
13311 struct drm_crtc *crtc;
13312 struct intel_crtc_state *first_crtc_state = NULL;
13313 struct intel_crtc_state *other_crtc_state = NULL;
13314 enum pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
13315 int i;
13316
13317 /* look at all crtc's that are going to be enabled in during modeset */
13318 for_each_crtc_in_state(state, crtc, crtc_state, i) {
13319 intel_crtc = to_intel_crtc(crtc);
13320
13321 if (!crtc_state->active || !needs_modeset(crtc_state))
13322 continue;
13323
13324 if (first_crtc_state) {
13325 other_crtc_state = to_intel_crtc_state(crtc_state);
13326 break;
13327 } else {
13328 first_crtc_state = to_intel_crtc_state(crtc_state);
13329 first_pipe = intel_crtc->pipe;
13330 }
13331 }
13332
13333 /* No workaround needed? */
13334 if (!first_crtc_state)
13335 return 0;
13336
13337 /* w/a possibly needed, check how many crtc's are already enabled. */
13338 for_each_intel_crtc(state->dev, intel_crtc) {
13339 struct intel_crtc_state *pipe_config;
13340
13341 pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
13342 if (IS_ERR(pipe_config))
13343 return PTR_ERR(pipe_config);
13344
13345 pipe_config->hsw_workaround_pipe = INVALID_PIPE;
13346
13347 if (!pipe_config->base.active ||
13348 needs_modeset(&pipe_config->base))
13349 continue;
13350
13351 /* 2 or more enabled crtcs means no need for w/a */
13352 if (enabled_pipe != INVALID_PIPE)
13353 return 0;
13354
13355 enabled_pipe = intel_crtc->pipe;
13356 }
13357
13358 if (enabled_pipe != INVALID_PIPE)
13359 first_crtc_state->hsw_workaround_pipe = enabled_pipe;
13360 else if (other_crtc_state)
13361 other_crtc_state->hsw_workaround_pipe = first_pipe;
13362
13363 return 0;
13364}
13365
27c329ed
ML
13366static int intel_modeset_all_pipes(struct drm_atomic_state *state)
13367{
13368 struct drm_crtc *crtc;
13369 struct drm_crtc_state *crtc_state;
13370 int ret = 0;
13371
13372 /* add all active pipes to the state */
13373 for_each_crtc(state->dev, crtc) {
13374 crtc_state = drm_atomic_get_crtc_state(state, crtc);
13375 if (IS_ERR(crtc_state))
13376 return PTR_ERR(crtc_state);
13377
13378 if (!crtc_state->active || needs_modeset(crtc_state))
13379 continue;
13380
13381 crtc_state->mode_changed = true;
13382
13383 ret = drm_atomic_add_affected_connectors(state, crtc);
13384 if (ret)
13385 break;
13386
13387 ret = drm_atomic_add_affected_planes(state, crtc);
13388 if (ret)
13389 break;
13390 }
13391
13392 return ret;
13393}
13394
c347a676 13395static int intel_modeset_checks(struct drm_atomic_state *state)
054518dd 13396{
565602d7
ML
13397 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
13398 struct drm_i915_private *dev_priv = state->dev->dev_private;
13399 struct drm_crtc *crtc;
13400 struct drm_crtc_state *crtc_state;
13401 int ret = 0, i;
054518dd 13402
b359283a
ML
13403 if (!check_digital_port_conflicts(state)) {
13404 DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
13405 return -EINVAL;
13406 }
13407
565602d7
ML
13408 intel_state->modeset = true;
13409 intel_state->active_crtcs = dev_priv->active_crtcs;
13410
13411 for_each_crtc_in_state(state, crtc, crtc_state, i) {
13412 if (crtc_state->active)
13413 intel_state->active_crtcs |= 1 << i;
13414 else
13415 intel_state->active_crtcs &= ~(1 << i);
8b4a7d05
MR
13416
13417 if (crtc_state->active != crtc->state->active)
13418 intel_state->active_pipe_changes |= drm_crtc_mask(crtc);
565602d7
ML
13419 }
13420
054518dd
ACO
13421 /*
13422 * See if the config requires any additional preparation, e.g.
13423 * to adjust global state with pipes off. We need to do this
13424 * here so we can get the modeset_pipe updated config for the new
13425 * mode set on this crtc. For other crtcs we need to use the
13426 * adjusted_mode bits in the crtc directly.
13427 */
27c329ed 13428 if (dev_priv->display.modeset_calc_cdclk) {
c89e39f3 13429 if (!intel_state->cdclk_pll_vco)
63911d72 13430 intel_state->cdclk_pll_vco = dev_priv->cdclk_pll.vco;
b2045352
VS
13431 if (!intel_state->cdclk_pll_vco)
13432 intel_state->cdclk_pll_vco = dev_priv->skl_preferred_vco_freq;
c89e39f3 13433
27c329ed 13434 ret = dev_priv->display.modeset_calc_cdclk(state);
c89e39f3
CT
13435 if (ret < 0)
13436 return ret;
27c329ed 13437
c89e39f3 13438 if (intel_state->dev_cdclk != dev_priv->cdclk_freq ||
63911d72 13439 intel_state->cdclk_pll_vco != dev_priv->cdclk_pll.vco)
27c329ed
ML
13440 ret = intel_modeset_all_pipes(state);
13441
13442 if (ret < 0)
054518dd 13443 return ret;
e8788cbc
ML
13444
13445 DRM_DEBUG_KMS("New cdclk calculated to be atomic %u, actual %u\n",
13446 intel_state->cdclk, intel_state->dev_cdclk);
27c329ed 13447 } else
1a617b77 13448 to_intel_atomic_state(state)->cdclk = dev_priv->atomic_cdclk_freq;
054518dd 13449
ad421372 13450 intel_modeset_clear_plls(state);
054518dd 13451
565602d7 13452 if (IS_HASWELL(dev_priv))
ad421372 13453 return haswell_mode_set_planes_workaround(state);
99d736a2 13454
ad421372 13455 return 0;
c347a676
ACO
13456}
13457
aa363136
MR
13458/*
13459 * Handle calculation of various watermark data at the end of the atomic check
13460 * phase. The code here should be run after the per-crtc and per-plane 'check'
13461 * handlers to ensure that all derived state has been updated.
13462 */
55994c2c 13463static int calc_watermark_data(struct drm_atomic_state *state)
aa363136
MR
13464{
13465 struct drm_device *dev = state->dev;
98d39494 13466 struct drm_i915_private *dev_priv = to_i915(dev);
98d39494
MR
13467
13468 /* Is there platform-specific watermark information to calculate? */
13469 if (dev_priv->display.compute_global_watermarks)
55994c2c
MR
13470 return dev_priv->display.compute_global_watermarks(state);
13471
13472 return 0;
aa363136
MR
13473}
13474
74c090b1
ML
13475/**
13476 * intel_atomic_check - validate state object
13477 * @dev: drm device
13478 * @state: state to validate
13479 */
13480static int intel_atomic_check(struct drm_device *dev,
13481 struct drm_atomic_state *state)
c347a676 13482{
dd8b3bdb 13483 struct drm_i915_private *dev_priv = to_i915(dev);
aa363136 13484 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
c347a676
ACO
13485 struct drm_crtc *crtc;
13486 struct drm_crtc_state *crtc_state;
13487 int ret, i;
61333b60 13488 bool any_ms = false;
c347a676 13489
74c090b1 13490 ret = drm_atomic_helper_check_modeset(dev, state);
054518dd
ACO
13491 if (ret)
13492 return ret;
13493
c347a676 13494 for_each_crtc_in_state(state, crtc, crtc_state, i) {
cfb23ed6
ML
13495 struct intel_crtc_state *pipe_config =
13496 to_intel_crtc_state(crtc_state);
1ed51de9
DV
13497
13498 /* Catch I915_MODE_FLAG_INHERITED */
13499 if (crtc_state->mode.private_flags != crtc->state->mode.private_flags)
13500 crtc_state->mode_changed = true;
cfb23ed6 13501
af4a879e 13502 if (!needs_modeset(crtc_state))
c347a676
ACO
13503 continue;
13504
af4a879e
DV
13505 if (!crtc_state->enable) {
13506 any_ms = true;
cfb23ed6 13507 continue;
af4a879e 13508 }
cfb23ed6 13509
26495481
DV
13510 /* FIXME: For only active_changed we shouldn't need to do any
13511 * state recomputation at all. */
13512
1ed51de9
DV
13513 ret = drm_atomic_add_affected_connectors(state, crtc);
13514 if (ret)
13515 return ret;
b359283a 13516
cfb23ed6 13517 ret = intel_modeset_pipe_config(crtc, pipe_config);
25aa1c39
ML
13518 if (ret) {
13519 intel_dump_pipe_config(to_intel_crtc(crtc),
13520 pipe_config, "[failed]");
c347a676 13521 return ret;
25aa1c39 13522 }
c347a676 13523
73831236 13524 if (i915.fastboot &&
dd8b3bdb 13525 intel_pipe_config_compare(dev,
cfb23ed6 13526 to_intel_crtc_state(crtc->state),
1ed51de9 13527 pipe_config, true)) {
26495481 13528 crtc_state->mode_changed = false;
bfd16b2a 13529 to_intel_crtc_state(crtc_state)->update_pipe = true;
26495481
DV
13530 }
13531
af4a879e 13532 if (needs_modeset(crtc_state))
26495481 13533 any_ms = true;
cfb23ed6 13534
af4a879e
DV
13535 ret = drm_atomic_add_affected_planes(state, crtc);
13536 if (ret)
13537 return ret;
61333b60 13538
26495481
DV
13539 intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
13540 needs_modeset(crtc_state) ?
13541 "[modeset]" : "[fastset]");
c347a676
ACO
13542 }
13543
61333b60
ML
13544 if (any_ms) {
13545 ret = intel_modeset_checks(state);
13546
13547 if (ret)
13548 return ret;
27c329ed 13549 } else
dd8b3bdb 13550 intel_state->cdclk = dev_priv->cdclk_freq;
76305b1a 13551
dd8b3bdb 13552 ret = drm_atomic_helper_check_planes(dev, state);
aa363136
MR
13553 if (ret)
13554 return ret;
13555
f51be2e0 13556 intel_fbc_choose_crtc(dev_priv, state);
55994c2c 13557 return calc_watermark_data(state);
054518dd
ACO
13558}
13559
5008e874
ML
13560static int intel_atomic_prepare_commit(struct drm_device *dev,
13561 struct drm_atomic_state *state,
81072bfd 13562 bool nonblock)
5008e874 13563{
7580d774
ML
13564 struct drm_i915_private *dev_priv = dev->dev_private;
13565 struct drm_plane_state *plane_state;
5008e874 13566 struct drm_crtc_state *crtc_state;
7580d774 13567 struct drm_plane *plane;
5008e874
ML
13568 struct drm_crtc *crtc;
13569 int i, ret;
13570
5a21b665
DV
13571 if (nonblock) {
13572 DRM_DEBUG_KMS("i915 does not yet support nonblocking commit\n");
13573 return -EINVAL;
13574 }
a6747b73 13575
5a21b665
DV
13576 for_each_crtc_in_state(state, crtc, crtc_state, i) {
13577 if (state->legacy_cursor_update)
a6747b73
ML
13578 continue;
13579
5a21b665
DV
13580 ret = intel_crtc_wait_for_pending_flips(crtc);
13581 if (ret)
13582 return ret;
5008e874 13583
5a21b665
DV
13584 if (atomic_read(&to_intel_crtc(crtc)->unpin_work_count) >= 2)
13585 flush_workqueue(dev_priv->wq);
d55dbd06
ML
13586 }
13587
f935675f
ML
13588 ret = mutex_lock_interruptible(&dev->struct_mutex);
13589 if (ret)
13590 return ret;
13591
5008e874 13592 ret = drm_atomic_helper_prepare_planes(dev, state);
f7e5838b 13593 mutex_unlock(&dev->struct_mutex);
7580d774 13594
21daaeee 13595 if (!ret && !nonblock) {
7580d774
ML
13596 for_each_plane_in_state(state, plane, plane_state, i) {
13597 struct intel_plane_state *intel_plane_state =
13598 to_intel_plane_state(plane_state);
13599
13600 if (!intel_plane_state->wait_req)
13601 continue;
13602
13603 ret = __i915_wait_request(intel_plane_state->wait_req,
299259a3 13604 true, NULL, NULL);
f7e5838b 13605 if (ret) {
f4457ae7
CW
13606 /* Any hang should be swallowed by the wait */
13607 WARN_ON(ret == -EIO);
f7e5838b
CW
13608 mutex_lock(&dev->struct_mutex);
13609 drm_atomic_helper_cleanup_planes(dev, state);
13610 mutex_unlock(&dev->struct_mutex);
7580d774 13611 break;
f7e5838b 13612 }
7580d774 13613 }
7580d774 13614 }
5008e874
ML
13615
13616 return ret;
13617}
13618
a2991414
ML
13619u32 intel_crtc_get_vblank_counter(struct intel_crtc *crtc)
13620{
13621 struct drm_device *dev = crtc->base.dev;
13622
13623 if (!dev->max_vblank_count)
13624 return drm_accurate_vblank_count(&crtc->base);
13625
13626 return dev->driver->get_vblank_counter(dev, crtc->pipe);
13627}
13628
5a21b665
DV
13629static void intel_atomic_wait_for_vblanks(struct drm_device *dev,
13630 struct drm_i915_private *dev_priv,
13631 unsigned crtc_mask)
e8861675 13632{
5a21b665
DV
13633 unsigned last_vblank_count[I915_MAX_PIPES];
13634 enum pipe pipe;
13635 int ret;
e8861675 13636
5a21b665
DV
13637 if (!crtc_mask)
13638 return;
e8861675 13639
5a21b665
DV
13640 for_each_pipe(dev_priv, pipe) {
13641 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
e8861675 13642
5a21b665 13643 if (!((1 << pipe) & crtc_mask))
e8861675
ML
13644 continue;
13645
5a21b665
DV
13646 ret = drm_crtc_vblank_get(crtc);
13647 if (WARN_ON(ret != 0)) {
13648 crtc_mask &= ~(1 << pipe);
13649 continue;
e8861675
ML
13650 }
13651
5a21b665 13652 last_vblank_count[pipe] = drm_crtc_vblank_count(crtc);
e8861675
ML
13653 }
13654
5a21b665
DV
13655 for_each_pipe(dev_priv, pipe) {
13656 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
13657 long lret;
e8861675 13658
5a21b665
DV
13659 if (!((1 << pipe) & crtc_mask))
13660 continue;
d55dbd06 13661
5a21b665
DV
13662 lret = wait_event_timeout(dev->vblank[pipe].queue,
13663 last_vblank_count[pipe] !=
13664 drm_crtc_vblank_count(crtc),
13665 msecs_to_jiffies(50));
d55dbd06 13666
5a21b665 13667 WARN(!lret, "pipe %c vblank wait timed out\n", pipe_name(pipe));
d55dbd06 13668
5a21b665 13669 drm_crtc_vblank_put(crtc);
d55dbd06
ML
13670 }
13671}
13672
5a21b665 13673static bool needs_vblank_wait(struct intel_crtc_state *crtc_state)
a6747b73 13674{
5a21b665
DV
13675 /* fb updated, need to unpin old fb */
13676 if (crtc_state->fb_changed)
13677 return true;
a6747b73 13678
5a21b665
DV
13679 /* wm changes, need vblank before final wm's */
13680 if (crtc_state->update_wm_post)
13681 return true;
a6747b73 13682
5a21b665
DV
13683 /*
13684 * cxsr is re-enabled after vblank.
13685 * This is already handled by crtc_state->update_wm_post,
13686 * but added for clarity.
13687 */
13688 if (crtc_state->disable_cxsr)
13689 return true;
a6747b73 13690
5a21b665 13691 return false;
e8861675
ML
13692}
13693
74c090b1
ML
13694/**
13695 * intel_atomic_commit - commit validated state object
13696 * @dev: DRM device
13697 * @state: the top-level driver state object
81072bfd 13698 * @nonblock: nonblocking commit
74c090b1
ML
13699 *
13700 * This function commits a top-level state object that has been validated
13701 * with drm_atomic_helper_check().
13702 *
13703 * FIXME: Atomic modeset support for i915 is not yet complete. At the moment
13704 * we can only handle plane-related operations and do not yet support
81072bfd 13705 * nonblocking commit.
74c090b1
ML
13706 *
13707 * RETURNS
13708 * Zero for success or -errno.
13709 */
13710static int intel_atomic_commit(struct drm_device *dev,
13711 struct drm_atomic_state *state,
81072bfd 13712 bool nonblock)
a6778b3c 13713{
565602d7 13714 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
fbee40df 13715 struct drm_i915_private *dev_priv = dev->dev_private;
29ceb0e6 13716 struct drm_crtc_state *old_crtc_state;
7580d774 13717 struct drm_crtc *crtc;
5a21b665 13718 struct intel_crtc_state *intel_cstate;
565602d7 13719 int ret = 0, i;
5a21b665
DV
13720 bool hw_check = intel_state->modeset;
13721 unsigned long put_domains[I915_MAX_PIPES] = {};
13722 unsigned crtc_vblank_mask = 0;
a6778b3c 13723
81072bfd 13724 ret = intel_atomic_prepare_commit(dev, state, nonblock);
7580d774
ML
13725 if (ret) {
13726 DRM_DEBUG_ATOMIC("Preparing state failed with %i\n", ret);
d4afb8cc 13727 return ret;
7580d774 13728 }
d4afb8cc 13729
1c5e19f8 13730 drm_atomic_helper_swap_state(dev, state);
279e99d7 13731 dev_priv->wm.distrust_bios_wm = false;
734fa01f 13732 dev_priv->wm.skl_results = intel_state->wm_results;
a1475e77 13733 intel_shared_dpll_commit(state);
1c5e19f8 13734
565602d7
ML
13735 if (intel_state->modeset) {
13736 memcpy(dev_priv->min_pixclk, intel_state->min_pixclk,
13737 sizeof(intel_state->min_pixclk));
13738 dev_priv->active_crtcs = intel_state->active_crtcs;
1a617b77 13739 dev_priv->atomic_cdclk_freq = intel_state->cdclk;
5a21b665
DV
13740
13741 intel_display_power_get(dev_priv, POWER_DOMAIN_MODESET);
565602d7
ML
13742 }
13743
29ceb0e6 13744 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
a539205a
ML
13745 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13746
5a21b665
DV
13747 if (needs_modeset(crtc->state) ||
13748 to_intel_crtc_state(crtc->state)->update_pipe) {
13749 hw_check = true;
13750
13751 put_domains[to_intel_crtc(crtc)->pipe] =
13752 modeset_get_crtc_power_domains(crtc,
13753 to_intel_crtc_state(crtc->state));
13754 }
13755
61333b60
ML
13756 if (!needs_modeset(crtc->state))
13757 continue;
13758
29ceb0e6 13759 intel_pre_plane_update(to_intel_crtc_state(old_crtc_state));
460da916 13760
29ceb0e6
VS
13761 if (old_crtc_state->active) {
13762 intel_crtc_disable_planes(crtc, old_crtc_state->plane_mask);
a539205a 13763 dev_priv->display.crtc_disable(crtc);
eddfcbcd 13764 intel_crtc->active = false;
58f9c0bc 13765 intel_fbc_disable(intel_crtc);
eddfcbcd 13766 intel_disable_shared_dpll(intel_crtc);
9bbc8258
VS
13767
13768 /*
13769 * Underruns don't always raise
13770 * interrupts, so check manually.
13771 */
13772 intel_check_cpu_fifo_underruns(dev_priv);
13773 intel_check_pch_fifo_underruns(dev_priv);
b9001114
ML
13774
13775 if (!crtc->state->active)
13776 intel_update_watermarks(crtc);
a539205a 13777 }
b8cecdf5 13778 }
7758a113 13779
ea9d758d
DV
13780 /* Only after disabling all output pipelines that will be changed can we
13781 * update the the output configuration. */
4740b0f2 13782 intel_modeset_update_crtc_state(state);
f6e5b160 13783
565602d7 13784 if (intel_state->modeset) {
4740b0f2 13785 drm_atomic_helper_update_legacy_modeset_state(state->dev, state);
33c8df89
ML
13786
13787 if (dev_priv->display.modeset_commit_cdclk &&
c89e39f3 13788 (intel_state->dev_cdclk != dev_priv->cdclk_freq ||
63911d72 13789 intel_state->cdclk_pll_vco != dev_priv->cdclk_pll.vco))
33c8df89 13790 dev_priv->display.modeset_commit_cdclk(state);
f6d1973d 13791
c0ead703 13792 intel_modeset_verify_disabled(dev);
4740b0f2 13793 }
47fab737 13794
a6778b3c 13795 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
29ceb0e6 13796 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
f6ac4b2a
ML
13797 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13798 bool modeset = needs_modeset(crtc->state);
5a21b665
DV
13799 struct intel_crtc_state *pipe_config =
13800 to_intel_crtc_state(crtc->state);
13801 bool update_pipe = !modeset && pipe_config->update_pipe;
9f836f90 13802
f6ac4b2a 13803 if (modeset && crtc->state->active) {
a539205a
ML
13804 update_scanline_offset(to_intel_crtc(crtc));
13805 dev_priv->display.crtc_enable(crtc);
13806 }
80715b2f 13807
f6ac4b2a 13808 if (!modeset)
29ceb0e6 13809 intel_pre_plane_update(to_intel_crtc_state(old_crtc_state));
f6ac4b2a 13810
5a21b665
DV
13811 if (crtc->state->active &&
13812 drm_atomic_get_existing_plane_state(state, crtc->primary))
13813 intel_fbc_enable(intel_crtc);
13814
13815 if (crtc->state->active &&
13816 (crtc->state->planes_changed || update_pipe))
13817 drm_atomic_helper_commit_planes_on_crtc(old_crtc_state);
f6d1973d 13818
5a21b665
DV
13819 if (pipe_config->base.active && needs_vblank_wait(pipe_config))
13820 crtc_vblank_mask |= 1 << i;
177246a8
MR
13821 }
13822
d55dbd06
ML
13823 /* FIXME: add subpixel order */
13824
5a21b665
DV
13825 if (!state->legacy_cursor_update)
13826 intel_atomic_wait_for_vblanks(dev, dev_priv, crtc_vblank_mask);
13827
13828 /*
13829 * Now that the vblank has passed, we can go ahead and program the
13830 * optimal watermarks on platforms that need two-step watermark
13831 * programming.
13832 *
13833 * TODO: Move this (and other cleanup) to an async worker eventually.
13834 */
13835 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
13836 intel_cstate = to_intel_crtc_state(crtc->state);
13837
13838 if (dev_priv->display.optimize_watermarks)
13839 dev_priv->display.optimize_watermarks(intel_cstate);
13840 }
13841
13842 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
13843 intel_post_plane_update(to_intel_crtc_state(old_crtc_state));
13844
13845 if (put_domains[i])
13846 modeset_put_power_domains(dev_priv, put_domains[i]);
13847
13848 intel_modeset_verify_crtc(crtc, old_crtc_state, crtc->state);
13849 }
13850
13851 if (intel_state->modeset)
13852 intel_display_power_put(dev_priv, POWER_DOMAIN_MODESET);
13853
13854 mutex_lock(&dev->struct_mutex);
13855 drm_atomic_helper_cleanup_planes(dev, state);
13856 mutex_unlock(&dev->struct_mutex);
13857
ee165b1a 13858 drm_atomic_state_free(state);
f30da187 13859
75714940
MK
13860 /* As one of the primary mmio accessors, KMS has a high likelihood
13861 * of triggering bugs in unclaimed access. After we finish
13862 * modesetting, see if an error has been flagged, and if so
13863 * enable debugging for the next modeset - and hope we catch
13864 * the culprit.
13865 *
13866 * XXX note that we assume display power is on at this point.
13867 * This might hold true now but we need to add pm helper to check
13868 * unclaimed only when the hardware is on, as atomic commits
13869 * can happen also when the device is completely off.
13870 */
13871 intel_uncore_arm_unclaimed_mmio_detection(dev_priv);
13872
74c090b1 13873 return 0;
7f27126e
JB
13874}
13875
c0c36b94
CW
13876void intel_crtc_restore_mode(struct drm_crtc *crtc)
13877{
83a57153
ACO
13878 struct drm_device *dev = crtc->dev;
13879 struct drm_atomic_state *state;
e694eb02 13880 struct drm_crtc_state *crtc_state;
2bfb4627 13881 int ret;
83a57153
ACO
13882
13883 state = drm_atomic_state_alloc(dev);
13884 if (!state) {
78108b7c
VS
13885 DRM_DEBUG_KMS("[CRTC:%d:%s] crtc restore failed, out of memory",
13886 crtc->base.id, crtc->name);
83a57153
ACO
13887 return;
13888 }
13889
e694eb02 13890 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
83a57153 13891
e694eb02
ML
13892retry:
13893 crtc_state = drm_atomic_get_crtc_state(state, crtc);
13894 ret = PTR_ERR_OR_ZERO(crtc_state);
13895 if (!ret) {
13896 if (!crtc_state->active)
13897 goto out;
83a57153 13898
e694eb02 13899 crtc_state->mode_changed = true;
74c090b1 13900 ret = drm_atomic_commit(state);
83a57153
ACO
13901 }
13902
e694eb02
ML
13903 if (ret == -EDEADLK) {
13904 drm_atomic_state_clear(state);
13905 drm_modeset_backoff(state->acquire_ctx);
13906 goto retry;
4ed9fb37 13907 }
4be07317 13908
2bfb4627 13909 if (ret)
e694eb02 13910out:
2bfb4627 13911 drm_atomic_state_free(state);
c0c36b94
CW
13912}
13913
25c5b266
DV
13914#undef for_each_intel_crtc_masked
13915
f6e5b160 13916static const struct drm_crtc_funcs intel_crtc_funcs = {
82cf435b 13917 .gamma_set = drm_atomic_helper_legacy_gamma_set,
74c090b1 13918 .set_config = drm_atomic_helper_set_config,
82cf435b 13919 .set_property = drm_atomic_helper_crtc_set_property,
f6e5b160 13920 .destroy = intel_crtc_destroy,
5a21b665 13921 .page_flip = intel_crtc_page_flip,
1356837e
MR
13922 .atomic_duplicate_state = intel_crtc_duplicate_state,
13923 .atomic_destroy_state = intel_crtc_destroy_state,
f6e5b160
CW
13924};
13925
6beb8c23
MR
13926/**
13927 * intel_prepare_plane_fb - Prepare fb for usage on plane
13928 * @plane: drm plane to prepare for
13929 * @fb: framebuffer to prepare for presentation
13930 *
13931 * Prepares a framebuffer for usage on a display plane. Generally this
13932 * involves pinning the underlying object and updating the frontbuffer tracking
13933 * bits. Some older platforms need special physical address handling for
13934 * cursor planes.
13935 *
f935675f
ML
13936 * Must be called with struct_mutex held.
13937 *
6beb8c23
MR
13938 * Returns 0 on success, negative error code on failure.
13939 */
13940int
13941intel_prepare_plane_fb(struct drm_plane *plane,
d136dfee 13942 const struct drm_plane_state *new_state)
465c120c
MR
13943{
13944 struct drm_device *dev = plane->dev;
844f9111 13945 struct drm_framebuffer *fb = new_state->fb;
6beb8c23 13946 struct intel_plane *intel_plane = to_intel_plane(plane);
6beb8c23 13947 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
1ee49399 13948 struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->state->fb);
6beb8c23 13949 int ret = 0;
465c120c 13950
1ee49399 13951 if (!obj && !old_obj)
465c120c
MR
13952 return 0;
13953
5008e874
ML
13954 if (old_obj) {
13955 struct drm_crtc_state *crtc_state =
13956 drm_atomic_get_existing_crtc_state(new_state->state, plane->state->crtc);
13957
13958 /* Big Hammer, we also need to ensure that any pending
13959 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
13960 * current scanout is retired before unpinning the old
13961 * framebuffer. Note that we rely on userspace rendering
13962 * into the buffer attached to the pipe they are waiting
13963 * on. If not, userspace generates a GPU hang with IPEHR
13964 * point to the MI_WAIT_FOR_EVENT.
13965 *
13966 * This should only fail upon a hung GPU, in which case we
13967 * can safely continue.
13968 */
13969 if (needs_modeset(crtc_state))
13970 ret = i915_gem_object_wait_rendering(old_obj, true);
f4457ae7
CW
13971 if (ret) {
13972 /* GPU hangs should have been swallowed by the wait */
13973 WARN_ON(ret == -EIO);
f935675f 13974 return ret;
f4457ae7 13975 }
5008e874
ML
13976 }
13977
5a21b665
DV
13978 /* For framebuffer backed by dmabuf, wait for fence */
13979 if (obj && obj->base.dma_buf) {
13980 long lret;
13981
13982 lret = reservation_object_wait_timeout_rcu(obj->base.dma_buf->resv,
13983 false, true,
13984 MAX_SCHEDULE_TIMEOUT);
13985 if (lret == -ERESTARTSYS)
13986 return lret;
13987
13988 WARN(lret < 0, "waiting returns %li\n", lret);
13989 }
13990
1ee49399
ML
13991 if (!obj) {
13992 ret = 0;
13993 } else if (plane->type == DRM_PLANE_TYPE_CURSOR &&
6beb8c23
MR
13994 INTEL_INFO(dev)->cursor_needs_physical) {
13995 int align = IS_I830(dev) ? 16 * 1024 : 256;
13996 ret = i915_gem_object_attach_phys(obj, align);
13997 if (ret)
13998 DRM_DEBUG_KMS("failed to attach phys object\n");
13999 } else {
3465c580 14000 ret = intel_pin_and_fence_fb_obj(fb, new_state->rotation);
6beb8c23 14001 }
465c120c 14002
7580d774
ML
14003 if (ret == 0) {
14004 if (obj) {
14005 struct intel_plane_state *plane_state =
14006 to_intel_plane_state(new_state);
14007
14008 i915_gem_request_assign(&plane_state->wait_req,
14009 obj->last_write_req);
14010 }
14011
a9ff8714 14012 i915_gem_track_fb(old_obj, obj, intel_plane->frontbuffer_bit);
7580d774 14013 }
fdd508a6 14014
6beb8c23
MR
14015 return ret;
14016}
14017
38f3ce3a
MR
14018/**
14019 * intel_cleanup_plane_fb - Cleans up an fb after plane use
14020 * @plane: drm plane to clean up for
14021 * @fb: old framebuffer that was on plane
14022 *
14023 * Cleans up a framebuffer that has just been removed from a plane.
f935675f
ML
14024 *
14025 * Must be called with struct_mutex held.
38f3ce3a
MR
14026 */
14027void
14028intel_cleanup_plane_fb(struct drm_plane *plane,
d136dfee 14029 const struct drm_plane_state *old_state)
38f3ce3a
MR
14030{
14031 struct drm_device *dev = plane->dev;
1ee49399 14032 struct intel_plane *intel_plane = to_intel_plane(plane);
7580d774 14033 struct intel_plane_state *old_intel_state;
1ee49399
ML
14034 struct drm_i915_gem_object *old_obj = intel_fb_obj(old_state->fb);
14035 struct drm_i915_gem_object *obj = intel_fb_obj(plane->state->fb);
38f3ce3a 14036
7580d774
ML
14037 old_intel_state = to_intel_plane_state(old_state);
14038
1ee49399 14039 if (!obj && !old_obj)
38f3ce3a
MR
14040 return;
14041
1ee49399
ML
14042 if (old_obj && (plane->type != DRM_PLANE_TYPE_CURSOR ||
14043 !INTEL_INFO(dev)->cursor_needs_physical))
3465c580 14044 intel_unpin_fb_obj(old_state->fb, old_state->rotation);
1ee49399
ML
14045
14046 /* prepare_fb aborted? */
14047 if ((old_obj && (old_obj->frontbuffer_bits & intel_plane->frontbuffer_bit)) ||
14048 (obj && !(obj->frontbuffer_bits & intel_plane->frontbuffer_bit)))
14049 i915_gem_track_fb(old_obj, obj, intel_plane->frontbuffer_bit);
7580d774
ML
14050
14051 i915_gem_request_assign(&old_intel_state->wait_req, NULL);
465c120c
MR
14052}
14053
6156a456
CK
14054int
14055skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
14056{
14057 int max_scale;
14058 struct drm_device *dev;
14059 struct drm_i915_private *dev_priv;
14060 int crtc_clock, cdclk;
14061
bf8a0af0 14062 if (!intel_crtc || !crtc_state->base.enable)
6156a456
CK
14063 return DRM_PLANE_HELPER_NO_SCALING;
14064
14065 dev = intel_crtc->base.dev;
14066 dev_priv = dev->dev_private;
14067 crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
27c329ed 14068 cdclk = to_intel_atomic_state(crtc_state->base.state)->cdclk;
6156a456 14069
54bf1ce6 14070 if (WARN_ON_ONCE(!crtc_clock || cdclk < crtc_clock))
6156a456
CK
14071 return DRM_PLANE_HELPER_NO_SCALING;
14072
14073 /*
14074 * skl max scale is lower of:
14075 * close to 3 but not 3, -1 is for that purpose
14076 * or
14077 * cdclk/crtc_clock
14078 */
14079 max_scale = min((1 << 16) * 3 - 1, (1 << 8) * ((cdclk << 8) / crtc_clock));
14080
14081 return max_scale;
14082}
14083
465c120c 14084static int
3c692a41 14085intel_check_primary_plane(struct drm_plane *plane,
061e4b8d 14086 struct intel_crtc_state *crtc_state,
3c692a41
GP
14087 struct intel_plane_state *state)
14088{
2b875c22
MR
14089 struct drm_crtc *crtc = state->base.crtc;
14090 struct drm_framebuffer *fb = state->base.fb;
6156a456 14091 int min_scale = DRM_PLANE_HELPER_NO_SCALING;
061e4b8d
ML
14092 int max_scale = DRM_PLANE_HELPER_NO_SCALING;
14093 bool can_position = false;
465c120c 14094
693bdc28
VS
14095 if (INTEL_INFO(plane->dev)->gen >= 9) {
14096 /* use scaler when colorkey is not required */
14097 if (state->ckey.flags == I915_SET_COLORKEY_NONE) {
14098 min_scale = 1;
14099 max_scale = skl_max_scale(to_intel_crtc(crtc), crtc_state);
14100 }
d8106366 14101 can_position = true;
6156a456 14102 }
d8106366 14103
061e4b8d
ML
14104 return drm_plane_helper_check_update(plane, crtc, fb, &state->src,
14105 &state->dst, &state->clip,
da20eabd
ML
14106 min_scale, max_scale,
14107 can_position, true,
14108 &state->visible);
14af293f
GP
14109}
14110
5a21b665
DV
14111static void intel_begin_crtc_commit(struct drm_crtc *crtc,
14112 struct drm_crtc_state *old_crtc_state)
14113{
14114 struct drm_device *dev = crtc->dev;
14115 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
14116 struct intel_crtc_state *old_intel_state =
14117 to_intel_crtc_state(old_crtc_state);
14118 bool modeset = needs_modeset(crtc->state);
14119
14120 /* Perform vblank evasion around commit operation */
14121 intel_pipe_update_start(intel_crtc);
14122
14123 if (modeset)
14124 return;
14125
14126 if (crtc->state->color_mgmt_changed || to_intel_crtc_state(crtc->state)->update_pipe) {
14127 intel_color_set_csc(crtc->state);
14128 intel_color_load_luts(crtc->state);
14129 }
14130
14131 if (to_intel_crtc_state(crtc->state)->update_pipe)
14132 intel_update_pipe_config(intel_crtc, old_intel_state);
14133 else if (INTEL_INFO(dev)->gen >= 9)
14134 skl_detach_scalers(intel_crtc);
14135}
14136
14137static void intel_finish_crtc_commit(struct drm_crtc *crtc,
14138 struct drm_crtc_state *old_crtc_state)
14139{
14140 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
14141
14142 intel_pipe_update_end(intel_crtc, NULL);
14143}
14144
cf4c7c12 14145/**
4a3b8769
MR
14146 * intel_plane_destroy - destroy a plane
14147 * @plane: plane to destroy
cf4c7c12 14148 *
4a3b8769
MR
14149 * Common destruction function for all types of planes (primary, cursor,
14150 * sprite).
cf4c7c12 14151 */
4a3b8769 14152void intel_plane_destroy(struct drm_plane *plane)
465c120c
MR
14153{
14154 struct intel_plane *intel_plane = to_intel_plane(plane);
14155 drm_plane_cleanup(plane);
14156 kfree(intel_plane);
14157}
14158
65a3fea0 14159const struct drm_plane_funcs intel_plane_funcs = {
70a101f8
MR
14160 .update_plane = drm_atomic_helper_update_plane,
14161 .disable_plane = drm_atomic_helper_disable_plane,
3d7d6510 14162 .destroy = intel_plane_destroy,
c196e1d6 14163 .set_property = drm_atomic_helper_plane_set_property,
a98b3431
MR
14164 .atomic_get_property = intel_plane_atomic_get_property,
14165 .atomic_set_property = intel_plane_atomic_set_property,
ea2c67bb
MR
14166 .atomic_duplicate_state = intel_plane_duplicate_state,
14167 .atomic_destroy_state = intel_plane_destroy_state,
14168
465c120c
MR
14169};
14170
14171static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
14172 int pipe)
14173{
fca0ce2a
VS
14174 struct intel_plane *primary = NULL;
14175 struct intel_plane_state *state = NULL;
465c120c 14176 const uint32_t *intel_primary_formats;
45e3743a 14177 unsigned int num_formats;
fca0ce2a 14178 int ret;
465c120c
MR
14179
14180 primary = kzalloc(sizeof(*primary), GFP_KERNEL);
fca0ce2a
VS
14181 if (!primary)
14182 goto fail;
465c120c 14183
8e7d688b 14184 state = intel_create_plane_state(&primary->base);
fca0ce2a
VS
14185 if (!state)
14186 goto fail;
8e7d688b 14187 primary->base.state = &state->base;
ea2c67bb 14188
465c120c
MR
14189 primary->can_scale = false;
14190 primary->max_downscale = 1;
6156a456
CK
14191 if (INTEL_INFO(dev)->gen >= 9) {
14192 primary->can_scale = true;
af99ceda 14193 state->scaler_id = -1;
6156a456 14194 }
465c120c
MR
14195 primary->pipe = pipe;
14196 primary->plane = pipe;
a9ff8714 14197 primary->frontbuffer_bit = INTEL_FRONTBUFFER_PRIMARY(pipe);
c59cb179 14198 primary->check_plane = intel_check_primary_plane;
465c120c
MR
14199 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
14200 primary->plane = !pipe;
14201
6c0fd451
DL
14202 if (INTEL_INFO(dev)->gen >= 9) {
14203 intel_primary_formats = skl_primary_formats;
14204 num_formats = ARRAY_SIZE(skl_primary_formats);
a8d201af
ML
14205
14206 primary->update_plane = skylake_update_primary_plane;
14207 primary->disable_plane = skylake_disable_primary_plane;
14208 } else if (HAS_PCH_SPLIT(dev)) {
14209 intel_primary_formats = i965_primary_formats;
14210 num_formats = ARRAY_SIZE(i965_primary_formats);
14211
14212 primary->update_plane = ironlake_update_primary_plane;
14213 primary->disable_plane = i9xx_disable_primary_plane;
6c0fd451 14214 } else if (INTEL_INFO(dev)->gen >= 4) {
568db4f2
DL
14215 intel_primary_formats = i965_primary_formats;
14216 num_formats = ARRAY_SIZE(i965_primary_formats);
a8d201af
ML
14217
14218 primary->update_plane = i9xx_update_primary_plane;
14219 primary->disable_plane = i9xx_disable_primary_plane;
6c0fd451
DL
14220 } else {
14221 intel_primary_formats = i8xx_primary_formats;
14222 num_formats = ARRAY_SIZE(i8xx_primary_formats);
a8d201af
ML
14223
14224 primary->update_plane = i9xx_update_primary_plane;
14225 primary->disable_plane = i9xx_disable_primary_plane;
465c120c
MR
14226 }
14227
fca0ce2a
VS
14228 ret = drm_universal_plane_init(dev, &primary->base, 0,
14229 &intel_plane_funcs,
14230 intel_primary_formats, num_formats,
14231 DRM_PLANE_TYPE_PRIMARY, NULL);
14232 if (ret)
14233 goto fail;
48404c1e 14234
3b7a5119
SJ
14235 if (INTEL_INFO(dev)->gen >= 4)
14236 intel_create_rotation_property(dev, primary);
48404c1e 14237
ea2c67bb
MR
14238 drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
14239
465c120c 14240 return &primary->base;
fca0ce2a
VS
14241
14242fail:
14243 kfree(state);
14244 kfree(primary);
14245
14246 return NULL;
465c120c
MR
14247}
14248
3b7a5119
SJ
14249void intel_create_rotation_property(struct drm_device *dev, struct intel_plane *plane)
14250{
14251 if (!dev->mode_config.rotation_property) {
14252 unsigned long flags = BIT(DRM_ROTATE_0) |
14253 BIT(DRM_ROTATE_180);
14254
14255 if (INTEL_INFO(dev)->gen >= 9)
14256 flags |= BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_270);
14257
14258 dev->mode_config.rotation_property =
14259 drm_mode_create_rotation_property(dev, flags);
14260 }
14261 if (dev->mode_config.rotation_property)
14262 drm_object_attach_property(&plane->base.base,
14263 dev->mode_config.rotation_property,
14264 plane->base.state->rotation);
14265}
14266
3d7d6510 14267static int
852e787c 14268intel_check_cursor_plane(struct drm_plane *plane,
061e4b8d 14269 struct intel_crtc_state *crtc_state,
852e787c 14270 struct intel_plane_state *state)
3d7d6510 14271{
061e4b8d 14272 struct drm_crtc *crtc = crtc_state->base.crtc;
2b875c22 14273 struct drm_framebuffer *fb = state->base.fb;
757f9a3e 14274 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
b29ec92c 14275 enum pipe pipe = to_intel_plane(plane)->pipe;
757f9a3e
GP
14276 unsigned stride;
14277 int ret;
3d7d6510 14278
061e4b8d
ML
14279 ret = drm_plane_helper_check_update(plane, crtc, fb, &state->src,
14280 &state->dst, &state->clip,
3d7d6510
MR
14281 DRM_PLANE_HELPER_NO_SCALING,
14282 DRM_PLANE_HELPER_NO_SCALING,
852e787c 14283 true, true, &state->visible);
757f9a3e
GP
14284 if (ret)
14285 return ret;
14286
757f9a3e
GP
14287 /* if we want to turn off the cursor ignore width and height */
14288 if (!obj)
da20eabd 14289 return 0;
757f9a3e 14290
757f9a3e 14291 /* Check for which cursor types we support */
061e4b8d 14292 if (!cursor_size_ok(plane->dev, state->base.crtc_w, state->base.crtc_h)) {
ea2c67bb
MR
14293 DRM_DEBUG("Cursor dimension %dx%d not supported\n",
14294 state->base.crtc_w, state->base.crtc_h);
757f9a3e
GP
14295 return -EINVAL;
14296 }
14297
ea2c67bb
MR
14298 stride = roundup_pow_of_two(state->base.crtc_w) * 4;
14299 if (obj->base.size < stride * state->base.crtc_h) {
757f9a3e
GP
14300 DRM_DEBUG_KMS("buffer is too small\n");
14301 return -ENOMEM;
14302 }
14303
3a656b54 14304 if (fb->modifier[0] != DRM_FORMAT_MOD_NONE) {
757f9a3e 14305 DRM_DEBUG_KMS("cursor cannot be tiled\n");
da20eabd 14306 return -EINVAL;
32b7eeec
MR
14307 }
14308
b29ec92c
VS
14309 /*
14310 * There's something wrong with the cursor on CHV pipe C.
14311 * If it straddles the left edge of the screen then
14312 * moving it away from the edge or disabling it often
14313 * results in a pipe underrun, and often that can lead to
14314 * dead pipe (constant underrun reported, and it scans
14315 * out just a solid color). To recover from that, the
14316 * display power well must be turned off and on again.
14317 * Refuse the put the cursor into that compromised position.
14318 */
14319 if (IS_CHERRYVIEW(plane->dev) && pipe == PIPE_C &&
14320 state->visible && state->base.crtc_x < 0) {
14321 DRM_DEBUG_KMS("CHV cursor C not allowed to straddle the left screen edge\n");
14322 return -EINVAL;
14323 }
14324
da20eabd 14325 return 0;
852e787c 14326}
3d7d6510 14327
a8ad0d8e
ML
14328static void
14329intel_disable_cursor_plane(struct drm_plane *plane,
7fabf5ef 14330 struct drm_crtc *crtc)
a8ad0d8e 14331{
f2858021
ML
14332 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
14333
14334 intel_crtc->cursor_addr = 0;
55a08b3f 14335 intel_crtc_update_cursor(crtc, NULL);
a8ad0d8e
ML
14336}
14337
f4a2cf29 14338static void
55a08b3f
ML
14339intel_update_cursor_plane(struct drm_plane *plane,
14340 const struct intel_crtc_state *crtc_state,
14341 const struct intel_plane_state *state)
852e787c 14342{
55a08b3f
ML
14343 struct drm_crtc *crtc = crtc_state->base.crtc;
14344 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ea2c67bb 14345 struct drm_device *dev = plane->dev;
2b875c22 14346 struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
a912f12f 14347 uint32_t addr;
852e787c 14348
f4a2cf29 14349 if (!obj)
a912f12f 14350 addr = 0;
f4a2cf29 14351 else if (!INTEL_INFO(dev)->cursor_needs_physical)
a912f12f 14352 addr = i915_gem_obj_ggtt_offset(obj);
f4a2cf29 14353 else
a912f12f 14354 addr = obj->phys_handle->busaddr;
852e787c 14355
a912f12f 14356 intel_crtc->cursor_addr = addr;
55a08b3f 14357 intel_crtc_update_cursor(crtc, state);
852e787c
GP
14358}
14359
3d7d6510
MR
14360static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
14361 int pipe)
14362{
fca0ce2a
VS
14363 struct intel_plane *cursor = NULL;
14364 struct intel_plane_state *state = NULL;
14365 int ret;
3d7d6510
MR
14366
14367 cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
fca0ce2a
VS
14368 if (!cursor)
14369 goto fail;
3d7d6510 14370
8e7d688b 14371 state = intel_create_plane_state(&cursor->base);
fca0ce2a
VS
14372 if (!state)
14373 goto fail;
8e7d688b 14374 cursor->base.state = &state->base;
ea2c67bb 14375
3d7d6510
MR
14376 cursor->can_scale = false;
14377 cursor->max_downscale = 1;
14378 cursor->pipe = pipe;
14379 cursor->plane = pipe;
a9ff8714 14380 cursor->frontbuffer_bit = INTEL_FRONTBUFFER_CURSOR(pipe);
c59cb179 14381 cursor->check_plane = intel_check_cursor_plane;
55a08b3f 14382 cursor->update_plane = intel_update_cursor_plane;
a8ad0d8e 14383 cursor->disable_plane = intel_disable_cursor_plane;
3d7d6510 14384
fca0ce2a
VS
14385 ret = drm_universal_plane_init(dev, &cursor->base, 0,
14386 &intel_plane_funcs,
14387 intel_cursor_formats,
14388 ARRAY_SIZE(intel_cursor_formats),
14389 DRM_PLANE_TYPE_CURSOR, NULL);
14390 if (ret)
14391 goto fail;
4398ad45
VS
14392
14393 if (INTEL_INFO(dev)->gen >= 4) {
14394 if (!dev->mode_config.rotation_property)
14395 dev->mode_config.rotation_property =
14396 drm_mode_create_rotation_property(dev,
14397 BIT(DRM_ROTATE_0) |
14398 BIT(DRM_ROTATE_180));
14399 if (dev->mode_config.rotation_property)
14400 drm_object_attach_property(&cursor->base.base,
14401 dev->mode_config.rotation_property,
8e7d688b 14402 state->base.rotation);
4398ad45
VS
14403 }
14404
af99ceda
CK
14405 if (INTEL_INFO(dev)->gen >=9)
14406 state->scaler_id = -1;
14407
ea2c67bb
MR
14408 drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
14409
3d7d6510 14410 return &cursor->base;
fca0ce2a
VS
14411
14412fail:
14413 kfree(state);
14414 kfree(cursor);
14415
14416 return NULL;
3d7d6510
MR
14417}
14418
549e2bfb
CK
14419static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
14420 struct intel_crtc_state *crtc_state)
14421{
14422 int i;
14423 struct intel_scaler *intel_scaler;
14424 struct intel_crtc_scaler_state *scaler_state = &crtc_state->scaler_state;
14425
14426 for (i = 0; i < intel_crtc->num_scalers; i++) {
14427 intel_scaler = &scaler_state->scalers[i];
14428 intel_scaler->in_use = 0;
549e2bfb
CK
14429 intel_scaler->mode = PS_SCALER_MODE_DYN;
14430 }
14431
14432 scaler_state->scaler_id = -1;
14433}
14434
b358d0a6 14435static void intel_crtc_init(struct drm_device *dev, int pipe)
79e53945 14436{
fbee40df 14437 struct drm_i915_private *dev_priv = dev->dev_private;
79e53945 14438 struct intel_crtc *intel_crtc;
f5de6e07 14439 struct intel_crtc_state *crtc_state = NULL;
3d7d6510
MR
14440 struct drm_plane *primary = NULL;
14441 struct drm_plane *cursor = NULL;
8563b1e8 14442 int ret;
79e53945 14443
955382f3 14444 intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
79e53945
JB
14445 if (intel_crtc == NULL)
14446 return;
14447
f5de6e07
ACO
14448 crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
14449 if (!crtc_state)
14450 goto fail;
550acefd
ACO
14451 intel_crtc->config = crtc_state;
14452 intel_crtc->base.state = &crtc_state->base;
07878248 14453 crtc_state->base.crtc = &intel_crtc->base;
f5de6e07 14454
549e2bfb
CK
14455 /* initialize shared scalers */
14456 if (INTEL_INFO(dev)->gen >= 9) {
14457 if (pipe == PIPE_C)
14458 intel_crtc->num_scalers = 1;
14459 else
14460 intel_crtc->num_scalers = SKL_NUM_SCALERS;
14461
14462 skl_init_scalers(dev, intel_crtc, crtc_state);
14463 }
14464
465c120c 14465 primary = intel_primary_plane_create(dev, pipe);
3d7d6510
MR
14466 if (!primary)
14467 goto fail;
14468
14469 cursor = intel_cursor_plane_create(dev, pipe);
14470 if (!cursor)
14471 goto fail;
14472
465c120c 14473 ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
f9882876 14474 cursor, &intel_crtc_funcs, NULL);
3d7d6510
MR
14475 if (ret)
14476 goto fail;
79e53945 14477
1f1c2e24
VS
14478 /*
14479 * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
8c0f92e1 14480 * is hooked to pipe B. Hence we want plane A feeding pipe B.
1f1c2e24 14481 */
80824003
JB
14482 intel_crtc->pipe = pipe;
14483 intel_crtc->plane = pipe;
3a77c4c4 14484 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
28c97730 14485 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
e2e767ab 14486 intel_crtc->plane = !pipe;
80824003
JB
14487 }
14488
4b0e333e
CW
14489 intel_crtc->cursor_base = ~0;
14490 intel_crtc->cursor_cntl = ~0;
dc41c154 14491 intel_crtc->cursor_size = ~0;
8d7849db 14492
852eb00d
VS
14493 intel_crtc->wm.cxsr_allowed = true;
14494
22fd0fab
JB
14495 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
14496 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
14497 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
14498 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
14499
79e53945 14500 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
87b6b101 14501
8563b1e8
LL
14502 intel_color_init(&intel_crtc->base);
14503
87b6b101 14504 WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
3d7d6510
MR
14505 return;
14506
14507fail:
14508 if (primary)
14509 drm_plane_cleanup(primary);
14510 if (cursor)
14511 drm_plane_cleanup(cursor);
f5de6e07 14512 kfree(crtc_state);
3d7d6510 14513 kfree(intel_crtc);
79e53945
JB
14514}
14515
752aa88a
JB
14516enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
14517{
14518 struct drm_encoder *encoder = connector->base.encoder;
6e9f798d 14519 struct drm_device *dev = connector->base.dev;
752aa88a 14520
51fd371b 14521 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
752aa88a 14522
d3babd3f 14523 if (!encoder || WARN_ON(!encoder->crtc))
752aa88a
JB
14524 return INVALID_PIPE;
14525
14526 return to_intel_crtc(encoder->crtc)->pipe;
14527}
14528
08d7b3d1 14529int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
05394f39 14530 struct drm_file *file)
08d7b3d1 14531{
08d7b3d1 14532 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
7707e653 14533 struct drm_crtc *drmmode_crtc;
c05422d5 14534 struct intel_crtc *crtc;
08d7b3d1 14535
7707e653 14536 drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
08d7b3d1 14537
7707e653 14538 if (!drmmode_crtc) {
08d7b3d1 14539 DRM_ERROR("no such CRTC id\n");
3f2c2057 14540 return -ENOENT;
08d7b3d1
CW
14541 }
14542
7707e653 14543 crtc = to_intel_crtc(drmmode_crtc);
c05422d5 14544 pipe_from_crtc_id->pipe = crtc->pipe;
08d7b3d1 14545
c05422d5 14546 return 0;
08d7b3d1
CW
14547}
14548
66a9278e 14549static int intel_encoder_clones(struct intel_encoder *encoder)
79e53945 14550{
66a9278e
DV
14551 struct drm_device *dev = encoder->base.dev;
14552 struct intel_encoder *source_encoder;
79e53945 14553 int index_mask = 0;
79e53945
JB
14554 int entry = 0;
14555
b2784e15 14556 for_each_intel_encoder(dev, source_encoder) {
bc079e8b 14557 if (encoders_cloneable(encoder, source_encoder))
66a9278e
DV
14558 index_mask |= (1 << entry);
14559
79e53945
JB
14560 entry++;
14561 }
4ef69c7a 14562
79e53945
JB
14563 return index_mask;
14564}
14565
4d302442
CW
14566static bool has_edp_a(struct drm_device *dev)
14567{
14568 struct drm_i915_private *dev_priv = dev->dev_private;
14569
14570 if (!IS_MOBILE(dev))
14571 return false;
14572
14573 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
14574 return false;
14575
e3589908 14576 if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
4d302442
CW
14577 return false;
14578
14579 return true;
14580}
14581
84b4e042
JB
14582static bool intel_crt_present(struct drm_device *dev)
14583{
14584 struct drm_i915_private *dev_priv = dev->dev_private;
14585
884497ed
DL
14586 if (INTEL_INFO(dev)->gen >= 9)
14587 return false;
14588
cf404ce4 14589 if (IS_HSW_ULT(dev) || IS_BDW_ULT(dev))
84b4e042
JB
14590 return false;
14591
14592 if (IS_CHERRYVIEW(dev))
14593 return false;
14594
65e472e4
VS
14595 if (HAS_PCH_LPT_H(dev) && I915_READ(SFUSE_STRAP) & SFUSE_STRAP_CRT_DISABLED)
14596 return false;
14597
70ac54d0
VS
14598 /* DDI E can't be used if DDI A requires 4 lanes */
14599 if (HAS_DDI(dev) && I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_A_4_LANES)
14600 return false;
14601
e4abb733 14602 if (!dev_priv->vbt.int_crt_support)
84b4e042
JB
14603 return false;
14604
14605 return true;
14606}
14607
79e53945
JB
14608static void intel_setup_outputs(struct drm_device *dev)
14609{
725e30ad 14610 struct drm_i915_private *dev_priv = dev->dev_private;
4ef69c7a 14611 struct intel_encoder *encoder;
cb0953d7 14612 bool dpd_is_edp = false;
79e53945 14613
c9093354 14614 intel_lvds_init(dev);
79e53945 14615
84b4e042 14616 if (intel_crt_present(dev))
79935fca 14617 intel_crt_init(dev);
cb0953d7 14618
c776eb2e
VK
14619 if (IS_BROXTON(dev)) {
14620 /*
14621 * FIXME: Broxton doesn't support port detection via the
14622 * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
14623 * detect the ports.
14624 */
14625 intel_ddi_init(dev, PORT_A);
14626 intel_ddi_init(dev, PORT_B);
14627 intel_ddi_init(dev, PORT_C);
c6c794a2
SS
14628
14629 intel_dsi_init(dev);
c776eb2e 14630 } else if (HAS_DDI(dev)) {
0e72a5b5
ED
14631 int found;
14632
de31facd
JB
14633 /*
14634 * Haswell uses DDI functions to detect digital outputs.
14635 * On SKL pre-D0 the strap isn't connected, so we assume
14636 * it's there.
14637 */
77179400 14638 found = I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_INIT_DISPLAY_DETECTED;
de31facd 14639 /* WaIgnoreDDIAStrap: skl */
ef11bdb3 14640 if (found || IS_SKYLAKE(dev) || IS_KABYLAKE(dev))
0e72a5b5
ED
14641 intel_ddi_init(dev, PORT_A);
14642
14643 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
14644 * register */
14645 found = I915_READ(SFUSE_STRAP);
14646
14647 if (found & SFUSE_STRAP_DDIB_DETECTED)
14648 intel_ddi_init(dev, PORT_B);
14649 if (found & SFUSE_STRAP_DDIC_DETECTED)
14650 intel_ddi_init(dev, PORT_C);
14651 if (found & SFUSE_STRAP_DDID_DETECTED)
14652 intel_ddi_init(dev, PORT_D);
2800e4c2
RV
14653 /*
14654 * On SKL we don't have a way to detect DDI-E so we rely on VBT.
14655 */
ef11bdb3 14656 if ((IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) &&
2800e4c2
RV
14657 (dev_priv->vbt.ddi_port_info[PORT_E].supports_dp ||
14658 dev_priv->vbt.ddi_port_info[PORT_E].supports_dvi ||
14659 dev_priv->vbt.ddi_port_info[PORT_E].supports_hdmi))
14660 intel_ddi_init(dev, PORT_E);
14661
0e72a5b5 14662 } else if (HAS_PCH_SPLIT(dev)) {
cb0953d7 14663 int found;
5d8a7752 14664 dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
270b3042
DV
14665
14666 if (has_edp_a(dev))
14667 intel_dp_init(dev, DP_A, PORT_A);
cb0953d7 14668
dc0fa718 14669 if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
461ed3ca 14670 /* PCH SDVOB multiplex with HDMIB */
2a5c0832 14671 found = intel_sdvo_init(dev, PCH_SDVOB, PORT_B);
30ad48b7 14672 if (!found)
e2debe91 14673 intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
5eb08b69 14674 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
ab9d7c30 14675 intel_dp_init(dev, PCH_DP_B, PORT_B);
30ad48b7
ZW
14676 }
14677
dc0fa718 14678 if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
e2debe91 14679 intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
30ad48b7 14680
dc0fa718 14681 if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
e2debe91 14682 intel_hdmi_init(dev, PCH_HDMID, PORT_D);
30ad48b7 14683
5eb08b69 14684 if (I915_READ(PCH_DP_C) & DP_DETECTED)
ab9d7c30 14685 intel_dp_init(dev, PCH_DP_C, PORT_C);
5eb08b69 14686
270b3042 14687 if (I915_READ(PCH_DP_D) & DP_DETECTED)
ab9d7c30 14688 intel_dp_init(dev, PCH_DP_D, PORT_D);
666a4537 14689 } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
e17ac6db
VS
14690 /*
14691 * The DP_DETECTED bit is the latched state of the DDC
14692 * SDA pin at boot. However since eDP doesn't require DDC
14693 * (no way to plug in a DP->HDMI dongle) the DDC pins for
14694 * eDP ports may have been muxed to an alternate function.
14695 * Thus we can't rely on the DP_DETECTED bit alone to detect
14696 * eDP ports. Consult the VBT as well as DP_DETECTED to
14697 * detect eDP ports.
14698 */
e66eb81d 14699 if (I915_READ(VLV_HDMIB) & SDVO_DETECTED &&
d2182a66 14700 !intel_dp_is_edp(dev, PORT_B))
e66eb81d
VS
14701 intel_hdmi_init(dev, VLV_HDMIB, PORT_B);
14702 if (I915_READ(VLV_DP_B) & DP_DETECTED ||
e17ac6db 14703 intel_dp_is_edp(dev, PORT_B))
e66eb81d 14704 intel_dp_init(dev, VLV_DP_B, PORT_B);
585a94b8 14705
e66eb81d 14706 if (I915_READ(VLV_HDMIC) & SDVO_DETECTED &&
d2182a66 14707 !intel_dp_is_edp(dev, PORT_C))
e66eb81d
VS
14708 intel_hdmi_init(dev, VLV_HDMIC, PORT_C);
14709 if (I915_READ(VLV_DP_C) & DP_DETECTED ||
e17ac6db 14710 intel_dp_is_edp(dev, PORT_C))
e66eb81d 14711 intel_dp_init(dev, VLV_DP_C, PORT_C);
19c03924 14712
9418c1f1 14713 if (IS_CHERRYVIEW(dev)) {
e17ac6db 14714 /* eDP not supported on port D, so don't check VBT */
e66eb81d
VS
14715 if (I915_READ(CHV_HDMID) & SDVO_DETECTED)
14716 intel_hdmi_init(dev, CHV_HDMID, PORT_D);
14717 if (I915_READ(CHV_DP_D) & DP_DETECTED)
14718 intel_dp_init(dev, CHV_DP_D, PORT_D);
9418c1f1
VS
14719 }
14720
3cfca973 14721 intel_dsi_init(dev);
09da55dc 14722 } else if (!IS_GEN2(dev) && !IS_PINEVIEW(dev)) {
27185ae1 14723 bool found = false;
7d57382e 14724
e2debe91 14725 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
b01f2c3a 14726 DRM_DEBUG_KMS("probing SDVOB\n");
2a5c0832 14727 found = intel_sdvo_init(dev, GEN3_SDVOB, PORT_B);
3fec3d2f 14728 if (!found && IS_G4X(dev)) {
b01f2c3a 14729 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
e2debe91 14730 intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
b01f2c3a 14731 }
27185ae1 14732
3fec3d2f 14733 if (!found && IS_G4X(dev))
ab9d7c30 14734 intel_dp_init(dev, DP_B, PORT_B);
725e30ad 14735 }
13520b05
KH
14736
14737 /* Before G4X SDVOC doesn't have its own detect register */
13520b05 14738
e2debe91 14739 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
b01f2c3a 14740 DRM_DEBUG_KMS("probing SDVOC\n");
2a5c0832 14741 found = intel_sdvo_init(dev, GEN3_SDVOC, PORT_C);
b01f2c3a 14742 }
27185ae1 14743
e2debe91 14744 if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
27185ae1 14745
3fec3d2f 14746 if (IS_G4X(dev)) {
b01f2c3a 14747 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
e2debe91 14748 intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
b01f2c3a 14749 }
3fec3d2f 14750 if (IS_G4X(dev))
ab9d7c30 14751 intel_dp_init(dev, DP_C, PORT_C);
725e30ad 14752 }
27185ae1 14753
3fec3d2f 14754 if (IS_G4X(dev) &&
e7281eab 14755 (I915_READ(DP_D) & DP_DETECTED))
ab9d7c30 14756 intel_dp_init(dev, DP_D, PORT_D);
bad720ff 14757 } else if (IS_GEN2(dev))
79e53945
JB
14758 intel_dvo_init(dev);
14759
103a196f 14760 if (SUPPORTS_TV(dev))
79e53945
JB
14761 intel_tv_init(dev);
14762
0bc12bcb 14763 intel_psr_init(dev);
7c8f8a70 14764
b2784e15 14765 for_each_intel_encoder(dev, encoder) {
4ef69c7a
CW
14766 encoder->base.possible_crtcs = encoder->crtc_mask;
14767 encoder->base.possible_clones =
66a9278e 14768 intel_encoder_clones(encoder);
79e53945 14769 }
47356eb6 14770
dde86e2d 14771 intel_init_pch_refclk(dev);
270b3042
DV
14772
14773 drm_helper_move_panel_connectors_to_head(dev);
79e53945
JB
14774}
14775
14776static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
14777{
60a5ca01 14778 struct drm_device *dev = fb->dev;
79e53945 14779 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
79e53945 14780
ef2d633e 14781 drm_framebuffer_cleanup(fb);
60a5ca01 14782 mutex_lock(&dev->struct_mutex);
ef2d633e 14783 WARN_ON(!intel_fb->obj->framebuffer_references--);
60a5ca01
VS
14784 drm_gem_object_unreference(&intel_fb->obj->base);
14785 mutex_unlock(&dev->struct_mutex);
79e53945
JB
14786 kfree(intel_fb);
14787}
14788
14789static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
05394f39 14790 struct drm_file *file,
79e53945
JB
14791 unsigned int *handle)
14792{
14793 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
05394f39 14794 struct drm_i915_gem_object *obj = intel_fb->obj;
79e53945 14795
cc917ab4
CW
14796 if (obj->userptr.mm) {
14797 DRM_DEBUG("attempting to use a userptr for a framebuffer, denied\n");
14798 return -EINVAL;
14799 }
14800
05394f39 14801 return drm_gem_handle_create(file, &obj->base, handle);
79e53945
JB
14802}
14803
86c98588
RV
14804static int intel_user_framebuffer_dirty(struct drm_framebuffer *fb,
14805 struct drm_file *file,
14806 unsigned flags, unsigned color,
14807 struct drm_clip_rect *clips,
14808 unsigned num_clips)
14809{
14810 struct drm_device *dev = fb->dev;
14811 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
14812 struct drm_i915_gem_object *obj = intel_fb->obj;
14813
14814 mutex_lock(&dev->struct_mutex);
74b4ea1e 14815 intel_fb_obj_flush(obj, false, ORIGIN_DIRTYFB);
86c98588
RV
14816 mutex_unlock(&dev->struct_mutex);
14817
14818 return 0;
14819}
14820
79e53945
JB
14821static const struct drm_framebuffer_funcs intel_fb_funcs = {
14822 .destroy = intel_user_framebuffer_destroy,
14823 .create_handle = intel_user_framebuffer_create_handle,
86c98588 14824 .dirty = intel_user_framebuffer_dirty,
79e53945
JB
14825};
14826
b321803d
DL
14827static
14828u32 intel_fb_pitch_limit(struct drm_device *dev, uint64_t fb_modifier,
14829 uint32_t pixel_format)
14830{
14831 u32 gen = INTEL_INFO(dev)->gen;
14832
14833 if (gen >= 9) {
ac484963
VS
14834 int cpp = drm_format_plane_cpp(pixel_format, 0);
14835
b321803d
DL
14836 /* "The stride in bytes must not exceed the of the size of 8K
14837 * pixels and 32K bytes."
14838 */
ac484963 14839 return min(8192 * cpp, 32768);
666a4537 14840 } else if (gen >= 5 && !IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
b321803d
DL
14841 return 32*1024;
14842 } else if (gen >= 4) {
14843 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14844 return 16*1024;
14845 else
14846 return 32*1024;
14847 } else if (gen >= 3) {
14848 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14849 return 8*1024;
14850 else
14851 return 16*1024;
14852 } else {
14853 /* XXX DSPC is limited to 4k tiled */
14854 return 8*1024;
14855 }
14856}
14857
b5ea642a
DV
14858static int intel_framebuffer_init(struct drm_device *dev,
14859 struct intel_framebuffer *intel_fb,
14860 struct drm_mode_fb_cmd2 *mode_cmd,
14861 struct drm_i915_gem_object *obj)
79e53945 14862{
7b49f948 14863 struct drm_i915_private *dev_priv = to_i915(dev);
6761dd31 14864 unsigned int aligned_height;
79e53945 14865 int ret;
b321803d 14866 u32 pitch_limit, stride_alignment;
79e53945 14867
dd4916c5
DV
14868 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
14869
2a80eada
DV
14870 if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
14871 /* Enforce that fb modifier and tiling mode match, but only for
14872 * X-tiled. This is needed for FBC. */
14873 if (!!(obj->tiling_mode == I915_TILING_X) !=
14874 !!(mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED)) {
14875 DRM_DEBUG("tiling_mode doesn't match fb modifier\n");
14876 return -EINVAL;
14877 }
14878 } else {
14879 if (obj->tiling_mode == I915_TILING_X)
14880 mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
14881 else if (obj->tiling_mode == I915_TILING_Y) {
14882 DRM_DEBUG("No Y tiling for legacy addfb\n");
14883 return -EINVAL;
14884 }
14885 }
14886
9a8f0a12
TU
14887 /* Passed in modifier sanity checking. */
14888 switch (mode_cmd->modifier[0]) {
14889 case I915_FORMAT_MOD_Y_TILED:
14890 case I915_FORMAT_MOD_Yf_TILED:
14891 if (INTEL_INFO(dev)->gen < 9) {
14892 DRM_DEBUG("Unsupported tiling 0x%llx!\n",
14893 mode_cmd->modifier[0]);
14894 return -EINVAL;
14895 }
14896 case DRM_FORMAT_MOD_NONE:
14897 case I915_FORMAT_MOD_X_TILED:
14898 break;
14899 default:
c0f40428
JB
14900 DRM_DEBUG("Unsupported fb modifier 0x%llx!\n",
14901 mode_cmd->modifier[0]);
57cd6508 14902 return -EINVAL;
c16ed4be 14903 }
57cd6508 14904
7b49f948
VS
14905 stride_alignment = intel_fb_stride_alignment(dev_priv,
14906 mode_cmd->modifier[0],
b321803d
DL
14907 mode_cmd->pixel_format);
14908 if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
14909 DRM_DEBUG("pitch (%d) must be at least %u byte aligned\n",
14910 mode_cmd->pitches[0], stride_alignment);
57cd6508 14911 return -EINVAL;
c16ed4be 14912 }
57cd6508 14913
b321803d
DL
14914 pitch_limit = intel_fb_pitch_limit(dev, mode_cmd->modifier[0],
14915 mode_cmd->pixel_format);
a35cdaa0 14916 if (mode_cmd->pitches[0] > pitch_limit) {
b321803d
DL
14917 DRM_DEBUG("%s pitch (%u) must be at less than %d\n",
14918 mode_cmd->modifier[0] != DRM_FORMAT_MOD_NONE ?
2a80eada 14919 "tiled" : "linear",
a35cdaa0 14920 mode_cmd->pitches[0], pitch_limit);
5d7bd705 14921 return -EINVAL;
c16ed4be 14922 }
5d7bd705 14923
2a80eada 14924 if (mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED &&
c16ed4be
CW
14925 mode_cmd->pitches[0] != obj->stride) {
14926 DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
14927 mode_cmd->pitches[0], obj->stride);
5d7bd705 14928 return -EINVAL;
c16ed4be 14929 }
5d7bd705 14930
57779d06 14931 /* Reject formats not supported by any plane early. */
308e5bcb 14932 switch (mode_cmd->pixel_format) {
57779d06 14933 case DRM_FORMAT_C8:
04b3924d
VS
14934 case DRM_FORMAT_RGB565:
14935 case DRM_FORMAT_XRGB8888:
14936 case DRM_FORMAT_ARGB8888:
57779d06
VS
14937 break;
14938 case DRM_FORMAT_XRGB1555:
c16ed4be 14939 if (INTEL_INFO(dev)->gen > 3) {
4ee62c76
VS
14940 DRM_DEBUG("unsupported pixel format: %s\n",
14941 drm_get_format_name(mode_cmd->pixel_format));
57779d06 14942 return -EINVAL;
c16ed4be 14943 }
57779d06 14944 break;
57779d06 14945 case DRM_FORMAT_ABGR8888:
666a4537
WB
14946 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev) &&
14947 INTEL_INFO(dev)->gen < 9) {
6c0fd451
DL
14948 DRM_DEBUG("unsupported pixel format: %s\n",
14949 drm_get_format_name(mode_cmd->pixel_format));
14950 return -EINVAL;
14951 }
14952 break;
14953 case DRM_FORMAT_XBGR8888:
04b3924d 14954 case DRM_FORMAT_XRGB2101010:
57779d06 14955 case DRM_FORMAT_XBGR2101010:
c16ed4be 14956 if (INTEL_INFO(dev)->gen < 4) {
4ee62c76
VS
14957 DRM_DEBUG("unsupported pixel format: %s\n",
14958 drm_get_format_name(mode_cmd->pixel_format));
57779d06 14959 return -EINVAL;
c16ed4be 14960 }
b5626747 14961 break;
7531208b 14962 case DRM_FORMAT_ABGR2101010:
666a4537 14963 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
7531208b
DL
14964 DRM_DEBUG("unsupported pixel format: %s\n",
14965 drm_get_format_name(mode_cmd->pixel_format));
14966 return -EINVAL;
14967 }
14968 break;
04b3924d
VS
14969 case DRM_FORMAT_YUYV:
14970 case DRM_FORMAT_UYVY:
14971 case DRM_FORMAT_YVYU:
14972 case DRM_FORMAT_VYUY:
c16ed4be 14973 if (INTEL_INFO(dev)->gen < 5) {
4ee62c76
VS
14974 DRM_DEBUG("unsupported pixel format: %s\n",
14975 drm_get_format_name(mode_cmd->pixel_format));
57779d06 14976 return -EINVAL;
c16ed4be 14977 }
57cd6508
CW
14978 break;
14979 default:
4ee62c76
VS
14980 DRM_DEBUG("unsupported pixel format: %s\n",
14981 drm_get_format_name(mode_cmd->pixel_format));
57cd6508
CW
14982 return -EINVAL;
14983 }
14984
90f9a336
VS
14985 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
14986 if (mode_cmd->offsets[0] != 0)
14987 return -EINVAL;
14988
ec2c981e 14989 aligned_height = intel_fb_align_height(dev, mode_cmd->height,
091df6cb
DV
14990 mode_cmd->pixel_format,
14991 mode_cmd->modifier[0]);
53155c0a
DV
14992 /* FIXME drm helper for size checks (especially planar formats)? */
14993 if (obj->base.size < aligned_height * mode_cmd->pitches[0])
14994 return -EINVAL;
14995
c7d73f6a
DV
14996 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
14997 intel_fb->obj = obj;
14998
2d7a215f
VS
14999 intel_fill_fb_info(dev_priv, &intel_fb->base);
15000
79e53945
JB
15001 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
15002 if (ret) {
15003 DRM_ERROR("framebuffer init failed %d\n", ret);
15004 return ret;
15005 }
15006
0b05e1e0
VS
15007 intel_fb->obj->framebuffer_references++;
15008
79e53945
JB
15009 return 0;
15010}
15011
79e53945
JB
15012static struct drm_framebuffer *
15013intel_user_framebuffer_create(struct drm_device *dev,
15014 struct drm_file *filp,
1eb83451 15015 const struct drm_mode_fb_cmd2 *user_mode_cmd)
79e53945 15016{
dcb1394e 15017 struct drm_framebuffer *fb;
05394f39 15018 struct drm_i915_gem_object *obj;
76dc3769 15019 struct drm_mode_fb_cmd2 mode_cmd = *user_mode_cmd;
79e53945 15020
308e5bcb 15021 obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
76dc3769 15022 mode_cmd.handles[0]));
c8725226 15023 if (&obj->base == NULL)
cce13ff7 15024 return ERR_PTR(-ENOENT);
79e53945 15025
92907cbb 15026 fb = intel_framebuffer_create(dev, &mode_cmd, obj);
dcb1394e
LW
15027 if (IS_ERR(fb))
15028 drm_gem_object_unreference_unlocked(&obj->base);
15029
15030 return fb;
79e53945
JB
15031}
15032
0695726e 15033#ifndef CONFIG_DRM_FBDEV_EMULATION
0632fef6 15034static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
4520f53a
DV
15035{
15036}
15037#endif
15038
79e53945 15039static const struct drm_mode_config_funcs intel_mode_funcs = {
79e53945 15040 .fb_create = intel_user_framebuffer_create,
0632fef6 15041 .output_poll_changed = intel_fbdev_output_poll_changed,
5ee67f1c
MR
15042 .atomic_check = intel_atomic_check,
15043 .atomic_commit = intel_atomic_commit,
de419ab6
ML
15044 .atomic_state_alloc = intel_atomic_state_alloc,
15045 .atomic_state_clear = intel_atomic_state_clear,
79e53945
JB
15046};
15047
88212941
ID
15048/**
15049 * intel_init_display_hooks - initialize the display modesetting hooks
15050 * @dev_priv: device private
15051 */
15052void intel_init_display_hooks(struct drm_i915_private *dev_priv)
e70236a8 15053{
88212941 15054 if (INTEL_INFO(dev_priv)->gen >= 9) {
bc8d7dff 15055 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
5724dbd1
DL
15056 dev_priv->display.get_initial_plane_config =
15057 skylake_get_initial_plane_config;
bc8d7dff
DL
15058 dev_priv->display.crtc_compute_clock =
15059 haswell_crtc_compute_clock;
15060 dev_priv->display.crtc_enable = haswell_crtc_enable;
15061 dev_priv->display.crtc_disable = haswell_crtc_disable;
88212941 15062 } else if (HAS_DDI(dev_priv)) {
0e8ffe1b 15063 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
5724dbd1
DL
15064 dev_priv->display.get_initial_plane_config =
15065 ironlake_get_initial_plane_config;
797d0259
ACO
15066 dev_priv->display.crtc_compute_clock =
15067 haswell_crtc_compute_clock;
4f771f10
PZ
15068 dev_priv->display.crtc_enable = haswell_crtc_enable;
15069 dev_priv->display.crtc_disable = haswell_crtc_disable;
88212941 15070 } else if (HAS_PCH_SPLIT(dev_priv)) {
0e8ffe1b 15071 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
5724dbd1
DL
15072 dev_priv->display.get_initial_plane_config =
15073 ironlake_get_initial_plane_config;
3fb37703
ACO
15074 dev_priv->display.crtc_compute_clock =
15075 ironlake_crtc_compute_clock;
76e5a89c
DV
15076 dev_priv->display.crtc_enable = ironlake_crtc_enable;
15077 dev_priv->display.crtc_disable = ironlake_crtc_disable;
65b3d6a9 15078 } else if (IS_CHERRYVIEW(dev_priv)) {
89b667f8 15079 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
5724dbd1
DL
15080 dev_priv->display.get_initial_plane_config =
15081 i9xx_get_initial_plane_config;
65b3d6a9
ACO
15082 dev_priv->display.crtc_compute_clock = chv_crtc_compute_clock;
15083 dev_priv->display.crtc_enable = valleyview_crtc_enable;
15084 dev_priv->display.crtc_disable = i9xx_crtc_disable;
15085 } else if (IS_VALLEYVIEW(dev_priv)) {
15086 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
15087 dev_priv->display.get_initial_plane_config =
15088 i9xx_get_initial_plane_config;
15089 dev_priv->display.crtc_compute_clock = vlv_crtc_compute_clock;
89b667f8
JB
15090 dev_priv->display.crtc_enable = valleyview_crtc_enable;
15091 dev_priv->display.crtc_disable = i9xx_crtc_disable;
19ec6693
ACO
15092 } else if (IS_G4X(dev_priv)) {
15093 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
15094 dev_priv->display.get_initial_plane_config =
15095 i9xx_get_initial_plane_config;
15096 dev_priv->display.crtc_compute_clock = g4x_crtc_compute_clock;
15097 dev_priv->display.crtc_enable = i9xx_crtc_enable;
15098 dev_priv->display.crtc_disable = i9xx_crtc_disable;
70e8aa21
ACO
15099 } else if (IS_PINEVIEW(dev_priv)) {
15100 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
15101 dev_priv->display.get_initial_plane_config =
15102 i9xx_get_initial_plane_config;
15103 dev_priv->display.crtc_compute_clock = pnv_crtc_compute_clock;
15104 dev_priv->display.crtc_enable = i9xx_crtc_enable;
15105 dev_priv->display.crtc_disable = i9xx_crtc_disable;
81c97f52 15106 } else if (!IS_GEN2(dev_priv)) {
0e8ffe1b 15107 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
5724dbd1
DL
15108 dev_priv->display.get_initial_plane_config =
15109 i9xx_get_initial_plane_config;
d6dfee7a 15110 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
76e5a89c
DV
15111 dev_priv->display.crtc_enable = i9xx_crtc_enable;
15112 dev_priv->display.crtc_disable = i9xx_crtc_disable;
81c97f52
ACO
15113 } else {
15114 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
15115 dev_priv->display.get_initial_plane_config =
15116 i9xx_get_initial_plane_config;
15117 dev_priv->display.crtc_compute_clock = i8xx_crtc_compute_clock;
15118 dev_priv->display.crtc_enable = i9xx_crtc_enable;
15119 dev_priv->display.crtc_disable = i9xx_crtc_disable;
f564048e 15120 }
e70236a8 15121
e70236a8 15122 /* Returns the core display clock speed */
88212941 15123 if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv))
1652d19e
VS
15124 dev_priv->display.get_display_clock_speed =
15125 skylake_get_display_clock_speed;
88212941 15126 else if (IS_BROXTON(dev_priv))
acd3f3d3
BP
15127 dev_priv->display.get_display_clock_speed =
15128 broxton_get_display_clock_speed;
88212941 15129 else if (IS_BROADWELL(dev_priv))
1652d19e
VS
15130 dev_priv->display.get_display_clock_speed =
15131 broadwell_get_display_clock_speed;
88212941 15132 else if (IS_HASWELL(dev_priv))
1652d19e
VS
15133 dev_priv->display.get_display_clock_speed =
15134 haswell_get_display_clock_speed;
88212941 15135 else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
25eb05fc
JB
15136 dev_priv->display.get_display_clock_speed =
15137 valleyview_get_display_clock_speed;
88212941 15138 else if (IS_GEN5(dev_priv))
b37a6434
VS
15139 dev_priv->display.get_display_clock_speed =
15140 ilk_get_display_clock_speed;
88212941
ID
15141 else if (IS_I945G(dev_priv) || IS_BROADWATER(dev_priv) ||
15142 IS_GEN6(dev_priv) || IS_IVYBRIDGE(dev_priv))
e70236a8
JB
15143 dev_priv->display.get_display_clock_speed =
15144 i945_get_display_clock_speed;
88212941 15145 else if (IS_GM45(dev_priv))
34edce2f
VS
15146 dev_priv->display.get_display_clock_speed =
15147 gm45_get_display_clock_speed;
88212941 15148 else if (IS_CRESTLINE(dev_priv))
34edce2f
VS
15149 dev_priv->display.get_display_clock_speed =
15150 i965gm_get_display_clock_speed;
88212941 15151 else if (IS_PINEVIEW(dev_priv))
34edce2f
VS
15152 dev_priv->display.get_display_clock_speed =
15153 pnv_get_display_clock_speed;
88212941 15154 else if (IS_G33(dev_priv) || IS_G4X(dev_priv))
34edce2f
VS
15155 dev_priv->display.get_display_clock_speed =
15156 g33_get_display_clock_speed;
88212941 15157 else if (IS_I915G(dev_priv))
e70236a8
JB
15158 dev_priv->display.get_display_clock_speed =
15159 i915_get_display_clock_speed;
88212941 15160 else if (IS_I945GM(dev_priv) || IS_845G(dev_priv))
e70236a8
JB
15161 dev_priv->display.get_display_clock_speed =
15162 i9xx_misc_get_display_clock_speed;
88212941 15163 else if (IS_I915GM(dev_priv))
e70236a8
JB
15164 dev_priv->display.get_display_clock_speed =
15165 i915gm_get_display_clock_speed;
88212941 15166 else if (IS_I865G(dev_priv))
e70236a8
JB
15167 dev_priv->display.get_display_clock_speed =
15168 i865_get_display_clock_speed;
88212941 15169 else if (IS_I85X(dev_priv))
e70236a8 15170 dev_priv->display.get_display_clock_speed =
1b1d2716 15171 i85x_get_display_clock_speed;
623e01e5 15172 else { /* 830 */
88212941 15173 WARN(!IS_I830(dev_priv), "Unknown platform. Assuming 133 MHz CDCLK\n");
e70236a8
JB
15174 dev_priv->display.get_display_clock_speed =
15175 i830_get_display_clock_speed;
623e01e5 15176 }
e70236a8 15177
88212941 15178 if (IS_GEN5(dev_priv)) {
3bb11b53 15179 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
88212941 15180 } else if (IS_GEN6(dev_priv)) {
3bb11b53 15181 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
88212941 15182 } else if (IS_IVYBRIDGE(dev_priv)) {
3bb11b53
SJ
15183 /* FIXME: detect B0+ stepping and use auto training */
15184 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
88212941 15185 } else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
3bb11b53 15186 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
445e780b
VS
15187 }
15188
15189 if (IS_BROADWELL(dev_priv)) {
15190 dev_priv->display.modeset_commit_cdclk =
15191 broadwell_modeset_commit_cdclk;
15192 dev_priv->display.modeset_calc_cdclk =
15193 broadwell_modeset_calc_cdclk;
88212941 15194 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
27c329ed
ML
15195 dev_priv->display.modeset_commit_cdclk =
15196 valleyview_modeset_commit_cdclk;
15197 dev_priv->display.modeset_calc_cdclk =
15198 valleyview_modeset_calc_cdclk;
88212941 15199 } else if (IS_BROXTON(dev_priv)) {
27c329ed
ML
15200 dev_priv->display.modeset_commit_cdclk =
15201 broxton_modeset_commit_cdclk;
15202 dev_priv->display.modeset_calc_cdclk =
15203 broxton_modeset_calc_cdclk;
c89e39f3
CT
15204 } else if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv)) {
15205 dev_priv->display.modeset_commit_cdclk =
15206 skl_modeset_commit_cdclk;
15207 dev_priv->display.modeset_calc_cdclk =
15208 skl_modeset_calc_cdclk;
e70236a8 15209 }
5a21b665
DV
15210
15211 switch (INTEL_INFO(dev_priv)->gen) {
15212 case 2:
15213 dev_priv->display.queue_flip = intel_gen2_queue_flip;
15214 break;
15215
15216 case 3:
15217 dev_priv->display.queue_flip = intel_gen3_queue_flip;
15218 break;
15219
15220 case 4:
15221 case 5:
15222 dev_priv->display.queue_flip = intel_gen4_queue_flip;
15223 break;
15224
15225 case 6:
15226 dev_priv->display.queue_flip = intel_gen6_queue_flip;
15227 break;
15228 case 7:
15229 case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
15230 dev_priv->display.queue_flip = intel_gen7_queue_flip;
15231 break;
15232 case 9:
15233 /* Drop through - unsupported since execlist only. */
15234 default:
15235 /* Default just returns -ENODEV to indicate unsupported */
15236 dev_priv->display.queue_flip = intel_default_queue_flip;
15237 }
e70236a8
JB
15238}
15239
b690e96c
JB
15240/*
15241 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
15242 * resume, or other times. This quirk makes sure that's the case for
15243 * affected systems.
15244 */
0206e353 15245static void quirk_pipea_force(struct drm_device *dev)
b690e96c
JB
15246{
15247 struct drm_i915_private *dev_priv = dev->dev_private;
15248
15249 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
bc0daf48 15250 DRM_INFO("applying pipe a force quirk\n");
b690e96c
JB
15251}
15252
b6b5d049
VS
15253static void quirk_pipeb_force(struct drm_device *dev)
15254{
15255 struct drm_i915_private *dev_priv = dev->dev_private;
15256
15257 dev_priv->quirks |= QUIRK_PIPEB_FORCE;
15258 DRM_INFO("applying pipe b force quirk\n");
15259}
15260
435793df
KP
15261/*
15262 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
15263 */
15264static void quirk_ssc_force_disable(struct drm_device *dev)
15265{
15266 struct drm_i915_private *dev_priv = dev->dev_private;
15267 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
bc0daf48 15268 DRM_INFO("applying lvds SSC disable quirk\n");
435793df
KP
15269}
15270
4dca20ef 15271/*
5a15ab5b
CE
15272 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
15273 * brightness value
4dca20ef
CE
15274 */
15275static void quirk_invert_brightness(struct drm_device *dev)
15276{
15277 struct drm_i915_private *dev_priv = dev->dev_private;
15278 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
bc0daf48 15279 DRM_INFO("applying inverted panel brightness quirk\n");
435793df
KP
15280}
15281
9c72cc6f
SD
15282/* Some VBT's incorrectly indicate no backlight is present */
15283static void quirk_backlight_present(struct drm_device *dev)
15284{
15285 struct drm_i915_private *dev_priv = dev->dev_private;
15286 dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
15287 DRM_INFO("applying backlight present quirk\n");
15288}
15289
b690e96c
JB
15290struct intel_quirk {
15291 int device;
15292 int subsystem_vendor;
15293 int subsystem_device;
15294 void (*hook)(struct drm_device *dev);
15295};
15296
5f85f176
EE
15297/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
15298struct intel_dmi_quirk {
15299 void (*hook)(struct drm_device *dev);
15300 const struct dmi_system_id (*dmi_id_list)[];
15301};
15302
15303static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
15304{
15305 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
15306 return 1;
15307}
15308
15309static const struct intel_dmi_quirk intel_dmi_quirks[] = {
15310 {
15311 .dmi_id_list = &(const struct dmi_system_id[]) {
15312 {
15313 .callback = intel_dmi_reverse_brightness,
15314 .ident = "NCR Corporation",
15315 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
15316 DMI_MATCH(DMI_PRODUCT_NAME, ""),
15317 },
15318 },
15319 { } /* terminating entry */
15320 },
15321 .hook = quirk_invert_brightness,
15322 },
15323};
15324
c43b5634 15325static struct intel_quirk intel_quirks[] = {
b690e96c
JB
15326 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
15327 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
15328
b690e96c
JB
15329 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
15330 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
15331
5f080c0f
VS
15332 /* 830 needs to leave pipe A & dpll A up */
15333 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
15334
b6b5d049
VS
15335 /* 830 needs to leave pipe B & dpll B up */
15336 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
15337
435793df
KP
15338 /* Lenovo U160 cannot use SSC on LVDS */
15339 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
070d329a
MAS
15340
15341 /* Sony Vaio Y cannot use SSC on LVDS */
15342 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
5a15ab5b 15343
be505f64
AH
15344 /* Acer Aspire 5734Z must invert backlight brightness */
15345 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
15346
15347 /* Acer/eMachines G725 */
15348 { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
15349
15350 /* Acer/eMachines e725 */
15351 { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
15352
15353 /* Acer/Packard Bell NCL20 */
15354 { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
15355
15356 /* Acer Aspire 4736Z */
15357 { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
0f540c3a
JN
15358
15359 /* Acer Aspire 5336 */
15360 { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
2e93a1aa
SD
15361
15362 /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
15363 { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
d4967d8c 15364
dfb3d47b
SD
15365 /* Acer C720 Chromebook (Core i3 4005U) */
15366 { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
15367
b2a9601c 15368 /* Apple Macbook 2,1 (Core 2 T7400) */
15369 { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
15370
1b9448b0
JN
15371 /* Apple Macbook 4,1 */
15372 { 0x2a02, 0x106b, 0x00a1, quirk_backlight_present },
15373
d4967d8c
SD
15374 /* Toshiba CB35 Chromebook (Celeron 2955U) */
15375 { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
724cb06f
SD
15376
15377 /* HP Chromebook 14 (Celeron 2955U) */
15378 { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
cf6f0af9
JN
15379
15380 /* Dell Chromebook 11 */
15381 { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
9be64eee
JN
15382
15383 /* Dell Chromebook 11 (2015 version) */
15384 { 0x0a16, 0x1028, 0x0a35, quirk_backlight_present },
b690e96c
JB
15385};
15386
15387static void intel_init_quirks(struct drm_device *dev)
15388{
15389 struct pci_dev *d = dev->pdev;
15390 int i;
15391
15392 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
15393 struct intel_quirk *q = &intel_quirks[i];
15394
15395 if (d->device == q->device &&
15396 (d->subsystem_vendor == q->subsystem_vendor ||
15397 q->subsystem_vendor == PCI_ANY_ID) &&
15398 (d->subsystem_device == q->subsystem_device ||
15399 q->subsystem_device == PCI_ANY_ID))
15400 q->hook(dev);
15401 }
5f85f176
EE
15402 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
15403 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
15404 intel_dmi_quirks[i].hook(dev);
15405 }
b690e96c
JB
15406}
15407
9cce37f4
JB
15408/* Disable the VGA plane that we never use */
15409static void i915_disable_vga(struct drm_device *dev)
15410{
15411 struct drm_i915_private *dev_priv = dev->dev_private;
15412 u8 sr1;
f0f59a00 15413 i915_reg_t vga_reg = i915_vgacntrl_reg(dev);
9cce37f4 15414
2b37c616 15415 /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
9cce37f4 15416 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
3fdcf431 15417 outb(SR01, VGA_SR_INDEX);
9cce37f4
JB
15418 sr1 = inb(VGA_SR_DATA);
15419 outb(sr1 | 1<<5, VGA_SR_DATA);
15420 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
15421 udelay(300);
15422
01f5a626 15423 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
9cce37f4
JB
15424 POSTING_READ(vga_reg);
15425}
15426
f817586c
DV
15427void intel_modeset_init_hw(struct drm_device *dev)
15428{
1a617b77
ML
15429 struct drm_i915_private *dev_priv = dev->dev_private;
15430
b6283055 15431 intel_update_cdclk(dev);
1a617b77
ML
15432
15433 dev_priv->atomic_cdclk_freq = dev_priv->cdclk_freq;
15434
f817586c 15435 intel_init_clock_gating(dev);
dc97997a 15436 intel_enable_gt_powersave(dev_priv);
f817586c
DV
15437}
15438
d93c0372
MR
15439/*
15440 * Calculate what we think the watermarks should be for the state we've read
15441 * out of the hardware and then immediately program those watermarks so that
15442 * we ensure the hardware settings match our internal state.
15443 *
15444 * We can calculate what we think WM's should be by creating a duplicate of the
15445 * current state (which was constructed during hardware readout) and running it
15446 * through the atomic check code to calculate new watermark values in the
15447 * state object.
15448 */
15449static void sanitize_watermarks(struct drm_device *dev)
15450{
15451 struct drm_i915_private *dev_priv = to_i915(dev);
15452 struct drm_atomic_state *state;
15453 struct drm_crtc *crtc;
15454 struct drm_crtc_state *cstate;
15455 struct drm_modeset_acquire_ctx ctx;
15456 int ret;
15457 int i;
15458
15459 /* Only supported on platforms that use atomic watermark design */
ed4a6a7c 15460 if (!dev_priv->display.optimize_watermarks)
d93c0372
MR
15461 return;
15462
15463 /*
15464 * We need to hold connection_mutex before calling duplicate_state so
15465 * that the connector loop is protected.
15466 */
15467 drm_modeset_acquire_init(&ctx, 0);
15468retry:
0cd1262d 15469 ret = drm_modeset_lock_all_ctx(dev, &ctx);
d93c0372
MR
15470 if (ret == -EDEADLK) {
15471 drm_modeset_backoff(&ctx);
15472 goto retry;
15473 } else if (WARN_ON(ret)) {
0cd1262d 15474 goto fail;
d93c0372
MR
15475 }
15476
15477 state = drm_atomic_helper_duplicate_state(dev, &ctx);
15478 if (WARN_ON(IS_ERR(state)))
0cd1262d 15479 goto fail;
d93c0372 15480
ed4a6a7c
MR
15481 /*
15482 * Hardware readout is the only time we don't want to calculate
15483 * intermediate watermarks (since we don't trust the current
15484 * watermarks).
15485 */
15486 to_intel_atomic_state(state)->skip_intermediate_wm = true;
15487
d93c0372
MR
15488 ret = intel_atomic_check(dev, state);
15489 if (ret) {
15490 /*
15491 * If we fail here, it means that the hardware appears to be
15492 * programmed in a way that shouldn't be possible, given our
15493 * understanding of watermark requirements. This might mean a
15494 * mistake in the hardware readout code or a mistake in the
15495 * watermark calculations for a given platform. Raise a WARN
15496 * so that this is noticeable.
15497 *
15498 * If this actually happens, we'll have to just leave the
15499 * BIOS-programmed watermarks untouched and hope for the best.
15500 */
15501 WARN(true, "Could not determine valid watermarks for inherited state\n");
0cd1262d 15502 goto fail;
d93c0372
MR
15503 }
15504
15505 /* Write calculated watermark values back */
d93c0372
MR
15506 for_each_crtc_in_state(state, crtc, cstate, i) {
15507 struct intel_crtc_state *cs = to_intel_crtc_state(cstate);
15508
ed4a6a7c
MR
15509 cs->wm.need_postvbl_update = true;
15510 dev_priv->display.optimize_watermarks(cs);
d93c0372
MR
15511 }
15512
15513 drm_atomic_state_free(state);
0cd1262d 15514fail:
d93c0372
MR
15515 drm_modeset_drop_locks(&ctx);
15516 drm_modeset_acquire_fini(&ctx);
15517}
15518
79e53945
JB
15519void intel_modeset_init(struct drm_device *dev)
15520{
72e96d64
JL
15521 struct drm_i915_private *dev_priv = to_i915(dev);
15522 struct i915_ggtt *ggtt = &dev_priv->ggtt;
1fe47785 15523 int sprite, ret;
8cc87b75 15524 enum pipe pipe;
46f297fb 15525 struct intel_crtc *crtc;
79e53945
JB
15526
15527 drm_mode_config_init(dev);
15528
15529 dev->mode_config.min_width = 0;
15530 dev->mode_config.min_height = 0;
15531
019d96cb
DA
15532 dev->mode_config.preferred_depth = 24;
15533 dev->mode_config.prefer_shadow = 1;
15534
25bab385
TU
15535 dev->mode_config.allow_fb_modifiers = true;
15536
e6ecefaa 15537 dev->mode_config.funcs = &intel_mode_funcs;
79e53945 15538
b690e96c
JB
15539 intel_init_quirks(dev);
15540
1fa61106
ED
15541 intel_init_pm(dev);
15542
e3c74757
BW
15543 if (INTEL_INFO(dev)->num_pipes == 0)
15544 return;
15545
69f92f67
LW
15546 /*
15547 * There may be no VBT; and if the BIOS enabled SSC we can
15548 * just keep using it to avoid unnecessary flicker. Whereas if the
15549 * BIOS isn't using it, don't assume it will work even if the VBT
15550 * indicates as much.
15551 */
15552 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)) {
15553 bool bios_lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
15554 DREF_SSC1_ENABLE);
15555
15556 if (dev_priv->vbt.lvds_use_ssc != bios_lvds_use_ssc) {
15557 DRM_DEBUG_KMS("SSC %sabled by BIOS, overriding VBT which says %sabled\n",
15558 bios_lvds_use_ssc ? "en" : "dis",
15559 dev_priv->vbt.lvds_use_ssc ? "en" : "dis");
15560 dev_priv->vbt.lvds_use_ssc = bios_lvds_use_ssc;
15561 }
15562 }
15563
a6c45cf0
CW
15564 if (IS_GEN2(dev)) {
15565 dev->mode_config.max_width = 2048;
15566 dev->mode_config.max_height = 2048;
15567 } else if (IS_GEN3(dev)) {
5e4d6fa7
KP
15568 dev->mode_config.max_width = 4096;
15569 dev->mode_config.max_height = 4096;
79e53945 15570 } else {
a6c45cf0
CW
15571 dev->mode_config.max_width = 8192;
15572 dev->mode_config.max_height = 8192;
79e53945 15573 }
068be561 15574
dc41c154
VS
15575 if (IS_845G(dev) || IS_I865G(dev)) {
15576 dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
15577 dev->mode_config.cursor_height = 1023;
15578 } else if (IS_GEN2(dev)) {
068be561
DL
15579 dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
15580 dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
15581 } else {
15582 dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
15583 dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
15584 }
15585
72e96d64 15586 dev->mode_config.fb_base = ggtt->mappable_base;
79e53945 15587
28c97730 15588 DRM_DEBUG_KMS("%d display pipe%s available.\n",
7eb552ae
BW
15589 INTEL_INFO(dev)->num_pipes,
15590 INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
79e53945 15591
055e393f 15592 for_each_pipe(dev_priv, pipe) {
8cc87b75 15593 intel_crtc_init(dev, pipe);
3bdcfc0c 15594 for_each_sprite(dev_priv, pipe, sprite) {
1fe47785 15595 ret = intel_plane_init(dev, pipe, sprite);
7f1f3851 15596 if (ret)
06da8da2 15597 DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
1fe47785 15598 pipe_name(pipe), sprite_name(pipe, sprite), ret);
7f1f3851 15599 }
79e53945
JB
15600 }
15601
bfa7df01
VS
15602 intel_update_czclk(dev_priv);
15603 intel_update_cdclk(dev);
15604
e72f9fbf 15605 intel_shared_dpll_init(dev);
ee7b9f93 15606
b2045352
VS
15607 if (dev_priv->max_cdclk_freq == 0)
15608 intel_update_max_cdclk(dev);
15609
9cce37f4
JB
15610 /* Just disable it once at startup */
15611 i915_disable_vga(dev);
79e53945 15612 intel_setup_outputs(dev);
11be49eb 15613
6e9f798d 15614 drm_modeset_lock_all(dev);
043e9bda 15615 intel_modeset_setup_hw_state(dev);
6e9f798d 15616 drm_modeset_unlock_all(dev);
46f297fb 15617
d3fcc808 15618 for_each_intel_crtc(dev, crtc) {
eeebeac5
ML
15619 struct intel_initial_plane_config plane_config = {};
15620
46f297fb
JB
15621 if (!crtc->active)
15622 continue;
15623
46f297fb 15624 /*
46f297fb
JB
15625 * Note that reserving the BIOS fb up front prevents us
15626 * from stuffing other stolen allocations like the ring
15627 * on top. This prevents some ugliness at boot time, and
15628 * can even allow for smooth boot transitions if the BIOS
15629 * fb is large enough for the active pipe configuration.
15630 */
eeebeac5
ML
15631 dev_priv->display.get_initial_plane_config(crtc,
15632 &plane_config);
15633
15634 /*
15635 * If the fb is shared between multiple heads, we'll
15636 * just get the first one.
15637 */
15638 intel_find_initial_plane_obj(crtc, &plane_config);
46f297fb 15639 }
d93c0372
MR
15640
15641 /*
15642 * Make sure hardware watermarks really match the state we read out.
15643 * Note that we need to do this after reconstructing the BIOS fb's
15644 * since the watermark calculation done here will use pstate->fb.
15645 */
15646 sanitize_watermarks(dev);
2c7111db
CW
15647}
15648
7fad798e
DV
15649static void intel_enable_pipe_a(struct drm_device *dev)
15650{
15651 struct intel_connector *connector;
15652 struct drm_connector *crt = NULL;
15653 struct intel_load_detect_pipe load_detect_temp;
208bf9fd 15654 struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
7fad798e
DV
15655
15656 /* We can't just switch on the pipe A, we need to set things up with a
15657 * proper mode and output configuration. As a gross hack, enable pipe A
15658 * by enabling the load detect pipe once. */
3a3371ff 15659 for_each_intel_connector(dev, connector) {
7fad798e
DV
15660 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
15661 crt = &connector->base;
15662 break;
15663 }
15664 }
15665
15666 if (!crt)
15667 return;
15668
208bf9fd 15669 if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
49172fee 15670 intel_release_load_detect_pipe(crt, &load_detect_temp, ctx);
7fad798e
DV
15671}
15672
fa555837
DV
15673static bool
15674intel_check_plane_mapping(struct intel_crtc *crtc)
15675{
7eb552ae
BW
15676 struct drm_device *dev = crtc->base.dev;
15677 struct drm_i915_private *dev_priv = dev->dev_private;
649636ef 15678 u32 val;
fa555837 15679
7eb552ae 15680 if (INTEL_INFO(dev)->num_pipes == 1)
fa555837
DV
15681 return true;
15682
649636ef 15683 val = I915_READ(DSPCNTR(!crtc->plane));
fa555837
DV
15684
15685 if ((val & DISPLAY_PLANE_ENABLE) &&
15686 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
15687 return false;
15688
15689 return true;
15690}
15691
02e93c35
VS
15692static bool intel_crtc_has_encoders(struct intel_crtc *crtc)
15693{
15694 struct drm_device *dev = crtc->base.dev;
15695 struct intel_encoder *encoder;
15696
15697 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
15698 return true;
15699
15700 return false;
15701}
15702
dd756198
VS
15703static bool intel_encoder_has_connectors(struct intel_encoder *encoder)
15704{
15705 struct drm_device *dev = encoder->base.dev;
15706 struct intel_connector *connector;
15707
15708 for_each_connector_on_encoder(dev, &encoder->base, connector)
15709 return true;
15710
15711 return false;
15712}
15713
24929352
DV
15714static void intel_sanitize_crtc(struct intel_crtc *crtc)
15715{
15716 struct drm_device *dev = crtc->base.dev;
15717 struct drm_i915_private *dev_priv = dev->dev_private;
4d1de975 15718 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
24929352 15719
24929352 15720 /* Clear any frame start delays used for debugging left by the BIOS */
4d1de975
JN
15721 if (!transcoder_is_dsi(cpu_transcoder)) {
15722 i915_reg_t reg = PIPECONF(cpu_transcoder);
15723
15724 I915_WRITE(reg,
15725 I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
15726 }
24929352 15727
d3eaf884 15728 /* restore vblank interrupts to correct state */
9625604c 15729 drm_crtc_vblank_reset(&crtc->base);
d297e103 15730 if (crtc->active) {
f9cd7b88
VS
15731 struct intel_plane *plane;
15732
9625604c 15733 drm_crtc_vblank_on(&crtc->base);
f9cd7b88
VS
15734
15735 /* Disable everything but the primary plane */
15736 for_each_intel_plane_on_crtc(dev, crtc, plane) {
15737 if (plane->base.type == DRM_PLANE_TYPE_PRIMARY)
15738 continue;
15739
15740 plane->disable_plane(&plane->base, &crtc->base);
15741 }
9625604c 15742 }
d3eaf884 15743
24929352 15744 /* We need to sanitize the plane -> pipe mapping first because this will
fa555837
DV
15745 * disable the crtc (and hence change the state) if it is wrong. Note
15746 * that gen4+ has a fixed plane -> pipe mapping. */
15747 if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
24929352
DV
15748 bool plane;
15749
78108b7c
VS
15750 DRM_DEBUG_KMS("[CRTC:%d:%s] wrong plane connection detected!\n",
15751 crtc->base.base.id, crtc->base.name);
24929352
DV
15752
15753 /* Pipe has the wrong plane attached and the plane is active.
15754 * Temporarily change the plane mapping and disable everything
15755 * ... */
15756 plane = crtc->plane;
b70709a6 15757 to_intel_plane_state(crtc->base.primary->state)->visible = true;
24929352 15758 crtc->plane = !plane;
b17d48e2 15759 intel_crtc_disable_noatomic(&crtc->base);
24929352 15760 crtc->plane = plane;
24929352 15761 }
24929352 15762
7fad798e
DV
15763 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
15764 crtc->pipe == PIPE_A && !crtc->active) {
15765 /* BIOS forgot to enable pipe A, this mostly happens after
15766 * resume. Force-enable the pipe to fix this, the update_dpms
15767 * call below we restore the pipe to the right state, but leave
15768 * the required bits on. */
15769 intel_enable_pipe_a(dev);
15770 }
15771
24929352
DV
15772 /* Adjust the state of the output pipe according to whether we
15773 * have active connectors/encoders. */
842e0307 15774 if (crtc->active && !intel_crtc_has_encoders(crtc))
b17d48e2 15775 intel_crtc_disable_noatomic(&crtc->base);
24929352 15776
a3ed6aad 15777 if (crtc->active || HAS_GMCH_DISPLAY(dev)) {
4cc31489
DV
15778 /*
15779 * We start out with underrun reporting disabled to avoid races.
15780 * For correct bookkeeping mark this on active crtcs.
15781 *
c5ab3bc0
DV
15782 * Also on gmch platforms we dont have any hardware bits to
15783 * disable the underrun reporting. Which means we need to start
15784 * out with underrun reporting disabled also on inactive pipes,
15785 * since otherwise we'll complain about the garbage we read when
15786 * e.g. coming up after runtime pm.
15787 *
4cc31489
DV
15788 * No protection against concurrent access is required - at
15789 * worst a fifo underrun happens which also sets this to false.
15790 */
15791 crtc->cpu_fifo_underrun_disabled = true;
15792 crtc->pch_fifo_underrun_disabled = true;
15793 }
24929352
DV
15794}
15795
15796static void intel_sanitize_encoder(struct intel_encoder *encoder)
15797{
15798 struct intel_connector *connector;
15799 struct drm_device *dev = encoder->base.dev;
15800
15801 /* We need to check both for a crtc link (meaning that the
15802 * encoder is active and trying to read from a pipe) and the
15803 * pipe itself being active. */
15804 bool has_active_crtc = encoder->base.crtc &&
15805 to_intel_crtc(encoder->base.crtc)->active;
15806
dd756198 15807 if (intel_encoder_has_connectors(encoder) && !has_active_crtc) {
24929352
DV
15808 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
15809 encoder->base.base.id,
8e329a03 15810 encoder->base.name);
24929352
DV
15811
15812 /* Connector is active, but has no active pipe. This is
15813 * fallout from our resume register restoring. Disable
15814 * the encoder manually again. */
15815 if (encoder->base.crtc) {
15816 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
15817 encoder->base.base.id,
8e329a03 15818 encoder->base.name);
24929352 15819 encoder->disable(encoder);
a62d1497
VS
15820 if (encoder->post_disable)
15821 encoder->post_disable(encoder);
24929352 15822 }
7f1950fb 15823 encoder->base.crtc = NULL;
24929352
DV
15824
15825 /* Inconsistent output/port/pipe state happens presumably due to
15826 * a bug in one of the get_hw_state functions. Or someplace else
15827 * in our code, like the register restore mess on resume. Clamp
15828 * things to off as a safer default. */
3a3371ff 15829 for_each_intel_connector(dev, connector) {
24929352
DV
15830 if (connector->encoder != encoder)
15831 continue;
7f1950fb
EE
15832 connector->base.dpms = DRM_MODE_DPMS_OFF;
15833 connector->base.encoder = NULL;
24929352
DV
15834 }
15835 }
15836 /* Enabled encoders without active connectors will be fixed in
15837 * the crtc fixup. */
15838}
15839
04098753 15840void i915_redisable_vga_power_on(struct drm_device *dev)
0fde901f
KM
15841{
15842 struct drm_i915_private *dev_priv = dev->dev_private;
f0f59a00 15843 i915_reg_t vga_reg = i915_vgacntrl_reg(dev);
0fde901f 15844
04098753
ID
15845 if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
15846 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
15847 i915_disable_vga(dev);
15848 }
15849}
15850
15851void i915_redisable_vga(struct drm_device *dev)
15852{
15853 struct drm_i915_private *dev_priv = dev->dev_private;
15854
8dc8a27c
PZ
15855 /* This function can be called both from intel_modeset_setup_hw_state or
15856 * at a very early point in our resume sequence, where the power well
15857 * structures are not yet restored. Since this function is at a very
15858 * paranoid "someone might have enabled VGA while we were not looking"
15859 * level, just check if the power well is enabled instead of trying to
15860 * follow the "don't touch the power well if we don't need it" policy
15861 * the rest of the driver uses. */
6392f847 15862 if (!intel_display_power_get_if_enabled(dev_priv, POWER_DOMAIN_VGA))
8dc8a27c
PZ
15863 return;
15864
04098753 15865 i915_redisable_vga_power_on(dev);
6392f847
ID
15866
15867 intel_display_power_put(dev_priv, POWER_DOMAIN_VGA);
0fde901f
KM
15868}
15869
f9cd7b88 15870static bool primary_get_hw_state(struct intel_plane *plane)
98ec7739 15871{
f9cd7b88 15872 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
98ec7739 15873
f9cd7b88 15874 return I915_READ(DSPCNTR(plane->plane)) & DISPLAY_PLANE_ENABLE;
d032ffa0
ML
15875}
15876
f9cd7b88
VS
15877/* FIXME read out full plane state for all planes */
15878static void readout_plane_state(struct intel_crtc *crtc)
d032ffa0 15879{
b26d3ea3 15880 struct drm_plane *primary = crtc->base.primary;
f9cd7b88 15881 struct intel_plane_state *plane_state =
b26d3ea3 15882 to_intel_plane_state(primary->state);
d032ffa0 15883
19b8d387 15884 plane_state->visible = crtc->active &&
b26d3ea3
ML
15885 primary_get_hw_state(to_intel_plane(primary));
15886
15887 if (plane_state->visible)
15888 crtc->base.state->plane_mask |= 1 << drm_plane_index(primary);
98ec7739
VS
15889}
15890
30e984df 15891static void intel_modeset_readout_hw_state(struct drm_device *dev)
24929352
DV
15892{
15893 struct drm_i915_private *dev_priv = dev->dev_private;
15894 enum pipe pipe;
24929352
DV
15895 struct intel_crtc *crtc;
15896 struct intel_encoder *encoder;
15897 struct intel_connector *connector;
5358901f 15898 int i;
24929352 15899
565602d7
ML
15900 dev_priv->active_crtcs = 0;
15901
d3fcc808 15902 for_each_intel_crtc(dev, crtc) {
565602d7
ML
15903 struct intel_crtc_state *crtc_state = crtc->config;
15904 int pixclk = 0;
3b117c8f 15905
565602d7
ML
15906 __drm_atomic_helper_crtc_destroy_state(&crtc->base, &crtc_state->base);
15907 memset(crtc_state, 0, sizeof(*crtc_state));
15908 crtc_state->base.crtc = &crtc->base;
24929352 15909
565602d7
ML
15910 crtc_state->base.active = crtc_state->base.enable =
15911 dev_priv->display.get_pipe_config(crtc, crtc_state);
15912
15913 crtc->base.enabled = crtc_state->base.enable;
15914 crtc->active = crtc_state->base.active;
15915
15916 if (crtc_state->base.active) {
15917 dev_priv->active_crtcs |= 1 << crtc->pipe;
15918
c89e39f3 15919 if (INTEL_GEN(dev_priv) >= 9 || IS_BROADWELL(dev_priv))
565602d7 15920 pixclk = ilk_pipe_pixel_rate(crtc_state);
9558d15d 15921 else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
565602d7
ML
15922 pixclk = crtc_state->base.adjusted_mode.crtc_clock;
15923 else
15924 WARN_ON(dev_priv->display.modeset_calc_cdclk);
9558d15d
VS
15925
15926 /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
15927 if (IS_BROADWELL(dev_priv) && crtc_state->ips_enabled)
15928 pixclk = DIV_ROUND_UP(pixclk * 100, 95);
565602d7
ML
15929 }
15930
15931 dev_priv->min_pixclk[crtc->pipe] = pixclk;
b70709a6 15932
f9cd7b88 15933 readout_plane_state(crtc);
24929352 15934
78108b7c
VS
15935 DRM_DEBUG_KMS("[CRTC:%d:%s] hw state readout: %s\n",
15936 crtc->base.base.id, crtc->base.name,
24929352
DV
15937 crtc->active ? "enabled" : "disabled");
15938 }
15939
5358901f
DV
15940 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
15941 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
15942
2edd6443
ACO
15943 pll->on = pll->funcs.get_hw_state(dev_priv, pll,
15944 &pll->config.hw_state);
3e369b76 15945 pll->config.crtc_mask = 0;
d3fcc808 15946 for_each_intel_crtc(dev, crtc) {
2dd66ebd 15947 if (crtc->active && crtc->config->shared_dpll == pll)
3e369b76 15948 pll->config.crtc_mask |= 1 << crtc->pipe;
5358901f 15949 }
2dd66ebd 15950 pll->active_mask = pll->config.crtc_mask;
5358901f 15951
1e6f2ddc 15952 DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
3e369b76 15953 pll->name, pll->config.crtc_mask, pll->on);
5358901f
DV
15954 }
15955
b2784e15 15956 for_each_intel_encoder(dev, encoder) {
24929352
DV
15957 pipe = 0;
15958
15959 if (encoder->get_hw_state(encoder, &pipe)) {
045ac3b5
JB
15960 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
15961 encoder->base.crtc = &crtc->base;
6e3c9717 15962 encoder->get_config(encoder, crtc->config);
24929352
DV
15963 } else {
15964 encoder->base.crtc = NULL;
15965 }
15966
6f2bcceb 15967 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
24929352 15968 encoder->base.base.id,
8e329a03 15969 encoder->base.name,
24929352 15970 encoder->base.crtc ? "enabled" : "disabled",
6f2bcceb 15971 pipe_name(pipe));
24929352
DV
15972 }
15973
3a3371ff 15974 for_each_intel_connector(dev, connector) {
24929352
DV
15975 if (connector->get_hw_state(connector)) {
15976 connector->base.dpms = DRM_MODE_DPMS_ON;
2aa974c9
ML
15977
15978 encoder = connector->encoder;
15979 connector->base.encoder = &encoder->base;
15980
15981 if (encoder->base.crtc &&
15982 encoder->base.crtc->state->active) {
15983 /*
15984 * This has to be done during hardware readout
15985 * because anything calling .crtc_disable may
15986 * rely on the connector_mask being accurate.
15987 */
15988 encoder->base.crtc->state->connector_mask |=
15989 1 << drm_connector_index(&connector->base);
e87a52b3
ML
15990 encoder->base.crtc->state->encoder_mask |=
15991 1 << drm_encoder_index(&encoder->base);
2aa974c9
ML
15992 }
15993
24929352
DV
15994 } else {
15995 connector->base.dpms = DRM_MODE_DPMS_OFF;
15996 connector->base.encoder = NULL;
15997 }
15998 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
15999 connector->base.base.id,
c23cc417 16000 connector->base.name,
24929352
DV
16001 connector->base.encoder ? "enabled" : "disabled");
16002 }
7f4c6284
VS
16003
16004 for_each_intel_crtc(dev, crtc) {
16005 crtc->base.hwmode = crtc->config->base.adjusted_mode;
16006
16007 memset(&crtc->base.mode, 0, sizeof(crtc->base.mode));
16008 if (crtc->base.state->active) {
16009 intel_mode_from_pipe_config(&crtc->base.mode, crtc->config);
16010 intel_mode_from_pipe_config(&crtc->base.state->adjusted_mode, crtc->config);
16011 WARN_ON(drm_atomic_set_mode_for_crtc(crtc->base.state, &crtc->base.mode));
16012
16013 /*
16014 * The initial mode needs to be set in order to keep
16015 * the atomic core happy. It wants a valid mode if the
16016 * crtc's enabled, so we do the above call.
16017 *
16018 * At this point some state updated by the connectors
16019 * in their ->detect() callback has not run yet, so
16020 * no recalculation can be done yet.
16021 *
16022 * Even if we could do a recalculation and modeset
16023 * right now it would cause a double modeset if
16024 * fbdev or userspace chooses a different initial mode.
16025 *
16026 * If that happens, someone indicated they wanted a
16027 * mode change, which means it's safe to do a full
16028 * recalculation.
16029 */
16030 crtc->base.state->mode.private_flags = I915_MODE_FLAG_INHERITED;
9eca6832
VS
16031
16032 drm_calc_timestamping_constants(&crtc->base, &crtc->base.hwmode);
16033 update_scanline_offset(crtc);
7f4c6284 16034 }
e3b247da
VS
16035
16036 intel_pipe_config_sanity_check(dev_priv, crtc->config);
7f4c6284 16037 }
30e984df
DV
16038}
16039
043e9bda
ML
16040/* Scan out the current hw modeset state,
16041 * and sanitizes it to the current state
16042 */
16043static void
16044intel_modeset_setup_hw_state(struct drm_device *dev)
30e984df
DV
16045{
16046 struct drm_i915_private *dev_priv = dev->dev_private;
16047 enum pipe pipe;
30e984df
DV
16048 struct intel_crtc *crtc;
16049 struct intel_encoder *encoder;
35c95375 16050 int i;
30e984df
DV
16051
16052 intel_modeset_readout_hw_state(dev);
24929352
DV
16053
16054 /* HW state is read out, now we need to sanitize this mess. */
b2784e15 16055 for_each_intel_encoder(dev, encoder) {
24929352
DV
16056 intel_sanitize_encoder(encoder);
16057 }
16058
055e393f 16059 for_each_pipe(dev_priv, pipe) {
24929352
DV
16060 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
16061 intel_sanitize_crtc(crtc);
6e3c9717
ACO
16062 intel_dump_pipe_config(crtc, crtc->config,
16063 "[setup_hw_state]");
24929352 16064 }
9a935856 16065
d29b2f9d
ACO
16066 intel_modeset_update_connector_atomic_state(dev);
16067
35c95375
DV
16068 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
16069 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
16070
2dd66ebd 16071 if (!pll->on || pll->active_mask)
35c95375
DV
16072 continue;
16073
16074 DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
16075
2edd6443 16076 pll->funcs.disable(dev_priv, pll);
35c95375
DV
16077 pll->on = false;
16078 }
16079
666a4537 16080 if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev))
6eb1a681
VS
16081 vlv_wm_get_hw_state(dev);
16082 else if (IS_GEN9(dev))
3078999f
PB
16083 skl_wm_get_hw_state(dev);
16084 else if (HAS_PCH_SPLIT(dev))
243e6a44 16085 ilk_wm_get_hw_state(dev);
292b990e
ML
16086
16087 for_each_intel_crtc(dev, crtc) {
16088 unsigned long put_domains;
16089
74bff5f9 16090 put_domains = modeset_get_crtc_power_domains(&crtc->base, crtc->config);
292b990e
ML
16091 if (WARN_ON(put_domains))
16092 modeset_put_power_domains(dev_priv, put_domains);
16093 }
16094 intel_display_set_init_power(dev_priv, false);
010cf73d
PZ
16095
16096 intel_fbc_init_pipe_state(dev_priv);
043e9bda 16097}
7d0bc1ea 16098
043e9bda
ML
16099void intel_display_resume(struct drm_device *dev)
16100{
e2c8b870
ML
16101 struct drm_i915_private *dev_priv = to_i915(dev);
16102 struct drm_atomic_state *state = dev_priv->modeset_restore_state;
16103 struct drm_modeset_acquire_ctx ctx;
043e9bda 16104 int ret;
e2c8b870 16105 bool setup = false;
f30da187 16106
e2c8b870 16107 dev_priv->modeset_restore_state = NULL;
043e9bda 16108
ea49c9ac
ML
16109 /*
16110 * This is a cludge because with real atomic modeset mode_config.mutex
16111 * won't be taken. Unfortunately some probed state like
16112 * audio_codec_enable is still protected by mode_config.mutex, so lock
16113 * it here for now.
16114 */
16115 mutex_lock(&dev->mode_config.mutex);
e2c8b870 16116 drm_modeset_acquire_init(&ctx, 0);
043e9bda 16117
e2c8b870
ML
16118retry:
16119 ret = drm_modeset_lock_all_ctx(dev, &ctx);
043e9bda 16120
e2c8b870
ML
16121 if (ret == 0 && !setup) {
16122 setup = true;
043e9bda 16123
e2c8b870
ML
16124 intel_modeset_setup_hw_state(dev);
16125 i915_redisable_vga(dev);
45e2b5f6 16126 }
8af6cf88 16127
e2c8b870
ML
16128 if (ret == 0 && state) {
16129 struct drm_crtc_state *crtc_state;
16130 struct drm_crtc *crtc;
16131 int i;
043e9bda 16132
e2c8b870
ML
16133 state->acquire_ctx = &ctx;
16134
e3d5457c
VS
16135 /* ignore any reset values/BIOS leftovers in the WM registers */
16136 to_intel_atomic_state(state)->skip_intermediate_wm = true;
16137
e2c8b870
ML
16138 for_each_crtc_in_state(state, crtc, crtc_state, i) {
16139 /*
16140 * Force recalculation even if we restore
16141 * current state. With fast modeset this may not result
16142 * in a modeset when the state is compatible.
16143 */
16144 crtc_state->mode_changed = true;
16145 }
16146
16147 ret = drm_atomic_commit(state);
043e9bda
ML
16148 }
16149
e2c8b870
ML
16150 if (ret == -EDEADLK) {
16151 drm_modeset_backoff(&ctx);
16152 goto retry;
16153 }
043e9bda 16154
e2c8b870
ML
16155 drm_modeset_drop_locks(&ctx);
16156 drm_modeset_acquire_fini(&ctx);
ea49c9ac 16157 mutex_unlock(&dev->mode_config.mutex);
043e9bda 16158
e2c8b870
ML
16159 if (ret) {
16160 DRM_ERROR("Restoring old state failed with %i\n", ret);
16161 drm_atomic_state_free(state);
16162 }
2c7111db
CW
16163}
16164
16165void intel_modeset_gem_init(struct drm_device *dev)
16166{
dc97997a 16167 struct drm_i915_private *dev_priv = to_i915(dev);
484b41dd 16168 struct drm_crtc *c;
2ff8fde1 16169 struct drm_i915_gem_object *obj;
e0d6149b 16170 int ret;
484b41dd 16171
dc97997a 16172 intel_init_gt_powersave(dev_priv);
ae48434c 16173
1833b134 16174 intel_modeset_init_hw(dev);
02e792fb 16175
1ee8da6d 16176 intel_setup_overlay(dev_priv);
484b41dd
JB
16177
16178 /*
16179 * Make sure any fbs we allocated at startup are properly
16180 * pinned & fenced. When we do the allocation it's too early
16181 * for this.
16182 */
70e1e0ec 16183 for_each_crtc(dev, c) {
2ff8fde1
MR
16184 obj = intel_fb_obj(c->primary->fb);
16185 if (obj == NULL)
484b41dd
JB
16186 continue;
16187
e0d6149b 16188 mutex_lock(&dev->struct_mutex);
3465c580
VS
16189 ret = intel_pin_and_fence_fb_obj(c->primary->fb,
16190 c->primary->state->rotation);
e0d6149b
TU
16191 mutex_unlock(&dev->struct_mutex);
16192 if (ret) {
484b41dd
JB
16193 DRM_ERROR("failed to pin boot fb on pipe %d\n",
16194 to_intel_crtc(c)->pipe);
66e514c1 16195 drm_framebuffer_unreference(c->primary->fb);
5a21b665 16196 c->primary->fb = NULL;
36750f28 16197 c->primary->crtc = c->primary->state->crtc = NULL;
5a21b665 16198 update_state_fb(c->primary);
36750f28 16199 c->state->plane_mask &= ~(1 << drm_plane_index(c->primary));
484b41dd
JB
16200 }
16201 }
0962c3c9
VS
16202
16203 intel_backlight_register(dev);
79e53945
JB
16204}
16205
4932e2c3
ID
16206void intel_connector_unregister(struct intel_connector *intel_connector)
16207{
16208 struct drm_connector *connector = &intel_connector->base;
16209
16210 intel_panel_destroy_backlight(connector);
34ea3d38 16211 drm_connector_unregister(connector);
4932e2c3
ID
16212}
16213
79e53945
JB
16214void intel_modeset_cleanup(struct drm_device *dev)
16215{
652c393a 16216 struct drm_i915_private *dev_priv = dev->dev_private;
19c8054c 16217 struct intel_connector *connector;
652c393a 16218
dc97997a 16219 intel_disable_gt_powersave(dev_priv);
2eb5252e 16220
0962c3c9
VS
16221 intel_backlight_unregister(dev);
16222
fd0c0642
DV
16223 /*
16224 * Interrupts and polling as the first thing to avoid creating havoc.
2eb5252e 16225 * Too much stuff here (turning of connectors, ...) would
fd0c0642
DV
16226 * experience fancy races otherwise.
16227 */
2aeb7d3a 16228 intel_irq_uninstall(dev_priv);
eb21b92b 16229
fd0c0642
DV
16230 /*
16231 * Due to the hpd irq storm handling the hotplug work can re-arm the
16232 * poll handlers. Hence disable polling after hpd handling is shut down.
16233 */
f87ea761 16234 drm_kms_helper_poll_fini(dev);
fd0c0642 16235
723bfd70
JB
16236 intel_unregister_dsm_handler();
16237
c937ab3e 16238 intel_fbc_global_disable(dev_priv);
69341a5e 16239
1630fe75
CW
16240 /* flush any delayed tasks or pending work */
16241 flush_scheduled_work();
16242
db31af1d 16243 /* destroy the backlight and sysfs files before encoders/connectors */
19c8054c
JN
16244 for_each_intel_connector(dev, connector)
16245 connector->unregister(connector);
d9255d57 16246
79e53945 16247 drm_mode_config_cleanup(dev);
4d7bb011 16248
1ee8da6d 16249 intel_cleanup_overlay(dev_priv);
ae48434c 16250
dc97997a 16251 intel_cleanup_gt_powersave(dev_priv);
f5949141
DV
16252
16253 intel_teardown_gmbus(dev);
79e53945
JB
16254}
16255
f1c79df3
ZW
16256/*
16257 * Return which encoder is currently attached for connector.
16258 */
df0e9248 16259struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
79e53945 16260{
df0e9248
CW
16261 return &intel_attached_encoder(connector)->base;
16262}
f1c79df3 16263
df0e9248
CW
16264void intel_connector_attach_encoder(struct intel_connector *connector,
16265 struct intel_encoder *encoder)
16266{
16267 connector->encoder = encoder;
16268 drm_mode_connector_attach_encoder(&connector->base,
16269 &encoder->base);
79e53945 16270}
28d52043
DA
16271
16272/*
16273 * set vga decode state - true == enable VGA decode
16274 */
16275int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
16276{
16277 struct drm_i915_private *dev_priv = dev->dev_private;
a885b3cc 16278 unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
28d52043
DA
16279 u16 gmch_ctrl;
16280
75fa041d
CW
16281 if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
16282 DRM_ERROR("failed to read control word\n");
16283 return -EIO;
16284 }
16285
c0cc8a55
CW
16286 if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
16287 return 0;
16288
28d52043
DA
16289 if (state)
16290 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
16291 else
16292 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
75fa041d
CW
16293
16294 if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
16295 DRM_ERROR("failed to write control word\n");
16296 return -EIO;
16297 }
16298
28d52043
DA
16299 return 0;
16300}
c4a1d9e4 16301
c4a1d9e4 16302struct intel_display_error_state {
ff57f1b0
PZ
16303
16304 u32 power_well_driver;
16305
63b66e5b
CW
16306 int num_transcoders;
16307
c4a1d9e4
CW
16308 struct intel_cursor_error_state {
16309 u32 control;
16310 u32 position;
16311 u32 base;
16312 u32 size;
52331309 16313 } cursor[I915_MAX_PIPES];
c4a1d9e4
CW
16314
16315 struct intel_pipe_error_state {
ddf9c536 16316 bool power_domain_on;
c4a1d9e4 16317 u32 source;
f301b1e1 16318 u32 stat;
52331309 16319 } pipe[I915_MAX_PIPES];
c4a1d9e4
CW
16320
16321 struct intel_plane_error_state {
16322 u32 control;
16323 u32 stride;
16324 u32 size;
16325 u32 pos;
16326 u32 addr;
16327 u32 surface;
16328 u32 tile_offset;
52331309 16329 } plane[I915_MAX_PIPES];
63b66e5b
CW
16330
16331 struct intel_transcoder_error_state {
ddf9c536 16332 bool power_domain_on;
63b66e5b
CW
16333 enum transcoder cpu_transcoder;
16334
16335 u32 conf;
16336
16337 u32 htotal;
16338 u32 hblank;
16339 u32 hsync;
16340 u32 vtotal;
16341 u32 vblank;
16342 u32 vsync;
16343 } transcoder[4];
c4a1d9e4
CW
16344};
16345
16346struct intel_display_error_state *
c033666a 16347intel_display_capture_error_state(struct drm_i915_private *dev_priv)
c4a1d9e4 16348{
c4a1d9e4 16349 struct intel_display_error_state *error;
63b66e5b
CW
16350 int transcoders[] = {
16351 TRANSCODER_A,
16352 TRANSCODER_B,
16353 TRANSCODER_C,
16354 TRANSCODER_EDP,
16355 };
c4a1d9e4
CW
16356 int i;
16357
c033666a 16358 if (INTEL_INFO(dev_priv)->num_pipes == 0)
63b66e5b
CW
16359 return NULL;
16360
9d1cb914 16361 error = kzalloc(sizeof(*error), GFP_ATOMIC);
c4a1d9e4
CW
16362 if (error == NULL)
16363 return NULL;
16364
c033666a 16365 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
ff57f1b0
PZ
16366 error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
16367
055e393f 16368 for_each_pipe(dev_priv, i) {
ddf9c536 16369 error->pipe[i].power_domain_on =
f458ebbc
DV
16370 __intel_display_power_is_enabled(dev_priv,
16371 POWER_DOMAIN_PIPE(i));
ddf9c536 16372 if (!error->pipe[i].power_domain_on)
9d1cb914
PZ
16373 continue;
16374
5efb3e28
VS
16375 error->cursor[i].control = I915_READ(CURCNTR(i));
16376 error->cursor[i].position = I915_READ(CURPOS(i));
16377 error->cursor[i].base = I915_READ(CURBASE(i));
c4a1d9e4
CW
16378
16379 error->plane[i].control = I915_READ(DSPCNTR(i));
16380 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
c033666a 16381 if (INTEL_GEN(dev_priv) <= 3) {
51889b35 16382 error->plane[i].size = I915_READ(DSPSIZE(i));
80ca378b
PZ
16383 error->plane[i].pos = I915_READ(DSPPOS(i));
16384 }
c033666a 16385 if (INTEL_GEN(dev_priv) <= 7 && !IS_HASWELL(dev_priv))
ca291363 16386 error->plane[i].addr = I915_READ(DSPADDR(i));
c033666a 16387 if (INTEL_GEN(dev_priv) >= 4) {
c4a1d9e4
CW
16388 error->plane[i].surface = I915_READ(DSPSURF(i));
16389 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
16390 }
16391
c4a1d9e4 16392 error->pipe[i].source = I915_READ(PIPESRC(i));
f301b1e1 16393
c033666a 16394 if (HAS_GMCH_DISPLAY(dev_priv))
f301b1e1 16395 error->pipe[i].stat = I915_READ(PIPESTAT(i));
63b66e5b
CW
16396 }
16397
4d1de975 16398 /* Note: this does not include DSI transcoders. */
c033666a 16399 error->num_transcoders = INTEL_INFO(dev_priv)->num_pipes;
2d1fe073 16400 if (HAS_DDI(dev_priv))
63b66e5b
CW
16401 error->num_transcoders++; /* Account for eDP. */
16402
16403 for (i = 0; i < error->num_transcoders; i++) {
16404 enum transcoder cpu_transcoder = transcoders[i];
16405
ddf9c536 16406 error->transcoder[i].power_domain_on =
f458ebbc 16407 __intel_display_power_is_enabled(dev_priv,
38cc1daf 16408 POWER_DOMAIN_TRANSCODER(cpu_transcoder));
ddf9c536 16409 if (!error->transcoder[i].power_domain_on)
9d1cb914
PZ
16410 continue;
16411
63b66e5b
CW
16412 error->transcoder[i].cpu_transcoder = cpu_transcoder;
16413
16414 error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
16415 error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
16416 error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
16417 error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
16418 error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
16419 error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
16420 error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
c4a1d9e4
CW
16421 }
16422
16423 return error;
16424}
16425
edc3d884
MK
16426#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
16427
c4a1d9e4 16428void
edc3d884 16429intel_display_print_error_state(struct drm_i915_error_state_buf *m,
c4a1d9e4
CW
16430 struct drm_device *dev,
16431 struct intel_display_error_state *error)
16432{
055e393f 16433 struct drm_i915_private *dev_priv = dev->dev_private;
c4a1d9e4
CW
16434 int i;
16435
63b66e5b
CW
16436 if (!error)
16437 return;
16438
edc3d884 16439 err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
190be112 16440 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
edc3d884 16441 err_printf(m, "PWR_WELL_CTL2: %08x\n",
ff57f1b0 16442 error->power_well_driver);
055e393f 16443 for_each_pipe(dev_priv, i) {
edc3d884 16444 err_printf(m, "Pipe [%d]:\n", i);
ddf9c536 16445 err_printf(m, " Power: %s\n",
87ad3212 16446 onoff(error->pipe[i].power_domain_on));
edc3d884 16447 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
f301b1e1 16448 err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
edc3d884
MK
16449
16450 err_printf(m, "Plane [%d]:\n", i);
16451 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
16452 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
80ca378b 16453 if (INTEL_INFO(dev)->gen <= 3) {
edc3d884
MK
16454 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
16455 err_printf(m, " POS: %08x\n", error->plane[i].pos);
80ca378b 16456 }
4b71a570 16457 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
edc3d884 16458 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
c4a1d9e4 16459 if (INTEL_INFO(dev)->gen >= 4) {
edc3d884
MK
16460 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
16461 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
c4a1d9e4
CW
16462 }
16463
edc3d884
MK
16464 err_printf(m, "Cursor [%d]:\n", i);
16465 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
16466 err_printf(m, " POS: %08x\n", error->cursor[i].position);
16467 err_printf(m, " BASE: %08x\n", error->cursor[i].base);
c4a1d9e4 16468 }
63b66e5b
CW
16469
16470 for (i = 0; i < error->num_transcoders; i++) {
da205630 16471 err_printf(m, "CPU transcoder: %s\n",
63b66e5b 16472 transcoder_name(error->transcoder[i].cpu_transcoder));
ddf9c536 16473 err_printf(m, " Power: %s\n",
87ad3212 16474 onoff(error->transcoder[i].power_domain_on));
63b66e5b
CW
16475 err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
16476 err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
16477 err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
16478 err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
16479 err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
16480 err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
16481 err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
16482 }
c4a1d9e4 16483}