]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/gpu/drm/i915/intel_display.c
drm/i915: Use atomics to manipulate obj->frontbuffer_bits
[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"
5d723d7a 37#include "intel_frontbuffer.h"
760285e7 38#include <drm/i915_drm.h>
79e53945 39#include "i915_drv.h"
c37efb99 40#include "i915_gem_dmabuf.h"
db18b6a6 41#include "intel_dsi.h"
e5510fac 42#include "i915_trace.h"
319c1d42 43#include <drm/drm_atomic.h>
c196e1d6 44#include <drm/drm_atomic_helper.h>
760285e7
DH
45#include <drm/drm_dp_helper.h>
46#include <drm/drm_crtc_helper.h>
465c120c
MR
47#include <drm/drm_plane_helper.h>
48#include <drm/drm_rect.h>
c0f372b3 49#include <linux/dma_remapping.h>
fd8e058a 50#include <linux/reservation.h>
79e53945 51
5a21b665
DV
52static bool is_mmio_work(struct intel_flip_work *work)
53{
54 return work->mmio_work.func;
55}
56
465c120c 57/* Primary plane formats for gen <= 3 */
568db4f2 58static const uint32_t i8xx_primary_formats[] = {
67fe7dc5
DL
59 DRM_FORMAT_C8,
60 DRM_FORMAT_RGB565,
465c120c 61 DRM_FORMAT_XRGB1555,
67fe7dc5 62 DRM_FORMAT_XRGB8888,
465c120c
MR
63};
64
65/* Primary plane formats for gen >= 4 */
568db4f2 66static const uint32_t i965_primary_formats[] = {
6c0fd451
DL
67 DRM_FORMAT_C8,
68 DRM_FORMAT_RGB565,
69 DRM_FORMAT_XRGB8888,
70 DRM_FORMAT_XBGR8888,
71 DRM_FORMAT_XRGB2101010,
72 DRM_FORMAT_XBGR2101010,
73};
74
75static const uint32_t skl_primary_formats[] = {
67fe7dc5
DL
76 DRM_FORMAT_C8,
77 DRM_FORMAT_RGB565,
78 DRM_FORMAT_XRGB8888,
465c120c 79 DRM_FORMAT_XBGR8888,
67fe7dc5 80 DRM_FORMAT_ARGB8888,
465c120c
MR
81 DRM_FORMAT_ABGR8888,
82 DRM_FORMAT_XRGB2101010,
465c120c 83 DRM_FORMAT_XBGR2101010,
ea916ea0
KM
84 DRM_FORMAT_YUYV,
85 DRM_FORMAT_YVYU,
86 DRM_FORMAT_UYVY,
87 DRM_FORMAT_VYUY,
465c120c
MR
88};
89
3d7d6510
MR
90/* Cursor formats */
91static const uint32_t intel_cursor_formats[] = {
92 DRM_FORMAT_ARGB8888,
93};
94
f1f644dc 95static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 96 struct intel_crtc_state *pipe_config);
18442d08 97static void ironlake_pch_clock_get(struct intel_crtc *crtc,
5cec258b 98 struct intel_crtc_state *pipe_config);
f1f644dc 99
eb1bfe80
JB
100static int intel_framebuffer_init(struct drm_device *dev,
101 struct intel_framebuffer *ifb,
102 struct drm_mode_fb_cmd2 *mode_cmd,
103 struct drm_i915_gem_object *obj);
5b18e57c
DV
104static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc);
105static void intel_set_pipe_timings(struct intel_crtc *intel_crtc);
bc58be60 106static void intel_set_pipe_src_size(struct intel_crtc *intel_crtc);
29407aab 107static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
f769cd24
VK
108 struct intel_link_m_n *m_n,
109 struct intel_link_m_n *m2_n2);
29407aab 110static void ironlake_set_pipeconf(struct drm_crtc *crtc);
229fca97 111static void haswell_set_pipeconf(struct drm_crtc *crtc);
391bf048 112static void haswell_set_pipemisc(struct drm_crtc *crtc);
d288f65f 113static void vlv_prepare_pll(struct intel_crtc *crtc,
5cec258b 114 const struct intel_crtc_state *pipe_config);
d288f65f 115static void chv_prepare_pll(struct intel_crtc *crtc,
5cec258b 116 const struct intel_crtc_state *pipe_config);
5a21b665
DV
117static void intel_begin_crtc_commit(struct drm_crtc *, struct drm_crtc_state *);
118static void intel_finish_crtc_commit(struct drm_crtc *, struct drm_crtc_state *);
549e2bfb
CK
119static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
120 struct intel_crtc_state *crtc_state);
bfd16b2a
ML
121static void skylake_pfit_enable(struct intel_crtc *crtc);
122static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force);
123static void ironlake_pfit_enable(struct intel_crtc *crtc);
043e9bda 124static void intel_modeset_setup_hw_state(struct drm_device *dev);
2622a081 125static void intel_pre_disable_primary_noatomic(struct drm_crtc *crtc);
4e5ca60f 126static int ilk_max_pixel_rate(struct drm_atomic_state *state);
324513c0 127static int bxt_calc_cdclk(int max_pixclk);
e7457a9a 128
d4906093 129struct intel_limit {
4c5def93
ACO
130 struct {
131 int min, max;
132 } dot, vco, n, m, m1, m2, p, p1;
133
134 struct {
135 int dot_limit;
136 int p2_slow, p2_fast;
137 } p2;
d4906093 138};
79e53945 139
bfa7df01
VS
140/* returns HPLL frequency in kHz */
141static int valleyview_get_vco(struct drm_i915_private *dev_priv)
142{
143 int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
144
145 /* Obtain SKU information */
146 mutex_lock(&dev_priv->sb_lock);
147 hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
148 CCK_FUSE_HPLL_FREQ_MASK;
149 mutex_unlock(&dev_priv->sb_lock);
150
151 return vco_freq[hpll_freq] * 1000;
152}
153
c30fec65
VS
154int vlv_get_cck_clock(struct drm_i915_private *dev_priv,
155 const char *name, u32 reg, int ref_freq)
bfa7df01
VS
156{
157 u32 val;
158 int divider;
159
bfa7df01
VS
160 mutex_lock(&dev_priv->sb_lock);
161 val = vlv_cck_read(dev_priv, reg);
162 mutex_unlock(&dev_priv->sb_lock);
163
164 divider = val & CCK_FREQUENCY_VALUES;
165
166 WARN((val & CCK_FREQUENCY_STATUS) !=
167 (divider << CCK_FREQUENCY_STATUS_SHIFT),
168 "%s change in progress\n", name);
169
c30fec65
VS
170 return DIV_ROUND_CLOSEST(ref_freq << 1, divider + 1);
171}
172
173static int vlv_get_cck_clock_hpll(struct drm_i915_private *dev_priv,
174 const char *name, u32 reg)
175{
176 if (dev_priv->hpll_freq == 0)
177 dev_priv->hpll_freq = valleyview_get_vco(dev_priv);
178
179 return vlv_get_cck_clock(dev_priv, name, reg,
180 dev_priv->hpll_freq);
bfa7df01
VS
181}
182
e7dc33f3
VS
183static int
184intel_pch_rawclk(struct drm_i915_private *dev_priv)
d2acd215 185{
e7dc33f3
VS
186 return (I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK) * 1000;
187}
d2acd215 188
e7dc33f3
VS
189static int
190intel_vlv_hrawclk(struct drm_i915_private *dev_priv)
191{
19ab4ed3 192 /* RAWCLK_FREQ_VLV register updated from power well code */
35d38d1f
VS
193 return vlv_get_cck_clock_hpll(dev_priv, "hrawclk",
194 CCK_DISPLAY_REF_CLOCK_CONTROL);
d2acd215
DV
195}
196
e7dc33f3
VS
197static int
198intel_g4x_hrawclk(struct drm_i915_private *dev_priv)
79e50a4f 199{
79e50a4f
JN
200 uint32_t clkcfg;
201
e7dc33f3 202 /* hrawclock is 1/4 the FSB frequency */
79e50a4f
JN
203 clkcfg = I915_READ(CLKCFG);
204 switch (clkcfg & CLKCFG_FSB_MASK) {
205 case CLKCFG_FSB_400:
e7dc33f3 206 return 100000;
79e50a4f 207 case CLKCFG_FSB_533:
e7dc33f3 208 return 133333;
79e50a4f 209 case CLKCFG_FSB_667:
e7dc33f3 210 return 166667;
79e50a4f 211 case CLKCFG_FSB_800:
e7dc33f3 212 return 200000;
79e50a4f 213 case CLKCFG_FSB_1067:
e7dc33f3 214 return 266667;
79e50a4f 215 case CLKCFG_FSB_1333:
e7dc33f3 216 return 333333;
79e50a4f
JN
217 /* these two are just a guess; one of them might be right */
218 case CLKCFG_FSB_1600:
219 case CLKCFG_FSB_1600_ALT:
e7dc33f3 220 return 400000;
79e50a4f 221 default:
e7dc33f3 222 return 133333;
79e50a4f
JN
223 }
224}
225
19ab4ed3 226void intel_update_rawclk(struct drm_i915_private *dev_priv)
e7dc33f3
VS
227{
228 if (HAS_PCH_SPLIT(dev_priv))
229 dev_priv->rawclk_freq = intel_pch_rawclk(dev_priv);
230 else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
231 dev_priv->rawclk_freq = intel_vlv_hrawclk(dev_priv);
232 else if (IS_G4X(dev_priv) || IS_PINEVIEW(dev_priv))
233 dev_priv->rawclk_freq = intel_g4x_hrawclk(dev_priv);
234 else
235 return; /* no rawclk on other platforms, or no need to know it */
236
237 DRM_DEBUG_DRIVER("rawclk rate: %d kHz\n", dev_priv->rawclk_freq);
238}
239
bfa7df01
VS
240static void intel_update_czclk(struct drm_i915_private *dev_priv)
241{
666a4537 242 if (!(IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)))
bfa7df01
VS
243 return;
244
245 dev_priv->czclk_freq = vlv_get_cck_clock_hpll(dev_priv, "czclk",
246 CCK_CZ_CLOCK_CONTROL);
247
248 DRM_DEBUG_DRIVER("CZ clock rate: %d kHz\n", dev_priv->czclk_freq);
249}
250
021357ac 251static inline u32 /* units of 100MHz */
21a727b3
VS
252intel_fdi_link_freq(struct drm_i915_private *dev_priv,
253 const struct intel_crtc_state *pipe_config)
021357ac 254{
21a727b3
VS
255 if (HAS_DDI(dev_priv))
256 return pipe_config->port_clock; /* SPLL */
257 else if (IS_GEN5(dev_priv))
258 return ((I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2) * 10000;
e3b247da 259 else
21a727b3 260 return 270000;
021357ac
CW
261}
262
1b6f4958 263static const struct intel_limit intel_limits_i8xx_dac = {
0206e353 264 .dot = { .min = 25000, .max = 350000 },
9c333719 265 .vco = { .min = 908000, .max = 1512000 },
91dbe5fb 266 .n = { .min = 2, .max = 16 },
0206e353
AJ
267 .m = { .min = 96, .max = 140 },
268 .m1 = { .min = 18, .max = 26 },
269 .m2 = { .min = 6, .max = 16 },
270 .p = { .min = 4, .max = 128 },
271 .p1 = { .min = 2, .max = 33 },
273e27ca
EA
272 .p2 = { .dot_limit = 165000,
273 .p2_slow = 4, .p2_fast = 2 },
e4b36699
KP
274};
275
1b6f4958 276static const struct intel_limit intel_limits_i8xx_dvo = {
5d536e28 277 .dot = { .min = 25000, .max = 350000 },
9c333719 278 .vco = { .min = 908000, .max = 1512000 },
91dbe5fb 279 .n = { .min = 2, .max = 16 },
5d536e28
DV
280 .m = { .min = 96, .max = 140 },
281 .m1 = { .min = 18, .max = 26 },
282 .m2 = { .min = 6, .max = 16 },
283 .p = { .min = 4, .max = 128 },
284 .p1 = { .min = 2, .max = 33 },
285 .p2 = { .dot_limit = 165000,
286 .p2_slow = 4, .p2_fast = 4 },
287};
288
1b6f4958 289static const struct intel_limit intel_limits_i8xx_lvds = {
0206e353 290 .dot = { .min = 25000, .max = 350000 },
9c333719 291 .vco = { .min = 908000, .max = 1512000 },
91dbe5fb 292 .n = { .min = 2, .max = 16 },
0206e353
AJ
293 .m = { .min = 96, .max = 140 },
294 .m1 = { .min = 18, .max = 26 },
295 .m2 = { .min = 6, .max = 16 },
296 .p = { .min = 4, .max = 128 },
297 .p1 = { .min = 1, .max = 6 },
273e27ca
EA
298 .p2 = { .dot_limit = 165000,
299 .p2_slow = 14, .p2_fast = 7 },
e4b36699 300};
273e27ca 301
1b6f4958 302static const struct intel_limit intel_limits_i9xx_sdvo = {
0206e353
AJ
303 .dot = { .min = 20000, .max = 400000 },
304 .vco = { .min = 1400000, .max = 2800000 },
305 .n = { .min = 1, .max = 6 },
306 .m = { .min = 70, .max = 120 },
4f7dfb67
PJ
307 .m1 = { .min = 8, .max = 18 },
308 .m2 = { .min = 3, .max = 7 },
0206e353
AJ
309 .p = { .min = 5, .max = 80 },
310 .p1 = { .min = 1, .max = 8 },
273e27ca
EA
311 .p2 = { .dot_limit = 200000,
312 .p2_slow = 10, .p2_fast = 5 },
e4b36699
KP
313};
314
1b6f4958 315static const struct intel_limit intel_limits_i9xx_lvds = {
0206e353
AJ
316 .dot = { .min = 20000, .max = 400000 },
317 .vco = { .min = 1400000, .max = 2800000 },
318 .n = { .min = 1, .max = 6 },
319 .m = { .min = 70, .max = 120 },
53a7d2d1
PJ
320 .m1 = { .min = 8, .max = 18 },
321 .m2 = { .min = 3, .max = 7 },
0206e353
AJ
322 .p = { .min = 7, .max = 98 },
323 .p1 = { .min = 1, .max = 8 },
273e27ca
EA
324 .p2 = { .dot_limit = 112000,
325 .p2_slow = 14, .p2_fast = 7 },
e4b36699
KP
326};
327
273e27ca 328
1b6f4958 329static const struct intel_limit intel_limits_g4x_sdvo = {
273e27ca
EA
330 .dot = { .min = 25000, .max = 270000 },
331 .vco = { .min = 1750000, .max = 3500000},
332 .n = { .min = 1, .max = 4 },
333 .m = { .min = 104, .max = 138 },
334 .m1 = { .min = 17, .max = 23 },
335 .m2 = { .min = 5, .max = 11 },
336 .p = { .min = 10, .max = 30 },
337 .p1 = { .min = 1, .max = 3},
338 .p2 = { .dot_limit = 270000,
339 .p2_slow = 10,
340 .p2_fast = 10
044c7c41 341 },
e4b36699
KP
342};
343
1b6f4958 344static const struct intel_limit intel_limits_g4x_hdmi = {
273e27ca
EA
345 .dot = { .min = 22000, .max = 400000 },
346 .vco = { .min = 1750000, .max = 3500000},
347 .n = { .min = 1, .max = 4 },
348 .m = { .min = 104, .max = 138 },
349 .m1 = { .min = 16, .max = 23 },
350 .m2 = { .min = 5, .max = 11 },
351 .p = { .min = 5, .max = 80 },
352 .p1 = { .min = 1, .max = 8},
353 .p2 = { .dot_limit = 165000,
354 .p2_slow = 10, .p2_fast = 5 },
e4b36699
KP
355};
356
1b6f4958 357static const struct intel_limit intel_limits_g4x_single_channel_lvds = {
273e27ca
EA
358 .dot = { .min = 20000, .max = 115000 },
359 .vco = { .min = 1750000, .max = 3500000 },
360 .n = { .min = 1, .max = 3 },
361 .m = { .min = 104, .max = 138 },
362 .m1 = { .min = 17, .max = 23 },
363 .m2 = { .min = 5, .max = 11 },
364 .p = { .min = 28, .max = 112 },
365 .p1 = { .min = 2, .max = 8 },
366 .p2 = { .dot_limit = 0,
367 .p2_slow = 14, .p2_fast = 14
044c7c41 368 },
e4b36699
KP
369};
370
1b6f4958 371static const struct intel_limit intel_limits_g4x_dual_channel_lvds = {
273e27ca
EA
372 .dot = { .min = 80000, .max = 224000 },
373 .vco = { .min = 1750000, .max = 3500000 },
374 .n = { .min = 1, .max = 3 },
375 .m = { .min = 104, .max = 138 },
376 .m1 = { .min = 17, .max = 23 },
377 .m2 = { .min = 5, .max = 11 },
378 .p = { .min = 14, .max = 42 },
379 .p1 = { .min = 2, .max = 6 },
380 .p2 = { .dot_limit = 0,
381 .p2_slow = 7, .p2_fast = 7
044c7c41 382 },
e4b36699
KP
383};
384
1b6f4958 385static const struct intel_limit intel_limits_pineview_sdvo = {
0206e353
AJ
386 .dot = { .min = 20000, .max = 400000},
387 .vco = { .min = 1700000, .max = 3500000 },
273e27ca 388 /* Pineview's Ncounter is a ring counter */
0206e353
AJ
389 .n = { .min = 3, .max = 6 },
390 .m = { .min = 2, .max = 256 },
273e27ca 391 /* Pineview only has one combined m divider, which we treat as m2. */
0206e353
AJ
392 .m1 = { .min = 0, .max = 0 },
393 .m2 = { .min = 0, .max = 254 },
394 .p = { .min = 5, .max = 80 },
395 .p1 = { .min = 1, .max = 8 },
273e27ca
EA
396 .p2 = { .dot_limit = 200000,
397 .p2_slow = 10, .p2_fast = 5 },
e4b36699
KP
398};
399
1b6f4958 400static const struct intel_limit intel_limits_pineview_lvds = {
0206e353
AJ
401 .dot = { .min = 20000, .max = 400000 },
402 .vco = { .min = 1700000, .max = 3500000 },
403 .n = { .min = 3, .max = 6 },
404 .m = { .min = 2, .max = 256 },
405 .m1 = { .min = 0, .max = 0 },
406 .m2 = { .min = 0, .max = 254 },
407 .p = { .min = 7, .max = 112 },
408 .p1 = { .min = 1, .max = 8 },
273e27ca
EA
409 .p2 = { .dot_limit = 112000,
410 .p2_slow = 14, .p2_fast = 14 },
e4b36699
KP
411};
412
273e27ca
EA
413/* Ironlake / Sandybridge
414 *
415 * We calculate clock using (register_value + 2) for N/M1/M2, so here
416 * the range value for them is (actual_value - 2).
417 */
1b6f4958 418static const struct intel_limit intel_limits_ironlake_dac = {
273e27ca
EA
419 .dot = { .min = 25000, .max = 350000 },
420 .vco = { .min = 1760000, .max = 3510000 },
421 .n = { .min = 1, .max = 5 },
422 .m = { .min = 79, .max = 127 },
423 .m1 = { .min = 12, .max = 22 },
424 .m2 = { .min = 5, .max = 9 },
425 .p = { .min = 5, .max = 80 },
426 .p1 = { .min = 1, .max = 8 },
427 .p2 = { .dot_limit = 225000,
428 .p2_slow = 10, .p2_fast = 5 },
e4b36699
KP
429};
430
1b6f4958 431static const struct intel_limit intel_limits_ironlake_single_lvds = {
273e27ca
EA
432 .dot = { .min = 25000, .max = 350000 },
433 .vco = { .min = 1760000, .max = 3510000 },
434 .n = { .min = 1, .max = 3 },
435 .m = { .min = 79, .max = 118 },
436 .m1 = { .min = 12, .max = 22 },
437 .m2 = { .min = 5, .max = 9 },
438 .p = { .min = 28, .max = 112 },
439 .p1 = { .min = 2, .max = 8 },
440 .p2 = { .dot_limit = 225000,
441 .p2_slow = 14, .p2_fast = 14 },
b91ad0ec
ZW
442};
443
1b6f4958 444static const struct intel_limit intel_limits_ironlake_dual_lvds = {
273e27ca
EA
445 .dot = { .min = 25000, .max = 350000 },
446 .vco = { .min = 1760000, .max = 3510000 },
447 .n = { .min = 1, .max = 3 },
448 .m = { .min = 79, .max = 127 },
449 .m1 = { .min = 12, .max = 22 },
450 .m2 = { .min = 5, .max = 9 },
451 .p = { .min = 14, .max = 56 },
452 .p1 = { .min = 2, .max = 8 },
453 .p2 = { .dot_limit = 225000,
454 .p2_slow = 7, .p2_fast = 7 },
b91ad0ec
ZW
455};
456
273e27ca 457/* LVDS 100mhz refclk limits. */
1b6f4958 458static const struct intel_limit intel_limits_ironlake_single_lvds_100m = {
273e27ca
EA
459 .dot = { .min = 25000, .max = 350000 },
460 .vco = { .min = 1760000, .max = 3510000 },
461 .n = { .min = 1, .max = 2 },
462 .m = { .min = 79, .max = 126 },
463 .m1 = { .min = 12, .max = 22 },
464 .m2 = { .min = 5, .max = 9 },
465 .p = { .min = 28, .max = 112 },
0206e353 466 .p1 = { .min = 2, .max = 8 },
273e27ca
EA
467 .p2 = { .dot_limit = 225000,
468 .p2_slow = 14, .p2_fast = 14 },
b91ad0ec
ZW
469};
470
1b6f4958 471static const struct intel_limit intel_limits_ironlake_dual_lvds_100m = {
273e27ca
EA
472 .dot = { .min = 25000, .max = 350000 },
473 .vco = { .min = 1760000, .max = 3510000 },
474 .n = { .min = 1, .max = 3 },
475 .m = { .min = 79, .max = 126 },
476 .m1 = { .min = 12, .max = 22 },
477 .m2 = { .min = 5, .max = 9 },
478 .p = { .min = 14, .max = 42 },
0206e353 479 .p1 = { .min = 2, .max = 6 },
273e27ca
EA
480 .p2 = { .dot_limit = 225000,
481 .p2_slow = 7, .p2_fast = 7 },
4547668a
ZY
482};
483
1b6f4958 484static const struct intel_limit intel_limits_vlv = {
f01b7962
VS
485 /*
486 * These are the data rate limits (measured in fast clocks)
487 * since those are the strictest limits we have. The fast
488 * clock and actual rate limits are more relaxed, so checking
489 * them would make no difference.
490 */
491 .dot = { .min = 25000 * 5, .max = 270000 * 5 },
75e53986 492 .vco = { .min = 4000000, .max = 6000000 },
a0c4da24 493 .n = { .min = 1, .max = 7 },
a0c4da24
JB
494 .m1 = { .min = 2, .max = 3 },
495 .m2 = { .min = 11, .max = 156 },
b99ab663 496 .p1 = { .min = 2, .max = 3 },
5fdc9c49 497 .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
a0c4da24
JB
498};
499
1b6f4958 500static const struct intel_limit intel_limits_chv = {
ef9348c8
CML
501 /*
502 * These are the data rate limits (measured in fast clocks)
503 * since those are the strictest limits we have. The fast
504 * clock and actual rate limits are more relaxed, so checking
505 * them would make no difference.
506 */
507 .dot = { .min = 25000 * 5, .max = 540000 * 5},
17fe1021 508 .vco = { .min = 4800000, .max = 6480000 },
ef9348c8
CML
509 .n = { .min = 1, .max = 1 },
510 .m1 = { .min = 2, .max = 2 },
511 .m2 = { .min = 24 << 22, .max = 175 << 22 },
512 .p1 = { .min = 2, .max = 4 },
513 .p2 = { .p2_slow = 1, .p2_fast = 14 },
514};
515
1b6f4958 516static const struct intel_limit intel_limits_bxt = {
5ab7b0b7
ID
517 /* FIXME: find real dot limits */
518 .dot = { .min = 0, .max = INT_MAX },
e6292556 519 .vco = { .min = 4800000, .max = 6700000 },
5ab7b0b7
ID
520 .n = { .min = 1, .max = 1 },
521 .m1 = { .min = 2, .max = 2 },
522 /* FIXME: find real m2 limits */
523 .m2 = { .min = 2 << 22, .max = 255 << 22 },
524 .p1 = { .min = 2, .max = 4 },
525 .p2 = { .p2_slow = 1, .p2_fast = 20 },
526};
527
cdba954e
ACO
528static bool
529needs_modeset(struct drm_crtc_state *state)
530{
fc596660 531 return drm_atomic_crtc_needs_modeset(state);
cdba954e
ACO
532}
533
dccbea3b
ID
534/*
535 * Platform specific helpers to calculate the port PLL loopback- (clock.m),
536 * and post-divider (clock.p) values, pre- (clock.vco) and post-divided fast
537 * (clock.dot) clock rates. This fast dot clock is fed to the port's IO logic.
538 * The helpers' return value is the rate of the clock that is fed to the
539 * display engine's pipe which can be the above fast dot clock rate or a
540 * divided-down version of it.
541 */
f2b115e6 542/* m1 is reserved as 0 in Pineview, n is a ring counter */
9e2c8475 543static int pnv_calc_dpll_params(int refclk, struct dpll *clock)
79e53945 544{
2177832f
SL
545 clock->m = clock->m2 + 2;
546 clock->p = clock->p1 * clock->p2;
ed5ca77e 547 if (WARN_ON(clock->n == 0 || clock->p == 0))
dccbea3b 548 return 0;
fb03ac01
VS
549 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
550 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
dccbea3b
ID
551
552 return clock->dot;
2177832f
SL
553}
554
7429e9d4
DV
555static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
556{
557 return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
558}
559
9e2c8475 560static int i9xx_calc_dpll_params(int refclk, struct dpll *clock)
2177832f 561{
7429e9d4 562 clock->m = i9xx_dpll_compute_m(clock);
79e53945 563 clock->p = clock->p1 * clock->p2;
ed5ca77e 564 if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
dccbea3b 565 return 0;
fb03ac01
VS
566 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
567 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
dccbea3b
ID
568
569 return clock->dot;
79e53945
JB
570}
571
9e2c8475 572static int vlv_calc_dpll_params(int refclk, struct dpll *clock)
589eca67
ID
573{
574 clock->m = clock->m1 * clock->m2;
575 clock->p = clock->p1 * clock->p2;
576 if (WARN_ON(clock->n == 0 || clock->p == 0))
dccbea3b 577 return 0;
589eca67
ID
578 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
579 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
dccbea3b
ID
580
581 return clock->dot / 5;
589eca67
ID
582}
583
9e2c8475 584int chv_calc_dpll_params(int refclk, struct dpll *clock)
ef9348c8
CML
585{
586 clock->m = clock->m1 * clock->m2;
587 clock->p = clock->p1 * clock->p2;
588 if (WARN_ON(clock->n == 0 || clock->p == 0))
dccbea3b 589 return 0;
ef9348c8
CML
590 clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
591 clock->n << 22);
592 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
dccbea3b
ID
593
594 return clock->dot / 5;
ef9348c8
CML
595}
596
7c04d1d9 597#define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
79e53945
JB
598/**
599 * Returns whether the given set of divisors are valid for a given refclk with
600 * the given connectors.
601 */
602
1b894b59 603static bool intel_PLL_is_valid(struct drm_device *dev,
1b6f4958 604 const struct intel_limit *limit,
9e2c8475 605 const struct dpll *clock)
79e53945 606{
f01b7962
VS
607 if (clock->n < limit->n.min || limit->n.max < clock->n)
608 INTELPllInvalid("n out of range\n");
79e53945 609 if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
0206e353 610 INTELPllInvalid("p1 out of range\n");
79e53945 611 if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
0206e353 612 INTELPllInvalid("m2 out of range\n");
79e53945 613 if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
0206e353 614 INTELPllInvalid("m1 out of range\n");
f01b7962 615
666a4537
WB
616 if (!IS_PINEVIEW(dev) && !IS_VALLEYVIEW(dev) &&
617 !IS_CHERRYVIEW(dev) && !IS_BROXTON(dev))
f01b7962
VS
618 if (clock->m1 <= clock->m2)
619 INTELPllInvalid("m1 <= m2\n");
620
666a4537 621 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev) && !IS_BROXTON(dev)) {
f01b7962
VS
622 if (clock->p < limit->p.min || limit->p.max < clock->p)
623 INTELPllInvalid("p out of range\n");
624 if (clock->m < limit->m.min || limit->m.max < clock->m)
625 INTELPllInvalid("m out of range\n");
626 }
627
79e53945 628 if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
0206e353 629 INTELPllInvalid("vco out of range\n");
79e53945
JB
630 /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
631 * connector, etc., rather than just a single range.
632 */
633 if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
0206e353 634 INTELPllInvalid("dot out of range\n");
79e53945
JB
635
636 return true;
637}
638
3b1429d9 639static int
1b6f4958 640i9xx_select_p2_div(const struct intel_limit *limit,
3b1429d9
VS
641 const struct intel_crtc_state *crtc_state,
642 int target)
79e53945 643{
3b1429d9 644 struct drm_device *dev = crtc_state->base.crtc->dev;
79e53945 645
2d84d2b3 646 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
79e53945 647 /*
a210b028
DV
648 * For LVDS just rely on its current settings for dual-channel.
649 * We haven't figured out how to reliably set up different
650 * single/dual channel state, if we even can.
79e53945 651 */
1974cad0 652 if (intel_is_dual_link_lvds(dev))
3b1429d9 653 return limit->p2.p2_fast;
79e53945 654 else
3b1429d9 655 return limit->p2.p2_slow;
79e53945
JB
656 } else {
657 if (target < limit->p2.dot_limit)
3b1429d9 658 return limit->p2.p2_slow;
79e53945 659 else
3b1429d9 660 return limit->p2.p2_fast;
79e53945 661 }
3b1429d9
VS
662}
663
70e8aa21
ACO
664/*
665 * Returns a set of divisors for the desired target clock with the given
666 * refclk, or FALSE. The returned values represent the clock equation:
667 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
668 *
669 * Target and reference clocks are specified in kHz.
670 *
671 * If match_clock is provided, then best_clock P divider must match the P
672 * divider from @match_clock used for LVDS downclocking.
673 */
3b1429d9 674static bool
1b6f4958 675i9xx_find_best_dpll(const struct intel_limit *limit,
3b1429d9 676 struct intel_crtc_state *crtc_state,
9e2c8475
ACO
677 int target, int refclk, struct dpll *match_clock,
678 struct dpll *best_clock)
3b1429d9
VS
679{
680 struct drm_device *dev = crtc_state->base.crtc->dev;
9e2c8475 681 struct dpll clock;
3b1429d9 682 int err = target;
79e53945 683
0206e353 684 memset(best_clock, 0, sizeof(*best_clock));
79e53945 685
3b1429d9
VS
686 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
687
42158660
ZY
688 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
689 clock.m1++) {
690 for (clock.m2 = limit->m2.min;
691 clock.m2 <= limit->m2.max; clock.m2++) {
c0efc387 692 if (clock.m2 >= clock.m1)
42158660
ZY
693 break;
694 for (clock.n = limit->n.min;
695 clock.n <= limit->n.max; clock.n++) {
696 for (clock.p1 = limit->p1.min;
697 clock.p1 <= limit->p1.max; clock.p1++) {
79e53945
JB
698 int this_err;
699
dccbea3b 700 i9xx_calc_dpll_params(refclk, &clock);
ac58c3f0
DV
701 if (!intel_PLL_is_valid(dev, limit,
702 &clock))
703 continue;
704 if (match_clock &&
705 clock.p != match_clock->p)
706 continue;
707
708 this_err = abs(clock.dot - target);
709 if (this_err < err) {
710 *best_clock = clock;
711 err = this_err;
712 }
713 }
714 }
715 }
716 }
717
718 return (err != target);
719}
720
70e8aa21
ACO
721/*
722 * Returns a set of divisors for the desired target clock with the given
723 * refclk, or FALSE. The returned values represent the clock equation:
724 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
725 *
726 * Target and reference clocks are specified in kHz.
727 *
728 * If match_clock is provided, then best_clock P divider must match the P
729 * divider from @match_clock used for LVDS downclocking.
730 */
ac58c3f0 731static bool
1b6f4958 732pnv_find_best_dpll(const struct intel_limit *limit,
a93e255f 733 struct intel_crtc_state *crtc_state,
9e2c8475
ACO
734 int target, int refclk, struct dpll *match_clock,
735 struct dpll *best_clock)
79e53945 736{
3b1429d9 737 struct drm_device *dev = crtc_state->base.crtc->dev;
9e2c8475 738 struct dpll clock;
79e53945
JB
739 int err = target;
740
0206e353 741 memset(best_clock, 0, sizeof(*best_clock));
79e53945 742
3b1429d9
VS
743 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
744
42158660
ZY
745 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
746 clock.m1++) {
747 for (clock.m2 = limit->m2.min;
748 clock.m2 <= limit->m2.max; clock.m2++) {
42158660
ZY
749 for (clock.n = limit->n.min;
750 clock.n <= limit->n.max; clock.n++) {
751 for (clock.p1 = limit->p1.min;
752 clock.p1 <= limit->p1.max; clock.p1++) {
79e53945
JB
753 int this_err;
754
dccbea3b 755 pnv_calc_dpll_params(refclk, &clock);
1b894b59
CW
756 if (!intel_PLL_is_valid(dev, limit,
757 &clock))
79e53945 758 continue;
cec2f356
SP
759 if (match_clock &&
760 clock.p != match_clock->p)
761 continue;
79e53945
JB
762
763 this_err = abs(clock.dot - target);
764 if (this_err < err) {
765 *best_clock = clock;
766 err = this_err;
767 }
768 }
769 }
770 }
771 }
772
773 return (err != target);
774}
775
997c030c
ACO
776/*
777 * Returns a set of divisors for the desired target clock with the given
778 * refclk, or FALSE. The returned values represent the clock equation:
779 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
70e8aa21
ACO
780 *
781 * Target and reference clocks are specified in kHz.
782 *
783 * If match_clock is provided, then best_clock P divider must match the P
784 * divider from @match_clock used for LVDS downclocking.
997c030c 785 */
d4906093 786static bool
1b6f4958 787g4x_find_best_dpll(const struct intel_limit *limit,
a93e255f 788 struct intel_crtc_state *crtc_state,
9e2c8475
ACO
789 int target, int refclk, struct dpll *match_clock,
790 struct dpll *best_clock)
d4906093 791{
3b1429d9 792 struct drm_device *dev = crtc_state->base.crtc->dev;
9e2c8475 793 struct dpll clock;
d4906093 794 int max_n;
3b1429d9 795 bool found = false;
6ba770dc
AJ
796 /* approximately equals target * 0.00585 */
797 int err_most = (target >> 8) + (target >> 9);
d4906093
ML
798
799 memset(best_clock, 0, sizeof(*best_clock));
3b1429d9
VS
800
801 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
802
d4906093 803 max_n = limit->n.max;
f77f13e2 804 /* based on hardware requirement, prefer smaller n to precision */
d4906093 805 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
f77f13e2 806 /* based on hardware requirement, prefere larger m1,m2 */
d4906093
ML
807 for (clock.m1 = limit->m1.max;
808 clock.m1 >= limit->m1.min; clock.m1--) {
809 for (clock.m2 = limit->m2.max;
810 clock.m2 >= limit->m2.min; clock.m2--) {
811 for (clock.p1 = limit->p1.max;
812 clock.p1 >= limit->p1.min; clock.p1--) {
813 int this_err;
814
dccbea3b 815 i9xx_calc_dpll_params(refclk, &clock);
1b894b59
CW
816 if (!intel_PLL_is_valid(dev, limit,
817 &clock))
d4906093 818 continue;
1b894b59
CW
819
820 this_err = abs(clock.dot - target);
d4906093
ML
821 if (this_err < err_most) {
822 *best_clock = clock;
823 err_most = this_err;
824 max_n = clock.n;
825 found = true;
826 }
827 }
828 }
829 }
830 }
2c07245f
ZW
831 return found;
832}
833
d5dd62bd
ID
834/*
835 * Check if the calculated PLL configuration is more optimal compared to the
836 * best configuration and error found so far. Return the calculated error.
837 */
838static bool vlv_PLL_is_optimal(struct drm_device *dev, int target_freq,
9e2c8475
ACO
839 const struct dpll *calculated_clock,
840 const struct dpll *best_clock,
d5dd62bd
ID
841 unsigned int best_error_ppm,
842 unsigned int *error_ppm)
843{
9ca3ba01
ID
844 /*
845 * For CHV ignore the error and consider only the P value.
846 * Prefer a bigger P value based on HW requirements.
847 */
848 if (IS_CHERRYVIEW(dev)) {
849 *error_ppm = 0;
850
851 return calculated_clock->p > best_clock->p;
852 }
853
24be4e46
ID
854 if (WARN_ON_ONCE(!target_freq))
855 return false;
856
d5dd62bd
ID
857 *error_ppm = div_u64(1000000ULL *
858 abs(target_freq - calculated_clock->dot),
859 target_freq);
860 /*
861 * Prefer a better P value over a better (smaller) error if the error
862 * is small. Ensure this preference for future configurations too by
863 * setting the error to 0.
864 */
865 if (*error_ppm < 100 && calculated_clock->p > best_clock->p) {
866 *error_ppm = 0;
867
868 return true;
869 }
870
871 return *error_ppm + 10 < best_error_ppm;
872}
873
65b3d6a9
ACO
874/*
875 * Returns a set of divisors for the desired target clock with the given
876 * refclk, or FALSE. The returned values represent the clock equation:
877 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
878 */
a0c4da24 879static bool
1b6f4958 880vlv_find_best_dpll(const struct intel_limit *limit,
a93e255f 881 struct intel_crtc_state *crtc_state,
9e2c8475
ACO
882 int target, int refclk, struct dpll *match_clock,
883 struct dpll *best_clock)
a0c4da24 884{
a93e255f 885 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
a919ff14 886 struct drm_device *dev = crtc->base.dev;
9e2c8475 887 struct dpll clock;
69e4f900 888 unsigned int bestppm = 1000000;
27e639bf
VS
889 /* min update 19.2 MHz */
890 int max_n = min(limit->n.max, refclk / 19200);
49e497ef 891 bool found = false;
a0c4da24 892
6b4bf1c4
VS
893 target *= 5; /* fast clock */
894
895 memset(best_clock, 0, sizeof(*best_clock));
a0c4da24
JB
896
897 /* based on hardware requirement, prefer smaller n to precision */
27e639bf 898 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
811bbf05 899 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
889059d8 900 for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
c1a9ae43 901 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
6b4bf1c4 902 clock.p = clock.p1 * clock.p2;
a0c4da24 903 /* based on hardware requirement, prefer bigger m1,m2 values */
6b4bf1c4 904 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
d5dd62bd 905 unsigned int ppm;
69e4f900 906
6b4bf1c4
VS
907 clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
908 refclk * clock.m1);
909
dccbea3b 910 vlv_calc_dpll_params(refclk, &clock);
43b0ac53 911
f01b7962
VS
912 if (!intel_PLL_is_valid(dev, limit,
913 &clock))
43b0ac53
VS
914 continue;
915
d5dd62bd
ID
916 if (!vlv_PLL_is_optimal(dev, target,
917 &clock,
918 best_clock,
919 bestppm, &ppm))
920 continue;
6b4bf1c4 921
d5dd62bd
ID
922 *best_clock = clock;
923 bestppm = ppm;
924 found = true;
a0c4da24
JB
925 }
926 }
927 }
928 }
a0c4da24 929
49e497ef 930 return found;
a0c4da24 931}
a4fc5ed6 932
65b3d6a9
ACO
933/*
934 * Returns a set of divisors for the desired target clock with the given
935 * refclk, or FALSE. The returned values represent the clock equation:
936 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
937 */
ef9348c8 938static bool
1b6f4958 939chv_find_best_dpll(const struct intel_limit *limit,
a93e255f 940 struct intel_crtc_state *crtc_state,
9e2c8475
ACO
941 int target, int refclk, struct dpll *match_clock,
942 struct dpll *best_clock)
ef9348c8 943{
a93e255f 944 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
a919ff14 945 struct drm_device *dev = crtc->base.dev;
9ca3ba01 946 unsigned int best_error_ppm;
9e2c8475 947 struct dpll clock;
ef9348c8
CML
948 uint64_t m2;
949 int found = false;
950
951 memset(best_clock, 0, sizeof(*best_clock));
9ca3ba01 952 best_error_ppm = 1000000;
ef9348c8
CML
953
954 /*
955 * Based on hardware doc, the n always set to 1, and m1 always
956 * set to 2. If requires to support 200Mhz refclk, we need to
957 * revisit this because n may not 1 anymore.
958 */
959 clock.n = 1, clock.m1 = 2;
960 target *= 5; /* fast clock */
961
962 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
963 for (clock.p2 = limit->p2.p2_fast;
964 clock.p2 >= limit->p2.p2_slow;
965 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
9ca3ba01 966 unsigned int error_ppm;
ef9348c8
CML
967
968 clock.p = clock.p1 * clock.p2;
969
970 m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
971 clock.n) << 22, refclk * clock.m1);
972
973 if (m2 > INT_MAX/clock.m1)
974 continue;
975
976 clock.m2 = m2;
977
dccbea3b 978 chv_calc_dpll_params(refclk, &clock);
ef9348c8
CML
979
980 if (!intel_PLL_is_valid(dev, limit, &clock))
981 continue;
982
9ca3ba01
ID
983 if (!vlv_PLL_is_optimal(dev, target, &clock, best_clock,
984 best_error_ppm, &error_ppm))
985 continue;
986
987 *best_clock = clock;
988 best_error_ppm = error_ppm;
989 found = true;
ef9348c8
CML
990 }
991 }
992
993 return found;
994}
995
5ab7b0b7 996bool bxt_find_best_dpll(struct intel_crtc_state *crtc_state, int target_clock,
9e2c8475 997 struct dpll *best_clock)
5ab7b0b7 998{
65b3d6a9 999 int refclk = 100000;
1b6f4958 1000 const struct intel_limit *limit = &intel_limits_bxt;
5ab7b0b7 1001
65b3d6a9 1002 return chv_find_best_dpll(limit, crtc_state,
5ab7b0b7
ID
1003 target_clock, refclk, NULL, best_clock);
1004}
1005
20ddf665
VS
1006bool intel_crtc_active(struct drm_crtc *crtc)
1007{
1008 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1009
1010 /* Be paranoid as we can arrive here with only partial
1011 * state retrieved from the hardware during setup.
1012 *
241bfc38 1013 * We can ditch the adjusted_mode.crtc_clock check as soon
20ddf665
VS
1014 * as Haswell has gained clock readout/fastboot support.
1015 *
66e514c1 1016 * We can ditch the crtc->primary->fb check as soon as we can
20ddf665 1017 * properly reconstruct framebuffers.
c3d1f436
MR
1018 *
1019 * FIXME: The intel_crtc->active here should be switched to
1020 * crtc->state->active once we have proper CRTC states wired up
1021 * for atomic.
20ddf665 1022 */
c3d1f436 1023 return intel_crtc->active && crtc->primary->state->fb &&
6e3c9717 1024 intel_crtc->config->base.adjusted_mode.crtc_clock;
20ddf665
VS
1025}
1026
a5c961d1
PZ
1027enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
1028 enum pipe pipe)
1029{
1030 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
1031 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1032
6e3c9717 1033 return intel_crtc->config->cpu_transcoder;
a5c961d1
PZ
1034}
1035
fbf49ea2
VS
1036static bool pipe_dsl_stopped(struct drm_device *dev, enum pipe pipe)
1037{
fac5e23e 1038 struct drm_i915_private *dev_priv = to_i915(dev);
f0f59a00 1039 i915_reg_t reg = PIPEDSL(pipe);
fbf49ea2
VS
1040 u32 line1, line2;
1041 u32 line_mask;
1042
1043 if (IS_GEN2(dev))
1044 line_mask = DSL_LINEMASK_GEN2;
1045 else
1046 line_mask = DSL_LINEMASK_GEN3;
1047
1048 line1 = I915_READ(reg) & line_mask;
6adfb1ef 1049 msleep(5);
fbf49ea2
VS
1050 line2 = I915_READ(reg) & line_mask;
1051
1052 return line1 == line2;
1053}
1054
ab7ad7f6
KP
1055/*
1056 * intel_wait_for_pipe_off - wait for pipe to turn off
575f7ab7 1057 * @crtc: crtc whose pipe to wait for
9d0498a2
JB
1058 *
1059 * After disabling a pipe, we can't wait for vblank in the usual way,
1060 * spinning on the vblank interrupt status bit, since we won't actually
1061 * see an interrupt when the pipe is disabled.
1062 *
ab7ad7f6
KP
1063 * On Gen4 and above:
1064 * wait for the pipe register state bit to turn off
1065 *
1066 * Otherwise:
1067 * wait for the display line value to settle (it usually
1068 * ends up stopping at the start of the next frame).
58e10eb9 1069 *
9d0498a2 1070 */
575f7ab7 1071static void intel_wait_for_pipe_off(struct intel_crtc *crtc)
9d0498a2 1072{
575f7ab7 1073 struct drm_device *dev = crtc->base.dev;
fac5e23e 1074 struct drm_i915_private *dev_priv = to_i915(dev);
6e3c9717 1075 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
575f7ab7 1076 enum pipe pipe = crtc->pipe;
ab7ad7f6
KP
1077
1078 if (INTEL_INFO(dev)->gen >= 4) {
f0f59a00 1079 i915_reg_t reg = PIPECONF(cpu_transcoder);
ab7ad7f6
KP
1080
1081 /* Wait for the Pipe State to go off */
b8511f53
CW
1082 if (intel_wait_for_register(dev_priv,
1083 reg, I965_PIPECONF_ACTIVE, 0,
1084 100))
284637d9 1085 WARN(1, "pipe_off wait timed out\n");
ab7ad7f6 1086 } else {
ab7ad7f6 1087 /* Wait for the display line to settle */
fbf49ea2 1088 if (wait_for(pipe_dsl_stopped(dev, pipe), 100))
284637d9 1089 WARN(1, "pipe_off wait timed out\n");
ab7ad7f6 1090 }
79e53945
JB
1091}
1092
b24e7179 1093/* Only for pre-ILK configs */
55607e8a
DV
1094void assert_pll(struct drm_i915_private *dev_priv,
1095 enum pipe pipe, bool state)
b24e7179 1096{
b24e7179
JB
1097 u32 val;
1098 bool cur_state;
1099
649636ef 1100 val = I915_READ(DPLL(pipe));
b24e7179 1101 cur_state = !!(val & DPLL_VCO_ENABLE);
e2c719b7 1102 I915_STATE_WARN(cur_state != state,
b24e7179 1103 "PLL state assertion failure (expected %s, current %s)\n",
87ad3212 1104 onoff(state), onoff(cur_state));
b24e7179 1105}
b24e7179 1106
23538ef1 1107/* XXX: the dsi pll is shared between MIPI DSI ports */
8563b1e8 1108void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
23538ef1
JN
1109{
1110 u32 val;
1111 bool cur_state;
1112
a580516d 1113 mutex_lock(&dev_priv->sb_lock);
23538ef1 1114 val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
a580516d 1115 mutex_unlock(&dev_priv->sb_lock);
23538ef1
JN
1116
1117 cur_state = val & DSI_PLL_VCO_EN;
e2c719b7 1118 I915_STATE_WARN(cur_state != state,
23538ef1 1119 "DSI PLL state assertion failure (expected %s, current %s)\n",
87ad3212 1120 onoff(state), onoff(cur_state));
23538ef1 1121}
23538ef1 1122
040484af
JB
1123static void assert_fdi_tx(struct drm_i915_private *dev_priv,
1124 enum pipe pipe, bool state)
1125{
040484af 1126 bool cur_state;
ad80a810
PZ
1127 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1128 pipe);
040484af 1129
2d1fe073 1130 if (HAS_DDI(dev_priv)) {
affa9354 1131 /* DDI does not have a specific FDI_TX register */
649636ef 1132 u32 val = I915_READ(TRANS_DDI_FUNC_CTL(cpu_transcoder));
ad80a810 1133 cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
bf507ef7 1134 } else {
649636ef 1135 u32 val = I915_READ(FDI_TX_CTL(pipe));
bf507ef7
ED
1136 cur_state = !!(val & FDI_TX_ENABLE);
1137 }
e2c719b7 1138 I915_STATE_WARN(cur_state != state,
040484af 1139 "FDI TX state assertion failure (expected %s, current %s)\n",
87ad3212 1140 onoff(state), onoff(cur_state));
040484af
JB
1141}
1142#define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
1143#define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
1144
1145static void assert_fdi_rx(struct drm_i915_private *dev_priv,
1146 enum pipe pipe, bool state)
1147{
040484af
JB
1148 u32 val;
1149 bool cur_state;
1150
649636ef 1151 val = I915_READ(FDI_RX_CTL(pipe));
d63fa0dc 1152 cur_state = !!(val & FDI_RX_ENABLE);
e2c719b7 1153 I915_STATE_WARN(cur_state != state,
040484af 1154 "FDI RX state assertion failure (expected %s, current %s)\n",
87ad3212 1155 onoff(state), onoff(cur_state));
040484af
JB
1156}
1157#define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
1158#define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
1159
1160static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
1161 enum pipe pipe)
1162{
040484af
JB
1163 u32 val;
1164
1165 /* ILK FDI PLL is always enabled */
7e22dbbb 1166 if (IS_GEN5(dev_priv))
040484af
JB
1167 return;
1168
bf507ef7 1169 /* On Haswell, DDI ports are responsible for the FDI PLL setup */
2d1fe073 1170 if (HAS_DDI(dev_priv))
bf507ef7
ED
1171 return;
1172
649636ef 1173 val = I915_READ(FDI_TX_CTL(pipe));
e2c719b7 1174 I915_STATE_WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
040484af
JB
1175}
1176
55607e8a
DV
1177void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
1178 enum pipe pipe, bool state)
040484af 1179{
040484af 1180 u32 val;
55607e8a 1181 bool cur_state;
040484af 1182
649636ef 1183 val = I915_READ(FDI_RX_CTL(pipe));
55607e8a 1184 cur_state = !!(val & FDI_RX_PLL_ENABLE);
e2c719b7 1185 I915_STATE_WARN(cur_state != state,
55607e8a 1186 "FDI RX PLL assertion failure (expected %s, current %s)\n",
87ad3212 1187 onoff(state), onoff(cur_state));
040484af
JB
1188}
1189
b680c37a
DV
1190void assert_panel_unlocked(struct drm_i915_private *dev_priv,
1191 enum pipe pipe)
ea0760cf 1192{
91c8a326 1193 struct drm_device *dev = &dev_priv->drm;
f0f59a00 1194 i915_reg_t pp_reg;
ea0760cf
JB
1195 u32 val;
1196 enum pipe panel_pipe = PIPE_A;
0de3b485 1197 bool locked = true;
ea0760cf 1198
bedd4dba
JN
1199 if (WARN_ON(HAS_DDI(dev)))
1200 return;
1201
1202 if (HAS_PCH_SPLIT(dev)) {
1203 u32 port_sel;
1204
ea0760cf 1205 pp_reg = PCH_PP_CONTROL;
bedd4dba
JN
1206 port_sel = I915_READ(PCH_PP_ON_DELAYS) & PANEL_PORT_SELECT_MASK;
1207
1208 if (port_sel == PANEL_PORT_SELECT_LVDS &&
1209 I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
1210 panel_pipe = PIPE_B;
1211 /* XXX: else fix for eDP */
666a4537 1212 } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
bedd4dba
JN
1213 /* presumably write lock depends on pipe, not port select */
1214 pp_reg = VLV_PIPE_PP_CONTROL(pipe);
1215 panel_pipe = pipe;
ea0760cf
JB
1216 } else {
1217 pp_reg = PP_CONTROL;
bedd4dba
JN
1218 if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
1219 panel_pipe = PIPE_B;
ea0760cf
JB
1220 }
1221
1222 val = I915_READ(pp_reg);
1223 if (!(val & PANEL_POWER_ON) ||
ec49ba2d 1224 ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
ea0760cf
JB
1225 locked = false;
1226
e2c719b7 1227 I915_STATE_WARN(panel_pipe == pipe && locked,
ea0760cf 1228 "panel assertion failure, pipe %c regs locked\n",
9db4a9c7 1229 pipe_name(pipe));
ea0760cf
JB
1230}
1231
93ce0ba6
JN
1232static void assert_cursor(struct drm_i915_private *dev_priv,
1233 enum pipe pipe, bool state)
1234{
91c8a326 1235 struct drm_device *dev = &dev_priv->drm;
93ce0ba6
JN
1236 bool cur_state;
1237
d9d82081 1238 if (IS_845G(dev) || IS_I865G(dev))
0b87c24e 1239 cur_state = I915_READ(CURCNTR(PIPE_A)) & CURSOR_ENABLE;
d9d82081 1240 else
5efb3e28 1241 cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
93ce0ba6 1242
e2c719b7 1243 I915_STATE_WARN(cur_state != state,
93ce0ba6 1244 "cursor on pipe %c assertion failure (expected %s, current %s)\n",
87ad3212 1245 pipe_name(pipe), onoff(state), onoff(cur_state));
93ce0ba6
JN
1246}
1247#define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
1248#define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
1249
b840d907
JB
1250void assert_pipe(struct drm_i915_private *dev_priv,
1251 enum pipe pipe, bool state)
b24e7179 1252{
63d7bbe9 1253 bool cur_state;
702e7a56
PZ
1254 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1255 pipe);
4feed0eb 1256 enum intel_display_power_domain power_domain;
b24e7179 1257
b6b5d049
VS
1258 /* if we need the pipe quirk it must be always on */
1259 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1260 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
8e636784
DV
1261 state = true;
1262
4feed0eb
ID
1263 power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
1264 if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
649636ef 1265 u32 val = I915_READ(PIPECONF(cpu_transcoder));
69310161 1266 cur_state = !!(val & PIPECONF_ENABLE);
4feed0eb
ID
1267
1268 intel_display_power_put(dev_priv, power_domain);
1269 } else {
1270 cur_state = false;
69310161
PZ
1271 }
1272
e2c719b7 1273 I915_STATE_WARN(cur_state != state,
63d7bbe9 1274 "pipe %c assertion failure (expected %s, current %s)\n",
87ad3212 1275 pipe_name(pipe), onoff(state), onoff(cur_state));
b24e7179
JB
1276}
1277
931872fc
CW
1278static void assert_plane(struct drm_i915_private *dev_priv,
1279 enum plane plane, bool state)
b24e7179 1280{
b24e7179 1281 u32 val;
931872fc 1282 bool cur_state;
b24e7179 1283
649636ef 1284 val = I915_READ(DSPCNTR(plane));
931872fc 1285 cur_state = !!(val & DISPLAY_PLANE_ENABLE);
e2c719b7 1286 I915_STATE_WARN(cur_state != state,
931872fc 1287 "plane %c assertion failure (expected %s, current %s)\n",
87ad3212 1288 plane_name(plane), onoff(state), onoff(cur_state));
b24e7179
JB
1289}
1290
931872fc
CW
1291#define assert_plane_enabled(d, p) assert_plane(d, p, true)
1292#define assert_plane_disabled(d, p) assert_plane(d, p, false)
1293
b24e7179
JB
1294static void assert_planes_disabled(struct drm_i915_private *dev_priv,
1295 enum pipe pipe)
1296{
91c8a326 1297 struct drm_device *dev = &dev_priv->drm;
649636ef 1298 int i;
b24e7179 1299
653e1026
VS
1300 /* Primary planes are fixed to pipes on gen4+ */
1301 if (INTEL_INFO(dev)->gen >= 4) {
649636ef 1302 u32 val = I915_READ(DSPCNTR(pipe));
e2c719b7 1303 I915_STATE_WARN(val & DISPLAY_PLANE_ENABLE,
28c05794
AJ
1304 "plane %c assertion failure, should be disabled but not\n",
1305 plane_name(pipe));
19ec1358 1306 return;
28c05794 1307 }
19ec1358 1308
b24e7179 1309 /* Need to check both planes against the pipe */
055e393f 1310 for_each_pipe(dev_priv, i) {
649636ef
VS
1311 u32 val = I915_READ(DSPCNTR(i));
1312 enum pipe cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
b24e7179 1313 DISPPLANE_SEL_PIPE_SHIFT;
e2c719b7 1314 I915_STATE_WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
9db4a9c7
JB
1315 "plane %c assertion failure, should be off on pipe %c but is still active\n",
1316 plane_name(i), pipe_name(pipe));
b24e7179
JB
1317 }
1318}
1319
19332d7a
JB
1320static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
1321 enum pipe pipe)
1322{
91c8a326 1323 struct drm_device *dev = &dev_priv->drm;
649636ef 1324 int sprite;
19332d7a 1325
7feb8b88 1326 if (INTEL_INFO(dev)->gen >= 9) {
3bdcfc0c 1327 for_each_sprite(dev_priv, pipe, sprite) {
649636ef 1328 u32 val = I915_READ(PLANE_CTL(pipe, sprite));
e2c719b7 1329 I915_STATE_WARN(val & PLANE_CTL_ENABLE,
7feb8b88
DL
1330 "plane %d assertion failure, should be off on pipe %c but is still active\n",
1331 sprite, pipe_name(pipe));
1332 }
666a4537 1333 } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
3bdcfc0c 1334 for_each_sprite(dev_priv, pipe, sprite) {
649636ef 1335 u32 val = I915_READ(SPCNTR(pipe, sprite));
e2c719b7 1336 I915_STATE_WARN(val & SP_ENABLE,
20674eef 1337 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
1fe47785 1338 sprite_name(pipe, sprite), pipe_name(pipe));
20674eef
VS
1339 }
1340 } else if (INTEL_INFO(dev)->gen >= 7) {
649636ef 1341 u32 val = I915_READ(SPRCTL(pipe));
e2c719b7 1342 I915_STATE_WARN(val & SPRITE_ENABLE,
06da8da2 1343 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
20674eef
VS
1344 plane_name(pipe), pipe_name(pipe));
1345 } else if (INTEL_INFO(dev)->gen >= 5) {
649636ef 1346 u32 val = I915_READ(DVSCNTR(pipe));
e2c719b7 1347 I915_STATE_WARN(val & DVS_ENABLE,
06da8da2 1348 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
20674eef 1349 plane_name(pipe), pipe_name(pipe));
19332d7a
JB
1350 }
1351}
1352
08c71e5e
VS
1353static void assert_vblank_disabled(struct drm_crtc *crtc)
1354{
e2c719b7 1355 if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
08c71e5e
VS
1356 drm_crtc_vblank_put(crtc);
1357}
1358
7abd4b35
ACO
1359void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
1360 enum pipe pipe)
92f2584a 1361{
92f2584a
JB
1362 u32 val;
1363 bool enabled;
1364
649636ef 1365 val = I915_READ(PCH_TRANSCONF(pipe));
92f2584a 1366 enabled = !!(val & TRANS_ENABLE);
e2c719b7 1367 I915_STATE_WARN(enabled,
9db4a9c7
JB
1368 "transcoder assertion failed, should be off on pipe %c but is still active\n",
1369 pipe_name(pipe));
92f2584a
JB
1370}
1371
4e634389
KP
1372static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
1373 enum pipe pipe, u32 port_sel, u32 val)
f0575e92
KP
1374{
1375 if ((val & DP_PORT_EN) == 0)
1376 return false;
1377
2d1fe073 1378 if (HAS_PCH_CPT(dev_priv)) {
f0f59a00 1379 u32 trans_dp_ctl = I915_READ(TRANS_DP_CTL(pipe));
f0575e92
KP
1380 if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
1381 return false;
2d1fe073 1382 } else if (IS_CHERRYVIEW(dev_priv)) {
44f37d1f
CML
1383 if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
1384 return false;
f0575e92
KP
1385 } else {
1386 if ((val & DP_PIPE_MASK) != (pipe << 30))
1387 return false;
1388 }
1389 return true;
1390}
1391
1519b995
KP
1392static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
1393 enum pipe pipe, u32 val)
1394{
dc0fa718 1395 if ((val & SDVO_ENABLE) == 0)
1519b995
KP
1396 return false;
1397
2d1fe073 1398 if (HAS_PCH_CPT(dev_priv)) {
dc0fa718 1399 if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
1519b995 1400 return false;
2d1fe073 1401 } else if (IS_CHERRYVIEW(dev_priv)) {
44f37d1f
CML
1402 if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
1403 return false;
1519b995 1404 } else {
dc0fa718 1405 if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
1519b995
KP
1406 return false;
1407 }
1408 return true;
1409}
1410
1411static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
1412 enum pipe pipe, u32 val)
1413{
1414 if ((val & LVDS_PORT_EN) == 0)
1415 return false;
1416
2d1fe073 1417 if (HAS_PCH_CPT(dev_priv)) {
1519b995
KP
1418 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1419 return false;
1420 } else {
1421 if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
1422 return false;
1423 }
1424 return true;
1425}
1426
1427static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
1428 enum pipe pipe, u32 val)
1429{
1430 if ((val & ADPA_DAC_ENABLE) == 0)
1431 return false;
2d1fe073 1432 if (HAS_PCH_CPT(dev_priv)) {
1519b995
KP
1433 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1434 return false;
1435 } else {
1436 if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
1437 return false;
1438 }
1439 return true;
1440}
1441
291906f1 1442static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
f0f59a00
VS
1443 enum pipe pipe, i915_reg_t reg,
1444 u32 port_sel)
291906f1 1445{
47a05eca 1446 u32 val = I915_READ(reg);
e2c719b7 1447 I915_STATE_WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
291906f1 1448 "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
f0f59a00 1449 i915_mmio_reg_offset(reg), pipe_name(pipe));
de9a35ab 1450
2d1fe073 1451 I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && (val & DP_PORT_EN) == 0
75c5da27 1452 && (val & DP_PIPEB_SELECT),
de9a35ab 1453 "IBX PCH dp port still using transcoder B\n");
291906f1
JB
1454}
1455
1456static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
f0f59a00 1457 enum pipe pipe, i915_reg_t reg)
291906f1 1458{
47a05eca 1459 u32 val = I915_READ(reg);
e2c719b7 1460 I915_STATE_WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
23c99e77 1461 "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
f0f59a00 1462 i915_mmio_reg_offset(reg), pipe_name(pipe));
de9a35ab 1463
2d1fe073 1464 I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && (val & SDVO_ENABLE) == 0
75c5da27 1465 && (val & SDVO_PIPE_B_SELECT),
de9a35ab 1466 "IBX PCH hdmi port still using transcoder B\n");
291906f1
JB
1467}
1468
1469static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
1470 enum pipe pipe)
1471{
291906f1 1472 u32 val;
291906f1 1473
f0575e92
KP
1474 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
1475 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
1476 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
291906f1 1477
649636ef 1478 val = I915_READ(PCH_ADPA);
e2c719b7 1479 I915_STATE_WARN(adpa_pipe_enabled(dev_priv, pipe, val),
291906f1 1480 "PCH VGA enabled on transcoder %c, should be disabled\n",
9db4a9c7 1481 pipe_name(pipe));
291906f1 1482
649636ef 1483 val = I915_READ(PCH_LVDS);
e2c719b7 1484 I915_STATE_WARN(lvds_pipe_enabled(dev_priv, pipe, val),
291906f1 1485 "PCH LVDS enabled on transcoder %c, should be disabled\n",
9db4a9c7 1486 pipe_name(pipe));
291906f1 1487
e2debe91
PZ
1488 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
1489 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
1490 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
291906f1
JB
1491}
1492
cd2d34d9
VS
1493static void _vlv_enable_pll(struct intel_crtc *crtc,
1494 const struct intel_crtc_state *pipe_config)
1495{
1496 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1497 enum pipe pipe = crtc->pipe;
1498
1499 I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
1500 POSTING_READ(DPLL(pipe));
1501 udelay(150);
1502
2c30b43b
CW
1503 if (intel_wait_for_register(dev_priv,
1504 DPLL(pipe),
1505 DPLL_LOCK_VLV,
1506 DPLL_LOCK_VLV,
1507 1))
cd2d34d9
VS
1508 DRM_ERROR("DPLL %d failed to lock\n", pipe);
1509}
1510
d288f65f 1511static void vlv_enable_pll(struct intel_crtc *crtc,
5cec258b 1512 const struct intel_crtc_state *pipe_config)
87442f73 1513{
cd2d34d9 1514 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
8bd3f301 1515 enum pipe pipe = crtc->pipe;
87442f73 1516
8bd3f301 1517 assert_pipe_disabled(dev_priv, pipe);
87442f73 1518
87442f73 1519 /* PLL is protected by panel, make sure we can write it */
7d1a83cb 1520 assert_panel_unlocked(dev_priv, pipe);
87442f73 1521
cd2d34d9
VS
1522 if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
1523 _vlv_enable_pll(crtc, pipe_config);
426115cf 1524
8bd3f301
VS
1525 I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
1526 POSTING_READ(DPLL_MD(pipe));
87442f73
DV
1527}
1528
cd2d34d9
VS
1529
1530static void _chv_enable_pll(struct intel_crtc *crtc,
1531 const struct intel_crtc_state *pipe_config)
9d556c99 1532{
cd2d34d9 1533 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
8bd3f301 1534 enum pipe pipe = crtc->pipe;
9d556c99 1535 enum dpio_channel port = vlv_pipe_to_channel(pipe);
9d556c99
CML
1536 u32 tmp;
1537
a580516d 1538 mutex_lock(&dev_priv->sb_lock);
9d556c99
CML
1539
1540 /* Enable back the 10bit clock to display controller */
1541 tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1542 tmp |= DPIO_DCLKP_EN;
1543 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
1544
54433e91
VS
1545 mutex_unlock(&dev_priv->sb_lock);
1546
9d556c99
CML
1547 /*
1548 * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
1549 */
1550 udelay(1);
1551
1552 /* Enable PLL */
d288f65f 1553 I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
9d556c99
CML
1554
1555 /* Check PLL is locked */
6b18826a
CW
1556 if (intel_wait_for_register(dev_priv,
1557 DPLL(pipe), DPLL_LOCK_VLV, DPLL_LOCK_VLV,
1558 1))
9d556c99 1559 DRM_ERROR("PLL %d failed to lock\n", pipe);
cd2d34d9
VS
1560}
1561
1562static void chv_enable_pll(struct intel_crtc *crtc,
1563 const struct intel_crtc_state *pipe_config)
1564{
1565 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1566 enum pipe pipe = crtc->pipe;
1567
1568 assert_pipe_disabled(dev_priv, pipe);
1569
1570 /* PLL is protected by panel, make sure we can write it */
1571 assert_panel_unlocked(dev_priv, pipe);
1572
1573 if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
1574 _chv_enable_pll(crtc, pipe_config);
9d556c99 1575
c231775c
VS
1576 if (pipe != PIPE_A) {
1577 /*
1578 * WaPixelRepeatModeFixForC0:chv
1579 *
1580 * DPLLCMD is AWOL. Use chicken bits to propagate
1581 * the value from DPLLBMD to either pipe B or C.
1582 */
1583 I915_WRITE(CBR4_VLV, pipe == PIPE_B ? CBR_DPLLBMD_PIPE_B : CBR_DPLLBMD_PIPE_C);
1584 I915_WRITE(DPLL_MD(PIPE_B), pipe_config->dpll_hw_state.dpll_md);
1585 I915_WRITE(CBR4_VLV, 0);
1586 dev_priv->chv_dpll_md[pipe] = pipe_config->dpll_hw_state.dpll_md;
1587
1588 /*
1589 * DPLLB VGA mode also seems to cause problems.
1590 * We should always have it disabled.
1591 */
1592 WARN_ON((I915_READ(DPLL(PIPE_B)) & DPLL_VGA_MODE_DIS) == 0);
1593 } else {
1594 I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
1595 POSTING_READ(DPLL_MD(pipe));
1596 }
9d556c99
CML
1597}
1598
1c4e0274
VS
1599static int intel_num_dvo_pipes(struct drm_device *dev)
1600{
1601 struct intel_crtc *crtc;
1602 int count = 0;
1603
2d84d2b3 1604 for_each_intel_crtc(dev, crtc) {
3538b9df 1605 count += crtc->base.state->active &&
2d84d2b3
VS
1606 intel_crtc_has_type(crtc->config, INTEL_OUTPUT_DVO);
1607 }
1c4e0274
VS
1608
1609 return count;
1610}
1611
66e3d5c0 1612static void i9xx_enable_pll(struct intel_crtc *crtc)
63d7bbe9 1613{
66e3d5c0 1614 struct drm_device *dev = crtc->base.dev;
fac5e23e 1615 struct drm_i915_private *dev_priv = to_i915(dev);
f0f59a00 1616 i915_reg_t reg = DPLL(crtc->pipe);
6e3c9717 1617 u32 dpll = crtc->config->dpll_hw_state.dpll;
63d7bbe9 1618
66e3d5c0 1619 assert_pipe_disabled(dev_priv, crtc->pipe);
58c6eaa2 1620
63d7bbe9 1621 /* PLL is protected by panel, make sure we can write it */
66e3d5c0
DV
1622 if (IS_MOBILE(dev) && !IS_I830(dev))
1623 assert_panel_unlocked(dev_priv, crtc->pipe);
63d7bbe9 1624
1c4e0274
VS
1625 /* Enable DVO 2x clock on both PLLs if necessary */
1626 if (IS_I830(dev) && intel_num_dvo_pipes(dev) > 0) {
1627 /*
1628 * It appears to be important that we don't enable this
1629 * for the current pipe before otherwise configuring the
1630 * PLL. No idea how this should be handled if multiple
1631 * DVO outputs are enabled simultaneosly.
1632 */
1633 dpll |= DPLL_DVO_2X_MODE;
1634 I915_WRITE(DPLL(!crtc->pipe),
1635 I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
1636 }
66e3d5c0 1637
c2b63374
VS
1638 /*
1639 * Apparently we need to have VGA mode enabled prior to changing
1640 * the P1/P2 dividers. Otherwise the DPLL will keep using the old
1641 * dividers, even though the register value does change.
1642 */
1643 I915_WRITE(reg, 0);
1644
8e7a65aa
VS
1645 I915_WRITE(reg, dpll);
1646
66e3d5c0
DV
1647 /* Wait for the clocks to stabilize. */
1648 POSTING_READ(reg);
1649 udelay(150);
1650
1651 if (INTEL_INFO(dev)->gen >= 4) {
1652 I915_WRITE(DPLL_MD(crtc->pipe),
6e3c9717 1653 crtc->config->dpll_hw_state.dpll_md);
66e3d5c0
DV
1654 } else {
1655 /* The pixel multiplier can only be updated once the
1656 * DPLL is enabled and the clocks are stable.
1657 *
1658 * So write it again.
1659 */
1660 I915_WRITE(reg, dpll);
1661 }
63d7bbe9
JB
1662
1663 /* We do this three times for luck */
66e3d5c0 1664 I915_WRITE(reg, dpll);
63d7bbe9
JB
1665 POSTING_READ(reg);
1666 udelay(150); /* wait for warmup */
66e3d5c0 1667 I915_WRITE(reg, dpll);
63d7bbe9
JB
1668 POSTING_READ(reg);
1669 udelay(150); /* wait for warmup */
66e3d5c0 1670 I915_WRITE(reg, dpll);
63d7bbe9
JB
1671 POSTING_READ(reg);
1672 udelay(150); /* wait for warmup */
1673}
1674
1675/**
50b44a44 1676 * i9xx_disable_pll - disable a PLL
63d7bbe9
JB
1677 * @dev_priv: i915 private structure
1678 * @pipe: pipe PLL to disable
1679 *
1680 * Disable the PLL for @pipe, making sure the pipe is off first.
1681 *
1682 * Note! This is for pre-ILK only.
1683 */
1c4e0274 1684static void i9xx_disable_pll(struct intel_crtc *crtc)
63d7bbe9 1685{
1c4e0274 1686 struct drm_device *dev = crtc->base.dev;
fac5e23e 1687 struct drm_i915_private *dev_priv = to_i915(dev);
1c4e0274
VS
1688 enum pipe pipe = crtc->pipe;
1689
1690 /* Disable DVO 2x clock on both PLLs if necessary */
1691 if (IS_I830(dev) &&
2d84d2b3 1692 intel_crtc_has_type(crtc->config, INTEL_OUTPUT_DVO) &&
3538b9df 1693 !intel_num_dvo_pipes(dev)) {
1c4e0274
VS
1694 I915_WRITE(DPLL(PIPE_B),
1695 I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
1696 I915_WRITE(DPLL(PIPE_A),
1697 I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
1698 }
1699
b6b5d049
VS
1700 /* Don't disable pipe or pipe PLLs if needed */
1701 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1702 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
63d7bbe9
JB
1703 return;
1704
1705 /* Make sure the pipe isn't still relying on us */
1706 assert_pipe_disabled(dev_priv, pipe);
1707
b8afb911 1708 I915_WRITE(DPLL(pipe), DPLL_VGA_MODE_DIS);
50b44a44 1709 POSTING_READ(DPLL(pipe));
63d7bbe9
JB
1710}
1711
f6071166
JB
1712static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1713{
b8afb911 1714 u32 val;
f6071166
JB
1715
1716 /* Make sure the pipe isn't still relying on us */
1717 assert_pipe_disabled(dev_priv, pipe);
1718
03ed5cbf
VS
1719 val = DPLL_INTEGRATED_REF_CLK_VLV |
1720 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
1721 if (pipe != PIPE_A)
1722 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
1723
f6071166
JB
1724 I915_WRITE(DPLL(pipe), val);
1725 POSTING_READ(DPLL(pipe));
076ed3b2
CML
1726}
1727
1728static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1729{
d752048d 1730 enum dpio_channel port = vlv_pipe_to_channel(pipe);
076ed3b2
CML
1731 u32 val;
1732
a11b0703
VS
1733 /* Make sure the pipe isn't still relying on us */
1734 assert_pipe_disabled(dev_priv, pipe);
076ed3b2 1735
60bfe44f
VS
1736 val = DPLL_SSC_REF_CLK_CHV |
1737 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
a11b0703
VS
1738 if (pipe != PIPE_A)
1739 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
03ed5cbf 1740
a11b0703
VS
1741 I915_WRITE(DPLL(pipe), val);
1742 POSTING_READ(DPLL(pipe));
d752048d 1743
a580516d 1744 mutex_lock(&dev_priv->sb_lock);
d752048d
VS
1745
1746 /* Disable 10bit clock to display controller */
1747 val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1748 val &= ~DPIO_DCLKP_EN;
1749 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
1750
a580516d 1751 mutex_unlock(&dev_priv->sb_lock);
f6071166
JB
1752}
1753
e4607fcf 1754void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
9b6de0a1
VS
1755 struct intel_digital_port *dport,
1756 unsigned int expected_mask)
89b667f8
JB
1757{
1758 u32 port_mask;
f0f59a00 1759 i915_reg_t dpll_reg;
89b667f8 1760
e4607fcf
CML
1761 switch (dport->port) {
1762 case PORT_B:
89b667f8 1763 port_mask = DPLL_PORTB_READY_MASK;
00fc31b7 1764 dpll_reg = DPLL(0);
e4607fcf
CML
1765 break;
1766 case PORT_C:
89b667f8 1767 port_mask = DPLL_PORTC_READY_MASK;
00fc31b7 1768 dpll_reg = DPLL(0);
9b6de0a1 1769 expected_mask <<= 4;
00fc31b7
CML
1770 break;
1771 case PORT_D:
1772 port_mask = DPLL_PORTD_READY_MASK;
1773 dpll_reg = DPIO_PHY_STATUS;
e4607fcf
CML
1774 break;
1775 default:
1776 BUG();
1777 }
89b667f8 1778
370004d3
CW
1779 if (intel_wait_for_register(dev_priv,
1780 dpll_reg, port_mask, expected_mask,
1781 1000))
9b6de0a1
VS
1782 WARN(1, "timed out waiting for port %c ready: got 0x%x, expected 0x%x\n",
1783 port_name(dport->port), I915_READ(dpll_reg) & port_mask, expected_mask);
89b667f8
JB
1784}
1785
b8a4f404
PZ
1786static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1787 enum pipe pipe)
040484af 1788{
91c8a326 1789 struct drm_device *dev = &dev_priv->drm;
7c26e5c6 1790 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
e2b78267 1791 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
f0f59a00
VS
1792 i915_reg_t reg;
1793 uint32_t val, pipeconf_val;
040484af 1794
040484af 1795 /* Make sure PCH DPLL is enabled */
8106ddbd 1796 assert_shared_dpll_enabled(dev_priv, intel_crtc->config->shared_dpll);
040484af
JB
1797
1798 /* FDI must be feeding us bits for PCH ports */
1799 assert_fdi_tx_enabled(dev_priv, pipe);
1800 assert_fdi_rx_enabled(dev_priv, pipe);
1801
23670b32
DV
1802 if (HAS_PCH_CPT(dev)) {
1803 /* Workaround: Set the timing override bit before enabling the
1804 * pch transcoder. */
1805 reg = TRANS_CHICKEN2(pipe);
1806 val = I915_READ(reg);
1807 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
1808 I915_WRITE(reg, val);
59c859d6 1809 }
23670b32 1810
ab9412ba 1811 reg = PCH_TRANSCONF(pipe);
040484af 1812 val = I915_READ(reg);
5f7f726d 1813 pipeconf_val = I915_READ(PIPECONF(pipe));
e9bcff5c 1814
2d1fe073 1815 if (HAS_PCH_IBX(dev_priv)) {
e9bcff5c 1816 /*
c5de7c6f
VS
1817 * Make the BPC in transcoder be consistent with
1818 * that in pipeconf reg. For HDMI we must use 8bpc
1819 * here for both 8bpc and 12bpc.
e9bcff5c 1820 */
dfd07d72 1821 val &= ~PIPECONF_BPC_MASK;
2d84d2b3 1822 if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_HDMI))
c5de7c6f
VS
1823 val |= PIPECONF_8BPC;
1824 else
1825 val |= pipeconf_val & PIPECONF_BPC_MASK;
e9bcff5c 1826 }
5f7f726d
PZ
1827
1828 val &= ~TRANS_INTERLACE_MASK;
1829 if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
2d1fe073 1830 if (HAS_PCH_IBX(dev_priv) &&
2d84d2b3 1831 intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
7c26e5c6
PZ
1832 val |= TRANS_LEGACY_INTERLACED_ILK;
1833 else
1834 val |= TRANS_INTERLACED;
5f7f726d
PZ
1835 else
1836 val |= TRANS_PROGRESSIVE;
1837
040484af 1838 I915_WRITE(reg, val | TRANS_ENABLE);
650fbd84
CW
1839 if (intel_wait_for_register(dev_priv,
1840 reg, TRANS_STATE_ENABLE, TRANS_STATE_ENABLE,
1841 100))
4bb6f1f3 1842 DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
040484af
JB
1843}
1844
8fb033d7 1845static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
937bb610 1846 enum transcoder cpu_transcoder)
040484af 1847{
8fb033d7 1848 u32 val, pipeconf_val;
8fb033d7 1849
8fb033d7 1850 /* FDI must be feeding us bits for PCH ports */
1a240d4d 1851 assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
937bb610 1852 assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
8fb033d7 1853
223a6fdf 1854 /* Workaround: set timing override bit. */
36c0d0cf 1855 val = I915_READ(TRANS_CHICKEN2(PIPE_A));
23670b32 1856 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
36c0d0cf 1857 I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
223a6fdf 1858
25f3ef11 1859 val = TRANS_ENABLE;
937bb610 1860 pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
8fb033d7 1861
9a76b1c6
PZ
1862 if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
1863 PIPECONF_INTERLACED_ILK)
a35f2679 1864 val |= TRANS_INTERLACED;
8fb033d7
PZ
1865 else
1866 val |= TRANS_PROGRESSIVE;
1867
ab9412ba 1868 I915_WRITE(LPT_TRANSCONF, val);
d9f96244
CW
1869 if (intel_wait_for_register(dev_priv,
1870 LPT_TRANSCONF,
1871 TRANS_STATE_ENABLE,
1872 TRANS_STATE_ENABLE,
1873 100))
937bb610 1874 DRM_ERROR("Failed to enable PCH transcoder\n");
8fb033d7
PZ
1875}
1876
b8a4f404
PZ
1877static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
1878 enum pipe pipe)
040484af 1879{
91c8a326 1880 struct drm_device *dev = &dev_priv->drm;
f0f59a00
VS
1881 i915_reg_t reg;
1882 uint32_t val;
040484af
JB
1883
1884 /* FDI relies on the transcoder */
1885 assert_fdi_tx_disabled(dev_priv, pipe);
1886 assert_fdi_rx_disabled(dev_priv, pipe);
1887
291906f1
JB
1888 /* Ports must be off as well */
1889 assert_pch_ports_disabled(dev_priv, pipe);
1890
ab9412ba 1891 reg = PCH_TRANSCONF(pipe);
040484af
JB
1892 val = I915_READ(reg);
1893 val &= ~TRANS_ENABLE;
1894 I915_WRITE(reg, val);
1895 /* wait for PCH transcoder off, transcoder state */
a7d04662
CW
1896 if (intel_wait_for_register(dev_priv,
1897 reg, TRANS_STATE_ENABLE, 0,
1898 50))
4bb6f1f3 1899 DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
23670b32 1900
c465613b 1901 if (HAS_PCH_CPT(dev)) {
23670b32
DV
1902 /* Workaround: Clear the timing override chicken bit again. */
1903 reg = TRANS_CHICKEN2(pipe);
1904 val = I915_READ(reg);
1905 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
1906 I915_WRITE(reg, val);
1907 }
040484af
JB
1908}
1909
ab4d966c 1910static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
8fb033d7 1911{
8fb033d7
PZ
1912 u32 val;
1913
ab9412ba 1914 val = I915_READ(LPT_TRANSCONF);
8fb033d7 1915 val &= ~TRANS_ENABLE;
ab9412ba 1916 I915_WRITE(LPT_TRANSCONF, val);
8fb033d7 1917 /* wait for PCH transcoder off, transcoder state */
dfdb4749
CW
1918 if (intel_wait_for_register(dev_priv,
1919 LPT_TRANSCONF, TRANS_STATE_ENABLE, 0,
1920 50))
8a52fd9f 1921 DRM_ERROR("Failed to disable PCH transcoder\n");
223a6fdf
PZ
1922
1923 /* Workaround: clear timing override bit. */
36c0d0cf 1924 val = I915_READ(TRANS_CHICKEN2(PIPE_A));
23670b32 1925 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
36c0d0cf 1926 I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
040484af
JB
1927}
1928
b24e7179 1929/**
309cfea8 1930 * intel_enable_pipe - enable a pipe, asserting requirements
0372264a 1931 * @crtc: crtc responsible for the pipe
b24e7179 1932 *
0372264a 1933 * Enable @crtc's pipe, making sure that various hardware specific requirements
b24e7179 1934 * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
b24e7179 1935 */
e1fdc473 1936static void intel_enable_pipe(struct intel_crtc *crtc)
b24e7179 1937{
0372264a 1938 struct drm_device *dev = crtc->base.dev;
fac5e23e 1939 struct drm_i915_private *dev_priv = to_i915(dev);
0372264a 1940 enum pipe pipe = crtc->pipe;
1a70a728 1941 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
1a240d4d 1942 enum pipe pch_transcoder;
f0f59a00 1943 i915_reg_t reg;
b24e7179
JB
1944 u32 val;
1945
9e2ee2dd
VS
1946 DRM_DEBUG_KMS("enabling pipe %c\n", pipe_name(pipe));
1947
58c6eaa2 1948 assert_planes_disabled(dev_priv, pipe);
93ce0ba6 1949 assert_cursor_disabled(dev_priv, pipe);
58c6eaa2
DV
1950 assert_sprites_disabled(dev_priv, pipe);
1951
2d1fe073 1952 if (HAS_PCH_LPT(dev_priv))
cc391bbb
PZ
1953 pch_transcoder = TRANSCODER_A;
1954 else
1955 pch_transcoder = pipe;
1956
b24e7179
JB
1957 /*
1958 * A pipe without a PLL won't actually be able to drive bits from
1959 * a plane. On ILK+ the pipe PLLs are integrated, so we don't
1960 * need the check.
1961 */
2d1fe073 1962 if (HAS_GMCH_DISPLAY(dev_priv))
d7edc4e5 1963 if (intel_crtc_has_type(crtc->config, INTEL_OUTPUT_DSI))
23538ef1
JN
1964 assert_dsi_pll_enabled(dev_priv);
1965 else
1966 assert_pll_enabled(dev_priv, pipe);
040484af 1967 else {
6e3c9717 1968 if (crtc->config->has_pch_encoder) {
040484af 1969 /* if driving the PCH, we need FDI enabled */
cc391bbb 1970 assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
1a240d4d
DV
1971 assert_fdi_tx_pll_enabled(dev_priv,
1972 (enum pipe) cpu_transcoder);
040484af
JB
1973 }
1974 /* FIXME: assert CPU port conditions for SNB+ */
1975 }
b24e7179 1976
702e7a56 1977 reg = PIPECONF(cpu_transcoder);
b24e7179 1978 val = I915_READ(reg);
7ad25d48 1979 if (val & PIPECONF_ENABLE) {
b6b5d049
VS
1980 WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1981 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
00d70b15 1982 return;
7ad25d48 1983 }
00d70b15
CW
1984
1985 I915_WRITE(reg, val | PIPECONF_ENABLE);
851855d8 1986 POSTING_READ(reg);
b7792d8b
VS
1987
1988 /*
1989 * Until the pipe starts DSL will read as 0, which would cause
1990 * an apparent vblank timestamp jump, which messes up also the
1991 * frame count when it's derived from the timestamps. So let's
1992 * wait for the pipe to start properly before we call
1993 * drm_crtc_vblank_on()
1994 */
1995 if (dev->max_vblank_count == 0 &&
1996 wait_for(intel_get_crtc_scanline(crtc) != crtc->scanline_offset, 50))
1997 DRM_ERROR("pipe %c didn't start\n", pipe_name(pipe));
b24e7179
JB
1998}
1999
2000/**
309cfea8 2001 * intel_disable_pipe - disable a pipe, asserting requirements
575f7ab7 2002 * @crtc: crtc whose pipes is to be disabled
b24e7179 2003 *
575f7ab7
VS
2004 * Disable the pipe of @crtc, making sure that various hardware
2005 * specific requirements are met, if applicable, e.g. plane
2006 * disabled, panel fitter off, etc.
b24e7179
JB
2007 *
2008 * Will wait until the pipe has shut down before returning.
2009 */
575f7ab7 2010static void intel_disable_pipe(struct intel_crtc *crtc)
b24e7179 2011{
fac5e23e 2012 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6e3c9717 2013 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
575f7ab7 2014 enum pipe pipe = crtc->pipe;
f0f59a00 2015 i915_reg_t reg;
b24e7179
JB
2016 u32 val;
2017
9e2ee2dd
VS
2018 DRM_DEBUG_KMS("disabling pipe %c\n", pipe_name(pipe));
2019
b24e7179
JB
2020 /*
2021 * Make sure planes won't keep trying to pump pixels to us,
2022 * or we might hang the display.
2023 */
2024 assert_planes_disabled(dev_priv, pipe);
93ce0ba6 2025 assert_cursor_disabled(dev_priv, pipe);
19332d7a 2026 assert_sprites_disabled(dev_priv, pipe);
b24e7179 2027
702e7a56 2028 reg = PIPECONF(cpu_transcoder);
b24e7179 2029 val = I915_READ(reg);
00d70b15
CW
2030 if ((val & PIPECONF_ENABLE) == 0)
2031 return;
2032
67adc644
VS
2033 /*
2034 * Double wide has implications for planes
2035 * so best keep it disabled when not needed.
2036 */
6e3c9717 2037 if (crtc->config->double_wide)
67adc644
VS
2038 val &= ~PIPECONF_DOUBLE_WIDE;
2039
2040 /* Don't disable pipe or pipe PLLs if needed */
b6b5d049
VS
2041 if (!(pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) &&
2042 !(pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
67adc644
VS
2043 val &= ~PIPECONF_ENABLE;
2044
2045 I915_WRITE(reg, val);
2046 if ((val & PIPECONF_ENABLE) == 0)
2047 intel_wait_for_pipe_off(crtc);
b24e7179
JB
2048}
2049
832be82f
VS
2050static unsigned int intel_tile_size(const struct drm_i915_private *dev_priv)
2051{
2052 return IS_GEN2(dev_priv) ? 2048 : 4096;
2053}
2054
27ba3910
VS
2055static unsigned int intel_tile_width_bytes(const struct drm_i915_private *dev_priv,
2056 uint64_t fb_modifier, unsigned int cpp)
7b49f948
VS
2057{
2058 switch (fb_modifier) {
2059 case DRM_FORMAT_MOD_NONE:
2060 return cpp;
2061 case I915_FORMAT_MOD_X_TILED:
2062 if (IS_GEN2(dev_priv))
2063 return 128;
2064 else
2065 return 512;
2066 case I915_FORMAT_MOD_Y_TILED:
2067 if (IS_GEN2(dev_priv) || HAS_128_BYTE_Y_TILING(dev_priv))
2068 return 128;
2069 else
2070 return 512;
2071 case I915_FORMAT_MOD_Yf_TILED:
2072 switch (cpp) {
2073 case 1:
2074 return 64;
2075 case 2:
2076 case 4:
2077 return 128;
2078 case 8:
2079 case 16:
2080 return 256;
2081 default:
2082 MISSING_CASE(cpp);
2083 return cpp;
2084 }
2085 break;
2086 default:
2087 MISSING_CASE(fb_modifier);
2088 return cpp;
2089 }
2090}
2091
832be82f
VS
2092unsigned int intel_tile_height(const struct drm_i915_private *dev_priv,
2093 uint64_t fb_modifier, unsigned int cpp)
a57ce0b2 2094{
832be82f
VS
2095 if (fb_modifier == DRM_FORMAT_MOD_NONE)
2096 return 1;
2097 else
2098 return intel_tile_size(dev_priv) /
27ba3910 2099 intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
6761dd31
TU
2100}
2101
8d0deca8
VS
2102/* Return the tile dimensions in pixel units */
2103static void intel_tile_dims(const struct drm_i915_private *dev_priv,
2104 unsigned int *tile_width,
2105 unsigned int *tile_height,
2106 uint64_t fb_modifier,
2107 unsigned int cpp)
2108{
2109 unsigned int tile_width_bytes =
2110 intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
2111
2112 *tile_width = tile_width_bytes / cpp;
2113 *tile_height = intel_tile_size(dev_priv) / tile_width_bytes;
2114}
2115
6761dd31
TU
2116unsigned int
2117intel_fb_align_height(struct drm_device *dev, unsigned int height,
832be82f 2118 uint32_t pixel_format, uint64_t fb_modifier)
6761dd31 2119{
832be82f
VS
2120 unsigned int cpp = drm_format_plane_cpp(pixel_format, 0);
2121 unsigned int tile_height = intel_tile_height(to_i915(dev), fb_modifier, cpp);
2122
2123 return ALIGN(height, tile_height);
a57ce0b2
JB
2124}
2125
1663b9d6
VS
2126unsigned int intel_rotation_info_size(const struct intel_rotation_info *rot_info)
2127{
2128 unsigned int size = 0;
2129 int i;
2130
2131 for (i = 0 ; i < ARRAY_SIZE(rot_info->plane); i++)
2132 size += rot_info->plane[i].width * rot_info->plane[i].height;
2133
2134 return size;
2135}
2136
75c82a53 2137static void
3465c580
VS
2138intel_fill_fb_ggtt_view(struct i915_ggtt_view *view,
2139 const struct drm_framebuffer *fb,
2140 unsigned int rotation)
f64b98cd 2141{
2d7a215f
VS
2142 if (intel_rotation_90_or_270(rotation)) {
2143 *view = i915_ggtt_view_rotated;
2144 view->params.rotated = to_intel_framebuffer(fb)->rot_info;
2145 } else {
2146 *view = i915_ggtt_view_normal;
2147 }
2148}
50470bb0 2149
2d7a215f
VS
2150static void
2151intel_fill_fb_info(struct drm_i915_private *dev_priv,
2152 struct drm_framebuffer *fb)
2153{
2154 struct intel_rotation_info *info = &to_intel_framebuffer(fb)->rot_info;
2155 unsigned int tile_size, tile_width, tile_height, cpp;
50470bb0 2156
d9b3288e
VS
2157 tile_size = intel_tile_size(dev_priv);
2158
2159 cpp = drm_format_plane_cpp(fb->pixel_format, 0);
8d0deca8
VS
2160 intel_tile_dims(dev_priv, &tile_width, &tile_height,
2161 fb->modifier[0], cpp);
d9b3288e 2162
1663b9d6
VS
2163 info->plane[0].width = DIV_ROUND_UP(fb->pitches[0], tile_width * cpp);
2164 info->plane[0].height = DIV_ROUND_UP(fb->height, tile_height);
84fe03f7 2165
89e3e142 2166 if (info->pixel_format == DRM_FORMAT_NV12) {
832be82f 2167 cpp = drm_format_plane_cpp(fb->pixel_format, 1);
8d0deca8
VS
2168 intel_tile_dims(dev_priv, &tile_width, &tile_height,
2169 fb->modifier[1], cpp);
d9b3288e 2170
2d7a215f 2171 info->uv_offset = fb->offsets[1];
1663b9d6
VS
2172 info->plane[1].width = DIV_ROUND_UP(fb->pitches[1], tile_width * cpp);
2173 info->plane[1].height = DIV_ROUND_UP(fb->height / 2, tile_height);
89e3e142 2174 }
f64b98cd
TU
2175}
2176
603525d7 2177static unsigned int intel_linear_alignment(const struct drm_i915_private *dev_priv)
4e9a86b6
VS
2178{
2179 if (INTEL_INFO(dev_priv)->gen >= 9)
2180 return 256 * 1024;
985b8bb4 2181 else if (IS_BROADWATER(dev_priv) || IS_CRESTLINE(dev_priv) ||
666a4537 2182 IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
4e9a86b6
VS
2183 return 128 * 1024;
2184 else if (INTEL_INFO(dev_priv)->gen >= 4)
2185 return 4 * 1024;
2186 else
44c5905e 2187 return 0;
4e9a86b6
VS
2188}
2189
603525d7
VS
2190static unsigned int intel_surf_alignment(const struct drm_i915_private *dev_priv,
2191 uint64_t fb_modifier)
2192{
2193 switch (fb_modifier) {
2194 case DRM_FORMAT_MOD_NONE:
2195 return intel_linear_alignment(dev_priv);
2196 case I915_FORMAT_MOD_X_TILED:
2197 if (INTEL_INFO(dev_priv)->gen >= 9)
2198 return 256 * 1024;
2199 return 0;
2200 case I915_FORMAT_MOD_Y_TILED:
2201 case I915_FORMAT_MOD_Yf_TILED:
2202 return 1 * 1024 * 1024;
2203 default:
2204 MISSING_CASE(fb_modifier);
2205 return 0;
2206 }
2207}
2208
127bd2ac 2209int
3465c580
VS
2210intel_pin_and_fence_fb_obj(struct drm_framebuffer *fb,
2211 unsigned int rotation)
6b95a207 2212{
850c4cdc 2213 struct drm_device *dev = fb->dev;
fac5e23e 2214 struct drm_i915_private *dev_priv = to_i915(dev);
850c4cdc 2215 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
f64b98cd 2216 struct i915_ggtt_view view;
6b95a207
KH
2217 u32 alignment;
2218 int ret;
2219
ebcdd39e
MR
2220 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
2221
603525d7 2222 alignment = intel_surf_alignment(dev_priv, fb->modifier[0]);
6b95a207 2223
3465c580 2224 intel_fill_fb_ggtt_view(&view, fb, rotation);
f64b98cd 2225
693db184
CW
2226 /* Note that the w/a also requires 64 PTE of padding following the
2227 * bo. We currently fill all unused PTE with the shadow page and so
2228 * we should always have valid PTE following the scanout preventing
2229 * the VT-d warning.
2230 */
48f112fe 2231 if (intel_scanout_needs_vtd_wa(dev_priv) && alignment < 256 * 1024)
693db184
CW
2232 alignment = 256 * 1024;
2233
d6dd6843
PZ
2234 /*
2235 * Global gtt pte registers are special registers which actually forward
2236 * writes to a chunk of system memory. Which means that there is no risk
2237 * that the register values disappear as soon as we call
2238 * intel_runtime_pm_put(), so it is correct to wrap only the
2239 * pin/unpin/fence and not more.
2240 */
2241 intel_runtime_pm_get(dev_priv);
2242
7580d774
ML
2243 ret = i915_gem_object_pin_to_display_plane(obj, alignment,
2244 &view);
48b956c5 2245 if (ret)
b26a6b35 2246 goto err_pm;
6b95a207
KH
2247
2248 /* Install a fence for tiled scan-out. Pre-i965 always needs a
2249 * fence, whereas 965+ only requires a fence if using
2250 * framebuffer compression. For simplicity, we always install
2251 * a fence as the cost is not that onerous.
2252 */
9807216f
VK
2253 if (view.type == I915_GGTT_VIEW_NORMAL) {
2254 ret = i915_gem_object_get_fence(obj);
2255 if (ret == -EDEADLK) {
2256 /*
2257 * -EDEADLK means there are no free fences
2258 * no pending flips.
2259 *
2260 * This is propagated to atomic, but it uses
2261 * -EDEADLK to force a locking recovery, so
2262 * change the returned error to -EBUSY.
2263 */
2264 ret = -EBUSY;
2265 goto err_unpin;
2266 } else if (ret)
2267 goto err_unpin;
1690e1eb 2268
9807216f
VK
2269 i915_gem_object_pin_fence(obj);
2270 }
6b95a207 2271
d6dd6843 2272 intel_runtime_pm_put(dev_priv);
6b95a207 2273 return 0;
48b956c5
CW
2274
2275err_unpin:
f64b98cd 2276 i915_gem_object_unpin_from_display_plane(obj, &view);
b26a6b35 2277err_pm:
d6dd6843 2278 intel_runtime_pm_put(dev_priv);
48b956c5 2279 return ret;
6b95a207
KH
2280}
2281
fb4b8ce1 2282void intel_unpin_fb_obj(struct drm_framebuffer *fb, unsigned int rotation)
1690e1eb 2283{
82bc3b2d 2284 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
f64b98cd 2285 struct i915_ggtt_view view;
82bc3b2d 2286
ebcdd39e
MR
2287 WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
2288
3465c580 2289 intel_fill_fb_ggtt_view(&view, fb, rotation);
f64b98cd 2290
9807216f
VK
2291 if (view.type == I915_GGTT_VIEW_NORMAL)
2292 i915_gem_object_unpin_fence(obj);
2293
f64b98cd 2294 i915_gem_object_unpin_from_display_plane(obj, &view);
1690e1eb
CW
2295}
2296
29cf9491
VS
2297/*
2298 * Adjust the tile offset by moving the difference into
2299 * the x/y offsets.
2300 *
2301 * Input tile dimensions and pitch must already be
2302 * rotated to match x and y, and in pixel units.
2303 */
2304static u32 intel_adjust_tile_offset(int *x, int *y,
2305 unsigned int tile_width,
2306 unsigned int tile_height,
2307 unsigned int tile_size,
2308 unsigned int pitch_tiles,
2309 u32 old_offset,
2310 u32 new_offset)
2311{
2312 unsigned int tiles;
2313
2314 WARN_ON(old_offset & (tile_size - 1));
2315 WARN_ON(new_offset & (tile_size - 1));
2316 WARN_ON(new_offset > old_offset);
2317
2318 tiles = (old_offset - new_offset) / tile_size;
2319
2320 *y += tiles / pitch_tiles * tile_height;
2321 *x += tiles % pitch_tiles * tile_width;
2322
2323 return new_offset;
2324}
2325
8d0deca8
VS
2326/*
2327 * Computes the linear offset to the base tile and adjusts
2328 * x, y. bytes per pixel is assumed to be a power-of-two.
2329 *
2330 * In the 90/270 rotated case, x and y are assumed
2331 * to be already rotated to match the rotated GTT view, and
2332 * pitch is the tile_height aligned framebuffer height.
2333 */
4f2d9934
VS
2334u32 intel_compute_tile_offset(int *x, int *y,
2335 const struct drm_framebuffer *fb, int plane,
8d0deca8
VS
2336 unsigned int pitch,
2337 unsigned int rotation)
c2c75131 2338{
4f2d9934
VS
2339 const struct drm_i915_private *dev_priv = to_i915(fb->dev);
2340 uint64_t fb_modifier = fb->modifier[plane];
2341 unsigned int cpp = drm_format_plane_cpp(fb->pixel_format, plane);
29cf9491
VS
2342 u32 offset, offset_aligned, alignment;
2343
2344 alignment = intel_surf_alignment(dev_priv, fb_modifier);
2345 if (alignment)
2346 alignment--;
2347
b5c65338 2348 if (fb_modifier != DRM_FORMAT_MOD_NONE) {
8d0deca8
VS
2349 unsigned int tile_size, tile_width, tile_height;
2350 unsigned int tile_rows, tiles, pitch_tiles;
c2c75131 2351
d843310d 2352 tile_size = intel_tile_size(dev_priv);
8d0deca8
VS
2353 intel_tile_dims(dev_priv, &tile_width, &tile_height,
2354 fb_modifier, cpp);
2355
2356 if (intel_rotation_90_or_270(rotation)) {
2357 pitch_tiles = pitch / tile_height;
2358 swap(tile_width, tile_height);
2359 } else {
2360 pitch_tiles = pitch / (tile_width * cpp);
2361 }
d843310d
VS
2362
2363 tile_rows = *y / tile_height;
2364 *y %= tile_height;
c2c75131 2365
8d0deca8
VS
2366 tiles = *x / tile_width;
2367 *x %= tile_width;
bc752862 2368
29cf9491
VS
2369 offset = (tile_rows * pitch_tiles + tiles) * tile_size;
2370 offset_aligned = offset & ~alignment;
bc752862 2371
29cf9491
VS
2372 intel_adjust_tile_offset(x, y, tile_width, tile_height,
2373 tile_size, pitch_tiles,
2374 offset, offset_aligned);
2375 } else {
bc752862 2376 offset = *y * pitch + *x * cpp;
29cf9491
VS
2377 offset_aligned = offset & ~alignment;
2378
4e9a86b6
VS
2379 *y = (offset & alignment) / pitch;
2380 *x = ((offset & alignment) - *y * pitch) / cpp;
bc752862 2381 }
29cf9491
VS
2382
2383 return offset_aligned;
c2c75131
DV
2384}
2385
b35d63fa 2386static int i9xx_format_to_fourcc(int format)
46f297fb
JB
2387{
2388 switch (format) {
2389 case DISPPLANE_8BPP:
2390 return DRM_FORMAT_C8;
2391 case DISPPLANE_BGRX555:
2392 return DRM_FORMAT_XRGB1555;
2393 case DISPPLANE_BGRX565:
2394 return DRM_FORMAT_RGB565;
2395 default:
2396 case DISPPLANE_BGRX888:
2397 return DRM_FORMAT_XRGB8888;
2398 case DISPPLANE_RGBX888:
2399 return DRM_FORMAT_XBGR8888;
2400 case DISPPLANE_BGRX101010:
2401 return DRM_FORMAT_XRGB2101010;
2402 case DISPPLANE_RGBX101010:
2403 return DRM_FORMAT_XBGR2101010;
2404 }
2405}
2406
bc8d7dff
DL
2407static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
2408{
2409 switch (format) {
2410 case PLANE_CTL_FORMAT_RGB_565:
2411 return DRM_FORMAT_RGB565;
2412 default:
2413 case PLANE_CTL_FORMAT_XRGB_8888:
2414 if (rgb_order) {
2415 if (alpha)
2416 return DRM_FORMAT_ABGR8888;
2417 else
2418 return DRM_FORMAT_XBGR8888;
2419 } else {
2420 if (alpha)
2421 return DRM_FORMAT_ARGB8888;
2422 else
2423 return DRM_FORMAT_XRGB8888;
2424 }
2425 case PLANE_CTL_FORMAT_XRGB_2101010:
2426 if (rgb_order)
2427 return DRM_FORMAT_XBGR2101010;
2428 else
2429 return DRM_FORMAT_XRGB2101010;
2430 }
2431}
2432
5724dbd1 2433static bool
f6936e29
DV
2434intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
2435 struct intel_initial_plane_config *plane_config)
46f297fb
JB
2436{
2437 struct drm_device *dev = crtc->base.dev;
3badb49f 2438 struct drm_i915_private *dev_priv = to_i915(dev);
72e96d64 2439 struct i915_ggtt *ggtt = &dev_priv->ggtt;
46f297fb
JB
2440 struct drm_i915_gem_object *obj = NULL;
2441 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
2d14030b 2442 struct drm_framebuffer *fb = &plane_config->fb->base;
f37b5c2b
DV
2443 u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
2444 u32 size_aligned = round_up(plane_config->base + plane_config->size,
2445 PAGE_SIZE);
2446
2447 size_aligned -= base_aligned;
46f297fb 2448
ff2652ea
CW
2449 if (plane_config->size == 0)
2450 return false;
2451
3badb49f
PZ
2452 /* If the FB is too big, just don't use it since fbdev is not very
2453 * important and we should probably use that space with FBC or other
2454 * features. */
72e96d64 2455 if (size_aligned * 2 > ggtt->stolen_usable_size)
3badb49f
PZ
2456 return false;
2457
12c83d99
TU
2458 mutex_lock(&dev->struct_mutex);
2459
f37b5c2b
DV
2460 obj = i915_gem_object_create_stolen_for_preallocated(dev,
2461 base_aligned,
2462 base_aligned,
2463 size_aligned);
12c83d99
TU
2464 if (!obj) {
2465 mutex_unlock(&dev->struct_mutex);
484b41dd 2466 return false;
12c83d99 2467 }
46f297fb 2468
49af449b
DL
2469 obj->tiling_mode = plane_config->tiling;
2470 if (obj->tiling_mode == I915_TILING_X)
6bf129df 2471 obj->stride = fb->pitches[0];
46f297fb 2472
6bf129df
DL
2473 mode_cmd.pixel_format = fb->pixel_format;
2474 mode_cmd.width = fb->width;
2475 mode_cmd.height = fb->height;
2476 mode_cmd.pitches[0] = fb->pitches[0];
18c5247e
DV
2477 mode_cmd.modifier[0] = fb->modifier[0];
2478 mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
46f297fb 2479
6bf129df 2480 if (intel_framebuffer_init(dev, to_intel_framebuffer(fb),
484b41dd 2481 &mode_cmd, obj)) {
46f297fb
JB
2482 DRM_DEBUG_KMS("intel fb init failed\n");
2483 goto out_unref_obj;
2484 }
12c83d99 2485
46f297fb 2486 mutex_unlock(&dev->struct_mutex);
484b41dd 2487
f6936e29 2488 DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
484b41dd 2489 return true;
46f297fb
JB
2490
2491out_unref_obj:
f8c417cd 2492 i915_gem_object_put(obj);
46f297fb 2493 mutex_unlock(&dev->struct_mutex);
484b41dd
JB
2494 return false;
2495}
2496
5a21b665
DV
2497/* Update plane->state->fb to match plane->fb after driver-internal updates */
2498static void
2499update_state_fb(struct drm_plane *plane)
2500{
2501 if (plane->fb == plane->state->fb)
2502 return;
2503
2504 if (plane->state->fb)
2505 drm_framebuffer_unreference(plane->state->fb);
2506 plane->state->fb = plane->fb;
2507 if (plane->state->fb)
2508 drm_framebuffer_reference(plane->state->fb);
2509}
2510
5724dbd1 2511static void
f6936e29
DV
2512intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
2513 struct intel_initial_plane_config *plane_config)
484b41dd
JB
2514{
2515 struct drm_device *dev = intel_crtc->base.dev;
fac5e23e 2516 struct drm_i915_private *dev_priv = to_i915(dev);
484b41dd
JB
2517 struct drm_crtc *c;
2518 struct intel_crtc *i;
2ff8fde1 2519 struct drm_i915_gem_object *obj;
88595ac9 2520 struct drm_plane *primary = intel_crtc->base.primary;
be5651f2 2521 struct drm_plane_state *plane_state = primary->state;
200757f5
MR
2522 struct drm_crtc_state *crtc_state = intel_crtc->base.state;
2523 struct intel_plane *intel_plane = to_intel_plane(primary);
0a8d8a86
MR
2524 struct intel_plane_state *intel_state =
2525 to_intel_plane_state(plane_state);
88595ac9 2526 struct drm_framebuffer *fb;
484b41dd 2527
2d14030b 2528 if (!plane_config->fb)
484b41dd
JB
2529 return;
2530
f6936e29 2531 if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
88595ac9
DV
2532 fb = &plane_config->fb->base;
2533 goto valid_fb;
f55548b5 2534 }
484b41dd 2535
2d14030b 2536 kfree(plane_config->fb);
484b41dd
JB
2537
2538 /*
2539 * Failed to alloc the obj, check to see if we should share
2540 * an fb with another CRTC instead
2541 */
70e1e0ec 2542 for_each_crtc(dev, c) {
484b41dd
JB
2543 i = to_intel_crtc(c);
2544
2545 if (c == &intel_crtc->base)
2546 continue;
2547
2ff8fde1
MR
2548 if (!i->active)
2549 continue;
2550
88595ac9
DV
2551 fb = c->primary->fb;
2552 if (!fb)
484b41dd
JB
2553 continue;
2554
88595ac9 2555 obj = intel_fb_obj(fb);
2ff8fde1 2556 if (i915_gem_obj_ggtt_offset(obj) == plane_config->base) {
88595ac9
DV
2557 drm_framebuffer_reference(fb);
2558 goto valid_fb;
484b41dd
JB
2559 }
2560 }
88595ac9 2561
200757f5
MR
2562 /*
2563 * We've failed to reconstruct the BIOS FB. Current display state
2564 * indicates that the primary plane is visible, but has a NULL FB,
2565 * which will lead to problems later if we don't fix it up. The
2566 * simplest solution is to just disable the primary plane now and
2567 * pretend the BIOS never had it enabled.
2568 */
2569 to_intel_plane_state(plane_state)->visible = false;
2570 crtc_state->plane_mask &= ~(1 << drm_plane_index(primary));
2622a081 2571 intel_pre_disable_primary_noatomic(&intel_crtc->base);
200757f5
MR
2572 intel_plane->disable_plane(primary, &intel_crtc->base);
2573
88595ac9
DV
2574 return;
2575
2576valid_fb:
f44e2659
VS
2577 plane_state->src_x = 0;
2578 plane_state->src_y = 0;
be5651f2
ML
2579 plane_state->src_w = fb->width << 16;
2580 plane_state->src_h = fb->height << 16;
2581
f44e2659
VS
2582 plane_state->crtc_x = 0;
2583 plane_state->crtc_y = 0;
be5651f2
ML
2584 plane_state->crtc_w = fb->width;
2585 plane_state->crtc_h = fb->height;
2586
0a8d8a86
MR
2587 intel_state->src.x1 = plane_state->src_x;
2588 intel_state->src.y1 = plane_state->src_y;
2589 intel_state->src.x2 = plane_state->src_x + plane_state->src_w;
2590 intel_state->src.y2 = plane_state->src_y + plane_state->src_h;
2591 intel_state->dst.x1 = plane_state->crtc_x;
2592 intel_state->dst.y1 = plane_state->crtc_y;
2593 intel_state->dst.x2 = plane_state->crtc_x + plane_state->crtc_w;
2594 intel_state->dst.y2 = plane_state->crtc_y + plane_state->crtc_h;
2595
88595ac9
DV
2596 obj = intel_fb_obj(fb);
2597 if (obj->tiling_mode != I915_TILING_NONE)
2598 dev_priv->preserve_bios_swizzle = true;
2599
be5651f2
ML
2600 drm_framebuffer_reference(fb);
2601 primary->fb = primary->state->fb = fb;
36750f28 2602 primary->crtc = primary->state->crtc = &intel_crtc->base;
36750f28 2603 intel_crtc->base.state->plane_mask |= (1 << drm_plane_index(primary));
faf5bf0a
CW
2604 atomic_or(to_intel_plane(primary)->frontbuffer_bit,
2605 &obj->frontbuffer_bits);
46f297fb
JB
2606}
2607
a8d201af
ML
2608static void i9xx_update_primary_plane(struct drm_plane *primary,
2609 const struct intel_crtc_state *crtc_state,
2610 const struct intel_plane_state *plane_state)
81255565 2611{
a8d201af 2612 struct drm_device *dev = primary->dev;
fac5e23e 2613 struct drm_i915_private *dev_priv = to_i915(dev);
a8d201af
ML
2614 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
2615 struct drm_framebuffer *fb = plane_state->base.fb;
2616 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
81255565 2617 int plane = intel_crtc->plane;
54ea9da8 2618 u32 linear_offset;
81255565 2619 u32 dspcntr;
f0f59a00 2620 i915_reg_t reg = DSPCNTR(plane);
8d0deca8 2621 unsigned int rotation = plane_state->base.rotation;
ac484963 2622 int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
54ea9da8
VS
2623 int x = plane_state->src.x1 >> 16;
2624 int y = plane_state->src.y1 >> 16;
c9ba6fad 2625
f45651ba
VS
2626 dspcntr = DISPPLANE_GAMMA_ENABLE;
2627
fdd508a6 2628 dspcntr |= DISPLAY_PLANE_ENABLE;
f45651ba
VS
2629
2630 if (INTEL_INFO(dev)->gen < 4) {
2631 if (intel_crtc->pipe == PIPE_B)
2632 dspcntr |= DISPPLANE_SEL_PIPE_B;
2633
2634 /* pipesrc and dspsize control the size that is scaled from,
2635 * which should always be the user's requested size.
2636 */
2637 I915_WRITE(DSPSIZE(plane),
a8d201af
ML
2638 ((crtc_state->pipe_src_h - 1) << 16) |
2639 (crtc_state->pipe_src_w - 1));
f45651ba 2640 I915_WRITE(DSPPOS(plane), 0);
c14b0485
VS
2641 } else if (IS_CHERRYVIEW(dev) && plane == PLANE_B) {
2642 I915_WRITE(PRIMSIZE(plane),
a8d201af
ML
2643 ((crtc_state->pipe_src_h - 1) << 16) |
2644 (crtc_state->pipe_src_w - 1));
c14b0485
VS
2645 I915_WRITE(PRIMPOS(plane), 0);
2646 I915_WRITE(PRIMCNSTALPHA(plane), 0);
f45651ba 2647 }
81255565 2648
57779d06
VS
2649 switch (fb->pixel_format) {
2650 case DRM_FORMAT_C8:
81255565
JB
2651 dspcntr |= DISPPLANE_8BPP;
2652 break;
57779d06 2653 case DRM_FORMAT_XRGB1555:
57779d06 2654 dspcntr |= DISPPLANE_BGRX555;
81255565 2655 break;
57779d06
VS
2656 case DRM_FORMAT_RGB565:
2657 dspcntr |= DISPPLANE_BGRX565;
2658 break;
2659 case DRM_FORMAT_XRGB8888:
57779d06
VS
2660 dspcntr |= DISPPLANE_BGRX888;
2661 break;
2662 case DRM_FORMAT_XBGR8888:
57779d06
VS
2663 dspcntr |= DISPPLANE_RGBX888;
2664 break;
2665 case DRM_FORMAT_XRGB2101010:
57779d06
VS
2666 dspcntr |= DISPPLANE_BGRX101010;
2667 break;
2668 case DRM_FORMAT_XBGR2101010:
57779d06 2669 dspcntr |= DISPPLANE_RGBX101010;
81255565
JB
2670 break;
2671 default:
baba133a 2672 BUG();
81255565 2673 }
57779d06 2674
f45651ba
VS
2675 if (INTEL_INFO(dev)->gen >= 4 &&
2676 obj->tiling_mode != I915_TILING_NONE)
2677 dspcntr |= DISPPLANE_TILED;
81255565 2678
de1aa629
VS
2679 if (IS_G4X(dev))
2680 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
2681
ac484963 2682 linear_offset = y * fb->pitches[0] + x * cpp;
81255565 2683
c2c75131
DV
2684 if (INTEL_INFO(dev)->gen >= 4) {
2685 intel_crtc->dspaddr_offset =
4f2d9934 2686 intel_compute_tile_offset(&x, &y, fb, 0,
8d0deca8 2687 fb->pitches[0], rotation);
c2c75131
DV
2688 linear_offset -= intel_crtc->dspaddr_offset;
2689 } else {
e506a0c6 2690 intel_crtc->dspaddr_offset = linear_offset;
c2c75131 2691 }
e506a0c6 2692
8d0deca8 2693 if (rotation == BIT(DRM_ROTATE_180)) {
48404c1e
SJ
2694 dspcntr |= DISPPLANE_ROTATE_180;
2695
a8d201af
ML
2696 x += (crtc_state->pipe_src_w - 1);
2697 y += (crtc_state->pipe_src_h - 1);
48404c1e
SJ
2698
2699 /* Finding the last pixel of the last line of the display
2700 data and adding to linear_offset*/
2701 linear_offset +=
a8d201af 2702 (crtc_state->pipe_src_h - 1) * fb->pitches[0] +
ac484963 2703 (crtc_state->pipe_src_w - 1) * cpp;
48404c1e
SJ
2704 }
2705
2db3366b
PZ
2706 intel_crtc->adjusted_x = x;
2707 intel_crtc->adjusted_y = y;
2708
48404c1e
SJ
2709 I915_WRITE(reg, dspcntr);
2710
01f2c773 2711 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
a6c45cf0 2712 if (INTEL_INFO(dev)->gen >= 4) {
85ba7b7d
DV
2713 I915_WRITE(DSPSURF(plane),
2714 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
5eddb70b 2715 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
e506a0c6 2716 I915_WRITE(DSPLINOFF(plane), linear_offset);
5eddb70b 2717 } else
f343c5f6 2718 I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
5eddb70b 2719 POSTING_READ(reg);
17638cd6
JB
2720}
2721
a8d201af
ML
2722static void i9xx_disable_primary_plane(struct drm_plane *primary,
2723 struct drm_crtc *crtc)
17638cd6
JB
2724{
2725 struct drm_device *dev = crtc->dev;
fac5e23e 2726 struct drm_i915_private *dev_priv = to_i915(dev);
17638cd6 2727 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
17638cd6 2728 int plane = intel_crtc->plane;
f45651ba 2729
a8d201af
ML
2730 I915_WRITE(DSPCNTR(plane), 0);
2731 if (INTEL_INFO(dev_priv)->gen >= 4)
fdd508a6 2732 I915_WRITE(DSPSURF(plane), 0);
a8d201af
ML
2733 else
2734 I915_WRITE(DSPADDR(plane), 0);
2735 POSTING_READ(DSPCNTR(plane));
2736}
c9ba6fad 2737
a8d201af
ML
2738static void ironlake_update_primary_plane(struct drm_plane *primary,
2739 const struct intel_crtc_state *crtc_state,
2740 const struct intel_plane_state *plane_state)
2741{
2742 struct drm_device *dev = primary->dev;
fac5e23e 2743 struct drm_i915_private *dev_priv = to_i915(dev);
a8d201af
ML
2744 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
2745 struct drm_framebuffer *fb = plane_state->base.fb;
2746 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
2747 int plane = intel_crtc->plane;
54ea9da8 2748 u32 linear_offset;
a8d201af
ML
2749 u32 dspcntr;
2750 i915_reg_t reg = DSPCNTR(plane);
8d0deca8 2751 unsigned int rotation = plane_state->base.rotation;
ac484963 2752 int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
a8d201af
ML
2753 int x = plane_state->src.x1 >> 16;
2754 int y = plane_state->src.y1 >> 16;
c9ba6fad 2755
f45651ba 2756 dspcntr = DISPPLANE_GAMMA_ENABLE;
fdd508a6 2757 dspcntr |= DISPLAY_PLANE_ENABLE;
f45651ba
VS
2758
2759 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
2760 dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
17638cd6 2761
57779d06
VS
2762 switch (fb->pixel_format) {
2763 case DRM_FORMAT_C8:
17638cd6
JB
2764 dspcntr |= DISPPLANE_8BPP;
2765 break;
57779d06
VS
2766 case DRM_FORMAT_RGB565:
2767 dspcntr |= DISPPLANE_BGRX565;
17638cd6 2768 break;
57779d06 2769 case DRM_FORMAT_XRGB8888:
57779d06
VS
2770 dspcntr |= DISPPLANE_BGRX888;
2771 break;
2772 case DRM_FORMAT_XBGR8888:
57779d06
VS
2773 dspcntr |= DISPPLANE_RGBX888;
2774 break;
2775 case DRM_FORMAT_XRGB2101010:
57779d06
VS
2776 dspcntr |= DISPPLANE_BGRX101010;
2777 break;
2778 case DRM_FORMAT_XBGR2101010:
57779d06 2779 dspcntr |= DISPPLANE_RGBX101010;
17638cd6
JB
2780 break;
2781 default:
baba133a 2782 BUG();
17638cd6
JB
2783 }
2784
2785 if (obj->tiling_mode != I915_TILING_NONE)
2786 dspcntr |= DISPPLANE_TILED;
17638cd6 2787
f45651ba 2788 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev))
1f5d76db 2789 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
17638cd6 2790
ac484963 2791 linear_offset = y * fb->pitches[0] + x * cpp;
c2c75131 2792 intel_crtc->dspaddr_offset =
4f2d9934 2793 intel_compute_tile_offset(&x, &y, fb, 0,
8d0deca8 2794 fb->pitches[0], rotation);
c2c75131 2795 linear_offset -= intel_crtc->dspaddr_offset;
8d0deca8 2796 if (rotation == BIT(DRM_ROTATE_180)) {
48404c1e
SJ
2797 dspcntr |= DISPPLANE_ROTATE_180;
2798
2799 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev)) {
a8d201af
ML
2800 x += (crtc_state->pipe_src_w - 1);
2801 y += (crtc_state->pipe_src_h - 1);
48404c1e
SJ
2802
2803 /* Finding the last pixel of the last line of the display
2804 data and adding to linear_offset*/
2805 linear_offset +=
a8d201af 2806 (crtc_state->pipe_src_h - 1) * fb->pitches[0] +
ac484963 2807 (crtc_state->pipe_src_w - 1) * cpp;
48404c1e
SJ
2808 }
2809 }
2810
2db3366b
PZ
2811 intel_crtc->adjusted_x = x;
2812 intel_crtc->adjusted_y = y;
2813
48404c1e 2814 I915_WRITE(reg, dspcntr);
17638cd6 2815
01f2c773 2816 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
85ba7b7d
DV
2817 I915_WRITE(DSPSURF(plane),
2818 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
b3dc685e 2819 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
bc1c91eb
DL
2820 I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
2821 } else {
2822 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2823 I915_WRITE(DSPLINOFF(plane), linear_offset);
2824 }
17638cd6 2825 POSTING_READ(reg);
17638cd6
JB
2826}
2827
7b49f948
VS
2828u32 intel_fb_stride_alignment(const struct drm_i915_private *dev_priv,
2829 uint64_t fb_modifier, uint32_t pixel_format)
b321803d 2830{
7b49f948 2831 if (fb_modifier == DRM_FORMAT_MOD_NONE) {
b321803d 2832 return 64;
7b49f948
VS
2833 } else {
2834 int cpp = drm_format_plane_cpp(pixel_format, 0);
2835
27ba3910 2836 return intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
b321803d
DL
2837 }
2838}
2839
44eb0cb9
MK
2840u32 intel_plane_obj_offset(struct intel_plane *intel_plane,
2841 struct drm_i915_gem_object *obj,
2842 unsigned int plane)
121920fa 2843{
ce7f1728 2844 struct i915_ggtt_view view;
dedf278c 2845 struct i915_vma *vma;
44eb0cb9 2846 u64 offset;
121920fa 2847
e7941294 2848 intel_fill_fb_ggtt_view(&view, intel_plane->base.state->fb,
3465c580 2849 intel_plane->base.state->rotation);
121920fa 2850
ce7f1728 2851 vma = i915_gem_obj_to_ggtt_view(obj, &view);
dedf278c 2852 if (WARN(!vma, "ggtt vma for display object not found! (view=%u)\n",
ce7f1728 2853 view.type))
dedf278c
TU
2854 return -1;
2855
44eb0cb9 2856 offset = vma->node.start;
dedf278c
TU
2857
2858 if (plane == 1) {
7723f47d 2859 offset += vma->ggtt_view.params.rotated.uv_start_page *
dedf278c
TU
2860 PAGE_SIZE;
2861 }
2862
44eb0cb9
MK
2863 WARN_ON(upper_32_bits(offset));
2864
2865 return lower_32_bits(offset);
121920fa
TU
2866}
2867
e435d6e5
ML
2868static void skl_detach_scaler(struct intel_crtc *intel_crtc, int id)
2869{
2870 struct drm_device *dev = intel_crtc->base.dev;
fac5e23e 2871 struct drm_i915_private *dev_priv = to_i915(dev);
e435d6e5
ML
2872
2873 I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, id), 0);
2874 I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, id), 0);
2875 I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, id), 0);
e435d6e5
ML
2876}
2877
a1b2278e
CK
2878/*
2879 * This function detaches (aka. unbinds) unused scalers in hardware
2880 */
0583236e 2881static void skl_detach_scalers(struct intel_crtc *intel_crtc)
a1b2278e 2882{
a1b2278e
CK
2883 struct intel_crtc_scaler_state *scaler_state;
2884 int i;
2885
a1b2278e
CK
2886 scaler_state = &intel_crtc->config->scaler_state;
2887
2888 /* loop through and disable scalers that aren't in use */
2889 for (i = 0; i < intel_crtc->num_scalers; i++) {
e435d6e5
ML
2890 if (!scaler_state->scalers[i].in_use)
2891 skl_detach_scaler(intel_crtc, i);
a1b2278e
CK
2892 }
2893}
2894
6156a456 2895u32 skl_plane_ctl_format(uint32_t pixel_format)
70d21f0e 2896{
6156a456 2897 switch (pixel_format) {
d161cf7a 2898 case DRM_FORMAT_C8:
c34ce3d1 2899 return PLANE_CTL_FORMAT_INDEXED;
70d21f0e 2900 case DRM_FORMAT_RGB565:
c34ce3d1 2901 return PLANE_CTL_FORMAT_RGB_565;
70d21f0e 2902 case DRM_FORMAT_XBGR8888:
c34ce3d1 2903 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
6156a456 2904 case DRM_FORMAT_XRGB8888:
c34ce3d1 2905 return PLANE_CTL_FORMAT_XRGB_8888;
6156a456
CK
2906 /*
2907 * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
2908 * to be already pre-multiplied. We need to add a knob (or a different
2909 * DRM_FORMAT) for user-space to configure that.
2910 */
f75fb42a 2911 case DRM_FORMAT_ABGR8888:
c34ce3d1 2912 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX |
6156a456 2913 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
6156a456 2914 case DRM_FORMAT_ARGB8888:
c34ce3d1 2915 return PLANE_CTL_FORMAT_XRGB_8888 |
6156a456 2916 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
70d21f0e 2917 case DRM_FORMAT_XRGB2101010:
c34ce3d1 2918 return PLANE_CTL_FORMAT_XRGB_2101010;
70d21f0e 2919 case DRM_FORMAT_XBGR2101010:
c34ce3d1 2920 return PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
6156a456 2921 case DRM_FORMAT_YUYV:
c34ce3d1 2922 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
6156a456 2923 case DRM_FORMAT_YVYU:
c34ce3d1 2924 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
6156a456 2925 case DRM_FORMAT_UYVY:
c34ce3d1 2926 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
6156a456 2927 case DRM_FORMAT_VYUY:
c34ce3d1 2928 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
70d21f0e 2929 default:
4249eeef 2930 MISSING_CASE(pixel_format);
70d21f0e 2931 }
8cfcba41 2932
c34ce3d1 2933 return 0;
6156a456 2934}
70d21f0e 2935
6156a456
CK
2936u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
2937{
6156a456 2938 switch (fb_modifier) {
30af77c4 2939 case DRM_FORMAT_MOD_NONE:
70d21f0e 2940 break;
30af77c4 2941 case I915_FORMAT_MOD_X_TILED:
c34ce3d1 2942 return PLANE_CTL_TILED_X;
b321803d 2943 case I915_FORMAT_MOD_Y_TILED:
c34ce3d1 2944 return PLANE_CTL_TILED_Y;
b321803d 2945 case I915_FORMAT_MOD_Yf_TILED:
c34ce3d1 2946 return PLANE_CTL_TILED_YF;
70d21f0e 2947 default:
6156a456 2948 MISSING_CASE(fb_modifier);
70d21f0e 2949 }
8cfcba41 2950
c34ce3d1 2951 return 0;
6156a456 2952}
70d21f0e 2953
6156a456
CK
2954u32 skl_plane_ctl_rotation(unsigned int rotation)
2955{
3b7a5119 2956 switch (rotation) {
6156a456
CK
2957 case BIT(DRM_ROTATE_0):
2958 break;
1e8df167
SJ
2959 /*
2960 * DRM_ROTATE_ is counter clockwise to stay compatible with Xrandr
2961 * while i915 HW rotation is clockwise, thats why this swapping.
2962 */
3b7a5119 2963 case BIT(DRM_ROTATE_90):
1e8df167 2964 return PLANE_CTL_ROTATE_270;
3b7a5119 2965 case BIT(DRM_ROTATE_180):
c34ce3d1 2966 return PLANE_CTL_ROTATE_180;
3b7a5119 2967 case BIT(DRM_ROTATE_270):
1e8df167 2968 return PLANE_CTL_ROTATE_90;
6156a456
CK
2969 default:
2970 MISSING_CASE(rotation);
2971 }
2972
c34ce3d1 2973 return 0;
6156a456
CK
2974}
2975
a8d201af
ML
2976static void skylake_update_primary_plane(struct drm_plane *plane,
2977 const struct intel_crtc_state *crtc_state,
2978 const struct intel_plane_state *plane_state)
6156a456 2979{
a8d201af 2980 struct drm_device *dev = plane->dev;
fac5e23e 2981 struct drm_i915_private *dev_priv = to_i915(dev);
a8d201af
ML
2982 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
2983 struct drm_framebuffer *fb = plane_state->base.fb;
2984 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
6156a456
CK
2985 int pipe = intel_crtc->pipe;
2986 u32 plane_ctl, stride_div, stride;
2987 u32 tile_height, plane_offset, plane_size;
a8d201af 2988 unsigned int rotation = plane_state->base.rotation;
6156a456 2989 int x_offset, y_offset;
44eb0cb9 2990 u32 surf_addr;
a8d201af
ML
2991 int scaler_id = plane_state->scaler_id;
2992 int src_x = plane_state->src.x1 >> 16;
2993 int src_y = plane_state->src.y1 >> 16;
2994 int src_w = drm_rect_width(&plane_state->src) >> 16;
2995 int src_h = drm_rect_height(&plane_state->src) >> 16;
2996 int dst_x = plane_state->dst.x1;
2997 int dst_y = plane_state->dst.y1;
2998 int dst_w = drm_rect_width(&plane_state->dst);
2999 int dst_h = drm_rect_height(&plane_state->dst);
70d21f0e 3000
6156a456
CK
3001 plane_ctl = PLANE_CTL_ENABLE |
3002 PLANE_CTL_PIPE_GAMMA_ENABLE |
3003 PLANE_CTL_PIPE_CSC_ENABLE;
3004
3005 plane_ctl |= skl_plane_ctl_format(fb->pixel_format);
3006 plane_ctl |= skl_plane_ctl_tiling(fb->modifier[0]);
3007 plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
6156a456
CK
3008 plane_ctl |= skl_plane_ctl_rotation(rotation);
3009
7b49f948 3010 stride_div = intel_fb_stride_alignment(dev_priv, fb->modifier[0],
b321803d 3011 fb->pixel_format);
dedf278c 3012 surf_addr = intel_plane_obj_offset(to_intel_plane(plane), obj, 0);
3b7a5119 3013
a42e5a23
PZ
3014 WARN_ON(drm_rect_width(&plane_state->src) == 0);
3015
3b7a5119 3016 if (intel_rotation_90_or_270(rotation)) {
832be82f
VS
3017 int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
3018
3b7a5119 3019 /* stride = Surface height in tiles */
832be82f 3020 tile_height = intel_tile_height(dev_priv, fb->modifier[0], cpp);
3b7a5119 3021 stride = DIV_ROUND_UP(fb->height, tile_height);
a8d201af
ML
3022 x_offset = stride * tile_height - src_y - src_h;
3023 y_offset = src_x;
6156a456 3024 plane_size = (src_w - 1) << 16 | (src_h - 1);
3b7a5119
SJ
3025 } else {
3026 stride = fb->pitches[0] / stride_div;
a8d201af
ML
3027 x_offset = src_x;
3028 y_offset = src_y;
6156a456 3029 plane_size = (src_h - 1) << 16 | (src_w - 1);
3b7a5119
SJ
3030 }
3031 plane_offset = y_offset << 16 | x_offset;
b321803d 3032
2db3366b
PZ
3033 intel_crtc->adjusted_x = x_offset;
3034 intel_crtc->adjusted_y = y_offset;
3035
70d21f0e 3036 I915_WRITE(PLANE_CTL(pipe, 0), plane_ctl);
3b7a5119
SJ
3037 I915_WRITE(PLANE_OFFSET(pipe, 0), plane_offset);
3038 I915_WRITE(PLANE_SIZE(pipe, 0), plane_size);
3039 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
6156a456
CK
3040
3041 if (scaler_id >= 0) {
3042 uint32_t ps_ctrl = 0;
3043
3044 WARN_ON(!dst_w || !dst_h);
3045 ps_ctrl = PS_SCALER_EN | PS_PLANE_SEL(0) |
3046 crtc_state->scaler_state.scalers[scaler_id].mode;
3047 I915_WRITE(SKL_PS_CTRL(pipe, scaler_id), ps_ctrl);
3048 I915_WRITE(SKL_PS_PWR_GATE(pipe, scaler_id), 0);
3049 I915_WRITE(SKL_PS_WIN_POS(pipe, scaler_id), (dst_x << 16) | dst_y);
3050 I915_WRITE(SKL_PS_WIN_SZ(pipe, scaler_id), (dst_w << 16) | dst_h);
3051 I915_WRITE(PLANE_POS(pipe, 0), 0);
3052 } else {
3053 I915_WRITE(PLANE_POS(pipe, 0), (dst_y << 16) | dst_x);
3054 }
3055
121920fa 3056 I915_WRITE(PLANE_SURF(pipe, 0), surf_addr);
70d21f0e
DL
3057
3058 POSTING_READ(PLANE_SURF(pipe, 0));
3059}
3060
a8d201af
ML
3061static void skylake_disable_primary_plane(struct drm_plane *primary,
3062 struct drm_crtc *crtc)
17638cd6
JB
3063{
3064 struct drm_device *dev = crtc->dev;
fac5e23e 3065 struct drm_i915_private *dev_priv = to_i915(dev);
a8d201af 3066 int pipe = to_intel_crtc(crtc)->pipe;
17638cd6 3067
a8d201af
ML
3068 I915_WRITE(PLANE_CTL(pipe, 0), 0);
3069 I915_WRITE(PLANE_SURF(pipe, 0), 0);
3070 POSTING_READ(PLANE_SURF(pipe, 0));
3071}
29b9bde6 3072
a8d201af
ML
3073/* Assume fb object is pinned & idle & fenced and just update base pointers */
3074static int
3075intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
3076 int x, int y, enum mode_set_atomic state)
3077{
3078 /* Support for kgdboc is disabled, this needs a major rework. */
3079 DRM_ERROR("legacy panic handler not supported any more.\n");
3080
3081 return -ENODEV;
81255565
JB
3082}
3083
5a21b665
DV
3084static void intel_complete_page_flips(struct drm_i915_private *dev_priv)
3085{
3086 struct intel_crtc *crtc;
3087
91c8a326 3088 for_each_intel_crtc(&dev_priv->drm, crtc)
5a21b665
DV
3089 intel_finish_page_flip_cs(dev_priv, crtc->pipe);
3090}
3091
7514747d
VS
3092static void intel_update_primary_planes(struct drm_device *dev)
3093{
7514747d 3094 struct drm_crtc *crtc;
96a02917 3095
70e1e0ec 3096 for_each_crtc(dev, crtc) {
11c22da6
ML
3097 struct intel_plane *plane = to_intel_plane(crtc->primary);
3098 struct intel_plane_state *plane_state;
96a02917 3099
11c22da6 3100 drm_modeset_lock_crtc(crtc, &plane->base);
11c22da6
ML
3101 plane_state = to_intel_plane_state(plane->base.state);
3102
a8d201af
ML
3103 if (plane_state->visible)
3104 plane->update_plane(&plane->base,
3105 to_intel_crtc_state(crtc->state),
3106 plane_state);
11c22da6
ML
3107
3108 drm_modeset_unlock_crtc(crtc);
96a02917
VS
3109 }
3110}
3111
c033666a 3112void intel_prepare_reset(struct drm_i915_private *dev_priv)
7514747d
VS
3113{
3114 /* no reset support for gen2 */
c033666a 3115 if (IS_GEN2(dev_priv))
7514747d
VS
3116 return;
3117
3118 /* reset doesn't touch the display */
c033666a 3119 if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
7514747d
VS
3120 return;
3121
91c8a326 3122 drm_modeset_lock_all(&dev_priv->drm);
f98ce92f
VS
3123 /*
3124 * Disabling the crtcs gracefully seems nicer. Also the
3125 * g33 docs say we should at least disable all the planes.
3126 */
91c8a326 3127 intel_display_suspend(&dev_priv->drm);
7514747d
VS
3128}
3129
c033666a 3130void intel_finish_reset(struct drm_i915_private *dev_priv)
7514747d 3131{
5a21b665
DV
3132 /*
3133 * Flips in the rings will be nuked by the reset,
3134 * so complete all pending flips so that user space
3135 * will get its events and not get stuck.
3136 */
3137 intel_complete_page_flips(dev_priv);
3138
7514747d 3139 /* no reset support for gen2 */
c033666a 3140 if (IS_GEN2(dev_priv))
7514747d
VS
3141 return;
3142
3143 /* reset doesn't touch the display */
c033666a 3144 if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv)) {
7514747d
VS
3145 /*
3146 * Flips in the rings have been nuked by the reset,
3147 * so update the base address of all primary
3148 * planes to the the last fb to make sure we're
3149 * showing the correct fb after a reset.
11c22da6
ML
3150 *
3151 * FIXME: Atomic will make this obsolete since we won't schedule
3152 * CS-based flips (which might get lost in gpu resets) any more.
7514747d 3153 */
91c8a326 3154 intel_update_primary_planes(&dev_priv->drm);
7514747d
VS
3155 return;
3156 }
3157
3158 /*
3159 * The display has been reset as well,
3160 * so need a full re-initialization.
3161 */
3162 intel_runtime_pm_disable_interrupts(dev_priv);
3163 intel_runtime_pm_enable_interrupts(dev_priv);
3164
91c8a326 3165 intel_modeset_init_hw(&dev_priv->drm);
7514747d
VS
3166
3167 spin_lock_irq(&dev_priv->irq_lock);
3168 if (dev_priv->display.hpd_irq_setup)
91d14251 3169 dev_priv->display.hpd_irq_setup(dev_priv);
7514747d
VS
3170 spin_unlock_irq(&dev_priv->irq_lock);
3171
91c8a326 3172 intel_display_resume(&dev_priv->drm);
7514747d
VS
3173
3174 intel_hpd_init(dev_priv);
3175
91c8a326 3176 drm_modeset_unlock_all(&dev_priv->drm);
7514747d
VS
3177}
3178
7d5e3799
CW
3179static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
3180{
5a21b665
DV
3181 struct drm_device *dev = crtc->dev;
3182 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3183 unsigned reset_counter;
3184 bool pending;
3185
3186 reset_counter = i915_reset_counter(&to_i915(dev)->gpu_error);
3187 if (intel_crtc->reset_counter != reset_counter)
3188 return false;
3189
3190 spin_lock_irq(&dev->event_lock);
3191 pending = to_intel_crtc(crtc)->flip_work != NULL;
3192 spin_unlock_irq(&dev->event_lock);
3193
3194 return pending;
7d5e3799
CW
3195}
3196
bfd16b2a
ML
3197static void intel_update_pipe_config(struct intel_crtc *crtc,
3198 struct intel_crtc_state *old_crtc_state)
e30e8f75
GP
3199{
3200 struct drm_device *dev = crtc->base.dev;
fac5e23e 3201 struct drm_i915_private *dev_priv = to_i915(dev);
bfd16b2a
ML
3202 struct intel_crtc_state *pipe_config =
3203 to_intel_crtc_state(crtc->base.state);
e30e8f75 3204
bfd16b2a
ML
3205 /* drm_atomic_helper_update_legacy_modeset_state might not be called. */
3206 crtc->base.mode = crtc->base.state->mode;
3207
3208 DRM_DEBUG_KMS("Updating pipe size %ix%i -> %ix%i\n",
3209 old_crtc_state->pipe_src_w, old_crtc_state->pipe_src_h,
3210 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
e30e8f75
GP
3211
3212 /*
3213 * Update pipe size and adjust fitter if needed: the reason for this is
3214 * that in compute_mode_changes we check the native mode (not the pfit
3215 * mode) to see if we can flip rather than do a full mode set. In the
3216 * fastboot case, we'll flip, but if we don't update the pipesrc and
3217 * pfit state, we'll end up with a big fb scanned out into the wrong
3218 * sized surface.
e30e8f75
GP
3219 */
3220
e30e8f75 3221 I915_WRITE(PIPESRC(crtc->pipe),
bfd16b2a
ML
3222 ((pipe_config->pipe_src_w - 1) << 16) |
3223 (pipe_config->pipe_src_h - 1));
3224
3225 /* on skylake this is done by detaching scalers */
3226 if (INTEL_INFO(dev)->gen >= 9) {
3227 skl_detach_scalers(crtc);
3228
3229 if (pipe_config->pch_pfit.enabled)
3230 skylake_pfit_enable(crtc);
3231 } else if (HAS_PCH_SPLIT(dev)) {
3232 if (pipe_config->pch_pfit.enabled)
3233 ironlake_pfit_enable(crtc);
3234 else if (old_crtc_state->pch_pfit.enabled)
3235 ironlake_pfit_disable(crtc, true);
e30e8f75 3236 }
e30e8f75
GP
3237}
3238
5e84e1a4
ZW
3239static void intel_fdi_normal_train(struct drm_crtc *crtc)
3240{
3241 struct drm_device *dev = crtc->dev;
fac5e23e 3242 struct drm_i915_private *dev_priv = to_i915(dev);
5e84e1a4
ZW
3243 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3244 int pipe = intel_crtc->pipe;
f0f59a00
VS
3245 i915_reg_t reg;
3246 u32 temp;
5e84e1a4
ZW
3247
3248 /* enable normal train */
3249 reg = FDI_TX_CTL(pipe);
3250 temp = I915_READ(reg);
61e499bf 3251 if (IS_IVYBRIDGE(dev)) {
357555c0
JB
3252 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3253 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
61e499bf
KP
3254 } else {
3255 temp &= ~FDI_LINK_TRAIN_NONE;
3256 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
357555c0 3257 }
5e84e1a4
ZW
3258 I915_WRITE(reg, temp);
3259
3260 reg = FDI_RX_CTL(pipe);
3261 temp = I915_READ(reg);
3262 if (HAS_PCH_CPT(dev)) {
3263 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3264 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
3265 } else {
3266 temp &= ~FDI_LINK_TRAIN_NONE;
3267 temp |= FDI_LINK_TRAIN_NONE;
3268 }
3269 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
3270
3271 /* wait one idle pattern time */
3272 POSTING_READ(reg);
3273 udelay(1000);
357555c0
JB
3274
3275 /* IVB wants error correction enabled */
3276 if (IS_IVYBRIDGE(dev))
3277 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
3278 FDI_FE_ERRC_ENABLE);
5e84e1a4
ZW
3279}
3280
8db9d77b
ZW
3281/* The FDI link training functions for ILK/Ibexpeak. */
3282static void ironlake_fdi_link_train(struct drm_crtc *crtc)
3283{
3284 struct drm_device *dev = crtc->dev;
fac5e23e 3285 struct drm_i915_private *dev_priv = to_i915(dev);
8db9d77b
ZW
3286 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3287 int pipe = intel_crtc->pipe;
f0f59a00
VS
3288 i915_reg_t reg;
3289 u32 temp, tries;
8db9d77b 3290
1c8562f6 3291 /* FDI needs bits from pipe first */
0fc932b8 3292 assert_pipe_enabled(dev_priv, pipe);
0fc932b8 3293
e1a44743
AJ
3294 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3295 for train result */
5eddb70b
CW
3296 reg = FDI_RX_IMR(pipe);
3297 temp = I915_READ(reg);
e1a44743
AJ
3298 temp &= ~FDI_RX_SYMBOL_LOCK;
3299 temp &= ~FDI_RX_BIT_LOCK;
5eddb70b
CW
3300 I915_WRITE(reg, temp);
3301 I915_READ(reg);
e1a44743
AJ
3302 udelay(150);
3303
8db9d77b 3304 /* enable CPU FDI TX and PCH FDI RX */
5eddb70b
CW
3305 reg = FDI_TX_CTL(pipe);
3306 temp = I915_READ(reg);
627eb5a3 3307 temp &= ~FDI_DP_PORT_WIDTH_MASK;
6e3c9717 3308 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
8db9d77b
ZW
3309 temp &= ~FDI_LINK_TRAIN_NONE;
3310 temp |= FDI_LINK_TRAIN_PATTERN_1;
5eddb70b 3311 I915_WRITE(reg, temp | FDI_TX_ENABLE);
8db9d77b 3312
5eddb70b
CW
3313 reg = FDI_RX_CTL(pipe);
3314 temp = I915_READ(reg);
8db9d77b
ZW
3315 temp &= ~FDI_LINK_TRAIN_NONE;
3316 temp |= FDI_LINK_TRAIN_PATTERN_1;
5eddb70b
CW
3317 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3318
3319 POSTING_READ(reg);
8db9d77b
ZW
3320 udelay(150);
3321
5b2adf89 3322 /* Ironlake workaround, enable clock pointer after FDI enable*/
8f5718a6
DV
3323 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
3324 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
3325 FDI_RX_PHASE_SYNC_POINTER_EN);
5b2adf89 3326
5eddb70b 3327 reg = FDI_RX_IIR(pipe);
e1a44743 3328 for (tries = 0; tries < 5; tries++) {
5eddb70b 3329 temp = I915_READ(reg);
8db9d77b
ZW
3330 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3331
3332 if ((temp & FDI_RX_BIT_LOCK)) {
3333 DRM_DEBUG_KMS("FDI train 1 done.\n");
5eddb70b 3334 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
8db9d77b
ZW
3335 break;
3336 }
8db9d77b 3337 }
e1a44743 3338 if (tries == 5)
5eddb70b 3339 DRM_ERROR("FDI train 1 fail!\n");
8db9d77b
ZW
3340
3341 /* Train 2 */
5eddb70b
CW
3342 reg = FDI_TX_CTL(pipe);
3343 temp = I915_READ(reg);
8db9d77b
ZW
3344 temp &= ~FDI_LINK_TRAIN_NONE;
3345 temp |= FDI_LINK_TRAIN_PATTERN_2;
5eddb70b 3346 I915_WRITE(reg, temp);
8db9d77b 3347
5eddb70b
CW
3348 reg = FDI_RX_CTL(pipe);
3349 temp = I915_READ(reg);
8db9d77b
ZW
3350 temp &= ~FDI_LINK_TRAIN_NONE;
3351 temp |= FDI_LINK_TRAIN_PATTERN_2;
5eddb70b 3352 I915_WRITE(reg, temp);
8db9d77b 3353
5eddb70b
CW
3354 POSTING_READ(reg);
3355 udelay(150);
8db9d77b 3356
5eddb70b 3357 reg = FDI_RX_IIR(pipe);
e1a44743 3358 for (tries = 0; tries < 5; tries++) {
5eddb70b 3359 temp = I915_READ(reg);
8db9d77b
ZW
3360 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3361
3362 if (temp & FDI_RX_SYMBOL_LOCK) {
5eddb70b 3363 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
8db9d77b
ZW
3364 DRM_DEBUG_KMS("FDI train 2 done.\n");
3365 break;
3366 }
8db9d77b 3367 }
e1a44743 3368 if (tries == 5)
5eddb70b 3369 DRM_ERROR("FDI train 2 fail!\n");
8db9d77b
ZW
3370
3371 DRM_DEBUG_KMS("FDI train done\n");
5c5313c8 3372
8db9d77b
ZW
3373}
3374
0206e353 3375static const int snb_b_fdi_train_param[] = {
8db9d77b
ZW
3376 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
3377 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
3378 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
3379 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
3380};
3381
3382/* The FDI link training functions for SNB/Cougarpoint. */
3383static void gen6_fdi_link_train(struct drm_crtc *crtc)
3384{
3385 struct drm_device *dev = crtc->dev;
fac5e23e 3386 struct drm_i915_private *dev_priv = to_i915(dev);
8db9d77b
ZW
3387 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3388 int pipe = intel_crtc->pipe;
f0f59a00
VS
3389 i915_reg_t reg;
3390 u32 temp, i, retry;
8db9d77b 3391
e1a44743
AJ
3392 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3393 for train result */
5eddb70b
CW
3394 reg = FDI_RX_IMR(pipe);
3395 temp = I915_READ(reg);
e1a44743
AJ
3396 temp &= ~FDI_RX_SYMBOL_LOCK;
3397 temp &= ~FDI_RX_BIT_LOCK;
5eddb70b
CW
3398 I915_WRITE(reg, temp);
3399
3400 POSTING_READ(reg);
e1a44743
AJ
3401 udelay(150);
3402
8db9d77b 3403 /* enable CPU FDI TX and PCH FDI RX */
5eddb70b
CW
3404 reg = FDI_TX_CTL(pipe);
3405 temp = I915_READ(reg);
627eb5a3 3406 temp &= ~FDI_DP_PORT_WIDTH_MASK;
6e3c9717 3407 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
8db9d77b
ZW
3408 temp &= ~FDI_LINK_TRAIN_NONE;
3409 temp |= FDI_LINK_TRAIN_PATTERN_1;
3410 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3411 /* SNB-B */
3412 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
5eddb70b 3413 I915_WRITE(reg, temp | FDI_TX_ENABLE);
8db9d77b 3414
d74cf324
DV
3415 I915_WRITE(FDI_RX_MISC(pipe),
3416 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3417
5eddb70b
CW
3418 reg = FDI_RX_CTL(pipe);
3419 temp = I915_READ(reg);
8db9d77b
ZW
3420 if (HAS_PCH_CPT(dev)) {
3421 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3422 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3423 } else {
3424 temp &= ~FDI_LINK_TRAIN_NONE;
3425 temp |= FDI_LINK_TRAIN_PATTERN_1;
3426 }
5eddb70b
CW
3427 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3428
3429 POSTING_READ(reg);
8db9d77b
ZW
3430 udelay(150);
3431
0206e353 3432 for (i = 0; i < 4; i++) {
5eddb70b
CW
3433 reg = FDI_TX_CTL(pipe);
3434 temp = I915_READ(reg);
8db9d77b
ZW
3435 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3436 temp |= snb_b_fdi_train_param[i];
5eddb70b
CW
3437 I915_WRITE(reg, temp);
3438
3439 POSTING_READ(reg);
8db9d77b
ZW
3440 udelay(500);
3441
fa37d39e
SP
3442 for (retry = 0; retry < 5; retry++) {
3443 reg = FDI_RX_IIR(pipe);
3444 temp = I915_READ(reg);
3445 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3446 if (temp & FDI_RX_BIT_LOCK) {
3447 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3448 DRM_DEBUG_KMS("FDI train 1 done.\n");
3449 break;
3450 }
3451 udelay(50);
8db9d77b 3452 }
fa37d39e
SP
3453 if (retry < 5)
3454 break;
8db9d77b
ZW
3455 }
3456 if (i == 4)
5eddb70b 3457 DRM_ERROR("FDI train 1 fail!\n");
8db9d77b
ZW
3458
3459 /* Train 2 */
5eddb70b
CW
3460 reg = FDI_TX_CTL(pipe);
3461 temp = I915_READ(reg);
8db9d77b
ZW
3462 temp &= ~FDI_LINK_TRAIN_NONE;
3463 temp |= FDI_LINK_TRAIN_PATTERN_2;
3464 if (IS_GEN6(dev)) {
3465 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3466 /* SNB-B */
3467 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
3468 }
5eddb70b 3469 I915_WRITE(reg, temp);
8db9d77b 3470
5eddb70b
CW
3471 reg = FDI_RX_CTL(pipe);
3472 temp = I915_READ(reg);
8db9d77b
ZW
3473 if (HAS_PCH_CPT(dev)) {
3474 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3475 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
3476 } else {
3477 temp &= ~FDI_LINK_TRAIN_NONE;
3478 temp |= FDI_LINK_TRAIN_PATTERN_2;
3479 }
5eddb70b
CW
3480 I915_WRITE(reg, temp);
3481
3482 POSTING_READ(reg);
8db9d77b
ZW
3483 udelay(150);
3484
0206e353 3485 for (i = 0; i < 4; i++) {
5eddb70b
CW
3486 reg = FDI_TX_CTL(pipe);
3487 temp = I915_READ(reg);
8db9d77b
ZW
3488 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3489 temp |= snb_b_fdi_train_param[i];
5eddb70b
CW
3490 I915_WRITE(reg, temp);
3491
3492 POSTING_READ(reg);
8db9d77b
ZW
3493 udelay(500);
3494
fa37d39e
SP
3495 for (retry = 0; retry < 5; retry++) {
3496 reg = FDI_RX_IIR(pipe);
3497 temp = I915_READ(reg);
3498 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3499 if (temp & FDI_RX_SYMBOL_LOCK) {
3500 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3501 DRM_DEBUG_KMS("FDI train 2 done.\n");
3502 break;
3503 }
3504 udelay(50);
8db9d77b 3505 }
fa37d39e
SP
3506 if (retry < 5)
3507 break;
8db9d77b
ZW
3508 }
3509 if (i == 4)
5eddb70b 3510 DRM_ERROR("FDI train 2 fail!\n");
8db9d77b
ZW
3511
3512 DRM_DEBUG_KMS("FDI train done.\n");
3513}
3514
357555c0
JB
3515/* Manual link training for Ivy Bridge A0 parts */
3516static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
3517{
3518 struct drm_device *dev = crtc->dev;
fac5e23e 3519 struct drm_i915_private *dev_priv = to_i915(dev);
357555c0
JB
3520 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3521 int pipe = intel_crtc->pipe;
f0f59a00
VS
3522 i915_reg_t reg;
3523 u32 temp, i, j;
357555c0
JB
3524
3525 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3526 for train result */
3527 reg = FDI_RX_IMR(pipe);
3528 temp = I915_READ(reg);
3529 temp &= ~FDI_RX_SYMBOL_LOCK;
3530 temp &= ~FDI_RX_BIT_LOCK;
3531 I915_WRITE(reg, temp);
3532
3533 POSTING_READ(reg);
3534 udelay(150);
3535
01a415fd
DV
3536 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
3537 I915_READ(FDI_RX_IIR(pipe)));
3538
139ccd3f
JB
3539 /* Try each vswing and preemphasis setting twice before moving on */
3540 for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
3541 /* disable first in case we need to retry */
3542 reg = FDI_TX_CTL(pipe);
3543 temp = I915_READ(reg);
3544 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
3545 temp &= ~FDI_TX_ENABLE;
3546 I915_WRITE(reg, temp);
357555c0 3547
139ccd3f
JB
3548 reg = FDI_RX_CTL(pipe);
3549 temp = I915_READ(reg);
3550 temp &= ~FDI_LINK_TRAIN_AUTO;
3551 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3552 temp &= ~FDI_RX_ENABLE;
3553 I915_WRITE(reg, temp);
357555c0 3554
139ccd3f 3555 /* enable CPU FDI TX and PCH FDI RX */
357555c0
JB
3556 reg = FDI_TX_CTL(pipe);
3557 temp = I915_READ(reg);
139ccd3f 3558 temp &= ~FDI_DP_PORT_WIDTH_MASK;
6e3c9717 3559 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
139ccd3f 3560 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
357555c0 3561 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
139ccd3f
JB
3562 temp |= snb_b_fdi_train_param[j/2];
3563 temp |= FDI_COMPOSITE_SYNC;
3564 I915_WRITE(reg, temp | FDI_TX_ENABLE);
357555c0 3565
139ccd3f
JB
3566 I915_WRITE(FDI_RX_MISC(pipe),
3567 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
357555c0 3568
139ccd3f 3569 reg = FDI_RX_CTL(pipe);
357555c0 3570 temp = I915_READ(reg);
139ccd3f
JB
3571 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3572 temp |= FDI_COMPOSITE_SYNC;
3573 I915_WRITE(reg, temp | FDI_RX_ENABLE);
357555c0 3574
139ccd3f
JB
3575 POSTING_READ(reg);
3576 udelay(1); /* should be 0.5us */
357555c0 3577
139ccd3f
JB
3578 for (i = 0; i < 4; i++) {
3579 reg = FDI_RX_IIR(pipe);
3580 temp = I915_READ(reg);
3581 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
357555c0 3582
139ccd3f
JB
3583 if (temp & FDI_RX_BIT_LOCK ||
3584 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
3585 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3586 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
3587 i);
3588 break;
3589 }
3590 udelay(1); /* should be 0.5us */
3591 }
3592 if (i == 4) {
3593 DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
3594 continue;
3595 }
357555c0 3596
139ccd3f 3597 /* Train 2 */
357555c0
JB
3598 reg = FDI_TX_CTL(pipe);
3599 temp = I915_READ(reg);
139ccd3f
JB
3600 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3601 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
3602 I915_WRITE(reg, temp);
3603
3604 reg = FDI_RX_CTL(pipe);
3605 temp = I915_READ(reg);
3606 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3607 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
357555c0
JB
3608 I915_WRITE(reg, temp);
3609
3610 POSTING_READ(reg);
139ccd3f 3611 udelay(2); /* should be 1.5us */
357555c0 3612
139ccd3f
JB
3613 for (i = 0; i < 4; i++) {
3614 reg = FDI_RX_IIR(pipe);
3615 temp = I915_READ(reg);
3616 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
357555c0 3617
139ccd3f
JB
3618 if (temp & FDI_RX_SYMBOL_LOCK ||
3619 (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
3620 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3621 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
3622 i);
3623 goto train_done;
3624 }
3625 udelay(2); /* should be 1.5us */
357555c0 3626 }
139ccd3f
JB
3627 if (i == 4)
3628 DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
357555c0 3629 }
357555c0 3630
139ccd3f 3631train_done:
357555c0
JB
3632 DRM_DEBUG_KMS("FDI train done.\n");
3633}
3634
88cefb6c 3635static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
2c07245f 3636{
88cefb6c 3637 struct drm_device *dev = intel_crtc->base.dev;
fac5e23e 3638 struct drm_i915_private *dev_priv = to_i915(dev);
2c07245f 3639 int pipe = intel_crtc->pipe;
f0f59a00
VS
3640 i915_reg_t reg;
3641 u32 temp;
c64e311e 3642
c98e9dcf 3643 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
5eddb70b
CW
3644 reg = FDI_RX_CTL(pipe);
3645 temp = I915_READ(reg);
627eb5a3 3646 temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
6e3c9717 3647 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
dfd07d72 3648 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
5eddb70b
CW
3649 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
3650
3651 POSTING_READ(reg);
c98e9dcf
JB
3652 udelay(200);
3653
3654 /* Switch from Rawclk to PCDclk */
5eddb70b
CW
3655 temp = I915_READ(reg);
3656 I915_WRITE(reg, temp | FDI_PCDCLK);
3657
3658 POSTING_READ(reg);
c98e9dcf
JB
3659 udelay(200);
3660
20749730
PZ
3661 /* Enable CPU FDI TX PLL, always on for Ironlake */
3662 reg = FDI_TX_CTL(pipe);
3663 temp = I915_READ(reg);
3664 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
3665 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
5eddb70b 3666
20749730
PZ
3667 POSTING_READ(reg);
3668 udelay(100);
6be4a607 3669 }
0e23b99d
JB
3670}
3671
88cefb6c
DV
3672static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
3673{
3674 struct drm_device *dev = intel_crtc->base.dev;
fac5e23e 3675 struct drm_i915_private *dev_priv = to_i915(dev);
88cefb6c 3676 int pipe = intel_crtc->pipe;
f0f59a00
VS
3677 i915_reg_t reg;
3678 u32 temp;
88cefb6c
DV
3679
3680 /* Switch from PCDclk to Rawclk */
3681 reg = FDI_RX_CTL(pipe);
3682 temp = I915_READ(reg);
3683 I915_WRITE(reg, temp & ~FDI_PCDCLK);
3684
3685 /* Disable CPU FDI TX PLL */
3686 reg = FDI_TX_CTL(pipe);
3687 temp = I915_READ(reg);
3688 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
3689
3690 POSTING_READ(reg);
3691 udelay(100);
3692
3693 reg = FDI_RX_CTL(pipe);
3694 temp = I915_READ(reg);
3695 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
3696
3697 /* Wait for the clocks to turn off. */
3698 POSTING_READ(reg);
3699 udelay(100);
3700}
3701
0fc932b8
JB
3702static void ironlake_fdi_disable(struct drm_crtc *crtc)
3703{
3704 struct drm_device *dev = crtc->dev;
fac5e23e 3705 struct drm_i915_private *dev_priv = to_i915(dev);
0fc932b8
JB
3706 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3707 int pipe = intel_crtc->pipe;
f0f59a00
VS
3708 i915_reg_t reg;
3709 u32 temp;
0fc932b8
JB
3710
3711 /* disable CPU FDI tx and PCH FDI rx */
3712 reg = FDI_TX_CTL(pipe);
3713 temp = I915_READ(reg);
3714 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
3715 POSTING_READ(reg);
3716
3717 reg = FDI_RX_CTL(pipe);
3718 temp = I915_READ(reg);
3719 temp &= ~(0x7 << 16);
dfd07d72 3720 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
0fc932b8
JB
3721 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
3722
3723 POSTING_READ(reg);
3724 udelay(100);
3725
3726 /* Ironlake workaround, disable clock pointer after downing FDI */
eba905b2 3727 if (HAS_PCH_IBX(dev))
6f06ce18 3728 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
0fc932b8
JB
3729
3730 /* still set train pattern 1 */
3731 reg = FDI_TX_CTL(pipe);
3732 temp = I915_READ(reg);
3733 temp &= ~FDI_LINK_TRAIN_NONE;
3734 temp |= FDI_LINK_TRAIN_PATTERN_1;
3735 I915_WRITE(reg, temp);
3736
3737 reg = FDI_RX_CTL(pipe);
3738 temp = I915_READ(reg);
3739 if (HAS_PCH_CPT(dev)) {
3740 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3741 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3742 } else {
3743 temp &= ~FDI_LINK_TRAIN_NONE;
3744 temp |= FDI_LINK_TRAIN_PATTERN_1;
3745 }
3746 /* BPC in FDI rx is consistent with that in PIPECONF */
3747 temp &= ~(0x07 << 16);
dfd07d72 3748 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
0fc932b8
JB
3749 I915_WRITE(reg, temp);
3750
3751 POSTING_READ(reg);
3752 udelay(100);
3753}
3754
5dce5b93
CW
3755bool intel_has_pending_fb_unpin(struct drm_device *dev)
3756{
3757 struct intel_crtc *crtc;
3758
3759 /* Note that we don't need to be called with mode_config.lock here
3760 * as our list of CRTC objects is static for the lifetime of the
3761 * device and so cannot disappear as we iterate. Similarly, we can
3762 * happily treat the predicates as racy, atomic checks as userspace
3763 * cannot claim and pin a new fb without at least acquring the
3764 * struct_mutex and so serialising with us.
3765 */
d3fcc808 3766 for_each_intel_crtc(dev, crtc) {
5dce5b93
CW
3767 if (atomic_read(&crtc->unpin_work_count) == 0)
3768 continue;
3769
5a21b665 3770 if (crtc->flip_work)
5dce5b93
CW
3771 intel_wait_for_vblank(dev, crtc->pipe);
3772
3773 return true;
3774 }
3775
3776 return false;
3777}
3778
5a21b665 3779static void page_flip_completed(struct intel_crtc *intel_crtc)
d6bbafa1
CW
3780{
3781 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
5a21b665
DV
3782 struct intel_flip_work *work = intel_crtc->flip_work;
3783
3784 intel_crtc->flip_work = NULL;
d6bbafa1
CW
3785
3786 if (work->event)
560ce1dc 3787 drm_crtc_send_vblank_event(&intel_crtc->base, work->event);
d6bbafa1
CW
3788
3789 drm_crtc_vblank_put(&intel_crtc->base);
3790
5a21b665 3791 wake_up_all(&dev_priv->pending_flip_queue);
143f73b3 3792 queue_work(dev_priv->wq, &work->unpin_work);
5a21b665
DV
3793
3794 trace_i915_flip_complete(intel_crtc->plane,
3795 work->pending_flip_obj);
d6bbafa1
CW
3796}
3797
5008e874 3798static int intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
e6c3a2a6 3799{
0f91128d 3800 struct drm_device *dev = crtc->dev;
fac5e23e 3801 struct drm_i915_private *dev_priv = to_i915(dev);
5008e874 3802 long ret;
e6c3a2a6 3803
2c10d571 3804 WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
5008e874
ML
3805
3806 ret = wait_event_interruptible_timeout(
3807 dev_priv->pending_flip_queue,
3808 !intel_crtc_has_pending_flip(crtc),
3809 60*HZ);
3810
3811 if (ret < 0)
3812 return ret;
3813
5a21b665
DV
3814 if (ret == 0) {
3815 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3816 struct intel_flip_work *work;
3817
3818 spin_lock_irq(&dev->event_lock);
3819 work = intel_crtc->flip_work;
3820 if (work && !is_mmio_work(work)) {
3821 WARN_ONCE(1, "Removing stuck page flip\n");
3822 page_flip_completed(intel_crtc);
3823 }
3824 spin_unlock_irq(&dev->event_lock);
3825 }
5bb61643 3826
5008e874 3827 return 0;
e6c3a2a6
CW
3828}
3829
060f02d8
VS
3830static void lpt_disable_iclkip(struct drm_i915_private *dev_priv)
3831{
3832 u32 temp;
3833
3834 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
3835
3836 mutex_lock(&dev_priv->sb_lock);
3837
3838 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
3839 temp |= SBI_SSCCTL_DISABLE;
3840 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
3841
3842 mutex_unlock(&dev_priv->sb_lock);
3843}
3844
e615efe4
ED
3845/* Program iCLKIP clock to the desired frequency */
3846static void lpt_program_iclkip(struct drm_crtc *crtc)
3847{
64b46a06 3848 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
6e3c9717 3849 int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
e615efe4
ED
3850 u32 divsel, phaseinc, auxdiv, phasedir = 0;
3851 u32 temp;
3852
060f02d8 3853 lpt_disable_iclkip(dev_priv);
e615efe4 3854
64b46a06
VS
3855 /* The iCLK virtual clock root frequency is in MHz,
3856 * but the adjusted_mode->crtc_clock in in KHz. To get the
3857 * divisors, it is necessary to divide one by another, so we
3858 * convert the virtual clock precision to KHz here for higher
3859 * precision.
3860 */
3861 for (auxdiv = 0; auxdiv < 2; auxdiv++) {
e615efe4
ED
3862 u32 iclk_virtual_root_freq = 172800 * 1000;
3863 u32 iclk_pi_range = 64;
64b46a06 3864 u32 desired_divisor;
e615efe4 3865
64b46a06
VS
3866 desired_divisor = DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
3867 clock << auxdiv);
3868 divsel = (desired_divisor / iclk_pi_range) - 2;
3869 phaseinc = desired_divisor % iclk_pi_range;
e615efe4 3870
64b46a06
VS
3871 /*
3872 * Near 20MHz is a corner case which is
3873 * out of range for the 7-bit divisor
3874 */
3875 if (divsel <= 0x7f)
3876 break;
e615efe4
ED
3877 }
3878
3879 /* This should not happen with any sane values */
3880 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
3881 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
3882 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
3883 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
3884
3885 DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
12d7ceed 3886 clock,
e615efe4
ED
3887 auxdiv,
3888 divsel,
3889 phasedir,
3890 phaseinc);
3891
060f02d8
VS
3892 mutex_lock(&dev_priv->sb_lock);
3893
e615efe4 3894 /* Program SSCDIVINTPHASE6 */
988d6ee8 3895 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
e615efe4
ED
3896 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
3897 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
3898 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
3899 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
3900 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
3901 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
988d6ee8 3902 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
e615efe4
ED
3903
3904 /* Program SSCAUXDIV */
988d6ee8 3905 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
e615efe4
ED
3906 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
3907 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
988d6ee8 3908 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
e615efe4
ED
3909
3910 /* Enable modulator and associated divider */
988d6ee8 3911 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
e615efe4 3912 temp &= ~SBI_SSCCTL_DISABLE;
988d6ee8 3913 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
e615efe4 3914
060f02d8
VS
3915 mutex_unlock(&dev_priv->sb_lock);
3916
e615efe4
ED
3917 /* Wait for initialization time */
3918 udelay(24);
3919
3920 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
3921}
3922
8802e5b6
VS
3923int lpt_get_iclkip(struct drm_i915_private *dev_priv)
3924{
3925 u32 divsel, phaseinc, auxdiv;
3926 u32 iclk_virtual_root_freq = 172800 * 1000;
3927 u32 iclk_pi_range = 64;
3928 u32 desired_divisor;
3929 u32 temp;
3930
3931 if ((I915_READ(PIXCLK_GATE) & PIXCLK_GATE_UNGATE) == 0)
3932 return 0;
3933
3934 mutex_lock(&dev_priv->sb_lock);
3935
3936 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
3937 if (temp & SBI_SSCCTL_DISABLE) {
3938 mutex_unlock(&dev_priv->sb_lock);
3939 return 0;
3940 }
3941
3942 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
3943 divsel = (temp & SBI_SSCDIVINTPHASE_DIVSEL_MASK) >>
3944 SBI_SSCDIVINTPHASE_DIVSEL_SHIFT;
3945 phaseinc = (temp & SBI_SSCDIVINTPHASE_INCVAL_MASK) >>
3946 SBI_SSCDIVINTPHASE_INCVAL_SHIFT;
3947
3948 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
3949 auxdiv = (temp & SBI_SSCAUXDIV_FINALDIV2SEL_MASK) >>
3950 SBI_SSCAUXDIV_FINALDIV2SEL_SHIFT;
3951
3952 mutex_unlock(&dev_priv->sb_lock);
3953
3954 desired_divisor = (divsel + 2) * iclk_pi_range + phaseinc;
3955
3956 return DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
3957 desired_divisor << auxdiv);
3958}
3959
275f01b2
DV
3960static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
3961 enum pipe pch_transcoder)
3962{
3963 struct drm_device *dev = crtc->base.dev;
fac5e23e 3964 struct drm_i915_private *dev_priv = to_i915(dev);
6e3c9717 3965 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
275f01b2
DV
3966
3967 I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
3968 I915_READ(HTOTAL(cpu_transcoder)));
3969 I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
3970 I915_READ(HBLANK(cpu_transcoder)));
3971 I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
3972 I915_READ(HSYNC(cpu_transcoder)));
3973
3974 I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
3975 I915_READ(VTOTAL(cpu_transcoder)));
3976 I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
3977 I915_READ(VBLANK(cpu_transcoder)));
3978 I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
3979 I915_READ(VSYNC(cpu_transcoder)));
3980 I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
3981 I915_READ(VSYNCSHIFT(cpu_transcoder)));
3982}
3983
003632d9 3984static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
1fbc0d78 3985{
fac5e23e 3986 struct drm_i915_private *dev_priv = to_i915(dev);
1fbc0d78
DV
3987 uint32_t temp;
3988
3989 temp = I915_READ(SOUTH_CHICKEN1);
003632d9 3990 if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
1fbc0d78
DV
3991 return;
3992
3993 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
3994 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
3995
003632d9
ACO
3996 temp &= ~FDI_BC_BIFURCATION_SELECT;
3997 if (enable)
3998 temp |= FDI_BC_BIFURCATION_SELECT;
3999
4000 DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
1fbc0d78
DV
4001 I915_WRITE(SOUTH_CHICKEN1, temp);
4002 POSTING_READ(SOUTH_CHICKEN1);
4003}
4004
4005static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
4006{
4007 struct drm_device *dev = intel_crtc->base.dev;
1fbc0d78
DV
4008
4009 switch (intel_crtc->pipe) {
4010 case PIPE_A:
4011 break;
4012 case PIPE_B:
6e3c9717 4013 if (intel_crtc->config->fdi_lanes > 2)
003632d9 4014 cpt_set_fdi_bc_bifurcation(dev, false);
1fbc0d78 4015 else
003632d9 4016 cpt_set_fdi_bc_bifurcation(dev, true);
1fbc0d78
DV
4017
4018 break;
4019 case PIPE_C:
003632d9 4020 cpt_set_fdi_bc_bifurcation(dev, true);
1fbc0d78
DV
4021
4022 break;
4023 default:
4024 BUG();
4025 }
4026}
4027
c48b5305
VS
4028/* Return which DP Port should be selected for Transcoder DP control */
4029static enum port
4030intel_trans_dp_port_sel(struct drm_crtc *crtc)
4031{
4032 struct drm_device *dev = crtc->dev;
4033 struct intel_encoder *encoder;
4034
4035 for_each_encoder_on_crtc(dev, crtc, encoder) {
cca0502b 4036 if (encoder->type == INTEL_OUTPUT_DP ||
c48b5305
VS
4037 encoder->type == INTEL_OUTPUT_EDP)
4038 return enc_to_dig_port(&encoder->base)->port;
4039 }
4040
4041 return -1;
4042}
4043
f67a559d
JB
4044/*
4045 * Enable PCH resources required for PCH ports:
4046 * - PCH PLLs
4047 * - FDI training & RX/TX
4048 * - update transcoder timings
4049 * - DP transcoding bits
4050 * - transcoder
4051 */
4052static void ironlake_pch_enable(struct drm_crtc *crtc)
0e23b99d
JB
4053{
4054 struct drm_device *dev = crtc->dev;
fac5e23e 4055 struct drm_i915_private *dev_priv = to_i915(dev);
0e23b99d
JB
4056 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4057 int pipe = intel_crtc->pipe;
f0f59a00 4058 u32 temp;
2c07245f 4059
ab9412ba 4060 assert_pch_transcoder_disabled(dev_priv, pipe);
e7e164db 4061
1fbc0d78
DV
4062 if (IS_IVYBRIDGE(dev))
4063 ivybridge_update_fdi_bc_bifurcation(intel_crtc);
4064
cd986abb
DV
4065 /* Write the TU size bits before fdi link training, so that error
4066 * detection works. */
4067 I915_WRITE(FDI_RX_TUSIZE1(pipe),
4068 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
4069
c98e9dcf 4070 /* For PCH output, training FDI link */
674cf967 4071 dev_priv->display.fdi_link_train(crtc);
2c07245f 4072
3ad8a208
DV
4073 /* We need to program the right clock selection before writing the pixel
4074 * mutliplier into the DPLL. */
303b81e0 4075 if (HAS_PCH_CPT(dev)) {
ee7b9f93 4076 u32 sel;
4b645f14 4077
c98e9dcf 4078 temp = I915_READ(PCH_DPLL_SEL);
11887397
DV
4079 temp |= TRANS_DPLL_ENABLE(pipe);
4080 sel = TRANS_DPLLB_SEL(pipe);
8106ddbd
ACO
4081 if (intel_crtc->config->shared_dpll ==
4082 intel_get_shared_dpll_by_id(dev_priv, DPLL_ID_PCH_PLL_B))
ee7b9f93
JB
4083 temp |= sel;
4084 else
4085 temp &= ~sel;
c98e9dcf 4086 I915_WRITE(PCH_DPLL_SEL, temp);
c98e9dcf 4087 }
5eddb70b 4088
3ad8a208
DV
4089 /* XXX: pch pll's can be enabled any time before we enable the PCH
4090 * transcoder, and we actually should do this to not upset any PCH
4091 * transcoder that already use the clock when we share it.
4092 *
4093 * Note that enable_shared_dpll tries to do the right thing, but
4094 * get_shared_dpll unconditionally resets the pll - we need that to have
4095 * the right LVDS enable sequence. */
85b3894f 4096 intel_enable_shared_dpll(intel_crtc);
3ad8a208 4097
d9b6cb56
JB
4098 /* set transcoder timing, panel must allow it */
4099 assert_panel_unlocked(dev_priv, pipe);
275f01b2 4100 ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
8db9d77b 4101
303b81e0 4102 intel_fdi_normal_train(crtc);
5e84e1a4 4103
c98e9dcf 4104 /* For PCH DP, enable TRANS_DP_CTL */
37a5650b 4105 if (HAS_PCH_CPT(dev) && intel_crtc_has_dp_encoder(intel_crtc->config)) {
9c4edaee
VS
4106 const struct drm_display_mode *adjusted_mode =
4107 &intel_crtc->config->base.adjusted_mode;
dfd07d72 4108 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
f0f59a00 4109 i915_reg_t reg = TRANS_DP_CTL(pipe);
5eddb70b
CW
4110 temp = I915_READ(reg);
4111 temp &= ~(TRANS_DP_PORT_SEL_MASK |
220cad3c
EA
4112 TRANS_DP_SYNC_MASK |
4113 TRANS_DP_BPC_MASK);
e3ef4479 4114 temp |= TRANS_DP_OUTPUT_ENABLE;
9325c9f0 4115 temp |= bpc << 9; /* same format but at 11:9 */
c98e9dcf 4116
9c4edaee 4117 if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
5eddb70b 4118 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
9c4edaee 4119 if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
5eddb70b 4120 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
c98e9dcf
JB
4121
4122 switch (intel_trans_dp_port_sel(crtc)) {
c48b5305 4123 case PORT_B:
5eddb70b 4124 temp |= TRANS_DP_PORT_SEL_B;
c98e9dcf 4125 break;
c48b5305 4126 case PORT_C:
5eddb70b 4127 temp |= TRANS_DP_PORT_SEL_C;
c98e9dcf 4128 break;
c48b5305 4129 case PORT_D:
5eddb70b 4130 temp |= TRANS_DP_PORT_SEL_D;
c98e9dcf
JB
4131 break;
4132 default:
e95d41e1 4133 BUG();
32f9d658 4134 }
2c07245f 4135
5eddb70b 4136 I915_WRITE(reg, temp);
6be4a607 4137 }
b52eb4dc 4138
b8a4f404 4139 ironlake_enable_pch_transcoder(dev_priv, pipe);
f67a559d
JB
4140}
4141
1507e5bd
PZ
4142static void lpt_pch_enable(struct drm_crtc *crtc)
4143{
4144 struct drm_device *dev = crtc->dev;
fac5e23e 4145 struct drm_i915_private *dev_priv = to_i915(dev);
1507e5bd 4146 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6e3c9717 4147 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
1507e5bd 4148
ab9412ba 4149 assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
1507e5bd 4150
8c52b5e8 4151 lpt_program_iclkip(crtc);
1507e5bd 4152
0540e488 4153 /* Set transcoder timing. */
275f01b2 4154 ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
1507e5bd 4155
937bb610 4156 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
f67a559d
JB
4157}
4158
a1520318 4159static void cpt_verify_modeset(struct drm_device *dev, int pipe)
d4270e57 4160{
fac5e23e 4161 struct drm_i915_private *dev_priv = to_i915(dev);
f0f59a00 4162 i915_reg_t dslreg = PIPEDSL(pipe);
d4270e57
JB
4163 u32 temp;
4164
4165 temp = I915_READ(dslreg);
4166 udelay(500);
4167 if (wait_for(I915_READ(dslreg) != temp, 5)) {
d4270e57 4168 if (wait_for(I915_READ(dslreg) != temp, 5))
84f44ce7 4169 DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
d4270e57
JB
4170 }
4171}
4172
86adf9d7
ML
4173static int
4174skl_update_scaler(struct intel_crtc_state *crtc_state, bool force_detach,
4175 unsigned scaler_user, int *scaler_id, unsigned int rotation,
4176 int src_w, int src_h, int dst_w, int dst_h)
a1b2278e 4177{
86adf9d7
ML
4178 struct intel_crtc_scaler_state *scaler_state =
4179 &crtc_state->scaler_state;
4180 struct intel_crtc *intel_crtc =
4181 to_intel_crtc(crtc_state->base.crtc);
a1b2278e 4182 int need_scaling;
6156a456
CK
4183
4184 need_scaling = intel_rotation_90_or_270(rotation) ?
4185 (src_h != dst_w || src_w != dst_h):
4186 (src_w != dst_w || src_h != dst_h);
a1b2278e
CK
4187
4188 /*
4189 * if plane is being disabled or scaler is no more required or force detach
4190 * - free scaler binded to this plane/crtc
4191 * - in order to do this, update crtc->scaler_usage
4192 *
4193 * Here scaler state in crtc_state is set free so that
4194 * scaler can be assigned to other user. Actual register
4195 * update to free the scaler is done in plane/panel-fit programming.
4196 * For this purpose crtc/plane_state->scaler_id isn't reset here.
4197 */
86adf9d7 4198 if (force_detach || !need_scaling) {
a1b2278e 4199 if (*scaler_id >= 0) {
86adf9d7 4200 scaler_state->scaler_users &= ~(1 << scaler_user);
a1b2278e
CK
4201 scaler_state->scalers[*scaler_id].in_use = 0;
4202
86adf9d7
ML
4203 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4204 "Staged freeing scaler id %d scaler_users = 0x%x\n",
4205 intel_crtc->pipe, scaler_user, *scaler_id,
a1b2278e
CK
4206 scaler_state->scaler_users);
4207 *scaler_id = -1;
4208 }
4209 return 0;
4210 }
4211
4212 /* range checks */
4213 if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
4214 dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
4215
4216 src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
4217 dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
86adf9d7 4218 DRM_DEBUG_KMS("scaler_user index %u.%u: src %ux%u dst %ux%u "
a1b2278e 4219 "size is out of scaler range\n",
86adf9d7 4220 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h);
a1b2278e
CK
4221 return -EINVAL;
4222 }
4223
86adf9d7
ML
4224 /* mark this plane as a scaler user in crtc_state */
4225 scaler_state->scaler_users |= (1 << scaler_user);
4226 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4227 "staged scaling request for %ux%u->%ux%u scaler_users = 0x%x\n",
4228 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h,
4229 scaler_state->scaler_users);
4230
4231 return 0;
4232}
4233
4234/**
4235 * skl_update_scaler_crtc - Stages update to scaler state for a given crtc.
4236 *
4237 * @state: crtc's scaler state
86adf9d7
ML
4238 *
4239 * Return
4240 * 0 - scaler_usage updated successfully
4241 * error - requested scaling cannot be supported or other error condition
4242 */
e435d6e5 4243int skl_update_scaler_crtc(struct intel_crtc_state *state)
86adf9d7
ML
4244{
4245 struct intel_crtc *intel_crtc = to_intel_crtc(state->base.crtc);
7c5f93b0 4246 const struct drm_display_mode *adjusted_mode = &state->base.adjusted_mode;
86adf9d7 4247
78108b7c
VS
4248 DRM_DEBUG_KMS("Updating scaler for [CRTC:%d:%s] scaler_user index %u.%u\n",
4249 intel_crtc->base.base.id, intel_crtc->base.name,
4250 intel_crtc->pipe, SKL_CRTC_INDEX);
86adf9d7 4251
e435d6e5 4252 return skl_update_scaler(state, !state->base.active, SKL_CRTC_INDEX,
fa5a7970 4253 &state->scaler_state.scaler_id, BIT(DRM_ROTATE_0),
86adf9d7 4254 state->pipe_src_w, state->pipe_src_h,
aad941d5 4255 adjusted_mode->crtc_hdisplay, adjusted_mode->crtc_vdisplay);
86adf9d7
ML
4256}
4257
4258/**
4259 * skl_update_scaler_plane - Stages update to scaler state for a given plane.
4260 *
4261 * @state: crtc's scaler state
86adf9d7
ML
4262 * @plane_state: atomic plane state to update
4263 *
4264 * Return
4265 * 0 - scaler_usage updated successfully
4266 * error - requested scaling cannot be supported or other error condition
4267 */
da20eabd
ML
4268static int skl_update_scaler_plane(struct intel_crtc_state *crtc_state,
4269 struct intel_plane_state *plane_state)
86adf9d7
ML
4270{
4271
4272 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
da20eabd
ML
4273 struct intel_plane *intel_plane =
4274 to_intel_plane(plane_state->base.plane);
86adf9d7
ML
4275 struct drm_framebuffer *fb = plane_state->base.fb;
4276 int ret;
4277
4278 bool force_detach = !fb || !plane_state->visible;
4279
72660ce0
VS
4280 DRM_DEBUG_KMS("Updating scaler for [PLANE:%d:%s] scaler_user index %u.%u\n",
4281 intel_plane->base.base.id, intel_plane->base.name,
4282 intel_crtc->pipe, drm_plane_index(&intel_plane->base));
86adf9d7
ML
4283
4284 ret = skl_update_scaler(crtc_state, force_detach,
4285 drm_plane_index(&intel_plane->base),
4286 &plane_state->scaler_id,
4287 plane_state->base.rotation,
4288 drm_rect_width(&plane_state->src) >> 16,
4289 drm_rect_height(&plane_state->src) >> 16,
4290 drm_rect_width(&plane_state->dst),
4291 drm_rect_height(&plane_state->dst));
4292
4293 if (ret || plane_state->scaler_id < 0)
4294 return ret;
4295
a1b2278e 4296 /* check colorkey */
818ed961 4297 if (plane_state->ckey.flags != I915_SET_COLORKEY_NONE) {
72660ce0
VS
4298 DRM_DEBUG_KMS("[PLANE:%d:%s] scaling with color key not allowed",
4299 intel_plane->base.base.id,
4300 intel_plane->base.name);
a1b2278e
CK
4301 return -EINVAL;
4302 }
4303
4304 /* Check src format */
86adf9d7
ML
4305 switch (fb->pixel_format) {
4306 case DRM_FORMAT_RGB565:
4307 case DRM_FORMAT_XBGR8888:
4308 case DRM_FORMAT_XRGB8888:
4309 case DRM_FORMAT_ABGR8888:
4310 case DRM_FORMAT_ARGB8888:
4311 case DRM_FORMAT_XRGB2101010:
4312 case DRM_FORMAT_XBGR2101010:
4313 case DRM_FORMAT_YUYV:
4314 case DRM_FORMAT_YVYU:
4315 case DRM_FORMAT_UYVY:
4316 case DRM_FORMAT_VYUY:
4317 break;
4318 default:
72660ce0
VS
4319 DRM_DEBUG_KMS("[PLANE:%d:%s] FB:%d unsupported scaling format 0x%x\n",
4320 intel_plane->base.base.id, intel_plane->base.name,
4321 fb->base.id, fb->pixel_format);
86adf9d7 4322 return -EINVAL;
a1b2278e
CK
4323 }
4324
a1b2278e
CK
4325 return 0;
4326}
4327
e435d6e5
ML
4328static void skylake_scaler_disable(struct intel_crtc *crtc)
4329{
4330 int i;
4331
4332 for (i = 0; i < crtc->num_scalers; i++)
4333 skl_detach_scaler(crtc, i);
4334}
4335
4336static void skylake_pfit_enable(struct intel_crtc *crtc)
bd2e244f
JB
4337{
4338 struct drm_device *dev = crtc->base.dev;
fac5e23e 4339 struct drm_i915_private *dev_priv = to_i915(dev);
bd2e244f 4340 int pipe = crtc->pipe;
a1b2278e
CK
4341 struct intel_crtc_scaler_state *scaler_state =
4342 &crtc->config->scaler_state;
4343
4344 DRM_DEBUG_KMS("for crtc_state = %p\n", crtc->config);
4345
6e3c9717 4346 if (crtc->config->pch_pfit.enabled) {
a1b2278e
CK
4347 int id;
4348
4349 if (WARN_ON(crtc->config->scaler_state.scaler_id < 0)) {
4350 DRM_ERROR("Requesting pfit without getting a scaler first\n");
4351 return;
4352 }
4353
4354 id = scaler_state->scaler_id;
4355 I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
4356 PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
4357 I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
4358 I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
4359
4360 DRM_DEBUG_KMS("for crtc_state = %p scaler_id = %d\n", crtc->config, id);
bd2e244f
JB
4361 }
4362}
4363
b074cec8
JB
4364static void ironlake_pfit_enable(struct intel_crtc *crtc)
4365{
4366 struct drm_device *dev = crtc->base.dev;
fac5e23e 4367 struct drm_i915_private *dev_priv = to_i915(dev);
b074cec8
JB
4368 int pipe = crtc->pipe;
4369
6e3c9717 4370 if (crtc->config->pch_pfit.enabled) {
b074cec8
JB
4371 /* Force use of hard-coded filter coefficients
4372 * as some pre-programmed values are broken,
4373 * e.g. x201.
4374 */
4375 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
4376 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
4377 PF_PIPE_SEL_IVB(pipe));
4378 else
4379 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
6e3c9717
ACO
4380 I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
4381 I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
d4270e57
JB
4382 }
4383}
4384
20bc8673 4385void hsw_enable_ips(struct intel_crtc *crtc)
d77e4531 4386{
cea165c3 4387 struct drm_device *dev = crtc->base.dev;
fac5e23e 4388 struct drm_i915_private *dev_priv = to_i915(dev);
d77e4531 4389
6e3c9717 4390 if (!crtc->config->ips_enabled)
d77e4531
PZ
4391 return;
4392
307e4498
ML
4393 /*
4394 * We can only enable IPS after we enable a plane and wait for a vblank
4395 * This function is called from post_plane_update, which is run after
4396 * a vblank wait.
4397 */
cea165c3 4398
d77e4531 4399 assert_plane_enabled(dev_priv, crtc->plane);
cea165c3 4400 if (IS_BROADWELL(dev)) {
2a114cc1
BW
4401 mutex_lock(&dev_priv->rps.hw_lock);
4402 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
4403 mutex_unlock(&dev_priv->rps.hw_lock);
4404 /* Quoting Art Runyan: "its not safe to expect any particular
4405 * value in IPS_CTL bit 31 after enabling IPS through the
e59150dc
JB
4406 * mailbox." Moreover, the mailbox may return a bogus state,
4407 * so we need to just enable it and continue on.
2a114cc1
BW
4408 */
4409 } else {
4410 I915_WRITE(IPS_CTL, IPS_ENABLE);
4411 /* The bit only becomes 1 in the next vblank, so this wait here
4412 * is essentially intel_wait_for_vblank. If we don't have this
4413 * and don't wait for vblanks until the end of crtc_enable, then
4414 * the HW state readout code will complain that the expected
4415 * IPS_CTL value is not the one we read. */
2ec9ba3c
CW
4416 if (intel_wait_for_register(dev_priv,
4417 IPS_CTL, IPS_ENABLE, IPS_ENABLE,
4418 50))
2a114cc1
BW
4419 DRM_ERROR("Timed out waiting for IPS enable\n");
4420 }
d77e4531
PZ
4421}
4422
20bc8673 4423void hsw_disable_ips(struct intel_crtc *crtc)
d77e4531
PZ
4424{
4425 struct drm_device *dev = crtc->base.dev;
fac5e23e 4426 struct drm_i915_private *dev_priv = to_i915(dev);
d77e4531 4427
6e3c9717 4428 if (!crtc->config->ips_enabled)
d77e4531
PZ
4429 return;
4430
4431 assert_plane_enabled(dev_priv, crtc->plane);
23d0b130 4432 if (IS_BROADWELL(dev)) {
2a114cc1
BW
4433 mutex_lock(&dev_priv->rps.hw_lock);
4434 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
4435 mutex_unlock(&dev_priv->rps.hw_lock);
23d0b130 4436 /* wait for pcode to finish disabling IPS, which may take up to 42ms */
b85c1ecf
CW
4437 if (intel_wait_for_register(dev_priv,
4438 IPS_CTL, IPS_ENABLE, 0,
4439 42))
23d0b130 4440 DRM_ERROR("Timed out waiting for IPS disable\n");
e59150dc 4441 } else {
2a114cc1 4442 I915_WRITE(IPS_CTL, 0);
e59150dc
JB
4443 POSTING_READ(IPS_CTL);
4444 }
d77e4531
PZ
4445
4446 /* We need to wait for a vblank before we can disable the plane. */
4447 intel_wait_for_vblank(dev, crtc->pipe);
4448}
4449
7cac945f 4450static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
d3eedb1a 4451{
7cac945f 4452 if (intel_crtc->overlay) {
d3eedb1a 4453 struct drm_device *dev = intel_crtc->base.dev;
fac5e23e 4454 struct drm_i915_private *dev_priv = to_i915(dev);
d3eedb1a
VS
4455
4456 mutex_lock(&dev->struct_mutex);
4457 dev_priv->mm.interruptible = false;
4458 (void) intel_overlay_switch_off(intel_crtc->overlay);
4459 dev_priv->mm.interruptible = true;
4460 mutex_unlock(&dev->struct_mutex);
4461 }
4462
4463 /* Let userspace switch the overlay on again. In most cases userspace
4464 * has to recompute where to put it anyway.
4465 */
4466}
4467
87d4300a
ML
4468/**
4469 * intel_post_enable_primary - Perform operations after enabling primary plane
4470 * @crtc: the CRTC whose primary plane was just enabled
4471 *
4472 * Performs potentially sleeping operations that must be done after the primary
4473 * plane is enabled, such as updating FBC and IPS. Note that this may be
4474 * called due to an explicit primary plane update, or due to an implicit
4475 * re-enable that is caused when a sprite plane is updated to no longer
4476 * completely hide the primary plane.
4477 */
4478static void
4479intel_post_enable_primary(struct drm_crtc *crtc)
a5c4d7bc
VS
4480{
4481 struct drm_device *dev = crtc->dev;
fac5e23e 4482 struct drm_i915_private *dev_priv = to_i915(dev);
a5c4d7bc
VS
4483 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4484 int pipe = intel_crtc->pipe;
a5c4d7bc 4485
87d4300a
ML
4486 /*
4487 * FIXME IPS should be fine as long as one plane is
4488 * enabled, but in practice it seems to have problems
4489 * when going from primary only to sprite only and vice
4490 * versa.
4491 */
a5c4d7bc
VS
4492 hsw_enable_ips(intel_crtc);
4493
f99d7069 4494 /*
87d4300a
ML
4495 * Gen2 reports pipe underruns whenever all planes are disabled.
4496 * So don't enable underrun reporting before at least some planes
4497 * are enabled.
4498 * FIXME: Need to fix the logic to work when we turn off all planes
4499 * but leave the pipe running.
f99d7069 4500 */
87d4300a
ML
4501 if (IS_GEN2(dev))
4502 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4503
aca7b684
VS
4504 /* Underruns don't always raise interrupts, so check manually. */
4505 intel_check_cpu_fifo_underruns(dev_priv);
4506 intel_check_pch_fifo_underruns(dev_priv);
a5c4d7bc
VS
4507}
4508
2622a081 4509/* FIXME move all this to pre_plane_update() with proper state tracking */
87d4300a
ML
4510static void
4511intel_pre_disable_primary(struct drm_crtc *crtc)
a5c4d7bc
VS
4512{
4513 struct drm_device *dev = crtc->dev;
fac5e23e 4514 struct drm_i915_private *dev_priv = to_i915(dev);
a5c4d7bc
VS
4515 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4516 int pipe = intel_crtc->pipe;
a5c4d7bc 4517
87d4300a
ML
4518 /*
4519 * Gen2 reports pipe underruns whenever all planes are disabled.
4520 * So diasble underrun reporting before all the planes get disabled.
4521 * FIXME: Need to fix the logic to work when we turn off all planes
4522 * but leave the pipe running.
4523 */
4524 if (IS_GEN2(dev))
4525 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
a5c4d7bc 4526
2622a081
VS
4527 /*
4528 * FIXME IPS should be fine as long as one plane is
4529 * enabled, but in practice it seems to have problems
4530 * when going from primary only to sprite only and vice
4531 * versa.
4532 */
4533 hsw_disable_ips(intel_crtc);
4534}
4535
4536/* FIXME get rid of this and use pre_plane_update */
4537static void
4538intel_pre_disable_primary_noatomic(struct drm_crtc *crtc)
4539{
4540 struct drm_device *dev = crtc->dev;
fac5e23e 4541 struct drm_i915_private *dev_priv = to_i915(dev);
2622a081
VS
4542 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4543 int pipe = intel_crtc->pipe;
4544
4545 intel_pre_disable_primary(crtc);
4546
87d4300a
ML
4547 /*
4548 * Vblank time updates from the shadow to live plane control register
4549 * are blocked if the memory self-refresh mode is active at that
4550 * moment. So to make sure the plane gets truly disabled, disable
4551 * first the self-refresh mode. The self-refresh enable bit in turn
4552 * will be checked/applied by the HW only at the next frame start
4553 * event which is after the vblank start event, so we need to have a
4554 * wait-for-vblank between disabling the plane and the pipe.
4555 */
262cd2e1 4556 if (HAS_GMCH_DISPLAY(dev)) {
87d4300a 4557 intel_set_memory_cxsr(dev_priv, false);
262cd2e1
VS
4558 dev_priv->wm.vlv.cxsr = false;
4559 intel_wait_for_vblank(dev, pipe);
4560 }
87d4300a
ML
4561}
4562
5a21b665
DV
4563static void intel_post_plane_update(struct intel_crtc_state *old_crtc_state)
4564{
4565 struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
4566 struct drm_atomic_state *old_state = old_crtc_state->base.state;
4567 struct intel_crtc_state *pipe_config =
4568 to_intel_crtc_state(crtc->base.state);
4569 struct drm_device *dev = crtc->base.dev;
4570 struct drm_plane *primary = crtc->base.primary;
4571 struct drm_plane_state *old_pri_state =
4572 drm_atomic_get_existing_plane_state(old_state, primary);
4573
4574 intel_frontbuffer_flip(dev, pipe_config->fb_bits);
4575
4576 crtc->wm.cxsr_allowed = true;
4577
4578 if (pipe_config->update_wm_post && pipe_config->base.active)
4579 intel_update_watermarks(&crtc->base);
4580
4581 if (old_pri_state) {
4582 struct intel_plane_state *primary_state =
4583 to_intel_plane_state(primary->state);
4584 struct intel_plane_state *old_primary_state =
4585 to_intel_plane_state(old_pri_state);
4586
4587 intel_fbc_post_update(crtc);
4588
4589 if (primary_state->visible &&
4590 (needs_modeset(&pipe_config->base) ||
4591 !old_primary_state->visible))
4592 intel_post_enable_primary(&crtc->base);
4593 }
4594}
4595
5c74cd73 4596static void intel_pre_plane_update(struct intel_crtc_state *old_crtc_state)
ac21b225 4597{
5c74cd73 4598 struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
ac21b225 4599 struct drm_device *dev = crtc->base.dev;
fac5e23e 4600 struct drm_i915_private *dev_priv = to_i915(dev);
ab1d3a0e
ML
4601 struct intel_crtc_state *pipe_config =
4602 to_intel_crtc_state(crtc->base.state);
5c74cd73
ML
4603 struct drm_atomic_state *old_state = old_crtc_state->base.state;
4604 struct drm_plane *primary = crtc->base.primary;
4605 struct drm_plane_state *old_pri_state =
4606 drm_atomic_get_existing_plane_state(old_state, primary);
4607 bool modeset = needs_modeset(&pipe_config->base);
ac21b225 4608
5c74cd73
ML
4609 if (old_pri_state) {
4610 struct intel_plane_state *primary_state =
4611 to_intel_plane_state(primary->state);
4612 struct intel_plane_state *old_primary_state =
4613 to_intel_plane_state(old_pri_state);
4614
faf68d92 4615 intel_fbc_pre_update(crtc, pipe_config, primary_state);
31ae71fc 4616
5c74cd73
ML
4617 if (old_primary_state->visible &&
4618 (modeset || !primary_state->visible))
4619 intel_pre_disable_primary(&crtc->base);
4620 }
852eb00d 4621
a4015f9a 4622 if (pipe_config->disable_cxsr && HAS_GMCH_DISPLAY(dev)) {
852eb00d 4623 crtc->wm.cxsr_allowed = false;
2dfd178d 4624
2622a081
VS
4625 /*
4626 * Vblank time updates from the shadow to live plane control register
4627 * are blocked if the memory self-refresh mode is active at that
4628 * moment. So to make sure the plane gets truly disabled, disable
4629 * first the self-refresh mode. The self-refresh enable bit in turn
4630 * will be checked/applied by the HW only at the next frame start
4631 * event which is after the vblank start event, so we need to have a
4632 * wait-for-vblank between disabling the plane and the pipe.
4633 */
4634 if (old_crtc_state->base.active) {
2dfd178d 4635 intel_set_memory_cxsr(dev_priv, false);
2622a081
VS
4636 dev_priv->wm.vlv.cxsr = false;
4637 intel_wait_for_vblank(dev, crtc->pipe);
4638 }
852eb00d 4639 }
92826fcd 4640
ed4a6a7c
MR
4641 /*
4642 * IVB workaround: must disable low power watermarks for at least
4643 * one frame before enabling scaling. LP watermarks can be re-enabled
4644 * when scaling is disabled.
4645 *
4646 * WaCxSRDisabledForSpriteScaling:ivb
4647 */
4648 if (pipe_config->disable_lp_wm) {
4649 ilk_disable_lp_wm(dev);
4650 intel_wait_for_vblank(dev, crtc->pipe);
4651 }
4652
4653 /*
4654 * If we're doing a modeset, we're done. No need to do any pre-vblank
4655 * watermark programming here.
4656 */
4657 if (needs_modeset(&pipe_config->base))
4658 return;
4659
4660 /*
4661 * For platforms that support atomic watermarks, program the
4662 * 'intermediate' watermarks immediately. On pre-gen9 platforms, these
4663 * will be the intermediate values that are safe for both pre- and
4664 * post- vblank; when vblank happens, the 'active' values will be set
4665 * to the final 'target' values and we'll do this again to get the
4666 * optimal watermarks. For gen9+ platforms, the values we program here
4667 * will be the final target values which will get automatically latched
4668 * at vblank time; no further programming will be necessary.
4669 *
4670 * If a platform hasn't been transitioned to atomic watermarks yet,
4671 * we'll continue to update watermarks the old way, if flags tell
4672 * us to.
4673 */
4674 if (dev_priv->display.initial_watermarks != NULL)
4675 dev_priv->display.initial_watermarks(pipe_config);
caed361d 4676 else if (pipe_config->update_wm_pre)
92826fcd 4677 intel_update_watermarks(&crtc->base);
ac21b225
ML
4678}
4679
d032ffa0 4680static void intel_crtc_disable_planes(struct drm_crtc *crtc, unsigned plane_mask)
87d4300a
ML
4681{
4682 struct drm_device *dev = crtc->dev;
4683 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
d032ffa0 4684 struct drm_plane *p;
87d4300a
ML
4685 int pipe = intel_crtc->pipe;
4686
7cac945f 4687 intel_crtc_dpms_overlay_disable(intel_crtc);
27321ae8 4688
d032ffa0
ML
4689 drm_for_each_plane_mask(p, dev, plane_mask)
4690 to_intel_plane(p)->disable_plane(p, crtc);
f98551ae 4691
f99d7069
DV
4692 /*
4693 * FIXME: Once we grow proper nuclear flip support out of this we need
4694 * to compute the mask of flip planes precisely. For the time being
4695 * consider this a flip to a NULL plane.
4696 */
4697 intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
a5c4d7bc
VS
4698}
4699
f67a559d
JB
4700static void ironlake_crtc_enable(struct drm_crtc *crtc)
4701{
4702 struct drm_device *dev = crtc->dev;
fac5e23e 4703 struct drm_i915_private *dev_priv = to_i915(dev);
f67a559d 4704 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 4705 struct intel_encoder *encoder;
f67a559d 4706 int pipe = intel_crtc->pipe;
b95c5321
ML
4707 struct intel_crtc_state *pipe_config =
4708 to_intel_crtc_state(crtc->state);
f67a559d 4709
53d9f4e9 4710 if (WARN_ON(intel_crtc->active))
f67a559d
JB
4711 return;
4712
b2c0593a
VS
4713 /*
4714 * Sometimes spurious CPU pipe underruns happen during FDI
4715 * training, at least with VGA+HDMI cloning. Suppress them.
4716 *
4717 * On ILK we get an occasional spurious CPU pipe underruns
4718 * between eDP port A enable and vdd enable. Also PCH port
4719 * enable seems to result in the occasional CPU pipe underrun.
4720 *
4721 * Spurious PCH underruns also occur during PCH enabling.
4722 */
4723 if (intel_crtc->config->has_pch_encoder || IS_GEN5(dev_priv))
4724 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
81b088ca
VS
4725 if (intel_crtc->config->has_pch_encoder)
4726 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
4727
6e3c9717 4728 if (intel_crtc->config->has_pch_encoder)
b14b1055
DV
4729 intel_prepare_shared_dpll(intel_crtc);
4730
37a5650b 4731 if (intel_crtc_has_dp_encoder(intel_crtc->config))
fe3cd48d 4732 intel_dp_set_m_n(intel_crtc, M1_N1);
29407aab
DV
4733
4734 intel_set_pipe_timings(intel_crtc);
bc58be60 4735 intel_set_pipe_src_size(intel_crtc);
29407aab 4736
6e3c9717 4737 if (intel_crtc->config->has_pch_encoder) {
29407aab 4738 intel_cpu_transcoder_set_m_n(intel_crtc,
6e3c9717 4739 &intel_crtc->config->fdi_m_n, NULL);
29407aab
DV
4740 }
4741
4742 ironlake_set_pipeconf(crtc);
4743
f67a559d 4744 intel_crtc->active = true;
8664281b 4745
f6736a1a 4746 for_each_encoder_on_crtc(dev, crtc, encoder)
952735ee
DV
4747 if (encoder->pre_enable)
4748 encoder->pre_enable(encoder);
f67a559d 4749
6e3c9717 4750 if (intel_crtc->config->has_pch_encoder) {
fff367c7
DV
4751 /* Note: FDI PLL enabling _must_ be done before we enable the
4752 * cpu pipes, hence this is separate from all the other fdi/pch
4753 * enabling. */
88cefb6c 4754 ironlake_fdi_pll_enable(intel_crtc);
46b6f814
DV
4755 } else {
4756 assert_fdi_tx_disabled(dev_priv, pipe);
4757 assert_fdi_rx_disabled(dev_priv, pipe);
4758 }
f67a559d 4759
b074cec8 4760 ironlake_pfit_enable(intel_crtc);
f67a559d 4761
9c54c0dd
JB
4762 /*
4763 * On ILK+ LUT must be loaded before the pipe is running but with
4764 * clocks enabled
4765 */
b95c5321 4766 intel_color_load_luts(&pipe_config->base);
9c54c0dd 4767
1d5bf5d9
ID
4768 if (dev_priv->display.initial_watermarks != NULL)
4769 dev_priv->display.initial_watermarks(intel_crtc->config);
e1fdc473 4770 intel_enable_pipe(intel_crtc);
f67a559d 4771
6e3c9717 4772 if (intel_crtc->config->has_pch_encoder)
f67a559d 4773 ironlake_pch_enable(crtc);
c98e9dcf 4774
f9b61ff6
DV
4775 assert_vblank_disabled(crtc);
4776 drm_crtc_vblank_on(crtc);
4777
fa5c73b1
DV
4778 for_each_encoder_on_crtc(dev, crtc, encoder)
4779 encoder->enable(encoder);
61b77ddd
DV
4780
4781 if (HAS_PCH_CPT(dev))
a1520318 4782 cpt_verify_modeset(dev, intel_crtc->pipe);
37ca8d4c
VS
4783
4784 /* Must wait for vblank to avoid spurious PCH FIFO underruns */
4785 if (intel_crtc->config->has_pch_encoder)
4786 intel_wait_for_vblank(dev, pipe);
b2c0593a 4787 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
37ca8d4c 4788 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
6be4a607
JB
4789}
4790
42db64ef
PZ
4791/* IPS only exists on ULT machines and is tied to pipe A. */
4792static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
4793{
f5adf94e 4794 return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
42db64ef
PZ
4795}
4796
4f771f10
PZ
4797static void haswell_crtc_enable(struct drm_crtc *crtc)
4798{
4799 struct drm_device *dev = crtc->dev;
fac5e23e 4800 struct drm_i915_private *dev_priv = to_i915(dev);
4f771f10
PZ
4801 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4802 struct intel_encoder *encoder;
99d736a2 4803 int pipe = intel_crtc->pipe, hsw_workaround_pipe;
4d1de975 4804 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
99d736a2
ML
4805 struct intel_crtc_state *pipe_config =
4806 to_intel_crtc_state(crtc->state);
4f771f10 4807
53d9f4e9 4808 if (WARN_ON(intel_crtc->active))
4f771f10
PZ
4809 return;
4810
81b088ca
VS
4811 if (intel_crtc->config->has_pch_encoder)
4812 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
4813 false);
4814
95a7a2ae
ID
4815 for_each_encoder_on_crtc(dev, crtc, encoder)
4816 if (encoder->pre_pll_enable)
4817 encoder->pre_pll_enable(encoder);
4818
8106ddbd 4819 if (intel_crtc->config->shared_dpll)
df8ad70c
DV
4820 intel_enable_shared_dpll(intel_crtc);
4821
37a5650b 4822 if (intel_crtc_has_dp_encoder(intel_crtc->config))
fe3cd48d 4823 intel_dp_set_m_n(intel_crtc, M1_N1);
229fca97 4824
d7edc4e5 4825 if (!transcoder_is_dsi(cpu_transcoder))
4d1de975
JN
4826 intel_set_pipe_timings(intel_crtc);
4827
bc58be60 4828 intel_set_pipe_src_size(intel_crtc);
229fca97 4829
4d1de975
JN
4830 if (cpu_transcoder != TRANSCODER_EDP &&
4831 !transcoder_is_dsi(cpu_transcoder)) {
4832 I915_WRITE(PIPE_MULT(cpu_transcoder),
6e3c9717 4833 intel_crtc->config->pixel_multiplier - 1);
ebb69c95
CT
4834 }
4835
6e3c9717 4836 if (intel_crtc->config->has_pch_encoder) {
229fca97 4837 intel_cpu_transcoder_set_m_n(intel_crtc,
6e3c9717 4838 &intel_crtc->config->fdi_m_n, NULL);
229fca97
DV
4839 }
4840
d7edc4e5 4841 if (!transcoder_is_dsi(cpu_transcoder))
4d1de975
JN
4842 haswell_set_pipeconf(crtc);
4843
391bf048 4844 haswell_set_pipemisc(crtc);
229fca97 4845
b95c5321 4846 intel_color_set_csc(&pipe_config->base);
229fca97 4847
4f771f10 4848 intel_crtc->active = true;
8664281b 4849
6b698516
DV
4850 if (intel_crtc->config->has_pch_encoder)
4851 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
4852 else
4853 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4854
7d4aefd0 4855 for_each_encoder_on_crtc(dev, crtc, encoder) {
4f771f10
PZ
4856 if (encoder->pre_enable)
4857 encoder->pre_enable(encoder);
7d4aefd0 4858 }
4f771f10 4859
d2d65408 4860 if (intel_crtc->config->has_pch_encoder)
4fe9467d 4861 dev_priv->display.fdi_link_train(crtc);
4fe9467d 4862
d7edc4e5 4863 if (!transcoder_is_dsi(cpu_transcoder))
7d4aefd0 4864 intel_ddi_enable_pipe_clock(intel_crtc);
4f771f10 4865
1c132b44 4866 if (INTEL_INFO(dev)->gen >= 9)
e435d6e5 4867 skylake_pfit_enable(intel_crtc);
ff6d9f55 4868 else
1c132b44 4869 ironlake_pfit_enable(intel_crtc);
4f771f10
PZ
4870
4871 /*
4872 * On ILK+ LUT must be loaded before the pipe is running but with
4873 * clocks enabled
4874 */
b95c5321 4875 intel_color_load_luts(&pipe_config->base);
4f771f10 4876
1f544388 4877 intel_ddi_set_pipe_settings(crtc);
d7edc4e5 4878 if (!transcoder_is_dsi(cpu_transcoder))
7d4aefd0 4879 intel_ddi_enable_transcoder_func(crtc);
4f771f10 4880
1d5bf5d9
ID
4881 if (dev_priv->display.initial_watermarks != NULL)
4882 dev_priv->display.initial_watermarks(pipe_config);
4883 else
4884 intel_update_watermarks(crtc);
4d1de975
JN
4885
4886 /* XXX: Do the pipe assertions at the right place for BXT DSI. */
d7edc4e5 4887 if (!transcoder_is_dsi(cpu_transcoder))
4d1de975 4888 intel_enable_pipe(intel_crtc);
42db64ef 4889
6e3c9717 4890 if (intel_crtc->config->has_pch_encoder)
1507e5bd 4891 lpt_pch_enable(crtc);
4f771f10 4892
a65347ba 4893 if (intel_crtc->config->dp_encoder_is_mst)
0e32b39c
DA
4894 intel_ddi_set_vc_payload_alloc(crtc, true);
4895
f9b61ff6
DV
4896 assert_vblank_disabled(crtc);
4897 drm_crtc_vblank_on(crtc);
4898
8807e55b 4899 for_each_encoder_on_crtc(dev, crtc, encoder) {
4f771f10 4900 encoder->enable(encoder);
8807e55b
JN
4901 intel_opregion_notify_encoder(encoder, true);
4902 }
4f771f10 4903
6b698516
DV
4904 if (intel_crtc->config->has_pch_encoder) {
4905 intel_wait_for_vblank(dev, pipe);
4906 intel_wait_for_vblank(dev, pipe);
4907 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
d2d65408
VS
4908 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
4909 true);
6b698516 4910 }
d2d65408 4911
e4916946
PZ
4912 /* If we change the relative order between pipe/planes enabling, we need
4913 * to change the workaround. */
99d736a2
ML
4914 hsw_workaround_pipe = pipe_config->hsw_workaround_pipe;
4915 if (IS_HASWELL(dev) && hsw_workaround_pipe != INVALID_PIPE) {
4916 intel_wait_for_vblank(dev, hsw_workaround_pipe);
4917 intel_wait_for_vblank(dev, hsw_workaround_pipe);
4918 }
4f771f10
PZ
4919}
4920
bfd16b2a 4921static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force)
3f8dce3a
DV
4922{
4923 struct drm_device *dev = crtc->base.dev;
fac5e23e 4924 struct drm_i915_private *dev_priv = to_i915(dev);
3f8dce3a
DV
4925 int pipe = crtc->pipe;
4926
4927 /* To avoid upsetting the power well on haswell only disable the pfit if
4928 * it's in use. The hw state code will make sure we get this right. */
bfd16b2a 4929 if (force || crtc->config->pch_pfit.enabled) {
3f8dce3a
DV
4930 I915_WRITE(PF_CTL(pipe), 0);
4931 I915_WRITE(PF_WIN_POS(pipe), 0);
4932 I915_WRITE(PF_WIN_SZ(pipe), 0);
4933 }
4934}
4935
6be4a607
JB
4936static void ironlake_crtc_disable(struct drm_crtc *crtc)
4937{
4938 struct drm_device *dev = crtc->dev;
fac5e23e 4939 struct drm_i915_private *dev_priv = to_i915(dev);
6be4a607 4940 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 4941 struct intel_encoder *encoder;
6be4a607 4942 int pipe = intel_crtc->pipe;
b52eb4dc 4943
b2c0593a
VS
4944 /*
4945 * Sometimes spurious CPU pipe underruns happen when the
4946 * pipe is already disabled, but FDI RX/TX is still enabled.
4947 * Happens at least with VGA+HDMI cloning. Suppress them.
4948 */
4949 if (intel_crtc->config->has_pch_encoder) {
4950 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
37ca8d4c 4951 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
b2c0593a 4952 }
37ca8d4c 4953
ea9d758d
DV
4954 for_each_encoder_on_crtc(dev, crtc, encoder)
4955 encoder->disable(encoder);
4956
f9b61ff6
DV
4957 drm_crtc_vblank_off(crtc);
4958 assert_vblank_disabled(crtc);
4959
575f7ab7 4960 intel_disable_pipe(intel_crtc);
32f9d658 4961
bfd16b2a 4962 ironlake_pfit_disable(intel_crtc, false);
2c07245f 4963
b2c0593a 4964 if (intel_crtc->config->has_pch_encoder)
5a74f70a
VS
4965 ironlake_fdi_disable(crtc);
4966
bf49ec8c
DV
4967 for_each_encoder_on_crtc(dev, crtc, encoder)
4968 if (encoder->post_disable)
4969 encoder->post_disable(encoder);
2c07245f 4970
6e3c9717 4971 if (intel_crtc->config->has_pch_encoder) {
d925c59a 4972 ironlake_disable_pch_transcoder(dev_priv, pipe);
6be4a607 4973
d925c59a 4974 if (HAS_PCH_CPT(dev)) {
f0f59a00
VS
4975 i915_reg_t reg;
4976 u32 temp;
4977
d925c59a
DV
4978 /* disable TRANS_DP_CTL */
4979 reg = TRANS_DP_CTL(pipe);
4980 temp = I915_READ(reg);
4981 temp &= ~(TRANS_DP_OUTPUT_ENABLE |
4982 TRANS_DP_PORT_SEL_MASK);
4983 temp |= TRANS_DP_PORT_SEL_NONE;
4984 I915_WRITE(reg, temp);
4985
4986 /* disable DPLL_SEL */
4987 temp = I915_READ(PCH_DPLL_SEL);
11887397 4988 temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
d925c59a 4989 I915_WRITE(PCH_DPLL_SEL, temp);
9db4a9c7 4990 }
e3421a18 4991
d925c59a
DV
4992 ironlake_fdi_pll_disable(intel_crtc);
4993 }
81b088ca 4994
b2c0593a 4995 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
81b088ca 4996 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
6be4a607 4997}
1b3c7a47 4998
4f771f10 4999static void haswell_crtc_disable(struct drm_crtc *crtc)
ee7b9f93 5000{
4f771f10 5001 struct drm_device *dev = crtc->dev;
fac5e23e 5002 struct drm_i915_private *dev_priv = to_i915(dev);
ee7b9f93 5003 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4f771f10 5004 struct intel_encoder *encoder;
6e3c9717 5005 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
ee7b9f93 5006
d2d65408
VS
5007 if (intel_crtc->config->has_pch_encoder)
5008 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5009 false);
5010
8807e55b
JN
5011 for_each_encoder_on_crtc(dev, crtc, encoder) {
5012 intel_opregion_notify_encoder(encoder, false);
4f771f10 5013 encoder->disable(encoder);
8807e55b 5014 }
4f771f10 5015
f9b61ff6
DV
5016 drm_crtc_vblank_off(crtc);
5017 assert_vblank_disabled(crtc);
5018
4d1de975 5019 /* XXX: Do the pipe assertions at the right place for BXT DSI. */
d7edc4e5 5020 if (!transcoder_is_dsi(cpu_transcoder))
4d1de975 5021 intel_disable_pipe(intel_crtc);
4f771f10 5022
6e3c9717 5023 if (intel_crtc->config->dp_encoder_is_mst)
a4bf214f
VS
5024 intel_ddi_set_vc_payload_alloc(crtc, false);
5025
d7edc4e5 5026 if (!transcoder_is_dsi(cpu_transcoder))
7d4aefd0 5027 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
4f771f10 5028
1c132b44 5029 if (INTEL_INFO(dev)->gen >= 9)
e435d6e5 5030 skylake_scaler_disable(intel_crtc);
ff6d9f55 5031 else
bfd16b2a 5032 ironlake_pfit_disable(intel_crtc, false);
4f771f10 5033
d7edc4e5 5034 if (!transcoder_is_dsi(cpu_transcoder))
7d4aefd0 5035 intel_ddi_disable_pipe_clock(intel_crtc);
4f771f10 5036
97b040aa
ID
5037 for_each_encoder_on_crtc(dev, crtc, encoder)
5038 if (encoder->post_disable)
5039 encoder->post_disable(encoder);
81b088ca 5040
92966a37
VS
5041 if (intel_crtc->config->has_pch_encoder) {
5042 lpt_disable_pch_transcoder(dev_priv);
503a74e9 5043 lpt_disable_iclkip(dev_priv);
92966a37
VS
5044 intel_ddi_fdi_disable(crtc);
5045
81b088ca
VS
5046 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5047 true);
92966a37 5048 }
4f771f10
PZ
5049}
5050
2dd24552
JB
5051static void i9xx_pfit_enable(struct intel_crtc *crtc)
5052{
5053 struct drm_device *dev = crtc->base.dev;
fac5e23e 5054 struct drm_i915_private *dev_priv = to_i915(dev);
6e3c9717 5055 struct intel_crtc_state *pipe_config = crtc->config;
2dd24552 5056
681a8504 5057 if (!pipe_config->gmch_pfit.control)
2dd24552
JB
5058 return;
5059
2dd24552 5060 /*
c0b03411
DV
5061 * The panel fitter should only be adjusted whilst the pipe is disabled,
5062 * according to register description and PRM.
2dd24552 5063 */
c0b03411
DV
5064 WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
5065 assert_pipe_disabled(dev_priv, crtc->pipe);
2dd24552 5066
b074cec8
JB
5067 I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
5068 I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
5a80c45c
DV
5069
5070 /* Border color in case we don't scale up to the full screen. Black by
5071 * default, change to something else for debugging. */
5072 I915_WRITE(BCLRPAT(crtc->pipe), 0);
2dd24552
JB
5073}
5074
d05410f9
DA
5075static enum intel_display_power_domain port_to_power_domain(enum port port)
5076{
5077 switch (port) {
5078 case PORT_A:
6331a704 5079 return POWER_DOMAIN_PORT_DDI_A_LANES;
d05410f9 5080 case PORT_B:
6331a704 5081 return POWER_DOMAIN_PORT_DDI_B_LANES;
d05410f9 5082 case PORT_C:
6331a704 5083 return POWER_DOMAIN_PORT_DDI_C_LANES;
d05410f9 5084 case PORT_D:
6331a704 5085 return POWER_DOMAIN_PORT_DDI_D_LANES;
d8e19f99 5086 case PORT_E:
6331a704 5087 return POWER_DOMAIN_PORT_DDI_E_LANES;
d05410f9 5088 default:
b9fec167 5089 MISSING_CASE(port);
d05410f9
DA
5090 return POWER_DOMAIN_PORT_OTHER;
5091 }
5092}
5093
25f78f58
VS
5094static enum intel_display_power_domain port_to_aux_power_domain(enum port port)
5095{
5096 switch (port) {
5097 case PORT_A:
5098 return POWER_DOMAIN_AUX_A;
5099 case PORT_B:
5100 return POWER_DOMAIN_AUX_B;
5101 case PORT_C:
5102 return POWER_DOMAIN_AUX_C;
5103 case PORT_D:
5104 return POWER_DOMAIN_AUX_D;
5105 case PORT_E:
5106 /* FIXME: Check VBT for actual wiring of PORT E */
5107 return POWER_DOMAIN_AUX_D;
5108 default:
b9fec167 5109 MISSING_CASE(port);
25f78f58
VS
5110 return POWER_DOMAIN_AUX_A;
5111 }
5112}
5113
319be8ae
ID
5114enum intel_display_power_domain
5115intel_display_port_power_domain(struct intel_encoder *intel_encoder)
5116{
5117 struct drm_device *dev = intel_encoder->base.dev;
5118 struct intel_digital_port *intel_dig_port;
5119
5120 switch (intel_encoder->type) {
5121 case INTEL_OUTPUT_UNKNOWN:
5122 /* Only DDI platforms should ever use this output type */
5123 WARN_ON_ONCE(!HAS_DDI(dev));
cca0502b 5124 case INTEL_OUTPUT_DP:
319be8ae
ID
5125 case INTEL_OUTPUT_HDMI:
5126 case INTEL_OUTPUT_EDP:
5127 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
d05410f9 5128 return port_to_power_domain(intel_dig_port->port);
0e32b39c
DA
5129 case INTEL_OUTPUT_DP_MST:
5130 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
5131 return port_to_power_domain(intel_dig_port->port);
319be8ae
ID
5132 case INTEL_OUTPUT_ANALOG:
5133 return POWER_DOMAIN_PORT_CRT;
5134 case INTEL_OUTPUT_DSI:
5135 return POWER_DOMAIN_PORT_DSI;
5136 default:
5137 return POWER_DOMAIN_PORT_OTHER;
5138 }
5139}
5140
25f78f58
VS
5141enum intel_display_power_domain
5142intel_display_port_aux_power_domain(struct intel_encoder *intel_encoder)
5143{
5144 struct drm_device *dev = intel_encoder->base.dev;
5145 struct intel_digital_port *intel_dig_port;
5146
5147 switch (intel_encoder->type) {
5148 case INTEL_OUTPUT_UNKNOWN:
651174a4
ID
5149 case INTEL_OUTPUT_HDMI:
5150 /*
5151 * Only DDI platforms should ever use these output types.
5152 * We can get here after the HDMI detect code has already set
5153 * the type of the shared encoder. Since we can't be sure
5154 * what's the status of the given connectors, play safe and
5155 * run the DP detection too.
5156 */
25f78f58 5157 WARN_ON_ONCE(!HAS_DDI(dev));
cca0502b 5158 case INTEL_OUTPUT_DP:
25f78f58
VS
5159 case INTEL_OUTPUT_EDP:
5160 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
5161 return port_to_aux_power_domain(intel_dig_port->port);
5162 case INTEL_OUTPUT_DP_MST:
5163 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
5164 return port_to_aux_power_domain(intel_dig_port->port);
5165 default:
b9fec167 5166 MISSING_CASE(intel_encoder->type);
25f78f58
VS
5167 return POWER_DOMAIN_AUX_A;
5168 }
5169}
5170
74bff5f9
ML
5171static unsigned long get_crtc_power_domains(struct drm_crtc *crtc,
5172 struct intel_crtc_state *crtc_state)
77d22dca 5173{
319be8ae 5174 struct drm_device *dev = crtc->dev;
74bff5f9 5175 struct drm_encoder *encoder;
319be8ae
ID
5176 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5177 enum pipe pipe = intel_crtc->pipe;
77d22dca 5178 unsigned long mask;
74bff5f9 5179 enum transcoder transcoder = crtc_state->cpu_transcoder;
77d22dca 5180
74bff5f9 5181 if (!crtc_state->base.active)
292b990e
ML
5182 return 0;
5183
77d22dca
ID
5184 mask = BIT(POWER_DOMAIN_PIPE(pipe));
5185 mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
74bff5f9
ML
5186 if (crtc_state->pch_pfit.enabled ||
5187 crtc_state->pch_pfit.force_thru)
77d22dca
ID
5188 mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
5189
74bff5f9
ML
5190 drm_for_each_encoder_mask(encoder, dev, crtc_state->base.encoder_mask) {
5191 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
5192
319be8ae 5193 mask |= BIT(intel_display_port_power_domain(intel_encoder));
74bff5f9 5194 }
319be8ae 5195
15e7ec29
ML
5196 if (crtc_state->shared_dpll)
5197 mask |= BIT(POWER_DOMAIN_PLLS);
5198
77d22dca
ID
5199 return mask;
5200}
5201
74bff5f9
ML
5202static unsigned long
5203modeset_get_crtc_power_domains(struct drm_crtc *crtc,
5204 struct intel_crtc_state *crtc_state)
77d22dca 5205{
fac5e23e 5206 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
292b990e
ML
5207 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5208 enum intel_display_power_domain domain;
5a21b665 5209 unsigned long domains, new_domains, old_domains;
77d22dca 5210
292b990e 5211 old_domains = intel_crtc->enabled_power_domains;
74bff5f9
ML
5212 intel_crtc->enabled_power_domains = new_domains =
5213 get_crtc_power_domains(crtc, crtc_state);
77d22dca 5214
5a21b665 5215 domains = new_domains & ~old_domains;
292b990e
ML
5216
5217 for_each_power_domain(domain, domains)
5218 intel_display_power_get(dev_priv, domain);
5219
5a21b665 5220 return old_domains & ~new_domains;
292b990e
ML
5221}
5222
5223static void modeset_put_power_domains(struct drm_i915_private *dev_priv,
5224 unsigned long domains)
5225{
5226 enum intel_display_power_domain domain;
5227
5228 for_each_power_domain(domain, domains)
5229 intel_display_power_put(dev_priv, domain);
5230}
77d22dca 5231
adafdc6f
MK
5232static int intel_compute_max_dotclk(struct drm_i915_private *dev_priv)
5233{
5234 int max_cdclk_freq = dev_priv->max_cdclk_freq;
5235
5236 if (INTEL_INFO(dev_priv)->gen >= 9 ||
5237 IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
5238 return max_cdclk_freq;
5239 else if (IS_CHERRYVIEW(dev_priv))
5240 return max_cdclk_freq*95/100;
5241 else if (INTEL_INFO(dev_priv)->gen < 4)
5242 return 2*max_cdclk_freq*90/100;
5243 else
5244 return max_cdclk_freq*90/100;
5245}
5246
b2045352
VS
5247static int skl_calc_cdclk(int max_pixclk, int vco);
5248
560a7ae4
DL
5249static void intel_update_max_cdclk(struct drm_device *dev)
5250{
fac5e23e 5251 struct drm_i915_private *dev_priv = to_i915(dev);
560a7ae4 5252
ef11bdb3 5253 if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) {
560a7ae4 5254 u32 limit = I915_READ(SKL_DFSM) & SKL_DFSM_CDCLK_LIMIT_MASK;
b2045352
VS
5255 int max_cdclk, vco;
5256
5257 vco = dev_priv->skl_preferred_vco_freq;
63911d72 5258 WARN_ON(vco != 8100000 && vco != 8640000);
560a7ae4 5259
b2045352
VS
5260 /*
5261 * Use the lower (vco 8640) cdclk values as a
5262 * first guess. skl_calc_cdclk() will correct it
5263 * if the preferred vco is 8100 instead.
5264 */
560a7ae4 5265 if (limit == SKL_DFSM_CDCLK_LIMIT_675)
487ed2e4 5266 max_cdclk = 617143;
560a7ae4 5267 else if (limit == SKL_DFSM_CDCLK_LIMIT_540)
b2045352 5268 max_cdclk = 540000;
560a7ae4 5269 else if (limit == SKL_DFSM_CDCLK_LIMIT_450)
b2045352 5270 max_cdclk = 432000;
560a7ae4 5271 else
487ed2e4 5272 max_cdclk = 308571;
b2045352
VS
5273
5274 dev_priv->max_cdclk_freq = skl_calc_cdclk(max_cdclk, vco);
281c114f
MR
5275 } else if (IS_BROXTON(dev)) {
5276 dev_priv->max_cdclk_freq = 624000;
560a7ae4
DL
5277 } else if (IS_BROADWELL(dev)) {
5278 /*
5279 * FIXME with extra cooling we can allow
5280 * 540 MHz for ULX and 675 Mhz for ULT.
5281 * How can we know if extra cooling is
5282 * available? PCI ID, VTB, something else?
5283 */
5284 if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
5285 dev_priv->max_cdclk_freq = 450000;
5286 else if (IS_BDW_ULX(dev))
5287 dev_priv->max_cdclk_freq = 450000;
5288 else if (IS_BDW_ULT(dev))
5289 dev_priv->max_cdclk_freq = 540000;
5290 else
5291 dev_priv->max_cdclk_freq = 675000;
0904deaf
MK
5292 } else if (IS_CHERRYVIEW(dev)) {
5293 dev_priv->max_cdclk_freq = 320000;
560a7ae4
DL
5294 } else if (IS_VALLEYVIEW(dev)) {
5295 dev_priv->max_cdclk_freq = 400000;
5296 } else {
5297 /* otherwise assume cdclk is fixed */
5298 dev_priv->max_cdclk_freq = dev_priv->cdclk_freq;
5299 }
5300
adafdc6f
MK
5301 dev_priv->max_dotclk_freq = intel_compute_max_dotclk(dev_priv);
5302
560a7ae4
DL
5303 DRM_DEBUG_DRIVER("Max CD clock rate: %d kHz\n",
5304 dev_priv->max_cdclk_freq);
adafdc6f
MK
5305
5306 DRM_DEBUG_DRIVER("Max dotclock rate: %d kHz\n",
5307 dev_priv->max_dotclk_freq);
560a7ae4
DL
5308}
5309
5310static void intel_update_cdclk(struct drm_device *dev)
5311{
fac5e23e 5312 struct drm_i915_private *dev_priv = to_i915(dev);
560a7ae4
DL
5313
5314 dev_priv->cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
2f2a121a 5315
83d7c81f 5316 if (INTEL_GEN(dev_priv) >= 9)
709e05c3
VS
5317 DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz, VCO: %d kHz, ref: %d kHz\n",
5318 dev_priv->cdclk_freq, dev_priv->cdclk_pll.vco,
5319 dev_priv->cdclk_pll.ref);
2f2a121a
VS
5320 else
5321 DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
5322 dev_priv->cdclk_freq);
560a7ae4
DL
5323
5324 /*
b5d99ff9
VS
5325 * 9:0 CMBUS [sic] CDCLK frequency (cdfreq):
5326 * Programmng [sic] note: bit[9:2] should be programmed to the number
5327 * of cdclk that generates 4MHz reference clock freq which is used to
5328 * generate GMBus clock. This will vary with the cdclk freq.
560a7ae4 5329 */
b5d99ff9 5330 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
560a7ae4 5331 I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->cdclk_freq, 1000));
560a7ae4
DL
5332}
5333
92891e45
VS
5334/* convert from kHz to .1 fixpoint MHz with -1MHz offset */
5335static int skl_cdclk_decimal(int cdclk)
5336{
5337 return DIV_ROUND_CLOSEST(cdclk - 1000, 500);
5338}
5339
5f199dfa
VS
5340static int bxt_de_pll_vco(struct drm_i915_private *dev_priv, int cdclk)
5341{
5342 int ratio;
5343
5344 if (cdclk == dev_priv->cdclk_pll.ref)
5345 return 0;
5346
5347 switch (cdclk) {
5348 default:
5349 MISSING_CASE(cdclk);
5350 case 144000:
5351 case 288000:
5352 case 384000:
5353 case 576000:
5354 ratio = 60;
5355 break;
5356 case 624000:
5357 ratio = 65;
5358 break;
5359 }
5360
5361 return dev_priv->cdclk_pll.ref * ratio;
5362}
5363
2b73001e
VS
5364static void bxt_de_pll_disable(struct drm_i915_private *dev_priv)
5365{
5366 I915_WRITE(BXT_DE_PLL_ENABLE, 0);
5367
5368 /* Timeout 200us */
95cac283
CW
5369 if (intel_wait_for_register(dev_priv,
5370 BXT_DE_PLL_ENABLE, BXT_DE_PLL_LOCK, 0,
5371 1))
2b73001e 5372 DRM_ERROR("timeout waiting for DE PLL unlock\n");
83d7c81f
VS
5373
5374 dev_priv->cdclk_pll.vco = 0;
2b73001e
VS
5375}
5376
5f199dfa 5377static void bxt_de_pll_enable(struct drm_i915_private *dev_priv, int vco)
2b73001e 5378{
5f199dfa 5379 int ratio = DIV_ROUND_CLOSEST(vco, dev_priv->cdclk_pll.ref);
2b73001e
VS
5380 u32 val;
5381
5382 val = I915_READ(BXT_DE_PLL_CTL);
5383 val &= ~BXT_DE_PLL_RATIO_MASK;
5f199dfa 5384 val |= BXT_DE_PLL_RATIO(ratio);
2b73001e
VS
5385 I915_WRITE(BXT_DE_PLL_CTL, val);
5386
5387 I915_WRITE(BXT_DE_PLL_ENABLE, BXT_DE_PLL_PLL_ENABLE);
5388
5389 /* Timeout 200us */
e084e1b9
CW
5390 if (intel_wait_for_register(dev_priv,
5391 BXT_DE_PLL_ENABLE,
5392 BXT_DE_PLL_LOCK,
5393 BXT_DE_PLL_LOCK,
5394 1))
2b73001e 5395 DRM_ERROR("timeout waiting for DE PLL lock\n");
83d7c81f 5396
5f199dfa 5397 dev_priv->cdclk_pll.vco = vco;
2b73001e
VS
5398}
5399
324513c0 5400static void bxt_set_cdclk(struct drm_i915_private *dev_priv, int cdclk)
f8437dd1 5401{
5f199dfa
VS
5402 u32 val, divider;
5403 int vco, ret;
f8437dd1 5404
5f199dfa
VS
5405 vco = bxt_de_pll_vco(dev_priv, cdclk);
5406
5407 DRM_DEBUG_DRIVER("Changing CDCLK to %d kHz (VCO %d kHz)\n", cdclk, vco);
5408
5409 /* cdclk = vco / 2 / div{1,1.5,2,4} */
5410 switch (DIV_ROUND_CLOSEST(vco, cdclk)) {
5411 case 8:
f8437dd1 5412 divider = BXT_CDCLK_CD2X_DIV_SEL_4;
f8437dd1 5413 break;
5f199dfa 5414 case 4:
f8437dd1 5415 divider = BXT_CDCLK_CD2X_DIV_SEL_2;
f8437dd1 5416 break;
5f199dfa 5417 case 3:
f8437dd1 5418 divider = BXT_CDCLK_CD2X_DIV_SEL_1_5;
f8437dd1 5419 break;
5f199dfa 5420 case 2:
f8437dd1 5421 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
f8437dd1
VK
5422 break;
5423 default:
5f199dfa
VS
5424 WARN_ON(cdclk != dev_priv->cdclk_pll.ref);
5425 WARN_ON(vco != 0);
f8437dd1 5426
5f199dfa
VS
5427 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
5428 break;
f8437dd1
VK
5429 }
5430
f8437dd1 5431 /* Inform power controller of upcoming frequency change */
5f199dfa 5432 mutex_lock(&dev_priv->rps.hw_lock);
f8437dd1
VK
5433 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
5434 0x80000000);
5435 mutex_unlock(&dev_priv->rps.hw_lock);
5436
5437 if (ret) {
5438 DRM_ERROR("PCode CDCLK freq change notify failed (err %d, freq %d)\n",
9ef56154 5439 ret, cdclk);
f8437dd1
VK
5440 return;
5441 }
5442
5f199dfa
VS
5443 if (dev_priv->cdclk_pll.vco != 0 &&
5444 dev_priv->cdclk_pll.vco != vco)
2b73001e 5445 bxt_de_pll_disable(dev_priv);
f8437dd1 5446
5f199dfa
VS
5447 if (dev_priv->cdclk_pll.vco != vco)
5448 bxt_de_pll_enable(dev_priv, vco);
f8437dd1 5449
5f199dfa
VS
5450 val = divider | skl_cdclk_decimal(cdclk);
5451 /*
5452 * FIXME if only the cd2x divider needs changing, it could be done
5453 * without shutting off the pipe (if only one pipe is active).
5454 */
5455 val |= BXT_CDCLK_CD2X_PIPE_NONE;
5456 /*
5457 * Disable SSA Precharge when CD clock frequency < 500 MHz,
5458 * enable otherwise.
5459 */
5460 if (cdclk >= 500000)
5461 val |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
5462 I915_WRITE(CDCLK_CTL, val);
f8437dd1
VK
5463
5464 mutex_lock(&dev_priv->rps.hw_lock);
5465 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
9ef56154 5466 DIV_ROUND_UP(cdclk, 25000));
f8437dd1
VK
5467 mutex_unlock(&dev_priv->rps.hw_lock);
5468
5469 if (ret) {
5470 DRM_ERROR("PCode CDCLK freq set failed, (err %d, freq %d)\n",
9ef56154 5471 ret, cdclk);
f8437dd1
VK
5472 return;
5473 }
5474
91c8a326 5475 intel_update_cdclk(&dev_priv->drm);
f8437dd1
VK
5476}
5477
d66a2194 5478static void bxt_sanitize_cdclk(struct drm_i915_private *dev_priv)
f8437dd1 5479{
d66a2194
ID
5480 u32 cdctl, expected;
5481
91c8a326 5482 intel_update_cdclk(&dev_priv->drm);
f8437dd1 5483
d66a2194
ID
5484 if (dev_priv->cdclk_pll.vco == 0 ||
5485 dev_priv->cdclk_freq == dev_priv->cdclk_pll.ref)
5486 goto sanitize;
5487
5488 /* DPLL okay; verify the cdclock
5489 *
5490 * Some BIOS versions leave an incorrect decimal frequency value and
5491 * set reserved MBZ bits in CDCLK_CTL at least during exiting from S4,
5492 * so sanitize this register.
5493 */
5494 cdctl = I915_READ(CDCLK_CTL);
5495 /*
5496 * Let's ignore the pipe field, since BIOS could have configured the
5497 * dividers both synching to an active pipe, or asynchronously
5498 * (PIPE_NONE).
5499 */
5500 cdctl &= ~BXT_CDCLK_CD2X_PIPE_NONE;
5501
5502 expected = (cdctl & BXT_CDCLK_CD2X_DIV_SEL_MASK) |
5503 skl_cdclk_decimal(dev_priv->cdclk_freq);
5504 /*
5505 * Disable SSA Precharge when CD clock frequency < 500 MHz,
5506 * enable otherwise.
5507 */
5508 if (dev_priv->cdclk_freq >= 500000)
5509 expected |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
5510
5511 if (cdctl == expected)
5512 /* All well; nothing to sanitize */
5513 return;
5514
5515sanitize:
5516 DRM_DEBUG_KMS("Sanitizing cdclk programmed by pre-os\n");
5517
5518 /* force cdclk programming */
5519 dev_priv->cdclk_freq = 0;
5520
5521 /* force full PLL disable + enable */
5522 dev_priv->cdclk_pll.vco = -1;
5523}
5524
324513c0 5525void bxt_init_cdclk(struct drm_i915_private *dev_priv)
d66a2194
ID
5526{
5527 bxt_sanitize_cdclk(dev_priv);
5528
5529 if (dev_priv->cdclk_freq != 0 && dev_priv->cdclk_pll.vco != 0)
089c6fd5 5530 return;
c2e001ef 5531
f8437dd1
VK
5532 /*
5533 * FIXME:
5534 * - The initial CDCLK needs to be read from VBT.
5535 * Need to make this change after VBT has changes for BXT.
f8437dd1 5536 */
324513c0 5537 bxt_set_cdclk(dev_priv, bxt_calc_cdclk(0));
f8437dd1
VK
5538}
5539
324513c0 5540void bxt_uninit_cdclk(struct drm_i915_private *dev_priv)
f8437dd1 5541{
324513c0 5542 bxt_set_cdclk(dev_priv, dev_priv->cdclk_pll.ref);
f8437dd1
VK
5543}
5544
a8ca4934
VS
5545static int skl_calc_cdclk(int max_pixclk, int vco)
5546{
63911d72 5547 if (vco == 8640000) {
a8ca4934 5548 if (max_pixclk > 540000)
487ed2e4 5549 return 617143;
a8ca4934
VS
5550 else if (max_pixclk > 432000)
5551 return 540000;
487ed2e4 5552 else if (max_pixclk > 308571)
a8ca4934
VS
5553 return 432000;
5554 else
487ed2e4 5555 return 308571;
a8ca4934 5556 } else {
a8ca4934
VS
5557 if (max_pixclk > 540000)
5558 return 675000;
5559 else if (max_pixclk > 450000)
5560 return 540000;
5561 else if (max_pixclk > 337500)
5562 return 450000;
5563 else
5564 return 337500;
5565 }
5566}
5567
ea61791e
VS
5568static void
5569skl_dpll0_update(struct drm_i915_private *dev_priv)
5d96d8af 5570{
ea61791e 5571 u32 val;
5d96d8af 5572
709e05c3 5573 dev_priv->cdclk_pll.ref = 24000;
1c3f7700 5574 dev_priv->cdclk_pll.vco = 0;
709e05c3 5575
ea61791e 5576 val = I915_READ(LCPLL1_CTL);
1c3f7700 5577 if ((val & LCPLL_PLL_ENABLE) == 0)
ea61791e 5578 return;
5d96d8af 5579
1c3f7700
ID
5580 if (WARN_ON((val & LCPLL_PLL_LOCK) == 0))
5581 return;
9f7eb31a 5582
ea61791e
VS
5583 val = I915_READ(DPLL_CTRL1);
5584
1c3f7700
ID
5585 if (WARN_ON((val & (DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) |
5586 DPLL_CTRL1_SSC(SKL_DPLL0) |
5587 DPLL_CTRL1_OVERRIDE(SKL_DPLL0))) !=
5588 DPLL_CTRL1_OVERRIDE(SKL_DPLL0)))
5589 return;
9f7eb31a 5590
ea61791e
VS
5591 switch (val & DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0)) {
5592 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810, SKL_DPLL0):
5593 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1350, SKL_DPLL0):
5594 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1620, SKL_DPLL0):
5595 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_2700, SKL_DPLL0):
63911d72 5596 dev_priv->cdclk_pll.vco = 8100000;
ea61791e
VS
5597 break;
5598 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080, SKL_DPLL0):
5599 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_2160, SKL_DPLL0):
63911d72 5600 dev_priv->cdclk_pll.vco = 8640000;
ea61791e
VS
5601 break;
5602 default:
5603 MISSING_CASE(val & DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
ea61791e
VS
5604 break;
5605 }
5d96d8af
DL
5606}
5607
b2045352
VS
5608void skl_set_preferred_cdclk_vco(struct drm_i915_private *dev_priv, int vco)
5609{
5610 bool changed = dev_priv->skl_preferred_vco_freq != vco;
5611
5612 dev_priv->skl_preferred_vco_freq = vco;
5613
5614 if (changed)
91c8a326 5615 intel_update_max_cdclk(&dev_priv->drm);
b2045352
VS
5616}
5617
5d96d8af 5618static void
3861fc60 5619skl_dpll0_enable(struct drm_i915_private *dev_priv, int vco)
5d96d8af 5620{
a8ca4934 5621 int min_cdclk = skl_calc_cdclk(0, vco);
5d96d8af
DL
5622 u32 val;
5623
63911d72 5624 WARN_ON(vco != 8100000 && vco != 8640000);
b2045352 5625
5d96d8af 5626 /* select the minimum CDCLK before enabling DPLL 0 */
9ef56154 5627 val = CDCLK_FREQ_337_308 | skl_cdclk_decimal(min_cdclk);
5d96d8af
DL
5628 I915_WRITE(CDCLK_CTL, val);
5629 POSTING_READ(CDCLK_CTL);
5630
5631 /*
5632 * We always enable DPLL0 with the lowest link rate possible, but still
5633 * taking into account the VCO required to operate the eDP panel at the
5634 * desired frequency. The usual DP link rates operate with a VCO of
5635 * 8100 while the eDP 1.4 alternate link rates need a VCO of 8640.
5636 * The modeset code is responsible for the selection of the exact link
5637 * rate later on, with the constraint of choosing a frequency that
a8ca4934 5638 * works with vco.
5d96d8af
DL
5639 */
5640 val = I915_READ(DPLL_CTRL1);
5641
5642 val &= ~(DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) | DPLL_CTRL1_SSC(SKL_DPLL0) |
5643 DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
5644 val |= DPLL_CTRL1_OVERRIDE(SKL_DPLL0);
63911d72 5645 if (vco == 8640000)
5d96d8af
DL
5646 val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080,
5647 SKL_DPLL0);
5648 else
5649 val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810,
5650 SKL_DPLL0);
5651
5652 I915_WRITE(DPLL_CTRL1, val);
5653 POSTING_READ(DPLL_CTRL1);
5654
5655 I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) | LCPLL_PLL_ENABLE);
5656
e24ca054
CW
5657 if (intel_wait_for_register(dev_priv,
5658 LCPLL1_CTL, LCPLL_PLL_LOCK, LCPLL_PLL_LOCK,
5659 5))
5d96d8af 5660 DRM_ERROR("DPLL0 not locked\n");
1cd593e0 5661
63911d72 5662 dev_priv->cdclk_pll.vco = vco;
b2045352
VS
5663
5664 /* We'll want to keep using the current vco from now on. */
5665 skl_set_preferred_cdclk_vco(dev_priv, vco);
5d96d8af
DL
5666}
5667
430e05de
VS
5668static void
5669skl_dpll0_disable(struct drm_i915_private *dev_priv)
5670{
5671 I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) & ~LCPLL_PLL_ENABLE);
8ad32a05
CW
5672 if (intel_wait_for_register(dev_priv,
5673 LCPLL1_CTL, LCPLL_PLL_LOCK, 0,
5674 1))
430e05de 5675 DRM_ERROR("Couldn't disable DPLL0\n");
1cd593e0 5676
63911d72 5677 dev_priv->cdclk_pll.vco = 0;
430e05de
VS
5678}
5679
5d96d8af
DL
5680static bool skl_cdclk_pcu_ready(struct drm_i915_private *dev_priv)
5681{
5682 int ret;
5683 u32 val;
5684
5685 /* inform PCU we want to change CDCLK */
5686 val = SKL_CDCLK_PREPARE_FOR_CHANGE;
5687 mutex_lock(&dev_priv->rps.hw_lock);
5688 ret = sandybridge_pcode_read(dev_priv, SKL_PCODE_CDCLK_CONTROL, &val);
5689 mutex_unlock(&dev_priv->rps.hw_lock);
5690
5691 return ret == 0 && (val & SKL_CDCLK_READY_FOR_CHANGE);
5692}
5693
5694static bool skl_cdclk_wait_for_pcu_ready(struct drm_i915_private *dev_priv)
5695{
848496e5 5696 return _wait_for(skl_cdclk_pcu_ready(dev_priv), 3000, 10) == 0;
5d96d8af
DL
5697}
5698
1cd593e0 5699static void skl_set_cdclk(struct drm_i915_private *dev_priv, int cdclk, int vco)
5d96d8af 5700{
91c8a326 5701 struct drm_device *dev = &dev_priv->drm;
5d96d8af
DL
5702 u32 freq_select, pcu_ack;
5703
1cd593e0
VS
5704 WARN_ON((cdclk == 24000) != (vco == 0));
5705
63911d72 5706 DRM_DEBUG_DRIVER("Changing CDCLK to %d kHz (VCO %d kHz)\n", cdclk, vco);
5d96d8af
DL
5707
5708 if (!skl_cdclk_wait_for_pcu_ready(dev_priv)) {
5709 DRM_ERROR("failed to inform PCU about cdclk change\n");
5710 return;
5711 }
5712
5713 /* set CDCLK_CTL */
9ef56154 5714 switch (cdclk) {
5d96d8af
DL
5715 case 450000:
5716 case 432000:
5717 freq_select = CDCLK_FREQ_450_432;
5718 pcu_ack = 1;
5719 break;
5720 case 540000:
5721 freq_select = CDCLK_FREQ_540;
5722 pcu_ack = 2;
5723 break;
487ed2e4 5724 case 308571:
5d96d8af
DL
5725 case 337500:
5726 default:
5727 freq_select = CDCLK_FREQ_337_308;
5728 pcu_ack = 0;
5729 break;
487ed2e4 5730 case 617143:
5d96d8af
DL
5731 case 675000:
5732 freq_select = CDCLK_FREQ_675_617;
5733 pcu_ack = 3;
5734 break;
5735 }
5736
63911d72
VS
5737 if (dev_priv->cdclk_pll.vco != 0 &&
5738 dev_priv->cdclk_pll.vco != vco)
1cd593e0
VS
5739 skl_dpll0_disable(dev_priv);
5740
63911d72 5741 if (dev_priv->cdclk_pll.vco != vco)
1cd593e0
VS
5742 skl_dpll0_enable(dev_priv, vco);
5743
9ef56154 5744 I915_WRITE(CDCLK_CTL, freq_select | skl_cdclk_decimal(cdclk));
5d96d8af
DL
5745 POSTING_READ(CDCLK_CTL);
5746
5747 /* inform PCU of the change */
5748 mutex_lock(&dev_priv->rps.hw_lock);
5749 sandybridge_pcode_write(dev_priv, SKL_PCODE_CDCLK_CONTROL, pcu_ack);
5750 mutex_unlock(&dev_priv->rps.hw_lock);
560a7ae4
DL
5751
5752 intel_update_cdclk(dev);
5d96d8af
DL
5753}
5754
9f7eb31a
VS
5755static void skl_sanitize_cdclk(struct drm_i915_private *dev_priv);
5756
5d96d8af
DL
5757void skl_uninit_cdclk(struct drm_i915_private *dev_priv)
5758{
709e05c3 5759 skl_set_cdclk(dev_priv, dev_priv->cdclk_pll.ref, 0);
5d96d8af
DL
5760}
5761
5762void skl_init_cdclk(struct drm_i915_private *dev_priv)
5763{
9f7eb31a
VS
5764 int cdclk, vco;
5765
5766 skl_sanitize_cdclk(dev_priv);
5d96d8af 5767
63911d72 5768 if (dev_priv->cdclk_freq != 0 && dev_priv->cdclk_pll.vco != 0) {
9f7eb31a
VS
5769 /*
5770 * Use the current vco as our initial
5771 * guess as to what the preferred vco is.
5772 */
5773 if (dev_priv->skl_preferred_vco_freq == 0)
5774 skl_set_preferred_cdclk_vco(dev_priv,
63911d72 5775 dev_priv->cdclk_pll.vco);
70c2c184 5776 return;
1cd593e0 5777 }
5d96d8af 5778
70c2c184
VS
5779 vco = dev_priv->skl_preferred_vco_freq;
5780 if (vco == 0)
63911d72 5781 vco = 8100000;
70c2c184 5782 cdclk = skl_calc_cdclk(0, vco);
5d96d8af 5783
70c2c184 5784 skl_set_cdclk(dev_priv, cdclk, vco);
5d96d8af
DL
5785}
5786
9f7eb31a 5787static void skl_sanitize_cdclk(struct drm_i915_private *dev_priv)
c73666f3 5788{
09492498 5789 uint32_t cdctl, expected;
c73666f3 5790
f1b391a5
SK
5791 /*
5792 * check if the pre-os intialized the display
5793 * There is SWF18 scratchpad register defined which is set by the
5794 * pre-os which can be used by the OS drivers to check the status
5795 */
5796 if ((I915_READ(SWF_ILK(0x18)) & 0x00FFFFFF) == 0)
5797 goto sanitize;
5798
91c8a326 5799 intel_update_cdclk(&dev_priv->drm);
c73666f3 5800 /* Is PLL enabled and locked ? */
1c3f7700
ID
5801 if (dev_priv->cdclk_pll.vco == 0 ||
5802 dev_priv->cdclk_freq == dev_priv->cdclk_pll.ref)
c73666f3
SK
5803 goto sanitize;
5804
5805 /* DPLL okay; verify the cdclock
5806 *
5807 * Noticed in some instances that the freq selection is correct but
5808 * decimal part is programmed wrong from BIOS where pre-os does not
5809 * enable display. Verify the same as well.
5810 */
09492498
VS
5811 cdctl = I915_READ(CDCLK_CTL);
5812 expected = (cdctl & CDCLK_FREQ_SEL_MASK) |
5813 skl_cdclk_decimal(dev_priv->cdclk_freq);
5814 if (cdctl == expected)
c73666f3 5815 /* All well; nothing to sanitize */
9f7eb31a 5816 return;
c89e39f3 5817
9f7eb31a
VS
5818sanitize:
5819 DRM_DEBUG_KMS("Sanitizing cdclk programmed by pre-os\n");
c73666f3 5820
9f7eb31a
VS
5821 /* force cdclk programming */
5822 dev_priv->cdclk_freq = 0;
5823 /* force full PLL disable + enable */
63911d72 5824 dev_priv->cdclk_pll.vco = -1;
c73666f3
SK
5825}
5826
30a970c6
JB
5827/* Adjust CDclk dividers to allow high res or save power if possible */
5828static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
5829{
fac5e23e 5830 struct drm_i915_private *dev_priv = to_i915(dev);
30a970c6
JB
5831 u32 val, cmd;
5832
164dfd28
VK
5833 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5834 != dev_priv->cdclk_freq);
d60c4473 5835
dfcab17e 5836 if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
30a970c6 5837 cmd = 2;
dfcab17e 5838 else if (cdclk == 266667)
30a970c6
JB
5839 cmd = 1;
5840 else
5841 cmd = 0;
5842
5843 mutex_lock(&dev_priv->rps.hw_lock);
5844 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5845 val &= ~DSPFREQGUAR_MASK;
5846 val |= (cmd << DSPFREQGUAR_SHIFT);
5847 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5848 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5849 DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
5850 50)) {
5851 DRM_ERROR("timed out waiting for CDclk change\n");
5852 }
5853 mutex_unlock(&dev_priv->rps.hw_lock);
5854
54433e91
VS
5855 mutex_lock(&dev_priv->sb_lock);
5856
dfcab17e 5857 if (cdclk == 400000) {
6bcda4f0 5858 u32 divider;
30a970c6 5859
6bcda4f0 5860 divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
30a970c6 5861
30a970c6
JB
5862 /* adjust cdclk divider */
5863 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
87d5d259 5864 val &= ~CCK_FREQUENCY_VALUES;
30a970c6
JB
5865 val |= divider;
5866 vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
a877e801
VS
5867
5868 if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
87d5d259 5869 CCK_FREQUENCY_STATUS) == (divider << CCK_FREQUENCY_STATUS_SHIFT),
a877e801
VS
5870 50))
5871 DRM_ERROR("timed out waiting for CDclk change\n");
30a970c6
JB
5872 }
5873
30a970c6
JB
5874 /* adjust self-refresh exit latency value */
5875 val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
5876 val &= ~0x7f;
5877
5878 /*
5879 * For high bandwidth configs, we set a higher latency in the bunit
5880 * so that the core display fetch happens in time to avoid underruns.
5881 */
dfcab17e 5882 if (cdclk == 400000)
30a970c6
JB
5883 val |= 4500 / 250; /* 4.5 usec */
5884 else
5885 val |= 3000 / 250; /* 3.0 usec */
5886 vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
54433e91 5887
a580516d 5888 mutex_unlock(&dev_priv->sb_lock);
30a970c6 5889
b6283055 5890 intel_update_cdclk(dev);
30a970c6
JB
5891}
5892
383c5a6a
VS
5893static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
5894{
fac5e23e 5895 struct drm_i915_private *dev_priv = to_i915(dev);
383c5a6a
VS
5896 u32 val, cmd;
5897
164dfd28
VK
5898 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5899 != dev_priv->cdclk_freq);
383c5a6a
VS
5900
5901 switch (cdclk) {
383c5a6a
VS
5902 case 333333:
5903 case 320000:
383c5a6a 5904 case 266667:
383c5a6a 5905 case 200000:
383c5a6a
VS
5906 break;
5907 default:
5f77eeb0 5908 MISSING_CASE(cdclk);
383c5a6a
VS
5909 return;
5910 }
5911
9d0d3fda
VS
5912 /*
5913 * Specs are full of misinformation, but testing on actual
5914 * hardware has shown that we just need to write the desired
5915 * CCK divider into the Punit register.
5916 */
5917 cmd = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
5918
383c5a6a
VS
5919 mutex_lock(&dev_priv->rps.hw_lock);
5920 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5921 val &= ~DSPFREQGUAR_MASK_CHV;
5922 val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
5923 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5924 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5925 DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
5926 50)) {
5927 DRM_ERROR("timed out waiting for CDclk change\n");
5928 }
5929 mutex_unlock(&dev_priv->rps.hw_lock);
5930
b6283055 5931 intel_update_cdclk(dev);
383c5a6a
VS
5932}
5933
30a970c6
JB
5934static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
5935 int max_pixclk)
5936{
6bcda4f0 5937 int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
6cca3195 5938 int limit = IS_CHERRYVIEW(dev_priv) ? 95 : 90;
29dc7ef3 5939
30a970c6
JB
5940 /*
5941 * Really only a few cases to deal with, as only 4 CDclks are supported:
5942 * 200MHz
5943 * 267MHz
29dc7ef3 5944 * 320/333MHz (depends on HPLL freq)
6cca3195
VS
5945 * 400MHz (VLV only)
5946 * So we check to see whether we're above 90% (VLV) or 95% (CHV)
5947 * of the lower bin and adjust if needed.
e37c67a1
VS
5948 *
5949 * We seem to get an unstable or solid color picture at 200MHz.
5950 * Not sure what's wrong. For now use 200MHz only when all pipes
5951 * are off.
30a970c6 5952 */
6cca3195
VS
5953 if (!IS_CHERRYVIEW(dev_priv) &&
5954 max_pixclk > freq_320*limit/100)
dfcab17e 5955 return 400000;
6cca3195 5956 else if (max_pixclk > 266667*limit/100)
29dc7ef3 5957 return freq_320;
e37c67a1 5958 else if (max_pixclk > 0)
dfcab17e 5959 return 266667;
e37c67a1
VS
5960 else
5961 return 200000;
30a970c6
JB
5962}
5963
324513c0 5964static int bxt_calc_cdclk(int max_pixclk)
f8437dd1 5965{
760e1477 5966 if (max_pixclk > 576000)
f8437dd1 5967 return 624000;
760e1477 5968 else if (max_pixclk > 384000)
f8437dd1 5969 return 576000;
760e1477 5970 else if (max_pixclk > 288000)
f8437dd1 5971 return 384000;
760e1477 5972 else if (max_pixclk > 144000)
f8437dd1
VK
5973 return 288000;
5974 else
5975 return 144000;
5976}
5977
e8788cbc 5978/* Compute the max pixel clock for new configuration. */
a821fc46
ACO
5979static int intel_mode_max_pixclk(struct drm_device *dev,
5980 struct drm_atomic_state *state)
30a970c6 5981{
565602d7 5982 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
fac5e23e 5983 struct drm_i915_private *dev_priv = to_i915(dev);
565602d7
ML
5984 struct drm_crtc *crtc;
5985 struct drm_crtc_state *crtc_state;
5986 unsigned max_pixclk = 0, i;
5987 enum pipe pipe;
30a970c6 5988
565602d7
ML
5989 memcpy(intel_state->min_pixclk, dev_priv->min_pixclk,
5990 sizeof(intel_state->min_pixclk));
304603f4 5991
565602d7
ML
5992 for_each_crtc_in_state(state, crtc, crtc_state, i) {
5993 int pixclk = 0;
5994
5995 if (crtc_state->enable)
5996 pixclk = crtc_state->adjusted_mode.crtc_clock;
304603f4 5997
565602d7 5998 intel_state->min_pixclk[i] = pixclk;
30a970c6
JB
5999 }
6000
565602d7
ML
6001 for_each_pipe(dev_priv, pipe)
6002 max_pixclk = max(intel_state->min_pixclk[pipe], max_pixclk);
6003
30a970c6
JB
6004 return max_pixclk;
6005}
6006
27c329ed 6007static int valleyview_modeset_calc_cdclk(struct drm_atomic_state *state)
30a970c6 6008{
27c329ed 6009 struct drm_device *dev = state->dev;
fac5e23e 6010 struct drm_i915_private *dev_priv = to_i915(dev);
27c329ed 6011 int max_pixclk = intel_mode_max_pixclk(dev, state);
1a617b77
ML
6012 struct intel_atomic_state *intel_state =
6013 to_intel_atomic_state(state);
30a970c6 6014
1a617b77 6015 intel_state->cdclk = intel_state->dev_cdclk =
27c329ed 6016 valleyview_calc_cdclk(dev_priv, max_pixclk);
0a9ab303 6017
1a617b77
ML
6018 if (!intel_state->active_crtcs)
6019 intel_state->dev_cdclk = valleyview_calc_cdclk(dev_priv, 0);
6020
27c329ed
ML
6021 return 0;
6022}
304603f4 6023
324513c0 6024static int bxt_modeset_calc_cdclk(struct drm_atomic_state *state)
27c329ed 6025{
4e5ca60f 6026 int max_pixclk = ilk_max_pixel_rate(state);
1a617b77
ML
6027 struct intel_atomic_state *intel_state =
6028 to_intel_atomic_state(state);
85a96e7a 6029
1a617b77 6030 intel_state->cdclk = intel_state->dev_cdclk =
324513c0 6031 bxt_calc_cdclk(max_pixclk);
85a96e7a 6032
1a617b77 6033 if (!intel_state->active_crtcs)
324513c0 6034 intel_state->dev_cdclk = bxt_calc_cdclk(0);
1a617b77 6035
27c329ed 6036 return 0;
30a970c6
JB
6037}
6038
1e69cd74
VS
6039static void vlv_program_pfi_credits(struct drm_i915_private *dev_priv)
6040{
6041 unsigned int credits, default_credits;
6042
6043 if (IS_CHERRYVIEW(dev_priv))
6044 default_credits = PFI_CREDIT(12);
6045 else
6046 default_credits = PFI_CREDIT(8);
6047
bfa7df01 6048 if (dev_priv->cdclk_freq >= dev_priv->czclk_freq) {
1e69cd74
VS
6049 /* CHV suggested value is 31 or 63 */
6050 if (IS_CHERRYVIEW(dev_priv))
fcc0008f 6051 credits = PFI_CREDIT_63;
1e69cd74
VS
6052 else
6053 credits = PFI_CREDIT(15);
6054 } else {
6055 credits = default_credits;
6056 }
6057
6058 /*
6059 * WA - write default credits before re-programming
6060 * FIXME: should we also set the resend bit here?
6061 */
6062 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
6063 default_credits);
6064
6065 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
6066 credits | PFI_CREDIT_RESEND);
6067
6068 /*
6069 * FIXME is this guaranteed to clear
6070 * immediately or should we poll for it?
6071 */
6072 WARN_ON(I915_READ(GCI_CONTROL) & PFI_CREDIT_RESEND);
6073}
6074
27c329ed 6075static void valleyview_modeset_commit_cdclk(struct drm_atomic_state *old_state)
30a970c6 6076{
a821fc46 6077 struct drm_device *dev = old_state->dev;
fac5e23e 6078 struct drm_i915_private *dev_priv = to_i915(dev);
1a617b77
ML
6079 struct intel_atomic_state *old_intel_state =
6080 to_intel_atomic_state(old_state);
6081 unsigned req_cdclk = old_intel_state->dev_cdclk;
30a970c6 6082
27c329ed
ML
6083 /*
6084 * FIXME: We can end up here with all power domains off, yet
6085 * with a CDCLK frequency other than the minimum. To account
6086 * for this take the PIPE-A power domain, which covers the HW
6087 * blocks needed for the following programming. This can be
6088 * removed once it's guaranteed that we get here either with
6089 * the minimum CDCLK set, or the required power domains
6090 * enabled.
6091 */
6092 intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
738c05c0 6093
27c329ed
ML
6094 if (IS_CHERRYVIEW(dev))
6095 cherryview_set_cdclk(dev, req_cdclk);
6096 else
6097 valleyview_set_cdclk(dev, req_cdclk);
738c05c0 6098
27c329ed 6099 vlv_program_pfi_credits(dev_priv);
1e69cd74 6100
27c329ed 6101 intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
30a970c6
JB
6102}
6103
89b667f8
JB
6104static void valleyview_crtc_enable(struct drm_crtc *crtc)
6105{
6106 struct drm_device *dev = crtc->dev;
a72e4c9f 6107 struct drm_i915_private *dev_priv = to_i915(dev);
89b667f8
JB
6108 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6109 struct intel_encoder *encoder;
b95c5321
ML
6110 struct intel_crtc_state *pipe_config =
6111 to_intel_crtc_state(crtc->state);
89b667f8 6112 int pipe = intel_crtc->pipe;
89b667f8 6113
53d9f4e9 6114 if (WARN_ON(intel_crtc->active))
89b667f8
JB
6115 return;
6116
37a5650b 6117 if (intel_crtc_has_dp_encoder(intel_crtc->config))
fe3cd48d 6118 intel_dp_set_m_n(intel_crtc, M1_N1);
5b18e57c
DV
6119
6120 intel_set_pipe_timings(intel_crtc);
bc58be60 6121 intel_set_pipe_src_size(intel_crtc);
5b18e57c 6122
c14b0485 6123 if (IS_CHERRYVIEW(dev) && pipe == PIPE_B) {
fac5e23e 6124 struct drm_i915_private *dev_priv = to_i915(dev);
c14b0485
VS
6125
6126 I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
6127 I915_WRITE(CHV_CANVAS(pipe), 0);
6128 }
6129
5b18e57c
DV
6130 i9xx_set_pipeconf(intel_crtc);
6131
89b667f8 6132 intel_crtc->active = true;
89b667f8 6133
a72e4c9f 6134 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4a3436e8 6135
89b667f8
JB
6136 for_each_encoder_on_crtc(dev, crtc, encoder)
6137 if (encoder->pre_pll_enable)
6138 encoder->pre_pll_enable(encoder);
6139
cd2d34d9
VS
6140 if (IS_CHERRYVIEW(dev)) {
6141 chv_prepare_pll(intel_crtc, intel_crtc->config);
6142 chv_enable_pll(intel_crtc, intel_crtc->config);
6143 } else {
6144 vlv_prepare_pll(intel_crtc, intel_crtc->config);
6145 vlv_enable_pll(intel_crtc, intel_crtc->config);
9d556c99 6146 }
89b667f8
JB
6147
6148 for_each_encoder_on_crtc(dev, crtc, encoder)
6149 if (encoder->pre_enable)
6150 encoder->pre_enable(encoder);
6151
2dd24552
JB
6152 i9xx_pfit_enable(intel_crtc);
6153
b95c5321 6154 intel_color_load_luts(&pipe_config->base);
63cbb074 6155
caed361d 6156 intel_update_watermarks(crtc);
e1fdc473 6157 intel_enable_pipe(intel_crtc);
be6a6f8e 6158
4b3a9526
VS
6159 assert_vblank_disabled(crtc);
6160 drm_crtc_vblank_on(crtc);
6161
f9b61ff6
DV
6162 for_each_encoder_on_crtc(dev, crtc, encoder)
6163 encoder->enable(encoder);
89b667f8
JB
6164}
6165
f13c2ef3
DV
6166static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
6167{
6168 struct drm_device *dev = crtc->base.dev;
fac5e23e 6169 struct drm_i915_private *dev_priv = to_i915(dev);
f13c2ef3 6170
6e3c9717
ACO
6171 I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
6172 I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
f13c2ef3
DV
6173}
6174
0b8765c6 6175static void i9xx_crtc_enable(struct drm_crtc *crtc)
79e53945
JB
6176{
6177 struct drm_device *dev = crtc->dev;
a72e4c9f 6178 struct drm_i915_private *dev_priv = to_i915(dev);
79e53945 6179 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 6180 struct intel_encoder *encoder;
b95c5321
ML
6181 struct intel_crtc_state *pipe_config =
6182 to_intel_crtc_state(crtc->state);
cd2d34d9 6183 enum pipe pipe = intel_crtc->pipe;
79e53945 6184
53d9f4e9 6185 if (WARN_ON(intel_crtc->active))
f7abfe8b
CW
6186 return;
6187
f13c2ef3
DV
6188 i9xx_set_pll_dividers(intel_crtc);
6189
37a5650b 6190 if (intel_crtc_has_dp_encoder(intel_crtc->config))
fe3cd48d 6191 intel_dp_set_m_n(intel_crtc, M1_N1);
5b18e57c
DV
6192
6193 intel_set_pipe_timings(intel_crtc);
bc58be60 6194 intel_set_pipe_src_size(intel_crtc);
5b18e57c 6195
5b18e57c
DV
6196 i9xx_set_pipeconf(intel_crtc);
6197
f7abfe8b 6198 intel_crtc->active = true;
6b383a7f 6199
4a3436e8 6200 if (!IS_GEN2(dev))
a72e4c9f 6201 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4a3436e8 6202
9d6d9f19
MK
6203 for_each_encoder_on_crtc(dev, crtc, encoder)
6204 if (encoder->pre_enable)
6205 encoder->pre_enable(encoder);
6206
f6736a1a
DV
6207 i9xx_enable_pll(intel_crtc);
6208
2dd24552
JB
6209 i9xx_pfit_enable(intel_crtc);
6210
b95c5321 6211 intel_color_load_luts(&pipe_config->base);
63cbb074 6212
f37fcc2a 6213 intel_update_watermarks(crtc);
e1fdc473 6214 intel_enable_pipe(intel_crtc);
be6a6f8e 6215
4b3a9526
VS
6216 assert_vblank_disabled(crtc);
6217 drm_crtc_vblank_on(crtc);
6218
f9b61ff6
DV
6219 for_each_encoder_on_crtc(dev, crtc, encoder)
6220 encoder->enable(encoder);
0b8765c6 6221}
79e53945 6222
87476d63
DV
6223static void i9xx_pfit_disable(struct intel_crtc *crtc)
6224{
6225 struct drm_device *dev = crtc->base.dev;
fac5e23e 6226 struct drm_i915_private *dev_priv = to_i915(dev);
87476d63 6227
6e3c9717 6228 if (!crtc->config->gmch_pfit.control)
328d8e82 6229 return;
87476d63 6230
328d8e82 6231 assert_pipe_disabled(dev_priv, crtc->pipe);
87476d63 6232
328d8e82
DV
6233 DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
6234 I915_READ(PFIT_CONTROL));
6235 I915_WRITE(PFIT_CONTROL, 0);
87476d63
DV
6236}
6237
0b8765c6
JB
6238static void i9xx_crtc_disable(struct drm_crtc *crtc)
6239{
6240 struct drm_device *dev = crtc->dev;
fac5e23e 6241 struct drm_i915_private *dev_priv = to_i915(dev);
0b8765c6 6242 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 6243 struct intel_encoder *encoder;
0b8765c6 6244 int pipe = intel_crtc->pipe;
ef9c3aee 6245
6304cd91
VS
6246 /*
6247 * On gen2 planes are double buffered but the pipe isn't, so we must
6248 * wait for planes to fully turn off before disabling the pipe.
6249 */
90e83e53
ACO
6250 if (IS_GEN2(dev))
6251 intel_wait_for_vblank(dev, pipe);
6304cd91 6252
4b3a9526
VS
6253 for_each_encoder_on_crtc(dev, crtc, encoder)
6254 encoder->disable(encoder);
6255
f9b61ff6
DV
6256 drm_crtc_vblank_off(crtc);
6257 assert_vblank_disabled(crtc);
6258
575f7ab7 6259 intel_disable_pipe(intel_crtc);
24a1f16d 6260
87476d63 6261 i9xx_pfit_disable(intel_crtc);
24a1f16d 6262
89b667f8
JB
6263 for_each_encoder_on_crtc(dev, crtc, encoder)
6264 if (encoder->post_disable)
6265 encoder->post_disable(encoder);
6266
d7edc4e5 6267 if (!intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_DSI)) {
076ed3b2
CML
6268 if (IS_CHERRYVIEW(dev))
6269 chv_disable_pll(dev_priv, pipe);
6270 else if (IS_VALLEYVIEW(dev))
6271 vlv_disable_pll(dev_priv, pipe);
6272 else
1c4e0274 6273 i9xx_disable_pll(intel_crtc);
076ed3b2 6274 }
0b8765c6 6275
d6db995f
VS
6276 for_each_encoder_on_crtc(dev, crtc, encoder)
6277 if (encoder->post_pll_disable)
6278 encoder->post_pll_disable(encoder);
6279
4a3436e8 6280 if (!IS_GEN2(dev))
a72e4c9f 6281 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
0b8765c6
JB
6282}
6283
b17d48e2
ML
6284static void intel_crtc_disable_noatomic(struct drm_crtc *crtc)
6285{
842e0307 6286 struct intel_encoder *encoder;
b17d48e2
ML
6287 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6288 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
6289 enum intel_display_power_domain domain;
6290 unsigned long domains;
6291
6292 if (!intel_crtc->active)
6293 return;
6294
a539205a 6295 if (to_intel_plane_state(crtc->primary->state)->visible) {
5a21b665 6296 WARN_ON(intel_crtc->flip_work);
fc32b1fd 6297
2622a081 6298 intel_pre_disable_primary_noatomic(crtc);
54a41961
ML
6299
6300 intel_crtc_disable_planes(crtc, 1 << drm_plane_index(crtc->primary));
6301 to_intel_plane_state(crtc->primary->state)->visible = false;
a539205a
ML
6302 }
6303
b17d48e2 6304 dev_priv->display.crtc_disable(crtc);
842e0307 6305
78108b7c
VS
6306 DRM_DEBUG_KMS("[CRTC:%d:%s] hw state adjusted, was enabled, now disabled\n",
6307 crtc->base.id, crtc->name);
842e0307
ML
6308
6309 WARN_ON(drm_atomic_set_mode_for_crtc(crtc->state, NULL) < 0);
6310 crtc->state->active = false;
37d9078b 6311 intel_crtc->active = false;
842e0307
ML
6312 crtc->enabled = false;
6313 crtc->state->connector_mask = 0;
6314 crtc->state->encoder_mask = 0;
6315
6316 for_each_encoder_on_crtc(crtc->dev, crtc, encoder)
6317 encoder->base.crtc = NULL;
6318
58f9c0bc 6319 intel_fbc_disable(intel_crtc);
37d9078b 6320 intel_update_watermarks(crtc);
1f7457b1 6321 intel_disable_shared_dpll(intel_crtc);
b17d48e2
ML
6322
6323 domains = intel_crtc->enabled_power_domains;
6324 for_each_power_domain(domain, domains)
6325 intel_display_power_put(dev_priv, domain);
6326 intel_crtc->enabled_power_domains = 0;
565602d7
ML
6327
6328 dev_priv->active_crtcs &= ~(1 << intel_crtc->pipe);
6329 dev_priv->min_pixclk[intel_crtc->pipe] = 0;
b17d48e2
ML
6330}
6331
6b72d486
ML
6332/*
6333 * turn all crtc's off, but do not adjust state
6334 * This has to be paired with a call to intel_modeset_setup_hw_state.
6335 */
70e0bd74 6336int intel_display_suspend(struct drm_device *dev)
ee7b9f93 6337{
e2c8b870 6338 struct drm_i915_private *dev_priv = to_i915(dev);
70e0bd74 6339 struct drm_atomic_state *state;
e2c8b870 6340 int ret;
70e0bd74 6341
e2c8b870
ML
6342 state = drm_atomic_helper_suspend(dev);
6343 ret = PTR_ERR_OR_ZERO(state);
70e0bd74
ML
6344 if (ret)
6345 DRM_ERROR("Suspending crtc's failed with %i\n", ret);
e2c8b870
ML
6346 else
6347 dev_priv->modeset_restore_state = state;
70e0bd74 6348 return ret;
ee7b9f93
JB
6349}
6350
ea5b213a 6351void intel_encoder_destroy(struct drm_encoder *encoder)
7e7d76c3 6352{
4ef69c7a 6353 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
ea5b213a 6354
ea5b213a
CW
6355 drm_encoder_cleanup(encoder);
6356 kfree(intel_encoder);
7e7d76c3
JB
6357}
6358
0a91ca29
DV
6359/* Cross check the actual hw state with our own modeset state tracking (and it's
6360 * internal consistency). */
5a21b665 6361static void intel_connector_verify_state(struct intel_connector *connector)
79e53945 6362{
5a21b665 6363 struct drm_crtc *crtc = connector->base.state->crtc;
35dd3c64
ML
6364
6365 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
6366 connector->base.base.id,
6367 connector->base.name);
6368
0a91ca29 6369 if (connector->get_hw_state(connector)) {
e85376cb 6370 struct intel_encoder *encoder = connector->encoder;
5a21b665 6371 struct drm_connector_state *conn_state = connector->base.state;
0a91ca29 6372
35dd3c64
ML
6373 I915_STATE_WARN(!crtc,
6374 "connector enabled without attached crtc\n");
0a91ca29 6375
35dd3c64
ML
6376 if (!crtc)
6377 return;
6378
6379 I915_STATE_WARN(!crtc->state->active,
6380 "connector is active, but attached crtc isn't\n");
6381
e85376cb 6382 if (!encoder || encoder->type == INTEL_OUTPUT_DP_MST)
35dd3c64
ML
6383 return;
6384
e85376cb 6385 I915_STATE_WARN(conn_state->best_encoder != &encoder->base,
35dd3c64
ML
6386 "atomic encoder doesn't match attached encoder\n");
6387
e85376cb 6388 I915_STATE_WARN(conn_state->crtc != encoder->base.crtc,
35dd3c64
ML
6389 "attached encoder crtc differs from connector crtc\n");
6390 } else {
4d688a2a
ML
6391 I915_STATE_WARN(crtc && crtc->state->active,
6392 "attached crtc is active, but connector isn't\n");
5a21b665 6393 I915_STATE_WARN(!crtc && connector->base.state->best_encoder,
35dd3c64 6394 "best encoder set without crtc!\n");
0a91ca29 6395 }
79e53945
JB
6396}
6397
08d9bc92
ACO
6398int intel_connector_init(struct intel_connector *connector)
6399{
5350a031 6400 drm_atomic_helper_connector_reset(&connector->base);
08d9bc92 6401
5350a031 6402 if (!connector->base.state)
08d9bc92
ACO
6403 return -ENOMEM;
6404
08d9bc92
ACO
6405 return 0;
6406}
6407
6408struct intel_connector *intel_connector_alloc(void)
6409{
6410 struct intel_connector *connector;
6411
6412 connector = kzalloc(sizeof *connector, GFP_KERNEL);
6413 if (!connector)
6414 return NULL;
6415
6416 if (intel_connector_init(connector) < 0) {
6417 kfree(connector);
6418 return NULL;
6419 }
6420
6421 return connector;
6422}
6423
f0947c37
DV
6424/* Simple connector->get_hw_state implementation for encoders that support only
6425 * one connector and no cloning and hence the encoder state determines the state
6426 * of the connector. */
6427bool intel_connector_get_hw_state(struct intel_connector *connector)
ea5b213a 6428{
24929352 6429 enum pipe pipe = 0;
f0947c37 6430 struct intel_encoder *encoder = connector->encoder;
ea5b213a 6431
f0947c37 6432 return encoder->get_hw_state(encoder, &pipe);
ea5b213a
CW
6433}
6434
6d293983 6435static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
d272ddfa 6436{
6d293983
ACO
6437 if (crtc_state->base.enable && crtc_state->has_pch_encoder)
6438 return crtc_state->fdi_lanes;
d272ddfa
VS
6439
6440 return 0;
6441}
6442
6d293983 6443static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
5cec258b 6444 struct intel_crtc_state *pipe_config)
1857e1da 6445{
6d293983
ACO
6446 struct drm_atomic_state *state = pipe_config->base.state;
6447 struct intel_crtc *other_crtc;
6448 struct intel_crtc_state *other_crtc_state;
6449
1857e1da
DV
6450 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
6451 pipe_name(pipe), pipe_config->fdi_lanes);
6452 if (pipe_config->fdi_lanes > 4) {
6453 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
6454 pipe_name(pipe), pipe_config->fdi_lanes);
6d293983 6455 return -EINVAL;
1857e1da
DV
6456 }
6457
bafb6553 6458 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
1857e1da
DV
6459 if (pipe_config->fdi_lanes > 2) {
6460 DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
6461 pipe_config->fdi_lanes);
6d293983 6462 return -EINVAL;
1857e1da 6463 } else {
6d293983 6464 return 0;
1857e1da
DV
6465 }
6466 }
6467
6468 if (INTEL_INFO(dev)->num_pipes == 2)
6d293983 6469 return 0;
1857e1da
DV
6470
6471 /* Ivybridge 3 pipe is really complicated */
6472 switch (pipe) {
6473 case PIPE_A:
6d293983 6474 return 0;
1857e1da 6475 case PIPE_B:
6d293983
ACO
6476 if (pipe_config->fdi_lanes <= 2)
6477 return 0;
6478
6479 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_C));
6480 other_crtc_state =
6481 intel_atomic_get_crtc_state(state, other_crtc);
6482 if (IS_ERR(other_crtc_state))
6483 return PTR_ERR(other_crtc_state);
6484
6485 if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
1857e1da
DV
6486 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
6487 pipe_name(pipe), pipe_config->fdi_lanes);
6d293983 6488 return -EINVAL;
1857e1da 6489 }
6d293983 6490 return 0;
1857e1da 6491 case PIPE_C:
251cc67c
VS
6492 if (pipe_config->fdi_lanes > 2) {
6493 DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
6494 pipe_name(pipe), pipe_config->fdi_lanes);
6d293983 6495 return -EINVAL;
251cc67c 6496 }
6d293983
ACO
6497
6498 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_B));
6499 other_crtc_state =
6500 intel_atomic_get_crtc_state(state, other_crtc);
6501 if (IS_ERR(other_crtc_state))
6502 return PTR_ERR(other_crtc_state);
6503
6504 if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
1857e1da 6505 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
6d293983 6506 return -EINVAL;
1857e1da 6507 }
6d293983 6508 return 0;
1857e1da
DV
6509 default:
6510 BUG();
6511 }
6512}
6513
e29c22c0
DV
6514#define RETRY 1
6515static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
5cec258b 6516 struct intel_crtc_state *pipe_config)
877d48d5 6517{
1857e1da 6518 struct drm_device *dev = intel_crtc->base.dev;
7c5f93b0 6519 const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
6d293983
ACO
6520 int lane, link_bw, fdi_dotclock, ret;
6521 bool needs_recompute = false;
877d48d5 6522
e29c22c0 6523retry:
877d48d5
DV
6524 /* FDI is a binary signal running at ~2.7GHz, encoding
6525 * each output octet as 10 bits. The actual frequency
6526 * is stored as a divider into a 100MHz clock, and the
6527 * mode pixel clock is stored in units of 1KHz.
6528 * Hence the bw of each lane in terms of the mode signal
6529 * is:
6530 */
21a727b3 6531 link_bw = intel_fdi_link_freq(to_i915(dev), pipe_config);
877d48d5 6532
241bfc38 6533 fdi_dotclock = adjusted_mode->crtc_clock;
877d48d5 6534
2bd89a07 6535 lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
877d48d5
DV
6536 pipe_config->pipe_bpp);
6537
6538 pipe_config->fdi_lanes = lane;
6539
2bd89a07 6540 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
877d48d5 6541 link_bw, &pipe_config->fdi_m_n);
1857e1da 6542
e3b247da 6543 ret = ironlake_check_fdi_lanes(dev, intel_crtc->pipe, pipe_config);
6d293983 6544 if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
e29c22c0
DV
6545 pipe_config->pipe_bpp -= 2*3;
6546 DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
6547 pipe_config->pipe_bpp);
6548 needs_recompute = true;
6549 pipe_config->bw_constrained = true;
6550
6551 goto retry;
6552 }
6553
6554 if (needs_recompute)
6555 return RETRY;
6556
6d293983 6557 return ret;
877d48d5
DV
6558}
6559
8cfb3407
VS
6560static bool pipe_config_supports_ips(struct drm_i915_private *dev_priv,
6561 struct intel_crtc_state *pipe_config)
6562{
6563 if (pipe_config->pipe_bpp > 24)
6564 return false;
6565
6566 /* HSW can handle pixel rate up to cdclk? */
2d1fe073 6567 if (IS_HASWELL(dev_priv))
8cfb3407
VS
6568 return true;
6569
6570 /*
b432e5cf
VS
6571 * We compare against max which means we must take
6572 * the increased cdclk requirement into account when
6573 * calculating the new cdclk.
6574 *
6575 * Should measure whether using a lower cdclk w/o IPS
8cfb3407
VS
6576 */
6577 return ilk_pipe_pixel_rate(pipe_config) <=
6578 dev_priv->max_cdclk_freq * 95 / 100;
6579}
6580
42db64ef 6581static void hsw_compute_ips_config(struct intel_crtc *crtc,
5cec258b 6582 struct intel_crtc_state *pipe_config)
42db64ef 6583{
8cfb3407 6584 struct drm_device *dev = crtc->base.dev;
fac5e23e 6585 struct drm_i915_private *dev_priv = to_i915(dev);
8cfb3407 6586
d330a953 6587 pipe_config->ips_enabled = i915.enable_ips &&
8cfb3407
VS
6588 hsw_crtc_supports_ips(crtc) &&
6589 pipe_config_supports_ips(dev_priv, pipe_config);
42db64ef
PZ
6590}
6591
39acb4aa
VS
6592static bool intel_crtc_supports_double_wide(const struct intel_crtc *crtc)
6593{
6594 const struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6595
6596 /* GDG double wide on either pipe, otherwise pipe A only */
6597 return INTEL_INFO(dev_priv)->gen < 4 &&
6598 (crtc->pipe == PIPE_A || IS_I915G(dev_priv));
6599}
6600
a43f6e0f 6601static int intel_crtc_compute_config(struct intel_crtc *crtc,
5cec258b 6602 struct intel_crtc_state *pipe_config)
79e53945 6603{
a43f6e0f 6604 struct drm_device *dev = crtc->base.dev;
fac5e23e 6605 struct drm_i915_private *dev_priv = to_i915(dev);
7c5f93b0 6606 const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
f3261156 6607 int clock_limit = dev_priv->max_dotclk_freq;
89749350 6608
cf532bb2 6609 if (INTEL_INFO(dev)->gen < 4) {
f3261156 6610 clock_limit = dev_priv->max_cdclk_freq * 9 / 10;
cf532bb2
VS
6611
6612 /*
39acb4aa 6613 * Enable double wide mode when the dot clock
cf532bb2 6614 * is > 90% of the (display) core speed.
cf532bb2 6615 */
39acb4aa
VS
6616 if (intel_crtc_supports_double_wide(crtc) &&
6617 adjusted_mode->crtc_clock > clock_limit) {
f3261156 6618 clock_limit = dev_priv->max_dotclk_freq;
cf532bb2 6619 pipe_config->double_wide = true;
ad3a4479 6620 }
f3261156 6621 }
ad3a4479 6622
f3261156
VS
6623 if (adjusted_mode->crtc_clock > clock_limit) {
6624 DRM_DEBUG_KMS("requested pixel clock (%d kHz) too high (max: %d kHz, double wide: %s)\n",
6625 adjusted_mode->crtc_clock, clock_limit,
6626 yesno(pipe_config->double_wide));
6627 return -EINVAL;
2c07245f 6628 }
89749350 6629
1d1d0e27
VS
6630 /*
6631 * Pipe horizontal size must be even in:
6632 * - DVO ganged mode
6633 * - LVDS dual channel mode
6634 * - Double wide pipe
6635 */
2d84d2b3 6636 if ((intel_crtc_has_type(pipe_config, INTEL_OUTPUT_LVDS) &&
1d1d0e27
VS
6637 intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
6638 pipe_config->pipe_src_w &= ~1;
6639
8693a824
DL
6640 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
6641 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
44f46b42
CW
6642 */
6643 if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
aad941d5 6644 adjusted_mode->crtc_hsync_start == adjusted_mode->crtc_hdisplay)
e29c22c0 6645 return -EINVAL;
44f46b42 6646
f5adf94e 6647 if (HAS_IPS(dev))
a43f6e0f
DV
6648 hsw_compute_ips_config(crtc, pipe_config);
6649
877d48d5 6650 if (pipe_config->has_pch_encoder)
a43f6e0f 6651 return ironlake_fdi_compute_config(crtc, pipe_config);
877d48d5 6652
cf5a15be 6653 return 0;
79e53945
JB
6654}
6655
1652d19e
VS
6656static int skylake_get_display_clock_speed(struct drm_device *dev)
6657{
6658 struct drm_i915_private *dev_priv = to_i915(dev);
ea61791e 6659 uint32_t cdctl;
1652d19e 6660
ea61791e 6661 skl_dpll0_update(dev_priv);
1652d19e 6662
63911d72 6663 if (dev_priv->cdclk_pll.vco == 0)
709e05c3 6664 return dev_priv->cdclk_pll.ref;
1652d19e 6665
ea61791e 6666 cdctl = I915_READ(CDCLK_CTL);
1652d19e 6667
63911d72 6668 if (dev_priv->cdclk_pll.vco == 8640000) {
1652d19e
VS
6669 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6670 case CDCLK_FREQ_450_432:
6671 return 432000;
6672 case CDCLK_FREQ_337_308:
487ed2e4 6673 return 308571;
ea61791e
VS
6674 case CDCLK_FREQ_540:
6675 return 540000;
1652d19e 6676 case CDCLK_FREQ_675_617:
487ed2e4 6677 return 617143;
1652d19e 6678 default:
ea61791e 6679 MISSING_CASE(cdctl & CDCLK_FREQ_SEL_MASK);
1652d19e
VS
6680 }
6681 } else {
1652d19e
VS
6682 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6683 case CDCLK_FREQ_450_432:
6684 return 450000;
6685 case CDCLK_FREQ_337_308:
6686 return 337500;
ea61791e
VS
6687 case CDCLK_FREQ_540:
6688 return 540000;
1652d19e
VS
6689 case CDCLK_FREQ_675_617:
6690 return 675000;
6691 default:
ea61791e 6692 MISSING_CASE(cdctl & CDCLK_FREQ_SEL_MASK);
1652d19e
VS
6693 }
6694 }
6695
709e05c3 6696 return dev_priv->cdclk_pll.ref;
1652d19e
VS
6697}
6698
83d7c81f
VS
6699static void bxt_de_pll_update(struct drm_i915_private *dev_priv)
6700{
6701 u32 val;
6702
6703 dev_priv->cdclk_pll.ref = 19200;
1c3f7700 6704 dev_priv->cdclk_pll.vco = 0;
83d7c81f
VS
6705
6706 val = I915_READ(BXT_DE_PLL_ENABLE);
1c3f7700 6707 if ((val & BXT_DE_PLL_PLL_ENABLE) == 0)
83d7c81f 6708 return;
83d7c81f 6709
1c3f7700
ID
6710 if (WARN_ON((val & BXT_DE_PLL_LOCK) == 0))
6711 return;
83d7c81f
VS
6712
6713 val = I915_READ(BXT_DE_PLL_CTL);
6714 dev_priv->cdclk_pll.vco = (val & BXT_DE_PLL_RATIO_MASK) *
6715 dev_priv->cdclk_pll.ref;
6716}
6717
acd3f3d3
BP
6718static int broxton_get_display_clock_speed(struct drm_device *dev)
6719{
6720 struct drm_i915_private *dev_priv = to_i915(dev);
f5986242
VS
6721 u32 divider;
6722 int div, vco;
acd3f3d3 6723
83d7c81f
VS
6724 bxt_de_pll_update(dev_priv);
6725
f5986242
VS
6726 vco = dev_priv->cdclk_pll.vco;
6727 if (vco == 0)
6728 return dev_priv->cdclk_pll.ref;
acd3f3d3 6729
f5986242 6730 divider = I915_READ(CDCLK_CTL) & BXT_CDCLK_CD2X_DIV_SEL_MASK;
acd3f3d3 6731
f5986242 6732 switch (divider) {
acd3f3d3 6733 case BXT_CDCLK_CD2X_DIV_SEL_1:
f5986242
VS
6734 div = 2;
6735 break;
acd3f3d3 6736 case BXT_CDCLK_CD2X_DIV_SEL_1_5:
f5986242
VS
6737 div = 3;
6738 break;
acd3f3d3 6739 case BXT_CDCLK_CD2X_DIV_SEL_2:
f5986242
VS
6740 div = 4;
6741 break;
acd3f3d3 6742 case BXT_CDCLK_CD2X_DIV_SEL_4:
f5986242
VS
6743 div = 8;
6744 break;
6745 default:
6746 MISSING_CASE(divider);
6747 return dev_priv->cdclk_pll.ref;
acd3f3d3
BP
6748 }
6749
f5986242 6750 return DIV_ROUND_CLOSEST(vco, div);
acd3f3d3
BP
6751}
6752
1652d19e
VS
6753static int broadwell_get_display_clock_speed(struct drm_device *dev)
6754{
fac5e23e 6755 struct drm_i915_private *dev_priv = to_i915(dev);
1652d19e
VS
6756 uint32_t lcpll = I915_READ(LCPLL_CTL);
6757 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6758
6759 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6760 return 800000;
6761 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6762 return 450000;
6763 else if (freq == LCPLL_CLK_FREQ_450)
6764 return 450000;
6765 else if (freq == LCPLL_CLK_FREQ_54O_BDW)
6766 return 540000;
6767 else if (freq == LCPLL_CLK_FREQ_337_5_BDW)
6768 return 337500;
6769 else
6770 return 675000;
6771}
6772
6773static int haswell_get_display_clock_speed(struct drm_device *dev)
6774{
fac5e23e 6775 struct drm_i915_private *dev_priv = to_i915(dev);
1652d19e
VS
6776 uint32_t lcpll = I915_READ(LCPLL_CTL);
6777 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6778
6779 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6780 return 800000;
6781 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6782 return 450000;
6783 else if (freq == LCPLL_CLK_FREQ_450)
6784 return 450000;
6785 else if (IS_HSW_ULT(dev))
6786 return 337500;
6787 else
6788 return 540000;
79e53945
JB
6789}
6790
25eb05fc
JB
6791static int valleyview_get_display_clock_speed(struct drm_device *dev)
6792{
bfa7df01
VS
6793 return vlv_get_cck_clock_hpll(to_i915(dev), "cdclk",
6794 CCK_DISPLAY_CLOCK_CONTROL);
25eb05fc
JB
6795}
6796
b37a6434
VS
6797static int ilk_get_display_clock_speed(struct drm_device *dev)
6798{
6799 return 450000;
6800}
6801
e70236a8
JB
6802static int i945_get_display_clock_speed(struct drm_device *dev)
6803{
6804 return 400000;
6805}
79e53945 6806
e70236a8 6807static int i915_get_display_clock_speed(struct drm_device *dev)
79e53945 6808{
e907f170 6809 return 333333;
e70236a8 6810}
79e53945 6811
e70236a8
JB
6812static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
6813{
6814 return 200000;
6815}
79e53945 6816
257a7ffc
DV
6817static int pnv_get_display_clock_speed(struct drm_device *dev)
6818{
6819 u16 gcfgc = 0;
6820
6821 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6822
6823 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6824 case GC_DISPLAY_CLOCK_267_MHZ_PNV:
e907f170 6825 return 266667;
257a7ffc 6826 case GC_DISPLAY_CLOCK_333_MHZ_PNV:
e907f170 6827 return 333333;
257a7ffc 6828 case GC_DISPLAY_CLOCK_444_MHZ_PNV:
e907f170 6829 return 444444;
257a7ffc
DV
6830 case GC_DISPLAY_CLOCK_200_MHZ_PNV:
6831 return 200000;
6832 default:
6833 DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
6834 case GC_DISPLAY_CLOCK_133_MHZ_PNV:
e907f170 6835 return 133333;
257a7ffc 6836 case GC_DISPLAY_CLOCK_167_MHZ_PNV:
e907f170 6837 return 166667;
257a7ffc
DV
6838 }
6839}
6840
e70236a8
JB
6841static int i915gm_get_display_clock_speed(struct drm_device *dev)
6842{
6843 u16 gcfgc = 0;
79e53945 6844
e70236a8
JB
6845 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6846
6847 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
e907f170 6848 return 133333;
e70236a8
JB
6849 else {
6850 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6851 case GC_DISPLAY_CLOCK_333_MHZ:
e907f170 6852 return 333333;
e70236a8
JB
6853 default:
6854 case GC_DISPLAY_CLOCK_190_200_MHZ:
6855 return 190000;
79e53945 6856 }
e70236a8
JB
6857 }
6858}
6859
6860static int i865_get_display_clock_speed(struct drm_device *dev)
6861{
e907f170 6862 return 266667;
e70236a8
JB
6863}
6864
1b1d2716 6865static int i85x_get_display_clock_speed(struct drm_device *dev)
e70236a8
JB
6866{
6867 u16 hpllcc = 0;
1b1d2716 6868
65cd2b3f
VS
6869 /*
6870 * 852GM/852GMV only supports 133 MHz and the HPLLCC
6871 * encoding is different :(
6872 * FIXME is this the right way to detect 852GM/852GMV?
6873 */
6874 if (dev->pdev->revision == 0x1)
6875 return 133333;
6876
1b1d2716
VS
6877 pci_bus_read_config_word(dev->pdev->bus,
6878 PCI_DEVFN(0, 3), HPLLCC, &hpllcc);
6879
e70236a8
JB
6880 /* Assume that the hardware is in the high speed state. This
6881 * should be the default.
6882 */
6883 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
6884 case GC_CLOCK_133_200:
1b1d2716 6885 case GC_CLOCK_133_200_2:
e70236a8
JB
6886 case GC_CLOCK_100_200:
6887 return 200000;
6888 case GC_CLOCK_166_250:
6889 return 250000;
6890 case GC_CLOCK_100_133:
e907f170 6891 return 133333;
1b1d2716
VS
6892 case GC_CLOCK_133_266:
6893 case GC_CLOCK_133_266_2:
6894 case GC_CLOCK_166_266:
6895 return 266667;
e70236a8 6896 }
79e53945 6897
e70236a8
JB
6898 /* Shouldn't happen */
6899 return 0;
6900}
79e53945 6901
e70236a8
JB
6902static int i830_get_display_clock_speed(struct drm_device *dev)
6903{
e907f170 6904 return 133333;
79e53945
JB
6905}
6906
34edce2f
VS
6907static unsigned int intel_hpll_vco(struct drm_device *dev)
6908{
fac5e23e 6909 struct drm_i915_private *dev_priv = to_i915(dev);
34edce2f
VS
6910 static const unsigned int blb_vco[8] = {
6911 [0] = 3200000,
6912 [1] = 4000000,
6913 [2] = 5333333,
6914 [3] = 4800000,
6915 [4] = 6400000,
6916 };
6917 static const unsigned int pnv_vco[8] = {
6918 [0] = 3200000,
6919 [1] = 4000000,
6920 [2] = 5333333,
6921 [3] = 4800000,
6922 [4] = 2666667,
6923 };
6924 static const unsigned int cl_vco[8] = {
6925 [0] = 3200000,
6926 [1] = 4000000,
6927 [2] = 5333333,
6928 [3] = 6400000,
6929 [4] = 3333333,
6930 [5] = 3566667,
6931 [6] = 4266667,
6932 };
6933 static const unsigned int elk_vco[8] = {
6934 [0] = 3200000,
6935 [1] = 4000000,
6936 [2] = 5333333,
6937 [3] = 4800000,
6938 };
6939 static const unsigned int ctg_vco[8] = {
6940 [0] = 3200000,
6941 [1] = 4000000,
6942 [2] = 5333333,
6943 [3] = 6400000,
6944 [4] = 2666667,
6945 [5] = 4266667,
6946 };
6947 const unsigned int *vco_table;
6948 unsigned int vco;
6949 uint8_t tmp = 0;
6950
6951 /* FIXME other chipsets? */
6952 if (IS_GM45(dev))
6953 vco_table = ctg_vco;
6954 else if (IS_G4X(dev))
6955 vco_table = elk_vco;
6956 else if (IS_CRESTLINE(dev))
6957 vco_table = cl_vco;
6958 else if (IS_PINEVIEW(dev))
6959 vco_table = pnv_vco;
6960 else if (IS_G33(dev))
6961 vco_table = blb_vco;
6962 else
6963 return 0;
6964
6965 tmp = I915_READ(IS_MOBILE(dev) ? HPLLVCO_MOBILE : HPLLVCO);
6966
6967 vco = vco_table[tmp & 0x7];
6968 if (vco == 0)
6969 DRM_ERROR("Bad HPLL VCO (HPLLVCO=0x%02x)\n", tmp);
6970 else
6971 DRM_DEBUG_KMS("HPLL VCO %u kHz\n", vco);
6972
6973 return vco;
6974}
6975
6976static int gm45_get_display_clock_speed(struct drm_device *dev)
6977{
6978 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
6979 uint16_t tmp = 0;
6980
6981 pci_read_config_word(dev->pdev, GCFGC, &tmp);
6982
6983 cdclk_sel = (tmp >> 12) & 0x1;
6984
6985 switch (vco) {
6986 case 2666667:
6987 case 4000000:
6988 case 5333333:
6989 return cdclk_sel ? 333333 : 222222;
6990 case 3200000:
6991 return cdclk_sel ? 320000 : 228571;
6992 default:
6993 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u, CFGC=0x%04x\n", vco, tmp);
6994 return 222222;
6995 }
6996}
6997
6998static int i965gm_get_display_clock_speed(struct drm_device *dev)
6999{
7000 static const uint8_t div_3200[] = { 16, 10, 8 };
7001 static const uint8_t div_4000[] = { 20, 12, 10 };
7002 static const uint8_t div_5333[] = { 24, 16, 14 };
7003 const uint8_t *div_table;
7004 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
7005 uint16_t tmp = 0;
7006
7007 pci_read_config_word(dev->pdev, GCFGC, &tmp);
7008
7009 cdclk_sel = ((tmp >> 8) & 0x1f) - 1;
7010
7011 if (cdclk_sel >= ARRAY_SIZE(div_3200))
7012 goto fail;
7013
7014 switch (vco) {
7015 case 3200000:
7016 div_table = div_3200;
7017 break;
7018 case 4000000:
7019 div_table = div_4000;
7020 break;
7021 case 5333333:
7022 div_table = div_5333;
7023 break;
7024 default:
7025 goto fail;
7026 }
7027
7028 return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
7029
caf4e252 7030fail:
34edce2f
VS
7031 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%04x\n", vco, tmp);
7032 return 200000;
7033}
7034
7035static int g33_get_display_clock_speed(struct drm_device *dev)
7036{
7037 static const uint8_t div_3200[] = { 12, 10, 8, 7, 5, 16 };
7038 static const uint8_t div_4000[] = { 14, 12, 10, 8, 6, 20 };
7039 static const uint8_t div_4800[] = { 20, 14, 12, 10, 8, 24 };
7040 static const uint8_t div_5333[] = { 20, 16, 12, 12, 8, 28 };
7041 const uint8_t *div_table;
7042 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
7043 uint16_t tmp = 0;
7044
7045 pci_read_config_word(dev->pdev, GCFGC, &tmp);
7046
7047 cdclk_sel = (tmp >> 4) & 0x7;
7048
7049 if (cdclk_sel >= ARRAY_SIZE(div_3200))
7050 goto fail;
7051
7052 switch (vco) {
7053 case 3200000:
7054 div_table = div_3200;
7055 break;
7056 case 4000000:
7057 div_table = div_4000;
7058 break;
7059 case 4800000:
7060 div_table = div_4800;
7061 break;
7062 case 5333333:
7063 div_table = div_5333;
7064 break;
7065 default:
7066 goto fail;
7067 }
7068
7069 return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
7070
caf4e252 7071fail:
34edce2f
VS
7072 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%08x\n", vco, tmp);
7073 return 190476;
7074}
7075
2c07245f 7076static void
a65851af 7077intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
2c07245f 7078{
a65851af
VS
7079 while (*num > DATA_LINK_M_N_MASK ||
7080 *den > DATA_LINK_M_N_MASK) {
2c07245f
ZW
7081 *num >>= 1;
7082 *den >>= 1;
7083 }
7084}
7085
a65851af
VS
7086static void compute_m_n(unsigned int m, unsigned int n,
7087 uint32_t *ret_m, uint32_t *ret_n)
7088{
7089 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
7090 *ret_m = div_u64((uint64_t) m * *ret_n, n);
7091 intel_reduce_m_n_ratio(ret_m, ret_n);
7092}
7093
e69d0bc1
DV
7094void
7095intel_link_compute_m_n(int bits_per_pixel, int nlanes,
7096 int pixel_clock, int link_clock,
7097 struct intel_link_m_n *m_n)
2c07245f 7098{
e69d0bc1 7099 m_n->tu = 64;
a65851af
VS
7100
7101 compute_m_n(bits_per_pixel * pixel_clock,
7102 link_clock * nlanes * 8,
7103 &m_n->gmch_m, &m_n->gmch_n);
7104
7105 compute_m_n(pixel_clock, link_clock,
7106 &m_n->link_m, &m_n->link_n);
2c07245f
ZW
7107}
7108
a7615030
CW
7109static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
7110{
d330a953
JN
7111 if (i915.panel_use_ssc >= 0)
7112 return i915.panel_use_ssc != 0;
41aa3448 7113 return dev_priv->vbt.lvds_use_ssc
435793df 7114 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
a7615030
CW
7115}
7116
7429e9d4 7117static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
c65d77d8 7118{
7df00d7a 7119 return (1 << dpll->n) << 16 | dpll->m2;
7429e9d4 7120}
f47709a9 7121
7429e9d4
DV
7122static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
7123{
7124 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
c65d77d8
JB
7125}
7126
f47709a9 7127static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
190f68c5 7128 struct intel_crtc_state *crtc_state,
9e2c8475 7129 struct dpll *reduced_clock)
a7516a05 7130{
f47709a9 7131 struct drm_device *dev = crtc->base.dev;
a7516a05
JB
7132 u32 fp, fp2 = 0;
7133
7134 if (IS_PINEVIEW(dev)) {
190f68c5 7135 fp = pnv_dpll_compute_fp(&crtc_state->dpll);
a7516a05 7136 if (reduced_clock)
7429e9d4 7137 fp2 = pnv_dpll_compute_fp(reduced_clock);
a7516a05 7138 } else {
190f68c5 7139 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
a7516a05 7140 if (reduced_clock)
7429e9d4 7141 fp2 = i9xx_dpll_compute_fp(reduced_clock);
a7516a05
JB
7142 }
7143
190f68c5 7144 crtc_state->dpll_hw_state.fp0 = fp;
a7516a05 7145
f47709a9 7146 crtc->lowfreq_avail = false;
2d84d2b3 7147 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
ab585dea 7148 reduced_clock) {
190f68c5 7149 crtc_state->dpll_hw_state.fp1 = fp2;
f47709a9 7150 crtc->lowfreq_avail = true;
a7516a05 7151 } else {
190f68c5 7152 crtc_state->dpll_hw_state.fp1 = fp;
a7516a05
JB
7153 }
7154}
7155
5e69f97f
CML
7156static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
7157 pipe)
89b667f8
JB
7158{
7159 u32 reg_val;
7160
7161 /*
7162 * PLLB opamp always calibrates to max value of 0x3f, force enable it
7163 * and set it to a reasonable value instead.
7164 */
ab3c759a 7165 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
89b667f8
JB
7166 reg_val &= 0xffffff00;
7167 reg_val |= 0x00000030;
ab3c759a 7168 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
89b667f8 7169
ab3c759a 7170 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
89b667f8
JB
7171 reg_val &= 0x8cffffff;
7172 reg_val = 0x8c000000;
ab3c759a 7173 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
89b667f8 7174
ab3c759a 7175 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
89b667f8 7176 reg_val &= 0xffffff00;
ab3c759a 7177 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
89b667f8 7178
ab3c759a 7179 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
89b667f8
JB
7180 reg_val &= 0x00ffffff;
7181 reg_val |= 0xb0000000;
ab3c759a 7182 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
89b667f8
JB
7183}
7184
b551842d
DV
7185static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
7186 struct intel_link_m_n *m_n)
7187{
7188 struct drm_device *dev = crtc->base.dev;
fac5e23e 7189 struct drm_i915_private *dev_priv = to_i915(dev);
b551842d
DV
7190 int pipe = crtc->pipe;
7191
e3b95f1e
DV
7192 I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7193 I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
7194 I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
7195 I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
b551842d
DV
7196}
7197
7198static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
f769cd24
VK
7199 struct intel_link_m_n *m_n,
7200 struct intel_link_m_n *m2_n2)
b551842d
DV
7201{
7202 struct drm_device *dev = crtc->base.dev;
fac5e23e 7203 struct drm_i915_private *dev_priv = to_i915(dev);
b551842d 7204 int pipe = crtc->pipe;
6e3c9717 7205 enum transcoder transcoder = crtc->config->cpu_transcoder;
b551842d
DV
7206
7207 if (INTEL_INFO(dev)->gen >= 5) {
7208 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
7209 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
7210 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
7211 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
f769cd24
VK
7212 /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
7213 * for gen < 8) and if DRRS is supported (to make sure the
7214 * registers are not unnecessarily accessed).
7215 */
44395bfe 7216 if (m2_n2 && (IS_CHERRYVIEW(dev) || INTEL_INFO(dev)->gen < 8) &&
6e3c9717 7217 crtc->config->has_drrs) {
f769cd24
VK
7218 I915_WRITE(PIPE_DATA_M2(transcoder),
7219 TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
7220 I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
7221 I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
7222 I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
7223 }
b551842d 7224 } else {
e3b95f1e
DV
7225 I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7226 I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
7227 I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
7228 I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
b551842d
DV
7229 }
7230}
7231
fe3cd48d 7232void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
03afc4a2 7233{
fe3cd48d
R
7234 struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
7235
7236 if (m_n == M1_N1) {
7237 dp_m_n = &crtc->config->dp_m_n;
7238 dp_m2_n2 = &crtc->config->dp_m2_n2;
7239 } else if (m_n == M2_N2) {
7240
7241 /*
7242 * M2_N2 registers are not supported. Hence m2_n2 divider value
7243 * needs to be programmed into M1_N1.
7244 */
7245 dp_m_n = &crtc->config->dp_m2_n2;
7246 } else {
7247 DRM_ERROR("Unsupported divider value\n");
7248 return;
7249 }
7250
6e3c9717
ACO
7251 if (crtc->config->has_pch_encoder)
7252 intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
03afc4a2 7253 else
fe3cd48d 7254 intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
03afc4a2
DV
7255}
7256
251ac862
DV
7257static void vlv_compute_dpll(struct intel_crtc *crtc,
7258 struct intel_crtc_state *pipe_config)
bdd4b6a6 7259{
03ed5cbf 7260 pipe_config->dpll_hw_state.dpll = DPLL_INTEGRATED_REF_CLK_VLV |
cd2d34d9 7261 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
03ed5cbf
VS
7262 if (crtc->pipe != PIPE_A)
7263 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
bdd4b6a6 7264
cd2d34d9 7265 /* DPLL not used with DSI, but still need the rest set up */
d7edc4e5 7266 if (!intel_crtc_has_type(pipe_config, INTEL_OUTPUT_DSI))
cd2d34d9
VS
7267 pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE |
7268 DPLL_EXT_BUFFER_ENABLE_VLV;
7269
03ed5cbf
VS
7270 pipe_config->dpll_hw_state.dpll_md =
7271 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
7272}
bdd4b6a6 7273
03ed5cbf
VS
7274static void chv_compute_dpll(struct intel_crtc *crtc,
7275 struct intel_crtc_state *pipe_config)
7276{
7277 pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLK_CHV |
cd2d34d9 7278 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
03ed5cbf
VS
7279 if (crtc->pipe != PIPE_A)
7280 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
7281
cd2d34d9 7282 /* DPLL not used with DSI, but still need the rest set up */
d7edc4e5 7283 if (!intel_crtc_has_type(pipe_config, INTEL_OUTPUT_DSI))
cd2d34d9
VS
7284 pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE;
7285
03ed5cbf
VS
7286 pipe_config->dpll_hw_state.dpll_md =
7287 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
bdd4b6a6
DV
7288}
7289
d288f65f 7290static void vlv_prepare_pll(struct intel_crtc *crtc,
5cec258b 7291 const struct intel_crtc_state *pipe_config)
a0c4da24 7292{
f47709a9 7293 struct drm_device *dev = crtc->base.dev;
fac5e23e 7294 struct drm_i915_private *dev_priv = to_i915(dev);
cd2d34d9 7295 enum pipe pipe = crtc->pipe;
bdd4b6a6 7296 u32 mdiv;
a0c4da24 7297 u32 bestn, bestm1, bestm2, bestp1, bestp2;
bdd4b6a6 7298 u32 coreclk, reg_val;
a0c4da24 7299
cd2d34d9
VS
7300 /* Enable Refclk */
7301 I915_WRITE(DPLL(pipe),
7302 pipe_config->dpll_hw_state.dpll &
7303 ~(DPLL_VCO_ENABLE | DPLL_EXT_BUFFER_ENABLE_VLV));
7304
7305 /* No need to actually set up the DPLL with DSI */
7306 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
7307 return;
7308
a580516d 7309 mutex_lock(&dev_priv->sb_lock);
09153000 7310
d288f65f
VS
7311 bestn = pipe_config->dpll.n;
7312 bestm1 = pipe_config->dpll.m1;
7313 bestm2 = pipe_config->dpll.m2;
7314 bestp1 = pipe_config->dpll.p1;
7315 bestp2 = pipe_config->dpll.p2;
a0c4da24 7316
89b667f8
JB
7317 /* See eDP HDMI DPIO driver vbios notes doc */
7318
7319 /* PLL B needs special handling */
bdd4b6a6 7320 if (pipe == PIPE_B)
5e69f97f 7321 vlv_pllb_recal_opamp(dev_priv, pipe);
89b667f8
JB
7322
7323 /* Set up Tx target for periodic Rcomp update */
ab3c759a 7324 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
89b667f8
JB
7325
7326 /* Disable target IRef on PLL */
ab3c759a 7327 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
89b667f8 7328 reg_val &= 0x00ffffff;
ab3c759a 7329 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
89b667f8
JB
7330
7331 /* Disable fast lock */
ab3c759a 7332 vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
89b667f8
JB
7333
7334 /* Set idtafcrecal before PLL is enabled */
a0c4da24
JB
7335 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
7336 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
7337 mdiv |= ((bestn << DPIO_N_SHIFT));
a0c4da24 7338 mdiv |= (1 << DPIO_K_SHIFT);
7df5080b
JB
7339
7340 /*
7341 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
7342 * but we don't support that).
7343 * Note: don't use the DAC post divider as it seems unstable.
7344 */
7345 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
ab3c759a 7346 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
a0c4da24 7347
a0c4da24 7348 mdiv |= DPIO_ENABLE_CALIBRATION;
ab3c759a 7349 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
a0c4da24 7350
89b667f8 7351 /* Set HBR and RBR LPF coefficients */
d288f65f 7352 if (pipe_config->port_clock == 162000 ||
2d84d2b3
VS
7353 intel_crtc_has_type(crtc->config, INTEL_OUTPUT_ANALOG) ||
7354 intel_crtc_has_type(crtc->config, INTEL_OUTPUT_HDMI))
ab3c759a 7355 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
885b0120 7356 0x009f0003);
89b667f8 7357 else
ab3c759a 7358 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
89b667f8
JB
7359 0x00d0000f);
7360
37a5650b 7361 if (intel_crtc_has_dp_encoder(pipe_config)) {
89b667f8 7362 /* Use SSC source */
bdd4b6a6 7363 if (pipe == PIPE_A)
ab3c759a 7364 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
7365 0x0df40000);
7366 else
ab3c759a 7367 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
7368 0x0df70000);
7369 } else { /* HDMI or VGA */
7370 /* Use bend source */
bdd4b6a6 7371 if (pipe == PIPE_A)
ab3c759a 7372 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
7373 0x0df70000);
7374 else
ab3c759a 7375 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
7376 0x0df40000);
7377 }
a0c4da24 7378
ab3c759a 7379 coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
89b667f8 7380 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
2210ce7f 7381 if (intel_crtc_has_dp_encoder(crtc->config))
89b667f8 7382 coreclk |= 0x01000000;
ab3c759a 7383 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
a0c4da24 7384
ab3c759a 7385 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
a580516d 7386 mutex_unlock(&dev_priv->sb_lock);
a0c4da24
JB
7387}
7388
d288f65f 7389static void chv_prepare_pll(struct intel_crtc *crtc,
5cec258b 7390 const struct intel_crtc_state *pipe_config)
9d556c99
CML
7391{
7392 struct drm_device *dev = crtc->base.dev;
fac5e23e 7393 struct drm_i915_private *dev_priv = to_i915(dev);
cd2d34d9 7394 enum pipe pipe = crtc->pipe;
9d556c99 7395 enum dpio_channel port = vlv_pipe_to_channel(pipe);
9cbe40c1 7396 u32 loopfilter, tribuf_calcntr;
9d556c99 7397 u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
a945ce7e 7398 u32 dpio_val;
9cbe40c1 7399 int vco;
9d556c99 7400
cd2d34d9
VS
7401 /* Enable Refclk and SSC */
7402 I915_WRITE(DPLL(pipe),
7403 pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
7404
7405 /* No need to actually set up the DPLL with DSI */
7406 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
7407 return;
7408
d288f65f
VS
7409 bestn = pipe_config->dpll.n;
7410 bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
7411 bestm1 = pipe_config->dpll.m1;
7412 bestm2 = pipe_config->dpll.m2 >> 22;
7413 bestp1 = pipe_config->dpll.p1;
7414 bestp2 = pipe_config->dpll.p2;
9cbe40c1 7415 vco = pipe_config->dpll.vco;
a945ce7e 7416 dpio_val = 0;
9cbe40c1 7417 loopfilter = 0;
9d556c99 7418
a580516d 7419 mutex_lock(&dev_priv->sb_lock);
9d556c99 7420
9d556c99
CML
7421 /* p1 and p2 divider */
7422 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
7423 5 << DPIO_CHV_S1_DIV_SHIFT |
7424 bestp1 << DPIO_CHV_P1_DIV_SHIFT |
7425 bestp2 << DPIO_CHV_P2_DIV_SHIFT |
7426 1 << DPIO_CHV_K_DIV_SHIFT);
7427
7428 /* Feedback post-divider - m2 */
7429 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
7430
7431 /* Feedback refclk divider - n and m1 */
7432 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
7433 DPIO_CHV_M1_DIV_BY_2 |
7434 1 << DPIO_CHV_N_DIV_SHIFT);
7435
7436 /* M2 fraction division */
25a25dfc 7437 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
9d556c99
CML
7438
7439 /* M2 fraction division enable */
a945ce7e
VP
7440 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
7441 dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
7442 dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
7443 if (bestm2_frac)
7444 dpio_val |= DPIO_CHV_FRAC_DIV_EN;
7445 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
9d556c99 7446
de3a0fde
VP
7447 /* Program digital lock detect threshold */
7448 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
7449 dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
7450 DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
7451 dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
7452 if (!bestm2_frac)
7453 dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
7454 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
7455
9d556c99 7456 /* Loop filter */
9cbe40c1
VP
7457 if (vco == 5400000) {
7458 loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
7459 loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
7460 loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
7461 tribuf_calcntr = 0x9;
7462 } else if (vco <= 6200000) {
7463 loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
7464 loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
7465 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7466 tribuf_calcntr = 0x9;
7467 } else if (vco <= 6480000) {
7468 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7469 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7470 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7471 tribuf_calcntr = 0x8;
7472 } else {
7473 /* Not supported. Apply the same limits as in the max case */
7474 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7475 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7476 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7477 tribuf_calcntr = 0;
7478 }
9d556c99
CML
7479 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
7480
968040b2 7481 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
9cbe40c1
VP
7482 dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
7483 dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
7484 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
7485
9d556c99
CML
7486 /* AFC Recal */
7487 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
7488 vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
7489 DPIO_AFC_RECAL);
7490
a580516d 7491 mutex_unlock(&dev_priv->sb_lock);
9d556c99
CML
7492}
7493
d288f65f
VS
7494/**
7495 * vlv_force_pll_on - forcibly enable just the PLL
7496 * @dev_priv: i915 private structure
7497 * @pipe: pipe PLL to enable
7498 * @dpll: PLL configuration
7499 *
7500 * Enable the PLL for @pipe using the supplied @dpll config. To be used
7501 * in cases where we need the PLL enabled even when @pipe is not going to
7502 * be enabled.
7503 */
3f36b937
TU
7504int vlv_force_pll_on(struct drm_device *dev, enum pipe pipe,
7505 const struct dpll *dpll)
d288f65f
VS
7506{
7507 struct intel_crtc *crtc =
7508 to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
3f36b937
TU
7509 struct intel_crtc_state *pipe_config;
7510
7511 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
7512 if (!pipe_config)
7513 return -ENOMEM;
7514
7515 pipe_config->base.crtc = &crtc->base;
7516 pipe_config->pixel_multiplier = 1;
7517 pipe_config->dpll = *dpll;
d288f65f
VS
7518
7519 if (IS_CHERRYVIEW(dev)) {
3f36b937
TU
7520 chv_compute_dpll(crtc, pipe_config);
7521 chv_prepare_pll(crtc, pipe_config);
7522 chv_enable_pll(crtc, pipe_config);
d288f65f 7523 } else {
3f36b937
TU
7524 vlv_compute_dpll(crtc, pipe_config);
7525 vlv_prepare_pll(crtc, pipe_config);
7526 vlv_enable_pll(crtc, pipe_config);
d288f65f 7527 }
3f36b937
TU
7528
7529 kfree(pipe_config);
7530
7531 return 0;
d288f65f
VS
7532}
7533
7534/**
7535 * vlv_force_pll_off - forcibly disable just the PLL
7536 * @dev_priv: i915 private structure
7537 * @pipe: pipe PLL to disable
7538 *
7539 * Disable the PLL for @pipe. To be used in cases where we need
7540 * the PLL enabled even when @pipe is not going to be enabled.
7541 */
7542void vlv_force_pll_off(struct drm_device *dev, enum pipe pipe)
7543{
7544 if (IS_CHERRYVIEW(dev))
7545 chv_disable_pll(to_i915(dev), pipe);
7546 else
7547 vlv_disable_pll(to_i915(dev), pipe);
7548}
7549
251ac862
DV
7550static void i9xx_compute_dpll(struct intel_crtc *crtc,
7551 struct intel_crtc_state *crtc_state,
9e2c8475 7552 struct dpll *reduced_clock)
eb1cbe48 7553{
f47709a9 7554 struct drm_device *dev = crtc->base.dev;
fac5e23e 7555 struct drm_i915_private *dev_priv = to_i915(dev);
eb1cbe48 7556 u32 dpll;
190f68c5 7557 struct dpll *clock = &crtc_state->dpll;
eb1cbe48 7558
190f68c5 7559 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
2a8f64ca 7560
eb1cbe48
DV
7561 dpll = DPLL_VGA_MODE_DIS;
7562
2d84d2b3 7563 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS))
eb1cbe48
DV
7564 dpll |= DPLLB_MODE_LVDS;
7565 else
7566 dpll |= DPLLB_MODE_DAC_SERIAL;
6cc5f341 7567
ef1b460d 7568 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
190f68c5 7569 dpll |= (crtc_state->pixel_multiplier - 1)
198a037f 7570 << SDVO_MULTIPLIER_SHIFT_HIRES;
eb1cbe48 7571 }
198a037f 7572
3d6e9ee0
VS
7573 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO) ||
7574 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
4a33e48d 7575 dpll |= DPLL_SDVO_HIGH_SPEED;
198a037f 7576
37a5650b 7577 if (intel_crtc_has_dp_encoder(crtc_state))
4a33e48d 7578 dpll |= DPLL_SDVO_HIGH_SPEED;
eb1cbe48
DV
7579
7580 /* compute bitmask from p1 value */
7581 if (IS_PINEVIEW(dev))
7582 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
7583 else {
7584 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7585 if (IS_G4X(dev) && reduced_clock)
7586 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
7587 }
7588 switch (clock->p2) {
7589 case 5:
7590 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
7591 break;
7592 case 7:
7593 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
7594 break;
7595 case 10:
7596 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
7597 break;
7598 case 14:
7599 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
7600 break;
7601 }
7602 if (INTEL_INFO(dev)->gen >= 4)
7603 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
7604
190f68c5 7605 if (crtc_state->sdvo_tv_clock)
eb1cbe48 7606 dpll |= PLL_REF_INPUT_TVCLKINBC;
2d84d2b3 7607 else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
ceb41007 7608 intel_panel_use_ssc(dev_priv))
eb1cbe48
DV
7609 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7610 else
7611 dpll |= PLL_REF_INPUT_DREFCLK;
7612
7613 dpll |= DPLL_VCO_ENABLE;
190f68c5 7614 crtc_state->dpll_hw_state.dpll = dpll;
8bcc2795 7615
eb1cbe48 7616 if (INTEL_INFO(dev)->gen >= 4) {
190f68c5 7617 u32 dpll_md = (crtc_state->pixel_multiplier - 1)
ef1b460d 7618 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
190f68c5 7619 crtc_state->dpll_hw_state.dpll_md = dpll_md;
eb1cbe48
DV
7620 }
7621}
7622
251ac862
DV
7623static void i8xx_compute_dpll(struct intel_crtc *crtc,
7624 struct intel_crtc_state *crtc_state,
9e2c8475 7625 struct dpll *reduced_clock)
eb1cbe48 7626{
f47709a9 7627 struct drm_device *dev = crtc->base.dev;
fac5e23e 7628 struct drm_i915_private *dev_priv = to_i915(dev);
eb1cbe48 7629 u32 dpll;
190f68c5 7630 struct dpll *clock = &crtc_state->dpll;
eb1cbe48 7631
190f68c5 7632 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
2a8f64ca 7633
eb1cbe48
DV
7634 dpll = DPLL_VGA_MODE_DIS;
7635
2d84d2b3 7636 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
eb1cbe48
DV
7637 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7638 } else {
7639 if (clock->p1 == 2)
7640 dpll |= PLL_P1_DIVIDE_BY_TWO;
7641 else
7642 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7643 if (clock->p2 == 4)
7644 dpll |= PLL_P2_DIVIDE_BY_4;
7645 }
7646
2d84d2b3 7647 if (!IS_I830(dev) && intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DVO))
4a33e48d
DV
7648 dpll |= DPLL_DVO_2X_MODE;
7649
2d84d2b3 7650 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
ceb41007 7651 intel_panel_use_ssc(dev_priv))
eb1cbe48
DV
7652 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7653 else
7654 dpll |= PLL_REF_INPUT_DREFCLK;
7655
7656 dpll |= DPLL_VCO_ENABLE;
190f68c5 7657 crtc_state->dpll_hw_state.dpll = dpll;
eb1cbe48
DV
7658}
7659
8a654f3b 7660static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
b0e77b9c
PZ
7661{
7662 struct drm_device *dev = intel_crtc->base.dev;
fac5e23e 7663 struct drm_i915_private *dev_priv = to_i915(dev);
b0e77b9c 7664 enum pipe pipe = intel_crtc->pipe;
6e3c9717 7665 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
7c5f93b0 7666 const struct drm_display_mode *adjusted_mode = &intel_crtc->config->base.adjusted_mode;
1caea6e9
VS
7667 uint32_t crtc_vtotal, crtc_vblank_end;
7668 int vsyncshift = 0;
4d8a62ea
DV
7669
7670 /* We need to be careful not to changed the adjusted mode, for otherwise
7671 * the hw state checker will get angry at the mismatch. */
7672 crtc_vtotal = adjusted_mode->crtc_vtotal;
7673 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
b0e77b9c 7674
609aeaca 7675 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
b0e77b9c 7676 /* the chip adds 2 halflines automatically */
4d8a62ea
DV
7677 crtc_vtotal -= 1;
7678 crtc_vblank_end -= 1;
609aeaca 7679
2d84d2b3 7680 if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
609aeaca
VS
7681 vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
7682 else
7683 vsyncshift = adjusted_mode->crtc_hsync_start -
7684 adjusted_mode->crtc_htotal / 2;
1caea6e9
VS
7685 if (vsyncshift < 0)
7686 vsyncshift += adjusted_mode->crtc_htotal;
b0e77b9c
PZ
7687 }
7688
7689 if (INTEL_INFO(dev)->gen > 3)
fe2b8f9d 7690 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
b0e77b9c 7691
fe2b8f9d 7692 I915_WRITE(HTOTAL(cpu_transcoder),
b0e77b9c
PZ
7693 (adjusted_mode->crtc_hdisplay - 1) |
7694 ((adjusted_mode->crtc_htotal - 1) << 16));
fe2b8f9d 7695 I915_WRITE(HBLANK(cpu_transcoder),
b0e77b9c
PZ
7696 (adjusted_mode->crtc_hblank_start - 1) |
7697 ((adjusted_mode->crtc_hblank_end - 1) << 16));
fe2b8f9d 7698 I915_WRITE(HSYNC(cpu_transcoder),
b0e77b9c
PZ
7699 (adjusted_mode->crtc_hsync_start - 1) |
7700 ((adjusted_mode->crtc_hsync_end - 1) << 16));
7701
fe2b8f9d 7702 I915_WRITE(VTOTAL(cpu_transcoder),
b0e77b9c 7703 (adjusted_mode->crtc_vdisplay - 1) |
4d8a62ea 7704 ((crtc_vtotal - 1) << 16));
fe2b8f9d 7705 I915_WRITE(VBLANK(cpu_transcoder),
b0e77b9c 7706 (adjusted_mode->crtc_vblank_start - 1) |
4d8a62ea 7707 ((crtc_vblank_end - 1) << 16));
fe2b8f9d 7708 I915_WRITE(VSYNC(cpu_transcoder),
b0e77b9c
PZ
7709 (adjusted_mode->crtc_vsync_start - 1) |
7710 ((adjusted_mode->crtc_vsync_end - 1) << 16));
7711
b5e508d4
PZ
7712 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
7713 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
7714 * documented on the DDI_FUNC_CTL register description, EDP Input Select
7715 * bits. */
7716 if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
7717 (pipe == PIPE_B || pipe == PIPE_C))
7718 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
7719
bc58be60
JN
7720}
7721
7722static void intel_set_pipe_src_size(struct intel_crtc *intel_crtc)
7723{
7724 struct drm_device *dev = intel_crtc->base.dev;
fac5e23e 7725 struct drm_i915_private *dev_priv = to_i915(dev);
bc58be60
JN
7726 enum pipe pipe = intel_crtc->pipe;
7727
b0e77b9c
PZ
7728 /* pipesrc controls the size that is scaled from, which should
7729 * always be the user's requested size.
7730 */
7731 I915_WRITE(PIPESRC(pipe),
6e3c9717
ACO
7732 ((intel_crtc->config->pipe_src_w - 1) << 16) |
7733 (intel_crtc->config->pipe_src_h - 1));
b0e77b9c
PZ
7734}
7735
1bd1bd80 7736static void intel_get_pipe_timings(struct intel_crtc *crtc,
5cec258b 7737 struct intel_crtc_state *pipe_config)
1bd1bd80
DV
7738{
7739 struct drm_device *dev = crtc->base.dev;
fac5e23e 7740 struct drm_i915_private *dev_priv = to_i915(dev);
1bd1bd80
DV
7741 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
7742 uint32_t tmp;
7743
7744 tmp = I915_READ(HTOTAL(cpu_transcoder));
2d112de7
ACO
7745 pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
7746 pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 7747 tmp = I915_READ(HBLANK(cpu_transcoder));
2d112de7
ACO
7748 pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
7749 pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 7750 tmp = I915_READ(HSYNC(cpu_transcoder));
2d112de7
ACO
7751 pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
7752 pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80
DV
7753
7754 tmp = I915_READ(VTOTAL(cpu_transcoder));
2d112de7
ACO
7755 pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
7756 pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 7757 tmp = I915_READ(VBLANK(cpu_transcoder));
2d112de7
ACO
7758 pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
7759 pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 7760 tmp = I915_READ(VSYNC(cpu_transcoder));
2d112de7
ACO
7761 pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
7762 pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80
DV
7763
7764 if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
2d112de7
ACO
7765 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
7766 pipe_config->base.adjusted_mode.crtc_vtotal += 1;
7767 pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
1bd1bd80 7768 }
bc58be60
JN
7769}
7770
7771static void intel_get_pipe_src_size(struct intel_crtc *crtc,
7772 struct intel_crtc_state *pipe_config)
7773{
7774 struct drm_device *dev = crtc->base.dev;
fac5e23e 7775 struct drm_i915_private *dev_priv = to_i915(dev);
bc58be60 7776 u32 tmp;
1bd1bd80
DV
7777
7778 tmp = I915_READ(PIPESRC(crtc->pipe));
37327abd
VS
7779 pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
7780 pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
7781
2d112de7
ACO
7782 pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
7783 pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
1bd1bd80
DV
7784}
7785
f6a83288 7786void intel_mode_from_pipe_config(struct drm_display_mode *mode,
5cec258b 7787 struct intel_crtc_state *pipe_config)
babea61d 7788{
2d112de7
ACO
7789 mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
7790 mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
7791 mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
7792 mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
babea61d 7793
2d112de7
ACO
7794 mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
7795 mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
7796 mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
7797 mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
babea61d 7798
2d112de7 7799 mode->flags = pipe_config->base.adjusted_mode.flags;
cd13f5ab 7800 mode->type = DRM_MODE_TYPE_DRIVER;
babea61d 7801
2d112de7
ACO
7802 mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
7803 mode->flags |= pipe_config->base.adjusted_mode.flags;
cd13f5ab
ML
7804
7805 mode->hsync = drm_mode_hsync(mode);
7806 mode->vrefresh = drm_mode_vrefresh(mode);
7807 drm_mode_set_name(mode);
babea61d
JB
7808}
7809
84b046f3
DV
7810static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
7811{
7812 struct drm_device *dev = intel_crtc->base.dev;
fac5e23e 7813 struct drm_i915_private *dev_priv = to_i915(dev);
84b046f3
DV
7814 uint32_t pipeconf;
7815
9f11a9e4 7816 pipeconf = 0;
84b046f3 7817
b6b5d049
VS
7818 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
7819 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
7820 pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
67c72a12 7821
6e3c9717 7822 if (intel_crtc->config->double_wide)
cf532bb2 7823 pipeconf |= PIPECONF_DOUBLE_WIDE;
84b046f3 7824
ff9ce46e 7825 /* only g4x and later have fancy bpc/dither controls */
666a4537 7826 if (IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
ff9ce46e 7827 /* Bspec claims that we can't use dithering for 30bpp pipes. */
6e3c9717 7828 if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
ff9ce46e 7829 pipeconf |= PIPECONF_DITHER_EN |
84b046f3 7830 PIPECONF_DITHER_TYPE_SP;
84b046f3 7831
6e3c9717 7832 switch (intel_crtc->config->pipe_bpp) {
ff9ce46e
DV
7833 case 18:
7834 pipeconf |= PIPECONF_6BPC;
7835 break;
7836 case 24:
7837 pipeconf |= PIPECONF_8BPC;
7838 break;
7839 case 30:
7840 pipeconf |= PIPECONF_10BPC;
7841 break;
7842 default:
7843 /* Case prevented by intel_choose_pipe_bpp_dither. */
7844 BUG();
84b046f3
DV
7845 }
7846 }
7847
7848 if (HAS_PIPE_CXSR(dev)) {
7849 if (intel_crtc->lowfreq_avail) {
7850 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
7851 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
7852 } else {
7853 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
84b046f3
DV
7854 }
7855 }
7856
6e3c9717 7857 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
efc2cfff 7858 if (INTEL_INFO(dev)->gen < 4 ||
2d84d2b3 7859 intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
efc2cfff
VS
7860 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
7861 else
7862 pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
7863 } else
84b046f3
DV
7864 pipeconf |= PIPECONF_PROGRESSIVE;
7865
666a4537
WB
7866 if ((IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) &&
7867 intel_crtc->config->limited_color_range)
9f11a9e4 7868 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
9c8e09b7 7869
84b046f3
DV
7870 I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
7871 POSTING_READ(PIPECONF(intel_crtc->pipe));
7872}
7873
81c97f52
ACO
7874static int i8xx_crtc_compute_clock(struct intel_crtc *crtc,
7875 struct intel_crtc_state *crtc_state)
7876{
7877 struct drm_device *dev = crtc->base.dev;
fac5e23e 7878 struct drm_i915_private *dev_priv = to_i915(dev);
1b6f4958 7879 const struct intel_limit *limit;
81c97f52
ACO
7880 int refclk = 48000;
7881
7882 memset(&crtc_state->dpll_hw_state, 0,
7883 sizeof(crtc_state->dpll_hw_state));
7884
2d84d2b3 7885 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
81c97f52
ACO
7886 if (intel_panel_use_ssc(dev_priv)) {
7887 refclk = dev_priv->vbt.lvds_ssc_freq;
7888 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
7889 }
7890
7891 limit = &intel_limits_i8xx_lvds;
2d84d2b3 7892 } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DVO)) {
81c97f52
ACO
7893 limit = &intel_limits_i8xx_dvo;
7894 } else {
7895 limit = &intel_limits_i8xx_dac;
7896 }
7897
7898 if (!crtc_state->clock_set &&
7899 !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7900 refclk, NULL, &crtc_state->dpll)) {
7901 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7902 return -EINVAL;
7903 }
7904
7905 i8xx_compute_dpll(crtc, crtc_state, NULL);
7906
7907 return 0;
7908}
7909
19ec6693
ACO
7910static int g4x_crtc_compute_clock(struct intel_crtc *crtc,
7911 struct intel_crtc_state *crtc_state)
7912{
7913 struct drm_device *dev = crtc->base.dev;
fac5e23e 7914 struct drm_i915_private *dev_priv = to_i915(dev);
1b6f4958 7915 const struct intel_limit *limit;
19ec6693
ACO
7916 int refclk = 96000;
7917
7918 memset(&crtc_state->dpll_hw_state, 0,
7919 sizeof(crtc_state->dpll_hw_state));
7920
2d84d2b3 7921 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
19ec6693
ACO
7922 if (intel_panel_use_ssc(dev_priv)) {
7923 refclk = dev_priv->vbt.lvds_ssc_freq;
7924 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
7925 }
7926
7927 if (intel_is_dual_link_lvds(dev))
7928 limit = &intel_limits_g4x_dual_channel_lvds;
7929 else
7930 limit = &intel_limits_g4x_single_channel_lvds;
2d84d2b3
VS
7931 } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI) ||
7932 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
19ec6693 7933 limit = &intel_limits_g4x_hdmi;
2d84d2b3 7934 } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO)) {
19ec6693
ACO
7935 limit = &intel_limits_g4x_sdvo;
7936 } else {
7937 /* The option is for other outputs */
7938 limit = &intel_limits_i9xx_sdvo;
7939 }
7940
7941 if (!crtc_state->clock_set &&
7942 !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7943 refclk, NULL, &crtc_state->dpll)) {
7944 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7945 return -EINVAL;
7946 }
7947
7948 i9xx_compute_dpll(crtc, crtc_state, NULL);
7949
7950 return 0;
7951}
7952
70e8aa21
ACO
7953static int pnv_crtc_compute_clock(struct intel_crtc *crtc,
7954 struct intel_crtc_state *crtc_state)
7955{
7956 struct drm_device *dev = crtc->base.dev;
fac5e23e 7957 struct drm_i915_private *dev_priv = to_i915(dev);
1b6f4958 7958 const struct intel_limit *limit;
70e8aa21
ACO
7959 int refclk = 96000;
7960
7961 memset(&crtc_state->dpll_hw_state, 0,
7962 sizeof(crtc_state->dpll_hw_state));
7963
2d84d2b3 7964 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
70e8aa21
ACO
7965 if (intel_panel_use_ssc(dev_priv)) {
7966 refclk = dev_priv->vbt.lvds_ssc_freq;
7967 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
7968 }
7969
7970 limit = &intel_limits_pineview_lvds;
7971 } else {
7972 limit = &intel_limits_pineview_sdvo;
7973 }
7974
7975 if (!crtc_state->clock_set &&
7976 !pnv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7977 refclk, NULL, &crtc_state->dpll)) {
7978 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7979 return -EINVAL;
7980 }
7981
7982 i9xx_compute_dpll(crtc, crtc_state, NULL);
7983
7984 return 0;
7985}
7986
190f68c5
ACO
7987static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
7988 struct intel_crtc_state *crtc_state)
79e53945 7989{
c7653199 7990 struct drm_device *dev = crtc->base.dev;
fac5e23e 7991 struct drm_i915_private *dev_priv = to_i915(dev);
1b6f4958 7992 const struct intel_limit *limit;
81c97f52 7993 int refclk = 96000;
79e53945 7994
dd3cd74a
ACO
7995 memset(&crtc_state->dpll_hw_state, 0,
7996 sizeof(crtc_state->dpll_hw_state));
7997
2d84d2b3 7998 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
70e8aa21
ACO
7999 if (intel_panel_use_ssc(dev_priv)) {
8000 refclk = dev_priv->vbt.lvds_ssc_freq;
8001 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
8002 }
43565a06 8003
70e8aa21
ACO
8004 limit = &intel_limits_i9xx_lvds;
8005 } else {
8006 limit = &intel_limits_i9xx_sdvo;
81c97f52 8007 }
79e53945 8008
70e8aa21
ACO
8009 if (!crtc_state->clock_set &&
8010 !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8011 refclk, NULL, &crtc_state->dpll)) {
8012 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8013 return -EINVAL;
f47709a9 8014 }
7026d4ac 8015
81c97f52 8016 i9xx_compute_dpll(crtc, crtc_state, NULL);
79e53945 8017
c8f7a0db 8018 return 0;
f564048e
EA
8019}
8020
65b3d6a9
ACO
8021static int chv_crtc_compute_clock(struct intel_crtc *crtc,
8022 struct intel_crtc_state *crtc_state)
8023{
8024 int refclk = 100000;
1b6f4958 8025 const struct intel_limit *limit = &intel_limits_chv;
65b3d6a9
ACO
8026
8027 memset(&crtc_state->dpll_hw_state, 0,
8028 sizeof(crtc_state->dpll_hw_state));
8029
65b3d6a9
ACO
8030 if (!crtc_state->clock_set &&
8031 !chv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8032 refclk, NULL, &crtc_state->dpll)) {
8033 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8034 return -EINVAL;
8035 }
8036
8037 chv_compute_dpll(crtc, crtc_state);
8038
8039 return 0;
8040}
8041
8042static int vlv_crtc_compute_clock(struct intel_crtc *crtc,
8043 struct intel_crtc_state *crtc_state)
8044{
8045 int refclk = 100000;
1b6f4958 8046 const struct intel_limit *limit = &intel_limits_vlv;
65b3d6a9
ACO
8047
8048 memset(&crtc_state->dpll_hw_state, 0,
8049 sizeof(crtc_state->dpll_hw_state));
8050
65b3d6a9
ACO
8051 if (!crtc_state->clock_set &&
8052 !vlv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8053 refclk, NULL, &crtc_state->dpll)) {
8054 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8055 return -EINVAL;
8056 }
8057
8058 vlv_compute_dpll(crtc, crtc_state);
8059
8060 return 0;
8061}
8062
2fa2fe9a 8063static void i9xx_get_pfit_config(struct intel_crtc *crtc,
5cec258b 8064 struct intel_crtc_state *pipe_config)
2fa2fe9a
DV
8065{
8066 struct drm_device *dev = crtc->base.dev;
fac5e23e 8067 struct drm_i915_private *dev_priv = to_i915(dev);
2fa2fe9a
DV
8068 uint32_t tmp;
8069
dc9e7dec
VS
8070 if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
8071 return;
8072
2fa2fe9a 8073 tmp = I915_READ(PFIT_CONTROL);
06922821
DV
8074 if (!(tmp & PFIT_ENABLE))
8075 return;
2fa2fe9a 8076
06922821 8077 /* Check whether the pfit is attached to our pipe. */
2fa2fe9a
DV
8078 if (INTEL_INFO(dev)->gen < 4) {
8079 if (crtc->pipe != PIPE_B)
8080 return;
2fa2fe9a
DV
8081 } else {
8082 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
8083 return;
8084 }
8085
06922821 8086 pipe_config->gmch_pfit.control = tmp;
2fa2fe9a 8087 pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
2fa2fe9a
DV
8088}
8089
acbec814 8090static void vlv_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 8091 struct intel_crtc_state *pipe_config)
acbec814
JB
8092{
8093 struct drm_device *dev = crtc->base.dev;
fac5e23e 8094 struct drm_i915_private *dev_priv = to_i915(dev);
acbec814 8095 int pipe = pipe_config->cpu_transcoder;
9e2c8475 8096 struct dpll clock;
acbec814 8097 u32 mdiv;
662c6ecb 8098 int refclk = 100000;
acbec814 8099
b521973b
VS
8100 /* In case of DSI, DPLL will not be used */
8101 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
f573de5a
SK
8102 return;
8103
a580516d 8104 mutex_lock(&dev_priv->sb_lock);
ab3c759a 8105 mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
a580516d 8106 mutex_unlock(&dev_priv->sb_lock);
acbec814
JB
8107
8108 clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
8109 clock.m2 = mdiv & DPIO_M2DIV_MASK;
8110 clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
8111 clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
8112 clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
8113
dccbea3b 8114 pipe_config->port_clock = vlv_calc_dpll_params(refclk, &clock);
acbec814
JB
8115}
8116
5724dbd1
DL
8117static void
8118i9xx_get_initial_plane_config(struct intel_crtc *crtc,
8119 struct intel_initial_plane_config *plane_config)
1ad292b5
JB
8120{
8121 struct drm_device *dev = crtc->base.dev;
fac5e23e 8122 struct drm_i915_private *dev_priv = to_i915(dev);
1ad292b5
JB
8123 u32 val, base, offset;
8124 int pipe = crtc->pipe, plane = crtc->plane;
8125 int fourcc, pixel_format;
6761dd31 8126 unsigned int aligned_height;
b113d5ee 8127 struct drm_framebuffer *fb;
1b842c89 8128 struct intel_framebuffer *intel_fb;
1ad292b5 8129
42a7b088
DL
8130 val = I915_READ(DSPCNTR(plane));
8131 if (!(val & DISPLAY_PLANE_ENABLE))
8132 return;
8133
d9806c9f 8134 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
1b842c89 8135 if (!intel_fb) {
1ad292b5
JB
8136 DRM_DEBUG_KMS("failed to alloc fb\n");
8137 return;
8138 }
8139
1b842c89
DL
8140 fb = &intel_fb->base;
8141
18c5247e
DV
8142 if (INTEL_INFO(dev)->gen >= 4) {
8143 if (val & DISPPLANE_TILED) {
49af449b 8144 plane_config->tiling = I915_TILING_X;
18c5247e
DV
8145 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
8146 }
8147 }
1ad292b5
JB
8148
8149 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
b35d63fa 8150 fourcc = i9xx_format_to_fourcc(pixel_format);
b113d5ee
DL
8151 fb->pixel_format = fourcc;
8152 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
1ad292b5
JB
8153
8154 if (INTEL_INFO(dev)->gen >= 4) {
49af449b 8155 if (plane_config->tiling)
1ad292b5
JB
8156 offset = I915_READ(DSPTILEOFF(plane));
8157 else
8158 offset = I915_READ(DSPLINOFF(plane));
8159 base = I915_READ(DSPSURF(plane)) & 0xfffff000;
8160 } else {
8161 base = I915_READ(DSPADDR(plane));
8162 }
8163 plane_config->base = base;
8164
8165 val = I915_READ(PIPESRC(pipe));
b113d5ee
DL
8166 fb->width = ((val >> 16) & 0xfff) + 1;
8167 fb->height = ((val >> 0) & 0xfff) + 1;
1ad292b5
JB
8168
8169 val = I915_READ(DSPSTRIDE(pipe));
b113d5ee 8170 fb->pitches[0] = val & 0xffffffc0;
1ad292b5 8171
b113d5ee 8172 aligned_height = intel_fb_align_height(dev, fb->height,
091df6cb
DV
8173 fb->pixel_format,
8174 fb->modifier[0]);
1ad292b5 8175
f37b5c2b 8176 plane_config->size = fb->pitches[0] * aligned_height;
1ad292b5 8177
2844a921
DL
8178 DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
8179 pipe_name(pipe), plane, fb->width, fb->height,
8180 fb->bits_per_pixel, base, fb->pitches[0],
8181 plane_config->size);
1ad292b5 8182
2d14030b 8183 plane_config->fb = intel_fb;
1ad292b5
JB
8184}
8185
70b23a98 8186static void chv_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 8187 struct intel_crtc_state *pipe_config)
70b23a98
VS
8188{
8189 struct drm_device *dev = crtc->base.dev;
fac5e23e 8190 struct drm_i915_private *dev_priv = to_i915(dev);
70b23a98
VS
8191 int pipe = pipe_config->cpu_transcoder;
8192 enum dpio_channel port = vlv_pipe_to_channel(pipe);
9e2c8475 8193 struct dpll clock;
0d7b6b11 8194 u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2, pll_dw3;
70b23a98
VS
8195 int refclk = 100000;
8196
b521973b
VS
8197 /* In case of DSI, DPLL will not be used */
8198 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
8199 return;
8200
a580516d 8201 mutex_lock(&dev_priv->sb_lock);
70b23a98
VS
8202 cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
8203 pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
8204 pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
8205 pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
0d7b6b11 8206 pll_dw3 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
a580516d 8207 mutex_unlock(&dev_priv->sb_lock);
70b23a98
VS
8208
8209 clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
0d7b6b11
ID
8210 clock.m2 = (pll_dw0 & 0xff) << 22;
8211 if (pll_dw3 & DPIO_CHV_FRAC_DIV_EN)
8212 clock.m2 |= pll_dw2 & 0x3fffff;
70b23a98
VS
8213 clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
8214 clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
8215 clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
8216
dccbea3b 8217 pipe_config->port_clock = chv_calc_dpll_params(refclk, &clock);
70b23a98
VS
8218}
8219
0e8ffe1b 8220static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
5cec258b 8221 struct intel_crtc_state *pipe_config)
0e8ffe1b
DV
8222{
8223 struct drm_device *dev = crtc->base.dev;
fac5e23e 8224 struct drm_i915_private *dev_priv = to_i915(dev);
1729050e 8225 enum intel_display_power_domain power_domain;
0e8ffe1b 8226 uint32_t tmp;
1729050e 8227 bool ret;
0e8ffe1b 8228
1729050e
ID
8229 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
8230 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
b5482bd0
ID
8231 return false;
8232
e143a21c 8233 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
8106ddbd 8234 pipe_config->shared_dpll = NULL;
eccb140b 8235
1729050e
ID
8236 ret = false;
8237
0e8ffe1b
DV
8238 tmp = I915_READ(PIPECONF(crtc->pipe));
8239 if (!(tmp & PIPECONF_ENABLE))
1729050e 8240 goto out;
0e8ffe1b 8241
666a4537 8242 if (IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
42571aef
VS
8243 switch (tmp & PIPECONF_BPC_MASK) {
8244 case PIPECONF_6BPC:
8245 pipe_config->pipe_bpp = 18;
8246 break;
8247 case PIPECONF_8BPC:
8248 pipe_config->pipe_bpp = 24;
8249 break;
8250 case PIPECONF_10BPC:
8251 pipe_config->pipe_bpp = 30;
8252 break;
8253 default:
8254 break;
8255 }
8256 }
8257
666a4537
WB
8258 if ((IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) &&
8259 (tmp & PIPECONF_COLOR_RANGE_SELECT))
b5a9fa09
DV
8260 pipe_config->limited_color_range = true;
8261
282740f7
VS
8262 if (INTEL_INFO(dev)->gen < 4)
8263 pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
8264
1bd1bd80 8265 intel_get_pipe_timings(crtc, pipe_config);
bc58be60 8266 intel_get_pipe_src_size(crtc, pipe_config);
1bd1bd80 8267
2fa2fe9a
DV
8268 i9xx_get_pfit_config(crtc, pipe_config);
8269
6c49f241 8270 if (INTEL_INFO(dev)->gen >= 4) {
c231775c
VS
8271 /* No way to read it out on pipes B and C */
8272 if (IS_CHERRYVIEW(dev) && crtc->pipe != PIPE_A)
8273 tmp = dev_priv->chv_dpll_md[crtc->pipe];
8274 else
8275 tmp = I915_READ(DPLL_MD(crtc->pipe));
6c49f241
DV
8276 pipe_config->pixel_multiplier =
8277 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
8278 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
8bcc2795 8279 pipe_config->dpll_hw_state.dpll_md = tmp;
6c49f241
DV
8280 } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
8281 tmp = I915_READ(DPLL(crtc->pipe));
8282 pipe_config->pixel_multiplier =
8283 ((tmp & SDVO_MULTIPLIER_MASK)
8284 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
8285 } else {
8286 /* Note that on i915G/GM the pixel multiplier is in the sdvo
8287 * port and will be fixed up in the encoder->get_config
8288 * function. */
8289 pipe_config->pixel_multiplier = 1;
8290 }
8bcc2795 8291 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
666a4537 8292 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
1c4e0274
VS
8293 /*
8294 * DPLL_DVO_2X_MODE must be enabled for both DPLLs
8295 * on 830. Filter it out here so that we don't
8296 * report errors due to that.
8297 */
8298 if (IS_I830(dev))
8299 pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
8300
8bcc2795
DV
8301 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
8302 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
165e901c
VS
8303 } else {
8304 /* Mask out read-only status bits. */
8305 pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
8306 DPLL_PORTC_READY_MASK |
8307 DPLL_PORTB_READY_MASK);
8bcc2795 8308 }
6c49f241 8309
70b23a98
VS
8310 if (IS_CHERRYVIEW(dev))
8311 chv_crtc_clock_get(crtc, pipe_config);
8312 else if (IS_VALLEYVIEW(dev))
acbec814
JB
8313 vlv_crtc_clock_get(crtc, pipe_config);
8314 else
8315 i9xx_crtc_clock_get(crtc, pipe_config);
18442d08 8316
0f64614d
VS
8317 /*
8318 * Normally the dotclock is filled in by the encoder .get_config()
8319 * but in case the pipe is enabled w/o any ports we need a sane
8320 * default.
8321 */
8322 pipe_config->base.adjusted_mode.crtc_clock =
8323 pipe_config->port_clock / pipe_config->pixel_multiplier;
8324
1729050e
ID
8325 ret = true;
8326
8327out:
8328 intel_display_power_put(dev_priv, power_domain);
8329
8330 return ret;
0e8ffe1b
DV
8331}
8332
dde86e2d 8333static void ironlake_init_pch_refclk(struct drm_device *dev)
13d83a67 8334{
fac5e23e 8335 struct drm_i915_private *dev_priv = to_i915(dev);
13d83a67 8336 struct intel_encoder *encoder;
1c1a24d2 8337 int i;
74cfd7ac 8338 u32 val, final;
13d83a67 8339 bool has_lvds = false;
199e5d79 8340 bool has_cpu_edp = false;
199e5d79 8341 bool has_panel = false;
99eb6a01
KP
8342 bool has_ck505 = false;
8343 bool can_ssc = false;
1c1a24d2 8344 bool using_ssc_source = false;
13d83a67
JB
8345
8346 /* We need to take the global config into account */
b2784e15 8347 for_each_intel_encoder(dev, encoder) {
199e5d79
KP
8348 switch (encoder->type) {
8349 case INTEL_OUTPUT_LVDS:
8350 has_panel = true;
8351 has_lvds = true;
8352 break;
8353 case INTEL_OUTPUT_EDP:
8354 has_panel = true;
2de6905f 8355 if (enc_to_dig_port(&encoder->base)->port == PORT_A)
199e5d79
KP
8356 has_cpu_edp = true;
8357 break;
6847d71b
PZ
8358 default:
8359 break;
13d83a67
JB
8360 }
8361 }
8362
99eb6a01 8363 if (HAS_PCH_IBX(dev)) {
41aa3448 8364 has_ck505 = dev_priv->vbt.display_clock_mode;
99eb6a01
KP
8365 can_ssc = has_ck505;
8366 } else {
8367 has_ck505 = false;
8368 can_ssc = true;
8369 }
8370
1c1a24d2
L
8371 /* Check if any DPLLs are using the SSC source */
8372 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
8373 u32 temp = I915_READ(PCH_DPLL(i));
8374
8375 if (!(temp & DPLL_VCO_ENABLE))
8376 continue;
8377
8378 if ((temp & PLL_REF_INPUT_MASK) ==
8379 PLLB_REF_INPUT_SPREADSPECTRUMIN) {
8380 using_ssc_source = true;
8381 break;
8382 }
8383 }
8384
8385 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d using_ssc_source %d\n",
8386 has_panel, has_lvds, has_ck505, using_ssc_source);
13d83a67
JB
8387
8388 /* Ironlake: try to setup display ref clock before DPLL
8389 * enabling. This is only under driver's control after
8390 * PCH B stepping, previous chipset stepping should be
8391 * ignoring this setting.
8392 */
74cfd7ac
CW
8393 val = I915_READ(PCH_DREF_CONTROL);
8394
8395 /* As we must carefully and slowly disable/enable each source in turn,
8396 * compute the final state we want first and check if we need to
8397 * make any changes at all.
8398 */
8399 final = val;
8400 final &= ~DREF_NONSPREAD_SOURCE_MASK;
8401 if (has_ck505)
8402 final |= DREF_NONSPREAD_CK505_ENABLE;
8403 else
8404 final |= DREF_NONSPREAD_SOURCE_ENABLE;
8405
8c07eb68 8406 final &= ~DREF_SSC_SOURCE_MASK;
74cfd7ac 8407 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
8c07eb68 8408 final &= ~DREF_SSC1_ENABLE;
74cfd7ac
CW
8409
8410 if (has_panel) {
8411 final |= DREF_SSC_SOURCE_ENABLE;
8412
8413 if (intel_panel_use_ssc(dev_priv) && can_ssc)
8414 final |= DREF_SSC1_ENABLE;
8415
8416 if (has_cpu_edp) {
8417 if (intel_panel_use_ssc(dev_priv) && can_ssc)
8418 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
8419 else
8420 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
8421 } else
8422 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
1c1a24d2
L
8423 } else if (using_ssc_source) {
8424 final |= DREF_SSC_SOURCE_ENABLE;
8425 final |= DREF_SSC1_ENABLE;
74cfd7ac
CW
8426 }
8427
8428 if (final == val)
8429 return;
8430
13d83a67 8431 /* Always enable nonspread source */
74cfd7ac 8432 val &= ~DREF_NONSPREAD_SOURCE_MASK;
13d83a67 8433
99eb6a01 8434 if (has_ck505)
74cfd7ac 8435 val |= DREF_NONSPREAD_CK505_ENABLE;
99eb6a01 8436 else
74cfd7ac 8437 val |= DREF_NONSPREAD_SOURCE_ENABLE;
13d83a67 8438
199e5d79 8439 if (has_panel) {
74cfd7ac
CW
8440 val &= ~DREF_SSC_SOURCE_MASK;
8441 val |= DREF_SSC_SOURCE_ENABLE;
13d83a67 8442
199e5d79 8443 /* SSC must be turned on before enabling the CPU output */
99eb6a01 8444 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
199e5d79 8445 DRM_DEBUG_KMS("Using SSC on panel\n");
74cfd7ac 8446 val |= DREF_SSC1_ENABLE;
e77166b5 8447 } else
74cfd7ac 8448 val &= ~DREF_SSC1_ENABLE;
199e5d79
KP
8449
8450 /* Get SSC going before enabling the outputs */
74cfd7ac 8451 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
8452 POSTING_READ(PCH_DREF_CONTROL);
8453 udelay(200);
8454
74cfd7ac 8455 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
13d83a67
JB
8456
8457 /* Enable CPU source on CPU attached eDP */
199e5d79 8458 if (has_cpu_edp) {
99eb6a01 8459 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
199e5d79 8460 DRM_DEBUG_KMS("Using SSC on eDP\n");
74cfd7ac 8461 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
eba905b2 8462 } else
74cfd7ac 8463 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
199e5d79 8464 } else
74cfd7ac 8465 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
199e5d79 8466
74cfd7ac 8467 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
8468 POSTING_READ(PCH_DREF_CONTROL);
8469 udelay(200);
8470 } else {
1c1a24d2 8471 DRM_DEBUG_KMS("Disabling CPU source output\n");
199e5d79 8472
74cfd7ac 8473 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
199e5d79
KP
8474
8475 /* Turn off CPU output */
74cfd7ac 8476 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
199e5d79 8477
74cfd7ac 8478 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
8479 POSTING_READ(PCH_DREF_CONTROL);
8480 udelay(200);
8481
1c1a24d2
L
8482 if (!using_ssc_source) {
8483 DRM_DEBUG_KMS("Disabling SSC source\n");
199e5d79 8484
1c1a24d2
L
8485 /* Turn off the SSC source */
8486 val &= ~DREF_SSC_SOURCE_MASK;
8487 val |= DREF_SSC_SOURCE_DISABLE;
f165d283 8488
1c1a24d2
L
8489 /* Turn off SSC1 */
8490 val &= ~DREF_SSC1_ENABLE;
8491
8492 I915_WRITE(PCH_DREF_CONTROL, val);
8493 POSTING_READ(PCH_DREF_CONTROL);
8494 udelay(200);
8495 }
13d83a67 8496 }
74cfd7ac
CW
8497
8498 BUG_ON(val != final);
13d83a67
JB
8499}
8500
f31f2d55 8501static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
dde86e2d 8502{
f31f2d55 8503 uint32_t tmp;
dde86e2d 8504
0ff066a9
PZ
8505 tmp = I915_READ(SOUTH_CHICKEN2);
8506 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
8507 I915_WRITE(SOUTH_CHICKEN2, tmp);
dde86e2d 8508
cf3598c2
ID
8509 if (wait_for_us(I915_READ(SOUTH_CHICKEN2) &
8510 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
0ff066a9 8511 DRM_ERROR("FDI mPHY reset assert timeout\n");
dde86e2d 8512
0ff066a9
PZ
8513 tmp = I915_READ(SOUTH_CHICKEN2);
8514 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
8515 I915_WRITE(SOUTH_CHICKEN2, tmp);
dde86e2d 8516
cf3598c2
ID
8517 if (wait_for_us((I915_READ(SOUTH_CHICKEN2) &
8518 FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
0ff066a9 8519 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
f31f2d55
PZ
8520}
8521
8522/* WaMPhyProgramming:hsw */
8523static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
8524{
8525 uint32_t tmp;
dde86e2d
PZ
8526
8527 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
8528 tmp &= ~(0xFF << 24);
8529 tmp |= (0x12 << 24);
8530 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
8531
dde86e2d
PZ
8532 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
8533 tmp |= (1 << 11);
8534 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
8535
8536 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
8537 tmp |= (1 << 11);
8538 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
8539
dde86e2d
PZ
8540 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
8541 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
8542 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
8543
8544 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
8545 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
8546 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
8547
0ff066a9
PZ
8548 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
8549 tmp &= ~(7 << 13);
8550 tmp |= (5 << 13);
8551 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
dde86e2d 8552
0ff066a9
PZ
8553 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
8554 tmp &= ~(7 << 13);
8555 tmp |= (5 << 13);
8556 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
dde86e2d
PZ
8557
8558 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
8559 tmp &= ~0xFF;
8560 tmp |= 0x1C;
8561 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
8562
8563 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
8564 tmp &= ~0xFF;
8565 tmp |= 0x1C;
8566 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
8567
8568 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
8569 tmp &= ~(0xFF << 16);
8570 tmp |= (0x1C << 16);
8571 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
8572
8573 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
8574 tmp &= ~(0xFF << 16);
8575 tmp |= (0x1C << 16);
8576 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
8577
0ff066a9
PZ
8578 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
8579 tmp |= (1 << 27);
8580 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
dde86e2d 8581
0ff066a9
PZ
8582 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
8583 tmp |= (1 << 27);
8584 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
dde86e2d 8585
0ff066a9
PZ
8586 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
8587 tmp &= ~(0xF << 28);
8588 tmp |= (4 << 28);
8589 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
dde86e2d 8590
0ff066a9
PZ
8591 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
8592 tmp &= ~(0xF << 28);
8593 tmp |= (4 << 28);
8594 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
f31f2d55
PZ
8595}
8596
2fa86a1f
PZ
8597/* Implements 3 different sequences from BSpec chapter "Display iCLK
8598 * Programming" based on the parameters passed:
8599 * - Sequence to enable CLKOUT_DP
8600 * - Sequence to enable CLKOUT_DP without spread
8601 * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
8602 */
8603static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
8604 bool with_fdi)
f31f2d55 8605{
fac5e23e 8606 struct drm_i915_private *dev_priv = to_i915(dev);
2fa86a1f
PZ
8607 uint32_t reg, tmp;
8608
8609 if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
8610 with_spread = true;
c2699524 8611 if (WARN(HAS_PCH_LPT_LP(dev) && with_fdi, "LP PCH doesn't have FDI\n"))
2fa86a1f 8612 with_fdi = false;
f31f2d55 8613
a580516d 8614 mutex_lock(&dev_priv->sb_lock);
f31f2d55
PZ
8615
8616 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8617 tmp &= ~SBI_SSCCTL_DISABLE;
8618 tmp |= SBI_SSCCTL_PATHALT;
8619 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8620
8621 udelay(24);
8622
2fa86a1f
PZ
8623 if (with_spread) {
8624 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8625 tmp &= ~SBI_SSCCTL_PATHALT;
8626 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
f31f2d55 8627
2fa86a1f
PZ
8628 if (with_fdi) {
8629 lpt_reset_fdi_mphy(dev_priv);
8630 lpt_program_fdi_mphy(dev_priv);
8631 }
8632 }
dde86e2d 8633
c2699524 8634 reg = HAS_PCH_LPT_LP(dev) ? SBI_GEN0 : SBI_DBUFF0;
2fa86a1f
PZ
8635 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8636 tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8637 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
c00db246 8638
a580516d 8639 mutex_unlock(&dev_priv->sb_lock);
dde86e2d
PZ
8640}
8641
47701c3b
PZ
8642/* Sequence to disable CLKOUT_DP */
8643static void lpt_disable_clkout_dp(struct drm_device *dev)
8644{
fac5e23e 8645 struct drm_i915_private *dev_priv = to_i915(dev);
47701c3b
PZ
8646 uint32_t reg, tmp;
8647
a580516d 8648 mutex_lock(&dev_priv->sb_lock);
47701c3b 8649
c2699524 8650 reg = HAS_PCH_LPT_LP(dev) ? SBI_GEN0 : SBI_DBUFF0;
47701c3b
PZ
8651 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8652 tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8653 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
8654
8655 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8656 if (!(tmp & SBI_SSCCTL_DISABLE)) {
8657 if (!(tmp & SBI_SSCCTL_PATHALT)) {
8658 tmp |= SBI_SSCCTL_PATHALT;
8659 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8660 udelay(32);
8661 }
8662 tmp |= SBI_SSCCTL_DISABLE;
8663 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8664 }
8665
a580516d 8666 mutex_unlock(&dev_priv->sb_lock);
47701c3b
PZ
8667}
8668
f7be2c21
VS
8669#define BEND_IDX(steps) ((50 + (steps)) / 5)
8670
8671static const uint16_t sscdivintphase[] = {
8672 [BEND_IDX( 50)] = 0x3B23,
8673 [BEND_IDX( 45)] = 0x3B23,
8674 [BEND_IDX( 40)] = 0x3C23,
8675 [BEND_IDX( 35)] = 0x3C23,
8676 [BEND_IDX( 30)] = 0x3D23,
8677 [BEND_IDX( 25)] = 0x3D23,
8678 [BEND_IDX( 20)] = 0x3E23,
8679 [BEND_IDX( 15)] = 0x3E23,
8680 [BEND_IDX( 10)] = 0x3F23,
8681 [BEND_IDX( 5)] = 0x3F23,
8682 [BEND_IDX( 0)] = 0x0025,
8683 [BEND_IDX( -5)] = 0x0025,
8684 [BEND_IDX(-10)] = 0x0125,
8685 [BEND_IDX(-15)] = 0x0125,
8686 [BEND_IDX(-20)] = 0x0225,
8687 [BEND_IDX(-25)] = 0x0225,
8688 [BEND_IDX(-30)] = 0x0325,
8689 [BEND_IDX(-35)] = 0x0325,
8690 [BEND_IDX(-40)] = 0x0425,
8691 [BEND_IDX(-45)] = 0x0425,
8692 [BEND_IDX(-50)] = 0x0525,
8693};
8694
8695/*
8696 * Bend CLKOUT_DP
8697 * steps -50 to 50 inclusive, in steps of 5
8698 * < 0 slow down the clock, > 0 speed up the clock, 0 == no bend (135MHz)
8699 * change in clock period = -(steps / 10) * 5.787 ps
8700 */
8701static void lpt_bend_clkout_dp(struct drm_i915_private *dev_priv, int steps)
8702{
8703 uint32_t tmp;
8704 int idx = BEND_IDX(steps);
8705
8706 if (WARN_ON(steps % 5 != 0))
8707 return;
8708
8709 if (WARN_ON(idx >= ARRAY_SIZE(sscdivintphase)))
8710 return;
8711
8712 mutex_lock(&dev_priv->sb_lock);
8713
8714 if (steps % 10 != 0)
8715 tmp = 0xAAAAAAAB;
8716 else
8717 tmp = 0x00000000;
8718 intel_sbi_write(dev_priv, SBI_SSCDITHPHASE, tmp, SBI_ICLK);
8719
8720 tmp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE, SBI_ICLK);
8721 tmp &= 0xffff0000;
8722 tmp |= sscdivintphase[idx];
8723 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE, tmp, SBI_ICLK);
8724
8725 mutex_unlock(&dev_priv->sb_lock);
8726}
8727
8728#undef BEND_IDX
8729
bf8fa3d3
PZ
8730static void lpt_init_pch_refclk(struct drm_device *dev)
8731{
bf8fa3d3
PZ
8732 struct intel_encoder *encoder;
8733 bool has_vga = false;
8734
b2784e15 8735 for_each_intel_encoder(dev, encoder) {
bf8fa3d3
PZ
8736 switch (encoder->type) {
8737 case INTEL_OUTPUT_ANALOG:
8738 has_vga = true;
8739 break;
6847d71b
PZ
8740 default:
8741 break;
bf8fa3d3
PZ
8742 }
8743 }
8744
f7be2c21
VS
8745 if (has_vga) {
8746 lpt_bend_clkout_dp(to_i915(dev), 0);
47701c3b 8747 lpt_enable_clkout_dp(dev, true, true);
f7be2c21 8748 } else {
47701c3b 8749 lpt_disable_clkout_dp(dev);
f7be2c21 8750 }
bf8fa3d3
PZ
8751}
8752
dde86e2d
PZ
8753/*
8754 * Initialize reference clocks when the driver loads
8755 */
8756void intel_init_pch_refclk(struct drm_device *dev)
8757{
8758 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
8759 ironlake_init_pch_refclk(dev);
8760 else if (HAS_PCH_LPT(dev))
8761 lpt_init_pch_refclk(dev);
8762}
8763
6ff93609 8764static void ironlake_set_pipeconf(struct drm_crtc *crtc)
79e53945 8765{
fac5e23e 8766 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
79e53945
JB
8767 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8768 int pipe = intel_crtc->pipe;
c8203565
PZ
8769 uint32_t val;
8770
78114071 8771 val = 0;
c8203565 8772
6e3c9717 8773 switch (intel_crtc->config->pipe_bpp) {
c8203565 8774 case 18:
dfd07d72 8775 val |= PIPECONF_6BPC;
c8203565
PZ
8776 break;
8777 case 24:
dfd07d72 8778 val |= PIPECONF_8BPC;
c8203565
PZ
8779 break;
8780 case 30:
dfd07d72 8781 val |= PIPECONF_10BPC;
c8203565
PZ
8782 break;
8783 case 36:
dfd07d72 8784 val |= PIPECONF_12BPC;
c8203565
PZ
8785 break;
8786 default:
cc769b62
PZ
8787 /* Case prevented by intel_choose_pipe_bpp_dither. */
8788 BUG();
c8203565
PZ
8789 }
8790
6e3c9717 8791 if (intel_crtc->config->dither)
c8203565
PZ
8792 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8793
6e3c9717 8794 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
c8203565
PZ
8795 val |= PIPECONF_INTERLACED_ILK;
8796 else
8797 val |= PIPECONF_PROGRESSIVE;
8798
6e3c9717 8799 if (intel_crtc->config->limited_color_range)
3685a8f3 8800 val |= PIPECONF_COLOR_RANGE_SELECT;
3685a8f3 8801
c8203565
PZ
8802 I915_WRITE(PIPECONF(pipe), val);
8803 POSTING_READ(PIPECONF(pipe));
8804}
8805
6ff93609 8806static void haswell_set_pipeconf(struct drm_crtc *crtc)
ee2b0b38 8807{
fac5e23e 8808 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
ee2b0b38 8809 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6e3c9717 8810 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
391bf048 8811 u32 val = 0;
ee2b0b38 8812
391bf048 8813 if (IS_HASWELL(dev_priv) && intel_crtc->config->dither)
ee2b0b38
PZ
8814 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8815
6e3c9717 8816 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
ee2b0b38
PZ
8817 val |= PIPECONF_INTERLACED_ILK;
8818 else
8819 val |= PIPECONF_PROGRESSIVE;
8820
702e7a56
PZ
8821 I915_WRITE(PIPECONF(cpu_transcoder), val);
8822 POSTING_READ(PIPECONF(cpu_transcoder));
391bf048
JN
8823}
8824
391bf048
JN
8825static void haswell_set_pipemisc(struct drm_crtc *crtc)
8826{
fac5e23e 8827 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
391bf048 8828 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
756f85cf 8829
391bf048
JN
8830 if (IS_BROADWELL(dev_priv) || INTEL_INFO(dev_priv)->gen >= 9) {
8831 u32 val = 0;
756f85cf 8832
6e3c9717 8833 switch (intel_crtc->config->pipe_bpp) {
756f85cf
PZ
8834 case 18:
8835 val |= PIPEMISC_DITHER_6_BPC;
8836 break;
8837 case 24:
8838 val |= PIPEMISC_DITHER_8_BPC;
8839 break;
8840 case 30:
8841 val |= PIPEMISC_DITHER_10_BPC;
8842 break;
8843 case 36:
8844 val |= PIPEMISC_DITHER_12_BPC;
8845 break;
8846 default:
8847 /* Case prevented by pipe_config_set_bpp. */
8848 BUG();
8849 }
8850
6e3c9717 8851 if (intel_crtc->config->dither)
756f85cf
PZ
8852 val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
8853
391bf048 8854 I915_WRITE(PIPEMISC(intel_crtc->pipe), val);
756f85cf 8855 }
ee2b0b38
PZ
8856}
8857
d4b1931c
PZ
8858int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
8859{
8860 /*
8861 * Account for spread spectrum to avoid
8862 * oversubscribing the link. Max center spread
8863 * is 2.5%; use 5% for safety's sake.
8864 */
8865 u32 bps = target_clock * bpp * 21 / 20;
619d4d04 8866 return DIV_ROUND_UP(bps, link_bw * 8);
d4b1931c
PZ
8867}
8868
7429e9d4 8869static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
6cf86a5e 8870{
7429e9d4 8871 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
f48d8f23
PZ
8872}
8873
b75ca6f6
ACO
8874static void ironlake_compute_dpll(struct intel_crtc *intel_crtc,
8875 struct intel_crtc_state *crtc_state,
9e2c8475 8876 struct dpll *reduced_clock)
79e53945 8877{
de13a2e3 8878 struct drm_crtc *crtc = &intel_crtc->base;
79e53945 8879 struct drm_device *dev = crtc->dev;
fac5e23e 8880 struct drm_i915_private *dev_priv = to_i915(dev);
b75ca6f6 8881 u32 dpll, fp, fp2;
3d6e9ee0 8882 int factor;
79e53945 8883
c1858123 8884 /* Enable autotuning of the PLL clock (if permissible) */
8febb297 8885 factor = 21;
3d6e9ee0 8886 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
8febb297 8887 if ((intel_panel_use_ssc(dev_priv) &&
e91e941b 8888 dev_priv->vbt.lvds_ssc_freq == 100000) ||
f0b44056 8889 (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
8febb297 8890 factor = 25;
190f68c5 8891 } else if (crtc_state->sdvo_tv_clock)
8febb297 8892 factor = 20;
c1858123 8893
b75ca6f6
ACO
8894 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
8895
190f68c5 8896 if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
b75ca6f6
ACO
8897 fp |= FP_CB_TUNE;
8898
8899 if (reduced_clock) {
8900 fp2 = i9xx_dpll_compute_fp(reduced_clock);
2c07245f 8901
b75ca6f6
ACO
8902 if (reduced_clock->m < factor * reduced_clock->n)
8903 fp2 |= FP_CB_TUNE;
8904 } else {
8905 fp2 = fp;
8906 }
9a7c7890 8907
5eddb70b 8908 dpll = 0;
2c07245f 8909
3d6e9ee0 8910 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS))
a07d6787
EA
8911 dpll |= DPLLB_MODE_LVDS;
8912 else
8913 dpll |= DPLLB_MODE_DAC_SERIAL;
198a037f 8914
190f68c5 8915 dpll |= (crtc_state->pixel_multiplier - 1)
ef1b460d 8916 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
198a037f 8917
3d6e9ee0
VS
8918 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO) ||
8919 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
4a33e48d 8920 dpll |= DPLL_SDVO_HIGH_SPEED;
3d6e9ee0 8921
37a5650b 8922 if (intel_crtc_has_dp_encoder(crtc_state))
4a33e48d 8923 dpll |= DPLL_SDVO_HIGH_SPEED;
79e53945 8924
a07d6787 8925 /* compute bitmask from p1 value */
190f68c5 8926 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
a07d6787 8927 /* also FPA1 */
190f68c5 8928 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
a07d6787 8929
190f68c5 8930 switch (crtc_state->dpll.p2) {
a07d6787
EA
8931 case 5:
8932 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
8933 break;
8934 case 7:
8935 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
8936 break;
8937 case 10:
8938 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
8939 break;
8940 case 14:
8941 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
8942 break;
79e53945
JB
8943 }
8944
3d6e9ee0
VS
8945 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
8946 intel_panel_use_ssc(dev_priv))
43565a06 8947 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
79e53945
JB
8948 else
8949 dpll |= PLL_REF_INPUT_DREFCLK;
8950
b75ca6f6
ACO
8951 dpll |= DPLL_VCO_ENABLE;
8952
8953 crtc_state->dpll_hw_state.dpll = dpll;
8954 crtc_state->dpll_hw_state.fp0 = fp;
8955 crtc_state->dpll_hw_state.fp1 = fp2;
de13a2e3
PZ
8956}
8957
190f68c5
ACO
8958static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
8959 struct intel_crtc_state *crtc_state)
de13a2e3 8960{
997c030c 8961 struct drm_device *dev = crtc->base.dev;
fac5e23e 8962 struct drm_i915_private *dev_priv = to_i915(dev);
9e2c8475 8963 struct dpll reduced_clock;
7ed9f894 8964 bool has_reduced_clock = false;
e2b78267 8965 struct intel_shared_dpll *pll;
1b6f4958 8966 const struct intel_limit *limit;
997c030c 8967 int refclk = 120000;
de13a2e3 8968
dd3cd74a
ACO
8969 memset(&crtc_state->dpll_hw_state, 0,
8970 sizeof(crtc_state->dpll_hw_state));
8971
ded220e2
ACO
8972 crtc->lowfreq_avail = false;
8973
8974 /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
8975 if (!crtc_state->has_pch_encoder)
8976 return 0;
79e53945 8977
2d84d2b3 8978 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
997c030c
ACO
8979 if (intel_panel_use_ssc(dev_priv)) {
8980 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
8981 dev_priv->vbt.lvds_ssc_freq);
8982 refclk = dev_priv->vbt.lvds_ssc_freq;
8983 }
8984
8985 if (intel_is_dual_link_lvds(dev)) {
8986 if (refclk == 100000)
8987 limit = &intel_limits_ironlake_dual_lvds_100m;
8988 else
8989 limit = &intel_limits_ironlake_dual_lvds;
8990 } else {
8991 if (refclk == 100000)
8992 limit = &intel_limits_ironlake_single_lvds_100m;
8993 else
8994 limit = &intel_limits_ironlake_single_lvds;
8995 }
8996 } else {
8997 limit = &intel_limits_ironlake_dac;
8998 }
8999
364ee29d 9000 if (!crtc_state->clock_set &&
997c030c
ACO
9001 !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
9002 refclk, NULL, &crtc_state->dpll)) {
364ee29d
ACO
9003 DRM_ERROR("Couldn't find PLL settings for mode!\n");
9004 return -EINVAL;
f47709a9 9005 }
79e53945 9006
b75ca6f6
ACO
9007 ironlake_compute_dpll(crtc, crtc_state,
9008 has_reduced_clock ? &reduced_clock : NULL);
66e985c0 9009
ded220e2
ACO
9010 pll = intel_get_shared_dpll(crtc, crtc_state, NULL);
9011 if (pll == NULL) {
9012 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
9013 pipe_name(crtc->pipe));
9014 return -EINVAL;
3fb37703 9015 }
79e53945 9016
2d84d2b3 9017 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
ded220e2 9018 has_reduced_clock)
c7653199 9019 crtc->lowfreq_avail = true;
e2b78267 9020
c8f7a0db 9021 return 0;
79e53945
JB
9022}
9023
eb14cb74
VS
9024static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
9025 struct intel_link_m_n *m_n)
9026{
9027 struct drm_device *dev = crtc->base.dev;
fac5e23e 9028 struct drm_i915_private *dev_priv = to_i915(dev);
eb14cb74
VS
9029 enum pipe pipe = crtc->pipe;
9030
9031 m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
9032 m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
9033 m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
9034 & ~TU_SIZE_MASK;
9035 m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
9036 m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
9037 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
9038}
9039
9040static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
9041 enum transcoder transcoder,
b95af8be
VK
9042 struct intel_link_m_n *m_n,
9043 struct intel_link_m_n *m2_n2)
72419203
DV
9044{
9045 struct drm_device *dev = crtc->base.dev;
fac5e23e 9046 struct drm_i915_private *dev_priv = to_i915(dev);
eb14cb74 9047 enum pipe pipe = crtc->pipe;
72419203 9048
eb14cb74
VS
9049 if (INTEL_INFO(dev)->gen >= 5) {
9050 m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
9051 m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
9052 m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
9053 & ~TU_SIZE_MASK;
9054 m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
9055 m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
9056 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
b95af8be
VK
9057 /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
9058 * gen < 8) and if DRRS is supported (to make sure the
9059 * registers are not unnecessarily read).
9060 */
9061 if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
6e3c9717 9062 crtc->config->has_drrs) {
b95af8be
VK
9063 m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
9064 m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
9065 m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
9066 & ~TU_SIZE_MASK;
9067 m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
9068 m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
9069 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
9070 }
eb14cb74
VS
9071 } else {
9072 m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
9073 m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
9074 m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
9075 & ~TU_SIZE_MASK;
9076 m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
9077 m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
9078 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
9079 }
9080}
9081
9082void intel_dp_get_m_n(struct intel_crtc *crtc,
5cec258b 9083 struct intel_crtc_state *pipe_config)
eb14cb74 9084{
681a8504 9085 if (pipe_config->has_pch_encoder)
eb14cb74
VS
9086 intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
9087 else
9088 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
b95af8be
VK
9089 &pipe_config->dp_m_n,
9090 &pipe_config->dp_m2_n2);
eb14cb74 9091}
72419203 9092
eb14cb74 9093static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
5cec258b 9094 struct intel_crtc_state *pipe_config)
eb14cb74
VS
9095{
9096 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
b95af8be 9097 &pipe_config->fdi_m_n, NULL);
72419203
DV
9098}
9099
bd2e244f 9100static void skylake_get_pfit_config(struct intel_crtc *crtc,
5cec258b 9101 struct intel_crtc_state *pipe_config)
bd2e244f
JB
9102{
9103 struct drm_device *dev = crtc->base.dev;
fac5e23e 9104 struct drm_i915_private *dev_priv = to_i915(dev);
a1b2278e
CK
9105 struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
9106 uint32_t ps_ctrl = 0;
9107 int id = -1;
9108 int i;
bd2e244f 9109
a1b2278e
CK
9110 /* find scaler attached to this pipe */
9111 for (i = 0; i < crtc->num_scalers; i++) {
9112 ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
9113 if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
9114 id = i;
9115 pipe_config->pch_pfit.enabled = true;
9116 pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
9117 pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
9118 break;
9119 }
9120 }
bd2e244f 9121
a1b2278e
CK
9122 scaler_state->scaler_id = id;
9123 if (id >= 0) {
9124 scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
9125 } else {
9126 scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
bd2e244f
JB
9127 }
9128}
9129
5724dbd1
DL
9130static void
9131skylake_get_initial_plane_config(struct intel_crtc *crtc,
9132 struct intel_initial_plane_config *plane_config)
bc8d7dff
DL
9133{
9134 struct drm_device *dev = crtc->base.dev;
fac5e23e 9135 struct drm_i915_private *dev_priv = to_i915(dev);
40f46283 9136 u32 val, base, offset, stride_mult, tiling;
bc8d7dff
DL
9137 int pipe = crtc->pipe;
9138 int fourcc, pixel_format;
6761dd31 9139 unsigned int aligned_height;
bc8d7dff 9140 struct drm_framebuffer *fb;
1b842c89 9141 struct intel_framebuffer *intel_fb;
bc8d7dff 9142
d9806c9f 9143 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
1b842c89 9144 if (!intel_fb) {
bc8d7dff
DL
9145 DRM_DEBUG_KMS("failed to alloc fb\n");
9146 return;
9147 }
9148
1b842c89
DL
9149 fb = &intel_fb->base;
9150
bc8d7dff 9151 val = I915_READ(PLANE_CTL(pipe, 0));
42a7b088
DL
9152 if (!(val & PLANE_CTL_ENABLE))
9153 goto error;
9154
bc8d7dff
DL
9155 pixel_format = val & PLANE_CTL_FORMAT_MASK;
9156 fourcc = skl_format_to_fourcc(pixel_format,
9157 val & PLANE_CTL_ORDER_RGBX,
9158 val & PLANE_CTL_ALPHA_MASK);
9159 fb->pixel_format = fourcc;
9160 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
9161
40f46283
DL
9162 tiling = val & PLANE_CTL_TILED_MASK;
9163 switch (tiling) {
9164 case PLANE_CTL_TILED_LINEAR:
9165 fb->modifier[0] = DRM_FORMAT_MOD_NONE;
9166 break;
9167 case PLANE_CTL_TILED_X:
9168 plane_config->tiling = I915_TILING_X;
9169 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
9170 break;
9171 case PLANE_CTL_TILED_Y:
9172 fb->modifier[0] = I915_FORMAT_MOD_Y_TILED;
9173 break;
9174 case PLANE_CTL_TILED_YF:
9175 fb->modifier[0] = I915_FORMAT_MOD_Yf_TILED;
9176 break;
9177 default:
9178 MISSING_CASE(tiling);
9179 goto error;
9180 }
9181
bc8d7dff
DL
9182 base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
9183 plane_config->base = base;
9184
9185 offset = I915_READ(PLANE_OFFSET(pipe, 0));
9186
9187 val = I915_READ(PLANE_SIZE(pipe, 0));
9188 fb->height = ((val >> 16) & 0xfff) + 1;
9189 fb->width = ((val >> 0) & 0x1fff) + 1;
9190
9191 val = I915_READ(PLANE_STRIDE(pipe, 0));
7b49f948 9192 stride_mult = intel_fb_stride_alignment(dev_priv, fb->modifier[0],
40f46283 9193 fb->pixel_format);
bc8d7dff
DL
9194 fb->pitches[0] = (val & 0x3ff) * stride_mult;
9195
9196 aligned_height = intel_fb_align_height(dev, fb->height,
091df6cb
DV
9197 fb->pixel_format,
9198 fb->modifier[0]);
bc8d7dff 9199
f37b5c2b 9200 plane_config->size = fb->pitches[0] * aligned_height;
bc8d7dff
DL
9201
9202 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9203 pipe_name(pipe), fb->width, fb->height,
9204 fb->bits_per_pixel, base, fb->pitches[0],
9205 plane_config->size);
9206
2d14030b 9207 plane_config->fb = intel_fb;
bc8d7dff
DL
9208 return;
9209
9210error:
9211 kfree(fb);
9212}
9213
2fa2fe9a 9214static void ironlake_get_pfit_config(struct intel_crtc *crtc,
5cec258b 9215 struct intel_crtc_state *pipe_config)
2fa2fe9a
DV
9216{
9217 struct drm_device *dev = crtc->base.dev;
fac5e23e 9218 struct drm_i915_private *dev_priv = to_i915(dev);
2fa2fe9a
DV
9219 uint32_t tmp;
9220
9221 tmp = I915_READ(PF_CTL(crtc->pipe));
9222
9223 if (tmp & PF_ENABLE) {
fd4daa9c 9224 pipe_config->pch_pfit.enabled = true;
2fa2fe9a
DV
9225 pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
9226 pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
cb8b2a30
DV
9227
9228 /* We currently do not free assignements of panel fitters on
9229 * ivb/hsw (since we don't use the higher upscaling modes which
9230 * differentiates them) so just WARN about this case for now. */
9231 if (IS_GEN7(dev)) {
9232 WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
9233 PF_PIPE_SEL_IVB(crtc->pipe));
9234 }
2fa2fe9a 9235 }
79e53945
JB
9236}
9237
5724dbd1
DL
9238static void
9239ironlake_get_initial_plane_config(struct intel_crtc *crtc,
9240 struct intel_initial_plane_config *plane_config)
4c6baa59
JB
9241{
9242 struct drm_device *dev = crtc->base.dev;
fac5e23e 9243 struct drm_i915_private *dev_priv = to_i915(dev);
4c6baa59 9244 u32 val, base, offset;
aeee5a49 9245 int pipe = crtc->pipe;
4c6baa59 9246 int fourcc, pixel_format;
6761dd31 9247 unsigned int aligned_height;
b113d5ee 9248 struct drm_framebuffer *fb;
1b842c89 9249 struct intel_framebuffer *intel_fb;
4c6baa59 9250
42a7b088
DL
9251 val = I915_READ(DSPCNTR(pipe));
9252 if (!(val & DISPLAY_PLANE_ENABLE))
9253 return;
9254
d9806c9f 9255 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
1b842c89 9256 if (!intel_fb) {
4c6baa59
JB
9257 DRM_DEBUG_KMS("failed to alloc fb\n");
9258 return;
9259 }
9260
1b842c89
DL
9261 fb = &intel_fb->base;
9262
18c5247e
DV
9263 if (INTEL_INFO(dev)->gen >= 4) {
9264 if (val & DISPPLANE_TILED) {
49af449b 9265 plane_config->tiling = I915_TILING_X;
18c5247e
DV
9266 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
9267 }
9268 }
4c6baa59
JB
9269
9270 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
b35d63fa 9271 fourcc = i9xx_format_to_fourcc(pixel_format);
b113d5ee
DL
9272 fb->pixel_format = fourcc;
9273 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
4c6baa59 9274
aeee5a49 9275 base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
4c6baa59 9276 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
aeee5a49 9277 offset = I915_READ(DSPOFFSET(pipe));
4c6baa59 9278 } else {
49af449b 9279 if (plane_config->tiling)
aeee5a49 9280 offset = I915_READ(DSPTILEOFF(pipe));
4c6baa59 9281 else
aeee5a49 9282 offset = I915_READ(DSPLINOFF(pipe));
4c6baa59
JB
9283 }
9284 plane_config->base = base;
9285
9286 val = I915_READ(PIPESRC(pipe));
b113d5ee
DL
9287 fb->width = ((val >> 16) & 0xfff) + 1;
9288 fb->height = ((val >> 0) & 0xfff) + 1;
4c6baa59
JB
9289
9290 val = I915_READ(DSPSTRIDE(pipe));
b113d5ee 9291 fb->pitches[0] = val & 0xffffffc0;
4c6baa59 9292
b113d5ee 9293 aligned_height = intel_fb_align_height(dev, fb->height,
091df6cb
DV
9294 fb->pixel_format,
9295 fb->modifier[0]);
4c6baa59 9296
f37b5c2b 9297 plane_config->size = fb->pitches[0] * aligned_height;
4c6baa59 9298
2844a921
DL
9299 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9300 pipe_name(pipe), fb->width, fb->height,
9301 fb->bits_per_pixel, base, fb->pitches[0],
9302 plane_config->size);
b113d5ee 9303
2d14030b 9304 plane_config->fb = intel_fb;
4c6baa59
JB
9305}
9306
0e8ffe1b 9307static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
5cec258b 9308 struct intel_crtc_state *pipe_config)
0e8ffe1b
DV
9309{
9310 struct drm_device *dev = crtc->base.dev;
fac5e23e 9311 struct drm_i915_private *dev_priv = to_i915(dev);
1729050e 9312 enum intel_display_power_domain power_domain;
0e8ffe1b 9313 uint32_t tmp;
1729050e 9314 bool ret;
0e8ffe1b 9315
1729050e
ID
9316 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
9317 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
930e8c9e
PZ
9318 return false;
9319
e143a21c 9320 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
8106ddbd 9321 pipe_config->shared_dpll = NULL;
eccb140b 9322
1729050e 9323 ret = false;
0e8ffe1b
DV
9324 tmp = I915_READ(PIPECONF(crtc->pipe));
9325 if (!(tmp & PIPECONF_ENABLE))
1729050e 9326 goto out;
0e8ffe1b 9327
42571aef
VS
9328 switch (tmp & PIPECONF_BPC_MASK) {
9329 case PIPECONF_6BPC:
9330 pipe_config->pipe_bpp = 18;
9331 break;
9332 case PIPECONF_8BPC:
9333 pipe_config->pipe_bpp = 24;
9334 break;
9335 case PIPECONF_10BPC:
9336 pipe_config->pipe_bpp = 30;
9337 break;
9338 case PIPECONF_12BPC:
9339 pipe_config->pipe_bpp = 36;
9340 break;
9341 default:
9342 break;
9343 }
9344
b5a9fa09
DV
9345 if (tmp & PIPECONF_COLOR_RANGE_SELECT)
9346 pipe_config->limited_color_range = true;
9347
ab9412ba 9348 if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
66e985c0 9349 struct intel_shared_dpll *pll;
8106ddbd 9350 enum intel_dpll_id pll_id;
66e985c0 9351
88adfff1
DV
9352 pipe_config->has_pch_encoder = true;
9353
627eb5a3
DV
9354 tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
9355 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
9356 FDI_DP_PORT_WIDTH_SHIFT) + 1;
72419203
DV
9357
9358 ironlake_get_fdi_m_n_config(crtc, pipe_config);
6c49f241 9359
2d1fe073 9360 if (HAS_PCH_IBX(dev_priv)) {
d9a7bc67
ID
9361 /*
9362 * The pipe->pch transcoder and pch transcoder->pll
9363 * mapping is fixed.
9364 */
8106ddbd 9365 pll_id = (enum intel_dpll_id) crtc->pipe;
c0d43d62
DV
9366 } else {
9367 tmp = I915_READ(PCH_DPLL_SEL);
9368 if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
8106ddbd 9369 pll_id = DPLL_ID_PCH_PLL_B;
c0d43d62 9370 else
8106ddbd 9371 pll_id= DPLL_ID_PCH_PLL_A;
c0d43d62 9372 }
66e985c0 9373
8106ddbd
ACO
9374 pipe_config->shared_dpll =
9375 intel_get_shared_dpll_by_id(dev_priv, pll_id);
9376 pll = pipe_config->shared_dpll;
66e985c0 9377
2edd6443
ACO
9378 WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
9379 &pipe_config->dpll_hw_state));
c93f54cf
DV
9380
9381 tmp = pipe_config->dpll_hw_state.dpll;
9382 pipe_config->pixel_multiplier =
9383 ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
9384 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
18442d08
VS
9385
9386 ironlake_pch_clock_get(crtc, pipe_config);
6c49f241
DV
9387 } else {
9388 pipe_config->pixel_multiplier = 1;
627eb5a3
DV
9389 }
9390
1bd1bd80 9391 intel_get_pipe_timings(crtc, pipe_config);
bc58be60 9392 intel_get_pipe_src_size(crtc, pipe_config);
1bd1bd80 9393
2fa2fe9a
DV
9394 ironlake_get_pfit_config(crtc, pipe_config);
9395
1729050e
ID
9396 ret = true;
9397
9398out:
9399 intel_display_power_put(dev_priv, power_domain);
9400
9401 return ret;
0e8ffe1b
DV
9402}
9403
be256dc7
PZ
9404static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
9405{
91c8a326 9406 struct drm_device *dev = &dev_priv->drm;
be256dc7 9407 struct intel_crtc *crtc;
be256dc7 9408
d3fcc808 9409 for_each_intel_crtc(dev, crtc)
e2c719b7 9410 I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
be256dc7
PZ
9411 pipe_name(crtc->pipe));
9412
e2c719b7
RC
9413 I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
9414 I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
01403de3
VS
9415 I915_STATE_WARN(I915_READ(WRPLL_CTL(0)) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
9416 I915_STATE_WARN(I915_READ(WRPLL_CTL(1)) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
e2c719b7
RC
9417 I915_STATE_WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
9418 I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
be256dc7 9419 "CPU PWM1 enabled\n");
c5107b87 9420 if (IS_HASWELL(dev))
e2c719b7 9421 I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
c5107b87 9422 "CPU PWM2 enabled\n");
e2c719b7 9423 I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
be256dc7 9424 "PCH PWM1 enabled\n");
e2c719b7 9425 I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
be256dc7 9426 "Utility pin enabled\n");
e2c719b7 9427 I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
be256dc7 9428
9926ada1
PZ
9429 /*
9430 * In theory we can still leave IRQs enabled, as long as only the HPD
9431 * interrupts remain enabled. We used to check for that, but since it's
9432 * gen-specific and since we only disable LCPLL after we fully disable
9433 * the interrupts, the check below should be enough.
9434 */
e2c719b7 9435 I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
be256dc7
PZ
9436}
9437
9ccd5aeb
PZ
9438static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
9439{
91c8a326 9440 struct drm_device *dev = &dev_priv->drm;
9ccd5aeb
PZ
9441
9442 if (IS_HASWELL(dev))
9443 return I915_READ(D_COMP_HSW);
9444 else
9445 return I915_READ(D_COMP_BDW);
9446}
9447
3c4c9b81
PZ
9448static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
9449{
91c8a326 9450 struct drm_device *dev = &dev_priv->drm;
3c4c9b81
PZ
9451
9452 if (IS_HASWELL(dev)) {
9453 mutex_lock(&dev_priv->rps.hw_lock);
9454 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
9455 val))
f475dadf 9456 DRM_ERROR("Failed to write to D_COMP\n");
3c4c9b81
PZ
9457 mutex_unlock(&dev_priv->rps.hw_lock);
9458 } else {
9ccd5aeb
PZ
9459 I915_WRITE(D_COMP_BDW, val);
9460 POSTING_READ(D_COMP_BDW);
3c4c9b81 9461 }
be256dc7
PZ
9462}
9463
9464/*
9465 * This function implements pieces of two sequences from BSpec:
9466 * - Sequence for display software to disable LCPLL
9467 * - Sequence for display software to allow package C8+
9468 * The steps implemented here are just the steps that actually touch the LCPLL
9469 * register. Callers should take care of disabling all the display engine
9470 * functions, doing the mode unset, fixing interrupts, etc.
9471 */
6ff58d53
PZ
9472static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
9473 bool switch_to_fclk, bool allow_power_down)
be256dc7
PZ
9474{
9475 uint32_t val;
9476
9477 assert_can_disable_lcpll(dev_priv);
9478
9479 val = I915_READ(LCPLL_CTL);
9480
9481 if (switch_to_fclk) {
9482 val |= LCPLL_CD_SOURCE_FCLK;
9483 I915_WRITE(LCPLL_CTL, val);
9484
f53dd63f
ID
9485 if (wait_for_us(I915_READ(LCPLL_CTL) &
9486 LCPLL_CD_SOURCE_FCLK_DONE, 1))
be256dc7
PZ
9487 DRM_ERROR("Switching to FCLK failed\n");
9488
9489 val = I915_READ(LCPLL_CTL);
9490 }
9491
9492 val |= LCPLL_PLL_DISABLE;
9493 I915_WRITE(LCPLL_CTL, val);
9494 POSTING_READ(LCPLL_CTL);
9495
24d8441d 9496 if (intel_wait_for_register(dev_priv, LCPLL_CTL, LCPLL_PLL_LOCK, 0, 1))
be256dc7
PZ
9497 DRM_ERROR("LCPLL still locked\n");
9498
9ccd5aeb 9499 val = hsw_read_dcomp(dev_priv);
be256dc7 9500 val |= D_COMP_COMP_DISABLE;
3c4c9b81 9501 hsw_write_dcomp(dev_priv, val);
be256dc7
PZ
9502 ndelay(100);
9503
9ccd5aeb
PZ
9504 if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
9505 1))
be256dc7
PZ
9506 DRM_ERROR("D_COMP RCOMP still in progress\n");
9507
9508 if (allow_power_down) {
9509 val = I915_READ(LCPLL_CTL);
9510 val |= LCPLL_POWER_DOWN_ALLOW;
9511 I915_WRITE(LCPLL_CTL, val);
9512 POSTING_READ(LCPLL_CTL);
9513 }
9514}
9515
9516/*
9517 * Fully restores LCPLL, disallowing power down and switching back to LCPLL
9518 * source.
9519 */
6ff58d53 9520static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
be256dc7
PZ
9521{
9522 uint32_t val;
9523
9524 val = I915_READ(LCPLL_CTL);
9525
9526 if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
9527 LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
9528 return;
9529
a8a8bd54
PZ
9530 /*
9531 * Make sure we're not on PC8 state before disabling PC8, otherwise
9532 * we'll hang the machine. To prevent PC8 state, just enable force_wake.
a8a8bd54 9533 */
59bad947 9534 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
215733fa 9535
be256dc7
PZ
9536 if (val & LCPLL_POWER_DOWN_ALLOW) {
9537 val &= ~LCPLL_POWER_DOWN_ALLOW;
9538 I915_WRITE(LCPLL_CTL, val);
35d8f2eb 9539 POSTING_READ(LCPLL_CTL);
be256dc7
PZ
9540 }
9541
9ccd5aeb 9542 val = hsw_read_dcomp(dev_priv);
be256dc7
PZ
9543 val |= D_COMP_COMP_FORCE;
9544 val &= ~D_COMP_COMP_DISABLE;
3c4c9b81 9545 hsw_write_dcomp(dev_priv, val);
be256dc7
PZ
9546
9547 val = I915_READ(LCPLL_CTL);
9548 val &= ~LCPLL_PLL_DISABLE;
9549 I915_WRITE(LCPLL_CTL, val);
9550
93220c08
CW
9551 if (intel_wait_for_register(dev_priv,
9552 LCPLL_CTL, LCPLL_PLL_LOCK, LCPLL_PLL_LOCK,
9553 5))
be256dc7
PZ
9554 DRM_ERROR("LCPLL not locked yet\n");
9555
9556 if (val & LCPLL_CD_SOURCE_FCLK) {
9557 val = I915_READ(LCPLL_CTL);
9558 val &= ~LCPLL_CD_SOURCE_FCLK;
9559 I915_WRITE(LCPLL_CTL, val);
9560
f53dd63f
ID
9561 if (wait_for_us((I915_READ(LCPLL_CTL) &
9562 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
be256dc7
PZ
9563 DRM_ERROR("Switching back to LCPLL failed\n");
9564 }
215733fa 9565
59bad947 9566 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
91c8a326 9567 intel_update_cdclk(&dev_priv->drm);
be256dc7
PZ
9568}
9569
765dab67
PZ
9570/*
9571 * Package states C8 and deeper are really deep PC states that can only be
9572 * reached when all the devices on the system allow it, so even if the graphics
9573 * device allows PC8+, it doesn't mean the system will actually get to these
9574 * states. Our driver only allows PC8+ when going into runtime PM.
9575 *
9576 * The requirements for PC8+ are that all the outputs are disabled, the power
9577 * well is disabled and most interrupts are disabled, and these are also
9578 * requirements for runtime PM. When these conditions are met, we manually do
9579 * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
9580 * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
9581 * hang the machine.
9582 *
9583 * When we really reach PC8 or deeper states (not just when we allow it) we lose
9584 * the state of some registers, so when we come back from PC8+ we need to
9585 * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
9586 * need to take care of the registers kept by RC6. Notice that this happens even
9587 * if we don't put the device in PCI D3 state (which is what currently happens
9588 * because of the runtime PM support).
9589 *
9590 * For more, read "Display Sequences for Package C8" on the hardware
9591 * documentation.
9592 */
a14cb6fc 9593void hsw_enable_pc8(struct drm_i915_private *dev_priv)
c67a470b 9594{
91c8a326 9595 struct drm_device *dev = &dev_priv->drm;
c67a470b
PZ
9596 uint32_t val;
9597
c67a470b
PZ
9598 DRM_DEBUG_KMS("Enabling package C8+\n");
9599
c2699524 9600 if (HAS_PCH_LPT_LP(dev)) {
c67a470b
PZ
9601 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9602 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
9603 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9604 }
9605
9606 lpt_disable_clkout_dp(dev);
c67a470b
PZ
9607 hsw_disable_lcpll(dev_priv, true, true);
9608}
9609
a14cb6fc 9610void hsw_disable_pc8(struct drm_i915_private *dev_priv)
c67a470b 9611{
91c8a326 9612 struct drm_device *dev = &dev_priv->drm;
c67a470b
PZ
9613 uint32_t val;
9614
c67a470b
PZ
9615 DRM_DEBUG_KMS("Disabling package C8+\n");
9616
9617 hsw_restore_lcpll(dev_priv);
c67a470b
PZ
9618 lpt_init_pch_refclk(dev);
9619
c2699524 9620 if (HAS_PCH_LPT_LP(dev)) {
c67a470b
PZ
9621 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9622 val |= PCH_LP_PARTITION_LEVEL_DISABLE;
9623 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9624 }
c67a470b
PZ
9625}
9626
324513c0 9627static void bxt_modeset_commit_cdclk(struct drm_atomic_state *old_state)
f8437dd1 9628{
a821fc46 9629 struct drm_device *dev = old_state->dev;
1a617b77
ML
9630 struct intel_atomic_state *old_intel_state =
9631 to_intel_atomic_state(old_state);
9632 unsigned int req_cdclk = old_intel_state->dev_cdclk;
f8437dd1 9633
324513c0 9634 bxt_set_cdclk(to_i915(dev), req_cdclk);
f8437dd1
VK
9635}
9636
b432e5cf 9637/* compute the max rate for new configuration */
27c329ed 9638static int ilk_max_pixel_rate(struct drm_atomic_state *state)
b432e5cf 9639{
565602d7 9640 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
fac5e23e 9641 struct drm_i915_private *dev_priv = to_i915(state->dev);
565602d7
ML
9642 struct drm_crtc *crtc;
9643 struct drm_crtc_state *cstate;
27c329ed 9644 struct intel_crtc_state *crtc_state;
565602d7
ML
9645 unsigned max_pixel_rate = 0, i;
9646 enum pipe pipe;
b432e5cf 9647
565602d7
ML
9648 memcpy(intel_state->min_pixclk, dev_priv->min_pixclk,
9649 sizeof(intel_state->min_pixclk));
27c329ed 9650
565602d7
ML
9651 for_each_crtc_in_state(state, crtc, cstate, i) {
9652 int pixel_rate;
27c329ed 9653
565602d7
ML
9654 crtc_state = to_intel_crtc_state(cstate);
9655 if (!crtc_state->base.enable) {
9656 intel_state->min_pixclk[i] = 0;
b432e5cf 9657 continue;
565602d7 9658 }
b432e5cf 9659
27c329ed 9660 pixel_rate = ilk_pipe_pixel_rate(crtc_state);
b432e5cf
VS
9661
9662 /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
565602d7 9663 if (IS_BROADWELL(dev_priv) && crtc_state->ips_enabled)
b432e5cf
VS
9664 pixel_rate = DIV_ROUND_UP(pixel_rate * 100, 95);
9665
565602d7 9666 intel_state->min_pixclk[i] = pixel_rate;
b432e5cf
VS
9667 }
9668
565602d7
ML
9669 for_each_pipe(dev_priv, pipe)
9670 max_pixel_rate = max(intel_state->min_pixclk[pipe], max_pixel_rate);
9671
b432e5cf
VS
9672 return max_pixel_rate;
9673}
9674
9675static void broadwell_set_cdclk(struct drm_device *dev, int cdclk)
9676{
fac5e23e 9677 struct drm_i915_private *dev_priv = to_i915(dev);
b432e5cf
VS
9678 uint32_t val, data;
9679 int ret;
9680
9681 if (WARN((I915_READ(LCPLL_CTL) &
9682 (LCPLL_PLL_DISABLE | LCPLL_PLL_LOCK |
9683 LCPLL_CD_CLOCK_DISABLE | LCPLL_ROOT_CD_CLOCK_DISABLE |
9684 LCPLL_CD2X_CLOCK_DISABLE | LCPLL_POWER_DOWN_ALLOW |
9685 LCPLL_CD_SOURCE_FCLK)) != LCPLL_PLL_LOCK,
9686 "trying to change cdclk frequency with cdclk not enabled\n"))
9687 return;
9688
9689 mutex_lock(&dev_priv->rps.hw_lock);
9690 ret = sandybridge_pcode_write(dev_priv,
9691 BDW_PCODE_DISPLAY_FREQ_CHANGE_REQ, 0x0);
9692 mutex_unlock(&dev_priv->rps.hw_lock);
9693 if (ret) {
9694 DRM_ERROR("failed to inform pcode about cdclk change\n");
9695 return;
9696 }
9697
9698 val = I915_READ(LCPLL_CTL);
9699 val |= LCPLL_CD_SOURCE_FCLK;
9700 I915_WRITE(LCPLL_CTL, val);
9701
5ba00178
TU
9702 if (wait_for_us(I915_READ(LCPLL_CTL) &
9703 LCPLL_CD_SOURCE_FCLK_DONE, 1))
b432e5cf
VS
9704 DRM_ERROR("Switching to FCLK failed\n");
9705
9706 val = I915_READ(LCPLL_CTL);
9707 val &= ~LCPLL_CLK_FREQ_MASK;
9708
9709 switch (cdclk) {
9710 case 450000:
9711 val |= LCPLL_CLK_FREQ_450;
9712 data = 0;
9713 break;
9714 case 540000:
9715 val |= LCPLL_CLK_FREQ_54O_BDW;
9716 data = 1;
9717 break;
9718 case 337500:
9719 val |= LCPLL_CLK_FREQ_337_5_BDW;
9720 data = 2;
9721 break;
9722 case 675000:
9723 val |= LCPLL_CLK_FREQ_675_BDW;
9724 data = 3;
9725 break;
9726 default:
9727 WARN(1, "invalid cdclk frequency\n");
9728 return;
9729 }
9730
9731 I915_WRITE(LCPLL_CTL, val);
9732
9733 val = I915_READ(LCPLL_CTL);
9734 val &= ~LCPLL_CD_SOURCE_FCLK;
9735 I915_WRITE(LCPLL_CTL, val);
9736
5ba00178
TU
9737 if (wait_for_us((I915_READ(LCPLL_CTL) &
9738 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
b432e5cf
VS
9739 DRM_ERROR("Switching back to LCPLL failed\n");
9740
9741 mutex_lock(&dev_priv->rps.hw_lock);
9742 sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ, data);
9743 mutex_unlock(&dev_priv->rps.hw_lock);
9744
7f1052a8
VS
9745 I915_WRITE(CDCLK_FREQ, DIV_ROUND_CLOSEST(cdclk, 1000) - 1);
9746
b432e5cf
VS
9747 intel_update_cdclk(dev);
9748
9749 WARN(cdclk != dev_priv->cdclk_freq,
9750 "cdclk requested %d kHz but got %d kHz\n",
9751 cdclk, dev_priv->cdclk_freq);
9752}
9753
587c7914
VS
9754static int broadwell_calc_cdclk(int max_pixclk)
9755{
9756 if (max_pixclk > 540000)
9757 return 675000;
9758 else if (max_pixclk > 450000)
9759 return 540000;
9760 else if (max_pixclk > 337500)
9761 return 450000;
9762 else
9763 return 337500;
9764}
9765
27c329ed 9766static int broadwell_modeset_calc_cdclk(struct drm_atomic_state *state)
b432e5cf 9767{
27c329ed 9768 struct drm_i915_private *dev_priv = to_i915(state->dev);
1a617b77 9769 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
27c329ed 9770 int max_pixclk = ilk_max_pixel_rate(state);
b432e5cf
VS
9771 int cdclk;
9772
9773 /*
9774 * FIXME should also account for plane ratio
9775 * once 64bpp pixel formats are supported.
9776 */
587c7914 9777 cdclk = broadwell_calc_cdclk(max_pixclk);
b432e5cf 9778
b432e5cf 9779 if (cdclk > dev_priv->max_cdclk_freq) {
63ba534e
ML
9780 DRM_DEBUG_KMS("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
9781 cdclk, dev_priv->max_cdclk_freq);
9782 return -EINVAL;
b432e5cf
VS
9783 }
9784
1a617b77
ML
9785 intel_state->cdclk = intel_state->dev_cdclk = cdclk;
9786 if (!intel_state->active_crtcs)
587c7914 9787 intel_state->dev_cdclk = broadwell_calc_cdclk(0);
b432e5cf
VS
9788
9789 return 0;
9790}
9791
27c329ed 9792static void broadwell_modeset_commit_cdclk(struct drm_atomic_state *old_state)
b432e5cf 9793{
27c329ed 9794 struct drm_device *dev = old_state->dev;
1a617b77
ML
9795 struct intel_atomic_state *old_intel_state =
9796 to_intel_atomic_state(old_state);
9797 unsigned req_cdclk = old_intel_state->dev_cdclk;
b432e5cf 9798
27c329ed 9799 broadwell_set_cdclk(dev, req_cdclk);
b432e5cf
VS
9800}
9801
c89e39f3
CT
9802static int skl_modeset_calc_cdclk(struct drm_atomic_state *state)
9803{
9804 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
9805 struct drm_i915_private *dev_priv = to_i915(state->dev);
9806 const int max_pixclk = ilk_max_pixel_rate(state);
a8ca4934 9807 int vco = intel_state->cdclk_pll_vco;
c89e39f3
CT
9808 int cdclk;
9809
9810 /*
9811 * FIXME should also account for plane ratio
9812 * once 64bpp pixel formats are supported.
9813 */
a8ca4934 9814 cdclk = skl_calc_cdclk(max_pixclk, vco);
c89e39f3
CT
9815
9816 /*
9817 * FIXME move the cdclk caclulation to
9818 * compute_config() so we can fail gracegully.
9819 */
9820 if (cdclk > dev_priv->max_cdclk_freq) {
9821 DRM_ERROR("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
9822 cdclk, dev_priv->max_cdclk_freq);
9823 cdclk = dev_priv->max_cdclk_freq;
9824 }
9825
9826 intel_state->cdclk = intel_state->dev_cdclk = cdclk;
9827 if (!intel_state->active_crtcs)
a8ca4934 9828 intel_state->dev_cdclk = skl_calc_cdclk(0, vco);
c89e39f3
CT
9829
9830 return 0;
9831}
9832
9833static void skl_modeset_commit_cdclk(struct drm_atomic_state *old_state)
9834{
1cd593e0
VS
9835 struct drm_i915_private *dev_priv = to_i915(old_state->dev);
9836 struct intel_atomic_state *intel_state = to_intel_atomic_state(old_state);
9837 unsigned int req_cdclk = intel_state->dev_cdclk;
9838 unsigned int req_vco = intel_state->cdclk_pll_vco;
c89e39f3 9839
1cd593e0 9840 skl_set_cdclk(dev_priv, req_cdclk, req_vco);
c89e39f3
CT
9841}
9842
190f68c5
ACO
9843static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
9844 struct intel_crtc_state *crtc_state)
09b4ddf9 9845{
d7edc4e5 9846 if (!intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DSI)) {
af3997b5
MK
9847 if (!intel_ddi_pll_select(crtc, crtc_state))
9848 return -EINVAL;
9849 }
716c2e55 9850
c7653199 9851 crtc->lowfreq_avail = false;
644cef34 9852
c8f7a0db 9853 return 0;
79e53945
JB
9854}
9855
3760b59c
S
9856static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
9857 enum port port,
9858 struct intel_crtc_state *pipe_config)
9859{
8106ddbd
ACO
9860 enum intel_dpll_id id;
9861
3760b59c
S
9862 switch (port) {
9863 case PORT_A:
9864 pipe_config->ddi_pll_sel = SKL_DPLL0;
08250c4b 9865 id = DPLL_ID_SKL_DPLL0;
3760b59c
S
9866 break;
9867 case PORT_B:
9868 pipe_config->ddi_pll_sel = SKL_DPLL1;
08250c4b 9869 id = DPLL_ID_SKL_DPLL1;
3760b59c
S
9870 break;
9871 case PORT_C:
9872 pipe_config->ddi_pll_sel = SKL_DPLL2;
08250c4b 9873 id = DPLL_ID_SKL_DPLL2;
3760b59c
S
9874 break;
9875 default:
9876 DRM_ERROR("Incorrect port type\n");
8106ddbd 9877 return;
3760b59c 9878 }
8106ddbd
ACO
9879
9880 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
3760b59c
S
9881}
9882
96b7dfb7
S
9883static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
9884 enum port port,
5cec258b 9885 struct intel_crtc_state *pipe_config)
96b7dfb7 9886{
8106ddbd 9887 enum intel_dpll_id id;
a3c988ea 9888 u32 temp;
96b7dfb7
S
9889
9890 temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
9891 pipe_config->ddi_pll_sel = temp >> (port * 3 + 1);
9892
9893 switch (pipe_config->ddi_pll_sel) {
3148ade7 9894 case SKL_DPLL0:
a3c988ea
ACO
9895 id = DPLL_ID_SKL_DPLL0;
9896 break;
96b7dfb7 9897 case SKL_DPLL1:
8106ddbd 9898 id = DPLL_ID_SKL_DPLL1;
96b7dfb7
S
9899 break;
9900 case SKL_DPLL2:
8106ddbd 9901 id = DPLL_ID_SKL_DPLL2;
96b7dfb7
S
9902 break;
9903 case SKL_DPLL3:
8106ddbd 9904 id = DPLL_ID_SKL_DPLL3;
96b7dfb7 9905 break;
8106ddbd
ACO
9906 default:
9907 MISSING_CASE(pipe_config->ddi_pll_sel);
9908 return;
96b7dfb7 9909 }
8106ddbd
ACO
9910
9911 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
96b7dfb7
S
9912}
9913
7d2c8175
DL
9914static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
9915 enum port port,
5cec258b 9916 struct intel_crtc_state *pipe_config)
7d2c8175 9917{
8106ddbd
ACO
9918 enum intel_dpll_id id;
9919
7d2c8175
DL
9920 pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
9921
9922 switch (pipe_config->ddi_pll_sel) {
9923 case PORT_CLK_SEL_WRPLL1:
8106ddbd 9924 id = DPLL_ID_WRPLL1;
7d2c8175
DL
9925 break;
9926 case PORT_CLK_SEL_WRPLL2:
8106ddbd 9927 id = DPLL_ID_WRPLL2;
7d2c8175 9928 break;
00490c22 9929 case PORT_CLK_SEL_SPLL:
8106ddbd 9930 id = DPLL_ID_SPLL;
79bd23da 9931 break;
9d16da65
ACO
9932 case PORT_CLK_SEL_LCPLL_810:
9933 id = DPLL_ID_LCPLL_810;
9934 break;
9935 case PORT_CLK_SEL_LCPLL_1350:
9936 id = DPLL_ID_LCPLL_1350;
9937 break;
9938 case PORT_CLK_SEL_LCPLL_2700:
9939 id = DPLL_ID_LCPLL_2700;
9940 break;
8106ddbd
ACO
9941 default:
9942 MISSING_CASE(pipe_config->ddi_pll_sel);
9943 /* fall through */
9944 case PORT_CLK_SEL_NONE:
8106ddbd 9945 return;
7d2c8175 9946 }
8106ddbd
ACO
9947
9948 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
7d2c8175
DL
9949}
9950
cf30429e
JN
9951static bool hsw_get_transcoder_state(struct intel_crtc *crtc,
9952 struct intel_crtc_state *pipe_config,
9953 unsigned long *power_domain_mask)
9954{
9955 struct drm_device *dev = crtc->base.dev;
fac5e23e 9956 struct drm_i915_private *dev_priv = to_i915(dev);
cf30429e
JN
9957 enum intel_display_power_domain power_domain;
9958 u32 tmp;
9959
d9a7bc67
ID
9960 /*
9961 * The pipe->transcoder mapping is fixed with the exception of the eDP
9962 * transcoder handled below.
9963 */
cf30429e
JN
9964 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
9965
9966 /*
9967 * XXX: Do intel_display_power_get_if_enabled before reading this (for
9968 * consistency and less surprising code; it's in always on power).
9969 */
9970 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
9971 if (tmp & TRANS_DDI_FUNC_ENABLE) {
9972 enum pipe trans_edp_pipe;
9973 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
9974 default:
9975 WARN(1, "unknown pipe linked to edp transcoder\n");
9976 case TRANS_DDI_EDP_INPUT_A_ONOFF:
9977 case TRANS_DDI_EDP_INPUT_A_ON:
9978 trans_edp_pipe = PIPE_A;
9979 break;
9980 case TRANS_DDI_EDP_INPUT_B_ONOFF:
9981 trans_edp_pipe = PIPE_B;
9982 break;
9983 case TRANS_DDI_EDP_INPUT_C_ONOFF:
9984 trans_edp_pipe = PIPE_C;
9985 break;
9986 }
9987
9988 if (trans_edp_pipe == crtc->pipe)
9989 pipe_config->cpu_transcoder = TRANSCODER_EDP;
9990 }
9991
9992 power_domain = POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder);
9993 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
9994 return false;
9995 *power_domain_mask |= BIT(power_domain);
9996
9997 tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
9998
9999 return tmp & PIPECONF_ENABLE;
10000}
10001
4d1de975
JN
10002static bool bxt_get_dsi_transcoder_state(struct intel_crtc *crtc,
10003 struct intel_crtc_state *pipe_config,
10004 unsigned long *power_domain_mask)
10005{
10006 struct drm_device *dev = crtc->base.dev;
fac5e23e 10007 struct drm_i915_private *dev_priv = to_i915(dev);
4d1de975
JN
10008 enum intel_display_power_domain power_domain;
10009 enum port port;
10010 enum transcoder cpu_transcoder;
10011 u32 tmp;
10012
4d1de975
JN
10013 for_each_port_masked(port, BIT(PORT_A) | BIT(PORT_C)) {
10014 if (port == PORT_A)
10015 cpu_transcoder = TRANSCODER_DSI_A;
10016 else
10017 cpu_transcoder = TRANSCODER_DSI_C;
10018
10019 power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
10020 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
10021 continue;
10022 *power_domain_mask |= BIT(power_domain);
10023
db18b6a6
ID
10024 /*
10025 * The PLL needs to be enabled with a valid divider
10026 * configuration, otherwise accessing DSI registers will hang
10027 * the machine. See BSpec North Display Engine
10028 * registers/MIPI[BXT]. We can break out here early, since we
10029 * need the same DSI PLL to be enabled for both DSI ports.
10030 */
10031 if (!intel_dsi_pll_is_enabled(dev_priv))
10032 break;
10033
4d1de975
JN
10034 /* XXX: this works for video mode only */
10035 tmp = I915_READ(BXT_MIPI_PORT_CTRL(port));
10036 if (!(tmp & DPI_ENABLE))
10037 continue;
10038
10039 tmp = I915_READ(MIPI_CTRL(port));
10040 if ((tmp & BXT_PIPE_SELECT_MASK) != BXT_PIPE_SELECT(crtc->pipe))
10041 continue;
10042
10043 pipe_config->cpu_transcoder = cpu_transcoder;
4d1de975
JN
10044 break;
10045 }
10046
d7edc4e5 10047 return transcoder_is_dsi(pipe_config->cpu_transcoder);
4d1de975
JN
10048}
10049
26804afd 10050static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
5cec258b 10051 struct intel_crtc_state *pipe_config)
26804afd
DV
10052{
10053 struct drm_device *dev = crtc->base.dev;
fac5e23e 10054 struct drm_i915_private *dev_priv = to_i915(dev);
d452c5b6 10055 struct intel_shared_dpll *pll;
26804afd
DV
10056 enum port port;
10057 uint32_t tmp;
10058
10059 tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
10060
10061 port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
10062
ef11bdb3 10063 if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev))
96b7dfb7 10064 skylake_get_ddi_pll(dev_priv, port, pipe_config);
3760b59c
S
10065 else if (IS_BROXTON(dev))
10066 bxt_get_ddi_pll(dev_priv, port, pipe_config);
96b7dfb7
S
10067 else
10068 haswell_get_ddi_pll(dev_priv, port, pipe_config);
9cd86933 10069
8106ddbd
ACO
10070 pll = pipe_config->shared_dpll;
10071 if (pll) {
2edd6443
ACO
10072 WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
10073 &pipe_config->dpll_hw_state));
d452c5b6
DV
10074 }
10075
26804afd
DV
10076 /*
10077 * Haswell has only FDI/PCH transcoder A. It is which is connected to
10078 * DDI E. So just check whether this pipe is wired to DDI E and whether
10079 * the PCH transcoder is on.
10080 */
ca370455
DL
10081 if (INTEL_INFO(dev)->gen < 9 &&
10082 (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
26804afd
DV
10083 pipe_config->has_pch_encoder = true;
10084
10085 tmp = I915_READ(FDI_RX_CTL(PIPE_A));
10086 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
10087 FDI_DP_PORT_WIDTH_SHIFT) + 1;
10088
10089 ironlake_get_fdi_m_n_config(crtc, pipe_config);
10090 }
10091}
10092
0e8ffe1b 10093static bool haswell_get_pipe_config(struct intel_crtc *crtc,
5cec258b 10094 struct intel_crtc_state *pipe_config)
0e8ffe1b
DV
10095{
10096 struct drm_device *dev = crtc->base.dev;
fac5e23e 10097 struct drm_i915_private *dev_priv = to_i915(dev);
1729050e
ID
10098 enum intel_display_power_domain power_domain;
10099 unsigned long power_domain_mask;
cf30429e 10100 bool active;
0e8ffe1b 10101
1729050e
ID
10102 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
10103 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
b5482bd0 10104 return false;
1729050e
ID
10105 power_domain_mask = BIT(power_domain);
10106
8106ddbd 10107 pipe_config->shared_dpll = NULL;
c0d43d62 10108
cf30429e 10109 active = hsw_get_transcoder_state(crtc, pipe_config, &power_domain_mask);
eccb140b 10110
d7edc4e5
VS
10111 if (IS_BROXTON(dev_priv) &&
10112 bxt_get_dsi_transcoder_state(crtc, pipe_config, &power_domain_mask)) {
10113 WARN_ON(active);
10114 active = true;
4d1de975
JN
10115 }
10116
cf30429e 10117 if (!active)
1729050e 10118 goto out;
0e8ffe1b 10119
d7edc4e5 10120 if (!transcoder_is_dsi(pipe_config->cpu_transcoder)) {
4d1de975
JN
10121 haswell_get_ddi_port_state(crtc, pipe_config);
10122 intel_get_pipe_timings(crtc, pipe_config);
10123 }
627eb5a3 10124
bc58be60 10125 intel_get_pipe_src_size(crtc, pipe_config);
1bd1bd80 10126
05dc698c
LL
10127 pipe_config->gamma_mode =
10128 I915_READ(GAMMA_MODE(crtc->pipe)) & GAMMA_MODE_MODE_MASK;
10129
a1b2278e
CK
10130 if (INTEL_INFO(dev)->gen >= 9) {
10131 skl_init_scalers(dev, crtc, pipe_config);
10132 }
10133
af99ceda
CK
10134 if (INTEL_INFO(dev)->gen >= 9) {
10135 pipe_config->scaler_state.scaler_id = -1;
10136 pipe_config->scaler_state.scaler_users &= ~(1 << SKL_CRTC_INDEX);
10137 }
10138
1729050e
ID
10139 power_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
10140 if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
10141 power_domain_mask |= BIT(power_domain);
1c132b44 10142 if (INTEL_INFO(dev)->gen >= 9)
bd2e244f 10143 skylake_get_pfit_config(crtc, pipe_config);
ff6d9f55 10144 else
1c132b44 10145 ironlake_get_pfit_config(crtc, pipe_config);
bd2e244f 10146 }
88adfff1 10147
e59150dc
JB
10148 if (IS_HASWELL(dev))
10149 pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
10150 (I915_READ(IPS_CTL) & IPS_ENABLE);
42db64ef 10151
4d1de975
JN
10152 if (pipe_config->cpu_transcoder != TRANSCODER_EDP &&
10153 !transcoder_is_dsi(pipe_config->cpu_transcoder)) {
ebb69c95
CT
10154 pipe_config->pixel_multiplier =
10155 I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
10156 } else {
10157 pipe_config->pixel_multiplier = 1;
10158 }
6c49f241 10159
1729050e
ID
10160out:
10161 for_each_power_domain(power_domain, power_domain_mask)
10162 intel_display_power_put(dev_priv, power_domain);
10163
cf30429e 10164 return active;
0e8ffe1b
DV
10165}
10166
55a08b3f
ML
10167static void i845_update_cursor(struct drm_crtc *crtc, u32 base,
10168 const struct intel_plane_state *plane_state)
560b85bb
CW
10169{
10170 struct drm_device *dev = crtc->dev;
fac5e23e 10171 struct drm_i915_private *dev_priv = to_i915(dev);
560b85bb 10172 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
dc41c154 10173 uint32_t cntl = 0, size = 0;
560b85bb 10174
55a08b3f
ML
10175 if (plane_state && plane_state->visible) {
10176 unsigned int width = plane_state->base.crtc_w;
10177 unsigned int height = plane_state->base.crtc_h;
dc41c154
VS
10178 unsigned int stride = roundup_pow_of_two(width) * 4;
10179
10180 switch (stride) {
10181 default:
10182 WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
10183 width, stride);
10184 stride = 256;
10185 /* fallthrough */
10186 case 256:
10187 case 512:
10188 case 1024:
10189 case 2048:
10190 break;
4b0e333e
CW
10191 }
10192
dc41c154
VS
10193 cntl |= CURSOR_ENABLE |
10194 CURSOR_GAMMA_ENABLE |
10195 CURSOR_FORMAT_ARGB |
10196 CURSOR_STRIDE(stride);
10197
10198 size = (height << 12) | width;
4b0e333e 10199 }
560b85bb 10200
dc41c154
VS
10201 if (intel_crtc->cursor_cntl != 0 &&
10202 (intel_crtc->cursor_base != base ||
10203 intel_crtc->cursor_size != size ||
10204 intel_crtc->cursor_cntl != cntl)) {
10205 /* On these chipsets we can only modify the base/size/stride
10206 * whilst the cursor is disabled.
10207 */
0b87c24e
VS
10208 I915_WRITE(CURCNTR(PIPE_A), 0);
10209 POSTING_READ(CURCNTR(PIPE_A));
dc41c154 10210 intel_crtc->cursor_cntl = 0;
4b0e333e 10211 }
560b85bb 10212
99d1f387 10213 if (intel_crtc->cursor_base != base) {
0b87c24e 10214 I915_WRITE(CURBASE(PIPE_A), base);
99d1f387
VS
10215 intel_crtc->cursor_base = base;
10216 }
4726e0b0 10217
dc41c154
VS
10218 if (intel_crtc->cursor_size != size) {
10219 I915_WRITE(CURSIZE, size);
10220 intel_crtc->cursor_size = size;
4b0e333e 10221 }
560b85bb 10222
4b0e333e 10223 if (intel_crtc->cursor_cntl != cntl) {
0b87c24e
VS
10224 I915_WRITE(CURCNTR(PIPE_A), cntl);
10225 POSTING_READ(CURCNTR(PIPE_A));
4b0e333e 10226 intel_crtc->cursor_cntl = cntl;
560b85bb 10227 }
560b85bb
CW
10228}
10229
55a08b3f
ML
10230static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base,
10231 const struct intel_plane_state *plane_state)
65a21cd6
JB
10232{
10233 struct drm_device *dev = crtc->dev;
fac5e23e 10234 struct drm_i915_private *dev_priv = to_i915(dev);
65a21cd6
JB
10235 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10236 int pipe = intel_crtc->pipe;
663f3122 10237 uint32_t cntl = 0;
4b0e333e 10238
55a08b3f 10239 if (plane_state && plane_state->visible) {
4b0e333e 10240 cntl = MCURSOR_GAMMA_ENABLE;
55a08b3f 10241 switch (plane_state->base.crtc_w) {
4726e0b0
SK
10242 case 64:
10243 cntl |= CURSOR_MODE_64_ARGB_AX;
10244 break;
10245 case 128:
10246 cntl |= CURSOR_MODE_128_ARGB_AX;
10247 break;
10248 case 256:
10249 cntl |= CURSOR_MODE_256_ARGB_AX;
10250 break;
10251 default:
55a08b3f 10252 MISSING_CASE(plane_state->base.crtc_w);
4726e0b0 10253 return;
65a21cd6 10254 }
4b0e333e 10255 cntl |= pipe << 28; /* Connect to correct pipe */
47bf17a7 10256
fc6f93bc 10257 if (HAS_DDI(dev))
47bf17a7 10258 cntl |= CURSOR_PIPE_CSC_ENABLE;
65a21cd6 10259
55a08b3f
ML
10260 if (plane_state->base.rotation == BIT(DRM_ROTATE_180))
10261 cntl |= CURSOR_ROTATE_180;
10262 }
4398ad45 10263
4b0e333e
CW
10264 if (intel_crtc->cursor_cntl != cntl) {
10265 I915_WRITE(CURCNTR(pipe), cntl);
10266 POSTING_READ(CURCNTR(pipe));
10267 intel_crtc->cursor_cntl = cntl;
65a21cd6 10268 }
4b0e333e 10269
65a21cd6 10270 /* and commit changes on next vblank */
5efb3e28
VS
10271 I915_WRITE(CURBASE(pipe), base);
10272 POSTING_READ(CURBASE(pipe));
99d1f387
VS
10273
10274 intel_crtc->cursor_base = base;
65a21cd6
JB
10275}
10276
cda4b7d3 10277/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
6b383a7f 10278static void intel_crtc_update_cursor(struct drm_crtc *crtc,
55a08b3f 10279 const struct intel_plane_state *plane_state)
cda4b7d3
CW
10280{
10281 struct drm_device *dev = crtc->dev;
fac5e23e 10282 struct drm_i915_private *dev_priv = to_i915(dev);
cda4b7d3
CW
10283 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10284 int pipe = intel_crtc->pipe;
55a08b3f
ML
10285 u32 base = intel_crtc->cursor_addr;
10286 u32 pos = 0;
cda4b7d3 10287
55a08b3f
ML
10288 if (plane_state) {
10289 int x = plane_state->base.crtc_x;
10290 int y = plane_state->base.crtc_y;
cda4b7d3 10291
55a08b3f
ML
10292 if (x < 0) {
10293 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
10294 x = -x;
10295 }
10296 pos |= x << CURSOR_X_SHIFT;
cda4b7d3 10297
55a08b3f
ML
10298 if (y < 0) {
10299 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
10300 y = -y;
10301 }
10302 pos |= y << CURSOR_Y_SHIFT;
10303
10304 /* ILK+ do this automagically */
10305 if (HAS_GMCH_DISPLAY(dev) &&
10306 plane_state->base.rotation == BIT(DRM_ROTATE_180)) {
10307 base += (plane_state->base.crtc_h *
10308 plane_state->base.crtc_w - 1) * 4;
10309 }
cda4b7d3 10310 }
cda4b7d3 10311
5efb3e28
VS
10312 I915_WRITE(CURPOS(pipe), pos);
10313
8ac54669 10314 if (IS_845G(dev) || IS_I865G(dev))
55a08b3f 10315 i845_update_cursor(crtc, base, plane_state);
5efb3e28 10316 else
55a08b3f 10317 i9xx_update_cursor(crtc, base, plane_state);
cda4b7d3
CW
10318}
10319
dc41c154
VS
10320static bool cursor_size_ok(struct drm_device *dev,
10321 uint32_t width, uint32_t height)
10322{
10323 if (width == 0 || height == 0)
10324 return false;
10325
10326 /*
10327 * 845g/865g are special in that they are only limited by
10328 * the width of their cursors, the height is arbitrary up to
10329 * the precision of the register. Everything else requires
10330 * square cursors, limited to a few power-of-two sizes.
10331 */
10332 if (IS_845G(dev) || IS_I865G(dev)) {
10333 if ((width & 63) != 0)
10334 return false;
10335
10336 if (width > (IS_845G(dev) ? 64 : 512))
10337 return false;
10338
10339 if (height > 1023)
10340 return false;
10341 } else {
10342 switch (width | height) {
10343 case 256:
10344 case 128:
10345 if (IS_GEN2(dev))
10346 return false;
10347 case 64:
10348 break;
10349 default:
10350 return false;
10351 }
10352 }
10353
10354 return true;
10355}
10356
79e53945
JB
10357/* VESA 640x480x72Hz mode to set on the pipe */
10358static struct drm_display_mode load_detect_mode = {
10359 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
10360 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
10361};
10362
a8bb6818
DV
10363struct drm_framebuffer *
10364__intel_framebuffer_create(struct drm_device *dev,
10365 struct drm_mode_fb_cmd2 *mode_cmd,
10366 struct drm_i915_gem_object *obj)
d2dff872
CW
10367{
10368 struct intel_framebuffer *intel_fb;
10369 int ret;
10370
10371 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
dcb1394e 10372 if (!intel_fb)
d2dff872 10373 return ERR_PTR(-ENOMEM);
d2dff872
CW
10374
10375 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
dd4916c5
DV
10376 if (ret)
10377 goto err;
d2dff872
CW
10378
10379 return &intel_fb->base;
dcb1394e 10380
dd4916c5 10381err:
dd4916c5 10382 kfree(intel_fb);
dd4916c5 10383 return ERR_PTR(ret);
d2dff872
CW
10384}
10385
b5ea642a 10386static struct drm_framebuffer *
a8bb6818
DV
10387intel_framebuffer_create(struct drm_device *dev,
10388 struct drm_mode_fb_cmd2 *mode_cmd,
10389 struct drm_i915_gem_object *obj)
10390{
10391 struct drm_framebuffer *fb;
10392 int ret;
10393
10394 ret = i915_mutex_lock_interruptible(dev);
10395 if (ret)
10396 return ERR_PTR(ret);
10397 fb = __intel_framebuffer_create(dev, mode_cmd, obj);
10398 mutex_unlock(&dev->struct_mutex);
10399
10400 return fb;
10401}
10402
d2dff872
CW
10403static u32
10404intel_framebuffer_pitch_for_width(int width, int bpp)
10405{
10406 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
10407 return ALIGN(pitch, 64);
10408}
10409
10410static u32
10411intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
10412{
10413 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
1267a26b 10414 return PAGE_ALIGN(pitch * mode->vdisplay);
d2dff872
CW
10415}
10416
10417static struct drm_framebuffer *
10418intel_framebuffer_create_for_mode(struct drm_device *dev,
10419 struct drm_display_mode *mode,
10420 int depth, int bpp)
10421{
dcb1394e 10422 struct drm_framebuffer *fb;
d2dff872 10423 struct drm_i915_gem_object *obj;
0fed39bd 10424 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
d2dff872 10425
d37cd8a8 10426 obj = i915_gem_object_create(dev,
d2dff872 10427 intel_framebuffer_size_for_mode(mode, bpp));
fe3db79b
CW
10428 if (IS_ERR(obj))
10429 return ERR_CAST(obj);
d2dff872
CW
10430
10431 mode_cmd.width = mode->hdisplay;
10432 mode_cmd.height = mode->vdisplay;
308e5bcb
JB
10433 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
10434 bpp);
5ca0c34a 10435 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
d2dff872 10436
dcb1394e
LW
10437 fb = intel_framebuffer_create(dev, &mode_cmd, obj);
10438 if (IS_ERR(fb))
34911fd3 10439 i915_gem_object_put_unlocked(obj);
dcb1394e
LW
10440
10441 return fb;
d2dff872
CW
10442}
10443
10444static struct drm_framebuffer *
10445mode_fits_in_fbdev(struct drm_device *dev,
10446 struct drm_display_mode *mode)
10447{
0695726e 10448#ifdef CONFIG_DRM_FBDEV_EMULATION
fac5e23e 10449 struct drm_i915_private *dev_priv = to_i915(dev);
d2dff872
CW
10450 struct drm_i915_gem_object *obj;
10451 struct drm_framebuffer *fb;
10452
4c0e5528 10453 if (!dev_priv->fbdev)
d2dff872
CW
10454 return NULL;
10455
4c0e5528 10456 if (!dev_priv->fbdev->fb)
d2dff872
CW
10457 return NULL;
10458
4c0e5528
DV
10459 obj = dev_priv->fbdev->fb->obj;
10460 BUG_ON(!obj);
10461
8bcd4553 10462 fb = &dev_priv->fbdev->fb->base;
01f2c773
VS
10463 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
10464 fb->bits_per_pixel))
d2dff872
CW
10465 return NULL;
10466
01f2c773 10467 if (obj->base.size < mode->vdisplay * fb->pitches[0])
d2dff872
CW
10468 return NULL;
10469
edde3617 10470 drm_framebuffer_reference(fb);
d2dff872 10471 return fb;
4520f53a
DV
10472#else
10473 return NULL;
10474#endif
d2dff872
CW
10475}
10476
d3a40d1b
ACO
10477static int intel_modeset_setup_plane_state(struct drm_atomic_state *state,
10478 struct drm_crtc *crtc,
10479 struct drm_display_mode *mode,
10480 struct drm_framebuffer *fb,
10481 int x, int y)
10482{
10483 struct drm_plane_state *plane_state;
10484 int hdisplay, vdisplay;
10485 int ret;
10486
10487 plane_state = drm_atomic_get_plane_state(state, crtc->primary);
10488 if (IS_ERR(plane_state))
10489 return PTR_ERR(plane_state);
10490
10491 if (mode)
10492 drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
10493 else
10494 hdisplay = vdisplay = 0;
10495
10496 ret = drm_atomic_set_crtc_for_plane(plane_state, fb ? crtc : NULL);
10497 if (ret)
10498 return ret;
10499 drm_atomic_set_fb_for_plane(plane_state, fb);
10500 plane_state->crtc_x = 0;
10501 plane_state->crtc_y = 0;
10502 plane_state->crtc_w = hdisplay;
10503 plane_state->crtc_h = vdisplay;
10504 plane_state->src_x = x << 16;
10505 plane_state->src_y = y << 16;
10506 plane_state->src_w = hdisplay << 16;
10507 plane_state->src_h = vdisplay << 16;
10508
10509 return 0;
10510}
10511
d2434ab7 10512bool intel_get_load_detect_pipe(struct drm_connector *connector,
7173188d 10513 struct drm_display_mode *mode,
51fd371b
RC
10514 struct intel_load_detect_pipe *old,
10515 struct drm_modeset_acquire_ctx *ctx)
79e53945
JB
10516{
10517 struct intel_crtc *intel_crtc;
d2434ab7
DV
10518 struct intel_encoder *intel_encoder =
10519 intel_attached_encoder(connector);
79e53945 10520 struct drm_crtc *possible_crtc;
4ef69c7a 10521 struct drm_encoder *encoder = &intel_encoder->base;
79e53945
JB
10522 struct drm_crtc *crtc = NULL;
10523 struct drm_device *dev = encoder->dev;
94352cf9 10524 struct drm_framebuffer *fb;
51fd371b 10525 struct drm_mode_config *config = &dev->mode_config;
edde3617 10526 struct drm_atomic_state *state = NULL, *restore_state = NULL;
944b0c76 10527 struct drm_connector_state *connector_state;
4be07317 10528 struct intel_crtc_state *crtc_state;
51fd371b 10529 int ret, i = -1;
79e53945 10530
d2dff872 10531 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
c23cc417 10532 connector->base.id, connector->name,
8e329a03 10533 encoder->base.id, encoder->name);
d2dff872 10534
edde3617
ML
10535 old->restore_state = NULL;
10536
51fd371b
RC
10537retry:
10538 ret = drm_modeset_lock(&config->connection_mutex, ctx);
10539 if (ret)
ad3c558f 10540 goto fail;
6e9f798d 10541
79e53945
JB
10542 /*
10543 * Algorithm gets a little messy:
7a5e4805 10544 *
79e53945
JB
10545 * - if the connector already has an assigned crtc, use it (but make
10546 * sure it's on first)
7a5e4805 10547 *
79e53945
JB
10548 * - try to find the first unused crtc that can drive this connector,
10549 * and use that if we find one
79e53945
JB
10550 */
10551
10552 /* See if we already have a CRTC for this connector */
edde3617
ML
10553 if (connector->state->crtc) {
10554 crtc = connector->state->crtc;
8261b191 10555
51fd371b 10556 ret = drm_modeset_lock(&crtc->mutex, ctx);
4d02e2de 10557 if (ret)
ad3c558f 10558 goto fail;
8261b191
CW
10559
10560 /* Make sure the crtc and connector are running */
edde3617 10561 goto found;
79e53945
JB
10562 }
10563
10564 /* Find an unused one (if possible) */
70e1e0ec 10565 for_each_crtc(dev, possible_crtc) {
79e53945
JB
10566 i++;
10567 if (!(encoder->possible_crtcs & (1 << i)))
10568 continue;
edde3617
ML
10569
10570 ret = drm_modeset_lock(&possible_crtc->mutex, ctx);
10571 if (ret)
10572 goto fail;
10573
10574 if (possible_crtc->state->enable) {
10575 drm_modeset_unlock(&possible_crtc->mutex);
a459249c 10576 continue;
edde3617 10577 }
a459249c
VS
10578
10579 crtc = possible_crtc;
10580 break;
79e53945
JB
10581 }
10582
10583 /*
10584 * If we didn't find an unused CRTC, don't use any.
10585 */
10586 if (!crtc) {
7173188d 10587 DRM_DEBUG_KMS("no pipe available for load-detect\n");
ad3c558f 10588 goto fail;
79e53945
JB
10589 }
10590
edde3617
ML
10591found:
10592 intel_crtc = to_intel_crtc(crtc);
10593
4d02e2de
DV
10594 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
10595 if (ret)
ad3c558f 10596 goto fail;
79e53945 10597
83a57153 10598 state = drm_atomic_state_alloc(dev);
edde3617
ML
10599 restore_state = drm_atomic_state_alloc(dev);
10600 if (!state || !restore_state) {
10601 ret = -ENOMEM;
10602 goto fail;
10603 }
83a57153
ACO
10604
10605 state->acquire_ctx = ctx;
edde3617 10606 restore_state->acquire_ctx = ctx;
83a57153 10607
944b0c76
ACO
10608 connector_state = drm_atomic_get_connector_state(state, connector);
10609 if (IS_ERR(connector_state)) {
10610 ret = PTR_ERR(connector_state);
10611 goto fail;
10612 }
10613
edde3617
ML
10614 ret = drm_atomic_set_crtc_for_connector(connector_state, crtc);
10615 if (ret)
10616 goto fail;
944b0c76 10617
4be07317
ACO
10618 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
10619 if (IS_ERR(crtc_state)) {
10620 ret = PTR_ERR(crtc_state);
10621 goto fail;
10622 }
10623
49d6fa21 10624 crtc_state->base.active = crtc_state->base.enable = true;
4be07317 10625
6492711d
CW
10626 if (!mode)
10627 mode = &load_detect_mode;
79e53945 10628
d2dff872
CW
10629 /* We need a framebuffer large enough to accommodate all accesses
10630 * that the plane may generate whilst we perform load detection.
10631 * We can not rely on the fbcon either being present (we get called
10632 * during its initialisation to detect all boot displays, or it may
10633 * not even exist) or that it is large enough to satisfy the
10634 * requested mode.
10635 */
94352cf9
DV
10636 fb = mode_fits_in_fbdev(dev, mode);
10637 if (fb == NULL) {
d2dff872 10638 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
94352cf9 10639 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
d2dff872
CW
10640 } else
10641 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
94352cf9 10642 if (IS_ERR(fb)) {
d2dff872 10643 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
412b61d8 10644 goto fail;
79e53945 10645 }
79e53945 10646
d3a40d1b
ACO
10647 ret = intel_modeset_setup_plane_state(state, crtc, mode, fb, 0, 0);
10648 if (ret)
10649 goto fail;
10650
edde3617
ML
10651 drm_framebuffer_unreference(fb);
10652
10653 ret = drm_atomic_set_mode_for_crtc(&crtc_state->base, mode);
10654 if (ret)
10655 goto fail;
10656
10657 ret = PTR_ERR_OR_ZERO(drm_atomic_get_connector_state(restore_state, connector));
10658 if (!ret)
10659 ret = PTR_ERR_OR_ZERO(drm_atomic_get_crtc_state(restore_state, crtc));
10660 if (!ret)
10661 ret = PTR_ERR_OR_ZERO(drm_atomic_get_plane_state(restore_state, crtc->primary));
10662 if (ret) {
10663 DRM_DEBUG_KMS("Failed to create a copy of old state to restore: %i\n", ret);
10664 goto fail;
10665 }
8c7b5ccb 10666
3ba86073
ML
10667 ret = drm_atomic_commit(state);
10668 if (ret) {
6492711d 10669 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
412b61d8 10670 goto fail;
79e53945 10671 }
edde3617
ML
10672
10673 old->restore_state = restore_state;
7173188d 10674
79e53945 10675 /* let the connector get through one full cycle before testing */
9d0498a2 10676 intel_wait_for_vblank(dev, intel_crtc->pipe);
7173188d 10677 return true;
412b61d8 10678
ad3c558f 10679fail:
e5d958ef 10680 drm_atomic_state_free(state);
edde3617
ML
10681 drm_atomic_state_free(restore_state);
10682 restore_state = state = NULL;
83a57153 10683
51fd371b
RC
10684 if (ret == -EDEADLK) {
10685 drm_modeset_backoff(ctx);
10686 goto retry;
10687 }
10688
412b61d8 10689 return false;
79e53945
JB
10690}
10691
d2434ab7 10692void intel_release_load_detect_pipe(struct drm_connector *connector,
49172fee
ACO
10693 struct intel_load_detect_pipe *old,
10694 struct drm_modeset_acquire_ctx *ctx)
79e53945 10695{
d2434ab7
DV
10696 struct intel_encoder *intel_encoder =
10697 intel_attached_encoder(connector);
4ef69c7a 10698 struct drm_encoder *encoder = &intel_encoder->base;
edde3617 10699 struct drm_atomic_state *state = old->restore_state;
d3a40d1b 10700 int ret;
79e53945 10701
d2dff872 10702 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
c23cc417 10703 connector->base.id, connector->name,
8e329a03 10704 encoder->base.id, encoder->name);
d2dff872 10705
edde3617 10706 if (!state)
0622a53c 10707 return;
79e53945 10708
edde3617
ML
10709 ret = drm_atomic_commit(state);
10710 if (ret) {
10711 DRM_DEBUG_KMS("Couldn't release load detect pipe: %i\n", ret);
10712 drm_atomic_state_free(state);
10713 }
79e53945
JB
10714}
10715
da4a1efa 10716static int i9xx_pll_refclk(struct drm_device *dev,
5cec258b 10717 const struct intel_crtc_state *pipe_config)
da4a1efa 10718{
fac5e23e 10719 struct drm_i915_private *dev_priv = to_i915(dev);
da4a1efa
VS
10720 u32 dpll = pipe_config->dpll_hw_state.dpll;
10721
10722 if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
e91e941b 10723 return dev_priv->vbt.lvds_ssc_freq;
da4a1efa
VS
10724 else if (HAS_PCH_SPLIT(dev))
10725 return 120000;
10726 else if (!IS_GEN2(dev))
10727 return 96000;
10728 else
10729 return 48000;
10730}
10731
79e53945 10732/* Returns the clock of the currently programmed mode of the given pipe. */
f1f644dc 10733static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 10734 struct intel_crtc_state *pipe_config)
79e53945 10735{
f1f644dc 10736 struct drm_device *dev = crtc->base.dev;
fac5e23e 10737 struct drm_i915_private *dev_priv = to_i915(dev);
f1f644dc 10738 int pipe = pipe_config->cpu_transcoder;
293623f7 10739 u32 dpll = pipe_config->dpll_hw_state.dpll;
79e53945 10740 u32 fp;
9e2c8475 10741 struct dpll clock;
dccbea3b 10742 int port_clock;
da4a1efa 10743 int refclk = i9xx_pll_refclk(dev, pipe_config);
79e53945
JB
10744
10745 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
293623f7 10746 fp = pipe_config->dpll_hw_state.fp0;
79e53945 10747 else
293623f7 10748 fp = pipe_config->dpll_hw_state.fp1;
79e53945
JB
10749
10750 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
f2b115e6
AJ
10751 if (IS_PINEVIEW(dev)) {
10752 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
10753 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
2177832f
SL
10754 } else {
10755 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
10756 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
10757 }
10758
a6c45cf0 10759 if (!IS_GEN2(dev)) {
f2b115e6
AJ
10760 if (IS_PINEVIEW(dev))
10761 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
10762 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
2177832f
SL
10763 else
10764 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
79e53945
JB
10765 DPLL_FPA01_P1_POST_DIV_SHIFT);
10766
10767 switch (dpll & DPLL_MODE_MASK) {
10768 case DPLLB_MODE_DAC_SERIAL:
10769 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
10770 5 : 10;
10771 break;
10772 case DPLLB_MODE_LVDS:
10773 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
10774 7 : 14;
10775 break;
10776 default:
28c97730 10777 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
79e53945 10778 "mode\n", (int)(dpll & DPLL_MODE_MASK));
f1f644dc 10779 return;
79e53945
JB
10780 }
10781
ac58c3f0 10782 if (IS_PINEVIEW(dev))
dccbea3b 10783 port_clock = pnv_calc_dpll_params(refclk, &clock);
ac58c3f0 10784 else
dccbea3b 10785 port_clock = i9xx_calc_dpll_params(refclk, &clock);
79e53945 10786 } else {
0fb58223 10787 u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
b1c560d1 10788 bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
79e53945
JB
10789
10790 if (is_lvds) {
10791 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
10792 DPLL_FPA01_P1_POST_DIV_SHIFT);
b1c560d1
VS
10793
10794 if (lvds & LVDS_CLKB_POWER_UP)
10795 clock.p2 = 7;
10796 else
10797 clock.p2 = 14;
79e53945
JB
10798 } else {
10799 if (dpll & PLL_P1_DIVIDE_BY_TWO)
10800 clock.p1 = 2;
10801 else {
10802 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
10803 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
10804 }
10805 if (dpll & PLL_P2_DIVIDE_BY_4)
10806 clock.p2 = 4;
10807 else
10808 clock.p2 = 2;
79e53945 10809 }
da4a1efa 10810
dccbea3b 10811 port_clock = i9xx_calc_dpll_params(refclk, &clock);
79e53945
JB
10812 }
10813
18442d08
VS
10814 /*
10815 * This value includes pixel_multiplier. We will use
241bfc38 10816 * port_clock to compute adjusted_mode.crtc_clock in the
18442d08
VS
10817 * encoder's get_config() function.
10818 */
dccbea3b 10819 pipe_config->port_clock = port_clock;
f1f644dc
JB
10820}
10821
6878da05
VS
10822int intel_dotclock_calculate(int link_freq,
10823 const struct intel_link_m_n *m_n)
f1f644dc 10824{
f1f644dc
JB
10825 /*
10826 * The calculation for the data clock is:
1041a02f 10827 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
f1f644dc 10828 * But we want to avoid losing precison if possible, so:
1041a02f 10829 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
f1f644dc
JB
10830 *
10831 * and the link clock is simpler:
1041a02f 10832 * link_clock = (m * link_clock) / n
f1f644dc
JB
10833 */
10834
6878da05
VS
10835 if (!m_n->link_n)
10836 return 0;
f1f644dc 10837
6878da05
VS
10838 return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
10839}
f1f644dc 10840
18442d08 10841static void ironlake_pch_clock_get(struct intel_crtc *crtc,
5cec258b 10842 struct intel_crtc_state *pipe_config)
6878da05 10843{
e3b247da 10844 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
79e53945 10845
18442d08
VS
10846 /* read out port_clock from the DPLL */
10847 i9xx_crtc_clock_get(crtc, pipe_config);
f1f644dc 10848
f1f644dc 10849 /*
e3b247da
VS
10850 * In case there is an active pipe without active ports,
10851 * we may need some idea for the dotclock anyway.
10852 * Calculate one based on the FDI configuration.
79e53945 10853 */
2d112de7 10854 pipe_config->base.adjusted_mode.crtc_clock =
21a727b3 10855 intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
18442d08 10856 &pipe_config->fdi_m_n);
79e53945
JB
10857}
10858
10859/** Returns the currently programmed mode of the given pipe. */
10860struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
10861 struct drm_crtc *crtc)
10862{
fac5e23e 10863 struct drm_i915_private *dev_priv = to_i915(dev);
79e53945 10864 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6e3c9717 10865 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
79e53945 10866 struct drm_display_mode *mode;
3f36b937 10867 struct intel_crtc_state *pipe_config;
fe2b8f9d
PZ
10868 int htot = I915_READ(HTOTAL(cpu_transcoder));
10869 int hsync = I915_READ(HSYNC(cpu_transcoder));
10870 int vtot = I915_READ(VTOTAL(cpu_transcoder));
10871 int vsync = I915_READ(VSYNC(cpu_transcoder));
293623f7 10872 enum pipe pipe = intel_crtc->pipe;
79e53945
JB
10873
10874 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
10875 if (!mode)
10876 return NULL;
10877
3f36b937
TU
10878 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
10879 if (!pipe_config) {
10880 kfree(mode);
10881 return NULL;
10882 }
10883
f1f644dc
JB
10884 /*
10885 * Construct a pipe_config sufficient for getting the clock info
10886 * back out of crtc_clock_get.
10887 *
10888 * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
10889 * to use a real value here instead.
10890 */
3f36b937
TU
10891 pipe_config->cpu_transcoder = (enum transcoder) pipe;
10892 pipe_config->pixel_multiplier = 1;
10893 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(pipe));
10894 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(pipe));
10895 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(pipe));
10896 i9xx_crtc_clock_get(intel_crtc, pipe_config);
10897
10898 mode->clock = pipe_config->port_clock / pipe_config->pixel_multiplier;
79e53945
JB
10899 mode->hdisplay = (htot & 0xffff) + 1;
10900 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
10901 mode->hsync_start = (hsync & 0xffff) + 1;
10902 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
10903 mode->vdisplay = (vtot & 0xffff) + 1;
10904 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
10905 mode->vsync_start = (vsync & 0xffff) + 1;
10906 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
10907
10908 drm_mode_set_name(mode);
79e53945 10909
3f36b937
TU
10910 kfree(pipe_config);
10911
79e53945
JB
10912 return mode;
10913}
10914
10915static void intel_crtc_destroy(struct drm_crtc *crtc)
10916{
10917 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
67e77c5a 10918 struct drm_device *dev = crtc->dev;
51cbaf01 10919 struct intel_flip_work *work;
67e77c5a 10920
5e2d7afc 10921 spin_lock_irq(&dev->event_lock);
5a21b665
DV
10922 work = intel_crtc->flip_work;
10923 intel_crtc->flip_work = NULL;
10924 spin_unlock_irq(&dev->event_lock);
67e77c5a 10925
5a21b665 10926 if (work) {
51cbaf01
ML
10927 cancel_work_sync(&work->mmio_work);
10928 cancel_work_sync(&work->unpin_work);
5a21b665 10929 kfree(work);
67e77c5a 10930 }
79e53945
JB
10931
10932 drm_crtc_cleanup(crtc);
67e77c5a 10933
79e53945
JB
10934 kfree(intel_crtc);
10935}
10936
6b95a207
KH
10937static void intel_unpin_work_fn(struct work_struct *__work)
10938{
51cbaf01
ML
10939 struct intel_flip_work *work =
10940 container_of(__work, struct intel_flip_work, unpin_work);
5a21b665
DV
10941 struct intel_crtc *crtc = to_intel_crtc(work->crtc);
10942 struct drm_device *dev = crtc->base.dev;
10943 struct drm_plane *primary = crtc->base.primary;
03f476e1 10944
5a21b665
DV
10945 if (is_mmio_work(work))
10946 flush_work(&work->mmio_work);
03f476e1 10947
5a21b665
DV
10948 mutex_lock(&dev->struct_mutex);
10949 intel_unpin_fb_obj(work->old_fb, primary->state->rotation);
f8c417cd 10950 i915_gem_object_put(work->pending_flip_obj);
5a21b665 10951 mutex_unlock(&dev->struct_mutex);
143f73b3 10952
e8a261ea
CW
10953 i915_gem_request_put(work->flip_queued_req);
10954
5a21b665
DV
10955 intel_frontbuffer_flip_complete(dev, to_intel_plane(primary)->frontbuffer_bit);
10956 intel_fbc_post_update(crtc);
10957 drm_framebuffer_unreference(work->old_fb);
143f73b3 10958
5a21b665
DV
10959 BUG_ON(atomic_read(&crtc->unpin_work_count) == 0);
10960 atomic_dec(&crtc->unpin_work_count);
a6747b73 10961
5a21b665
DV
10962 kfree(work);
10963}
d9e86c0e 10964
5a21b665
DV
10965/* Is 'a' after or equal to 'b'? */
10966static bool g4x_flip_count_after_eq(u32 a, u32 b)
10967{
10968 return !((a - b) & 0x80000000);
10969}
143f73b3 10970
5a21b665
DV
10971static bool __pageflip_finished_cs(struct intel_crtc *crtc,
10972 struct intel_flip_work *work)
10973{
10974 struct drm_device *dev = crtc->base.dev;
fac5e23e 10975 struct drm_i915_private *dev_priv = to_i915(dev);
5a21b665 10976 unsigned reset_counter;
143f73b3 10977
5a21b665
DV
10978 reset_counter = i915_reset_counter(&dev_priv->gpu_error);
10979 if (crtc->reset_counter != reset_counter)
10980 return true;
143f73b3 10981
5a21b665
DV
10982 /*
10983 * The relevant registers doen't exist on pre-ctg.
10984 * As the flip done interrupt doesn't trigger for mmio
10985 * flips on gmch platforms, a flip count check isn't
10986 * really needed there. But since ctg has the registers,
10987 * include it in the check anyway.
10988 */
10989 if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
10990 return true;
b4a98e57 10991
5a21b665
DV
10992 /*
10993 * BDW signals flip done immediately if the plane
10994 * is disabled, even if the plane enable is already
10995 * armed to occur at the next vblank :(
10996 */
f99d7069 10997
5a21b665
DV
10998 /*
10999 * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
11000 * used the same base address. In that case the mmio flip might
11001 * have completed, but the CS hasn't even executed the flip yet.
11002 *
11003 * A flip count check isn't enough as the CS might have updated
11004 * the base address just after start of vblank, but before we
11005 * managed to process the interrupt. This means we'd complete the
11006 * CS flip too soon.
11007 *
11008 * Combining both checks should get us a good enough result. It may
11009 * still happen that the CS flip has been executed, but has not
11010 * yet actually completed. But in case the base address is the same
11011 * anyway, we don't really care.
11012 */
11013 return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
11014 crtc->flip_work->gtt_offset &&
11015 g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_G4X(crtc->pipe)),
11016 crtc->flip_work->flip_count);
11017}
b4a98e57 11018
5a21b665
DV
11019static bool
11020__pageflip_finished_mmio(struct intel_crtc *crtc,
11021 struct intel_flip_work *work)
11022{
11023 /*
11024 * MMIO work completes when vblank is different from
11025 * flip_queued_vblank.
11026 *
11027 * Reset counter value doesn't matter, this is handled by
11028 * i915_wait_request finishing early, so no need to handle
11029 * reset here.
11030 */
11031 return intel_crtc_get_vblank_counter(crtc) != work->flip_queued_vblank;
6b95a207
KH
11032}
11033
51cbaf01
ML
11034
11035static bool pageflip_finished(struct intel_crtc *crtc,
11036 struct intel_flip_work *work)
11037{
11038 if (!atomic_read(&work->pending))
11039 return false;
11040
11041 smp_rmb();
11042
5a21b665
DV
11043 if (is_mmio_work(work))
11044 return __pageflip_finished_mmio(crtc, work);
11045 else
11046 return __pageflip_finished_cs(crtc, work);
11047}
11048
11049void intel_finish_page_flip_cs(struct drm_i915_private *dev_priv, int pipe)
11050{
91c8a326 11051 struct drm_device *dev = &dev_priv->drm;
5a21b665
DV
11052 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
11053 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11054 struct intel_flip_work *work;
11055 unsigned long flags;
11056
11057 /* Ignore early vblank irqs */
11058 if (!crtc)
11059 return;
11060
51cbaf01 11061 /*
5a21b665
DV
11062 * This is called both by irq handlers and the reset code (to complete
11063 * lost pageflips) so needs the full irqsave spinlocks.
51cbaf01 11064 */
5a21b665
DV
11065 spin_lock_irqsave(&dev->event_lock, flags);
11066 work = intel_crtc->flip_work;
11067
11068 if (work != NULL &&
11069 !is_mmio_work(work) &&
11070 pageflip_finished(intel_crtc, work))
11071 page_flip_completed(intel_crtc);
11072
11073 spin_unlock_irqrestore(&dev->event_lock, flags);
75f7f3ec
VS
11074}
11075
51cbaf01 11076void intel_finish_page_flip_mmio(struct drm_i915_private *dev_priv, int pipe)
6b95a207 11077{
91c8a326 11078 struct drm_device *dev = &dev_priv->drm;
5251f04e
ML
11079 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
11080 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
51cbaf01 11081 struct intel_flip_work *work;
6b95a207
KH
11082 unsigned long flags;
11083
5251f04e
ML
11084 /* Ignore early vblank irqs */
11085 if (!crtc)
11086 return;
f326038a
DV
11087
11088 /*
11089 * This is called both by irq handlers and the reset code (to complete
11090 * lost pageflips) so needs the full irqsave spinlocks.
e7d841ca 11091 */
6b95a207 11092 spin_lock_irqsave(&dev->event_lock, flags);
5a21b665 11093 work = intel_crtc->flip_work;
5251f04e 11094
5a21b665
DV
11095 if (work != NULL &&
11096 is_mmio_work(work) &&
11097 pageflip_finished(intel_crtc, work))
11098 page_flip_completed(intel_crtc);
5251f04e 11099
6b95a207
KH
11100 spin_unlock_irqrestore(&dev->event_lock, flags);
11101}
11102
5a21b665
DV
11103static inline void intel_mark_page_flip_active(struct intel_crtc *crtc,
11104 struct intel_flip_work *work)
84c33a64 11105{
5a21b665 11106 work->flip_queued_vblank = intel_crtc_get_vblank_counter(crtc);
84c33a64 11107
5a21b665
DV
11108 /* Ensure that the work item is consistent when activating it ... */
11109 smp_mb__before_atomic();
11110 atomic_set(&work->pending, 1);
11111}
a6747b73 11112
5a21b665
DV
11113static int intel_gen2_queue_flip(struct drm_device *dev,
11114 struct drm_crtc *crtc,
11115 struct drm_framebuffer *fb,
11116 struct drm_i915_gem_object *obj,
11117 struct drm_i915_gem_request *req,
11118 uint32_t flags)
11119{
7e37f889 11120 struct intel_ring *ring = req->ring;
5a21b665
DV
11121 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11122 u32 flip_mask;
11123 int ret;
143f73b3 11124
5a21b665
DV
11125 ret = intel_ring_begin(req, 6);
11126 if (ret)
11127 return ret;
143f73b3 11128
5a21b665
DV
11129 /* Can't queue multiple flips, so wait for the previous
11130 * one to finish before executing the next.
11131 */
11132 if (intel_crtc->plane)
11133 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
11134 else
11135 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
b5321f30
CW
11136 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
11137 intel_ring_emit(ring, MI_NOOP);
11138 intel_ring_emit(ring, MI_DISPLAY_FLIP |
5a21b665 11139 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
b5321f30
CW
11140 intel_ring_emit(ring, fb->pitches[0]);
11141 intel_ring_emit(ring, intel_crtc->flip_work->gtt_offset);
11142 intel_ring_emit(ring, 0); /* aux display base address, unused */
143f73b3 11143
5a21b665
DV
11144 return 0;
11145}
84c33a64 11146
5a21b665
DV
11147static int intel_gen3_queue_flip(struct drm_device *dev,
11148 struct drm_crtc *crtc,
11149 struct drm_framebuffer *fb,
11150 struct drm_i915_gem_object *obj,
11151 struct drm_i915_gem_request *req,
11152 uint32_t flags)
11153{
7e37f889 11154 struct intel_ring *ring = req->ring;
5a21b665
DV
11155 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11156 u32 flip_mask;
11157 int ret;
d55dbd06 11158
5a21b665
DV
11159 ret = intel_ring_begin(req, 6);
11160 if (ret)
11161 return ret;
d55dbd06 11162
5a21b665
DV
11163 if (intel_crtc->plane)
11164 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
11165 else
11166 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
b5321f30
CW
11167 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
11168 intel_ring_emit(ring, MI_NOOP);
11169 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
5a21b665 11170 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
b5321f30
CW
11171 intel_ring_emit(ring, fb->pitches[0]);
11172 intel_ring_emit(ring, intel_crtc->flip_work->gtt_offset);
11173 intel_ring_emit(ring, MI_NOOP);
fd8e058a 11174
5a21b665
DV
11175 return 0;
11176}
84c33a64 11177
5a21b665
DV
11178static int intel_gen4_queue_flip(struct drm_device *dev,
11179 struct drm_crtc *crtc,
11180 struct drm_framebuffer *fb,
11181 struct drm_i915_gem_object *obj,
11182 struct drm_i915_gem_request *req,
11183 uint32_t flags)
11184{
7e37f889 11185 struct intel_ring *ring = req->ring;
fac5e23e 11186 struct drm_i915_private *dev_priv = to_i915(dev);
5a21b665
DV
11187 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11188 uint32_t pf, pipesrc;
11189 int ret;
143f73b3 11190
5a21b665
DV
11191 ret = intel_ring_begin(req, 4);
11192 if (ret)
11193 return ret;
143f73b3 11194
5a21b665
DV
11195 /* i965+ uses the linear or tiled offsets from the
11196 * Display Registers (which do not change across a page-flip)
11197 * so we need only reprogram the base address.
11198 */
b5321f30 11199 intel_ring_emit(ring, MI_DISPLAY_FLIP |
5a21b665 11200 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
b5321f30
CW
11201 intel_ring_emit(ring, fb->pitches[0]);
11202 intel_ring_emit(ring, intel_crtc->flip_work->gtt_offset |
5a21b665
DV
11203 obj->tiling_mode);
11204
11205 /* XXX Enabling the panel-fitter across page-flip is so far
11206 * untested on non-native modes, so ignore it for now.
11207 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
11208 */
11209 pf = 0;
11210 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
b5321f30 11211 intel_ring_emit(ring, pf | pipesrc);
143f73b3 11212
5a21b665 11213 return 0;
8c9f3aaf
JB
11214}
11215
5a21b665
DV
11216static int intel_gen6_queue_flip(struct drm_device *dev,
11217 struct drm_crtc *crtc,
11218 struct drm_framebuffer *fb,
11219 struct drm_i915_gem_object *obj,
11220 struct drm_i915_gem_request *req,
11221 uint32_t flags)
da20eabd 11222{
7e37f889 11223 struct intel_ring *ring = req->ring;
fac5e23e 11224 struct drm_i915_private *dev_priv = to_i915(dev);
5a21b665
DV
11225 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11226 uint32_t pf, pipesrc;
11227 int ret;
d21fbe87 11228
5a21b665
DV
11229 ret = intel_ring_begin(req, 4);
11230 if (ret)
11231 return ret;
92826fcd 11232
b5321f30 11233 intel_ring_emit(ring, MI_DISPLAY_FLIP |
5a21b665 11234 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
b5321f30
CW
11235 intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
11236 intel_ring_emit(ring, intel_crtc->flip_work->gtt_offset);
92826fcd 11237
5a21b665
DV
11238 /* Contrary to the suggestions in the documentation,
11239 * "Enable Panel Fitter" does not seem to be required when page
11240 * flipping with a non-native mode, and worse causes a normal
11241 * modeset to fail.
11242 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
11243 */
11244 pf = 0;
11245 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
b5321f30 11246 intel_ring_emit(ring, pf | pipesrc);
7809e5ae 11247
5a21b665 11248 return 0;
7809e5ae
MR
11249}
11250
5a21b665
DV
11251static int intel_gen7_queue_flip(struct drm_device *dev,
11252 struct drm_crtc *crtc,
11253 struct drm_framebuffer *fb,
11254 struct drm_i915_gem_object *obj,
11255 struct drm_i915_gem_request *req,
11256 uint32_t flags)
d21fbe87 11257{
7e37f889 11258 struct intel_ring *ring = req->ring;
5a21b665
DV
11259 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11260 uint32_t plane_bit = 0;
11261 int len, ret;
d21fbe87 11262
5a21b665
DV
11263 switch (intel_crtc->plane) {
11264 case PLANE_A:
11265 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
11266 break;
11267 case PLANE_B:
11268 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
11269 break;
11270 case PLANE_C:
11271 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
11272 break;
11273 default:
11274 WARN_ONCE(1, "unknown plane in flip command\n");
11275 return -ENODEV;
11276 }
11277
11278 len = 4;
b5321f30 11279 if (req->engine->id == RCS) {
5a21b665
DV
11280 len += 6;
11281 /*
11282 * On Gen 8, SRM is now taking an extra dword to accommodate
11283 * 48bits addresses, and we need a NOOP for the batch size to
11284 * stay even.
11285 */
11286 if (IS_GEN8(dev))
11287 len += 2;
11288 }
11289
11290 /*
11291 * BSpec MI_DISPLAY_FLIP for IVB:
11292 * "The full packet must be contained within the same cache line."
11293 *
11294 * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
11295 * cacheline, if we ever start emitting more commands before
11296 * the MI_DISPLAY_FLIP we may need to first emit everything else,
11297 * then do the cacheline alignment, and finally emit the
11298 * MI_DISPLAY_FLIP.
11299 */
11300 ret = intel_ring_cacheline_align(req);
11301 if (ret)
11302 return ret;
11303
11304 ret = intel_ring_begin(req, len);
11305 if (ret)
11306 return ret;
11307
11308 /* Unmask the flip-done completion message. Note that the bspec says that
11309 * we should do this for both the BCS and RCS, and that we must not unmask
11310 * more than one flip event at any time (or ensure that one flip message
11311 * can be sent by waiting for flip-done prior to queueing new flips).
11312 * Experimentation says that BCS works despite DERRMR masking all
11313 * flip-done completion events and that unmasking all planes at once
11314 * for the RCS also doesn't appear to drop events. Setting the DERRMR
11315 * to zero does lead to lockups within MI_DISPLAY_FLIP.
11316 */
b5321f30
CW
11317 if (req->engine->id == RCS) {
11318 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
11319 intel_ring_emit_reg(ring, DERRMR);
11320 intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
5a21b665
DV
11321 DERRMR_PIPEB_PRI_FLIP_DONE |
11322 DERRMR_PIPEC_PRI_FLIP_DONE));
11323 if (IS_GEN8(dev))
b5321f30 11324 intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8 |
5a21b665
DV
11325 MI_SRM_LRM_GLOBAL_GTT);
11326 else
b5321f30 11327 intel_ring_emit(ring, MI_STORE_REGISTER_MEM |
5a21b665 11328 MI_SRM_LRM_GLOBAL_GTT);
b5321f30
CW
11329 intel_ring_emit_reg(ring, DERRMR);
11330 intel_ring_emit(ring, req->engine->scratch.gtt_offset + 256);
5a21b665 11331 if (IS_GEN8(dev)) {
b5321f30
CW
11332 intel_ring_emit(ring, 0);
11333 intel_ring_emit(ring, MI_NOOP);
5a21b665
DV
11334 }
11335 }
11336
b5321f30
CW
11337 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
11338 intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
11339 intel_ring_emit(ring, intel_crtc->flip_work->gtt_offset);
11340 intel_ring_emit(ring, (MI_NOOP));
5a21b665
DV
11341
11342 return 0;
11343}
11344
11345static bool use_mmio_flip(struct intel_engine_cs *engine,
11346 struct drm_i915_gem_object *obj)
11347{
c37efb99
CW
11348 struct reservation_object *resv;
11349
5a21b665
DV
11350 /*
11351 * This is not being used for older platforms, because
11352 * non-availability of flip done interrupt forces us to use
11353 * CS flips. Older platforms derive flip done using some clever
11354 * tricks involving the flip_pending status bits and vblank irqs.
11355 * So using MMIO flips there would disrupt this mechanism.
11356 */
11357
11358 if (engine == NULL)
11359 return true;
11360
11361 if (INTEL_GEN(engine->i915) < 5)
11362 return false;
11363
11364 if (i915.use_mmio_flip < 0)
11365 return false;
11366 else if (i915.use_mmio_flip > 0)
11367 return true;
11368 else if (i915.enable_execlists)
11369 return true;
c37efb99
CW
11370
11371 resv = i915_gem_object_get_dmabuf_resv(obj);
11372 if (resv && !reservation_object_test_signaled_rcu(resv, false))
5a21b665 11373 return true;
c37efb99 11374
d72d908b
CW
11375 return engine != i915_gem_active_get_engine(&obj->last_write,
11376 &obj->base.dev->struct_mutex);
5a21b665
DV
11377}
11378
11379static void skl_do_mmio_flip(struct intel_crtc *intel_crtc,
11380 unsigned int rotation,
11381 struct intel_flip_work *work)
11382{
11383 struct drm_device *dev = intel_crtc->base.dev;
fac5e23e 11384 struct drm_i915_private *dev_priv = to_i915(dev);
5a21b665
DV
11385 struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
11386 const enum pipe pipe = intel_crtc->pipe;
11387 u32 ctl, stride, tile_height;
11388
11389 ctl = I915_READ(PLANE_CTL(pipe, 0));
11390 ctl &= ~PLANE_CTL_TILED_MASK;
11391 switch (fb->modifier[0]) {
11392 case DRM_FORMAT_MOD_NONE:
11393 break;
11394 case I915_FORMAT_MOD_X_TILED:
11395 ctl |= PLANE_CTL_TILED_X;
11396 break;
11397 case I915_FORMAT_MOD_Y_TILED:
11398 ctl |= PLANE_CTL_TILED_Y;
11399 break;
11400 case I915_FORMAT_MOD_Yf_TILED:
11401 ctl |= PLANE_CTL_TILED_YF;
11402 break;
11403 default:
11404 MISSING_CASE(fb->modifier[0]);
11405 }
11406
11407 /*
11408 * The stride is either expressed as a multiple of 64 bytes chunks for
11409 * linear buffers or in number of tiles for tiled buffers.
11410 */
11411 if (intel_rotation_90_or_270(rotation)) {
11412 /* stride = Surface height in tiles */
11413 tile_height = intel_tile_height(dev_priv, fb->modifier[0], 0);
11414 stride = DIV_ROUND_UP(fb->height, tile_height);
11415 } else {
11416 stride = fb->pitches[0] /
11417 intel_fb_stride_alignment(dev_priv, fb->modifier[0],
11418 fb->pixel_format);
11419 }
11420
11421 /*
11422 * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
11423 * PLANE_SURF updates, the update is then guaranteed to be atomic.
11424 */
11425 I915_WRITE(PLANE_CTL(pipe, 0), ctl);
11426 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
11427
11428 I915_WRITE(PLANE_SURF(pipe, 0), work->gtt_offset);
11429 POSTING_READ(PLANE_SURF(pipe, 0));
11430}
11431
11432static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc,
11433 struct intel_flip_work *work)
11434{
11435 struct drm_device *dev = intel_crtc->base.dev;
fac5e23e 11436 struct drm_i915_private *dev_priv = to_i915(dev);
5a21b665
DV
11437 struct intel_framebuffer *intel_fb =
11438 to_intel_framebuffer(intel_crtc->base.primary->fb);
11439 struct drm_i915_gem_object *obj = intel_fb->obj;
11440 i915_reg_t reg = DSPCNTR(intel_crtc->plane);
11441 u32 dspcntr;
11442
11443 dspcntr = I915_READ(reg);
11444
11445 if (obj->tiling_mode != I915_TILING_NONE)
11446 dspcntr |= DISPPLANE_TILED;
11447 else
11448 dspcntr &= ~DISPPLANE_TILED;
11449
11450 I915_WRITE(reg, dspcntr);
11451
11452 I915_WRITE(DSPSURF(intel_crtc->plane), work->gtt_offset);
11453 POSTING_READ(DSPSURF(intel_crtc->plane));
11454}
11455
11456static void intel_mmio_flip_work_func(struct work_struct *w)
11457{
11458 struct intel_flip_work *work =
11459 container_of(w, struct intel_flip_work, mmio_work);
11460 struct intel_crtc *crtc = to_intel_crtc(work->crtc);
11461 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
11462 struct intel_framebuffer *intel_fb =
11463 to_intel_framebuffer(crtc->base.primary->fb);
11464 struct drm_i915_gem_object *obj = intel_fb->obj;
c37efb99 11465 struct reservation_object *resv;
5a21b665
DV
11466
11467 if (work->flip_queued_req)
776f3236
CW
11468 WARN_ON(i915_wait_request(work->flip_queued_req,
11469 false, NULL,
11470 NO_WAITBOOST));
5a21b665
DV
11471
11472 /* For framebuffer backed by dmabuf, wait for fence */
c37efb99
CW
11473 resv = i915_gem_object_get_dmabuf_resv(obj);
11474 if (resv)
11475 WARN_ON(reservation_object_wait_timeout_rcu(resv, false, false,
5a21b665
DV
11476 MAX_SCHEDULE_TIMEOUT) < 0);
11477
11478 intel_pipe_update_start(crtc);
11479
11480 if (INTEL_GEN(dev_priv) >= 9)
11481 skl_do_mmio_flip(crtc, work->rotation, work);
11482 else
11483 /* use_mmio_flip() retricts MMIO flips to ilk+ */
11484 ilk_do_mmio_flip(crtc, work);
11485
11486 intel_pipe_update_end(crtc, work);
11487}
11488
11489static int intel_default_queue_flip(struct drm_device *dev,
11490 struct drm_crtc *crtc,
11491 struct drm_framebuffer *fb,
11492 struct drm_i915_gem_object *obj,
11493 struct drm_i915_gem_request *req,
11494 uint32_t flags)
11495{
11496 return -ENODEV;
11497}
11498
11499static bool __pageflip_stall_check_cs(struct drm_i915_private *dev_priv,
11500 struct intel_crtc *intel_crtc,
11501 struct intel_flip_work *work)
11502{
11503 u32 addr, vblank;
11504
11505 if (!atomic_read(&work->pending))
11506 return false;
11507
11508 smp_rmb();
11509
11510 vblank = intel_crtc_get_vblank_counter(intel_crtc);
11511 if (work->flip_ready_vblank == 0) {
11512 if (work->flip_queued_req &&
f69a02c9 11513 !i915_gem_request_completed(work->flip_queued_req))
5a21b665
DV
11514 return false;
11515
11516 work->flip_ready_vblank = vblank;
11517 }
11518
11519 if (vblank - work->flip_ready_vblank < 3)
11520 return false;
11521
11522 /* Potential stall - if we see that the flip has happened,
11523 * assume a missed interrupt. */
11524 if (INTEL_GEN(dev_priv) >= 4)
11525 addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
11526 else
11527 addr = I915_READ(DSPADDR(intel_crtc->plane));
11528
11529 /* There is a potential issue here with a false positive after a flip
11530 * to the same address. We could address this by checking for a
11531 * non-incrementing frame counter.
11532 */
11533 return addr == work->gtt_offset;
11534}
11535
11536void intel_check_page_flip(struct drm_i915_private *dev_priv, int pipe)
11537{
91c8a326 11538 struct drm_device *dev = &dev_priv->drm;
5a21b665
DV
11539 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
11540 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11541 struct intel_flip_work *work;
11542
11543 WARN_ON(!in_interrupt());
11544
11545 if (crtc == NULL)
11546 return;
11547
11548 spin_lock(&dev->event_lock);
11549 work = intel_crtc->flip_work;
11550
11551 if (work != NULL && !is_mmio_work(work) &&
11552 __pageflip_stall_check_cs(dev_priv, intel_crtc, work)) {
11553 WARN_ONCE(1,
11554 "Kicking stuck page flip: queued at %d, now %d\n",
11555 work->flip_queued_vblank, intel_crtc_get_vblank_counter(intel_crtc));
11556 page_flip_completed(intel_crtc);
11557 work = NULL;
11558 }
11559
11560 if (work != NULL && !is_mmio_work(work) &&
11561 intel_crtc_get_vblank_counter(intel_crtc) - work->flip_queued_vblank > 1)
11562 intel_queue_rps_boost_for_request(work->flip_queued_req);
11563 spin_unlock(&dev->event_lock);
11564}
11565
11566static int intel_crtc_page_flip(struct drm_crtc *crtc,
11567 struct drm_framebuffer *fb,
11568 struct drm_pending_vblank_event *event,
11569 uint32_t page_flip_flags)
11570{
11571 struct drm_device *dev = crtc->dev;
fac5e23e 11572 struct drm_i915_private *dev_priv = to_i915(dev);
5a21b665
DV
11573 struct drm_framebuffer *old_fb = crtc->primary->fb;
11574 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
11575 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11576 struct drm_plane *primary = crtc->primary;
11577 enum pipe pipe = intel_crtc->pipe;
11578 struct intel_flip_work *work;
11579 struct intel_engine_cs *engine;
11580 bool mmio_flip;
8e637178 11581 struct drm_i915_gem_request *request;
5a21b665
DV
11582 int ret;
11583
11584 /*
11585 * drm_mode_page_flip_ioctl() should already catch this, but double
11586 * check to be safe. In the future we may enable pageflipping from
11587 * a disabled primary plane.
11588 */
11589 if (WARN_ON(intel_fb_obj(old_fb) == NULL))
11590 return -EBUSY;
11591
11592 /* Can't change pixel format via MI display flips. */
11593 if (fb->pixel_format != crtc->primary->fb->pixel_format)
11594 return -EINVAL;
11595
11596 /*
11597 * TILEOFF/LINOFF registers can't be changed via MI display flips.
11598 * Note that pitch changes could also affect these register.
11599 */
11600 if (INTEL_INFO(dev)->gen > 3 &&
11601 (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
11602 fb->pitches[0] != crtc->primary->fb->pitches[0]))
11603 return -EINVAL;
11604
11605 if (i915_terminally_wedged(&dev_priv->gpu_error))
11606 goto out_hang;
11607
11608 work = kzalloc(sizeof(*work), GFP_KERNEL);
11609 if (work == NULL)
11610 return -ENOMEM;
11611
11612 work->event = event;
11613 work->crtc = crtc;
11614 work->old_fb = old_fb;
11615 INIT_WORK(&work->unpin_work, intel_unpin_work_fn);
11616
11617 ret = drm_crtc_vblank_get(crtc);
11618 if (ret)
11619 goto free_work;
11620
11621 /* We borrow the event spin lock for protecting flip_work */
11622 spin_lock_irq(&dev->event_lock);
11623 if (intel_crtc->flip_work) {
11624 /* Before declaring the flip queue wedged, check if
11625 * the hardware completed the operation behind our backs.
11626 */
11627 if (pageflip_finished(intel_crtc, intel_crtc->flip_work)) {
11628 DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
11629 page_flip_completed(intel_crtc);
11630 } else {
11631 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
11632 spin_unlock_irq(&dev->event_lock);
11633
11634 drm_crtc_vblank_put(crtc);
11635 kfree(work);
11636 return -EBUSY;
11637 }
11638 }
11639 intel_crtc->flip_work = work;
11640 spin_unlock_irq(&dev->event_lock);
11641
11642 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
11643 flush_workqueue(dev_priv->wq);
11644
11645 /* Reference the objects for the scheduled work. */
11646 drm_framebuffer_reference(work->old_fb);
5a21b665
DV
11647
11648 crtc->primary->fb = fb;
11649 update_state_fb(crtc->primary);
faf68d92
ML
11650
11651 intel_fbc_pre_update(intel_crtc, intel_crtc->config,
11652 to_intel_plane_state(primary->state));
5a21b665 11653
25dc556a 11654 work->pending_flip_obj = i915_gem_object_get(obj);
5a21b665
DV
11655
11656 ret = i915_mutex_lock_interruptible(dev);
11657 if (ret)
11658 goto cleanup;
11659
11660 intel_crtc->reset_counter = i915_reset_counter(&dev_priv->gpu_error);
11661 if (__i915_reset_in_progress_or_wedged(intel_crtc->reset_counter)) {
11662 ret = -EIO;
11663 goto cleanup;
11664 }
11665
11666 atomic_inc(&intel_crtc->unpin_work_count);
11667
11668 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
11669 work->flip_count = I915_READ(PIPE_FLIPCOUNT_G4X(pipe)) + 1;
11670
11671 if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
11672 engine = &dev_priv->engine[BCS];
11673 if (obj->tiling_mode != intel_fb_obj(work->old_fb)->tiling_mode)
11674 /* vlv: DISPLAY_FLIP fails to change tiling */
11675 engine = NULL;
11676 } else if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
11677 engine = &dev_priv->engine[BCS];
11678 } else if (INTEL_INFO(dev)->gen >= 7) {
d72d908b
CW
11679 engine = i915_gem_active_get_engine(&obj->last_write,
11680 &obj->base.dev->struct_mutex);
5a21b665
DV
11681 if (engine == NULL || engine->id != RCS)
11682 engine = &dev_priv->engine[BCS];
11683 } else {
11684 engine = &dev_priv->engine[RCS];
11685 }
11686
11687 mmio_flip = use_mmio_flip(engine, obj);
11688
5a21b665
DV
11689 ret = intel_pin_and_fence_fb_obj(fb, primary->state->rotation);
11690 if (ret)
11691 goto cleanup_pending;
11692
11693 work->gtt_offset = intel_plane_obj_offset(to_intel_plane(primary),
11694 obj, 0);
11695 work->gtt_offset += intel_crtc->dspaddr_offset;
11696 work->rotation = crtc->primary->state->rotation;
11697
11698 if (mmio_flip) {
11699 INIT_WORK(&work->mmio_work, intel_mmio_flip_work_func);
11700
d72d908b
CW
11701 work->flip_queued_req = i915_gem_active_get(&obj->last_write,
11702 &obj->base.dev->struct_mutex);
5a21b665
DV
11703 schedule_work(&work->mmio_work);
11704 } else {
8e637178
CW
11705 request = i915_gem_request_alloc(engine, engine->last_context);
11706 if (IS_ERR(request)) {
11707 ret = PTR_ERR(request);
11708 goto cleanup_unpin;
11709 }
11710
11711 ret = i915_gem_object_sync(obj, request);
11712 if (ret)
11713 goto cleanup_request;
11714
5a21b665
DV
11715 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, request,
11716 page_flip_flags);
11717 if (ret)
8e637178 11718 goto cleanup_request;
5a21b665
DV
11719
11720 intel_mark_page_flip_active(intel_crtc, work);
11721
8e637178 11722 work->flip_queued_req = i915_gem_request_get(request);
5a21b665
DV
11723 i915_add_request_no_flush(request);
11724 }
11725
11726 i915_gem_track_fb(intel_fb_obj(old_fb), obj,
11727 to_intel_plane(primary)->frontbuffer_bit);
11728 mutex_unlock(&dev->struct_mutex);
11729
11730 intel_frontbuffer_flip_prepare(dev,
11731 to_intel_plane(primary)->frontbuffer_bit);
11732
11733 trace_i915_flip_request(intel_crtc->plane, obj);
11734
11735 return 0;
11736
8e637178
CW
11737cleanup_request:
11738 i915_add_request_no_flush(request);
5a21b665
DV
11739cleanup_unpin:
11740 intel_unpin_fb_obj(fb, crtc->primary->state->rotation);
11741cleanup_pending:
5a21b665
DV
11742 atomic_dec(&intel_crtc->unpin_work_count);
11743 mutex_unlock(&dev->struct_mutex);
11744cleanup:
11745 crtc->primary->fb = old_fb;
11746 update_state_fb(crtc->primary);
11747
34911fd3 11748 i915_gem_object_put_unlocked(obj);
5a21b665
DV
11749 drm_framebuffer_unreference(work->old_fb);
11750
11751 spin_lock_irq(&dev->event_lock);
11752 intel_crtc->flip_work = NULL;
11753 spin_unlock_irq(&dev->event_lock);
11754
11755 drm_crtc_vblank_put(crtc);
11756free_work:
11757 kfree(work);
11758
11759 if (ret == -EIO) {
11760 struct drm_atomic_state *state;
11761 struct drm_plane_state *plane_state;
11762
11763out_hang:
11764 state = drm_atomic_state_alloc(dev);
11765 if (!state)
11766 return -ENOMEM;
11767 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
11768
11769retry:
11770 plane_state = drm_atomic_get_plane_state(state, primary);
11771 ret = PTR_ERR_OR_ZERO(plane_state);
11772 if (!ret) {
11773 drm_atomic_set_fb_for_plane(plane_state, fb);
11774
11775 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
11776 if (!ret)
11777 ret = drm_atomic_commit(state);
11778 }
11779
11780 if (ret == -EDEADLK) {
11781 drm_modeset_backoff(state->acquire_ctx);
11782 drm_atomic_state_clear(state);
11783 goto retry;
11784 }
11785
11786 if (ret)
11787 drm_atomic_state_free(state);
11788
11789 if (ret == 0 && event) {
11790 spin_lock_irq(&dev->event_lock);
11791 drm_crtc_send_vblank_event(crtc, event);
11792 spin_unlock_irq(&dev->event_lock);
11793 }
11794 }
11795 return ret;
11796}
11797
11798
11799/**
11800 * intel_wm_need_update - Check whether watermarks need updating
11801 * @plane: drm plane
11802 * @state: new plane state
11803 *
11804 * Check current plane state versus the new one to determine whether
11805 * watermarks need to be recalculated.
11806 *
11807 * Returns true or false.
11808 */
11809static bool intel_wm_need_update(struct drm_plane *plane,
11810 struct drm_plane_state *state)
11811{
11812 struct intel_plane_state *new = to_intel_plane_state(state);
11813 struct intel_plane_state *cur = to_intel_plane_state(plane->state);
11814
11815 /* Update watermarks on tiling or size changes. */
11816 if (new->visible != cur->visible)
11817 return true;
11818
11819 if (!cur->base.fb || !new->base.fb)
11820 return false;
11821
11822 if (cur->base.fb->modifier[0] != new->base.fb->modifier[0] ||
11823 cur->base.rotation != new->base.rotation ||
11824 drm_rect_width(&new->src) != drm_rect_width(&cur->src) ||
11825 drm_rect_height(&new->src) != drm_rect_height(&cur->src) ||
11826 drm_rect_width(&new->dst) != drm_rect_width(&cur->dst) ||
11827 drm_rect_height(&new->dst) != drm_rect_height(&cur->dst))
11828 return true;
11829
11830 return false;
11831}
11832
11833static bool needs_scaling(struct intel_plane_state *state)
11834{
11835 int src_w = drm_rect_width(&state->src) >> 16;
11836 int src_h = drm_rect_height(&state->src) >> 16;
11837 int dst_w = drm_rect_width(&state->dst);
11838 int dst_h = drm_rect_height(&state->dst);
11839
11840 return (src_w != dst_w || src_h != dst_h);
11841}
d21fbe87 11842
da20eabd
ML
11843int intel_plane_atomic_calc_changes(struct drm_crtc_state *crtc_state,
11844 struct drm_plane_state *plane_state)
11845{
ab1d3a0e 11846 struct intel_crtc_state *pipe_config = to_intel_crtc_state(crtc_state);
da20eabd
ML
11847 struct drm_crtc *crtc = crtc_state->crtc;
11848 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11849 struct drm_plane *plane = plane_state->plane;
11850 struct drm_device *dev = crtc->dev;
ed4a6a7c 11851 struct drm_i915_private *dev_priv = to_i915(dev);
da20eabd
ML
11852 struct intel_plane_state *old_plane_state =
11853 to_intel_plane_state(plane->state);
da20eabd
ML
11854 bool mode_changed = needs_modeset(crtc_state);
11855 bool was_crtc_enabled = crtc->state->active;
11856 bool is_crtc_enabled = crtc_state->active;
da20eabd
ML
11857 bool turn_off, turn_on, visible, was_visible;
11858 struct drm_framebuffer *fb = plane_state->fb;
78108b7c 11859 int ret;
da20eabd 11860
84114990 11861 if (INTEL_GEN(dev) >= 9 && plane->type != DRM_PLANE_TYPE_CURSOR) {
da20eabd
ML
11862 ret = skl_update_scaler_plane(
11863 to_intel_crtc_state(crtc_state),
11864 to_intel_plane_state(plane_state));
11865 if (ret)
11866 return ret;
11867 }
11868
da20eabd
ML
11869 was_visible = old_plane_state->visible;
11870 visible = to_intel_plane_state(plane_state)->visible;
11871
11872 if (!was_crtc_enabled && WARN_ON(was_visible))
11873 was_visible = false;
11874
35c08f43
ML
11875 /*
11876 * Visibility is calculated as if the crtc was on, but
11877 * after scaler setup everything depends on it being off
11878 * when the crtc isn't active.
f818ffea
VS
11879 *
11880 * FIXME this is wrong for watermarks. Watermarks should also
11881 * be computed as if the pipe would be active. Perhaps move
11882 * per-plane wm computation to the .check_plane() hook, and
11883 * only combine the results from all planes in the current place?
35c08f43
ML
11884 */
11885 if (!is_crtc_enabled)
11886 to_intel_plane_state(plane_state)->visible = visible = false;
da20eabd
ML
11887
11888 if (!was_visible && !visible)
11889 return 0;
11890
e8861675
ML
11891 if (fb != old_plane_state->base.fb)
11892 pipe_config->fb_changed = true;
11893
da20eabd
ML
11894 turn_off = was_visible && (!visible || mode_changed);
11895 turn_on = visible && (!was_visible || mode_changed);
11896
72660ce0 11897 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] has [PLANE:%d:%s] with fb %i\n",
78108b7c
VS
11898 intel_crtc->base.base.id,
11899 intel_crtc->base.name,
72660ce0
VS
11900 plane->base.id, plane->name,
11901 fb ? fb->base.id : -1);
da20eabd 11902
72660ce0
VS
11903 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] visible %i -> %i, off %i, on %i, ms %i\n",
11904 plane->base.id, plane->name,
11905 was_visible, visible,
da20eabd
ML
11906 turn_off, turn_on, mode_changed);
11907
caed361d
VS
11908 if (turn_on) {
11909 pipe_config->update_wm_pre = true;
11910
11911 /* must disable cxsr around plane enable/disable */
11912 if (plane->type != DRM_PLANE_TYPE_CURSOR)
11913 pipe_config->disable_cxsr = true;
11914 } else if (turn_off) {
11915 pipe_config->update_wm_post = true;
92826fcd 11916
852eb00d 11917 /* must disable cxsr around plane enable/disable */
e8861675 11918 if (plane->type != DRM_PLANE_TYPE_CURSOR)
ab1d3a0e 11919 pipe_config->disable_cxsr = true;
852eb00d 11920 } else if (intel_wm_need_update(plane, plane_state)) {
caed361d
VS
11921 /* FIXME bollocks */
11922 pipe_config->update_wm_pre = true;
11923 pipe_config->update_wm_post = true;
852eb00d 11924 }
da20eabd 11925
ed4a6a7c 11926 /* Pre-gen9 platforms need two-step watermark updates */
caed361d
VS
11927 if ((pipe_config->update_wm_pre || pipe_config->update_wm_post) &&
11928 INTEL_INFO(dev)->gen < 9 && dev_priv->display.optimize_watermarks)
ed4a6a7c
MR
11929 to_intel_crtc_state(crtc_state)->wm.need_postvbl_update = true;
11930
8be6ca85 11931 if (visible || was_visible)
cd202f69 11932 pipe_config->fb_bits |= to_intel_plane(plane)->frontbuffer_bit;
a9ff8714 11933
31ae71fc
ML
11934 /*
11935 * WaCxSRDisabledForSpriteScaling:ivb
11936 *
11937 * cstate->update_wm was already set above, so this flag will
11938 * take effect when we commit and program watermarks.
11939 */
11940 if (plane->type == DRM_PLANE_TYPE_OVERLAY && IS_IVYBRIDGE(dev) &&
11941 needs_scaling(to_intel_plane_state(plane_state)) &&
11942 !needs_scaling(old_plane_state))
11943 pipe_config->disable_lp_wm = true;
d21fbe87 11944
da20eabd
ML
11945 return 0;
11946}
11947
6d3a1ce7
ML
11948static bool encoders_cloneable(const struct intel_encoder *a,
11949 const struct intel_encoder *b)
11950{
11951 /* masks could be asymmetric, so check both ways */
11952 return a == b || (a->cloneable & (1 << b->type) &&
11953 b->cloneable & (1 << a->type));
11954}
11955
11956static bool check_single_encoder_cloning(struct drm_atomic_state *state,
11957 struct intel_crtc *crtc,
11958 struct intel_encoder *encoder)
11959{
11960 struct intel_encoder *source_encoder;
11961 struct drm_connector *connector;
11962 struct drm_connector_state *connector_state;
11963 int i;
11964
11965 for_each_connector_in_state(state, connector, connector_state, i) {
11966 if (connector_state->crtc != &crtc->base)
11967 continue;
11968
11969 source_encoder =
11970 to_intel_encoder(connector_state->best_encoder);
11971 if (!encoders_cloneable(encoder, source_encoder))
11972 return false;
11973 }
11974
11975 return true;
11976}
11977
6d3a1ce7
ML
11978static int intel_crtc_atomic_check(struct drm_crtc *crtc,
11979 struct drm_crtc_state *crtc_state)
11980{
cf5a15be 11981 struct drm_device *dev = crtc->dev;
fac5e23e 11982 struct drm_i915_private *dev_priv = to_i915(dev);
6d3a1ce7 11983 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
cf5a15be
ML
11984 struct intel_crtc_state *pipe_config =
11985 to_intel_crtc_state(crtc_state);
6d3a1ce7 11986 struct drm_atomic_state *state = crtc_state->state;
4d20cd86 11987 int ret;
6d3a1ce7
ML
11988 bool mode_changed = needs_modeset(crtc_state);
11989
852eb00d 11990 if (mode_changed && !crtc_state->active)
caed361d 11991 pipe_config->update_wm_post = true;
eddfcbcd 11992
ad421372
ML
11993 if (mode_changed && crtc_state->enable &&
11994 dev_priv->display.crtc_compute_clock &&
8106ddbd 11995 !WARN_ON(pipe_config->shared_dpll)) {
ad421372
ML
11996 ret = dev_priv->display.crtc_compute_clock(intel_crtc,
11997 pipe_config);
11998 if (ret)
11999 return ret;
12000 }
12001
82cf435b
LL
12002 if (crtc_state->color_mgmt_changed) {
12003 ret = intel_color_check(crtc, crtc_state);
12004 if (ret)
12005 return ret;
e7852a4b
LL
12006
12007 /*
12008 * Changing color management on Intel hardware is
12009 * handled as part of planes update.
12010 */
12011 crtc_state->planes_changed = true;
82cf435b
LL
12012 }
12013
e435d6e5 12014 ret = 0;
86c8bbbe 12015 if (dev_priv->display.compute_pipe_wm) {
e3bddded 12016 ret = dev_priv->display.compute_pipe_wm(pipe_config);
ed4a6a7c
MR
12017 if (ret) {
12018 DRM_DEBUG_KMS("Target pipe watermarks are invalid\n");
12019 return ret;
12020 }
12021 }
12022
12023 if (dev_priv->display.compute_intermediate_wm &&
12024 !to_intel_atomic_state(state)->skip_intermediate_wm) {
12025 if (WARN_ON(!dev_priv->display.compute_pipe_wm))
12026 return 0;
12027
12028 /*
12029 * Calculate 'intermediate' watermarks that satisfy both the
12030 * old state and the new state. We can program these
12031 * immediately.
12032 */
12033 ret = dev_priv->display.compute_intermediate_wm(crtc->dev,
12034 intel_crtc,
12035 pipe_config);
12036 if (ret) {
12037 DRM_DEBUG_KMS("No valid intermediate pipe watermarks are possible\n");
86c8bbbe 12038 return ret;
ed4a6a7c 12039 }
e3d5457c
VS
12040 } else if (dev_priv->display.compute_intermediate_wm) {
12041 if (HAS_PCH_SPLIT(dev_priv) && INTEL_GEN(dev_priv) < 9)
12042 pipe_config->wm.ilk.intermediate = pipe_config->wm.ilk.optimal;
86c8bbbe
MR
12043 }
12044
e435d6e5
ML
12045 if (INTEL_INFO(dev)->gen >= 9) {
12046 if (mode_changed)
12047 ret = skl_update_scaler_crtc(pipe_config);
12048
12049 if (!ret)
12050 ret = intel_atomic_setup_scalers(dev, intel_crtc,
12051 pipe_config);
12052 }
12053
12054 return ret;
6d3a1ce7
ML
12055}
12056
65b38e0d 12057static const struct drm_crtc_helper_funcs intel_helper_funcs = {
f6e5b160 12058 .mode_set_base_atomic = intel_pipe_set_base_atomic,
5a21b665
DV
12059 .atomic_begin = intel_begin_crtc_commit,
12060 .atomic_flush = intel_finish_crtc_commit,
6d3a1ce7 12061 .atomic_check = intel_crtc_atomic_check,
f6e5b160
CW
12062};
12063
d29b2f9d
ACO
12064static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
12065{
12066 struct intel_connector *connector;
12067
12068 for_each_intel_connector(dev, connector) {
8863dc7f
DV
12069 if (connector->base.state->crtc)
12070 drm_connector_unreference(&connector->base);
12071
d29b2f9d
ACO
12072 if (connector->base.encoder) {
12073 connector->base.state->best_encoder =
12074 connector->base.encoder;
12075 connector->base.state->crtc =
12076 connector->base.encoder->crtc;
8863dc7f
DV
12077
12078 drm_connector_reference(&connector->base);
d29b2f9d
ACO
12079 } else {
12080 connector->base.state->best_encoder = NULL;
12081 connector->base.state->crtc = NULL;
12082 }
12083 }
12084}
12085
050f7aeb 12086static void
eba905b2 12087connected_sink_compute_bpp(struct intel_connector *connector,
5cec258b 12088 struct intel_crtc_state *pipe_config)
050f7aeb
DV
12089{
12090 int bpp = pipe_config->pipe_bpp;
12091
12092 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
12093 connector->base.base.id,
c23cc417 12094 connector->base.name);
050f7aeb
DV
12095
12096 /* Don't use an invalid EDID bpc value */
12097 if (connector->base.display_info.bpc &&
12098 connector->base.display_info.bpc * 3 < bpp) {
12099 DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
12100 bpp, connector->base.display_info.bpc*3);
12101 pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
12102 }
12103
013dd9e0
JN
12104 /* Clamp bpp to default limit on screens without EDID 1.4 */
12105 if (connector->base.display_info.bpc == 0) {
12106 int type = connector->base.connector_type;
12107 int clamp_bpp = 24;
12108
12109 /* Fall back to 18 bpp when DP sink capability is unknown. */
12110 if (type == DRM_MODE_CONNECTOR_DisplayPort ||
12111 type == DRM_MODE_CONNECTOR_eDP)
12112 clamp_bpp = 18;
12113
12114 if (bpp > clamp_bpp) {
12115 DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of %d\n",
12116 bpp, clamp_bpp);
12117 pipe_config->pipe_bpp = clamp_bpp;
12118 }
050f7aeb
DV
12119 }
12120}
12121
4e53c2e0 12122static int
050f7aeb 12123compute_baseline_pipe_bpp(struct intel_crtc *crtc,
5cec258b 12124 struct intel_crtc_state *pipe_config)
4e53c2e0 12125{
050f7aeb 12126 struct drm_device *dev = crtc->base.dev;
1486017f 12127 struct drm_atomic_state *state;
da3ced29
ACO
12128 struct drm_connector *connector;
12129 struct drm_connector_state *connector_state;
1486017f 12130 int bpp, i;
4e53c2e0 12131
666a4537 12132 if ((IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)))
4e53c2e0 12133 bpp = 10*3;
d328c9d7
DV
12134 else if (INTEL_INFO(dev)->gen >= 5)
12135 bpp = 12*3;
12136 else
12137 bpp = 8*3;
12138
4e53c2e0 12139
4e53c2e0
DV
12140 pipe_config->pipe_bpp = bpp;
12141
1486017f
ACO
12142 state = pipe_config->base.state;
12143
4e53c2e0 12144 /* Clamp display bpp to EDID value */
da3ced29
ACO
12145 for_each_connector_in_state(state, connector, connector_state, i) {
12146 if (connector_state->crtc != &crtc->base)
4e53c2e0
DV
12147 continue;
12148
da3ced29
ACO
12149 connected_sink_compute_bpp(to_intel_connector(connector),
12150 pipe_config);
4e53c2e0
DV
12151 }
12152
12153 return bpp;
12154}
12155
644db711
DV
12156static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
12157{
12158 DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
12159 "type: 0x%x flags: 0x%x\n",
1342830c 12160 mode->crtc_clock,
644db711
DV
12161 mode->crtc_hdisplay, mode->crtc_hsync_start,
12162 mode->crtc_hsync_end, mode->crtc_htotal,
12163 mode->crtc_vdisplay, mode->crtc_vsync_start,
12164 mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
12165}
12166
c0b03411 12167static void intel_dump_pipe_config(struct intel_crtc *crtc,
5cec258b 12168 struct intel_crtc_state *pipe_config,
c0b03411
DV
12169 const char *context)
12170{
6a60cd87
CK
12171 struct drm_device *dev = crtc->base.dev;
12172 struct drm_plane *plane;
12173 struct intel_plane *intel_plane;
12174 struct intel_plane_state *state;
12175 struct drm_framebuffer *fb;
12176
78108b7c
VS
12177 DRM_DEBUG_KMS("[CRTC:%d:%s]%s config %p for pipe %c\n",
12178 crtc->base.base.id, crtc->base.name,
6a60cd87 12179 context, pipe_config, pipe_name(crtc->pipe));
c0b03411 12180
da205630 12181 DRM_DEBUG_KMS("cpu_transcoder: %s\n", transcoder_name(pipe_config->cpu_transcoder));
c0b03411
DV
12182 DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
12183 pipe_config->pipe_bpp, pipe_config->dither);
12184 DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
12185 pipe_config->has_pch_encoder,
12186 pipe_config->fdi_lanes,
12187 pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
12188 pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
12189 pipe_config->fdi_m_n.tu);
90a6b7b0 12190 DRM_DEBUG_KMS("dp: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
37a5650b 12191 intel_crtc_has_dp_encoder(pipe_config),
90a6b7b0 12192 pipe_config->lane_count,
eb14cb74
VS
12193 pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
12194 pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
12195 pipe_config->dp_m_n.tu);
b95af8be 12196
90a6b7b0 12197 DRM_DEBUG_KMS("dp: %i, lanes: %i, gmch_m2: %u, gmch_n2: %u, link_m2: %u, link_n2: %u, tu2: %u\n",
37a5650b 12198 intel_crtc_has_dp_encoder(pipe_config),
90a6b7b0 12199 pipe_config->lane_count,
b95af8be
VK
12200 pipe_config->dp_m2_n2.gmch_m,
12201 pipe_config->dp_m2_n2.gmch_n,
12202 pipe_config->dp_m2_n2.link_m,
12203 pipe_config->dp_m2_n2.link_n,
12204 pipe_config->dp_m2_n2.tu);
12205
55072d19
DV
12206 DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
12207 pipe_config->has_audio,
12208 pipe_config->has_infoframe);
12209
c0b03411 12210 DRM_DEBUG_KMS("requested mode:\n");
2d112de7 12211 drm_mode_debug_printmodeline(&pipe_config->base.mode);
c0b03411 12212 DRM_DEBUG_KMS("adjusted mode:\n");
2d112de7
ACO
12213 drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
12214 intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
d71b8d4a 12215 DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
37327abd
VS
12216 DRM_DEBUG_KMS("pipe src size: %dx%d\n",
12217 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
0ec463d3
TU
12218 DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
12219 crtc->num_scalers,
12220 pipe_config->scaler_state.scaler_users,
12221 pipe_config->scaler_state.scaler_id);
c0b03411
DV
12222 DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
12223 pipe_config->gmch_pfit.control,
12224 pipe_config->gmch_pfit.pgm_ratios,
12225 pipe_config->gmch_pfit.lvds_border_bits);
fd4daa9c 12226 DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
c0b03411 12227 pipe_config->pch_pfit.pos,
fd4daa9c
CW
12228 pipe_config->pch_pfit.size,
12229 pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
42db64ef 12230 DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
cf532bb2 12231 DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
6a60cd87 12232
415ff0f6 12233 if (IS_BROXTON(dev)) {
05712c15 12234 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: ebb0: 0x%x, ebb4: 0x%x,"
415ff0f6 12235 "pll0: 0x%x, pll1: 0x%x, pll2: 0x%x, pll3: 0x%x, "
c8453338 12236 "pll6: 0x%x, pll8: 0x%x, pll9: 0x%x, pll10: 0x%x, pcsdw12: 0x%x\n",
415ff0f6
TU
12237 pipe_config->ddi_pll_sel,
12238 pipe_config->dpll_hw_state.ebb0,
05712c15 12239 pipe_config->dpll_hw_state.ebb4,
415ff0f6
TU
12240 pipe_config->dpll_hw_state.pll0,
12241 pipe_config->dpll_hw_state.pll1,
12242 pipe_config->dpll_hw_state.pll2,
12243 pipe_config->dpll_hw_state.pll3,
12244 pipe_config->dpll_hw_state.pll6,
12245 pipe_config->dpll_hw_state.pll8,
05712c15 12246 pipe_config->dpll_hw_state.pll9,
c8453338 12247 pipe_config->dpll_hw_state.pll10,
415ff0f6 12248 pipe_config->dpll_hw_state.pcsdw12);
ef11bdb3 12249 } else if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) {
415ff0f6
TU
12250 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: "
12251 "ctrl1: 0x%x, cfgcr1: 0x%x, cfgcr2: 0x%x\n",
12252 pipe_config->ddi_pll_sel,
12253 pipe_config->dpll_hw_state.ctrl1,
12254 pipe_config->dpll_hw_state.cfgcr1,
12255 pipe_config->dpll_hw_state.cfgcr2);
12256 } else if (HAS_DDI(dev)) {
1260f07e 12257 DRM_DEBUG_KMS("ddi_pll_sel: 0x%x; dpll_hw_state: wrpll: 0x%x spll: 0x%x\n",
415ff0f6 12258 pipe_config->ddi_pll_sel,
00490c22
ML
12259 pipe_config->dpll_hw_state.wrpll,
12260 pipe_config->dpll_hw_state.spll);
415ff0f6
TU
12261 } else {
12262 DRM_DEBUG_KMS("dpll_hw_state: dpll: 0x%x, dpll_md: 0x%x, "
12263 "fp0: 0x%x, fp1: 0x%x\n",
12264 pipe_config->dpll_hw_state.dpll,
12265 pipe_config->dpll_hw_state.dpll_md,
12266 pipe_config->dpll_hw_state.fp0,
12267 pipe_config->dpll_hw_state.fp1);
12268 }
12269
6a60cd87
CK
12270 DRM_DEBUG_KMS("planes on this crtc\n");
12271 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
12272 intel_plane = to_intel_plane(plane);
12273 if (intel_plane->pipe != crtc->pipe)
12274 continue;
12275
12276 state = to_intel_plane_state(plane->state);
12277 fb = state->base.fb;
12278 if (!fb) {
1d577e02
VS
12279 DRM_DEBUG_KMS("[PLANE:%d:%s] disabled, scaler_id = %d\n",
12280 plane->base.id, plane->name, state->scaler_id);
6a60cd87
CK
12281 continue;
12282 }
12283
1d577e02
VS
12284 DRM_DEBUG_KMS("[PLANE:%d:%s] enabled",
12285 plane->base.id, plane->name);
12286 DRM_DEBUG_KMS("\tFB:%d, fb = %ux%u format = %s",
12287 fb->base.id, fb->width, fb->height,
12288 drm_get_format_name(fb->pixel_format));
12289 DRM_DEBUG_KMS("\tscaler:%d src %dx%d+%d+%d dst %dx%d+%d+%d\n",
12290 state->scaler_id,
12291 state->src.x1 >> 16, state->src.y1 >> 16,
12292 drm_rect_width(&state->src) >> 16,
12293 drm_rect_height(&state->src) >> 16,
12294 state->dst.x1, state->dst.y1,
12295 drm_rect_width(&state->dst),
12296 drm_rect_height(&state->dst));
6a60cd87 12297 }
c0b03411
DV
12298}
12299
5448a00d 12300static bool check_digital_port_conflicts(struct drm_atomic_state *state)
00f0b378 12301{
5448a00d 12302 struct drm_device *dev = state->dev;
da3ced29 12303 struct drm_connector *connector;
00f0b378 12304 unsigned int used_ports = 0;
477321e0 12305 unsigned int used_mst_ports = 0;
00f0b378
VS
12306
12307 /*
12308 * Walk the connector list instead of the encoder
12309 * list to detect the problem on ddi platforms
12310 * where there's just one encoder per digital port.
12311 */
0bff4858
VS
12312 drm_for_each_connector(connector, dev) {
12313 struct drm_connector_state *connector_state;
12314 struct intel_encoder *encoder;
12315
12316 connector_state = drm_atomic_get_existing_connector_state(state, connector);
12317 if (!connector_state)
12318 connector_state = connector->state;
12319
5448a00d 12320 if (!connector_state->best_encoder)
00f0b378
VS
12321 continue;
12322
5448a00d
ACO
12323 encoder = to_intel_encoder(connector_state->best_encoder);
12324
12325 WARN_ON(!connector_state->crtc);
00f0b378
VS
12326
12327 switch (encoder->type) {
12328 unsigned int port_mask;
12329 case INTEL_OUTPUT_UNKNOWN:
12330 if (WARN_ON(!HAS_DDI(dev)))
12331 break;
cca0502b 12332 case INTEL_OUTPUT_DP:
00f0b378
VS
12333 case INTEL_OUTPUT_HDMI:
12334 case INTEL_OUTPUT_EDP:
12335 port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
12336
12337 /* the same port mustn't appear more than once */
12338 if (used_ports & port_mask)
12339 return false;
12340
12341 used_ports |= port_mask;
477321e0
VS
12342 break;
12343 case INTEL_OUTPUT_DP_MST:
12344 used_mst_ports |=
12345 1 << enc_to_mst(&encoder->base)->primary->port;
12346 break;
00f0b378
VS
12347 default:
12348 break;
12349 }
12350 }
12351
477321e0
VS
12352 /* can't mix MST and SST/HDMI on the same port */
12353 if (used_ports & used_mst_ports)
12354 return false;
12355
00f0b378
VS
12356 return true;
12357}
12358
83a57153
ACO
12359static void
12360clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
12361{
12362 struct drm_crtc_state tmp_state;
663a3640 12363 struct intel_crtc_scaler_state scaler_state;
4978cc93 12364 struct intel_dpll_hw_state dpll_hw_state;
8106ddbd 12365 struct intel_shared_dpll *shared_dpll;
8504c74c 12366 uint32_t ddi_pll_sel;
c4e2d043 12367 bool force_thru;
83a57153 12368
7546a384
ACO
12369 /* FIXME: before the switch to atomic started, a new pipe_config was
12370 * kzalloc'd. Code that depends on any field being zero should be
12371 * fixed, so that the crtc_state can be safely duplicated. For now,
12372 * only fields that are know to not cause problems are preserved. */
12373
83a57153 12374 tmp_state = crtc_state->base;
663a3640 12375 scaler_state = crtc_state->scaler_state;
4978cc93
ACO
12376 shared_dpll = crtc_state->shared_dpll;
12377 dpll_hw_state = crtc_state->dpll_hw_state;
8504c74c 12378 ddi_pll_sel = crtc_state->ddi_pll_sel;
c4e2d043 12379 force_thru = crtc_state->pch_pfit.force_thru;
4978cc93 12380
83a57153 12381 memset(crtc_state, 0, sizeof *crtc_state);
4978cc93 12382
83a57153 12383 crtc_state->base = tmp_state;
663a3640 12384 crtc_state->scaler_state = scaler_state;
4978cc93
ACO
12385 crtc_state->shared_dpll = shared_dpll;
12386 crtc_state->dpll_hw_state = dpll_hw_state;
8504c74c 12387 crtc_state->ddi_pll_sel = ddi_pll_sel;
c4e2d043 12388 crtc_state->pch_pfit.force_thru = force_thru;
83a57153
ACO
12389}
12390
548ee15b 12391static int
b8cecdf5 12392intel_modeset_pipe_config(struct drm_crtc *crtc,
b359283a 12393 struct intel_crtc_state *pipe_config)
ee7b9f93 12394{
b359283a 12395 struct drm_atomic_state *state = pipe_config->base.state;
7758a113 12396 struct intel_encoder *encoder;
da3ced29 12397 struct drm_connector *connector;
0b901879 12398 struct drm_connector_state *connector_state;
d328c9d7 12399 int base_bpp, ret = -EINVAL;
0b901879 12400 int i;
e29c22c0 12401 bool retry = true;
ee7b9f93 12402
83a57153 12403 clear_intel_crtc_state(pipe_config);
7758a113 12404
e143a21c
DV
12405 pipe_config->cpu_transcoder =
12406 (enum transcoder) to_intel_crtc(crtc)->pipe;
b8cecdf5 12407
2960bc9c
ID
12408 /*
12409 * Sanitize sync polarity flags based on requested ones. If neither
12410 * positive or negative polarity is requested, treat this as meaning
12411 * negative polarity.
12412 */
2d112de7 12413 if (!(pipe_config->base.adjusted_mode.flags &
2960bc9c 12414 (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
2d112de7 12415 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
2960bc9c 12416
2d112de7 12417 if (!(pipe_config->base.adjusted_mode.flags &
2960bc9c 12418 (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
2d112de7 12419 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
2960bc9c 12420
d328c9d7
DV
12421 base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
12422 pipe_config);
12423 if (base_bpp < 0)
4e53c2e0
DV
12424 goto fail;
12425
e41a56be
VS
12426 /*
12427 * Determine the real pipe dimensions. Note that stereo modes can
12428 * increase the actual pipe size due to the frame doubling and
12429 * insertion of additional space for blanks between the frame. This
12430 * is stored in the crtc timings. We use the requested mode to do this
12431 * computation to clearly distinguish it from the adjusted mode, which
12432 * can be changed by the connectors in the below retry loop.
12433 */
2d112de7 12434 drm_crtc_get_hv_timing(&pipe_config->base.mode,
ecb7e16b
GP
12435 &pipe_config->pipe_src_w,
12436 &pipe_config->pipe_src_h);
e41a56be 12437
253c84c8
VS
12438 for_each_connector_in_state(state, connector, connector_state, i) {
12439 if (connector_state->crtc != crtc)
12440 continue;
12441
12442 encoder = to_intel_encoder(connector_state->best_encoder);
12443
e25148d0
VS
12444 if (!check_single_encoder_cloning(state, to_intel_crtc(crtc), encoder)) {
12445 DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
12446 goto fail;
12447 }
12448
253c84c8
VS
12449 /*
12450 * Determine output_types before calling the .compute_config()
12451 * hooks so that the hooks can use this information safely.
12452 */
12453 pipe_config->output_types |= 1 << encoder->type;
12454 }
12455
e29c22c0 12456encoder_retry:
ef1b460d 12457 /* Ensure the port clock defaults are reset when retrying. */
ff9a6750 12458 pipe_config->port_clock = 0;
ef1b460d 12459 pipe_config->pixel_multiplier = 1;
ff9a6750 12460
135c81b8 12461 /* Fill in default crtc timings, allow encoders to overwrite them. */
2d112de7
ACO
12462 drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
12463 CRTC_STEREO_DOUBLE);
135c81b8 12464
7758a113
DV
12465 /* Pass our mode to the connectors and the CRTC to give them a chance to
12466 * adjust it according to limitations or connector properties, and also
12467 * a chance to reject the mode entirely.
47f1c6c9 12468 */
da3ced29 12469 for_each_connector_in_state(state, connector, connector_state, i) {
0b901879 12470 if (connector_state->crtc != crtc)
7758a113 12471 continue;
7ae89233 12472
0b901879
ACO
12473 encoder = to_intel_encoder(connector_state->best_encoder);
12474
efea6e8e
DV
12475 if (!(encoder->compute_config(encoder, pipe_config))) {
12476 DRM_DEBUG_KMS("Encoder config failure\n");
7758a113
DV
12477 goto fail;
12478 }
ee7b9f93 12479 }
47f1c6c9 12480
ff9a6750
DV
12481 /* Set default port clock if not overwritten by the encoder. Needs to be
12482 * done afterwards in case the encoder adjusts the mode. */
12483 if (!pipe_config->port_clock)
2d112de7 12484 pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
241bfc38 12485 * pipe_config->pixel_multiplier;
ff9a6750 12486
a43f6e0f 12487 ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
e29c22c0 12488 if (ret < 0) {
7758a113
DV
12489 DRM_DEBUG_KMS("CRTC fixup failed\n");
12490 goto fail;
ee7b9f93 12491 }
e29c22c0
DV
12492
12493 if (ret == RETRY) {
12494 if (WARN(!retry, "loop in pipe configuration computation\n")) {
12495 ret = -EINVAL;
12496 goto fail;
12497 }
12498
12499 DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
12500 retry = false;
12501 goto encoder_retry;
12502 }
12503
e8fa4270
DV
12504 /* Dithering seems to not pass-through bits correctly when it should, so
12505 * only enable it on 6bpc panels. */
12506 pipe_config->dither = pipe_config->pipe_bpp == 6*3;
62f0ace5 12507 DRM_DEBUG_KMS("hw max bpp: %i, pipe bpp: %i, dithering: %i\n",
d328c9d7 12508 base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
4e53c2e0 12509
7758a113 12510fail:
548ee15b 12511 return ret;
ee7b9f93 12512}
47f1c6c9 12513
ea9d758d 12514static void
4740b0f2 12515intel_modeset_update_crtc_state(struct drm_atomic_state *state)
ea9d758d 12516{
0a9ab303
ACO
12517 struct drm_crtc *crtc;
12518 struct drm_crtc_state *crtc_state;
8a75d157 12519 int i;
ea9d758d 12520
7668851f 12521 /* Double check state. */
8a75d157 12522 for_each_crtc_in_state(state, crtc, crtc_state, i) {
3cb480bc 12523 to_intel_crtc(crtc)->config = to_intel_crtc_state(crtc->state);
fc467a22
ML
12524
12525 /* Update hwmode for vblank functions */
12526 if (crtc->state->active)
12527 crtc->hwmode = crtc->state->adjusted_mode;
12528 else
12529 crtc->hwmode.crtc_clock = 0;
61067a5e
ML
12530
12531 /*
12532 * Update legacy state to satisfy fbc code. This can
12533 * be removed when fbc uses the atomic state.
12534 */
12535 if (drm_atomic_get_existing_plane_state(state, crtc->primary)) {
12536 struct drm_plane_state *plane_state = crtc->primary->state;
12537
12538 crtc->primary->fb = plane_state->fb;
12539 crtc->x = plane_state->src_x >> 16;
12540 crtc->y = plane_state->src_y >> 16;
12541 }
ea9d758d 12542 }
ea9d758d
DV
12543}
12544
3bd26263 12545static bool intel_fuzzy_clock_check(int clock1, int clock2)
f1f644dc 12546{
3bd26263 12547 int diff;
f1f644dc
JB
12548
12549 if (clock1 == clock2)
12550 return true;
12551
12552 if (!clock1 || !clock2)
12553 return false;
12554
12555 diff = abs(clock1 - clock2);
12556
12557 if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
12558 return true;
12559
12560 return false;
12561}
12562
25c5b266
DV
12563#define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
12564 list_for_each_entry((intel_crtc), \
12565 &(dev)->mode_config.crtc_list, \
12566 base.head) \
95150bdf 12567 for_each_if (mask & (1 <<(intel_crtc)->pipe))
25c5b266 12568
cfb23ed6
ML
12569static bool
12570intel_compare_m_n(unsigned int m, unsigned int n,
12571 unsigned int m2, unsigned int n2,
12572 bool exact)
12573{
12574 if (m == m2 && n == n2)
12575 return true;
12576
12577 if (exact || !m || !n || !m2 || !n2)
12578 return false;
12579
12580 BUILD_BUG_ON(DATA_LINK_M_N_MASK > INT_MAX);
12581
31d10b57
ML
12582 if (n > n2) {
12583 while (n > n2) {
cfb23ed6
ML
12584 m2 <<= 1;
12585 n2 <<= 1;
12586 }
31d10b57
ML
12587 } else if (n < n2) {
12588 while (n < n2) {
cfb23ed6
ML
12589 m <<= 1;
12590 n <<= 1;
12591 }
12592 }
12593
31d10b57
ML
12594 if (n != n2)
12595 return false;
12596
12597 return intel_fuzzy_clock_check(m, m2);
cfb23ed6
ML
12598}
12599
12600static bool
12601intel_compare_link_m_n(const struct intel_link_m_n *m_n,
12602 struct intel_link_m_n *m2_n2,
12603 bool adjust)
12604{
12605 if (m_n->tu == m2_n2->tu &&
12606 intel_compare_m_n(m_n->gmch_m, m_n->gmch_n,
12607 m2_n2->gmch_m, m2_n2->gmch_n, !adjust) &&
12608 intel_compare_m_n(m_n->link_m, m_n->link_n,
12609 m2_n2->link_m, m2_n2->link_n, !adjust)) {
12610 if (adjust)
12611 *m2_n2 = *m_n;
12612
12613 return true;
12614 }
12615
12616 return false;
12617}
12618
0e8ffe1b 12619static bool
2fa2fe9a 12620intel_pipe_config_compare(struct drm_device *dev,
5cec258b 12621 struct intel_crtc_state *current_config,
cfb23ed6
ML
12622 struct intel_crtc_state *pipe_config,
12623 bool adjust)
0e8ffe1b 12624{
cfb23ed6
ML
12625 bool ret = true;
12626
12627#define INTEL_ERR_OR_DBG_KMS(fmt, ...) \
12628 do { \
12629 if (!adjust) \
12630 DRM_ERROR(fmt, ##__VA_ARGS__); \
12631 else \
12632 DRM_DEBUG_KMS(fmt, ##__VA_ARGS__); \
12633 } while (0)
12634
66e985c0
DV
12635#define PIPE_CONF_CHECK_X(name) \
12636 if (current_config->name != pipe_config->name) { \
cfb23ed6 12637 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
66e985c0
DV
12638 "(expected 0x%08x, found 0x%08x)\n", \
12639 current_config->name, \
12640 pipe_config->name); \
cfb23ed6 12641 ret = false; \
66e985c0
DV
12642 }
12643
08a24034
DV
12644#define PIPE_CONF_CHECK_I(name) \
12645 if (current_config->name != pipe_config->name) { \
cfb23ed6 12646 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
08a24034
DV
12647 "(expected %i, found %i)\n", \
12648 current_config->name, \
12649 pipe_config->name); \
cfb23ed6
ML
12650 ret = false; \
12651 }
12652
8106ddbd
ACO
12653#define PIPE_CONF_CHECK_P(name) \
12654 if (current_config->name != pipe_config->name) { \
12655 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
12656 "(expected %p, found %p)\n", \
12657 current_config->name, \
12658 pipe_config->name); \
12659 ret = false; \
12660 }
12661
cfb23ed6
ML
12662#define PIPE_CONF_CHECK_M_N(name) \
12663 if (!intel_compare_link_m_n(&current_config->name, \
12664 &pipe_config->name,\
12665 adjust)) { \
12666 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
12667 "(expected tu %i gmch %i/%i link %i/%i, " \
12668 "found tu %i, gmch %i/%i link %i/%i)\n", \
12669 current_config->name.tu, \
12670 current_config->name.gmch_m, \
12671 current_config->name.gmch_n, \
12672 current_config->name.link_m, \
12673 current_config->name.link_n, \
12674 pipe_config->name.tu, \
12675 pipe_config->name.gmch_m, \
12676 pipe_config->name.gmch_n, \
12677 pipe_config->name.link_m, \
12678 pipe_config->name.link_n); \
12679 ret = false; \
12680 }
12681
55c561a7
DV
12682/* This is required for BDW+ where there is only one set of registers for
12683 * switching between high and low RR.
12684 * This macro can be used whenever a comparison has to be made between one
12685 * hw state and multiple sw state variables.
12686 */
cfb23ed6
ML
12687#define PIPE_CONF_CHECK_M_N_ALT(name, alt_name) \
12688 if (!intel_compare_link_m_n(&current_config->name, \
12689 &pipe_config->name, adjust) && \
12690 !intel_compare_link_m_n(&current_config->alt_name, \
12691 &pipe_config->name, adjust)) { \
12692 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
12693 "(expected tu %i gmch %i/%i link %i/%i, " \
12694 "or tu %i gmch %i/%i link %i/%i, " \
12695 "found tu %i, gmch %i/%i link %i/%i)\n", \
12696 current_config->name.tu, \
12697 current_config->name.gmch_m, \
12698 current_config->name.gmch_n, \
12699 current_config->name.link_m, \
12700 current_config->name.link_n, \
12701 current_config->alt_name.tu, \
12702 current_config->alt_name.gmch_m, \
12703 current_config->alt_name.gmch_n, \
12704 current_config->alt_name.link_m, \
12705 current_config->alt_name.link_n, \
12706 pipe_config->name.tu, \
12707 pipe_config->name.gmch_m, \
12708 pipe_config->name.gmch_n, \
12709 pipe_config->name.link_m, \
12710 pipe_config->name.link_n); \
12711 ret = false; \
88adfff1
DV
12712 }
12713
1bd1bd80
DV
12714#define PIPE_CONF_CHECK_FLAGS(name, mask) \
12715 if ((current_config->name ^ pipe_config->name) & (mask)) { \
cfb23ed6 12716 INTEL_ERR_OR_DBG_KMS("mismatch in " #name "(" #mask ") " \
1bd1bd80
DV
12717 "(expected %i, found %i)\n", \
12718 current_config->name & (mask), \
12719 pipe_config->name & (mask)); \
cfb23ed6 12720 ret = false; \
1bd1bd80
DV
12721 }
12722
5e550656
VS
12723#define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
12724 if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
cfb23ed6 12725 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
5e550656
VS
12726 "(expected %i, found %i)\n", \
12727 current_config->name, \
12728 pipe_config->name); \
cfb23ed6 12729 ret = false; \
5e550656
VS
12730 }
12731
bb760063
DV
12732#define PIPE_CONF_QUIRK(quirk) \
12733 ((current_config->quirks | pipe_config->quirks) & (quirk))
12734
eccb140b
DV
12735 PIPE_CONF_CHECK_I(cpu_transcoder);
12736
08a24034
DV
12737 PIPE_CONF_CHECK_I(has_pch_encoder);
12738 PIPE_CONF_CHECK_I(fdi_lanes);
cfb23ed6 12739 PIPE_CONF_CHECK_M_N(fdi_m_n);
08a24034 12740
90a6b7b0 12741 PIPE_CONF_CHECK_I(lane_count);
95a7a2ae 12742 PIPE_CONF_CHECK_X(lane_lat_optim_mask);
b95af8be
VK
12743
12744 if (INTEL_INFO(dev)->gen < 8) {
cfb23ed6
ML
12745 PIPE_CONF_CHECK_M_N(dp_m_n);
12746
cfb23ed6
ML
12747 if (current_config->has_drrs)
12748 PIPE_CONF_CHECK_M_N(dp_m2_n2);
12749 } else
12750 PIPE_CONF_CHECK_M_N_ALT(dp_m_n, dp_m2_n2);
eb14cb74 12751
253c84c8 12752 PIPE_CONF_CHECK_X(output_types);
a65347ba 12753
2d112de7
ACO
12754 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
12755 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
12756 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
12757 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
12758 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
12759 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
1bd1bd80 12760
2d112de7
ACO
12761 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
12762 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
12763 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
12764 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
12765 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
12766 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
1bd1bd80 12767
c93f54cf 12768 PIPE_CONF_CHECK_I(pixel_multiplier);
6897b4b5 12769 PIPE_CONF_CHECK_I(has_hdmi_sink);
b5a9fa09 12770 if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
666a4537 12771 IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev))
b5a9fa09 12772 PIPE_CONF_CHECK_I(limited_color_range);
e43823ec 12773 PIPE_CONF_CHECK_I(has_infoframe);
6c49f241 12774
9ed109a7
DV
12775 PIPE_CONF_CHECK_I(has_audio);
12776
2d112de7 12777 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
1bd1bd80
DV
12778 DRM_MODE_FLAG_INTERLACE);
12779
bb760063 12780 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
2d112de7 12781 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063 12782 DRM_MODE_FLAG_PHSYNC);
2d112de7 12783 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063 12784 DRM_MODE_FLAG_NHSYNC);
2d112de7 12785 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063 12786 DRM_MODE_FLAG_PVSYNC);
2d112de7 12787 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063
DV
12788 DRM_MODE_FLAG_NVSYNC);
12789 }
045ac3b5 12790
333b8ca8 12791 PIPE_CONF_CHECK_X(gmch_pfit.control);
e2ff2d4a
DV
12792 /* pfit ratios are autocomputed by the hw on gen4+ */
12793 if (INTEL_INFO(dev)->gen < 4)
7f7d8dd6 12794 PIPE_CONF_CHECK_X(gmch_pfit.pgm_ratios);
333b8ca8 12795 PIPE_CONF_CHECK_X(gmch_pfit.lvds_border_bits);
9953599b 12796
bfd16b2a
ML
12797 if (!adjust) {
12798 PIPE_CONF_CHECK_I(pipe_src_w);
12799 PIPE_CONF_CHECK_I(pipe_src_h);
12800
12801 PIPE_CONF_CHECK_I(pch_pfit.enabled);
12802 if (current_config->pch_pfit.enabled) {
12803 PIPE_CONF_CHECK_X(pch_pfit.pos);
12804 PIPE_CONF_CHECK_X(pch_pfit.size);
12805 }
2fa2fe9a 12806
7aefe2b5
ML
12807 PIPE_CONF_CHECK_I(scaler_state.scaler_id);
12808 }
a1b2278e 12809
e59150dc
JB
12810 /* BDW+ don't expose a synchronous way to read the state */
12811 if (IS_HASWELL(dev))
12812 PIPE_CONF_CHECK_I(ips_enabled);
42db64ef 12813
282740f7
VS
12814 PIPE_CONF_CHECK_I(double_wide);
12815
26804afd
DV
12816 PIPE_CONF_CHECK_X(ddi_pll_sel);
12817
8106ddbd 12818 PIPE_CONF_CHECK_P(shared_dpll);
66e985c0 12819 PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
8bcc2795 12820 PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
66e985c0
DV
12821 PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
12822 PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
d452c5b6 12823 PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
00490c22 12824 PIPE_CONF_CHECK_X(dpll_hw_state.spll);
3f4cd19f
DL
12825 PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
12826 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
12827 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
c0d43d62 12828
47eacbab
VS
12829 PIPE_CONF_CHECK_X(dsi_pll.ctrl);
12830 PIPE_CONF_CHECK_X(dsi_pll.div);
12831
42571aef
VS
12832 if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
12833 PIPE_CONF_CHECK_I(pipe_bpp);
12834
2d112de7 12835 PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
a9a7e98a 12836 PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
5e550656 12837
66e985c0 12838#undef PIPE_CONF_CHECK_X
08a24034 12839#undef PIPE_CONF_CHECK_I
8106ddbd 12840#undef PIPE_CONF_CHECK_P
1bd1bd80 12841#undef PIPE_CONF_CHECK_FLAGS
5e550656 12842#undef PIPE_CONF_CHECK_CLOCK_FUZZY
bb760063 12843#undef PIPE_CONF_QUIRK
cfb23ed6 12844#undef INTEL_ERR_OR_DBG_KMS
88adfff1 12845
cfb23ed6 12846 return ret;
0e8ffe1b
DV
12847}
12848
e3b247da
VS
12849static void intel_pipe_config_sanity_check(struct drm_i915_private *dev_priv,
12850 const struct intel_crtc_state *pipe_config)
12851{
12852 if (pipe_config->has_pch_encoder) {
21a727b3 12853 int fdi_dotclock = intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
e3b247da
VS
12854 &pipe_config->fdi_m_n);
12855 int dotclock = pipe_config->base.adjusted_mode.crtc_clock;
12856
12857 /*
12858 * FDI already provided one idea for the dotclock.
12859 * Yell if the encoder disagrees.
12860 */
12861 WARN(!intel_fuzzy_clock_check(fdi_dotclock, dotclock),
12862 "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
12863 fdi_dotclock, dotclock);
12864 }
12865}
12866
c0ead703
ML
12867static void verify_wm_state(struct drm_crtc *crtc,
12868 struct drm_crtc_state *new_state)
08db6652 12869{
e7c84544 12870 struct drm_device *dev = crtc->dev;
fac5e23e 12871 struct drm_i915_private *dev_priv = to_i915(dev);
08db6652 12872 struct skl_ddb_allocation hw_ddb, *sw_ddb;
e7c84544
ML
12873 struct skl_ddb_entry *hw_entry, *sw_entry;
12874 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
12875 const enum pipe pipe = intel_crtc->pipe;
08db6652
DL
12876 int plane;
12877
e7c84544 12878 if (INTEL_INFO(dev)->gen < 9 || !new_state->active)
08db6652
DL
12879 return;
12880
12881 skl_ddb_get_hw_state(dev_priv, &hw_ddb);
12882 sw_ddb = &dev_priv->wm.skl_hw.ddb;
12883
e7c84544
ML
12884 /* planes */
12885 for_each_plane(dev_priv, pipe, plane) {
12886 hw_entry = &hw_ddb.plane[pipe][plane];
12887 sw_entry = &sw_ddb->plane[pipe][plane];
08db6652 12888
e7c84544 12889 if (skl_ddb_entry_equal(hw_entry, sw_entry))
08db6652
DL
12890 continue;
12891
e7c84544
ML
12892 DRM_ERROR("mismatch in DDB state pipe %c plane %d "
12893 "(expected (%u,%u), found (%u,%u))\n",
12894 pipe_name(pipe), plane + 1,
12895 sw_entry->start, sw_entry->end,
12896 hw_entry->start, hw_entry->end);
12897 }
08db6652 12898
e7c84544
ML
12899 /* cursor */
12900 hw_entry = &hw_ddb.plane[pipe][PLANE_CURSOR];
12901 sw_entry = &sw_ddb->plane[pipe][PLANE_CURSOR];
08db6652 12902
e7c84544 12903 if (!skl_ddb_entry_equal(hw_entry, sw_entry)) {
08db6652
DL
12904 DRM_ERROR("mismatch in DDB state pipe %c cursor "
12905 "(expected (%u,%u), found (%u,%u))\n",
12906 pipe_name(pipe),
12907 sw_entry->start, sw_entry->end,
12908 hw_entry->start, hw_entry->end);
12909 }
12910}
12911
91d1b4bd 12912static void
c0ead703 12913verify_connector_state(struct drm_device *dev, struct drm_crtc *crtc)
8af6cf88 12914{
35dd3c64 12915 struct drm_connector *connector;
8af6cf88 12916
e7c84544 12917 drm_for_each_connector(connector, dev) {
35dd3c64
ML
12918 struct drm_encoder *encoder = connector->encoder;
12919 struct drm_connector_state *state = connector->state;
ad3c558f 12920
e7c84544
ML
12921 if (state->crtc != crtc)
12922 continue;
12923
5a21b665 12924 intel_connector_verify_state(to_intel_connector(connector));
8af6cf88 12925
ad3c558f 12926 I915_STATE_WARN(state->best_encoder != encoder,
35dd3c64 12927 "connector's atomic encoder doesn't match legacy encoder\n");
8af6cf88 12928 }
91d1b4bd
DV
12929}
12930
12931static void
c0ead703 12932verify_encoder_state(struct drm_device *dev)
91d1b4bd
DV
12933{
12934 struct intel_encoder *encoder;
12935 struct intel_connector *connector;
8af6cf88 12936
b2784e15 12937 for_each_intel_encoder(dev, encoder) {
8af6cf88 12938 bool enabled = false;
4d20cd86 12939 enum pipe pipe;
8af6cf88
DV
12940
12941 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
12942 encoder->base.base.id,
8e329a03 12943 encoder->base.name);
8af6cf88 12944
3a3371ff 12945 for_each_intel_connector(dev, connector) {
4d20cd86 12946 if (connector->base.state->best_encoder != &encoder->base)
8af6cf88
DV
12947 continue;
12948 enabled = true;
ad3c558f
ML
12949
12950 I915_STATE_WARN(connector->base.state->crtc !=
12951 encoder->base.crtc,
12952 "connector's crtc doesn't match encoder crtc\n");
8af6cf88 12953 }
0e32b39c 12954
e2c719b7 12955 I915_STATE_WARN(!!encoder->base.crtc != enabled,
8af6cf88
DV
12956 "encoder's enabled state mismatch "
12957 "(expected %i, found %i)\n",
12958 !!encoder->base.crtc, enabled);
7c60d198
ML
12959
12960 if (!encoder->base.crtc) {
4d20cd86 12961 bool active;
7c60d198 12962
4d20cd86
ML
12963 active = encoder->get_hw_state(encoder, &pipe);
12964 I915_STATE_WARN(active,
12965 "encoder detached but still enabled on pipe %c.\n",
12966 pipe_name(pipe));
7c60d198 12967 }
8af6cf88 12968 }
91d1b4bd
DV
12969}
12970
12971static void
c0ead703
ML
12972verify_crtc_state(struct drm_crtc *crtc,
12973 struct drm_crtc_state *old_crtc_state,
12974 struct drm_crtc_state *new_crtc_state)
91d1b4bd 12975{
e7c84544 12976 struct drm_device *dev = crtc->dev;
fac5e23e 12977 struct drm_i915_private *dev_priv = to_i915(dev);
91d1b4bd 12978 struct intel_encoder *encoder;
e7c84544
ML
12979 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
12980 struct intel_crtc_state *pipe_config, *sw_config;
12981 struct drm_atomic_state *old_state;
12982 bool active;
045ac3b5 12983
e7c84544 12984 old_state = old_crtc_state->state;
ec2dc6a0 12985 __drm_atomic_helper_crtc_destroy_state(old_crtc_state);
e7c84544
ML
12986 pipe_config = to_intel_crtc_state(old_crtc_state);
12987 memset(pipe_config, 0, sizeof(*pipe_config));
12988 pipe_config->base.crtc = crtc;
12989 pipe_config->base.state = old_state;
8af6cf88 12990
78108b7c 12991 DRM_DEBUG_KMS("[CRTC:%d:%s]\n", crtc->base.id, crtc->name);
8af6cf88 12992
e7c84544 12993 active = dev_priv->display.get_pipe_config(intel_crtc, pipe_config);
d62cf62a 12994
e7c84544
ML
12995 /* hw state is inconsistent with the pipe quirk */
12996 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
12997 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
12998 active = new_crtc_state->active;
6c49f241 12999
e7c84544
ML
13000 I915_STATE_WARN(new_crtc_state->active != active,
13001 "crtc active state doesn't match with hw state "
13002 "(expected %i, found %i)\n", new_crtc_state->active, active);
0e8ffe1b 13003
e7c84544
ML
13004 I915_STATE_WARN(intel_crtc->active != new_crtc_state->active,
13005 "transitional active state does not match atomic hw state "
13006 "(expected %i, found %i)\n", new_crtc_state->active, intel_crtc->active);
4d20cd86 13007
e7c84544
ML
13008 for_each_encoder_on_crtc(dev, crtc, encoder) {
13009 enum pipe pipe;
4d20cd86 13010
e7c84544
ML
13011 active = encoder->get_hw_state(encoder, &pipe);
13012 I915_STATE_WARN(active != new_crtc_state->active,
13013 "[ENCODER:%i] active %i with crtc active %i\n",
13014 encoder->base.base.id, active, new_crtc_state->active);
4d20cd86 13015
e7c84544
ML
13016 I915_STATE_WARN(active && intel_crtc->pipe != pipe,
13017 "Encoder connected to wrong pipe %c\n",
13018 pipe_name(pipe));
4d20cd86 13019
253c84c8
VS
13020 if (active) {
13021 pipe_config->output_types |= 1 << encoder->type;
e7c84544 13022 encoder->get_config(encoder, pipe_config);
253c84c8 13023 }
e7c84544 13024 }
53d9f4e9 13025
e7c84544
ML
13026 if (!new_crtc_state->active)
13027 return;
cfb23ed6 13028
e7c84544 13029 intel_pipe_config_sanity_check(dev_priv, pipe_config);
e3b247da 13030
e7c84544
ML
13031 sw_config = to_intel_crtc_state(crtc->state);
13032 if (!intel_pipe_config_compare(dev, sw_config,
13033 pipe_config, false)) {
13034 I915_STATE_WARN(1, "pipe state doesn't match!\n");
13035 intel_dump_pipe_config(intel_crtc, pipe_config,
13036 "[hw state]");
13037 intel_dump_pipe_config(intel_crtc, sw_config,
13038 "[sw state]");
8af6cf88
DV
13039 }
13040}
13041
91d1b4bd 13042static void
c0ead703
ML
13043verify_single_dpll_state(struct drm_i915_private *dev_priv,
13044 struct intel_shared_dpll *pll,
13045 struct drm_crtc *crtc,
13046 struct drm_crtc_state *new_state)
91d1b4bd 13047{
91d1b4bd 13048 struct intel_dpll_hw_state dpll_hw_state;
e7c84544
ML
13049 unsigned crtc_mask;
13050 bool active;
5358901f 13051
e7c84544 13052 memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
5358901f 13053
e7c84544 13054 DRM_DEBUG_KMS("%s\n", pll->name);
5358901f 13055
e7c84544 13056 active = pll->funcs.get_hw_state(dev_priv, pll, &dpll_hw_state);
5358901f 13057
e7c84544
ML
13058 if (!(pll->flags & INTEL_DPLL_ALWAYS_ON)) {
13059 I915_STATE_WARN(!pll->on && pll->active_mask,
13060 "pll in active use but not on in sw tracking\n");
13061 I915_STATE_WARN(pll->on && !pll->active_mask,
13062 "pll is on but not used by any active crtc\n");
13063 I915_STATE_WARN(pll->on != active,
13064 "pll on state mismatch (expected %i, found %i)\n",
13065 pll->on, active);
13066 }
5358901f 13067
e7c84544 13068 if (!crtc) {
2dd66ebd 13069 I915_STATE_WARN(pll->active_mask & ~pll->config.crtc_mask,
e7c84544
ML
13070 "more active pll users than references: %x vs %x\n",
13071 pll->active_mask, pll->config.crtc_mask);
5358901f 13072
e7c84544
ML
13073 return;
13074 }
13075
13076 crtc_mask = 1 << drm_crtc_index(crtc);
13077
13078 if (new_state->active)
13079 I915_STATE_WARN(!(pll->active_mask & crtc_mask),
13080 "pll active mismatch (expected pipe %c in active mask 0x%02x)\n",
13081 pipe_name(drm_crtc_index(crtc)), pll->active_mask);
13082 else
13083 I915_STATE_WARN(pll->active_mask & crtc_mask,
13084 "pll active mismatch (didn't expect pipe %c in active mask 0x%02x)\n",
13085 pipe_name(drm_crtc_index(crtc)), pll->active_mask);
2dd66ebd 13086
e7c84544
ML
13087 I915_STATE_WARN(!(pll->config.crtc_mask & crtc_mask),
13088 "pll enabled crtcs mismatch (expected 0x%x in 0x%02x)\n",
13089 crtc_mask, pll->config.crtc_mask);
66e985c0 13090
e7c84544
ML
13091 I915_STATE_WARN(pll->on && memcmp(&pll->config.hw_state,
13092 &dpll_hw_state,
13093 sizeof(dpll_hw_state)),
13094 "pll hw state mismatch\n");
13095}
13096
13097static void
c0ead703
ML
13098verify_shared_dpll_state(struct drm_device *dev, struct drm_crtc *crtc,
13099 struct drm_crtc_state *old_crtc_state,
13100 struct drm_crtc_state *new_crtc_state)
e7c84544 13101{
fac5e23e 13102 struct drm_i915_private *dev_priv = to_i915(dev);
e7c84544
ML
13103 struct intel_crtc_state *old_state = to_intel_crtc_state(old_crtc_state);
13104 struct intel_crtc_state *new_state = to_intel_crtc_state(new_crtc_state);
13105
13106 if (new_state->shared_dpll)
c0ead703 13107 verify_single_dpll_state(dev_priv, new_state->shared_dpll, crtc, new_crtc_state);
e7c84544
ML
13108
13109 if (old_state->shared_dpll &&
13110 old_state->shared_dpll != new_state->shared_dpll) {
13111 unsigned crtc_mask = 1 << drm_crtc_index(crtc);
13112 struct intel_shared_dpll *pll = old_state->shared_dpll;
13113
13114 I915_STATE_WARN(pll->active_mask & crtc_mask,
13115 "pll active mismatch (didn't expect pipe %c in active mask)\n",
13116 pipe_name(drm_crtc_index(crtc)));
13117 I915_STATE_WARN(pll->config.crtc_mask & crtc_mask,
13118 "pll enabled crtcs mismatch (found %x in enabled mask)\n",
13119 pipe_name(drm_crtc_index(crtc)));
5358901f 13120 }
8af6cf88
DV
13121}
13122
e7c84544 13123static void
c0ead703 13124intel_modeset_verify_crtc(struct drm_crtc *crtc,
e7c84544
ML
13125 struct drm_crtc_state *old_state,
13126 struct drm_crtc_state *new_state)
13127{
5a21b665
DV
13128 if (!needs_modeset(new_state) &&
13129 !to_intel_crtc_state(new_state)->update_pipe)
13130 return;
13131
c0ead703 13132 verify_wm_state(crtc, new_state);
5a21b665 13133 verify_connector_state(crtc->dev, crtc);
c0ead703
ML
13134 verify_crtc_state(crtc, old_state, new_state);
13135 verify_shared_dpll_state(crtc->dev, crtc, old_state, new_state);
e7c84544
ML
13136}
13137
13138static void
c0ead703 13139verify_disabled_dpll_state(struct drm_device *dev)
e7c84544 13140{
fac5e23e 13141 struct drm_i915_private *dev_priv = to_i915(dev);
e7c84544
ML
13142 int i;
13143
13144 for (i = 0; i < dev_priv->num_shared_dpll; i++)
c0ead703 13145 verify_single_dpll_state(dev_priv, &dev_priv->shared_dplls[i], NULL, NULL);
e7c84544
ML
13146}
13147
13148static void
c0ead703 13149intel_modeset_verify_disabled(struct drm_device *dev)
e7c84544 13150{
c0ead703
ML
13151 verify_encoder_state(dev);
13152 verify_connector_state(dev, NULL);
13153 verify_disabled_dpll_state(dev);
e7c84544
ML
13154}
13155
80715b2f
VS
13156static void update_scanline_offset(struct intel_crtc *crtc)
13157{
13158 struct drm_device *dev = crtc->base.dev;
13159
13160 /*
13161 * The scanline counter increments at the leading edge of hsync.
13162 *
13163 * On most platforms it starts counting from vtotal-1 on the
13164 * first active line. That means the scanline counter value is
13165 * always one less than what we would expect. Ie. just after
13166 * start of vblank, which also occurs at start of hsync (on the
13167 * last active line), the scanline counter will read vblank_start-1.
13168 *
13169 * On gen2 the scanline counter starts counting from 1 instead
13170 * of vtotal-1, so we have to subtract one (or rather add vtotal-1
13171 * to keep the value positive), instead of adding one.
13172 *
13173 * On HSW+ the behaviour of the scanline counter depends on the output
13174 * type. For DP ports it behaves like most other platforms, but on HDMI
13175 * there's an extra 1 line difference. So we need to add two instead of
13176 * one to the value.
13177 */
13178 if (IS_GEN2(dev)) {
124abe07 13179 const struct drm_display_mode *adjusted_mode = &crtc->config->base.adjusted_mode;
80715b2f
VS
13180 int vtotal;
13181
124abe07
VS
13182 vtotal = adjusted_mode->crtc_vtotal;
13183 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
80715b2f
VS
13184 vtotal /= 2;
13185
13186 crtc->scanline_offset = vtotal - 1;
13187 } else if (HAS_DDI(dev) &&
2d84d2b3 13188 intel_crtc_has_type(crtc->config, INTEL_OUTPUT_HDMI)) {
80715b2f
VS
13189 crtc->scanline_offset = 2;
13190 } else
13191 crtc->scanline_offset = 1;
13192}
13193
ad421372 13194static void intel_modeset_clear_plls(struct drm_atomic_state *state)
ed6739ef 13195{
225da59b 13196 struct drm_device *dev = state->dev;
ed6739ef 13197 struct drm_i915_private *dev_priv = to_i915(dev);
ad421372 13198 struct intel_shared_dpll_config *shared_dpll = NULL;
0a9ab303
ACO
13199 struct drm_crtc *crtc;
13200 struct drm_crtc_state *crtc_state;
0a9ab303 13201 int i;
ed6739ef
ACO
13202
13203 if (!dev_priv->display.crtc_compute_clock)
ad421372 13204 return;
ed6739ef 13205
0a9ab303 13206 for_each_crtc_in_state(state, crtc, crtc_state, i) {
fb1a38a9 13207 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8106ddbd
ACO
13208 struct intel_shared_dpll *old_dpll =
13209 to_intel_crtc_state(crtc->state)->shared_dpll;
0a9ab303 13210
fb1a38a9 13211 if (!needs_modeset(crtc_state))
225da59b
ACO
13212 continue;
13213
8106ddbd 13214 to_intel_crtc_state(crtc_state)->shared_dpll = NULL;
fb1a38a9 13215
8106ddbd 13216 if (!old_dpll)
fb1a38a9 13217 continue;
0a9ab303 13218
ad421372
ML
13219 if (!shared_dpll)
13220 shared_dpll = intel_atomic_get_shared_dpll_state(state);
ed6739ef 13221
8106ddbd 13222 intel_shared_dpll_config_put(shared_dpll, old_dpll, intel_crtc);
ad421372 13223 }
ed6739ef
ACO
13224}
13225
99d736a2
ML
13226/*
13227 * This implements the workaround described in the "notes" section of the mode
13228 * set sequence documentation. When going from no pipes or single pipe to
13229 * multiple pipes, and planes are enabled after the pipe, we need to wait at
13230 * least 2 vblanks on the first pipe before enabling planes on the second pipe.
13231 */
13232static int haswell_mode_set_planes_workaround(struct drm_atomic_state *state)
13233{
13234 struct drm_crtc_state *crtc_state;
13235 struct intel_crtc *intel_crtc;
13236 struct drm_crtc *crtc;
13237 struct intel_crtc_state *first_crtc_state = NULL;
13238 struct intel_crtc_state *other_crtc_state = NULL;
13239 enum pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
13240 int i;
13241
13242 /* look at all crtc's that are going to be enabled in during modeset */
13243 for_each_crtc_in_state(state, crtc, crtc_state, i) {
13244 intel_crtc = to_intel_crtc(crtc);
13245
13246 if (!crtc_state->active || !needs_modeset(crtc_state))
13247 continue;
13248
13249 if (first_crtc_state) {
13250 other_crtc_state = to_intel_crtc_state(crtc_state);
13251 break;
13252 } else {
13253 first_crtc_state = to_intel_crtc_state(crtc_state);
13254 first_pipe = intel_crtc->pipe;
13255 }
13256 }
13257
13258 /* No workaround needed? */
13259 if (!first_crtc_state)
13260 return 0;
13261
13262 /* w/a possibly needed, check how many crtc's are already enabled. */
13263 for_each_intel_crtc(state->dev, intel_crtc) {
13264 struct intel_crtc_state *pipe_config;
13265
13266 pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
13267 if (IS_ERR(pipe_config))
13268 return PTR_ERR(pipe_config);
13269
13270 pipe_config->hsw_workaround_pipe = INVALID_PIPE;
13271
13272 if (!pipe_config->base.active ||
13273 needs_modeset(&pipe_config->base))
13274 continue;
13275
13276 /* 2 or more enabled crtcs means no need for w/a */
13277 if (enabled_pipe != INVALID_PIPE)
13278 return 0;
13279
13280 enabled_pipe = intel_crtc->pipe;
13281 }
13282
13283 if (enabled_pipe != INVALID_PIPE)
13284 first_crtc_state->hsw_workaround_pipe = enabled_pipe;
13285 else if (other_crtc_state)
13286 other_crtc_state->hsw_workaround_pipe = first_pipe;
13287
13288 return 0;
13289}
13290
27c329ed
ML
13291static int intel_modeset_all_pipes(struct drm_atomic_state *state)
13292{
13293 struct drm_crtc *crtc;
13294 struct drm_crtc_state *crtc_state;
13295 int ret = 0;
13296
13297 /* add all active pipes to the state */
13298 for_each_crtc(state->dev, crtc) {
13299 crtc_state = drm_atomic_get_crtc_state(state, crtc);
13300 if (IS_ERR(crtc_state))
13301 return PTR_ERR(crtc_state);
13302
13303 if (!crtc_state->active || needs_modeset(crtc_state))
13304 continue;
13305
13306 crtc_state->mode_changed = true;
13307
13308 ret = drm_atomic_add_affected_connectors(state, crtc);
13309 if (ret)
13310 break;
13311
13312 ret = drm_atomic_add_affected_planes(state, crtc);
13313 if (ret)
13314 break;
13315 }
13316
13317 return ret;
13318}
13319
c347a676 13320static int intel_modeset_checks(struct drm_atomic_state *state)
054518dd 13321{
565602d7 13322 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
fac5e23e 13323 struct drm_i915_private *dev_priv = to_i915(state->dev);
565602d7
ML
13324 struct drm_crtc *crtc;
13325 struct drm_crtc_state *crtc_state;
13326 int ret = 0, i;
054518dd 13327
b359283a
ML
13328 if (!check_digital_port_conflicts(state)) {
13329 DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
13330 return -EINVAL;
13331 }
13332
565602d7
ML
13333 intel_state->modeset = true;
13334 intel_state->active_crtcs = dev_priv->active_crtcs;
13335
13336 for_each_crtc_in_state(state, crtc, crtc_state, i) {
13337 if (crtc_state->active)
13338 intel_state->active_crtcs |= 1 << i;
13339 else
13340 intel_state->active_crtcs &= ~(1 << i);
8b4a7d05
MR
13341
13342 if (crtc_state->active != crtc->state->active)
13343 intel_state->active_pipe_changes |= drm_crtc_mask(crtc);
565602d7
ML
13344 }
13345
054518dd
ACO
13346 /*
13347 * See if the config requires any additional preparation, e.g.
13348 * to adjust global state with pipes off. We need to do this
13349 * here so we can get the modeset_pipe updated config for the new
13350 * mode set on this crtc. For other crtcs we need to use the
13351 * adjusted_mode bits in the crtc directly.
13352 */
27c329ed 13353 if (dev_priv->display.modeset_calc_cdclk) {
c89e39f3 13354 if (!intel_state->cdclk_pll_vco)
63911d72 13355 intel_state->cdclk_pll_vco = dev_priv->cdclk_pll.vco;
b2045352
VS
13356 if (!intel_state->cdclk_pll_vco)
13357 intel_state->cdclk_pll_vco = dev_priv->skl_preferred_vco_freq;
c89e39f3 13358
27c329ed 13359 ret = dev_priv->display.modeset_calc_cdclk(state);
c89e39f3
CT
13360 if (ret < 0)
13361 return ret;
27c329ed 13362
c89e39f3 13363 if (intel_state->dev_cdclk != dev_priv->cdclk_freq ||
63911d72 13364 intel_state->cdclk_pll_vco != dev_priv->cdclk_pll.vco)
27c329ed
ML
13365 ret = intel_modeset_all_pipes(state);
13366
13367 if (ret < 0)
054518dd 13368 return ret;
e8788cbc
ML
13369
13370 DRM_DEBUG_KMS("New cdclk calculated to be atomic %u, actual %u\n",
13371 intel_state->cdclk, intel_state->dev_cdclk);
27c329ed 13372 } else
1a617b77 13373 to_intel_atomic_state(state)->cdclk = dev_priv->atomic_cdclk_freq;
054518dd 13374
ad421372 13375 intel_modeset_clear_plls(state);
054518dd 13376
565602d7 13377 if (IS_HASWELL(dev_priv))
ad421372 13378 return haswell_mode_set_planes_workaround(state);
99d736a2 13379
ad421372 13380 return 0;
c347a676
ACO
13381}
13382
aa363136
MR
13383/*
13384 * Handle calculation of various watermark data at the end of the atomic check
13385 * phase. The code here should be run after the per-crtc and per-plane 'check'
13386 * handlers to ensure that all derived state has been updated.
13387 */
55994c2c 13388static int calc_watermark_data(struct drm_atomic_state *state)
aa363136
MR
13389{
13390 struct drm_device *dev = state->dev;
98d39494 13391 struct drm_i915_private *dev_priv = to_i915(dev);
98d39494
MR
13392
13393 /* Is there platform-specific watermark information to calculate? */
13394 if (dev_priv->display.compute_global_watermarks)
55994c2c
MR
13395 return dev_priv->display.compute_global_watermarks(state);
13396
13397 return 0;
aa363136
MR
13398}
13399
74c090b1
ML
13400/**
13401 * intel_atomic_check - validate state object
13402 * @dev: drm device
13403 * @state: state to validate
13404 */
13405static int intel_atomic_check(struct drm_device *dev,
13406 struct drm_atomic_state *state)
c347a676 13407{
dd8b3bdb 13408 struct drm_i915_private *dev_priv = to_i915(dev);
aa363136 13409 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
c347a676
ACO
13410 struct drm_crtc *crtc;
13411 struct drm_crtc_state *crtc_state;
13412 int ret, i;
61333b60 13413 bool any_ms = false;
c347a676 13414
74c090b1 13415 ret = drm_atomic_helper_check_modeset(dev, state);
054518dd
ACO
13416 if (ret)
13417 return ret;
13418
c347a676 13419 for_each_crtc_in_state(state, crtc, crtc_state, i) {
cfb23ed6
ML
13420 struct intel_crtc_state *pipe_config =
13421 to_intel_crtc_state(crtc_state);
1ed51de9
DV
13422
13423 /* Catch I915_MODE_FLAG_INHERITED */
13424 if (crtc_state->mode.private_flags != crtc->state->mode.private_flags)
13425 crtc_state->mode_changed = true;
cfb23ed6 13426
af4a879e 13427 if (!needs_modeset(crtc_state))
c347a676
ACO
13428 continue;
13429
af4a879e
DV
13430 if (!crtc_state->enable) {
13431 any_ms = true;
cfb23ed6 13432 continue;
af4a879e 13433 }
cfb23ed6 13434
26495481
DV
13435 /* FIXME: For only active_changed we shouldn't need to do any
13436 * state recomputation at all. */
13437
1ed51de9
DV
13438 ret = drm_atomic_add_affected_connectors(state, crtc);
13439 if (ret)
13440 return ret;
b359283a 13441
cfb23ed6 13442 ret = intel_modeset_pipe_config(crtc, pipe_config);
25aa1c39
ML
13443 if (ret) {
13444 intel_dump_pipe_config(to_intel_crtc(crtc),
13445 pipe_config, "[failed]");
c347a676 13446 return ret;
25aa1c39 13447 }
c347a676 13448
73831236 13449 if (i915.fastboot &&
dd8b3bdb 13450 intel_pipe_config_compare(dev,
cfb23ed6 13451 to_intel_crtc_state(crtc->state),
1ed51de9 13452 pipe_config, true)) {
26495481 13453 crtc_state->mode_changed = false;
bfd16b2a 13454 to_intel_crtc_state(crtc_state)->update_pipe = true;
26495481
DV
13455 }
13456
af4a879e 13457 if (needs_modeset(crtc_state))
26495481 13458 any_ms = true;
cfb23ed6 13459
af4a879e
DV
13460 ret = drm_atomic_add_affected_planes(state, crtc);
13461 if (ret)
13462 return ret;
61333b60 13463
26495481
DV
13464 intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
13465 needs_modeset(crtc_state) ?
13466 "[modeset]" : "[fastset]");
c347a676
ACO
13467 }
13468
61333b60
ML
13469 if (any_ms) {
13470 ret = intel_modeset_checks(state);
13471
13472 if (ret)
13473 return ret;
27c329ed 13474 } else
dd8b3bdb 13475 intel_state->cdclk = dev_priv->cdclk_freq;
76305b1a 13476
dd8b3bdb 13477 ret = drm_atomic_helper_check_planes(dev, state);
aa363136
MR
13478 if (ret)
13479 return ret;
13480
f51be2e0 13481 intel_fbc_choose_crtc(dev_priv, state);
55994c2c 13482 return calc_watermark_data(state);
054518dd
ACO
13483}
13484
5008e874
ML
13485static int intel_atomic_prepare_commit(struct drm_device *dev,
13486 struct drm_atomic_state *state,
81072bfd 13487 bool nonblock)
5008e874 13488{
fac5e23e 13489 struct drm_i915_private *dev_priv = to_i915(dev);
7580d774 13490 struct drm_plane_state *plane_state;
5008e874 13491 struct drm_crtc_state *crtc_state;
7580d774 13492 struct drm_plane *plane;
5008e874
ML
13493 struct drm_crtc *crtc;
13494 int i, ret;
13495
5a21b665
DV
13496 for_each_crtc_in_state(state, crtc, crtc_state, i) {
13497 if (state->legacy_cursor_update)
a6747b73
ML
13498 continue;
13499
5a21b665
DV
13500 ret = intel_crtc_wait_for_pending_flips(crtc);
13501 if (ret)
13502 return ret;
5008e874 13503
5a21b665
DV
13504 if (atomic_read(&to_intel_crtc(crtc)->unpin_work_count) >= 2)
13505 flush_workqueue(dev_priv->wq);
d55dbd06
ML
13506 }
13507
f935675f
ML
13508 ret = mutex_lock_interruptible(&dev->struct_mutex);
13509 if (ret)
13510 return ret;
13511
5008e874 13512 ret = drm_atomic_helper_prepare_planes(dev, state);
f7e5838b 13513 mutex_unlock(&dev->struct_mutex);
7580d774 13514
21daaeee 13515 if (!ret && !nonblock) {
7580d774
ML
13516 for_each_plane_in_state(state, plane, plane_state, i) {
13517 struct intel_plane_state *intel_plane_state =
13518 to_intel_plane_state(plane_state);
13519
13520 if (!intel_plane_state->wait_req)
13521 continue;
13522
776f3236
CW
13523 ret = i915_wait_request(intel_plane_state->wait_req,
13524 true, NULL, NULL);
f7e5838b 13525 if (ret) {
f4457ae7
CW
13526 /* Any hang should be swallowed by the wait */
13527 WARN_ON(ret == -EIO);
f7e5838b
CW
13528 mutex_lock(&dev->struct_mutex);
13529 drm_atomic_helper_cleanup_planes(dev, state);
13530 mutex_unlock(&dev->struct_mutex);
7580d774 13531 break;
f7e5838b 13532 }
7580d774 13533 }
7580d774 13534 }
5008e874
ML
13535
13536 return ret;
13537}
13538
a2991414
ML
13539u32 intel_crtc_get_vblank_counter(struct intel_crtc *crtc)
13540{
13541 struct drm_device *dev = crtc->base.dev;
13542
13543 if (!dev->max_vblank_count)
13544 return drm_accurate_vblank_count(&crtc->base);
13545
13546 return dev->driver->get_vblank_counter(dev, crtc->pipe);
13547}
13548
5a21b665
DV
13549static void intel_atomic_wait_for_vblanks(struct drm_device *dev,
13550 struct drm_i915_private *dev_priv,
13551 unsigned crtc_mask)
e8861675 13552{
5a21b665
DV
13553 unsigned last_vblank_count[I915_MAX_PIPES];
13554 enum pipe pipe;
13555 int ret;
e8861675 13556
5a21b665
DV
13557 if (!crtc_mask)
13558 return;
e8861675 13559
5a21b665
DV
13560 for_each_pipe(dev_priv, pipe) {
13561 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
e8861675 13562
5a21b665 13563 if (!((1 << pipe) & crtc_mask))
e8861675
ML
13564 continue;
13565
5a21b665
DV
13566 ret = drm_crtc_vblank_get(crtc);
13567 if (WARN_ON(ret != 0)) {
13568 crtc_mask &= ~(1 << pipe);
13569 continue;
e8861675
ML
13570 }
13571
5a21b665 13572 last_vblank_count[pipe] = drm_crtc_vblank_count(crtc);
e8861675
ML
13573 }
13574
5a21b665
DV
13575 for_each_pipe(dev_priv, pipe) {
13576 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
13577 long lret;
e8861675 13578
5a21b665
DV
13579 if (!((1 << pipe) & crtc_mask))
13580 continue;
d55dbd06 13581
5a21b665
DV
13582 lret = wait_event_timeout(dev->vblank[pipe].queue,
13583 last_vblank_count[pipe] !=
13584 drm_crtc_vblank_count(crtc),
13585 msecs_to_jiffies(50));
d55dbd06 13586
5a21b665 13587 WARN(!lret, "pipe %c vblank wait timed out\n", pipe_name(pipe));
d55dbd06 13588
5a21b665 13589 drm_crtc_vblank_put(crtc);
d55dbd06
ML
13590 }
13591}
13592
5a21b665 13593static bool needs_vblank_wait(struct intel_crtc_state *crtc_state)
a6747b73 13594{
5a21b665
DV
13595 /* fb updated, need to unpin old fb */
13596 if (crtc_state->fb_changed)
13597 return true;
a6747b73 13598
5a21b665
DV
13599 /* wm changes, need vblank before final wm's */
13600 if (crtc_state->update_wm_post)
13601 return true;
a6747b73 13602
5a21b665
DV
13603 /*
13604 * cxsr is re-enabled after vblank.
13605 * This is already handled by crtc_state->update_wm_post,
13606 * but added for clarity.
13607 */
13608 if (crtc_state->disable_cxsr)
13609 return true;
a6747b73 13610
5a21b665 13611 return false;
e8861675
ML
13612}
13613
94f05024 13614static void intel_atomic_commit_tail(struct drm_atomic_state *state)
a6778b3c 13615{
94f05024 13616 struct drm_device *dev = state->dev;
565602d7 13617 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
fac5e23e 13618 struct drm_i915_private *dev_priv = to_i915(dev);
29ceb0e6 13619 struct drm_crtc_state *old_crtc_state;
7580d774 13620 struct drm_crtc *crtc;
5a21b665 13621 struct intel_crtc_state *intel_cstate;
94f05024
DV
13622 struct drm_plane *plane;
13623 struct drm_plane_state *plane_state;
5a21b665
DV
13624 bool hw_check = intel_state->modeset;
13625 unsigned long put_domains[I915_MAX_PIPES] = {};
13626 unsigned crtc_vblank_mask = 0;
94f05024 13627 int i, ret;
a6778b3c 13628
94f05024
DV
13629 for_each_plane_in_state(state, plane, plane_state, i) {
13630 struct intel_plane_state *intel_plane_state =
13631 to_intel_plane_state(plane_state);
ea0000f0 13632
94f05024
DV
13633 if (!intel_plane_state->wait_req)
13634 continue;
d4afb8cc 13635
776f3236
CW
13636 ret = i915_wait_request(intel_plane_state->wait_req,
13637 true, NULL, NULL);
94f05024
DV
13638 /* EIO should be eaten, and we can't get interrupted in the
13639 * worker, and blocking commits have waited already. */
13640 WARN_ON(ret);
13641 }
1c5e19f8 13642
ea0000f0
DV
13643 drm_atomic_helper_wait_for_dependencies(state);
13644
565602d7
ML
13645 if (intel_state->modeset) {
13646 memcpy(dev_priv->min_pixclk, intel_state->min_pixclk,
13647 sizeof(intel_state->min_pixclk));
13648 dev_priv->active_crtcs = intel_state->active_crtcs;
1a617b77 13649 dev_priv->atomic_cdclk_freq = intel_state->cdclk;
5a21b665
DV
13650
13651 intel_display_power_get(dev_priv, POWER_DOMAIN_MODESET);
565602d7
ML
13652 }
13653
29ceb0e6 13654 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
a539205a
ML
13655 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13656
5a21b665
DV
13657 if (needs_modeset(crtc->state) ||
13658 to_intel_crtc_state(crtc->state)->update_pipe) {
13659 hw_check = true;
13660
13661 put_domains[to_intel_crtc(crtc)->pipe] =
13662 modeset_get_crtc_power_domains(crtc,
13663 to_intel_crtc_state(crtc->state));
13664 }
13665
61333b60
ML
13666 if (!needs_modeset(crtc->state))
13667 continue;
13668
29ceb0e6 13669 intel_pre_plane_update(to_intel_crtc_state(old_crtc_state));
460da916 13670
29ceb0e6
VS
13671 if (old_crtc_state->active) {
13672 intel_crtc_disable_planes(crtc, old_crtc_state->plane_mask);
a539205a 13673 dev_priv->display.crtc_disable(crtc);
eddfcbcd 13674 intel_crtc->active = false;
58f9c0bc 13675 intel_fbc_disable(intel_crtc);
eddfcbcd 13676 intel_disable_shared_dpll(intel_crtc);
9bbc8258
VS
13677
13678 /*
13679 * Underruns don't always raise
13680 * interrupts, so check manually.
13681 */
13682 intel_check_cpu_fifo_underruns(dev_priv);
13683 intel_check_pch_fifo_underruns(dev_priv);
b9001114
ML
13684
13685 if (!crtc->state->active)
13686 intel_update_watermarks(crtc);
a539205a 13687 }
b8cecdf5 13688 }
7758a113 13689
ea9d758d
DV
13690 /* Only after disabling all output pipelines that will be changed can we
13691 * update the the output configuration. */
4740b0f2 13692 intel_modeset_update_crtc_state(state);
f6e5b160 13693
565602d7 13694 if (intel_state->modeset) {
4740b0f2 13695 drm_atomic_helper_update_legacy_modeset_state(state->dev, state);
33c8df89
ML
13696
13697 if (dev_priv->display.modeset_commit_cdclk &&
c89e39f3 13698 (intel_state->dev_cdclk != dev_priv->cdclk_freq ||
63911d72 13699 intel_state->cdclk_pll_vco != dev_priv->cdclk_pll.vco))
33c8df89 13700 dev_priv->display.modeset_commit_cdclk(state);
f6d1973d 13701
c0ead703 13702 intel_modeset_verify_disabled(dev);
4740b0f2 13703 }
47fab737 13704
a6778b3c 13705 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
29ceb0e6 13706 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
f6ac4b2a
ML
13707 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13708 bool modeset = needs_modeset(crtc->state);
5a21b665
DV
13709 struct intel_crtc_state *pipe_config =
13710 to_intel_crtc_state(crtc->state);
9f836f90 13711
f6ac4b2a 13712 if (modeset && crtc->state->active) {
a539205a
ML
13713 update_scanline_offset(to_intel_crtc(crtc));
13714 dev_priv->display.crtc_enable(crtc);
13715 }
80715b2f 13716
1f7528c4
DV
13717 /* Complete events for now disable pipes here. */
13718 if (modeset && !crtc->state->active && crtc->state->event) {
13719 spin_lock_irq(&dev->event_lock);
13720 drm_crtc_send_vblank_event(crtc, crtc->state->event);
13721 spin_unlock_irq(&dev->event_lock);
13722
13723 crtc->state->event = NULL;
13724 }
13725
f6ac4b2a 13726 if (!modeset)
29ceb0e6 13727 intel_pre_plane_update(to_intel_crtc_state(old_crtc_state));
f6ac4b2a 13728
5a21b665
DV
13729 if (crtc->state->active &&
13730 drm_atomic_get_existing_plane_state(state, crtc->primary))
faf68d92 13731 intel_fbc_enable(intel_crtc, pipe_config, to_intel_plane_state(crtc->primary->state));
5a21b665 13732
1f7528c4 13733 if (crtc->state->active)
5a21b665 13734 drm_atomic_helper_commit_planes_on_crtc(old_crtc_state);
f6d1973d 13735
5a21b665
DV
13736 if (pipe_config->base.active && needs_vblank_wait(pipe_config))
13737 crtc_vblank_mask |= 1 << i;
177246a8
MR
13738 }
13739
94f05024
DV
13740 /* FIXME: We should call drm_atomic_helper_commit_hw_done() here
13741 * already, but still need the state for the delayed optimization. To
13742 * fix this:
13743 * - wrap the optimization/post_plane_update stuff into a per-crtc work.
13744 * - schedule that vblank worker _before_ calling hw_done
13745 * - at the start of commit_tail, cancel it _synchrously
13746 * - switch over to the vblank wait helper in the core after that since
13747 * we don't need out special handling any more.
13748 */
5a21b665
DV
13749 if (!state->legacy_cursor_update)
13750 intel_atomic_wait_for_vblanks(dev, dev_priv, crtc_vblank_mask);
13751
13752 /*
13753 * Now that the vblank has passed, we can go ahead and program the
13754 * optimal watermarks on platforms that need two-step watermark
13755 * programming.
13756 *
13757 * TODO: Move this (and other cleanup) to an async worker eventually.
13758 */
13759 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
13760 intel_cstate = to_intel_crtc_state(crtc->state);
13761
13762 if (dev_priv->display.optimize_watermarks)
13763 dev_priv->display.optimize_watermarks(intel_cstate);
13764 }
13765
13766 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
13767 intel_post_plane_update(to_intel_crtc_state(old_crtc_state));
13768
13769 if (put_domains[i])
13770 modeset_put_power_domains(dev_priv, put_domains[i]);
13771
13772 intel_modeset_verify_crtc(crtc, old_crtc_state, crtc->state);
13773 }
13774
94f05024
DV
13775 drm_atomic_helper_commit_hw_done(state);
13776
5a21b665
DV
13777 if (intel_state->modeset)
13778 intel_display_power_put(dev_priv, POWER_DOMAIN_MODESET);
13779
13780 mutex_lock(&dev->struct_mutex);
13781 drm_atomic_helper_cleanup_planes(dev, state);
13782 mutex_unlock(&dev->struct_mutex);
13783
ea0000f0
DV
13784 drm_atomic_helper_commit_cleanup_done(state);
13785
ee165b1a 13786 drm_atomic_state_free(state);
f30da187 13787
75714940
MK
13788 /* As one of the primary mmio accessors, KMS has a high likelihood
13789 * of triggering bugs in unclaimed access. After we finish
13790 * modesetting, see if an error has been flagged, and if so
13791 * enable debugging for the next modeset - and hope we catch
13792 * the culprit.
13793 *
13794 * XXX note that we assume display power is on at this point.
13795 * This might hold true now but we need to add pm helper to check
13796 * unclaimed only when the hardware is on, as atomic commits
13797 * can happen also when the device is completely off.
13798 */
13799 intel_uncore_arm_unclaimed_mmio_detection(dev_priv);
94f05024
DV
13800}
13801
13802static void intel_atomic_commit_work(struct work_struct *work)
13803{
13804 struct drm_atomic_state *state = container_of(work,
13805 struct drm_atomic_state,
13806 commit_work);
13807 intel_atomic_commit_tail(state);
13808}
13809
6c9c1b38
DV
13810static void intel_atomic_track_fbs(struct drm_atomic_state *state)
13811{
13812 struct drm_plane_state *old_plane_state;
13813 struct drm_plane *plane;
6c9c1b38
DV
13814 int i;
13815
faf5bf0a
CW
13816 for_each_plane_in_state(state, plane, old_plane_state, i)
13817 i915_gem_track_fb(intel_fb_obj(old_plane_state->fb),
13818 intel_fb_obj(plane->state->fb),
13819 to_intel_plane(plane)->frontbuffer_bit);
6c9c1b38
DV
13820}
13821
94f05024
DV
13822/**
13823 * intel_atomic_commit - commit validated state object
13824 * @dev: DRM device
13825 * @state: the top-level driver state object
13826 * @nonblock: nonblocking commit
13827 *
13828 * This function commits a top-level state object that has been validated
13829 * with drm_atomic_helper_check().
13830 *
13831 * FIXME: Atomic modeset support for i915 is not yet complete. At the moment
13832 * nonblocking commits are only safe for pure plane updates. Everything else
13833 * should work though.
13834 *
13835 * RETURNS
13836 * Zero for success or -errno.
13837 */
13838static int intel_atomic_commit(struct drm_device *dev,
13839 struct drm_atomic_state *state,
13840 bool nonblock)
13841{
13842 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
fac5e23e 13843 struct drm_i915_private *dev_priv = to_i915(dev);
94f05024
DV
13844 int ret = 0;
13845
13846 if (intel_state->modeset && nonblock) {
13847 DRM_DEBUG_KMS("nonblocking commit for modeset not yet implemented.\n");
13848 return -EINVAL;
13849 }
13850
13851 ret = drm_atomic_helper_setup_commit(state, nonblock);
13852 if (ret)
13853 return ret;
13854
13855 INIT_WORK(&state->commit_work, intel_atomic_commit_work);
13856
13857 ret = intel_atomic_prepare_commit(dev, state, nonblock);
13858 if (ret) {
13859 DRM_DEBUG_ATOMIC("Preparing state failed with %i\n", ret);
13860 return ret;
13861 }
13862
13863 drm_atomic_helper_swap_state(state, true);
13864 dev_priv->wm.distrust_bios_wm = false;
13865 dev_priv->wm.skl_results = intel_state->wm_results;
13866 intel_shared_dpll_commit(state);
6c9c1b38 13867 intel_atomic_track_fbs(state);
94f05024
DV
13868
13869 if (nonblock)
13870 queue_work(system_unbound_wq, &state->commit_work);
13871 else
13872 intel_atomic_commit_tail(state);
75714940 13873
74c090b1 13874 return 0;
7f27126e
JB
13875}
13876
c0c36b94
CW
13877void intel_crtc_restore_mode(struct drm_crtc *crtc)
13878{
83a57153
ACO
13879 struct drm_device *dev = crtc->dev;
13880 struct drm_atomic_state *state;
e694eb02 13881 struct drm_crtc_state *crtc_state;
2bfb4627 13882 int ret;
83a57153
ACO
13883
13884 state = drm_atomic_state_alloc(dev);
13885 if (!state) {
78108b7c
VS
13886 DRM_DEBUG_KMS("[CRTC:%d:%s] crtc restore failed, out of memory",
13887 crtc->base.id, crtc->name);
83a57153
ACO
13888 return;
13889 }
13890
e694eb02 13891 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
83a57153 13892
e694eb02
ML
13893retry:
13894 crtc_state = drm_atomic_get_crtc_state(state, crtc);
13895 ret = PTR_ERR_OR_ZERO(crtc_state);
13896 if (!ret) {
13897 if (!crtc_state->active)
13898 goto out;
83a57153 13899
e694eb02 13900 crtc_state->mode_changed = true;
74c090b1 13901 ret = drm_atomic_commit(state);
83a57153
ACO
13902 }
13903
e694eb02
ML
13904 if (ret == -EDEADLK) {
13905 drm_atomic_state_clear(state);
13906 drm_modeset_backoff(state->acquire_ctx);
13907 goto retry;
4ed9fb37 13908 }
4be07317 13909
2bfb4627 13910 if (ret)
e694eb02 13911out:
2bfb4627 13912 drm_atomic_state_free(state);
c0c36b94
CW
13913}
13914
25c5b266
DV
13915#undef for_each_intel_crtc_masked
13916
a8784875
BP
13917/*
13918 * FIXME: Remove this once i915 is fully DRIVER_ATOMIC by calling
13919 * drm_atomic_helper_legacy_gamma_set() directly.
13920 */
13921static int intel_atomic_legacy_gamma_set(struct drm_crtc *crtc,
13922 u16 *red, u16 *green, u16 *blue,
13923 uint32_t size)
13924{
13925 struct drm_device *dev = crtc->dev;
13926 struct drm_mode_config *config = &dev->mode_config;
13927 struct drm_crtc_state *state;
13928 int ret;
13929
13930 ret = drm_atomic_helper_legacy_gamma_set(crtc, red, green, blue, size);
13931 if (ret)
13932 return ret;
13933
13934 /*
13935 * Make sure we update the legacy properties so this works when
13936 * atomic is not enabled.
13937 */
13938
13939 state = crtc->state;
13940
13941 drm_object_property_set_value(&crtc->base,
13942 config->degamma_lut_property,
13943 (state->degamma_lut) ?
13944 state->degamma_lut->base.id : 0);
13945
13946 drm_object_property_set_value(&crtc->base,
13947 config->ctm_property,
13948 (state->ctm) ?
13949 state->ctm->base.id : 0);
13950
13951 drm_object_property_set_value(&crtc->base,
13952 config->gamma_lut_property,
13953 (state->gamma_lut) ?
13954 state->gamma_lut->base.id : 0);
13955
13956 return 0;
13957}
13958
f6e5b160 13959static const struct drm_crtc_funcs intel_crtc_funcs = {
a8784875 13960 .gamma_set = intel_atomic_legacy_gamma_set,
74c090b1 13961 .set_config = drm_atomic_helper_set_config,
82cf435b 13962 .set_property = drm_atomic_helper_crtc_set_property,
f6e5b160 13963 .destroy = intel_crtc_destroy,
527b6abe 13964 .page_flip = intel_crtc_page_flip,
1356837e
MR
13965 .atomic_duplicate_state = intel_crtc_duplicate_state,
13966 .atomic_destroy_state = intel_crtc_destroy_state,
f6e5b160
CW
13967};
13968
6beb8c23
MR
13969/**
13970 * intel_prepare_plane_fb - Prepare fb for usage on plane
13971 * @plane: drm plane to prepare for
13972 * @fb: framebuffer to prepare for presentation
13973 *
13974 * Prepares a framebuffer for usage on a display plane. Generally this
13975 * involves pinning the underlying object and updating the frontbuffer tracking
13976 * bits. Some older platforms need special physical address handling for
13977 * cursor planes.
13978 *
f935675f
ML
13979 * Must be called with struct_mutex held.
13980 *
6beb8c23
MR
13981 * Returns 0 on success, negative error code on failure.
13982 */
13983int
13984intel_prepare_plane_fb(struct drm_plane *plane,
d136dfee 13985 const struct drm_plane_state *new_state)
465c120c
MR
13986{
13987 struct drm_device *dev = plane->dev;
844f9111 13988 struct drm_framebuffer *fb = new_state->fb;
6beb8c23 13989 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
1ee49399 13990 struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->state->fb);
c37efb99 13991 struct reservation_object *resv;
6beb8c23 13992 int ret = 0;
465c120c 13993
1ee49399 13994 if (!obj && !old_obj)
465c120c
MR
13995 return 0;
13996
5008e874
ML
13997 if (old_obj) {
13998 struct drm_crtc_state *crtc_state =
13999 drm_atomic_get_existing_crtc_state(new_state->state, plane->state->crtc);
14000
14001 /* Big Hammer, we also need to ensure that any pending
14002 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
14003 * current scanout is retired before unpinning the old
14004 * framebuffer. Note that we rely on userspace rendering
14005 * into the buffer attached to the pipe they are waiting
14006 * on. If not, userspace generates a GPU hang with IPEHR
14007 * point to the MI_WAIT_FOR_EVENT.
14008 *
14009 * This should only fail upon a hung GPU, in which case we
14010 * can safely continue.
14011 */
14012 if (needs_modeset(crtc_state))
14013 ret = i915_gem_object_wait_rendering(old_obj, true);
f4457ae7
CW
14014 if (ret) {
14015 /* GPU hangs should have been swallowed by the wait */
14016 WARN_ON(ret == -EIO);
f935675f 14017 return ret;
f4457ae7 14018 }
5008e874
ML
14019 }
14020
c37efb99
CW
14021 if (!obj)
14022 return 0;
14023
5a21b665 14024 /* For framebuffer backed by dmabuf, wait for fence */
c37efb99
CW
14025 resv = i915_gem_object_get_dmabuf_resv(obj);
14026 if (resv) {
5a21b665
DV
14027 long lret;
14028
c37efb99 14029 lret = reservation_object_wait_timeout_rcu(resv, false, true,
5a21b665
DV
14030 MAX_SCHEDULE_TIMEOUT);
14031 if (lret == -ERESTARTSYS)
14032 return lret;
14033
14034 WARN(lret < 0, "waiting returns %li\n", lret);
14035 }
14036
c37efb99 14037 if (plane->type == DRM_PLANE_TYPE_CURSOR &&
6beb8c23
MR
14038 INTEL_INFO(dev)->cursor_needs_physical) {
14039 int align = IS_I830(dev) ? 16 * 1024 : 256;
14040 ret = i915_gem_object_attach_phys(obj, align);
14041 if (ret)
14042 DRM_DEBUG_KMS("failed to attach phys object\n");
14043 } else {
3465c580 14044 ret = intel_pin_and_fence_fb_obj(fb, new_state->rotation);
6beb8c23 14045 }
465c120c 14046
c37efb99 14047 if (ret == 0) {
27c01aae 14048 to_intel_plane_state(new_state)->wait_req =
d72d908b
CW
14049 i915_gem_active_get(&obj->last_write,
14050 &obj->base.dev->struct_mutex);
7580d774 14051 }
fdd508a6 14052
6beb8c23
MR
14053 return ret;
14054}
14055
38f3ce3a
MR
14056/**
14057 * intel_cleanup_plane_fb - Cleans up an fb after plane use
14058 * @plane: drm plane to clean up for
14059 * @fb: old framebuffer that was on plane
14060 *
14061 * Cleans up a framebuffer that has just been removed from a plane.
f935675f
ML
14062 *
14063 * Must be called with struct_mutex held.
38f3ce3a
MR
14064 */
14065void
14066intel_cleanup_plane_fb(struct drm_plane *plane,
d136dfee 14067 const struct drm_plane_state *old_state)
38f3ce3a
MR
14068{
14069 struct drm_device *dev = plane->dev;
7580d774 14070 struct intel_plane_state *old_intel_state;
84978257 14071 struct intel_plane_state *intel_state = to_intel_plane_state(plane->state);
1ee49399
ML
14072 struct drm_i915_gem_object *old_obj = intel_fb_obj(old_state->fb);
14073 struct drm_i915_gem_object *obj = intel_fb_obj(plane->state->fb);
38f3ce3a 14074
7580d774
ML
14075 old_intel_state = to_intel_plane_state(old_state);
14076
1ee49399 14077 if (!obj && !old_obj)
38f3ce3a
MR
14078 return;
14079
1ee49399
ML
14080 if (old_obj && (plane->type != DRM_PLANE_TYPE_CURSOR ||
14081 !INTEL_INFO(dev)->cursor_needs_physical))
3465c580 14082 intel_unpin_fb_obj(old_state->fb, old_state->rotation);
1ee49399 14083
84978257 14084 i915_gem_request_assign(&intel_state->wait_req, NULL);
7580d774 14085 i915_gem_request_assign(&old_intel_state->wait_req, NULL);
465c120c
MR
14086}
14087
6156a456
CK
14088int
14089skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
14090{
14091 int max_scale;
6156a456
CK
14092 int crtc_clock, cdclk;
14093
bf8a0af0 14094 if (!intel_crtc || !crtc_state->base.enable)
6156a456
CK
14095 return DRM_PLANE_HELPER_NO_SCALING;
14096
6156a456 14097 crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
27c329ed 14098 cdclk = to_intel_atomic_state(crtc_state->base.state)->cdclk;
6156a456 14099
54bf1ce6 14100 if (WARN_ON_ONCE(!crtc_clock || cdclk < crtc_clock))
6156a456
CK
14101 return DRM_PLANE_HELPER_NO_SCALING;
14102
14103 /*
14104 * skl max scale is lower of:
14105 * close to 3 but not 3, -1 is for that purpose
14106 * or
14107 * cdclk/crtc_clock
14108 */
14109 max_scale = min((1 << 16) * 3 - 1, (1 << 8) * ((cdclk << 8) / crtc_clock));
14110
14111 return max_scale;
14112}
14113
465c120c 14114static int
3c692a41 14115intel_check_primary_plane(struct drm_plane *plane,
061e4b8d 14116 struct intel_crtc_state *crtc_state,
3c692a41
GP
14117 struct intel_plane_state *state)
14118{
2b875c22
MR
14119 struct drm_crtc *crtc = state->base.crtc;
14120 struct drm_framebuffer *fb = state->base.fb;
6156a456 14121 int min_scale = DRM_PLANE_HELPER_NO_SCALING;
061e4b8d
ML
14122 int max_scale = DRM_PLANE_HELPER_NO_SCALING;
14123 bool can_position = false;
465c120c 14124
693bdc28
VS
14125 if (INTEL_INFO(plane->dev)->gen >= 9) {
14126 /* use scaler when colorkey is not required */
14127 if (state->ckey.flags == I915_SET_COLORKEY_NONE) {
14128 min_scale = 1;
14129 max_scale = skl_max_scale(to_intel_crtc(crtc), crtc_state);
14130 }
d8106366 14131 can_position = true;
6156a456 14132 }
d8106366 14133
061e4b8d
ML
14134 return drm_plane_helper_check_update(plane, crtc, fb, &state->src,
14135 &state->dst, &state->clip,
9b8b013d 14136 state->base.rotation,
da20eabd
ML
14137 min_scale, max_scale,
14138 can_position, true,
14139 &state->visible);
14af293f
GP
14140}
14141
5a21b665
DV
14142static void intel_begin_crtc_commit(struct drm_crtc *crtc,
14143 struct drm_crtc_state *old_crtc_state)
14144{
14145 struct drm_device *dev = crtc->dev;
14146 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
14147 struct intel_crtc_state *old_intel_state =
14148 to_intel_crtc_state(old_crtc_state);
14149 bool modeset = needs_modeset(crtc->state);
14150
14151 /* Perform vblank evasion around commit operation */
14152 intel_pipe_update_start(intel_crtc);
14153
14154 if (modeset)
14155 return;
14156
14157 if (crtc->state->color_mgmt_changed || to_intel_crtc_state(crtc->state)->update_pipe) {
14158 intel_color_set_csc(crtc->state);
14159 intel_color_load_luts(crtc->state);
14160 }
14161
14162 if (to_intel_crtc_state(crtc->state)->update_pipe)
14163 intel_update_pipe_config(intel_crtc, old_intel_state);
14164 else if (INTEL_INFO(dev)->gen >= 9)
14165 skl_detach_scalers(intel_crtc);
14166}
14167
14168static void intel_finish_crtc_commit(struct drm_crtc *crtc,
14169 struct drm_crtc_state *old_crtc_state)
14170{
14171 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
14172
14173 intel_pipe_update_end(intel_crtc, NULL);
14174}
14175
cf4c7c12 14176/**
4a3b8769
MR
14177 * intel_plane_destroy - destroy a plane
14178 * @plane: plane to destroy
cf4c7c12 14179 *
4a3b8769
MR
14180 * Common destruction function for all types of planes (primary, cursor,
14181 * sprite).
cf4c7c12 14182 */
4a3b8769 14183void intel_plane_destroy(struct drm_plane *plane)
465c120c 14184{
69ae561f
VS
14185 if (!plane)
14186 return;
14187
465c120c 14188 drm_plane_cleanup(plane);
69ae561f 14189 kfree(to_intel_plane(plane));
465c120c
MR
14190}
14191
65a3fea0 14192const struct drm_plane_funcs intel_plane_funcs = {
70a101f8
MR
14193 .update_plane = drm_atomic_helper_update_plane,
14194 .disable_plane = drm_atomic_helper_disable_plane,
3d7d6510 14195 .destroy = intel_plane_destroy,
c196e1d6 14196 .set_property = drm_atomic_helper_plane_set_property,
a98b3431
MR
14197 .atomic_get_property = intel_plane_atomic_get_property,
14198 .atomic_set_property = intel_plane_atomic_set_property,
ea2c67bb
MR
14199 .atomic_duplicate_state = intel_plane_duplicate_state,
14200 .atomic_destroy_state = intel_plane_destroy_state,
14201
465c120c
MR
14202};
14203
14204static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
14205 int pipe)
14206{
fca0ce2a
VS
14207 struct intel_plane *primary = NULL;
14208 struct intel_plane_state *state = NULL;
465c120c 14209 const uint32_t *intel_primary_formats;
45e3743a 14210 unsigned int num_formats;
fca0ce2a 14211 int ret;
465c120c
MR
14212
14213 primary = kzalloc(sizeof(*primary), GFP_KERNEL);
fca0ce2a
VS
14214 if (!primary)
14215 goto fail;
465c120c 14216
8e7d688b 14217 state = intel_create_plane_state(&primary->base);
fca0ce2a
VS
14218 if (!state)
14219 goto fail;
8e7d688b 14220 primary->base.state = &state->base;
ea2c67bb 14221
465c120c
MR
14222 primary->can_scale = false;
14223 primary->max_downscale = 1;
6156a456
CK
14224 if (INTEL_INFO(dev)->gen >= 9) {
14225 primary->can_scale = true;
af99ceda 14226 state->scaler_id = -1;
6156a456 14227 }
465c120c
MR
14228 primary->pipe = pipe;
14229 primary->plane = pipe;
a9ff8714 14230 primary->frontbuffer_bit = INTEL_FRONTBUFFER_PRIMARY(pipe);
c59cb179 14231 primary->check_plane = intel_check_primary_plane;
465c120c
MR
14232 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
14233 primary->plane = !pipe;
14234
6c0fd451
DL
14235 if (INTEL_INFO(dev)->gen >= 9) {
14236 intel_primary_formats = skl_primary_formats;
14237 num_formats = ARRAY_SIZE(skl_primary_formats);
a8d201af
ML
14238
14239 primary->update_plane = skylake_update_primary_plane;
14240 primary->disable_plane = skylake_disable_primary_plane;
14241 } else if (HAS_PCH_SPLIT(dev)) {
14242 intel_primary_formats = i965_primary_formats;
14243 num_formats = ARRAY_SIZE(i965_primary_formats);
14244
14245 primary->update_plane = ironlake_update_primary_plane;
14246 primary->disable_plane = i9xx_disable_primary_plane;
6c0fd451 14247 } else if (INTEL_INFO(dev)->gen >= 4) {
568db4f2
DL
14248 intel_primary_formats = i965_primary_formats;
14249 num_formats = ARRAY_SIZE(i965_primary_formats);
a8d201af
ML
14250
14251 primary->update_plane = i9xx_update_primary_plane;
14252 primary->disable_plane = i9xx_disable_primary_plane;
6c0fd451
DL
14253 } else {
14254 intel_primary_formats = i8xx_primary_formats;
14255 num_formats = ARRAY_SIZE(i8xx_primary_formats);
a8d201af
ML
14256
14257 primary->update_plane = i9xx_update_primary_plane;
14258 primary->disable_plane = i9xx_disable_primary_plane;
465c120c
MR
14259 }
14260
38573dc1
VS
14261 if (INTEL_INFO(dev)->gen >= 9)
14262 ret = drm_universal_plane_init(dev, &primary->base, 0,
14263 &intel_plane_funcs,
14264 intel_primary_formats, num_formats,
14265 DRM_PLANE_TYPE_PRIMARY,
14266 "plane 1%c", pipe_name(pipe));
14267 else if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
14268 ret = drm_universal_plane_init(dev, &primary->base, 0,
14269 &intel_plane_funcs,
14270 intel_primary_formats, num_formats,
14271 DRM_PLANE_TYPE_PRIMARY,
14272 "primary %c", pipe_name(pipe));
14273 else
14274 ret = drm_universal_plane_init(dev, &primary->base, 0,
14275 &intel_plane_funcs,
14276 intel_primary_formats, num_formats,
14277 DRM_PLANE_TYPE_PRIMARY,
14278 "plane %c", plane_name(primary->plane));
fca0ce2a
VS
14279 if (ret)
14280 goto fail;
48404c1e 14281
3b7a5119
SJ
14282 if (INTEL_INFO(dev)->gen >= 4)
14283 intel_create_rotation_property(dev, primary);
48404c1e 14284
ea2c67bb
MR
14285 drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
14286
465c120c 14287 return &primary->base;
fca0ce2a
VS
14288
14289fail:
14290 kfree(state);
14291 kfree(primary);
14292
14293 return NULL;
465c120c
MR
14294}
14295
3b7a5119
SJ
14296void intel_create_rotation_property(struct drm_device *dev, struct intel_plane *plane)
14297{
14298 if (!dev->mode_config.rotation_property) {
14299 unsigned long flags = BIT(DRM_ROTATE_0) |
14300 BIT(DRM_ROTATE_180);
14301
14302 if (INTEL_INFO(dev)->gen >= 9)
14303 flags |= BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_270);
14304
14305 dev->mode_config.rotation_property =
14306 drm_mode_create_rotation_property(dev, flags);
14307 }
14308 if (dev->mode_config.rotation_property)
14309 drm_object_attach_property(&plane->base.base,
14310 dev->mode_config.rotation_property,
14311 plane->base.state->rotation);
14312}
14313
3d7d6510 14314static int
852e787c 14315intel_check_cursor_plane(struct drm_plane *plane,
061e4b8d 14316 struct intel_crtc_state *crtc_state,
852e787c 14317 struct intel_plane_state *state)
3d7d6510 14318{
061e4b8d 14319 struct drm_crtc *crtc = crtc_state->base.crtc;
2b875c22 14320 struct drm_framebuffer *fb = state->base.fb;
757f9a3e 14321 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
b29ec92c 14322 enum pipe pipe = to_intel_plane(plane)->pipe;
757f9a3e
GP
14323 unsigned stride;
14324 int ret;
3d7d6510 14325
061e4b8d
ML
14326 ret = drm_plane_helper_check_update(plane, crtc, fb, &state->src,
14327 &state->dst, &state->clip,
9b8b013d 14328 state->base.rotation,
3d7d6510
MR
14329 DRM_PLANE_HELPER_NO_SCALING,
14330 DRM_PLANE_HELPER_NO_SCALING,
852e787c 14331 true, true, &state->visible);
757f9a3e
GP
14332 if (ret)
14333 return ret;
14334
757f9a3e
GP
14335 /* if we want to turn off the cursor ignore width and height */
14336 if (!obj)
da20eabd 14337 return 0;
757f9a3e 14338
757f9a3e 14339 /* Check for which cursor types we support */
061e4b8d 14340 if (!cursor_size_ok(plane->dev, state->base.crtc_w, state->base.crtc_h)) {
ea2c67bb
MR
14341 DRM_DEBUG("Cursor dimension %dx%d not supported\n",
14342 state->base.crtc_w, state->base.crtc_h);
757f9a3e
GP
14343 return -EINVAL;
14344 }
14345
ea2c67bb
MR
14346 stride = roundup_pow_of_two(state->base.crtc_w) * 4;
14347 if (obj->base.size < stride * state->base.crtc_h) {
757f9a3e
GP
14348 DRM_DEBUG_KMS("buffer is too small\n");
14349 return -ENOMEM;
14350 }
14351
3a656b54 14352 if (fb->modifier[0] != DRM_FORMAT_MOD_NONE) {
757f9a3e 14353 DRM_DEBUG_KMS("cursor cannot be tiled\n");
da20eabd 14354 return -EINVAL;
32b7eeec
MR
14355 }
14356
b29ec92c
VS
14357 /*
14358 * There's something wrong with the cursor on CHV pipe C.
14359 * If it straddles the left edge of the screen then
14360 * moving it away from the edge or disabling it often
14361 * results in a pipe underrun, and often that can lead to
14362 * dead pipe (constant underrun reported, and it scans
14363 * out just a solid color). To recover from that, the
14364 * display power well must be turned off and on again.
14365 * Refuse the put the cursor into that compromised position.
14366 */
14367 if (IS_CHERRYVIEW(plane->dev) && pipe == PIPE_C &&
14368 state->visible && state->base.crtc_x < 0) {
14369 DRM_DEBUG_KMS("CHV cursor C not allowed to straddle the left screen edge\n");
14370 return -EINVAL;
14371 }
14372
da20eabd 14373 return 0;
852e787c 14374}
3d7d6510 14375
a8ad0d8e
ML
14376static void
14377intel_disable_cursor_plane(struct drm_plane *plane,
7fabf5ef 14378 struct drm_crtc *crtc)
a8ad0d8e 14379{
f2858021
ML
14380 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
14381
14382 intel_crtc->cursor_addr = 0;
55a08b3f 14383 intel_crtc_update_cursor(crtc, NULL);
a8ad0d8e
ML
14384}
14385
f4a2cf29 14386static void
55a08b3f
ML
14387intel_update_cursor_plane(struct drm_plane *plane,
14388 const struct intel_crtc_state *crtc_state,
14389 const struct intel_plane_state *state)
852e787c 14390{
55a08b3f
ML
14391 struct drm_crtc *crtc = crtc_state->base.crtc;
14392 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ea2c67bb 14393 struct drm_device *dev = plane->dev;
2b875c22 14394 struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
a912f12f 14395 uint32_t addr;
852e787c 14396
f4a2cf29 14397 if (!obj)
a912f12f 14398 addr = 0;
f4a2cf29 14399 else if (!INTEL_INFO(dev)->cursor_needs_physical)
a912f12f 14400 addr = i915_gem_obj_ggtt_offset(obj);
f4a2cf29 14401 else
a912f12f 14402 addr = obj->phys_handle->busaddr;
852e787c 14403
a912f12f 14404 intel_crtc->cursor_addr = addr;
55a08b3f 14405 intel_crtc_update_cursor(crtc, state);
852e787c
GP
14406}
14407
3d7d6510
MR
14408static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
14409 int pipe)
14410{
fca0ce2a
VS
14411 struct intel_plane *cursor = NULL;
14412 struct intel_plane_state *state = NULL;
14413 int ret;
3d7d6510
MR
14414
14415 cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
fca0ce2a
VS
14416 if (!cursor)
14417 goto fail;
3d7d6510 14418
8e7d688b 14419 state = intel_create_plane_state(&cursor->base);
fca0ce2a
VS
14420 if (!state)
14421 goto fail;
8e7d688b 14422 cursor->base.state = &state->base;
ea2c67bb 14423
3d7d6510
MR
14424 cursor->can_scale = false;
14425 cursor->max_downscale = 1;
14426 cursor->pipe = pipe;
14427 cursor->plane = pipe;
a9ff8714 14428 cursor->frontbuffer_bit = INTEL_FRONTBUFFER_CURSOR(pipe);
c59cb179 14429 cursor->check_plane = intel_check_cursor_plane;
55a08b3f 14430 cursor->update_plane = intel_update_cursor_plane;
a8ad0d8e 14431 cursor->disable_plane = intel_disable_cursor_plane;
3d7d6510 14432
fca0ce2a
VS
14433 ret = drm_universal_plane_init(dev, &cursor->base, 0,
14434 &intel_plane_funcs,
14435 intel_cursor_formats,
14436 ARRAY_SIZE(intel_cursor_formats),
38573dc1
VS
14437 DRM_PLANE_TYPE_CURSOR,
14438 "cursor %c", pipe_name(pipe));
fca0ce2a
VS
14439 if (ret)
14440 goto fail;
4398ad45
VS
14441
14442 if (INTEL_INFO(dev)->gen >= 4) {
14443 if (!dev->mode_config.rotation_property)
14444 dev->mode_config.rotation_property =
14445 drm_mode_create_rotation_property(dev,
14446 BIT(DRM_ROTATE_0) |
14447 BIT(DRM_ROTATE_180));
14448 if (dev->mode_config.rotation_property)
14449 drm_object_attach_property(&cursor->base.base,
14450 dev->mode_config.rotation_property,
8e7d688b 14451 state->base.rotation);
4398ad45
VS
14452 }
14453
af99ceda
CK
14454 if (INTEL_INFO(dev)->gen >=9)
14455 state->scaler_id = -1;
14456
ea2c67bb
MR
14457 drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
14458
3d7d6510 14459 return &cursor->base;
fca0ce2a
VS
14460
14461fail:
14462 kfree(state);
14463 kfree(cursor);
14464
14465 return NULL;
3d7d6510
MR
14466}
14467
549e2bfb
CK
14468static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
14469 struct intel_crtc_state *crtc_state)
14470{
14471 int i;
14472 struct intel_scaler *intel_scaler;
14473 struct intel_crtc_scaler_state *scaler_state = &crtc_state->scaler_state;
14474
14475 for (i = 0; i < intel_crtc->num_scalers; i++) {
14476 intel_scaler = &scaler_state->scalers[i];
14477 intel_scaler->in_use = 0;
549e2bfb
CK
14478 intel_scaler->mode = PS_SCALER_MODE_DYN;
14479 }
14480
14481 scaler_state->scaler_id = -1;
14482}
14483
b358d0a6 14484static void intel_crtc_init(struct drm_device *dev, int pipe)
79e53945 14485{
fac5e23e 14486 struct drm_i915_private *dev_priv = to_i915(dev);
79e53945 14487 struct intel_crtc *intel_crtc;
f5de6e07 14488 struct intel_crtc_state *crtc_state = NULL;
3d7d6510
MR
14489 struct drm_plane *primary = NULL;
14490 struct drm_plane *cursor = NULL;
8563b1e8 14491 int ret;
79e53945 14492
955382f3 14493 intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
79e53945
JB
14494 if (intel_crtc == NULL)
14495 return;
14496
f5de6e07
ACO
14497 crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
14498 if (!crtc_state)
14499 goto fail;
550acefd
ACO
14500 intel_crtc->config = crtc_state;
14501 intel_crtc->base.state = &crtc_state->base;
07878248 14502 crtc_state->base.crtc = &intel_crtc->base;
f5de6e07 14503
549e2bfb
CK
14504 /* initialize shared scalers */
14505 if (INTEL_INFO(dev)->gen >= 9) {
14506 if (pipe == PIPE_C)
14507 intel_crtc->num_scalers = 1;
14508 else
14509 intel_crtc->num_scalers = SKL_NUM_SCALERS;
14510
14511 skl_init_scalers(dev, intel_crtc, crtc_state);
14512 }
14513
465c120c 14514 primary = intel_primary_plane_create(dev, pipe);
3d7d6510
MR
14515 if (!primary)
14516 goto fail;
14517
14518 cursor = intel_cursor_plane_create(dev, pipe);
14519 if (!cursor)
14520 goto fail;
14521
465c120c 14522 ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
4d5d72b7
VS
14523 cursor, &intel_crtc_funcs,
14524 "pipe %c", pipe_name(pipe));
3d7d6510
MR
14525 if (ret)
14526 goto fail;
79e53945 14527
1f1c2e24
VS
14528 /*
14529 * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
8c0f92e1 14530 * is hooked to pipe B. Hence we want plane A feeding pipe B.
1f1c2e24 14531 */
80824003
JB
14532 intel_crtc->pipe = pipe;
14533 intel_crtc->plane = pipe;
3a77c4c4 14534 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
28c97730 14535 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
e2e767ab 14536 intel_crtc->plane = !pipe;
80824003
JB
14537 }
14538
4b0e333e
CW
14539 intel_crtc->cursor_base = ~0;
14540 intel_crtc->cursor_cntl = ~0;
dc41c154 14541 intel_crtc->cursor_size = ~0;
8d7849db 14542
852eb00d
VS
14543 intel_crtc->wm.cxsr_allowed = true;
14544
22fd0fab
JB
14545 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
14546 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
14547 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
14548 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
14549
79e53945 14550 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
87b6b101 14551
8563b1e8
LL
14552 intel_color_init(&intel_crtc->base);
14553
87b6b101 14554 WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
3d7d6510
MR
14555 return;
14556
14557fail:
69ae561f
VS
14558 intel_plane_destroy(primary);
14559 intel_plane_destroy(cursor);
f5de6e07 14560 kfree(crtc_state);
3d7d6510 14561 kfree(intel_crtc);
79e53945
JB
14562}
14563
752aa88a
JB
14564enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
14565{
14566 struct drm_encoder *encoder = connector->base.encoder;
6e9f798d 14567 struct drm_device *dev = connector->base.dev;
752aa88a 14568
51fd371b 14569 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
752aa88a 14570
d3babd3f 14571 if (!encoder || WARN_ON(!encoder->crtc))
752aa88a
JB
14572 return INVALID_PIPE;
14573
14574 return to_intel_crtc(encoder->crtc)->pipe;
14575}
14576
08d7b3d1 14577int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
05394f39 14578 struct drm_file *file)
08d7b3d1 14579{
08d7b3d1 14580 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
7707e653 14581 struct drm_crtc *drmmode_crtc;
c05422d5 14582 struct intel_crtc *crtc;
08d7b3d1 14583
7707e653 14584 drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
71240ed2 14585 if (!drmmode_crtc)
3f2c2057 14586 return -ENOENT;
08d7b3d1 14587
7707e653 14588 crtc = to_intel_crtc(drmmode_crtc);
c05422d5 14589 pipe_from_crtc_id->pipe = crtc->pipe;
08d7b3d1 14590
c05422d5 14591 return 0;
08d7b3d1
CW
14592}
14593
66a9278e 14594static int intel_encoder_clones(struct intel_encoder *encoder)
79e53945 14595{
66a9278e
DV
14596 struct drm_device *dev = encoder->base.dev;
14597 struct intel_encoder *source_encoder;
79e53945 14598 int index_mask = 0;
79e53945
JB
14599 int entry = 0;
14600
b2784e15 14601 for_each_intel_encoder(dev, source_encoder) {
bc079e8b 14602 if (encoders_cloneable(encoder, source_encoder))
66a9278e
DV
14603 index_mask |= (1 << entry);
14604
79e53945
JB
14605 entry++;
14606 }
4ef69c7a 14607
79e53945
JB
14608 return index_mask;
14609}
14610
4d302442
CW
14611static bool has_edp_a(struct drm_device *dev)
14612{
fac5e23e 14613 struct drm_i915_private *dev_priv = to_i915(dev);
4d302442
CW
14614
14615 if (!IS_MOBILE(dev))
14616 return false;
14617
14618 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
14619 return false;
14620
e3589908 14621 if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
4d302442
CW
14622 return false;
14623
14624 return true;
14625}
14626
84b4e042
JB
14627static bool intel_crt_present(struct drm_device *dev)
14628{
fac5e23e 14629 struct drm_i915_private *dev_priv = to_i915(dev);
84b4e042 14630
884497ed
DL
14631 if (INTEL_INFO(dev)->gen >= 9)
14632 return false;
14633
cf404ce4 14634 if (IS_HSW_ULT(dev) || IS_BDW_ULT(dev))
84b4e042
JB
14635 return false;
14636
14637 if (IS_CHERRYVIEW(dev))
14638 return false;
14639
65e472e4
VS
14640 if (HAS_PCH_LPT_H(dev) && I915_READ(SFUSE_STRAP) & SFUSE_STRAP_CRT_DISABLED)
14641 return false;
14642
70ac54d0
VS
14643 /* DDI E can't be used if DDI A requires 4 lanes */
14644 if (HAS_DDI(dev) && I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_A_4_LANES)
14645 return false;
14646
e4abb733 14647 if (!dev_priv->vbt.int_crt_support)
84b4e042
JB
14648 return false;
14649
14650 return true;
14651}
14652
79e53945
JB
14653static void intel_setup_outputs(struct drm_device *dev)
14654{
fac5e23e 14655 struct drm_i915_private *dev_priv = to_i915(dev);
4ef69c7a 14656 struct intel_encoder *encoder;
cb0953d7 14657 bool dpd_is_edp = false;
79e53945 14658
97a824e1
ID
14659 /*
14660 * intel_edp_init_connector() depends on this completing first, to
14661 * prevent the registeration of both eDP and LVDS and the incorrect
14662 * sharing of the PPS.
14663 */
c9093354 14664 intel_lvds_init(dev);
79e53945 14665
84b4e042 14666 if (intel_crt_present(dev))
79935fca 14667 intel_crt_init(dev);
cb0953d7 14668
c776eb2e
VK
14669 if (IS_BROXTON(dev)) {
14670 /*
14671 * FIXME: Broxton doesn't support port detection via the
14672 * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
14673 * detect the ports.
14674 */
14675 intel_ddi_init(dev, PORT_A);
14676 intel_ddi_init(dev, PORT_B);
14677 intel_ddi_init(dev, PORT_C);
c6c794a2
SS
14678
14679 intel_dsi_init(dev);
c776eb2e 14680 } else if (HAS_DDI(dev)) {
0e72a5b5
ED
14681 int found;
14682
de31facd
JB
14683 /*
14684 * Haswell uses DDI functions to detect digital outputs.
14685 * On SKL pre-D0 the strap isn't connected, so we assume
14686 * it's there.
14687 */
77179400 14688 found = I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_INIT_DISPLAY_DETECTED;
de31facd 14689 /* WaIgnoreDDIAStrap: skl */
ef11bdb3 14690 if (found || IS_SKYLAKE(dev) || IS_KABYLAKE(dev))
0e72a5b5
ED
14691 intel_ddi_init(dev, PORT_A);
14692
14693 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
14694 * register */
14695 found = I915_READ(SFUSE_STRAP);
14696
14697 if (found & SFUSE_STRAP_DDIB_DETECTED)
14698 intel_ddi_init(dev, PORT_B);
14699 if (found & SFUSE_STRAP_DDIC_DETECTED)
14700 intel_ddi_init(dev, PORT_C);
14701 if (found & SFUSE_STRAP_DDID_DETECTED)
14702 intel_ddi_init(dev, PORT_D);
2800e4c2
RV
14703 /*
14704 * On SKL we don't have a way to detect DDI-E so we rely on VBT.
14705 */
ef11bdb3 14706 if ((IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) &&
2800e4c2
RV
14707 (dev_priv->vbt.ddi_port_info[PORT_E].supports_dp ||
14708 dev_priv->vbt.ddi_port_info[PORT_E].supports_dvi ||
14709 dev_priv->vbt.ddi_port_info[PORT_E].supports_hdmi))
14710 intel_ddi_init(dev, PORT_E);
14711
0e72a5b5 14712 } else if (HAS_PCH_SPLIT(dev)) {
cb0953d7 14713 int found;
5d8a7752 14714 dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
270b3042
DV
14715
14716 if (has_edp_a(dev))
14717 intel_dp_init(dev, DP_A, PORT_A);
cb0953d7 14718
dc0fa718 14719 if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
461ed3ca 14720 /* PCH SDVOB multiplex with HDMIB */
2a5c0832 14721 found = intel_sdvo_init(dev, PCH_SDVOB, PORT_B);
30ad48b7 14722 if (!found)
e2debe91 14723 intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
5eb08b69 14724 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
ab9d7c30 14725 intel_dp_init(dev, PCH_DP_B, PORT_B);
30ad48b7
ZW
14726 }
14727
dc0fa718 14728 if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
e2debe91 14729 intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
30ad48b7 14730
dc0fa718 14731 if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
e2debe91 14732 intel_hdmi_init(dev, PCH_HDMID, PORT_D);
30ad48b7 14733
5eb08b69 14734 if (I915_READ(PCH_DP_C) & DP_DETECTED)
ab9d7c30 14735 intel_dp_init(dev, PCH_DP_C, PORT_C);
5eb08b69 14736
270b3042 14737 if (I915_READ(PCH_DP_D) & DP_DETECTED)
ab9d7c30 14738 intel_dp_init(dev, PCH_DP_D, PORT_D);
666a4537 14739 } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
22f35042 14740 bool has_edp, has_port;
457c52d8 14741
e17ac6db
VS
14742 /*
14743 * The DP_DETECTED bit is the latched state of the DDC
14744 * SDA pin at boot. However since eDP doesn't require DDC
14745 * (no way to plug in a DP->HDMI dongle) the DDC pins for
14746 * eDP ports may have been muxed to an alternate function.
14747 * Thus we can't rely on the DP_DETECTED bit alone to detect
14748 * eDP ports. Consult the VBT as well as DP_DETECTED to
14749 * detect eDP ports.
22f35042
VS
14750 *
14751 * Sadly the straps seem to be missing sometimes even for HDMI
14752 * ports (eg. on Voyo V3 - CHT x7-Z8700), so check both strap
14753 * and VBT for the presence of the port. Additionally we can't
14754 * trust the port type the VBT declares as we've seen at least
14755 * HDMI ports that the VBT claim are DP or eDP.
e17ac6db 14756 */
457c52d8 14757 has_edp = intel_dp_is_edp(dev, PORT_B);
22f35042
VS
14758 has_port = intel_bios_is_port_present(dev_priv, PORT_B);
14759 if (I915_READ(VLV_DP_B) & DP_DETECTED || has_port)
457c52d8 14760 has_edp &= intel_dp_init(dev, VLV_DP_B, PORT_B);
22f35042 14761 if ((I915_READ(VLV_HDMIB) & SDVO_DETECTED || has_port) && !has_edp)
e66eb81d 14762 intel_hdmi_init(dev, VLV_HDMIB, PORT_B);
585a94b8 14763
457c52d8 14764 has_edp = intel_dp_is_edp(dev, PORT_C);
22f35042
VS
14765 has_port = intel_bios_is_port_present(dev_priv, PORT_C);
14766 if (I915_READ(VLV_DP_C) & DP_DETECTED || has_port)
457c52d8 14767 has_edp &= intel_dp_init(dev, VLV_DP_C, PORT_C);
22f35042 14768 if ((I915_READ(VLV_HDMIC) & SDVO_DETECTED || has_port) && !has_edp)
e66eb81d 14769 intel_hdmi_init(dev, VLV_HDMIC, PORT_C);
19c03924 14770
9418c1f1 14771 if (IS_CHERRYVIEW(dev)) {
22f35042
VS
14772 /*
14773 * eDP not supported on port D,
14774 * so no need to worry about it
14775 */
14776 has_port = intel_bios_is_port_present(dev_priv, PORT_D);
14777 if (I915_READ(CHV_DP_D) & DP_DETECTED || has_port)
e66eb81d 14778 intel_dp_init(dev, CHV_DP_D, PORT_D);
22f35042
VS
14779 if (I915_READ(CHV_HDMID) & SDVO_DETECTED || has_port)
14780 intel_hdmi_init(dev, CHV_HDMID, PORT_D);
9418c1f1
VS
14781 }
14782
3cfca973 14783 intel_dsi_init(dev);
09da55dc 14784 } else if (!IS_GEN2(dev) && !IS_PINEVIEW(dev)) {
27185ae1 14785 bool found = false;
7d57382e 14786
e2debe91 14787 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
b01f2c3a 14788 DRM_DEBUG_KMS("probing SDVOB\n");
2a5c0832 14789 found = intel_sdvo_init(dev, GEN3_SDVOB, PORT_B);
3fec3d2f 14790 if (!found && IS_G4X(dev)) {
b01f2c3a 14791 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
e2debe91 14792 intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
b01f2c3a 14793 }
27185ae1 14794
3fec3d2f 14795 if (!found && IS_G4X(dev))
ab9d7c30 14796 intel_dp_init(dev, DP_B, PORT_B);
725e30ad 14797 }
13520b05
KH
14798
14799 /* Before G4X SDVOC doesn't have its own detect register */
13520b05 14800
e2debe91 14801 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
b01f2c3a 14802 DRM_DEBUG_KMS("probing SDVOC\n");
2a5c0832 14803 found = intel_sdvo_init(dev, GEN3_SDVOC, PORT_C);
b01f2c3a 14804 }
27185ae1 14805
e2debe91 14806 if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
27185ae1 14807
3fec3d2f 14808 if (IS_G4X(dev)) {
b01f2c3a 14809 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
e2debe91 14810 intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
b01f2c3a 14811 }
3fec3d2f 14812 if (IS_G4X(dev))
ab9d7c30 14813 intel_dp_init(dev, DP_C, PORT_C);
725e30ad 14814 }
27185ae1 14815
3fec3d2f 14816 if (IS_G4X(dev) &&
e7281eab 14817 (I915_READ(DP_D) & DP_DETECTED))
ab9d7c30 14818 intel_dp_init(dev, DP_D, PORT_D);
bad720ff 14819 } else if (IS_GEN2(dev))
79e53945
JB
14820 intel_dvo_init(dev);
14821
103a196f 14822 if (SUPPORTS_TV(dev))
79e53945
JB
14823 intel_tv_init(dev);
14824
0bc12bcb 14825 intel_psr_init(dev);
7c8f8a70 14826
b2784e15 14827 for_each_intel_encoder(dev, encoder) {
4ef69c7a
CW
14828 encoder->base.possible_crtcs = encoder->crtc_mask;
14829 encoder->base.possible_clones =
66a9278e 14830 intel_encoder_clones(encoder);
79e53945 14831 }
47356eb6 14832
dde86e2d 14833 intel_init_pch_refclk(dev);
270b3042
DV
14834
14835 drm_helper_move_panel_connectors_to_head(dev);
79e53945
JB
14836}
14837
14838static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
14839{
60a5ca01 14840 struct drm_device *dev = fb->dev;
79e53945 14841 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
79e53945 14842
ef2d633e 14843 drm_framebuffer_cleanup(fb);
60a5ca01 14844 mutex_lock(&dev->struct_mutex);
ef2d633e 14845 WARN_ON(!intel_fb->obj->framebuffer_references--);
f8c417cd 14846 i915_gem_object_put(intel_fb->obj);
60a5ca01 14847 mutex_unlock(&dev->struct_mutex);
79e53945
JB
14848 kfree(intel_fb);
14849}
14850
14851static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
05394f39 14852 struct drm_file *file,
79e53945
JB
14853 unsigned int *handle)
14854{
14855 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
05394f39 14856 struct drm_i915_gem_object *obj = intel_fb->obj;
79e53945 14857
cc917ab4
CW
14858 if (obj->userptr.mm) {
14859 DRM_DEBUG("attempting to use a userptr for a framebuffer, denied\n");
14860 return -EINVAL;
14861 }
14862
05394f39 14863 return drm_gem_handle_create(file, &obj->base, handle);
79e53945
JB
14864}
14865
86c98588
RV
14866static int intel_user_framebuffer_dirty(struct drm_framebuffer *fb,
14867 struct drm_file *file,
14868 unsigned flags, unsigned color,
14869 struct drm_clip_rect *clips,
14870 unsigned num_clips)
14871{
14872 struct drm_device *dev = fb->dev;
14873 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
14874 struct drm_i915_gem_object *obj = intel_fb->obj;
14875
14876 mutex_lock(&dev->struct_mutex);
74b4ea1e 14877 intel_fb_obj_flush(obj, false, ORIGIN_DIRTYFB);
86c98588
RV
14878 mutex_unlock(&dev->struct_mutex);
14879
14880 return 0;
14881}
14882
79e53945
JB
14883static const struct drm_framebuffer_funcs intel_fb_funcs = {
14884 .destroy = intel_user_framebuffer_destroy,
14885 .create_handle = intel_user_framebuffer_create_handle,
86c98588 14886 .dirty = intel_user_framebuffer_dirty,
79e53945
JB
14887};
14888
b321803d
DL
14889static
14890u32 intel_fb_pitch_limit(struct drm_device *dev, uint64_t fb_modifier,
14891 uint32_t pixel_format)
14892{
14893 u32 gen = INTEL_INFO(dev)->gen;
14894
14895 if (gen >= 9) {
ac484963
VS
14896 int cpp = drm_format_plane_cpp(pixel_format, 0);
14897
b321803d
DL
14898 /* "The stride in bytes must not exceed the of the size of 8K
14899 * pixels and 32K bytes."
14900 */
ac484963 14901 return min(8192 * cpp, 32768);
666a4537 14902 } else if (gen >= 5 && !IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
b321803d
DL
14903 return 32*1024;
14904 } else if (gen >= 4) {
14905 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14906 return 16*1024;
14907 else
14908 return 32*1024;
14909 } else if (gen >= 3) {
14910 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14911 return 8*1024;
14912 else
14913 return 16*1024;
14914 } else {
14915 /* XXX DSPC is limited to 4k tiled */
14916 return 8*1024;
14917 }
14918}
14919
b5ea642a
DV
14920static int intel_framebuffer_init(struct drm_device *dev,
14921 struct intel_framebuffer *intel_fb,
14922 struct drm_mode_fb_cmd2 *mode_cmd,
14923 struct drm_i915_gem_object *obj)
79e53945 14924{
7b49f948 14925 struct drm_i915_private *dev_priv = to_i915(dev);
6761dd31 14926 unsigned int aligned_height;
79e53945 14927 int ret;
b321803d 14928 u32 pitch_limit, stride_alignment;
79e53945 14929
dd4916c5
DV
14930 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
14931
2a80eada
DV
14932 if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
14933 /* Enforce that fb modifier and tiling mode match, but only for
14934 * X-tiled. This is needed for FBC. */
14935 if (!!(obj->tiling_mode == I915_TILING_X) !=
14936 !!(mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED)) {
14937 DRM_DEBUG("tiling_mode doesn't match fb modifier\n");
14938 return -EINVAL;
14939 }
14940 } else {
14941 if (obj->tiling_mode == I915_TILING_X)
14942 mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
14943 else if (obj->tiling_mode == I915_TILING_Y) {
14944 DRM_DEBUG("No Y tiling for legacy addfb\n");
14945 return -EINVAL;
14946 }
14947 }
14948
9a8f0a12
TU
14949 /* Passed in modifier sanity checking. */
14950 switch (mode_cmd->modifier[0]) {
14951 case I915_FORMAT_MOD_Y_TILED:
14952 case I915_FORMAT_MOD_Yf_TILED:
14953 if (INTEL_INFO(dev)->gen < 9) {
14954 DRM_DEBUG("Unsupported tiling 0x%llx!\n",
14955 mode_cmd->modifier[0]);
14956 return -EINVAL;
14957 }
14958 case DRM_FORMAT_MOD_NONE:
14959 case I915_FORMAT_MOD_X_TILED:
14960 break;
14961 default:
c0f40428
JB
14962 DRM_DEBUG("Unsupported fb modifier 0x%llx!\n",
14963 mode_cmd->modifier[0]);
57cd6508 14964 return -EINVAL;
c16ed4be 14965 }
57cd6508 14966
7b49f948
VS
14967 stride_alignment = intel_fb_stride_alignment(dev_priv,
14968 mode_cmd->modifier[0],
b321803d
DL
14969 mode_cmd->pixel_format);
14970 if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
14971 DRM_DEBUG("pitch (%d) must be at least %u byte aligned\n",
14972 mode_cmd->pitches[0], stride_alignment);
57cd6508 14973 return -EINVAL;
c16ed4be 14974 }
57cd6508 14975
b321803d
DL
14976 pitch_limit = intel_fb_pitch_limit(dev, mode_cmd->modifier[0],
14977 mode_cmd->pixel_format);
a35cdaa0 14978 if (mode_cmd->pitches[0] > pitch_limit) {
b321803d
DL
14979 DRM_DEBUG("%s pitch (%u) must be at less than %d\n",
14980 mode_cmd->modifier[0] != DRM_FORMAT_MOD_NONE ?
2a80eada 14981 "tiled" : "linear",
a35cdaa0 14982 mode_cmd->pitches[0], pitch_limit);
5d7bd705 14983 return -EINVAL;
c16ed4be 14984 }
5d7bd705 14985
2a80eada 14986 if (mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED &&
c16ed4be
CW
14987 mode_cmd->pitches[0] != obj->stride) {
14988 DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
14989 mode_cmd->pitches[0], obj->stride);
5d7bd705 14990 return -EINVAL;
c16ed4be 14991 }
5d7bd705 14992
57779d06 14993 /* Reject formats not supported by any plane early. */
308e5bcb 14994 switch (mode_cmd->pixel_format) {
57779d06 14995 case DRM_FORMAT_C8:
04b3924d
VS
14996 case DRM_FORMAT_RGB565:
14997 case DRM_FORMAT_XRGB8888:
14998 case DRM_FORMAT_ARGB8888:
57779d06
VS
14999 break;
15000 case DRM_FORMAT_XRGB1555:
c16ed4be 15001 if (INTEL_INFO(dev)->gen > 3) {
4ee62c76
VS
15002 DRM_DEBUG("unsupported pixel format: %s\n",
15003 drm_get_format_name(mode_cmd->pixel_format));
57779d06 15004 return -EINVAL;
c16ed4be 15005 }
57779d06 15006 break;
57779d06 15007 case DRM_FORMAT_ABGR8888:
666a4537
WB
15008 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev) &&
15009 INTEL_INFO(dev)->gen < 9) {
6c0fd451
DL
15010 DRM_DEBUG("unsupported pixel format: %s\n",
15011 drm_get_format_name(mode_cmd->pixel_format));
15012 return -EINVAL;
15013 }
15014 break;
15015 case DRM_FORMAT_XBGR8888:
04b3924d 15016 case DRM_FORMAT_XRGB2101010:
57779d06 15017 case DRM_FORMAT_XBGR2101010:
c16ed4be 15018 if (INTEL_INFO(dev)->gen < 4) {
4ee62c76
VS
15019 DRM_DEBUG("unsupported pixel format: %s\n",
15020 drm_get_format_name(mode_cmd->pixel_format));
57779d06 15021 return -EINVAL;
c16ed4be 15022 }
b5626747 15023 break;
7531208b 15024 case DRM_FORMAT_ABGR2101010:
666a4537 15025 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
7531208b
DL
15026 DRM_DEBUG("unsupported pixel format: %s\n",
15027 drm_get_format_name(mode_cmd->pixel_format));
15028 return -EINVAL;
15029 }
15030 break;
04b3924d
VS
15031 case DRM_FORMAT_YUYV:
15032 case DRM_FORMAT_UYVY:
15033 case DRM_FORMAT_YVYU:
15034 case DRM_FORMAT_VYUY:
c16ed4be 15035 if (INTEL_INFO(dev)->gen < 5) {
4ee62c76
VS
15036 DRM_DEBUG("unsupported pixel format: %s\n",
15037 drm_get_format_name(mode_cmd->pixel_format));
57779d06 15038 return -EINVAL;
c16ed4be 15039 }
57cd6508
CW
15040 break;
15041 default:
4ee62c76
VS
15042 DRM_DEBUG("unsupported pixel format: %s\n",
15043 drm_get_format_name(mode_cmd->pixel_format));
57cd6508
CW
15044 return -EINVAL;
15045 }
15046
90f9a336
VS
15047 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
15048 if (mode_cmd->offsets[0] != 0)
15049 return -EINVAL;
15050
ec2c981e 15051 aligned_height = intel_fb_align_height(dev, mode_cmd->height,
091df6cb
DV
15052 mode_cmd->pixel_format,
15053 mode_cmd->modifier[0]);
53155c0a
DV
15054 /* FIXME drm helper for size checks (especially planar formats)? */
15055 if (obj->base.size < aligned_height * mode_cmd->pitches[0])
15056 return -EINVAL;
15057
c7d73f6a
DV
15058 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
15059 intel_fb->obj = obj;
15060
2d7a215f
VS
15061 intel_fill_fb_info(dev_priv, &intel_fb->base);
15062
79e53945
JB
15063 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
15064 if (ret) {
15065 DRM_ERROR("framebuffer init failed %d\n", ret);
15066 return ret;
15067 }
15068
0b05e1e0
VS
15069 intel_fb->obj->framebuffer_references++;
15070
79e53945
JB
15071 return 0;
15072}
15073
79e53945
JB
15074static struct drm_framebuffer *
15075intel_user_framebuffer_create(struct drm_device *dev,
15076 struct drm_file *filp,
1eb83451 15077 const struct drm_mode_fb_cmd2 *user_mode_cmd)
79e53945 15078{
dcb1394e 15079 struct drm_framebuffer *fb;
05394f39 15080 struct drm_i915_gem_object *obj;
76dc3769 15081 struct drm_mode_fb_cmd2 mode_cmd = *user_mode_cmd;
79e53945 15082
03ac0642
CW
15083 obj = i915_gem_object_lookup(filp, mode_cmd.handles[0]);
15084 if (!obj)
cce13ff7 15085 return ERR_PTR(-ENOENT);
79e53945 15086
92907cbb 15087 fb = intel_framebuffer_create(dev, &mode_cmd, obj);
dcb1394e 15088 if (IS_ERR(fb))
34911fd3 15089 i915_gem_object_put_unlocked(obj);
dcb1394e
LW
15090
15091 return fb;
79e53945
JB
15092}
15093
0695726e 15094#ifndef CONFIG_DRM_FBDEV_EMULATION
0632fef6 15095static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
4520f53a
DV
15096{
15097}
15098#endif
15099
79e53945 15100static const struct drm_mode_config_funcs intel_mode_funcs = {
79e53945 15101 .fb_create = intel_user_framebuffer_create,
0632fef6 15102 .output_poll_changed = intel_fbdev_output_poll_changed,
5ee67f1c
MR
15103 .atomic_check = intel_atomic_check,
15104 .atomic_commit = intel_atomic_commit,
de419ab6
ML
15105 .atomic_state_alloc = intel_atomic_state_alloc,
15106 .atomic_state_clear = intel_atomic_state_clear,
79e53945
JB
15107};
15108
88212941
ID
15109/**
15110 * intel_init_display_hooks - initialize the display modesetting hooks
15111 * @dev_priv: device private
15112 */
15113void intel_init_display_hooks(struct drm_i915_private *dev_priv)
e70236a8 15114{
88212941 15115 if (INTEL_INFO(dev_priv)->gen >= 9) {
bc8d7dff 15116 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
5724dbd1
DL
15117 dev_priv->display.get_initial_plane_config =
15118 skylake_get_initial_plane_config;
bc8d7dff
DL
15119 dev_priv->display.crtc_compute_clock =
15120 haswell_crtc_compute_clock;
15121 dev_priv->display.crtc_enable = haswell_crtc_enable;
15122 dev_priv->display.crtc_disable = haswell_crtc_disable;
88212941 15123 } else if (HAS_DDI(dev_priv)) {
0e8ffe1b 15124 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
5724dbd1
DL
15125 dev_priv->display.get_initial_plane_config =
15126 ironlake_get_initial_plane_config;
797d0259
ACO
15127 dev_priv->display.crtc_compute_clock =
15128 haswell_crtc_compute_clock;
4f771f10
PZ
15129 dev_priv->display.crtc_enable = haswell_crtc_enable;
15130 dev_priv->display.crtc_disable = haswell_crtc_disable;
88212941 15131 } else if (HAS_PCH_SPLIT(dev_priv)) {
0e8ffe1b 15132 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
5724dbd1
DL
15133 dev_priv->display.get_initial_plane_config =
15134 ironlake_get_initial_plane_config;
3fb37703
ACO
15135 dev_priv->display.crtc_compute_clock =
15136 ironlake_crtc_compute_clock;
76e5a89c
DV
15137 dev_priv->display.crtc_enable = ironlake_crtc_enable;
15138 dev_priv->display.crtc_disable = ironlake_crtc_disable;
65b3d6a9 15139 } else if (IS_CHERRYVIEW(dev_priv)) {
89b667f8 15140 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
5724dbd1
DL
15141 dev_priv->display.get_initial_plane_config =
15142 i9xx_get_initial_plane_config;
65b3d6a9
ACO
15143 dev_priv->display.crtc_compute_clock = chv_crtc_compute_clock;
15144 dev_priv->display.crtc_enable = valleyview_crtc_enable;
15145 dev_priv->display.crtc_disable = i9xx_crtc_disable;
15146 } else if (IS_VALLEYVIEW(dev_priv)) {
15147 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
15148 dev_priv->display.get_initial_plane_config =
15149 i9xx_get_initial_plane_config;
15150 dev_priv->display.crtc_compute_clock = vlv_crtc_compute_clock;
89b667f8
JB
15151 dev_priv->display.crtc_enable = valleyview_crtc_enable;
15152 dev_priv->display.crtc_disable = i9xx_crtc_disable;
19ec6693
ACO
15153 } else if (IS_G4X(dev_priv)) {
15154 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
15155 dev_priv->display.get_initial_plane_config =
15156 i9xx_get_initial_plane_config;
15157 dev_priv->display.crtc_compute_clock = g4x_crtc_compute_clock;
15158 dev_priv->display.crtc_enable = i9xx_crtc_enable;
15159 dev_priv->display.crtc_disable = i9xx_crtc_disable;
70e8aa21
ACO
15160 } else if (IS_PINEVIEW(dev_priv)) {
15161 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
15162 dev_priv->display.get_initial_plane_config =
15163 i9xx_get_initial_plane_config;
15164 dev_priv->display.crtc_compute_clock = pnv_crtc_compute_clock;
15165 dev_priv->display.crtc_enable = i9xx_crtc_enable;
15166 dev_priv->display.crtc_disable = i9xx_crtc_disable;
81c97f52 15167 } else if (!IS_GEN2(dev_priv)) {
0e8ffe1b 15168 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
5724dbd1
DL
15169 dev_priv->display.get_initial_plane_config =
15170 i9xx_get_initial_plane_config;
d6dfee7a 15171 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
76e5a89c
DV
15172 dev_priv->display.crtc_enable = i9xx_crtc_enable;
15173 dev_priv->display.crtc_disable = i9xx_crtc_disable;
81c97f52
ACO
15174 } else {
15175 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
15176 dev_priv->display.get_initial_plane_config =
15177 i9xx_get_initial_plane_config;
15178 dev_priv->display.crtc_compute_clock = i8xx_crtc_compute_clock;
15179 dev_priv->display.crtc_enable = i9xx_crtc_enable;
15180 dev_priv->display.crtc_disable = i9xx_crtc_disable;
f564048e 15181 }
e70236a8 15182
e70236a8 15183 /* Returns the core display clock speed */
88212941 15184 if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv))
1652d19e
VS
15185 dev_priv->display.get_display_clock_speed =
15186 skylake_get_display_clock_speed;
88212941 15187 else if (IS_BROXTON(dev_priv))
acd3f3d3
BP
15188 dev_priv->display.get_display_clock_speed =
15189 broxton_get_display_clock_speed;
88212941 15190 else if (IS_BROADWELL(dev_priv))
1652d19e
VS
15191 dev_priv->display.get_display_clock_speed =
15192 broadwell_get_display_clock_speed;
88212941 15193 else if (IS_HASWELL(dev_priv))
1652d19e
VS
15194 dev_priv->display.get_display_clock_speed =
15195 haswell_get_display_clock_speed;
88212941 15196 else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
25eb05fc
JB
15197 dev_priv->display.get_display_clock_speed =
15198 valleyview_get_display_clock_speed;
88212941 15199 else if (IS_GEN5(dev_priv))
b37a6434
VS
15200 dev_priv->display.get_display_clock_speed =
15201 ilk_get_display_clock_speed;
88212941
ID
15202 else if (IS_I945G(dev_priv) || IS_BROADWATER(dev_priv) ||
15203 IS_GEN6(dev_priv) || IS_IVYBRIDGE(dev_priv))
e70236a8
JB
15204 dev_priv->display.get_display_clock_speed =
15205 i945_get_display_clock_speed;
88212941 15206 else if (IS_GM45(dev_priv))
34edce2f
VS
15207 dev_priv->display.get_display_clock_speed =
15208 gm45_get_display_clock_speed;
88212941 15209 else if (IS_CRESTLINE(dev_priv))
34edce2f
VS
15210 dev_priv->display.get_display_clock_speed =
15211 i965gm_get_display_clock_speed;
88212941 15212 else if (IS_PINEVIEW(dev_priv))
34edce2f
VS
15213 dev_priv->display.get_display_clock_speed =
15214 pnv_get_display_clock_speed;
88212941 15215 else if (IS_G33(dev_priv) || IS_G4X(dev_priv))
34edce2f
VS
15216 dev_priv->display.get_display_clock_speed =
15217 g33_get_display_clock_speed;
88212941 15218 else if (IS_I915G(dev_priv))
e70236a8
JB
15219 dev_priv->display.get_display_clock_speed =
15220 i915_get_display_clock_speed;
88212941 15221 else if (IS_I945GM(dev_priv) || IS_845G(dev_priv))
e70236a8
JB
15222 dev_priv->display.get_display_clock_speed =
15223 i9xx_misc_get_display_clock_speed;
88212941 15224 else if (IS_I915GM(dev_priv))
e70236a8
JB
15225 dev_priv->display.get_display_clock_speed =
15226 i915gm_get_display_clock_speed;
88212941 15227 else if (IS_I865G(dev_priv))
e70236a8
JB
15228 dev_priv->display.get_display_clock_speed =
15229 i865_get_display_clock_speed;
88212941 15230 else if (IS_I85X(dev_priv))
e70236a8 15231 dev_priv->display.get_display_clock_speed =
1b1d2716 15232 i85x_get_display_clock_speed;
623e01e5 15233 else { /* 830 */
88212941 15234 WARN(!IS_I830(dev_priv), "Unknown platform. Assuming 133 MHz CDCLK\n");
e70236a8
JB
15235 dev_priv->display.get_display_clock_speed =
15236 i830_get_display_clock_speed;
623e01e5 15237 }
e70236a8 15238
88212941 15239 if (IS_GEN5(dev_priv)) {
3bb11b53 15240 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
88212941 15241 } else if (IS_GEN6(dev_priv)) {
3bb11b53 15242 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
88212941 15243 } else if (IS_IVYBRIDGE(dev_priv)) {
3bb11b53
SJ
15244 /* FIXME: detect B0+ stepping and use auto training */
15245 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
88212941 15246 } else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
3bb11b53 15247 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
445e780b
VS
15248 }
15249
15250 if (IS_BROADWELL(dev_priv)) {
15251 dev_priv->display.modeset_commit_cdclk =
15252 broadwell_modeset_commit_cdclk;
15253 dev_priv->display.modeset_calc_cdclk =
15254 broadwell_modeset_calc_cdclk;
88212941 15255 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
27c329ed
ML
15256 dev_priv->display.modeset_commit_cdclk =
15257 valleyview_modeset_commit_cdclk;
15258 dev_priv->display.modeset_calc_cdclk =
15259 valleyview_modeset_calc_cdclk;
88212941 15260 } else if (IS_BROXTON(dev_priv)) {
27c329ed 15261 dev_priv->display.modeset_commit_cdclk =
324513c0 15262 bxt_modeset_commit_cdclk;
27c329ed 15263 dev_priv->display.modeset_calc_cdclk =
324513c0 15264 bxt_modeset_calc_cdclk;
c89e39f3
CT
15265 } else if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv)) {
15266 dev_priv->display.modeset_commit_cdclk =
15267 skl_modeset_commit_cdclk;
15268 dev_priv->display.modeset_calc_cdclk =
15269 skl_modeset_calc_cdclk;
e70236a8 15270 }
5a21b665
DV
15271
15272 switch (INTEL_INFO(dev_priv)->gen) {
15273 case 2:
15274 dev_priv->display.queue_flip = intel_gen2_queue_flip;
15275 break;
15276
15277 case 3:
15278 dev_priv->display.queue_flip = intel_gen3_queue_flip;
15279 break;
15280
15281 case 4:
15282 case 5:
15283 dev_priv->display.queue_flip = intel_gen4_queue_flip;
15284 break;
15285
15286 case 6:
15287 dev_priv->display.queue_flip = intel_gen6_queue_flip;
15288 break;
15289 case 7:
15290 case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
15291 dev_priv->display.queue_flip = intel_gen7_queue_flip;
15292 break;
15293 case 9:
15294 /* Drop through - unsupported since execlist only. */
15295 default:
15296 /* Default just returns -ENODEV to indicate unsupported */
15297 dev_priv->display.queue_flip = intel_default_queue_flip;
15298 }
e70236a8
JB
15299}
15300
b690e96c
JB
15301/*
15302 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
15303 * resume, or other times. This quirk makes sure that's the case for
15304 * affected systems.
15305 */
0206e353 15306static void quirk_pipea_force(struct drm_device *dev)
b690e96c 15307{
fac5e23e 15308 struct drm_i915_private *dev_priv = to_i915(dev);
b690e96c
JB
15309
15310 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
bc0daf48 15311 DRM_INFO("applying pipe a force quirk\n");
b690e96c
JB
15312}
15313
b6b5d049
VS
15314static void quirk_pipeb_force(struct drm_device *dev)
15315{
fac5e23e 15316 struct drm_i915_private *dev_priv = to_i915(dev);
b6b5d049
VS
15317
15318 dev_priv->quirks |= QUIRK_PIPEB_FORCE;
15319 DRM_INFO("applying pipe b force quirk\n");
15320}
15321
435793df
KP
15322/*
15323 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
15324 */
15325static void quirk_ssc_force_disable(struct drm_device *dev)
15326{
fac5e23e 15327 struct drm_i915_private *dev_priv = to_i915(dev);
435793df 15328 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
bc0daf48 15329 DRM_INFO("applying lvds SSC disable quirk\n");
435793df
KP
15330}
15331
4dca20ef 15332/*
5a15ab5b
CE
15333 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
15334 * brightness value
4dca20ef
CE
15335 */
15336static void quirk_invert_brightness(struct drm_device *dev)
15337{
fac5e23e 15338 struct drm_i915_private *dev_priv = to_i915(dev);
4dca20ef 15339 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
bc0daf48 15340 DRM_INFO("applying inverted panel brightness quirk\n");
435793df
KP
15341}
15342
9c72cc6f
SD
15343/* Some VBT's incorrectly indicate no backlight is present */
15344static void quirk_backlight_present(struct drm_device *dev)
15345{
fac5e23e 15346 struct drm_i915_private *dev_priv = to_i915(dev);
9c72cc6f
SD
15347 dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
15348 DRM_INFO("applying backlight present quirk\n");
15349}
15350
b690e96c
JB
15351struct intel_quirk {
15352 int device;
15353 int subsystem_vendor;
15354 int subsystem_device;
15355 void (*hook)(struct drm_device *dev);
15356};
15357
5f85f176
EE
15358/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
15359struct intel_dmi_quirk {
15360 void (*hook)(struct drm_device *dev);
15361 const struct dmi_system_id (*dmi_id_list)[];
15362};
15363
15364static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
15365{
15366 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
15367 return 1;
15368}
15369
15370static const struct intel_dmi_quirk intel_dmi_quirks[] = {
15371 {
15372 .dmi_id_list = &(const struct dmi_system_id[]) {
15373 {
15374 .callback = intel_dmi_reverse_brightness,
15375 .ident = "NCR Corporation",
15376 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
15377 DMI_MATCH(DMI_PRODUCT_NAME, ""),
15378 },
15379 },
15380 { } /* terminating entry */
15381 },
15382 .hook = quirk_invert_brightness,
15383 },
15384};
15385
c43b5634 15386static struct intel_quirk intel_quirks[] = {
b690e96c
JB
15387 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
15388 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
15389
b690e96c
JB
15390 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
15391 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
15392
5f080c0f
VS
15393 /* 830 needs to leave pipe A & dpll A up */
15394 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
15395
b6b5d049
VS
15396 /* 830 needs to leave pipe B & dpll B up */
15397 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
15398
435793df
KP
15399 /* Lenovo U160 cannot use SSC on LVDS */
15400 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
070d329a
MAS
15401
15402 /* Sony Vaio Y cannot use SSC on LVDS */
15403 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
5a15ab5b 15404
be505f64
AH
15405 /* Acer Aspire 5734Z must invert backlight brightness */
15406 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
15407
15408 /* Acer/eMachines G725 */
15409 { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
15410
15411 /* Acer/eMachines e725 */
15412 { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
15413
15414 /* Acer/Packard Bell NCL20 */
15415 { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
15416
15417 /* Acer Aspire 4736Z */
15418 { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
0f540c3a
JN
15419
15420 /* Acer Aspire 5336 */
15421 { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
2e93a1aa
SD
15422
15423 /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
15424 { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
d4967d8c 15425
dfb3d47b
SD
15426 /* Acer C720 Chromebook (Core i3 4005U) */
15427 { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
15428
b2a9601c 15429 /* Apple Macbook 2,1 (Core 2 T7400) */
15430 { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
15431
1b9448b0
JN
15432 /* Apple Macbook 4,1 */
15433 { 0x2a02, 0x106b, 0x00a1, quirk_backlight_present },
15434
d4967d8c
SD
15435 /* Toshiba CB35 Chromebook (Celeron 2955U) */
15436 { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
724cb06f
SD
15437
15438 /* HP Chromebook 14 (Celeron 2955U) */
15439 { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
cf6f0af9
JN
15440
15441 /* Dell Chromebook 11 */
15442 { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
9be64eee
JN
15443
15444 /* Dell Chromebook 11 (2015 version) */
15445 { 0x0a16, 0x1028, 0x0a35, quirk_backlight_present },
b690e96c
JB
15446};
15447
15448static void intel_init_quirks(struct drm_device *dev)
15449{
15450 struct pci_dev *d = dev->pdev;
15451 int i;
15452
15453 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
15454 struct intel_quirk *q = &intel_quirks[i];
15455
15456 if (d->device == q->device &&
15457 (d->subsystem_vendor == q->subsystem_vendor ||
15458 q->subsystem_vendor == PCI_ANY_ID) &&
15459 (d->subsystem_device == q->subsystem_device ||
15460 q->subsystem_device == PCI_ANY_ID))
15461 q->hook(dev);
15462 }
5f85f176
EE
15463 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
15464 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
15465 intel_dmi_quirks[i].hook(dev);
15466 }
b690e96c
JB
15467}
15468
9cce37f4
JB
15469/* Disable the VGA plane that we never use */
15470static void i915_disable_vga(struct drm_device *dev)
15471{
fac5e23e 15472 struct drm_i915_private *dev_priv = to_i915(dev);
9cce37f4 15473 u8 sr1;
f0f59a00 15474 i915_reg_t vga_reg = i915_vgacntrl_reg(dev);
9cce37f4 15475
2b37c616 15476 /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
9cce37f4 15477 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
3fdcf431 15478 outb(SR01, VGA_SR_INDEX);
9cce37f4
JB
15479 sr1 = inb(VGA_SR_DATA);
15480 outb(sr1 | 1<<5, VGA_SR_DATA);
15481 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
15482 udelay(300);
15483
01f5a626 15484 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
9cce37f4
JB
15485 POSTING_READ(vga_reg);
15486}
15487
f817586c
DV
15488void intel_modeset_init_hw(struct drm_device *dev)
15489{
fac5e23e 15490 struct drm_i915_private *dev_priv = to_i915(dev);
1a617b77 15491
b6283055 15492 intel_update_cdclk(dev);
1a617b77
ML
15493
15494 dev_priv->atomic_cdclk_freq = dev_priv->cdclk_freq;
15495
f817586c 15496 intel_init_clock_gating(dev);
f817586c
DV
15497}
15498
d93c0372
MR
15499/*
15500 * Calculate what we think the watermarks should be for the state we've read
15501 * out of the hardware and then immediately program those watermarks so that
15502 * we ensure the hardware settings match our internal state.
15503 *
15504 * We can calculate what we think WM's should be by creating a duplicate of the
15505 * current state (which was constructed during hardware readout) and running it
15506 * through the atomic check code to calculate new watermark values in the
15507 * state object.
15508 */
15509static void sanitize_watermarks(struct drm_device *dev)
15510{
15511 struct drm_i915_private *dev_priv = to_i915(dev);
15512 struct drm_atomic_state *state;
15513 struct drm_crtc *crtc;
15514 struct drm_crtc_state *cstate;
15515 struct drm_modeset_acquire_ctx ctx;
15516 int ret;
15517 int i;
15518
15519 /* Only supported on platforms that use atomic watermark design */
ed4a6a7c 15520 if (!dev_priv->display.optimize_watermarks)
d93c0372
MR
15521 return;
15522
15523 /*
15524 * We need to hold connection_mutex before calling duplicate_state so
15525 * that the connector loop is protected.
15526 */
15527 drm_modeset_acquire_init(&ctx, 0);
15528retry:
0cd1262d 15529 ret = drm_modeset_lock_all_ctx(dev, &ctx);
d93c0372
MR
15530 if (ret == -EDEADLK) {
15531 drm_modeset_backoff(&ctx);
15532 goto retry;
15533 } else if (WARN_ON(ret)) {
0cd1262d 15534 goto fail;
d93c0372
MR
15535 }
15536
15537 state = drm_atomic_helper_duplicate_state(dev, &ctx);
15538 if (WARN_ON(IS_ERR(state)))
0cd1262d 15539 goto fail;
d93c0372 15540
ed4a6a7c
MR
15541 /*
15542 * Hardware readout is the only time we don't want to calculate
15543 * intermediate watermarks (since we don't trust the current
15544 * watermarks).
15545 */
15546 to_intel_atomic_state(state)->skip_intermediate_wm = true;
15547
d93c0372
MR
15548 ret = intel_atomic_check(dev, state);
15549 if (ret) {
15550 /*
15551 * If we fail here, it means that the hardware appears to be
15552 * programmed in a way that shouldn't be possible, given our
15553 * understanding of watermark requirements. This might mean a
15554 * mistake in the hardware readout code or a mistake in the
15555 * watermark calculations for a given platform. Raise a WARN
15556 * so that this is noticeable.
15557 *
15558 * If this actually happens, we'll have to just leave the
15559 * BIOS-programmed watermarks untouched and hope for the best.
15560 */
15561 WARN(true, "Could not determine valid watermarks for inherited state\n");
0cd1262d 15562 goto fail;
d93c0372
MR
15563 }
15564
15565 /* Write calculated watermark values back */
d93c0372
MR
15566 for_each_crtc_in_state(state, crtc, cstate, i) {
15567 struct intel_crtc_state *cs = to_intel_crtc_state(cstate);
15568
ed4a6a7c
MR
15569 cs->wm.need_postvbl_update = true;
15570 dev_priv->display.optimize_watermarks(cs);
d93c0372
MR
15571 }
15572
15573 drm_atomic_state_free(state);
0cd1262d 15574fail:
d93c0372
MR
15575 drm_modeset_drop_locks(&ctx);
15576 drm_modeset_acquire_fini(&ctx);
15577}
15578
79e53945
JB
15579void intel_modeset_init(struct drm_device *dev)
15580{
72e96d64
JL
15581 struct drm_i915_private *dev_priv = to_i915(dev);
15582 struct i915_ggtt *ggtt = &dev_priv->ggtt;
1fe47785 15583 int sprite, ret;
8cc87b75 15584 enum pipe pipe;
46f297fb 15585 struct intel_crtc *crtc;
79e53945
JB
15586
15587 drm_mode_config_init(dev);
15588
15589 dev->mode_config.min_width = 0;
15590 dev->mode_config.min_height = 0;
15591
019d96cb
DA
15592 dev->mode_config.preferred_depth = 24;
15593 dev->mode_config.prefer_shadow = 1;
15594
25bab385
TU
15595 dev->mode_config.allow_fb_modifiers = true;
15596
e6ecefaa 15597 dev->mode_config.funcs = &intel_mode_funcs;
79e53945 15598
b690e96c
JB
15599 intel_init_quirks(dev);
15600
1fa61106
ED
15601 intel_init_pm(dev);
15602
e3c74757
BW
15603 if (INTEL_INFO(dev)->num_pipes == 0)
15604 return;
15605
69f92f67
LW
15606 /*
15607 * There may be no VBT; and if the BIOS enabled SSC we can
15608 * just keep using it to avoid unnecessary flicker. Whereas if the
15609 * BIOS isn't using it, don't assume it will work even if the VBT
15610 * indicates as much.
15611 */
15612 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)) {
15613 bool bios_lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
15614 DREF_SSC1_ENABLE);
15615
15616 if (dev_priv->vbt.lvds_use_ssc != bios_lvds_use_ssc) {
15617 DRM_DEBUG_KMS("SSC %sabled by BIOS, overriding VBT which says %sabled\n",
15618 bios_lvds_use_ssc ? "en" : "dis",
15619 dev_priv->vbt.lvds_use_ssc ? "en" : "dis");
15620 dev_priv->vbt.lvds_use_ssc = bios_lvds_use_ssc;
15621 }
15622 }
15623
a6c45cf0
CW
15624 if (IS_GEN2(dev)) {
15625 dev->mode_config.max_width = 2048;
15626 dev->mode_config.max_height = 2048;
15627 } else if (IS_GEN3(dev)) {
5e4d6fa7
KP
15628 dev->mode_config.max_width = 4096;
15629 dev->mode_config.max_height = 4096;
79e53945 15630 } else {
a6c45cf0
CW
15631 dev->mode_config.max_width = 8192;
15632 dev->mode_config.max_height = 8192;
79e53945 15633 }
068be561 15634
dc41c154
VS
15635 if (IS_845G(dev) || IS_I865G(dev)) {
15636 dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
15637 dev->mode_config.cursor_height = 1023;
15638 } else if (IS_GEN2(dev)) {
068be561
DL
15639 dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
15640 dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
15641 } else {
15642 dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
15643 dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
15644 }
15645
72e96d64 15646 dev->mode_config.fb_base = ggtt->mappable_base;
79e53945 15647
28c97730 15648 DRM_DEBUG_KMS("%d display pipe%s available.\n",
7eb552ae
BW
15649 INTEL_INFO(dev)->num_pipes,
15650 INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
79e53945 15651
055e393f 15652 for_each_pipe(dev_priv, pipe) {
8cc87b75 15653 intel_crtc_init(dev, pipe);
3bdcfc0c 15654 for_each_sprite(dev_priv, pipe, sprite) {
1fe47785 15655 ret = intel_plane_init(dev, pipe, sprite);
7f1f3851 15656 if (ret)
06da8da2 15657 DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
1fe47785 15658 pipe_name(pipe), sprite_name(pipe, sprite), ret);
7f1f3851 15659 }
79e53945
JB
15660 }
15661
bfa7df01
VS
15662 intel_update_czclk(dev_priv);
15663 intel_update_cdclk(dev);
15664
e72f9fbf 15665 intel_shared_dpll_init(dev);
ee7b9f93 15666
b2045352
VS
15667 if (dev_priv->max_cdclk_freq == 0)
15668 intel_update_max_cdclk(dev);
15669
9cce37f4
JB
15670 /* Just disable it once at startup */
15671 i915_disable_vga(dev);
79e53945 15672 intel_setup_outputs(dev);
11be49eb 15673
6e9f798d 15674 drm_modeset_lock_all(dev);
043e9bda 15675 intel_modeset_setup_hw_state(dev);
6e9f798d 15676 drm_modeset_unlock_all(dev);
46f297fb 15677
d3fcc808 15678 for_each_intel_crtc(dev, crtc) {
eeebeac5
ML
15679 struct intel_initial_plane_config plane_config = {};
15680
46f297fb
JB
15681 if (!crtc->active)
15682 continue;
15683
46f297fb 15684 /*
46f297fb
JB
15685 * Note that reserving the BIOS fb up front prevents us
15686 * from stuffing other stolen allocations like the ring
15687 * on top. This prevents some ugliness at boot time, and
15688 * can even allow for smooth boot transitions if the BIOS
15689 * fb is large enough for the active pipe configuration.
15690 */
eeebeac5
ML
15691 dev_priv->display.get_initial_plane_config(crtc,
15692 &plane_config);
15693
15694 /*
15695 * If the fb is shared between multiple heads, we'll
15696 * just get the first one.
15697 */
15698 intel_find_initial_plane_obj(crtc, &plane_config);
46f297fb 15699 }
d93c0372
MR
15700
15701 /*
15702 * Make sure hardware watermarks really match the state we read out.
15703 * Note that we need to do this after reconstructing the BIOS fb's
15704 * since the watermark calculation done here will use pstate->fb.
15705 */
15706 sanitize_watermarks(dev);
2c7111db
CW
15707}
15708
7fad798e
DV
15709static void intel_enable_pipe_a(struct drm_device *dev)
15710{
15711 struct intel_connector *connector;
15712 struct drm_connector *crt = NULL;
15713 struct intel_load_detect_pipe load_detect_temp;
208bf9fd 15714 struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
7fad798e
DV
15715
15716 /* We can't just switch on the pipe A, we need to set things up with a
15717 * proper mode and output configuration. As a gross hack, enable pipe A
15718 * by enabling the load detect pipe once. */
3a3371ff 15719 for_each_intel_connector(dev, connector) {
7fad798e
DV
15720 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
15721 crt = &connector->base;
15722 break;
15723 }
15724 }
15725
15726 if (!crt)
15727 return;
15728
208bf9fd 15729 if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
49172fee 15730 intel_release_load_detect_pipe(crt, &load_detect_temp, ctx);
7fad798e
DV
15731}
15732
fa555837
DV
15733static bool
15734intel_check_plane_mapping(struct intel_crtc *crtc)
15735{
7eb552ae 15736 struct drm_device *dev = crtc->base.dev;
fac5e23e 15737 struct drm_i915_private *dev_priv = to_i915(dev);
649636ef 15738 u32 val;
fa555837 15739
7eb552ae 15740 if (INTEL_INFO(dev)->num_pipes == 1)
fa555837
DV
15741 return true;
15742
649636ef 15743 val = I915_READ(DSPCNTR(!crtc->plane));
fa555837
DV
15744
15745 if ((val & DISPLAY_PLANE_ENABLE) &&
15746 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
15747 return false;
15748
15749 return true;
15750}
15751
02e93c35
VS
15752static bool intel_crtc_has_encoders(struct intel_crtc *crtc)
15753{
15754 struct drm_device *dev = crtc->base.dev;
15755 struct intel_encoder *encoder;
15756
15757 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
15758 return true;
15759
15760 return false;
15761}
15762
dd756198
VS
15763static bool intel_encoder_has_connectors(struct intel_encoder *encoder)
15764{
15765 struct drm_device *dev = encoder->base.dev;
15766 struct intel_connector *connector;
15767
15768 for_each_connector_on_encoder(dev, &encoder->base, connector)
15769 return true;
15770
15771 return false;
15772}
15773
24929352
DV
15774static void intel_sanitize_crtc(struct intel_crtc *crtc)
15775{
15776 struct drm_device *dev = crtc->base.dev;
fac5e23e 15777 struct drm_i915_private *dev_priv = to_i915(dev);
4d1de975 15778 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
24929352 15779
24929352 15780 /* Clear any frame start delays used for debugging left by the BIOS */
4d1de975
JN
15781 if (!transcoder_is_dsi(cpu_transcoder)) {
15782 i915_reg_t reg = PIPECONF(cpu_transcoder);
15783
15784 I915_WRITE(reg,
15785 I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
15786 }
24929352 15787
d3eaf884 15788 /* restore vblank interrupts to correct state */
9625604c 15789 drm_crtc_vblank_reset(&crtc->base);
d297e103 15790 if (crtc->active) {
f9cd7b88
VS
15791 struct intel_plane *plane;
15792
9625604c 15793 drm_crtc_vblank_on(&crtc->base);
f9cd7b88
VS
15794
15795 /* Disable everything but the primary plane */
15796 for_each_intel_plane_on_crtc(dev, crtc, plane) {
15797 if (plane->base.type == DRM_PLANE_TYPE_PRIMARY)
15798 continue;
15799
15800 plane->disable_plane(&plane->base, &crtc->base);
15801 }
9625604c 15802 }
d3eaf884 15803
24929352 15804 /* We need to sanitize the plane -> pipe mapping first because this will
fa555837
DV
15805 * disable the crtc (and hence change the state) if it is wrong. Note
15806 * that gen4+ has a fixed plane -> pipe mapping. */
15807 if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
24929352
DV
15808 bool plane;
15809
78108b7c
VS
15810 DRM_DEBUG_KMS("[CRTC:%d:%s] wrong plane connection detected!\n",
15811 crtc->base.base.id, crtc->base.name);
24929352
DV
15812
15813 /* Pipe has the wrong plane attached and the plane is active.
15814 * Temporarily change the plane mapping and disable everything
15815 * ... */
15816 plane = crtc->plane;
b70709a6 15817 to_intel_plane_state(crtc->base.primary->state)->visible = true;
24929352 15818 crtc->plane = !plane;
b17d48e2 15819 intel_crtc_disable_noatomic(&crtc->base);
24929352 15820 crtc->plane = plane;
24929352 15821 }
24929352 15822
7fad798e
DV
15823 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
15824 crtc->pipe == PIPE_A && !crtc->active) {
15825 /* BIOS forgot to enable pipe A, this mostly happens after
15826 * resume. Force-enable the pipe to fix this, the update_dpms
15827 * call below we restore the pipe to the right state, but leave
15828 * the required bits on. */
15829 intel_enable_pipe_a(dev);
15830 }
15831
24929352
DV
15832 /* Adjust the state of the output pipe according to whether we
15833 * have active connectors/encoders. */
842e0307 15834 if (crtc->active && !intel_crtc_has_encoders(crtc))
b17d48e2 15835 intel_crtc_disable_noatomic(&crtc->base);
24929352 15836
a3ed6aad 15837 if (crtc->active || HAS_GMCH_DISPLAY(dev)) {
4cc31489
DV
15838 /*
15839 * We start out with underrun reporting disabled to avoid races.
15840 * For correct bookkeeping mark this on active crtcs.
15841 *
c5ab3bc0
DV
15842 * Also on gmch platforms we dont have any hardware bits to
15843 * disable the underrun reporting. Which means we need to start
15844 * out with underrun reporting disabled also on inactive pipes,
15845 * since otherwise we'll complain about the garbage we read when
15846 * e.g. coming up after runtime pm.
15847 *
4cc31489
DV
15848 * No protection against concurrent access is required - at
15849 * worst a fifo underrun happens which also sets this to false.
15850 */
15851 crtc->cpu_fifo_underrun_disabled = true;
15852 crtc->pch_fifo_underrun_disabled = true;
15853 }
24929352
DV
15854}
15855
15856static void intel_sanitize_encoder(struct intel_encoder *encoder)
15857{
15858 struct intel_connector *connector;
15859 struct drm_device *dev = encoder->base.dev;
15860
15861 /* We need to check both for a crtc link (meaning that the
15862 * encoder is active and trying to read from a pipe) and the
15863 * pipe itself being active. */
15864 bool has_active_crtc = encoder->base.crtc &&
15865 to_intel_crtc(encoder->base.crtc)->active;
15866
dd756198 15867 if (intel_encoder_has_connectors(encoder) && !has_active_crtc) {
24929352
DV
15868 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
15869 encoder->base.base.id,
8e329a03 15870 encoder->base.name);
24929352
DV
15871
15872 /* Connector is active, but has no active pipe. This is
15873 * fallout from our resume register restoring. Disable
15874 * the encoder manually again. */
15875 if (encoder->base.crtc) {
15876 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
15877 encoder->base.base.id,
8e329a03 15878 encoder->base.name);
24929352 15879 encoder->disable(encoder);
a62d1497
VS
15880 if (encoder->post_disable)
15881 encoder->post_disable(encoder);
24929352 15882 }
7f1950fb 15883 encoder->base.crtc = NULL;
24929352
DV
15884
15885 /* Inconsistent output/port/pipe state happens presumably due to
15886 * a bug in one of the get_hw_state functions. Or someplace else
15887 * in our code, like the register restore mess on resume. Clamp
15888 * things to off as a safer default. */
3a3371ff 15889 for_each_intel_connector(dev, connector) {
24929352
DV
15890 if (connector->encoder != encoder)
15891 continue;
7f1950fb
EE
15892 connector->base.dpms = DRM_MODE_DPMS_OFF;
15893 connector->base.encoder = NULL;
24929352
DV
15894 }
15895 }
15896 /* Enabled encoders without active connectors will be fixed in
15897 * the crtc fixup. */
15898}
15899
04098753 15900void i915_redisable_vga_power_on(struct drm_device *dev)
0fde901f 15901{
fac5e23e 15902 struct drm_i915_private *dev_priv = to_i915(dev);
f0f59a00 15903 i915_reg_t vga_reg = i915_vgacntrl_reg(dev);
0fde901f 15904
04098753
ID
15905 if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
15906 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
15907 i915_disable_vga(dev);
15908 }
15909}
15910
15911void i915_redisable_vga(struct drm_device *dev)
15912{
fac5e23e 15913 struct drm_i915_private *dev_priv = to_i915(dev);
04098753 15914
8dc8a27c
PZ
15915 /* This function can be called both from intel_modeset_setup_hw_state or
15916 * at a very early point in our resume sequence, where the power well
15917 * structures are not yet restored. Since this function is at a very
15918 * paranoid "someone might have enabled VGA while we were not looking"
15919 * level, just check if the power well is enabled instead of trying to
15920 * follow the "don't touch the power well if we don't need it" policy
15921 * the rest of the driver uses. */
6392f847 15922 if (!intel_display_power_get_if_enabled(dev_priv, POWER_DOMAIN_VGA))
8dc8a27c
PZ
15923 return;
15924
04098753 15925 i915_redisable_vga_power_on(dev);
6392f847
ID
15926
15927 intel_display_power_put(dev_priv, POWER_DOMAIN_VGA);
0fde901f
KM
15928}
15929
f9cd7b88 15930static bool primary_get_hw_state(struct intel_plane *plane)
98ec7739 15931{
f9cd7b88 15932 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
98ec7739 15933
f9cd7b88 15934 return I915_READ(DSPCNTR(plane->plane)) & DISPLAY_PLANE_ENABLE;
d032ffa0
ML
15935}
15936
f9cd7b88
VS
15937/* FIXME read out full plane state for all planes */
15938static void readout_plane_state(struct intel_crtc *crtc)
d032ffa0 15939{
b26d3ea3 15940 struct drm_plane *primary = crtc->base.primary;
f9cd7b88 15941 struct intel_plane_state *plane_state =
b26d3ea3 15942 to_intel_plane_state(primary->state);
d032ffa0 15943
19b8d387 15944 plane_state->visible = crtc->active &&
b26d3ea3
ML
15945 primary_get_hw_state(to_intel_plane(primary));
15946
15947 if (plane_state->visible)
15948 crtc->base.state->plane_mask |= 1 << drm_plane_index(primary);
98ec7739
VS
15949}
15950
30e984df 15951static void intel_modeset_readout_hw_state(struct drm_device *dev)
24929352 15952{
fac5e23e 15953 struct drm_i915_private *dev_priv = to_i915(dev);
24929352 15954 enum pipe pipe;
24929352
DV
15955 struct intel_crtc *crtc;
15956 struct intel_encoder *encoder;
15957 struct intel_connector *connector;
5358901f 15958 int i;
24929352 15959
565602d7
ML
15960 dev_priv->active_crtcs = 0;
15961
d3fcc808 15962 for_each_intel_crtc(dev, crtc) {
565602d7
ML
15963 struct intel_crtc_state *crtc_state = crtc->config;
15964 int pixclk = 0;
3b117c8f 15965
ec2dc6a0 15966 __drm_atomic_helper_crtc_destroy_state(&crtc_state->base);
565602d7
ML
15967 memset(crtc_state, 0, sizeof(*crtc_state));
15968 crtc_state->base.crtc = &crtc->base;
24929352 15969
565602d7
ML
15970 crtc_state->base.active = crtc_state->base.enable =
15971 dev_priv->display.get_pipe_config(crtc, crtc_state);
15972
15973 crtc->base.enabled = crtc_state->base.enable;
15974 crtc->active = crtc_state->base.active;
15975
15976 if (crtc_state->base.active) {
15977 dev_priv->active_crtcs |= 1 << crtc->pipe;
15978
c89e39f3 15979 if (INTEL_GEN(dev_priv) >= 9 || IS_BROADWELL(dev_priv))
565602d7 15980 pixclk = ilk_pipe_pixel_rate(crtc_state);
9558d15d 15981 else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
565602d7
ML
15982 pixclk = crtc_state->base.adjusted_mode.crtc_clock;
15983 else
15984 WARN_ON(dev_priv->display.modeset_calc_cdclk);
9558d15d
VS
15985
15986 /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
15987 if (IS_BROADWELL(dev_priv) && crtc_state->ips_enabled)
15988 pixclk = DIV_ROUND_UP(pixclk * 100, 95);
565602d7
ML
15989 }
15990
15991 dev_priv->min_pixclk[crtc->pipe] = pixclk;
b70709a6 15992
f9cd7b88 15993 readout_plane_state(crtc);
24929352 15994
78108b7c
VS
15995 DRM_DEBUG_KMS("[CRTC:%d:%s] hw state readout: %s\n",
15996 crtc->base.base.id, crtc->base.name,
24929352
DV
15997 crtc->active ? "enabled" : "disabled");
15998 }
15999
5358901f
DV
16000 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
16001 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
16002
2edd6443
ACO
16003 pll->on = pll->funcs.get_hw_state(dev_priv, pll,
16004 &pll->config.hw_state);
3e369b76 16005 pll->config.crtc_mask = 0;
d3fcc808 16006 for_each_intel_crtc(dev, crtc) {
2dd66ebd 16007 if (crtc->active && crtc->config->shared_dpll == pll)
3e369b76 16008 pll->config.crtc_mask |= 1 << crtc->pipe;
5358901f 16009 }
2dd66ebd 16010 pll->active_mask = pll->config.crtc_mask;
5358901f 16011
1e6f2ddc 16012 DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
3e369b76 16013 pll->name, pll->config.crtc_mask, pll->on);
5358901f
DV
16014 }
16015
b2784e15 16016 for_each_intel_encoder(dev, encoder) {
24929352
DV
16017 pipe = 0;
16018
16019 if (encoder->get_hw_state(encoder, &pipe)) {
045ac3b5
JB
16020 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
16021 encoder->base.crtc = &crtc->base;
253c84c8 16022 crtc->config->output_types |= 1 << encoder->type;
6e3c9717 16023 encoder->get_config(encoder, crtc->config);
24929352
DV
16024 } else {
16025 encoder->base.crtc = NULL;
16026 }
16027
6f2bcceb 16028 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
24929352 16029 encoder->base.base.id,
8e329a03 16030 encoder->base.name,
24929352 16031 encoder->base.crtc ? "enabled" : "disabled",
6f2bcceb 16032 pipe_name(pipe));
24929352
DV
16033 }
16034
3a3371ff 16035 for_each_intel_connector(dev, connector) {
24929352
DV
16036 if (connector->get_hw_state(connector)) {
16037 connector->base.dpms = DRM_MODE_DPMS_ON;
2aa974c9
ML
16038
16039 encoder = connector->encoder;
16040 connector->base.encoder = &encoder->base;
16041
16042 if (encoder->base.crtc &&
16043 encoder->base.crtc->state->active) {
16044 /*
16045 * This has to be done during hardware readout
16046 * because anything calling .crtc_disable may
16047 * rely on the connector_mask being accurate.
16048 */
16049 encoder->base.crtc->state->connector_mask |=
16050 1 << drm_connector_index(&connector->base);
e87a52b3
ML
16051 encoder->base.crtc->state->encoder_mask |=
16052 1 << drm_encoder_index(&encoder->base);
2aa974c9
ML
16053 }
16054
24929352
DV
16055 } else {
16056 connector->base.dpms = DRM_MODE_DPMS_OFF;
16057 connector->base.encoder = NULL;
16058 }
16059 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
16060 connector->base.base.id,
c23cc417 16061 connector->base.name,
24929352
DV
16062 connector->base.encoder ? "enabled" : "disabled");
16063 }
7f4c6284
VS
16064
16065 for_each_intel_crtc(dev, crtc) {
16066 crtc->base.hwmode = crtc->config->base.adjusted_mode;
16067
16068 memset(&crtc->base.mode, 0, sizeof(crtc->base.mode));
16069 if (crtc->base.state->active) {
16070 intel_mode_from_pipe_config(&crtc->base.mode, crtc->config);
16071 intel_mode_from_pipe_config(&crtc->base.state->adjusted_mode, crtc->config);
16072 WARN_ON(drm_atomic_set_mode_for_crtc(crtc->base.state, &crtc->base.mode));
16073
16074 /*
16075 * The initial mode needs to be set in order to keep
16076 * the atomic core happy. It wants a valid mode if the
16077 * crtc's enabled, so we do the above call.
16078 *
16079 * At this point some state updated by the connectors
16080 * in their ->detect() callback has not run yet, so
16081 * no recalculation can be done yet.
16082 *
16083 * Even if we could do a recalculation and modeset
16084 * right now it would cause a double modeset if
16085 * fbdev or userspace chooses a different initial mode.
16086 *
16087 * If that happens, someone indicated they wanted a
16088 * mode change, which means it's safe to do a full
16089 * recalculation.
16090 */
16091 crtc->base.state->mode.private_flags = I915_MODE_FLAG_INHERITED;
9eca6832
VS
16092
16093 drm_calc_timestamping_constants(&crtc->base, &crtc->base.hwmode);
16094 update_scanline_offset(crtc);
7f4c6284 16095 }
e3b247da
VS
16096
16097 intel_pipe_config_sanity_check(dev_priv, crtc->config);
7f4c6284 16098 }
30e984df
DV
16099}
16100
043e9bda
ML
16101/* Scan out the current hw modeset state,
16102 * and sanitizes it to the current state
16103 */
16104static void
16105intel_modeset_setup_hw_state(struct drm_device *dev)
30e984df 16106{
fac5e23e 16107 struct drm_i915_private *dev_priv = to_i915(dev);
30e984df 16108 enum pipe pipe;
30e984df
DV
16109 struct intel_crtc *crtc;
16110 struct intel_encoder *encoder;
35c95375 16111 int i;
30e984df
DV
16112
16113 intel_modeset_readout_hw_state(dev);
24929352
DV
16114
16115 /* HW state is read out, now we need to sanitize this mess. */
b2784e15 16116 for_each_intel_encoder(dev, encoder) {
24929352
DV
16117 intel_sanitize_encoder(encoder);
16118 }
16119
055e393f 16120 for_each_pipe(dev_priv, pipe) {
24929352
DV
16121 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
16122 intel_sanitize_crtc(crtc);
6e3c9717
ACO
16123 intel_dump_pipe_config(crtc, crtc->config,
16124 "[setup_hw_state]");
24929352 16125 }
9a935856 16126
d29b2f9d
ACO
16127 intel_modeset_update_connector_atomic_state(dev);
16128
35c95375
DV
16129 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
16130 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
16131
2dd66ebd 16132 if (!pll->on || pll->active_mask)
35c95375
DV
16133 continue;
16134
16135 DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
16136
2edd6443 16137 pll->funcs.disable(dev_priv, pll);
35c95375
DV
16138 pll->on = false;
16139 }
16140
666a4537 16141 if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev))
6eb1a681
VS
16142 vlv_wm_get_hw_state(dev);
16143 else if (IS_GEN9(dev))
3078999f
PB
16144 skl_wm_get_hw_state(dev);
16145 else if (HAS_PCH_SPLIT(dev))
243e6a44 16146 ilk_wm_get_hw_state(dev);
292b990e
ML
16147
16148 for_each_intel_crtc(dev, crtc) {
16149 unsigned long put_domains;
16150
74bff5f9 16151 put_domains = modeset_get_crtc_power_domains(&crtc->base, crtc->config);
292b990e
ML
16152 if (WARN_ON(put_domains))
16153 modeset_put_power_domains(dev_priv, put_domains);
16154 }
16155 intel_display_set_init_power(dev_priv, false);
010cf73d
PZ
16156
16157 intel_fbc_init_pipe_state(dev_priv);
043e9bda 16158}
7d0bc1ea 16159
043e9bda
ML
16160void intel_display_resume(struct drm_device *dev)
16161{
e2c8b870
ML
16162 struct drm_i915_private *dev_priv = to_i915(dev);
16163 struct drm_atomic_state *state = dev_priv->modeset_restore_state;
16164 struct drm_modeset_acquire_ctx ctx;
043e9bda 16165 int ret;
e2c8b870 16166 bool setup = false;
f30da187 16167
e2c8b870 16168 dev_priv->modeset_restore_state = NULL;
043e9bda 16169
ea49c9ac
ML
16170 /*
16171 * This is a cludge because with real atomic modeset mode_config.mutex
16172 * won't be taken. Unfortunately some probed state like
16173 * audio_codec_enable is still protected by mode_config.mutex, so lock
16174 * it here for now.
16175 */
16176 mutex_lock(&dev->mode_config.mutex);
e2c8b870 16177 drm_modeset_acquire_init(&ctx, 0);
043e9bda 16178
e2c8b870
ML
16179retry:
16180 ret = drm_modeset_lock_all_ctx(dev, &ctx);
043e9bda 16181
e2c8b870
ML
16182 if (ret == 0 && !setup) {
16183 setup = true;
043e9bda 16184
e2c8b870
ML
16185 intel_modeset_setup_hw_state(dev);
16186 i915_redisable_vga(dev);
45e2b5f6 16187 }
8af6cf88 16188
e2c8b870
ML
16189 if (ret == 0 && state) {
16190 struct drm_crtc_state *crtc_state;
16191 struct drm_crtc *crtc;
16192 int i;
043e9bda 16193
e2c8b870
ML
16194 state->acquire_ctx = &ctx;
16195
e3d5457c
VS
16196 /* ignore any reset values/BIOS leftovers in the WM registers */
16197 to_intel_atomic_state(state)->skip_intermediate_wm = true;
16198
e2c8b870
ML
16199 for_each_crtc_in_state(state, crtc, crtc_state, i) {
16200 /*
16201 * Force recalculation even if we restore
16202 * current state. With fast modeset this may not result
16203 * in a modeset when the state is compatible.
16204 */
16205 crtc_state->mode_changed = true;
16206 }
16207
16208 ret = drm_atomic_commit(state);
043e9bda
ML
16209 }
16210
e2c8b870
ML
16211 if (ret == -EDEADLK) {
16212 drm_modeset_backoff(&ctx);
16213 goto retry;
16214 }
043e9bda 16215
e2c8b870
ML
16216 drm_modeset_drop_locks(&ctx);
16217 drm_modeset_acquire_fini(&ctx);
ea49c9ac 16218 mutex_unlock(&dev->mode_config.mutex);
043e9bda 16219
e2c8b870
ML
16220 if (ret) {
16221 DRM_ERROR("Restoring old state failed with %i\n", ret);
16222 drm_atomic_state_free(state);
16223 }
2c7111db
CW
16224}
16225
16226void intel_modeset_gem_init(struct drm_device *dev)
16227{
dc97997a 16228 struct drm_i915_private *dev_priv = to_i915(dev);
484b41dd 16229 struct drm_crtc *c;
2ff8fde1 16230 struct drm_i915_gem_object *obj;
e0d6149b 16231 int ret;
484b41dd 16232
dc97997a 16233 intel_init_gt_powersave(dev_priv);
ae48434c 16234
1833b134 16235 intel_modeset_init_hw(dev);
02e792fb 16236
1ee8da6d 16237 intel_setup_overlay(dev_priv);
484b41dd
JB
16238
16239 /*
16240 * Make sure any fbs we allocated at startup are properly
16241 * pinned & fenced. When we do the allocation it's too early
16242 * for this.
16243 */
70e1e0ec 16244 for_each_crtc(dev, c) {
2ff8fde1
MR
16245 obj = intel_fb_obj(c->primary->fb);
16246 if (obj == NULL)
484b41dd
JB
16247 continue;
16248
e0d6149b 16249 mutex_lock(&dev->struct_mutex);
3465c580
VS
16250 ret = intel_pin_and_fence_fb_obj(c->primary->fb,
16251 c->primary->state->rotation);
e0d6149b
TU
16252 mutex_unlock(&dev->struct_mutex);
16253 if (ret) {
484b41dd
JB
16254 DRM_ERROR("failed to pin boot fb on pipe %d\n",
16255 to_intel_crtc(c)->pipe);
66e514c1 16256 drm_framebuffer_unreference(c->primary->fb);
5a21b665 16257 c->primary->fb = NULL;
36750f28 16258 c->primary->crtc = c->primary->state->crtc = NULL;
5a21b665 16259 update_state_fb(c->primary);
36750f28 16260 c->state->plane_mask &= ~(1 << drm_plane_index(c->primary));
484b41dd
JB
16261 }
16262 }
1ebaa0b9
CW
16263}
16264
16265int intel_connector_register(struct drm_connector *connector)
16266{
16267 struct intel_connector *intel_connector = to_intel_connector(connector);
16268 int ret;
16269
16270 ret = intel_backlight_device_register(intel_connector);
16271 if (ret)
16272 goto err;
16273
16274 return 0;
0962c3c9 16275
1ebaa0b9
CW
16276err:
16277 return ret;
79e53945
JB
16278}
16279
c191eca1 16280void intel_connector_unregister(struct drm_connector *connector)
4932e2c3 16281{
e63d87c0 16282 struct intel_connector *intel_connector = to_intel_connector(connector);
4932e2c3 16283
e63d87c0 16284 intel_backlight_device_unregister(intel_connector);
4932e2c3 16285 intel_panel_destroy_backlight(connector);
4932e2c3
ID
16286}
16287
79e53945
JB
16288void intel_modeset_cleanup(struct drm_device *dev)
16289{
fac5e23e 16290 struct drm_i915_private *dev_priv = to_i915(dev);
652c393a 16291
dc97997a 16292 intel_disable_gt_powersave(dev_priv);
2eb5252e 16293
fd0c0642
DV
16294 /*
16295 * Interrupts and polling as the first thing to avoid creating havoc.
2eb5252e 16296 * Too much stuff here (turning of connectors, ...) would
fd0c0642
DV
16297 * experience fancy races otherwise.
16298 */
2aeb7d3a 16299 intel_irq_uninstall(dev_priv);
eb21b92b 16300
fd0c0642
DV
16301 /*
16302 * Due to the hpd irq storm handling the hotplug work can re-arm the
16303 * poll handlers. Hence disable polling after hpd handling is shut down.
16304 */
f87ea761 16305 drm_kms_helper_poll_fini(dev);
fd0c0642 16306
723bfd70
JB
16307 intel_unregister_dsm_handler();
16308
c937ab3e 16309 intel_fbc_global_disable(dev_priv);
69341a5e 16310
1630fe75
CW
16311 /* flush any delayed tasks or pending work */
16312 flush_scheduled_work();
16313
79e53945 16314 drm_mode_config_cleanup(dev);
4d7bb011 16315
1ee8da6d 16316 intel_cleanup_overlay(dev_priv);
ae48434c 16317
dc97997a 16318 intel_cleanup_gt_powersave(dev_priv);
f5949141
DV
16319
16320 intel_teardown_gmbus(dev);
79e53945
JB
16321}
16322
df0e9248
CW
16323void intel_connector_attach_encoder(struct intel_connector *connector,
16324 struct intel_encoder *encoder)
16325{
16326 connector->encoder = encoder;
16327 drm_mode_connector_attach_encoder(&connector->base,
16328 &encoder->base);
79e53945 16329}
28d52043
DA
16330
16331/*
16332 * set vga decode state - true == enable VGA decode
16333 */
16334int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
16335{
fac5e23e 16336 struct drm_i915_private *dev_priv = to_i915(dev);
a885b3cc 16337 unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
28d52043
DA
16338 u16 gmch_ctrl;
16339
75fa041d
CW
16340 if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
16341 DRM_ERROR("failed to read control word\n");
16342 return -EIO;
16343 }
16344
c0cc8a55
CW
16345 if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
16346 return 0;
16347
28d52043
DA
16348 if (state)
16349 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
16350 else
16351 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
75fa041d
CW
16352
16353 if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
16354 DRM_ERROR("failed to write control word\n");
16355 return -EIO;
16356 }
16357
28d52043
DA
16358 return 0;
16359}
c4a1d9e4 16360
c4a1d9e4 16361struct intel_display_error_state {
ff57f1b0
PZ
16362
16363 u32 power_well_driver;
16364
63b66e5b
CW
16365 int num_transcoders;
16366
c4a1d9e4
CW
16367 struct intel_cursor_error_state {
16368 u32 control;
16369 u32 position;
16370 u32 base;
16371 u32 size;
52331309 16372 } cursor[I915_MAX_PIPES];
c4a1d9e4
CW
16373
16374 struct intel_pipe_error_state {
ddf9c536 16375 bool power_domain_on;
c4a1d9e4 16376 u32 source;
f301b1e1 16377 u32 stat;
52331309 16378 } pipe[I915_MAX_PIPES];
c4a1d9e4
CW
16379
16380 struct intel_plane_error_state {
16381 u32 control;
16382 u32 stride;
16383 u32 size;
16384 u32 pos;
16385 u32 addr;
16386 u32 surface;
16387 u32 tile_offset;
52331309 16388 } plane[I915_MAX_PIPES];
63b66e5b
CW
16389
16390 struct intel_transcoder_error_state {
ddf9c536 16391 bool power_domain_on;
63b66e5b
CW
16392 enum transcoder cpu_transcoder;
16393
16394 u32 conf;
16395
16396 u32 htotal;
16397 u32 hblank;
16398 u32 hsync;
16399 u32 vtotal;
16400 u32 vblank;
16401 u32 vsync;
16402 } transcoder[4];
c4a1d9e4
CW
16403};
16404
16405struct intel_display_error_state *
c033666a 16406intel_display_capture_error_state(struct drm_i915_private *dev_priv)
c4a1d9e4 16407{
c4a1d9e4 16408 struct intel_display_error_state *error;
63b66e5b
CW
16409 int transcoders[] = {
16410 TRANSCODER_A,
16411 TRANSCODER_B,
16412 TRANSCODER_C,
16413 TRANSCODER_EDP,
16414 };
c4a1d9e4
CW
16415 int i;
16416
c033666a 16417 if (INTEL_INFO(dev_priv)->num_pipes == 0)
63b66e5b
CW
16418 return NULL;
16419
9d1cb914 16420 error = kzalloc(sizeof(*error), GFP_ATOMIC);
c4a1d9e4
CW
16421 if (error == NULL)
16422 return NULL;
16423
c033666a 16424 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
ff57f1b0
PZ
16425 error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
16426
055e393f 16427 for_each_pipe(dev_priv, i) {
ddf9c536 16428 error->pipe[i].power_domain_on =
f458ebbc
DV
16429 __intel_display_power_is_enabled(dev_priv,
16430 POWER_DOMAIN_PIPE(i));
ddf9c536 16431 if (!error->pipe[i].power_domain_on)
9d1cb914
PZ
16432 continue;
16433
5efb3e28
VS
16434 error->cursor[i].control = I915_READ(CURCNTR(i));
16435 error->cursor[i].position = I915_READ(CURPOS(i));
16436 error->cursor[i].base = I915_READ(CURBASE(i));
c4a1d9e4
CW
16437
16438 error->plane[i].control = I915_READ(DSPCNTR(i));
16439 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
c033666a 16440 if (INTEL_GEN(dev_priv) <= 3) {
51889b35 16441 error->plane[i].size = I915_READ(DSPSIZE(i));
80ca378b
PZ
16442 error->plane[i].pos = I915_READ(DSPPOS(i));
16443 }
c033666a 16444 if (INTEL_GEN(dev_priv) <= 7 && !IS_HASWELL(dev_priv))
ca291363 16445 error->plane[i].addr = I915_READ(DSPADDR(i));
c033666a 16446 if (INTEL_GEN(dev_priv) >= 4) {
c4a1d9e4
CW
16447 error->plane[i].surface = I915_READ(DSPSURF(i));
16448 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
16449 }
16450
c4a1d9e4 16451 error->pipe[i].source = I915_READ(PIPESRC(i));
f301b1e1 16452
c033666a 16453 if (HAS_GMCH_DISPLAY(dev_priv))
f301b1e1 16454 error->pipe[i].stat = I915_READ(PIPESTAT(i));
63b66e5b
CW
16455 }
16456
4d1de975 16457 /* Note: this does not include DSI transcoders. */
c033666a 16458 error->num_transcoders = INTEL_INFO(dev_priv)->num_pipes;
2d1fe073 16459 if (HAS_DDI(dev_priv))
63b66e5b
CW
16460 error->num_transcoders++; /* Account for eDP. */
16461
16462 for (i = 0; i < error->num_transcoders; i++) {
16463 enum transcoder cpu_transcoder = transcoders[i];
16464
ddf9c536 16465 error->transcoder[i].power_domain_on =
f458ebbc 16466 __intel_display_power_is_enabled(dev_priv,
38cc1daf 16467 POWER_DOMAIN_TRANSCODER(cpu_transcoder));
ddf9c536 16468 if (!error->transcoder[i].power_domain_on)
9d1cb914
PZ
16469 continue;
16470
63b66e5b
CW
16471 error->transcoder[i].cpu_transcoder = cpu_transcoder;
16472
16473 error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
16474 error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
16475 error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
16476 error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
16477 error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
16478 error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
16479 error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
c4a1d9e4
CW
16480 }
16481
16482 return error;
16483}
16484
edc3d884
MK
16485#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
16486
c4a1d9e4 16487void
edc3d884 16488intel_display_print_error_state(struct drm_i915_error_state_buf *m,
c4a1d9e4
CW
16489 struct drm_device *dev,
16490 struct intel_display_error_state *error)
16491{
fac5e23e 16492 struct drm_i915_private *dev_priv = to_i915(dev);
c4a1d9e4
CW
16493 int i;
16494
63b66e5b
CW
16495 if (!error)
16496 return;
16497
edc3d884 16498 err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
190be112 16499 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
edc3d884 16500 err_printf(m, "PWR_WELL_CTL2: %08x\n",
ff57f1b0 16501 error->power_well_driver);
055e393f 16502 for_each_pipe(dev_priv, i) {
edc3d884 16503 err_printf(m, "Pipe [%d]:\n", i);
ddf9c536 16504 err_printf(m, " Power: %s\n",
87ad3212 16505 onoff(error->pipe[i].power_domain_on));
edc3d884 16506 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
f301b1e1 16507 err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
edc3d884
MK
16508
16509 err_printf(m, "Plane [%d]:\n", i);
16510 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
16511 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
80ca378b 16512 if (INTEL_INFO(dev)->gen <= 3) {
edc3d884
MK
16513 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
16514 err_printf(m, " POS: %08x\n", error->plane[i].pos);
80ca378b 16515 }
4b71a570 16516 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
edc3d884 16517 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
c4a1d9e4 16518 if (INTEL_INFO(dev)->gen >= 4) {
edc3d884
MK
16519 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
16520 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
c4a1d9e4
CW
16521 }
16522
edc3d884
MK
16523 err_printf(m, "Cursor [%d]:\n", i);
16524 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
16525 err_printf(m, " POS: %08x\n", error->cursor[i].position);
16526 err_printf(m, " BASE: %08x\n", error->cursor[i].base);
c4a1d9e4 16527 }
63b66e5b
CW
16528
16529 for (i = 0; i < error->num_transcoders; i++) {
da205630 16530 err_printf(m, "CPU transcoder: %s\n",
63b66e5b 16531 transcoder_name(error->transcoder[i].cpu_transcoder));
ddf9c536 16532 err_printf(m, " Power: %s\n",
87ad3212 16533 onoff(error->transcoder[i].power_domain_on));
63b66e5b
CW
16534 err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
16535 err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
16536 err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
16537 err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
16538 err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
16539 err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
16540 err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
16541 }
c4a1d9e4 16542}