]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/gpu/drm/i915/intel_display.c
drm/i915: Restore lost "Initialized i915" welcome message
[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
44cb734c
ID
1205 pp_reg = PP_CONTROL(0);
1206 port_sel = I915_READ(PP_ON_DELAYS(0)) & PANEL_PORT_SELECT_MASK;
bedd4dba
JN
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 1213 /* presumably write lock depends on pipe, not port select */
44cb734c 1214 pp_reg = PP_CONTROL(pipe);
bedd4dba 1215 panel_pipe = pipe;
ea0760cf 1216 } else {
44cb734c 1217 pp_reg = PP_CONTROL(0);
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
b7076546 1910void 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 */
09fa8bb9 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);
09fa8bb9 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
603525d7 2150static unsigned int intel_linear_alignment(const struct drm_i915_private *dev_priv)
4e9a86b6
VS
2151{
2152 if (INTEL_INFO(dev_priv)->gen >= 9)
2153 return 256 * 1024;
985b8bb4 2154 else if (IS_BROADWATER(dev_priv) || IS_CRESTLINE(dev_priv) ||
666a4537 2155 IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
4e9a86b6
VS
2156 return 128 * 1024;
2157 else if (INTEL_INFO(dev_priv)->gen >= 4)
2158 return 4 * 1024;
2159 else
44c5905e 2160 return 0;
4e9a86b6
VS
2161}
2162
603525d7
VS
2163static unsigned int intel_surf_alignment(const struct drm_i915_private *dev_priv,
2164 uint64_t fb_modifier)
2165{
2166 switch (fb_modifier) {
2167 case DRM_FORMAT_MOD_NONE:
2168 return intel_linear_alignment(dev_priv);
2169 case I915_FORMAT_MOD_X_TILED:
2170 if (INTEL_INFO(dev_priv)->gen >= 9)
2171 return 256 * 1024;
2172 return 0;
2173 case I915_FORMAT_MOD_Y_TILED:
2174 case I915_FORMAT_MOD_Yf_TILED:
2175 return 1 * 1024 * 1024;
2176 default:
2177 MISSING_CASE(fb_modifier);
2178 return 0;
2179 }
2180}
2181
058d88c4
CW
2182struct i915_vma *
2183intel_pin_and_fence_fb_obj(struct drm_framebuffer *fb, unsigned int rotation)
6b95a207 2184{
850c4cdc 2185 struct drm_device *dev = fb->dev;
fac5e23e 2186 struct drm_i915_private *dev_priv = to_i915(dev);
850c4cdc 2187 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
f64b98cd 2188 struct i915_ggtt_view view;
058d88c4 2189 struct i915_vma *vma;
6b95a207 2190 u32 alignment;
6b95a207 2191
ebcdd39e
MR
2192 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
2193
603525d7 2194 alignment = intel_surf_alignment(dev_priv, fb->modifier[0]);
6b95a207 2195
3465c580 2196 intel_fill_fb_ggtt_view(&view, fb, rotation);
f64b98cd 2197
693db184
CW
2198 /* Note that the w/a also requires 64 PTE of padding following the
2199 * bo. We currently fill all unused PTE with the shadow page and so
2200 * we should always have valid PTE following the scanout preventing
2201 * the VT-d warning.
2202 */
48f112fe 2203 if (intel_scanout_needs_vtd_wa(dev_priv) && alignment < 256 * 1024)
693db184
CW
2204 alignment = 256 * 1024;
2205
d6dd6843
PZ
2206 /*
2207 * Global gtt pte registers are special registers which actually forward
2208 * writes to a chunk of system memory. Which means that there is no risk
2209 * that the register values disappear as soon as we call
2210 * intel_runtime_pm_put(), so it is correct to wrap only the
2211 * pin/unpin/fence and not more.
2212 */
2213 intel_runtime_pm_get(dev_priv);
2214
058d88c4 2215 vma = i915_gem_object_pin_to_display_plane(obj, alignment, &view);
49ef5294
CW
2216 if (IS_ERR(vma))
2217 goto err;
6b95a207 2218
05a20d09 2219 if (i915_vma_is_map_and_fenceable(vma)) {
49ef5294
CW
2220 /* Install a fence for tiled scan-out. Pre-i965 always needs a
2221 * fence, whereas 965+ only requires a fence if using
2222 * framebuffer compression. For simplicity, we always, when
2223 * possible, install a fence as the cost is not that onerous.
2224 *
2225 * If we fail to fence the tiled scanout, then either the
2226 * modeset will reject the change (which is highly unlikely as
2227 * the affected systems, all but one, do not have unmappable
2228 * space) or we will not be able to enable full powersaving
2229 * techniques (also likely not to apply due to various limits
2230 * FBC and the like impose on the size of the buffer, which
2231 * presumably we violated anyway with this unmappable buffer).
2232 * Anyway, it is presumably better to stumble onwards with
2233 * something and try to run the system in a "less than optimal"
2234 * mode that matches the user configuration.
2235 */
2236 if (i915_vma_get_fence(vma) == 0)
2237 i915_vma_pin_fence(vma);
9807216f 2238 }
6b95a207 2239
49ef5294 2240err:
d6dd6843 2241 intel_runtime_pm_put(dev_priv);
058d88c4 2242 return vma;
6b95a207
KH
2243}
2244
fb4b8ce1 2245void intel_unpin_fb_obj(struct drm_framebuffer *fb, unsigned int rotation)
1690e1eb 2246{
82bc3b2d 2247 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
f64b98cd 2248 struct i915_ggtt_view view;
058d88c4 2249 struct i915_vma *vma;
82bc3b2d 2250
ebcdd39e
MR
2251 WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
2252
3465c580 2253 intel_fill_fb_ggtt_view(&view, fb, rotation);
05a20d09 2254 vma = i915_gem_object_to_ggtt(obj, &view);
f64b98cd 2255
49ef5294 2256 i915_vma_unpin_fence(vma);
058d88c4 2257 i915_gem_object_unpin_from_display_plane(vma);
1690e1eb
CW
2258}
2259
ef78ec94
VS
2260static int intel_fb_pitch(const struct drm_framebuffer *fb, int plane,
2261 unsigned int rotation)
2262{
2263 if (intel_rotation_90_or_270(rotation))
2264 return to_intel_framebuffer(fb)->rotated[plane].pitch;
2265 else
2266 return fb->pitches[plane];
2267}
2268
6687c906
VS
2269/*
2270 * Convert the x/y offsets into a linear offset.
2271 * Only valid with 0/180 degree rotation, which is fine since linear
2272 * offset is only used with linear buffers on pre-hsw and tiled buffers
2273 * with gen2/3, and 90/270 degree rotations isn't supported on any of them.
2274 */
2275u32 intel_fb_xy_to_linear(int x, int y,
2949056c
VS
2276 const struct intel_plane_state *state,
2277 int plane)
6687c906 2278{
2949056c 2279 const struct drm_framebuffer *fb = state->base.fb;
6687c906
VS
2280 unsigned int cpp = drm_format_plane_cpp(fb->pixel_format, plane);
2281 unsigned int pitch = fb->pitches[plane];
2282
2283 return y * pitch + x * cpp;
2284}
2285
2286/*
2287 * Add the x/y offsets derived from fb->offsets[] to the user
2288 * specified plane src x/y offsets. The resulting x/y offsets
2289 * specify the start of scanout from the beginning of the gtt mapping.
2290 */
2291void intel_add_fb_offsets(int *x, int *y,
2949056c
VS
2292 const struct intel_plane_state *state,
2293 int plane)
6687c906
VS
2294
2295{
2949056c
VS
2296 const struct intel_framebuffer *intel_fb = to_intel_framebuffer(state->base.fb);
2297 unsigned int rotation = state->base.rotation;
6687c906
VS
2298
2299 if (intel_rotation_90_or_270(rotation)) {
2300 *x += intel_fb->rotated[plane].x;
2301 *y += intel_fb->rotated[plane].y;
2302 } else {
2303 *x += intel_fb->normal[plane].x;
2304 *y += intel_fb->normal[plane].y;
2305 }
2306}
2307
29cf9491 2308/*
29cf9491
VS
2309 * Input tile dimensions and pitch must already be
2310 * rotated to match x and y, and in pixel units.
2311 */
66a2d927
VS
2312static u32 _intel_adjust_tile_offset(int *x, int *y,
2313 unsigned int tile_width,
2314 unsigned int tile_height,
2315 unsigned int tile_size,
2316 unsigned int pitch_tiles,
2317 u32 old_offset,
2318 u32 new_offset)
29cf9491 2319{
b9b24038 2320 unsigned int pitch_pixels = pitch_tiles * tile_width;
29cf9491
VS
2321 unsigned int tiles;
2322
2323 WARN_ON(old_offset & (tile_size - 1));
2324 WARN_ON(new_offset & (tile_size - 1));
2325 WARN_ON(new_offset > old_offset);
2326
2327 tiles = (old_offset - new_offset) / tile_size;
2328
2329 *y += tiles / pitch_tiles * tile_height;
2330 *x += tiles % pitch_tiles * tile_width;
2331
b9b24038
VS
2332 /* minimize x in case it got needlessly big */
2333 *y += *x / pitch_pixels * tile_height;
2334 *x %= pitch_pixels;
2335
29cf9491
VS
2336 return new_offset;
2337}
2338
66a2d927
VS
2339/*
2340 * Adjust the tile offset by moving the difference into
2341 * the x/y offsets.
2342 */
2343static u32 intel_adjust_tile_offset(int *x, int *y,
2344 const struct intel_plane_state *state, int plane,
2345 u32 old_offset, u32 new_offset)
2346{
2347 const struct drm_i915_private *dev_priv = to_i915(state->base.plane->dev);
2348 const struct drm_framebuffer *fb = state->base.fb;
2349 unsigned int cpp = drm_format_plane_cpp(fb->pixel_format, plane);
2350 unsigned int rotation = state->base.rotation;
2351 unsigned int pitch = intel_fb_pitch(fb, plane, rotation);
2352
2353 WARN_ON(new_offset > old_offset);
2354
2355 if (fb->modifier[plane] != DRM_FORMAT_MOD_NONE) {
2356 unsigned int tile_size, tile_width, tile_height;
2357 unsigned int pitch_tiles;
2358
2359 tile_size = intel_tile_size(dev_priv);
2360 intel_tile_dims(dev_priv, &tile_width, &tile_height,
2361 fb->modifier[plane], cpp);
2362
2363 if (intel_rotation_90_or_270(rotation)) {
2364 pitch_tiles = pitch / tile_height;
2365 swap(tile_width, tile_height);
2366 } else {
2367 pitch_tiles = pitch / (tile_width * cpp);
2368 }
2369
2370 _intel_adjust_tile_offset(x, y, tile_width, tile_height,
2371 tile_size, pitch_tiles,
2372 old_offset, new_offset);
2373 } else {
2374 old_offset += *y * pitch + *x * cpp;
2375
2376 *y = (old_offset - new_offset) / pitch;
2377 *x = ((old_offset - new_offset) - *y * pitch) / cpp;
2378 }
2379
2380 return new_offset;
2381}
2382
8d0deca8
VS
2383/*
2384 * Computes the linear offset to the base tile and adjusts
2385 * x, y. bytes per pixel is assumed to be a power-of-two.
2386 *
2387 * In the 90/270 rotated case, x and y are assumed
2388 * to be already rotated to match the rotated GTT view, and
2389 * pitch is the tile_height aligned framebuffer height.
6687c906
VS
2390 *
2391 * This function is used when computing the derived information
2392 * under intel_framebuffer, so using any of that information
2393 * here is not allowed. Anything under drm_framebuffer can be
2394 * used. This is why the user has to pass in the pitch since it
2395 * is specified in the rotated orientation.
8d0deca8 2396 */
6687c906
VS
2397static u32 _intel_compute_tile_offset(const struct drm_i915_private *dev_priv,
2398 int *x, int *y,
2399 const struct drm_framebuffer *fb, int plane,
2400 unsigned int pitch,
2401 unsigned int rotation,
2402 u32 alignment)
c2c75131 2403{
4f2d9934
VS
2404 uint64_t fb_modifier = fb->modifier[plane];
2405 unsigned int cpp = drm_format_plane_cpp(fb->pixel_format, plane);
6687c906 2406 u32 offset, offset_aligned;
29cf9491 2407
29cf9491
VS
2408 if (alignment)
2409 alignment--;
2410
b5c65338 2411 if (fb_modifier != DRM_FORMAT_MOD_NONE) {
8d0deca8
VS
2412 unsigned int tile_size, tile_width, tile_height;
2413 unsigned int tile_rows, tiles, pitch_tiles;
c2c75131 2414
d843310d 2415 tile_size = intel_tile_size(dev_priv);
8d0deca8
VS
2416 intel_tile_dims(dev_priv, &tile_width, &tile_height,
2417 fb_modifier, cpp);
2418
2419 if (intel_rotation_90_or_270(rotation)) {
2420 pitch_tiles = pitch / tile_height;
2421 swap(tile_width, tile_height);
2422 } else {
2423 pitch_tiles = pitch / (tile_width * cpp);
2424 }
d843310d
VS
2425
2426 tile_rows = *y / tile_height;
2427 *y %= tile_height;
c2c75131 2428
8d0deca8
VS
2429 tiles = *x / tile_width;
2430 *x %= tile_width;
bc752862 2431
29cf9491
VS
2432 offset = (tile_rows * pitch_tiles + tiles) * tile_size;
2433 offset_aligned = offset & ~alignment;
bc752862 2434
66a2d927
VS
2435 _intel_adjust_tile_offset(x, y, tile_width, tile_height,
2436 tile_size, pitch_tiles,
2437 offset, offset_aligned);
29cf9491 2438 } else {
bc752862 2439 offset = *y * pitch + *x * cpp;
29cf9491
VS
2440 offset_aligned = offset & ~alignment;
2441
4e9a86b6
VS
2442 *y = (offset & alignment) / pitch;
2443 *x = ((offset & alignment) - *y * pitch) / cpp;
bc752862 2444 }
29cf9491
VS
2445
2446 return offset_aligned;
c2c75131
DV
2447}
2448
6687c906 2449u32 intel_compute_tile_offset(int *x, int *y,
2949056c
VS
2450 const struct intel_plane_state *state,
2451 int plane)
6687c906 2452{
2949056c
VS
2453 const struct drm_i915_private *dev_priv = to_i915(state->base.plane->dev);
2454 const struct drm_framebuffer *fb = state->base.fb;
2455 unsigned int rotation = state->base.rotation;
ef78ec94 2456 int pitch = intel_fb_pitch(fb, plane, rotation);
8d970654
VS
2457 u32 alignment;
2458
2459 /* AUX_DIST needs only 4K alignment */
2460 if (fb->pixel_format == DRM_FORMAT_NV12 && plane == 1)
2461 alignment = 4096;
2462 else
2463 alignment = intel_surf_alignment(dev_priv, fb->modifier[plane]);
6687c906
VS
2464
2465 return _intel_compute_tile_offset(dev_priv, x, y, fb, plane, pitch,
2466 rotation, alignment);
2467}
2468
2469/* Convert the fb->offset[] linear offset into x/y offsets */
2470static void intel_fb_offset_to_xy(int *x, int *y,
2471 const struct drm_framebuffer *fb, int plane)
2472{
2473 unsigned int cpp = drm_format_plane_cpp(fb->pixel_format, plane);
2474 unsigned int pitch = fb->pitches[plane];
2475 u32 linear_offset = fb->offsets[plane];
2476
2477 *y = linear_offset / pitch;
2478 *x = linear_offset % pitch / cpp;
2479}
2480
72618ebf
VS
2481static unsigned int intel_fb_modifier_to_tiling(uint64_t fb_modifier)
2482{
2483 switch (fb_modifier) {
2484 case I915_FORMAT_MOD_X_TILED:
2485 return I915_TILING_X;
2486 case I915_FORMAT_MOD_Y_TILED:
2487 return I915_TILING_Y;
2488 default:
2489 return I915_TILING_NONE;
2490 }
2491}
2492
6687c906
VS
2493static int
2494intel_fill_fb_info(struct drm_i915_private *dev_priv,
2495 struct drm_framebuffer *fb)
2496{
2497 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
2498 struct intel_rotation_info *rot_info = &intel_fb->rot_info;
2499 u32 gtt_offset_rotated = 0;
2500 unsigned int max_size = 0;
2501 uint32_t format = fb->pixel_format;
2502 int i, num_planes = drm_format_num_planes(format);
2503 unsigned int tile_size = intel_tile_size(dev_priv);
2504
2505 for (i = 0; i < num_planes; i++) {
2506 unsigned int width, height;
2507 unsigned int cpp, size;
2508 u32 offset;
2509 int x, y;
2510
2511 cpp = drm_format_plane_cpp(format, i);
2512 width = drm_format_plane_width(fb->width, format, i);
2513 height = drm_format_plane_height(fb->height, format, i);
2514
2515 intel_fb_offset_to_xy(&x, &y, fb, i);
2516
60d5f2a4
VS
2517 /*
2518 * The fence (if used) is aligned to the start of the object
2519 * so having the framebuffer wrap around across the edge of the
2520 * fenced region doesn't really work. We have no API to configure
2521 * the fence start offset within the object (nor could we probably
2522 * on gen2/3). So it's just easier if we just require that the
2523 * fb layout agrees with the fence layout. We already check that the
2524 * fb stride matches the fence stride elsewhere.
2525 */
2526 if (i915_gem_object_is_tiled(intel_fb->obj) &&
2527 (x + width) * cpp > fb->pitches[i]) {
2528 DRM_DEBUG("bad fb plane %d offset: 0x%x\n",
2529 i, fb->offsets[i]);
2530 return -EINVAL;
2531 }
2532
6687c906
VS
2533 /*
2534 * First pixel of the framebuffer from
2535 * the start of the normal gtt mapping.
2536 */
2537 intel_fb->normal[i].x = x;
2538 intel_fb->normal[i].y = y;
2539
2540 offset = _intel_compute_tile_offset(dev_priv, &x, &y,
2541 fb, 0, fb->pitches[i],
cc926387 2542 DRM_ROTATE_0, tile_size);
6687c906
VS
2543 offset /= tile_size;
2544
2545 if (fb->modifier[i] != DRM_FORMAT_MOD_NONE) {
2546 unsigned int tile_width, tile_height;
2547 unsigned int pitch_tiles;
2548 struct drm_rect r;
2549
2550 intel_tile_dims(dev_priv, &tile_width, &tile_height,
2551 fb->modifier[i], cpp);
2552
2553 rot_info->plane[i].offset = offset;
2554 rot_info->plane[i].stride = DIV_ROUND_UP(fb->pitches[i], tile_width * cpp);
2555 rot_info->plane[i].width = DIV_ROUND_UP(x + width, tile_width);
2556 rot_info->plane[i].height = DIV_ROUND_UP(y + height, tile_height);
2557
2558 intel_fb->rotated[i].pitch =
2559 rot_info->plane[i].height * tile_height;
2560
2561 /* how many tiles does this plane need */
2562 size = rot_info->plane[i].stride * rot_info->plane[i].height;
2563 /*
2564 * If the plane isn't horizontally tile aligned,
2565 * we need one more tile.
2566 */
2567 if (x != 0)
2568 size++;
2569
2570 /* rotate the x/y offsets to match the GTT view */
2571 r.x1 = x;
2572 r.y1 = y;
2573 r.x2 = x + width;
2574 r.y2 = y + height;
2575 drm_rect_rotate(&r,
2576 rot_info->plane[i].width * tile_width,
2577 rot_info->plane[i].height * tile_height,
cc926387 2578 DRM_ROTATE_270);
6687c906
VS
2579 x = r.x1;
2580 y = r.y1;
2581
2582 /* rotate the tile dimensions to match the GTT view */
2583 pitch_tiles = intel_fb->rotated[i].pitch / tile_height;
2584 swap(tile_width, tile_height);
2585
2586 /*
2587 * We only keep the x/y offsets, so push all of the
2588 * gtt offset into the x/y offsets.
2589 */
66a2d927
VS
2590 _intel_adjust_tile_offset(&x, &y, tile_size,
2591 tile_width, tile_height, pitch_tiles,
2592 gtt_offset_rotated * tile_size, 0);
6687c906
VS
2593
2594 gtt_offset_rotated += rot_info->plane[i].width * rot_info->plane[i].height;
2595
2596 /*
2597 * First pixel of the framebuffer from
2598 * the start of the rotated gtt mapping.
2599 */
2600 intel_fb->rotated[i].x = x;
2601 intel_fb->rotated[i].y = y;
2602 } else {
2603 size = DIV_ROUND_UP((y + height) * fb->pitches[i] +
2604 x * cpp, tile_size);
2605 }
2606
2607 /* how many tiles in total needed in the bo */
2608 max_size = max(max_size, offset + size);
2609 }
2610
2611 if (max_size * tile_size > to_intel_framebuffer(fb)->obj->base.size) {
2612 DRM_DEBUG("fb too big for bo (need %u bytes, have %zu bytes)\n",
2613 max_size * tile_size, to_intel_framebuffer(fb)->obj->base.size);
2614 return -EINVAL;
2615 }
2616
2617 return 0;
2618}
2619
b35d63fa 2620static int i9xx_format_to_fourcc(int format)
46f297fb
JB
2621{
2622 switch (format) {
2623 case DISPPLANE_8BPP:
2624 return DRM_FORMAT_C8;
2625 case DISPPLANE_BGRX555:
2626 return DRM_FORMAT_XRGB1555;
2627 case DISPPLANE_BGRX565:
2628 return DRM_FORMAT_RGB565;
2629 default:
2630 case DISPPLANE_BGRX888:
2631 return DRM_FORMAT_XRGB8888;
2632 case DISPPLANE_RGBX888:
2633 return DRM_FORMAT_XBGR8888;
2634 case DISPPLANE_BGRX101010:
2635 return DRM_FORMAT_XRGB2101010;
2636 case DISPPLANE_RGBX101010:
2637 return DRM_FORMAT_XBGR2101010;
2638 }
2639}
2640
bc8d7dff
DL
2641static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
2642{
2643 switch (format) {
2644 case PLANE_CTL_FORMAT_RGB_565:
2645 return DRM_FORMAT_RGB565;
2646 default:
2647 case PLANE_CTL_FORMAT_XRGB_8888:
2648 if (rgb_order) {
2649 if (alpha)
2650 return DRM_FORMAT_ABGR8888;
2651 else
2652 return DRM_FORMAT_XBGR8888;
2653 } else {
2654 if (alpha)
2655 return DRM_FORMAT_ARGB8888;
2656 else
2657 return DRM_FORMAT_XRGB8888;
2658 }
2659 case PLANE_CTL_FORMAT_XRGB_2101010:
2660 if (rgb_order)
2661 return DRM_FORMAT_XBGR2101010;
2662 else
2663 return DRM_FORMAT_XRGB2101010;
2664 }
2665}
2666
5724dbd1 2667static bool
f6936e29
DV
2668intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
2669 struct intel_initial_plane_config *plane_config)
46f297fb
JB
2670{
2671 struct drm_device *dev = crtc->base.dev;
3badb49f 2672 struct drm_i915_private *dev_priv = to_i915(dev);
72e96d64 2673 struct i915_ggtt *ggtt = &dev_priv->ggtt;
46f297fb
JB
2674 struct drm_i915_gem_object *obj = NULL;
2675 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
2d14030b 2676 struct drm_framebuffer *fb = &plane_config->fb->base;
f37b5c2b
DV
2677 u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
2678 u32 size_aligned = round_up(plane_config->base + plane_config->size,
2679 PAGE_SIZE);
2680
2681 size_aligned -= base_aligned;
46f297fb 2682
ff2652ea
CW
2683 if (plane_config->size == 0)
2684 return false;
2685
3badb49f
PZ
2686 /* If the FB is too big, just don't use it since fbdev is not very
2687 * important and we should probably use that space with FBC or other
2688 * features. */
72e96d64 2689 if (size_aligned * 2 > ggtt->stolen_usable_size)
3badb49f
PZ
2690 return false;
2691
12c83d99
TU
2692 mutex_lock(&dev->struct_mutex);
2693
f37b5c2b
DV
2694 obj = i915_gem_object_create_stolen_for_preallocated(dev,
2695 base_aligned,
2696 base_aligned,
2697 size_aligned);
12c83d99
TU
2698 if (!obj) {
2699 mutex_unlock(&dev->struct_mutex);
484b41dd 2700 return false;
12c83d99 2701 }
46f297fb 2702
3e510a8e
CW
2703 if (plane_config->tiling == I915_TILING_X)
2704 obj->tiling_and_stride = fb->pitches[0] | I915_TILING_X;
46f297fb 2705
6bf129df
DL
2706 mode_cmd.pixel_format = fb->pixel_format;
2707 mode_cmd.width = fb->width;
2708 mode_cmd.height = fb->height;
2709 mode_cmd.pitches[0] = fb->pitches[0];
18c5247e
DV
2710 mode_cmd.modifier[0] = fb->modifier[0];
2711 mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
46f297fb 2712
6bf129df 2713 if (intel_framebuffer_init(dev, to_intel_framebuffer(fb),
484b41dd 2714 &mode_cmd, obj)) {
46f297fb
JB
2715 DRM_DEBUG_KMS("intel fb init failed\n");
2716 goto out_unref_obj;
2717 }
12c83d99 2718
46f297fb 2719 mutex_unlock(&dev->struct_mutex);
484b41dd 2720
f6936e29 2721 DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
484b41dd 2722 return true;
46f297fb
JB
2723
2724out_unref_obj:
f8c417cd 2725 i915_gem_object_put(obj);
46f297fb 2726 mutex_unlock(&dev->struct_mutex);
484b41dd
JB
2727 return false;
2728}
2729
5a21b665
DV
2730/* Update plane->state->fb to match plane->fb after driver-internal updates */
2731static void
2732update_state_fb(struct drm_plane *plane)
2733{
2734 if (plane->fb == plane->state->fb)
2735 return;
2736
2737 if (plane->state->fb)
2738 drm_framebuffer_unreference(plane->state->fb);
2739 plane->state->fb = plane->fb;
2740 if (plane->state->fb)
2741 drm_framebuffer_reference(plane->state->fb);
2742}
2743
5724dbd1 2744static void
f6936e29
DV
2745intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
2746 struct intel_initial_plane_config *plane_config)
484b41dd
JB
2747{
2748 struct drm_device *dev = intel_crtc->base.dev;
fac5e23e 2749 struct drm_i915_private *dev_priv = to_i915(dev);
484b41dd
JB
2750 struct drm_crtc *c;
2751 struct intel_crtc *i;
2ff8fde1 2752 struct drm_i915_gem_object *obj;
88595ac9 2753 struct drm_plane *primary = intel_crtc->base.primary;
be5651f2 2754 struct drm_plane_state *plane_state = primary->state;
200757f5
MR
2755 struct drm_crtc_state *crtc_state = intel_crtc->base.state;
2756 struct intel_plane *intel_plane = to_intel_plane(primary);
0a8d8a86
MR
2757 struct intel_plane_state *intel_state =
2758 to_intel_plane_state(plane_state);
88595ac9 2759 struct drm_framebuffer *fb;
484b41dd 2760
2d14030b 2761 if (!plane_config->fb)
484b41dd
JB
2762 return;
2763
f6936e29 2764 if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
88595ac9
DV
2765 fb = &plane_config->fb->base;
2766 goto valid_fb;
f55548b5 2767 }
484b41dd 2768
2d14030b 2769 kfree(plane_config->fb);
484b41dd
JB
2770
2771 /*
2772 * Failed to alloc the obj, check to see if we should share
2773 * an fb with another CRTC instead
2774 */
70e1e0ec 2775 for_each_crtc(dev, c) {
484b41dd
JB
2776 i = to_intel_crtc(c);
2777
2778 if (c == &intel_crtc->base)
2779 continue;
2780
2ff8fde1
MR
2781 if (!i->active)
2782 continue;
2783
88595ac9
DV
2784 fb = c->primary->fb;
2785 if (!fb)
484b41dd
JB
2786 continue;
2787
88595ac9 2788 obj = intel_fb_obj(fb);
058d88c4 2789 if (i915_gem_object_ggtt_offset(obj, NULL) == plane_config->base) {
88595ac9
DV
2790 drm_framebuffer_reference(fb);
2791 goto valid_fb;
484b41dd
JB
2792 }
2793 }
88595ac9 2794
200757f5
MR
2795 /*
2796 * We've failed to reconstruct the BIOS FB. Current display state
2797 * indicates that the primary plane is visible, but has a NULL FB,
2798 * which will lead to problems later if we don't fix it up. The
2799 * simplest solution is to just disable the primary plane now and
2800 * pretend the BIOS never had it enabled.
2801 */
936e71e3 2802 to_intel_plane_state(plane_state)->base.visible = false;
200757f5 2803 crtc_state->plane_mask &= ~(1 << drm_plane_index(primary));
2622a081 2804 intel_pre_disable_primary_noatomic(&intel_crtc->base);
200757f5
MR
2805 intel_plane->disable_plane(primary, &intel_crtc->base);
2806
88595ac9
DV
2807 return;
2808
2809valid_fb:
f44e2659
VS
2810 plane_state->src_x = 0;
2811 plane_state->src_y = 0;
be5651f2
ML
2812 plane_state->src_w = fb->width << 16;
2813 plane_state->src_h = fb->height << 16;
2814
f44e2659
VS
2815 plane_state->crtc_x = 0;
2816 plane_state->crtc_y = 0;
be5651f2
ML
2817 plane_state->crtc_w = fb->width;
2818 plane_state->crtc_h = fb->height;
2819
936e71e3
VS
2820 intel_state->base.src.x1 = plane_state->src_x;
2821 intel_state->base.src.y1 = plane_state->src_y;
2822 intel_state->base.src.x2 = plane_state->src_x + plane_state->src_w;
2823 intel_state->base.src.y2 = plane_state->src_y + plane_state->src_h;
2824 intel_state->base.dst.x1 = plane_state->crtc_x;
2825 intel_state->base.dst.y1 = plane_state->crtc_y;
2826 intel_state->base.dst.x2 = plane_state->crtc_x + plane_state->crtc_w;
2827 intel_state->base.dst.y2 = plane_state->crtc_y + plane_state->crtc_h;
0a8d8a86 2828
88595ac9 2829 obj = intel_fb_obj(fb);
3e510a8e 2830 if (i915_gem_object_is_tiled(obj))
88595ac9
DV
2831 dev_priv->preserve_bios_swizzle = true;
2832
be5651f2
ML
2833 drm_framebuffer_reference(fb);
2834 primary->fb = primary->state->fb = fb;
36750f28 2835 primary->crtc = primary->state->crtc = &intel_crtc->base;
36750f28 2836 intel_crtc->base.state->plane_mask |= (1 << drm_plane_index(primary));
faf5bf0a
CW
2837 atomic_or(to_intel_plane(primary)->frontbuffer_bit,
2838 &obj->frontbuffer_bits);
46f297fb
JB
2839}
2840
b63a16f6
VS
2841static int skl_max_plane_width(const struct drm_framebuffer *fb, int plane,
2842 unsigned int rotation)
2843{
2844 int cpp = drm_format_plane_cpp(fb->pixel_format, plane);
2845
2846 switch (fb->modifier[plane]) {
2847 case DRM_FORMAT_MOD_NONE:
2848 case I915_FORMAT_MOD_X_TILED:
2849 switch (cpp) {
2850 case 8:
2851 return 4096;
2852 case 4:
2853 case 2:
2854 case 1:
2855 return 8192;
2856 default:
2857 MISSING_CASE(cpp);
2858 break;
2859 }
2860 break;
2861 case I915_FORMAT_MOD_Y_TILED:
2862 case I915_FORMAT_MOD_Yf_TILED:
2863 switch (cpp) {
2864 case 8:
2865 return 2048;
2866 case 4:
2867 return 4096;
2868 case 2:
2869 case 1:
2870 return 8192;
2871 default:
2872 MISSING_CASE(cpp);
2873 break;
2874 }
2875 break;
2876 default:
2877 MISSING_CASE(fb->modifier[plane]);
2878 }
2879
2880 return 2048;
2881}
2882
2883static int skl_check_main_surface(struct intel_plane_state *plane_state)
2884{
2885 const struct drm_i915_private *dev_priv = to_i915(plane_state->base.plane->dev);
2886 const struct drm_framebuffer *fb = plane_state->base.fb;
2887 unsigned int rotation = plane_state->base.rotation;
cc926387
DV
2888 int x = plane_state->base.src.x1 >> 16;
2889 int y = plane_state->base.src.y1 >> 16;
2890 int w = drm_rect_width(&plane_state->base.src) >> 16;
2891 int h = drm_rect_height(&plane_state->base.src) >> 16;
b63a16f6
VS
2892 int max_width = skl_max_plane_width(fb, 0, rotation);
2893 int max_height = 4096;
8d970654 2894 u32 alignment, offset, aux_offset = plane_state->aux.offset;
b63a16f6
VS
2895
2896 if (w > max_width || h > max_height) {
2897 DRM_DEBUG_KMS("requested Y/RGB source size %dx%d too big (limit %dx%d)\n",
2898 w, h, max_width, max_height);
2899 return -EINVAL;
2900 }
2901
2902 intel_add_fb_offsets(&x, &y, plane_state, 0);
2903 offset = intel_compute_tile_offset(&x, &y, plane_state, 0);
2904
2905 alignment = intel_surf_alignment(dev_priv, fb->modifier[0]);
2906
8d970654
VS
2907 /*
2908 * AUX surface offset is specified as the distance from the
2909 * main surface offset, and it must be non-negative. Make
2910 * sure that is what we will get.
2911 */
2912 if (offset > aux_offset)
2913 offset = intel_adjust_tile_offset(&x, &y, plane_state, 0,
2914 offset, aux_offset & ~(alignment - 1));
2915
b63a16f6
VS
2916 /*
2917 * When using an X-tiled surface, the plane blows up
2918 * if the x offset + width exceed the stride.
2919 *
2920 * TODO: linear and Y-tiled seem fine, Yf untested,
2921 */
2922 if (fb->modifier[0] == I915_FORMAT_MOD_X_TILED) {
2923 int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
2924
2925 while ((x + w) * cpp > fb->pitches[0]) {
2926 if (offset == 0) {
2927 DRM_DEBUG_KMS("Unable to find suitable display surface offset\n");
2928 return -EINVAL;
2929 }
2930
2931 offset = intel_adjust_tile_offset(&x, &y, plane_state, 0,
2932 offset, offset - alignment);
2933 }
2934 }
2935
2936 plane_state->main.offset = offset;
2937 plane_state->main.x = x;
2938 plane_state->main.y = y;
2939
2940 return 0;
2941}
2942
8d970654
VS
2943static int skl_check_nv12_aux_surface(struct intel_plane_state *plane_state)
2944{
2945 const struct drm_framebuffer *fb = plane_state->base.fb;
2946 unsigned int rotation = plane_state->base.rotation;
2947 int max_width = skl_max_plane_width(fb, 1, rotation);
2948 int max_height = 4096;
cc926387
DV
2949 int x = plane_state->base.src.x1 >> 17;
2950 int y = plane_state->base.src.y1 >> 17;
2951 int w = drm_rect_width(&plane_state->base.src) >> 17;
2952 int h = drm_rect_height(&plane_state->base.src) >> 17;
8d970654
VS
2953 u32 offset;
2954
2955 intel_add_fb_offsets(&x, &y, plane_state, 1);
2956 offset = intel_compute_tile_offset(&x, &y, plane_state, 1);
2957
2958 /* FIXME not quite sure how/if these apply to the chroma plane */
2959 if (w > max_width || h > max_height) {
2960 DRM_DEBUG_KMS("CbCr source size %dx%d too big (limit %dx%d)\n",
2961 w, h, max_width, max_height);
2962 return -EINVAL;
2963 }
2964
2965 plane_state->aux.offset = offset;
2966 plane_state->aux.x = x;
2967 plane_state->aux.y = y;
2968
2969 return 0;
2970}
2971
b63a16f6
VS
2972int skl_check_plane_surface(struct intel_plane_state *plane_state)
2973{
2974 const struct drm_framebuffer *fb = plane_state->base.fb;
2975 unsigned int rotation = plane_state->base.rotation;
2976 int ret;
2977
2978 /* Rotate src coordinates to match rotated GTT view */
2979 if (intel_rotation_90_or_270(rotation))
cc926387
DV
2980 drm_rect_rotate(&plane_state->base.src,
2981 fb->width, fb->height, DRM_ROTATE_270);
b63a16f6 2982
8d970654
VS
2983 /*
2984 * Handle the AUX surface first since
2985 * the main surface setup depends on it.
2986 */
2987 if (fb->pixel_format == DRM_FORMAT_NV12) {
2988 ret = skl_check_nv12_aux_surface(plane_state);
2989 if (ret)
2990 return ret;
2991 } else {
2992 plane_state->aux.offset = ~0xfff;
2993 plane_state->aux.x = 0;
2994 plane_state->aux.y = 0;
2995 }
2996
b63a16f6
VS
2997 ret = skl_check_main_surface(plane_state);
2998 if (ret)
2999 return ret;
3000
3001 return 0;
3002}
3003
a8d201af
ML
3004static void i9xx_update_primary_plane(struct drm_plane *primary,
3005 const struct intel_crtc_state *crtc_state,
3006 const struct intel_plane_state *plane_state)
81255565 3007{
a8d201af 3008 struct drm_device *dev = primary->dev;
fac5e23e 3009 struct drm_i915_private *dev_priv = to_i915(dev);
a8d201af
ML
3010 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
3011 struct drm_framebuffer *fb = plane_state->base.fb;
3012 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
81255565 3013 int plane = intel_crtc->plane;
54ea9da8 3014 u32 linear_offset;
81255565 3015 u32 dspcntr;
f0f59a00 3016 i915_reg_t reg = DSPCNTR(plane);
8d0deca8 3017 unsigned int rotation = plane_state->base.rotation;
936e71e3
VS
3018 int x = plane_state->base.src.x1 >> 16;
3019 int y = plane_state->base.src.y1 >> 16;
c9ba6fad 3020
f45651ba
VS
3021 dspcntr = DISPPLANE_GAMMA_ENABLE;
3022
fdd508a6 3023 dspcntr |= DISPLAY_PLANE_ENABLE;
f45651ba
VS
3024
3025 if (INTEL_INFO(dev)->gen < 4) {
3026 if (intel_crtc->pipe == PIPE_B)
3027 dspcntr |= DISPPLANE_SEL_PIPE_B;
3028
3029 /* pipesrc and dspsize control the size that is scaled from,
3030 * which should always be the user's requested size.
3031 */
3032 I915_WRITE(DSPSIZE(plane),
a8d201af
ML
3033 ((crtc_state->pipe_src_h - 1) << 16) |
3034 (crtc_state->pipe_src_w - 1));
f45651ba 3035 I915_WRITE(DSPPOS(plane), 0);
c14b0485
VS
3036 } else if (IS_CHERRYVIEW(dev) && plane == PLANE_B) {
3037 I915_WRITE(PRIMSIZE(plane),
a8d201af
ML
3038 ((crtc_state->pipe_src_h - 1) << 16) |
3039 (crtc_state->pipe_src_w - 1));
c14b0485
VS
3040 I915_WRITE(PRIMPOS(plane), 0);
3041 I915_WRITE(PRIMCNSTALPHA(plane), 0);
f45651ba 3042 }
81255565 3043
57779d06
VS
3044 switch (fb->pixel_format) {
3045 case DRM_FORMAT_C8:
81255565
JB
3046 dspcntr |= DISPPLANE_8BPP;
3047 break;
57779d06 3048 case DRM_FORMAT_XRGB1555:
57779d06 3049 dspcntr |= DISPPLANE_BGRX555;
81255565 3050 break;
57779d06
VS
3051 case DRM_FORMAT_RGB565:
3052 dspcntr |= DISPPLANE_BGRX565;
3053 break;
3054 case DRM_FORMAT_XRGB8888:
57779d06
VS
3055 dspcntr |= DISPPLANE_BGRX888;
3056 break;
3057 case DRM_FORMAT_XBGR8888:
57779d06
VS
3058 dspcntr |= DISPPLANE_RGBX888;
3059 break;
3060 case DRM_FORMAT_XRGB2101010:
57779d06
VS
3061 dspcntr |= DISPPLANE_BGRX101010;
3062 break;
3063 case DRM_FORMAT_XBGR2101010:
57779d06 3064 dspcntr |= DISPPLANE_RGBX101010;
81255565
JB
3065 break;
3066 default:
baba133a 3067 BUG();
81255565 3068 }
57779d06 3069
72618ebf
VS
3070 if (INTEL_GEN(dev_priv) >= 4 &&
3071 fb->modifier[0] == I915_FORMAT_MOD_X_TILED)
f45651ba 3072 dspcntr |= DISPPLANE_TILED;
81255565 3073
de1aa629
VS
3074 if (IS_G4X(dev))
3075 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
3076
2949056c 3077 intel_add_fb_offsets(&x, &y, plane_state, 0);
81255565 3078
6687c906 3079 if (INTEL_INFO(dev)->gen >= 4)
c2c75131 3080 intel_crtc->dspaddr_offset =
2949056c 3081 intel_compute_tile_offset(&x, &y, plane_state, 0);
e506a0c6 3082
31ad61e4 3083 if (rotation == DRM_ROTATE_180) {
48404c1e
SJ
3084 dspcntr |= DISPPLANE_ROTATE_180;
3085
a8d201af
ML
3086 x += (crtc_state->pipe_src_w - 1);
3087 y += (crtc_state->pipe_src_h - 1);
48404c1e
SJ
3088 }
3089
2949056c 3090 linear_offset = intel_fb_xy_to_linear(x, y, plane_state, 0);
6687c906
VS
3091
3092 if (INTEL_INFO(dev)->gen < 4)
3093 intel_crtc->dspaddr_offset = linear_offset;
3094
2db3366b
PZ
3095 intel_crtc->adjusted_x = x;
3096 intel_crtc->adjusted_y = y;
3097
48404c1e
SJ
3098 I915_WRITE(reg, dspcntr);
3099
01f2c773 3100 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
a6c45cf0 3101 if (INTEL_INFO(dev)->gen >= 4) {
85ba7b7d 3102 I915_WRITE(DSPSURF(plane),
6687c906
VS
3103 intel_fb_gtt_offset(fb, rotation) +
3104 intel_crtc->dspaddr_offset);
5eddb70b 3105 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
e506a0c6 3106 I915_WRITE(DSPLINOFF(plane), linear_offset);
5eddb70b 3107 } else
058d88c4 3108 I915_WRITE(DSPADDR(plane), i915_gem_object_ggtt_offset(obj, NULL) + linear_offset);
5eddb70b 3109 POSTING_READ(reg);
17638cd6
JB
3110}
3111
a8d201af
ML
3112static void i9xx_disable_primary_plane(struct drm_plane *primary,
3113 struct drm_crtc *crtc)
17638cd6
JB
3114{
3115 struct drm_device *dev = crtc->dev;
fac5e23e 3116 struct drm_i915_private *dev_priv = to_i915(dev);
17638cd6 3117 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
17638cd6 3118 int plane = intel_crtc->plane;
f45651ba 3119
a8d201af
ML
3120 I915_WRITE(DSPCNTR(plane), 0);
3121 if (INTEL_INFO(dev_priv)->gen >= 4)
fdd508a6 3122 I915_WRITE(DSPSURF(plane), 0);
a8d201af
ML
3123 else
3124 I915_WRITE(DSPADDR(plane), 0);
3125 POSTING_READ(DSPCNTR(plane));
3126}
c9ba6fad 3127
a8d201af
ML
3128static void ironlake_update_primary_plane(struct drm_plane *primary,
3129 const struct intel_crtc_state *crtc_state,
3130 const struct intel_plane_state *plane_state)
3131{
3132 struct drm_device *dev = primary->dev;
fac5e23e 3133 struct drm_i915_private *dev_priv = to_i915(dev);
a8d201af
ML
3134 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
3135 struct drm_framebuffer *fb = plane_state->base.fb;
a8d201af 3136 int plane = intel_crtc->plane;
54ea9da8 3137 u32 linear_offset;
a8d201af
ML
3138 u32 dspcntr;
3139 i915_reg_t reg = DSPCNTR(plane);
8d0deca8 3140 unsigned int rotation = plane_state->base.rotation;
936e71e3
VS
3141 int x = plane_state->base.src.x1 >> 16;
3142 int y = plane_state->base.src.y1 >> 16;
c9ba6fad 3143
f45651ba 3144 dspcntr = DISPPLANE_GAMMA_ENABLE;
fdd508a6 3145 dspcntr |= DISPLAY_PLANE_ENABLE;
f45651ba
VS
3146
3147 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
3148 dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
17638cd6 3149
57779d06
VS
3150 switch (fb->pixel_format) {
3151 case DRM_FORMAT_C8:
17638cd6
JB
3152 dspcntr |= DISPPLANE_8BPP;
3153 break;
57779d06
VS
3154 case DRM_FORMAT_RGB565:
3155 dspcntr |= DISPPLANE_BGRX565;
17638cd6 3156 break;
57779d06 3157 case DRM_FORMAT_XRGB8888:
57779d06
VS
3158 dspcntr |= DISPPLANE_BGRX888;
3159 break;
3160 case DRM_FORMAT_XBGR8888:
57779d06
VS
3161 dspcntr |= DISPPLANE_RGBX888;
3162 break;
3163 case DRM_FORMAT_XRGB2101010:
57779d06
VS
3164 dspcntr |= DISPPLANE_BGRX101010;
3165 break;
3166 case DRM_FORMAT_XBGR2101010:
57779d06 3167 dspcntr |= DISPPLANE_RGBX101010;
17638cd6
JB
3168 break;
3169 default:
baba133a 3170 BUG();
17638cd6
JB
3171 }
3172
72618ebf 3173 if (fb->modifier[0] == I915_FORMAT_MOD_X_TILED)
17638cd6 3174 dspcntr |= DISPPLANE_TILED;
17638cd6 3175
f45651ba 3176 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev))
1f5d76db 3177 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
17638cd6 3178
2949056c 3179 intel_add_fb_offsets(&x, &y, plane_state, 0);
6687c906 3180
c2c75131 3181 intel_crtc->dspaddr_offset =
2949056c 3182 intel_compute_tile_offset(&x, &y, plane_state, 0);
6687c906 3183
31ad61e4 3184 if (rotation == DRM_ROTATE_180) {
48404c1e
SJ
3185 dspcntr |= DISPPLANE_ROTATE_180;
3186
3187 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev)) {
a8d201af
ML
3188 x += (crtc_state->pipe_src_w - 1);
3189 y += (crtc_state->pipe_src_h - 1);
48404c1e
SJ
3190 }
3191 }
3192
2949056c 3193 linear_offset = intel_fb_xy_to_linear(x, y, plane_state, 0);
6687c906 3194
2db3366b
PZ
3195 intel_crtc->adjusted_x = x;
3196 intel_crtc->adjusted_y = y;
3197
48404c1e 3198 I915_WRITE(reg, dspcntr);
17638cd6 3199
01f2c773 3200 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
85ba7b7d 3201 I915_WRITE(DSPSURF(plane),
6687c906
VS
3202 intel_fb_gtt_offset(fb, rotation) +
3203 intel_crtc->dspaddr_offset);
b3dc685e 3204 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
bc1c91eb
DL
3205 I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
3206 } else {
3207 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
3208 I915_WRITE(DSPLINOFF(plane), linear_offset);
3209 }
17638cd6 3210 POSTING_READ(reg);
17638cd6
JB
3211}
3212
7b49f948
VS
3213u32 intel_fb_stride_alignment(const struct drm_i915_private *dev_priv,
3214 uint64_t fb_modifier, uint32_t pixel_format)
b321803d 3215{
7b49f948 3216 if (fb_modifier == DRM_FORMAT_MOD_NONE) {
b321803d 3217 return 64;
7b49f948
VS
3218 } else {
3219 int cpp = drm_format_plane_cpp(pixel_format, 0);
3220
27ba3910 3221 return intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
b321803d
DL
3222 }
3223}
3224
6687c906
VS
3225u32 intel_fb_gtt_offset(struct drm_framebuffer *fb,
3226 unsigned int rotation)
121920fa 3227{
6687c906 3228 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
ce7f1728 3229 struct i915_ggtt_view view;
058d88c4 3230 struct i915_vma *vma;
121920fa 3231
6687c906 3232 intel_fill_fb_ggtt_view(&view, fb, rotation);
dedf278c 3233
058d88c4
CW
3234 vma = i915_gem_object_to_ggtt(obj, &view);
3235 if (WARN(!vma, "ggtt vma for display object not found! (view=%u)\n",
3236 view.type))
3237 return -1;
3238
bde13ebd 3239 return i915_ggtt_offset(vma);
121920fa
TU
3240}
3241
e435d6e5
ML
3242static void skl_detach_scaler(struct intel_crtc *intel_crtc, int id)
3243{
3244 struct drm_device *dev = intel_crtc->base.dev;
fac5e23e 3245 struct drm_i915_private *dev_priv = to_i915(dev);
e435d6e5
ML
3246
3247 I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, id), 0);
3248 I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, id), 0);
3249 I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, id), 0);
e435d6e5
ML
3250}
3251
a1b2278e
CK
3252/*
3253 * This function detaches (aka. unbinds) unused scalers in hardware
3254 */
0583236e 3255static void skl_detach_scalers(struct intel_crtc *intel_crtc)
a1b2278e 3256{
a1b2278e
CK
3257 struct intel_crtc_scaler_state *scaler_state;
3258 int i;
3259
a1b2278e
CK
3260 scaler_state = &intel_crtc->config->scaler_state;
3261
3262 /* loop through and disable scalers that aren't in use */
3263 for (i = 0; i < intel_crtc->num_scalers; i++) {
e435d6e5
ML
3264 if (!scaler_state->scalers[i].in_use)
3265 skl_detach_scaler(intel_crtc, i);
a1b2278e
CK
3266 }
3267}
3268
d2196774
VS
3269u32 skl_plane_stride(const struct drm_framebuffer *fb, int plane,
3270 unsigned int rotation)
3271{
3272 const struct drm_i915_private *dev_priv = to_i915(fb->dev);
3273 u32 stride = intel_fb_pitch(fb, plane, rotation);
3274
3275 /*
3276 * The stride is either expressed as a multiple of 64 bytes chunks for
3277 * linear buffers or in number of tiles for tiled buffers.
3278 */
3279 if (intel_rotation_90_or_270(rotation)) {
3280 int cpp = drm_format_plane_cpp(fb->pixel_format, plane);
3281
3282 stride /= intel_tile_height(dev_priv, fb->modifier[0], cpp);
3283 } else {
3284 stride /= intel_fb_stride_alignment(dev_priv, fb->modifier[0],
3285 fb->pixel_format);
3286 }
3287
3288 return stride;
3289}
3290
6156a456 3291u32 skl_plane_ctl_format(uint32_t pixel_format)
70d21f0e 3292{
6156a456 3293 switch (pixel_format) {
d161cf7a 3294 case DRM_FORMAT_C8:
c34ce3d1 3295 return PLANE_CTL_FORMAT_INDEXED;
70d21f0e 3296 case DRM_FORMAT_RGB565:
c34ce3d1 3297 return PLANE_CTL_FORMAT_RGB_565;
70d21f0e 3298 case DRM_FORMAT_XBGR8888:
c34ce3d1 3299 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
6156a456 3300 case DRM_FORMAT_XRGB8888:
c34ce3d1 3301 return PLANE_CTL_FORMAT_XRGB_8888;
6156a456
CK
3302 /*
3303 * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
3304 * to be already pre-multiplied. We need to add a knob (or a different
3305 * DRM_FORMAT) for user-space to configure that.
3306 */
f75fb42a 3307 case DRM_FORMAT_ABGR8888:
c34ce3d1 3308 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX |
6156a456 3309 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
6156a456 3310 case DRM_FORMAT_ARGB8888:
c34ce3d1 3311 return PLANE_CTL_FORMAT_XRGB_8888 |
6156a456 3312 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
70d21f0e 3313 case DRM_FORMAT_XRGB2101010:
c34ce3d1 3314 return PLANE_CTL_FORMAT_XRGB_2101010;
70d21f0e 3315 case DRM_FORMAT_XBGR2101010:
c34ce3d1 3316 return PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
6156a456 3317 case DRM_FORMAT_YUYV:
c34ce3d1 3318 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
6156a456 3319 case DRM_FORMAT_YVYU:
c34ce3d1 3320 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
6156a456 3321 case DRM_FORMAT_UYVY:
c34ce3d1 3322 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
6156a456 3323 case DRM_FORMAT_VYUY:
c34ce3d1 3324 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
70d21f0e 3325 default:
4249eeef 3326 MISSING_CASE(pixel_format);
70d21f0e 3327 }
8cfcba41 3328
c34ce3d1 3329 return 0;
6156a456 3330}
70d21f0e 3331
6156a456
CK
3332u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
3333{
6156a456 3334 switch (fb_modifier) {
30af77c4 3335 case DRM_FORMAT_MOD_NONE:
70d21f0e 3336 break;
30af77c4 3337 case I915_FORMAT_MOD_X_TILED:
c34ce3d1 3338 return PLANE_CTL_TILED_X;
b321803d 3339 case I915_FORMAT_MOD_Y_TILED:
c34ce3d1 3340 return PLANE_CTL_TILED_Y;
b321803d 3341 case I915_FORMAT_MOD_Yf_TILED:
c34ce3d1 3342 return PLANE_CTL_TILED_YF;
70d21f0e 3343 default:
6156a456 3344 MISSING_CASE(fb_modifier);
70d21f0e 3345 }
8cfcba41 3346
c34ce3d1 3347 return 0;
6156a456 3348}
70d21f0e 3349
6156a456
CK
3350u32 skl_plane_ctl_rotation(unsigned int rotation)
3351{
3b7a5119 3352 switch (rotation) {
31ad61e4 3353 case DRM_ROTATE_0:
6156a456 3354 break;
1e8df167
SJ
3355 /*
3356 * DRM_ROTATE_ is counter clockwise to stay compatible with Xrandr
3357 * while i915 HW rotation is clockwise, thats why this swapping.
3358 */
31ad61e4 3359 case DRM_ROTATE_90:
1e8df167 3360 return PLANE_CTL_ROTATE_270;
31ad61e4 3361 case DRM_ROTATE_180:
c34ce3d1 3362 return PLANE_CTL_ROTATE_180;
31ad61e4 3363 case DRM_ROTATE_270:
1e8df167 3364 return PLANE_CTL_ROTATE_90;
6156a456
CK
3365 default:
3366 MISSING_CASE(rotation);
3367 }
3368
c34ce3d1 3369 return 0;
6156a456
CK
3370}
3371
a8d201af
ML
3372static void skylake_update_primary_plane(struct drm_plane *plane,
3373 const struct intel_crtc_state *crtc_state,
3374 const struct intel_plane_state *plane_state)
6156a456 3375{
a8d201af 3376 struct drm_device *dev = plane->dev;
fac5e23e 3377 struct drm_i915_private *dev_priv = to_i915(dev);
a8d201af
ML
3378 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
3379 struct drm_framebuffer *fb = plane_state->base.fb;
62e0fb88 3380 const struct skl_wm_values *wm = &dev_priv->wm.skl_results;
6156a456 3381 int pipe = intel_crtc->pipe;
d2196774 3382 u32 plane_ctl;
a8d201af 3383 unsigned int rotation = plane_state->base.rotation;
d2196774 3384 u32 stride = skl_plane_stride(fb, 0, rotation);
b63a16f6 3385 u32 surf_addr = plane_state->main.offset;
a8d201af 3386 int scaler_id = plane_state->scaler_id;
b63a16f6
VS
3387 int src_x = plane_state->main.x;
3388 int src_y = plane_state->main.y;
936e71e3
VS
3389 int src_w = drm_rect_width(&plane_state->base.src) >> 16;
3390 int src_h = drm_rect_height(&plane_state->base.src) >> 16;
3391 int dst_x = plane_state->base.dst.x1;
3392 int dst_y = plane_state->base.dst.y1;
3393 int dst_w = drm_rect_width(&plane_state->base.dst);
3394 int dst_h = drm_rect_height(&plane_state->base.dst);
70d21f0e 3395
6156a456
CK
3396 plane_ctl = PLANE_CTL_ENABLE |
3397 PLANE_CTL_PIPE_GAMMA_ENABLE |
3398 PLANE_CTL_PIPE_CSC_ENABLE;
3399
3400 plane_ctl |= skl_plane_ctl_format(fb->pixel_format);
3401 plane_ctl |= skl_plane_ctl_tiling(fb->modifier[0]);
3402 plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
6156a456
CK
3403 plane_ctl |= skl_plane_ctl_rotation(rotation);
3404
6687c906
VS
3405 /* Sizes are 0 based */
3406 src_w--;
3407 src_h--;
3408 dst_w--;
3409 dst_h--;
3410
3411 intel_crtc->adjusted_x = src_x;
3412 intel_crtc->adjusted_y = src_y;
2db3366b 3413
62e0fb88
L
3414 if (wm->dirty_pipes & drm_crtc_mask(&intel_crtc->base))
3415 skl_write_plane_wm(intel_crtc, wm, 0);
3416
70d21f0e 3417 I915_WRITE(PLANE_CTL(pipe, 0), plane_ctl);
6687c906 3418 I915_WRITE(PLANE_OFFSET(pipe, 0), (src_y << 16) | src_x);
ef78ec94 3419 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
6687c906 3420 I915_WRITE(PLANE_SIZE(pipe, 0), (src_h << 16) | src_w);
6156a456
CK
3421
3422 if (scaler_id >= 0) {
3423 uint32_t ps_ctrl = 0;
3424
3425 WARN_ON(!dst_w || !dst_h);
3426 ps_ctrl = PS_SCALER_EN | PS_PLANE_SEL(0) |
3427 crtc_state->scaler_state.scalers[scaler_id].mode;
3428 I915_WRITE(SKL_PS_CTRL(pipe, scaler_id), ps_ctrl);
3429 I915_WRITE(SKL_PS_PWR_GATE(pipe, scaler_id), 0);
3430 I915_WRITE(SKL_PS_WIN_POS(pipe, scaler_id), (dst_x << 16) | dst_y);
3431 I915_WRITE(SKL_PS_WIN_SZ(pipe, scaler_id), (dst_w << 16) | dst_h);
3432 I915_WRITE(PLANE_POS(pipe, 0), 0);
3433 } else {
3434 I915_WRITE(PLANE_POS(pipe, 0), (dst_y << 16) | dst_x);
3435 }
3436
6687c906
VS
3437 I915_WRITE(PLANE_SURF(pipe, 0),
3438 intel_fb_gtt_offset(fb, rotation) + surf_addr);
70d21f0e
DL
3439
3440 POSTING_READ(PLANE_SURF(pipe, 0));
3441}
3442
a8d201af
ML
3443static void skylake_disable_primary_plane(struct drm_plane *primary,
3444 struct drm_crtc *crtc)
17638cd6
JB
3445{
3446 struct drm_device *dev = crtc->dev;
fac5e23e 3447 struct drm_i915_private *dev_priv = to_i915(dev);
62e0fb88
L
3448 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3449 int pipe = intel_crtc->pipe;
3450
3451 skl_write_plane_wm(intel_crtc, &dev_priv->wm.skl_results, 0);
17638cd6 3452
a8d201af
ML
3453 I915_WRITE(PLANE_CTL(pipe, 0), 0);
3454 I915_WRITE(PLANE_SURF(pipe, 0), 0);
3455 POSTING_READ(PLANE_SURF(pipe, 0));
3456}
29b9bde6 3457
a8d201af
ML
3458/* Assume fb object is pinned & idle & fenced and just update base pointers */
3459static int
3460intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
3461 int x, int y, enum mode_set_atomic state)
3462{
3463 /* Support for kgdboc is disabled, this needs a major rework. */
3464 DRM_ERROR("legacy panic handler not supported any more.\n");
3465
3466 return -ENODEV;
81255565
JB
3467}
3468
5a21b665
DV
3469static void intel_complete_page_flips(struct drm_i915_private *dev_priv)
3470{
3471 struct intel_crtc *crtc;
3472
91c8a326 3473 for_each_intel_crtc(&dev_priv->drm, crtc)
5a21b665
DV
3474 intel_finish_page_flip_cs(dev_priv, crtc->pipe);
3475}
3476
7514747d
VS
3477static void intel_update_primary_planes(struct drm_device *dev)
3478{
7514747d 3479 struct drm_crtc *crtc;
96a02917 3480
70e1e0ec 3481 for_each_crtc(dev, crtc) {
11c22da6 3482 struct intel_plane *plane = to_intel_plane(crtc->primary);
73974893
ML
3483 struct intel_plane_state *plane_state =
3484 to_intel_plane_state(plane->base.state);
11c22da6 3485
936e71e3 3486 if (plane_state->base.visible)
a8d201af
ML
3487 plane->update_plane(&plane->base,
3488 to_intel_crtc_state(crtc->state),
3489 plane_state);
73974893
ML
3490 }
3491}
3492
3493static int
3494__intel_display_resume(struct drm_device *dev,
3495 struct drm_atomic_state *state)
3496{
3497 struct drm_crtc_state *crtc_state;
3498 struct drm_crtc *crtc;
3499 int i, ret;
11c22da6 3500
73974893
ML
3501 intel_modeset_setup_hw_state(dev);
3502 i915_redisable_vga(dev);
3503
3504 if (!state)
3505 return 0;
3506
3507 for_each_crtc_in_state(state, crtc, crtc_state, i) {
3508 /*
3509 * Force recalculation even if we restore
3510 * current state. With fast modeset this may not result
3511 * in a modeset when the state is compatible.
3512 */
3513 crtc_state->mode_changed = true;
96a02917 3514 }
73974893
ML
3515
3516 /* ignore any reset values/BIOS leftovers in the WM registers */
3517 to_intel_atomic_state(state)->skip_intermediate_wm = true;
3518
3519 ret = drm_atomic_commit(state);
3520
3521 WARN_ON(ret == -EDEADLK);
3522 return ret;
96a02917
VS
3523}
3524
4ac2ba2f
VS
3525static bool gpu_reset_clobbers_display(struct drm_i915_private *dev_priv)
3526{
ae98104b
VS
3527 return intel_has_gpu_reset(dev_priv) &&
3528 INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv);
4ac2ba2f
VS
3529}
3530
c033666a 3531void intel_prepare_reset(struct drm_i915_private *dev_priv)
7514747d 3532{
73974893
ML
3533 struct drm_device *dev = &dev_priv->drm;
3534 struct drm_modeset_acquire_ctx *ctx = &dev_priv->reset_ctx;
3535 struct drm_atomic_state *state;
3536 int ret;
3537
73974893
ML
3538 /*
3539 * Need mode_config.mutex so that we don't
3540 * trample ongoing ->detect() and whatnot.
3541 */
3542 mutex_lock(&dev->mode_config.mutex);
3543 drm_modeset_acquire_init(ctx, 0);
3544 while (1) {
3545 ret = drm_modeset_lock_all_ctx(dev, ctx);
3546 if (ret != -EDEADLK)
3547 break;
3548
3549 drm_modeset_backoff(ctx);
3550 }
3551
3552 /* reset doesn't touch the display, but flips might get nuked anyway, */
522a63de 3553 if (!i915.force_reset_modeset_test &&
4ac2ba2f 3554 !gpu_reset_clobbers_display(dev_priv))
7514747d
VS
3555 return;
3556
f98ce92f
VS
3557 /*
3558 * Disabling the crtcs gracefully seems nicer. Also the
3559 * g33 docs say we should at least disable all the planes.
3560 */
73974893
ML
3561 state = drm_atomic_helper_duplicate_state(dev, ctx);
3562 if (IS_ERR(state)) {
3563 ret = PTR_ERR(state);
3564 state = NULL;
3565 DRM_ERROR("Duplicating state failed with %i\n", ret);
3566 goto err;
3567 }
3568
3569 ret = drm_atomic_helper_disable_all(dev, ctx);
3570 if (ret) {
3571 DRM_ERROR("Suspending crtc's failed with %i\n", ret);
3572 goto err;
3573 }
3574
3575 dev_priv->modeset_restore_state = state;
3576 state->acquire_ctx = ctx;
3577 return;
3578
3579err:
3580 drm_atomic_state_free(state);
7514747d
VS
3581}
3582
c033666a 3583void intel_finish_reset(struct drm_i915_private *dev_priv)
7514747d 3584{
73974893
ML
3585 struct drm_device *dev = &dev_priv->drm;
3586 struct drm_modeset_acquire_ctx *ctx = &dev_priv->reset_ctx;
3587 struct drm_atomic_state *state = dev_priv->modeset_restore_state;
3588 int ret;
3589
5a21b665
DV
3590 /*
3591 * Flips in the rings will be nuked by the reset,
3592 * so complete all pending flips so that user space
3593 * will get its events and not get stuck.
3594 */
3595 intel_complete_page_flips(dev_priv);
3596
73974893
ML
3597 dev_priv->modeset_restore_state = NULL;
3598
7514747d 3599 /* reset doesn't touch the display */
4ac2ba2f 3600 if (!gpu_reset_clobbers_display(dev_priv)) {
522a63de
ML
3601 if (!state) {
3602 /*
3603 * Flips in the rings have been nuked by the reset,
3604 * so update the base address of all primary
3605 * planes to the the last fb to make sure we're
3606 * showing the correct fb after a reset.
3607 *
3608 * FIXME: Atomic will make this obsolete since we won't schedule
3609 * CS-based flips (which might get lost in gpu resets) any more.
3610 */
3611 intel_update_primary_planes(dev);
3612 } else {
3613 ret = __intel_display_resume(dev, state);
3614 if (ret)
3615 DRM_ERROR("Restoring old state failed with %i\n", ret);
3616 }
73974893
ML
3617 } else {
3618 /*
3619 * The display has been reset as well,
3620 * so need a full re-initialization.
3621 */
3622 intel_runtime_pm_disable_interrupts(dev_priv);
3623 intel_runtime_pm_enable_interrupts(dev_priv);
7514747d 3624
73974893 3625 intel_modeset_init_hw(dev);
7514747d 3626
73974893
ML
3627 spin_lock_irq(&dev_priv->irq_lock);
3628 if (dev_priv->display.hpd_irq_setup)
3629 dev_priv->display.hpd_irq_setup(dev_priv);
3630 spin_unlock_irq(&dev_priv->irq_lock);
7514747d 3631
73974893
ML
3632 ret = __intel_display_resume(dev, state);
3633 if (ret)
3634 DRM_ERROR("Restoring old state failed with %i\n", ret);
7514747d 3635
73974893
ML
3636 intel_hpd_init(dev_priv);
3637 }
7514747d 3638
73974893
ML
3639 drm_modeset_drop_locks(ctx);
3640 drm_modeset_acquire_fini(ctx);
3641 mutex_unlock(&dev->mode_config.mutex);
7514747d
VS
3642}
3643
7d5e3799
CW
3644static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
3645{
5a21b665
DV
3646 struct drm_device *dev = crtc->dev;
3647 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3648 unsigned reset_counter;
3649 bool pending;
3650
3651 reset_counter = i915_reset_counter(&to_i915(dev)->gpu_error);
3652 if (intel_crtc->reset_counter != reset_counter)
3653 return false;
3654
3655 spin_lock_irq(&dev->event_lock);
3656 pending = to_intel_crtc(crtc)->flip_work != NULL;
3657 spin_unlock_irq(&dev->event_lock);
3658
3659 return pending;
7d5e3799
CW
3660}
3661
bfd16b2a
ML
3662static void intel_update_pipe_config(struct intel_crtc *crtc,
3663 struct intel_crtc_state *old_crtc_state)
e30e8f75
GP
3664{
3665 struct drm_device *dev = crtc->base.dev;
fac5e23e 3666 struct drm_i915_private *dev_priv = to_i915(dev);
bfd16b2a
ML
3667 struct intel_crtc_state *pipe_config =
3668 to_intel_crtc_state(crtc->base.state);
e30e8f75 3669
bfd16b2a
ML
3670 /* drm_atomic_helper_update_legacy_modeset_state might not be called. */
3671 crtc->base.mode = crtc->base.state->mode;
3672
3673 DRM_DEBUG_KMS("Updating pipe size %ix%i -> %ix%i\n",
3674 old_crtc_state->pipe_src_w, old_crtc_state->pipe_src_h,
3675 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
e30e8f75
GP
3676
3677 /*
3678 * Update pipe size and adjust fitter if needed: the reason for this is
3679 * that in compute_mode_changes we check the native mode (not the pfit
3680 * mode) to see if we can flip rather than do a full mode set. In the
3681 * fastboot case, we'll flip, but if we don't update the pipesrc and
3682 * pfit state, we'll end up with a big fb scanned out into the wrong
3683 * sized surface.
e30e8f75
GP
3684 */
3685
e30e8f75 3686 I915_WRITE(PIPESRC(crtc->pipe),
bfd16b2a
ML
3687 ((pipe_config->pipe_src_w - 1) << 16) |
3688 (pipe_config->pipe_src_h - 1));
3689
3690 /* on skylake this is done by detaching scalers */
3691 if (INTEL_INFO(dev)->gen >= 9) {
3692 skl_detach_scalers(crtc);
3693
3694 if (pipe_config->pch_pfit.enabled)
3695 skylake_pfit_enable(crtc);
3696 } else if (HAS_PCH_SPLIT(dev)) {
3697 if (pipe_config->pch_pfit.enabled)
3698 ironlake_pfit_enable(crtc);
3699 else if (old_crtc_state->pch_pfit.enabled)
3700 ironlake_pfit_disable(crtc, true);
e30e8f75 3701 }
e30e8f75
GP
3702}
3703
5e84e1a4
ZW
3704static void intel_fdi_normal_train(struct drm_crtc *crtc)
3705{
3706 struct drm_device *dev = crtc->dev;
fac5e23e 3707 struct drm_i915_private *dev_priv = to_i915(dev);
5e84e1a4
ZW
3708 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3709 int pipe = intel_crtc->pipe;
f0f59a00
VS
3710 i915_reg_t reg;
3711 u32 temp;
5e84e1a4
ZW
3712
3713 /* enable normal train */
3714 reg = FDI_TX_CTL(pipe);
3715 temp = I915_READ(reg);
61e499bf 3716 if (IS_IVYBRIDGE(dev)) {
357555c0
JB
3717 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3718 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
61e499bf
KP
3719 } else {
3720 temp &= ~FDI_LINK_TRAIN_NONE;
3721 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
357555c0 3722 }
5e84e1a4
ZW
3723 I915_WRITE(reg, temp);
3724
3725 reg = FDI_RX_CTL(pipe);
3726 temp = I915_READ(reg);
3727 if (HAS_PCH_CPT(dev)) {
3728 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3729 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
3730 } else {
3731 temp &= ~FDI_LINK_TRAIN_NONE;
3732 temp |= FDI_LINK_TRAIN_NONE;
3733 }
3734 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
3735
3736 /* wait one idle pattern time */
3737 POSTING_READ(reg);
3738 udelay(1000);
357555c0
JB
3739
3740 /* IVB wants error correction enabled */
3741 if (IS_IVYBRIDGE(dev))
3742 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
3743 FDI_FE_ERRC_ENABLE);
5e84e1a4
ZW
3744}
3745
8db9d77b
ZW
3746/* The FDI link training functions for ILK/Ibexpeak. */
3747static void ironlake_fdi_link_train(struct drm_crtc *crtc)
3748{
3749 struct drm_device *dev = crtc->dev;
fac5e23e 3750 struct drm_i915_private *dev_priv = to_i915(dev);
8db9d77b
ZW
3751 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3752 int pipe = intel_crtc->pipe;
f0f59a00
VS
3753 i915_reg_t reg;
3754 u32 temp, tries;
8db9d77b 3755
1c8562f6 3756 /* FDI needs bits from pipe first */
0fc932b8 3757 assert_pipe_enabled(dev_priv, pipe);
0fc932b8 3758
e1a44743
AJ
3759 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3760 for train result */
5eddb70b
CW
3761 reg = FDI_RX_IMR(pipe);
3762 temp = I915_READ(reg);
e1a44743
AJ
3763 temp &= ~FDI_RX_SYMBOL_LOCK;
3764 temp &= ~FDI_RX_BIT_LOCK;
5eddb70b
CW
3765 I915_WRITE(reg, temp);
3766 I915_READ(reg);
e1a44743
AJ
3767 udelay(150);
3768
8db9d77b 3769 /* enable CPU FDI TX and PCH FDI RX */
5eddb70b
CW
3770 reg = FDI_TX_CTL(pipe);
3771 temp = I915_READ(reg);
627eb5a3 3772 temp &= ~FDI_DP_PORT_WIDTH_MASK;
6e3c9717 3773 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
8db9d77b
ZW
3774 temp &= ~FDI_LINK_TRAIN_NONE;
3775 temp |= FDI_LINK_TRAIN_PATTERN_1;
5eddb70b 3776 I915_WRITE(reg, temp | FDI_TX_ENABLE);
8db9d77b 3777
5eddb70b
CW
3778 reg = FDI_RX_CTL(pipe);
3779 temp = I915_READ(reg);
8db9d77b
ZW
3780 temp &= ~FDI_LINK_TRAIN_NONE;
3781 temp |= FDI_LINK_TRAIN_PATTERN_1;
5eddb70b
CW
3782 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3783
3784 POSTING_READ(reg);
8db9d77b
ZW
3785 udelay(150);
3786
5b2adf89 3787 /* Ironlake workaround, enable clock pointer after FDI enable*/
8f5718a6
DV
3788 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
3789 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
3790 FDI_RX_PHASE_SYNC_POINTER_EN);
5b2adf89 3791
5eddb70b 3792 reg = FDI_RX_IIR(pipe);
e1a44743 3793 for (tries = 0; tries < 5; tries++) {
5eddb70b 3794 temp = I915_READ(reg);
8db9d77b
ZW
3795 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3796
3797 if ((temp & FDI_RX_BIT_LOCK)) {
3798 DRM_DEBUG_KMS("FDI train 1 done.\n");
5eddb70b 3799 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
8db9d77b
ZW
3800 break;
3801 }
8db9d77b 3802 }
e1a44743 3803 if (tries == 5)
5eddb70b 3804 DRM_ERROR("FDI train 1 fail!\n");
8db9d77b
ZW
3805
3806 /* Train 2 */
5eddb70b
CW
3807 reg = FDI_TX_CTL(pipe);
3808 temp = I915_READ(reg);
8db9d77b
ZW
3809 temp &= ~FDI_LINK_TRAIN_NONE;
3810 temp |= FDI_LINK_TRAIN_PATTERN_2;
5eddb70b 3811 I915_WRITE(reg, temp);
8db9d77b 3812
5eddb70b
CW
3813 reg = FDI_RX_CTL(pipe);
3814 temp = I915_READ(reg);
8db9d77b
ZW
3815 temp &= ~FDI_LINK_TRAIN_NONE;
3816 temp |= FDI_LINK_TRAIN_PATTERN_2;
5eddb70b 3817 I915_WRITE(reg, temp);
8db9d77b 3818
5eddb70b
CW
3819 POSTING_READ(reg);
3820 udelay(150);
8db9d77b 3821
5eddb70b 3822 reg = FDI_RX_IIR(pipe);
e1a44743 3823 for (tries = 0; tries < 5; tries++) {
5eddb70b 3824 temp = I915_READ(reg);
8db9d77b
ZW
3825 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3826
3827 if (temp & FDI_RX_SYMBOL_LOCK) {
5eddb70b 3828 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
8db9d77b
ZW
3829 DRM_DEBUG_KMS("FDI train 2 done.\n");
3830 break;
3831 }
8db9d77b 3832 }
e1a44743 3833 if (tries == 5)
5eddb70b 3834 DRM_ERROR("FDI train 2 fail!\n");
8db9d77b
ZW
3835
3836 DRM_DEBUG_KMS("FDI train done\n");
5c5313c8 3837
8db9d77b
ZW
3838}
3839
0206e353 3840static const int snb_b_fdi_train_param[] = {
8db9d77b
ZW
3841 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
3842 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
3843 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
3844 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
3845};
3846
3847/* The FDI link training functions for SNB/Cougarpoint. */
3848static void gen6_fdi_link_train(struct drm_crtc *crtc)
3849{
3850 struct drm_device *dev = crtc->dev;
fac5e23e 3851 struct drm_i915_private *dev_priv = to_i915(dev);
8db9d77b
ZW
3852 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3853 int pipe = intel_crtc->pipe;
f0f59a00
VS
3854 i915_reg_t reg;
3855 u32 temp, i, retry;
8db9d77b 3856
e1a44743
AJ
3857 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3858 for train result */
5eddb70b
CW
3859 reg = FDI_RX_IMR(pipe);
3860 temp = I915_READ(reg);
e1a44743
AJ
3861 temp &= ~FDI_RX_SYMBOL_LOCK;
3862 temp &= ~FDI_RX_BIT_LOCK;
5eddb70b
CW
3863 I915_WRITE(reg, temp);
3864
3865 POSTING_READ(reg);
e1a44743
AJ
3866 udelay(150);
3867
8db9d77b 3868 /* enable CPU FDI TX and PCH FDI RX */
5eddb70b
CW
3869 reg = FDI_TX_CTL(pipe);
3870 temp = I915_READ(reg);
627eb5a3 3871 temp &= ~FDI_DP_PORT_WIDTH_MASK;
6e3c9717 3872 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
8db9d77b
ZW
3873 temp &= ~FDI_LINK_TRAIN_NONE;
3874 temp |= FDI_LINK_TRAIN_PATTERN_1;
3875 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3876 /* SNB-B */
3877 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
5eddb70b 3878 I915_WRITE(reg, temp | FDI_TX_ENABLE);
8db9d77b 3879
d74cf324
DV
3880 I915_WRITE(FDI_RX_MISC(pipe),
3881 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3882
5eddb70b
CW
3883 reg = FDI_RX_CTL(pipe);
3884 temp = I915_READ(reg);
8db9d77b
ZW
3885 if (HAS_PCH_CPT(dev)) {
3886 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3887 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3888 } else {
3889 temp &= ~FDI_LINK_TRAIN_NONE;
3890 temp |= FDI_LINK_TRAIN_PATTERN_1;
3891 }
5eddb70b
CW
3892 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3893
3894 POSTING_READ(reg);
8db9d77b
ZW
3895 udelay(150);
3896
0206e353 3897 for (i = 0; i < 4; i++) {
5eddb70b
CW
3898 reg = FDI_TX_CTL(pipe);
3899 temp = I915_READ(reg);
8db9d77b
ZW
3900 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3901 temp |= snb_b_fdi_train_param[i];
5eddb70b
CW
3902 I915_WRITE(reg, temp);
3903
3904 POSTING_READ(reg);
8db9d77b
ZW
3905 udelay(500);
3906
fa37d39e
SP
3907 for (retry = 0; retry < 5; retry++) {
3908 reg = FDI_RX_IIR(pipe);
3909 temp = I915_READ(reg);
3910 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3911 if (temp & FDI_RX_BIT_LOCK) {
3912 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3913 DRM_DEBUG_KMS("FDI train 1 done.\n");
3914 break;
3915 }
3916 udelay(50);
8db9d77b 3917 }
fa37d39e
SP
3918 if (retry < 5)
3919 break;
8db9d77b
ZW
3920 }
3921 if (i == 4)
5eddb70b 3922 DRM_ERROR("FDI train 1 fail!\n");
8db9d77b
ZW
3923
3924 /* Train 2 */
5eddb70b
CW
3925 reg = FDI_TX_CTL(pipe);
3926 temp = I915_READ(reg);
8db9d77b
ZW
3927 temp &= ~FDI_LINK_TRAIN_NONE;
3928 temp |= FDI_LINK_TRAIN_PATTERN_2;
3929 if (IS_GEN6(dev)) {
3930 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3931 /* SNB-B */
3932 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
3933 }
5eddb70b 3934 I915_WRITE(reg, temp);
8db9d77b 3935
5eddb70b
CW
3936 reg = FDI_RX_CTL(pipe);
3937 temp = I915_READ(reg);
8db9d77b
ZW
3938 if (HAS_PCH_CPT(dev)) {
3939 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3940 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
3941 } else {
3942 temp &= ~FDI_LINK_TRAIN_NONE;
3943 temp |= FDI_LINK_TRAIN_PATTERN_2;
3944 }
5eddb70b
CW
3945 I915_WRITE(reg, temp);
3946
3947 POSTING_READ(reg);
8db9d77b
ZW
3948 udelay(150);
3949
0206e353 3950 for (i = 0; i < 4; i++) {
5eddb70b
CW
3951 reg = FDI_TX_CTL(pipe);
3952 temp = I915_READ(reg);
8db9d77b
ZW
3953 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3954 temp |= snb_b_fdi_train_param[i];
5eddb70b
CW
3955 I915_WRITE(reg, temp);
3956
3957 POSTING_READ(reg);
8db9d77b
ZW
3958 udelay(500);
3959
fa37d39e
SP
3960 for (retry = 0; retry < 5; retry++) {
3961 reg = FDI_RX_IIR(pipe);
3962 temp = I915_READ(reg);
3963 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3964 if (temp & FDI_RX_SYMBOL_LOCK) {
3965 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3966 DRM_DEBUG_KMS("FDI train 2 done.\n");
3967 break;
3968 }
3969 udelay(50);
8db9d77b 3970 }
fa37d39e
SP
3971 if (retry < 5)
3972 break;
8db9d77b
ZW
3973 }
3974 if (i == 4)
5eddb70b 3975 DRM_ERROR("FDI train 2 fail!\n");
8db9d77b
ZW
3976
3977 DRM_DEBUG_KMS("FDI train done.\n");
3978}
3979
357555c0
JB
3980/* Manual link training for Ivy Bridge A0 parts */
3981static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
3982{
3983 struct drm_device *dev = crtc->dev;
fac5e23e 3984 struct drm_i915_private *dev_priv = to_i915(dev);
357555c0
JB
3985 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3986 int pipe = intel_crtc->pipe;
f0f59a00
VS
3987 i915_reg_t reg;
3988 u32 temp, i, j;
357555c0
JB
3989
3990 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3991 for train result */
3992 reg = FDI_RX_IMR(pipe);
3993 temp = I915_READ(reg);
3994 temp &= ~FDI_RX_SYMBOL_LOCK;
3995 temp &= ~FDI_RX_BIT_LOCK;
3996 I915_WRITE(reg, temp);
3997
3998 POSTING_READ(reg);
3999 udelay(150);
4000
01a415fd
DV
4001 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
4002 I915_READ(FDI_RX_IIR(pipe)));
4003
139ccd3f
JB
4004 /* Try each vswing and preemphasis setting twice before moving on */
4005 for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
4006 /* disable first in case we need to retry */
4007 reg = FDI_TX_CTL(pipe);
4008 temp = I915_READ(reg);
4009 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
4010 temp &= ~FDI_TX_ENABLE;
4011 I915_WRITE(reg, temp);
357555c0 4012
139ccd3f
JB
4013 reg = FDI_RX_CTL(pipe);
4014 temp = I915_READ(reg);
4015 temp &= ~FDI_LINK_TRAIN_AUTO;
4016 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
4017 temp &= ~FDI_RX_ENABLE;
4018 I915_WRITE(reg, temp);
357555c0 4019
139ccd3f 4020 /* enable CPU FDI TX and PCH FDI RX */
357555c0
JB
4021 reg = FDI_TX_CTL(pipe);
4022 temp = I915_READ(reg);
139ccd3f 4023 temp &= ~FDI_DP_PORT_WIDTH_MASK;
6e3c9717 4024 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
139ccd3f 4025 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
357555c0 4026 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
139ccd3f
JB
4027 temp |= snb_b_fdi_train_param[j/2];
4028 temp |= FDI_COMPOSITE_SYNC;
4029 I915_WRITE(reg, temp | FDI_TX_ENABLE);
357555c0 4030
139ccd3f
JB
4031 I915_WRITE(FDI_RX_MISC(pipe),
4032 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
357555c0 4033
139ccd3f 4034 reg = FDI_RX_CTL(pipe);
357555c0 4035 temp = I915_READ(reg);
139ccd3f
JB
4036 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
4037 temp |= FDI_COMPOSITE_SYNC;
4038 I915_WRITE(reg, temp | FDI_RX_ENABLE);
357555c0 4039
139ccd3f
JB
4040 POSTING_READ(reg);
4041 udelay(1); /* should be 0.5us */
357555c0 4042
139ccd3f
JB
4043 for (i = 0; i < 4; i++) {
4044 reg = FDI_RX_IIR(pipe);
4045 temp = I915_READ(reg);
4046 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
357555c0 4047
139ccd3f
JB
4048 if (temp & FDI_RX_BIT_LOCK ||
4049 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
4050 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
4051 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
4052 i);
4053 break;
4054 }
4055 udelay(1); /* should be 0.5us */
4056 }
4057 if (i == 4) {
4058 DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
4059 continue;
4060 }
357555c0 4061
139ccd3f 4062 /* Train 2 */
357555c0
JB
4063 reg = FDI_TX_CTL(pipe);
4064 temp = I915_READ(reg);
139ccd3f
JB
4065 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
4066 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
4067 I915_WRITE(reg, temp);
4068
4069 reg = FDI_RX_CTL(pipe);
4070 temp = I915_READ(reg);
4071 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
4072 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
357555c0
JB
4073 I915_WRITE(reg, temp);
4074
4075 POSTING_READ(reg);
139ccd3f 4076 udelay(2); /* should be 1.5us */
357555c0 4077
139ccd3f
JB
4078 for (i = 0; i < 4; i++) {
4079 reg = FDI_RX_IIR(pipe);
4080 temp = I915_READ(reg);
4081 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
357555c0 4082
139ccd3f
JB
4083 if (temp & FDI_RX_SYMBOL_LOCK ||
4084 (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
4085 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
4086 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
4087 i);
4088 goto train_done;
4089 }
4090 udelay(2); /* should be 1.5us */
357555c0 4091 }
139ccd3f
JB
4092 if (i == 4)
4093 DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
357555c0 4094 }
357555c0 4095
139ccd3f 4096train_done:
357555c0
JB
4097 DRM_DEBUG_KMS("FDI train done.\n");
4098}
4099
88cefb6c 4100static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
2c07245f 4101{
88cefb6c 4102 struct drm_device *dev = intel_crtc->base.dev;
fac5e23e 4103 struct drm_i915_private *dev_priv = to_i915(dev);
2c07245f 4104 int pipe = intel_crtc->pipe;
f0f59a00
VS
4105 i915_reg_t reg;
4106 u32 temp;
c64e311e 4107
c98e9dcf 4108 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
5eddb70b
CW
4109 reg = FDI_RX_CTL(pipe);
4110 temp = I915_READ(reg);
627eb5a3 4111 temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
6e3c9717 4112 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
dfd07d72 4113 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
5eddb70b
CW
4114 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
4115
4116 POSTING_READ(reg);
c98e9dcf
JB
4117 udelay(200);
4118
4119 /* Switch from Rawclk to PCDclk */
5eddb70b
CW
4120 temp = I915_READ(reg);
4121 I915_WRITE(reg, temp | FDI_PCDCLK);
4122
4123 POSTING_READ(reg);
c98e9dcf
JB
4124 udelay(200);
4125
20749730
PZ
4126 /* Enable CPU FDI TX PLL, always on for Ironlake */
4127 reg = FDI_TX_CTL(pipe);
4128 temp = I915_READ(reg);
4129 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
4130 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
5eddb70b 4131
20749730
PZ
4132 POSTING_READ(reg);
4133 udelay(100);
6be4a607 4134 }
0e23b99d
JB
4135}
4136
88cefb6c
DV
4137static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
4138{
4139 struct drm_device *dev = intel_crtc->base.dev;
fac5e23e 4140 struct drm_i915_private *dev_priv = to_i915(dev);
88cefb6c 4141 int pipe = intel_crtc->pipe;
f0f59a00
VS
4142 i915_reg_t reg;
4143 u32 temp;
88cefb6c
DV
4144
4145 /* Switch from PCDclk to Rawclk */
4146 reg = FDI_RX_CTL(pipe);
4147 temp = I915_READ(reg);
4148 I915_WRITE(reg, temp & ~FDI_PCDCLK);
4149
4150 /* Disable CPU FDI TX PLL */
4151 reg = FDI_TX_CTL(pipe);
4152 temp = I915_READ(reg);
4153 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
4154
4155 POSTING_READ(reg);
4156 udelay(100);
4157
4158 reg = FDI_RX_CTL(pipe);
4159 temp = I915_READ(reg);
4160 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
4161
4162 /* Wait for the clocks to turn off. */
4163 POSTING_READ(reg);
4164 udelay(100);
4165}
4166
0fc932b8
JB
4167static void ironlake_fdi_disable(struct drm_crtc *crtc)
4168{
4169 struct drm_device *dev = crtc->dev;
fac5e23e 4170 struct drm_i915_private *dev_priv = to_i915(dev);
0fc932b8
JB
4171 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4172 int pipe = intel_crtc->pipe;
f0f59a00
VS
4173 i915_reg_t reg;
4174 u32 temp;
0fc932b8
JB
4175
4176 /* disable CPU FDI tx and PCH FDI rx */
4177 reg = FDI_TX_CTL(pipe);
4178 temp = I915_READ(reg);
4179 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
4180 POSTING_READ(reg);
4181
4182 reg = FDI_RX_CTL(pipe);
4183 temp = I915_READ(reg);
4184 temp &= ~(0x7 << 16);
dfd07d72 4185 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
0fc932b8
JB
4186 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
4187
4188 POSTING_READ(reg);
4189 udelay(100);
4190
4191 /* Ironlake workaround, disable clock pointer after downing FDI */
eba905b2 4192 if (HAS_PCH_IBX(dev))
6f06ce18 4193 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
0fc932b8
JB
4194
4195 /* still set train pattern 1 */
4196 reg = FDI_TX_CTL(pipe);
4197 temp = I915_READ(reg);
4198 temp &= ~FDI_LINK_TRAIN_NONE;
4199 temp |= FDI_LINK_TRAIN_PATTERN_1;
4200 I915_WRITE(reg, temp);
4201
4202 reg = FDI_RX_CTL(pipe);
4203 temp = I915_READ(reg);
4204 if (HAS_PCH_CPT(dev)) {
4205 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
4206 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
4207 } else {
4208 temp &= ~FDI_LINK_TRAIN_NONE;
4209 temp |= FDI_LINK_TRAIN_PATTERN_1;
4210 }
4211 /* BPC in FDI rx is consistent with that in PIPECONF */
4212 temp &= ~(0x07 << 16);
dfd07d72 4213 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
0fc932b8
JB
4214 I915_WRITE(reg, temp);
4215
4216 POSTING_READ(reg);
4217 udelay(100);
4218}
4219
5dce5b93
CW
4220bool intel_has_pending_fb_unpin(struct drm_device *dev)
4221{
4222 struct intel_crtc *crtc;
4223
4224 /* Note that we don't need to be called with mode_config.lock here
4225 * as our list of CRTC objects is static for the lifetime of the
4226 * device and so cannot disappear as we iterate. Similarly, we can
4227 * happily treat the predicates as racy, atomic checks as userspace
4228 * cannot claim and pin a new fb without at least acquring the
4229 * struct_mutex and so serialising with us.
4230 */
d3fcc808 4231 for_each_intel_crtc(dev, crtc) {
5dce5b93
CW
4232 if (atomic_read(&crtc->unpin_work_count) == 0)
4233 continue;
4234
5a21b665 4235 if (crtc->flip_work)
5dce5b93
CW
4236 intel_wait_for_vblank(dev, crtc->pipe);
4237
4238 return true;
4239 }
4240
4241 return false;
4242}
4243
5a21b665 4244static void page_flip_completed(struct intel_crtc *intel_crtc)
d6bbafa1
CW
4245{
4246 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
5a21b665
DV
4247 struct intel_flip_work *work = intel_crtc->flip_work;
4248
4249 intel_crtc->flip_work = NULL;
d6bbafa1
CW
4250
4251 if (work->event)
560ce1dc 4252 drm_crtc_send_vblank_event(&intel_crtc->base, work->event);
d6bbafa1
CW
4253
4254 drm_crtc_vblank_put(&intel_crtc->base);
4255
5a21b665 4256 wake_up_all(&dev_priv->pending_flip_queue);
143f73b3 4257 queue_work(dev_priv->wq, &work->unpin_work);
5a21b665
DV
4258
4259 trace_i915_flip_complete(intel_crtc->plane,
4260 work->pending_flip_obj);
d6bbafa1
CW
4261}
4262
5008e874 4263static int intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
e6c3a2a6 4264{
0f91128d 4265 struct drm_device *dev = crtc->dev;
fac5e23e 4266 struct drm_i915_private *dev_priv = to_i915(dev);
5008e874 4267 long ret;
e6c3a2a6 4268
2c10d571 4269 WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
5008e874
ML
4270
4271 ret = wait_event_interruptible_timeout(
4272 dev_priv->pending_flip_queue,
4273 !intel_crtc_has_pending_flip(crtc),
4274 60*HZ);
4275
4276 if (ret < 0)
4277 return ret;
4278
5a21b665
DV
4279 if (ret == 0) {
4280 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4281 struct intel_flip_work *work;
4282
4283 spin_lock_irq(&dev->event_lock);
4284 work = intel_crtc->flip_work;
4285 if (work && !is_mmio_work(work)) {
4286 WARN_ONCE(1, "Removing stuck page flip\n");
4287 page_flip_completed(intel_crtc);
4288 }
4289 spin_unlock_irq(&dev->event_lock);
4290 }
5bb61643 4291
5008e874 4292 return 0;
e6c3a2a6
CW
4293}
4294
b7076546 4295void lpt_disable_iclkip(struct drm_i915_private *dev_priv)
060f02d8
VS
4296{
4297 u32 temp;
4298
4299 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
4300
4301 mutex_lock(&dev_priv->sb_lock);
4302
4303 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
4304 temp |= SBI_SSCCTL_DISABLE;
4305 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
4306
4307 mutex_unlock(&dev_priv->sb_lock);
4308}
4309
e615efe4
ED
4310/* Program iCLKIP clock to the desired frequency */
4311static void lpt_program_iclkip(struct drm_crtc *crtc)
4312{
64b46a06 4313 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
6e3c9717 4314 int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
e615efe4
ED
4315 u32 divsel, phaseinc, auxdiv, phasedir = 0;
4316 u32 temp;
4317
060f02d8 4318 lpt_disable_iclkip(dev_priv);
e615efe4 4319
64b46a06
VS
4320 /* The iCLK virtual clock root frequency is in MHz,
4321 * but the adjusted_mode->crtc_clock in in KHz. To get the
4322 * divisors, it is necessary to divide one by another, so we
4323 * convert the virtual clock precision to KHz here for higher
4324 * precision.
4325 */
4326 for (auxdiv = 0; auxdiv < 2; auxdiv++) {
e615efe4
ED
4327 u32 iclk_virtual_root_freq = 172800 * 1000;
4328 u32 iclk_pi_range = 64;
64b46a06 4329 u32 desired_divisor;
e615efe4 4330
64b46a06
VS
4331 desired_divisor = DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
4332 clock << auxdiv);
4333 divsel = (desired_divisor / iclk_pi_range) - 2;
4334 phaseinc = desired_divisor % iclk_pi_range;
e615efe4 4335
64b46a06
VS
4336 /*
4337 * Near 20MHz is a corner case which is
4338 * out of range for the 7-bit divisor
4339 */
4340 if (divsel <= 0x7f)
4341 break;
e615efe4
ED
4342 }
4343
4344 /* This should not happen with any sane values */
4345 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
4346 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
4347 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
4348 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
4349
4350 DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
12d7ceed 4351 clock,
e615efe4
ED
4352 auxdiv,
4353 divsel,
4354 phasedir,
4355 phaseinc);
4356
060f02d8
VS
4357 mutex_lock(&dev_priv->sb_lock);
4358
e615efe4 4359 /* Program SSCDIVINTPHASE6 */
988d6ee8 4360 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
e615efe4
ED
4361 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
4362 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
4363 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
4364 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
4365 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
4366 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
988d6ee8 4367 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
e615efe4
ED
4368
4369 /* Program SSCAUXDIV */
988d6ee8 4370 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
e615efe4
ED
4371 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
4372 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
988d6ee8 4373 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
e615efe4
ED
4374
4375 /* Enable modulator and associated divider */
988d6ee8 4376 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
e615efe4 4377 temp &= ~SBI_SSCCTL_DISABLE;
988d6ee8 4378 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
e615efe4 4379
060f02d8
VS
4380 mutex_unlock(&dev_priv->sb_lock);
4381
e615efe4
ED
4382 /* Wait for initialization time */
4383 udelay(24);
4384
4385 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
4386}
4387
8802e5b6
VS
4388int lpt_get_iclkip(struct drm_i915_private *dev_priv)
4389{
4390 u32 divsel, phaseinc, auxdiv;
4391 u32 iclk_virtual_root_freq = 172800 * 1000;
4392 u32 iclk_pi_range = 64;
4393 u32 desired_divisor;
4394 u32 temp;
4395
4396 if ((I915_READ(PIXCLK_GATE) & PIXCLK_GATE_UNGATE) == 0)
4397 return 0;
4398
4399 mutex_lock(&dev_priv->sb_lock);
4400
4401 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
4402 if (temp & SBI_SSCCTL_DISABLE) {
4403 mutex_unlock(&dev_priv->sb_lock);
4404 return 0;
4405 }
4406
4407 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
4408 divsel = (temp & SBI_SSCDIVINTPHASE_DIVSEL_MASK) >>
4409 SBI_SSCDIVINTPHASE_DIVSEL_SHIFT;
4410 phaseinc = (temp & SBI_SSCDIVINTPHASE_INCVAL_MASK) >>
4411 SBI_SSCDIVINTPHASE_INCVAL_SHIFT;
4412
4413 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
4414 auxdiv = (temp & SBI_SSCAUXDIV_FINALDIV2SEL_MASK) >>
4415 SBI_SSCAUXDIV_FINALDIV2SEL_SHIFT;
4416
4417 mutex_unlock(&dev_priv->sb_lock);
4418
4419 desired_divisor = (divsel + 2) * iclk_pi_range + phaseinc;
4420
4421 return DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
4422 desired_divisor << auxdiv);
4423}
4424
275f01b2
DV
4425static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
4426 enum pipe pch_transcoder)
4427{
4428 struct drm_device *dev = crtc->base.dev;
fac5e23e 4429 struct drm_i915_private *dev_priv = to_i915(dev);
6e3c9717 4430 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
275f01b2
DV
4431
4432 I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
4433 I915_READ(HTOTAL(cpu_transcoder)));
4434 I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
4435 I915_READ(HBLANK(cpu_transcoder)));
4436 I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
4437 I915_READ(HSYNC(cpu_transcoder)));
4438
4439 I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
4440 I915_READ(VTOTAL(cpu_transcoder)));
4441 I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
4442 I915_READ(VBLANK(cpu_transcoder)));
4443 I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
4444 I915_READ(VSYNC(cpu_transcoder)));
4445 I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
4446 I915_READ(VSYNCSHIFT(cpu_transcoder)));
4447}
4448
003632d9 4449static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
1fbc0d78 4450{
fac5e23e 4451 struct drm_i915_private *dev_priv = to_i915(dev);
1fbc0d78
DV
4452 uint32_t temp;
4453
4454 temp = I915_READ(SOUTH_CHICKEN1);
003632d9 4455 if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
1fbc0d78
DV
4456 return;
4457
4458 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
4459 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
4460
003632d9
ACO
4461 temp &= ~FDI_BC_BIFURCATION_SELECT;
4462 if (enable)
4463 temp |= FDI_BC_BIFURCATION_SELECT;
4464
4465 DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
1fbc0d78
DV
4466 I915_WRITE(SOUTH_CHICKEN1, temp);
4467 POSTING_READ(SOUTH_CHICKEN1);
4468}
4469
4470static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
4471{
4472 struct drm_device *dev = intel_crtc->base.dev;
1fbc0d78
DV
4473
4474 switch (intel_crtc->pipe) {
4475 case PIPE_A:
4476 break;
4477 case PIPE_B:
6e3c9717 4478 if (intel_crtc->config->fdi_lanes > 2)
003632d9 4479 cpt_set_fdi_bc_bifurcation(dev, false);
1fbc0d78 4480 else
003632d9 4481 cpt_set_fdi_bc_bifurcation(dev, true);
1fbc0d78
DV
4482
4483 break;
4484 case PIPE_C:
003632d9 4485 cpt_set_fdi_bc_bifurcation(dev, true);
1fbc0d78
DV
4486
4487 break;
4488 default:
4489 BUG();
4490 }
4491}
4492
c48b5305
VS
4493/* Return which DP Port should be selected for Transcoder DP control */
4494static enum port
4495intel_trans_dp_port_sel(struct drm_crtc *crtc)
4496{
4497 struct drm_device *dev = crtc->dev;
4498 struct intel_encoder *encoder;
4499
4500 for_each_encoder_on_crtc(dev, crtc, encoder) {
cca0502b 4501 if (encoder->type == INTEL_OUTPUT_DP ||
c48b5305
VS
4502 encoder->type == INTEL_OUTPUT_EDP)
4503 return enc_to_dig_port(&encoder->base)->port;
4504 }
4505
4506 return -1;
4507}
4508
f67a559d
JB
4509/*
4510 * Enable PCH resources required for PCH ports:
4511 * - PCH PLLs
4512 * - FDI training & RX/TX
4513 * - update transcoder timings
4514 * - DP transcoding bits
4515 * - transcoder
4516 */
4517static void ironlake_pch_enable(struct drm_crtc *crtc)
0e23b99d
JB
4518{
4519 struct drm_device *dev = crtc->dev;
fac5e23e 4520 struct drm_i915_private *dev_priv = to_i915(dev);
0e23b99d
JB
4521 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4522 int pipe = intel_crtc->pipe;
f0f59a00 4523 u32 temp;
2c07245f 4524
ab9412ba 4525 assert_pch_transcoder_disabled(dev_priv, pipe);
e7e164db 4526
1fbc0d78
DV
4527 if (IS_IVYBRIDGE(dev))
4528 ivybridge_update_fdi_bc_bifurcation(intel_crtc);
4529
cd986abb
DV
4530 /* Write the TU size bits before fdi link training, so that error
4531 * detection works. */
4532 I915_WRITE(FDI_RX_TUSIZE1(pipe),
4533 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
4534
c98e9dcf 4535 /* For PCH output, training FDI link */
674cf967 4536 dev_priv->display.fdi_link_train(crtc);
2c07245f 4537
3ad8a208
DV
4538 /* We need to program the right clock selection before writing the pixel
4539 * mutliplier into the DPLL. */
303b81e0 4540 if (HAS_PCH_CPT(dev)) {
ee7b9f93 4541 u32 sel;
4b645f14 4542
c98e9dcf 4543 temp = I915_READ(PCH_DPLL_SEL);
11887397
DV
4544 temp |= TRANS_DPLL_ENABLE(pipe);
4545 sel = TRANS_DPLLB_SEL(pipe);
8106ddbd
ACO
4546 if (intel_crtc->config->shared_dpll ==
4547 intel_get_shared_dpll_by_id(dev_priv, DPLL_ID_PCH_PLL_B))
ee7b9f93
JB
4548 temp |= sel;
4549 else
4550 temp &= ~sel;
c98e9dcf 4551 I915_WRITE(PCH_DPLL_SEL, temp);
c98e9dcf 4552 }
5eddb70b 4553
3ad8a208
DV
4554 /* XXX: pch pll's can be enabled any time before we enable the PCH
4555 * transcoder, and we actually should do this to not upset any PCH
4556 * transcoder that already use the clock when we share it.
4557 *
4558 * Note that enable_shared_dpll tries to do the right thing, but
4559 * get_shared_dpll unconditionally resets the pll - we need that to have
4560 * the right LVDS enable sequence. */
85b3894f 4561 intel_enable_shared_dpll(intel_crtc);
3ad8a208 4562
d9b6cb56
JB
4563 /* set transcoder timing, panel must allow it */
4564 assert_panel_unlocked(dev_priv, pipe);
275f01b2 4565 ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
8db9d77b 4566
303b81e0 4567 intel_fdi_normal_train(crtc);
5e84e1a4 4568
c98e9dcf 4569 /* For PCH DP, enable TRANS_DP_CTL */
37a5650b 4570 if (HAS_PCH_CPT(dev) && intel_crtc_has_dp_encoder(intel_crtc->config)) {
9c4edaee
VS
4571 const struct drm_display_mode *adjusted_mode =
4572 &intel_crtc->config->base.adjusted_mode;
dfd07d72 4573 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
f0f59a00 4574 i915_reg_t reg = TRANS_DP_CTL(pipe);
5eddb70b
CW
4575 temp = I915_READ(reg);
4576 temp &= ~(TRANS_DP_PORT_SEL_MASK |
220cad3c
EA
4577 TRANS_DP_SYNC_MASK |
4578 TRANS_DP_BPC_MASK);
e3ef4479 4579 temp |= TRANS_DP_OUTPUT_ENABLE;
9325c9f0 4580 temp |= bpc << 9; /* same format but at 11:9 */
c98e9dcf 4581
9c4edaee 4582 if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
5eddb70b 4583 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
9c4edaee 4584 if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
5eddb70b 4585 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
c98e9dcf
JB
4586
4587 switch (intel_trans_dp_port_sel(crtc)) {
c48b5305 4588 case PORT_B:
5eddb70b 4589 temp |= TRANS_DP_PORT_SEL_B;
c98e9dcf 4590 break;
c48b5305 4591 case PORT_C:
5eddb70b 4592 temp |= TRANS_DP_PORT_SEL_C;
c98e9dcf 4593 break;
c48b5305 4594 case PORT_D:
5eddb70b 4595 temp |= TRANS_DP_PORT_SEL_D;
c98e9dcf
JB
4596 break;
4597 default:
e95d41e1 4598 BUG();
32f9d658 4599 }
2c07245f 4600
5eddb70b 4601 I915_WRITE(reg, temp);
6be4a607 4602 }
b52eb4dc 4603
b8a4f404 4604 ironlake_enable_pch_transcoder(dev_priv, pipe);
f67a559d
JB
4605}
4606
1507e5bd
PZ
4607static void lpt_pch_enable(struct drm_crtc *crtc)
4608{
4609 struct drm_device *dev = crtc->dev;
fac5e23e 4610 struct drm_i915_private *dev_priv = to_i915(dev);
1507e5bd 4611 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6e3c9717 4612 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
1507e5bd 4613
ab9412ba 4614 assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
1507e5bd 4615
8c52b5e8 4616 lpt_program_iclkip(crtc);
1507e5bd 4617
0540e488 4618 /* Set transcoder timing. */
275f01b2 4619 ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
1507e5bd 4620
937bb610 4621 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
f67a559d
JB
4622}
4623
a1520318 4624static void cpt_verify_modeset(struct drm_device *dev, int pipe)
d4270e57 4625{
fac5e23e 4626 struct drm_i915_private *dev_priv = to_i915(dev);
f0f59a00 4627 i915_reg_t dslreg = PIPEDSL(pipe);
d4270e57
JB
4628 u32 temp;
4629
4630 temp = I915_READ(dslreg);
4631 udelay(500);
4632 if (wait_for(I915_READ(dslreg) != temp, 5)) {
d4270e57 4633 if (wait_for(I915_READ(dslreg) != temp, 5))
84f44ce7 4634 DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
d4270e57
JB
4635 }
4636}
4637
86adf9d7
ML
4638static int
4639skl_update_scaler(struct intel_crtc_state *crtc_state, bool force_detach,
4640 unsigned scaler_user, int *scaler_id, unsigned int rotation,
4641 int src_w, int src_h, int dst_w, int dst_h)
a1b2278e 4642{
86adf9d7
ML
4643 struct intel_crtc_scaler_state *scaler_state =
4644 &crtc_state->scaler_state;
4645 struct intel_crtc *intel_crtc =
4646 to_intel_crtc(crtc_state->base.crtc);
a1b2278e 4647 int need_scaling;
6156a456
CK
4648
4649 need_scaling = intel_rotation_90_or_270(rotation) ?
4650 (src_h != dst_w || src_w != dst_h):
4651 (src_w != dst_w || src_h != dst_h);
a1b2278e
CK
4652
4653 /*
4654 * if plane is being disabled or scaler is no more required or force detach
4655 * - free scaler binded to this plane/crtc
4656 * - in order to do this, update crtc->scaler_usage
4657 *
4658 * Here scaler state in crtc_state is set free so that
4659 * scaler can be assigned to other user. Actual register
4660 * update to free the scaler is done in plane/panel-fit programming.
4661 * For this purpose crtc/plane_state->scaler_id isn't reset here.
4662 */
86adf9d7 4663 if (force_detach || !need_scaling) {
a1b2278e 4664 if (*scaler_id >= 0) {
86adf9d7 4665 scaler_state->scaler_users &= ~(1 << scaler_user);
a1b2278e
CK
4666 scaler_state->scalers[*scaler_id].in_use = 0;
4667
86adf9d7
ML
4668 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4669 "Staged freeing scaler id %d scaler_users = 0x%x\n",
4670 intel_crtc->pipe, scaler_user, *scaler_id,
a1b2278e
CK
4671 scaler_state->scaler_users);
4672 *scaler_id = -1;
4673 }
4674 return 0;
4675 }
4676
4677 /* range checks */
4678 if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
4679 dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
4680
4681 src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
4682 dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
86adf9d7 4683 DRM_DEBUG_KMS("scaler_user index %u.%u: src %ux%u dst %ux%u "
a1b2278e 4684 "size is out of scaler range\n",
86adf9d7 4685 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h);
a1b2278e
CK
4686 return -EINVAL;
4687 }
4688
86adf9d7
ML
4689 /* mark this plane as a scaler user in crtc_state */
4690 scaler_state->scaler_users |= (1 << scaler_user);
4691 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4692 "staged scaling request for %ux%u->%ux%u scaler_users = 0x%x\n",
4693 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h,
4694 scaler_state->scaler_users);
4695
4696 return 0;
4697}
4698
4699/**
4700 * skl_update_scaler_crtc - Stages update to scaler state for a given crtc.
4701 *
4702 * @state: crtc's scaler state
86adf9d7
ML
4703 *
4704 * Return
4705 * 0 - scaler_usage updated successfully
4706 * error - requested scaling cannot be supported or other error condition
4707 */
e435d6e5 4708int skl_update_scaler_crtc(struct intel_crtc_state *state)
86adf9d7
ML
4709{
4710 struct intel_crtc *intel_crtc = to_intel_crtc(state->base.crtc);
7c5f93b0 4711 const struct drm_display_mode *adjusted_mode = &state->base.adjusted_mode;
86adf9d7 4712
78108b7c
VS
4713 DRM_DEBUG_KMS("Updating scaler for [CRTC:%d:%s] scaler_user index %u.%u\n",
4714 intel_crtc->base.base.id, intel_crtc->base.name,
4715 intel_crtc->pipe, SKL_CRTC_INDEX);
86adf9d7 4716
e435d6e5 4717 return skl_update_scaler(state, !state->base.active, SKL_CRTC_INDEX,
31ad61e4 4718 &state->scaler_state.scaler_id, DRM_ROTATE_0,
86adf9d7 4719 state->pipe_src_w, state->pipe_src_h,
aad941d5 4720 adjusted_mode->crtc_hdisplay, adjusted_mode->crtc_vdisplay);
86adf9d7
ML
4721}
4722
4723/**
4724 * skl_update_scaler_plane - Stages update to scaler state for a given plane.
4725 *
4726 * @state: crtc's scaler state
86adf9d7
ML
4727 * @plane_state: atomic plane state to update
4728 *
4729 * Return
4730 * 0 - scaler_usage updated successfully
4731 * error - requested scaling cannot be supported or other error condition
4732 */
da20eabd
ML
4733static int skl_update_scaler_plane(struct intel_crtc_state *crtc_state,
4734 struct intel_plane_state *plane_state)
86adf9d7
ML
4735{
4736
4737 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
da20eabd
ML
4738 struct intel_plane *intel_plane =
4739 to_intel_plane(plane_state->base.plane);
86adf9d7
ML
4740 struct drm_framebuffer *fb = plane_state->base.fb;
4741 int ret;
4742
936e71e3 4743 bool force_detach = !fb || !plane_state->base.visible;
86adf9d7 4744
72660ce0
VS
4745 DRM_DEBUG_KMS("Updating scaler for [PLANE:%d:%s] scaler_user index %u.%u\n",
4746 intel_plane->base.base.id, intel_plane->base.name,
4747 intel_crtc->pipe, drm_plane_index(&intel_plane->base));
86adf9d7
ML
4748
4749 ret = skl_update_scaler(crtc_state, force_detach,
4750 drm_plane_index(&intel_plane->base),
4751 &plane_state->scaler_id,
4752 plane_state->base.rotation,
936e71e3
VS
4753 drm_rect_width(&plane_state->base.src) >> 16,
4754 drm_rect_height(&plane_state->base.src) >> 16,
4755 drm_rect_width(&plane_state->base.dst),
4756 drm_rect_height(&plane_state->base.dst));
86adf9d7
ML
4757
4758 if (ret || plane_state->scaler_id < 0)
4759 return ret;
4760
a1b2278e 4761 /* check colorkey */
818ed961 4762 if (plane_state->ckey.flags != I915_SET_COLORKEY_NONE) {
72660ce0
VS
4763 DRM_DEBUG_KMS("[PLANE:%d:%s] scaling with color key not allowed",
4764 intel_plane->base.base.id,
4765 intel_plane->base.name);
a1b2278e
CK
4766 return -EINVAL;
4767 }
4768
4769 /* Check src format */
86adf9d7
ML
4770 switch (fb->pixel_format) {
4771 case DRM_FORMAT_RGB565:
4772 case DRM_FORMAT_XBGR8888:
4773 case DRM_FORMAT_XRGB8888:
4774 case DRM_FORMAT_ABGR8888:
4775 case DRM_FORMAT_ARGB8888:
4776 case DRM_FORMAT_XRGB2101010:
4777 case DRM_FORMAT_XBGR2101010:
4778 case DRM_FORMAT_YUYV:
4779 case DRM_FORMAT_YVYU:
4780 case DRM_FORMAT_UYVY:
4781 case DRM_FORMAT_VYUY:
4782 break;
4783 default:
72660ce0
VS
4784 DRM_DEBUG_KMS("[PLANE:%d:%s] FB:%d unsupported scaling format 0x%x\n",
4785 intel_plane->base.base.id, intel_plane->base.name,
4786 fb->base.id, fb->pixel_format);
86adf9d7 4787 return -EINVAL;
a1b2278e
CK
4788 }
4789
a1b2278e
CK
4790 return 0;
4791}
4792
e435d6e5
ML
4793static void skylake_scaler_disable(struct intel_crtc *crtc)
4794{
4795 int i;
4796
4797 for (i = 0; i < crtc->num_scalers; i++)
4798 skl_detach_scaler(crtc, i);
4799}
4800
4801static void skylake_pfit_enable(struct intel_crtc *crtc)
bd2e244f
JB
4802{
4803 struct drm_device *dev = crtc->base.dev;
fac5e23e 4804 struct drm_i915_private *dev_priv = to_i915(dev);
bd2e244f 4805 int pipe = crtc->pipe;
a1b2278e
CK
4806 struct intel_crtc_scaler_state *scaler_state =
4807 &crtc->config->scaler_state;
4808
4809 DRM_DEBUG_KMS("for crtc_state = %p\n", crtc->config);
4810
6e3c9717 4811 if (crtc->config->pch_pfit.enabled) {
a1b2278e
CK
4812 int id;
4813
4814 if (WARN_ON(crtc->config->scaler_state.scaler_id < 0)) {
4815 DRM_ERROR("Requesting pfit without getting a scaler first\n");
4816 return;
4817 }
4818
4819 id = scaler_state->scaler_id;
4820 I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
4821 PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
4822 I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
4823 I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
4824
4825 DRM_DEBUG_KMS("for crtc_state = %p scaler_id = %d\n", crtc->config, id);
bd2e244f
JB
4826 }
4827}
4828
b074cec8
JB
4829static void ironlake_pfit_enable(struct intel_crtc *crtc)
4830{
4831 struct drm_device *dev = crtc->base.dev;
fac5e23e 4832 struct drm_i915_private *dev_priv = to_i915(dev);
b074cec8
JB
4833 int pipe = crtc->pipe;
4834
6e3c9717 4835 if (crtc->config->pch_pfit.enabled) {
b074cec8
JB
4836 /* Force use of hard-coded filter coefficients
4837 * as some pre-programmed values are broken,
4838 * e.g. x201.
4839 */
4840 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
4841 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
4842 PF_PIPE_SEL_IVB(pipe));
4843 else
4844 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
6e3c9717
ACO
4845 I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
4846 I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
d4270e57
JB
4847 }
4848}
4849
20bc8673 4850void hsw_enable_ips(struct intel_crtc *crtc)
d77e4531 4851{
cea165c3 4852 struct drm_device *dev = crtc->base.dev;
fac5e23e 4853 struct drm_i915_private *dev_priv = to_i915(dev);
d77e4531 4854
6e3c9717 4855 if (!crtc->config->ips_enabled)
d77e4531
PZ
4856 return;
4857
307e4498
ML
4858 /*
4859 * We can only enable IPS after we enable a plane and wait for a vblank
4860 * This function is called from post_plane_update, which is run after
4861 * a vblank wait.
4862 */
cea165c3 4863
d77e4531 4864 assert_plane_enabled(dev_priv, crtc->plane);
cea165c3 4865 if (IS_BROADWELL(dev)) {
2a114cc1
BW
4866 mutex_lock(&dev_priv->rps.hw_lock);
4867 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
4868 mutex_unlock(&dev_priv->rps.hw_lock);
4869 /* Quoting Art Runyan: "its not safe to expect any particular
4870 * value in IPS_CTL bit 31 after enabling IPS through the
e59150dc
JB
4871 * mailbox." Moreover, the mailbox may return a bogus state,
4872 * so we need to just enable it and continue on.
2a114cc1
BW
4873 */
4874 } else {
4875 I915_WRITE(IPS_CTL, IPS_ENABLE);
4876 /* The bit only becomes 1 in the next vblank, so this wait here
4877 * is essentially intel_wait_for_vblank. If we don't have this
4878 * and don't wait for vblanks until the end of crtc_enable, then
4879 * the HW state readout code will complain that the expected
4880 * IPS_CTL value is not the one we read. */
2ec9ba3c
CW
4881 if (intel_wait_for_register(dev_priv,
4882 IPS_CTL, IPS_ENABLE, IPS_ENABLE,
4883 50))
2a114cc1
BW
4884 DRM_ERROR("Timed out waiting for IPS enable\n");
4885 }
d77e4531
PZ
4886}
4887
20bc8673 4888void hsw_disable_ips(struct intel_crtc *crtc)
d77e4531
PZ
4889{
4890 struct drm_device *dev = crtc->base.dev;
fac5e23e 4891 struct drm_i915_private *dev_priv = to_i915(dev);
d77e4531 4892
6e3c9717 4893 if (!crtc->config->ips_enabled)
d77e4531
PZ
4894 return;
4895
4896 assert_plane_enabled(dev_priv, crtc->plane);
23d0b130 4897 if (IS_BROADWELL(dev)) {
2a114cc1
BW
4898 mutex_lock(&dev_priv->rps.hw_lock);
4899 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
4900 mutex_unlock(&dev_priv->rps.hw_lock);
23d0b130 4901 /* wait for pcode to finish disabling IPS, which may take up to 42ms */
b85c1ecf
CW
4902 if (intel_wait_for_register(dev_priv,
4903 IPS_CTL, IPS_ENABLE, 0,
4904 42))
23d0b130 4905 DRM_ERROR("Timed out waiting for IPS disable\n");
e59150dc 4906 } else {
2a114cc1 4907 I915_WRITE(IPS_CTL, 0);
e59150dc
JB
4908 POSTING_READ(IPS_CTL);
4909 }
d77e4531
PZ
4910
4911 /* We need to wait for a vblank before we can disable the plane. */
4912 intel_wait_for_vblank(dev, crtc->pipe);
4913}
4914
7cac945f 4915static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
d3eedb1a 4916{
7cac945f 4917 if (intel_crtc->overlay) {
d3eedb1a 4918 struct drm_device *dev = intel_crtc->base.dev;
fac5e23e 4919 struct drm_i915_private *dev_priv = to_i915(dev);
d3eedb1a
VS
4920
4921 mutex_lock(&dev->struct_mutex);
4922 dev_priv->mm.interruptible = false;
4923 (void) intel_overlay_switch_off(intel_crtc->overlay);
4924 dev_priv->mm.interruptible = true;
4925 mutex_unlock(&dev->struct_mutex);
4926 }
4927
4928 /* Let userspace switch the overlay on again. In most cases userspace
4929 * has to recompute where to put it anyway.
4930 */
4931}
4932
87d4300a
ML
4933/**
4934 * intel_post_enable_primary - Perform operations after enabling primary plane
4935 * @crtc: the CRTC whose primary plane was just enabled
4936 *
4937 * Performs potentially sleeping operations that must be done after the primary
4938 * plane is enabled, such as updating FBC and IPS. Note that this may be
4939 * called due to an explicit primary plane update, or due to an implicit
4940 * re-enable that is caused when a sprite plane is updated to no longer
4941 * completely hide the primary plane.
4942 */
4943static void
4944intel_post_enable_primary(struct drm_crtc *crtc)
a5c4d7bc
VS
4945{
4946 struct drm_device *dev = crtc->dev;
fac5e23e 4947 struct drm_i915_private *dev_priv = to_i915(dev);
a5c4d7bc
VS
4948 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4949 int pipe = intel_crtc->pipe;
a5c4d7bc 4950
87d4300a
ML
4951 /*
4952 * FIXME IPS should be fine as long as one plane is
4953 * enabled, but in practice it seems to have problems
4954 * when going from primary only to sprite only and vice
4955 * versa.
4956 */
a5c4d7bc
VS
4957 hsw_enable_ips(intel_crtc);
4958
f99d7069 4959 /*
87d4300a
ML
4960 * Gen2 reports pipe underruns whenever all planes are disabled.
4961 * So don't enable underrun reporting before at least some planes
4962 * are enabled.
4963 * FIXME: Need to fix the logic to work when we turn off all planes
4964 * but leave the pipe running.
f99d7069 4965 */
87d4300a
ML
4966 if (IS_GEN2(dev))
4967 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4968
aca7b684
VS
4969 /* Underruns don't always raise interrupts, so check manually. */
4970 intel_check_cpu_fifo_underruns(dev_priv);
4971 intel_check_pch_fifo_underruns(dev_priv);
a5c4d7bc
VS
4972}
4973
2622a081 4974/* FIXME move all this to pre_plane_update() with proper state tracking */
87d4300a
ML
4975static void
4976intel_pre_disable_primary(struct drm_crtc *crtc)
a5c4d7bc
VS
4977{
4978 struct drm_device *dev = crtc->dev;
fac5e23e 4979 struct drm_i915_private *dev_priv = to_i915(dev);
a5c4d7bc
VS
4980 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4981 int pipe = intel_crtc->pipe;
a5c4d7bc 4982
87d4300a
ML
4983 /*
4984 * Gen2 reports pipe underruns whenever all planes are disabled.
4985 * So diasble underrun reporting before all the planes get disabled.
4986 * FIXME: Need to fix the logic to work when we turn off all planes
4987 * but leave the pipe running.
4988 */
4989 if (IS_GEN2(dev))
4990 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
a5c4d7bc 4991
2622a081
VS
4992 /*
4993 * FIXME IPS should be fine as long as one plane is
4994 * enabled, but in practice it seems to have problems
4995 * when going from primary only to sprite only and vice
4996 * versa.
4997 */
4998 hsw_disable_ips(intel_crtc);
4999}
5000
5001/* FIXME get rid of this and use pre_plane_update */
5002static void
5003intel_pre_disable_primary_noatomic(struct drm_crtc *crtc)
5004{
5005 struct drm_device *dev = crtc->dev;
fac5e23e 5006 struct drm_i915_private *dev_priv = to_i915(dev);
2622a081
VS
5007 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5008 int pipe = intel_crtc->pipe;
5009
5010 intel_pre_disable_primary(crtc);
5011
87d4300a
ML
5012 /*
5013 * Vblank time updates from the shadow to live plane control register
5014 * are blocked if the memory self-refresh mode is active at that
5015 * moment. So to make sure the plane gets truly disabled, disable
5016 * first the self-refresh mode. The self-refresh enable bit in turn
5017 * will be checked/applied by the HW only at the next frame start
5018 * event which is after the vblank start event, so we need to have a
5019 * wait-for-vblank between disabling the plane and the pipe.
5020 */
262cd2e1 5021 if (HAS_GMCH_DISPLAY(dev)) {
87d4300a 5022 intel_set_memory_cxsr(dev_priv, false);
262cd2e1
VS
5023 dev_priv->wm.vlv.cxsr = false;
5024 intel_wait_for_vblank(dev, pipe);
5025 }
87d4300a
ML
5026}
5027
5a21b665
DV
5028static void intel_post_plane_update(struct intel_crtc_state *old_crtc_state)
5029{
5030 struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
5031 struct drm_atomic_state *old_state = old_crtc_state->base.state;
5032 struct intel_crtc_state *pipe_config =
5033 to_intel_crtc_state(crtc->base.state);
5a21b665
DV
5034 struct drm_plane *primary = crtc->base.primary;
5035 struct drm_plane_state *old_pri_state =
5036 drm_atomic_get_existing_plane_state(old_state, primary);
5037
5748b6a1 5038 intel_frontbuffer_flip(to_i915(crtc->base.dev), pipe_config->fb_bits);
5a21b665
DV
5039
5040 crtc->wm.cxsr_allowed = true;
5041
5042 if (pipe_config->update_wm_post && pipe_config->base.active)
5043 intel_update_watermarks(&crtc->base);
5044
5045 if (old_pri_state) {
5046 struct intel_plane_state *primary_state =
5047 to_intel_plane_state(primary->state);
5048 struct intel_plane_state *old_primary_state =
5049 to_intel_plane_state(old_pri_state);
5050
5051 intel_fbc_post_update(crtc);
5052
936e71e3 5053 if (primary_state->base.visible &&
5a21b665 5054 (needs_modeset(&pipe_config->base) ||
936e71e3 5055 !old_primary_state->base.visible))
5a21b665
DV
5056 intel_post_enable_primary(&crtc->base);
5057 }
5058}
5059
5c74cd73 5060static void intel_pre_plane_update(struct intel_crtc_state *old_crtc_state)
ac21b225 5061{
5c74cd73 5062 struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
ac21b225 5063 struct drm_device *dev = crtc->base.dev;
fac5e23e 5064 struct drm_i915_private *dev_priv = to_i915(dev);
ab1d3a0e
ML
5065 struct intel_crtc_state *pipe_config =
5066 to_intel_crtc_state(crtc->base.state);
5c74cd73
ML
5067 struct drm_atomic_state *old_state = old_crtc_state->base.state;
5068 struct drm_plane *primary = crtc->base.primary;
5069 struct drm_plane_state *old_pri_state =
5070 drm_atomic_get_existing_plane_state(old_state, primary);
5071 bool modeset = needs_modeset(&pipe_config->base);
ac21b225 5072
5c74cd73
ML
5073 if (old_pri_state) {
5074 struct intel_plane_state *primary_state =
5075 to_intel_plane_state(primary->state);
5076 struct intel_plane_state *old_primary_state =
5077 to_intel_plane_state(old_pri_state);
5078
faf68d92 5079 intel_fbc_pre_update(crtc, pipe_config, primary_state);
31ae71fc 5080
936e71e3
VS
5081 if (old_primary_state->base.visible &&
5082 (modeset || !primary_state->base.visible))
5c74cd73
ML
5083 intel_pre_disable_primary(&crtc->base);
5084 }
852eb00d 5085
a4015f9a 5086 if (pipe_config->disable_cxsr && HAS_GMCH_DISPLAY(dev)) {
852eb00d 5087 crtc->wm.cxsr_allowed = false;
2dfd178d 5088
2622a081
VS
5089 /*
5090 * Vblank time updates from the shadow to live plane control register
5091 * are blocked if the memory self-refresh mode is active at that
5092 * moment. So to make sure the plane gets truly disabled, disable
5093 * first the self-refresh mode. The self-refresh enable bit in turn
5094 * will be checked/applied by the HW only at the next frame start
5095 * event which is after the vblank start event, so we need to have a
5096 * wait-for-vblank between disabling the plane and the pipe.
5097 */
5098 if (old_crtc_state->base.active) {
2dfd178d 5099 intel_set_memory_cxsr(dev_priv, false);
2622a081
VS
5100 dev_priv->wm.vlv.cxsr = false;
5101 intel_wait_for_vblank(dev, crtc->pipe);
5102 }
852eb00d 5103 }
92826fcd 5104
ed4a6a7c
MR
5105 /*
5106 * IVB workaround: must disable low power watermarks for at least
5107 * one frame before enabling scaling. LP watermarks can be re-enabled
5108 * when scaling is disabled.
5109 *
5110 * WaCxSRDisabledForSpriteScaling:ivb
5111 */
5112 if (pipe_config->disable_lp_wm) {
5113 ilk_disable_lp_wm(dev);
5114 intel_wait_for_vblank(dev, crtc->pipe);
5115 }
5116
5117 /*
5118 * If we're doing a modeset, we're done. No need to do any pre-vblank
5119 * watermark programming here.
5120 */
5121 if (needs_modeset(&pipe_config->base))
5122 return;
5123
5124 /*
5125 * For platforms that support atomic watermarks, program the
5126 * 'intermediate' watermarks immediately. On pre-gen9 platforms, these
5127 * will be the intermediate values that are safe for both pre- and
5128 * post- vblank; when vblank happens, the 'active' values will be set
5129 * to the final 'target' values and we'll do this again to get the
5130 * optimal watermarks. For gen9+ platforms, the values we program here
5131 * will be the final target values which will get automatically latched
5132 * at vblank time; no further programming will be necessary.
5133 *
5134 * If a platform hasn't been transitioned to atomic watermarks yet,
5135 * we'll continue to update watermarks the old way, if flags tell
5136 * us to.
5137 */
5138 if (dev_priv->display.initial_watermarks != NULL)
5139 dev_priv->display.initial_watermarks(pipe_config);
caed361d 5140 else if (pipe_config->update_wm_pre)
92826fcd 5141 intel_update_watermarks(&crtc->base);
ac21b225
ML
5142}
5143
d032ffa0 5144static void intel_crtc_disable_planes(struct drm_crtc *crtc, unsigned plane_mask)
87d4300a
ML
5145{
5146 struct drm_device *dev = crtc->dev;
5147 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
d032ffa0 5148 struct drm_plane *p;
87d4300a
ML
5149 int pipe = intel_crtc->pipe;
5150
7cac945f 5151 intel_crtc_dpms_overlay_disable(intel_crtc);
27321ae8 5152
d032ffa0
ML
5153 drm_for_each_plane_mask(p, dev, plane_mask)
5154 to_intel_plane(p)->disable_plane(p, crtc);
f98551ae 5155
f99d7069
DV
5156 /*
5157 * FIXME: Once we grow proper nuclear flip support out of this we need
5158 * to compute the mask of flip planes precisely. For the time being
5159 * consider this a flip to a NULL plane.
5160 */
5748b6a1 5161 intel_frontbuffer_flip(to_i915(dev), INTEL_FRONTBUFFER_ALL_MASK(pipe));
a5c4d7bc
VS
5162}
5163
fb1c98b1 5164static void intel_encoders_pre_pll_enable(struct drm_crtc *crtc,
fd6bbda9 5165 struct intel_crtc_state *crtc_state,
fb1c98b1
ML
5166 struct drm_atomic_state *old_state)
5167{
5168 struct drm_connector_state *old_conn_state;
5169 struct drm_connector *conn;
5170 int i;
5171
5172 for_each_connector_in_state(old_state, conn, old_conn_state, i) {
5173 struct drm_connector_state *conn_state = conn->state;
5174 struct intel_encoder *encoder =
5175 to_intel_encoder(conn_state->best_encoder);
5176
5177 if (conn_state->crtc != crtc)
5178 continue;
5179
5180 if (encoder->pre_pll_enable)
fd6bbda9 5181 encoder->pre_pll_enable(encoder, crtc_state, conn_state);
fb1c98b1
ML
5182 }
5183}
5184
5185static void intel_encoders_pre_enable(struct drm_crtc *crtc,
fd6bbda9 5186 struct intel_crtc_state *crtc_state,
fb1c98b1
ML
5187 struct drm_atomic_state *old_state)
5188{
5189 struct drm_connector_state *old_conn_state;
5190 struct drm_connector *conn;
5191 int i;
5192
5193 for_each_connector_in_state(old_state, conn, old_conn_state, i) {
5194 struct drm_connector_state *conn_state = conn->state;
5195 struct intel_encoder *encoder =
5196 to_intel_encoder(conn_state->best_encoder);
5197
5198 if (conn_state->crtc != crtc)
5199 continue;
5200
5201 if (encoder->pre_enable)
fd6bbda9 5202 encoder->pre_enable(encoder, crtc_state, conn_state);
fb1c98b1
ML
5203 }
5204}
5205
5206static void intel_encoders_enable(struct drm_crtc *crtc,
fd6bbda9 5207 struct intel_crtc_state *crtc_state,
fb1c98b1
ML
5208 struct drm_atomic_state *old_state)
5209{
5210 struct drm_connector_state *old_conn_state;
5211 struct drm_connector *conn;
5212 int i;
5213
5214 for_each_connector_in_state(old_state, conn, old_conn_state, i) {
5215 struct drm_connector_state *conn_state = conn->state;
5216 struct intel_encoder *encoder =
5217 to_intel_encoder(conn_state->best_encoder);
5218
5219 if (conn_state->crtc != crtc)
5220 continue;
5221
fd6bbda9 5222 encoder->enable(encoder, crtc_state, conn_state);
fb1c98b1
ML
5223 intel_opregion_notify_encoder(encoder, true);
5224 }
5225}
5226
5227static void intel_encoders_disable(struct drm_crtc *crtc,
fd6bbda9 5228 struct intel_crtc_state *old_crtc_state,
fb1c98b1
ML
5229 struct drm_atomic_state *old_state)
5230{
5231 struct drm_connector_state *old_conn_state;
5232 struct drm_connector *conn;
5233 int i;
5234
5235 for_each_connector_in_state(old_state, conn, old_conn_state, i) {
5236 struct intel_encoder *encoder =
5237 to_intel_encoder(old_conn_state->best_encoder);
5238
5239 if (old_conn_state->crtc != crtc)
5240 continue;
5241
5242 intel_opregion_notify_encoder(encoder, false);
fd6bbda9 5243 encoder->disable(encoder, old_crtc_state, old_conn_state);
fb1c98b1
ML
5244 }
5245}
5246
5247static void intel_encoders_post_disable(struct drm_crtc *crtc,
fd6bbda9 5248 struct intel_crtc_state *old_crtc_state,
fb1c98b1
ML
5249 struct drm_atomic_state *old_state)
5250{
5251 struct drm_connector_state *old_conn_state;
5252 struct drm_connector *conn;
5253 int i;
5254
5255 for_each_connector_in_state(old_state, conn, old_conn_state, i) {
5256 struct intel_encoder *encoder =
5257 to_intel_encoder(old_conn_state->best_encoder);
5258
5259 if (old_conn_state->crtc != crtc)
5260 continue;
5261
5262 if (encoder->post_disable)
fd6bbda9 5263 encoder->post_disable(encoder, old_crtc_state, old_conn_state);
fb1c98b1
ML
5264 }
5265}
5266
5267static void intel_encoders_post_pll_disable(struct drm_crtc *crtc,
fd6bbda9 5268 struct intel_crtc_state *old_crtc_state,
fb1c98b1
ML
5269 struct drm_atomic_state *old_state)
5270{
5271 struct drm_connector_state *old_conn_state;
5272 struct drm_connector *conn;
5273 int i;
5274
5275 for_each_connector_in_state(old_state, conn, old_conn_state, i) {
5276 struct intel_encoder *encoder =
5277 to_intel_encoder(old_conn_state->best_encoder);
5278
5279 if (old_conn_state->crtc != crtc)
5280 continue;
5281
5282 if (encoder->post_pll_disable)
fd6bbda9 5283 encoder->post_pll_disable(encoder, old_crtc_state, old_conn_state);
fb1c98b1
ML
5284 }
5285}
5286
4a806558
ML
5287static void ironlake_crtc_enable(struct intel_crtc_state *pipe_config,
5288 struct drm_atomic_state *old_state)
f67a559d 5289{
4a806558 5290 struct drm_crtc *crtc = pipe_config->base.crtc;
f67a559d 5291 struct drm_device *dev = crtc->dev;
fac5e23e 5292 struct drm_i915_private *dev_priv = to_i915(dev);
f67a559d
JB
5293 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5294 int pipe = intel_crtc->pipe;
f67a559d 5295
53d9f4e9 5296 if (WARN_ON(intel_crtc->active))
f67a559d
JB
5297 return;
5298
b2c0593a
VS
5299 /*
5300 * Sometimes spurious CPU pipe underruns happen during FDI
5301 * training, at least with VGA+HDMI cloning. Suppress them.
5302 *
5303 * On ILK we get an occasional spurious CPU pipe underruns
5304 * between eDP port A enable and vdd enable. Also PCH port
5305 * enable seems to result in the occasional CPU pipe underrun.
5306 *
5307 * Spurious PCH underruns also occur during PCH enabling.
5308 */
5309 if (intel_crtc->config->has_pch_encoder || IS_GEN5(dev_priv))
5310 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
81b088ca
VS
5311 if (intel_crtc->config->has_pch_encoder)
5312 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
5313
6e3c9717 5314 if (intel_crtc->config->has_pch_encoder)
b14b1055
DV
5315 intel_prepare_shared_dpll(intel_crtc);
5316
37a5650b 5317 if (intel_crtc_has_dp_encoder(intel_crtc->config))
fe3cd48d 5318 intel_dp_set_m_n(intel_crtc, M1_N1);
29407aab
DV
5319
5320 intel_set_pipe_timings(intel_crtc);
bc58be60 5321 intel_set_pipe_src_size(intel_crtc);
29407aab 5322
6e3c9717 5323 if (intel_crtc->config->has_pch_encoder) {
29407aab 5324 intel_cpu_transcoder_set_m_n(intel_crtc,
6e3c9717 5325 &intel_crtc->config->fdi_m_n, NULL);
29407aab
DV
5326 }
5327
5328 ironlake_set_pipeconf(crtc);
5329
f67a559d 5330 intel_crtc->active = true;
8664281b 5331
fd6bbda9 5332 intel_encoders_pre_enable(crtc, pipe_config, old_state);
f67a559d 5333
6e3c9717 5334 if (intel_crtc->config->has_pch_encoder) {
fff367c7
DV
5335 /* Note: FDI PLL enabling _must_ be done before we enable the
5336 * cpu pipes, hence this is separate from all the other fdi/pch
5337 * enabling. */
88cefb6c 5338 ironlake_fdi_pll_enable(intel_crtc);
46b6f814
DV
5339 } else {
5340 assert_fdi_tx_disabled(dev_priv, pipe);
5341 assert_fdi_rx_disabled(dev_priv, pipe);
5342 }
f67a559d 5343
b074cec8 5344 ironlake_pfit_enable(intel_crtc);
f67a559d 5345
9c54c0dd
JB
5346 /*
5347 * On ILK+ LUT must be loaded before the pipe is running but with
5348 * clocks enabled
5349 */
b95c5321 5350 intel_color_load_luts(&pipe_config->base);
9c54c0dd 5351
1d5bf5d9
ID
5352 if (dev_priv->display.initial_watermarks != NULL)
5353 dev_priv->display.initial_watermarks(intel_crtc->config);
e1fdc473 5354 intel_enable_pipe(intel_crtc);
f67a559d 5355
6e3c9717 5356 if (intel_crtc->config->has_pch_encoder)
f67a559d 5357 ironlake_pch_enable(crtc);
c98e9dcf 5358
f9b61ff6
DV
5359 assert_vblank_disabled(crtc);
5360 drm_crtc_vblank_on(crtc);
5361
fd6bbda9 5362 intel_encoders_enable(crtc, pipe_config, old_state);
61b77ddd
DV
5363
5364 if (HAS_PCH_CPT(dev))
a1520318 5365 cpt_verify_modeset(dev, intel_crtc->pipe);
37ca8d4c
VS
5366
5367 /* Must wait for vblank to avoid spurious PCH FIFO underruns */
5368 if (intel_crtc->config->has_pch_encoder)
5369 intel_wait_for_vblank(dev, pipe);
b2c0593a 5370 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
37ca8d4c 5371 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
6be4a607
JB
5372}
5373
42db64ef
PZ
5374/* IPS only exists on ULT machines and is tied to pipe A. */
5375static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
5376{
f5adf94e 5377 return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
42db64ef
PZ
5378}
5379
4a806558
ML
5380static void haswell_crtc_enable(struct intel_crtc_state *pipe_config,
5381 struct drm_atomic_state *old_state)
4f771f10 5382{
4a806558 5383 struct drm_crtc *crtc = pipe_config->base.crtc;
4f771f10 5384 struct drm_device *dev = crtc->dev;
fac5e23e 5385 struct drm_i915_private *dev_priv = to_i915(dev);
4f771f10 5386 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
99d736a2 5387 int pipe = intel_crtc->pipe, hsw_workaround_pipe;
4d1de975 5388 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
4f771f10 5389
53d9f4e9 5390 if (WARN_ON(intel_crtc->active))
4f771f10
PZ
5391 return;
5392
81b088ca
VS
5393 if (intel_crtc->config->has_pch_encoder)
5394 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5395 false);
5396
fd6bbda9 5397 intel_encoders_pre_pll_enable(crtc, pipe_config, old_state);
95a7a2ae 5398
8106ddbd 5399 if (intel_crtc->config->shared_dpll)
df8ad70c
DV
5400 intel_enable_shared_dpll(intel_crtc);
5401
37a5650b 5402 if (intel_crtc_has_dp_encoder(intel_crtc->config))
fe3cd48d 5403 intel_dp_set_m_n(intel_crtc, M1_N1);
229fca97 5404
d7edc4e5 5405 if (!transcoder_is_dsi(cpu_transcoder))
4d1de975
JN
5406 intel_set_pipe_timings(intel_crtc);
5407
bc58be60 5408 intel_set_pipe_src_size(intel_crtc);
229fca97 5409
4d1de975
JN
5410 if (cpu_transcoder != TRANSCODER_EDP &&
5411 !transcoder_is_dsi(cpu_transcoder)) {
5412 I915_WRITE(PIPE_MULT(cpu_transcoder),
6e3c9717 5413 intel_crtc->config->pixel_multiplier - 1);
ebb69c95
CT
5414 }
5415
6e3c9717 5416 if (intel_crtc->config->has_pch_encoder) {
229fca97 5417 intel_cpu_transcoder_set_m_n(intel_crtc,
6e3c9717 5418 &intel_crtc->config->fdi_m_n, NULL);
229fca97
DV
5419 }
5420
d7edc4e5 5421 if (!transcoder_is_dsi(cpu_transcoder))
4d1de975
JN
5422 haswell_set_pipeconf(crtc);
5423
391bf048 5424 haswell_set_pipemisc(crtc);
229fca97 5425
b95c5321 5426 intel_color_set_csc(&pipe_config->base);
229fca97 5427
4f771f10 5428 intel_crtc->active = true;
8664281b 5429
6b698516
DV
5430 if (intel_crtc->config->has_pch_encoder)
5431 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
5432 else
5433 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
5434
fd6bbda9 5435 intel_encoders_pre_enable(crtc, pipe_config, old_state);
4f771f10 5436
d2d65408 5437 if (intel_crtc->config->has_pch_encoder)
4fe9467d 5438 dev_priv->display.fdi_link_train(crtc);
4fe9467d 5439
d7edc4e5 5440 if (!transcoder_is_dsi(cpu_transcoder))
7d4aefd0 5441 intel_ddi_enable_pipe_clock(intel_crtc);
4f771f10 5442
1c132b44 5443 if (INTEL_INFO(dev)->gen >= 9)
e435d6e5 5444 skylake_pfit_enable(intel_crtc);
ff6d9f55 5445 else
1c132b44 5446 ironlake_pfit_enable(intel_crtc);
4f771f10
PZ
5447
5448 /*
5449 * On ILK+ LUT must be loaded before the pipe is running but with
5450 * clocks enabled
5451 */
b95c5321 5452 intel_color_load_luts(&pipe_config->base);
4f771f10 5453
1f544388 5454 intel_ddi_set_pipe_settings(crtc);
d7edc4e5 5455 if (!transcoder_is_dsi(cpu_transcoder))
7d4aefd0 5456 intel_ddi_enable_transcoder_func(crtc);
4f771f10 5457
1d5bf5d9
ID
5458 if (dev_priv->display.initial_watermarks != NULL)
5459 dev_priv->display.initial_watermarks(pipe_config);
5460 else
5461 intel_update_watermarks(crtc);
4d1de975
JN
5462
5463 /* XXX: Do the pipe assertions at the right place for BXT DSI. */
d7edc4e5 5464 if (!transcoder_is_dsi(cpu_transcoder))
4d1de975 5465 intel_enable_pipe(intel_crtc);
42db64ef 5466
6e3c9717 5467 if (intel_crtc->config->has_pch_encoder)
1507e5bd 5468 lpt_pch_enable(crtc);
4f771f10 5469
a65347ba 5470 if (intel_crtc->config->dp_encoder_is_mst)
0e32b39c
DA
5471 intel_ddi_set_vc_payload_alloc(crtc, true);
5472
f9b61ff6
DV
5473 assert_vblank_disabled(crtc);
5474 drm_crtc_vblank_on(crtc);
5475
fd6bbda9 5476 intel_encoders_enable(crtc, pipe_config, old_state);
4f771f10 5477
6b698516
DV
5478 if (intel_crtc->config->has_pch_encoder) {
5479 intel_wait_for_vblank(dev, pipe);
5480 intel_wait_for_vblank(dev, pipe);
5481 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
d2d65408
VS
5482 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5483 true);
6b698516 5484 }
d2d65408 5485
e4916946
PZ
5486 /* If we change the relative order between pipe/planes enabling, we need
5487 * to change the workaround. */
99d736a2
ML
5488 hsw_workaround_pipe = pipe_config->hsw_workaround_pipe;
5489 if (IS_HASWELL(dev) && hsw_workaround_pipe != INVALID_PIPE) {
5490 intel_wait_for_vblank(dev, hsw_workaround_pipe);
5491 intel_wait_for_vblank(dev, hsw_workaround_pipe);
5492 }
4f771f10
PZ
5493}
5494
bfd16b2a 5495static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force)
3f8dce3a
DV
5496{
5497 struct drm_device *dev = crtc->base.dev;
fac5e23e 5498 struct drm_i915_private *dev_priv = to_i915(dev);
3f8dce3a
DV
5499 int pipe = crtc->pipe;
5500
5501 /* To avoid upsetting the power well on haswell only disable the pfit if
5502 * it's in use. The hw state code will make sure we get this right. */
bfd16b2a 5503 if (force || crtc->config->pch_pfit.enabled) {
3f8dce3a
DV
5504 I915_WRITE(PF_CTL(pipe), 0);
5505 I915_WRITE(PF_WIN_POS(pipe), 0);
5506 I915_WRITE(PF_WIN_SZ(pipe), 0);
5507 }
5508}
5509
4a806558
ML
5510static void ironlake_crtc_disable(struct intel_crtc_state *old_crtc_state,
5511 struct drm_atomic_state *old_state)
6be4a607 5512{
4a806558 5513 struct drm_crtc *crtc = old_crtc_state->base.crtc;
6be4a607 5514 struct drm_device *dev = crtc->dev;
fac5e23e 5515 struct drm_i915_private *dev_priv = to_i915(dev);
6be4a607
JB
5516 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5517 int pipe = intel_crtc->pipe;
b52eb4dc 5518
b2c0593a
VS
5519 /*
5520 * Sometimes spurious CPU pipe underruns happen when the
5521 * pipe is already disabled, but FDI RX/TX is still enabled.
5522 * Happens at least with VGA+HDMI cloning. Suppress them.
5523 */
5524 if (intel_crtc->config->has_pch_encoder) {
5525 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
37ca8d4c 5526 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
b2c0593a 5527 }
37ca8d4c 5528
fd6bbda9 5529 intel_encoders_disable(crtc, old_crtc_state, old_state);
ea9d758d 5530
f9b61ff6
DV
5531 drm_crtc_vblank_off(crtc);
5532 assert_vblank_disabled(crtc);
5533
575f7ab7 5534 intel_disable_pipe(intel_crtc);
32f9d658 5535
bfd16b2a 5536 ironlake_pfit_disable(intel_crtc, false);
2c07245f 5537
b2c0593a 5538 if (intel_crtc->config->has_pch_encoder)
5a74f70a
VS
5539 ironlake_fdi_disable(crtc);
5540
fd6bbda9 5541 intel_encoders_post_disable(crtc, old_crtc_state, old_state);
2c07245f 5542
6e3c9717 5543 if (intel_crtc->config->has_pch_encoder) {
d925c59a 5544 ironlake_disable_pch_transcoder(dev_priv, pipe);
6be4a607 5545
d925c59a 5546 if (HAS_PCH_CPT(dev)) {
f0f59a00
VS
5547 i915_reg_t reg;
5548 u32 temp;
5549
d925c59a
DV
5550 /* disable TRANS_DP_CTL */
5551 reg = TRANS_DP_CTL(pipe);
5552 temp = I915_READ(reg);
5553 temp &= ~(TRANS_DP_OUTPUT_ENABLE |
5554 TRANS_DP_PORT_SEL_MASK);
5555 temp |= TRANS_DP_PORT_SEL_NONE;
5556 I915_WRITE(reg, temp);
5557
5558 /* disable DPLL_SEL */
5559 temp = I915_READ(PCH_DPLL_SEL);
11887397 5560 temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
d925c59a 5561 I915_WRITE(PCH_DPLL_SEL, temp);
9db4a9c7 5562 }
e3421a18 5563
d925c59a
DV
5564 ironlake_fdi_pll_disable(intel_crtc);
5565 }
81b088ca 5566
b2c0593a 5567 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
81b088ca 5568 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
6be4a607 5569}
1b3c7a47 5570
4a806558
ML
5571static void haswell_crtc_disable(struct intel_crtc_state *old_crtc_state,
5572 struct drm_atomic_state *old_state)
ee7b9f93 5573{
4a806558 5574 struct drm_crtc *crtc = old_crtc_state->base.crtc;
4f771f10 5575 struct drm_device *dev = crtc->dev;
fac5e23e 5576 struct drm_i915_private *dev_priv = to_i915(dev);
ee7b9f93 5577 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6e3c9717 5578 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
ee7b9f93 5579
d2d65408
VS
5580 if (intel_crtc->config->has_pch_encoder)
5581 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5582 false);
5583
fd6bbda9 5584 intel_encoders_disable(crtc, old_crtc_state, old_state);
4f771f10 5585
f9b61ff6
DV
5586 drm_crtc_vblank_off(crtc);
5587 assert_vblank_disabled(crtc);
5588
4d1de975 5589 /* XXX: Do the pipe assertions at the right place for BXT DSI. */
d7edc4e5 5590 if (!transcoder_is_dsi(cpu_transcoder))
4d1de975 5591 intel_disable_pipe(intel_crtc);
4f771f10 5592
6e3c9717 5593 if (intel_crtc->config->dp_encoder_is_mst)
a4bf214f
VS
5594 intel_ddi_set_vc_payload_alloc(crtc, false);
5595
d7edc4e5 5596 if (!transcoder_is_dsi(cpu_transcoder))
7d4aefd0 5597 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
4f771f10 5598
1c132b44 5599 if (INTEL_INFO(dev)->gen >= 9)
e435d6e5 5600 skylake_scaler_disable(intel_crtc);
ff6d9f55 5601 else
bfd16b2a 5602 ironlake_pfit_disable(intel_crtc, false);
4f771f10 5603
d7edc4e5 5604 if (!transcoder_is_dsi(cpu_transcoder))
7d4aefd0 5605 intel_ddi_disable_pipe_clock(intel_crtc);
4f771f10 5606
fd6bbda9 5607 intel_encoders_post_disable(crtc, old_crtc_state, old_state);
81b088ca 5608
b7076546 5609 if (old_crtc_state->has_pch_encoder)
81b088ca
VS
5610 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5611 true);
4f771f10
PZ
5612}
5613
2dd24552
JB
5614static void i9xx_pfit_enable(struct intel_crtc *crtc)
5615{
5616 struct drm_device *dev = crtc->base.dev;
fac5e23e 5617 struct drm_i915_private *dev_priv = to_i915(dev);
6e3c9717 5618 struct intel_crtc_state *pipe_config = crtc->config;
2dd24552 5619
681a8504 5620 if (!pipe_config->gmch_pfit.control)
2dd24552
JB
5621 return;
5622
2dd24552 5623 /*
c0b03411
DV
5624 * The panel fitter should only be adjusted whilst the pipe is disabled,
5625 * according to register description and PRM.
2dd24552 5626 */
c0b03411
DV
5627 WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
5628 assert_pipe_disabled(dev_priv, crtc->pipe);
2dd24552 5629
b074cec8
JB
5630 I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
5631 I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
5a80c45c
DV
5632
5633 /* Border color in case we don't scale up to the full screen. Black by
5634 * default, change to something else for debugging. */
5635 I915_WRITE(BCLRPAT(crtc->pipe), 0);
2dd24552
JB
5636}
5637
d05410f9
DA
5638static enum intel_display_power_domain port_to_power_domain(enum port port)
5639{
5640 switch (port) {
5641 case PORT_A:
6331a704 5642 return POWER_DOMAIN_PORT_DDI_A_LANES;
d05410f9 5643 case PORT_B:
6331a704 5644 return POWER_DOMAIN_PORT_DDI_B_LANES;
d05410f9 5645 case PORT_C:
6331a704 5646 return POWER_DOMAIN_PORT_DDI_C_LANES;
d05410f9 5647 case PORT_D:
6331a704 5648 return POWER_DOMAIN_PORT_DDI_D_LANES;
d8e19f99 5649 case PORT_E:
6331a704 5650 return POWER_DOMAIN_PORT_DDI_E_LANES;
d05410f9 5651 default:
b9fec167 5652 MISSING_CASE(port);
d05410f9
DA
5653 return POWER_DOMAIN_PORT_OTHER;
5654 }
5655}
5656
25f78f58
VS
5657static enum intel_display_power_domain port_to_aux_power_domain(enum port port)
5658{
5659 switch (port) {
5660 case PORT_A:
5661 return POWER_DOMAIN_AUX_A;
5662 case PORT_B:
5663 return POWER_DOMAIN_AUX_B;
5664 case PORT_C:
5665 return POWER_DOMAIN_AUX_C;
5666 case PORT_D:
5667 return POWER_DOMAIN_AUX_D;
5668 case PORT_E:
5669 /* FIXME: Check VBT for actual wiring of PORT E */
5670 return POWER_DOMAIN_AUX_D;
5671 default:
b9fec167 5672 MISSING_CASE(port);
25f78f58
VS
5673 return POWER_DOMAIN_AUX_A;
5674 }
5675}
5676
319be8ae
ID
5677enum intel_display_power_domain
5678intel_display_port_power_domain(struct intel_encoder *intel_encoder)
5679{
5680 struct drm_device *dev = intel_encoder->base.dev;
5681 struct intel_digital_port *intel_dig_port;
5682
5683 switch (intel_encoder->type) {
5684 case INTEL_OUTPUT_UNKNOWN:
5685 /* Only DDI platforms should ever use this output type */
5686 WARN_ON_ONCE(!HAS_DDI(dev));
cca0502b 5687 case INTEL_OUTPUT_DP:
319be8ae
ID
5688 case INTEL_OUTPUT_HDMI:
5689 case INTEL_OUTPUT_EDP:
5690 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
d05410f9 5691 return port_to_power_domain(intel_dig_port->port);
0e32b39c
DA
5692 case INTEL_OUTPUT_DP_MST:
5693 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
5694 return port_to_power_domain(intel_dig_port->port);
319be8ae
ID
5695 case INTEL_OUTPUT_ANALOG:
5696 return POWER_DOMAIN_PORT_CRT;
5697 case INTEL_OUTPUT_DSI:
5698 return POWER_DOMAIN_PORT_DSI;
5699 default:
5700 return POWER_DOMAIN_PORT_OTHER;
5701 }
5702}
5703
25f78f58
VS
5704enum intel_display_power_domain
5705intel_display_port_aux_power_domain(struct intel_encoder *intel_encoder)
5706{
5707 struct drm_device *dev = intel_encoder->base.dev;
5708 struct intel_digital_port *intel_dig_port;
5709
5710 switch (intel_encoder->type) {
5711 case INTEL_OUTPUT_UNKNOWN:
651174a4
ID
5712 case INTEL_OUTPUT_HDMI:
5713 /*
5714 * Only DDI platforms should ever use these output types.
5715 * We can get here after the HDMI detect code has already set
5716 * the type of the shared encoder. Since we can't be sure
5717 * what's the status of the given connectors, play safe and
5718 * run the DP detection too.
5719 */
25f78f58 5720 WARN_ON_ONCE(!HAS_DDI(dev));
cca0502b 5721 case INTEL_OUTPUT_DP:
25f78f58
VS
5722 case INTEL_OUTPUT_EDP:
5723 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
5724 return port_to_aux_power_domain(intel_dig_port->port);
5725 case INTEL_OUTPUT_DP_MST:
5726 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
5727 return port_to_aux_power_domain(intel_dig_port->port);
5728 default:
b9fec167 5729 MISSING_CASE(intel_encoder->type);
25f78f58
VS
5730 return POWER_DOMAIN_AUX_A;
5731 }
5732}
5733
74bff5f9
ML
5734static unsigned long get_crtc_power_domains(struct drm_crtc *crtc,
5735 struct intel_crtc_state *crtc_state)
77d22dca 5736{
319be8ae 5737 struct drm_device *dev = crtc->dev;
74bff5f9 5738 struct drm_encoder *encoder;
319be8ae
ID
5739 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5740 enum pipe pipe = intel_crtc->pipe;
77d22dca 5741 unsigned long mask;
74bff5f9 5742 enum transcoder transcoder = crtc_state->cpu_transcoder;
77d22dca 5743
74bff5f9 5744 if (!crtc_state->base.active)
292b990e
ML
5745 return 0;
5746
77d22dca
ID
5747 mask = BIT(POWER_DOMAIN_PIPE(pipe));
5748 mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
74bff5f9
ML
5749 if (crtc_state->pch_pfit.enabled ||
5750 crtc_state->pch_pfit.force_thru)
77d22dca
ID
5751 mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
5752
74bff5f9
ML
5753 drm_for_each_encoder_mask(encoder, dev, crtc_state->base.encoder_mask) {
5754 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
5755
319be8ae 5756 mask |= BIT(intel_display_port_power_domain(intel_encoder));
74bff5f9 5757 }
319be8ae 5758
15e7ec29
ML
5759 if (crtc_state->shared_dpll)
5760 mask |= BIT(POWER_DOMAIN_PLLS);
5761
77d22dca
ID
5762 return mask;
5763}
5764
74bff5f9
ML
5765static unsigned long
5766modeset_get_crtc_power_domains(struct drm_crtc *crtc,
5767 struct intel_crtc_state *crtc_state)
77d22dca 5768{
fac5e23e 5769 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
292b990e
ML
5770 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5771 enum intel_display_power_domain domain;
5a21b665 5772 unsigned long domains, new_domains, old_domains;
77d22dca 5773
292b990e 5774 old_domains = intel_crtc->enabled_power_domains;
74bff5f9
ML
5775 intel_crtc->enabled_power_domains = new_domains =
5776 get_crtc_power_domains(crtc, crtc_state);
77d22dca 5777
5a21b665 5778 domains = new_domains & ~old_domains;
292b990e
ML
5779
5780 for_each_power_domain(domain, domains)
5781 intel_display_power_get(dev_priv, domain);
5782
5a21b665 5783 return old_domains & ~new_domains;
292b990e
ML
5784}
5785
5786static void modeset_put_power_domains(struct drm_i915_private *dev_priv,
5787 unsigned long domains)
5788{
5789 enum intel_display_power_domain domain;
5790
5791 for_each_power_domain(domain, domains)
5792 intel_display_power_put(dev_priv, domain);
5793}
77d22dca 5794
adafdc6f
MK
5795static int intel_compute_max_dotclk(struct drm_i915_private *dev_priv)
5796{
5797 int max_cdclk_freq = dev_priv->max_cdclk_freq;
5798
5799 if (INTEL_INFO(dev_priv)->gen >= 9 ||
5800 IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
5801 return max_cdclk_freq;
5802 else if (IS_CHERRYVIEW(dev_priv))
5803 return max_cdclk_freq*95/100;
5804 else if (INTEL_INFO(dev_priv)->gen < 4)
5805 return 2*max_cdclk_freq*90/100;
5806 else
5807 return max_cdclk_freq*90/100;
5808}
5809
b2045352
VS
5810static int skl_calc_cdclk(int max_pixclk, int vco);
5811
560a7ae4
DL
5812static void intel_update_max_cdclk(struct drm_device *dev)
5813{
fac5e23e 5814 struct drm_i915_private *dev_priv = to_i915(dev);
560a7ae4 5815
ef11bdb3 5816 if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) {
560a7ae4 5817 u32 limit = I915_READ(SKL_DFSM) & SKL_DFSM_CDCLK_LIMIT_MASK;
b2045352
VS
5818 int max_cdclk, vco;
5819
5820 vco = dev_priv->skl_preferred_vco_freq;
63911d72 5821 WARN_ON(vco != 8100000 && vco != 8640000);
560a7ae4 5822
b2045352
VS
5823 /*
5824 * Use the lower (vco 8640) cdclk values as a
5825 * first guess. skl_calc_cdclk() will correct it
5826 * if the preferred vco is 8100 instead.
5827 */
560a7ae4 5828 if (limit == SKL_DFSM_CDCLK_LIMIT_675)
487ed2e4 5829 max_cdclk = 617143;
560a7ae4 5830 else if (limit == SKL_DFSM_CDCLK_LIMIT_540)
b2045352 5831 max_cdclk = 540000;
560a7ae4 5832 else if (limit == SKL_DFSM_CDCLK_LIMIT_450)
b2045352 5833 max_cdclk = 432000;
560a7ae4 5834 else
487ed2e4 5835 max_cdclk = 308571;
b2045352
VS
5836
5837 dev_priv->max_cdclk_freq = skl_calc_cdclk(max_cdclk, vco);
281c114f
MR
5838 } else if (IS_BROXTON(dev)) {
5839 dev_priv->max_cdclk_freq = 624000;
560a7ae4
DL
5840 } else if (IS_BROADWELL(dev)) {
5841 /*
5842 * FIXME with extra cooling we can allow
5843 * 540 MHz for ULX and 675 Mhz for ULT.
5844 * How can we know if extra cooling is
5845 * available? PCI ID, VTB, something else?
5846 */
5847 if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
5848 dev_priv->max_cdclk_freq = 450000;
5849 else if (IS_BDW_ULX(dev))
5850 dev_priv->max_cdclk_freq = 450000;
5851 else if (IS_BDW_ULT(dev))
5852 dev_priv->max_cdclk_freq = 540000;
5853 else
5854 dev_priv->max_cdclk_freq = 675000;
0904deaf
MK
5855 } else if (IS_CHERRYVIEW(dev)) {
5856 dev_priv->max_cdclk_freq = 320000;
560a7ae4
DL
5857 } else if (IS_VALLEYVIEW(dev)) {
5858 dev_priv->max_cdclk_freq = 400000;
5859 } else {
5860 /* otherwise assume cdclk is fixed */
5861 dev_priv->max_cdclk_freq = dev_priv->cdclk_freq;
5862 }
5863
adafdc6f
MK
5864 dev_priv->max_dotclk_freq = intel_compute_max_dotclk(dev_priv);
5865
560a7ae4
DL
5866 DRM_DEBUG_DRIVER("Max CD clock rate: %d kHz\n",
5867 dev_priv->max_cdclk_freq);
adafdc6f
MK
5868
5869 DRM_DEBUG_DRIVER("Max dotclock rate: %d kHz\n",
5870 dev_priv->max_dotclk_freq);
560a7ae4
DL
5871}
5872
5873static void intel_update_cdclk(struct drm_device *dev)
5874{
fac5e23e 5875 struct drm_i915_private *dev_priv = to_i915(dev);
560a7ae4
DL
5876
5877 dev_priv->cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
2f2a121a 5878
83d7c81f 5879 if (INTEL_GEN(dev_priv) >= 9)
709e05c3
VS
5880 DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz, VCO: %d kHz, ref: %d kHz\n",
5881 dev_priv->cdclk_freq, dev_priv->cdclk_pll.vco,
5882 dev_priv->cdclk_pll.ref);
2f2a121a
VS
5883 else
5884 DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
5885 dev_priv->cdclk_freq);
560a7ae4
DL
5886
5887 /*
b5d99ff9
VS
5888 * 9:0 CMBUS [sic] CDCLK frequency (cdfreq):
5889 * Programmng [sic] note: bit[9:2] should be programmed to the number
5890 * of cdclk that generates 4MHz reference clock freq which is used to
5891 * generate GMBus clock. This will vary with the cdclk freq.
560a7ae4 5892 */
b5d99ff9 5893 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
560a7ae4 5894 I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->cdclk_freq, 1000));
560a7ae4
DL
5895}
5896
92891e45
VS
5897/* convert from kHz to .1 fixpoint MHz with -1MHz offset */
5898static int skl_cdclk_decimal(int cdclk)
5899{
5900 return DIV_ROUND_CLOSEST(cdclk - 1000, 500);
5901}
5902
5f199dfa
VS
5903static int bxt_de_pll_vco(struct drm_i915_private *dev_priv, int cdclk)
5904{
5905 int ratio;
5906
5907 if (cdclk == dev_priv->cdclk_pll.ref)
5908 return 0;
5909
5910 switch (cdclk) {
5911 default:
5912 MISSING_CASE(cdclk);
5913 case 144000:
5914 case 288000:
5915 case 384000:
5916 case 576000:
5917 ratio = 60;
5918 break;
5919 case 624000:
5920 ratio = 65;
5921 break;
5922 }
5923
5924 return dev_priv->cdclk_pll.ref * ratio;
5925}
5926
2b73001e
VS
5927static void bxt_de_pll_disable(struct drm_i915_private *dev_priv)
5928{
5929 I915_WRITE(BXT_DE_PLL_ENABLE, 0);
5930
5931 /* Timeout 200us */
95cac283
CW
5932 if (intel_wait_for_register(dev_priv,
5933 BXT_DE_PLL_ENABLE, BXT_DE_PLL_LOCK, 0,
5934 1))
2b73001e 5935 DRM_ERROR("timeout waiting for DE PLL unlock\n");
83d7c81f
VS
5936
5937 dev_priv->cdclk_pll.vco = 0;
2b73001e
VS
5938}
5939
5f199dfa 5940static void bxt_de_pll_enable(struct drm_i915_private *dev_priv, int vco)
2b73001e 5941{
5f199dfa 5942 int ratio = DIV_ROUND_CLOSEST(vco, dev_priv->cdclk_pll.ref);
2b73001e
VS
5943 u32 val;
5944
5945 val = I915_READ(BXT_DE_PLL_CTL);
5946 val &= ~BXT_DE_PLL_RATIO_MASK;
5f199dfa 5947 val |= BXT_DE_PLL_RATIO(ratio);
2b73001e
VS
5948 I915_WRITE(BXT_DE_PLL_CTL, val);
5949
5950 I915_WRITE(BXT_DE_PLL_ENABLE, BXT_DE_PLL_PLL_ENABLE);
5951
5952 /* Timeout 200us */
e084e1b9
CW
5953 if (intel_wait_for_register(dev_priv,
5954 BXT_DE_PLL_ENABLE,
5955 BXT_DE_PLL_LOCK,
5956 BXT_DE_PLL_LOCK,
5957 1))
2b73001e 5958 DRM_ERROR("timeout waiting for DE PLL lock\n");
83d7c81f 5959
5f199dfa 5960 dev_priv->cdclk_pll.vco = vco;
2b73001e
VS
5961}
5962
324513c0 5963static void bxt_set_cdclk(struct drm_i915_private *dev_priv, int cdclk)
f8437dd1 5964{
5f199dfa
VS
5965 u32 val, divider;
5966 int vco, ret;
f8437dd1 5967
5f199dfa
VS
5968 vco = bxt_de_pll_vco(dev_priv, cdclk);
5969
5970 DRM_DEBUG_DRIVER("Changing CDCLK to %d kHz (VCO %d kHz)\n", cdclk, vco);
5971
5972 /* cdclk = vco / 2 / div{1,1.5,2,4} */
5973 switch (DIV_ROUND_CLOSEST(vco, cdclk)) {
5974 case 8:
f8437dd1 5975 divider = BXT_CDCLK_CD2X_DIV_SEL_4;
f8437dd1 5976 break;
5f199dfa 5977 case 4:
f8437dd1 5978 divider = BXT_CDCLK_CD2X_DIV_SEL_2;
f8437dd1 5979 break;
5f199dfa 5980 case 3:
f8437dd1 5981 divider = BXT_CDCLK_CD2X_DIV_SEL_1_5;
f8437dd1 5982 break;
5f199dfa 5983 case 2:
f8437dd1 5984 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
f8437dd1
VK
5985 break;
5986 default:
5f199dfa
VS
5987 WARN_ON(cdclk != dev_priv->cdclk_pll.ref);
5988 WARN_ON(vco != 0);
f8437dd1 5989
5f199dfa
VS
5990 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
5991 break;
f8437dd1
VK
5992 }
5993
f8437dd1 5994 /* Inform power controller of upcoming frequency change */
5f199dfa 5995 mutex_lock(&dev_priv->rps.hw_lock);
f8437dd1
VK
5996 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
5997 0x80000000);
5998 mutex_unlock(&dev_priv->rps.hw_lock);
5999
6000 if (ret) {
6001 DRM_ERROR("PCode CDCLK freq change notify failed (err %d, freq %d)\n",
9ef56154 6002 ret, cdclk);
f8437dd1
VK
6003 return;
6004 }
6005
5f199dfa
VS
6006 if (dev_priv->cdclk_pll.vco != 0 &&
6007 dev_priv->cdclk_pll.vco != vco)
2b73001e 6008 bxt_de_pll_disable(dev_priv);
f8437dd1 6009
5f199dfa
VS
6010 if (dev_priv->cdclk_pll.vco != vco)
6011 bxt_de_pll_enable(dev_priv, vco);
f8437dd1 6012
5f199dfa
VS
6013 val = divider | skl_cdclk_decimal(cdclk);
6014 /*
6015 * FIXME if only the cd2x divider needs changing, it could be done
6016 * without shutting off the pipe (if only one pipe is active).
6017 */
6018 val |= BXT_CDCLK_CD2X_PIPE_NONE;
6019 /*
6020 * Disable SSA Precharge when CD clock frequency < 500 MHz,
6021 * enable otherwise.
6022 */
6023 if (cdclk >= 500000)
6024 val |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
6025 I915_WRITE(CDCLK_CTL, val);
f8437dd1
VK
6026
6027 mutex_lock(&dev_priv->rps.hw_lock);
6028 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
9ef56154 6029 DIV_ROUND_UP(cdclk, 25000));
f8437dd1
VK
6030 mutex_unlock(&dev_priv->rps.hw_lock);
6031
6032 if (ret) {
6033 DRM_ERROR("PCode CDCLK freq set failed, (err %d, freq %d)\n",
9ef56154 6034 ret, cdclk);
f8437dd1
VK
6035 return;
6036 }
6037
91c8a326 6038 intel_update_cdclk(&dev_priv->drm);
f8437dd1
VK
6039}
6040
d66a2194 6041static void bxt_sanitize_cdclk(struct drm_i915_private *dev_priv)
f8437dd1 6042{
d66a2194
ID
6043 u32 cdctl, expected;
6044
91c8a326 6045 intel_update_cdclk(&dev_priv->drm);
f8437dd1 6046
d66a2194
ID
6047 if (dev_priv->cdclk_pll.vco == 0 ||
6048 dev_priv->cdclk_freq == dev_priv->cdclk_pll.ref)
6049 goto sanitize;
6050
6051 /* DPLL okay; verify the cdclock
6052 *
6053 * Some BIOS versions leave an incorrect decimal frequency value and
6054 * set reserved MBZ bits in CDCLK_CTL at least during exiting from S4,
6055 * so sanitize this register.
6056 */
6057 cdctl = I915_READ(CDCLK_CTL);
6058 /*
6059 * Let's ignore the pipe field, since BIOS could have configured the
6060 * dividers both synching to an active pipe, or asynchronously
6061 * (PIPE_NONE).
6062 */
6063 cdctl &= ~BXT_CDCLK_CD2X_PIPE_NONE;
6064
6065 expected = (cdctl & BXT_CDCLK_CD2X_DIV_SEL_MASK) |
6066 skl_cdclk_decimal(dev_priv->cdclk_freq);
6067 /*
6068 * Disable SSA Precharge when CD clock frequency < 500 MHz,
6069 * enable otherwise.
6070 */
6071 if (dev_priv->cdclk_freq >= 500000)
6072 expected |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
6073
6074 if (cdctl == expected)
6075 /* All well; nothing to sanitize */
6076 return;
6077
6078sanitize:
6079 DRM_DEBUG_KMS("Sanitizing cdclk programmed by pre-os\n");
6080
6081 /* force cdclk programming */
6082 dev_priv->cdclk_freq = 0;
6083
6084 /* force full PLL disable + enable */
6085 dev_priv->cdclk_pll.vco = -1;
6086}
6087
324513c0 6088void bxt_init_cdclk(struct drm_i915_private *dev_priv)
d66a2194
ID
6089{
6090 bxt_sanitize_cdclk(dev_priv);
6091
6092 if (dev_priv->cdclk_freq != 0 && dev_priv->cdclk_pll.vco != 0)
089c6fd5 6093 return;
c2e001ef 6094
f8437dd1
VK
6095 /*
6096 * FIXME:
6097 * - The initial CDCLK needs to be read from VBT.
6098 * Need to make this change after VBT has changes for BXT.
f8437dd1 6099 */
324513c0 6100 bxt_set_cdclk(dev_priv, bxt_calc_cdclk(0));
f8437dd1
VK
6101}
6102
324513c0 6103void bxt_uninit_cdclk(struct drm_i915_private *dev_priv)
f8437dd1 6104{
324513c0 6105 bxt_set_cdclk(dev_priv, dev_priv->cdclk_pll.ref);
f8437dd1
VK
6106}
6107
a8ca4934
VS
6108static int skl_calc_cdclk(int max_pixclk, int vco)
6109{
63911d72 6110 if (vco == 8640000) {
a8ca4934 6111 if (max_pixclk > 540000)
487ed2e4 6112 return 617143;
a8ca4934
VS
6113 else if (max_pixclk > 432000)
6114 return 540000;
487ed2e4 6115 else if (max_pixclk > 308571)
a8ca4934
VS
6116 return 432000;
6117 else
487ed2e4 6118 return 308571;
a8ca4934 6119 } else {
a8ca4934
VS
6120 if (max_pixclk > 540000)
6121 return 675000;
6122 else if (max_pixclk > 450000)
6123 return 540000;
6124 else if (max_pixclk > 337500)
6125 return 450000;
6126 else
6127 return 337500;
6128 }
6129}
6130
ea61791e
VS
6131static void
6132skl_dpll0_update(struct drm_i915_private *dev_priv)
5d96d8af 6133{
ea61791e 6134 u32 val;
5d96d8af 6135
709e05c3 6136 dev_priv->cdclk_pll.ref = 24000;
1c3f7700 6137 dev_priv->cdclk_pll.vco = 0;
709e05c3 6138
ea61791e 6139 val = I915_READ(LCPLL1_CTL);
1c3f7700 6140 if ((val & LCPLL_PLL_ENABLE) == 0)
ea61791e 6141 return;
5d96d8af 6142
1c3f7700
ID
6143 if (WARN_ON((val & LCPLL_PLL_LOCK) == 0))
6144 return;
9f7eb31a 6145
ea61791e
VS
6146 val = I915_READ(DPLL_CTRL1);
6147
1c3f7700
ID
6148 if (WARN_ON((val & (DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) |
6149 DPLL_CTRL1_SSC(SKL_DPLL0) |
6150 DPLL_CTRL1_OVERRIDE(SKL_DPLL0))) !=
6151 DPLL_CTRL1_OVERRIDE(SKL_DPLL0)))
6152 return;
9f7eb31a 6153
ea61791e
VS
6154 switch (val & DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0)) {
6155 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810, SKL_DPLL0):
6156 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1350, SKL_DPLL0):
6157 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1620, SKL_DPLL0):
6158 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_2700, SKL_DPLL0):
63911d72 6159 dev_priv->cdclk_pll.vco = 8100000;
ea61791e
VS
6160 break;
6161 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080, SKL_DPLL0):
6162 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_2160, SKL_DPLL0):
63911d72 6163 dev_priv->cdclk_pll.vco = 8640000;
ea61791e
VS
6164 break;
6165 default:
6166 MISSING_CASE(val & DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
ea61791e
VS
6167 break;
6168 }
5d96d8af
DL
6169}
6170
b2045352
VS
6171void skl_set_preferred_cdclk_vco(struct drm_i915_private *dev_priv, int vco)
6172{
6173 bool changed = dev_priv->skl_preferred_vco_freq != vco;
6174
6175 dev_priv->skl_preferred_vco_freq = vco;
6176
6177 if (changed)
91c8a326 6178 intel_update_max_cdclk(&dev_priv->drm);
b2045352
VS
6179}
6180
5d96d8af 6181static void
3861fc60 6182skl_dpll0_enable(struct drm_i915_private *dev_priv, int vco)
5d96d8af 6183{
a8ca4934 6184 int min_cdclk = skl_calc_cdclk(0, vco);
5d96d8af
DL
6185 u32 val;
6186
63911d72 6187 WARN_ON(vco != 8100000 && vco != 8640000);
b2045352 6188
5d96d8af 6189 /* select the minimum CDCLK before enabling DPLL 0 */
9ef56154 6190 val = CDCLK_FREQ_337_308 | skl_cdclk_decimal(min_cdclk);
5d96d8af
DL
6191 I915_WRITE(CDCLK_CTL, val);
6192 POSTING_READ(CDCLK_CTL);
6193
6194 /*
6195 * We always enable DPLL0 with the lowest link rate possible, but still
6196 * taking into account the VCO required to operate the eDP panel at the
6197 * desired frequency. The usual DP link rates operate with a VCO of
6198 * 8100 while the eDP 1.4 alternate link rates need a VCO of 8640.
6199 * The modeset code is responsible for the selection of the exact link
6200 * rate later on, with the constraint of choosing a frequency that
a8ca4934 6201 * works with vco.
5d96d8af
DL
6202 */
6203 val = I915_READ(DPLL_CTRL1);
6204
6205 val &= ~(DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) | DPLL_CTRL1_SSC(SKL_DPLL0) |
6206 DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
6207 val |= DPLL_CTRL1_OVERRIDE(SKL_DPLL0);
63911d72 6208 if (vco == 8640000)
5d96d8af
DL
6209 val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080,
6210 SKL_DPLL0);
6211 else
6212 val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810,
6213 SKL_DPLL0);
6214
6215 I915_WRITE(DPLL_CTRL1, val);
6216 POSTING_READ(DPLL_CTRL1);
6217
6218 I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) | LCPLL_PLL_ENABLE);
6219
e24ca054
CW
6220 if (intel_wait_for_register(dev_priv,
6221 LCPLL1_CTL, LCPLL_PLL_LOCK, LCPLL_PLL_LOCK,
6222 5))
5d96d8af 6223 DRM_ERROR("DPLL0 not locked\n");
1cd593e0 6224
63911d72 6225 dev_priv->cdclk_pll.vco = vco;
b2045352
VS
6226
6227 /* We'll want to keep using the current vco from now on. */
6228 skl_set_preferred_cdclk_vco(dev_priv, vco);
5d96d8af
DL
6229}
6230
430e05de
VS
6231static void
6232skl_dpll0_disable(struct drm_i915_private *dev_priv)
6233{
6234 I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) & ~LCPLL_PLL_ENABLE);
8ad32a05
CW
6235 if (intel_wait_for_register(dev_priv,
6236 LCPLL1_CTL, LCPLL_PLL_LOCK, 0,
6237 1))
430e05de 6238 DRM_ERROR("Couldn't disable DPLL0\n");
1cd593e0 6239
63911d72 6240 dev_priv->cdclk_pll.vco = 0;
430e05de
VS
6241}
6242
5d96d8af
DL
6243static bool skl_cdclk_pcu_ready(struct drm_i915_private *dev_priv)
6244{
6245 int ret;
6246 u32 val;
6247
6248 /* inform PCU we want to change CDCLK */
6249 val = SKL_CDCLK_PREPARE_FOR_CHANGE;
6250 mutex_lock(&dev_priv->rps.hw_lock);
6251 ret = sandybridge_pcode_read(dev_priv, SKL_PCODE_CDCLK_CONTROL, &val);
6252 mutex_unlock(&dev_priv->rps.hw_lock);
6253
6254 return ret == 0 && (val & SKL_CDCLK_READY_FOR_CHANGE);
6255}
6256
6257static bool skl_cdclk_wait_for_pcu_ready(struct drm_i915_private *dev_priv)
6258{
848496e5 6259 return _wait_for(skl_cdclk_pcu_ready(dev_priv), 3000, 10) == 0;
5d96d8af
DL
6260}
6261
1cd593e0 6262static void skl_set_cdclk(struct drm_i915_private *dev_priv, int cdclk, int vco)
5d96d8af 6263{
91c8a326 6264 struct drm_device *dev = &dev_priv->drm;
5d96d8af
DL
6265 u32 freq_select, pcu_ack;
6266
1cd593e0
VS
6267 WARN_ON((cdclk == 24000) != (vco == 0));
6268
63911d72 6269 DRM_DEBUG_DRIVER("Changing CDCLK to %d kHz (VCO %d kHz)\n", cdclk, vco);
5d96d8af
DL
6270
6271 if (!skl_cdclk_wait_for_pcu_ready(dev_priv)) {
6272 DRM_ERROR("failed to inform PCU about cdclk change\n");
6273 return;
6274 }
6275
6276 /* set CDCLK_CTL */
9ef56154 6277 switch (cdclk) {
5d96d8af
DL
6278 case 450000:
6279 case 432000:
6280 freq_select = CDCLK_FREQ_450_432;
6281 pcu_ack = 1;
6282 break;
6283 case 540000:
6284 freq_select = CDCLK_FREQ_540;
6285 pcu_ack = 2;
6286 break;
487ed2e4 6287 case 308571:
5d96d8af
DL
6288 case 337500:
6289 default:
6290 freq_select = CDCLK_FREQ_337_308;
6291 pcu_ack = 0;
6292 break;
487ed2e4 6293 case 617143:
5d96d8af
DL
6294 case 675000:
6295 freq_select = CDCLK_FREQ_675_617;
6296 pcu_ack = 3;
6297 break;
6298 }
6299
63911d72
VS
6300 if (dev_priv->cdclk_pll.vco != 0 &&
6301 dev_priv->cdclk_pll.vco != vco)
1cd593e0
VS
6302 skl_dpll0_disable(dev_priv);
6303
63911d72 6304 if (dev_priv->cdclk_pll.vco != vco)
1cd593e0
VS
6305 skl_dpll0_enable(dev_priv, vco);
6306
9ef56154 6307 I915_WRITE(CDCLK_CTL, freq_select | skl_cdclk_decimal(cdclk));
5d96d8af
DL
6308 POSTING_READ(CDCLK_CTL);
6309
6310 /* inform PCU of the change */
6311 mutex_lock(&dev_priv->rps.hw_lock);
6312 sandybridge_pcode_write(dev_priv, SKL_PCODE_CDCLK_CONTROL, pcu_ack);
6313 mutex_unlock(&dev_priv->rps.hw_lock);
560a7ae4
DL
6314
6315 intel_update_cdclk(dev);
5d96d8af
DL
6316}
6317
9f7eb31a
VS
6318static void skl_sanitize_cdclk(struct drm_i915_private *dev_priv);
6319
5d96d8af
DL
6320void skl_uninit_cdclk(struct drm_i915_private *dev_priv)
6321{
709e05c3 6322 skl_set_cdclk(dev_priv, dev_priv->cdclk_pll.ref, 0);
5d96d8af
DL
6323}
6324
6325void skl_init_cdclk(struct drm_i915_private *dev_priv)
6326{
9f7eb31a
VS
6327 int cdclk, vco;
6328
6329 skl_sanitize_cdclk(dev_priv);
5d96d8af 6330
63911d72 6331 if (dev_priv->cdclk_freq != 0 && dev_priv->cdclk_pll.vco != 0) {
9f7eb31a
VS
6332 /*
6333 * Use the current vco as our initial
6334 * guess as to what the preferred vco is.
6335 */
6336 if (dev_priv->skl_preferred_vco_freq == 0)
6337 skl_set_preferred_cdclk_vco(dev_priv,
63911d72 6338 dev_priv->cdclk_pll.vco);
70c2c184 6339 return;
1cd593e0 6340 }
5d96d8af 6341
70c2c184
VS
6342 vco = dev_priv->skl_preferred_vco_freq;
6343 if (vco == 0)
63911d72 6344 vco = 8100000;
70c2c184 6345 cdclk = skl_calc_cdclk(0, vco);
5d96d8af 6346
70c2c184 6347 skl_set_cdclk(dev_priv, cdclk, vco);
5d96d8af
DL
6348}
6349
9f7eb31a 6350static void skl_sanitize_cdclk(struct drm_i915_private *dev_priv)
c73666f3 6351{
09492498 6352 uint32_t cdctl, expected;
c73666f3 6353
f1b391a5
SK
6354 /*
6355 * check if the pre-os intialized the display
6356 * There is SWF18 scratchpad register defined which is set by the
6357 * pre-os which can be used by the OS drivers to check the status
6358 */
6359 if ((I915_READ(SWF_ILK(0x18)) & 0x00FFFFFF) == 0)
6360 goto sanitize;
6361
91c8a326 6362 intel_update_cdclk(&dev_priv->drm);
c73666f3 6363 /* Is PLL enabled and locked ? */
1c3f7700
ID
6364 if (dev_priv->cdclk_pll.vco == 0 ||
6365 dev_priv->cdclk_freq == dev_priv->cdclk_pll.ref)
c73666f3
SK
6366 goto sanitize;
6367
6368 /* DPLL okay; verify the cdclock
6369 *
6370 * Noticed in some instances that the freq selection is correct but
6371 * decimal part is programmed wrong from BIOS where pre-os does not
6372 * enable display. Verify the same as well.
6373 */
09492498
VS
6374 cdctl = I915_READ(CDCLK_CTL);
6375 expected = (cdctl & CDCLK_FREQ_SEL_MASK) |
6376 skl_cdclk_decimal(dev_priv->cdclk_freq);
6377 if (cdctl == expected)
c73666f3 6378 /* All well; nothing to sanitize */
9f7eb31a 6379 return;
c89e39f3 6380
9f7eb31a
VS
6381sanitize:
6382 DRM_DEBUG_KMS("Sanitizing cdclk programmed by pre-os\n");
c73666f3 6383
9f7eb31a
VS
6384 /* force cdclk programming */
6385 dev_priv->cdclk_freq = 0;
6386 /* force full PLL disable + enable */
63911d72 6387 dev_priv->cdclk_pll.vco = -1;
c73666f3
SK
6388}
6389
30a970c6
JB
6390/* Adjust CDclk dividers to allow high res or save power if possible */
6391static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
6392{
fac5e23e 6393 struct drm_i915_private *dev_priv = to_i915(dev);
30a970c6
JB
6394 u32 val, cmd;
6395
164dfd28
VK
6396 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
6397 != dev_priv->cdclk_freq);
d60c4473 6398
dfcab17e 6399 if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
30a970c6 6400 cmd = 2;
dfcab17e 6401 else if (cdclk == 266667)
30a970c6
JB
6402 cmd = 1;
6403 else
6404 cmd = 0;
6405
6406 mutex_lock(&dev_priv->rps.hw_lock);
6407 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
6408 val &= ~DSPFREQGUAR_MASK;
6409 val |= (cmd << DSPFREQGUAR_SHIFT);
6410 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
6411 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
6412 DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
6413 50)) {
6414 DRM_ERROR("timed out waiting for CDclk change\n");
6415 }
6416 mutex_unlock(&dev_priv->rps.hw_lock);
6417
54433e91
VS
6418 mutex_lock(&dev_priv->sb_lock);
6419
dfcab17e 6420 if (cdclk == 400000) {
6bcda4f0 6421 u32 divider;
30a970c6 6422
6bcda4f0 6423 divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
30a970c6 6424
30a970c6
JB
6425 /* adjust cdclk divider */
6426 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
87d5d259 6427 val &= ~CCK_FREQUENCY_VALUES;
30a970c6
JB
6428 val |= divider;
6429 vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
a877e801
VS
6430
6431 if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
87d5d259 6432 CCK_FREQUENCY_STATUS) == (divider << CCK_FREQUENCY_STATUS_SHIFT),
a877e801
VS
6433 50))
6434 DRM_ERROR("timed out waiting for CDclk change\n");
30a970c6
JB
6435 }
6436
30a970c6
JB
6437 /* adjust self-refresh exit latency value */
6438 val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
6439 val &= ~0x7f;
6440
6441 /*
6442 * For high bandwidth configs, we set a higher latency in the bunit
6443 * so that the core display fetch happens in time to avoid underruns.
6444 */
dfcab17e 6445 if (cdclk == 400000)
30a970c6
JB
6446 val |= 4500 / 250; /* 4.5 usec */
6447 else
6448 val |= 3000 / 250; /* 3.0 usec */
6449 vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
54433e91 6450
a580516d 6451 mutex_unlock(&dev_priv->sb_lock);
30a970c6 6452
b6283055 6453 intel_update_cdclk(dev);
30a970c6
JB
6454}
6455
383c5a6a
VS
6456static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
6457{
fac5e23e 6458 struct drm_i915_private *dev_priv = to_i915(dev);
383c5a6a
VS
6459 u32 val, cmd;
6460
164dfd28
VK
6461 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
6462 != dev_priv->cdclk_freq);
383c5a6a
VS
6463
6464 switch (cdclk) {
383c5a6a
VS
6465 case 333333:
6466 case 320000:
383c5a6a 6467 case 266667:
383c5a6a 6468 case 200000:
383c5a6a
VS
6469 break;
6470 default:
5f77eeb0 6471 MISSING_CASE(cdclk);
383c5a6a
VS
6472 return;
6473 }
6474
9d0d3fda
VS
6475 /*
6476 * Specs are full of misinformation, but testing on actual
6477 * hardware has shown that we just need to write the desired
6478 * CCK divider into the Punit register.
6479 */
6480 cmd = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
6481
383c5a6a
VS
6482 mutex_lock(&dev_priv->rps.hw_lock);
6483 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
6484 val &= ~DSPFREQGUAR_MASK_CHV;
6485 val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
6486 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
6487 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
6488 DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
6489 50)) {
6490 DRM_ERROR("timed out waiting for CDclk change\n");
6491 }
6492 mutex_unlock(&dev_priv->rps.hw_lock);
6493
b6283055 6494 intel_update_cdclk(dev);
383c5a6a
VS
6495}
6496
30a970c6
JB
6497static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
6498 int max_pixclk)
6499{
6bcda4f0 6500 int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
6cca3195 6501 int limit = IS_CHERRYVIEW(dev_priv) ? 95 : 90;
29dc7ef3 6502
30a970c6
JB
6503 /*
6504 * Really only a few cases to deal with, as only 4 CDclks are supported:
6505 * 200MHz
6506 * 267MHz
29dc7ef3 6507 * 320/333MHz (depends on HPLL freq)
6cca3195
VS
6508 * 400MHz (VLV only)
6509 * So we check to see whether we're above 90% (VLV) or 95% (CHV)
6510 * of the lower bin and adjust if needed.
e37c67a1
VS
6511 *
6512 * We seem to get an unstable or solid color picture at 200MHz.
6513 * Not sure what's wrong. For now use 200MHz only when all pipes
6514 * are off.
30a970c6 6515 */
6cca3195
VS
6516 if (!IS_CHERRYVIEW(dev_priv) &&
6517 max_pixclk > freq_320*limit/100)
dfcab17e 6518 return 400000;
6cca3195 6519 else if (max_pixclk > 266667*limit/100)
29dc7ef3 6520 return freq_320;
e37c67a1 6521 else if (max_pixclk > 0)
dfcab17e 6522 return 266667;
e37c67a1
VS
6523 else
6524 return 200000;
30a970c6
JB
6525}
6526
324513c0 6527static int bxt_calc_cdclk(int max_pixclk)
f8437dd1 6528{
760e1477 6529 if (max_pixclk > 576000)
f8437dd1 6530 return 624000;
760e1477 6531 else if (max_pixclk > 384000)
f8437dd1 6532 return 576000;
760e1477 6533 else if (max_pixclk > 288000)
f8437dd1 6534 return 384000;
760e1477 6535 else if (max_pixclk > 144000)
f8437dd1
VK
6536 return 288000;
6537 else
6538 return 144000;
6539}
6540
e8788cbc 6541/* Compute the max pixel clock for new configuration. */
a821fc46
ACO
6542static int intel_mode_max_pixclk(struct drm_device *dev,
6543 struct drm_atomic_state *state)
30a970c6 6544{
565602d7 6545 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
fac5e23e 6546 struct drm_i915_private *dev_priv = to_i915(dev);
565602d7
ML
6547 struct drm_crtc *crtc;
6548 struct drm_crtc_state *crtc_state;
6549 unsigned max_pixclk = 0, i;
6550 enum pipe pipe;
30a970c6 6551
565602d7
ML
6552 memcpy(intel_state->min_pixclk, dev_priv->min_pixclk,
6553 sizeof(intel_state->min_pixclk));
304603f4 6554
565602d7
ML
6555 for_each_crtc_in_state(state, crtc, crtc_state, i) {
6556 int pixclk = 0;
6557
6558 if (crtc_state->enable)
6559 pixclk = crtc_state->adjusted_mode.crtc_clock;
304603f4 6560
565602d7 6561 intel_state->min_pixclk[i] = pixclk;
30a970c6
JB
6562 }
6563
565602d7
ML
6564 for_each_pipe(dev_priv, pipe)
6565 max_pixclk = max(intel_state->min_pixclk[pipe], max_pixclk);
6566
30a970c6
JB
6567 return max_pixclk;
6568}
6569
27c329ed 6570static int valleyview_modeset_calc_cdclk(struct drm_atomic_state *state)
30a970c6 6571{
27c329ed 6572 struct drm_device *dev = state->dev;
fac5e23e 6573 struct drm_i915_private *dev_priv = to_i915(dev);
27c329ed 6574 int max_pixclk = intel_mode_max_pixclk(dev, state);
1a617b77
ML
6575 struct intel_atomic_state *intel_state =
6576 to_intel_atomic_state(state);
30a970c6 6577
1a617b77 6578 intel_state->cdclk = intel_state->dev_cdclk =
27c329ed 6579 valleyview_calc_cdclk(dev_priv, max_pixclk);
0a9ab303 6580
1a617b77
ML
6581 if (!intel_state->active_crtcs)
6582 intel_state->dev_cdclk = valleyview_calc_cdclk(dev_priv, 0);
6583
27c329ed
ML
6584 return 0;
6585}
304603f4 6586
324513c0 6587static int bxt_modeset_calc_cdclk(struct drm_atomic_state *state)
27c329ed 6588{
4e5ca60f 6589 int max_pixclk = ilk_max_pixel_rate(state);
1a617b77
ML
6590 struct intel_atomic_state *intel_state =
6591 to_intel_atomic_state(state);
85a96e7a 6592
1a617b77 6593 intel_state->cdclk = intel_state->dev_cdclk =
324513c0 6594 bxt_calc_cdclk(max_pixclk);
85a96e7a 6595
1a617b77 6596 if (!intel_state->active_crtcs)
324513c0 6597 intel_state->dev_cdclk = bxt_calc_cdclk(0);
1a617b77 6598
27c329ed 6599 return 0;
30a970c6
JB
6600}
6601
1e69cd74
VS
6602static void vlv_program_pfi_credits(struct drm_i915_private *dev_priv)
6603{
6604 unsigned int credits, default_credits;
6605
6606 if (IS_CHERRYVIEW(dev_priv))
6607 default_credits = PFI_CREDIT(12);
6608 else
6609 default_credits = PFI_CREDIT(8);
6610
bfa7df01 6611 if (dev_priv->cdclk_freq >= dev_priv->czclk_freq) {
1e69cd74
VS
6612 /* CHV suggested value is 31 or 63 */
6613 if (IS_CHERRYVIEW(dev_priv))
fcc0008f 6614 credits = PFI_CREDIT_63;
1e69cd74
VS
6615 else
6616 credits = PFI_CREDIT(15);
6617 } else {
6618 credits = default_credits;
6619 }
6620
6621 /*
6622 * WA - write default credits before re-programming
6623 * FIXME: should we also set the resend bit here?
6624 */
6625 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
6626 default_credits);
6627
6628 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
6629 credits | PFI_CREDIT_RESEND);
6630
6631 /*
6632 * FIXME is this guaranteed to clear
6633 * immediately or should we poll for it?
6634 */
6635 WARN_ON(I915_READ(GCI_CONTROL) & PFI_CREDIT_RESEND);
6636}
6637
27c329ed 6638static void valleyview_modeset_commit_cdclk(struct drm_atomic_state *old_state)
30a970c6 6639{
a821fc46 6640 struct drm_device *dev = old_state->dev;
fac5e23e 6641 struct drm_i915_private *dev_priv = to_i915(dev);
1a617b77
ML
6642 struct intel_atomic_state *old_intel_state =
6643 to_intel_atomic_state(old_state);
6644 unsigned req_cdclk = old_intel_state->dev_cdclk;
30a970c6 6645
27c329ed
ML
6646 /*
6647 * FIXME: We can end up here with all power domains off, yet
6648 * with a CDCLK frequency other than the minimum. To account
6649 * for this take the PIPE-A power domain, which covers the HW
6650 * blocks needed for the following programming. This can be
6651 * removed once it's guaranteed that we get here either with
6652 * the minimum CDCLK set, or the required power domains
6653 * enabled.
6654 */
6655 intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
738c05c0 6656
27c329ed
ML
6657 if (IS_CHERRYVIEW(dev))
6658 cherryview_set_cdclk(dev, req_cdclk);
6659 else
6660 valleyview_set_cdclk(dev, req_cdclk);
738c05c0 6661
27c329ed 6662 vlv_program_pfi_credits(dev_priv);
1e69cd74 6663
27c329ed 6664 intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
30a970c6
JB
6665}
6666
4a806558
ML
6667static void valleyview_crtc_enable(struct intel_crtc_state *pipe_config,
6668 struct drm_atomic_state *old_state)
89b667f8 6669{
4a806558 6670 struct drm_crtc *crtc = pipe_config->base.crtc;
89b667f8 6671 struct drm_device *dev = crtc->dev;
a72e4c9f 6672 struct drm_i915_private *dev_priv = to_i915(dev);
89b667f8 6673 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
89b667f8 6674 int pipe = intel_crtc->pipe;
89b667f8 6675
53d9f4e9 6676 if (WARN_ON(intel_crtc->active))
89b667f8
JB
6677 return;
6678
37a5650b 6679 if (intel_crtc_has_dp_encoder(intel_crtc->config))
fe3cd48d 6680 intel_dp_set_m_n(intel_crtc, M1_N1);
5b18e57c
DV
6681
6682 intel_set_pipe_timings(intel_crtc);
bc58be60 6683 intel_set_pipe_src_size(intel_crtc);
5b18e57c 6684
c14b0485 6685 if (IS_CHERRYVIEW(dev) && pipe == PIPE_B) {
fac5e23e 6686 struct drm_i915_private *dev_priv = to_i915(dev);
c14b0485
VS
6687
6688 I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
6689 I915_WRITE(CHV_CANVAS(pipe), 0);
6690 }
6691
5b18e57c
DV
6692 i9xx_set_pipeconf(intel_crtc);
6693
89b667f8 6694 intel_crtc->active = true;
89b667f8 6695
a72e4c9f 6696 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4a3436e8 6697
fd6bbda9 6698 intel_encoders_pre_pll_enable(crtc, pipe_config, old_state);
89b667f8 6699
cd2d34d9
VS
6700 if (IS_CHERRYVIEW(dev)) {
6701 chv_prepare_pll(intel_crtc, intel_crtc->config);
6702 chv_enable_pll(intel_crtc, intel_crtc->config);
6703 } else {
6704 vlv_prepare_pll(intel_crtc, intel_crtc->config);
6705 vlv_enable_pll(intel_crtc, intel_crtc->config);
9d556c99 6706 }
89b667f8 6707
fd6bbda9 6708 intel_encoders_pre_enable(crtc, pipe_config, old_state);
89b667f8 6709
2dd24552
JB
6710 i9xx_pfit_enable(intel_crtc);
6711
b95c5321 6712 intel_color_load_luts(&pipe_config->base);
63cbb074 6713
caed361d 6714 intel_update_watermarks(crtc);
e1fdc473 6715 intel_enable_pipe(intel_crtc);
be6a6f8e 6716
4b3a9526
VS
6717 assert_vblank_disabled(crtc);
6718 drm_crtc_vblank_on(crtc);
6719
fd6bbda9 6720 intel_encoders_enable(crtc, pipe_config, old_state);
89b667f8
JB
6721}
6722
f13c2ef3
DV
6723static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
6724{
6725 struct drm_device *dev = crtc->base.dev;
fac5e23e 6726 struct drm_i915_private *dev_priv = to_i915(dev);
f13c2ef3 6727
6e3c9717
ACO
6728 I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
6729 I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
f13c2ef3
DV
6730}
6731
4a806558
ML
6732static void i9xx_crtc_enable(struct intel_crtc_state *pipe_config,
6733 struct drm_atomic_state *old_state)
79e53945 6734{
4a806558 6735 struct drm_crtc *crtc = pipe_config->base.crtc;
79e53945 6736 struct drm_device *dev = crtc->dev;
a72e4c9f 6737 struct drm_i915_private *dev_priv = to_i915(dev);
79e53945 6738 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
cd2d34d9 6739 enum pipe pipe = intel_crtc->pipe;
79e53945 6740
53d9f4e9 6741 if (WARN_ON(intel_crtc->active))
f7abfe8b
CW
6742 return;
6743
f13c2ef3
DV
6744 i9xx_set_pll_dividers(intel_crtc);
6745
37a5650b 6746 if (intel_crtc_has_dp_encoder(intel_crtc->config))
fe3cd48d 6747 intel_dp_set_m_n(intel_crtc, M1_N1);
5b18e57c
DV
6748
6749 intel_set_pipe_timings(intel_crtc);
bc58be60 6750 intel_set_pipe_src_size(intel_crtc);
5b18e57c 6751
5b18e57c
DV
6752 i9xx_set_pipeconf(intel_crtc);
6753
f7abfe8b 6754 intel_crtc->active = true;
6b383a7f 6755
4a3436e8 6756 if (!IS_GEN2(dev))
a72e4c9f 6757 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4a3436e8 6758
fd6bbda9 6759 intel_encoders_pre_enable(crtc, pipe_config, old_state);
9d6d9f19 6760
f6736a1a
DV
6761 i9xx_enable_pll(intel_crtc);
6762
2dd24552
JB
6763 i9xx_pfit_enable(intel_crtc);
6764
b95c5321 6765 intel_color_load_luts(&pipe_config->base);
63cbb074 6766
f37fcc2a 6767 intel_update_watermarks(crtc);
e1fdc473 6768 intel_enable_pipe(intel_crtc);
be6a6f8e 6769
4b3a9526
VS
6770 assert_vblank_disabled(crtc);
6771 drm_crtc_vblank_on(crtc);
6772
fd6bbda9 6773 intel_encoders_enable(crtc, pipe_config, old_state);
0b8765c6 6774}
79e53945 6775
87476d63
DV
6776static void i9xx_pfit_disable(struct intel_crtc *crtc)
6777{
6778 struct drm_device *dev = crtc->base.dev;
fac5e23e 6779 struct drm_i915_private *dev_priv = to_i915(dev);
87476d63 6780
6e3c9717 6781 if (!crtc->config->gmch_pfit.control)
328d8e82 6782 return;
87476d63 6783
328d8e82 6784 assert_pipe_disabled(dev_priv, crtc->pipe);
87476d63 6785
328d8e82
DV
6786 DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
6787 I915_READ(PFIT_CONTROL));
6788 I915_WRITE(PFIT_CONTROL, 0);
87476d63
DV
6789}
6790
4a806558
ML
6791static void i9xx_crtc_disable(struct intel_crtc_state *old_crtc_state,
6792 struct drm_atomic_state *old_state)
0b8765c6 6793{
4a806558 6794 struct drm_crtc *crtc = old_crtc_state->base.crtc;
0b8765c6 6795 struct drm_device *dev = crtc->dev;
fac5e23e 6796 struct drm_i915_private *dev_priv = to_i915(dev);
0b8765c6
JB
6797 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6798 int pipe = intel_crtc->pipe;
ef9c3aee 6799
6304cd91
VS
6800 /*
6801 * On gen2 planes are double buffered but the pipe isn't, so we must
6802 * wait for planes to fully turn off before disabling the pipe.
6803 */
90e83e53
ACO
6804 if (IS_GEN2(dev))
6805 intel_wait_for_vblank(dev, pipe);
6304cd91 6806
fd6bbda9 6807 intel_encoders_disable(crtc, old_crtc_state, old_state);
4b3a9526 6808
f9b61ff6
DV
6809 drm_crtc_vblank_off(crtc);
6810 assert_vblank_disabled(crtc);
6811
575f7ab7 6812 intel_disable_pipe(intel_crtc);
24a1f16d 6813
87476d63 6814 i9xx_pfit_disable(intel_crtc);
24a1f16d 6815
fd6bbda9 6816 intel_encoders_post_disable(crtc, old_crtc_state, old_state);
89b667f8 6817
d7edc4e5 6818 if (!intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_DSI)) {
076ed3b2
CML
6819 if (IS_CHERRYVIEW(dev))
6820 chv_disable_pll(dev_priv, pipe);
6821 else if (IS_VALLEYVIEW(dev))
6822 vlv_disable_pll(dev_priv, pipe);
6823 else
1c4e0274 6824 i9xx_disable_pll(intel_crtc);
076ed3b2 6825 }
0b8765c6 6826
fd6bbda9 6827 intel_encoders_post_pll_disable(crtc, old_crtc_state, old_state);
d6db995f 6828
4a3436e8 6829 if (!IS_GEN2(dev))
a72e4c9f 6830 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
0b8765c6
JB
6831}
6832
b17d48e2
ML
6833static void intel_crtc_disable_noatomic(struct drm_crtc *crtc)
6834{
842e0307 6835 struct intel_encoder *encoder;
b17d48e2
ML
6836 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6837 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
6838 enum intel_display_power_domain domain;
6839 unsigned long domains;
4a806558
ML
6840 struct drm_atomic_state *state;
6841 struct intel_crtc_state *crtc_state;
6842 int ret;
b17d48e2
ML
6843
6844 if (!intel_crtc->active)
6845 return;
6846
936e71e3 6847 if (to_intel_plane_state(crtc->primary->state)->base.visible) {
5a21b665 6848 WARN_ON(intel_crtc->flip_work);
fc32b1fd 6849
2622a081 6850 intel_pre_disable_primary_noatomic(crtc);
54a41961
ML
6851
6852 intel_crtc_disable_planes(crtc, 1 << drm_plane_index(crtc->primary));
936e71e3 6853 to_intel_plane_state(crtc->primary->state)->base.visible = false;
a539205a
ML
6854 }
6855
4a806558
ML
6856 state = drm_atomic_state_alloc(crtc->dev);
6857 state->acquire_ctx = crtc->dev->mode_config.acquire_ctx;
6858
6859 /* Everything's already locked, -EDEADLK can't happen. */
6860 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
6861 ret = drm_atomic_add_affected_connectors(state, crtc);
6862
6863 WARN_ON(IS_ERR(crtc_state) || ret);
6864
6865 dev_priv->display.crtc_disable(crtc_state, state);
6866
6867 drm_atomic_state_free(state);
842e0307 6868
78108b7c
VS
6869 DRM_DEBUG_KMS("[CRTC:%d:%s] hw state adjusted, was enabled, now disabled\n",
6870 crtc->base.id, crtc->name);
842e0307
ML
6871
6872 WARN_ON(drm_atomic_set_mode_for_crtc(crtc->state, NULL) < 0);
6873 crtc->state->active = false;
37d9078b 6874 intel_crtc->active = false;
842e0307
ML
6875 crtc->enabled = false;
6876 crtc->state->connector_mask = 0;
6877 crtc->state->encoder_mask = 0;
6878
6879 for_each_encoder_on_crtc(crtc->dev, crtc, encoder)
6880 encoder->base.crtc = NULL;
6881
58f9c0bc 6882 intel_fbc_disable(intel_crtc);
37d9078b 6883 intel_update_watermarks(crtc);
1f7457b1 6884 intel_disable_shared_dpll(intel_crtc);
b17d48e2
ML
6885
6886 domains = intel_crtc->enabled_power_domains;
6887 for_each_power_domain(domain, domains)
6888 intel_display_power_put(dev_priv, domain);
6889 intel_crtc->enabled_power_domains = 0;
565602d7
ML
6890
6891 dev_priv->active_crtcs &= ~(1 << intel_crtc->pipe);
6892 dev_priv->min_pixclk[intel_crtc->pipe] = 0;
b17d48e2
ML
6893}
6894
6b72d486
ML
6895/*
6896 * turn all crtc's off, but do not adjust state
6897 * This has to be paired with a call to intel_modeset_setup_hw_state.
6898 */
70e0bd74 6899int intel_display_suspend(struct drm_device *dev)
ee7b9f93 6900{
e2c8b870 6901 struct drm_i915_private *dev_priv = to_i915(dev);
70e0bd74 6902 struct drm_atomic_state *state;
e2c8b870 6903 int ret;
70e0bd74 6904
e2c8b870
ML
6905 state = drm_atomic_helper_suspend(dev);
6906 ret = PTR_ERR_OR_ZERO(state);
70e0bd74
ML
6907 if (ret)
6908 DRM_ERROR("Suspending crtc's failed with %i\n", ret);
e2c8b870
ML
6909 else
6910 dev_priv->modeset_restore_state = state;
70e0bd74 6911 return ret;
ee7b9f93
JB
6912}
6913
ea5b213a 6914void intel_encoder_destroy(struct drm_encoder *encoder)
7e7d76c3 6915{
4ef69c7a 6916 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
ea5b213a 6917
ea5b213a
CW
6918 drm_encoder_cleanup(encoder);
6919 kfree(intel_encoder);
7e7d76c3
JB
6920}
6921
0a91ca29
DV
6922/* Cross check the actual hw state with our own modeset state tracking (and it's
6923 * internal consistency). */
5a21b665 6924static void intel_connector_verify_state(struct intel_connector *connector)
79e53945 6925{
5a21b665 6926 struct drm_crtc *crtc = connector->base.state->crtc;
35dd3c64
ML
6927
6928 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
6929 connector->base.base.id,
6930 connector->base.name);
6931
0a91ca29 6932 if (connector->get_hw_state(connector)) {
e85376cb 6933 struct intel_encoder *encoder = connector->encoder;
5a21b665 6934 struct drm_connector_state *conn_state = connector->base.state;
0a91ca29 6935
35dd3c64
ML
6936 I915_STATE_WARN(!crtc,
6937 "connector enabled without attached crtc\n");
0a91ca29 6938
35dd3c64
ML
6939 if (!crtc)
6940 return;
6941
6942 I915_STATE_WARN(!crtc->state->active,
6943 "connector is active, but attached crtc isn't\n");
6944
e85376cb 6945 if (!encoder || encoder->type == INTEL_OUTPUT_DP_MST)
35dd3c64
ML
6946 return;
6947
e85376cb 6948 I915_STATE_WARN(conn_state->best_encoder != &encoder->base,
35dd3c64
ML
6949 "atomic encoder doesn't match attached encoder\n");
6950
e85376cb 6951 I915_STATE_WARN(conn_state->crtc != encoder->base.crtc,
35dd3c64
ML
6952 "attached encoder crtc differs from connector crtc\n");
6953 } else {
4d688a2a
ML
6954 I915_STATE_WARN(crtc && crtc->state->active,
6955 "attached crtc is active, but connector isn't\n");
5a21b665 6956 I915_STATE_WARN(!crtc && connector->base.state->best_encoder,
35dd3c64 6957 "best encoder set without crtc!\n");
0a91ca29 6958 }
79e53945
JB
6959}
6960
08d9bc92
ACO
6961int intel_connector_init(struct intel_connector *connector)
6962{
5350a031 6963 drm_atomic_helper_connector_reset(&connector->base);
08d9bc92 6964
5350a031 6965 if (!connector->base.state)
08d9bc92
ACO
6966 return -ENOMEM;
6967
08d9bc92
ACO
6968 return 0;
6969}
6970
6971struct intel_connector *intel_connector_alloc(void)
6972{
6973 struct intel_connector *connector;
6974
6975 connector = kzalloc(sizeof *connector, GFP_KERNEL);
6976 if (!connector)
6977 return NULL;
6978
6979 if (intel_connector_init(connector) < 0) {
6980 kfree(connector);
6981 return NULL;
6982 }
6983
6984 return connector;
6985}
6986
f0947c37
DV
6987/* Simple connector->get_hw_state implementation for encoders that support only
6988 * one connector and no cloning and hence the encoder state determines the state
6989 * of the connector. */
6990bool intel_connector_get_hw_state(struct intel_connector *connector)
ea5b213a 6991{
24929352 6992 enum pipe pipe = 0;
f0947c37 6993 struct intel_encoder *encoder = connector->encoder;
ea5b213a 6994
f0947c37 6995 return encoder->get_hw_state(encoder, &pipe);
ea5b213a
CW
6996}
6997
6d293983 6998static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
d272ddfa 6999{
6d293983
ACO
7000 if (crtc_state->base.enable && crtc_state->has_pch_encoder)
7001 return crtc_state->fdi_lanes;
d272ddfa
VS
7002
7003 return 0;
7004}
7005
6d293983 7006static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
5cec258b 7007 struct intel_crtc_state *pipe_config)
1857e1da 7008{
6d293983
ACO
7009 struct drm_atomic_state *state = pipe_config->base.state;
7010 struct intel_crtc *other_crtc;
7011 struct intel_crtc_state *other_crtc_state;
7012
1857e1da
DV
7013 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
7014 pipe_name(pipe), pipe_config->fdi_lanes);
7015 if (pipe_config->fdi_lanes > 4) {
7016 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
7017 pipe_name(pipe), pipe_config->fdi_lanes);
6d293983 7018 return -EINVAL;
1857e1da
DV
7019 }
7020
bafb6553 7021 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
1857e1da
DV
7022 if (pipe_config->fdi_lanes > 2) {
7023 DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
7024 pipe_config->fdi_lanes);
6d293983 7025 return -EINVAL;
1857e1da 7026 } else {
6d293983 7027 return 0;
1857e1da
DV
7028 }
7029 }
7030
7031 if (INTEL_INFO(dev)->num_pipes == 2)
6d293983 7032 return 0;
1857e1da
DV
7033
7034 /* Ivybridge 3 pipe is really complicated */
7035 switch (pipe) {
7036 case PIPE_A:
6d293983 7037 return 0;
1857e1da 7038 case PIPE_B:
6d293983
ACO
7039 if (pipe_config->fdi_lanes <= 2)
7040 return 0;
7041
7042 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_C));
7043 other_crtc_state =
7044 intel_atomic_get_crtc_state(state, other_crtc);
7045 if (IS_ERR(other_crtc_state))
7046 return PTR_ERR(other_crtc_state);
7047
7048 if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
1857e1da
DV
7049 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
7050 pipe_name(pipe), pipe_config->fdi_lanes);
6d293983 7051 return -EINVAL;
1857e1da 7052 }
6d293983 7053 return 0;
1857e1da 7054 case PIPE_C:
251cc67c
VS
7055 if (pipe_config->fdi_lanes > 2) {
7056 DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
7057 pipe_name(pipe), pipe_config->fdi_lanes);
6d293983 7058 return -EINVAL;
251cc67c 7059 }
6d293983
ACO
7060
7061 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_B));
7062 other_crtc_state =
7063 intel_atomic_get_crtc_state(state, other_crtc);
7064 if (IS_ERR(other_crtc_state))
7065 return PTR_ERR(other_crtc_state);
7066
7067 if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
1857e1da 7068 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
6d293983 7069 return -EINVAL;
1857e1da 7070 }
6d293983 7071 return 0;
1857e1da
DV
7072 default:
7073 BUG();
7074 }
7075}
7076
e29c22c0
DV
7077#define RETRY 1
7078static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
5cec258b 7079 struct intel_crtc_state *pipe_config)
877d48d5 7080{
1857e1da 7081 struct drm_device *dev = intel_crtc->base.dev;
7c5f93b0 7082 const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
6d293983
ACO
7083 int lane, link_bw, fdi_dotclock, ret;
7084 bool needs_recompute = false;
877d48d5 7085
e29c22c0 7086retry:
877d48d5
DV
7087 /* FDI is a binary signal running at ~2.7GHz, encoding
7088 * each output octet as 10 bits. The actual frequency
7089 * is stored as a divider into a 100MHz clock, and the
7090 * mode pixel clock is stored in units of 1KHz.
7091 * Hence the bw of each lane in terms of the mode signal
7092 * is:
7093 */
21a727b3 7094 link_bw = intel_fdi_link_freq(to_i915(dev), pipe_config);
877d48d5 7095
241bfc38 7096 fdi_dotclock = adjusted_mode->crtc_clock;
877d48d5 7097
2bd89a07 7098 lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
877d48d5
DV
7099 pipe_config->pipe_bpp);
7100
7101 pipe_config->fdi_lanes = lane;
7102
2bd89a07 7103 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
877d48d5 7104 link_bw, &pipe_config->fdi_m_n);
1857e1da 7105
e3b247da 7106 ret = ironlake_check_fdi_lanes(dev, intel_crtc->pipe, pipe_config);
6d293983 7107 if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
e29c22c0
DV
7108 pipe_config->pipe_bpp -= 2*3;
7109 DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
7110 pipe_config->pipe_bpp);
7111 needs_recompute = true;
7112 pipe_config->bw_constrained = true;
7113
7114 goto retry;
7115 }
7116
7117 if (needs_recompute)
7118 return RETRY;
7119
6d293983 7120 return ret;
877d48d5
DV
7121}
7122
8cfb3407
VS
7123static bool pipe_config_supports_ips(struct drm_i915_private *dev_priv,
7124 struct intel_crtc_state *pipe_config)
7125{
7126 if (pipe_config->pipe_bpp > 24)
7127 return false;
7128
7129 /* HSW can handle pixel rate up to cdclk? */
2d1fe073 7130 if (IS_HASWELL(dev_priv))
8cfb3407
VS
7131 return true;
7132
7133 /*
b432e5cf
VS
7134 * We compare against max which means we must take
7135 * the increased cdclk requirement into account when
7136 * calculating the new cdclk.
7137 *
7138 * Should measure whether using a lower cdclk w/o IPS
8cfb3407
VS
7139 */
7140 return ilk_pipe_pixel_rate(pipe_config) <=
7141 dev_priv->max_cdclk_freq * 95 / 100;
7142}
7143
42db64ef 7144static void hsw_compute_ips_config(struct intel_crtc *crtc,
5cec258b 7145 struct intel_crtc_state *pipe_config)
42db64ef 7146{
8cfb3407 7147 struct drm_device *dev = crtc->base.dev;
fac5e23e 7148 struct drm_i915_private *dev_priv = to_i915(dev);
8cfb3407 7149
d330a953 7150 pipe_config->ips_enabled = i915.enable_ips &&
8cfb3407
VS
7151 hsw_crtc_supports_ips(crtc) &&
7152 pipe_config_supports_ips(dev_priv, pipe_config);
42db64ef
PZ
7153}
7154
39acb4aa
VS
7155static bool intel_crtc_supports_double_wide(const struct intel_crtc *crtc)
7156{
7157 const struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
7158
7159 /* GDG double wide on either pipe, otherwise pipe A only */
7160 return INTEL_INFO(dev_priv)->gen < 4 &&
7161 (crtc->pipe == PIPE_A || IS_I915G(dev_priv));
7162}
7163
a43f6e0f 7164static int intel_crtc_compute_config(struct intel_crtc *crtc,
5cec258b 7165 struct intel_crtc_state *pipe_config)
79e53945 7166{
a43f6e0f 7167 struct drm_device *dev = crtc->base.dev;
fac5e23e 7168 struct drm_i915_private *dev_priv = to_i915(dev);
7c5f93b0 7169 const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
f3261156 7170 int clock_limit = dev_priv->max_dotclk_freq;
89749350 7171
cf532bb2 7172 if (INTEL_INFO(dev)->gen < 4) {
f3261156 7173 clock_limit = dev_priv->max_cdclk_freq * 9 / 10;
cf532bb2
VS
7174
7175 /*
39acb4aa 7176 * Enable double wide mode when the dot clock
cf532bb2 7177 * is > 90% of the (display) core speed.
cf532bb2 7178 */
39acb4aa
VS
7179 if (intel_crtc_supports_double_wide(crtc) &&
7180 adjusted_mode->crtc_clock > clock_limit) {
f3261156 7181 clock_limit = dev_priv->max_dotclk_freq;
cf532bb2 7182 pipe_config->double_wide = true;
ad3a4479 7183 }
f3261156 7184 }
ad3a4479 7185
f3261156
VS
7186 if (adjusted_mode->crtc_clock > clock_limit) {
7187 DRM_DEBUG_KMS("requested pixel clock (%d kHz) too high (max: %d kHz, double wide: %s)\n",
7188 adjusted_mode->crtc_clock, clock_limit,
7189 yesno(pipe_config->double_wide));
7190 return -EINVAL;
2c07245f 7191 }
89749350 7192
1d1d0e27
VS
7193 /*
7194 * Pipe horizontal size must be even in:
7195 * - DVO ganged mode
7196 * - LVDS dual channel mode
7197 * - Double wide pipe
7198 */
2d84d2b3 7199 if ((intel_crtc_has_type(pipe_config, INTEL_OUTPUT_LVDS) &&
1d1d0e27
VS
7200 intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
7201 pipe_config->pipe_src_w &= ~1;
7202
8693a824
DL
7203 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
7204 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
44f46b42
CW
7205 */
7206 if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
aad941d5 7207 adjusted_mode->crtc_hsync_start == adjusted_mode->crtc_hdisplay)
e29c22c0 7208 return -EINVAL;
44f46b42 7209
f5adf94e 7210 if (HAS_IPS(dev))
a43f6e0f
DV
7211 hsw_compute_ips_config(crtc, pipe_config);
7212
877d48d5 7213 if (pipe_config->has_pch_encoder)
a43f6e0f 7214 return ironlake_fdi_compute_config(crtc, pipe_config);
877d48d5 7215
cf5a15be 7216 return 0;
79e53945
JB
7217}
7218
1652d19e
VS
7219static int skylake_get_display_clock_speed(struct drm_device *dev)
7220{
7221 struct drm_i915_private *dev_priv = to_i915(dev);
ea61791e 7222 uint32_t cdctl;
1652d19e 7223
ea61791e 7224 skl_dpll0_update(dev_priv);
1652d19e 7225
63911d72 7226 if (dev_priv->cdclk_pll.vco == 0)
709e05c3 7227 return dev_priv->cdclk_pll.ref;
1652d19e 7228
ea61791e 7229 cdctl = I915_READ(CDCLK_CTL);
1652d19e 7230
63911d72 7231 if (dev_priv->cdclk_pll.vco == 8640000) {
1652d19e
VS
7232 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
7233 case CDCLK_FREQ_450_432:
7234 return 432000;
7235 case CDCLK_FREQ_337_308:
487ed2e4 7236 return 308571;
ea61791e
VS
7237 case CDCLK_FREQ_540:
7238 return 540000;
1652d19e 7239 case CDCLK_FREQ_675_617:
487ed2e4 7240 return 617143;
1652d19e 7241 default:
ea61791e 7242 MISSING_CASE(cdctl & CDCLK_FREQ_SEL_MASK);
1652d19e
VS
7243 }
7244 } else {
1652d19e
VS
7245 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
7246 case CDCLK_FREQ_450_432:
7247 return 450000;
7248 case CDCLK_FREQ_337_308:
7249 return 337500;
ea61791e
VS
7250 case CDCLK_FREQ_540:
7251 return 540000;
1652d19e
VS
7252 case CDCLK_FREQ_675_617:
7253 return 675000;
7254 default:
ea61791e 7255 MISSING_CASE(cdctl & CDCLK_FREQ_SEL_MASK);
1652d19e
VS
7256 }
7257 }
7258
709e05c3 7259 return dev_priv->cdclk_pll.ref;
1652d19e
VS
7260}
7261
83d7c81f
VS
7262static void bxt_de_pll_update(struct drm_i915_private *dev_priv)
7263{
7264 u32 val;
7265
7266 dev_priv->cdclk_pll.ref = 19200;
1c3f7700 7267 dev_priv->cdclk_pll.vco = 0;
83d7c81f
VS
7268
7269 val = I915_READ(BXT_DE_PLL_ENABLE);
1c3f7700 7270 if ((val & BXT_DE_PLL_PLL_ENABLE) == 0)
83d7c81f 7271 return;
83d7c81f 7272
1c3f7700
ID
7273 if (WARN_ON((val & BXT_DE_PLL_LOCK) == 0))
7274 return;
83d7c81f
VS
7275
7276 val = I915_READ(BXT_DE_PLL_CTL);
7277 dev_priv->cdclk_pll.vco = (val & BXT_DE_PLL_RATIO_MASK) *
7278 dev_priv->cdclk_pll.ref;
7279}
7280
acd3f3d3
BP
7281static int broxton_get_display_clock_speed(struct drm_device *dev)
7282{
7283 struct drm_i915_private *dev_priv = to_i915(dev);
f5986242
VS
7284 u32 divider;
7285 int div, vco;
acd3f3d3 7286
83d7c81f
VS
7287 bxt_de_pll_update(dev_priv);
7288
f5986242
VS
7289 vco = dev_priv->cdclk_pll.vco;
7290 if (vco == 0)
7291 return dev_priv->cdclk_pll.ref;
acd3f3d3 7292
f5986242 7293 divider = I915_READ(CDCLK_CTL) & BXT_CDCLK_CD2X_DIV_SEL_MASK;
acd3f3d3 7294
f5986242 7295 switch (divider) {
acd3f3d3 7296 case BXT_CDCLK_CD2X_DIV_SEL_1:
f5986242
VS
7297 div = 2;
7298 break;
acd3f3d3 7299 case BXT_CDCLK_CD2X_DIV_SEL_1_5:
f5986242
VS
7300 div = 3;
7301 break;
acd3f3d3 7302 case BXT_CDCLK_CD2X_DIV_SEL_2:
f5986242
VS
7303 div = 4;
7304 break;
acd3f3d3 7305 case BXT_CDCLK_CD2X_DIV_SEL_4:
f5986242
VS
7306 div = 8;
7307 break;
7308 default:
7309 MISSING_CASE(divider);
7310 return dev_priv->cdclk_pll.ref;
acd3f3d3
BP
7311 }
7312
f5986242 7313 return DIV_ROUND_CLOSEST(vco, div);
acd3f3d3
BP
7314}
7315
1652d19e
VS
7316static int broadwell_get_display_clock_speed(struct drm_device *dev)
7317{
fac5e23e 7318 struct drm_i915_private *dev_priv = to_i915(dev);
1652d19e
VS
7319 uint32_t lcpll = I915_READ(LCPLL_CTL);
7320 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
7321
7322 if (lcpll & LCPLL_CD_SOURCE_FCLK)
7323 return 800000;
7324 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
7325 return 450000;
7326 else if (freq == LCPLL_CLK_FREQ_450)
7327 return 450000;
7328 else if (freq == LCPLL_CLK_FREQ_54O_BDW)
7329 return 540000;
7330 else if (freq == LCPLL_CLK_FREQ_337_5_BDW)
7331 return 337500;
7332 else
7333 return 675000;
7334}
7335
7336static int haswell_get_display_clock_speed(struct drm_device *dev)
7337{
fac5e23e 7338 struct drm_i915_private *dev_priv = to_i915(dev);
1652d19e
VS
7339 uint32_t lcpll = I915_READ(LCPLL_CTL);
7340 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
7341
7342 if (lcpll & LCPLL_CD_SOURCE_FCLK)
7343 return 800000;
7344 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
7345 return 450000;
7346 else if (freq == LCPLL_CLK_FREQ_450)
7347 return 450000;
7348 else if (IS_HSW_ULT(dev))
7349 return 337500;
7350 else
7351 return 540000;
79e53945
JB
7352}
7353
25eb05fc
JB
7354static int valleyview_get_display_clock_speed(struct drm_device *dev)
7355{
bfa7df01
VS
7356 return vlv_get_cck_clock_hpll(to_i915(dev), "cdclk",
7357 CCK_DISPLAY_CLOCK_CONTROL);
25eb05fc
JB
7358}
7359
b37a6434
VS
7360static int ilk_get_display_clock_speed(struct drm_device *dev)
7361{
7362 return 450000;
7363}
7364
e70236a8
JB
7365static int i945_get_display_clock_speed(struct drm_device *dev)
7366{
7367 return 400000;
7368}
79e53945 7369
e70236a8 7370static int i915_get_display_clock_speed(struct drm_device *dev)
79e53945 7371{
e907f170 7372 return 333333;
e70236a8 7373}
79e53945 7374
e70236a8
JB
7375static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
7376{
7377 return 200000;
7378}
79e53945 7379
257a7ffc
DV
7380static int pnv_get_display_clock_speed(struct drm_device *dev)
7381{
52a05c30 7382 struct pci_dev *pdev = dev->pdev;
257a7ffc
DV
7383 u16 gcfgc = 0;
7384
52a05c30 7385 pci_read_config_word(pdev, GCFGC, &gcfgc);
257a7ffc
DV
7386
7387 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
7388 case GC_DISPLAY_CLOCK_267_MHZ_PNV:
e907f170 7389 return 266667;
257a7ffc 7390 case GC_DISPLAY_CLOCK_333_MHZ_PNV:
e907f170 7391 return 333333;
257a7ffc 7392 case GC_DISPLAY_CLOCK_444_MHZ_PNV:
e907f170 7393 return 444444;
257a7ffc
DV
7394 case GC_DISPLAY_CLOCK_200_MHZ_PNV:
7395 return 200000;
7396 default:
7397 DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
7398 case GC_DISPLAY_CLOCK_133_MHZ_PNV:
e907f170 7399 return 133333;
257a7ffc 7400 case GC_DISPLAY_CLOCK_167_MHZ_PNV:
e907f170 7401 return 166667;
257a7ffc
DV
7402 }
7403}
7404
e70236a8
JB
7405static int i915gm_get_display_clock_speed(struct drm_device *dev)
7406{
52a05c30 7407 struct pci_dev *pdev = dev->pdev;
e70236a8 7408 u16 gcfgc = 0;
79e53945 7409
52a05c30 7410 pci_read_config_word(pdev, GCFGC, &gcfgc);
e70236a8
JB
7411
7412 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
e907f170 7413 return 133333;
e70236a8
JB
7414 else {
7415 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
7416 case GC_DISPLAY_CLOCK_333_MHZ:
e907f170 7417 return 333333;
e70236a8
JB
7418 default:
7419 case GC_DISPLAY_CLOCK_190_200_MHZ:
7420 return 190000;
79e53945 7421 }
e70236a8
JB
7422 }
7423}
7424
7425static int i865_get_display_clock_speed(struct drm_device *dev)
7426{
e907f170 7427 return 266667;
e70236a8
JB
7428}
7429
1b1d2716 7430static int i85x_get_display_clock_speed(struct drm_device *dev)
e70236a8 7431{
52a05c30 7432 struct pci_dev *pdev = dev->pdev;
e70236a8 7433 u16 hpllcc = 0;
1b1d2716 7434
65cd2b3f
VS
7435 /*
7436 * 852GM/852GMV only supports 133 MHz and the HPLLCC
7437 * encoding is different :(
7438 * FIXME is this the right way to detect 852GM/852GMV?
7439 */
52a05c30 7440 if (pdev->revision == 0x1)
65cd2b3f
VS
7441 return 133333;
7442
52a05c30 7443 pci_bus_read_config_word(pdev->bus,
1b1d2716
VS
7444 PCI_DEVFN(0, 3), HPLLCC, &hpllcc);
7445
e70236a8
JB
7446 /* Assume that the hardware is in the high speed state. This
7447 * should be the default.
7448 */
7449 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
7450 case GC_CLOCK_133_200:
1b1d2716 7451 case GC_CLOCK_133_200_2:
e70236a8
JB
7452 case GC_CLOCK_100_200:
7453 return 200000;
7454 case GC_CLOCK_166_250:
7455 return 250000;
7456 case GC_CLOCK_100_133:
e907f170 7457 return 133333;
1b1d2716
VS
7458 case GC_CLOCK_133_266:
7459 case GC_CLOCK_133_266_2:
7460 case GC_CLOCK_166_266:
7461 return 266667;
e70236a8 7462 }
79e53945 7463
e70236a8
JB
7464 /* Shouldn't happen */
7465 return 0;
7466}
79e53945 7467
e70236a8
JB
7468static int i830_get_display_clock_speed(struct drm_device *dev)
7469{
e907f170 7470 return 133333;
79e53945
JB
7471}
7472
34edce2f
VS
7473static unsigned int intel_hpll_vco(struct drm_device *dev)
7474{
fac5e23e 7475 struct drm_i915_private *dev_priv = to_i915(dev);
34edce2f
VS
7476 static const unsigned int blb_vco[8] = {
7477 [0] = 3200000,
7478 [1] = 4000000,
7479 [2] = 5333333,
7480 [3] = 4800000,
7481 [4] = 6400000,
7482 };
7483 static const unsigned int pnv_vco[8] = {
7484 [0] = 3200000,
7485 [1] = 4000000,
7486 [2] = 5333333,
7487 [3] = 4800000,
7488 [4] = 2666667,
7489 };
7490 static const unsigned int cl_vco[8] = {
7491 [0] = 3200000,
7492 [1] = 4000000,
7493 [2] = 5333333,
7494 [3] = 6400000,
7495 [4] = 3333333,
7496 [5] = 3566667,
7497 [6] = 4266667,
7498 };
7499 static const unsigned int elk_vco[8] = {
7500 [0] = 3200000,
7501 [1] = 4000000,
7502 [2] = 5333333,
7503 [3] = 4800000,
7504 };
7505 static const unsigned int ctg_vco[8] = {
7506 [0] = 3200000,
7507 [1] = 4000000,
7508 [2] = 5333333,
7509 [3] = 6400000,
7510 [4] = 2666667,
7511 [5] = 4266667,
7512 };
7513 const unsigned int *vco_table;
7514 unsigned int vco;
7515 uint8_t tmp = 0;
7516
7517 /* FIXME other chipsets? */
7518 if (IS_GM45(dev))
7519 vco_table = ctg_vco;
7520 else if (IS_G4X(dev))
7521 vco_table = elk_vco;
7522 else if (IS_CRESTLINE(dev))
7523 vco_table = cl_vco;
7524 else if (IS_PINEVIEW(dev))
7525 vco_table = pnv_vco;
7526 else if (IS_G33(dev))
7527 vco_table = blb_vco;
7528 else
7529 return 0;
7530
7531 tmp = I915_READ(IS_MOBILE(dev) ? HPLLVCO_MOBILE : HPLLVCO);
7532
7533 vco = vco_table[tmp & 0x7];
7534 if (vco == 0)
7535 DRM_ERROR("Bad HPLL VCO (HPLLVCO=0x%02x)\n", tmp);
7536 else
7537 DRM_DEBUG_KMS("HPLL VCO %u kHz\n", vco);
7538
7539 return vco;
7540}
7541
7542static int gm45_get_display_clock_speed(struct drm_device *dev)
7543{
52a05c30 7544 struct pci_dev *pdev = dev->pdev;
34edce2f
VS
7545 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
7546 uint16_t tmp = 0;
7547
52a05c30 7548 pci_read_config_word(pdev, GCFGC, &tmp);
34edce2f
VS
7549
7550 cdclk_sel = (tmp >> 12) & 0x1;
7551
7552 switch (vco) {
7553 case 2666667:
7554 case 4000000:
7555 case 5333333:
7556 return cdclk_sel ? 333333 : 222222;
7557 case 3200000:
7558 return cdclk_sel ? 320000 : 228571;
7559 default:
7560 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u, CFGC=0x%04x\n", vco, tmp);
7561 return 222222;
7562 }
7563}
7564
7565static int i965gm_get_display_clock_speed(struct drm_device *dev)
7566{
52a05c30 7567 struct pci_dev *pdev = dev->pdev;
34edce2f
VS
7568 static const uint8_t div_3200[] = { 16, 10, 8 };
7569 static const uint8_t div_4000[] = { 20, 12, 10 };
7570 static const uint8_t div_5333[] = { 24, 16, 14 };
7571 const uint8_t *div_table;
7572 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
7573 uint16_t tmp = 0;
7574
52a05c30 7575 pci_read_config_word(pdev, GCFGC, &tmp);
34edce2f
VS
7576
7577 cdclk_sel = ((tmp >> 8) & 0x1f) - 1;
7578
7579 if (cdclk_sel >= ARRAY_SIZE(div_3200))
7580 goto fail;
7581
7582 switch (vco) {
7583 case 3200000:
7584 div_table = div_3200;
7585 break;
7586 case 4000000:
7587 div_table = div_4000;
7588 break;
7589 case 5333333:
7590 div_table = div_5333;
7591 break;
7592 default:
7593 goto fail;
7594 }
7595
7596 return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
7597
caf4e252 7598fail:
34edce2f
VS
7599 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%04x\n", vco, tmp);
7600 return 200000;
7601}
7602
7603static int g33_get_display_clock_speed(struct drm_device *dev)
7604{
52a05c30 7605 struct pci_dev *pdev = dev->pdev;
34edce2f
VS
7606 static const uint8_t div_3200[] = { 12, 10, 8, 7, 5, 16 };
7607 static const uint8_t div_4000[] = { 14, 12, 10, 8, 6, 20 };
7608 static const uint8_t div_4800[] = { 20, 14, 12, 10, 8, 24 };
7609 static const uint8_t div_5333[] = { 20, 16, 12, 12, 8, 28 };
7610 const uint8_t *div_table;
7611 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
7612 uint16_t tmp = 0;
7613
52a05c30 7614 pci_read_config_word(pdev, GCFGC, &tmp);
34edce2f
VS
7615
7616 cdclk_sel = (tmp >> 4) & 0x7;
7617
7618 if (cdclk_sel >= ARRAY_SIZE(div_3200))
7619 goto fail;
7620
7621 switch (vco) {
7622 case 3200000:
7623 div_table = div_3200;
7624 break;
7625 case 4000000:
7626 div_table = div_4000;
7627 break;
7628 case 4800000:
7629 div_table = div_4800;
7630 break;
7631 case 5333333:
7632 div_table = div_5333;
7633 break;
7634 default:
7635 goto fail;
7636 }
7637
7638 return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
7639
caf4e252 7640fail:
34edce2f
VS
7641 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%08x\n", vco, tmp);
7642 return 190476;
7643}
7644
2c07245f 7645static void
a65851af 7646intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
2c07245f 7647{
a65851af
VS
7648 while (*num > DATA_LINK_M_N_MASK ||
7649 *den > DATA_LINK_M_N_MASK) {
2c07245f
ZW
7650 *num >>= 1;
7651 *den >>= 1;
7652 }
7653}
7654
a65851af
VS
7655static void compute_m_n(unsigned int m, unsigned int n,
7656 uint32_t *ret_m, uint32_t *ret_n)
7657{
7658 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
7659 *ret_m = div_u64((uint64_t) m * *ret_n, n);
7660 intel_reduce_m_n_ratio(ret_m, ret_n);
7661}
7662
e69d0bc1
DV
7663void
7664intel_link_compute_m_n(int bits_per_pixel, int nlanes,
7665 int pixel_clock, int link_clock,
7666 struct intel_link_m_n *m_n)
2c07245f 7667{
e69d0bc1 7668 m_n->tu = 64;
a65851af
VS
7669
7670 compute_m_n(bits_per_pixel * pixel_clock,
7671 link_clock * nlanes * 8,
7672 &m_n->gmch_m, &m_n->gmch_n);
7673
7674 compute_m_n(pixel_clock, link_clock,
7675 &m_n->link_m, &m_n->link_n);
2c07245f
ZW
7676}
7677
a7615030
CW
7678static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
7679{
d330a953
JN
7680 if (i915.panel_use_ssc >= 0)
7681 return i915.panel_use_ssc != 0;
41aa3448 7682 return dev_priv->vbt.lvds_use_ssc
435793df 7683 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
a7615030
CW
7684}
7685
7429e9d4 7686static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
c65d77d8 7687{
7df00d7a 7688 return (1 << dpll->n) << 16 | dpll->m2;
7429e9d4 7689}
f47709a9 7690
7429e9d4
DV
7691static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
7692{
7693 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
c65d77d8
JB
7694}
7695
f47709a9 7696static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
190f68c5 7697 struct intel_crtc_state *crtc_state,
9e2c8475 7698 struct dpll *reduced_clock)
a7516a05 7699{
f47709a9 7700 struct drm_device *dev = crtc->base.dev;
a7516a05
JB
7701 u32 fp, fp2 = 0;
7702
7703 if (IS_PINEVIEW(dev)) {
190f68c5 7704 fp = pnv_dpll_compute_fp(&crtc_state->dpll);
a7516a05 7705 if (reduced_clock)
7429e9d4 7706 fp2 = pnv_dpll_compute_fp(reduced_clock);
a7516a05 7707 } else {
190f68c5 7708 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
a7516a05 7709 if (reduced_clock)
7429e9d4 7710 fp2 = i9xx_dpll_compute_fp(reduced_clock);
a7516a05
JB
7711 }
7712
190f68c5 7713 crtc_state->dpll_hw_state.fp0 = fp;
a7516a05 7714
f47709a9 7715 crtc->lowfreq_avail = false;
2d84d2b3 7716 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
ab585dea 7717 reduced_clock) {
190f68c5 7718 crtc_state->dpll_hw_state.fp1 = fp2;
f47709a9 7719 crtc->lowfreq_avail = true;
a7516a05 7720 } else {
190f68c5 7721 crtc_state->dpll_hw_state.fp1 = fp;
a7516a05
JB
7722 }
7723}
7724
5e69f97f
CML
7725static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
7726 pipe)
89b667f8
JB
7727{
7728 u32 reg_val;
7729
7730 /*
7731 * PLLB opamp always calibrates to max value of 0x3f, force enable it
7732 * and set it to a reasonable value instead.
7733 */
ab3c759a 7734 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
89b667f8
JB
7735 reg_val &= 0xffffff00;
7736 reg_val |= 0x00000030;
ab3c759a 7737 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
89b667f8 7738
ab3c759a 7739 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
89b667f8
JB
7740 reg_val &= 0x8cffffff;
7741 reg_val = 0x8c000000;
ab3c759a 7742 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
89b667f8 7743
ab3c759a 7744 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
89b667f8 7745 reg_val &= 0xffffff00;
ab3c759a 7746 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
89b667f8 7747
ab3c759a 7748 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
89b667f8
JB
7749 reg_val &= 0x00ffffff;
7750 reg_val |= 0xb0000000;
ab3c759a 7751 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
89b667f8
JB
7752}
7753
b551842d
DV
7754static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
7755 struct intel_link_m_n *m_n)
7756{
7757 struct drm_device *dev = crtc->base.dev;
fac5e23e 7758 struct drm_i915_private *dev_priv = to_i915(dev);
b551842d
DV
7759 int pipe = crtc->pipe;
7760
e3b95f1e
DV
7761 I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7762 I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
7763 I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
7764 I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
b551842d
DV
7765}
7766
7767static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
f769cd24
VK
7768 struct intel_link_m_n *m_n,
7769 struct intel_link_m_n *m2_n2)
b551842d
DV
7770{
7771 struct drm_device *dev = crtc->base.dev;
fac5e23e 7772 struct drm_i915_private *dev_priv = to_i915(dev);
b551842d 7773 int pipe = crtc->pipe;
6e3c9717 7774 enum transcoder transcoder = crtc->config->cpu_transcoder;
b551842d
DV
7775
7776 if (INTEL_INFO(dev)->gen >= 5) {
7777 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
7778 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
7779 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
7780 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
f769cd24
VK
7781 /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
7782 * for gen < 8) and if DRRS is supported (to make sure the
7783 * registers are not unnecessarily accessed).
7784 */
44395bfe 7785 if (m2_n2 && (IS_CHERRYVIEW(dev) || INTEL_INFO(dev)->gen < 8) &&
6e3c9717 7786 crtc->config->has_drrs) {
f769cd24
VK
7787 I915_WRITE(PIPE_DATA_M2(transcoder),
7788 TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
7789 I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
7790 I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
7791 I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
7792 }
b551842d 7793 } else {
e3b95f1e
DV
7794 I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7795 I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
7796 I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
7797 I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
b551842d
DV
7798 }
7799}
7800
fe3cd48d 7801void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
03afc4a2 7802{
fe3cd48d
R
7803 struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
7804
7805 if (m_n == M1_N1) {
7806 dp_m_n = &crtc->config->dp_m_n;
7807 dp_m2_n2 = &crtc->config->dp_m2_n2;
7808 } else if (m_n == M2_N2) {
7809
7810 /*
7811 * M2_N2 registers are not supported. Hence m2_n2 divider value
7812 * needs to be programmed into M1_N1.
7813 */
7814 dp_m_n = &crtc->config->dp_m2_n2;
7815 } else {
7816 DRM_ERROR("Unsupported divider value\n");
7817 return;
7818 }
7819
6e3c9717
ACO
7820 if (crtc->config->has_pch_encoder)
7821 intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
03afc4a2 7822 else
fe3cd48d 7823 intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
03afc4a2
DV
7824}
7825
251ac862
DV
7826static void vlv_compute_dpll(struct intel_crtc *crtc,
7827 struct intel_crtc_state *pipe_config)
bdd4b6a6 7828{
03ed5cbf 7829 pipe_config->dpll_hw_state.dpll = DPLL_INTEGRATED_REF_CLK_VLV |
cd2d34d9 7830 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
03ed5cbf
VS
7831 if (crtc->pipe != PIPE_A)
7832 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
bdd4b6a6 7833
cd2d34d9 7834 /* DPLL not used with DSI, but still need the rest set up */
d7edc4e5 7835 if (!intel_crtc_has_type(pipe_config, INTEL_OUTPUT_DSI))
cd2d34d9
VS
7836 pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE |
7837 DPLL_EXT_BUFFER_ENABLE_VLV;
7838
03ed5cbf
VS
7839 pipe_config->dpll_hw_state.dpll_md =
7840 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
7841}
bdd4b6a6 7842
03ed5cbf
VS
7843static void chv_compute_dpll(struct intel_crtc *crtc,
7844 struct intel_crtc_state *pipe_config)
7845{
7846 pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLK_CHV |
cd2d34d9 7847 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
03ed5cbf
VS
7848 if (crtc->pipe != PIPE_A)
7849 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
7850
cd2d34d9 7851 /* DPLL not used with DSI, but still need the rest set up */
d7edc4e5 7852 if (!intel_crtc_has_type(pipe_config, INTEL_OUTPUT_DSI))
cd2d34d9
VS
7853 pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE;
7854
03ed5cbf
VS
7855 pipe_config->dpll_hw_state.dpll_md =
7856 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
bdd4b6a6
DV
7857}
7858
d288f65f 7859static void vlv_prepare_pll(struct intel_crtc *crtc,
5cec258b 7860 const struct intel_crtc_state *pipe_config)
a0c4da24 7861{
f47709a9 7862 struct drm_device *dev = crtc->base.dev;
fac5e23e 7863 struct drm_i915_private *dev_priv = to_i915(dev);
cd2d34d9 7864 enum pipe pipe = crtc->pipe;
bdd4b6a6 7865 u32 mdiv;
a0c4da24 7866 u32 bestn, bestm1, bestm2, bestp1, bestp2;
bdd4b6a6 7867 u32 coreclk, reg_val;
a0c4da24 7868
cd2d34d9
VS
7869 /* Enable Refclk */
7870 I915_WRITE(DPLL(pipe),
7871 pipe_config->dpll_hw_state.dpll &
7872 ~(DPLL_VCO_ENABLE | DPLL_EXT_BUFFER_ENABLE_VLV));
7873
7874 /* No need to actually set up the DPLL with DSI */
7875 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
7876 return;
7877
a580516d 7878 mutex_lock(&dev_priv->sb_lock);
09153000 7879
d288f65f
VS
7880 bestn = pipe_config->dpll.n;
7881 bestm1 = pipe_config->dpll.m1;
7882 bestm2 = pipe_config->dpll.m2;
7883 bestp1 = pipe_config->dpll.p1;
7884 bestp2 = pipe_config->dpll.p2;
a0c4da24 7885
89b667f8
JB
7886 /* See eDP HDMI DPIO driver vbios notes doc */
7887
7888 /* PLL B needs special handling */
bdd4b6a6 7889 if (pipe == PIPE_B)
5e69f97f 7890 vlv_pllb_recal_opamp(dev_priv, pipe);
89b667f8
JB
7891
7892 /* Set up Tx target for periodic Rcomp update */
ab3c759a 7893 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
89b667f8
JB
7894
7895 /* Disable target IRef on PLL */
ab3c759a 7896 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
89b667f8 7897 reg_val &= 0x00ffffff;
ab3c759a 7898 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
89b667f8
JB
7899
7900 /* Disable fast lock */
ab3c759a 7901 vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
89b667f8
JB
7902
7903 /* Set idtafcrecal before PLL is enabled */
a0c4da24
JB
7904 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
7905 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
7906 mdiv |= ((bestn << DPIO_N_SHIFT));
a0c4da24 7907 mdiv |= (1 << DPIO_K_SHIFT);
7df5080b
JB
7908
7909 /*
7910 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
7911 * but we don't support that).
7912 * Note: don't use the DAC post divider as it seems unstable.
7913 */
7914 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
ab3c759a 7915 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
a0c4da24 7916
a0c4da24 7917 mdiv |= DPIO_ENABLE_CALIBRATION;
ab3c759a 7918 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
a0c4da24 7919
89b667f8 7920 /* Set HBR and RBR LPF coefficients */
d288f65f 7921 if (pipe_config->port_clock == 162000 ||
2d84d2b3
VS
7922 intel_crtc_has_type(crtc->config, INTEL_OUTPUT_ANALOG) ||
7923 intel_crtc_has_type(crtc->config, INTEL_OUTPUT_HDMI))
ab3c759a 7924 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
885b0120 7925 0x009f0003);
89b667f8 7926 else
ab3c759a 7927 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
89b667f8
JB
7928 0x00d0000f);
7929
37a5650b 7930 if (intel_crtc_has_dp_encoder(pipe_config)) {
89b667f8 7931 /* Use SSC source */
bdd4b6a6 7932 if (pipe == PIPE_A)
ab3c759a 7933 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
7934 0x0df40000);
7935 else
ab3c759a 7936 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
7937 0x0df70000);
7938 } else { /* HDMI or VGA */
7939 /* Use bend source */
bdd4b6a6 7940 if (pipe == PIPE_A)
ab3c759a 7941 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
7942 0x0df70000);
7943 else
ab3c759a 7944 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
7945 0x0df40000);
7946 }
a0c4da24 7947
ab3c759a 7948 coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
89b667f8 7949 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
2210ce7f 7950 if (intel_crtc_has_dp_encoder(crtc->config))
89b667f8 7951 coreclk |= 0x01000000;
ab3c759a 7952 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
a0c4da24 7953
ab3c759a 7954 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
a580516d 7955 mutex_unlock(&dev_priv->sb_lock);
a0c4da24
JB
7956}
7957
d288f65f 7958static void chv_prepare_pll(struct intel_crtc *crtc,
5cec258b 7959 const struct intel_crtc_state *pipe_config)
9d556c99
CML
7960{
7961 struct drm_device *dev = crtc->base.dev;
fac5e23e 7962 struct drm_i915_private *dev_priv = to_i915(dev);
cd2d34d9 7963 enum pipe pipe = crtc->pipe;
9d556c99 7964 enum dpio_channel port = vlv_pipe_to_channel(pipe);
9cbe40c1 7965 u32 loopfilter, tribuf_calcntr;
9d556c99 7966 u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
a945ce7e 7967 u32 dpio_val;
9cbe40c1 7968 int vco;
9d556c99 7969
cd2d34d9
VS
7970 /* Enable Refclk and SSC */
7971 I915_WRITE(DPLL(pipe),
7972 pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
7973
7974 /* No need to actually set up the DPLL with DSI */
7975 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
7976 return;
7977
d288f65f
VS
7978 bestn = pipe_config->dpll.n;
7979 bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
7980 bestm1 = pipe_config->dpll.m1;
7981 bestm2 = pipe_config->dpll.m2 >> 22;
7982 bestp1 = pipe_config->dpll.p1;
7983 bestp2 = pipe_config->dpll.p2;
9cbe40c1 7984 vco = pipe_config->dpll.vco;
a945ce7e 7985 dpio_val = 0;
9cbe40c1 7986 loopfilter = 0;
9d556c99 7987
a580516d 7988 mutex_lock(&dev_priv->sb_lock);
9d556c99 7989
9d556c99
CML
7990 /* p1 and p2 divider */
7991 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
7992 5 << DPIO_CHV_S1_DIV_SHIFT |
7993 bestp1 << DPIO_CHV_P1_DIV_SHIFT |
7994 bestp2 << DPIO_CHV_P2_DIV_SHIFT |
7995 1 << DPIO_CHV_K_DIV_SHIFT);
7996
7997 /* Feedback post-divider - m2 */
7998 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
7999
8000 /* Feedback refclk divider - n and m1 */
8001 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
8002 DPIO_CHV_M1_DIV_BY_2 |
8003 1 << DPIO_CHV_N_DIV_SHIFT);
8004
8005 /* M2 fraction division */
25a25dfc 8006 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
9d556c99
CML
8007
8008 /* M2 fraction division enable */
a945ce7e
VP
8009 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
8010 dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
8011 dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
8012 if (bestm2_frac)
8013 dpio_val |= DPIO_CHV_FRAC_DIV_EN;
8014 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
9d556c99 8015
de3a0fde
VP
8016 /* Program digital lock detect threshold */
8017 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
8018 dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
8019 DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
8020 dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
8021 if (!bestm2_frac)
8022 dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
8023 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
8024
9d556c99 8025 /* Loop filter */
9cbe40c1
VP
8026 if (vco == 5400000) {
8027 loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
8028 loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
8029 loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
8030 tribuf_calcntr = 0x9;
8031 } else if (vco <= 6200000) {
8032 loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
8033 loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
8034 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
8035 tribuf_calcntr = 0x9;
8036 } else if (vco <= 6480000) {
8037 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
8038 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
8039 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
8040 tribuf_calcntr = 0x8;
8041 } else {
8042 /* Not supported. Apply the same limits as in the max case */
8043 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
8044 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
8045 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
8046 tribuf_calcntr = 0;
8047 }
9d556c99
CML
8048 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
8049
968040b2 8050 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
9cbe40c1
VP
8051 dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
8052 dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
8053 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
8054
9d556c99
CML
8055 /* AFC Recal */
8056 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
8057 vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
8058 DPIO_AFC_RECAL);
8059
a580516d 8060 mutex_unlock(&dev_priv->sb_lock);
9d556c99
CML
8061}
8062
d288f65f
VS
8063/**
8064 * vlv_force_pll_on - forcibly enable just the PLL
8065 * @dev_priv: i915 private structure
8066 * @pipe: pipe PLL to enable
8067 * @dpll: PLL configuration
8068 *
8069 * Enable the PLL for @pipe using the supplied @dpll config. To be used
8070 * in cases where we need the PLL enabled even when @pipe is not going to
8071 * be enabled.
8072 */
3f36b937
TU
8073int vlv_force_pll_on(struct drm_device *dev, enum pipe pipe,
8074 const struct dpll *dpll)
d288f65f
VS
8075{
8076 struct intel_crtc *crtc =
8077 to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
3f36b937
TU
8078 struct intel_crtc_state *pipe_config;
8079
8080 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
8081 if (!pipe_config)
8082 return -ENOMEM;
8083
8084 pipe_config->base.crtc = &crtc->base;
8085 pipe_config->pixel_multiplier = 1;
8086 pipe_config->dpll = *dpll;
d288f65f
VS
8087
8088 if (IS_CHERRYVIEW(dev)) {
3f36b937
TU
8089 chv_compute_dpll(crtc, pipe_config);
8090 chv_prepare_pll(crtc, pipe_config);
8091 chv_enable_pll(crtc, pipe_config);
d288f65f 8092 } else {
3f36b937
TU
8093 vlv_compute_dpll(crtc, pipe_config);
8094 vlv_prepare_pll(crtc, pipe_config);
8095 vlv_enable_pll(crtc, pipe_config);
d288f65f 8096 }
3f36b937
TU
8097
8098 kfree(pipe_config);
8099
8100 return 0;
d288f65f
VS
8101}
8102
8103/**
8104 * vlv_force_pll_off - forcibly disable just the PLL
8105 * @dev_priv: i915 private structure
8106 * @pipe: pipe PLL to disable
8107 *
8108 * Disable the PLL for @pipe. To be used in cases where we need
8109 * the PLL enabled even when @pipe is not going to be enabled.
8110 */
8111void vlv_force_pll_off(struct drm_device *dev, enum pipe pipe)
8112{
8113 if (IS_CHERRYVIEW(dev))
8114 chv_disable_pll(to_i915(dev), pipe);
8115 else
8116 vlv_disable_pll(to_i915(dev), pipe);
8117}
8118
251ac862
DV
8119static void i9xx_compute_dpll(struct intel_crtc *crtc,
8120 struct intel_crtc_state *crtc_state,
9e2c8475 8121 struct dpll *reduced_clock)
eb1cbe48 8122{
f47709a9 8123 struct drm_device *dev = crtc->base.dev;
fac5e23e 8124 struct drm_i915_private *dev_priv = to_i915(dev);
eb1cbe48 8125 u32 dpll;
190f68c5 8126 struct dpll *clock = &crtc_state->dpll;
eb1cbe48 8127
190f68c5 8128 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
2a8f64ca 8129
eb1cbe48
DV
8130 dpll = DPLL_VGA_MODE_DIS;
8131
2d84d2b3 8132 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS))
eb1cbe48
DV
8133 dpll |= DPLLB_MODE_LVDS;
8134 else
8135 dpll |= DPLLB_MODE_DAC_SERIAL;
6cc5f341 8136
ef1b460d 8137 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
190f68c5 8138 dpll |= (crtc_state->pixel_multiplier - 1)
198a037f 8139 << SDVO_MULTIPLIER_SHIFT_HIRES;
eb1cbe48 8140 }
198a037f 8141
3d6e9ee0
VS
8142 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO) ||
8143 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
4a33e48d 8144 dpll |= DPLL_SDVO_HIGH_SPEED;
198a037f 8145
37a5650b 8146 if (intel_crtc_has_dp_encoder(crtc_state))
4a33e48d 8147 dpll |= DPLL_SDVO_HIGH_SPEED;
eb1cbe48
DV
8148
8149 /* compute bitmask from p1 value */
8150 if (IS_PINEVIEW(dev))
8151 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
8152 else {
8153 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
8154 if (IS_G4X(dev) && reduced_clock)
8155 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
8156 }
8157 switch (clock->p2) {
8158 case 5:
8159 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
8160 break;
8161 case 7:
8162 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
8163 break;
8164 case 10:
8165 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
8166 break;
8167 case 14:
8168 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
8169 break;
8170 }
8171 if (INTEL_INFO(dev)->gen >= 4)
8172 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
8173
190f68c5 8174 if (crtc_state->sdvo_tv_clock)
eb1cbe48 8175 dpll |= PLL_REF_INPUT_TVCLKINBC;
2d84d2b3 8176 else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
ceb41007 8177 intel_panel_use_ssc(dev_priv))
eb1cbe48
DV
8178 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
8179 else
8180 dpll |= PLL_REF_INPUT_DREFCLK;
8181
8182 dpll |= DPLL_VCO_ENABLE;
190f68c5 8183 crtc_state->dpll_hw_state.dpll = dpll;
8bcc2795 8184
eb1cbe48 8185 if (INTEL_INFO(dev)->gen >= 4) {
190f68c5 8186 u32 dpll_md = (crtc_state->pixel_multiplier - 1)
ef1b460d 8187 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
190f68c5 8188 crtc_state->dpll_hw_state.dpll_md = dpll_md;
eb1cbe48
DV
8189 }
8190}
8191
251ac862
DV
8192static void i8xx_compute_dpll(struct intel_crtc *crtc,
8193 struct intel_crtc_state *crtc_state,
9e2c8475 8194 struct dpll *reduced_clock)
eb1cbe48 8195{
f47709a9 8196 struct drm_device *dev = crtc->base.dev;
fac5e23e 8197 struct drm_i915_private *dev_priv = to_i915(dev);
eb1cbe48 8198 u32 dpll;
190f68c5 8199 struct dpll *clock = &crtc_state->dpll;
eb1cbe48 8200
190f68c5 8201 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
2a8f64ca 8202
eb1cbe48
DV
8203 dpll = DPLL_VGA_MODE_DIS;
8204
2d84d2b3 8205 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
eb1cbe48
DV
8206 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
8207 } else {
8208 if (clock->p1 == 2)
8209 dpll |= PLL_P1_DIVIDE_BY_TWO;
8210 else
8211 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
8212 if (clock->p2 == 4)
8213 dpll |= PLL_P2_DIVIDE_BY_4;
8214 }
8215
2d84d2b3 8216 if (!IS_I830(dev) && intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DVO))
4a33e48d
DV
8217 dpll |= DPLL_DVO_2X_MODE;
8218
2d84d2b3 8219 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
ceb41007 8220 intel_panel_use_ssc(dev_priv))
eb1cbe48
DV
8221 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
8222 else
8223 dpll |= PLL_REF_INPUT_DREFCLK;
8224
8225 dpll |= DPLL_VCO_ENABLE;
190f68c5 8226 crtc_state->dpll_hw_state.dpll = dpll;
eb1cbe48
DV
8227}
8228
8a654f3b 8229static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
b0e77b9c
PZ
8230{
8231 struct drm_device *dev = intel_crtc->base.dev;
fac5e23e 8232 struct drm_i915_private *dev_priv = to_i915(dev);
b0e77b9c 8233 enum pipe pipe = intel_crtc->pipe;
6e3c9717 8234 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
7c5f93b0 8235 const struct drm_display_mode *adjusted_mode = &intel_crtc->config->base.adjusted_mode;
1caea6e9
VS
8236 uint32_t crtc_vtotal, crtc_vblank_end;
8237 int vsyncshift = 0;
4d8a62ea
DV
8238
8239 /* We need to be careful not to changed the adjusted mode, for otherwise
8240 * the hw state checker will get angry at the mismatch. */
8241 crtc_vtotal = adjusted_mode->crtc_vtotal;
8242 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
b0e77b9c 8243
609aeaca 8244 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
b0e77b9c 8245 /* the chip adds 2 halflines automatically */
4d8a62ea
DV
8246 crtc_vtotal -= 1;
8247 crtc_vblank_end -= 1;
609aeaca 8248
2d84d2b3 8249 if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
609aeaca
VS
8250 vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
8251 else
8252 vsyncshift = adjusted_mode->crtc_hsync_start -
8253 adjusted_mode->crtc_htotal / 2;
1caea6e9
VS
8254 if (vsyncshift < 0)
8255 vsyncshift += adjusted_mode->crtc_htotal;
b0e77b9c
PZ
8256 }
8257
8258 if (INTEL_INFO(dev)->gen > 3)
fe2b8f9d 8259 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
b0e77b9c 8260
fe2b8f9d 8261 I915_WRITE(HTOTAL(cpu_transcoder),
b0e77b9c
PZ
8262 (adjusted_mode->crtc_hdisplay - 1) |
8263 ((adjusted_mode->crtc_htotal - 1) << 16));
fe2b8f9d 8264 I915_WRITE(HBLANK(cpu_transcoder),
b0e77b9c
PZ
8265 (adjusted_mode->crtc_hblank_start - 1) |
8266 ((adjusted_mode->crtc_hblank_end - 1) << 16));
fe2b8f9d 8267 I915_WRITE(HSYNC(cpu_transcoder),
b0e77b9c
PZ
8268 (adjusted_mode->crtc_hsync_start - 1) |
8269 ((adjusted_mode->crtc_hsync_end - 1) << 16));
8270
fe2b8f9d 8271 I915_WRITE(VTOTAL(cpu_transcoder),
b0e77b9c 8272 (adjusted_mode->crtc_vdisplay - 1) |
4d8a62ea 8273 ((crtc_vtotal - 1) << 16));
fe2b8f9d 8274 I915_WRITE(VBLANK(cpu_transcoder),
b0e77b9c 8275 (adjusted_mode->crtc_vblank_start - 1) |
4d8a62ea 8276 ((crtc_vblank_end - 1) << 16));
fe2b8f9d 8277 I915_WRITE(VSYNC(cpu_transcoder),
b0e77b9c
PZ
8278 (adjusted_mode->crtc_vsync_start - 1) |
8279 ((adjusted_mode->crtc_vsync_end - 1) << 16));
8280
b5e508d4
PZ
8281 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
8282 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
8283 * documented on the DDI_FUNC_CTL register description, EDP Input Select
8284 * bits. */
8285 if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
8286 (pipe == PIPE_B || pipe == PIPE_C))
8287 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
8288
bc58be60
JN
8289}
8290
8291static void intel_set_pipe_src_size(struct intel_crtc *intel_crtc)
8292{
8293 struct drm_device *dev = intel_crtc->base.dev;
fac5e23e 8294 struct drm_i915_private *dev_priv = to_i915(dev);
bc58be60
JN
8295 enum pipe pipe = intel_crtc->pipe;
8296
b0e77b9c
PZ
8297 /* pipesrc controls the size that is scaled from, which should
8298 * always be the user's requested size.
8299 */
8300 I915_WRITE(PIPESRC(pipe),
6e3c9717
ACO
8301 ((intel_crtc->config->pipe_src_w - 1) << 16) |
8302 (intel_crtc->config->pipe_src_h - 1));
b0e77b9c
PZ
8303}
8304
1bd1bd80 8305static void intel_get_pipe_timings(struct intel_crtc *crtc,
5cec258b 8306 struct intel_crtc_state *pipe_config)
1bd1bd80
DV
8307{
8308 struct drm_device *dev = crtc->base.dev;
fac5e23e 8309 struct drm_i915_private *dev_priv = to_i915(dev);
1bd1bd80
DV
8310 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
8311 uint32_t tmp;
8312
8313 tmp = I915_READ(HTOTAL(cpu_transcoder));
2d112de7
ACO
8314 pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
8315 pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 8316 tmp = I915_READ(HBLANK(cpu_transcoder));
2d112de7
ACO
8317 pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
8318 pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 8319 tmp = I915_READ(HSYNC(cpu_transcoder));
2d112de7
ACO
8320 pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
8321 pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80
DV
8322
8323 tmp = I915_READ(VTOTAL(cpu_transcoder));
2d112de7
ACO
8324 pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
8325 pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 8326 tmp = I915_READ(VBLANK(cpu_transcoder));
2d112de7
ACO
8327 pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
8328 pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 8329 tmp = I915_READ(VSYNC(cpu_transcoder));
2d112de7
ACO
8330 pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
8331 pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80
DV
8332
8333 if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
2d112de7
ACO
8334 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
8335 pipe_config->base.adjusted_mode.crtc_vtotal += 1;
8336 pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
1bd1bd80 8337 }
bc58be60
JN
8338}
8339
8340static void intel_get_pipe_src_size(struct intel_crtc *crtc,
8341 struct intel_crtc_state *pipe_config)
8342{
8343 struct drm_device *dev = crtc->base.dev;
fac5e23e 8344 struct drm_i915_private *dev_priv = to_i915(dev);
bc58be60 8345 u32 tmp;
1bd1bd80
DV
8346
8347 tmp = I915_READ(PIPESRC(crtc->pipe));
37327abd
VS
8348 pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
8349 pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
8350
2d112de7
ACO
8351 pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
8352 pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
1bd1bd80
DV
8353}
8354
f6a83288 8355void intel_mode_from_pipe_config(struct drm_display_mode *mode,
5cec258b 8356 struct intel_crtc_state *pipe_config)
babea61d 8357{
2d112de7
ACO
8358 mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
8359 mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
8360 mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
8361 mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
babea61d 8362
2d112de7
ACO
8363 mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
8364 mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
8365 mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
8366 mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
babea61d 8367
2d112de7 8368 mode->flags = pipe_config->base.adjusted_mode.flags;
cd13f5ab 8369 mode->type = DRM_MODE_TYPE_DRIVER;
babea61d 8370
2d112de7
ACO
8371 mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
8372 mode->flags |= pipe_config->base.adjusted_mode.flags;
cd13f5ab
ML
8373
8374 mode->hsync = drm_mode_hsync(mode);
8375 mode->vrefresh = drm_mode_vrefresh(mode);
8376 drm_mode_set_name(mode);
babea61d
JB
8377}
8378
84b046f3
DV
8379static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
8380{
8381 struct drm_device *dev = intel_crtc->base.dev;
fac5e23e 8382 struct drm_i915_private *dev_priv = to_i915(dev);
84b046f3
DV
8383 uint32_t pipeconf;
8384
9f11a9e4 8385 pipeconf = 0;
84b046f3 8386
b6b5d049
VS
8387 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
8388 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
8389 pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
67c72a12 8390
6e3c9717 8391 if (intel_crtc->config->double_wide)
cf532bb2 8392 pipeconf |= PIPECONF_DOUBLE_WIDE;
84b046f3 8393
ff9ce46e 8394 /* only g4x and later have fancy bpc/dither controls */
666a4537 8395 if (IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
ff9ce46e 8396 /* Bspec claims that we can't use dithering for 30bpp pipes. */
6e3c9717 8397 if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
ff9ce46e 8398 pipeconf |= PIPECONF_DITHER_EN |
84b046f3 8399 PIPECONF_DITHER_TYPE_SP;
84b046f3 8400
6e3c9717 8401 switch (intel_crtc->config->pipe_bpp) {
ff9ce46e
DV
8402 case 18:
8403 pipeconf |= PIPECONF_6BPC;
8404 break;
8405 case 24:
8406 pipeconf |= PIPECONF_8BPC;
8407 break;
8408 case 30:
8409 pipeconf |= PIPECONF_10BPC;
8410 break;
8411 default:
8412 /* Case prevented by intel_choose_pipe_bpp_dither. */
8413 BUG();
84b046f3
DV
8414 }
8415 }
8416
8417 if (HAS_PIPE_CXSR(dev)) {
8418 if (intel_crtc->lowfreq_avail) {
8419 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
8420 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
8421 } else {
8422 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
84b046f3
DV
8423 }
8424 }
8425
6e3c9717 8426 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
efc2cfff 8427 if (INTEL_INFO(dev)->gen < 4 ||
2d84d2b3 8428 intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
efc2cfff
VS
8429 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
8430 else
8431 pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
8432 } else
84b046f3
DV
8433 pipeconf |= PIPECONF_PROGRESSIVE;
8434
666a4537
WB
8435 if ((IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) &&
8436 intel_crtc->config->limited_color_range)
9f11a9e4 8437 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
9c8e09b7 8438
84b046f3
DV
8439 I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
8440 POSTING_READ(PIPECONF(intel_crtc->pipe));
8441}
8442
81c97f52
ACO
8443static int i8xx_crtc_compute_clock(struct intel_crtc *crtc,
8444 struct intel_crtc_state *crtc_state)
8445{
8446 struct drm_device *dev = crtc->base.dev;
fac5e23e 8447 struct drm_i915_private *dev_priv = to_i915(dev);
1b6f4958 8448 const struct intel_limit *limit;
81c97f52
ACO
8449 int refclk = 48000;
8450
8451 memset(&crtc_state->dpll_hw_state, 0,
8452 sizeof(crtc_state->dpll_hw_state));
8453
2d84d2b3 8454 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
81c97f52
ACO
8455 if (intel_panel_use_ssc(dev_priv)) {
8456 refclk = dev_priv->vbt.lvds_ssc_freq;
8457 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
8458 }
8459
8460 limit = &intel_limits_i8xx_lvds;
2d84d2b3 8461 } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DVO)) {
81c97f52
ACO
8462 limit = &intel_limits_i8xx_dvo;
8463 } else {
8464 limit = &intel_limits_i8xx_dac;
8465 }
8466
8467 if (!crtc_state->clock_set &&
8468 !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8469 refclk, NULL, &crtc_state->dpll)) {
8470 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8471 return -EINVAL;
8472 }
8473
8474 i8xx_compute_dpll(crtc, crtc_state, NULL);
8475
8476 return 0;
8477}
8478
19ec6693
ACO
8479static int g4x_crtc_compute_clock(struct intel_crtc *crtc,
8480 struct intel_crtc_state *crtc_state)
8481{
8482 struct drm_device *dev = crtc->base.dev;
fac5e23e 8483 struct drm_i915_private *dev_priv = to_i915(dev);
1b6f4958 8484 const struct intel_limit *limit;
19ec6693
ACO
8485 int refclk = 96000;
8486
8487 memset(&crtc_state->dpll_hw_state, 0,
8488 sizeof(crtc_state->dpll_hw_state));
8489
2d84d2b3 8490 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
19ec6693
ACO
8491 if (intel_panel_use_ssc(dev_priv)) {
8492 refclk = dev_priv->vbt.lvds_ssc_freq;
8493 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
8494 }
8495
8496 if (intel_is_dual_link_lvds(dev))
8497 limit = &intel_limits_g4x_dual_channel_lvds;
8498 else
8499 limit = &intel_limits_g4x_single_channel_lvds;
2d84d2b3
VS
8500 } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI) ||
8501 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
19ec6693 8502 limit = &intel_limits_g4x_hdmi;
2d84d2b3 8503 } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO)) {
19ec6693
ACO
8504 limit = &intel_limits_g4x_sdvo;
8505 } else {
8506 /* The option is for other outputs */
8507 limit = &intel_limits_i9xx_sdvo;
8508 }
8509
8510 if (!crtc_state->clock_set &&
8511 !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8512 refclk, NULL, &crtc_state->dpll)) {
8513 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8514 return -EINVAL;
8515 }
8516
8517 i9xx_compute_dpll(crtc, crtc_state, NULL);
8518
8519 return 0;
8520}
8521
70e8aa21
ACO
8522static int pnv_crtc_compute_clock(struct intel_crtc *crtc,
8523 struct intel_crtc_state *crtc_state)
8524{
8525 struct drm_device *dev = crtc->base.dev;
fac5e23e 8526 struct drm_i915_private *dev_priv = to_i915(dev);
1b6f4958 8527 const struct intel_limit *limit;
70e8aa21
ACO
8528 int refclk = 96000;
8529
8530 memset(&crtc_state->dpll_hw_state, 0,
8531 sizeof(crtc_state->dpll_hw_state));
8532
2d84d2b3 8533 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
70e8aa21
ACO
8534 if (intel_panel_use_ssc(dev_priv)) {
8535 refclk = dev_priv->vbt.lvds_ssc_freq;
8536 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
8537 }
8538
8539 limit = &intel_limits_pineview_lvds;
8540 } else {
8541 limit = &intel_limits_pineview_sdvo;
8542 }
8543
8544 if (!crtc_state->clock_set &&
8545 !pnv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8546 refclk, NULL, &crtc_state->dpll)) {
8547 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8548 return -EINVAL;
8549 }
8550
8551 i9xx_compute_dpll(crtc, crtc_state, NULL);
8552
8553 return 0;
8554}
8555
190f68c5
ACO
8556static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
8557 struct intel_crtc_state *crtc_state)
79e53945 8558{
c7653199 8559 struct drm_device *dev = crtc->base.dev;
fac5e23e 8560 struct drm_i915_private *dev_priv = to_i915(dev);
1b6f4958 8561 const struct intel_limit *limit;
81c97f52 8562 int refclk = 96000;
79e53945 8563
dd3cd74a
ACO
8564 memset(&crtc_state->dpll_hw_state, 0,
8565 sizeof(crtc_state->dpll_hw_state));
8566
2d84d2b3 8567 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
70e8aa21
ACO
8568 if (intel_panel_use_ssc(dev_priv)) {
8569 refclk = dev_priv->vbt.lvds_ssc_freq;
8570 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
8571 }
43565a06 8572
70e8aa21
ACO
8573 limit = &intel_limits_i9xx_lvds;
8574 } else {
8575 limit = &intel_limits_i9xx_sdvo;
81c97f52 8576 }
79e53945 8577
70e8aa21
ACO
8578 if (!crtc_state->clock_set &&
8579 !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8580 refclk, NULL, &crtc_state->dpll)) {
8581 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8582 return -EINVAL;
f47709a9 8583 }
7026d4ac 8584
81c97f52 8585 i9xx_compute_dpll(crtc, crtc_state, NULL);
79e53945 8586
c8f7a0db 8587 return 0;
f564048e
EA
8588}
8589
65b3d6a9
ACO
8590static int chv_crtc_compute_clock(struct intel_crtc *crtc,
8591 struct intel_crtc_state *crtc_state)
8592{
8593 int refclk = 100000;
1b6f4958 8594 const struct intel_limit *limit = &intel_limits_chv;
65b3d6a9
ACO
8595
8596 memset(&crtc_state->dpll_hw_state, 0,
8597 sizeof(crtc_state->dpll_hw_state));
8598
65b3d6a9
ACO
8599 if (!crtc_state->clock_set &&
8600 !chv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8601 refclk, NULL, &crtc_state->dpll)) {
8602 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8603 return -EINVAL;
8604 }
8605
8606 chv_compute_dpll(crtc, crtc_state);
8607
8608 return 0;
8609}
8610
8611static int vlv_crtc_compute_clock(struct intel_crtc *crtc,
8612 struct intel_crtc_state *crtc_state)
8613{
8614 int refclk = 100000;
1b6f4958 8615 const struct intel_limit *limit = &intel_limits_vlv;
65b3d6a9
ACO
8616
8617 memset(&crtc_state->dpll_hw_state, 0,
8618 sizeof(crtc_state->dpll_hw_state));
8619
65b3d6a9
ACO
8620 if (!crtc_state->clock_set &&
8621 !vlv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8622 refclk, NULL, &crtc_state->dpll)) {
8623 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8624 return -EINVAL;
8625 }
8626
8627 vlv_compute_dpll(crtc, crtc_state);
8628
8629 return 0;
8630}
8631
2fa2fe9a 8632static void i9xx_get_pfit_config(struct intel_crtc *crtc,
5cec258b 8633 struct intel_crtc_state *pipe_config)
2fa2fe9a
DV
8634{
8635 struct drm_device *dev = crtc->base.dev;
fac5e23e 8636 struct drm_i915_private *dev_priv = to_i915(dev);
2fa2fe9a
DV
8637 uint32_t tmp;
8638
dc9e7dec
VS
8639 if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
8640 return;
8641
2fa2fe9a 8642 tmp = I915_READ(PFIT_CONTROL);
06922821
DV
8643 if (!(tmp & PFIT_ENABLE))
8644 return;
2fa2fe9a 8645
06922821 8646 /* Check whether the pfit is attached to our pipe. */
2fa2fe9a
DV
8647 if (INTEL_INFO(dev)->gen < 4) {
8648 if (crtc->pipe != PIPE_B)
8649 return;
2fa2fe9a
DV
8650 } else {
8651 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
8652 return;
8653 }
8654
06922821 8655 pipe_config->gmch_pfit.control = tmp;
2fa2fe9a 8656 pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
2fa2fe9a
DV
8657}
8658
acbec814 8659static void vlv_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 8660 struct intel_crtc_state *pipe_config)
acbec814
JB
8661{
8662 struct drm_device *dev = crtc->base.dev;
fac5e23e 8663 struct drm_i915_private *dev_priv = to_i915(dev);
acbec814 8664 int pipe = pipe_config->cpu_transcoder;
9e2c8475 8665 struct dpll clock;
acbec814 8666 u32 mdiv;
662c6ecb 8667 int refclk = 100000;
acbec814 8668
b521973b
VS
8669 /* In case of DSI, DPLL will not be used */
8670 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
f573de5a
SK
8671 return;
8672
a580516d 8673 mutex_lock(&dev_priv->sb_lock);
ab3c759a 8674 mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
a580516d 8675 mutex_unlock(&dev_priv->sb_lock);
acbec814
JB
8676
8677 clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
8678 clock.m2 = mdiv & DPIO_M2DIV_MASK;
8679 clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
8680 clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
8681 clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
8682
dccbea3b 8683 pipe_config->port_clock = vlv_calc_dpll_params(refclk, &clock);
acbec814
JB
8684}
8685
5724dbd1
DL
8686static void
8687i9xx_get_initial_plane_config(struct intel_crtc *crtc,
8688 struct intel_initial_plane_config *plane_config)
1ad292b5
JB
8689{
8690 struct drm_device *dev = crtc->base.dev;
fac5e23e 8691 struct drm_i915_private *dev_priv = to_i915(dev);
1ad292b5
JB
8692 u32 val, base, offset;
8693 int pipe = crtc->pipe, plane = crtc->plane;
8694 int fourcc, pixel_format;
6761dd31 8695 unsigned int aligned_height;
b113d5ee 8696 struct drm_framebuffer *fb;
1b842c89 8697 struct intel_framebuffer *intel_fb;
1ad292b5 8698
42a7b088
DL
8699 val = I915_READ(DSPCNTR(plane));
8700 if (!(val & DISPLAY_PLANE_ENABLE))
8701 return;
8702
d9806c9f 8703 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
1b842c89 8704 if (!intel_fb) {
1ad292b5
JB
8705 DRM_DEBUG_KMS("failed to alloc fb\n");
8706 return;
8707 }
8708
1b842c89
DL
8709 fb = &intel_fb->base;
8710
18c5247e
DV
8711 if (INTEL_INFO(dev)->gen >= 4) {
8712 if (val & DISPPLANE_TILED) {
49af449b 8713 plane_config->tiling = I915_TILING_X;
18c5247e
DV
8714 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
8715 }
8716 }
1ad292b5
JB
8717
8718 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
b35d63fa 8719 fourcc = i9xx_format_to_fourcc(pixel_format);
b113d5ee
DL
8720 fb->pixel_format = fourcc;
8721 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
1ad292b5
JB
8722
8723 if (INTEL_INFO(dev)->gen >= 4) {
49af449b 8724 if (plane_config->tiling)
1ad292b5
JB
8725 offset = I915_READ(DSPTILEOFF(plane));
8726 else
8727 offset = I915_READ(DSPLINOFF(plane));
8728 base = I915_READ(DSPSURF(plane)) & 0xfffff000;
8729 } else {
8730 base = I915_READ(DSPADDR(plane));
8731 }
8732 plane_config->base = base;
8733
8734 val = I915_READ(PIPESRC(pipe));
b113d5ee
DL
8735 fb->width = ((val >> 16) & 0xfff) + 1;
8736 fb->height = ((val >> 0) & 0xfff) + 1;
1ad292b5
JB
8737
8738 val = I915_READ(DSPSTRIDE(pipe));
b113d5ee 8739 fb->pitches[0] = val & 0xffffffc0;
1ad292b5 8740
b113d5ee 8741 aligned_height = intel_fb_align_height(dev, fb->height,
091df6cb
DV
8742 fb->pixel_format,
8743 fb->modifier[0]);
1ad292b5 8744
f37b5c2b 8745 plane_config->size = fb->pitches[0] * aligned_height;
1ad292b5 8746
2844a921
DL
8747 DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
8748 pipe_name(pipe), plane, fb->width, fb->height,
8749 fb->bits_per_pixel, base, fb->pitches[0],
8750 plane_config->size);
1ad292b5 8751
2d14030b 8752 plane_config->fb = intel_fb;
1ad292b5
JB
8753}
8754
70b23a98 8755static void chv_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 8756 struct intel_crtc_state *pipe_config)
70b23a98
VS
8757{
8758 struct drm_device *dev = crtc->base.dev;
fac5e23e 8759 struct drm_i915_private *dev_priv = to_i915(dev);
70b23a98
VS
8760 int pipe = pipe_config->cpu_transcoder;
8761 enum dpio_channel port = vlv_pipe_to_channel(pipe);
9e2c8475 8762 struct dpll clock;
0d7b6b11 8763 u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2, pll_dw3;
70b23a98
VS
8764 int refclk = 100000;
8765
b521973b
VS
8766 /* In case of DSI, DPLL will not be used */
8767 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
8768 return;
8769
a580516d 8770 mutex_lock(&dev_priv->sb_lock);
70b23a98
VS
8771 cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
8772 pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
8773 pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
8774 pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
0d7b6b11 8775 pll_dw3 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
a580516d 8776 mutex_unlock(&dev_priv->sb_lock);
70b23a98
VS
8777
8778 clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
0d7b6b11
ID
8779 clock.m2 = (pll_dw0 & 0xff) << 22;
8780 if (pll_dw3 & DPIO_CHV_FRAC_DIV_EN)
8781 clock.m2 |= pll_dw2 & 0x3fffff;
70b23a98
VS
8782 clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
8783 clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
8784 clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
8785
dccbea3b 8786 pipe_config->port_clock = chv_calc_dpll_params(refclk, &clock);
70b23a98
VS
8787}
8788
0e8ffe1b 8789static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
5cec258b 8790 struct intel_crtc_state *pipe_config)
0e8ffe1b
DV
8791{
8792 struct drm_device *dev = crtc->base.dev;
fac5e23e 8793 struct drm_i915_private *dev_priv = to_i915(dev);
1729050e 8794 enum intel_display_power_domain power_domain;
0e8ffe1b 8795 uint32_t tmp;
1729050e 8796 bool ret;
0e8ffe1b 8797
1729050e
ID
8798 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
8799 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
b5482bd0
ID
8800 return false;
8801
e143a21c 8802 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
8106ddbd 8803 pipe_config->shared_dpll = NULL;
eccb140b 8804
1729050e
ID
8805 ret = false;
8806
0e8ffe1b
DV
8807 tmp = I915_READ(PIPECONF(crtc->pipe));
8808 if (!(tmp & PIPECONF_ENABLE))
1729050e 8809 goto out;
0e8ffe1b 8810
666a4537 8811 if (IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
42571aef
VS
8812 switch (tmp & PIPECONF_BPC_MASK) {
8813 case PIPECONF_6BPC:
8814 pipe_config->pipe_bpp = 18;
8815 break;
8816 case PIPECONF_8BPC:
8817 pipe_config->pipe_bpp = 24;
8818 break;
8819 case PIPECONF_10BPC:
8820 pipe_config->pipe_bpp = 30;
8821 break;
8822 default:
8823 break;
8824 }
8825 }
8826
666a4537
WB
8827 if ((IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) &&
8828 (tmp & PIPECONF_COLOR_RANGE_SELECT))
b5a9fa09
DV
8829 pipe_config->limited_color_range = true;
8830
282740f7
VS
8831 if (INTEL_INFO(dev)->gen < 4)
8832 pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
8833
1bd1bd80 8834 intel_get_pipe_timings(crtc, pipe_config);
bc58be60 8835 intel_get_pipe_src_size(crtc, pipe_config);
1bd1bd80 8836
2fa2fe9a
DV
8837 i9xx_get_pfit_config(crtc, pipe_config);
8838
6c49f241 8839 if (INTEL_INFO(dev)->gen >= 4) {
c231775c
VS
8840 /* No way to read it out on pipes B and C */
8841 if (IS_CHERRYVIEW(dev) && crtc->pipe != PIPE_A)
8842 tmp = dev_priv->chv_dpll_md[crtc->pipe];
8843 else
8844 tmp = I915_READ(DPLL_MD(crtc->pipe));
6c49f241
DV
8845 pipe_config->pixel_multiplier =
8846 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
8847 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
8bcc2795 8848 pipe_config->dpll_hw_state.dpll_md = tmp;
6c49f241
DV
8849 } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
8850 tmp = I915_READ(DPLL(crtc->pipe));
8851 pipe_config->pixel_multiplier =
8852 ((tmp & SDVO_MULTIPLIER_MASK)
8853 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
8854 } else {
8855 /* Note that on i915G/GM the pixel multiplier is in the sdvo
8856 * port and will be fixed up in the encoder->get_config
8857 * function. */
8858 pipe_config->pixel_multiplier = 1;
8859 }
8bcc2795 8860 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
666a4537 8861 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
1c4e0274
VS
8862 /*
8863 * DPLL_DVO_2X_MODE must be enabled for both DPLLs
8864 * on 830. Filter it out here so that we don't
8865 * report errors due to that.
8866 */
8867 if (IS_I830(dev))
8868 pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
8869
8bcc2795
DV
8870 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
8871 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
165e901c
VS
8872 } else {
8873 /* Mask out read-only status bits. */
8874 pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
8875 DPLL_PORTC_READY_MASK |
8876 DPLL_PORTB_READY_MASK);
8bcc2795 8877 }
6c49f241 8878
70b23a98
VS
8879 if (IS_CHERRYVIEW(dev))
8880 chv_crtc_clock_get(crtc, pipe_config);
8881 else if (IS_VALLEYVIEW(dev))
acbec814
JB
8882 vlv_crtc_clock_get(crtc, pipe_config);
8883 else
8884 i9xx_crtc_clock_get(crtc, pipe_config);
18442d08 8885
0f64614d
VS
8886 /*
8887 * Normally the dotclock is filled in by the encoder .get_config()
8888 * but in case the pipe is enabled w/o any ports we need a sane
8889 * default.
8890 */
8891 pipe_config->base.adjusted_mode.crtc_clock =
8892 pipe_config->port_clock / pipe_config->pixel_multiplier;
8893
1729050e
ID
8894 ret = true;
8895
8896out:
8897 intel_display_power_put(dev_priv, power_domain);
8898
8899 return ret;
0e8ffe1b
DV
8900}
8901
dde86e2d 8902static void ironlake_init_pch_refclk(struct drm_device *dev)
13d83a67 8903{
fac5e23e 8904 struct drm_i915_private *dev_priv = to_i915(dev);
13d83a67 8905 struct intel_encoder *encoder;
1c1a24d2 8906 int i;
74cfd7ac 8907 u32 val, final;
13d83a67 8908 bool has_lvds = false;
199e5d79 8909 bool has_cpu_edp = false;
199e5d79 8910 bool has_panel = false;
99eb6a01
KP
8911 bool has_ck505 = false;
8912 bool can_ssc = false;
1c1a24d2 8913 bool using_ssc_source = false;
13d83a67
JB
8914
8915 /* We need to take the global config into account */
b2784e15 8916 for_each_intel_encoder(dev, encoder) {
199e5d79
KP
8917 switch (encoder->type) {
8918 case INTEL_OUTPUT_LVDS:
8919 has_panel = true;
8920 has_lvds = true;
8921 break;
8922 case INTEL_OUTPUT_EDP:
8923 has_panel = true;
2de6905f 8924 if (enc_to_dig_port(&encoder->base)->port == PORT_A)
199e5d79
KP
8925 has_cpu_edp = true;
8926 break;
6847d71b
PZ
8927 default:
8928 break;
13d83a67
JB
8929 }
8930 }
8931
99eb6a01 8932 if (HAS_PCH_IBX(dev)) {
41aa3448 8933 has_ck505 = dev_priv->vbt.display_clock_mode;
99eb6a01
KP
8934 can_ssc = has_ck505;
8935 } else {
8936 has_ck505 = false;
8937 can_ssc = true;
8938 }
8939
1c1a24d2
L
8940 /* Check if any DPLLs are using the SSC source */
8941 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
8942 u32 temp = I915_READ(PCH_DPLL(i));
8943
8944 if (!(temp & DPLL_VCO_ENABLE))
8945 continue;
8946
8947 if ((temp & PLL_REF_INPUT_MASK) ==
8948 PLLB_REF_INPUT_SPREADSPECTRUMIN) {
8949 using_ssc_source = true;
8950 break;
8951 }
8952 }
8953
8954 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d using_ssc_source %d\n",
8955 has_panel, has_lvds, has_ck505, using_ssc_source);
13d83a67
JB
8956
8957 /* Ironlake: try to setup display ref clock before DPLL
8958 * enabling. This is only under driver's control after
8959 * PCH B stepping, previous chipset stepping should be
8960 * ignoring this setting.
8961 */
74cfd7ac
CW
8962 val = I915_READ(PCH_DREF_CONTROL);
8963
8964 /* As we must carefully and slowly disable/enable each source in turn,
8965 * compute the final state we want first and check if we need to
8966 * make any changes at all.
8967 */
8968 final = val;
8969 final &= ~DREF_NONSPREAD_SOURCE_MASK;
8970 if (has_ck505)
8971 final |= DREF_NONSPREAD_CK505_ENABLE;
8972 else
8973 final |= DREF_NONSPREAD_SOURCE_ENABLE;
8974
8c07eb68 8975 final &= ~DREF_SSC_SOURCE_MASK;
74cfd7ac 8976 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
8c07eb68 8977 final &= ~DREF_SSC1_ENABLE;
74cfd7ac
CW
8978
8979 if (has_panel) {
8980 final |= DREF_SSC_SOURCE_ENABLE;
8981
8982 if (intel_panel_use_ssc(dev_priv) && can_ssc)
8983 final |= DREF_SSC1_ENABLE;
8984
8985 if (has_cpu_edp) {
8986 if (intel_panel_use_ssc(dev_priv) && can_ssc)
8987 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
8988 else
8989 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
8990 } else
8991 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
1c1a24d2
L
8992 } else if (using_ssc_source) {
8993 final |= DREF_SSC_SOURCE_ENABLE;
8994 final |= DREF_SSC1_ENABLE;
74cfd7ac
CW
8995 }
8996
8997 if (final == val)
8998 return;
8999
13d83a67 9000 /* Always enable nonspread source */
74cfd7ac 9001 val &= ~DREF_NONSPREAD_SOURCE_MASK;
13d83a67 9002
99eb6a01 9003 if (has_ck505)
74cfd7ac 9004 val |= DREF_NONSPREAD_CK505_ENABLE;
99eb6a01 9005 else
74cfd7ac 9006 val |= DREF_NONSPREAD_SOURCE_ENABLE;
13d83a67 9007
199e5d79 9008 if (has_panel) {
74cfd7ac
CW
9009 val &= ~DREF_SSC_SOURCE_MASK;
9010 val |= DREF_SSC_SOURCE_ENABLE;
13d83a67 9011
199e5d79 9012 /* SSC must be turned on before enabling the CPU output */
99eb6a01 9013 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
199e5d79 9014 DRM_DEBUG_KMS("Using SSC on panel\n");
74cfd7ac 9015 val |= DREF_SSC1_ENABLE;
e77166b5 9016 } else
74cfd7ac 9017 val &= ~DREF_SSC1_ENABLE;
199e5d79
KP
9018
9019 /* Get SSC going before enabling the outputs */
74cfd7ac 9020 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
9021 POSTING_READ(PCH_DREF_CONTROL);
9022 udelay(200);
9023
74cfd7ac 9024 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
13d83a67
JB
9025
9026 /* Enable CPU source on CPU attached eDP */
199e5d79 9027 if (has_cpu_edp) {
99eb6a01 9028 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
199e5d79 9029 DRM_DEBUG_KMS("Using SSC on eDP\n");
74cfd7ac 9030 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
eba905b2 9031 } else
74cfd7ac 9032 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
199e5d79 9033 } else
74cfd7ac 9034 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
199e5d79 9035
74cfd7ac 9036 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
9037 POSTING_READ(PCH_DREF_CONTROL);
9038 udelay(200);
9039 } else {
1c1a24d2 9040 DRM_DEBUG_KMS("Disabling CPU source output\n");
199e5d79 9041
74cfd7ac 9042 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
199e5d79
KP
9043
9044 /* Turn off CPU output */
74cfd7ac 9045 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
199e5d79 9046
74cfd7ac 9047 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
9048 POSTING_READ(PCH_DREF_CONTROL);
9049 udelay(200);
9050
1c1a24d2
L
9051 if (!using_ssc_source) {
9052 DRM_DEBUG_KMS("Disabling SSC source\n");
199e5d79 9053
1c1a24d2
L
9054 /* Turn off the SSC source */
9055 val &= ~DREF_SSC_SOURCE_MASK;
9056 val |= DREF_SSC_SOURCE_DISABLE;
f165d283 9057
1c1a24d2
L
9058 /* Turn off SSC1 */
9059 val &= ~DREF_SSC1_ENABLE;
9060
9061 I915_WRITE(PCH_DREF_CONTROL, val);
9062 POSTING_READ(PCH_DREF_CONTROL);
9063 udelay(200);
9064 }
13d83a67 9065 }
74cfd7ac
CW
9066
9067 BUG_ON(val != final);
13d83a67
JB
9068}
9069
f31f2d55 9070static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
dde86e2d 9071{
f31f2d55 9072 uint32_t tmp;
dde86e2d 9073
0ff066a9
PZ
9074 tmp = I915_READ(SOUTH_CHICKEN2);
9075 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
9076 I915_WRITE(SOUTH_CHICKEN2, tmp);
dde86e2d 9077
cf3598c2
ID
9078 if (wait_for_us(I915_READ(SOUTH_CHICKEN2) &
9079 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
0ff066a9 9080 DRM_ERROR("FDI mPHY reset assert timeout\n");
dde86e2d 9081
0ff066a9
PZ
9082 tmp = I915_READ(SOUTH_CHICKEN2);
9083 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
9084 I915_WRITE(SOUTH_CHICKEN2, tmp);
dde86e2d 9085
cf3598c2
ID
9086 if (wait_for_us((I915_READ(SOUTH_CHICKEN2) &
9087 FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
0ff066a9 9088 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
f31f2d55
PZ
9089}
9090
9091/* WaMPhyProgramming:hsw */
9092static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
9093{
9094 uint32_t tmp;
dde86e2d
PZ
9095
9096 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
9097 tmp &= ~(0xFF << 24);
9098 tmp |= (0x12 << 24);
9099 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
9100
dde86e2d
PZ
9101 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
9102 tmp |= (1 << 11);
9103 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
9104
9105 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
9106 tmp |= (1 << 11);
9107 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
9108
dde86e2d
PZ
9109 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
9110 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
9111 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
9112
9113 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
9114 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
9115 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
9116
0ff066a9
PZ
9117 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
9118 tmp &= ~(7 << 13);
9119 tmp |= (5 << 13);
9120 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
dde86e2d 9121
0ff066a9
PZ
9122 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
9123 tmp &= ~(7 << 13);
9124 tmp |= (5 << 13);
9125 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
dde86e2d
PZ
9126
9127 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
9128 tmp &= ~0xFF;
9129 tmp |= 0x1C;
9130 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
9131
9132 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
9133 tmp &= ~0xFF;
9134 tmp |= 0x1C;
9135 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
9136
9137 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
9138 tmp &= ~(0xFF << 16);
9139 tmp |= (0x1C << 16);
9140 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
9141
9142 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
9143 tmp &= ~(0xFF << 16);
9144 tmp |= (0x1C << 16);
9145 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
9146
0ff066a9
PZ
9147 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
9148 tmp |= (1 << 27);
9149 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
dde86e2d 9150
0ff066a9
PZ
9151 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
9152 tmp |= (1 << 27);
9153 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
dde86e2d 9154
0ff066a9
PZ
9155 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
9156 tmp &= ~(0xF << 28);
9157 tmp |= (4 << 28);
9158 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
dde86e2d 9159
0ff066a9
PZ
9160 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
9161 tmp &= ~(0xF << 28);
9162 tmp |= (4 << 28);
9163 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
f31f2d55
PZ
9164}
9165
2fa86a1f
PZ
9166/* Implements 3 different sequences from BSpec chapter "Display iCLK
9167 * Programming" based on the parameters passed:
9168 * - Sequence to enable CLKOUT_DP
9169 * - Sequence to enable CLKOUT_DP without spread
9170 * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
9171 */
9172static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
9173 bool with_fdi)
f31f2d55 9174{
fac5e23e 9175 struct drm_i915_private *dev_priv = to_i915(dev);
2fa86a1f
PZ
9176 uint32_t reg, tmp;
9177
9178 if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
9179 with_spread = true;
c2699524 9180 if (WARN(HAS_PCH_LPT_LP(dev) && with_fdi, "LP PCH doesn't have FDI\n"))
2fa86a1f 9181 with_fdi = false;
f31f2d55 9182
a580516d 9183 mutex_lock(&dev_priv->sb_lock);
f31f2d55
PZ
9184
9185 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
9186 tmp &= ~SBI_SSCCTL_DISABLE;
9187 tmp |= SBI_SSCCTL_PATHALT;
9188 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
9189
9190 udelay(24);
9191
2fa86a1f
PZ
9192 if (with_spread) {
9193 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
9194 tmp &= ~SBI_SSCCTL_PATHALT;
9195 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
f31f2d55 9196
2fa86a1f
PZ
9197 if (with_fdi) {
9198 lpt_reset_fdi_mphy(dev_priv);
9199 lpt_program_fdi_mphy(dev_priv);
9200 }
9201 }
dde86e2d 9202
c2699524 9203 reg = HAS_PCH_LPT_LP(dev) ? SBI_GEN0 : SBI_DBUFF0;
2fa86a1f
PZ
9204 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
9205 tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
9206 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
c00db246 9207
a580516d 9208 mutex_unlock(&dev_priv->sb_lock);
dde86e2d
PZ
9209}
9210
47701c3b
PZ
9211/* Sequence to disable CLKOUT_DP */
9212static void lpt_disable_clkout_dp(struct drm_device *dev)
9213{
fac5e23e 9214 struct drm_i915_private *dev_priv = to_i915(dev);
47701c3b
PZ
9215 uint32_t reg, tmp;
9216
a580516d 9217 mutex_lock(&dev_priv->sb_lock);
47701c3b 9218
c2699524 9219 reg = HAS_PCH_LPT_LP(dev) ? SBI_GEN0 : SBI_DBUFF0;
47701c3b
PZ
9220 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
9221 tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
9222 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
9223
9224 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
9225 if (!(tmp & SBI_SSCCTL_DISABLE)) {
9226 if (!(tmp & SBI_SSCCTL_PATHALT)) {
9227 tmp |= SBI_SSCCTL_PATHALT;
9228 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
9229 udelay(32);
9230 }
9231 tmp |= SBI_SSCCTL_DISABLE;
9232 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
9233 }
9234
a580516d 9235 mutex_unlock(&dev_priv->sb_lock);
47701c3b
PZ
9236}
9237
f7be2c21
VS
9238#define BEND_IDX(steps) ((50 + (steps)) / 5)
9239
9240static const uint16_t sscdivintphase[] = {
9241 [BEND_IDX( 50)] = 0x3B23,
9242 [BEND_IDX( 45)] = 0x3B23,
9243 [BEND_IDX( 40)] = 0x3C23,
9244 [BEND_IDX( 35)] = 0x3C23,
9245 [BEND_IDX( 30)] = 0x3D23,
9246 [BEND_IDX( 25)] = 0x3D23,
9247 [BEND_IDX( 20)] = 0x3E23,
9248 [BEND_IDX( 15)] = 0x3E23,
9249 [BEND_IDX( 10)] = 0x3F23,
9250 [BEND_IDX( 5)] = 0x3F23,
9251 [BEND_IDX( 0)] = 0x0025,
9252 [BEND_IDX( -5)] = 0x0025,
9253 [BEND_IDX(-10)] = 0x0125,
9254 [BEND_IDX(-15)] = 0x0125,
9255 [BEND_IDX(-20)] = 0x0225,
9256 [BEND_IDX(-25)] = 0x0225,
9257 [BEND_IDX(-30)] = 0x0325,
9258 [BEND_IDX(-35)] = 0x0325,
9259 [BEND_IDX(-40)] = 0x0425,
9260 [BEND_IDX(-45)] = 0x0425,
9261 [BEND_IDX(-50)] = 0x0525,
9262};
9263
9264/*
9265 * Bend CLKOUT_DP
9266 * steps -50 to 50 inclusive, in steps of 5
9267 * < 0 slow down the clock, > 0 speed up the clock, 0 == no bend (135MHz)
9268 * change in clock period = -(steps / 10) * 5.787 ps
9269 */
9270static void lpt_bend_clkout_dp(struct drm_i915_private *dev_priv, int steps)
9271{
9272 uint32_t tmp;
9273 int idx = BEND_IDX(steps);
9274
9275 if (WARN_ON(steps % 5 != 0))
9276 return;
9277
9278 if (WARN_ON(idx >= ARRAY_SIZE(sscdivintphase)))
9279 return;
9280
9281 mutex_lock(&dev_priv->sb_lock);
9282
9283 if (steps % 10 != 0)
9284 tmp = 0xAAAAAAAB;
9285 else
9286 tmp = 0x00000000;
9287 intel_sbi_write(dev_priv, SBI_SSCDITHPHASE, tmp, SBI_ICLK);
9288
9289 tmp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE, SBI_ICLK);
9290 tmp &= 0xffff0000;
9291 tmp |= sscdivintphase[idx];
9292 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE, tmp, SBI_ICLK);
9293
9294 mutex_unlock(&dev_priv->sb_lock);
9295}
9296
9297#undef BEND_IDX
9298
bf8fa3d3
PZ
9299static void lpt_init_pch_refclk(struct drm_device *dev)
9300{
bf8fa3d3
PZ
9301 struct intel_encoder *encoder;
9302 bool has_vga = false;
9303
b2784e15 9304 for_each_intel_encoder(dev, encoder) {
bf8fa3d3
PZ
9305 switch (encoder->type) {
9306 case INTEL_OUTPUT_ANALOG:
9307 has_vga = true;
9308 break;
6847d71b
PZ
9309 default:
9310 break;
bf8fa3d3
PZ
9311 }
9312 }
9313
f7be2c21
VS
9314 if (has_vga) {
9315 lpt_bend_clkout_dp(to_i915(dev), 0);
47701c3b 9316 lpt_enable_clkout_dp(dev, true, true);
f7be2c21 9317 } else {
47701c3b 9318 lpt_disable_clkout_dp(dev);
f7be2c21 9319 }
bf8fa3d3
PZ
9320}
9321
dde86e2d
PZ
9322/*
9323 * Initialize reference clocks when the driver loads
9324 */
9325void intel_init_pch_refclk(struct drm_device *dev)
9326{
9327 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
9328 ironlake_init_pch_refclk(dev);
9329 else if (HAS_PCH_LPT(dev))
9330 lpt_init_pch_refclk(dev);
9331}
9332
6ff93609 9333static void ironlake_set_pipeconf(struct drm_crtc *crtc)
79e53945 9334{
fac5e23e 9335 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
79e53945
JB
9336 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9337 int pipe = intel_crtc->pipe;
c8203565
PZ
9338 uint32_t val;
9339
78114071 9340 val = 0;
c8203565 9341
6e3c9717 9342 switch (intel_crtc->config->pipe_bpp) {
c8203565 9343 case 18:
dfd07d72 9344 val |= PIPECONF_6BPC;
c8203565
PZ
9345 break;
9346 case 24:
dfd07d72 9347 val |= PIPECONF_8BPC;
c8203565
PZ
9348 break;
9349 case 30:
dfd07d72 9350 val |= PIPECONF_10BPC;
c8203565
PZ
9351 break;
9352 case 36:
dfd07d72 9353 val |= PIPECONF_12BPC;
c8203565
PZ
9354 break;
9355 default:
cc769b62
PZ
9356 /* Case prevented by intel_choose_pipe_bpp_dither. */
9357 BUG();
c8203565
PZ
9358 }
9359
6e3c9717 9360 if (intel_crtc->config->dither)
c8203565
PZ
9361 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
9362
6e3c9717 9363 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
c8203565
PZ
9364 val |= PIPECONF_INTERLACED_ILK;
9365 else
9366 val |= PIPECONF_PROGRESSIVE;
9367
6e3c9717 9368 if (intel_crtc->config->limited_color_range)
3685a8f3 9369 val |= PIPECONF_COLOR_RANGE_SELECT;
3685a8f3 9370
c8203565
PZ
9371 I915_WRITE(PIPECONF(pipe), val);
9372 POSTING_READ(PIPECONF(pipe));
9373}
9374
6ff93609 9375static void haswell_set_pipeconf(struct drm_crtc *crtc)
ee2b0b38 9376{
fac5e23e 9377 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
ee2b0b38 9378 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6e3c9717 9379 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
391bf048 9380 u32 val = 0;
ee2b0b38 9381
391bf048 9382 if (IS_HASWELL(dev_priv) && intel_crtc->config->dither)
ee2b0b38
PZ
9383 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
9384
6e3c9717 9385 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
ee2b0b38
PZ
9386 val |= PIPECONF_INTERLACED_ILK;
9387 else
9388 val |= PIPECONF_PROGRESSIVE;
9389
702e7a56
PZ
9390 I915_WRITE(PIPECONF(cpu_transcoder), val);
9391 POSTING_READ(PIPECONF(cpu_transcoder));
391bf048
JN
9392}
9393
391bf048
JN
9394static void haswell_set_pipemisc(struct drm_crtc *crtc)
9395{
fac5e23e 9396 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
391bf048 9397 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
756f85cf 9398
391bf048
JN
9399 if (IS_BROADWELL(dev_priv) || INTEL_INFO(dev_priv)->gen >= 9) {
9400 u32 val = 0;
756f85cf 9401
6e3c9717 9402 switch (intel_crtc->config->pipe_bpp) {
756f85cf
PZ
9403 case 18:
9404 val |= PIPEMISC_DITHER_6_BPC;
9405 break;
9406 case 24:
9407 val |= PIPEMISC_DITHER_8_BPC;
9408 break;
9409 case 30:
9410 val |= PIPEMISC_DITHER_10_BPC;
9411 break;
9412 case 36:
9413 val |= PIPEMISC_DITHER_12_BPC;
9414 break;
9415 default:
9416 /* Case prevented by pipe_config_set_bpp. */
9417 BUG();
9418 }
9419
6e3c9717 9420 if (intel_crtc->config->dither)
756f85cf
PZ
9421 val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
9422
391bf048 9423 I915_WRITE(PIPEMISC(intel_crtc->pipe), val);
756f85cf 9424 }
ee2b0b38
PZ
9425}
9426
d4b1931c
PZ
9427int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
9428{
9429 /*
9430 * Account for spread spectrum to avoid
9431 * oversubscribing the link. Max center spread
9432 * is 2.5%; use 5% for safety's sake.
9433 */
9434 u32 bps = target_clock * bpp * 21 / 20;
619d4d04 9435 return DIV_ROUND_UP(bps, link_bw * 8);
d4b1931c
PZ
9436}
9437
7429e9d4 9438static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
6cf86a5e 9439{
7429e9d4 9440 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
f48d8f23
PZ
9441}
9442
b75ca6f6
ACO
9443static void ironlake_compute_dpll(struct intel_crtc *intel_crtc,
9444 struct intel_crtc_state *crtc_state,
9e2c8475 9445 struct dpll *reduced_clock)
79e53945 9446{
de13a2e3 9447 struct drm_crtc *crtc = &intel_crtc->base;
79e53945 9448 struct drm_device *dev = crtc->dev;
fac5e23e 9449 struct drm_i915_private *dev_priv = to_i915(dev);
b75ca6f6 9450 u32 dpll, fp, fp2;
3d6e9ee0 9451 int factor;
79e53945 9452
c1858123 9453 /* Enable autotuning of the PLL clock (if permissible) */
8febb297 9454 factor = 21;
3d6e9ee0 9455 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
8febb297 9456 if ((intel_panel_use_ssc(dev_priv) &&
e91e941b 9457 dev_priv->vbt.lvds_ssc_freq == 100000) ||
f0b44056 9458 (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
8febb297 9459 factor = 25;
190f68c5 9460 } else if (crtc_state->sdvo_tv_clock)
8febb297 9461 factor = 20;
c1858123 9462
b75ca6f6
ACO
9463 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
9464
190f68c5 9465 if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
b75ca6f6
ACO
9466 fp |= FP_CB_TUNE;
9467
9468 if (reduced_clock) {
9469 fp2 = i9xx_dpll_compute_fp(reduced_clock);
2c07245f 9470
b75ca6f6
ACO
9471 if (reduced_clock->m < factor * reduced_clock->n)
9472 fp2 |= FP_CB_TUNE;
9473 } else {
9474 fp2 = fp;
9475 }
9a7c7890 9476
5eddb70b 9477 dpll = 0;
2c07245f 9478
3d6e9ee0 9479 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS))
a07d6787
EA
9480 dpll |= DPLLB_MODE_LVDS;
9481 else
9482 dpll |= DPLLB_MODE_DAC_SERIAL;
198a037f 9483
190f68c5 9484 dpll |= (crtc_state->pixel_multiplier - 1)
ef1b460d 9485 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
198a037f 9486
3d6e9ee0
VS
9487 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO) ||
9488 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
4a33e48d 9489 dpll |= DPLL_SDVO_HIGH_SPEED;
3d6e9ee0 9490
37a5650b 9491 if (intel_crtc_has_dp_encoder(crtc_state))
4a33e48d 9492 dpll |= DPLL_SDVO_HIGH_SPEED;
79e53945 9493
a07d6787 9494 /* compute bitmask from p1 value */
190f68c5 9495 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
a07d6787 9496 /* also FPA1 */
190f68c5 9497 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
a07d6787 9498
190f68c5 9499 switch (crtc_state->dpll.p2) {
a07d6787
EA
9500 case 5:
9501 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
9502 break;
9503 case 7:
9504 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
9505 break;
9506 case 10:
9507 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
9508 break;
9509 case 14:
9510 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
9511 break;
79e53945
JB
9512 }
9513
3d6e9ee0
VS
9514 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
9515 intel_panel_use_ssc(dev_priv))
43565a06 9516 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
79e53945
JB
9517 else
9518 dpll |= PLL_REF_INPUT_DREFCLK;
9519
b75ca6f6
ACO
9520 dpll |= DPLL_VCO_ENABLE;
9521
9522 crtc_state->dpll_hw_state.dpll = dpll;
9523 crtc_state->dpll_hw_state.fp0 = fp;
9524 crtc_state->dpll_hw_state.fp1 = fp2;
de13a2e3
PZ
9525}
9526
190f68c5
ACO
9527static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
9528 struct intel_crtc_state *crtc_state)
de13a2e3 9529{
997c030c 9530 struct drm_device *dev = crtc->base.dev;
fac5e23e 9531 struct drm_i915_private *dev_priv = to_i915(dev);
9e2c8475 9532 struct dpll reduced_clock;
7ed9f894 9533 bool has_reduced_clock = false;
e2b78267 9534 struct intel_shared_dpll *pll;
1b6f4958 9535 const struct intel_limit *limit;
997c030c 9536 int refclk = 120000;
de13a2e3 9537
dd3cd74a
ACO
9538 memset(&crtc_state->dpll_hw_state, 0,
9539 sizeof(crtc_state->dpll_hw_state));
9540
ded220e2
ACO
9541 crtc->lowfreq_avail = false;
9542
9543 /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
9544 if (!crtc_state->has_pch_encoder)
9545 return 0;
79e53945 9546
2d84d2b3 9547 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
997c030c
ACO
9548 if (intel_panel_use_ssc(dev_priv)) {
9549 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
9550 dev_priv->vbt.lvds_ssc_freq);
9551 refclk = dev_priv->vbt.lvds_ssc_freq;
9552 }
9553
9554 if (intel_is_dual_link_lvds(dev)) {
9555 if (refclk == 100000)
9556 limit = &intel_limits_ironlake_dual_lvds_100m;
9557 else
9558 limit = &intel_limits_ironlake_dual_lvds;
9559 } else {
9560 if (refclk == 100000)
9561 limit = &intel_limits_ironlake_single_lvds_100m;
9562 else
9563 limit = &intel_limits_ironlake_single_lvds;
9564 }
9565 } else {
9566 limit = &intel_limits_ironlake_dac;
9567 }
9568
364ee29d 9569 if (!crtc_state->clock_set &&
997c030c
ACO
9570 !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
9571 refclk, NULL, &crtc_state->dpll)) {
364ee29d
ACO
9572 DRM_ERROR("Couldn't find PLL settings for mode!\n");
9573 return -EINVAL;
f47709a9 9574 }
79e53945 9575
b75ca6f6
ACO
9576 ironlake_compute_dpll(crtc, crtc_state,
9577 has_reduced_clock ? &reduced_clock : NULL);
66e985c0 9578
ded220e2
ACO
9579 pll = intel_get_shared_dpll(crtc, crtc_state, NULL);
9580 if (pll == NULL) {
9581 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
9582 pipe_name(crtc->pipe));
9583 return -EINVAL;
3fb37703 9584 }
79e53945 9585
2d84d2b3 9586 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
ded220e2 9587 has_reduced_clock)
c7653199 9588 crtc->lowfreq_avail = true;
e2b78267 9589
c8f7a0db 9590 return 0;
79e53945
JB
9591}
9592
eb14cb74
VS
9593static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
9594 struct intel_link_m_n *m_n)
9595{
9596 struct drm_device *dev = crtc->base.dev;
fac5e23e 9597 struct drm_i915_private *dev_priv = to_i915(dev);
eb14cb74
VS
9598 enum pipe pipe = crtc->pipe;
9599
9600 m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
9601 m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
9602 m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
9603 & ~TU_SIZE_MASK;
9604 m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
9605 m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
9606 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
9607}
9608
9609static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
9610 enum transcoder transcoder,
b95af8be
VK
9611 struct intel_link_m_n *m_n,
9612 struct intel_link_m_n *m2_n2)
72419203
DV
9613{
9614 struct drm_device *dev = crtc->base.dev;
fac5e23e 9615 struct drm_i915_private *dev_priv = to_i915(dev);
eb14cb74 9616 enum pipe pipe = crtc->pipe;
72419203 9617
eb14cb74
VS
9618 if (INTEL_INFO(dev)->gen >= 5) {
9619 m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
9620 m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
9621 m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
9622 & ~TU_SIZE_MASK;
9623 m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
9624 m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
9625 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
b95af8be
VK
9626 /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
9627 * gen < 8) and if DRRS is supported (to make sure the
9628 * registers are not unnecessarily read).
9629 */
9630 if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
6e3c9717 9631 crtc->config->has_drrs) {
b95af8be
VK
9632 m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
9633 m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
9634 m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
9635 & ~TU_SIZE_MASK;
9636 m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
9637 m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
9638 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
9639 }
eb14cb74
VS
9640 } else {
9641 m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
9642 m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
9643 m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
9644 & ~TU_SIZE_MASK;
9645 m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
9646 m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
9647 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
9648 }
9649}
9650
9651void intel_dp_get_m_n(struct intel_crtc *crtc,
5cec258b 9652 struct intel_crtc_state *pipe_config)
eb14cb74 9653{
681a8504 9654 if (pipe_config->has_pch_encoder)
eb14cb74
VS
9655 intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
9656 else
9657 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
b95af8be
VK
9658 &pipe_config->dp_m_n,
9659 &pipe_config->dp_m2_n2);
eb14cb74 9660}
72419203 9661
eb14cb74 9662static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
5cec258b 9663 struct intel_crtc_state *pipe_config)
eb14cb74
VS
9664{
9665 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
b95af8be 9666 &pipe_config->fdi_m_n, NULL);
72419203
DV
9667}
9668
bd2e244f 9669static void skylake_get_pfit_config(struct intel_crtc *crtc,
5cec258b 9670 struct intel_crtc_state *pipe_config)
bd2e244f
JB
9671{
9672 struct drm_device *dev = crtc->base.dev;
fac5e23e 9673 struct drm_i915_private *dev_priv = to_i915(dev);
a1b2278e
CK
9674 struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
9675 uint32_t ps_ctrl = 0;
9676 int id = -1;
9677 int i;
bd2e244f 9678
a1b2278e
CK
9679 /* find scaler attached to this pipe */
9680 for (i = 0; i < crtc->num_scalers; i++) {
9681 ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
9682 if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
9683 id = i;
9684 pipe_config->pch_pfit.enabled = true;
9685 pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
9686 pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
9687 break;
9688 }
9689 }
bd2e244f 9690
a1b2278e
CK
9691 scaler_state->scaler_id = id;
9692 if (id >= 0) {
9693 scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
9694 } else {
9695 scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
bd2e244f
JB
9696 }
9697}
9698
5724dbd1
DL
9699static void
9700skylake_get_initial_plane_config(struct intel_crtc *crtc,
9701 struct intel_initial_plane_config *plane_config)
bc8d7dff
DL
9702{
9703 struct drm_device *dev = crtc->base.dev;
fac5e23e 9704 struct drm_i915_private *dev_priv = to_i915(dev);
40f46283 9705 u32 val, base, offset, stride_mult, tiling;
bc8d7dff
DL
9706 int pipe = crtc->pipe;
9707 int fourcc, pixel_format;
6761dd31 9708 unsigned int aligned_height;
bc8d7dff 9709 struct drm_framebuffer *fb;
1b842c89 9710 struct intel_framebuffer *intel_fb;
bc8d7dff 9711
d9806c9f 9712 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
1b842c89 9713 if (!intel_fb) {
bc8d7dff
DL
9714 DRM_DEBUG_KMS("failed to alloc fb\n");
9715 return;
9716 }
9717
1b842c89
DL
9718 fb = &intel_fb->base;
9719
bc8d7dff 9720 val = I915_READ(PLANE_CTL(pipe, 0));
42a7b088
DL
9721 if (!(val & PLANE_CTL_ENABLE))
9722 goto error;
9723
bc8d7dff
DL
9724 pixel_format = val & PLANE_CTL_FORMAT_MASK;
9725 fourcc = skl_format_to_fourcc(pixel_format,
9726 val & PLANE_CTL_ORDER_RGBX,
9727 val & PLANE_CTL_ALPHA_MASK);
9728 fb->pixel_format = fourcc;
9729 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
9730
40f46283
DL
9731 tiling = val & PLANE_CTL_TILED_MASK;
9732 switch (tiling) {
9733 case PLANE_CTL_TILED_LINEAR:
9734 fb->modifier[0] = DRM_FORMAT_MOD_NONE;
9735 break;
9736 case PLANE_CTL_TILED_X:
9737 plane_config->tiling = I915_TILING_X;
9738 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
9739 break;
9740 case PLANE_CTL_TILED_Y:
9741 fb->modifier[0] = I915_FORMAT_MOD_Y_TILED;
9742 break;
9743 case PLANE_CTL_TILED_YF:
9744 fb->modifier[0] = I915_FORMAT_MOD_Yf_TILED;
9745 break;
9746 default:
9747 MISSING_CASE(tiling);
9748 goto error;
9749 }
9750
bc8d7dff
DL
9751 base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
9752 plane_config->base = base;
9753
9754 offset = I915_READ(PLANE_OFFSET(pipe, 0));
9755
9756 val = I915_READ(PLANE_SIZE(pipe, 0));
9757 fb->height = ((val >> 16) & 0xfff) + 1;
9758 fb->width = ((val >> 0) & 0x1fff) + 1;
9759
9760 val = I915_READ(PLANE_STRIDE(pipe, 0));
7b49f948 9761 stride_mult = intel_fb_stride_alignment(dev_priv, fb->modifier[0],
40f46283 9762 fb->pixel_format);
bc8d7dff
DL
9763 fb->pitches[0] = (val & 0x3ff) * stride_mult;
9764
9765 aligned_height = intel_fb_align_height(dev, fb->height,
091df6cb
DV
9766 fb->pixel_format,
9767 fb->modifier[0]);
bc8d7dff 9768
f37b5c2b 9769 plane_config->size = fb->pitches[0] * aligned_height;
bc8d7dff
DL
9770
9771 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9772 pipe_name(pipe), fb->width, fb->height,
9773 fb->bits_per_pixel, base, fb->pitches[0],
9774 plane_config->size);
9775
2d14030b 9776 plane_config->fb = intel_fb;
bc8d7dff
DL
9777 return;
9778
9779error:
d1a3a036 9780 kfree(intel_fb);
bc8d7dff
DL
9781}
9782
2fa2fe9a 9783static void ironlake_get_pfit_config(struct intel_crtc *crtc,
5cec258b 9784 struct intel_crtc_state *pipe_config)
2fa2fe9a
DV
9785{
9786 struct drm_device *dev = crtc->base.dev;
fac5e23e 9787 struct drm_i915_private *dev_priv = to_i915(dev);
2fa2fe9a
DV
9788 uint32_t tmp;
9789
9790 tmp = I915_READ(PF_CTL(crtc->pipe));
9791
9792 if (tmp & PF_ENABLE) {
fd4daa9c 9793 pipe_config->pch_pfit.enabled = true;
2fa2fe9a
DV
9794 pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
9795 pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
cb8b2a30
DV
9796
9797 /* We currently do not free assignements of panel fitters on
9798 * ivb/hsw (since we don't use the higher upscaling modes which
9799 * differentiates them) so just WARN about this case for now. */
9800 if (IS_GEN7(dev)) {
9801 WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
9802 PF_PIPE_SEL_IVB(crtc->pipe));
9803 }
2fa2fe9a 9804 }
79e53945
JB
9805}
9806
5724dbd1
DL
9807static void
9808ironlake_get_initial_plane_config(struct intel_crtc *crtc,
9809 struct intel_initial_plane_config *plane_config)
4c6baa59
JB
9810{
9811 struct drm_device *dev = crtc->base.dev;
fac5e23e 9812 struct drm_i915_private *dev_priv = to_i915(dev);
4c6baa59 9813 u32 val, base, offset;
aeee5a49 9814 int pipe = crtc->pipe;
4c6baa59 9815 int fourcc, pixel_format;
6761dd31 9816 unsigned int aligned_height;
b113d5ee 9817 struct drm_framebuffer *fb;
1b842c89 9818 struct intel_framebuffer *intel_fb;
4c6baa59 9819
42a7b088
DL
9820 val = I915_READ(DSPCNTR(pipe));
9821 if (!(val & DISPLAY_PLANE_ENABLE))
9822 return;
9823
d9806c9f 9824 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
1b842c89 9825 if (!intel_fb) {
4c6baa59
JB
9826 DRM_DEBUG_KMS("failed to alloc fb\n");
9827 return;
9828 }
9829
1b842c89
DL
9830 fb = &intel_fb->base;
9831
18c5247e
DV
9832 if (INTEL_INFO(dev)->gen >= 4) {
9833 if (val & DISPPLANE_TILED) {
49af449b 9834 plane_config->tiling = I915_TILING_X;
18c5247e
DV
9835 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
9836 }
9837 }
4c6baa59
JB
9838
9839 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
b35d63fa 9840 fourcc = i9xx_format_to_fourcc(pixel_format);
b113d5ee
DL
9841 fb->pixel_format = fourcc;
9842 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
4c6baa59 9843
aeee5a49 9844 base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
4c6baa59 9845 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
aeee5a49 9846 offset = I915_READ(DSPOFFSET(pipe));
4c6baa59 9847 } else {
49af449b 9848 if (plane_config->tiling)
aeee5a49 9849 offset = I915_READ(DSPTILEOFF(pipe));
4c6baa59 9850 else
aeee5a49 9851 offset = I915_READ(DSPLINOFF(pipe));
4c6baa59
JB
9852 }
9853 plane_config->base = base;
9854
9855 val = I915_READ(PIPESRC(pipe));
b113d5ee
DL
9856 fb->width = ((val >> 16) & 0xfff) + 1;
9857 fb->height = ((val >> 0) & 0xfff) + 1;
4c6baa59
JB
9858
9859 val = I915_READ(DSPSTRIDE(pipe));
b113d5ee 9860 fb->pitches[0] = val & 0xffffffc0;
4c6baa59 9861
b113d5ee 9862 aligned_height = intel_fb_align_height(dev, fb->height,
091df6cb
DV
9863 fb->pixel_format,
9864 fb->modifier[0]);
4c6baa59 9865
f37b5c2b 9866 plane_config->size = fb->pitches[0] * aligned_height;
4c6baa59 9867
2844a921
DL
9868 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9869 pipe_name(pipe), fb->width, fb->height,
9870 fb->bits_per_pixel, base, fb->pitches[0],
9871 plane_config->size);
b113d5ee 9872
2d14030b 9873 plane_config->fb = intel_fb;
4c6baa59
JB
9874}
9875
0e8ffe1b 9876static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
5cec258b 9877 struct intel_crtc_state *pipe_config)
0e8ffe1b
DV
9878{
9879 struct drm_device *dev = crtc->base.dev;
fac5e23e 9880 struct drm_i915_private *dev_priv = to_i915(dev);
1729050e 9881 enum intel_display_power_domain power_domain;
0e8ffe1b 9882 uint32_t tmp;
1729050e 9883 bool ret;
0e8ffe1b 9884
1729050e
ID
9885 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
9886 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
930e8c9e
PZ
9887 return false;
9888
e143a21c 9889 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
8106ddbd 9890 pipe_config->shared_dpll = NULL;
eccb140b 9891
1729050e 9892 ret = false;
0e8ffe1b
DV
9893 tmp = I915_READ(PIPECONF(crtc->pipe));
9894 if (!(tmp & PIPECONF_ENABLE))
1729050e 9895 goto out;
0e8ffe1b 9896
42571aef
VS
9897 switch (tmp & PIPECONF_BPC_MASK) {
9898 case PIPECONF_6BPC:
9899 pipe_config->pipe_bpp = 18;
9900 break;
9901 case PIPECONF_8BPC:
9902 pipe_config->pipe_bpp = 24;
9903 break;
9904 case PIPECONF_10BPC:
9905 pipe_config->pipe_bpp = 30;
9906 break;
9907 case PIPECONF_12BPC:
9908 pipe_config->pipe_bpp = 36;
9909 break;
9910 default:
9911 break;
9912 }
9913
b5a9fa09
DV
9914 if (tmp & PIPECONF_COLOR_RANGE_SELECT)
9915 pipe_config->limited_color_range = true;
9916
ab9412ba 9917 if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
66e985c0 9918 struct intel_shared_dpll *pll;
8106ddbd 9919 enum intel_dpll_id pll_id;
66e985c0 9920
88adfff1
DV
9921 pipe_config->has_pch_encoder = true;
9922
627eb5a3
DV
9923 tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
9924 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
9925 FDI_DP_PORT_WIDTH_SHIFT) + 1;
72419203
DV
9926
9927 ironlake_get_fdi_m_n_config(crtc, pipe_config);
6c49f241 9928
2d1fe073 9929 if (HAS_PCH_IBX(dev_priv)) {
d9a7bc67
ID
9930 /*
9931 * The pipe->pch transcoder and pch transcoder->pll
9932 * mapping is fixed.
9933 */
8106ddbd 9934 pll_id = (enum intel_dpll_id) crtc->pipe;
c0d43d62
DV
9935 } else {
9936 tmp = I915_READ(PCH_DPLL_SEL);
9937 if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
8106ddbd 9938 pll_id = DPLL_ID_PCH_PLL_B;
c0d43d62 9939 else
8106ddbd 9940 pll_id= DPLL_ID_PCH_PLL_A;
c0d43d62 9941 }
66e985c0 9942
8106ddbd
ACO
9943 pipe_config->shared_dpll =
9944 intel_get_shared_dpll_by_id(dev_priv, pll_id);
9945 pll = pipe_config->shared_dpll;
66e985c0 9946
2edd6443
ACO
9947 WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
9948 &pipe_config->dpll_hw_state));
c93f54cf
DV
9949
9950 tmp = pipe_config->dpll_hw_state.dpll;
9951 pipe_config->pixel_multiplier =
9952 ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
9953 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
18442d08
VS
9954
9955 ironlake_pch_clock_get(crtc, pipe_config);
6c49f241
DV
9956 } else {
9957 pipe_config->pixel_multiplier = 1;
627eb5a3
DV
9958 }
9959
1bd1bd80 9960 intel_get_pipe_timings(crtc, pipe_config);
bc58be60 9961 intel_get_pipe_src_size(crtc, pipe_config);
1bd1bd80 9962
2fa2fe9a
DV
9963 ironlake_get_pfit_config(crtc, pipe_config);
9964
1729050e
ID
9965 ret = true;
9966
9967out:
9968 intel_display_power_put(dev_priv, power_domain);
9969
9970 return ret;
0e8ffe1b
DV
9971}
9972
be256dc7
PZ
9973static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
9974{
91c8a326 9975 struct drm_device *dev = &dev_priv->drm;
be256dc7 9976 struct intel_crtc *crtc;
be256dc7 9977
d3fcc808 9978 for_each_intel_crtc(dev, crtc)
e2c719b7 9979 I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
be256dc7
PZ
9980 pipe_name(crtc->pipe));
9981
e2c719b7
RC
9982 I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
9983 I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
01403de3
VS
9984 I915_STATE_WARN(I915_READ(WRPLL_CTL(0)) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
9985 I915_STATE_WARN(I915_READ(WRPLL_CTL(1)) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
44cb734c 9986 I915_STATE_WARN(I915_READ(PP_STATUS(0)) & PP_ON, "Panel power on\n");
e2c719b7 9987 I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
be256dc7 9988 "CPU PWM1 enabled\n");
c5107b87 9989 if (IS_HASWELL(dev))
e2c719b7 9990 I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
c5107b87 9991 "CPU PWM2 enabled\n");
e2c719b7 9992 I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
be256dc7 9993 "PCH PWM1 enabled\n");
e2c719b7 9994 I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
be256dc7 9995 "Utility pin enabled\n");
e2c719b7 9996 I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
be256dc7 9997
9926ada1
PZ
9998 /*
9999 * In theory we can still leave IRQs enabled, as long as only the HPD
10000 * interrupts remain enabled. We used to check for that, but since it's
10001 * gen-specific and since we only disable LCPLL after we fully disable
10002 * the interrupts, the check below should be enough.
10003 */
e2c719b7 10004 I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
be256dc7
PZ
10005}
10006
9ccd5aeb
PZ
10007static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
10008{
91c8a326 10009 struct drm_device *dev = &dev_priv->drm;
9ccd5aeb
PZ
10010
10011 if (IS_HASWELL(dev))
10012 return I915_READ(D_COMP_HSW);
10013 else
10014 return I915_READ(D_COMP_BDW);
10015}
10016
3c4c9b81
PZ
10017static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
10018{
91c8a326 10019 struct drm_device *dev = &dev_priv->drm;
3c4c9b81
PZ
10020
10021 if (IS_HASWELL(dev)) {
10022 mutex_lock(&dev_priv->rps.hw_lock);
10023 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
10024 val))
79cf219a 10025 DRM_DEBUG_KMS("Failed to write to D_COMP\n");
3c4c9b81
PZ
10026 mutex_unlock(&dev_priv->rps.hw_lock);
10027 } else {
9ccd5aeb
PZ
10028 I915_WRITE(D_COMP_BDW, val);
10029 POSTING_READ(D_COMP_BDW);
3c4c9b81 10030 }
be256dc7
PZ
10031}
10032
10033/*
10034 * This function implements pieces of two sequences from BSpec:
10035 * - Sequence for display software to disable LCPLL
10036 * - Sequence for display software to allow package C8+
10037 * The steps implemented here are just the steps that actually touch the LCPLL
10038 * register. Callers should take care of disabling all the display engine
10039 * functions, doing the mode unset, fixing interrupts, etc.
10040 */
6ff58d53
PZ
10041static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
10042 bool switch_to_fclk, bool allow_power_down)
be256dc7
PZ
10043{
10044 uint32_t val;
10045
10046 assert_can_disable_lcpll(dev_priv);
10047
10048 val = I915_READ(LCPLL_CTL);
10049
10050 if (switch_to_fclk) {
10051 val |= LCPLL_CD_SOURCE_FCLK;
10052 I915_WRITE(LCPLL_CTL, val);
10053
f53dd63f
ID
10054 if (wait_for_us(I915_READ(LCPLL_CTL) &
10055 LCPLL_CD_SOURCE_FCLK_DONE, 1))
be256dc7
PZ
10056 DRM_ERROR("Switching to FCLK failed\n");
10057
10058 val = I915_READ(LCPLL_CTL);
10059 }
10060
10061 val |= LCPLL_PLL_DISABLE;
10062 I915_WRITE(LCPLL_CTL, val);
10063 POSTING_READ(LCPLL_CTL);
10064
24d8441d 10065 if (intel_wait_for_register(dev_priv, LCPLL_CTL, LCPLL_PLL_LOCK, 0, 1))
be256dc7
PZ
10066 DRM_ERROR("LCPLL still locked\n");
10067
9ccd5aeb 10068 val = hsw_read_dcomp(dev_priv);
be256dc7 10069 val |= D_COMP_COMP_DISABLE;
3c4c9b81 10070 hsw_write_dcomp(dev_priv, val);
be256dc7
PZ
10071 ndelay(100);
10072
9ccd5aeb
PZ
10073 if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
10074 1))
be256dc7
PZ
10075 DRM_ERROR("D_COMP RCOMP still in progress\n");
10076
10077 if (allow_power_down) {
10078 val = I915_READ(LCPLL_CTL);
10079 val |= LCPLL_POWER_DOWN_ALLOW;
10080 I915_WRITE(LCPLL_CTL, val);
10081 POSTING_READ(LCPLL_CTL);
10082 }
10083}
10084
10085/*
10086 * Fully restores LCPLL, disallowing power down and switching back to LCPLL
10087 * source.
10088 */
6ff58d53 10089static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
be256dc7
PZ
10090{
10091 uint32_t val;
10092
10093 val = I915_READ(LCPLL_CTL);
10094
10095 if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
10096 LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
10097 return;
10098
a8a8bd54
PZ
10099 /*
10100 * Make sure we're not on PC8 state before disabling PC8, otherwise
10101 * we'll hang the machine. To prevent PC8 state, just enable force_wake.
a8a8bd54 10102 */
59bad947 10103 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
215733fa 10104
be256dc7
PZ
10105 if (val & LCPLL_POWER_DOWN_ALLOW) {
10106 val &= ~LCPLL_POWER_DOWN_ALLOW;
10107 I915_WRITE(LCPLL_CTL, val);
35d8f2eb 10108 POSTING_READ(LCPLL_CTL);
be256dc7
PZ
10109 }
10110
9ccd5aeb 10111 val = hsw_read_dcomp(dev_priv);
be256dc7
PZ
10112 val |= D_COMP_COMP_FORCE;
10113 val &= ~D_COMP_COMP_DISABLE;
3c4c9b81 10114 hsw_write_dcomp(dev_priv, val);
be256dc7
PZ
10115
10116 val = I915_READ(LCPLL_CTL);
10117 val &= ~LCPLL_PLL_DISABLE;
10118 I915_WRITE(LCPLL_CTL, val);
10119
93220c08
CW
10120 if (intel_wait_for_register(dev_priv,
10121 LCPLL_CTL, LCPLL_PLL_LOCK, LCPLL_PLL_LOCK,
10122 5))
be256dc7
PZ
10123 DRM_ERROR("LCPLL not locked yet\n");
10124
10125 if (val & LCPLL_CD_SOURCE_FCLK) {
10126 val = I915_READ(LCPLL_CTL);
10127 val &= ~LCPLL_CD_SOURCE_FCLK;
10128 I915_WRITE(LCPLL_CTL, val);
10129
f53dd63f
ID
10130 if (wait_for_us((I915_READ(LCPLL_CTL) &
10131 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
be256dc7
PZ
10132 DRM_ERROR("Switching back to LCPLL failed\n");
10133 }
215733fa 10134
59bad947 10135 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
91c8a326 10136 intel_update_cdclk(&dev_priv->drm);
be256dc7
PZ
10137}
10138
765dab67
PZ
10139/*
10140 * Package states C8 and deeper are really deep PC states that can only be
10141 * reached when all the devices on the system allow it, so even if the graphics
10142 * device allows PC8+, it doesn't mean the system will actually get to these
10143 * states. Our driver only allows PC8+ when going into runtime PM.
10144 *
10145 * The requirements for PC8+ are that all the outputs are disabled, the power
10146 * well is disabled and most interrupts are disabled, and these are also
10147 * requirements for runtime PM. When these conditions are met, we manually do
10148 * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
10149 * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
10150 * hang the machine.
10151 *
10152 * When we really reach PC8 or deeper states (not just when we allow it) we lose
10153 * the state of some registers, so when we come back from PC8+ we need to
10154 * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
10155 * need to take care of the registers kept by RC6. Notice that this happens even
10156 * if we don't put the device in PCI D3 state (which is what currently happens
10157 * because of the runtime PM support).
10158 *
10159 * For more, read "Display Sequences for Package C8" on the hardware
10160 * documentation.
10161 */
a14cb6fc 10162void hsw_enable_pc8(struct drm_i915_private *dev_priv)
c67a470b 10163{
91c8a326 10164 struct drm_device *dev = &dev_priv->drm;
c67a470b
PZ
10165 uint32_t val;
10166
c67a470b
PZ
10167 DRM_DEBUG_KMS("Enabling package C8+\n");
10168
c2699524 10169 if (HAS_PCH_LPT_LP(dev)) {
c67a470b
PZ
10170 val = I915_READ(SOUTH_DSPCLK_GATE_D);
10171 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
10172 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
10173 }
10174
10175 lpt_disable_clkout_dp(dev);
c67a470b
PZ
10176 hsw_disable_lcpll(dev_priv, true, true);
10177}
10178
a14cb6fc 10179void hsw_disable_pc8(struct drm_i915_private *dev_priv)
c67a470b 10180{
91c8a326 10181 struct drm_device *dev = &dev_priv->drm;
c67a470b
PZ
10182 uint32_t val;
10183
c67a470b
PZ
10184 DRM_DEBUG_KMS("Disabling package C8+\n");
10185
10186 hsw_restore_lcpll(dev_priv);
c67a470b
PZ
10187 lpt_init_pch_refclk(dev);
10188
c2699524 10189 if (HAS_PCH_LPT_LP(dev)) {
c67a470b
PZ
10190 val = I915_READ(SOUTH_DSPCLK_GATE_D);
10191 val |= PCH_LP_PARTITION_LEVEL_DISABLE;
10192 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
10193 }
c67a470b
PZ
10194}
10195
324513c0 10196static void bxt_modeset_commit_cdclk(struct drm_atomic_state *old_state)
f8437dd1 10197{
a821fc46 10198 struct drm_device *dev = old_state->dev;
1a617b77
ML
10199 struct intel_atomic_state *old_intel_state =
10200 to_intel_atomic_state(old_state);
10201 unsigned int req_cdclk = old_intel_state->dev_cdclk;
f8437dd1 10202
324513c0 10203 bxt_set_cdclk(to_i915(dev), req_cdclk);
f8437dd1
VK
10204}
10205
b432e5cf 10206/* compute the max rate for new configuration */
27c329ed 10207static int ilk_max_pixel_rate(struct drm_atomic_state *state)
b432e5cf 10208{
565602d7 10209 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
fac5e23e 10210 struct drm_i915_private *dev_priv = to_i915(state->dev);
565602d7
ML
10211 struct drm_crtc *crtc;
10212 struct drm_crtc_state *cstate;
27c329ed 10213 struct intel_crtc_state *crtc_state;
565602d7
ML
10214 unsigned max_pixel_rate = 0, i;
10215 enum pipe pipe;
b432e5cf 10216
565602d7
ML
10217 memcpy(intel_state->min_pixclk, dev_priv->min_pixclk,
10218 sizeof(intel_state->min_pixclk));
27c329ed 10219
565602d7
ML
10220 for_each_crtc_in_state(state, crtc, cstate, i) {
10221 int pixel_rate;
27c329ed 10222
565602d7
ML
10223 crtc_state = to_intel_crtc_state(cstate);
10224 if (!crtc_state->base.enable) {
10225 intel_state->min_pixclk[i] = 0;
b432e5cf 10226 continue;
565602d7 10227 }
b432e5cf 10228
27c329ed 10229 pixel_rate = ilk_pipe_pixel_rate(crtc_state);
b432e5cf
VS
10230
10231 /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
565602d7 10232 if (IS_BROADWELL(dev_priv) && crtc_state->ips_enabled)
b432e5cf
VS
10233 pixel_rate = DIV_ROUND_UP(pixel_rate * 100, 95);
10234
565602d7 10235 intel_state->min_pixclk[i] = pixel_rate;
b432e5cf
VS
10236 }
10237
565602d7
ML
10238 for_each_pipe(dev_priv, pipe)
10239 max_pixel_rate = max(intel_state->min_pixclk[pipe], max_pixel_rate);
10240
b432e5cf
VS
10241 return max_pixel_rate;
10242}
10243
10244static void broadwell_set_cdclk(struct drm_device *dev, int cdclk)
10245{
fac5e23e 10246 struct drm_i915_private *dev_priv = to_i915(dev);
b432e5cf
VS
10247 uint32_t val, data;
10248 int ret;
10249
10250 if (WARN((I915_READ(LCPLL_CTL) &
10251 (LCPLL_PLL_DISABLE | LCPLL_PLL_LOCK |
10252 LCPLL_CD_CLOCK_DISABLE | LCPLL_ROOT_CD_CLOCK_DISABLE |
10253 LCPLL_CD2X_CLOCK_DISABLE | LCPLL_POWER_DOWN_ALLOW |
10254 LCPLL_CD_SOURCE_FCLK)) != LCPLL_PLL_LOCK,
10255 "trying to change cdclk frequency with cdclk not enabled\n"))
10256 return;
10257
10258 mutex_lock(&dev_priv->rps.hw_lock);
10259 ret = sandybridge_pcode_write(dev_priv,
10260 BDW_PCODE_DISPLAY_FREQ_CHANGE_REQ, 0x0);
10261 mutex_unlock(&dev_priv->rps.hw_lock);
10262 if (ret) {
10263 DRM_ERROR("failed to inform pcode about cdclk change\n");
10264 return;
10265 }
10266
10267 val = I915_READ(LCPLL_CTL);
10268 val |= LCPLL_CD_SOURCE_FCLK;
10269 I915_WRITE(LCPLL_CTL, val);
10270
5ba00178
TU
10271 if (wait_for_us(I915_READ(LCPLL_CTL) &
10272 LCPLL_CD_SOURCE_FCLK_DONE, 1))
b432e5cf
VS
10273 DRM_ERROR("Switching to FCLK failed\n");
10274
10275 val = I915_READ(LCPLL_CTL);
10276 val &= ~LCPLL_CLK_FREQ_MASK;
10277
10278 switch (cdclk) {
10279 case 450000:
10280 val |= LCPLL_CLK_FREQ_450;
10281 data = 0;
10282 break;
10283 case 540000:
10284 val |= LCPLL_CLK_FREQ_54O_BDW;
10285 data = 1;
10286 break;
10287 case 337500:
10288 val |= LCPLL_CLK_FREQ_337_5_BDW;
10289 data = 2;
10290 break;
10291 case 675000:
10292 val |= LCPLL_CLK_FREQ_675_BDW;
10293 data = 3;
10294 break;
10295 default:
10296 WARN(1, "invalid cdclk frequency\n");
10297 return;
10298 }
10299
10300 I915_WRITE(LCPLL_CTL, val);
10301
10302 val = I915_READ(LCPLL_CTL);
10303 val &= ~LCPLL_CD_SOURCE_FCLK;
10304 I915_WRITE(LCPLL_CTL, val);
10305
5ba00178
TU
10306 if (wait_for_us((I915_READ(LCPLL_CTL) &
10307 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
b432e5cf
VS
10308 DRM_ERROR("Switching back to LCPLL failed\n");
10309
10310 mutex_lock(&dev_priv->rps.hw_lock);
10311 sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ, data);
10312 mutex_unlock(&dev_priv->rps.hw_lock);
10313
7f1052a8
VS
10314 I915_WRITE(CDCLK_FREQ, DIV_ROUND_CLOSEST(cdclk, 1000) - 1);
10315
b432e5cf
VS
10316 intel_update_cdclk(dev);
10317
10318 WARN(cdclk != dev_priv->cdclk_freq,
10319 "cdclk requested %d kHz but got %d kHz\n",
10320 cdclk, dev_priv->cdclk_freq);
10321}
10322
587c7914
VS
10323static int broadwell_calc_cdclk(int max_pixclk)
10324{
10325 if (max_pixclk > 540000)
10326 return 675000;
10327 else if (max_pixclk > 450000)
10328 return 540000;
10329 else if (max_pixclk > 337500)
10330 return 450000;
10331 else
10332 return 337500;
10333}
10334
27c329ed 10335static int broadwell_modeset_calc_cdclk(struct drm_atomic_state *state)
b432e5cf 10336{
27c329ed 10337 struct drm_i915_private *dev_priv = to_i915(state->dev);
1a617b77 10338 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
27c329ed 10339 int max_pixclk = ilk_max_pixel_rate(state);
b432e5cf
VS
10340 int cdclk;
10341
10342 /*
10343 * FIXME should also account for plane ratio
10344 * once 64bpp pixel formats are supported.
10345 */
587c7914 10346 cdclk = broadwell_calc_cdclk(max_pixclk);
b432e5cf 10347
b432e5cf 10348 if (cdclk > dev_priv->max_cdclk_freq) {
63ba534e
ML
10349 DRM_DEBUG_KMS("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
10350 cdclk, dev_priv->max_cdclk_freq);
10351 return -EINVAL;
b432e5cf
VS
10352 }
10353
1a617b77
ML
10354 intel_state->cdclk = intel_state->dev_cdclk = cdclk;
10355 if (!intel_state->active_crtcs)
587c7914 10356 intel_state->dev_cdclk = broadwell_calc_cdclk(0);
b432e5cf
VS
10357
10358 return 0;
10359}
10360
27c329ed 10361static void broadwell_modeset_commit_cdclk(struct drm_atomic_state *old_state)
b432e5cf 10362{
27c329ed 10363 struct drm_device *dev = old_state->dev;
1a617b77
ML
10364 struct intel_atomic_state *old_intel_state =
10365 to_intel_atomic_state(old_state);
10366 unsigned req_cdclk = old_intel_state->dev_cdclk;
b432e5cf 10367
27c329ed 10368 broadwell_set_cdclk(dev, req_cdclk);
b432e5cf
VS
10369}
10370
c89e39f3
CT
10371static int skl_modeset_calc_cdclk(struct drm_atomic_state *state)
10372{
10373 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
10374 struct drm_i915_private *dev_priv = to_i915(state->dev);
10375 const int max_pixclk = ilk_max_pixel_rate(state);
a8ca4934 10376 int vco = intel_state->cdclk_pll_vco;
c89e39f3
CT
10377 int cdclk;
10378
10379 /*
10380 * FIXME should also account for plane ratio
10381 * once 64bpp pixel formats are supported.
10382 */
a8ca4934 10383 cdclk = skl_calc_cdclk(max_pixclk, vco);
c89e39f3
CT
10384
10385 /*
10386 * FIXME move the cdclk caclulation to
10387 * compute_config() so we can fail gracegully.
10388 */
10389 if (cdclk > dev_priv->max_cdclk_freq) {
10390 DRM_ERROR("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
10391 cdclk, dev_priv->max_cdclk_freq);
10392 cdclk = dev_priv->max_cdclk_freq;
10393 }
10394
10395 intel_state->cdclk = intel_state->dev_cdclk = cdclk;
10396 if (!intel_state->active_crtcs)
a8ca4934 10397 intel_state->dev_cdclk = skl_calc_cdclk(0, vco);
c89e39f3
CT
10398
10399 return 0;
10400}
10401
10402static void skl_modeset_commit_cdclk(struct drm_atomic_state *old_state)
10403{
1cd593e0
VS
10404 struct drm_i915_private *dev_priv = to_i915(old_state->dev);
10405 struct intel_atomic_state *intel_state = to_intel_atomic_state(old_state);
10406 unsigned int req_cdclk = intel_state->dev_cdclk;
10407 unsigned int req_vco = intel_state->cdclk_pll_vco;
c89e39f3 10408
1cd593e0 10409 skl_set_cdclk(dev_priv, req_cdclk, req_vco);
c89e39f3
CT
10410}
10411
190f68c5
ACO
10412static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
10413 struct intel_crtc_state *crtc_state)
09b4ddf9 10414{
d7edc4e5 10415 if (!intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DSI)) {
af3997b5
MK
10416 if (!intel_ddi_pll_select(crtc, crtc_state))
10417 return -EINVAL;
10418 }
716c2e55 10419
c7653199 10420 crtc->lowfreq_avail = false;
644cef34 10421
c8f7a0db 10422 return 0;
79e53945
JB
10423}
10424
3760b59c
S
10425static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
10426 enum port port,
10427 struct intel_crtc_state *pipe_config)
10428{
8106ddbd
ACO
10429 enum intel_dpll_id id;
10430
3760b59c
S
10431 switch (port) {
10432 case PORT_A:
10433 pipe_config->ddi_pll_sel = SKL_DPLL0;
08250c4b 10434 id = DPLL_ID_SKL_DPLL0;
3760b59c
S
10435 break;
10436 case PORT_B:
10437 pipe_config->ddi_pll_sel = SKL_DPLL1;
08250c4b 10438 id = DPLL_ID_SKL_DPLL1;
3760b59c
S
10439 break;
10440 case PORT_C:
10441 pipe_config->ddi_pll_sel = SKL_DPLL2;
08250c4b 10442 id = DPLL_ID_SKL_DPLL2;
3760b59c
S
10443 break;
10444 default:
10445 DRM_ERROR("Incorrect port type\n");
8106ddbd 10446 return;
3760b59c 10447 }
8106ddbd
ACO
10448
10449 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
3760b59c
S
10450}
10451
96b7dfb7
S
10452static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
10453 enum port port,
5cec258b 10454 struct intel_crtc_state *pipe_config)
96b7dfb7 10455{
8106ddbd 10456 enum intel_dpll_id id;
a3c988ea 10457 u32 temp;
96b7dfb7
S
10458
10459 temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
10460 pipe_config->ddi_pll_sel = temp >> (port * 3 + 1);
10461
10462 switch (pipe_config->ddi_pll_sel) {
3148ade7 10463 case SKL_DPLL0:
a3c988ea
ACO
10464 id = DPLL_ID_SKL_DPLL0;
10465 break;
96b7dfb7 10466 case SKL_DPLL1:
8106ddbd 10467 id = DPLL_ID_SKL_DPLL1;
96b7dfb7
S
10468 break;
10469 case SKL_DPLL2:
8106ddbd 10470 id = DPLL_ID_SKL_DPLL2;
96b7dfb7
S
10471 break;
10472 case SKL_DPLL3:
8106ddbd 10473 id = DPLL_ID_SKL_DPLL3;
96b7dfb7 10474 break;
8106ddbd
ACO
10475 default:
10476 MISSING_CASE(pipe_config->ddi_pll_sel);
10477 return;
96b7dfb7 10478 }
8106ddbd
ACO
10479
10480 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
96b7dfb7
S
10481}
10482
7d2c8175
DL
10483static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
10484 enum port port,
5cec258b 10485 struct intel_crtc_state *pipe_config)
7d2c8175 10486{
8106ddbd
ACO
10487 enum intel_dpll_id id;
10488
7d2c8175
DL
10489 pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
10490
10491 switch (pipe_config->ddi_pll_sel) {
10492 case PORT_CLK_SEL_WRPLL1:
8106ddbd 10493 id = DPLL_ID_WRPLL1;
7d2c8175
DL
10494 break;
10495 case PORT_CLK_SEL_WRPLL2:
8106ddbd 10496 id = DPLL_ID_WRPLL2;
7d2c8175 10497 break;
00490c22 10498 case PORT_CLK_SEL_SPLL:
8106ddbd 10499 id = DPLL_ID_SPLL;
79bd23da 10500 break;
9d16da65
ACO
10501 case PORT_CLK_SEL_LCPLL_810:
10502 id = DPLL_ID_LCPLL_810;
10503 break;
10504 case PORT_CLK_SEL_LCPLL_1350:
10505 id = DPLL_ID_LCPLL_1350;
10506 break;
10507 case PORT_CLK_SEL_LCPLL_2700:
10508 id = DPLL_ID_LCPLL_2700;
10509 break;
8106ddbd
ACO
10510 default:
10511 MISSING_CASE(pipe_config->ddi_pll_sel);
10512 /* fall through */
10513 case PORT_CLK_SEL_NONE:
8106ddbd 10514 return;
7d2c8175 10515 }
8106ddbd
ACO
10516
10517 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
7d2c8175
DL
10518}
10519
cf30429e
JN
10520static bool hsw_get_transcoder_state(struct intel_crtc *crtc,
10521 struct intel_crtc_state *pipe_config,
10522 unsigned long *power_domain_mask)
10523{
10524 struct drm_device *dev = crtc->base.dev;
fac5e23e 10525 struct drm_i915_private *dev_priv = to_i915(dev);
cf30429e
JN
10526 enum intel_display_power_domain power_domain;
10527 u32 tmp;
10528
d9a7bc67
ID
10529 /*
10530 * The pipe->transcoder mapping is fixed with the exception of the eDP
10531 * transcoder handled below.
10532 */
cf30429e
JN
10533 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
10534
10535 /*
10536 * XXX: Do intel_display_power_get_if_enabled before reading this (for
10537 * consistency and less surprising code; it's in always on power).
10538 */
10539 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
10540 if (tmp & TRANS_DDI_FUNC_ENABLE) {
10541 enum pipe trans_edp_pipe;
10542 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
10543 default:
10544 WARN(1, "unknown pipe linked to edp transcoder\n");
10545 case TRANS_DDI_EDP_INPUT_A_ONOFF:
10546 case TRANS_DDI_EDP_INPUT_A_ON:
10547 trans_edp_pipe = PIPE_A;
10548 break;
10549 case TRANS_DDI_EDP_INPUT_B_ONOFF:
10550 trans_edp_pipe = PIPE_B;
10551 break;
10552 case TRANS_DDI_EDP_INPUT_C_ONOFF:
10553 trans_edp_pipe = PIPE_C;
10554 break;
10555 }
10556
10557 if (trans_edp_pipe == crtc->pipe)
10558 pipe_config->cpu_transcoder = TRANSCODER_EDP;
10559 }
10560
10561 power_domain = POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder);
10562 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
10563 return false;
10564 *power_domain_mask |= BIT(power_domain);
10565
10566 tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
10567
10568 return tmp & PIPECONF_ENABLE;
10569}
10570
4d1de975
JN
10571static bool bxt_get_dsi_transcoder_state(struct intel_crtc *crtc,
10572 struct intel_crtc_state *pipe_config,
10573 unsigned long *power_domain_mask)
10574{
10575 struct drm_device *dev = crtc->base.dev;
fac5e23e 10576 struct drm_i915_private *dev_priv = to_i915(dev);
4d1de975
JN
10577 enum intel_display_power_domain power_domain;
10578 enum port port;
10579 enum transcoder cpu_transcoder;
10580 u32 tmp;
10581
4d1de975
JN
10582 for_each_port_masked(port, BIT(PORT_A) | BIT(PORT_C)) {
10583 if (port == PORT_A)
10584 cpu_transcoder = TRANSCODER_DSI_A;
10585 else
10586 cpu_transcoder = TRANSCODER_DSI_C;
10587
10588 power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
10589 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
10590 continue;
10591 *power_domain_mask |= BIT(power_domain);
10592
db18b6a6
ID
10593 /*
10594 * The PLL needs to be enabled with a valid divider
10595 * configuration, otherwise accessing DSI registers will hang
10596 * the machine. See BSpec North Display Engine
10597 * registers/MIPI[BXT]. We can break out here early, since we
10598 * need the same DSI PLL to be enabled for both DSI ports.
10599 */
10600 if (!intel_dsi_pll_is_enabled(dev_priv))
10601 break;
10602
4d1de975
JN
10603 /* XXX: this works for video mode only */
10604 tmp = I915_READ(BXT_MIPI_PORT_CTRL(port));
10605 if (!(tmp & DPI_ENABLE))
10606 continue;
10607
10608 tmp = I915_READ(MIPI_CTRL(port));
10609 if ((tmp & BXT_PIPE_SELECT_MASK) != BXT_PIPE_SELECT(crtc->pipe))
10610 continue;
10611
10612 pipe_config->cpu_transcoder = cpu_transcoder;
4d1de975
JN
10613 break;
10614 }
10615
d7edc4e5 10616 return transcoder_is_dsi(pipe_config->cpu_transcoder);
4d1de975
JN
10617}
10618
26804afd 10619static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
5cec258b 10620 struct intel_crtc_state *pipe_config)
26804afd
DV
10621{
10622 struct drm_device *dev = crtc->base.dev;
fac5e23e 10623 struct drm_i915_private *dev_priv = to_i915(dev);
d452c5b6 10624 struct intel_shared_dpll *pll;
26804afd
DV
10625 enum port port;
10626 uint32_t tmp;
10627
10628 tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
10629
10630 port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
10631
ef11bdb3 10632 if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev))
96b7dfb7 10633 skylake_get_ddi_pll(dev_priv, port, pipe_config);
3760b59c
S
10634 else if (IS_BROXTON(dev))
10635 bxt_get_ddi_pll(dev_priv, port, pipe_config);
96b7dfb7
S
10636 else
10637 haswell_get_ddi_pll(dev_priv, port, pipe_config);
9cd86933 10638
8106ddbd
ACO
10639 pll = pipe_config->shared_dpll;
10640 if (pll) {
2edd6443
ACO
10641 WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
10642 &pipe_config->dpll_hw_state));
d452c5b6
DV
10643 }
10644
26804afd
DV
10645 /*
10646 * Haswell has only FDI/PCH transcoder A. It is which is connected to
10647 * DDI E. So just check whether this pipe is wired to DDI E and whether
10648 * the PCH transcoder is on.
10649 */
ca370455
DL
10650 if (INTEL_INFO(dev)->gen < 9 &&
10651 (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
26804afd
DV
10652 pipe_config->has_pch_encoder = true;
10653
10654 tmp = I915_READ(FDI_RX_CTL(PIPE_A));
10655 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
10656 FDI_DP_PORT_WIDTH_SHIFT) + 1;
10657
10658 ironlake_get_fdi_m_n_config(crtc, pipe_config);
10659 }
10660}
10661
0e8ffe1b 10662static bool haswell_get_pipe_config(struct intel_crtc *crtc,
5cec258b 10663 struct intel_crtc_state *pipe_config)
0e8ffe1b
DV
10664{
10665 struct drm_device *dev = crtc->base.dev;
fac5e23e 10666 struct drm_i915_private *dev_priv = to_i915(dev);
1729050e
ID
10667 enum intel_display_power_domain power_domain;
10668 unsigned long power_domain_mask;
cf30429e 10669 bool active;
0e8ffe1b 10670
1729050e
ID
10671 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
10672 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
b5482bd0 10673 return false;
1729050e
ID
10674 power_domain_mask = BIT(power_domain);
10675
8106ddbd 10676 pipe_config->shared_dpll = NULL;
c0d43d62 10677
cf30429e 10678 active = hsw_get_transcoder_state(crtc, pipe_config, &power_domain_mask);
eccb140b 10679
d7edc4e5
VS
10680 if (IS_BROXTON(dev_priv) &&
10681 bxt_get_dsi_transcoder_state(crtc, pipe_config, &power_domain_mask)) {
10682 WARN_ON(active);
10683 active = true;
4d1de975
JN
10684 }
10685
cf30429e 10686 if (!active)
1729050e 10687 goto out;
0e8ffe1b 10688
d7edc4e5 10689 if (!transcoder_is_dsi(pipe_config->cpu_transcoder)) {
4d1de975
JN
10690 haswell_get_ddi_port_state(crtc, pipe_config);
10691 intel_get_pipe_timings(crtc, pipe_config);
10692 }
627eb5a3 10693
bc58be60 10694 intel_get_pipe_src_size(crtc, pipe_config);
1bd1bd80 10695
05dc698c
LL
10696 pipe_config->gamma_mode =
10697 I915_READ(GAMMA_MODE(crtc->pipe)) & GAMMA_MODE_MODE_MASK;
10698
a1b2278e
CK
10699 if (INTEL_INFO(dev)->gen >= 9) {
10700 skl_init_scalers(dev, crtc, pipe_config);
10701 }
10702
af99ceda
CK
10703 if (INTEL_INFO(dev)->gen >= 9) {
10704 pipe_config->scaler_state.scaler_id = -1;
10705 pipe_config->scaler_state.scaler_users &= ~(1 << SKL_CRTC_INDEX);
10706 }
10707
1729050e
ID
10708 power_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
10709 if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
10710 power_domain_mask |= BIT(power_domain);
1c132b44 10711 if (INTEL_INFO(dev)->gen >= 9)
bd2e244f 10712 skylake_get_pfit_config(crtc, pipe_config);
ff6d9f55 10713 else
1c132b44 10714 ironlake_get_pfit_config(crtc, pipe_config);
bd2e244f 10715 }
88adfff1 10716
e59150dc
JB
10717 if (IS_HASWELL(dev))
10718 pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
10719 (I915_READ(IPS_CTL) & IPS_ENABLE);
42db64ef 10720
4d1de975
JN
10721 if (pipe_config->cpu_transcoder != TRANSCODER_EDP &&
10722 !transcoder_is_dsi(pipe_config->cpu_transcoder)) {
ebb69c95
CT
10723 pipe_config->pixel_multiplier =
10724 I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
10725 } else {
10726 pipe_config->pixel_multiplier = 1;
10727 }
6c49f241 10728
1729050e
ID
10729out:
10730 for_each_power_domain(power_domain, power_domain_mask)
10731 intel_display_power_put(dev_priv, power_domain);
10732
cf30429e 10733 return active;
0e8ffe1b
DV
10734}
10735
55a08b3f
ML
10736static void i845_update_cursor(struct drm_crtc *crtc, u32 base,
10737 const struct intel_plane_state *plane_state)
560b85bb
CW
10738{
10739 struct drm_device *dev = crtc->dev;
fac5e23e 10740 struct drm_i915_private *dev_priv = to_i915(dev);
560b85bb 10741 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
dc41c154 10742 uint32_t cntl = 0, size = 0;
560b85bb 10743
936e71e3 10744 if (plane_state && plane_state->base.visible) {
55a08b3f
ML
10745 unsigned int width = plane_state->base.crtc_w;
10746 unsigned int height = plane_state->base.crtc_h;
dc41c154
VS
10747 unsigned int stride = roundup_pow_of_two(width) * 4;
10748
10749 switch (stride) {
10750 default:
10751 WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
10752 width, stride);
10753 stride = 256;
10754 /* fallthrough */
10755 case 256:
10756 case 512:
10757 case 1024:
10758 case 2048:
10759 break;
4b0e333e
CW
10760 }
10761
dc41c154
VS
10762 cntl |= CURSOR_ENABLE |
10763 CURSOR_GAMMA_ENABLE |
10764 CURSOR_FORMAT_ARGB |
10765 CURSOR_STRIDE(stride);
10766
10767 size = (height << 12) | width;
4b0e333e 10768 }
560b85bb 10769
dc41c154
VS
10770 if (intel_crtc->cursor_cntl != 0 &&
10771 (intel_crtc->cursor_base != base ||
10772 intel_crtc->cursor_size != size ||
10773 intel_crtc->cursor_cntl != cntl)) {
10774 /* On these chipsets we can only modify the base/size/stride
10775 * whilst the cursor is disabled.
10776 */
0b87c24e
VS
10777 I915_WRITE(CURCNTR(PIPE_A), 0);
10778 POSTING_READ(CURCNTR(PIPE_A));
dc41c154 10779 intel_crtc->cursor_cntl = 0;
4b0e333e 10780 }
560b85bb 10781
99d1f387 10782 if (intel_crtc->cursor_base != base) {
0b87c24e 10783 I915_WRITE(CURBASE(PIPE_A), base);
99d1f387
VS
10784 intel_crtc->cursor_base = base;
10785 }
4726e0b0 10786
dc41c154
VS
10787 if (intel_crtc->cursor_size != size) {
10788 I915_WRITE(CURSIZE, size);
10789 intel_crtc->cursor_size = size;
4b0e333e 10790 }
560b85bb 10791
4b0e333e 10792 if (intel_crtc->cursor_cntl != cntl) {
0b87c24e
VS
10793 I915_WRITE(CURCNTR(PIPE_A), cntl);
10794 POSTING_READ(CURCNTR(PIPE_A));
4b0e333e 10795 intel_crtc->cursor_cntl = cntl;
560b85bb 10796 }
560b85bb
CW
10797}
10798
55a08b3f
ML
10799static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base,
10800 const struct intel_plane_state *plane_state)
65a21cd6
JB
10801{
10802 struct drm_device *dev = crtc->dev;
fac5e23e 10803 struct drm_i915_private *dev_priv = to_i915(dev);
65a21cd6 10804 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
62e0fb88 10805 const struct skl_wm_values *wm = &dev_priv->wm.skl_results;
65a21cd6 10806 int pipe = intel_crtc->pipe;
663f3122 10807 uint32_t cntl = 0;
4b0e333e 10808
62e0fb88
L
10809 if (INTEL_GEN(dev_priv) >= 9 && wm->dirty_pipes & drm_crtc_mask(crtc))
10810 skl_write_cursor_wm(intel_crtc, wm);
10811
936e71e3 10812 if (plane_state && plane_state->base.visible) {
4b0e333e 10813 cntl = MCURSOR_GAMMA_ENABLE;
55a08b3f 10814 switch (plane_state->base.crtc_w) {
4726e0b0
SK
10815 case 64:
10816 cntl |= CURSOR_MODE_64_ARGB_AX;
10817 break;
10818 case 128:
10819 cntl |= CURSOR_MODE_128_ARGB_AX;
10820 break;
10821 case 256:
10822 cntl |= CURSOR_MODE_256_ARGB_AX;
10823 break;
10824 default:
55a08b3f 10825 MISSING_CASE(plane_state->base.crtc_w);
4726e0b0 10826 return;
65a21cd6 10827 }
4b0e333e 10828 cntl |= pipe << 28; /* Connect to correct pipe */
47bf17a7 10829
fc6f93bc 10830 if (HAS_DDI(dev))
47bf17a7 10831 cntl |= CURSOR_PIPE_CSC_ENABLE;
65a21cd6 10832
31ad61e4 10833 if (plane_state->base.rotation == DRM_ROTATE_180)
55a08b3f
ML
10834 cntl |= CURSOR_ROTATE_180;
10835 }
4398ad45 10836
4b0e333e
CW
10837 if (intel_crtc->cursor_cntl != cntl) {
10838 I915_WRITE(CURCNTR(pipe), cntl);
10839 POSTING_READ(CURCNTR(pipe));
10840 intel_crtc->cursor_cntl = cntl;
65a21cd6 10841 }
4b0e333e 10842
65a21cd6 10843 /* and commit changes on next vblank */
5efb3e28
VS
10844 I915_WRITE(CURBASE(pipe), base);
10845 POSTING_READ(CURBASE(pipe));
99d1f387
VS
10846
10847 intel_crtc->cursor_base = base;
65a21cd6
JB
10848}
10849
cda4b7d3 10850/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
6b383a7f 10851static void intel_crtc_update_cursor(struct drm_crtc *crtc,
55a08b3f 10852 const struct intel_plane_state *plane_state)
cda4b7d3
CW
10853{
10854 struct drm_device *dev = crtc->dev;
fac5e23e 10855 struct drm_i915_private *dev_priv = to_i915(dev);
cda4b7d3
CW
10856 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10857 int pipe = intel_crtc->pipe;
55a08b3f
ML
10858 u32 base = intel_crtc->cursor_addr;
10859 u32 pos = 0;
cda4b7d3 10860
55a08b3f
ML
10861 if (plane_state) {
10862 int x = plane_state->base.crtc_x;
10863 int y = plane_state->base.crtc_y;
cda4b7d3 10864
55a08b3f
ML
10865 if (x < 0) {
10866 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
10867 x = -x;
10868 }
10869 pos |= x << CURSOR_X_SHIFT;
cda4b7d3 10870
55a08b3f
ML
10871 if (y < 0) {
10872 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
10873 y = -y;
10874 }
10875 pos |= y << CURSOR_Y_SHIFT;
10876
10877 /* ILK+ do this automagically */
10878 if (HAS_GMCH_DISPLAY(dev) &&
31ad61e4 10879 plane_state->base.rotation == DRM_ROTATE_180) {
55a08b3f
ML
10880 base += (plane_state->base.crtc_h *
10881 plane_state->base.crtc_w - 1) * 4;
10882 }
cda4b7d3 10883 }
cda4b7d3 10884
5efb3e28
VS
10885 I915_WRITE(CURPOS(pipe), pos);
10886
8ac54669 10887 if (IS_845G(dev) || IS_I865G(dev))
55a08b3f 10888 i845_update_cursor(crtc, base, plane_state);
5efb3e28 10889 else
55a08b3f 10890 i9xx_update_cursor(crtc, base, plane_state);
cda4b7d3
CW
10891}
10892
dc41c154
VS
10893static bool cursor_size_ok(struct drm_device *dev,
10894 uint32_t width, uint32_t height)
10895{
10896 if (width == 0 || height == 0)
10897 return false;
10898
10899 /*
10900 * 845g/865g are special in that they are only limited by
10901 * the width of their cursors, the height is arbitrary up to
10902 * the precision of the register. Everything else requires
10903 * square cursors, limited to a few power-of-two sizes.
10904 */
10905 if (IS_845G(dev) || IS_I865G(dev)) {
10906 if ((width & 63) != 0)
10907 return false;
10908
10909 if (width > (IS_845G(dev) ? 64 : 512))
10910 return false;
10911
10912 if (height > 1023)
10913 return false;
10914 } else {
10915 switch (width | height) {
10916 case 256:
10917 case 128:
10918 if (IS_GEN2(dev))
10919 return false;
10920 case 64:
10921 break;
10922 default:
10923 return false;
10924 }
10925 }
10926
10927 return true;
10928}
10929
79e53945
JB
10930/* VESA 640x480x72Hz mode to set on the pipe */
10931static struct drm_display_mode load_detect_mode = {
10932 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
10933 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
10934};
10935
a8bb6818
DV
10936struct drm_framebuffer *
10937__intel_framebuffer_create(struct drm_device *dev,
10938 struct drm_mode_fb_cmd2 *mode_cmd,
10939 struct drm_i915_gem_object *obj)
d2dff872
CW
10940{
10941 struct intel_framebuffer *intel_fb;
10942 int ret;
10943
10944 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
dcb1394e 10945 if (!intel_fb)
d2dff872 10946 return ERR_PTR(-ENOMEM);
d2dff872
CW
10947
10948 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
dd4916c5
DV
10949 if (ret)
10950 goto err;
d2dff872
CW
10951
10952 return &intel_fb->base;
dcb1394e 10953
dd4916c5 10954err:
dd4916c5 10955 kfree(intel_fb);
dd4916c5 10956 return ERR_PTR(ret);
d2dff872
CW
10957}
10958
b5ea642a 10959static struct drm_framebuffer *
a8bb6818
DV
10960intel_framebuffer_create(struct drm_device *dev,
10961 struct drm_mode_fb_cmd2 *mode_cmd,
10962 struct drm_i915_gem_object *obj)
10963{
10964 struct drm_framebuffer *fb;
10965 int ret;
10966
10967 ret = i915_mutex_lock_interruptible(dev);
10968 if (ret)
10969 return ERR_PTR(ret);
10970 fb = __intel_framebuffer_create(dev, mode_cmd, obj);
10971 mutex_unlock(&dev->struct_mutex);
10972
10973 return fb;
10974}
10975
d2dff872
CW
10976static u32
10977intel_framebuffer_pitch_for_width(int width, int bpp)
10978{
10979 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
10980 return ALIGN(pitch, 64);
10981}
10982
10983static u32
10984intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
10985{
10986 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
1267a26b 10987 return PAGE_ALIGN(pitch * mode->vdisplay);
d2dff872
CW
10988}
10989
10990static struct drm_framebuffer *
10991intel_framebuffer_create_for_mode(struct drm_device *dev,
10992 struct drm_display_mode *mode,
10993 int depth, int bpp)
10994{
dcb1394e 10995 struct drm_framebuffer *fb;
d2dff872 10996 struct drm_i915_gem_object *obj;
0fed39bd 10997 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
d2dff872 10998
d37cd8a8 10999 obj = i915_gem_object_create(dev,
d2dff872 11000 intel_framebuffer_size_for_mode(mode, bpp));
fe3db79b
CW
11001 if (IS_ERR(obj))
11002 return ERR_CAST(obj);
d2dff872
CW
11003
11004 mode_cmd.width = mode->hdisplay;
11005 mode_cmd.height = mode->vdisplay;
308e5bcb
JB
11006 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
11007 bpp);
5ca0c34a 11008 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
d2dff872 11009
dcb1394e
LW
11010 fb = intel_framebuffer_create(dev, &mode_cmd, obj);
11011 if (IS_ERR(fb))
34911fd3 11012 i915_gem_object_put_unlocked(obj);
dcb1394e
LW
11013
11014 return fb;
d2dff872
CW
11015}
11016
11017static struct drm_framebuffer *
11018mode_fits_in_fbdev(struct drm_device *dev,
11019 struct drm_display_mode *mode)
11020{
0695726e 11021#ifdef CONFIG_DRM_FBDEV_EMULATION
fac5e23e 11022 struct drm_i915_private *dev_priv = to_i915(dev);
d2dff872
CW
11023 struct drm_i915_gem_object *obj;
11024 struct drm_framebuffer *fb;
11025
4c0e5528 11026 if (!dev_priv->fbdev)
d2dff872
CW
11027 return NULL;
11028
4c0e5528 11029 if (!dev_priv->fbdev->fb)
d2dff872
CW
11030 return NULL;
11031
4c0e5528
DV
11032 obj = dev_priv->fbdev->fb->obj;
11033 BUG_ON(!obj);
11034
8bcd4553 11035 fb = &dev_priv->fbdev->fb->base;
01f2c773
VS
11036 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
11037 fb->bits_per_pixel))
d2dff872
CW
11038 return NULL;
11039
01f2c773 11040 if (obj->base.size < mode->vdisplay * fb->pitches[0])
d2dff872
CW
11041 return NULL;
11042
edde3617 11043 drm_framebuffer_reference(fb);
d2dff872 11044 return fb;
4520f53a
DV
11045#else
11046 return NULL;
11047#endif
d2dff872
CW
11048}
11049
d3a40d1b
ACO
11050static int intel_modeset_setup_plane_state(struct drm_atomic_state *state,
11051 struct drm_crtc *crtc,
11052 struct drm_display_mode *mode,
11053 struct drm_framebuffer *fb,
11054 int x, int y)
11055{
11056 struct drm_plane_state *plane_state;
11057 int hdisplay, vdisplay;
11058 int ret;
11059
11060 plane_state = drm_atomic_get_plane_state(state, crtc->primary);
11061 if (IS_ERR(plane_state))
11062 return PTR_ERR(plane_state);
11063
11064 if (mode)
11065 drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
11066 else
11067 hdisplay = vdisplay = 0;
11068
11069 ret = drm_atomic_set_crtc_for_plane(plane_state, fb ? crtc : NULL);
11070 if (ret)
11071 return ret;
11072 drm_atomic_set_fb_for_plane(plane_state, fb);
11073 plane_state->crtc_x = 0;
11074 plane_state->crtc_y = 0;
11075 plane_state->crtc_w = hdisplay;
11076 plane_state->crtc_h = vdisplay;
11077 plane_state->src_x = x << 16;
11078 plane_state->src_y = y << 16;
11079 plane_state->src_w = hdisplay << 16;
11080 plane_state->src_h = vdisplay << 16;
11081
11082 return 0;
11083}
11084
d2434ab7 11085bool intel_get_load_detect_pipe(struct drm_connector *connector,
7173188d 11086 struct drm_display_mode *mode,
51fd371b
RC
11087 struct intel_load_detect_pipe *old,
11088 struct drm_modeset_acquire_ctx *ctx)
79e53945
JB
11089{
11090 struct intel_crtc *intel_crtc;
d2434ab7
DV
11091 struct intel_encoder *intel_encoder =
11092 intel_attached_encoder(connector);
79e53945 11093 struct drm_crtc *possible_crtc;
4ef69c7a 11094 struct drm_encoder *encoder = &intel_encoder->base;
79e53945
JB
11095 struct drm_crtc *crtc = NULL;
11096 struct drm_device *dev = encoder->dev;
94352cf9 11097 struct drm_framebuffer *fb;
51fd371b 11098 struct drm_mode_config *config = &dev->mode_config;
edde3617 11099 struct drm_atomic_state *state = NULL, *restore_state = NULL;
944b0c76 11100 struct drm_connector_state *connector_state;
4be07317 11101 struct intel_crtc_state *crtc_state;
51fd371b 11102 int ret, i = -1;
79e53945 11103
d2dff872 11104 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
c23cc417 11105 connector->base.id, connector->name,
8e329a03 11106 encoder->base.id, encoder->name);
d2dff872 11107
edde3617
ML
11108 old->restore_state = NULL;
11109
51fd371b
RC
11110retry:
11111 ret = drm_modeset_lock(&config->connection_mutex, ctx);
11112 if (ret)
ad3c558f 11113 goto fail;
6e9f798d 11114
79e53945
JB
11115 /*
11116 * Algorithm gets a little messy:
7a5e4805 11117 *
79e53945
JB
11118 * - if the connector already has an assigned crtc, use it (but make
11119 * sure it's on first)
7a5e4805 11120 *
79e53945
JB
11121 * - try to find the first unused crtc that can drive this connector,
11122 * and use that if we find one
79e53945
JB
11123 */
11124
11125 /* See if we already have a CRTC for this connector */
edde3617
ML
11126 if (connector->state->crtc) {
11127 crtc = connector->state->crtc;
8261b191 11128
51fd371b 11129 ret = drm_modeset_lock(&crtc->mutex, ctx);
4d02e2de 11130 if (ret)
ad3c558f 11131 goto fail;
8261b191
CW
11132
11133 /* Make sure the crtc and connector are running */
edde3617 11134 goto found;
79e53945
JB
11135 }
11136
11137 /* Find an unused one (if possible) */
70e1e0ec 11138 for_each_crtc(dev, possible_crtc) {
79e53945
JB
11139 i++;
11140 if (!(encoder->possible_crtcs & (1 << i)))
11141 continue;
edde3617
ML
11142
11143 ret = drm_modeset_lock(&possible_crtc->mutex, ctx);
11144 if (ret)
11145 goto fail;
11146
11147 if (possible_crtc->state->enable) {
11148 drm_modeset_unlock(&possible_crtc->mutex);
a459249c 11149 continue;
edde3617 11150 }
a459249c
VS
11151
11152 crtc = possible_crtc;
11153 break;
79e53945
JB
11154 }
11155
11156 /*
11157 * If we didn't find an unused CRTC, don't use any.
11158 */
11159 if (!crtc) {
7173188d 11160 DRM_DEBUG_KMS("no pipe available for load-detect\n");
ad3c558f 11161 goto fail;
79e53945
JB
11162 }
11163
edde3617
ML
11164found:
11165 intel_crtc = to_intel_crtc(crtc);
11166
4d02e2de
DV
11167 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
11168 if (ret)
ad3c558f 11169 goto fail;
79e53945 11170
83a57153 11171 state = drm_atomic_state_alloc(dev);
edde3617
ML
11172 restore_state = drm_atomic_state_alloc(dev);
11173 if (!state || !restore_state) {
11174 ret = -ENOMEM;
11175 goto fail;
11176 }
83a57153
ACO
11177
11178 state->acquire_ctx = ctx;
edde3617 11179 restore_state->acquire_ctx = ctx;
83a57153 11180
944b0c76
ACO
11181 connector_state = drm_atomic_get_connector_state(state, connector);
11182 if (IS_ERR(connector_state)) {
11183 ret = PTR_ERR(connector_state);
11184 goto fail;
11185 }
11186
edde3617
ML
11187 ret = drm_atomic_set_crtc_for_connector(connector_state, crtc);
11188 if (ret)
11189 goto fail;
944b0c76 11190
4be07317
ACO
11191 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
11192 if (IS_ERR(crtc_state)) {
11193 ret = PTR_ERR(crtc_state);
11194 goto fail;
11195 }
11196
49d6fa21 11197 crtc_state->base.active = crtc_state->base.enable = true;
4be07317 11198
6492711d
CW
11199 if (!mode)
11200 mode = &load_detect_mode;
79e53945 11201
d2dff872
CW
11202 /* We need a framebuffer large enough to accommodate all accesses
11203 * that the plane may generate whilst we perform load detection.
11204 * We can not rely on the fbcon either being present (we get called
11205 * during its initialisation to detect all boot displays, or it may
11206 * not even exist) or that it is large enough to satisfy the
11207 * requested mode.
11208 */
94352cf9
DV
11209 fb = mode_fits_in_fbdev(dev, mode);
11210 if (fb == NULL) {
d2dff872 11211 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
94352cf9 11212 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
d2dff872
CW
11213 } else
11214 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
94352cf9 11215 if (IS_ERR(fb)) {
d2dff872 11216 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
412b61d8 11217 goto fail;
79e53945 11218 }
79e53945 11219
d3a40d1b
ACO
11220 ret = intel_modeset_setup_plane_state(state, crtc, mode, fb, 0, 0);
11221 if (ret)
11222 goto fail;
11223
edde3617
ML
11224 drm_framebuffer_unreference(fb);
11225
11226 ret = drm_atomic_set_mode_for_crtc(&crtc_state->base, mode);
11227 if (ret)
11228 goto fail;
11229
11230 ret = PTR_ERR_OR_ZERO(drm_atomic_get_connector_state(restore_state, connector));
11231 if (!ret)
11232 ret = PTR_ERR_OR_ZERO(drm_atomic_get_crtc_state(restore_state, crtc));
11233 if (!ret)
11234 ret = PTR_ERR_OR_ZERO(drm_atomic_get_plane_state(restore_state, crtc->primary));
11235 if (ret) {
11236 DRM_DEBUG_KMS("Failed to create a copy of old state to restore: %i\n", ret);
11237 goto fail;
11238 }
8c7b5ccb 11239
3ba86073
ML
11240 ret = drm_atomic_commit(state);
11241 if (ret) {
6492711d 11242 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
412b61d8 11243 goto fail;
79e53945 11244 }
edde3617
ML
11245
11246 old->restore_state = restore_state;
7173188d 11247
79e53945 11248 /* let the connector get through one full cycle before testing */
9d0498a2 11249 intel_wait_for_vblank(dev, intel_crtc->pipe);
7173188d 11250 return true;
412b61d8 11251
ad3c558f 11252fail:
e5d958ef 11253 drm_atomic_state_free(state);
edde3617
ML
11254 drm_atomic_state_free(restore_state);
11255 restore_state = state = NULL;
83a57153 11256
51fd371b
RC
11257 if (ret == -EDEADLK) {
11258 drm_modeset_backoff(ctx);
11259 goto retry;
11260 }
11261
412b61d8 11262 return false;
79e53945
JB
11263}
11264
d2434ab7 11265void intel_release_load_detect_pipe(struct drm_connector *connector,
49172fee
ACO
11266 struct intel_load_detect_pipe *old,
11267 struct drm_modeset_acquire_ctx *ctx)
79e53945 11268{
d2434ab7
DV
11269 struct intel_encoder *intel_encoder =
11270 intel_attached_encoder(connector);
4ef69c7a 11271 struct drm_encoder *encoder = &intel_encoder->base;
edde3617 11272 struct drm_atomic_state *state = old->restore_state;
d3a40d1b 11273 int ret;
79e53945 11274
d2dff872 11275 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
c23cc417 11276 connector->base.id, connector->name,
8e329a03 11277 encoder->base.id, encoder->name);
d2dff872 11278
edde3617 11279 if (!state)
0622a53c 11280 return;
79e53945 11281
edde3617
ML
11282 ret = drm_atomic_commit(state);
11283 if (ret) {
11284 DRM_DEBUG_KMS("Couldn't release load detect pipe: %i\n", ret);
11285 drm_atomic_state_free(state);
11286 }
79e53945
JB
11287}
11288
da4a1efa 11289static int i9xx_pll_refclk(struct drm_device *dev,
5cec258b 11290 const struct intel_crtc_state *pipe_config)
da4a1efa 11291{
fac5e23e 11292 struct drm_i915_private *dev_priv = to_i915(dev);
da4a1efa
VS
11293 u32 dpll = pipe_config->dpll_hw_state.dpll;
11294
11295 if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
e91e941b 11296 return dev_priv->vbt.lvds_ssc_freq;
da4a1efa
VS
11297 else if (HAS_PCH_SPLIT(dev))
11298 return 120000;
11299 else if (!IS_GEN2(dev))
11300 return 96000;
11301 else
11302 return 48000;
11303}
11304
79e53945 11305/* Returns the clock of the currently programmed mode of the given pipe. */
f1f644dc 11306static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 11307 struct intel_crtc_state *pipe_config)
79e53945 11308{
f1f644dc 11309 struct drm_device *dev = crtc->base.dev;
fac5e23e 11310 struct drm_i915_private *dev_priv = to_i915(dev);
f1f644dc 11311 int pipe = pipe_config->cpu_transcoder;
293623f7 11312 u32 dpll = pipe_config->dpll_hw_state.dpll;
79e53945 11313 u32 fp;
9e2c8475 11314 struct dpll clock;
dccbea3b 11315 int port_clock;
da4a1efa 11316 int refclk = i9xx_pll_refclk(dev, pipe_config);
79e53945
JB
11317
11318 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
293623f7 11319 fp = pipe_config->dpll_hw_state.fp0;
79e53945 11320 else
293623f7 11321 fp = pipe_config->dpll_hw_state.fp1;
79e53945
JB
11322
11323 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
f2b115e6
AJ
11324 if (IS_PINEVIEW(dev)) {
11325 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
11326 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
2177832f
SL
11327 } else {
11328 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
11329 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
11330 }
11331
a6c45cf0 11332 if (!IS_GEN2(dev)) {
f2b115e6
AJ
11333 if (IS_PINEVIEW(dev))
11334 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
11335 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
2177832f
SL
11336 else
11337 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
79e53945
JB
11338 DPLL_FPA01_P1_POST_DIV_SHIFT);
11339
11340 switch (dpll & DPLL_MODE_MASK) {
11341 case DPLLB_MODE_DAC_SERIAL:
11342 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
11343 5 : 10;
11344 break;
11345 case DPLLB_MODE_LVDS:
11346 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
11347 7 : 14;
11348 break;
11349 default:
28c97730 11350 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
79e53945 11351 "mode\n", (int)(dpll & DPLL_MODE_MASK));
f1f644dc 11352 return;
79e53945
JB
11353 }
11354
ac58c3f0 11355 if (IS_PINEVIEW(dev))
dccbea3b 11356 port_clock = pnv_calc_dpll_params(refclk, &clock);
ac58c3f0 11357 else
dccbea3b 11358 port_clock = i9xx_calc_dpll_params(refclk, &clock);
79e53945 11359 } else {
0fb58223 11360 u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
b1c560d1 11361 bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
79e53945
JB
11362
11363 if (is_lvds) {
11364 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
11365 DPLL_FPA01_P1_POST_DIV_SHIFT);
b1c560d1
VS
11366
11367 if (lvds & LVDS_CLKB_POWER_UP)
11368 clock.p2 = 7;
11369 else
11370 clock.p2 = 14;
79e53945
JB
11371 } else {
11372 if (dpll & PLL_P1_DIVIDE_BY_TWO)
11373 clock.p1 = 2;
11374 else {
11375 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
11376 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
11377 }
11378 if (dpll & PLL_P2_DIVIDE_BY_4)
11379 clock.p2 = 4;
11380 else
11381 clock.p2 = 2;
79e53945 11382 }
da4a1efa 11383
dccbea3b 11384 port_clock = i9xx_calc_dpll_params(refclk, &clock);
79e53945
JB
11385 }
11386
18442d08
VS
11387 /*
11388 * This value includes pixel_multiplier. We will use
241bfc38 11389 * port_clock to compute adjusted_mode.crtc_clock in the
18442d08
VS
11390 * encoder's get_config() function.
11391 */
dccbea3b 11392 pipe_config->port_clock = port_clock;
f1f644dc
JB
11393}
11394
6878da05
VS
11395int intel_dotclock_calculate(int link_freq,
11396 const struct intel_link_m_n *m_n)
f1f644dc 11397{
f1f644dc
JB
11398 /*
11399 * The calculation for the data clock is:
1041a02f 11400 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
f1f644dc 11401 * But we want to avoid losing precison if possible, so:
1041a02f 11402 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
f1f644dc
JB
11403 *
11404 * and the link clock is simpler:
1041a02f 11405 * link_clock = (m * link_clock) / n
f1f644dc
JB
11406 */
11407
6878da05
VS
11408 if (!m_n->link_n)
11409 return 0;
f1f644dc 11410
6878da05
VS
11411 return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
11412}
f1f644dc 11413
18442d08 11414static void ironlake_pch_clock_get(struct intel_crtc *crtc,
5cec258b 11415 struct intel_crtc_state *pipe_config)
6878da05 11416{
e3b247da 11417 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
79e53945 11418
18442d08
VS
11419 /* read out port_clock from the DPLL */
11420 i9xx_crtc_clock_get(crtc, pipe_config);
f1f644dc 11421
f1f644dc 11422 /*
e3b247da
VS
11423 * In case there is an active pipe without active ports,
11424 * we may need some idea for the dotclock anyway.
11425 * Calculate one based on the FDI configuration.
79e53945 11426 */
2d112de7 11427 pipe_config->base.adjusted_mode.crtc_clock =
21a727b3 11428 intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
18442d08 11429 &pipe_config->fdi_m_n);
79e53945
JB
11430}
11431
11432/** Returns the currently programmed mode of the given pipe. */
11433struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
11434 struct drm_crtc *crtc)
11435{
fac5e23e 11436 struct drm_i915_private *dev_priv = to_i915(dev);
79e53945 11437 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6e3c9717 11438 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
79e53945 11439 struct drm_display_mode *mode;
3f36b937 11440 struct intel_crtc_state *pipe_config;
fe2b8f9d
PZ
11441 int htot = I915_READ(HTOTAL(cpu_transcoder));
11442 int hsync = I915_READ(HSYNC(cpu_transcoder));
11443 int vtot = I915_READ(VTOTAL(cpu_transcoder));
11444 int vsync = I915_READ(VSYNC(cpu_transcoder));
293623f7 11445 enum pipe pipe = intel_crtc->pipe;
79e53945
JB
11446
11447 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
11448 if (!mode)
11449 return NULL;
11450
3f36b937
TU
11451 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
11452 if (!pipe_config) {
11453 kfree(mode);
11454 return NULL;
11455 }
11456
f1f644dc
JB
11457 /*
11458 * Construct a pipe_config sufficient for getting the clock info
11459 * back out of crtc_clock_get.
11460 *
11461 * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
11462 * to use a real value here instead.
11463 */
3f36b937
TU
11464 pipe_config->cpu_transcoder = (enum transcoder) pipe;
11465 pipe_config->pixel_multiplier = 1;
11466 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(pipe));
11467 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(pipe));
11468 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(pipe));
11469 i9xx_crtc_clock_get(intel_crtc, pipe_config);
11470
11471 mode->clock = pipe_config->port_clock / pipe_config->pixel_multiplier;
79e53945
JB
11472 mode->hdisplay = (htot & 0xffff) + 1;
11473 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
11474 mode->hsync_start = (hsync & 0xffff) + 1;
11475 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
11476 mode->vdisplay = (vtot & 0xffff) + 1;
11477 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
11478 mode->vsync_start = (vsync & 0xffff) + 1;
11479 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
11480
11481 drm_mode_set_name(mode);
79e53945 11482
3f36b937
TU
11483 kfree(pipe_config);
11484
79e53945
JB
11485 return mode;
11486}
11487
11488static void intel_crtc_destroy(struct drm_crtc *crtc)
11489{
11490 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
67e77c5a 11491 struct drm_device *dev = crtc->dev;
51cbaf01 11492 struct intel_flip_work *work;
67e77c5a 11493
5e2d7afc 11494 spin_lock_irq(&dev->event_lock);
5a21b665
DV
11495 work = intel_crtc->flip_work;
11496 intel_crtc->flip_work = NULL;
11497 spin_unlock_irq(&dev->event_lock);
67e77c5a 11498
5a21b665 11499 if (work) {
51cbaf01
ML
11500 cancel_work_sync(&work->mmio_work);
11501 cancel_work_sync(&work->unpin_work);
5a21b665 11502 kfree(work);
67e77c5a 11503 }
79e53945
JB
11504
11505 drm_crtc_cleanup(crtc);
67e77c5a 11506
79e53945
JB
11507 kfree(intel_crtc);
11508}
11509
6b95a207
KH
11510static void intel_unpin_work_fn(struct work_struct *__work)
11511{
51cbaf01
ML
11512 struct intel_flip_work *work =
11513 container_of(__work, struct intel_flip_work, unpin_work);
5a21b665
DV
11514 struct intel_crtc *crtc = to_intel_crtc(work->crtc);
11515 struct drm_device *dev = crtc->base.dev;
11516 struct drm_plane *primary = crtc->base.primary;
03f476e1 11517
5a21b665
DV
11518 if (is_mmio_work(work))
11519 flush_work(&work->mmio_work);
03f476e1 11520
5a21b665
DV
11521 mutex_lock(&dev->struct_mutex);
11522 intel_unpin_fb_obj(work->old_fb, primary->state->rotation);
f8c417cd 11523 i915_gem_object_put(work->pending_flip_obj);
5a21b665 11524 mutex_unlock(&dev->struct_mutex);
143f73b3 11525
e8a261ea
CW
11526 i915_gem_request_put(work->flip_queued_req);
11527
5748b6a1
CW
11528 intel_frontbuffer_flip_complete(to_i915(dev),
11529 to_intel_plane(primary)->frontbuffer_bit);
5a21b665
DV
11530 intel_fbc_post_update(crtc);
11531 drm_framebuffer_unreference(work->old_fb);
143f73b3 11532
5a21b665
DV
11533 BUG_ON(atomic_read(&crtc->unpin_work_count) == 0);
11534 atomic_dec(&crtc->unpin_work_count);
a6747b73 11535
5a21b665
DV
11536 kfree(work);
11537}
d9e86c0e 11538
5a21b665
DV
11539/* Is 'a' after or equal to 'b'? */
11540static bool g4x_flip_count_after_eq(u32 a, u32 b)
11541{
11542 return !((a - b) & 0x80000000);
11543}
143f73b3 11544
5a21b665
DV
11545static bool __pageflip_finished_cs(struct intel_crtc *crtc,
11546 struct intel_flip_work *work)
11547{
11548 struct drm_device *dev = crtc->base.dev;
fac5e23e 11549 struct drm_i915_private *dev_priv = to_i915(dev);
5a21b665 11550 unsigned reset_counter;
143f73b3 11551
5a21b665
DV
11552 reset_counter = i915_reset_counter(&dev_priv->gpu_error);
11553 if (crtc->reset_counter != reset_counter)
11554 return true;
143f73b3 11555
5a21b665
DV
11556 /*
11557 * The relevant registers doen't exist on pre-ctg.
11558 * As the flip done interrupt doesn't trigger for mmio
11559 * flips on gmch platforms, a flip count check isn't
11560 * really needed there. But since ctg has the registers,
11561 * include it in the check anyway.
11562 */
11563 if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
11564 return true;
b4a98e57 11565
5a21b665
DV
11566 /*
11567 * BDW signals flip done immediately if the plane
11568 * is disabled, even if the plane enable is already
11569 * armed to occur at the next vblank :(
11570 */
f99d7069 11571
5a21b665
DV
11572 /*
11573 * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
11574 * used the same base address. In that case the mmio flip might
11575 * have completed, but the CS hasn't even executed the flip yet.
11576 *
11577 * A flip count check isn't enough as the CS might have updated
11578 * the base address just after start of vblank, but before we
11579 * managed to process the interrupt. This means we'd complete the
11580 * CS flip too soon.
11581 *
11582 * Combining both checks should get us a good enough result. It may
11583 * still happen that the CS flip has been executed, but has not
11584 * yet actually completed. But in case the base address is the same
11585 * anyway, we don't really care.
11586 */
11587 return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
11588 crtc->flip_work->gtt_offset &&
11589 g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_G4X(crtc->pipe)),
11590 crtc->flip_work->flip_count);
11591}
b4a98e57 11592
5a21b665
DV
11593static bool
11594__pageflip_finished_mmio(struct intel_crtc *crtc,
11595 struct intel_flip_work *work)
11596{
11597 /*
11598 * MMIO work completes when vblank is different from
11599 * flip_queued_vblank.
11600 *
11601 * Reset counter value doesn't matter, this is handled by
11602 * i915_wait_request finishing early, so no need to handle
11603 * reset here.
11604 */
11605 return intel_crtc_get_vblank_counter(crtc) != work->flip_queued_vblank;
6b95a207
KH
11606}
11607
51cbaf01
ML
11608
11609static bool pageflip_finished(struct intel_crtc *crtc,
11610 struct intel_flip_work *work)
11611{
11612 if (!atomic_read(&work->pending))
11613 return false;
11614
11615 smp_rmb();
11616
5a21b665
DV
11617 if (is_mmio_work(work))
11618 return __pageflip_finished_mmio(crtc, work);
11619 else
11620 return __pageflip_finished_cs(crtc, work);
11621}
11622
11623void intel_finish_page_flip_cs(struct drm_i915_private *dev_priv, int pipe)
11624{
91c8a326 11625 struct drm_device *dev = &dev_priv->drm;
5a21b665
DV
11626 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
11627 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11628 struct intel_flip_work *work;
11629 unsigned long flags;
11630
11631 /* Ignore early vblank irqs */
11632 if (!crtc)
11633 return;
11634
51cbaf01 11635 /*
5a21b665
DV
11636 * This is called both by irq handlers and the reset code (to complete
11637 * lost pageflips) so needs the full irqsave spinlocks.
51cbaf01 11638 */
5a21b665
DV
11639 spin_lock_irqsave(&dev->event_lock, flags);
11640 work = intel_crtc->flip_work;
11641
11642 if (work != NULL &&
11643 !is_mmio_work(work) &&
11644 pageflip_finished(intel_crtc, work))
11645 page_flip_completed(intel_crtc);
11646
11647 spin_unlock_irqrestore(&dev->event_lock, flags);
75f7f3ec
VS
11648}
11649
51cbaf01 11650void intel_finish_page_flip_mmio(struct drm_i915_private *dev_priv, int pipe)
6b95a207 11651{
91c8a326 11652 struct drm_device *dev = &dev_priv->drm;
5251f04e
ML
11653 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
11654 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
51cbaf01 11655 struct intel_flip_work *work;
6b95a207
KH
11656 unsigned long flags;
11657
5251f04e
ML
11658 /* Ignore early vblank irqs */
11659 if (!crtc)
11660 return;
f326038a
DV
11661
11662 /*
11663 * This is called both by irq handlers and the reset code (to complete
11664 * lost pageflips) so needs the full irqsave spinlocks.
e7d841ca 11665 */
6b95a207 11666 spin_lock_irqsave(&dev->event_lock, flags);
5a21b665 11667 work = intel_crtc->flip_work;
5251f04e 11668
5a21b665
DV
11669 if (work != NULL &&
11670 is_mmio_work(work) &&
11671 pageflip_finished(intel_crtc, work))
11672 page_flip_completed(intel_crtc);
5251f04e 11673
6b95a207
KH
11674 spin_unlock_irqrestore(&dev->event_lock, flags);
11675}
11676
5a21b665
DV
11677static inline void intel_mark_page_flip_active(struct intel_crtc *crtc,
11678 struct intel_flip_work *work)
84c33a64 11679{
5a21b665 11680 work->flip_queued_vblank = intel_crtc_get_vblank_counter(crtc);
84c33a64 11681
5a21b665
DV
11682 /* Ensure that the work item is consistent when activating it ... */
11683 smp_mb__before_atomic();
11684 atomic_set(&work->pending, 1);
11685}
a6747b73 11686
5a21b665
DV
11687static int intel_gen2_queue_flip(struct drm_device *dev,
11688 struct drm_crtc *crtc,
11689 struct drm_framebuffer *fb,
11690 struct drm_i915_gem_object *obj,
11691 struct drm_i915_gem_request *req,
11692 uint32_t flags)
11693{
7e37f889 11694 struct intel_ring *ring = req->ring;
5a21b665
DV
11695 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11696 u32 flip_mask;
11697 int ret;
143f73b3 11698
5a21b665
DV
11699 ret = intel_ring_begin(req, 6);
11700 if (ret)
11701 return ret;
143f73b3 11702
5a21b665
DV
11703 /* Can't queue multiple flips, so wait for the previous
11704 * one to finish before executing the next.
11705 */
11706 if (intel_crtc->plane)
11707 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
11708 else
11709 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
b5321f30
CW
11710 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
11711 intel_ring_emit(ring, MI_NOOP);
11712 intel_ring_emit(ring, MI_DISPLAY_FLIP |
5a21b665 11713 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
b5321f30
CW
11714 intel_ring_emit(ring, fb->pitches[0]);
11715 intel_ring_emit(ring, intel_crtc->flip_work->gtt_offset);
11716 intel_ring_emit(ring, 0); /* aux display base address, unused */
143f73b3 11717
5a21b665
DV
11718 return 0;
11719}
84c33a64 11720
5a21b665
DV
11721static int intel_gen3_queue_flip(struct drm_device *dev,
11722 struct drm_crtc *crtc,
11723 struct drm_framebuffer *fb,
11724 struct drm_i915_gem_object *obj,
11725 struct drm_i915_gem_request *req,
11726 uint32_t flags)
11727{
7e37f889 11728 struct intel_ring *ring = req->ring;
5a21b665
DV
11729 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11730 u32 flip_mask;
11731 int ret;
d55dbd06 11732
5a21b665
DV
11733 ret = intel_ring_begin(req, 6);
11734 if (ret)
11735 return ret;
d55dbd06 11736
5a21b665
DV
11737 if (intel_crtc->plane)
11738 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
11739 else
11740 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
b5321f30
CW
11741 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
11742 intel_ring_emit(ring, MI_NOOP);
11743 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
5a21b665 11744 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
b5321f30
CW
11745 intel_ring_emit(ring, fb->pitches[0]);
11746 intel_ring_emit(ring, intel_crtc->flip_work->gtt_offset);
11747 intel_ring_emit(ring, MI_NOOP);
fd8e058a 11748
5a21b665
DV
11749 return 0;
11750}
84c33a64 11751
5a21b665
DV
11752static int intel_gen4_queue_flip(struct drm_device *dev,
11753 struct drm_crtc *crtc,
11754 struct drm_framebuffer *fb,
11755 struct drm_i915_gem_object *obj,
11756 struct drm_i915_gem_request *req,
11757 uint32_t flags)
11758{
7e37f889 11759 struct intel_ring *ring = req->ring;
fac5e23e 11760 struct drm_i915_private *dev_priv = to_i915(dev);
5a21b665
DV
11761 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11762 uint32_t pf, pipesrc;
11763 int ret;
143f73b3 11764
5a21b665
DV
11765 ret = intel_ring_begin(req, 4);
11766 if (ret)
11767 return ret;
143f73b3 11768
5a21b665
DV
11769 /* i965+ uses the linear or tiled offsets from the
11770 * Display Registers (which do not change across a page-flip)
11771 * so we need only reprogram the base address.
11772 */
b5321f30 11773 intel_ring_emit(ring, MI_DISPLAY_FLIP |
5a21b665 11774 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
b5321f30
CW
11775 intel_ring_emit(ring, fb->pitches[0]);
11776 intel_ring_emit(ring, intel_crtc->flip_work->gtt_offset |
72618ebf 11777 intel_fb_modifier_to_tiling(fb->modifier[0]));
5a21b665
DV
11778
11779 /* XXX Enabling the panel-fitter across page-flip is so far
11780 * untested on non-native modes, so ignore it for now.
11781 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
11782 */
11783 pf = 0;
11784 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
b5321f30 11785 intel_ring_emit(ring, pf | pipesrc);
143f73b3 11786
5a21b665 11787 return 0;
8c9f3aaf
JB
11788}
11789
5a21b665
DV
11790static int intel_gen6_queue_flip(struct drm_device *dev,
11791 struct drm_crtc *crtc,
11792 struct drm_framebuffer *fb,
11793 struct drm_i915_gem_object *obj,
11794 struct drm_i915_gem_request *req,
11795 uint32_t flags)
da20eabd 11796{
7e37f889 11797 struct intel_ring *ring = req->ring;
fac5e23e 11798 struct drm_i915_private *dev_priv = to_i915(dev);
5a21b665
DV
11799 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11800 uint32_t pf, pipesrc;
11801 int ret;
d21fbe87 11802
5a21b665
DV
11803 ret = intel_ring_begin(req, 4);
11804 if (ret)
11805 return ret;
92826fcd 11806
b5321f30 11807 intel_ring_emit(ring, MI_DISPLAY_FLIP |
5a21b665 11808 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
72618ebf
VS
11809 intel_ring_emit(ring, fb->pitches[0] |
11810 intel_fb_modifier_to_tiling(fb->modifier[0]));
b5321f30 11811 intel_ring_emit(ring, intel_crtc->flip_work->gtt_offset);
92826fcd 11812
5a21b665
DV
11813 /* Contrary to the suggestions in the documentation,
11814 * "Enable Panel Fitter" does not seem to be required when page
11815 * flipping with a non-native mode, and worse causes a normal
11816 * modeset to fail.
11817 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
11818 */
11819 pf = 0;
11820 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
b5321f30 11821 intel_ring_emit(ring, pf | pipesrc);
7809e5ae 11822
5a21b665 11823 return 0;
7809e5ae
MR
11824}
11825
5a21b665
DV
11826static int intel_gen7_queue_flip(struct drm_device *dev,
11827 struct drm_crtc *crtc,
11828 struct drm_framebuffer *fb,
11829 struct drm_i915_gem_object *obj,
11830 struct drm_i915_gem_request *req,
11831 uint32_t flags)
d21fbe87 11832{
7e37f889 11833 struct intel_ring *ring = req->ring;
5a21b665
DV
11834 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11835 uint32_t plane_bit = 0;
11836 int len, ret;
d21fbe87 11837
5a21b665
DV
11838 switch (intel_crtc->plane) {
11839 case PLANE_A:
11840 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
11841 break;
11842 case PLANE_B:
11843 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
11844 break;
11845 case PLANE_C:
11846 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
11847 break;
11848 default:
11849 WARN_ONCE(1, "unknown plane in flip command\n");
11850 return -ENODEV;
11851 }
11852
11853 len = 4;
b5321f30 11854 if (req->engine->id == RCS) {
5a21b665
DV
11855 len += 6;
11856 /*
11857 * On Gen 8, SRM is now taking an extra dword to accommodate
11858 * 48bits addresses, and we need a NOOP for the batch size to
11859 * stay even.
11860 */
11861 if (IS_GEN8(dev))
11862 len += 2;
11863 }
11864
11865 /*
11866 * BSpec MI_DISPLAY_FLIP for IVB:
11867 * "The full packet must be contained within the same cache line."
11868 *
11869 * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
11870 * cacheline, if we ever start emitting more commands before
11871 * the MI_DISPLAY_FLIP we may need to first emit everything else,
11872 * then do the cacheline alignment, and finally emit the
11873 * MI_DISPLAY_FLIP.
11874 */
11875 ret = intel_ring_cacheline_align(req);
11876 if (ret)
11877 return ret;
11878
11879 ret = intel_ring_begin(req, len);
11880 if (ret)
11881 return ret;
11882
11883 /* Unmask the flip-done completion message. Note that the bspec says that
11884 * we should do this for both the BCS and RCS, and that we must not unmask
11885 * more than one flip event at any time (or ensure that one flip message
11886 * can be sent by waiting for flip-done prior to queueing new flips).
11887 * Experimentation says that BCS works despite DERRMR masking all
11888 * flip-done completion events and that unmasking all planes at once
11889 * for the RCS also doesn't appear to drop events. Setting the DERRMR
11890 * to zero does lead to lockups within MI_DISPLAY_FLIP.
11891 */
b5321f30
CW
11892 if (req->engine->id == RCS) {
11893 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
11894 intel_ring_emit_reg(ring, DERRMR);
11895 intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
5a21b665
DV
11896 DERRMR_PIPEB_PRI_FLIP_DONE |
11897 DERRMR_PIPEC_PRI_FLIP_DONE));
11898 if (IS_GEN8(dev))
b5321f30 11899 intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8 |
5a21b665
DV
11900 MI_SRM_LRM_GLOBAL_GTT);
11901 else
b5321f30 11902 intel_ring_emit(ring, MI_STORE_REGISTER_MEM |
5a21b665 11903 MI_SRM_LRM_GLOBAL_GTT);
b5321f30 11904 intel_ring_emit_reg(ring, DERRMR);
bde13ebd
CW
11905 intel_ring_emit(ring,
11906 i915_ggtt_offset(req->engine->scratch) + 256);
5a21b665 11907 if (IS_GEN8(dev)) {
b5321f30
CW
11908 intel_ring_emit(ring, 0);
11909 intel_ring_emit(ring, MI_NOOP);
5a21b665
DV
11910 }
11911 }
11912
b5321f30 11913 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
72618ebf
VS
11914 intel_ring_emit(ring, fb->pitches[0] |
11915 intel_fb_modifier_to_tiling(fb->modifier[0]));
b5321f30
CW
11916 intel_ring_emit(ring, intel_crtc->flip_work->gtt_offset);
11917 intel_ring_emit(ring, (MI_NOOP));
5a21b665
DV
11918
11919 return 0;
11920}
11921
11922static bool use_mmio_flip(struct intel_engine_cs *engine,
11923 struct drm_i915_gem_object *obj)
11924{
c37efb99
CW
11925 struct reservation_object *resv;
11926
5a21b665
DV
11927 /*
11928 * This is not being used for older platforms, because
11929 * non-availability of flip done interrupt forces us to use
11930 * CS flips. Older platforms derive flip done using some clever
11931 * tricks involving the flip_pending status bits and vblank irqs.
11932 * So using MMIO flips there would disrupt this mechanism.
11933 */
11934
11935 if (engine == NULL)
11936 return true;
11937
11938 if (INTEL_GEN(engine->i915) < 5)
11939 return false;
11940
11941 if (i915.use_mmio_flip < 0)
11942 return false;
11943 else if (i915.use_mmio_flip > 0)
11944 return true;
11945 else if (i915.enable_execlists)
11946 return true;
c37efb99
CW
11947
11948 resv = i915_gem_object_get_dmabuf_resv(obj);
11949 if (resv && !reservation_object_test_signaled_rcu(resv, false))
5a21b665 11950 return true;
c37efb99 11951
d72d908b
CW
11952 return engine != i915_gem_active_get_engine(&obj->last_write,
11953 &obj->base.dev->struct_mutex);
5a21b665
DV
11954}
11955
11956static void skl_do_mmio_flip(struct intel_crtc *intel_crtc,
11957 unsigned int rotation,
11958 struct intel_flip_work *work)
11959{
11960 struct drm_device *dev = intel_crtc->base.dev;
fac5e23e 11961 struct drm_i915_private *dev_priv = to_i915(dev);
5a21b665
DV
11962 struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
11963 const enum pipe pipe = intel_crtc->pipe;
d2196774 11964 u32 ctl, stride = skl_plane_stride(fb, 0, rotation);
5a21b665
DV
11965
11966 ctl = I915_READ(PLANE_CTL(pipe, 0));
11967 ctl &= ~PLANE_CTL_TILED_MASK;
11968 switch (fb->modifier[0]) {
11969 case DRM_FORMAT_MOD_NONE:
11970 break;
11971 case I915_FORMAT_MOD_X_TILED:
11972 ctl |= PLANE_CTL_TILED_X;
11973 break;
11974 case I915_FORMAT_MOD_Y_TILED:
11975 ctl |= PLANE_CTL_TILED_Y;
11976 break;
11977 case I915_FORMAT_MOD_Yf_TILED:
11978 ctl |= PLANE_CTL_TILED_YF;
11979 break;
11980 default:
11981 MISSING_CASE(fb->modifier[0]);
11982 }
11983
5a21b665
DV
11984 /*
11985 * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
11986 * PLANE_SURF updates, the update is then guaranteed to be atomic.
11987 */
11988 I915_WRITE(PLANE_CTL(pipe, 0), ctl);
11989 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
11990
11991 I915_WRITE(PLANE_SURF(pipe, 0), work->gtt_offset);
11992 POSTING_READ(PLANE_SURF(pipe, 0));
11993}
11994
11995static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc,
11996 struct intel_flip_work *work)
11997{
11998 struct drm_device *dev = intel_crtc->base.dev;
fac5e23e 11999 struct drm_i915_private *dev_priv = to_i915(dev);
72618ebf 12000 struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
5a21b665
DV
12001 i915_reg_t reg = DSPCNTR(intel_crtc->plane);
12002 u32 dspcntr;
12003
12004 dspcntr = I915_READ(reg);
12005
72618ebf 12006 if (fb->modifier[0] == I915_FORMAT_MOD_X_TILED)
5a21b665
DV
12007 dspcntr |= DISPPLANE_TILED;
12008 else
12009 dspcntr &= ~DISPPLANE_TILED;
12010
12011 I915_WRITE(reg, dspcntr);
12012
12013 I915_WRITE(DSPSURF(intel_crtc->plane), work->gtt_offset);
12014 POSTING_READ(DSPSURF(intel_crtc->plane));
12015}
12016
12017static void intel_mmio_flip_work_func(struct work_struct *w)
12018{
12019 struct intel_flip_work *work =
12020 container_of(w, struct intel_flip_work, mmio_work);
12021 struct intel_crtc *crtc = to_intel_crtc(work->crtc);
12022 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
12023 struct intel_framebuffer *intel_fb =
12024 to_intel_framebuffer(crtc->base.primary->fb);
12025 struct drm_i915_gem_object *obj = intel_fb->obj;
c37efb99 12026 struct reservation_object *resv;
5a21b665
DV
12027
12028 if (work->flip_queued_req)
776f3236
CW
12029 WARN_ON(i915_wait_request(work->flip_queued_req,
12030 false, NULL,
12031 NO_WAITBOOST));
5a21b665
DV
12032
12033 /* For framebuffer backed by dmabuf, wait for fence */
c37efb99
CW
12034 resv = i915_gem_object_get_dmabuf_resv(obj);
12035 if (resv)
12036 WARN_ON(reservation_object_wait_timeout_rcu(resv, false, false,
5a21b665
DV
12037 MAX_SCHEDULE_TIMEOUT) < 0);
12038
12039 intel_pipe_update_start(crtc);
12040
12041 if (INTEL_GEN(dev_priv) >= 9)
12042 skl_do_mmio_flip(crtc, work->rotation, work);
12043 else
12044 /* use_mmio_flip() retricts MMIO flips to ilk+ */
12045 ilk_do_mmio_flip(crtc, work);
12046
12047 intel_pipe_update_end(crtc, work);
12048}
12049
12050static int intel_default_queue_flip(struct drm_device *dev,
12051 struct drm_crtc *crtc,
12052 struct drm_framebuffer *fb,
12053 struct drm_i915_gem_object *obj,
12054 struct drm_i915_gem_request *req,
12055 uint32_t flags)
12056{
12057 return -ENODEV;
12058}
12059
12060static bool __pageflip_stall_check_cs(struct drm_i915_private *dev_priv,
12061 struct intel_crtc *intel_crtc,
12062 struct intel_flip_work *work)
12063{
12064 u32 addr, vblank;
12065
12066 if (!atomic_read(&work->pending))
12067 return false;
12068
12069 smp_rmb();
12070
12071 vblank = intel_crtc_get_vblank_counter(intel_crtc);
12072 if (work->flip_ready_vblank == 0) {
12073 if (work->flip_queued_req &&
f69a02c9 12074 !i915_gem_request_completed(work->flip_queued_req))
5a21b665
DV
12075 return false;
12076
12077 work->flip_ready_vblank = vblank;
12078 }
12079
12080 if (vblank - work->flip_ready_vblank < 3)
12081 return false;
12082
12083 /* Potential stall - if we see that the flip has happened,
12084 * assume a missed interrupt. */
12085 if (INTEL_GEN(dev_priv) >= 4)
12086 addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
12087 else
12088 addr = I915_READ(DSPADDR(intel_crtc->plane));
12089
12090 /* There is a potential issue here with a false positive after a flip
12091 * to the same address. We could address this by checking for a
12092 * non-incrementing frame counter.
12093 */
12094 return addr == work->gtt_offset;
12095}
12096
12097void intel_check_page_flip(struct drm_i915_private *dev_priv, int pipe)
12098{
91c8a326 12099 struct drm_device *dev = &dev_priv->drm;
5a21b665
DV
12100 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
12101 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
12102 struct intel_flip_work *work;
12103
12104 WARN_ON(!in_interrupt());
12105
12106 if (crtc == NULL)
12107 return;
12108
12109 spin_lock(&dev->event_lock);
12110 work = intel_crtc->flip_work;
12111
12112 if (work != NULL && !is_mmio_work(work) &&
12113 __pageflip_stall_check_cs(dev_priv, intel_crtc, work)) {
12114 WARN_ONCE(1,
12115 "Kicking stuck page flip: queued at %d, now %d\n",
12116 work->flip_queued_vblank, intel_crtc_get_vblank_counter(intel_crtc));
12117 page_flip_completed(intel_crtc);
12118 work = NULL;
12119 }
12120
12121 if (work != NULL && !is_mmio_work(work) &&
12122 intel_crtc_get_vblank_counter(intel_crtc) - work->flip_queued_vblank > 1)
12123 intel_queue_rps_boost_for_request(work->flip_queued_req);
12124 spin_unlock(&dev->event_lock);
12125}
12126
12127static int intel_crtc_page_flip(struct drm_crtc *crtc,
12128 struct drm_framebuffer *fb,
12129 struct drm_pending_vblank_event *event,
12130 uint32_t page_flip_flags)
12131{
12132 struct drm_device *dev = crtc->dev;
fac5e23e 12133 struct drm_i915_private *dev_priv = to_i915(dev);
5a21b665
DV
12134 struct drm_framebuffer *old_fb = crtc->primary->fb;
12135 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
12136 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
12137 struct drm_plane *primary = crtc->primary;
12138 enum pipe pipe = intel_crtc->pipe;
12139 struct intel_flip_work *work;
12140 struct intel_engine_cs *engine;
12141 bool mmio_flip;
8e637178 12142 struct drm_i915_gem_request *request;
058d88c4 12143 struct i915_vma *vma;
5a21b665
DV
12144 int ret;
12145
12146 /*
12147 * drm_mode_page_flip_ioctl() should already catch this, but double
12148 * check to be safe. In the future we may enable pageflipping from
12149 * a disabled primary plane.
12150 */
12151 if (WARN_ON(intel_fb_obj(old_fb) == NULL))
12152 return -EBUSY;
12153
12154 /* Can't change pixel format via MI display flips. */
12155 if (fb->pixel_format != crtc->primary->fb->pixel_format)
12156 return -EINVAL;
12157
12158 /*
12159 * TILEOFF/LINOFF registers can't be changed via MI display flips.
12160 * Note that pitch changes could also affect these register.
12161 */
12162 if (INTEL_INFO(dev)->gen > 3 &&
12163 (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
12164 fb->pitches[0] != crtc->primary->fb->pitches[0]))
12165 return -EINVAL;
12166
12167 if (i915_terminally_wedged(&dev_priv->gpu_error))
12168 goto out_hang;
12169
12170 work = kzalloc(sizeof(*work), GFP_KERNEL);
12171 if (work == NULL)
12172 return -ENOMEM;
12173
12174 work->event = event;
12175 work->crtc = crtc;
12176 work->old_fb = old_fb;
12177 INIT_WORK(&work->unpin_work, intel_unpin_work_fn);
12178
12179 ret = drm_crtc_vblank_get(crtc);
12180 if (ret)
12181 goto free_work;
12182
12183 /* We borrow the event spin lock for protecting flip_work */
12184 spin_lock_irq(&dev->event_lock);
12185 if (intel_crtc->flip_work) {
12186 /* Before declaring the flip queue wedged, check if
12187 * the hardware completed the operation behind our backs.
12188 */
12189 if (pageflip_finished(intel_crtc, intel_crtc->flip_work)) {
12190 DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
12191 page_flip_completed(intel_crtc);
12192 } else {
12193 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
12194 spin_unlock_irq(&dev->event_lock);
12195
12196 drm_crtc_vblank_put(crtc);
12197 kfree(work);
12198 return -EBUSY;
12199 }
12200 }
12201 intel_crtc->flip_work = work;
12202 spin_unlock_irq(&dev->event_lock);
12203
12204 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
12205 flush_workqueue(dev_priv->wq);
12206
12207 /* Reference the objects for the scheduled work. */
12208 drm_framebuffer_reference(work->old_fb);
5a21b665
DV
12209
12210 crtc->primary->fb = fb;
12211 update_state_fb(crtc->primary);
faf68d92 12212
25dc556a 12213 work->pending_flip_obj = i915_gem_object_get(obj);
5a21b665
DV
12214
12215 ret = i915_mutex_lock_interruptible(dev);
12216 if (ret)
12217 goto cleanup;
12218
12219 intel_crtc->reset_counter = i915_reset_counter(&dev_priv->gpu_error);
12220 if (__i915_reset_in_progress_or_wedged(intel_crtc->reset_counter)) {
12221 ret = -EIO;
12222 goto cleanup;
12223 }
12224
12225 atomic_inc(&intel_crtc->unpin_work_count);
12226
12227 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
12228 work->flip_count = I915_READ(PIPE_FLIPCOUNT_G4X(pipe)) + 1;
12229
12230 if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
12231 engine = &dev_priv->engine[BCS];
72618ebf 12232 if (fb->modifier[0] != old_fb->modifier[0])
5a21b665
DV
12233 /* vlv: DISPLAY_FLIP fails to change tiling */
12234 engine = NULL;
12235 } else if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
12236 engine = &dev_priv->engine[BCS];
12237 } else if (INTEL_INFO(dev)->gen >= 7) {
d72d908b
CW
12238 engine = i915_gem_active_get_engine(&obj->last_write,
12239 &obj->base.dev->struct_mutex);
5a21b665
DV
12240 if (engine == NULL || engine->id != RCS)
12241 engine = &dev_priv->engine[BCS];
12242 } else {
12243 engine = &dev_priv->engine[RCS];
12244 }
12245
12246 mmio_flip = use_mmio_flip(engine, obj);
12247
058d88c4
CW
12248 vma = intel_pin_and_fence_fb_obj(fb, primary->state->rotation);
12249 if (IS_ERR(vma)) {
12250 ret = PTR_ERR(vma);
5a21b665 12251 goto cleanup_pending;
058d88c4 12252 }
5a21b665 12253
6687c906 12254 work->gtt_offset = intel_fb_gtt_offset(fb, primary->state->rotation);
5a21b665
DV
12255 work->gtt_offset += intel_crtc->dspaddr_offset;
12256 work->rotation = crtc->primary->state->rotation;
12257
1f061316
PZ
12258 /*
12259 * There's the potential that the next frame will not be compatible with
12260 * FBC, so we want to call pre_update() before the actual page flip.
12261 * The problem is that pre_update() caches some information about the fb
12262 * object, so we want to do this only after the object is pinned. Let's
12263 * be on the safe side and do this immediately before scheduling the
12264 * flip.
12265 */
12266 intel_fbc_pre_update(intel_crtc, intel_crtc->config,
12267 to_intel_plane_state(primary->state));
12268
5a21b665
DV
12269 if (mmio_flip) {
12270 INIT_WORK(&work->mmio_work, intel_mmio_flip_work_func);
12271
d72d908b
CW
12272 work->flip_queued_req = i915_gem_active_get(&obj->last_write,
12273 &obj->base.dev->struct_mutex);
5a21b665
DV
12274 schedule_work(&work->mmio_work);
12275 } else {
8e637178
CW
12276 request = i915_gem_request_alloc(engine, engine->last_context);
12277 if (IS_ERR(request)) {
12278 ret = PTR_ERR(request);
12279 goto cleanup_unpin;
12280 }
12281
12282 ret = i915_gem_object_sync(obj, request);
12283 if (ret)
12284 goto cleanup_request;
12285
5a21b665
DV
12286 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, request,
12287 page_flip_flags);
12288 if (ret)
8e637178 12289 goto cleanup_request;
5a21b665
DV
12290
12291 intel_mark_page_flip_active(intel_crtc, work);
12292
8e637178 12293 work->flip_queued_req = i915_gem_request_get(request);
5a21b665
DV
12294 i915_add_request_no_flush(request);
12295 }
12296
12297 i915_gem_track_fb(intel_fb_obj(old_fb), obj,
12298 to_intel_plane(primary)->frontbuffer_bit);
12299 mutex_unlock(&dev->struct_mutex);
12300
5748b6a1 12301 intel_frontbuffer_flip_prepare(to_i915(dev),
5a21b665
DV
12302 to_intel_plane(primary)->frontbuffer_bit);
12303
12304 trace_i915_flip_request(intel_crtc->plane, obj);
12305
12306 return 0;
12307
8e637178
CW
12308cleanup_request:
12309 i915_add_request_no_flush(request);
5a21b665
DV
12310cleanup_unpin:
12311 intel_unpin_fb_obj(fb, crtc->primary->state->rotation);
12312cleanup_pending:
5a21b665
DV
12313 atomic_dec(&intel_crtc->unpin_work_count);
12314 mutex_unlock(&dev->struct_mutex);
12315cleanup:
12316 crtc->primary->fb = old_fb;
12317 update_state_fb(crtc->primary);
12318
34911fd3 12319 i915_gem_object_put_unlocked(obj);
5a21b665
DV
12320 drm_framebuffer_unreference(work->old_fb);
12321
12322 spin_lock_irq(&dev->event_lock);
12323 intel_crtc->flip_work = NULL;
12324 spin_unlock_irq(&dev->event_lock);
12325
12326 drm_crtc_vblank_put(crtc);
12327free_work:
12328 kfree(work);
12329
12330 if (ret == -EIO) {
12331 struct drm_atomic_state *state;
12332 struct drm_plane_state *plane_state;
12333
12334out_hang:
12335 state = drm_atomic_state_alloc(dev);
12336 if (!state)
12337 return -ENOMEM;
12338 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
12339
12340retry:
12341 plane_state = drm_atomic_get_plane_state(state, primary);
12342 ret = PTR_ERR_OR_ZERO(plane_state);
12343 if (!ret) {
12344 drm_atomic_set_fb_for_plane(plane_state, fb);
12345
12346 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
12347 if (!ret)
12348 ret = drm_atomic_commit(state);
12349 }
12350
12351 if (ret == -EDEADLK) {
12352 drm_modeset_backoff(state->acquire_ctx);
12353 drm_atomic_state_clear(state);
12354 goto retry;
12355 }
12356
12357 if (ret)
12358 drm_atomic_state_free(state);
12359
12360 if (ret == 0 && event) {
12361 spin_lock_irq(&dev->event_lock);
12362 drm_crtc_send_vblank_event(crtc, event);
12363 spin_unlock_irq(&dev->event_lock);
12364 }
12365 }
12366 return ret;
12367}
12368
12369
12370/**
12371 * intel_wm_need_update - Check whether watermarks need updating
12372 * @plane: drm plane
12373 * @state: new plane state
12374 *
12375 * Check current plane state versus the new one to determine whether
12376 * watermarks need to be recalculated.
12377 *
12378 * Returns true or false.
12379 */
12380static bool intel_wm_need_update(struct drm_plane *plane,
12381 struct drm_plane_state *state)
12382{
12383 struct intel_plane_state *new = to_intel_plane_state(state);
12384 struct intel_plane_state *cur = to_intel_plane_state(plane->state);
12385
12386 /* Update watermarks on tiling or size changes. */
936e71e3 12387 if (new->base.visible != cur->base.visible)
5a21b665
DV
12388 return true;
12389
12390 if (!cur->base.fb || !new->base.fb)
12391 return false;
12392
12393 if (cur->base.fb->modifier[0] != new->base.fb->modifier[0] ||
12394 cur->base.rotation != new->base.rotation ||
936e71e3
VS
12395 drm_rect_width(&new->base.src) != drm_rect_width(&cur->base.src) ||
12396 drm_rect_height(&new->base.src) != drm_rect_height(&cur->base.src) ||
12397 drm_rect_width(&new->base.dst) != drm_rect_width(&cur->base.dst) ||
12398 drm_rect_height(&new->base.dst) != drm_rect_height(&cur->base.dst))
5a21b665
DV
12399 return true;
12400
12401 return false;
12402}
12403
12404static bool needs_scaling(struct intel_plane_state *state)
12405{
936e71e3
VS
12406 int src_w = drm_rect_width(&state->base.src) >> 16;
12407 int src_h = drm_rect_height(&state->base.src) >> 16;
12408 int dst_w = drm_rect_width(&state->base.dst);
12409 int dst_h = drm_rect_height(&state->base.dst);
5a21b665
DV
12410
12411 return (src_w != dst_w || src_h != dst_h);
12412}
d21fbe87 12413
da20eabd
ML
12414int intel_plane_atomic_calc_changes(struct drm_crtc_state *crtc_state,
12415 struct drm_plane_state *plane_state)
12416{
ab1d3a0e 12417 struct intel_crtc_state *pipe_config = to_intel_crtc_state(crtc_state);
da20eabd
ML
12418 struct drm_crtc *crtc = crtc_state->crtc;
12419 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
12420 struct drm_plane *plane = plane_state->plane;
12421 struct drm_device *dev = crtc->dev;
ed4a6a7c 12422 struct drm_i915_private *dev_priv = to_i915(dev);
da20eabd
ML
12423 struct intel_plane_state *old_plane_state =
12424 to_intel_plane_state(plane->state);
da20eabd
ML
12425 bool mode_changed = needs_modeset(crtc_state);
12426 bool was_crtc_enabled = crtc->state->active;
12427 bool is_crtc_enabled = crtc_state->active;
da20eabd
ML
12428 bool turn_off, turn_on, visible, was_visible;
12429 struct drm_framebuffer *fb = plane_state->fb;
78108b7c 12430 int ret;
da20eabd 12431
84114990 12432 if (INTEL_GEN(dev) >= 9 && plane->type != DRM_PLANE_TYPE_CURSOR) {
da20eabd
ML
12433 ret = skl_update_scaler_plane(
12434 to_intel_crtc_state(crtc_state),
12435 to_intel_plane_state(plane_state));
12436 if (ret)
12437 return ret;
12438 }
12439
936e71e3
VS
12440 was_visible = old_plane_state->base.visible;
12441 visible = to_intel_plane_state(plane_state)->base.visible;
da20eabd
ML
12442
12443 if (!was_crtc_enabled && WARN_ON(was_visible))
12444 was_visible = false;
12445
35c08f43
ML
12446 /*
12447 * Visibility is calculated as if the crtc was on, but
12448 * after scaler setup everything depends on it being off
12449 * when the crtc isn't active.
f818ffea
VS
12450 *
12451 * FIXME this is wrong for watermarks. Watermarks should also
12452 * be computed as if the pipe would be active. Perhaps move
12453 * per-plane wm computation to the .check_plane() hook, and
12454 * only combine the results from all planes in the current place?
35c08f43
ML
12455 */
12456 if (!is_crtc_enabled)
936e71e3 12457 to_intel_plane_state(plane_state)->base.visible = visible = false;
da20eabd
ML
12458
12459 if (!was_visible && !visible)
12460 return 0;
12461
e8861675
ML
12462 if (fb != old_plane_state->base.fb)
12463 pipe_config->fb_changed = true;
12464
da20eabd
ML
12465 turn_off = was_visible && (!visible || mode_changed);
12466 turn_on = visible && (!was_visible || mode_changed);
12467
72660ce0 12468 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] has [PLANE:%d:%s] with fb %i\n",
78108b7c
VS
12469 intel_crtc->base.base.id,
12470 intel_crtc->base.name,
72660ce0
VS
12471 plane->base.id, plane->name,
12472 fb ? fb->base.id : -1);
da20eabd 12473
72660ce0
VS
12474 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] visible %i -> %i, off %i, on %i, ms %i\n",
12475 plane->base.id, plane->name,
12476 was_visible, visible,
da20eabd
ML
12477 turn_off, turn_on, mode_changed);
12478
caed361d
VS
12479 if (turn_on) {
12480 pipe_config->update_wm_pre = true;
12481
12482 /* must disable cxsr around plane enable/disable */
12483 if (plane->type != DRM_PLANE_TYPE_CURSOR)
12484 pipe_config->disable_cxsr = true;
12485 } else if (turn_off) {
12486 pipe_config->update_wm_post = true;
92826fcd 12487
852eb00d 12488 /* must disable cxsr around plane enable/disable */
e8861675 12489 if (plane->type != DRM_PLANE_TYPE_CURSOR)
ab1d3a0e 12490 pipe_config->disable_cxsr = true;
852eb00d 12491 } else if (intel_wm_need_update(plane, plane_state)) {
caed361d
VS
12492 /* FIXME bollocks */
12493 pipe_config->update_wm_pre = true;
12494 pipe_config->update_wm_post = true;
852eb00d 12495 }
da20eabd 12496
ed4a6a7c 12497 /* Pre-gen9 platforms need two-step watermark updates */
caed361d
VS
12498 if ((pipe_config->update_wm_pre || pipe_config->update_wm_post) &&
12499 INTEL_INFO(dev)->gen < 9 && dev_priv->display.optimize_watermarks)
ed4a6a7c
MR
12500 to_intel_crtc_state(crtc_state)->wm.need_postvbl_update = true;
12501
8be6ca85 12502 if (visible || was_visible)
cd202f69 12503 pipe_config->fb_bits |= to_intel_plane(plane)->frontbuffer_bit;
a9ff8714 12504
31ae71fc
ML
12505 /*
12506 * WaCxSRDisabledForSpriteScaling:ivb
12507 *
12508 * cstate->update_wm was already set above, so this flag will
12509 * take effect when we commit and program watermarks.
12510 */
12511 if (plane->type == DRM_PLANE_TYPE_OVERLAY && IS_IVYBRIDGE(dev) &&
12512 needs_scaling(to_intel_plane_state(plane_state)) &&
12513 !needs_scaling(old_plane_state))
12514 pipe_config->disable_lp_wm = true;
d21fbe87 12515
da20eabd
ML
12516 return 0;
12517}
12518
6d3a1ce7
ML
12519static bool encoders_cloneable(const struct intel_encoder *a,
12520 const struct intel_encoder *b)
12521{
12522 /* masks could be asymmetric, so check both ways */
12523 return a == b || (a->cloneable & (1 << b->type) &&
12524 b->cloneable & (1 << a->type));
12525}
12526
12527static bool check_single_encoder_cloning(struct drm_atomic_state *state,
12528 struct intel_crtc *crtc,
12529 struct intel_encoder *encoder)
12530{
12531 struct intel_encoder *source_encoder;
12532 struct drm_connector *connector;
12533 struct drm_connector_state *connector_state;
12534 int i;
12535
12536 for_each_connector_in_state(state, connector, connector_state, i) {
12537 if (connector_state->crtc != &crtc->base)
12538 continue;
12539
12540 source_encoder =
12541 to_intel_encoder(connector_state->best_encoder);
12542 if (!encoders_cloneable(encoder, source_encoder))
12543 return false;
12544 }
12545
12546 return true;
12547}
12548
6d3a1ce7
ML
12549static int intel_crtc_atomic_check(struct drm_crtc *crtc,
12550 struct drm_crtc_state *crtc_state)
12551{
cf5a15be 12552 struct drm_device *dev = crtc->dev;
fac5e23e 12553 struct drm_i915_private *dev_priv = to_i915(dev);
6d3a1ce7 12554 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
cf5a15be
ML
12555 struct intel_crtc_state *pipe_config =
12556 to_intel_crtc_state(crtc_state);
6d3a1ce7 12557 struct drm_atomic_state *state = crtc_state->state;
4d20cd86 12558 int ret;
6d3a1ce7
ML
12559 bool mode_changed = needs_modeset(crtc_state);
12560
852eb00d 12561 if (mode_changed && !crtc_state->active)
caed361d 12562 pipe_config->update_wm_post = true;
eddfcbcd 12563
ad421372
ML
12564 if (mode_changed && crtc_state->enable &&
12565 dev_priv->display.crtc_compute_clock &&
8106ddbd 12566 !WARN_ON(pipe_config->shared_dpll)) {
ad421372
ML
12567 ret = dev_priv->display.crtc_compute_clock(intel_crtc,
12568 pipe_config);
12569 if (ret)
12570 return ret;
12571 }
12572
82cf435b
LL
12573 if (crtc_state->color_mgmt_changed) {
12574 ret = intel_color_check(crtc, crtc_state);
12575 if (ret)
12576 return ret;
e7852a4b
LL
12577
12578 /*
12579 * Changing color management on Intel hardware is
12580 * handled as part of planes update.
12581 */
12582 crtc_state->planes_changed = true;
82cf435b
LL
12583 }
12584
e435d6e5 12585 ret = 0;
86c8bbbe 12586 if (dev_priv->display.compute_pipe_wm) {
e3bddded 12587 ret = dev_priv->display.compute_pipe_wm(pipe_config);
ed4a6a7c
MR
12588 if (ret) {
12589 DRM_DEBUG_KMS("Target pipe watermarks are invalid\n");
12590 return ret;
12591 }
12592 }
12593
12594 if (dev_priv->display.compute_intermediate_wm &&
12595 !to_intel_atomic_state(state)->skip_intermediate_wm) {
12596 if (WARN_ON(!dev_priv->display.compute_pipe_wm))
12597 return 0;
12598
12599 /*
12600 * Calculate 'intermediate' watermarks that satisfy both the
12601 * old state and the new state. We can program these
12602 * immediately.
12603 */
12604 ret = dev_priv->display.compute_intermediate_wm(crtc->dev,
12605 intel_crtc,
12606 pipe_config);
12607 if (ret) {
12608 DRM_DEBUG_KMS("No valid intermediate pipe watermarks are possible\n");
86c8bbbe 12609 return ret;
ed4a6a7c 12610 }
e3d5457c
VS
12611 } else if (dev_priv->display.compute_intermediate_wm) {
12612 if (HAS_PCH_SPLIT(dev_priv) && INTEL_GEN(dev_priv) < 9)
12613 pipe_config->wm.ilk.intermediate = pipe_config->wm.ilk.optimal;
86c8bbbe
MR
12614 }
12615
e435d6e5
ML
12616 if (INTEL_INFO(dev)->gen >= 9) {
12617 if (mode_changed)
12618 ret = skl_update_scaler_crtc(pipe_config);
12619
12620 if (!ret)
12621 ret = intel_atomic_setup_scalers(dev, intel_crtc,
12622 pipe_config);
12623 }
12624
12625 return ret;
6d3a1ce7
ML
12626}
12627
65b38e0d 12628static const struct drm_crtc_helper_funcs intel_helper_funcs = {
f6e5b160 12629 .mode_set_base_atomic = intel_pipe_set_base_atomic,
5a21b665
DV
12630 .atomic_begin = intel_begin_crtc_commit,
12631 .atomic_flush = intel_finish_crtc_commit,
6d3a1ce7 12632 .atomic_check = intel_crtc_atomic_check,
f6e5b160
CW
12633};
12634
d29b2f9d
ACO
12635static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
12636{
12637 struct intel_connector *connector;
12638
12639 for_each_intel_connector(dev, connector) {
8863dc7f
DV
12640 if (connector->base.state->crtc)
12641 drm_connector_unreference(&connector->base);
12642
d29b2f9d
ACO
12643 if (connector->base.encoder) {
12644 connector->base.state->best_encoder =
12645 connector->base.encoder;
12646 connector->base.state->crtc =
12647 connector->base.encoder->crtc;
8863dc7f
DV
12648
12649 drm_connector_reference(&connector->base);
d29b2f9d
ACO
12650 } else {
12651 connector->base.state->best_encoder = NULL;
12652 connector->base.state->crtc = NULL;
12653 }
12654 }
12655}
12656
050f7aeb 12657static void
eba905b2 12658connected_sink_compute_bpp(struct intel_connector *connector,
5cec258b 12659 struct intel_crtc_state *pipe_config)
050f7aeb
DV
12660{
12661 int bpp = pipe_config->pipe_bpp;
12662
12663 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
12664 connector->base.base.id,
c23cc417 12665 connector->base.name);
050f7aeb
DV
12666
12667 /* Don't use an invalid EDID bpc value */
12668 if (connector->base.display_info.bpc &&
12669 connector->base.display_info.bpc * 3 < bpp) {
12670 DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
12671 bpp, connector->base.display_info.bpc*3);
12672 pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
12673 }
12674
196f954e
MK
12675 /* Clamp bpp to 8 on screens without EDID 1.4 */
12676 if (connector->base.display_info.bpc == 0 && bpp > 24) {
12677 DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
12678 bpp);
12679 pipe_config->pipe_bpp = 24;
050f7aeb
DV
12680 }
12681}
12682
4e53c2e0 12683static int
050f7aeb 12684compute_baseline_pipe_bpp(struct intel_crtc *crtc,
5cec258b 12685 struct intel_crtc_state *pipe_config)
4e53c2e0 12686{
050f7aeb 12687 struct drm_device *dev = crtc->base.dev;
1486017f 12688 struct drm_atomic_state *state;
da3ced29
ACO
12689 struct drm_connector *connector;
12690 struct drm_connector_state *connector_state;
1486017f 12691 int bpp, i;
4e53c2e0 12692
666a4537 12693 if ((IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)))
4e53c2e0 12694 bpp = 10*3;
d328c9d7
DV
12695 else if (INTEL_INFO(dev)->gen >= 5)
12696 bpp = 12*3;
12697 else
12698 bpp = 8*3;
12699
4e53c2e0 12700
4e53c2e0
DV
12701 pipe_config->pipe_bpp = bpp;
12702
1486017f
ACO
12703 state = pipe_config->base.state;
12704
4e53c2e0 12705 /* Clamp display bpp to EDID value */
da3ced29
ACO
12706 for_each_connector_in_state(state, connector, connector_state, i) {
12707 if (connector_state->crtc != &crtc->base)
4e53c2e0
DV
12708 continue;
12709
da3ced29
ACO
12710 connected_sink_compute_bpp(to_intel_connector(connector),
12711 pipe_config);
4e53c2e0
DV
12712 }
12713
12714 return bpp;
12715}
12716
644db711
DV
12717static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
12718{
12719 DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
12720 "type: 0x%x flags: 0x%x\n",
1342830c 12721 mode->crtc_clock,
644db711
DV
12722 mode->crtc_hdisplay, mode->crtc_hsync_start,
12723 mode->crtc_hsync_end, mode->crtc_htotal,
12724 mode->crtc_vdisplay, mode->crtc_vsync_start,
12725 mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
12726}
12727
c0b03411 12728static void intel_dump_pipe_config(struct intel_crtc *crtc,
5cec258b 12729 struct intel_crtc_state *pipe_config,
c0b03411
DV
12730 const char *context)
12731{
6a60cd87
CK
12732 struct drm_device *dev = crtc->base.dev;
12733 struct drm_plane *plane;
12734 struct intel_plane *intel_plane;
12735 struct intel_plane_state *state;
12736 struct drm_framebuffer *fb;
12737
78108b7c
VS
12738 DRM_DEBUG_KMS("[CRTC:%d:%s]%s config %p for pipe %c\n",
12739 crtc->base.base.id, crtc->base.name,
6a60cd87 12740 context, pipe_config, pipe_name(crtc->pipe));
c0b03411 12741
da205630 12742 DRM_DEBUG_KMS("cpu_transcoder: %s\n", transcoder_name(pipe_config->cpu_transcoder));
c0b03411
DV
12743 DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
12744 pipe_config->pipe_bpp, pipe_config->dither);
12745 DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
12746 pipe_config->has_pch_encoder,
12747 pipe_config->fdi_lanes,
12748 pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
12749 pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
12750 pipe_config->fdi_m_n.tu);
90a6b7b0 12751 DRM_DEBUG_KMS("dp: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
37a5650b 12752 intel_crtc_has_dp_encoder(pipe_config),
90a6b7b0 12753 pipe_config->lane_count,
eb14cb74
VS
12754 pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
12755 pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
12756 pipe_config->dp_m_n.tu);
b95af8be 12757
90a6b7b0 12758 DRM_DEBUG_KMS("dp: %i, lanes: %i, gmch_m2: %u, gmch_n2: %u, link_m2: %u, link_n2: %u, tu2: %u\n",
37a5650b 12759 intel_crtc_has_dp_encoder(pipe_config),
90a6b7b0 12760 pipe_config->lane_count,
b95af8be
VK
12761 pipe_config->dp_m2_n2.gmch_m,
12762 pipe_config->dp_m2_n2.gmch_n,
12763 pipe_config->dp_m2_n2.link_m,
12764 pipe_config->dp_m2_n2.link_n,
12765 pipe_config->dp_m2_n2.tu);
12766
55072d19
DV
12767 DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
12768 pipe_config->has_audio,
12769 pipe_config->has_infoframe);
12770
c0b03411 12771 DRM_DEBUG_KMS("requested mode:\n");
2d112de7 12772 drm_mode_debug_printmodeline(&pipe_config->base.mode);
c0b03411 12773 DRM_DEBUG_KMS("adjusted mode:\n");
2d112de7
ACO
12774 drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
12775 intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
d71b8d4a 12776 DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
37327abd
VS
12777 DRM_DEBUG_KMS("pipe src size: %dx%d\n",
12778 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
0ec463d3
TU
12779 DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
12780 crtc->num_scalers,
12781 pipe_config->scaler_state.scaler_users,
12782 pipe_config->scaler_state.scaler_id);
c0b03411
DV
12783 DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
12784 pipe_config->gmch_pfit.control,
12785 pipe_config->gmch_pfit.pgm_ratios,
12786 pipe_config->gmch_pfit.lvds_border_bits);
fd4daa9c 12787 DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
c0b03411 12788 pipe_config->pch_pfit.pos,
fd4daa9c
CW
12789 pipe_config->pch_pfit.size,
12790 pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
42db64ef 12791 DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
cf532bb2 12792 DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
6a60cd87 12793
415ff0f6 12794 if (IS_BROXTON(dev)) {
05712c15 12795 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: ebb0: 0x%x, ebb4: 0x%x,"
415ff0f6 12796 "pll0: 0x%x, pll1: 0x%x, pll2: 0x%x, pll3: 0x%x, "
c8453338 12797 "pll6: 0x%x, pll8: 0x%x, pll9: 0x%x, pll10: 0x%x, pcsdw12: 0x%x\n",
415ff0f6
TU
12798 pipe_config->ddi_pll_sel,
12799 pipe_config->dpll_hw_state.ebb0,
05712c15 12800 pipe_config->dpll_hw_state.ebb4,
415ff0f6
TU
12801 pipe_config->dpll_hw_state.pll0,
12802 pipe_config->dpll_hw_state.pll1,
12803 pipe_config->dpll_hw_state.pll2,
12804 pipe_config->dpll_hw_state.pll3,
12805 pipe_config->dpll_hw_state.pll6,
12806 pipe_config->dpll_hw_state.pll8,
05712c15 12807 pipe_config->dpll_hw_state.pll9,
c8453338 12808 pipe_config->dpll_hw_state.pll10,
415ff0f6 12809 pipe_config->dpll_hw_state.pcsdw12);
ef11bdb3 12810 } else if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) {
415ff0f6
TU
12811 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: "
12812 "ctrl1: 0x%x, cfgcr1: 0x%x, cfgcr2: 0x%x\n",
12813 pipe_config->ddi_pll_sel,
12814 pipe_config->dpll_hw_state.ctrl1,
12815 pipe_config->dpll_hw_state.cfgcr1,
12816 pipe_config->dpll_hw_state.cfgcr2);
12817 } else if (HAS_DDI(dev)) {
1260f07e 12818 DRM_DEBUG_KMS("ddi_pll_sel: 0x%x; dpll_hw_state: wrpll: 0x%x spll: 0x%x\n",
415ff0f6 12819 pipe_config->ddi_pll_sel,
00490c22
ML
12820 pipe_config->dpll_hw_state.wrpll,
12821 pipe_config->dpll_hw_state.spll);
415ff0f6
TU
12822 } else {
12823 DRM_DEBUG_KMS("dpll_hw_state: dpll: 0x%x, dpll_md: 0x%x, "
12824 "fp0: 0x%x, fp1: 0x%x\n",
12825 pipe_config->dpll_hw_state.dpll,
12826 pipe_config->dpll_hw_state.dpll_md,
12827 pipe_config->dpll_hw_state.fp0,
12828 pipe_config->dpll_hw_state.fp1);
12829 }
12830
6a60cd87
CK
12831 DRM_DEBUG_KMS("planes on this crtc\n");
12832 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
12833 intel_plane = to_intel_plane(plane);
12834 if (intel_plane->pipe != crtc->pipe)
12835 continue;
12836
12837 state = to_intel_plane_state(plane->state);
12838 fb = state->base.fb;
12839 if (!fb) {
1d577e02
VS
12840 DRM_DEBUG_KMS("[PLANE:%d:%s] disabled, scaler_id = %d\n",
12841 plane->base.id, plane->name, state->scaler_id);
6a60cd87
CK
12842 continue;
12843 }
12844
1d577e02
VS
12845 DRM_DEBUG_KMS("[PLANE:%d:%s] enabled",
12846 plane->base.id, plane->name);
12847 DRM_DEBUG_KMS("\tFB:%d, fb = %ux%u format = %s",
12848 fb->base.id, fb->width, fb->height,
12849 drm_get_format_name(fb->pixel_format));
12850 DRM_DEBUG_KMS("\tscaler:%d src %dx%d+%d+%d dst %dx%d+%d+%d\n",
12851 state->scaler_id,
936e71e3
VS
12852 state->base.src.x1 >> 16,
12853 state->base.src.y1 >> 16,
12854 drm_rect_width(&state->base.src) >> 16,
12855 drm_rect_height(&state->base.src) >> 16,
12856 state->base.dst.x1, state->base.dst.y1,
12857 drm_rect_width(&state->base.dst),
12858 drm_rect_height(&state->base.dst));
6a60cd87 12859 }
c0b03411
DV
12860}
12861
5448a00d 12862static bool check_digital_port_conflicts(struct drm_atomic_state *state)
00f0b378 12863{
5448a00d 12864 struct drm_device *dev = state->dev;
da3ced29 12865 struct drm_connector *connector;
00f0b378 12866 unsigned int used_ports = 0;
477321e0 12867 unsigned int used_mst_ports = 0;
00f0b378
VS
12868
12869 /*
12870 * Walk the connector list instead of the encoder
12871 * list to detect the problem on ddi platforms
12872 * where there's just one encoder per digital port.
12873 */
0bff4858
VS
12874 drm_for_each_connector(connector, dev) {
12875 struct drm_connector_state *connector_state;
12876 struct intel_encoder *encoder;
12877
12878 connector_state = drm_atomic_get_existing_connector_state(state, connector);
12879 if (!connector_state)
12880 connector_state = connector->state;
12881
5448a00d 12882 if (!connector_state->best_encoder)
00f0b378
VS
12883 continue;
12884
5448a00d
ACO
12885 encoder = to_intel_encoder(connector_state->best_encoder);
12886
12887 WARN_ON(!connector_state->crtc);
00f0b378
VS
12888
12889 switch (encoder->type) {
12890 unsigned int port_mask;
12891 case INTEL_OUTPUT_UNKNOWN:
12892 if (WARN_ON(!HAS_DDI(dev)))
12893 break;
cca0502b 12894 case INTEL_OUTPUT_DP:
00f0b378
VS
12895 case INTEL_OUTPUT_HDMI:
12896 case INTEL_OUTPUT_EDP:
12897 port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
12898
12899 /* the same port mustn't appear more than once */
12900 if (used_ports & port_mask)
12901 return false;
12902
12903 used_ports |= port_mask;
477321e0
VS
12904 break;
12905 case INTEL_OUTPUT_DP_MST:
12906 used_mst_ports |=
12907 1 << enc_to_mst(&encoder->base)->primary->port;
12908 break;
00f0b378
VS
12909 default:
12910 break;
12911 }
12912 }
12913
477321e0
VS
12914 /* can't mix MST and SST/HDMI on the same port */
12915 if (used_ports & used_mst_ports)
12916 return false;
12917
00f0b378
VS
12918 return true;
12919}
12920
83a57153
ACO
12921static void
12922clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
12923{
12924 struct drm_crtc_state tmp_state;
663a3640 12925 struct intel_crtc_scaler_state scaler_state;
4978cc93 12926 struct intel_dpll_hw_state dpll_hw_state;
8106ddbd 12927 struct intel_shared_dpll *shared_dpll;
8504c74c 12928 uint32_t ddi_pll_sel;
c4e2d043 12929 bool force_thru;
83a57153 12930
7546a384
ACO
12931 /* FIXME: before the switch to atomic started, a new pipe_config was
12932 * kzalloc'd. Code that depends on any field being zero should be
12933 * fixed, so that the crtc_state can be safely duplicated. For now,
12934 * only fields that are know to not cause problems are preserved. */
12935
83a57153 12936 tmp_state = crtc_state->base;
663a3640 12937 scaler_state = crtc_state->scaler_state;
4978cc93
ACO
12938 shared_dpll = crtc_state->shared_dpll;
12939 dpll_hw_state = crtc_state->dpll_hw_state;
8504c74c 12940 ddi_pll_sel = crtc_state->ddi_pll_sel;
c4e2d043 12941 force_thru = crtc_state->pch_pfit.force_thru;
4978cc93 12942
83a57153 12943 memset(crtc_state, 0, sizeof *crtc_state);
4978cc93 12944
83a57153 12945 crtc_state->base = tmp_state;
663a3640 12946 crtc_state->scaler_state = scaler_state;
4978cc93
ACO
12947 crtc_state->shared_dpll = shared_dpll;
12948 crtc_state->dpll_hw_state = dpll_hw_state;
8504c74c 12949 crtc_state->ddi_pll_sel = ddi_pll_sel;
c4e2d043 12950 crtc_state->pch_pfit.force_thru = force_thru;
83a57153
ACO
12951}
12952
548ee15b 12953static int
b8cecdf5 12954intel_modeset_pipe_config(struct drm_crtc *crtc,
b359283a 12955 struct intel_crtc_state *pipe_config)
ee7b9f93 12956{
b359283a 12957 struct drm_atomic_state *state = pipe_config->base.state;
7758a113 12958 struct intel_encoder *encoder;
da3ced29 12959 struct drm_connector *connector;
0b901879 12960 struct drm_connector_state *connector_state;
d328c9d7 12961 int base_bpp, ret = -EINVAL;
0b901879 12962 int i;
e29c22c0 12963 bool retry = true;
ee7b9f93 12964
83a57153 12965 clear_intel_crtc_state(pipe_config);
7758a113 12966
e143a21c
DV
12967 pipe_config->cpu_transcoder =
12968 (enum transcoder) to_intel_crtc(crtc)->pipe;
b8cecdf5 12969
2960bc9c
ID
12970 /*
12971 * Sanitize sync polarity flags based on requested ones. If neither
12972 * positive or negative polarity is requested, treat this as meaning
12973 * negative polarity.
12974 */
2d112de7 12975 if (!(pipe_config->base.adjusted_mode.flags &
2960bc9c 12976 (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
2d112de7 12977 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
2960bc9c 12978
2d112de7 12979 if (!(pipe_config->base.adjusted_mode.flags &
2960bc9c 12980 (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
2d112de7 12981 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
2960bc9c 12982
d328c9d7
DV
12983 base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
12984 pipe_config);
12985 if (base_bpp < 0)
4e53c2e0
DV
12986 goto fail;
12987
e41a56be
VS
12988 /*
12989 * Determine the real pipe dimensions. Note that stereo modes can
12990 * increase the actual pipe size due to the frame doubling and
12991 * insertion of additional space for blanks between the frame. This
12992 * is stored in the crtc timings. We use the requested mode to do this
12993 * computation to clearly distinguish it from the adjusted mode, which
12994 * can be changed by the connectors in the below retry loop.
12995 */
2d112de7 12996 drm_crtc_get_hv_timing(&pipe_config->base.mode,
ecb7e16b
GP
12997 &pipe_config->pipe_src_w,
12998 &pipe_config->pipe_src_h);
e41a56be 12999
253c84c8
VS
13000 for_each_connector_in_state(state, connector, connector_state, i) {
13001 if (connector_state->crtc != crtc)
13002 continue;
13003
13004 encoder = to_intel_encoder(connector_state->best_encoder);
13005
e25148d0
VS
13006 if (!check_single_encoder_cloning(state, to_intel_crtc(crtc), encoder)) {
13007 DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
13008 goto fail;
13009 }
13010
253c84c8
VS
13011 /*
13012 * Determine output_types before calling the .compute_config()
13013 * hooks so that the hooks can use this information safely.
13014 */
13015 pipe_config->output_types |= 1 << encoder->type;
13016 }
13017
e29c22c0 13018encoder_retry:
ef1b460d 13019 /* Ensure the port clock defaults are reset when retrying. */
ff9a6750 13020 pipe_config->port_clock = 0;
ef1b460d 13021 pipe_config->pixel_multiplier = 1;
ff9a6750 13022
135c81b8 13023 /* Fill in default crtc timings, allow encoders to overwrite them. */
2d112de7
ACO
13024 drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
13025 CRTC_STEREO_DOUBLE);
135c81b8 13026
7758a113
DV
13027 /* Pass our mode to the connectors and the CRTC to give them a chance to
13028 * adjust it according to limitations or connector properties, and also
13029 * a chance to reject the mode entirely.
47f1c6c9 13030 */
da3ced29 13031 for_each_connector_in_state(state, connector, connector_state, i) {
0b901879 13032 if (connector_state->crtc != crtc)
7758a113 13033 continue;
7ae89233 13034
0b901879
ACO
13035 encoder = to_intel_encoder(connector_state->best_encoder);
13036
0a478c27 13037 if (!(encoder->compute_config(encoder, pipe_config, connector_state))) {
efea6e8e 13038 DRM_DEBUG_KMS("Encoder config failure\n");
7758a113
DV
13039 goto fail;
13040 }
ee7b9f93 13041 }
47f1c6c9 13042
ff9a6750
DV
13043 /* Set default port clock if not overwritten by the encoder. Needs to be
13044 * done afterwards in case the encoder adjusts the mode. */
13045 if (!pipe_config->port_clock)
2d112de7 13046 pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
241bfc38 13047 * pipe_config->pixel_multiplier;
ff9a6750 13048
a43f6e0f 13049 ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
e29c22c0 13050 if (ret < 0) {
7758a113
DV
13051 DRM_DEBUG_KMS("CRTC fixup failed\n");
13052 goto fail;
ee7b9f93 13053 }
e29c22c0
DV
13054
13055 if (ret == RETRY) {
13056 if (WARN(!retry, "loop in pipe configuration computation\n")) {
13057 ret = -EINVAL;
13058 goto fail;
13059 }
13060
13061 DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
13062 retry = false;
13063 goto encoder_retry;
13064 }
13065
e8fa4270
DV
13066 /* Dithering seems to not pass-through bits correctly when it should, so
13067 * only enable it on 6bpc panels. */
13068 pipe_config->dither = pipe_config->pipe_bpp == 6*3;
62f0ace5 13069 DRM_DEBUG_KMS("hw max bpp: %i, pipe bpp: %i, dithering: %i\n",
d328c9d7 13070 base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
4e53c2e0 13071
7758a113 13072fail:
548ee15b 13073 return ret;
ee7b9f93 13074}
47f1c6c9 13075
ea9d758d 13076static void
4740b0f2 13077intel_modeset_update_crtc_state(struct drm_atomic_state *state)
ea9d758d 13078{
0a9ab303
ACO
13079 struct drm_crtc *crtc;
13080 struct drm_crtc_state *crtc_state;
8a75d157 13081 int i;
ea9d758d 13082
7668851f 13083 /* Double check state. */
8a75d157 13084 for_each_crtc_in_state(state, crtc, crtc_state, i) {
3cb480bc 13085 to_intel_crtc(crtc)->config = to_intel_crtc_state(crtc->state);
fc467a22
ML
13086
13087 /* Update hwmode for vblank functions */
13088 if (crtc->state->active)
13089 crtc->hwmode = crtc->state->adjusted_mode;
13090 else
13091 crtc->hwmode.crtc_clock = 0;
61067a5e
ML
13092
13093 /*
13094 * Update legacy state to satisfy fbc code. This can
13095 * be removed when fbc uses the atomic state.
13096 */
13097 if (drm_atomic_get_existing_plane_state(state, crtc->primary)) {
13098 struct drm_plane_state *plane_state = crtc->primary->state;
13099
13100 crtc->primary->fb = plane_state->fb;
13101 crtc->x = plane_state->src_x >> 16;
13102 crtc->y = plane_state->src_y >> 16;
13103 }
ea9d758d 13104 }
ea9d758d
DV
13105}
13106
3bd26263 13107static bool intel_fuzzy_clock_check(int clock1, int clock2)
f1f644dc 13108{
3bd26263 13109 int diff;
f1f644dc
JB
13110
13111 if (clock1 == clock2)
13112 return true;
13113
13114 if (!clock1 || !clock2)
13115 return false;
13116
13117 diff = abs(clock1 - clock2);
13118
13119 if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
13120 return true;
13121
13122 return false;
13123}
13124
25c5b266
DV
13125#define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
13126 list_for_each_entry((intel_crtc), \
13127 &(dev)->mode_config.crtc_list, \
13128 base.head) \
95150bdf 13129 for_each_if (mask & (1 <<(intel_crtc)->pipe))
25c5b266 13130
cfb23ed6
ML
13131static bool
13132intel_compare_m_n(unsigned int m, unsigned int n,
13133 unsigned int m2, unsigned int n2,
13134 bool exact)
13135{
13136 if (m == m2 && n == n2)
13137 return true;
13138
13139 if (exact || !m || !n || !m2 || !n2)
13140 return false;
13141
13142 BUILD_BUG_ON(DATA_LINK_M_N_MASK > INT_MAX);
13143
31d10b57
ML
13144 if (n > n2) {
13145 while (n > n2) {
cfb23ed6
ML
13146 m2 <<= 1;
13147 n2 <<= 1;
13148 }
31d10b57
ML
13149 } else if (n < n2) {
13150 while (n < n2) {
cfb23ed6
ML
13151 m <<= 1;
13152 n <<= 1;
13153 }
13154 }
13155
31d10b57
ML
13156 if (n != n2)
13157 return false;
13158
13159 return intel_fuzzy_clock_check(m, m2);
cfb23ed6
ML
13160}
13161
13162static bool
13163intel_compare_link_m_n(const struct intel_link_m_n *m_n,
13164 struct intel_link_m_n *m2_n2,
13165 bool adjust)
13166{
13167 if (m_n->tu == m2_n2->tu &&
13168 intel_compare_m_n(m_n->gmch_m, m_n->gmch_n,
13169 m2_n2->gmch_m, m2_n2->gmch_n, !adjust) &&
13170 intel_compare_m_n(m_n->link_m, m_n->link_n,
13171 m2_n2->link_m, m2_n2->link_n, !adjust)) {
13172 if (adjust)
13173 *m2_n2 = *m_n;
13174
13175 return true;
13176 }
13177
13178 return false;
13179}
13180
0e8ffe1b 13181static bool
2fa2fe9a 13182intel_pipe_config_compare(struct drm_device *dev,
5cec258b 13183 struct intel_crtc_state *current_config,
cfb23ed6
ML
13184 struct intel_crtc_state *pipe_config,
13185 bool adjust)
0e8ffe1b 13186{
cfb23ed6
ML
13187 bool ret = true;
13188
13189#define INTEL_ERR_OR_DBG_KMS(fmt, ...) \
13190 do { \
13191 if (!adjust) \
13192 DRM_ERROR(fmt, ##__VA_ARGS__); \
13193 else \
13194 DRM_DEBUG_KMS(fmt, ##__VA_ARGS__); \
13195 } while (0)
13196
66e985c0
DV
13197#define PIPE_CONF_CHECK_X(name) \
13198 if (current_config->name != pipe_config->name) { \
cfb23ed6 13199 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
66e985c0
DV
13200 "(expected 0x%08x, found 0x%08x)\n", \
13201 current_config->name, \
13202 pipe_config->name); \
cfb23ed6 13203 ret = false; \
66e985c0
DV
13204 }
13205
08a24034
DV
13206#define PIPE_CONF_CHECK_I(name) \
13207 if (current_config->name != pipe_config->name) { \
cfb23ed6 13208 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
08a24034
DV
13209 "(expected %i, found %i)\n", \
13210 current_config->name, \
13211 pipe_config->name); \
cfb23ed6
ML
13212 ret = false; \
13213 }
13214
8106ddbd
ACO
13215#define PIPE_CONF_CHECK_P(name) \
13216 if (current_config->name != pipe_config->name) { \
13217 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
13218 "(expected %p, found %p)\n", \
13219 current_config->name, \
13220 pipe_config->name); \
13221 ret = false; \
13222 }
13223
cfb23ed6
ML
13224#define PIPE_CONF_CHECK_M_N(name) \
13225 if (!intel_compare_link_m_n(&current_config->name, \
13226 &pipe_config->name,\
13227 adjust)) { \
13228 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
13229 "(expected tu %i gmch %i/%i link %i/%i, " \
13230 "found tu %i, gmch %i/%i link %i/%i)\n", \
13231 current_config->name.tu, \
13232 current_config->name.gmch_m, \
13233 current_config->name.gmch_n, \
13234 current_config->name.link_m, \
13235 current_config->name.link_n, \
13236 pipe_config->name.tu, \
13237 pipe_config->name.gmch_m, \
13238 pipe_config->name.gmch_n, \
13239 pipe_config->name.link_m, \
13240 pipe_config->name.link_n); \
13241 ret = false; \
13242 }
13243
55c561a7
DV
13244/* This is required for BDW+ where there is only one set of registers for
13245 * switching between high and low RR.
13246 * This macro can be used whenever a comparison has to be made between one
13247 * hw state and multiple sw state variables.
13248 */
cfb23ed6
ML
13249#define PIPE_CONF_CHECK_M_N_ALT(name, alt_name) \
13250 if (!intel_compare_link_m_n(&current_config->name, \
13251 &pipe_config->name, adjust) && \
13252 !intel_compare_link_m_n(&current_config->alt_name, \
13253 &pipe_config->name, adjust)) { \
13254 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
13255 "(expected tu %i gmch %i/%i link %i/%i, " \
13256 "or tu %i gmch %i/%i link %i/%i, " \
13257 "found tu %i, gmch %i/%i link %i/%i)\n", \
13258 current_config->name.tu, \
13259 current_config->name.gmch_m, \
13260 current_config->name.gmch_n, \
13261 current_config->name.link_m, \
13262 current_config->name.link_n, \
13263 current_config->alt_name.tu, \
13264 current_config->alt_name.gmch_m, \
13265 current_config->alt_name.gmch_n, \
13266 current_config->alt_name.link_m, \
13267 current_config->alt_name.link_n, \
13268 pipe_config->name.tu, \
13269 pipe_config->name.gmch_m, \
13270 pipe_config->name.gmch_n, \
13271 pipe_config->name.link_m, \
13272 pipe_config->name.link_n); \
13273 ret = false; \
88adfff1
DV
13274 }
13275
1bd1bd80
DV
13276#define PIPE_CONF_CHECK_FLAGS(name, mask) \
13277 if ((current_config->name ^ pipe_config->name) & (mask)) { \
cfb23ed6 13278 INTEL_ERR_OR_DBG_KMS("mismatch in " #name "(" #mask ") " \
1bd1bd80
DV
13279 "(expected %i, found %i)\n", \
13280 current_config->name & (mask), \
13281 pipe_config->name & (mask)); \
cfb23ed6 13282 ret = false; \
1bd1bd80
DV
13283 }
13284
5e550656
VS
13285#define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
13286 if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
cfb23ed6 13287 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
5e550656
VS
13288 "(expected %i, found %i)\n", \
13289 current_config->name, \
13290 pipe_config->name); \
cfb23ed6 13291 ret = false; \
5e550656
VS
13292 }
13293
bb760063
DV
13294#define PIPE_CONF_QUIRK(quirk) \
13295 ((current_config->quirks | pipe_config->quirks) & (quirk))
13296
eccb140b
DV
13297 PIPE_CONF_CHECK_I(cpu_transcoder);
13298
08a24034
DV
13299 PIPE_CONF_CHECK_I(has_pch_encoder);
13300 PIPE_CONF_CHECK_I(fdi_lanes);
cfb23ed6 13301 PIPE_CONF_CHECK_M_N(fdi_m_n);
08a24034 13302
90a6b7b0 13303 PIPE_CONF_CHECK_I(lane_count);
95a7a2ae 13304 PIPE_CONF_CHECK_X(lane_lat_optim_mask);
b95af8be
VK
13305
13306 if (INTEL_INFO(dev)->gen < 8) {
cfb23ed6
ML
13307 PIPE_CONF_CHECK_M_N(dp_m_n);
13308
cfb23ed6
ML
13309 if (current_config->has_drrs)
13310 PIPE_CONF_CHECK_M_N(dp_m2_n2);
13311 } else
13312 PIPE_CONF_CHECK_M_N_ALT(dp_m_n, dp_m2_n2);
eb14cb74 13313
253c84c8 13314 PIPE_CONF_CHECK_X(output_types);
a65347ba 13315
2d112de7
ACO
13316 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
13317 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
13318 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
13319 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
13320 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
13321 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
1bd1bd80 13322
2d112de7
ACO
13323 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
13324 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
13325 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
13326 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
13327 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
13328 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
1bd1bd80 13329
c93f54cf 13330 PIPE_CONF_CHECK_I(pixel_multiplier);
6897b4b5 13331 PIPE_CONF_CHECK_I(has_hdmi_sink);
b5a9fa09 13332 if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
666a4537 13333 IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev))
b5a9fa09 13334 PIPE_CONF_CHECK_I(limited_color_range);
e43823ec 13335 PIPE_CONF_CHECK_I(has_infoframe);
6c49f241 13336
9ed109a7
DV
13337 PIPE_CONF_CHECK_I(has_audio);
13338
2d112de7 13339 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
1bd1bd80
DV
13340 DRM_MODE_FLAG_INTERLACE);
13341
bb760063 13342 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
2d112de7 13343 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063 13344 DRM_MODE_FLAG_PHSYNC);
2d112de7 13345 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063 13346 DRM_MODE_FLAG_NHSYNC);
2d112de7 13347 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063 13348 DRM_MODE_FLAG_PVSYNC);
2d112de7 13349 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063
DV
13350 DRM_MODE_FLAG_NVSYNC);
13351 }
045ac3b5 13352
333b8ca8 13353 PIPE_CONF_CHECK_X(gmch_pfit.control);
e2ff2d4a
DV
13354 /* pfit ratios are autocomputed by the hw on gen4+ */
13355 if (INTEL_INFO(dev)->gen < 4)
7f7d8dd6 13356 PIPE_CONF_CHECK_X(gmch_pfit.pgm_ratios);
333b8ca8 13357 PIPE_CONF_CHECK_X(gmch_pfit.lvds_border_bits);
9953599b 13358
bfd16b2a
ML
13359 if (!adjust) {
13360 PIPE_CONF_CHECK_I(pipe_src_w);
13361 PIPE_CONF_CHECK_I(pipe_src_h);
13362
13363 PIPE_CONF_CHECK_I(pch_pfit.enabled);
13364 if (current_config->pch_pfit.enabled) {
13365 PIPE_CONF_CHECK_X(pch_pfit.pos);
13366 PIPE_CONF_CHECK_X(pch_pfit.size);
13367 }
2fa2fe9a 13368
7aefe2b5
ML
13369 PIPE_CONF_CHECK_I(scaler_state.scaler_id);
13370 }
a1b2278e 13371
e59150dc
JB
13372 /* BDW+ don't expose a synchronous way to read the state */
13373 if (IS_HASWELL(dev))
13374 PIPE_CONF_CHECK_I(ips_enabled);
42db64ef 13375
282740f7
VS
13376 PIPE_CONF_CHECK_I(double_wide);
13377
26804afd
DV
13378 PIPE_CONF_CHECK_X(ddi_pll_sel);
13379
8106ddbd 13380 PIPE_CONF_CHECK_P(shared_dpll);
66e985c0 13381 PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
8bcc2795 13382 PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
66e985c0
DV
13383 PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
13384 PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
d452c5b6 13385 PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
00490c22 13386 PIPE_CONF_CHECK_X(dpll_hw_state.spll);
3f4cd19f
DL
13387 PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
13388 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
13389 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
c0d43d62 13390
47eacbab
VS
13391 PIPE_CONF_CHECK_X(dsi_pll.ctrl);
13392 PIPE_CONF_CHECK_X(dsi_pll.div);
13393
42571aef
VS
13394 if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
13395 PIPE_CONF_CHECK_I(pipe_bpp);
13396
2d112de7 13397 PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
a9a7e98a 13398 PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
5e550656 13399
66e985c0 13400#undef PIPE_CONF_CHECK_X
08a24034 13401#undef PIPE_CONF_CHECK_I
8106ddbd 13402#undef PIPE_CONF_CHECK_P
1bd1bd80 13403#undef PIPE_CONF_CHECK_FLAGS
5e550656 13404#undef PIPE_CONF_CHECK_CLOCK_FUZZY
bb760063 13405#undef PIPE_CONF_QUIRK
cfb23ed6 13406#undef INTEL_ERR_OR_DBG_KMS
88adfff1 13407
cfb23ed6 13408 return ret;
0e8ffe1b
DV
13409}
13410
e3b247da
VS
13411static void intel_pipe_config_sanity_check(struct drm_i915_private *dev_priv,
13412 const struct intel_crtc_state *pipe_config)
13413{
13414 if (pipe_config->has_pch_encoder) {
21a727b3 13415 int fdi_dotclock = intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
e3b247da
VS
13416 &pipe_config->fdi_m_n);
13417 int dotclock = pipe_config->base.adjusted_mode.crtc_clock;
13418
13419 /*
13420 * FDI already provided one idea for the dotclock.
13421 * Yell if the encoder disagrees.
13422 */
13423 WARN(!intel_fuzzy_clock_check(fdi_dotclock, dotclock),
13424 "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
13425 fdi_dotclock, dotclock);
13426 }
13427}
13428
c0ead703
ML
13429static void verify_wm_state(struct drm_crtc *crtc,
13430 struct drm_crtc_state *new_state)
08db6652 13431{
e7c84544 13432 struct drm_device *dev = crtc->dev;
fac5e23e 13433 struct drm_i915_private *dev_priv = to_i915(dev);
08db6652 13434 struct skl_ddb_allocation hw_ddb, *sw_ddb;
e7c84544
ML
13435 struct skl_ddb_entry *hw_entry, *sw_entry;
13436 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13437 const enum pipe pipe = intel_crtc->pipe;
08db6652
DL
13438 int plane;
13439
e7c84544 13440 if (INTEL_INFO(dev)->gen < 9 || !new_state->active)
08db6652
DL
13441 return;
13442
13443 skl_ddb_get_hw_state(dev_priv, &hw_ddb);
13444 sw_ddb = &dev_priv->wm.skl_hw.ddb;
13445
e7c84544
ML
13446 /* planes */
13447 for_each_plane(dev_priv, pipe, plane) {
13448 hw_entry = &hw_ddb.plane[pipe][plane];
13449 sw_entry = &sw_ddb->plane[pipe][plane];
08db6652 13450
e7c84544 13451 if (skl_ddb_entry_equal(hw_entry, sw_entry))
08db6652
DL
13452 continue;
13453
e7c84544
ML
13454 DRM_ERROR("mismatch in DDB state pipe %c plane %d "
13455 "(expected (%u,%u), found (%u,%u))\n",
13456 pipe_name(pipe), plane + 1,
13457 sw_entry->start, sw_entry->end,
13458 hw_entry->start, hw_entry->end);
13459 }
08db6652 13460
e7c84544
ML
13461 /* cursor */
13462 hw_entry = &hw_ddb.plane[pipe][PLANE_CURSOR];
13463 sw_entry = &sw_ddb->plane[pipe][PLANE_CURSOR];
08db6652 13464
e7c84544 13465 if (!skl_ddb_entry_equal(hw_entry, sw_entry)) {
08db6652
DL
13466 DRM_ERROR("mismatch in DDB state pipe %c cursor "
13467 "(expected (%u,%u), found (%u,%u))\n",
13468 pipe_name(pipe),
13469 sw_entry->start, sw_entry->end,
13470 hw_entry->start, hw_entry->end);
13471 }
13472}
13473
91d1b4bd 13474static void
c0ead703 13475verify_connector_state(struct drm_device *dev, struct drm_crtc *crtc)
8af6cf88 13476{
35dd3c64 13477 struct drm_connector *connector;
8af6cf88 13478
e7c84544 13479 drm_for_each_connector(connector, dev) {
35dd3c64
ML
13480 struct drm_encoder *encoder = connector->encoder;
13481 struct drm_connector_state *state = connector->state;
ad3c558f 13482
e7c84544
ML
13483 if (state->crtc != crtc)
13484 continue;
13485
5a21b665 13486 intel_connector_verify_state(to_intel_connector(connector));
8af6cf88 13487
ad3c558f 13488 I915_STATE_WARN(state->best_encoder != encoder,
35dd3c64 13489 "connector's atomic encoder doesn't match legacy encoder\n");
8af6cf88 13490 }
91d1b4bd
DV
13491}
13492
13493static void
c0ead703 13494verify_encoder_state(struct drm_device *dev)
91d1b4bd
DV
13495{
13496 struct intel_encoder *encoder;
13497 struct intel_connector *connector;
8af6cf88 13498
b2784e15 13499 for_each_intel_encoder(dev, encoder) {
8af6cf88 13500 bool enabled = false;
4d20cd86 13501 enum pipe pipe;
8af6cf88
DV
13502
13503 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
13504 encoder->base.base.id,
8e329a03 13505 encoder->base.name);
8af6cf88 13506
3a3371ff 13507 for_each_intel_connector(dev, connector) {
4d20cd86 13508 if (connector->base.state->best_encoder != &encoder->base)
8af6cf88
DV
13509 continue;
13510 enabled = true;
ad3c558f
ML
13511
13512 I915_STATE_WARN(connector->base.state->crtc !=
13513 encoder->base.crtc,
13514 "connector's crtc doesn't match encoder crtc\n");
8af6cf88 13515 }
0e32b39c 13516
e2c719b7 13517 I915_STATE_WARN(!!encoder->base.crtc != enabled,
8af6cf88
DV
13518 "encoder's enabled state mismatch "
13519 "(expected %i, found %i)\n",
13520 !!encoder->base.crtc, enabled);
7c60d198
ML
13521
13522 if (!encoder->base.crtc) {
4d20cd86 13523 bool active;
7c60d198 13524
4d20cd86
ML
13525 active = encoder->get_hw_state(encoder, &pipe);
13526 I915_STATE_WARN(active,
13527 "encoder detached but still enabled on pipe %c.\n",
13528 pipe_name(pipe));
7c60d198 13529 }
8af6cf88 13530 }
91d1b4bd
DV
13531}
13532
13533static void
c0ead703
ML
13534verify_crtc_state(struct drm_crtc *crtc,
13535 struct drm_crtc_state *old_crtc_state,
13536 struct drm_crtc_state *new_crtc_state)
91d1b4bd 13537{
e7c84544 13538 struct drm_device *dev = crtc->dev;
fac5e23e 13539 struct drm_i915_private *dev_priv = to_i915(dev);
91d1b4bd 13540 struct intel_encoder *encoder;
e7c84544
ML
13541 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13542 struct intel_crtc_state *pipe_config, *sw_config;
13543 struct drm_atomic_state *old_state;
13544 bool active;
045ac3b5 13545
e7c84544 13546 old_state = old_crtc_state->state;
ec2dc6a0 13547 __drm_atomic_helper_crtc_destroy_state(old_crtc_state);
e7c84544
ML
13548 pipe_config = to_intel_crtc_state(old_crtc_state);
13549 memset(pipe_config, 0, sizeof(*pipe_config));
13550 pipe_config->base.crtc = crtc;
13551 pipe_config->base.state = old_state;
8af6cf88 13552
78108b7c 13553 DRM_DEBUG_KMS("[CRTC:%d:%s]\n", crtc->base.id, crtc->name);
8af6cf88 13554
e7c84544 13555 active = dev_priv->display.get_pipe_config(intel_crtc, pipe_config);
d62cf62a 13556
e7c84544
ML
13557 /* hw state is inconsistent with the pipe quirk */
13558 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
13559 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
13560 active = new_crtc_state->active;
6c49f241 13561
e7c84544
ML
13562 I915_STATE_WARN(new_crtc_state->active != active,
13563 "crtc active state doesn't match with hw state "
13564 "(expected %i, found %i)\n", new_crtc_state->active, active);
0e8ffe1b 13565
e7c84544
ML
13566 I915_STATE_WARN(intel_crtc->active != new_crtc_state->active,
13567 "transitional active state does not match atomic hw state "
13568 "(expected %i, found %i)\n", new_crtc_state->active, intel_crtc->active);
4d20cd86 13569
e7c84544
ML
13570 for_each_encoder_on_crtc(dev, crtc, encoder) {
13571 enum pipe pipe;
4d20cd86 13572
e7c84544
ML
13573 active = encoder->get_hw_state(encoder, &pipe);
13574 I915_STATE_WARN(active != new_crtc_state->active,
13575 "[ENCODER:%i] active %i with crtc active %i\n",
13576 encoder->base.base.id, active, new_crtc_state->active);
4d20cd86 13577
e7c84544
ML
13578 I915_STATE_WARN(active && intel_crtc->pipe != pipe,
13579 "Encoder connected to wrong pipe %c\n",
13580 pipe_name(pipe));
4d20cd86 13581
253c84c8
VS
13582 if (active) {
13583 pipe_config->output_types |= 1 << encoder->type;
e7c84544 13584 encoder->get_config(encoder, pipe_config);
253c84c8 13585 }
e7c84544 13586 }
53d9f4e9 13587
e7c84544
ML
13588 if (!new_crtc_state->active)
13589 return;
cfb23ed6 13590
e7c84544 13591 intel_pipe_config_sanity_check(dev_priv, pipe_config);
e3b247da 13592
e7c84544
ML
13593 sw_config = to_intel_crtc_state(crtc->state);
13594 if (!intel_pipe_config_compare(dev, sw_config,
13595 pipe_config, false)) {
13596 I915_STATE_WARN(1, "pipe state doesn't match!\n");
13597 intel_dump_pipe_config(intel_crtc, pipe_config,
13598 "[hw state]");
13599 intel_dump_pipe_config(intel_crtc, sw_config,
13600 "[sw state]");
8af6cf88
DV
13601 }
13602}
13603
91d1b4bd 13604static void
c0ead703
ML
13605verify_single_dpll_state(struct drm_i915_private *dev_priv,
13606 struct intel_shared_dpll *pll,
13607 struct drm_crtc *crtc,
13608 struct drm_crtc_state *new_state)
91d1b4bd 13609{
91d1b4bd 13610 struct intel_dpll_hw_state dpll_hw_state;
e7c84544
ML
13611 unsigned crtc_mask;
13612 bool active;
5358901f 13613
e7c84544 13614 memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
5358901f 13615
e7c84544 13616 DRM_DEBUG_KMS("%s\n", pll->name);
5358901f 13617
e7c84544 13618 active = pll->funcs.get_hw_state(dev_priv, pll, &dpll_hw_state);
5358901f 13619
e7c84544
ML
13620 if (!(pll->flags & INTEL_DPLL_ALWAYS_ON)) {
13621 I915_STATE_WARN(!pll->on && pll->active_mask,
13622 "pll in active use but not on in sw tracking\n");
13623 I915_STATE_WARN(pll->on && !pll->active_mask,
13624 "pll is on but not used by any active crtc\n");
13625 I915_STATE_WARN(pll->on != active,
13626 "pll on state mismatch (expected %i, found %i)\n",
13627 pll->on, active);
13628 }
5358901f 13629
e7c84544 13630 if (!crtc) {
2dd66ebd 13631 I915_STATE_WARN(pll->active_mask & ~pll->config.crtc_mask,
e7c84544
ML
13632 "more active pll users than references: %x vs %x\n",
13633 pll->active_mask, pll->config.crtc_mask);
5358901f 13634
e7c84544
ML
13635 return;
13636 }
13637
13638 crtc_mask = 1 << drm_crtc_index(crtc);
13639
13640 if (new_state->active)
13641 I915_STATE_WARN(!(pll->active_mask & crtc_mask),
13642 "pll active mismatch (expected pipe %c in active mask 0x%02x)\n",
13643 pipe_name(drm_crtc_index(crtc)), pll->active_mask);
13644 else
13645 I915_STATE_WARN(pll->active_mask & crtc_mask,
13646 "pll active mismatch (didn't expect pipe %c in active mask 0x%02x)\n",
13647 pipe_name(drm_crtc_index(crtc)), pll->active_mask);
2dd66ebd 13648
e7c84544
ML
13649 I915_STATE_WARN(!(pll->config.crtc_mask & crtc_mask),
13650 "pll enabled crtcs mismatch (expected 0x%x in 0x%02x)\n",
13651 crtc_mask, pll->config.crtc_mask);
66e985c0 13652
e7c84544
ML
13653 I915_STATE_WARN(pll->on && memcmp(&pll->config.hw_state,
13654 &dpll_hw_state,
13655 sizeof(dpll_hw_state)),
13656 "pll hw state mismatch\n");
13657}
13658
13659static void
c0ead703
ML
13660verify_shared_dpll_state(struct drm_device *dev, struct drm_crtc *crtc,
13661 struct drm_crtc_state *old_crtc_state,
13662 struct drm_crtc_state *new_crtc_state)
e7c84544 13663{
fac5e23e 13664 struct drm_i915_private *dev_priv = to_i915(dev);
e7c84544
ML
13665 struct intel_crtc_state *old_state = to_intel_crtc_state(old_crtc_state);
13666 struct intel_crtc_state *new_state = to_intel_crtc_state(new_crtc_state);
13667
13668 if (new_state->shared_dpll)
c0ead703 13669 verify_single_dpll_state(dev_priv, new_state->shared_dpll, crtc, new_crtc_state);
e7c84544
ML
13670
13671 if (old_state->shared_dpll &&
13672 old_state->shared_dpll != new_state->shared_dpll) {
13673 unsigned crtc_mask = 1 << drm_crtc_index(crtc);
13674 struct intel_shared_dpll *pll = old_state->shared_dpll;
13675
13676 I915_STATE_WARN(pll->active_mask & crtc_mask,
13677 "pll active mismatch (didn't expect pipe %c in active mask)\n",
13678 pipe_name(drm_crtc_index(crtc)));
13679 I915_STATE_WARN(pll->config.crtc_mask & crtc_mask,
13680 "pll enabled crtcs mismatch (found %x in enabled mask)\n",
13681 pipe_name(drm_crtc_index(crtc)));
5358901f 13682 }
8af6cf88
DV
13683}
13684
e7c84544 13685static void
c0ead703 13686intel_modeset_verify_crtc(struct drm_crtc *crtc,
e7c84544
ML
13687 struct drm_crtc_state *old_state,
13688 struct drm_crtc_state *new_state)
13689{
5a21b665
DV
13690 if (!needs_modeset(new_state) &&
13691 !to_intel_crtc_state(new_state)->update_pipe)
13692 return;
13693
c0ead703 13694 verify_wm_state(crtc, new_state);
5a21b665 13695 verify_connector_state(crtc->dev, crtc);
c0ead703
ML
13696 verify_crtc_state(crtc, old_state, new_state);
13697 verify_shared_dpll_state(crtc->dev, crtc, old_state, new_state);
e7c84544
ML
13698}
13699
13700static void
c0ead703 13701verify_disabled_dpll_state(struct drm_device *dev)
e7c84544 13702{
fac5e23e 13703 struct drm_i915_private *dev_priv = to_i915(dev);
e7c84544
ML
13704 int i;
13705
13706 for (i = 0; i < dev_priv->num_shared_dpll; i++)
c0ead703 13707 verify_single_dpll_state(dev_priv, &dev_priv->shared_dplls[i], NULL, NULL);
e7c84544
ML
13708}
13709
13710static void
c0ead703 13711intel_modeset_verify_disabled(struct drm_device *dev)
e7c84544 13712{
c0ead703
ML
13713 verify_encoder_state(dev);
13714 verify_connector_state(dev, NULL);
13715 verify_disabled_dpll_state(dev);
e7c84544
ML
13716}
13717
80715b2f
VS
13718static void update_scanline_offset(struct intel_crtc *crtc)
13719{
13720 struct drm_device *dev = crtc->base.dev;
13721
13722 /*
13723 * The scanline counter increments at the leading edge of hsync.
13724 *
13725 * On most platforms it starts counting from vtotal-1 on the
13726 * first active line. That means the scanline counter value is
13727 * always one less than what we would expect. Ie. just after
13728 * start of vblank, which also occurs at start of hsync (on the
13729 * last active line), the scanline counter will read vblank_start-1.
13730 *
13731 * On gen2 the scanline counter starts counting from 1 instead
13732 * of vtotal-1, so we have to subtract one (or rather add vtotal-1
13733 * to keep the value positive), instead of adding one.
13734 *
13735 * On HSW+ the behaviour of the scanline counter depends on the output
13736 * type. For DP ports it behaves like most other platforms, but on HDMI
13737 * there's an extra 1 line difference. So we need to add two instead of
13738 * one to the value.
13739 */
13740 if (IS_GEN2(dev)) {
124abe07 13741 const struct drm_display_mode *adjusted_mode = &crtc->config->base.adjusted_mode;
80715b2f
VS
13742 int vtotal;
13743
124abe07
VS
13744 vtotal = adjusted_mode->crtc_vtotal;
13745 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
80715b2f
VS
13746 vtotal /= 2;
13747
13748 crtc->scanline_offset = vtotal - 1;
13749 } else if (HAS_DDI(dev) &&
2d84d2b3 13750 intel_crtc_has_type(crtc->config, INTEL_OUTPUT_HDMI)) {
80715b2f
VS
13751 crtc->scanline_offset = 2;
13752 } else
13753 crtc->scanline_offset = 1;
13754}
13755
ad421372 13756static void intel_modeset_clear_plls(struct drm_atomic_state *state)
ed6739ef 13757{
225da59b 13758 struct drm_device *dev = state->dev;
ed6739ef 13759 struct drm_i915_private *dev_priv = to_i915(dev);
ad421372 13760 struct intel_shared_dpll_config *shared_dpll = NULL;
0a9ab303
ACO
13761 struct drm_crtc *crtc;
13762 struct drm_crtc_state *crtc_state;
0a9ab303 13763 int i;
ed6739ef
ACO
13764
13765 if (!dev_priv->display.crtc_compute_clock)
ad421372 13766 return;
ed6739ef 13767
0a9ab303 13768 for_each_crtc_in_state(state, crtc, crtc_state, i) {
fb1a38a9 13769 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8106ddbd
ACO
13770 struct intel_shared_dpll *old_dpll =
13771 to_intel_crtc_state(crtc->state)->shared_dpll;
0a9ab303 13772
fb1a38a9 13773 if (!needs_modeset(crtc_state))
225da59b
ACO
13774 continue;
13775
8106ddbd 13776 to_intel_crtc_state(crtc_state)->shared_dpll = NULL;
fb1a38a9 13777
8106ddbd 13778 if (!old_dpll)
fb1a38a9 13779 continue;
0a9ab303 13780
ad421372
ML
13781 if (!shared_dpll)
13782 shared_dpll = intel_atomic_get_shared_dpll_state(state);
ed6739ef 13783
8106ddbd 13784 intel_shared_dpll_config_put(shared_dpll, old_dpll, intel_crtc);
ad421372 13785 }
ed6739ef
ACO
13786}
13787
99d736a2
ML
13788/*
13789 * This implements the workaround described in the "notes" section of the mode
13790 * set sequence documentation. When going from no pipes or single pipe to
13791 * multiple pipes, and planes are enabled after the pipe, we need to wait at
13792 * least 2 vblanks on the first pipe before enabling planes on the second pipe.
13793 */
13794static int haswell_mode_set_planes_workaround(struct drm_atomic_state *state)
13795{
13796 struct drm_crtc_state *crtc_state;
13797 struct intel_crtc *intel_crtc;
13798 struct drm_crtc *crtc;
13799 struct intel_crtc_state *first_crtc_state = NULL;
13800 struct intel_crtc_state *other_crtc_state = NULL;
13801 enum pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
13802 int i;
13803
13804 /* look at all crtc's that are going to be enabled in during modeset */
13805 for_each_crtc_in_state(state, crtc, crtc_state, i) {
13806 intel_crtc = to_intel_crtc(crtc);
13807
13808 if (!crtc_state->active || !needs_modeset(crtc_state))
13809 continue;
13810
13811 if (first_crtc_state) {
13812 other_crtc_state = to_intel_crtc_state(crtc_state);
13813 break;
13814 } else {
13815 first_crtc_state = to_intel_crtc_state(crtc_state);
13816 first_pipe = intel_crtc->pipe;
13817 }
13818 }
13819
13820 /* No workaround needed? */
13821 if (!first_crtc_state)
13822 return 0;
13823
13824 /* w/a possibly needed, check how many crtc's are already enabled. */
13825 for_each_intel_crtc(state->dev, intel_crtc) {
13826 struct intel_crtc_state *pipe_config;
13827
13828 pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
13829 if (IS_ERR(pipe_config))
13830 return PTR_ERR(pipe_config);
13831
13832 pipe_config->hsw_workaround_pipe = INVALID_PIPE;
13833
13834 if (!pipe_config->base.active ||
13835 needs_modeset(&pipe_config->base))
13836 continue;
13837
13838 /* 2 or more enabled crtcs means no need for w/a */
13839 if (enabled_pipe != INVALID_PIPE)
13840 return 0;
13841
13842 enabled_pipe = intel_crtc->pipe;
13843 }
13844
13845 if (enabled_pipe != INVALID_PIPE)
13846 first_crtc_state->hsw_workaround_pipe = enabled_pipe;
13847 else if (other_crtc_state)
13848 other_crtc_state->hsw_workaround_pipe = first_pipe;
13849
13850 return 0;
13851}
13852
27c329ed
ML
13853static int intel_modeset_all_pipes(struct drm_atomic_state *state)
13854{
13855 struct drm_crtc *crtc;
13856 struct drm_crtc_state *crtc_state;
13857 int ret = 0;
13858
13859 /* add all active pipes to the state */
13860 for_each_crtc(state->dev, crtc) {
13861 crtc_state = drm_atomic_get_crtc_state(state, crtc);
13862 if (IS_ERR(crtc_state))
13863 return PTR_ERR(crtc_state);
13864
13865 if (!crtc_state->active || needs_modeset(crtc_state))
13866 continue;
13867
13868 crtc_state->mode_changed = true;
13869
13870 ret = drm_atomic_add_affected_connectors(state, crtc);
13871 if (ret)
13872 break;
13873
13874 ret = drm_atomic_add_affected_planes(state, crtc);
13875 if (ret)
13876 break;
13877 }
13878
13879 return ret;
13880}
13881
c347a676 13882static int intel_modeset_checks(struct drm_atomic_state *state)
054518dd 13883{
565602d7 13884 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
fac5e23e 13885 struct drm_i915_private *dev_priv = to_i915(state->dev);
565602d7
ML
13886 struct drm_crtc *crtc;
13887 struct drm_crtc_state *crtc_state;
13888 int ret = 0, i;
054518dd 13889
b359283a
ML
13890 if (!check_digital_port_conflicts(state)) {
13891 DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
13892 return -EINVAL;
13893 }
13894
565602d7
ML
13895 intel_state->modeset = true;
13896 intel_state->active_crtcs = dev_priv->active_crtcs;
13897
13898 for_each_crtc_in_state(state, crtc, crtc_state, i) {
13899 if (crtc_state->active)
13900 intel_state->active_crtcs |= 1 << i;
13901 else
13902 intel_state->active_crtcs &= ~(1 << i);
8b4a7d05
MR
13903
13904 if (crtc_state->active != crtc->state->active)
13905 intel_state->active_pipe_changes |= drm_crtc_mask(crtc);
565602d7
ML
13906 }
13907
054518dd
ACO
13908 /*
13909 * See if the config requires any additional preparation, e.g.
13910 * to adjust global state with pipes off. We need to do this
13911 * here so we can get the modeset_pipe updated config for the new
13912 * mode set on this crtc. For other crtcs we need to use the
13913 * adjusted_mode bits in the crtc directly.
13914 */
27c329ed 13915 if (dev_priv->display.modeset_calc_cdclk) {
c89e39f3 13916 if (!intel_state->cdclk_pll_vco)
63911d72 13917 intel_state->cdclk_pll_vco = dev_priv->cdclk_pll.vco;
b2045352
VS
13918 if (!intel_state->cdclk_pll_vco)
13919 intel_state->cdclk_pll_vco = dev_priv->skl_preferred_vco_freq;
c89e39f3 13920
27c329ed 13921 ret = dev_priv->display.modeset_calc_cdclk(state);
c89e39f3
CT
13922 if (ret < 0)
13923 return ret;
27c329ed 13924
c89e39f3 13925 if (intel_state->dev_cdclk != dev_priv->cdclk_freq ||
63911d72 13926 intel_state->cdclk_pll_vco != dev_priv->cdclk_pll.vco)
27c329ed
ML
13927 ret = intel_modeset_all_pipes(state);
13928
13929 if (ret < 0)
054518dd 13930 return ret;
e8788cbc
ML
13931
13932 DRM_DEBUG_KMS("New cdclk calculated to be atomic %u, actual %u\n",
13933 intel_state->cdclk, intel_state->dev_cdclk);
27c329ed 13934 } else
1a617b77 13935 to_intel_atomic_state(state)->cdclk = dev_priv->atomic_cdclk_freq;
054518dd 13936
ad421372 13937 intel_modeset_clear_plls(state);
054518dd 13938
565602d7 13939 if (IS_HASWELL(dev_priv))
ad421372 13940 return haswell_mode_set_planes_workaround(state);
99d736a2 13941
ad421372 13942 return 0;
c347a676
ACO
13943}
13944
aa363136
MR
13945/*
13946 * Handle calculation of various watermark data at the end of the atomic check
13947 * phase. The code here should be run after the per-crtc and per-plane 'check'
13948 * handlers to ensure that all derived state has been updated.
13949 */
55994c2c 13950static int calc_watermark_data(struct drm_atomic_state *state)
aa363136
MR
13951{
13952 struct drm_device *dev = state->dev;
98d39494 13953 struct drm_i915_private *dev_priv = to_i915(dev);
98d39494
MR
13954
13955 /* Is there platform-specific watermark information to calculate? */
13956 if (dev_priv->display.compute_global_watermarks)
55994c2c
MR
13957 return dev_priv->display.compute_global_watermarks(state);
13958
13959 return 0;
aa363136
MR
13960}
13961
74c090b1
ML
13962/**
13963 * intel_atomic_check - validate state object
13964 * @dev: drm device
13965 * @state: state to validate
13966 */
13967static int intel_atomic_check(struct drm_device *dev,
13968 struct drm_atomic_state *state)
c347a676 13969{
dd8b3bdb 13970 struct drm_i915_private *dev_priv = to_i915(dev);
aa363136 13971 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
c347a676
ACO
13972 struct drm_crtc *crtc;
13973 struct drm_crtc_state *crtc_state;
13974 int ret, i;
61333b60 13975 bool any_ms = false;
c347a676 13976
74c090b1 13977 ret = drm_atomic_helper_check_modeset(dev, state);
054518dd
ACO
13978 if (ret)
13979 return ret;
13980
c347a676 13981 for_each_crtc_in_state(state, crtc, crtc_state, i) {
cfb23ed6
ML
13982 struct intel_crtc_state *pipe_config =
13983 to_intel_crtc_state(crtc_state);
1ed51de9
DV
13984
13985 /* Catch I915_MODE_FLAG_INHERITED */
13986 if (crtc_state->mode.private_flags != crtc->state->mode.private_flags)
13987 crtc_state->mode_changed = true;
cfb23ed6 13988
af4a879e 13989 if (!needs_modeset(crtc_state))
c347a676
ACO
13990 continue;
13991
af4a879e
DV
13992 if (!crtc_state->enable) {
13993 any_ms = true;
cfb23ed6 13994 continue;
af4a879e 13995 }
cfb23ed6 13996
26495481
DV
13997 /* FIXME: For only active_changed we shouldn't need to do any
13998 * state recomputation at all. */
13999
1ed51de9
DV
14000 ret = drm_atomic_add_affected_connectors(state, crtc);
14001 if (ret)
14002 return ret;
b359283a 14003
cfb23ed6 14004 ret = intel_modeset_pipe_config(crtc, pipe_config);
25aa1c39
ML
14005 if (ret) {
14006 intel_dump_pipe_config(to_intel_crtc(crtc),
14007 pipe_config, "[failed]");
c347a676 14008 return ret;
25aa1c39 14009 }
c347a676 14010
73831236 14011 if (i915.fastboot &&
dd8b3bdb 14012 intel_pipe_config_compare(dev,
cfb23ed6 14013 to_intel_crtc_state(crtc->state),
1ed51de9 14014 pipe_config, true)) {
26495481 14015 crtc_state->mode_changed = false;
bfd16b2a 14016 to_intel_crtc_state(crtc_state)->update_pipe = true;
26495481
DV
14017 }
14018
af4a879e 14019 if (needs_modeset(crtc_state))
26495481 14020 any_ms = true;
cfb23ed6 14021
af4a879e
DV
14022 ret = drm_atomic_add_affected_planes(state, crtc);
14023 if (ret)
14024 return ret;
61333b60 14025
26495481
DV
14026 intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
14027 needs_modeset(crtc_state) ?
14028 "[modeset]" : "[fastset]");
c347a676
ACO
14029 }
14030
61333b60
ML
14031 if (any_ms) {
14032 ret = intel_modeset_checks(state);
14033
14034 if (ret)
14035 return ret;
27c329ed 14036 } else
dd8b3bdb 14037 intel_state->cdclk = dev_priv->cdclk_freq;
76305b1a 14038
dd8b3bdb 14039 ret = drm_atomic_helper_check_planes(dev, state);
aa363136
MR
14040 if (ret)
14041 return ret;
14042
f51be2e0 14043 intel_fbc_choose_crtc(dev_priv, state);
55994c2c 14044 return calc_watermark_data(state);
054518dd
ACO
14045}
14046
5008e874
ML
14047static int intel_atomic_prepare_commit(struct drm_device *dev,
14048 struct drm_atomic_state *state,
81072bfd 14049 bool nonblock)
5008e874 14050{
fac5e23e 14051 struct drm_i915_private *dev_priv = to_i915(dev);
7580d774 14052 struct drm_plane_state *plane_state;
5008e874 14053 struct drm_crtc_state *crtc_state;
7580d774 14054 struct drm_plane *plane;
5008e874
ML
14055 struct drm_crtc *crtc;
14056 int i, ret;
14057
5a21b665
DV
14058 for_each_crtc_in_state(state, crtc, crtc_state, i) {
14059 if (state->legacy_cursor_update)
a6747b73
ML
14060 continue;
14061
5a21b665
DV
14062 ret = intel_crtc_wait_for_pending_flips(crtc);
14063 if (ret)
14064 return ret;
5008e874 14065
5a21b665
DV
14066 if (atomic_read(&to_intel_crtc(crtc)->unpin_work_count) >= 2)
14067 flush_workqueue(dev_priv->wq);
d55dbd06
ML
14068 }
14069
f935675f
ML
14070 ret = mutex_lock_interruptible(&dev->struct_mutex);
14071 if (ret)
14072 return ret;
14073
5008e874 14074 ret = drm_atomic_helper_prepare_planes(dev, state);
f7e5838b 14075 mutex_unlock(&dev->struct_mutex);
7580d774 14076
21daaeee 14077 if (!ret && !nonblock) {
7580d774
ML
14078 for_each_plane_in_state(state, plane, plane_state, i) {
14079 struct intel_plane_state *intel_plane_state =
14080 to_intel_plane_state(plane_state);
14081
14082 if (!intel_plane_state->wait_req)
14083 continue;
14084
776f3236
CW
14085 ret = i915_wait_request(intel_plane_state->wait_req,
14086 true, NULL, NULL);
f7e5838b 14087 if (ret) {
f4457ae7
CW
14088 /* Any hang should be swallowed by the wait */
14089 WARN_ON(ret == -EIO);
f7e5838b
CW
14090 mutex_lock(&dev->struct_mutex);
14091 drm_atomic_helper_cleanup_planes(dev, state);
14092 mutex_unlock(&dev->struct_mutex);
7580d774 14093 break;
f7e5838b 14094 }
7580d774 14095 }
7580d774 14096 }
5008e874
ML
14097
14098 return ret;
14099}
14100
a2991414
ML
14101u32 intel_crtc_get_vblank_counter(struct intel_crtc *crtc)
14102{
14103 struct drm_device *dev = crtc->base.dev;
14104
14105 if (!dev->max_vblank_count)
14106 return drm_accurate_vblank_count(&crtc->base);
14107
14108 return dev->driver->get_vblank_counter(dev, crtc->pipe);
14109}
14110
5a21b665
DV
14111static void intel_atomic_wait_for_vblanks(struct drm_device *dev,
14112 struct drm_i915_private *dev_priv,
14113 unsigned crtc_mask)
e8861675 14114{
5a21b665
DV
14115 unsigned last_vblank_count[I915_MAX_PIPES];
14116 enum pipe pipe;
14117 int ret;
e8861675 14118
5a21b665
DV
14119 if (!crtc_mask)
14120 return;
e8861675 14121
5a21b665
DV
14122 for_each_pipe(dev_priv, pipe) {
14123 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
e8861675 14124
5a21b665 14125 if (!((1 << pipe) & crtc_mask))
e8861675
ML
14126 continue;
14127
5a21b665
DV
14128 ret = drm_crtc_vblank_get(crtc);
14129 if (WARN_ON(ret != 0)) {
14130 crtc_mask &= ~(1 << pipe);
14131 continue;
e8861675
ML
14132 }
14133
5a21b665 14134 last_vblank_count[pipe] = drm_crtc_vblank_count(crtc);
e8861675
ML
14135 }
14136
5a21b665
DV
14137 for_each_pipe(dev_priv, pipe) {
14138 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
14139 long lret;
e8861675 14140
5a21b665
DV
14141 if (!((1 << pipe) & crtc_mask))
14142 continue;
d55dbd06 14143
5a21b665
DV
14144 lret = wait_event_timeout(dev->vblank[pipe].queue,
14145 last_vblank_count[pipe] !=
14146 drm_crtc_vblank_count(crtc),
14147 msecs_to_jiffies(50));
d55dbd06 14148
5a21b665 14149 WARN(!lret, "pipe %c vblank wait timed out\n", pipe_name(pipe));
d55dbd06 14150
5a21b665 14151 drm_crtc_vblank_put(crtc);
d55dbd06
ML
14152 }
14153}
14154
5a21b665 14155static bool needs_vblank_wait(struct intel_crtc_state *crtc_state)
a6747b73 14156{
5a21b665
DV
14157 /* fb updated, need to unpin old fb */
14158 if (crtc_state->fb_changed)
14159 return true;
a6747b73 14160
5a21b665
DV
14161 /* wm changes, need vblank before final wm's */
14162 if (crtc_state->update_wm_post)
14163 return true;
a6747b73 14164
5a21b665
DV
14165 /*
14166 * cxsr is re-enabled after vblank.
14167 * This is already handled by crtc_state->update_wm_post,
14168 * but added for clarity.
14169 */
14170 if (crtc_state->disable_cxsr)
14171 return true;
a6747b73 14172
5a21b665 14173 return false;
e8861675
ML
14174}
14175
94f05024 14176static void intel_atomic_commit_tail(struct drm_atomic_state *state)
a6778b3c 14177{
94f05024 14178 struct drm_device *dev = state->dev;
565602d7 14179 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
fac5e23e 14180 struct drm_i915_private *dev_priv = to_i915(dev);
29ceb0e6 14181 struct drm_crtc_state *old_crtc_state;
7580d774 14182 struct drm_crtc *crtc;
5a21b665 14183 struct intel_crtc_state *intel_cstate;
94f05024
DV
14184 struct drm_plane *plane;
14185 struct drm_plane_state *plane_state;
5a21b665
DV
14186 bool hw_check = intel_state->modeset;
14187 unsigned long put_domains[I915_MAX_PIPES] = {};
14188 unsigned crtc_vblank_mask = 0;
94f05024 14189 int i, ret;
a6778b3c 14190
94f05024
DV
14191 for_each_plane_in_state(state, plane, plane_state, i) {
14192 struct intel_plane_state *intel_plane_state =
14193 to_intel_plane_state(plane_state);
ea0000f0 14194
94f05024
DV
14195 if (!intel_plane_state->wait_req)
14196 continue;
d4afb8cc 14197
776f3236
CW
14198 ret = i915_wait_request(intel_plane_state->wait_req,
14199 true, NULL, NULL);
94f05024
DV
14200 /* EIO should be eaten, and we can't get interrupted in the
14201 * worker, and blocking commits have waited already. */
14202 WARN_ON(ret);
14203 }
1c5e19f8 14204
ea0000f0
DV
14205 drm_atomic_helper_wait_for_dependencies(state);
14206
565602d7
ML
14207 if (intel_state->modeset) {
14208 memcpy(dev_priv->min_pixclk, intel_state->min_pixclk,
14209 sizeof(intel_state->min_pixclk));
14210 dev_priv->active_crtcs = intel_state->active_crtcs;
1a617b77 14211 dev_priv->atomic_cdclk_freq = intel_state->cdclk;
5a21b665
DV
14212
14213 intel_display_power_get(dev_priv, POWER_DOMAIN_MODESET);
565602d7
ML
14214 }
14215
29ceb0e6 14216 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
a539205a
ML
14217 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
14218
5a21b665
DV
14219 if (needs_modeset(crtc->state) ||
14220 to_intel_crtc_state(crtc->state)->update_pipe) {
14221 hw_check = true;
14222
14223 put_domains[to_intel_crtc(crtc)->pipe] =
14224 modeset_get_crtc_power_domains(crtc,
14225 to_intel_crtc_state(crtc->state));
14226 }
14227
61333b60
ML
14228 if (!needs_modeset(crtc->state))
14229 continue;
14230
29ceb0e6 14231 intel_pre_plane_update(to_intel_crtc_state(old_crtc_state));
460da916 14232
29ceb0e6
VS
14233 if (old_crtc_state->active) {
14234 intel_crtc_disable_planes(crtc, old_crtc_state->plane_mask);
4a806558 14235 dev_priv->display.crtc_disable(to_intel_crtc_state(old_crtc_state), state);
eddfcbcd 14236 intel_crtc->active = false;
58f9c0bc 14237 intel_fbc_disable(intel_crtc);
eddfcbcd 14238 intel_disable_shared_dpll(intel_crtc);
9bbc8258
VS
14239
14240 /*
14241 * Underruns don't always raise
14242 * interrupts, so check manually.
14243 */
14244 intel_check_cpu_fifo_underruns(dev_priv);
14245 intel_check_pch_fifo_underruns(dev_priv);
b9001114
ML
14246
14247 if (!crtc->state->active)
14248 intel_update_watermarks(crtc);
a539205a 14249 }
b8cecdf5 14250 }
7758a113 14251
ea9d758d
DV
14252 /* Only after disabling all output pipelines that will be changed can we
14253 * update the the output configuration. */
4740b0f2 14254 intel_modeset_update_crtc_state(state);
f6e5b160 14255
565602d7 14256 if (intel_state->modeset) {
4740b0f2 14257 drm_atomic_helper_update_legacy_modeset_state(state->dev, state);
33c8df89
ML
14258
14259 if (dev_priv->display.modeset_commit_cdclk &&
c89e39f3 14260 (intel_state->dev_cdclk != dev_priv->cdclk_freq ||
63911d72 14261 intel_state->cdclk_pll_vco != dev_priv->cdclk_pll.vco))
33c8df89 14262 dev_priv->display.modeset_commit_cdclk(state);
f6d1973d 14263
656d1b89
L
14264 /*
14265 * SKL workaround: bspec recommends we disable the SAGV when we
14266 * have more then one pipe enabled
14267 */
14268 if (IS_SKYLAKE(dev_priv) && !skl_can_enable_sagv(state))
14269 skl_disable_sagv(dev_priv);
14270
c0ead703 14271 intel_modeset_verify_disabled(dev);
4740b0f2 14272 }
47fab737 14273
a6778b3c 14274 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
29ceb0e6 14275 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
f6ac4b2a
ML
14276 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
14277 bool modeset = needs_modeset(crtc->state);
5a21b665
DV
14278 struct intel_crtc_state *pipe_config =
14279 to_intel_crtc_state(crtc->state);
9f836f90 14280
f6ac4b2a 14281 if (modeset && crtc->state->active) {
a539205a 14282 update_scanline_offset(to_intel_crtc(crtc));
4a806558 14283 dev_priv->display.crtc_enable(pipe_config, state);
a539205a 14284 }
80715b2f 14285
1f7528c4
DV
14286 /* Complete events for now disable pipes here. */
14287 if (modeset && !crtc->state->active && crtc->state->event) {
14288 spin_lock_irq(&dev->event_lock);
14289 drm_crtc_send_vblank_event(crtc, crtc->state->event);
14290 spin_unlock_irq(&dev->event_lock);
14291
14292 crtc->state->event = NULL;
14293 }
14294
f6ac4b2a 14295 if (!modeset)
29ceb0e6 14296 intel_pre_plane_update(to_intel_crtc_state(old_crtc_state));
f6ac4b2a 14297
5a21b665
DV
14298 if (crtc->state->active &&
14299 drm_atomic_get_existing_plane_state(state, crtc->primary))
faf68d92 14300 intel_fbc_enable(intel_crtc, pipe_config, to_intel_plane_state(crtc->primary->state));
5a21b665 14301
1f7528c4 14302 if (crtc->state->active)
5a21b665 14303 drm_atomic_helper_commit_planes_on_crtc(old_crtc_state);
f6d1973d 14304
5a21b665
DV
14305 if (pipe_config->base.active && needs_vblank_wait(pipe_config))
14306 crtc_vblank_mask |= 1 << i;
177246a8
MR
14307 }
14308
94f05024
DV
14309 /* FIXME: We should call drm_atomic_helper_commit_hw_done() here
14310 * already, but still need the state for the delayed optimization. To
14311 * fix this:
14312 * - wrap the optimization/post_plane_update stuff into a per-crtc work.
14313 * - schedule that vblank worker _before_ calling hw_done
14314 * - at the start of commit_tail, cancel it _synchrously
14315 * - switch over to the vblank wait helper in the core after that since
14316 * we don't need out special handling any more.
14317 */
5a21b665
DV
14318 if (!state->legacy_cursor_update)
14319 intel_atomic_wait_for_vblanks(dev, dev_priv, crtc_vblank_mask);
14320
14321 /*
14322 * Now that the vblank has passed, we can go ahead and program the
14323 * optimal watermarks on platforms that need two-step watermark
14324 * programming.
14325 *
14326 * TODO: Move this (and other cleanup) to an async worker eventually.
14327 */
14328 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
14329 intel_cstate = to_intel_crtc_state(crtc->state);
14330
14331 if (dev_priv->display.optimize_watermarks)
14332 dev_priv->display.optimize_watermarks(intel_cstate);
14333 }
14334
14335 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
14336 intel_post_plane_update(to_intel_crtc_state(old_crtc_state));
14337
14338 if (put_domains[i])
14339 modeset_put_power_domains(dev_priv, put_domains[i]);
14340
14341 intel_modeset_verify_crtc(crtc, old_crtc_state, crtc->state);
14342 }
14343
656d1b89
L
14344 if (IS_SKYLAKE(dev_priv) && intel_state->modeset &&
14345 skl_can_enable_sagv(state))
14346 skl_enable_sagv(dev_priv);
14347
94f05024
DV
14348 drm_atomic_helper_commit_hw_done(state);
14349
5a21b665
DV
14350 if (intel_state->modeset)
14351 intel_display_power_put(dev_priv, POWER_DOMAIN_MODESET);
14352
14353 mutex_lock(&dev->struct_mutex);
14354 drm_atomic_helper_cleanup_planes(dev, state);
14355 mutex_unlock(&dev->struct_mutex);
14356
ea0000f0
DV
14357 drm_atomic_helper_commit_cleanup_done(state);
14358
ee165b1a 14359 drm_atomic_state_free(state);
f30da187 14360
75714940
MK
14361 /* As one of the primary mmio accessors, KMS has a high likelihood
14362 * of triggering bugs in unclaimed access. After we finish
14363 * modesetting, see if an error has been flagged, and if so
14364 * enable debugging for the next modeset - and hope we catch
14365 * the culprit.
14366 *
14367 * XXX note that we assume display power is on at this point.
14368 * This might hold true now but we need to add pm helper to check
14369 * unclaimed only when the hardware is on, as atomic commits
14370 * can happen also when the device is completely off.
14371 */
14372 intel_uncore_arm_unclaimed_mmio_detection(dev_priv);
94f05024
DV
14373}
14374
14375static void intel_atomic_commit_work(struct work_struct *work)
14376{
14377 struct drm_atomic_state *state = container_of(work,
14378 struct drm_atomic_state,
14379 commit_work);
14380 intel_atomic_commit_tail(state);
14381}
14382
6c9c1b38
DV
14383static void intel_atomic_track_fbs(struct drm_atomic_state *state)
14384{
14385 struct drm_plane_state *old_plane_state;
14386 struct drm_plane *plane;
6c9c1b38
DV
14387 int i;
14388
faf5bf0a
CW
14389 for_each_plane_in_state(state, plane, old_plane_state, i)
14390 i915_gem_track_fb(intel_fb_obj(old_plane_state->fb),
14391 intel_fb_obj(plane->state->fb),
14392 to_intel_plane(plane)->frontbuffer_bit);
6c9c1b38
DV
14393}
14394
94f05024
DV
14395/**
14396 * intel_atomic_commit - commit validated state object
14397 * @dev: DRM device
14398 * @state: the top-level driver state object
14399 * @nonblock: nonblocking commit
14400 *
14401 * This function commits a top-level state object that has been validated
14402 * with drm_atomic_helper_check().
14403 *
14404 * FIXME: Atomic modeset support for i915 is not yet complete. At the moment
14405 * nonblocking commits are only safe for pure plane updates. Everything else
14406 * should work though.
14407 *
14408 * RETURNS
14409 * Zero for success or -errno.
14410 */
14411static int intel_atomic_commit(struct drm_device *dev,
14412 struct drm_atomic_state *state,
14413 bool nonblock)
14414{
14415 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
fac5e23e 14416 struct drm_i915_private *dev_priv = to_i915(dev);
94f05024
DV
14417 int ret = 0;
14418
14419 if (intel_state->modeset && nonblock) {
14420 DRM_DEBUG_KMS("nonblocking commit for modeset not yet implemented.\n");
14421 return -EINVAL;
14422 }
14423
14424 ret = drm_atomic_helper_setup_commit(state, nonblock);
14425 if (ret)
14426 return ret;
14427
14428 INIT_WORK(&state->commit_work, intel_atomic_commit_work);
14429
14430 ret = intel_atomic_prepare_commit(dev, state, nonblock);
14431 if (ret) {
14432 DRM_DEBUG_ATOMIC("Preparing state failed with %i\n", ret);
14433 return ret;
14434 }
14435
14436 drm_atomic_helper_swap_state(state, true);
14437 dev_priv->wm.distrust_bios_wm = false;
14438 dev_priv->wm.skl_results = intel_state->wm_results;
14439 intel_shared_dpll_commit(state);
6c9c1b38 14440 intel_atomic_track_fbs(state);
94f05024
DV
14441
14442 if (nonblock)
14443 queue_work(system_unbound_wq, &state->commit_work);
14444 else
14445 intel_atomic_commit_tail(state);
75714940 14446
74c090b1 14447 return 0;
7f27126e
JB
14448}
14449
c0c36b94
CW
14450void intel_crtc_restore_mode(struct drm_crtc *crtc)
14451{
83a57153
ACO
14452 struct drm_device *dev = crtc->dev;
14453 struct drm_atomic_state *state;
e694eb02 14454 struct drm_crtc_state *crtc_state;
2bfb4627 14455 int ret;
83a57153
ACO
14456
14457 state = drm_atomic_state_alloc(dev);
14458 if (!state) {
78108b7c
VS
14459 DRM_DEBUG_KMS("[CRTC:%d:%s] crtc restore failed, out of memory",
14460 crtc->base.id, crtc->name);
83a57153
ACO
14461 return;
14462 }
14463
e694eb02 14464 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
83a57153 14465
e694eb02
ML
14466retry:
14467 crtc_state = drm_atomic_get_crtc_state(state, crtc);
14468 ret = PTR_ERR_OR_ZERO(crtc_state);
14469 if (!ret) {
14470 if (!crtc_state->active)
14471 goto out;
83a57153 14472
e694eb02 14473 crtc_state->mode_changed = true;
74c090b1 14474 ret = drm_atomic_commit(state);
83a57153
ACO
14475 }
14476
e694eb02
ML
14477 if (ret == -EDEADLK) {
14478 drm_atomic_state_clear(state);
14479 drm_modeset_backoff(state->acquire_ctx);
14480 goto retry;
4ed9fb37 14481 }
4be07317 14482
2bfb4627 14483 if (ret)
e694eb02 14484out:
2bfb4627 14485 drm_atomic_state_free(state);
c0c36b94
CW
14486}
14487
25c5b266
DV
14488#undef for_each_intel_crtc_masked
14489
a8784875
BP
14490/*
14491 * FIXME: Remove this once i915 is fully DRIVER_ATOMIC by calling
14492 * drm_atomic_helper_legacy_gamma_set() directly.
14493 */
14494static int intel_atomic_legacy_gamma_set(struct drm_crtc *crtc,
14495 u16 *red, u16 *green, u16 *blue,
14496 uint32_t size)
14497{
14498 struct drm_device *dev = crtc->dev;
14499 struct drm_mode_config *config = &dev->mode_config;
14500 struct drm_crtc_state *state;
14501 int ret;
14502
14503 ret = drm_atomic_helper_legacy_gamma_set(crtc, red, green, blue, size);
14504 if (ret)
14505 return ret;
14506
14507 /*
14508 * Make sure we update the legacy properties so this works when
14509 * atomic is not enabled.
14510 */
14511
14512 state = crtc->state;
14513
14514 drm_object_property_set_value(&crtc->base,
14515 config->degamma_lut_property,
14516 (state->degamma_lut) ?
14517 state->degamma_lut->base.id : 0);
14518
14519 drm_object_property_set_value(&crtc->base,
14520 config->ctm_property,
14521 (state->ctm) ?
14522 state->ctm->base.id : 0);
14523
14524 drm_object_property_set_value(&crtc->base,
14525 config->gamma_lut_property,
14526 (state->gamma_lut) ?
14527 state->gamma_lut->base.id : 0);
14528
14529 return 0;
14530}
14531
f6e5b160 14532static const struct drm_crtc_funcs intel_crtc_funcs = {
a8784875 14533 .gamma_set = intel_atomic_legacy_gamma_set,
74c090b1 14534 .set_config = drm_atomic_helper_set_config,
82cf435b 14535 .set_property = drm_atomic_helper_crtc_set_property,
f6e5b160 14536 .destroy = intel_crtc_destroy,
527b6abe 14537 .page_flip = intel_crtc_page_flip,
1356837e
MR
14538 .atomic_duplicate_state = intel_crtc_duplicate_state,
14539 .atomic_destroy_state = intel_crtc_destroy_state,
f6e5b160
CW
14540};
14541
6beb8c23
MR
14542/**
14543 * intel_prepare_plane_fb - Prepare fb for usage on plane
14544 * @plane: drm plane to prepare for
14545 * @fb: framebuffer to prepare for presentation
14546 *
14547 * Prepares a framebuffer for usage on a display plane. Generally this
14548 * involves pinning the underlying object and updating the frontbuffer tracking
14549 * bits. Some older platforms need special physical address handling for
14550 * cursor planes.
14551 *
f935675f
ML
14552 * Must be called with struct_mutex held.
14553 *
6beb8c23
MR
14554 * Returns 0 on success, negative error code on failure.
14555 */
14556int
14557intel_prepare_plane_fb(struct drm_plane *plane,
d136dfee 14558 const struct drm_plane_state *new_state)
465c120c
MR
14559{
14560 struct drm_device *dev = plane->dev;
844f9111 14561 struct drm_framebuffer *fb = new_state->fb;
6beb8c23 14562 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
1ee49399 14563 struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->state->fb);
c37efb99 14564 struct reservation_object *resv;
6beb8c23 14565 int ret = 0;
465c120c 14566
1ee49399 14567 if (!obj && !old_obj)
465c120c
MR
14568 return 0;
14569
5008e874
ML
14570 if (old_obj) {
14571 struct drm_crtc_state *crtc_state =
14572 drm_atomic_get_existing_crtc_state(new_state->state, plane->state->crtc);
14573
14574 /* Big Hammer, we also need to ensure that any pending
14575 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
14576 * current scanout is retired before unpinning the old
14577 * framebuffer. Note that we rely on userspace rendering
14578 * into the buffer attached to the pipe they are waiting
14579 * on. If not, userspace generates a GPU hang with IPEHR
14580 * point to the MI_WAIT_FOR_EVENT.
14581 *
14582 * This should only fail upon a hung GPU, in which case we
14583 * can safely continue.
14584 */
14585 if (needs_modeset(crtc_state))
14586 ret = i915_gem_object_wait_rendering(old_obj, true);
f4457ae7
CW
14587 if (ret) {
14588 /* GPU hangs should have been swallowed by the wait */
14589 WARN_ON(ret == -EIO);
f935675f 14590 return ret;
f4457ae7 14591 }
5008e874
ML
14592 }
14593
c37efb99
CW
14594 if (!obj)
14595 return 0;
14596
5a21b665 14597 /* For framebuffer backed by dmabuf, wait for fence */
c37efb99
CW
14598 resv = i915_gem_object_get_dmabuf_resv(obj);
14599 if (resv) {
5a21b665
DV
14600 long lret;
14601
c37efb99 14602 lret = reservation_object_wait_timeout_rcu(resv, false, true,
5a21b665
DV
14603 MAX_SCHEDULE_TIMEOUT);
14604 if (lret == -ERESTARTSYS)
14605 return lret;
14606
14607 WARN(lret < 0, "waiting returns %li\n", lret);
14608 }
14609
c37efb99 14610 if (plane->type == DRM_PLANE_TYPE_CURSOR &&
6beb8c23
MR
14611 INTEL_INFO(dev)->cursor_needs_physical) {
14612 int align = IS_I830(dev) ? 16 * 1024 : 256;
14613 ret = i915_gem_object_attach_phys(obj, align);
14614 if (ret)
14615 DRM_DEBUG_KMS("failed to attach phys object\n");
14616 } else {
058d88c4
CW
14617 struct i915_vma *vma;
14618
14619 vma = intel_pin_and_fence_fb_obj(fb, new_state->rotation);
14620 if (IS_ERR(vma))
14621 ret = PTR_ERR(vma);
6beb8c23 14622 }
465c120c 14623
c37efb99 14624 if (ret == 0) {
27c01aae 14625 to_intel_plane_state(new_state)->wait_req =
d72d908b
CW
14626 i915_gem_active_get(&obj->last_write,
14627 &obj->base.dev->struct_mutex);
7580d774 14628 }
fdd508a6 14629
6beb8c23
MR
14630 return ret;
14631}
14632
38f3ce3a
MR
14633/**
14634 * intel_cleanup_plane_fb - Cleans up an fb after plane use
14635 * @plane: drm plane to clean up for
14636 * @fb: old framebuffer that was on plane
14637 *
14638 * Cleans up a framebuffer that has just been removed from a plane.
f935675f
ML
14639 *
14640 * Must be called with struct_mutex held.
38f3ce3a
MR
14641 */
14642void
14643intel_cleanup_plane_fb(struct drm_plane *plane,
d136dfee 14644 const struct drm_plane_state *old_state)
38f3ce3a
MR
14645{
14646 struct drm_device *dev = plane->dev;
7580d774 14647 struct intel_plane_state *old_intel_state;
84978257 14648 struct intel_plane_state *intel_state = to_intel_plane_state(plane->state);
1ee49399
ML
14649 struct drm_i915_gem_object *old_obj = intel_fb_obj(old_state->fb);
14650 struct drm_i915_gem_object *obj = intel_fb_obj(plane->state->fb);
38f3ce3a 14651
7580d774
ML
14652 old_intel_state = to_intel_plane_state(old_state);
14653
1ee49399 14654 if (!obj && !old_obj)
38f3ce3a
MR
14655 return;
14656
1ee49399
ML
14657 if (old_obj && (plane->type != DRM_PLANE_TYPE_CURSOR ||
14658 !INTEL_INFO(dev)->cursor_needs_physical))
3465c580 14659 intel_unpin_fb_obj(old_state->fb, old_state->rotation);
1ee49399 14660
84978257 14661 i915_gem_request_assign(&intel_state->wait_req, NULL);
7580d774 14662 i915_gem_request_assign(&old_intel_state->wait_req, NULL);
465c120c
MR
14663}
14664
6156a456
CK
14665int
14666skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
14667{
14668 int max_scale;
6156a456
CK
14669 int crtc_clock, cdclk;
14670
bf8a0af0 14671 if (!intel_crtc || !crtc_state->base.enable)
6156a456
CK
14672 return DRM_PLANE_HELPER_NO_SCALING;
14673
6156a456 14674 crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
27c329ed 14675 cdclk = to_intel_atomic_state(crtc_state->base.state)->cdclk;
6156a456 14676
54bf1ce6 14677 if (WARN_ON_ONCE(!crtc_clock || cdclk < crtc_clock))
6156a456
CK
14678 return DRM_PLANE_HELPER_NO_SCALING;
14679
14680 /*
14681 * skl max scale is lower of:
14682 * close to 3 but not 3, -1 is for that purpose
14683 * or
14684 * cdclk/crtc_clock
14685 */
14686 max_scale = min((1 << 16) * 3 - 1, (1 << 8) * ((cdclk << 8) / crtc_clock));
14687
14688 return max_scale;
14689}
14690
465c120c 14691static int
3c692a41 14692intel_check_primary_plane(struct drm_plane *plane,
061e4b8d 14693 struct intel_crtc_state *crtc_state,
3c692a41
GP
14694 struct intel_plane_state *state)
14695{
b63a16f6 14696 struct drm_i915_private *dev_priv = to_i915(plane->dev);
2b875c22 14697 struct drm_crtc *crtc = state->base.crtc;
6156a456 14698 int min_scale = DRM_PLANE_HELPER_NO_SCALING;
061e4b8d
ML
14699 int max_scale = DRM_PLANE_HELPER_NO_SCALING;
14700 bool can_position = false;
b63a16f6 14701 int ret;
465c120c 14702
b63a16f6 14703 if (INTEL_GEN(dev_priv) >= 9) {
693bdc28
VS
14704 /* use scaler when colorkey is not required */
14705 if (state->ckey.flags == I915_SET_COLORKEY_NONE) {
14706 min_scale = 1;
14707 max_scale = skl_max_scale(to_intel_crtc(crtc), crtc_state);
14708 }
d8106366 14709 can_position = true;
6156a456 14710 }
d8106366 14711
cc926387
DV
14712 ret = drm_plane_helper_check_state(&state->base,
14713 &state->clip,
14714 min_scale, max_scale,
14715 can_position, true);
b63a16f6
VS
14716 if (ret)
14717 return ret;
14718
cc926387 14719 if (!state->base.fb)
b63a16f6
VS
14720 return 0;
14721
14722 if (INTEL_GEN(dev_priv) >= 9) {
14723 ret = skl_check_plane_surface(state);
14724 if (ret)
14725 return ret;
14726 }
14727
14728 return 0;
14af293f
GP
14729}
14730
5a21b665
DV
14731static void intel_begin_crtc_commit(struct drm_crtc *crtc,
14732 struct drm_crtc_state *old_crtc_state)
14733{
14734 struct drm_device *dev = crtc->dev;
62e0fb88 14735 struct drm_i915_private *dev_priv = to_i915(dev);
5a21b665
DV
14736 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
14737 struct intel_crtc_state *old_intel_state =
14738 to_intel_crtc_state(old_crtc_state);
14739 bool modeset = needs_modeset(crtc->state);
62e0fb88 14740 enum pipe pipe = intel_crtc->pipe;
5a21b665
DV
14741
14742 /* Perform vblank evasion around commit operation */
14743 intel_pipe_update_start(intel_crtc);
14744
14745 if (modeset)
14746 return;
14747
14748 if (crtc->state->color_mgmt_changed || to_intel_crtc_state(crtc->state)->update_pipe) {
14749 intel_color_set_csc(crtc->state);
14750 intel_color_load_luts(crtc->state);
14751 }
14752
14753 if (to_intel_crtc_state(crtc->state)->update_pipe)
14754 intel_update_pipe_config(intel_crtc, old_intel_state);
62e0fb88 14755 else if (INTEL_GEN(dev_priv) >= 9) {
5a21b665 14756 skl_detach_scalers(intel_crtc);
62e0fb88
L
14757
14758 I915_WRITE(PIPE_WM_LINETIME(pipe),
14759 dev_priv->wm.skl_hw.wm_linetime[pipe]);
14760 }
5a21b665
DV
14761}
14762
14763static void intel_finish_crtc_commit(struct drm_crtc *crtc,
14764 struct drm_crtc_state *old_crtc_state)
14765{
14766 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
14767
14768 intel_pipe_update_end(intel_crtc, NULL);
14769}
14770
cf4c7c12 14771/**
4a3b8769
MR
14772 * intel_plane_destroy - destroy a plane
14773 * @plane: plane to destroy
cf4c7c12 14774 *
4a3b8769
MR
14775 * Common destruction function for all types of planes (primary, cursor,
14776 * sprite).
cf4c7c12 14777 */
4a3b8769 14778void intel_plane_destroy(struct drm_plane *plane)
465c120c 14779{
69ae561f
VS
14780 if (!plane)
14781 return;
14782
465c120c 14783 drm_plane_cleanup(plane);
69ae561f 14784 kfree(to_intel_plane(plane));
465c120c
MR
14785}
14786
65a3fea0 14787const struct drm_plane_funcs intel_plane_funcs = {
70a101f8
MR
14788 .update_plane = drm_atomic_helper_update_plane,
14789 .disable_plane = drm_atomic_helper_disable_plane,
3d7d6510 14790 .destroy = intel_plane_destroy,
c196e1d6 14791 .set_property = drm_atomic_helper_plane_set_property,
a98b3431
MR
14792 .atomic_get_property = intel_plane_atomic_get_property,
14793 .atomic_set_property = intel_plane_atomic_set_property,
ea2c67bb
MR
14794 .atomic_duplicate_state = intel_plane_duplicate_state,
14795 .atomic_destroy_state = intel_plane_destroy_state,
14796
465c120c
MR
14797};
14798
14799static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
14800 int pipe)
14801{
fca0ce2a
VS
14802 struct intel_plane *primary = NULL;
14803 struct intel_plane_state *state = NULL;
465c120c 14804 const uint32_t *intel_primary_formats;
45e3743a 14805 unsigned int num_formats;
fca0ce2a 14806 int ret;
465c120c
MR
14807
14808 primary = kzalloc(sizeof(*primary), GFP_KERNEL);
fca0ce2a
VS
14809 if (!primary)
14810 goto fail;
465c120c 14811
8e7d688b 14812 state = intel_create_plane_state(&primary->base);
fca0ce2a
VS
14813 if (!state)
14814 goto fail;
8e7d688b 14815 primary->base.state = &state->base;
ea2c67bb 14816
465c120c
MR
14817 primary->can_scale = false;
14818 primary->max_downscale = 1;
6156a456
CK
14819 if (INTEL_INFO(dev)->gen >= 9) {
14820 primary->can_scale = true;
af99ceda 14821 state->scaler_id = -1;
6156a456 14822 }
465c120c
MR
14823 primary->pipe = pipe;
14824 primary->plane = pipe;
a9ff8714 14825 primary->frontbuffer_bit = INTEL_FRONTBUFFER_PRIMARY(pipe);
c59cb179 14826 primary->check_plane = intel_check_primary_plane;
465c120c
MR
14827 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
14828 primary->plane = !pipe;
14829
6c0fd451
DL
14830 if (INTEL_INFO(dev)->gen >= 9) {
14831 intel_primary_formats = skl_primary_formats;
14832 num_formats = ARRAY_SIZE(skl_primary_formats);
a8d201af
ML
14833
14834 primary->update_plane = skylake_update_primary_plane;
14835 primary->disable_plane = skylake_disable_primary_plane;
14836 } else if (HAS_PCH_SPLIT(dev)) {
14837 intel_primary_formats = i965_primary_formats;
14838 num_formats = ARRAY_SIZE(i965_primary_formats);
14839
14840 primary->update_plane = ironlake_update_primary_plane;
14841 primary->disable_plane = i9xx_disable_primary_plane;
6c0fd451 14842 } else if (INTEL_INFO(dev)->gen >= 4) {
568db4f2
DL
14843 intel_primary_formats = i965_primary_formats;
14844 num_formats = ARRAY_SIZE(i965_primary_formats);
a8d201af
ML
14845
14846 primary->update_plane = i9xx_update_primary_plane;
14847 primary->disable_plane = i9xx_disable_primary_plane;
6c0fd451
DL
14848 } else {
14849 intel_primary_formats = i8xx_primary_formats;
14850 num_formats = ARRAY_SIZE(i8xx_primary_formats);
a8d201af
ML
14851
14852 primary->update_plane = i9xx_update_primary_plane;
14853 primary->disable_plane = i9xx_disable_primary_plane;
465c120c
MR
14854 }
14855
38573dc1
VS
14856 if (INTEL_INFO(dev)->gen >= 9)
14857 ret = drm_universal_plane_init(dev, &primary->base, 0,
14858 &intel_plane_funcs,
14859 intel_primary_formats, num_formats,
14860 DRM_PLANE_TYPE_PRIMARY,
14861 "plane 1%c", pipe_name(pipe));
14862 else if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
14863 ret = drm_universal_plane_init(dev, &primary->base, 0,
14864 &intel_plane_funcs,
14865 intel_primary_formats, num_formats,
14866 DRM_PLANE_TYPE_PRIMARY,
14867 "primary %c", pipe_name(pipe));
14868 else
14869 ret = drm_universal_plane_init(dev, &primary->base, 0,
14870 &intel_plane_funcs,
14871 intel_primary_formats, num_formats,
14872 DRM_PLANE_TYPE_PRIMARY,
14873 "plane %c", plane_name(primary->plane));
fca0ce2a
VS
14874 if (ret)
14875 goto fail;
48404c1e 14876
3b7a5119
SJ
14877 if (INTEL_INFO(dev)->gen >= 4)
14878 intel_create_rotation_property(dev, primary);
48404c1e 14879
ea2c67bb
MR
14880 drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
14881
465c120c 14882 return &primary->base;
fca0ce2a
VS
14883
14884fail:
14885 kfree(state);
14886 kfree(primary);
14887
14888 return NULL;
465c120c
MR
14889}
14890
3b7a5119
SJ
14891void intel_create_rotation_property(struct drm_device *dev, struct intel_plane *plane)
14892{
14893 if (!dev->mode_config.rotation_property) {
31ad61e4
JL
14894 unsigned long flags = DRM_ROTATE_0 |
14895 DRM_ROTATE_180;
3b7a5119
SJ
14896
14897 if (INTEL_INFO(dev)->gen >= 9)
31ad61e4 14898 flags |= DRM_ROTATE_90 | DRM_ROTATE_270;
3b7a5119
SJ
14899
14900 dev->mode_config.rotation_property =
14901 drm_mode_create_rotation_property(dev, flags);
14902 }
14903 if (dev->mode_config.rotation_property)
14904 drm_object_attach_property(&plane->base.base,
14905 dev->mode_config.rotation_property,
14906 plane->base.state->rotation);
14907}
14908
3d7d6510 14909static int
852e787c 14910intel_check_cursor_plane(struct drm_plane *plane,
061e4b8d 14911 struct intel_crtc_state *crtc_state,
852e787c 14912 struct intel_plane_state *state)
3d7d6510 14913{
2b875c22 14914 struct drm_framebuffer *fb = state->base.fb;
757f9a3e 14915 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
b29ec92c 14916 enum pipe pipe = to_intel_plane(plane)->pipe;
757f9a3e
GP
14917 unsigned stride;
14918 int ret;
3d7d6510 14919
f8856a44
VS
14920 ret = drm_plane_helper_check_state(&state->base,
14921 &state->clip,
14922 DRM_PLANE_HELPER_NO_SCALING,
14923 DRM_PLANE_HELPER_NO_SCALING,
14924 true, true);
757f9a3e
GP
14925 if (ret)
14926 return ret;
14927
757f9a3e
GP
14928 /* if we want to turn off the cursor ignore width and height */
14929 if (!obj)
da20eabd 14930 return 0;
757f9a3e 14931
757f9a3e 14932 /* Check for which cursor types we support */
061e4b8d 14933 if (!cursor_size_ok(plane->dev, state->base.crtc_w, state->base.crtc_h)) {
ea2c67bb
MR
14934 DRM_DEBUG("Cursor dimension %dx%d not supported\n",
14935 state->base.crtc_w, state->base.crtc_h);
757f9a3e
GP
14936 return -EINVAL;
14937 }
14938
ea2c67bb
MR
14939 stride = roundup_pow_of_two(state->base.crtc_w) * 4;
14940 if (obj->base.size < stride * state->base.crtc_h) {
757f9a3e
GP
14941 DRM_DEBUG_KMS("buffer is too small\n");
14942 return -ENOMEM;
14943 }
14944
3a656b54 14945 if (fb->modifier[0] != DRM_FORMAT_MOD_NONE) {
757f9a3e 14946 DRM_DEBUG_KMS("cursor cannot be tiled\n");
da20eabd 14947 return -EINVAL;
32b7eeec
MR
14948 }
14949
b29ec92c
VS
14950 /*
14951 * There's something wrong with the cursor on CHV pipe C.
14952 * If it straddles the left edge of the screen then
14953 * moving it away from the edge or disabling it often
14954 * results in a pipe underrun, and often that can lead to
14955 * dead pipe (constant underrun reported, and it scans
14956 * out just a solid color). To recover from that, the
14957 * display power well must be turned off and on again.
14958 * Refuse the put the cursor into that compromised position.
14959 */
14960 if (IS_CHERRYVIEW(plane->dev) && pipe == PIPE_C &&
936e71e3 14961 state->base.visible && state->base.crtc_x < 0) {
b29ec92c
VS
14962 DRM_DEBUG_KMS("CHV cursor C not allowed to straddle the left screen edge\n");
14963 return -EINVAL;
14964 }
14965
da20eabd 14966 return 0;
852e787c 14967}
3d7d6510 14968
a8ad0d8e
ML
14969static void
14970intel_disable_cursor_plane(struct drm_plane *plane,
7fabf5ef 14971 struct drm_crtc *crtc)
a8ad0d8e 14972{
f2858021
ML
14973 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
14974
14975 intel_crtc->cursor_addr = 0;
55a08b3f 14976 intel_crtc_update_cursor(crtc, NULL);
a8ad0d8e
ML
14977}
14978
f4a2cf29 14979static void
55a08b3f
ML
14980intel_update_cursor_plane(struct drm_plane *plane,
14981 const struct intel_crtc_state *crtc_state,
14982 const struct intel_plane_state *state)
852e787c 14983{
55a08b3f
ML
14984 struct drm_crtc *crtc = crtc_state->base.crtc;
14985 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ea2c67bb 14986 struct drm_device *dev = plane->dev;
2b875c22 14987 struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
a912f12f 14988 uint32_t addr;
852e787c 14989
f4a2cf29 14990 if (!obj)
a912f12f 14991 addr = 0;
f4a2cf29 14992 else if (!INTEL_INFO(dev)->cursor_needs_physical)
058d88c4 14993 addr = i915_gem_object_ggtt_offset(obj, NULL);
f4a2cf29 14994 else
a912f12f 14995 addr = obj->phys_handle->busaddr;
852e787c 14996
a912f12f 14997 intel_crtc->cursor_addr = addr;
55a08b3f 14998 intel_crtc_update_cursor(crtc, state);
852e787c
GP
14999}
15000
3d7d6510
MR
15001static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
15002 int pipe)
15003{
fca0ce2a
VS
15004 struct intel_plane *cursor = NULL;
15005 struct intel_plane_state *state = NULL;
15006 int ret;
3d7d6510
MR
15007
15008 cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
fca0ce2a
VS
15009 if (!cursor)
15010 goto fail;
3d7d6510 15011
8e7d688b 15012 state = intel_create_plane_state(&cursor->base);
fca0ce2a
VS
15013 if (!state)
15014 goto fail;
8e7d688b 15015 cursor->base.state = &state->base;
ea2c67bb 15016
3d7d6510
MR
15017 cursor->can_scale = false;
15018 cursor->max_downscale = 1;
15019 cursor->pipe = pipe;
15020 cursor->plane = pipe;
a9ff8714 15021 cursor->frontbuffer_bit = INTEL_FRONTBUFFER_CURSOR(pipe);
c59cb179 15022 cursor->check_plane = intel_check_cursor_plane;
55a08b3f 15023 cursor->update_plane = intel_update_cursor_plane;
a8ad0d8e 15024 cursor->disable_plane = intel_disable_cursor_plane;
3d7d6510 15025
fca0ce2a
VS
15026 ret = drm_universal_plane_init(dev, &cursor->base, 0,
15027 &intel_plane_funcs,
15028 intel_cursor_formats,
15029 ARRAY_SIZE(intel_cursor_formats),
38573dc1
VS
15030 DRM_PLANE_TYPE_CURSOR,
15031 "cursor %c", pipe_name(pipe));
fca0ce2a
VS
15032 if (ret)
15033 goto fail;
4398ad45
VS
15034
15035 if (INTEL_INFO(dev)->gen >= 4) {
15036 if (!dev->mode_config.rotation_property)
15037 dev->mode_config.rotation_property =
15038 drm_mode_create_rotation_property(dev,
31ad61e4
JL
15039 DRM_ROTATE_0 |
15040 DRM_ROTATE_180);
4398ad45
VS
15041 if (dev->mode_config.rotation_property)
15042 drm_object_attach_property(&cursor->base.base,
15043 dev->mode_config.rotation_property,
8e7d688b 15044 state->base.rotation);
4398ad45
VS
15045 }
15046
af99ceda
CK
15047 if (INTEL_INFO(dev)->gen >=9)
15048 state->scaler_id = -1;
15049
ea2c67bb
MR
15050 drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
15051
3d7d6510 15052 return &cursor->base;
fca0ce2a
VS
15053
15054fail:
15055 kfree(state);
15056 kfree(cursor);
15057
15058 return NULL;
3d7d6510
MR
15059}
15060
549e2bfb
CK
15061static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
15062 struct intel_crtc_state *crtc_state)
15063{
15064 int i;
15065 struct intel_scaler *intel_scaler;
15066 struct intel_crtc_scaler_state *scaler_state = &crtc_state->scaler_state;
15067
15068 for (i = 0; i < intel_crtc->num_scalers; i++) {
15069 intel_scaler = &scaler_state->scalers[i];
15070 intel_scaler->in_use = 0;
549e2bfb
CK
15071 intel_scaler->mode = PS_SCALER_MODE_DYN;
15072 }
15073
15074 scaler_state->scaler_id = -1;
15075}
15076
b358d0a6 15077static void intel_crtc_init(struct drm_device *dev, int pipe)
79e53945 15078{
fac5e23e 15079 struct drm_i915_private *dev_priv = to_i915(dev);
79e53945 15080 struct intel_crtc *intel_crtc;
f5de6e07 15081 struct intel_crtc_state *crtc_state = NULL;
3d7d6510
MR
15082 struct drm_plane *primary = NULL;
15083 struct drm_plane *cursor = NULL;
8563b1e8 15084 int ret;
79e53945 15085
955382f3 15086 intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
79e53945
JB
15087 if (intel_crtc == NULL)
15088 return;
15089
f5de6e07
ACO
15090 crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
15091 if (!crtc_state)
15092 goto fail;
550acefd
ACO
15093 intel_crtc->config = crtc_state;
15094 intel_crtc->base.state = &crtc_state->base;
07878248 15095 crtc_state->base.crtc = &intel_crtc->base;
f5de6e07 15096
549e2bfb
CK
15097 /* initialize shared scalers */
15098 if (INTEL_INFO(dev)->gen >= 9) {
15099 if (pipe == PIPE_C)
15100 intel_crtc->num_scalers = 1;
15101 else
15102 intel_crtc->num_scalers = SKL_NUM_SCALERS;
15103
15104 skl_init_scalers(dev, intel_crtc, crtc_state);
15105 }
15106
465c120c 15107 primary = intel_primary_plane_create(dev, pipe);
3d7d6510
MR
15108 if (!primary)
15109 goto fail;
15110
15111 cursor = intel_cursor_plane_create(dev, pipe);
15112 if (!cursor)
15113 goto fail;
15114
465c120c 15115 ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
4d5d72b7
VS
15116 cursor, &intel_crtc_funcs,
15117 "pipe %c", pipe_name(pipe));
3d7d6510
MR
15118 if (ret)
15119 goto fail;
79e53945 15120
1f1c2e24
VS
15121 /*
15122 * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
8c0f92e1 15123 * is hooked to pipe B. Hence we want plane A feeding pipe B.
1f1c2e24 15124 */
80824003
JB
15125 intel_crtc->pipe = pipe;
15126 intel_crtc->plane = pipe;
3a77c4c4 15127 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
28c97730 15128 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
e2e767ab 15129 intel_crtc->plane = !pipe;
80824003
JB
15130 }
15131
4b0e333e
CW
15132 intel_crtc->cursor_base = ~0;
15133 intel_crtc->cursor_cntl = ~0;
dc41c154 15134 intel_crtc->cursor_size = ~0;
8d7849db 15135
852eb00d
VS
15136 intel_crtc->wm.cxsr_allowed = true;
15137
22fd0fab
JB
15138 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
15139 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
15140 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
15141 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
15142
79e53945 15143 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
87b6b101 15144
8563b1e8
LL
15145 intel_color_init(&intel_crtc->base);
15146
87b6b101 15147 WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
3d7d6510
MR
15148 return;
15149
15150fail:
69ae561f
VS
15151 intel_plane_destroy(primary);
15152 intel_plane_destroy(cursor);
f5de6e07 15153 kfree(crtc_state);
3d7d6510 15154 kfree(intel_crtc);
79e53945
JB
15155}
15156
752aa88a
JB
15157enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
15158{
15159 struct drm_encoder *encoder = connector->base.encoder;
6e9f798d 15160 struct drm_device *dev = connector->base.dev;
752aa88a 15161
51fd371b 15162 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
752aa88a 15163
d3babd3f 15164 if (!encoder || WARN_ON(!encoder->crtc))
752aa88a
JB
15165 return INVALID_PIPE;
15166
15167 return to_intel_crtc(encoder->crtc)->pipe;
15168}
15169
08d7b3d1 15170int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
05394f39 15171 struct drm_file *file)
08d7b3d1 15172{
08d7b3d1 15173 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
7707e653 15174 struct drm_crtc *drmmode_crtc;
c05422d5 15175 struct intel_crtc *crtc;
08d7b3d1 15176
7707e653 15177 drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
71240ed2 15178 if (!drmmode_crtc)
3f2c2057 15179 return -ENOENT;
08d7b3d1 15180
7707e653 15181 crtc = to_intel_crtc(drmmode_crtc);
c05422d5 15182 pipe_from_crtc_id->pipe = crtc->pipe;
08d7b3d1 15183
c05422d5 15184 return 0;
08d7b3d1
CW
15185}
15186
66a9278e 15187static int intel_encoder_clones(struct intel_encoder *encoder)
79e53945 15188{
66a9278e
DV
15189 struct drm_device *dev = encoder->base.dev;
15190 struct intel_encoder *source_encoder;
79e53945 15191 int index_mask = 0;
79e53945
JB
15192 int entry = 0;
15193
b2784e15 15194 for_each_intel_encoder(dev, source_encoder) {
bc079e8b 15195 if (encoders_cloneable(encoder, source_encoder))
66a9278e
DV
15196 index_mask |= (1 << entry);
15197
79e53945
JB
15198 entry++;
15199 }
4ef69c7a 15200
79e53945
JB
15201 return index_mask;
15202}
15203
4d302442
CW
15204static bool has_edp_a(struct drm_device *dev)
15205{
fac5e23e 15206 struct drm_i915_private *dev_priv = to_i915(dev);
4d302442
CW
15207
15208 if (!IS_MOBILE(dev))
15209 return false;
15210
15211 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
15212 return false;
15213
e3589908 15214 if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
4d302442
CW
15215 return false;
15216
15217 return true;
15218}
15219
84b4e042
JB
15220static bool intel_crt_present(struct drm_device *dev)
15221{
fac5e23e 15222 struct drm_i915_private *dev_priv = to_i915(dev);
84b4e042 15223
884497ed
DL
15224 if (INTEL_INFO(dev)->gen >= 9)
15225 return false;
15226
cf404ce4 15227 if (IS_HSW_ULT(dev) || IS_BDW_ULT(dev))
84b4e042
JB
15228 return false;
15229
15230 if (IS_CHERRYVIEW(dev))
15231 return false;
15232
65e472e4
VS
15233 if (HAS_PCH_LPT_H(dev) && I915_READ(SFUSE_STRAP) & SFUSE_STRAP_CRT_DISABLED)
15234 return false;
15235
70ac54d0
VS
15236 /* DDI E can't be used if DDI A requires 4 lanes */
15237 if (HAS_DDI(dev) && I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_A_4_LANES)
15238 return false;
15239
e4abb733 15240 if (!dev_priv->vbt.int_crt_support)
84b4e042
JB
15241 return false;
15242
15243 return true;
15244}
15245
8090ba8c
ID
15246void intel_pps_unlock_regs_wa(struct drm_i915_private *dev_priv)
15247{
15248 int pps_num;
15249 int pps_idx;
15250
15251 if (HAS_DDI(dev_priv))
15252 return;
15253 /*
15254 * This w/a is needed at least on CPT/PPT, but to be sure apply it
15255 * everywhere where registers can be write protected.
15256 */
15257 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
15258 pps_num = 2;
15259 else
15260 pps_num = 1;
15261
15262 for (pps_idx = 0; pps_idx < pps_num; pps_idx++) {
15263 u32 val = I915_READ(PP_CONTROL(pps_idx));
15264
15265 val = (val & ~PANEL_UNLOCK_MASK) | PANEL_UNLOCK_REGS;
15266 I915_WRITE(PP_CONTROL(pps_idx), val);
15267 }
15268}
15269
44cb734c
ID
15270static void intel_pps_init(struct drm_i915_private *dev_priv)
15271{
15272 if (HAS_PCH_SPLIT(dev_priv) || IS_BROXTON(dev_priv))
15273 dev_priv->pps_mmio_base = PCH_PPS_BASE;
15274 else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
15275 dev_priv->pps_mmio_base = VLV_PPS_BASE;
15276 else
15277 dev_priv->pps_mmio_base = PPS_BASE;
8090ba8c
ID
15278
15279 intel_pps_unlock_regs_wa(dev_priv);
44cb734c
ID
15280}
15281
79e53945
JB
15282static void intel_setup_outputs(struct drm_device *dev)
15283{
fac5e23e 15284 struct drm_i915_private *dev_priv = to_i915(dev);
4ef69c7a 15285 struct intel_encoder *encoder;
cb0953d7 15286 bool dpd_is_edp = false;
79e53945 15287
44cb734c
ID
15288 intel_pps_init(dev_priv);
15289
97a824e1
ID
15290 /*
15291 * intel_edp_init_connector() depends on this completing first, to
15292 * prevent the registeration of both eDP and LVDS and the incorrect
15293 * sharing of the PPS.
15294 */
c9093354 15295 intel_lvds_init(dev);
79e53945 15296
84b4e042 15297 if (intel_crt_present(dev))
79935fca 15298 intel_crt_init(dev);
cb0953d7 15299
c776eb2e
VK
15300 if (IS_BROXTON(dev)) {
15301 /*
15302 * FIXME: Broxton doesn't support port detection via the
15303 * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
15304 * detect the ports.
15305 */
15306 intel_ddi_init(dev, PORT_A);
15307 intel_ddi_init(dev, PORT_B);
15308 intel_ddi_init(dev, PORT_C);
c6c794a2
SS
15309
15310 intel_dsi_init(dev);
c776eb2e 15311 } else if (HAS_DDI(dev)) {
0e72a5b5
ED
15312 int found;
15313
de31facd
JB
15314 /*
15315 * Haswell uses DDI functions to detect digital outputs.
15316 * On SKL pre-D0 the strap isn't connected, so we assume
15317 * it's there.
15318 */
77179400 15319 found = I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_INIT_DISPLAY_DETECTED;
de31facd 15320 /* WaIgnoreDDIAStrap: skl */
ef11bdb3 15321 if (found || IS_SKYLAKE(dev) || IS_KABYLAKE(dev))
0e72a5b5
ED
15322 intel_ddi_init(dev, PORT_A);
15323
15324 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
15325 * register */
15326 found = I915_READ(SFUSE_STRAP);
15327
15328 if (found & SFUSE_STRAP_DDIB_DETECTED)
15329 intel_ddi_init(dev, PORT_B);
15330 if (found & SFUSE_STRAP_DDIC_DETECTED)
15331 intel_ddi_init(dev, PORT_C);
15332 if (found & SFUSE_STRAP_DDID_DETECTED)
15333 intel_ddi_init(dev, PORT_D);
2800e4c2
RV
15334 /*
15335 * On SKL we don't have a way to detect DDI-E so we rely on VBT.
15336 */
ef11bdb3 15337 if ((IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) &&
2800e4c2
RV
15338 (dev_priv->vbt.ddi_port_info[PORT_E].supports_dp ||
15339 dev_priv->vbt.ddi_port_info[PORT_E].supports_dvi ||
15340 dev_priv->vbt.ddi_port_info[PORT_E].supports_hdmi))
15341 intel_ddi_init(dev, PORT_E);
15342
0e72a5b5 15343 } else if (HAS_PCH_SPLIT(dev)) {
cb0953d7 15344 int found;
5d8a7752 15345 dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
270b3042
DV
15346
15347 if (has_edp_a(dev))
15348 intel_dp_init(dev, DP_A, PORT_A);
cb0953d7 15349
dc0fa718 15350 if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
461ed3ca 15351 /* PCH SDVOB multiplex with HDMIB */
2a5c0832 15352 found = intel_sdvo_init(dev, PCH_SDVOB, PORT_B);
30ad48b7 15353 if (!found)
e2debe91 15354 intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
5eb08b69 15355 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
ab9d7c30 15356 intel_dp_init(dev, PCH_DP_B, PORT_B);
30ad48b7
ZW
15357 }
15358
dc0fa718 15359 if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
e2debe91 15360 intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
30ad48b7 15361
dc0fa718 15362 if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
e2debe91 15363 intel_hdmi_init(dev, PCH_HDMID, PORT_D);
30ad48b7 15364
5eb08b69 15365 if (I915_READ(PCH_DP_C) & DP_DETECTED)
ab9d7c30 15366 intel_dp_init(dev, PCH_DP_C, PORT_C);
5eb08b69 15367
270b3042 15368 if (I915_READ(PCH_DP_D) & DP_DETECTED)
ab9d7c30 15369 intel_dp_init(dev, PCH_DP_D, PORT_D);
666a4537 15370 } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
22f35042 15371 bool has_edp, has_port;
457c52d8 15372
e17ac6db
VS
15373 /*
15374 * The DP_DETECTED bit is the latched state of the DDC
15375 * SDA pin at boot. However since eDP doesn't require DDC
15376 * (no way to plug in a DP->HDMI dongle) the DDC pins for
15377 * eDP ports may have been muxed to an alternate function.
15378 * Thus we can't rely on the DP_DETECTED bit alone to detect
15379 * eDP ports. Consult the VBT as well as DP_DETECTED to
15380 * detect eDP ports.
22f35042
VS
15381 *
15382 * Sadly the straps seem to be missing sometimes even for HDMI
15383 * ports (eg. on Voyo V3 - CHT x7-Z8700), so check both strap
15384 * and VBT for the presence of the port. Additionally we can't
15385 * trust the port type the VBT declares as we've seen at least
15386 * HDMI ports that the VBT claim are DP or eDP.
e17ac6db 15387 */
457c52d8 15388 has_edp = intel_dp_is_edp(dev, PORT_B);
22f35042
VS
15389 has_port = intel_bios_is_port_present(dev_priv, PORT_B);
15390 if (I915_READ(VLV_DP_B) & DP_DETECTED || has_port)
457c52d8 15391 has_edp &= intel_dp_init(dev, VLV_DP_B, PORT_B);
22f35042 15392 if ((I915_READ(VLV_HDMIB) & SDVO_DETECTED || has_port) && !has_edp)
e66eb81d 15393 intel_hdmi_init(dev, VLV_HDMIB, PORT_B);
585a94b8 15394
457c52d8 15395 has_edp = intel_dp_is_edp(dev, PORT_C);
22f35042
VS
15396 has_port = intel_bios_is_port_present(dev_priv, PORT_C);
15397 if (I915_READ(VLV_DP_C) & DP_DETECTED || has_port)
457c52d8 15398 has_edp &= intel_dp_init(dev, VLV_DP_C, PORT_C);
22f35042 15399 if ((I915_READ(VLV_HDMIC) & SDVO_DETECTED || has_port) && !has_edp)
e66eb81d 15400 intel_hdmi_init(dev, VLV_HDMIC, PORT_C);
19c03924 15401
9418c1f1 15402 if (IS_CHERRYVIEW(dev)) {
22f35042
VS
15403 /*
15404 * eDP not supported on port D,
15405 * so no need to worry about it
15406 */
15407 has_port = intel_bios_is_port_present(dev_priv, PORT_D);
15408 if (I915_READ(CHV_DP_D) & DP_DETECTED || has_port)
e66eb81d 15409 intel_dp_init(dev, CHV_DP_D, PORT_D);
22f35042
VS
15410 if (I915_READ(CHV_HDMID) & SDVO_DETECTED || has_port)
15411 intel_hdmi_init(dev, CHV_HDMID, PORT_D);
9418c1f1
VS
15412 }
15413
3cfca973 15414 intel_dsi_init(dev);
09da55dc 15415 } else if (!IS_GEN2(dev) && !IS_PINEVIEW(dev)) {
27185ae1 15416 bool found = false;
7d57382e 15417
e2debe91 15418 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
b01f2c3a 15419 DRM_DEBUG_KMS("probing SDVOB\n");
2a5c0832 15420 found = intel_sdvo_init(dev, GEN3_SDVOB, PORT_B);
3fec3d2f 15421 if (!found && IS_G4X(dev)) {
b01f2c3a 15422 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
e2debe91 15423 intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
b01f2c3a 15424 }
27185ae1 15425
3fec3d2f 15426 if (!found && IS_G4X(dev))
ab9d7c30 15427 intel_dp_init(dev, DP_B, PORT_B);
725e30ad 15428 }
13520b05
KH
15429
15430 /* Before G4X SDVOC doesn't have its own detect register */
13520b05 15431
e2debe91 15432 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
b01f2c3a 15433 DRM_DEBUG_KMS("probing SDVOC\n");
2a5c0832 15434 found = intel_sdvo_init(dev, GEN3_SDVOC, PORT_C);
b01f2c3a 15435 }
27185ae1 15436
e2debe91 15437 if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
27185ae1 15438
3fec3d2f 15439 if (IS_G4X(dev)) {
b01f2c3a 15440 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
e2debe91 15441 intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
b01f2c3a 15442 }
3fec3d2f 15443 if (IS_G4X(dev))
ab9d7c30 15444 intel_dp_init(dev, DP_C, PORT_C);
725e30ad 15445 }
27185ae1 15446
3fec3d2f 15447 if (IS_G4X(dev) &&
e7281eab 15448 (I915_READ(DP_D) & DP_DETECTED))
ab9d7c30 15449 intel_dp_init(dev, DP_D, PORT_D);
bad720ff 15450 } else if (IS_GEN2(dev))
79e53945
JB
15451 intel_dvo_init(dev);
15452
103a196f 15453 if (SUPPORTS_TV(dev))
79e53945
JB
15454 intel_tv_init(dev);
15455
0bc12bcb 15456 intel_psr_init(dev);
7c8f8a70 15457
b2784e15 15458 for_each_intel_encoder(dev, encoder) {
4ef69c7a
CW
15459 encoder->base.possible_crtcs = encoder->crtc_mask;
15460 encoder->base.possible_clones =
66a9278e 15461 intel_encoder_clones(encoder);
79e53945 15462 }
47356eb6 15463
dde86e2d 15464 intel_init_pch_refclk(dev);
270b3042
DV
15465
15466 drm_helper_move_panel_connectors_to_head(dev);
79e53945
JB
15467}
15468
15469static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
15470{
60a5ca01 15471 struct drm_device *dev = fb->dev;
79e53945 15472 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
79e53945 15473
ef2d633e 15474 drm_framebuffer_cleanup(fb);
60a5ca01 15475 mutex_lock(&dev->struct_mutex);
ef2d633e 15476 WARN_ON(!intel_fb->obj->framebuffer_references--);
f8c417cd 15477 i915_gem_object_put(intel_fb->obj);
60a5ca01 15478 mutex_unlock(&dev->struct_mutex);
79e53945
JB
15479 kfree(intel_fb);
15480}
15481
15482static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
05394f39 15483 struct drm_file *file,
79e53945
JB
15484 unsigned int *handle)
15485{
15486 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
05394f39 15487 struct drm_i915_gem_object *obj = intel_fb->obj;
79e53945 15488
cc917ab4
CW
15489 if (obj->userptr.mm) {
15490 DRM_DEBUG("attempting to use a userptr for a framebuffer, denied\n");
15491 return -EINVAL;
15492 }
15493
05394f39 15494 return drm_gem_handle_create(file, &obj->base, handle);
79e53945
JB
15495}
15496
86c98588
RV
15497static int intel_user_framebuffer_dirty(struct drm_framebuffer *fb,
15498 struct drm_file *file,
15499 unsigned flags, unsigned color,
15500 struct drm_clip_rect *clips,
15501 unsigned num_clips)
15502{
15503 struct drm_device *dev = fb->dev;
15504 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
15505 struct drm_i915_gem_object *obj = intel_fb->obj;
15506
15507 mutex_lock(&dev->struct_mutex);
74b4ea1e 15508 intel_fb_obj_flush(obj, false, ORIGIN_DIRTYFB);
86c98588
RV
15509 mutex_unlock(&dev->struct_mutex);
15510
15511 return 0;
15512}
15513
79e53945
JB
15514static const struct drm_framebuffer_funcs intel_fb_funcs = {
15515 .destroy = intel_user_framebuffer_destroy,
15516 .create_handle = intel_user_framebuffer_create_handle,
86c98588 15517 .dirty = intel_user_framebuffer_dirty,
79e53945
JB
15518};
15519
b321803d
DL
15520static
15521u32 intel_fb_pitch_limit(struct drm_device *dev, uint64_t fb_modifier,
15522 uint32_t pixel_format)
15523{
15524 u32 gen = INTEL_INFO(dev)->gen;
15525
15526 if (gen >= 9) {
ac484963
VS
15527 int cpp = drm_format_plane_cpp(pixel_format, 0);
15528
b321803d
DL
15529 /* "The stride in bytes must not exceed the of the size of 8K
15530 * pixels and 32K bytes."
15531 */
ac484963 15532 return min(8192 * cpp, 32768);
666a4537 15533 } else if (gen >= 5 && !IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
b321803d
DL
15534 return 32*1024;
15535 } else if (gen >= 4) {
15536 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
15537 return 16*1024;
15538 else
15539 return 32*1024;
15540 } else if (gen >= 3) {
15541 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
15542 return 8*1024;
15543 else
15544 return 16*1024;
15545 } else {
15546 /* XXX DSPC is limited to 4k tiled */
15547 return 8*1024;
15548 }
15549}
15550
b5ea642a
DV
15551static int intel_framebuffer_init(struct drm_device *dev,
15552 struct intel_framebuffer *intel_fb,
15553 struct drm_mode_fb_cmd2 *mode_cmd,
15554 struct drm_i915_gem_object *obj)
79e53945 15555{
7b49f948 15556 struct drm_i915_private *dev_priv = to_i915(dev);
c2ff7370 15557 unsigned int tiling = i915_gem_object_get_tiling(obj);
79e53945 15558 int ret;
b321803d 15559 u32 pitch_limit, stride_alignment;
79e53945 15560
dd4916c5
DV
15561 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
15562
2a80eada 15563 if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
c2ff7370
VS
15564 /*
15565 * If there's a fence, enforce that
15566 * the fb modifier and tiling mode match.
15567 */
15568 if (tiling != I915_TILING_NONE &&
15569 tiling != intel_fb_modifier_to_tiling(mode_cmd->modifier[0])) {
2a80eada
DV
15570 DRM_DEBUG("tiling_mode doesn't match fb modifier\n");
15571 return -EINVAL;
15572 }
15573 } else {
c2ff7370 15574 if (tiling == I915_TILING_X) {
2a80eada 15575 mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
c2ff7370 15576 } else if (tiling == I915_TILING_Y) {
2a80eada
DV
15577 DRM_DEBUG("No Y tiling for legacy addfb\n");
15578 return -EINVAL;
15579 }
15580 }
15581
9a8f0a12
TU
15582 /* Passed in modifier sanity checking. */
15583 switch (mode_cmd->modifier[0]) {
15584 case I915_FORMAT_MOD_Y_TILED:
15585 case I915_FORMAT_MOD_Yf_TILED:
15586 if (INTEL_INFO(dev)->gen < 9) {
15587 DRM_DEBUG("Unsupported tiling 0x%llx!\n",
15588 mode_cmd->modifier[0]);
15589 return -EINVAL;
15590 }
15591 case DRM_FORMAT_MOD_NONE:
15592 case I915_FORMAT_MOD_X_TILED:
15593 break;
15594 default:
c0f40428
JB
15595 DRM_DEBUG("Unsupported fb modifier 0x%llx!\n",
15596 mode_cmd->modifier[0]);
57cd6508 15597 return -EINVAL;
c16ed4be 15598 }
57cd6508 15599
c2ff7370
VS
15600 /*
15601 * gen2/3 display engine uses the fence if present,
15602 * so the tiling mode must match the fb modifier exactly.
15603 */
15604 if (INTEL_INFO(dev_priv)->gen < 4 &&
15605 tiling != intel_fb_modifier_to_tiling(mode_cmd->modifier[0])) {
15606 DRM_DEBUG("tiling_mode must match fb modifier exactly on gen2/3\n");
15607 return -EINVAL;
15608 }
15609
7b49f948
VS
15610 stride_alignment = intel_fb_stride_alignment(dev_priv,
15611 mode_cmd->modifier[0],
b321803d
DL
15612 mode_cmd->pixel_format);
15613 if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
15614 DRM_DEBUG("pitch (%d) must be at least %u byte aligned\n",
15615 mode_cmd->pitches[0], stride_alignment);
57cd6508 15616 return -EINVAL;
c16ed4be 15617 }
57cd6508 15618
b321803d
DL
15619 pitch_limit = intel_fb_pitch_limit(dev, mode_cmd->modifier[0],
15620 mode_cmd->pixel_format);
a35cdaa0 15621 if (mode_cmd->pitches[0] > pitch_limit) {
b321803d
DL
15622 DRM_DEBUG("%s pitch (%u) must be at less than %d\n",
15623 mode_cmd->modifier[0] != DRM_FORMAT_MOD_NONE ?
2a80eada 15624 "tiled" : "linear",
a35cdaa0 15625 mode_cmd->pitches[0], pitch_limit);
5d7bd705 15626 return -EINVAL;
c16ed4be 15627 }
5d7bd705 15628
c2ff7370
VS
15629 /*
15630 * If there's a fence, enforce that
15631 * the fb pitch and fence stride match.
15632 */
15633 if (tiling != I915_TILING_NONE &&
3e510a8e 15634 mode_cmd->pitches[0] != i915_gem_object_get_stride(obj)) {
c16ed4be 15635 DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
3e510a8e
CW
15636 mode_cmd->pitches[0],
15637 i915_gem_object_get_stride(obj));
5d7bd705 15638 return -EINVAL;
c16ed4be 15639 }
5d7bd705 15640
57779d06 15641 /* Reject formats not supported by any plane early. */
308e5bcb 15642 switch (mode_cmd->pixel_format) {
57779d06 15643 case DRM_FORMAT_C8:
04b3924d
VS
15644 case DRM_FORMAT_RGB565:
15645 case DRM_FORMAT_XRGB8888:
15646 case DRM_FORMAT_ARGB8888:
57779d06
VS
15647 break;
15648 case DRM_FORMAT_XRGB1555:
c16ed4be 15649 if (INTEL_INFO(dev)->gen > 3) {
4ee62c76
VS
15650 DRM_DEBUG("unsupported pixel format: %s\n",
15651 drm_get_format_name(mode_cmd->pixel_format));
57779d06 15652 return -EINVAL;
c16ed4be 15653 }
57779d06 15654 break;
57779d06 15655 case DRM_FORMAT_ABGR8888:
666a4537
WB
15656 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev) &&
15657 INTEL_INFO(dev)->gen < 9) {
6c0fd451
DL
15658 DRM_DEBUG("unsupported pixel format: %s\n",
15659 drm_get_format_name(mode_cmd->pixel_format));
15660 return -EINVAL;
15661 }
15662 break;
15663 case DRM_FORMAT_XBGR8888:
04b3924d 15664 case DRM_FORMAT_XRGB2101010:
57779d06 15665 case DRM_FORMAT_XBGR2101010:
c16ed4be 15666 if (INTEL_INFO(dev)->gen < 4) {
4ee62c76
VS
15667 DRM_DEBUG("unsupported pixel format: %s\n",
15668 drm_get_format_name(mode_cmd->pixel_format));
57779d06 15669 return -EINVAL;
c16ed4be 15670 }
b5626747 15671 break;
7531208b 15672 case DRM_FORMAT_ABGR2101010:
666a4537 15673 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
7531208b
DL
15674 DRM_DEBUG("unsupported pixel format: %s\n",
15675 drm_get_format_name(mode_cmd->pixel_format));
15676 return -EINVAL;
15677 }
15678 break;
04b3924d
VS
15679 case DRM_FORMAT_YUYV:
15680 case DRM_FORMAT_UYVY:
15681 case DRM_FORMAT_YVYU:
15682 case DRM_FORMAT_VYUY:
c16ed4be 15683 if (INTEL_INFO(dev)->gen < 5) {
4ee62c76
VS
15684 DRM_DEBUG("unsupported pixel format: %s\n",
15685 drm_get_format_name(mode_cmd->pixel_format));
57779d06 15686 return -EINVAL;
c16ed4be 15687 }
57cd6508
CW
15688 break;
15689 default:
4ee62c76
VS
15690 DRM_DEBUG("unsupported pixel format: %s\n",
15691 drm_get_format_name(mode_cmd->pixel_format));
57cd6508
CW
15692 return -EINVAL;
15693 }
15694
90f9a336
VS
15695 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
15696 if (mode_cmd->offsets[0] != 0)
15697 return -EINVAL;
15698
c7d73f6a
DV
15699 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
15700 intel_fb->obj = obj;
15701
6687c906
VS
15702 ret = intel_fill_fb_info(dev_priv, &intel_fb->base);
15703 if (ret)
15704 return ret;
2d7a215f 15705
79e53945
JB
15706 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
15707 if (ret) {
15708 DRM_ERROR("framebuffer init failed %d\n", ret);
15709 return ret;
15710 }
15711
0b05e1e0
VS
15712 intel_fb->obj->framebuffer_references++;
15713
79e53945
JB
15714 return 0;
15715}
15716
79e53945
JB
15717static struct drm_framebuffer *
15718intel_user_framebuffer_create(struct drm_device *dev,
15719 struct drm_file *filp,
1eb83451 15720 const struct drm_mode_fb_cmd2 *user_mode_cmd)
79e53945 15721{
dcb1394e 15722 struct drm_framebuffer *fb;
05394f39 15723 struct drm_i915_gem_object *obj;
76dc3769 15724 struct drm_mode_fb_cmd2 mode_cmd = *user_mode_cmd;
79e53945 15725
03ac0642
CW
15726 obj = i915_gem_object_lookup(filp, mode_cmd.handles[0]);
15727 if (!obj)
cce13ff7 15728 return ERR_PTR(-ENOENT);
79e53945 15729
92907cbb 15730 fb = intel_framebuffer_create(dev, &mode_cmd, obj);
dcb1394e 15731 if (IS_ERR(fb))
34911fd3 15732 i915_gem_object_put_unlocked(obj);
dcb1394e
LW
15733
15734 return fb;
79e53945
JB
15735}
15736
0695726e 15737#ifndef CONFIG_DRM_FBDEV_EMULATION
0632fef6 15738static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
4520f53a
DV
15739{
15740}
15741#endif
15742
79e53945 15743static const struct drm_mode_config_funcs intel_mode_funcs = {
79e53945 15744 .fb_create = intel_user_framebuffer_create,
0632fef6 15745 .output_poll_changed = intel_fbdev_output_poll_changed,
5ee67f1c
MR
15746 .atomic_check = intel_atomic_check,
15747 .atomic_commit = intel_atomic_commit,
de419ab6
ML
15748 .atomic_state_alloc = intel_atomic_state_alloc,
15749 .atomic_state_clear = intel_atomic_state_clear,
79e53945
JB
15750};
15751
88212941
ID
15752/**
15753 * intel_init_display_hooks - initialize the display modesetting hooks
15754 * @dev_priv: device private
15755 */
15756void intel_init_display_hooks(struct drm_i915_private *dev_priv)
e70236a8 15757{
88212941 15758 if (INTEL_INFO(dev_priv)->gen >= 9) {
bc8d7dff 15759 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
5724dbd1
DL
15760 dev_priv->display.get_initial_plane_config =
15761 skylake_get_initial_plane_config;
bc8d7dff
DL
15762 dev_priv->display.crtc_compute_clock =
15763 haswell_crtc_compute_clock;
15764 dev_priv->display.crtc_enable = haswell_crtc_enable;
15765 dev_priv->display.crtc_disable = haswell_crtc_disable;
88212941 15766 } else if (HAS_DDI(dev_priv)) {
0e8ffe1b 15767 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
5724dbd1
DL
15768 dev_priv->display.get_initial_plane_config =
15769 ironlake_get_initial_plane_config;
797d0259
ACO
15770 dev_priv->display.crtc_compute_clock =
15771 haswell_crtc_compute_clock;
4f771f10
PZ
15772 dev_priv->display.crtc_enable = haswell_crtc_enable;
15773 dev_priv->display.crtc_disable = haswell_crtc_disable;
88212941 15774 } else if (HAS_PCH_SPLIT(dev_priv)) {
0e8ffe1b 15775 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
5724dbd1
DL
15776 dev_priv->display.get_initial_plane_config =
15777 ironlake_get_initial_plane_config;
3fb37703
ACO
15778 dev_priv->display.crtc_compute_clock =
15779 ironlake_crtc_compute_clock;
76e5a89c
DV
15780 dev_priv->display.crtc_enable = ironlake_crtc_enable;
15781 dev_priv->display.crtc_disable = ironlake_crtc_disable;
65b3d6a9 15782 } else if (IS_CHERRYVIEW(dev_priv)) {
89b667f8 15783 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
5724dbd1
DL
15784 dev_priv->display.get_initial_plane_config =
15785 i9xx_get_initial_plane_config;
65b3d6a9
ACO
15786 dev_priv->display.crtc_compute_clock = chv_crtc_compute_clock;
15787 dev_priv->display.crtc_enable = valleyview_crtc_enable;
15788 dev_priv->display.crtc_disable = i9xx_crtc_disable;
15789 } else if (IS_VALLEYVIEW(dev_priv)) {
15790 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
15791 dev_priv->display.get_initial_plane_config =
15792 i9xx_get_initial_plane_config;
15793 dev_priv->display.crtc_compute_clock = vlv_crtc_compute_clock;
89b667f8
JB
15794 dev_priv->display.crtc_enable = valleyview_crtc_enable;
15795 dev_priv->display.crtc_disable = i9xx_crtc_disable;
19ec6693
ACO
15796 } else if (IS_G4X(dev_priv)) {
15797 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
15798 dev_priv->display.get_initial_plane_config =
15799 i9xx_get_initial_plane_config;
15800 dev_priv->display.crtc_compute_clock = g4x_crtc_compute_clock;
15801 dev_priv->display.crtc_enable = i9xx_crtc_enable;
15802 dev_priv->display.crtc_disable = i9xx_crtc_disable;
70e8aa21
ACO
15803 } else if (IS_PINEVIEW(dev_priv)) {
15804 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
15805 dev_priv->display.get_initial_plane_config =
15806 i9xx_get_initial_plane_config;
15807 dev_priv->display.crtc_compute_clock = pnv_crtc_compute_clock;
15808 dev_priv->display.crtc_enable = i9xx_crtc_enable;
15809 dev_priv->display.crtc_disable = i9xx_crtc_disable;
81c97f52 15810 } else if (!IS_GEN2(dev_priv)) {
0e8ffe1b 15811 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
5724dbd1
DL
15812 dev_priv->display.get_initial_plane_config =
15813 i9xx_get_initial_plane_config;
d6dfee7a 15814 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
76e5a89c
DV
15815 dev_priv->display.crtc_enable = i9xx_crtc_enable;
15816 dev_priv->display.crtc_disable = i9xx_crtc_disable;
81c97f52
ACO
15817 } else {
15818 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
15819 dev_priv->display.get_initial_plane_config =
15820 i9xx_get_initial_plane_config;
15821 dev_priv->display.crtc_compute_clock = i8xx_crtc_compute_clock;
15822 dev_priv->display.crtc_enable = i9xx_crtc_enable;
15823 dev_priv->display.crtc_disable = i9xx_crtc_disable;
f564048e 15824 }
e70236a8 15825
e70236a8 15826 /* Returns the core display clock speed */
88212941 15827 if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv))
1652d19e
VS
15828 dev_priv->display.get_display_clock_speed =
15829 skylake_get_display_clock_speed;
88212941 15830 else if (IS_BROXTON(dev_priv))
acd3f3d3
BP
15831 dev_priv->display.get_display_clock_speed =
15832 broxton_get_display_clock_speed;
88212941 15833 else if (IS_BROADWELL(dev_priv))
1652d19e
VS
15834 dev_priv->display.get_display_clock_speed =
15835 broadwell_get_display_clock_speed;
88212941 15836 else if (IS_HASWELL(dev_priv))
1652d19e
VS
15837 dev_priv->display.get_display_clock_speed =
15838 haswell_get_display_clock_speed;
88212941 15839 else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
25eb05fc
JB
15840 dev_priv->display.get_display_clock_speed =
15841 valleyview_get_display_clock_speed;
88212941 15842 else if (IS_GEN5(dev_priv))
b37a6434
VS
15843 dev_priv->display.get_display_clock_speed =
15844 ilk_get_display_clock_speed;
88212941
ID
15845 else if (IS_I945G(dev_priv) || IS_BROADWATER(dev_priv) ||
15846 IS_GEN6(dev_priv) || IS_IVYBRIDGE(dev_priv))
e70236a8
JB
15847 dev_priv->display.get_display_clock_speed =
15848 i945_get_display_clock_speed;
88212941 15849 else if (IS_GM45(dev_priv))
34edce2f
VS
15850 dev_priv->display.get_display_clock_speed =
15851 gm45_get_display_clock_speed;
88212941 15852 else if (IS_CRESTLINE(dev_priv))
34edce2f
VS
15853 dev_priv->display.get_display_clock_speed =
15854 i965gm_get_display_clock_speed;
88212941 15855 else if (IS_PINEVIEW(dev_priv))
34edce2f
VS
15856 dev_priv->display.get_display_clock_speed =
15857 pnv_get_display_clock_speed;
88212941 15858 else if (IS_G33(dev_priv) || IS_G4X(dev_priv))
34edce2f
VS
15859 dev_priv->display.get_display_clock_speed =
15860 g33_get_display_clock_speed;
88212941 15861 else if (IS_I915G(dev_priv))
e70236a8
JB
15862 dev_priv->display.get_display_clock_speed =
15863 i915_get_display_clock_speed;
88212941 15864 else if (IS_I945GM(dev_priv) || IS_845G(dev_priv))
e70236a8
JB
15865 dev_priv->display.get_display_clock_speed =
15866 i9xx_misc_get_display_clock_speed;
88212941 15867 else if (IS_I915GM(dev_priv))
e70236a8
JB
15868 dev_priv->display.get_display_clock_speed =
15869 i915gm_get_display_clock_speed;
88212941 15870 else if (IS_I865G(dev_priv))
e70236a8
JB
15871 dev_priv->display.get_display_clock_speed =
15872 i865_get_display_clock_speed;
88212941 15873 else if (IS_I85X(dev_priv))
e70236a8 15874 dev_priv->display.get_display_clock_speed =
1b1d2716 15875 i85x_get_display_clock_speed;
623e01e5 15876 else { /* 830 */
88212941 15877 WARN(!IS_I830(dev_priv), "Unknown platform. Assuming 133 MHz CDCLK\n");
e70236a8
JB
15878 dev_priv->display.get_display_clock_speed =
15879 i830_get_display_clock_speed;
623e01e5 15880 }
e70236a8 15881
88212941 15882 if (IS_GEN5(dev_priv)) {
3bb11b53 15883 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
88212941 15884 } else if (IS_GEN6(dev_priv)) {
3bb11b53 15885 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
88212941 15886 } else if (IS_IVYBRIDGE(dev_priv)) {
3bb11b53
SJ
15887 /* FIXME: detect B0+ stepping and use auto training */
15888 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
88212941 15889 } else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
3bb11b53 15890 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
445e780b
VS
15891 }
15892
15893 if (IS_BROADWELL(dev_priv)) {
15894 dev_priv->display.modeset_commit_cdclk =
15895 broadwell_modeset_commit_cdclk;
15896 dev_priv->display.modeset_calc_cdclk =
15897 broadwell_modeset_calc_cdclk;
88212941 15898 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
27c329ed
ML
15899 dev_priv->display.modeset_commit_cdclk =
15900 valleyview_modeset_commit_cdclk;
15901 dev_priv->display.modeset_calc_cdclk =
15902 valleyview_modeset_calc_cdclk;
88212941 15903 } else if (IS_BROXTON(dev_priv)) {
27c329ed 15904 dev_priv->display.modeset_commit_cdclk =
324513c0 15905 bxt_modeset_commit_cdclk;
27c329ed 15906 dev_priv->display.modeset_calc_cdclk =
324513c0 15907 bxt_modeset_calc_cdclk;
c89e39f3
CT
15908 } else if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv)) {
15909 dev_priv->display.modeset_commit_cdclk =
15910 skl_modeset_commit_cdclk;
15911 dev_priv->display.modeset_calc_cdclk =
15912 skl_modeset_calc_cdclk;
e70236a8 15913 }
5a21b665
DV
15914
15915 switch (INTEL_INFO(dev_priv)->gen) {
15916 case 2:
15917 dev_priv->display.queue_flip = intel_gen2_queue_flip;
15918 break;
15919
15920 case 3:
15921 dev_priv->display.queue_flip = intel_gen3_queue_flip;
15922 break;
15923
15924 case 4:
15925 case 5:
15926 dev_priv->display.queue_flip = intel_gen4_queue_flip;
15927 break;
15928
15929 case 6:
15930 dev_priv->display.queue_flip = intel_gen6_queue_flip;
15931 break;
15932 case 7:
15933 case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
15934 dev_priv->display.queue_flip = intel_gen7_queue_flip;
15935 break;
15936 case 9:
15937 /* Drop through - unsupported since execlist only. */
15938 default:
15939 /* Default just returns -ENODEV to indicate unsupported */
15940 dev_priv->display.queue_flip = intel_default_queue_flip;
15941 }
e70236a8
JB
15942}
15943
b690e96c
JB
15944/*
15945 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
15946 * resume, or other times. This quirk makes sure that's the case for
15947 * affected systems.
15948 */
0206e353 15949static void quirk_pipea_force(struct drm_device *dev)
b690e96c 15950{
fac5e23e 15951 struct drm_i915_private *dev_priv = to_i915(dev);
b690e96c
JB
15952
15953 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
bc0daf48 15954 DRM_INFO("applying pipe a force quirk\n");
b690e96c
JB
15955}
15956
b6b5d049
VS
15957static void quirk_pipeb_force(struct drm_device *dev)
15958{
fac5e23e 15959 struct drm_i915_private *dev_priv = to_i915(dev);
b6b5d049
VS
15960
15961 dev_priv->quirks |= QUIRK_PIPEB_FORCE;
15962 DRM_INFO("applying pipe b force quirk\n");
15963}
15964
435793df
KP
15965/*
15966 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
15967 */
15968static void quirk_ssc_force_disable(struct drm_device *dev)
15969{
fac5e23e 15970 struct drm_i915_private *dev_priv = to_i915(dev);
435793df 15971 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
bc0daf48 15972 DRM_INFO("applying lvds SSC disable quirk\n");
435793df
KP
15973}
15974
4dca20ef 15975/*
5a15ab5b
CE
15976 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
15977 * brightness value
4dca20ef
CE
15978 */
15979static void quirk_invert_brightness(struct drm_device *dev)
15980{
fac5e23e 15981 struct drm_i915_private *dev_priv = to_i915(dev);
4dca20ef 15982 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
bc0daf48 15983 DRM_INFO("applying inverted panel brightness quirk\n");
435793df
KP
15984}
15985
9c72cc6f
SD
15986/* Some VBT's incorrectly indicate no backlight is present */
15987static void quirk_backlight_present(struct drm_device *dev)
15988{
fac5e23e 15989 struct drm_i915_private *dev_priv = to_i915(dev);
9c72cc6f
SD
15990 dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
15991 DRM_INFO("applying backlight present quirk\n");
15992}
15993
b690e96c
JB
15994struct intel_quirk {
15995 int device;
15996 int subsystem_vendor;
15997 int subsystem_device;
15998 void (*hook)(struct drm_device *dev);
15999};
16000
5f85f176
EE
16001/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
16002struct intel_dmi_quirk {
16003 void (*hook)(struct drm_device *dev);
16004 const struct dmi_system_id (*dmi_id_list)[];
16005};
16006
16007static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
16008{
16009 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
16010 return 1;
16011}
16012
16013static const struct intel_dmi_quirk intel_dmi_quirks[] = {
16014 {
16015 .dmi_id_list = &(const struct dmi_system_id[]) {
16016 {
16017 .callback = intel_dmi_reverse_brightness,
16018 .ident = "NCR Corporation",
16019 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
16020 DMI_MATCH(DMI_PRODUCT_NAME, ""),
16021 },
16022 },
16023 { } /* terminating entry */
16024 },
16025 .hook = quirk_invert_brightness,
16026 },
16027};
16028
c43b5634 16029static struct intel_quirk intel_quirks[] = {
b690e96c
JB
16030 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
16031 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
16032
b690e96c
JB
16033 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
16034 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
16035
5f080c0f
VS
16036 /* 830 needs to leave pipe A & dpll A up */
16037 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
16038
b6b5d049
VS
16039 /* 830 needs to leave pipe B & dpll B up */
16040 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
16041
435793df
KP
16042 /* Lenovo U160 cannot use SSC on LVDS */
16043 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
070d329a
MAS
16044
16045 /* Sony Vaio Y cannot use SSC on LVDS */
16046 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
5a15ab5b 16047
be505f64
AH
16048 /* Acer Aspire 5734Z must invert backlight brightness */
16049 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
16050
16051 /* Acer/eMachines G725 */
16052 { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
16053
16054 /* Acer/eMachines e725 */
16055 { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
16056
16057 /* Acer/Packard Bell NCL20 */
16058 { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
16059
16060 /* Acer Aspire 4736Z */
16061 { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
0f540c3a
JN
16062
16063 /* Acer Aspire 5336 */
16064 { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
2e93a1aa
SD
16065
16066 /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
16067 { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
d4967d8c 16068
dfb3d47b
SD
16069 /* Acer C720 Chromebook (Core i3 4005U) */
16070 { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
16071
b2a9601c 16072 /* Apple Macbook 2,1 (Core 2 T7400) */
16073 { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
16074
1b9448b0
JN
16075 /* Apple Macbook 4,1 */
16076 { 0x2a02, 0x106b, 0x00a1, quirk_backlight_present },
16077
d4967d8c
SD
16078 /* Toshiba CB35 Chromebook (Celeron 2955U) */
16079 { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
724cb06f
SD
16080
16081 /* HP Chromebook 14 (Celeron 2955U) */
16082 { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
cf6f0af9
JN
16083
16084 /* Dell Chromebook 11 */
16085 { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
9be64eee
JN
16086
16087 /* Dell Chromebook 11 (2015 version) */
16088 { 0x0a16, 0x1028, 0x0a35, quirk_backlight_present },
b690e96c
JB
16089};
16090
16091static void intel_init_quirks(struct drm_device *dev)
16092{
16093 struct pci_dev *d = dev->pdev;
16094 int i;
16095
16096 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
16097 struct intel_quirk *q = &intel_quirks[i];
16098
16099 if (d->device == q->device &&
16100 (d->subsystem_vendor == q->subsystem_vendor ||
16101 q->subsystem_vendor == PCI_ANY_ID) &&
16102 (d->subsystem_device == q->subsystem_device ||
16103 q->subsystem_device == PCI_ANY_ID))
16104 q->hook(dev);
16105 }
5f85f176
EE
16106 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
16107 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
16108 intel_dmi_quirks[i].hook(dev);
16109 }
b690e96c
JB
16110}
16111
9cce37f4
JB
16112/* Disable the VGA plane that we never use */
16113static void i915_disable_vga(struct drm_device *dev)
16114{
fac5e23e 16115 struct drm_i915_private *dev_priv = to_i915(dev);
52a05c30 16116 struct pci_dev *pdev = dev_priv->drm.pdev;
9cce37f4 16117 u8 sr1;
f0f59a00 16118 i915_reg_t vga_reg = i915_vgacntrl_reg(dev);
9cce37f4 16119
2b37c616 16120 /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
52a05c30 16121 vga_get_uninterruptible(pdev, VGA_RSRC_LEGACY_IO);
3fdcf431 16122 outb(SR01, VGA_SR_INDEX);
9cce37f4
JB
16123 sr1 = inb(VGA_SR_DATA);
16124 outb(sr1 | 1<<5, VGA_SR_DATA);
52a05c30 16125 vga_put(pdev, VGA_RSRC_LEGACY_IO);
9cce37f4
JB
16126 udelay(300);
16127
01f5a626 16128 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
9cce37f4
JB
16129 POSTING_READ(vga_reg);
16130}
16131
f817586c
DV
16132void intel_modeset_init_hw(struct drm_device *dev)
16133{
fac5e23e 16134 struct drm_i915_private *dev_priv = to_i915(dev);
1a617b77 16135
b6283055 16136 intel_update_cdclk(dev);
1a617b77
ML
16137
16138 dev_priv->atomic_cdclk_freq = dev_priv->cdclk_freq;
16139
f817586c 16140 intel_init_clock_gating(dev);
f817586c
DV
16141}
16142
d93c0372
MR
16143/*
16144 * Calculate what we think the watermarks should be for the state we've read
16145 * out of the hardware and then immediately program those watermarks so that
16146 * we ensure the hardware settings match our internal state.
16147 *
16148 * We can calculate what we think WM's should be by creating a duplicate of the
16149 * current state (which was constructed during hardware readout) and running it
16150 * through the atomic check code to calculate new watermark values in the
16151 * state object.
16152 */
16153static void sanitize_watermarks(struct drm_device *dev)
16154{
16155 struct drm_i915_private *dev_priv = to_i915(dev);
16156 struct drm_atomic_state *state;
16157 struct drm_crtc *crtc;
16158 struct drm_crtc_state *cstate;
16159 struct drm_modeset_acquire_ctx ctx;
16160 int ret;
16161 int i;
16162
16163 /* Only supported on platforms that use atomic watermark design */
ed4a6a7c 16164 if (!dev_priv->display.optimize_watermarks)
d93c0372
MR
16165 return;
16166
16167 /*
16168 * We need to hold connection_mutex before calling duplicate_state so
16169 * that the connector loop is protected.
16170 */
16171 drm_modeset_acquire_init(&ctx, 0);
16172retry:
0cd1262d 16173 ret = drm_modeset_lock_all_ctx(dev, &ctx);
d93c0372
MR
16174 if (ret == -EDEADLK) {
16175 drm_modeset_backoff(&ctx);
16176 goto retry;
16177 } else if (WARN_ON(ret)) {
0cd1262d 16178 goto fail;
d93c0372
MR
16179 }
16180
16181 state = drm_atomic_helper_duplicate_state(dev, &ctx);
16182 if (WARN_ON(IS_ERR(state)))
0cd1262d 16183 goto fail;
d93c0372 16184
ed4a6a7c
MR
16185 /*
16186 * Hardware readout is the only time we don't want to calculate
16187 * intermediate watermarks (since we don't trust the current
16188 * watermarks).
16189 */
16190 to_intel_atomic_state(state)->skip_intermediate_wm = true;
16191
d93c0372
MR
16192 ret = intel_atomic_check(dev, state);
16193 if (ret) {
16194 /*
16195 * If we fail here, it means that the hardware appears to be
16196 * programmed in a way that shouldn't be possible, given our
16197 * understanding of watermark requirements. This might mean a
16198 * mistake in the hardware readout code or a mistake in the
16199 * watermark calculations for a given platform. Raise a WARN
16200 * so that this is noticeable.
16201 *
16202 * If this actually happens, we'll have to just leave the
16203 * BIOS-programmed watermarks untouched and hope for the best.
16204 */
16205 WARN(true, "Could not determine valid watermarks for inherited state\n");
0cd1262d 16206 goto fail;
d93c0372
MR
16207 }
16208
16209 /* Write calculated watermark values back */
d93c0372
MR
16210 for_each_crtc_in_state(state, crtc, cstate, i) {
16211 struct intel_crtc_state *cs = to_intel_crtc_state(cstate);
16212
ed4a6a7c
MR
16213 cs->wm.need_postvbl_update = true;
16214 dev_priv->display.optimize_watermarks(cs);
d93c0372
MR
16215 }
16216
16217 drm_atomic_state_free(state);
0cd1262d 16218fail:
d93c0372
MR
16219 drm_modeset_drop_locks(&ctx);
16220 drm_modeset_acquire_fini(&ctx);
16221}
16222
79e53945
JB
16223void intel_modeset_init(struct drm_device *dev)
16224{
72e96d64
JL
16225 struct drm_i915_private *dev_priv = to_i915(dev);
16226 struct i915_ggtt *ggtt = &dev_priv->ggtt;
1fe47785 16227 int sprite, ret;
8cc87b75 16228 enum pipe pipe;
46f297fb 16229 struct intel_crtc *crtc;
79e53945
JB
16230
16231 drm_mode_config_init(dev);
16232
16233 dev->mode_config.min_width = 0;
16234 dev->mode_config.min_height = 0;
16235
019d96cb
DA
16236 dev->mode_config.preferred_depth = 24;
16237 dev->mode_config.prefer_shadow = 1;
16238
25bab385
TU
16239 dev->mode_config.allow_fb_modifiers = true;
16240
e6ecefaa 16241 dev->mode_config.funcs = &intel_mode_funcs;
79e53945 16242
b690e96c
JB
16243 intel_init_quirks(dev);
16244
1fa61106
ED
16245 intel_init_pm(dev);
16246
e3c74757
BW
16247 if (INTEL_INFO(dev)->num_pipes == 0)
16248 return;
16249
69f92f67
LW
16250 /*
16251 * There may be no VBT; and if the BIOS enabled SSC we can
16252 * just keep using it to avoid unnecessary flicker. Whereas if the
16253 * BIOS isn't using it, don't assume it will work even if the VBT
16254 * indicates as much.
16255 */
16256 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)) {
16257 bool bios_lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
16258 DREF_SSC1_ENABLE);
16259
16260 if (dev_priv->vbt.lvds_use_ssc != bios_lvds_use_ssc) {
16261 DRM_DEBUG_KMS("SSC %sabled by BIOS, overriding VBT which says %sabled\n",
16262 bios_lvds_use_ssc ? "en" : "dis",
16263 dev_priv->vbt.lvds_use_ssc ? "en" : "dis");
16264 dev_priv->vbt.lvds_use_ssc = bios_lvds_use_ssc;
16265 }
16266 }
16267
a6c45cf0
CW
16268 if (IS_GEN2(dev)) {
16269 dev->mode_config.max_width = 2048;
16270 dev->mode_config.max_height = 2048;
16271 } else if (IS_GEN3(dev)) {
5e4d6fa7
KP
16272 dev->mode_config.max_width = 4096;
16273 dev->mode_config.max_height = 4096;
79e53945 16274 } else {
a6c45cf0
CW
16275 dev->mode_config.max_width = 8192;
16276 dev->mode_config.max_height = 8192;
79e53945 16277 }
068be561 16278
dc41c154
VS
16279 if (IS_845G(dev) || IS_I865G(dev)) {
16280 dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
16281 dev->mode_config.cursor_height = 1023;
16282 } else if (IS_GEN2(dev)) {
068be561
DL
16283 dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
16284 dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
16285 } else {
16286 dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
16287 dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
16288 }
16289
72e96d64 16290 dev->mode_config.fb_base = ggtt->mappable_base;
79e53945 16291
28c97730 16292 DRM_DEBUG_KMS("%d display pipe%s available.\n",
7eb552ae
BW
16293 INTEL_INFO(dev)->num_pipes,
16294 INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
79e53945 16295
055e393f 16296 for_each_pipe(dev_priv, pipe) {
8cc87b75 16297 intel_crtc_init(dev, pipe);
3bdcfc0c 16298 for_each_sprite(dev_priv, pipe, sprite) {
1fe47785 16299 ret = intel_plane_init(dev, pipe, sprite);
7f1f3851 16300 if (ret)
06da8da2 16301 DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
1fe47785 16302 pipe_name(pipe), sprite_name(pipe, sprite), ret);
7f1f3851 16303 }
79e53945
JB
16304 }
16305
bfa7df01
VS
16306 intel_update_czclk(dev_priv);
16307 intel_update_cdclk(dev);
16308
e72f9fbf 16309 intel_shared_dpll_init(dev);
ee7b9f93 16310
b2045352
VS
16311 if (dev_priv->max_cdclk_freq == 0)
16312 intel_update_max_cdclk(dev);
16313
9cce37f4
JB
16314 /* Just disable it once at startup */
16315 i915_disable_vga(dev);
79e53945 16316 intel_setup_outputs(dev);
11be49eb 16317
6e9f798d 16318 drm_modeset_lock_all(dev);
043e9bda 16319 intel_modeset_setup_hw_state(dev);
6e9f798d 16320 drm_modeset_unlock_all(dev);
46f297fb 16321
d3fcc808 16322 for_each_intel_crtc(dev, crtc) {
eeebeac5
ML
16323 struct intel_initial_plane_config plane_config = {};
16324
46f297fb
JB
16325 if (!crtc->active)
16326 continue;
16327
46f297fb 16328 /*
46f297fb
JB
16329 * Note that reserving the BIOS fb up front prevents us
16330 * from stuffing other stolen allocations like the ring
16331 * on top. This prevents some ugliness at boot time, and
16332 * can even allow for smooth boot transitions if the BIOS
16333 * fb is large enough for the active pipe configuration.
16334 */
eeebeac5
ML
16335 dev_priv->display.get_initial_plane_config(crtc,
16336 &plane_config);
16337
16338 /*
16339 * If the fb is shared between multiple heads, we'll
16340 * just get the first one.
16341 */
16342 intel_find_initial_plane_obj(crtc, &plane_config);
46f297fb 16343 }
d93c0372
MR
16344
16345 /*
16346 * Make sure hardware watermarks really match the state we read out.
16347 * Note that we need to do this after reconstructing the BIOS fb's
16348 * since the watermark calculation done here will use pstate->fb.
16349 */
16350 sanitize_watermarks(dev);
2c7111db
CW
16351}
16352
7fad798e
DV
16353static void intel_enable_pipe_a(struct drm_device *dev)
16354{
16355 struct intel_connector *connector;
16356 struct drm_connector *crt = NULL;
16357 struct intel_load_detect_pipe load_detect_temp;
208bf9fd 16358 struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
7fad798e
DV
16359
16360 /* We can't just switch on the pipe A, we need to set things up with a
16361 * proper mode and output configuration. As a gross hack, enable pipe A
16362 * by enabling the load detect pipe once. */
3a3371ff 16363 for_each_intel_connector(dev, connector) {
7fad798e
DV
16364 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
16365 crt = &connector->base;
16366 break;
16367 }
16368 }
16369
16370 if (!crt)
16371 return;
16372
208bf9fd 16373 if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
49172fee 16374 intel_release_load_detect_pipe(crt, &load_detect_temp, ctx);
7fad798e
DV
16375}
16376
fa555837
DV
16377static bool
16378intel_check_plane_mapping(struct intel_crtc *crtc)
16379{
7eb552ae 16380 struct drm_device *dev = crtc->base.dev;
fac5e23e 16381 struct drm_i915_private *dev_priv = to_i915(dev);
649636ef 16382 u32 val;
fa555837 16383
7eb552ae 16384 if (INTEL_INFO(dev)->num_pipes == 1)
fa555837
DV
16385 return true;
16386
649636ef 16387 val = I915_READ(DSPCNTR(!crtc->plane));
fa555837
DV
16388
16389 if ((val & DISPLAY_PLANE_ENABLE) &&
16390 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
16391 return false;
16392
16393 return true;
16394}
16395
02e93c35
VS
16396static bool intel_crtc_has_encoders(struct intel_crtc *crtc)
16397{
16398 struct drm_device *dev = crtc->base.dev;
16399 struct intel_encoder *encoder;
16400
16401 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
16402 return true;
16403
16404 return false;
16405}
16406
496b0fc3
ML
16407static struct intel_connector *intel_encoder_find_connector(struct intel_encoder *encoder)
16408{
16409 struct drm_device *dev = encoder->base.dev;
16410 struct intel_connector *connector;
16411
16412 for_each_connector_on_encoder(dev, &encoder->base, connector)
16413 return connector;
16414
16415 return NULL;
16416}
16417
a168f5b3
VS
16418static bool has_pch_trancoder(struct drm_i915_private *dev_priv,
16419 enum transcoder pch_transcoder)
16420{
16421 return HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv) ||
16422 (HAS_PCH_LPT_H(dev_priv) && pch_transcoder == TRANSCODER_A);
16423}
16424
24929352
DV
16425static void intel_sanitize_crtc(struct intel_crtc *crtc)
16426{
16427 struct drm_device *dev = crtc->base.dev;
fac5e23e 16428 struct drm_i915_private *dev_priv = to_i915(dev);
4d1de975 16429 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
24929352 16430
24929352 16431 /* Clear any frame start delays used for debugging left by the BIOS */
4d1de975
JN
16432 if (!transcoder_is_dsi(cpu_transcoder)) {
16433 i915_reg_t reg = PIPECONF(cpu_transcoder);
16434
16435 I915_WRITE(reg,
16436 I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
16437 }
24929352 16438
d3eaf884 16439 /* restore vblank interrupts to correct state */
9625604c 16440 drm_crtc_vblank_reset(&crtc->base);
d297e103 16441 if (crtc->active) {
f9cd7b88
VS
16442 struct intel_plane *plane;
16443
9625604c 16444 drm_crtc_vblank_on(&crtc->base);
f9cd7b88
VS
16445
16446 /* Disable everything but the primary plane */
16447 for_each_intel_plane_on_crtc(dev, crtc, plane) {
16448 if (plane->base.type == DRM_PLANE_TYPE_PRIMARY)
16449 continue;
16450
16451 plane->disable_plane(&plane->base, &crtc->base);
16452 }
9625604c 16453 }
d3eaf884 16454
24929352 16455 /* We need to sanitize the plane -> pipe mapping first because this will
fa555837
DV
16456 * disable the crtc (and hence change the state) if it is wrong. Note
16457 * that gen4+ has a fixed plane -> pipe mapping. */
16458 if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
24929352
DV
16459 bool plane;
16460
78108b7c
VS
16461 DRM_DEBUG_KMS("[CRTC:%d:%s] wrong plane connection detected!\n",
16462 crtc->base.base.id, crtc->base.name);
24929352
DV
16463
16464 /* Pipe has the wrong plane attached and the plane is active.
16465 * Temporarily change the plane mapping and disable everything
16466 * ... */
16467 plane = crtc->plane;
936e71e3 16468 to_intel_plane_state(crtc->base.primary->state)->base.visible = true;
24929352 16469 crtc->plane = !plane;
b17d48e2 16470 intel_crtc_disable_noatomic(&crtc->base);
24929352 16471 crtc->plane = plane;
24929352 16472 }
24929352 16473
7fad798e
DV
16474 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
16475 crtc->pipe == PIPE_A && !crtc->active) {
16476 /* BIOS forgot to enable pipe A, this mostly happens after
16477 * resume. Force-enable the pipe to fix this, the update_dpms
16478 * call below we restore the pipe to the right state, but leave
16479 * the required bits on. */
16480 intel_enable_pipe_a(dev);
16481 }
16482
24929352
DV
16483 /* Adjust the state of the output pipe according to whether we
16484 * have active connectors/encoders. */
842e0307 16485 if (crtc->active && !intel_crtc_has_encoders(crtc))
b17d48e2 16486 intel_crtc_disable_noatomic(&crtc->base);
24929352 16487
a3ed6aad 16488 if (crtc->active || HAS_GMCH_DISPLAY(dev)) {
4cc31489
DV
16489 /*
16490 * We start out with underrun reporting disabled to avoid races.
16491 * For correct bookkeeping mark this on active crtcs.
16492 *
c5ab3bc0
DV
16493 * Also on gmch platforms we dont have any hardware bits to
16494 * disable the underrun reporting. Which means we need to start
16495 * out with underrun reporting disabled also on inactive pipes,
16496 * since otherwise we'll complain about the garbage we read when
16497 * e.g. coming up after runtime pm.
16498 *
4cc31489
DV
16499 * No protection against concurrent access is required - at
16500 * worst a fifo underrun happens which also sets this to false.
16501 */
16502 crtc->cpu_fifo_underrun_disabled = true;
a168f5b3
VS
16503 /*
16504 * We track the PCH trancoder underrun reporting state
16505 * within the crtc. With crtc for pipe A housing the underrun
16506 * reporting state for PCH transcoder A, crtc for pipe B housing
16507 * it for PCH transcoder B, etc. LPT-H has only PCH transcoder A,
16508 * and marking underrun reporting as disabled for the non-existing
16509 * PCH transcoders B and C would prevent enabling the south
16510 * error interrupt (see cpt_can_enable_serr_int()).
16511 */
16512 if (has_pch_trancoder(dev_priv, (enum transcoder)crtc->pipe))
16513 crtc->pch_fifo_underrun_disabled = true;
4cc31489 16514 }
24929352
DV
16515}
16516
16517static void intel_sanitize_encoder(struct intel_encoder *encoder)
16518{
16519 struct intel_connector *connector;
24929352
DV
16520
16521 /* We need to check both for a crtc link (meaning that the
16522 * encoder is active and trying to read from a pipe) and the
16523 * pipe itself being active. */
16524 bool has_active_crtc = encoder->base.crtc &&
16525 to_intel_crtc(encoder->base.crtc)->active;
16526
496b0fc3
ML
16527 connector = intel_encoder_find_connector(encoder);
16528 if (connector && !has_active_crtc) {
24929352
DV
16529 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
16530 encoder->base.base.id,
8e329a03 16531 encoder->base.name);
24929352
DV
16532
16533 /* Connector is active, but has no active pipe. This is
16534 * fallout from our resume register restoring. Disable
16535 * the encoder manually again. */
16536 if (encoder->base.crtc) {
fd6bbda9
ML
16537 struct drm_crtc_state *crtc_state = encoder->base.crtc->state;
16538
24929352
DV
16539 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
16540 encoder->base.base.id,
8e329a03 16541 encoder->base.name);
fd6bbda9 16542 encoder->disable(encoder, to_intel_crtc_state(crtc_state), connector->base.state);
a62d1497 16543 if (encoder->post_disable)
fd6bbda9 16544 encoder->post_disable(encoder, to_intel_crtc_state(crtc_state), connector->base.state);
24929352 16545 }
7f1950fb 16546 encoder->base.crtc = NULL;
24929352
DV
16547
16548 /* Inconsistent output/port/pipe state happens presumably due to
16549 * a bug in one of the get_hw_state functions. Or someplace else
16550 * in our code, like the register restore mess on resume. Clamp
16551 * things to off as a safer default. */
fd6bbda9
ML
16552
16553 connector->base.dpms = DRM_MODE_DPMS_OFF;
16554 connector->base.encoder = NULL;
24929352
DV
16555 }
16556 /* Enabled encoders without active connectors will be fixed in
16557 * the crtc fixup. */
16558}
16559
04098753 16560void i915_redisable_vga_power_on(struct drm_device *dev)
0fde901f 16561{
fac5e23e 16562 struct drm_i915_private *dev_priv = to_i915(dev);
f0f59a00 16563 i915_reg_t vga_reg = i915_vgacntrl_reg(dev);
0fde901f 16564
04098753
ID
16565 if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
16566 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
16567 i915_disable_vga(dev);
16568 }
16569}
16570
16571void i915_redisable_vga(struct drm_device *dev)
16572{
fac5e23e 16573 struct drm_i915_private *dev_priv = to_i915(dev);
04098753 16574
8dc8a27c
PZ
16575 /* This function can be called both from intel_modeset_setup_hw_state or
16576 * at a very early point in our resume sequence, where the power well
16577 * structures are not yet restored. Since this function is at a very
16578 * paranoid "someone might have enabled VGA while we were not looking"
16579 * level, just check if the power well is enabled instead of trying to
16580 * follow the "don't touch the power well if we don't need it" policy
16581 * the rest of the driver uses. */
6392f847 16582 if (!intel_display_power_get_if_enabled(dev_priv, POWER_DOMAIN_VGA))
8dc8a27c
PZ
16583 return;
16584
04098753 16585 i915_redisable_vga_power_on(dev);
6392f847
ID
16586
16587 intel_display_power_put(dev_priv, POWER_DOMAIN_VGA);
0fde901f
KM
16588}
16589
f9cd7b88 16590static bool primary_get_hw_state(struct intel_plane *plane)
98ec7739 16591{
f9cd7b88 16592 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
98ec7739 16593
f9cd7b88 16594 return I915_READ(DSPCNTR(plane->plane)) & DISPLAY_PLANE_ENABLE;
d032ffa0
ML
16595}
16596
f9cd7b88
VS
16597/* FIXME read out full plane state for all planes */
16598static void readout_plane_state(struct intel_crtc *crtc)
d032ffa0 16599{
b26d3ea3 16600 struct drm_plane *primary = crtc->base.primary;
f9cd7b88 16601 struct intel_plane_state *plane_state =
b26d3ea3 16602 to_intel_plane_state(primary->state);
d032ffa0 16603
936e71e3 16604 plane_state->base.visible = crtc->active &&
b26d3ea3
ML
16605 primary_get_hw_state(to_intel_plane(primary));
16606
936e71e3 16607 if (plane_state->base.visible)
b26d3ea3 16608 crtc->base.state->plane_mask |= 1 << drm_plane_index(primary);
98ec7739
VS
16609}
16610
30e984df 16611static void intel_modeset_readout_hw_state(struct drm_device *dev)
24929352 16612{
fac5e23e 16613 struct drm_i915_private *dev_priv = to_i915(dev);
24929352 16614 enum pipe pipe;
24929352
DV
16615 struct intel_crtc *crtc;
16616 struct intel_encoder *encoder;
16617 struct intel_connector *connector;
5358901f 16618 int i;
24929352 16619
565602d7
ML
16620 dev_priv->active_crtcs = 0;
16621
d3fcc808 16622 for_each_intel_crtc(dev, crtc) {
565602d7
ML
16623 struct intel_crtc_state *crtc_state = crtc->config;
16624 int pixclk = 0;
3b117c8f 16625
ec2dc6a0 16626 __drm_atomic_helper_crtc_destroy_state(&crtc_state->base);
565602d7
ML
16627 memset(crtc_state, 0, sizeof(*crtc_state));
16628 crtc_state->base.crtc = &crtc->base;
24929352 16629
565602d7
ML
16630 crtc_state->base.active = crtc_state->base.enable =
16631 dev_priv->display.get_pipe_config(crtc, crtc_state);
16632
16633 crtc->base.enabled = crtc_state->base.enable;
16634 crtc->active = crtc_state->base.active;
16635
16636 if (crtc_state->base.active) {
16637 dev_priv->active_crtcs |= 1 << crtc->pipe;
16638
c89e39f3 16639 if (INTEL_GEN(dev_priv) >= 9 || IS_BROADWELL(dev_priv))
565602d7 16640 pixclk = ilk_pipe_pixel_rate(crtc_state);
9558d15d 16641 else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
565602d7
ML
16642 pixclk = crtc_state->base.adjusted_mode.crtc_clock;
16643 else
16644 WARN_ON(dev_priv->display.modeset_calc_cdclk);
9558d15d
VS
16645
16646 /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
16647 if (IS_BROADWELL(dev_priv) && crtc_state->ips_enabled)
16648 pixclk = DIV_ROUND_UP(pixclk * 100, 95);
565602d7
ML
16649 }
16650
16651 dev_priv->min_pixclk[crtc->pipe] = pixclk;
b70709a6 16652
f9cd7b88 16653 readout_plane_state(crtc);
24929352 16654
78108b7c
VS
16655 DRM_DEBUG_KMS("[CRTC:%d:%s] hw state readout: %s\n",
16656 crtc->base.base.id, crtc->base.name,
24929352
DV
16657 crtc->active ? "enabled" : "disabled");
16658 }
16659
5358901f
DV
16660 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
16661 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
16662
2edd6443
ACO
16663 pll->on = pll->funcs.get_hw_state(dev_priv, pll,
16664 &pll->config.hw_state);
3e369b76 16665 pll->config.crtc_mask = 0;
d3fcc808 16666 for_each_intel_crtc(dev, crtc) {
2dd66ebd 16667 if (crtc->active && crtc->config->shared_dpll == pll)
3e369b76 16668 pll->config.crtc_mask |= 1 << crtc->pipe;
5358901f 16669 }
2dd66ebd 16670 pll->active_mask = pll->config.crtc_mask;
5358901f 16671
1e6f2ddc 16672 DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
3e369b76 16673 pll->name, pll->config.crtc_mask, pll->on);
5358901f
DV
16674 }
16675
b2784e15 16676 for_each_intel_encoder(dev, encoder) {
24929352
DV
16677 pipe = 0;
16678
16679 if (encoder->get_hw_state(encoder, &pipe)) {
045ac3b5
JB
16680 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
16681 encoder->base.crtc = &crtc->base;
253c84c8 16682 crtc->config->output_types |= 1 << encoder->type;
6e3c9717 16683 encoder->get_config(encoder, crtc->config);
24929352
DV
16684 } else {
16685 encoder->base.crtc = NULL;
16686 }
16687
6f2bcceb 16688 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
24929352 16689 encoder->base.base.id,
8e329a03 16690 encoder->base.name,
24929352 16691 encoder->base.crtc ? "enabled" : "disabled",
6f2bcceb 16692 pipe_name(pipe));
24929352
DV
16693 }
16694
3a3371ff 16695 for_each_intel_connector(dev, connector) {
24929352
DV
16696 if (connector->get_hw_state(connector)) {
16697 connector->base.dpms = DRM_MODE_DPMS_ON;
2aa974c9
ML
16698
16699 encoder = connector->encoder;
16700 connector->base.encoder = &encoder->base;
16701
16702 if (encoder->base.crtc &&
16703 encoder->base.crtc->state->active) {
16704 /*
16705 * This has to be done during hardware readout
16706 * because anything calling .crtc_disable may
16707 * rely on the connector_mask being accurate.
16708 */
16709 encoder->base.crtc->state->connector_mask |=
16710 1 << drm_connector_index(&connector->base);
e87a52b3
ML
16711 encoder->base.crtc->state->encoder_mask |=
16712 1 << drm_encoder_index(&encoder->base);
2aa974c9
ML
16713 }
16714
24929352
DV
16715 } else {
16716 connector->base.dpms = DRM_MODE_DPMS_OFF;
16717 connector->base.encoder = NULL;
16718 }
16719 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
16720 connector->base.base.id,
c23cc417 16721 connector->base.name,
24929352
DV
16722 connector->base.encoder ? "enabled" : "disabled");
16723 }
7f4c6284
VS
16724
16725 for_each_intel_crtc(dev, crtc) {
16726 crtc->base.hwmode = crtc->config->base.adjusted_mode;
16727
16728 memset(&crtc->base.mode, 0, sizeof(crtc->base.mode));
16729 if (crtc->base.state->active) {
16730 intel_mode_from_pipe_config(&crtc->base.mode, crtc->config);
16731 intel_mode_from_pipe_config(&crtc->base.state->adjusted_mode, crtc->config);
16732 WARN_ON(drm_atomic_set_mode_for_crtc(crtc->base.state, &crtc->base.mode));
16733
16734 /*
16735 * The initial mode needs to be set in order to keep
16736 * the atomic core happy. It wants a valid mode if the
16737 * crtc's enabled, so we do the above call.
16738 *
16739 * At this point some state updated by the connectors
16740 * in their ->detect() callback has not run yet, so
16741 * no recalculation can be done yet.
16742 *
16743 * Even if we could do a recalculation and modeset
16744 * right now it would cause a double modeset if
16745 * fbdev or userspace chooses a different initial mode.
16746 *
16747 * If that happens, someone indicated they wanted a
16748 * mode change, which means it's safe to do a full
16749 * recalculation.
16750 */
16751 crtc->base.state->mode.private_flags = I915_MODE_FLAG_INHERITED;
9eca6832
VS
16752
16753 drm_calc_timestamping_constants(&crtc->base, &crtc->base.hwmode);
16754 update_scanline_offset(crtc);
7f4c6284 16755 }
e3b247da
VS
16756
16757 intel_pipe_config_sanity_check(dev_priv, crtc->config);
7f4c6284 16758 }
30e984df
DV
16759}
16760
043e9bda
ML
16761/* Scan out the current hw modeset state,
16762 * and sanitizes it to the current state
16763 */
16764static void
16765intel_modeset_setup_hw_state(struct drm_device *dev)
30e984df 16766{
fac5e23e 16767 struct drm_i915_private *dev_priv = to_i915(dev);
30e984df 16768 enum pipe pipe;
30e984df
DV
16769 struct intel_crtc *crtc;
16770 struct intel_encoder *encoder;
35c95375 16771 int i;
30e984df
DV
16772
16773 intel_modeset_readout_hw_state(dev);
24929352
DV
16774
16775 /* HW state is read out, now we need to sanitize this mess. */
b2784e15 16776 for_each_intel_encoder(dev, encoder) {
24929352
DV
16777 intel_sanitize_encoder(encoder);
16778 }
16779
055e393f 16780 for_each_pipe(dev_priv, pipe) {
24929352
DV
16781 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
16782 intel_sanitize_crtc(crtc);
6e3c9717
ACO
16783 intel_dump_pipe_config(crtc, crtc->config,
16784 "[setup_hw_state]");
24929352 16785 }
9a935856 16786
d29b2f9d
ACO
16787 intel_modeset_update_connector_atomic_state(dev);
16788
35c95375
DV
16789 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
16790 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
16791
2dd66ebd 16792 if (!pll->on || pll->active_mask)
35c95375
DV
16793 continue;
16794
16795 DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
16796
2edd6443 16797 pll->funcs.disable(dev_priv, pll);
35c95375
DV
16798 pll->on = false;
16799 }
16800
666a4537 16801 if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev))
6eb1a681
VS
16802 vlv_wm_get_hw_state(dev);
16803 else if (IS_GEN9(dev))
3078999f
PB
16804 skl_wm_get_hw_state(dev);
16805 else if (HAS_PCH_SPLIT(dev))
243e6a44 16806 ilk_wm_get_hw_state(dev);
292b990e
ML
16807
16808 for_each_intel_crtc(dev, crtc) {
16809 unsigned long put_domains;
16810
74bff5f9 16811 put_domains = modeset_get_crtc_power_domains(&crtc->base, crtc->config);
292b990e
ML
16812 if (WARN_ON(put_domains))
16813 modeset_put_power_domains(dev_priv, put_domains);
16814 }
16815 intel_display_set_init_power(dev_priv, false);
010cf73d
PZ
16816
16817 intel_fbc_init_pipe_state(dev_priv);
043e9bda 16818}
7d0bc1ea 16819
043e9bda
ML
16820void intel_display_resume(struct drm_device *dev)
16821{
e2c8b870
ML
16822 struct drm_i915_private *dev_priv = to_i915(dev);
16823 struct drm_atomic_state *state = dev_priv->modeset_restore_state;
16824 struct drm_modeset_acquire_ctx ctx;
043e9bda 16825 int ret;
f30da187 16826
e2c8b870 16827 dev_priv->modeset_restore_state = NULL;
73974893
ML
16828 if (state)
16829 state->acquire_ctx = &ctx;
043e9bda 16830
ea49c9ac
ML
16831 /*
16832 * This is a cludge because with real atomic modeset mode_config.mutex
16833 * won't be taken. Unfortunately some probed state like
16834 * audio_codec_enable is still protected by mode_config.mutex, so lock
16835 * it here for now.
16836 */
16837 mutex_lock(&dev->mode_config.mutex);
e2c8b870 16838 drm_modeset_acquire_init(&ctx, 0);
043e9bda 16839
73974893
ML
16840 while (1) {
16841 ret = drm_modeset_lock_all_ctx(dev, &ctx);
16842 if (ret != -EDEADLK)
16843 break;
043e9bda 16844
e2c8b870 16845 drm_modeset_backoff(&ctx);
e2c8b870 16846 }
043e9bda 16847
73974893
ML
16848 if (!ret)
16849 ret = __intel_display_resume(dev, state);
16850
e2c8b870
ML
16851 drm_modeset_drop_locks(&ctx);
16852 drm_modeset_acquire_fini(&ctx);
ea49c9ac 16853 mutex_unlock(&dev->mode_config.mutex);
043e9bda 16854
e2c8b870
ML
16855 if (ret) {
16856 DRM_ERROR("Restoring old state failed with %i\n", ret);
16857 drm_atomic_state_free(state);
16858 }
2c7111db
CW
16859}
16860
16861void intel_modeset_gem_init(struct drm_device *dev)
16862{
dc97997a 16863 struct drm_i915_private *dev_priv = to_i915(dev);
484b41dd 16864 struct drm_crtc *c;
2ff8fde1 16865 struct drm_i915_gem_object *obj;
484b41dd 16866
dc97997a 16867 intel_init_gt_powersave(dev_priv);
ae48434c 16868
1833b134 16869 intel_modeset_init_hw(dev);
02e792fb 16870
1ee8da6d 16871 intel_setup_overlay(dev_priv);
484b41dd
JB
16872
16873 /*
16874 * Make sure any fbs we allocated at startup are properly
16875 * pinned & fenced. When we do the allocation it's too early
16876 * for this.
16877 */
70e1e0ec 16878 for_each_crtc(dev, c) {
058d88c4
CW
16879 struct i915_vma *vma;
16880
2ff8fde1
MR
16881 obj = intel_fb_obj(c->primary->fb);
16882 if (obj == NULL)
484b41dd
JB
16883 continue;
16884
e0d6149b 16885 mutex_lock(&dev->struct_mutex);
058d88c4 16886 vma = intel_pin_and_fence_fb_obj(c->primary->fb,
3465c580 16887 c->primary->state->rotation);
e0d6149b 16888 mutex_unlock(&dev->struct_mutex);
058d88c4 16889 if (IS_ERR(vma)) {
484b41dd
JB
16890 DRM_ERROR("failed to pin boot fb on pipe %d\n",
16891 to_intel_crtc(c)->pipe);
66e514c1 16892 drm_framebuffer_unreference(c->primary->fb);
5a21b665 16893 c->primary->fb = NULL;
36750f28 16894 c->primary->crtc = c->primary->state->crtc = NULL;
5a21b665 16895 update_state_fb(c->primary);
36750f28 16896 c->state->plane_mask &= ~(1 << drm_plane_index(c->primary));
484b41dd
JB
16897 }
16898 }
1ebaa0b9
CW
16899}
16900
16901int intel_connector_register(struct drm_connector *connector)
16902{
16903 struct intel_connector *intel_connector = to_intel_connector(connector);
16904 int ret;
16905
16906 ret = intel_backlight_device_register(intel_connector);
16907 if (ret)
16908 goto err;
16909
16910 return 0;
0962c3c9 16911
1ebaa0b9
CW
16912err:
16913 return ret;
79e53945
JB
16914}
16915
c191eca1 16916void intel_connector_unregister(struct drm_connector *connector)
4932e2c3 16917{
e63d87c0 16918 struct intel_connector *intel_connector = to_intel_connector(connector);
4932e2c3 16919
e63d87c0 16920 intel_backlight_device_unregister(intel_connector);
4932e2c3 16921 intel_panel_destroy_backlight(connector);
4932e2c3
ID
16922}
16923
79e53945
JB
16924void intel_modeset_cleanup(struct drm_device *dev)
16925{
fac5e23e 16926 struct drm_i915_private *dev_priv = to_i915(dev);
652c393a 16927
dc97997a 16928 intel_disable_gt_powersave(dev_priv);
2eb5252e 16929
fd0c0642
DV
16930 /*
16931 * Interrupts and polling as the first thing to avoid creating havoc.
2eb5252e 16932 * Too much stuff here (turning of connectors, ...) would
fd0c0642
DV
16933 * experience fancy races otherwise.
16934 */
2aeb7d3a 16935 intel_irq_uninstall(dev_priv);
eb21b92b 16936
fd0c0642
DV
16937 /*
16938 * Due to the hpd irq storm handling the hotplug work can re-arm the
16939 * poll handlers. Hence disable polling after hpd handling is shut down.
16940 */
f87ea761 16941 drm_kms_helper_poll_fini(dev);
fd0c0642 16942
723bfd70
JB
16943 intel_unregister_dsm_handler();
16944
c937ab3e 16945 intel_fbc_global_disable(dev_priv);
69341a5e 16946
1630fe75
CW
16947 /* flush any delayed tasks or pending work */
16948 flush_scheduled_work();
16949
79e53945 16950 drm_mode_config_cleanup(dev);
4d7bb011 16951
1ee8da6d 16952 intel_cleanup_overlay(dev_priv);
ae48434c 16953
dc97997a 16954 intel_cleanup_gt_powersave(dev_priv);
f5949141
DV
16955
16956 intel_teardown_gmbus(dev);
79e53945
JB
16957}
16958
df0e9248
CW
16959void intel_connector_attach_encoder(struct intel_connector *connector,
16960 struct intel_encoder *encoder)
16961{
16962 connector->encoder = encoder;
16963 drm_mode_connector_attach_encoder(&connector->base,
16964 &encoder->base);
79e53945 16965}
28d52043
DA
16966
16967/*
16968 * set vga decode state - true == enable VGA decode
16969 */
16970int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
16971{
fac5e23e 16972 struct drm_i915_private *dev_priv = to_i915(dev);
a885b3cc 16973 unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
28d52043
DA
16974 u16 gmch_ctrl;
16975
75fa041d
CW
16976 if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
16977 DRM_ERROR("failed to read control word\n");
16978 return -EIO;
16979 }
16980
c0cc8a55
CW
16981 if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
16982 return 0;
16983
28d52043
DA
16984 if (state)
16985 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
16986 else
16987 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
75fa041d
CW
16988
16989 if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
16990 DRM_ERROR("failed to write control word\n");
16991 return -EIO;
16992 }
16993
28d52043
DA
16994 return 0;
16995}
c4a1d9e4 16996
c4a1d9e4 16997struct intel_display_error_state {
ff57f1b0
PZ
16998
16999 u32 power_well_driver;
17000
63b66e5b
CW
17001 int num_transcoders;
17002
c4a1d9e4
CW
17003 struct intel_cursor_error_state {
17004 u32 control;
17005 u32 position;
17006 u32 base;
17007 u32 size;
52331309 17008 } cursor[I915_MAX_PIPES];
c4a1d9e4
CW
17009
17010 struct intel_pipe_error_state {
ddf9c536 17011 bool power_domain_on;
c4a1d9e4 17012 u32 source;
f301b1e1 17013 u32 stat;
52331309 17014 } pipe[I915_MAX_PIPES];
c4a1d9e4
CW
17015
17016 struct intel_plane_error_state {
17017 u32 control;
17018 u32 stride;
17019 u32 size;
17020 u32 pos;
17021 u32 addr;
17022 u32 surface;
17023 u32 tile_offset;
52331309 17024 } plane[I915_MAX_PIPES];
63b66e5b
CW
17025
17026 struct intel_transcoder_error_state {
ddf9c536 17027 bool power_domain_on;
63b66e5b
CW
17028 enum transcoder cpu_transcoder;
17029
17030 u32 conf;
17031
17032 u32 htotal;
17033 u32 hblank;
17034 u32 hsync;
17035 u32 vtotal;
17036 u32 vblank;
17037 u32 vsync;
17038 } transcoder[4];
c4a1d9e4
CW
17039};
17040
17041struct intel_display_error_state *
c033666a 17042intel_display_capture_error_state(struct drm_i915_private *dev_priv)
c4a1d9e4 17043{
c4a1d9e4 17044 struct intel_display_error_state *error;
63b66e5b
CW
17045 int transcoders[] = {
17046 TRANSCODER_A,
17047 TRANSCODER_B,
17048 TRANSCODER_C,
17049 TRANSCODER_EDP,
17050 };
c4a1d9e4
CW
17051 int i;
17052
c033666a 17053 if (INTEL_INFO(dev_priv)->num_pipes == 0)
63b66e5b
CW
17054 return NULL;
17055
9d1cb914 17056 error = kzalloc(sizeof(*error), GFP_ATOMIC);
c4a1d9e4
CW
17057 if (error == NULL)
17058 return NULL;
17059
c033666a 17060 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
ff57f1b0
PZ
17061 error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
17062
055e393f 17063 for_each_pipe(dev_priv, i) {
ddf9c536 17064 error->pipe[i].power_domain_on =
f458ebbc
DV
17065 __intel_display_power_is_enabled(dev_priv,
17066 POWER_DOMAIN_PIPE(i));
ddf9c536 17067 if (!error->pipe[i].power_domain_on)
9d1cb914
PZ
17068 continue;
17069
5efb3e28
VS
17070 error->cursor[i].control = I915_READ(CURCNTR(i));
17071 error->cursor[i].position = I915_READ(CURPOS(i));
17072 error->cursor[i].base = I915_READ(CURBASE(i));
c4a1d9e4
CW
17073
17074 error->plane[i].control = I915_READ(DSPCNTR(i));
17075 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
c033666a 17076 if (INTEL_GEN(dev_priv) <= 3) {
51889b35 17077 error->plane[i].size = I915_READ(DSPSIZE(i));
80ca378b
PZ
17078 error->plane[i].pos = I915_READ(DSPPOS(i));
17079 }
c033666a 17080 if (INTEL_GEN(dev_priv) <= 7 && !IS_HASWELL(dev_priv))
ca291363 17081 error->plane[i].addr = I915_READ(DSPADDR(i));
c033666a 17082 if (INTEL_GEN(dev_priv) >= 4) {
c4a1d9e4
CW
17083 error->plane[i].surface = I915_READ(DSPSURF(i));
17084 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
17085 }
17086
c4a1d9e4 17087 error->pipe[i].source = I915_READ(PIPESRC(i));
f301b1e1 17088
c033666a 17089 if (HAS_GMCH_DISPLAY(dev_priv))
f301b1e1 17090 error->pipe[i].stat = I915_READ(PIPESTAT(i));
63b66e5b
CW
17091 }
17092
4d1de975 17093 /* Note: this does not include DSI transcoders. */
c033666a 17094 error->num_transcoders = INTEL_INFO(dev_priv)->num_pipes;
2d1fe073 17095 if (HAS_DDI(dev_priv))
63b66e5b
CW
17096 error->num_transcoders++; /* Account for eDP. */
17097
17098 for (i = 0; i < error->num_transcoders; i++) {
17099 enum transcoder cpu_transcoder = transcoders[i];
17100
ddf9c536 17101 error->transcoder[i].power_domain_on =
f458ebbc 17102 __intel_display_power_is_enabled(dev_priv,
38cc1daf 17103 POWER_DOMAIN_TRANSCODER(cpu_transcoder));
ddf9c536 17104 if (!error->transcoder[i].power_domain_on)
9d1cb914
PZ
17105 continue;
17106
63b66e5b
CW
17107 error->transcoder[i].cpu_transcoder = cpu_transcoder;
17108
17109 error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
17110 error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
17111 error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
17112 error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
17113 error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
17114 error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
17115 error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
c4a1d9e4
CW
17116 }
17117
17118 return error;
17119}
17120
edc3d884
MK
17121#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
17122
c4a1d9e4 17123void
edc3d884 17124intel_display_print_error_state(struct drm_i915_error_state_buf *m,
c4a1d9e4
CW
17125 struct drm_device *dev,
17126 struct intel_display_error_state *error)
17127{
fac5e23e 17128 struct drm_i915_private *dev_priv = to_i915(dev);
c4a1d9e4
CW
17129 int i;
17130
63b66e5b
CW
17131 if (!error)
17132 return;
17133
edc3d884 17134 err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
190be112 17135 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
edc3d884 17136 err_printf(m, "PWR_WELL_CTL2: %08x\n",
ff57f1b0 17137 error->power_well_driver);
055e393f 17138 for_each_pipe(dev_priv, i) {
edc3d884 17139 err_printf(m, "Pipe [%d]:\n", i);
ddf9c536 17140 err_printf(m, " Power: %s\n",
87ad3212 17141 onoff(error->pipe[i].power_domain_on));
edc3d884 17142 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
f301b1e1 17143 err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
edc3d884
MK
17144
17145 err_printf(m, "Plane [%d]:\n", i);
17146 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
17147 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
80ca378b 17148 if (INTEL_INFO(dev)->gen <= 3) {
edc3d884
MK
17149 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
17150 err_printf(m, " POS: %08x\n", error->plane[i].pos);
80ca378b 17151 }
4b71a570 17152 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
edc3d884 17153 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
c4a1d9e4 17154 if (INTEL_INFO(dev)->gen >= 4) {
edc3d884
MK
17155 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
17156 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
c4a1d9e4
CW
17157 }
17158
edc3d884
MK
17159 err_printf(m, "Cursor [%d]:\n", i);
17160 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
17161 err_printf(m, " POS: %08x\n", error->cursor[i].position);
17162 err_printf(m, " BASE: %08x\n", error->cursor[i].base);
c4a1d9e4 17163 }
63b66e5b
CW
17164
17165 for (i = 0; i < error->num_transcoders; i++) {
da205630 17166 err_printf(m, "CPU transcoder: %s\n",
63b66e5b 17167 transcoder_name(error->transcoder[i].cpu_transcoder));
ddf9c536 17168 err_printf(m, " Power: %s\n",
87ad3212 17169 onoff(error->transcoder[i].power_domain_on));
63b66e5b
CW
17170 err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
17171 err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
17172 err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
17173 err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
17174 err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
17175 err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
17176 err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
17177 }
c4a1d9e4 17178}