]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/gpu/drm/i915/intel_display.c
drm/i915: Convert wait_for(I915_READ(reg)) to intel_wait_for_register()
[mirror_ubuntu-bionic-kernel.git] / drivers / gpu / drm / i915 / intel_display.c
CommitLineData
79e53945
JB
1/*
2 * Copyright © 2006-2007 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 *
23 * Authors:
24 * Eric Anholt <eric@anholt.net>
25 */
26
618563e3 27#include <linux/dmi.h>
c1c7af60
JB
28#include <linux/module.h>
29#include <linux/input.h>
79e53945 30#include <linux/i2c.h>
7662c8bd 31#include <linux/kernel.h>
5a0e3ad6 32#include <linux/slab.h>
9cce37f4 33#include <linux/vgaarb.h>
e0dac65e 34#include <drm/drm_edid.h>
760285e7 35#include <drm/drmP.h>
79e53945 36#include "intel_drv.h"
760285e7 37#include <drm/i915_drm.h>
79e53945 38#include "i915_drv.h"
c37efb99 39#include "i915_gem_dmabuf.h"
db18b6a6 40#include "intel_dsi.h"
e5510fac 41#include "i915_trace.h"
319c1d42 42#include <drm/drm_atomic.h>
c196e1d6 43#include <drm/drm_atomic_helper.h>
760285e7
DH
44#include <drm/drm_dp_helper.h>
45#include <drm/drm_crtc_helper.h>
465c120c
MR
46#include <drm/drm_plane_helper.h>
47#include <drm/drm_rect.h>
c0f372b3 48#include <linux/dma_remapping.h>
fd8e058a 49#include <linux/reservation.h>
79e53945 50
5a21b665
DV
51static bool is_mmio_work(struct intel_flip_work *work)
52{
53 return work->mmio_work.func;
54}
55
465c120c 56/* Primary plane formats for gen <= 3 */
568db4f2 57static const uint32_t i8xx_primary_formats[] = {
67fe7dc5
DL
58 DRM_FORMAT_C8,
59 DRM_FORMAT_RGB565,
465c120c 60 DRM_FORMAT_XRGB1555,
67fe7dc5 61 DRM_FORMAT_XRGB8888,
465c120c
MR
62};
63
64/* Primary plane formats for gen >= 4 */
568db4f2 65static const uint32_t i965_primary_formats[] = {
6c0fd451
DL
66 DRM_FORMAT_C8,
67 DRM_FORMAT_RGB565,
68 DRM_FORMAT_XRGB8888,
69 DRM_FORMAT_XBGR8888,
70 DRM_FORMAT_XRGB2101010,
71 DRM_FORMAT_XBGR2101010,
72};
73
74static const uint32_t skl_primary_formats[] = {
67fe7dc5
DL
75 DRM_FORMAT_C8,
76 DRM_FORMAT_RGB565,
77 DRM_FORMAT_XRGB8888,
465c120c 78 DRM_FORMAT_XBGR8888,
67fe7dc5 79 DRM_FORMAT_ARGB8888,
465c120c
MR
80 DRM_FORMAT_ABGR8888,
81 DRM_FORMAT_XRGB2101010,
465c120c 82 DRM_FORMAT_XBGR2101010,
ea916ea0
KM
83 DRM_FORMAT_YUYV,
84 DRM_FORMAT_YVYU,
85 DRM_FORMAT_UYVY,
86 DRM_FORMAT_VYUY,
465c120c
MR
87};
88
3d7d6510
MR
89/* Cursor formats */
90static const uint32_t intel_cursor_formats[] = {
91 DRM_FORMAT_ARGB8888,
92};
93
f1f644dc 94static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 95 struct intel_crtc_state *pipe_config);
18442d08 96static void ironlake_pch_clock_get(struct intel_crtc *crtc,
5cec258b 97 struct intel_crtc_state *pipe_config);
f1f644dc 98
eb1bfe80
JB
99static int intel_framebuffer_init(struct drm_device *dev,
100 struct intel_framebuffer *ifb,
101 struct drm_mode_fb_cmd2 *mode_cmd,
102 struct drm_i915_gem_object *obj);
5b18e57c
DV
103static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc);
104static void intel_set_pipe_timings(struct intel_crtc *intel_crtc);
bc58be60 105static void intel_set_pipe_src_size(struct intel_crtc *intel_crtc);
29407aab 106static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
f769cd24
VK
107 struct intel_link_m_n *m_n,
108 struct intel_link_m_n *m2_n2);
29407aab 109static void ironlake_set_pipeconf(struct drm_crtc *crtc);
229fca97 110static void haswell_set_pipeconf(struct drm_crtc *crtc);
391bf048 111static void haswell_set_pipemisc(struct drm_crtc *crtc);
d288f65f 112static void vlv_prepare_pll(struct intel_crtc *crtc,
5cec258b 113 const struct intel_crtc_state *pipe_config);
d288f65f 114static void chv_prepare_pll(struct intel_crtc *crtc,
5cec258b 115 const struct intel_crtc_state *pipe_config);
5a21b665
DV
116static void intel_begin_crtc_commit(struct drm_crtc *, struct drm_crtc_state *);
117static void intel_finish_crtc_commit(struct drm_crtc *, struct drm_crtc_state *);
549e2bfb
CK
118static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
119 struct intel_crtc_state *crtc_state);
bfd16b2a
ML
120static void skylake_pfit_enable(struct intel_crtc *crtc);
121static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force);
122static void ironlake_pfit_enable(struct intel_crtc *crtc);
043e9bda 123static void intel_modeset_setup_hw_state(struct drm_device *dev);
2622a081 124static void intel_pre_disable_primary_noatomic(struct drm_crtc *crtc);
4e5ca60f 125static int ilk_max_pixel_rate(struct drm_atomic_state *state);
324513c0 126static int bxt_calc_cdclk(int max_pixclk);
e7457a9a 127
d4906093 128struct intel_limit {
4c5def93
ACO
129 struct {
130 int min, max;
131 } dot, vco, n, m, m1, m2, p, p1;
132
133 struct {
134 int dot_limit;
135 int p2_slow, p2_fast;
136 } p2;
d4906093 137};
79e53945 138
bfa7df01
VS
139/* returns HPLL frequency in kHz */
140static int valleyview_get_vco(struct drm_i915_private *dev_priv)
141{
142 int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
143
144 /* Obtain SKU information */
145 mutex_lock(&dev_priv->sb_lock);
146 hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
147 CCK_FUSE_HPLL_FREQ_MASK;
148 mutex_unlock(&dev_priv->sb_lock);
149
150 return vco_freq[hpll_freq] * 1000;
151}
152
c30fec65
VS
153int vlv_get_cck_clock(struct drm_i915_private *dev_priv,
154 const char *name, u32 reg, int ref_freq)
bfa7df01
VS
155{
156 u32 val;
157 int divider;
158
bfa7df01
VS
159 mutex_lock(&dev_priv->sb_lock);
160 val = vlv_cck_read(dev_priv, reg);
161 mutex_unlock(&dev_priv->sb_lock);
162
163 divider = val & CCK_FREQUENCY_VALUES;
164
165 WARN((val & CCK_FREQUENCY_STATUS) !=
166 (divider << CCK_FREQUENCY_STATUS_SHIFT),
167 "%s change in progress\n", name);
168
c30fec65
VS
169 return DIV_ROUND_CLOSEST(ref_freq << 1, divider + 1);
170}
171
172static int vlv_get_cck_clock_hpll(struct drm_i915_private *dev_priv,
173 const char *name, u32 reg)
174{
175 if (dev_priv->hpll_freq == 0)
176 dev_priv->hpll_freq = valleyview_get_vco(dev_priv);
177
178 return vlv_get_cck_clock(dev_priv, name, reg,
179 dev_priv->hpll_freq);
bfa7df01
VS
180}
181
e7dc33f3
VS
182static int
183intel_pch_rawclk(struct drm_i915_private *dev_priv)
d2acd215 184{
e7dc33f3
VS
185 return (I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK) * 1000;
186}
d2acd215 187
e7dc33f3
VS
188static int
189intel_vlv_hrawclk(struct drm_i915_private *dev_priv)
190{
19ab4ed3 191 /* RAWCLK_FREQ_VLV register updated from power well code */
35d38d1f
VS
192 return vlv_get_cck_clock_hpll(dev_priv, "hrawclk",
193 CCK_DISPLAY_REF_CLOCK_CONTROL);
d2acd215
DV
194}
195
e7dc33f3
VS
196static int
197intel_g4x_hrawclk(struct drm_i915_private *dev_priv)
79e50a4f 198{
79e50a4f
JN
199 uint32_t clkcfg;
200
e7dc33f3 201 /* hrawclock is 1/4 the FSB frequency */
79e50a4f
JN
202 clkcfg = I915_READ(CLKCFG);
203 switch (clkcfg & CLKCFG_FSB_MASK) {
204 case CLKCFG_FSB_400:
e7dc33f3 205 return 100000;
79e50a4f 206 case CLKCFG_FSB_533:
e7dc33f3 207 return 133333;
79e50a4f 208 case CLKCFG_FSB_667:
e7dc33f3 209 return 166667;
79e50a4f 210 case CLKCFG_FSB_800:
e7dc33f3 211 return 200000;
79e50a4f 212 case CLKCFG_FSB_1067:
e7dc33f3 213 return 266667;
79e50a4f 214 case CLKCFG_FSB_1333:
e7dc33f3 215 return 333333;
79e50a4f
JN
216 /* these two are just a guess; one of them might be right */
217 case CLKCFG_FSB_1600:
218 case CLKCFG_FSB_1600_ALT:
e7dc33f3 219 return 400000;
79e50a4f 220 default:
e7dc33f3 221 return 133333;
79e50a4f
JN
222 }
223}
224
19ab4ed3 225void intel_update_rawclk(struct drm_i915_private *dev_priv)
e7dc33f3
VS
226{
227 if (HAS_PCH_SPLIT(dev_priv))
228 dev_priv->rawclk_freq = intel_pch_rawclk(dev_priv);
229 else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
230 dev_priv->rawclk_freq = intel_vlv_hrawclk(dev_priv);
231 else if (IS_G4X(dev_priv) || IS_PINEVIEW(dev_priv))
232 dev_priv->rawclk_freq = intel_g4x_hrawclk(dev_priv);
233 else
234 return; /* no rawclk on other platforms, or no need to know it */
235
236 DRM_DEBUG_DRIVER("rawclk rate: %d kHz\n", dev_priv->rawclk_freq);
237}
238
bfa7df01
VS
239static void intel_update_czclk(struct drm_i915_private *dev_priv)
240{
666a4537 241 if (!(IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)))
bfa7df01
VS
242 return;
243
244 dev_priv->czclk_freq = vlv_get_cck_clock_hpll(dev_priv, "czclk",
245 CCK_CZ_CLOCK_CONTROL);
246
247 DRM_DEBUG_DRIVER("CZ clock rate: %d kHz\n", dev_priv->czclk_freq);
248}
249
021357ac 250static inline u32 /* units of 100MHz */
21a727b3
VS
251intel_fdi_link_freq(struct drm_i915_private *dev_priv,
252 const struct intel_crtc_state *pipe_config)
021357ac 253{
21a727b3
VS
254 if (HAS_DDI(dev_priv))
255 return pipe_config->port_clock; /* SPLL */
256 else if (IS_GEN5(dev_priv))
257 return ((I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2) * 10000;
e3b247da 258 else
21a727b3 259 return 270000;
021357ac
CW
260}
261
1b6f4958 262static const struct intel_limit intel_limits_i8xx_dac = {
0206e353 263 .dot = { .min = 25000, .max = 350000 },
9c333719 264 .vco = { .min = 908000, .max = 1512000 },
91dbe5fb 265 .n = { .min = 2, .max = 16 },
0206e353
AJ
266 .m = { .min = 96, .max = 140 },
267 .m1 = { .min = 18, .max = 26 },
268 .m2 = { .min = 6, .max = 16 },
269 .p = { .min = 4, .max = 128 },
270 .p1 = { .min = 2, .max = 33 },
273e27ca
EA
271 .p2 = { .dot_limit = 165000,
272 .p2_slow = 4, .p2_fast = 2 },
e4b36699
KP
273};
274
1b6f4958 275static const struct intel_limit intel_limits_i8xx_dvo = {
5d536e28 276 .dot = { .min = 25000, .max = 350000 },
9c333719 277 .vco = { .min = 908000, .max = 1512000 },
91dbe5fb 278 .n = { .min = 2, .max = 16 },
5d536e28
DV
279 .m = { .min = 96, .max = 140 },
280 .m1 = { .min = 18, .max = 26 },
281 .m2 = { .min = 6, .max = 16 },
282 .p = { .min = 4, .max = 128 },
283 .p1 = { .min = 2, .max = 33 },
284 .p2 = { .dot_limit = 165000,
285 .p2_slow = 4, .p2_fast = 4 },
286};
287
1b6f4958 288static const struct intel_limit intel_limits_i8xx_lvds = {
0206e353 289 .dot = { .min = 25000, .max = 350000 },
9c333719 290 .vco = { .min = 908000, .max = 1512000 },
91dbe5fb 291 .n = { .min = 2, .max = 16 },
0206e353
AJ
292 .m = { .min = 96, .max = 140 },
293 .m1 = { .min = 18, .max = 26 },
294 .m2 = { .min = 6, .max = 16 },
295 .p = { .min = 4, .max = 128 },
296 .p1 = { .min = 1, .max = 6 },
273e27ca
EA
297 .p2 = { .dot_limit = 165000,
298 .p2_slow = 14, .p2_fast = 7 },
e4b36699 299};
273e27ca 300
1b6f4958 301static const struct intel_limit intel_limits_i9xx_sdvo = {
0206e353
AJ
302 .dot = { .min = 20000, .max = 400000 },
303 .vco = { .min = 1400000, .max = 2800000 },
304 .n = { .min = 1, .max = 6 },
305 .m = { .min = 70, .max = 120 },
4f7dfb67
PJ
306 .m1 = { .min = 8, .max = 18 },
307 .m2 = { .min = 3, .max = 7 },
0206e353
AJ
308 .p = { .min = 5, .max = 80 },
309 .p1 = { .min = 1, .max = 8 },
273e27ca
EA
310 .p2 = { .dot_limit = 200000,
311 .p2_slow = 10, .p2_fast = 5 },
e4b36699
KP
312};
313
1b6f4958 314static const struct intel_limit intel_limits_i9xx_lvds = {
0206e353
AJ
315 .dot = { .min = 20000, .max = 400000 },
316 .vco = { .min = 1400000, .max = 2800000 },
317 .n = { .min = 1, .max = 6 },
318 .m = { .min = 70, .max = 120 },
53a7d2d1
PJ
319 .m1 = { .min = 8, .max = 18 },
320 .m2 = { .min = 3, .max = 7 },
0206e353
AJ
321 .p = { .min = 7, .max = 98 },
322 .p1 = { .min = 1, .max = 8 },
273e27ca
EA
323 .p2 = { .dot_limit = 112000,
324 .p2_slow = 14, .p2_fast = 7 },
e4b36699
KP
325};
326
273e27ca 327
1b6f4958 328static const struct intel_limit intel_limits_g4x_sdvo = {
273e27ca
EA
329 .dot = { .min = 25000, .max = 270000 },
330 .vco = { .min = 1750000, .max = 3500000},
331 .n = { .min = 1, .max = 4 },
332 .m = { .min = 104, .max = 138 },
333 .m1 = { .min = 17, .max = 23 },
334 .m2 = { .min = 5, .max = 11 },
335 .p = { .min = 10, .max = 30 },
336 .p1 = { .min = 1, .max = 3},
337 .p2 = { .dot_limit = 270000,
338 .p2_slow = 10,
339 .p2_fast = 10
044c7c41 340 },
e4b36699
KP
341};
342
1b6f4958 343static const struct intel_limit intel_limits_g4x_hdmi = {
273e27ca
EA
344 .dot = { .min = 22000, .max = 400000 },
345 .vco = { .min = 1750000, .max = 3500000},
346 .n = { .min = 1, .max = 4 },
347 .m = { .min = 104, .max = 138 },
348 .m1 = { .min = 16, .max = 23 },
349 .m2 = { .min = 5, .max = 11 },
350 .p = { .min = 5, .max = 80 },
351 .p1 = { .min = 1, .max = 8},
352 .p2 = { .dot_limit = 165000,
353 .p2_slow = 10, .p2_fast = 5 },
e4b36699
KP
354};
355
1b6f4958 356static const struct intel_limit intel_limits_g4x_single_channel_lvds = {
273e27ca
EA
357 .dot = { .min = 20000, .max = 115000 },
358 .vco = { .min = 1750000, .max = 3500000 },
359 .n = { .min = 1, .max = 3 },
360 .m = { .min = 104, .max = 138 },
361 .m1 = { .min = 17, .max = 23 },
362 .m2 = { .min = 5, .max = 11 },
363 .p = { .min = 28, .max = 112 },
364 .p1 = { .min = 2, .max = 8 },
365 .p2 = { .dot_limit = 0,
366 .p2_slow = 14, .p2_fast = 14
044c7c41 367 },
e4b36699
KP
368};
369
1b6f4958 370static const struct intel_limit intel_limits_g4x_dual_channel_lvds = {
273e27ca
EA
371 .dot = { .min = 80000, .max = 224000 },
372 .vco = { .min = 1750000, .max = 3500000 },
373 .n = { .min = 1, .max = 3 },
374 .m = { .min = 104, .max = 138 },
375 .m1 = { .min = 17, .max = 23 },
376 .m2 = { .min = 5, .max = 11 },
377 .p = { .min = 14, .max = 42 },
378 .p1 = { .min = 2, .max = 6 },
379 .p2 = { .dot_limit = 0,
380 .p2_slow = 7, .p2_fast = 7
044c7c41 381 },
e4b36699
KP
382};
383
1b6f4958 384static const struct intel_limit intel_limits_pineview_sdvo = {
0206e353
AJ
385 .dot = { .min = 20000, .max = 400000},
386 .vco = { .min = 1700000, .max = 3500000 },
273e27ca 387 /* Pineview's Ncounter is a ring counter */
0206e353
AJ
388 .n = { .min = 3, .max = 6 },
389 .m = { .min = 2, .max = 256 },
273e27ca 390 /* Pineview only has one combined m divider, which we treat as m2. */
0206e353
AJ
391 .m1 = { .min = 0, .max = 0 },
392 .m2 = { .min = 0, .max = 254 },
393 .p = { .min = 5, .max = 80 },
394 .p1 = { .min = 1, .max = 8 },
273e27ca
EA
395 .p2 = { .dot_limit = 200000,
396 .p2_slow = 10, .p2_fast = 5 },
e4b36699
KP
397};
398
1b6f4958 399static const struct intel_limit intel_limits_pineview_lvds = {
0206e353
AJ
400 .dot = { .min = 20000, .max = 400000 },
401 .vco = { .min = 1700000, .max = 3500000 },
402 .n = { .min = 3, .max = 6 },
403 .m = { .min = 2, .max = 256 },
404 .m1 = { .min = 0, .max = 0 },
405 .m2 = { .min = 0, .max = 254 },
406 .p = { .min = 7, .max = 112 },
407 .p1 = { .min = 1, .max = 8 },
273e27ca
EA
408 .p2 = { .dot_limit = 112000,
409 .p2_slow = 14, .p2_fast = 14 },
e4b36699
KP
410};
411
273e27ca
EA
412/* Ironlake / Sandybridge
413 *
414 * We calculate clock using (register_value + 2) for N/M1/M2, so here
415 * the range value for them is (actual_value - 2).
416 */
1b6f4958 417static const struct intel_limit intel_limits_ironlake_dac = {
273e27ca
EA
418 .dot = { .min = 25000, .max = 350000 },
419 .vco = { .min = 1760000, .max = 3510000 },
420 .n = { .min = 1, .max = 5 },
421 .m = { .min = 79, .max = 127 },
422 .m1 = { .min = 12, .max = 22 },
423 .m2 = { .min = 5, .max = 9 },
424 .p = { .min = 5, .max = 80 },
425 .p1 = { .min = 1, .max = 8 },
426 .p2 = { .dot_limit = 225000,
427 .p2_slow = 10, .p2_fast = 5 },
e4b36699
KP
428};
429
1b6f4958 430static const struct intel_limit intel_limits_ironlake_single_lvds = {
273e27ca
EA
431 .dot = { .min = 25000, .max = 350000 },
432 .vco = { .min = 1760000, .max = 3510000 },
433 .n = { .min = 1, .max = 3 },
434 .m = { .min = 79, .max = 118 },
435 .m1 = { .min = 12, .max = 22 },
436 .m2 = { .min = 5, .max = 9 },
437 .p = { .min = 28, .max = 112 },
438 .p1 = { .min = 2, .max = 8 },
439 .p2 = { .dot_limit = 225000,
440 .p2_slow = 14, .p2_fast = 14 },
b91ad0ec
ZW
441};
442
1b6f4958 443static const struct intel_limit intel_limits_ironlake_dual_lvds = {
273e27ca
EA
444 .dot = { .min = 25000, .max = 350000 },
445 .vco = { .min = 1760000, .max = 3510000 },
446 .n = { .min = 1, .max = 3 },
447 .m = { .min = 79, .max = 127 },
448 .m1 = { .min = 12, .max = 22 },
449 .m2 = { .min = 5, .max = 9 },
450 .p = { .min = 14, .max = 56 },
451 .p1 = { .min = 2, .max = 8 },
452 .p2 = { .dot_limit = 225000,
453 .p2_slow = 7, .p2_fast = 7 },
b91ad0ec
ZW
454};
455
273e27ca 456/* LVDS 100mhz refclk limits. */
1b6f4958 457static const struct intel_limit intel_limits_ironlake_single_lvds_100m = {
273e27ca
EA
458 .dot = { .min = 25000, .max = 350000 },
459 .vco = { .min = 1760000, .max = 3510000 },
460 .n = { .min = 1, .max = 2 },
461 .m = { .min = 79, .max = 126 },
462 .m1 = { .min = 12, .max = 22 },
463 .m2 = { .min = 5, .max = 9 },
464 .p = { .min = 28, .max = 112 },
0206e353 465 .p1 = { .min = 2, .max = 8 },
273e27ca
EA
466 .p2 = { .dot_limit = 225000,
467 .p2_slow = 14, .p2_fast = 14 },
b91ad0ec
ZW
468};
469
1b6f4958 470static const struct intel_limit intel_limits_ironlake_dual_lvds_100m = {
273e27ca
EA
471 .dot = { .min = 25000, .max = 350000 },
472 .vco = { .min = 1760000, .max = 3510000 },
473 .n = { .min = 1, .max = 3 },
474 .m = { .min = 79, .max = 126 },
475 .m1 = { .min = 12, .max = 22 },
476 .m2 = { .min = 5, .max = 9 },
477 .p = { .min = 14, .max = 42 },
0206e353 478 .p1 = { .min = 2, .max = 6 },
273e27ca
EA
479 .p2 = { .dot_limit = 225000,
480 .p2_slow = 7, .p2_fast = 7 },
4547668a
ZY
481};
482
1b6f4958 483static const struct intel_limit intel_limits_vlv = {
f01b7962
VS
484 /*
485 * These are the data rate limits (measured in fast clocks)
486 * since those are the strictest limits we have. The fast
487 * clock and actual rate limits are more relaxed, so checking
488 * them would make no difference.
489 */
490 .dot = { .min = 25000 * 5, .max = 270000 * 5 },
75e53986 491 .vco = { .min = 4000000, .max = 6000000 },
a0c4da24 492 .n = { .min = 1, .max = 7 },
a0c4da24
JB
493 .m1 = { .min = 2, .max = 3 },
494 .m2 = { .min = 11, .max = 156 },
b99ab663 495 .p1 = { .min = 2, .max = 3 },
5fdc9c49 496 .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
a0c4da24
JB
497};
498
1b6f4958 499static const struct intel_limit intel_limits_chv = {
ef9348c8
CML
500 /*
501 * These are the data rate limits (measured in fast clocks)
502 * since those are the strictest limits we have. The fast
503 * clock and actual rate limits are more relaxed, so checking
504 * them would make no difference.
505 */
506 .dot = { .min = 25000 * 5, .max = 540000 * 5},
17fe1021 507 .vco = { .min = 4800000, .max = 6480000 },
ef9348c8
CML
508 .n = { .min = 1, .max = 1 },
509 .m1 = { .min = 2, .max = 2 },
510 .m2 = { .min = 24 << 22, .max = 175 << 22 },
511 .p1 = { .min = 2, .max = 4 },
512 .p2 = { .p2_slow = 1, .p2_fast = 14 },
513};
514
1b6f4958 515static const struct intel_limit intel_limits_bxt = {
5ab7b0b7
ID
516 /* FIXME: find real dot limits */
517 .dot = { .min = 0, .max = INT_MAX },
e6292556 518 .vco = { .min = 4800000, .max = 6700000 },
5ab7b0b7
ID
519 .n = { .min = 1, .max = 1 },
520 .m1 = { .min = 2, .max = 2 },
521 /* FIXME: find real m2 limits */
522 .m2 = { .min = 2 << 22, .max = 255 << 22 },
523 .p1 = { .min = 2, .max = 4 },
524 .p2 = { .p2_slow = 1, .p2_fast = 20 },
525};
526
cdba954e
ACO
527static bool
528needs_modeset(struct drm_crtc_state *state)
529{
fc596660 530 return drm_atomic_crtc_needs_modeset(state);
cdba954e
ACO
531}
532
e0638cdf
PZ
533/**
534 * Returns whether any output on the specified pipe is of the specified type
535 */
4093561b 536bool intel_pipe_has_type(struct intel_crtc *crtc, enum intel_output_type type)
e0638cdf 537{
409ee761 538 struct drm_device *dev = crtc->base.dev;
e0638cdf
PZ
539 struct intel_encoder *encoder;
540
409ee761 541 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
e0638cdf
PZ
542 if (encoder->type == type)
543 return true;
544
545 return false;
546}
547
d0737e1d
ACO
548/**
549 * Returns whether any output on the specified pipe will have the specified
550 * type after a staged modeset is complete, i.e., the same as
551 * intel_pipe_has_type() but looking at encoder->new_crtc instead of
552 * encoder->crtc.
553 */
a93e255f
ACO
554static bool intel_pipe_will_have_type(const struct intel_crtc_state *crtc_state,
555 int type)
d0737e1d 556{
a93e255f 557 struct drm_atomic_state *state = crtc_state->base.state;
da3ced29 558 struct drm_connector *connector;
a93e255f 559 struct drm_connector_state *connector_state;
d0737e1d 560 struct intel_encoder *encoder;
a93e255f
ACO
561 int i, num_connectors = 0;
562
da3ced29 563 for_each_connector_in_state(state, connector, connector_state, i) {
a93e255f
ACO
564 if (connector_state->crtc != crtc_state->base.crtc)
565 continue;
566
567 num_connectors++;
d0737e1d 568
a93e255f
ACO
569 encoder = to_intel_encoder(connector_state->best_encoder);
570 if (encoder->type == type)
d0737e1d 571 return true;
a93e255f
ACO
572 }
573
574 WARN_ON(num_connectors == 0);
d0737e1d
ACO
575
576 return false;
577}
578
dccbea3b
ID
579/*
580 * Platform specific helpers to calculate the port PLL loopback- (clock.m),
581 * and post-divider (clock.p) values, pre- (clock.vco) and post-divided fast
582 * (clock.dot) clock rates. This fast dot clock is fed to the port's IO logic.
583 * The helpers' return value is the rate of the clock that is fed to the
584 * display engine's pipe which can be the above fast dot clock rate or a
585 * divided-down version of it.
586 */
f2b115e6 587/* m1 is reserved as 0 in Pineview, n is a ring counter */
9e2c8475 588static int pnv_calc_dpll_params(int refclk, struct dpll *clock)
79e53945 589{
2177832f
SL
590 clock->m = clock->m2 + 2;
591 clock->p = clock->p1 * clock->p2;
ed5ca77e 592 if (WARN_ON(clock->n == 0 || clock->p == 0))
dccbea3b 593 return 0;
fb03ac01
VS
594 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
595 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
dccbea3b
ID
596
597 return clock->dot;
2177832f
SL
598}
599
7429e9d4
DV
600static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
601{
602 return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
603}
604
9e2c8475 605static int i9xx_calc_dpll_params(int refclk, struct dpll *clock)
2177832f 606{
7429e9d4 607 clock->m = i9xx_dpll_compute_m(clock);
79e53945 608 clock->p = clock->p1 * clock->p2;
ed5ca77e 609 if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
dccbea3b 610 return 0;
fb03ac01
VS
611 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
612 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
dccbea3b
ID
613
614 return clock->dot;
79e53945
JB
615}
616
9e2c8475 617static int vlv_calc_dpll_params(int refclk, struct dpll *clock)
589eca67
ID
618{
619 clock->m = clock->m1 * clock->m2;
620 clock->p = clock->p1 * clock->p2;
621 if (WARN_ON(clock->n == 0 || clock->p == 0))
dccbea3b 622 return 0;
589eca67
ID
623 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
624 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
dccbea3b
ID
625
626 return clock->dot / 5;
589eca67
ID
627}
628
9e2c8475 629int chv_calc_dpll_params(int refclk, struct dpll *clock)
ef9348c8
CML
630{
631 clock->m = clock->m1 * clock->m2;
632 clock->p = clock->p1 * clock->p2;
633 if (WARN_ON(clock->n == 0 || clock->p == 0))
dccbea3b 634 return 0;
ef9348c8
CML
635 clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
636 clock->n << 22);
637 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
dccbea3b
ID
638
639 return clock->dot / 5;
ef9348c8
CML
640}
641
7c04d1d9 642#define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
79e53945
JB
643/**
644 * Returns whether the given set of divisors are valid for a given refclk with
645 * the given connectors.
646 */
647
1b894b59 648static bool intel_PLL_is_valid(struct drm_device *dev,
1b6f4958 649 const struct intel_limit *limit,
9e2c8475 650 const struct dpll *clock)
79e53945 651{
f01b7962
VS
652 if (clock->n < limit->n.min || limit->n.max < clock->n)
653 INTELPllInvalid("n out of range\n");
79e53945 654 if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
0206e353 655 INTELPllInvalid("p1 out of range\n");
79e53945 656 if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
0206e353 657 INTELPllInvalid("m2 out of range\n");
79e53945 658 if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
0206e353 659 INTELPllInvalid("m1 out of range\n");
f01b7962 660
666a4537
WB
661 if (!IS_PINEVIEW(dev) && !IS_VALLEYVIEW(dev) &&
662 !IS_CHERRYVIEW(dev) && !IS_BROXTON(dev))
f01b7962
VS
663 if (clock->m1 <= clock->m2)
664 INTELPllInvalid("m1 <= m2\n");
665
666a4537 666 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev) && !IS_BROXTON(dev)) {
f01b7962
VS
667 if (clock->p < limit->p.min || limit->p.max < clock->p)
668 INTELPllInvalid("p out of range\n");
669 if (clock->m < limit->m.min || limit->m.max < clock->m)
670 INTELPllInvalid("m out of range\n");
671 }
672
79e53945 673 if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
0206e353 674 INTELPllInvalid("vco out of range\n");
79e53945
JB
675 /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
676 * connector, etc., rather than just a single range.
677 */
678 if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
0206e353 679 INTELPllInvalid("dot out of range\n");
79e53945
JB
680
681 return true;
682}
683
3b1429d9 684static int
1b6f4958 685i9xx_select_p2_div(const struct intel_limit *limit,
3b1429d9
VS
686 const struct intel_crtc_state *crtc_state,
687 int target)
79e53945 688{
3b1429d9 689 struct drm_device *dev = crtc_state->base.crtc->dev;
79e53945 690
a93e255f 691 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
79e53945 692 /*
a210b028
DV
693 * For LVDS just rely on its current settings for dual-channel.
694 * We haven't figured out how to reliably set up different
695 * single/dual channel state, if we even can.
79e53945 696 */
1974cad0 697 if (intel_is_dual_link_lvds(dev))
3b1429d9 698 return limit->p2.p2_fast;
79e53945 699 else
3b1429d9 700 return limit->p2.p2_slow;
79e53945
JB
701 } else {
702 if (target < limit->p2.dot_limit)
3b1429d9 703 return limit->p2.p2_slow;
79e53945 704 else
3b1429d9 705 return limit->p2.p2_fast;
79e53945 706 }
3b1429d9
VS
707}
708
70e8aa21
ACO
709/*
710 * Returns a set of divisors for the desired target clock with the given
711 * refclk, or FALSE. The returned values represent the clock equation:
712 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
713 *
714 * Target and reference clocks are specified in kHz.
715 *
716 * If match_clock is provided, then best_clock P divider must match the P
717 * divider from @match_clock used for LVDS downclocking.
718 */
3b1429d9 719static bool
1b6f4958 720i9xx_find_best_dpll(const struct intel_limit *limit,
3b1429d9 721 struct intel_crtc_state *crtc_state,
9e2c8475
ACO
722 int target, int refclk, struct dpll *match_clock,
723 struct dpll *best_clock)
3b1429d9
VS
724{
725 struct drm_device *dev = crtc_state->base.crtc->dev;
9e2c8475 726 struct dpll clock;
3b1429d9 727 int err = target;
79e53945 728
0206e353 729 memset(best_clock, 0, sizeof(*best_clock));
79e53945 730
3b1429d9
VS
731 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
732
42158660
ZY
733 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
734 clock.m1++) {
735 for (clock.m2 = limit->m2.min;
736 clock.m2 <= limit->m2.max; clock.m2++) {
c0efc387 737 if (clock.m2 >= clock.m1)
42158660
ZY
738 break;
739 for (clock.n = limit->n.min;
740 clock.n <= limit->n.max; clock.n++) {
741 for (clock.p1 = limit->p1.min;
742 clock.p1 <= limit->p1.max; clock.p1++) {
79e53945
JB
743 int this_err;
744
dccbea3b 745 i9xx_calc_dpll_params(refclk, &clock);
ac58c3f0
DV
746 if (!intel_PLL_is_valid(dev, limit,
747 &clock))
748 continue;
749 if (match_clock &&
750 clock.p != match_clock->p)
751 continue;
752
753 this_err = abs(clock.dot - target);
754 if (this_err < err) {
755 *best_clock = clock;
756 err = this_err;
757 }
758 }
759 }
760 }
761 }
762
763 return (err != target);
764}
765
70e8aa21
ACO
766/*
767 * Returns a set of divisors for the desired target clock with the given
768 * refclk, or FALSE. The returned values represent the clock equation:
769 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
770 *
771 * Target and reference clocks are specified in kHz.
772 *
773 * If match_clock is provided, then best_clock P divider must match the P
774 * divider from @match_clock used for LVDS downclocking.
775 */
ac58c3f0 776static bool
1b6f4958 777pnv_find_best_dpll(const struct intel_limit *limit,
a93e255f 778 struct intel_crtc_state *crtc_state,
9e2c8475
ACO
779 int target, int refclk, struct dpll *match_clock,
780 struct dpll *best_clock)
79e53945 781{
3b1429d9 782 struct drm_device *dev = crtc_state->base.crtc->dev;
9e2c8475 783 struct dpll clock;
79e53945
JB
784 int err = target;
785
0206e353 786 memset(best_clock, 0, sizeof(*best_clock));
79e53945 787
3b1429d9
VS
788 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
789
42158660
ZY
790 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
791 clock.m1++) {
792 for (clock.m2 = limit->m2.min;
793 clock.m2 <= limit->m2.max; clock.m2++) {
42158660
ZY
794 for (clock.n = limit->n.min;
795 clock.n <= limit->n.max; clock.n++) {
796 for (clock.p1 = limit->p1.min;
797 clock.p1 <= limit->p1.max; clock.p1++) {
79e53945
JB
798 int this_err;
799
dccbea3b 800 pnv_calc_dpll_params(refclk, &clock);
1b894b59
CW
801 if (!intel_PLL_is_valid(dev, limit,
802 &clock))
79e53945 803 continue;
cec2f356
SP
804 if (match_clock &&
805 clock.p != match_clock->p)
806 continue;
79e53945
JB
807
808 this_err = abs(clock.dot - target);
809 if (this_err < err) {
810 *best_clock = clock;
811 err = this_err;
812 }
813 }
814 }
815 }
816 }
817
818 return (err != target);
819}
820
997c030c
ACO
821/*
822 * Returns a set of divisors for the desired target clock with the given
823 * refclk, or FALSE. The returned values represent the clock equation:
824 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
70e8aa21
ACO
825 *
826 * Target and reference clocks are specified in kHz.
827 *
828 * If match_clock is provided, then best_clock P divider must match the P
829 * divider from @match_clock used for LVDS downclocking.
997c030c 830 */
d4906093 831static bool
1b6f4958 832g4x_find_best_dpll(const struct intel_limit *limit,
a93e255f 833 struct intel_crtc_state *crtc_state,
9e2c8475
ACO
834 int target, int refclk, struct dpll *match_clock,
835 struct dpll *best_clock)
d4906093 836{
3b1429d9 837 struct drm_device *dev = crtc_state->base.crtc->dev;
9e2c8475 838 struct dpll clock;
d4906093 839 int max_n;
3b1429d9 840 bool found = false;
6ba770dc
AJ
841 /* approximately equals target * 0.00585 */
842 int err_most = (target >> 8) + (target >> 9);
d4906093
ML
843
844 memset(best_clock, 0, sizeof(*best_clock));
3b1429d9
VS
845
846 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
847
d4906093 848 max_n = limit->n.max;
f77f13e2 849 /* based on hardware requirement, prefer smaller n to precision */
d4906093 850 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
f77f13e2 851 /* based on hardware requirement, prefere larger m1,m2 */
d4906093
ML
852 for (clock.m1 = limit->m1.max;
853 clock.m1 >= limit->m1.min; clock.m1--) {
854 for (clock.m2 = limit->m2.max;
855 clock.m2 >= limit->m2.min; clock.m2--) {
856 for (clock.p1 = limit->p1.max;
857 clock.p1 >= limit->p1.min; clock.p1--) {
858 int this_err;
859
dccbea3b 860 i9xx_calc_dpll_params(refclk, &clock);
1b894b59
CW
861 if (!intel_PLL_is_valid(dev, limit,
862 &clock))
d4906093 863 continue;
1b894b59
CW
864
865 this_err = abs(clock.dot - target);
d4906093
ML
866 if (this_err < err_most) {
867 *best_clock = clock;
868 err_most = this_err;
869 max_n = clock.n;
870 found = true;
871 }
872 }
873 }
874 }
875 }
2c07245f
ZW
876 return found;
877}
878
d5dd62bd
ID
879/*
880 * Check if the calculated PLL configuration is more optimal compared to the
881 * best configuration and error found so far. Return the calculated error.
882 */
883static bool vlv_PLL_is_optimal(struct drm_device *dev, int target_freq,
9e2c8475
ACO
884 const struct dpll *calculated_clock,
885 const struct dpll *best_clock,
d5dd62bd
ID
886 unsigned int best_error_ppm,
887 unsigned int *error_ppm)
888{
9ca3ba01
ID
889 /*
890 * For CHV ignore the error and consider only the P value.
891 * Prefer a bigger P value based on HW requirements.
892 */
893 if (IS_CHERRYVIEW(dev)) {
894 *error_ppm = 0;
895
896 return calculated_clock->p > best_clock->p;
897 }
898
24be4e46
ID
899 if (WARN_ON_ONCE(!target_freq))
900 return false;
901
d5dd62bd
ID
902 *error_ppm = div_u64(1000000ULL *
903 abs(target_freq - calculated_clock->dot),
904 target_freq);
905 /*
906 * Prefer a better P value over a better (smaller) error if the error
907 * is small. Ensure this preference for future configurations too by
908 * setting the error to 0.
909 */
910 if (*error_ppm < 100 && calculated_clock->p > best_clock->p) {
911 *error_ppm = 0;
912
913 return true;
914 }
915
916 return *error_ppm + 10 < best_error_ppm;
917}
918
65b3d6a9
ACO
919/*
920 * Returns a set of divisors for the desired target clock with the given
921 * refclk, or FALSE. The returned values represent the clock equation:
922 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
923 */
a0c4da24 924static bool
1b6f4958 925vlv_find_best_dpll(const struct intel_limit *limit,
a93e255f 926 struct intel_crtc_state *crtc_state,
9e2c8475
ACO
927 int target, int refclk, struct dpll *match_clock,
928 struct dpll *best_clock)
a0c4da24 929{
a93e255f 930 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
a919ff14 931 struct drm_device *dev = crtc->base.dev;
9e2c8475 932 struct dpll clock;
69e4f900 933 unsigned int bestppm = 1000000;
27e639bf
VS
934 /* min update 19.2 MHz */
935 int max_n = min(limit->n.max, refclk / 19200);
49e497ef 936 bool found = false;
a0c4da24 937
6b4bf1c4
VS
938 target *= 5; /* fast clock */
939
940 memset(best_clock, 0, sizeof(*best_clock));
a0c4da24
JB
941
942 /* based on hardware requirement, prefer smaller n to precision */
27e639bf 943 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
811bbf05 944 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
889059d8 945 for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
c1a9ae43 946 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
6b4bf1c4 947 clock.p = clock.p1 * clock.p2;
a0c4da24 948 /* based on hardware requirement, prefer bigger m1,m2 values */
6b4bf1c4 949 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
d5dd62bd 950 unsigned int ppm;
69e4f900 951
6b4bf1c4
VS
952 clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
953 refclk * clock.m1);
954
dccbea3b 955 vlv_calc_dpll_params(refclk, &clock);
43b0ac53 956
f01b7962
VS
957 if (!intel_PLL_is_valid(dev, limit,
958 &clock))
43b0ac53
VS
959 continue;
960
d5dd62bd
ID
961 if (!vlv_PLL_is_optimal(dev, target,
962 &clock,
963 best_clock,
964 bestppm, &ppm))
965 continue;
6b4bf1c4 966
d5dd62bd
ID
967 *best_clock = clock;
968 bestppm = ppm;
969 found = true;
a0c4da24
JB
970 }
971 }
972 }
973 }
a0c4da24 974
49e497ef 975 return found;
a0c4da24 976}
a4fc5ed6 977
65b3d6a9
ACO
978/*
979 * Returns a set of divisors for the desired target clock with the given
980 * refclk, or FALSE. The returned values represent the clock equation:
981 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
982 */
ef9348c8 983static bool
1b6f4958 984chv_find_best_dpll(const struct intel_limit *limit,
a93e255f 985 struct intel_crtc_state *crtc_state,
9e2c8475
ACO
986 int target, int refclk, struct dpll *match_clock,
987 struct dpll *best_clock)
ef9348c8 988{
a93e255f 989 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
a919ff14 990 struct drm_device *dev = crtc->base.dev;
9ca3ba01 991 unsigned int best_error_ppm;
9e2c8475 992 struct dpll clock;
ef9348c8
CML
993 uint64_t m2;
994 int found = false;
995
996 memset(best_clock, 0, sizeof(*best_clock));
9ca3ba01 997 best_error_ppm = 1000000;
ef9348c8
CML
998
999 /*
1000 * Based on hardware doc, the n always set to 1, and m1 always
1001 * set to 2. If requires to support 200Mhz refclk, we need to
1002 * revisit this because n may not 1 anymore.
1003 */
1004 clock.n = 1, clock.m1 = 2;
1005 target *= 5; /* fast clock */
1006
1007 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
1008 for (clock.p2 = limit->p2.p2_fast;
1009 clock.p2 >= limit->p2.p2_slow;
1010 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
9ca3ba01 1011 unsigned int error_ppm;
ef9348c8
CML
1012
1013 clock.p = clock.p1 * clock.p2;
1014
1015 m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
1016 clock.n) << 22, refclk * clock.m1);
1017
1018 if (m2 > INT_MAX/clock.m1)
1019 continue;
1020
1021 clock.m2 = m2;
1022
dccbea3b 1023 chv_calc_dpll_params(refclk, &clock);
ef9348c8
CML
1024
1025 if (!intel_PLL_is_valid(dev, limit, &clock))
1026 continue;
1027
9ca3ba01
ID
1028 if (!vlv_PLL_is_optimal(dev, target, &clock, best_clock,
1029 best_error_ppm, &error_ppm))
1030 continue;
1031
1032 *best_clock = clock;
1033 best_error_ppm = error_ppm;
1034 found = true;
ef9348c8
CML
1035 }
1036 }
1037
1038 return found;
1039}
1040
5ab7b0b7 1041bool bxt_find_best_dpll(struct intel_crtc_state *crtc_state, int target_clock,
9e2c8475 1042 struct dpll *best_clock)
5ab7b0b7 1043{
65b3d6a9 1044 int refclk = 100000;
1b6f4958 1045 const struct intel_limit *limit = &intel_limits_bxt;
5ab7b0b7 1046
65b3d6a9 1047 return chv_find_best_dpll(limit, crtc_state,
5ab7b0b7
ID
1048 target_clock, refclk, NULL, best_clock);
1049}
1050
20ddf665
VS
1051bool intel_crtc_active(struct drm_crtc *crtc)
1052{
1053 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1054
1055 /* Be paranoid as we can arrive here with only partial
1056 * state retrieved from the hardware during setup.
1057 *
241bfc38 1058 * We can ditch the adjusted_mode.crtc_clock check as soon
20ddf665
VS
1059 * as Haswell has gained clock readout/fastboot support.
1060 *
66e514c1 1061 * We can ditch the crtc->primary->fb check as soon as we can
20ddf665 1062 * properly reconstruct framebuffers.
c3d1f436
MR
1063 *
1064 * FIXME: The intel_crtc->active here should be switched to
1065 * crtc->state->active once we have proper CRTC states wired up
1066 * for atomic.
20ddf665 1067 */
c3d1f436 1068 return intel_crtc->active && crtc->primary->state->fb &&
6e3c9717 1069 intel_crtc->config->base.adjusted_mode.crtc_clock;
20ddf665
VS
1070}
1071
a5c961d1
PZ
1072enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
1073 enum pipe pipe)
1074{
1075 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
1076 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1077
6e3c9717 1078 return intel_crtc->config->cpu_transcoder;
a5c961d1
PZ
1079}
1080
fbf49ea2
VS
1081static bool pipe_dsl_stopped(struct drm_device *dev, enum pipe pipe)
1082{
1083 struct drm_i915_private *dev_priv = dev->dev_private;
f0f59a00 1084 i915_reg_t reg = PIPEDSL(pipe);
fbf49ea2
VS
1085 u32 line1, line2;
1086 u32 line_mask;
1087
1088 if (IS_GEN2(dev))
1089 line_mask = DSL_LINEMASK_GEN2;
1090 else
1091 line_mask = DSL_LINEMASK_GEN3;
1092
1093 line1 = I915_READ(reg) & line_mask;
6adfb1ef 1094 msleep(5);
fbf49ea2
VS
1095 line2 = I915_READ(reg) & line_mask;
1096
1097 return line1 == line2;
1098}
1099
ab7ad7f6
KP
1100/*
1101 * intel_wait_for_pipe_off - wait for pipe to turn off
575f7ab7 1102 * @crtc: crtc whose pipe to wait for
9d0498a2
JB
1103 *
1104 * After disabling a pipe, we can't wait for vblank in the usual way,
1105 * spinning on the vblank interrupt status bit, since we won't actually
1106 * see an interrupt when the pipe is disabled.
1107 *
ab7ad7f6
KP
1108 * On Gen4 and above:
1109 * wait for the pipe register state bit to turn off
1110 *
1111 * Otherwise:
1112 * wait for the display line value to settle (it usually
1113 * ends up stopping at the start of the next frame).
58e10eb9 1114 *
9d0498a2 1115 */
575f7ab7 1116static void intel_wait_for_pipe_off(struct intel_crtc *crtc)
9d0498a2 1117{
575f7ab7 1118 struct drm_device *dev = crtc->base.dev;
9d0498a2 1119 struct drm_i915_private *dev_priv = dev->dev_private;
6e3c9717 1120 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
575f7ab7 1121 enum pipe pipe = crtc->pipe;
ab7ad7f6
KP
1122
1123 if (INTEL_INFO(dev)->gen >= 4) {
f0f59a00 1124 i915_reg_t reg = PIPECONF(cpu_transcoder);
ab7ad7f6
KP
1125
1126 /* Wait for the Pipe State to go off */
b8511f53
CW
1127 if (intel_wait_for_register(dev_priv,
1128 reg, I965_PIPECONF_ACTIVE, 0,
1129 100))
284637d9 1130 WARN(1, "pipe_off wait timed out\n");
ab7ad7f6 1131 } else {
ab7ad7f6 1132 /* Wait for the display line to settle */
fbf49ea2 1133 if (wait_for(pipe_dsl_stopped(dev, pipe), 100))
284637d9 1134 WARN(1, "pipe_off wait timed out\n");
ab7ad7f6 1135 }
79e53945
JB
1136}
1137
b24e7179 1138/* Only for pre-ILK configs */
55607e8a
DV
1139void assert_pll(struct drm_i915_private *dev_priv,
1140 enum pipe pipe, bool state)
b24e7179 1141{
b24e7179
JB
1142 u32 val;
1143 bool cur_state;
1144
649636ef 1145 val = I915_READ(DPLL(pipe));
b24e7179 1146 cur_state = !!(val & DPLL_VCO_ENABLE);
e2c719b7 1147 I915_STATE_WARN(cur_state != state,
b24e7179 1148 "PLL state assertion failure (expected %s, current %s)\n",
87ad3212 1149 onoff(state), onoff(cur_state));
b24e7179 1150}
b24e7179 1151
23538ef1 1152/* XXX: the dsi pll is shared between MIPI DSI ports */
8563b1e8 1153void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
23538ef1
JN
1154{
1155 u32 val;
1156 bool cur_state;
1157
a580516d 1158 mutex_lock(&dev_priv->sb_lock);
23538ef1 1159 val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
a580516d 1160 mutex_unlock(&dev_priv->sb_lock);
23538ef1
JN
1161
1162 cur_state = val & DSI_PLL_VCO_EN;
e2c719b7 1163 I915_STATE_WARN(cur_state != state,
23538ef1 1164 "DSI PLL state assertion failure (expected %s, current %s)\n",
87ad3212 1165 onoff(state), onoff(cur_state));
23538ef1 1166}
23538ef1 1167
040484af
JB
1168static void assert_fdi_tx(struct drm_i915_private *dev_priv,
1169 enum pipe pipe, bool state)
1170{
040484af 1171 bool cur_state;
ad80a810
PZ
1172 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1173 pipe);
040484af 1174
2d1fe073 1175 if (HAS_DDI(dev_priv)) {
affa9354 1176 /* DDI does not have a specific FDI_TX register */
649636ef 1177 u32 val = I915_READ(TRANS_DDI_FUNC_CTL(cpu_transcoder));
ad80a810 1178 cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
bf507ef7 1179 } else {
649636ef 1180 u32 val = I915_READ(FDI_TX_CTL(pipe));
bf507ef7
ED
1181 cur_state = !!(val & FDI_TX_ENABLE);
1182 }
e2c719b7 1183 I915_STATE_WARN(cur_state != state,
040484af 1184 "FDI TX state assertion failure (expected %s, current %s)\n",
87ad3212 1185 onoff(state), onoff(cur_state));
040484af
JB
1186}
1187#define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
1188#define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
1189
1190static void assert_fdi_rx(struct drm_i915_private *dev_priv,
1191 enum pipe pipe, bool state)
1192{
040484af
JB
1193 u32 val;
1194 bool cur_state;
1195
649636ef 1196 val = I915_READ(FDI_RX_CTL(pipe));
d63fa0dc 1197 cur_state = !!(val & FDI_RX_ENABLE);
e2c719b7 1198 I915_STATE_WARN(cur_state != state,
040484af 1199 "FDI RX state assertion failure (expected %s, current %s)\n",
87ad3212 1200 onoff(state), onoff(cur_state));
040484af
JB
1201}
1202#define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
1203#define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
1204
1205static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
1206 enum pipe pipe)
1207{
040484af
JB
1208 u32 val;
1209
1210 /* ILK FDI PLL is always enabled */
7e22dbbb 1211 if (IS_GEN5(dev_priv))
040484af
JB
1212 return;
1213
bf507ef7 1214 /* On Haswell, DDI ports are responsible for the FDI PLL setup */
2d1fe073 1215 if (HAS_DDI(dev_priv))
bf507ef7
ED
1216 return;
1217
649636ef 1218 val = I915_READ(FDI_TX_CTL(pipe));
e2c719b7 1219 I915_STATE_WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
040484af
JB
1220}
1221
55607e8a
DV
1222void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
1223 enum pipe pipe, bool state)
040484af 1224{
040484af 1225 u32 val;
55607e8a 1226 bool cur_state;
040484af 1227
649636ef 1228 val = I915_READ(FDI_RX_CTL(pipe));
55607e8a 1229 cur_state = !!(val & FDI_RX_PLL_ENABLE);
e2c719b7 1230 I915_STATE_WARN(cur_state != state,
55607e8a 1231 "FDI RX PLL assertion failure (expected %s, current %s)\n",
87ad3212 1232 onoff(state), onoff(cur_state));
040484af
JB
1233}
1234
b680c37a
DV
1235void assert_panel_unlocked(struct drm_i915_private *dev_priv,
1236 enum pipe pipe)
ea0760cf 1237{
bedd4dba 1238 struct drm_device *dev = dev_priv->dev;
f0f59a00 1239 i915_reg_t pp_reg;
ea0760cf
JB
1240 u32 val;
1241 enum pipe panel_pipe = PIPE_A;
0de3b485 1242 bool locked = true;
ea0760cf 1243
bedd4dba
JN
1244 if (WARN_ON(HAS_DDI(dev)))
1245 return;
1246
1247 if (HAS_PCH_SPLIT(dev)) {
1248 u32 port_sel;
1249
ea0760cf 1250 pp_reg = PCH_PP_CONTROL;
bedd4dba
JN
1251 port_sel = I915_READ(PCH_PP_ON_DELAYS) & PANEL_PORT_SELECT_MASK;
1252
1253 if (port_sel == PANEL_PORT_SELECT_LVDS &&
1254 I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
1255 panel_pipe = PIPE_B;
1256 /* XXX: else fix for eDP */
666a4537 1257 } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
bedd4dba
JN
1258 /* presumably write lock depends on pipe, not port select */
1259 pp_reg = VLV_PIPE_PP_CONTROL(pipe);
1260 panel_pipe = pipe;
ea0760cf
JB
1261 } else {
1262 pp_reg = PP_CONTROL;
bedd4dba
JN
1263 if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
1264 panel_pipe = PIPE_B;
ea0760cf
JB
1265 }
1266
1267 val = I915_READ(pp_reg);
1268 if (!(val & PANEL_POWER_ON) ||
ec49ba2d 1269 ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
ea0760cf
JB
1270 locked = false;
1271
e2c719b7 1272 I915_STATE_WARN(panel_pipe == pipe && locked,
ea0760cf 1273 "panel assertion failure, pipe %c regs locked\n",
9db4a9c7 1274 pipe_name(pipe));
ea0760cf
JB
1275}
1276
93ce0ba6
JN
1277static void assert_cursor(struct drm_i915_private *dev_priv,
1278 enum pipe pipe, bool state)
1279{
1280 struct drm_device *dev = dev_priv->dev;
1281 bool cur_state;
1282
d9d82081 1283 if (IS_845G(dev) || IS_I865G(dev))
0b87c24e 1284 cur_state = I915_READ(CURCNTR(PIPE_A)) & CURSOR_ENABLE;
d9d82081 1285 else
5efb3e28 1286 cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
93ce0ba6 1287
e2c719b7 1288 I915_STATE_WARN(cur_state != state,
93ce0ba6 1289 "cursor on pipe %c assertion failure (expected %s, current %s)\n",
87ad3212 1290 pipe_name(pipe), onoff(state), onoff(cur_state));
93ce0ba6
JN
1291}
1292#define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
1293#define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
1294
b840d907
JB
1295void assert_pipe(struct drm_i915_private *dev_priv,
1296 enum pipe pipe, bool state)
b24e7179 1297{
63d7bbe9 1298 bool cur_state;
702e7a56
PZ
1299 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1300 pipe);
4feed0eb 1301 enum intel_display_power_domain power_domain;
b24e7179 1302
b6b5d049
VS
1303 /* if we need the pipe quirk it must be always on */
1304 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1305 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
8e636784
DV
1306 state = true;
1307
4feed0eb
ID
1308 power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
1309 if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
649636ef 1310 u32 val = I915_READ(PIPECONF(cpu_transcoder));
69310161 1311 cur_state = !!(val & PIPECONF_ENABLE);
4feed0eb
ID
1312
1313 intel_display_power_put(dev_priv, power_domain);
1314 } else {
1315 cur_state = false;
69310161
PZ
1316 }
1317
e2c719b7 1318 I915_STATE_WARN(cur_state != state,
63d7bbe9 1319 "pipe %c assertion failure (expected %s, current %s)\n",
87ad3212 1320 pipe_name(pipe), onoff(state), onoff(cur_state));
b24e7179
JB
1321}
1322
931872fc
CW
1323static void assert_plane(struct drm_i915_private *dev_priv,
1324 enum plane plane, bool state)
b24e7179 1325{
b24e7179 1326 u32 val;
931872fc 1327 bool cur_state;
b24e7179 1328
649636ef 1329 val = I915_READ(DSPCNTR(plane));
931872fc 1330 cur_state = !!(val & DISPLAY_PLANE_ENABLE);
e2c719b7 1331 I915_STATE_WARN(cur_state != state,
931872fc 1332 "plane %c assertion failure (expected %s, current %s)\n",
87ad3212 1333 plane_name(plane), onoff(state), onoff(cur_state));
b24e7179
JB
1334}
1335
931872fc
CW
1336#define assert_plane_enabled(d, p) assert_plane(d, p, true)
1337#define assert_plane_disabled(d, p) assert_plane(d, p, false)
1338
b24e7179
JB
1339static void assert_planes_disabled(struct drm_i915_private *dev_priv,
1340 enum pipe pipe)
1341{
653e1026 1342 struct drm_device *dev = dev_priv->dev;
649636ef 1343 int i;
b24e7179 1344
653e1026
VS
1345 /* Primary planes are fixed to pipes on gen4+ */
1346 if (INTEL_INFO(dev)->gen >= 4) {
649636ef 1347 u32 val = I915_READ(DSPCNTR(pipe));
e2c719b7 1348 I915_STATE_WARN(val & DISPLAY_PLANE_ENABLE,
28c05794
AJ
1349 "plane %c assertion failure, should be disabled but not\n",
1350 plane_name(pipe));
19ec1358 1351 return;
28c05794 1352 }
19ec1358 1353
b24e7179 1354 /* Need to check both planes against the pipe */
055e393f 1355 for_each_pipe(dev_priv, i) {
649636ef
VS
1356 u32 val = I915_READ(DSPCNTR(i));
1357 enum pipe cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
b24e7179 1358 DISPPLANE_SEL_PIPE_SHIFT;
e2c719b7 1359 I915_STATE_WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
9db4a9c7
JB
1360 "plane %c assertion failure, should be off on pipe %c but is still active\n",
1361 plane_name(i), pipe_name(pipe));
b24e7179
JB
1362 }
1363}
1364
19332d7a
JB
1365static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
1366 enum pipe pipe)
1367{
20674eef 1368 struct drm_device *dev = dev_priv->dev;
649636ef 1369 int sprite;
19332d7a 1370
7feb8b88 1371 if (INTEL_INFO(dev)->gen >= 9) {
3bdcfc0c 1372 for_each_sprite(dev_priv, pipe, sprite) {
649636ef 1373 u32 val = I915_READ(PLANE_CTL(pipe, sprite));
e2c719b7 1374 I915_STATE_WARN(val & PLANE_CTL_ENABLE,
7feb8b88
DL
1375 "plane %d assertion failure, should be off on pipe %c but is still active\n",
1376 sprite, pipe_name(pipe));
1377 }
666a4537 1378 } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
3bdcfc0c 1379 for_each_sprite(dev_priv, pipe, sprite) {
649636ef 1380 u32 val = I915_READ(SPCNTR(pipe, sprite));
e2c719b7 1381 I915_STATE_WARN(val & SP_ENABLE,
20674eef 1382 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
1fe47785 1383 sprite_name(pipe, sprite), pipe_name(pipe));
20674eef
VS
1384 }
1385 } else if (INTEL_INFO(dev)->gen >= 7) {
649636ef 1386 u32 val = I915_READ(SPRCTL(pipe));
e2c719b7 1387 I915_STATE_WARN(val & SPRITE_ENABLE,
06da8da2 1388 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
20674eef
VS
1389 plane_name(pipe), pipe_name(pipe));
1390 } else if (INTEL_INFO(dev)->gen >= 5) {
649636ef 1391 u32 val = I915_READ(DVSCNTR(pipe));
e2c719b7 1392 I915_STATE_WARN(val & DVS_ENABLE,
06da8da2 1393 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
20674eef 1394 plane_name(pipe), pipe_name(pipe));
19332d7a
JB
1395 }
1396}
1397
08c71e5e
VS
1398static void assert_vblank_disabled(struct drm_crtc *crtc)
1399{
e2c719b7 1400 if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
08c71e5e
VS
1401 drm_crtc_vblank_put(crtc);
1402}
1403
7abd4b35
ACO
1404void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
1405 enum pipe pipe)
92f2584a 1406{
92f2584a
JB
1407 u32 val;
1408 bool enabled;
1409
649636ef 1410 val = I915_READ(PCH_TRANSCONF(pipe));
92f2584a 1411 enabled = !!(val & TRANS_ENABLE);
e2c719b7 1412 I915_STATE_WARN(enabled,
9db4a9c7
JB
1413 "transcoder assertion failed, should be off on pipe %c but is still active\n",
1414 pipe_name(pipe));
92f2584a
JB
1415}
1416
4e634389
KP
1417static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
1418 enum pipe pipe, u32 port_sel, u32 val)
f0575e92
KP
1419{
1420 if ((val & DP_PORT_EN) == 0)
1421 return false;
1422
2d1fe073 1423 if (HAS_PCH_CPT(dev_priv)) {
f0f59a00 1424 u32 trans_dp_ctl = I915_READ(TRANS_DP_CTL(pipe));
f0575e92
KP
1425 if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
1426 return false;
2d1fe073 1427 } else if (IS_CHERRYVIEW(dev_priv)) {
44f37d1f
CML
1428 if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
1429 return false;
f0575e92
KP
1430 } else {
1431 if ((val & DP_PIPE_MASK) != (pipe << 30))
1432 return false;
1433 }
1434 return true;
1435}
1436
1519b995
KP
1437static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
1438 enum pipe pipe, u32 val)
1439{
dc0fa718 1440 if ((val & SDVO_ENABLE) == 0)
1519b995
KP
1441 return false;
1442
2d1fe073 1443 if (HAS_PCH_CPT(dev_priv)) {
dc0fa718 1444 if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
1519b995 1445 return false;
2d1fe073 1446 } else if (IS_CHERRYVIEW(dev_priv)) {
44f37d1f
CML
1447 if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
1448 return false;
1519b995 1449 } else {
dc0fa718 1450 if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
1519b995
KP
1451 return false;
1452 }
1453 return true;
1454}
1455
1456static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
1457 enum pipe pipe, u32 val)
1458{
1459 if ((val & LVDS_PORT_EN) == 0)
1460 return false;
1461
2d1fe073 1462 if (HAS_PCH_CPT(dev_priv)) {
1519b995
KP
1463 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1464 return false;
1465 } else {
1466 if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
1467 return false;
1468 }
1469 return true;
1470}
1471
1472static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
1473 enum pipe pipe, u32 val)
1474{
1475 if ((val & ADPA_DAC_ENABLE) == 0)
1476 return false;
2d1fe073 1477 if (HAS_PCH_CPT(dev_priv)) {
1519b995
KP
1478 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1479 return false;
1480 } else {
1481 if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
1482 return false;
1483 }
1484 return true;
1485}
1486
291906f1 1487static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
f0f59a00
VS
1488 enum pipe pipe, i915_reg_t reg,
1489 u32 port_sel)
291906f1 1490{
47a05eca 1491 u32 val = I915_READ(reg);
e2c719b7 1492 I915_STATE_WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
291906f1 1493 "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
f0f59a00 1494 i915_mmio_reg_offset(reg), pipe_name(pipe));
de9a35ab 1495
2d1fe073 1496 I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && (val & DP_PORT_EN) == 0
75c5da27 1497 && (val & DP_PIPEB_SELECT),
de9a35ab 1498 "IBX PCH dp port still using transcoder B\n");
291906f1
JB
1499}
1500
1501static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
f0f59a00 1502 enum pipe pipe, i915_reg_t reg)
291906f1 1503{
47a05eca 1504 u32 val = I915_READ(reg);
e2c719b7 1505 I915_STATE_WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
23c99e77 1506 "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
f0f59a00 1507 i915_mmio_reg_offset(reg), pipe_name(pipe));
de9a35ab 1508
2d1fe073 1509 I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && (val & SDVO_ENABLE) == 0
75c5da27 1510 && (val & SDVO_PIPE_B_SELECT),
de9a35ab 1511 "IBX PCH hdmi port still using transcoder B\n");
291906f1
JB
1512}
1513
1514static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
1515 enum pipe pipe)
1516{
291906f1 1517 u32 val;
291906f1 1518
f0575e92
KP
1519 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
1520 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
1521 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
291906f1 1522
649636ef 1523 val = I915_READ(PCH_ADPA);
e2c719b7 1524 I915_STATE_WARN(adpa_pipe_enabled(dev_priv, pipe, val),
291906f1 1525 "PCH VGA enabled on transcoder %c, should be disabled\n",
9db4a9c7 1526 pipe_name(pipe));
291906f1 1527
649636ef 1528 val = I915_READ(PCH_LVDS);
e2c719b7 1529 I915_STATE_WARN(lvds_pipe_enabled(dev_priv, pipe, val),
291906f1 1530 "PCH LVDS enabled on transcoder %c, should be disabled\n",
9db4a9c7 1531 pipe_name(pipe));
291906f1 1532
e2debe91
PZ
1533 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
1534 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
1535 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
291906f1
JB
1536}
1537
cd2d34d9
VS
1538static void _vlv_enable_pll(struct intel_crtc *crtc,
1539 const struct intel_crtc_state *pipe_config)
1540{
1541 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1542 enum pipe pipe = crtc->pipe;
1543
1544 I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
1545 POSTING_READ(DPLL(pipe));
1546 udelay(150);
1547
2c30b43b
CW
1548 if (intel_wait_for_register(dev_priv,
1549 DPLL(pipe),
1550 DPLL_LOCK_VLV,
1551 DPLL_LOCK_VLV,
1552 1))
cd2d34d9
VS
1553 DRM_ERROR("DPLL %d failed to lock\n", pipe);
1554}
1555
d288f65f 1556static void vlv_enable_pll(struct intel_crtc *crtc,
5cec258b 1557 const struct intel_crtc_state *pipe_config)
87442f73 1558{
cd2d34d9 1559 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
8bd3f301 1560 enum pipe pipe = crtc->pipe;
87442f73 1561
8bd3f301 1562 assert_pipe_disabled(dev_priv, pipe);
87442f73 1563
87442f73 1564 /* PLL is protected by panel, make sure we can write it */
7d1a83cb 1565 assert_panel_unlocked(dev_priv, pipe);
87442f73 1566
cd2d34d9
VS
1567 if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
1568 _vlv_enable_pll(crtc, pipe_config);
426115cf 1569
8bd3f301
VS
1570 I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
1571 POSTING_READ(DPLL_MD(pipe));
87442f73
DV
1572}
1573
cd2d34d9
VS
1574
1575static void _chv_enable_pll(struct intel_crtc *crtc,
1576 const struct intel_crtc_state *pipe_config)
9d556c99 1577{
cd2d34d9 1578 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
8bd3f301 1579 enum pipe pipe = crtc->pipe;
9d556c99 1580 enum dpio_channel port = vlv_pipe_to_channel(pipe);
9d556c99
CML
1581 u32 tmp;
1582
a580516d 1583 mutex_lock(&dev_priv->sb_lock);
9d556c99
CML
1584
1585 /* Enable back the 10bit clock to display controller */
1586 tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1587 tmp |= DPIO_DCLKP_EN;
1588 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
1589
54433e91
VS
1590 mutex_unlock(&dev_priv->sb_lock);
1591
9d556c99
CML
1592 /*
1593 * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
1594 */
1595 udelay(1);
1596
1597 /* Enable PLL */
d288f65f 1598 I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
9d556c99
CML
1599
1600 /* Check PLL is locked */
6b18826a
CW
1601 if (intel_wait_for_register(dev_priv,
1602 DPLL(pipe), DPLL_LOCK_VLV, DPLL_LOCK_VLV,
1603 1))
9d556c99 1604 DRM_ERROR("PLL %d failed to lock\n", pipe);
cd2d34d9
VS
1605}
1606
1607static void chv_enable_pll(struct intel_crtc *crtc,
1608 const struct intel_crtc_state *pipe_config)
1609{
1610 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1611 enum pipe pipe = crtc->pipe;
1612
1613 assert_pipe_disabled(dev_priv, pipe);
1614
1615 /* PLL is protected by panel, make sure we can write it */
1616 assert_panel_unlocked(dev_priv, pipe);
1617
1618 if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
1619 _chv_enable_pll(crtc, pipe_config);
9d556c99 1620
c231775c
VS
1621 if (pipe != PIPE_A) {
1622 /*
1623 * WaPixelRepeatModeFixForC0:chv
1624 *
1625 * DPLLCMD is AWOL. Use chicken bits to propagate
1626 * the value from DPLLBMD to either pipe B or C.
1627 */
1628 I915_WRITE(CBR4_VLV, pipe == PIPE_B ? CBR_DPLLBMD_PIPE_B : CBR_DPLLBMD_PIPE_C);
1629 I915_WRITE(DPLL_MD(PIPE_B), pipe_config->dpll_hw_state.dpll_md);
1630 I915_WRITE(CBR4_VLV, 0);
1631 dev_priv->chv_dpll_md[pipe] = pipe_config->dpll_hw_state.dpll_md;
1632
1633 /*
1634 * DPLLB VGA mode also seems to cause problems.
1635 * We should always have it disabled.
1636 */
1637 WARN_ON((I915_READ(DPLL(PIPE_B)) & DPLL_VGA_MODE_DIS) == 0);
1638 } else {
1639 I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
1640 POSTING_READ(DPLL_MD(pipe));
1641 }
9d556c99
CML
1642}
1643
1c4e0274
VS
1644static int intel_num_dvo_pipes(struct drm_device *dev)
1645{
1646 struct intel_crtc *crtc;
1647 int count = 0;
1648
1649 for_each_intel_crtc(dev, crtc)
3538b9df 1650 count += crtc->base.state->active &&
409ee761 1651 intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO);
1c4e0274
VS
1652
1653 return count;
1654}
1655
66e3d5c0 1656static void i9xx_enable_pll(struct intel_crtc *crtc)
63d7bbe9 1657{
66e3d5c0
DV
1658 struct drm_device *dev = crtc->base.dev;
1659 struct drm_i915_private *dev_priv = dev->dev_private;
f0f59a00 1660 i915_reg_t reg = DPLL(crtc->pipe);
6e3c9717 1661 u32 dpll = crtc->config->dpll_hw_state.dpll;
63d7bbe9 1662
66e3d5c0 1663 assert_pipe_disabled(dev_priv, crtc->pipe);
58c6eaa2 1664
63d7bbe9 1665 /* PLL is protected by panel, make sure we can write it */
66e3d5c0
DV
1666 if (IS_MOBILE(dev) && !IS_I830(dev))
1667 assert_panel_unlocked(dev_priv, crtc->pipe);
63d7bbe9 1668
1c4e0274
VS
1669 /* Enable DVO 2x clock on both PLLs if necessary */
1670 if (IS_I830(dev) && intel_num_dvo_pipes(dev) > 0) {
1671 /*
1672 * It appears to be important that we don't enable this
1673 * for the current pipe before otherwise configuring the
1674 * PLL. No idea how this should be handled if multiple
1675 * DVO outputs are enabled simultaneosly.
1676 */
1677 dpll |= DPLL_DVO_2X_MODE;
1678 I915_WRITE(DPLL(!crtc->pipe),
1679 I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
1680 }
66e3d5c0 1681
c2b63374
VS
1682 /*
1683 * Apparently we need to have VGA mode enabled prior to changing
1684 * the P1/P2 dividers. Otherwise the DPLL will keep using the old
1685 * dividers, even though the register value does change.
1686 */
1687 I915_WRITE(reg, 0);
1688
8e7a65aa
VS
1689 I915_WRITE(reg, dpll);
1690
66e3d5c0
DV
1691 /* Wait for the clocks to stabilize. */
1692 POSTING_READ(reg);
1693 udelay(150);
1694
1695 if (INTEL_INFO(dev)->gen >= 4) {
1696 I915_WRITE(DPLL_MD(crtc->pipe),
6e3c9717 1697 crtc->config->dpll_hw_state.dpll_md);
66e3d5c0
DV
1698 } else {
1699 /* The pixel multiplier can only be updated once the
1700 * DPLL is enabled and the clocks are stable.
1701 *
1702 * So write it again.
1703 */
1704 I915_WRITE(reg, dpll);
1705 }
63d7bbe9
JB
1706
1707 /* We do this three times for luck */
66e3d5c0 1708 I915_WRITE(reg, dpll);
63d7bbe9
JB
1709 POSTING_READ(reg);
1710 udelay(150); /* wait for warmup */
66e3d5c0 1711 I915_WRITE(reg, dpll);
63d7bbe9
JB
1712 POSTING_READ(reg);
1713 udelay(150); /* wait for warmup */
66e3d5c0 1714 I915_WRITE(reg, dpll);
63d7bbe9
JB
1715 POSTING_READ(reg);
1716 udelay(150); /* wait for warmup */
1717}
1718
1719/**
50b44a44 1720 * i9xx_disable_pll - disable a PLL
63d7bbe9
JB
1721 * @dev_priv: i915 private structure
1722 * @pipe: pipe PLL to disable
1723 *
1724 * Disable the PLL for @pipe, making sure the pipe is off first.
1725 *
1726 * Note! This is for pre-ILK only.
1727 */
1c4e0274 1728static void i9xx_disable_pll(struct intel_crtc *crtc)
63d7bbe9 1729{
1c4e0274
VS
1730 struct drm_device *dev = crtc->base.dev;
1731 struct drm_i915_private *dev_priv = dev->dev_private;
1732 enum pipe pipe = crtc->pipe;
1733
1734 /* Disable DVO 2x clock on both PLLs if necessary */
1735 if (IS_I830(dev) &&
409ee761 1736 intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO) &&
3538b9df 1737 !intel_num_dvo_pipes(dev)) {
1c4e0274
VS
1738 I915_WRITE(DPLL(PIPE_B),
1739 I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
1740 I915_WRITE(DPLL(PIPE_A),
1741 I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
1742 }
1743
b6b5d049
VS
1744 /* Don't disable pipe or pipe PLLs if needed */
1745 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1746 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
63d7bbe9
JB
1747 return;
1748
1749 /* Make sure the pipe isn't still relying on us */
1750 assert_pipe_disabled(dev_priv, pipe);
1751
b8afb911 1752 I915_WRITE(DPLL(pipe), DPLL_VGA_MODE_DIS);
50b44a44 1753 POSTING_READ(DPLL(pipe));
63d7bbe9
JB
1754}
1755
f6071166
JB
1756static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1757{
b8afb911 1758 u32 val;
f6071166
JB
1759
1760 /* Make sure the pipe isn't still relying on us */
1761 assert_pipe_disabled(dev_priv, pipe);
1762
03ed5cbf
VS
1763 val = DPLL_INTEGRATED_REF_CLK_VLV |
1764 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
1765 if (pipe != PIPE_A)
1766 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
1767
f6071166
JB
1768 I915_WRITE(DPLL(pipe), val);
1769 POSTING_READ(DPLL(pipe));
076ed3b2
CML
1770}
1771
1772static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1773{
d752048d 1774 enum dpio_channel port = vlv_pipe_to_channel(pipe);
076ed3b2
CML
1775 u32 val;
1776
a11b0703
VS
1777 /* Make sure the pipe isn't still relying on us */
1778 assert_pipe_disabled(dev_priv, pipe);
076ed3b2 1779
60bfe44f
VS
1780 val = DPLL_SSC_REF_CLK_CHV |
1781 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
a11b0703
VS
1782 if (pipe != PIPE_A)
1783 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
03ed5cbf 1784
a11b0703
VS
1785 I915_WRITE(DPLL(pipe), val);
1786 POSTING_READ(DPLL(pipe));
d752048d 1787
a580516d 1788 mutex_lock(&dev_priv->sb_lock);
d752048d
VS
1789
1790 /* Disable 10bit clock to display controller */
1791 val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1792 val &= ~DPIO_DCLKP_EN;
1793 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
1794
a580516d 1795 mutex_unlock(&dev_priv->sb_lock);
f6071166
JB
1796}
1797
e4607fcf 1798void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
9b6de0a1
VS
1799 struct intel_digital_port *dport,
1800 unsigned int expected_mask)
89b667f8
JB
1801{
1802 u32 port_mask;
f0f59a00 1803 i915_reg_t dpll_reg;
89b667f8 1804
e4607fcf
CML
1805 switch (dport->port) {
1806 case PORT_B:
89b667f8 1807 port_mask = DPLL_PORTB_READY_MASK;
00fc31b7 1808 dpll_reg = DPLL(0);
e4607fcf
CML
1809 break;
1810 case PORT_C:
89b667f8 1811 port_mask = DPLL_PORTC_READY_MASK;
00fc31b7 1812 dpll_reg = DPLL(0);
9b6de0a1 1813 expected_mask <<= 4;
00fc31b7
CML
1814 break;
1815 case PORT_D:
1816 port_mask = DPLL_PORTD_READY_MASK;
1817 dpll_reg = DPIO_PHY_STATUS;
e4607fcf
CML
1818 break;
1819 default:
1820 BUG();
1821 }
89b667f8 1822
370004d3
CW
1823 if (intel_wait_for_register(dev_priv,
1824 dpll_reg, port_mask, expected_mask,
1825 1000))
9b6de0a1
VS
1826 WARN(1, "timed out waiting for port %c ready: got 0x%x, expected 0x%x\n",
1827 port_name(dport->port), I915_READ(dpll_reg) & port_mask, expected_mask);
89b667f8
JB
1828}
1829
b8a4f404
PZ
1830static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1831 enum pipe pipe)
040484af 1832{
23670b32 1833 struct drm_device *dev = dev_priv->dev;
7c26e5c6 1834 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
e2b78267 1835 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
f0f59a00
VS
1836 i915_reg_t reg;
1837 uint32_t val, pipeconf_val;
040484af 1838
040484af 1839 /* Make sure PCH DPLL is enabled */
8106ddbd 1840 assert_shared_dpll_enabled(dev_priv, intel_crtc->config->shared_dpll);
040484af
JB
1841
1842 /* FDI must be feeding us bits for PCH ports */
1843 assert_fdi_tx_enabled(dev_priv, pipe);
1844 assert_fdi_rx_enabled(dev_priv, pipe);
1845
23670b32
DV
1846 if (HAS_PCH_CPT(dev)) {
1847 /* Workaround: Set the timing override bit before enabling the
1848 * pch transcoder. */
1849 reg = TRANS_CHICKEN2(pipe);
1850 val = I915_READ(reg);
1851 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
1852 I915_WRITE(reg, val);
59c859d6 1853 }
23670b32 1854
ab9412ba 1855 reg = PCH_TRANSCONF(pipe);
040484af 1856 val = I915_READ(reg);
5f7f726d 1857 pipeconf_val = I915_READ(PIPECONF(pipe));
e9bcff5c 1858
2d1fe073 1859 if (HAS_PCH_IBX(dev_priv)) {
e9bcff5c 1860 /*
c5de7c6f
VS
1861 * Make the BPC in transcoder be consistent with
1862 * that in pipeconf reg. For HDMI we must use 8bpc
1863 * here for both 8bpc and 12bpc.
e9bcff5c 1864 */
dfd07d72 1865 val &= ~PIPECONF_BPC_MASK;
c5de7c6f
VS
1866 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_HDMI))
1867 val |= PIPECONF_8BPC;
1868 else
1869 val |= pipeconf_val & PIPECONF_BPC_MASK;
e9bcff5c 1870 }
5f7f726d
PZ
1871
1872 val &= ~TRANS_INTERLACE_MASK;
1873 if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
2d1fe073 1874 if (HAS_PCH_IBX(dev_priv) &&
409ee761 1875 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
7c26e5c6
PZ
1876 val |= TRANS_LEGACY_INTERLACED_ILK;
1877 else
1878 val |= TRANS_INTERLACED;
5f7f726d
PZ
1879 else
1880 val |= TRANS_PROGRESSIVE;
1881
040484af 1882 I915_WRITE(reg, val | TRANS_ENABLE);
650fbd84
CW
1883 if (intel_wait_for_register(dev_priv,
1884 reg, TRANS_STATE_ENABLE, TRANS_STATE_ENABLE,
1885 100))
4bb6f1f3 1886 DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
040484af
JB
1887}
1888
8fb033d7 1889static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
937bb610 1890 enum transcoder cpu_transcoder)
040484af 1891{
8fb033d7 1892 u32 val, pipeconf_val;
8fb033d7 1893
8fb033d7 1894 /* FDI must be feeding us bits for PCH ports */
1a240d4d 1895 assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
937bb610 1896 assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
8fb033d7 1897
223a6fdf 1898 /* Workaround: set timing override bit. */
36c0d0cf 1899 val = I915_READ(TRANS_CHICKEN2(PIPE_A));
23670b32 1900 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
36c0d0cf 1901 I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
223a6fdf 1902
25f3ef11 1903 val = TRANS_ENABLE;
937bb610 1904 pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
8fb033d7 1905
9a76b1c6
PZ
1906 if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
1907 PIPECONF_INTERLACED_ILK)
a35f2679 1908 val |= TRANS_INTERLACED;
8fb033d7
PZ
1909 else
1910 val |= TRANS_PROGRESSIVE;
1911
ab9412ba 1912 I915_WRITE(LPT_TRANSCONF, val);
d9f96244
CW
1913 if (intel_wait_for_register(dev_priv,
1914 LPT_TRANSCONF,
1915 TRANS_STATE_ENABLE,
1916 TRANS_STATE_ENABLE,
1917 100))
937bb610 1918 DRM_ERROR("Failed to enable PCH transcoder\n");
8fb033d7
PZ
1919}
1920
b8a4f404
PZ
1921static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
1922 enum pipe pipe)
040484af 1923{
23670b32 1924 struct drm_device *dev = dev_priv->dev;
f0f59a00
VS
1925 i915_reg_t reg;
1926 uint32_t val;
040484af
JB
1927
1928 /* FDI relies on the transcoder */
1929 assert_fdi_tx_disabled(dev_priv, pipe);
1930 assert_fdi_rx_disabled(dev_priv, pipe);
1931
291906f1
JB
1932 /* Ports must be off as well */
1933 assert_pch_ports_disabled(dev_priv, pipe);
1934
ab9412ba 1935 reg = PCH_TRANSCONF(pipe);
040484af
JB
1936 val = I915_READ(reg);
1937 val &= ~TRANS_ENABLE;
1938 I915_WRITE(reg, val);
1939 /* wait for PCH transcoder off, transcoder state */
1940 if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
4bb6f1f3 1941 DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
23670b32 1942
c465613b 1943 if (HAS_PCH_CPT(dev)) {
23670b32
DV
1944 /* Workaround: Clear the timing override chicken bit again. */
1945 reg = TRANS_CHICKEN2(pipe);
1946 val = I915_READ(reg);
1947 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
1948 I915_WRITE(reg, val);
1949 }
040484af
JB
1950}
1951
ab4d966c 1952static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
8fb033d7 1953{
8fb033d7
PZ
1954 u32 val;
1955
ab9412ba 1956 val = I915_READ(LPT_TRANSCONF);
8fb033d7 1957 val &= ~TRANS_ENABLE;
ab9412ba 1958 I915_WRITE(LPT_TRANSCONF, val);
8fb033d7 1959 /* wait for PCH transcoder off, transcoder state */
ab9412ba 1960 if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
8a52fd9f 1961 DRM_ERROR("Failed to disable PCH transcoder\n");
223a6fdf
PZ
1962
1963 /* Workaround: clear timing override bit. */
36c0d0cf 1964 val = I915_READ(TRANS_CHICKEN2(PIPE_A));
23670b32 1965 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
36c0d0cf 1966 I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
040484af
JB
1967}
1968
b24e7179 1969/**
309cfea8 1970 * intel_enable_pipe - enable a pipe, asserting requirements
0372264a 1971 * @crtc: crtc responsible for the pipe
b24e7179 1972 *
0372264a 1973 * Enable @crtc's pipe, making sure that various hardware specific requirements
b24e7179 1974 * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
b24e7179 1975 */
e1fdc473 1976static void intel_enable_pipe(struct intel_crtc *crtc)
b24e7179 1977{
0372264a
PZ
1978 struct drm_device *dev = crtc->base.dev;
1979 struct drm_i915_private *dev_priv = dev->dev_private;
1980 enum pipe pipe = crtc->pipe;
1a70a728 1981 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
1a240d4d 1982 enum pipe pch_transcoder;
f0f59a00 1983 i915_reg_t reg;
b24e7179
JB
1984 u32 val;
1985
9e2ee2dd
VS
1986 DRM_DEBUG_KMS("enabling pipe %c\n", pipe_name(pipe));
1987
58c6eaa2 1988 assert_planes_disabled(dev_priv, pipe);
93ce0ba6 1989 assert_cursor_disabled(dev_priv, pipe);
58c6eaa2
DV
1990 assert_sprites_disabled(dev_priv, pipe);
1991
2d1fe073 1992 if (HAS_PCH_LPT(dev_priv))
cc391bbb
PZ
1993 pch_transcoder = TRANSCODER_A;
1994 else
1995 pch_transcoder = pipe;
1996
b24e7179
JB
1997 /*
1998 * A pipe without a PLL won't actually be able to drive bits from
1999 * a plane. On ILK+ the pipe PLLs are integrated, so we don't
2000 * need the check.
2001 */
2d1fe073 2002 if (HAS_GMCH_DISPLAY(dev_priv))
a65347ba 2003 if (crtc->config->has_dsi_encoder)
23538ef1
JN
2004 assert_dsi_pll_enabled(dev_priv);
2005 else
2006 assert_pll_enabled(dev_priv, pipe);
040484af 2007 else {
6e3c9717 2008 if (crtc->config->has_pch_encoder) {
040484af 2009 /* if driving the PCH, we need FDI enabled */
cc391bbb 2010 assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
1a240d4d
DV
2011 assert_fdi_tx_pll_enabled(dev_priv,
2012 (enum pipe) cpu_transcoder);
040484af
JB
2013 }
2014 /* FIXME: assert CPU port conditions for SNB+ */
2015 }
b24e7179 2016
702e7a56 2017 reg = PIPECONF(cpu_transcoder);
b24e7179 2018 val = I915_READ(reg);
7ad25d48 2019 if (val & PIPECONF_ENABLE) {
b6b5d049
VS
2020 WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
2021 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
00d70b15 2022 return;
7ad25d48 2023 }
00d70b15
CW
2024
2025 I915_WRITE(reg, val | PIPECONF_ENABLE);
851855d8 2026 POSTING_READ(reg);
b7792d8b
VS
2027
2028 /*
2029 * Until the pipe starts DSL will read as 0, which would cause
2030 * an apparent vblank timestamp jump, which messes up also the
2031 * frame count when it's derived from the timestamps. So let's
2032 * wait for the pipe to start properly before we call
2033 * drm_crtc_vblank_on()
2034 */
2035 if (dev->max_vblank_count == 0 &&
2036 wait_for(intel_get_crtc_scanline(crtc) != crtc->scanline_offset, 50))
2037 DRM_ERROR("pipe %c didn't start\n", pipe_name(pipe));
b24e7179
JB
2038}
2039
2040/**
309cfea8 2041 * intel_disable_pipe - disable a pipe, asserting requirements
575f7ab7 2042 * @crtc: crtc whose pipes is to be disabled
b24e7179 2043 *
575f7ab7
VS
2044 * Disable the pipe of @crtc, making sure that various hardware
2045 * specific requirements are met, if applicable, e.g. plane
2046 * disabled, panel fitter off, etc.
b24e7179
JB
2047 *
2048 * Will wait until the pipe has shut down before returning.
2049 */
575f7ab7 2050static void intel_disable_pipe(struct intel_crtc *crtc)
b24e7179 2051{
575f7ab7 2052 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
6e3c9717 2053 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
575f7ab7 2054 enum pipe pipe = crtc->pipe;
f0f59a00 2055 i915_reg_t reg;
b24e7179
JB
2056 u32 val;
2057
9e2ee2dd
VS
2058 DRM_DEBUG_KMS("disabling pipe %c\n", pipe_name(pipe));
2059
b24e7179
JB
2060 /*
2061 * Make sure planes won't keep trying to pump pixels to us,
2062 * or we might hang the display.
2063 */
2064 assert_planes_disabled(dev_priv, pipe);
93ce0ba6 2065 assert_cursor_disabled(dev_priv, pipe);
19332d7a 2066 assert_sprites_disabled(dev_priv, pipe);
b24e7179 2067
702e7a56 2068 reg = PIPECONF(cpu_transcoder);
b24e7179 2069 val = I915_READ(reg);
00d70b15
CW
2070 if ((val & PIPECONF_ENABLE) == 0)
2071 return;
2072
67adc644
VS
2073 /*
2074 * Double wide has implications for planes
2075 * so best keep it disabled when not needed.
2076 */
6e3c9717 2077 if (crtc->config->double_wide)
67adc644
VS
2078 val &= ~PIPECONF_DOUBLE_WIDE;
2079
2080 /* Don't disable pipe or pipe PLLs if needed */
b6b5d049
VS
2081 if (!(pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) &&
2082 !(pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
67adc644
VS
2083 val &= ~PIPECONF_ENABLE;
2084
2085 I915_WRITE(reg, val);
2086 if ((val & PIPECONF_ENABLE) == 0)
2087 intel_wait_for_pipe_off(crtc);
b24e7179
JB
2088}
2089
693db184
CW
2090static bool need_vtd_wa(struct drm_device *dev)
2091{
2092#ifdef CONFIG_INTEL_IOMMU
2093 if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
2094 return true;
2095#endif
2096 return false;
2097}
2098
832be82f
VS
2099static unsigned int intel_tile_size(const struct drm_i915_private *dev_priv)
2100{
2101 return IS_GEN2(dev_priv) ? 2048 : 4096;
2102}
2103
27ba3910
VS
2104static unsigned int intel_tile_width_bytes(const struct drm_i915_private *dev_priv,
2105 uint64_t fb_modifier, unsigned int cpp)
7b49f948
VS
2106{
2107 switch (fb_modifier) {
2108 case DRM_FORMAT_MOD_NONE:
2109 return cpp;
2110 case I915_FORMAT_MOD_X_TILED:
2111 if (IS_GEN2(dev_priv))
2112 return 128;
2113 else
2114 return 512;
2115 case I915_FORMAT_MOD_Y_TILED:
2116 if (IS_GEN2(dev_priv) || HAS_128_BYTE_Y_TILING(dev_priv))
2117 return 128;
2118 else
2119 return 512;
2120 case I915_FORMAT_MOD_Yf_TILED:
2121 switch (cpp) {
2122 case 1:
2123 return 64;
2124 case 2:
2125 case 4:
2126 return 128;
2127 case 8:
2128 case 16:
2129 return 256;
2130 default:
2131 MISSING_CASE(cpp);
2132 return cpp;
2133 }
2134 break;
2135 default:
2136 MISSING_CASE(fb_modifier);
2137 return cpp;
2138 }
2139}
2140
832be82f
VS
2141unsigned int intel_tile_height(const struct drm_i915_private *dev_priv,
2142 uint64_t fb_modifier, unsigned int cpp)
a57ce0b2 2143{
832be82f
VS
2144 if (fb_modifier == DRM_FORMAT_MOD_NONE)
2145 return 1;
2146 else
2147 return intel_tile_size(dev_priv) /
27ba3910 2148 intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
6761dd31
TU
2149}
2150
8d0deca8
VS
2151/* Return the tile dimensions in pixel units */
2152static void intel_tile_dims(const struct drm_i915_private *dev_priv,
2153 unsigned int *tile_width,
2154 unsigned int *tile_height,
2155 uint64_t fb_modifier,
2156 unsigned int cpp)
2157{
2158 unsigned int tile_width_bytes =
2159 intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
2160
2161 *tile_width = tile_width_bytes / cpp;
2162 *tile_height = intel_tile_size(dev_priv) / tile_width_bytes;
2163}
2164
6761dd31
TU
2165unsigned int
2166intel_fb_align_height(struct drm_device *dev, unsigned int height,
832be82f 2167 uint32_t pixel_format, uint64_t fb_modifier)
6761dd31 2168{
832be82f
VS
2169 unsigned int cpp = drm_format_plane_cpp(pixel_format, 0);
2170 unsigned int tile_height = intel_tile_height(to_i915(dev), fb_modifier, cpp);
2171
2172 return ALIGN(height, tile_height);
a57ce0b2
JB
2173}
2174
1663b9d6
VS
2175unsigned int intel_rotation_info_size(const struct intel_rotation_info *rot_info)
2176{
2177 unsigned int size = 0;
2178 int i;
2179
2180 for (i = 0 ; i < ARRAY_SIZE(rot_info->plane); i++)
2181 size += rot_info->plane[i].width * rot_info->plane[i].height;
2182
2183 return size;
2184}
2185
75c82a53 2186static void
3465c580
VS
2187intel_fill_fb_ggtt_view(struct i915_ggtt_view *view,
2188 const struct drm_framebuffer *fb,
2189 unsigned int rotation)
f64b98cd 2190{
2d7a215f
VS
2191 if (intel_rotation_90_or_270(rotation)) {
2192 *view = i915_ggtt_view_rotated;
2193 view->params.rotated = to_intel_framebuffer(fb)->rot_info;
2194 } else {
2195 *view = i915_ggtt_view_normal;
2196 }
2197}
50470bb0 2198
2d7a215f
VS
2199static void
2200intel_fill_fb_info(struct drm_i915_private *dev_priv,
2201 struct drm_framebuffer *fb)
2202{
2203 struct intel_rotation_info *info = &to_intel_framebuffer(fb)->rot_info;
2204 unsigned int tile_size, tile_width, tile_height, cpp;
50470bb0 2205
d9b3288e
VS
2206 tile_size = intel_tile_size(dev_priv);
2207
2208 cpp = drm_format_plane_cpp(fb->pixel_format, 0);
8d0deca8
VS
2209 intel_tile_dims(dev_priv, &tile_width, &tile_height,
2210 fb->modifier[0], cpp);
d9b3288e 2211
1663b9d6
VS
2212 info->plane[0].width = DIV_ROUND_UP(fb->pitches[0], tile_width * cpp);
2213 info->plane[0].height = DIV_ROUND_UP(fb->height, tile_height);
84fe03f7 2214
89e3e142 2215 if (info->pixel_format == DRM_FORMAT_NV12) {
832be82f 2216 cpp = drm_format_plane_cpp(fb->pixel_format, 1);
8d0deca8
VS
2217 intel_tile_dims(dev_priv, &tile_width, &tile_height,
2218 fb->modifier[1], cpp);
d9b3288e 2219
2d7a215f 2220 info->uv_offset = fb->offsets[1];
1663b9d6
VS
2221 info->plane[1].width = DIV_ROUND_UP(fb->pitches[1], tile_width * cpp);
2222 info->plane[1].height = DIV_ROUND_UP(fb->height / 2, tile_height);
89e3e142 2223 }
f64b98cd
TU
2224}
2225
603525d7 2226static unsigned int intel_linear_alignment(const struct drm_i915_private *dev_priv)
4e9a86b6
VS
2227{
2228 if (INTEL_INFO(dev_priv)->gen >= 9)
2229 return 256 * 1024;
985b8bb4 2230 else if (IS_BROADWATER(dev_priv) || IS_CRESTLINE(dev_priv) ||
666a4537 2231 IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
4e9a86b6
VS
2232 return 128 * 1024;
2233 else if (INTEL_INFO(dev_priv)->gen >= 4)
2234 return 4 * 1024;
2235 else
44c5905e 2236 return 0;
4e9a86b6
VS
2237}
2238
603525d7
VS
2239static unsigned int intel_surf_alignment(const struct drm_i915_private *dev_priv,
2240 uint64_t fb_modifier)
2241{
2242 switch (fb_modifier) {
2243 case DRM_FORMAT_MOD_NONE:
2244 return intel_linear_alignment(dev_priv);
2245 case I915_FORMAT_MOD_X_TILED:
2246 if (INTEL_INFO(dev_priv)->gen >= 9)
2247 return 256 * 1024;
2248 return 0;
2249 case I915_FORMAT_MOD_Y_TILED:
2250 case I915_FORMAT_MOD_Yf_TILED:
2251 return 1 * 1024 * 1024;
2252 default:
2253 MISSING_CASE(fb_modifier);
2254 return 0;
2255 }
2256}
2257
127bd2ac 2258int
3465c580
VS
2259intel_pin_and_fence_fb_obj(struct drm_framebuffer *fb,
2260 unsigned int rotation)
6b95a207 2261{
850c4cdc 2262 struct drm_device *dev = fb->dev;
ce453d81 2263 struct drm_i915_private *dev_priv = dev->dev_private;
850c4cdc 2264 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
f64b98cd 2265 struct i915_ggtt_view view;
6b95a207
KH
2266 u32 alignment;
2267 int ret;
2268
ebcdd39e
MR
2269 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
2270
603525d7 2271 alignment = intel_surf_alignment(dev_priv, fb->modifier[0]);
6b95a207 2272
3465c580 2273 intel_fill_fb_ggtt_view(&view, fb, rotation);
f64b98cd 2274
693db184
CW
2275 /* Note that the w/a also requires 64 PTE of padding following the
2276 * bo. We currently fill all unused PTE with the shadow page and so
2277 * we should always have valid PTE following the scanout preventing
2278 * the VT-d warning.
2279 */
2280 if (need_vtd_wa(dev) && alignment < 256 * 1024)
2281 alignment = 256 * 1024;
2282
d6dd6843
PZ
2283 /*
2284 * Global gtt pte registers are special registers which actually forward
2285 * writes to a chunk of system memory. Which means that there is no risk
2286 * that the register values disappear as soon as we call
2287 * intel_runtime_pm_put(), so it is correct to wrap only the
2288 * pin/unpin/fence and not more.
2289 */
2290 intel_runtime_pm_get(dev_priv);
2291
7580d774
ML
2292 ret = i915_gem_object_pin_to_display_plane(obj, alignment,
2293 &view);
48b956c5 2294 if (ret)
b26a6b35 2295 goto err_pm;
6b95a207
KH
2296
2297 /* Install a fence for tiled scan-out. Pre-i965 always needs a
2298 * fence, whereas 965+ only requires a fence if using
2299 * framebuffer compression. For simplicity, we always install
2300 * a fence as the cost is not that onerous.
2301 */
9807216f
VK
2302 if (view.type == I915_GGTT_VIEW_NORMAL) {
2303 ret = i915_gem_object_get_fence(obj);
2304 if (ret == -EDEADLK) {
2305 /*
2306 * -EDEADLK means there are no free fences
2307 * no pending flips.
2308 *
2309 * This is propagated to atomic, but it uses
2310 * -EDEADLK to force a locking recovery, so
2311 * change the returned error to -EBUSY.
2312 */
2313 ret = -EBUSY;
2314 goto err_unpin;
2315 } else if (ret)
2316 goto err_unpin;
1690e1eb 2317
9807216f
VK
2318 i915_gem_object_pin_fence(obj);
2319 }
6b95a207 2320
d6dd6843 2321 intel_runtime_pm_put(dev_priv);
6b95a207 2322 return 0;
48b956c5
CW
2323
2324err_unpin:
f64b98cd 2325 i915_gem_object_unpin_from_display_plane(obj, &view);
b26a6b35 2326err_pm:
d6dd6843 2327 intel_runtime_pm_put(dev_priv);
48b956c5 2328 return ret;
6b95a207
KH
2329}
2330
fb4b8ce1 2331void intel_unpin_fb_obj(struct drm_framebuffer *fb, unsigned int rotation)
1690e1eb 2332{
82bc3b2d 2333 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
f64b98cd 2334 struct i915_ggtt_view view;
82bc3b2d 2335
ebcdd39e
MR
2336 WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
2337
3465c580 2338 intel_fill_fb_ggtt_view(&view, fb, rotation);
f64b98cd 2339
9807216f
VK
2340 if (view.type == I915_GGTT_VIEW_NORMAL)
2341 i915_gem_object_unpin_fence(obj);
2342
f64b98cd 2343 i915_gem_object_unpin_from_display_plane(obj, &view);
1690e1eb
CW
2344}
2345
29cf9491
VS
2346/*
2347 * Adjust the tile offset by moving the difference into
2348 * the x/y offsets.
2349 *
2350 * Input tile dimensions and pitch must already be
2351 * rotated to match x and y, and in pixel units.
2352 */
2353static u32 intel_adjust_tile_offset(int *x, int *y,
2354 unsigned int tile_width,
2355 unsigned int tile_height,
2356 unsigned int tile_size,
2357 unsigned int pitch_tiles,
2358 u32 old_offset,
2359 u32 new_offset)
2360{
2361 unsigned int tiles;
2362
2363 WARN_ON(old_offset & (tile_size - 1));
2364 WARN_ON(new_offset & (tile_size - 1));
2365 WARN_ON(new_offset > old_offset);
2366
2367 tiles = (old_offset - new_offset) / tile_size;
2368
2369 *y += tiles / pitch_tiles * tile_height;
2370 *x += tiles % pitch_tiles * tile_width;
2371
2372 return new_offset;
2373}
2374
8d0deca8
VS
2375/*
2376 * Computes the linear offset to the base tile and adjusts
2377 * x, y. bytes per pixel is assumed to be a power-of-two.
2378 *
2379 * In the 90/270 rotated case, x and y are assumed
2380 * to be already rotated to match the rotated GTT view, and
2381 * pitch is the tile_height aligned framebuffer height.
2382 */
4f2d9934
VS
2383u32 intel_compute_tile_offset(int *x, int *y,
2384 const struct drm_framebuffer *fb, int plane,
8d0deca8
VS
2385 unsigned int pitch,
2386 unsigned int rotation)
c2c75131 2387{
4f2d9934
VS
2388 const struct drm_i915_private *dev_priv = to_i915(fb->dev);
2389 uint64_t fb_modifier = fb->modifier[plane];
2390 unsigned int cpp = drm_format_plane_cpp(fb->pixel_format, plane);
29cf9491
VS
2391 u32 offset, offset_aligned, alignment;
2392
2393 alignment = intel_surf_alignment(dev_priv, fb_modifier);
2394 if (alignment)
2395 alignment--;
2396
b5c65338 2397 if (fb_modifier != DRM_FORMAT_MOD_NONE) {
8d0deca8
VS
2398 unsigned int tile_size, tile_width, tile_height;
2399 unsigned int tile_rows, tiles, pitch_tiles;
c2c75131 2400
d843310d 2401 tile_size = intel_tile_size(dev_priv);
8d0deca8
VS
2402 intel_tile_dims(dev_priv, &tile_width, &tile_height,
2403 fb_modifier, cpp);
2404
2405 if (intel_rotation_90_or_270(rotation)) {
2406 pitch_tiles = pitch / tile_height;
2407 swap(tile_width, tile_height);
2408 } else {
2409 pitch_tiles = pitch / (tile_width * cpp);
2410 }
d843310d
VS
2411
2412 tile_rows = *y / tile_height;
2413 *y %= tile_height;
c2c75131 2414
8d0deca8
VS
2415 tiles = *x / tile_width;
2416 *x %= tile_width;
bc752862 2417
29cf9491
VS
2418 offset = (tile_rows * pitch_tiles + tiles) * tile_size;
2419 offset_aligned = offset & ~alignment;
bc752862 2420
29cf9491
VS
2421 intel_adjust_tile_offset(x, y, tile_width, tile_height,
2422 tile_size, pitch_tiles,
2423 offset, offset_aligned);
2424 } else {
bc752862 2425 offset = *y * pitch + *x * cpp;
29cf9491
VS
2426 offset_aligned = offset & ~alignment;
2427
4e9a86b6
VS
2428 *y = (offset & alignment) / pitch;
2429 *x = ((offset & alignment) - *y * pitch) / cpp;
bc752862 2430 }
29cf9491
VS
2431
2432 return offset_aligned;
c2c75131
DV
2433}
2434
b35d63fa 2435static int i9xx_format_to_fourcc(int format)
46f297fb
JB
2436{
2437 switch (format) {
2438 case DISPPLANE_8BPP:
2439 return DRM_FORMAT_C8;
2440 case DISPPLANE_BGRX555:
2441 return DRM_FORMAT_XRGB1555;
2442 case DISPPLANE_BGRX565:
2443 return DRM_FORMAT_RGB565;
2444 default:
2445 case DISPPLANE_BGRX888:
2446 return DRM_FORMAT_XRGB8888;
2447 case DISPPLANE_RGBX888:
2448 return DRM_FORMAT_XBGR8888;
2449 case DISPPLANE_BGRX101010:
2450 return DRM_FORMAT_XRGB2101010;
2451 case DISPPLANE_RGBX101010:
2452 return DRM_FORMAT_XBGR2101010;
2453 }
2454}
2455
bc8d7dff
DL
2456static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
2457{
2458 switch (format) {
2459 case PLANE_CTL_FORMAT_RGB_565:
2460 return DRM_FORMAT_RGB565;
2461 default:
2462 case PLANE_CTL_FORMAT_XRGB_8888:
2463 if (rgb_order) {
2464 if (alpha)
2465 return DRM_FORMAT_ABGR8888;
2466 else
2467 return DRM_FORMAT_XBGR8888;
2468 } else {
2469 if (alpha)
2470 return DRM_FORMAT_ARGB8888;
2471 else
2472 return DRM_FORMAT_XRGB8888;
2473 }
2474 case PLANE_CTL_FORMAT_XRGB_2101010:
2475 if (rgb_order)
2476 return DRM_FORMAT_XBGR2101010;
2477 else
2478 return DRM_FORMAT_XRGB2101010;
2479 }
2480}
2481
5724dbd1 2482static bool
f6936e29
DV
2483intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
2484 struct intel_initial_plane_config *plane_config)
46f297fb
JB
2485{
2486 struct drm_device *dev = crtc->base.dev;
3badb49f 2487 struct drm_i915_private *dev_priv = to_i915(dev);
72e96d64 2488 struct i915_ggtt *ggtt = &dev_priv->ggtt;
46f297fb
JB
2489 struct drm_i915_gem_object *obj = NULL;
2490 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
2d14030b 2491 struct drm_framebuffer *fb = &plane_config->fb->base;
f37b5c2b
DV
2492 u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
2493 u32 size_aligned = round_up(plane_config->base + plane_config->size,
2494 PAGE_SIZE);
2495
2496 size_aligned -= base_aligned;
46f297fb 2497
ff2652ea
CW
2498 if (plane_config->size == 0)
2499 return false;
2500
3badb49f
PZ
2501 /* If the FB is too big, just don't use it since fbdev is not very
2502 * important and we should probably use that space with FBC or other
2503 * features. */
72e96d64 2504 if (size_aligned * 2 > ggtt->stolen_usable_size)
3badb49f
PZ
2505 return false;
2506
12c83d99
TU
2507 mutex_lock(&dev->struct_mutex);
2508
f37b5c2b
DV
2509 obj = i915_gem_object_create_stolen_for_preallocated(dev,
2510 base_aligned,
2511 base_aligned,
2512 size_aligned);
12c83d99
TU
2513 if (!obj) {
2514 mutex_unlock(&dev->struct_mutex);
484b41dd 2515 return false;
12c83d99 2516 }
46f297fb 2517
49af449b
DL
2518 obj->tiling_mode = plane_config->tiling;
2519 if (obj->tiling_mode == I915_TILING_X)
6bf129df 2520 obj->stride = fb->pitches[0];
46f297fb 2521
6bf129df
DL
2522 mode_cmd.pixel_format = fb->pixel_format;
2523 mode_cmd.width = fb->width;
2524 mode_cmd.height = fb->height;
2525 mode_cmd.pitches[0] = fb->pitches[0];
18c5247e
DV
2526 mode_cmd.modifier[0] = fb->modifier[0];
2527 mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
46f297fb 2528
6bf129df 2529 if (intel_framebuffer_init(dev, to_intel_framebuffer(fb),
484b41dd 2530 &mode_cmd, obj)) {
46f297fb
JB
2531 DRM_DEBUG_KMS("intel fb init failed\n");
2532 goto out_unref_obj;
2533 }
12c83d99 2534
46f297fb 2535 mutex_unlock(&dev->struct_mutex);
484b41dd 2536
f6936e29 2537 DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
484b41dd 2538 return true;
46f297fb
JB
2539
2540out_unref_obj:
2541 drm_gem_object_unreference(&obj->base);
2542 mutex_unlock(&dev->struct_mutex);
484b41dd
JB
2543 return false;
2544}
2545
5a21b665
DV
2546/* Update plane->state->fb to match plane->fb after driver-internal updates */
2547static void
2548update_state_fb(struct drm_plane *plane)
2549{
2550 if (plane->fb == plane->state->fb)
2551 return;
2552
2553 if (plane->state->fb)
2554 drm_framebuffer_unreference(plane->state->fb);
2555 plane->state->fb = plane->fb;
2556 if (plane->state->fb)
2557 drm_framebuffer_reference(plane->state->fb);
2558}
2559
5724dbd1 2560static void
f6936e29
DV
2561intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
2562 struct intel_initial_plane_config *plane_config)
484b41dd
JB
2563{
2564 struct drm_device *dev = intel_crtc->base.dev;
d9ceb816 2565 struct drm_i915_private *dev_priv = dev->dev_private;
484b41dd
JB
2566 struct drm_crtc *c;
2567 struct intel_crtc *i;
2ff8fde1 2568 struct drm_i915_gem_object *obj;
88595ac9 2569 struct drm_plane *primary = intel_crtc->base.primary;
be5651f2 2570 struct drm_plane_state *plane_state = primary->state;
200757f5
MR
2571 struct drm_crtc_state *crtc_state = intel_crtc->base.state;
2572 struct intel_plane *intel_plane = to_intel_plane(primary);
0a8d8a86
MR
2573 struct intel_plane_state *intel_state =
2574 to_intel_plane_state(plane_state);
88595ac9 2575 struct drm_framebuffer *fb;
484b41dd 2576
2d14030b 2577 if (!plane_config->fb)
484b41dd
JB
2578 return;
2579
f6936e29 2580 if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
88595ac9
DV
2581 fb = &plane_config->fb->base;
2582 goto valid_fb;
f55548b5 2583 }
484b41dd 2584
2d14030b 2585 kfree(plane_config->fb);
484b41dd
JB
2586
2587 /*
2588 * Failed to alloc the obj, check to see if we should share
2589 * an fb with another CRTC instead
2590 */
70e1e0ec 2591 for_each_crtc(dev, c) {
484b41dd
JB
2592 i = to_intel_crtc(c);
2593
2594 if (c == &intel_crtc->base)
2595 continue;
2596
2ff8fde1
MR
2597 if (!i->active)
2598 continue;
2599
88595ac9
DV
2600 fb = c->primary->fb;
2601 if (!fb)
484b41dd
JB
2602 continue;
2603
88595ac9 2604 obj = intel_fb_obj(fb);
2ff8fde1 2605 if (i915_gem_obj_ggtt_offset(obj) == plane_config->base) {
88595ac9
DV
2606 drm_framebuffer_reference(fb);
2607 goto valid_fb;
484b41dd
JB
2608 }
2609 }
88595ac9 2610
200757f5
MR
2611 /*
2612 * We've failed to reconstruct the BIOS FB. Current display state
2613 * indicates that the primary plane is visible, but has a NULL FB,
2614 * which will lead to problems later if we don't fix it up. The
2615 * simplest solution is to just disable the primary plane now and
2616 * pretend the BIOS never had it enabled.
2617 */
2618 to_intel_plane_state(plane_state)->visible = false;
2619 crtc_state->plane_mask &= ~(1 << drm_plane_index(primary));
2622a081 2620 intel_pre_disable_primary_noatomic(&intel_crtc->base);
200757f5
MR
2621 intel_plane->disable_plane(primary, &intel_crtc->base);
2622
88595ac9
DV
2623 return;
2624
2625valid_fb:
f44e2659
VS
2626 plane_state->src_x = 0;
2627 plane_state->src_y = 0;
be5651f2
ML
2628 plane_state->src_w = fb->width << 16;
2629 plane_state->src_h = fb->height << 16;
2630
f44e2659
VS
2631 plane_state->crtc_x = 0;
2632 plane_state->crtc_y = 0;
be5651f2
ML
2633 plane_state->crtc_w = fb->width;
2634 plane_state->crtc_h = fb->height;
2635
0a8d8a86
MR
2636 intel_state->src.x1 = plane_state->src_x;
2637 intel_state->src.y1 = plane_state->src_y;
2638 intel_state->src.x2 = plane_state->src_x + plane_state->src_w;
2639 intel_state->src.y2 = plane_state->src_y + plane_state->src_h;
2640 intel_state->dst.x1 = plane_state->crtc_x;
2641 intel_state->dst.y1 = plane_state->crtc_y;
2642 intel_state->dst.x2 = plane_state->crtc_x + plane_state->crtc_w;
2643 intel_state->dst.y2 = plane_state->crtc_y + plane_state->crtc_h;
2644
88595ac9
DV
2645 obj = intel_fb_obj(fb);
2646 if (obj->tiling_mode != I915_TILING_NONE)
2647 dev_priv->preserve_bios_swizzle = true;
2648
be5651f2
ML
2649 drm_framebuffer_reference(fb);
2650 primary->fb = primary->state->fb = fb;
36750f28 2651 primary->crtc = primary->state->crtc = &intel_crtc->base;
36750f28 2652 intel_crtc->base.state->plane_mask |= (1 << drm_plane_index(primary));
a9ff8714 2653 obj->frontbuffer_bits |= to_intel_plane(primary)->frontbuffer_bit;
46f297fb
JB
2654}
2655
a8d201af
ML
2656static void i9xx_update_primary_plane(struct drm_plane *primary,
2657 const struct intel_crtc_state *crtc_state,
2658 const struct intel_plane_state *plane_state)
81255565 2659{
a8d201af 2660 struct drm_device *dev = primary->dev;
81255565 2661 struct drm_i915_private *dev_priv = dev->dev_private;
a8d201af
ML
2662 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
2663 struct drm_framebuffer *fb = plane_state->base.fb;
2664 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
81255565 2665 int plane = intel_crtc->plane;
54ea9da8 2666 u32 linear_offset;
81255565 2667 u32 dspcntr;
f0f59a00 2668 i915_reg_t reg = DSPCNTR(plane);
8d0deca8 2669 unsigned int rotation = plane_state->base.rotation;
ac484963 2670 int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
54ea9da8
VS
2671 int x = plane_state->src.x1 >> 16;
2672 int y = plane_state->src.y1 >> 16;
c9ba6fad 2673
f45651ba
VS
2674 dspcntr = DISPPLANE_GAMMA_ENABLE;
2675
fdd508a6 2676 dspcntr |= DISPLAY_PLANE_ENABLE;
f45651ba
VS
2677
2678 if (INTEL_INFO(dev)->gen < 4) {
2679 if (intel_crtc->pipe == PIPE_B)
2680 dspcntr |= DISPPLANE_SEL_PIPE_B;
2681
2682 /* pipesrc and dspsize control the size that is scaled from,
2683 * which should always be the user's requested size.
2684 */
2685 I915_WRITE(DSPSIZE(plane),
a8d201af
ML
2686 ((crtc_state->pipe_src_h - 1) << 16) |
2687 (crtc_state->pipe_src_w - 1));
f45651ba 2688 I915_WRITE(DSPPOS(plane), 0);
c14b0485
VS
2689 } else if (IS_CHERRYVIEW(dev) && plane == PLANE_B) {
2690 I915_WRITE(PRIMSIZE(plane),
a8d201af
ML
2691 ((crtc_state->pipe_src_h - 1) << 16) |
2692 (crtc_state->pipe_src_w - 1));
c14b0485
VS
2693 I915_WRITE(PRIMPOS(plane), 0);
2694 I915_WRITE(PRIMCNSTALPHA(plane), 0);
f45651ba 2695 }
81255565 2696
57779d06
VS
2697 switch (fb->pixel_format) {
2698 case DRM_FORMAT_C8:
81255565
JB
2699 dspcntr |= DISPPLANE_8BPP;
2700 break;
57779d06 2701 case DRM_FORMAT_XRGB1555:
57779d06 2702 dspcntr |= DISPPLANE_BGRX555;
81255565 2703 break;
57779d06
VS
2704 case DRM_FORMAT_RGB565:
2705 dspcntr |= DISPPLANE_BGRX565;
2706 break;
2707 case DRM_FORMAT_XRGB8888:
57779d06
VS
2708 dspcntr |= DISPPLANE_BGRX888;
2709 break;
2710 case DRM_FORMAT_XBGR8888:
57779d06
VS
2711 dspcntr |= DISPPLANE_RGBX888;
2712 break;
2713 case DRM_FORMAT_XRGB2101010:
57779d06
VS
2714 dspcntr |= DISPPLANE_BGRX101010;
2715 break;
2716 case DRM_FORMAT_XBGR2101010:
57779d06 2717 dspcntr |= DISPPLANE_RGBX101010;
81255565
JB
2718 break;
2719 default:
baba133a 2720 BUG();
81255565 2721 }
57779d06 2722
f45651ba
VS
2723 if (INTEL_INFO(dev)->gen >= 4 &&
2724 obj->tiling_mode != I915_TILING_NONE)
2725 dspcntr |= DISPPLANE_TILED;
81255565 2726
de1aa629
VS
2727 if (IS_G4X(dev))
2728 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
2729
ac484963 2730 linear_offset = y * fb->pitches[0] + x * cpp;
81255565 2731
c2c75131
DV
2732 if (INTEL_INFO(dev)->gen >= 4) {
2733 intel_crtc->dspaddr_offset =
4f2d9934 2734 intel_compute_tile_offset(&x, &y, fb, 0,
8d0deca8 2735 fb->pitches[0], rotation);
c2c75131
DV
2736 linear_offset -= intel_crtc->dspaddr_offset;
2737 } else {
e506a0c6 2738 intel_crtc->dspaddr_offset = linear_offset;
c2c75131 2739 }
e506a0c6 2740
8d0deca8 2741 if (rotation == BIT(DRM_ROTATE_180)) {
48404c1e
SJ
2742 dspcntr |= DISPPLANE_ROTATE_180;
2743
a8d201af
ML
2744 x += (crtc_state->pipe_src_w - 1);
2745 y += (crtc_state->pipe_src_h - 1);
48404c1e
SJ
2746
2747 /* Finding the last pixel of the last line of the display
2748 data and adding to linear_offset*/
2749 linear_offset +=
a8d201af 2750 (crtc_state->pipe_src_h - 1) * fb->pitches[0] +
ac484963 2751 (crtc_state->pipe_src_w - 1) * cpp;
48404c1e
SJ
2752 }
2753
2db3366b
PZ
2754 intel_crtc->adjusted_x = x;
2755 intel_crtc->adjusted_y = y;
2756
48404c1e
SJ
2757 I915_WRITE(reg, dspcntr);
2758
01f2c773 2759 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
a6c45cf0 2760 if (INTEL_INFO(dev)->gen >= 4) {
85ba7b7d
DV
2761 I915_WRITE(DSPSURF(plane),
2762 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
5eddb70b 2763 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
e506a0c6 2764 I915_WRITE(DSPLINOFF(plane), linear_offset);
5eddb70b 2765 } else
f343c5f6 2766 I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
5eddb70b 2767 POSTING_READ(reg);
17638cd6
JB
2768}
2769
a8d201af
ML
2770static void i9xx_disable_primary_plane(struct drm_plane *primary,
2771 struct drm_crtc *crtc)
17638cd6
JB
2772{
2773 struct drm_device *dev = crtc->dev;
2774 struct drm_i915_private *dev_priv = dev->dev_private;
2775 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
17638cd6 2776 int plane = intel_crtc->plane;
f45651ba 2777
a8d201af
ML
2778 I915_WRITE(DSPCNTR(plane), 0);
2779 if (INTEL_INFO(dev_priv)->gen >= 4)
fdd508a6 2780 I915_WRITE(DSPSURF(plane), 0);
a8d201af
ML
2781 else
2782 I915_WRITE(DSPADDR(plane), 0);
2783 POSTING_READ(DSPCNTR(plane));
2784}
c9ba6fad 2785
a8d201af
ML
2786static void ironlake_update_primary_plane(struct drm_plane *primary,
2787 const struct intel_crtc_state *crtc_state,
2788 const struct intel_plane_state *plane_state)
2789{
2790 struct drm_device *dev = primary->dev;
2791 struct drm_i915_private *dev_priv = dev->dev_private;
2792 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
2793 struct drm_framebuffer *fb = plane_state->base.fb;
2794 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
2795 int plane = intel_crtc->plane;
54ea9da8 2796 u32 linear_offset;
a8d201af
ML
2797 u32 dspcntr;
2798 i915_reg_t reg = DSPCNTR(plane);
8d0deca8 2799 unsigned int rotation = plane_state->base.rotation;
ac484963 2800 int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
a8d201af
ML
2801 int x = plane_state->src.x1 >> 16;
2802 int y = plane_state->src.y1 >> 16;
c9ba6fad 2803
f45651ba 2804 dspcntr = DISPPLANE_GAMMA_ENABLE;
fdd508a6 2805 dspcntr |= DISPLAY_PLANE_ENABLE;
f45651ba
VS
2806
2807 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
2808 dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
17638cd6 2809
57779d06
VS
2810 switch (fb->pixel_format) {
2811 case DRM_FORMAT_C8:
17638cd6
JB
2812 dspcntr |= DISPPLANE_8BPP;
2813 break;
57779d06
VS
2814 case DRM_FORMAT_RGB565:
2815 dspcntr |= DISPPLANE_BGRX565;
17638cd6 2816 break;
57779d06 2817 case DRM_FORMAT_XRGB8888:
57779d06
VS
2818 dspcntr |= DISPPLANE_BGRX888;
2819 break;
2820 case DRM_FORMAT_XBGR8888:
57779d06
VS
2821 dspcntr |= DISPPLANE_RGBX888;
2822 break;
2823 case DRM_FORMAT_XRGB2101010:
57779d06
VS
2824 dspcntr |= DISPPLANE_BGRX101010;
2825 break;
2826 case DRM_FORMAT_XBGR2101010:
57779d06 2827 dspcntr |= DISPPLANE_RGBX101010;
17638cd6
JB
2828 break;
2829 default:
baba133a 2830 BUG();
17638cd6
JB
2831 }
2832
2833 if (obj->tiling_mode != I915_TILING_NONE)
2834 dspcntr |= DISPPLANE_TILED;
17638cd6 2835
f45651ba 2836 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev))
1f5d76db 2837 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
17638cd6 2838
ac484963 2839 linear_offset = y * fb->pitches[0] + x * cpp;
c2c75131 2840 intel_crtc->dspaddr_offset =
4f2d9934 2841 intel_compute_tile_offset(&x, &y, fb, 0,
8d0deca8 2842 fb->pitches[0], rotation);
c2c75131 2843 linear_offset -= intel_crtc->dspaddr_offset;
8d0deca8 2844 if (rotation == BIT(DRM_ROTATE_180)) {
48404c1e
SJ
2845 dspcntr |= DISPPLANE_ROTATE_180;
2846
2847 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev)) {
a8d201af
ML
2848 x += (crtc_state->pipe_src_w - 1);
2849 y += (crtc_state->pipe_src_h - 1);
48404c1e
SJ
2850
2851 /* Finding the last pixel of the last line of the display
2852 data and adding to linear_offset*/
2853 linear_offset +=
a8d201af 2854 (crtc_state->pipe_src_h - 1) * fb->pitches[0] +
ac484963 2855 (crtc_state->pipe_src_w - 1) * cpp;
48404c1e
SJ
2856 }
2857 }
2858
2db3366b
PZ
2859 intel_crtc->adjusted_x = x;
2860 intel_crtc->adjusted_y = y;
2861
48404c1e 2862 I915_WRITE(reg, dspcntr);
17638cd6 2863
01f2c773 2864 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
85ba7b7d
DV
2865 I915_WRITE(DSPSURF(plane),
2866 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
b3dc685e 2867 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
bc1c91eb
DL
2868 I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
2869 } else {
2870 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2871 I915_WRITE(DSPLINOFF(plane), linear_offset);
2872 }
17638cd6 2873 POSTING_READ(reg);
17638cd6
JB
2874}
2875
7b49f948
VS
2876u32 intel_fb_stride_alignment(const struct drm_i915_private *dev_priv,
2877 uint64_t fb_modifier, uint32_t pixel_format)
b321803d 2878{
7b49f948 2879 if (fb_modifier == DRM_FORMAT_MOD_NONE) {
b321803d 2880 return 64;
7b49f948
VS
2881 } else {
2882 int cpp = drm_format_plane_cpp(pixel_format, 0);
2883
27ba3910 2884 return intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
b321803d
DL
2885 }
2886}
2887
44eb0cb9
MK
2888u32 intel_plane_obj_offset(struct intel_plane *intel_plane,
2889 struct drm_i915_gem_object *obj,
2890 unsigned int plane)
121920fa 2891{
ce7f1728 2892 struct i915_ggtt_view view;
dedf278c 2893 struct i915_vma *vma;
44eb0cb9 2894 u64 offset;
121920fa 2895
e7941294 2896 intel_fill_fb_ggtt_view(&view, intel_plane->base.state->fb,
3465c580 2897 intel_plane->base.state->rotation);
121920fa 2898
ce7f1728 2899 vma = i915_gem_obj_to_ggtt_view(obj, &view);
dedf278c 2900 if (WARN(!vma, "ggtt vma for display object not found! (view=%u)\n",
ce7f1728 2901 view.type))
dedf278c
TU
2902 return -1;
2903
44eb0cb9 2904 offset = vma->node.start;
dedf278c
TU
2905
2906 if (plane == 1) {
7723f47d 2907 offset += vma->ggtt_view.params.rotated.uv_start_page *
dedf278c
TU
2908 PAGE_SIZE;
2909 }
2910
44eb0cb9
MK
2911 WARN_ON(upper_32_bits(offset));
2912
2913 return lower_32_bits(offset);
121920fa
TU
2914}
2915
e435d6e5
ML
2916static void skl_detach_scaler(struct intel_crtc *intel_crtc, int id)
2917{
2918 struct drm_device *dev = intel_crtc->base.dev;
2919 struct drm_i915_private *dev_priv = dev->dev_private;
2920
2921 I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, id), 0);
2922 I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, id), 0);
2923 I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, id), 0);
e435d6e5
ML
2924}
2925
a1b2278e
CK
2926/*
2927 * This function detaches (aka. unbinds) unused scalers in hardware
2928 */
0583236e 2929static void skl_detach_scalers(struct intel_crtc *intel_crtc)
a1b2278e 2930{
a1b2278e
CK
2931 struct intel_crtc_scaler_state *scaler_state;
2932 int i;
2933
a1b2278e
CK
2934 scaler_state = &intel_crtc->config->scaler_state;
2935
2936 /* loop through and disable scalers that aren't in use */
2937 for (i = 0; i < intel_crtc->num_scalers; i++) {
e435d6e5
ML
2938 if (!scaler_state->scalers[i].in_use)
2939 skl_detach_scaler(intel_crtc, i);
a1b2278e
CK
2940 }
2941}
2942
6156a456 2943u32 skl_plane_ctl_format(uint32_t pixel_format)
70d21f0e 2944{
6156a456 2945 switch (pixel_format) {
d161cf7a 2946 case DRM_FORMAT_C8:
c34ce3d1 2947 return PLANE_CTL_FORMAT_INDEXED;
70d21f0e 2948 case DRM_FORMAT_RGB565:
c34ce3d1 2949 return PLANE_CTL_FORMAT_RGB_565;
70d21f0e 2950 case DRM_FORMAT_XBGR8888:
c34ce3d1 2951 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
6156a456 2952 case DRM_FORMAT_XRGB8888:
c34ce3d1 2953 return PLANE_CTL_FORMAT_XRGB_8888;
6156a456
CK
2954 /*
2955 * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
2956 * to be already pre-multiplied. We need to add a knob (or a different
2957 * DRM_FORMAT) for user-space to configure that.
2958 */
f75fb42a 2959 case DRM_FORMAT_ABGR8888:
c34ce3d1 2960 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX |
6156a456 2961 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
6156a456 2962 case DRM_FORMAT_ARGB8888:
c34ce3d1 2963 return PLANE_CTL_FORMAT_XRGB_8888 |
6156a456 2964 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
70d21f0e 2965 case DRM_FORMAT_XRGB2101010:
c34ce3d1 2966 return PLANE_CTL_FORMAT_XRGB_2101010;
70d21f0e 2967 case DRM_FORMAT_XBGR2101010:
c34ce3d1 2968 return PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
6156a456 2969 case DRM_FORMAT_YUYV:
c34ce3d1 2970 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
6156a456 2971 case DRM_FORMAT_YVYU:
c34ce3d1 2972 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
6156a456 2973 case DRM_FORMAT_UYVY:
c34ce3d1 2974 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
6156a456 2975 case DRM_FORMAT_VYUY:
c34ce3d1 2976 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
70d21f0e 2977 default:
4249eeef 2978 MISSING_CASE(pixel_format);
70d21f0e 2979 }
8cfcba41 2980
c34ce3d1 2981 return 0;
6156a456 2982}
70d21f0e 2983
6156a456
CK
2984u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
2985{
6156a456 2986 switch (fb_modifier) {
30af77c4 2987 case DRM_FORMAT_MOD_NONE:
70d21f0e 2988 break;
30af77c4 2989 case I915_FORMAT_MOD_X_TILED:
c34ce3d1 2990 return PLANE_CTL_TILED_X;
b321803d 2991 case I915_FORMAT_MOD_Y_TILED:
c34ce3d1 2992 return PLANE_CTL_TILED_Y;
b321803d 2993 case I915_FORMAT_MOD_Yf_TILED:
c34ce3d1 2994 return PLANE_CTL_TILED_YF;
70d21f0e 2995 default:
6156a456 2996 MISSING_CASE(fb_modifier);
70d21f0e 2997 }
8cfcba41 2998
c34ce3d1 2999 return 0;
6156a456 3000}
70d21f0e 3001
6156a456
CK
3002u32 skl_plane_ctl_rotation(unsigned int rotation)
3003{
3b7a5119 3004 switch (rotation) {
6156a456
CK
3005 case BIT(DRM_ROTATE_0):
3006 break;
1e8df167
SJ
3007 /*
3008 * DRM_ROTATE_ is counter clockwise to stay compatible with Xrandr
3009 * while i915 HW rotation is clockwise, thats why this swapping.
3010 */
3b7a5119 3011 case BIT(DRM_ROTATE_90):
1e8df167 3012 return PLANE_CTL_ROTATE_270;
3b7a5119 3013 case BIT(DRM_ROTATE_180):
c34ce3d1 3014 return PLANE_CTL_ROTATE_180;
3b7a5119 3015 case BIT(DRM_ROTATE_270):
1e8df167 3016 return PLANE_CTL_ROTATE_90;
6156a456
CK
3017 default:
3018 MISSING_CASE(rotation);
3019 }
3020
c34ce3d1 3021 return 0;
6156a456
CK
3022}
3023
a8d201af
ML
3024static void skylake_update_primary_plane(struct drm_plane *plane,
3025 const struct intel_crtc_state *crtc_state,
3026 const struct intel_plane_state *plane_state)
6156a456 3027{
a8d201af 3028 struct drm_device *dev = plane->dev;
6156a456 3029 struct drm_i915_private *dev_priv = dev->dev_private;
a8d201af
ML
3030 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
3031 struct drm_framebuffer *fb = plane_state->base.fb;
3032 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
6156a456
CK
3033 int pipe = intel_crtc->pipe;
3034 u32 plane_ctl, stride_div, stride;
3035 u32 tile_height, plane_offset, plane_size;
a8d201af 3036 unsigned int rotation = plane_state->base.rotation;
6156a456 3037 int x_offset, y_offset;
44eb0cb9 3038 u32 surf_addr;
a8d201af
ML
3039 int scaler_id = plane_state->scaler_id;
3040 int src_x = plane_state->src.x1 >> 16;
3041 int src_y = plane_state->src.y1 >> 16;
3042 int src_w = drm_rect_width(&plane_state->src) >> 16;
3043 int src_h = drm_rect_height(&plane_state->src) >> 16;
3044 int dst_x = plane_state->dst.x1;
3045 int dst_y = plane_state->dst.y1;
3046 int dst_w = drm_rect_width(&plane_state->dst);
3047 int dst_h = drm_rect_height(&plane_state->dst);
70d21f0e 3048
6156a456
CK
3049 plane_ctl = PLANE_CTL_ENABLE |
3050 PLANE_CTL_PIPE_GAMMA_ENABLE |
3051 PLANE_CTL_PIPE_CSC_ENABLE;
3052
3053 plane_ctl |= skl_plane_ctl_format(fb->pixel_format);
3054 plane_ctl |= skl_plane_ctl_tiling(fb->modifier[0]);
3055 plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
6156a456
CK
3056 plane_ctl |= skl_plane_ctl_rotation(rotation);
3057
7b49f948 3058 stride_div = intel_fb_stride_alignment(dev_priv, fb->modifier[0],
b321803d 3059 fb->pixel_format);
dedf278c 3060 surf_addr = intel_plane_obj_offset(to_intel_plane(plane), obj, 0);
3b7a5119 3061
a42e5a23
PZ
3062 WARN_ON(drm_rect_width(&plane_state->src) == 0);
3063
3b7a5119 3064 if (intel_rotation_90_or_270(rotation)) {
832be82f
VS
3065 int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
3066
3b7a5119 3067 /* stride = Surface height in tiles */
832be82f 3068 tile_height = intel_tile_height(dev_priv, fb->modifier[0], cpp);
3b7a5119 3069 stride = DIV_ROUND_UP(fb->height, tile_height);
a8d201af
ML
3070 x_offset = stride * tile_height - src_y - src_h;
3071 y_offset = src_x;
6156a456 3072 plane_size = (src_w - 1) << 16 | (src_h - 1);
3b7a5119
SJ
3073 } else {
3074 stride = fb->pitches[0] / stride_div;
a8d201af
ML
3075 x_offset = src_x;
3076 y_offset = src_y;
6156a456 3077 plane_size = (src_h - 1) << 16 | (src_w - 1);
3b7a5119
SJ
3078 }
3079 plane_offset = y_offset << 16 | x_offset;
b321803d 3080
2db3366b
PZ
3081 intel_crtc->adjusted_x = x_offset;
3082 intel_crtc->adjusted_y = y_offset;
3083
70d21f0e 3084 I915_WRITE(PLANE_CTL(pipe, 0), plane_ctl);
3b7a5119
SJ
3085 I915_WRITE(PLANE_OFFSET(pipe, 0), plane_offset);
3086 I915_WRITE(PLANE_SIZE(pipe, 0), plane_size);
3087 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
6156a456
CK
3088
3089 if (scaler_id >= 0) {
3090 uint32_t ps_ctrl = 0;
3091
3092 WARN_ON(!dst_w || !dst_h);
3093 ps_ctrl = PS_SCALER_EN | PS_PLANE_SEL(0) |
3094 crtc_state->scaler_state.scalers[scaler_id].mode;
3095 I915_WRITE(SKL_PS_CTRL(pipe, scaler_id), ps_ctrl);
3096 I915_WRITE(SKL_PS_PWR_GATE(pipe, scaler_id), 0);
3097 I915_WRITE(SKL_PS_WIN_POS(pipe, scaler_id), (dst_x << 16) | dst_y);
3098 I915_WRITE(SKL_PS_WIN_SZ(pipe, scaler_id), (dst_w << 16) | dst_h);
3099 I915_WRITE(PLANE_POS(pipe, 0), 0);
3100 } else {
3101 I915_WRITE(PLANE_POS(pipe, 0), (dst_y << 16) | dst_x);
3102 }
3103
121920fa 3104 I915_WRITE(PLANE_SURF(pipe, 0), surf_addr);
70d21f0e
DL
3105
3106 POSTING_READ(PLANE_SURF(pipe, 0));
3107}
3108
a8d201af
ML
3109static void skylake_disable_primary_plane(struct drm_plane *primary,
3110 struct drm_crtc *crtc)
17638cd6
JB
3111{
3112 struct drm_device *dev = crtc->dev;
3113 struct drm_i915_private *dev_priv = dev->dev_private;
a8d201af 3114 int pipe = to_intel_crtc(crtc)->pipe;
17638cd6 3115
a8d201af
ML
3116 I915_WRITE(PLANE_CTL(pipe, 0), 0);
3117 I915_WRITE(PLANE_SURF(pipe, 0), 0);
3118 POSTING_READ(PLANE_SURF(pipe, 0));
3119}
29b9bde6 3120
a8d201af
ML
3121/* Assume fb object is pinned & idle & fenced and just update base pointers */
3122static int
3123intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
3124 int x, int y, enum mode_set_atomic state)
3125{
3126 /* Support for kgdboc is disabled, this needs a major rework. */
3127 DRM_ERROR("legacy panic handler not supported any more.\n");
3128
3129 return -ENODEV;
81255565
JB
3130}
3131
5a21b665
DV
3132static void intel_complete_page_flips(struct drm_i915_private *dev_priv)
3133{
3134 struct intel_crtc *crtc;
3135
3136 for_each_intel_crtc(dev_priv->dev, crtc)
3137 intel_finish_page_flip_cs(dev_priv, crtc->pipe);
3138}
3139
7514747d
VS
3140static void intel_update_primary_planes(struct drm_device *dev)
3141{
7514747d 3142 struct drm_crtc *crtc;
96a02917 3143
70e1e0ec 3144 for_each_crtc(dev, crtc) {
11c22da6
ML
3145 struct intel_plane *plane = to_intel_plane(crtc->primary);
3146 struct intel_plane_state *plane_state;
96a02917 3147
11c22da6 3148 drm_modeset_lock_crtc(crtc, &plane->base);
11c22da6
ML
3149 plane_state = to_intel_plane_state(plane->base.state);
3150
a8d201af
ML
3151 if (plane_state->visible)
3152 plane->update_plane(&plane->base,
3153 to_intel_crtc_state(crtc->state),
3154 plane_state);
11c22da6
ML
3155
3156 drm_modeset_unlock_crtc(crtc);
96a02917
VS
3157 }
3158}
3159
c033666a 3160void intel_prepare_reset(struct drm_i915_private *dev_priv)
7514747d
VS
3161{
3162 /* no reset support for gen2 */
c033666a 3163 if (IS_GEN2(dev_priv))
7514747d
VS
3164 return;
3165
3166 /* reset doesn't touch the display */
c033666a 3167 if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
7514747d
VS
3168 return;
3169
c033666a 3170 drm_modeset_lock_all(dev_priv->dev);
f98ce92f
VS
3171 /*
3172 * Disabling the crtcs gracefully seems nicer. Also the
3173 * g33 docs say we should at least disable all the planes.
3174 */
c033666a 3175 intel_display_suspend(dev_priv->dev);
7514747d
VS
3176}
3177
c033666a 3178void intel_finish_reset(struct drm_i915_private *dev_priv)
7514747d 3179{
5a21b665
DV
3180 /*
3181 * Flips in the rings will be nuked by the reset,
3182 * so complete all pending flips so that user space
3183 * will get its events and not get stuck.
3184 */
3185 intel_complete_page_flips(dev_priv);
3186
7514747d 3187 /* no reset support for gen2 */
c033666a 3188 if (IS_GEN2(dev_priv))
7514747d
VS
3189 return;
3190
3191 /* reset doesn't touch the display */
c033666a 3192 if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv)) {
7514747d
VS
3193 /*
3194 * Flips in the rings have been nuked by the reset,
3195 * so update the base address of all primary
3196 * planes to the the last fb to make sure we're
3197 * showing the correct fb after a reset.
11c22da6
ML
3198 *
3199 * FIXME: Atomic will make this obsolete since we won't schedule
3200 * CS-based flips (which might get lost in gpu resets) any more.
7514747d 3201 */
c033666a 3202 intel_update_primary_planes(dev_priv->dev);
7514747d
VS
3203 return;
3204 }
3205
3206 /*
3207 * The display has been reset as well,
3208 * so need a full re-initialization.
3209 */
3210 intel_runtime_pm_disable_interrupts(dev_priv);
3211 intel_runtime_pm_enable_interrupts(dev_priv);
3212
c033666a 3213 intel_modeset_init_hw(dev_priv->dev);
7514747d
VS
3214
3215 spin_lock_irq(&dev_priv->irq_lock);
3216 if (dev_priv->display.hpd_irq_setup)
91d14251 3217 dev_priv->display.hpd_irq_setup(dev_priv);
7514747d
VS
3218 spin_unlock_irq(&dev_priv->irq_lock);
3219
c033666a 3220 intel_display_resume(dev_priv->dev);
7514747d
VS
3221
3222 intel_hpd_init(dev_priv);
3223
c033666a 3224 drm_modeset_unlock_all(dev_priv->dev);
7514747d
VS
3225}
3226
7d5e3799
CW
3227static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
3228{
5a21b665
DV
3229 struct drm_device *dev = crtc->dev;
3230 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3231 unsigned reset_counter;
3232 bool pending;
3233
3234 reset_counter = i915_reset_counter(&to_i915(dev)->gpu_error);
3235 if (intel_crtc->reset_counter != reset_counter)
3236 return false;
3237
3238 spin_lock_irq(&dev->event_lock);
3239 pending = to_intel_crtc(crtc)->flip_work != NULL;
3240 spin_unlock_irq(&dev->event_lock);
3241
3242 return pending;
7d5e3799
CW
3243}
3244
bfd16b2a
ML
3245static void intel_update_pipe_config(struct intel_crtc *crtc,
3246 struct intel_crtc_state *old_crtc_state)
e30e8f75
GP
3247{
3248 struct drm_device *dev = crtc->base.dev;
3249 struct drm_i915_private *dev_priv = dev->dev_private;
bfd16b2a
ML
3250 struct intel_crtc_state *pipe_config =
3251 to_intel_crtc_state(crtc->base.state);
e30e8f75 3252
bfd16b2a
ML
3253 /* drm_atomic_helper_update_legacy_modeset_state might not be called. */
3254 crtc->base.mode = crtc->base.state->mode;
3255
3256 DRM_DEBUG_KMS("Updating pipe size %ix%i -> %ix%i\n",
3257 old_crtc_state->pipe_src_w, old_crtc_state->pipe_src_h,
3258 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
e30e8f75
GP
3259
3260 /*
3261 * Update pipe size and adjust fitter if needed: the reason for this is
3262 * that in compute_mode_changes we check the native mode (not the pfit
3263 * mode) to see if we can flip rather than do a full mode set. In the
3264 * fastboot case, we'll flip, but if we don't update the pipesrc and
3265 * pfit state, we'll end up with a big fb scanned out into the wrong
3266 * sized surface.
e30e8f75
GP
3267 */
3268
e30e8f75 3269 I915_WRITE(PIPESRC(crtc->pipe),
bfd16b2a
ML
3270 ((pipe_config->pipe_src_w - 1) << 16) |
3271 (pipe_config->pipe_src_h - 1));
3272
3273 /* on skylake this is done by detaching scalers */
3274 if (INTEL_INFO(dev)->gen >= 9) {
3275 skl_detach_scalers(crtc);
3276
3277 if (pipe_config->pch_pfit.enabled)
3278 skylake_pfit_enable(crtc);
3279 } else if (HAS_PCH_SPLIT(dev)) {
3280 if (pipe_config->pch_pfit.enabled)
3281 ironlake_pfit_enable(crtc);
3282 else if (old_crtc_state->pch_pfit.enabled)
3283 ironlake_pfit_disable(crtc, true);
e30e8f75 3284 }
e30e8f75
GP
3285}
3286
5e84e1a4
ZW
3287static void intel_fdi_normal_train(struct drm_crtc *crtc)
3288{
3289 struct drm_device *dev = crtc->dev;
3290 struct drm_i915_private *dev_priv = dev->dev_private;
3291 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3292 int pipe = intel_crtc->pipe;
f0f59a00
VS
3293 i915_reg_t reg;
3294 u32 temp;
5e84e1a4
ZW
3295
3296 /* enable normal train */
3297 reg = FDI_TX_CTL(pipe);
3298 temp = I915_READ(reg);
61e499bf 3299 if (IS_IVYBRIDGE(dev)) {
357555c0
JB
3300 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3301 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
61e499bf
KP
3302 } else {
3303 temp &= ~FDI_LINK_TRAIN_NONE;
3304 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
357555c0 3305 }
5e84e1a4
ZW
3306 I915_WRITE(reg, temp);
3307
3308 reg = FDI_RX_CTL(pipe);
3309 temp = I915_READ(reg);
3310 if (HAS_PCH_CPT(dev)) {
3311 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3312 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
3313 } else {
3314 temp &= ~FDI_LINK_TRAIN_NONE;
3315 temp |= FDI_LINK_TRAIN_NONE;
3316 }
3317 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
3318
3319 /* wait one idle pattern time */
3320 POSTING_READ(reg);
3321 udelay(1000);
357555c0
JB
3322
3323 /* IVB wants error correction enabled */
3324 if (IS_IVYBRIDGE(dev))
3325 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
3326 FDI_FE_ERRC_ENABLE);
5e84e1a4
ZW
3327}
3328
8db9d77b
ZW
3329/* The FDI link training functions for ILK/Ibexpeak. */
3330static void ironlake_fdi_link_train(struct drm_crtc *crtc)
3331{
3332 struct drm_device *dev = crtc->dev;
3333 struct drm_i915_private *dev_priv = dev->dev_private;
3334 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3335 int pipe = intel_crtc->pipe;
f0f59a00
VS
3336 i915_reg_t reg;
3337 u32 temp, tries;
8db9d77b 3338
1c8562f6 3339 /* FDI needs bits from pipe first */
0fc932b8 3340 assert_pipe_enabled(dev_priv, pipe);
0fc932b8 3341
e1a44743
AJ
3342 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3343 for train result */
5eddb70b
CW
3344 reg = FDI_RX_IMR(pipe);
3345 temp = I915_READ(reg);
e1a44743
AJ
3346 temp &= ~FDI_RX_SYMBOL_LOCK;
3347 temp &= ~FDI_RX_BIT_LOCK;
5eddb70b
CW
3348 I915_WRITE(reg, temp);
3349 I915_READ(reg);
e1a44743
AJ
3350 udelay(150);
3351
8db9d77b 3352 /* enable CPU FDI TX and PCH FDI RX */
5eddb70b
CW
3353 reg = FDI_TX_CTL(pipe);
3354 temp = I915_READ(reg);
627eb5a3 3355 temp &= ~FDI_DP_PORT_WIDTH_MASK;
6e3c9717 3356 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
8db9d77b
ZW
3357 temp &= ~FDI_LINK_TRAIN_NONE;
3358 temp |= FDI_LINK_TRAIN_PATTERN_1;
5eddb70b 3359 I915_WRITE(reg, temp | FDI_TX_ENABLE);
8db9d77b 3360
5eddb70b
CW
3361 reg = FDI_RX_CTL(pipe);
3362 temp = I915_READ(reg);
8db9d77b
ZW
3363 temp &= ~FDI_LINK_TRAIN_NONE;
3364 temp |= FDI_LINK_TRAIN_PATTERN_1;
5eddb70b
CW
3365 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3366
3367 POSTING_READ(reg);
8db9d77b
ZW
3368 udelay(150);
3369
5b2adf89 3370 /* Ironlake workaround, enable clock pointer after FDI enable*/
8f5718a6
DV
3371 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
3372 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
3373 FDI_RX_PHASE_SYNC_POINTER_EN);
5b2adf89 3374
5eddb70b 3375 reg = FDI_RX_IIR(pipe);
e1a44743 3376 for (tries = 0; tries < 5; tries++) {
5eddb70b 3377 temp = I915_READ(reg);
8db9d77b
ZW
3378 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3379
3380 if ((temp & FDI_RX_BIT_LOCK)) {
3381 DRM_DEBUG_KMS("FDI train 1 done.\n");
5eddb70b 3382 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
8db9d77b
ZW
3383 break;
3384 }
8db9d77b 3385 }
e1a44743 3386 if (tries == 5)
5eddb70b 3387 DRM_ERROR("FDI train 1 fail!\n");
8db9d77b
ZW
3388
3389 /* Train 2 */
5eddb70b
CW
3390 reg = FDI_TX_CTL(pipe);
3391 temp = I915_READ(reg);
8db9d77b
ZW
3392 temp &= ~FDI_LINK_TRAIN_NONE;
3393 temp |= FDI_LINK_TRAIN_PATTERN_2;
5eddb70b 3394 I915_WRITE(reg, temp);
8db9d77b 3395
5eddb70b
CW
3396 reg = FDI_RX_CTL(pipe);
3397 temp = I915_READ(reg);
8db9d77b
ZW
3398 temp &= ~FDI_LINK_TRAIN_NONE;
3399 temp |= FDI_LINK_TRAIN_PATTERN_2;
5eddb70b 3400 I915_WRITE(reg, temp);
8db9d77b 3401
5eddb70b
CW
3402 POSTING_READ(reg);
3403 udelay(150);
8db9d77b 3404
5eddb70b 3405 reg = FDI_RX_IIR(pipe);
e1a44743 3406 for (tries = 0; tries < 5; tries++) {
5eddb70b 3407 temp = I915_READ(reg);
8db9d77b
ZW
3408 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3409
3410 if (temp & FDI_RX_SYMBOL_LOCK) {
5eddb70b 3411 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
8db9d77b
ZW
3412 DRM_DEBUG_KMS("FDI train 2 done.\n");
3413 break;
3414 }
8db9d77b 3415 }
e1a44743 3416 if (tries == 5)
5eddb70b 3417 DRM_ERROR("FDI train 2 fail!\n");
8db9d77b
ZW
3418
3419 DRM_DEBUG_KMS("FDI train done\n");
5c5313c8 3420
8db9d77b
ZW
3421}
3422
0206e353 3423static const int snb_b_fdi_train_param[] = {
8db9d77b
ZW
3424 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
3425 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
3426 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
3427 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
3428};
3429
3430/* The FDI link training functions for SNB/Cougarpoint. */
3431static void gen6_fdi_link_train(struct drm_crtc *crtc)
3432{
3433 struct drm_device *dev = crtc->dev;
3434 struct drm_i915_private *dev_priv = dev->dev_private;
3435 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3436 int pipe = intel_crtc->pipe;
f0f59a00
VS
3437 i915_reg_t reg;
3438 u32 temp, i, retry;
8db9d77b 3439
e1a44743
AJ
3440 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3441 for train result */
5eddb70b
CW
3442 reg = FDI_RX_IMR(pipe);
3443 temp = I915_READ(reg);
e1a44743
AJ
3444 temp &= ~FDI_RX_SYMBOL_LOCK;
3445 temp &= ~FDI_RX_BIT_LOCK;
5eddb70b
CW
3446 I915_WRITE(reg, temp);
3447
3448 POSTING_READ(reg);
e1a44743
AJ
3449 udelay(150);
3450
8db9d77b 3451 /* enable CPU FDI TX and PCH FDI RX */
5eddb70b
CW
3452 reg = FDI_TX_CTL(pipe);
3453 temp = I915_READ(reg);
627eb5a3 3454 temp &= ~FDI_DP_PORT_WIDTH_MASK;
6e3c9717 3455 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
8db9d77b
ZW
3456 temp &= ~FDI_LINK_TRAIN_NONE;
3457 temp |= FDI_LINK_TRAIN_PATTERN_1;
3458 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3459 /* SNB-B */
3460 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
5eddb70b 3461 I915_WRITE(reg, temp | FDI_TX_ENABLE);
8db9d77b 3462
d74cf324
DV
3463 I915_WRITE(FDI_RX_MISC(pipe),
3464 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3465
5eddb70b
CW
3466 reg = FDI_RX_CTL(pipe);
3467 temp = I915_READ(reg);
8db9d77b
ZW
3468 if (HAS_PCH_CPT(dev)) {
3469 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3470 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3471 } else {
3472 temp &= ~FDI_LINK_TRAIN_NONE;
3473 temp |= FDI_LINK_TRAIN_PATTERN_1;
3474 }
5eddb70b
CW
3475 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3476
3477 POSTING_READ(reg);
8db9d77b
ZW
3478 udelay(150);
3479
0206e353 3480 for (i = 0; i < 4; i++) {
5eddb70b
CW
3481 reg = FDI_TX_CTL(pipe);
3482 temp = I915_READ(reg);
8db9d77b
ZW
3483 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3484 temp |= snb_b_fdi_train_param[i];
5eddb70b
CW
3485 I915_WRITE(reg, temp);
3486
3487 POSTING_READ(reg);
8db9d77b
ZW
3488 udelay(500);
3489
fa37d39e
SP
3490 for (retry = 0; retry < 5; retry++) {
3491 reg = FDI_RX_IIR(pipe);
3492 temp = I915_READ(reg);
3493 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3494 if (temp & FDI_RX_BIT_LOCK) {
3495 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3496 DRM_DEBUG_KMS("FDI train 1 done.\n");
3497 break;
3498 }
3499 udelay(50);
8db9d77b 3500 }
fa37d39e
SP
3501 if (retry < 5)
3502 break;
8db9d77b
ZW
3503 }
3504 if (i == 4)
5eddb70b 3505 DRM_ERROR("FDI train 1 fail!\n");
8db9d77b
ZW
3506
3507 /* Train 2 */
5eddb70b
CW
3508 reg = FDI_TX_CTL(pipe);
3509 temp = I915_READ(reg);
8db9d77b
ZW
3510 temp &= ~FDI_LINK_TRAIN_NONE;
3511 temp |= FDI_LINK_TRAIN_PATTERN_2;
3512 if (IS_GEN6(dev)) {
3513 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3514 /* SNB-B */
3515 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
3516 }
5eddb70b 3517 I915_WRITE(reg, temp);
8db9d77b 3518
5eddb70b
CW
3519 reg = FDI_RX_CTL(pipe);
3520 temp = I915_READ(reg);
8db9d77b
ZW
3521 if (HAS_PCH_CPT(dev)) {
3522 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3523 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
3524 } else {
3525 temp &= ~FDI_LINK_TRAIN_NONE;
3526 temp |= FDI_LINK_TRAIN_PATTERN_2;
3527 }
5eddb70b
CW
3528 I915_WRITE(reg, temp);
3529
3530 POSTING_READ(reg);
8db9d77b
ZW
3531 udelay(150);
3532
0206e353 3533 for (i = 0; i < 4; i++) {
5eddb70b
CW
3534 reg = FDI_TX_CTL(pipe);
3535 temp = I915_READ(reg);
8db9d77b
ZW
3536 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3537 temp |= snb_b_fdi_train_param[i];
5eddb70b
CW
3538 I915_WRITE(reg, temp);
3539
3540 POSTING_READ(reg);
8db9d77b
ZW
3541 udelay(500);
3542
fa37d39e
SP
3543 for (retry = 0; retry < 5; retry++) {
3544 reg = FDI_RX_IIR(pipe);
3545 temp = I915_READ(reg);
3546 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3547 if (temp & FDI_RX_SYMBOL_LOCK) {
3548 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3549 DRM_DEBUG_KMS("FDI train 2 done.\n");
3550 break;
3551 }
3552 udelay(50);
8db9d77b 3553 }
fa37d39e
SP
3554 if (retry < 5)
3555 break;
8db9d77b
ZW
3556 }
3557 if (i == 4)
5eddb70b 3558 DRM_ERROR("FDI train 2 fail!\n");
8db9d77b
ZW
3559
3560 DRM_DEBUG_KMS("FDI train done.\n");
3561}
3562
357555c0
JB
3563/* Manual link training for Ivy Bridge A0 parts */
3564static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
3565{
3566 struct drm_device *dev = crtc->dev;
3567 struct drm_i915_private *dev_priv = dev->dev_private;
3568 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3569 int pipe = intel_crtc->pipe;
f0f59a00
VS
3570 i915_reg_t reg;
3571 u32 temp, i, j;
357555c0
JB
3572
3573 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3574 for train result */
3575 reg = FDI_RX_IMR(pipe);
3576 temp = I915_READ(reg);
3577 temp &= ~FDI_RX_SYMBOL_LOCK;
3578 temp &= ~FDI_RX_BIT_LOCK;
3579 I915_WRITE(reg, temp);
3580
3581 POSTING_READ(reg);
3582 udelay(150);
3583
01a415fd
DV
3584 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
3585 I915_READ(FDI_RX_IIR(pipe)));
3586
139ccd3f
JB
3587 /* Try each vswing and preemphasis setting twice before moving on */
3588 for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
3589 /* disable first in case we need to retry */
3590 reg = FDI_TX_CTL(pipe);
3591 temp = I915_READ(reg);
3592 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
3593 temp &= ~FDI_TX_ENABLE;
3594 I915_WRITE(reg, temp);
357555c0 3595
139ccd3f
JB
3596 reg = FDI_RX_CTL(pipe);
3597 temp = I915_READ(reg);
3598 temp &= ~FDI_LINK_TRAIN_AUTO;
3599 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3600 temp &= ~FDI_RX_ENABLE;
3601 I915_WRITE(reg, temp);
357555c0 3602
139ccd3f 3603 /* enable CPU FDI TX and PCH FDI RX */
357555c0
JB
3604 reg = FDI_TX_CTL(pipe);
3605 temp = I915_READ(reg);
139ccd3f 3606 temp &= ~FDI_DP_PORT_WIDTH_MASK;
6e3c9717 3607 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
139ccd3f 3608 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
357555c0 3609 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
139ccd3f
JB
3610 temp |= snb_b_fdi_train_param[j/2];
3611 temp |= FDI_COMPOSITE_SYNC;
3612 I915_WRITE(reg, temp | FDI_TX_ENABLE);
357555c0 3613
139ccd3f
JB
3614 I915_WRITE(FDI_RX_MISC(pipe),
3615 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
357555c0 3616
139ccd3f 3617 reg = FDI_RX_CTL(pipe);
357555c0 3618 temp = I915_READ(reg);
139ccd3f
JB
3619 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3620 temp |= FDI_COMPOSITE_SYNC;
3621 I915_WRITE(reg, temp | FDI_RX_ENABLE);
357555c0 3622
139ccd3f
JB
3623 POSTING_READ(reg);
3624 udelay(1); /* should be 0.5us */
357555c0 3625
139ccd3f
JB
3626 for (i = 0; i < 4; i++) {
3627 reg = FDI_RX_IIR(pipe);
3628 temp = I915_READ(reg);
3629 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
357555c0 3630
139ccd3f
JB
3631 if (temp & FDI_RX_BIT_LOCK ||
3632 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
3633 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3634 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
3635 i);
3636 break;
3637 }
3638 udelay(1); /* should be 0.5us */
3639 }
3640 if (i == 4) {
3641 DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
3642 continue;
3643 }
357555c0 3644
139ccd3f 3645 /* Train 2 */
357555c0
JB
3646 reg = FDI_TX_CTL(pipe);
3647 temp = I915_READ(reg);
139ccd3f
JB
3648 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3649 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
3650 I915_WRITE(reg, temp);
3651
3652 reg = FDI_RX_CTL(pipe);
3653 temp = I915_READ(reg);
3654 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3655 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
357555c0
JB
3656 I915_WRITE(reg, temp);
3657
3658 POSTING_READ(reg);
139ccd3f 3659 udelay(2); /* should be 1.5us */
357555c0 3660
139ccd3f
JB
3661 for (i = 0; i < 4; i++) {
3662 reg = FDI_RX_IIR(pipe);
3663 temp = I915_READ(reg);
3664 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
357555c0 3665
139ccd3f
JB
3666 if (temp & FDI_RX_SYMBOL_LOCK ||
3667 (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
3668 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3669 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
3670 i);
3671 goto train_done;
3672 }
3673 udelay(2); /* should be 1.5us */
357555c0 3674 }
139ccd3f
JB
3675 if (i == 4)
3676 DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
357555c0 3677 }
357555c0 3678
139ccd3f 3679train_done:
357555c0
JB
3680 DRM_DEBUG_KMS("FDI train done.\n");
3681}
3682
88cefb6c 3683static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
2c07245f 3684{
88cefb6c 3685 struct drm_device *dev = intel_crtc->base.dev;
2c07245f 3686 struct drm_i915_private *dev_priv = dev->dev_private;
2c07245f 3687 int pipe = intel_crtc->pipe;
f0f59a00
VS
3688 i915_reg_t reg;
3689 u32 temp;
c64e311e 3690
c98e9dcf 3691 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
5eddb70b
CW
3692 reg = FDI_RX_CTL(pipe);
3693 temp = I915_READ(reg);
627eb5a3 3694 temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
6e3c9717 3695 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
dfd07d72 3696 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
5eddb70b
CW
3697 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
3698
3699 POSTING_READ(reg);
c98e9dcf
JB
3700 udelay(200);
3701
3702 /* Switch from Rawclk to PCDclk */
5eddb70b
CW
3703 temp = I915_READ(reg);
3704 I915_WRITE(reg, temp | FDI_PCDCLK);
3705
3706 POSTING_READ(reg);
c98e9dcf
JB
3707 udelay(200);
3708
20749730
PZ
3709 /* Enable CPU FDI TX PLL, always on for Ironlake */
3710 reg = FDI_TX_CTL(pipe);
3711 temp = I915_READ(reg);
3712 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
3713 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
5eddb70b 3714
20749730
PZ
3715 POSTING_READ(reg);
3716 udelay(100);
6be4a607 3717 }
0e23b99d
JB
3718}
3719
88cefb6c
DV
3720static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
3721{
3722 struct drm_device *dev = intel_crtc->base.dev;
3723 struct drm_i915_private *dev_priv = dev->dev_private;
3724 int pipe = intel_crtc->pipe;
f0f59a00
VS
3725 i915_reg_t reg;
3726 u32 temp;
88cefb6c
DV
3727
3728 /* Switch from PCDclk to Rawclk */
3729 reg = FDI_RX_CTL(pipe);
3730 temp = I915_READ(reg);
3731 I915_WRITE(reg, temp & ~FDI_PCDCLK);
3732
3733 /* Disable CPU FDI TX PLL */
3734 reg = FDI_TX_CTL(pipe);
3735 temp = I915_READ(reg);
3736 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
3737
3738 POSTING_READ(reg);
3739 udelay(100);
3740
3741 reg = FDI_RX_CTL(pipe);
3742 temp = I915_READ(reg);
3743 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
3744
3745 /* Wait for the clocks to turn off. */
3746 POSTING_READ(reg);
3747 udelay(100);
3748}
3749
0fc932b8
JB
3750static void ironlake_fdi_disable(struct drm_crtc *crtc)
3751{
3752 struct drm_device *dev = crtc->dev;
3753 struct drm_i915_private *dev_priv = dev->dev_private;
3754 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3755 int pipe = intel_crtc->pipe;
f0f59a00
VS
3756 i915_reg_t reg;
3757 u32 temp;
0fc932b8
JB
3758
3759 /* disable CPU FDI tx and PCH FDI rx */
3760 reg = FDI_TX_CTL(pipe);
3761 temp = I915_READ(reg);
3762 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
3763 POSTING_READ(reg);
3764
3765 reg = FDI_RX_CTL(pipe);
3766 temp = I915_READ(reg);
3767 temp &= ~(0x7 << 16);
dfd07d72 3768 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
0fc932b8
JB
3769 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
3770
3771 POSTING_READ(reg);
3772 udelay(100);
3773
3774 /* Ironlake workaround, disable clock pointer after downing FDI */
eba905b2 3775 if (HAS_PCH_IBX(dev))
6f06ce18 3776 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
0fc932b8
JB
3777
3778 /* still set train pattern 1 */
3779 reg = FDI_TX_CTL(pipe);
3780 temp = I915_READ(reg);
3781 temp &= ~FDI_LINK_TRAIN_NONE;
3782 temp |= FDI_LINK_TRAIN_PATTERN_1;
3783 I915_WRITE(reg, temp);
3784
3785 reg = FDI_RX_CTL(pipe);
3786 temp = I915_READ(reg);
3787 if (HAS_PCH_CPT(dev)) {
3788 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3789 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3790 } else {
3791 temp &= ~FDI_LINK_TRAIN_NONE;
3792 temp |= FDI_LINK_TRAIN_PATTERN_1;
3793 }
3794 /* BPC in FDI rx is consistent with that in PIPECONF */
3795 temp &= ~(0x07 << 16);
dfd07d72 3796 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
0fc932b8
JB
3797 I915_WRITE(reg, temp);
3798
3799 POSTING_READ(reg);
3800 udelay(100);
3801}
3802
5dce5b93
CW
3803bool intel_has_pending_fb_unpin(struct drm_device *dev)
3804{
3805 struct intel_crtc *crtc;
3806
3807 /* Note that we don't need to be called with mode_config.lock here
3808 * as our list of CRTC objects is static for the lifetime of the
3809 * device and so cannot disappear as we iterate. Similarly, we can
3810 * happily treat the predicates as racy, atomic checks as userspace
3811 * cannot claim and pin a new fb without at least acquring the
3812 * struct_mutex and so serialising with us.
3813 */
d3fcc808 3814 for_each_intel_crtc(dev, crtc) {
5dce5b93
CW
3815 if (atomic_read(&crtc->unpin_work_count) == 0)
3816 continue;
3817
5a21b665 3818 if (crtc->flip_work)
5dce5b93
CW
3819 intel_wait_for_vblank(dev, crtc->pipe);
3820
3821 return true;
3822 }
3823
3824 return false;
3825}
3826
5a21b665 3827static void page_flip_completed(struct intel_crtc *intel_crtc)
d6bbafa1
CW
3828{
3829 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
5a21b665
DV
3830 struct intel_flip_work *work = intel_crtc->flip_work;
3831
3832 intel_crtc->flip_work = NULL;
d6bbafa1
CW
3833
3834 if (work->event)
560ce1dc 3835 drm_crtc_send_vblank_event(&intel_crtc->base, work->event);
d6bbafa1
CW
3836
3837 drm_crtc_vblank_put(&intel_crtc->base);
3838
5a21b665 3839 wake_up_all(&dev_priv->pending_flip_queue);
143f73b3 3840 queue_work(dev_priv->wq, &work->unpin_work);
5a21b665
DV
3841
3842 trace_i915_flip_complete(intel_crtc->plane,
3843 work->pending_flip_obj);
d6bbafa1
CW
3844}
3845
5008e874 3846static int intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
e6c3a2a6 3847{
0f91128d 3848 struct drm_device *dev = crtc->dev;
5bb61643 3849 struct drm_i915_private *dev_priv = dev->dev_private;
5008e874 3850 long ret;
e6c3a2a6 3851
2c10d571 3852 WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
5008e874
ML
3853
3854 ret = wait_event_interruptible_timeout(
3855 dev_priv->pending_flip_queue,
3856 !intel_crtc_has_pending_flip(crtc),
3857 60*HZ);
3858
3859 if (ret < 0)
3860 return ret;
3861
5a21b665
DV
3862 if (ret == 0) {
3863 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3864 struct intel_flip_work *work;
3865
3866 spin_lock_irq(&dev->event_lock);
3867 work = intel_crtc->flip_work;
3868 if (work && !is_mmio_work(work)) {
3869 WARN_ONCE(1, "Removing stuck page flip\n");
3870 page_flip_completed(intel_crtc);
3871 }
3872 spin_unlock_irq(&dev->event_lock);
3873 }
5bb61643 3874
5008e874 3875 return 0;
e6c3a2a6
CW
3876}
3877
060f02d8
VS
3878static void lpt_disable_iclkip(struct drm_i915_private *dev_priv)
3879{
3880 u32 temp;
3881
3882 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
3883
3884 mutex_lock(&dev_priv->sb_lock);
3885
3886 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
3887 temp |= SBI_SSCCTL_DISABLE;
3888 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
3889
3890 mutex_unlock(&dev_priv->sb_lock);
3891}
3892
e615efe4
ED
3893/* Program iCLKIP clock to the desired frequency */
3894static void lpt_program_iclkip(struct drm_crtc *crtc)
3895{
64b46a06 3896 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
6e3c9717 3897 int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
e615efe4
ED
3898 u32 divsel, phaseinc, auxdiv, phasedir = 0;
3899 u32 temp;
3900
060f02d8 3901 lpt_disable_iclkip(dev_priv);
e615efe4 3902
64b46a06
VS
3903 /* The iCLK virtual clock root frequency is in MHz,
3904 * but the adjusted_mode->crtc_clock in in KHz. To get the
3905 * divisors, it is necessary to divide one by another, so we
3906 * convert the virtual clock precision to KHz here for higher
3907 * precision.
3908 */
3909 for (auxdiv = 0; auxdiv < 2; auxdiv++) {
e615efe4
ED
3910 u32 iclk_virtual_root_freq = 172800 * 1000;
3911 u32 iclk_pi_range = 64;
64b46a06 3912 u32 desired_divisor;
e615efe4 3913
64b46a06
VS
3914 desired_divisor = DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
3915 clock << auxdiv);
3916 divsel = (desired_divisor / iclk_pi_range) - 2;
3917 phaseinc = desired_divisor % iclk_pi_range;
e615efe4 3918
64b46a06
VS
3919 /*
3920 * Near 20MHz is a corner case which is
3921 * out of range for the 7-bit divisor
3922 */
3923 if (divsel <= 0x7f)
3924 break;
e615efe4
ED
3925 }
3926
3927 /* This should not happen with any sane values */
3928 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
3929 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
3930 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
3931 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
3932
3933 DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
12d7ceed 3934 clock,
e615efe4
ED
3935 auxdiv,
3936 divsel,
3937 phasedir,
3938 phaseinc);
3939
060f02d8
VS
3940 mutex_lock(&dev_priv->sb_lock);
3941
e615efe4 3942 /* Program SSCDIVINTPHASE6 */
988d6ee8 3943 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
e615efe4
ED
3944 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
3945 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
3946 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
3947 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
3948 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
3949 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
988d6ee8 3950 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
e615efe4
ED
3951
3952 /* Program SSCAUXDIV */
988d6ee8 3953 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
e615efe4
ED
3954 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
3955 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
988d6ee8 3956 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
e615efe4
ED
3957
3958 /* Enable modulator and associated divider */
988d6ee8 3959 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
e615efe4 3960 temp &= ~SBI_SSCCTL_DISABLE;
988d6ee8 3961 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
e615efe4 3962
060f02d8
VS
3963 mutex_unlock(&dev_priv->sb_lock);
3964
e615efe4
ED
3965 /* Wait for initialization time */
3966 udelay(24);
3967
3968 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
3969}
3970
8802e5b6
VS
3971int lpt_get_iclkip(struct drm_i915_private *dev_priv)
3972{
3973 u32 divsel, phaseinc, auxdiv;
3974 u32 iclk_virtual_root_freq = 172800 * 1000;
3975 u32 iclk_pi_range = 64;
3976 u32 desired_divisor;
3977 u32 temp;
3978
3979 if ((I915_READ(PIXCLK_GATE) & PIXCLK_GATE_UNGATE) == 0)
3980 return 0;
3981
3982 mutex_lock(&dev_priv->sb_lock);
3983
3984 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
3985 if (temp & SBI_SSCCTL_DISABLE) {
3986 mutex_unlock(&dev_priv->sb_lock);
3987 return 0;
3988 }
3989
3990 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
3991 divsel = (temp & SBI_SSCDIVINTPHASE_DIVSEL_MASK) >>
3992 SBI_SSCDIVINTPHASE_DIVSEL_SHIFT;
3993 phaseinc = (temp & SBI_SSCDIVINTPHASE_INCVAL_MASK) >>
3994 SBI_SSCDIVINTPHASE_INCVAL_SHIFT;
3995
3996 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
3997 auxdiv = (temp & SBI_SSCAUXDIV_FINALDIV2SEL_MASK) >>
3998 SBI_SSCAUXDIV_FINALDIV2SEL_SHIFT;
3999
4000 mutex_unlock(&dev_priv->sb_lock);
4001
4002 desired_divisor = (divsel + 2) * iclk_pi_range + phaseinc;
4003
4004 return DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
4005 desired_divisor << auxdiv);
4006}
4007
275f01b2
DV
4008static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
4009 enum pipe pch_transcoder)
4010{
4011 struct drm_device *dev = crtc->base.dev;
4012 struct drm_i915_private *dev_priv = dev->dev_private;
6e3c9717 4013 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
275f01b2
DV
4014
4015 I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
4016 I915_READ(HTOTAL(cpu_transcoder)));
4017 I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
4018 I915_READ(HBLANK(cpu_transcoder)));
4019 I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
4020 I915_READ(HSYNC(cpu_transcoder)));
4021
4022 I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
4023 I915_READ(VTOTAL(cpu_transcoder)));
4024 I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
4025 I915_READ(VBLANK(cpu_transcoder)));
4026 I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
4027 I915_READ(VSYNC(cpu_transcoder)));
4028 I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
4029 I915_READ(VSYNCSHIFT(cpu_transcoder)));
4030}
4031
003632d9 4032static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
1fbc0d78
DV
4033{
4034 struct drm_i915_private *dev_priv = dev->dev_private;
4035 uint32_t temp;
4036
4037 temp = I915_READ(SOUTH_CHICKEN1);
003632d9 4038 if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
1fbc0d78
DV
4039 return;
4040
4041 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
4042 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
4043
003632d9
ACO
4044 temp &= ~FDI_BC_BIFURCATION_SELECT;
4045 if (enable)
4046 temp |= FDI_BC_BIFURCATION_SELECT;
4047
4048 DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
1fbc0d78
DV
4049 I915_WRITE(SOUTH_CHICKEN1, temp);
4050 POSTING_READ(SOUTH_CHICKEN1);
4051}
4052
4053static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
4054{
4055 struct drm_device *dev = intel_crtc->base.dev;
1fbc0d78
DV
4056
4057 switch (intel_crtc->pipe) {
4058 case PIPE_A:
4059 break;
4060 case PIPE_B:
6e3c9717 4061 if (intel_crtc->config->fdi_lanes > 2)
003632d9 4062 cpt_set_fdi_bc_bifurcation(dev, false);
1fbc0d78 4063 else
003632d9 4064 cpt_set_fdi_bc_bifurcation(dev, true);
1fbc0d78
DV
4065
4066 break;
4067 case PIPE_C:
003632d9 4068 cpt_set_fdi_bc_bifurcation(dev, true);
1fbc0d78
DV
4069
4070 break;
4071 default:
4072 BUG();
4073 }
4074}
4075
c48b5305
VS
4076/* Return which DP Port should be selected for Transcoder DP control */
4077static enum port
4078intel_trans_dp_port_sel(struct drm_crtc *crtc)
4079{
4080 struct drm_device *dev = crtc->dev;
4081 struct intel_encoder *encoder;
4082
4083 for_each_encoder_on_crtc(dev, crtc, encoder) {
4084 if (encoder->type == INTEL_OUTPUT_DISPLAYPORT ||
4085 encoder->type == INTEL_OUTPUT_EDP)
4086 return enc_to_dig_port(&encoder->base)->port;
4087 }
4088
4089 return -1;
4090}
4091
f67a559d
JB
4092/*
4093 * Enable PCH resources required for PCH ports:
4094 * - PCH PLLs
4095 * - FDI training & RX/TX
4096 * - update transcoder timings
4097 * - DP transcoding bits
4098 * - transcoder
4099 */
4100static void ironlake_pch_enable(struct drm_crtc *crtc)
0e23b99d
JB
4101{
4102 struct drm_device *dev = crtc->dev;
4103 struct drm_i915_private *dev_priv = dev->dev_private;
4104 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4105 int pipe = intel_crtc->pipe;
f0f59a00 4106 u32 temp;
2c07245f 4107
ab9412ba 4108 assert_pch_transcoder_disabled(dev_priv, pipe);
e7e164db 4109
1fbc0d78
DV
4110 if (IS_IVYBRIDGE(dev))
4111 ivybridge_update_fdi_bc_bifurcation(intel_crtc);
4112
cd986abb
DV
4113 /* Write the TU size bits before fdi link training, so that error
4114 * detection works. */
4115 I915_WRITE(FDI_RX_TUSIZE1(pipe),
4116 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
4117
c98e9dcf 4118 /* For PCH output, training FDI link */
674cf967 4119 dev_priv->display.fdi_link_train(crtc);
2c07245f 4120
3ad8a208
DV
4121 /* We need to program the right clock selection before writing the pixel
4122 * mutliplier into the DPLL. */
303b81e0 4123 if (HAS_PCH_CPT(dev)) {
ee7b9f93 4124 u32 sel;
4b645f14 4125
c98e9dcf 4126 temp = I915_READ(PCH_DPLL_SEL);
11887397
DV
4127 temp |= TRANS_DPLL_ENABLE(pipe);
4128 sel = TRANS_DPLLB_SEL(pipe);
8106ddbd
ACO
4129 if (intel_crtc->config->shared_dpll ==
4130 intel_get_shared_dpll_by_id(dev_priv, DPLL_ID_PCH_PLL_B))
ee7b9f93
JB
4131 temp |= sel;
4132 else
4133 temp &= ~sel;
c98e9dcf 4134 I915_WRITE(PCH_DPLL_SEL, temp);
c98e9dcf 4135 }
5eddb70b 4136
3ad8a208
DV
4137 /* XXX: pch pll's can be enabled any time before we enable the PCH
4138 * transcoder, and we actually should do this to not upset any PCH
4139 * transcoder that already use the clock when we share it.
4140 *
4141 * Note that enable_shared_dpll tries to do the right thing, but
4142 * get_shared_dpll unconditionally resets the pll - we need that to have
4143 * the right LVDS enable sequence. */
85b3894f 4144 intel_enable_shared_dpll(intel_crtc);
3ad8a208 4145
d9b6cb56
JB
4146 /* set transcoder timing, panel must allow it */
4147 assert_panel_unlocked(dev_priv, pipe);
275f01b2 4148 ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
8db9d77b 4149
303b81e0 4150 intel_fdi_normal_train(crtc);
5e84e1a4 4151
c98e9dcf 4152 /* For PCH DP, enable TRANS_DP_CTL */
6e3c9717 4153 if (HAS_PCH_CPT(dev) && intel_crtc->config->has_dp_encoder) {
9c4edaee
VS
4154 const struct drm_display_mode *adjusted_mode =
4155 &intel_crtc->config->base.adjusted_mode;
dfd07d72 4156 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
f0f59a00 4157 i915_reg_t reg = TRANS_DP_CTL(pipe);
5eddb70b
CW
4158 temp = I915_READ(reg);
4159 temp &= ~(TRANS_DP_PORT_SEL_MASK |
220cad3c
EA
4160 TRANS_DP_SYNC_MASK |
4161 TRANS_DP_BPC_MASK);
e3ef4479 4162 temp |= TRANS_DP_OUTPUT_ENABLE;
9325c9f0 4163 temp |= bpc << 9; /* same format but at 11:9 */
c98e9dcf 4164
9c4edaee 4165 if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
5eddb70b 4166 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
9c4edaee 4167 if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
5eddb70b 4168 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
c98e9dcf
JB
4169
4170 switch (intel_trans_dp_port_sel(crtc)) {
c48b5305 4171 case PORT_B:
5eddb70b 4172 temp |= TRANS_DP_PORT_SEL_B;
c98e9dcf 4173 break;
c48b5305 4174 case PORT_C:
5eddb70b 4175 temp |= TRANS_DP_PORT_SEL_C;
c98e9dcf 4176 break;
c48b5305 4177 case PORT_D:
5eddb70b 4178 temp |= TRANS_DP_PORT_SEL_D;
c98e9dcf
JB
4179 break;
4180 default:
e95d41e1 4181 BUG();
32f9d658 4182 }
2c07245f 4183
5eddb70b 4184 I915_WRITE(reg, temp);
6be4a607 4185 }
b52eb4dc 4186
b8a4f404 4187 ironlake_enable_pch_transcoder(dev_priv, pipe);
f67a559d
JB
4188}
4189
1507e5bd
PZ
4190static void lpt_pch_enable(struct drm_crtc *crtc)
4191{
4192 struct drm_device *dev = crtc->dev;
4193 struct drm_i915_private *dev_priv = dev->dev_private;
4194 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6e3c9717 4195 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
1507e5bd 4196
ab9412ba 4197 assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
1507e5bd 4198
8c52b5e8 4199 lpt_program_iclkip(crtc);
1507e5bd 4200
0540e488 4201 /* Set transcoder timing. */
275f01b2 4202 ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
1507e5bd 4203
937bb610 4204 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
f67a559d
JB
4205}
4206
a1520318 4207static void cpt_verify_modeset(struct drm_device *dev, int pipe)
d4270e57
JB
4208{
4209 struct drm_i915_private *dev_priv = dev->dev_private;
f0f59a00 4210 i915_reg_t dslreg = PIPEDSL(pipe);
d4270e57
JB
4211 u32 temp;
4212
4213 temp = I915_READ(dslreg);
4214 udelay(500);
4215 if (wait_for(I915_READ(dslreg) != temp, 5)) {
d4270e57 4216 if (wait_for(I915_READ(dslreg) != temp, 5))
84f44ce7 4217 DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
d4270e57
JB
4218 }
4219}
4220
86adf9d7
ML
4221static int
4222skl_update_scaler(struct intel_crtc_state *crtc_state, bool force_detach,
4223 unsigned scaler_user, int *scaler_id, unsigned int rotation,
4224 int src_w, int src_h, int dst_w, int dst_h)
a1b2278e 4225{
86adf9d7
ML
4226 struct intel_crtc_scaler_state *scaler_state =
4227 &crtc_state->scaler_state;
4228 struct intel_crtc *intel_crtc =
4229 to_intel_crtc(crtc_state->base.crtc);
a1b2278e 4230 int need_scaling;
6156a456
CK
4231
4232 need_scaling = intel_rotation_90_or_270(rotation) ?
4233 (src_h != dst_w || src_w != dst_h):
4234 (src_w != dst_w || src_h != dst_h);
a1b2278e
CK
4235
4236 /*
4237 * if plane is being disabled or scaler is no more required or force detach
4238 * - free scaler binded to this plane/crtc
4239 * - in order to do this, update crtc->scaler_usage
4240 *
4241 * Here scaler state in crtc_state is set free so that
4242 * scaler can be assigned to other user. Actual register
4243 * update to free the scaler is done in plane/panel-fit programming.
4244 * For this purpose crtc/plane_state->scaler_id isn't reset here.
4245 */
86adf9d7 4246 if (force_detach || !need_scaling) {
a1b2278e 4247 if (*scaler_id >= 0) {
86adf9d7 4248 scaler_state->scaler_users &= ~(1 << scaler_user);
a1b2278e
CK
4249 scaler_state->scalers[*scaler_id].in_use = 0;
4250
86adf9d7
ML
4251 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4252 "Staged freeing scaler id %d scaler_users = 0x%x\n",
4253 intel_crtc->pipe, scaler_user, *scaler_id,
a1b2278e
CK
4254 scaler_state->scaler_users);
4255 *scaler_id = -1;
4256 }
4257 return 0;
4258 }
4259
4260 /* range checks */
4261 if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
4262 dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
4263
4264 src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
4265 dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
86adf9d7 4266 DRM_DEBUG_KMS("scaler_user index %u.%u: src %ux%u dst %ux%u "
a1b2278e 4267 "size is out of scaler range\n",
86adf9d7 4268 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h);
a1b2278e
CK
4269 return -EINVAL;
4270 }
4271
86adf9d7
ML
4272 /* mark this plane as a scaler user in crtc_state */
4273 scaler_state->scaler_users |= (1 << scaler_user);
4274 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4275 "staged scaling request for %ux%u->%ux%u scaler_users = 0x%x\n",
4276 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h,
4277 scaler_state->scaler_users);
4278
4279 return 0;
4280}
4281
4282/**
4283 * skl_update_scaler_crtc - Stages update to scaler state for a given crtc.
4284 *
4285 * @state: crtc's scaler state
86adf9d7
ML
4286 *
4287 * Return
4288 * 0 - scaler_usage updated successfully
4289 * error - requested scaling cannot be supported or other error condition
4290 */
e435d6e5 4291int skl_update_scaler_crtc(struct intel_crtc_state *state)
86adf9d7
ML
4292{
4293 struct intel_crtc *intel_crtc = to_intel_crtc(state->base.crtc);
7c5f93b0 4294 const struct drm_display_mode *adjusted_mode = &state->base.adjusted_mode;
86adf9d7 4295
78108b7c
VS
4296 DRM_DEBUG_KMS("Updating scaler for [CRTC:%d:%s] scaler_user index %u.%u\n",
4297 intel_crtc->base.base.id, intel_crtc->base.name,
4298 intel_crtc->pipe, SKL_CRTC_INDEX);
86adf9d7 4299
e435d6e5 4300 return skl_update_scaler(state, !state->base.active, SKL_CRTC_INDEX,
fa5a7970 4301 &state->scaler_state.scaler_id, BIT(DRM_ROTATE_0),
86adf9d7 4302 state->pipe_src_w, state->pipe_src_h,
aad941d5 4303 adjusted_mode->crtc_hdisplay, adjusted_mode->crtc_vdisplay);
86adf9d7
ML
4304}
4305
4306/**
4307 * skl_update_scaler_plane - Stages update to scaler state for a given plane.
4308 *
4309 * @state: crtc's scaler state
86adf9d7
ML
4310 * @plane_state: atomic plane state to update
4311 *
4312 * Return
4313 * 0 - scaler_usage updated successfully
4314 * error - requested scaling cannot be supported or other error condition
4315 */
da20eabd
ML
4316static int skl_update_scaler_plane(struct intel_crtc_state *crtc_state,
4317 struct intel_plane_state *plane_state)
86adf9d7
ML
4318{
4319
4320 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
da20eabd
ML
4321 struct intel_plane *intel_plane =
4322 to_intel_plane(plane_state->base.plane);
86adf9d7
ML
4323 struct drm_framebuffer *fb = plane_state->base.fb;
4324 int ret;
4325
4326 bool force_detach = !fb || !plane_state->visible;
4327
72660ce0
VS
4328 DRM_DEBUG_KMS("Updating scaler for [PLANE:%d:%s] scaler_user index %u.%u\n",
4329 intel_plane->base.base.id, intel_plane->base.name,
4330 intel_crtc->pipe, drm_plane_index(&intel_plane->base));
86adf9d7
ML
4331
4332 ret = skl_update_scaler(crtc_state, force_detach,
4333 drm_plane_index(&intel_plane->base),
4334 &plane_state->scaler_id,
4335 plane_state->base.rotation,
4336 drm_rect_width(&plane_state->src) >> 16,
4337 drm_rect_height(&plane_state->src) >> 16,
4338 drm_rect_width(&plane_state->dst),
4339 drm_rect_height(&plane_state->dst));
4340
4341 if (ret || plane_state->scaler_id < 0)
4342 return ret;
4343
a1b2278e 4344 /* check colorkey */
818ed961 4345 if (plane_state->ckey.flags != I915_SET_COLORKEY_NONE) {
72660ce0
VS
4346 DRM_DEBUG_KMS("[PLANE:%d:%s] scaling with color key not allowed",
4347 intel_plane->base.base.id,
4348 intel_plane->base.name);
a1b2278e
CK
4349 return -EINVAL;
4350 }
4351
4352 /* Check src format */
86adf9d7
ML
4353 switch (fb->pixel_format) {
4354 case DRM_FORMAT_RGB565:
4355 case DRM_FORMAT_XBGR8888:
4356 case DRM_FORMAT_XRGB8888:
4357 case DRM_FORMAT_ABGR8888:
4358 case DRM_FORMAT_ARGB8888:
4359 case DRM_FORMAT_XRGB2101010:
4360 case DRM_FORMAT_XBGR2101010:
4361 case DRM_FORMAT_YUYV:
4362 case DRM_FORMAT_YVYU:
4363 case DRM_FORMAT_UYVY:
4364 case DRM_FORMAT_VYUY:
4365 break;
4366 default:
72660ce0
VS
4367 DRM_DEBUG_KMS("[PLANE:%d:%s] FB:%d unsupported scaling format 0x%x\n",
4368 intel_plane->base.base.id, intel_plane->base.name,
4369 fb->base.id, fb->pixel_format);
86adf9d7 4370 return -EINVAL;
a1b2278e
CK
4371 }
4372
a1b2278e
CK
4373 return 0;
4374}
4375
e435d6e5
ML
4376static void skylake_scaler_disable(struct intel_crtc *crtc)
4377{
4378 int i;
4379
4380 for (i = 0; i < crtc->num_scalers; i++)
4381 skl_detach_scaler(crtc, i);
4382}
4383
4384static void skylake_pfit_enable(struct intel_crtc *crtc)
bd2e244f
JB
4385{
4386 struct drm_device *dev = crtc->base.dev;
4387 struct drm_i915_private *dev_priv = dev->dev_private;
4388 int pipe = crtc->pipe;
a1b2278e
CK
4389 struct intel_crtc_scaler_state *scaler_state =
4390 &crtc->config->scaler_state;
4391
4392 DRM_DEBUG_KMS("for crtc_state = %p\n", crtc->config);
4393
6e3c9717 4394 if (crtc->config->pch_pfit.enabled) {
a1b2278e
CK
4395 int id;
4396
4397 if (WARN_ON(crtc->config->scaler_state.scaler_id < 0)) {
4398 DRM_ERROR("Requesting pfit without getting a scaler first\n");
4399 return;
4400 }
4401
4402 id = scaler_state->scaler_id;
4403 I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
4404 PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
4405 I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
4406 I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
4407
4408 DRM_DEBUG_KMS("for crtc_state = %p scaler_id = %d\n", crtc->config, id);
bd2e244f
JB
4409 }
4410}
4411
b074cec8
JB
4412static void ironlake_pfit_enable(struct intel_crtc *crtc)
4413{
4414 struct drm_device *dev = crtc->base.dev;
4415 struct drm_i915_private *dev_priv = dev->dev_private;
4416 int pipe = crtc->pipe;
4417
6e3c9717 4418 if (crtc->config->pch_pfit.enabled) {
b074cec8
JB
4419 /* Force use of hard-coded filter coefficients
4420 * as some pre-programmed values are broken,
4421 * e.g. x201.
4422 */
4423 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
4424 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
4425 PF_PIPE_SEL_IVB(pipe));
4426 else
4427 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
6e3c9717
ACO
4428 I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
4429 I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
d4270e57
JB
4430 }
4431}
4432
20bc8673 4433void hsw_enable_ips(struct intel_crtc *crtc)
d77e4531 4434{
cea165c3
VS
4435 struct drm_device *dev = crtc->base.dev;
4436 struct drm_i915_private *dev_priv = dev->dev_private;
d77e4531 4437
6e3c9717 4438 if (!crtc->config->ips_enabled)
d77e4531
PZ
4439 return;
4440
307e4498
ML
4441 /*
4442 * We can only enable IPS after we enable a plane and wait for a vblank
4443 * This function is called from post_plane_update, which is run after
4444 * a vblank wait.
4445 */
cea165c3 4446
d77e4531 4447 assert_plane_enabled(dev_priv, crtc->plane);
cea165c3 4448 if (IS_BROADWELL(dev)) {
2a114cc1
BW
4449 mutex_lock(&dev_priv->rps.hw_lock);
4450 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
4451 mutex_unlock(&dev_priv->rps.hw_lock);
4452 /* Quoting Art Runyan: "its not safe to expect any particular
4453 * value in IPS_CTL bit 31 after enabling IPS through the
e59150dc
JB
4454 * mailbox." Moreover, the mailbox may return a bogus state,
4455 * so we need to just enable it and continue on.
2a114cc1
BW
4456 */
4457 } else {
4458 I915_WRITE(IPS_CTL, IPS_ENABLE);
4459 /* The bit only becomes 1 in the next vblank, so this wait here
4460 * is essentially intel_wait_for_vblank. If we don't have this
4461 * and don't wait for vblanks until the end of crtc_enable, then
4462 * the HW state readout code will complain that the expected
4463 * IPS_CTL value is not the one we read. */
4464 if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
4465 DRM_ERROR("Timed out waiting for IPS enable\n");
4466 }
d77e4531
PZ
4467}
4468
20bc8673 4469void hsw_disable_ips(struct intel_crtc *crtc)
d77e4531
PZ
4470{
4471 struct drm_device *dev = crtc->base.dev;
4472 struct drm_i915_private *dev_priv = dev->dev_private;
4473
6e3c9717 4474 if (!crtc->config->ips_enabled)
d77e4531
PZ
4475 return;
4476
4477 assert_plane_enabled(dev_priv, crtc->plane);
23d0b130 4478 if (IS_BROADWELL(dev)) {
2a114cc1
BW
4479 mutex_lock(&dev_priv->rps.hw_lock);
4480 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
4481 mutex_unlock(&dev_priv->rps.hw_lock);
23d0b130
BW
4482 /* wait for pcode to finish disabling IPS, which may take up to 42ms */
4483 if (wait_for((I915_READ(IPS_CTL) & IPS_ENABLE) == 0, 42))
4484 DRM_ERROR("Timed out waiting for IPS disable\n");
e59150dc 4485 } else {
2a114cc1 4486 I915_WRITE(IPS_CTL, 0);
e59150dc
JB
4487 POSTING_READ(IPS_CTL);
4488 }
d77e4531
PZ
4489
4490 /* We need to wait for a vblank before we can disable the plane. */
4491 intel_wait_for_vblank(dev, crtc->pipe);
4492}
4493
7cac945f 4494static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
d3eedb1a 4495{
7cac945f 4496 if (intel_crtc->overlay) {
d3eedb1a
VS
4497 struct drm_device *dev = intel_crtc->base.dev;
4498 struct drm_i915_private *dev_priv = dev->dev_private;
4499
4500 mutex_lock(&dev->struct_mutex);
4501 dev_priv->mm.interruptible = false;
4502 (void) intel_overlay_switch_off(intel_crtc->overlay);
4503 dev_priv->mm.interruptible = true;
4504 mutex_unlock(&dev->struct_mutex);
4505 }
4506
4507 /* Let userspace switch the overlay on again. In most cases userspace
4508 * has to recompute where to put it anyway.
4509 */
4510}
4511
87d4300a
ML
4512/**
4513 * intel_post_enable_primary - Perform operations after enabling primary plane
4514 * @crtc: the CRTC whose primary plane was just enabled
4515 *
4516 * Performs potentially sleeping operations that must be done after the primary
4517 * plane is enabled, such as updating FBC and IPS. Note that this may be
4518 * called due to an explicit primary plane update, or due to an implicit
4519 * re-enable that is caused when a sprite plane is updated to no longer
4520 * completely hide the primary plane.
4521 */
4522static void
4523intel_post_enable_primary(struct drm_crtc *crtc)
a5c4d7bc
VS
4524{
4525 struct drm_device *dev = crtc->dev;
87d4300a 4526 struct drm_i915_private *dev_priv = dev->dev_private;
a5c4d7bc
VS
4527 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4528 int pipe = intel_crtc->pipe;
a5c4d7bc 4529
87d4300a
ML
4530 /*
4531 * FIXME IPS should be fine as long as one plane is
4532 * enabled, but in practice it seems to have problems
4533 * when going from primary only to sprite only and vice
4534 * versa.
4535 */
a5c4d7bc
VS
4536 hsw_enable_ips(intel_crtc);
4537
f99d7069 4538 /*
87d4300a
ML
4539 * Gen2 reports pipe underruns whenever all planes are disabled.
4540 * So don't enable underrun reporting before at least some planes
4541 * are enabled.
4542 * FIXME: Need to fix the logic to work when we turn off all planes
4543 * but leave the pipe running.
f99d7069 4544 */
87d4300a
ML
4545 if (IS_GEN2(dev))
4546 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4547
aca7b684
VS
4548 /* Underruns don't always raise interrupts, so check manually. */
4549 intel_check_cpu_fifo_underruns(dev_priv);
4550 intel_check_pch_fifo_underruns(dev_priv);
a5c4d7bc
VS
4551}
4552
2622a081 4553/* FIXME move all this to pre_plane_update() with proper state tracking */
87d4300a
ML
4554static void
4555intel_pre_disable_primary(struct drm_crtc *crtc)
a5c4d7bc
VS
4556{
4557 struct drm_device *dev = crtc->dev;
4558 struct drm_i915_private *dev_priv = dev->dev_private;
4559 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4560 int pipe = intel_crtc->pipe;
a5c4d7bc 4561
87d4300a
ML
4562 /*
4563 * Gen2 reports pipe underruns whenever all planes are disabled.
4564 * So diasble underrun reporting before all the planes get disabled.
4565 * FIXME: Need to fix the logic to work when we turn off all planes
4566 * but leave the pipe running.
4567 */
4568 if (IS_GEN2(dev))
4569 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
a5c4d7bc 4570
2622a081
VS
4571 /*
4572 * FIXME IPS should be fine as long as one plane is
4573 * enabled, but in practice it seems to have problems
4574 * when going from primary only to sprite only and vice
4575 * versa.
4576 */
4577 hsw_disable_ips(intel_crtc);
4578}
4579
4580/* FIXME get rid of this and use pre_plane_update */
4581static void
4582intel_pre_disable_primary_noatomic(struct drm_crtc *crtc)
4583{
4584 struct drm_device *dev = crtc->dev;
4585 struct drm_i915_private *dev_priv = dev->dev_private;
4586 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4587 int pipe = intel_crtc->pipe;
4588
4589 intel_pre_disable_primary(crtc);
4590
87d4300a
ML
4591 /*
4592 * Vblank time updates from the shadow to live plane control register
4593 * are blocked if the memory self-refresh mode is active at that
4594 * moment. So to make sure the plane gets truly disabled, disable
4595 * first the self-refresh mode. The self-refresh enable bit in turn
4596 * will be checked/applied by the HW only at the next frame start
4597 * event which is after the vblank start event, so we need to have a
4598 * wait-for-vblank between disabling the plane and the pipe.
4599 */
262cd2e1 4600 if (HAS_GMCH_DISPLAY(dev)) {
87d4300a 4601 intel_set_memory_cxsr(dev_priv, false);
262cd2e1
VS
4602 dev_priv->wm.vlv.cxsr = false;
4603 intel_wait_for_vblank(dev, pipe);
4604 }
87d4300a
ML
4605}
4606
5a21b665
DV
4607static void intel_post_plane_update(struct intel_crtc_state *old_crtc_state)
4608{
4609 struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
4610 struct drm_atomic_state *old_state = old_crtc_state->base.state;
4611 struct intel_crtc_state *pipe_config =
4612 to_intel_crtc_state(crtc->base.state);
4613 struct drm_device *dev = crtc->base.dev;
4614 struct drm_plane *primary = crtc->base.primary;
4615 struct drm_plane_state *old_pri_state =
4616 drm_atomic_get_existing_plane_state(old_state, primary);
4617
4618 intel_frontbuffer_flip(dev, pipe_config->fb_bits);
4619
4620 crtc->wm.cxsr_allowed = true;
4621
4622 if (pipe_config->update_wm_post && pipe_config->base.active)
4623 intel_update_watermarks(&crtc->base);
4624
4625 if (old_pri_state) {
4626 struct intel_plane_state *primary_state =
4627 to_intel_plane_state(primary->state);
4628 struct intel_plane_state *old_primary_state =
4629 to_intel_plane_state(old_pri_state);
4630
4631 intel_fbc_post_update(crtc);
4632
4633 if (primary_state->visible &&
4634 (needs_modeset(&pipe_config->base) ||
4635 !old_primary_state->visible))
4636 intel_post_enable_primary(&crtc->base);
4637 }
4638}
4639
5c74cd73 4640static void intel_pre_plane_update(struct intel_crtc_state *old_crtc_state)
ac21b225 4641{
5c74cd73 4642 struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
ac21b225 4643 struct drm_device *dev = crtc->base.dev;
eddfcbcd 4644 struct drm_i915_private *dev_priv = dev->dev_private;
ab1d3a0e
ML
4645 struct intel_crtc_state *pipe_config =
4646 to_intel_crtc_state(crtc->base.state);
5c74cd73
ML
4647 struct drm_atomic_state *old_state = old_crtc_state->base.state;
4648 struct drm_plane *primary = crtc->base.primary;
4649 struct drm_plane_state *old_pri_state =
4650 drm_atomic_get_existing_plane_state(old_state, primary);
4651 bool modeset = needs_modeset(&pipe_config->base);
ac21b225 4652
5c74cd73
ML
4653 if (old_pri_state) {
4654 struct intel_plane_state *primary_state =
4655 to_intel_plane_state(primary->state);
4656 struct intel_plane_state *old_primary_state =
4657 to_intel_plane_state(old_pri_state);
4658
faf68d92 4659 intel_fbc_pre_update(crtc, pipe_config, primary_state);
31ae71fc 4660
5c74cd73
ML
4661 if (old_primary_state->visible &&
4662 (modeset || !primary_state->visible))
4663 intel_pre_disable_primary(&crtc->base);
4664 }
852eb00d 4665
a4015f9a 4666 if (pipe_config->disable_cxsr && HAS_GMCH_DISPLAY(dev)) {
852eb00d 4667 crtc->wm.cxsr_allowed = false;
2dfd178d 4668
2622a081
VS
4669 /*
4670 * Vblank time updates from the shadow to live plane control register
4671 * are blocked if the memory self-refresh mode is active at that
4672 * moment. So to make sure the plane gets truly disabled, disable
4673 * first the self-refresh mode. The self-refresh enable bit in turn
4674 * will be checked/applied by the HW only at the next frame start
4675 * event which is after the vblank start event, so we need to have a
4676 * wait-for-vblank between disabling the plane and the pipe.
4677 */
4678 if (old_crtc_state->base.active) {
2dfd178d 4679 intel_set_memory_cxsr(dev_priv, false);
2622a081
VS
4680 dev_priv->wm.vlv.cxsr = false;
4681 intel_wait_for_vblank(dev, crtc->pipe);
4682 }
852eb00d 4683 }
92826fcd 4684
ed4a6a7c
MR
4685 /*
4686 * IVB workaround: must disable low power watermarks for at least
4687 * one frame before enabling scaling. LP watermarks can be re-enabled
4688 * when scaling is disabled.
4689 *
4690 * WaCxSRDisabledForSpriteScaling:ivb
4691 */
4692 if (pipe_config->disable_lp_wm) {
4693 ilk_disable_lp_wm(dev);
4694 intel_wait_for_vblank(dev, crtc->pipe);
4695 }
4696
4697 /*
4698 * If we're doing a modeset, we're done. No need to do any pre-vblank
4699 * watermark programming here.
4700 */
4701 if (needs_modeset(&pipe_config->base))
4702 return;
4703
4704 /*
4705 * For platforms that support atomic watermarks, program the
4706 * 'intermediate' watermarks immediately. On pre-gen9 platforms, these
4707 * will be the intermediate values that are safe for both pre- and
4708 * post- vblank; when vblank happens, the 'active' values will be set
4709 * to the final 'target' values and we'll do this again to get the
4710 * optimal watermarks. For gen9+ platforms, the values we program here
4711 * will be the final target values which will get automatically latched
4712 * at vblank time; no further programming will be necessary.
4713 *
4714 * If a platform hasn't been transitioned to atomic watermarks yet,
4715 * we'll continue to update watermarks the old way, if flags tell
4716 * us to.
4717 */
4718 if (dev_priv->display.initial_watermarks != NULL)
4719 dev_priv->display.initial_watermarks(pipe_config);
caed361d 4720 else if (pipe_config->update_wm_pre)
92826fcd 4721 intel_update_watermarks(&crtc->base);
ac21b225
ML
4722}
4723
d032ffa0 4724static void intel_crtc_disable_planes(struct drm_crtc *crtc, unsigned plane_mask)
87d4300a
ML
4725{
4726 struct drm_device *dev = crtc->dev;
4727 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
d032ffa0 4728 struct drm_plane *p;
87d4300a
ML
4729 int pipe = intel_crtc->pipe;
4730
7cac945f 4731 intel_crtc_dpms_overlay_disable(intel_crtc);
27321ae8 4732
d032ffa0
ML
4733 drm_for_each_plane_mask(p, dev, plane_mask)
4734 to_intel_plane(p)->disable_plane(p, crtc);
f98551ae 4735
f99d7069
DV
4736 /*
4737 * FIXME: Once we grow proper nuclear flip support out of this we need
4738 * to compute the mask of flip planes precisely. For the time being
4739 * consider this a flip to a NULL plane.
4740 */
4741 intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
a5c4d7bc
VS
4742}
4743
f67a559d
JB
4744static void ironlake_crtc_enable(struct drm_crtc *crtc)
4745{
4746 struct drm_device *dev = crtc->dev;
4747 struct drm_i915_private *dev_priv = dev->dev_private;
4748 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 4749 struct intel_encoder *encoder;
f67a559d 4750 int pipe = intel_crtc->pipe;
b95c5321
ML
4751 struct intel_crtc_state *pipe_config =
4752 to_intel_crtc_state(crtc->state);
f67a559d 4753
53d9f4e9 4754 if (WARN_ON(intel_crtc->active))
f67a559d
JB
4755 return;
4756
b2c0593a
VS
4757 /*
4758 * Sometimes spurious CPU pipe underruns happen during FDI
4759 * training, at least with VGA+HDMI cloning. Suppress them.
4760 *
4761 * On ILK we get an occasional spurious CPU pipe underruns
4762 * between eDP port A enable and vdd enable. Also PCH port
4763 * enable seems to result in the occasional CPU pipe underrun.
4764 *
4765 * Spurious PCH underruns also occur during PCH enabling.
4766 */
4767 if (intel_crtc->config->has_pch_encoder || IS_GEN5(dev_priv))
4768 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
81b088ca
VS
4769 if (intel_crtc->config->has_pch_encoder)
4770 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
4771
6e3c9717 4772 if (intel_crtc->config->has_pch_encoder)
b14b1055
DV
4773 intel_prepare_shared_dpll(intel_crtc);
4774
6e3c9717 4775 if (intel_crtc->config->has_dp_encoder)
fe3cd48d 4776 intel_dp_set_m_n(intel_crtc, M1_N1);
29407aab
DV
4777
4778 intel_set_pipe_timings(intel_crtc);
bc58be60 4779 intel_set_pipe_src_size(intel_crtc);
29407aab 4780
6e3c9717 4781 if (intel_crtc->config->has_pch_encoder) {
29407aab 4782 intel_cpu_transcoder_set_m_n(intel_crtc,
6e3c9717 4783 &intel_crtc->config->fdi_m_n, NULL);
29407aab
DV
4784 }
4785
4786 ironlake_set_pipeconf(crtc);
4787
f67a559d 4788 intel_crtc->active = true;
8664281b 4789
f6736a1a 4790 for_each_encoder_on_crtc(dev, crtc, encoder)
952735ee
DV
4791 if (encoder->pre_enable)
4792 encoder->pre_enable(encoder);
f67a559d 4793
6e3c9717 4794 if (intel_crtc->config->has_pch_encoder) {
fff367c7
DV
4795 /* Note: FDI PLL enabling _must_ be done before we enable the
4796 * cpu pipes, hence this is separate from all the other fdi/pch
4797 * enabling. */
88cefb6c 4798 ironlake_fdi_pll_enable(intel_crtc);
46b6f814
DV
4799 } else {
4800 assert_fdi_tx_disabled(dev_priv, pipe);
4801 assert_fdi_rx_disabled(dev_priv, pipe);
4802 }
f67a559d 4803
b074cec8 4804 ironlake_pfit_enable(intel_crtc);
f67a559d 4805
9c54c0dd
JB
4806 /*
4807 * On ILK+ LUT must be loaded before the pipe is running but with
4808 * clocks enabled
4809 */
b95c5321 4810 intel_color_load_luts(&pipe_config->base);
9c54c0dd 4811
1d5bf5d9
ID
4812 if (dev_priv->display.initial_watermarks != NULL)
4813 dev_priv->display.initial_watermarks(intel_crtc->config);
e1fdc473 4814 intel_enable_pipe(intel_crtc);
f67a559d 4815
6e3c9717 4816 if (intel_crtc->config->has_pch_encoder)
f67a559d 4817 ironlake_pch_enable(crtc);
c98e9dcf 4818
f9b61ff6
DV
4819 assert_vblank_disabled(crtc);
4820 drm_crtc_vblank_on(crtc);
4821
fa5c73b1
DV
4822 for_each_encoder_on_crtc(dev, crtc, encoder)
4823 encoder->enable(encoder);
61b77ddd
DV
4824
4825 if (HAS_PCH_CPT(dev))
a1520318 4826 cpt_verify_modeset(dev, intel_crtc->pipe);
37ca8d4c
VS
4827
4828 /* Must wait for vblank to avoid spurious PCH FIFO underruns */
4829 if (intel_crtc->config->has_pch_encoder)
4830 intel_wait_for_vblank(dev, pipe);
b2c0593a 4831 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
37ca8d4c 4832 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
6be4a607
JB
4833}
4834
42db64ef
PZ
4835/* IPS only exists on ULT machines and is tied to pipe A. */
4836static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
4837{
f5adf94e 4838 return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
42db64ef
PZ
4839}
4840
4f771f10
PZ
4841static void haswell_crtc_enable(struct drm_crtc *crtc)
4842{
4843 struct drm_device *dev = crtc->dev;
4844 struct drm_i915_private *dev_priv = dev->dev_private;
4845 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4846 struct intel_encoder *encoder;
99d736a2 4847 int pipe = intel_crtc->pipe, hsw_workaround_pipe;
4d1de975 4848 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
99d736a2
ML
4849 struct intel_crtc_state *pipe_config =
4850 to_intel_crtc_state(crtc->state);
4f771f10 4851
53d9f4e9 4852 if (WARN_ON(intel_crtc->active))
4f771f10
PZ
4853 return;
4854
81b088ca
VS
4855 if (intel_crtc->config->has_pch_encoder)
4856 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
4857 false);
4858
95a7a2ae
ID
4859 for_each_encoder_on_crtc(dev, crtc, encoder)
4860 if (encoder->pre_pll_enable)
4861 encoder->pre_pll_enable(encoder);
4862
8106ddbd 4863 if (intel_crtc->config->shared_dpll)
df8ad70c
DV
4864 intel_enable_shared_dpll(intel_crtc);
4865
6e3c9717 4866 if (intel_crtc->config->has_dp_encoder)
fe3cd48d 4867 intel_dp_set_m_n(intel_crtc, M1_N1);
229fca97 4868
4d1de975
JN
4869 if (!intel_crtc->config->has_dsi_encoder)
4870 intel_set_pipe_timings(intel_crtc);
4871
bc58be60 4872 intel_set_pipe_src_size(intel_crtc);
229fca97 4873
4d1de975
JN
4874 if (cpu_transcoder != TRANSCODER_EDP &&
4875 !transcoder_is_dsi(cpu_transcoder)) {
4876 I915_WRITE(PIPE_MULT(cpu_transcoder),
6e3c9717 4877 intel_crtc->config->pixel_multiplier - 1);
ebb69c95
CT
4878 }
4879
6e3c9717 4880 if (intel_crtc->config->has_pch_encoder) {
229fca97 4881 intel_cpu_transcoder_set_m_n(intel_crtc,
6e3c9717 4882 &intel_crtc->config->fdi_m_n, NULL);
229fca97
DV
4883 }
4884
4d1de975
JN
4885 if (!intel_crtc->config->has_dsi_encoder)
4886 haswell_set_pipeconf(crtc);
4887
391bf048 4888 haswell_set_pipemisc(crtc);
229fca97 4889
b95c5321 4890 intel_color_set_csc(&pipe_config->base);
229fca97 4891
4f771f10 4892 intel_crtc->active = true;
8664281b 4893
6b698516
DV
4894 if (intel_crtc->config->has_pch_encoder)
4895 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
4896 else
4897 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4898
7d4aefd0 4899 for_each_encoder_on_crtc(dev, crtc, encoder) {
4f771f10
PZ
4900 if (encoder->pre_enable)
4901 encoder->pre_enable(encoder);
7d4aefd0 4902 }
4f771f10 4903
d2d65408 4904 if (intel_crtc->config->has_pch_encoder)
4fe9467d 4905 dev_priv->display.fdi_link_train(crtc);
4fe9467d 4906
a65347ba 4907 if (!intel_crtc->config->has_dsi_encoder)
7d4aefd0 4908 intel_ddi_enable_pipe_clock(intel_crtc);
4f771f10 4909
1c132b44 4910 if (INTEL_INFO(dev)->gen >= 9)
e435d6e5 4911 skylake_pfit_enable(intel_crtc);
ff6d9f55 4912 else
1c132b44 4913 ironlake_pfit_enable(intel_crtc);
4f771f10
PZ
4914
4915 /*
4916 * On ILK+ LUT must be loaded before the pipe is running but with
4917 * clocks enabled
4918 */
b95c5321 4919 intel_color_load_luts(&pipe_config->base);
4f771f10 4920
1f544388 4921 intel_ddi_set_pipe_settings(crtc);
a65347ba 4922 if (!intel_crtc->config->has_dsi_encoder)
7d4aefd0 4923 intel_ddi_enable_transcoder_func(crtc);
4f771f10 4924
1d5bf5d9
ID
4925 if (dev_priv->display.initial_watermarks != NULL)
4926 dev_priv->display.initial_watermarks(pipe_config);
4927 else
4928 intel_update_watermarks(crtc);
4d1de975
JN
4929
4930 /* XXX: Do the pipe assertions at the right place for BXT DSI. */
4931 if (!intel_crtc->config->has_dsi_encoder)
4932 intel_enable_pipe(intel_crtc);
42db64ef 4933
6e3c9717 4934 if (intel_crtc->config->has_pch_encoder)
1507e5bd 4935 lpt_pch_enable(crtc);
4f771f10 4936
a65347ba 4937 if (intel_crtc->config->dp_encoder_is_mst)
0e32b39c
DA
4938 intel_ddi_set_vc_payload_alloc(crtc, true);
4939
f9b61ff6
DV
4940 assert_vblank_disabled(crtc);
4941 drm_crtc_vblank_on(crtc);
4942
8807e55b 4943 for_each_encoder_on_crtc(dev, crtc, encoder) {
4f771f10 4944 encoder->enable(encoder);
8807e55b
JN
4945 intel_opregion_notify_encoder(encoder, true);
4946 }
4f771f10 4947
6b698516
DV
4948 if (intel_crtc->config->has_pch_encoder) {
4949 intel_wait_for_vblank(dev, pipe);
4950 intel_wait_for_vblank(dev, pipe);
4951 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
d2d65408
VS
4952 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
4953 true);
6b698516 4954 }
d2d65408 4955
e4916946
PZ
4956 /* If we change the relative order between pipe/planes enabling, we need
4957 * to change the workaround. */
99d736a2
ML
4958 hsw_workaround_pipe = pipe_config->hsw_workaround_pipe;
4959 if (IS_HASWELL(dev) && hsw_workaround_pipe != INVALID_PIPE) {
4960 intel_wait_for_vblank(dev, hsw_workaround_pipe);
4961 intel_wait_for_vblank(dev, hsw_workaround_pipe);
4962 }
4f771f10
PZ
4963}
4964
bfd16b2a 4965static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force)
3f8dce3a
DV
4966{
4967 struct drm_device *dev = crtc->base.dev;
4968 struct drm_i915_private *dev_priv = dev->dev_private;
4969 int pipe = crtc->pipe;
4970
4971 /* To avoid upsetting the power well on haswell only disable the pfit if
4972 * it's in use. The hw state code will make sure we get this right. */
bfd16b2a 4973 if (force || crtc->config->pch_pfit.enabled) {
3f8dce3a
DV
4974 I915_WRITE(PF_CTL(pipe), 0);
4975 I915_WRITE(PF_WIN_POS(pipe), 0);
4976 I915_WRITE(PF_WIN_SZ(pipe), 0);
4977 }
4978}
4979
6be4a607
JB
4980static void ironlake_crtc_disable(struct drm_crtc *crtc)
4981{
4982 struct drm_device *dev = crtc->dev;
4983 struct drm_i915_private *dev_priv = dev->dev_private;
4984 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 4985 struct intel_encoder *encoder;
6be4a607 4986 int pipe = intel_crtc->pipe;
b52eb4dc 4987
b2c0593a
VS
4988 /*
4989 * Sometimes spurious CPU pipe underruns happen when the
4990 * pipe is already disabled, but FDI RX/TX is still enabled.
4991 * Happens at least with VGA+HDMI cloning. Suppress them.
4992 */
4993 if (intel_crtc->config->has_pch_encoder) {
4994 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
37ca8d4c 4995 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
b2c0593a 4996 }
37ca8d4c 4997
ea9d758d
DV
4998 for_each_encoder_on_crtc(dev, crtc, encoder)
4999 encoder->disable(encoder);
5000
f9b61ff6
DV
5001 drm_crtc_vblank_off(crtc);
5002 assert_vblank_disabled(crtc);
5003
575f7ab7 5004 intel_disable_pipe(intel_crtc);
32f9d658 5005
bfd16b2a 5006 ironlake_pfit_disable(intel_crtc, false);
2c07245f 5007
b2c0593a 5008 if (intel_crtc->config->has_pch_encoder)
5a74f70a
VS
5009 ironlake_fdi_disable(crtc);
5010
bf49ec8c
DV
5011 for_each_encoder_on_crtc(dev, crtc, encoder)
5012 if (encoder->post_disable)
5013 encoder->post_disable(encoder);
2c07245f 5014
6e3c9717 5015 if (intel_crtc->config->has_pch_encoder) {
d925c59a 5016 ironlake_disable_pch_transcoder(dev_priv, pipe);
6be4a607 5017
d925c59a 5018 if (HAS_PCH_CPT(dev)) {
f0f59a00
VS
5019 i915_reg_t reg;
5020 u32 temp;
5021
d925c59a
DV
5022 /* disable TRANS_DP_CTL */
5023 reg = TRANS_DP_CTL(pipe);
5024 temp = I915_READ(reg);
5025 temp &= ~(TRANS_DP_OUTPUT_ENABLE |
5026 TRANS_DP_PORT_SEL_MASK);
5027 temp |= TRANS_DP_PORT_SEL_NONE;
5028 I915_WRITE(reg, temp);
5029
5030 /* disable DPLL_SEL */
5031 temp = I915_READ(PCH_DPLL_SEL);
11887397 5032 temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
d925c59a 5033 I915_WRITE(PCH_DPLL_SEL, temp);
9db4a9c7 5034 }
e3421a18 5035
d925c59a
DV
5036 ironlake_fdi_pll_disable(intel_crtc);
5037 }
81b088ca 5038
b2c0593a 5039 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
81b088ca 5040 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
6be4a607 5041}
1b3c7a47 5042
4f771f10 5043static void haswell_crtc_disable(struct drm_crtc *crtc)
ee7b9f93 5044{
4f771f10
PZ
5045 struct drm_device *dev = crtc->dev;
5046 struct drm_i915_private *dev_priv = dev->dev_private;
ee7b9f93 5047 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4f771f10 5048 struct intel_encoder *encoder;
6e3c9717 5049 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
ee7b9f93 5050
d2d65408
VS
5051 if (intel_crtc->config->has_pch_encoder)
5052 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5053 false);
5054
8807e55b
JN
5055 for_each_encoder_on_crtc(dev, crtc, encoder) {
5056 intel_opregion_notify_encoder(encoder, false);
4f771f10 5057 encoder->disable(encoder);
8807e55b 5058 }
4f771f10 5059
f9b61ff6
DV
5060 drm_crtc_vblank_off(crtc);
5061 assert_vblank_disabled(crtc);
5062
4d1de975
JN
5063 /* XXX: Do the pipe assertions at the right place for BXT DSI. */
5064 if (!intel_crtc->config->has_dsi_encoder)
5065 intel_disable_pipe(intel_crtc);
4f771f10 5066
6e3c9717 5067 if (intel_crtc->config->dp_encoder_is_mst)
a4bf214f
VS
5068 intel_ddi_set_vc_payload_alloc(crtc, false);
5069
a65347ba 5070 if (!intel_crtc->config->has_dsi_encoder)
7d4aefd0 5071 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
4f771f10 5072
1c132b44 5073 if (INTEL_INFO(dev)->gen >= 9)
e435d6e5 5074 skylake_scaler_disable(intel_crtc);
ff6d9f55 5075 else
bfd16b2a 5076 ironlake_pfit_disable(intel_crtc, false);
4f771f10 5077
a65347ba 5078 if (!intel_crtc->config->has_dsi_encoder)
7d4aefd0 5079 intel_ddi_disable_pipe_clock(intel_crtc);
4f771f10 5080
97b040aa
ID
5081 for_each_encoder_on_crtc(dev, crtc, encoder)
5082 if (encoder->post_disable)
5083 encoder->post_disable(encoder);
81b088ca 5084
92966a37
VS
5085 if (intel_crtc->config->has_pch_encoder) {
5086 lpt_disable_pch_transcoder(dev_priv);
503a74e9 5087 lpt_disable_iclkip(dev_priv);
92966a37
VS
5088 intel_ddi_fdi_disable(crtc);
5089
81b088ca
VS
5090 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5091 true);
92966a37 5092 }
4f771f10
PZ
5093}
5094
2dd24552
JB
5095static void i9xx_pfit_enable(struct intel_crtc *crtc)
5096{
5097 struct drm_device *dev = crtc->base.dev;
5098 struct drm_i915_private *dev_priv = dev->dev_private;
6e3c9717 5099 struct intel_crtc_state *pipe_config = crtc->config;
2dd24552 5100
681a8504 5101 if (!pipe_config->gmch_pfit.control)
2dd24552
JB
5102 return;
5103
2dd24552 5104 /*
c0b03411
DV
5105 * The panel fitter should only be adjusted whilst the pipe is disabled,
5106 * according to register description and PRM.
2dd24552 5107 */
c0b03411
DV
5108 WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
5109 assert_pipe_disabled(dev_priv, crtc->pipe);
2dd24552 5110
b074cec8
JB
5111 I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
5112 I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
5a80c45c
DV
5113
5114 /* Border color in case we don't scale up to the full screen. Black by
5115 * default, change to something else for debugging. */
5116 I915_WRITE(BCLRPAT(crtc->pipe), 0);
2dd24552
JB
5117}
5118
d05410f9
DA
5119static enum intel_display_power_domain port_to_power_domain(enum port port)
5120{
5121 switch (port) {
5122 case PORT_A:
6331a704 5123 return POWER_DOMAIN_PORT_DDI_A_LANES;
d05410f9 5124 case PORT_B:
6331a704 5125 return POWER_DOMAIN_PORT_DDI_B_LANES;
d05410f9 5126 case PORT_C:
6331a704 5127 return POWER_DOMAIN_PORT_DDI_C_LANES;
d05410f9 5128 case PORT_D:
6331a704 5129 return POWER_DOMAIN_PORT_DDI_D_LANES;
d8e19f99 5130 case PORT_E:
6331a704 5131 return POWER_DOMAIN_PORT_DDI_E_LANES;
d05410f9 5132 default:
b9fec167 5133 MISSING_CASE(port);
d05410f9
DA
5134 return POWER_DOMAIN_PORT_OTHER;
5135 }
5136}
5137
25f78f58
VS
5138static enum intel_display_power_domain port_to_aux_power_domain(enum port port)
5139{
5140 switch (port) {
5141 case PORT_A:
5142 return POWER_DOMAIN_AUX_A;
5143 case PORT_B:
5144 return POWER_DOMAIN_AUX_B;
5145 case PORT_C:
5146 return POWER_DOMAIN_AUX_C;
5147 case PORT_D:
5148 return POWER_DOMAIN_AUX_D;
5149 case PORT_E:
5150 /* FIXME: Check VBT for actual wiring of PORT E */
5151 return POWER_DOMAIN_AUX_D;
5152 default:
b9fec167 5153 MISSING_CASE(port);
25f78f58
VS
5154 return POWER_DOMAIN_AUX_A;
5155 }
5156}
5157
319be8ae
ID
5158enum intel_display_power_domain
5159intel_display_port_power_domain(struct intel_encoder *intel_encoder)
5160{
5161 struct drm_device *dev = intel_encoder->base.dev;
5162 struct intel_digital_port *intel_dig_port;
5163
5164 switch (intel_encoder->type) {
5165 case INTEL_OUTPUT_UNKNOWN:
5166 /* Only DDI platforms should ever use this output type */
5167 WARN_ON_ONCE(!HAS_DDI(dev));
5168 case INTEL_OUTPUT_DISPLAYPORT:
5169 case INTEL_OUTPUT_HDMI:
5170 case INTEL_OUTPUT_EDP:
5171 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
d05410f9 5172 return port_to_power_domain(intel_dig_port->port);
0e32b39c
DA
5173 case INTEL_OUTPUT_DP_MST:
5174 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
5175 return port_to_power_domain(intel_dig_port->port);
319be8ae
ID
5176 case INTEL_OUTPUT_ANALOG:
5177 return POWER_DOMAIN_PORT_CRT;
5178 case INTEL_OUTPUT_DSI:
5179 return POWER_DOMAIN_PORT_DSI;
5180 default:
5181 return POWER_DOMAIN_PORT_OTHER;
5182 }
5183}
5184
25f78f58
VS
5185enum intel_display_power_domain
5186intel_display_port_aux_power_domain(struct intel_encoder *intel_encoder)
5187{
5188 struct drm_device *dev = intel_encoder->base.dev;
5189 struct intel_digital_port *intel_dig_port;
5190
5191 switch (intel_encoder->type) {
5192 case INTEL_OUTPUT_UNKNOWN:
651174a4
ID
5193 case INTEL_OUTPUT_HDMI:
5194 /*
5195 * Only DDI platforms should ever use these output types.
5196 * We can get here after the HDMI detect code has already set
5197 * the type of the shared encoder. Since we can't be sure
5198 * what's the status of the given connectors, play safe and
5199 * run the DP detection too.
5200 */
25f78f58
VS
5201 WARN_ON_ONCE(!HAS_DDI(dev));
5202 case INTEL_OUTPUT_DISPLAYPORT:
5203 case INTEL_OUTPUT_EDP:
5204 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
5205 return port_to_aux_power_domain(intel_dig_port->port);
5206 case INTEL_OUTPUT_DP_MST:
5207 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
5208 return port_to_aux_power_domain(intel_dig_port->port);
5209 default:
b9fec167 5210 MISSING_CASE(intel_encoder->type);
25f78f58
VS
5211 return POWER_DOMAIN_AUX_A;
5212 }
5213}
5214
74bff5f9
ML
5215static unsigned long get_crtc_power_domains(struct drm_crtc *crtc,
5216 struct intel_crtc_state *crtc_state)
77d22dca 5217{
319be8ae 5218 struct drm_device *dev = crtc->dev;
74bff5f9 5219 struct drm_encoder *encoder;
319be8ae
ID
5220 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5221 enum pipe pipe = intel_crtc->pipe;
77d22dca 5222 unsigned long mask;
74bff5f9 5223 enum transcoder transcoder = crtc_state->cpu_transcoder;
77d22dca 5224
74bff5f9 5225 if (!crtc_state->base.active)
292b990e
ML
5226 return 0;
5227
77d22dca
ID
5228 mask = BIT(POWER_DOMAIN_PIPE(pipe));
5229 mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
74bff5f9
ML
5230 if (crtc_state->pch_pfit.enabled ||
5231 crtc_state->pch_pfit.force_thru)
77d22dca
ID
5232 mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
5233
74bff5f9
ML
5234 drm_for_each_encoder_mask(encoder, dev, crtc_state->base.encoder_mask) {
5235 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
5236
319be8ae 5237 mask |= BIT(intel_display_port_power_domain(intel_encoder));
74bff5f9 5238 }
319be8ae 5239
15e7ec29
ML
5240 if (crtc_state->shared_dpll)
5241 mask |= BIT(POWER_DOMAIN_PLLS);
5242
77d22dca
ID
5243 return mask;
5244}
5245
74bff5f9
ML
5246static unsigned long
5247modeset_get_crtc_power_domains(struct drm_crtc *crtc,
5248 struct intel_crtc_state *crtc_state)
77d22dca 5249{
292b990e
ML
5250 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
5251 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5252 enum intel_display_power_domain domain;
5a21b665 5253 unsigned long domains, new_domains, old_domains;
77d22dca 5254
292b990e 5255 old_domains = intel_crtc->enabled_power_domains;
74bff5f9
ML
5256 intel_crtc->enabled_power_domains = new_domains =
5257 get_crtc_power_domains(crtc, crtc_state);
77d22dca 5258
5a21b665 5259 domains = new_domains & ~old_domains;
292b990e
ML
5260
5261 for_each_power_domain(domain, domains)
5262 intel_display_power_get(dev_priv, domain);
5263
5a21b665 5264 return old_domains & ~new_domains;
292b990e
ML
5265}
5266
5267static void modeset_put_power_domains(struct drm_i915_private *dev_priv,
5268 unsigned long domains)
5269{
5270 enum intel_display_power_domain domain;
5271
5272 for_each_power_domain(domain, domains)
5273 intel_display_power_put(dev_priv, domain);
5274}
77d22dca 5275
adafdc6f
MK
5276static int intel_compute_max_dotclk(struct drm_i915_private *dev_priv)
5277{
5278 int max_cdclk_freq = dev_priv->max_cdclk_freq;
5279
5280 if (INTEL_INFO(dev_priv)->gen >= 9 ||
5281 IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
5282 return max_cdclk_freq;
5283 else if (IS_CHERRYVIEW(dev_priv))
5284 return max_cdclk_freq*95/100;
5285 else if (INTEL_INFO(dev_priv)->gen < 4)
5286 return 2*max_cdclk_freq*90/100;
5287 else
5288 return max_cdclk_freq*90/100;
5289}
5290
b2045352
VS
5291static int skl_calc_cdclk(int max_pixclk, int vco);
5292
560a7ae4
DL
5293static void intel_update_max_cdclk(struct drm_device *dev)
5294{
5295 struct drm_i915_private *dev_priv = dev->dev_private;
5296
ef11bdb3 5297 if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) {
560a7ae4 5298 u32 limit = I915_READ(SKL_DFSM) & SKL_DFSM_CDCLK_LIMIT_MASK;
b2045352
VS
5299 int max_cdclk, vco;
5300
5301 vco = dev_priv->skl_preferred_vco_freq;
63911d72 5302 WARN_ON(vco != 8100000 && vco != 8640000);
560a7ae4 5303
b2045352
VS
5304 /*
5305 * Use the lower (vco 8640) cdclk values as a
5306 * first guess. skl_calc_cdclk() will correct it
5307 * if the preferred vco is 8100 instead.
5308 */
560a7ae4 5309 if (limit == SKL_DFSM_CDCLK_LIMIT_675)
487ed2e4 5310 max_cdclk = 617143;
560a7ae4 5311 else if (limit == SKL_DFSM_CDCLK_LIMIT_540)
b2045352 5312 max_cdclk = 540000;
560a7ae4 5313 else if (limit == SKL_DFSM_CDCLK_LIMIT_450)
b2045352 5314 max_cdclk = 432000;
560a7ae4 5315 else
487ed2e4 5316 max_cdclk = 308571;
b2045352
VS
5317
5318 dev_priv->max_cdclk_freq = skl_calc_cdclk(max_cdclk, vco);
281c114f
MR
5319 } else if (IS_BROXTON(dev)) {
5320 dev_priv->max_cdclk_freq = 624000;
560a7ae4
DL
5321 } else if (IS_BROADWELL(dev)) {
5322 /*
5323 * FIXME with extra cooling we can allow
5324 * 540 MHz for ULX and 675 Mhz for ULT.
5325 * How can we know if extra cooling is
5326 * available? PCI ID, VTB, something else?
5327 */
5328 if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
5329 dev_priv->max_cdclk_freq = 450000;
5330 else if (IS_BDW_ULX(dev))
5331 dev_priv->max_cdclk_freq = 450000;
5332 else if (IS_BDW_ULT(dev))
5333 dev_priv->max_cdclk_freq = 540000;
5334 else
5335 dev_priv->max_cdclk_freq = 675000;
0904deaf
MK
5336 } else if (IS_CHERRYVIEW(dev)) {
5337 dev_priv->max_cdclk_freq = 320000;
560a7ae4
DL
5338 } else if (IS_VALLEYVIEW(dev)) {
5339 dev_priv->max_cdclk_freq = 400000;
5340 } else {
5341 /* otherwise assume cdclk is fixed */
5342 dev_priv->max_cdclk_freq = dev_priv->cdclk_freq;
5343 }
5344
adafdc6f
MK
5345 dev_priv->max_dotclk_freq = intel_compute_max_dotclk(dev_priv);
5346
560a7ae4
DL
5347 DRM_DEBUG_DRIVER("Max CD clock rate: %d kHz\n",
5348 dev_priv->max_cdclk_freq);
adafdc6f
MK
5349
5350 DRM_DEBUG_DRIVER("Max dotclock rate: %d kHz\n",
5351 dev_priv->max_dotclk_freq);
560a7ae4
DL
5352}
5353
5354static void intel_update_cdclk(struct drm_device *dev)
5355{
5356 struct drm_i915_private *dev_priv = dev->dev_private;
5357
5358 dev_priv->cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
2f2a121a 5359
83d7c81f 5360 if (INTEL_GEN(dev_priv) >= 9)
709e05c3
VS
5361 DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz, VCO: %d kHz, ref: %d kHz\n",
5362 dev_priv->cdclk_freq, dev_priv->cdclk_pll.vco,
5363 dev_priv->cdclk_pll.ref);
2f2a121a
VS
5364 else
5365 DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
5366 dev_priv->cdclk_freq);
560a7ae4
DL
5367
5368 /*
b5d99ff9
VS
5369 * 9:0 CMBUS [sic] CDCLK frequency (cdfreq):
5370 * Programmng [sic] note: bit[9:2] should be programmed to the number
5371 * of cdclk that generates 4MHz reference clock freq which is used to
5372 * generate GMBus clock. This will vary with the cdclk freq.
560a7ae4 5373 */
b5d99ff9 5374 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
560a7ae4 5375 I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->cdclk_freq, 1000));
560a7ae4
DL
5376}
5377
92891e45
VS
5378/* convert from kHz to .1 fixpoint MHz with -1MHz offset */
5379static int skl_cdclk_decimal(int cdclk)
5380{
5381 return DIV_ROUND_CLOSEST(cdclk - 1000, 500);
5382}
5383
5f199dfa
VS
5384static int bxt_de_pll_vco(struct drm_i915_private *dev_priv, int cdclk)
5385{
5386 int ratio;
5387
5388 if (cdclk == dev_priv->cdclk_pll.ref)
5389 return 0;
5390
5391 switch (cdclk) {
5392 default:
5393 MISSING_CASE(cdclk);
5394 case 144000:
5395 case 288000:
5396 case 384000:
5397 case 576000:
5398 ratio = 60;
5399 break;
5400 case 624000:
5401 ratio = 65;
5402 break;
5403 }
5404
5405 return dev_priv->cdclk_pll.ref * ratio;
5406}
5407
2b73001e
VS
5408static void bxt_de_pll_disable(struct drm_i915_private *dev_priv)
5409{
5410 I915_WRITE(BXT_DE_PLL_ENABLE, 0);
5411
5412 /* Timeout 200us */
5413 if (wait_for((I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK) == 0, 1))
5414 DRM_ERROR("timeout waiting for DE PLL unlock\n");
83d7c81f
VS
5415
5416 dev_priv->cdclk_pll.vco = 0;
2b73001e
VS
5417}
5418
5f199dfa 5419static void bxt_de_pll_enable(struct drm_i915_private *dev_priv, int vco)
2b73001e 5420{
5f199dfa 5421 int ratio = DIV_ROUND_CLOSEST(vco, dev_priv->cdclk_pll.ref);
2b73001e
VS
5422 u32 val;
5423
5424 val = I915_READ(BXT_DE_PLL_CTL);
5425 val &= ~BXT_DE_PLL_RATIO_MASK;
5f199dfa 5426 val |= BXT_DE_PLL_RATIO(ratio);
2b73001e
VS
5427 I915_WRITE(BXT_DE_PLL_CTL, val);
5428
5429 I915_WRITE(BXT_DE_PLL_ENABLE, BXT_DE_PLL_PLL_ENABLE);
5430
5431 /* Timeout 200us */
5432 if (wait_for((I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK) != 0, 1))
5433 DRM_ERROR("timeout waiting for DE PLL lock\n");
83d7c81f 5434
5f199dfa 5435 dev_priv->cdclk_pll.vco = vco;
2b73001e
VS
5436}
5437
324513c0 5438static void bxt_set_cdclk(struct drm_i915_private *dev_priv, int cdclk)
f8437dd1 5439{
5f199dfa
VS
5440 u32 val, divider;
5441 int vco, ret;
f8437dd1 5442
5f199dfa
VS
5443 vco = bxt_de_pll_vco(dev_priv, cdclk);
5444
5445 DRM_DEBUG_DRIVER("Changing CDCLK to %d kHz (VCO %d kHz)\n", cdclk, vco);
5446
5447 /* cdclk = vco / 2 / div{1,1.5,2,4} */
5448 switch (DIV_ROUND_CLOSEST(vco, cdclk)) {
5449 case 8:
f8437dd1 5450 divider = BXT_CDCLK_CD2X_DIV_SEL_4;
f8437dd1 5451 break;
5f199dfa 5452 case 4:
f8437dd1 5453 divider = BXT_CDCLK_CD2X_DIV_SEL_2;
f8437dd1 5454 break;
5f199dfa 5455 case 3:
f8437dd1 5456 divider = BXT_CDCLK_CD2X_DIV_SEL_1_5;
f8437dd1 5457 break;
5f199dfa 5458 case 2:
f8437dd1 5459 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
f8437dd1
VK
5460 break;
5461 default:
5f199dfa
VS
5462 WARN_ON(cdclk != dev_priv->cdclk_pll.ref);
5463 WARN_ON(vco != 0);
f8437dd1 5464
5f199dfa
VS
5465 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
5466 break;
f8437dd1
VK
5467 }
5468
f8437dd1 5469 /* Inform power controller of upcoming frequency change */
5f199dfa 5470 mutex_lock(&dev_priv->rps.hw_lock);
f8437dd1
VK
5471 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
5472 0x80000000);
5473 mutex_unlock(&dev_priv->rps.hw_lock);
5474
5475 if (ret) {
5476 DRM_ERROR("PCode CDCLK freq change notify failed (err %d, freq %d)\n",
9ef56154 5477 ret, cdclk);
f8437dd1
VK
5478 return;
5479 }
5480
5f199dfa
VS
5481 if (dev_priv->cdclk_pll.vco != 0 &&
5482 dev_priv->cdclk_pll.vco != vco)
2b73001e 5483 bxt_de_pll_disable(dev_priv);
f8437dd1 5484
5f199dfa
VS
5485 if (dev_priv->cdclk_pll.vco != vco)
5486 bxt_de_pll_enable(dev_priv, vco);
f8437dd1 5487
5f199dfa
VS
5488 val = divider | skl_cdclk_decimal(cdclk);
5489 /*
5490 * FIXME if only the cd2x divider needs changing, it could be done
5491 * without shutting off the pipe (if only one pipe is active).
5492 */
5493 val |= BXT_CDCLK_CD2X_PIPE_NONE;
5494 /*
5495 * Disable SSA Precharge when CD clock frequency < 500 MHz,
5496 * enable otherwise.
5497 */
5498 if (cdclk >= 500000)
5499 val |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
5500 I915_WRITE(CDCLK_CTL, val);
f8437dd1
VK
5501
5502 mutex_lock(&dev_priv->rps.hw_lock);
5503 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
9ef56154 5504 DIV_ROUND_UP(cdclk, 25000));
f8437dd1
VK
5505 mutex_unlock(&dev_priv->rps.hw_lock);
5506
5507 if (ret) {
5508 DRM_ERROR("PCode CDCLK freq set failed, (err %d, freq %d)\n",
9ef56154 5509 ret, cdclk);
f8437dd1
VK
5510 return;
5511 }
5512
c6c4696f 5513 intel_update_cdclk(dev_priv->dev);
f8437dd1
VK
5514}
5515
d66a2194 5516static void bxt_sanitize_cdclk(struct drm_i915_private *dev_priv)
f8437dd1 5517{
d66a2194
ID
5518 u32 cdctl, expected;
5519
089c6fd5 5520 intel_update_cdclk(dev_priv->dev);
f8437dd1 5521
d66a2194
ID
5522 if (dev_priv->cdclk_pll.vco == 0 ||
5523 dev_priv->cdclk_freq == dev_priv->cdclk_pll.ref)
5524 goto sanitize;
5525
5526 /* DPLL okay; verify the cdclock
5527 *
5528 * Some BIOS versions leave an incorrect decimal frequency value and
5529 * set reserved MBZ bits in CDCLK_CTL at least during exiting from S4,
5530 * so sanitize this register.
5531 */
5532 cdctl = I915_READ(CDCLK_CTL);
5533 /*
5534 * Let's ignore the pipe field, since BIOS could have configured the
5535 * dividers both synching to an active pipe, or asynchronously
5536 * (PIPE_NONE).
5537 */
5538 cdctl &= ~BXT_CDCLK_CD2X_PIPE_NONE;
5539
5540 expected = (cdctl & BXT_CDCLK_CD2X_DIV_SEL_MASK) |
5541 skl_cdclk_decimal(dev_priv->cdclk_freq);
5542 /*
5543 * Disable SSA Precharge when CD clock frequency < 500 MHz,
5544 * enable otherwise.
5545 */
5546 if (dev_priv->cdclk_freq >= 500000)
5547 expected |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
5548
5549 if (cdctl == expected)
5550 /* All well; nothing to sanitize */
5551 return;
5552
5553sanitize:
5554 DRM_DEBUG_KMS("Sanitizing cdclk programmed by pre-os\n");
5555
5556 /* force cdclk programming */
5557 dev_priv->cdclk_freq = 0;
5558
5559 /* force full PLL disable + enable */
5560 dev_priv->cdclk_pll.vco = -1;
5561}
5562
324513c0 5563void bxt_init_cdclk(struct drm_i915_private *dev_priv)
d66a2194
ID
5564{
5565 bxt_sanitize_cdclk(dev_priv);
5566
5567 if (dev_priv->cdclk_freq != 0 && dev_priv->cdclk_pll.vco != 0)
089c6fd5 5568 return;
c2e001ef 5569
f8437dd1
VK
5570 /*
5571 * FIXME:
5572 * - The initial CDCLK needs to be read from VBT.
5573 * Need to make this change after VBT has changes for BXT.
f8437dd1 5574 */
324513c0 5575 bxt_set_cdclk(dev_priv, bxt_calc_cdclk(0));
f8437dd1
VK
5576}
5577
324513c0 5578void bxt_uninit_cdclk(struct drm_i915_private *dev_priv)
f8437dd1 5579{
324513c0 5580 bxt_set_cdclk(dev_priv, dev_priv->cdclk_pll.ref);
f8437dd1
VK
5581}
5582
a8ca4934
VS
5583static int skl_calc_cdclk(int max_pixclk, int vco)
5584{
63911d72 5585 if (vco == 8640000) {
a8ca4934 5586 if (max_pixclk > 540000)
487ed2e4 5587 return 617143;
a8ca4934
VS
5588 else if (max_pixclk > 432000)
5589 return 540000;
487ed2e4 5590 else if (max_pixclk > 308571)
a8ca4934
VS
5591 return 432000;
5592 else
487ed2e4 5593 return 308571;
a8ca4934 5594 } else {
a8ca4934
VS
5595 if (max_pixclk > 540000)
5596 return 675000;
5597 else if (max_pixclk > 450000)
5598 return 540000;
5599 else if (max_pixclk > 337500)
5600 return 450000;
5601 else
5602 return 337500;
5603 }
5604}
5605
ea61791e
VS
5606static void
5607skl_dpll0_update(struct drm_i915_private *dev_priv)
5d96d8af 5608{
ea61791e 5609 u32 val;
5d96d8af 5610
709e05c3 5611 dev_priv->cdclk_pll.ref = 24000;
1c3f7700 5612 dev_priv->cdclk_pll.vco = 0;
709e05c3 5613
ea61791e 5614 val = I915_READ(LCPLL1_CTL);
1c3f7700 5615 if ((val & LCPLL_PLL_ENABLE) == 0)
ea61791e 5616 return;
5d96d8af 5617
1c3f7700
ID
5618 if (WARN_ON((val & LCPLL_PLL_LOCK) == 0))
5619 return;
9f7eb31a 5620
ea61791e
VS
5621 val = I915_READ(DPLL_CTRL1);
5622
1c3f7700
ID
5623 if (WARN_ON((val & (DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) |
5624 DPLL_CTRL1_SSC(SKL_DPLL0) |
5625 DPLL_CTRL1_OVERRIDE(SKL_DPLL0))) !=
5626 DPLL_CTRL1_OVERRIDE(SKL_DPLL0)))
5627 return;
9f7eb31a 5628
ea61791e
VS
5629 switch (val & DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0)) {
5630 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810, SKL_DPLL0):
5631 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1350, SKL_DPLL0):
5632 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1620, SKL_DPLL0):
5633 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_2700, SKL_DPLL0):
63911d72 5634 dev_priv->cdclk_pll.vco = 8100000;
ea61791e
VS
5635 break;
5636 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080, SKL_DPLL0):
5637 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_2160, SKL_DPLL0):
63911d72 5638 dev_priv->cdclk_pll.vco = 8640000;
ea61791e
VS
5639 break;
5640 default:
5641 MISSING_CASE(val & DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
ea61791e
VS
5642 break;
5643 }
5d96d8af
DL
5644}
5645
b2045352
VS
5646void skl_set_preferred_cdclk_vco(struct drm_i915_private *dev_priv, int vco)
5647{
5648 bool changed = dev_priv->skl_preferred_vco_freq != vco;
5649
5650 dev_priv->skl_preferred_vco_freq = vco;
5651
5652 if (changed)
5653 intel_update_max_cdclk(dev_priv->dev);
5654}
5655
5d96d8af 5656static void
3861fc60 5657skl_dpll0_enable(struct drm_i915_private *dev_priv, int vco)
5d96d8af 5658{
a8ca4934 5659 int min_cdclk = skl_calc_cdclk(0, vco);
5d96d8af
DL
5660 u32 val;
5661
63911d72 5662 WARN_ON(vco != 8100000 && vco != 8640000);
b2045352 5663
5d96d8af 5664 /* select the minimum CDCLK before enabling DPLL 0 */
9ef56154 5665 val = CDCLK_FREQ_337_308 | skl_cdclk_decimal(min_cdclk);
5d96d8af
DL
5666 I915_WRITE(CDCLK_CTL, val);
5667 POSTING_READ(CDCLK_CTL);
5668
5669 /*
5670 * We always enable DPLL0 with the lowest link rate possible, but still
5671 * taking into account the VCO required to operate the eDP panel at the
5672 * desired frequency. The usual DP link rates operate with a VCO of
5673 * 8100 while the eDP 1.4 alternate link rates need a VCO of 8640.
5674 * The modeset code is responsible for the selection of the exact link
5675 * rate later on, with the constraint of choosing a frequency that
a8ca4934 5676 * works with vco.
5d96d8af
DL
5677 */
5678 val = I915_READ(DPLL_CTRL1);
5679
5680 val &= ~(DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) | DPLL_CTRL1_SSC(SKL_DPLL0) |
5681 DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
5682 val |= DPLL_CTRL1_OVERRIDE(SKL_DPLL0);
63911d72 5683 if (vco == 8640000)
5d96d8af
DL
5684 val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080,
5685 SKL_DPLL0);
5686 else
5687 val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810,
5688 SKL_DPLL0);
5689
5690 I915_WRITE(DPLL_CTRL1, val);
5691 POSTING_READ(DPLL_CTRL1);
5692
5693 I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) | LCPLL_PLL_ENABLE);
5694
5695 if (wait_for(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK, 5))
5696 DRM_ERROR("DPLL0 not locked\n");
1cd593e0 5697
63911d72 5698 dev_priv->cdclk_pll.vco = vco;
b2045352
VS
5699
5700 /* We'll want to keep using the current vco from now on. */
5701 skl_set_preferred_cdclk_vco(dev_priv, vco);
5d96d8af
DL
5702}
5703
430e05de
VS
5704static void
5705skl_dpll0_disable(struct drm_i915_private *dev_priv)
5706{
5707 I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) & ~LCPLL_PLL_ENABLE);
5708 if (wait_for(!(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK), 1))
5709 DRM_ERROR("Couldn't disable DPLL0\n");
1cd593e0 5710
63911d72 5711 dev_priv->cdclk_pll.vco = 0;
430e05de
VS
5712}
5713
5d96d8af
DL
5714static bool skl_cdclk_pcu_ready(struct drm_i915_private *dev_priv)
5715{
5716 int ret;
5717 u32 val;
5718
5719 /* inform PCU we want to change CDCLK */
5720 val = SKL_CDCLK_PREPARE_FOR_CHANGE;
5721 mutex_lock(&dev_priv->rps.hw_lock);
5722 ret = sandybridge_pcode_read(dev_priv, SKL_PCODE_CDCLK_CONTROL, &val);
5723 mutex_unlock(&dev_priv->rps.hw_lock);
5724
5725 return ret == 0 && (val & SKL_CDCLK_READY_FOR_CHANGE);
5726}
5727
5728static bool skl_cdclk_wait_for_pcu_ready(struct drm_i915_private *dev_priv)
5729{
5730 unsigned int i;
5731
5732 for (i = 0; i < 15; i++) {
5733 if (skl_cdclk_pcu_ready(dev_priv))
5734 return true;
5735 udelay(10);
5736 }
5737
5738 return false;
5739}
5740
1cd593e0 5741static void skl_set_cdclk(struct drm_i915_private *dev_priv, int cdclk, int vco)
5d96d8af 5742{
560a7ae4 5743 struct drm_device *dev = dev_priv->dev;
5d96d8af
DL
5744 u32 freq_select, pcu_ack;
5745
1cd593e0
VS
5746 WARN_ON((cdclk == 24000) != (vco == 0));
5747
63911d72 5748 DRM_DEBUG_DRIVER("Changing CDCLK to %d kHz (VCO %d kHz)\n", cdclk, vco);
5d96d8af
DL
5749
5750 if (!skl_cdclk_wait_for_pcu_ready(dev_priv)) {
5751 DRM_ERROR("failed to inform PCU about cdclk change\n");
5752 return;
5753 }
5754
5755 /* set CDCLK_CTL */
9ef56154 5756 switch (cdclk) {
5d96d8af
DL
5757 case 450000:
5758 case 432000:
5759 freq_select = CDCLK_FREQ_450_432;
5760 pcu_ack = 1;
5761 break;
5762 case 540000:
5763 freq_select = CDCLK_FREQ_540;
5764 pcu_ack = 2;
5765 break;
487ed2e4 5766 case 308571:
5d96d8af
DL
5767 case 337500:
5768 default:
5769 freq_select = CDCLK_FREQ_337_308;
5770 pcu_ack = 0;
5771 break;
487ed2e4 5772 case 617143:
5d96d8af
DL
5773 case 675000:
5774 freq_select = CDCLK_FREQ_675_617;
5775 pcu_ack = 3;
5776 break;
5777 }
5778
63911d72
VS
5779 if (dev_priv->cdclk_pll.vco != 0 &&
5780 dev_priv->cdclk_pll.vco != vco)
1cd593e0
VS
5781 skl_dpll0_disable(dev_priv);
5782
63911d72 5783 if (dev_priv->cdclk_pll.vco != vco)
1cd593e0
VS
5784 skl_dpll0_enable(dev_priv, vco);
5785
9ef56154 5786 I915_WRITE(CDCLK_CTL, freq_select | skl_cdclk_decimal(cdclk));
5d96d8af
DL
5787 POSTING_READ(CDCLK_CTL);
5788
5789 /* inform PCU of the change */
5790 mutex_lock(&dev_priv->rps.hw_lock);
5791 sandybridge_pcode_write(dev_priv, SKL_PCODE_CDCLK_CONTROL, pcu_ack);
5792 mutex_unlock(&dev_priv->rps.hw_lock);
560a7ae4
DL
5793
5794 intel_update_cdclk(dev);
5d96d8af
DL
5795}
5796
9f7eb31a
VS
5797static void skl_sanitize_cdclk(struct drm_i915_private *dev_priv);
5798
5d96d8af
DL
5799void skl_uninit_cdclk(struct drm_i915_private *dev_priv)
5800{
709e05c3 5801 skl_set_cdclk(dev_priv, dev_priv->cdclk_pll.ref, 0);
5d96d8af
DL
5802}
5803
5804void skl_init_cdclk(struct drm_i915_private *dev_priv)
5805{
9f7eb31a
VS
5806 int cdclk, vco;
5807
5808 skl_sanitize_cdclk(dev_priv);
5d96d8af 5809
63911d72 5810 if (dev_priv->cdclk_freq != 0 && dev_priv->cdclk_pll.vco != 0) {
9f7eb31a
VS
5811 /*
5812 * Use the current vco as our initial
5813 * guess as to what the preferred vco is.
5814 */
5815 if (dev_priv->skl_preferred_vco_freq == 0)
5816 skl_set_preferred_cdclk_vco(dev_priv,
63911d72 5817 dev_priv->cdclk_pll.vco);
70c2c184 5818 return;
1cd593e0 5819 }
5d96d8af 5820
70c2c184
VS
5821 vco = dev_priv->skl_preferred_vco_freq;
5822 if (vco == 0)
63911d72 5823 vco = 8100000;
70c2c184 5824 cdclk = skl_calc_cdclk(0, vco);
5d96d8af 5825
70c2c184 5826 skl_set_cdclk(dev_priv, cdclk, vco);
5d96d8af
DL
5827}
5828
9f7eb31a 5829static void skl_sanitize_cdclk(struct drm_i915_private *dev_priv)
c73666f3 5830{
09492498 5831 uint32_t cdctl, expected;
c73666f3 5832
f1b391a5
SK
5833 /*
5834 * check if the pre-os intialized the display
5835 * There is SWF18 scratchpad register defined which is set by the
5836 * pre-os which can be used by the OS drivers to check the status
5837 */
5838 if ((I915_READ(SWF_ILK(0x18)) & 0x00FFFFFF) == 0)
5839 goto sanitize;
5840
1c3f7700 5841 intel_update_cdclk(dev_priv->dev);
c73666f3 5842 /* Is PLL enabled and locked ? */
1c3f7700
ID
5843 if (dev_priv->cdclk_pll.vco == 0 ||
5844 dev_priv->cdclk_freq == dev_priv->cdclk_pll.ref)
c73666f3
SK
5845 goto sanitize;
5846
5847 /* DPLL okay; verify the cdclock
5848 *
5849 * Noticed in some instances that the freq selection is correct but
5850 * decimal part is programmed wrong from BIOS where pre-os does not
5851 * enable display. Verify the same as well.
5852 */
09492498
VS
5853 cdctl = I915_READ(CDCLK_CTL);
5854 expected = (cdctl & CDCLK_FREQ_SEL_MASK) |
5855 skl_cdclk_decimal(dev_priv->cdclk_freq);
5856 if (cdctl == expected)
c73666f3 5857 /* All well; nothing to sanitize */
9f7eb31a 5858 return;
c89e39f3 5859
9f7eb31a
VS
5860sanitize:
5861 DRM_DEBUG_KMS("Sanitizing cdclk programmed by pre-os\n");
c73666f3 5862
9f7eb31a
VS
5863 /* force cdclk programming */
5864 dev_priv->cdclk_freq = 0;
5865 /* force full PLL disable + enable */
63911d72 5866 dev_priv->cdclk_pll.vco = -1;
c73666f3
SK
5867}
5868
30a970c6
JB
5869/* Adjust CDclk dividers to allow high res or save power if possible */
5870static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
5871{
5872 struct drm_i915_private *dev_priv = dev->dev_private;
5873 u32 val, cmd;
5874
164dfd28
VK
5875 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5876 != dev_priv->cdclk_freq);
d60c4473 5877
dfcab17e 5878 if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
30a970c6 5879 cmd = 2;
dfcab17e 5880 else if (cdclk == 266667)
30a970c6
JB
5881 cmd = 1;
5882 else
5883 cmd = 0;
5884
5885 mutex_lock(&dev_priv->rps.hw_lock);
5886 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5887 val &= ~DSPFREQGUAR_MASK;
5888 val |= (cmd << DSPFREQGUAR_SHIFT);
5889 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5890 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5891 DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
5892 50)) {
5893 DRM_ERROR("timed out waiting for CDclk change\n");
5894 }
5895 mutex_unlock(&dev_priv->rps.hw_lock);
5896
54433e91
VS
5897 mutex_lock(&dev_priv->sb_lock);
5898
dfcab17e 5899 if (cdclk == 400000) {
6bcda4f0 5900 u32 divider;
30a970c6 5901
6bcda4f0 5902 divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
30a970c6 5903
30a970c6
JB
5904 /* adjust cdclk divider */
5905 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
87d5d259 5906 val &= ~CCK_FREQUENCY_VALUES;
30a970c6
JB
5907 val |= divider;
5908 vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
a877e801
VS
5909
5910 if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
87d5d259 5911 CCK_FREQUENCY_STATUS) == (divider << CCK_FREQUENCY_STATUS_SHIFT),
a877e801
VS
5912 50))
5913 DRM_ERROR("timed out waiting for CDclk change\n");
30a970c6
JB
5914 }
5915
30a970c6
JB
5916 /* adjust self-refresh exit latency value */
5917 val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
5918 val &= ~0x7f;
5919
5920 /*
5921 * For high bandwidth configs, we set a higher latency in the bunit
5922 * so that the core display fetch happens in time to avoid underruns.
5923 */
dfcab17e 5924 if (cdclk == 400000)
30a970c6
JB
5925 val |= 4500 / 250; /* 4.5 usec */
5926 else
5927 val |= 3000 / 250; /* 3.0 usec */
5928 vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
54433e91 5929
a580516d 5930 mutex_unlock(&dev_priv->sb_lock);
30a970c6 5931
b6283055 5932 intel_update_cdclk(dev);
30a970c6
JB
5933}
5934
383c5a6a
VS
5935static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
5936{
5937 struct drm_i915_private *dev_priv = dev->dev_private;
5938 u32 val, cmd;
5939
164dfd28
VK
5940 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5941 != dev_priv->cdclk_freq);
383c5a6a
VS
5942
5943 switch (cdclk) {
383c5a6a
VS
5944 case 333333:
5945 case 320000:
383c5a6a 5946 case 266667:
383c5a6a 5947 case 200000:
383c5a6a
VS
5948 break;
5949 default:
5f77eeb0 5950 MISSING_CASE(cdclk);
383c5a6a
VS
5951 return;
5952 }
5953
9d0d3fda
VS
5954 /*
5955 * Specs are full of misinformation, but testing on actual
5956 * hardware has shown that we just need to write the desired
5957 * CCK divider into the Punit register.
5958 */
5959 cmd = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
5960
383c5a6a
VS
5961 mutex_lock(&dev_priv->rps.hw_lock);
5962 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5963 val &= ~DSPFREQGUAR_MASK_CHV;
5964 val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
5965 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5966 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5967 DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
5968 50)) {
5969 DRM_ERROR("timed out waiting for CDclk change\n");
5970 }
5971 mutex_unlock(&dev_priv->rps.hw_lock);
5972
b6283055 5973 intel_update_cdclk(dev);
383c5a6a
VS
5974}
5975
30a970c6
JB
5976static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
5977 int max_pixclk)
5978{
6bcda4f0 5979 int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
6cca3195 5980 int limit = IS_CHERRYVIEW(dev_priv) ? 95 : 90;
29dc7ef3 5981
30a970c6
JB
5982 /*
5983 * Really only a few cases to deal with, as only 4 CDclks are supported:
5984 * 200MHz
5985 * 267MHz
29dc7ef3 5986 * 320/333MHz (depends on HPLL freq)
6cca3195
VS
5987 * 400MHz (VLV only)
5988 * So we check to see whether we're above 90% (VLV) or 95% (CHV)
5989 * of the lower bin and adjust if needed.
e37c67a1
VS
5990 *
5991 * We seem to get an unstable or solid color picture at 200MHz.
5992 * Not sure what's wrong. For now use 200MHz only when all pipes
5993 * are off.
30a970c6 5994 */
6cca3195
VS
5995 if (!IS_CHERRYVIEW(dev_priv) &&
5996 max_pixclk > freq_320*limit/100)
dfcab17e 5997 return 400000;
6cca3195 5998 else if (max_pixclk > 266667*limit/100)
29dc7ef3 5999 return freq_320;
e37c67a1 6000 else if (max_pixclk > 0)
dfcab17e 6001 return 266667;
e37c67a1
VS
6002 else
6003 return 200000;
30a970c6
JB
6004}
6005
324513c0 6006static int bxt_calc_cdclk(int max_pixclk)
f8437dd1 6007{
760e1477 6008 if (max_pixclk > 576000)
f8437dd1 6009 return 624000;
760e1477 6010 else if (max_pixclk > 384000)
f8437dd1 6011 return 576000;
760e1477 6012 else if (max_pixclk > 288000)
f8437dd1 6013 return 384000;
760e1477 6014 else if (max_pixclk > 144000)
f8437dd1
VK
6015 return 288000;
6016 else
6017 return 144000;
6018}
6019
e8788cbc 6020/* Compute the max pixel clock for new configuration. */
a821fc46
ACO
6021static int intel_mode_max_pixclk(struct drm_device *dev,
6022 struct drm_atomic_state *state)
30a970c6 6023{
565602d7
ML
6024 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
6025 struct drm_i915_private *dev_priv = dev->dev_private;
6026 struct drm_crtc *crtc;
6027 struct drm_crtc_state *crtc_state;
6028 unsigned max_pixclk = 0, i;
6029 enum pipe pipe;
30a970c6 6030
565602d7
ML
6031 memcpy(intel_state->min_pixclk, dev_priv->min_pixclk,
6032 sizeof(intel_state->min_pixclk));
304603f4 6033
565602d7
ML
6034 for_each_crtc_in_state(state, crtc, crtc_state, i) {
6035 int pixclk = 0;
6036
6037 if (crtc_state->enable)
6038 pixclk = crtc_state->adjusted_mode.crtc_clock;
304603f4 6039
565602d7 6040 intel_state->min_pixclk[i] = pixclk;
30a970c6
JB
6041 }
6042
565602d7
ML
6043 for_each_pipe(dev_priv, pipe)
6044 max_pixclk = max(intel_state->min_pixclk[pipe], max_pixclk);
6045
30a970c6
JB
6046 return max_pixclk;
6047}
6048
27c329ed 6049static int valleyview_modeset_calc_cdclk(struct drm_atomic_state *state)
30a970c6 6050{
27c329ed
ML
6051 struct drm_device *dev = state->dev;
6052 struct drm_i915_private *dev_priv = dev->dev_private;
6053 int max_pixclk = intel_mode_max_pixclk(dev, state);
1a617b77
ML
6054 struct intel_atomic_state *intel_state =
6055 to_intel_atomic_state(state);
30a970c6 6056
1a617b77 6057 intel_state->cdclk = intel_state->dev_cdclk =
27c329ed 6058 valleyview_calc_cdclk(dev_priv, max_pixclk);
0a9ab303 6059
1a617b77
ML
6060 if (!intel_state->active_crtcs)
6061 intel_state->dev_cdclk = valleyview_calc_cdclk(dev_priv, 0);
6062
27c329ed
ML
6063 return 0;
6064}
304603f4 6065
324513c0 6066static int bxt_modeset_calc_cdclk(struct drm_atomic_state *state)
27c329ed 6067{
4e5ca60f 6068 int max_pixclk = ilk_max_pixel_rate(state);
1a617b77
ML
6069 struct intel_atomic_state *intel_state =
6070 to_intel_atomic_state(state);
85a96e7a 6071
1a617b77 6072 intel_state->cdclk = intel_state->dev_cdclk =
324513c0 6073 bxt_calc_cdclk(max_pixclk);
85a96e7a 6074
1a617b77 6075 if (!intel_state->active_crtcs)
324513c0 6076 intel_state->dev_cdclk = bxt_calc_cdclk(0);
1a617b77 6077
27c329ed 6078 return 0;
30a970c6
JB
6079}
6080
1e69cd74
VS
6081static void vlv_program_pfi_credits(struct drm_i915_private *dev_priv)
6082{
6083 unsigned int credits, default_credits;
6084
6085 if (IS_CHERRYVIEW(dev_priv))
6086 default_credits = PFI_CREDIT(12);
6087 else
6088 default_credits = PFI_CREDIT(8);
6089
bfa7df01 6090 if (dev_priv->cdclk_freq >= dev_priv->czclk_freq) {
1e69cd74
VS
6091 /* CHV suggested value is 31 or 63 */
6092 if (IS_CHERRYVIEW(dev_priv))
fcc0008f 6093 credits = PFI_CREDIT_63;
1e69cd74
VS
6094 else
6095 credits = PFI_CREDIT(15);
6096 } else {
6097 credits = default_credits;
6098 }
6099
6100 /*
6101 * WA - write default credits before re-programming
6102 * FIXME: should we also set the resend bit here?
6103 */
6104 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
6105 default_credits);
6106
6107 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
6108 credits | PFI_CREDIT_RESEND);
6109
6110 /*
6111 * FIXME is this guaranteed to clear
6112 * immediately or should we poll for it?
6113 */
6114 WARN_ON(I915_READ(GCI_CONTROL) & PFI_CREDIT_RESEND);
6115}
6116
27c329ed 6117static void valleyview_modeset_commit_cdclk(struct drm_atomic_state *old_state)
30a970c6 6118{
a821fc46 6119 struct drm_device *dev = old_state->dev;
30a970c6 6120 struct drm_i915_private *dev_priv = dev->dev_private;
1a617b77
ML
6121 struct intel_atomic_state *old_intel_state =
6122 to_intel_atomic_state(old_state);
6123 unsigned req_cdclk = old_intel_state->dev_cdclk;
30a970c6 6124
27c329ed
ML
6125 /*
6126 * FIXME: We can end up here with all power domains off, yet
6127 * with a CDCLK frequency other than the minimum. To account
6128 * for this take the PIPE-A power domain, which covers the HW
6129 * blocks needed for the following programming. This can be
6130 * removed once it's guaranteed that we get here either with
6131 * the minimum CDCLK set, or the required power domains
6132 * enabled.
6133 */
6134 intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
738c05c0 6135
27c329ed
ML
6136 if (IS_CHERRYVIEW(dev))
6137 cherryview_set_cdclk(dev, req_cdclk);
6138 else
6139 valleyview_set_cdclk(dev, req_cdclk);
738c05c0 6140
27c329ed 6141 vlv_program_pfi_credits(dev_priv);
1e69cd74 6142
27c329ed 6143 intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
30a970c6
JB
6144}
6145
89b667f8
JB
6146static void valleyview_crtc_enable(struct drm_crtc *crtc)
6147{
6148 struct drm_device *dev = crtc->dev;
a72e4c9f 6149 struct drm_i915_private *dev_priv = to_i915(dev);
89b667f8
JB
6150 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6151 struct intel_encoder *encoder;
b95c5321
ML
6152 struct intel_crtc_state *pipe_config =
6153 to_intel_crtc_state(crtc->state);
89b667f8 6154 int pipe = intel_crtc->pipe;
89b667f8 6155
53d9f4e9 6156 if (WARN_ON(intel_crtc->active))
89b667f8
JB
6157 return;
6158
6e3c9717 6159 if (intel_crtc->config->has_dp_encoder)
fe3cd48d 6160 intel_dp_set_m_n(intel_crtc, M1_N1);
5b18e57c
DV
6161
6162 intel_set_pipe_timings(intel_crtc);
bc58be60 6163 intel_set_pipe_src_size(intel_crtc);
5b18e57c 6164
c14b0485
VS
6165 if (IS_CHERRYVIEW(dev) && pipe == PIPE_B) {
6166 struct drm_i915_private *dev_priv = dev->dev_private;
6167
6168 I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
6169 I915_WRITE(CHV_CANVAS(pipe), 0);
6170 }
6171
5b18e57c
DV
6172 i9xx_set_pipeconf(intel_crtc);
6173
89b667f8 6174 intel_crtc->active = true;
89b667f8 6175
a72e4c9f 6176 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4a3436e8 6177
89b667f8
JB
6178 for_each_encoder_on_crtc(dev, crtc, encoder)
6179 if (encoder->pre_pll_enable)
6180 encoder->pre_pll_enable(encoder);
6181
cd2d34d9
VS
6182 if (IS_CHERRYVIEW(dev)) {
6183 chv_prepare_pll(intel_crtc, intel_crtc->config);
6184 chv_enable_pll(intel_crtc, intel_crtc->config);
6185 } else {
6186 vlv_prepare_pll(intel_crtc, intel_crtc->config);
6187 vlv_enable_pll(intel_crtc, intel_crtc->config);
9d556c99 6188 }
89b667f8
JB
6189
6190 for_each_encoder_on_crtc(dev, crtc, encoder)
6191 if (encoder->pre_enable)
6192 encoder->pre_enable(encoder);
6193
2dd24552
JB
6194 i9xx_pfit_enable(intel_crtc);
6195
b95c5321 6196 intel_color_load_luts(&pipe_config->base);
63cbb074 6197
caed361d 6198 intel_update_watermarks(crtc);
e1fdc473 6199 intel_enable_pipe(intel_crtc);
be6a6f8e 6200
4b3a9526
VS
6201 assert_vblank_disabled(crtc);
6202 drm_crtc_vblank_on(crtc);
6203
f9b61ff6
DV
6204 for_each_encoder_on_crtc(dev, crtc, encoder)
6205 encoder->enable(encoder);
89b667f8
JB
6206}
6207
f13c2ef3
DV
6208static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
6209{
6210 struct drm_device *dev = crtc->base.dev;
6211 struct drm_i915_private *dev_priv = dev->dev_private;
6212
6e3c9717
ACO
6213 I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
6214 I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
f13c2ef3
DV
6215}
6216
0b8765c6 6217static void i9xx_crtc_enable(struct drm_crtc *crtc)
79e53945
JB
6218{
6219 struct drm_device *dev = crtc->dev;
a72e4c9f 6220 struct drm_i915_private *dev_priv = to_i915(dev);
79e53945 6221 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 6222 struct intel_encoder *encoder;
b95c5321
ML
6223 struct intel_crtc_state *pipe_config =
6224 to_intel_crtc_state(crtc->state);
cd2d34d9 6225 enum pipe pipe = intel_crtc->pipe;
79e53945 6226
53d9f4e9 6227 if (WARN_ON(intel_crtc->active))
f7abfe8b
CW
6228 return;
6229
f13c2ef3
DV
6230 i9xx_set_pll_dividers(intel_crtc);
6231
6e3c9717 6232 if (intel_crtc->config->has_dp_encoder)
fe3cd48d 6233 intel_dp_set_m_n(intel_crtc, M1_N1);
5b18e57c
DV
6234
6235 intel_set_pipe_timings(intel_crtc);
bc58be60 6236 intel_set_pipe_src_size(intel_crtc);
5b18e57c 6237
5b18e57c
DV
6238 i9xx_set_pipeconf(intel_crtc);
6239
f7abfe8b 6240 intel_crtc->active = true;
6b383a7f 6241
4a3436e8 6242 if (!IS_GEN2(dev))
a72e4c9f 6243 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4a3436e8 6244
9d6d9f19
MK
6245 for_each_encoder_on_crtc(dev, crtc, encoder)
6246 if (encoder->pre_enable)
6247 encoder->pre_enable(encoder);
6248
f6736a1a
DV
6249 i9xx_enable_pll(intel_crtc);
6250
2dd24552
JB
6251 i9xx_pfit_enable(intel_crtc);
6252
b95c5321 6253 intel_color_load_luts(&pipe_config->base);
63cbb074 6254
f37fcc2a 6255 intel_update_watermarks(crtc);
e1fdc473 6256 intel_enable_pipe(intel_crtc);
be6a6f8e 6257
4b3a9526
VS
6258 assert_vblank_disabled(crtc);
6259 drm_crtc_vblank_on(crtc);
6260
f9b61ff6
DV
6261 for_each_encoder_on_crtc(dev, crtc, encoder)
6262 encoder->enable(encoder);
0b8765c6 6263}
79e53945 6264
87476d63
DV
6265static void i9xx_pfit_disable(struct intel_crtc *crtc)
6266{
6267 struct drm_device *dev = crtc->base.dev;
6268 struct drm_i915_private *dev_priv = dev->dev_private;
87476d63 6269
6e3c9717 6270 if (!crtc->config->gmch_pfit.control)
328d8e82 6271 return;
87476d63 6272
328d8e82 6273 assert_pipe_disabled(dev_priv, crtc->pipe);
87476d63 6274
328d8e82
DV
6275 DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
6276 I915_READ(PFIT_CONTROL));
6277 I915_WRITE(PFIT_CONTROL, 0);
87476d63
DV
6278}
6279
0b8765c6
JB
6280static void i9xx_crtc_disable(struct drm_crtc *crtc)
6281{
6282 struct drm_device *dev = crtc->dev;
6283 struct drm_i915_private *dev_priv = dev->dev_private;
6284 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 6285 struct intel_encoder *encoder;
0b8765c6 6286 int pipe = intel_crtc->pipe;
ef9c3aee 6287
6304cd91
VS
6288 /*
6289 * On gen2 planes are double buffered but the pipe isn't, so we must
6290 * wait for planes to fully turn off before disabling the pipe.
6291 */
90e83e53
ACO
6292 if (IS_GEN2(dev))
6293 intel_wait_for_vblank(dev, pipe);
6304cd91 6294
4b3a9526
VS
6295 for_each_encoder_on_crtc(dev, crtc, encoder)
6296 encoder->disable(encoder);
6297
f9b61ff6
DV
6298 drm_crtc_vblank_off(crtc);
6299 assert_vblank_disabled(crtc);
6300
575f7ab7 6301 intel_disable_pipe(intel_crtc);
24a1f16d 6302
87476d63 6303 i9xx_pfit_disable(intel_crtc);
24a1f16d 6304
89b667f8
JB
6305 for_each_encoder_on_crtc(dev, crtc, encoder)
6306 if (encoder->post_disable)
6307 encoder->post_disable(encoder);
6308
a65347ba 6309 if (!intel_crtc->config->has_dsi_encoder) {
076ed3b2
CML
6310 if (IS_CHERRYVIEW(dev))
6311 chv_disable_pll(dev_priv, pipe);
6312 else if (IS_VALLEYVIEW(dev))
6313 vlv_disable_pll(dev_priv, pipe);
6314 else
1c4e0274 6315 i9xx_disable_pll(intel_crtc);
076ed3b2 6316 }
0b8765c6 6317
d6db995f
VS
6318 for_each_encoder_on_crtc(dev, crtc, encoder)
6319 if (encoder->post_pll_disable)
6320 encoder->post_pll_disable(encoder);
6321
4a3436e8 6322 if (!IS_GEN2(dev))
a72e4c9f 6323 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
0b8765c6
JB
6324}
6325
b17d48e2
ML
6326static void intel_crtc_disable_noatomic(struct drm_crtc *crtc)
6327{
842e0307 6328 struct intel_encoder *encoder;
b17d48e2
ML
6329 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6330 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
6331 enum intel_display_power_domain domain;
6332 unsigned long domains;
6333
6334 if (!intel_crtc->active)
6335 return;
6336
a539205a 6337 if (to_intel_plane_state(crtc->primary->state)->visible) {
5a21b665 6338 WARN_ON(intel_crtc->flip_work);
fc32b1fd 6339
2622a081 6340 intel_pre_disable_primary_noatomic(crtc);
54a41961
ML
6341
6342 intel_crtc_disable_planes(crtc, 1 << drm_plane_index(crtc->primary));
6343 to_intel_plane_state(crtc->primary->state)->visible = false;
a539205a
ML
6344 }
6345
b17d48e2 6346 dev_priv->display.crtc_disable(crtc);
842e0307 6347
78108b7c
VS
6348 DRM_DEBUG_KMS("[CRTC:%d:%s] hw state adjusted, was enabled, now disabled\n",
6349 crtc->base.id, crtc->name);
842e0307
ML
6350
6351 WARN_ON(drm_atomic_set_mode_for_crtc(crtc->state, NULL) < 0);
6352 crtc->state->active = false;
37d9078b 6353 intel_crtc->active = false;
842e0307
ML
6354 crtc->enabled = false;
6355 crtc->state->connector_mask = 0;
6356 crtc->state->encoder_mask = 0;
6357
6358 for_each_encoder_on_crtc(crtc->dev, crtc, encoder)
6359 encoder->base.crtc = NULL;
6360
58f9c0bc 6361 intel_fbc_disable(intel_crtc);
37d9078b 6362 intel_update_watermarks(crtc);
1f7457b1 6363 intel_disable_shared_dpll(intel_crtc);
b17d48e2
ML
6364
6365 domains = intel_crtc->enabled_power_domains;
6366 for_each_power_domain(domain, domains)
6367 intel_display_power_put(dev_priv, domain);
6368 intel_crtc->enabled_power_domains = 0;
565602d7
ML
6369
6370 dev_priv->active_crtcs &= ~(1 << intel_crtc->pipe);
6371 dev_priv->min_pixclk[intel_crtc->pipe] = 0;
b17d48e2
ML
6372}
6373
6b72d486
ML
6374/*
6375 * turn all crtc's off, but do not adjust state
6376 * This has to be paired with a call to intel_modeset_setup_hw_state.
6377 */
70e0bd74 6378int intel_display_suspend(struct drm_device *dev)
ee7b9f93 6379{
e2c8b870 6380 struct drm_i915_private *dev_priv = to_i915(dev);
70e0bd74 6381 struct drm_atomic_state *state;
e2c8b870 6382 int ret;
70e0bd74 6383
e2c8b870
ML
6384 state = drm_atomic_helper_suspend(dev);
6385 ret = PTR_ERR_OR_ZERO(state);
70e0bd74
ML
6386 if (ret)
6387 DRM_ERROR("Suspending crtc's failed with %i\n", ret);
e2c8b870
ML
6388 else
6389 dev_priv->modeset_restore_state = state;
70e0bd74 6390 return ret;
ee7b9f93
JB
6391}
6392
ea5b213a 6393void intel_encoder_destroy(struct drm_encoder *encoder)
7e7d76c3 6394{
4ef69c7a 6395 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
ea5b213a 6396
ea5b213a
CW
6397 drm_encoder_cleanup(encoder);
6398 kfree(intel_encoder);
7e7d76c3
JB
6399}
6400
0a91ca29
DV
6401/* Cross check the actual hw state with our own modeset state tracking (and it's
6402 * internal consistency). */
5a21b665 6403static void intel_connector_verify_state(struct intel_connector *connector)
79e53945 6404{
5a21b665 6405 struct drm_crtc *crtc = connector->base.state->crtc;
35dd3c64
ML
6406
6407 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
6408 connector->base.base.id,
6409 connector->base.name);
6410
0a91ca29 6411 if (connector->get_hw_state(connector)) {
e85376cb 6412 struct intel_encoder *encoder = connector->encoder;
5a21b665 6413 struct drm_connector_state *conn_state = connector->base.state;
0a91ca29 6414
35dd3c64
ML
6415 I915_STATE_WARN(!crtc,
6416 "connector enabled without attached crtc\n");
0a91ca29 6417
35dd3c64
ML
6418 if (!crtc)
6419 return;
6420
6421 I915_STATE_WARN(!crtc->state->active,
6422 "connector is active, but attached crtc isn't\n");
6423
e85376cb 6424 if (!encoder || encoder->type == INTEL_OUTPUT_DP_MST)
35dd3c64
ML
6425 return;
6426
e85376cb 6427 I915_STATE_WARN(conn_state->best_encoder != &encoder->base,
35dd3c64
ML
6428 "atomic encoder doesn't match attached encoder\n");
6429
e85376cb 6430 I915_STATE_WARN(conn_state->crtc != encoder->base.crtc,
35dd3c64
ML
6431 "attached encoder crtc differs from connector crtc\n");
6432 } else {
4d688a2a
ML
6433 I915_STATE_WARN(crtc && crtc->state->active,
6434 "attached crtc is active, but connector isn't\n");
5a21b665 6435 I915_STATE_WARN(!crtc && connector->base.state->best_encoder,
35dd3c64 6436 "best encoder set without crtc!\n");
0a91ca29 6437 }
79e53945
JB
6438}
6439
08d9bc92
ACO
6440int intel_connector_init(struct intel_connector *connector)
6441{
5350a031 6442 drm_atomic_helper_connector_reset(&connector->base);
08d9bc92 6443
5350a031 6444 if (!connector->base.state)
08d9bc92
ACO
6445 return -ENOMEM;
6446
08d9bc92
ACO
6447 return 0;
6448}
6449
6450struct intel_connector *intel_connector_alloc(void)
6451{
6452 struct intel_connector *connector;
6453
6454 connector = kzalloc(sizeof *connector, GFP_KERNEL);
6455 if (!connector)
6456 return NULL;
6457
6458 if (intel_connector_init(connector) < 0) {
6459 kfree(connector);
6460 return NULL;
6461 }
6462
6463 return connector;
6464}
6465
f0947c37
DV
6466/* Simple connector->get_hw_state implementation for encoders that support only
6467 * one connector and no cloning and hence the encoder state determines the state
6468 * of the connector. */
6469bool intel_connector_get_hw_state(struct intel_connector *connector)
ea5b213a 6470{
24929352 6471 enum pipe pipe = 0;
f0947c37 6472 struct intel_encoder *encoder = connector->encoder;
ea5b213a 6473
f0947c37 6474 return encoder->get_hw_state(encoder, &pipe);
ea5b213a
CW
6475}
6476
6d293983 6477static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
d272ddfa 6478{
6d293983
ACO
6479 if (crtc_state->base.enable && crtc_state->has_pch_encoder)
6480 return crtc_state->fdi_lanes;
d272ddfa
VS
6481
6482 return 0;
6483}
6484
6d293983 6485static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
5cec258b 6486 struct intel_crtc_state *pipe_config)
1857e1da 6487{
6d293983
ACO
6488 struct drm_atomic_state *state = pipe_config->base.state;
6489 struct intel_crtc *other_crtc;
6490 struct intel_crtc_state *other_crtc_state;
6491
1857e1da
DV
6492 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
6493 pipe_name(pipe), pipe_config->fdi_lanes);
6494 if (pipe_config->fdi_lanes > 4) {
6495 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
6496 pipe_name(pipe), pipe_config->fdi_lanes);
6d293983 6497 return -EINVAL;
1857e1da
DV
6498 }
6499
bafb6553 6500 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
1857e1da
DV
6501 if (pipe_config->fdi_lanes > 2) {
6502 DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
6503 pipe_config->fdi_lanes);
6d293983 6504 return -EINVAL;
1857e1da 6505 } else {
6d293983 6506 return 0;
1857e1da
DV
6507 }
6508 }
6509
6510 if (INTEL_INFO(dev)->num_pipes == 2)
6d293983 6511 return 0;
1857e1da
DV
6512
6513 /* Ivybridge 3 pipe is really complicated */
6514 switch (pipe) {
6515 case PIPE_A:
6d293983 6516 return 0;
1857e1da 6517 case PIPE_B:
6d293983
ACO
6518 if (pipe_config->fdi_lanes <= 2)
6519 return 0;
6520
6521 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_C));
6522 other_crtc_state =
6523 intel_atomic_get_crtc_state(state, other_crtc);
6524 if (IS_ERR(other_crtc_state))
6525 return PTR_ERR(other_crtc_state);
6526
6527 if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
1857e1da
DV
6528 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
6529 pipe_name(pipe), pipe_config->fdi_lanes);
6d293983 6530 return -EINVAL;
1857e1da 6531 }
6d293983 6532 return 0;
1857e1da 6533 case PIPE_C:
251cc67c
VS
6534 if (pipe_config->fdi_lanes > 2) {
6535 DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
6536 pipe_name(pipe), pipe_config->fdi_lanes);
6d293983 6537 return -EINVAL;
251cc67c 6538 }
6d293983
ACO
6539
6540 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_B));
6541 other_crtc_state =
6542 intel_atomic_get_crtc_state(state, other_crtc);
6543 if (IS_ERR(other_crtc_state))
6544 return PTR_ERR(other_crtc_state);
6545
6546 if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
1857e1da 6547 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
6d293983 6548 return -EINVAL;
1857e1da 6549 }
6d293983 6550 return 0;
1857e1da
DV
6551 default:
6552 BUG();
6553 }
6554}
6555
e29c22c0
DV
6556#define RETRY 1
6557static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
5cec258b 6558 struct intel_crtc_state *pipe_config)
877d48d5 6559{
1857e1da 6560 struct drm_device *dev = intel_crtc->base.dev;
7c5f93b0 6561 const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
6d293983
ACO
6562 int lane, link_bw, fdi_dotclock, ret;
6563 bool needs_recompute = false;
877d48d5 6564
e29c22c0 6565retry:
877d48d5
DV
6566 /* FDI is a binary signal running at ~2.7GHz, encoding
6567 * each output octet as 10 bits. The actual frequency
6568 * is stored as a divider into a 100MHz clock, and the
6569 * mode pixel clock is stored in units of 1KHz.
6570 * Hence the bw of each lane in terms of the mode signal
6571 * is:
6572 */
21a727b3 6573 link_bw = intel_fdi_link_freq(to_i915(dev), pipe_config);
877d48d5 6574
241bfc38 6575 fdi_dotclock = adjusted_mode->crtc_clock;
877d48d5 6576
2bd89a07 6577 lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
877d48d5
DV
6578 pipe_config->pipe_bpp);
6579
6580 pipe_config->fdi_lanes = lane;
6581
2bd89a07 6582 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
877d48d5 6583 link_bw, &pipe_config->fdi_m_n);
1857e1da 6584
e3b247da 6585 ret = ironlake_check_fdi_lanes(dev, intel_crtc->pipe, pipe_config);
6d293983 6586 if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
e29c22c0
DV
6587 pipe_config->pipe_bpp -= 2*3;
6588 DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
6589 pipe_config->pipe_bpp);
6590 needs_recompute = true;
6591 pipe_config->bw_constrained = true;
6592
6593 goto retry;
6594 }
6595
6596 if (needs_recompute)
6597 return RETRY;
6598
6d293983 6599 return ret;
877d48d5
DV
6600}
6601
8cfb3407
VS
6602static bool pipe_config_supports_ips(struct drm_i915_private *dev_priv,
6603 struct intel_crtc_state *pipe_config)
6604{
6605 if (pipe_config->pipe_bpp > 24)
6606 return false;
6607
6608 /* HSW can handle pixel rate up to cdclk? */
2d1fe073 6609 if (IS_HASWELL(dev_priv))
8cfb3407
VS
6610 return true;
6611
6612 /*
b432e5cf
VS
6613 * We compare against max which means we must take
6614 * the increased cdclk requirement into account when
6615 * calculating the new cdclk.
6616 *
6617 * Should measure whether using a lower cdclk w/o IPS
8cfb3407
VS
6618 */
6619 return ilk_pipe_pixel_rate(pipe_config) <=
6620 dev_priv->max_cdclk_freq * 95 / 100;
6621}
6622
42db64ef 6623static void hsw_compute_ips_config(struct intel_crtc *crtc,
5cec258b 6624 struct intel_crtc_state *pipe_config)
42db64ef 6625{
8cfb3407
VS
6626 struct drm_device *dev = crtc->base.dev;
6627 struct drm_i915_private *dev_priv = dev->dev_private;
6628
d330a953 6629 pipe_config->ips_enabled = i915.enable_ips &&
8cfb3407
VS
6630 hsw_crtc_supports_ips(crtc) &&
6631 pipe_config_supports_ips(dev_priv, pipe_config);
42db64ef
PZ
6632}
6633
39acb4aa
VS
6634static bool intel_crtc_supports_double_wide(const struct intel_crtc *crtc)
6635{
6636 const struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6637
6638 /* GDG double wide on either pipe, otherwise pipe A only */
6639 return INTEL_INFO(dev_priv)->gen < 4 &&
6640 (crtc->pipe == PIPE_A || IS_I915G(dev_priv));
6641}
6642
a43f6e0f 6643static int intel_crtc_compute_config(struct intel_crtc *crtc,
5cec258b 6644 struct intel_crtc_state *pipe_config)
79e53945 6645{
a43f6e0f 6646 struct drm_device *dev = crtc->base.dev;
8bd31e67 6647 struct drm_i915_private *dev_priv = dev->dev_private;
7c5f93b0 6648 const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
f3261156 6649 int clock_limit = dev_priv->max_dotclk_freq;
89749350 6650
cf532bb2 6651 if (INTEL_INFO(dev)->gen < 4) {
f3261156 6652 clock_limit = dev_priv->max_cdclk_freq * 9 / 10;
cf532bb2
VS
6653
6654 /*
39acb4aa 6655 * Enable double wide mode when the dot clock
cf532bb2 6656 * is > 90% of the (display) core speed.
cf532bb2 6657 */
39acb4aa
VS
6658 if (intel_crtc_supports_double_wide(crtc) &&
6659 adjusted_mode->crtc_clock > clock_limit) {
f3261156 6660 clock_limit = dev_priv->max_dotclk_freq;
cf532bb2 6661 pipe_config->double_wide = true;
ad3a4479 6662 }
f3261156 6663 }
ad3a4479 6664
f3261156
VS
6665 if (adjusted_mode->crtc_clock > clock_limit) {
6666 DRM_DEBUG_KMS("requested pixel clock (%d kHz) too high (max: %d kHz, double wide: %s)\n",
6667 adjusted_mode->crtc_clock, clock_limit,
6668 yesno(pipe_config->double_wide));
6669 return -EINVAL;
2c07245f 6670 }
89749350 6671
1d1d0e27
VS
6672 /*
6673 * Pipe horizontal size must be even in:
6674 * - DVO ganged mode
6675 * - LVDS dual channel mode
6676 * - Double wide pipe
6677 */
a93e255f 6678 if ((intel_pipe_will_have_type(pipe_config, INTEL_OUTPUT_LVDS) &&
1d1d0e27
VS
6679 intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
6680 pipe_config->pipe_src_w &= ~1;
6681
8693a824
DL
6682 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
6683 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
44f46b42
CW
6684 */
6685 if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
aad941d5 6686 adjusted_mode->crtc_hsync_start == adjusted_mode->crtc_hdisplay)
e29c22c0 6687 return -EINVAL;
44f46b42 6688
f5adf94e 6689 if (HAS_IPS(dev))
a43f6e0f
DV
6690 hsw_compute_ips_config(crtc, pipe_config);
6691
877d48d5 6692 if (pipe_config->has_pch_encoder)
a43f6e0f 6693 return ironlake_fdi_compute_config(crtc, pipe_config);
877d48d5 6694
cf5a15be 6695 return 0;
79e53945
JB
6696}
6697
1652d19e
VS
6698static int skylake_get_display_clock_speed(struct drm_device *dev)
6699{
6700 struct drm_i915_private *dev_priv = to_i915(dev);
ea61791e 6701 uint32_t cdctl;
1652d19e 6702
ea61791e 6703 skl_dpll0_update(dev_priv);
1652d19e 6704
63911d72 6705 if (dev_priv->cdclk_pll.vco == 0)
709e05c3 6706 return dev_priv->cdclk_pll.ref;
1652d19e 6707
ea61791e 6708 cdctl = I915_READ(CDCLK_CTL);
1652d19e 6709
63911d72 6710 if (dev_priv->cdclk_pll.vco == 8640000) {
1652d19e
VS
6711 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6712 case CDCLK_FREQ_450_432:
6713 return 432000;
6714 case CDCLK_FREQ_337_308:
487ed2e4 6715 return 308571;
ea61791e
VS
6716 case CDCLK_FREQ_540:
6717 return 540000;
1652d19e 6718 case CDCLK_FREQ_675_617:
487ed2e4 6719 return 617143;
1652d19e 6720 default:
ea61791e 6721 MISSING_CASE(cdctl & CDCLK_FREQ_SEL_MASK);
1652d19e
VS
6722 }
6723 } else {
1652d19e
VS
6724 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6725 case CDCLK_FREQ_450_432:
6726 return 450000;
6727 case CDCLK_FREQ_337_308:
6728 return 337500;
ea61791e
VS
6729 case CDCLK_FREQ_540:
6730 return 540000;
1652d19e
VS
6731 case CDCLK_FREQ_675_617:
6732 return 675000;
6733 default:
ea61791e 6734 MISSING_CASE(cdctl & CDCLK_FREQ_SEL_MASK);
1652d19e
VS
6735 }
6736 }
6737
709e05c3 6738 return dev_priv->cdclk_pll.ref;
1652d19e
VS
6739}
6740
83d7c81f
VS
6741static void bxt_de_pll_update(struct drm_i915_private *dev_priv)
6742{
6743 u32 val;
6744
6745 dev_priv->cdclk_pll.ref = 19200;
1c3f7700 6746 dev_priv->cdclk_pll.vco = 0;
83d7c81f
VS
6747
6748 val = I915_READ(BXT_DE_PLL_ENABLE);
1c3f7700 6749 if ((val & BXT_DE_PLL_PLL_ENABLE) == 0)
83d7c81f 6750 return;
83d7c81f 6751
1c3f7700
ID
6752 if (WARN_ON((val & BXT_DE_PLL_LOCK) == 0))
6753 return;
83d7c81f
VS
6754
6755 val = I915_READ(BXT_DE_PLL_CTL);
6756 dev_priv->cdclk_pll.vco = (val & BXT_DE_PLL_RATIO_MASK) *
6757 dev_priv->cdclk_pll.ref;
6758}
6759
acd3f3d3
BP
6760static int broxton_get_display_clock_speed(struct drm_device *dev)
6761{
6762 struct drm_i915_private *dev_priv = to_i915(dev);
f5986242
VS
6763 u32 divider;
6764 int div, vco;
acd3f3d3 6765
83d7c81f
VS
6766 bxt_de_pll_update(dev_priv);
6767
f5986242
VS
6768 vco = dev_priv->cdclk_pll.vco;
6769 if (vco == 0)
6770 return dev_priv->cdclk_pll.ref;
acd3f3d3 6771
f5986242 6772 divider = I915_READ(CDCLK_CTL) & BXT_CDCLK_CD2X_DIV_SEL_MASK;
acd3f3d3 6773
f5986242 6774 switch (divider) {
acd3f3d3 6775 case BXT_CDCLK_CD2X_DIV_SEL_1:
f5986242
VS
6776 div = 2;
6777 break;
acd3f3d3 6778 case BXT_CDCLK_CD2X_DIV_SEL_1_5:
f5986242
VS
6779 div = 3;
6780 break;
acd3f3d3 6781 case BXT_CDCLK_CD2X_DIV_SEL_2:
f5986242
VS
6782 div = 4;
6783 break;
acd3f3d3 6784 case BXT_CDCLK_CD2X_DIV_SEL_4:
f5986242
VS
6785 div = 8;
6786 break;
6787 default:
6788 MISSING_CASE(divider);
6789 return dev_priv->cdclk_pll.ref;
acd3f3d3
BP
6790 }
6791
f5986242 6792 return DIV_ROUND_CLOSEST(vco, div);
acd3f3d3
BP
6793}
6794
1652d19e
VS
6795static int broadwell_get_display_clock_speed(struct drm_device *dev)
6796{
6797 struct drm_i915_private *dev_priv = dev->dev_private;
6798 uint32_t lcpll = I915_READ(LCPLL_CTL);
6799 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6800
6801 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6802 return 800000;
6803 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6804 return 450000;
6805 else if (freq == LCPLL_CLK_FREQ_450)
6806 return 450000;
6807 else if (freq == LCPLL_CLK_FREQ_54O_BDW)
6808 return 540000;
6809 else if (freq == LCPLL_CLK_FREQ_337_5_BDW)
6810 return 337500;
6811 else
6812 return 675000;
6813}
6814
6815static int haswell_get_display_clock_speed(struct drm_device *dev)
6816{
6817 struct drm_i915_private *dev_priv = dev->dev_private;
6818 uint32_t lcpll = I915_READ(LCPLL_CTL);
6819 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6820
6821 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6822 return 800000;
6823 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6824 return 450000;
6825 else if (freq == LCPLL_CLK_FREQ_450)
6826 return 450000;
6827 else if (IS_HSW_ULT(dev))
6828 return 337500;
6829 else
6830 return 540000;
79e53945
JB
6831}
6832
25eb05fc
JB
6833static int valleyview_get_display_clock_speed(struct drm_device *dev)
6834{
bfa7df01
VS
6835 return vlv_get_cck_clock_hpll(to_i915(dev), "cdclk",
6836 CCK_DISPLAY_CLOCK_CONTROL);
25eb05fc
JB
6837}
6838
b37a6434
VS
6839static int ilk_get_display_clock_speed(struct drm_device *dev)
6840{
6841 return 450000;
6842}
6843
e70236a8
JB
6844static int i945_get_display_clock_speed(struct drm_device *dev)
6845{
6846 return 400000;
6847}
79e53945 6848
e70236a8 6849static int i915_get_display_clock_speed(struct drm_device *dev)
79e53945 6850{
e907f170 6851 return 333333;
e70236a8 6852}
79e53945 6853
e70236a8
JB
6854static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
6855{
6856 return 200000;
6857}
79e53945 6858
257a7ffc
DV
6859static int pnv_get_display_clock_speed(struct drm_device *dev)
6860{
6861 u16 gcfgc = 0;
6862
6863 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6864
6865 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6866 case GC_DISPLAY_CLOCK_267_MHZ_PNV:
e907f170 6867 return 266667;
257a7ffc 6868 case GC_DISPLAY_CLOCK_333_MHZ_PNV:
e907f170 6869 return 333333;
257a7ffc 6870 case GC_DISPLAY_CLOCK_444_MHZ_PNV:
e907f170 6871 return 444444;
257a7ffc
DV
6872 case GC_DISPLAY_CLOCK_200_MHZ_PNV:
6873 return 200000;
6874 default:
6875 DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
6876 case GC_DISPLAY_CLOCK_133_MHZ_PNV:
e907f170 6877 return 133333;
257a7ffc 6878 case GC_DISPLAY_CLOCK_167_MHZ_PNV:
e907f170 6879 return 166667;
257a7ffc
DV
6880 }
6881}
6882
e70236a8
JB
6883static int i915gm_get_display_clock_speed(struct drm_device *dev)
6884{
6885 u16 gcfgc = 0;
79e53945 6886
e70236a8
JB
6887 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6888
6889 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
e907f170 6890 return 133333;
e70236a8
JB
6891 else {
6892 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6893 case GC_DISPLAY_CLOCK_333_MHZ:
e907f170 6894 return 333333;
e70236a8
JB
6895 default:
6896 case GC_DISPLAY_CLOCK_190_200_MHZ:
6897 return 190000;
79e53945 6898 }
e70236a8
JB
6899 }
6900}
6901
6902static int i865_get_display_clock_speed(struct drm_device *dev)
6903{
e907f170 6904 return 266667;
e70236a8
JB
6905}
6906
1b1d2716 6907static int i85x_get_display_clock_speed(struct drm_device *dev)
e70236a8
JB
6908{
6909 u16 hpllcc = 0;
1b1d2716 6910
65cd2b3f
VS
6911 /*
6912 * 852GM/852GMV only supports 133 MHz and the HPLLCC
6913 * encoding is different :(
6914 * FIXME is this the right way to detect 852GM/852GMV?
6915 */
6916 if (dev->pdev->revision == 0x1)
6917 return 133333;
6918
1b1d2716
VS
6919 pci_bus_read_config_word(dev->pdev->bus,
6920 PCI_DEVFN(0, 3), HPLLCC, &hpllcc);
6921
e70236a8
JB
6922 /* Assume that the hardware is in the high speed state. This
6923 * should be the default.
6924 */
6925 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
6926 case GC_CLOCK_133_200:
1b1d2716 6927 case GC_CLOCK_133_200_2:
e70236a8
JB
6928 case GC_CLOCK_100_200:
6929 return 200000;
6930 case GC_CLOCK_166_250:
6931 return 250000;
6932 case GC_CLOCK_100_133:
e907f170 6933 return 133333;
1b1d2716
VS
6934 case GC_CLOCK_133_266:
6935 case GC_CLOCK_133_266_2:
6936 case GC_CLOCK_166_266:
6937 return 266667;
e70236a8 6938 }
79e53945 6939
e70236a8
JB
6940 /* Shouldn't happen */
6941 return 0;
6942}
79e53945 6943
e70236a8
JB
6944static int i830_get_display_clock_speed(struct drm_device *dev)
6945{
e907f170 6946 return 133333;
79e53945
JB
6947}
6948
34edce2f
VS
6949static unsigned int intel_hpll_vco(struct drm_device *dev)
6950{
6951 struct drm_i915_private *dev_priv = dev->dev_private;
6952 static const unsigned int blb_vco[8] = {
6953 [0] = 3200000,
6954 [1] = 4000000,
6955 [2] = 5333333,
6956 [3] = 4800000,
6957 [4] = 6400000,
6958 };
6959 static const unsigned int pnv_vco[8] = {
6960 [0] = 3200000,
6961 [1] = 4000000,
6962 [2] = 5333333,
6963 [3] = 4800000,
6964 [4] = 2666667,
6965 };
6966 static const unsigned int cl_vco[8] = {
6967 [0] = 3200000,
6968 [1] = 4000000,
6969 [2] = 5333333,
6970 [3] = 6400000,
6971 [4] = 3333333,
6972 [5] = 3566667,
6973 [6] = 4266667,
6974 };
6975 static const unsigned int elk_vco[8] = {
6976 [0] = 3200000,
6977 [1] = 4000000,
6978 [2] = 5333333,
6979 [3] = 4800000,
6980 };
6981 static const unsigned int ctg_vco[8] = {
6982 [0] = 3200000,
6983 [1] = 4000000,
6984 [2] = 5333333,
6985 [3] = 6400000,
6986 [4] = 2666667,
6987 [5] = 4266667,
6988 };
6989 const unsigned int *vco_table;
6990 unsigned int vco;
6991 uint8_t tmp = 0;
6992
6993 /* FIXME other chipsets? */
6994 if (IS_GM45(dev))
6995 vco_table = ctg_vco;
6996 else if (IS_G4X(dev))
6997 vco_table = elk_vco;
6998 else if (IS_CRESTLINE(dev))
6999 vco_table = cl_vco;
7000 else if (IS_PINEVIEW(dev))
7001 vco_table = pnv_vco;
7002 else if (IS_G33(dev))
7003 vco_table = blb_vco;
7004 else
7005 return 0;
7006
7007 tmp = I915_READ(IS_MOBILE(dev) ? HPLLVCO_MOBILE : HPLLVCO);
7008
7009 vco = vco_table[tmp & 0x7];
7010 if (vco == 0)
7011 DRM_ERROR("Bad HPLL VCO (HPLLVCO=0x%02x)\n", tmp);
7012 else
7013 DRM_DEBUG_KMS("HPLL VCO %u kHz\n", vco);
7014
7015 return vco;
7016}
7017
7018static int gm45_get_display_clock_speed(struct drm_device *dev)
7019{
7020 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
7021 uint16_t tmp = 0;
7022
7023 pci_read_config_word(dev->pdev, GCFGC, &tmp);
7024
7025 cdclk_sel = (tmp >> 12) & 0x1;
7026
7027 switch (vco) {
7028 case 2666667:
7029 case 4000000:
7030 case 5333333:
7031 return cdclk_sel ? 333333 : 222222;
7032 case 3200000:
7033 return cdclk_sel ? 320000 : 228571;
7034 default:
7035 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u, CFGC=0x%04x\n", vco, tmp);
7036 return 222222;
7037 }
7038}
7039
7040static int i965gm_get_display_clock_speed(struct drm_device *dev)
7041{
7042 static const uint8_t div_3200[] = { 16, 10, 8 };
7043 static const uint8_t div_4000[] = { 20, 12, 10 };
7044 static const uint8_t div_5333[] = { 24, 16, 14 };
7045 const uint8_t *div_table;
7046 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
7047 uint16_t tmp = 0;
7048
7049 pci_read_config_word(dev->pdev, GCFGC, &tmp);
7050
7051 cdclk_sel = ((tmp >> 8) & 0x1f) - 1;
7052
7053 if (cdclk_sel >= ARRAY_SIZE(div_3200))
7054 goto fail;
7055
7056 switch (vco) {
7057 case 3200000:
7058 div_table = div_3200;
7059 break;
7060 case 4000000:
7061 div_table = div_4000;
7062 break;
7063 case 5333333:
7064 div_table = div_5333;
7065 break;
7066 default:
7067 goto fail;
7068 }
7069
7070 return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
7071
caf4e252 7072fail:
34edce2f
VS
7073 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%04x\n", vco, tmp);
7074 return 200000;
7075}
7076
7077static int g33_get_display_clock_speed(struct drm_device *dev)
7078{
7079 static const uint8_t div_3200[] = { 12, 10, 8, 7, 5, 16 };
7080 static const uint8_t div_4000[] = { 14, 12, 10, 8, 6, 20 };
7081 static const uint8_t div_4800[] = { 20, 14, 12, 10, 8, 24 };
7082 static const uint8_t div_5333[] = { 20, 16, 12, 12, 8, 28 };
7083 const uint8_t *div_table;
7084 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
7085 uint16_t tmp = 0;
7086
7087 pci_read_config_word(dev->pdev, GCFGC, &tmp);
7088
7089 cdclk_sel = (tmp >> 4) & 0x7;
7090
7091 if (cdclk_sel >= ARRAY_SIZE(div_3200))
7092 goto fail;
7093
7094 switch (vco) {
7095 case 3200000:
7096 div_table = div_3200;
7097 break;
7098 case 4000000:
7099 div_table = div_4000;
7100 break;
7101 case 4800000:
7102 div_table = div_4800;
7103 break;
7104 case 5333333:
7105 div_table = div_5333;
7106 break;
7107 default:
7108 goto fail;
7109 }
7110
7111 return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
7112
caf4e252 7113fail:
34edce2f
VS
7114 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%08x\n", vco, tmp);
7115 return 190476;
7116}
7117
2c07245f 7118static void
a65851af 7119intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
2c07245f 7120{
a65851af
VS
7121 while (*num > DATA_LINK_M_N_MASK ||
7122 *den > DATA_LINK_M_N_MASK) {
2c07245f
ZW
7123 *num >>= 1;
7124 *den >>= 1;
7125 }
7126}
7127
a65851af
VS
7128static void compute_m_n(unsigned int m, unsigned int n,
7129 uint32_t *ret_m, uint32_t *ret_n)
7130{
7131 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
7132 *ret_m = div_u64((uint64_t) m * *ret_n, n);
7133 intel_reduce_m_n_ratio(ret_m, ret_n);
7134}
7135
e69d0bc1
DV
7136void
7137intel_link_compute_m_n(int bits_per_pixel, int nlanes,
7138 int pixel_clock, int link_clock,
7139 struct intel_link_m_n *m_n)
2c07245f 7140{
e69d0bc1 7141 m_n->tu = 64;
a65851af
VS
7142
7143 compute_m_n(bits_per_pixel * pixel_clock,
7144 link_clock * nlanes * 8,
7145 &m_n->gmch_m, &m_n->gmch_n);
7146
7147 compute_m_n(pixel_clock, link_clock,
7148 &m_n->link_m, &m_n->link_n);
2c07245f
ZW
7149}
7150
a7615030
CW
7151static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
7152{
d330a953
JN
7153 if (i915.panel_use_ssc >= 0)
7154 return i915.panel_use_ssc != 0;
41aa3448 7155 return dev_priv->vbt.lvds_use_ssc
435793df 7156 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
a7615030
CW
7157}
7158
7429e9d4 7159static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
c65d77d8 7160{
7df00d7a 7161 return (1 << dpll->n) << 16 | dpll->m2;
7429e9d4 7162}
f47709a9 7163
7429e9d4
DV
7164static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
7165{
7166 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
c65d77d8
JB
7167}
7168
f47709a9 7169static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
190f68c5 7170 struct intel_crtc_state *crtc_state,
9e2c8475 7171 struct dpll *reduced_clock)
a7516a05 7172{
f47709a9 7173 struct drm_device *dev = crtc->base.dev;
a7516a05
JB
7174 u32 fp, fp2 = 0;
7175
7176 if (IS_PINEVIEW(dev)) {
190f68c5 7177 fp = pnv_dpll_compute_fp(&crtc_state->dpll);
a7516a05 7178 if (reduced_clock)
7429e9d4 7179 fp2 = pnv_dpll_compute_fp(reduced_clock);
a7516a05 7180 } else {
190f68c5 7181 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
a7516a05 7182 if (reduced_clock)
7429e9d4 7183 fp2 = i9xx_dpll_compute_fp(reduced_clock);
a7516a05
JB
7184 }
7185
190f68c5 7186 crtc_state->dpll_hw_state.fp0 = fp;
a7516a05 7187
f47709a9 7188 crtc->lowfreq_avail = false;
a93e255f 7189 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
ab585dea 7190 reduced_clock) {
190f68c5 7191 crtc_state->dpll_hw_state.fp1 = fp2;
f47709a9 7192 crtc->lowfreq_avail = true;
a7516a05 7193 } else {
190f68c5 7194 crtc_state->dpll_hw_state.fp1 = fp;
a7516a05
JB
7195 }
7196}
7197
5e69f97f
CML
7198static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
7199 pipe)
89b667f8
JB
7200{
7201 u32 reg_val;
7202
7203 /*
7204 * PLLB opamp always calibrates to max value of 0x3f, force enable it
7205 * and set it to a reasonable value instead.
7206 */
ab3c759a 7207 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
89b667f8
JB
7208 reg_val &= 0xffffff00;
7209 reg_val |= 0x00000030;
ab3c759a 7210 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
89b667f8 7211
ab3c759a 7212 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
89b667f8
JB
7213 reg_val &= 0x8cffffff;
7214 reg_val = 0x8c000000;
ab3c759a 7215 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
89b667f8 7216
ab3c759a 7217 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
89b667f8 7218 reg_val &= 0xffffff00;
ab3c759a 7219 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
89b667f8 7220
ab3c759a 7221 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
89b667f8
JB
7222 reg_val &= 0x00ffffff;
7223 reg_val |= 0xb0000000;
ab3c759a 7224 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
89b667f8
JB
7225}
7226
b551842d
DV
7227static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
7228 struct intel_link_m_n *m_n)
7229{
7230 struct drm_device *dev = crtc->base.dev;
7231 struct drm_i915_private *dev_priv = dev->dev_private;
7232 int pipe = crtc->pipe;
7233
e3b95f1e
DV
7234 I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7235 I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
7236 I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
7237 I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
b551842d
DV
7238}
7239
7240static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
f769cd24
VK
7241 struct intel_link_m_n *m_n,
7242 struct intel_link_m_n *m2_n2)
b551842d
DV
7243{
7244 struct drm_device *dev = crtc->base.dev;
7245 struct drm_i915_private *dev_priv = dev->dev_private;
7246 int pipe = crtc->pipe;
6e3c9717 7247 enum transcoder transcoder = crtc->config->cpu_transcoder;
b551842d
DV
7248
7249 if (INTEL_INFO(dev)->gen >= 5) {
7250 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
7251 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
7252 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
7253 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
f769cd24
VK
7254 /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
7255 * for gen < 8) and if DRRS is supported (to make sure the
7256 * registers are not unnecessarily accessed).
7257 */
44395bfe 7258 if (m2_n2 && (IS_CHERRYVIEW(dev) || INTEL_INFO(dev)->gen < 8) &&
6e3c9717 7259 crtc->config->has_drrs) {
f769cd24
VK
7260 I915_WRITE(PIPE_DATA_M2(transcoder),
7261 TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
7262 I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
7263 I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
7264 I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
7265 }
b551842d 7266 } else {
e3b95f1e
DV
7267 I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7268 I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
7269 I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
7270 I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
b551842d
DV
7271 }
7272}
7273
fe3cd48d 7274void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
03afc4a2 7275{
fe3cd48d
R
7276 struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
7277
7278 if (m_n == M1_N1) {
7279 dp_m_n = &crtc->config->dp_m_n;
7280 dp_m2_n2 = &crtc->config->dp_m2_n2;
7281 } else if (m_n == M2_N2) {
7282
7283 /*
7284 * M2_N2 registers are not supported. Hence m2_n2 divider value
7285 * needs to be programmed into M1_N1.
7286 */
7287 dp_m_n = &crtc->config->dp_m2_n2;
7288 } else {
7289 DRM_ERROR("Unsupported divider value\n");
7290 return;
7291 }
7292
6e3c9717
ACO
7293 if (crtc->config->has_pch_encoder)
7294 intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
03afc4a2 7295 else
fe3cd48d 7296 intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
03afc4a2
DV
7297}
7298
251ac862
DV
7299static void vlv_compute_dpll(struct intel_crtc *crtc,
7300 struct intel_crtc_state *pipe_config)
bdd4b6a6 7301{
03ed5cbf 7302 pipe_config->dpll_hw_state.dpll = DPLL_INTEGRATED_REF_CLK_VLV |
cd2d34d9 7303 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
03ed5cbf
VS
7304 if (crtc->pipe != PIPE_A)
7305 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
bdd4b6a6 7306
cd2d34d9 7307 /* DPLL not used with DSI, but still need the rest set up */
187a1c07 7308 if (!pipe_config->has_dsi_encoder)
cd2d34d9
VS
7309 pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE |
7310 DPLL_EXT_BUFFER_ENABLE_VLV;
7311
03ed5cbf
VS
7312 pipe_config->dpll_hw_state.dpll_md =
7313 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
7314}
bdd4b6a6 7315
03ed5cbf
VS
7316static void chv_compute_dpll(struct intel_crtc *crtc,
7317 struct intel_crtc_state *pipe_config)
7318{
7319 pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLK_CHV |
cd2d34d9 7320 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
03ed5cbf
VS
7321 if (crtc->pipe != PIPE_A)
7322 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
7323
cd2d34d9 7324 /* DPLL not used with DSI, but still need the rest set up */
187a1c07 7325 if (!pipe_config->has_dsi_encoder)
cd2d34d9
VS
7326 pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE;
7327
03ed5cbf
VS
7328 pipe_config->dpll_hw_state.dpll_md =
7329 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
bdd4b6a6
DV
7330}
7331
d288f65f 7332static void vlv_prepare_pll(struct intel_crtc *crtc,
5cec258b 7333 const struct intel_crtc_state *pipe_config)
a0c4da24 7334{
f47709a9 7335 struct drm_device *dev = crtc->base.dev;
a0c4da24 7336 struct drm_i915_private *dev_priv = dev->dev_private;
cd2d34d9 7337 enum pipe pipe = crtc->pipe;
bdd4b6a6 7338 u32 mdiv;
a0c4da24 7339 u32 bestn, bestm1, bestm2, bestp1, bestp2;
bdd4b6a6 7340 u32 coreclk, reg_val;
a0c4da24 7341
cd2d34d9
VS
7342 /* Enable Refclk */
7343 I915_WRITE(DPLL(pipe),
7344 pipe_config->dpll_hw_state.dpll &
7345 ~(DPLL_VCO_ENABLE | DPLL_EXT_BUFFER_ENABLE_VLV));
7346
7347 /* No need to actually set up the DPLL with DSI */
7348 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
7349 return;
7350
a580516d 7351 mutex_lock(&dev_priv->sb_lock);
09153000 7352
d288f65f
VS
7353 bestn = pipe_config->dpll.n;
7354 bestm1 = pipe_config->dpll.m1;
7355 bestm2 = pipe_config->dpll.m2;
7356 bestp1 = pipe_config->dpll.p1;
7357 bestp2 = pipe_config->dpll.p2;
a0c4da24 7358
89b667f8
JB
7359 /* See eDP HDMI DPIO driver vbios notes doc */
7360
7361 /* PLL B needs special handling */
bdd4b6a6 7362 if (pipe == PIPE_B)
5e69f97f 7363 vlv_pllb_recal_opamp(dev_priv, pipe);
89b667f8
JB
7364
7365 /* Set up Tx target for periodic Rcomp update */
ab3c759a 7366 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
89b667f8
JB
7367
7368 /* Disable target IRef on PLL */
ab3c759a 7369 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
89b667f8 7370 reg_val &= 0x00ffffff;
ab3c759a 7371 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
89b667f8
JB
7372
7373 /* Disable fast lock */
ab3c759a 7374 vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
89b667f8
JB
7375
7376 /* Set idtafcrecal before PLL is enabled */
a0c4da24
JB
7377 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
7378 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
7379 mdiv |= ((bestn << DPIO_N_SHIFT));
a0c4da24 7380 mdiv |= (1 << DPIO_K_SHIFT);
7df5080b
JB
7381
7382 /*
7383 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
7384 * but we don't support that).
7385 * Note: don't use the DAC post divider as it seems unstable.
7386 */
7387 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
ab3c759a 7388 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
a0c4da24 7389
a0c4da24 7390 mdiv |= DPIO_ENABLE_CALIBRATION;
ab3c759a 7391 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
a0c4da24 7392
89b667f8 7393 /* Set HBR and RBR LPF coefficients */
d288f65f 7394 if (pipe_config->port_clock == 162000 ||
409ee761
ACO
7395 intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG) ||
7396 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
ab3c759a 7397 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
885b0120 7398 0x009f0003);
89b667f8 7399 else
ab3c759a 7400 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
89b667f8
JB
7401 0x00d0000f);
7402
681a8504 7403 if (pipe_config->has_dp_encoder) {
89b667f8 7404 /* Use SSC source */
bdd4b6a6 7405 if (pipe == PIPE_A)
ab3c759a 7406 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
7407 0x0df40000);
7408 else
ab3c759a 7409 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
7410 0x0df70000);
7411 } else { /* HDMI or VGA */
7412 /* Use bend source */
bdd4b6a6 7413 if (pipe == PIPE_A)
ab3c759a 7414 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
7415 0x0df70000);
7416 else
ab3c759a 7417 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
7418 0x0df40000);
7419 }
a0c4da24 7420
ab3c759a 7421 coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
89b667f8 7422 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
409ee761
ACO
7423 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
7424 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
89b667f8 7425 coreclk |= 0x01000000;
ab3c759a 7426 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
a0c4da24 7427
ab3c759a 7428 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
a580516d 7429 mutex_unlock(&dev_priv->sb_lock);
a0c4da24
JB
7430}
7431
d288f65f 7432static void chv_prepare_pll(struct intel_crtc *crtc,
5cec258b 7433 const struct intel_crtc_state *pipe_config)
9d556c99
CML
7434{
7435 struct drm_device *dev = crtc->base.dev;
7436 struct drm_i915_private *dev_priv = dev->dev_private;
cd2d34d9 7437 enum pipe pipe = crtc->pipe;
9d556c99 7438 enum dpio_channel port = vlv_pipe_to_channel(pipe);
9cbe40c1 7439 u32 loopfilter, tribuf_calcntr;
9d556c99 7440 u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
a945ce7e 7441 u32 dpio_val;
9cbe40c1 7442 int vco;
9d556c99 7443
cd2d34d9
VS
7444 /* Enable Refclk and SSC */
7445 I915_WRITE(DPLL(pipe),
7446 pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
7447
7448 /* No need to actually set up the DPLL with DSI */
7449 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
7450 return;
7451
d288f65f
VS
7452 bestn = pipe_config->dpll.n;
7453 bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
7454 bestm1 = pipe_config->dpll.m1;
7455 bestm2 = pipe_config->dpll.m2 >> 22;
7456 bestp1 = pipe_config->dpll.p1;
7457 bestp2 = pipe_config->dpll.p2;
9cbe40c1 7458 vco = pipe_config->dpll.vco;
a945ce7e 7459 dpio_val = 0;
9cbe40c1 7460 loopfilter = 0;
9d556c99 7461
a580516d 7462 mutex_lock(&dev_priv->sb_lock);
9d556c99 7463
9d556c99
CML
7464 /* p1 and p2 divider */
7465 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
7466 5 << DPIO_CHV_S1_DIV_SHIFT |
7467 bestp1 << DPIO_CHV_P1_DIV_SHIFT |
7468 bestp2 << DPIO_CHV_P2_DIV_SHIFT |
7469 1 << DPIO_CHV_K_DIV_SHIFT);
7470
7471 /* Feedback post-divider - m2 */
7472 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
7473
7474 /* Feedback refclk divider - n and m1 */
7475 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
7476 DPIO_CHV_M1_DIV_BY_2 |
7477 1 << DPIO_CHV_N_DIV_SHIFT);
7478
7479 /* M2 fraction division */
25a25dfc 7480 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
9d556c99
CML
7481
7482 /* M2 fraction division enable */
a945ce7e
VP
7483 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
7484 dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
7485 dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
7486 if (bestm2_frac)
7487 dpio_val |= DPIO_CHV_FRAC_DIV_EN;
7488 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
9d556c99 7489
de3a0fde
VP
7490 /* Program digital lock detect threshold */
7491 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
7492 dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
7493 DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
7494 dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
7495 if (!bestm2_frac)
7496 dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
7497 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
7498
9d556c99 7499 /* Loop filter */
9cbe40c1
VP
7500 if (vco == 5400000) {
7501 loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
7502 loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
7503 loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
7504 tribuf_calcntr = 0x9;
7505 } else if (vco <= 6200000) {
7506 loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
7507 loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
7508 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7509 tribuf_calcntr = 0x9;
7510 } else if (vco <= 6480000) {
7511 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7512 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7513 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7514 tribuf_calcntr = 0x8;
7515 } else {
7516 /* Not supported. Apply the same limits as in the max case */
7517 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7518 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7519 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7520 tribuf_calcntr = 0;
7521 }
9d556c99
CML
7522 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
7523
968040b2 7524 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
9cbe40c1
VP
7525 dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
7526 dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
7527 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
7528
9d556c99
CML
7529 /* AFC Recal */
7530 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
7531 vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
7532 DPIO_AFC_RECAL);
7533
a580516d 7534 mutex_unlock(&dev_priv->sb_lock);
9d556c99
CML
7535}
7536
d288f65f
VS
7537/**
7538 * vlv_force_pll_on - forcibly enable just the PLL
7539 * @dev_priv: i915 private structure
7540 * @pipe: pipe PLL to enable
7541 * @dpll: PLL configuration
7542 *
7543 * Enable the PLL for @pipe using the supplied @dpll config. To be used
7544 * in cases where we need the PLL enabled even when @pipe is not going to
7545 * be enabled.
7546 */
3f36b937
TU
7547int vlv_force_pll_on(struct drm_device *dev, enum pipe pipe,
7548 const struct dpll *dpll)
d288f65f
VS
7549{
7550 struct intel_crtc *crtc =
7551 to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
3f36b937
TU
7552 struct intel_crtc_state *pipe_config;
7553
7554 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
7555 if (!pipe_config)
7556 return -ENOMEM;
7557
7558 pipe_config->base.crtc = &crtc->base;
7559 pipe_config->pixel_multiplier = 1;
7560 pipe_config->dpll = *dpll;
d288f65f
VS
7561
7562 if (IS_CHERRYVIEW(dev)) {
3f36b937
TU
7563 chv_compute_dpll(crtc, pipe_config);
7564 chv_prepare_pll(crtc, pipe_config);
7565 chv_enable_pll(crtc, pipe_config);
d288f65f 7566 } else {
3f36b937
TU
7567 vlv_compute_dpll(crtc, pipe_config);
7568 vlv_prepare_pll(crtc, pipe_config);
7569 vlv_enable_pll(crtc, pipe_config);
d288f65f 7570 }
3f36b937
TU
7571
7572 kfree(pipe_config);
7573
7574 return 0;
d288f65f
VS
7575}
7576
7577/**
7578 * vlv_force_pll_off - forcibly disable just the PLL
7579 * @dev_priv: i915 private structure
7580 * @pipe: pipe PLL to disable
7581 *
7582 * Disable the PLL for @pipe. To be used in cases where we need
7583 * the PLL enabled even when @pipe is not going to be enabled.
7584 */
7585void vlv_force_pll_off(struct drm_device *dev, enum pipe pipe)
7586{
7587 if (IS_CHERRYVIEW(dev))
7588 chv_disable_pll(to_i915(dev), pipe);
7589 else
7590 vlv_disable_pll(to_i915(dev), pipe);
7591}
7592
251ac862
DV
7593static void i9xx_compute_dpll(struct intel_crtc *crtc,
7594 struct intel_crtc_state *crtc_state,
9e2c8475 7595 struct dpll *reduced_clock)
eb1cbe48 7596{
f47709a9 7597 struct drm_device *dev = crtc->base.dev;
eb1cbe48 7598 struct drm_i915_private *dev_priv = dev->dev_private;
eb1cbe48
DV
7599 u32 dpll;
7600 bool is_sdvo;
190f68c5 7601 struct dpll *clock = &crtc_state->dpll;
eb1cbe48 7602
190f68c5 7603 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
2a8f64ca 7604
a93e255f
ACO
7605 is_sdvo = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO) ||
7606 intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI);
eb1cbe48
DV
7607
7608 dpll = DPLL_VGA_MODE_DIS;
7609
a93e255f 7610 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
eb1cbe48
DV
7611 dpll |= DPLLB_MODE_LVDS;
7612 else
7613 dpll |= DPLLB_MODE_DAC_SERIAL;
6cc5f341 7614
ef1b460d 7615 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
190f68c5 7616 dpll |= (crtc_state->pixel_multiplier - 1)
198a037f 7617 << SDVO_MULTIPLIER_SHIFT_HIRES;
eb1cbe48 7618 }
198a037f
DV
7619
7620 if (is_sdvo)
4a33e48d 7621 dpll |= DPLL_SDVO_HIGH_SPEED;
198a037f 7622
190f68c5 7623 if (crtc_state->has_dp_encoder)
4a33e48d 7624 dpll |= DPLL_SDVO_HIGH_SPEED;
eb1cbe48
DV
7625
7626 /* compute bitmask from p1 value */
7627 if (IS_PINEVIEW(dev))
7628 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
7629 else {
7630 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7631 if (IS_G4X(dev) && reduced_clock)
7632 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
7633 }
7634 switch (clock->p2) {
7635 case 5:
7636 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
7637 break;
7638 case 7:
7639 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
7640 break;
7641 case 10:
7642 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
7643 break;
7644 case 14:
7645 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
7646 break;
7647 }
7648 if (INTEL_INFO(dev)->gen >= 4)
7649 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
7650
190f68c5 7651 if (crtc_state->sdvo_tv_clock)
eb1cbe48 7652 dpll |= PLL_REF_INPUT_TVCLKINBC;
a93e255f 7653 else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
ceb41007 7654 intel_panel_use_ssc(dev_priv))
eb1cbe48
DV
7655 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7656 else
7657 dpll |= PLL_REF_INPUT_DREFCLK;
7658
7659 dpll |= DPLL_VCO_ENABLE;
190f68c5 7660 crtc_state->dpll_hw_state.dpll = dpll;
8bcc2795 7661
eb1cbe48 7662 if (INTEL_INFO(dev)->gen >= 4) {
190f68c5 7663 u32 dpll_md = (crtc_state->pixel_multiplier - 1)
ef1b460d 7664 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
190f68c5 7665 crtc_state->dpll_hw_state.dpll_md = dpll_md;
eb1cbe48
DV
7666 }
7667}
7668
251ac862
DV
7669static void i8xx_compute_dpll(struct intel_crtc *crtc,
7670 struct intel_crtc_state *crtc_state,
9e2c8475 7671 struct dpll *reduced_clock)
eb1cbe48 7672{
f47709a9 7673 struct drm_device *dev = crtc->base.dev;
eb1cbe48 7674 struct drm_i915_private *dev_priv = dev->dev_private;
eb1cbe48 7675 u32 dpll;
190f68c5 7676 struct dpll *clock = &crtc_state->dpll;
eb1cbe48 7677
190f68c5 7678 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
2a8f64ca 7679
eb1cbe48
DV
7680 dpll = DPLL_VGA_MODE_DIS;
7681
a93e255f 7682 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
eb1cbe48
DV
7683 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7684 } else {
7685 if (clock->p1 == 2)
7686 dpll |= PLL_P1_DIVIDE_BY_TWO;
7687 else
7688 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7689 if (clock->p2 == 4)
7690 dpll |= PLL_P2_DIVIDE_BY_4;
7691 }
7692
a93e255f 7693 if (!IS_I830(dev) && intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
4a33e48d
DV
7694 dpll |= DPLL_DVO_2X_MODE;
7695
a93e255f 7696 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
ceb41007 7697 intel_panel_use_ssc(dev_priv))
eb1cbe48
DV
7698 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7699 else
7700 dpll |= PLL_REF_INPUT_DREFCLK;
7701
7702 dpll |= DPLL_VCO_ENABLE;
190f68c5 7703 crtc_state->dpll_hw_state.dpll = dpll;
eb1cbe48
DV
7704}
7705
8a654f3b 7706static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
b0e77b9c
PZ
7707{
7708 struct drm_device *dev = intel_crtc->base.dev;
7709 struct drm_i915_private *dev_priv = dev->dev_private;
7710 enum pipe pipe = intel_crtc->pipe;
6e3c9717 7711 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
7c5f93b0 7712 const struct drm_display_mode *adjusted_mode = &intel_crtc->config->base.adjusted_mode;
1caea6e9
VS
7713 uint32_t crtc_vtotal, crtc_vblank_end;
7714 int vsyncshift = 0;
4d8a62ea
DV
7715
7716 /* We need to be careful not to changed the adjusted mode, for otherwise
7717 * the hw state checker will get angry at the mismatch. */
7718 crtc_vtotal = adjusted_mode->crtc_vtotal;
7719 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
b0e77b9c 7720
609aeaca 7721 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
b0e77b9c 7722 /* the chip adds 2 halflines automatically */
4d8a62ea
DV
7723 crtc_vtotal -= 1;
7724 crtc_vblank_end -= 1;
609aeaca 7725
409ee761 7726 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
609aeaca
VS
7727 vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
7728 else
7729 vsyncshift = adjusted_mode->crtc_hsync_start -
7730 adjusted_mode->crtc_htotal / 2;
1caea6e9
VS
7731 if (vsyncshift < 0)
7732 vsyncshift += adjusted_mode->crtc_htotal;
b0e77b9c
PZ
7733 }
7734
7735 if (INTEL_INFO(dev)->gen > 3)
fe2b8f9d 7736 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
b0e77b9c 7737
fe2b8f9d 7738 I915_WRITE(HTOTAL(cpu_transcoder),
b0e77b9c
PZ
7739 (adjusted_mode->crtc_hdisplay - 1) |
7740 ((adjusted_mode->crtc_htotal - 1) << 16));
fe2b8f9d 7741 I915_WRITE(HBLANK(cpu_transcoder),
b0e77b9c
PZ
7742 (adjusted_mode->crtc_hblank_start - 1) |
7743 ((adjusted_mode->crtc_hblank_end - 1) << 16));
fe2b8f9d 7744 I915_WRITE(HSYNC(cpu_transcoder),
b0e77b9c
PZ
7745 (adjusted_mode->crtc_hsync_start - 1) |
7746 ((adjusted_mode->crtc_hsync_end - 1) << 16));
7747
fe2b8f9d 7748 I915_WRITE(VTOTAL(cpu_transcoder),
b0e77b9c 7749 (adjusted_mode->crtc_vdisplay - 1) |
4d8a62ea 7750 ((crtc_vtotal - 1) << 16));
fe2b8f9d 7751 I915_WRITE(VBLANK(cpu_transcoder),
b0e77b9c 7752 (adjusted_mode->crtc_vblank_start - 1) |
4d8a62ea 7753 ((crtc_vblank_end - 1) << 16));
fe2b8f9d 7754 I915_WRITE(VSYNC(cpu_transcoder),
b0e77b9c
PZ
7755 (adjusted_mode->crtc_vsync_start - 1) |
7756 ((adjusted_mode->crtc_vsync_end - 1) << 16));
7757
b5e508d4
PZ
7758 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
7759 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
7760 * documented on the DDI_FUNC_CTL register description, EDP Input Select
7761 * bits. */
7762 if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
7763 (pipe == PIPE_B || pipe == PIPE_C))
7764 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
7765
bc58be60
JN
7766}
7767
7768static void intel_set_pipe_src_size(struct intel_crtc *intel_crtc)
7769{
7770 struct drm_device *dev = intel_crtc->base.dev;
7771 struct drm_i915_private *dev_priv = dev->dev_private;
7772 enum pipe pipe = intel_crtc->pipe;
7773
b0e77b9c
PZ
7774 /* pipesrc controls the size that is scaled from, which should
7775 * always be the user's requested size.
7776 */
7777 I915_WRITE(PIPESRC(pipe),
6e3c9717
ACO
7778 ((intel_crtc->config->pipe_src_w - 1) << 16) |
7779 (intel_crtc->config->pipe_src_h - 1));
b0e77b9c
PZ
7780}
7781
1bd1bd80 7782static void intel_get_pipe_timings(struct intel_crtc *crtc,
5cec258b 7783 struct intel_crtc_state *pipe_config)
1bd1bd80
DV
7784{
7785 struct drm_device *dev = crtc->base.dev;
7786 struct drm_i915_private *dev_priv = dev->dev_private;
7787 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
7788 uint32_t tmp;
7789
7790 tmp = I915_READ(HTOTAL(cpu_transcoder));
2d112de7
ACO
7791 pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
7792 pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 7793 tmp = I915_READ(HBLANK(cpu_transcoder));
2d112de7
ACO
7794 pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
7795 pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 7796 tmp = I915_READ(HSYNC(cpu_transcoder));
2d112de7
ACO
7797 pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
7798 pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80
DV
7799
7800 tmp = I915_READ(VTOTAL(cpu_transcoder));
2d112de7
ACO
7801 pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
7802 pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 7803 tmp = I915_READ(VBLANK(cpu_transcoder));
2d112de7
ACO
7804 pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
7805 pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 7806 tmp = I915_READ(VSYNC(cpu_transcoder));
2d112de7
ACO
7807 pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
7808 pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80
DV
7809
7810 if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
2d112de7
ACO
7811 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
7812 pipe_config->base.adjusted_mode.crtc_vtotal += 1;
7813 pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
1bd1bd80 7814 }
bc58be60
JN
7815}
7816
7817static void intel_get_pipe_src_size(struct intel_crtc *crtc,
7818 struct intel_crtc_state *pipe_config)
7819{
7820 struct drm_device *dev = crtc->base.dev;
7821 struct drm_i915_private *dev_priv = dev->dev_private;
7822 u32 tmp;
1bd1bd80
DV
7823
7824 tmp = I915_READ(PIPESRC(crtc->pipe));
37327abd
VS
7825 pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
7826 pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
7827
2d112de7
ACO
7828 pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
7829 pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
1bd1bd80
DV
7830}
7831
f6a83288 7832void intel_mode_from_pipe_config(struct drm_display_mode *mode,
5cec258b 7833 struct intel_crtc_state *pipe_config)
babea61d 7834{
2d112de7
ACO
7835 mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
7836 mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
7837 mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
7838 mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
babea61d 7839
2d112de7
ACO
7840 mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
7841 mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
7842 mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
7843 mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
babea61d 7844
2d112de7 7845 mode->flags = pipe_config->base.adjusted_mode.flags;
cd13f5ab 7846 mode->type = DRM_MODE_TYPE_DRIVER;
babea61d 7847
2d112de7
ACO
7848 mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
7849 mode->flags |= pipe_config->base.adjusted_mode.flags;
cd13f5ab
ML
7850
7851 mode->hsync = drm_mode_hsync(mode);
7852 mode->vrefresh = drm_mode_vrefresh(mode);
7853 drm_mode_set_name(mode);
babea61d
JB
7854}
7855
84b046f3
DV
7856static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
7857{
7858 struct drm_device *dev = intel_crtc->base.dev;
7859 struct drm_i915_private *dev_priv = dev->dev_private;
7860 uint32_t pipeconf;
7861
9f11a9e4 7862 pipeconf = 0;
84b046f3 7863
b6b5d049
VS
7864 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
7865 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
7866 pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
67c72a12 7867
6e3c9717 7868 if (intel_crtc->config->double_wide)
cf532bb2 7869 pipeconf |= PIPECONF_DOUBLE_WIDE;
84b046f3 7870
ff9ce46e 7871 /* only g4x and later have fancy bpc/dither controls */
666a4537 7872 if (IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
ff9ce46e 7873 /* Bspec claims that we can't use dithering for 30bpp pipes. */
6e3c9717 7874 if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
ff9ce46e 7875 pipeconf |= PIPECONF_DITHER_EN |
84b046f3 7876 PIPECONF_DITHER_TYPE_SP;
84b046f3 7877
6e3c9717 7878 switch (intel_crtc->config->pipe_bpp) {
ff9ce46e
DV
7879 case 18:
7880 pipeconf |= PIPECONF_6BPC;
7881 break;
7882 case 24:
7883 pipeconf |= PIPECONF_8BPC;
7884 break;
7885 case 30:
7886 pipeconf |= PIPECONF_10BPC;
7887 break;
7888 default:
7889 /* Case prevented by intel_choose_pipe_bpp_dither. */
7890 BUG();
84b046f3
DV
7891 }
7892 }
7893
7894 if (HAS_PIPE_CXSR(dev)) {
7895 if (intel_crtc->lowfreq_avail) {
7896 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
7897 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
7898 } else {
7899 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
84b046f3
DV
7900 }
7901 }
7902
6e3c9717 7903 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
efc2cfff 7904 if (INTEL_INFO(dev)->gen < 4 ||
409ee761 7905 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
efc2cfff
VS
7906 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
7907 else
7908 pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
7909 } else
84b046f3
DV
7910 pipeconf |= PIPECONF_PROGRESSIVE;
7911
666a4537
WB
7912 if ((IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) &&
7913 intel_crtc->config->limited_color_range)
9f11a9e4 7914 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
9c8e09b7 7915
84b046f3
DV
7916 I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
7917 POSTING_READ(PIPECONF(intel_crtc->pipe));
7918}
7919
81c97f52
ACO
7920static int i8xx_crtc_compute_clock(struct intel_crtc *crtc,
7921 struct intel_crtc_state *crtc_state)
7922{
7923 struct drm_device *dev = crtc->base.dev;
7924 struct drm_i915_private *dev_priv = dev->dev_private;
1b6f4958 7925 const struct intel_limit *limit;
81c97f52
ACO
7926 int refclk = 48000;
7927
7928 memset(&crtc_state->dpll_hw_state, 0,
7929 sizeof(crtc_state->dpll_hw_state));
7930
7931 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
7932 if (intel_panel_use_ssc(dev_priv)) {
7933 refclk = dev_priv->vbt.lvds_ssc_freq;
7934 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
7935 }
7936
7937 limit = &intel_limits_i8xx_lvds;
7938 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO)) {
7939 limit = &intel_limits_i8xx_dvo;
7940 } else {
7941 limit = &intel_limits_i8xx_dac;
7942 }
7943
7944 if (!crtc_state->clock_set &&
7945 !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7946 refclk, NULL, &crtc_state->dpll)) {
7947 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7948 return -EINVAL;
7949 }
7950
7951 i8xx_compute_dpll(crtc, crtc_state, NULL);
7952
7953 return 0;
7954}
7955
19ec6693
ACO
7956static int g4x_crtc_compute_clock(struct intel_crtc *crtc,
7957 struct intel_crtc_state *crtc_state)
7958{
7959 struct drm_device *dev = crtc->base.dev;
7960 struct drm_i915_private *dev_priv = dev->dev_private;
1b6f4958 7961 const struct intel_limit *limit;
19ec6693
ACO
7962 int refclk = 96000;
7963
7964 memset(&crtc_state->dpll_hw_state, 0,
7965 sizeof(crtc_state->dpll_hw_state));
7966
7967 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
7968 if (intel_panel_use_ssc(dev_priv)) {
7969 refclk = dev_priv->vbt.lvds_ssc_freq;
7970 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
7971 }
7972
7973 if (intel_is_dual_link_lvds(dev))
7974 limit = &intel_limits_g4x_dual_channel_lvds;
7975 else
7976 limit = &intel_limits_g4x_single_channel_lvds;
7977 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI) ||
7978 intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
7979 limit = &intel_limits_g4x_hdmi;
7980 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO)) {
7981 limit = &intel_limits_g4x_sdvo;
7982 } else {
7983 /* The option is for other outputs */
7984 limit = &intel_limits_i9xx_sdvo;
7985 }
7986
7987 if (!crtc_state->clock_set &&
7988 !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7989 refclk, NULL, &crtc_state->dpll)) {
7990 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7991 return -EINVAL;
7992 }
7993
7994 i9xx_compute_dpll(crtc, crtc_state, NULL);
7995
7996 return 0;
7997}
7998
70e8aa21
ACO
7999static int pnv_crtc_compute_clock(struct intel_crtc *crtc,
8000 struct intel_crtc_state *crtc_state)
8001{
8002 struct drm_device *dev = crtc->base.dev;
8003 struct drm_i915_private *dev_priv = dev->dev_private;
1b6f4958 8004 const struct intel_limit *limit;
70e8aa21
ACO
8005 int refclk = 96000;
8006
8007 memset(&crtc_state->dpll_hw_state, 0,
8008 sizeof(crtc_state->dpll_hw_state));
8009
8010 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
8011 if (intel_panel_use_ssc(dev_priv)) {
8012 refclk = dev_priv->vbt.lvds_ssc_freq;
8013 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
8014 }
8015
8016 limit = &intel_limits_pineview_lvds;
8017 } else {
8018 limit = &intel_limits_pineview_sdvo;
8019 }
8020
8021 if (!crtc_state->clock_set &&
8022 !pnv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8023 refclk, NULL, &crtc_state->dpll)) {
8024 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8025 return -EINVAL;
8026 }
8027
8028 i9xx_compute_dpll(crtc, crtc_state, NULL);
8029
8030 return 0;
8031}
8032
190f68c5
ACO
8033static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
8034 struct intel_crtc_state *crtc_state)
79e53945 8035{
c7653199 8036 struct drm_device *dev = crtc->base.dev;
79e53945 8037 struct drm_i915_private *dev_priv = dev->dev_private;
1b6f4958 8038 const struct intel_limit *limit;
81c97f52 8039 int refclk = 96000;
79e53945 8040
dd3cd74a
ACO
8041 memset(&crtc_state->dpll_hw_state, 0,
8042 sizeof(crtc_state->dpll_hw_state));
8043
70e8aa21
ACO
8044 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
8045 if (intel_panel_use_ssc(dev_priv)) {
8046 refclk = dev_priv->vbt.lvds_ssc_freq;
8047 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
8048 }
43565a06 8049
70e8aa21
ACO
8050 limit = &intel_limits_i9xx_lvds;
8051 } else {
8052 limit = &intel_limits_i9xx_sdvo;
81c97f52 8053 }
79e53945 8054
70e8aa21
ACO
8055 if (!crtc_state->clock_set &&
8056 !i9xx_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;
f47709a9 8060 }
7026d4ac 8061
81c97f52 8062 i9xx_compute_dpll(crtc, crtc_state, NULL);
79e53945 8063
c8f7a0db 8064 return 0;
f564048e
EA
8065}
8066
65b3d6a9
ACO
8067static int chv_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_chv;
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 !chv_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 chv_compute_dpll(crtc, crtc_state);
8084
8085 return 0;
8086}
8087
8088static int vlv_crtc_compute_clock(struct intel_crtc *crtc,
8089 struct intel_crtc_state *crtc_state)
8090{
8091 int refclk = 100000;
1b6f4958 8092 const struct intel_limit *limit = &intel_limits_vlv;
65b3d6a9
ACO
8093
8094 memset(&crtc_state->dpll_hw_state, 0,
8095 sizeof(crtc_state->dpll_hw_state));
8096
65b3d6a9
ACO
8097 if (!crtc_state->clock_set &&
8098 !vlv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8099 refclk, NULL, &crtc_state->dpll)) {
8100 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8101 return -EINVAL;
8102 }
8103
8104 vlv_compute_dpll(crtc, crtc_state);
8105
8106 return 0;
8107}
8108
2fa2fe9a 8109static void i9xx_get_pfit_config(struct intel_crtc *crtc,
5cec258b 8110 struct intel_crtc_state *pipe_config)
2fa2fe9a
DV
8111{
8112 struct drm_device *dev = crtc->base.dev;
8113 struct drm_i915_private *dev_priv = dev->dev_private;
8114 uint32_t tmp;
8115
dc9e7dec
VS
8116 if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
8117 return;
8118
2fa2fe9a 8119 tmp = I915_READ(PFIT_CONTROL);
06922821
DV
8120 if (!(tmp & PFIT_ENABLE))
8121 return;
2fa2fe9a 8122
06922821 8123 /* Check whether the pfit is attached to our pipe. */
2fa2fe9a
DV
8124 if (INTEL_INFO(dev)->gen < 4) {
8125 if (crtc->pipe != PIPE_B)
8126 return;
2fa2fe9a
DV
8127 } else {
8128 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
8129 return;
8130 }
8131
06922821 8132 pipe_config->gmch_pfit.control = tmp;
2fa2fe9a 8133 pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
2fa2fe9a
DV
8134}
8135
acbec814 8136static void vlv_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 8137 struct intel_crtc_state *pipe_config)
acbec814
JB
8138{
8139 struct drm_device *dev = crtc->base.dev;
8140 struct drm_i915_private *dev_priv = dev->dev_private;
8141 int pipe = pipe_config->cpu_transcoder;
9e2c8475 8142 struct dpll clock;
acbec814 8143 u32 mdiv;
662c6ecb 8144 int refclk = 100000;
acbec814 8145
b521973b
VS
8146 /* In case of DSI, DPLL will not be used */
8147 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
f573de5a
SK
8148 return;
8149
a580516d 8150 mutex_lock(&dev_priv->sb_lock);
ab3c759a 8151 mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
a580516d 8152 mutex_unlock(&dev_priv->sb_lock);
acbec814
JB
8153
8154 clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
8155 clock.m2 = mdiv & DPIO_M2DIV_MASK;
8156 clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
8157 clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
8158 clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
8159
dccbea3b 8160 pipe_config->port_clock = vlv_calc_dpll_params(refclk, &clock);
acbec814
JB
8161}
8162
5724dbd1
DL
8163static void
8164i9xx_get_initial_plane_config(struct intel_crtc *crtc,
8165 struct intel_initial_plane_config *plane_config)
1ad292b5
JB
8166{
8167 struct drm_device *dev = crtc->base.dev;
8168 struct drm_i915_private *dev_priv = dev->dev_private;
8169 u32 val, base, offset;
8170 int pipe = crtc->pipe, plane = crtc->plane;
8171 int fourcc, pixel_format;
6761dd31 8172 unsigned int aligned_height;
b113d5ee 8173 struct drm_framebuffer *fb;
1b842c89 8174 struct intel_framebuffer *intel_fb;
1ad292b5 8175
42a7b088
DL
8176 val = I915_READ(DSPCNTR(plane));
8177 if (!(val & DISPLAY_PLANE_ENABLE))
8178 return;
8179
d9806c9f 8180 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
1b842c89 8181 if (!intel_fb) {
1ad292b5
JB
8182 DRM_DEBUG_KMS("failed to alloc fb\n");
8183 return;
8184 }
8185
1b842c89
DL
8186 fb = &intel_fb->base;
8187
18c5247e
DV
8188 if (INTEL_INFO(dev)->gen >= 4) {
8189 if (val & DISPPLANE_TILED) {
49af449b 8190 plane_config->tiling = I915_TILING_X;
18c5247e
DV
8191 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
8192 }
8193 }
1ad292b5
JB
8194
8195 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
b35d63fa 8196 fourcc = i9xx_format_to_fourcc(pixel_format);
b113d5ee
DL
8197 fb->pixel_format = fourcc;
8198 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
1ad292b5
JB
8199
8200 if (INTEL_INFO(dev)->gen >= 4) {
49af449b 8201 if (plane_config->tiling)
1ad292b5
JB
8202 offset = I915_READ(DSPTILEOFF(plane));
8203 else
8204 offset = I915_READ(DSPLINOFF(plane));
8205 base = I915_READ(DSPSURF(plane)) & 0xfffff000;
8206 } else {
8207 base = I915_READ(DSPADDR(plane));
8208 }
8209 plane_config->base = base;
8210
8211 val = I915_READ(PIPESRC(pipe));
b113d5ee
DL
8212 fb->width = ((val >> 16) & 0xfff) + 1;
8213 fb->height = ((val >> 0) & 0xfff) + 1;
1ad292b5
JB
8214
8215 val = I915_READ(DSPSTRIDE(pipe));
b113d5ee 8216 fb->pitches[0] = val & 0xffffffc0;
1ad292b5 8217
b113d5ee 8218 aligned_height = intel_fb_align_height(dev, fb->height,
091df6cb
DV
8219 fb->pixel_format,
8220 fb->modifier[0]);
1ad292b5 8221
f37b5c2b 8222 plane_config->size = fb->pitches[0] * aligned_height;
1ad292b5 8223
2844a921
DL
8224 DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
8225 pipe_name(pipe), plane, fb->width, fb->height,
8226 fb->bits_per_pixel, base, fb->pitches[0],
8227 plane_config->size);
1ad292b5 8228
2d14030b 8229 plane_config->fb = intel_fb;
1ad292b5
JB
8230}
8231
70b23a98 8232static void chv_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 8233 struct intel_crtc_state *pipe_config)
70b23a98
VS
8234{
8235 struct drm_device *dev = crtc->base.dev;
8236 struct drm_i915_private *dev_priv = dev->dev_private;
8237 int pipe = pipe_config->cpu_transcoder;
8238 enum dpio_channel port = vlv_pipe_to_channel(pipe);
9e2c8475 8239 struct dpll clock;
0d7b6b11 8240 u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2, pll_dw3;
70b23a98
VS
8241 int refclk = 100000;
8242
b521973b
VS
8243 /* In case of DSI, DPLL will not be used */
8244 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
8245 return;
8246
a580516d 8247 mutex_lock(&dev_priv->sb_lock);
70b23a98
VS
8248 cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
8249 pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
8250 pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
8251 pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
0d7b6b11 8252 pll_dw3 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
a580516d 8253 mutex_unlock(&dev_priv->sb_lock);
70b23a98
VS
8254
8255 clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
0d7b6b11
ID
8256 clock.m2 = (pll_dw0 & 0xff) << 22;
8257 if (pll_dw3 & DPIO_CHV_FRAC_DIV_EN)
8258 clock.m2 |= pll_dw2 & 0x3fffff;
70b23a98
VS
8259 clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
8260 clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
8261 clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
8262
dccbea3b 8263 pipe_config->port_clock = chv_calc_dpll_params(refclk, &clock);
70b23a98
VS
8264}
8265
0e8ffe1b 8266static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
5cec258b 8267 struct intel_crtc_state *pipe_config)
0e8ffe1b
DV
8268{
8269 struct drm_device *dev = crtc->base.dev;
8270 struct drm_i915_private *dev_priv = dev->dev_private;
1729050e 8271 enum intel_display_power_domain power_domain;
0e8ffe1b 8272 uint32_t tmp;
1729050e 8273 bool ret;
0e8ffe1b 8274
1729050e
ID
8275 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
8276 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
b5482bd0
ID
8277 return false;
8278
e143a21c 8279 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
8106ddbd 8280 pipe_config->shared_dpll = NULL;
eccb140b 8281
1729050e
ID
8282 ret = false;
8283
0e8ffe1b
DV
8284 tmp = I915_READ(PIPECONF(crtc->pipe));
8285 if (!(tmp & PIPECONF_ENABLE))
1729050e 8286 goto out;
0e8ffe1b 8287
666a4537 8288 if (IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
42571aef
VS
8289 switch (tmp & PIPECONF_BPC_MASK) {
8290 case PIPECONF_6BPC:
8291 pipe_config->pipe_bpp = 18;
8292 break;
8293 case PIPECONF_8BPC:
8294 pipe_config->pipe_bpp = 24;
8295 break;
8296 case PIPECONF_10BPC:
8297 pipe_config->pipe_bpp = 30;
8298 break;
8299 default:
8300 break;
8301 }
8302 }
8303
666a4537
WB
8304 if ((IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) &&
8305 (tmp & PIPECONF_COLOR_RANGE_SELECT))
b5a9fa09
DV
8306 pipe_config->limited_color_range = true;
8307
282740f7
VS
8308 if (INTEL_INFO(dev)->gen < 4)
8309 pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
8310
1bd1bd80 8311 intel_get_pipe_timings(crtc, pipe_config);
bc58be60 8312 intel_get_pipe_src_size(crtc, pipe_config);
1bd1bd80 8313
2fa2fe9a
DV
8314 i9xx_get_pfit_config(crtc, pipe_config);
8315
6c49f241 8316 if (INTEL_INFO(dev)->gen >= 4) {
c231775c
VS
8317 /* No way to read it out on pipes B and C */
8318 if (IS_CHERRYVIEW(dev) && crtc->pipe != PIPE_A)
8319 tmp = dev_priv->chv_dpll_md[crtc->pipe];
8320 else
8321 tmp = I915_READ(DPLL_MD(crtc->pipe));
6c49f241
DV
8322 pipe_config->pixel_multiplier =
8323 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
8324 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
8bcc2795 8325 pipe_config->dpll_hw_state.dpll_md = tmp;
6c49f241
DV
8326 } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
8327 tmp = I915_READ(DPLL(crtc->pipe));
8328 pipe_config->pixel_multiplier =
8329 ((tmp & SDVO_MULTIPLIER_MASK)
8330 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
8331 } else {
8332 /* Note that on i915G/GM the pixel multiplier is in the sdvo
8333 * port and will be fixed up in the encoder->get_config
8334 * function. */
8335 pipe_config->pixel_multiplier = 1;
8336 }
8bcc2795 8337 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
666a4537 8338 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
1c4e0274
VS
8339 /*
8340 * DPLL_DVO_2X_MODE must be enabled for both DPLLs
8341 * on 830. Filter it out here so that we don't
8342 * report errors due to that.
8343 */
8344 if (IS_I830(dev))
8345 pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
8346
8bcc2795
DV
8347 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
8348 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
165e901c
VS
8349 } else {
8350 /* Mask out read-only status bits. */
8351 pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
8352 DPLL_PORTC_READY_MASK |
8353 DPLL_PORTB_READY_MASK);
8bcc2795 8354 }
6c49f241 8355
70b23a98
VS
8356 if (IS_CHERRYVIEW(dev))
8357 chv_crtc_clock_get(crtc, pipe_config);
8358 else if (IS_VALLEYVIEW(dev))
acbec814
JB
8359 vlv_crtc_clock_get(crtc, pipe_config);
8360 else
8361 i9xx_crtc_clock_get(crtc, pipe_config);
18442d08 8362
0f64614d
VS
8363 /*
8364 * Normally the dotclock is filled in by the encoder .get_config()
8365 * but in case the pipe is enabled w/o any ports we need a sane
8366 * default.
8367 */
8368 pipe_config->base.adjusted_mode.crtc_clock =
8369 pipe_config->port_clock / pipe_config->pixel_multiplier;
8370
1729050e
ID
8371 ret = true;
8372
8373out:
8374 intel_display_power_put(dev_priv, power_domain);
8375
8376 return ret;
0e8ffe1b
DV
8377}
8378
dde86e2d 8379static void ironlake_init_pch_refclk(struct drm_device *dev)
13d83a67
JB
8380{
8381 struct drm_i915_private *dev_priv = dev->dev_private;
13d83a67 8382 struct intel_encoder *encoder;
1c1a24d2 8383 int i;
74cfd7ac 8384 u32 val, final;
13d83a67 8385 bool has_lvds = false;
199e5d79 8386 bool has_cpu_edp = false;
199e5d79 8387 bool has_panel = false;
99eb6a01
KP
8388 bool has_ck505 = false;
8389 bool can_ssc = false;
1c1a24d2 8390 bool using_ssc_source = false;
13d83a67
JB
8391
8392 /* We need to take the global config into account */
b2784e15 8393 for_each_intel_encoder(dev, encoder) {
199e5d79
KP
8394 switch (encoder->type) {
8395 case INTEL_OUTPUT_LVDS:
8396 has_panel = true;
8397 has_lvds = true;
8398 break;
8399 case INTEL_OUTPUT_EDP:
8400 has_panel = true;
2de6905f 8401 if (enc_to_dig_port(&encoder->base)->port == PORT_A)
199e5d79
KP
8402 has_cpu_edp = true;
8403 break;
6847d71b
PZ
8404 default:
8405 break;
13d83a67
JB
8406 }
8407 }
8408
99eb6a01 8409 if (HAS_PCH_IBX(dev)) {
41aa3448 8410 has_ck505 = dev_priv->vbt.display_clock_mode;
99eb6a01
KP
8411 can_ssc = has_ck505;
8412 } else {
8413 has_ck505 = false;
8414 can_ssc = true;
8415 }
8416
1c1a24d2
L
8417 /* Check if any DPLLs are using the SSC source */
8418 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
8419 u32 temp = I915_READ(PCH_DPLL(i));
8420
8421 if (!(temp & DPLL_VCO_ENABLE))
8422 continue;
8423
8424 if ((temp & PLL_REF_INPUT_MASK) ==
8425 PLLB_REF_INPUT_SPREADSPECTRUMIN) {
8426 using_ssc_source = true;
8427 break;
8428 }
8429 }
8430
8431 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d using_ssc_source %d\n",
8432 has_panel, has_lvds, has_ck505, using_ssc_source);
13d83a67
JB
8433
8434 /* Ironlake: try to setup display ref clock before DPLL
8435 * enabling. This is only under driver's control after
8436 * PCH B stepping, previous chipset stepping should be
8437 * ignoring this setting.
8438 */
74cfd7ac
CW
8439 val = I915_READ(PCH_DREF_CONTROL);
8440
8441 /* As we must carefully and slowly disable/enable each source in turn,
8442 * compute the final state we want first and check if we need to
8443 * make any changes at all.
8444 */
8445 final = val;
8446 final &= ~DREF_NONSPREAD_SOURCE_MASK;
8447 if (has_ck505)
8448 final |= DREF_NONSPREAD_CK505_ENABLE;
8449 else
8450 final |= DREF_NONSPREAD_SOURCE_ENABLE;
8451
8c07eb68 8452 final &= ~DREF_SSC_SOURCE_MASK;
74cfd7ac 8453 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
8c07eb68 8454 final &= ~DREF_SSC1_ENABLE;
74cfd7ac
CW
8455
8456 if (has_panel) {
8457 final |= DREF_SSC_SOURCE_ENABLE;
8458
8459 if (intel_panel_use_ssc(dev_priv) && can_ssc)
8460 final |= DREF_SSC1_ENABLE;
8461
8462 if (has_cpu_edp) {
8463 if (intel_panel_use_ssc(dev_priv) && can_ssc)
8464 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
8465 else
8466 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
8467 } else
8468 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
1c1a24d2
L
8469 } else if (using_ssc_source) {
8470 final |= DREF_SSC_SOURCE_ENABLE;
8471 final |= DREF_SSC1_ENABLE;
74cfd7ac
CW
8472 }
8473
8474 if (final == val)
8475 return;
8476
13d83a67 8477 /* Always enable nonspread source */
74cfd7ac 8478 val &= ~DREF_NONSPREAD_SOURCE_MASK;
13d83a67 8479
99eb6a01 8480 if (has_ck505)
74cfd7ac 8481 val |= DREF_NONSPREAD_CK505_ENABLE;
99eb6a01 8482 else
74cfd7ac 8483 val |= DREF_NONSPREAD_SOURCE_ENABLE;
13d83a67 8484
199e5d79 8485 if (has_panel) {
74cfd7ac
CW
8486 val &= ~DREF_SSC_SOURCE_MASK;
8487 val |= DREF_SSC_SOURCE_ENABLE;
13d83a67 8488
199e5d79 8489 /* SSC must be turned on before enabling the CPU output */
99eb6a01 8490 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
199e5d79 8491 DRM_DEBUG_KMS("Using SSC on panel\n");
74cfd7ac 8492 val |= DREF_SSC1_ENABLE;
e77166b5 8493 } else
74cfd7ac 8494 val &= ~DREF_SSC1_ENABLE;
199e5d79
KP
8495
8496 /* Get SSC going before enabling the outputs */
74cfd7ac 8497 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
8498 POSTING_READ(PCH_DREF_CONTROL);
8499 udelay(200);
8500
74cfd7ac 8501 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
13d83a67
JB
8502
8503 /* Enable CPU source on CPU attached eDP */
199e5d79 8504 if (has_cpu_edp) {
99eb6a01 8505 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
199e5d79 8506 DRM_DEBUG_KMS("Using SSC on eDP\n");
74cfd7ac 8507 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
eba905b2 8508 } else
74cfd7ac 8509 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
199e5d79 8510 } else
74cfd7ac 8511 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
199e5d79 8512
74cfd7ac 8513 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
8514 POSTING_READ(PCH_DREF_CONTROL);
8515 udelay(200);
8516 } else {
1c1a24d2 8517 DRM_DEBUG_KMS("Disabling CPU source output\n");
199e5d79 8518
74cfd7ac 8519 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
199e5d79
KP
8520
8521 /* Turn off CPU output */
74cfd7ac 8522 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
199e5d79 8523
74cfd7ac 8524 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
8525 POSTING_READ(PCH_DREF_CONTROL);
8526 udelay(200);
8527
1c1a24d2
L
8528 if (!using_ssc_source) {
8529 DRM_DEBUG_KMS("Disabling SSC source\n");
199e5d79 8530
1c1a24d2
L
8531 /* Turn off the SSC source */
8532 val &= ~DREF_SSC_SOURCE_MASK;
8533 val |= DREF_SSC_SOURCE_DISABLE;
f165d283 8534
1c1a24d2
L
8535 /* Turn off SSC1 */
8536 val &= ~DREF_SSC1_ENABLE;
8537
8538 I915_WRITE(PCH_DREF_CONTROL, val);
8539 POSTING_READ(PCH_DREF_CONTROL);
8540 udelay(200);
8541 }
13d83a67 8542 }
74cfd7ac
CW
8543
8544 BUG_ON(val != final);
13d83a67
JB
8545}
8546
f31f2d55 8547static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
dde86e2d 8548{
f31f2d55 8549 uint32_t tmp;
dde86e2d 8550
0ff066a9
PZ
8551 tmp = I915_READ(SOUTH_CHICKEN2);
8552 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
8553 I915_WRITE(SOUTH_CHICKEN2, tmp);
dde86e2d 8554
cf3598c2
ID
8555 if (wait_for_us(I915_READ(SOUTH_CHICKEN2) &
8556 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
0ff066a9 8557 DRM_ERROR("FDI mPHY reset assert timeout\n");
dde86e2d 8558
0ff066a9
PZ
8559 tmp = I915_READ(SOUTH_CHICKEN2);
8560 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
8561 I915_WRITE(SOUTH_CHICKEN2, tmp);
dde86e2d 8562
cf3598c2
ID
8563 if (wait_for_us((I915_READ(SOUTH_CHICKEN2) &
8564 FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
0ff066a9 8565 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
f31f2d55
PZ
8566}
8567
8568/* WaMPhyProgramming:hsw */
8569static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
8570{
8571 uint32_t tmp;
dde86e2d
PZ
8572
8573 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
8574 tmp &= ~(0xFF << 24);
8575 tmp |= (0x12 << 24);
8576 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
8577
dde86e2d
PZ
8578 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
8579 tmp |= (1 << 11);
8580 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
8581
8582 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
8583 tmp |= (1 << 11);
8584 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
8585
dde86e2d
PZ
8586 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
8587 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
8588 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
8589
8590 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
8591 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
8592 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
8593
0ff066a9
PZ
8594 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
8595 tmp &= ~(7 << 13);
8596 tmp |= (5 << 13);
8597 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
dde86e2d 8598
0ff066a9
PZ
8599 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
8600 tmp &= ~(7 << 13);
8601 tmp |= (5 << 13);
8602 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
dde86e2d
PZ
8603
8604 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
8605 tmp &= ~0xFF;
8606 tmp |= 0x1C;
8607 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
8608
8609 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
8610 tmp &= ~0xFF;
8611 tmp |= 0x1C;
8612 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
8613
8614 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
8615 tmp &= ~(0xFF << 16);
8616 tmp |= (0x1C << 16);
8617 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
8618
8619 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
8620 tmp &= ~(0xFF << 16);
8621 tmp |= (0x1C << 16);
8622 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
8623
0ff066a9
PZ
8624 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
8625 tmp |= (1 << 27);
8626 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
dde86e2d 8627
0ff066a9
PZ
8628 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
8629 tmp |= (1 << 27);
8630 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
dde86e2d 8631
0ff066a9
PZ
8632 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
8633 tmp &= ~(0xF << 28);
8634 tmp |= (4 << 28);
8635 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
dde86e2d 8636
0ff066a9
PZ
8637 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
8638 tmp &= ~(0xF << 28);
8639 tmp |= (4 << 28);
8640 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
f31f2d55
PZ
8641}
8642
2fa86a1f
PZ
8643/* Implements 3 different sequences from BSpec chapter "Display iCLK
8644 * Programming" based on the parameters passed:
8645 * - Sequence to enable CLKOUT_DP
8646 * - Sequence to enable CLKOUT_DP without spread
8647 * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
8648 */
8649static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
8650 bool with_fdi)
f31f2d55
PZ
8651{
8652 struct drm_i915_private *dev_priv = dev->dev_private;
2fa86a1f
PZ
8653 uint32_t reg, tmp;
8654
8655 if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
8656 with_spread = true;
c2699524 8657 if (WARN(HAS_PCH_LPT_LP(dev) && with_fdi, "LP PCH doesn't have FDI\n"))
2fa86a1f 8658 with_fdi = false;
f31f2d55 8659
a580516d 8660 mutex_lock(&dev_priv->sb_lock);
f31f2d55
PZ
8661
8662 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8663 tmp &= ~SBI_SSCCTL_DISABLE;
8664 tmp |= SBI_SSCCTL_PATHALT;
8665 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8666
8667 udelay(24);
8668
2fa86a1f
PZ
8669 if (with_spread) {
8670 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8671 tmp &= ~SBI_SSCCTL_PATHALT;
8672 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
f31f2d55 8673
2fa86a1f
PZ
8674 if (with_fdi) {
8675 lpt_reset_fdi_mphy(dev_priv);
8676 lpt_program_fdi_mphy(dev_priv);
8677 }
8678 }
dde86e2d 8679
c2699524 8680 reg = HAS_PCH_LPT_LP(dev) ? SBI_GEN0 : SBI_DBUFF0;
2fa86a1f
PZ
8681 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8682 tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8683 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
c00db246 8684
a580516d 8685 mutex_unlock(&dev_priv->sb_lock);
dde86e2d
PZ
8686}
8687
47701c3b
PZ
8688/* Sequence to disable CLKOUT_DP */
8689static void lpt_disable_clkout_dp(struct drm_device *dev)
8690{
8691 struct drm_i915_private *dev_priv = dev->dev_private;
8692 uint32_t reg, tmp;
8693
a580516d 8694 mutex_lock(&dev_priv->sb_lock);
47701c3b 8695
c2699524 8696 reg = HAS_PCH_LPT_LP(dev) ? SBI_GEN0 : SBI_DBUFF0;
47701c3b
PZ
8697 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8698 tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8699 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
8700
8701 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8702 if (!(tmp & SBI_SSCCTL_DISABLE)) {
8703 if (!(tmp & SBI_SSCCTL_PATHALT)) {
8704 tmp |= SBI_SSCCTL_PATHALT;
8705 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8706 udelay(32);
8707 }
8708 tmp |= SBI_SSCCTL_DISABLE;
8709 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8710 }
8711
a580516d 8712 mutex_unlock(&dev_priv->sb_lock);
47701c3b
PZ
8713}
8714
f7be2c21
VS
8715#define BEND_IDX(steps) ((50 + (steps)) / 5)
8716
8717static const uint16_t sscdivintphase[] = {
8718 [BEND_IDX( 50)] = 0x3B23,
8719 [BEND_IDX( 45)] = 0x3B23,
8720 [BEND_IDX( 40)] = 0x3C23,
8721 [BEND_IDX( 35)] = 0x3C23,
8722 [BEND_IDX( 30)] = 0x3D23,
8723 [BEND_IDX( 25)] = 0x3D23,
8724 [BEND_IDX( 20)] = 0x3E23,
8725 [BEND_IDX( 15)] = 0x3E23,
8726 [BEND_IDX( 10)] = 0x3F23,
8727 [BEND_IDX( 5)] = 0x3F23,
8728 [BEND_IDX( 0)] = 0x0025,
8729 [BEND_IDX( -5)] = 0x0025,
8730 [BEND_IDX(-10)] = 0x0125,
8731 [BEND_IDX(-15)] = 0x0125,
8732 [BEND_IDX(-20)] = 0x0225,
8733 [BEND_IDX(-25)] = 0x0225,
8734 [BEND_IDX(-30)] = 0x0325,
8735 [BEND_IDX(-35)] = 0x0325,
8736 [BEND_IDX(-40)] = 0x0425,
8737 [BEND_IDX(-45)] = 0x0425,
8738 [BEND_IDX(-50)] = 0x0525,
8739};
8740
8741/*
8742 * Bend CLKOUT_DP
8743 * steps -50 to 50 inclusive, in steps of 5
8744 * < 0 slow down the clock, > 0 speed up the clock, 0 == no bend (135MHz)
8745 * change in clock period = -(steps / 10) * 5.787 ps
8746 */
8747static void lpt_bend_clkout_dp(struct drm_i915_private *dev_priv, int steps)
8748{
8749 uint32_t tmp;
8750 int idx = BEND_IDX(steps);
8751
8752 if (WARN_ON(steps % 5 != 0))
8753 return;
8754
8755 if (WARN_ON(idx >= ARRAY_SIZE(sscdivintphase)))
8756 return;
8757
8758 mutex_lock(&dev_priv->sb_lock);
8759
8760 if (steps % 10 != 0)
8761 tmp = 0xAAAAAAAB;
8762 else
8763 tmp = 0x00000000;
8764 intel_sbi_write(dev_priv, SBI_SSCDITHPHASE, tmp, SBI_ICLK);
8765
8766 tmp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE, SBI_ICLK);
8767 tmp &= 0xffff0000;
8768 tmp |= sscdivintphase[idx];
8769 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE, tmp, SBI_ICLK);
8770
8771 mutex_unlock(&dev_priv->sb_lock);
8772}
8773
8774#undef BEND_IDX
8775
bf8fa3d3
PZ
8776static void lpt_init_pch_refclk(struct drm_device *dev)
8777{
bf8fa3d3
PZ
8778 struct intel_encoder *encoder;
8779 bool has_vga = false;
8780
b2784e15 8781 for_each_intel_encoder(dev, encoder) {
bf8fa3d3
PZ
8782 switch (encoder->type) {
8783 case INTEL_OUTPUT_ANALOG:
8784 has_vga = true;
8785 break;
6847d71b
PZ
8786 default:
8787 break;
bf8fa3d3
PZ
8788 }
8789 }
8790
f7be2c21
VS
8791 if (has_vga) {
8792 lpt_bend_clkout_dp(to_i915(dev), 0);
47701c3b 8793 lpt_enable_clkout_dp(dev, true, true);
f7be2c21 8794 } else {
47701c3b 8795 lpt_disable_clkout_dp(dev);
f7be2c21 8796 }
bf8fa3d3
PZ
8797}
8798
dde86e2d
PZ
8799/*
8800 * Initialize reference clocks when the driver loads
8801 */
8802void intel_init_pch_refclk(struct drm_device *dev)
8803{
8804 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
8805 ironlake_init_pch_refclk(dev);
8806 else if (HAS_PCH_LPT(dev))
8807 lpt_init_pch_refclk(dev);
8808}
8809
6ff93609 8810static void ironlake_set_pipeconf(struct drm_crtc *crtc)
79e53945 8811{
c8203565 8812 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
79e53945
JB
8813 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8814 int pipe = intel_crtc->pipe;
c8203565
PZ
8815 uint32_t val;
8816
78114071 8817 val = 0;
c8203565 8818
6e3c9717 8819 switch (intel_crtc->config->pipe_bpp) {
c8203565 8820 case 18:
dfd07d72 8821 val |= PIPECONF_6BPC;
c8203565
PZ
8822 break;
8823 case 24:
dfd07d72 8824 val |= PIPECONF_8BPC;
c8203565
PZ
8825 break;
8826 case 30:
dfd07d72 8827 val |= PIPECONF_10BPC;
c8203565
PZ
8828 break;
8829 case 36:
dfd07d72 8830 val |= PIPECONF_12BPC;
c8203565
PZ
8831 break;
8832 default:
cc769b62
PZ
8833 /* Case prevented by intel_choose_pipe_bpp_dither. */
8834 BUG();
c8203565
PZ
8835 }
8836
6e3c9717 8837 if (intel_crtc->config->dither)
c8203565
PZ
8838 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8839
6e3c9717 8840 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
c8203565
PZ
8841 val |= PIPECONF_INTERLACED_ILK;
8842 else
8843 val |= PIPECONF_PROGRESSIVE;
8844
6e3c9717 8845 if (intel_crtc->config->limited_color_range)
3685a8f3 8846 val |= PIPECONF_COLOR_RANGE_SELECT;
3685a8f3 8847
c8203565
PZ
8848 I915_WRITE(PIPECONF(pipe), val);
8849 POSTING_READ(PIPECONF(pipe));
8850}
8851
6ff93609 8852static void haswell_set_pipeconf(struct drm_crtc *crtc)
ee2b0b38 8853{
391bf048 8854 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
ee2b0b38 8855 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6e3c9717 8856 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
391bf048 8857 u32 val = 0;
ee2b0b38 8858
391bf048 8859 if (IS_HASWELL(dev_priv) && intel_crtc->config->dither)
ee2b0b38
PZ
8860 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8861
6e3c9717 8862 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
ee2b0b38
PZ
8863 val |= PIPECONF_INTERLACED_ILK;
8864 else
8865 val |= PIPECONF_PROGRESSIVE;
8866
702e7a56
PZ
8867 I915_WRITE(PIPECONF(cpu_transcoder), val);
8868 POSTING_READ(PIPECONF(cpu_transcoder));
391bf048
JN
8869}
8870
391bf048
JN
8871static void haswell_set_pipemisc(struct drm_crtc *crtc)
8872{
8873 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
8874 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
756f85cf 8875
391bf048
JN
8876 if (IS_BROADWELL(dev_priv) || INTEL_INFO(dev_priv)->gen >= 9) {
8877 u32 val = 0;
756f85cf 8878
6e3c9717 8879 switch (intel_crtc->config->pipe_bpp) {
756f85cf
PZ
8880 case 18:
8881 val |= PIPEMISC_DITHER_6_BPC;
8882 break;
8883 case 24:
8884 val |= PIPEMISC_DITHER_8_BPC;
8885 break;
8886 case 30:
8887 val |= PIPEMISC_DITHER_10_BPC;
8888 break;
8889 case 36:
8890 val |= PIPEMISC_DITHER_12_BPC;
8891 break;
8892 default:
8893 /* Case prevented by pipe_config_set_bpp. */
8894 BUG();
8895 }
8896
6e3c9717 8897 if (intel_crtc->config->dither)
756f85cf
PZ
8898 val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
8899
391bf048 8900 I915_WRITE(PIPEMISC(intel_crtc->pipe), val);
756f85cf 8901 }
ee2b0b38
PZ
8902}
8903
d4b1931c
PZ
8904int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
8905{
8906 /*
8907 * Account for spread spectrum to avoid
8908 * oversubscribing the link. Max center spread
8909 * is 2.5%; use 5% for safety's sake.
8910 */
8911 u32 bps = target_clock * bpp * 21 / 20;
619d4d04 8912 return DIV_ROUND_UP(bps, link_bw * 8);
d4b1931c
PZ
8913}
8914
7429e9d4 8915static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
6cf86a5e 8916{
7429e9d4 8917 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
f48d8f23
PZ
8918}
8919
b75ca6f6
ACO
8920static void ironlake_compute_dpll(struct intel_crtc *intel_crtc,
8921 struct intel_crtc_state *crtc_state,
9e2c8475 8922 struct dpll *reduced_clock)
79e53945 8923{
de13a2e3 8924 struct drm_crtc *crtc = &intel_crtc->base;
79e53945
JB
8925 struct drm_device *dev = crtc->dev;
8926 struct drm_i915_private *dev_priv = dev->dev_private;
55bb9992 8927 struct drm_atomic_state *state = crtc_state->base.state;
da3ced29 8928 struct drm_connector *connector;
55bb9992
ACO
8929 struct drm_connector_state *connector_state;
8930 struct intel_encoder *encoder;
b75ca6f6 8931 u32 dpll, fp, fp2;
ceb41007 8932 int factor, i;
09ede541 8933 bool is_lvds = false, is_sdvo = false;
79e53945 8934
da3ced29 8935 for_each_connector_in_state(state, connector, connector_state, i) {
55bb9992
ACO
8936 if (connector_state->crtc != crtc_state->base.crtc)
8937 continue;
8938
8939 encoder = to_intel_encoder(connector_state->best_encoder);
8940
8941 switch (encoder->type) {
79e53945
JB
8942 case INTEL_OUTPUT_LVDS:
8943 is_lvds = true;
8944 break;
8945 case INTEL_OUTPUT_SDVO:
7d57382e 8946 case INTEL_OUTPUT_HDMI:
79e53945 8947 is_sdvo = true;
79e53945 8948 break;
6847d71b
PZ
8949 default:
8950 break;
79e53945
JB
8951 }
8952 }
79e53945 8953
c1858123 8954 /* Enable autotuning of the PLL clock (if permissible) */
8febb297
EA
8955 factor = 21;
8956 if (is_lvds) {
8957 if ((intel_panel_use_ssc(dev_priv) &&
e91e941b 8958 dev_priv->vbt.lvds_ssc_freq == 100000) ||
f0b44056 8959 (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
8febb297 8960 factor = 25;
190f68c5 8961 } else if (crtc_state->sdvo_tv_clock)
8febb297 8962 factor = 20;
c1858123 8963
b75ca6f6
ACO
8964 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
8965
190f68c5 8966 if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
b75ca6f6
ACO
8967 fp |= FP_CB_TUNE;
8968
8969 if (reduced_clock) {
8970 fp2 = i9xx_dpll_compute_fp(reduced_clock);
2c07245f 8971
b75ca6f6
ACO
8972 if (reduced_clock->m < factor * reduced_clock->n)
8973 fp2 |= FP_CB_TUNE;
8974 } else {
8975 fp2 = fp;
8976 }
9a7c7890 8977
5eddb70b 8978 dpll = 0;
2c07245f 8979
a07d6787
EA
8980 if (is_lvds)
8981 dpll |= DPLLB_MODE_LVDS;
8982 else
8983 dpll |= DPLLB_MODE_DAC_SERIAL;
198a037f 8984
190f68c5 8985 dpll |= (crtc_state->pixel_multiplier - 1)
ef1b460d 8986 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
198a037f
DV
8987
8988 if (is_sdvo)
4a33e48d 8989 dpll |= DPLL_SDVO_HIGH_SPEED;
190f68c5 8990 if (crtc_state->has_dp_encoder)
4a33e48d 8991 dpll |= DPLL_SDVO_HIGH_SPEED;
79e53945 8992
a07d6787 8993 /* compute bitmask from p1 value */
190f68c5 8994 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
a07d6787 8995 /* also FPA1 */
190f68c5 8996 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
a07d6787 8997
190f68c5 8998 switch (crtc_state->dpll.p2) {
a07d6787
EA
8999 case 5:
9000 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
9001 break;
9002 case 7:
9003 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
9004 break;
9005 case 10:
9006 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
9007 break;
9008 case 14:
9009 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
9010 break;
79e53945
JB
9011 }
9012
ceb41007 9013 if (is_lvds && intel_panel_use_ssc(dev_priv))
43565a06 9014 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
79e53945
JB
9015 else
9016 dpll |= PLL_REF_INPUT_DREFCLK;
9017
b75ca6f6
ACO
9018 dpll |= DPLL_VCO_ENABLE;
9019
9020 crtc_state->dpll_hw_state.dpll = dpll;
9021 crtc_state->dpll_hw_state.fp0 = fp;
9022 crtc_state->dpll_hw_state.fp1 = fp2;
de13a2e3
PZ
9023}
9024
190f68c5
ACO
9025static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
9026 struct intel_crtc_state *crtc_state)
de13a2e3 9027{
997c030c
ACO
9028 struct drm_device *dev = crtc->base.dev;
9029 struct drm_i915_private *dev_priv = dev->dev_private;
9e2c8475 9030 struct dpll reduced_clock;
7ed9f894 9031 bool has_reduced_clock = false;
e2b78267 9032 struct intel_shared_dpll *pll;
1b6f4958 9033 const struct intel_limit *limit;
997c030c 9034 int refclk = 120000;
de13a2e3 9035
dd3cd74a
ACO
9036 memset(&crtc_state->dpll_hw_state, 0,
9037 sizeof(crtc_state->dpll_hw_state));
9038
ded220e2
ACO
9039 crtc->lowfreq_avail = false;
9040
9041 /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
9042 if (!crtc_state->has_pch_encoder)
9043 return 0;
79e53945 9044
997c030c
ACO
9045 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
9046 if (intel_panel_use_ssc(dev_priv)) {
9047 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
9048 dev_priv->vbt.lvds_ssc_freq);
9049 refclk = dev_priv->vbt.lvds_ssc_freq;
9050 }
9051
9052 if (intel_is_dual_link_lvds(dev)) {
9053 if (refclk == 100000)
9054 limit = &intel_limits_ironlake_dual_lvds_100m;
9055 else
9056 limit = &intel_limits_ironlake_dual_lvds;
9057 } else {
9058 if (refclk == 100000)
9059 limit = &intel_limits_ironlake_single_lvds_100m;
9060 else
9061 limit = &intel_limits_ironlake_single_lvds;
9062 }
9063 } else {
9064 limit = &intel_limits_ironlake_dac;
9065 }
9066
364ee29d 9067 if (!crtc_state->clock_set &&
997c030c
ACO
9068 !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
9069 refclk, NULL, &crtc_state->dpll)) {
364ee29d
ACO
9070 DRM_ERROR("Couldn't find PLL settings for mode!\n");
9071 return -EINVAL;
f47709a9 9072 }
79e53945 9073
b75ca6f6
ACO
9074 ironlake_compute_dpll(crtc, crtc_state,
9075 has_reduced_clock ? &reduced_clock : NULL);
66e985c0 9076
ded220e2
ACO
9077 pll = intel_get_shared_dpll(crtc, crtc_state, NULL);
9078 if (pll == NULL) {
9079 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
9080 pipe_name(crtc->pipe));
9081 return -EINVAL;
3fb37703 9082 }
79e53945 9083
ded220e2
ACO
9084 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
9085 has_reduced_clock)
c7653199 9086 crtc->lowfreq_avail = true;
e2b78267 9087
c8f7a0db 9088 return 0;
79e53945
JB
9089}
9090
eb14cb74
VS
9091static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
9092 struct intel_link_m_n *m_n)
9093{
9094 struct drm_device *dev = crtc->base.dev;
9095 struct drm_i915_private *dev_priv = dev->dev_private;
9096 enum pipe pipe = crtc->pipe;
9097
9098 m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
9099 m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
9100 m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
9101 & ~TU_SIZE_MASK;
9102 m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
9103 m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
9104 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
9105}
9106
9107static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
9108 enum transcoder transcoder,
b95af8be
VK
9109 struct intel_link_m_n *m_n,
9110 struct intel_link_m_n *m2_n2)
72419203
DV
9111{
9112 struct drm_device *dev = crtc->base.dev;
9113 struct drm_i915_private *dev_priv = dev->dev_private;
eb14cb74 9114 enum pipe pipe = crtc->pipe;
72419203 9115
eb14cb74
VS
9116 if (INTEL_INFO(dev)->gen >= 5) {
9117 m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
9118 m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
9119 m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
9120 & ~TU_SIZE_MASK;
9121 m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
9122 m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
9123 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
b95af8be
VK
9124 /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
9125 * gen < 8) and if DRRS is supported (to make sure the
9126 * registers are not unnecessarily read).
9127 */
9128 if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
6e3c9717 9129 crtc->config->has_drrs) {
b95af8be
VK
9130 m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
9131 m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
9132 m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
9133 & ~TU_SIZE_MASK;
9134 m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
9135 m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
9136 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
9137 }
eb14cb74
VS
9138 } else {
9139 m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
9140 m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
9141 m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
9142 & ~TU_SIZE_MASK;
9143 m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
9144 m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
9145 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
9146 }
9147}
9148
9149void intel_dp_get_m_n(struct intel_crtc *crtc,
5cec258b 9150 struct intel_crtc_state *pipe_config)
eb14cb74 9151{
681a8504 9152 if (pipe_config->has_pch_encoder)
eb14cb74
VS
9153 intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
9154 else
9155 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
b95af8be
VK
9156 &pipe_config->dp_m_n,
9157 &pipe_config->dp_m2_n2);
eb14cb74 9158}
72419203 9159
eb14cb74 9160static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
5cec258b 9161 struct intel_crtc_state *pipe_config)
eb14cb74
VS
9162{
9163 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
b95af8be 9164 &pipe_config->fdi_m_n, NULL);
72419203
DV
9165}
9166
bd2e244f 9167static void skylake_get_pfit_config(struct intel_crtc *crtc,
5cec258b 9168 struct intel_crtc_state *pipe_config)
bd2e244f
JB
9169{
9170 struct drm_device *dev = crtc->base.dev;
9171 struct drm_i915_private *dev_priv = dev->dev_private;
a1b2278e
CK
9172 struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
9173 uint32_t ps_ctrl = 0;
9174 int id = -1;
9175 int i;
bd2e244f 9176
a1b2278e
CK
9177 /* find scaler attached to this pipe */
9178 for (i = 0; i < crtc->num_scalers; i++) {
9179 ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
9180 if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
9181 id = i;
9182 pipe_config->pch_pfit.enabled = true;
9183 pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
9184 pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
9185 break;
9186 }
9187 }
bd2e244f 9188
a1b2278e
CK
9189 scaler_state->scaler_id = id;
9190 if (id >= 0) {
9191 scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
9192 } else {
9193 scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
bd2e244f
JB
9194 }
9195}
9196
5724dbd1
DL
9197static void
9198skylake_get_initial_plane_config(struct intel_crtc *crtc,
9199 struct intel_initial_plane_config *plane_config)
bc8d7dff
DL
9200{
9201 struct drm_device *dev = crtc->base.dev;
9202 struct drm_i915_private *dev_priv = dev->dev_private;
40f46283 9203 u32 val, base, offset, stride_mult, tiling;
bc8d7dff
DL
9204 int pipe = crtc->pipe;
9205 int fourcc, pixel_format;
6761dd31 9206 unsigned int aligned_height;
bc8d7dff 9207 struct drm_framebuffer *fb;
1b842c89 9208 struct intel_framebuffer *intel_fb;
bc8d7dff 9209
d9806c9f 9210 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
1b842c89 9211 if (!intel_fb) {
bc8d7dff
DL
9212 DRM_DEBUG_KMS("failed to alloc fb\n");
9213 return;
9214 }
9215
1b842c89
DL
9216 fb = &intel_fb->base;
9217
bc8d7dff 9218 val = I915_READ(PLANE_CTL(pipe, 0));
42a7b088
DL
9219 if (!(val & PLANE_CTL_ENABLE))
9220 goto error;
9221
bc8d7dff
DL
9222 pixel_format = val & PLANE_CTL_FORMAT_MASK;
9223 fourcc = skl_format_to_fourcc(pixel_format,
9224 val & PLANE_CTL_ORDER_RGBX,
9225 val & PLANE_CTL_ALPHA_MASK);
9226 fb->pixel_format = fourcc;
9227 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
9228
40f46283
DL
9229 tiling = val & PLANE_CTL_TILED_MASK;
9230 switch (tiling) {
9231 case PLANE_CTL_TILED_LINEAR:
9232 fb->modifier[0] = DRM_FORMAT_MOD_NONE;
9233 break;
9234 case PLANE_CTL_TILED_X:
9235 plane_config->tiling = I915_TILING_X;
9236 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
9237 break;
9238 case PLANE_CTL_TILED_Y:
9239 fb->modifier[0] = I915_FORMAT_MOD_Y_TILED;
9240 break;
9241 case PLANE_CTL_TILED_YF:
9242 fb->modifier[0] = I915_FORMAT_MOD_Yf_TILED;
9243 break;
9244 default:
9245 MISSING_CASE(tiling);
9246 goto error;
9247 }
9248
bc8d7dff
DL
9249 base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
9250 plane_config->base = base;
9251
9252 offset = I915_READ(PLANE_OFFSET(pipe, 0));
9253
9254 val = I915_READ(PLANE_SIZE(pipe, 0));
9255 fb->height = ((val >> 16) & 0xfff) + 1;
9256 fb->width = ((val >> 0) & 0x1fff) + 1;
9257
9258 val = I915_READ(PLANE_STRIDE(pipe, 0));
7b49f948 9259 stride_mult = intel_fb_stride_alignment(dev_priv, fb->modifier[0],
40f46283 9260 fb->pixel_format);
bc8d7dff
DL
9261 fb->pitches[0] = (val & 0x3ff) * stride_mult;
9262
9263 aligned_height = intel_fb_align_height(dev, fb->height,
091df6cb
DV
9264 fb->pixel_format,
9265 fb->modifier[0]);
bc8d7dff 9266
f37b5c2b 9267 plane_config->size = fb->pitches[0] * aligned_height;
bc8d7dff
DL
9268
9269 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9270 pipe_name(pipe), fb->width, fb->height,
9271 fb->bits_per_pixel, base, fb->pitches[0],
9272 plane_config->size);
9273
2d14030b 9274 plane_config->fb = intel_fb;
bc8d7dff
DL
9275 return;
9276
9277error:
9278 kfree(fb);
9279}
9280
2fa2fe9a 9281static void ironlake_get_pfit_config(struct intel_crtc *crtc,
5cec258b 9282 struct intel_crtc_state *pipe_config)
2fa2fe9a
DV
9283{
9284 struct drm_device *dev = crtc->base.dev;
9285 struct drm_i915_private *dev_priv = dev->dev_private;
9286 uint32_t tmp;
9287
9288 tmp = I915_READ(PF_CTL(crtc->pipe));
9289
9290 if (tmp & PF_ENABLE) {
fd4daa9c 9291 pipe_config->pch_pfit.enabled = true;
2fa2fe9a
DV
9292 pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
9293 pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
cb8b2a30
DV
9294
9295 /* We currently do not free assignements of panel fitters on
9296 * ivb/hsw (since we don't use the higher upscaling modes which
9297 * differentiates them) so just WARN about this case for now. */
9298 if (IS_GEN7(dev)) {
9299 WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
9300 PF_PIPE_SEL_IVB(crtc->pipe));
9301 }
2fa2fe9a 9302 }
79e53945
JB
9303}
9304
5724dbd1
DL
9305static void
9306ironlake_get_initial_plane_config(struct intel_crtc *crtc,
9307 struct intel_initial_plane_config *plane_config)
4c6baa59
JB
9308{
9309 struct drm_device *dev = crtc->base.dev;
9310 struct drm_i915_private *dev_priv = dev->dev_private;
9311 u32 val, base, offset;
aeee5a49 9312 int pipe = crtc->pipe;
4c6baa59 9313 int fourcc, pixel_format;
6761dd31 9314 unsigned int aligned_height;
b113d5ee 9315 struct drm_framebuffer *fb;
1b842c89 9316 struct intel_framebuffer *intel_fb;
4c6baa59 9317
42a7b088
DL
9318 val = I915_READ(DSPCNTR(pipe));
9319 if (!(val & DISPLAY_PLANE_ENABLE))
9320 return;
9321
d9806c9f 9322 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
1b842c89 9323 if (!intel_fb) {
4c6baa59
JB
9324 DRM_DEBUG_KMS("failed to alloc fb\n");
9325 return;
9326 }
9327
1b842c89
DL
9328 fb = &intel_fb->base;
9329
18c5247e
DV
9330 if (INTEL_INFO(dev)->gen >= 4) {
9331 if (val & DISPPLANE_TILED) {
49af449b 9332 plane_config->tiling = I915_TILING_X;
18c5247e
DV
9333 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
9334 }
9335 }
4c6baa59
JB
9336
9337 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
b35d63fa 9338 fourcc = i9xx_format_to_fourcc(pixel_format);
b113d5ee
DL
9339 fb->pixel_format = fourcc;
9340 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
4c6baa59 9341
aeee5a49 9342 base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
4c6baa59 9343 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
aeee5a49 9344 offset = I915_READ(DSPOFFSET(pipe));
4c6baa59 9345 } else {
49af449b 9346 if (plane_config->tiling)
aeee5a49 9347 offset = I915_READ(DSPTILEOFF(pipe));
4c6baa59 9348 else
aeee5a49 9349 offset = I915_READ(DSPLINOFF(pipe));
4c6baa59
JB
9350 }
9351 plane_config->base = base;
9352
9353 val = I915_READ(PIPESRC(pipe));
b113d5ee
DL
9354 fb->width = ((val >> 16) & 0xfff) + 1;
9355 fb->height = ((val >> 0) & 0xfff) + 1;
4c6baa59
JB
9356
9357 val = I915_READ(DSPSTRIDE(pipe));
b113d5ee 9358 fb->pitches[0] = val & 0xffffffc0;
4c6baa59 9359
b113d5ee 9360 aligned_height = intel_fb_align_height(dev, fb->height,
091df6cb
DV
9361 fb->pixel_format,
9362 fb->modifier[0]);
4c6baa59 9363
f37b5c2b 9364 plane_config->size = fb->pitches[0] * aligned_height;
4c6baa59 9365
2844a921
DL
9366 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9367 pipe_name(pipe), fb->width, fb->height,
9368 fb->bits_per_pixel, base, fb->pitches[0],
9369 plane_config->size);
b113d5ee 9370
2d14030b 9371 plane_config->fb = intel_fb;
4c6baa59
JB
9372}
9373
0e8ffe1b 9374static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
5cec258b 9375 struct intel_crtc_state *pipe_config)
0e8ffe1b
DV
9376{
9377 struct drm_device *dev = crtc->base.dev;
9378 struct drm_i915_private *dev_priv = dev->dev_private;
1729050e 9379 enum intel_display_power_domain power_domain;
0e8ffe1b 9380 uint32_t tmp;
1729050e 9381 bool ret;
0e8ffe1b 9382
1729050e
ID
9383 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
9384 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
930e8c9e
PZ
9385 return false;
9386
e143a21c 9387 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
8106ddbd 9388 pipe_config->shared_dpll = NULL;
eccb140b 9389
1729050e 9390 ret = false;
0e8ffe1b
DV
9391 tmp = I915_READ(PIPECONF(crtc->pipe));
9392 if (!(tmp & PIPECONF_ENABLE))
1729050e 9393 goto out;
0e8ffe1b 9394
42571aef
VS
9395 switch (tmp & PIPECONF_BPC_MASK) {
9396 case PIPECONF_6BPC:
9397 pipe_config->pipe_bpp = 18;
9398 break;
9399 case PIPECONF_8BPC:
9400 pipe_config->pipe_bpp = 24;
9401 break;
9402 case PIPECONF_10BPC:
9403 pipe_config->pipe_bpp = 30;
9404 break;
9405 case PIPECONF_12BPC:
9406 pipe_config->pipe_bpp = 36;
9407 break;
9408 default:
9409 break;
9410 }
9411
b5a9fa09
DV
9412 if (tmp & PIPECONF_COLOR_RANGE_SELECT)
9413 pipe_config->limited_color_range = true;
9414
ab9412ba 9415 if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
66e985c0 9416 struct intel_shared_dpll *pll;
8106ddbd 9417 enum intel_dpll_id pll_id;
66e985c0 9418
88adfff1
DV
9419 pipe_config->has_pch_encoder = true;
9420
627eb5a3
DV
9421 tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
9422 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
9423 FDI_DP_PORT_WIDTH_SHIFT) + 1;
72419203
DV
9424
9425 ironlake_get_fdi_m_n_config(crtc, pipe_config);
6c49f241 9426
2d1fe073 9427 if (HAS_PCH_IBX(dev_priv)) {
d9a7bc67
ID
9428 /*
9429 * The pipe->pch transcoder and pch transcoder->pll
9430 * mapping is fixed.
9431 */
8106ddbd 9432 pll_id = (enum intel_dpll_id) crtc->pipe;
c0d43d62
DV
9433 } else {
9434 tmp = I915_READ(PCH_DPLL_SEL);
9435 if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
8106ddbd 9436 pll_id = DPLL_ID_PCH_PLL_B;
c0d43d62 9437 else
8106ddbd 9438 pll_id= DPLL_ID_PCH_PLL_A;
c0d43d62 9439 }
66e985c0 9440
8106ddbd
ACO
9441 pipe_config->shared_dpll =
9442 intel_get_shared_dpll_by_id(dev_priv, pll_id);
9443 pll = pipe_config->shared_dpll;
66e985c0 9444
2edd6443
ACO
9445 WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
9446 &pipe_config->dpll_hw_state));
c93f54cf
DV
9447
9448 tmp = pipe_config->dpll_hw_state.dpll;
9449 pipe_config->pixel_multiplier =
9450 ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
9451 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
18442d08
VS
9452
9453 ironlake_pch_clock_get(crtc, pipe_config);
6c49f241
DV
9454 } else {
9455 pipe_config->pixel_multiplier = 1;
627eb5a3
DV
9456 }
9457
1bd1bd80 9458 intel_get_pipe_timings(crtc, pipe_config);
bc58be60 9459 intel_get_pipe_src_size(crtc, pipe_config);
1bd1bd80 9460
2fa2fe9a
DV
9461 ironlake_get_pfit_config(crtc, pipe_config);
9462
1729050e
ID
9463 ret = true;
9464
9465out:
9466 intel_display_power_put(dev_priv, power_domain);
9467
9468 return ret;
0e8ffe1b
DV
9469}
9470
be256dc7
PZ
9471static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
9472{
9473 struct drm_device *dev = dev_priv->dev;
be256dc7 9474 struct intel_crtc *crtc;
be256dc7 9475
d3fcc808 9476 for_each_intel_crtc(dev, crtc)
e2c719b7 9477 I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
be256dc7
PZ
9478 pipe_name(crtc->pipe));
9479
e2c719b7
RC
9480 I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
9481 I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
01403de3
VS
9482 I915_STATE_WARN(I915_READ(WRPLL_CTL(0)) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
9483 I915_STATE_WARN(I915_READ(WRPLL_CTL(1)) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
e2c719b7
RC
9484 I915_STATE_WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
9485 I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
be256dc7 9486 "CPU PWM1 enabled\n");
c5107b87 9487 if (IS_HASWELL(dev))
e2c719b7 9488 I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
c5107b87 9489 "CPU PWM2 enabled\n");
e2c719b7 9490 I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
be256dc7 9491 "PCH PWM1 enabled\n");
e2c719b7 9492 I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
be256dc7 9493 "Utility pin enabled\n");
e2c719b7 9494 I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
be256dc7 9495
9926ada1
PZ
9496 /*
9497 * In theory we can still leave IRQs enabled, as long as only the HPD
9498 * interrupts remain enabled. We used to check for that, but since it's
9499 * gen-specific and since we only disable LCPLL after we fully disable
9500 * the interrupts, the check below should be enough.
9501 */
e2c719b7 9502 I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
be256dc7
PZ
9503}
9504
9ccd5aeb
PZ
9505static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
9506{
9507 struct drm_device *dev = dev_priv->dev;
9508
9509 if (IS_HASWELL(dev))
9510 return I915_READ(D_COMP_HSW);
9511 else
9512 return I915_READ(D_COMP_BDW);
9513}
9514
3c4c9b81
PZ
9515static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
9516{
9517 struct drm_device *dev = dev_priv->dev;
9518
9519 if (IS_HASWELL(dev)) {
9520 mutex_lock(&dev_priv->rps.hw_lock);
9521 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
9522 val))
f475dadf 9523 DRM_ERROR("Failed to write to D_COMP\n");
3c4c9b81
PZ
9524 mutex_unlock(&dev_priv->rps.hw_lock);
9525 } else {
9ccd5aeb
PZ
9526 I915_WRITE(D_COMP_BDW, val);
9527 POSTING_READ(D_COMP_BDW);
3c4c9b81 9528 }
be256dc7
PZ
9529}
9530
9531/*
9532 * This function implements pieces of two sequences from BSpec:
9533 * - Sequence for display software to disable LCPLL
9534 * - Sequence for display software to allow package C8+
9535 * The steps implemented here are just the steps that actually touch the LCPLL
9536 * register. Callers should take care of disabling all the display engine
9537 * functions, doing the mode unset, fixing interrupts, etc.
9538 */
6ff58d53
PZ
9539static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
9540 bool switch_to_fclk, bool allow_power_down)
be256dc7
PZ
9541{
9542 uint32_t val;
9543
9544 assert_can_disable_lcpll(dev_priv);
9545
9546 val = I915_READ(LCPLL_CTL);
9547
9548 if (switch_to_fclk) {
9549 val |= LCPLL_CD_SOURCE_FCLK;
9550 I915_WRITE(LCPLL_CTL, val);
9551
f53dd63f
ID
9552 if (wait_for_us(I915_READ(LCPLL_CTL) &
9553 LCPLL_CD_SOURCE_FCLK_DONE, 1))
be256dc7
PZ
9554 DRM_ERROR("Switching to FCLK failed\n");
9555
9556 val = I915_READ(LCPLL_CTL);
9557 }
9558
9559 val |= LCPLL_PLL_DISABLE;
9560 I915_WRITE(LCPLL_CTL, val);
9561 POSTING_READ(LCPLL_CTL);
9562
9563 if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
9564 DRM_ERROR("LCPLL still locked\n");
9565
9ccd5aeb 9566 val = hsw_read_dcomp(dev_priv);
be256dc7 9567 val |= D_COMP_COMP_DISABLE;
3c4c9b81 9568 hsw_write_dcomp(dev_priv, val);
be256dc7
PZ
9569 ndelay(100);
9570
9ccd5aeb
PZ
9571 if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
9572 1))
be256dc7
PZ
9573 DRM_ERROR("D_COMP RCOMP still in progress\n");
9574
9575 if (allow_power_down) {
9576 val = I915_READ(LCPLL_CTL);
9577 val |= LCPLL_POWER_DOWN_ALLOW;
9578 I915_WRITE(LCPLL_CTL, val);
9579 POSTING_READ(LCPLL_CTL);
9580 }
9581}
9582
9583/*
9584 * Fully restores LCPLL, disallowing power down and switching back to LCPLL
9585 * source.
9586 */
6ff58d53 9587static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
be256dc7
PZ
9588{
9589 uint32_t val;
9590
9591 val = I915_READ(LCPLL_CTL);
9592
9593 if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
9594 LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
9595 return;
9596
a8a8bd54
PZ
9597 /*
9598 * Make sure we're not on PC8 state before disabling PC8, otherwise
9599 * we'll hang the machine. To prevent PC8 state, just enable force_wake.
a8a8bd54 9600 */
59bad947 9601 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
215733fa 9602
be256dc7
PZ
9603 if (val & LCPLL_POWER_DOWN_ALLOW) {
9604 val &= ~LCPLL_POWER_DOWN_ALLOW;
9605 I915_WRITE(LCPLL_CTL, val);
35d8f2eb 9606 POSTING_READ(LCPLL_CTL);
be256dc7
PZ
9607 }
9608
9ccd5aeb 9609 val = hsw_read_dcomp(dev_priv);
be256dc7
PZ
9610 val |= D_COMP_COMP_FORCE;
9611 val &= ~D_COMP_COMP_DISABLE;
3c4c9b81 9612 hsw_write_dcomp(dev_priv, val);
be256dc7
PZ
9613
9614 val = I915_READ(LCPLL_CTL);
9615 val &= ~LCPLL_PLL_DISABLE;
9616 I915_WRITE(LCPLL_CTL, val);
9617
9618 if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
9619 DRM_ERROR("LCPLL not locked yet\n");
9620
9621 if (val & LCPLL_CD_SOURCE_FCLK) {
9622 val = I915_READ(LCPLL_CTL);
9623 val &= ~LCPLL_CD_SOURCE_FCLK;
9624 I915_WRITE(LCPLL_CTL, val);
9625
f53dd63f
ID
9626 if (wait_for_us((I915_READ(LCPLL_CTL) &
9627 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
be256dc7
PZ
9628 DRM_ERROR("Switching back to LCPLL failed\n");
9629 }
215733fa 9630
59bad947 9631 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
b6283055 9632 intel_update_cdclk(dev_priv->dev);
be256dc7
PZ
9633}
9634
765dab67
PZ
9635/*
9636 * Package states C8 and deeper are really deep PC states that can only be
9637 * reached when all the devices on the system allow it, so even if the graphics
9638 * device allows PC8+, it doesn't mean the system will actually get to these
9639 * states. Our driver only allows PC8+ when going into runtime PM.
9640 *
9641 * The requirements for PC8+ are that all the outputs are disabled, the power
9642 * well is disabled and most interrupts are disabled, and these are also
9643 * requirements for runtime PM. When these conditions are met, we manually do
9644 * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
9645 * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
9646 * hang the machine.
9647 *
9648 * When we really reach PC8 or deeper states (not just when we allow it) we lose
9649 * the state of some registers, so when we come back from PC8+ we need to
9650 * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
9651 * need to take care of the registers kept by RC6. Notice that this happens even
9652 * if we don't put the device in PCI D3 state (which is what currently happens
9653 * because of the runtime PM support).
9654 *
9655 * For more, read "Display Sequences for Package C8" on the hardware
9656 * documentation.
9657 */
a14cb6fc 9658void hsw_enable_pc8(struct drm_i915_private *dev_priv)
c67a470b 9659{
c67a470b
PZ
9660 struct drm_device *dev = dev_priv->dev;
9661 uint32_t val;
9662
c67a470b
PZ
9663 DRM_DEBUG_KMS("Enabling package C8+\n");
9664
c2699524 9665 if (HAS_PCH_LPT_LP(dev)) {
c67a470b
PZ
9666 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9667 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
9668 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9669 }
9670
9671 lpt_disable_clkout_dp(dev);
c67a470b
PZ
9672 hsw_disable_lcpll(dev_priv, true, true);
9673}
9674
a14cb6fc 9675void hsw_disable_pc8(struct drm_i915_private *dev_priv)
c67a470b
PZ
9676{
9677 struct drm_device *dev = dev_priv->dev;
9678 uint32_t val;
9679
c67a470b
PZ
9680 DRM_DEBUG_KMS("Disabling package C8+\n");
9681
9682 hsw_restore_lcpll(dev_priv);
c67a470b
PZ
9683 lpt_init_pch_refclk(dev);
9684
c2699524 9685 if (HAS_PCH_LPT_LP(dev)) {
c67a470b
PZ
9686 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9687 val |= PCH_LP_PARTITION_LEVEL_DISABLE;
9688 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9689 }
c67a470b
PZ
9690}
9691
324513c0 9692static void bxt_modeset_commit_cdclk(struct drm_atomic_state *old_state)
f8437dd1 9693{
a821fc46 9694 struct drm_device *dev = old_state->dev;
1a617b77
ML
9695 struct intel_atomic_state *old_intel_state =
9696 to_intel_atomic_state(old_state);
9697 unsigned int req_cdclk = old_intel_state->dev_cdclk;
f8437dd1 9698
324513c0 9699 bxt_set_cdclk(to_i915(dev), req_cdclk);
f8437dd1
VK
9700}
9701
b432e5cf 9702/* compute the max rate for new configuration */
27c329ed 9703static int ilk_max_pixel_rate(struct drm_atomic_state *state)
b432e5cf 9704{
565602d7
ML
9705 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
9706 struct drm_i915_private *dev_priv = state->dev->dev_private;
9707 struct drm_crtc *crtc;
9708 struct drm_crtc_state *cstate;
27c329ed 9709 struct intel_crtc_state *crtc_state;
565602d7
ML
9710 unsigned max_pixel_rate = 0, i;
9711 enum pipe pipe;
b432e5cf 9712
565602d7
ML
9713 memcpy(intel_state->min_pixclk, dev_priv->min_pixclk,
9714 sizeof(intel_state->min_pixclk));
27c329ed 9715
565602d7
ML
9716 for_each_crtc_in_state(state, crtc, cstate, i) {
9717 int pixel_rate;
27c329ed 9718
565602d7
ML
9719 crtc_state = to_intel_crtc_state(cstate);
9720 if (!crtc_state->base.enable) {
9721 intel_state->min_pixclk[i] = 0;
b432e5cf 9722 continue;
565602d7 9723 }
b432e5cf 9724
27c329ed 9725 pixel_rate = ilk_pipe_pixel_rate(crtc_state);
b432e5cf
VS
9726
9727 /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
565602d7 9728 if (IS_BROADWELL(dev_priv) && crtc_state->ips_enabled)
b432e5cf
VS
9729 pixel_rate = DIV_ROUND_UP(pixel_rate * 100, 95);
9730
565602d7 9731 intel_state->min_pixclk[i] = pixel_rate;
b432e5cf
VS
9732 }
9733
565602d7
ML
9734 for_each_pipe(dev_priv, pipe)
9735 max_pixel_rate = max(intel_state->min_pixclk[pipe], max_pixel_rate);
9736
b432e5cf
VS
9737 return max_pixel_rate;
9738}
9739
9740static void broadwell_set_cdclk(struct drm_device *dev, int cdclk)
9741{
9742 struct drm_i915_private *dev_priv = dev->dev_private;
9743 uint32_t val, data;
9744 int ret;
9745
9746 if (WARN((I915_READ(LCPLL_CTL) &
9747 (LCPLL_PLL_DISABLE | LCPLL_PLL_LOCK |
9748 LCPLL_CD_CLOCK_DISABLE | LCPLL_ROOT_CD_CLOCK_DISABLE |
9749 LCPLL_CD2X_CLOCK_DISABLE | LCPLL_POWER_DOWN_ALLOW |
9750 LCPLL_CD_SOURCE_FCLK)) != LCPLL_PLL_LOCK,
9751 "trying to change cdclk frequency with cdclk not enabled\n"))
9752 return;
9753
9754 mutex_lock(&dev_priv->rps.hw_lock);
9755 ret = sandybridge_pcode_write(dev_priv,
9756 BDW_PCODE_DISPLAY_FREQ_CHANGE_REQ, 0x0);
9757 mutex_unlock(&dev_priv->rps.hw_lock);
9758 if (ret) {
9759 DRM_ERROR("failed to inform pcode about cdclk change\n");
9760 return;
9761 }
9762
9763 val = I915_READ(LCPLL_CTL);
9764 val |= LCPLL_CD_SOURCE_FCLK;
9765 I915_WRITE(LCPLL_CTL, val);
9766
5ba00178
TU
9767 if (wait_for_us(I915_READ(LCPLL_CTL) &
9768 LCPLL_CD_SOURCE_FCLK_DONE, 1))
b432e5cf
VS
9769 DRM_ERROR("Switching to FCLK failed\n");
9770
9771 val = I915_READ(LCPLL_CTL);
9772 val &= ~LCPLL_CLK_FREQ_MASK;
9773
9774 switch (cdclk) {
9775 case 450000:
9776 val |= LCPLL_CLK_FREQ_450;
9777 data = 0;
9778 break;
9779 case 540000:
9780 val |= LCPLL_CLK_FREQ_54O_BDW;
9781 data = 1;
9782 break;
9783 case 337500:
9784 val |= LCPLL_CLK_FREQ_337_5_BDW;
9785 data = 2;
9786 break;
9787 case 675000:
9788 val |= LCPLL_CLK_FREQ_675_BDW;
9789 data = 3;
9790 break;
9791 default:
9792 WARN(1, "invalid cdclk frequency\n");
9793 return;
9794 }
9795
9796 I915_WRITE(LCPLL_CTL, val);
9797
9798 val = I915_READ(LCPLL_CTL);
9799 val &= ~LCPLL_CD_SOURCE_FCLK;
9800 I915_WRITE(LCPLL_CTL, val);
9801
5ba00178
TU
9802 if (wait_for_us((I915_READ(LCPLL_CTL) &
9803 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
b432e5cf
VS
9804 DRM_ERROR("Switching back to LCPLL failed\n");
9805
9806 mutex_lock(&dev_priv->rps.hw_lock);
9807 sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ, data);
9808 mutex_unlock(&dev_priv->rps.hw_lock);
9809
7f1052a8
VS
9810 I915_WRITE(CDCLK_FREQ, DIV_ROUND_CLOSEST(cdclk, 1000) - 1);
9811
b432e5cf
VS
9812 intel_update_cdclk(dev);
9813
9814 WARN(cdclk != dev_priv->cdclk_freq,
9815 "cdclk requested %d kHz but got %d kHz\n",
9816 cdclk, dev_priv->cdclk_freq);
9817}
9818
587c7914
VS
9819static int broadwell_calc_cdclk(int max_pixclk)
9820{
9821 if (max_pixclk > 540000)
9822 return 675000;
9823 else if (max_pixclk > 450000)
9824 return 540000;
9825 else if (max_pixclk > 337500)
9826 return 450000;
9827 else
9828 return 337500;
9829}
9830
27c329ed 9831static int broadwell_modeset_calc_cdclk(struct drm_atomic_state *state)
b432e5cf 9832{
27c329ed 9833 struct drm_i915_private *dev_priv = to_i915(state->dev);
1a617b77 9834 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
27c329ed 9835 int max_pixclk = ilk_max_pixel_rate(state);
b432e5cf
VS
9836 int cdclk;
9837
9838 /*
9839 * FIXME should also account for plane ratio
9840 * once 64bpp pixel formats are supported.
9841 */
587c7914 9842 cdclk = broadwell_calc_cdclk(max_pixclk);
b432e5cf 9843
b432e5cf 9844 if (cdclk > dev_priv->max_cdclk_freq) {
63ba534e
ML
9845 DRM_DEBUG_KMS("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
9846 cdclk, dev_priv->max_cdclk_freq);
9847 return -EINVAL;
b432e5cf
VS
9848 }
9849
1a617b77
ML
9850 intel_state->cdclk = intel_state->dev_cdclk = cdclk;
9851 if (!intel_state->active_crtcs)
587c7914 9852 intel_state->dev_cdclk = broadwell_calc_cdclk(0);
b432e5cf
VS
9853
9854 return 0;
9855}
9856
27c329ed 9857static void broadwell_modeset_commit_cdclk(struct drm_atomic_state *old_state)
b432e5cf 9858{
27c329ed 9859 struct drm_device *dev = old_state->dev;
1a617b77
ML
9860 struct intel_atomic_state *old_intel_state =
9861 to_intel_atomic_state(old_state);
9862 unsigned req_cdclk = old_intel_state->dev_cdclk;
b432e5cf 9863
27c329ed 9864 broadwell_set_cdclk(dev, req_cdclk);
b432e5cf
VS
9865}
9866
c89e39f3
CT
9867static int skl_modeset_calc_cdclk(struct drm_atomic_state *state)
9868{
9869 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
9870 struct drm_i915_private *dev_priv = to_i915(state->dev);
9871 const int max_pixclk = ilk_max_pixel_rate(state);
a8ca4934 9872 int vco = intel_state->cdclk_pll_vco;
c89e39f3
CT
9873 int cdclk;
9874
9875 /*
9876 * FIXME should also account for plane ratio
9877 * once 64bpp pixel formats are supported.
9878 */
a8ca4934 9879 cdclk = skl_calc_cdclk(max_pixclk, vco);
c89e39f3
CT
9880
9881 /*
9882 * FIXME move the cdclk caclulation to
9883 * compute_config() so we can fail gracegully.
9884 */
9885 if (cdclk > dev_priv->max_cdclk_freq) {
9886 DRM_ERROR("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
9887 cdclk, dev_priv->max_cdclk_freq);
9888 cdclk = dev_priv->max_cdclk_freq;
9889 }
9890
9891 intel_state->cdclk = intel_state->dev_cdclk = cdclk;
9892 if (!intel_state->active_crtcs)
a8ca4934 9893 intel_state->dev_cdclk = skl_calc_cdclk(0, vco);
c89e39f3
CT
9894
9895 return 0;
9896}
9897
9898static void skl_modeset_commit_cdclk(struct drm_atomic_state *old_state)
9899{
1cd593e0
VS
9900 struct drm_i915_private *dev_priv = to_i915(old_state->dev);
9901 struct intel_atomic_state *intel_state = to_intel_atomic_state(old_state);
9902 unsigned int req_cdclk = intel_state->dev_cdclk;
9903 unsigned int req_vco = intel_state->cdclk_pll_vco;
c89e39f3 9904
1cd593e0 9905 skl_set_cdclk(dev_priv, req_cdclk, req_vco);
c89e39f3
CT
9906}
9907
190f68c5
ACO
9908static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
9909 struct intel_crtc_state *crtc_state)
09b4ddf9 9910{
af3997b5
MK
9911 struct intel_encoder *intel_encoder =
9912 intel_ddi_get_crtc_new_encoder(crtc_state);
9913
9914 if (intel_encoder->type != INTEL_OUTPUT_DSI) {
9915 if (!intel_ddi_pll_select(crtc, crtc_state))
9916 return -EINVAL;
9917 }
716c2e55 9918
c7653199 9919 crtc->lowfreq_avail = false;
644cef34 9920
c8f7a0db 9921 return 0;
79e53945
JB
9922}
9923
3760b59c
S
9924static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
9925 enum port port,
9926 struct intel_crtc_state *pipe_config)
9927{
8106ddbd
ACO
9928 enum intel_dpll_id id;
9929
3760b59c
S
9930 switch (port) {
9931 case PORT_A:
9932 pipe_config->ddi_pll_sel = SKL_DPLL0;
08250c4b 9933 id = DPLL_ID_SKL_DPLL0;
3760b59c
S
9934 break;
9935 case PORT_B:
9936 pipe_config->ddi_pll_sel = SKL_DPLL1;
08250c4b 9937 id = DPLL_ID_SKL_DPLL1;
3760b59c
S
9938 break;
9939 case PORT_C:
9940 pipe_config->ddi_pll_sel = SKL_DPLL2;
08250c4b 9941 id = DPLL_ID_SKL_DPLL2;
3760b59c
S
9942 break;
9943 default:
9944 DRM_ERROR("Incorrect port type\n");
8106ddbd 9945 return;
3760b59c 9946 }
8106ddbd
ACO
9947
9948 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
3760b59c
S
9949}
9950
96b7dfb7
S
9951static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
9952 enum port port,
5cec258b 9953 struct intel_crtc_state *pipe_config)
96b7dfb7 9954{
8106ddbd 9955 enum intel_dpll_id id;
a3c988ea 9956 u32 temp;
96b7dfb7
S
9957
9958 temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
9959 pipe_config->ddi_pll_sel = temp >> (port * 3 + 1);
9960
9961 switch (pipe_config->ddi_pll_sel) {
3148ade7 9962 case SKL_DPLL0:
a3c988ea
ACO
9963 id = DPLL_ID_SKL_DPLL0;
9964 break;
96b7dfb7 9965 case SKL_DPLL1:
8106ddbd 9966 id = DPLL_ID_SKL_DPLL1;
96b7dfb7
S
9967 break;
9968 case SKL_DPLL2:
8106ddbd 9969 id = DPLL_ID_SKL_DPLL2;
96b7dfb7
S
9970 break;
9971 case SKL_DPLL3:
8106ddbd 9972 id = DPLL_ID_SKL_DPLL3;
96b7dfb7 9973 break;
8106ddbd
ACO
9974 default:
9975 MISSING_CASE(pipe_config->ddi_pll_sel);
9976 return;
96b7dfb7 9977 }
8106ddbd
ACO
9978
9979 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
96b7dfb7
S
9980}
9981
7d2c8175
DL
9982static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
9983 enum port port,
5cec258b 9984 struct intel_crtc_state *pipe_config)
7d2c8175 9985{
8106ddbd
ACO
9986 enum intel_dpll_id id;
9987
7d2c8175
DL
9988 pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
9989
9990 switch (pipe_config->ddi_pll_sel) {
9991 case PORT_CLK_SEL_WRPLL1:
8106ddbd 9992 id = DPLL_ID_WRPLL1;
7d2c8175
DL
9993 break;
9994 case PORT_CLK_SEL_WRPLL2:
8106ddbd 9995 id = DPLL_ID_WRPLL2;
7d2c8175 9996 break;
00490c22 9997 case PORT_CLK_SEL_SPLL:
8106ddbd 9998 id = DPLL_ID_SPLL;
79bd23da 9999 break;
9d16da65
ACO
10000 case PORT_CLK_SEL_LCPLL_810:
10001 id = DPLL_ID_LCPLL_810;
10002 break;
10003 case PORT_CLK_SEL_LCPLL_1350:
10004 id = DPLL_ID_LCPLL_1350;
10005 break;
10006 case PORT_CLK_SEL_LCPLL_2700:
10007 id = DPLL_ID_LCPLL_2700;
10008 break;
8106ddbd
ACO
10009 default:
10010 MISSING_CASE(pipe_config->ddi_pll_sel);
10011 /* fall through */
10012 case PORT_CLK_SEL_NONE:
8106ddbd 10013 return;
7d2c8175 10014 }
8106ddbd
ACO
10015
10016 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
7d2c8175
DL
10017}
10018
cf30429e
JN
10019static bool hsw_get_transcoder_state(struct intel_crtc *crtc,
10020 struct intel_crtc_state *pipe_config,
10021 unsigned long *power_domain_mask)
10022{
10023 struct drm_device *dev = crtc->base.dev;
10024 struct drm_i915_private *dev_priv = dev->dev_private;
10025 enum intel_display_power_domain power_domain;
10026 u32 tmp;
10027
d9a7bc67
ID
10028 /*
10029 * The pipe->transcoder mapping is fixed with the exception of the eDP
10030 * transcoder handled below.
10031 */
cf30429e
JN
10032 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
10033
10034 /*
10035 * XXX: Do intel_display_power_get_if_enabled before reading this (for
10036 * consistency and less surprising code; it's in always on power).
10037 */
10038 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
10039 if (tmp & TRANS_DDI_FUNC_ENABLE) {
10040 enum pipe trans_edp_pipe;
10041 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
10042 default:
10043 WARN(1, "unknown pipe linked to edp transcoder\n");
10044 case TRANS_DDI_EDP_INPUT_A_ONOFF:
10045 case TRANS_DDI_EDP_INPUT_A_ON:
10046 trans_edp_pipe = PIPE_A;
10047 break;
10048 case TRANS_DDI_EDP_INPUT_B_ONOFF:
10049 trans_edp_pipe = PIPE_B;
10050 break;
10051 case TRANS_DDI_EDP_INPUT_C_ONOFF:
10052 trans_edp_pipe = PIPE_C;
10053 break;
10054 }
10055
10056 if (trans_edp_pipe == crtc->pipe)
10057 pipe_config->cpu_transcoder = TRANSCODER_EDP;
10058 }
10059
10060 power_domain = POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder);
10061 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
10062 return false;
10063 *power_domain_mask |= BIT(power_domain);
10064
10065 tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
10066
10067 return tmp & PIPECONF_ENABLE;
10068}
10069
4d1de975
JN
10070static bool bxt_get_dsi_transcoder_state(struct intel_crtc *crtc,
10071 struct intel_crtc_state *pipe_config,
10072 unsigned long *power_domain_mask)
10073{
10074 struct drm_device *dev = crtc->base.dev;
10075 struct drm_i915_private *dev_priv = dev->dev_private;
10076 enum intel_display_power_domain power_domain;
10077 enum port port;
10078 enum transcoder cpu_transcoder;
10079 u32 tmp;
10080
10081 pipe_config->has_dsi_encoder = false;
10082
10083 for_each_port_masked(port, BIT(PORT_A) | BIT(PORT_C)) {
10084 if (port == PORT_A)
10085 cpu_transcoder = TRANSCODER_DSI_A;
10086 else
10087 cpu_transcoder = TRANSCODER_DSI_C;
10088
10089 power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
10090 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
10091 continue;
10092 *power_domain_mask |= BIT(power_domain);
10093
db18b6a6
ID
10094 /*
10095 * The PLL needs to be enabled with a valid divider
10096 * configuration, otherwise accessing DSI registers will hang
10097 * the machine. See BSpec North Display Engine
10098 * registers/MIPI[BXT]. We can break out here early, since we
10099 * need the same DSI PLL to be enabled for both DSI ports.
10100 */
10101 if (!intel_dsi_pll_is_enabled(dev_priv))
10102 break;
10103
4d1de975
JN
10104 /* XXX: this works for video mode only */
10105 tmp = I915_READ(BXT_MIPI_PORT_CTRL(port));
10106 if (!(tmp & DPI_ENABLE))
10107 continue;
10108
10109 tmp = I915_READ(MIPI_CTRL(port));
10110 if ((tmp & BXT_PIPE_SELECT_MASK) != BXT_PIPE_SELECT(crtc->pipe))
10111 continue;
10112
10113 pipe_config->cpu_transcoder = cpu_transcoder;
10114 pipe_config->has_dsi_encoder = true;
10115 break;
10116 }
10117
10118 return pipe_config->has_dsi_encoder;
10119}
10120
26804afd 10121static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
5cec258b 10122 struct intel_crtc_state *pipe_config)
26804afd
DV
10123{
10124 struct drm_device *dev = crtc->base.dev;
10125 struct drm_i915_private *dev_priv = dev->dev_private;
d452c5b6 10126 struct intel_shared_dpll *pll;
26804afd
DV
10127 enum port port;
10128 uint32_t tmp;
10129
10130 tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
10131
10132 port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
10133
ef11bdb3 10134 if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev))
96b7dfb7 10135 skylake_get_ddi_pll(dev_priv, port, pipe_config);
3760b59c
S
10136 else if (IS_BROXTON(dev))
10137 bxt_get_ddi_pll(dev_priv, port, pipe_config);
96b7dfb7
S
10138 else
10139 haswell_get_ddi_pll(dev_priv, port, pipe_config);
9cd86933 10140
8106ddbd
ACO
10141 pll = pipe_config->shared_dpll;
10142 if (pll) {
2edd6443
ACO
10143 WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
10144 &pipe_config->dpll_hw_state));
d452c5b6
DV
10145 }
10146
26804afd
DV
10147 /*
10148 * Haswell has only FDI/PCH transcoder A. It is which is connected to
10149 * DDI E. So just check whether this pipe is wired to DDI E and whether
10150 * the PCH transcoder is on.
10151 */
ca370455
DL
10152 if (INTEL_INFO(dev)->gen < 9 &&
10153 (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
26804afd
DV
10154 pipe_config->has_pch_encoder = true;
10155
10156 tmp = I915_READ(FDI_RX_CTL(PIPE_A));
10157 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
10158 FDI_DP_PORT_WIDTH_SHIFT) + 1;
10159
10160 ironlake_get_fdi_m_n_config(crtc, pipe_config);
10161 }
10162}
10163
0e8ffe1b 10164static bool haswell_get_pipe_config(struct intel_crtc *crtc,
5cec258b 10165 struct intel_crtc_state *pipe_config)
0e8ffe1b
DV
10166{
10167 struct drm_device *dev = crtc->base.dev;
10168 struct drm_i915_private *dev_priv = dev->dev_private;
1729050e
ID
10169 enum intel_display_power_domain power_domain;
10170 unsigned long power_domain_mask;
cf30429e 10171 bool active;
0e8ffe1b 10172
1729050e
ID
10173 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
10174 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
b5482bd0 10175 return false;
1729050e
ID
10176 power_domain_mask = BIT(power_domain);
10177
8106ddbd 10178 pipe_config->shared_dpll = NULL;
c0d43d62 10179
cf30429e 10180 active = hsw_get_transcoder_state(crtc, pipe_config, &power_domain_mask);
eccb140b 10181
4d1de975
JN
10182 if (IS_BROXTON(dev_priv)) {
10183 bxt_get_dsi_transcoder_state(crtc, pipe_config,
10184 &power_domain_mask);
10185 WARN_ON(active && pipe_config->has_dsi_encoder);
10186 if (pipe_config->has_dsi_encoder)
10187 active = true;
10188 }
10189
cf30429e 10190 if (!active)
1729050e 10191 goto out;
0e8ffe1b 10192
4d1de975
JN
10193 if (!pipe_config->has_dsi_encoder) {
10194 haswell_get_ddi_port_state(crtc, pipe_config);
10195 intel_get_pipe_timings(crtc, pipe_config);
10196 }
627eb5a3 10197
bc58be60 10198 intel_get_pipe_src_size(crtc, pipe_config);
1bd1bd80 10199
05dc698c
LL
10200 pipe_config->gamma_mode =
10201 I915_READ(GAMMA_MODE(crtc->pipe)) & GAMMA_MODE_MODE_MASK;
10202
a1b2278e
CK
10203 if (INTEL_INFO(dev)->gen >= 9) {
10204 skl_init_scalers(dev, crtc, pipe_config);
10205 }
10206
af99ceda
CK
10207 if (INTEL_INFO(dev)->gen >= 9) {
10208 pipe_config->scaler_state.scaler_id = -1;
10209 pipe_config->scaler_state.scaler_users &= ~(1 << SKL_CRTC_INDEX);
10210 }
10211
1729050e
ID
10212 power_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
10213 if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
10214 power_domain_mask |= BIT(power_domain);
1c132b44 10215 if (INTEL_INFO(dev)->gen >= 9)
bd2e244f 10216 skylake_get_pfit_config(crtc, pipe_config);
ff6d9f55 10217 else
1c132b44 10218 ironlake_get_pfit_config(crtc, pipe_config);
bd2e244f 10219 }
88adfff1 10220
e59150dc
JB
10221 if (IS_HASWELL(dev))
10222 pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
10223 (I915_READ(IPS_CTL) & IPS_ENABLE);
42db64ef 10224
4d1de975
JN
10225 if (pipe_config->cpu_transcoder != TRANSCODER_EDP &&
10226 !transcoder_is_dsi(pipe_config->cpu_transcoder)) {
ebb69c95
CT
10227 pipe_config->pixel_multiplier =
10228 I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
10229 } else {
10230 pipe_config->pixel_multiplier = 1;
10231 }
6c49f241 10232
1729050e
ID
10233out:
10234 for_each_power_domain(power_domain, power_domain_mask)
10235 intel_display_power_put(dev_priv, power_domain);
10236
cf30429e 10237 return active;
0e8ffe1b
DV
10238}
10239
55a08b3f
ML
10240static void i845_update_cursor(struct drm_crtc *crtc, u32 base,
10241 const struct intel_plane_state *plane_state)
560b85bb
CW
10242{
10243 struct drm_device *dev = crtc->dev;
10244 struct drm_i915_private *dev_priv = dev->dev_private;
10245 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
dc41c154 10246 uint32_t cntl = 0, size = 0;
560b85bb 10247
55a08b3f
ML
10248 if (plane_state && plane_state->visible) {
10249 unsigned int width = plane_state->base.crtc_w;
10250 unsigned int height = plane_state->base.crtc_h;
dc41c154
VS
10251 unsigned int stride = roundup_pow_of_two(width) * 4;
10252
10253 switch (stride) {
10254 default:
10255 WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
10256 width, stride);
10257 stride = 256;
10258 /* fallthrough */
10259 case 256:
10260 case 512:
10261 case 1024:
10262 case 2048:
10263 break;
4b0e333e
CW
10264 }
10265
dc41c154
VS
10266 cntl |= CURSOR_ENABLE |
10267 CURSOR_GAMMA_ENABLE |
10268 CURSOR_FORMAT_ARGB |
10269 CURSOR_STRIDE(stride);
10270
10271 size = (height << 12) | width;
4b0e333e 10272 }
560b85bb 10273
dc41c154
VS
10274 if (intel_crtc->cursor_cntl != 0 &&
10275 (intel_crtc->cursor_base != base ||
10276 intel_crtc->cursor_size != size ||
10277 intel_crtc->cursor_cntl != cntl)) {
10278 /* On these chipsets we can only modify the base/size/stride
10279 * whilst the cursor is disabled.
10280 */
0b87c24e
VS
10281 I915_WRITE(CURCNTR(PIPE_A), 0);
10282 POSTING_READ(CURCNTR(PIPE_A));
dc41c154 10283 intel_crtc->cursor_cntl = 0;
4b0e333e 10284 }
560b85bb 10285
99d1f387 10286 if (intel_crtc->cursor_base != base) {
0b87c24e 10287 I915_WRITE(CURBASE(PIPE_A), base);
99d1f387
VS
10288 intel_crtc->cursor_base = base;
10289 }
4726e0b0 10290
dc41c154
VS
10291 if (intel_crtc->cursor_size != size) {
10292 I915_WRITE(CURSIZE, size);
10293 intel_crtc->cursor_size = size;
4b0e333e 10294 }
560b85bb 10295
4b0e333e 10296 if (intel_crtc->cursor_cntl != cntl) {
0b87c24e
VS
10297 I915_WRITE(CURCNTR(PIPE_A), cntl);
10298 POSTING_READ(CURCNTR(PIPE_A));
4b0e333e 10299 intel_crtc->cursor_cntl = cntl;
560b85bb 10300 }
560b85bb
CW
10301}
10302
55a08b3f
ML
10303static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base,
10304 const struct intel_plane_state *plane_state)
65a21cd6
JB
10305{
10306 struct drm_device *dev = crtc->dev;
10307 struct drm_i915_private *dev_priv = dev->dev_private;
10308 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10309 int pipe = intel_crtc->pipe;
663f3122 10310 uint32_t cntl = 0;
4b0e333e 10311
55a08b3f 10312 if (plane_state && plane_state->visible) {
4b0e333e 10313 cntl = MCURSOR_GAMMA_ENABLE;
55a08b3f 10314 switch (plane_state->base.crtc_w) {
4726e0b0
SK
10315 case 64:
10316 cntl |= CURSOR_MODE_64_ARGB_AX;
10317 break;
10318 case 128:
10319 cntl |= CURSOR_MODE_128_ARGB_AX;
10320 break;
10321 case 256:
10322 cntl |= CURSOR_MODE_256_ARGB_AX;
10323 break;
10324 default:
55a08b3f 10325 MISSING_CASE(plane_state->base.crtc_w);
4726e0b0 10326 return;
65a21cd6 10327 }
4b0e333e 10328 cntl |= pipe << 28; /* Connect to correct pipe */
47bf17a7 10329
fc6f93bc 10330 if (HAS_DDI(dev))
47bf17a7 10331 cntl |= CURSOR_PIPE_CSC_ENABLE;
65a21cd6 10332
55a08b3f
ML
10333 if (plane_state->base.rotation == BIT(DRM_ROTATE_180))
10334 cntl |= CURSOR_ROTATE_180;
10335 }
4398ad45 10336
4b0e333e
CW
10337 if (intel_crtc->cursor_cntl != cntl) {
10338 I915_WRITE(CURCNTR(pipe), cntl);
10339 POSTING_READ(CURCNTR(pipe));
10340 intel_crtc->cursor_cntl = cntl;
65a21cd6 10341 }
4b0e333e 10342
65a21cd6 10343 /* and commit changes on next vblank */
5efb3e28
VS
10344 I915_WRITE(CURBASE(pipe), base);
10345 POSTING_READ(CURBASE(pipe));
99d1f387
VS
10346
10347 intel_crtc->cursor_base = base;
65a21cd6
JB
10348}
10349
cda4b7d3 10350/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
6b383a7f 10351static void intel_crtc_update_cursor(struct drm_crtc *crtc,
55a08b3f 10352 const struct intel_plane_state *plane_state)
cda4b7d3
CW
10353{
10354 struct drm_device *dev = crtc->dev;
10355 struct drm_i915_private *dev_priv = dev->dev_private;
10356 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10357 int pipe = intel_crtc->pipe;
55a08b3f
ML
10358 u32 base = intel_crtc->cursor_addr;
10359 u32 pos = 0;
cda4b7d3 10360
55a08b3f
ML
10361 if (plane_state) {
10362 int x = plane_state->base.crtc_x;
10363 int y = plane_state->base.crtc_y;
cda4b7d3 10364
55a08b3f
ML
10365 if (x < 0) {
10366 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
10367 x = -x;
10368 }
10369 pos |= x << CURSOR_X_SHIFT;
cda4b7d3 10370
55a08b3f
ML
10371 if (y < 0) {
10372 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
10373 y = -y;
10374 }
10375 pos |= y << CURSOR_Y_SHIFT;
10376
10377 /* ILK+ do this automagically */
10378 if (HAS_GMCH_DISPLAY(dev) &&
10379 plane_state->base.rotation == BIT(DRM_ROTATE_180)) {
10380 base += (plane_state->base.crtc_h *
10381 plane_state->base.crtc_w - 1) * 4;
10382 }
cda4b7d3 10383 }
cda4b7d3 10384
5efb3e28
VS
10385 I915_WRITE(CURPOS(pipe), pos);
10386
8ac54669 10387 if (IS_845G(dev) || IS_I865G(dev))
55a08b3f 10388 i845_update_cursor(crtc, base, plane_state);
5efb3e28 10389 else
55a08b3f 10390 i9xx_update_cursor(crtc, base, plane_state);
cda4b7d3
CW
10391}
10392
dc41c154
VS
10393static bool cursor_size_ok(struct drm_device *dev,
10394 uint32_t width, uint32_t height)
10395{
10396 if (width == 0 || height == 0)
10397 return false;
10398
10399 /*
10400 * 845g/865g are special in that they are only limited by
10401 * the width of their cursors, the height is arbitrary up to
10402 * the precision of the register. Everything else requires
10403 * square cursors, limited to a few power-of-two sizes.
10404 */
10405 if (IS_845G(dev) || IS_I865G(dev)) {
10406 if ((width & 63) != 0)
10407 return false;
10408
10409 if (width > (IS_845G(dev) ? 64 : 512))
10410 return false;
10411
10412 if (height > 1023)
10413 return false;
10414 } else {
10415 switch (width | height) {
10416 case 256:
10417 case 128:
10418 if (IS_GEN2(dev))
10419 return false;
10420 case 64:
10421 break;
10422 default:
10423 return false;
10424 }
10425 }
10426
10427 return true;
10428}
10429
79e53945
JB
10430/* VESA 640x480x72Hz mode to set on the pipe */
10431static struct drm_display_mode load_detect_mode = {
10432 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
10433 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
10434};
10435
a8bb6818
DV
10436struct drm_framebuffer *
10437__intel_framebuffer_create(struct drm_device *dev,
10438 struct drm_mode_fb_cmd2 *mode_cmd,
10439 struct drm_i915_gem_object *obj)
d2dff872
CW
10440{
10441 struct intel_framebuffer *intel_fb;
10442 int ret;
10443
10444 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
dcb1394e 10445 if (!intel_fb)
d2dff872 10446 return ERR_PTR(-ENOMEM);
d2dff872
CW
10447
10448 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
dd4916c5
DV
10449 if (ret)
10450 goto err;
d2dff872
CW
10451
10452 return &intel_fb->base;
dcb1394e 10453
dd4916c5 10454err:
dd4916c5 10455 kfree(intel_fb);
dd4916c5 10456 return ERR_PTR(ret);
d2dff872
CW
10457}
10458
b5ea642a 10459static struct drm_framebuffer *
a8bb6818
DV
10460intel_framebuffer_create(struct drm_device *dev,
10461 struct drm_mode_fb_cmd2 *mode_cmd,
10462 struct drm_i915_gem_object *obj)
10463{
10464 struct drm_framebuffer *fb;
10465 int ret;
10466
10467 ret = i915_mutex_lock_interruptible(dev);
10468 if (ret)
10469 return ERR_PTR(ret);
10470 fb = __intel_framebuffer_create(dev, mode_cmd, obj);
10471 mutex_unlock(&dev->struct_mutex);
10472
10473 return fb;
10474}
10475
d2dff872
CW
10476static u32
10477intel_framebuffer_pitch_for_width(int width, int bpp)
10478{
10479 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
10480 return ALIGN(pitch, 64);
10481}
10482
10483static u32
10484intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
10485{
10486 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
1267a26b 10487 return PAGE_ALIGN(pitch * mode->vdisplay);
d2dff872
CW
10488}
10489
10490static struct drm_framebuffer *
10491intel_framebuffer_create_for_mode(struct drm_device *dev,
10492 struct drm_display_mode *mode,
10493 int depth, int bpp)
10494{
dcb1394e 10495 struct drm_framebuffer *fb;
d2dff872 10496 struct drm_i915_gem_object *obj;
0fed39bd 10497 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
d2dff872 10498
d37cd8a8 10499 obj = i915_gem_object_create(dev,
d2dff872 10500 intel_framebuffer_size_for_mode(mode, bpp));
fe3db79b
CW
10501 if (IS_ERR(obj))
10502 return ERR_CAST(obj);
d2dff872
CW
10503
10504 mode_cmd.width = mode->hdisplay;
10505 mode_cmd.height = mode->vdisplay;
308e5bcb
JB
10506 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
10507 bpp);
5ca0c34a 10508 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
d2dff872 10509
dcb1394e
LW
10510 fb = intel_framebuffer_create(dev, &mode_cmd, obj);
10511 if (IS_ERR(fb))
10512 drm_gem_object_unreference_unlocked(&obj->base);
10513
10514 return fb;
d2dff872
CW
10515}
10516
10517static struct drm_framebuffer *
10518mode_fits_in_fbdev(struct drm_device *dev,
10519 struct drm_display_mode *mode)
10520{
0695726e 10521#ifdef CONFIG_DRM_FBDEV_EMULATION
d2dff872
CW
10522 struct drm_i915_private *dev_priv = dev->dev_private;
10523 struct drm_i915_gem_object *obj;
10524 struct drm_framebuffer *fb;
10525
4c0e5528 10526 if (!dev_priv->fbdev)
d2dff872
CW
10527 return NULL;
10528
4c0e5528 10529 if (!dev_priv->fbdev->fb)
d2dff872
CW
10530 return NULL;
10531
4c0e5528
DV
10532 obj = dev_priv->fbdev->fb->obj;
10533 BUG_ON(!obj);
10534
8bcd4553 10535 fb = &dev_priv->fbdev->fb->base;
01f2c773
VS
10536 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
10537 fb->bits_per_pixel))
d2dff872
CW
10538 return NULL;
10539
01f2c773 10540 if (obj->base.size < mode->vdisplay * fb->pitches[0])
d2dff872
CW
10541 return NULL;
10542
edde3617 10543 drm_framebuffer_reference(fb);
d2dff872 10544 return fb;
4520f53a
DV
10545#else
10546 return NULL;
10547#endif
d2dff872
CW
10548}
10549
d3a40d1b
ACO
10550static int intel_modeset_setup_plane_state(struct drm_atomic_state *state,
10551 struct drm_crtc *crtc,
10552 struct drm_display_mode *mode,
10553 struct drm_framebuffer *fb,
10554 int x, int y)
10555{
10556 struct drm_plane_state *plane_state;
10557 int hdisplay, vdisplay;
10558 int ret;
10559
10560 plane_state = drm_atomic_get_plane_state(state, crtc->primary);
10561 if (IS_ERR(plane_state))
10562 return PTR_ERR(plane_state);
10563
10564 if (mode)
10565 drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
10566 else
10567 hdisplay = vdisplay = 0;
10568
10569 ret = drm_atomic_set_crtc_for_plane(plane_state, fb ? crtc : NULL);
10570 if (ret)
10571 return ret;
10572 drm_atomic_set_fb_for_plane(plane_state, fb);
10573 plane_state->crtc_x = 0;
10574 plane_state->crtc_y = 0;
10575 plane_state->crtc_w = hdisplay;
10576 plane_state->crtc_h = vdisplay;
10577 plane_state->src_x = x << 16;
10578 plane_state->src_y = y << 16;
10579 plane_state->src_w = hdisplay << 16;
10580 plane_state->src_h = vdisplay << 16;
10581
10582 return 0;
10583}
10584
d2434ab7 10585bool intel_get_load_detect_pipe(struct drm_connector *connector,
7173188d 10586 struct drm_display_mode *mode,
51fd371b
RC
10587 struct intel_load_detect_pipe *old,
10588 struct drm_modeset_acquire_ctx *ctx)
79e53945
JB
10589{
10590 struct intel_crtc *intel_crtc;
d2434ab7
DV
10591 struct intel_encoder *intel_encoder =
10592 intel_attached_encoder(connector);
79e53945 10593 struct drm_crtc *possible_crtc;
4ef69c7a 10594 struct drm_encoder *encoder = &intel_encoder->base;
79e53945
JB
10595 struct drm_crtc *crtc = NULL;
10596 struct drm_device *dev = encoder->dev;
94352cf9 10597 struct drm_framebuffer *fb;
51fd371b 10598 struct drm_mode_config *config = &dev->mode_config;
edde3617 10599 struct drm_atomic_state *state = NULL, *restore_state = NULL;
944b0c76 10600 struct drm_connector_state *connector_state;
4be07317 10601 struct intel_crtc_state *crtc_state;
51fd371b 10602 int ret, i = -1;
79e53945 10603
d2dff872 10604 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
c23cc417 10605 connector->base.id, connector->name,
8e329a03 10606 encoder->base.id, encoder->name);
d2dff872 10607
edde3617
ML
10608 old->restore_state = NULL;
10609
51fd371b
RC
10610retry:
10611 ret = drm_modeset_lock(&config->connection_mutex, ctx);
10612 if (ret)
ad3c558f 10613 goto fail;
6e9f798d 10614
79e53945
JB
10615 /*
10616 * Algorithm gets a little messy:
7a5e4805 10617 *
79e53945
JB
10618 * - if the connector already has an assigned crtc, use it (but make
10619 * sure it's on first)
7a5e4805 10620 *
79e53945
JB
10621 * - try to find the first unused crtc that can drive this connector,
10622 * and use that if we find one
79e53945
JB
10623 */
10624
10625 /* See if we already have a CRTC for this connector */
edde3617
ML
10626 if (connector->state->crtc) {
10627 crtc = connector->state->crtc;
8261b191 10628
51fd371b 10629 ret = drm_modeset_lock(&crtc->mutex, ctx);
4d02e2de 10630 if (ret)
ad3c558f 10631 goto fail;
8261b191
CW
10632
10633 /* Make sure the crtc and connector are running */
edde3617 10634 goto found;
79e53945
JB
10635 }
10636
10637 /* Find an unused one (if possible) */
70e1e0ec 10638 for_each_crtc(dev, possible_crtc) {
79e53945
JB
10639 i++;
10640 if (!(encoder->possible_crtcs & (1 << i)))
10641 continue;
edde3617
ML
10642
10643 ret = drm_modeset_lock(&possible_crtc->mutex, ctx);
10644 if (ret)
10645 goto fail;
10646
10647 if (possible_crtc->state->enable) {
10648 drm_modeset_unlock(&possible_crtc->mutex);
a459249c 10649 continue;
edde3617 10650 }
a459249c
VS
10651
10652 crtc = possible_crtc;
10653 break;
79e53945
JB
10654 }
10655
10656 /*
10657 * If we didn't find an unused CRTC, don't use any.
10658 */
10659 if (!crtc) {
7173188d 10660 DRM_DEBUG_KMS("no pipe available for load-detect\n");
ad3c558f 10661 goto fail;
79e53945
JB
10662 }
10663
edde3617
ML
10664found:
10665 intel_crtc = to_intel_crtc(crtc);
10666
4d02e2de
DV
10667 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
10668 if (ret)
ad3c558f 10669 goto fail;
79e53945 10670
83a57153 10671 state = drm_atomic_state_alloc(dev);
edde3617
ML
10672 restore_state = drm_atomic_state_alloc(dev);
10673 if (!state || !restore_state) {
10674 ret = -ENOMEM;
10675 goto fail;
10676 }
83a57153
ACO
10677
10678 state->acquire_ctx = ctx;
edde3617 10679 restore_state->acquire_ctx = ctx;
83a57153 10680
944b0c76
ACO
10681 connector_state = drm_atomic_get_connector_state(state, connector);
10682 if (IS_ERR(connector_state)) {
10683 ret = PTR_ERR(connector_state);
10684 goto fail;
10685 }
10686
edde3617
ML
10687 ret = drm_atomic_set_crtc_for_connector(connector_state, crtc);
10688 if (ret)
10689 goto fail;
944b0c76 10690
4be07317
ACO
10691 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
10692 if (IS_ERR(crtc_state)) {
10693 ret = PTR_ERR(crtc_state);
10694 goto fail;
10695 }
10696
49d6fa21 10697 crtc_state->base.active = crtc_state->base.enable = true;
4be07317 10698
6492711d
CW
10699 if (!mode)
10700 mode = &load_detect_mode;
79e53945 10701
d2dff872
CW
10702 /* We need a framebuffer large enough to accommodate all accesses
10703 * that the plane may generate whilst we perform load detection.
10704 * We can not rely on the fbcon either being present (we get called
10705 * during its initialisation to detect all boot displays, or it may
10706 * not even exist) or that it is large enough to satisfy the
10707 * requested mode.
10708 */
94352cf9
DV
10709 fb = mode_fits_in_fbdev(dev, mode);
10710 if (fb == NULL) {
d2dff872 10711 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
94352cf9 10712 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
d2dff872
CW
10713 } else
10714 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
94352cf9 10715 if (IS_ERR(fb)) {
d2dff872 10716 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
412b61d8 10717 goto fail;
79e53945 10718 }
79e53945 10719
d3a40d1b
ACO
10720 ret = intel_modeset_setup_plane_state(state, crtc, mode, fb, 0, 0);
10721 if (ret)
10722 goto fail;
10723
edde3617
ML
10724 drm_framebuffer_unreference(fb);
10725
10726 ret = drm_atomic_set_mode_for_crtc(&crtc_state->base, mode);
10727 if (ret)
10728 goto fail;
10729
10730 ret = PTR_ERR_OR_ZERO(drm_atomic_get_connector_state(restore_state, connector));
10731 if (!ret)
10732 ret = PTR_ERR_OR_ZERO(drm_atomic_get_crtc_state(restore_state, crtc));
10733 if (!ret)
10734 ret = PTR_ERR_OR_ZERO(drm_atomic_get_plane_state(restore_state, crtc->primary));
10735 if (ret) {
10736 DRM_DEBUG_KMS("Failed to create a copy of old state to restore: %i\n", ret);
10737 goto fail;
10738 }
8c7b5ccb 10739
3ba86073
ML
10740 ret = drm_atomic_commit(state);
10741 if (ret) {
6492711d 10742 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
412b61d8 10743 goto fail;
79e53945 10744 }
edde3617
ML
10745
10746 old->restore_state = restore_state;
7173188d 10747
79e53945 10748 /* let the connector get through one full cycle before testing */
9d0498a2 10749 intel_wait_for_vblank(dev, intel_crtc->pipe);
7173188d 10750 return true;
412b61d8 10751
ad3c558f 10752fail:
e5d958ef 10753 drm_atomic_state_free(state);
edde3617
ML
10754 drm_atomic_state_free(restore_state);
10755 restore_state = state = NULL;
83a57153 10756
51fd371b
RC
10757 if (ret == -EDEADLK) {
10758 drm_modeset_backoff(ctx);
10759 goto retry;
10760 }
10761
412b61d8 10762 return false;
79e53945
JB
10763}
10764
d2434ab7 10765void intel_release_load_detect_pipe(struct drm_connector *connector,
49172fee
ACO
10766 struct intel_load_detect_pipe *old,
10767 struct drm_modeset_acquire_ctx *ctx)
79e53945 10768{
d2434ab7
DV
10769 struct intel_encoder *intel_encoder =
10770 intel_attached_encoder(connector);
4ef69c7a 10771 struct drm_encoder *encoder = &intel_encoder->base;
edde3617 10772 struct drm_atomic_state *state = old->restore_state;
d3a40d1b 10773 int ret;
79e53945 10774
d2dff872 10775 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
c23cc417 10776 connector->base.id, connector->name,
8e329a03 10777 encoder->base.id, encoder->name);
d2dff872 10778
edde3617 10779 if (!state)
0622a53c 10780 return;
79e53945 10781
edde3617
ML
10782 ret = drm_atomic_commit(state);
10783 if (ret) {
10784 DRM_DEBUG_KMS("Couldn't release load detect pipe: %i\n", ret);
10785 drm_atomic_state_free(state);
10786 }
79e53945
JB
10787}
10788
da4a1efa 10789static int i9xx_pll_refclk(struct drm_device *dev,
5cec258b 10790 const struct intel_crtc_state *pipe_config)
da4a1efa
VS
10791{
10792 struct drm_i915_private *dev_priv = dev->dev_private;
10793 u32 dpll = pipe_config->dpll_hw_state.dpll;
10794
10795 if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
e91e941b 10796 return dev_priv->vbt.lvds_ssc_freq;
da4a1efa
VS
10797 else if (HAS_PCH_SPLIT(dev))
10798 return 120000;
10799 else if (!IS_GEN2(dev))
10800 return 96000;
10801 else
10802 return 48000;
10803}
10804
79e53945 10805/* Returns the clock of the currently programmed mode of the given pipe. */
f1f644dc 10806static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 10807 struct intel_crtc_state *pipe_config)
79e53945 10808{
f1f644dc 10809 struct drm_device *dev = crtc->base.dev;
79e53945 10810 struct drm_i915_private *dev_priv = dev->dev_private;
f1f644dc 10811 int pipe = pipe_config->cpu_transcoder;
293623f7 10812 u32 dpll = pipe_config->dpll_hw_state.dpll;
79e53945 10813 u32 fp;
9e2c8475 10814 struct dpll clock;
dccbea3b 10815 int port_clock;
da4a1efa 10816 int refclk = i9xx_pll_refclk(dev, pipe_config);
79e53945
JB
10817
10818 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
293623f7 10819 fp = pipe_config->dpll_hw_state.fp0;
79e53945 10820 else
293623f7 10821 fp = pipe_config->dpll_hw_state.fp1;
79e53945
JB
10822
10823 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
f2b115e6
AJ
10824 if (IS_PINEVIEW(dev)) {
10825 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
10826 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
2177832f
SL
10827 } else {
10828 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
10829 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
10830 }
10831
a6c45cf0 10832 if (!IS_GEN2(dev)) {
f2b115e6
AJ
10833 if (IS_PINEVIEW(dev))
10834 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
10835 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
2177832f
SL
10836 else
10837 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
79e53945
JB
10838 DPLL_FPA01_P1_POST_DIV_SHIFT);
10839
10840 switch (dpll & DPLL_MODE_MASK) {
10841 case DPLLB_MODE_DAC_SERIAL:
10842 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
10843 5 : 10;
10844 break;
10845 case DPLLB_MODE_LVDS:
10846 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
10847 7 : 14;
10848 break;
10849 default:
28c97730 10850 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
79e53945 10851 "mode\n", (int)(dpll & DPLL_MODE_MASK));
f1f644dc 10852 return;
79e53945
JB
10853 }
10854
ac58c3f0 10855 if (IS_PINEVIEW(dev))
dccbea3b 10856 port_clock = pnv_calc_dpll_params(refclk, &clock);
ac58c3f0 10857 else
dccbea3b 10858 port_clock = i9xx_calc_dpll_params(refclk, &clock);
79e53945 10859 } else {
0fb58223 10860 u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
b1c560d1 10861 bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
79e53945
JB
10862
10863 if (is_lvds) {
10864 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
10865 DPLL_FPA01_P1_POST_DIV_SHIFT);
b1c560d1
VS
10866
10867 if (lvds & LVDS_CLKB_POWER_UP)
10868 clock.p2 = 7;
10869 else
10870 clock.p2 = 14;
79e53945
JB
10871 } else {
10872 if (dpll & PLL_P1_DIVIDE_BY_TWO)
10873 clock.p1 = 2;
10874 else {
10875 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
10876 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
10877 }
10878 if (dpll & PLL_P2_DIVIDE_BY_4)
10879 clock.p2 = 4;
10880 else
10881 clock.p2 = 2;
79e53945 10882 }
da4a1efa 10883
dccbea3b 10884 port_clock = i9xx_calc_dpll_params(refclk, &clock);
79e53945
JB
10885 }
10886
18442d08
VS
10887 /*
10888 * This value includes pixel_multiplier. We will use
241bfc38 10889 * port_clock to compute adjusted_mode.crtc_clock in the
18442d08
VS
10890 * encoder's get_config() function.
10891 */
dccbea3b 10892 pipe_config->port_clock = port_clock;
f1f644dc
JB
10893}
10894
6878da05
VS
10895int intel_dotclock_calculate(int link_freq,
10896 const struct intel_link_m_n *m_n)
f1f644dc 10897{
f1f644dc
JB
10898 /*
10899 * The calculation for the data clock is:
1041a02f 10900 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
f1f644dc 10901 * But we want to avoid losing precison if possible, so:
1041a02f 10902 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
f1f644dc
JB
10903 *
10904 * and the link clock is simpler:
1041a02f 10905 * link_clock = (m * link_clock) / n
f1f644dc
JB
10906 */
10907
6878da05
VS
10908 if (!m_n->link_n)
10909 return 0;
f1f644dc 10910
6878da05
VS
10911 return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
10912}
f1f644dc 10913
18442d08 10914static void ironlake_pch_clock_get(struct intel_crtc *crtc,
5cec258b 10915 struct intel_crtc_state *pipe_config)
6878da05 10916{
e3b247da 10917 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
79e53945 10918
18442d08
VS
10919 /* read out port_clock from the DPLL */
10920 i9xx_crtc_clock_get(crtc, pipe_config);
f1f644dc 10921
f1f644dc 10922 /*
e3b247da
VS
10923 * In case there is an active pipe without active ports,
10924 * we may need some idea for the dotclock anyway.
10925 * Calculate one based on the FDI configuration.
79e53945 10926 */
2d112de7 10927 pipe_config->base.adjusted_mode.crtc_clock =
21a727b3 10928 intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
18442d08 10929 &pipe_config->fdi_m_n);
79e53945
JB
10930}
10931
10932/** Returns the currently programmed mode of the given pipe. */
10933struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
10934 struct drm_crtc *crtc)
10935{
548f245b 10936 struct drm_i915_private *dev_priv = dev->dev_private;
79e53945 10937 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6e3c9717 10938 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
79e53945 10939 struct drm_display_mode *mode;
3f36b937 10940 struct intel_crtc_state *pipe_config;
fe2b8f9d
PZ
10941 int htot = I915_READ(HTOTAL(cpu_transcoder));
10942 int hsync = I915_READ(HSYNC(cpu_transcoder));
10943 int vtot = I915_READ(VTOTAL(cpu_transcoder));
10944 int vsync = I915_READ(VSYNC(cpu_transcoder));
293623f7 10945 enum pipe pipe = intel_crtc->pipe;
79e53945
JB
10946
10947 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
10948 if (!mode)
10949 return NULL;
10950
3f36b937
TU
10951 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
10952 if (!pipe_config) {
10953 kfree(mode);
10954 return NULL;
10955 }
10956
f1f644dc
JB
10957 /*
10958 * Construct a pipe_config sufficient for getting the clock info
10959 * back out of crtc_clock_get.
10960 *
10961 * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
10962 * to use a real value here instead.
10963 */
3f36b937
TU
10964 pipe_config->cpu_transcoder = (enum transcoder) pipe;
10965 pipe_config->pixel_multiplier = 1;
10966 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(pipe));
10967 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(pipe));
10968 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(pipe));
10969 i9xx_crtc_clock_get(intel_crtc, pipe_config);
10970
10971 mode->clock = pipe_config->port_clock / pipe_config->pixel_multiplier;
79e53945
JB
10972 mode->hdisplay = (htot & 0xffff) + 1;
10973 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
10974 mode->hsync_start = (hsync & 0xffff) + 1;
10975 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
10976 mode->vdisplay = (vtot & 0xffff) + 1;
10977 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
10978 mode->vsync_start = (vsync & 0xffff) + 1;
10979 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
10980
10981 drm_mode_set_name(mode);
79e53945 10982
3f36b937
TU
10983 kfree(pipe_config);
10984
79e53945
JB
10985 return mode;
10986}
10987
7d993739 10988void intel_mark_busy(struct drm_i915_private *dev_priv)
f047e395 10989{
f62a0076
CW
10990 if (dev_priv->mm.busy)
10991 return;
10992
43694d69 10993 intel_runtime_pm_get(dev_priv);
c67a470b 10994 i915_update_gfx_val(dev_priv);
7d993739 10995 if (INTEL_GEN(dev_priv) >= 6)
43cf3bf0 10996 gen6_rps_busy(dev_priv);
f62a0076 10997 dev_priv->mm.busy = true;
f047e395
CW
10998}
10999
7d993739 11000void intel_mark_idle(struct drm_i915_private *dev_priv)
652c393a 11001{
f62a0076
CW
11002 if (!dev_priv->mm.busy)
11003 return;
11004
11005 dev_priv->mm.busy = false;
11006
7d993739
TU
11007 if (INTEL_GEN(dev_priv) >= 6)
11008 gen6_rps_idle(dev_priv);
bb4cdd53 11009
43694d69 11010 intel_runtime_pm_put(dev_priv);
652c393a
JB
11011}
11012
79e53945
JB
11013static void intel_crtc_destroy(struct drm_crtc *crtc)
11014{
11015 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
67e77c5a 11016 struct drm_device *dev = crtc->dev;
51cbaf01 11017 struct intel_flip_work *work;
67e77c5a 11018
5e2d7afc 11019 spin_lock_irq(&dev->event_lock);
5a21b665
DV
11020 work = intel_crtc->flip_work;
11021 intel_crtc->flip_work = NULL;
11022 spin_unlock_irq(&dev->event_lock);
67e77c5a 11023
5a21b665 11024 if (work) {
51cbaf01
ML
11025 cancel_work_sync(&work->mmio_work);
11026 cancel_work_sync(&work->unpin_work);
5a21b665 11027 kfree(work);
67e77c5a 11028 }
79e53945
JB
11029
11030 drm_crtc_cleanup(crtc);
67e77c5a 11031
79e53945
JB
11032 kfree(intel_crtc);
11033}
11034
6b95a207
KH
11035static void intel_unpin_work_fn(struct work_struct *__work)
11036{
51cbaf01
ML
11037 struct intel_flip_work *work =
11038 container_of(__work, struct intel_flip_work, unpin_work);
5a21b665
DV
11039 struct intel_crtc *crtc = to_intel_crtc(work->crtc);
11040 struct drm_device *dev = crtc->base.dev;
11041 struct drm_plane *primary = crtc->base.primary;
03f476e1 11042
5a21b665
DV
11043 if (is_mmio_work(work))
11044 flush_work(&work->mmio_work);
03f476e1 11045
5a21b665
DV
11046 mutex_lock(&dev->struct_mutex);
11047 intel_unpin_fb_obj(work->old_fb, primary->state->rotation);
11048 drm_gem_object_unreference(&work->pending_flip_obj->base);
143f73b3 11049
5a21b665
DV
11050 if (work->flip_queued_req)
11051 i915_gem_request_assign(&work->flip_queued_req, NULL);
11052 mutex_unlock(&dev->struct_mutex);
143f73b3 11053
5a21b665
DV
11054 intel_frontbuffer_flip_complete(dev, to_intel_plane(primary)->frontbuffer_bit);
11055 intel_fbc_post_update(crtc);
11056 drm_framebuffer_unreference(work->old_fb);
143f73b3 11057
5a21b665
DV
11058 BUG_ON(atomic_read(&crtc->unpin_work_count) == 0);
11059 atomic_dec(&crtc->unpin_work_count);
a6747b73 11060
5a21b665
DV
11061 kfree(work);
11062}
d9e86c0e 11063
5a21b665
DV
11064/* Is 'a' after or equal to 'b'? */
11065static bool g4x_flip_count_after_eq(u32 a, u32 b)
11066{
11067 return !((a - b) & 0x80000000);
11068}
143f73b3 11069
5a21b665
DV
11070static bool __pageflip_finished_cs(struct intel_crtc *crtc,
11071 struct intel_flip_work *work)
11072{
11073 struct drm_device *dev = crtc->base.dev;
11074 struct drm_i915_private *dev_priv = dev->dev_private;
11075 unsigned reset_counter;
143f73b3 11076
5a21b665
DV
11077 reset_counter = i915_reset_counter(&dev_priv->gpu_error);
11078 if (crtc->reset_counter != reset_counter)
11079 return true;
143f73b3 11080
5a21b665
DV
11081 /*
11082 * The relevant registers doen't exist on pre-ctg.
11083 * As the flip done interrupt doesn't trigger for mmio
11084 * flips on gmch platforms, a flip count check isn't
11085 * really needed there. But since ctg has the registers,
11086 * include it in the check anyway.
11087 */
11088 if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
11089 return true;
b4a98e57 11090
5a21b665
DV
11091 /*
11092 * BDW signals flip done immediately if the plane
11093 * is disabled, even if the plane enable is already
11094 * armed to occur at the next vblank :(
11095 */
f99d7069 11096
5a21b665
DV
11097 /*
11098 * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
11099 * used the same base address. In that case the mmio flip might
11100 * have completed, but the CS hasn't even executed the flip yet.
11101 *
11102 * A flip count check isn't enough as the CS might have updated
11103 * the base address just after start of vblank, but before we
11104 * managed to process the interrupt. This means we'd complete the
11105 * CS flip too soon.
11106 *
11107 * Combining both checks should get us a good enough result. It may
11108 * still happen that the CS flip has been executed, but has not
11109 * yet actually completed. But in case the base address is the same
11110 * anyway, we don't really care.
11111 */
11112 return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
11113 crtc->flip_work->gtt_offset &&
11114 g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_G4X(crtc->pipe)),
11115 crtc->flip_work->flip_count);
11116}
b4a98e57 11117
5a21b665
DV
11118static bool
11119__pageflip_finished_mmio(struct intel_crtc *crtc,
11120 struct intel_flip_work *work)
11121{
11122 /*
11123 * MMIO work completes when vblank is different from
11124 * flip_queued_vblank.
11125 *
11126 * Reset counter value doesn't matter, this is handled by
11127 * i915_wait_request finishing early, so no need to handle
11128 * reset here.
11129 */
11130 return intel_crtc_get_vblank_counter(crtc) != work->flip_queued_vblank;
6b95a207
KH
11131}
11132
51cbaf01
ML
11133
11134static bool pageflip_finished(struct intel_crtc *crtc,
11135 struct intel_flip_work *work)
11136{
11137 if (!atomic_read(&work->pending))
11138 return false;
11139
11140 smp_rmb();
11141
5a21b665
DV
11142 if (is_mmio_work(work))
11143 return __pageflip_finished_mmio(crtc, work);
11144 else
11145 return __pageflip_finished_cs(crtc, work);
11146}
11147
11148void intel_finish_page_flip_cs(struct drm_i915_private *dev_priv, int pipe)
11149{
11150 struct drm_device *dev = dev_priv->dev;
11151 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
11152 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11153 struct intel_flip_work *work;
11154 unsigned long flags;
11155
11156 /* Ignore early vblank irqs */
11157 if (!crtc)
11158 return;
11159
51cbaf01 11160 /*
5a21b665
DV
11161 * This is called both by irq handlers and the reset code (to complete
11162 * lost pageflips) so needs the full irqsave spinlocks.
51cbaf01 11163 */
5a21b665
DV
11164 spin_lock_irqsave(&dev->event_lock, flags);
11165 work = intel_crtc->flip_work;
11166
11167 if (work != NULL &&
11168 !is_mmio_work(work) &&
11169 pageflip_finished(intel_crtc, work))
11170 page_flip_completed(intel_crtc);
11171
11172 spin_unlock_irqrestore(&dev->event_lock, flags);
75f7f3ec
VS
11173}
11174
51cbaf01 11175void intel_finish_page_flip_mmio(struct drm_i915_private *dev_priv, int pipe)
6b95a207 11176{
91d14251 11177 struct drm_device *dev = dev_priv->dev;
5251f04e
ML
11178 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
11179 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
51cbaf01 11180 struct intel_flip_work *work;
6b95a207
KH
11181 unsigned long flags;
11182
5251f04e
ML
11183 /* Ignore early vblank irqs */
11184 if (!crtc)
11185 return;
f326038a
DV
11186
11187 /*
11188 * This is called both by irq handlers and the reset code (to complete
11189 * lost pageflips) so needs the full irqsave spinlocks.
e7d841ca 11190 */
6b95a207 11191 spin_lock_irqsave(&dev->event_lock, flags);
5a21b665 11192 work = intel_crtc->flip_work;
5251f04e 11193
5a21b665
DV
11194 if (work != NULL &&
11195 is_mmio_work(work) &&
11196 pageflip_finished(intel_crtc, work))
11197 page_flip_completed(intel_crtc);
5251f04e 11198
6b95a207
KH
11199 spin_unlock_irqrestore(&dev->event_lock, flags);
11200}
11201
5a21b665
DV
11202static inline void intel_mark_page_flip_active(struct intel_crtc *crtc,
11203 struct intel_flip_work *work)
84c33a64 11204{
5a21b665 11205 work->flip_queued_vblank = intel_crtc_get_vblank_counter(crtc);
84c33a64 11206
5a21b665
DV
11207 /* Ensure that the work item is consistent when activating it ... */
11208 smp_mb__before_atomic();
11209 atomic_set(&work->pending, 1);
11210}
a6747b73 11211
5a21b665
DV
11212static int intel_gen2_queue_flip(struct drm_device *dev,
11213 struct drm_crtc *crtc,
11214 struct drm_framebuffer *fb,
11215 struct drm_i915_gem_object *obj,
11216 struct drm_i915_gem_request *req,
11217 uint32_t flags)
11218{
11219 struct intel_engine_cs *engine = req->engine;
11220 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11221 u32 flip_mask;
11222 int ret;
143f73b3 11223
5a21b665
DV
11224 ret = intel_ring_begin(req, 6);
11225 if (ret)
11226 return ret;
143f73b3 11227
5a21b665
DV
11228 /* Can't queue multiple flips, so wait for the previous
11229 * one to finish before executing the next.
11230 */
11231 if (intel_crtc->plane)
11232 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
11233 else
11234 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
11235 intel_ring_emit(engine, MI_WAIT_FOR_EVENT | flip_mask);
11236 intel_ring_emit(engine, MI_NOOP);
11237 intel_ring_emit(engine, MI_DISPLAY_FLIP |
11238 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
11239 intel_ring_emit(engine, fb->pitches[0]);
11240 intel_ring_emit(engine, intel_crtc->flip_work->gtt_offset);
11241 intel_ring_emit(engine, 0); /* aux display base address, unused */
143f73b3 11242
5a21b665
DV
11243 return 0;
11244}
84c33a64 11245
5a21b665
DV
11246static int intel_gen3_queue_flip(struct drm_device *dev,
11247 struct drm_crtc *crtc,
11248 struct drm_framebuffer *fb,
11249 struct drm_i915_gem_object *obj,
11250 struct drm_i915_gem_request *req,
11251 uint32_t flags)
11252{
11253 struct intel_engine_cs *engine = req->engine;
11254 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11255 u32 flip_mask;
11256 int ret;
d55dbd06 11257
5a21b665
DV
11258 ret = intel_ring_begin(req, 6);
11259 if (ret)
11260 return ret;
d55dbd06 11261
5a21b665
DV
11262 if (intel_crtc->plane)
11263 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
11264 else
11265 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
11266 intel_ring_emit(engine, MI_WAIT_FOR_EVENT | flip_mask);
11267 intel_ring_emit(engine, MI_NOOP);
11268 intel_ring_emit(engine, MI_DISPLAY_FLIP_I915 |
11269 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
11270 intel_ring_emit(engine, fb->pitches[0]);
11271 intel_ring_emit(engine, intel_crtc->flip_work->gtt_offset);
11272 intel_ring_emit(engine, MI_NOOP);
fd8e058a 11273
5a21b665
DV
11274 return 0;
11275}
84c33a64 11276
5a21b665
DV
11277static int intel_gen4_queue_flip(struct drm_device *dev,
11278 struct drm_crtc *crtc,
11279 struct drm_framebuffer *fb,
11280 struct drm_i915_gem_object *obj,
11281 struct drm_i915_gem_request *req,
11282 uint32_t flags)
11283{
11284 struct intel_engine_cs *engine = req->engine;
11285 struct drm_i915_private *dev_priv = dev->dev_private;
11286 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11287 uint32_t pf, pipesrc;
11288 int ret;
143f73b3 11289
5a21b665
DV
11290 ret = intel_ring_begin(req, 4);
11291 if (ret)
11292 return ret;
143f73b3 11293
5a21b665
DV
11294 /* i965+ uses the linear or tiled offsets from the
11295 * Display Registers (which do not change across a page-flip)
11296 * so we need only reprogram the base address.
11297 */
11298 intel_ring_emit(engine, MI_DISPLAY_FLIP |
11299 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
11300 intel_ring_emit(engine, fb->pitches[0]);
11301 intel_ring_emit(engine, intel_crtc->flip_work->gtt_offset |
11302 obj->tiling_mode);
11303
11304 /* XXX Enabling the panel-fitter across page-flip is so far
11305 * untested on non-native modes, so ignore it for now.
11306 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
11307 */
11308 pf = 0;
11309 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
11310 intel_ring_emit(engine, pf | pipesrc);
143f73b3 11311
5a21b665 11312 return 0;
8c9f3aaf
JB
11313}
11314
5a21b665
DV
11315static int intel_gen6_queue_flip(struct drm_device *dev,
11316 struct drm_crtc *crtc,
11317 struct drm_framebuffer *fb,
11318 struct drm_i915_gem_object *obj,
11319 struct drm_i915_gem_request *req,
11320 uint32_t flags)
da20eabd 11321{
5a21b665
DV
11322 struct intel_engine_cs *engine = req->engine;
11323 struct drm_i915_private *dev_priv = dev->dev_private;
11324 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11325 uint32_t pf, pipesrc;
11326 int ret;
d21fbe87 11327
5a21b665
DV
11328 ret = intel_ring_begin(req, 4);
11329 if (ret)
11330 return ret;
92826fcd 11331
5a21b665
DV
11332 intel_ring_emit(engine, MI_DISPLAY_FLIP |
11333 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
11334 intel_ring_emit(engine, fb->pitches[0] | obj->tiling_mode);
11335 intel_ring_emit(engine, intel_crtc->flip_work->gtt_offset);
92826fcd 11336
5a21b665
DV
11337 /* Contrary to the suggestions in the documentation,
11338 * "Enable Panel Fitter" does not seem to be required when page
11339 * flipping with a non-native mode, and worse causes a normal
11340 * modeset to fail.
11341 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
11342 */
11343 pf = 0;
11344 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
11345 intel_ring_emit(engine, pf | pipesrc);
7809e5ae 11346
5a21b665 11347 return 0;
7809e5ae
MR
11348}
11349
5a21b665
DV
11350static int intel_gen7_queue_flip(struct drm_device *dev,
11351 struct drm_crtc *crtc,
11352 struct drm_framebuffer *fb,
11353 struct drm_i915_gem_object *obj,
11354 struct drm_i915_gem_request *req,
11355 uint32_t flags)
d21fbe87 11356{
5a21b665
DV
11357 struct intel_engine_cs *engine = req->engine;
11358 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11359 uint32_t plane_bit = 0;
11360 int len, ret;
d21fbe87 11361
5a21b665
DV
11362 switch (intel_crtc->plane) {
11363 case PLANE_A:
11364 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
11365 break;
11366 case PLANE_B:
11367 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
11368 break;
11369 case PLANE_C:
11370 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
11371 break;
11372 default:
11373 WARN_ONCE(1, "unknown plane in flip command\n");
11374 return -ENODEV;
11375 }
11376
11377 len = 4;
11378 if (engine->id == RCS) {
11379 len += 6;
11380 /*
11381 * On Gen 8, SRM is now taking an extra dword to accommodate
11382 * 48bits addresses, and we need a NOOP for the batch size to
11383 * stay even.
11384 */
11385 if (IS_GEN8(dev))
11386 len += 2;
11387 }
11388
11389 /*
11390 * BSpec MI_DISPLAY_FLIP for IVB:
11391 * "The full packet must be contained within the same cache line."
11392 *
11393 * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
11394 * cacheline, if we ever start emitting more commands before
11395 * the MI_DISPLAY_FLIP we may need to first emit everything else,
11396 * then do the cacheline alignment, and finally emit the
11397 * MI_DISPLAY_FLIP.
11398 */
11399 ret = intel_ring_cacheline_align(req);
11400 if (ret)
11401 return ret;
11402
11403 ret = intel_ring_begin(req, len);
11404 if (ret)
11405 return ret;
11406
11407 /* Unmask the flip-done completion message. Note that the bspec says that
11408 * we should do this for both the BCS and RCS, and that we must not unmask
11409 * more than one flip event at any time (or ensure that one flip message
11410 * can be sent by waiting for flip-done prior to queueing new flips).
11411 * Experimentation says that BCS works despite DERRMR masking all
11412 * flip-done completion events and that unmasking all planes at once
11413 * for the RCS also doesn't appear to drop events. Setting the DERRMR
11414 * to zero does lead to lockups within MI_DISPLAY_FLIP.
11415 */
11416 if (engine->id == RCS) {
11417 intel_ring_emit(engine, MI_LOAD_REGISTER_IMM(1));
11418 intel_ring_emit_reg(engine, DERRMR);
11419 intel_ring_emit(engine, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
11420 DERRMR_PIPEB_PRI_FLIP_DONE |
11421 DERRMR_PIPEC_PRI_FLIP_DONE));
11422 if (IS_GEN8(dev))
11423 intel_ring_emit(engine, MI_STORE_REGISTER_MEM_GEN8 |
11424 MI_SRM_LRM_GLOBAL_GTT);
11425 else
11426 intel_ring_emit(engine, MI_STORE_REGISTER_MEM |
11427 MI_SRM_LRM_GLOBAL_GTT);
11428 intel_ring_emit_reg(engine, DERRMR);
11429 intel_ring_emit(engine, engine->scratch.gtt_offset + 256);
11430 if (IS_GEN8(dev)) {
11431 intel_ring_emit(engine, 0);
11432 intel_ring_emit(engine, MI_NOOP);
11433 }
11434 }
11435
11436 intel_ring_emit(engine, MI_DISPLAY_FLIP_I915 | plane_bit);
11437 intel_ring_emit(engine, (fb->pitches[0] | obj->tiling_mode));
11438 intel_ring_emit(engine, intel_crtc->flip_work->gtt_offset);
11439 intel_ring_emit(engine, (MI_NOOP));
11440
11441 return 0;
11442}
11443
11444static bool use_mmio_flip(struct intel_engine_cs *engine,
11445 struct drm_i915_gem_object *obj)
11446{
c37efb99
CW
11447 struct reservation_object *resv;
11448
5a21b665
DV
11449 /*
11450 * This is not being used for older platforms, because
11451 * non-availability of flip done interrupt forces us to use
11452 * CS flips. Older platforms derive flip done using some clever
11453 * tricks involving the flip_pending status bits and vblank irqs.
11454 * So using MMIO flips there would disrupt this mechanism.
11455 */
11456
11457 if (engine == NULL)
11458 return true;
11459
11460 if (INTEL_GEN(engine->i915) < 5)
11461 return false;
11462
11463 if (i915.use_mmio_flip < 0)
11464 return false;
11465 else if (i915.use_mmio_flip > 0)
11466 return true;
11467 else if (i915.enable_execlists)
11468 return true;
c37efb99
CW
11469
11470 resv = i915_gem_object_get_dmabuf_resv(obj);
11471 if (resv && !reservation_object_test_signaled_rcu(resv, false))
5a21b665 11472 return true;
c37efb99
CW
11473
11474 return engine != i915_gem_request_get_engine(obj->last_write_req);
5a21b665
DV
11475}
11476
11477static void skl_do_mmio_flip(struct intel_crtc *intel_crtc,
11478 unsigned int rotation,
11479 struct intel_flip_work *work)
11480{
11481 struct drm_device *dev = intel_crtc->base.dev;
11482 struct drm_i915_private *dev_priv = dev->dev_private;
11483 struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
11484 const enum pipe pipe = intel_crtc->pipe;
11485 u32 ctl, stride, tile_height;
11486
11487 ctl = I915_READ(PLANE_CTL(pipe, 0));
11488 ctl &= ~PLANE_CTL_TILED_MASK;
11489 switch (fb->modifier[0]) {
11490 case DRM_FORMAT_MOD_NONE:
11491 break;
11492 case I915_FORMAT_MOD_X_TILED:
11493 ctl |= PLANE_CTL_TILED_X;
11494 break;
11495 case I915_FORMAT_MOD_Y_TILED:
11496 ctl |= PLANE_CTL_TILED_Y;
11497 break;
11498 case I915_FORMAT_MOD_Yf_TILED:
11499 ctl |= PLANE_CTL_TILED_YF;
11500 break;
11501 default:
11502 MISSING_CASE(fb->modifier[0]);
11503 }
11504
11505 /*
11506 * The stride is either expressed as a multiple of 64 bytes chunks for
11507 * linear buffers or in number of tiles for tiled buffers.
11508 */
11509 if (intel_rotation_90_or_270(rotation)) {
11510 /* stride = Surface height in tiles */
11511 tile_height = intel_tile_height(dev_priv, fb->modifier[0], 0);
11512 stride = DIV_ROUND_UP(fb->height, tile_height);
11513 } else {
11514 stride = fb->pitches[0] /
11515 intel_fb_stride_alignment(dev_priv, fb->modifier[0],
11516 fb->pixel_format);
11517 }
11518
11519 /*
11520 * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
11521 * PLANE_SURF updates, the update is then guaranteed to be atomic.
11522 */
11523 I915_WRITE(PLANE_CTL(pipe, 0), ctl);
11524 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
11525
11526 I915_WRITE(PLANE_SURF(pipe, 0), work->gtt_offset);
11527 POSTING_READ(PLANE_SURF(pipe, 0));
11528}
11529
11530static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc,
11531 struct intel_flip_work *work)
11532{
11533 struct drm_device *dev = intel_crtc->base.dev;
11534 struct drm_i915_private *dev_priv = dev->dev_private;
11535 struct intel_framebuffer *intel_fb =
11536 to_intel_framebuffer(intel_crtc->base.primary->fb);
11537 struct drm_i915_gem_object *obj = intel_fb->obj;
11538 i915_reg_t reg = DSPCNTR(intel_crtc->plane);
11539 u32 dspcntr;
11540
11541 dspcntr = I915_READ(reg);
11542
11543 if (obj->tiling_mode != I915_TILING_NONE)
11544 dspcntr |= DISPPLANE_TILED;
11545 else
11546 dspcntr &= ~DISPPLANE_TILED;
11547
11548 I915_WRITE(reg, dspcntr);
11549
11550 I915_WRITE(DSPSURF(intel_crtc->plane), work->gtt_offset);
11551 POSTING_READ(DSPSURF(intel_crtc->plane));
11552}
11553
11554static void intel_mmio_flip_work_func(struct work_struct *w)
11555{
11556 struct intel_flip_work *work =
11557 container_of(w, struct intel_flip_work, mmio_work);
11558 struct intel_crtc *crtc = to_intel_crtc(work->crtc);
11559 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
11560 struct intel_framebuffer *intel_fb =
11561 to_intel_framebuffer(crtc->base.primary->fb);
11562 struct drm_i915_gem_object *obj = intel_fb->obj;
c37efb99 11563 struct reservation_object *resv;
5a21b665
DV
11564
11565 if (work->flip_queued_req)
11566 WARN_ON(__i915_wait_request(work->flip_queued_req,
11567 false, NULL,
11568 &dev_priv->rps.mmioflips));
11569
11570 /* For framebuffer backed by dmabuf, wait for fence */
c37efb99
CW
11571 resv = i915_gem_object_get_dmabuf_resv(obj);
11572 if (resv)
11573 WARN_ON(reservation_object_wait_timeout_rcu(resv, false, false,
5a21b665
DV
11574 MAX_SCHEDULE_TIMEOUT) < 0);
11575
11576 intel_pipe_update_start(crtc);
11577
11578 if (INTEL_GEN(dev_priv) >= 9)
11579 skl_do_mmio_flip(crtc, work->rotation, work);
11580 else
11581 /* use_mmio_flip() retricts MMIO flips to ilk+ */
11582 ilk_do_mmio_flip(crtc, work);
11583
11584 intel_pipe_update_end(crtc, work);
11585}
11586
11587static int intel_default_queue_flip(struct drm_device *dev,
11588 struct drm_crtc *crtc,
11589 struct drm_framebuffer *fb,
11590 struct drm_i915_gem_object *obj,
11591 struct drm_i915_gem_request *req,
11592 uint32_t flags)
11593{
11594 return -ENODEV;
11595}
11596
11597static bool __pageflip_stall_check_cs(struct drm_i915_private *dev_priv,
11598 struct intel_crtc *intel_crtc,
11599 struct intel_flip_work *work)
11600{
11601 u32 addr, vblank;
11602
11603 if (!atomic_read(&work->pending))
11604 return false;
11605
11606 smp_rmb();
11607
11608 vblank = intel_crtc_get_vblank_counter(intel_crtc);
11609 if (work->flip_ready_vblank == 0) {
11610 if (work->flip_queued_req &&
11611 !i915_gem_request_completed(work->flip_queued_req, true))
11612 return false;
11613
11614 work->flip_ready_vblank = vblank;
11615 }
11616
11617 if (vblank - work->flip_ready_vblank < 3)
11618 return false;
11619
11620 /* Potential stall - if we see that the flip has happened,
11621 * assume a missed interrupt. */
11622 if (INTEL_GEN(dev_priv) >= 4)
11623 addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
11624 else
11625 addr = I915_READ(DSPADDR(intel_crtc->plane));
11626
11627 /* There is a potential issue here with a false positive after a flip
11628 * to the same address. We could address this by checking for a
11629 * non-incrementing frame counter.
11630 */
11631 return addr == work->gtt_offset;
11632}
11633
11634void intel_check_page_flip(struct drm_i915_private *dev_priv, int pipe)
11635{
11636 struct drm_device *dev = dev_priv->dev;
11637 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
11638 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11639 struct intel_flip_work *work;
11640
11641 WARN_ON(!in_interrupt());
11642
11643 if (crtc == NULL)
11644 return;
11645
11646 spin_lock(&dev->event_lock);
11647 work = intel_crtc->flip_work;
11648
11649 if (work != NULL && !is_mmio_work(work) &&
11650 __pageflip_stall_check_cs(dev_priv, intel_crtc, work)) {
11651 WARN_ONCE(1,
11652 "Kicking stuck page flip: queued at %d, now %d\n",
11653 work->flip_queued_vblank, intel_crtc_get_vblank_counter(intel_crtc));
11654 page_flip_completed(intel_crtc);
11655 work = NULL;
11656 }
11657
11658 if (work != NULL && !is_mmio_work(work) &&
11659 intel_crtc_get_vblank_counter(intel_crtc) - work->flip_queued_vblank > 1)
11660 intel_queue_rps_boost_for_request(work->flip_queued_req);
11661 spin_unlock(&dev->event_lock);
11662}
11663
11664static int intel_crtc_page_flip(struct drm_crtc *crtc,
11665 struct drm_framebuffer *fb,
11666 struct drm_pending_vblank_event *event,
11667 uint32_t page_flip_flags)
11668{
11669 struct drm_device *dev = crtc->dev;
11670 struct drm_i915_private *dev_priv = dev->dev_private;
11671 struct drm_framebuffer *old_fb = crtc->primary->fb;
11672 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
11673 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11674 struct drm_plane *primary = crtc->primary;
11675 enum pipe pipe = intel_crtc->pipe;
11676 struct intel_flip_work *work;
11677 struct intel_engine_cs *engine;
11678 bool mmio_flip;
11679 struct drm_i915_gem_request *request = NULL;
11680 int ret;
11681
11682 /*
11683 * drm_mode_page_flip_ioctl() should already catch this, but double
11684 * check to be safe. In the future we may enable pageflipping from
11685 * a disabled primary plane.
11686 */
11687 if (WARN_ON(intel_fb_obj(old_fb) == NULL))
11688 return -EBUSY;
11689
11690 /* Can't change pixel format via MI display flips. */
11691 if (fb->pixel_format != crtc->primary->fb->pixel_format)
11692 return -EINVAL;
11693
11694 /*
11695 * TILEOFF/LINOFF registers can't be changed via MI display flips.
11696 * Note that pitch changes could also affect these register.
11697 */
11698 if (INTEL_INFO(dev)->gen > 3 &&
11699 (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
11700 fb->pitches[0] != crtc->primary->fb->pitches[0]))
11701 return -EINVAL;
11702
11703 if (i915_terminally_wedged(&dev_priv->gpu_error))
11704 goto out_hang;
11705
11706 work = kzalloc(sizeof(*work), GFP_KERNEL);
11707 if (work == NULL)
11708 return -ENOMEM;
11709
11710 work->event = event;
11711 work->crtc = crtc;
11712 work->old_fb = old_fb;
11713 INIT_WORK(&work->unpin_work, intel_unpin_work_fn);
11714
11715 ret = drm_crtc_vblank_get(crtc);
11716 if (ret)
11717 goto free_work;
11718
11719 /* We borrow the event spin lock for protecting flip_work */
11720 spin_lock_irq(&dev->event_lock);
11721 if (intel_crtc->flip_work) {
11722 /* Before declaring the flip queue wedged, check if
11723 * the hardware completed the operation behind our backs.
11724 */
11725 if (pageflip_finished(intel_crtc, intel_crtc->flip_work)) {
11726 DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
11727 page_flip_completed(intel_crtc);
11728 } else {
11729 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
11730 spin_unlock_irq(&dev->event_lock);
11731
11732 drm_crtc_vblank_put(crtc);
11733 kfree(work);
11734 return -EBUSY;
11735 }
11736 }
11737 intel_crtc->flip_work = work;
11738 spin_unlock_irq(&dev->event_lock);
11739
11740 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
11741 flush_workqueue(dev_priv->wq);
11742
11743 /* Reference the objects for the scheduled work. */
11744 drm_framebuffer_reference(work->old_fb);
11745 drm_gem_object_reference(&obj->base);
11746
11747 crtc->primary->fb = fb;
11748 update_state_fb(crtc->primary);
faf68d92
ML
11749
11750 intel_fbc_pre_update(intel_crtc, intel_crtc->config,
11751 to_intel_plane_state(primary->state));
5a21b665
DV
11752
11753 work->pending_flip_obj = obj;
11754
11755 ret = i915_mutex_lock_interruptible(dev);
11756 if (ret)
11757 goto cleanup;
11758
11759 intel_crtc->reset_counter = i915_reset_counter(&dev_priv->gpu_error);
11760 if (__i915_reset_in_progress_or_wedged(intel_crtc->reset_counter)) {
11761 ret = -EIO;
11762 goto cleanup;
11763 }
11764
11765 atomic_inc(&intel_crtc->unpin_work_count);
11766
11767 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
11768 work->flip_count = I915_READ(PIPE_FLIPCOUNT_G4X(pipe)) + 1;
11769
11770 if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
11771 engine = &dev_priv->engine[BCS];
11772 if (obj->tiling_mode != intel_fb_obj(work->old_fb)->tiling_mode)
11773 /* vlv: DISPLAY_FLIP fails to change tiling */
11774 engine = NULL;
11775 } else if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
11776 engine = &dev_priv->engine[BCS];
11777 } else if (INTEL_INFO(dev)->gen >= 7) {
11778 engine = i915_gem_request_get_engine(obj->last_write_req);
11779 if (engine == NULL || engine->id != RCS)
11780 engine = &dev_priv->engine[BCS];
11781 } else {
11782 engine = &dev_priv->engine[RCS];
11783 }
11784
11785 mmio_flip = use_mmio_flip(engine, obj);
11786
11787 /* When using CS flips, we want to emit semaphores between rings.
11788 * However, when using mmio flips we will create a task to do the
11789 * synchronisation, so all we want here is to pin the framebuffer
11790 * into the display plane and skip any waits.
11791 */
11792 if (!mmio_flip) {
11793 ret = i915_gem_object_sync(obj, engine, &request);
11794 if (!ret && !request) {
11795 request = i915_gem_request_alloc(engine, NULL);
11796 ret = PTR_ERR_OR_ZERO(request);
11797 }
11798
11799 if (ret)
11800 goto cleanup_pending;
11801 }
11802
11803 ret = intel_pin_and_fence_fb_obj(fb, primary->state->rotation);
11804 if (ret)
11805 goto cleanup_pending;
11806
11807 work->gtt_offset = intel_plane_obj_offset(to_intel_plane(primary),
11808 obj, 0);
11809 work->gtt_offset += intel_crtc->dspaddr_offset;
11810 work->rotation = crtc->primary->state->rotation;
11811
11812 if (mmio_flip) {
11813 INIT_WORK(&work->mmio_work, intel_mmio_flip_work_func);
11814
11815 i915_gem_request_assign(&work->flip_queued_req,
11816 obj->last_write_req);
11817
11818 schedule_work(&work->mmio_work);
11819 } else {
11820 i915_gem_request_assign(&work->flip_queued_req, request);
11821 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, request,
11822 page_flip_flags);
11823 if (ret)
11824 goto cleanup_unpin;
11825
11826 intel_mark_page_flip_active(intel_crtc, work);
11827
11828 i915_add_request_no_flush(request);
11829 }
11830
11831 i915_gem_track_fb(intel_fb_obj(old_fb), obj,
11832 to_intel_plane(primary)->frontbuffer_bit);
11833 mutex_unlock(&dev->struct_mutex);
11834
11835 intel_frontbuffer_flip_prepare(dev,
11836 to_intel_plane(primary)->frontbuffer_bit);
11837
11838 trace_i915_flip_request(intel_crtc->plane, obj);
11839
11840 return 0;
11841
11842cleanup_unpin:
11843 intel_unpin_fb_obj(fb, crtc->primary->state->rotation);
11844cleanup_pending:
11845 if (!IS_ERR_OR_NULL(request))
11846 i915_add_request_no_flush(request);
11847 atomic_dec(&intel_crtc->unpin_work_count);
11848 mutex_unlock(&dev->struct_mutex);
11849cleanup:
11850 crtc->primary->fb = old_fb;
11851 update_state_fb(crtc->primary);
11852
11853 drm_gem_object_unreference_unlocked(&obj->base);
11854 drm_framebuffer_unreference(work->old_fb);
11855
11856 spin_lock_irq(&dev->event_lock);
11857 intel_crtc->flip_work = NULL;
11858 spin_unlock_irq(&dev->event_lock);
11859
11860 drm_crtc_vblank_put(crtc);
11861free_work:
11862 kfree(work);
11863
11864 if (ret == -EIO) {
11865 struct drm_atomic_state *state;
11866 struct drm_plane_state *plane_state;
11867
11868out_hang:
11869 state = drm_atomic_state_alloc(dev);
11870 if (!state)
11871 return -ENOMEM;
11872 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
11873
11874retry:
11875 plane_state = drm_atomic_get_plane_state(state, primary);
11876 ret = PTR_ERR_OR_ZERO(plane_state);
11877 if (!ret) {
11878 drm_atomic_set_fb_for_plane(plane_state, fb);
11879
11880 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
11881 if (!ret)
11882 ret = drm_atomic_commit(state);
11883 }
11884
11885 if (ret == -EDEADLK) {
11886 drm_modeset_backoff(state->acquire_ctx);
11887 drm_atomic_state_clear(state);
11888 goto retry;
11889 }
11890
11891 if (ret)
11892 drm_atomic_state_free(state);
11893
11894 if (ret == 0 && event) {
11895 spin_lock_irq(&dev->event_lock);
11896 drm_crtc_send_vblank_event(crtc, event);
11897 spin_unlock_irq(&dev->event_lock);
11898 }
11899 }
11900 return ret;
11901}
11902
11903
11904/**
11905 * intel_wm_need_update - Check whether watermarks need updating
11906 * @plane: drm plane
11907 * @state: new plane state
11908 *
11909 * Check current plane state versus the new one to determine whether
11910 * watermarks need to be recalculated.
11911 *
11912 * Returns true or false.
11913 */
11914static bool intel_wm_need_update(struct drm_plane *plane,
11915 struct drm_plane_state *state)
11916{
11917 struct intel_plane_state *new = to_intel_plane_state(state);
11918 struct intel_plane_state *cur = to_intel_plane_state(plane->state);
11919
11920 /* Update watermarks on tiling or size changes. */
11921 if (new->visible != cur->visible)
11922 return true;
11923
11924 if (!cur->base.fb || !new->base.fb)
11925 return false;
11926
11927 if (cur->base.fb->modifier[0] != new->base.fb->modifier[0] ||
11928 cur->base.rotation != new->base.rotation ||
11929 drm_rect_width(&new->src) != drm_rect_width(&cur->src) ||
11930 drm_rect_height(&new->src) != drm_rect_height(&cur->src) ||
11931 drm_rect_width(&new->dst) != drm_rect_width(&cur->dst) ||
11932 drm_rect_height(&new->dst) != drm_rect_height(&cur->dst))
11933 return true;
11934
11935 return false;
11936}
11937
11938static bool needs_scaling(struct intel_plane_state *state)
11939{
11940 int src_w = drm_rect_width(&state->src) >> 16;
11941 int src_h = drm_rect_height(&state->src) >> 16;
11942 int dst_w = drm_rect_width(&state->dst);
11943 int dst_h = drm_rect_height(&state->dst);
11944
11945 return (src_w != dst_w || src_h != dst_h);
11946}
d21fbe87 11947
da20eabd
ML
11948int intel_plane_atomic_calc_changes(struct drm_crtc_state *crtc_state,
11949 struct drm_plane_state *plane_state)
11950{
ab1d3a0e 11951 struct intel_crtc_state *pipe_config = to_intel_crtc_state(crtc_state);
da20eabd
ML
11952 struct drm_crtc *crtc = crtc_state->crtc;
11953 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11954 struct drm_plane *plane = plane_state->plane;
11955 struct drm_device *dev = crtc->dev;
ed4a6a7c 11956 struct drm_i915_private *dev_priv = to_i915(dev);
da20eabd
ML
11957 struct intel_plane_state *old_plane_state =
11958 to_intel_plane_state(plane->state);
da20eabd
ML
11959 bool mode_changed = needs_modeset(crtc_state);
11960 bool was_crtc_enabled = crtc->state->active;
11961 bool is_crtc_enabled = crtc_state->active;
da20eabd
ML
11962 bool turn_off, turn_on, visible, was_visible;
11963 struct drm_framebuffer *fb = plane_state->fb;
78108b7c 11964 int ret;
da20eabd
ML
11965
11966 if (crtc_state && INTEL_INFO(dev)->gen >= 9 &&
11967 plane->type != DRM_PLANE_TYPE_CURSOR) {
11968 ret = skl_update_scaler_plane(
11969 to_intel_crtc_state(crtc_state),
11970 to_intel_plane_state(plane_state));
11971 if (ret)
11972 return ret;
11973 }
11974
da20eabd
ML
11975 was_visible = old_plane_state->visible;
11976 visible = to_intel_plane_state(plane_state)->visible;
11977
11978 if (!was_crtc_enabled && WARN_ON(was_visible))
11979 was_visible = false;
11980
35c08f43
ML
11981 /*
11982 * Visibility is calculated as if the crtc was on, but
11983 * after scaler setup everything depends on it being off
11984 * when the crtc isn't active.
f818ffea
VS
11985 *
11986 * FIXME this is wrong for watermarks. Watermarks should also
11987 * be computed as if the pipe would be active. Perhaps move
11988 * per-plane wm computation to the .check_plane() hook, and
11989 * only combine the results from all planes in the current place?
35c08f43
ML
11990 */
11991 if (!is_crtc_enabled)
11992 to_intel_plane_state(plane_state)->visible = visible = false;
da20eabd
ML
11993
11994 if (!was_visible && !visible)
11995 return 0;
11996
e8861675
ML
11997 if (fb != old_plane_state->base.fb)
11998 pipe_config->fb_changed = true;
11999
da20eabd
ML
12000 turn_off = was_visible && (!visible || mode_changed);
12001 turn_on = visible && (!was_visible || mode_changed);
12002
72660ce0 12003 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] has [PLANE:%d:%s] with fb %i\n",
78108b7c
VS
12004 intel_crtc->base.base.id,
12005 intel_crtc->base.name,
72660ce0
VS
12006 plane->base.id, plane->name,
12007 fb ? fb->base.id : -1);
da20eabd 12008
72660ce0
VS
12009 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] visible %i -> %i, off %i, on %i, ms %i\n",
12010 plane->base.id, plane->name,
12011 was_visible, visible,
da20eabd
ML
12012 turn_off, turn_on, mode_changed);
12013
caed361d
VS
12014 if (turn_on) {
12015 pipe_config->update_wm_pre = true;
12016
12017 /* must disable cxsr around plane enable/disable */
12018 if (plane->type != DRM_PLANE_TYPE_CURSOR)
12019 pipe_config->disable_cxsr = true;
12020 } else if (turn_off) {
12021 pipe_config->update_wm_post = true;
92826fcd 12022
852eb00d 12023 /* must disable cxsr around plane enable/disable */
e8861675 12024 if (plane->type != DRM_PLANE_TYPE_CURSOR)
ab1d3a0e 12025 pipe_config->disable_cxsr = true;
852eb00d 12026 } else if (intel_wm_need_update(plane, plane_state)) {
caed361d
VS
12027 /* FIXME bollocks */
12028 pipe_config->update_wm_pre = true;
12029 pipe_config->update_wm_post = true;
852eb00d 12030 }
da20eabd 12031
ed4a6a7c 12032 /* Pre-gen9 platforms need two-step watermark updates */
caed361d
VS
12033 if ((pipe_config->update_wm_pre || pipe_config->update_wm_post) &&
12034 INTEL_INFO(dev)->gen < 9 && dev_priv->display.optimize_watermarks)
ed4a6a7c
MR
12035 to_intel_crtc_state(crtc_state)->wm.need_postvbl_update = true;
12036
8be6ca85 12037 if (visible || was_visible)
cd202f69 12038 pipe_config->fb_bits |= to_intel_plane(plane)->frontbuffer_bit;
a9ff8714 12039
31ae71fc
ML
12040 /*
12041 * WaCxSRDisabledForSpriteScaling:ivb
12042 *
12043 * cstate->update_wm was already set above, so this flag will
12044 * take effect when we commit and program watermarks.
12045 */
12046 if (plane->type == DRM_PLANE_TYPE_OVERLAY && IS_IVYBRIDGE(dev) &&
12047 needs_scaling(to_intel_plane_state(plane_state)) &&
12048 !needs_scaling(old_plane_state))
12049 pipe_config->disable_lp_wm = true;
d21fbe87 12050
da20eabd
ML
12051 return 0;
12052}
12053
6d3a1ce7
ML
12054static bool encoders_cloneable(const struct intel_encoder *a,
12055 const struct intel_encoder *b)
12056{
12057 /* masks could be asymmetric, so check both ways */
12058 return a == b || (a->cloneable & (1 << b->type) &&
12059 b->cloneable & (1 << a->type));
12060}
12061
12062static bool check_single_encoder_cloning(struct drm_atomic_state *state,
12063 struct intel_crtc *crtc,
12064 struct intel_encoder *encoder)
12065{
12066 struct intel_encoder *source_encoder;
12067 struct drm_connector *connector;
12068 struct drm_connector_state *connector_state;
12069 int i;
12070
12071 for_each_connector_in_state(state, connector, connector_state, i) {
12072 if (connector_state->crtc != &crtc->base)
12073 continue;
12074
12075 source_encoder =
12076 to_intel_encoder(connector_state->best_encoder);
12077 if (!encoders_cloneable(encoder, source_encoder))
12078 return false;
12079 }
12080
12081 return true;
12082}
12083
12084static bool check_encoder_cloning(struct drm_atomic_state *state,
12085 struct intel_crtc *crtc)
12086{
12087 struct intel_encoder *encoder;
12088 struct drm_connector *connector;
12089 struct drm_connector_state *connector_state;
12090 int i;
12091
12092 for_each_connector_in_state(state, connector, connector_state, i) {
12093 if (connector_state->crtc != &crtc->base)
12094 continue;
12095
12096 encoder = to_intel_encoder(connector_state->best_encoder);
12097 if (!check_single_encoder_cloning(state, crtc, encoder))
12098 return false;
12099 }
12100
12101 return true;
12102}
12103
12104static int intel_crtc_atomic_check(struct drm_crtc *crtc,
12105 struct drm_crtc_state *crtc_state)
12106{
cf5a15be 12107 struct drm_device *dev = crtc->dev;
ad421372 12108 struct drm_i915_private *dev_priv = dev->dev_private;
6d3a1ce7 12109 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
cf5a15be
ML
12110 struct intel_crtc_state *pipe_config =
12111 to_intel_crtc_state(crtc_state);
6d3a1ce7 12112 struct drm_atomic_state *state = crtc_state->state;
4d20cd86 12113 int ret;
6d3a1ce7
ML
12114 bool mode_changed = needs_modeset(crtc_state);
12115
12116 if (mode_changed && !check_encoder_cloning(state, intel_crtc)) {
12117 DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
12118 return -EINVAL;
12119 }
12120
852eb00d 12121 if (mode_changed && !crtc_state->active)
caed361d 12122 pipe_config->update_wm_post = true;
eddfcbcd 12123
ad421372
ML
12124 if (mode_changed && crtc_state->enable &&
12125 dev_priv->display.crtc_compute_clock &&
8106ddbd 12126 !WARN_ON(pipe_config->shared_dpll)) {
ad421372
ML
12127 ret = dev_priv->display.crtc_compute_clock(intel_crtc,
12128 pipe_config);
12129 if (ret)
12130 return ret;
12131 }
12132
82cf435b
LL
12133 if (crtc_state->color_mgmt_changed) {
12134 ret = intel_color_check(crtc, crtc_state);
12135 if (ret)
12136 return ret;
12137 }
12138
e435d6e5 12139 ret = 0;
86c8bbbe 12140 if (dev_priv->display.compute_pipe_wm) {
e3bddded 12141 ret = dev_priv->display.compute_pipe_wm(pipe_config);
ed4a6a7c
MR
12142 if (ret) {
12143 DRM_DEBUG_KMS("Target pipe watermarks are invalid\n");
12144 return ret;
12145 }
12146 }
12147
12148 if (dev_priv->display.compute_intermediate_wm &&
12149 !to_intel_atomic_state(state)->skip_intermediate_wm) {
12150 if (WARN_ON(!dev_priv->display.compute_pipe_wm))
12151 return 0;
12152
12153 /*
12154 * Calculate 'intermediate' watermarks that satisfy both the
12155 * old state and the new state. We can program these
12156 * immediately.
12157 */
12158 ret = dev_priv->display.compute_intermediate_wm(crtc->dev,
12159 intel_crtc,
12160 pipe_config);
12161 if (ret) {
12162 DRM_DEBUG_KMS("No valid intermediate pipe watermarks are possible\n");
86c8bbbe 12163 return ret;
ed4a6a7c 12164 }
e3d5457c
VS
12165 } else if (dev_priv->display.compute_intermediate_wm) {
12166 if (HAS_PCH_SPLIT(dev_priv) && INTEL_GEN(dev_priv) < 9)
12167 pipe_config->wm.ilk.intermediate = pipe_config->wm.ilk.optimal;
86c8bbbe
MR
12168 }
12169
e435d6e5
ML
12170 if (INTEL_INFO(dev)->gen >= 9) {
12171 if (mode_changed)
12172 ret = skl_update_scaler_crtc(pipe_config);
12173
12174 if (!ret)
12175 ret = intel_atomic_setup_scalers(dev, intel_crtc,
12176 pipe_config);
12177 }
12178
12179 return ret;
6d3a1ce7
ML
12180}
12181
65b38e0d 12182static const struct drm_crtc_helper_funcs intel_helper_funcs = {
f6e5b160 12183 .mode_set_base_atomic = intel_pipe_set_base_atomic,
5a21b665
DV
12184 .atomic_begin = intel_begin_crtc_commit,
12185 .atomic_flush = intel_finish_crtc_commit,
6d3a1ce7 12186 .atomic_check = intel_crtc_atomic_check,
f6e5b160
CW
12187};
12188
d29b2f9d
ACO
12189static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
12190{
12191 struct intel_connector *connector;
12192
12193 for_each_intel_connector(dev, connector) {
8863dc7f
DV
12194 if (connector->base.state->crtc)
12195 drm_connector_unreference(&connector->base);
12196
d29b2f9d
ACO
12197 if (connector->base.encoder) {
12198 connector->base.state->best_encoder =
12199 connector->base.encoder;
12200 connector->base.state->crtc =
12201 connector->base.encoder->crtc;
8863dc7f
DV
12202
12203 drm_connector_reference(&connector->base);
d29b2f9d
ACO
12204 } else {
12205 connector->base.state->best_encoder = NULL;
12206 connector->base.state->crtc = NULL;
12207 }
12208 }
12209}
12210
050f7aeb 12211static void
eba905b2 12212connected_sink_compute_bpp(struct intel_connector *connector,
5cec258b 12213 struct intel_crtc_state *pipe_config)
050f7aeb
DV
12214{
12215 int bpp = pipe_config->pipe_bpp;
12216
12217 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
12218 connector->base.base.id,
c23cc417 12219 connector->base.name);
050f7aeb
DV
12220
12221 /* Don't use an invalid EDID bpc value */
12222 if (connector->base.display_info.bpc &&
12223 connector->base.display_info.bpc * 3 < bpp) {
12224 DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
12225 bpp, connector->base.display_info.bpc*3);
12226 pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
12227 }
12228
013dd9e0
JN
12229 /* Clamp bpp to default limit on screens without EDID 1.4 */
12230 if (connector->base.display_info.bpc == 0) {
12231 int type = connector->base.connector_type;
12232 int clamp_bpp = 24;
12233
12234 /* Fall back to 18 bpp when DP sink capability is unknown. */
12235 if (type == DRM_MODE_CONNECTOR_DisplayPort ||
12236 type == DRM_MODE_CONNECTOR_eDP)
12237 clamp_bpp = 18;
12238
12239 if (bpp > clamp_bpp) {
12240 DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of %d\n",
12241 bpp, clamp_bpp);
12242 pipe_config->pipe_bpp = clamp_bpp;
12243 }
050f7aeb
DV
12244 }
12245}
12246
4e53c2e0 12247static int
050f7aeb 12248compute_baseline_pipe_bpp(struct intel_crtc *crtc,
5cec258b 12249 struct intel_crtc_state *pipe_config)
4e53c2e0 12250{
050f7aeb 12251 struct drm_device *dev = crtc->base.dev;
1486017f 12252 struct drm_atomic_state *state;
da3ced29
ACO
12253 struct drm_connector *connector;
12254 struct drm_connector_state *connector_state;
1486017f 12255 int bpp, i;
4e53c2e0 12256
666a4537 12257 if ((IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)))
4e53c2e0 12258 bpp = 10*3;
d328c9d7
DV
12259 else if (INTEL_INFO(dev)->gen >= 5)
12260 bpp = 12*3;
12261 else
12262 bpp = 8*3;
12263
4e53c2e0 12264
4e53c2e0
DV
12265 pipe_config->pipe_bpp = bpp;
12266
1486017f
ACO
12267 state = pipe_config->base.state;
12268
4e53c2e0 12269 /* Clamp display bpp to EDID value */
da3ced29
ACO
12270 for_each_connector_in_state(state, connector, connector_state, i) {
12271 if (connector_state->crtc != &crtc->base)
4e53c2e0
DV
12272 continue;
12273
da3ced29
ACO
12274 connected_sink_compute_bpp(to_intel_connector(connector),
12275 pipe_config);
4e53c2e0
DV
12276 }
12277
12278 return bpp;
12279}
12280
644db711
DV
12281static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
12282{
12283 DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
12284 "type: 0x%x flags: 0x%x\n",
1342830c 12285 mode->crtc_clock,
644db711
DV
12286 mode->crtc_hdisplay, mode->crtc_hsync_start,
12287 mode->crtc_hsync_end, mode->crtc_htotal,
12288 mode->crtc_vdisplay, mode->crtc_vsync_start,
12289 mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
12290}
12291
c0b03411 12292static void intel_dump_pipe_config(struct intel_crtc *crtc,
5cec258b 12293 struct intel_crtc_state *pipe_config,
c0b03411
DV
12294 const char *context)
12295{
6a60cd87
CK
12296 struct drm_device *dev = crtc->base.dev;
12297 struct drm_plane *plane;
12298 struct intel_plane *intel_plane;
12299 struct intel_plane_state *state;
12300 struct drm_framebuffer *fb;
12301
78108b7c
VS
12302 DRM_DEBUG_KMS("[CRTC:%d:%s]%s config %p for pipe %c\n",
12303 crtc->base.base.id, crtc->base.name,
6a60cd87 12304 context, pipe_config, pipe_name(crtc->pipe));
c0b03411 12305
da205630 12306 DRM_DEBUG_KMS("cpu_transcoder: %s\n", transcoder_name(pipe_config->cpu_transcoder));
c0b03411
DV
12307 DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
12308 pipe_config->pipe_bpp, pipe_config->dither);
12309 DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
12310 pipe_config->has_pch_encoder,
12311 pipe_config->fdi_lanes,
12312 pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
12313 pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
12314 pipe_config->fdi_m_n.tu);
90a6b7b0 12315 DRM_DEBUG_KMS("dp: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
eb14cb74 12316 pipe_config->has_dp_encoder,
90a6b7b0 12317 pipe_config->lane_count,
eb14cb74
VS
12318 pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
12319 pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
12320 pipe_config->dp_m_n.tu);
b95af8be 12321
90a6b7b0 12322 DRM_DEBUG_KMS("dp: %i, lanes: %i, gmch_m2: %u, gmch_n2: %u, link_m2: %u, link_n2: %u, tu2: %u\n",
b95af8be 12323 pipe_config->has_dp_encoder,
90a6b7b0 12324 pipe_config->lane_count,
b95af8be
VK
12325 pipe_config->dp_m2_n2.gmch_m,
12326 pipe_config->dp_m2_n2.gmch_n,
12327 pipe_config->dp_m2_n2.link_m,
12328 pipe_config->dp_m2_n2.link_n,
12329 pipe_config->dp_m2_n2.tu);
12330
55072d19
DV
12331 DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
12332 pipe_config->has_audio,
12333 pipe_config->has_infoframe);
12334
c0b03411 12335 DRM_DEBUG_KMS("requested mode:\n");
2d112de7 12336 drm_mode_debug_printmodeline(&pipe_config->base.mode);
c0b03411 12337 DRM_DEBUG_KMS("adjusted mode:\n");
2d112de7
ACO
12338 drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
12339 intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
d71b8d4a 12340 DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
37327abd
VS
12341 DRM_DEBUG_KMS("pipe src size: %dx%d\n",
12342 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
0ec463d3
TU
12343 DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
12344 crtc->num_scalers,
12345 pipe_config->scaler_state.scaler_users,
12346 pipe_config->scaler_state.scaler_id);
c0b03411
DV
12347 DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
12348 pipe_config->gmch_pfit.control,
12349 pipe_config->gmch_pfit.pgm_ratios,
12350 pipe_config->gmch_pfit.lvds_border_bits);
fd4daa9c 12351 DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
c0b03411 12352 pipe_config->pch_pfit.pos,
fd4daa9c
CW
12353 pipe_config->pch_pfit.size,
12354 pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
42db64ef 12355 DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
cf532bb2 12356 DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
6a60cd87 12357
415ff0f6 12358 if (IS_BROXTON(dev)) {
05712c15 12359 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: ebb0: 0x%x, ebb4: 0x%x,"
415ff0f6 12360 "pll0: 0x%x, pll1: 0x%x, pll2: 0x%x, pll3: 0x%x, "
c8453338 12361 "pll6: 0x%x, pll8: 0x%x, pll9: 0x%x, pll10: 0x%x, pcsdw12: 0x%x\n",
415ff0f6
TU
12362 pipe_config->ddi_pll_sel,
12363 pipe_config->dpll_hw_state.ebb0,
05712c15 12364 pipe_config->dpll_hw_state.ebb4,
415ff0f6
TU
12365 pipe_config->dpll_hw_state.pll0,
12366 pipe_config->dpll_hw_state.pll1,
12367 pipe_config->dpll_hw_state.pll2,
12368 pipe_config->dpll_hw_state.pll3,
12369 pipe_config->dpll_hw_state.pll6,
12370 pipe_config->dpll_hw_state.pll8,
05712c15 12371 pipe_config->dpll_hw_state.pll9,
c8453338 12372 pipe_config->dpll_hw_state.pll10,
415ff0f6 12373 pipe_config->dpll_hw_state.pcsdw12);
ef11bdb3 12374 } else if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) {
415ff0f6
TU
12375 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: "
12376 "ctrl1: 0x%x, cfgcr1: 0x%x, cfgcr2: 0x%x\n",
12377 pipe_config->ddi_pll_sel,
12378 pipe_config->dpll_hw_state.ctrl1,
12379 pipe_config->dpll_hw_state.cfgcr1,
12380 pipe_config->dpll_hw_state.cfgcr2);
12381 } else if (HAS_DDI(dev)) {
1260f07e 12382 DRM_DEBUG_KMS("ddi_pll_sel: 0x%x; dpll_hw_state: wrpll: 0x%x spll: 0x%x\n",
415ff0f6 12383 pipe_config->ddi_pll_sel,
00490c22
ML
12384 pipe_config->dpll_hw_state.wrpll,
12385 pipe_config->dpll_hw_state.spll);
415ff0f6
TU
12386 } else {
12387 DRM_DEBUG_KMS("dpll_hw_state: dpll: 0x%x, dpll_md: 0x%x, "
12388 "fp0: 0x%x, fp1: 0x%x\n",
12389 pipe_config->dpll_hw_state.dpll,
12390 pipe_config->dpll_hw_state.dpll_md,
12391 pipe_config->dpll_hw_state.fp0,
12392 pipe_config->dpll_hw_state.fp1);
12393 }
12394
6a60cd87
CK
12395 DRM_DEBUG_KMS("planes on this crtc\n");
12396 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
12397 intel_plane = to_intel_plane(plane);
12398 if (intel_plane->pipe != crtc->pipe)
12399 continue;
12400
12401 state = to_intel_plane_state(plane->state);
12402 fb = state->base.fb;
12403 if (!fb) {
1d577e02
VS
12404 DRM_DEBUG_KMS("[PLANE:%d:%s] disabled, scaler_id = %d\n",
12405 plane->base.id, plane->name, state->scaler_id);
6a60cd87
CK
12406 continue;
12407 }
12408
1d577e02
VS
12409 DRM_DEBUG_KMS("[PLANE:%d:%s] enabled",
12410 plane->base.id, plane->name);
12411 DRM_DEBUG_KMS("\tFB:%d, fb = %ux%u format = %s",
12412 fb->base.id, fb->width, fb->height,
12413 drm_get_format_name(fb->pixel_format));
12414 DRM_DEBUG_KMS("\tscaler:%d src %dx%d+%d+%d dst %dx%d+%d+%d\n",
12415 state->scaler_id,
12416 state->src.x1 >> 16, state->src.y1 >> 16,
12417 drm_rect_width(&state->src) >> 16,
12418 drm_rect_height(&state->src) >> 16,
12419 state->dst.x1, state->dst.y1,
12420 drm_rect_width(&state->dst),
12421 drm_rect_height(&state->dst));
6a60cd87 12422 }
c0b03411
DV
12423}
12424
5448a00d 12425static bool check_digital_port_conflicts(struct drm_atomic_state *state)
00f0b378 12426{
5448a00d 12427 struct drm_device *dev = state->dev;
da3ced29 12428 struct drm_connector *connector;
00f0b378
VS
12429 unsigned int used_ports = 0;
12430
12431 /*
12432 * Walk the connector list instead of the encoder
12433 * list to detect the problem on ddi platforms
12434 * where there's just one encoder per digital port.
12435 */
0bff4858
VS
12436 drm_for_each_connector(connector, dev) {
12437 struct drm_connector_state *connector_state;
12438 struct intel_encoder *encoder;
12439
12440 connector_state = drm_atomic_get_existing_connector_state(state, connector);
12441 if (!connector_state)
12442 connector_state = connector->state;
12443
5448a00d 12444 if (!connector_state->best_encoder)
00f0b378
VS
12445 continue;
12446
5448a00d
ACO
12447 encoder = to_intel_encoder(connector_state->best_encoder);
12448
12449 WARN_ON(!connector_state->crtc);
00f0b378
VS
12450
12451 switch (encoder->type) {
12452 unsigned int port_mask;
12453 case INTEL_OUTPUT_UNKNOWN:
12454 if (WARN_ON(!HAS_DDI(dev)))
12455 break;
12456 case INTEL_OUTPUT_DISPLAYPORT:
12457 case INTEL_OUTPUT_HDMI:
12458 case INTEL_OUTPUT_EDP:
12459 port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
12460
12461 /* the same port mustn't appear more than once */
12462 if (used_ports & port_mask)
12463 return false;
12464
12465 used_ports |= port_mask;
12466 default:
12467 break;
12468 }
12469 }
12470
12471 return true;
12472}
12473
83a57153
ACO
12474static void
12475clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
12476{
12477 struct drm_crtc_state tmp_state;
663a3640 12478 struct intel_crtc_scaler_state scaler_state;
4978cc93 12479 struct intel_dpll_hw_state dpll_hw_state;
8106ddbd 12480 struct intel_shared_dpll *shared_dpll;
8504c74c 12481 uint32_t ddi_pll_sel;
c4e2d043 12482 bool force_thru;
83a57153 12483
7546a384
ACO
12484 /* FIXME: before the switch to atomic started, a new pipe_config was
12485 * kzalloc'd. Code that depends on any field being zero should be
12486 * fixed, so that the crtc_state can be safely duplicated. For now,
12487 * only fields that are know to not cause problems are preserved. */
12488
83a57153 12489 tmp_state = crtc_state->base;
663a3640 12490 scaler_state = crtc_state->scaler_state;
4978cc93
ACO
12491 shared_dpll = crtc_state->shared_dpll;
12492 dpll_hw_state = crtc_state->dpll_hw_state;
8504c74c 12493 ddi_pll_sel = crtc_state->ddi_pll_sel;
c4e2d043 12494 force_thru = crtc_state->pch_pfit.force_thru;
4978cc93 12495
83a57153 12496 memset(crtc_state, 0, sizeof *crtc_state);
4978cc93 12497
83a57153 12498 crtc_state->base = tmp_state;
663a3640 12499 crtc_state->scaler_state = scaler_state;
4978cc93
ACO
12500 crtc_state->shared_dpll = shared_dpll;
12501 crtc_state->dpll_hw_state = dpll_hw_state;
8504c74c 12502 crtc_state->ddi_pll_sel = ddi_pll_sel;
c4e2d043 12503 crtc_state->pch_pfit.force_thru = force_thru;
83a57153
ACO
12504}
12505
548ee15b 12506static int
b8cecdf5 12507intel_modeset_pipe_config(struct drm_crtc *crtc,
b359283a 12508 struct intel_crtc_state *pipe_config)
ee7b9f93 12509{
b359283a 12510 struct drm_atomic_state *state = pipe_config->base.state;
7758a113 12511 struct intel_encoder *encoder;
da3ced29 12512 struct drm_connector *connector;
0b901879 12513 struct drm_connector_state *connector_state;
d328c9d7 12514 int base_bpp, ret = -EINVAL;
0b901879 12515 int i;
e29c22c0 12516 bool retry = true;
ee7b9f93 12517
83a57153 12518 clear_intel_crtc_state(pipe_config);
7758a113 12519
e143a21c
DV
12520 pipe_config->cpu_transcoder =
12521 (enum transcoder) to_intel_crtc(crtc)->pipe;
b8cecdf5 12522
2960bc9c
ID
12523 /*
12524 * Sanitize sync polarity flags based on requested ones. If neither
12525 * positive or negative polarity is requested, treat this as meaning
12526 * negative polarity.
12527 */
2d112de7 12528 if (!(pipe_config->base.adjusted_mode.flags &
2960bc9c 12529 (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
2d112de7 12530 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
2960bc9c 12531
2d112de7 12532 if (!(pipe_config->base.adjusted_mode.flags &
2960bc9c 12533 (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
2d112de7 12534 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
2960bc9c 12535
d328c9d7
DV
12536 base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
12537 pipe_config);
12538 if (base_bpp < 0)
4e53c2e0
DV
12539 goto fail;
12540
e41a56be
VS
12541 /*
12542 * Determine the real pipe dimensions. Note that stereo modes can
12543 * increase the actual pipe size due to the frame doubling and
12544 * insertion of additional space for blanks between the frame. This
12545 * is stored in the crtc timings. We use the requested mode to do this
12546 * computation to clearly distinguish it from the adjusted mode, which
12547 * can be changed by the connectors in the below retry loop.
12548 */
2d112de7 12549 drm_crtc_get_hv_timing(&pipe_config->base.mode,
ecb7e16b
GP
12550 &pipe_config->pipe_src_w,
12551 &pipe_config->pipe_src_h);
e41a56be 12552
e29c22c0 12553encoder_retry:
ef1b460d 12554 /* Ensure the port clock defaults are reset when retrying. */
ff9a6750 12555 pipe_config->port_clock = 0;
ef1b460d 12556 pipe_config->pixel_multiplier = 1;
ff9a6750 12557
135c81b8 12558 /* Fill in default crtc timings, allow encoders to overwrite them. */
2d112de7
ACO
12559 drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
12560 CRTC_STEREO_DOUBLE);
135c81b8 12561
7758a113
DV
12562 /* Pass our mode to the connectors and the CRTC to give them a chance to
12563 * adjust it according to limitations or connector properties, and also
12564 * a chance to reject the mode entirely.
47f1c6c9 12565 */
da3ced29 12566 for_each_connector_in_state(state, connector, connector_state, i) {
0b901879 12567 if (connector_state->crtc != crtc)
7758a113 12568 continue;
7ae89233 12569
0b901879
ACO
12570 encoder = to_intel_encoder(connector_state->best_encoder);
12571
efea6e8e
DV
12572 if (!(encoder->compute_config(encoder, pipe_config))) {
12573 DRM_DEBUG_KMS("Encoder config failure\n");
7758a113
DV
12574 goto fail;
12575 }
ee7b9f93 12576 }
47f1c6c9 12577
ff9a6750
DV
12578 /* Set default port clock if not overwritten by the encoder. Needs to be
12579 * done afterwards in case the encoder adjusts the mode. */
12580 if (!pipe_config->port_clock)
2d112de7 12581 pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
241bfc38 12582 * pipe_config->pixel_multiplier;
ff9a6750 12583
a43f6e0f 12584 ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
e29c22c0 12585 if (ret < 0) {
7758a113
DV
12586 DRM_DEBUG_KMS("CRTC fixup failed\n");
12587 goto fail;
ee7b9f93 12588 }
e29c22c0
DV
12589
12590 if (ret == RETRY) {
12591 if (WARN(!retry, "loop in pipe configuration computation\n")) {
12592 ret = -EINVAL;
12593 goto fail;
12594 }
12595
12596 DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
12597 retry = false;
12598 goto encoder_retry;
12599 }
12600
e8fa4270
DV
12601 /* Dithering seems to not pass-through bits correctly when it should, so
12602 * only enable it on 6bpc panels. */
12603 pipe_config->dither = pipe_config->pipe_bpp == 6*3;
62f0ace5 12604 DRM_DEBUG_KMS("hw max bpp: %i, pipe bpp: %i, dithering: %i\n",
d328c9d7 12605 base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
4e53c2e0 12606
7758a113 12607fail:
548ee15b 12608 return ret;
ee7b9f93 12609}
47f1c6c9 12610
ea9d758d 12611static void
4740b0f2 12612intel_modeset_update_crtc_state(struct drm_atomic_state *state)
ea9d758d 12613{
0a9ab303
ACO
12614 struct drm_crtc *crtc;
12615 struct drm_crtc_state *crtc_state;
8a75d157 12616 int i;
ea9d758d 12617
7668851f 12618 /* Double check state. */
8a75d157 12619 for_each_crtc_in_state(state, crtc, crtc_state, i) {
3cb480bc 12620 to_intel_crtc(crtc)->config = to_intel_crtc_state(crtc->state);
fc467a22
ML
12621
12622 /* Update hwmode for vblank functions */
12623 if (crtc->state->active)
12624 crtc->hwmode = crtc->state->adjusted_mode;
12625 else
12626 crtc->hwmode.crtc_clock = 0;
61067a5e
ML
12627
12628 /*
12629 * Update legacy state to satisfy fbc code. This can
12630 * be removed when fbc uses the atomic state.
12631 */
12632 if (drm_atomic_get_existing_plane_state(state, crtc->primary)) {
12633 struct drm_plane_state *plane_state = crtc->primary->state;
12634
12635 crtc->primary->fb = plane_state->fb;
12636 crtc->x = plane_state->src_x >> 16;
12637 crtc->y = plane_state->src_y >> 16;
12638 }
ea9d758d 12639 }
ea9d758d
DV
12640}
12641
3bd26263 12642static bool intel_fuzzy_clock_check(int clock1, int clock2)
f1f644dc 12643{
3bd26263 12644 int diff;
f1f644dc
JB
12645
12646 if (clock1 == clock2)
12647 return true;
12648
12649 if (!clock1 || !clock2)
12650 return false;
12651
12652 diff = abs(clock1 - clock2);
12653
12654 if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
12655 return true;
12656
12657 return false;
12658}
12659
25c5b266
DV
12660#define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
12661 list_for_each_entry((intel_crtc), \
12662 &(dev)->mode_config.crtc_list, \
12663 base.head) \
95150bdf 12664 for_each_if (mask & (1 <<(intel_crtc)->pipe))
25c5b266 12665
cfb23ed6
ML
12666static bool
12667intel_compare_m_n(unsigned int m, unsigned int n,
12668 unsigned int m2, unsigned int n2,
12669 bool exact)
12670{
12671 if (m == m2 && n == n2)
12672 return true;
12673
12674 if (exact || !m || !n || !m2 || !n2)
12675 return false;
12676
12677 BUILD_BUG_ON(DATA_LINK_M_N_MASK > INT_MAX);
12678
31d10b57
ML
12679 if (n > n2) {
12680 while (n > n2) {
cfb23ed6
ML
12681 m2 <<= 1;
12682 n2 <<= 1;
12683 }
31d10b57
ML
12684 } else if (n < n2) {
12685 while (n < n2) {
cfb23ed6
ML
12686 m <<= 1;
12687 n <<= 1;
12688 }
12689 }
12690
31d10b57
ML
12691 if (n != n2)
12692 return false;
12693
12694 return intel_fuzzy_clock_check(m, m2);
cfb23ed6
ML
12695}
12696
12697static bool
12698intel_compare_link_m_n(const struct intel_link_m_n *m_n,
12699 struct intel_link_m_n *m2_n2,
12700 bool adjust)
12701{
12702 if (m_n->tu == m2_n2->tu &&
12703 intel_compare_m_n(m_n->gmch_m, m_n->gmch_n,
12704 m2_n2->gmch_m, m2_n2->gmch_n, !adjust) &&
12705 intel_compare_m_n(m_n->link_m, m_n->link_n,
12706 m2_n2->link_m, m2_n2->link_n, !adjust)) {
12707 if (adjust)
12708 *m2_n2 = *m_n;
12709
12710 return true;
12711 }
12712
12713 return false;
12714}
12715
0e8ffe1b 12716static bool
2fa2fe9a 12717intel_pipe_config_compare(struct drm_device *dev,
5cec258b 12718 struct intel_crtc_state *current_config,
cfb23ed6
ML
12719 struct intel_crtc_state *pipe_config,
12720 bool adjust)
0e8ffe1b 12721{
cfb23ed6
ML
12722 bool ret = true;
12723
12724#define INTEL_ERR_OR_DBG_KMS(fmt, ...) \
12725 do { \
12726 if (!adjust) \
12727 DRM_ERROR(fmt, ##__VA_ARGS__); \
12728 else \
12729 DRM_DEBUG_KMS(fmt, ##__VA_ARGS__); \
12730 } while (0)
12731
66e985c0
DV
12732#define PIPE_CONF_CHECK_X(name) \
12733 if (current_config->name != pipe_config->name) { \
cfb23ed6 12734 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
66e985c0
DV
12735 "(expected 0x%08x, found 0x%08x)\n", \
12736 current_config->name, \
12737 pipe_config->name); \
cfb23ed6 12738 ret = false; \
66e985c0
DV
12739 }
12740
08a24034
DV
12741#define PIPE_CONF_CHECK_I(name) \
12742 if (current_config->name != pipe_config->name) { \
cfb23ed6 12743 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
08a24034
DV
12744 "(expected %i, found %i)\n", \
12745 current_config->name, \
12746 pipe_config->name); \
cfb23ed6
ML
12747 ret = false; \
12748 }
12749
8106ddbd
ACO
12750#define PIPE_CONF_CHECK_P(name) \
12751 if (current_config->name != pipe_config->name) { \
12752 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
12753 "(expected %p, found %p)\n", \
12754 current_config->name, \
12755 pipe_config->name); \
12756 ret = false; \
12757 }
12758
cfb23ed6
ML
12759#define PIPE_CONF_CHECK_M_N(name) \
12760 if (!intel_compare_link_m_n(&current_config->name, \
12761 &pipe_config->name,\
12762 adjust)) { \
12763 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
12764 "(expected tu %i gmch %i/%i link %i/%i, " \
12765 "found tu %i, gmch %i/%i link %i/%i)\n", \
12766 current_config->name.tu, \
12767 current_config->name.gmch_m, \
12768 current_config->name.gmch_n, \
12769 current_config->name.link_m, \
12770 current_config->name.link_n, \
12771 pipe_config->name.tu, \
12772 pipe_config->name.gmch_m, \
12773 pipe_config->name.gmch_n, \
12774 pipe_config->name.link_m, \
12775 pipe_config->name.link_n); \
12776 ret = false; \
12777 }
12778
55c561a7
DV
12779/* This is required for BDW+ where there is only one set of registers for
12780 * switching between high and low RR.
12781 * This macro can be used whenever a comparison has to be made between one
12782 * hw state and multiple sw state variables.
12783 */
cfb23ed6
ML
12784#define PIPE_CONF_CHECK_M_N_ALT(name, alt_name) \
12785 if (!intel_compare_link_m_n(&current_config->name, \
12786 &pipe_config->name, adjust) && \
12787 !intel_compare_link_m_n(&current_config->alt_name, \
12788 &pipe_config->name, adjust)) { \
12789 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
12790 "(expected tu %i gmch %i/%i link %i/%i, " \
12791 "or tu %i gmch %i/%i link %i/%i, " \
12792 "found tu %i, gmch %i/%i link %i/%i)\n", \
12793 current_config->name.tu, \
12794 current_config->name.gmch_m, \
12795 current_config->name.gmch_n, \
12796 current_config->name.link_m, \
12797 current_config->name.link_n, \
12798 current_config->alt_name.tu, \
12799 current_config->alt_name.gmch_m, \
12800 current_config->alt_name.gmch_n, \
12801 current_config->alt_name.link_m, \
12802 current_config->alt_name.link_n, \
12803 pipe_config->name.tu, \
12804 pipe_config->name.gmch_m, \
12805 pipe_config->name.gmch_n, \
12806 pipe_config->name.link_m, \
12807 pipe_config->name.link_n); \
12808 ret = false; \
88adfff1
DV
12809 }
12810
1bd1bd80
DV
12811#define PIPE_CONF_CHECK_FLAGS(name, mask) \
12812 if ((current_config->name ^ pipe_config->name) & (mask)) { \
cfb23ed6 12813 INTEL_ERR_OR_DBG_KMS("mismatch in " #name "(" #mask ") " \
1bd1bd80
DV
12814 "(expected %i, found %i)\n", \
12815 current_config->name & (mask), \
12816 pipe_config->name & (mask)); \
cfb23ed6 12817 ret = false; \
1bd1bd80
DV
12818 }
12819
5e550656
VS
12820#define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
12821 if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
cfb23ed6 12822 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
5e550656
VS
12823 "(expected %i, found %i)\n", \
12824 current_config->name, \
12825 pipe_config->name); \
cfb23ed6 12826 ret = false; \
5e550656
VS
12827 }
12828
bb760063
DV
12829#define PIPE_CONF_QUIRK(quirk) \
12830 ((current_config->quirks | pipe_config->quirks) & (quirk))
12831
eccb140b
DV
12832 PIPE_CONF_CHECK_I(cpu_transcoder);
12833
08a24034
DV
12834 PIPE_CONF_CHECK_I(has_pch_encoder);
12835 PIPE_CONF_CHECK_I(fdi_lanes);
cfb23ed6 12836 PIPE_CONF_CHECK_M_N(fdi_m_n);
08a24034 12837
eb14cb74 12838 PIPE_CONF_CHECK_I(has_dp_encoder);
90a6b7b0 12839 PIPE_CONF_CHECK_I(lane_count);
95a7a2ae 12840 PIPE_CONF_CHECK_X(lane_lat_optim_mask);
b95af8be
VK
12841
12842 if (INTEL_INFO(dev)->gen < 8) {
cfb23ed6
ML
12843 PIPE_CONF_CHECK_M_N(dp_m_n);
12844
cfb23ed6
ML
12845 if (current_config->has_drrs)
12846 PIPE_CONF_CHECK_M_N(dp_m2_n2);
12847 } else
12848 PIPE_CONF_CHECK_M_N_ALT(dp_m_n, dp_m2_n2);
eb14cb74 12849
a65347ba
JN
12850 PIPE_CONF_CHECK_I(has_dsi_encoder);
12851
2d112de7
ACO
12852 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
12853 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
12854 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
12855 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
12856 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
12857 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
1bd1bd80 12858
2d112de7
ACO
12859 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
12860 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
12861 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
12862 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
12863 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
12864 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
1bd1bd80 12865
c93f54cf 12866 PIPE_CONF_CHECK_I(pixel_multiplier);
6897b4b5 12867 PIPE_CONF_CHECK_I(has_hdmi_sink);
b5a9fa09 12868 if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
666a4537 12869 IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev))
b5a9fa09 12870 PIPE_CONF_CHECK_I(limited_color_range);
e43823ec 12871 PIPE_CONF_CHECK_I(has_infoframe);
6c49f241 12872
9ed109a7
DV
12873 PIPE_CONF_CHECK_I(has_audio);
12874
2d112de7 12875 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
1bd1bd80
DV
12876 DRM_MODE_FLAG_INTERLACE);
12877
bb760063 12878 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
2d112de7 12879 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063 12880 DRM_MODE_FLAG_PHSYNC);
2d112de7 12881 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063 12882 DRM_MODE_FLAG_NHSYNC);
2d112de7 12883 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063 12884 DRM_MODE_FLAG_PVSYNC);
2d112de7 12885 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063
DV
12886 DRM_MODE_FLAG_NVSYNC);
12887 }
045ac3b5 12888
333b8ca8 12889 PIPE_CONF_CHECK_X(gmch_pfit.control);
e2ff2d4a
DV
12890 /* pfit ratios are autocomputed by the hw on gen4+ */
12891 if (INTEL_INFO(dev)->gen < 4)
7f7d8dd6 12892 PIPE_CONF_CHECK_X(gmch_pfit.pgm_ratios);
333b8ca8 12893 PIPE_CONF_CHECK_X(gmch_pfit.lvds_border_bits);
9953599b 12894
bfd16b2a
ML
12895 if (!adjust) {
12896 PIPE_CONF_CHECK_I(pipe_src_w);
12897 PIPE_CONF_CHECK_I(pipe_src_h);
12898
12899 PIPE_CONF_CHECK_I(pch_pfit.enabled);
12900 if (current_config->pch_pfit.enabled) {
12901 PIPE_CONF_CHECK_X(pch_pfit.pos);
12902 PIPE_CONF_CHECK_X(pch_pfit.size);
12903 }
2fa2fe9a 12904
7aefe2b5
ML
12905 PIPE_CONF_CHECK_I(scaler_state.scaler_id);
12906 }
a1b2278e 12907
e59150dc
JB
12908 /* BDW+ don't expose a synchronous way to read the state */
12909 if (IS_HASWELL(dev))
12910 PIPE_CONF_CHECK_I(ips_enabled);
42db64ef 12911
282740f7
VS
12912 PIPE_CONF_CHECK_I(double_wide);
12913
26804afd
DV
12914 PIPE_CONF_CHECK_X(ddi_pll_sel);
12915
8106ddbd 12916 PIPE_CONF_CHECK_P(shared_dpll);
66e985c0 12917 PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
8bcc2795 12918 PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
66e985c0
DV
12919 PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
12920 PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
d452c5b6 12921 PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
00490c22 12922 PIPE_CONF_CHECK_X(dpll_hw_state.spll);
3f4cd19f
DL
12923 PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
12924 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
12925 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
c0d43d62 12926
47eacbab
VS
12927 PIPE_CONF_CHECK_X(dsi_pll.ctrl);
12928 PIPE_CONF_CHECK_X(dsi_pll.div);
12929
42571aef
VS
12930 if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
12931 PIPE_CONF_CHECK_I(pipe_bpp);
12932
2d112de7 12933 PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
a9a7e98a 12934 PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
5e550656 12935
66e985c0 12936#undef PIPE_CONF_CHECK_X
08a24034 12937#undef PIPE_CONF_CHECK_I
8106ddbd 12938#undef PIPE_CONF_CHECK_P
1bd1bd80 12939#undef PIPE_CONF_CHECK_FLAGS
5e550656 12940#undef PIPE_CONF_CHECK_CLOCK_FUZZY
bb760063 12941#undef PIPE_CONF_QUIRK
cfb23ed6 12942#undef INTEL_ERR_OR_DBG_KMS
88adfff1 12943
cfb23ed6 12944 return ret;
0e8ffe1b
DV
12945}
12946
e3b247da
VS
12947static void intel_pipe_config_sanity_check(struct drm_i915_private *dev_priv,
12948 const struct intel_crtc_state *pipe_config)
12949{
12950 if (pipe_config->has_pch_encoder) {
21a727b3 12951 int fdi_dotclock = intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
e3b247da
VS
12952 &pipe_config->fdi_m_n);
12953 int dotclock = pipe_config->base.adjusted_mode.crtc_clock;
12954
12955 /*
12956 * FDI already provided one idea for the dotclock.
12957 * Yell if the encoder disagrees.
12958 */
12959 WARN(!intel_fuzzy_clock_check(fdi_dotclock, dotclock),
12960 "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
12961 fdi_dotclock, dotclock);
12962 }
12963}
12964
c0ead703
ML
12965static void verify_wm_state(struct drm_crtc *crtc,
12966 struct drm_crtc_state *new_state)
08db6652 12967{
e7c84544 12968 struct drm_device *dev = crtc->dev;
08db6652
DL
12969 struct drm_i915_private *dev_priv = dev->dev_private;
12970 struct skl_ddb_allocation hw_ddb, *sw_ddb;
e7c84544
ML
12971 struct skl_ddb_entry *hw_entry, *sw_entry;
12972 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
12973 const enum pipe pipe = intel_crtc->pipe;
08db6652
DL
12974 int plane;
12975
e7c84544 12976 if (INTEL_INFO(dev)->gen < 9 || !new_state->active)
08db6652
DL
12977 return;
12978
12979 skl_ddb_get_hw_state(dev_priv, &hw_ddb);
12980 sw_ddb = &dev_priv->wm.skl_hw.ddb;
12981
e7c84544
ML
12982 /* planes */
12983 for_each_plane(dev_priv, pipe, plane) {
12984 hw_entry = &hw_ddb.plane[pipe][plane];
12985 sw_entry = &sw_ddb->plane[pipe][plane];
08db6652 12986
e7c84544 12987 if (skl_ddb_entry_equal(hw_entry, sw_entry))
08db6652
DL
12988 continue;
12989
e7c84544
ML
12990 DRM_ERROR("mismatch in DDB state pipe %c plane %d "
12991 "(expected (%u,%u), found (%u,%u))\n",
12992 pipe_name(pipe), plane + 1,
12993 sw_entry->start, sw_entry->end,
12994 hw_entry->start, hw_entry->end);
12995 }
08db6652 12996
e7c84544
ML
12997 /* cursor */
12998 hw_entry = &hw_ddb.plane[pipe][PLANE_CURSOR];
12999 sw_entry = &sw_ddb->plane[pipe][PLANE_CURSOR];
08db6652 13000
e7c84544 13001 if (!skl_ddb_entry_equal(hw_entry, sw_entry)) {
08db6652
DL
13002 DRM_ERROR("mismatch in DDB state pipe %c cursor "
13003 "(expected (%u,%u), found (%u,%u))\n",
13004 pipe_name(pipe),
13005 sw_entry->start, sw_entry->end,
13006 hw_entry->start, hw_entry->end);
13007 }
13008}
13009
91d1b4bd 13010static void
c0ead703 13011verify_connector_state(struct drm_device *dev, struct drm_crtc *crtc)
8af6cf88 13012{
35dd3c64 13013 struct drm_connector *connector;
8af6cf88 13014
e7c84544 13015 drm_for_each_connector(connector, dev) {
35dd3c64
ML
13016 struct drm_encoder *encoder = connector->encoder;
13017 struct drm_connector_state *state = connector->state;
ad3c558f 13018
e7c84544
ML
13019 if (state->crtc != crtc)
13020 continue;
13021
5a21b665 13022 intel_connector_verify_state(to_intel_connector(connector));
8af6cf88 13023
ad3c558f 13024 I915_STATE_WARN(state->best_encoder != encoder,
35dd3c64 13025 "connector's atomic encoder doesn't match legacy encoder\n");
8af6cf88 13026 }
91d1b4bd
DV
13027}
13028
13029static void
c0ead703 13030verify_encoder_state(struct drm_device *dev)
91d1b4bd
DV
13031{
13032 struct intel_encoder *encoder;
13033 struct intel_connector *connector;
8af6cf88 13034
b2784e15 13035 for_each_intel_encoder(dev, encoder) {
8af6cf88 13036 bool enabled = false;
4d20cd86 13037 enum pipe pipe;
8af6cf88
DV
13038
13039 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
13040 encoder->base.base.id,
8e329a03 13041 encoder->base.name);
8af6cf88 13042
3a3371ff 13043 for_each_intel_connector(dev, connector) {
4d20cd86 13044 if (connector->base.state->best_encoder != &encoder->base)
8af6cf88
DV
13045 continue;
13046 enabled = true;
ad3c558f
ML
13047
13048 I915_STATE_WARN(connector->base.state->crtc !=
13049 encoder->base.crtc,
13050 "connector's crtc doesn't match encoder crtc\n");
8af6cf88 13051 }
0e32b39c 13052
e2c719b7 13053 I915_STATE_WARN(!!encoder->base.crtc != enabled,
8af6cf88
DV
13054 "encoder's enabled state mismatch "
13055 "(expected %i, found %i)\n",
13056 !!encoder->base.crtc, enabled);
7c60d198
ML
13057
13058 if (!encoder->base.crtc) {
4d20cd86 13059 bool active;
7c60d198 13060
4d20cd86
ML
13061 active = encoder->get_hw_state(encoder, &pipe);
13062 I915_STATE_WARN(active,
13063 "encoder detached but still enabled on pipe %c.\n",
13064 pipe_name(pipe));
7c60d198 13065 }
8af6cf88 13066 }
91d1b4bd
DV
13067}
13068
13069static void
c0ead703
ML
13070verify_crtc_state(struct drm_crtc *crtc,
13071 struct drm_crtc_state *old_crtc_state,
13072 struct drm_crtc_state *new_crtc_state)
91d1b4bd 13073{
e7c84544 13074 struct drm_device *dev = crtc->dev;
fbee40df 13075 struct drm_i915_private *dev_priv = dev->dev_private;
91d1b4bd 13076 struct intel_encoder *encoder;
e7c84544
ML
13077 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13078 struct intel_crtc_state *pipe_config, *sw_config;
13079 struct drm_atomic_state *old_state;
13080 bool active;
045ac3b5 13081
e7c84544 13082 old_state = old_crtc_state->state;
ec2dc6a0 13083 __drm_atomic_helper_crtc_destroy_state(old_crtc_state);
e7c84544
ML
13084 pipe_config = to_intel_crtc_state(old_crtc_state);
13085 memset(pipe_config, 0, sizeof(*pipe_config));
13086 pipe_config->base.crtc = crtc;
13087 pipe_config->base.state = old_state;
8af6cf88 13088
78108b7c 13089 DRM_DEBUG_KMS("[CRTC:%d:%s]\n", crtc->base.id, crtc->name);
8af6cf88 13090
e7c84544 13091 active = dev_priv->display.get_pipe_config(intel_crtc, pipe_config);
d62cf62a 13092
e7c84544
ML
13093 /* hw state is inconsistent with the pipe quirk */
13094 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
13095 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
13096 active = new_crtc_state->active;
6c49f241 13097
e7c84544
ML
13098 I915_STATE_WARN(new_crtc_state->active != active,
13099 "crtc active state doesn't match with hw state "
13100 "(expected %i, found %i)\n", new_crtc_state->active, active);
0e8ffe1b 13101
e7c84544
ML
13102 I915_STATE_WARN(intel_crtc->active != new_crtc_state->active,
13103 "transitional active state does not match atomic hw state "
13104 "(expected %i, found %i)\n", new_crtc_state->active, intel_crtc->active);
4d20cd86 13105
e7c84544
ML
13106 for_each_encoder_on_crtc(dev, crtc, encoder) {
13107 enum pipe pipe;
4d20cd86 13108
e7c84544
ML
13109 active = encoder->get_hw_state(encoder, &pipe);
13110 I915_STATE_WARN(active != new_crtc_state->active,
13111 "[ENCODER:%i] active %i with crtc active %i\n",
13112 encoder->base.base.id, active, new_crtc_state->active);
4d20cd86 13113
e7c84544
ML
13114 I915_STATE_WARN(active && intel_crtc->pipe != pipe,
13115 "Encoder connected to wrong pipe %c\n",
13116 pipe_name(pipe));
4d20cd86 13117
e7c84544
ML
13118 if (active)
13119 encoder->get_config(encoder, pipe_config);
13120 }
53d9f4e9 13121
e7c84544
ML
13122 if (!new_crtc_state->active)
13123 return;
cfb23ed6 13124
e7c84544 13125 intel_pipe_config_sanity_check(dev_priv, pipe_config);
e3b247da 13126
e7c84544
ML
13127 sw_config = to_intel_crtc_state(crtc->state);
13128 if (!intel_pipe_config_compare(dev, sw_config,
13129 pipe_config, false)) {
13130 I915_STATE_WARN(1, "pipe state doesn't match!\n");
13131 intel_dump_pipe_config(intel_crtc, pipe_config,
13132 "[hw state]");
13133 intel_dump_pipe_config(intel_crtc, sw_config,
13134 "[sw state]");
8af6cf88
DV
13135 }
13136}
13137
91d1b4bd 13138static void
c0ead703
ML
13139verify_single_dpll_state(struct drm_i915_private *dev_priv,
13140 struct intel_shared_dpll *pll,
13141 struct drm_crtc *crtc,
13142 struct drm_crtc_state *new_state)
91d1b4bd 13143{
91d1b4bd 13144 struct intel_dpll_hw_state dpll_hw_state;
e7c84544
ML
13145 unsigned crtc_mask;
13146 bool active;
5358901f 13147
e7c84544 13148 memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
5358901f 13149
e7c84544 13150 DRM_DEBUG_KMS("%s\n", pll->name);
5358901f 13151
e7c84544 13152 active = pll->funcs.get_hw_state(dev_priv, pll, &dpll_hw_state);
5358901f 13153
e7c84544
ML
13154 if (!(pll->flags & INTEL_DPLL_ALWAYS_ON)) {
13155 I915_STATE_WARN(!pll->on && pll->active_mask,
13156 "pll in active use but not on in sw tracking\n");
13157 I915_STATE_WARN(pll->on && !pll->active_mask,
13158 "pll is on but not used by any active crtc\n");
13159 I915_STATE_WARN(pll->on != active,
13160 "pll on state mismatch (expected %i, found %i)\n",
13161 pll->on, active);
13162 }
5358901f 13163
e7c84544 13164 if (!crtc) {
2dd66ebd 13165 I915_STATE_WARN(pll->active_mask & ~pll->config.crtc_mask,
e7c84544
ML
13166 "more active pll users than references: %x vs %x\n",
13167 pll->active_mask, pll->config.crtc_mask);
5358901f 13168
e7c84544
ML
13169 return;
13170 }
13171
13172 crtc_mask = 1 << drm_crtc_index(crtc);
13173
13174 if (new_state->active)
13175 I915_STATE_WARN(!(pll->active_mask & crtc_mask),
13176 "pll active mismatch (expected pipe %c in active mask 0x%02x)\n",
13177 pipe_name(drm_crtc_index(crtc)), pll->active_mask);
13178 else
13179 I915_STATE_WARN(pll->active_mask & crtc_mask,
13180 "pll active mismatch (didn't expect pipe %c in active mask 0x%02x)\n",
13181 pipe_name(drm_crtc_index(crtc)), pll->active_mask);
2dd66ebd 13182
e7c84544
ML
13183 I915_STATE_WARN(!(pll->config.crtc_mask & crtc_mask),
13184 "pll enabled crtcs mismatch (expected 0x%x in 0x%02x)\n",
13185 crtc_mask, pll->config.crtc_mask);
66e985c0 13186
e7c84544
ML
13187 I915_STATE_WARN(pll->on && memcmp(&pll->config.hw_state,
13188 &dpll_hw_state,
13189 sizeof(dpll_hw_state)),
13190 "pll hw state mismatch\n");
13191}
13192
13193static void
c0ead703
ML
13194verify_shared_dpll_state(struct drm_device *dev, struct drm_crtc *crtc,
13195 struct drm_crtc_state *old_crtc_state,
13196 struct drm_crtc_state *new_crtc_state)
e7c84544
ML
13197{
13198 struct drm_i915_private *dev_priv = dev->dev_private;
13199 struct intel_crtc_state *old_state = to_intel_crtc_state(old_crtc_state);
13200 struct intel_crtc_state *new_state = to_intel_crtc_state(new_crtc_state);
13201
13202 if (new_state->shared_dpll)
c0ead703 13203 verify_single_dpll_state(dev_priv, new_state->shared_dpll, crtc, new_crtc_state);
e7c84544
ML
13204
13205 if (old_state->shared_dpll &&
13206 old_state->shared_dpll != new_state->shared_dpll) {
13207 unsigned crtc_mask = 1 << drm_crtc_index(crtc);
13208 struct intel_shared_dpll *pll = old_state->shared_dpll;
13209
13210 I915_STATE_WARN(pll->active_mask & crtc_mask,
13211 "pll active mismatch (didn't expect pipe %c in active mask)\n",
13212 pipe_name(drm_crtc_index(crtc)));
13213 I915_STATE_WARN(pll->config.crtc_mask & crtc_mask,
13214 "pll enabled crtcs mismatch (found %x in enabled mask)\n",
13215 pipe_name(drm_crtc_index(crtc)));
5358901f 13216 }
8af6cf88
DV
13217}
13218
e7c84544 13219static void
c0ead703 13220intel_modeset_verify_crtc(struct drm_crtc *crtc,
e7c84544
ML
13221 struct drm_crtc_state *old_state,
13222 struct drm_crtc_state *new_state)
13223{
5a21b665
DV
13224 if (!needs_modeset(new_state) &&
13225 !to_intel_crtc_state(new_state)->update_pipe)
13226 return;
13227
c0ead703 13228 verify_wm_state(crtc, new_state);
5a21b665 13229 verify_connector_state(crtc->dev, crtc);
c0ead703
ML
13230 verify_crtc_state(crtc, old_state, new_state);
13231 verify_shared_dpll_state(crtc->dev, crtc, old_state, new_state);
e7c84544
ML
13232}
13233
13234static void
c0ead703 13235verify_disabled_dpll_state(struct drm_device *dev)
e7c84544
ML
13236{
13237 struct drm_i915_private *dev_priv = dev->dev_private;
13238 int i;
13239
13240 for (i = 0; i < dev_priv->num_shared_dpll; i++)
c0ead703 13241 verify_single_dpll_state(dev_priv, &dev_priv->shared_dplls[i], NULL, NULL);
e7c84544
ML
13242}
13243
13244static void
c0ead703 13245intel_modeset_verify_disabled(struct drm_device *dev)
e7c84544 13246{
c0ead703
ML
13247 verify_encoder_state(dev);
13248 verify_connector_state(dev, NULL);
13249 verify_disabled_dpll_state(dev);
e7c84544
ML
13250}
13251
80715b2f
VS
13252static void update_scanline_offset(struct intel_crtc *crtc)
13253{
13254 struct drm_device *dev = crtc->base.dev;
13255
13256 /*
13257 * The scanline counter increments at the leading edge of hsync.
13258 *
13259 * On most platforms it starts counting from vtotal-1 on the
13260 * first active line. That means the scanline counter value is
13261 * always one less than what we would expect. Ie. just after
13262 * start of vblank, which also occurs at start of hsync (on the
13263 * last active line), the scanline counter will read vblank_start-1.
13264 *
13265 * On gen2 the scanline counter starts counting from 1 instead
13266 * of vtotal-1, so we have to subtract one (or rather add vtotal-1
13267 * to keep the value positive), instead of adding one.
13268 *
13269 * On HSW+ the behaviour of the scanline counter depends on the output
13270 * type. For DP ports it behaves like most other platforms, but on HDMI
13271 * there's an extra 1 line difference. So we need to add two instead of
13272 * one to the value.
13273 */
13274 if (IS_GEN2(dev)) {
124abe07 13275 const struct drm_display_mode *adjusted_mode = &crtc->config->base.adjusted_mode;
80715b2f
VS
13276 int vtotal;
13277
124abe07
VS
13278 vtotal = adjusted_mode->crtc_vtotal;
13279 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
80715b2f
VS
13280 vtotal /= 2;
13281
13282 crtc->scanline_offset = vtotal - 1;
13283 } else if (HAS_DDI(dev) &&
409ee761 13284 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI)) {
80715b2f
VS
13285 crtc->scanline_offset = 2;
13286 } else
13287 crtc->scanline_offset = 1;
13288}
13289
ad421372 13290static void intel_modeset_clear_plls(struct drm_atomic_state *state)
ed6739ef 13291{
225da59b 13292 struct drm_device *dev = state->dev;
ed6739ef 13293 struct drm_i915_private *dev_priv = to_i915(dev);
ad421372 13294 struct intel_shared_dpll_config *shared_dpll = NULL;
0a9ab303
ACO
13295 struct drm_crtc *crtc;
13296 struct drm_crtc_state *crtc_state;
0a9ab303 13297 int i;
ed6739ef
ACO
13298
13299 if (!dev_priv->display.crtc_compute_clock)
ad421372 13300 return;
ed6739ef 13301
0a9ab303 13302 for_each_crtc_in_state(state, crtc, crtc_state, i) {
fb1a38a9 13303 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8106ddbd
ACO
13304 struct intel_shared_dpll *old_dpll =
13305 to_intel_crtc_state(crtc->state)->shared_dpll;
0a9ab303 13306
fb1a38a9 13307 if (!needs_modeset(crtc_state))
225da59b
ACO
13308 continue;
13309
8106ddbd 13310 to_intel_crtc_state(crtc_state)->shared_dpll = NULL;
fb1a38a9 13311
8106ddbd 13312 if (!old_dpll)
fb1a38a9 13313 continue;
0a9ab303 13314
ad421372
ML
13315 if (!shared_dpll)
13316 shared_dpll = intel_atomic_get_shared_dpll_state(state);
ed6739ef 13317
8106ddbd 13318 intel_shared_dpll_config_put(shared_dpll, old_dpll, intel_crtc);
ad421372 13319 }
ed6739ef
ACO
13320}
13321
99d736a2
ML
13322/*
13323 * This implements the workaround described in the "notes" section of the mode
13324 * set sequence documentation. When going from no pipes or single pipe to
13325 * multiple pipes, and planes are enabled after the pipe, we need to wait at
13326 * least 2 vblanks on the first pipe before enabling planes on the second pipe.
13327 */
13328static int haswell_mode_set_planes_workaround(struct drm_atomic_state *state)
13329{
13330 struct drm_crtc_state *crtc_state;
13331 struct intel_crtc *intel_crtc;
13332 struct drm_crtc *crtc;
13333 struct intel_crtc_state *first_crtc_state = NULL;
13334 struct intel_crtc_state *other_crtc_state = NULL;
13335 enum pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
13336 int i;
13337
13338 /* look at all crtc's that are going to be enabled in during modeset */
13339 for_each_crtc_in_state(state, crtc, crtc_state, i) {
13340 intel_crtc = to_intel_crtc(crtc);
13341
13342 if (!crtc_state->active || !needs_modeset(crtc_state))
13343 continue;
13344
13345 if (first_crtc_state) {
13346 other_crtc_state = to_intel_crtc_state(crtc_state);
13347 break;
13348 } else {
13349 first_crtc_state = to_intel_crtc_state(crtc_state);
13350 first_pipe = intel_crtc->pipe;
13351 }
13352 }
13353
13354 /* No workaround needed? */
13355 if (!first_crtc_state)
13356 return 0;
13357
13358 /* w/a possibly needed, check how many crtc's are already enabled. */
13359 for_each_intel_crtc(state->dev, intel_crtc) {
13360 struct intel_crtc_state *pipe_config;
13361
13362 pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
13363 if (IS_ERR(pipe_config))
13364 return PTR_ERR(pipe_config);
13365
13366 pipe_config->hsw_workaround_pipe = INVALID_PIPE;
13367
13368 if (!pipe_config->base.active ||
13369 needs_modeset(&pipe_config->base))
13370 continue;
13371
13372 /* 2 or more enabled crtcs means no need for w/a */
13373 if (enabled_pipe != INVALID_PIPE)
13374 return 0;
13375
13376 enabled_pipe = intel_crtc->pipe;
13377 }
13378
13379 if (enabled_pipe != INVALID_PIPE)
13380 first_crtc_state->hsw_workaround_pipe = enabled_pipe;
13381 else if (other_crtc_state)
13382 other_crtc_state->hsw_workaround_pipe = first_pipe;
13383
13384 return 0;
13385}
13386
27c329ed
ML
13387static int intel_modeset_all_pipes(struct drm_atomic_state *state)
13388{
13389 struct drm_crtc *crtc;
13390 struct drm_crtc_state *crtc_state;
13391 int ret = 0;
13392
13393 /* add all active pipes to the state */
13394 for_each_crtc(state->dev, crtc) {
13395 crtc_state = drm_atomic_get_crtc_state(state, crtc);
13396 if (IS_ERR(crtc_state))
13397 return PTR_ERR(crtc_state);
13398
13399 if (!crtc_state->active || needs_modeset(crtc_state))
13400 continue;
13401
13402 crtc_state->mode_changed = true;
13403
13404 ret = drm_atomic_add_affected_connectors(state, crtc);
13405 if (ret)
13406 break;
13407
13408 ret = drm_atomic_add_affected_planes(state, crtc);
13409 if (ret)
13410 break;
13411 }
13412
13413 return ret;
13414}
13415
c347a676 13416static int intel_modeset_checks(struct drm_atomic_state *state)
054518dd 13417{
565602d7
ML
13418 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
13419 struct drm_i915_private *dev_priv = state->dev->dev_private;
13420 struct drm_crtc *crtc;
13421 struct drm_crtc_state *crtc_state;
13422 int ret = 0, i;
054518dd 13423
b359283a
ML
13424 if (!check_digital_port_conflicts(state)) {
13425 DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
13426 return -EINVAL;
13427 }
13428
565602d7
ML
13429 intel_state->modeset = true;
13430 intel_state->active_crtcs = dev_priv->active_crtcs;
13431
13432 for_each_crtc_in_state(state, crtc, crtc_state, i) {
13433 if (crtc_state->active)
13434 intel_state->active_crtcs |= 1 << i;
13435 else
13436 intel_state->active_crtcs &= ~(1 << i);
8b4a7d05
MR
13437
13438 if (crtc_state->active != crtc->state->active)
13439 intel_state->active_pipe_changes |= drm_crtc_mask(crtc);
565602d7
ML
13440 }
13441
054518dd
ACO
13442 /*
13443 * See if the config requires any additional preparation, e.g.
13444 * to adjust global state with pipes off. We need to do this
13445 * here so we can get the modeset_pipe updated config for the new
13446 * mode set on this crtc. For other crtcs we need to use the
13447 * adjusted_mode bits in the crtc directly.
13448 */
27c329ed 13449 if (dev_priv->display.modeset_calc_cdclk) {
c89e39f3 13450 if (!intel_state->cdclk_pll_vco)
63911d72 13451 intel_state->cdclk_pll_vco = dev_priv->cdclk_pll.vco;
b2045352
VS
13452 if (!intel_state->cdclk_pll_vco)
13453 intel_state->cdclk_pll_vco = dev_priv->skl_preferred_vco_freq;
c89e39f3 13454
27c329ed 13455 ret = dev_priv->display.modeset_calc_cdclk(state);
c89e39f3
CT
13456 if (ret < 0)
13457 return ret;
27c329ed 13458
c89e39f3 13459 if (intel_state->dev_cdclk != dev_priv->cdclk_freq ||
63911d72 13460 intel_state->cdclk_pll_vco != dev_priv->cdclk_pll.vco)
27c329ed
ML
13461 ret = intel_modeset_all_pipes(state);
13462
13463 if (ret < 0)
054518dd 13464 return ret;
e8788cbc
ML
13465
13466 DRM_DEBUG_KMS("New cdclk calculated to be atomic %u, actual %u\n",
13467 intel_state->cdclk, intel_state->dev_cdclk);
27c329ed 13468 } else
1a617b77 13469 to_intel_atomic_state(state)->cdclk = dev_priv->atomic_cdclk_freq;
054518dd 13470
ad421372 13471 intel_modeset_clear_plls(state);
054518dd 13472
565602d7 13473 if (IS_HASWELL(dev_priv))
ad421372 13474 return haswell_mode_set_planes_workaround(state);
99d736a2 13475
ad421372 13476 return 0;
c347a676
ACO
13477}
13478
aa363136
MR
13479/*
13480 * Handle calculation of various watermark data at the end of the atomic check
13481 * phase. The code here should be run after the per-crtc and per-plane 'check'
13482 * handlers to ensure that all derived state has been updated.
13483 */
55994c2c 13484static int calc_watermark_data(struct drm_atomic_state *state)
aa363136
MR
13485{
13486 struct drm_device *dev = state->dev;
98d39494 13487 struct drm_i915_private *dev_priv = to_i915(dev);
98d39494
MR
13488
13489 /* Is there platform-specific watermark information to calculate? */
13490 if (dev_priv->display.compute_global_watermarks)
55994c2c
MR
13491 return dev_priv->display.compute_global_watermarks(state);
13492
13493 return 0;
aa363136
MR
13494}
13495
74c090b1
ML
13496/**
13497 * intel_atomic_check - validate state object
13498 * @dev: drm device
13499 * @state: state to validate
13500 */
13501static int intel_atomic_check(struct drm_device *dev,
13502 struct drm_atomic_state *state)
c347a676 13503{
dd8b3bdb 13504 struct drm_i915_private *dev_priv = to_i915(dev);
aa363136 13505 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
c347a676
ACO
13506 struct drm_crtc *crtc;
13507 struct drm_crtc_state *crtc_state;
13508 int ret, i;
61333b60 13509 bool any_ms = false;
c347a676 13510
74c090b1 13511 ret = drm_atomic_helper_check_modeset(dev, state);
054518dd
ACO
13512 if (ret)
13513 return ret;
13514
c347a676 13515 for_each_crtc_in_state(state, crtc, crtc_state, i) {
cfb23ed6
ML
13516 struct intel_crtc_state *pipe_config =
13517 to_intel_crtc_state(crtc_state);
1ed51de9
DV
13518
13519 /* Catch I915_MODE_FLAG_INHERITED */
13520 if (crtc_state->mode.private_flags != crtc->state->mode.private_flags)
13521 crtc_state->mode_changed = true;
cfb23ed6 13522
af4a879e 13523 if (!needs_modeset(crtc_state))
c347a676
ACO
13524 continue;
13525
af4a879e
DV
13526 if (!crtc_state->enable) {
13527 any_ms = true;
cfb23ed6 13528 continue;
af4a879e 13529 }
cfb23ed6 13530
26495481
DV
13531 /* FIXME: For only active_changed we shouldn't need to do any
13532 * state recomputation at all. */
13533
1ed51de9
DV
13534 ret = drm_atomic_add_affected_connectors(state, crtc);
13535 if (ret)
13536 return ret;
b359283a 13537
cfb23ed6 13538 ret = intel_modeset_pipe_config(crtc, pipe_config);
25aa1c39
ML
13539 if (ret) {
13540 intel_dump_pipe_config(to_intel_crtc(crtc),
13541 pipe_config, "[failed]");
c347a676 13542 return ret;
25aa1c39 13543 }
c347a676 13544
73831236 13545 if (i915.fastboot &&
dd8b3bdb 13546 intel_pipe_config_compare(dev,
cfb23ed6 13547 to_intel_crtc_state(crtc->state),
1ed51de9 13548 pipe_config, true)) {
26495481 13549 crtc_state->mode_changed = false;
bfd16b2a 13550 to_intel_crtc_state(crtc_state)->update_pipe = true;
26495481
DV
13551 }
13552
af4a879e 13553 if (needs_modeset(crtc_state))
26495481 13554 any_ms = true;
cfb23ed6 13555
af4a879e
DV
13556 ret = drm_atomic_add_affected_planes(state, crtc);
13557 if (ret)
13558 return ret;
61333b60 13559
26495481
DV
13560 intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
13561 needs_modeset(crtc_state) ?
13562 "[modeset]" : "[fastset]");
c347a676
ACO
13563 }
13564
61333b60
ML
13565 if (any_ms) {
13566 ret = intel_modeset_checks(state);
13567
13568 if (ret)
13569 return ret;
27c329ed 13570 } else
dd8b3bdb 13571 intel_state->cdclk = dev_priv->cdclk_freq;
76305b1a 13572
dd8b3bdb 13573 ret = drm_atomic_helper_check_planes(dev, state);
aa363136
MR
13574 if (ret)
13575 return ret;
13576
f51be2e0 13577 intel_fbc_choose_crtc(dev_priv, state);
55994c2c 13578 return calc_watermark_data(state);
054518dd
ACO
13579}
13580
5008e874
ML
13581static int intel_atomic_prepare_commit(struct drm_device *dev,
13582 struct drm_atomic_state *state,
81072bfd 13583 bool nonblock)
5008e874 13584{
7580d774
ML
13585 struct drm_i915_private *dev_priv = dev->dev_private;
13586 struct drm_plane_state *plane_state;
5008e874 13587 struct drm_crtc_state *crtc_state;
7580d774 13588 struct drm_plane *plane;
5008e874
ML
13589 struct drm_crtc *crtc;
13590 int i, ret;
13591
5a21b665
DV
13592 for_each_crtc_in_state(state, crtc, crtc_state, i) {
13593 if (state->legacy_cursor_update)
a6747b73
ML
13594 continue;
13595
5a21b665
DV
13596 ret = intel_crtc_wait_for_pending_flips(crtc);
13597 if (ret)
13598 return ret;
5008e874 13599
5a21b665
DV
13600 if (atomic_read(&to_intel_crtc(crtc)->unpin_work_count) >= 2)
13601 flush_workqueue(dev_priv->wq);
d55dbd06
ML
13602 }
13603
f935675f
ML
13604 ret = mutex_lock_interruptible(&dev->struct_mutex);
13605 if (ret)
13606 return ret;
13607
5008e874 13608 ret = drm_atomic_helper_prepare_planes(dev, state);
f7e5838b 13609 mutex_unlock(&dev->struct_mutex);
7580d774 13610
21daaeee 13611 if (!ret && !nonblock) {
7580d774
ML
13612 for_each_plane_in_state(state, plane, plane_state, i) {
13613 struct intel_plane_state *intel_plane_state =
13614 to_intel_plane_state(plane_state);
13615
13616 if (!intel_plane_state->wait_req)
13617 continue;
13618
13619 ret = __i915_wait_request(intel_plane_state->wait_req,
299259a3 13620 true, NULL, NULL);
f7e5838b 13621 if (ret) {
f4457ae7
CW
13622 /* Any hang should be swallowed by the wait */
13623 WARN_ON(ret == -EIO);
f7e5838b
CW
13624 mutex_lock(&dev->struct_mutex);
13625 drm_atomic_helper_cleanup_planes(dev, state);
13626 mutex_unlock(&dev->struct_mutex);
7580d774 13627 break;
f7e5838b 13628 }
7580d774 13629 }
7580d774 13630 }
5008e874
ML
13631
13632 return ret;
13633}
13634
a2991414
ML
13635u32 intel_crtc_get_vblank_counter(struct intel_crtc *crtc)
13636{
13637 struct drm_device *dev = crtc->base.dev;
13638
13639 if (!dev->max_vblank_count)
13640 return drm_accurate_vblank_count(&crtc->base);
13641
13642 return dev->driver->get_vblank_counter(dev, crtc->pipe);
13643}
13644
5a21b665
DV
13645static void intel_atomic_wait_for_vblanks(struct drm_device *dev,
13646 struct drm_i915_private *dev_priv,
13647 unsigned crtc_mask)
e8861675 13648{
5a21b665
DV
13649 unsigned last_vblank_count[I915_MAX_PIPES];
13650 enum pipe pipe;
13651 int ret;
e8861675 13652
5a21b665
DV
13653 if (!crtc_mask)
13654 return;
e8861675 13655
5a21b665
DV
13656 for_each_pipe(dev_priv, pipe) {
13657 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
e8861675 13658
5a21b665 13659 if (!((1 << pipe) & crtc_mask))
e8861675
ML
13660 continue;
13661
5a21b665
DV
13662 ret = drm_crtc_vblank_get(crtc);
13663 if (WARN_ON(ret != 0)) {
13664 crtc_mask &= ~(1 << pipe);
13665 continue;
e8861675
ML
13666 }
13667
5a21b665 13668 last_vblank_count[pipe] = drm_crtc_vblank_count(crtc);
e8861675
ML
13669 }
13670
5a21b665
DV
13671 for_each_pipe(dev_priv, pipe) {
13672 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
13673 long lret;
e8861675 13674
5a21b665
DV
13675 if (!((1 << pipe) & crtc_mask))
13676 continue;
d55dbd06 13677
5a21b665
DV
13678 lret = wait_event_timeout(dev->vblank[pipe].queue,
13679 last_vblank_count[pipe] !=
13680 drm_crtc_vblank_count(crtc),
13681 msecs_to_jiffies(50));
d55dbd06 13682
5a21b665 13683 WARN(!lret, "pipe %c vblank wait timed out\n", pipe_name(pipe));
d55dbd06 13684
5a21b665 13685 drm_crtc_vblank_put(crtc);
d55dbd06
ML
13686 }
13687}
13688
5a21b665 13689static bool needs_vblank_wait(struct intel_crtc_state *crtc_state)
a6747b73 13690{
5a21b665
DV
13691 /* fb updated, need to unpin old fb */
13692 if (crtc_state->fb_changed)
13693 return true;
a6747b73 13694
5a21b665
DV
13695 /* wm changes, need vblank before final wm's */
13696 if (crtc_state->update_wm_post)
13697 return true;
a6747b73 13698
5a21b665
DV
13699 /*
13700 * cxsr is re-enabled after vblank.
13701 * This is already handled by crtc_state->update_wm_post,
13702 * but added for clarity.
13703 */
13704 if (crtc_state->disable_cxsr)
13705 return true;
a6747b73 13706
5a21b665 13707 return false;
e8861675
ML
13708}
13709
94f05024 13710static void intel_atomic_commit_tail(struct drm_atomic_state *state)
a6778b3c 13711{
94f05024 13712 struct drm_device *dev = state->dev;
565602d7 13713 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
fbee40df 13714 struct drm_i915_private *dev_priv = dev->dev_private;
29ceb0e6 13715 struct drm_crtc_state *old_crtc_state;
7580d774 13716 struct drm_crtc *crtc;
5a21b665 13717 struct intel_crtc_state *intel_cstate;
94f05024
DV
13718 struct drm_plane *plane;
13719 struct drm_plane_state *plane_state;
5a21b665
DV
13720 bool hw_check = intel_state->modeset;
13721 unsigned long put_domains[I915_MAX_PIPES] = {};
13722 unsigned crtc_vblank_mask = 0;
94f05024 13723 int i, ret;
a6778b3c 13724
94f05024
DV
13725 for_each_plane_in_state(state, plane, plane_state, i) {
13726 struct intel_plane_state *intel_plane_state =
13727 to_intel_plane_state(plane_state);
ea0000f0 13728
94f05024
DV
13729 if (!intel_plane_state->wait_req)
13730 continue;
d4afb8cc 13731
94f05024
DV
13732 ret = __i915_wait_request(intel_plane_state->wait_req,
13733 true, NULL, NULL);
13734 /* EIO should be eaten, and we can't get interrupted in the
13735 * worker, and blocking commits have waited already. */
13736 WARN_ON(ret);
13737 }
1c5e19f8 13738
ea0000f0
DV
13739 drm_atomic_helper_wait_for_dependencies(state);
13740
565602d7
ML
13741 if (intel_state->modeset) {
13742 memcpy(dev_priv->min_pixclk, intel_state->min_pixclk,
13743 sizeof(intel_state->min_pixclk));
13744 dev_priv->active_crtcs = intel_state->active_crtcs;
1a617b77 13745 dev_priv->atomic_cdclk_freq = intel_state->cdclk;
5a21b665
DV
13746
13747 intel_display_power_get(dev_priv, POWER_DOMAIN_MODESET);
565602d7
ML
13748 }
13749
29ceb0e6 13750 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
a539205a
ML
13751 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13752
5a21b665
DV
13753 if (needs_modeset(crtc->state) ||
13754 to_intel_crtc_state(crtc->state)->update_pipe) {
13755 hw_check = true;
13756
13757 put_domains[to_intel_crtc(crtc)->pipe] =
13758 modeset_get_crtc_power_domains(crtc,
13759 to_intel_crtc_state(crtc->state));
13760 }
13761
61333b60
ML
13762 if (!needs_modeset(crtc->state))
13763 continue;
13764
29ceb0e6 13765 intel_pre_plane_update(to_intel_crtc_state(old_crtc_state));
460da916 13766
29ceb0e6
VS
13767 if (old_crtc_state->active) {
13768 intel_crtc_disable_planes(crtc, old_crtc_state->plane_mask);
a539205a 13769 dev_priv->display.crtc_disable(crtc);
eddfcbcd 13770 intel_crtc->active = false;
58f9c0bc 13771 intel_fbc_disable(intel_crtc);
eddfcbcd 13772 intel_disable_shared_dpll(intel_crtc);
9bbc8258
VS
13773
13774 /*
13775 * Underruns don't always raise
13776 * interrupts, so check manually.
13777 */
13778 intel_check_cpu_fifo_underruns(dev_priv);
13779 intel_check_pch_fifo_underruns(dev_priv);
b9001114
ML
13780
13781 if (!crtc->state->active)
13782 intel_update_watermarks(crtc);
a539205a 13783 }
b8cecdf5 13784 }
7758a113 13785
ea9d758d
DV
13786 /* Only after disabling all output pipelines that will be changed can we
13787 * update the the output configuration. */
4740b0f2 13788 intel_modeset_update_crtc_state(state);
f6e5b160 13789
565602d7 13790 if (intel_state->modeset) {
4740b0f2 13791 drm_atomic_helper_update_legacy_modeset_state(state->dev, state);
33c8df89
ML
13792
13793 if (dev_priv->display.modeset_commit_cdclk &&
c89e39f3 13794 (intel_state->dev_cdclk != dev_priv->cdclk_freq ||
63911d72 13795 intel_state->cdclk_pll_vco != dev_priv->cdclk_pll.vco))
33c8df89 13796 dev_priv->display.modeset_commit_cdclk(state);
f6d1973d 13797
c0ead703 13798 intel_modeset_verify_disabled(dev);
4740b0f2 13799 }
47fab737 13800
a6778b3c 13801 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
29ceb0e6 13802 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
f6ac4b2a
ML
13803 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13804 bool modeset = needs_modeset(crtc->state);
5a21b665
DV
13805 struct intel_crtc_state *pipe_config =
13806 to_intel_crtc_state(crtc->state);
9f836f90 13807
f6ac4b2a 13808 if (modeset && crtc->state->active) {
a539205a
ML
13809 update_scanline_offset(to_intel_crtc(crtc));
13810 dev_priv->display.crtc_enable(crtc);
13811 }
80715b2f 13812
1f7528c4
DV
13813 /* Complete events for now disable pipes here. */
13814 if (modeset && !crtc->state->active && crtc->state->event) {
13815 spin_lock_irq(&dev->event_lock);
13816 drm_crtc_send_vblank_event(crtc, crtc->state->event);
13817 spin_unlock_irq(&dev->event_lock);
13818
13819 crtc->state->event = NULL;
13820 }
13821
f6ac4b2a 13822 if (!modeset)
29ceb0e6 13823 intel_pre_plane_update(to_intel_crtc_state(old_crtc_state));
f6ac4b2a 13824
5a21b665
DV
13825 if (crtc->state->active &&
13826 drm_atomic_get_existing_plane_state(state, crtc->primary))
faf68d92 13827 intel_fbc_enable(intel_crtc, pipe_config, to_intel_plane_state(crtc->primary->state));
5a21b665 13828
1f7528c4 13829 if (crtc->state->active)
5a21b665 13830 drm_atomic_helper_commit_planes_on_crtc(old_crtc_state);
f6d1973d 13831
5a21b665
DV
13832 if (pipe_config->base.active && needs_vblank_wait(pipe_config))
13833 crtc_vblank_mask |= 1 << i;
177246a8
MR
13834 }
13835
94f05024
DV
13836 /* FIXME: We should call drm_atomic_helper_commit_hw_done() here
13837 * already, but still need the state for the delayed optimization. To
13838 * fix this:
13839 * - wrap the optimization/post_plane_update stuff into a per-crtc work.
13840 * - schedule that vblank worker _before_ calling hw_done
13841 * - at the start of commit_tail, cancel it _synchrously
13842 * - switch over to the vblank wait helper in the core after that since
13843 * we don't need out special handling any more.
13844 */
5a21b665
DV
13845 if (!state->legacy_cursor_update)
13846 intel_atomic_wait_for_vblanks(dev, dev_priv, crtc_vblank_mask);
13847
13848 /*
13849 * Now that the vblank has passed, we can go ahead and program the
13850 * optimal watermarks on platforms that need two-step watermark
13851 * programming.
13852 *
13853 * TODO: Move this (and other cleanup) to an async worker eventually.
13854 */
13855 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
13856 intel_cstate = to_intel_crtc_state(crtc->state);
13857
13858 if (dev_priv->display.optimize_watermarks)
13859 dev_priv->display.optimize_watermarks(intel_cstate);
13860 }
13861
13862 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
13863 intel_post_plane_update(to_intel_crtc_state(old_crtc_state));
13864
13865 if (put_domains[i])
13866 modeset_put_power_domains(dev_priv, put_domains[i]);
13867
13868 intel_modeset_verify_crtc(crtc, old_crtc_state, crtc->state);
13869 }
13870
94f05024
DV
13871 drm_atomic_helper_commit_hw_done(state);
13872
5a21b665
DV
13873 if (intel_state->modeset)
13874 intel_display_power_put(dev_priv, POWER_DOMAIN_MODESET);
13875
13876 mutex_lock(&dev->struct_mutex);
13877 drm_atomic_helper_cleanup_planes(dev, state);
13878 mutex_unlock(&dev->struct_mutex);
13879
ea0000f0
DV
13880 drm_atomic_helper_commit_cleanup_done(state);
13881
ee165b1a 13882 drm_atomic_state_free(state);
f30da187 13883
75714940
MK
13884 /* As one of the primary mmio accessors, KMS has a high likelihood
13885 * of triggering bugs in unclaimed access. After we finish
13886 * modesetting, see if an error has been flagged, and if so
13887 * enable debugging for the next modeset - and hope we catch
13888 * the culprit.
13889 *
13890 * XXX note that we assume display power is on at this point.
13891 * This might hold true now but we need to add pm helper to check
13892 * unclaimed only when the hardware is on, as atomic commits
13893 * can happen also when the device is completely off.
13894 */
13895 intel_uncore_arm_unclaimed_mmio_detection(dev_priv);
94f05024
DV
13896}
13897
13898static void intel_atomic_commit_work(struct work_struct *work)
13899{
13900 struct drm_atomic_state *state = container_of(work,
13901 struct drm_atomic_state,
13902 commit_work);
13903 intel_atomic_commit_tail(state);
13904}
13905
6c9c1b38
DV
13906static void intel_atomic_track_fbs(struct drm_atomic_state *state)
13907{
13908 struct drm_plane_state *old_plane_state;
13909 struct drm_plane *plane;
13910 struct drm_i915_gem_object *obj, *old_obj;
13911 struct intel_plane *intel_plane;
13912 int i;
13913
13914 mutex_lock(&state->dev->struct_mutex);
13915 for_each_plane_in_state(state, plane, old_plane_state, i) {
13916 obj = intel_fb_obj(plane->state->fb);
13917 old_obj = intel_fb_obj(old_plane_state->fb);
13918 intel_plane = to_intel_plane(plane);
13919
13920 i915_gem_track_fb(old_obj, obj, intel_plane->frontbuffer_bit);
13921 }
13922 mutex_unlock(&state->dev->struct_mutex);
13923}
13924
94f05024
DV
13925/**
13926 * intel_atomic_commit - commit validated state object
13927 * @dev: DRM device
13928 * @state: the top-level driver state object
13929 * @nonblock: nonblocking commit
13930 *
13931 * This function commits a top-level state object that has been validated
13932 * with drm_atomic_helper_check().
13933 *
13934 * FIXME: Atomic modeset support for i915 is not yet complete. At the moment
13935 * nonblocking commits are only safe for pure plane updates. Everything else
13936 * should work though.
13937 *
13938 * RETURNS
13939 * Zero for success or -errno.
13940 */
13941static int intel_atomic_commit(struct drm_device *dev,
13942 struct drm_atomic_state *state,
13943 bool nonblock)
13944{
13945 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
13946 struct drm_i915_private *dev_priv = dev->dev_private;
13947 int ret = 0;
13948
13949 if (intel_state->modeset && nonblock) {
13950 DRM_DEBUG_KMS("nonblocking commit for modeset not yet implemented.\n");
13951 return -EINVAL;
13952 }
13953
13954 ret = drm_atomic_helper_setup_commit(state, nonblock);
13955 if (ret)
13956 return ret;
13957
13958 INIT_WORK(&state->commit_work, intel_atomic_commit_work);
13959
13960 ret = intel_atomic_prepare_commit(dev, state, nonblock);
13961 if (ret) {
13962 DRM_DEBUG_ATOMIC("Preparing state failed with %i\n", ret);
13963 return ret;
13964 }
13965
13966 drm_atomic_helper_swap_state(state, true);
13967 dev_priv->wm.distrust_bios_wm = false;
13968 dev_priv->wm.skl_results = intel_state->wm_results;
13969 intel_shared_dpll_commit(state);
6c9c1b38 13970 intel_atomic_track_fbs(state);
94f05024
DV
13971
13972 if (nonblock)
13973 queue_work(system_unbound_wq, &state->commit_work);
13974 else
13975 intel_atomic_commit_tail(state);
75714940 13976
74c090b1 13977 return 0;
7f27126e
JB
13978}
13979
c0c36b94
CW
13980void intel_crtc_restore_mode(struct drm_crtc *crtc)
13981{
83a57153
ACO
13982 struct drm_device *dev = crtc->dev;
13983 struct drm_atomic_state *state;
e694eb02 13984 struct drm_crtc_state *crtc_state;
2bfb4627 13985 int ret;
83a57153
ACO
13986
13987 state = drm_atomic_state_alloc(dev);
13988 if (!state) {
78108b7c
VS
13989 DRM_DEBUG_KMS("[CRTC:%d:%s] crtc restore failed, out of memory",
13990 crtc->base.id, crtc->name);
83a57153
ACO
13991 return;
13992 }
13993
e694eb02 13994 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
83a57153 13995
e694eb02
ML
13996retry:
13997 crtc_state = drm_atomic_get_crtc_state(state, crtc);
13998 ret = PTR_ERR_OR_ZERO(crtc_state);
13999 if (!ret) {
14000 if (!crtc_state->active)
14001 goto out;
83a57153 14002
e694eb02 14003 crtc_state->mode_changed = true;
74c090b1 14004 ret = drm_atomic_commit(state);
83a57153
ACO
14005 }
14006
e694eb02
ML
14007 if (ret == -EDEADLK) {
14008 drm_atomic_state_clear(state);
14009 drm_modeset_backoff(state->acquire_ctx);
14010 goto retry;
4ed9fb37 14011 }
4be07317 14012
2bfb4627 14013 if (ret)
e694eb02 14014out:
2bfb4627 14015 drm_atomic_state_free(state);
c0c36b94
CW
14016}
14017
25c5b266
DV
14018#undef for_each_intel_crtc_masked
14019
f6e5b160 14020static const struct drm_crtc_funcs intel_crtc_funcs = {
82cf435b 14021 .gamma_set = drm_atomic_helper_legacy_gamma_set,
74c090b1 14022 .set_config = drm_atomic_helper_set_config,
82cf435b 14023 .set_property = drm_atomic_helper_crtc_set_property,
f6e5b160 14024 .destroy = intel_crtc_destroy,
527b6abe 14025 .page_flip = intel_crtc_page_flip,
1356837e
MR
14026 .atomic_duplicate_state = intel_crtc_duplicate_state,
14027 .atomic_destroy_state = intel_crtc_destroy_state,
f6e5b160
CW
14028};
14029
6beb8c23
MR
14030/**
14031 * intel_prepare_plane_fb - Prepare fb for usage on plane
14032 * @plane: drm plane to prepare for
14033 * @fb: framebuffer to prepare for presentation
14034 *
14035 * Prepares a framebuffer for usage on a display plane. Generally this
14036 * involves pinning the underlying object and updating the frontbuffer tracking
14037 * bits. Some older platforms need special physical address handling for
14038 * cursor planes.
14039 *
f935675f
ML
14040 * Must be called with struct_mutex held.
14041 *
6beb8c23
MR
14042 * Returns 0 on success, negative error code on failure.
14043 */
14044int
14045intel_prepare_plane_fb(struct drm_plane *plane,
d136dfee 14046 const struct drm_plane_state *new_state)
465c120c
MR
14047{
14048 struct drm_device *dev = plane->dev;
844f9111 14049 struct drm_framebuffer *fb = new_state->fb;
6beb8c23 14050 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
1ee49399 14051 struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->state->fb);
c37efb99 14052 struct reservation_object *resv;
6beb8c23 14053 int ret = 0;
465c120c 14054
1ee49399 14055 if (!obj && !old_obj)
465c120c
MR
14056 return 0;
14057
5008e874
ML
14058 if (old_obj) {
14059 struct drm_crtc_state *crtc_state =
14060 drm_atomic_get_existing_crtc_state(new_state->state, plane->state->crtc);
14061
14062 /* Big Hammer, we also need to ensure that any pending
14063 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
14064 * current scanout is retired before unpinning the old
14065 * framebuffer. Note that we rely on userspace rendering
14066 * into the buffer attached to the pipe they are waiting
14067 * on. If not, userspace generates a GPU hang with IPEHR
14068 * point to the MI_WAIT_FOR_EVENT.
14069 *
14070 * This should only fail upon a hung GPU, in which case we
14071 * can safely continue.
14072 */
14073 if (needs_modeset(crtc_state))
14074 ret = i915_gem_object_wait_rendering(old_obj, true);
f4457ae7
CW
14075 if (ret) {
14076 /* GPU hangs should have been swallowed by the wait */
14077 WARN_ON(ret == -EIO);
f935675f 14078 return ret;
f4457ae7 14079 }
5008e874
ML
14080 }
14081
c37efb99
CW
14082 if (!obj)
14083 return 0;
14084
5a21b665 14085 /* For framebuffer backed by dmabuf, wait for fence */
c37efb99
CW
14086 resv = i915_gem_object_get_dmabuf_resv(obj);
14087 if (resv) {
5a21b665
DV
14088 long lret;
14089
c37efb99 14090 lret = reservation_object_wait_timeout_rcu(resv, false, true,
5a21b665
DV
14091 MAX_SCHEDULE_TIMEOUT);
14092 if (lret == -ERESTARTSYS)
14093 return lret;
14094
14095 WARN(lret < 0, "waiting returns %li\n", lret);
14096 }
14097
c37efb99 14098 if (plane->type == DRM_PLANE_TYPE_CURSOR &&
6beb8c23
MR
14099 INTEL_INFO(dev)->cursor_needs_physical) {
14100 int align = IS_I830(dev) ? 16 * 1024 : 256;
14101 ret = i915_gem_object_attach_phys(obj, align);
14102 if (ret)
14103 DRM_DEBUG_KMS("failed to attach phys object\n");
14104 } else {
3465c580 14105 ret = intel_pin_and_fence_fb_obj(fb, new_state->rotation);
6beb8c23 14106 }
465c120c 14107
c37efb99 14108 if (ret == 0) {
6c9c1b38
DV
14109 struct intel_plane_state *plane_state =
14110 to_intel_plane_state(new_state);
7580d774 14111
6c9c1b38
DV
14112 i915_gem_request_assign(&plane_state->wait_req,
14113 obj->last_write_req);
7580d774 14114 }
fdd508a6 14115
6beb8c23
MR
14116 return ret;
14117}
14118
38f3ce3a
MR
14119/**
14120 * intel_cleanup_plane_fb - Cleans up an fb after plane use
14121 * @plane: drm plane to clean up for
14122 * @fb: old framebuffer that was on plane
14123 *
14124 * Cleans up a framebuffer that has just been removed from a plane.
f935675f
ML
14125 *
14126 * Must be called with struct_mutex held.
38f3ce3a
MR
14127 */
14128void
14129intel_cleanup_plane_fb(struct drm_plane *plane,
d136dfee 14130 const struct drm_plane_state *old_state)
38f3ce3a
MR
14131{
14132 struct drm_device *dev = plane->dev;
7580d774 14133 struct intel_plane_state *old_intel_state;
1ee49399
ML
14134 struct drm_i915_gem_object *old_obj = intel_fb_obj(old_state->fb);
14135 struct drm_i915_gem_object *obj = intel_fb_obj(plane->state->fb);
38f3ce3a 14136
7580d774
ML
14137 old_intel_state = to_intel_plane_state(old_state);
14138
1ee49399 14139 if (!obj && !old_obj)
38f3ce3a
MR
14140 return;
14141
1ee49399
ML
14142 if (old_obj && (plane->type != DRM_PLANE_TYPE_CURSOR ||
14143 !INTEL_INFO(dev)->cursor_needs_physical))
3465c580 14144 intel_unpin_fb_obj(old_state->fb, old_state->rotation);
1ee49399 14145
7580d774 14146 i915_gem_request_assign(&old_intel_state->wait_req, NULL);
465c120c
MR
14147}
14148
6156a456
CK
14149int
14150skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
14151{
14152 int max_scale;
14153 struct drm_device *dev;
14154 struct drm_i915_private *dev_priv;
14155 int crtc_clock, cdclk;
14156
bf8a0af0 14157 if (!intel_crtc || !crtc_state->base.enable)
6156a456
CK
14158 return DRM_PLANE_HELPER_NO_SCALING;
14159
14160 dev = intel_crtc->base.dev;
14161 dev_priv = dev->dev_private;
14162 crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
27c329ed 14163 cdclk = to_intel_atomic_state(crtc_state->base.state)->cdclk;
6156a456 14164
54bf1ce6 14165 if (WARN_ON_ONCE(!crtc_clock || cdclk < crtc_clock))
6156a456
CK
14166 return DRM_PLANE_HELPER_NO_SCALING;
14167
14168 /*
14169 * skl max scale is lower of:
14170 * close to 3 but not 3, -1 is for that purpose
14171 * or
14172 * cdclk/crtc_clock
14173 */
14174 max_scale = min((1 << 16) * 3 - 1, (1 << 8) * ((cdclk << 8) / crtc_clock));
14175
14176 return max_scale;
14177}
14178
465c120c 14179static int
3c692a41 14180intel_check_primary_plane(struct drm_plane *plane,
061e4b8d 14181 struct intel_crtc_state *crtc_state,
3c692a41
GP
14182 struct intel_plane_state *state)
14183{
2b875c22
MR
14184 struct drm_crtc *crtc = state->base.crtc;
14185 struct drm_framebuffer *fb = state->base.fb;
6156a456 14186 int min_scale = DRM_PLANE_HELPER_NO_SCALING;
061e4b8d
ML
14187 int max_scale = DRM_PLANE_HELPER_NO_SCALING;
14188 bool can_position = false;
465c120c 14189
693bdc28
VS
14190 if (INTEL_INFO(plane->dev)->gen >= 9) {
14191 /* use scaler when colorkey is not required */
14192 if (state->ckey.flags == I915_SET_COLORKEY_NONE) {
14193 min_scale = 1;
14194 max_scale = skl_max_scale(to_intel_crtc(crtc), crtc_state);
14195 }
d8106366 14196 can_position = true;
6156a456 14197 }
d8106366 14198
061e4b8d
ML
14199 return drm_plane_helper_check_update(plane, crtc, fb, &state->src,
14200 &state->dst, &state->clip,
9b8b013d 14201 state->base.rotation,
da20eabd
ML
14202 min_scale, max_scale,
14203 can_position, true,
14204 &state->visible);
14af293f
GP
14205}
14206
5a21b665
DV
14207static void intel_begin_crtc_commit(struct drm_crtc *crtc,
14208 struct drm_crtc_state *old_crtc_state)
14209{
14210 struct drm_device *dev = crtc->dev;
14211 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
14212 struct intel_crtc_state *old_intel_state =
14213 to_intel_crtc_state(old_crtc_state);
14214 bool modeset = needs_modeset(crtc->state);
14215
14216 /* Perform vblank evasion around commit operation */
14217 intel_pipe_update_start(intel_crtc);
14218
14219 if (modeset)
14220 return;
14221
14222 if (crtc->state->color_mgmt_changed || to_intel_crtc_state(crtc->state)->update_pipe) {
14223 intel_color_set_csc(crtc->state);
14224 intel_color_load_luts(crtc->state);
14225 }
14226
14227 if (to_intel_crtc_state(crtc->state)->update_pipe)
14228 intel_update_pipe_config(intel_crtc, old_intel_state);
14229 else if (INTEL_INFO(dev)->gen >= 9)
14230 skl_detach_scalers(intel_crtc);
14231}
14232
14233static void intel_finish_crtc_commit(struct drm_crtc *crtc,
14234 struct drm_crtc_state *old_crtc_state)
14235{
14236 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
14237
14238 intel_pipe_update_end(intel_crtc, NULL);
14239}
14240
cf4c7c12 14241/**
4a3b8769
MR
14242 * intel_plane_destroy - destroy a plane
14243 * @plane: plane to destroy
cf4c7c12 14244 *
4a3b8769
MR
14245 * Common destruction function for all types of planes (primary, cursor,
14246 * sprite).
cf4c7c12 14247 */
4a3b8769 14248void intel_plane_destroy(struct drm_plane *plane)
465c120c 14249{
69ae561f
VS
14250 if (!plane)
14251 return;
14252
465c120c 14253 drm_plane_cleanup(plane);
69ae561f 14254 kfree(to_intel_plane(plane));
465c120c
MR
14255}
14256
65a3fea0 14257const struct drm_plane_funcs intel_plane_funcs = {
70a101f8
MR
14258 .update_plane = drm_atomic_helper_update_plane,
14259 .disable_plane = drm_atomic_helper_disable_plane,
3d7d6510 14260 .destroy = intel_plane_destroy,
c196e1d6 14261 .set_property = drm_atomic_helper_plane_set_property,
a98b3431
MR
14262 .atomic_get_property = intel_plane_atomic_get_property,
14263 .atomic_set_property = intel_plane_atomic_set_property,
ea2c67bb
MR
14264 .atomic_duplicate_state = intel_plane_duplicate_state,
14265 .atomic_destroy_state = intel_plane_destroy_state,
14266
465c120c
MR
14267};
14268
14269static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
14270 int pipe)
14271{
fca0ce2a
VS
14272 struct intel_plane *primary = NULL;
14273 struct intel_plane_state *state = NULL;
465c120c 14274 const uint32_t *intel_primary_formats;
45e3743a 14275 unsigned int num_formats;
fca0ce2a 14276 int ret;
465c120c
MR
14277
14278 primary = kzalloc(sizeof(*primary), GFP_KERNEL);
fca0ce2a
VS
14279 if (!primary)
14280 goto fail;
465c120c 14281
8e7d688b 14282 state = intel_create_plane_state(&primary->base);
fca0ce2a
VS
14283 if (!state)
14284 goto fail;
8e7d688b 14285 primary->base.state = &state->base;
ea2c67bb 14286
465c120c
MR
14287 primary->can_scale = false;
14288 primary->max_downscale = 1;
6156a456
CK
14289 if (INTEL_INFO(dev)->gen >= 9) {
14290 primary->can_scale = true;
af99ceda 14291 state->scaler_id = -1;
6156a456 14292 }
465c120c
MR
14293 primary->pipe = pipe;
14294 primary->plane = pipe;
a9ff8714 14295 primary->frontbuffer_bit = INTEL_FRONTBUFFER_PRIMARY(pipe);
c59cb179 14296 primary->check_plane = intel_check_primary_plane;
465c120c
MR
14297 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
14298 primary->plane = !pipe;
14299
6c0fd451
DL
14300 if (INTEL_INFO(dev)->gen >= 9) {
14301 intel_primary_formats = skl_primary_formats;
14302 num_formats = ARRAY_SIZE(skl_primary_formats);
a8d201af
ML
14303
14304 primary->update_plane = skylake_update_primary_plane;
14305 primary->disable_plane = skylake_disable_primary_plane;
14306 } else if (HAS_PCH_SPLIT(dev)) {
14307 intel_primary_formats = i965_primary_formats;
14308 num_formats = ARRAY_SIZE(i965_primary_formats);
14309
14310 primary->update_plane = ironlake_update_primary_plane;
14311 primary->disable_plane = i9xx_disable_primary_plane;
6c0fd451 14312 } else if (INTEL_INFO(dev)->gen >= 4) {
568db4f2
DL
14313 intel_primary_formats = i965_primary_formats;
14314 num_formats = ARRAY_SIZE(i965_primary_formats);
a8d201af
ML
14315
14316 primary->update_plane = i9xx_update_primary_plane;
14317 primary->disable_plane = i9xx_disable_primary_plane;
6c0fd451
DL
14318 } else {
14319 intel_primary_formats = i8xx_primary_formats;
14320 num_formats = ARRAY_SIZE(i8xx_primary_formats);
a8d201af
ML
14321
14322 primary->update_plane = i9xx_update_primary_plane;
14323 primary->disable_plane = i9xx_disable_primary_plane;
465c120c
MR
14324 }
14325
38573dc1
VS
14326 if (INTEL_INFO(dev)->gen >= 9)
14327 ret = drm_universal_plane_init(dev, &primary->base, 0,
14328 &intel_plane_funcs,
14329 intel_primary_formats, num_formats,
14330 DRM_PLANE_TYPE_PRIMARY,
14331 "plane 1%c", pipe_name(pipe));
14332 else if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
14333 ret = drm_universal_plane_init(dev, &primary->base, 0,
14334 &intel_plane_funcs,
14335 intel_primary_formats, num_formats,
14336 DRM_PLANE_TYPE_PRIMARY,
14337 "primary %c", pipe_name(pipe));
14338 else
14339 ret = drm_universal_plane_init(dev, &primary->base, 0,
14340 &intel_plane_funcs,
14341 intel_primary_formats, num_formats,
14342 DRM_PLANE_TYPE_PRIMARY,
14343 "plane %c", plane_name(primary->plane));
fca0ce2a
VS
14344 if (ret)
14345 goto fail;
48404c1e 14346
3b7a5119
SJ
14347 if (INTEL_INFO(dev)->gen >= 4)
14348 intel_create_rotation_property(dev, primary);
48404c1e 14349
ea2c67bb
MR
14350 drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
14351
465c120c 14352 return &primary->base;
fca0ce2a
VS
14353
14354fail:
14355 kfree(state);
14356 kfree(primary);
14357
14358 return NULL;
465c120c
MR
14359}
14360
3b7a5119
SJ
14361void intel_create_rotation_property(struct drm_device *dev, struct intel_plane *plane)
14362{
14363 if (!dev->mode_config.rotation_property) {
14364 unsigned long flags = BIT(DRM_ROTATE_0) |
14365 BIT(DRM_ROTATE_180);
14366
14367 if (INTEL_INFO(dev)->gen >= 9)
14368 flags |= BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_270);
14369
14370 dev->mode_config.rotation_property =
14371 drm_mode_create_rotation_property(dev, flags);
14372 }
14373 if (dev->mode_config.rotation_property)
14374 drm_object_attach_property(&plane->base.base,
14375 dev->mode_config.rotation_property,
14376 plane->base.state->rotation);
14377}
14378
3d7d6510 14379static int
852e787c 14380intel_check_cursor_plane(struct drm_plane *plane,
061e4b8d 14381 struct intel_crtc_state *crtc_state,
852e787c 14382 struct intel_plane_state *state)
3d7d6510 14383{
061e4b8d 14384 struct drm_crtc *crtc = crtc_state->base.crtc;
2b875c22 14385 struct drm_framebuffer *fb = state->base.fb;
757f9a3e 14386 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
b29ec92c 14387 enum pipe pipe = to_intel_plane(plane)->pipe;
757f9a3e
GP
14388 unsigned stride;
14389 int ret;
3d7d6510 14390
061e4b8d
ML
14391 ret = drm_plane_helper_check_update(plane, crtc, fb, &state->src,
14392 &state->dst, &state->clip,
9b8b013d 14393 state->base.rotation,
3d7d6510
MR
14394 DRM_PLANE_HELPER_NO_SCALING,
14395 DRM_PLANE_HELPER_NO_SCALING,
852e787c 14396 true, true, &state->visible);
757f9a3e
GP
14397 if (ret)
14398 return ret;
14399
757f9a3e
GP
14400 /* if we want to turn off the cursor ignore width and height */
14401 if (!obj)
da20eabd 14402 return 0;
757f9a3e 14403
757f9a3e 14404 /* Check for which cursor types we support */
061e4b8d 14405 if (!cursor_size_ok(plane->dev, state->base.crtc_w, state->base.crtc_h)) {
ea2c67bb
MR
14406 DRM_DEBUG("Cursor dimension %dx%d not supported\n",
14407 state->base.crtc_w, state->base.crtc_h);
757f9a3e
GP
14408 return -EINVAL;
14409 }
14410
ea2c67bb
MR
14411 stride = roundup_pow_of_two(state->base.crtc_w) * 4;
14412 if (obj->base.size < stride * state->base.crtc_h) {
757f9a3e
GP
14413 DRM_DEBUG_KMS("buffer is too small\n");
14414 return -ENOMEM;
14415 }
14416
3a656b54 14417 if (fb->modifier[0] != DRM_FORMAT_MOD_NONE) {
757f9a3e 14418 DRM_DEBUG_KMS("cursor cannot be tiled\n");
da20eabd 14419 return -EINVAL;
32b7eeec
MR
14420 }
14421
b29ec92c
VS
14422 /*
14423 * There's something wrong with the cursor on CHV pipe C.
14424 * If it straddles the left edge of the screen then
14425 * moving it away from the edge or disabling it often
14426 * results in a pipe underrun, and often that can lead to
14427 * dead pipe (constant underrun reported, and it scans
14428 * out just a solid color). To recover from that, the
14429 * display power well must be turned off and on again.
14430 * Refuse the put the cursor into that compromised position.
14431 */
14432 if (IS_CHERRYVIEW(plane->dev) && pipe == PIPE_C &&
14433 state->visible && state->base.crtc_x < 0) {
14434 DRM_DEBUG_KMS("CHV cursor C not allowed to straddle the left screen edge\n");
14435 return -EINVAL;
14436 }
14437
da20eabd 14438 return 0;
852e787c 14439}
3d7d6510 14440
a8ad0d8e
ML
14441static void
14442intel_disable_cursor_plane(struct drm_plane *plane,
7fabf5ef 14443 struct drm_crtc *crtc)
a8ad0d8e 14444{
f2858021
ML
14445 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
14446
14447 intel_crtc->cursor_addr = 0;
55a08b3f 14448 intel_crtc_update_cursor(crtc, NULL);
a8ad0d8e
ML
14449}
14450
f4a2cf29 14451static void
55a08b3f
ML
14452intel_update_cursor_plane(struct drm_plane *plane,
14453 const struct intel_crtc_state *crtc_state,
14454 const struct intel_plane_state *state)
852e787c 14455{
55a08b3f
ML
14456 struct drm_crtc *crtc = crtc_state->base.crtc;
14457 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ea2c67bb 14458 struct drm_device *dev = plane->dev;
2b875c22 14459 struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
a912f12f 14460 uint32_t addr;
852e787c 14461
f4a2cf29 14462 if (!obj)
a912f12f 14463 addr = 0;
f4a2cf29 14464 else if (!INTEL_INFO(dev)->cursor_needs_physical)
a912f12f 14465 addr = i915_gem_obj_ggtt_offset(obj);
f4a2cf29 14466 else
a912f12f 14467 addr = obj->phys_handle->busaddr;
852e787c 14468
a912f12f 14469 intel_crtc->cursor_addr = addr;
55a08b3f 14470 intel_crtc_update_cursor(crtc, state);
852e787c
GP
14471}
14472
3d7d6510
MR
14473static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
14474 int pipe)
14475{
fca0ce2a
VS
14476 struct intel_plane *cursor = NULL;
14477 struct intel_plane_state *state = NULL;
14478 int ret;
3d7d6510
MR
14479
14480 cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
fca0ce2a
VS
14481 if (!cursor)
14482 goto fail;
3d7d6510 14483
8e7d688b 14484 state = intel_create_plane_state(&cursor->base);
fca0ce2a
VS
14485 if (!state)
14486 goto fail;
8e7d688b 14487 cursor->base.state = &state->base;
ea2c67bb 14488
3d7d6510
MR
14489 cursor->can_scale = false;
14490 cursor->max_downscale = 1;
14491 cursor->pipe = pipe;
14492 cursor->plane = pipe;
a9ff8714 14493 cursor->frontbuffer_bit = INTEL_FRONTBUFFER_CURSOR(pipe);
c59cb179 14494 cursor->check_plane = intel_check_cursor_plane;
55a08b3f 14495 cursor->update_plane = intel_update_cursor_plane;
a8ad0d8e 14496 cursor->disable_plane = intel_disable_cursor_plane;
3d7d6510 14497
fca0ce2a
VS
14498 ret = drm_universal_plane_init(dev, &cursor->base, 0,
14499 &intel_plane_funcs,
14500 intel_cursor_formats,
14501 ARRAY_SIZE(intel_cursor_formats),
38573dc1
VS
14502 DRM_PLANE_TYPE_CURSOR,
14503 "cursor %c", pipe_name(pipe));
fca0ce2a
VS
14504 if (ret)
14505 goto fail;
4398ad45
VS
14506
14507 if (INTEL_INFO(dev)->gen >= 4) {
14508 if (!dev->mode_config.rotation_property)
14509 dev->mode_config.rotation_property =
14510 drm_mode_create_rotation_property(dev,
14511 BIT(DRM_ROTATE_0) |
14512 BIT(DRM_ROTATE_180));
14513 if (dev->mode_config.rotation_property)
14514 drm_object_attach_property(&cursor->base.base,
14515 dev->mode_config.rotation_property,
8e7d688b 14516 state->base.rotation);
4398ad45
VS
14517 }
14518
af99ceda
CK
14519 if (INTEL_INFO(dev)->gen >=9)
14520 state->scaler_id = -1;
14521
ea2c67bb
MR
14522 drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
14523
3d7d6510 14524 return &cursor->base;
fca0ce2a
VS
14525
14526fail:
14527 kfree(state);
14528 kfree(cursor);
14529
14530 return NULL;
3d7d6510
MR
14531}
14532
549e2bfb
CK
14533static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
14534 struct intel_crtc_state *crtc_state)
14535{
14536 int i;
14537 struct intel_scaler *intel_scaler;
14538 struct intel_crtc_scaler_state *scaler_state = &crtc_state->scaler_state;
14539
14540 for (i = 0; i < intel_crtc->num_scalers; i++) {
14541 intel_scaler = &scaler_state->scalers[i];
14542 intel_scaler->in_use = 0;
549e2bfb
CK
14543 intel_scaler->mode = PS_SCALER_MODE_DYN;
14544 }
14545
14546 scaler_state->scaler_id = -1;
14547}
14548
b358d0a6 14549static void intel_crtc_init(struct drm_device *dev, int pipe)
79e53945 14550{
fbee40df 14551 struct drm_i915_private *dev_priv = dev->dev_private;
79e53945 14552 struct intel_crtc *intel_crtc;
f5de6e07 14553 struct intel_crtc_state *crtc_state = NULL;
3d7d6510
MR
14554 struct drm_plane *primary = NULL;
14555 struct drm_plane *cursor = NULL;
8563b1e8 14556 int ret;
79e53945 14557
955382f3 14558 intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
79e53945
JB
14559 if (intel_crtc == NULL)
14560 return;
14561
f5de6e07
ACO
14562 crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
14563 if (!crtc_state)
14564 goto fail;
550acefd
ACO
14565 intel_crtc->config = crtc_state;
14566 intel_crtc->base.state = &crtc_state->base;
07878248 14567 crtc_state->base.crtc = &intel_crtc->base;
f5de6e07 14568
549e2bfb
CK
14569 /* initialize shared scalers */
14570 if (INTEL_INFO(dev)->gen >= 9) {
14571 if (pipe == PIPE_C)
14572 intel_crtc->num_scalers = 1;
14573 else
14574 intel_crtc->num_scalers = SKL_NUM_SCALERS;
14575
14576 skl_init_scalers(dev, intel_crtc, crtc_state);
14577 }
14578
465c120c 14579 primary = intel_primary_plane_create(dev, pipe);
3d7d6510
MR
14580 if (!primary)
14581 goto fail;
14582
14583 cursor = intel_cursor_plane_create(dev, pipe);
14584 if (!cursor)
14585 goto fail;
14586
465c120c 14587 ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
4d5d72b7
VS
14588 cursor, &intel_crtc_funcs,
14589 "pipe %c", pipe_name(pipe));
3d7d6510
MR
14590 if (ret)
14591 goto fail;
79e53945 14592
1f1c2e24
VS
14593 /*
14594 * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
8c0f92e1 14595 * is hooked to pipe B. Hence we want plane A feeding pipe B.
1f1c2e24 14596 */
80824003
JB
14597 intel_crtc->pipe = pipe;
14598 intel_crtc->plane = pipe;
3a77c4c4 14599 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
28c97730 14600 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
e2e767ab 14601 intel_crtc->plane = !pipe;
80824003
JB
14602 }
14603
4b0e333e
CW
14604 intel_crtc->cursor_base = ~0;
14605 intel_crtc->cursor_cntl = ~0;
dc41c154 14606 intel_crtc->cursor_size = ~0;
8d7849db 14607
852eb00d
VS
14608 intel_crtc->wm.cxsr_allowed = true;
14609
22fd0fab
JB
14610 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
14611 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
14612 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
14613 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
14614
79e53945 14615 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
87b6b101 14616
8563b1e8
LL
14617 intel_color_init(&intel_crtc->base);
14618
87b6b101 14619 WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
3d7d6510
MR
14620 return;
14621
14622fail:
69ae561f
VS
14623 intel_plane_destroy(primary);
14624 intel_plane_destroy(cursor);
f5de6e07 14625 kfree(crtc_state);
3d7d6510 14626 kfree(intel_crtc);
79e53945
JB
14627}
14628
752aa88a
JB
14629enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
14630{
14631 struct drm_encoder *encoder = connector->base.encoder;
6e9f798d 14632 struct drm_device *dev = connector->base.dev;
752aa88a 14633
51fd371b 14634 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
752aa88a 14635
d3babd3f 14636 if (!encoder || WARN_ON(!encoder->crtc))
752aa88a
JB
14637 return INVALID_PIPE;
14638
14639 return to_intel_crtc(encoder->crtc)->pipe;
14640}
14641
08d7b3d1 14642int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
05394f39 14643 struct drm_file *file)
08d7b3d1 14644{
08d7b3d1 14645 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
7707e653 14646 struct drm_crtc *drmmode_crtc;
c05422d5 14647 struct intel_crtc *crtc;
08d7b3d1 14648
7707e653 14649 drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
71240ed2 14650 if (!drmmode_crtc)
3f2c2057 14651 return -ENOENT;
08d7b3d1 14652
7707e653 14653 crtc = to_intel_crtc(drmmode_crtc);
c05422d5 14654 pipe_from_crtc_id->pipe = crtc->pipe;
08d7b3d1 14655
c05422d5 14656 return 0;
08d7b3d1
CW
14657}
14658
66a9278e 14659static int intel_encoder_clones(struct intel_encoder *encoder)
79e53945 14660{
66a9278e
DV
14661 struct drm_device *dev = encoder->base.dev;
14662 struct intel_encoder *source_encoder;
79e53945 14663 int index_mask = 0;
79e53945
JB
14664 int entry = 0;
14665
b2784e15 14666 for_each_intel_encoder(dev, source_encoder) {
bc079e8b 14667 if (encoders_cloneable(encoder, source_encoder))
66a9278e
DV
14668 index_mask |= (1 << entry);
14669
79e53945
JB
14670 entry++;
14671 }
4ef69c7a 14672
79e53945
JB
14673 return index_mask;
14674}
14675
4d302442
CW
14676static bool has_edp_a(struct drm_device *dev)
14677{
14678 struct drm_i915_private *dev_priv = dev->dev_private;
14679
14680 if (!IS_MOBILE(dev))
14681 return false;
14682
14683 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
14684 return false;
14685
e3589908 14686 if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
4d302442
CW
14687 return false;
14688
14689 return true;
14690}
14691
84b4e042
JB
14692static bool intel_crt_present(struct drm_device *dev)
14693{
14694 struct drm_i915_private *dev_priv = dev->dev_private;
14695
884497ed
DL
14696 if (INTEL_INFO(dev)->gen >= 9)
14697 return false;
14698
cf404ce4 14699 if (IS_HSW_ULT(dev) || IS_BDW_ULT(dev))
84b4e042
JB
14700 return false;
14701
14702 if (IS_CHERRYVIEW(dev))
14703 return false;
14704
65e472e4
VS
14705 if (HAS_PCH_LPT_H(dev) && I915_READ(SFUSE_STRAP) & SFUSE_STRAP_CRT_DISABLED)
14706 return false;
14707
70ac54d0
VS
14708 /* DDI E can't be used if DDI A requires 4 lanes */
14709 if (HAS_DDI(dev) && I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_A_4_LANES)
14710 return false;
14711
e4abb733 14712 if (!dev_priv->vbt.int_crt_support)
84b4e042
JB
14713 return false;
14714
14715 return true;
14716}
14717
79e53945
JB
14718static void intel_setup_outputs(struct drm_device *dev)
14719{
725e30ad 14720 struct drm_i915_private *dev_priv = dev->dev_private;
4ef69c7a 14721 struct intel_encoder *encoder;
cb0953d7 14722 bool dpd_is_edp = false;
79e53945 14723
97a824e1
ID
14724 /*
14725 * intel_edp_init_connector() depends on this completing first, to
14726 * prevent the registeration of both eDP and LVDS and the incorrect
14727 * sharing of the PPS.
14728 */
c9093354 14729 intel_lvds_init(dev);
79e53945 14730
84b4e042 14731 if (intel_crt_present(dev))
79935fca 14732 intel_crt_init(dev);
cb0953d7 14733
c776eb2e
VK
14734 if (IS_BROXTON(dev)) {
14735 /*
14736 * FIXME: Broxton doesn't support port detection via the
14737 * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
14738 * detect the ports.
14739 */
14740 intel_ddi_init(dev, PORT_A);
14741 intel_ddi_init(dev, PORT_B);
14742 intel_ddi_init(dev, PORT_C);
c6c794a2
SS
14743
14744 intel_dsi_init(dev);
c776eb2e 14745 } else if (HAS_DDI(dev)) {
0e72a5b5
ED
14746 int found;
14747
de31facd
JB
14748 /*
14749 * Haswell uses DDI functions to detect digital outputs.
14750 * On SKL pre-D0 the strap isn't connected, so we assume
14751 * it's there.
14752 */
77179400 14753 found = I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_INIT_DISPLAY_DETECTED;
de31facd 14754 /* WaIgnoreDDIAStrap: skl */
ef11bdb3 14755 if (found || IS_SKYLAKE(dev) || IS_KABYLAKE(dev))
0e72a5b5
ED
14756 intel_ddi_init(dev, PORT_A);
14757
14758 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
14759 * register */
14760 found = I915_READ(SFUSE_STRAP);
14761
14762 if (found & SFUSE_STRAP_DDIB_DETECTED)
14763 intel_ddi_init(dev, PORT_B);
14764 if (found & SFUSE_STRAP_DDIC_DETECTED)
14765 intel_ddi_init(dev, PORT_C);
14766 if (found & SFUSE_STRAP_DDID_DETECTED)
14767 intel_ddi_init(dev, PORT_D);
2800e4c2
RV
14768 /*
14769 * On SKL we don't have a way to detect DDI-E so we rely on VBT.
14770 */
ef11bdb3 14771 if ((IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) &&
2800e4c2
RV
14772 (dev_priv->vbt.ddi_port_info[PORT_E].supports_dp ||
14773 dev_priv->vbt.ddi_port_info[PORT_E].supports_dvi ||
14774 dev_priv->vbt.ddi_port_info[PORT_E].supports_hdmi))
14775 intel_ddi_init(dev, PORT_E);
14776
0e72a5b5 14777 } else if (HAS_PCH_SPLIT(dev)) {
cb0953d7 14778 int found;
5d8a7752 14779 dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
270b3042
DV
14780
14781 if (has_edp_a(dev))
14782 intel_dp_init(dev, DP_A, PORT_A);
cb0953d7 14783
dc0fa718 14784 if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
461ed3ca 14785 /* PCH SDVOB multiplex with HDMIB */
2a5c0832 14786 found = intel_sdvo_init(dev, PCH_SDVOB, PORT_B);
30ad48b7 14787 if (!found)
e2debe91 14788 intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
5eb08b69 14789 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
ab9d7c30 14790 intel_dp_init(dev, PCH_DP_B, PORT_B);
30ad48b7
ZW
14791 }
14792
dc0fa718 14793 if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
e2debe91 14794 intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
30ad48b7 14795
dc0fa718 14796 if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
e2debe91 14797 intel_hdmi_init(dev, PCH_HDMID, PORT_D);
30ad48b7 14798
5eb08b69 14799 if (I915_READ(PCH_DP_C) & DP_DETECTED)
ab9d7c30 14800 intel_dp_init(dev, PCH_DP_C, PORT_C);
5eb08b69 14801
270b3042 14802 if (I915_READ(PCH_DP_D) & DP_DETECTED)
ab9d7c30 14803 intel_dp_init(dev, PCH_DP_D, PORT_D);
666a4537 14804 } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
22f35042 14805 bool has_edp, has_port;
457c52d8 14806
e17ac6db
VS
14807 /*
14808 * The DP_DETECTED bit is the latched state of the DDC
14809 * SDA pin at boot. However since eDP doesn't require DDC
14810 * (no way to plug in a DP->HDMI dongle) the DDC pins for
14811 * eDP ports may have been muxed to an alternate function.
14812 * Thus we can't rely on the DP_DETECTED bit alone to detect
14813 * eDP ports. Consult the VBT as well as DP_DETECTED to
14814 * detect eDP ports.
22f35042
VS
14815 *
14816 * Sadly the straps seem to be missing sometimes even for HDMI
14817 * ports (eg. on Voyo V3 - CHT x7-Z8700), so check both strap
14818 * and VBT for the presence of the port. Additionally we can't
14819 * trust the port type the VBT declares as we've seen at least
14820 * HDMI ports that the VBT claim are DP or eDP.
e17ac6db 14821 */
457c52d8 14822 has_edp = intel_dp_is_edp(dev, PORT_B);
22f35042
VS
14823 has_port = intel_bios_is_port_present(dev_priv, PORT_B);
14824 if (I915_READ(VLV_DP_B) & DP_DETECTED || has_port)
457c52d8 14825 has_edp &= intel_dp_init(dev, VLV_DP_B, PORT_B);
22f35042 14826 if ((I915_READ(VLV_HDMIB) & SDVO_DETECTED || has_port) && !has_edp)
e66eb81d 14827 intel_hdmi_init(dev, VLV_HDMIB, PORT_B);
585a94b8 14828
457c52d8 14829 has_edp = intel_dp_is_edp(dev, PORT_C);
22f35042
VS
14830 has_port = intel_bios_is_port_present(dev_priv, PORT_C);
14831 if (I915_READ(VLV_DP_C) & DP_DETECTED || has_port)
457c52d8 14832 has_edp &= intel_dp_init(dev, VLV_DP_C, PORT_C);
22f35042 14833 if ((I915_READ(VLV_HDMIC) & SDVO_DETECTED || has_port) && !has_edp)
e66eb81d 14834 intel_hdmi_init(dev, VLV_HDMIC, PORT_C);
19c03924 14835
9418c1f1 14836 if (IS_CHERRYVIEW(dev)) {
22f35042
VS
14837 /*
14838 * eDP not supported on port D,
14839 * so no need to worry about it
14840 */
14841 has_port = intel_bios_is_port_present(dev_priv, PORT_D);
14842 if (I915_READ(CHV_DP_D) & DP_DETECTED || has_port)
e66eb81d 14843 intel_dp_init(dev, CHV_DP_D, PORT_D);
22f35042
VS
14844 if (I915_READ(CHV_HDMID) & SDVO_DETECTED || has_port)
14845 intel_hdmi_init(dev, CHV_HDMID, PORT_D);
9418c1f1
VS
14846 }
14847
3cfca973 14848 intel_dsi_init(dev);
09da55dc 14849 } else if (!IS_GEN2(dev) && !IS_PINEVIEW(dev)) {
27185ae1 14850 bool found = false;
7d57382e 14851
e2debe91 14852 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
b01f2c3a 14853 DRM_DEBUG_KMS("probing SDVOB\n");
2a5c0832 14854 found = intel_sdvo_init(dev, GEN3_SDVOB, PORT_B);
3fec3d2f 14855 if (!found && IS_G4X(dev)) {
b01f2c3a 14856 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
e2debe91 14857 intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
b01f2c3a 14858 }
27185ae1 14859
3fec3d2f 14860 if (!found && IS_G4X(dev))
ab9d7c30 14861 intel_dp_init(dev, DP_B, PORT_B);
725e30ad 14862 }
13520b05
KH
14863
14864 /* Before G4X SDVOC doesn't have its own detect register */
13520b05 14865
e2debe91 14866 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
b01f2c3a 14867 DRM_DEBUG_KMS("probing SDVOC\n");
2a5c0832 14868 found = intel_sdvo_init(dev, GEN3_SDVOC, PORT_C);
b01f2c3a 14869 }
27185ae1 14870
e2debe91 14871 if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
27185ae1 14872
3fec3d2f 14873 if (IS_G4X(dev)) {
b01f2c3a 14874 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
e2debe91 14875 intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
b01f2c3a 14876 }
3fec3d2f 14877 if (IS_G4X(dev))
ab9d7c30 14878 intel_dp_init(dev, DP_C, PORT_C);
725e30ad 14879 }
27185ae1 14880
3fec3d2f 14881 if (IS_G4X(dev) &&
e7281eab 14882 (I915_READ(DP_D) & DP_DETECTED))
ab9d7c30 14883 intel_dp_init(dev, DP_D, PORT_D);
bad720ff 14884 } else if (IS_GEN2(dev))
79e53945
JB
14885 intel_dvo_init(dev);
14886
103a196f 14887 if (SUPPORTS_TV(dev))
79e53945
JB
14888 intel_tv_init(dev);
14889
0bc12bcb 14890 intel_psr_init(dev);
7c8f8a70 14891
b2784e15 14892 for_each_intel_encoder(dev, encoder) {
4ef69c7a
CW
14893 encoder->base.possible_crtcs = encoder->crtc_mask;
14894 encoder->base.possible_clones =
66a9278e 14895 intel_encoder_clones(encoder);
79e53945 14896 }
47356eb6 14897
dde86e2d 14898 intel_init_pch_refclk(dev);
270b3042
DV
14899
14900 drm_helper_move_panel_connectors_to_head(dev);
79e53945
JB
14901}
14902
14903static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
14904{
60a5ca01 14905 struct drm_device *dev = fb->dev;
79e53945 14906 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
79e53945 14907
ef2d633e 14908 drm_framebuffer_cleanup(fb);
60a5ca01 14909 mutex_lock(&dev->struct_mutex);
ef2d633e 14910 WARN_ON(!intel_fb->obj->framebuffer_references--);
60a5ca01
VS
14911 drm_gem_object_unreference(&intel_fb->obj->base);
14912 mutex_unlock(&dev->struct_mutex);
79e53945
JB
14913 kfree(intel_fb);
14914}
14915
14916static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
05394f39 14917 struct drm_file *file,
79e53945
JB
14918 unsigned int *handle)
14919{
14920 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
05394f39 14921 struct drm_i915_gem_object *obj = intel_fb->obj;
79e53945 14922
cc917ab4
CW
14923 if (obj->userptr.mm) {
14924 DRM_DEBUG("attempting to use a userptr for a framebuffer, denied\n");
14925 return -EINVAL;
14926 }
14927
05394f39 14928 return drm_gem_handle_create(file, &obj->base, handle);
79e53945
JB
14929}
14930
86c98588
RV
14931static int intel_user_framebuffer_dirty(struct drm_framebuffer *fb,
14932 struct drm_file *file,
14933 unsigned flags, unsigned color,
14934 struct drm_clip_rect *clips,
14935 unsigned num_clips)
14936{
14937 struct drm_device *dev = fb->dev;
14938 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
14939 struct drm_i915_gem_object *obj = intel_fb->obj;
14940
14941 mutex_lock(&dev->struct_mutex);
74b4ea1e 14942 intel_fb_obj_flush(obj, false, ORIGIN_DIRTYFB);
86c98588
RV
14943 mutex_unlock(&dev->struct_mutex);
14944
14945 return 0;
14946}
14947
79e53945
JB
14948static const struct drm_framebuffer_funcs intel_fb_funcs = {
14949 .destroy = intel_user_framebuffer_destroy,
14950 .create_handle = intel_user_framebuffer_create_handle,
86c98588 14951 .dirty = intel_user_framebuffer_dirty,
79e53945
JB
14952};
14953
b321803d
DL
14954static
14955u32 intel_fb_pitch_limit(struct drm_device *dev, uint64_t fb_modifier,
14956 uint32_t pixel_format)
14957{
14958 u32 gen = INTEL_INFO(dev)->gen;
14959
14960 if (gen >= 9) {
ac484963
VS
14961 int cpp = drm_format_plane_cpp(pixel_format, 0);
14962
b321803d
DL
14963 /* "The stride in bytes must not exceed the of the size of 8K
14964 * pixels and 32K bytes."
14965 */
ac484963 14966 return min(8192 * cpp, 32768);
666a4537 14967 } else if (gen >= 5 && !IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
b321803d
DL
14968 return 32*1024;
14969 } else if (gen >= 4) {
14970 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14971 return 16*1024;
14972 else
14973 return 32*1024;
14974 } else if (gen >= 3) {
14975 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14976 return 8*1024;
14977 else
14978 return 16*1024;
14979 } else {
14980 /* XXX DSPC is limited to 4k tiled */
14981 return 8*1024;
14982 }
14983}
14984
b5ea642a
DV
14985static int intel_framebuffer_init(struct drm_device *dev,
14986 struct intel_framebuffer *intel_fb,
14987 struct drm_mode_fb_cmd2 *mode_cmd,
14988 struct drm_i915_gem_object *obj)
79e53945 14989{
7b49f948 14990 struct drm_i915_private *dev_priv = to_i915(dev);
6761dd31 14991 unsigned int aligned_height;
79e53945 14992 int ret;
b321803d 14993 u32 pitch_limit, stride_alignment;
79e53945 14994
dd4916c5
DV
14995 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
14996
2a80eada
DV
14997 if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
14998 /* Enforce that fb modifier and tiling mode match, but only for
14999 * X-tiled. This is needed for FBC. */
15000 if (!!(obj->tiling_mode == I915_TILING_X) !=
15001 !!(mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED)) {
15002 DRM_DEBUG("tiling_mode doesn't match fb modifier\n");
15003 return -EINVAL;
15004 }
15005 } else {
15006 if (obj->tiling_mode == I915_TILING_X)
15007 mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
15008 else if (obj->tiling_mode == I915_TILING_Y) {
15009 DRM_DEBUG("No Y tiling for legacy addfb\n");
15010 return -EINVAL;
15011 }
15012 }
15013
9a8f0a12
TU
15014 /* Passed in modifier sanity checking. */
15015 switch (mode_cmd->modifier[0]) {
15016 case I915_FORMAT_MOD_Y_TILED:
15017 case I915_FORMAT_MOD_Yf_TILED:
15018 if (INTEL_INFO(dev)->gen < 9) {
15019 DRM_DEBUG("Unsupported tiling 0x%llx!\n",
15020 mode_cmd->modifier[0]);
15021 return -EINVAL;
15022 }
15023 case DRM_FORMAT_MOD_NONE:
15024 case I915_FORMAT_MOD_X_TILED:
15025 break;
15026 default:
c0f40428
JB
15027 DRM_DEBUG("Unsupported fb modifier 0x%llx!\n",
15028 mode_cmd->modifier[0]);
57cd6508 15029 return -EINVAL;
c16ed4be 15030 }
57cd6508 15031
7b49f948
VS
15032 stride_alignment = intel_fb_stride_alignment(dev_priv,
15033 mode_cmd->modifier[0],
b321803d
DL
15034 mode_cmd->pixel_format);
15035 if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
15036 DRM_DEBUG("pitch (%d) must be at least %u byte aligned\n",
15037 mode_cmd->pitches[0], stride_alignment);
57cd6508 15038 return -EINVAL;
c16ed4be 15039 }
57cd6508 15040
b321803d
DL
15041 pitch_limit = intel_fb_pitch_limit(dev, mode_cmd->modifier[0],
15042 mode_cmd->pixel_format);
a35cdaa0 15043 if (mode_cmd->pitches[0] > pitch_limit) {
b321803d
DL
15044 DRM_DEBUG("%s pitch (%u) must be at less than %d\n",
15045 mode_cmd->modifier[0] != DRM_FORMAT_MOD_NONE ?
2a80eada 15046 "tiled" : "linear",
a35cdaa0 15047 mode_cmd->pitches[0], pitch_limit);
5d7bd705 15048 return -EINVAL;
c16ed4be 15049 }
5d7bd705 15050
2a80eada 15051 if (mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED &&
c16ed4be
CW
15052 mode_cmd->pitches[0] != obj->stride) {
15053 DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
15054 mode_cmd->pitches[0], obj->stride);
5d7bd705 15055 return -EINVAL;
c16ed4be 15056 }
5d7bd705 15057
57779d06 15058 /* Reject formats not supported by any plane early. */
308e5bcb 15059 switch (mode_cmd->pixel_format) {
57779d06 15060 case DRM_FORMAT_C8:
04b3924d
VS
15061 case DRM_FORMAT_RGB565:
15062 case DRM_FORMAT_XRGB8888:
15063 case DRM_FORMAT_ARGB8888:
57779d06
VS
15064 break;
15065 case DRM_FORMAT_XRGB1555:
c16ed4be 15066 if (INTEL_INFO(dev)->gen > 3) {
4ee62c76
VS
15067 DRM_DEBUG("unsupported pixel format: %s\n",
15068 drm_get_format_name(mode_cmd->pixel_format));
57779d06 15069 return -EINVAL;
c16ed4be 15070 }
57779d06 15071 break;
57779d06 15072 case DRM_FORMAT_ABGR8888:
666a4537
WB
15073 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev) &&
15074 INTEL_INFO(dev)->gen < 9) {
6c0fd451
DL
15075 DRM_DEBUG("unsupported pixel format: %s\n",
15076 drm_get_format_name(mode_cmd->pixel_format));
15077 return -EINVAL;
15078 }
15079 break;
15080 case DRM_FORMAT_XBGR8888:
04b3924d 15081 case DRM_FORMAT_XRGB2101010:
57779d06 15082 case DRM_FORMAT_XBGR2101010:
c16ed4be 15083 if (INTEL_INFO(dev)->gen < 4) {
4ee62c76
VS
15084 DRM_DEBUG("unsupported pixel format: %s\n",
15085 drm_get_format_name(mode_cmd->pixel_format));
57779d06 15086 return -EINVAL;
c16ed4be 15087 }
b5626747 15088 break;
7531208b 15089 case DRM_FORMAT_ABGR2101010:
666a4537 15090 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
7531208b
DL
15091 DRM_DEBUG("unsupported pixel format: %s\n",
15092 drm_get_format_name(mode_cmd->pixel_format));
15093 return -EINVAL;
15094 }
15095 break;
04b3924d
VS
15096 case DRM_FORMAT_YUYV:
15097 case DRM_FORMAT_UYVY:
15098 case DRM_FORMAT_YVYU:
15099 case DRM_FORMAT_VYUY:
c16ed4be 15100 if (INTEL_INFO(dev)->gen < 5) {
4ee62c76
VS
15101 DRM_DEBUG("unsupported pixel format: %s\n",
15102 drm_get_format_name(mode_cmd->pixel_format));
57779d06 15103 return -EINVAL;
c16ed4be 15104 }
57cd6508
CW
15105 break;
15106 default:
4ee62c76
VS
15107 DRM_DEBUG("unsupported pixel format: %s\n",
15108 drm_get_format_name(mode_cmd->pixel_format));
57cd6508
CW
15109 return -EINVAL;
15110 }
15111
90f9a336
VS
15112 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
15113 if (mode_cmd->offsets[0] != 0)
15114 return -EINVAL;
15115
ec2c981e 15116 aligned_height = intel_fb_align_height(dev, mode_cmd->height,
091df6cb
DV
15117 mode_cmd->pixel_format,
15118 mode_cmd->modifier[0]);
53155c0a
DV
15119 /* FIXME drm helper for size checks (especially planar formats)? */
15120 if (obj->base.size < aligned_height * mode_cmd->pitches[0])
15121 return -EINVAL;
15122
c7d73f6a
DV
15123 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
15124 intel_fb->obj = obj;
15125
2d7a215f
VS
15126 intel_fill_fb_info(dev_priv, &intel_fb->base);
15127
79e53945
JB
15128 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
15129 if (ret) {
15130 DRM_ERROR("framebuffer init failed %d\n", ret);
15131 return ret;
15132 }
15133
0b05e1e0
VS
15134 intel_fb->obj->framebuffer_references++;
15135
79e53945
JB
15136 return 0;
15137}
15138
79e53945
JB
15139static struct drm_framebuffer *
15140intel_user_framebuffer_create(struct drm_device *dev,
15141 struct drm_file *filp,
1eb83451 15142 const struct drm_mode_fb_cmd2 *user_mode_cmd)
79e53945 15143{
dcb1394e 15144 struct drm_framebuffer *fb;
05394f39 15145 struct drm_i915_gem_object *obj;
76dc3769 15146 struct drm_mode_fb_cmd2 mode_cmd = *user_mode_cmd;
79e53945 15147
a8ad0bd8 15148 obj = to_intel_bo(drm_gem_object_lookup(filp, mode_cmd.handles[0]));
c8725226 15149 if (&obj->base == NULL)
cce13ff7 15150 return ERR_PTR(-ENOENT);
79e53945 15151
92907cbb 15152 fb = intel_framebuffer_create(dev, &mode_cmd, obj);
dcb1394e
LW
15153 if (IS_ERR(fb))
15154 drm_gem_object_unreference_unlocked(&obj->base);
15155
15156 return fb;
79e53945
JB
15157}
15158
0695726e 15159#ifndef CONFIG_DRM_FBDEV_EMULATION
0632fef6 15160static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
4520f53a
DV
15161{
15162}
15163#endif
15164
79e53945 15165static const struct drm_mode_config_funcs intel_mode_funcs = {
79e53945 15166 .fb_create = intel_user_framebuffer_create,
0632fef6 15167 .output_poll_changed = intel_fbdev_output_poll_changed,
5ee67f1c
MR
15168 .atomic_check = intel_atomic_check,
15169 .atomic_commit = intel_atomic_commit,
de419ab6
ML
15170 .atomic_state_alloc = intel_atomic_state_alloc,
15171 .atomic_state_clear = intel_atomic_state_clear,
79e53945
JB
15172};
15173
88212941
ID
15174/**
15175 * intel_init_display_hooks - initialize the display modesetting hooks
15176 * @dev_priv: device private
15177 */
15178void intel_init_display_hooks(struct drm_i915_private *dev_priv)
e70236a8 15179{
88212941 15180 if (INTEL_INFO(dev_priv)->gen >= 9) {
bc8d7dff 15181 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
5724dbd1
DL
15182 dev_priv->display.get_initial_plane_config =
15183 skylake_get_initial_plane_config;
bc8d7dff
DL
15184 dev_priv->display.crtc_compute_clock =
15185 haswell_crtc_compute_clock;
15186 dev_priv->display.crtc_enable = haswell_crtc_enable;
15187 dev_priv->display.crtc_disable = haswell_crtc_disable;
88212941 15188 } else if (HAS_DDI(dev_priv)) {
0e8ffe1b 15189 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
5724dbd1
DL
15190 dev_priv->display.get_initial_plane_config =
15191 ironlake_get_initial_plane_config;
797d0259
ACO
15192 dev_priv->display.crtc_compute_clock =
15193 haswell_crtc_compute_clock;
4f771f10
PZ
15194 dev_priv->display.crtc_enable = haswell_crtc_enable;
15195 dev_priv->display.crtc_disable = haswell_crtc_disable;
88212941 15196 } else if (HAS_PCH_SPLIT(dev_priv)) {
0e8ffe1b 15197 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
5724dbd1
DL
15198 dev_priv->display.get_initial_plane_config =
15199 ironlake_get_initial_plane_config;
3fb37703
ACO
15200 dev_priv->display.crtc_compute_clock =
15201 ironlake_crtc_compute_clock;
76e5a89c
DV
15202 dev_priv->display.crtc_enable = ironlake_crtc_enable;
15203 dev_priv->display.crtc_disable = ironlake_crtc_disable;
65b3d6a9 15204 } else if (IS_CHERRYVIEW(dev_priv)) {
89b667f8 15205 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
5724dbd1
DL
15206 dev_priv->display.get_initial_plane_config =
15207 i9xx_get_initial_plane_config;
65b3d6a9
ACO
15208 dev_priv->display.crtc_compute_clock = chv_crtc_compute_clock;
15209 dev_priv->display.crtc_enable = valleyview_crtc_enable;
15210 dev_priv->display.crtc_disable = i9xx_crtc_disable;
15211 } else if (IS_VALLEYVIEW(dev_priv)) {
15212 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
15213 dev_priv->display.get_initial_plane_config =
15214 i9xx_get_initial_plane_config;
15215 dev_priv->display.crtc_compute_clock = vlv_crtc_compute_clock;
89b667f8
JB
15216 dev_priv->display.crtc_enable = valleyview_crtc_enable;
15217 dev_priv->display.crtc_disable = i9xx_crtc_disable;
19ec6693
ACO
15218 } else if (IS_G4X(dev_priv)) {
15219 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
15220 dev_priv->display.get_initial_plane_config =
15221 i9xx_get_initial_plane_config;
15222 dev_priv->display.crtc_compute_clock = g4x_crtc_compute_clock;
15223 dev_priv->display.crtc_enable = i9xx_crtc_enable;
15224 dev_priv->display.crtc_disable = i9xx_crtc_disable;
70e8aa21
ACO
15225 } else if (IS_PINEVIEW(dev_priv)) {
15226 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
15227 dev_priv->display.get_initial_plane_config =
15228 i9xx_get_initial_plane_config;
15229 dev_priv->display.crtc_compute_clock = pnv_crtc_compute_clock;
15230 dev_priv->display.crtc_enable = i9xx_crtc_enable;
15231 dev_priv->display.crtc_disable = i9xx_crtc_disable;
81c97f52 15232 } else if (!IS_GEN2(dev_priv)) {
0e8ffe1b 15233 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
5724dbd1
DL
15234 dev_priv->display.get_initial_plane_config =
15235 i9xx_get_initial_plane_config;
d6dfee7a 15236 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
76e5a89c
DV
15237 dev_priv->display.crtc_enable = i9xx_crtc_enable;
15238 dev_priv->display.crtc_disable = i9xx_crtc_disable;
81c97f52
ACO
15239 } else {
15240 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
15241 dev_priv->display.get_initial_plane_config =
15242 i9xx_get_initial_plane_config;
15243 dev_priv->display.crtc_compute_clock = i8xx_crtc_compute_clock;
15244 dev_priv->display.crtc_enable = i9xx_crtc_enable;
15245 dev_priv->display.crtc_disable = i9xx_crtc_disable;
f564048e 15246 }
e70236a8 15247
e70236a8 15248 /* Returns the core display clock speed */
88212941 15249 if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv))
1652d19e
VS
15250 dev_priv->display.get_display_clock_speed =
15251 skylake_get_display_clock_speed;
88212941 15252 else if (IS_BROXTON(dev_priv))
acd3f3d3
BP
15253 dev_priv->display.get_display_clock_speed =
15254 broxton_get_display_clock_speed;
88212941 15255 else if (IS_BROADWELL(dev_priv))
1652d19e
VS
15256 dev_priv->display.get_display_clock_speed =
15257 broadwell_get_display_clock_speed;
88212941 15258 else if (IS_HASWELL(dev_priv))
1652d19e
VS
15259 dev_priv->display.get_display_clock_speed =
15260 haswell_get_display_clock_speed;
88212941 15261 else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
25eb05fc
JB
15262 dev_priv->display.get_display_clock_speed =
15263 valleyview_get_display_clock_speed;
88212941 15264 else if (IS_GEN5(dev_priv))
b37a6434
VS
15265 dev_priv->display.get_display_clock_speed =
15266 ilk_get_display_clock_speed;
88212941
ID
15267 else if (IS_I945G(dev_priv) || IS_BROADWATER(dev_priv) ||
15268 IS_GEN6(dev_priv) || IS_IVYBRIDGE(dev_priv))
e70236a8
JB
15269 dev_priv->display.get_display_clock_speed =
15270 i945_get_display_clock_speed;
88212941 15271 else if (IS_GM45(dev_priv))
34edce2f
VS
15272 dev_priv->display.get_display_clock_speed =
15273 gm45_get_display_clock_speed;
88212941 15274 else if (IS_CRESTLINE(dev_priv))
34edce2f
VS
15275 dev_priv->display.get_display_clock_speed =
15276 i965gm_get_display_clock_speed;
88212941 15277 else if (IS_PINEVIEW(dev_priv))
34edce2f
VS
15278 dev_priv->display.get_display_clock_speed =
15279 pnv_get_display_clock_speed;
88212941 15280 else if (IS_G33(dev_priv) || IS_G4X(dev_priv))
34edce2f
VS
15281 dev_priv->display.get_display_clock_speed =
15282 g33_get_display_clock_speed;
88212941 15283 else if (IS_I915G(dev_priv))
e70236a8
JB
15284 dev_priv->display.get_display_clock_speed =
15285 i915_get_display_clock_speed;
88212941 15286 else if (IS_I945GM(dev_priv) || IS_845G(dev_priv))
e70236a8
JB
15287 dev_priv->display.get_display_clock_speed =
15288 i9xx_misc_get_display_clock_speed;
88212941 15289 else if (IS_I915GM(dev_priv))
e70236a8
JB
15290 dev_priv->display.get_display_clock_speed =
15291 i915gm_get_display_clock_speed;
88212941 15292 else if (IS_I865G(dev_priv))
e70236a8
JB
15293 dev_priv->display.get_display_clock_speed =
15294 i865_get_display_clock_speed;
88212941 15295 else if (IS_I85X(dev_priv))
e70236a8 15296 dev_priv->display.get_display_clock_speed =
1b1d2716 15297 i85x_get_display_clock_speed;
623e01e5 15298 else { /* 830 */
88212941 15299 WARN(!IS_I830(dev_priv), "Unknown platform. Assuming 133 MHz CDCLK\n");
e70236a8
JB
15300 dev_priv->display.get_display_clock_speed =
15301 i830_get_display_clock_speed;
623e01e5 15302 }
e70236a8 15303
88212941 15304 if (IS_GEN5(dev_priv)) {
3bb11b53 15305 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
88212941 15306 } else if (IS_GEN6(dev_priv)) {
3bb11b53 15307 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
88212941 15308 } else if (IS_IVYBRIDGE(dev_priv)) {
3bb11b53
SJ
15309 /* FIXME: detect B0+ stepping and use auto training */
15310 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
88212941 15311 } else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
3bb11b53 15312 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
445e780b
VS
15313 }
15314
15315 if (IS_BROADWELL(dev_priv)) {
15316 dev_priv->display.modeset_commit_cdclk =
15317 broadwell_modeset_commit_cdclk;
15318 dev_priv->display.modeset_calc_cdclk =
15319 broadwell_modeset_calc_cdclk;
88212941 15320 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
27c329ed
ML
15321 dev_priv->display.modeset_commit_cdclk =
15322 valleyview_modeset_commit_cdclk;
15323 dev_priv->display.modeset_calc_cdclk =
15324 valleyview_modeset_calc_cdclk;
88212941 15325 } else if (IS_BROXTON(dev_priv)) {
27c329ed 15326 dev_priv->display.modeset_commit_cdclk =
324513c0 15327 bxt_modeset_commit_cdclk;
27c329ed 15328 dev_priv->display.modeset_calc_cdclk =
324513c0 15329 bxt_modeset_calc_cdclk;
c89e39f3
CT
15330 } else if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv)) {
15331 dev_priv->display.modeset_commit_cdclk =
15332 skl_modeset_commit_cdclk;
15333 dev_priv->display.modeset_calc_cdclk =
15334 skl_modeset_calc_cdclk;
e70236a8 15335 }
5a21b665
DV
15336
15337 switch (INTEL_INFO(dev_priv)->gen) {
15338 case 2:
15339 dev_priv->display.queue_flip = intel_gen2_queue_flip;
15340 break;
15341
15342 case 3:
15343 dev_priv->display.queue_flip = intel_gen3_queue_flip;
15344 break;
15345
15346 case 4:
15347 case 5:
15348 dev_priv->display.queue_flip = intel_gen4_queue_flip;
15349 break;
15350
15351 case 6:
15352 dev_priv->display.queue_flip = intel_gen6_queue_flip;
15353 break;
15354 case 7:
15355 case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
15356 dev_priv->display.queue_flip = intel_gen7_queue_flip;
15357 break;
15358 case 9:
15359 /* Drop through - unsupported since execlist only. */
15360 default:
15361 /* Default just returns -ENODEV to indicate unsupported */
15362 dev_priv->display.queue_flip = intel_default_queue_flip;
15363 }
e70236a8
JB
15364}
15365
b690e96c
JB
15366/*
15367 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
15368 * resume, or other times. This quirk makes sure that's the case for
15369 * affected systems.
15370 */
0206e353 15371static void quirk_pipea_force(struct drm_device *dev)
b690e96c
JB
15372{
15373 struct drm_i915_private *dev_priv = dev->dev_private;
15374
15375 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
bc0daf48 15376 DRM_INFO("applying pipe a force quirk\n");
b690e96c
JB
15377}
15378
b6b5d049
VS
15379static void quirk_pipeb_force(struct drm_device *dev)
15380{
15381 struct drm_i915_private *dev_priv = dev->dev_private;
15382
15383 dev_priv->quirks |= QUIRK_PIPEB_FORCE;
15384 DRM_INFO("applying pipe b force quirk\n");
15385}
15386
435793df
KP
15387/*
15388 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
15389 */
15390static void quirk_ssc_force_disable(struct drm_device *dev)
15391{
15392 struct drm_i915_private *dev_priv = dev->dev_private;
15393 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
bc0daf48 15394 DRM_INFO("applying lvds SSC disable quirk\n");
435793df
KP
15395}
15396
4dca20ef 15397/*
5a15ab5b
CE
15398 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
15399 * brightness value
4dca20ef
CE
15400 */
15401static void quirk_invert_brightness(struct drm_device *dev)
15402{
15403 struct drm_i915_private *dev_priv = dev->dev_private;
15404 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
bc0daf48 15405 DRM_INFO("applying inverted panel brightness quirk\n");
435793df
KP
15406}
15407
9c72cc6f
SD
15408/* Some VBT's incorrectly indicate no backlight is present */
15409static void quirk_backlight_present(struct drm_device *dev)
15410{
15411 struct drm_i915_private *dev_priv = dev->dev_private;
15412 dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
15413 DRM_INFO("applying backlight present quirk\n");
15414}
15415
b690e96c
JB
15416struct intel_quirk {
15417 int device;
15418 int subsystem_vendor;
15419 int subsystem_device;
15420 void (*hook)(struct drm_device *dev);
15421};
15422
5f85f176
EE
15423/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
15424struct intel_dmi_quirk {
15425 void (*hook)(struct drm_device *dev);
15426 const struct dmi_system_id (*dmi_id_list)[];
15427};
15428
15429static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
15430{
15431 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
15432 return 1;
15433}
15434
15435static const struct intel_dmi_quirk intel_dmi_quirks[] = {
15436 {
15437 .dmi_id_list = &(const struct dmi_system_id[]) {
15438 {
15439 .callback = intel_dmi_reverse_brightness,
15440 .ident = "NCR Corporation",
15441 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
15442 DMI_MATCH(DMI_PRODUCT_NAME, ""),
15443 },
15444 },
15445 { } /* terminating entry */
15446 },
15447 .hook = quirk_invert_brightness,
15448 },
15449};
15450
c43b5634 15451static struct intel_quirk intel_quirks[] = {
b690e96c
JB
15452 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
15453 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
15454
b690e96c
JB
15455 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
15456 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
15457
5f080c0f
VS
15458 /* 830 needs to leave pipe A & dpll A up */
15459 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
15460
b6b5d049
VS
15461 /* 830 needs to leave pipe B & dpll B up */
15462 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
15463
435793df
KP
15464 /* Lenovo U160 cannot use SSC on LVDS */
15465 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
070d329a
MAS
15466
15467 /* Sony Vaio Y cannot use SSC on LVDS */
15468 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
5a15ab5b 15469
be505f64
AH
15470 /* Acer Aspire 5734Z must invert backlight brightness */
15471 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
15472
15473 /* Acer/eMachines G725 */
15474 { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
15475
15476 /* Acer/eMachines e725 */
15477 { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
15478
15479 /* Acer/Packard Bell NCL20 */
15480 { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
15481
15482 /* Acer Aspire 4736Z */
15483 { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
0f540c3a
JN
15484
15485 /* Acer Aspire 5336 */
15486 { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
2e93a1aa
SD
15487
15488 /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
15489 { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
d4967d8c 15490
dfb3d47b
SD
15491 /* Acer C720 Chromebook (Core i3 4005U) */
15492 { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
15493
b2a9601c 15494 /* Apple Macbook 2,1 (Core 2 T7400) */
15495 { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
15496
1b9448b0
JN
15497 /* Apple Macbook 4,1 */
15498 { 0x2a02, 0x106b, 0x00a1, quirk_backlight_present },
15499
d4967d8c
SD
15500 /* Toshiba CB35 Chromebook (Celeron 2955U) */
15501 { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
724cb06f
SD
15502
15503 /* HP Chromebook 14 (Celeron 2955U) */
15504 { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
cf6f0af9
JN
15505
15506 /* Dell Chromebook 11 */
15507 { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
9be64eee
JN
15508
15509 /* Dell Chromebook 11 (2015 version) */
15510 { 0x0a16, 0x1028, 0x0a35, quirk_backlight_present },
b690e96c
JB
15511};
15512
15513static void intel_init_quirks(struct drm_device *dev)
15514{
15515 struct pci_dev *d = dev->pdev;
15516 int i;
15517
15518 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
15519 struct intel_quirk *q = &intel_quirks[i];
15520
15521 if (d->device == q->device &&
15522 (d->subsystem_vendor == q->subsystem_vendor ||
15523 q->subsystem_vendor == PCI_ANY_ID) &&
15524 (d->subsystem_device == q->subsystem_device ||
15525 q->subsystem_device == PCI_ANY_ID))
15526 q->hook(dev);
15527 }
5f85f176
EE
15528 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
15529 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
15530 intel_dmi_quirks[i].hook(dev);
15531 }
b690e96c
JB
15532}
15533
9cce37f4
JB
15534/* Disable the VGA plane that we never use */
15535static void i915_disable_vga(struct drm_device *dev)
15536{
15537 struct drm_i915_private *dev_priv = dev->dev_private;
15538 u8 sr1;
f0f59a00 15539 i915_reg_t vga_reg = i915_vgacntrl_reg(dev);
9cce37f4 15540
2b37c616 15541 /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
9cce37f4 15542 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
3fdcf431 15543 outb(SR01, VGA_SR_INDEX);
9cce37f4
JB
15544 sr1 = inb(VGA_SR_DATA);
15545 outb(sr1 | 1<<5, VGA_SR_DATA);
15546 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
15547 udelay(300);
15548
01f5a626 15549 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
9cce37f4
JB
15550 POSTING_READ(vga_reg);
15551}
15552
f817586c
DV
15553void intel_modeset_init_hw(struct drm_device *dev)
15554{
1a617b77
ML
15555 struct drm_i915_private *dev_priv = dev->dev_private;
15556
b6283055 15557 intel_update_cdclk(dev);
1a617b77
ML
15558
15559 dev_priv->atomic_cdclk_freq = dev_priv->cdclk_freq;
15560
f817586c 15561 intel_init_clock_gating(dev);
dc97997a 15562 intel_enable_gt_powersave(dev_priv);
f817586c
DV
15563}
15564
d93c0372
MR
15565/*
15566 * Calculate what we think the watermarks should be for the state we've read
15567 * out of the hardware and then immediately program those watermarks so that
15568 * we ensure the hardware settings match our internal state.
15569 *
15570 * We can calculate what we think WM's should be by creating a duplicate of the
15571 * current state (which was constructed during hardware readout) and running it
15572 * through the atomic check code to calculate new watermark values in the
15573 * state object.
15574 */
15575static void sanitize_watermarks(struct drm_device *dev)
15576{
15577 struct drm_i915_private *dev_priv = to_i915(dev);
15578 struct drm_atomic_state *state;
15579 struct drm_crtc *crtc;
15580 struct drm_crtc_state *cstate;
15581 struct drm_modeset_acquire_ctx ctx;
15582 int ret;
15583 int i;
15584
15585 /* Only supported on platforms that use atomic watermark design */
ed4a6a7c 15586 if (!dev_priv->display.optimize_watermarks)
d93c0372
MR
15587 return;
15588
15589 /*
15590 * We need to hold connection_mutex before calling duplicate_state so
15591 * that the connector loop is protected.
15592 */
15593 drm_modeset_acquire_init(&ctx, 0);
15594retry:
0cd1262d 15595 ret = drm_modeset_lock_all_ctx(dev, &ctx);
d93c0372
MR
15596 if (ret == -EDEADLK) {
15597 drm_modeset_backoff(&ctx);
15598 goto retry;
15599 } else if (WARN_ON(ret)) {
0cd1262d 15600 goto fail;
d93c0372
MR
15601 }
15602
15603 state = drm_atomic_helper_duplicate_state(dev, &ctx);
15604 if (WARN_ON(IS_ERR(state)))
0cd1262d 15605 goto fail;
d93c0372 15606
ed4a6a7c
MR
15607 /*
15608 * Hardware readout is the only time we don't want to calculate
15609 * intermediate watermarks (since we don't trust the current
15610 * watermarks).
15611 */
15612 to_intel_atomic_state(state)->skip_intermediate_wm = true;
15613
d93c0372
MR
15614 ret = intel_atomic_check(dev, state);
15615 if (ret) {
15616 /*
15617 * If we fail here, it means that the hardware appears to be
15618 * programmed in a way that shouldn't be possible, given our
15619 * understanding of watermark requirements. This might mean a
15620 * mistake in the hardware readout code or a mistake in the
15621 * watermark calculations for a given platform. Raise a WARN
15622 * so that this is noticeable.
15623 *
15624 * If this actually happens, we'll have to just leave the
15625 * BIOS-programmed watermarks untouched and hope for the best.
15626 */
15627 WARN(true, "Could not determine valid watermarks for inherited state\n");
0cd1262d 15628 goto fail;
d93c0372
MR
15629 }
15630
15631 /* Write calculated watermark values back */
d93c0372
MR
15632 for_each_crtc_in_state(state, crtc, cstate, i) {
15633 struct intel_crtc_state *cs = to_intel_crtc_state(cstate);
15634
ed4a6a7c
MR
15635 cs->wm.need_postvbl_update = true;
15636 dev_priv->display.optimize_watermarks(cs);
d93c0372
MR
15637 }
15638
15639 drm_atomic_state_free(state);
0cd1262d 15640fail:
d93c0372
MR
15641 drm_modeset_drop_locks(&ctx);
15642 drm_modeset_acquire_fini(&ctx);
15643}
15644
79e53945
JB
15645void intel_modeset_init(struct drm_device *dev)
15646{
72e96d64
JL
15647 struct drm_i915_private *dev_priv = to_i915(dev);
15648 struct i915_ggtt *ggtt = &dev_priv->ggtt;
1fe47785 15649 int sprite, ret;
8cc87b75 15650 enum pipe pipe;
46f297fb 15651 struct intel_crtc *crtc;
79e53945
JB
15652
15653 drm_mode_config_init(dev);
15654
15655 dev->mode_config.min_width = 0;
15656 dev->mode_config.min_height = 0;
15657
019d96cb
DA
15658 dev->mode_config.preferred_depth = 24;
15659 dev->mode_config.prefer_shadow = 1;
15660
25bab385
TU
15661 dev->mode_config.allow_fb_modifiers = true;
15662
e6ecefaa 15663 dev->mode_config.funcs = &intel_mode_funcs;
79e53945 15664
b690e96c
JB
15665 intel_init_quirks(dev);
15666
1fa61106
ED
15667 intel_init_pm(dev);
15668
e3c74757
BW
15669 if (INTEL_INFO(dev)->num_pipes == 0)
15670 return;
15671
69f92f67
LW
15672 /*
15673 * There may be no VBT; and if the BIOS enabled SSC we can
15674 * just keep using it to avoid unnecessary flicker. Whereas if the
15675 * BIOS isn't using it, don't assume it will work even if the VBT
15676 * indicates as much.
15677 */
15678 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)) {
15679 bool bios_lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
15680 DREF_SSC1_ENABLE);
15681
15682 if (dev_priv->vbt.lvds_use_ssc != bios_lvds_use_ssc) {
15683 DRM_DEBUG_KMS("SSC %sabled by BIOS, overriding VBT which says %sabled\n",
15684 bios_lvds_use_ssc ? "en" : "dis",
15685 dev_priv->vbt.lvds_use_ssc ? "en" : "dis");
15686 dev_priv->vbt.lvds_use_ssc = bios_lvds_use_ssc;
15687 }
15688 }
15689
a6c45cf0
CW
15690 if (IS_GEN2(dev)) {
15691 dev->mode_config.max_width = 2048;
15692 dev->mode_config.max_height = 2048;
15693 } else if (IS_GEN3(dev)) {
5e4d6fa7
KP
15694 dev->mode_config.max_width = 4096;
15695 dev->mode_config.max_height = 4096;
79e53945 15696 } else {
a6c45cf0
CW
15697 dev->mode_config.max_width = 8192;
15698 dev->mode_config.max_height = 8192;
79e53945 15699 }
068be561 15700
dc41c154
VS
15701 if (IS_845G(dev) || IS_I865G(dev)) {
15702 dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
15703 dev->mode_config.cursor_height = 1023;
15704 } else if (IS_GEN2(dev)) {
068be561
DL
15705 dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
15706 dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
15707 } else {
15708 dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
15709 dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
15710 }
15711
72e96d64 15712 dev->mode_config.fb_base = ggtt->mappable_base;
79e53945 15713
28c97730 15714 DRM_DEBUG_KMS("%d display pipe%s available.\n",
7eb552ae
BW
15715 INTEL_INFO(dev)->num_pipes,
15716 INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
79e53945 15717
055e393f 15718 for_each_pipe(dev_priv, pipe) {
8cc87b75 15719 intel_crtc_init(dev, pipe);
3bdcfc0c 15720 for_each_sprite(dev_priv, pipe, sprite) {
1fe47785 15721 ret = intel_plane_init(dev, pipe, sprite);
7f1f3851 15722 if (ret)
06da8da2 15723 DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
1fe47785 15724 pipe_name(pipe), sprite_name(pipe, sprite), ret);
7f1f3851 15725 }
79e53945
JB
15726 }
15727
bfa7df01
VS
15728 intel_update_czclk(dev_priv);
15729 intel_update_cdclk(dev);
15730
e72f9fbf 15731 intel_shared_dpll_init(dev);
ee7b9f93 15732
b2045352
VS
15733 if (dev_priv->max_cdclk_freq == 0)
15734 intel_update_max_cdclk(dev);
15735
9cce37f4
JB
15736 /* Just disable it once at startup */
15737 i915_disable_vga(dev);
79e53945 15738 intel_setup_outputs(dev);
11be49eb 15739
6e9f798d 15740 drm_modeset_lock_all(dev);
043e9bda 15741 intel_modeset_setup_hw_state(dev);
6e9f798d 15742 drm_modeset_unlock_all(dev);
46f297fb 15743
d3fcc808 15744 for_each_intel_crtc(dev, crtc) {
eeebeac5
ML
15745 struct intel_initial_plane_config plane_config = {};
15746
46f297fb
JB
15747 if (!crtc->active)
15748 continue;
15749
46f297fb 15750 /*
46f297fb
JB
15751 * Note that reserving the BIOS fb up front prevents us
15752 * from stuffing other stolen allocations like the ring
15753 * on top. This prevents some ugliness at boot time, and
15754 * can even allow for smooth boot transitions if the BIOS
15755 * fb is large enough for the active pipe configuration.
15756 */
eeebeac5
ML
15757 dev_priv->display.get_initial_plane_config(crtc,
15758 &plane_config);
15759
15760 /*
15761 * If the fb is shared between multiple heads, we'll
15762 * just get the first one.
15763 */
15764 intel_find_initial_plane_obj(crtc, &plane_config);
46f297fb 15765 }
d93c0372
MR
15766
15767 /*
15768 * Make sure hardware watermarks really match the state we read out.
15769 * Note that we need to do this after reconstructing the BIOS fb's
15770 * since the watermark calculation done here will use pstate->fb.
15771 */
15772 sanitize_watermarks(dev);
2c7111db
CW
15773}
15774
7fad798e
DV
15775static void intel_enable_pipe_a(struct drm_device *dev)
15776{
15777 struct intel_connector *connector;
15778 struct drm_connector *crt = NULL;
15779 struct intel_load_detect_pipe load_detect_temp;
208bf9fd 15780 struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
7fad798e
DV
15781
15782 /* We can't just switch on the pipe A, we need to set things up with a
15783 * proper mode and output configuration. As a gross hack, enable pipe A
15784 * by enabling the load detect pipe once. */
3a3371ff 15785 for_each_intel_connector(dev, connector) {
7fad798e
DV
15786 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
15787 crt = &connector->base;
15788 break;
15789 }
15790 }
15791
15792 if (!crt)
15793 return;
15794
208bf9fd 15795 if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
49172fee 15796 intel_release_load_detect_pipe(crt, &load_detect_temp, ctx);
7fad798e
DV
15797}
15798
fa555837
DV
15799static bool
15800intel_check_plane_mapping(struct intel_crtc *crtc)
15801{
7eb552ae
BW
15802 struct drm_device *dev = crtc->base.dev;
15803 struct drm_i915_private *dev_priv = dev->dev_private;
649636ef 15804 u32 val;
fa555837 15805
7eb552ae 15806 if (INTEL_INFO(dev)->num_pipes == 1)
fa555837
DV
15807 return true;
15808
649636ef 15809 val = I915_READ(DSPCNTR(!crtc->plane));
fa555837
DV
15810
15811 if ((val & DISPLAY_PLANE_ENABLE) &&
15812 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
15813 return false;
15814
15815 return true;
15816}
15817
02e93c35
VS
15818static bool intel_crtc_has_encoders(struct intel_crtc *crtc)
15819{
15820 struct drm_device *dev = crtc->base.dev;
15821 struct intel_encoder *encoder;
15822
15823 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
15824 return true;
15825
15826 return false;
15827}
15828
dd756198
VS
15829static bool intel_encoder_has_connectors(struct intel_encoder *encoder)
15830{
15831 struct drm_device *dev = encoder->base.dev;
15832 struct intel_connector *connector;
15833
15834 for_each_connector_on_encoder(dev, &encoder->base, connector)
15835 return true;
15836
15837 return false;
15838}
15839
24929352
DV
15840static void intel_sanitize_crtc(struct intel_crtc *crtc)
15841{
15842 struct drm_device *dev = crtc->base.dev;
15843 struct drm_i915_private *dev_priv = dev->dev_private;
4d1de975 15844 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
24929352 15845
24929352 15846 /* Clear any frame start delays used for debugging left by the BIOS */
4d1de975
JN
15847 if (!transcoder_is_dsi(cpu_transcoder)) {
15848 i915_reg_t reg = PIPECONF(cpu_transcoder);
15849
15850 I915_WRITE(reg,
15851 I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
15852 }
24929352 15853
d3eaf884 15854 /* restore vblank interrupts to correct state */
9625604c 15855 drm_crtc_vblank_reset(&crtc->base);
d297e103 15856 if (crtc->active) {
f9cd7b88
VS
15857 struct intel_plane *plane;
15858
9625604c 15859 drm_crtc_vblank_on(&crtc->base);
f9cd7b88
VS
15860
15861 /* Disable everything but the primary plane */
15862 for_each_intel_plane_on_crtc(dev, crtc, plane) {
15863 if (plane->base.type == DRM_PLANE_TYPE_PRIMARY)
15864 continue;
15865
15866 plane->disable_plane(&plane->base, &crtc->base);
15867 }
9625604c 15868 }
d3eaf884 15869
24929352 15870 /* We need to sanitize the plane -> pipe mapping first because this will
fa555837
DV
15871 * disable the crtc (and hence change the state) if it is wrong. Note
15872 * that gen4+ has a fixed plane -> pipe mapping. */
15873 if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
24929352
DV
15874 bool plane;
15875
78108b7c
VS
15876 DRM_DEBUG_KMS("[CRTC:%d:%s] wrong plane connection detected!\n",
15877 crtc->base.base.id, crtc->base.name);
24929352
DV
15878
15879 /* Pipe has the wrong plane attached and the plane is active.
15880 * Temporarily change the plane mapping and disable everything
15881 * ... */
15882 plane = crtc->plane;
b70709a6 15883 to_intel_plane_state(crtc->base.primary->state)->visible = true;
24929352 15884 crtc->plane = !plane;
b17d48e2 15885 intel_crtc_disable_noatomic(&crtc->base);
24929352 15886 crtc->plane = plane;
24929352 15887 }
24929352 15888
7fad798e
DV
15889 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
15890 crtc->pipe == PIPE_A && !crtc->active) {
15891 /* BIOS forgot to enable pipe A, this mostly happens after
15892 * resume. Force-enable the pipe to fix this, the update_dpms
15893 * call below we restore the pipe to the right state, but leave
15894 * the required bits on. */
15895 intel_enable_pipe_a(dev);
15896 }
15897
24929352
DV
15898 /* Adjust the state of the output pipe according to whether we
15899 * have active connectors/encoders. */
842e0307 15900 if (crtc->active && !intel_crtc_has_encoders(crtc))
b17d48e2 15901 intel_crtc_disable_noatomic(&crtc->base);
24929352 15902
a3ed6aad 15903 if (crtc->active || HAS_GMCH_DISPLAY(dev)) {
4cc31489
DV
15904 /*
15905 * We start out with underrun reporting disabled to avoid races.
15906 * For correct bookkeeping mark this on active crtcs.
15907 *
c5ab3bc0
DV
15908 * Also on gmch platforms we dont have any hardware bits to
15909 * disable the underrun reporting. Which means we need to start
15910 * out with underrun reporting disabled also on inactive pipes,
15911 * since otherwise we'll complain about the garbage we read when
15912 * e.g. coming up after runtime pm.
15913 *
4cc31489
DV
15914 * No protection against concurrent access is required - at
15915 * worst a fifo underrun happens which also sets this to false.
15916 */
15917 crtc->cpu_fifo_underrun_disabled = true;
15918 crtc->pch_fifo_underrun_disabled = true;
15919 }
24929352
DV
15920}
15921
15922static void intel_sanitize_encoder(struct intel_encoder *encoder)
15923{
15924 struct intel_connector *connector;
15925 struct drm_device *dev = encoder->base.dev;
15926
15927 /* We need to check both for a crtc link (meaning that the
15928 * encoder is active and trying to read from a pipe) and the
15929 * pipe itself being active. */
15930 bool has_active_crtc = encoder->base.crtc &&
15931 to_intel_crtc(encoder->base.crtc)->active;
15932
dd756198 15933 if (intel_encoder_has_connectors(encoder) && !has_active_crtc) {
24929352
DV
15934 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
15935 encoder->base.base.id,
8e329a03 15936 encoder->base.name);
24929352
DV
15937
15938 /* Connector is active, but has no active pipe. This is
15939 * fallout from our resume register restoring. Disable
15940 * the encoder manually again. */
15941 if (encoder->base.crtc) {
15942 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
15943 encoder->base.base.id,
8e329a03 15944 encoder->base.name);
24929352 15945 encoder->disable(encoder);
a62d1497
VS
15946 if (encoder->post_disable)
15947 encoder->post_disable(encoder);
24929352 15948 }
7f1950fb 15949 encoder->base.crtc = NULL;
24929352
DV
15950
15951 /* Inconsistent output/port/pipe state happens presumably due to
15952 * a bug in one of the get_hw_state functions. Or someplace else
15953 * in our code, like the register restore mess on resume. Clamp
15954 * things to off as a safer default. */
3a3371ff 15955 for_each_intel_connector(dev, connector) {
24929352
DV
15956 if (connector->encoder != encoder)
15957 continue;
7f1950fb
EE
15958 connector->base.dpms = DRM_MODE_DPMS_OFF;
15959 connector->base.encoder = NULL;
24929352
DV
15960 }
15961 }
15962 /* Enabled encoders without active connectors will be fixed in
15963 * the crtc fixup. */
15964}
15965
04098753 15966void i915_redisable_vga_power_on(struct drm_device *dev)
0fde901f
KM
15967{
15968 struct drm_i915_private *dev_priv = dev->dev_private;
f0f59a00 15969 i915_reg_t vga_reg = i915_vgacntrl_reg(dev);
0fde901f 15970
04098753
ID
15971 if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
15972 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
15973 i915_disable_vga(dev);
15974 }
15975}
15976
15977void i915_redisable_vga(struct drm_device *dev)
15978{
15979 struct drm_i915_private *dev_priv = dev->dev_private;
15980
8dc8a27c
PZ
15981 /* This function can be called both from intel_modeset_setup_hw_state or
15982 * at a very early point in our resume sequence, where the power well
15983 * structures are not yet restored. Since this function is at a very
15984 * paranoid "someone might have enabled VGA while we were not looking"
15985 * level, just check if the power well is enabled instead of trying to
15986 * follow the "don't touch the power well if we don't need it" policy
15987 * the rest of the driver uses. */
6392f847 15988 if (!intel_display_power_get_if_enabled(dev_priv, POWER_DOMAIN_VGA))
8dc8a27c
PZ
15989 return;
15990
04098753 15991 i915_redisable_vga_power_on(dev);
6392f847
ID
15992
15993 intel_display_power_put(dev_priv, POWER_DOMAIN_VGA);
0fde901f
KM
15994}
15995
f9cd7b88 15996static bool primary_get_hw_state(struct intel_plane *plane)
98ec7739 15997{
f9cd7b88 15998 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
98ec7739 15999
f9cd7b88 16000 return I915_READ(DSPCNTR(plane->plane)) & DISPLAY_PLANE_ENABLE;
d032ffa0
ML
16001}
16002
f9cd7b88
VS
16003/* FIXME read out full plane state for all planes */
16004static void readout_plane_state(struct intel_crtc *crtc)
d032ffa0 16005{
b26d3ea3 16006 struct drm_plane *primary = crtc->base.primary;
f9cd7b88 16007 struct intel_plane_state *plane_state =
b26d3ea3 16008 to_intel_plane_state(primary->state);
d032ffa0 16009
19b8d387 16010 plane_state->visible = crtc->active &&
b26d3ea3
ML
16011 primary_get_hw_state(to_intel_plane(primary));
16012
16013 if (plane_state->visible)
16014 crtc->base.state->plane_mask |= 1 << drm_plane_index(primary);
98ec7739
VS
16015}
16016
30e984df 16017static void intel_modeset_readout_hw_state(struct drm_device *dev)
24929352
DV
16018{
16019 struct drm_i915_private *dev_priv = dev->dev_private;
16020 enum pipe pipe;
24929352
DV
16021 struct intel_crtc *crtc;
16022 struct intel_encoder *encoder;
16023 struct intel_connector *connector;
5358901f 16024 int i;
24929352 16025
565602d7
ML
16026 dev_priv->active_crtcs = 0;
16027
d3fcc808 16028 for_each_intel_crtc(dev, crtc) {
565602d7
ML
16029 struct intel_crtc_state *crtc_state = crtc->config;
16030 int pixclk = 0;
3b117c8f 16031
ec2dc6a0 16032 __drm_atomic_helper_crtc_destroy_state(&crtc_state->base);
565602d7
ML
16033 memset(crtc_state, 0, sizeof(*crtc_state));
16034 crtc_state->base.crtc = &crtc->base;
24929352 16035
565602d7
ML
16036 crtc_state->base.active = crtc_state->base.enable =
16037 dev_priv->display.get_pipe_config(crtc, crtc_state);
16038
16039 crtc->base.enabled = crtc_state->base.enable;
16040 crtc->active = crtc_state->base.active;
16041
16042 if (crtc_state->base.active) {
16043 dev_priv->active_crtcs |= 1 << crtc->pipe;
16044
c89e39f3 16045 if (INTEL_GEN(dev_priv) >= 9 || IS_BROADWELL(dev_priv))
565602d7 16046 pixclk = ilk_pipe_pixel_rate(crtc_state);
9558d15d 16047 else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
565602d7
ML
16048 pixclk = crtc_state->base.adjusted_mode.crtc_clock;
16049 else
16050 WARN_ON(dev_priv->display.modeset_calc_cdclk);
9558d15d
VS
16051
16052 /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
16053 if (IS_BROADWELL(dev_priv) && crtc_state->ips_enabled)
16054 pixclk = DIV_ROUND_UP(pixclk * 100, 95);
565602d7
ML
16055 }
16056
16057 dev_priv->min_pixclk[crtc->pipe] = pixclk;
b70709a6 16058
f9cd7b88 16059 readout_plane_state(crtc);
24929352 16060
78108b7c
VS
16061 DRM_DEBUG_KMS("[CRTC:%d:%s] hw state readout: %s\n",
16062 crtc->base.base.id, crtc->base.name,
24929352
DV
16063 crtc->active ? "enabled" : "disabled");
16064 }
16065
5358901f
DV
16066 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
16067 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
16068
2edd6443
ACO
16069 pll->on = pll->funcs.get_hw_state(dev_priv, pll,
16070 &pll->config.hw_state);
3e369b76 16071 pll->config.crtc_mask = 0;
d3fcc808 16072 for_each_intel_crtc(dev, crtc) {
2dd66ebd 16073 if (crtc->active && crtc->config->shared_dpll == pll)
3e369b76 16074 pll->config.crtc_mask |= 1 << crtc->pipe;
5358901f 16075 }
2dd66ebd 16076 pll->active_mask = pll->config.crtc_mask;
5358901f 16077
1e6f2ddc 16078 DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
3e369b76 16079 pll->name, pll->config.crtc_mask, pll->on);
5358901f
DV
16080 }
16081
b2784e15 16082 for_each_intel_encoder(dev, encoder) {
24929352
DV
16083 pipe = 0;
16084
16085 if (encoder->get_hw_state(encoder, &pipe)) {
045ac3b5
JB
16086 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
16087 encoder->base.crtc = &crtc->base;
6e3c9717 16088 encoder->get_config(encoder, crtc->config);
24929352
DV
16089 } else {
16090 encoder->base.crtc = NULL;
16091 }
16092
6f2bcceb 16093 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
24929352 16094 encoder->base.base.id,
8e329a03 16095 encoder->base.name,
24929352 16096 encoder->base.crtc ? "enabled" : "disabled",
6f2bcceb 16097 pipe_name(pipe));
24929352
DV
16098 }
16099
3a3371ff 16100 for_each_intel_connector(dev, connector) {
24929352
DV
16101 if (connector->get_hw_state(connector)) {
16102 connector->base.dpms = DRM_MODE_DPMS_ON;
2aa974c9
ML
16103
16104 encoder = connector->encoder;
16105 connector->base.encoder = &encoder->base;
16106
16107 if (encoder->base.crtc &&
16108 encoder->base.crtc->state->active) {
16109 /*
16110 * This has to be done during hardware readout
16111 * because anything calling .crtc_disable may
16112 * rely on the connector_mask being accurate.
16113 */
16114 encoder->base.crtc->state->connector_mask |=
16115 1 << drm_connector_index(&connector->base);
e87a52b3
ML
16116 encoder->base.crtc->state->encoder_mask |=
16117 1 << drm_encoder_index(&encoder->base);
2aa974c9
ML
16118 }
16119
24929352
DV
16120 } else {
16121 connector->base.dpms = DRM_MODE_DPMS_OFF;
16122 connector->base.encoder = NULL;
16123 }
16124 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
16125 connector->base.base.id,
c23cc417 16126 connector->base.name,
24929352
DV
16127 connector->base.encoder ? "enabled" : "disabled");
16128 }
7f4c6284
VS
16129
16130 for_each_intel_crtc(dev, crtc) {
16131 crtc->base.hwmode = crtc->config->base.adjusted_mode;
16132
16133 memset(&crtc->base.mode, 0, sizeof(crtc->base.mode));
16134 if (crtc->base.state->active) {
16135 intel_mode_from_pipe_config(&crtc->base.mode, crtc->config);
16136 intel_mode_from_pipe_config(&crtc->base.state->adjusted_mode, crtc->config);
16137 WARN_ON(drm_atomic_set_mode_for_crtc(crtc->base.state, &crtc->base.mode));
16138
16139 /*
16140 * The initial mode needs to be set in order to keep
16141 * the atomic core happy. It wants a valid mode if the
16142 * crtc's enabled, so we do the above call.
16143 *
16144 * At this point some state updated by the connectors
16145 * in their ->detect() callback has not run yet, so
16146 * no recalculation can be done yet.
16147 *
16148 * Even if we could do a recalculation and modeset
16149 * right now it would cause a double modeset if
16150 * fbdev or userspace chooses a different initial mode.
16151 *
16152 * If that happens, someone indicated they wanted a
16153 * mode change, which means it's safe to do a full
16154 * recalculation.
16155 */
16156 crtc->base.state->mode.private_flags = I915_MODE_FLAG_INHERITED;
9eca6832
VS
16157
16158 drm_calc_timestamping_constants(&crtc->base, &crtc->base.hwmode);
16159 update_scanline_offset(crtc);
7f4c6284 16160 }
e3b247da
VS
16161
16162 intel_pipe_config_sanity_check(dev_priv, crtc->config);
7f4c6284 16163 }
30e984df
DV
16164}
16165
043e9bda
ML
16166/* Scan out the current hw modeset state,
16167 * and sanitizes it to the current state
16168 */
16169static void
16170intel_modeset_setup_hw_state(struct drm_device *dev)
30e984df
DV
16171{
16172 struct drm_i915_private *dev_priv = dev->dev_private;
16173 enum pipe pipe;
30e984df
DV
16174 struct intel_crtc *crtc;
16175 struct intel_encoder *encoder;
35c95375 16176 int i;
30e984df
DV
16177
16178 intel_modeset_readout_hw_state(dev);
24929352
DV
16179
16180 /* HW state is read out, now we need to sanitize this mess. */
b2784e15 16181 for_each_intel_encoder(dev, encoder) {
24929352
DV
16182 intel_sanitize_encoder(encoder);
16183 }
16184
055e393f 16185 for_each_pipe(dev_priv, pipe) {
24929352
DV
16186 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
16187 intel_sanitize_crtc(crtc);
6e3c9717
ACO
16188 intel_dump_pipe_config(crtc, crtc->config,
16189 "[setup_hw_state]");
24929352 16190 }
9a935856 16191
d29b2f9d
ACO
16192 intel_modeset_update_connector_atomic_state(dev);
16193
35c95375
DV
16194 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
16195 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
16196
2dd66ebd 16197 if (!pll->on || pll->active_mask)
35c95375
DV
16198 continue;
16199
16200 DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
16201
2edd6443 16202 pll->funcs.disable(dev_priv, pll);
35c95375
DV
16203 pll->on = false;
16204 }
16205
666a4537 16206 if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev))
6eb1a681
VS
16207 vlv_wm_get_hw_state(dev);
16208 else if (IS_GEN9(dev))
3078999f
PB
16209 skl_wm_get_hw_state(dev);
16210 else if (HAS_PCH_SPLIT(dev))
243e6a44 16211 ilk_wm_get_hw_state(dev);
292b990e
ML
16212
16213 for_each_intel_crtc(dev, crtc) {
16214 unsigned long put_domains;
16215
74bff5f9 16216 put_domains = modeset_get_crtc_power_domains(&crtc->base, crtc->config);
292b990e
ML
16217 if (WARN_ON(put_domains))
16218 modeset_put_power_domains(dev_priv, put_domains);
16219 }
16220 intel_display_set_init_power(dev_priv, false);
010cf73d
PZ
16221
16222 intel_fbc_init_pipe_state(dev_priv);
043e9bda 16223}
7d0bc1ea 16224
043e9bda
ML
16225void intel_display_resume(struct drm_device *dev)
16226{
e2c8b870
ML
16227 struct drm_i915_private *dev_priv = to_i915(dev);
16228 struct drm_atomic_state *state = dev_priv->modeset_restore_state;
16229 struct drm_modeset_acquire_ctx ctx;
043e9bda 16230 int ret;
e2c8b870 16231 bool setup = false;
f30da187 16232
e2c8b870 16233 dev_priv->modeset_restore_state = NULL;
043e9bda 16234
ea49c9ac
ML
16235 /*
16236 * This is a cludge because with real atomic modeset mode_config.mutex
16237 * won't be taken. Unfortunately some probed state like
16238 * audio_codec_enable is still protected by mode_config.mutex, so lock
16239 * it here for now.
16240 */
16241 mutex_lock(&dev->mode_config.mutex);
e2c8b870 16242 drm_modeset_acquire_init(&ctx, 0);
043e9bda 16243
e2c8b870
ML
16244retry:
16245 ret = drm_modeset_lock_all_ctx(dev, &ctx);
043e9bda 16246
e2c8b870
ML
16247 if (ret == 0 && !setup) {
16248 setup = true;
043e9bda 16249
e2c8b870
ML
16250 intel_modeset_setup_hw_state(dev);
16251 i915_redisable_vga(dev);
45e2b5f6 16252 }
8af6cf88 16253
e2c8b870
ML
16254 if (ret == 0 && state) {
16255 struct drm_crtc_state *crtc_state;
16256 struct drm_crtc *crtc;
16257 int i;
043e9bda 16258
e2c8b870
ML
16259 state->acquire_ctx = &ctx;
16260
e3d5457c
VS
16261 /* ignore any reset values/BIOS leftovers in the WM registers */
16262 to_intel_atomic_state(state)->skip_intermediate_wm = true;
16263
e2c8b870
ML
16264 for_each_crtc_in_state(state, crtc, crtc_state, i) {
16265 /*
16266 * Force recalculation even if we restore
16267 * current state. With fast modeset this may not result
16268 * in a modeset when the state is compatible.
16269 */
16270 crtc_state->mode_changed = true;
16271 }
16272
16273 ret = drm_atomic_commit(state);
043e9bda
ML
16274 }
16275
e2c8b870
ML
16276 if (ret == -EDEADLK) {
16277 drm_modeset_backoff(&ctx);
16278 goto retry;
16279 }
043e9bda 16280
e2c8b870
ML
16281 drm_modeset_drop_locks(&ctx);
16282 drm_modeset_acquire_fini(&ctx);
ea49c9ac 16283 mutex_unlock(&dev->mode_config.mutex);
043e9bda 16284
e2c8b870
ML
16285 if (ret) {
16286 DRM_ERROR("Restoring old state failed with %i\n", ret);
16287 drm_atomic_state_free(state);
16288 }
2c7111db
CW
16289}
16290
16291void intel_modeset_gem_init(struct drm_device *dev)
16292{
dc97997a 16293 struct drm_i915_private *dev_priv = to_i915(dev);
484b41dd 16294 struct drm_crtc *c;
2ff8fde1 16295 struct drm_i915_gem_object *obj;
e0d6149b 16296 int ret;
484b41dd 16297
dc97997a 16298 intel_init_gt_powersave(dev_priv);
ae48434c 16299
1833b134 16300 intel_modeset_init_hw(dev);
02e792fb 16301
1ee8da6d 16302 intel_setup_overlay(dev_priv);
484b41dd
JB
16303
16304 /*
16305 * Make sure any fbs we allocated at startup are properly
16306 * pinned & fenced. When we do the allocation it's too early
16307 * for this.
16308 */
70e1e0ec 16309 for_each_crtc(dev, c) {
2ff8fde1
MR
16310 obj = intel_fb_obj(c->primary->fb);
16311 if (obj == NULL)
484b41dd
JB
16312 continue;
16313
e0d6149b 16314 mutex_lock(&dev->struct_mutex);
3465c580
VS
16315 ret = intel_pin_and_fence_fb_obj(c->primary->fb,
16316 c->primary->state->rotation);
e0d6149b
TU
16317 mutex_unlock(&dev->struct_mutex);
16318 if (ret) {
484b41dd
JB
16319 DRM_ERROR("failed to pin boot fb on pipe %d\n",
16320 to_intel_crtc(c)->pipe);
66e514c1 16321 drm_framebuffer_unreference(c->primary->fb);
5a21b665 16322 c->primary->fb = NULL;
36750f28 16323 c->primary->crtc = c->primary->state->crtc = NULL;
5a21b665 16324 update_state_fb(c->primary);
36750f28 16325 c->state->plane_mask &= ~(1 << drm_plane_index(c->primary));
484b41dd
JB
16326 }
16327 }
1ebaa0b9
CW
16328}
16329
16330int intel_connector_register(struct drm_connector *connector)
16331{
16332 struct intel_connector *intel_connector = to_intel_connector(connector);
16333 int ret;
16334
16335 ret = intel_backlight_device_register(intel_connector);
16336 if (ret)
16337 goto err;
16338
16339 return 0;
0962c3c9 16340
1ebaa0b9
CW
16341err:
16342 return ret;
79e53945
JB
16343}
16344
c191eca1 16345void intel_connector_unregister(struct drm_connector *connector)
4932e2c3 16346{
e63d87c0 16347 struct intel_connector *intel_connector = to_intel_connector(connector);
4932e2c3 16348
e63d87c0 16349 intel_backlight_device_unregister(intel_connector);
4932e2c3 16350 intel_panel_destroy_backlight(connector);
4932e2c3
ID
16351}
16352
79e53945
JB
16353void intel_modeset_cleanup(struct drm_device *dev)
16354{
652c393a 16355 struct drm_i915_private *dev_priv = dev->dev_private;
652c393a 16356
dc97997a 16357 intel_disable_gt_powersave(dev_priv);
2eb5252e 16358
fd0c0642
DV
16359 /*
16360 * Interrupts and polling as the first thing to avoid creating havoc.
2eb5252e 16361 * Too much stuff here (turning of connectors, ...) would
fd0c0642
DV
16362 * experience fancy races otherwise.
16363 */
2aeb7d3a 16364 intel_irq_uninstall(dev_priv);
eb21b92b 16365
fd0c0642
DV
16366 /*
16367 * Due to the hpd irq storm handling the hotplug work can re-arm the
16368 * poll handlers. Hence disable polling after hpd handling is shut down.
16369 */
f87ea761 16370 drm_kms_helper_poll_fini(dev);
fd0c0642 16371
723bfd70
JB
16372 intel_unregister_dsm_handler();
16373
c937ab3e 16374 intel_fbc_global_disable(dev_priv);
69341a5e 16375
1630fe75
CW
16376 /* flush any delayed tasks or pending work */
16377 flush_scheduled_work();
16378
79e53945 16379 drm_mode_config_cleanup(dev);
4d7bb011 16380
1ee8da6d 16381 intel_cleanup_overlay(dev_priv);
ae48434c 16382
dc97997a 16383 intel_cleanup_gt_powersave(dev_priv);
f5949141
DV
16384
16385 intel_teardown_gmbus(dev);
79e53945
JB
16386}
16387
df0e9248
CW
16388void intel_connector_attach_encoder(struct intel_connector *connector,
16389 struct intel_encoder *encoder)
16390{
16391 connector->encoder = encoder;
16392 drm_mode_connector_attach_encoder(&connector->base,
16393 &encoder->base);
79e53945 16394}
28d52043
DA
16395
16396/*
16397 * set vga decode state - true == enable VGA decode
16398 */
16399int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
16400{
16401 struct drm_i915_private *dev_priv = dev->dev_private;
a885b3cc 16402 unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
28d52043
DA
16403 u16 gmch_ctrl;
16404
75fa041d
CW
16405 if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
16406 DRM_ERROR("failed to read control word\n");
16407 return -EIO;
16408 }
16409
c0cc8a55
CW
16410 if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
16411 return 0;
16412
28d52043
DA
16413 if (state)
16414 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
16415 else
16416 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
75fa041d
CW
16417
16418 if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
16419 DRM_ERROR("failed to write control word\n");
16420 return -EIO;
16421 }
16422
28d52043
DA
16423 return 0;
16424}
c4a1d9e4 16425
c4a1d9e4 16426struct intel_display_error_state {
ff57f1b0
PZ
16427
16428 u32 power_well_driver;
16429
63b66e5b
CW
16430 int num_transcoders;
16431
c4a1d9e4
CW
16432 struct intel_cursor_error_state {
16433 u32 control;
16434 u32 position;
16435 u32 base;
16436 u32 size;
52331309 16437 } cursor[I915_MAX_PIPES];
c4a1d9e4
CW
16438
16439 struct intel_pipe_error_state {
ddf9c536 16440 bool power_domain_on;
c4a1d9e4 16441 u32 source;
f301b1e1 16442 u32 stat;
52331309 16443 } pipe[I915_MAX_PIPES];
c4a1d9e4
CW
16444
16445 struct intel_plane_error_state {
16446 u32 control;
16447 u32 stride;
16448 u32 size;
16449 u32 pos;
16450 u32 addr;
16451 u32 surface;
16452 u32 tile_offset;
52331309 16453 } plane[I915_MAX_PIPES];
63b66e5b
CW
16454
16455 struct intel_transcoder_error_state {
ddf9c536 16456 bool power_domain_on;
63b66e5b
CW
16457 enum transcoder cpu_transcoder;
16458
16459 u32 conf;
16460
16461 u32 htotal;
16462 u32 hblank;
16463 u32 hsync;
16464 u32 vtotal;
16465 u32 vblank;
16466 u32 vsync;
16467 } transcoder[4];
c4a1d9e4
CW
16468};
16469
16470struct intel_display_error_state *
c033666a 16471intel_display_capture_error_state(struct drm_i915_private *dev_priv)
c4a1d9e4 16472{
c4a1d9e4 16473 struct intel_display_error_state *error;
63b66e5b
CW
16474 int transcoders[] = {
16475 TRANSCODER_A,
16476 TRANSCODER_B,
16477 TRANSCODER_C,
16478 TRANSCODER_EDP,
16479 };
c4a1d9e4
CW
16480 int i;
16481
c033666a 16482 if (INTEL_INFO(dev_priv)->num_pipes == 0)
63b66e5b
CW
16483 return NULL;
16484
9d1cb914 16485 error = kzalloc(sizeof(*error), GFP_ATOMIC);
c4a1d9e4
CW
16486 if (error == NULL)
16487 return NULL;
16488
c033666a 16489 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
ff57f1b0
PZ
16490 error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
16491
055e393f 16492 for_each_pipe(dev_priv, i) {
ddf9c536 16493 error->pipe[i].power_domain_on =
f458ebbc
DV
16494 __intel_display_power_is_enabled(dev_priv,
16495 POWER_DOMAIN_PIPE(i));
ddf9c536 16496 if (!error->pipe[i].power_domain_on)
9d1cb914
PZ
16497 continue;
16498
5efb3e28
VS
16499 error->cursor[i].control = I915_READ(CURCNTR(i));
16500 error->cursor[i].position = I915_READ(CURPOS(i));
16501 error->cursor[i].base = I915_READ(CURBASE(i));
c4a1d9e4
CW
16502
16503 error->plane[i].control = I915_READ(DSPCNTR(i));
16504 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
c033666a 16505 if (INTEL_GEN(dev_priv) <= 3) {
51889b35 16506 error->plane[i].size = I915_READ(DSPSIZE(i));
80ca378b
PZ
16507 error->plane[i].pos = I915_READ(DSPPOS(i));
16508 }
c033666a 16509 if (INTEL_GEN(dev_priv) <= 7 && !IS_HASWELL(dev_priv))
ca291363 16510 error->plane[i].addr = I915_READ(DSPADDR(i));
c033666a 16511 if (INTEL_GEN(dev_priv) >= 4) {
c4a1d9e4
CW
16512 error->plane[i].surface = I915_READ(DSPSURF(i));
16513 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
16514 }
16515
c4a1d9e4 16516 error->pipe[i].source = I915_READ(PIPESRC(i));
f301b1e1 16517
c033666a 16518 if (HAS_GMCH_DISPLAY(dev_priv))
f301b1e1 16519 error->pipe[i].stat = I915_READ(PIPESTAT(i));
63b66e5b
CW
16520 }
16521
4d1de975 16522 /* Note: this does not include DSI transcoders. */
c033666a 16523 error->num_transcoders = INTEL_INFO(dev_priv)->num_pipes;
2d1fe073 16524 if (HAS_DDI(dev_priv))
63b66e5b
CW
16525 error->num_transcoders++; /* Account for eDP. */
16526
16527 for (i = 0; i < error->num_transcoders; i++) {
16528 enum transcoder cpu_transcoder = transcoders[i];
16529
ddf9c536 16530 error->transcoder[i].power_domain_on =
f458ebbc 16531 __intel_display_power_is_enabled(dev_priv,
38cc1daf 16532 POWER_DOMAIN_TRANSCODER(cpu_transcoder));
ddf9c536 16533 if (!error->transcoder[i].power_domain_on)
9d1cb914
PZ
16534 continue;
16535
63b66e5b
CW
16536 error->transcoder[i].cpu_transcoder = cpu_transcoder;
16537
16538 error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
16539 error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
16540 error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
16541 error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
16542 error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
16543 error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
16544 error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
c4a1d9e4
CW
16545 }
16546
16547 return error;
16548}
16549
edc3d884
MK
16550#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
16551
c4a1d9e4 16552void
edc3d884 16553intel_display_print_error_state(struct drm_i915_error_state_buf *m,
c4a1d9e4
CW
16554 struct drm_device *dev,
16555 struct intel_display_error_state *error)
16556{
055e393f 16557 struct drm_i915_private *dev_priv = dev->dev_private;
c4a1d9e4
CW
16558 int i;
16559
63b66e5b
CW
16560 if (!error)
16561 return;
16562
edc3d884 16563 err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
190be112 16564 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
edc3d884 16565 err_printf(m, "PWR_WELL_CTL2: %08x\n",
ff57f1b0 16566 error->power_well_driver);
055e393f 16567 for_each_pipe(dev_priv, i) {
edc3d884 16568 err_printf(m, "Pipe [%d]:\n", i);
ddf9c536 16569 err_printf(m, " Power: %s\n",
87ad3212 16570 onoff(error->pipe[i].power_domain_on));
edc3d884 16571 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
f301b1e1 16572 err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
edc3d884
MK
16573
16574 err_printf(m, "Plane [%d]:\n", i);
16575 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
16576 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
80ca378b 16577 if (INTEL_INFO(dev)->gen <= 3) {
edc3d884
MK
16578 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
16579 err_printf(m, " POS: %08x\n", error->plane[i].pos);
80ca378b 16580 }
4b71a570 16581 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
edc3d884 16582 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
c4a1d9e4 16583 if (INTEL_INFO(dev)->gen >= 4) {
edc3d884
MK
16584 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
16585 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
c4a1d9e4
CW
16586 }
16587
edc3d884
MK
16588 err_printf(m, "Cursor [%d]:\n", i);
16589 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
16590 err_printf(m, " POS: %08x\n", error->cursor[i].position);
16591 err_printf(m, " BASE: %08x\n", error->cursor[i].base);
c4a1d9e4 16592 }
63b66e5b
CW
16593
16594 for (i = 0; i < error->num_transcoders; i++) {
da205630 16595 err_printf(m, "CPU transcoder: %s\n",
63b66e5b 16596 transcoder_name(error->transcoder[i].cpu_transcoder));
ddf9c536 16597 err_printf(m, " Power: %s\n",
87ad3212 16598 onoff(error->transcoder[i].power_domain_on));
63b66e5b
CW
16599 err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
16600 err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
16601 err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
16602 err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
16603 err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
16604 err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
16605 err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
16606 }
c4a1d9e4 16607}