]> Git Repo - linux.git/blame - drivers/gpu/drm/i915/intel_display.c
drm/i915: Clean up rotation DSPCNTR/DVSCNTR/etc. setup
[linux.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 <[email protected]>
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"
db18b6a6 40#include "intel_dsi.h"
e5510fac 41#include "i915_trace.h"
319c1d42 42#include <drm/drm_atomic.h>
c196e1d6 43#include <drm/drm_atomic_helper.h>
760285e7
DH
44#include <drm/drm_dp_helper.h>
45#include <drm/drm_crtc_helper.h>
465c120c
MR
46#include <drm/drm_plane_helper.h>
47#include <drm/drm_rect.h>
c0f372b3 48#include <linux/dma_remapping.h>
fd8e058a 49#include <linux/reservation.h>
79e53945 50
5a21b665
SV
51static bool is_mmio_work(struct intel_flip_work *work)
52{
53 return work->mmio_work.func;
54}
55
465c120c 56/* Primary plane formats for gen <= 3 */
568db4f2 57static const uint32_t i8xx_primary_formats[] = {
67fe7dc5
DL
58 DRM_FORMAT_C8,
59 DRM_FORMAT_RGB565,
465c120c 60 DRM_FORMAT_XRGB1555,
67fe7dc5 61 DRM_FORMAT_XRGB8888,
465c120c
MR
62};
63
64/* Primary plane formats for gen >= 4 */
568db4f2 65static const uint32_t i965_primary_formats[] = {
6c0fd451
DL
66 DRM_FORMAT_C8,
67 DRM_FORMAT_RGB565,
68 DRM_FORMAT_XRGB8888,
69 DRM_FORMAT_XBGR8888,
70 DRM_FORMAT_XRGB2101010,
71 DRM_FORMAT_XBGR2101010,
72};
73
74static const uint32_t skl_primary_formats[] = {
67fe7dc5
DL
75 DRM_FORMAT_C8,
76 DRM_FORMAT_RGB565,
77 DRM_FORMAT_XRGB8888,
465c120c 78 DRM_FORMAT_XBGR8888,
67fe7dc5 79 DRM_FORMAT_ARGB8888,
465c120c
MR
80 DRM_FORMAT_ABGR8888,
81 DRM_FORMAT_XRGB2101010,
465c120c 82 DRM_FORMAT_XBGR2101010,
ea916ea0
KM
83 DRM_FORMAT_YUYV,
84 DRM_FORMAT_YVYU,
85 DRM_FORMAT_UYVY,
86 DRM_FORMAT_VYUY,
465c120c
MR
87};
88
3d7d6510
MR
89/* Cursor formats */
90static const uint32_t intel_cursor_formats[] = {
91 DRM_FORMAT_ARGB8888,
92};
93
f1f644dc 94static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 95 struct intel_crtc_state *pipe_config);
18442d08 96static void ironlake_pch_clock_get(struct intel_crtc *crtc,
5cec258b 97 struct intel_crtc_state *pipe_config);
f1f644dc 98
eb1bfe80
JB
99static int intel_framebuffer_init(struct drm_device *dev,
100 struct intel_framebuffer *ifb,
101 struct drm_mode_fb_cmd2 *mode_cmd,
102 struct drm_i915_gem_object *obj);
5b18e57c
SV
103static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc);
104static void intel_set_pipe_timings(struct intel_crtc *intel_crtc);
bc58be60 105static void intel_set_pipe_src_size(struct intel_crtc *intel_crtc);
29407aab 106static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
f769cd24
VK
107 struct intel_link_m_n *m_n,
108 struct intel_link_m_n *m2_n2);
29407aab 109static void ironlake_set_pipeconf(struct drm_crtc *crtc);
229fca97 110static void haswell_set_pipeconf(struct drm_crtc *crtc);
391bf048 111static void haswell_set_pipemisc(struct drm_crtc *crtc);
d288f65f 112static void vlv_prepare_pll(struct intel_crtc *crtc,
5cec258b 113 const struct intel_crtc_state *pipe_config);
d288f65f 114static void chv_prepare_pll(struct intel_crtc *crtc,
5cec258b 115 const struct intel_crtc_state *pipe_config);
5a21b665
SV
116static void intel_begin_crtc_commit(struct drm_crtc *, struct drm_crtc_state *);
117static void intel_finish_crtc_commit(struct drm_crtc *, struct drm_crtc_state *);
65edccce
VS
118static void skl_init_scalers(struct drm_i915_private *dev_priv,
119 struct intel_crtc *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
SV
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
SV
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
SV
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
e2d214ae 603static bool intel_PLL_is_valid(struct drm_i915_private *dev_priv,
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
e2d214ae
TU
616 if (!IS_PINEVIEW(dev_priv) && !IS_VALLEYVIEW(dev_priv) &&
617 !IS_CHERRYVIEW(dev_priv) && !IS_BROXTON(dev_priv))
f01b7962
VS
618 if (clock->m1 <= clock->m2)
619 INTELPllInvalid("m1 <= m2\n");
620
e2d214ae
TU
621 if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv) &&
622 !IS_BROXTON(dev_priv)) {
f01b7962
VS
623 if (clock->p < limit->p.min || limit->p.max < clock->p)
624 INTELPllInvalid("p out of range\n");
625 if (clock->m < limit->m.min || limit->m.max < clock->m)
626 INTELPllInvalid("m out of range\n");
627 }
628
79e53945 629 if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
0206e353 630 INTELPllInvalid("vco out of range\n");
79e53945
JB
631 /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
632 * connector, etc., rather than just a single range.
633 */
634 if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
0206e353 635 INTELPllInvalid("dot out of range\n");
79e53945
JB
636
637 return true;
638}
639
3b1429d9 640static int
1b6f4958 641i9xx_select_p2_div(const struct intel_limit *limit,
3b1429d9
VS
642 const struct intel_crtc_state *crtc_state,
643 int target)
79e53945 644{
3b1429d9 645 struct drm_device *dev = crtc_state->base.crtc->dev;
79e53945 646
2d84d2b3 647 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
79e53945 648 /*
a210b028
SV
649 * For LVDS just rely on its current settings for dual-channel.
650 * We haven't figured out how to reliably set up different
651 * single/dual channel state, if we even can.
79e53945 652 */
1974cad0 653 if (intel_is_dual_link_lvds(dev))
3b1429d9 654 return limit->p2.p2_fast;
79e53945 655 else
3b1429d9 656 return limit->p2.p2_slow;
79e53945
JB
657 } else {
658 if (target < limit->p2.dot_limit)
3b1429d9 659 return limit->p2.p2_slow;
79e53945 660 else
3b1429d9 661 return limit->p2.p2_fast;
79e53945 662 }
3b1429d9
VS
663}
664
70e8aa21
ACO
665/*
666 * Returns a set of divisors for the desired target clock with the given
667 * refclk, or FALSE. The returned values represent the clock equation:
668 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
669 *
670 * Target and reference clocks are specified in kHz.
671 *
672 * If match_clock is provided, then best_clock P divider must match the P
673 * divider from @match_clock used for LVDS downclocking.
674 */
3b1429d9 675static bool
1b6f4958 676i9xx_find_best_dpll(const struct intel_limit *limit,
3b1429d9 677 struct intel_crtc_state *crtc_state,
9e2c8475
ACO
678 int target, int refclk, struct dpll *match_clock,
679 struct dpll *best_clock)
3b1429d9
VS
680{
681 struct drm_device *dev = crtc_state->base.crtc->dev;
9e2c8475 682 struct dpll clock;
3b1429d9 683 int err = target;
79e53945 684
0206e353 685 memset(best_clock, 0, sizeof(*best_clock));
79e53945 686
3b1429d9
VS
687 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
688
42158660
ZY
689 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
690 clock.m1++) {
691 for (clock.m2 = limit->m2.min;
692 clock.m2 <= limit->m2.max; clock.m2++) {
c0efc387 693 if (clock.m2 >= clock.m1)
42158660
ZY
694 break;
695 for (clock.n = limit->n.min;
696 clock.n <= limit->n.max; clock.n++) {
697 for (clock.p1 = limit->p1.min;
698 clock.p1 <= limit->p1.max; clock.p1++) {
79e53945
JB
699 int this_err;
700
dccbea3b 701 i9xx_calc_dpll_params(refclk, &clock);
e2d214ae
TU
702 if (!intel_PLL_is_valid(to_i915(dev),
703 limit,
ac58c3f0
SV
704 &clock))
705 continue;
706 if (match_clock &&
707 clock.p != match_clock->p)
708 continue;
709
710 this_err = abs(clock.dot - target);
711 if (this_err < err) {
712 *best_clock = clock;
713 err = this_err;
714 }
715 }
716 }
717 }
718 }
719
720 return (err != target);
721}
722
70e8aa21
ACO
723/*
724 * Returns a set of divisors for the desired target clock with the given
725 * refclk, or FALSE. The returned values represent the clock equation:
726 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
727 *
728 * Target and reference clocks are specified in kHz.
729 *
730 * If match_clock is provided, then best_clock P divider must match the P
731 * divider from @match_clock used for LVDS downclocking.
732 */
ac58c3f0 733static bool
1b6f4958 734pnv_find_best_dpll(const struct intel_limit *limit,
a93e255f 735 struct intel_crtc_state *crtc_state,
9e2c8475
ACO
736 int target, int refclk, struct dpll *match_clock,
737 struct dpll *best_clock)
79e53945 738{
3b1429d9 739 struct drm_device *dev = crtc_state->base.crtc->dev;
9e2c8475 740 struct dpll clock;
79e53945
JB
741 int err = target;
742
0206e353 743 memset(best_clock, 0, sizeof(*best_clock));
79e53945 744
3b1429d9
VS
745 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
746
42158660
ZY
747 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
748 clock.m1++) {
749 for (clock.m2 = limit->m2.min;
750 clock.m2 <= limit->m2.max; clock.m2++) {
42158660
ZY
751 for (clock.n = limit->n.min;
752 clock.n <= limit->n.max; clock.n++) {
753 for (clock.p1 = limit->p1.min;
754 clock.p1 <= limit->p1.max; clock.p1++) {
79e53945
JB
755 int this_err;
756
dccbea3b 757 pnv_calc_dpll_params(refclk, &clock);
e2d214ae
TU
758 if (!intel_PLL_is_valid(to_i915(dev),
759 limit,
1b894b59 760 &clock))
79e53945 761 continue;
cec2f356
SP
762 if (match_clock &&
763 clock.p != match_clock->p)
764 continue;
79e53945
JB
765
766 this_err = abs(clock.dot - target);
767 if (this_err < err) {
768 *best_clock = clock;
769 err = this_err;
770 }
771 }
772 }
773 }
774 }
775
776 return (err != target);
777}
778
997c030c
ACO
779/*
780 * Returns a set of divisors for the desired target clock with the given
781 * refclk, or FALSE. The returned values represent the clock equation:
782 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
70e8aa21
ACO
783 *
784 * Target and reference clocks are specified in kHz.
785 *
786 * If match_clock is provided, then best_clock P divider must match the P
787 * divider from @match_clock used for LVDS downclocking.
997c030c 788 */
d4906093 789static bool
1b6f4958 790g4x_find_best_dpll(const struct intel_limit *limit,
a93e255f 791 struct intel_crtc_state *crtc_state,
9e2c8475
ACO
792 int target, int refclk, struct dpll *match_clock,
793 struct dpll *best_clock)
d4906093 794{
3b1429d9 795 struct drm_device *dev = crtc_state->base.crtc->dev;
9e2c8475 796 struct dpll clock;
d4906093 797 int max_n;
3b1429d9 798 bool found = false;
6ba770dc
AJ
799 /* approximately equals target * 0.00585 */
800 int err_most = (target >> 8) + (target >> 9);
d4906093
ML
801
802 memset(best_clock, 0, sizeof(*best_clock));
3b1429d9
VS
803
804 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
805
d4906093 806 max_n = limit->n.max;
f77f13e2 807 /* based on hardware requirement, prefer smaller n to precision */
d4906093 808 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
f77f13e2 809 /* based on hardware requirement, prefere larger m1,m2 */
d4906093
ML
810 for (clock.m1 = limit->m1.max;
811 clock.m1 >= limit->m1.min; clock.m1--) {
812 for (clock.m2 = limit->m2.max;
813 clock.m2 >= limit->m2.min; clock.m2--) {
814 for (clock.p1 = limit->p1.max;
815 clock.p1 >= limit->p1.min; clock.p1--) {
816 int this_err;
817
dccbea3b 818 i9xx_calc_dpll_params(refclk, &clock);
e2d214ae
TU
819 if (!intel_PLL_is_valid(to_i915(dev),
820 limit,
1b894b59 821 &clock))
d4906093 822 continue;
1b894b59
CW
823
824 this_err = abs(clock.dot - target);
d4906093
ML
825 if (this_err < err_most) {
826 *best_clock = clock;
827 err_most = this_err;
828 max_n = clock.n;
829 found = true;
830 }
831 }
832 }
833 }
834 }
2c07245f
ZW
835 return found;
836}
837
d5dd62bd
ID
838/*
839 * Check if the calculated PLL configuration is more optimal compared to the
840 * best configuration and error found so far. Return the calculated error.
841 */
842static bool vlv_PLL_is_optimal(struct drm_device *dev, int target_freq,
9e2c8475
ACO
843 const struct dpll *calculated_clock,
844 const struct dpll *best_clock,
d5dd62bd
ID
845 unsigned int best_error_ppm,
846 unsigned int *error_ppm)
847{
9ca3ba01
ID
848 /*
849 * For CHV ignore the error and consider only the P value.
850 * Prefer a bigger P value based on HW requirements.
851 */
920a14b2 852 if (IS_CHERRYVIEW(to_i915(dev))) {
9ca3ba01
ID
853 *error_ppm = 0;
854
855 return calculated_clock->p > best_clock->p;
856 }
857
24be4e46
ID
858 if (WARN_ON_ONCE(!target_freq))
859 return false;
860
d5dd62bd
ID
861 *error_ppm = div_u64(1000000ULL *
862 abs(target_freq - calculated_clock->dot),
863 target_freq);
864 /*
865 * Prefer a better P value over a better (smaller) error if the error
866 * is small. Ensure this preference for future configurations too by
867 * setting the error to 0.
868 */
869 if (*error_ppm < 100 && calculated_clock->p > best_clock->p) {
870 *error_ppm = 0;
871
872 return true;
873 }
874
875 return *error_ppm + 10 < best_error_ppm;
876}
877
65b3d6a9
ACO
878/*
879 * Returns a set of divisors for the desired target clock with the given
880 * refclk, or FALSE. The returned values represent the clock equation:
881 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
882 */
a0c4da24 883static bool
1b6f4958 884vlv_find_best_dpll(const struct intel_limit *limit,
a93e255f 885 struct intel_crtc_state *crtc_state,
9e2c8475
ACO
886 int target, int refclk, struct dpll *match_clock,
887 struct dpll *best_clock)
a0c4da24 888{
a93e255f 889 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
a919ff14 890 struct drm_device *dev = crtc->base.dev;
9e2c8475 891 struct dpll clock;
69e4f900 892 unsigned int bestppm = 1000000;
27e639bf
VS
893 /* min update 19.2 MHz */
894 int max_n = min(limit->n.max, refclk / 19200);
49e497ef 895 bool found = false;
a0c4da24 896
6b4bf1c4
VS
897 target *= 5; /* fast clock */
898
899 memset(best_clock, 0, sizeof(*best_clock));
a0c4da24
JB
900
901 /* based on hardware requirement, prefer smaller n to precision */
27e639bf 902 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
811bbf05 903 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
889059d8 904 for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
c1a9ae43 905 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
6b4bf1c4 906 clock.p = clock.p1 * clock.p2;
a0c4da24 907 /* based on hardware requirement, prefer bigger m1,m2 values */
6b4bf1c4 908 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
d5dd62bd 909 unsigned int ppm;
69e4f900 910
6b4bf1c4
VS
911 clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
912 refclk * clock.m1);
913
dccbea3b 914 vlv_calc_dpll_params(refclk, &clock);
43b0ac53 915
e2d214ae
TU
916 if (!intel_PLL_is_valid(to_i915(dev),
917 limit,
f01b7962 918 &clock))
43b0ac53
VS
919 continue;
920
d5dd62bd
ID
921 if (!vlv_PLL_is_optimal(dev, target,
922 &clock,
923 best_clock,
924 bestppm, &ppm))
925 continue;
6b4bf1c4 926
d5dd62bd
ID
927 *best_clock = clock;
928 bestppm = ppm;
929 found = true;
a0c4da24
JB
930 }
931 }
932 }
933 }
a0c4da24 934
49e497ef 935 return found;
a0c4da24 936}
a4fc5ed6 937
65b3d6a9
ACO
938/*
939 * Returns a set of divisors for the desired target clock with the given
940 * refclk, or FALSE. The returned values represent the clock equation:
941 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
942 */
ef9348c8 943static bool
1b6f4958 944chv_find_best_dpll(const struct intel_limit *limit,
a93e255f 945 struct intel_crtc_state *crtc_state,
9e2c8475
ACO
946 int target, int refclk, struct dpll *match_clock,
947 struct dpll *best_clock)
ef9348c8 948{
a93e255f 949 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
a919ff14 950 struct drm_device *dev = crtc->base.dev;
9ca3ba01 951 unsigned int best_error_ppm;
9e2c8475 952 struct dpll clock;
ef9348c8
CML
953 uint64_t m2;
954 int found = false;
955
956 memset(best_clock, 0, sizeof(*best_clock));
9ca3ba01 957 best_error_ppm = 1000000;
ef9348c8
CML
958
959 /*
960 * Based on hardware doc, the n always set to 1, and m1 always
961 * set to 2. If requires to support 200Mhz refclk, we need to
962 * revisit this because n may not 1 anymore.
963 */
964 clock.n = 1, clock.m1 = 2;
965 target *= 5; /* fast clock */
966
967 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
968 for (clock.p2 = limit->p2.p2_fast;
969 clock.p2 >= limit->p2.p2_slow;
970 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
9ca3ba01 971 unsigned int error_ppm;
ef9348c8
CML
972
973 clock.p = clock.p1 * clock.p2;
974
975 m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
976 clock.n) << 22, refclk * clock.m1);
977
978 if (m2 > INT_MAX/clock.m1)
979 continue;
980
981 clock.m2 = m2;
982
dccbea3b 983 chv_calc_dpll_params(refclk, &clock);
ef9348c8 984
e2d214ae 985 if (!intel_PLL_is_valid(to_i915(dev), limit, &clock))
ef9348c8
CML
986 continue;
987
9ca3ba01
ID
988 if (!vlv_PLL_is_optimal(dev, target, &clock, best_clock,
989 best_error_ppm, &error_ppm))
990 continue;
991
992 *best_clock = clock;
993 best_error_ppm = error_ppm;
994 found = true;
ef9348c8
CML
995 }
996 }
997
998 return found;
999}
1000
5ab7b0b7 1001bool bxt_find_best_dpll(struct intel_crtc_state *crtc_state, int target_clock,
9e2c8475 1002 struct dpll *best_clock)
5ab7b0b7 1003{
65b3d6a9 1004 int refclk = 100000;
1b6f4958 1005 const struct intel_limit *limit = &intel_limits_bxt;
5ab7b0b7 1006
65b3d6a9 1007 return chv_find_best_dpll(limit, crtc_state,
5ab7b0b7
ID
1008 target_clock, refclk, NULL, best_clock);
1009}
1010
525b9311 1011bool intel_crtc_active(struct intel_crtc *crtc)
20ddf665 1012{
20ddf665
VS
1013 /* Be paranoid as we can arrive here with only partial
1014 * state retrieved from the hardware during setup.
1015 *
241bfc38 1016 * We can ditch the adjusted_mode.crtc_clock check as soon
20ddf665
VS
1017 * as Haswell has gained clock readout/fastboot support.
1018 *
66e514c1 1019 * We can ditch the crtc->primary->fb check as soon as we can
20ddf665 1020 * properly reconstruct framebuffers.
c3d1f436
MR
1021 *
1022 * FIXME: The intel_crtc->active here should be switched to
1023 * crtc->state->active once we have proper CRTC states wired up
1024 * for atomic.
20ddf665 1025 */
525b9311
VS
1026 return crtc->active && crtc->base.primary->state->fb &&
1027 crtc->config->base.adjusted_mode.crtc_clock;
20ddf665
VS
1028}
1029
a5c961d1
PZ
1030enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
1031 enum pipe pipe)
1032{
98187836 1033 struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
a5c961d1 1034
e2af48c6 1035 return crtc->config->cpu_transcoder;
a5c961d1
PZ
1036}
1037
fbf49ea2
VS
1038static bool pipe_dsl_stopped(struct drm_device *dev, enum pipe pipe)
1039{
fac5e23e 1040 struct drm_i915_private *dev_priv = to_i915(dev);
f0f59a00 1041 i915_reg_t reg = PIPEDSL(pipe);
fbf49ea2
VS
1042 u32 line1, line2;
1043 u32 line_mask;
1044
5db94019 1045 if (IS_GEN2(dev_priv))
fbf49ea2
VS
1046 line_mask = DSL_LINEMASK_GEN2;
1047 else
1048 line_mask = DSL_LINEMASK_GEN3;
1049
1050 line1 = I915_READ(reg) & line_mask;
6adfb1ef 1051 msleep(5);
fbf49ea2
VS
1052 line2 = I915_READ(reg) & line_mask;
1053
1054 return line1 == line2;
1055}
1056
ab7ad7f6
KP
1057/*
1058 * intel_wait_for_pipe_off - wait for pipe to turn off
575f7ab7 1059 * @crtc: crtc whose pipe to wait for
9d0498a2
JB
1060 *
1061 * After disabling a pipe, we can't wait for vblank in the usual way,
1062 * spinning on the vblank interrupt status bit, since we won't actually
1063 * see an interrupt when the pipe is disabled.
1064 *
ab7ad7f6
KP
1065 * On Gen4 and above:
1066 * wait for the pipe register state bit to turn off
1067 *
1068 * Otherwise:
1069 * wait for the display line value to settle (it usually
1070 * ends up stopping at the start of the next frame).
58e10eb9 1071 *
9d0498a2 1072 */
575f7ab7 1073static void intel_wait_for_pipe_off(struct intel_crtc *crtc)
9d0498a2 1074{
575f7ab7 1075 struct drm_device *dev = crtc->base.dev;
fac5e23e 1076 struct drm_i915_private *dev_priv = to_i915(dev);
6e3c9717 1077 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
575f7ab7 1078 enum pipe pipe = crtc->pipe;
ab7ad7f6
KP
1079
1080 if (INTEL_INFO(dev)->gen >= 4) {
f0f59a00 1081 i915_reg_t reg = PIPECONF(cpu_transcoder);
ab7ad7f6
KP
1082
1083 /* Wait for the Pipe State to go off */
b8511f53
CW
1084 if (intel_wait_for_register(dev_priv,
1085 reg, I965_PIPECONF_ACTIVE, 0,
1086 100))
284637d9 1087 WARN(1, "pipe_off wait timed out\n");
ab7ad7f6 1088 } else {
ab7ad7f6 1089 /* Wait for the display line to settle */
fbf49ea2 1090 if (wait_for(pipe_dsl_stopped(dev, pipe), 100))
284637d9 1091 WARN(1, "pipe_off wait timed out\n");
ab7ad7f6 1092 }
79e53945
JB
1093}
1094
b24e7179 1095/* Only for pre-ILK configs */
55607e8a
SV
1096void assert_pll(struct drm_i915_private *dev_priv,
1097 enum pipe pipe, bool state)
b24e7179 1098{
b24e7179
JB
1099 u32 val;
1100 bool cur_state;
1101
649636ef 1102 val = I915_READ(DPLL(pipe));
b24e7179 1103 cur_state = !!(val & DPLL_VCO_ENABLE);
e2c719b7 1104 I915_STATE_WARN(cur_state != state,
b24e7179 1105 "PLL state assertion failure (expected %s, current %s)\n",
87ad3212 1106 onoff(state), onoff(cur_state));
b24e7179 1107}
b24e7179 1108
23538ef1 1109/* XXX: the dsi pll is shared between MIPI DSI ports */
8563b1e8 1110void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
23538ef1
JN
1111{
1112 u32 val;
1113 bool cur_state;
1114
a580516d 1115 mutex_lock(&dev_priv->sb_lock);
23538ef1 1116 val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
a580516d 1117 mutex_unlock(&dev_priv->sb_lock);
23538ef1
JN
1118
1119 cur_state = val & DSI_PLL_VCO_EN;
e2c719b7 1120 I915_STATE_WARN(cur_state != state,
23538ef1 1121 "DSI PLL state assertion failure (expected %s, current %s)\n",
87ad3212 1122 onoff(state), onoff(cur_state));
23538ef1 1123}
23538ef1 1124
040484af
JB
1125static void assert_fdi_tx(struct drm_i915_private *dev_priv,
1126 enum pipe pipe, bool state)
1127{
040484af 1128 bool cur_state;
ad80a810
PZ
1129 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1130 pipe);
040484af 1131
2d1fe073 1132 if (HAS_DDI(dev_priv)) {
affa9354 1133 /* DDI does not have a specific FDI_TX register */
649636ef 1134 u32 val = I915_READ(TRANS_DDI_FUNC_CTL(cpu_transcoder));
ad80a810 1135 cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
bf507ef7 1136 } else {
649636ef 1137 u32 val = I915_READ(FDI_TX_CTL(pipe));
bf507ef7
ED
1138 cur_state = !!(val & FDI_TX_ENABLE);
1139 }
e2c719b7 1140 I915_STATE_WARN(cur_state != state,
040484af 1141 "FDI TX state assertion failure (expected %s, current %s)\n",
87ad3212 1142 onoff(state), onoff(cur_state));
040484af
JB
1143}
1144#define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
1145#define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
1146
1147static void assert_fdi_rx(struct drm_i915_private *dev_priv,
1148 enum pipe pipe, bool state)
1149{
040484af
JB
1150 u32 val;
1151 bool cur_state;
1152
649636ef 1153 val = I915_READ(FDI_RX_CTL(pipe));
d63fa0dc 1154 cur_state = !!(val & FDI_RX_ENABLE);
e2c719b7 1155 I915_STATE_WARN(cur_state != state,
040484af 1156 "FDI RX state assertion failure (expected %s, current %s)\n",
87ad3212 1157 onoff(state), onoff(cur_state));
040484af
JB
1158}
1159#define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
1160#define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
1161
1162static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
1163 enum pipe pipe)
1164{
040484af
JB
1165 u32 val;
1166
1167 /* ILK FDI PLL is always enabled */
7e22dbbb 1168 if (IS_GEN5(dev_priv))
040484af
JB
1169 return;
1170
bf507ef7 1171 /* On Haswell, DDI ports are responsible for the FDI PLL setup */
2d1fe073 1172 if (HAS_DDI(dev_priv))
bf507ef7
ED
1173 return;
1174
649636ef 1175 val = I915_READ(FDI_TX_CTL(pipe));
e2c719b7 1176 I915_STATE_WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
040484af
JB
1177}
1178
55607e8a
SV
1179void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
1180 enum pipe pipe, bool state)
040484af 1181{
040484af 1182 u32 val;
55607e8a 1183 bool cur_state;
040484af 1184
649636ef 1185 val = I915_READ(FDI_RX_CTL(pipe));
55607e8a 1186 cur_state = !!(val & FDI_RX_PLL_ENABLE);
e2c719b7 1187 I915_STATE_WARN(cur_state != state,
55607e8a 1188 "FDI RX PLL assertion failure (expected %s, current %s)\n",
87ad3212 1189 onoff(state), onoff(cur_state));
040484af
JB
1190}
1191
4f8036a2 1192void assert_panel_unlocked(struct drm_i915_private *dev_priv, enum pipe pipe)
ea0760cf 1193{
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
4f8036a2 1199 if (WARN_ON(HAS_DDI(dev_priv)))
bedd4dba
JN
1200 return;
1201
4f8036a2 1202 if (HAS_PCH_SPLIT(dev_priv)) {
bedd4dba
JN
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 */
4f8036a2 1212 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
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{
93ce0ba6
JN
1235 bool cur_state;
1236
50a0bc90 1237 if (IS_845G(dev_priv) || IS_I865G(dev_priv))
0b87c24e 1238 cur_state = I915_READ(CURCNTR(PIPE_A)) & CURSOR_ENABLE;
d9d82081 1239 else
5efb3e28 1240 cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
93ce0ba6 1241
e2c719b7 1242 I915_STATE_WARN(cur_state != state,
93ce0ba6 1243 "cursor on pipe %c assertion failure (expected %s, current %s)\n",
87ad3212 1244 pipe_name(pipe), onoff(state), onoff(cur_state));
93ce0ba6
JN
1245}
1246#define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
1247#define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
1248
b840d907
JB
1249void assert_pipe(struct drm_i915_private *dev_priv,
1250 enum pipe pipe, bool state)
b24e7179 1251{
63d7bbe9 1252 bool cur_state;
702e7a56
PZ
1253 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1254 pipe);
4feed0eb 1255 enum intel_display_power_domain power_domain;
b24e7179 1256
b6b5d049
VS
1257 /* if we need the pipe quirk it must be always on */
1258 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1259 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
8e636784
SV
1260 state = true;
1261
4feed0eb
ID
1262 power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
1263 if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
649636ef 1264 u32 val = I915_READ(PIPECONF(cpu_transcoder));
69310161 1265 cur_state = !!(val & PIPECONF_ENABLE);
4feed0eb
ID
1266
1267 intel_display_power_put(dev_priv, power_domain);
1268 } else {
1269 cur_state = false;
69310161
PZ
1270 }
1271
e2c719b7 1272 I915_STATE_WARN(cur_state != state,
63d7bbe9 1273 "pipe %c assertion failure (expected %s, current %s)\n",
87ad3212 1274 pipe_name(pipe), onoff(state), onoff(cur_state));
b24e7179
JB
1275}
1276
931872fc
CW
1277static void assert_plane(struct drm_i915_private *dev_priv,
1278 enum plane plane, bool state)
b24e7179 1279{
b24e7179 1280 u32 val;
931872fc 1281 bool cur_state;
b24e7179 1282
649636ef 1283 val = I915_READ(DSPCNTR(plane));
931872fc 1284 cur_state = !!(val & DISPLAY_PLANE_ENABLE);
e2c719b7 1285 I915_STATE_WARN(cur_state != state,
931872fc 1286 "plane %c assertion failure (expected %s, current %s)\n",
87ad3212 1287 plane_name(plane), onoff(state), onoff(cur_state));
b24e7179
JB
1288}
1289
931872fc
CW
1290#define assert_plane_enabled(d, p) assert_plane(d, p, true)
1291#define assert_plane_disabled(d, p) assert_plane(d, p, false)
1292
b24e7179
JB
1293static void assert_planes_disabled(struct drm_i915_private *dev_priv,
1294 enum pipe pipe)
1295{
91c8a326 1296 struct drm_device *dev = &dev_priv->drm;
649636ef 1297 int i;
b24e7179 1298
653e1026
VS
1299 /* Primary planes are fixed to pipes on gen4+ */
1300 if (INTEL_INFO(dev)->gen >= 4) {
649636ef 1301 u32 val = I915_READ(DSPCNTR(pipe));
e2c719b7 1302 I915_STATE_WARN(val & DISPLAY_PLANE_ENABLE,
28c05794
AJ
1303 "plane %c assertion failure, should be disabled but not\n",
1304 plane_name(pipe));
19ec1358 1305 return;
28c05794 1306 }
19ec1358 1307
b24e7179 1308 /* Need to check both planes against the pipe */
055e393f 1309 for_each_pipe(dev_priv, i) {
649636ef
VS
1310 u32 val = I915_READ(DSPCNTR(i));
1311 enum pipe cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
b24e7179 1312 DISPPLANE_SEL_PIPE_SHIFT;
e2c719b7 1313 I915_STATE_WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
9db4a9c7
JB
1314 "plane %c assertion failure, should be off on pipe %c but is still active\n",
1315 plane_name(i), pipe_name(pipe));
b24e7179
JB
1316 }
1317}
1318
19332d7a
JB
1319static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
1320 enum pipe pipe)
1321{
91c8a326 1322 struct drm_device *dev = &dev_priv->drm;
649636ef 1323 int sprite;
19332d7a 1324
7feb8b88 1325 if (INTEL_INFO(dev)->gen >= 9) {
3bdcfc0c 1326 for_each_sprite(dev_priv, pipe, sprite) {
649636ef 1327 u32 val = I915_READ(PLANE_CTL(pipe, sprite));
e2c719b7 1328 I915_STATE_WARN(val & PLANE_CTL_ENABLE,
7feb8b88
DL
1329 "plane %d assertion failure, should be off on pipe %c but is still active\n",
1330 sprite, pipe_name(pipe));
1331 }
920a14b2 1332 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
3bdcfc0c 1333 for_each_sprite(dev_priv, pipe, sprite) {
649636ef 1334 u32 val = I915_READ(SPCNTR(pipe, sprite));
e2c719b7 1335 I915_STATE_WARN(val & SP_ENABLE,
20674eef 1336 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
1fe47785 1337 sprite_name(pipe, sprite), pipe_name(pipe));
20674eef
VS
1338 }
1339 } else if (INTEL_INFO(dev)->gen >= 7) {
649636ef 1340 u32 val = I915_READ(SPRCTL(pipe));
e2c719b7 1341 I915_STATE_WARN(val & SPRITE_ENABLE,
06da8da2 1342 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
20674eef
VS
1343 plane_name(pipe), pipe_name(pipe));
1344 } else if (INTEL_INFO(dev)->gen >= 5) {
649636ef 1345 u32 val = I915_READ(DVSCNTR(pipe));
e2c719b7 1346 I915_STATE_WARN(val & DVS_ENABLE,
06da8da2 1347 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
20674eef 1348 plane_name(pipe), pipe_name(pipe));
19332d7a
JB
1349 }
1350}
1351
08c71e5e
VS
1352static void assert_vblank_disabled(struct drm_crtc *crtc)
1353{
e2c719b7 1354 if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
08c71e5e
VS
1355 drm_crtc_vblank_put(crtc);
1356}
1357
7abd4b35
ACO
1358void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
1359 enum pipe pipe)
92f2584a 1360{
92f2584a
JB
1361 u32 val;
1362 bool enabled;
1363
649636ef 1364 val = I915_READ(PCH_TRANSCONF(pipe));
92f2584a 1365 enabled = !!(val & TRANS_ENABLE);
e2c719b7 1366 I915_STATE_WARN(enabled,
9db4a9c7
JB
1367 "transcoder assertion failed, should be off on pipe %c but is still active\n",
1368 pipe_name(pipe));
92f2584a
JB
1369}
1370
4e634389
KP
1371static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
1372 enum pipe pipe, u32 port_sel, u32 val)
f0575e92
KP
1373{
1374 if ((val & DP_PORT_EN) == 0)
1375 return false;
1376
2d1fe073 1377 if (HAS_PCH_CPT(dev_priv)) {
f0f59a00 1378 u32 trans_dp_ctl = I915_READ(TRANS_DP_CTL(pipe));
f0575e92
KP
1379 if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
1380 return false;
2d1fe073 1381 } else if (IS_CHERRYVIEW(dev_priv)) {
44f37d1f
CML
1382 if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
1383 return false;
f0575e92
KP
1384 } else {
1385 if ((val & DP_PIPE_MASK) != (pipe << 30))
1386 return false;
1387 }
1388 return true;
1389}
1390
1519b995
KP
1391static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
1392 enum pipe pipe, u32 val)
1393{
dc0fa718 1394 if ((val & SDVO_ENABLE) == 0)
1519b995
KP
1395 return false;
1396
2d1fe073 1397 if (HAS_PCH_CPT(dev_priv)) {
dc0fa718 1398 if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
1519b995 1399 return false;
2d1fe073 1400 } else if (IS_CHERRYVIEW(dev_priv)) {
44f37d1f
CML
1401 if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
1402 return false;
1519b995 1403 } else {
dc0fa718 1404 if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
1519b995
KP
1405 return false;
1406 }
1407 return true;
1408}
1409
1410static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
1411 enum pipe pipe, u32 val)
1412{
1413 if ((val & LVDS_PORT_EN) == 0)
1414 return false;
1415
2d1fe073 1416 if (HAS_PCH_CPT(dev_priv)) {
1519b995
KP
1417 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1418 return false;
1419 } else {
1420 if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
1421 return false;
1422 }
1423 return true;
1424}
1425
1426static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
1427 enum pipe pipe, u32 val)
1428{
1429 if ((val & ADPA_DAC_ENABLE) == 0)
1430 return false;
2d1fe073 1431 if (HAS_PCH_CPT(dev_priv)) {
1519b995
KP
1432 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1433 return false;
1434 } else {
1435 if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
1436 return false;
1437 }
1438 return true;
1439}
1440
291906f1 1441static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
f0f59a00
VS
1442 enum pipe pipe, i915_reg_t reg,
1443 u32 port_sel)
291906f1 1444{
47a05eca 1445 u32 val = I915_READ(reg);
e2c719b7 1446 I915_STATE_WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
291906f1 1447 "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
f0f59a00 1448 i915_mmio_reg_offset(reg), pipe_name(pipe));
de9a35ab 1449
2d1fe073 1450 I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && (val & DP_PORT_EN) == 0
75c5da27 1451 && (val & DP_PIPEB_SELECT),
de9a35ab 1452 "IBX PCH dp port still using transcoder B\n");
291906f1
JB
1453}
1454
1455static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
f0f59a00 1456 enum pipe pipe, i915_reg_t reg)
291906f1 1457{
47a05eca 1458 u32 val = I915_READ(reg);
e2c719b7 1459 I915_STATE_WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
23c99e77 1460 "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
f0f59a00 1461 i915_mmio_reg_offset(reg), pipe_name(pipe));
de9a35ab 1462
2d1fe073 1463 I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && (val & SDVO_ENABLE) == 0
75c5da27 1464 && (val & SDVO_PIPE_B_SELECT),
de9a35ab 1465 "IBX PCH hdmi port still using transcoder B\n");
291906f1
JB
1466}
1467
1468static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
1469 enum pipe pipe)
1470{
291906f1 1471 u32 val;
291906f1 1472
f0575e92
KP
1473 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
1474 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
1475 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
291906f1 1476
649636ef 1477 val = I915_READ(PCH_ADPA);
e2c719b7 1478 I915_STATE_WARN(adpa_pipe_enabled(dev_priv, pipe, val),
291906f1 1479 "PCH VGA enabled on transcoder %c, should be disabled\n",
9db4a9c7 1480 pipe_name(pipe));
291906f1 1481
649636ef 1482 val = I915_READ(PCH_LVDS);
e2c719b7 1483 I915_STATE_WARN(lvds_pipe_enabled(dev_priv, pipe, val),
291906f1 1484 "PCH LVDS enabled on transcoder %c, should be disabled\n",
9db4a9c7 1485 pipe_name(pipe));
291906f1 1486
e2debe91
PZ
1487 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
1488 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
1489 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
291906f1
JB
1490}
1491
cd2d34d9
VS
1492static void _vlv_enable_pll(struct intel_crtc *crtc,
1493 const struct intel_crtc_state *pipe_config)
1494{
1495 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1496 enum pipe pipe = crtc->pipe;
1497
1498 I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
1499 POSTING_READ(DPLL(pipe));
1500 udelay(150);
1501
2c30b43b
CW
1502 if (intel_wait_for_register(dev_priv,
1503 DPLL(pipe),
1504 DPLL_LOCK_VLV,
1505 DPLL_LOCK_VLV,
1506 1))
cd2d34d9
VS
1507 DRM_ERROR("DPLL %d failed to lock\n", pipe);
1508}
1509
d288f65f 1510static void vlv_enable_pll(struct intel_crtc *crtc,
5cec258b 1511 const struct intel_crtc_state *pipe_config)
87442f73 1512{
cd2d34d9 1513 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
8bd3f301 1514 enum pipe pipe = crtc->pipe;
87442f73 1515
8bd3f301 1516 assert_pipe_disabled(dev_priv, pipe);
87442f73 1517
87442f73 1518 /* PLL is protected by panel, make sure we can write it */
7d1a83cb 1519 assert_panel_unlocked(dev_priv, pipe);
87442f73 1520
cd2d34d9
VS
1521 if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
1522 _vlv_enable_pll(crtc, pipe_config);
426115cf 1523
8bd3f301
VS
1524 I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
1525 POSTING_READ(DPLL_MD(pipe));
87442f73
SV
1526}
1527
cd2d34d9
VS
1528
1529static void _chv_enable_pll(struct intel_crtc *crtc,
1530 const struct intel_crtc_state *pipe_config)
9d556c99 1531{
cd2d34d9 1532 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
8bd3f301 1533 enum pipe pipe = crtc->pipe;
9d556c99 1534 enum dpio_channel port = vlv_pipe_to_channel(pipe);
9d556c99
CML
1535 u32 tmp;
1536
a580516d 1537 mutex_lock(&dev_priv->sb_lock);
9d556c99
CML
1538
1539 /* Enable back the 10bit clock to display controller */
1540 tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1541 tmp |= DPIO_DCLKP_EN;
1542 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
1543
54433e91
VS
1544 mutex_unlock(&dev_priv->sb_lock);
1545
9d556c99
CML
1546 /*
1547 * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
1548 */
1549 udelay(1);
1550
1551 /* Enable PLL */
d288f65f 1552 I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
9d556c99
CML
1553
1554 /* Check PLL is locked */
6b18826a
CW
1555 if (intel_wait_for_register(dev_priv,
1556 DPLL(pipe), DPLL_LOCK_VLV, DPLL_LOCK_VLV,
1557 1))
9d556c99 1558 DRM_ERROR("PLL %d failed to lock\n", pipe);
cd2d34d9
VS
1559}
1560
1561static void chv_enable_pll(struct intel_crtc *crtc,
1562 const struct intel_crtc_state *pipe_config)
1563{
1564 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1565 enum pipe pipe = crtc->pipe;
1566
1567 assert_pipe_disabled(dev_priv, pipe);
1568
1569 /* PLL is protected by panel, make sure we can write it */
1570 assert_panel_unlocked(dev_priv, pipe);
1571
1572 if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
1573 _chv_enable_pll(crtc, pipe_config);
9d556c99 1574
c231775c
VS
1575 if (pipe != PIPE_A) {
1576 /*
1577 * WaPixelRepeatModeFixForC0:chv
1578 *
1579 * DPLLCMD is AWOL. Use chicken bits to propagate
1580 * the value from DPLLBMD to either pipe B or C.
1581 */
1582 I915_WRITE(CBR4_VLV, pipe == PIPE_B ? CBR_DPLLBMD_PIPE_B : CBR_DPLLBMD_PIPE_C);
1583 I915_WRITE(DPLL_MD(PIPE_B), pipe_config->dpll_hw_state.dpll_md);
1584 I915_WRITE(CBR4_VLV, 0);
1585 dev_priv->chv_dpll_md[pipe] = pipe_config->dpll_hw_state.dpll_md;
1586
1587 /*
1588 * DPLLB VGA mode also seems to cause problems.
1589 * We should always have it disabled.
1590 */
1591 WARN_ON((I915_READ(DPLL(PIPE_B)) & DPLL_VGA_MODE_DIS) == 0);
1592 } else {
1593 I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
1594 POSTING_READ(DPLL_MD(pipe));
1595 }
9d556c99
CML
1596}
1597
1c4e0274
VS
1598static int intel_num_dvo_pipes(struct drm_device *dev)
1599{
1600 struct intel_crtc *crtc;
1601 int count = 0;
1602
2d84d2b3 1603 for_each_intel_crtc(dev, crtc) {
3538b9df 1604 count += crtc->base.state->active &&
2d84d2b3
VS
1605 intel_crtc_has_type(crtc->config, INTEL_OUTPUT_DVO);
1606 }
1c4e0274
VS
1607
1608 return count;
1609}
1610
66e3d5c0 1611static void i9xx_enable_pll(struct intel_crtc *crtc)
63d7bbe9 1612{
66e3d5c0 1613 struct drm_device *dev = crtc->base.dev;
fac5e23e 1614 struct drm_i915_private *dev_priv = to_i915(dev);
f0f59a00 1615 i915_reg_t reg = DPLL(crtc->pipe);
6e3c9717 1616 u32 dpll = crtc->config->dpll_hw_state.dpll;
63d7bbe9 1617
66e3d5c0 1618 assert_pipe_disabled(dev_priv, crtc->pipe);
58c6eaa2 1619
63d7bbe9 1620 /* PLL is protected by panel, make sure we can write it */
50a0bc90 1621 if (IS_MOBILE(dev_priv) && !IS_I830(dev_priv))
66e3d5c0 1622 assert_panel_unlocked(dev_priv, crtc->pipe);
63d7bbe9 1623
1c4e0274 1624 /* Enable DVO 2x clock on both PLLs if necessary */
50a0bc90 1625 if (IS_I830(dev_priv) && intel_num_dvo_pipes(dev) > 0) {
1c4e0274
VS
1626 /*
1627 * It appears to be important that we don't enable this
1628 * for the current pipe before otherwise configuring the
1629 * PLL. No idea how this should be handled if multiple
1630 * DVO outputs are enabled simultaneosly.
1631 */
1632 dpll |= DPLL_DVO_2X_MODE;
1633 I915_WRITE(DPLL(!crtc->pipe),
1634 I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
1635 }
66e3d5c0 1636
c2b63374
VS
1637 /*
1638 * Apparently we need to have VGA mode enabled prior to changing
1639 * the P1/P2 dividers. Otherwise the DPLL will keep using the old
1640 * dividers, even though the register value does change.
1641 */
1642 I915_WRITE(reg, 0);
1643
8e7a65aa
VS
1644 I915_WRITE(reg, dpll);
1645
66e3d5c0
SV
1646 /* Wait for the clocks to stabilize. */
1647 POSTING_READ(reg);
1648 udelay(150);
1649
1650 if (INTEL_INFO(dev)->gen >= 4) {
1651 I915_WRITE(DPLL_MD(crtc->pipe),
6e3c9717 1652 crtc->config->dpll_hw_state.dpll_md);
66e3d5c0
SV
1653 } else {
1654 /* The pixel multiplier can only be updated once the
1655 * DPLL is enabled and the clocks are stable.
1656 *
1657 * So write it again.
1658 */
1659 I915_WRITE(reg, dpll);
1660 }
63d7bbe9
JB
1661
1662 /* We do this three times for luck */
66e3d5c0 1663 I915_WRITE(reg, dpll);
63d7bbe9
JB
1664 POSTING_READ(reg);
1665 udelay(150); /* wait for warmup */
66e3d5c0 1666 I915_WRITE(reg, dpll);
63d7bbe9
JB
1667 POSTING_READ(reg);
1668 udelay(150); /* wait for warmup */
66e3d5c0 1669 I915_WRITE(reg, dpll);
63d7bbe9
JB
1670 POSTING_READ(reg);
1671 udelay(150); /* wait for warmup */
1672}
1673
1674/**
50b44a44 1675 * i9xx_disable_pll - disable a PLL
63d7bbe9
JB
1676 * @dev_priv: i915 private structure
1677 * @pipe: pipe PLL to disable
1678 *
1679 * Disable the PLL for @pipe, making sure the pipe is off first.
1680 *
1681 * Note! This is for pre-ILK only.
1682 */
1c4e0274 1683static void i9xx_disable_pll(struct intel_crtc *crtc)
63d7bbe9 1684{
1c4e0274 1685 struct drm_device *dev = crtc->base.dev;
fac5e23e 1686 struct drm_i915_private *dev_priv = to_i915(dev);
1c4e0274
VS
1687 enum pipe pipe = crtc->pipe;
1688
1689 /* Disable DVO 2x clock on both PLLs if necessary */
50a0bc90 1690 if (IS_I830(dev_priv) &&
2d84d2b3 1691 intel_crtc_has_type(crtc->config, INTEL_OUTPUT_DVO) &&
3538b9df 1692 !intel_num_dvo_pipes(dev)) {
1c4e0274
VS
1693 I915_WRITE(DPLL(PIPE_B),
1694 I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
1695 I915_WRITE(DPLL(PIPE_A),
1696 I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
1697 }
1698
b6b5d049
VS
1699 /* Don't disable pipe or pipe PLLs if needed */
1700 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1701 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
63d7bbe9
JB
1702 return;
1703
1704 /* Make sure the pipe isn't still relying on us */
1705 assert_pipe_disabled(dev_priv, pipe);
1706
b8afb911 1707 I915_WRITE(DPLL(pipe), DPLL_VGA_MODE_DIS);
50b44a44 1708 POSTING_READ(DPLL(pipe));
63d7bbe9
JB
1709}
1710
f6071166
JB
1711static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1712{
b8afb911 1713 u32 val;
f6071166
JB
1714
1715 /* Make sure the pipe isn't still relying on us */
1716 assert_pipe_disabled(dev_priv, pipe);
1717
03ed5cbf
VS
1718 val = DPLL_INTEGRATED_REF_CLK_VLV |
1719 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
1720 if (pipe != PIPE_A)
1721 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
1722
f6071166
JB
1723 I915_WRITE(DPLL(pipe), val);
1724 POSTING_READ(DPLL(pipe));
076ed3b2
CML
1725}
1726
1727static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1728{
d752048d 1729 enum dpio_channel port = vlv_pipe_to_channel(pipe);
076ed3b2
CML
1730 u32 val;
1731
a11b0703
VS
1732 /* Make sure the pipe isn't still relying on us */
1733 assert_pipe_disabled(dev_priv, pipe);
076ed3b2 1734
60bfe44f
VS
1735 val = DPLL_SSC_REF_CLK_CHV |
1736 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
a11b0703
VS
1737 if (pipe != PIPE_A)
1738 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
03ed5cbf 1739
a11b0703
VS
1740 I915_WRITE(DPLL(pipe), val);
1741 POSTING_READ(DPLL(pipe));
d752048d 1742
a580516d 1743 mutex_lock(&dev_priv->sb_lock);
d752048d
VS
1744
1745 /* Disable 10bit clock to display controller */
1746 val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1747 val &= ~DPIO_DCLKP_EN;
1748 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
1749
a580516d 1750 mutex_unlock(&dev_priv->sb_lock);
f6071166
JB
1751}
1752
e4607fcf 1753void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
9b6de0a1
VS
1754 struct intel_digital_port *dport,
1755 unsigned int expected_mask)
89b667f8
JB
1756{
1757 u32 port_mask;
f0f59a00 1758 i915_reg_t dpll_reg;
89b667f8 1759
e4607fcf
CML
1760 switch (dport->port) {
1761 case PORT_B:
89b667f8 1762 port_mask = DPLL_PORTB_READY_MASK;
00fc31b7 1763 dpll_reg = DPLL(0);
e4607fcf
CML
1764 break;
1765 case PORT_C:
89b667f8 1766 port_mask = DPLL_PORTC_READY_MASK;
00fc31b7 1767 dpll_reg = DPLL(0);
9b6de0a1 1768 expected_mask <<= 4;
00fc31b7
CML
1769 break;
1770 case PORT_D:
1771 port_mask = DPLL_PORTD_READY_MASK;
1772 dpll_reg = DPIO_PHY_STATUS;
e4607fcf
CML
1773 break;
1774 default:
1775 BUG();
1776 }
89b667f8 1777
370004d3
CW
1778 if (intel_wait_for_register(dev_priv,
1779 dpll_reg, port_mask, expected_mask,
1780 1000))
9b6de0a1
VS
1781 WARN(1, "timed out waiting for port %c ready: got 0x%x, expected 0x%x\n",
1782 port_name(dport->port), I915_READ(dpll_reg) & port_mask, expected_mask);
89b667f8
JB
1783}
1784
b8a4f404
PZ
1785static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1786 enum pipe pipe)
040484af 1787{
98187836
VS
1788 struct intel_crtc *intel_crtc = intel_get_crtc_for_pipe(dev_priv,
1789 pipe);
f0f59a00
VS
1790 i915_reg_t reg;
1791 uint32_t val, pipeconf_val;
040484af 1792
040484af 1793 /* Make sure PCH DPLL is enabled */
8106ddbd 1794 assert_shared_dpll_enabled(dev_priv, intel_crtc->config->shared_dpll);
040484af
JB
1795
1796 /* FDI must be feeding us bits for PCH ports */
1797 assert_fdi_tx_enabled(dev_priv, pipe);
1798 assert_fdi_rx_enabled(dev_priv, pipe);
1799
6e266956 1800 if (HAS_PCH_CPT(dev_priv)) {
23670b32
SV
1801 /* Workaround: Set the timing override bit before enabling the
1802 * pch transcoder. */
1803 reg = TRANS_CHICKEN2(pipe);
1804 val = I915_READ(reg);
1805 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
1806 I915_WRITE(reg, val);
59c859d6 1807 }
23670b32 1808
ab9412ba 1809 reg = PCH_TRANSCONF(pipe);
040484af 1810 val = I915_READ(reg);
5f7f726d 1811 pipeconf_val = I915_READ(PIPECONF(pipe));
e9bcff5c 1812
2d1fe073 1813 if (HAS_PCH_IBX(dev_priv)) {
e9bcff5c 1814 /*
c5de7c6f
VS
1815 * Make the BPC in transcoder be consistent with
1816 * that in pipeconf reg. For HDMI we must use 8bpc
1817 * here for both 8bpc and 12bpc.
e9bcff5c 1818 */
dfd07d72 1819 val &= ~PIPECONF_BPC_MASK;
2d84d2b3 1820 if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_HDMI))
c5de7c6f
VS
1821 val |= PIPECONF_8BPC;
1822 else
1823 val |= pipeconf_val & PIPECONF_BPC_MASK;
e9bcff5c 1824 }
5f7f726d
PZ
1825
1826 val &= ~TRANS_INTERLACE_MASK;
1827 if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
2d1fe073 1828 if (HAS_PCH_IBX(dev_priv) &&
2d84d2b3 1829 intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
7c26e5c6
PZ
1830 val |= TRANS_LEGACY_INTERLACED_ILK;
1831 else
1832 val |= TRANS_INTERLACED;
5f7f726d
PZ
1833 else
1834 val |= TRANS_PROGRESSIVE;
1835
040484af 1836 I915_WRITE(reg, val | TRANS_ENABLE);
650fbd84
CW
1837 if (intel_wait_for_register(dev_priv,
1838 reg, TRANS_STATE_ENABLE, TRANS_STATE_ENABLE,
1839 100))
4bb6f1f3 1840 DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
040484af
JB
1841}
1842
8fb033d7 1843static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
937bb610 1844 enum transcoder cpu_transcoder)
040484af 1845{
8fb033d7 1846 u32 val, pipeconf_val;
8fb033d7 1847
8fb033d7 1848 /* FDI must be feeding us bits for PCH ports */
1a240d4d 1849 assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
937bb610 1850 assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
8fb033d7 1851
223a6fdf 1852 /* Workaround: set timing override bit. */
36c0d0cf 1853 val = I915_READ(TRANS_CHICKEN2(PIPE_A));
23670b32 1854 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
36c0d0cf 1855 I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
223a6fdf 1856
25f3ef11 1857 val = TRANS_ENABLE;
937bb610 1858 pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
8fb033d7 1859
9a76b1c6
PZ
1860 if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
1861 PIPECONF_INTERLACED_ILK)
a35f2679 1862 val |= TRANS_INTERLACED;
8fb033d7
PZ
1863 else
1864 val |= TRANS_PROGRESSIVE;
1865
ab9412ba 1866 I915_WRITE(LPT_TRANSCONF, val);
d9f96244
CW
1867 if (intel_wait_for_register(dev_priv,
1868 LPT_TRANSCONF,
1869 TRANS_STATE_ENABLE,
1870 TRANS_STATE_ENABLE,
1871 100))
937bb610 1872 DRM_ERROR("Failed to enable PCH transcoder\n");
8fb033d7
PZ
1873}
1874
b8a4f404
PZ
1875static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
1876 enum pipe pipe)
040484af 1877{
f0f59a00
VS
1878 i915_reg_t reg;
1879 uint32_t val;
040484af
JB
1880
1881 /* FDI relies on the transcoder */
1882 assert_fdi_tx_disabled(dev_priv, pipe);
1883 assert_fdi_rx_disabled(dev_priv, pipe);
1884
291906f1
JB
1885 /* Ports must be off as well */
1886 assert_pch_ports_disabled(dev_priv, pipe);
1887
ab9412ba 1888 reg = PCH_TRANSCONF(pipe);
040484af
JB
1889 val = I915_READ(reg);
1890 val &= ~TRANS_ENABLE;
1891 I915_WRITE(reg, val);
1892 /* wait for PCH transcoder off, transcoder state */
a7d04662
CW
1893 if (intel_wait_for_register(dev_priv,
1894 reg, TRANS_STATE_ENABLE, 0,
1895 50))
4bb6f1f3 1896 DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
23670b32 1897
6e266956 1898 if (HAS_PCH_CPT(dev_priv)) {
23670b32
SV
1899 /* Workaround: Clear the timing override chicken bit again. */
1900 reg = TRANS_CHICKEN2(pipe);
1901 val = I915_READ(reg);
1902 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
1903 I915_WRITE(reg, val);
1904 }
040484af
JB
1905}
1906
b7076546 1907void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
8fb033d7 1908{
8fb033d7
PZ
1909 u32 val;
1910
ab9412ba 1911 val = I915_READ(LPT_TRANSCONF);
8fb033d7 1912 val &= ~TRANS_ENABLE;
ab9412ba 1913 I915_WRITE(LPT_TRANSCONF, val);
8fb033d7 1914 /* wait for PCH transcoder off, transcoder state */
dfdb4749
CW
1915 if (intel_wait_for_register(dev_priv,
1916 LPT_TRANSCONF, TRANS_STATE_ENABLE, 0,
1917 50))
8a52fd9f 1918 DRM_ERROR("Failed to disable PCH transcoder\n");
223a6fdf
PZ
1919
1920 /* Workaround: clear timing override bit. */
36c0d0cf 1921 val = I915_READ(TRANS_CHICKEN2(PIPE_A));
23670b32 1922 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
36c0d0cf 1923 I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
040484af
JB
1924}
1925
65f2130c
VS
1926enum transcoder intel_crtc_pch_transcoder(struct intel_crtc *crtc)
1927{
1928 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1929
1930 WARN_ON(!crtc->config->has_pch_encoder);
1931
1932 if (HAS_PCH_LPT(dev_priv))
1933 return TRANSCODER_A;
1934 else
1935 return (enum transcoder) crtc->pipe;
1936}
1937
b24e7179 1938/**
309cfea8 1939 * intel_enable_pipe - enable a pipe, asserting requirements
0372264a 1940 * @crtc: crtc responsible for the pipe
b24e7179 1941 *
0372264a 1942 * Enable @crtc's pipe, making sure that various hardware specific requirements
b24e7179 1943 * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
b24e7179 1944 */
e1fdc473 1945static void intel_enable_pipe(struct intel_crtc *crtc)
b24e7179 1946{
0372264a 1947 struct drm_device *dev = crtc->base.dev;
fac5e23e 1948 struct drm_i915_private *dev_priv = to_i915(dev);
0372264a 1949 enum pipe pipe = crtc->pipe;
1a70a728 1950 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
f0f59a00 1951 i915_reg_t reg;
b24e7179
JB
1952 u32 val;
1953
9e2ee2dd
VS
1954 DRM_DEBUG_KMS("enabling pipe %c\n", pipe_name(pipe));
1955
58c6eaa2 1956 assert_planes_disabled(dev_priv, pipe);
93ce0ba6 1957 assert_cursor_disabled(dev_priv, pipe);
58c6eaa2
SV
1958 assert_sprites_disabled(dev_priv, pipe);
1959
b24e7179
JB
1960 /*
1961 * A pipe without a PLL won't actually be able to drive bits from
1962 * a plane. On ILK+ the pipe PLLs are integrated, so we don't
1963 * need the check.
1964 */
09fa8bb9 1965 if (HAS_GMCH_DISPLAY(dev_priv)) {
d7edc4e5 1966 if (intel_crtc_has_type(crtc->config, INTEL_OUTPUT_DSI))
23538ef1
JN
1967 assert_dsi_pll_enabled(dev_priv);
1968 else
1969 assert_pll_enabled(dev_priv, pipe);
09fa8bb9 1970 } else {
6e3c9717 1971 if (crtc->config->has_pch_encoder) {
040484af 1972 /* if driving the PCH, we need FDI enabled */
65f2130c
VS
1973 assert_fdi_rx_pll_enabled(dev_priv,
1974 (enum pipe) intel_crtc_pch_transcoder(crtc));
1a240d4d
SV
1975 assert_fdi_tx_pll_enabled(dev_priv,
1976 (enum pipe) cpu_transcoder);
040484af
JB
1977 }
1978 /* FIXME: assert CPU port conditions for SNB+ */
1979 }
b24e7179 1980
702e7a56 1981 reg = PIPECONF(cpu_transcoder);
b24e7179 1982 val = I915_READ(reg);
7ad25d48 1983 if (val & PIPECONF_ENABLE) {
b6b5d049
VS
1984 WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1985 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
00d70b15 1986 return;
7ad25d48 1987 }
00d70b15
CW
1988
1989 I915_WRITE(reg, val | PIPECONF_ENABLE);
851855d8 1990 POSTING_READ(reg);
b7792d8b
VS
1991
1992 /*
1993 * Until the pipe starts DSL will read as 0, which would cause
1994 * an apparent vblank timestamp jump, which messes up also the
1995 * frame count when it's derived from the timestamps. So let's
1996 * wait for the pipe to start properly before we call
1997 * drm_crtc_vblank_on()
1998 */
1999 if (dev->max_vblank_count == 0 &&
2000 wait_for(intel_get_crtc_scanline(crtc) != crtc->scanline_offset, 50))
2001 DRM_ERROR("pipe %c didn't start\n", pipe_name(pipe));
b24e7179
JB
2002}
2003
2004/**
309cfea8 2005 * intel_disable_pipe - disable a pipe, asserting requirements
575f7ab7 2006 * @crtc: crtc whose pipes is to be disabled
b24e7179 2007 *
575f7ab7
VS
2008 * Disable the pipe of @crtc, making sure that various hardware
2009 * specific requirements are met, if applicable, e.g. plane
2010 * disabled, panel fitter off, etc.
b24e7179
JB
2011 *
2012 * Will wait until the pipe has shut down before returning.
2013 */
575f7ab7 2014static void intel_disable_pipe(struct intel_crtc *crtc)
b24e7179 2015{
fac5e23e 2016 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6e3c9717 2017 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
575f7ab7 2018 enum pipe pipe = crtc->pipe;
f0f59a00 2019 i915_reg_t reg;
b24e7179
JB
2020 u32 val;
2021
9e2ee2dd
VS
2022 DRM_DEBUG_KMS("disabling pipe %c\n", pipe_name(pipe));
2023
b24e7179
JB
2024 /*
2025 * Make sure planes won't keep trying to pump pixels to us,
2026 * or we might hang the display.
2027 */
2028 assert_planes_disabled(dev_priv, pipe);
93ce0ba6 2029 assert_cursor_disabled(dev_priv, pipe);
19332d7a 2030 assert_sprites_disabled(dev_priv, pipe);
b24e7179 2031
702e7a56 2032 reg = PIPECONF(cpu_transcoder);
b24e7179 2033 val = I915_READ(reg);
00d70b15
CW
2034 if ((val & PIPECONF_ENABLE) == 0)
2035 return;
2036
67adc644
VS
2037 /*
2038 * Double wide has implications for planes
2039 * so best keep it disabled when not needed.
2040 */
6e3c9717 2041 if (crtc->config->double_wide)
67adc644
VS
2042 val &= ~PIPECONF_DOUBLE_WIDE;
2043
2044 /* Don't disable pipe or pipe PLLs if needed */
b6b5d049
VS
2045 if (!(pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) &&
2046 !(pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
67adc644
VS
2047 val &= ~PIPECONF_ENABLE;
2048
2049 I915_WRITE(reg, val);
2050 if ((val & PIPECONF_ENABLE) == 0)
2051 intel_wait_for_pipe_off(crtc);
b24e7179
JB
2052}
2053
832be82f
VS
2054static unsigned int intel_tile_size(const struct drm_i915_private *dev_priv)
2055{
2056 return IS_GEN2(dev_priv) ? 2048 : 4096;
2057}
2058
27ba3910
VS
2059static unsigned int intel_tile_width_bytes(const struct drm_i915_private *dev_priv,
2060 uint64_t fb_modifier, unsigned int cpp)
7b49f948
VS
2061{
2062 switch (fb_modifier) {
2063 case DRM_FORMAT_MOD_NONE:
2064 return cpp;
2065 case I915_FORMAT_MOD_X_TILED:
2066 if (IS_GEN2(dev_priv))
2067 return 128;
2068 else
2069 return 512;
2070 case I915_FORMAT_MOD_Y_TILED:
2071 if (IS_GEN2(dev_priv) || HAS_128_BYTE_Y_TILING(dev_priv))
2072 return 128;
2073 else
2074 return 512;
2075 case I915_FORMAT_MOD_Yf_TILED:
2076 switch (cpp) {
2077 case 1:
2078 return 64;
2079 case 2:
2080 case 4:
2081 return 128;
2082 case 8:
2083 case 16:
2084 return 256;
2085 default:
2086 MISSING_CASE(cpp);
2087 return cpp;
2088 }
2089 break;
2090 default:
2091 MISSING_CASE(fb_modifier);
2092 return cpp;
2093 }
2094}
2095
832be82f
VS
2096unsigned int intel_tile_height(const struct drm_i915_private *dev_priv,
2097 uint64_t fb_modifier, unsigned int cpp)
a57ce0b2 2098{
832be82f
VS
2099 if (fb_modifier == DRM_FORMAT_MOD_NONE)
2100 return 1;
2101 else
2102 return intel_tile_size(dev_priv) /
27ba3910 2103 intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
6761dd31
TU
2104}
2105
8d0deca8
VS
2106/* Return the tile dimensions in pixel units */
2107static void intel_tile_dims(const struct drm_i915_private *dev_priv,
2108 unsigned int *tile_width,
2109 unsigned int *tile_height,
2110 uint64_t fb_modifier,
2111 unsigned int cpp)
2112{
2113 unsigned int tile_width_bytes =
2114 intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
2115
2116 *tile_width = tile_width_bytes / cpp;
2117 *tile_height = intel_tile_size(dev_priv) / tile_width_bytes;
2118}
2119
6761dd31
TU
2120unsigned int
2121intel_fb_align_height(struct drm_device *dev, unsigned int height,
832be82f 2122 uint32_t pixel_format, uint64_t fb_modifier)
6761dd31 2123{
832be82f
VS
2124 unsigned int cpp = drm_format_plane_cpp(pixel_format, 0);
2125 unsigned int tile_height = intel_tile_height(to_i915(dev), fb_modifier, cpp);
2126
2127 return ALIGN(height, tile_height);
a57ce0b2
JB
2128}
2129
1663b9d6
VS
2130unsigned int intel_rotation_info_size(const struct intel_rotation_info *rot_info)
2131{
2132 unsigned int size = 0;
2133 int i;
2134
2135 for (i = 0 ; i < ARRAY_SIZE(rot_info->plane); i++)
2136 size += rot_info->plane[i].width * rot_info->plane[i].height;
2137
2138 return size;
2139}
2140
75c82a53 2141static void
3465c580
VS
2142intel_fill_fb_ggtt_view(struct i915_ggtt_view *view,
2143 const struct drm_framebuffer *fb,
2144 unsigned int rotation)
f64b98cd 2145{
bd2ef25d 2146 if (drm_rotation_90_or_270(rotation)) {
2d7a215f
VS
2147 *view = i915_ggtt_view_rotated;
2148 view->params.rotated = to_intel_framebuffer(fb)->rot_info;
2149 } else {
2150 *view = i915_ggtt_view_normal;
2151 }
2152}
50470bb0 2153
603525d7 2154static unsigned int intel_linear_alignment(const struct drm_i915_private *dev_priv)
4e9a86b6
VS
2155{
2156 if (INTEL_INFO(dev_priv)->gen >= 9)
2157 return 256 * 1024;
985b8bb4 2158 else if (IS_BROADWATER(dev_priv) || IS_CRESTLINE(dev_priv) ||
666a4537 2159 IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
4e9a86b6
VS
2160 return 128 * 1024;
2161 else if (INTEL_INFO(dev_priv)->gen >= 4)
2162 return 4 * 1024;
2163 else
44c5905e 2164 return 0;
4e9a86b6
VS
2165}
2166
603525d7
VS
2167static unsigned int intel_surf_alignment(const struct drm_i915_private *dev_priv,
2168 uint64_t fb_modifier)
2169{
2170 switch (fb_modifier) {
2171 case DRM_FORMAT_MOD_NONE:
2172 return intel_linear_alignment(dev_priv);
2173 case I915_FORMAT_MOD_X_TILED:
2174 if (INTEL_INFO(dev_priv)->gen >= 9)
2175 return 256 * 1024;
2176 return 0;
2177 case I915_FORMAT_MOD_Y_TILED:
2178 case I915_FORMAT_MOD_Yf_TILED:
2179 return 1 * 1024 * 1024;
2180 default:
2181 MISSING_CASE(fb_modifier);
2182 return 0;
2183 }
2184}
2185
058d88c4
CW
2186struct i915_vma *
2187intel_pin_and_fence_fb_obj(struct drm_framebuffer *fb, unsigned int rotation)
6b95a207 2188{
850c4cdc 2189 struct drm_device *dev = fb->dev;
fac5e23e 2190 struct drm_i915_private *dev_priv = to_i915(dev);
850c4cdc 2191 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
f64b98cd 2192 struct i915_ggtt_view view;
058d88c4 2193 struct i915_vma *vma;
6b95a207 2194 u32 alignment;
6b95a207 2195
ebcdd39e
MR
2196 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
2197
603525d7 2198 alignment = intel_surf_alignment(dev_priv, fb->modifier[0]);
6b95a207 2199
3465c580 2200 intel_fill_fb_ggtt_view(&view, fb, rotation);
f64b98cd 2201
693db184
CW
2202 /* Note that the w/a also requires 64 PTE of padding following the
2203 * bo. We currently fill all unused PTE with the shadow page and so
2204 * we should always have valid PTE following the scanout preventing
2205 * the VT-d warning.
2206 */
48f112fe 2207 if (intel_scanout_needs_vtd_wa(dev_priv) && alignment < 256 * 1024)
693db184
CW
2208 alignment = 256 * 1024;
2209
d6dd6843
PZ
2210 /*
2211 * Global gtt pte registers are special registers which actually forward
2212 * writes to a chunk of system memory. Which means that there is no risk
2213 * that the register values disappear as soon as we call
2214 * intel_runtime_pm_put(), so it is correct to wrap only the
2215 * pin/unpin/fence and not more.
2216 */
2217 intel_runtime_pm_get(dev_priv);
2218
058d88c4 2219 vma = i915_gem_object_pin_to_display_plane(obj, alignment, &view);
49ef5294
CW
2220 if (IS_ERR(vma))
2221 goto err;
6b95a207 2222
05a20d09 2223 if (i915_vma_is_map_and_fenceable(vma)) {
49ef5294
CW
2224 /* Install a fence for tiled scan-out. Pre-i965 always needs a
2225 * fence, whereas 965+ only requires a fence if using
2226 * framebuffer compression. For simplicity, we always, when
2227 * possible, install a fence as the cost is not that onerous.
2228 *
2229 * If we fail to fence the tiled scanout, then either the
2230 * modeset will reject the change (which is highly unlikely as
2231 * the affected systems, all but one, do not have unmappable
2232 * space) or we will not be able to enable full powersaving
2233 * techniques (also likely not to apply due to various limits
2234 * FBC and the like impose on the size of the buffer, which
2235 * presumably we violated anyway with this unmappable buffer).
2236 * Anyway, it is presumably better to stumble onwards with
2237 * something and try to run the system in a "less than optimal"
2238 * mode that matches the user configuration.
2239 */
2240 if (i915_vma_get_fence(vma) == 0)
2241 i915_vma_pin_fence(vma);
9807216f 2242 }
6b95a207 2243
49ef5294 2244err:
d6dd6843 2245 intel_runtime_pm_put(dev_priv);
058d88c4 2246 return vma;
6b95a207
KH
2247}
2248
fb4b8ce1 2249void intel_unpin_fb_obj(struct drm_framebuffer *fb, unsigned int rotation)
1690e1eb 2250{
82bc3b2d 2251 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
f64b98cd 2252 struct i915_ggtt_view view;
058d88c4 2253 struct i915_vma *vma;
82bc3b2d 2254
ebcdd39e
MR
2255 WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
2256
3465c580 2257 intel_fill_fb_ggtt_view(&view, fb, rotation);
05a20d09 2258 vma = i915_gem_object_to_ggtt(obj, &view);
f64b98cd 2259
49ef5294 2260 i915_vma_unpin_fence(vma);
058d88c4 2261 i915_gem_object_unpin_from_display_plane(vma);
1690e1eb
CW
2262}
2263
ef78ec94
VS
2264static int intel_fb_pitch(const struct drm_framebuffer *fb, int plane,
2265 unsigned int rotation)
2266{
bd2ef25d 2267 if (drm_rotation_90_or_270(rotation))
ef78ec94
VS
2268 return to_intel_framebuffer(fb)->rotated[plane].pitch;
2269 else
2270 return fb->pitches[plane];
2271}
2272
6687c906
VS
2273/*
2274 * Convert the x/y offsets into a linear offset.
2275 * Only valid with 0/180 degree rotation, which is fine since linear
2276 * offset is only used with linear buffers on pre-hsw and tiled buffers
2277 * with gen2/3, and 90/270 degree rotations isn't supported on any of them.
2278 */
2279u32 intel_fb_xy_to_linear(int x, int y,
2949056c
VS
2280 const struct intel_plane_state *state,
2281 int plane)
6687c906 2282{
2949056c 2283 const struct drm_framebuffer *fb = state->base.fb;
6687c906
VS
2284 unsigned int cpp = drm_format_plane_cpp(fb->pixel_format, plane);
2285 unsigned int pitch = fb->pitches[plane];
2286
2287 return y * pitch + x * cpp;
2288}
2289
2290/*
2291 * Add the x/y offsets derived from fb->offsets[] to the user
2292 * specified plane src x/y offsets. The resulting x/y offsets
2293 * specify the start of scanout from the beginning of the gtt mapping.
2294 */
2295void intel_add_fb_offsets(int *x, int *y,
2949056c
VS
2296 const struct intel_plane_state *state,
2297 int plane)
6687c906
VS
2298
2299{
2949056c
VS
2300 const struct intel_framebuffer *intel_fb = to_intel_framebuffer(state->base.fb);
2301 unsigned int rotation = state->base.rotation;
6687c906 2302
bd2ef25d 2303 if (drm_rotation_90_or_270(rotation)) {
6687c906
VS
2304 *x += intel_fb->rotated[plane].x;
2305 *y += intel_fb->rotated[plane].y;
2306 } else {
2307 *x += intel_fb->normal[plane].x;
2308 *y += intel_fb->normal[plane].y;
2309 }
2310}
2311
29cf9491 2312/*
29cf9491
VS
2313 * Input tile dimensions and pitch must already be
2314 * rotated to match x and y, and in pixel units.
2315 */
66a2d927
VS
2316static u32 _intel_adjust_tile_offset(int *x, int *y,
2317 unsigned int tile_width,
2318 unsigned int tile_height,
2319 unsigned int tile_size,
2320 unsigned int pitch_tiles,
2321 u32 old_offset,
2322 u32 new_offset)
29cf9491 2323{
b9b24038 2324 unsigned int pitch_pixels = pitch_tiles * tile_width;
29cf9491
VS
2325 unsigned int tiles;
2326
2327 WARN_ON(old_offset & (tile_size - 1));
2328 WARN_ON(new_offset & (tile_size - 1));
2329 WARN_ON(new_offset > old_offset);
2330
2331 tiles = (old_offset - new_offset) / tile_size;
2332
2333 *y += tiles / pitch_tiles * tile_height;
2334 *x += tiles % pitch_tiles * tile_width;
2335
b9b24038
VS
2336 /* minimize x in case it got needlessly big */
2337 *y += *x / pitch_pixels * tile_height;
2338 *x %= pitch_pixels;
2339
29cf9491
VS
2340 return new_offset;
2341}
2342
66a2d927
VS
2343/*
2344 * Adjust the tile offset by moving the difference into
2345 * the x/y offsets.
2346 */
2347static u32 intel_adjust_tile_offset(int *x, int *y,
2348 const struct intel_plane_state *state, int plane,
2349 u32 old_offset, u32 new_offset)
2350{
2351 const struct drm_i915_private *dev_priv = to_i915(state->base.plane->dev);
2352 const struct drm_framebuffer *fb = state->base.fb;
2353 unsigned int cpp = drm_format_plane_cpp(fb->pixel_format, plane);
2354 unsigned int rotation = state->base.rotation;
2355 unsigned int pitch = intel_fb_pitch(fb, plane, rotation);
2356
2357 WARN_ON(new_offset > old_offset);
2358
2359 if (fb->modifier[plane] != DRM_FORMAT_MOD_NONE) {
2360 unsigned int tile_size, tile_width, tile_height;
2361 unsigned int pitch_tiles;
2362
2363 tile_size = intel_tile_size(dev_priv);
2364 intel_tile_dims(dev_priv, &tile_width, &tile_height,
2365 fb->modifier[plane], cpp);
2366
bd2ef25d 2367 if (drm_rotation_90_or_270(rotation)) {
66a2d927
VS
2368 pitch_tiles = pitch / tile_height;
2369 swap(tile_width, tile_height);
2370 } else {
2371 pitch_tiles = pitch / (tile_width * cpp);
2372 }
2373
2374 _intel_adjust_tile_offset(x, y, tile_width, tile_height,
2375 tile_size, pitch_tiles,
2376 old_offset, new_offset);
2377 } else {
2378 old_offset += *y * pitch + *x * cpp;
2379
2380 *y = (old_offset - new_offset) / pitch;
2381 *x = ((old_offset - new_offset) - *y * pitch) / cpp;
2382 }
2383
2384 return new_offset;
2385}
2386
8d0deca8
VS
2387/*
2388 * Computes the linear offset to the base tile and adjusts
2389 * x, y. bytes per pixel is assumed to be a power-of-two.
2390 *
2391 * In the 90/270 rotated case, x and y are assumed
2392 * to be already rotated to match the rotated GTT view, and
2393 * pitch is the tile_height aligned framebuffer height.
6687c906
VS
2394 *
2395 * This function is used when computing the derived information
2396 * under intel_framebuffer, so using any of that information
2397 * here is not allowed. Anything under drm_framebuffer can be
2398 * used. This is why the user has to pass in the pitch since it
2399 * is specified in the rotated orientation.
8d0deca8 2400 */
6687c906
VS
2401static u32 _intel_compute_tile_offset(const struct drm_i915_private *dev_priv,
2402 int *x, int *y,
2403 const struct drm_framebuffer *fb, int plane,
2404 unsigned int pitch,
2405 unsigned int rotation,
2406 u32 alignment)
c2c75131 2407{
4f2d9934
VS
2408 uint64_t fb_modifier = fb->modifier[plane];
2409 unsigned int cpp = drm_format_plane_cpp(fb->pixel_format, plane);
6687c906 2410 u32 offset, offset_aligned;
29cf9491 2411
29cf9491
VS
2412 if (alignment)
2413 alignment--;
2414
b5c65338 2415 if (fb_modifier != DRM_FORMAT_MOD_NONE) {
8d0deca8
VS
2416 unsigned int tile_size, tile_width, tile_height;
2417 unsigned int tile_rows, tiles, pitch_tiles;
c2c75131 2418
d843310d 2419 tile_size = intel_tile_size(dev_priv);
8d0deca8
VS
2420 intel_tile_dims(dev_priv, &tile_width, &tile_height,
2421 fb_modifier, cpp);
2422
bd2ef25d 2423 if (drm_rotation_90_or_270(rotation)) {
8d0deca8
VS
2424 pitch_tiles = pitch / tile_height;
2425 swap(tile_width, tile_height);
2426 } else {
2427 pitch_tiles = pitch / (tile_width * cpp);
2428 }
d843310d
VS
2429
2430 tile_rows = *y / tile_height;
2431 *y %= tile_height;
c2c75131 2432
8d0deca8
VS
2433 tiles = *x / tile_width;
2434 *x %= tile_width;
bc752862 2435
29cf9491
VS
2436 offset = (tile_rows * pitch_tiles + tiles) * tile_size;
2437 offset_aligned = offset & ~alignment;
bc752862 2438
66a2d927
VS
2439 _intel_adjust_tile_offset(x, y, tile_width, tile_height,
2440 tile_size, pitch_tiles,
2441 offset, offset_aligned);
29cf9491 2442 } else {
bc752862 2443 offset = *y * pitch + *x * cpp;
29cf9491
VS
2444 offset_aligned = offset & ~alignment;
2445
4e9a86b6
VS
2446 *y = (offset & alignment) / pitch;
2447 *x = ((offset & alignment) - *y * pitch) / cpp;
bc752862 2448 }
29cf9491
VS
2449
2450 return offset_aligned;
c2c75131
SV
2451}
2452
6687c906 2453u32 intel_compute_tile_offset(int *x, int *y,
2949056c
VS
2454 const struct intel_plane_state *state,
2455 int plane)
6687c906 2456{
2949056c
VS
2457 const struct drm_i915_private *dev_priv = to_i915(state->base.plane->dev);
2458 const struct drm_framebuffer *fb = state->base.fb;
2459 unsigned int rotation = state->base.rotation;
ef78ec94 2460 int pitch = intel_fb_pitch(fb, plane, rotation);
8d970654
VS
2461 u32 alignment;
2462
2463 /* AUX_DIST needs only 4K alignment */
2464 if (fb->pixel_format == DRM_FORMAT_NV12 && plane == 1)
2465 alignment = 4096;
2466 else
2467 alignment = intel_surf_alignment(dev_priv, fb->modifier[plane]);
6687c906
VS
2468
2469 return _intel_compute_tile_offset(dev_priv, x, y, fb, plane, pitch,
2470 rotation, alignment);
2471}
2472
2473/* Convert the fb->offset[] linear offset into x/y offsets */
2474static void intel_fb_offset_to_xy(int *x, int *y,
2475 const struct drm_framebuffer *fb, int plane)
2476{
2477 unsigned int cpp = drm_format_plane_cpp(fb->pixel_format, plane);
2478 unsigned int pitch = fb->pitches[plane];
2479 u32 linear_offset = fb->offsets[plane];
2480
2481 *y = linear_offset / pitch;
2482 *x = linear_offset % pitch / cpp;
2483}
2484
72618ebf
VS
2485static unsigned int intel_fb_modifier_to_tiling(uint64_t fb_modifier)
2486{
2487 switch (fb_modifier) {
2488 case I915_FORMAT_MOD_X_TILED:
2489 return I915_TILING_X;
2490 case I915_FORMAT_MOD_Y_TILED:
2491 return I915_TILING_Y;
2492 default:
2493 return I915_TILING_NONE;
2494 }
2495}
2496
6687c906
VS
2497static int
2498intel_fill_fb_info(struct drm_i915_private *dev_priv,
2499 struct drm_framebuffer *fb)
2500{
2501 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
2502 struct intel_rotation_info *rot_info = &intel_fb->rot_info;
2503 u32 gtt_offset_rotated = 0;
2504 unsigned int max_size = 0;
2505 uint32_t format = fb->pixel_format;
2506 int i, num_planes = drm_format_num_planes(format);
2507 unsigned int tile_size = intel_tile_size(dev_priv);
2508
2509 for (i = 0; i < num_planes; i++) {
2510 unsigned int width, height;
2511 unsigned int cpp, size;
2512 u32 offset;
2513 int x, y;
2514
2515 cpp = drm_format_plane_cpp(format, i);
2516 width = drm_format_plane_width(fb->width, format, i);
2517 height = drm_format_plane_height(fb->height, format, i);
2518
2519 intel_fb_offset_to_xy(&x, &y, fb, i);
2520
60d5f2a4
VS
2521 /*
2522 * The fence (if used) is aligned to the start of the object
2523 * so having the framebuffer wrap around across the edge of the
2524 * fenced region doesn't really work. We have no API to configure
2525 * the fence start offset within the object (nor could we probably
2526 * on gen2/3). So it's just easier if we just require that the
2527 * fb layout agrees with the fence layout. We already check that the
2528 * fb stride matches the fence stride elsewhere.
2529 */
2530 if (i915_gem_object_is_tiled(intel_fb->obj) &&
2531 (x + width) * cpp > fb->pitches[i]) {
2532 DRM_DEBUG("bad fb plane %d offset: 0x%x\n",
2533 i, fb->offsets[i]);
2534 return -EINVAL;
2535 }
2536
6687c906
VS
2537 /*
2538 * First pixel of the framebuffer from
2539 * the start of the normal gtt mapping.
2540 */
2541 intel_fb->normal[i].x = x;
2542 intel_fb->normal[i].y = y;
2543
2544 offset = _intel_compute_tile_offset(dev_priv, &x, &y,
2545 fb, 0, fb->pitches[i],
cc926387 2546 DRM_ROTATE_0, tile_size);
6687c906
VS
2547 offset /= tile_size;
2548
2549 if (fb->modifier[i] != DRM_FORMAT_MOD_NONE) {
2550 unsigned int tile_width, tile_height;
2551 unsigned int pitch_tiles;
2552 struct drm_rect r;
2553
2554 intel_tile_dims(dev_priv, &tile_width, &tile_height,
2555 fb->modifier[i], cpp);
2556
2557 rot_info->plane[i].offset = offset;
2558 rot_info->plane[i].stride = DIV_ROUND_UP(fb->pitches[i], tile_width * cpp);
2559 rot_info->plane[i].width = DIV_ROUND_UP(x + width, tile_width);
2560 rot_info->plane[i].height = DIV_ROUND_UP(y + height, tile_height);
2561
2562 intel_fb->rotated[i].pitch =
2563 rot_info->plane[i].height * tile_height;
2564
2565 /* how many tiles does this plane need */
2566 size = rot_info->plane[i].stride * rot_info->plane[i].height;
2567 /*
2568 * If the plane isn't horizontally tile aligned,
2569 * we need one more tile.
2570 */
2571 if (x != 0)
2572 size++;
2573
2574 /* rotate the x/y offsets to match the GTT view */
2575 r.x1 = x;
2576 r.y1 = y;
2577 r.x2 = x + width;
2578 r.y2 = y + height;
2579 drm_rect_rotate(&r,
2580 rot_info->plane[i].width * tile_width,
2581 rot_info->plane[i].height * tile_height,
cc926387 2582 DRM_ROTATE_270);
6687c906
VS
2583 x = r.x1;
2584 y = r.y1;
2585
2586 /* rotate the tile dimensions to match the GTT view */
2587 pitch_tiles = intel_fb->rotated[i].pitch / tile_height;
2588 swap(tile_width, tile_height);
2589
2590 /*
2591 * We only keep the x/y offsets, so push all of the
2592 * gtt offset into the x/y offsets.
2593 */
66a2d927
VS
2594 _intel_adjust_tile_offset(&x, &y, tile_size,
2595 tile_width, tile_height, pitch_tiles,
2596 gtt_offset_rotated * tile_size, 0);
6687c906
VS
2597
2598 gtt_offset_rotated += rot_info->plane[i].width * rot_info->plane[i].height;
2599
2600 /*
2601 * First pixel of the framebuffer from
2602 * the start of the rotated gtt mapping.
2603 */
2604 intel_fb->rotated[i].x = x;
2605 intel_fb->rotated[i].y = y;
2606 } else {
2607 size = DIV_ROUND_UP((y + height) * fb->pitches[i] +
2608 x * cpp, tile_size);
2609 }
2610
2611 /* how many tiles in total needed in the bo */
2612 max_size = max(max_size, offset + size);
2613 }
2614
2615 if (max_size * tile_size > to_intel_framebuffer(fb)->obj->base.size) {
2616 DRM_DEBUG("fb too big for bo (need %u bytes, have %zu bytes)\n",
2617 max_size * tile_size, to_intel_framebuffer(fb)->obj->base.size);
2618 return -EINVAL;
2619 }
2620
2621 return 0;
2622}
2623
b35d63fa 2624static int i9xx_format_to_fourcc(int format)
46f297fb
JB
2625{
2626 switch (format) {
2627 case DISPPLANE_8BPP:
2628 return DRM_FORMAT_C8;
2629 case DISPPLANE_BGRX555:
2630 return DRM_FORMAT_XRGB1555;
2631 case DISPPLANE_BGRX565:
2632 return DRM_FORMAT_RGB565;
2633 default:
2634 case DISPPLANE_BGRX888:
2635 return DRM_FORMAT_XRGB8888;
2636 case DISPPLANE_RGBX888:
2637 return DRM_FORMAT_XBGR8888;
2638 case DISPPLANE_BGRX101010:
2639 return DRM_FORMAT_XRGB2101010;
2640 case DISPPLANE_RGBX101010:
2641 return DRM_FORMAT_XBGR2101010;
2642 }
2643}
2644
bc8d7dff
DL
2645static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
2646{
2647 switch (format) {
2648 case PLANE_CTL_FORMAT_RGB_565:
2649 return DRM_FORMAT_RGB565;
2650 default:
2651 case PLANE_CTL_FORMAT_XRGB_8888:
2652 if (rgb_order) {
2653 if (alpha)
2654 return DRM_FORMAT_ABGR8888;
2655 else
2656 return DRM_FORMAT_XBGR8888;
2657 } else {
2658 if (alpha)
2659 return DRM_FORMAT_ARGB8888;
2660 else
2661 return DRM_FORMAT_XRGB8888;
2662 }
2663 case PLANE_CTL_FORMAT_XRGB_2101010:
2664 if (rgb_order)
2665 return DRM_FORMAT_XBGR2101010;
2666 else
2667 return DRM_FORMAT_XRGB2101010;
2668 }
2669}
2670
5724dbd1 2671static bool
f6936e29
SV
2672intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
2673 struct intel_initial_plane_config *plane_config)
46f297fb
JB
2674{
2675 struct drm_device *dev = crtc->base.dev;
3badb49f 2676 struct drm_i915_private *dev_priv = to_i915(dev);
72e96d64 2677 struct i915_ggtt *ggtt = &dev_priv->ggtt;
46f297fb
JB
2678 struct drm_i915_gem_object *obj = NULL;
2679 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
2d14030b 2680 struct drm_framebuffer *fb = &plane_config->fb->base;
f37b5c2b
SV
2681 u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
2682 u32 size_aligned = round_up(plane_config->base + plane_config->size,
2683 PAGE_SIZE);
2684
2685 size_aligned -= base_aligned;
46f297fb 2686
ff2652ea
CW
2687 if (plane_config->size == 0)
2688 return false;
2689
3badb49f
PZ
2690 /* If the FB is too big, just don't use it since fbdev is not very
2691 * important and we should probably use that space with FBC or other
2692 * features. */
72e96d64 2693 if (size_aligned * 2 > ggtt->stolen_usable_size)
3badb49f
PZ
2694 return false;
2695
12c83d99
TU
2696 mutex_lock(&dev->struct_mutex);
2697
f37b5c2b
SV
2698 obj = i915_gem_object_create_stolen_for_preallocated(dev,
2699 base_aligned,
2700 base_aligned,
2701 size_aligned);
12c83d99
TU
2702 if (!obj) {
2703 mutex_unlock(&dev->struct_mutex);
484b41dd 2704 return false;
12c83d99 2705 }
46f297fb 2706
3e510a8e
CW
2707 if (plane_config->tiling == I915_TILING_X)
2708 obj->tiling_and_stride = fb->pitches[0] | I915_TILING_X;
46f297fb 2709
6bf129df
DL
2710 mode_cmd.pixel_format = fb->pixel_format;
2711 mode_cmd.width = fb->width;
2712 mode_cmd.height = fb->height;
2713 mode_cmd.pitches[0] = fb->pitches[0];
18c5247e
SV
2714 mode_cmd.modifier[0] = fb->modifier[0];
2715 mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
46f297fb 2716
6bf129df 2717 if (intel_framebuffer_init(dev, to_intel_framebuffer(fb),
484b41dd 2718 &mode_cmd, obj)) {
46f297fb
JB
2719 DRM_DEBUG_KMS("intel fb init failed\n");
2720 goto out_unref_obj;
2721 }
12c83d99 2722
46f297fb 2723 mutex_unlock(&dev->struct_mutex);
484b41dd 2724
f6936e29 2725 DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
484b41dd 2726 return true;
46f297fb
JB
2727
2728out_unref_obj:
f8c417cd 2729 i915_gem_object_put(obj);
46f297fb 2730 mutex_unlock(&dev->struct_mutex);
484b41dd
JB
2731 return false;
2732}
2733
5a21b665
SV
2734/* Update plane->state->fb to match plane->fb after driver-internal updates */
2735static void
2736update_state_fb(struct drm_plane *plane)
2737{
2738 if (plane->fb == plane->state->fb)
2739 return;
2740
2741 if (plane->state->fb)
2742 drm_framebuffer_unreference(plane->state->fb);
2743 plane->state->fb = plane->fb;
2744 if (plane->state->fb)
2745 drm_framebuffer_reference(plane->state->fb);
2746}
2747
5724dbd1 2748static void
f6936e29
SV
2749intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
2750 struct intel_initial_plane_config *plane_config)
484b41dd
JB
2751{
2752 struct drm_device *dev = intel_crtc->base.dev;
fac5e23e 2753 struct drm_i915_private *dev_priv = to_i915(dev);
484b41dd
JB
2754 struct drm_crtc *c;
2755 struct intel_crtc *i;
2ff8fde1 2756 struct drm_i915_gem_object *obj;
88595ac9 2757 struct drm_plane *primary = intel_crtc->base.primary;
be5651f2 2758 struct drm_plane_state *plane_state = primary->state;
200757f5
MR
2759 struct drm_crtc_state *crtc_state = intel_crtc->base.state;
2760 struct intel_plane *intel_plane = to_intel_plane(primary);
0a8d8a86
MR
2761 struct intel_plane_state *intel_state =
2762 to_intel_plane_state(plane_state);
88595ac9 2763 struct drm_framebuffer *fb;
484b41dd 2764
2d14030b 2765 if (!plane_config->fb)
484b41dd
JB
2766 return;
2767
f6936e29 2768 if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
88595ac9
SV
2769 fb = &plane_config->fb->base;
2770 goto valid_fb;
f55548b5 2771 }
484b41dd 2772
2d14030b 2773 kfree(plane_config->fb);
484b41dd
JB
2774
2775 /*
2776 * Failed to alloc the obj, check to see if we should share
2777 * an fb with another CRTC instead
2778 */
70e1e0ec 2779 for_each_crtc(dev, c) {
484b41dd
JB
2780 i = to_intel_crtc(c);
2781
2782 if (c == &intel_crtc->base)
2783 continue;
2784
2ff8fde1
MR
2785 if (!i->active)
2786 continue;
2787
88595ac9
SV
2788 fb = c->primary->fb;
2789 if (!fb)
484b41dd
JB
2790 continue;
2791
88595ac9 2792 obj = intel_fb_obj(fb);
058d88c4 2793 if (i915_gem_object_ggtt_offset(obj, NULL) == plane_config->base) {
88595ac9
SV
2794 drm_framebuffer_reference(fb);
2795 goto valid_fb;
484b41dd
JB
2796 }
2797 }
88595ac9 2798
200757f5
MR
2799 /*
2800 * We've failed to reconstruct the BIOS FB. Current display state
2801 * indicates that the primary plane is visible, but has a NULL FB,
2802 * which will lead to problems later if we don't fix it up. The
2803 * simplest solution is to just disable the primary plane now and
2804 * pretend the BIOS never had it enabled.
2805 */
936e71e3 2806 to_intel_plane_state(plane_state)->base.visible = false;
200757f5 2807 crtc_state->plane_mask &= ~(1 << drm_plane_index(primary));
2622a081 2808 intel_pre_disable_primary_noatomic(&intel_crtc->base);
200757f5
MR
2809 intel_plane->disable_plane(primary, &intel_crtc->base);
2810
88595ac9
SV
2811 return;
2812
2813valid_fb:
f44e2659
VS
2814 plane_state->src_x = 0;
2815 plane_state->src_y = 0;
be5651f2
ML
2816 plane_state->src_w = fb->width << 16;
2817 plane_state->src_h = fb->height << 16;
2818
f44e2659
VS
2819 plane_state->crtc_x = 0;
2820 plane_state->crtc_y = 0;
be5651f2
ML
2821 plane_state->crtc_w = fb->width;
2822 plane_state->crtc_h = fb->height;
2823
936e71e3
VS
2824 intel_state->base.src.x1 = plane_state->src_x;
2825 intel_state->base.src.y1 = plane_state->src_y;
2826 intel_state->base.src.x2 = plane_state->src_x + plane_state->src_w;
2827 intel_state->base.src.y2 = plane_state->src_y + plane_state->src_h;
2828 intel_state->base.dst.x1 = plane_state->crtc_x;
2829 intel_state->base.dst.y1 = plane_state->crtc_y;
2830 intel_state->base.dst.x2 = plane_state->crtc_x + plane_state->crtc_w;
2831 intel_state->base.dst.y2 = plane_state->crtc_y + plane_state->crtc_h;
0a8d8a86 2832
88595ac9 2833 obj = intel_fb_obj(fb);
3e510a8e 2834 if (i915_gem_object_is_tiled(obj))
88595ac9
SV
2835 dev_priv->preserve_bios_swizzle = true;
2836
be5651f2
ML
2837 drm_framebuffer_reference(fb);
2838 primary->fb = primary->state->fb = fb;
36750f28 2839 primary->crtc = primary->state->crtc = &intel_crtc->base;
36750f28 2840 intel_crtc->base.state->plane_mask |= (1 << drm_plane_index(primary));
faf5bf0a
CW
2841 atomic_or(to_intel_plane(primary)->frontbuffer_bit,
2842 &obj->frontbuffer_bits);
46f297fb
JB
2843}
2844
b63a16f6
VS
2845static int skl_max_plane_width(const struct drm_framebuffer *fb, int plane,
2846 unsigned int rotation)
2847{
2848 int cpp = drm_format_plane_cpp(fb->pixel_format, plane);
2849
2850 switch (fb->modifier[plane]) {
2851 case DRM_FORMAT_MOD_NONE:
2852 case I915_FORMAT_MOD_X_TILED:
2853 switch (cpp) {
2854 case 8:
2855 return 4096;
2856 case 4:
2857 case 2:
2858 case 1:
2859 return 8192;
2860 default:
2861 MISSING_CASE(cpp);
2862 break;
2863 }
2864 break;
2865 case I915_FORMAT_MOD_Y_TILED:
2866 case I915_FORMAT_MOD_Yf_TILED:
2867 switch (cpp) {
2868 case 8:
2869 return 2048;
2870 case 4:
2871 return 4096;
2872 case 2:
2873 case 1:
2874 return 8192;
2875 default:
2876 MISSING_CASE(cpp);
2877 break;
2878 }
2879 break;
2880 default:
2881 MISSING_CASE(fb->modifier[plane]);
2882 }
2883
2884 return 2048;
2885}
2886
2887static int skl_check_main_surface(struct intel_plane_state *plane_state)
2888{
2889 const struct drm_i915_private *dev_priv = to_i915(plane_state->base.plane->dev);
2890 const struct drm_framebuffer *fb = plane_state->base.fb;
2891 unsigned int rotation = plane_state->base.rotation;
cc926387
SV
2892 int x = plane_state->base.src.x1 >> 16;
2893 int y = plane_state->base.src.y1 >> 16;
2894 int w = drm_rect_width(&plane_state->base.src) >> 16;
2895 int h = drm_rect_height(&plane_state->base.src) >> 16;
b63a16f6
VS
2896 int max_width = skl_max_plane_width(fb, 0, rotation);
2897 int max_height = 4096;
8d970654 2898 u32 alignment, offset, aux_offset = plane_state->aux.offset;
b63a16f6
VS
2899
2900 if (w > max_width || h > max_height) {
2901 DRM_DEBUG_KMS("requested Y/RGB source size %dx%d too big (limit %dx%d)\n",
2902 w, h, max_width, max_height);
2903 return -EINVAL;
2904 }
2905
2906 intel_add_fb_offsets(&x, &y, plane_state, 0);
2907 offset = intel_compute_tile_offset(&x, &y, plane_state, 0);
2908
2909 alignment = intel_surf_alignment(dev_priv, fb->modifier[0]);
2910
8d970654
VS
2911 /*
2912 * AUX surface offset is specified as the distance from the
2913 * main surface offset, and it must be non-negative. Make
2914 * sure that is what we will get.
2915 */
2916 if (offset > aux_offset)
2917 offset = intel_adjust_tile_offset(&x, &y, plane_state, 0,
2918 offset, aux_offset & ~(alignment - 1));
2919
b63a16f6
VS
2920 /*
2921 * When using an X-tiled surface, the plane blows up
2922 * if the x offset + width exceed the stride.
2923 *
2924 * TODO: linear and Y-tiled seem fine, Yf untested,
2925 */
2926 if (fb->modifier[0] == I915_FORMAT_MOD_X_TILED) {
2927 int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
2928
2929 while ((x + w) * cpp > fb->pitches[0]) {
2930 if (offset == 0) {
2931 DRM_DEBUG_KMS("Unable to find suitable display surface offset\n");
2932 return -EINVAL;
2933 }
2934
2935 offset = intel_adjust_tile_offset(&x, &y, plane_state, 0,
2936 offset, offset - alignment);
2937 }
2938 }
2939
2940 plane_state->main.offset = offset;
2941 plane_state->main.x = x;
2942 plane_state->main.y = y;
2943
2944 return 0;
2945}
2946
8d970654
VS
2947static int skl_check_nv12_aux_surface(struct intel_plane_state *plane_state)
2948{
2949 const struct drm_framebuffer *fb = plane_state->base.fb;
2950 unsigned int rotation = plane_state->base.rotation;
2951 int max_width = skl_max_plane_width(fb, 1, rotation);
2952 int max_height = 4096;
cc926387
SV
2953 int x = plane_state->base.src.x1 >> 17;
2954 int y = plane_state->base.src.y1 >> 17;
2955 int w = drm_rect_width(&plane_state->base.src) >> 17;
2956 int h = drm_rect_height(&plane_state->base.src) >> 17;
8d970654
VS
2957 u32 offset;
2958
2959 intel_add_fb_offsets(&x, &y, plane_state, 1);
2960 offset = intel_compute_tile_offset(&x, &y, plane_state, 1);
2961
2962 /* FIXME not quite sure how/if these apply to the chroma plane */
2963 if (w > max_width || h > max_height) {
2964 DRM_DEBUG_KMS("CbCr source size %dx%d too big (limit %dx%d)\n",
2965 w, h, max_width, max_height);
2966 return -EINVAL;
2967 }
2968
2969 plane_state->aux.offset = offset;
2970 plane_state->aux.x = x;
2971 plane_state->aux.y = y;
2972
2973 return 0;
2974}
2975
b63a16f6
VS
2976int skl_check_plane_surface(struct intel_plane_state *plane_state)
2977{
2978 const struct drm_framebuffer *fb = plane_state->base.fb;
2979 unsigned int rotation = plane_state->base.rotation;
2980 int ret;
2981
2982 /* Rotate src coordinates to match rotated GTT view */
bd2ef25d 2983 if (drm_rotation_90_or_270(rotation))
cc926387 2984 drm_rect_rotate(&plane_state->base.src,
da064b47
VS
2985 fb->width << 16, fb->height << 16,
2986 DRM_ROTATE_270);
b63a16f6 2987
8d970654
VS
2988 /*
2989 * Handle the AUX surface first since
2990 * the main surface setup depends on it.
2991 */
2992 if (fb->pixel_format == DRM_FORMAT_NV12) {
2993 ret = skl_check_nv12_aux_surface(plane_state);
2994 if (ret)
2995 return ret;
2996 } else {
2997 plane_state->aux.offset = ~0xfff;
2998 plane_state->aux.x = 0;
2999 plane_state->aux.y = 0;
3000 }
3001
b63a16f6
VS
3002 ret = skl_check_main_surface(plane_state);
3003 if (ret)
3004 return ret;
3005
3006 return 0;
3007}
3008
a8d201af
ML
3009static void i9xx_update_primary_plane(struct drm_plane *primary,
3010 const struct intel_crtc_state *crtc_state,
3011 const struct intel_plane_state *plane_state)
81255565 3012{
a8d201af 3013 struct drm_device *dev = primary->dev;
fac5e23e 3014 struct drm_i915_private *dev_priv = to_i915(dev);
a8d201af
ML
3015 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
3016 struct drm_framebuffer *fb = plane_state->base.fb;
81255565 3017 int plane = intel_crtc->plane;
54ea9da8 3018 u32 linear_offset;
81255565 3019 u32 dspcntr;
f0f59a00 3020 i915_reg_t reg = DSPCNTR(plane);
8d0deca8 3021 unsigned int rotation = plane_state->base.rotation;
936e71e3
VS
3022 int x = plane_state->base.src.x1 >> 16;
3023 int y = plane_state->base.src.y1 >> 16;
c9ba6fad 3024
f45651ba
VS
3025 dspcntr = DISPPLANE_GAMMA_ENABLE;
3026
fdd508a6 3027 dspcntr |= DISPLAY_PLANE_ENABLE;
f45651ba
VS
3028
3029 if (INTEL_INFO(dev)->gen < 4) {
3030 if (intel_crtc->pipe == PIPE_B)
3031 dspcntr |= DISPPLANE_SEL_PIPE_B;
3032
3033 /* pipesrc and dspsize control the size that is scaled from,
3034 * which should always be the user's requested size.
3035 */
3036 I915_WRITE(DSPSIZE(plane),
a8d201af
ML
3037 ((crtc_state->pipe_src_h - 1) << 16) |
3038 (crtc_state->pipe_src_w - 1));
f45651ba 3039 I915_WRITE(DSPPOS(plane), 0);
920a14b2 3040 } else if (IS_CHERRYVIEW(dev_priv) && plane == PLANE_B) {
c14b0485 3041 I915_WRITE(PRIMSIZE(plane),
a8d201af
ML
3042 ((crtc_state->pipe_src_h - 1) << 16) |
3043 (crtc_state->pipe_src_w - 1));
c14b0485
VS
3044 I915_WRITE(PRIMPOS(plane), 0);
3045 I915_WRITE(PRIMCNSTALPHA(plane), 0);
f45651ba 3046 }
81255565 3047
57779d06
VS
3048 switch (fb->pixel_format) {
3049 case DRM_FORMAT_C8:
81255565
JB
3050 dspcntr |= DISPPLANE_8BPP;
3051 break;
57779d06 3052 case DRM_FORMAT_XRGB1555:
57779d06 3053 dspcntr |= DISPPLANE_BGRX555;
81255565 3054 break;
57779d06
VS
3055 case DRM_FORMAT_RGB565:
3056 dspcntr |= DISPPLANE_BGRX565;
3057 break;
3058 case DRM_FORMAT_XRGB8888:
57779d06
VS
3059 dspcntr |= DISPPLANE_BGRX888;
3060 break;
3061 case DRM_FORMAT_XBGR8888:
57779d06
VS
3062 dspcntr |= DISPPLANE_RGBX888;
3063 break;
3064 case DRM_FORMAT_XRGB2101010:
57779d06
VS
3065 dspcntr |= DISPPLANE_BGRX101010;
3066 break;
3067 case DRM_FORMAT_XBGR2101010:
57779d06 3068 dspcntr |= DISPPLANE_RGBX101010;
81255565
JB
3069 break;
3070 default:
baba133a 3071 BUG();
81255565 3072 }
57779d06 3073
72618ebf
VS
3074 if (INTEL_GEN(dev_priv) >= 4 &&
3075 fb->modifier[0] == I915_FORMAT_MOD_X_TILED)
f45651ba 3076 dspcntr |= DISPPLANE_TILED;
81255565 3077
df0cd455
VS
3078 if (rotation & DRM_ROTATE_180)
3079 dspcntr |= DISPPLANE_ROTATE_180;
3080
9beb5fea 3081 if (IS_G4X(dev_priv))
de1aa629
VS
3082 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
3083
2949056c 3084 intel_add_fb_offsets(&x, &y, plane_state, 0);
81255565 3085
6687c906 3086 if (INTEL_INFO(dev)->gen >= 4)
c2c75131 3087 intel_crtc->dspaddr_offset =
2949056c 3088 intel_compute_tile_offset(&x, &y, plane_state, 0);
e506a0c6 3089
f22aa143 3090 if (rotation & DRM_ROTATE_180) {
df0cd455
VS
3091 x += crtc_state->pipe_src_w - 1;
3092 y += crtc_state->pipe_src_h - 1;
48404c1e
SJ
3093 }
3094
2949056c 3095 linear_offset = intel_fb_xy_to_linear(x, y, plane_state, 0);
6687c906
VS
3096
3097 if (INTEL_INFO(dev)->gen < 4)
3098 intel_crtc->dspaddr_offset = linear_offset;
3099
2db3366b
PZ
3100 intel_crtc->adjusted_x = x;
3101 intel_crtc->adjusted_y = y;
3102
48404c1e
SJ
3103 I915_WRITE(reg, dspcntr);
3104
01f2c773 3105 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
a6c45cf0 3106 if (INTEL_INFO(dev)->gen >= 4) {
85ba7b7d 3107 I915_WRITE(DSPSURF(plane),
6687c906
VS
3108 intel_fb_gtt_offset(fb, rotation) +
3109 intel_crtc->dspaddr_offset);
5eddb70b 3110 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
e506a0c6 3111 I915_WRITE(DSPLINOFF(plane), linear_offset);
bfb81049
VS
3112 } else {
3113 I915_WRITE(DSPADDR(plane),
3114 intel_fb_gtt_offset(fb, rotation) +
3115 intel_crtc->dspaddr_offset);
3116 }
5eddb70b 3117 POSTING_READ(reg);
17638cd6
JB
3118}
3119
a8d201af
ML
3120static void i9xx_disable_primary_plane(struct drm_plane *primary,
3121 struct drm_crtc *crtc)
17638cd6
JB
3122{
3123 struct drm_device *dev = crtc->dev;
fac5e23e 3124 struct drm_i915_private *dev_priv = to_i915(dev);
17638cd6 3125 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
17638cd6 3126 int plane = intel_crtc->plane;
f45651ba 3127
a8d201af
ML
3128 I915_WRITE(DSPCNTR(plane), 0);
3129 if (INTEL_INFO(dev_priv)->gen >= 4)
fdd508a6 3130 I915_WRITE(DSPSURF(plane), 0);
a8d201af
ML
3131 else
3132 I915_WRITE(DSPADDR(plane), 0);
3133 POSTING_READ(DSPCNTR(plane));
3134}
c9ba6fad 3135
a8d201af
ML
3136static void ironlake_update_primary_plane(struct drm_plane *primary,
3137 const struct intel_crtc_state *crtc_state,
3138 const struct intel_plane_state *plane_state)
3139{
3140 struct drm_device *dev = primary->dev;
fac5e23e 3141 struct drm_i915_private *dev_priv = to_i915(dev);
a8d201af
ML
3142 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
3143 struct drm_framebuffer *fb = plane_state->base.fb;
a8d201af 3144 int plane = intel_crtc->plane;
54ea9da8 3145 u32 linear_offset;
a8d201af
ML
3146 u32 dspcntr;
3147 i915_reg_t reg = DSPCNTR(plane);
8d0deca8 3148 unsigned int rotation = plane_state->base.rotation;
936e71e3
VS
3149 int x = plane_state->base.src.x1 >> 16;
3150 int y = plane_state->base.src.y1 >> 16;
c9ba6fad 3151
f45651ba 3152 dspcntr = DISPPLANE_GAMMA_ENABLE;
fdd508a6 3153 dspcntr |= DISPLAY_PLANE_ENABLE;
f45651ba 3154
8652744b 3155 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
f45651ba 3156 dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
17638cd6 3157
57779d06
VS
3158 switch (fb->pixel_format) {
3159 case DRM_FORMAT_C8:
17638cd6
JB
3160 dspcntr |= DISPPLANE_8BPP;
3161 break;
57779d06
VS
3162 case DRM_FORMAT_RGB565:
3163 dspcntr |= DISPPLANE_BGRX565;
17638cd6 3164 break;
57779d06 3165 case DRM_FORMAT_XRGB8888:
57779d06
VS
3166 dspcntr |= DISPPLANE_BGRX888;
3167 break;
3168 case DRM_FORMAT_XBGR8888:
57779d06
VS
3169 dspcntr |= DISPPLANE_RGBX888;
3170 break;
3171 case DRM_FORMAT_XRGB2101010:
57779d06
VS
3172 dspcntr |= DISPPLANE_BGRX101010;
3173 break;
3174 case DRM_FORMAT_XBGR2101010:
57779d06 3175 dspcntr |= DISPPLANE_RGBX101010;
17638cd6
JB
3176 break;
3177 default:
baba133a 3178 BUG();
17638cd6
JB
3179 }
3180
72618ebf 3181 if (fb->modifier[0] == I915_FORMAT_MOD_X_TILED)
17638cd6 3182 dspcntr |= DISPPLANE_TILED;
17638cd6 3183
df0cd455
VS
3184 if (rotation & DRM_ROTATE_180)
3185 dspcntr |= DISPPLANE_ROTATE_180;
3186
8652744b 3187 if (!IS_HASWELL(dev_priv) && !IS_BROADWELL(dev_priv))
1f5d76db 3188 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
17638cd6 3189
2949056c 3190 intel_add_fb_offsets(&x, &y, plane_state, 0);
6687c906 3191
c2c75131 3192 intel_crtc->dspaddr_offset =
2949056c 3193 intel_compute_tile_offset(&x, &y, plane_state, 0);
6687c906 3194
df0cd455
VS
3195 /* HSW+ does this automagically in hardware */
3196 if (!IS_HASWELL(dev_priv) && !IS_BROADWELL(dev_priv) &&
3197 rotation & DRM_ROTATE_180) {
3198 x += crtc_state->pipe_src_w - 1;
3199 y += crtc_state->pipe_src_h - 1;
48404c1e
SJ
3200 }
3201
2949056c 3202 linear_offset = intel_fb_xy_to_linear(x, y, plane_state, 0);
6687c906 3203
2db3366b
PZ
3204 intel_crtc->adjusted_x = x;
3205 intel_crtc->adjusted_y = y;
3206
48404c1e 3207 I915_WRITE(reg, dspcntr);
17638cd6 3208
01f2c773 3209 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
85ba7b7d 3210 I915_WRITE(DSPSURF(plane),
6687c906
VS
3211 intel_fb_gtt_offset(fb, rotation) +
3212 intel_crtc->dspaddr_offset);
8652744b 3213 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
bc1c91eb
DL
3214 I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
3215 } else {
3216 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
3217 I915_WRITE(DSPLINOFF(plane), linear_offset);
3218 }
17638cd6 3219 POSTING_READ(reg);
17638cd6
JB
3220}
3221
7b49f948
VS
3222u32 intel_fb_stride_alignment(const struct drm_i915_private *dev_priv,
3223 uint64_t fb_modifier, uint32_t pixel_format)
b321803d 3224{
7b49f948 3225 if (fb_modifier == DRM_FORMAT_MOD_NONE) {
b321803d 3226 return 64;
7b49f948
VS
3227 } else {
3228 int cpp = drm_format_plane_cpp(pixel_format, 0);
3229
27ba3910 3230 return intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
b321803d
DL
3231 }
3232}
3233
6687c906
VS
3234u32 intel_fb_gtt_offset(struct drm_framebuffer *fb,
3235 unsigned int rotation)
121920fa 3236{
6687c906 3237 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
ce7f1728 3238 struct i915_ggtt_view view;
058d88c4 3239 struct i915_vma *vma;
121920fa 3240
6687c906 3241 intel_fill_fb_ggtt_view(&view, fb, rotation);
dedf278c 3242
058d88c4
CW
3243 vma = i915_gem_object_to_ggtt(obj, &view);
3244 if (WARN(!vma, "ggtt vma for display object not found! (view=%u)\n",
3245 view.type))
3246 return -1;
3247
bde13ebd 3248 return i915_ggtt_offset(vma);
121920fa
TU
3249}
3250
e435d6e5
ML
3251static void skl_detach_scaler(struct intel_crtc *intel_crtc, int id)
3252{
3253 struct drm_device *dev = intel_crtc->base.dev;
fac5e23e 3254 struct drm_i915_private *dev_priv = to_i915(dev);
e435d6e5
ML
3255
3256 I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, id), 0);
3257 I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, id), 0);
3258 I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, id), 0);
e435d6e5
ML
3259}
3260
a1b2278e
CK
3261/*
3262 * This function detaches (aka. unbinds) unused scalers in hardware
3263 */
0583236e 3264static void skl_detach_scalers(struct intel_crtc *intel_crtc)
a1b2278e 3265{
a1b2278e
CK
3266 struct intel_crtc_scaler_state *scaler_state;
3267 int i;
3268
a1b2278e
CK
3269 scaler_state = &intel_crtc->config->scaler_state;
3270
3271 /* loop through and disable scalers that aren't in use */
3272 for (i = 0; i < intel_crtc->num_scalers; i++) {
e435d6e5
ML
3273 if (!scaler_state->scalers[i].in_use)
3274 skl_detach_scaler(intel_crtc, i);
a1b2278e
CK
3275 }
3276}
3277
d2196774
VS
3278u32 skl_plane_stride(const struct drm_framebuffer *fb, int plane,
3279 unsigned int rotation)
3280{
3281 const struct drm_i915_private *dev_priv = to_i915(fb->dev);
3282 u32 stride = intel_fb_pitch(fb, plane, rotation);
3283
3284 /*
3285 * The stride is either expressed as a multiple of 64 bytes chunks for
3286 * linear buffers or in number of tiles for tiled buffers.
3287 */
bd2ef25d 3288 if (drm_rotation_90_or_270(rotation)) {
d2196774
VS
3289 int cpp = drm_format_plane_cpp(fb->pixel_format, plane);
3290
3291 stride /= intel_tile_height(dev_priv, fb->modifier[0], cpp);
3292 } else {
3293 stride /= intel_fb_stride_alignment(dev_priv, fb->modifier[0],
3294 fb->pixel_format);
3295 }
3296
3297 return stride;
3298}
3299
6156a456 3300u32 skl_plane_ctl_format(uint32_t pixel_format)
70d21f0e 3301{
6156a456 3302 switch (pixel_format) {
d161cf7a 3303 case DRM_FORMAT_C8:
c34ce3d1 3304 return PLANE_CTL_FORMAT_INDEXED;
70d21f0e 3305 case DRM_FORMAT_RGB565:
c34ce3d1 3306 return PLANE_CTL_FORMAT_RGB_565;
70d21f0e 3307 case DRM_FORMAT_XBGR8888:
c34ce3d1 3308 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
6156a456 3309 case DRM_FORMAT_XRGB8888:
c34ce3d1 3310 return PLANE_CTL_FORMAT_XRGB_8888;
6156a456
CK
3311 /*
3312 * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
3313 * to be already pre-multiplied. We need to add a knob (or a different
3314 * DRM_FORMAT) for user-space to configure that.
3315 */
f75fb42a 3316 case DRM_FORMAT_ABGR8888:
c34ce3d1 3317 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX |
6156a456 3318 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
6156a456 3319 case DRM_FORMAT_ARGB8888:
c34ce3d1 3320 return PLANE_CTL_FORMAT_XRGB_8888 |
6156a456 3321 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
70d21f0e 3322 case DRM_FORMAT_XRGB2101010:
c34ce3d1 3323 return PLANE_CTL_FORMAT_XRGB_2101010;
70d21f0e 3324 case DRM_FORMAT_XBGR2101010:
c34ce3d1 3325 return PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
6156a456 3326 case DRM_FORMAT_YUYV:
c34ce3d1 3327 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
6156a456 3328 case DRM_FORMAT_YVYU:
c34ce3d1 3329 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
6156a456 3330 case DRM_FORMAT_UYVY:
c34ce3d1 3331 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
6156a456 3332 case DRM_FORMAT_VYUY:
c34ce3d1 3333 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
70d21f0e 3334 default:
4249eeef 3335 MISSING_CASE(pixel_format);
70d21f0e 3336 }
8cfcba41 3337
c34ce3d1 3338 return 0;
6156a456 3339}
70d21f0e 3340
6156a456
CK
3341u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
3342{
6156a456 3343 switch (fb_modifier) {
30af77c4 3344 case DRM_FORMAT_MOD_NONE:
70d21f0e 3345 break;
30af77c4 3346 case I915_FORMAT_MOD_X_TILED:
c34ce3d1 3347 return PLANE_CTL_TILED_X;
b321803d 3348 case I915_FORMAT_MOD_Y_TILED:
c34ce3d1 3349 return PLANE_CTL_TILED_Y;
b321803d 3350 case I915_FORMAT_MOD_Yf_TILED:
c34ce3d1 3351 return PLANE_CTL_TILED_YF;
70d21f0e 3352 default:
6156a456 3353 MISSING_CASE(fb_modifier);
70d21f0e 3354 }
8cfcba41 3355
c34ce3d1 3356 return 0;
6156a456 3357}
70d21f0e 3358
6156a456
CK
3359u32 skl_plane_ctl_rotation(unsigned int rotation)
3360{
3b7a5119 3361 switch (rotation) {
31ad61e4 3362 case DRM_ROTATE_0:
6156a456 3363 break;
1e8df167
SJ
3364 /*
3365 * DRM_ROTATE_ is counter clockwise to stay compatible with Xrandr
3366 * while i915 HW rotation is clockwise, thats why this swapping.
3367 */
31ad61e4 3368 case DRM_ROTATE_90:
1e8df167 3369 return PLANE_CTL_ROTATE_270;
31ad61e4 3370 case DRM_ROTATE_180:
c34ce3d1 3371 return PLANE_CTL_ROTATE_180;
31ad61e4 3372 case DRM_ROTATE_270:
1e8df167 3373 return PLANE_CTL_ROTATE_90;
6156a456
CK
3374 default:
3375 MISSING_CASE(rotation);
3376 }
3377
c34ce3d1 3378 return 0;
6156a456
CK
3379}
3380
a8d201af
ML
3381static void skylake_update_primary_plane(struct drm_plane *plane,
3382 const struct intel_crtc_state *crtc_state,
3383 const struct intel_plane_state *plane_state)
6156a456 3384{
a8d201af 3385 struct drm_device *dev = plane->dev;
fac5e23e 3386 struct drm_i915_private *dev_priv = to_i915(dev);
a8d201af
ML
3387 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
3388 struct drm_framebuffer *fb = plane_state->base.fb;
62e0fb88 3389 const struct skl_wm_values *wm = &dev_priv->wm.skl_results;
d8c0fafc 3390 const struct skl_plane_wm *p_wm =
3391 &crtc_state->wm.skl.optimal.planes[0];
6156a456 3392 int pipe = intel_crtc->pipe;
d2196774 3393 u32 plane_ctl;
a8d201af 3394 unsigned int rotation = plane_state->base.rotation;
d2196774 3395 u32 stride = skl_plane_stride(fb, 0, rotation);
b63a16f6 3396 u32 surf_addr = plane_state->main.offset;
a8d201af 3397 int scaler_id = plane_state->scaler_id;
b63a16f6
VS
3398 int src_x = plane_state->main.x;
3399 int src_y = plane_state->main.y;
936e71e3
VS
3400 int src_w = drm_rect_width(&plane_state->base.src) >> 16;
3401 int src_h = drm_rect_height(&plane_state->base.src) >> 16;
3402 int dst_x = plane_state->base.dst.x1;
3403 int dst_y = plane_state->base.dst.y1;
3404 int dst_w = drm_rect_width(&plane_state->base.dst);
3405 int dst_h = drm_rect_height(&plane_state->base.dst);
70d21f0e 3406
6156a456
CK
3407 plane_ctl = PLANE_CTL_ENABLE |
3408 PLANE_CTL_PIPE_GAMMA_ENABLE |
3409 PLANE_CTL_PIPE_CSC_ENABLE;
3410
3411 plane_ctl |= skl_plane_ctl_format(fb->pixel_format);
3412 plane_ctl |= skl_plane_ctl_tiling(fb->modifier[0]);
3413 plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
6156a456
CK
3414 plane_ctl |= skl_plane_ctl_rotation(rotation);
3415
6687c906
VS
3416 /* Sizes are 0 based */
3417 src_w--;
3418 src_h--;
3419 dst_w--;
3420 dst_h--;
3421
4c0b8a8b
PZ
3422 intel_crtc->dspaddr_offset = surf_addr;
3423
6687c906
VS
3424 intel_crtc->adjusted_x = src_x;
3425 intel_crtc->adjusted_y = src_y;
2db3366b 3426
62e0fb88 3427 if (wm->dirty_pipes & drm_crtc_mask(&intel_crtc->base))
d8c0fafc 3428 skl_write_plane_wm(intel_crtc, p_wm, &wm->ddb, 0);
62e0fb88 3429
70d21f0e 3430 I915_WRITE(PLANE_CTL(pipe, 0), plane_ctl);
6687c906 3431 I915_WRITE(PLANE_OFFSET(pipe, 0), (src_y << 16) | src_x);
ef78ec94 3432 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
6687c906 3433 I915_WRITE(PLANE_SIZE(pipe, 0), (src_h << 16) | src_w);
6156a456
CK
3434
3435 if (scaler_id >= 0) {
3436 uint32_t ps_ctrl = 0;
3437
3438 WARN_ON(!dst_w || !dst_h);
3439 ps_ctrl = PS_SCALER_EN | PS_PLANE_SEL(0) |
3440 crtc_state->scaler_state.scalers[scaler_id].mode;
3441 I915_WRITE(SKL_PS_CTRL(pipe, scaler_id), ps_ctrl);
3442 I915_WRITE(SKL_PS_PWR_GATE(pipe, scaler_id), 0);
3443 I915_WRITE(SKL_PS_WIN_POS(pipe, scaler_id), (dst_x << 16) | dst_y);
3444 I915_WRITE(SKL_PS_WIN_SZ(pipe, scaler_id), (dst_w << 16) | dst_h);
3445 I915_WRITE(PLANE_POS(pipe, 0), 0);
3446 } else {
3447 I915_WRITE(PLANE_POS(pipe, 0), (dst_y << 16) | dst_x);
3448 }
3449
6687c906
VS
3450 I915_WRITE(PLANE_SURF(pipe, 0),
3451 intel_fb_gtt_offset(fb, rotation) + surf_addr);
70d21f0e
DL
3452
3453 POSTING_READ(PLANE_SURF(pipe, 0));
3454}
3455
a8d201af
ML
3456static void skylake_disable_primary_plane(struct drm_plane *primary,
3457 struct drm_crtc *crtc)
17638cd6
JB
3458{
3459 struct drm_device *dev = crtc->dev;
fac5e23e 3460 struct drm_i915_private *dev_priv = to_i915(dev);
62e0fb88 3461 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
d8c0fafc 3462 struct intel_crtc_state *cstate = to_intel_crtc_state(crtc->state);
3463 const struct skl_plane_wm *p_wm = &cstate->wm.skl.optimal.planes[0];
62e0fb88
L
3464 int pipe = intel_crtc->pipe;
3465
ccebc23b
L
3466 /*
3467 * We only populate skl_results on watermark updates, and if the
3468 * plane's visiblity isn't actually changing neither is its watermarks.
3469 */
3470 if (!crtc->primary->state->visible)
d8c0fafc 3471 skl_write_plane_wm(intel_crtc, p_wm,
3472 &dev_priv->wm.skl_results.ddb, 0);
17638cd6 3473
a8d201af
ML
3474 I915_WRITE(PLANE_CTL(pipe, 0), 0);
3475 I915_WRITE(PLANE_SURF(pipe, 0), 0);
3476 POSTING_READ(PLANE_SURF(pipe, 0));
3477}
29b9bde6 3478
a8d201af
ML
3479/* Assume fb object is pinned & idle & fenced and just update base pointers */
3480static int
3481intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
3482 int x, int y, enum mode_set_atomic state)
3483{
3484 /* Support for kgdboc is disabled, this needs a major rework. */
3485 DRM_ERROR("legacy panic handler not supported any more.\n");
3486
3487 return -ENODEV;
81255565
JB
3488}
3489
5a21b665
SV
3490static void intel_complete_page_flips(struct drm_i915_private *dev_priv)
3491{
3492 struct intel_crtc *crtc;
3493
91c8a326 3494 for_each_intel_crtc(&dev_priv->drm, crtc)
5a21b665
SV
3495 intel_finish_page_flip_cs(dev_priv, crtc->pipe);
3496}
3497
7514747d
VS
3498static void intel_update_primary_planes(struct drm_device *dev)
3499{
7514747d 3500 struct drm_crtc *crtc;
96a02917 3501
70e1e0ec 3502 for_each_crtc(dev, crtc) {
11c22da6 3503 struct intel_plane *plane = to_intel_plane(crtc->primary);
73974893
ML
3504 struct intel_plane_state *plane_state =
3505 to_intel_plane_state(plane->base.state);
11c22da6 3506
936e71e3 3507 if (plane_state->base.visible)
a8d201af
ML
3508 plane->update_plane(&plane->base,
3509 to_intel_crtc_state(crtc->state),
3510 plane_state);
73974893
ML
3511 }
3512}
3513
3514static int
3515__intel_display_resume(struct drm_device *dev,
3516 struct drm_atomic_state *state)
3517{
3518 struct drm_crtc_state *crtc_state;
3519 struct drm_crtc *crtc;
3520 int i, ret;
11c22da6 3521
73974893
ML
3522 intel_modeset_setup_hw_state(dev);
3523 i915_redisable_vga(dev);
3524
3525 if (!state)
3526 return 0;
3527
3528 for_each_crtc_in_state(state, crtc, crtc_state, i) {
3529 /*
3530 * Force recalculation even if we restore
3531 * current state. With fast modeset this may not result
3532 * in a modeset when the state is compatible.
3533 */
3534 crtc_state->mode_changed = true;
96a02917 3535 }
73974893
ML
3536
3537 /* ignore any reset values/BIOS leftovers in the WM registers */
3538 to_intel_atomic_state(state)->skip_intermediate_wm = true;
3539
3540 ret = drm_atomic_commit(state);
3541
3542 WARN_ON(ret == -EDEADLK);
3543 return ret;
96a02917
VS
3544}
3545
4ac2ba2f
VS
3546static bool gpu_reset_clobbers_display(struct drm_i915_private *dev_priv)
3547{
ae98104b
VS
3548 return intel_has_gpu_reset(dev_priv) &&
3549 INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv);
4ac2ba2f
VS
3550}
3551
c033666a 3552void intel_prepare_reset(struct drm_i915_private *dev_priv)
7514747d 3553{
73974893
ML
3554 struct drm_device *dev = &dev_priv->drm;
3555 struct drm_modeset_acquire_ctx *ctx = &dev_priv->reset_ctx;
3556 struct drm_atomic_state *state;
3557 int ret;
3558
73974893
ML
3559 /*
3560 * Need mode_config.mutex so that we don't
3561 * trample ongoing ->detect() and whatnot.
3562 */
3563 mutex_lock(&dev->mode_config.mutex);
3564 drm_modeset_acquire_init(ctx, 0);
3565 while (1) {
3566 ret = drm_modeset_lock_all_ctx(dev, ctx);
3567 if (ret != -EDEADLK)
3568 break;
3569
3570 drm_modeset_backoff(ctx);
3571 }
3572
3573 /* reset doesn't touch the display, but flips might get nuked anyway, */
522a63de 3574 if (!i915.force_reset_modeset_test &&
4ac2ba2f 3575 !gpu_reset_clobbers_display(dev_priv))
7514747d
VS
3576 return;
3577
f98ce92f
VS
3578 /*
3579 * Disabling the crtcs gracefully seems nicer. Also the
3580 * g33 docs say we should at least disable all the planes.
3581 */
73974893
ML
3582 state = drm_atomic_helper_duplicate_state(dev, ctx);
3583 if (IS_ERR(state)) {
3584 ret = PTR_ERR(state);
3585 state = NULL;
3586 DRM_ERROR("Duplicating state failed with %i\n", ret);
3587 goto err;
3588 }
3589
3590 ret = drm_atomic_helper_disable_all(dev, ctx);
3591 if (ret) {
3592 DRM_ERROR("Suspending crtc's failed with %i\n", ret);
3593 goto err;
3594 }
3595
3596 dev_priv->modeset_restore_state = state;
3597 state->acquire_ctx = ctx;
3598 return;
3599
3600err:
0853695c 3601 drm_atomic_state_put(state);
7514747d
VS
3602}
3603
c033666a 3604void intel_finish_reset(struct drm_i915_private *dev_priv)
7514747d 3605{
73974893
ML
3606 struct drm_device *dev = &dev_priv->drm;
3607 struct drm_modeset_acquire_ctx *ctx = &dev_priv->reset_ctx;
3608 struct drm_atomic_state *state = dev_priv->modeset_restore_state;
3609 int ret;
3610
5a21b665
SV
3611 /*
3612 * Flips in the rings will be nuked by the reset,
3613 * so complete all pending flips so that user space
3614 * will get its events and not get stuck.
3615 */
3616 intel_complete_page_flips(dev_priv);
3617
73974893
ML
3618 dev_priv->modeset_restore_state = NULL;
3619
7514747d 3620 /* reset doesn't touch the display */
4ac2ba2f 3621 if (!gpu_reset_clobbers_display(dev_priv)) {
522a63de
ML
3622 if (!state) {
3623 /*
3624 * Flips in the rings have been nuked by the reset,
3625 * so update the base address of all primary
3626 * planes to the the last fb to make sure we're
3627 * showing the correct fb after a reset.
3628 *
3629 * FIXME: Atomic will make this obsolete since we won't schedule
3630 * CS-based flips (which might get lost in gpu resets) any more.
3631 */
3632 intel_update_primary_planes(dev);
3633 } else {
3634 ret = __intel_display_resume(dev, state);
3635 if (ret)
3636 DRM_ERROR("Restoring old state failed with %i\n", ret);
3637 }
73974893
ML
3638 } else {
3639 /*
3640 * The display has been reset as well,
3641 * so need a full re-initialization.
3642 */
3643 intel_runtime_pm_disable_interrupts(dev_priv);
3644 intel_runtime_pm_enable_interrupts(dev_priv);
7514747d 3645
51f59205 3646 intel_pps_unlock_regs_wa(dev_priv);
73974893 3647 intel_modeset_init_hw(dev);
7514747d 3648
73974893
ML
3649 spin_lock_irq(&dev_priv->irq_lock);
3650 if (dev_priv->display.hpd_irq_setup)
3651 dev_priv->display.hpd_irq_setup(dev_priv);
3652 spin_unlock_irq(&dev_priv->irq_lock);
7514747d 3653
73974893
ML
3654 ret = __intel_display_resume(dev, state);
3655 if (ret)
3656 DRM_ERROR("Restoring old state failed with %i\n", ret);
7514747d 3657
73974893
ML
3658 intel_hpd_init(dev_priv);
3659 }
7514747d 3660
0853695c
CW
3661 if (state)
3662 drm_atomic_state_put(state);
73974893
ML
3663 drm_modeset_drop_locks(ctx);
3664 drm_modeset_acquire_fini(ctx);
3665 mutex_unlock(&dev->mode_config.mutex);
7514747d
VS
3666}
3667
8af29b0c
CW
3668static bool abort_flip_on_reset(struct intel_crtc *crtc)
3669{
3670 struct i915_gpu_error *error = &to_i915(crtc->base.dev)->gpu_error;
3671
3672 if (i915_reset_in_progress(error))
3673 return true;
3674
3675 if (crtc->reset_count != i915_reset_count(error))
3676 return true;
3677
3678 return false;
3679}
3680
7d5e3799
CW
3681static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
3682{
5a21b665
SV
3683 struct drm_device *dev = crtc->dev;
3684 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5a21b665
SV
3685 bool pending;
3686
8af29b0c 3687 if (abort_flip_on_reset(intel_crtc))
5a21b665
SV
3688 return false;
3689
3690 spin_lock_irq(&dev->event_lock);
3691 pending = to_intel_crtc(crtc)->flip_work != NULL;
3692 spin_unlock_irq(&dev->event_lock);
3693
3694 return pending;
7d5e3799
CW
3695}
3696
bfd16b2a
ML
3697static void intel_update_pipe_config(struct intel_crtc *crtc,
3698 struct intel_crtc_state *old_crtc_state)
e30e8f75
GP
3699{
3700 struct drm_device *dev = crtc->base.dev;
fac5e23e 3701 struct drm_i915_private *dev_priv = to_i915(dev);
bfd16b2a
ML
3702 struct intel_crtc_state *pipe_config =
3703 to_intel_crtc_state(crtc->base.state);
e30e8f75 3704
bfd16b2a
ML
3705 /* drm_atomic_helper_update_legacy_modeset_state might not be called. */
3706 crtc->base.mode = crtc->base.state->mode;
3707
3708 DRM_DEBUG_KMS("Updating pipe size %ix%i -> %ix%i\n",
3709 old_crtc_state->pipe_src_w, old_crtc_state->pipe_src_h,
3710 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
e30e8f75
GP
3711
3712 /*
3713 * Update pipe size and adjust fitter if needed: the reason for this is
3714 * that in compute_mode_changes we check the native mode (not the pfit
3715 * mode) to see if we can flip rather than do a full mode set. In the
3716 * fastboot case, we'll flip, but if we don't update the pipesrc and
3717 * pfit state, we'll end up with a big fb scanned out into the wrong
3718 * sized surface.
e30e8f75
GP
3719 */
3720
e30e8f75 3721 I915_WRITE(PIPESRC(crtc->pipe),
bfd16b2a
ML
3722 ((pipe_config->pipe_src_w - 1) << 16) |
3723 (pipe_config->pipe_src_h - 1));
3724
3725 /* on skylake this is done by detaching scalers */
3726 if (INTEL_INFO(dev)->gen >= 9) {
3727 skl_detach_scalers(crtc);
3728
3729 if (pipe_config->pch_pfit.enabled)
3730 skylake_pfit_enable(crtc);
6e266956 3731 } else if (HAS_PCH_SPLIT(dev_priv)) {
bfd16b2a
ML
3732 if (pipe_config->pch_pfit.enabled)
3733 ironlake_pfit_enable(crtc);
3734 else if (old_crtc_state->pch_pfit.enabled)
3735 ironlake_pfit_disable(crtc, true);
e30e8f75 3736 }
e30e8f75
GP
3737}
3738
5e84e1a4
ZW
3739static void intel_fdi_normal_train(struct drm_crtc *crtc)
3740{
3741 struct drm_device *dev = crtc->dev;
fac5e23e 3742 struct drm_i915_private *dev_priv = to_i915(dev);
5e84e1a4
ZW
3743 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3744 int pipe = intel_crtc->pipe;
f0f59a00
VS
3745 i915_reg_t reg;
3746 u32 temp;
5e84e1a4
ZW
3747
3748 /* enable normal train */
3749 reg = FDI_TX_CTL(pipe);
3750 temp = I915_READ(reg);
fd6b8f43 3751 if (IS_IVYBRIDGE(dev_priv)) {
357555c0
JB
3752 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3753 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
61e499bf
KP
3754 } else {
3755 temp &= ~FDI_LINK_TRAIN_NONE;
3756 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
357555c0 3757 }
5e84e1a4
ZW
3758 I915_WRITE(reg, temp);
3759
3760 reg = FDI_RX_CTL(pipe);
3761 temp = I915_READ(reg);
6e266956 3762 if (HAS_PCH_CPT(dev_priv)) {
5e84e1a4
ZW
3763 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3764 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
3765 } else {
3766 temp &= ~FDI_LINK_TRAIN_NONE;
3767 temp |= FDI_LINK_TRAIN_NONE;
3768 }
3769 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
3770
3771 /* wait one idle pattern time */
3772 POSTING_READ(reg);
3773 udelay(1000);
357555c0
JB
3774
3775 /* IVB wants error correction enabled */
fd6b8f43 3776 if (IS_IVYBRIDGE(dev_priv))
357555c0
JB
3777 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
3778 FDI_FE_ERRC_ENABLE);
5e84e1a4
ZW
3779}
3780
8db9d77b
ZW
3781/* The FDI link training functions for ILK/Ibexpeak. */
3782static void ironlake_fdi_link_train(struct drm_crtc *crtc)
3783{
3784 struct drm_device *dev = crtc->dev;
fac5e23e 3785 struct drm_i915_private *dev_priv = to_i915(dev);
8db9d77b
ZW
3786 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3787 int pipe = intel_crtc->pipe;
f0f59a00
VS
3788 i915_reg_t reg;
3789 u32 temp, tries;
8db9d77b 3790
1c8562f6 3791 /* FDI needs bits from pipe first */
0fc932b8 3792 assert_pipe_enabled(dev_priv, pipe);
0fc932b8 3793
e1a44743
AJ
3794 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3795 for train result */
5eddb70b
CW
3796 reg = FDI_RX_IMR(pipe);
3797 temp = I915_READ(reg);
e1a44743
AJ
3798 temp &= ~FDI_RX_SYMBOL_LOCK;
3799 temp &= ~FDI_RX_BIT_LOCK;
5eddb70b
CW
3800 I915_WRITE(reg, temp);
3801 I915_READ(reg);
e1a44743
AJ
3802 udelay(150);
3803
8db9d77b 3804 /* enable CPU FDI TX and PCH FDI RX */
5eddb70b
CW
3805 reg = FDI_TX_CTL(pipe);
3806 temp = I915_READ(reg);
627eb5a3 3807 temp &= ~FDI_DP_PORT_WIDTH_MASK;
6e3c9717 3808 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
8db9d77b
ZW
3809 temp &= ~FDI_LINK_TRAIN_NONE;
3810 temp |= FDI_LINK_TRAIN_PATTERN_1;
5eddb70b 3811 I915_WRITE(reg, temp | FDI_TX_ENABLE);
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_1;
5eddb70b
CW
3817 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3818
3819 POSTING_READ(reg);
8db9d77b
ZW
3820 udelay(150);
3821
5b2adf89 3822 /* Ironlake workaround, enable clock pointer after FDI enable*/
8f5718a6
SV
3823 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
3824 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
3825 FDI_RX_PHASE_SYNC_POINTER_EN);
5b2adf89 3826
5eddb70b 3827 reg = FDI_RX_IIR(pipe);
e1a44743 3828 for (tries = 0; tries < 5; tries++) {
5eddb70b 3829 temp = I915_READ(reg);
8db9d77b
ZW
3830 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3831
3832 if ((temp & FDI_RX_BIT_LOCK)) {
3833 DRM_DEBUG_KMS("FDI train 1 done.\n");
5eddb70b 3834 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
8db9d77b
ZW
3835 break;
3836 }
8db9d77b 3837 }
e1a44743 3838 if (tries == 5)
5eddb70b 3839 DRM_ERROR("FDI train 1 fail!\n");
8db9d77b
ZW
3840
3841 /* Train 2 */
5eddb70b
CW
3842 reg = FDI_TX_CTL(pipe);
3843 temp = I915_READ(reg);
8db9d77b
ZW
3844 temp &= ~FDI_LINK_TRAIN_NONE;
3845 temp |= FDI_LINK_TRAIN_PATTERN_2;
5eddb70b 3846 I915_WRITE(reg, temp);
8db9d77b 3847
5eddb70b
CW
3848 reg = FDI_RX_CTL(pipe);
3849 temp = I915_READ(reg);
8db9d77b
ZW
3850 temp &= ~FDI_LINK_TRAIN_NONE;
3851 temp |= FDI_LINK_TRAIN_PATTERN_2;
5eddb70b 3852 I915_WRITE(reg, temp);
8db9d77b 3853
5eddb70b
CW
3854 POSTING_READ(reg);
3855 udelay(150);
8db9d77b 3856
5eddb70b 3857 reg = FDI_RX_IIR(pipe);
e1a44743 3858 for (tries = 0; tries < 5; tries++) {
5eddb70b 3859 temp = I915_READ(reg);
8db9d77b
ZW
3860 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3861
3862 if (temp & FDI_RX_SYMBOL_LOCK) {
5eddb70b 3863 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
8db9d77b
ZW
3864 DRM_DEBUG_KMS("FDI train 2 done.\n");
3865 break;
3866 }
8db9d77b 3867 }
e1a44743 3868 if (tries == 5)
5eddb70b 3869 DRM_ERROR("FDI train 2 fail!\n");
8db9d77b
ZW
3870
3871 DRM_DEBUG_KMS("FDI train done\n");
5c5313c8 3872
8db9d77b
ZW
3873}
3874
0206e353 3875static const int snb_b_fdi_train_param[] = {
8db9d77b
ZW
3876 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
3877 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
3878 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
3879 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
3880};
3881
3882/* The FDI link training functions for SNB/Cougarpoint. */
3883static void gen6_fdi_link_train(struct drm_crtc *crtc)
3884{
3885 struct drm_device *dev = crtc->dev;
fac5e23e 3886 struct drm_i915_private *dev_priv = to_i915(dev);
8db9d77b
ZW
3887 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3888 int pipe = intel_crtc->pipe;
f0f59a00
VS
3889 i915_reg_t reg;
3890 u32 temp, i, retry;
8db9d77b 3891
e1a44743
AJ
3892 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3893 for train result */
5eddb70b
CW
3894 reg = FDI_RX_IMR(pipe);
3895 temp = I915_READ(reg);
e1a44743
AJ
3896 temp &= ~FDI_RX_SYMBOL_LOCK;
3897 temp &= ~FDI_RX_BIT_LOCK;
5eddb70b
CW
3898 I915_WRITE(reg, temp);
3899
3900 POSTING_READ(reg);
e1a44743
AJ
3901 udelay(150);
3902
8db9d77b 3903 /* enable CPU FDI TX and PCH FDI RX */
5eddb70b
CW
3904 reg = FDI_TX_CTL(pipe);
3905 temp = I915_READ(reg);
627eb5a3 3906 temp &= ~FDI_DP_PORT_WIDTH_MASK;
6e3c9717 3907 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
8db9d77b
ZW
3908 temp &= ~FDI_LINK_TRAIN_NONE;
3909 temp |= FDI_LINK_TRAIN_PATTERN_1;
3910 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3911 /* SNB-B */
3912 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
5eddb70b 3913 I915_WRITE(reg, temp | FDI_TX_ENABLE);
8db9d77b 3914
d74cf324
SV
3915 I915_WRITE(FDI_RX_MISC(pipe),
3916 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3917
5eddb70b
CW
3918 reg = FDI_RX_CTL(pipe);
3919 temp = I915_READ(reg);
6e266956 3920 if (HAS_PCH_CPT(dev_priv)) {
8db9d77b
ZW
3921 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3922 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3923 } else {
3924 temp &= ~FDI_LINK_TRAIN_NONE;
3925 temp |= FDI_LINK_TRAIN_PATTERN_1;
3926 }
5eddb70b
CW
3927 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3928
3929 POSTING_READ(reg);
8db9d77b
ZW
3930 udelay(150);
3931
0206e353 3932 for (i = 0; i < 4; i++) {
5eddb70b
CW
3933 reg = FDI_TX_CTL(pipe);
3934 temp = I915_READ(reg);
8db9d77b
ZW
3935 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3936 temp |= snb_b_fdi_train_param[i];
5eddb70b
CW
3937 I915_WRITE(reg, temp);
3938
3939 POSTING_READ(reg);
8db9d77b
ZW
3940 udelay(500);
3941
fa37d39e
SP
3942 for (retry = 0; retry < 5; retry++) {
3943 reg = FDI_RX_IIR(pipe);
3944 temp = I915_READ(reg);
3945 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3946 if (temp & FDI_RX_BIT_LOCK) {
3947 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3948 DRM_DEBUG_KMS("FDI train 1 done.\n");
3949 break;
3950 }
3951 udelay(50);
8db9d77b 3952 }
fa37d39e
SP
3953 if (retry < 5)
3954 break;
8db9d77b
ZW
3955 }
3956 if (i == 4)
5eddb70b 3957 DRM_ERROR("FDI train 1 fail!\n");
8db9d77b
ZW
3958
3959 /* Train 2 */
5eddb70b
CW
3960 reg = FDI_TX_CTL(pipe);
3961 temp = I915_READ(reg);
8db9d77b
ZW
3962 temp &= ~FDI_LINK_TRAIN_NONE;
3963 temp |= FDI_LINK_TRAIN_PATTERN_2;
5db94019 3964 if (IS_GEN6(dev_priv)) {
8db9d77b
ZW
3965 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3966 /* SNB-B */
3967 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
3968 }
5eddb70b 3969 I915_WRITE(reg, temp);
8db9d77b 3970
5eddb70b
CW
3971 reg = FDI_RX_CTL(pipe);
3972 temp = I915_READ(reg);
6e266956 3973 if (HAS_PCH_CPT(dev_priv)) {
8db9d77b
ZW
3974 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3975 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
3976 } else {
3977 temp &= ~FDI_LINK_TRAIN_NONE;
3978 temp |= FDI_LINK_TRAIN_PATTERN_2;
3979 }
5eddb70b
CW
3980 I915_WRITE(reg, temp);
3981
3982 POSTING_READ(reg);
8db9d77b
ZW
3983 udelay(150);
3984
0206e353 3985 for (i = 0; i < 4; i++) {
5eddb70b
CW
3986 reg = FDI_TX_CTL(pipe);
3987 temp = I915_READ(reg);
8db9d77b
ZW
3988 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3989 temp |= snb_b_fdi_train_param[i];
5eddb70b
CW
3990 I915_WRITE(reg, temp);
3991
3992 POSTING_READ(reg);
8db9d77b
ZW
3993 udelay(500);
3994
fa37d39e
SP
3995 for (retry = 0; retry < 5; retry++) {
3996 reg = FDI_RX_IIR(pipe);
3997 temp = I915_READ(reg);
3998 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3999 if (temp & FDI_RX_SYMBOL_LOCK) {
4000 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
4001 DRM_DEBUG_KMS("FDI train 2 done.\n");
4002 break;
4003 }
4004 udelay(50);
8db9d77b 4005 }
fa37d39e
SP
4006 if (retry < 5)
4007 break;
8db9d77b
ZW
4008 }
4009 if (i == 4)
5eddb70b 4010 DRM_ERROR("FDI train 2 fail!\n");
8db9d77b
ZW
4011
4012 DRM_DEBUG_KMS("FDI train done.\n");
4013}
4014
357555c0
JB
4015/* Manual link training for Ivy Bridge A0 parts */
4016static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
4017{
4018 struct drm_device *dev = crtc->dev;
fac5e23e 4019 struct drm_i915_private *dev_priv = to_i915(dev);
357555c0
JB
4020 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4021 int pipe = intel_crtc->pipe;
f0f59a00
VS
4022 i915_reg_t reg;
4023 u32 temp, i, j;
357555c0
JB
4024
4025 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
4026 for train result */
4027 reg = FDI_RX_IMR(pipe);
4028 temp = I915_READ(reg);
4029 temp &= ~FDI_RX_SYMBOL_LOCK;
4030 temp &= ~FDI_RX_BIT_LOCK;
4031 I915_WRITE(reg, temp);
4032
4033 POSTING_READ(reg);
4034 udelay(150);
4035
01a415fd
SV
4036 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
4037 I915_READ(FDI_RX_IIR(pipe)));
4038
139ccd3f
JB
4039 /* Try each vswing and preemphasis setting twice before moving on */
4040 for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
4041 /* disable first in case we need to retry */
4042 reg = FDI_TX_CTL(pipe);
4043 temp = I915_READ(reg);
4044 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
4045 temp &= ~FDI_TX_ENABLE;
4046 I915_WRITE(reg, temp);
357555c0 4047
139ccd3f
JB
4048 reg = FDI_RX_CTL(pipe);
4049 temp = I915_READ(reg);
4050 temp &= ~FDI_LINK_TRAIN_AUTO;
4051 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
4052 temp &= ~FDI_RX_ENABLE;
4053 I915_WRITE(reg, temp);
357555c0 4054
139ccd3f 4055 /* enable CPU FDI TX and PCH FDI RX */
357555c0
JB
4056 reg = FDI_TX_CTL(pipe);
4057 temp = I915_READ(reg);
139ccd3f 4058 temp &= ~FDI_DP_PORT_WIDTH_MASK;
6e3c9717 4059 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
139ccd3f 4060 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
357555c0 4061 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
139ccd3f
JB
4062 temp |= snb_b_fdi_train_param[j/2];
4063 temp |= FDI_COMPOSITE_SYNC;
4064 I915_WRITE(reg, temp | FDI_TX_ENABLE);
357555c0 4065
139ccd3f
JB
4066 I915_WRITE(FDI_RX_MISC(pipe),
4067 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
357555c0 4068
139ccd3f 4069 reg = FDI_RX_CTL(pipe);
357555c0 4070 temp = I915_READ(reg);
139ccd3f
JB
4071 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
4072 temp |= FDI_COMPOSITE_SYNC;
4073 I915_WRITE(reg, temp | FDI_RX_ENABLE);
357555c0 4074
139ccd3f
JB
4075 POSTING_READ(reg);
4076 udelay(1); /* should be 0.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_BIT_LOCK ||
4084 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
4085 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
4086 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
4087 i);
4088 break;
4089 }
4090 udelay(1); /* should be 0.5us */
4091 }
4092 if (i == 4) {
4093 DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
4094 continue;
4095 }
357555c0 4096
139ccd3f 4097 /* Train 2 */
357555c0
JB
4098 reg = FDI_TX_CTL(pipe);
4099 temp = I915_READ(reg);
139ccd3f
JB
4100 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
4101 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
4102 I915_WRITE(reg, temp);
4103
4104 reg = FDI_RX_CTL(pipe);
4105 temp = I915_READ(reg);
4106 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
4107 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
357555c0
JB
4108 I915_WRITE(reg, temp);
4109
4110 POSTING_READ(reg);
139ccd3f 4111 udelay(2); /* should be 1.5us */
357555c0 4112
139ccd3f
JB
4113 for (i = 0; i < 4; i++) {
4114 reg = FDI_RX_IIR(pipe);
4115 temp = I915_READ(reg);
4116 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
357555c0 4117
139ccd3f
JB
4118 if (temp & FDI_RX_SYMBOL_LOCK ||
4119 (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
4120 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
4121 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
4122 i);
4123 goto train_done;
4124 }
4125 udelay(2); /* should be 1.5us */
357555c0 4126 }
139ccd3f
JB
4127 if (i == 4)
4128 DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
357555c0 4129 }
357555c0 4130
139ccd3f 4131train_done:
357555c0
JB
4132 DRM_DEBUG_KMS("FDI train done.\n");
4133}
4134
88cefb6c 4135static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
2c07245f 4136{
88cefb6c 4137 struct drm_device *dev = intel_crtc->base.dev;
fac5e23e 4138 struct drm_i915_private *dev_priv = to_i915(dev);
2c07245f 4139 int pipe = intel_crtc->pipe;
f0f59a00
VS
4140 i915_reg_t reg;
4141 u32 temp;
c64e311e 4142
c98e9dcf 4143 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
5eddb70b
CW
4144 reg = FDI_RX_CTL(pipe);
4145 temp = I915_READ(reg);
627eb5a3 4146 temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
6e3c9717 4147 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
dfd07d72 4148 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
5eddb70b
CW
4149 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
4150
4151 POSTING_READ(reg);
c98e9dcf
JB
4152 udelay(200);
4153
4154 /* Switch from Rawclk to PCDclk */
5eddb70b
CW
4155 temp = I915_READ(reg);
4156 I915_WRITE(reg, temp | FDI_PCDCLK);
4157
4158 POSTING_READ(reg);
c98e9dcf
JB
4159 udelay(200);
4160
20749730
PZ
4161 /* Enable CPU FDI TX PLL, always on for Ironlake */
4162 reg = FDI_TX_CTL(pipe);
4163 temp = I915_READ(reg);
4164 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
4165 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
5eddb70b 4166
20749730
PZ
4167 POSTING_READ(reg);
4168 udelay(100);
6be4a607 4169 }
0e23b99d
JB
4170}
4171
88cefb6c
SV
4172static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
4173{
4174 struct drm_device *dev = intel_crtc->base.dev;
fac5e23e 4175 struct drm_i915_private *dev_priv = to_i915(dev);
88cefb6c 4176 int pipe = intel_crtc->pipe;
f0f59a00
VS
4177 i915_reg_t reg;
4178 u32 temp;
88cefb6c
SV
4179
4180 /* Switch from PCDclk to Rawclk */
4181 reg = FDI_RX_CTL(pipe);
4182 temp = I915_READ(reg);
4183 I915_WRITE(reg, temp & ~FDI_PCDCLK);
4184
4185 /* Disable CPU FDI TX PLL */
4186 reg = FDI_TX_CTL(pipe);
4187 temp = I915_READ(reg);
4188 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
4189
4190 POSTING_READ(reg);
4191 udelay(100);
4192
4193 reg = FDI_RX_CTL(pipe);
4194 temp = I915_READ(reg);
4195 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
4196
4197 /* Wait for the clocks to turn off. */
4198 POSTING_READ(reg);
4199 udelay(100);
4200}
4201
0fc932b8
JB
4202static void ironlake_fdi_disable(struct drm_crtc *crtc)
4203{
4204 struct drm_device *dev = crtc->dev;
fac5e23e 4205 struct drm_i915_private *dev_priv = to_i915(dev);
0fc932b8
JB
4206 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4207 int pipe = intel_crtc->pipe;
f0f59a00
VS
4208 i915_reg_t reg;
4209 u32 temp;
0fc932b8
JB
4210
4211 /* disable CPU FDI tx and PCH FDI rx */
4212 reg = FDI_TX_CTL(pipe);
4213 temp = I915_READ(reg);
4214 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
4215 POSTING_READ(reg);
4216
4217 reg = FDI_RX_CTL(pipe);
4218 temp = I915_READ(reg);
4219 temp &= ~(0x7 << 16);
dfd07d72 4220 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
0fc932b8
JB
4221 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
4222
4223 POSTING_READ(reg);
4224 udelay(100);
4225
4226 /* Ironlake workaround, disable clock pointer after downing FDI */
6e266956 4227 if (HAS_PCH_IBX(dev_priv))
6f06ce18 4228 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
0fc932b8
JB
4229
4230 /* still set train pattern 1 */
4231 reg = FDI_TX_CTL(pipe);
4232 temp = I915_READ(reg);
4233 temp &= ~FDI_LINK_TRAIN_NONE;
4234 temp |= FDI_LINK_TRAIN_PATTERN_1;
4235 I915_WRITE(reg, temp);
4236
4237 reg = FDI_RX_CTL(pipe);
4238 temp = I915_READ(reg);
6e266956 4239 if (HAS_PCH_CPT(dev_priv)) {
0fc932b8
JB
4240 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
4241 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
4242 } else {
4243 temp &= ~FDI_LINK_TRAIN_NONE;
4244 temp |= FDI_LINK_TRAIN_PATTERN_1;
4245 }
4246 /* BPC in FDI rx is consistent with that in PIPECONF */
4247 temp &= ~(0x07 << 16);
dfd07d72 4248 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
0fc932b8
JB
4249 I915_WRITE(reg, temp);
4250
4251 POSTING_READ(reg);
4252 udelay(100);
4253}
4254
5dce5b93
CW
4255bool intel_has_pending_fb_unpin(struct drm_device *dev)
4256{
0f0f74bc 4257 struct drm_i915_private *dev_priv = to_i915(dev);
5dce5b93
CW
4258 struct intel_crtc *crtc;
4259
4260 /* Note that we don't need to be called with mode_config.lock here
4261 * as our list of CRTC objects is static for the lifetime of the
4262 * device and so cannot disappear as we iterate. Similarly, we can
4263 * happily treat the predicates as racy, atomic checks as userspace
4264 * cannot claim and pin a new fb without at least acquring the
4265 * struct_mutex and so serialising with us.
4266 */
d3fcc808 4267 for_each_intel_crtc(dev, crtc) {
5dce5b93
CW
4268 if (atomic_read(&crtc->unpin_work_count) == 0)
4269 continue;
4270
5a21b665 4271 if (crtc->flip_work)
0f0f74bc 4272 intel_wait_for_vblank(dev_priv, crtc->pipe);
5dce5b93
CW
4273
4274 return true;
4275 }
4276
4277 return false;
4278}
4279
5a21b665 4280static void page_flip_completed(struct intel_crtc *intel_crtc)
d6bbafa1
CW
4281{
4282 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
5a21b665
SV
4283 struct intel_flip_work *work = intel_crtc->flip_work;
4284
4285 intel_crtc->flip_work = NULL;
d6bbafa1
CW
4286
4287 if (work->event)
560ce1dc 4288 drm_crtc_send_vblank_event(&intel_crtc->base, work->event);
d6bbafa1
CW
4289
4290 drm_crtc_vblank_put(&intel_crtc->base);
4291
5a21b665 4292 wake_up_all(&dev_priv->pending_flip_queue);
143f73b3 4293 queue_work(dev_priv->wq, &work->unpin_work);
5a21b665
SV
4294
4295 trace_i915_flip_complete(intel_crtc->plane,
4296 work->pending_flip_obj);
d6bbafa1
CW
4297}
4298
5008e874 4299static int intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
e6c3a2a6 4300{
0f91128d 4301 struct drm_device *dev = crtc->dev;
fac5e23e 4302 struct drm_i915_private *dev_priv = to_i915(dev);
5008e874 4303 long ret;
e6c3a2a6 4304
2c10d571 4305 WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
5008e874
ML
4306
4307 ret = wait_event_interruptible_timeout(
4308 dev_priv->pending_flip_queue,
4309 !intel_crtc_has_pending_flip(crtc),
4310 60*HZ);
4311
4312 if (ret < 0)
4313 return ret;
4314
5a21b665
SV
4315 if (ret == 0) {
4316 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4317 struct intel_flip_work *work;
4318
4319 spin_lock_irq(&dev->event_lock);
4320 work = intel_crtc->flip_work;
4321 if (work && !is_mmio_work(work)) {
4322 WARN_ONCE(1, "Removing stuck page flip\n");
4323 page_flip_completed(intel_crtc);
4324 }
4325 spin_unlock_irq(&dev->event_lock);
4326 }
5bb61643 4327
5008e874 4328 return 0;
e6c3a2a6
CW
4329}
4330
b7076546 4331void lpt_disable_iclkip(struct drm_i915_private *dev_priv)
060f02d8
VS
4332{
4333 u32 temp;
4334
4335 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
4336
4337 mutex_lock(&dev_priv->sb_lock);
4338
4339 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
4340 temp |= SBI_SSCCTL_DISABLE;
4341 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
4342
4343 mutex_unlock(&dev_priv->sb_lock);
4344}
4345
e615efe4
ED
4346/* Program iCLKIP clock to the desired frequency */
4347static void lpt_program_iclkip(struct drm_crtc *crtc)
4348{
64b46a06 4349 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
6e3c9717 4350 int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
e615efe4
ED
4351 u32 divsel, phaseinc, auxdiv, phasedir = 0;
4352 u32 temp;
4353
060f02d8 4354 lpt_disable_iclkip(dev_priv);
e615efe4 4355
64b46a06
VS
4356 /* The iCLK virtual clock root frequency is in MHz,
4357 * but the adjusted_mode->crtc_clock in in KHz. To get the
4358 * divisors, it is necessary to divide one by another, so we
4359 * convert the virtual clock precision to KHz here for higher
4360 * precision.
4361 */
4362 for (auxdiv = 0; auxdiv < 2; auxdiv++) {
e615efe4
ED
4363 u32 iclk_virtual_root_freq = 172800 * 1000;
4364 u32 iclk_pi_range = 64;
64b46a06 4365 u32 desired_divisor;
e615efe4 4366
64b46a06
VS
4367 desired_divisor = DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
4368 clock << auxdiv);
4369 divsel = (desired_divisor / iclk_pi_range) - 2;
4370 phaseinc = desired_divisor % iclk_pi_range;
e615efe4 4371
64b46a06
VS
4372 /*
4373 * Near 20MHz is a corner case which is
4374 * out of range for the 7-bit divisor
4375 */
4376 if (divsel <= 0x7f)
4377 break;
e615efe4
ED
4378 }
4379
4380 /* This should not happen with any sane values */
4381 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
4382 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
4383 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
4384 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
4385
4386 DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
12d7ceed 4387 clock,
e615efe4
ED
4388 auxdiv,
4389 divsel,
4390 phasedir,
4391 phaseinc);
4392
060f02d8
VS
4393 mutex_lock(&dev_priv->sb_lock);
4394
e615efe4 4395 /* Program SSCDIVINTPHASE6 */
988d6ee8 4396 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
e615efe4
ED
4397 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
4398 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
4399 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
4400 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
4401 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
4402 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
988d6ee8 4403 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
e615efe4
ED
4404
4405 /* Program SSCAUXDIV */
988d6ee8 4406 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
e615efe4
ED
4407 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
4408 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
988d6ee8 4409 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
e615efe4
ED
4410
4411 /* Enable modulator and associated divider */
988d6ee8 4412 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
e615efe4 4413 temp &= ~SBI_SSCCTL_DISABLE;
988d6ee8 4414 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
e615efe4 4415
060f02d8
VS
4416 mutex_unlock(&dev_priv->sb_lock);
4417
e615efe4
ED
4418 /* Wait for initialization time */
4419 udelay(24);
4420
4421 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
4422}
4423
8802e5b6
VS
4424int lpt_get_iclkip(struct drm_i915_private *dev_priv)
4425{
4426 u32 divsel, phaseinc, auxdiv;
4427 u32 iclk_virtual_root_freq = 172800 * 1000;
4428 u32 iclk_pi_range = 64;
4429 u32 desired_divisor;
4430 u32 temp;
4431
4432 if ((I915_READ(PIXCLK_GATE) & PIXCLK_GATE_UNGATE) == 0)
4433 return 0;
4434
4435 mutex_lock(&dev_priv->sb_lock);
4436
4437 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
4438 if (temp & SBI_SSCCTL_DISABLE) {
4439 mutex_unlock(&dev_priv->sb_lock);
4440 return 0;
4441 }
4442
4443 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
4444 divsel = (temp & SBI_SSCDIVINTPHASE_DIVSEL_MASK) >>
4445 SBI_SSCDIVINTPHASE_DIVSEL_SHIFT;
4446 phaseinc = (temp & SBI_SSCDIVINTPHASE_INCVAL_MASK) >>
4447 SBI_SSCDIVINTPHASE_INCVAL_SHIFT;
4448
4449 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
4450 auxdiv = (temp & SBI_SSCAUXDIV_FINALDIV2SEL_MASK) >>
4451 SBI_SSCAUXDIV_FINALDIV2SEL_SHIFT;
4452
4453 mutex_unlock(&dev_priv->sb_lock);
4454
4455 desired_divisor = (divsel + 2) * iclk_pi_range + phaseinc;
4456
4457 return DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
4458 desired_divisor << auxdiv);
4459}
4460
275f01b2
SV
4461static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
4462 enum pipe pch_transcoder)
4463{
4464 struct drm_device *dev = crtc->base.dev;
fac5e23e 4465 struct drm_i915_private *dev_priv = to_i915(dev);
6e3c9717 4466 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
275f01b2
SV
4467
4468 I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
4469 I915_READ(HTOTAL(cpu_transcoder)));
4470 I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
4471 I915_READ(HBLANK(cpu_transcoder)));
4472 I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
4473 I915_READ(HSYNC(cpu_transcoder)));
4474
4475 I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
4476 I915_READ(VTOTAL(cpu_transcoder)));
4477 I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
4478 I915_READ(VBLANK(cpu_transcoder)));
4479 I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
4480 I915_READ(VSYNC(cpu_transcoder)));
4481 I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
4482 I915_READ(VSYNCSHIFT(cpu_transcoder)));
4483}
4484
003632d9 4485static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
1fbc0d78 4486{
fac5e23e 4487 struct drm_i915_private *dev_priv = to_i915(dev);
1fbc0d78
SV
4488 uint32_t temp;
4489
4490 temp = I915_READ(SOUTH_CHICKEN1);
003632d9 4491 if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
1fbc0d78
SV
4492 return;
4493
4494 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
4495 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
4496
003632d9
ACO
4497 temp &= ~FDI_BC_BIFURCATION_SELECT;
4498 if (enable)
4499 temp |= FDI_BC_BIFURCATION_SELECT;
4500
4501 DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
1fbc0d78
SV
4502 I915_WRITE(SOUTH_CHICKEN1, temp);
4503 POSTING_READ(SOUTH_CHICKEN1);
4504}
4505
4506static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
4507{
4508 struct drm_device *dev = intel_crtc->base.dev;
1fbc0d78
SV
4509
4510 switch (intel_crtc->pipe) {
4511 case PIPE_A:
4512 break;
4513 case PIPE_B:
6e3c9717 4514 if (intel_crtc->config->fdi_lanes > 2)
003632d9 4515 cpt_set_fdi_bc_bifurcation(dev, false);
1fbc0d78 4516 else
003632d9 4517 cpt_set_fdi_bc_bifurcation(dev, true);
1fbc0d78
SV
4518
4519 break;
4520 case PIPE_C:
003632d9 4521 cpt_set_fdi_bc_bifurcation(dev, true);
1fbc0d78
SV
4522
4523 break;
4524 default:
4525 BUG();
4526 }
4527}
4528
c48b5305
VS
4529/* Return which DP Port should be selected for Transcoder DP control */
4530static enum port
4531intel_trans_dp_port_sel(struct drm_crtc *crtc)
4532{
4533 struct drm_device *dev = crtc->dev;
4534 struct intel_encoder *encoder;
4535
4536 for_each_encoder_on_crtc(dev, crtc, encoder) {
cca0502b 4537 if (encoder->type == INTEL_OUTPUT_DP ||
c48b5305
VS
4538 encoder->type == INTEL_OUTPUT_EDP)
4539 return enc_to_dig_port(&encoder->base)->port;
4540 }
4541
4542 return -1;
4543}
4544
f67a559d
JB
4545/*
4546 * Enable PCH resources required for PCH ports:
4547 * - PCH PLLs
4548 * - FDI training & RX/TX
4549 * - update transcoder timings
4550 * - DP transcoding bits
4551 * - transcoder
4552 */
4553static void ironlake_pch_enable(struct drm_crtc *crtc)
0e23b99d
JB
4554{
4555 struct drm_device *dev = crtc->dev;
fac5e23e 4556 struct drm_i915_private *dev_priv = to_i915(dev);
0e23b99d
JB
4557 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4558 int pipe = intel_crtc->pipe;
f0f59a00 4559 u32 temp;
2c07245f 4560
ab9412ba 4561 assert_pch_transcoder_disabled(dev_priv, pipe);
e7e164db 4562
fd6b8f43 4563 if (IS_IVYBRIDGE(dev_priv))
1fbc0d78
SV
4564 ivybridge_update_fdi_bc_bifurcation(intel_crtc);
4565
cd986abb
SV
4566 /* Write the TU size bits before fdi link training, so that error
4567 * detection works. */
4568 I915_WRITE(FDI_RX_TUSIZE1(pipe),
4569 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
4570
c98e9dcf 4571 /* For PCH output, training FDI link */
674cf967 4572 dev_priv->display.fdi_link_train(crtc);
2c07245f 4573
3ad8a208
SV
4574 /* We need to program the right clock selection before writing the pixel
4575 * mutliplier into the DPLL. */
6e266956 4576 if (HAS_PCH_CPT(dev_priv)) {
ee7b9f93 4577 u32 sel;
4b645f14 4578
c98e9dcf 4579 temp = I915_READ(PCH_DPLL_SEL);
11887397
SV
4580 temp |= TRANS_DPLL_ENABLE(pipe);
4581 sel = TRANS_DPLLB_SEL(pipe);
8106ddbd
ACO
4582 if (intel_crtc->config->shared_dpll ==
4583 intel_get_shared_dpll_by_id(dev_priv, DPLL_ID_PCH_PLL_B))
ee7b9f93
JB
4584 temp |= sel;
4585 else
4586 temp &= ~sel;
c98e9dcf 4587 I915_WRITE(PCH_DPLL_SEL, temp);
c98e9dcf 4588 }
5eddb70b 4589
3ad8a208
SV
4590 /* XXX: pch pll's can be enabled any time before we enable the PCH
4591 * transcoder, and we actually should do this to not upset any PCH
4592 * transcoder that already use the clock when we share it.
4593 *
4594 * Note that enable_shared_dpll tries to do the right thing, but
4595 * get_shared_dpll unconditionally resets the pll - we need that to have
4596 * the right LVDS enable sequence. */
85b3894f 4597 intel_enable_shared_dpll(intel_crtc);
3ad8a208 4598
d9b6cb56
JB
4599 /* set transcoder timing, panel must allow it */
4600 assert_panel_unlocked(dev_priv, pipe);
275f01b2 4601 ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
8db9d77b 4602
303b81e0 4603 intel_fdi_normal_train(crtc);
5e84e1a4 4604
c98e9dcf 4605 /* For PCH DP, enable TRANS_DP_CTL */
6e266956
TU
4606 if (HAS_PCH_CPT(dev_priv) &&
4607 intel_crtc_has_dp_encoder(intel_crtc->config)) {
9c4edaee
VS
4608 const struct drm_display_mode *adjusted_mode =
4609 &intel_crtc->config->base.adjusted_mode;
dfd07d72 4610 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
f0f59a00 4611 i915_reg_t reg = TRANS_DP_CTL(pipe);
5eddb70b
CW
4612 temp = I915_READ(reg);
4613 temp &= ~(TRANS_DP_PORT_SEL_MASK |
220cad3c
EA
4614 TRANS_DP_SYNC_MASK |
4615 TRANS_DP_BPC_MASK);
e3ef4479 4616 temp |= TRANS_DP_OUTPUT_ENABLE;
9325c9f0 4617 temp |= bpc << 9; /* same format but at 11:9 */
c98e9dcf 4618
9c4edaee 4619 if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
5eddb70b 4620 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
9c4edaee 4621 if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
5eddb70b 4622 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
c98e9dcf
JB
4623
4624 switch (intel_trans_dp_port_sel(crtc)) {
c48b5305 4625 case PORT_B:
5eddb70b 4626 temp |= TRANS_DP_PORT_SEL_B;
c98e9dcf 4627 break;
c48b5305 4628 case PORT_C:
5eddb70b 4629 temp |= TRANS_DP_PORT_SEL_C;
c98e9dcf 4630 break;
c48b5305 4631 case PORT_D:
5eddb70b 4632 temp |= TRANS_DP_PORT_SEL_D;
c98e9dcf
JB
4633 break;
4634 default:
e95d41e1 4635 BUG();
32f9d658 4636 }
2c07245f 4637
5eddb70b 4638 I915_WRITE(reg, temp);
6be4a607 4639 }
b52eb4dc 4640
b8a4f404 4641 ironlake_enable_pch_transcoder(dev_priv, pipe);
f67a559d
JB
4642}
4643
1507e5bd
PZ
4644static void lpt_pch_enable(struct drm_crtc *crtc)
4645{
4646 struct drm_device *dev = crtc->dev;
fac5e23e 4647 struct drm_i915_private *dev_priv = to_i915(dev);
1507e5bd 4648 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6e3c9717 4649 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
1507e5bd 4650
ab9412ba 4651 assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
1507e5bd 4652
8c52b5e8 4653 lpt_program_iclkip(crtc);
1507e5bd 4654
0540e488 4655 /* Set transcoder timing. */
275f01b2 4656 ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
1507e5bd 4657
937bb610 4658 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
f67a559d
JB
4659}
4660
a1520318 4661static void cpt_verify_modeset(struct drm_device *dev, int pipe)
d4270e57 4662{
fac5e23e 4663 struct drm_i915_private *dev_priv = to_i915(dev);
f0f59a00 4664 i915_reg_t dslreg = PIPEDSL(pipe);
d4270e57
JB
4665 u32 temp;
4666
4667 temp = I915_READ(dslreg);
4668 udelay(500);
4669 if (wait_for(I915_READ(dslreg) != temp, 5)) {
d4270e57 4670 if (wait_for(I915_READ(dslreg) != temp, 5))
84f44ce7 4671 DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
d4270e57
JB
4672 }
4673}
4674
86adf9d7
ML
4675static int
4676skl_update_scaler(struct intel_crtc_state *crtc_state, bool force_detach,
4677 unsigned scaler_user, int *scaler_id, unsigned int rotation,
4678 int src_w, int src_h, int dst_w, int dst_h)
a1b2278e 4679{
86adf9d7
ML
4680 struct intel_crtc_scaler_state *scaler_state =
4681 &crtc_state->scaler_state;
4682 struct intel_crtc *intel_crtc =
4683 to_intel_crtc(crtc_state->base.crtc);
a1b2278e 4684 int need_scaling;
6156a456 4685
bd2ef25d 4686 need_scaling = drm_rotation_90_or_270(rotation) ?
6156a456
CK
4687 (src_h != dst_w || src_w != dst_h):
4688 (src_w != dst_w || src_h != dst_h);
a1b2278e
CK
4689
4690 /*
4691 * if plane is being disabled or scaler is no more required or force detach
4692 * - free scaler binded to this plane/crtc
4693 * - in order to do this, update crtc->scaler_usage
4694 *
4695 * Here scaler state in crtc_state is set free so that
4696 * scaler can be assigned to other user. Actual register
4697 * update to free the scaler is done in plane/panel-fit programming.
4698 * For this purpose crtc/plane_state->scaler_id isn't reset here.
4699 */
86adf9d7 4700 if (force_detach || !need_scaling) {
a1b2278e 4701 if (*scaler_id >= 0) {
86adf9d7 4702 scaler_state->scaler_users &= ~(1 << scaler_user);
a1b2278e
CK
4703 scaler_state->scalers[*scaler_id].in_use = 0;
4704
86adf9d7
ML
4705 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4706 "Staged freeing scaler id %d scaler_users = 0x%x\n",
4707 intel_crtc->pipe, scaler_user, *scaler_id,
a1b2278e
CK
4708 scaler_state->scaler_users);
4709 *scaler_id = -1;
4710 }
4711 return 0;
4712 }
4713
4714 /* range checks */
4715 if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
4716 dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
4717
4718 src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
4719 dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
86adf9d7 4720 DRM_DEBUG_KMS("scaler_user index %u.%u: src %ux%u dst %ux%u "
a1b2278e 4721 "size is out of scaler range\n",
86adf9d7 4722 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h);
a1b2278e
CK
4723 return -EINVAL;
4724 }
4725
86adf9d7
ML
4726 /* mark this plane as a scaler user in crtc_state */
4727 scaler_state->scaler_users |= (1 << scaler_user);
4728 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4729 "staged scaling request for %ux%u->%ux%u scaler_users = 0x%x\n",
4730 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h,
4731 scaler_state->scaler_users);
4732
4733 return 0;
4734}
4735
4736/**
4737 * skl_update_scaler_crtc - Stages update to scaler state for a given crtc.
4738 *
4739 * @state: crtc's scaler state
86adf9d7
ML
4740 *
4741 * Return
4742 * 0 - scaler_usage updated successfully
4743 * error - requested scaling cannot be supported or other error condition
4744 */
e435d6e5 4745int skl_update_scaler_crtc(struct intel_crtc_state *state)
86adf9d7
ML
4746{
4747 struct intel_crtc *intel_crtc = to_intel_crtc(state->base.crtc);
7c5f93b0 4748 const struct drm_display_mode *adjusted_mode = &state->base.adjusted_mode;
86adf9d7 4749
78108b7c
VS
4750 DRM_DEBUG_KMS("Updating scaler for [CRTC:%d:%s] scaler_user index %u.%u\n",
4751 intel_crtc->base.base.id, intel_crtc->base.name,
4752 intel_crtc->pipe, SKL_CRTC_INDEX);
86adf9d7 4753
e435d6e5 4754 return skl_update_scaler(state, !state->base.active, SKL_CRTC_INDEX,
31ad61e4 4755 &state->scaler_state.scaler_id, DRM_ROTATE_0,
86adf9d7 4756 state->pipe_src_w, state->pipe_src_h,
aad941d5 4757 adjusted_mode->crtc_hdisplay, adjusted_mode->crtc_vdisplay);
86adf9d7
ML
4758}
4759
4760/**
4761 * skl_update_scaler_plane - Stages update to scaler state for a given plane.
4762 *
4763 * @state: crtc's scaler state
86adf9d7
ML
4764 * @plane_state: atomic plane state to update
4765 *
4766 * Return
4767 * 0 - scaler_usage updated successfully
4768 * error - requested scaling cannot be supported or other error condition
4769 */
da20eabd
ML
4770static int skl_update_scaler_plane(struct intel_crtc_state *crtc_state,
4771 struct intel_plane_state *plane_state)
86adf9d7
ML
4772{
4773
4774 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
da20eabd
ML
4775 struct intel_plane *intel_plane =
4776 to_intel_plane(plane_state->base.plane);
86adf9d7
ML
4777 struct drm_framebuffer *fb = plane_state->base.fb;
4778 int ret;
4779
936e71e3 4780 bool force_detach = !fb || !plane_state->base.visible;
86adf9d7 4781
72660ce0
VS
4782 DRM_DEBUG_KMS("Updating scaler for [PLANE:%d:%s] scaler_user index %u.%u\n",
4783 intel_plane->base.base.id, intel_plane->base.name,
4784 intel_crtc->pipe, drm_plane_index(&intel_plane->base));
86adf9d7
ML
4785
4786 ret = skl_update_scaler(crtc_state, force_detach,
4787 drm_plane_index(&intel_plane->base),
4788 &plane_state->scaler_id,
4789 plane_state->base.rotation,
936e71e3
VS
4790 drm_rect_width(&plane_state->base.src) >> 16,
4791 drm_rect_height(&plane_state->base.src) >> 16,
4792 drm_rect_width(&plane_state->base.dst),
4793 drm_rect_height(&plane_state->base.dst));
86adf9d7
ML
4794
4795 if (ret || plane_state->scaler_id < 0)
4796 return ret;
4797
a1b2278e 4798 /* check colorkey */
818ed961 4799 if (plane_state->ckey.flags != I915_SET_COLORKEY_NONE) {
72660ce0
VS
4800 DRM_DEBUG_KMS("[PLANE:%d:%s] scaling with color key not allowed",
4801 intel_plane->base.base.id,
4802 intel_plane->base.name);
a1b2278e
CK
4803 return -EINVAL;
4804 }
4805
4806 /* Check src format */
86adf9d7
ML
4807 switch (fb->pixel_format) {
4808 case DRM_FORMAT_RGB565:
4809 case DRM_FORMAT_XBGR8888:
4810 case DRM_FORMAT_XRGB8888:
4811 case DRM_FORMAT_ABGR8888:
4812 case DRM_FORMAT_ARGB8888:
4813 case DRM_FORMAT_XRGB2101010:
4814 case DRM_FORMAT_XBGR2101010:
4815 case DRM_FORMAT_YUYV:
4816 case DRM_FORMAT_YVYU:
4817 case DRM_FORMAT_UYVY:
4818 case DRM_FORMAT_VYUY:
4819 break;
4820 default:
72660ce0
VS
4821 DRM_DEBUG_KMS("[PLANE:%d:%s] FB:%d unsupported scaling format 0x%x\n",
4822 intel_plane->base.base.id, intel_plane->base.name,
4823 fb->base.id, fb->pixel_format);
86adf9d7 4824 return -EINVAL;
a1b2278e
CK
4825 }
4826
a1b2278e
CK
4827 return 0;
4828}
4829
e435d6e5
ML
4830static void skylake_scaler_disable(struct intel_crtc *crtc)
4831{
4832 int i;
4833
4834 for (i = 0; i < crtc->num_scalers; i++)
4835 skl_detach_scaler(crtc, i);
4836}
4837
4838static void skylake_pfit_enable(struct intel_crtc *crtc)
bd2e244f
JB
4839{
4840 struct drm_device *dev = crtc->base.dev;
fac5e23e 4841 struct drm_i915_private *dev_priv = to_i915(dev);
bd2e244f 4842 int pipe = crtc->pipe;
a1b2278e
CK
4843 struct intel_crtc_scaler_state *scaler_state =
4844 &crtc->config->scaler_state;
4845
4846 DRM_DEBUG_KMS("for crtc_state = %p\n", crtc->config);
4847
6e3c9717 4848 if (crtc->config->pch_pfit.enabled) {
a1b2278e
CK
4849 int id;
4850
4851 if (WARN_ON(crtc->config->scaler_state.scaler_id < 0)) {
4852 DRM_ERROR("Requesting pfit without getting a scaler first\n");
4853 return;
4854 }
4855
4856 id = scaler_state->scaler_id;
4857 I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
4858 PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
4859 I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
4860 I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
4861
4862 DRM_DEBUG_KMS("for crtc_state = %p scaler_id = %d\n", crtc->config, id);
bd2e244f
JB
4863 }
4864}
4865
b074cec8
JB
4866static void ironlake_pfit_enable(struct intel_crtc *crtc)
4867{
4868 struct drm_device *dev = crtc->base.dev;
fac5e23e 4869 struct drm_i915_private *dev_priv = to_i915(dev);
b074cec8
JB
4870 int pipe = crtc->pipe;
4871
6e3c9717 4872 if (crtc->config->pch_pfit.enabled) {
b074cec8
JB
4873 /* Force use of hard-coded filter coefficients
4874 * as some pre-programmed values are broken,
4875 * e.g. x201.
4876 */
fd6b8f43 4877 if (IS_IVYBRIDGE(dev_priv) || IS_HASWELL(dev_priv))
b074cec8
JB
4878 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
4879 PF_PIPE_SEL_IVB(pipe));
4880 else
4881 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
6e3c9717
ACO
4882 I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
4883 I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
d4270e57
JB
4884 }
4885}
4886
20bc8673 4887void hsw_enable_ips(struct intel_crtc *crtc)
d77e4531 4888{
cea165c3 4889 struct drm_device *dev = crtc->base.dev;
fac5e23e 4890 struct drm_i915_private *dev_priv = to_i915(dev);
d77e4531 4891
6e3c9717 4892 if (!crtc->config->ips_enabled)
d77e4531
PZ
4893 return;
4894
307e4498
ML
4895 /*
4896 * We can only enable IPS after we enable a plane and wait for a vblank
4897 * This function is called from post_plane_update, which is run after
4898 * a vblank wait.
4899 */
cea165c3 4900
d77e4531 4901 assert_plane_enabled(dev_priv, crtc->plane);
8652744b 4902 if (IS_BROADWELL(dev_priv)) {
2a114cc1
BW
4903 mutex_lock(&dev_priv->rps.hw_lock);
4904 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
4905 mutex_unlock(&dev_priv->rps.hw_lock);
4906 /* Quoting Art Runyan: "its not safe to expect any particular
4907 * value in IPS_CTL bit 31 after enabling IPS through the
e59150dc
JB
4908 * mailbox." Moreover, the mailbox may return a bogus state,
4909 * so we need to just enable it and continue on.
2a114cc1
BW
4910 */
4911 } else {
4912 I915_WRITE(IPS_CTL, IPS_ENABLE);
4913 /* The bit only becomes 1 in the next vblank, so this wait here
4914 * is essentially intel_wait_for_vblank. If we don't have this
4915 * and don't wait for vblanks until the end of crtc_enable, then
4916 * the HW state readout code will complain that the expected
4917 * IPS_CTL value is not the one we read. */
2ec9ba3c
CW
4918 if (intel_wait_for_register(dev_priv,
4919 IPS_CTL, IPS_ENABLE, IPS_ENABLE,
4920 50))
2a114cc1
BW
4921 DRM_ERROR("Timed out waiting for IPS enable\n");
4922 }
d77e4531
PZ
4923}
4924
20bc8673 4925void hsw_disable_ips(struct intel_crtc *crtc)
d77e4531
PZ
4926{
4927 struct drm_device *dev = crtc->base.dev;
fac5e23e 4928 struct drm_i915_private *dev_priv = to_i915(dev);
d77e4531 4929
6e3c9717 4930 if (!crtc->config->ips_enabled)
d77e4531
PZ
4931 return;
4932
4933 assert_plane_enabled(dev_priv, crtc->plane);
8652744b 4934 if (IS_BROADWELL(dev_priv)) {
2a114cc1
BW
4935 mutex_lock(&dev_priv->rps.hw_lock);
4936 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
4937 mutex_unlock(&dev_priv->rps.hw_lock);
23d0b130 4938 /* wait for pcode to finish disabling IPS, which may take up to 42ms */
b85c1ecf
CW
4939 if (intel_wait_for_register(dev_priv,
4940 IPS_CTL, IPS_ENABLE, 0,
4941 42))
23d0b130 4942 DRM_ERROR("Timed out waiting for IPS disable\n");
e59150dc 4943 } else {
2a114cc1 4944 I915_WRITE(IPS_CTL, 0);
e59150dc
JB
4945 POSTING_READ(IPS_CTL);
4946 }
d77e4531
PZ
4947
4948 /* We need to wait for a vblank before we can disable the plane. */
0f0f74bc 4949 intel_wait_for_vblank(dev_priv, crtc->pipe);
d77e4531
PZ
4950}
4951
7cac945f 4952static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
d3eedb1a 4953{
7cac945f 4954 if (intel_crtc->overlay) {
d3eedb1a 4955 struct drm_device *dev = intel_crtc->base.dev;
fac5e23e 4956 struct drm_i915_private *dev_priv = to_i915(dev);
d3eedb1a
VS
4957
4958 mutex_lock(&dev->struct_mutex);
4959 dev_priv->mm.interruptible = false;
4960 (void) intel_overlay_switch_off(intel_crtc->overlay);
4961 dev_priv->mm.interruptible = true;
4962 mutex_unlock(&dev->struct_mutex);
4963 }
4964
4965 /* Let userspace switch the overlay on again. In most cases userspace
4966 * has to recompute where to put it anyway.
4967 */
4968}
4969
87d4300a
ML
4970/**
4971 * intel_post_enable_primary - Perform operations after enabling primary plane
4972 * @crtc: the CRTC whose primary plane was just enabled
4973 *
4974 * Performs potentially sleeping operations that must be done after the primary
4975 * plane is enabled, such as updating FBC and IPS. Note that this may be
4976 * called due to an explicit primary plane update, or due to an implicit
4977 * re-enable that is caused when a sprite plane is updated to no longer
4978 * completely hide the primary plane.
4979 */
4980static void
4981intel_post_enable_primary(struct drm_crtc *crtc)
a5c4d7bc
VS
4982{
4983 struct drm_device *dev = crtc->dev;
fac5e23e 4984 struct drm_i915_private *dev_priv = to_i915(dev);
a5c4d7bc
VS
4985 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4986 int pipe = intel_crtc->pipe;
a5c4d7bc 4987
87d4300a
ML
4988 /*
4989 * FIXME IPS should be fine as long as one plane is
4990 * enabled, but in practice it seems to have problems
4991 * when going from primary only to sprite only and vice
4992 * versa.
4993 */
a5c4d7bc
VS
4994 hsw_enable_ips(intel_crtc);
4995
f99d7069 4996 /*
87d4300a
ML
4997 * Gen2 reports pipe underruns whenever all planes are disabled.
4998 * So don't enable underrun reporting before at least some planes
4999 * are enabled.
5000 * FIXME: Need to fix the logic to work when we turn off all planes
5001 * but leave the pipe running.
f99d7069 5002 */
5db94019 5003 if (IS_GEN2(dev_priv))
87d4300a
ML
5004 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
5005
aca7b684
VS
5006 /* Underruns don't always raise interrupts, so check manually. */
5007 intel_check_cpu_fifo_underruns(dev_priv);
5008 intel_check_pch_fifo_underruns(dev_priv);
a5c4d7bc
VS
5009}
5010
2622a081 5011/* FIXME move all this to pre_plane_update() with proper state tracking */
87d4300a
ML
5012static void
5013intel_pre_disable_primary(struct drm_crtc *crtc)
a5c4d7bc
VS
5014{
5015 struct drm_device *dev = crtc->dev;
fac5e23e 5016 struct drm_i915_private *dev_priv = to_i915(dev);
a5c4d7bc
VS
5017 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5018 int pipe = intel_crtc->pipe;
a5c4d7bc 5019
87d4300a
ML
5020 /*
5021 * Gen2 reports pipe underruns whenever all planes are disabled.
5022 * So diasble underrun reporting before all the planes get disabled.
5023 * FIXME: Need to fix the logic to work when we turn off all planes
5024 * but leave the pipe running.
5025 */
5db94019 5026 if (IS_GEN2(dev_priv))
87d4300a 5027 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
a5c4d7bc 5028
2622a081
VS
5029 /*
5030 * FIXME IPS should be fine as long as one plane is
5031 * enabled, but in practice it seems to have problems
5032 * when going from primary only to sprite only and vice
5033 * versa.
5034 */
5035 hsw_disable_ips(intel_crtc);
5036}
5037
5038/* FIXME get rid of this and use pre_plane_update */
5039static void
5040intel_pre_disable_primary_noatomic(struct drm_crtc *crtc)
5041{
5042 struct drm_device *dev = crtc->dev;
fac5e23e 5043 struct drm_i915_private *dev_priv = to_i915(dev);
2622a081
VS
5044 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5045 int pipe = intel_crtc->pipe;
5046
5047 intel_pre_disable_primary(crtc);
5048
87d4300a
ML
5049 /*
5050 * Vblank time updates from the shadow to live plane control register
5051 * are blocked if the memory self-refresh mode is active at that
5052 * moment. So to make sure the plane gets truly disabled, disable
5053 * first the self-refresh mode. The self-refresh enable bit in turn
5054 * will be checked/applied by the HW only at the next frame start
5055 * event which is after the vblank start event, so we need to have a
5056 * wait-for-vblank between disabling the plane and the pipe.
5057 */
49cff963 5058 if (HAS_GMCH_DISPLAY(dev_priv)) {
87d4300a 5059 intel_set_memory_cxsr(dev_priv, false);
262cd2e1 5060 dev_priv->wm.vlv.cxsr = false;
0f0f74bc 5061 intel_wait_for_vblank(dev_priv, pipe);
262cd2e1 5062 }
87d4300a
ML
5063}
5064
5a21b665
SV
5065static void intel_post_plane_update(struct intel_crtc_state *old_crtc_state)
5066{
5067 struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
5068 struct drm_atomic_state *old_state = old_crtc_state->base.state;
5069 struct intel_crtc_state *pipe_config =
5070 to_intel_crtc_state(crtc->base.state);
5a21b665
SV
5071 struct drm_plane *primary = crtc->base.primary;
5072 struct drm_plane_state *old_pri_state =
5073 drm_atomic_get_existing_plane_state(old_state, primary);
5074
5748b6a1 5075 intel_frontbuffer_flip(to_i915(crtc->base.dev), pipe_config->fb_bits);
5a21b665
SV
5076
5077 crtc->wm.cxsr_allowed = true;
5078
5079 if (pipe_config->update_wm_post && pipe_config->base.active)
432081bc 5080 intel_update_watermarks(crtc);
5a21b665
SV
5081
5082 if (old_pri_state) {
5083 struct intel_plane_state *primary_state =
5084 to_intel_plane_state(primary->state);
5085 struct intel_plane_state *old_primary_state =
5086 to_intel_plane_state(old_pri_state);
5087
5088 intel_fbc_post_update(crtc);
5089
936e71e3 5090 if (primary_state->base.visible &&
5a21b665 5091 (needs_modeset(&pipe_config->base) ||
936e71e3 5092 !old_primary_state->base.visible))
5a21b665
SV
5093 intel_post_enable_primary(&crtc->base);
5094 }
5095}
5096
5c74cd73 5097static void intel_pre_plane_update(struct intel_crtc_state *old_crtc_state)
ac21b225 5098{
5c74cd73 5099 struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
ac21b225 5100 struct drm_device *dev = crtc->base.dev;
fac5e23e 5101 struct drm_i915_private *dev_priv = to_i915(dev);
ab1d3a0e
ML
5102 struct intel_crtc_state *pipe_config =
5103 to_intel_crtc_state(crtc->base.state);
5c74cd73
ML
5104 struct drm_atomic_state *old_state = old_crtc_state->base.state;
5105 struct drm_plane *primary = crtc->base.primary;
5106 struct drm_plane_state *old_pri_state =
5107 drm_atomic_get_existing_plane_state(old_state, primary);
5108 bool modeset = needs_modeset(&pipe_config->base);
ac21b225 5109
5c74cd73
ML
5110 if (old_pri_state) {
5111 struct intel_plane_state *primary_state =
5112 to_intel_plane_state(primary->state);
5113 struct intel_plane_state *old_primary_state =
5114 to_intel_plane_state(old_pri_state);
5115
faf68d92 5116 intel_fbc_pre_update(crtc, pipe_config, primary_state);
31ae71fc 5117
936e71e3
VS
5118 if (old_primary_state->base.visible &&
5119 (modeset || !primary_state->base.visible))
5c74cd73
ML
5120 intel_pre_disable_primary(&crtc->base);
5121 }
852eb00d 5122
49cff963 5123 if (pipe_config->disable_cxsr && HAS_GMCH_DISPLAY(dev_priv)) {
852eb00d 5124 crtc->wm.cxsr_allowed = false;
2dfd178d 5125
2622a081
VS
5126 /*
5127 * Vblank time updates from the shadow to live plane control register
5128 * are blocked if the memory self-refresh mode is active at that
5129 * moment. So to make sure the plane gets truly disabled, disable
5130 * first the self-refresh mode. The self-refresh enable bit in turn
5131 * will be checked/applied by the HW only at the next frame start
5132 * event which is after the vblank start event, so we need to have a
5133 * wait-for-vblank between disabling the plane and the pipe.
5134 */
5135 if (old_crtc_state->base.active) {
2dfd178d 5136 intel_set_memory_cxsr(dev_priv, false);
2622a081 5137 dev_priv->wm.vlv.cxsr = false;
0f0f74bc 5138 intel_wait_for_vblank(dev_priv, crtc->pipe);
2622a081 5139 }
852eb00d 5140 }
92826fcd 5141
ed4a6a7c
MR
5142 /*
5143 * IVB workaround: must disable low power watermarks for at least
5144 * one frame before enabling scaling. LP watermarks can be re-enabled
5145 * when scaling is disabled.
5146 *
5147 * WaCxSRDisabledForSpriteScaling:ivb
5148 */
5149 if (pipe_config->disable_lp_wm) {
5150 ilk_disable_lp_wm(dev);
0f0f74bc 5151 intel_wait_for_vblank(dev_priv, crtc->pipe);
ed4a6a7c
MR
5152 }
5153
5154 /*
5155 * If we're doing a modeset, we're done. No need to do any pre-vblank
5156 * watermark programming here.
5157 */
5158 if (needs_modeset(&pipe_config->base))
5159 return;
5160
5161 /*
5162 * For platforms that support atomic watermarks, program the
5163 * 'intermediate' watermarks immediately. On pre-gen9 platforms, these
5164 * will be the intermediate values that are safe for both pre- and
5165 * post- vblank; when vblank happens, the 'active' values will be set
5166 * to the final 'target' values and we'll do this again to get the
5167 * optimal watermarks. For gen9+ platforms, the values we program here
5168 * will be the final target values which will get automatically latched
5169 * at vblank time; no further programming will be necessary.
5170 *
5171 * If a platform hasn't been transitioned to atomic watermarks yet,
5172 * we'll continue to update watermarks the old way, if flags tell
5173 * us to.
5174 */
5175 if (dev_priv->display.initial_watermarks != NULL)
5176 dev_priv->display.initial_watermarks(pipe_config);
caed361d 5177 else if (pipe_config->update_wm_pre)
432081bc 5178 intel_update_watermarks(crtc);
ac21b225
ML
5179}
5180
d032ffa0 5181static void intel_crtc_disable_planes(struct drm_crtc *crtc, unsigned plane_mask)
87d4300a
ML
5182{
5183 struct drm_device *dev = crtc->dev;
5184 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
d032ffa0 5185 struct drm_plane *p;
87d4300a
ML
5186 int pipe = intel_crtc->pipe;
5187
7cac945f 5188 intel_crtc_dpms_overlay_disable(intel_crtc);
27321ae8 5189
d032ffa0
ML
5190 drm_for_each_plane_mask(p, dev, plane_mask)
5191 to_intel_plane(p)->disable_plane(p, crtc);
f98551ae 5192
f99d7069
SV
5193 /*
5194 * FIXME: Once we grow proper nuclear flip support out of this we need
5195 * to compute the mask of flip planes precisely. For the time being
5196 * consider this a flip to a NULL plane.
5197 */
5748b6a1 5198 intel_frontbuffer_flip(to_i915(dev), INTEL_FRONTBUFFER_ALL_MASK(pipe));
a5c4d7bc
VS
5199}
5200
fb1c98b1 5201static void intel_encoders_pre_pll_enable(struct drm_crtc *crtc,
fd6bbda9 5202 struct intel_crtc_state *crtc_state,
fb1c98b1
ML
5203 struct drm_atomic_state *old_state)
5204{
5205 struct drm_connector_state *old_conn_state;
5206 struct drm_connector *conn;
5207 int i;
5208
5209 for_each_connector_in_state(old_state, conn, old_conn_state, i) {
5210 struct drm_connector_state *conn_state = conn->state;
5211 struct intel_encoder *encoder =
5212 to_intel_encoder(conn_state->best_encoder);
5213
5214 if (conn_state->crtc != crtc)
5215 continue;
5216
5217 if (encoder->pre_pll_enable)
fd6bbda9 5218 encoder->pre_pll_enable(encoder, crtc_state, conn_state);
fb1c98b1
ML
5219 }
5220}
5221
5222static void intel_encoders_pre_enable(struct drm_crtc *crtc,
fd6bbda9 5223 struct intel_crtc_state *crtc_state,
fb1c98b1
ML
5224 struct drm_atomic_state *old_state)
5225{
5226 struct drm_connector_state *old_conn_state;
5227 struct drm_connector *conn;
5228 int i;
5229
5230 for_each_connector_in_state(old_state, conn, old_conn_state, i) {
5231 struct drm_connector_state *conn_state = conn->state;
5232 struct intel_encoder *encoder =
5233 to_intel_encoder(conn_state->best_encoder);
5234
5235 if (conn_state->crtc != crtc)
5236 continue;
5237
5238 if (encoder->pre_enable)
fd6bbda9 5239 encoder->pre_enable(encoder, crtc_state, conn_state);
fb1c98b1
ML
5240 }
5241}
5242
5243static void intel_encoders_enable(struct drm_crtc *crtc,
fd6bbda9 5244 struct intel_crtc_state *crtc_state,
fb1c98b1
ML
5245 struct drm_atomic_state *old_state)
5246{
5247 struct drm_connector_state *old_conn_state;
5248 struct drm_connector *conn;
5249 int i;
5250
5251 for_each_connector_in_state(old_state, conn, old_conn_state, i) {
5252 struct drm_connector_state *conn_state = conn->state;
5253 struct intel_encoder *encoder =
5254 to_intel_encoder(conn_state->best_encoder);
5255
5256 if (conn_state->crtc != crtc)
5257 continue;
5258
fd6bbda9 5259 encoder->enable(encoder, crtc_state, conn_state);
fb1c98b1
ML
5260 intel_opregion_notify_encoder(encoder, true);
5261 }
5262}
5263
5264static void intel_encoders_disable(struct drm_crtc *crtc,
fd6bbda9 5265 struct intel_crtc_state *old_crtc_state,
fb1c98b1
ML
5266 struct drm_atomic_state *old_state)
5267{
5268 struct drm_connector_state *old_conn_state;
5269 struct drm_connector *conn;
5270 int i;
5271
5272 for_each_connector_in_state(old_state, conn, old_conn_state, i) {
5273 struct intel_encoder *encoder =
5274 to_intel_encoder(old_conn_state->best_encoder);
5275
5276 if (old_conn_state->crtc != crtc)
5277 continue;
5278
5279 intel_opregion_notify_encoder(encoder, false);
fd6bbda9 5280 encoder->disable(encoder, old_crtc_state, old_conn_state);
fb1c98b1
ML
5281 }
5282}
5283
5284static void intel_encoders_post_disable(struct drm_crtc *crtc,
fd6bbda9 5285 struct intel_crtc_state *old_crtc_state,
fb1c98b1
ML
5286 struct drm_atomic_state *old_state)
5287{
5288 struct drm_connector_state *old_conn_state;
5289 struct drm_connector *conn;
5290 int i;
5291
5292 for_each_connector_in_state(old_state, conn, old_conn_state, i) {
5293 struct intel_encoder *encoder =
5294 to_intel_encoder(old_conn_state->best_encoder);
5295
5296 if (old_conn_state->crtc != crtc)
5297 continue;
5298
5299 if (encoder->post_disable)
fd6bbda9 5300 encoder->post_disable(encoder, old_crtc_state, old_conn_state);
fb1c98b1
ML
5301 }
5302}
5303
5304static void intel_encoders_post_pll_disable(struct drm_crtc *crtc,
fd6bbda9 5305 struct intel_crtc_state *old_crtc_state,
fb1c98b1
ML
5306 struct drm_atomic_state *old_state)
5307{
5308 struct drm_connector_state *old_conn_state;
5309 struct drm_connector *conn;
5310 int i;
5311
5312 for_each_connector_in_state(old_state, conn, old_conn_state, i) {
5313 struct intel_encoder *encoder =
5314 to_intel_encoder(old_conn_state->best_encoder);
5315
5316 if (old_conn_state->crtc != crtc)
5317 continue;
5318
5319 if (encoder->post_pll_disable)
fd6bbda9 5320 encoder->post_pll_disable(encoder, old_crtc_state, old_conn_state);
fb1c98b1
ML
5321 }
5322}
5323
4a806558
ML
5324static void ironlake_crtc_enable(struct intel_crtc_state *pipe_config,
5325 struct drm_atomic_state *old_state)
f67a559d 5326{
4a806558 5327 struct drm_crtc *crtc = pipe_config->base.crtc;
f67a559d 5328 struct drm_device *dev = crtc->dev;
fac5e23e 5329 struct drm_i915_private *dev_priv = to_i915(dev);
f67a559d
JB
5330 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5331 int pipe = intel_crtc->pipe;
f67a559d 5332
53d9f4e9 5333 if (WARN_ON(intel_crtc->active))
f67a559d
JB
5334 return;
5335
b2c0593a
VS
5336 /*
5337 * Sometimes spurious CPU pipe underruns happen during FDI
5338 * training, at least with VGA+HDMI cloning. Suppress them.
5339 *
5340 * On ILK we get an occasional spurious CPU pipe underruns
5341 * between eDP port A enable and vdd enable. Also PCH port
5342 * enable seems to result in the occasional CPU pipe underrun.
5343 *
5344 * Spurious PCH underruns also occur during PCH enabling.
5345 */
5346 if (intel_crtc->config->has_pch_encoder || IS_GEN5(dev_priv))
5347 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
81b088ca
VS
5348 if (intel_crtc->config->has_pch_encoder)
5349 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
5350
6e3c9717 5351 if (intel_crtc->config->has_pch_encoder)
b14b1055
SV
5352 intel_prepare_shared_dpll(intel_crtc);
5353
37a5650b 5354 if (intel_crtc_has_dp_encoder(intel_crtc->config))
fe3cd48d 5355 intel_dp_set_m_n(intel_crtc, M1_N1);
29407aab
SV
5356
5357 intel_set_pipe_timings(intel_crtc);
bc58be60 5358 intel_set_pipe_src_size(intel_crtc);
29407aab 5359
6e3c9717 5360 if (intel_crtc->config->has_pch_encoder) {
29407aab 5361 intel_cpu_transcoder_set_m_n(intel_crtc,
6e3c9717 5362 &intel_crtc->config->fdi_m_n, NULL);
29407aab
SV
5363 }
5364
5365 ironlake_set_pipeconf(crtc);
5366
f67a559d 5367 intel_crtc->active = true;
8664281b 5368
fd6bbda9 5369 intel_encoders_pre_enable(crtc, pipe_config, old_state);
f67a559d 5370
6e3c9717 5371 if (intel_crtc->config->has_pch_encoder) {
fff367c7
SV
5372 /* Note: FDI PLL enabling _must_ be done before we enable the
5373 * cpu pipes, hence this is separate from all the other fdi/pch
5374 * enabling. */
88cefb6c 5375 ironlake_fdi_pll_enable(intel_crtc);
46b6f814
SV
5376 } else {
5377 assert_fdi_tx_disabled(dev_priv, pipe);
5378 assert_fdi_rx_disabled(dev_priv, pipe);
5379 }
f67a559d 5380
b074cec8 5381 ironlake_pfit_enable(intel_crtc);
f67a559d 5382
9c54c0dd
JB
5383 /*
5384 * On ILK+ LUT must be loaded before the pipe is running but with
5385 * clocks enabled
5386 */
b95c5321 5387 intel_color_load_luts(&pipe_config->base);
9c54c0dd 5388
1d5bf5d9
ID
5389 if (dev_priv->display.initial_watermarks != NULL)
5390 dev_priv->display.initial_watermarks(intel_crtc->config);
e1fdc473 5391 intel_enable_pipe(intel_crtc);
f67a559d 5392
6e3c9717 5393 if (intel_crtc->config->has_pch_encoder)
f67a559d 5394 ironlake_pch_enable(crtc);
c98e9dcf 5395
f9b61ff6
SV
5396 assert_vblank_disabled(crtc);
5397 drm_crtc_vblank_on(crtc);
5398
fd6bbda9 5399 intel_encoders_enable(crtc, pipe_config, old_state);
61b77ddd 5400
6e266956 5401 if (HAS_PCH_CPT(dev_priv))
a1520318 5402 cpt_verify_modeset(dev, intel_crtc->pipe);
37ca8d4c
VS
5403
5404 /* Must wait for vblank to avoid spurious PCH FIFO underruns */
5405 if (intel_crtc->config->has_pch_encoder)
0f0f74bc 5406 intel_wait_for_vblank(dev_priv, pipe);
b2c0593a 5407 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
37ca8d4c 5408 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
6be4a607
JB
5409}
5410
42db64ef
PZ
5411/* IPS only exists on ULT machines and is tied to pipe A. */
5412static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
5413{
50a0bc90 5414 return HAS_IPS(to_i915(crtc->base.dev)) && crtc->pipe == PIPE_A;
42db64ef
PZ
5415}
5416
4a806558
ML
5417static void haswell_crtc_enable(struct intel_crtc_state *pipe_config,
5418 struct drm_atomic_state *old_state)
4f771f10 5419{
4a806558 5420 struct drm_crtc *crtc = pipe_config->base.crtc;
4f771f10 5421 struct drm_device *dev = crtc->dev;
fac5e23e 5422 struct drm_i915_private *dev_priv = to_i915(dev);
4f771f10 5423 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
99d736a2 5424 int pipe = intel_crtc->pipe, hsw_workaround_pipe;
4d1de975 5425 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
4f771f10 5426
53d9f4e9 5427 if (WARN_ON(intel_crtc->active))
4f771f10
PZ
5428 return;
5429
81b088ca
VS
5430 if (intel_crtc->config->has_pch_encoder)
5431 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5432 false);
5433
fd6bbda9 5434 intel_encoders_pre_pll_enable(crtc, pipe_config, old_state);
95a7a2ae 5435
8106ddbd 5436 if (intel_crtc->config->shared_dpll)
df8ad70c
SV
5437 intel_enable_shared_dpll(intel_crtc);
5438
37a5650b 5439 if (intel_crtc_has_dp_encoder(intel_crtc->config))
fe3cd48d 5440 intel_dp_set_m_n(intel_crtc, M1_N1);
229fca97 5441
d7edc4e5 5442 if (!transcoder_is_dsi(cpu_transcoder))
4d1de975
JN
5443 intel_set_pipe_timings(intel_crtc);
5444
bc58be60 5445 intel_set_pipe_src_size(intel_crtc);
229fca97 5446
4d1de975
JN
5447 if (cpu_transcoder != TRANSCODER_EDP &&
5448 !transcoder_is_dsi(cpu_transcoder)) {
5449 I915_WRITE(PIPE_MULT(cpu_transcoder),
6e3c9717 5450 intel_crtc->config->pixel_multiplier - 1);
ebb69c95
CT
5451 }
5452
6e3c9717 5453 if (intel_crtc->config->has_pch_encoder) {
229fca97 5454 intel_cpu_transcoder_set_m_n(intel_crtc,
6e3c9717 5455 &intel_crtc->config->fdi_m_n, NULL);
229fca97
SV
5456 }
5457
d7edc4e5 5458 if (!transcoder_is_dsi(cpu_transcoder))
4d1de975
JN
5459 haswell_set_pipeconf(crtc);
5460
391bf048 5461 haswell_set_pipemisc(crtc);
229fca97 5462
b95c5321 5463 intel_color_set_csc(&pipe_config->base);
229fca97 5464
4f771f10 5465 intel_crtc->active = true;
8664281b 5466
6b698516
SV
5467 if (intel_crtc->config->has_pch_encoder)
5468 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
5469 else
5470 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
5471
fd6bbda9 5472 intel_encoders_pre_enable(crtc, pipe_config, old_state);
4f771f10 5473
d2d65408 5474 if (intel_crtc->config->has_pch_encoder)
4fe9467d 5475 dev_priv->display.fdi_link_train(crtc);
4fe9467d 5476
d7edc4e5 5477 if (!transcoder_is_dsi(cpu_transcoder))
7d4aefd0 5478 intel_ddi_enable_pipe_clock(intel_crtc);
4f771f10 5479
1c132b44 5480 if (INTEL_INFO(dev)->gen >= 9)
e435d6e5 5481 skylake_pfit_enable(intel_crtc);
ff6d9f55 5482 else
1c132b44 5483 ironlake_pfit_enable(intel_crtc);
4f771f10
PZ
5484
5485 /*
5486 * On ILK+ LUT must be loaded before the pipe is running but with
5487 * clocks enabled
5488 */
b95c5321 5489 intel_color_load_luts(&pipe_config->base);
4f771f10 5490
1f544388 5491 intel_ddi_set_pipe_settings(crtc);
d7edc4e5 5492 if (!transcoder_is_dsi(cpu_transcoder))
7d4aefd0 5493 intel_ddi_enable_transcoder_func(crtc);
4f771f10 5494
1d5bf5d9
ID
5495 if (dev_priv->display.initial_watermarks != NULL)
5496 dev_priv->display.initial_watermarks(pipe_config);
5497 else
432081bc 5498 intel_update_watermarks(intel_crtc);
4d1de975
JN
5499
5500 /* XXX: Do the pipe assertions at the right place for BXT DSI. */
d7edc4e5 5501 if (!transcoder_is_dsi(cpu_transcoder))
4d1de975 5502 intel_enable_pipe(intel_crtc);
42db64ef 5503
6e3c9717 5504 if (intel_crtc->config->has_pch_encoder)
1507e5bd 5505 lpt_pch_enable(crtc);
4f771f10 5506
a65347ba 5507 if (intel_crtc->config->dp_encoder_is_mst)
0e32b39c
DA
5508 intel_ddi_set_vc_payload_alloc(crtc, true);
5509
f9b61ff6
SV
5510 assert_vblank_disabled(crtc);
5511 drm_crtc_vblank_on(crtc);
5512
fd6bbda9 5513 intel_encoders_enable(crtc, pipe_config, old_state);
4f771f10 5514
6b698516 5515 if (intel_crtc->config->has_pch_encoder) {
0f0f74bc
VS
5516 intel_wait_for_vblank(dev_priv, pipe);
5517 intel_wait_for_vblank(dev_priv, pipe);
6b698516 5518 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
d2d65408
VS
5519 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5520 true);
6b698516 5521 }
d2d65408 5522
e4916946
PZ
5523 /* If we change the relative order between pipe/planes enabling, we need
5524 * to change the workaround. */
99d736a2 5525 hsw_workaround_pipe = pipe_config->hsw_workaround_pipe;
772c2a51 5526 if (IS_HASWELL(dev_priv) && hsw_workaround_pipe != INVALID_PIPE) {
0f0f74bc
VS
5527 intel_wait_for_vblank(dev_priv, hsw_workaround_pipe);
5528 intel_wait_for_vblank(dev_priv, hsw_workaround_pipe);
99d736a2 5529 }
4f771f10
PZ
5530}
5531
bfd16b2a 5532static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force)
3f8dce3a
SV
5533{
5534 struct drm_device *dev = crtc->base.dev;
fac5e23e 5535 struct drm_i915_private *dev_priv = to_i915(dev);
3f8dce3a
SV
5536 int pipe = crtc->pipe;
5537
5538 /* To avoid upsetting the power well on haswell only disable the pfit if
5539 * it's in use. The hw state code will make sure we get this right. */
bfd16b2a 5540 if (force || crtc->config->pch_pfit.enabled) {
3f8dce3a
SV
5541 I915_WRITE(PF_CTL(pipe), 0);
5542 I915_WRITE(PF_WIN_POS(pipe), 0);
5543 I915_WRITE(PF_WIN_SZ(pipe), 0);
5544 }
5545}
5546
4a806558
ML
5547static void ironlake_crtc_disable(struct intel_crtc_state *old_crtc_state,
5548 struct drm_atomic_state *old_state)
6be4a607 5549{
4a806558 5550 struct drm_crtc *crtc = old_crtc_state->base.crtc;
6be4a607 5551 struct drm_device *dev = crtc->dev;
fac5e23e 5552 struct drm_i915_private *dev_priv = to_i915(dev);
6be4a607
JB
5553 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5554 int pipe = intel_crtc->pipe;
b52eb4dc 5555
b2c0593a
VS
5556 /*
5557 * Sometimes spurious CPU pipe underruns happen when the
5558 * pipe is already disabled, but FDI RX/TX is still enabled.
5559 * Happens at least with VGA+HDMI cloning. Suppress them.
5560 */
5561 if (intel_crtc->config->has_pch_encoder) {
5562 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
37ca8d4c 5563 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
b2c0593a 5564 }
37ca8d4c 5565
fd6bbda9 5566 intel_encoders_disable(crtc, old_crtc_state, old_state);
ea9d758d 5567
f9b61ff6
SV
5568 drm_crtc_vblank_off(crtc);
5569 assert_vblank_disabled(crtc);
5570
575f7ab7 5571 intel_disable_pipe(intel_crtc);
32f9d658 5572
bfd16b2a 5573 ironlake_pfit_disable(intel_crtc, false);
2c07245f 5574
b2c0593a 5575 if (intel_crtc->config->has_pch_encoder)
5a74f70a
VS
5576 ironlake_fdi_disable(crtc);
5577
fd6bbda9 5578 intel_encoders_post_disable(crtc, old_crtc_state, old_state);
2c07245f 5579
6e3c9717 5580 if (intel_crtc->config->has_pch_encoder) {
d925c59a 5581 ironlake_disable_pch_transcoder(dev_priv, pipe);
6be4a607 5582
6e266956 5583 if (HAS_PCH_CPT(dev_priv)) {
f0f59a00
VS
5584 i915_reg_t reg;
5585 u32 temp;
5586
d925c59a
SV
5587 /* disable TRANS_DP_CTL */
5588 reg = TRANS_DP_CTL(pipe);
5589 temp = I915_READ(reg);
5590 temp &= ~(TRANS_DP_OUTPUT_ENABLE |
5591 TRANS_DP_PORT_SEL_MASK);
5592 temp |= TRANS_DP_PORT_SEL_NONE;
5593 I915_WRITE(reg, temp);
5594
5595 /* disable DPLL_SEL */
5596 temp = I915_READ(PCH_DPLL_SEL);
11887397 5597 temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
d925c59a 5598 I915_WRITE(PCH_DPLL_SEL, temp);
9db4a9c7 5599 }
e3421a18 5600
d925c59a
SV
5601 ironlake_fdi_pll_disable(intel_crtc);
5602 }
81b088ca 5603
b2c0593a 5604 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
81b088ca 5605 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
6be4a607 5606}
1b3c7a47 5607
4a806558
ML
5608static void haswell_crtc_disable(struct intel_crtc_state *old_crtc_state,
5609 struct drm_atomic_state *old_state)
ee7b9f93 5610{
4a806558 5611 struct drm_crtc *crtc = old_crtc_state->base.crtc;
4f771f10 5612 struct drm_device *dev = crtc->dev;
fac5e23e 5613 struct drm_i915_private *dev_priv = to_i915(dev);
ee7b9f93 5614 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6e3c9717 5615 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
ee7b9f93 5616
d2d65408
VS
5617 if (intel_crtc->config->has_pch_encoder)
5618 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5619 false);
5620
fd6bbda9 5621 intel_encoders_disable(crtc, old_crtc_state, old_state);
4f771f10 5622
f9b61ff6
SV
5623 drm_crtc_vblank_off(crtc);
5624 assert_vblank_disabled(crtc);
5625
4d1de975 5626 /* XXX: Do the pipe assertions at the right place for BXT DSI. */
d7edc4e5 5627 if (!transcoder_is_dsi(cpu_transcoder))
4d1de975 5628 intel_disable_pipe(intel_crtc);
4f771f10 5629
6e3c9717 5630 if (intel_crtc->config->dp_encoder_is_mst)
a4bf214f
VS
5631 intel_ddi_set_vc_payload_alloc(crtc, false);
5632
d7edc4e5 5633 if (!transcoder_is_dsi(cpu_transcoder))
7d4aefd0 5634 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
4f771f10 5635
1c132b44 5636 if (INTEL_INFO(dev)->gen >= 9)
e435d6e5 5637 skylake_scaler_disable(intel_crtc);
ff6d9f55 5638 else
bfd16b2a 5639 ironlake_pfit_disable(intel_crtc, false);
4f771f10 5640
d7edc4e5 5641 if (!transcoder_is_dsi(cpu_transcoder))
7d4aefd0 5642 intel_ddi_disable_pipe_clock(intel_crtc);
4f771f10 5643
fd6bbda9 5644 intel_encoders_post_disable(crtc, old_crtc_state, old_state);
81b088ca 5645
b7076546 5646 if (old_crtc_state->has_pch_encoder)
81b088ca
VS
5647 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5648 true);
4f771f10
PZ
5649}
5650
2dd24552
JB
5651static void i9xx_pfit_enable(struct intel_crtc *crtc)
5652{
5653 struct drm_device *dev = crtc->base.dev;
fac5e23e 5654 struct drm_i915_private *dev_priv = to_i915(dev);
6e3c9717 5655 struct intel_crtc_state *pipe_config = crtc->config;
2dd24552 5656
681a8504 5657 if (!pipe_config->gmch_pfit.control)
2dd24552
JB
5658 return;
5659
2dd24552 5660 /*
c0b03411
SV
5661 * The panel fitter should only be adjusted whilst the pipe is disabled,
5662 * according to register description and PRM.
2dd24552 5663 */
c0b03411
SV
5664 WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
5665 assert_pipe_disabled(dev_priv, crtc->pipe);
2dd24552 5666
b074cec8
JB
5667 I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
5668 I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
5a80c45c
SV
5669
5670 /* Border color in case we don't scale up to the full screen. Black by
5671 * default, change to something else for debugging. */
5672 I915_WRITE(BCLRPAT(crtc->pipe), 0);
2dd24552
JB
5673}
5674
d05410f9
DA
5675static enum intel_display_power_domain port_to_power_domain(enum port port)
5676{
5677 switch (port) {
5678 case PORT_A:
6331a704 5679 return POWER_DOMAIN_PORT_DDI_A_LANES;
d05410f9 5680 case PORT_B:
6331a704 5681 return POWER_DOMAIN_PORT_DDI_B_LANES;
d05410f9 5682 case PORT_C:
6331a704 5683 return POWER_DOMAIN_PORT_DDI_C_LANES;
d05410f9 5684 case PORT_D:
6331a704 5685 return POWER_DOMAIN_PORT_DDI_D_LANES;
d8e19f99 5686 case PORT_E:
6331a704 5687 return POWER_DOMAIN_PORT_DDI_E_LANES;
d05410f9 5688 default:
b9fec167 5689 MISSING_CASE(port);
d05410f9
DA
5690 return POWER_DOMAIN_PORT_OTHER;
5691 }
5692}
5693
25f78f58
VS
5694static enum intel_display_power_domain port_to_aux_power_domain(enum port port)
5695{
5696 switch (port) {
5697 case PORT_A:
5698 return POWER_DOMAIN_AUX_A;
5699 case PORT_B:
5700 return POWER_DOMAIN_AUX_B;
5701 case PORT_C:
5702 return POWER_DOMAIN_AUX_C;
5703 case PORT_D:
5704 return POWER_DOMAIN_AUX_D;
5705 case PORT_E:
5706 /* FIXME: Check VBT for actual wiring of PORT E */
5707 return POWER_DOMAIN_AUX_D;
5708 default:
b9fec167 5709 MISSING_CASE(port);
25f78f58
VS
5710 return POWER_DOMAIN_AUX_A;
5711 }
5712}
5713
319be8ae
ID
5714enum intel_display_power_domain
5715intel_display_port_power_domain(struct intel_encoder *intel_encoder)
5716{
4f8036a2 5717 struct drm_i915_private *dev_priv = to_i915(intel_encoder->base.dev);
319be8ae
ID
5718 struct intel_digital_port *intel_dig_port;
5719
5720 switch (intel_encoder->type) {
5721 case INTEL_OUTPUT_UNKNOWN:
5722 /* Only DDI platforms should ever use this output type */
4f8036a2 5723 WARN_ON_ONCE(!HAS_DDI(dev_priv));
cca0502b 5724 case INTEL_OUTPUT_DP:
319be8ae
ID
5725 case INTEL_OUTPUT_HDMI:
5726 case INTEL_OUTPUT_EDP:
5727 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
d05410f9 5728 return port_to_power_domain(intel_dig_port->port);
0e32b39c
DA
5729 case INTEL_OUTPUT_DP_MST:
5730 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
5731 return port_to_power_domain(intel_dig_port->port);
319be8ae
ID
5732 case INTEL_OUTPUT_ANALOG:
5733 return POWER_DOMAIN_PORT_CRT;
5734 case INTEL_OUTPUT_DSI:
5735 return POWER_DOMAIN_PORT_DSI;
5736 default:
5737 return POWER_DOMAIN_PORT_OTHER;
5738 }
5739}
5740
25f78f58
VS
5741enum intel_display_power_domain
5742intel_display_port_aux_power_domain(struct intel_encoder *intel_encoder)
5743{
4f8036a2 5744 struct drm_i915_private *dev_priv = to_i915(intel_encoder->base.dev);
25f78f58
VS
5745 struct intel_digital_port *intel_dig_port;
5746
5747 switch (intel_encoder->type) {
5748 case INTEL_OUTPUT_UNKNOWN:
651174a4
ID
5749 case INTEL_OUTPUT_HDMI:
5750 /*
5751 * Only DDI platforms should ever use these output types.
5752 * We can get here after the HDMI detect code has already set
5753 * the type of the shared encoder. Since we can't be sure
5754 * what's the status of the given connectors, play safe and
5755 * run the DP detection too.
5756 */
4f8036a2 5757 WARN_ON_ONCE(!HAS_DDI(dev_priv));
cca0502b 5758 case INTEL_OUTPUT_DP:
25f78f58
VS
5759 case INTEL_OUTPUT_EDP:
5760 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
5761 return port_to_aux_power_domain(intel_dig_port->port);
5762 case INTEL_OUTPUT_DP_MST:
5763 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
5764 return port_to_aux_power_domain(intel_dig_port->port);
5765 default:
b9fec167 5766 MISSING_CASE(intel_encoder->type);
25f78f58
VS
5767 return POWER_DOMAIN_AUX_A;
5768 }
5769}
5770
74bff5f9
ML
5771static unsigned long get_crtc_power_domains(struct drm_crtc *crtc,
5772 struct intel_crtc_state *crtc_state)
77d22dca 5773{
319be8ae 5774 struct drm_device *dev = crtc->dev;
74bff5f9 5775 struct drm_encoder *encoder;
319be8ae
ID
5776 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5777 enum pipe pipe = intel_crtc->pipe;
77d22dca 5778 unsigned long mask;
74bff5f9 5779 enum transcoder transcoder = crtc_state->cpu_transcoder;
77d22dca 5780
74bff5f9 5781 if (!crtc_state->base.active)
292b990e
ML
5782 return 0;
5783
77d22dca
ID
5784 mask = BIT(POWER_DOMAIN_PIPE(pipe));
5785 mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
74bff5f9
ML
5786 if (crtc_state->pch_pfit.enabled ||
5787 crtc_state->pch_pfit.force_thru)
77d22dca
ID
5788 mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
5789
74bff5f9
ML
5790 drm_for_each_encoder_mask(encoder, dev, crtc_state->base.encoder_mask) {
5791 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
5792
319be8ae 5793 mask |= BIT(intel_display_port_power_domain(intel_encoder));
74bff5f9 5794 }
319be8ae 5795
15e7ec29
ML
5796 if (crtc_state->shared_dpll)
5797 mask |= BIT(POWER_DOMAIN_PLLS);
5798
77d22dca
ID
5799 return mask;
5800}
5801
74bff5f9
ML
5802static unsigned long
5803modeset_get_crtc_power_domains(struct drm_crtc *crtc,
5804 struct intel_crtc_state *crtc_state)
77d22dca 5805{
fac5e23e 5806 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
292b990e
ML
5807 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5808 enum intel_display_power_domain domain;
5a21b665 5809 unsigned long domains, new_domains, old_domains;
77d22dca 5810
292b990e 5811 old_domains = intel_crtc->enabled_power_domains;
74bff5f9
ML
5812 intel_crtc->enabled_power_domains = new_domains =
5813 get_crtc_power_domains(crtc, crtc_state);
77d22dca 5814
5a21b665 5815 domains = new_domains & ~old_domains;
292b990e
ML
5816
5817 for_each_power_domain(domain, domains)
5818 intel_display_power_get(dev_priv, domain);
5819
5a21b665 5820 return old_domains & ~new_domains;
292b990e
ML
5821}
5822
5823static void modeset_put_power_domains(struct drm_i915_private *dev_priv,
5824 unsigned long domains)
5825{
5826 enum intel_display_power_domain domain;
5827
5828 for_each_power_domain(domain, domains)
5829 intel_display_power_put(dev_priv, domain);
5830}
77d22dca 5831
adafdc6f
MK
5832static int intel_compute_max_dotclk(struct drm_i915_private *dev_priv)
5833{
5834 int max_cdclk_freq = dev_priv->max_cdclk_freq;
5835
5836 if (INTEL_INFO(dev_priv)->gen >= 9 ||
5837 IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
5838 return max_cdclk_freq;
5839 else if (IS_CHERRYVIEW(dev_priv))
5840 return max_cdclk_freq*95/100;
5841 else if (INTEL_INFO(dev_priv)->gen < 4)
5842 return 2*max_cdclk_freq*90/100;
5843 else
5844 return max_cdclk_freq*90/100;
5845}
5846
b2045352
VS
5847static int skl_calc_cdclk(int max_pixclk, int vco);
5848
4c75b940 5849static void intel_update_max_cdclk(struct drm_i915_private *dev_priv)
560a7ae4 5850{
0853723b 5851 if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv)) {
560a7ae4 5852 u32 limit = I915_READ(SKL_DFSM) & SKL_DFSM_CDCLK_LIMIT_MASK;
b2045352
VS
5853 int max_cdclk, vco;
5854
5855 vco = dev_priv->skl_preferred_vco_freq;
63911d72 5856 WARN_ON(vco != 8100000 && vco != 8640000);
560a7ae4 5857
b2045352
VS
5858 /*
5859 * Use the lower (vco 8640) cdclk values as a
5860 * first guess. skl_calc_cdclk() will correct it
5861 * if the preferred vco is 8100 instead.
5862 */
560a7ae4 5863 if (limit == SKL_DFSM_CDCLK_LIMIT_675)
487ed2e4 5864 max_cdclk = 617143;
560a7ae4 5865 else if (limit == SKL_DFSM_CDCLK_LIMIT_540)
b2045352 5866 max_cdclk = 540000;
560a7ae4 5867 else if (limit == SKL_DFSM_CDCLK_LIMIT_450)
b2045352 5868 max_cdclk = 432000;
560a7ae4 5869 else
487ed2e4 5870 max_cdclk = 308571;
b2045352
VS
5871
5872 dev_priv->max_cdclk_freq = skl_calc_cdclk(max_cdclk, vco);
e2d214ae 5873 } else if (IS_BROXTON(dev_priv)) {
281c114f 5874 dev_priv->max_cdclk_freq = 624000;
8652744b 5875 } else if (IS_BROADWELL(dev_priv)) {
560a7ae4
DL
5876 /*
5877 * FIXME with extra cooling we can allow
5878 * 540 MHz for ULX and 675 Mhz for ULT.
5879 * How can we know if extra cooling is
5880 * available? PCI ID, VTB, something else?
5881 */
5882 if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
5883 dev_priv->max_cdclk_freq = 450000;
50a0bc90 5884 else if (IS_BDW_ULX(dev_priv))
560a7ae4 5885 dev_priv->max_cdclk_freq = 450000;
50a0bc90 5886 else if (IS_BDW_ULT(dev_priv))
560a7ae4
DL
5887 dev_priv->max_cdclk_freq = 540000;
5888 else
5889 dev_priv->max_cdclk_freq = 675000;
920a14b2 5890 } else if (IS_CHERRYVIEW(dev_priv)) {
0904deaf 5891 dev_priv->max_cdclk_freq = 320000;
11a914c2 5892 } else if (IS_VALLEYVIEW(dev_priv)) {
560a7ae4
DL
5893 dev_priv->max_cdclk_freq = 400000;
5894 } else {
5895 /* otherwise assume cdclk is fixed */
5896 dev_priv->max_cdclk_freq = dev_priv->cdclk_freq;
5897 }
5898
adafdc6f
MK
5899 dev_priv->max_dotclk_freq = intel_compute_max_dotclk(dev_priv);
5900
560a7ae4
DL
5901 DRM_DEBUG_DRIVER("Max CD clock rate: %d kHz\n",
5902 dev_priv->max_cdclk_freq);
adafdc6f
MK
5903
5904 DRM_DEBUG_DRIVER("Max dotclock rate: %d kHz\n",
5905 dev_priv->max_dotclk_freq);
560a7ae4
DL
5906}
5907
4c75b940 5908static void intel_update_cdclk(struct drm_i915_private *dev_priv)
560a7ae4 5909{
1353c4fb 5910 dev_priv->cdclk_freq = dev_priv->display.get_display_clock_speed(dev_priv);
2f2a121a 5911
83d7c81f 5912 if (INTEL_GEN(dev_priv) >= 9)
709e05c3
VS
5913 DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz, VCO: %d kHz, ref: %d kHz\n",
5914 dev_priv->cdclk_freq, dev_priv->cdclk_pll.vco,
5915 dev_priv->cdclk_pll.ref);
2f2a121a
VS
5916 else
5917 DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
5918 dev_priv->cdclk_freq);
560a7ae4
DL
5919
5920 /*
b5d99ff9
VS
5921 * 9:0 CMBUS [sic] CDCLK frequency (cdfreq):
5922 * Programmng [sic] note: bit[9:2] should be programmed to the number
5923 * of cdclk that generates 4MHz reference clock freq which is used to
5924 * generate GMBus clock. This will vary with the cdclk freq.
560a7ae4 5925 */
b5d99ff9 5926 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
560a7ae4 5927 I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->cdclk_freq, 1000));
560a7ae4
DL
5928}
5929
92891e45
VS
5930/* convert from kHz to .1 fixpoint MHz with -1MHz offset */
5931static int skl_cdclk_decimal(int cdclk)
5932{
5933 return DIV_ROUND_CLOSEST(cdclk - 1000, 500);
5934}
5935
5f199dfa
VS
5936static int bxt_de_pll_vco(struct drm_i915_private *dev_priv, int cdclk)
5937{
5938 int ratio;
5939
5940 if (cdclk == dev_priv->cdclk_pll.ref)
5941 return 0;
5942
5943 switch (cdclk) {
5944 default:
5945 MISSING_CASE(cdclk);
5946 case 144000:
5947 case 288000:
5948 case 384000:
5949 case 576000:
5950 ratio = 60;
5951 break;
5952 case 624000:
5953 ratio = 65;
5954 break;
5955 }
5956
5957 return dev_priv->cdclk_pll.ref * ratio;
5958}
5959
2b73001e
VS
5960static void bxt_de_pll_disable(struct drm_i915_private *dev_priv)
5961{
5962 I915_WRITE(BXT_DE_PLL_ENABLE, 0);
5963
5964 /* Timeout 200us */
95cac283
CW
5965 if (intel_wait_for_register(dev_priv,
5966 BXT_DE_PLL_ENABLE, BXT_DE_PLL_LOCK, 0,
5967 1))
2b73001e 5968 DRM_ERROR("timeout waiting for DE PLL unlock\n");
83d7c81f
VS
5969
5970 dev_priv->cdclk_pll.vco = 0;
2b73001e
VS
5971}
5972
5f199dfa 5973static void bxt_de_pll_enable(struct drm_i915_private *dev_priv, int vco)
2b73001e 5974{
5f199dfa 5975 int ratio = DIV_ROUND_CLOSEST(vco, dev_priv->cdclk_pll.ref);
2b73001e
VS
5976 u32 val;
5977
5978 val = I915_READ(BXT_DE_PLL_CTL);
5979 val &= ~BXT_DE_PLL_RATIO_MASK;
5f199dfa 5980 val |= BXT_DE_PLL_RATIO(ratio);
2b73001e
VS
5981 I915_WRITE(BXT_DE_PLL_CTL, val);
5982
5983 I915_WRITE(BXT_DE_PLL_ENABLE, BXT_DE_PLL_PLL_ENABLE);
5984
5985 /* Timeout 200us */
e084e1b9
CW
5986 if (intel_wait_for_register(dev_priv,
5987 BXT_DE_PLL_ENABLE,
5988 BXT_DE_PLL_LOCK,
5989 BXT_DE_PLL_LOCK,
5990 1))
2b73001e 5991 DRM_ERROR("timeout waiting for DE PLL lock\n");
83d7c81f 5992
5f199dfa 5993 dev_priv->cdclk_pll.vco = vco;
2b73001e
VS
5994}
5995
324513c0 5996static void bxt_set_cdclk(struct drm_i915_private *dev_priv, int cdclk)
f8437dd1 5997{
5f199dfa
VS
5998 u32 val, divider;
5999 int vco, ret;
f8437dd1 6000
5f199dfa
VS
6001 vco = bxt_de_pll_vco(dev_priv, cdclk);
6002
6003 DRM_DEBUG_DRIVER("Changing CDCLK to %d kHz (VCO %d kHz)\n", cdclk, vco);
6004
6005 /* cdclk = vco / 2 / div{1,1.5,2,4} */
6006 switch (DIV_ROUND_CLOSEST(vco, cdclk)) {
6007 case 8:
f8437dd1 6008 divider = BXT_CDCLK_CD2X_DIV_SEL_4;
f8437dd1 6009 break;
5f199dfa 6010 case 4:
f8437dd1 6011 divider = BXT_CDCLK_CD2X_DIV_SEL_2;
f8437dd1 6012 break;
5f199dfa 6013 case 3:
f8437dd1 6014 divider = BXT_CDCLK_CD2X_DIV_SEL_1_5;
f8437dd1 6015 break;
5f199dfa 6016 case 2:
f8437dd1 6017 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
f8437dd1
VK
6018 break;
6019 default:
5f199dfa
VS
6020 WARN_ON(cdclk != dev_priv->cdclk_pll.ref);
6021 WARN_ON(vco != 0);
f8437dd1 6022
5f199dfa
VS
6023 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
6024 break;
f8437dd1
VK
6025 }
6026
f8437dd1 6027 /* Inform power controller of upcoming frequency change */
5f199dfa 6028 mutex_lock(&dev_priv->rps.hw_lock);
f8437dd1
VK
6029 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
6030 0x80000000);
6031 mutex_unlock(&dev_priv->rps.hw_lock);
6032
6033 if (ret) {
6034 DRM_ERROR("PCode CDCLK freq change notify failed (err %d, freq %d)\n",
9ef56154 6035 ret, cdclk);
f8437dd1
VK
6036 return;
6037 }
6038
5f199dfa
VS
6039 if (dev_priv->cdclk_pll.vco != 0 &&
6040 dev_priv->cdclk_pll.vco != vco)
2b73001e 6041 bxt_de_pll_disable(dev_priv);
f8437dd1 6042
5f199dfa
VS
6043 if (dev_priv->cdclk_pll.vco != vco)
6044 bxt_de_pll_enable(dev_priv, vco);
f8437dd1 6045
5f199dfa
VS
6046 val = divider | skl_cdclk_decimal(cdclk);
6047 /*
6048 * FIXME if only the cd2x divider needs changing, it could be done
6049 * without shutting off the pipe (if only one pipe is active).
6050 */
6051 val |= BXT_CDCLK_CD2X_PIPE_NONE;
6052 /*
6053 * Disable SSA Precharge when CD clock frequency < 500 MHz,
6054 * enable otherwise.
6055 */
6056 if (cdclk >= 500000)
6057 val |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
6058 I915_WRITE(CDCLK_CTL, val);
f8437dd1
VK
6059
6060 mutex_lock(&dev_priv->rps.hw_lock);
6061 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
9ef56154 6062 DIV_ROUND_UP(cdclk, 25000));
f8437dd1
VK
6063 mutex_unlock(&dev_priv->rps.hw_lock);
6064
6065 if (ret) {
6066 DRM_ERROR("PCode CDCLK freq set failed, (err %d, freq %d)\n",
9ef56154 6067 ret, cdclk);
f8437dd1
VK
6068 return;
6069 }
6070
4c75b940 6071 intel_update_cdclk(dev_priv);
f8437dd1
VK
6072}
6073
d66a2194 6074static void bxt_sanitize_cdclk(struct drm_i915_private *dev_priv)
f8437dd1 6075{
d66a2194
ID
6076 u32 cdctl, expected;
6077
4c75b940 6078 intel_update_cdclk(dev_priv);
f8437dd1 6079
d66a2194
ID
6080 if (dev_priv->cdclk_pll.vco == 0 ||
6081 dev_priv->cdclk_freq == dev_priv->cdclk_pll.ref)
6082 goto sanitize;
6083
6084 /* DPLL okay; verify the cdclock
6085 *
6086 * Some BIOS versions leave an incorrect decimal frequency value and
6087 * set reserved MBZ bits in CDCLK_CTL at least during exiting from S4,
6088 * so sanitize this register.
6089 */
6090 cdctl = I915_READ(CDCLK_CTL);
6091 /*
6092 * Let's ignore the pipe field, since BIOS could have configured the
6093 * dividers both synching to an active pipe, or asynchronously
6094 * (PIPE_NONE).
6095 */
6096 cdctl &= ~BXT_CDCLK_CD2X_PIPE_NONE;
6097
6098 expected = (cdctl & BXT_CDCLK_CD2X_DIV_SEL_MASK) |
6099 skl_cdclk_decimal(dev_priv->cdclk_freq);
6100 /*
6101 * Disable SSA Precharge when CD clock frequency < 500 MHz,
6102 * enable otherwise.
6103 */
6104 if (dev_priv->cdclk_freq >= 500000)
6105 expected |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
6106
6107 if (cdctl == expected)
6108 /* All well; nothing to sanitize */
6109 return;
6110
6111sanitize:
6112 DRM_DEBUG_KMS("Sanitizing cdclk programmed by pre-os\n");
6113
6114 /* force cdclk programming */
6115 dev_priv->cdclk_freq = 0;
6116
6117 /* force full PLL disable + enable */
6118 dev_priv->cdclk_pll.vco = -1;
6119}
6120
324513c0 6121void bxt_init_cdclk(struct drm_i915_private *dev_priv)
d66a2194
ID
6122{
6123 bxt_sanitize_cdclk(dev_priv);
6124
6125 if (dev_priv->cdclk_freq != 0 && dev_priv->cdclk_pll.vco != 0)
089c6fd5 6126 return;
c2e001ef 6127
f8437dd1
VK
6128 /*
6129 * FIXME:
6130 * - The initial CDCLK needs to be read from VBT.
6131 * Need to make this change after VBT has changes for BXT.
f8437dd1 6132 */
324513c0 6133 bxt_set_cdclk(dev_priv, bxt_calc_cdclk(0));
f8437dd1
VK
6134}
6135
324513c0 6136void bxt_uninit_cdclk(struct drm_i915_private *dev_priv)
f8437dd1 6137{
324513c0 6138 bxt_set_cdclk(dev_priv, dev_priv->cdclk_pll.ref);
f8437dd1
VK
6139}
6140
a8ca4934
VS
6141static int skl_calc_cdclk(int max_pixclk, int vco)
6142{
63911d72 6143 if (vco == 8640000) {
a8ca4934 6144 if (max_pixclk > 540000)
487ed2e4 6145 return 617143;
a8ca4934
VS
6146 else if (max_pixclk > 432000)
6147 return 540000;
487ed2e4 6148 else if (max_pixclk > 308571)
a8ca4934
VS
6149 return 432000;
6150 else
487ed2e4 6151 return 308571;
a8ca4934 6152 } else {
a8ca4934
VS
6153 if (max_pixclk > 540000)
6154 return 675000;
6155 else if (max_pixclk > 450000)
6156 return 540000;
6157 else if (max_pixclk > 337500)
6158 return 450000;
6159 else
6160 return 337500;
6161 }
6162}
6163
ea61791e
VS
6164static void
6165skl_dpll0_update(struct drm_i915_private *dev_priv)
5d96d8af 6166{
ea61791e 6167 u32 val;
5d96d8af 6168
709e05c3 6169 dev_priv->cdclk_pll.ref = 24000;
1c3f7700 6170 dev_priv->cdclk_pll.vco = 0;
709e05c3 6171
ea61791e 6172 val = I915_READ(LCPLL1_CTL);
1c3f7700 6173 if ((val & LCPLL_PLL_ENABLE) == 0)
ea61791e 6174 return;
5d96d8af 6175
1c3f7700
ID
6176 if (WARN_ON((val & LCPLL_PLL_LOCK) == 0))
6177 return;
9f7eb31a 6178
ea61791e
VS
6179 val = I915_READ(DPLL_CTRL1);
6180
1c3f7700
ID
6181 if (WARN_ON((val & (DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) |
6182 DPLL_CTRL1_SSC(SKL_DPLL0) |
6183 DPLL_CTRL1_OVERRIDE(SKL_DPLL0))) !=
6184 DPLL_CTRL1_OVERRIDE(SKL_DPLL0)))
6185 return;
9f7eb31a 6186
ea61791e
VS
6187 switch (val & DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0)) {
6188 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810, SKL_DPLL0):
6189 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1350, SKL_DPLL0):
6190 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1620, SKL_DPLL0):
6191 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_2700, SKL_DPLL0):
63911d72 6192 dev_priv->cdclk_pll.vco = 8100000;
ea61791e
VS
6193 break;
6194 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080, SKL_DPLL0):
6195 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_2160, SKL_DPLL0):
63911d72 6196 dev_priv->cdclk_pll.vco = 8640000;
ea61791e
VS
6197 break;
6198 default:
6199 MISSING_CASE(val & DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
ea61791e
VS
6200 break;
6201 }
5d96d8af
DL
6202}
6203
b2045352
VS
6204void skl_set_preferred_cdclk_vco(struct drm_i915_private *dev_priv, int vco)
6205{
6206 bool changed = dev_priv->skl_preferred_vco_freq != vco;
6207
6208 dev_priv->skl_preferred_vco_freq = vco;
6209
6210 if (changed)
4c75b940 6211 intel_update_max_cdclk(dev_priv);
b2045352
VS
6212}
6213
5d96d8af 6214static void
3861fc60 6215skl_dpll0_enable(struct drm_i915_private *dev_priv, int vco)
5d96d8af 6216{
a8ca4934 6217 int min_cdclk = skl_calc_cdclk(0, vco);
5d96d8af
DL
6218 u32 val;
6219
63911d72 6220 WARN_ON(vco != 8100000 && vco != 8640000);
b2045352 6221
5d96d8af 6222 /* select the minimum CDCLK before enabling DPLL 0 */
9ef56154 6223 val = CDCLK_FREQ_337_308 | skl_cdclk_decimal(min_cdclk);
5d96d8af
DL
6224 I915_WRITE(CDCLK_CTL, val);
6225 POSTING_READ(CDCLK_CTL);
6226
6227 /*
6228 * We always enable DPLL0 with the lowest link rate possible, but still
6229 * taking into account the VCO required to operate the eDP panel at the
6230 * desired frequency. The usual DP link rates operate with a VCO of
6231 * 8100 while the eDP 1.4 alternate link rates need a VCO of 8640.
6232 * The modeset code is responsible for the selection of the exact link
6233 * rate later on, with the constraint of choosing a frequency that
a8ca4934 6234 * works with vco.
5d96d8af
DL
6235 */
6236 val = I915_READ(DPLL_CTRL1);
6237
6238 val &= ~(DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) | DPLL_CTRL1_SSC(SKL_DPLL0) |
6239 DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
6240 val |= DPLL_CTRL1_OVERRIDE(SKL_DPLL0);
63911d72 6241 if (vco == 8640000)
5d96d8af
DL
6242 val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080,
6243 SKL_DPLL0);
6244 else
6245 val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810,
6246 SKL_DPLL0);
6247
6248 I915_WRITE(DPLL_CTRL1, val);
6249 POSTING_READ(DPLL_CTRL1);
6250
6251 I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) | LCPLL_PLL_ENABLE);
6252
e24ca054
CW
6253 if (intel_wait_for_register(dev_priv,
6254 LCPLL1_CTL, LCPLL_PLL_LOCK, LCPLL_PLL_LOCK,
6255 5))
5d96d8af 6256 DRM_ERROR("DPLL0 not locked\n");
1cd593e0 6257
63911d72 6258 dev_priv->cdclk_pll.vco = vco;
b2045352
VS
6259
6260 /* We'll want to keep using the current vco from now on. */
6261 skl_set_preferred_cdclk_vco(dev_priv, vco);
5d96d8af
DL
6262}
6263
430e05de
VS
6264static void
6265skl_dpll0_disable(struct drm_i915_private *dev_priv)
6266{
6267 I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) & ~LCPLL_PLL_ENABLE);
8ad32a05
CW
6268 if (intel_wait_for_register(dev_priv,
6269 LCPLL1_CTL, LCPLL_PLL_LOCK, 0,
6270 1))
430e05de 6271 DRM_ERROR("Couldn't disable DPLL0\n");
1cd593e0 6272
63911d72 6273 dev_priv->cdclk_pll.vco = 0;
430e05de
VS
6274}
6275
5d96d8af
DL
6276static bool skl_cdclk_pcu_ready(struct drm_i915_private *dev_priv)
6277{
6278 int ret;
6279 u32 val;
6280
6281 /* inform PCU we want to change CDCLK */
6282 val = SKL_CDCLK_PREPARE_FOR_CHANGE;
6283 mutex_lock(&dev_priv->rps.hw_lock);
6284 ret = sandybridge_pcode_read(dev_priv, SKL_PCODE_CDCLK_CONTROL, &val);
6285 mutex_unlock(&dev_priv->rps.hw_lock);
6286
6287 return ret == 0 && (val & SKL_CDCLK_READY_FOR_CHANGE);
6288}
6289
6290static bool skl_cdclk_wait_for_pcu_ready(struct drm_i915_private *dev_priv)
6291{
848496e5 6292 return _wait_for(skl_cdclk_pcu_ready(dev_priv), 3000, 10) == 0;
5d96d8af
DL
6293}
6294
1cd593e0 6295static void skl_set_cdclk(struct drm_i915_private *dev_priv, int cdclk, int vco)
5d96d8af
DL
6296{
6297 u32 freq_select, pcu_ack;
6298
1cd593e0
VS
6299 WARN_ON((cdclk == 24000) != (vco == 0));
6300
63911d72 6301 DRM_DEBUG_DRIVER("Changing CDCLK to %d kHz (VCO %d kHz)\n", cdclk, vco);
5d96d8af
DL
6302
6303 if (!skl_cdclk_wait_for_pcu_ready(dev_priv)) {
6304 DRM_ERROR("failed to inform PCU about cdclk change\n");
6305 return;
6306 }
6307
6308 /* set CDCLK_CTL */
9ef56154 6309 switch (cdclk) {
5d96d8af
DL
6310 case 450000:
6311 case 432000:
6312 freq_select = CDCLK_FREQ_450_432;
6313 pcu_ack = 1;
6314 break;
6315 case 540000:
6316 freq_select = CDCLK_FREQ_540;
6317 pcu_ack = 2;
6318 break;
487ed2e4 6319 case 308571:
5d96d8af
DL
6320 case 337500:
6321 default:
6322 freq_select = CDCLK_FREQ_337_308;
6323 pcu_ack = 0;
6324 break;
487ed2e4 6325 case 617143:
5d96d8af
DL
6326 case 675000:
6327 freq_select = CDCLK_FREQ_675_617;
6328 pcu_ack = 3;
6329 break;
6330 }
6331
63911d72
VS
6332 if (dev_priv->cdclk_pll.vco != 0 &&
6333 dev_priv->cdclk_pll.vco != vco)
1cd593e0
VS
6334 skl_dpll0_disable(dev_priv);
6335
63911d72 6336 if (dev_priv->cdclk_pll.vco != vco)
1cd593e0
VS
6337 skl_dpll0_enable(dev_priv, vco);
6338
9ef56154 6339 I915_WRITE(CDCLK_CTL, freq_select | skl_cdclk_decimal(cdclk));
5d96d8af
DL
6340 POSTING_READ(CDCLK_CTL);
6341
6342 /* inform PCU of the change */
6343 mutex_lock(&dev_priv->rps.hw_lock);
6344 sandybridge_pcode_write(dev_priv, SKL_PCODE_CDCLK_CONTROL, pcu_ack);
6345 mutex_unlock(&dev_priv->rps.hw_lock);
560a7ae4 6346
4c75b940 6347 intel_update_cdclk(dev_priv);
5d96d8af
DL
6348}
6349
9f7eb31a
VS
6350static void skl_sanitize_cdclk(struct drm_i915_private *dev_priv);
6351
5d96d8af
DL
6352void skl_uninit_cdclk(struct drm_i915_private *dev_priv)
6353{
709e05c3 6354 skl_set_cdclk(dev_priv, dev_priv->cdclk_pll.ref, 0);
5d96d8af
DL
6355}
6356
6357void skl_init_cdclk(struct drm_i915_private *dev_priv)
6358{
9f7eb31a
VS
6359 int cdclk, vco;
6360
6361 skl_sanitize_cdclk(dev_priv);
5d96d8af 6362
63911d72 6363 if (dev_priv->cdclk_freq != 0 && dev_priv->cdclk_pll.vco != 0) {
9f7eb31a
VS
6364 /*
6365 * Use the current vco as our initial
6366 * guess as to what the preferred vco is.
6367 */
6368 if (dev_priv->skl_preferred_vco_freq == 0)
6369 skl_set_preferred_cdclk_vco(dev_priv,
63911d72 6370 dev_priv->cdclk_pll.vco);
70c2c184 6371 return;
1cd593e0 6372 }
5d96d8af 6373
70c2c184
VS
6374 vco = dev_priv->skl_preferred_vco_freq;
6375 if (vco == 0)
63911d72 6376 vco = 8100000;
70c2c184 6377 cdclk = skl_calc_cdclk(0, vco);
5d96d8af 6378
70c2c184 6379 skl_set_cdclk(dev_priv, cdclk, vco);
5d96d8af
DL
6380}
6381
9f7eb31a 6382static void skl_sanitize_cdclk(struct drm_i915_private *dev_priv)
c73666f3 6383{
09492498 6384 uint32_t cdctl, expected;
c73666f3 6385
f1b391a5
SK
6386 /*
6387 * check if the pre-os intialized the display
6388 * There is SWF18 scratchpad register defined which is set by the
6389 * pre-os which can be used by the OS drivers to check the status
6390 */
6391 if ((I915_READ(SWF_ILK(0x18)) & 0x00FFFFFF) == 0)
6392 goto sanitize;
6393
4c75b940 6394 intel_update_cdclk(dev_priv);
c73666f3 6395 /* Is PLL enabled and locked ? */
1c3f7700
ID
6396 if (dev_priv->cdclk_pll.vco == 0 ||
6397 dev_priv->cdclk_freq == dev_priv->cdclk_pll.ref)
c73666f3
SK
6398 goto sanitize;
6399
6400 /* DPLL okay; verify the cdclock
6401 *
6402 * Noticed in some instances that the freq selection is correct but
6403 * decimal part is programmed wrong from BIOS where pre-os does not
6404 * enable display. Verify the same as well.
6405 */
09492498
VS
6406 cdctl = I915_READ(CDCLK_CTL);
6407 expected = (cdctl & CDCLK_FREQ_SEL_MASK) |
6408 skl_cdclk_decimal(dev_priv->cdclk_freq);
6409 if (cdctl == expected)
c73666f3 6410 /* All well; nothing to sanitize */
9f7eb31a 6411 return;
c89e39f3 6412
9f7eb31a
VS
6413sanitize:
6414 DRM_DEBUG_KMS("Sanitizing cdclk programmed by pre-os\n");
c73666f3 6415
9f7eb31a
VS
6416 /* force cdclk programming */
6417 dev_priv->cdclk_freq = 0;
6418 /* force full PLL disable + enable */
63911d72 6419 dev_priv->cdclk_pll.vco = -1;
c73666f3
SK
6420}
6421
30a970c6
JB
6422/* Adjust CDclk dividers to allow high res or save power if possible */
6423static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
6424{
fac5e23e 6425 struct drm_i915_private *dev_priv = to_i915(dev);
30a970c6
JB
6426 u32 val, cmd;
6427
1353c4fb 6428 WARN_ON(dev_priv->display.get_display_clock_speed(dev_priv)
164dfd28 6429 != dev_priv->cdclk_freq);
d60c4473 6430
dfcab17e 6431 if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
30a970c6 6432 cmd = 2;
dfcab17e 6433 else if (cdclk == 266667)
30a970c6
JB
6434 cmd = 1;
6435 else
6436 cmd = 0;
6437
6438 mutex_lock(&dev_priv->rps.hw_lock);
6439 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
6440 val &= ~DSPFREQGUAR_MASK;
6441 val |= (cmd << DSPFREQGUAR_SHIFT);
6442 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
6443 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
6444 DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
6445 50)) {
6446 DRM_ERROR("timed out waiting for CDclk change\n");
6447 }
6448 mutex_unlock(&dev_priv->rps.hw_lock);
6449
54433e91
VS
6450 mutex_lock(&dev_priv->sb_lock);
6451
dfcab17e 6452 if (cdclk == 400000) {
6bcda4f0 6453 u32 divider;
30a970c6 6454
6bcda4f0 6455 divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
30a970c6 6456
30a970c6
JB
6457 /* adjust cdclk divider */
6458 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
87d5d259 6459 val &= ~CCK_FREQUENCY_VALUES;
30a970c6
JB
6460 val |= divider;
6461 vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
a877e801
VS
6462
6463 if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
87d5d259 6464 CCK_FREQUENCY_STATUS) == (divider << CCK_FREQUENCY_STATUS_SHIFT),
a877e801
VS
6465 50))
6466 DRM_ERROR("timed out waiting for CDclk change\n");
30a970c6
JB
6467 }
6468
30a970c6
JB
6469 /* adjust self-refresh exit latency value */
6470 val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
6471 val &= ~0x7f;
6472
6473 /*
6474 * For high bandwidth configs, we set a higher latency in the bunit
6475 * so that the core display fetch happens in time to avoid underruns.
6476 */
dfcab17e 6477 if (cdclk == 400000)
30a970c6
JB
6478 val |= 4500 / 250; /* 4.5 usec */
6479 else
6480 val |= 3000 / 250; /* 3.0 usec */
6481 vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
54433e91 6482
a580516d 6483 mutex_unlock(&dev_priv->sb_lock);
30a970c6 6484
4c75b940 6485 intel_update_cdclk(dev_priv);
30a970c6
JB
6486}
6487
383c5a6a
VS
6488static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
6489{
fac5e23e 6490 struct drm_i915_private *dev_priv = to_i915(dev);
383c5a6a
VS
6491 u32 val, cmd;
6492
1353c4fb 6493 WARN_ON(dev_priv->display.get_display_clock_speed(dev_priv)
164dfd28 6494 != dev_priv->cdclk_freq);
383c5a6a
VS
6495
6496 switch (cdclk) {
383c5a6a
VS
6497 case 333333:
6498 case 320000:
383c5a6a 6499 case 266667:
383c5a6a 6500 case 200000:
383c5a6a
VS
6501 break;
6502 default:
5f77eeb0 6503 MISSING_CASE(cdclk);
383c5a6a
VS
6504 return;
6505 }
6506
9d0d3fda
VS
6507 /*
6508 * Specs are full of misinformation, but testing on actual
6509 * hardware has shown that we just need to write the desired
6510 * CCK divider into the Punit register.
6511 */
6512 cmd = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
6513
383c5a6a
VS
6514 mutex_lock(&dev_priv->rps.hw_lock);
6515 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
6516 val &= ~DSPFREQGUAR_MASK_CHV;
6517 val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
6518 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
6519 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
6520 DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
6521 50)) {
6522 DRM_ERROR("timed out waiting for CDclk change\n");
6523 }
6524 mutex_unlock(&dev_priv->rps.hw_lock);
6525
4c75b940 6526 intel_update_cdclk(dev_priv);
383c5a6a
VS
6527}
6528
30a970c6
JB
6529static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
6530 int max_pixclk)
6531{
6bcda4f0 6532 int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
6cca3195 6533 int limit = IS_CHERRYVIEW(dev_priv) ? 95 : 90;
29dc7ef3 6534
30a970c6
JB
6535 /*
6536 * Really only a few cases to deal with, as only 4 CDclks are supported:
6537 * 200MHz
6538 * 267MHz
29dc7ef3 6539 * 320/333MHz (depends on HPLL freq)
6cca3195
VS
6540 * 400MHz (VLV only)
6541 * So we check to see whether we're above 90% (VLV) or 95% (CHV)
6542 * of the lower bin and adjust if needed.
e37c67a1
VS
6543 *
6544 * We seem to get an unstable or solid color picture at 200MHz.
6545 * Not sure what's wrong. For now use 200MHz only when all pipes
6546 * are off.
30a970c6 6547 */
6cca3195
VS
6548 if (!IS_CHERRYVIEW(dev_priv) &&
6549 max_pixclk > freq_320*limit/100)
dfcab17e 6550 return 400000;
6cca3195 6551 else if (max_pixclk > 266667*limit/100)
29dc7ef3 6552 return freq_320;
e37c67a1 6553 else if (max_pixclk > 0)
dfcab17e 6554 return 266667;
e37c67a1
VS
6555 else
6556 return 200000;
30a970c6
JB
6557}
6558
324513c0 6559static int bxt_calc_cdclk(int max_pixclk)
f8437dd1 6560{
760e1477 6561 if (max_pixclk > 576000)
f8437dd1 6562 return 624000;
760e1477 6563 else if (max_pixclk > 384000)
f8437dd1 6564 return 576000;
760e1477 6565 else if (max_pixclk > 288000)
f8437dd1 6566 return 384000;
760e1477 6567 else if (max_pixclk > 144000)
f8437dd1
VK
6568 return 288000;
6569 else
6570 return 144000;
6571}
6572
e8788cbc 6573/* Compute the max pixel clock for new configuration. */
a821fc46
ACO
6574static int intel_mode_max_pixclk(struct drm_device *dev,
6575 struct drm_atomic_state *state)
30a970c6 6576{
565602d7 6577 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
fac5e23e 6578 struct drm_i915_private *dev_priv = to_i915(dev);
565602d7
ML
6579 struct drm_crtc *crtc;
6580 struct drm_crtc_state *crtc_state;
6581 unsigned max_pixclk = 0, i;
6582 enum pipe pipe;
30a970c6 6583
565602d7
ML
6584 memcpy(intel_state->min_pixclk, dev_priv->min_pixclk,
6585 sizeof(intel_state->min_pixclk));
304603f4 6586
565602d7
ML
6587 for_each_crtc_in_state(state, crtc, crtc_state, i) {
6588 int pixclk = 0;
6589
6590 if (crtc_state->enable)
6591 pixclk = crtc_state->adjusted_mode.crtc_clock;
304603f4 6592
565602d7 6593 intel_state->min_pixclk[i] = pixclk;
30a970c6
JB
6594 }
6595
565602d7
ML
6596 for_each_pipe(dev_priv, pipe)
6597 max_pixclk = max(intel_state->min_pixclk[pipe], max_pixclk);
6598
30a970c6
JB
6599 return max_pixclk;
6600}
6601
27c329ed 6602static int valleyview_modeset_calc_cdclk(struct drm_atomic_state *state)
30a970c6 6603{
27c329ed 6604 struct drm_device *dev = state->dev;
fac5e23e 6605 struct drm_i915_private *dev_priv = to_i915(dev);
27c329ed 6606 int max_pixclk = intel_mode_max_pixclk(dev, state);
1a617b77
ML
6607 struct intel_atomic_state *intel_state =
6608 to_intel_atomic_state(state);
30a970c6 6609
1a617b77 6610 intel_state->cdclk = intel_state->dev_cdclk =
27c329ed 6611 valleyview_calc_cdclk(dev_priv, max_pixclk);
0a9ab303 6612
1a617b77
ML
6613 if (!intel_state->active_crtcs)
6614 intel_state->dev_cdclk = valleyview_calc_cdclk(dev_priv, 0);
6615
27c329ed
ML
6616 return 0;
6617}
304603f4 6618
324513c0 6619static int bxt_modeset_calc_cdclk(struct drm_atomic_state *state)
27c329ed 6620{
4e5ca60f 6621 int max_pixclk = ilk_max_pixel_rate(state);
1a617b77
ML
6622 struct intel_atomic_state *intel_state =
6623 to_intel_atomic_state(state);
85a96e7a 6624
1a617b77 6625 intel_state->cdclk = intel_state->dev_cdclk =
324513c0 6626 bxt_calc_cdclk(max_pixclk);
85a96e7a 6627
1a617b77 6628 if (!intel_state->active_crtcs)
324513c0 6629 intel_state->dev_cdclk = bxt_calc_cdclk(0);
1a617b77 6630
27c329ed 6631 return 0;
30a970c6
JB
6632}
6633
1e69cd74
VS
6634static void vlv_program_pfi_credits(struct drm_i915_private *dev_priv)
6635{
6636 unsigned int credits, default_credits;
6637
6638 if (IS_CHERRYVIEW(dev_priv))
6639 default_credits = PFI_CREDIT(12);
6640 else
6641 default_credits = PFI_CREDIT(8);
6642
bfa7df01 6643 if (dev_priv->cdclk_freq >= dev_priv->czclk_freq) {
1e69cd74
VS
6644 /* CHV suggested value is 31 or 63 */
6645 if (IS_CHERRYVIEW(dev_priv))
fcc0008f 6646 credits = PFI_CREDIT_63;
1e69cd74
VS
6647 else
6648 credits = PFI_CREDIT(15);
6649 } else {
6650 credits = default_credits;
6651 }
6652
6653 /*
6654 * WA - write default credits before re-programming
6655 * FIXME: should we also set the resend bit here?
6656 */
6657 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
6658 default_credits);
6659
6660 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
6661 credits | PFI_CREDIT_RESEND);
6662
6663 /*
6664 * FIXME is this guaranteed to clear
6665 * immediately or should we poll for it?
6666 */
6667 WARN_ON(I915_READ(GCI_CONTROL) & PFI_CREDIT_RESEND);
6668}
6669
27c329ed 6670static void valleyview_modeset_commit_cdclk(struct drm_atomic_state *old_state)
30a970c6 6671{
a821fc46 6672 struct drm_device *dev = old_state->dev;
fac5e23e 6673 struct drm_i915_private *dev_priv = to_i915(dev);
1a617b77
ML
6674 struct intel_atomic_state *old_intel_state =
6675 to_intel_atomic_state(old_state);
6676 unsigned req_cdclk = old_intel_state->dev_cdclk;
30a970c6 6677
27c329ed
ML
6678 /*
6679 * FIXME: We can end up here with all power domains off, yet
6680 * with a CDCLK frequency other than the minimum. To account
6681 * for this take the PIPE-A power domain, which covers the HW
6682 * blocks needed for the following programming. This can be
6683 * removed once it's guaranteed that we get here either with
6684 * the minimum CDCLK set, or the required power domains
6685 * enabled.
6686 */
6687 intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
738c05c0 6688
920a14b2 6689 if (IS_CHERRYVIEW(dev_priv))
27c329ed
ML
6690 cherryview_set_cdclk(dev, req_cdclk);
6691 else
6692 valleyview_set_cdclk(dev, req_cdclk);
738c05c0 6693
27c329ed 6694 vlv_program_pfi_credits(dev_priv);
1e69cd74 6695
27c329ed 6696 intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
30a970c6
JB
6697}
6698
4a806558
ML
6699static void valleyview_crtc_enable(struct intel_crtc_state *pipe_config,
6700 struct drm_atomic_state *old_state)
89b667f8 6701{
4a806558 6702 struct drm_crtc *crtc = pipe_config->base.crtc;
89b667f8 6703 struct drm_device *dev = crtc->dev;
a72e4c9f 6704 struct drm_i915_private *dev_priv = to_i915(dev);
89b667f8 6705 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
89b667f8 6706 int pipe = intel_crtc->pipe;
89b667f8 6707
53d9f4e9 6708 if (WARN_ON(intel_crtc->active))
89b667f8
JB
6709 return;
6710
37a5650b 6711 if (intel_crtc_has_dp_encoder(intel_crtc->config))
fe3cd48d 6712 intel_dp_set_m_n(intel_crtc, M1_N1);
5b18e57c
SV
6713
6714 intel_set_pipe_timings(intel_crtc);
bc58be60 6715 intel_set_pipe_src_size(intel_crtc);
5b18e57c 6716
920a14b2 6717 if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_B) {
fac5e23e 6718 struct drm_i915_private *dev_priv = to_i915(dev);
c14b0485
VS
6719
6720 I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
6721 I915_WRITE(CHV_CANVAS(pipe), 0);
6722 }
6723
5b18e57c
SV
6724 i9xx_set_pipeconf(intel_crtc);
6725
89b667f8 6726 intel_crtc->active = true;
89b667f8 6727
a72e4c9f 6728 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4a3436e8 6729
fd6bbda9 6730 intel_encoders_pre_pll_enable(crtc, pipe_config, old_state);
89b667f8 6731
920a14b2 6732 if (IS_CHERRYVIEW(dev_priv)) {
cd2d34d9
VS
6733 chv_prepare_pll(intel_crtc, intel_crtc->config);
6734 chv_enable_pll(intel_crtc, intel_crtc->config);
6735 } else {
6736 vlv_prepare_pll(intel_crtc, intel_crtc->config);
6737 vlv_enable_pll(intel_crtc, intel_crtc->config);
9d556c99 6738 }
89b667f8 6739
fd6bbda9 6740 intel_encoders_pre_enable(crtc, pipe_config, old_state);
89b667f8 6741
2dd24552
JB
6742 i9xx_pfit_enable(intel_crtc);
6743
b95c5321 6744 intel_color_load_luts(&pipe_config->base);
63cbb074 6745
432081bc 6746 intel_update_watermarks(intel_crtc);
e1fdc473 6747 intel_enable_pipe(intel_crtc);
be6a6f8e 6748
4b3a9526
VS
6749 assert_vblank_disabled(crtc);
6750 drm_crtc_vblank_on(crtc);
6751
fd6bbda9 6752 intel_encoders_enable(crtc, pipe_config, old_state);
89b667f8
JB
6753}
6754
f13c2ef3
SV
6755static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
6756{
6757 struct drm_device *dev = crtc->base.dev;
fac5e23e 6758 struct drm_i915_private *dev_priv = to_i915(dev);
f13c2ef3 6759
6e3c9717
ACO
6760 I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
6761 I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
f13c2ef3
SV
6762}
6763
4a806558
ML
6764static void i9xx_crtc_enable(struct intel_crtc_state *pipe_config,
6765 struct drm_atomic_state *old_state)
79e53945 6766{
4a806558 6767 struct drm_crtc *crtc = pipe_config->base.crtc;
79e53945 6768 struct drm_device *dev = crtc->dev;
a72e4c9f 6769 struct drm_i915_private *dev_priv = to_i915(dev);
79e53945 6770 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
cd2d34d9 6771 enum pipe pipe = intel_crtc->pipe;
79e53945 6772
53d9f4e9 6773 if (WARN_ON(intel_crtc->active))
f7abfe8b
CW
6774 return;
6775
f13c2ef3
SV
6776 i9xx_set_pll_dividers(intel_crtc);
6777
37a5650b 6778 if (intel_crtc_has_dp_encoder(intel_crtc->config))
fe3cd48d 6779 intel_dp_set_m_n(intel_crtc, M1_N1);
5b18e57c
SV
6780
6781 intel_set_pipe_timings(intel_crtc);
bc58be60 6782 intel_set_pipe_src_size(intel_crtc);
5b18e57c 6783
5b18e57c
SV
6784 i9xx_set_pipeconf(intel_crtc);
6785
f7abfe8b 6786 intel_crtc->active = true;
6b383a7f 6787
5db94019 6788 if (!IS_GEN2(dev_priv))
a72e4c9f 6789 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4a3436e8 6790
fd6bbda9 6791 intel_encoders_pre_enable(crtc, pipe_config, old_state);
9d6d9f19 6792
f6736a1a
SV
6793 i9xx_enable_pll(intel_crtc);
6794
2dd24552
JB
6795 i9xx_pfit_enable(intel_crtc);
6796
b95c5321 6797 intel_color_load_luts(&pipe_config->base);
63cbb074 6798
432081bc 6799 intel_update_watermarks(intel_crtc);
e1fdc473 6800 intel_enable_pipe(intel_crtc);
be6a6f8e 6801
4b3a9526
VS
6802 assert_vblank_disabled(crtc);
6803 drm_crtc_vblank_on(crtc);
6804
fd6bbda9 6805 intel_encoders_enable(crtc, pipe_config, old_state);
0b8765c6 6806}
79e53945 6807
87476d63
SV
6808static void i9xx_pfit_disable(struct intel_crtc *crtc)
6809{
6810 struct drm_device *dev = crtc->base.dev;
fac5e23e 6811 struct drm_i915_private *dev_priv = to_i915(dev);
87476d63 6812
6e3c9717 6813 if (!crtc->config->gmch_pfit.control)
328d8e82 6814 return;
87476d63 6815
328d8e82 6816 assert_pipe_disabled(dev_priv, crtc->pipe);
87476d63 6817
328d8e82
SV
6818 DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
6819 I915_READ(PFIT_CONTROL));
6820 I915_WRITE(PFIT_CONTROL, 0);
87476d63
SV
6821}
6822
4a806558
ML
6823static void i9xx_crtc_disable(struct intel_crtc_state *old_crtc_state,
6824 struct drm_atomic_state *old_state)
0b8765c6 6825{
4a806558 6826 struct drm_crtc *crtc = old_crtc_state->base.crtc;
0b8765c6 6827 struct drm_device *dev = crtc->dev;
fac5e23e 6828 struct drm_i915_private *dev_priv = to_i915(dev);
0b8765c6
JB
6829 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6830 int pipe = intel_crtc->pipe;
ef9c3aee 6831
6304cd91
VS
6832 /*
6833 * On gen2 planes are double buffered but the pipe isn't, so we must
6834 * wait for planes to fully turn off before disabling the pipe.
6835 */
5db94019 6836 if (IS_GEN2(dev_priv))
0f0f74bc 6837 intel_wait_for_vblank(dev_priv, pipe);
6304cd91 6838
fd6bbda9 6839 intel_encoders_disable(crtc, old_crtc_state, old_state);
4b3a9526 6840
f9b61ff6
SV
6841 drm_crtc_vblank_off(crtc);
6842 assert_vblank_disabled(crtc);
6843
575f7ab7 6844 intel_disable_pipe(intel_crtc);
24a1f16d 6845
87476d63 6846 i9xx_pfit_disable(intel_crtc);
24a1f16d 6847
fd6bbda9 6848 intel_encoders_post_disable(crtc, old_crtc_state, old_state);
89b667f8 6849
d7edc4e5 6850 if (!intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_DSI)) {
920a14b2 6851 if (IS_CHERRYVIEW(dev_priv))
076ed3b2 6852 chv_disable_pll(dev_priv, pipe);
11a914c2 6853 else if (IS_VALLEYVIEW(dev_priv))
076ed3b2
CML
6854 vlv_disable_pll(dev_priv, pipe);
6855 else
1c4e0274 6856 i9xx_disable_pll(intel_crtc);
076ed3b2 6857 }
0b8765c6 6858
fd6bbda9 6859 intel_encoders_post_pll_disable(crtc, old_crtc_state, old_state);
d6db995f 6860
5db94019 6861 if (!IS_GEN2(dev_priv))
a72e4c9f 6862 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
0b8765c6
JB
6863}
6864
b17d48e2
ML
6865static void intel_crtc_disable_noatomic(struct drm_crtc *crtc)
6866{
842e0307 6867 struct intel_encoder *encoder;
b17d48e2
ML
6868 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6869 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
6870 enum intel_display_power_domain domain;
6871 unsigned long domains;
4a806558
ML
6872 struct drm_atomic_state *state;
6873 struct intel_crtc_state *crtc_state;
6874 int ret;
b17d48e2
ML
6875
6876 if (!intel_crtc->active)
6877 return;
6878
936e71e3 6879 if (to_intel_plane_state(crtc->primary->state)->base.visible) {
5a21b665 6880 WARN_ON(intel_crtc->flip_work);
fc32b1fd 6881
2622a081 6882 intel_pre_disable_primary_noatomic(crtc);
54a41961
ML
6883
6884 intel_crtc_disable_planes(crtc, 1 << drm_plane_index(crtc->primary));
936e71e3 6885 to_intel_plane_state(crtc->primary->state)->base.visible = false;
a539205a
ML
6886 }
6887
4a806558
ML
6888 state = drm_atomic_state_alloc(crtc->dev);
6889 state->acquire_ctx = crtc->dev->mode_config.acquire_ctx;
6890
6891 /* Everything's already locked, -EDEADLK can't happen. */
6892 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
6893 ret = drm_atomic_add_affected_connectors(state, crtc);
6894
6895 WARN_ON(IS_ERR(crtc_state) || ret);
6896
6897 dev_priv->display.crtc_disable(crtc_state, state);
6898
0853695c 6899 drm_atomic_state_put(state);
842e0307 6900
78108b7c
VS
6901 DRM_DEBUG_KMS("[CRTC:%d:%s] hw state adjusted, was enabled, now disabled\n",
6902 crtc->base.id, crtc->name);
842e0307
ML
6903
6904 WARN_ON(drm_atomic_set_mode_for_crtc(crtc->state, NULL) < 0);
6905 crtc->state->active = false;
37d9078b 6906 intel_crtc->active = false;
842e0307
ML
6907 crtc->enabled = false;
6908 crtc->state->connector_mask = 0;
6909 crtc->state->encoder_mask = 0;
6910
6911 for_each_encoder_on_crtc(crtc->dev, crtc, encoder)
6912 encoder->base.crtc = NULL;
6913
58f9c0bc 6914 intel_fbc_disable(intel_crtc);
432081bc 6915 intel_update_watermarks(intel_crtc);
1f7457b1 6916 intel_disable_shared_dpll(intel_crtc);
b17d48e2
ML
6917
6918 domains = intel_crtc->enabled_power_domains;
6919 for_each_power_domain(domain, domains)
6920 intel_display_power_put(dev_priv, domain);
6921 intel_crtc->enabled_power_domains = 0;
565602d7
ML
6922
6923 dev_priv->active_crtcs &= ~(1 << intel_crtc->pipe);
6924 dev_priv->min_pixclk[intel_crtc->pipe] = 0;
b17d48e2
ML
6925}
6926
6b72d486
ML
6927/*
6928 * turn all crtc's off, but do not adjust state
6929 * This has to be paired with a call to intel_modeset_setup_hw_state.
6930 */
70e0bd74 6931int intel_display_suspend(struct drm_device *dev)
ee7b9f93 6932{
e2c8b870 6933 struct drm_i915_private *dev_priv = to_i915(dev);
70e0bd74 6934 struct drm_atomic_state *state;
e2c8b870 6935 int ret;
70e0bd74 6936
e2c8b870
ML
6937 state = drm_atomic_helper_suspend(dev);
6938 ret = PTR_ERR_OR_ZERO(state);
70e0bd74
ML
6939 if (ret)
6940 DRM_ERROR("Suspending crtc's failed with %i\n", ret);
e2c8b870
ML
6941 else
6942 dev_priv->modeset_restore_state = state;
70e0bd74 6943 return ret;
ee7b9f93
JB
6944}
6945
ea5b213a 6946void intel_encoder_destroy(struct drm_encoder *encoder)
7e7d76c3 6947{
4ef69c7a 6948 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
ea5b213a 6949
ea5b213a
CW
6950 drm_encoder_cleanup(encoder);
6951 kfree(intel_encoder);
7e7d76c3
JB
6952}
6953
0a91ca29
SV
6954/* Cross check the actual hw state with our own modeset state tracking (and it's
6955 * internal consistency). */
5a21b665 6956static void intel_connector_verify_state(struct intel_connector *connector)
79e53945 6957{
5a21b665 6958 struct drm_crtc *crtc = connector->base.state->crtc;
35dd3c64
ML
6959
6960 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
6961 connector->base.base.id,
6962 connector->base.name);
6963
0a91ca29 6964 if (connector->get_hw_state(connector)) {
e85376cb 6965 struct intel_encoder *encoder = connector->encoder;
5a21b665 6966 struct drm_connector_state *conn_state = connector->base.state;
0a91ca29 6967
35dd3c64
ML
6968 I915_STATE_WARN(!crtc,
6969 "connector enabled without attached crtc\n");
0a91ca29 6970
35dd3c64
ML
6971 if (!crtc)
6972 return;
6973
6974 I915_STATE_WARN(!crtc->state->active,
6975 "connector is active, but attached crtc isn't\n");
6976
e85376cb 6977 if (!encoder || encoder->type == INTEL_OUTPUT_DP_MST)
35dd3c64
ML
6978 return;
6979
e85376cb 6980 I915_STATE_WARN(conn_state->best_encoder != &encoder->base,
35dd3c64
ML
6981 "atomic encoder doesn't match attached encoder\n");
6982
e85376cb 6983 I915_STATE_WARN(conn_state->crtc != encoder->base.crtc,
35dd3c64
ML
6984 "attached encoder crtc differs from connector crtc\n");
6985 } else {
4d688a2a
ML
6986 I915_STATE_WARN(crtc && crtc->state->active,
6987 "attached crtc is active, but connector isn't\n");
5a21b665 6988 I915_STATE_WARN(!crtc && connector->base.state->best_encoder,
35dd3c64 6989 "best encoder set without crtc!\n");
0a91ca29 6990 }
79e53945
JB
6991}
6992
08d9bc92
ACO
6993int intel_connector_init(struct intel_connector *connector)
6994{
5350a031 6995 drm_atomic_helper_connector_reset(&connector->base);
08d9bc92 6996
5350a031 6997 if (!connector->base.state)
08d9bc92
ACO
6998 return -ENOMEM;
6999
08d9bc92
ACO
7000 return 0;
7001}
7002
7003struct intel_connector *intel_connector_alloc(void)
7004{
7005 struct intel_connector *connector;
7006
7007 connector = kzalloc(sizeof *connector, GFP_KERNEL);
7008 if (!connector)
7009 return NULL;
7010
7011 if (intel_connector_init(connector) < 0) {
7012 kfree(connector);
7013 return NULL;
7014 }
7015
7016 return connector;
7017}
7018
f0947c37
SV
7019/* Simple connector->get_hw_state implementation for encoders that support only
7020 * one connector and no cloning and hence the encoder state determines the state
7021 * of the connector. */
7022bool intel_connector_get_hw_state(struct intel_connector *connector)
ea5b213a 7023{
24929352 7024 enum pipe pipe = 0;
f0947c37 7025 struct intel_encoder *encoder = connector->encoder;
ea5b213a 7026
f0947c37 7027 return encoder->get_hw_state(encoder, &pipe);
ea5b213a
CW
7028}
7029
6d293983 7030static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
d272ddfa 7031{
6d293983
ACO
7032 if (crtc_state->base.enable && crtc_state->has_pch_encoder)
7033 return crtc_state->fdi_lanes;
d272ddfa
VS
7034
7035 return 0;
7036}
7037
6d293983 7038static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
5cec258b 7039 struct intel_crtc_state *pipe_config)
1857e1da 7040{
8652744b 7041 struct drm_i915_private *dev_priv = to_i915(dev);
6d293983
ACO
7042 struct drm_atomic_state *state = pipe_config->base.state;
7043 struct intel_crtc *other_crtc;
7044 struct intel_crtc_state *other_crtc_state;
7045
1857e1da
SV
7046 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
7047 pipe_name(pipe), pipe_config->fdi_lanes);
7048 if (pipe_config->fdi_lanes > 4) {
7049 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
7050 pipe_name(pipe), pipe_config->fdi_lanes);
6d293983 7051 return -EINVAL;
1857e1da
SV
7052 }
7053
8652744b 7054 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
1857e1da
SV
7055 if (pipe_config->fdi_lanes > 2) {
7056 DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
7057 pipe_config->fdi_lanes);
6d293983 7058 return -EINVAL;
1857e1da 7059 } else {
6d293983 7060 return 0;
1857e1da
SV
7061 }
7062 }
7063
b7f05d4a 7064 if (INTEL_INFO(dev_priv)->num_pipes == 2)
6d293983 7065 return 0;
1857e1da
SV
7066
7067 /* Ivybridge 3 pipe is really complicated */
7068 switch (pipe) {
7069 case PIPE_A:
6d293983 7070 return 0;
1857e1da 7071 case PIPE_B:
6d293983
ACO
7072 if (pipe_config->fdi_lanes <= 2)
7073 return 0;
7074
b91eb5cc 7075 other_crtc = intel_get_crtc_for_pipe(dev_priv, PIPE_C);
6d293983
ACO
7076 other_crtc_state =
7077 intel_atomic_get_crtc_state(state, other_crtc);
7078 if (IS_ERR(other_crtc_state))
7079 return PTR_ERR(other_crtc_state);
7080
7081 if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
1857e1da
SV
7082 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
7083 pipe_name(pipe), pipe_config->fdi_lanes);
6d293983 7084 return -EINVAL;
1857e1da 7085 }
6d293983 7086 return 0;
1857e1da 7087 case PIPE_C:
251cc67c
VS
7088 if (pipe_config->fdi_lanes > 2) {
7089 DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
7090 pipe_name(pipe), pipe_config->fdi_lanes);
6d293983 7091 return -EINVAL;
251cc67c 7092 }
6d293983 7093
b91eb5cc 7094 other_crtc = intel_get_crtc_for_pipe(dev_priv, PIPE_B);
6d293983
ACO
7095 other_crtc_state =
7096 intel_atomic_get_crtc_state(state, other_crtc);
7097 if (IS_ERR(other_crtc_state))
7098 return PTR_ERR(other_crtc_state);
7099
7100 if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
1857e1da 7101 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
6d293983 7102 return -EINVAL;
1857e1da 7103 }
6d293983 7104 return 0;
1857e1da
SV
7105 default:
7106 BUG();
7107 }
7108}
7109
e29c22c0
SV
7110#define RETRY 1
7111static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
5cec258b 7112 struct intel_crtc_state *pipe_config)
877d48d5 7113{
1857e1da 7114 struct drm_device *dev = intel_crtc->base.dev;
7c5f93b0 7115 const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
6d293983
ACO
7116 int lane, link_bw, fdi_dotclock, ret;
7117 bool needs_recompute = false;
877d48d5 7118
e29c22c0 7119retry:
877d48d5
SV
7120 /* FDI is a binary signal running at ~2.7GHz, encoding
7121 * each output octet as 10 bits. The actual frequency
7122 * is stored as a divider into a 100MHz clock, and the
7123 * mode pixel clock is stored in units of 1KHz.
7124 * Hence the bw of each lane in terms of the mode signal
7125 * is:
7126 */
21a727b3 7127 link_bw = intel_fdi_link_freq(to_i915(dev), pipe_config);
877d48d5 7128
241bfc38 7129 fdi_dotclock = adjusted_mode->crtc_clock;
877d48d5 7130
2bd89a07 7131 lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
877d48d5
SV
7132 pipe_config->pipe_bpp);
7133
7134 pipe_config->fdi_lanes = lane;
7135
2bd89a07 7136 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
877d48d5 7137 link_bw, &pipe_config->fdi_m_n);
1857e1da 7138
e3b247da 7139 ret = ironlake_check_fdi_lanes(dev, intel_crtc->pipe, pipe_config);
6d293983 7140 if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
e29c22c0
SV
7141 pipe_config->pipe_bpp -= 2*3;
7142 DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
7143 pipe_config->pipe_bpp);
7144 needs_recompute = true;
7145 pipe_config->bw_constrained = true;
7146
7147 goto retry;
7148 }
7149
7150 if (needs_recompute)
7151 return RETRY;
7152
6d293983 7153 return ret;
877d48d5
SV
7154}
7155
8cfb3407
VS
7156static bool pipe_config_supports_ips(struct drm_i915_private *dev_priv,
7157 struct intel_crtc_state *pipe_config)
7158{
7159 if (pipe_config->pipe_bpp > 24)
7160 return false;
7161
7162 /* HSW can handle pixel rate up to cdclk? */
2d1fe073 7163 if (IS_HASWELL(dev_priv))
8cfb3407
VS
7164 return true;
7165
7166 /*
b432e5cf
VS
7167 * We compare against max which means we must take
7168 * the increased cdclk requirement into account when
7169 * calculating the new cdclk.
7170 *
7171 * Should measure whether using a lower cdclk w/o IPS
8cfb3407
VS
7172 */
7173 return ilk_pipe_pixel_rate(pipe_config) <=
7174 dev_priv->max_cdclk_freq * 95 / 100;
7175}
7176
42db64ef 7177static void hsw_compute_ips_config(struct intel_crtc *crtc,
5cec258b 7178 struct intel_crtc_state *pipe_config)
42db64ef 7179{
8cfb3407 7180 struct drm_device *dev = crtc->base.dev;
fac5e23e 7181 struct drm_i915_private *dev_priv = to_i915(dev);
8cfb3407 7182
d330a953 7183 pipe_config->ips_enabled = i915.enable_ips &&
8cfb3407
VS
7184 hsw_crtc_supports_ips(crtc) &&
7185 pipe_config_supports_ips(dev_priv, pipe_config);
42db64ef
PZ
7186}
7187
39acb4aa
VS
7188static bool intel_crtc_supports_double_wide(const struct intel_crtc *crtc)
7189{
7190 const struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
7191
7192 /* GDG double wide on either pipe, otherwise pipe A only */
7193 return INTEL_INFO(dev_priv)->gen < 4 &&
7194 (crtc->pipe == PIPE_A || IS_I915G(dev_priv));
7195}
7196
a43f6e0f 7197static int intel_crtc_compute_config(struct intel_crtc *crtc,
5cec258b 7198 struct intel_crtc_state *pipe_config)
79e53945 7199{
a43f6e0f 7200 struct drm_device *dev = crtc->base.dev;
fac5e23e 7201 struct drm_i915_private *dev_priv = to_i915(dev);
7c5f93b0 7202 const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
f3261156 7203 int clock_limit = dev_priv->max_dotclk_freq;
89749350 7204
cf532bb2 7205 if (INTEL_INFO(dev)->gen < 4) {
f3261156 7206 clock_limit = dev_priv->max_cdclk_freq * 9 / 10;
cf532bb2
VS
7207
7208 /*
39acb4aa 7209 * Enable double wide mode when the dot clock
cf532bb2 7210 * is > 90% of the (display) core speed.
cf532bb2 7211 */
39acb4aa
VS
7212 if (intel_crtc_supports_double_wide(crtc) &&
7213 adjusted_mode->crtc_clock > clock_limit) {
f3261156 7214 clock_limit = dev_priv->max_dotclk_freq;
cf532bb2 7215 pipe_config->double_wide = true;
ad3a4479 7216 }
f3261156 7217 }
ad3a4479 7218
f3261156
VS
7219 if (adjusted_mode->crtc_clock > clock_limit) {
7220 DRM_DEBUG_KMS("requested pixel clock (%d kHz) too high (max: %d kHz, double wide: %s)\n",
7221 adjusted_mode->crtc_clock, clock_limit,
7222 yesno(pipe_config->double_wide));
7223 return -EINVAL;
2c07245f 7224 }
89749350 7225
1d1d0e27
VS
7226 /*
7227 * Pipe horizontal size must be even in:
7228 * - DVO ganged mode
7229 * - LVDS dual channel mode
7230 * - Double wide pipe
7231 */
2d84d2b3 7232 if ((intel_crtc_has_type(pipe_config, INTEL_OUTPUT_LVDS) &&
1d1d0e27
VS
7233 intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
7234 pipe_config->pipe_src_w &= ~1;
7235
8693a824
DL
7236 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
7237 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
44f46b42 7238 */
9beb5fea 7239 if ((INTEL_GEN(dev_priv) > 4 || IS_G4X(dev_priv)) &&
aad941d5 7240 adjusted_mode->crtc_hsync_start == adjusted_mode->crtc_hdisplay)
e29c22c0 7241 return -EINVAL;
44f46b42 7242
50a0bc90 7243 if (HAS_IPS(dev_priv))
a43f6e0f
SV
7244 hsw_compute_ips_config(crtc, pipe_config);
7245
877d48d5 7246 if (pipe_config->has_pch_encoder)
a43f6e0f 7247 return ironlake_fdi_compute_config(crtc, pipe_config);
877d48d5 7248
cf5a15be 7249 return 0;
79e53945
JB
7250}
7251
1353c4fb 7252static int skylake_get_display_clock_speed(struct drm_i915_private *dev_priv)
1652d19e 7253{
1353c4fb 7254 u32 cdctl;
1652d19e 7255
ea61791e 7256 skl_dpll0_update(dev_priv);
1652d19e 7257
63911d72 7258 if (dev_priv->cdclk_pll.vco == 0)
709e05c3 7259 return dev_priv->cdclk_pll.ref;
1652d19e 7260
ea61791e 7261 cdctl = I915_READ(CDCLK_CTL);
1652d19e 7262
63911d72 7263 if (dev_priv->cdclk_pll.vco == 8640000) {
1652d19e
VS
7264 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
7265 case CDCLK_FREQ_450_432:
7266 return 432000;
7267 case CDCLK_FREQ_337_308:
487ed2e4 7268 return 308571;
ea61791e
VS
7269 case CDCLK_FREQ_540:
7270 return 540000;
1652d19e 7271 case CDCLK_FREQ_675_617:
487ed2e4 7272 return 617143;
1652d19e 7273 default:
ea61791e 7274 MISSING_CASE(cdctl & CDCLK_FREQ_SEL_MASK);
1652d19e
VS
7275 }
7276 } else {
1652d19e
VS
7277 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
7278 case CDCLK_FREQ_450_432:
7279 return 450000;
7280 case CDCLK_FREQ_337_308:
7281 return 337500;
ea61791e
VS
7282 case CDCLK_FREQ_540:
7283 return 540000;
1652d19e
VS
7284 case CDCLK_FREQ_675_617:
7285 return 675000;
7286 default:
ea61791e 7287 MISSING_CASE(cdctl & CDCLK_FREQ_SEL_MASK);
1652d19e
VS
7288 }
7289 }
7290
709e05c3 7291 return dev_priv->cdclk_pll.ref;
1652d19e
VS
7292}
7293
83d7c81f
VS
7294static void bxt_de_pll_update(struct drm_i915_private *dev_priv)
7295{
7296 u32 val;
7297
7298 dev_priv->cdclk_pll.ref = 19200;
1c3f7700 7299 dev_priv->cdclk_pll.vco = 0;
83d7c81f
VS
7300
7301 val = I915_READ(BXT_DE_PLL_ENABLE);
1c3f7700 7302 if ((val & BXT_DE_PLL_PLL_ENABLE) == 0)
83d7c81f 7303 return;
83d7c81f 7304
1c3f7700
ID
7305 if (WARN_ON((val & BXT_DE_PLL_LOCK) == 0))
7306 return;
83d7c81f
VS
7307
7308 val = I915_READ(BXT_DE_PLL_CTL);
7309 dev_priv->cdclk_pll.vco = (val & BXT_DE_PLL_RATIO_MASK) *
7310 dev_priv->cdclk_pll.ref;
7311}
7312
1353c4fb 7313static int broxton_get_display_clock_speed(struct drm_i915_private *dev_priv)
acd3f3d3 7314{
f5986242
VS
7315 u32 divider;
7316 int div, vco;
acd3f3d3 7317
83d7c81f
VS
7318 bxt_de_pll_update(dev_priv);
7319
f5986242
VS
7320 vco = dev_priv->cdclk_pll.vco;
7321 if (vco == 0)
7322 return dev_priv->cdclk_pll.ref;
acd3f3d3 7323
f5986242 7324 divider = I915_READ(CDCLK_CTL) & BXT_CDCLK_CD2X_DIV_SEL_MASK;
acd3f3d3 7325
f5986242 7326 switch (divider) {
acd3f3d3 7327 case BXT_CDCLK_CD2X_DIV_SEL_1:
f5986242
VS
7328 div = 2;
7329 break;
acd3f3d3 7330 case BXT_CDCLK_CD2X_DIV_SEL_1_5:
f5986242
VS
7331 div = 3;
7332 break;
acd3f3d3 7333 case BXT_CDCLK_CD2X_DIV_SEL_2:
f5986242
VS
7334 div = 4;
7335 break;
acd3f3d3 7336 case BXT_CDCLK_CD2X_DIV_SEL_4:
f5986242
VS
7337 div = 8;
7338 break;
7339 default:
7340 MISSING_CASE(divider);
7341 return dev_priv->cdclk_pll.ref;
acd3f3d3
BP
7342 }
7343
f5986242 7344 return DIV_ROUND_CLOSEST(vco, div);
acd3f3d3
BP
7345}
7346
1353c4fb 7347static int broadwell_get_display_clock_speed(struct drm_i915_private *dev_priv)
1652d19e 7348{
1652d19e
VS
7349 uint32_t lcpll = I915_READ(LCPLL_CTL);
7350 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
7351
7352 if (lcpll & LCPLL_CD_SOURCE_FCLK)
7353 return 800000;
7354 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
7355 return 450000;
7356 else if (freq == LCPLL_CLK_FREQ_450)
7357 return 450000;
7358 else if (freq == LCPLL_CLK_FREQ_54O_BDW)
7359 return 540000;
7360 else if (freq == LCPLL_CLK_FREQ_337_5_BDW)
7361 return 337500;
7362 else
7363 return 675000;
7364}
7365
1353c4fb 7366static int haswell_get_display_clock_speed(struct drm_i915_private *dev_priv)
1652d19e 7367{
1652d19e
VS
7368 uint32_t lcpll = I915_READ(LCPLL_CTL);
7369 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
7370
7371 if (lcpll & LCPLL_CD_SOURCE_FCLK)
7372 return 800000;
7373 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
7374 return 450000;
7375 else if (freq == LCPLL_CLK_FREQ_450)
7376 return 450000;
50a0bc90 7377 else if (IS_HSW_ULT(dev_priv))
1652d19e
VS
7378 return 337500;
7379 else
7380 return 540000;
79e53945
JB
7381}
7382
1353c4fb 7383static int valleyview_get_display_clock_speed(struct drm_i915_private *dev_priv)
25eb05fc 7384{
1353c4fb 7385 return vlv_get_cck_clock_hpll(dev_priv, "cdclk",
bfa7df01 7386 CCK_DISPLAY_CLOCK_CONTROL);
25eb05fc
JB
7387}
7388
1353c4fb 7389static int ilk_get_display_clock_speed(struct drm_i915_private *dev_priv)
b37a6434
VS
7390{
7391 return 450000;
7392}
7393
1353c4fb 7394static int i945_get_display_clock_speed(struct drm_i915_private *dev_priv)
e70236a8
JB
7395{
7396 return 400000;
7397}
79e53945 7398
1353c4fb 7399static int i915_get_display_clock_speed(struct drm_i915_private *dev_priv)
79e53945 7400{
e907f170 7401 return 333333;
e70236a8 7402}
79e53945 7403
1353c4fb 7404static int i9xx_misc_get_display_clock_speed(struct drm_i915_private *dev_priv)
e70236a8
JB
7405{
7406 return 200000;
7407}
79e53945 7408
1353c4fb 7409static int pnv_get_display_clock_speed(struct drm_i915_private *dev_priv)
257a7ffc 7410{
1353c4fb 7411 struct pci_dev *pdev = dev_priv->drm.pdev;
257a7ffc
SV
7412 u16 gcfgc = 0;
7413
52a05c30 7414 pci_read_config_word(pdev, GCFGC, &gcfgc);
257a7ffc
SV
7415
7416 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
7417 case GC_DISPLAY_CLOCK_267_MHZ_PNV:
e907f170 7418 return 266667;
257a7ffc 7419 case GC_DISPLAY_CLOCK_333_MHZ_PNV:
e907f170 7420 return 333333;
257a7ffc 7421 case GC_DISPLAY_CLOCK_444_MHZ_PNV:
e907f170 7422 return 444444;
257a7ffc
SV
7423 case GC_DISPLAY_CLOCK_200_MHZ_PNV:
7424 return 200000;
7425 default:
7426 DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
7427 case GC_DISPLAY_CLOCK_133_MHZ_PNV:
e907f170 7428 return 133333;
257a7ffc 7429 case GC_DISPLAY_CLOCK_167_MHZ_PNV:
e907f170 7430 return 166667;
257a7ffc
SV
7431 }
7432}
7433
1353c4fb 7434static int i915gm_get_display_clock_speed(struct drm_i915_private *dev_priv)
e70236a8 7435{
1353c4fb 7436 struct pci_dev *pdev = dev_priv->drm.pdev;
e70236a8 7437 u16 gcfgc = 0;
79e53945 7438
52a05c30 7439 pci_read_config_word(pdev, GCFGC, &gcfgc);
e70236a8
JB
7440
7441 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
e907f170 7442 return 133333;
e70236a8
JB
7443 else {
7444 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
7445 case GC_DISPLAY_CLOCK_333_MHZ:
e907f170 7446 return 333333;
e70236a8
JB
7447 default:
7448 case GC_DISPLAY_CLOCK_190_200_MHZ:
7449 return 190000;
79e53945 7450 }
e70236a8
JB
7451 }
7452}
7453
1353c4fb 7454static int i865_get_display_clock_speed(struct drm_i915_private *dev_priv)
e70236a8 7455{
e907f170 7456 return 266667;
e70236a8
JB
7457}
7458
1353c4fb 7459static int i85x_get_display_clock_speed(struct drm_i915_private *dev_priv)
e70236a8 7460{
1353c4fb 7461 struct pci_dev *pdev = dev_priv->drm.pdev;
e70236a8 7462 u16 hpllcc = 0;
1b1d2716 7463
65cd2b3f
VS
7464 /*
7465 * 852GM/852GMV only supports 133 MHz and the HPLLCC
7466 * encoding is different :(
7467 * FIXME is this the right way to detect 852GM/852GMV?
7468 */
52a05c30 7469 if (pdev->revision == 0x1)
65cd2b3f
VS
7470 return 133333;
7471
52a05c30 7472 pci_bus_read_config_word(pdev->bus,
1b1d2716
VS
7473 PCI_DEVFN(0, 3), HPLLCC, &hpllcc);
7474
e70236a8
JB
7475 /* Assume that the hardware is in the high speed state. This
7476 * should be the default.
7477 */
7478 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
7479 case GC_CLOCK_133_200:
1b1d2716 7480 case GC_CLOCK_133_200_2:
e70236a8
JB
7481 case GC_CLOCK_100_200:
7482 return 200000;
7483 case GC_CLOCK_166_250:
7484 return 250000;
7485 case GC_CLOCK_100_133:
e907f170 7486 return 133333;
1b1d2716
VS
7487 case GC_CLOCK_133_266:
7488 case GC_CLOCK_133_266_2:
7489 case GC_CLOCK_166_266:
7490 return 266667;
e70236a8 7491 }
79e53945 7492
e70236a8
JB
7493 /* Shouldn't happen */
7494 return 0;
7495}
79e53945 7496
1353c4fb 7497static int i830_get_display_clock_speed(struct drm_i915_private *dev_priv)
e70236a8 7498{
e907f170 7499 return 133333;
79e53945
JB
7500}
7501
1353c4fb 7502static unsigned int intel_hpll_vco(struct drm_i915_private *dev_priv)
34edce2f 7503{
34edce2f
VS
7504 static const unsigned int blb_vco[8] = {
7505 [0] = 3200000,
7506 [1] = 4000000,
7507 [2] = 5333333,
7508 [3] = 4800000,
7509 [4] = 6400000,
7510 };
7511 static const unsigned int pnv_vco[8] = {
7512 [0] = 3200000,
7513 [1] = 4000000,
7514 [2] = 5333333,
7515 [3] = 4800000,
7516 [4] = 2666667,
7517 };
7518 static const unsigned int cl_vco[8] = {
7519 [0] = 3200000,
7520 [1] = 4000000,
7521 [2] = 5333333,
7522 [3] = 6400000,
7523 [4] = 3333333,
7524 [5] = 3566667,
7525 [6] = 4266667,
7526 };
7527 static const unsigned int elk_vco[8] = {
7528 [0] = 3200000,
7529 [1] = 4000000,
7530 [2] = 5333333,
7531 [3] = 4800000,
7532 };
7533 static const unsigned int ctg_vco[8] = {
7534 [0] = 3200000,
7535 [1] = 4000000,
7536 [2] = 5333333,
7537 [3] = 6400000,
7538 [4] = 2666667,
7539 [5] = 4266667,
7540 };
7541 const unsigned int *vco_table;
7542 unsigned int vco;
7543 uint8_t tmp = 0;
7544
7545 /* FIXME other chipsets? */
50a0bc90 7546 if (IS_GM45(dev_priv))
34edce2f 7547 vco_table = ctg_vco;
9beb5fea 7548 else if (IS_G4X(dev_priv))
34edce2f 7549 vco_table = elk_vco;
1353c4fb 7550 else if (IS_CRESTLINE(dev_priv))
34edce2f 7551 vco_table = cl_vco;
1353c4fb 7552 else if (IS_PINEVIEW(dev_priv))
34edce2f 7553 vco_table = pnv_vco;
1353c4fb 7554 else if (IS_G33(dev_priv))
34edce2f
VS
7555 vco_table = blb_vco;
7556 else
7557 return 0;
7558
1353c4fb 7559 tmp = I915_READ(IS_MOBILE(dev_priv) ? HPLLVCO_MOBILE : HPLLVCO);
34edce2f
VS
7560
7561 vco = vco_table[tmp & 0x7];
7562 if (vco == 0)
7563 DRM_ERROR("Bad HPLL VCO (HPLLVCO=0x%02x)\n", tmp);
7564 else
7565 DRM_DEBUG_KMS("HPLL VCO %u kHz\n", vco);
7566
7567 return vco;
7568}
7569
1353c4fb 7570static int gm45_get_display_clock_speed(struct drm_i915_private *dev_priv)
34edce2f 7571{
1353c4fb
VS
7572 struct pci_dev *pdev = dev_priv->drm.pdev;
7573 unsigned int cdclk_sel, vco = intel_hpll_vco(dev_priv);
34edce2f
VS
7574 uint16_t tmp = 0;
7575
52a05c30 7576 pci_read_config_word(pdev, GCFGC, &tmp);
34edce2f
VS
7577
7578 cdclk_sel = (tmp >> 12) & 0x1;
7579
7580 switch (vco) {
7581 case 2666667:
7582 case 4000000:
7583 case 5333333:
7584 return cdclk_sel ? 333333 : 222222;
7585 case 3200000:
7586 return cdclk_sel ? 320000 : 228571;
7587 default:
7588 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u, CFGC=0x%04x\n", vco, tmp);
7589 return 222222;
7590 }
7591}
7592
1353c4fb 7593static int i965gm_get_display_clock_speed(struct drm_i915_private *dev_priv)
34edce2f 7594{
1353c4fb 7595 struct pci_dev *pdev = dev_priv->drm.pdev;
34edce2f
VS
7596 static const uint8_t div_3200[] = { 16, 10, 8 };
7597 static const uint8_t div_4000[] = { 20, 12, 10 };
7598 static const uint8_t div_5333[] = { 24, 16, 14 };
7599 const uint8_t *div_table;
1353c4fb 7600 unsigned int cdclk_sel, vco = intel_hpll_vco(dev_priv);
34edce2f
VS
7601 uint16_t tmp = 0;
7602
52a05c30 7603 pci_read_config_word(pdev, GCFGC, &tmp);
34edce2f
VS
7604
7605 cdclk_sel = ((tmp >> 8) & 0x1f) - 1;
7606
7607 if (cdclk_sel >= ARRAY_SIZE(div_3200))
7608 goto fail;
7609
7610 switch (vco) {
7611 case 3200000:
7612 div_table = div_3200;
7613 break;
7614 case 4000000:
7615 div_table = div_4000;
7616 break;
7617 case 5333333:
7618 div_table = div_5333;
7619 break;
7620 default:
7621 goto fail;
7622 }
7623
7624 return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
7625
caf4e252 7626fail:
34edce2f
VS
7627 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%04x\n", vco, tmp);
7628 return 200000;
7629}
7630
1353c4fb 7631static int g33_get_display_clock_speed(struct drm_i915_private *dev_priv)
34edce2f 7632{
1353c4fb 7633 struct pci_dev *pdev = dev_priv->drm.pdev;
34edce2f
VS
7634 static const uint8_t div_3200[] = { 12, 10, 8, 7, 5, 16 };
7635 static const uint8_t div_4000[] = { 14, 12, 10, 8, 6, 20 };
7636 static const uint8_t div_4800[] = { 20, 14, 12, 10, 8, 24 };
7637 static const uint8_t div_5333[] = { 20, 16, 12, 12, 8, 28 };
7638 const uint8_t *div_table;
1353c4fb 7639 unsigned int cdclk_sel, vco = intel_hpll_vco(dev_priv);
34edce2f
VS
7640 uint16_t tmp = 0;
7641
52a05c30 7642 pci_read_config_word(pdev, GCFGC, &tmp);
34edce2f
VS
7643
7644 cdclk_sel = (tmp >> 4) & 0x7;
7645
7646 if (cdclk_sel >= ARRAY_SIZE(div_3200))
7647 goto fail;
7648
7649 switch (vco) {
7650 case 3200000:
7651 div_table = div_3200;
7652 break;
7653 case 4000000:
7654 div_table = div_4000;
7655 break;
7656 case 4800000:
7657 div_table = div_4800;
7658 break;
7659 case 5333333:
7660 div_table = div_5333;
7661 break;
7662 default:
7663 goto fail;
7664 }
7665
7666 return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
7667
caf4e252 7668fail:
34edce2f
VS
7669 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%08x\n", vco, tmp);
7670 return 190476;
7671}
7672
2c07245f 7673static void
a65851af 7674intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
2c07245f 7675{
a65851af
VS
7676 while (*num > DATA_LINK_M_N_MASK ||
7677 *den > DATA_LINK_M_N_MASK) {
2c07245f
ZW
7678 *num >>= 1;
7679 *den >>= 1;
7680 }
7681}
7682
a65851af
VS
7683static void compute_m_n(unsigned int m, unsigned int n,
7684 uint32_t *ret_m, uint32_t *ret_n)
7685{
7686 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
7687 *ret_m = div_u64((uint64_t) m * *ret_n, n);
7688 intel_reduce_m_n_ratio(ret_m, ret_n);
7689}
7690
e69d0bc1
SV
7691void
7692intel_link_compute_m_n(int bits_per_pixel, int nlanes,
7693 int pixel_clock, int link_clock,
7694 struct intel_link_m_n *m_n)
2c07245f 7695{
e69d0bc1 7696 m_n->tu = 64;
a65851af
VS
7697
7698 compute_m_n(bits_per_pixel * pixel_clock,
7699 link_clock * nlanes * 8,
7700 &m_n->gmch_m, &m_n->gmch_n);
7701
7702 compute_m_n(pixel_clock, link_clock,
7703 &m_n->link_m, &m_n->link_n);
2c07245f
ZW
7704}
7705
a7615030
CW
7706static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
7707{
d330a953
JN
7708 if (i915.panel_use_ssc >= 0)
7709 return i915.panel_use_ssc != 0;
41aa3448 7710 return dev_priv->vbt.lvds_use_ssc
435793df 7711 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
a7615030
CW
7712}
7713
7429e9d4 7714static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
c65d77d8 7715{
7df00d7a 7716 return (1 << dpll->n) << 16 | dpll->m2;
7429e9d4 7717}
f47709a9 7718
7429e9d4
SV
7719static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
7720{
7721 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
c65d77d8
JB
7722}
7723
f47709a9 7724static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
190f68c5 7725 struct intel_crtc_state *crtc_state,
9e2c8475 7726 struct dpll *reduced_clock)
a7516a05 7727{
9b1e14f4 7728 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
a7516a05
JB
7729 u32 fp, fp2 = 0;
7730
9b1e14f4 7731 if (IS_PINEVIEW(dev_priv)) {
190f68c5 7732 fp = pnv_dpll_compute_fp(&crtc_state->dpll);
a7516a05 7733 if (reduced_clock)
7429e9d4 7734 fp2 = pnv_dpll_compute_fp(reduced_clock);
a7516a05 7735 } else {
190f68c5 7736 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
a7516a05 7737 if (reduced_clock)
7429e9d4 7738 fp2 = i9xx_dpll_compute_fp(reduced_clock);
a7516a05
JB
7739 }
7740
190f68c5 7741 crtc_state->dpll_hw_state.fp0 = fp;
a7516a05 7742
f47709a9 7743 crtc->lowfreq_avail = false;
2d84d2b3 7744 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
ab585dea 7745 reduced_clock) {
190f68c5 7746 crtc_state->dpll_hw_state.fp1 = fp2;
f47709a9 7747 crtc->lowfreq_avail = true;
a7516a05 7748 } else {
190f68c5 7749 crtc_state->dpll_hw_state.fp1 = fp;
a7516a05
JB
7750 }
7751}
7752
5e69f97f
CML
7753static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
7754 pipe)
89b667f8
JB
7755{
7756 u32 reg_val;
7757
7758 /*
7759 * PLLB opamp always calibrates to max value of 0x3f, force enable it
7760 * and set it to a reasonable value instead.
7761 */
ab3c759a 7762 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
89b667f8
JB
7763 reg_val &= 0xffffff00;
7764 reg_val |= 0x00000030;
ab3c759a 7765 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
89b667f8 7766
ab3c759a 7767 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
89b667f8
JB
7768 reg_val &= 0x8cffffff;
7769 reg_val = 0x8c000000;
ab3c759a 7770 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
89b667f8 7771
ab3c759a 7772 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
89b667f8 7773 reg_val &= 0xffffff00;
ab3c759a 7774 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
89b667f8 7775
ab3c759a 7776 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
89b667f8
JB
7777 reg_val &= 0x00ffffff;
7778 reg_val |= 0xb0000000;
ab3c759a 7779 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
89b667f8
JB
7780}
7781
b551842d
SV
7782static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
7783 struct intel_link_m_n *m_n)
7784{
7785 struct drm_device *dev = crtc->base.dev;
fac5e23e 7786 struct drm_i915_private *dev_priv = to_i915(dev);
b551842d
SV
7787 int pipe = crtc->pipe;
7788
e3b95f1e
SV
7789 I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7790 I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
7791 I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
7792 I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
b551842d
SV
7793}
7794
7795static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
f769cd24
VK
7796 struct intel_link_m_n *m_n,
7797 struct intel_link_m_n *m2_n2)
b551842d
SV
7798{
7799 struct drm_device *dev = crtc->base.dev;
fac5e23e 7800 struct drm_i915_private *dev_priv = to_i915(dev);
b551842d 7801 int pipe = crtc->pipe;
6e3c9717 7802 enum transcoder transcoder = crtc->config->cpu_transcoder;
b551842d
SV
7803
7804 if (INTEL_INFO(dev)->gen >= 5) {
7805 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
7806 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
7807 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
7808 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
f769cd24
VK
7809 /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
7810 * for gen < 8) and if DRRS is supported (to make sure the
7811 * registers are not unnecessarily accessed).
7812 */
920a14b2
TU
7813 if (m2_n2 && (IS_CHERRYVIEW(dev_priv) ||
7814 INTEL_GEN(dev_priv) < 8) && crtc->config->has_drrs) {
f769cd24
VK
7815 I915_WRITE(PIPE_DATA_M2(transcoder),
7816 TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
7817 I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
7818 I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
7819 I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
7820 }
b551842d 7821 } else {
e3b95f1e
SV
7822 I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7823 I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
7824 I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
7825 I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
b551842d
SV
7826 }
7827}
7828
fe3cd48d 7829void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
03afc4a2 7830{
fe3cd48d
R
7831 struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
7832
7833 if (m_n == M1_N1) {
7834 dp_m_n = &crtc->config->dp_m_n;
7835 dp_m2_n2 = &crtc->config->dp_m2_n2;
7836 } else if (m_n == M2_N2) {
7837
7838 /*
7839 * M2_N2 registers are not supported. Hence m2_n2 divider value
7840 * needs to be programmed into M1_N1.
7841 */
7842 dp_m_n = &crtc->config->dp_m2_n2;
7843 } else {
7844 DRM_ERROR("Unsupported divider value\n");
7845 return;
7846 }
7847
6e3c9717
ACO
7848 if (crtc->config->has_pch_encoder)
7849 intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
03afc4a2 7850 else
fe3cd48d 7851 intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
03afc4a2
SV
7852}
7853
251ac862
SV
7854static void vlv_compute_dpll(struct intel_crtc *crtc,
7855 struct intel_crtc_state *pipe_config)
bdd4b6a6 7856{
03ed5cbf 7857 pipe_config->dpll_hw_state.dpll = DPLL_INTEGRATED_REF_CLK_VLV |
cd2d34d9 7858 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
03ed5cbf
VS
7859 if (crtc->pipe != PIPE_A)
7860 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
bdd4b6a6 7861
cd2d34d9 7862 /* DPLL not used with DSI, but still need the rest set up */
d7edc4e5 7863 if (!intel_crtc_has_type(pipe_config, INTEL_OUTPUT_DSI))
cd2d34d9
VS
7864 pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE |
7865 DPLL_EXT_BUFFER_ENABLE_VLV;
7866
03ed5cbf
VS
7867 pipe_config->dpll_hw_state.dpll_md =
7868 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
7869}
bdd4b6a6 7870
03ed5cbf
VS
7871static void chv_compute_dpll(struct intel_crtc *crtc,
7872 struct intel_crtc_state *pipe_config)
7873{
7874 pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLK_CHV |
cd2d34d9 7875 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
03ed5cbf
VS
7876 if (crtc->pipe != PIPE_A)
7877 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
7878
cd2d34d9 7879 /* DPLL not used with DSI, but still need the rest set up */
d7edc4e5 7880 if (!intel_crtc_has_type(pipe_config, INTEL_OUTPUT_DSI))
cd2d34d9
VS
7881 pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE;
7882
03ed5cbf
VS
7883 pipe_config->dpll_hw_state.dpll_md =
7884 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
bdd4b6a6
SV
7885}
7886
d288f65f 7887static void vlv_prepare_pll(struct intel_crtc *crtc,
5cec258b 7888 const struct intel_crtc_state *pipe_config)
a0c4da24 7889{
f47709a9 7890 struct drm_device *dev = crtc->base.dev;
fac5e23e 7891 struct drm_i915_private *dev_priv = to_i915(dev);
cd2d34d9 7892 enum pipe pipe = crtc->pipe;
bdd4b6a6 7893 u32 mdiv;
a0c4da24 7894 u32 bestn, bestm1, bestm2, bestp1, bestp2;
bdd4b6a6 7895 u32 coreclk, reg_val;
a0c4da24 7896
cd2d34d9
VS
7897 /* Enable Refclk */
7898 I915_WRITE(DPLL(pipe),
7899 pipe_config->dpll_hw_state.dpll &
7900 ~(DPLL_VCO_ENABLE | DPLL_EXT_BUFFER_ENABLE_VLV));
7901
7902 /* No need to actually set up the DPLL with DSI */
7903 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
7904 return;
7905
a580516d 7906 mutex_lock(&dev_priv->sb_lock);
09153000 7907
d288f65f
VS
7908 bestn = pipe_config->dpll.n;
7909 bestm1 = pipe_config->dpll.m1;
7910 bestm2 = pipe_config->dpll.m2;
7911 bestp1 = pipe_config->dpll.p1;
7912 bestp2 = pipe_config->dpll.p2;
a0c4da24 7913
89b667f8
JB
7914 /* See eDP HDMI DPIO driver vbios notes doc */
7915
7916 /* PLL B needs special handling */
bdd4b6a6 7917 if (pipe == PIPE_B)
5e69f97f 7918 vlv_pllb_recal_opamp(dev_priv, pipe);
89b667f8
JB
7919
7920 /* Set up Tx target for periodic Rcomp update */
ab3c759a 7921 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
89b667f8
JB
7922
7923 /* Disable target IRef on PLL */
ab3c759a 7924 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
89b667f8 7925 reg_val &= 0x00ffffff;
ab3c759a 7926 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
89b667f8
JB
7927
7928 /* Disable fast lock */
ab3c759a 7929 vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
89b667f8
JB
7930
7931 /* Set idtafcrecal before PLL is enabled */
a0c4da24
JB
7932 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
7933 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
7934 mdiv |= ((bestn << DPIO_N_SHIFT));
a0c4da24 7935 mdiv |= (1 << DPIO_K_SHIFT);
7df5080b
JB
7936
7937 /*
7938 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
7939 * but we don't support that).
7940 * Note: don't use the DAC post divider as it seems unstable.
7941 */
7942 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
ab3c759a 7943 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
a0c4da24 7944
a0c4da24 7945 mdiv |= DPIO_ENABLE_CALIBRATION;
ab3c759a 7946 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
a0c4da24 7947
89b667f8 7948 /* Set HBR and RBR LPF coefficients */
d288f65f 7949 if (pipe_config->port_clock == 162000 ||
2d84d2b3
VS
7950 intel_crtc_has_type(crtc->config, INTEL_OUTPUT_ANALOG) ||
7951 intel_crtc_has_type(crtc->config, INTEL_OUTPUT_HDMI))
ab3c759a 7952 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
885b0120 7953 0x009f0003);
89b667f8 7954 else
ab3c759a 7955 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
89b667f8
JB
7956 0x00d0000f);
7957
37a5650b 7958 if (intel_crtc_has_dp_encoder(pipe_config)) {
89b667f8 7959 /* Use SSC source */
bdd4b6a6 7960 if (pipe == PIPE_A)
ab3c759a 7961 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
7962 0x0df40000);
7963 else
ab3c759a 7964 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
7965 0x0df70000);
7966 } else { /* HDMI or VGA */
7967 /* Use bend source */
bdd4b6a6 7968 if (pipe == PIPE_A)
ab3c759a 7969 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
7970 0x0df70000);
7971 else
ab3c759a 7972 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
7973 0x0df40000);
7974 }
a0c4da24 7975
ab3c759a 7976 coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
89b667f8 7977 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
2210ce7f 7978 if (intel_crtc_has_dp_encoder(crtc->config))
89b667f8 7979 coreclk |= 0x01000000;
ab3c759a 7980 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
a0c4da24 7981
ab3c759a 7982 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
a580516d 7983 mutex_unlock(&dev_priv->sb_lock);
a0c4da24
JB
7984}
7985
d288f65f 7986static void chv_prepare_pll(struct intel_crtc *crtc,
5cec258b 7987 const struct intel_crtc_state *pipe_config)
9d556c99
CML
7988{
7989 struct drm_device *dev = crtc->base.dev;
fac5e23e 7990 struct drm_i915_private *dev_priv = to_i915(dev);
cd2d34d9 7991 enum pipe pipe = crtc->pipe;
9d556c99 7992 enum dpio_channel port = vlv_pipe_to_channel(pipe);
9cbe40c1 7993 u32 loopfilter, tribuf_calcntr;
9d556c99 7994 u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
a945ce7e 7995 u32 dpio_val;
9cbe40c1 7996 int vco;
9d556c99 7997
cd2d34d9
VS
7998 /* Enable Refclk and SSC */
7999 I915_WRITE(DPLL(pipe),
8000 pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
8001
8002 /* No need to actually set up the DPLL with DSI */
8003 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
8004 return;
8005
d288f65f
VS
8006 bestn = pipe_config->dpll.n;
8007 bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
8008 bestm1 = pipe_config->dpll.m1;
8009 bestm2 = pipe_config->dpll.m2 >> 22;
8010 bestp1 = pipe_config->dpll.p1;
8011 bestp2 = pipe_config->dpll.p2;
9cbe40c1 8012 vco = pipe_config->dpll.vco;
a945ce7e 8013 dpio_val = 0;
9cbe40c1 8014 loopfilter = 0;
9d556c99 8015
a580516d 8016 mutex_lock(&dev_priv->sb_lock);
9d556c99 8017
9d556c99
CML
8018 /* p1 and p2 divider */
8019 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
8020 5 << DPIO_CHV_S1_DIV_SHIFT |
8021 bestp1 << DPIO_CHV_P1_DIV_SHIFT |
8022 bestp2 << DPIO_CHV_P2_DIV_SHIFT |
8023 1 << DPIO_CHV_K_DIV_SHIFT);
8024
8025 /* Feedback post-divider - m2 */
8026 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
8027
8028 /* Feedback refclk divider - n and m1 */
8029 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
8030 DPIO_CHV_M1_DIV_BY_2 |
8031 1 << DPIO_CHV_N_DIV_SHIFT);
8032
8033 /* M2 fraction division */
25a25dfc 8034 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
9d556c99
CML
8035
8036 /* M2 fraction division enable */
a945ce7e
VP
8037 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
8038 dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
8039 dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
8040 if (bestm2_frac)
8041 dpio_val |= DPIO_CHV_FRAC_DIV_EN;
8042 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
9d556c99 8043
de3a0fde
VP
8044 /* Program digital lock detect threshold */
8045 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
8046 dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
8047 DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
8048 dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
8049 if (!bestm2_frac)
8050 dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
8051 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
8052
9d556c99 8053 /* Loop filter */
9cbe40c1
VP
8054 if (vco == 5400000) {
8055 loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
8056 loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
8057 loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
8058 tribuf_calcntr = 0x9;
8059 } else if (vco <= 6200000) {
8060 loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
8061 loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
8062 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
8063 tribuf_calcntr = 0x9;
8064 } else if (vco <= 6480000) {
8065 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
8066 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
8067 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
8068 tribuf_calcntr = 0x8;
8069 } else {
8070 /* Not supported. Apply the same limits as in the max case */
8071 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
8072 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
8073 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
8074 tribuf_calcntr = 0;
8075 }
9d556c99
CML
8076 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
8077
968040b2 8078 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
9cbe40c1
VP
8079 dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
8080 dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
8081 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
8082
9d556c99
CML
8083 /* AFC Recal */
8084 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
8085 vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
8086 DPIO_AFC_RECAL);
8087
a580516d 8088 mutex_unlock(&dev_priv->sb_lock);
9d556c99
CML
8089}
8090
d288f65f
VS
8091/**
8092 * vlv_force_pll_on - forcibly enable just the PLL
8093 * @dev_priv: i915 private structure
8094 * @pipe: pipe PLL to enable
8095 * @dpll: PLL configuration
8096 *
8097 * Enable the PLL for @pipe using the supplied @dpll config. To be used
8098 * in cases where we need the PLL enabled even when @pipe is not going to
8099 * be enabled.
8100 */
30ad9814 8101int vlv_force_pll_on(struct drm_i915_private *dev_priv, enum pipe pipe,
3f36b937 8102 const struct dpll *dpll)
d288f65f 8103{
b91eb5cc 8104 struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
3f36b937
TU
8105 struct intel_crtc_state *pipe_config;
8106
8107 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
8108 if (!pipe_config)
8109 return -ENOMEM;
8110
8111 pipe_config->base.crtc = &crtc->base;
8112 pipe_config->pixel_multiplier = 1;
8113 pipe_config->dpll = *dpll;
d288f65f 8114
30ad9814 8115 if (IS_CHERRYVIEW(dev_priv)) {
3f36b937
TU
8116 chv_compute_dpll(crtc, pipe_config);
8117 chv_prepare_pll(crtc, pipe_config);
8118 chv_enable_pll(crtc, pipe_config);
d288f65f 8119 } else {
3f36b937
TU
8120 vlv_compute_dpll(crtc, pipe_config);
8121 vlv_prepare_pll(crtc, pipe_config);
8122 vlv_enable_pll(crtc, pipe_config);
d288f65f 8123 }
3f36b937
TU
8124
8125 kfree(pipe_config);
8126
8127 return 0;
d288f65f
VS
8128}
8129
8130/**
8131 * vlv_force_pll_off - forcibly disable just the PLL
8132 * @dev_priv: i915 private structure
8133 * @pipe: pipe PLL to disable
8134 *
8135 * Disable the PLL for @pipe. To be used in cases where we need
8136 * the PLL enabled even when @pipe is not going to be enabled.
8137 */
30ad9814 8138void vlv_force_pll_off(struct drm_i915_private *dev_priv, enum pipe pipe)
d288f65f 8139{
30ad9814
VS
8140 if (IS_CHERRYVIEW(dev_priv))
8141 chv_disable_pll(dev_priv, pipe);
d288f65f 8142 else
30ad9814 8143 vlv_disable_pll(dev_priv, pipe);
d288f65f
VS
8144}
8145
251ac862
SV
8146static void i9xx_compute_dpll(struct intel_crtc *crtc,
8147 struct intel_crtc_state *crtc_state,
9e2c8475 8148 struct dpll *reduced_clock)
eb1cbe48 8149{
9b1e14f4 8150 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
eb1cbe48 8151 u32 dpll;
190f68c5 8152 struct dpll *clock = &crtc_state->dpll;
eb1cbe48 8153
190f68c5 8154 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
2a8f64ca 8155
eb1cbe48
SV
8156 dpll = DPLL_VGA_MODE_DIS;
8157
2d84d2b3 8158 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS))
eb1cbe48
SV
8159 dpll |= DPLLB_MODE_LVDS;
8160 else
8161 dpll |= DPLLB_MODE_DAC_SERIAL;
6cc5f341 8162
50a0bc90 8163 if (IS_I945G(dev_priv) || IS_I945GM(dev_priv) || IS_G33(dev_priv)) {
190f68c5 8164 dpll |= (crtc_state->pixel_multiplier - 1)
198a037f 8165 << SDVO_MULTIPLIER_SHIFT_HIRES;
eb1cbe48 8166 }
198a037f 8167
3d6e9ee0
VS
8168 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO) ||
8169 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
4a33e48d 8170 dpll |= DPLL_SDVO_HIGH_SPEED;
198a037f 8171
37a5650b 8172 if (intel_crtc_has_dp_encoder(crtc_state))
4a33e48d 8173 dpll |= DPLL_SDVO_HIGH_SPEED;
eb1cbe48
SV
8174
8175 /* compute bitmask from p1 value */
9b1e14f4 8176 if (IS_PINEVIEW(dev_priv))
eb1cbe48
SV
8177 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
8178 else {
8179 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
9beb5fea 8180 if (IS_G4X(dev_priv) && reduced_clock)
eb1cbe48
SV
8181 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
8182 }
8183 switch (clock->p2) {
8184 case 5:
8185 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
8186 break;
8187 case 7:
8188 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
8189 break;
8190 case 10:
8191 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
8192 break;
8193 case 14:
8194 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
8195 break;
8196 }
9b1e14f4 8197 if (INTEL_GEN(dev_priv) >= 4)
eb1cbe48
SV
8198 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
8199
190f68c5 8200 if (crtc_state->sdvo_tv_clock)
eb1cbe48 8201 dpll |= PLL_REF_INPUT_TVCLKINBC;
2d84d2b3 8202 else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
ceb41007 8203 intel_panel_use_ssc(dev_priv))
eb1cbe48
SV
8204 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
8205 else
8206 dpll |= PLL_REF_INPUT_DREFCLK;
8207
8208 dpll |= DPLL_VCO_ENABLE;
190f68c5 8209 crtc_state->dpll_hw_state.dpll = dpll;
8bcc2795 8210
9b1e14f4 8211 if (INTEL_GEN(dev_priv) >= 4) {
190f68c5 8212 u32 dpll_md = (crtc_state->pixel_multiplier - 1)
ef1b460d 8213 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
190f68c5 8214 crtc_state->dpll_hw_state.dpll_md = dpll_md;
eb1cbe48
SV
8215 }
8216}
8217
251ac862
SV
8218static void i8xx_compute_dpll(struct intel_crtc *crtc,
8219 struct intel_crtc_state *crtc_state,
9e2c8475 8220 struct dpll *reduced_clock)
eb1cbe48 8221{
f47709a9 8222 struct drm_device *dev = crtc->base.dev;
fac5e23e 8223 struct drm_i915_private *dev_priv = to_i915(dev);
eb1cbe48 8224 u32 dpll;
190f68c5 8225 struct dpll *clock = &crtc_state->dpll;
eb1cbe48 8226
190f68c5 8227 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
2a8f64ca 8228
eb1cbe48
SV
8229 dpll = DPLL_VGA_MODE_DIS;
8230
2d84d2b3 8231 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
eb1cbe48
SV
8232 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
8233 } else {
8234 if (clock->p1 == 2)
8235 dpll |= PLL_P1_DIVIDE_BY_TWO;
8236 else
8237 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
8238 if (clock->p2 == 4)
8239 dpll |= PLL_P2_DIVIDE_BY_4;
8240 }
8241
50a0bc90
TU
8242 if (!IS_I830(dev_priv) &&
8243 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DVO))
4a33e48d
SV
8244 dpll |= DPLL_DVO_2X_MODE;
8245
2d84d2b3 8246 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
ceb41007 8247 intel_panel_use_ssc(dev_priv))
eb1cbe48
SV
8248 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
8249 else
8250 dpll |= PLL_REF_INPUT_DREFCLK;
8251
8252 dpll |= DPLL_VCO_ENABLE;
190f68c5 8253 crtc_state->dpll_hw_state.dpll = dpll;
eb1cbe48
SV
8254}
8255
8a654f3b 8256static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
b0e77b9c
PZ
8257{
8258 struct drm_device *dev = intel_crtc->base.dev;
fac5e23e 8259 struct drm_i915_private *dev_priv = to_i915(dev);
b0e77b9c 8260 enum pipe pipe = intel_crtc->pipe;
6e3c9717 8261 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
7c5f93b0 8262 const struct drm_display_mode *adjusted_mode = &intel_crtc->config->base.adjusted_mode;
1caea6e9
VS
8263 uint32_t crtc_vtotal, crtc_vblank_end;
8264 int vsyncshift = 0;
4d8a62ea
SV
8265
8266 /* We need to be careful not to changed the adjusted mode, for otherwise
8267 * the hw state checker will get angry at the mismatch. */
8268 crtc_vtotal = adjusted_mode->crtc_vtotal;
8269 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
b0e77b9c 8270
609aeaca 8271 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
b0e77b9c 8272 /* the chip adds 2 halflines automatically */
4d8a62ea
SV
8273 crtc_vtotal -= 1;
8274 crtc_vblank_end -= 1;
609aeaca 8275
2d84d2b3 8276 if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
609aeaca
VS
8277 vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
8278 else
8279 vsyncshift = adjusted_mode->crtc_hsync_start -
8280 adjusted_mode->crtc_htotal / 2;
1caea6e9
VS
8281 if (vsyncshift < 0)
8282 vsyncshift += adjusted_mode->crtc_htotal;
b0e77b9c
PZ
8283 }
8284
8285 if (INTEL_INFO(dev)->gen > 3)
fe2b8f9d 8286 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
b0e77b9c 8287
fe2b8f9d 8288 I915_WRITE(HTOTAL(cpu_transcoder),
b0e77b9c
PZ
8289 (adjusted_mode->crtc_hdisplay - 1) |
8290 ((adjusted_mode->crtc_htotal - 1) << 16));
fe2b8f9d 8291 I915_WRITE(HBLANK(cpu_transcoder),
b0e77b9c
PZ
8292 (adjusted_mode->crtc_hblank_start - 1) |
8293 ((adjusted_mode->crtc_hblank_end - 1) << 16));
fe2b8f9d 8294 I915_WRITE(HSYNC(cpu_transcoder),
b0e77b9c
PZ
8295 (adjusted_mode->crtc_hsync_start - 1) |
8296 ((adjusted_mode->crtc_hsync_end - 1) << 16));
8297
fe2b8f9d 8298 I915_WRITE(VTOTAL(cpu_transcoder),
b0e77b9c 8299 (adjusted_mode->crtc_vdisplay - 1) |
4d8a62ea 8300 ((crtc_vtotal - 1) << 16));
fe2b8f9d 8301 I915_WRITE(VBLANK(cpu_transcoder),
b0e77b9c 8302 (adjusted_mode->crtc_vblank_start - 1) |
4d8a62ea 8303 ((crtc_vblank_end - 1) << 16));
fe2b8f9d 8304 I915_WRITE(VSYNC(cpu_transcoder),
b0e77b9c
PZ
8305 (adjusted_mode->crtc_vsync_start - 1) |
8306 ((adjusted_mode->crtc_vsync_end - 1) << 16));
8307
b5e508d4
PZ
8308 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
8309 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
8310 * documented on the DDI_FUNC_CTL register description, EDP Input Select
8311 * bits. */
772c2a51 8312 if (IS_HASWELL(dev_priv) && cpu_transcoder == TRANSCODER_EDP &&
b5e508d4
PZ
8313 (pipe == PIPE_B || pipe == PIPE_C))
8314 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
8315
bc58be60
JN
8316}
8317
8318static void intel_set_pipe_src_size(struct intel_crtc *intel_crtc)
8319{
8320 struct drm_device *dev = intel_crtc->base.dev;
fac5e23e 8321 struct drm_i915_private *dev_priv = to_i915(dev);
bc58be60
JN
8322 enum pipe pipe = intel_crtc->pipe;
8323
b0e77b9c
PZ
8324 /* pipesrc controls the size that is scaled from, which should
8325 * always be the user's requested size.
8326 */
8327 I915_WRITE(PIPESRC(pipe),
6e3c9717
ACO
8328 ((intel_crtc->config->pipe_src_w - 1) << 16) |
8329 (intel_crtc->config->pipe_src_h - 1));
b0e77b9c
PZ
8330}
8331
1bd1bd80 8332static void intel_get_pipe_timings(struct intel_crtc *crtc,
5cec258b 8333 struct intel_crtc_state *pipe_config)
1bd1bd80
SV
8334{
8335 struct drm_device *dev = crtc->base.dev;
fac5e23e 8336 struct drm_i915_private *dev_priv = to_i915(dev);
1bd1bd80
SV
8337 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
8338 uint32_t tmp;
8339
8340 tmp = I915_READ(HTOTAL(cpu_transcoder));
2d112de7
ACO
8341 pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
8342 pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 8343 tmp = I915_READ(HBLANK(cpu_transcoder));
2d112de7
ACO
8344 pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
8345 pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 8346 tmp = I915_READ(HSYNC(cpu_transcoder));
2d112de7
ACO
8347 pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
8348 pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80
SV
8349
8350 tmp = I915_READ(VTOTAL(cpu_transcoder));
2d112de7
ACO
8351 pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
8352 pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 8353 tmp = I915_READ(VBLANK(cpu_transcoder));
2d112de7
ACO
8354 pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
8355 pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 8356 tmp = I915_READ(VSYNC(cpu_transcoder));
2d112de7
ACO
8357 pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
8358 pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80
SV
8359
8360 if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
2d112de7
ACO
8361 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
8362 pipe_config->base.adjusted_mode.crtc_vtotal += 1;
8363 pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
1bd1bd80 8364 }
bc58be60
JN
8365}
8366
8367static void intel_get_pipe_src_size(struct intel_crtc *crtc,
8368 struct intel_crtc_state *pipe_config)
8369{
8370 struct drm_device *dev = crtc->base.dev;
fac5e23e 8371 struct drm_i915_private *dev_priv = to_i915(dev);
bc58be60 8372 u32 tmp;
1bd1bd80
SV
8373
8374 tmp = I915_READ(PIPESRC(crtc->pipe));
37327abd
VS
8375 pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
8376 pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
8377
2d112de7
ACO
8378 pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
8379 pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
1bd1bd80
SV
8380}
8381
f6a83288 8382void intel_mode_from_pipe_config(struct drm_display_mode *mode,
5cec258b 8383 struct intel_crtc_state *pipe_config)
babea61d 8384{
2d112de7
ACO
8385 mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
8386 mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
8387 mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
8388 mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
babea61d 8389
2d112de7
ACO
8390 mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
8391 mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
8392 mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
8393 mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
babea61d 8394
2d112de7 8395 mode->flags = pipe_config->base.adjusted_mode.flags;
cd13f5ab 8396 mode->type = DRM_MODE_TYPE_DRIVER;
babea61d 8397
2d112de7
ACO
8398 mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
8399 mode->flags |= pipe_config->base.adjusted_mode.flags;
cd13f5ab
ML
8400
8401 mode->hsync = drm_mode_hsync(mode);
8402 mode->vrefresh = drm_mode_vrefresh(mode);
8403 drm_mode_set_name(mode);
babea61d
JB
8404}
8405
84b046f3
SV
8406static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
8407{
8408 struct drm_device *dev = intel_crtc->base.dev;
fac5e23e 8409 struct drm_i915_private *dev_priv = to_i915(dev);
84b046f3
SV
8410 uint32_t pipeconf;
8411
9f11a9e4 8412 pipeconf = 0;
84b046f3 8413
b6b5d049
VS
8414 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
8415 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
8416 pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
67c72a12 8417
6e3c9717 8418 if (intel_crtc->config->double_wide)
cf532bb2 8419 pipeconf |= PIPECONF_DOUBLE_WIDE;
84b046f3 8420
ff9ce46e 8421 /* only g4x and later have fancy bpc/dither controls */
9beb5fea
TU
8422 if (IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
8423 IS_CHERRYVIEW(dev_priv)) {
ff9ce46e 8424 /* Bspec claims that we can't use dithering for 30bpp pipes. */
6e3c9717 8425 if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
ff9ce46e 8426 pipeconf |= PIPECONF_DITHER_EN |
84b046f3 8427 PIPECONF_DITHER_TYPE_SP;
84b046f3 8428
6e3c9717 8429 switch (intel_crtc->config->pipe_bpp) {
ff9ce46e
SV
8430 case 18:
8431 pipeconf |= PIPECONF_6BPC;
8432 break;
8433 case 24:
8434 pipeconf |= PIPECONF_8BPC;
8435 break;
8436 case 30:
8437 pipeconf |= PIPECONF_10BPC;
8438 break;
8439 default:
8440 /* Case prevented by intel_choose_pipe_bpp_dither. */
8441 BUG();
84b046f3
SV
8442 }
8443 }
8444
56b857a5 8445 if (HAS_PIPE_CXSR(dev_priv)) {
84b046f3
SV
8446 if (intel_crtc->lowfreq_avail) {
8447 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
8448 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
8449 } else {
8450 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
84b046f3
SV
8451 }
8452 }
8453
6e3c9717 8454 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
efc2cfff 8455 if (INTEL_INFO(dev)->gen < 4 ||
2d84d2b3 8456 intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
efc2cfff
VS
8457 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
8458 else
8459 pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
8460 } else
84b046f3
SV
8461 pipeconf |= PIPECONF_PROGRESSIVE;
8462
920a14b2 8463 if ((IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
666a4537 8464 intel_crtc->config->limited_color_range)
9f11a9e4 8465 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
9c8e09b7 8466
84b046f3
SV
8467 I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
8468 POSTING_READ(PIPECONF(intel_crtc->pipe));
8469}
8470
81c97f52
ACO
8471static int i8xx_crtc_compute_clock(struct intel_crtc *crtc,
8472 struct intel_crtc_state *crtc_state)
8473{
8474 struct drm_device *dev = crtc->base.dev;
fac5e23e 8475 struct drm_i915_private *dev_priv = to_i915(dev);
1b6f4958 8476 const struct intel_limit *limit;
81c97f52
ACO
8477 int refclk = 48000;
8478
8479 memset(&crtc_state->dpll_hw_state, 0,
8480 sizeof(crtc_state->dpll_hw_state));
8481
2d84d2b3 8482 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
81c97f52
ACO
8483 if (intel_panel_use_ssc(dev_priv)) {
8484 refclk = dev_priv->vbt.lvds_ssc_freq;
8485 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
8486 }
8487
8488 limit = &intel_limits_i8xx_lvds;
2d84d2b3 8489 } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DVO)) {
81c97f52
ACO
8490 limit = &intel_limits_i8xx_dvo;
8491 } else {
8492 limit = &intel_limits_i8xx_dac;
8493 }
8494
8495 if (!crtc_state->clock_set &&
8496 !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8497 refclk, NULL, &crtc_state->dpll)) {
8498 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8499 return -EINVAL;
8500 }
8501
8502 i8xx_compute_dpll(crtc, crtc_state, NULL);
8503
8504 return 0;
8505}
8506
19ec6693
ACO
8507static int g4x_crtc_compute_clock(struct intel_crtc *crtc,
8508 struct intel_crtc_state *crtc_state)
8509{
8510 struct drm_device *dev = crtc->base.dev;
fac5e23e 8511 struct drm_i915_private *dev_priv = to_i915(dev);
1b6f4958 8512 const struct intel_limit *limit;
19ec6693
ACO
8513 int refclk = 96000;
8514
8515 memset(&crtc_state->dpll_hw_state, 0,
8516 sizeof(crtc_state->dpll_hw_state));
8517
2d84d2b3 8518 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
19ec6693
ACO
8519 if (intel_panel_use_ssc(dev_priv)) {
8520 refclk = dev_priv->vbt.lvds_ssc_freq;
8521 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
8522 }
8523
8524 if (intel_is_dual_link_lvds(dev))
8525 limit = &intel_limits_g4x_dual_channel_lvds;
8526 else
8527 limit = &intel_limits_g4x_single_channel_lvds;
2d84d2b3
VS
8528 } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI) ||
8529 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
19ec6693 8530 limit = &intel_limits_g4x_hdmi;
2d84d2b3 8531 } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO)) {
19ec6693
ACO
8532 limit = &intel_limits_g4x_sdvo;
8533 } else {
8534 /* The option is for other outputs */
8535 limit = &intel_limits_i9xx_sdvo;
8536 }
8537
8538 if (!crtc_state->clock_set &&
8539 !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8540 refclk, NULL, &crtc_state->dpll)) {
8541 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8542 return -EINVAL;
8543 }
8544
8545 i9xx_compute_dpll(crtc, crtc_state, NULL);
8546
8547 return 0;
8548}
8549
70e8aa21
ACO
8550static int pnv_crtc_compute_clock(struct intel_crtc *crtc,
8551 struct intel_crtc_state *crtc_state)
8552{
8553 struct drm_device *dev = crtc->base.dev;
fac5e23e 8554 struct drm_i915_private *dev_priv = to_i915(dev);
1b6f4958 8555 const struct intel_limit *limit;
70e8aa21
ACO
8556 int refclk = 96000;
8557
8558 memset(&crtc_state->dpll_hw_state, 0,
8559 sizeof(crtc_state->dpll_hw_state));
8560
2d84d2b3 8561 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
70e8aa21
ACO
8562 if (intel_panel_use_ssc(dev_priv)) {
8563 refclk = dev_priv->vbt.lvds_ssc_freq;
8564 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
8565 }
8566
8567 limit = &intel_limits_pineview_lvds;
8568 } else {
8569 limit = &intel_limits_pineview_sdvo;
8570 }
8571
8572 if (!crtc_state->clock_set &&
8573 !pnv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8574 refclk, NULL, &crtc_state->dpll)) {
8575 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8576 return -EINVAL;
8577 }
8578
8579 i9xx_compute_dpll(crtc, crtc_state, NULL);
8580
8581 return 0;
8582}
8583
190f68c5
ACO
8584static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
8585 struct intel_crtc_state *crtc_state)
79e53945 8586{
c7653199 8587 struct drm_device *dev = crtc->base.dev;
fac5e23e 8588 struct drm_i915_private *dev_priv = to_i915(dev);
1b6f4958 8589 const struct intel_limit *limit;
81c97f52 8590 int refclk = 96000;
79e53945 8591
dd3cd74a
ACO
8592 memset(&crtc_state->dpll_hw_state, 0,
8593 sizeof(crtc_state->dpll_hw_state));
8594
2d84d2b3 8595 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
70e8aa21
ACO
8596 if (intel_panel_use_ssc(dev_priv)) {
8597 refclk = dev_priv->vbt.lvds_ssc_freq;
8598 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
8599 }
43565a06 8600
70e8aa21
ACO
8601 limit = &intel_limits_i9xx_lvds;
8602 } else {
8603 limit = &intel_limits_i9xx_sdvo;
81c97f52 8604 }
79e53945 8605
70e8aa21
ACO
8606 if (!crtc_state->clock_set &&
8607 !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8608 refclk, NULL, &crtc_state->dpll)) {
8609 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8610 return -EINVAL;
f47709a9 8611 }
7026d4ac 8612
81c97f52 8613 i9xx_compute_dpll(crtc, crtc_state, NULL);
79e53945 8614
c8f7a0db 8615 return 0;
f564048e
EA
8616}
8617
65b3d6a9
ACO
8618static int chv_crtc_compute_clock(struct intel_crtc *crtc,
8619 struct intel_crtc_state *crtc_state)
8620{
8621 int refclk = 100000;
1b6f4958 8622 const struct intel_limit *limit = &intel_limits_chv;
65b3d6a9
ACO
8623
8624 memset(&crtc_state->dpll_hw_state, 0,
8625 sizeof(crtc_state->dpll_hw_state));
8626
65b3d6a9
ACO
8627 if (!crtc_state->clock_set &&
8628 !chv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8629 refclk, NULL, &crtc_state->dpll)) {
8630 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8631 return -EINVAL;
8632 }
8633
8634 chv_compute_dpll(crtc, crtc_state);
8635
8636 return 0;
8637}
8638
8639static int vlv_crtc_compute_clock(struct intel_crtc *crtc,
8640 struct intel_crtc_state *crtc_state)
8641{
8642 int refclk = 100000;
1b6f4958 8643 const struct intel_limit *limit = &intel_limits_vlv;
65b3d6a9
ACO
8644
8645 memset(&crtc_state->dpll_hw_state, 0,
8646 sizeof(crtc_state->dpll_hw_state));
8647
65b3d6a9
ACO
8648 if (!crtc_state->clock_set &&
8649 !vlv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8650 refclk, NULL, &crtc_state->dpll)) {
8651 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8652 return -EINVAL;
8653 }
8654
8655 vlv_compute_dpll(crtc, crtc_state);
8656
8657 return 0;
8658}
8659
2fa2fe9a 8660static void i9xx_get_pfit_config(struct intel_crtc *crtc,
5cec258b 8661 struct intel_crtc_state *pipe_config)
2fa2fe9a
SV
8662{
8663 struct drm_device *dev = crtc->base.dev;
fac5e23e 8664 struct drm_i915_private *dev_priv = to_i915(dev);
2fa2fe9a
SV
8665 uint32_t tmp;
8666
50a0bc90
TU
8667 if (INTEL_GEN(dev_priv) <= 3 &&
8668 (IS_I830(dev_priv) || !IS_MOBILE(dev_priv)))
dc9e7dec
VS
8669 return;
8670
2fa2fe9a 8671 tmp = I915_READ(PFIT_CONTROL);
06922821
SV
8672 if (!(tmp & PFIT_ENABLE))
8673 return;
2fa2fe9a 8674
06922821 8675 /* Check whether the pfit is attached to our pipe. */
2fa2fe9a
SV
8676 if (INTEL_INFO(dev)->gen < 4) {
8677 if (crtc->pipe != PIPE_B)
8678 return;
2fa2fe9a
SV
8679 } else {
8680 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
8681 return;
8682 }
8683
06922821 8684 pipe_config->gmch_pfit.control = tmp;
2fa2fe9a 8685 pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
2fa2fe9a
SV
8686}
8687
acbec814 8688static void vlv_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 8689 struct intel_crtc_state *pipe_config)
acbec814
JB
8690{
8691 struct drm_device *dev = crtc->base.dev;
fac5e23e 8692 struct drm_i915_private *dev_priv = to_i915(dev);
acbec814 8693 int pipe = pipe_config->cpu_transcoder;
9e2c8475 8694 struct dpll clock;
acbec814 8695 u32 mdiv;
662c6ecb 8696 int refclk = 100000;
acbec814 8697
b521973b
VS
8698 /* In case of DSI, DPLL will not be used */
8699 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
f573de5a
SK
8700 return;
8701
a580516d 8702 mutex_lock(&dev_priv->sb_lock);
ab3c759a 8703 mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
a580516d 8704 mutex_unlock(&dev_priv->sb_lock);
acbec814
JB
8705
8706 clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
8707 clock.m2 = mdiv & DPIO_M2DIV_MASK;
8708 clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
8709 clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
8710 clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
8711
dccbea3b 8712 pipe_config->port_clock = vlv_calc_dpll_params(refclk, &clock);
acbec814
JB
8713}
8714
5724dbd1
DL
8715static void
8716i9xx_get_initial_plane_config(struct intel_crtc *crtc,
8717 struct intel_initial_plane_config *plane_config)
1ad292b5
JB
8718{
8719 struct drm_device *dev = crtc->base.dev;
fac5e23e 8720 struct drm_i915_private *dev_priv = to_i915(dev);
1ad292b5
JB
8721 u32 val, base, offset;
8722 int pipe = crtc->pipe, plane = crtc->plane;
8723 int fourcc, pixel_format;
6761dd31 8724 unsigned int aligned_height;
b113d5ee 8725 struct drm_framebuffer *fb;
1b842c89 8726 struct intel_framebuffer *intel_fb;
1ad292b5 8727
42a7b088
DL
8728 val = I915_READ(DSPCNTR(plane));
8729 if (!(val & DISPLAY_PLANE_ENABLE))
8730 return;
8731
d9806c9f 8732 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
1b842c89 8733 if (!intel_fb) {
1ad292b5
JB
8734 DRM_DEBUG_KMS("failed to alloc fb\n");
8735 return;
8736 }
8737
1b842c89
DL
8738 fb = &intel_fb->base;
8739
18c5247e
SV
8740 if (INTEL_INFO(dev)->gen >= 4) {
8741 if (val & DISPPLANE_TILED) {
49af449b 8742 plane_config->tiling = I915_TILING_X;
18c5247e
SV
8743 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
8744 }
8745 }
1ad292b5
JB
8746
8747 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
b35d63fa 8748 fourcc = i9xx_format_to_fourcc(pixel_format);
b113d5ee
DL
8749 fb->pixel_format = fourcc;
8750 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
1ad292b5
JB
8751
8752 if (INTEL_INFO(dev)->gen >= 4) {
49af449b 8753 if (plane_config->tiling)
1ad292b5
JB
8754 offset = I915_READ(DSPTILEOFF(plane));
8755 else
8756 offset = I915_READ(DSPLINOFF(plane));
8757 base = I915_READ(DSPSURF(plane)) & 0xfffff000;
8758 } else {
8759 base = I915_READ(DSPADDR(plane));
8760 }
8761 plane_config->base = base;
8762
8763 val = I915_READ(PIPESRC(pipe));
b113d5ee
DL
8764 fb->width = ((val >> 16) & 0xfff) + 1;
8765 fb->height = ((val >> 0) & 0xfff) + 1;
1ad292b5
JB
8766
8767 val = I915_READ(DSPSTRIDE(pipe));
b113d5ee 8768 fb->pitches[0] = val & 0xffffffc0;
1ad292b5 8769
b113d5ee 8770 aligned_height = intel_fb_align_height(dev, fb->height,
091df6cb
SV
8771 fb->pixel_format,
8772 fb->modifier[0]);
1ad292b5 8773
f37b5c2b 8774 plane_config->size = fb->pitches[0] * aligned_height;
1ad292b5 8775
2844a921
DL
8776 DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
8777 pipe_name(pipe), plane, fb->width, fb->height,
8778 fb->bits_per_pixel, base, fb->pitches[0],
8779 plane_config->size);
1ad292b5 8780
2d14030b 8781 plane_config->fb = intel_fb;
1ad292b5
JB
8782}
8783
70b23a98 8784static void chv_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 8785 struct intel_crtc_state *pipe_config)
70b23a98
VS
8786{
8787 struct drm_device *dev = crtc->base.dev;
fac5e23e 8788 struct drm_i915_private *dev_priv = to_i915(dev);
70b23a98
VS
8789 int pipe = pipe_config->cpu_transcoder;
8790 enum dpio_channel port = vlv_pipe_to_channel(pipe);
9e2c8475 8791 struct dpll clock;
0d7b6b11 8792 u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2, pll_dw3;
70b23a98
VS
8793 int refclk = 100000;
8794
b521973b
VS
8795 /* In case of DSI, DPLL will not be used */
8796 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
8797 return;
8798
a580516d 8799 mutex_lock(&dev_priv->sb_lock);
70b23a98
VS
8800 cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
8801 pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
8802 pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
8803 pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
0d7b6b11 8804 pll_dw3 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
a580516d 8805 mutex_unlock(&dev_priv->sb_lock);
70b23a98
VS
8806
8807 clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
0d7b6b11
ID
8808 clock.m2 = (pll_dw0 & 0xff) << 22;
8809 if (pll_dw3 & DPIO_CHV_FRAC_DIV_EN)
8810 clock.m2 |= pll_dw2 & 0x3fffff;
70b23a98
VS
8811 clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
8812 clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
8813 clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
8814
dccbea3b 8815 pipe_config->port_clock = chv_calc_dpll_params(refclk, &clock);
70b23a98
VS
8816}
8817
0e8ffe1b 8818static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
5cec258b 8819 struct intel_crtc_state *pipe_config)
0e8ffe1b
SV
8820{
8821 struct drm_device *dev = crtc->base.dev;
fac5e23e 8822 struct drm_i915_private *dev_priv = to_i915(dev);
1729050e 8823 enum intel_display_power_domain power_domain;
0e8ffe1b 8824 uint32_t tmp;
1729050e 8825 bool ret;
0e8ffe1b 8826
1729050e
ID
8827 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
8828 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
b5482bd0
ID
8829 return false;
8830
e143a21c 8831 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
8106ddbd 8832 pipe_config->shared_dpll = NULL;
eccb140b 8833
1729050e
ID
8834 ret = false;
8835
0e8ffe1b
SV
8836 tmp = I915_READ(PIPECONF(crtc->pipe));
8837 if (!(tmp & PIPECONF_ENABLE))
1729050e 8838 goto out;
0e8ffe1b 8839
9beb5fea
TU
8840 if (IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
8841 IS_CHERRYVIEW(dev_priv)) {
42571aef
VS
8842 switch (tmp & PIPECONF_BPC_MASK) {
8843 case PIPECONF_6BPC:
8844 pipe_config->pipe_bpp = 18;
8845 break;
8846 case PIPECONF_8BPC:
8847 pipe_config->pipe_bpp = 24;
8848 break;
8849 case PIPECONF_10BPC:
8850 pipe_config->pipe_bpp = 30;
8851 break;
8852 default:
8853 break;
8854 }
8855 }
8856
920a14b2 8857 if ((IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
666a4537 8858 (tmp & PIPECONF_COLOR_RANGE_SELECT))
b5a9fa09
SV
8859 pipe_config->limited_color_range = true;
8860
282740f7
VS
8861 if (INTEL_INFO(dev)->gen < 4)
8862 pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
8863
1bd1bd80 8864 intel_get_pipe_timings(crtc, pipe_config);
bc58be60 8865 intel_get_pipe_src_size(crtc, pipe_config);
1bd1bd80 8866
2fa2fe9a
SV
8867 i9xx_get_pfit_config(crtc, pipe_config);
8868
6c49f241 8869 if (INTEL_INFO(dev)->gen >= 4) {
c231775c 8870 /* No way to read it out on pipes B and C */
920a14b2 8871 if (IS_CHERRYVIEW(dev_priv) && crtc->pipe != PIPE_A)
c231775c
VS
8872 tmp = dev_priv->chv_dpll_md[crtc->pipe];
8873 else
8874 tmp = I915_READ(DPLL_MD(crtc->pipe));
6c49f241
SV
8875 pipe_config->pixel_multiplier =
8876 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
8877 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
8bcc2795 8878 pipe_config->dpll_hw_state.dpll_md = tmp;
50a0bc90
TU
8879 } else if (IS_I945G(dev_priv) || IS_I945GM(dev_priv) ||
8880 IS_G33(dev_priv)) {
6c49f241
SV
8881 tmp = I915_READ(DPLL(crtc->pipe));
8882 pipe_config->pixel_multiplier =
8883 ((tmp & SDVO_MULTIPLIER_MASK)
8884 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
8885 } else {
8886 /* Note that on i915G/GM the pixel multiplier is in the sdvo
8887 * port and will be fixed up in the encoder->get_config
8888 * function. */
8889 pipe_config->pixel_multiplier = 1;
8890 }
8bcc2795 8891 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
920a14b2 8892 if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv)) {
1c4e0274
VS
8893 /*
8894 * DPLL_DVO_2X_MODE must be enabled for both DPLLs
8895 * on 830. Filter it out here so that we don't
8896 * report errors due to that.
8897 */
50a0bc90 8898 if (IS_I830(dev_priv))
1c4e0274
VS
8899 pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
8900
8bcc2795
SV
8901 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
8902 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
165e901c
VS
8903 } else {
8904 /* Mask out read-only status bits. */
8905 pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
8906 DPLL_PORTC_READY_MASK |
8907 DPLL_PORTB_READY_MASK);
8bcc2795 8908 }
6c49f241 8909
920a14b2 8910 if (IS_CHERRYVIEW(dev_priv))
70b23a98 8911 chv_crtc_clock_get(crtc, pipe_config);
11a914c2 8912 else if (IS_VALLEYVIEW(dev_priv))
acbec814
JB
8913 vlv_crtc_clock_get(crtc, pipe_config);
8914 else
8915 i9xx_crtc_clock_get(crtc, pipe_config);
18442d08 8916
0f64614d
VS
8917 /*
8918 * Normally the dotclock is filled in by the encoder .get_config()
8919 * but in case the pipe is enabled w/o any ports we need a sane
8920 * default.
8921 */
8922 pipe_config->base.adjusted_mode.crtc_clock =
8923 pipe_config->port_clock / pipe_config->pixel_multiplier;
8924
1729050e
ID
8925 ret = true;
8926
8927out:
8928 intel_display_power_put(dev_priv, power_domain);
8929
8930 return ret;
0e8ffe1b
SV
8931}
8932
dde86e2d 8933static void ironlake_init_pch_refclk(struct drm_device *dev)
13d83a67 8934{
fac5e23e 8935 struct drm_i915_private *dev_priv = to_i915(dev);
13d83a67 8936 struct intel_encoder *encoder;
1c1a24d2 8937 int i;
74cfd7ac 8938 u32 val, final;
13d83a67 8939 bool has_lvds = false;
199e5d79 8940 bool has_cpu_edp = false;
199e5d79 8941 bool has_panel = false;
99eb6a01
KP
8942 bool has_ck505 = false;
8943 bool can_ssc = false;
1c1a24d2 8944 bool using_ssc_source = false;
13d83a67
JB
8945
8946 /* We need to take the global config into account */
b2784e15 8947 for_each_intel_encoder(dev, encoder) {
199e5d79
KP
8948 switch (encoder->type) {
8949 case INTEL_OUTPUT_LVDS:
8950 has_panel = true;
8951 has_lvds = true;
8952 break;
8953 case INTEL_OUTPUT_EDP:
8954 has_panel = true;
2de6905f 8955 if (enc_to_dig_port(&encoder->base)->port == PORT_A)
199e5d79
KP
8956 has_cpu_edp = true;
8957 break;
6847d71b
PZ
8958 default:
8959 break;
13d83a67
JB
8960 }
8961 }
8962
6e266956 8963 if (HAS_PCH_IBX(dev_priv)) {
41aa3448 8964 has_ck505 = dev_priv->vbt.display_clock_mode;
99eb6a01
KP
8965 can_ssc = has_ck505;
8966 } else {
8967 has_ck505 = false;
8968 can_ssc = true;
8969 }
8970
1c1a24d2
L
8971 /* Check if any DPLLs are using the SSC source */
8972 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
8973 u32 temp = I915_READ(PCH_DPLL(i));
8974
8975 if (!(temp & DPLL_VCO_ENABLE))
8976 continue;
8977
8978 if ((temp & PLL_REF_INPUT_MASK) ==
8979 PLLB_REF_INPUT_SPREADSPECTRUMIN) {
8980 using_ssc_source = true;
8981 break;
8982 }
8983 }
8984
8985 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d using_ssc_source %d\n",
8986 has_panel, has_lvds, has_ck505, using_ssc_source);
13d83a67
JB
8987
8988 /* Ironlake: try to setup display ref clock before DPLL
8989 * enabling. This is only under driver's control after
8990 * PCH B stepping, previous chipset stepping should be
8991 * ignoring this setting.
8992 */
74cfd7ac
CW
8993 val = I915_READ(PCH_DREF_CONTROL);
8994
8995 /* As we must carefully and slowly disable/enable each source in turn,
8996 * compute the final state we want first and check if we need to
8997 * make any changes at all.
8998 */
8999 final = val;
9000 final &= ~DREF_NONSPREAD_SOURCE_MASK;
9001 if (has_ck505)
9002 final |= DREF_NONSPREAD_CK505_ENABLE;
9003 else
9004 final |= DREF_NONSPREAD_SOURCE_ENABLE;
9005
8c07eb68 9006 final &= ~DREF_SSC_SOURCE_MASK;
74cfd7ac 9007 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
8c07eb68 9008 final &= ~DREF_SSC1_ENABLE;
74cfd7ac
CW
9009
9010 if (has_panel) {
9011 final |= DREF_SSC_SOURCE_ENABLE;
9012
9013 if (intel_panel_use_ssc(dev_priv) && can_ssc)
9014 final |= DREF_SSC1_ENABLE;
9015
9016 if (has_cpu_edp) {
9017 if (intel_panel_use_ssc(dev_priv) && can_ssc)
9018 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
9019 else
9020 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
9021 } else
9022 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
1c1a24d2
L
9023 } else if (using_ssc_source) {
9024 final |= DREF_SSC_SOURCE_ENABLE;
9025 final |= DREF_SSC1_ENABLE;
74cfd7ac
CW
9026 }
9027
9028 if (final == val)
9029 return;
9030
13d83a67 9031 /* Always enable nonspread source */
74cfd7ac 9032 val &= ~DREF_NONSPREAD_SOURCE_MASK;
13d83a67 9033
99eb6a01 9034 if (has_ck505)
74cfd7ac 9035 val |= DREF_NONSPREAD_CK505_ENABLE;
99eb6a01 9036 else
74cfd7ac 9037 val |= DREF_NONSPREAD_SOURCE_ENABLE;
13d83a67 9038
199e5d79 9039 if (has_panel) {
74cfd7ac
CW
9040 val &= ~DREF_SSC_SOURCE_MASK;
9041 val |= DREF_SSC_SOURCE_ENABLE;
13d83a67 9042
199e5d79 9043 /* SSC must be turned on before enabling the CPU output */
99eb6a01 9044 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
199e5d79 9045 DRM_DEBUG_KMS("Using SSC on panel\n");
74cfd7ac 9046 val |= DREF_SSC1_ENABLE;
e77166b5 9047 } else
74cfd7ac 9048 val &= ~DREF_SSC1_ENABLE;
199e5d79
KP
9049
9050 /* Get SSC going before enabling the outputs */
74cfd7ac 9051 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
9052 POSTING_READ(PCH_DREF_CONTROL);
9053 udelay(200);
9054
74cfd7ac 9055 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
13d83a67
JB
9056
9057 /* Enable CPU source on CPU attached eDP */
199e5d79 9058 if (has_cpu_edp) {
99eb6a01 9059 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
199e5d79 9060 DRM_DEBUG_KMS("Using SSC on eDP\n");
74cfd7ac 9061 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
eba905b2 9062 } else
74cfd7ac 9063 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
199e5d79 9064 } else
74cfd7ac 9065 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
199e5d79 9066
74cfd7ac 9067 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
9068 POSTING_READ(PCH_DREF_CONTROL);
9069 udelay(200);
9070 } else {
1c1a24d2 9071 DRM_DEBUG_KMS("Disabling CPU source output\n");
199e5d79 9072
74cfd7ac 9073 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
199e5d79
KP
9074
9075 /* Turn off CPU output */
74cfd7ac 9076 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
199e5d79 9077
74cfd7ac 9078 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
9079 POSTING_READ(PCH_DREF_CONTROL);
9080 udelay(200);
9081
1c1a24d2
L
9082 if (!using_ssc_source) {
9083 DRM_DEBUG_KMS("Disabling SSC source\n");
199e5d79 9084
1c1a24d2
L
9085 /* Turn off the SSC source */
9086 val &= ~DREF_SSC_SOURCE_MASK;
9087 val |= DREF_SSC_SOURCE_DISABLE;
f165d283 9088
1c1a24d2
L
9089 /* Turn off SSC1 */
9090 val &= ~DREF_SSC1_ENABLE;
9091
9092 I915_WRITE(PCH_DREF_CONTROL, val);
9093 POSTING_READ(PCH_DREF_CONTROL);
9094 udelay(200);
9095 }
13d83a67 9096 }
74cfd7ac
CW
9097
9098 BUG_ON(val != final);
13d83a67
JB
9099}
9100
f31f2d55 9101static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
dde86e2d 9102{
f31f2d55 9103 uint32_t tmp;
dde86e2d 9104
0ff066a9
PZ
9105 tmp = I915_READ(SOUTH_CHICKEN2);
9106 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
9107 I915_WRITE(SOUTH_CHICKEN2, tmp);
dde86e2d 9108
cf3598c2
ID
9109 if (wait_for_us(I915_READ(SOUTH_CHICKEN2) &
9110 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
0ff066a9 9111 DRM_ERROR("FDI mPHY reset assert timeout\n");
dde86e2d 9112
0ff066a9
PZ
9113 tmp = I915_READ(SOUTH_CHICKEN2);
9114 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
9115 I915_WRITE(SOUTH_CHICKEN2, tmp);
dde86e2d 9116
cf3598c2
ID
9117 if (wait_for_us((I915_READ(SOUTH_CHICKEN2) &
9118 FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
0ff066a9 9119 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
f31f2d55
PZ
9120}
9121
9122/* WaMPhyProgramming:hsw */
9123static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
9124{
9125 uint32_t tmp;
dde86e2d
PZ
9126
9127 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
9128 tmp &= ~(0xFF << 24);
9129 tmp |= (0x12 << 24);
9130 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
9131
dde86e2d
PZ
9132 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
9133 tmp |= (1 << 11);
9134 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
9135
9136 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
9137 tmp |= (1 << 11);
9138 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
9139
dde86e2d
PZ
9140 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
9141 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
9142 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
9143
9144 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
9145 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
9146 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
9147
0ff066a9
PZ
9148 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
9149 tmp &= ~(7 << 13);
9150 tmp |= (5 << 13);
9151 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
dde86e2d 9152
0ff066a9
PZ
9153 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
9154 tmp &= ~(7 << 13);
9155 tmp |= (5 << 13);
9156 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
dde86e2d
PZ
9157
9158 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
9159 tmp &= ~0xFF;
9160 tmp |= 0x1C;
9161 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
9162
9163 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
9164 tmp &= ~0xFF;
9165 tmp |= 0x1C;
9166 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
9167
9168 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
9169 tmp &= ~(0xFF << 16);
9170 tmp |= (0x1C << 16);
9171 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
9172
9173 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
9174 tmp &= ~(0xFF << 16);
9175 tmp |= (0x1C << 16);
9176 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
9177
0ff066a9
PZ
9178 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
9179 tmp |= (1 << 27);
9180 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
dde86e2d 9181
0ff066a9
PZ
9182 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
9183 tmp |= (1 << 27);
9184 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
dde86e2d 9185
0ff066a9
PZ
9186 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
9187 tmp &= ~(0xF << 28);
9188 tmp |= (4 << 28);
9189 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
dde86e2d 9190
0ff066a9
PZ
9191 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
9192 tmp &= ~(0xF << 28);
9193 tmp |= (4 << 28);
9194 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
f31f2d55
PZ
9195}
9196
2fa86a1f
PZ
9197/* Implements 3 different sequences from BSpec chapter "Display iCLK
9198 * Programming" based on the parameters passed:
9199 * - Sequence to enable CLKOUT_DP
9200 * - Sequence to enable CLKOUT_DP without spread
9201 * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
9202 */
9203static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
9204 bool with_fdi)
f31f2d55 9205{
fac5e23e 9206 struct drm_i915_private *dev_priv = to_i915(dev);
2fa86a1f
PZ
9207 uint32_t reg, tmp;
9208
9209 if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
9210 with_spread = true;
4f8036a2
TU
9211 if (WARN(HAS_PCH_LPT_LP(dev_priv) &&
9212 with_fdi, "LP PCH doesn't have FDI\n"))
2fa86a1f 9213 with_fdi = false;
f31f2d55 9214
a580516d 9215 mutex_lock(&dev_priv->sb_lock);
f31f2d55
PZ
9216
9217 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
9218 tmp &= ~SBI_SSCCTL_DISABLE;
9219 tmp |= SBI_SSCCTL_PATHALT;
9220 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
9221
9222 udelay(24);
9223
2fa86a1f
PZ
9224 if (with_spread) {
9225 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
9226 tmp &= ~SBI_SSCCTL_PATHALT;
9227 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
f31f2d55 9228
2fa86a1f
PZ
9229 if (with_fdi) {
9230 lpt_reset_fdi_mphy(dev_priv);
9231 lpt_program_fdi_mphy(dev_priv);
9232 }
9233 }
dde86e2d 9234
4f8036a2 9235 reg = HAS_PCH_LPT_LP(dev_priv) ? SBI_GEN0 : SBI_DBUFF0;
2fa86a1f
PZ
9236 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
9237 tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
9238 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
c00db246 9239
a580516d 9240 mutex_unlock(&dev_priv->sb_lock);
dde86e2d
PZ
9241}
9242
47701c3b
PZ
9243/* Sequence to disable CLKOUT_DP */
9244static void lpt_disable_clkout_dp(struct drm_device *dev)
9245{
fac5e23e 9246 struct drm_i915_private *dev_priv = to_i915(dev);
47701c3b
PZ
9247 uint32_t reg, tmp;
9248
a580516d 9249 mutex_lock(&dev_priv->sb_lock);
47701c3b 9250
4f8036a2 9251 reg = HAS_PCH_LPT_LP(dev_priv) ? SBI_GEN0 : SBI_DBUFF0;
47701c3b
PZ
9252 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
9253 tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
9254 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
9255
9256 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
9257 if (!(tmp & SBI_SSCCTL_DISABLE)) {
9258 if (!(tmp & SBI_SSCCTL_PATHALT)) {
9259 tmp |= SBI_SSCCTL_PATHALT;
9260 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
9261 udelay(32);
9262 }
9263 tmp |= SBI_SSCCTL_DISABLE;
9264 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
9265 }
9266
a580516d 9267 mutex_unlock(&dev_priv->sb_lock);
47701c3b
PZ
9268}
9269
f7be2c21
VS
9270#define BEND_IDX(steps) ((50 + (steps)) / 5)
9271
9272static const uint16_t sscdivintphase[] = {
9273 [BEND_IDX( 50)] = 0x3B23,
9274 [BEND_IDX( 45)] = 0x3B23,
9275 [BEND_IDX( 40)] = 0x3C23,
9276 [BEND_IDX( 35)] = 0x3C23,
9277 [BEND_IDX( 30)] = 0x3D23,
9278 [BEND_IDX( 25)] = 0x3D23,
9279 [BEND_IDX( 20)] = 0x3E23,
9280 [BEND_IDX( 15)] = 0x3E23,
9281 [BEND_IDX( 10)] = 0x3F23,
9282 [BEND_IDX( 5)] = 0x3F23,
9283 [BEND_IDX( 0)] = 0x0025,
9284 [BEND_IDX( -5)] = 0x0025,
9285 [BEND_IDX(-10)] = 0x0125,
9286 [BEND_IDX(-15)] = 0x0125,
9287 [BEND_IDX(-20)] = 0x0225,
9288 [BEND_IDX(-25)] = 0x0225,
9289 [BEND_IDX(-30)] = 0x0325,
9290 [BEND_IDX(-35)] = 0x0325,
9291 [BEND_IDX(-40)] = 0x0425,
9292 [BEND_IDX(-45)] = 0x0425,
9293 [BEND_IDX(-50)] = 0x0525,
9294};
9295
9296/*
9297 * Bend CLKOUT_DP
9298 * steps -50 to 50 inclusive, in steps of 5
9299 * < 0 slow down the clock, > 0 speed up the clock, 0 == no bend (135MHz)
9300 * change in clock period = -(steps / 10) * 5.787 ps
9301 */
9302static void lpt_bend_clkout_dp(struct drm_i915_private *dev_priv, int steps)
9303{
9304 uint32_t tmp;
9305 int idx = BEND_IDX(steps);
9306
9307 if (WARN_ON(steps % 5 != 0))
9308 return;
9309
9310 if (WARN_ON(idx >= ARRAY_SIZE(sscdivintphase)))
9311 return;
9312
9313 mutex_lock(&dev_priv->sb_lock);
9314
9315 if (steps % 10 != 0)
9316 tmp = 0xAAAAAAAB;
9317 else
9318 tmp = 0x00000000;
9319 intel_sbi_write(dev_priv, SBI_SSCDITHPHASE, tmp, SBI_ICLK);
9320
9321 tmp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE, SBI_ICLK);
9322 tmp &= 0xffff0000;
9323 tmp |= sscdivintphase[idx];
9324 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE, tmp, SBI_ICLK);
9325
9326 mutex_unlock(&dev_priv->sb_lock);
9327}
9328
9329#undef BEND_IDX
9330
bf8fa3d3
PZ
9331static void lpt_init_pch_refclk(struct drm_device *dev)
9332{
bf8fa3d3
PZ
9333 struct intel_encoder *encoder;
9334 bool has_vga = false;
9335
b2784e15 9336 for_each_intel_encoder(dev, encoder) {
bf8fa3d3
PZ
9337 switch (encoder->type) {
9338 case INTEL_OUTPUT_ANALOG:
9339 has_vga = true;
9340 break;
6847d71b
PZ
9341 default:
9342 break;
bf8fa3d3
PZ
9343 }
9344 }
9345
f7be2c21
VS
9346 if (has_vga) {
9347 lpt_bend_clkout_dp(to_i915(dev), 0);
47701c3b 9348 lpt_enable_clkout_dp(dev, true, true);
f7be2c21 9349 } else {
47701c3b 9350 lpt_disable_clkout_dp(dev);
f7be2c21 9351 }
bf8fa3d3
PZ
9352}
9353
dde86e2d
PZ
9354/*
9355 * Initialize reference clocks when the driver loads
9356 */
9357void intel_init_pch_refclk(struct drm_device *dev)
9358{
6e266956
TU
9359 struct drm_i915_private *dev_priv = to_i915(dev);
9360
9361 if (HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv))
dde86e2d 9362 ironlake_init_pch_refclk(dev);
6e266956 9363 else if (HAS_PCH_LPT(dev_priv))
dde86e2d
PZ
9364 lpt_init_pch_refclk(dev);
9365}
9366
6ff93609 9367static void ironlake_set_pipeconf(struct drm_crtc *crtc)
79e53945 9368{
fac5e23e 9369 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
79e53945
JB
9370 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9371 int pipe = intel_crtc->pipe;
c8203565
PZ
9372 uint32_t val;
9373
78114071 9374 val = 0;
c8203565 9375
6e3c9717 9376 switch (intel_crtc->config->pipe_bpp) {
c8203565 9377 case 18:
dfd07d72 9378 val |= PIPECONF_6BPC;
c8203565
PZ
9379 break;
9380 case 24:
dfd07d72 9381 val |= PIPECONF_8BPC;
c8203565
PZ
9382 break;
9383 case 30:
dfd07d72 9384 val |= PIPECONF_10BPC;
c8203565
PZ
9385 break;
9386 case 36:
dfd07d72 9387 val |= PIPECONF_12BPC;
c8203565
PZ
9388 break;
9389 default:
cc769b62
PZ
9390 /* Case prevented by intel_choose_pipe_bpp_dither. */
9391 BUG();
c8203565
PZ
9392 }
9393
6e3c9717 9394 if (intel_crtc->config->dither)
c8203565
PZ
9395 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
9396
6e3c9717 9397 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
c8203565
PZ
9398 val |= PIPECONF_INTERLACED_ILK;
9399 else
9400 val |= PIPECONF_PROGRESSIVE;
9401
6e3c9717 9402 if (intel_crtc->config->limited_color_range)
3685a8f3 9403 val |= PIPECONF_COLOR_RANGE_SELECT;
3685a8f3 9404
c8203565
PZ
9405 I915_WRITE(PIPECONF(pipe), val);
9406 POSTING_READ(PIPECONF(pipe));
9407}
9408
6ff93609 9409static void haswell_set_pipeconf(struct drm_crtc *crtc)
ee2b0b38 9410{
fac5e23e 9411 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
ee2b0b38 9412 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6e3c9717 9413 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
391bf048 9414 u32 val = 0;
ee2b0b38 9415
391bf048 9416 if (IS_HASWELL(dev_priv) && intel_crtc->config->dither)
ee2b0b38
PZ
9417 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
9418
6e3c9717 9419 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
ee2b0b38
PZ
9420 val |= PIPECONF_INTERLACED_ILK;
9421 else
9422 val |= PIPECONF_PROGRESSIVE;
9423
702e7a56
PZ
9424 I915_WRITE(PIPECONF(cpu_transcoder), val);
9425 POSTING_READ(PIPECONF(cpu_transcoder));
391bf048
JN
9426}
9427
391bf048
JN
9428static void haswell_set_pipemisc(struct drm_crtc *crtc)
9429{
fac5e23e 9430 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
391bf048 9431 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
756f85cf 9432
391bf048
JN
9433 if (IS_BROADWELL(dev_priv) || INTEL_INFO(dev_priv)->gen >= 9) {
9434 u32 val = 0;
756f85cf 9435
6e3c9717 9436 switch (intel_crtc->config->pipe_bpp) {
756f85cf
PZ
9437 case 18:
9438 val |= PIPEMISC_DITHER_6_BPC;
9439 break;
9440 case 24:
9441 val |= PIPEMISC_DITHER_8_BPC;
9442 break;
9443 case 30:
9444 val |= PIPEMISC_DITHER_10_BPC;
9445 break;
9446 case 36:
9447 val |= PIPEMISC_DITHER_12_BPC;
9448 break;
9449 default:
9450 /* Case prevented by pipe_config_set_bpp. */
9451 BUG();
9452 }
9453
6e3c9717 9454 if (intel_crtc->config->dither)
756f85cf
PZ
9455 val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
9456
391bf048 9457 I915_WRITE(PIPEMISC(intel_crtc->pipe), val);
756f85cf 9458 }
ee2b0b38
PZ
9459}
9460
d4b1931c
PZ
9461int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
9462{
9463 /*
9464 * Account for spread spectrum to avoid
9465 * oversubscribing the link. Max center spread
9466 * is 2.5%; use 5% for safety's sake.
9467 */
9468 u32 bps = target_clock * bpp * 21 / 20;
619d4d04 9469 return DIV_ROUND_UP(bps, link_bw * 8);
d4b1931c
PZ
9470}
9471
7429e9d4 9472static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
6cf86a5e 9473{
7429e9d4 9474 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
f48d8f23
PZ
9475}
9476
b75ca6f6
ACO
9477static void ironlake_compute_dpll(struct intel_crtc *intel_crtc,
9478 struct intel_crtc_state *crtc_state,
9e2c8475 9479 struct dpll *reduced_clock)
79e53945 9480{
de13a2e3 9481 struct drm_crtc *crtc = &intel_crtc->base;
79e53945 9482 struct drm_device *dev = crtc->dev;
fac5e23e 9483 struct drm_i915_private *dev_priv = to_i915(dev);
b75ca6f6 9484 u32 dpll, fp, fp2;
3d6e9ee0 9485 int factor;
79e53945 9486
c1858123 9487 /* Enable autotuning of the PLL clock (if permissible) */
8febb297 9488 factor = 21;
3d6e9ee0 9489 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
8febb297 9490 if ((intel_panel_use_ssc(dev_priv) &&
e91e941b 9491 dev_priv->vbt.lvds_ssc_freq == 100000) ||
6e266956 9492 (HAS_PCH_IBX(dev_priv) && intel_is_dual_link_lvds(dev)))
8febb297 9493 factor = 25;
190f68c5 9494 } else if (crtc_state->sdvo_tv_clock)
8febb297 9495 factor = 20;
c1858123 9496
b75ca6f6
ACO
9497 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
9498
190f68c5 9499 if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
b75ca6f6
ACO
9500 fp |= FP_CB_TUNE;
9501
9502 if (reduced_clock) {
9503 fp2 = i9xx_dpll_compute_fp(reduced_clock);
2c07245f 9504
b75ca6f6
ACO
9505 if (reduced_clock->m < factor * reduced_clock->n)
9506 fp2 |= FP_CB_TUNE;
9507 } else {
9508 fp2 = fp;
9509 }
9a7c7890 9510
5eddb70b 9511 dpll = 0;
2c07245f 9512
3d6e9ee0 9513 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS))
a07d6787
EA
9514 dpll |= DPLLB_MODE_LVDS;
9515 else
9516 dpll |= DPLLB_MODE_DAC_SERIAL;
198a037f 9517
190f68c5 9518 dpll |= (crtc_state->pixel_multiplier - 1)
ef1b460d 9519 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
198a037f 9520
3d6e9ee0
VS
9521 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO) ||
9522 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
4a33e48d 9523 dpll |= DPLL_SDVO_HIGH_SPEED;
3d6e9ee0 9524
37a5650b 9525 if (intel_crtc_has_dp_encoder(crtc_state))
4a33e48d 9526 dpll |= DPLL_SDVO_HIGH_SPEED;
79e53945 9527
7d7f8633
VS
9528 /*
9529 * The high speed IO clock is only really required for
9530 * SDVO/HDMI/DP, but we also enable it for CRT to make it
9531 * possible to share the DPLL between CRT and HDMI. Enabling
9532 * the clock needlessly does no real harm, except use up a
9533 * bit of power potentially.
9534 *
9535 * We'll limit this to IVB with 3 pipes, since it has only two
9536 * DPLLs and so DPLL sharing is the only way to get three pipes
9537 * driving PCH ports at the same time. On SNB we could do this,
9538 * and potentially avoid enabling the second DPLL, but it's not
9539 * clear if it''s a win or loss power wise. No point in doing
9540 * this on ILK at all since it has a fixed DPLL<->pipe mapping.
9541 */
9542 if (INTEL_INFO(dev_priv)->num_pipes == 3 &&
9543 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_ANALOG))
9544 dpll |= DPLL_SDVO_HIGH_SPEED;
9545
a07d6787 9546 /* compute bitmask from p1 value */
190f68c5 9547 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
a07d6787 9548 /* also FPA1 */
190f68c5 9549 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
a07d6787 9550
190f68c5 9551 switch (crtc_state->dpll.p2) {
a07d6787
EA
9552 case 5:
9553 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
9554 break;
9555 case 7:
9556 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
9557 break;
9558 case 10:
9559 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
9560 break;
9561 case 14:
9562 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
9563 break;
79e53945
JB
9564 }
9565
3d6e9ee0
VS
9566 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
9567 intel_panel_use_ssc(dev_priv))
43565a06 9568 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
79e53945
JB
9569 else
9570 dpll |= PLL_REF_INPUT_DREFCLK;
9571
b75ca6f6
ACO
9572 dpll |= DPLL_VCO_ENABLE;
9573
9574 crtc_state->dpll_hw_state.dpll = dpll;
9575 crtc_state->dpll_hw_state.fp0 = fp;
9576 crtc_state->dpll_hw_state.fp1 = fp2;
de13a2e3
PZ
9577}
9578
190f68c5
ACO
9579static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
9580 struct intel_crtc_state *crtc_state)
de13a2e3 9581{
997c030c 9582 struct drm_device *dev = crtc->base.dev;
fac5e23e 9583 struct drm_i915_private *dev_priv = to_i915(dev);
9e2c8475 9584 struct dpll reduced_clock;
7ed9f894 9585 bool has_reduced_clock = false;
e2b78267 9586 struct intel_shared_dpll *pll;
1b6f4958 9587 const struct intel_limit *limit;
997c030c 9588 int refclk = 120000;
de13a2e3 9589
dd3cd74a
ACO
9590 memset(&crtc_state->dpll_hw_state, 0,
9591 sizeof(crtc_state->dpll_hw_state));
9592
ded220e2
ACO
9593 crtc->lowfreq_avail = false;
9594
9595 /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
9596 if (!crtc_state->has_pch_encoder)
9597 return 0;
79e53945 9598
2d84d2b3 9599 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
997c030c
ACO
9600 if (intel_panel_use_ssc(dev_priv)) {
9601 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
9602 dev_priv->vbt.lvds_ssc_freq);
9603 refclk = dev_priv->vbt.lvds_ssc_freq;
9604 }
9605
9606 if (intel_is_dual_link_lvds(dev)) {
9607 if (refclk == 100000)
9608 limit = &intel_limits_ironlake_dual_lvds_100m;
9609 else
9610 limit = &intel_limits_ironlake_dual_lvds;
9611 } else {
9612 if (refclk == 100000)
9613 limit = &intel_limits_ironlake_single_lvds_100m;
9614 else
9615 limit = &intel_limits_ironlake_single_lvds;
9616 }
9617 } else {
9618 limit = &intel_limits_ironlake_dac;
9619 }
9620
364ee29d 9621 if (!crtc_state->clock_set &&
997c030c
ACO
9622 !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
9623 refclk, NULL, &crtc_state->dpll)) {
364ee29d
ACO
9624 DRM_ERROR("Couldn't find PLL settings for mode!\n");
9625 return -EINVAL;
f47709a9 9626 }
79e53945 9627
b75ca6f6
ACO
9628 ironlake_compute_dpll(crtc, crtc_state,
9629 has_reduced_clock ? &reduced_clock : NULL);
66e985c0 9630
ded220e2
ACO
9631 pll = intel_get_shared_dpll(crtc, crtc_state, NULL);
9632 if (pll == NULL) {
9633 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
9634 pipe_name(crtc->pipe));
9635 return -EINVAL;
3fb37703 9636 }
79e53945 9637
2d84d2b3 9638 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
ded220e2 9639 has_reduced_clock)
c7653199 9640 crtc->lowfreq_avail = true;
e2b78267 9641
c8f7a0db 9642 return 0;
79e53945
JB
9643}
9644
eb14cb74
VS
9645static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
9646 struct intel_link_m_n *m_n)
9647{
9648 struct drm_device *dev = crtc->base.dev;
fac5e23e 9649 struct drm_i915_private *dev_priv = to_i915(dev);
eb14cb74
VS
9650 enum pipe pipe = crtc->pipe;
9651
9652 m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
9653 m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
9654 m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
9655 & ~TU_SIZE_MASK;
9656 m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
9657 m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
9658 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
9659}
9660
9661static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
9662 enum transcoder transcoder,
b95af8be
VK
9663 struct intel_link_m_n *m_n,
9664 struct intel_link_m_n *m2_n2)
72419203
SV
9665{
9666 struct drm_device *dev = crtc->base.dev;
fac5e23e 9667 struct drm_i915_private *dev_priv = to_i915(dev);
eb14cb74 9668 enum pipe pipe = crtc->pipe;
72419203 9669
eb14cb74
VS
9670 if (INTEL_INFO(dev)->gen >= 5) {
9671 m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
9672 m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
9673 m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
9674 & ~TU_SIZE_MASK;
9675 m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
9676 m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
9677 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
b95af8be
VK
9678 /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
9679 * gen < 8) and if DRRS is supported (to make sure the
9680 * registers are not unnecessarily read).
9681 */
9682 if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
6e3c9717 9683 crtc->config->has_drrs) {
b95af8be
VK
9684 m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
9685 m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
9686 m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
9687 & ~TU_SIZE_MASK;
9688 m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
9689 m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
9690 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
9691 }
eb14cb74
VS
9692 } else {
9693 m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
9694 m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
9695 m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
9696 & ~TU_SIZE_MASK;
9697 m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
9698 m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
9699 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
9700 }
9701}
9702
9703void intel_dp_get_m_n(struct intel_crtc *crtc,
5cec258b 9704 struct intel_crtc_state *pipe_config)
eb14cb74 9705{
681a8504 9706 if (pipe_config->has_pch_encoder)
eb14cb74
VS
9707 intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
9708 else
9709 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
b95af8be
VK
9710 &pipe_config->dp_m_n,
9711 &pipe_config->dp_m2_n2);
eb14cb74 9712}
72419203 9713
eb14cb74 9714static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
5cec258b 9715 struct intel_crtc_state *pipe_config)
eb14cb74
VS
9716{
9717 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
b95af8be 9718 &pipe_config->fdi_m_n, NULL);
72419203
SV
9719}
9720
bd2e244f 9721static void skylake_get_pfit_config(struct intel_crtc *crtc,
5cec258b 9722 struct intel_crtc_state *pipe_config)
bd2e244f
JB
9723{
9724 struct drm_device *dev = crtc->base.dev;
fac5e23e 9725 struct drm_i915_private *dev_priv = to_i915(dev);
a1b2278e
CK
9726 struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
9727 uint32_t ps_ctrl = 0;
9728 int id = -1;
9729 int i;
bd2e244f 9730
a1b2278e
CK
9731 /* find scaler attached to this pipe */
9732 for (i = 0; i < crtc->num_scalers; i++) {
9733 ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
9734 if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
9735 id = i;
9736 pipe_config->pch_pfit.enabled = true;
9737 pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
9738 pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
9739 break;
9740 }
9741 }
bd2e244f 9742
a1b2278e
CK
9743 scaler_state->scaler_id = id;
9744 if (id >= 0) {
9745 scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
9746 } else {
9747 scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
bd2e244f
JB
9748 }
9749}
9750
5724dbd1
DL
9751static void
9752skylake_get_initial_plane_config(struct intel_crtc *crtc,
9753 struct intel_initial_plane_config *plane_config)
bc8d7dff
DL
9754{
9755 struct drm_device *dev = crtc->base.dev;
fac5e23e 9756 struct drm_i915_private *dev_priv = to_i915(dev);
40f46283 9757 u32 val, base, offset, stride_mult, tiling;
bc8d7dff
DL
9758 int pipe = crtc->pipe;
9759 int fourcc, pixel_format;
6761dd31 9760 unsigned int aligned_height;
bc8d7dff 9761 struct drm_framebuffer *fb;
1b842c89 9762 struct intel_framebuffer *intel_fb;
bc8d7dff 9763
d9806c9f 9764 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
1b842c89 9765 if (!intel_fb) {
bc8d7dff
DL
9766 DRM_DEBUG_KMS("failed to alloc fb\n");
9767 return;
9768 }
9769
1b842c89
DL
9770 fb = &intel_fb->base;
9771
bc8d7dff 9772 val = I915_READ(PLANE_CTL(pipe, 0));
42a7b088
DL
9773 if (!(val & PLANE_CTL_ENABLE))
9774 goto error;
9775
bc8d7dff
DL
9776 pixel_format = val & PLANE_CTL_FORMAT_MASK;
9777 fourcc = skl_format_to_fourcc(pixel_format,
9778 val & PLANE_CTL_ORDER_RGBX,
9779 val & PLANE_CTL_ALPHA_MASK);
9780 fb->pixel_format = fourcc;
9781 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
9782
40f46283
DL
9783 tiling = val & PLANE_CTL_TILED_MASK;
9784 switch (tiling) {
9785 case PLANE_CTL_TILED_LINEAR:
9786 fb->modifier[0] = DRM_FORMAT_MOD_NONE;
9787 break;
9788 case PLANE_CTL_TILED_X:
9789 plane_config->tiling = I915_TILING_X;
9790 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
9791 break;
9792 case PLANE_CTL_TILED_Y:
9793 fb->modifier[0] = I915_FORMAT_MOD_Y_TILED;
9794 break;
9795 case PLANE_CTL_TILED_YF:
9796 fb->modifier[0] = I915_FORMAT_MOD_Yf_TILED;
9797 break;
9798 default:
9799 MISSING_CASE(tiling);
9800 goto error;
9801 }
9802
bc8d7dff
DL
9803 base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
9804 plane_config->base = base;
9805
9806 offset = I915_READ(PLANE_OFFSET(pipe, 0));
9807
9808 val = I915_READ(PLANE_SIZE(pipe, 0));
9809 fb->height = ((val >> 16) & 0xfff) + 1;
9810 fb->width = ((val >> 0) & 0x1fff) + 1;
9811
9812 val = I915_READ(PLANE_STRIDE(pipe, 0));
7b49f948 9813 stride_mult = intel_fb_stride_alignment(dev_priv, fb->modifier[0],
40f46283 9814 fb->pixel_format);
bc8d7dff
DL
9815 fb->pitches[0] = (val & 0x3ff) * stride_mult;
9816
9817 aligned_height = intel_fb_align_height(dev, fb->height,
091df6cb
SV
9818 fb->pixel_format,
9819 fb->modifier[0]);
bc8d7dff 9820
f37b5c2b 9821 plane_config->size = fb->pitches[0] * aligned_height;
bc8d7dff
DL
9822
9823 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9824 pipe_name(pipe), fb->width, fb->height,
9825 fb->bits_per_pixel, base, fb->pitches[0],
9826 plane_config->size);
9827
2d14030b 9828 plane_config->fb = intel_fb;
bc8d7dff
DL
9829 return;
9830
9831error:
d1a3a036 9832 kfree(intel_fb);
bc8d7dff
DL
9833}
9834
2fa2fe9a 9835static void ironlake_get_pfit_config(struct intel_crtc *crtc,
5cec258b 9836 struct intel_crtc_state *pipe_config)
2fa2fe9a
SV
9837{
9838 struct drm_device *dev = crtc->base.dev;
fac5e23e 9839 struct drm_i915_private *dev_priv = to_i915(dev);
2fa2fe9a
SV
9840 uint32_t tmp;
9841
9842 tmp = I915_READ(PF_CTL(crtc->pipe));
9843
9844 if (tmp & PF_ENABLE) {
fd4daa9c 9845 pipe_config->pch_pfit.enabled = true;
2fa2fe9a
SV
9846 pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
9847 pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
cb8b2a30
SV
9848
9849 /* We currently do not free assignements of panel fitters on
9850 * ivb/hsw (since we don't use the higher upscaling modes which
9851 * differentiates them) so just WARN about this case for now. */
5db94019 9852 if (IS_GEN7(dev_priv)) {
cb8b2a30
SV
9853 WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
9854 PF_PIPE_SEL_IVB(crtc->pipe));
9855 }
2fa2fe9a 9856 }
79e53945
JB
9857}
9858
5724dbd1
DL
9859static void
9860ironlake_get_initial_plane_config(struct intel_crtc *crtc,
9861 struct intel_initial_plane_config *plane_config)
4c6baa59
JB
9862{
9863 struct drm_device *dev = crtc->base.dev;
fac5e23e 9864 struct drm_i915_private *dev_priv = to_i915(dev);
4c6baa59 9865 u32 val, base, offset;
aeee5a49 9866 int pipe = crtc->pipe;
4c6baa59 9867 int fourcc, pixel_format;
6761dd31 9868 unsigned int aligned_height;
b113d5ee 9869 struct drm_framebuffer *fb;
1b842c89 9870 struct intel_framebuffer *intel_fb;
4c6baa59 9871
42a7b088
DL
9872 val = I915_READ(DSPCNTR(pipe));
9873 if (!(val & DISPLAY_PLANE_ENABLE))
9874 return;
9875
d9806c9f 9876 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
1b842c89 9877 if (!intel_fb) {
4c6baa59
JB
9878 DRM_DEBUG_KMS("failed to alloc fb\n");
9879 return;
9880 }
9881
1b842c89
DL
9882 fb = &intel_fb->base;
9883
18c5247e
SV
9884 if (INTEL_INFO(dev)->gen >= 4) {
9885 if (val & DISPPLANE_TILED) {
49af449b 9886 plane_config->tiling = I915_TILING_X;
18c5247e
SV
9887 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
9888 }
9889 }
4c6baa59
JB
9890
9891 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
b35d63fa 9892 fourcc = i9xx_format_to_fourcc(pixel_format);
b113d5ee
DL
9893 fb->pixel_format = fourcc;
9894 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
4c6baa59 9895
aeee5a49 9896 base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
8652744b 9897 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
aeee5a49 9898 offset = I915_READ(DSPOFFSET(pipe));
4c6baa59 9899 } else {
49af449b 9900 if (plane_config->tiling)
aeee5a49 9901 offset = I915_READ(DSPTILEOFF(pipe));
4c6baa59 9902 else
aeee5a49 9903 offset = I915_READ(DSPLINOFF(pipe));
4c6baa59
JB
9904 }
9905 plane_config->base = base;
9906
9907 val = I915_READ(PIPESRC(pipe));
b113d5ee
DL
9908 fb->width = ((val >> 16) & 0xfff) + 1;
9909 fb->height = ((val >> 0) & 0xfff) + 1;
4c6baa59
JB
9910
9911 val = I915_READ(DSPSTRIDE(pipe));
b113d5ee 9912 fb->pitches[0] = val & 0xffffffc0;
4c6baa59 9913
b113d5ee 9914 aligned_height = intel_fb_align_height(dev, fb->height,
091df6cb
SV
9915 fb->pixel_format,
9916 fb->modifier[0]);
4c6baa59 9917
f37b5c2b 9918 plane_config->size = fb->pitches[0] * aligned_height;
4c6baa59 9919
2844a921
DL
9920 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9921 pipe_name(pipe), fb->width, fb->height,
9922 fb->bits_per_pixel, base, fb->pitches[0],
9923 plane_config->size);
b113d5ee 9924
2d14030b 9925 plane_config->fb = intel_fb;
4c6baa59
JB
9926}
9927
0e8ffe1b 9928static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
5cec258b 9929 struct intel_crtc_state *pipe_config)
0e8ffe1b
SV
9930{
9931 struct drm_device *dev = crtc->base.dev;
fac5e23e 9932 struct drm_i915_private *dev_priv = to_i915(dev);
1729050e 9933 enum intel_display_power_domain power_domain;
0e8ffe1b 9934 uint32_t tmp;
1729050e 9935 bool ret;
0e8ffe1b 9936
1729050e
ID
9937 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
9938 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
930e8c9e
PZ
9939 return false;
9940
e143a21c 9941 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
8106ddbd 9942 pipe_config->shared_dpll = NULL;
eccb140b 9943
1729050e 9944 ret = false;
0e8ffe1b
SV
9945 tmp = I915_READ(PIPECONF(crtc->pipe));
9946 if (!(tmp & PIPECONF_ENABLE))
1729050e 9947 goto out;
0e8ffe1b 9948
42571aef
VS
9949 switch (tmp & PIPECONF_BPC_MASK) {
9950 case PIPECONF_6BPC:
9951 pipe_config->pipe_bpp = 18;
9952 break;
9953 case PIPECONF_8BPC:
9954 pipe_config->pipe_bpp = 24;
9955 break;
9956 case PIPECONF_10BPC:
9957 pipe_config->pipe_bpp = 30;
9958 break;
9959 case PIPECONF_12BPC:
9960 pipe_config->pipe_bpp = 36;
9961 break;
9962 default:
9963 break;
9964 }
9965
b5a9fa09
SV
9966 if (tmp & PIPECONF_COLOR_RANGE_SELECT)
9967 pipe_config->limited_color_range = true;
9968
ab9412ba 9969 if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
66e985c0 9970 struct intel_shared_dpll *pll;
8106ddbd 9971 enum intel_dpll_id pll_id;
66e985c0 9972
88adfff1
SV
9973 pipe_config->has_pch_encoder = true;
9974
627eb5a3
SV
9975 tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
9976 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
9977 FDI_DP_PORT_WIDTH_SHIFT) + 1;
72419203
SV
9978
9979 ironlake_get_fdi_m_n_config(crtc, pipe_config);
6c49f241 9980
2d1fe073 9981 if (HAS_PCH_IBX(dev_priv)) {
d9a7bc67
ID
9982 /*
9983 * The pipe->pch transcoder and pch transcoder->pll
9984 * mapping is fixed.
9985 */
8106ddbd 9986 pll_id = (enum intel_dpll_id) crtc->pipe;
c0d43d62
SV
9987 } else {
9988 tmp = I915_READ(PCH_DPLL_SEL);
9989 if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
8106ddbd 9990 pll_id = DPLL_ID_PCH_PLL_B;
c0d43d62 9991 else
8106ddbd 9992 pll_id= DPLL_ID_PCH_PLL_A;
c0d43d62 9993 }
66e985c0 9994
8106ddbd
ACO
9995 pipe_config->shared_dpll =
9996 intel_get_shared_dpll_by_id(dev_priv, pll_id);
9997 pll = pipe_config->shared_dpll;
66e985c0 9998
2edd6443
ACO
9999 WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
10000 &pipe_config->dpll_hw_state));
c93f54cf
SV
10001
10002 tmp = pipe_config->dpll_hw_state.dpll;
10003 pipe_config->pixel_multiplier =
10004 ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
10005 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
18442d08
VS
10006
10007 ironlake_pch_clock_get(crtc, pipe_config);
6c49f241
SV
10008 } else {
10009 pipe_config->pixel_multiplier = 1;
627eb5a3
SV
10010 }
10011
1bd1bd80 10012 intel_get_pipe_timings(crtc, pipe_config);
bc58be60 10013 intel_get_pipe_src_size(crtc, pipe_config);
1bd1bd80 10014
2fa2fe9a
SV
10015 ironlake_get_pfit_config(crtc, pipe_config);
10016
1729050e
ID
10017 ret = true;
10018
10019out:
10020 intel_display_power_put(dev_priv, power_domain);
10021
10022 return ret;
0e8ffe1b
SV
10023}
10024
be256dc7
PZ
10025static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
10026{
91c8a326 10027 struct drm_device *dev = &dev_priv->drm;
be256dc7 10028 struct intel_crtc *crtc;
be256dc7 10029
d3fcc808 10030 for_each_intel_crtc(dev, crtc)
e2c719b7 10031 I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
be256dc7
PZ
10032 pipe_name(crtc->pipe));
10033
e2c719b7
RC
10034 I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
10035 I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
01403de3
VS
10036 I915_STATE_WARN(I915_READ(WRPLL_CTL(0)) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
10037 I915_STATE_WARN(I915_READ(WRPLL_CTL(1)) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
44cb734c 10038 I915_STATE_WARN(I915_READ(PP_STATUS(0)) & PP_ON, "Panel power on\n");
e2c719b7 10039 I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
be256dc7 10040 "CPU PWM1 enabled\n");
772c2a51 10041 if (IS_HASWELL(dev_priv))
e2c719b7 10042 I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
c5107b87 10043 "CPU PWM2 enabled\n");
e2c719b7 10044 I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
be256dc7 10045 "PCH PWM1 enabled\n");
e2c719b7 10046 I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
be256dc7 10047 "Utility pin enabled\n");
e2c719b7 10048 I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
be256dc7 10049
9926ada1
PZ
10050 /*
10051 * In theory we can still leave IRQs enabled, as long as only the HPD
10052 * interrupts remain enabled. We used to check for that, but since it's
10053 * gen-specific and since we only disable LCPLL after we fully disable
10054 * the interrupts, the check below should be enough.
10055 */
e2c719b7 10056 I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
be256dc7
PZ
10057}
10058
9ccd5aeb
PZ
10059static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
10060{
772c2a51 10061 if (IS_HASWELL(dev_priv))
9ccd5aeb
PZ
10062 return I915_READ(D_COMP_HSW);
10063 else
10064 return I915_READ(D_COMP_BDW);
10065}
10066
3c4c9b81
PZ
10067static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
10068{
772c2a51 10069 if (IS_HASWELL(dev_priv)) {
3c4c9b81
PZ
10070 mutex_lock(&dev_priv->rps.hw_lock);
10071 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
10072 val))
79cf219a 10073 DRM_DEBUG_KMS("Failed to write to D_COMP\n");
3c4c9b81
PZ
10074 mutex_unlock(&dev_priv->rps.hw_lock);
10075 } else {
9ccd5aeb
PZ
10076 I915_WRITE(D_COMP_BDW, val);
10077 POSTING_READ(D_COMP_BDW);
3c4c9b81 10078 }
be256dc7
PZ
10079}
10080
10081/*
10082 * This function implements pieces of two sequences from BSpec:
10083 * - Sequence for display software to disable LCPLL
10084 * - Sequence for display software to allow package C8+
10085 * The steps implemented here are just the steps that actually touch the LCPLL
10086 * register. Callers should take care of disabling all the display engine
10087 * functions, doing the mode unset, fixing interrupts, etc.
10088 */
6ff58d53
PZ
10089static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
10090 bool switch_to_fclk, bool allow_power_down)
be256dc7
PZ
10091{
10092 uint32_t val;
10093
10094 assert_can_disable_lcpll(dev_priv);
10095
10096 val = I915_READ(LCPLL_CTL);
10097
10098 if (switch_to_fclk) {
10099 val |= LCPLL_CD_SOURCE_FCLK;
10100 I915_WRITE(LCPLL_CTL, val);
10101
f53dd63f
ID
10102 if (wait_for_us(I915_READ(LCPLL_CTL) &
10103 LCPLL_CD_SOURCE_FCLK_DONE, 1))
be256dc7
PZ
10104 DRM_ERROR("Switching to FCLK failed\n");
10105
10106 val = I915_READ(LCPLL_CTL);
10107 }
10108
10109 val |= LCPLL_PLL_DISABLE;
10110 I915_WRITE(LCPLL_CTL, val);
10111 POSTING_READ(LCPLL_CTL);
10112
24d8441d 10113 if (intel_wait_for_register(dev_priv, LCPLL_CTL, LCPLL_PLL_LOCK, 0, 1))
be256dc7
PZ
10114 DRM_ERROR("LCPLL still locked\n");
10115
9ccd5aeb 10116 val = hsw_read_dcomp(dev_priv);
be256dc7 10117 val |= D_COMP_COMP_DISABLE;
3c4c9b81 10118 hsw_write_dcomp(dev_priv, val);
be256dc7
PZ
10119 ndelay(100);
10120
9ccd5aeb
PZ
10121 if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
10122 1))
be256dc7
PZ
10123 DRM_ERROR("D_COMP RCOMP still in progress\n");
10124
10125 if (allow_power_down) {
10126 val = I915_READ(LCPLL_CTL);
10127 val |= LCPLL_POWER_DOWN_ALLOW;
10128 I915_WRITE(LCPLL_CTL, val);
10129 POSTING_READ(LCPLL_CTL);
10130 }
10131}
10132
10133/*
10134 * Fully restores LCPLL, disallowing power down and switching back to LCPLL
10135 * source.
10136 */
6ff58d53 10137static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
be256dc7
PZ
10138{
10139 uint32_t val;
10140
10141 val = I915_READ(LCPLL_CTL);
10142
10143 if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
10144 LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
10145 return;
10146
a8a8bd54
PZ
10147 /*
10148 * Make sure we're not on PC8 state before disabling PC8, otherwise
10149 * we'll hang the machine. To prevent PC8 state, just enable force_wake.
a8a8bd54 10150 */
59bad947 10151 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
215733fa 10152
be256dc7
PZ
10153 if (val & LCPLL_POWER_DOWN_ALLOW) {
10154 val &= ~LCPLL_POWER_DOWN_ALLOW;
10155 I915_WRITE(LCPLL_CTL, val);
35d8f2eb 10156 POSTING_READ(LCPLL_CTL);
be256dc7
PZ
10157 }
10158
9ccd5aeb 10159 val = hsw_read_dcomp(dev_priv);
be256dc7
PZ
10160 val |= D_COMP_COMP_FORCE;
10161 val &= ~D_COMP_COMP_DISABLE;
3c4c9b81 10162 hsw_write_dcomp(dev_priv, val);
be256dc7
PZ
10163
10164 val = I915_READ(LCPLL_CTL);
10165 val &= ~LCPLL_PLL_DISABLE;
10166 I915_WRITE(LCPLL_CTL, val);
10167
93220c08
CW
10168 if (intel_wait_for_register(dev_priv,
10169 LCPLL_CTL, LCPLL_PLL_LOCK, LCPLL_PLL_LOCK,
10170 5))
be256dc7
PZ
10171 DRM_ERROR("LCPLL not locked yet\n");
10172
10173 if (val & LCPLL_CD_SOURCE_FCLK) {
10174 val = I915_READ(LCPLL_CTL);
10175 val &= ~LCPLL_CD_SOURCE_FCLK;
10176 I915_WRITE(LCPLL_CTL, val);
10177
f53dd63f
ID
10178 if (wait_for_us((I915_READ(LCPLL_CTL) &
10179 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
be256dc7
PZ
10180 DRM_ERROR("Switching back to LCPLL failed\n");
10181 }
215733fa 10182
59bad947 10183 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
4c75b940 10184 intel_update_cdclk(dev_priv);
be256dc7
PZ
10185}
10186
765dab67
PZ
10187/*
10188 * Package states C8 and deeper are really deep PC states that can only be
10189 * reached when all the devices on the system allow it, so even if the graphics
10190 * device allows PC8+, it doesn't mean the system will actually get to these
10191 * states. Our driver only allows PC8+ when going into runtime PM.
10192 *
10193 * The requirements for PC8+ are that all the outputs are disabled, the power
10194 * well is disabled and most interrupts are disabled, and these are also
10195 * requirements for runtime PM. When these conditions are met, we manually do
10196 * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
10197 * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
10198 * hang the machine.
10199 *
10200 * When we really reach PC8 or deeper states (not just when we allow it) we lose
10201 * the state of some registers, so when we come back from PC8+ we need to
10202 * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
10203 * need to take care of the registers kept by RC6. Notice that this happens even
10204 * if we don't put the device in PCI D3 state (which is what currently happens
10205 * because of the runtime PM support).
10206 *
10207 * For more, read "Display Sequences for Package C8" on the hardware
10208 * documentation.
10209 */
a14cb6fc 10210void hsw_enable_pc8(struct drm_i915_private *dev_priv)
c67a470b 10211{
91c8a326 10212 struct drm_device *dev = &dev_priv->drm;
c67a470b
PZ
10213 uint32_t val;
10214
c67a470b
PZ
10215 DRM_DEBUG_KMS("Enabling package C8+\n");
10216
4f8036a2 10217 if (HAS_PCH_LPT_LP(dev_priv)) {
c67a470b
PZ
10218 val = I915_READ(SOUTH_DSPCLK_GATE_D);
10219 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
10220 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
10221 }
10222
10223 lpt_disable_clkout_dp(dev);
c67a470b
PZ
10224 hsw_disable_lcpll(dev_priv, true, true);
10225}
10226
a14cb6fc 10227void hsw_disable_pc8(struct drm_i915_private *dev_priv)
c67a470b 10228{
91c8a326 10229 struct drm_device *dev = &dev_priv->drm;
c67a470b
PZ
10230 uint32_t val;
10231
c67a470b
PZ
10232 DRM_DEBUG_KMS("Disabling package C8+\n");
10233
10234 hsw_restore_lcpll(dev_priv);
c67a470b
PZ
10235 lpt_init_pch_refclk(dev);
10236
4f8036a2 10237 if (HAS_PCH_LPT_LP(dev_priv)) {
c67a470b
PZ
10238 val = I915_READ(SOUTH_DSPCLK_GATE_D);
10239 val |= PCH_LP_PARTITION_LEVEL_DISABLE;
10240 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
10241 }
c67a470b
PZ
10242}
10243
324513c0 10244static void bxt_modeset_commit_cdclk(struct drm_atomic_state *old_state)
f8437dd1 10245{
a821fc46 10246 struct drm_device *dev = old_state->dev;
1a617b77
ML
10247 struct intel_atomic_state *old_intel_state =
10248 to_intel_atomic_state(old_state);
10249 unsigned int req_cdclk = old_intel_state->dev_cdclk;
f8437dd1 10250
324513c0 10251 bxt_set_cdclk(to_i915(dev), req_cdclk);
f8437dd1
VK
10252}
10253
b30ce9e0
DP
10254static int bdw_adjust_min_pipe_pixel_rate(struct intel_crtc_state *crtc_state,
10255 int pixel_rate)
10256{
9c754024
DP
10257 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
10258
b30ce9e0 10259 /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
9c754024 10260 if (IS_BROADWELL(dev_priv) && crtc_state->ips_enabled)
b30ce9e0
DP
10261 pixel_rate = DIV_ROUND_UP(pixel_rate * 100, 95);
10262
10263 /* BSpec says "Do not use DisplayPort with CDCLK less than
10264 * 432 MHz, audio enabled, port width x4, and link rate
10265 * HBR2 (5.4 GHz), or else there may be audio corruption or
10266 * screen corruption."
10267 */
10268 if (intel_crtc_has_dp_encoder(crtc_state) &&
10269 crtc_state->has_audio &&
10270 crtc_state->port_clock >= 540000 &&
10271 crtc_state->lane_count == 4)
10272 pixel_rate = max(432000, pixel_rate);
10273
10274 return pixel_rate;
10275}
10276
b432e5cf 10277/* compute the max rate for new configuration */
27c329ed 10278static int ilk_max_pixel_rate(struct drm_atomic_state *state)
b432e5cf 10279{
565602d7 10280 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
fac5e23e 10281 struct drm_i915_private *dev_priv = to_i915(state->dev);
565602d7
ML
10282 struct drm_crtc *crtc;
10283 struct drm_crtc_state *cstate;
27c329ed 10284 struct intel_crtc_state *crtc_state;
565602d7
ML
10285 unsigned max_pixel_rate = 0, i;
10286 enum pipe pipe;
b432e5cf 10287
565602d7
ML
10288 memcpy(intel_state->min_pixclk, dev_priv->min_pixclk,
10289 sizeof(intel_state->min_pixclk));
27c329ed 10290
565602d7
ML
10291 for_each_crtc_in_state(state, crtc, cstate, i) {
10292 int pixel_rate;
27c329ed 10293
565602d7
ML
10294 crtc_state = to_intel_crtc_state(cstate);
10295 if (!crtc_state->base.enable) {
10296 intel_state->min_pixclk[i] = 0;
b432e5cf 10297 continue;
565602d7 10298 }
b432e5cf 10299
27c329ed 10300 pixel_rate = ilk_pipe_pixel_rate(crtc_state);
b432e5cf 10301
9c754024 10302 if (IS_BROADWELL(dev_priv) || IS_GEN9(dev_priv))
b30ce9e0
DP
10303 pixel_rate = bdw_adjust_min_pipe_pixel_rate(crtc_state,
10304 pixel_rate);
b432e5cf 10305
565602d7 10306 intel_state->min_pixclk[i] = pixel_rate;
b432e5cf
VS
10307 }
10308
565602d7
ML
10309 for_each_pipe(dev_priv, pipe)
10310 max_pixel_rate = max(intel_state->min_pixclk[pipe], max_pixel_rate);
10311
b432e5cf
VS
10312 return max_pixel_rate;
10313}
10314
10315static void broadwell_set_cdclk(struct drm_device *dev, int cdclk)
10316{
fac5e23e 10317 struct drm_i915_private *dev_priv = to_i915(dev);
b432e5cf
VS
10318 uint32_t val, data;
10319 int ret;
10320
10321 if (WARN((I915_READ(LCPLL_CTL) &
10322 (LCPLL_PLL_DISABLE | LCPLL_PLL_LOCK |
10323 LCPLL_CD_CLOCK_DISABLE | LCPLL_ROOT_CD_CLOCK_DISABLE |
10324 LCPLL_CD2X_CLOCK_DISABLE | LCPLL_POWER_DOWN_ALLOW |
10325 LCPLL_CD_SOURCE_FCLK)) != LCPLL_PLL_LOCK,
10326 "trying to change cdclk frequency with cdclk not enabled\n"))
10327 return;
10328
10329 mutex_lock(&dev_priv->rps.hw_lock);
10330 ret = sandybridge_pcode_write(dev_priv,
10331 BDW_PCODE_DISPLAY_FREQ_CHANGE_REQ, 0x0);
10332 mutex_unlock(&dev_priv->rps.hw_lock);
10333 if (ret) {
10334 DRM_ERROR("failed to inform pcode about cdclk change\n");
10335 return;
10336 }
10337
10338 val = I915_READ(LCPLL_CTL);
10339 val |= LCPLL_CD_SOURCE_FCLK;
10340 I915_WRITE(LCPLL_CTL, val);
10341
5ba00178
TU
10342 if (wait_for_us(I915_READ(LCPLL_CTL) &
10343 LCPLL_CD_SOURCE_FCLK_DONE, 1))
b432e5cf
VS
10344 DRM_ERROR("Switching to FCLK failed\n");
10345
10346 val = I915_READ(LCPLL_CTL);
10347 val &= ~LCPLL_CLK_FREQ_MASK;
10348
10349 switch (cdclk) {
10350 case 450000:
10351 val |= LCPLL_CLK_FREQ_450;
10352 data = 0;
10353 break;
10354 case 540000:
10355 val |= LCPLL_CLK_FREQ_54O_BDW;
10356 data = 1;
10357 break;
10358 case 337500:
10359 val |= LCPLL_CLK_FREQ_337_5_BDW;
10360 data = 2;
10361 break;
10362 case 675000:
10363 val |= LCPLL_CLK_FREQ_675_BDW;
10364 data = 3;
10365 break;
10366 default:
10367 WARN(1, "invalid cdclk frequency\n");
10368 return;
10369 }
10370
10371 I915_WRITE(LCPLL_CTL, val);
10372
10373 val = I915_READ(LCPLL_CTL);
10374 val &= ~LCPLL_CD_SOURCE_FCLK;
10375 I915_WRITE(LCPLL_CTL, val);
10376
5ba00178
TU
10377 if (wait_for_us((I915_READ(LCPLL_CTL) &
10378 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
b432e5cf
VS
10379 DRM_ERROR("Switching back to LCPLL failed\n");
10380
10381 mutex_lock(&dev_priv->rps.hw_lock);
10382 sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ, data);
10383 mutex_unlock(&dev_priv->rps.hw_lock);
10384
7f1052a8
VS
10385 I915_WRITE(CDCLK_FREQ, DIV_ROUND_CLOSEST(cdclk, 1000) - 1);
10386
4c75b940 10387 intel_update_cdclk(dev_priv);
b432e5cf
VS
10388
10389 WARN(cdclk != dev_priv->cdclk_freq,
10390 "cdclk requested %d kHz but got %d kHz\n",
10391 cdclk, dev_priv->cdclk_freq);
10392}
10393
587c7914
VS
10394static int broadwell_calc_cdclk(int max_pixclk)
10395{
10396 if (max_pixclk > 540000)
10397 return 675000;
10398 else if (max_pixclk > 450000)
10399 return 540000;
10400 else if (max_pixclk > 337500)
10401 return 450000;
10402 else
10403 return 337500;
10404}
10405
27c329ed 10406static int broadwell_modeset_calc_cdclk(struct drm_atomic_state *state)
b432e5cf 10407{
27c329ed 10408 struct drm_i915_private *dev_priv = to_i915(state->dev);
1a617b77 10409 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
27c329ed 10410 int max_pixclk = ilk_max_pixel_rate(state);
b432e5cf
VS
10411 int cdclk;
10412
10413 /*
10414 * FIXME should also account for plane ratio
10415 * once 64bpp pixel formats are supported.
10416 */
587c7914 10417 cdclk = broadwell_calc_cdclk(max_pixclk);
b432e5cf 10418
b432e5cf 10419 if (cdclk > dev_priv->max_cdclk_freq) {
63ba534e
ML
10420 DRM_DEBUG_KMS("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
10421 cdclk, dev_priv->max_cdclk_freq);
10422 return -EINVAL;
b432e5cf
VS
10423 }
10424
1a617b77
ML
10425 intel_state->cdclk = intel_state->dev_cdclk = cdclk;
10426 if (!intel_state->active_crtcs)
587c7914 10427 intel_state->dev_cdclk = broadwell_calc_cdclk(0);
b432e5cf
VS
10428
10429 return 0;
10430}
10431
27c329ed 10432static void broadwell_modeset_commit_cdclk(struct drm_atomic_state *old_state)
b432e5cf 10433{
27c329ed 10434 struct drm_device *dev = old_state->dev;
1a617b77
ML
10435 struct intel_atomic_state *old_intel_state =
10436 to_intel_atomic_state(old_state);
10437 unsigned req_cdclk = old_intel_state->dev_cdclk;
b432e5cf 10438
27c329ed 10439 broadwell_set_cdclk(dev, req_cdclk);
b432e5cf
VS
10440}
10441
c89e39f3
CT
10442static int skl_modeset_calc_cdclk(struct drm_atomic_state *state)
10443{
10444 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
10445 struct drm_i915_private *dev_priv = to_i915(state->dev);
10446 const int max_pixclk = ilk_max_pixel_rate(state);
a8ca4934 10447 int vco = intel_state->cdclk_pll_vco;
c89e39f3
CT
10448 int cdclk;
10449
10450 /*
10451 * FIXME should also account for plane ratio
10452 * once 64bpp pixel formats are supported.
10453 */
a8ca4934 10454 cdclk = skl_calc_cdclk(max_pixclk, vco);
c89e39f3
CT
10455
10456 /*
10457 * FIXME move the cdclk caclulation to
10458 * compute_config() so we can fail gracegully.
10459 */
10460 if (cdclk > dev_priv->max_cdclk_freq) {
10461 DRM_ERROR("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
10462 cdclk, dev_priv->max_cdclk_freq);
10463 cdclk = dev_priv->max_cdclk_freq;
10464 }
10465
10466 intel_state->cdclk = intel_state->dev_cdclk = cdclk;
10467 if (!intel_state->active_crtcs)
a8ca4934 10468 intel_state->dev_cdclk = skl_calc_cdclk(0, vco);
c89e39f3
CT
10469
10470 return 0;
10471}
10472
10473static void skl_modeset_commit_cdclk(struct drm_atomic_state *old_state)
10474{
1cd593e0
VS
10475 struct drm_i915_private *dev_priv = to_i915(old_state->dev);
10476 struct intel_atomic_state *intel_state = to_intel_atomic_state(old_state);
10477 unsigned int req_cdclk = intel_state->dev_cdclk;
10478 unsigned int req_vco = intel_state->cdclk_pll_vco;
c89e39f3 10479
1cd593e0 10480 skl_set_cdclk(dev_priv, req_cdclk, req_vco);
c89e39f3
CT
10481}
10482
190f68c5
ACO
10483static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
10484 struct intel_crtc_state *crtc_state)
09b4ddf9 10485{
d7edc4e5 10486 if (!intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DSI)) {
af3997b5
MK
10487 if (!intel_ddi_pll_select(crtc, crtc_state))
10488 return -EINVAL;
10489 }
716c2e55 10490
c7653199 10491 crtc->lowfreq_avail = false;
644cef34 10492
c8f7a0db 10493 return 0;
79e53945
JB
10494}
10495
3760b59c
S
10496static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
10497 enum port port,
10498 struct intel_crtc_state *pipe_config)
10499{
8106ddbd
ACO
10500 enum intel_dpll_id id;
10501
3760b59c
S
10502 switch (port) {
10503 case PORT_A:
08250c4b 10504 id = DPLL_ID_SKL_DPLL0;
3760b59c
S
10505 break;
10506 case PORT_B:
08250c4b 10507 id = DPLL_ID_SKL_DPLL1;
3760b59c
S
10508 break;
10509 case PORT_C:
08250c4b 10510 id = DPLL_ID_SKL_DPLL2;
3760b59c
S
10511 break;
10512 default:
10513 DRM_ERROR("Incorrect port type\n");
8106ddbd 10514 return;
3760b59c 10515 }
8106ddbd
ACO
10516
10517 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
3760b59c
S
10518}
10519
96b7dfb7
S
10520static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
10521 enum port port,
5cec258b 10522 struct intel_crtc_state *pipe_config)
96b7dfb7 10523{
8106ddbd 10524 enum intel_dpll_id id;
a3c988ea 10525 u32 temp;
96b7dfb7
S
10526
10527 temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
c856052a 10528 id = temp >> (port * 3 + 1);
96b7dfb7 10529
c856052a 10530 if (WARN_ON(id < SKL_DPLL0 || id > SKL_DPLL3))
8106ddbd 10531 return;
8106ddbd
ACO
10532
10533 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
96b7dfb7
S
10534}
10535
7d2c8175
DL
10536static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
10537 enum port port,
5cec258b 10538 struct intel_crtc_state *pipe_config)
7d2c8175 10539{
8106ddbd 10540 enum intel_dpll_id id;
c856052a 10541 uint32_t ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
8106ddbd 10542
c856052a 10543 switch (ddi_pll_sel) {
7d2c8175 10544 case PORT_CLK_SEL_WRPLL1:
8106ddbd 10545 id = DPLL_ID_WRPLL1;
7d2c8175
DL
10546 break;
10547 case PORT_CLK_SEL_WRPLL2:
8106ddbd 10548 id = DPLL_ID_WRPLL2;
7d2c8175 10549 break;
00490c22 10550 case PORT_CLK_SEL_SPLL:
8106ddbd 10551 id = DPLL_ID_SPLL;
79bd23da 10552 break;
9d16da65
ACO
10553 case PORT_CLK_SEL_LCPLL_810:
10554 id = DPLL_ID_LCPLL_810;
10555 break;
10556 case PORT_CLK_SEL_LCPLL_1350:
10557 id = DPLL_ID_LCPLL_1350;
10558 break;
10559 case PORT_CLK_SEL_LCPLL_2700:
10560 id = DPLL_ID_LCPLL_2700;
10561 break;
8106ddbd 10562 default:
c856052a 10563 MISSING_CASE(ddi_pll_sel);
8106ddbd
ACO
10564 /* fall through */
10565 case PORT_CLK_SEL_NONE:
8106ddbd 10566 return;
7d2c8175 10567 }
8106ddbd
ACO
10568
10569 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
7d2c8175
DL
10570}
10571
cf30429e
JN
10572static bool hsw_get_transcoder_state(struct intel_crtc *crtc,
10573 struct intel_crtc_state *pipe_config,
10574 unsigned long *power_domain_mask)
10575{
10576 struct drm_device *dev = crtc->base.dev;
fac5e23e 10577 struct drm_i915_private *dev_priv = to_i915(dev);
cf30429e
JN
10578 enum intel_display_power_domain power_domain;
10579 u32 tmp;
10580
d9a7bc67
ID
10581 /*
10582 * The pipe->transcoder mapping is fixed with the exception of the eDP
10583 * transcoder handled below.
10584 */
cf30429e
JN
10585 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
10586
10587 /*
10588 * XXX: Do intel_display_power_get_if_enabled before reading this (for
10589 * consistency and less surprising code; it's in always on power).
10590 */
10591 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
10592 if (tmp & TRANS_DDI_FUNC_ENABLE) {
10593 enum pipe trans_edp_pipe;
10594 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
10595 default:
10596 WARN(1, "unknown pipe linked to edp transcoder\n");
10597 case TRANS_DDI_EDP_INPUT_A_ONOFF:
10598 case TRANS_DDI_EDP_INPUT_A_ON:
10599 trans_edp_pipe = PIPE_A;
10600 break;
10601 case TRANS_DDI_EDP_INPUT_B_ONOFF:
10602 trans_edp_pipe = PIPE_B;
10603 break;
10604 case TRANS_DDI_EDP_INPUT_C_ONOFF:
10605 trans_edp_pipe = PIPE_C;
10606 break;
10607 }
10608
10609 if (trans_edp_pipe == crtc->pipe)
10610 pipe_config->cpu_transcoder = TRANSCODER_EDP;
10611 }
10612
10613 power_domain = POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder);
10614 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
10615 return false;
10616 *power_domain_mask |= BIT(power_domain);
10617
10618 tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
10619
10620 return tmp & PIPECONF_ENABLE;
10621}
10622
4d1de975
JN
10623static bool bxt_get_dsi_transcoder_state(struct intel_crtc *crtc,
10624 struct intel_crtc_state *pipe_config,
10625 unsigned long *power_domain_mask)
10626{
10627 struct drm_device *dev = crtc->base.dev;
fac5e23e 10628 struct drm_i915_private *dev_priv = to_i915(dev);
4d1de975
JN
10629 enum intel_display_power_domain power_domain;
10630 enum port port;
10631 enum transcoder cpu_transcoder;
10632 u32 tmp;
10633
4d1de975
JN
10634 for_each_port_masked(port, BIT(PORT_A) | BIT(PORT_C)) {
10635 if (port == PORT_A)
10636 cpu_transcoder = TRANSCODER_DSI_A;
10637 else
10638 cpu_transcoder = TRANSCODER_DSI_C;
10639
10640 power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
10641 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
10642 continue;
10643 *power_domain_mask |= BIT(power_domain);
10644
db18b6a6
ID
10645 /*
10646 * The PLL needs to be enabled with a valid divider
10647 * configuration, otherwise accessing DSI registers will hang
10648 * the machine. See BSpec North Display Engine
10649 * registers/MIPI[BXT]. We can break out here early, since we
10650 * need the same DSI PLL to be enabled for both DSI ports.
10651 */
10652 if (!intel_dsi_pll_is_enabled(dev_priv))
10653 break;
10654
4d1de975
JN
10655 /* XXX: this works for video mode only */
10656 tmp = I915_READ(BXT_MIPI_PORT_CTRL(port));
10657 if (!(tmp & DPI_ENABLE))
10658 continue;
10659
10660 tmp = I915_READ(MIPI_CTRL(port));
10661 if ((tmp & BXT_PIPE_SELECT_MASK) != BXT_PIPE_SELECT(crtc->pipe))
10662 continue;
10663
10664 pipe_config->cpu_transcoder = cpu_transcoder;
4d1de975
JN
10665 break;
10666 }
10667
d7edc4e5 10668 return transcoder_is_dsi(pipe_config->cpu_transcoder);
4d1de975
JN
10669}
10670
26804afd 10671static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
5cec258b 10672 struct intel_crtc_state *pipe_config)
26804afd
SV
10673{
10674 struct drm_device *dev = crtc->base.dev;
fac5e23e 10675 struct drm_i915_private *dev_priv = to_i915(dev);
d452c5b6 10676 struct intel_shared_dpll *pll;
26804afd
SV
10677 enum port port;
10678 uint32_t tmp;
10679
10680 tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
10681
10682 port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
10683
0853723b 10684 if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv))
96b7dfb7 10685 skylake_get_ddi_pll(dev_priv, port, pipe_config);
e2d214ae 10686 else if (IS_BROXTON(dev_priv))
3760b59c 10687 bxt_get_ddi_pll(dev_priv, port, pipe_config);
96b7dfb7
S
10688 else
10689 haswell_get_ddi_pll(dev_priv, port, pipe_config);
9cd86933 10690
8106ddbd
ACO
10691 pll = pipe_config->shared_dpll;
10692 if (pll) {
2edd6443
ACO
10693 WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
10694 &pipe_config->dpll_hw_state));
d452c5b6
SV
10695 }
10696
26804afd
SV
10697 /*
10698 * Haswell has only FDI/PCH transcoder A. It is which is connected to
10699 * DDI E. So just check whether this pipe is wired to DDI E and whether
10700 * the PCH transcoder is on.
10701 */
ca370455
DL
10702 if (INTEL_INFO(dev)->gen < 9 &&
10703 (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
26804afd
SV
10704 pipe_config->has_pch_encoder = true;
10705
10706 tmp = I915_READ(FDI_RX_CTL(PIPE_A));
10707 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
10708 FDI_DP_PORT_WIDTH_SHIFT) + 1;
10709
10710 ironlake_get_fdi_m_n_config(crtc, pipe_config);
10711 }
10712}
10713
0e8ffe1b 10714static bool haswell_get_pipe_config(struct intel_crtc *crtc,
5cec258b 10715 struct intel_crtc_state *pipe_config)
0e8ffe1b
SV
10716{
10717 struct drm_device *dev = crtc->base.dev;
fac5e23e 10718 struct drm_i915_private *dev_priv = to_i915(dev);
1729050e
ID
10719 enum intel_display_power_domain power_domain;
10720 unsigned long power_domain_mask;
cf30429e 10721 bool active;
0e8ffe1b 10722
1729050e
ID
10723 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
10724 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
b5482bd0 10725 return false;
1729050e
ID
10726 power_domain_mask = BIT(power_domain);
10727
8106ddbd 10728 pipe_config->shared_dpll = NULL;
c0d43d62 10729
cf30429e 10730 active = hsw_get_transcoder_state(crtc, pipe_config, &power_domain_mask);
eccb140b 10731
d7edc4e5
VS
10732 if (IS_BROXTON(dev_priv) &&
10733 bxt_get_dsi_transcoder_state(crtc, pipe_config, &power_domain_mask)) {
10734 WARN_ON(active);
10735 active = true;
4d1de975
JN
10736 }
10737
cf30429e 10738 if (!active)
1729050e 10739 goto out;
0e8ffe1b 10740
d7edc4e5 10741 if (!transcoder_is_dsi(pipe_config->cpu_transcoder)) {
4d1de975
JN
10742 haswell_get_ddi_port_state(crtc, pipe_config);
10743 intel_get_pipe_timings(crtc, pipe_config);
10744 }
627eb5a3 10745
bc58be60 10746 intel_get_pipe_src_size(crtc, pipe_config);
1bd1bd80 10747
05dc698c
LL
10748 pipe_config->gamma_mode =
10749 I915_READ(GAMMA_MODE(crtc->pipe)) & GAMMA_MODE_MODE_MASK;
10750
a1b2278e 10751 if (INTEL_INFO(dev)->gen >= 9) {
65edccce 10752 skl_init_scalers(dev_priv, crtc, pipe_config);
a1b2278e 10753
af99ceda
CK
10754 pipe_config->scaler_state.scaler_id = -1;
10755 pipe_config->scaler_state.scaler_users &= ~(1 << SKL_CRTC_INDEX);
10756 }
10757
1729050e
ID
10758 power_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
10759 if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
10760 power_domain_mask |= BIT(power_domain);
1c132b44 10761 if (INTEL_INFO(dev)->gen >= 9)
bd2e244f 10762 skylake_get_pfit_config(crtc, pipe_config);
ff6d9f55 10763 else
1c132b44 10764 ironlake_get_pfit_config(crtc, pipe_config);
bd2e244f 10765 }
88adfff1 10766
772c2a51 10767 if (IS_HASWELL(dev_priv))
e59150dc
JB
10768 pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
10769 (I915_READ(IPS_CTL) & IPS_ENABLE);
42db64ef 10770
4d1de975
JN
10771 if (pipe_config->cpu_transcoder != TRANSCODER_EDP &&
10772 !transcoder_is_dsi(pipe_config->cpu_transcoder)) {
ebb69c95
CT
10773 pipe_config->pixel_multiplier =
10774 I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
10775 } else {
10776 pipe_config->pixel_multiplier = 1;
10777 }
6c49f241 10778
1729050e
ID
10779out:
10780 for_each_power_domain(power_domain, power_domain_mask)
10781 intel_display_power_put(dev_priv, power_domain);
10782
cf30429e 10783 return active;
0e8ffe1b
SV
10784}
10785
55a08b3f
ML
10786static void i845_update_cursor(struct drm_crtc *crtc, u32 base,
10787 const struct intel_plane_state *plane_state)
560b85bb
CW
10788{
10789 struct drm_device *dev = crtc->dev;
fac5e23e 10790 struct drm_i915_private *dev_priv = to_i915(dev);
560b85bb 10791 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
dc41c154 10792 uint32_t cntl = 0, size = 0;
560b85bb 10793
936e71e3 10794 if (plane_state && plane_state->base.visible) {
55a08b3f
ML
10795 unsigned int width = plane_state->base.crtc_w;
10796 unsigned int height = plane_state->base.crtc_h;
dc41c154
VS
10797 unsigned int stride = roundup_pow_of_two(width) * 4;
10798
10799 switch (stride) {
10800 default:
10801 WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
10802 width, stride);
10803 stride = 256;
10804 /* fallthrough */
10805 case 256:
10806 case 512:
10807 case 1024:
10808 case 2048:
10809 break;
4b0e333e
CW
10810 }
10811
dc41c154
VS
10812 cntl |= CURSOR_ENABLE |
10813 CURSOR_GAMMA_ENABLE |
10814 CURSOR_FORMAT_ARGB |
10815 CURSOR_STRIDE(stride);
10816
10817 size = (height << 12) | width;
4b0e333e 10818 }
560b85bb 10819
dc41c154
VS
10820 if (intel_crtc->cursor_cntl != 0 &&
10821 (intel_crtc->cursor_base != base ||
10822 intel_crtc->cursor_size != size ||
10823 intel_crtc->cursor_cntl != cntl)) {
10824 /* On these chipsets we can only modify the base/size/stride
10825 * whilst the cursor is disabled.
10826 */
0b87c24e
VS
10827 I915_WRITE(CURCNTR(PIPE_A), 0);
10828 POSTING_READ(CURCNTR(PIPE_A));
dc41c154 10829 intel_crtc->cursor_cntl = 0;
4b0e333e 10830 }
560b85bb 10831
99d1f387 10832 if (intel_crtc->cursor_base != base) {
0b87c24e 10833 I915_WRITE(CURBASE(PIPE_A), base);
99d1f387
VS
10834 intel_crtc->cursor_base = base;
10835 }
4726e0b0 10836
dc41c154
VS
10837 if (intel_crtc->cursor_size != size) {
10838 I915_WRITE(CURSIZE, size);
10839 intel_crtc->cursor_size = size;
4b0e333e 10840 }
560b85bb 10841
4b0e333e 10842 if (intel_crtc->cursor_cntl != cntl) {
0b87c24e
VS
10843 I915_WRITE(CURCNTR(PIPE_A), cntl);
10844 POSTING_READ(CURCNTR(PIPE_A));
4b0e333e 10845 intel_crtc->cursor_cntl = cntl;
560b85bb 10846 }
560b85bb
CW
10847}
10848
55a08b3f
ML
10849static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base,
10850 const struct intel_plane_state *plane_state)
65a21cd6
JB
10851{
10852 struct drm_device *dev = crtc->dev;
fac5e23e 10853 struct drm_i915_private *dev_priv = to_i915(dev);
65a21cd6 10854 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
d8c0fafc 10855 struct intel_crtc_state *cstate = to_intel_crtc_state(crtc->state);
62e0fb88 10856 const struct skl_wm_values *wm = &dev_priv->wm.skl_results;
d8c0fafc 10857 const struct skl_plane_wm *p_wm =
10858 &cstate->wm.skl.optimal.planes[PLANE_CURSOR];
65a21cd6 10859 int pipe = intel_crtc->pipe;
663f3122 10860 uint32_t cntl = 0;
4b0e333e 10861
62e0fb88 10862 if (INTEL_GEN(dev_priv) >= 9 && wm->dirty_pipes & drm_crtc_mask(crtc))
d8c0fafc 10863 skl_write_cursor_wm(intel_crtc, p_wm, &wm->ddb);
62e0fb88 10864
936e71e3 10865 if (plane_state && plane_state->base.visible) {
4b0e333e 10866 cntl = MCURSOR_GAMMA_ENABLE;
55a08b3f 10867 switch (plane_state->base.crtc_w) {
4726e0b0
SK
10868 case 64:
10869 cntl |= CURSOR_MODE_64_ARGB_AX;
10870 break;
10871 case 128:
10872 cntl |= CURSOR_MODE_128_ARGB_AX;
10873 break;
10874 case 256:
10875 cntl |= CURSOR_MODE_256_ARGB_AX;
10876 break;
10877 default:
55a08b3f 10878 MISSING_CASE(plane_state->base.crtc_w);
4726e0b0 10879 return;
65a21cd6 10880 }
4b0e333e 10881 cntl |= pipe << 28; /* Connect to correct pipe */
47bf17a7 10882
4f8036a2 10883 if (HAS_DDI(dev_priv))
47bf17a7 10884 cntl |= CURSOR_PIPE_CSC_ENABLE;
65a21cd6 10885
f22aa143 10886 if (plane_state->base.rotation & DRM_ROTATE_180)
55a08b3f
ML
10887 cntl |= CURSOR_ROTATE_180;
10888 }
4398ad45 10889
4b0e333e
CW
10890 if (intel_crtc->cursor_cntl != cntl) {
10891 I915_WRITE(CURCNTR(pipe), cntl);
10892 POSTING_READ(CURCNTR(pipe));
10893 intel_crtc->cursor_cntl = cntl;
65a21cd6 10894 }
4b0e333e 10895
65a21cd6 10896 /* and commit changes on next vblank */
5efb3e28
VS
10897 I915_WRITE(CURBASE(pipe), base);
10898 POSTING_READ(CURBASE(pipe));
99d1f387
VS
10899
10900 intel_crtc->cursor_base = base;
65a21cd6
JB
10901}
10902
cda4b7d3 10903/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
6b383a7f 10904static void intel_crtc_update_cursor(struct drm_crtc *crtc,
55a08b3f 10905 const struct intel_plane_state *plane_state)
cda4b7d3
CW
10906{
10907 struct drm_device *dev = crtc->dev;
fac5e23e 10908 struct drm_i915_private *dev_priv = to_i915(dev);
cda4b7d3
CW
10909 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10910 int pipe = intel_crtc->pipe;
55a08b3f
ML
10911 u32 base = intel_crtc->cursor_addr;
10912 u32 pos = 0;
cda4b7d3 10913
55a08b3f
ML
10914 if (plane_state) {
10915 int x = plane_state->base.crtc_x;
10916 int y = plane_state->base.crtc_y;
cda4b7d3 10917
55a08b3f
ML
10918 if (x < 0) {
10919 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
10920 x = -x;
10921 }
10922 pos |= x << CURSOR_X_SHIFT;
cda4b7d3 10923
55a08b3f
ML
10924 if (y < 0) {
10925 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
10926 y = -y;
10927 }
10928 pos |= y << CURSOR_Y_SHIFT;
10929
10930 /* ILK+ do this automagically */
49cff963 10931 if (HAS_GMCH_DISPLAY(dev_priv) &&
f22aa143 10932 plane_state->base.rotation & DRM_ROTATE_180) {
55a08b3f
ML
10933 base += (plane_state->base.crtc_h *
10934 plane_state->base.crtc_w - 1) * 4;
10935 }
cda4b7d3 10936 }
cda4b7d3 10937
5efb3e28
VS
10938 I915_WRITE(CURPOS(pipe), pos);
10939
50a0bc90 10940 if (IS_845G(dev_priv) || IS_I865G(dev_priv))
55a08b3f 10941 i845_update_cursor(crtc, base, plane_state);
5efb3e28 10942 else
55a08b3f 10943 i9xx_update_cursor(crtc, base, plane_state);
cda4b7d3
CW
10944}
10945
50a0bc90 10946static bool cursor_size_ok(struct drm_i915_private *dev_priv,
dc41c154
VS
10947 uint32_t width, uint32_t height)
10948{
10949 if (width == 0 || height == 0)
10950 return false;
10951
10952 /*
10953 * 845g/865g are special in that they are only limited by
10954 * the width of their cursors, the height is arbitrary up to
10955 * the precision of the register. Everything else requires
10956 * square cursors, limited to a few power-of-two sizes.
10957 */
50a0bc90 10958 if (IS_845G(dev_priv) || IS_I865G(dev_priv)) {
dc41c154
VS
10959 if ((width & 63) != 0)
10960 return false;
10961
50a0bc90 10962 if (width > (IS_845G(dev_priv) ? 64 : 512))
dc41c154
VS
10963 return false;
10964
10965 if (height > 1023)
10966 return false;
10967 } else {
10968 switch (width | height) {
10969 case 256:
10970 case 128:
50a0bc90 10971 if (IS_GEN2(dev_priv))
dc41c154
VS
10972 return false;
10973 case 64:
10974 break;
10975 default:
10976 return false;
10977 }
10978 }
10979
10980 return true;
10981}
10982
79e53945
JB
10983/* VESA 640x480x72Hz mode to set on the pipe */
10984static struct drm_display_mode load_detect_mode = {
10985 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
10986 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
10987};
10988
a8bb6818
SV
10989struct drm_framebuffer *
10990__intel_framebuffer_create(struct drm_device *dev,
10991 struct drm_mode_fb_cmd2 *mode_cmd,
10992 struct drm_i915_gem_object *obj)
d2dff872
CW
10993{
10994 struct intel_framebuffer *intel_fb;
10995 int ret;
10996
10997 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
dcb1394e 10998 if (!intel_fb)
d2dff872 10999 return ERR_PTR(-ENOMEM);
d2dff872
CW
11000
11001 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
dd4916c5
SV
11002 if (ret)
11003 goto err;
d2dff872
CW
11004
11005 return &intel_fb->base;
dcb1394e 11006
dd4916c5 11007err:
dd4916c5 11008 kfree(intel_fb);
dd4916c5 11009 return ERR_PTR(ret);
d2dff872
CW
11010}
11011
b5ea642a 11012static struct drm_framebuffer *
a8bb6818
SV
11013intel_framebuffer_create(struct drm_device *dev,
11014 struct drm_mode_fb_cmd2 *mode_cmd,
11015 struct drm_i915_gem_object *obj)
11016{
11017 struct drm_framebuffer *fb;
11018 int ret;
11019
11020 ret = i915_mutex_lock_interruptible(dev);
11021 if (ret)
11022 return ERR_PTR(ret);
11023 fb = __intel_framebuffer_create(dev, mode_cmd, obj);
11024 mutex_unlock(&dev->struct_mutex);
11025
11026 return fb;
11027}
11028
d2dff872
CW
11029static u32
11030intel_framebuffer_pitch_for_width(int width, int bpp)
11031{
11032 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
11033 return ALIGN(pitch, 64);
11034}
11035
11036static u32
11037intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
11038{
11039 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
1267a26b 11040 return PAGE_ALIGN(pitch * mode->vdisplay);
d2dff872
CW
11041}
11042
11043static struct drm_framebuffer *
11044intel_framebuffer_create_for_mode(struct drm_device *dev,
11045 struct drm_display_mode *mode,
11046 int depth, int bpp)
11047{
dcb1394e 11048 struct drm_framebuffer *fb;
d2dff872 11049 struct drm_i915_gem_object *obj;
0fed39bd 11050 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
d2dff872 11051
d37cd8a8 11052 obj = i915_gem_object_create(dev,
d2dff872 11053 intel_framebuffer_size_for_mode(mode, bpp));
fe3db79b
CW
11054 if (IS_ERR(obj))
11055 return ERR_CAST(obj);
d2dff872
CW
11056
11057 mode_cmd.width = mode->hdisplay;
11058 mode_cmd.height = mode->vdisplay;
308e5bcb
JB
11059 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
11060 bpp);
5ca0c34a 11061 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
d2dff872 11062
dcb1394e
LW
11063 fb = intel_framebuffer_create(dev, &mode_cmd, obj);
11064 if (IS_ERR(fb))
f0cd5182 11065 i915_gem_object_put(obj);
dcb1394e
LW
11066
11067 return fb;
d2dff872
CW
11068}
11069
11070static struct drm_framebuffer *
11071mode_fits_in_fbdev(struct drm_device *dev,
11072 struct drm_display_mode *mode)
11073{
0695726e 11074#ifdef CONFIG_DRM_FBDEV_EMULATION
fac5e23e 11075 struct drm_i915_private *dev_priv = to_i915(dev);
d2dff872
CW
11076 struct drm_i915_gem_object *obj;
11077 struct drm_framebuffer *fb;
11078
4c0e5528 11079 if (!dev_priv->fbdev)
d2dff872
CW
11080 return NULL;
11081
4c0e5528 11082 if (!dev_priv->fbdev->fb)
d2dff872
CW
11083 return NULL;
11084
4c0e5528
SV
11085 obj = dev_priv->fbdev->fb->obj;
11086 BUG_ON(!obj);
11087
8bcd4553 11088 fb = &dev_priv->fbdev->fb->base;
01f2c773
VS
11089 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
11090 fb->bits_per_pixel))
d2dff872
CW
11091 return NULL;
11092
01f2c773 11093 if (obj->base.size < mode->vdisplay * fb->pitches[0])
d2dff872
CW
11094 return NULL;
11095
edde3617 11096 drm_framebuffer_reference(fb);
d2dff872 11097 return fb;
4520f53a
SV
11098#else
11099 return NULL;
11100#endif
d2dff872
CW
11101}
11102
d3a40d1b
ACO
11103static int intel_modeset_setup_plane_state(struct drm_atomic_state *state,
11104 struct drm_crtc *crtc,
11105 struct drm_display_mode *mode,
11106 struct drm_framebuffer *fb,
11107 int x, int y)
11108{
11109 struct drm_plane_state *plane_state;
11110 int hdisplay, vdisplay;
11111 int ret;
11112
11113 plane_state = drm_atomic_get_plane_state(state, crtc->primary);
11114 if (IS_ERR(plane_state))
11115 return PTR_ERR(plane_state);
11116
11117 if (mode)
11118 drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
11119 else
11120 hdisplay = vdisplay = 0;
11121
11122 ret = drm_atomic_set_crtc_for_plane(plane_state, fb ? crtc : NULL);
11123 if (ret)
11124 return ret;
11125 drm_atomic_set_fb_for_plane(plane_state, fb);
11126 plane_state->crtc_x = 0;
11127 plane_state->crtc_y = 0;
11128 plane_state->crtc_w = hdisplay;
11129 plane_state->crtc_h = vdisplay;
11130 plane_state->src_x = x << 16;
11131 plane_state->src_y = y << 16;
11132 plane_state->src_w = hdisplay << 16;
11133 plane_state->src_h = vdisplay << 16;
11134
11135 return 0;
11136}
11137
d2434ab7 11138bool intel_get_load_detect_pipe(struct drm_connector *connector,
7173188d 11139 struct drm_display_mode *mode,
51fd371b
RC
11140 struct intel_load_detect_pipe *old,
11141 struct drm_modeset_acquire_ctx *ctx)
79e53945
JB
11142{
11143 struct intel_crtc *intel_crtc;
d2434ab7
SV
11144 struct intel_encoder *intel_encoder =
11145 intel_attached_encoder(connector);
79e53945 11146 struct drm_crtc *possible_crtc;
4ef69c7a 11147 struct drm_encoder *encoder = &intel_encoder->base;
79e53945
JB
11148 struct drm_crtc *crtc = NULL;
11149 struct drm_device *dev = encoder->dev;
0f0f74bc 11150 struct drm_i915_private *dev_priv = to_i915(dev);
94352cf9 11151 struct drm_framebuffer *fb;
51fd371b 11152 struct drm_mode_config *config = &dev->mode_config;
edde3617 11153 struct drm_atomic_state *state = NULL, *restore_state = NULL;
944b0c76 11154 struct drm_connector_state *connector_state;
4be07317 11155 struct intel_crtc_state *crtc_state;
51fd371b 11156 int ret, i = -1;
79e53945 11157
d2dff872 11158 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
c23cc417 11159 connector->base.id, connector->name,
8e329a03 11160 encoder->base.id, encoder->name);
d2dff872 11161
edde3617
ML
11162 old->restore_state = NULL;
11163
51fd371b
RC
11164retry:
11165 ret = drm_modeset_lock(&config->connection_mutex, ctx);
11166 if (ret)
ad3c558f 11167 goto fail;
6e9f798d 11168
79e53945
JB
11169 /*
11170 * Algorithm gets a little messy:
7a5e4805 11171 *
79e53945
JB
11172 * - if the connector already has an assigned crtc, use it (but make
11173 * sure it's on first)
7a5e4805 11174 *
79e53945
JB
11175 * - try to find the first unused crtc that can drive this connector,
11176 * and use that if we find one
79e53945
JB
11177 */
11178
11179 /* See if we already have a CRTC for this connector */
edde3617
ML
11180 if (connector->state->crtc) {
11181 crtc = connector->state->crtc;
8261b191 11182
51fd371b 11183 ret = drm_modeset_lock(&crtc->mutex, ctx);
4d02e2de 11184 if (ret)
ad3c558f 11185 goto fail;
8261b191
CW
11186
11187 /* Make sure the crtc and connector are running */
edde3617 11188 goto found;
79e53945
JB
11189 }
11190
11191 /* Find an unused one (if possible) */
70e1e0ec 11192 for_each_crtc(dev, possible_crtc) {
79e53945
JB
11193 i++;
11194 if (!(encoder->possible_crtcs & (1 << i)))
11195 continue;
edde3617
ML
11196
11197 ret = drm_modeset_lock(&possible_crtc->mutex, ctx);
11198 if (ret)
11199 goto fail;
11200
11201 if (possible_crtc->state->enable) {
11202 drm_modeset_unlock(&possible_crtc->mutex);
a459249c 11203 continue;
edde3617 11204 }
a459249c
VS
11205
11206 crtc = possible_crtc;
11207 break;
79e53945
JB
11208 }
11209
11210 /*
11211 * If we didn't find an unused CRTC, don't use any.
11212 */
11213 if (!crtc) {
7173188d 11214 DRM_DEBUG_KMS("no pipe available for load-detect\n");
ad3c558f 11215 goto fail;
79e53945
JB
11216 }
11217
edde3617
ML
11218found:
11219 intel_crtc = to_intel_crtc(crtc);
11220
4d02e2de
SV
11221 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
11222 if (ret)
ad3c558f 11223 goto fail;
79e53945 11224
83a57153 11225 state = drm_atomic_state_alloc(dev);
edde3617
ML
11226 restore_state = drm_atomic_state_alloc(dev);
11227 if (!state || !restore_state) {
11228 ret = -ENOMEM;
11229 goto fail;
11230 }
83a57153
ACO
11231
11232 state->acquire_ctx = ctx;
edde3617 11233 restore_state->acquire_ctx = ctx;
83a57153 11234
944b0c76
ACO
11235 connector_state = drm_atomic_get_connector_state(state, connector);
11236 if (IS_ERR(connector_state)) {
11237 ret = PTR_ERR(connector_state);
11238 goto fail;
11239 }
11240
edde3617
ML
11241 ret = drm_atomic_set_crtc_for_connector(connector_state, crtc);
11242 if (ret)
11243 goto fail;
944b0c76 11244
4be07317
ACO
11245 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
11246 if (IS_ERR(crtc_state)) {
11247 ret = PTR_ERR(crtc_state);
11248 goto fail;
11249 }
11250
49d6fa21 11251 crtc_state->base.active = crtc_state->base.enable = true;
4be07317 11252
6492711d
CW
11253 if (!mode)
11254 mode = &load_detect_mode;
79e53945 11255
d2dff872
CW
11256 /* We need a framebuffer large enough to accommodate all accesses
11257 * that the plane may generate whilst we perform load detection.
11258 * We can not rely on the fbcon either being present (we get called
11259 * during its initialisation to detect all boot displays, or it may
11260 * not even exist) or that it is large enough to satisfy the
11261 * requested mode.
11262 */
94352cf9
SV
11263 fb = mode_fits_in_fbdev(dev, mode);
11264 if (fb == NULL) {
d2dff872 11265 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
94352cf9 11266 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
d2dff872
CW
11267 } else
11268 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
94352cf9 11269 if (IS_ERR(fb)) {
d2dff872 11270 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
412b61d8 11271 goto fail;
79e53945 11272 }
79e53945 11273
d3a40d1b
ACO
11274 ret = intel_modeset_setup_plane_state(state, crtc, mode, fb, 0, 0);
11275 if (ret)
11276 goto fail;
11277
edde3617
ML
11278 drm_framebuffer_unreference(fb);
11279
11280 ret = drm_atomic_set_mode_for_crtc(&crtc_state->base, mode);
11281 if (ret)
11282 goto fail;
11283
11284 ret = PTR_ERR_OR_ZERO(drm_atomic_get_connector_state(restore_state, connector));
11285 if (!ret)
11286 ret = PTR_ERR_OR_ZERO(drm_atomic_get_crtc_state(restore_state, crtc));
11287 if (!ret)
11288 ret = PTR_ERR_OR_ZERO(drm_atomic_get_plane_state(restore_state, crtc->primary));
11289 if (ret) {
11290 DRM_DEBUG_KMS("Failed to create a copy of old state to restore: %i\n", ret);
11291 goto fail;
11292 }
8c7b5ccb 11293
3ba86073
ML
11294 ret = drm_atomic_commit(state);
11295 if (ret) {
6492711d 11296 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
412b61d8 11297 goto fail;
79e53945 11298 }
edde3617
ML
11299
11300 old->restore_state = restore_state;
7173188d 11301
79e53945 11302 /* let the connector get through one full cycle before testing */
0f0f74bc 11303 intel_wait_for_vblank(dev_priv, intel_crtc->pipe);
7173188d 11304 return true;
412b61d8 11305
ad3c558f 11306fail:
7fb71c8f
CW
11307 if (state) {
11308 drm_atomic_state_put(state);
11309 state = NULL;
11310 }
11311 if (restore_state) {
11312 drm_atomic_state_put(restore_state);
11313 restore_state = NULL;
11314 }
83a57153 11315
51fd371b
RC
11316 if (ret == -EDEADLK) {
11317 drm_modeset_backoff(ctx);
11318 goto retry;
11319 }
11320
412b61d8 11321 return false;
79e53945
JB
11322}
11323
d2434ab7 11324void intel_release_load_detect_pipe(struct drm_connector *connector,
49172fee
ACO
11325 struct intel_load_detect_pipe *old,
11326 struct drm_modeset_acquire_ctx *ctx)
79e53945 11327{
d2434ab7
SV
11328 struct intel_encoder *intel_encoder =
11329 intel_attached_encoder(connector);
4ef69c7a 11330 struct drm_encoder *encoder = &intel_encoder->base;
edde3617 11331 struct drm_atomic_state *state = old->restore_state;
d3a40d1b 11332 int ret;
79e53945 11333
d2dff872 11334 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
c23cc417 11335 connector->base.id, connector->name,
8e329a03 11336 encoder->base.id, encoder->name);
d2dff872 11337
edde3617 11338 if (!state)
0622a53c 11339 return;
79e53945 11340
edde3617 11341 ret = drm_atomic_commit(state);
0853695c 11342 if (ret)
edde3617 11343 DRM_DEBUG_KMS("Couldn't release load detect pipe: %i\n", ret);
0853695c 11344 drm_atomic_state_put(state);
79e53945
JB
11345}
11346
da4a1efa 11347static int i9xx_pll_refclk(struct drm_device *dev,
5cec258b 11348 const struct intel_crtc_state *pipe_config)
da4a1efa 11349{
fac5e23e 11350 struct drm_i915_private *dev_priv = to_i915(dev);
da4a1efa
VS
11351 u32 dpll = pipe_config->dpll_hw_state.dpll;
11352
11353 if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
e91e941b 11354 return dev_priv->vbt.lvds_ssc_freq;
6e266956 11355 else if (HAS_PCH_SPLIT(dev_priv))
da4a1efa 11356 return 120000;
5db94019 11357 else if (!IS_GEN2(dev_priv))
da4a1efa
VS
11358 return 96000;
11359 else
11360 return 48000;
11361}
11362
79e53945 11363/* Returns the clock of the currently programmed mode of the given pipe. */
f1f644dc 11364static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 11365 struct intel_crtc_state *pipe_config)
79e53945 11366{
f1f644dc 11367 struct drm_device *dev = crtc->base.dev;
fac5e23e 11368 struct drm_i915_private *dev_priv = to_i915(dev);
f1f644dc 11369 int pipe = pipe_config->cpu_transcoder;
293623f7 11370 u32 dpll = pipe_config->dpll_hw_state.dpll;
79e53945 11371 u32 fp;
9e2c8475 11372 struct dpll clock;
dccbea3b 11373 int port_clock;
da4a1efa 11374 int refclk = i9xx_pll_refclk(dev, pipe_config);
79e53945
JB
11375
11376 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
293623f7 11377 fp = pipe_config->dpll_hw_state.fp0;
79e53945 11378 else
293623f7 11379 fp = pipe_config->dpll_hw_state.fp1;
79e53945
JB
11380
11381 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
9b1e14f4 11382 if (IS_PINEVIEW(dev_priv)) {
f2b115e6
AJ
11383 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
11384 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
2177832f
SL
11385 } else {
11386 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
11387 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
11388 }
11389
5db94019 11390 if (!IS_GEN2(dev_priv)) {
9b1e14f4 11391 if (IS_PINEVIEW(dev_priv))
f2b115e6
AJ
11392 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
11393 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
2177832f
SL
11394 else
11395 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
79e53945
JB
11396 DPLL_FPA01_P1_POST_DIV_SHIFT);
11397
11398 switch (dpll & DPLL_MODE_MASK) {
11399 case DPLLB_MODE_DAC_SERIAL:
11400 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
11401 5 : 10;
11402 break;
11403 case DPLLB_MODE_LVDS:
11404 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
11405 7 : 14;
11406 break;
11407 default:
28c97730 11408 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
79e53945 11409 "mode\n", (int)(dpll & DPLL_MODE_MASK));
f1f644dc 11410 return;
79e53945
JB
11411 }
11412
9b1e14f4 11413 if (IS_PINEVIEW(dev_priv))
dccbea3b 11414 port_clock = pnv_calc_dpll_params(refclk, &clock);
ac58c3f0 11415 else
dccbea3b 11416 port_clock = i9xx_calc_dpll_params(refclk, &clock);
79e53945 11417 } else {
50a0bc90 11418 u32 lvds = IS_I830(dev_priv) ? 0 : I915_READ(LVDS);
b1c560d1 11419 bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
79e53945
JB
11420
11421 if (is_lvds) {
11422 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
11423 DPLL_FPA01_P1_POST_DIV_SHIFT);
b1c560d1
VS
11424
11425 if (lvds & LVDS_CLKB_POWER_UP)
11426 clock.p2 = 7;
11427 else
11428 clock.p2 = 14;
79e53945
JB
11429 } else {
11430 if (dpll & PLL_P1_DIVIDE_BY_TWO)
11431 clock.p1 = 2;
11432 else {
11433 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
11434 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
11435 }
11436 if (dpll & PLL_P2_DIVIDE_BY_4)
11437 clock.p2 = 4;
11438 else
11439 clock.p2 = 2;
79e53945 11440 }
da4a1efa 11441
dccbea3b 11442 port_clock = i9xx_calc_dpll_params(refclk, &clock);
79e53945
JB
11443 }
11444
18442d08
VS
11445 /*
11446 * This value includes pixel_multiplier. We will use
241bfc38 11447 * port_clock to compute adjusted_mode.crtc_clock in the
18442d08
VS
11448 * encoder's get_config() function.
11449 */
dccbea3b 11450 pipe_config->port_clock = port_clock;
f1f644dc
JB
11451}
11452
6878da05
VS
11453int intel_dotclock_calculate(int link_freq,
11454 const struct intel_link_m_n *m_n)
f1f644dc 11455{
f1f644dc
JB
11456 /*
11457 * The calculation for the data clock is:
1041a02f 11458 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
f1f644dc 11459 * But we want to avoid losing precison if possible, so:
1041a02f 11460 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
f1f644dc
JB
11461 *
11462 * and the link clock is simpler:
1041a02f 11463 * link_clock = (m * link_clock) / n
f1f644dc
JB
11464 */
11465
6878da05
VS
11466 if (!m_n->link_n)
11467 return 0;
f1f644dc 11468
6878da05
VS
11469 return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
11470}
f1f644dc 11471
18442d08 11472static void ironlake_pch_clock_get(struct intel_crtc *crtc,
5cec258b 11473 struct intel_crtc_state *pipe_config)
6878da05 11474{
e3b247da 11475 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
79e53945 11476
18442d08
VS
11477 /* read out port_clock from the DPLL */
11478 i9xx_crtc_clock_get(crtc, pipe_config);
f1f644dc 11479
f1f644dc 11480 /*
e3b247da
VS
11481 * In case there is an active pipe without active ports,
11482 * we may need some idea for the dotclock anyway.
11483 * Calculate one based on the FDI configuration.
79e53945 11484 */
2d112de7 11485 pipe_config->base.adjusted_mode.crtc_clock =
21a727b3 11486 intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
18442d08 11487 &pipe_config->fdi_m_n);
79e53945
JB
11488}
11489
11490/** Returns the currently programmed mode of the given pipe. */
11491struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
11492 struct drm_crtc *crtc)
11493{
fac5e23e 11494 struct drm_i915_private *dev_priv = to_i915(dev);
79e53945 11495 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6e3c9717 11496 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
79e53945 11497 struct drm_display_mode *mode;
3f36b937 11498 struct intel_crtc_state *pipe_config;
fe2b8f9d
PZ
11499 int htot = I915_READ(HTOTAL(cpu_transcoder));
11500 int hsync = I915_READ(HSYNC(cpu_transcoder));
11501 int vtot = I915_READ(VTOTAL(cpu_transcoder));
11502 int vsync = I915_READ(VSYNC(cpu_transcoder));
293623f7 11503 enum pipe pipe = intel_crtc->pipe;
79e53945
JB
11504
11505 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
11506 if (!mode)
11507 return NULL;
11508
3f36b937
TU
11509 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
11510 if (!pipe_config) {
11511 kfree(mode);
11512 return NULL;
11513 }
11514
f1f644dc
JB
11515 /*
11516 * Construct a pipe_config sufficient for getting the clock info
11517 * back out of crtc_clock_get.
11518 *
11519 * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
11520 * to use a real value here instead.
11521 */
3f36b937
TU
11522 pipe_config->cpu_transcoder = (enum transcoder) pipe;
11523 pipe_config->pixel_multiplier = 1;
11524 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(pipe));
11525 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(pipe));
11526 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(pipe));
11527 i9xx_crtc_clock_get(intel_crtc, pipe_config);
11528
11529 mode->clock = pipe_config->port_clock / pipe_config->pixel_multiplier;
79e53945
JB
11530 mode->hdisplay = (htot & 0xffff) + 1;
11531 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
11532 mode->hsync_start = (hsync & 0xffff) + 1;
11533 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
11534 mode->vdisplay = (vtot & 0xffff) + 1;
11535 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
11536 mode->vsync_start = (vsync & 0xffff) + 1;
11537 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
11538
11539 drm_mode_set_name(mode);
79e53945 11540
3f36b937
TU
11541 kfree(pipe_config);
11542
79e53945
JB
11543 return mode;
11544}
11545
11546static void intel_crtc_destroy(struct drm_crtc *crtc)
11547{
11548 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
67e77c5a 11549 struct drm_device *dev = crtc->dev;
51cbaf01 11550 struct intel_flip_work *work;
67e77c5a 11551
5e2d7afc 11552 spin_lock_irq(&dev->event_lock);
5a21b665
SV
11553 work = intel_crtc->flip_work;
11554 intel_crtc->flip_work = NULL;
11555 spin_unlock_irq(&dev->event_lock);
67e77c5a 11556
5a21b665 11557 if (work) {
51cbaf01
ML
11558 cancel_work_sync(&work->mmio_work);
11559 cancel_work_sync(&work->unpin_work);
5a21b665 11560 kfree(work);
67e77c5a 11561 }
79e53945
JB
11562
11563 drm_crtc_cleanup(crtc);
67e77c5a 11564
79e53945
JB
11565 kfree(intel_crtc);
11566}
11567
6b95a207
KH
11568static void intel_unpin_work_fn(struct work_struct *__work)
11569{
51cbaf01
ML
11570 struct intel_flip_work *work =
11571 container_of(__work, struct intel_flip_work, unpin_work);
5a21b665
SV
11572 struct intel_crtc *crtc = to_intel_crtc(work->crtc);
11573 struct drm_device *dev = crtc->base.dev;
11574 struct drm_plane *primary = crtc->base.primary;
03f476e1 11575
5a21b665
SV
11576 if (is_mmio_work(work))
11577 flush_work(&work->mmio_work);
03f476e1 11578
5a21b665
SV
11579 mutex_lock(&dev->struct_mutex);
11580 intel_unpin_fb_obj(work->old_fb, primary->state->rotation);
f8c417cd 11581 i915_gem_object_put(work->pending_flip_obj);
5a21b665 11582 mutex_unlock(&dev->struct_mutex);
143f73b3 11583
e8a261ea
CW
11584 i915_gem_request_put(work->flip_queued_req);
11585
5748b6a1
CW
11586 intel_frontbuffer_flip_complete(to_i915(dev),
11587 to_intel_plane(primary)->frontbuffer_bit);
5a21b665
SV
11588 intel_fbc_post_update(crtc);
11589 drm_framebuffer_unreference(work->old_fb);
143f73b3 11590
5a21b665
SV
11591 BUG_ON(atomic_read(&crtc->unpin_work_count) == 0);
11592 atomic_dec(&crtc->unpin_work_count);
a6747b73 11593
5a21b665
SV
11594 kfree(work);
11595}
d9e86c0e 11596
5a21b665
SV
11597/* Is 'a' after or equal to 'b'? */
11598static bool g4x_flip_count_after_eq(u32 a, u32 b)
11599{
11600 return !((a - b) & 0x80000000);
11601}
143f73b3 11602
5a21b665
SV
11603static bool __pageflip_finished_cs(struct intel_crtc *crtc,
11604 struct intel_flip_work *work)
11605{
11606 struct drm_device *dev = crtc->base.dev;
fac5e23e 11607 struct drm_i915_private *dev_priv = to_i915(dev);
143f73b3 11608
8af29b0c 11609 if (abort_flip_on_reset(crtc))
5a21b665 11610 return true;
143f73b3 11611
5a21b665
SV
11612 /*
11613 * The relevant registers doen't exist on pre-ctg.
11614 * As the flip done interrupt doesn't trigger for mmio
11615 * flips on gmch platforms, a flip count check isn't
11616 * really needed there. But since ctg has the registers,
11617 * include it in the check anyway.
11618 */
9beb5fea 11619 if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv))
5a21b665 11620 return true;
b4a98e57 11621
5a21b665
SV
11622 /*
11623 * BDW signals flip done immediately if the plane
11624 * is disabled, even if the plane enable is already
11625 * armed to occur at the next vblank :(
11626 */
f99d7069 11627
5a21b665
SV
11628 /*
11629 * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
11630 * used the same base address. In that case the mmio flip might
11631 * have completed, but the CS hasn't even executed the flip yet.
11632 *
11633 * A flip count check isn't enough as the CS might have updated
11634 * the base address just after start of vblank, but before we
11635 * managed to process the interrupt. This means we'd complete the
11636 * CS flip too soon.
11637 *
11638 * Combining both checks should get us a good enough result. It may
11639 * still happen that the CS flip has been executed, but has not
11640 * yet actually completed. But in case the base address is the same
11641 * anyway, we don't really care.
11642 */
11643 return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
11644 crtc->flip_work->gtt_offset &&
11645 g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_G4X(crtc->pipe)),
11646 crtc->flip_work->flip_count);
11647}
b4a98e57 11648
5a21b665
SV
11649static bool
11650__pageflip_finished_mmio(struct intel_crtc *crtc,
11651 struct intel_flip_work *work)
11652{
11653 /*
11654 * MMIO work completes when vblank is different from
11655 * flip_queued_vblank.
11656 *
11657 * Reset counter value doesn't matter, this is handled by
11658 * i915_wait_request finishing early, so no need to handle
11659 * reset here.
11660 */
11661 return intel_crtc_get_vblank_counter(crtc) != work->flip_queued_vblank;
6b95a207
KH
11662}
11663
51cbaf01
ML
11664
11665static bool pageflip_finished(struct intel_crtc *crtc,
11666 struct intel_flip_work *work)
11667{
11668 if (!atomic_read(&work->pending))
11669 return false;
11670
11671 smp_rmb();
11672
5a21b665
SV
11673 if (is_mmio_work(work))
11674 return __pageflip_finished_mmio(crtc, work);
11675 else
11676 return __pageflip_finished_cs(crtc, work);
11677}
11678
11679void intel_finish_page_flip_cs(struct drm_i915_private *dev_priv, int pipe)
11680{
91c8a326 11681 struct drm_device *dev = &dev_priv->drm;
98187836 11682 struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
5a21b665
SV
11683 struct intel_flip_work *work;
11684 unsigned long flags;
11685
11686 /* Ignore early vblank irqs */
11687 if (!crtc)
11688 return;
11689
51cbaf01 11690 /*
5a21b665
SV
11691 * This is called both by irq handlers and the reset code (to complete
11692 * lost pageflips) so needs the full irqsave spinlocks.
51cbaf01 11693 */
5a21b665 11694 spin_lock_irqsave(&dev->event_lock, flags);
e2af48c6 11695 work = crtc->flip_work;
5a21b665
SV
11696
11697 if (work != NULL &&
11698 !is_mmio_work(work) &&
e2af48c6
VS
11699 pageflip_finished(crtc, work))
11700 page_flip_completed(crtc);
5a21b665
SV
11701
11702 spin_unlock_irqrestore(&dev->event_lock, flags);
75f7f3ec
VS
11703}
11704
51cbaf01 11705void intel_finish_page_flip_mmio(struct drm_i915_private *dev_priv, int pipe)
6b95a207 11706{
91c8a326 11707 struct drm_device *dev = &dev_priv->drm;
98187836 11708 struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
51cbaf01 11709 struct intel_flip_work *work;
6b95a207
KH
11710 unsigned long flags;
11711
5251f04e
ML
11712 /* Ignore early vblank irqs */
11713 if (!crtc)
11714 return;
f326038a
SV
11715
11716 /*
11717 * This is called both by irq handlers and the reset code (to complete
11718 * lost pageflips) so needs the full irqsave spinlocks.
e7d841ca 11719 */
6b95a207 11720 spin_lock_irqsave(&dev->event_lock, flags);
e2af48c6 11721 work = crtc->flip_work;
5251f04e 11722
5a21b665
SV
11723 if (work != NULL &&
11724 is_mmio_work(work) &&
e2af48c6
VS
11725 pageflip_finished(crtc, work))
11726 page_flip_completed(crtc);
5251f04e 11727
6b95a207
KH
11728 spin_unlock_irqrestore(&dev->event_lock, flags);
11729}
11730
5a21b665
SV
11731static inline void intel_mark_page_flip_active(struct intel_crtc *crtc,
11732 struct intel_flip_work *work)
84c33a64 11733{
5a21b665 11734 work->flip_queued_vblank = intel_crtc_get_vblank_counter(crtc);
84c33a64 11735
5a21b665
SV
11736 /* Ensure that the work item is consistent when activating it ... */
11737 smp_mb__before_atomic();
11738 atomic_set(&work->pending, 1);
11739}
a6747b73 11740
5a21b665
SV
11741static int intel_gen2_queue_flip(struct drm_device *dev,
11742 struct drm_crtc *crtc,
11743 struct drm_framebuffer *fb,
11744 struct drm_i915_gem_object *obj,
11745 struct drm_i915_gem_request *req,
11746 uint32_t flags)
11747{
7e37f889 11748 struct intel_ring *ring = req->ring;
5a21b665
SV
11749 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11750 u32 flip_mask;
11751 int ret;
143f73b3 11752
5a21b665
SV
11753 ret = intel_ring_begin(req, 6);
11754 if (ret)
11755 return ret;
143f73b3 11756
5a21b665
SV
11757 /* Can't queue multiple flips, so wait for the previous
11758 * one to finish before executing the next.
11759 */
11760 if (intel_crtc->plane)
11761 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
11762 else
11763 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
b5321f30
CW
11764 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
11765 intel_ring_emit(ring, MI_NOOP);
11766 intel_ring_emit(ring, MI_DISPLAY_FLIP |
5a21b665 11767 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
b5321f30
CW
11768 intel_ring_emit(ring, fb->pitches[0]);
11769 intel_ring_emit(ring, intel_crtc->flip_work->gtt_offset);
11770 intel_ring_emit(ring, 0); /* aux display base address, unused */
143f73b3 11771
5a21b665
SV
11772 return 0;
11773}
84c33a64 11774
5a21b665
SV
11775static int intel_gen3_queue_flip(struct drm_device *dev,
11776 struct drm_crtc *crtc,
11777 struct drm_framebuffer *fb,
11778 struct drm_i915_gem_object *obj,
11779 struct drm_i915_gem_request *req,
11780 uint32_t flags)
11781{
7e37f889 11782 struct intel_ring *ring = req->ring;
5a21b665
SV
11783 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11784 u32 flip_mask;
11785 int ret;
d55dbd06 11786
5a21b665
SV
11787 ret = intel_ring_begin(req, 6);
11788 if (ret)
11789 return ret;
d55dbd06 11790
5a21b665
SV
11791 if (intel_crtc->plane)
11792 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
11793 else
11794 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
b5321f30
CW
11795 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
11796 intel_ring_emit(ring, MI_NOOP);
11797 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
5a21b665 11798 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
b5321f30
CW
11799 intel_ring_emit(ring, fb->pitches[0]);
11800 intel_ring_emit(ring, intel_crtc->flip_work->gtt_offset);
11801 intel_ring_emit(ring, MI_NOOP);
fd8e058a 11802
5a21b665
SV
11803 return 0;
11804}
84c33a64 11805
5a21b665
SV
11806static int intel_gen4_queue_flip(struct drm_device *dev,
11807 struct drm_crtc *crtc,
11808 struct drm_framebuffer *fb,
11809 struct drm_i915_gem_object *obj,
11810 struct drm_i915_gem_request *req,
11811 uint32_t flags)
11812{
7e37f889 11813 struct intel_ring *ring = req->ring;
fac5e23e 11814 struct drm_i915_private *dev_priv = to_i915(dev);
5a21b665
SV
11815 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11816 uint32_t pf, pipesrc;
11817 int ret;
143f73b3 11818
5a21b665
SV
11819 ret = intel_ring_begin(req, 4);
11820 if (ret)
11821 return ret;
143f73b3 11822
5a21b665
SV
11823 /* i965+ uses the linear or tiled offsets from the
11824 * Display Registers (which do not change across a page-flip)
11825 * so we need only reprogram the base address.
11826 */
b5321f30 11827 intel_ring_emit(ring, MI_DISPLAY_FLIP |
5a21b665 11828 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
b5321f30
CW
11829 intel_ring_emit(ring, fb->pitches[0]);
11830 intel_ring_emit(ring, intel_crtc->flip_work->gtt_offset |
72618ebf 11831 intel_fb_modifier_to_tiling(fb->modifier[0]));
5a21b665
SV
11832
11833 /* XXX Enabling the panel-fitter across page-flip is so far
11834 * untested on non-native modes, so ignore it for now.
11835 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
11836 */
11837 pf = 0;
11838 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
b5321f30 11839 intel_ring_emit(ring, pf | pipesrc);
143f73b3 11840
5a21b665 11841 return 0;
8c9f3aaf
JB
11842}
11843
5a21b665
SV
11844static int intel_gen6_queue_flip(struct drm_device *dev,
11845 struct drm_crtc *crtc,
11846 struct drm_framebuffer *fb,
11847 struct drm_i915_gem_object *obj,
11848 struct drm_i915_gem_request *req,
11849 uint32_t flags)
da20eabd 11850{
7e37f889 11851 struct intel_ring *ring = req->ring;
fac5e23e 11852 struct drm_i915_private *dev_priv = to_i915(dev);
5a21b665
SV
11853 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11854 uint32_t pf, pipesrc;
11855 int ret;
d21fbe87 11856
5a21b665
SV
11857 ret = intel_ring_begin(req, 4);
11858 if (ret)
11859 return ret;
92826fcd 11860
b5321f30 11861 intel_ring_emit(ring, MI_DISPLAY_FLIP |
5a21b665 11862 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
72618ebf
VS
11863 intel_ring_emit(ring, fb->pitches[0] |
11864 intel_fb_modifier_to_tiling(fb->modifier[0]));
b5321f30 11865 intel_ring_emit(ring, intel_crtc->flip_work->gtt_offset);
92826fcd 11866
5a21b665
SV
11867 /* Contrary to the suggestions in the documentation,
11868 * "Enable Panel Fitter" does not seem to be required when page
11869 * flipping with a non-native mode, and worse causes a normal
11870 * modeset to fail.
11871 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
11872 */
11873 pf = 0;
11874 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
b5321f30 11875 intel_ring_emit(ring, pf | pipesrc);
7809e5ae 11876
5a21b665 11877 return 0;
7809e5ae
MR
11878}
11879
5a21b665
SV
11880static int intel_gen7_queue_flip(struct drm_device *dev,
11881 struct drm_crtc *crtc,
11882 struct drm_framebuffer *fb,
11883 struct drm_i915_gem_object *obj,
11884 struct drm_i915_gem_request *req,
11885 uint32_t flags)
d21fbe87 11886{
5db94019 11887 struct drm_i915_private *dev_priv = to_i915(dev);
7e37f889 11888 struct intel_ring *ring = req->ring;
5a21b665
SV
11889 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11890 uint32_t plane_bit = 0;
11891 int len, ret;
d21fbe87 11892
5a21b665
SV
11893 switch (intel_crtc->plane) {
11894 case PLANE_A:
11895 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
11896 break;
11897 case PLANE_B:
11898 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
11899 break;
11900 case PLANE_C:
11901 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
11902 break;
11903 default:
11904 WARN_ONCE(1, "unknown plane in flip command\n");
11905 return -ENODEV;
11906 }
11907
11908 len = 4;
b5321f30 11909 if (req->engine->id == RCS) {
5a21b665
SV
11910 len += 6;
11911 /*
11912 * On Gen 8, SRM is now taking an extra dword to accommodate
11913 * 48bits addresses, and we need a NOOP for the batch size to
11914 * stay even.
11915 */
5db94019 11916 if (IS_GEN8(dev_priv))
5a21b665
SV
11917 len += 2;
11918 }
11919
11920 /*
11921 * BSpec MI_DISPLAY_FLIP for IVB:
11922 * "The full packet must be contained within the same cache line."
11923 *
11924 * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
11925 * cacheline, if we ever start emitting more commands before
11926 * the MI_DISPLAY_FLIP we may need to first emit everything else,
11927 * then do the cacheline alignment, and finally emit the
11928 * MI_DISPLAY_FLIP.
11929 */
11930 ret = intel_ring_cacheline_align(req);
11931 if (ret)
11932 return ret;
11933
11934 ret = intel_ring_begin(req, len);
11935 if (ret)
11936 return ret;
11937
11938 /* Unmask the flip-done completion message. Note that the bspec says that
11939 * we should do this for both the BCS and RCS, and that we must not unmask
11940 * more than one flip event at any time (or ensure that one flip message
11941 * can be sent by waiting for flip-done prior to queueing new flips).
11942 * Experimentation says that BCS works despite DERRMR masking all
11943 * flip-done completion events and that unmasking all planes at once
11944 * for the RCS also doesn't appear to drop events. Setting the DERRMR
11945 * to zero does lead to lockups within MI_DISPLAY_FLIP.
11946 */
b5321f30
CW
11947 if (req->engine->id == RCS) {
11948 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
11949 intel_ring_emit_reg(ring, DERRMR);
11950 intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
5a21b665
SV
11951 DERRMR_PIPEB_PRI_FLIP_DONE |
11952 DERRMR_PIPEC_PRI_FLIP_DONE));
5db94019 11953 if (IS_GEN8(dev_priv))
b5321f30 11954 intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8 |
5a21b665
SV
11955 MI_SRM_LRM_GLOBAL_GTT);
11956 else
b5321f30 11957 intel_ring_emit(ring, MI_STORE_REGISTER_MEM |
5a21b665 11958 MI_SRM_LRM_GLOBAL_GTT);
b5321f30 11959 intel_ring_emit_reg(ring, DERRMR);
bde13ebd
CW
11960 intel_ring_emit(ring,
11961 i915_ggtt_offset(req->engine->scratch) + 256);
5db94019 11962 if (IS_GEN8(dev_priv)) {
b5321f30
CW
11963 intel_ring_emit(ring, 0);
11964 intel_ring_emit(ring, MI_NOOP);
5a21b665
SV
11965 }
11966 }
11967
b5321f30 11968 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
72618ebf
VS
11969 intel_ring_emit(ring, fb->pitches[0] |
11970 intel_fb_modifier_to_tiling(fb->modifier[0]));
b5321f30
CW
11971 intel_ring_emit(ring, intel_crtc->flip_work->gtt_offset);
11972 intel_ring_emit(ring, (MI_NOOP));
5a21b665
SV
11973
11974 return 0;
11975}
11976
11977static bool use_mmio_flip(struct intel_engine_cs *engine,
11978 struct drm_i915_gem_object *obj)
11979{
11980 /*
11981 * This is not being used for older platforms, because
11982 * non-availability of flip done interrupt forces us to use
11983 * CS flips. Older platforms derive flip done using some clever
11984 * tricks involving the flip_pending status bits and vblank irqs.
11985 * So using MMIO flips there would disrupt this mechanism.
11986 */
11987
11988 if (engine == NULL)
11989 return true;
11990
11991 if (INTEL_GEN(engine->i915) < 5)
11992 return false;
11993
11994 if (i915.use_mmio_flip < 0)
11995 return false;
11996 else if (i915.use_mmio_flip > 0)
11997 return true;
11998 else if (i915.enable_execlists)
11999 return true;
c37efb99 12000
d07f0e59 12001 return engine != i915_gem_object_last_write_engine(obj);
5a21b665
SV
12002}
12003
12004static void skl_do_mmio_flip(struct intel_crtc *intel_crtc,
12005 unsigned int rotation,
12006 struct intel_flip_work *work)
12007{
12008 struct drm_device *dev = intel_crtc->base.dev;
fac5e23e 12009 struct drm_i915_private *dev_priv = to_i915(dev);
5a21b665
SV
12010 struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
12011 const enum pipe pipe = intel_crtc->pipe;
d2196774 12012 u32 ctl, stride = skl_plane_stride(fb, 0, rotation);
5a21b665
SV
12013
12014 ctl = I915_READ(PLANE_CTL(pipe, 0));
12015 ctl &= ~PLANE_CTL_TILED_MASK;
12016 switch (fb->modifier[0]) {
12017 case DRM_FORMAT_MOD_NONE:
12018 break;
12019 case I915_FORMAT_MOD_X_TILED:
12020 ctl |= PLANE_CTL_TILED_X;
12021 break;
12022 case I915_FORMAT_MOD_Y_TILED:
12023 ctl |= PLANE_CTL_TILED_Y;
12024 break;
12025 case I915_FORMAT_MOD_Yf_TILED:
12026 ctl |= PLANE_CTL_TILED_YF;
12027 break;
12028 default:
12029 MISSING_CASE(fb->modifier[0]);
12030 }
12031
5a21b665
SV
12032 /*
12033 * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
12034 * PLANE_SURF updates, the update is then guaranteed to be atomic.
12035 */
12036 I915_WRITE(PLANE_CTL(pipe, 0), ctl);
12037 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
12038
12039 I915_WRITE(PLANE_SURF(pipe, 0), work->gtt_offset);
12040 POSTING_READ(PLANE_SURF(pipe, 0));
12041}
12042
12043static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc,
12044 struct intel_flip_work *work)
12045{
12046 struct drm_device *dev = intel_crtc->base.dev;
fac5e23e 12047 struct drm_i915_private *dev_priv = to_i915(dev);
72618ebf 12048 struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
5a21b665
SV
12049 i915_reg_t reg = DSPCNTR(intel_crtc->plane);
12050 u32 dspcntr;
12051
12052 dspcntr = I915_READ(reg);
12053
72618ebf 12054 if (fb->modifier[0] == I915_FORMAT_MOD_X_TILED)
5a21b665
SV
12055 dspcntr |= DISPPLANE_TILED;
12056 else
12057 dspcntr &= ~DISPPLANE_TILED;
12058
12059 I915_WRITE(reg, dspcntr);
12060
12061 I915_WRITE(DSPSURF(intel_crtc->plane), work->gtt_offset);
12062 POSTING_READ(DSPSURF(intel_crtc->plane));
12063}
12064
12065static void intel_mmio_flip_work_func(struct work_struct *w)
12066{
12067 struct intel_flip_work *work =
12068 container_of(w, struct intel_flip_work, mmio_work);
12069 struct intel_crtc *crtc = to_intel_crtc(work->crtc);
12070 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
12071 struct intel_framebuffer *intel_fb =
12072 to_intel_framebuffer(crtc->base.primary->fb);
12073 struct drm_i915_gem_object *obj = intel_fb->obj;
12074
d07f0e59 12075 WARN_ON(i915_gem_object_wait(obj, 0, MAX_SCHEDULE_TIMEOUT, NULL) < 0);
5a21b665
SV
12076
12077 intel_pipe_update_start(crtc);
12078
12079 if (INTEL_GEN(dev_priv) >= 9)
12080 skl_do_mmio_flip(crtc, work->rotation, work);
12081 else
12082 /* use_mmio_flip() retricts MMIO flips to ilk+ */
12083 ilk_do_mmio_flip(crtc, work);
12084
12085 intel_pipe_update_end(crtc, work);
12086}
12087
12088static int intel_default_queue_flip(struct drm_device *dev,
12089 struct drm_crtc *crtc,
12090 struct drm_framebuffer *fb,
12091 struct drm_i915_gem_object *obj,
12092 struct drm_i915_gem_request *req,
12093 uint32_t flags)
12094{
12095 return -ENODEV;
12096}
12097
12098static bool __pageflip_stall_check_cs(struct drm_i915_private *dev_priv,
12099 struct intel_crtc *intel_crtc,
12100 struct intel_flip_work *work)
12101{
12102 u32 addr, vblank;
12103
12104 if (!atomic_read(&work->pending))
12105 return false;
12106
12107 smp_rmb();
12108
12109 vblank = intel_crtc_get_vblank_counter(intel_crtc);
12110 if (work->flip_ready_vblank == 0) {
12111 if (work->flip_queued_req &&
f69a02c9 12112 !i915_gem_request_completed(work->flip_queued_req))
5a21b665
SV
12113 return false;
12114
12115 work->flip_ready_vblank = vblank;
12116 }
12117
12118 if (vblank - work->flip_ready_vblank < 3)
12119 return false;
12120
12121 /* Potential stall - if we see that the flip has happened,
12122 * assume a missed interrupt. */
12123 if (INTEL_GEN(dev_priv) >= 4)
12124 addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
12125 else
12126 addr = I915_READ(DSPADDR(intel_crtc->plane));
12127
12128 /* There is a potential issue here with a false positive after a flip
12129 * to the same address. We could address this by checking for a
12130 * non-incrementing frame counter.
12131 */
12132 return addr == work->gtt_offset;
12133}
12134
12135void intel_check_page_flip(struct drm_i915_private *dev_priv, int pipe)
12136{
91c8a326 12137 struct drm_device *dev = &dev_priv->drm;
98187836 12138 struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
5a21b665
SV
12139 struct intel_flip_work *work;
12140
12141 WARN_ON(!in_interrupt());
12142
12143 if (crtc == NULL)
12144 return;
12145
12146 spin_lock(&dev->event_lock);
e2af48c6 12147 work = crtc->flip_work;
5a21b665
SV
12148
12149 if (work != NULL && !is_mmio_work(work) &&
e2af48c6 12150 __pageflip_stall_check_cs(dev_priv, crtc, work)) {
5a21b665
SV
12151 WARN_ONCE(1,
12152 "Kicking stuck page flip: queued at %d, now %d\n",
e2af48c6
VS
12153 work->flip_queued_vblank, intel_crtc_get_vblank_counter(crtc));
12154 page_flip_completed(crtc);
5a21b665
SV
12155 work = NULL;
12156 }
12157
12158 if (work != NULL && !is_mmio_work(work) &&
e2af48c6 12159 intel_crtc_get_vblank_counter(crtc) - work->flip_queued_vblank > 1)
5a21b665
SV
12160 intel_queue_rps_boost_for_request(work->flip_queued_req);
12161 spin_unlock(&dev->event_lock);
12162}
12163
12164static int intel_crtc_page_flip(struct drm_crtc *crtc,
12165 struct drm_framebuffer *fb,
12166 struct drm_pending_vblank_event *event,
12167 uint32_t page_flip_flags)
12168{
12169 struct drm_device *dev = crtc->dev;
fac5e23e 12170 struct drm_i915_private *dev_priv = to_i915(dev);
5a21b665
SV
12171 struct drm_framebuffer *old_fb = crtc->primary->fb;
12172 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
12173 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
12174 struct drm_plane *primary = crtc->primary;
12175 enum pipe pipe = intel_crtc->pipe;
12176 struct intel_flip_work *work;
12177 struct intel_engine_cs *engine;
12178 bool mmio_flip;
8e637178 12179 struct drm_i915_gem_request *request;
058d88c4 12180 struct i915_vma *vma;
5a21b665
SV
12181 int ret;
12182
12183 /*
12184 * drm_mode_page_flip_ioctl() should already catch this, but double
12185 * check to be safe. In the future we may enable pageflipping from
12186 * a disabled primary plane.
12187 */
12188 if (WARN_ON(intel_fb_obj(old_fb) == NULL))
12189 return -EBUSY;
12190
12191 /* Can't change pixel format via MI display flips. */
12192 if (fb->pixel_format != crtc->primary->fb->pixel_format)
12193 return -EINVAL;
12194
12195 /*
12196 * TILEOFF/LINOFF registers can't be changed via MI display flips.
12197 * Note that pitch changes could also affect these register.
12198 */
12199 if (INTEL_INFO(dev)->gen > 3 &&
12200 (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
12201 fb->pitches[0] != crtc->primary->fb->pitches[0]))
12202 return -EINVAL;
12203
12204 if (i915_terminally_wedged(&dev_priv->gpu_error))
12205 goto out_hang;
12206
12207 work = kzalloc(sizeof(*work), GFP_KERNEL);
12208 if (work == NULL)
12209 return -ENOMEM;
12210
12211 work->event = event;
12212 work->crtc = crtc;
12213 work->old_fb = old_fb;
12214 INIT_WORK(&work->unpin_work, intel_unpin_work_fn);
12215
12216 ret = drm_crtc_vblank_get(crtc);
12217 if (ret)
12218 goto free_work;
12219
12220 /* We borrow the event spin lock for protecting flip_work */
12221 spin_lock_irq(&dev->event_lock);
12222 if (intel_crtc->flip_work) {
12223 /* Before declaring the flip queue wedged, check if
12224 * the hardware completed the operation behind our backs.
12225 */
12226 if (pageflip_finished(intel_crtc, intel_crtc->flip_work)) {
12227 DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
12228 page_flip_completed(intel_crtc);
12229 } else {
12230 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
12231 spin_unlock_irq(&dev->event_lock);
12232
12233 drm_crtc_vblank_put(crtc);
12234 kfree(work);
12235 return -EBUSY;
12236 }
12237 }
12238 intel_crtc->flip_work = work;
12239 spin_unlock_irq(&dev->event_lock);
12240
12241 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
12242 flush_workqueue(dev_priv->wq);
12243
12244 /* Reference the objects for the scheduled work. */
12245 drm_framebuffer_reference(work->old_fb);
5a21b665
SV
12246
12247 crtc->primary->fb = fb;
12248 update_state_fb(crtc->primary);
faf68d92 12249
25dc556a 12250 work->pending_flip_obj = i915_gem_object_get(obj);
5a21b665
SV
12251
12252 ret = i915_mutex_lock_interruptible(dev);
12253 if (ret)
12254 goto cleanup;
12255
8af29b0c
CW
12256 intel_crtc->reset_count = i915_reset_count(&dev_priv->gpu_error);
12257 if (i915_reset_in_progress_or_wedged(&dev_priv->gpu_error)) {
5a21b665
SV
12258 ret = -EIO;
12259 goto cleanup;
12260 }
12261
12262 atomic_inc(&intel_crtc->unpin_work_count);
12263
9beb5fea 12264 if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
5a21b665
SV
12265 work->flip_count = I915_READ(PIPE_FLIPCOUNT_G4X(pipe)) + 1;
12266
920a14b2 12267 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
3b3f1650 12268 engine = dev_priv->engine[BCS];
72618ebf 12269 if (fb->modifier[0] != old_fb->modifier[0])
5a21b665
SV
12270 /* vlv: DISPLAY_FLIP fails to change tiling */
12271 engine = NULL;
fd6b8f43 12272 } else if (IS_IVYBRIDGE(dev_priv) || IS_HASWELL(dev_priv)) {
3b3f1650 12273 engine = dev_priv->engine[BCS];
5a21b665 12274 } else if (INTEL_INFO(dev)->gen >= 7) {
d07f0e59 12275 engine = i915_gem_object_last_write_engine(obj);
5a21b665 12276 if (engine == NULL || engine->id != RCS)
3b3f1650 12277 engine = dev_priv->engine[BCS];
5a21b665 12278 } else {
3b3f1650 12279 engine = dev_priv->engine[RCS];
5a21b665
SV
12280 }
12281
12282 mmio_flip = use_mmio_flip(engine, obj);
12283
058d88c4
CW
12284 vma = intel_pin_and_fence_fb_obj(fb, primary->state->rotation);
12285 if (IS_ERR(vma)) {
12286 ret = PTR_ERR(vma);
5a21b665 12287 goto cleanup_pending;
058d88c4 12288 }
5a21b665 12289
6687c906 12290 work->gtt_offset = intel_fb_gtt_offset(fb, primary->state->rotation);
5a21b665
SV
12291 work->gtt_offset += intel_crtc->dspaddr_offset;
12292 work->rotation = crtc->primary->state->rotation;
12293
1f061316
PZ
12294 /*
12295 * There's the potential that the next frame will not be compatible with
12296 * FBC, so we want to call pre_update() before the actual page flip.
12297 * The problem is that pre_update() caches some information about the fb
12298 * object, so we want to do this only after the object is pinned. Let's
12299 * be on the safe side and do this immediately before scheduling the
12300 * flip.
12301 */
12302 intel_fbc_pre_update(intel_crtc, intel_crtc->config,
12303 to_intel_plane_state(primary->state));
12304
5a21b665
SV
12305 if (mmio_flip) {
12306 INIT_WORK(&work->mmio_work, intel_mmio_flip_work_func);
6277c8d0 12307 queue_work(system_unbound_wq, &work->mmio_work);
5a21b665 12308 } else {
8e637178
CW
12309 request = i915_gem_request_alloc(engine, engine->last_context);
12310 if (IS_ERR(request)) {
12311 ret = PTR_ERR(request);
12312 goto cleanup_unpin;
12313 }
12314
a2bc4695 12315 ret = i915_gem_request_await_object(request, obj, false);
8e637178
CW
12316 if (ret)
12317 goto cleanup_request;
12318
5a21b665
SV
12319 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, request,
12320 page_flip_flags);
12321 if (ret)
8e637178 12322 goto cleanup_request;
5a21b665
SV
12323
12324 intel_mark_page_flip_active(intel_crtc, work);
12325
8e637178 12326 work->flip_queued_req = i915_gem_request_get(request);
5a21b665
SV
12327 i915_add_request_no_flush(request);
12328 }
12329
12330 i915_gem_track_fb(intel_fb_obj(old_fb), obj,
12331 to_intel_plane(primary)->frontbuffer_bit);
12332 mutex_unlock(&dev->struct_mutex);
12333
5748b6a1 12334 intel_frontbuffer_flip_prepare(to_i915(dev),
5a21b665
SV
12335 to_intel_plane(primary)->frontbuffer_bit);
12336
12337 trace_i915_flip_request(intel_crtc->plane, obj);
12338
12339 return 0;
12340
8e637178
CW
12341cleanup_request:
12342 i915_add_request_no_flush(request);
5a21b665
SV
12343cleanup_unpin:
12344 intel_unpin_fb_obj(fb, crtc->primary->state->rotation);
12345cleanup_pending:
5a21b665
SV
12346 atomic_dec(&intel_crtc->unpin_work_count);
12347 mutex_unlock(&dev->struct_mutex);
12348cleanup:
12349 crtc->primary->fb = old_fb;
12350 update_state_fb(crtc->primary);
12351
f0cd5182 12352 i915_gem_object_put(obj);
5a21b665
SV
12353 drm_framebuffer_unreference(work->old_fb);
12354
12355 spin_lock_irq(&dev->event_lock);
12356 intel_crtc->flip_work = NULL;
12357 spin_unlock_irq(&dev->event_lock);
12358
12359 drm_crtc_vblank_put(crtc);
12360free_work:
12361 kfree(work);
12362
12363 if (ret == -EIO) {
12364 struct drm_atomic_state *state;
12365 struct drm_plane_state *plane_state;
12366
12367out_hang:
12368 state = drm_atomic_state_alloc(dev);
12369 if (!state)
12370 return -ENOMEM;
12371 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
12372
12373retry:
12374 plane_state = drm_atomic_get_plane_state(state, primary);
12375 ret = PTR_ERR_OR_ZERO(plane_state);
12376 if (!ret) {
12377 drm_atomic_set_fb_for_plane(plane_state, fb);
12378
12379 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
12380 if (!ret)
12381 ret = drm_atomic_commit(state);
12382 }
12383
12384 if (ret == -EDEADLK) {
12385 drm_modeset_backoff(state->acquire_ctx);
12386 drm_atomic_state_clear(state);
12387 goto retry;
12388 }
12389
0853695c 12390 drm_atomic_state_put(state);
5a21b665
SV
12391
12392 if (ret == 0 && event) {
12393 spin_lock_irq(&dev->event_lock);
12394 drm_crtc_send_vblank_event(crtc, event);
12395 spin_unlock_irq(&dev->event_lock);
12396 }
12397 }
12398 return ret;
12399}
12400
12401
12402/**
12403 * intel_wm_need_update - Check whether watermarks need updating
12404 * @plane: drm plane
12405 * @state: new plane state
12406 *
12407 * Check current plane state versus the new one to determine whether
12408 * watermarks need to be recalculated.
12409 *
12410 * Returns true or false.
12411 */
12412static bool intel_wm_need_update(struct drm_plane *plane,
12413 struct drm_plane_state *state)
12414{
12415 struct intel_plane_state *new = to_intel_plane_state(state);
12416 struct intel_plane_state *cur = to_intel_plane_state(plane->state);
12417
12418 /* Update watermarks on tiling or size changes. */
936e71e3 12419 if (new->base.visible != cur->base.visible)
5a21b665
SV
12420 return true;
12421
12422 if (!cur->base.fb || !new->base.fb)
12423 return false;
12424
12425 if (cur->base.fb->modifier[0] != new->base.fb->modifier[0] ||
12426 cur->base.rotation != new->base.rotation ||
936e71e3
VS
12427 drm_rect_width(&new->base.src) != drm_rect_width(&cur->base.src) ||
12428 drm_rect_height(&new->base.src) != drm_rect_height(&cur->base.src) ||
12429 drm_rect_width(&new->base.dst) != drm_rect_width(&cur->base.dst) ||
12430 drm_rect_height(&new->base.dst) != drm_rect_height(&cur->base.dst))
5a21b665
SV
12431 return true;
12432
12433 return false;
12434}
12435
12436static bool needs_scaling(struct intel_plane_state *state)
12437{
936e71e3
VS
12438 int src_w = drm_rect_width(&state->base.src) >> 16;
12439 int src_h = drm_rect_height(&state->base.src) >> 16;
12440 int dst_w = drm_rect_width(&state->base.dst);
12441 int dst_h = drm_rect_height(&state->base.dst);
5a21b665
SV
12442
12443 return (src_w != dst_w || src_h != dst_h);
12444}
d21fbe87 12445
da20eabd
ML
12446int intel_plane_atomic_calc_changes(struct drm_crtc_state *crtc_state,
12447 struct drm_plane_state *plane_state)
12448{
ab1d3a0e 12449 struct intel_crtc_state *pipe_config = to_intel_crtc_state(crtc_state);
da20eabd
ML
12450 struct drm_crtc *crtc = crtc_state->crtc;
12451 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
12452 struct drm_plane *plane = plane_state->plane;
12453 struct drm_device *dev = crtc->dev;
ed4a6a7c 12454 struct drm_i915_private *dev_priv = to_i915(dev);
da20eabd
ML
12455 struct intel_plane_state *old_plane_state =
12456 to_intel_plane_state(plane->state);
da20eabd
ML
12457 bool mode_changed = needs_modeset(crtc_state);
12458 bool was_crtc_enabled = crtc->state->active;
12459 bool is_crtc_enabled = crtc_state->active;
da20eabd
ML
12460 bool turn_off, turn_on, visible, was_visible;
12461 struct drm_framebuffer *fb = plane_state->fb;
78108b7c 12462 int ret;
da20eabd 12463
55b8f2a7 12464 if (INTEL_GEN(dev_priv) >= 9 && plane->type != DRM_PLANE_TYPE_CURSOR) {
da20eabd
ML
12465 ret = skl_update_scaler_plane(
12466 to_intel_crtc_state(crtc_state),
12467 to_intel_plane_state(plane_state));
12468 if (ret)
12469 return ret;
12470 }
12471
936e71e3
VS
12472 was_visible = old_plane_state->base.visible;
12473 visible = to_intel_plane_state(plane_state)->base.visible;
da20eabd
ML
12474
12475 if (!was_crtc_enabled && WARN_ON(was_visible))
12476 was_visible = false;
12477
35c08f43
ML
12478 /*
12479 * Visibility is calculated as if the crtc was on, but
12480 * after scaler setup everything depends on it being off
12481 * when the crtc isn't active.
f818ffea
VS
12482 *
12483 * FIXME this is wrong for watermarks. Watermarks should also
12484 * be computed as if the pipe would be active. Perhaps move
12485 * per-plane wm computation to the .check_plane() hook, and
12486 * only combine the results from all planes in the current place?
35c08f43
ML
12487 */
12488 if (!is_crtc_enabled)
936e71e3 12489 to_intel_plane_state(plane_state)->base.visible = visible = false;
da20eabd
ML
12490
12491 if (!was_visible && !visible)
12492 return 0;
12493
e8861675
ML
12494 if (fb != old_plane_state->base.fb)
12495 pipe_config->fb_changed = true;
12496
da20eabd
ML
12497 turn_off = was_visible && (!visible || mode_changed);
12498 turn_on = visible && (!was_visible || mode_changed);
12499
72660ce0 12500 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] has [PLANE:%d:%s] with fb %i\n",
78108b7c
VS
12501 intel_crtc->base.base.id,
12502 intel_crtc->base.name,
72660ce0
VS
12503 plane->base.id, plane->name,
12504 fb ? fb->base.id : -1);
da20eabd 12505
72660ce0
VS
12506 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] visible %i -> %i, off %i, on %i, ms %i\n",
12507 plane->base.id, plane->name,
12508 was_visible, visible,
da20eabd
ML
12509 turn_off, turn_on, mode_changed);
12510
caed361d
VS
12511 if (turn_on) {
12512 pipe_config->update_wm_pre = true;
12513
12514 /* must disable cxsr around plane enable/disable */
12515 if (plane->type != DRM_PLANE_TYPE_CURSOR)
12516 pipe_config->disable_cxsr = true;
12517 } else if (turn_off) {
12518 pipe_config->update_wm_post = true;
92826fcd 12519
852eb00d 12520 /* must disable cxsr around plane enable/disable */
e8861675 12521 if (plane->type != DRM_PLANE_TYPE_CURSOR)
ab1d3a0e 12522 pipe_config->disable_cxsr = true;
852eb00d 12523 } else if (intel_wm_need_update(plane, plane_state)) {
caed361d
VS
12524 /* FIXME bollocks */
12525 pipe_config->update_wm_pre = true;
12526 pipe_config->update_wm_post = true;
852eb00d 12527 }
da20eabd 12528
ed4a6a7c 12529 /* Pre-gen9 platforms need two-step watermark updates */
caed361d
VS
12530 if ((pipe_config->update_wm_pre || pipe_config->update_wm_post) &&
12531 INTEL_INFO(dev)->gen < 9 && dev_priv->display.optimize_watermarks)
ed4a6a7c
MR
12532 to_intel_crtc_state(crtc_state)->wm.need_postvbl_update = true;
12533
8be6ca85 12534 if (visible || was_visible)
cd202f69 12535 pipe_config->fb_bits |= to_intel_plane(plane)->frontbuffer_bit;
a9ff8714 12536
31ae71fc
ML
12537 /*
12538 * WaCxSRDisabledForSpriteScaling:ivb
12539 *
12540 * cstate->update_wm was already set above, so this flag will
12541 * take effect when we commit and program watermarks.
12542 */
fd6b8f43 12543 if (plane->type == DRM_PLANE_TYPE_OVERLAY && IS_IVYBRIDGE(dev_priv) &&
31ae71fc
ML
12544 needs_scaling(to_intel_plane_state(plane_state)) &&
12545 !needs_scaling(old_plane_state))
12546 pipe_config->disable_lp_wm = true;
d21fbe87 12547
da20eabd
ML
12548 return 0;
12549}
12550
6d3a1ce7
ML
12551static bool encoders_cloneable(const struct intel_encoder *a,
12552 const struct intel_encoder *b)
12553{
12554 /* masks could be asymmetric, so check both ways */
12555 return a == b || (a->cloneable & (1 << b->type) &&
12556 b->cloneable & (1 << a->type));
12557}
12558
12559static bool check_single_encoder_cloning(struct drm_atomic_state *state,
12560 struct intel_crtc *crtc,
12561 struct intel_encoder *encoder)
12562{
12563 struct intel_encoder *source_encoder;
12564 struct drm_connector *connector;
12565 struct drm_connector_state *connector_state;
12566 int i;
12567
12568 for_each_connector_in_state(state, connector, connector_state, i) {
12569 if (connector_state->crtc != &crtc->base)
12570 continue;
12571
12572 source_encoder =
12573 to_intel_encoder(connector_state->best_encoder);
12574 if (!encoders_cloneable(encoder, source_encoder))
12575 return false;
12576 }
12577
12578 return true;
12579}
12580
6d3a1ce7
ML
12581static int intel_crtc_atomic_check(struct drm_crtc *crtc,
12582 struct drm_crtc_state *crtc_state)
12583{
cf5a15be 12584 struct drm_device *dev = crtc->dev;
fac5e23e 12585 struct drm_i915_private *dev_priv = to_i915(dev);
6d3a1ce7 12586 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
cf5a15be
ML
12587 struct intel_crtc_state *pipe_config =
12588 to_intel_crtc_state(crtc_state);
6d3a1ce7 12589 struct drm_atomic_state *state = crtc_state->state;
4d20cd86 12590 int ret;
6d3a1ce7
ML
12591 bool mode_changed = needs_modeset(crtc_state);
12592
852eb00d 12593 if (mode_changed && !crtc_state->active)
caed361d 12594 pipe_config->update_wm_post = true;
eddfcbcd 12595
ad421372
ML
12596 if (mode_changed && crtc_state->enable &&
12597 dev_priv->display.crtc_compute_clock &&
8106ddbd 12598 !WARN_ON(pipe_config->shared_dpll)) {
ad421372
ML
12599 ret = dev_priv->display.crtc_compute_clock(intel_crtc,
12600 pipe_config);
12601 if (ret)
12602 return ret;
12603 }
12604
82cf435b
LL
12605 if (crtc_state->color_mgmt_changed) {
12606 ret = intel_color_check(crtc, crtc_state);
12607 if (ret)
12608 return ret;
e7852a4b
LL
12609
12610 /*
12611 * Changing color management on Intel hardware is
12612 * handled as part of planes update.
12613 */
12614 crtc_state->planes_changed = true;
82cf435b
LL
12615 }
12616
e435d6e5 12617 ret = 0;
86c8bbbe 12618 if (dev_priv->display.compute_pipe_wm) {
e3bddded 12619 ret = dev_priv->display.compute_pipe_wm(pipe_config);
ed4a6a7c
MR
12620 if (ret) {
12621 DRM_DEBUG_KMS("Target pipe watermarks are invalid\n");
12622 return ret;
12623 }
12624 }
12625
12626 if (dev_priv->display.compute_intermediate_wm &&
12627 !to_intel_atomic_state(state)->skip_intermediate_wm) {
12628 if (WARN_ON(!dev_priv->display.compute_pipe_wm))
12629 return 0;
12630
12631 /*
12632 * Calculate 'intermediate' watermarks that satisfy both the
12633 * old state and the new state. We can program these
12634 * immediately.
12635 */
12636 ret = dev_priv->display.compute_intermediate_wm(crtc->dev,
12637 intel_crtc,
12638 pipe_config);
12639 if (ret) {
12640 DRM_DEBUG_KMS("No valid intermediate pipe watermarks are possible\n");
86c8bbbe 12641 return ret;
ed4a6a7c 12642 }
e3d5457c
VS
12643 } else if (dev_priv->display.compute_intermediate_wm) {
12644 if (HAS_PCH_SPLIT(dev_priv) && INTEL_GEN(dev_priv) < 9)
12645 pipe_config->wm.ilk.intermediate = pipe_config->wm.ilk.optimal;
86c8bbbe
MR
12646 }
12647
e435d6e5
ML
12648 if (INTEL_INFO(dev)->gen >= 9) {
12649 if (mode_changed)
12650 ret = skl_update_scaler_crtc(pipe_config);
12651
12652 if (!ret)
12653 ret = intel_atomic_setup_scalers(dev, intel_crtc,
12654 pipe_config);
12655 }
12656
12657 return ret;
6d3a1ce7
ML
12658}
12659
65b38e0d 12660static const struct drm_crtc_helper_funcs intel_helper_funcs = {
f6e5b160 12661 .mode_set_base_atomic = intel_pipe_set_base_atomic,
5a21b665
SV
12662 .atomic_begin = intel_begin_crtc_commit,
12663 .atomic_flush = intel_finish_crtc_commit,
6d3a1ce7 12664 .atomic_check = intel_crtc_atomic_check,
f6e5b160
CW
12665};
12666
d29b2f9d
ACO
12667static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
12668{
12669 struct intel_connector *connector;
12670
12671 for_each_intel_connector(dev, connector) {
8863dc7f
SV
12672 if (connector->base.state->crtc)
12673 drm_connector_unreference(&connector->base);
12674
d29b2f9d
ACO
12675 if (connector->base.encoder) {
12676 connector->base.state->best_encoder =
12677 connector->base.encoder;
12678 connector->base.state->crtc =
12679 connector->base.encoder->crtc;
8863dc7f
SV
12680
12681 drm_connector_reference(&connector->base);
d29b2f9d
ACO
12682 } else {
12683 connector->base.state->best_encoder = NULL;
12684 connector->base.state->crtc = NULL;
12685 }
12686 }
12687}
12688
050f7aeb 12689static void
eba905b2 12690connected_sink_compute_bpp(struct intel_connector *connector,
5cec258b 12691 struct intel_crtc_state *pipe_config)
050f7aeb 12692{
6a2a5c5d 12693 const struct drm_display_info *info = &connector->base.display_info;
050f7aeb
SV
12694 int bpp = pipe_config->pipe_bpp;
12695
12696 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
6a2a5c5d
VS
12697 connector->base.base.id,
12698 connector->base.name);
050f7aeb
SV
12699
12700 /* Don't use an invalid EDID bpc value */
6a2a5c5d 12701 if (info->bpc != 0 && info->bpc * 3 < bpp) {
050f7aeb 12702 DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
6a2a5c5d
VS
12703 bpp, info->bpc * 3);
12704 pipe_config->pipe_bpp = info->bpc * 3;
050f7aeb
SV
12705 }
12706
196f954e 12707 /* Clamp bpp to 8 on screens without EDID 1.4 */
6a2a5c5d 12708 if (info->bpc == 0 && bpp > 24) {
196f954e
MK
12709 DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
12710 bpp);
12711 pipe_config->pipe_bpp = 24;
050f7aeb
SV
12712 }
12713}
12714
4e53c2e0 12715static int
050f7aeb 12716compute_baseline_pipe_bpp(struct intel_crtc *crtc,
5cec258b 12717 struct intel_crtc_state *pipe_config)
4e53c2e0 12718{
9beb5fea 12719 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1486017f 12720 struct drm_atomic_state *state;
da3ced29
ACO
12721 struct drm_connector *connector;
12722 struct drm_connector_state *connector_state;
1486017f 12723 int bpp, i;
4e53c2e0 12724
9beb5fea
TU
12725 if ((IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
12726 IS_CHERRYVIEW(dev_priv)))
4e53c2e0 12727 bpp = 10*3;
9beb5fea 12728 else if (INTEL_GEN(dev_priv) >= 5)
d328c9d7
SV
12729 bpp = 12*3;
12730 else
12731 bpp = 8*3;
12732
4e53c2e0 12733
4e53c2e0
SV
12734 pipe_config->pipe_bpp = bpp;
12735
1486017f
ACO
12736 state = pipe_config->base.state;
12737
4e53c2e0 12738 /* Clamp display bpp to EDID value */
da3ced29
ACO
12739 for_each_connector_in_state(state, connector, connector_state, i) {
12740 if (connector_state->crtc != &crtc->base)
4e53c2e0
SV
12741 continue;
12742
da3ced29
ACO
12743 connected_sink_compute_bpp(to_intel_connector(connector),
12744 pipe_config);
4e53c2e0
SV
12745 }
12746
12747 return bpp;
12748}
12749
644db711
SV
12750static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
12751{
12752 DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
12753 "type: 0x%x flags: 0x%x\n",
1342830c 12754 mode->crtc_clock,
644db711
SV
12755 mode->crtc_hdisplay, mode->crtc_hsync_start,
12756 mode->crtc_hsync_end, mode->crtc_htotal,
12757 mode->crtc_vdisplay, mode->crtc_vsync_start,
12758 mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
12759}
12760
c0b03411 12761static void intel_dump_pipe_config(struct intel_crtc *crtc,
5cec258b 12762 struct intel_crtc_state *pipe_config,
c0b03411
SV
12763 const char *context)
12764{
6a60cd87 12765 struct drm_device *dev = crtc->base.dev;
4f8036a2 12766 struct drm_i915_private *dev_priv = to_i915(dev);
6a60cd87
CK
12767 struct drm_plane *plane;
12768 struct intel_plane *intel_plane;
12769 struct intel_plane_state *state;
12770 struct drm_framebuffer *fb;
12771
78108b7c
VS
12772 DRM_DEBUG_KMS("[CRTC:%d:%s]%s config %p for pipe %c\n",
12773 crtc->base.base.id, crtc->base.name,
6a60cd87 12774 context, pipe_config, pipe_name(crtc->pipe));
c0b03411 12775
da205630 12776 DRM_DEBUG_KMS("cpu_transcoder: %s\n", transcoder_name(pipe_config->cpu_transcoder));
c0b03411
SV
12777 DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
12778 pipe_config->pipe_bpp, pipe_config->dither);
12779 DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
12780 pipe_config->has_pch_encoder,
12781 pipe_config->fdi_lanes,
12782 pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
12783 pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
12784 pipe_config->fdi_m_n.tu);
90a6b7b0 12785 DRM_DEBUG_KMS("dp: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
37a5650b 12786 intel_crtc_has_dp_encoder(pipe_config),
90a6b7b0 12787 pipe_config->lane_count,
eb14cb74
VS
12788 pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
12789 pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
12790 pipe_config->dp_m_n.tu);
b95af8be 12791
90a6b7b0 12792 DRM_DEBUG_KMS("dp: %i, lanes: %i, gmch_m2: %u, gmch_n2: %u, link_m2: %u, link_n2: %u, tu2: %u\n",
37a5650b 12793 intel_crtc_has_dp_encoder(pipe_config),
90a6b7b0 12794 pipe_config->lane_count,
b95af8be
VK
12795 pipe_config->dp_m2_n2.gmch_m,
12796 pipe_config->dp_m2_n2.gmch_n,
12797 pipe_config->dp_m2_n2.link_m,
12798 pipe_config->dp_m2_n2.link_n,
12799 pipe_config->dp_m2_n2.tu);
12800
55072d19
SV
12801 DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
12802 pipe_config->has_audio,
12803 pipe_config->has_infoframe);
12804
c0b03411 12805 DRM_DEBUG_KMS("requested mode:\n");
2d112de7 12806 drm_mode_debug_printmodeline(&pipe_config->base.mode);
c0b03411 12807 DRM_DEBUG_KMS("adjusted mode:\n");
2d112de7
ACO
12808 drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
12809 intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
d71b8d4a 12810 DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
37327abd
VS
12811 DRM_DEBUG_KMS("pipe src size: %dx%d\n",
12812 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
0ec463d3
TU
12813 DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
12814 crtc->num_scalers,
12815 pipe_config->scaler_state.scaler_users,
12816 pipe_config->scaler_state.scaler_id);
c0b03411
SV
12817 DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
12818 pipe_config->gmch_pfit.control,
12819 pipe_config->gmch_pfit.pgm_ratios,
12820 pipe_config->gmch_pfit.lvds_border_bits);
fd4daa9c 12821 DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
c0b03411 12822 pipe_config->pch_pfit.pos,
fd4daa9c
CW
12823 pipe_config->pch_pfit.size,
12824 pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
42db64ef 12825 DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
cf532bb2 12826 DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
6a60cd87 12827
e2d214ae 12828 if (IS_BROXTON(dev_priv)) {
c856052a 12829 DRM_DEBUG_KMS("dpll_hw_state: ebb0: 0x%x, ebb4: 0x%x,"
415ff0f6 12830 "pll0: 0x%x, pll1: 0x%x, pll2: 0x%x, pll3: 0x%x, "
c8453338 12831 "pll6: 0x%x, pll8: 0x%x, pll9: 0x%x, pll10: 0x%x, pcsdw12: 0x%x\n",
415ff0f6 12832 pipe_config->dpll_hw_state.ebb0,
05712c15 12833 pipe_config->dpll_hw_state.ebb4,
415ff0f6
TU
12834 pipe_config->dpll_hw_state.pll0,
12835 pipe_config->dpll_hw_state.pll1,
12836 pipe_config->dpll_hw_state.pll2,
12837 pipe_config->dpll_hw_state.pll3,
12838 pipe_config->dpll_hw_state.pll6,
12839 pipe_config->dpll_hw_state.pll8,
05712c15 12840 pipe_config->dpll_hw_state.pll9,
c8453338 12841 pipe_config->dpll_hw_state.pll10,
415ff0f6 12842 pipe_config->dpll_hw_state.pcsdw12);
0853723b 12843 } else if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv)) {
c856052a 12844 DRM_DEBUG_KMS("dpll_hw_state: "
415ff0f6 12845 "ctrl1: 0x%x, cfgcr1: 0x%x, cfgcr2: 0x%x\n",
415ff0f6
TU
12846 pipe_config->dpll_hw_state.ctrl1,
12847 pipe_config->dpll_hw_state.cfgcr1,
12848 pipe_config->dpll_hw_state.cfgcr2);
4f8036a2 12849 } else if (HAS_DDI(dev_priv)) {
c856052a 12850 DRM_DEBUG_KMS("dpll_hw_state: wrpll: 0x%x spll: 0x%x\n",
00490c22
ML
12851 pipe_config->dpll_hw_state.wrpll,
12852 pipe_config->dpll_hw_state.spll);
415ff0f6
TU
12853 } else {
12854 DRM_DEBUG_KMS("dpll_hw_state: dpll: 0x%x, dpll_md: 0x%x, "
12855 "fp0: 0x%x, fp1: 0x%x\n",
12856 pipe_config->dpll_hw_state.dpll,
12857 pipe_config->dpll_hw_state.dpll_md,
12858 pipe_config->dpll_hw_state.fp0,
12859 pipe_config->dpll_hw_state.fp1);
12860 }
12861
6a60cd87
CK
12862 DRM_DEBUG_KMS("planes on this crtc\n");
12863 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
d3828147 12864 char *format_name;
6a60cd87
CK
12865 intel_plane = to_intel_plane(plane);
12866 if (intel_plane->pipe != crtc->pipe)
12867 continue;
12868
12869 state = to_intel_plane_state(plane->state);
12870 fb = state->base.fb;
12871 if (!fb) {
1d577e02
VS
12872 DRM_DEBUG_KMS("[PLANE:%d:%s] disabled, scaler_id = %d\n",
12873 plane->base.id, plane->name, state->scaler_id);
6a60cd87
CK
12874 continue;
12875 }
12876
90844f00
EE
12877 format_name = drm_get_format_name(fb->pixel_format);
12878
1d577e02
VS
12879 DRM_DEBUG_KMS("[PLANE:%d:%s] enabled",
12880 plane->base.id, plane->name);
12881 DRM_DEBUG_KMS("\tFB:%d, fb = %ux%u format = %s",
90844f00 12882 fb->base.id, fb->width, fb->height, format_name);
1d577e02
VS
12883 DRM_DEBUG_KMS("\tscaler:%d src %dx%d+%d+%d dst %dx%d+%d+%d\n",
12884 state->scaler_id,
936e71e3
VS
12885 state->base.src.x1 >> 16,
12886 state->base.src.y1 >> 16,
12887 drm_rect_width(&state->base.src) >> 16,
12888 drm_rect_height(&state->base.src) >> 16,
12889 state->base.dst.x1, state->base.dst.y1,
12890 drm_rect_width(&state->base.dst),
12891 drm_rect_height(&state->base.dst));
90844f00
EE
12892
12893 kfree(format_name);
6a60cd87 12894 }
c0b03411
SV
12895}
12896
5448a00d 12897static bool check_digital_port_conflicts(struct drm_atomic_state *state)
00f0b378 12898{
5448a00d 12899 struct drm_device *dev = state->dev;
da3ced29 12900 struct drm_connector *connector;
00f0b378 12901 unsigned int used_ports = 0;
477321e0 12902 unsigned int used_mst_ports = 0;
00f0b378
VS
12903
12904 /*
12905 * Walk the connector list instead of the encoder
12906 * list to detect the problem on ddi platforms
12907 * where there's just one encoder per digital port.
12908 */
0bff4858
VS
12909 drm_for_each_connector(connector, dev) {
12910 struct drm_connector_state *connector_state;
12911 struct intel_encoder *encoder;
12912
12913 connector_state = drm_atomic_get_existing_connector_state(state, connector);
12914 if (!connector_state)
12915 connector_state = connector->state;
12916
5448a00d 12917 if (!connector_state->best_encoder)
00f0b378
VS
12918 continue;
12919
5448a00d
ACO
12920 encoder = to_intel_encoder(connector_state->best_encoder);
12921
12922 WARN_ON(!connector_state->crtc);
00f0b378
VS
12923
12924 switch (encoder->type) {
12925 unsigned int port_mask;
12926 case INTEL_OUTPUT_UNKNOWN:
4f8036a2 12927 if (WARN_ON(!HAS_DDI(to_i915(dev))))
00f0b378 12928 break;
cca0502b 12929 case INTEL_OUTPUT_DP:
00f0b378
VS
12930 case INTEL_OUTPUT_HDMI:
12931 case INTEL_OUTPUT_EDP:
12932 port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
12933
12934 /* the same port mustn't appear more than once */
12935 if (used_ports & port_mask)
12936 return false;
12937
12938 used_ports |= port_mask;
477321e0
VS
12939 break;
12940 case INTEL_OUTPUT_DP_MST:
12941 used_mst_ports |=
12942 1 << enc_to_mst(&encoder->base)->primary->port;
12943 break;
00f0b378
VS
12944 default:
12945 break;
12946 }
12947 }
12948
477321e0
VS
12949 /* can't mix MST and SST/HDMI on the same port */
12950 if (used_ports & used_mst_ports)
12951 return false;
12952
00f0b378
VS
12953 return true;
12954}
12955
83a57153
ACO
12956static void
12957clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
12958{
12959 struct drm_crtc_state tmp_state;
663a3640 12960 struct intel_crtc_scaler_state scaler_state;
4978cc93 12961 struct intel_dpll_hw_state dpll_hw_state;
8106ddbd 12962 struct intel_shared_dpll *shared_dpll;
c4e2d043 12963 bool force_thru;
83a57153 12964
7546a384
ACO
12965 /* FIXME: before the switch to atomic started, a new pipe_config was
12966 * kzalloc'd. Code that depends on any field being zero should be
12967 * fixed, so that the crtc_state can be safely duplicated. For now,
12968 * only fields that are know to not cause problems are preserved. */
12969
83a57153 12970 tmp_state = crtc_state->base;
663a3640 12971 scaler_state = crtc_state->scaler_state;
4978cc93
ACO
12972 shared_dpll = crtc_state->shared_dpll;
12973 dpll_hw_state = crtc_state->dpll_hw_state;
c4e2d043 12974 force_thru = crtc_state->pch_pfit.force_thru;
4978cc93 12975
83a57153 12976 memset(crtc_state, 0, sizeof *crtc_state);
4978cc93 12977
83a57153 12978 crtc_state->base = tmp_state;
663a3640 12979 crtc_state->scaler_state = scaler_state;
4978cc93
ACO
12980 crtc_state->shared_dpll = shared_dpll;
12981 crtc_state->dpll_hw_state = dpll_hw_state;
c4e2d043 12982 crtc_state->pch_pfit.force_thru = force_thru;
83a57153
ACO
12983}
12984
548ee15b 12985static int
b8cecdf5 12986intel_modeset_pipe_config(struct drm_crtc *crtc,
b359283a 12987 struct intel_crtc_state *pipe_config)
ee7b9f93 12988{
b359283a 12989 struct drm_atomic_state *state = pipe_config->base.state;
7758a113 12990 struct intel_encoder *encoder;
da3ced29 12991 struct drm_connector *connector;
0b901879 12992 struct drm_connector_state *connector_state;
d328c9d7 12993 int base_bpp, ret = -EINVAL;
0b901879 12994 int i;
e29c22c0 12995 bool retry = true;
ee7b9f93 12996
83a57153 12997 clear_intel_crtc_state(pipe_config);
7758a113 12998
e143a21c
SV
12999 pipe_config->cpu_transcoder =
13000 (enum transcoder) to_intel_crtc(crtc)->pipe;
b8cecdf5 13001
2960bc9c
ID
13002 /*
13003 * Sanitize sync polarity flags based on requested ones. If neither
13004 * positive or negative polarity is requested, treat this as meaning
13005 * negative polarity.
13006 */
2d112de7 13007 if (!(pipe_config->base.adjusted_mode.flags &
2960bc9c 13008 (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
2d112de7 13009 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
2960bc9c 13010
2d112de7 13011 if (!(pipe_config->base.adjusted_mode.flags &
2960bc9c 13012 (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
2d112de7 13013 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
2960bc9c 13014
d328c9d7
SV
13015 base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
13016 pipe_config);
13017 if (base_bpp < 0)
4e53c2e0
SV
13018 goto fail;
13019
e41a56be
VS
13020 /*
13021 * Determine the real pipe dimensions. Note that stereo modes can
13022 * increase the actual pipe size due to the frame doubling and
13023 * insertion of additional space for blanks between the frame. This
13024 * is stored in the crtc timings. We use the requested mode to do this
13025 * computation to clearly distinguish it from the adjusted mode, which
13026 * can be changed by the connectors in the below retry loop.
13027 */
2d112de7 13028 drm_crtc_get_hv_timing(&pipe_config->base.mode,
ecb7e16b
GP
13029 &pipe_config->pipe_src_w,
13030 &pipe_config->pipe_src_h);
e41a56be 13031
253c84c8
VS
13032 for_each_connector_in_state(state, connector, connector_state, i) {
13033 if (connector_state->crtc != crtc)
13034 continue;
13035
13036 encoder = to_intel_encoder(connector_state->best_encoder);
13037
e25148d0
VS
13038 if (!check_single_encoder_cloning(state, to_intel_crtc(crtc), encoder)) {
13039 DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
13040 goto fail;
13041 }
13042
253c84c8
VS
13043 /*
13044 * Determine output_types before calling the .compute_config()
13045 * hooks so that the hooks can use this information safely.
13046 */
13047 pipe_config->output_types |= 1 << encoder->type;
13048 }
13049
e29c22c0 13050encoder_retry:
ef1b460d 13051 /* Ensure the port clock defaults are reset when retrying. */
ff9a6750 13052 pipe_config->port_clock = 0;
ef1b460d 13053 pipe_config->pixel_multiplier = 1;
ff9a6750 13054
135c81b8 13055 /* Fill in default crtc timings, allow encoders to overwrite them. */
2d112de7
ACO
13056 drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
13057 CRTC_STEREO_DOUBLE);
135c81b8 13058
7758a113
SV
13059 /* Pass our mode to the connectors and the CRTC to give them a chance to
13060 * adjust it according to limitations or connector properties, and also
13061 * a chance to reject the mode entirely.
47f1c6c9 13062 */
da3ced29 13063 for_each_connector_in_state(state, connector, connector_state, i) {
0b901879 13064 if (connector_state->crtc != crtc)
7758a113 13065 continue;
7ae89233 13066
0b901879
ACO
13067 encoder = to_intel_encoder(connector_state->best_encoder);
13068
0a478c27 13069 if (!(encoder->compute_config(encoder, pipe_config, connector_state))) {
efea6e8e 13070 DRM_DEBUG_KMS("Encoder config failure\n");
7758a113
SV
13071 goto fail;
13072 }
ee7b9f93 13073 }
47f1c6c9 13074
ff9a6750
SV
13075 /* Set default port clock if not overwritten by the encoder. Needs to be
13076 * done afterwards in case the encoder adjusts the mode. */
13077 if (!pipe_config->port_clock)
2d112de7 13078 pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
241bfc38 13079 * pipe_config->pixel_multiplier;
ff9a6750 13080
a43f6e0f 13081 ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
e29c22c0 13082 if (ret < 0) {
7758a113
SV
13083 DRM_DEBUG_KMS("CRTC fixup failed\n");
13084 goto fail;
ee7b9f93 13085 }
e29c22c0
SV
13086
13087 if (ret == RETRY) {
13088 if (WARN(!retry, "loop in pipe configuration computation\n")) {
13089 ret = -EINVAL;
13090 goto fail;
13091 }
13092
13093 DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
13094 retry = false;
13095 goto encoder_retry;
13096 }
13097
e8fa4270
SV
13098 /* Dithering seems to not pass-through bits correctly when it should, so
13099 * only enable it on 6bpc panels. */
13100 pipe_config->dither = pipe_config->pipe_bpp == 6*3;
62f0ace5 13101 DRM_DEBUG_KMS("hw max bpp: %i, pipe bpp: %i, dithering: %i\n",
d328c9d7 13102 base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
4e53c2e0 13103
7758a113 13104fail:
548ee15b 13105 return ret;
ee7b9f93 13106}
47f1c6c9 13107
ea9d758d 13108static void
4740b0f2 13109intel_modeset_update_crtc_state(struct drm_atomic_state *state)
ea9d758d 13110{
0a9ab303
ACO
13111 struct drm_crtc *crtc;
13112 struct drm_crtc_state *crtc_state;
8a75d157 13113 int i;
ea9d758d 13114
7668851f 13115 /* Double check state. */
8a75d157 13116 for_each_crtc_in_state(state, crtc, crtc_state, i) {
3cb480bc 13117 to_intel_crtc(crtc)->config = to_intel_crtc_state(crtc->state);
fc467a22
ML
13118
13119 /* Update hwmode for vblank functions */
13120 if (crtc->state->active)
13121 crtc->hwmode = crtc->state->adjusted_mode;
13122 else
13123 crtc->hwmode.crtc_clock = 0;
61067a5e
ML
13124
13125 /*
13126 * Update legacy state to satisfy fbc code. This can
13127 * be removed when fbc uses the atomic state.
13128 */
13129 if (drm_atomic_get_existing_plane_state(state, crtc->primary)) {
13130 struct drm_plane_state *plane_state = crtc->primary->state;
13131
13132 crtc->primary->fb = plane_state->fb;
13133 crtc->x = plane_state->src_x >> 16;
13134 crtc->y = plane_state->src_y >> 16;
13135 }
ea9d758d 13136 }
ea9d758d
SV
13137}
13138
3bd26263 13139static bool intel_fuzzy_clock_check(int clock1, int clock2)
f1f644dc 13140{
3bd26263 13141 int diff;
f1f644dc
JB
13142
13143 if (clock1 == clock2)
13144 return true;
13145
13146 if (!clock1 || !clock2)
13147 return false;
13148
13149 diff = abs(clock1 - clock2);
13150
13151 if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
13152 return true;
13153
13154 return false;
13155}
13156
cfb23ed6
ML
13157static bool
13158intel_compare_m_n(unsigned int m, unsigned int n,
13159 unsigned int m2, unsigned int n2,
13160 bool exact)
13161{
13162 if (m == m2 && n == n2)
13163 return true;
13164
13165 if (exact || !m || !n || !m2 || !n2)
13166 return false;
13167
13168 BUILD_BUG_ON(DATA_LINK_M_N_MASK > INT_MAX);
13169
31d10b57
ML
13170 if (n > n2) {
13171 while (n > n2) {
cfb23ed6
ML
13172 m2 <<= 1;
13173 n2 <<= 1;
13174 }
31d10b57
ML
13175 } else if (n < n2) {
13176 while (n < n2) {
cfb23ed6
ML
13177 m <<= 1;
13178 n <<= 1;
13179 }
13180 }
13181
31d10b57
ML
13182 if (n != n2)
13183 return false;
13184
13185 return intel_fuzzy_clock_check(m, m2);
cfb23ed6
ML
13186}
13187
13188static bool
13189intel_compare_link_m_n(const struct intel_link_m_n *m_n,
13190 struct intel_link_m_n *m2_n2,
13191 bool adjust)
13192{
13193 if (m_n->tu == m2_n2->tu &&
13194 intel_compare_m_n(m_n->gmch_m, m_n->gmch_n,
13195 m2_n2->gmch_m, m2_n2->gmch_n, !adjust) &&
13196 intel_compare_m_n(m_n->link_m, m_n->link_n,
13197 m2_n2->link_m, m2_n2->link_n, !adjust)) {
13198 if (adjust)
13199 *m2_n2 = *m_n;
13200
13201 return true;
13202 }
13203
13204 return false;
13205}
13206
0e8ffe1b 13207static bool
2fa2fe9a 13208intel_pipe_config_compare(struct drm_device *dev,
5cec258b 13209 struct intel_crtc_state *current_config,
cfb23ed6
ML
13210 struct intel_crtc_state *pipe_config,
13211 bool adjust)
0e8ffe1b 13212{
772c2a51 13213 struct drm_i915_private *dev_priv = to_i915(dev);
cfb23ed6
ML
13214 bool ret = true;
13215
13216#define INTEL_ERR_OR_DBG_KMS(fmt, ...) \
13217 do { \
13218 if (!adjust) \
13219 DRM_ERROR(fmt, ##__VA_ARGS__); \
13220 else \
13221 DRM_DEBUG_KMS(fmt, ##__VA_ARGS__); \
13222 } while (0)
13223
66e985c0
SV
13224#define PIPE_CONF_CHECK_X(name) \
13225 if (current_config->name != pipe_config->name) { \
cfb23ed6 13226 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
66e985c0
SV
13227 "(expected 0x%08x, found 0x%08x)\n", \
13228 current_config->name, \
13229 pipe_config->name); \
cfb23ed6 13230 ret = false; \
66e985c0
SV
13231 }
13232
08a24034
SV
13233#define PIPE_CONF_CHECK_I(name) \
13234 if (current_config->name != pipe_config->name) { \
cfb23ed6 13235 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
08a24034
SV
13236 "(expected %i, found %i)\n", \
13237 current_config->name, \
13238 pipe_config->name); \
cfb23ed6
ML
13239 ret = false; \
13240 }
13241
8106ddbd
ACO
13242#define PIPE_CONF_CHECK_P(name) \
13243 if (current_config->name != pipe_config->name) { \
13244 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
13245 "(expected %p, found %p)\n", \
13246 current_config->name, \
13247 pipe_config->name); \
13248 ret = false; \
13249 }
13250
cfb23ed6
ML
13251#define PIPE_CONF_CHECK_M_N(name) \
13252 if (!intel_compare_link_m_n(&current_config->name, \
13253 &pipe_config->name,\
13254 adjust)) { \
13255 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
13256 "(expected 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 pipe_config->name.tu, \
13264 pipe_config->name.gmch_m, \
13265 pipe_config->name.gmch_n, \
13266 pipe_config->name.link_m, \
13267 pipe_config->name.link_n); \
13268 ret = false; \
13269 }
13270
55c561a7
SV
13271/* This is required for BDW+ where there is only one set of registers for
13272 * switching between high and low RR.
13273 * This macro can be used whenever a comparison has to be made between one
13274 * hw state and multiple sw state variables.
13275 */
cfb23ed6
ML
13276#define PIPE_CONF_CHECK_M_N_ALT(name, alt_name) \
13277 if (!intel_compare_link_m_n(&current_config->name, \
13278 &pipe_config->name, adjust) && \
13279 !intel_compare_link_m_n(&current_config->alt_name, \
13280 &pipe_config->name, adjust)) { \
13281 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
13282 "(expected tu %i gmch %i/%i link %i/%i, " \
13283 "or tu %i gmch %i/%i link %i/%i, " \
13284 "found tu %i, gmch %i/%i link %i/%i)\n", \
13285 current_config->name.tu, \
13286 current_config->name.gmch_m, \
13287 current_config->name.gmch_n, \
13288 current_config->name.link_m, \
13289 current_config->name.link_n, \
13290 current_config->alt_name.tu, \
13291 current_config->alt_name.gmch_m, \
13292 current_config->alt_name.gmch_n, \
13293 current_config->alt_name.link_m, \
13294 current_config->alt_name.link_n, \
13295 pipe_config->name.tu, \
13296 pipe_config->name.gmch_m, \
13297 pipe_config->name.gmch_n, \
13298 pipe_config->name.link_m, \
13299 pipe_config->name.link_n); \
13300 ret = false; \
88adfff1
SV
13301 }
13302
1bd1bd80
SV
13303#define PIPE_CONF_CHECK_FLAGS(name, mask) \
13304 if ((current_config->name ^ pipe_config->name) & (mask)) { \
cfb23ed6 13305 INTEL_ERR_OR_DBG_KMS("mismatch in " #name "(" #mask ") " \
1bd1bd80
SV
13306 "(expected %i, found %i)\n", \
13307 current_config->name & (mask), \
13308 pipe_config->name & (mask)); \
cfb23ed6 13309 ret = false; \
1bd1bd80
SV
13310 }
13311
5e550656
VS
13312#define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
13313 if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
cfb23ed6 13314 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
5e550656
VS
13315 "(expected %i, found %i)\n", \
13316 current_config->name, \
13317 pipe_config->name); \
cfb23ed6 13318 ret = false; \
5e550656
VS
13319 }
13320
bb760063
SV
13321#define PIPE_CONF_QUIRK(quirk) \
13322 ((current_config->quirks | pipe_config->quirks) & (quirk))
13323
eccb140b
SV
13324 PIPE_CONF_CHECK_I(cpu_transcoder);
13325
08a24034
SV
13326 PIPE_CONF_CHECK_I(has_pch_encoder);
13327 PIPE_CONF_CHECK_I(fdi_lanes);
cfb23ed6 13328 PIPE_CONF_CHECK_M_N(fdi_m_n);
08a24034 13329
90a6b7b0 13330 PIPE_CONF_CHECK_I(lane_count);
95a7a2ae 13331 PIPE_CONF_CHECK_X(lane_lat_optim_mask);
b95af8be
VK
13332
13333 if (INTEL_INFO(dev)->gen < 8) {
cfb23ed6
ML
13334 PIPE_CONF_CHECK_M_N(dp_m_n);
13335
cfb23ed6
ML
13336 if (current_config->has_drrs)
13337 PIPE_CONF_CHECK_M_N(dp_m2_n2);
13338 } else
13339 PIPE_CONF_CHECK_M_N_ALT(dp_m_n, dp_m2_n2);
eb14cb74 13340
253c84c8 13341 PIPE_CONF_CHECK_X(output_types);
a65347ba 13342
2d112de7
ACO
13343 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
13344 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
13345 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
13346 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
13347 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
13348 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
1bd1bd80 13349
2d112de7
ACO
13350 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
13351 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
13352 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
13353 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
13354 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
13355 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
1bd1bd80 13356
c93f54cf 13357 PIPE_CONF_CHECK_I(pixel_multiplier);
6897b4b5 13358 PIPE_CONF_CHECK_I(has_hdmi_sink);
772c2a51 13359 if ((INTEL_GEN(dev_priv) < 8 && !IS_HASWELL(dev_priv)) ||
920a14b2 13360 IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
b5a9fa09 13361 PIPE_CONF_CHECK_I(limited_color_range);
e43823ec 13362 PIPE_CONF_CHECK_I(has_infoframe);
6c49f241 13363
9ed109a7
SV
13364 PIPE_CONF_CHECK_I(has_audio);
13365
2d112de7 13366 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
1bd1bd80
SV
13367 DRM_MODE_FLAG_INTERLACE);
13368
bb760063 13369 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
2d112de7 13370 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063 13371 DRM_MODE_FLAG_PHSYNC);
2d112de7 13372 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063 13373 DRM_MODE_FLAG_NHSYNC);
2d112de7 13374 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063 13375 DRM_MODE_FLAG_PVSYNC);
2d112de7 13376 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063
SV
13377 DRM_MODE_FLAG_NVSYNC);
13378 }
045ac3b5 13379
333b8ca8 13380 PIPE_CONF_CHECK_X(gmch_pfit.control);
e2ff2d4a
SV
13381 /* pfit ratios are autocomputed by the hw on gen4+ */
13382 if (INTEL_INFO(dev)->gen < 4)
7f7d8dd6 13383 PIPE_CONF_CHECK_X(gmch_pfit.pgm_ratios);
333b8ca8 13384 PIPE_CONF_CHECK_X(gmch_pfit.lvds_border_bits);
9953599b 13385
bfd16b2a
ML
13386 if (!adjust) {
13387 PIPE_CONF_CHECK_I(pipe_src_w);
13388 PIPE_CONF_CHECK_I(pipe_src_h);
13389
13390 PIPE_CONF_CHECK_I(pch_pfit.enabled);
13391 if (current_config->pch_pfit.enabled) {
13392 PIPE_CONF_CHECK_X(pch_pfit.pos);
13393 PIPE_CONF_CHECK_X(pch_pfit.size);
13394 }
2fa2fe9a 13395
7aefe2b5
ML
13396 PIPE_CONF_CHECK_I(scaler_state.scaler_id);
13397 }
a1b2278e 13398
e59150dc 13399 /* BDW+ don't expose a synchronous way to read the state */
772c2a51 13400 if (IS_HASWELL(dev_priv))
e59150dc 13401 PIPE_CONF_CHECK_I(ips_enabled);
42db64ef 13402
282740f7
VS
13403 PIPE_CONF_CHECK_I(double_wide);
13404
8106ddbd 13405 PIPE_CONF_CHECK_P(shared_dpll);
66e985c0 13406 PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
8bcc2795 13407 PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
66e985c0
SV
13408 PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
13409 PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
d452c5b6 13410 PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
00490c22 13411 PIPE_CONF_CHECK_X(dpll_hw_state.spll);
3f4cd19f
DL
13412 PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
13413 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
13414 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
c0d43d62 13415
47eacbab
VS
13416 PIPE_CONF_CHECK_X(dsi_pll.ctrl);
13417 PIPE_CONF_CHECK_X(dsi_pll.div);
13418
9beb5fea 13419 if (IS_G4X(dev_priv) || INTEL_GEN(dev_priv) >= 5)
42571aef
VS
13420 PIPE_CONF_CHECK_I(pipe_bpp);
13421
2d112de7 13422 PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
a9a7e98a 13423 PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
5e550656 13424
66e985c0 13425#undef PIPE_CONF_CHECK_X
08a24034 13426#undef PIPE_CONF_CHECK_I
8106ddbd 13427#undef PIPE_CONF_CHECK_P
1bd1bd80 13428#undef PIPE_CONF_CHECK_FLAGS
5e550656 13429#undef PIPE_CONF_CHECK_CLOCK_FUZZY
bb760063 13430#undef PIPE_CONF_QUIRK
cfb23ed6 13431#undef INTEL_ERR_OR_DBG_KMS
88adfff1 13432
cfb23ed6 13433 return ret;
0e8ffe1b
SV
13434}
13435
e3b247da
VS
13436static void intel_pipe_config_sanity_check(struct drm_i915_private *dev_priv,
13437 const struct intel_crtc_state *pipe_config)
13438{
13439 if (pipe_config->has_pch_encoder) {
21a727b3 13440 int fdi_dotclock = intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
e3b247da
VS
13441 &pipe_config->fdi_m_n);
13442 int dotclock = pipe_config->base.adjusted_mode.crtc_clock;
13443
13444 /*
13445 * FDI already provided one idea for the dotclock.
13446 * Yell if the encoder disagrees.
13447 */
13448 WARN(!intel_fuzzy_clock_check(fdi_dotclock, dotclock),
13449 "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
13450 fdi_dotclock, dotclock);
13451 }
13452}
13453
c0ead703
ML
13454static void verify_wm_state(struct drm_crtc *crtc,
13455 struct drm_crtc_state *new_state)
08db6652 13456{
e7c84544 13457 struct drm_device *dev = crtc->dev;
fac5e23e 13458 struct drm_i915_private *dev_priv = to_i915(dev);
08db6652 13459 struct skl_ddb_allocation hw_ddb, *sw_ddb;
3de8a14c 13460 struct skl_pipe_wm hw_wm, *sw_wm;
13461 struct skl_plane_wm *hw_plane_wm, *sw_plane_wm;
13462 struct skl_ddb_entry *hw_ddb_entry, *sw_ddb_entry;
e7c84544
ML
13463 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13464 const enum pipe pipe = intel_crtc->pipe;
3de8a14c 13465 int plane, level, max_level = ilk_wm_max_level(dev_priv);
08db6652 13466
e7c84544 13467 if (INTEL_INFO(dev)->gen < 9 || !new_state->active)
08db6652
DL
13468 return;
13469
3de8a14c 13470 skl_pipe_wm_get_hw_state(crtc, &hw_wm);
03af79e0 13471 sw_wm = &to_intel_crtc_state(new_state)->wm.skl.optimal;
3de8a14c 13472
08db6652
DL
13473 skl_ddb_get_hw_state(dev_priv, &hw_ddb);
13474 sw_ddb = &dev_priv->wm.skl_hw.ddb;
13475
e7c84544 13476 /* planes */
8b364b41 13477 for_each_universal_plane(dev_priv, pipe, plane) {
3de8a14c 13478 hw_plane_wm = &hw_wm.planes[plane];
13479 sw_plane_wm = &sw_wm->planes[plane];
08db6652 13480
3de8a14c 13481 /* Watermarks */
13482 for (level = 0; level <= max_level; level++) {
13483 if (skl_wm_level_equals(&hw_plane_wm->wm[level],
13484 &sw_plane_wm->wm[level]))
13485 continue;
13486
13487 DRM_ERROR("mismatch in WM pipe %c plane %d level %d (expected e=%d b=%u l=%u, got e=%d b=%u l=%u)\n",
13488 pipe_name(pipe), plane + 1, level,
13489 sw_plane_wm->wm[level].plane_en,
13490 sw_plane_wm->wm[level].plane_res_b,
13491 sw_plane_wm->wm[level].plane_res_l,
13492 hw_plane_wm->wm[level].plane_en,
13493 hw_plane_wm->wm[level].plane_res_b,
13494 hw_plane_wm->wm[level].plane_res_l);
13495 }
08db6652 13496
3de8a14c 13497 if (!skl_wm_level_equals(&hw_plane_wm->trans_wm,
13498 &sw_plane_wm->trans_wm)) {
13499 DRM_ERROR("mismatch in trans WM pipe %c plane %d (expected e=%d b=%u l=%u, got e=%d b=%u l=%u)\n",
13500 pipe_name(pipe), plane + 1,
13501 sw_plane_wm->trans_wm.plane_en,
13502 sw_plane_wm->trans_wm.plane_res_b,
13503 sw_plane_wm->trans_wm.plane_res_l,
13504 hw_plane_wm->trans_wm.plane_en,
13505 hw_plane_wm->trans_wm.plane_res_b,
13506 hw_plane_wm->trans_wm.plane_res_l);
13507 }
13508
13509 /* DDB */
13510 hw_ddb_entry = &hw_ddb.plane[pipe][plane];
13511 sw_ddb_entry = &sw_ddb->plane[pipe][plane];
13512
13513 if (!skl_ddb_entry_equal(hw_ddb_entry, sw_ddb_entry)) {
faccd994 13514 DRM_ERROR("mismatch in DDB state pipe %c plane %d (expected (%u,%u), found (%u,%u))\n",
3de8a14c 13515 pipe_name(pipe), plane + 1,
13516 sw_ddb_entry->start, sw_ddb_entry->end,
13517 hw_ddb_entry->start, hw_ddb_entry->end);
13518 }
e7c84544 13519 }
08db6652 13520
27082493
L
13521 /*
13522 * cursor
13523 * If the cursor plane isn't active, we may not have updated it's ddb
13524 * allocation. In that case since the ddb allocation will be updated
13525 * once the plane becomes visible, we can skip this check
13526 */
13527 if (intel_crtc->cursor_addr) {
3de8a14c 13528 hw_plane_wm = &hw_wm.planes[PLANE_CURSOR];
13529 sw_plane_wm = &sw_wm->planes[PLANE_CURSOR];
13530
13531 /* Watermarks */
13532 for (level = 0; level <= max_level; level++) {
13533 if (skl_wm_level_equals(&hw_plane_wm->wm[level],
13534 &sw_plane_wm->wm[level]))
13535 continue;
13536
13537 DRM_ERROR("mismatch in WM pipe %c cursor level %d (expected e=%d b=%u l=%u, got e=%d b=%u l=%u)\n",
13538 pipe_name(pipe), level,
13539 sw_plane_wm->wm[level].plane_en,
13540 sw_plane_wm->wm[level].plane_res_b,
13541 sw_plane_wm->wm[level].plane_res_l,
13542 hw_plane_wm->wm[level].plane_en,
13543 hw_plane_wm->wm[level].plane_res_b,
13544 hw_plane_wm->wm[level].plane_res_l);
13545 }
13546
13547 if (!skl_wm_level_equals(&hw_plane_wm->trans_wm,
13548 &sw_plane_wm->trans_wm)) {
13549 DRM_ERROR("mismatch in trans WM pipe %c cursor (expected e=%d b=%u l=%u, got e=%d b=%u l=%u)\n",
13550 pipe_name(pipe),
13551 sw_plane_wm->trans_wm.plane_en,
13552 sw_plane_wm->trans_wm.plane_res_b,
13553 sw_plane_wm->trans_wm.plane_res_l,
13554 hw_plane_wm->trans_wm.plane_en,
13555 hw_plane_wm->trans_wm.plane_res_b,
13556 hw_plane_wm->trans_wm.plane_res_l);
13557 }
13558
13559 /* DDB */
13560 hw_ddb_entry = &hw_ddb.plane[pipe][PLANE_CURSOR];
13561 sw_ddb_entry = &sw_ddb->plane[pipe][PLANE_CURSOR];
27082493 13562
3de8a14c 13563 if (!skl_ddb_entry_equal(hw_ddb_entry, sw_ddb_entry)) {
faccd994 13564 DRM_ERROR("mismatch in DDB state pipe %c cursor (expected (%u,%u), found (%u,%u))\n",
27082493 13565 pipe_name(pipe),
3de8a14c 13566 sw_ddb_entry->start, sw_ddb_entry->end,
13567 hw_ddb_entry->start, hw_ddb_entry->end);
27082493 13568 }
08db6652
DL
13569 }
13570}
13571
91d1b4bd 13572static void
677100ce
ML
13573verify_connector_state(struct drm_device *dev,
13574 struct drm_atomic_state *state,
13575 struct drm_crtc *crtc)
8af6cf88 13576{
35dd3c64 13577 struct drm_connector *connector;
677100ce
ML
13578 struct drm_connector_state *old_conn_state;
13579 int i;
8af6cf88 13580
677100ce 13581 for_each_connector_in_state(state, connector, old_conn_state, i) {
35dd3c64
ML
13582 struct drm_encoder *encoder = connector->encoder;
13583 struct drm_connector_state *state = connector->state;
ad3c558f 13584
e7c84544
ML
13585 if (state->crtc != crtc)
13586 continue;
13587
5a21b665 13588 intel_connector_verify_state(to_intel_connector(connector));
8af6cf88 13589
ad3c558f 13590 I915_STATE_WARN(state->best_encoder != encoder,
35dd3c64 13591 "connector's atomic encoder doesn't match legacy encoder\n");
8af6cf88 13592 }
91d1b4bd
SV
13593}
13594
13595static void
c0ead703 13596verify_encoder_state(struct drm_device *dev)
91d1b4bd
SV
13597{
13598 struct intel_encoder *encoder;
13599 struct intel_connector *connector;
8af6cf88 13600
b2784e15 13601 for_each_intel_encoder(dev, encoder) {
8af6cf88 13602 bool enabled = false;
4d20cd86 13603 enum pipe pipe;
8af6cf88
SV
13604
13605 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
13606 encoder->base.base.id,
8e329a03 13607 encoder->base.name);
8af6cf88 13608
3a3371ff 13609 for_each_intel_connector(dev, connector) {
4d20cd86 13610 if (connector->base.state->best_encoder != &encoder->base)
8af6cf88
SV
13611 continue;
13612 enabled = true;
ad3c558f
ML
13613
13614 I915_STATE_WARN(connector->base.state->crtc !=
13615 encoder->base.crtc,
13616 "connector's crtc doesn't match encoder crtc\n");
8af6cf88 13617 }
0e32b39c 13618
e2c719b7 13619 I915_STATE_WARN(!!encoder->base.crtc != enabled,
8af6cf88
SV
13620 "encoder's enabled state mismatch "
13621 "(expected %i, found %i)\n",
13622 !!encoder->base.crtc, enabled);
7c60d198
ML
13623
13624 if (!encoder->base.crtc) {
4d20cd86 13625 bool active;
7c60d198 13626
4d20cd86
ML
13627 active = encoder->get_hw_state(encoder, &pipe);
13628 I915_STATE_WARN(active,
13629 "encoder detached but still enabled on pipe %c.\n",
13630 pipe_name(pipe));
7c60d198 13631 }
8af6cf88 13632 }
91d1b4bd
SV
13633}
13634
13635static void
c0ead703
ML
13636verify_crtc_state(struct drm_crtc *crtc,
13637 struct drm_crtc_state *old_crtc_state,
13638 struct drm_crtc_state *new_crtc_state)
91d1b4bd 13639{
e7c84544 13640 struct drm_device *dev = crtc->dev;
fac5e23e 13641 struct drm_i915_private *dev_priv = to_i915(dev);
91d1b4bd 13642 struct intel_encoder *encoder;
e7c84544
ML
13643 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13644 struct intel_crtc_state *pipe_config, *sw_config;
13645 struct drm_atomic_state *old_state;
13646 bool active;
045ac3b5 13647
e7c84544 13648 old_state = old_crtc_state->state;
ec2dc6a0 13649 __drm_atomic_helper_crtc_destroy_state(old_crtc_state);
e7c84544
ML
13650 pipe_config = to_intel_crtc_state(old_crtc_state);
13651 memset(pipe_config, 0, sizeof(*pipe_config));
13652 pipe_config->base.crtc = crtc;
13653 pipe_config->base.state = old_state;
8af6cf88 13654
78108b7c 13655 DRM_DEBUG_KMS("[CRTC:%d:%s]\n", crtc->base.id, crtc->name);
8af6cf88 13656
e7c84544 13657 active = dev_priv->display.get_pipe_config(intel_crtc, pipe_config);
d62cf62a 13658
e7c84544
ML
13659 /* hw state is inconsistent with the pipe quirk */
13660 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
13661 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
13662 active = new_crtc_state->active;
6c49f241 13663
e7c84544
ML
13664 I915_STATE_WARN(new_crtc_state->active != active,
13665 "crtc active state doesn't match with hw state "
13666 "(expected %i, found %i)\n", new_crtc_state->active, active);
0e8ffe1b 13667
e7c84544
ML
13668 I915_STATE_WARN(intel_crtc->active != new_crtc_state->active,
13669 "transitional active state does not match atomic hw state "
13670 "(expected %i, found %i)\n", new_crtc_state->active, intel_crtc->active);
4d20cd86 13671
e7c84544
ML
13672 for_each_encoder_on_crtc(dev, crtc, encoder) {
13673 enum pipe pipe;
4d20cd86 13674
e7c84544
ML
13675 active = encoder->get_hw_state(encoder, &pipe);
13676 I915_STATE_WARN(active != new_crtc_state->active,
13677 "[ENCODER:%i] active %i with crtc active %i\n",
13678 encoder->base.base.id, active, new_crtc_state->active);
4d20cd86 13679
e7c84544
ML
13680 I915_STATE_WARN(active && intel_crtc->pipe != pipe,
13681 "Encoder connected to wrong pipe %c\n",
13682 pipe_name(pipe));
4d20cd86 13683
253c84c8
VS
13684 if (active) {
13685 pipe_config->output_types |= 1 << encoder->type;
e7c84544 13686 encoder->get_config(encoder, pipe_config);
253c84c8 13687 }
e7c84544 13688 }
53d9f4e9 13689
e7c84544
ML
13690 if (!new_crtc_state->active)
13691 return;
cfb23ed6 13692
e7c84544 13693 intel_pipe_config_sanity_check(dev_priv, pipe_config);
e3b247da 13694
e7c84544
ML
13695 sw_config = to_intel_crtc_state(crtc->state);
13696 if (!intel_pipe_config_compare(dev, sw_config,
13697 pipe_config, false)) {
13698 I915_STATE_WARN(1, "pipe state doesn't match!\n");
13699 intel_dump_pipe_config(intel_crtc, pipe_config,
13700 "[hw state]");
13701 intel_dump_pipe_config(intel_crtc, sw_config,
13702 "[sw state]");
8af6cf88
SV
13703 }
13704}
13705
91d1b4bd 13706static void
c0ead703
ML
13707verify_single_dpll_state(struct drm_i915_private *dev_priv,
13708 struct intel_shared_dpll *pll,
13709 struct drm_crtc *crtc,
13710 struct drm_crtc_state *new_state)
91d1b4bd 13711{
91d1b4bd 13712 struct intel_dpll_hw_state dpll_hw_state;
e7c84544
ML
13713 unsigned crtc_mask;
13714 bool active;
5358901f 13715
e7c84544 13716 memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
5358901f 13717
e7c84544 13718 DRM_DEBUG_KMS("%s\n", pll->name);
5358901f 13719
e7c84544 13720 active = pll->funcs.get_hw_state(dev_priv, pll, &dpll_hw_state);
5358901f 13721
e7c84544
ML
13722 if (!(pll->flags & INTEL_DPLL_ALWAYS_ON)) {
13723 I915_STATE_WARN(!pll->on && pll->active_mask,
13724 "pll in active use but not on in sw tracking\n");
13725 I915_STATE_WARN(pll->on && !pll->active_mask,
13726 "pll is on but not used by any active crtc\n");
13727 I915_STATE_WARN(pll->on != active,
13728 "pll on state mismatch (expected %i, found %i)\n",
13729 pll->on, active);
13730 }
5358901f 13731
e7c84544 13732 if (!crtc) {
2dd66ebd 13733 I915_STATE_WARN(pll->active_mask & ~pll->config.crtc_mask,
e7c84544
ML
13734 "more active pll users than references: %x vs %x\n",
13735 pll->active_mask, pll->config.crtc_mask);
5358901f 13736
e7c84544
ML
13737 return;
13738 }
13739
13740 crtc_mask = 1 << drm_crtc_index(crtc);
13741
13742 if (new_state->active)
13743 I915_STATE_WARN(!(pll->active_mask & crtc_mask),
13744 "pll active mismatch (expected pipe %c in active mask 0x%02x)\n",
13745 pipe_name(drm_crtc_index(crtc)), pll->active_mask);
13746 else
13747 I915_STATE_WARN(pll->active_mask & crtc_mask,
13748 "pll active mismatch (didn't expect pipe %c in active mask 0x%02x)\n",
13749 pipe_name(drm_crtc_index(crtc)), pll->active_mask);
2dd66ebd 13750
e7c84544
ML
13751 I915_STATE_WARN(!(pll->config.crtc_mask & crtc_mask),
13752 "pll enabled crtcs mismatch (expected 0x%x in 0x%02x)\n",
13753 crtc_mask, pll->config.crtc_mask);
66e985c0 13754
e7c84544
ML
13755 I915_STATE_WARN(pll->on && memcmp(&pll->config.hw_state,
13756 &dpll_hw_state,
13757 sizeof(dpll_hw_state)),
13758 "pll hw state mismatch\n");
13759}
13760
13761static void
c0ead703
ML
13762verify_shared_dpll_state(struct drm_device *dev, struct drm_crtc *crtc,
13763 struct drm_crtc_state *old_crtc_state,
13764 struct drm_crtc_state *new_crtc_state)
e7c84544 13765{
fac5e23e 13766 struct drm_i915_private *dev_priv = to_i915(dev);
e7c84544
ML
13767 struct intel_crtc_state *old_state = to_intel_crtc_state(old_crtc_state);
13768 struct intel_crtc_state *new_state = to_intel_crtc_state(new_crtc_state);
13769
13770 if (new_state->shared_dpll)
c0ead703 13771 verify_single_dpll_state(dev_priv, new_state->shared_dpll, crtc, new_crtc_state);
e7c84544
ML
13772
13773 if (old_state->shared_dpll &&
13774 old_state->shared_dpll != new_state->shared_dpll) {
13775 unsigned crtc_mask = 1 << drm_crtc_index(crtc);
13776 struct intel_shared_dpll *pll = old_state->shared_dpll;
13777
13778 I915_STATE_WARN(pll->active_mask & crtc_mask,
13779 "pll active mismatch (didn't expect pipe %c in active mask)\n",
13780 pipe_name(drm_crtc_index(crtc)));
13781 I915_STATE_WARN(pll->config.crtc_mask & crtc_mask,
13782 "pll enabled crtcs mismatch (found %x in enabled mask)\n",
13783 pipe_name(drm_crtc_index(crtc)));
5358901f 13784 }
8af6cf88
SV
13785}
13786
e7c84544 13787static void
c0ead703 13788intel_modeset_verify_crtc(struct drm_crtc *crtc,
677100ce
ML
13789 struct drm_atomic_state *state,
13790 struct drm_crtc_state *old_state,
13791 struct drm_crtc_state *new_state)
e7c84544 13792{
5a21b665
SV
13793 if (!needs_modeset(new_state) &&
13794 !to_intel_crtc_state(new_state)->update_pipe)
13795 return;
13796
c0ead703 13797 verify_wm_state(crtc, new_state);
677100ce 13798 verify_connector_state(crtc->dev, state, crtc);
c0ead703
ML
13799 verify_crtc_state(crtc, old_state, new_state);
13800 verify_shared_dpll_state(crtc->dev, crtc, old_state, new_state);
e7c84544
ML
13801}
13802
13803static void
c0ead703 13804verify_disabled_dpll_state(struct drm_device *dev)
e7c84544 13805{
fac5e23e 13806 struct drm_i915_private *dev_priv = to_i915(dev);
e7c84544
ML
13807 int i;
13808
13809 for (i = 0; i < dev_priv->num_shared_dpll; i++)
c0ead703 13810 verify_single_dpll_state(dev_priv, &dev_priv->shared_dplls[i], NULL, NULL);
e7c84544
ML
13811}
13812
13813static void
677100ce
ML
13814intel_modeset_verify_disabled(struct drm_device *dev,
13815 struct drm_atomic_state *state)
e7c84544 13816{
c0ead703 13817 verify_encoder_state(dev);
677100ce 13818 verify_connector_state(dev, state, NULL);
c0ead703 13819 verify_disabled_dpll_state(dev);
e7c84544
ML
13820}
13821
80715b2f
VS
13822static void update_scanline_offset(struct intel_crtc *crtc)
13823{
4f8036a2 13824 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
80715b2f
VS
13825
13826 /*
13827 * The scanline counter increments at the leading edge of hsync.
13828 *
13829 * On most platforms it starts counting from vtotal-1 on the
13830 * first active line. That means the scanline counter value is
13831 * always one less than what we would expect. Ie. just after
13832 * start of vblank, which also occurs at start of hsync (on the
13833 * last active line), the scanline counter will read vblank_start-1.
13834 *
13835 * On gen2 the scanline counter starts counting from 1 instead
13836 * of vtotal-1, so we have to subtract one (or rather add vtotal-1
13837 * to keep the value positive), instead of adding one.
13838 *
13839 * On HSW+ the behaviour of the scanline counter depends on the output
13840 * type. For DP ports it behaves like most other platforms, but on HDMI
13841 * there's an extra 1 line difference. So we need to add two instead of
13842 * one to the value.
13843 */
4f8036a2 13844 if (IS_GEN2(dev_priv)) {
124abe07 13845 const struct drm_display_mode *adjusted_mode = &crtc->config->base.adjusted_mode;
80715b2f
VS
13846 int vtotal;
13847
124abe07
VS
13848 vtotal = adjusted_mode->crtc_vtotal;
13849 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
80715b2f
VS
13850 vtotal /= 2;
13851
13852 crtc->scanline_offset = vtotal - 1;
4f8036a2 13853 } else if (HAS_DDI(dev_priv) &&
2d84d2b3 13854 intel_crtc_has_type(crtc->config, INTEL_OUTPUT_HDMI)) {
80715b2f
VS
13855 crtc->scanline_offset = 2;
13856 } else
13857 crtc->scanline_offset = 1;
13858}
13859
ad421372 13860static void intel_modeset_clear_plls(struct drm_atomic_state *state)
ed6739ef 13861{
225da59b 13862 struct drm_device *dev = state->dev;
ed6739ef 13863 struct drm_i915_private *dev_priv = to_i915(dev);
ad421372 13864 struct intel_shared_dpll_config *shared_dpll = NULL;
0a9ab303
ACO
13865 struct drm_crtc *crtc;
13866 struct drm_crtc_state *crtc_state;
0a9ab303 13867 int i;
ed6739ef
ACO
13868
13869 if (!dev_priv->display.crtc_compute_clock)
ad421372 13870 return;
ed6739ef 13871
0a9ab303 13872 for_each_crtc_in_state(state, crtc, crtc_state, i) {
fb1a38a9 13873 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8106ddbd
ACO
13874 struct intel_shared_dpll *old_dpll =
13875 to_intel_crtc_state(crtc->state)->shared_dpll;
0a9ab303 13876
fb1a38a9 13877 if (!needs_modeset(crtc_state))
225da59b
ACO
13878 continue;
13879
8106ddbd 13880 to_intel_crtc_state(crtc_state)->shared_dpll = NULL;
fb1a38a9 13881
8106ddbd 13882 if (!old_dpll)
fb1a38a9 13883 continue;
0a9ab303 13884
ad421372
ML
13885 if (!shared_dpll)
13886 shared_dpll = intel_atomic_get_shared_dpll_state(state);
ed6739ef 13887
8106ddbd 13888 intel_shared_dpll_config_put(shared_dpll, old_dpll, intel_crtc);
ad421372 13889 }
ed6739ef
ACO
13890}
13891
99d736a2
ML
13892/*
13893 * This implements the workaround described in the "notes" section of the mode
13894 * set sequence documentation. When going from no pipes or single pipe to
13895 * multiple pipes, and planes are enabled after the pipe, we need to wait at
13896 * least 2 vblanks on the first pipe before enabling planes on the second pipe.
13897 */
13898static int haswell_mode_set_planes_workaround(struct drm_atomic_state *state)
13899{
13900 struct drm_crtc_state *crtc_state;
13901 struct intel_crtc *intel_crtc;
13902 struct drm_crtc *crtc;
13903 struct intel_crtc_state *first_crtc_state = NULL;
13904 struct intel_crtc_state *other_crtc_state = NULL;
13905 enum pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
13906 int i;
13907
13908 /* look at all crtc's that are going to be enabled in during modeset */
13909 for_each_crtc_in_state(state, crtc, crtc_state, i) {
13910 intel_crtc = to_intel_crtc(crtc);
13911
13912 if (!crtc_state->active || !needs_modeset(crtc_state))
13913 continue;
13914
13915 if (first_crtc_state) {
13916 other_crtc_state = to_intel_crtc_state(crtc_state);
13917 break;
13918 } else {
13919 first_crtc_state = to_intel_crtc_state(crtc_state);
13920 first_pipe = intel_crtc->pipe;
13921 }
13922 }
13923
13924 /* No workaround needed? */
13925 if (!first_crtc_state)
13926 return 0;
13927
13928 /* w/a possibly needed, check how many crtc's are already enabled. */
13929 for_each_intel_crtc(state->dev, intel_crtc) {
13930 struct intel_crtc_state *pipe_config;
13931
13932 pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
13933 if (IS_ERR(pipe_config))
13934 return PTR_ERR(pipe_config);
13935
13936 pipe_config->hsw_workaround_pipe = INVALID_PIPE;
13937
13938 if (!pipe_config->base.active ||
13939 needs_modeset(&pipe_config->base))
13940 continue;
13941
13942 /* 2 or more enabled crtcs means no need for w/a */
13943 if (enabled_pipe != INVALID_PIPE)
13944 return 0;
13945
13946 enabled_pipe = intel_crtc->pipe;
13947 }
13948
13949 if (enabled_pipe != INVALID_PIPE)
13950 first_crtc_state->hsw_workaround_pipe = enabled_pipe;
13951 else if (other_crtc_state)
13952 other_crtc_state->hsw_workaround_pipe = first_pipe;
13953
13954 return 0;
13955}
13956
27c329ed
ML
13957static int intel_modeset_all_pipes(struct drm_atomic_state *state)
13958{
13959 struct drm_crtc *crtc;
13960 struct drm_crtc_state *crtc_state;
13961 int ret = 0;
13962
13963 /* add all active pipes to the state */
13964 for_each_crtc(state->dev, crtc) {
13965 crtc_state = drm_atomic_get_crtc_state(state, crtc);
13966 if (IS_ERR(crtc_state))
13967 return PTR_ERR(crtc_state);
13968
13969 if (!crtc_state->active || needs_modeset(crtc_state))
13970 continue;
13971
13972 crtc_state->mode_changed = true;
13973
13974 ret = drm_atomic_add_affected_connectors(state, crtc);
13975 if (ret)
13976 break;
13977
13978 ret = drm_atomic_add_affected_planes(state, crtc);
13979 if (ret)
13980 break;
13981 }
13982
13983 return ret;
13984}
13985
c347a676 13986static int intel_modeset_checks(struct drm_atomic_state *state)
054518dd 13987{
565602d7 13988 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
fac5e23e 13989 struct drm_i915_private *dev_priv = to_i915(state->dev);
565602d7
ML
13990 struct drm_crtc *crtc;
13991 struct drm_crtc_state *crtc_state;
13992 int ret = 0, i;
054518dd 13993
b359283a
ML
13994 if (!check_digital_port_conflicts(state)) {
13995 DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
13996 return -EINVAL;
13997 }
13998
565602d7
ML
13999 intel_state->modeset = true;
14000 intel_state->active_crtcs = dev_priv->active_crtcs;
14001
14002 for_each_crtc_in_state(state, crtc, crtc_state, i) {
14003 if (crtc_state->active)
14004 intel_state->active_crtcs |= 1 << i;
14005 else
14006 intel_state->active_crtcs &= ~(1 << i);
8b4a7d05
MR
14007
14008 if (crtc_state->active != crtc->state->active)
14009 intel_state->active_pipe_changes |= drm_crtc_mask(crtc);
565602d7
ML
14010 }
14011
054518dd
ACO
14012 /*
14013 * See if the config requires any additional preparation, e.g.
14014 * to adjust global state with pipes off. We need to do this
14015 * here so we can get the modeset_pipe updated config for the new
14016 * mode set on this crtc. For other crtcs we need to use the
14017 * adjusted_mode bits in the crtc directly.
14018 */
27c329ed 14019 if (dev_priv->display.modeset_calc_cdclk) {
c89e39f3 14020 if (!intel_state->cdclk_pll_vco)
63911d72 14021 intel_state->cdclk_pll_vco = dev_priv->cdclk_pll.vco;
b2045352
VS
14022 if (!intel_state->cdclk_pll_vco)
14023 intel_state->cdclk_pll_vco = dev_priv->skl_preferred_vco_freq;
c89e39f3 14024
27c329ed 14025 ret = dev_priv->display.modeset_calc_cdclk(state);
c89e39f3
CT
14026 if (ret < 0)
14027 return ret;
27c329ed 14028
c89e39f3 14029 if (intel_state->dev_cdclk != dev_priv->cdclk_freq ||
63911d72 14030 intel_state->cdclk_pll_vco != dev_priv->cdclk_pll.vco)
27c329ed
ML
14031 ret = intel_modeset_all_pipes(state);
14032
14033 if (ret < 0)
054518dd 14034 return ret;
e8788cbc
ML
14035
14036 DRM_DEBUG_KMS("New cdclk calculated to be atomic %u, actual %u\n",
14037 intel_state->cdclk, intel_state->dev_cdclk);
27c329ed 14038 } else
1a617b77 14039 to_intel_atomic_state(state)->cdclk = dev_priv->atomic_cdclk_freq;
054518dd 14040
ad421372 14041 intel_modeset_clear_plls(state);
054518dd 14042
565602d7 14043 if (IS_HASWELL(dev_priv))
ad421372 14044 return haswell_mode_set_planes_workaround(state);
99d736a2 14045
ad421372 14046 return 0;
c347a676
ACO
14047}
14048
aa363136
MR
14049/*
14050 * Handle calculation of various watermark data at the end of the atomic check
14051 * phase. The code here should be run after the per-crtc and per-plane 'check'
14052 * handlers to ensure that all derived state has been updated.
14053 */
55994c2c 14054static int calc_watermark_data(struct drm_atomic_state *state)
aa363136
MR
14055{
14056 struct drm_device *dev = state->dev;
98d39494 14057 struct drm_i915_private *dev_priv = to_i915(dev);
98d39494
MR
14058
14059 /* Is there platform-specific watermark information to calculate? */
14060 if (dev_priv->display.compute_global_watermarks)
55994c2c
MR
14061 return dev_priv->display.compute_global_watermarks(state);
14062
14063 return 0;
aa363136
MR
14064}
14065
74c090b1
ML
14066/**
14067 * intel_atomic_check - validate state object
14068 * @dev: drm device
14069 * @state: state to validate
14070 */
14071static int intel_atomic_check(struct drm_device *dev,
14072 struct drm_atomic_state *state)
c347a676 14073{
dd8b3bdb 14074 struct drm_i915_private *dev_priv = to_i915(dev);
aa363136 14075 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
c347a676
ACO
14076 struct drm_crtc *crtc;
14077 struct drm_crtc_state *crtc_state;
14078 int ret, i;
61333b60 14079 bool any_ms = false;
c347a676 14080
74c090b1 14081 ret = drm_atomic_helper_check_modeset(dev, state);
054518dd
ACO
14082 if (ret)
14083 return ret;
14084
c347a676 14085 for_each_crtc_in_state(state, crtc, crtc_state, i) {
cfb23ed6
ML
14086 struct intel_crtc_state *pipe_config =
14087 to_intel_crtc_state(crtc_state);
1ed51de9
SV
14088
14089 /* Catch I915_MODE_FLAG_INHERITED */
14090 if (crtc_state->mode.private_flags != crtc->state->mode.private_flags)
14091 crtc_state->mode_changed = true;
cfb23ed6 14092
af4a879e 14093 if (!needs_modeset(crtc_state))
c347a676
ACO
14094 continue;
14095
af4a879e
SV
14096 if (!crtc_state->enable) {
14097 any_ms = true;
cfb23ed6 14098 continue;
af4a879e 14099 }
cfb23ed6 14100
26495481
SV
14101 /* FIXME: For only active_changed we shouldn't need to do any
14102 * state recomputation at all. */
14103
1ed51de9
SV
14104 ret = drm_atomic_add_affected_connectors(state, crtc);
14105 if (ret)
14106 return ret;
b359283a 14107
cfb23ed6 14108 ret = intel_modeset_pipe_config(crtc, pipe_config);
25aa1c39
ML
14109 if (ret) {
14110 intel_dump_pipe_config(to_intel_crtc(crtc),
14111 pipe_config, "[failed]");
c347a676 14112 return ret;
25aa1c39 14113 }
c347a676 14114
73831236 14115 if (i915.fastboot &&
dd8b3bdb 14116 intel_pipe_config_compare(dev,
cfb23ed6 14117 to_intel_crtc_state(crtc->state),
1ed51de9 14118 pipe_config, true)) {
26495481 14119 crtc_state->mode_changed = false;
bfd16b2a 14120 to_intel_crtc_state(crtc_state)->update_pipe = true;
26495481
SV
14121 }
14122
af4a879e 14123 if (needs_modeset(crtc_state))
26495481 14124 any_ms = true;
cfb23ed6 14125
af4a879e
SV
14126 ret = drm_atomic_add_affected_planes(state, crtc);
14127 if (ret)
14128 return ret;
61333b60 14129
26495481
SV
14130 intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
14131 needs_modeset(crtc_state) ?
14132 "[modeset]" : "[fastset]");
c347a676
ACO
14133 }
14134
61333b60
ML
14135 if (any_ms) {
14136 ret = intel_modeset_checks(state);
14137
14138 if (ret)
14139 return ret;
27c329ed 14140 } else
dd8b3bdb 14141 intel_state->cdclk = dev_priv->cdclk_freq;
76305b1a 14142
dd8b3bdb 14143 ret = drm_atomic_helper_check_planes(dev, state);
aa363136
MR
14144 if (ret)
14145 return ret;
14146
f51be2e0 14147 intel_fbc_choose_crtc(dev_priv, state);
55994c2c 14148 return calc_watermark_data(state);
054518dd
ACO
14149}
14150
5008e874 14151static int intel_atomic_prepare_commit(struct drm_device *dev,
d07f0e59 14152 struct drm_atomic_state *state)
5008e874 14153{
fac5e23e 14154 struct drm_i915_private *dev_priv = to_i915(dev);
5008e874
ML
14155 struct drm_crtc_state *crtc_state;
14156 struct drm_crtc *crtc;
14157 int i, ret;
14158
5a21b665
SV
14159 for_each_crtc_in_state(state, crtc, crtc_state, i) {
14160 if (state->legacy_cursor_update)
a6747b73
ML
14161 continue;
14162
5a21b665
SV
14163 ret = intel_crtc_wait_for_pending_flips(crtc);
14164 if (ret)
14165 return ret;
5008e874 14166
5a21b665
SV
14167 if (atomic_read(&to_intel_crtc(crtc)->unpin_work_count) >= 2)
14168 flush_workqueue(dev_priv->wq);
d55dbd06
ML
14169 }
14170
f935675f
ML
14171 ret = mutex_lock_interruptible(&dev->struct_mutex);
14172 if (ret)
14173 return ret;
14174
5008e874 14175 ret = drm_atomic_helper_prepare_planes(dev, state);
f7e5838b 14176 mutex_unlock(&dev->struct_mutex);
7580d774 14177
5008e874
ML
14178 return ret;
14179}
14180
a2991414
ML
14181u32 intel_crtc_get_vblank_counter(struct intel_crtc *crtc)
14182{
14183 struct drm_device *dev = crtc->base.dev;
14184
14185 if (!dev->max_vblank_count)
14186 return drm_accurate_vblank_count(&crtc->base);
14187
14188 return dev->driver->get_vblank_counter(dev, crtc->pipe);
14189}
14190
5a21b665
SV
14191static void intel_atomic_wait_for_vblanks(struct drm_device *dev,
14192 struct drm_i915_private *dev_priv,
14193 unsigned crtc_mask)
e8861675 14194{
5a21b665
SV
14195 unsigned last_vblank_count[I915_MAX_PIPES];
14196 enum pipe pipe;
14197 int ret;
e8861675 14198
5a21b665
SV
14199 if (!crtc_mask)
14200 return;
e8861675 14201
5a21b665 14202 for_each_pipe(dev_priv, pipe) {
98187836
VS
14203 struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv,
14204 pipe);
e8861675 14205
5a21b665 14206 if (!((1 << pipe) & crtc_mask))
e8861675
ML
14207 continue;
14208
e2af48c6 14209 ret = drm_crtc_vblank_get(&crtc->base);
5a21b665
SV
14210 if (WARN_ON(ret != 0)) {
14211 crtc_mask &= ~(1 << pipe);
14212 continue;
e8861675
ML
14213 }
14214
e2af48c6 14215 last_vblank_count[pipe] = drm_crtc_vblank_count(&crtc->base);
e8861675
ML
14216 }
14217
5a21b665 14218 for_each_pipe(dev_priv, pipe) {
98187836
VS
14219 struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv,
14220 pipe);
5a21b665 14221 long lret;
e8861675 14222
5a21b665
SV
14223 if (!((1 << pipe) & crtc_mask))
14224 continue;
d55dbd06 14225
5a21b665
SV
14226 lret = wait_event_timeout(dev->vblank[pipe].queue,
14227 last_vblank_count[pipe] !=
e2af48c6 14228 drm_crtc_vblank_count(&crtc->base),
5a21b665 14229 msecs_to_jiffies(50));
d55dbd06 14230
5a21b665 14231 WARN(!lret, "pipe %c vblank wait timed out\n", pipe_name(pipe));
d55dbd06 14232
e2af48c6 14233 drm_crtc_vblank_put(&crtc->base);
d55dbd06
ML
14234 }
14235}
14236
5a21b665 14237static bool needs_vblank_wait(struct intel_crtc_state *crtc_state)
a6747b73 14238{
5a21b665
SV
14239 /* fb updated, need to unpin old fb */
14240 if (crtc_state->fb_changed)
14241 return true;
a6747b73 14242
5a21b665
SV
14243 /* wm changes, need vblank before final wm's */
14244 if (crtc_state->update_wm_post)
14245 return true;
a6747b73 14246
5a21b665
SV
14247 /*
14248 * cxsr is re-enabled after vblank.
14249 * This is already handled by crtc_state->update_wm_post,
14250 * but added for clarity.
14251 */
14252 if (crtc_state->disable_cxsr)
14253 return true;
a6747b73 14254
5a21b665 14255 return false;
e8861675
ML
14256}
14257
896e5bb0
L
14258static void intel_update_crtc(struct drm_crtc *crtc,
14259 struct drm_atomic_state *state,
14260 struct drm_crtc_state *old_crtc_state,
14261 unsigned int *crtc_vblank_mask)
14262{
14263 struct drm_device *dev = crtc->dev;
14264 struct drm_i915_private *dev_priv = to_i915(dev);
14265 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
14266 struct intel_crtc_state *pipe_config = to_intel_crtc_state(crtc->state);
14267 bool modeset = needs_modeset(crtc->state);
14268
14269 if (modeset) {
14270 update_scanline_offset(intel_crtc);
14271 dev_priv->display.crtc_enable(pipe_config, state);
14272 } else {
14273 intel_pre_plane_update(to_intel_crtc_state(old_crtc_state));
14274 }
14275
14276 if (drm_atomic_get_existing_plane_state(state, crtc->primary)) {
14277 intel_fbc_enable(
14278 intel_crtc, pipe_config,
14279 to_intel_plane_state(crtc->primary->state));
14280 }
14281
14282 drm_atomic_helper_commit_planes_on_crtc(old_crtc_state);
14283
14284 if (needs_vblank_wait(pipe_config))
14285 *crtc_vblank_mask |= drm_crtc_mask(crtc);
14286}
14287
14288static void intel_update_crtcs(struct drm_atomic_state *state,
14289 unsigned int *crtc_vblank_mask)
14290{
14291 struct drm_crtc *crtc;
14292 struct drm_crtc_state *old_crtc_state;
14293 int i;
14294
14295 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
14296 if (!crtc->state->active)
14297 continue;
14298
14299 intel_update_crtc(crtc, state, old_crtc_state,
14300 crtc_vblank_mask);
14301 }
14302}
14303
27082493
L
14304static void skl_update_crtcs(struct drm_atomic_state *state,
14305 unsigned int *crtc_vblank_mask)
14306{
0f0f74bc 14307 struct drm_i915_private *dev_priv = to_i915(state->dev);
27082493
L
14308 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
14309 struct drm_crtc *crtc;
ce0ba283 14310 struct intel_crtc *intel_crtc;
27082493 14311 struct drm_crtc_state *old_crtc_state;
ce0ba283 14312 struct intel_crtc_state *cstate;
27082493
L
14313 unsigned int updated = 0;
14314 bool progress;
14315 enum pipe pipe;
14316
14317 /*
14318 * Whenever the number of active pipes changes, we need to make sure we
14319 * update the pipes in the right order so that their ddb allocations
14320 * never overlap with eachother inbetween CRTC updates. Otherwise we'll
14321 * cause pipe underruns and other bad stuff.
14322 */
14323 do {
14324 int i;
14325 progress = false;
14326
14327 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
14328 bool vbl_wait = false;
14329 unsigned int cmask = drm_crtc_mask(crtc);
ce0ba283
L
14330
14331 intel_crtc = to_intel_crtc(crtc);
14332 cstate = to_intel_crtc_state(crtc->state);
14333 pipe = intel_crtc->pipe;
27082493
L
14334
14335 if (updated & cmask || !crtc->state->active)
14336 continue;
ce0ba283 14337 if (skl_ddb_allocation_overlaps(state, intel_crtc))
27082493
L
14338 continue;
14339
14340 updated |= cmask;
14341
14342 /*
14343 * If this is an already active pipe, it's DDB changed,
14344 * and this isn't the last pipe that needs updating
14345 * then we need to wait for a vblank to pass for the
14346 * new ddb allocation to take effect.
14347 */
ce0ba283
L
14348 if (!skl_ddb_entry_equal(&cstate->wm.skl.ddb,
14349 &intel_crtc->hw_ddb) &&
27082493
L
14350 !crtc->state->active_changed &&
14351 intel_state->wm_results.dirty_pipes != updated)
14352 vbl_wait = true;
14353
14354 intel_update_crtc(crtc, state, old_crtc_state,
14355 crtc_vblank_mask);
14356
14357 if (vbl_wait)
0f0f74bc 14358 intel_wait_for_vblank(dev_priv, pipe);
27082493
L
14359
14360 progress = true;
14361 }
14362 } while (progress);
14363}
14364
94f05024 14365static void intel_atomic_commit_tail(struct drm_atomic_state *state)
a6778b3c 14366{
94f05024 14367 struct drm_device *dev = state->dev;
565602d7 14368 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
fac5e23e 14369 struct drm_i915_private *dev_priv = to_i915(dev);
29ceb0e6 14370 struct drm_crtc_state *old_crtc_state;
7580d774 14371 struct drm_crtc *crtc;
5a21b665 14372 struct intel_crtc_state *intel_cstate;
5a21b665
SV
14373 bool hw_check = intel_state->modeset;
14374 unsigned long put_domains[I915_MAX_PIPES] = {};
14375 unsigned crtc_vblank_mask = 0;
e95433c7 14376 int i;
a6778b3c 14377
ea0000f0
SV
14378 drm_atomic_helper_wait_for_dependencies(state);
14379
c3b32658 14380 if (intel_state->modeset)
5a21b665 14381 intel_display_power_get(dev_priv, POWER_DOMAIN_MODESET);
565602d7 14382
29ceb0e6 14383 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
a539205a
ML
14384 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
14385
5a21b665
SV
14386 if (needs_modeset(crtc->state) ||
14387 to_intel_crtc_state(crtc->state)->update_pipe) {
14388 hw_check = true;
14389
14390 put_domains[to_intel_crtc(crtc)->pipe] =
14391 modeset_get_crtc_power_domains(crtc,
14392 to_intel_crtc_state(crtc->state));
14393 }
14394
61333b60
ML
14395 if (!needs_modeset(crtc->state))
14396 continue;
14397
29ceb0e6 14398 intel_pre_plane_update(to_intel_crtc_state(old_crtc_state));
460da916 14399
29ceb0e6
VS
14400 if (old_crtc_state->active) {
14401 intel_crtc_disable_planes(crtc, old_crtc_state->plane_mask);
4a806558 14402 dev_priv->display.crtc_disable(to_intel_crtc_state(old_crtc_state), state);
eddfcbcd 14403 intel_crtc->active = false;
58f9c0bc 14404 intel_fbc_disable(intel_crtc);
eddfcbcd 14405 intel_disable_shared_dpll(intel_crtc);
9bbc8258
VS
14406
14407 /*
14408 * Underruns don't always raise
14409 * interrupts, so check manually.
14410 */
14411 intel_check_cpu_fifo_underruns(dev_priv);
14412 intel_check_pch_fifo_underruns(dev_priv);
b9001114
ML
14413
14414 if (!crtc->state->active)
432081bc 14415 intel_update_watermarks(intel_crtc);
a539205a 14416 }
b8cecdf5 14417 }
7758a113 14418
ea9d758d
SV
14419 /* Only after disabling all output pipelines that will be changed can we
14420 * update the the output configuration. */
4740b0f2 14421 intel_modeset_update_crtc_state(state);
f6e5b160 14422
565602d7 14423 if (intel_state->modeset) {
4740b0f2 14424 drm_atomic_helper_update_legacy_modeset_state(state->dev, state);
33c8df89
ML
14425
14426 if (dev_priv->display.modeset_commit_cdclk &&
c89e39f3 14427 (intel_state->dev_cdclk != dev_priv->cdclk_freq ||
63911d72 14428 intel_state->cdclk_pll_vco != dev_priv->cdclk_pll.vco))
33c8df89 14429 dev_priv->display.modeset_commit_cdclk(state);
f6d1973d 14430
656d1b89
L
14431 /*
14432 * SKL workaround: bspec recommends we disable the SAGV when we
14433 * have more then one pipe enabled
14434 */
56feca91 14435 if (!intel_can_enable_sagv(state))
16dcdc4e 14436 intel_disable_sagv(dev_priv);
656d1b89 14437
677100ce 14438 intel_modeset_verify_disabled(dev, state);
4740b0f2 14439 }
47fab737 14440
896e5bb0 14441 /* Complete the events for pipes that have now been disabled */
29ceb0e6 14442 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
f6ac4b2a 14443 bool modeset = needs_modeset(crtc->state);
80715b2f 14444
1f7528c4
SV
14445 /* Complete events for now disable pipes here. */
14446 if (modeset && !crtc->state->active && crtc->state->event) {
14447 spin_lock_irq(&dev->event_lock);
14448 drm_crtc_send_vblank_event(crtc, crtc->state->event);
14449 spin_unlock_irq(&dev->event_lock);
14450
14451 crtc->state->event = NULL;
14452 }
177246a8
MR
14453 }
14454
896e5bb0
L
14455 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
14456 dev_priv->display.update_crtcs(state, &crtc_vblank_mask);
14457
94f05024
SV
14458 /* FIXME: We should call drm_atomic_helper_commit_hw_done() here
14459 * already, but still need the state for the delayed optimization. To
14460 * fix this:
14461 * - wrap the optimization/post_plane_update stuff into a per-crtc work.
14462 * - schedule that vblank worker _before_ calling hw_done
14463 * - at the start of commit_tail, cancel it _synchrously
14464 * - switch over to the vblank wait helper in the core after that since
14465 * we don't need out special handling any more.
14466 */
5a21b665
SV
14467 if (!state->legacy_cursor_update)
14468 intel_atomic_wait_for_vblanks(dev, dev_priv, crtc_vblank_mask);
14469
14470 /*
14471 * Now that the vblank has passed, we can go ahead and program the
14472 * optimal watermarks on platforms that need two-step watermark
14473 * programming.
14474 *
14475 * TODO: Move this (and other cleanup) to an async worker eventually.
14476 */
14477 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
14478 intel_cstate = to_intel_crtc_state(crtc->state);
14479
14480 if (dev_priv->display.optimize_watermarks)
14481 dev_priv->display.optimize_watermarks(intel_cstate);
14482 }
14483
14484 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
14485 intel_post_plane_update(to_intel_crtc_state(old_crtc_state));
14486
14487 if (put_domains[i])
14488 modeset_put_power_domains(dev_priv, put_domains[i]);
14489
677100ce 14490 intel_modeset_verify_crtc(crtc, state, old_crtc_state, crtc->state);
5a21b665
SV
14491 }
14492
56feca91 14493 if (intel_state->modeset && intel_can_enable_sagv(state))
16dcdc4e 14494 intel_enable_sagv(dev_priv);
656d1b89 14495
94f05024
SV
14496 drm_atomic_helper_commit_hw_done(state);
14497
5a21b665
SV
14498 if (intel_state->modeset)
14499 intel_display_power_put(dev_priv, POWER_DOMAIN_MODESET);
14500
14501 mutex_lock(&dev->struct_mutex);
14502 drm_atomic_helper_cleanup_planes(dev, state);
14503 mutex_unlock(&dev->struct_mutex);
14504
ea0000f0
SV
14505 drm_atomic_helper_commit_cleanup_done(state);
14506
0853695c 14507 drm_atomic_state_put(state);
f30da187 14508
75714940
MK
14509 /* As one of the primary mmio accessors, KMS has a high likelihood
14510 * of triggering bugs in unclaimed access. After we finish
14511 * modesetting, see if an error has been flagged, and if so
14512 * enable debugging for the next modeset - and hope we catch
14513 * the culprit.
14514 *
14515 * XXX note that we assume display power is on at this point.
14516 * This might hold true now but we need to add pm helper to check
14517 * unclaimed only when the hardware is on, as atomic commits
14518 * can happen also when the device is completely off.
14519 */
14520 intel_uncore_arm_unclaimed_mmio_detection(dev_priv);
94f05024
SV
14521}
14522
14523static void intel_atomic_commit_work(struct work_struct *work)
14524{
c004a90b
CW
14525 struct drm_atomic_state *state =
14526 container_of(work, struct drm_atomic_state, commit_work);
14527
94f05024
SV
14528 intel_atomic_commit_tail(state);
14529}
14530
c004a90b
CW
14531static int __i915_sw_fence_call
14532intel_atomic_commit_ready(struct i915_sw_fence *fence,
14533 enum i915_sw_fence_notify notify)
14534{
14535 struct intel_atomic_state *state =
14536 container_of(fence, struct intel_atomic_state, commit_ready);
14537
14538 switch (notify) {
14539 case FENCE_COMPLETE:
14540 if (state->base.commit_work.func)
14541 queue_work(system_unbound_wq, &state->base.commit_work);
14542 break;
14543
14544 case FENCE_FREE:
14545 drm_atomic_state_put(&state->base);
14546 break;
14547 }
14548
14549 return NOTIFY_DONE;
14550}
14551
6c9c1b38
SV
14552static void intel_atomic_track_fbs(struct drm_atomic_state *state)
14553{
14554 struct drm_plane_state *old_plane_state;
14555 struct drm_plane *plane;
6c9c1b38
SV
14556 int i;
14557
faf5bf0a
CW
14558 for_each_plane_in_state(state, plane, old_plane_state, i)
14559 i915_gem_track_fb(intel_fb_obj(old_plane_state->fb),
14560 intel_fb_obj(plane->state->fb),
14561 to_intel_plane(plane)->frontbuffer_bit);
6c9c1b38
SV
14562}
14563
94f05024
SV
14564/**
14565 * intel_atomic_commit - commit validated state object
14566 * @dev: DRM device
14567 * @state: the top-level driver state object
14568 * @nonblock: nonblocking commit
14569 *
14570 * This function commits a top-level state object that has been validated
14571 * with drm_atomic_helper_check().
14572 *
14573 * FIXME: Atomic modeset support for i915 is not yet complete. At the moment
14574 * nonblocking commits are only safe for pure plane updates. Everything else
14575 * should work though.
14576 *
14577 * RETURNS
14578 * Zero for success or -errno.
14579 */
14580static int intel_atomic_commit(struct drm_device *dev,
14581 struct drm_atomic_state *state,
14582 bool nonblock)
14583{
14584 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
fac5e23e 14585 struct drm_i915_private *dev_priv = to_i915(dev);
94f05024
SV
14586 int ret = 0;
14587
14588 if (intel_state->modeset && nonblock) {
14589 DRM_DEBUG_KMS("nonblocking commit for modeset not yet implemented.\n");
14590 return -EINVAL;
14591 }
14592
14593 ret = drm_atomic_helper_setup_commit(state, nonblock);
14594 if (ret)
14595 return ret;
14596
c004a90b
CW
14597 drm_atomic_state_get(state);
14598 i915_sw_fence_init(&intel_state->commit_ready,
14599 intel_atomic_commit_ready);
94f05024 14600
d07f0e59 14601 ret = intel_atomic_prepare_commit(dev, state);
94f05024
SV
14602 if (ret) {
14603 DRM_DEBUG_ATOMIC("Preparing state failed with %i\n", ret);
c004a90b 14604 i915_sw_fence_commit(&intel_state->commit_ready);
94f05024
SV
14605 return ret;
14606 }
14607
14608 drm_atomic_helper_swap_state(state, true);
14609 dev_priv->wm.distrust_bios_wm = false;
14610 dev_priv->wm.skl_results = intel_state->wm_results;
14611 intel_shared_dpll_commit(state);
6c9c1b38 14612 intel_atomic_track_fbs(state);
94f05024 14613
c3b32658
ML
14614 if (intel_state->modeset) {
14615 memcpy(dev_priv->min_pixclk, intel_state->min_pixclk,
14616 sizeof(intel_state->min_pixclk));
14617 dev_priv->active_crtcs = intel_state->active_crtcs;
14618 dev_priv->atomic_cdclk_freq = intel_state->cdclk;
14619 }
14620
0853695c 14621 drm_atomic_state_get(state);
c004a90b
CW
14622 INIT_WORK(&state->commit_work,
14623 nonblock ? intel_atomic_commit_work : NULL);
14624
14625 i915_sw_fence_commit(&intel_state->commit_ready);
14626 if (!nonblock) {
14627 i915_sw_fence_wait(&intel_state->commit_ready);
94f05024 14628 intel_atomic_commit_tail(state);
c004a90b 14629 }
75714940 14630
74c090b1 14631 return 0;
7f27126e
JB
14632}
14633
c0c36b94
CW
14634void intel_crtc_restore_mode(struct drm_crtc *crtc)
14635{
83a57153
ACO
14636 struct drm_device *dev = crtc->dev;
14637 struct drm_atomic_state *state;
e694eb02 14638 struct drm_crtc_state *crtc_state;
2bfb4627 14639 int ret;
83a57153
ACO
14640
14641 state = drm_atomic_state_alloc(dev);
14642 if (!state) {
78108b7c
VS
14643 DRM_DEBUG_KMS("[CRTC:%d:%s] crtc restore failed, out of memory",
14644 crtc->base.id, crtc->name);
83a57153
ACO
14645 return;
14646 }
14647
e694eb02 14648 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
83a57153 14649
e694eb02
ML
14650retry:
14651 crtc_state = drm_atomic_get_crtc_state(state, crtc);
14652 ret = PTR_ERR_OR_ZERO(crtc_state);
14653 if (!ret) {
14654 if (!crtc_state->active)
14655 goto out;
83a57153 14656
e694eb02 14657 crtc_state->mode_changed = true;
74c090b1 14658 ret = drm_atomic_commit(state);
83a57153
ACO
14659 }
14660
e694eb02
ML
14661 if (ret == -EDEADLK) {
14662 drm_atomic_state_clear(state);
14663 drm_modeset_backoff(state->acquire_ctx);
14664 goto retry;
4ed9fb37 14665 }
4be07317 14666
e694eb02 14667out:
0853695c 14668 drm_atomic_state_put(state);
c0c36b94
CW
14669}
14670
a8784875
BP
14671/*
14672 * FIXME: Remove this once i915 is fully DRIVER_ATOMIC by calling
14673 * drm_atomic_helper_legacy_gamma_set() directly.
14674 */
14675static int intel_atomic_legacy_gamma_set(struct drm_crtc *crtc,
14676 u16 *red, u16 *green, u16 *blue,
14677 uint32_t size)
14678{
14679 struct drm_device *dev = crtc->dev;
14680 struct drm_mode_config *config = &dev->mode_config;
14681 struct drm_crtc_state *state;
14682 int ret;
14683
14684 ret = drm_atomic_helper_legacy_gamma_set(crtc, red, green, blue, size);
14685 if (ret)
14686 return ret;
14687
14688 /*
14689 * Make sure we update the legacy properties so this works when
14690 * atomic is not enabled.
14691 */
14692
14693 state = crtc->state;
14694
14695 drm_object_property_set_value(&crtc->base,
14696 config->degamma_lut_property,
14697 (state->degamma_lut) ?
14698 state->degamma_lut->base.id : 0);
14699
14700 drm_object_property_set_value(&crtc->base,
14701 config->ctm_property,
14702 (state->ctm) ?
14703 state->ctm->base.id : 0);
14704
14705 drm_object_property_set_value(&crtc->base,
14706 config->gamma_lut_property,
14707 (state->gamma_lut) ?
14708 state->gamma_lut->base.id : 0);
14709
14710 return 0;
14711}
14712
f6e5b160 14713static const struct drm_crtc_funcs intel_crtc_funcs = {
a8784875 14714 .gamma_set = intel_atomic_legacy_gamma_set,
74c090b1 14715 .set_config = drm_atomic_helper_set_config,
82cf435b 14716 .set_property = drm_atomic_helper_crtc_set_property,
f6e5b160 14717 .destroy = intel_crtc_destroy,
527b6abe 14718 .page_flip = intel_crtc_page_flip,
1356837e
MR
14719 .atomic_duplicate_state = intel_crtc_duplicate_state,
14720 .atomic_destroy_state = intel_crtc_destroy_state,
f6e5b160
CW
14721};
14722
6beb8c23
MR
14723/**
14724 * intel_prepare_plane_fb - Prepare fb for usage on plane
14725 * @plane: drm plane to prepare for
14726 * @fb: framebuffer to prepare for presentation
14727 *
14728 * Prepares a framebuffer for usage on a display plane. Generally this
14729 * involves pinning the underlying object and updating the frontbuffer tracking
14730 * bits. Some older platforms need special physical address handling for
14731 * cursor planes.
14732 *
f935675f
ML
14733 * Must be called with struct_mutex held.
14734 *
6beb8c23
MR
14735 * Returns 0 on success, negative error code on failure.
14736 */
14737int
14738intel_prepare_plane_fb(struct drm_plane *plane,
1832040d 14739 struct drm_plane_state *new_state)
465c120c 14740{
c004a90b
CW
14741 struct intel_atomic_state *intel_state =
14742 to_intel_atomic_state(new_state->state);
b7f05d4a 14743 struct drm_i915_private *dev_priv = to_i915(plane->dev);
844f9111 14744 struct drm_framebuffer *fb = new_state->fb;
6beb8c23 14745 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
1ee49399 14746 struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->state->fb);
c004a90b 14747 int ret;
465c120c 14748
1ee49399 14749 if (!obj && !old_obj)
465c120c
MR
14750 return 0;
14751
5008e874
ML
14752 if (old_obj) {
14753 struct drm_crtc_state *crtc_state =
c004a90b
CW
14754 drm_atomic_get_existing_crtc_state(new_state->state,
14755 plane->state->crtc);
5008e874
ML
14756
14757 /* Big Hammer, we also need to ensure that any pending
14758 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
14759 * current scanout is retired before unpinning the old
14760 * framebuffer. Note that we rely on userspace rendering
14761 * into the buffer attached to the pipe they are waiting
14762 * on. If not, userspace generates a GPU hang with IPEHR
14763 * point to the MI_WAIT_FOR_EVENT.
14764 *
14765 * This should only fail upon a hung GPU, in which case we
14766 * can safely continue.
14767 */
c004a90b
CW
14768 if (needs_modeset(crtc_state)) {
14769 ret = i915_sw_fence_await_reservation(&intel_state->commit_ready,
14770 old_obj->resv, NULL,
14771 false, 0,
14772 GFP_KERNEL);
14773 if (ret < 0)
14774 return ret;
f4457ae7 14775 }
5008e874
ML
14776 }
14777
c004a90b
CW
14778 if (new_state->fence) { /* explicit fencing */
14779 ret = i915_sw_fence_await_dma_fence(&intel_state->commit_ready,
14780 new_state->fence,
14781 I915_FENCE_TIMEOUT,
14782 GFP_KERNEL);
14783 if (ret < 0)
14784 return ret;
14785 }
14786
c37efb99
CW
14787 if (!obj)
14788 return 0;
14789
c004a90b
CW
14790 if (!new_state->fence) { /* implicit fencing */
14791 ret = i915_sw_fence_await_reservation(&intel_state->commit_ready,
14792 obj->resv, NULL,
14793 false, I915_FENCE_TIMEOUT,
14794 GFP_KERNEL);
14795 if (ret < 0)
14796 return ret;
14797 }
5a21b665 14798
c37efb99 14799 if (plane->type == DRM_PLANE_TYPE_CURSOR &&
b7f05d4a 14800 INTEL_INFO(dev_priv)->cursor_needs_physical) {
50a0bc90 14801 int align = IS_I830(dev_priv) ? 16 * 1024 : 256;
6beb8c23 14802 ret = i915_gem_object_attach_phys(obj, align);
d07f0e59 14803 if (ret) {
6beb8c23 14804 DRM_DEBUG_KMS("failed to attach phys object\n");
d07f0e59
CW
14805 return ret;
14806 }
6beb8c23 14807 } else {
058d88c4
CW
14808 struct i915_vma *vma;
14809
14810 vma = intel_pin_and_fence_fb_obj(fb, new_state->rotation);
d07f0e59
CW
14811 if (IS_ERR(vma)) {
14812 DRM_DEBUG_KMS("failed to pin object\n");
14813 return PTR_ERR(vma);
14814 }
7580d774 14815 }
fdd508a6 14816
d07f0e59 14817 return 0;
6beb8c23
MR
14818}
14819
38f3ce3a
MR
14820/**
14821 * intel_cleanup_plane_fb - Cleans up an fb after plane use
14822 * @plane: drm plane to clean up for
14823 * @fb: old framebuffer that was on plane
14824 *
14825 * Cleans up a framebuffer that has just been removed from a plane.
f935675f
ML
14826 *
14827 * Must be called with struct_mutex held.
38f3ce3a
MR
14828 */
14829void
14830intel_cleanup_plane_fb(struct drm_plane *plane,
1832040d 14831 struct drm_plane_state *old_state)
38f3ce3a 14832{
b7f05d4a 14833 struct drm_i915_private *dev_priv = to_i915(plane->dev);
7580d774 14834 struct intel_plane_state *old_intel_state;
1ee49399
ML
14835 struct drm_i915_gem_object *old_obj = intel_fb_obj(old_state->fb);
14836 struct drm_i915_gem_object *obj = intel_fb_obj(plane->state->fb);
38f3ce3a 14837
7580d774
ML
14838 old_intel_state = to_intel_plane_state(old_state);
14839
1ee49399 14840 if (!obj && !old_obj)
38f3ce3a
MR
14841 return;
14842
1ee49399 14843 if (old_obj && (plane->type != DRM_PLANE_TYPE_CURSOR ||
b7f05d4a 14844 !INTEL_INFO(dev_priv)->cursor_needs_physical))
3465c580 14845 intel_unpin_fb_obj(old_state->fb, old_state->rotation);
465c120c
MR
14846}
14847
6156a456
CK
14848int
14849skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
14850{
14851 int max_scale;
6156a456
CK
14852 int crtc_clock, cdclk;
14853
bf8a0af0 14854 if (!intel_crtc || !crtc_state->base.enable)
6156a456
CK
14855 return DRM_PLANE_HELPER_NO_SCALING;
14856
6156a456 14857 crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
27c329ed 14858 cdclk = to_intel_atomic_state(crtc_state->base.state)->cdclk;
6156a456 14859
54bf1ce6 14860 if (WARN_ON_ONCE(!crtc_clock || cdclk < crtc_clock))
6156a456
CK
14861 return DRM_PLANE_HELPER_NO_SCALING;
14862
14863 /*
14864 * skl max scale is lower of:
14865 * close to 3 but not 3, -1 is for that purpose
14866 * or
14867 * cdclk/crtc_clock
14868 */
14869 max_scale = min((1 << 16) * 3 - 1, (1 << 8) * ((cdclk << 8) / crtc_clock));
14870
14871 return max_scale;
14872}
14873
465c120c 14874static int
3c692a41 14875intel_check_primary_plane(struct drm_plane *plane,
061e4b8d 14876 struct intel_crtc_state *crtc_state,
3c692a41
GP
14877 struct intel_plane_state *state)
14878{
b63a16f6 14879 struct drm_i915_private *dev_priv = to_i915(plane->dev);
2b875c22 14880 struct drm_crtc *crtc = state->base.crtc;
6156a456 14881 int min_scale = DRM_PLANE_HELPER_NO_SCALING;
061e4b8d
ML
14882 int max_scale = DRM_PLANE_HELPER_NO_SCALING;
14883 bool can_position = false;
b63a16f6 14884 int ret;
465c120c 14885
b63a16f6 14886 if (INTEL_GEN(dev_priv) >= 9) {
693bdc28
VS
14887 /* use scaler when colorkey is not required */
14888 if (state->ckey.flags == I915_SET_COLORKEY_NONE) {
14889 min_scale = 1;
14890 max_scale = skl_max_scale(to_intel_crtc(crtc), crtc_state);
14891 }
d8106366 14892 can_position = true;
6156a456 14893 }
d8106366 14894
cc926387
SV
14895 ret = drm_plane_helper_check_state(&state->base,
14896 &state->clip,
14897 min_scale, max_scale,
14898 can_position, true);
b63a16f6
VS
14899 if (ret)
14900 return ret;
14901
cc926387 14902 if (!state->base.fb)
b63a16f6
VS
14903 return 0;
14904
14905 if (INTEL_GEN(dev_priv) >= 9) {
14906 ret = skl_check_plane_surface(state);
14907 if (ret)
14908 return ret;
14909 }
14910
14911 return 0;
14af293f
GP
14912}
14913
5a21b665
SV
14914static void intel_begin_crtc_commit(struct drm_crtc *crtc,
14915 struct drm_crtc_state *old_crtc_state)
14916{
14917 struct drm_device *dev = crtc->dev;
62e0fb88 14918 struct drm_i915_private *dev_priv = to_i915(dev);
5a21b665 14919 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
b707aa50
L
14920 struct intel_crtc_state *intel_cstate =
14921 to_intel_crtc_state(crtc->state);
5a21b665
SV
14922 struct intel_crtc_state *old_intel_state =
14923 to_intel_crtc_state(old_crtc_state);
14924 bool modeset = needs_modeset(crtc->state);
62e0fb88 14925 enum pipe pipe = intel_crtc->pipe;
5a21b665
SV
14926
14927 /* Perform vblank evasion around commit operation */
14928 intel_pipe_update_start(intel_crtc);
14929
14930 if (modeset)
14931 return;
14932
14933 if (crtc->state->color_mgmt_changed || to_intel_crtc_state(crtc->state)->update_pipe) {
14934 intel_color_set_csc(crtc->state);
14935 intel_color_load_luts(crtc->state);
14936 }
14937
b707aa50 14938 if (intel_cstate->update_pipe) {
5a21b665 14939 intel_update_pipe_config(intel_crtc, old_intel_state);
b707aa50 14940 } else if (INTEL_GEN(dev_priv) >= 9) {
5a21b665 14941 skl_detach_scalers(intel_crtc);
62e0fb88
L
14942
14943 I915_WRITE(PIPE_WM_LINETIME(pipe),
b707aa50 14944 intel_cstate->wm.skl.optimal.linetime);
62e0fb88 14945 }
5a21b665
SV
14946}
14947
14948static void intel_finish_crtc_commit(struct drm_crtc *crtc,
14949 struct drm_crtc_state *old_crtc_state)
14950{
14951 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
14952
14953 intel_pipe_update_end(intel_crtc, NULL);
14954}
14955
cf4c7c12 14956/**
4a3b8769
MR
14957 * intel_plane_destroy - destroy a plane
14958 * @plane: plane to destroy
cf4c7c12 14959 *
4a3b8769
MR
14960 * Common destruction function for all types of planes (primary, cursor,
14961 * sprite).
cf4c7c12 14962 */
4a3b8769 14963void intel_plane_destroy(struct drm_plane *plane)
465c120c 14964{
465c120c 14965 drm_plane_cleanup(plane);
69ae561f 14966 kfree(to_intel_plane(plane));
465c120c
MR
14967}
14968
65a3fea0 14969const struct drm_plane_funcs intel_plane_funcs = {
70a101f8
MR
14970 .update_plane = drm_atomic_helper_update_plane,
14971 .disable_plane = drm_atomic_helper_disable_plane,
3d7d6510 14972 .destroy = intel_plane_destroy,
c196e1d6 14973 .set_property = drm_atomic_helper_plane_set_property,
a98b3431
MR
14974 .atomic_get_property = intel_plane_atomic_get_property,
14975 .atomic_set_property = intel_plane_atomic_set_property,
ea2c67bb
MR
14976 .atomic_duplicate_state = intel_plane_duplicate_state,
14977 .atomic_destroy_state = intel_plane_destroy_state,
465c120c
MR
14978};
14979
b079bd17 14980static struct intel_plane *
580503c7 14981intel_primary_plane_create(struct drm_i915_private *dev_priv, enum pipe pipe)
465c120c 14982{
fca0ce2a
VS
14983 struct intel_plane *primary = NULL;
14984 struct intel_plane_state *state = NULL;
465c120c 14985 const uint32_t *intel_primary_formats;
93ca7e00 14986 unsigned int supported_rotations;
45e3743a 14987 unsigned int num_formats;
fca0ce2a 14988 int ret;
465c120c
MR
14989
14990 primary = kzalloc(sizeof(*primary), GFP_KERNEL);
b079bd17
VS
14991 if (!primary) {
14992 ret = -ENOMEM;
fca0ce2a 14993 goto fail;
b079bd17 14994 }
465c120c 14995
8e7d688b 14996 state = intel_create_plane_state(&primary->base);
b079bd17
VS
14997 if (!state) {
14998 ret = -ENOMEM;
fca0ce2a 14999 goto fail;
b079bd17
VS
15000 }
15001
8e7d688b 15002 primary->base.state = &state->base;
ea2c67bb 15003
465c120c
MR
15004 primary->can_scale = false;
15005 primary->max_downscale = 1;
580503c7 15006 if (INTEL_GEN(dev_priv) >= 9) {
6156a456 15007 primary->can_scale = true;
af99ceda 15008 state->scaler_id = -1;
6156a456 15009 }
465c120c
MR
15010 primary->pipe = pipe;
15011 primary->plane = pipe;
a9ff8714 15012 primary->frontbuffer_bit = INTEL_FRONTBUFFER_PRIMARY(pipe);
c59cb179 15013 primary->check_plane = intel_check_primary_plane;
580503c7 15014 if (HAS_FBC(dev_priv) && INTEL_GEN(dev_priv) < 4)
465c120c
MR
15015 primary->plane = !pipe;
15016
580503c7 15017 if (INTEL_GEN(dev_priv) >= 9) {
6c0fd451
DL
15018 intel_primary_formats = skl_primary_formats;
15019 num_formats = ARRAY_SIZE(skl_primary_formats);
a8d201af
ML
15020
15021 primary->update_plane = skylake_update_primary_plane;
15022 primary->disable_plane = skylake_disable_primary_plane;
6e266956 15023 } else if (HAS_PCH_SPLIT(dev_priv)) {
a8d201af
ML
15024 intel_primary_formats = i965_primary_formats;
15025 num_formats = ARRAY_SIZE(i965_primary_formats);
15026
15027 primary->update_plane = ironlake_update_primary_plane;
15028 primary->disable_plane = i9xx_disable_primary_plane;
580503c7 15029 } else if (INTEL_GEN(dev_priv) >= 4) {
568db4f2
DL
15030 intel_primary_formats = i965_primary_formats;
15031 num_formats = ARRAY_SIZE(i965_primary_formats);
a8d201af
ML
15032
15033 primary->update_plane = i9xx_update_primary_plane;
15034 primary->disable_plane = i9xx_disable_primary_plane;
6c0fd451
DL
15035 } else {
15036 intel_primary_formats = i8xx_primary_formats;
15037 num_formats = ARRAY_SIZE(i8xx_primary_formats);
a8d201af
ML
15038
15039 primary->update_plane = i9xx_update_primary_plane;
15040 primary->disable_plane = i9xx_disable_primary_plane;
465c120c
MR
15041 }
15042
580503c7
VS
15043 if (INTEL_GEN(dev_priv) >= 9)
15044 ret = drm_universal_plane_init(&dev_priv->drm, &primary->base,
15045 0, &intel_plane_funcs,
38573dc1
VS
15046 intel_primary_formats, num_formats,
15047 DRM_PLANE_TYPE_PRIMARY,
15048 "plane 1%c", pipe_name(pipe));
9beb5fea 15049 else if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
580503c7
VS
15050 ret = drm_universal_plane_init(&dev_priv->drm, &primary->base,
15051 0, &intel_plane_funcs,
38573dc1
VS
15052 intel_primary_formats, num_formats,
15053 DRM_PLANE_TYPE_PRIMARY,
15054 "primary %c", pipe_name(pipe));
15055 else
580503c7
VS
15056 ret = drm_universal_plane_init(&dev_priv->drm, &primary->base,
15057 0, &intel_plane_funcs,
38573dc1
VS
15058 intel_primary_formats, num_formats,
15059 DRM_PLANE_TYPE_PRIMARY,
15060 "plane %c", plane_name(primary->plane));
fca0ce2a
VS
15061 if (ret)
15062 goto fail;
48404c1e 15063
5481e27f 15064 if (INTEL_GEN(dev_priv) >= 9) {
93ca7e00
VS
15065 supported_rotations =
15066 DRM_ROTATE_0 | DRM_ROTATE_90 |
15067 DRM_ROTATE_180 | DRM_ROTATE_270;
5481e27f 15068 } else if (INTEL_GEN(dev_priv) >= 4) {
93ca7e00
VS
15069 supported_rotations =
15070 DRM_ROTATE_0 | DRM_ROTATE_180;
15071 } else {
15072 supported_rotations = DRM_ROTATE_0;
15073 }
15074
5481e27f 15075 if (INTEL_GEN(dev_priv) >= 4)
93ca7e00
VS
15076 drm_plane_create_rotation_property(&primary->base,
15077 DRM_ROTATE_0,
15078 supported_rotations);
48404c1e 15079
ea2c67bb
MR
15080 drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
15081
b079bd17 15082 return primary;
fca0ce2a
VS
15083
15084fail:
15085 kfree(state);
15086 kfree(primary);
15087
b079bd17 15088 return ERR_PTR(ret);
465c120c
MR
15089}
15090
3d7d6510 15091static int
852e787c 15092intel_check_cursor_plane(struct drm_plane *plane,
061e4b8d 15093 struct intel_crtc_state *crtc_state,
852e787c 15094 struct intel_plane_state *state)
3d7d6510 15095{
2b875c22 15096 struct drm_framebuffer *fb = state->base.fb;
757f9a3e 15097 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
b29ec92c 15098 enum pipe pipe = to_intel_plane(plane)->pipe;
757f9a3e
GP
15099 unsigned stride;
15100 int ret;
3d7d6510 15101
f8856a44
VS
15102 ret = drm_plane_helper_check_state(&state->base,
15103 &state->clip,
15104 DRM_PLANE_HELPER_NO_SCALING,
15105 DRM_PLANE_HELPER_NO_SCALING,
15106 true, true);
757f9a3e
GP
15107 if (ret)
15108 return ret;
15109
757f9a3e
GP
15110 /* if we want to turn off the cursor ignore width and height */
15111 if (!obj)
da20eabd 15112 return 0;
757f9a3e 15113
757f9a3e 15114 /* Check for which cursor types we support */
50a0bc90
TU
15115 if (!cursor_size_ok(to_i915(plane->dev), state->base.crtc_w,
15116 state->base.crtc_h)) {
ea2c67bb
MR
15117 DRM_DEBUG("Cursor dimension %dx%d not supported\n",
15118 state->base.crtc_w, state->base.crtc_h);
757f9a3e
GP
15119 return -EINVAL;
15120 }
15121
ea2c67bb
MR
15122 stride = roundup_pow_of_two(state->base.crtc_w) * 4;
15123 if (obj->base.size < stride * state->base.crtc_h) {
757f9a3e
GP
15124 DRM_DEBUG_KMS("buffer is too small\n");
15125 return -ENOMEM;
15126 }
15127
3a656b54 15128 if (fb->modifier[0] != DRM_FORMAT_MOD_NONE) {
757f9a3e 15129 DRM_DEBUG_KMS("cursor cannot be tiled\n");
da20eabd 15130 return -EINVAL;
32b7eeec
MR
15131 }
15132
b29ec92c
VS
15133 /*
15134 * There's something wrong with the cursor on CHV pipe C.
15135 * If it straddles the left edge of the screen then
15136 * moving it away from the edge or disabling it often
15137 * results in a pipe underrun, and often that can lead to
15138 * dead pipe (constant underrun reported, and it scans
15139 * out just a solid color). To recover from that, the
15140 * display power well must be turned off and on again.
15141 * Refuse the put the cursor into that compromised position.
15142 */
920a14b2 15143 if (IS_CHERRYVIEW(to_i915(plane->dev)) && pipe == PIPE_C &&
936e71e3 15144 state->base.visible && state->base.crtc_x < 0) {
b29ec92c
VS
15145 DRM_DEBUG_KMS("CHV cursor C not allowed to straddle the left screen edge\n");
15146 return -EINVAL;
15147 }
15148
da20eabd 15149 return 0;
852e787c 15150}
3d7d6510 15151
a8ad0d8e
ML
15152static void
15153intel_disable_cursor_plane(struct drm_plane *plane,
7fabf5ef 15154 struct drm_crtc *crtc)
a8ad0d8e 15155{
f2858021
ML
15156 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
15157
15158 intel_crtc->cursor_addr = 0;
55a08b3f 15159 intel_crtc_update_cursor(crtc, NULL);
a8ad0d8e
ML
15160}
15161
f4a2cf29 15162static void
55a08b3f
ML
15163intel_update_cursor_plane(struct drm_plane *plane,
15164 const struct intel_crtc_state *crtc_state,
15165 const struct intel_plane_state *state)
852e787c 15166{
55a08b3f
ML
15167 struct drm_crtc *crtc = crtc_state->base.crtc;
15168 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
b7f05d4a 15169 struct drm_i915_private *dev_priv = to_i915(plane->dev);
2b875c22 15170 struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
a912f12f 15171 uint32_t addr;
852e787c 15172
f4a2cf29 15173 if (!obj)
a912f12f 15174 addr = 0;
b7f05d4a 15175 else if (!INTEL_INFO(dev_priv)->cursor_needs_physical)
058d88c4 15176 addr = i915_gem_object_ggtt_offset(obj, NULL);
f4a2cf29 15177 else
a912f12f 15178 addr = obj->phys_handle->busaddr;
852e787c 15179
a912f12f 15180 intel_crtc->cursor_addr = addr;
55a08b3f 15181 intel_crtc_update_cursor(crtc, state);
852e787c
GP
15182}
15183
b079bd17 15184static struct intel_plane *
580503c7 15185intel_cursor_plane_create(struct drm_i915_private *dev_priv, enum pipe pipe)
3d7d6510 15186{
fca0ce2a
VS
15187 struct intel_plane *cursor = NULL;
15188 struct intel_plane_state *state = NULL;
15189 int ret;
3d7d6510
MR
15190
15191 cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
b079bd17
VS
15192 if (!cursor) {
15193 ret = -ENOMEM;
fca0ce2a 15194 goto fail;
b079bd17 15195 }
3d7d6510 15196
8e7d688b 15197 state = intel_create_plane_state(&cursor->base);
b079bd17
VS
15198 if (!state) {
15199 ret = -ENOMEM;
fca0ce2a 15200 goto fail;
b079bd17
VS
15201 }
15202
8e7d688b 15203 cursor->base.state = &state->base;
ea2c67bb 15204
3d7d6510
MR
15205 cursor->can_scale = false;
15206 cursor->max_downscale = 1;
15207 cursor->pipe = pipe;
15208 cursor->plane = pipe;
a9ff8714 15209 cursor->frontbuffer_bit = INTEL_FRONTBUFFER_CURSOR(pipe);
c59cb179 15210 cursor->check_plane = intel_check_cursor_plane;
55a08b3f 15211 cursor->update_plane = intel_update_cursor_plane;
a8ad0d8e 15212 cursor->disable_plane = intel_disable_cursor_plane;
3d7d6510 15213
580503c7
VS
15214 ret = drm_universal_plane_init(&dev_priv->drm, &cursor->base,
15215 0, &intel_plane_funcs,
fca0ce2a
VS
15216 intel_cursor_formats,
15217 ARRAY_SIZE(intel_cursor_formats),
38573dc1
VS
15218 DRM_PLANE_TYPE_CURSOR,
15219 "cursor %c", pipe_name(pipe));
fca0ce2a
VS
15220 if (ret)
15221 goto fail;
4398ad45 15222
5481e27f 15223 if (INTEL_GEN(dev_priv) >= 4)
93ca7e00
VS
15224 drm_plane_create_rotation_property(&cursor->base,
15225 DRM_ROTATE_0,
15226 DRM_ROTATE_0 |
15227 DRM_ROTATE_180);
4398ad45 15228
580503c7 15229 if (INTEL_GEN(dev_priv) >= 9)
af99ceda
CK
15230 state->scaler_id = -1;
15231
ea2c67bb
MR
15232 drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
15233
b079bd17 15234 return cursor;
fca0ce2a
VS
15235
15236fail:
15237 kfree(state);
15238 kfree(cursor);
15239
b079bd17 15240 return ERR_PTR(ret);
3d7d6510
MR
15241}
15242
65edccce
VS
15243static void skl_init_scalers(struct drm_i915_private *dev_priv,
15244 struct intel_crtc *crtc,
15245 struct intel_crtc_state *crtc_state)
549e2bfb 15246{
65edccce
VS
15247 struct intel_crtc_scaler_state *scaler_state =
15248 &crtc_state->scaler_state;
549e2bfb 15249 int i;
549e2bfb 15250
65edccce
VS
15251 for (i = 0; i < crtc->num_scalers; i++) {
15252 struct intel_scaler *scaler = &scaler_state->scalers[i];
15253
15254 scaler->in_use = 0;
15255 scaler->mode = PS_SCALER_MODE_DYN;
549e2bfb
CK
15256 }
15257
15258 scaler_state->scaler_id = -1;
15259}
15260
5ab0d85b 15261static int intel_crtc_init(struct drm_i915_private *dev_priv, enum pipe pipe)
79e53945
JB
15262{
15263 struct intel_crtc *intel_crtc;
f5de6e07 15264 struct intel_crtc_state *crtc_state = NULL;
b079bd17
VS
15265 struct intel_plane *primary = NULL;
15266 struct intel_plane *cursor = NULL;
a81d6fa0 15267 int sprite, ret;
79e53945 15268
955382f3 15269 intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
b079bd17
VS
15270 if (!intel_crtc)
15271 return -ENOMEM;
79e53945 15272
f5de6e07 15273 crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
b079bd17
VS
15274 if (!crtc_state) {
15275 ret = -ENOMEM;
f5de6e07 15276 goto fail;
b079bd17 15277 }
550acefd
ACO
15278 intel_crtc->config = crtc_state;
15279 intel_crtc->base.state = &crtc_state->base;
07878248 15280 crtc_state->base.crtc = &intel_crtc->base;
f5de6e07 15281
549e2bfb 15282 /* initialize shared scalers */
5ab0d85b 15283 if (INTEL_GEN(dev_priv) >= 9) {
549e2bfb
CK
15284 if (pipe == PIPE_C)
15285 intel_crtc->num_scalers = 1;
15286 else
15287 intel_crtc->num_scalers = SKL_NUM_SCALERS;
15288
65edccce 15289 skl_init_scalers(dev_priv, intel_crtc, crtc_state);
549e2bfb
CK
15290 }
15291
580503c7 15292 primary = intel_primary_plane_create(dev_priv, pipe);
b079bd17
VS
15293 if (IS_ERR(primary)) {
15294 ret = PTR_ERR(primary);
3d7d6510 15295 goto fail;
b079bd17 15296 }
3d7d6510 15297
a81d6fa0 15298 for_each_sprite(dev_priv, pipe, sprite) {
b079bd17
VS
15299 struct intel_plane *plane;
15300
580503c7 15301 plane = intel_sprite_plane_create(dev_priv, pipe, sprite);
d2b2cbce 15302 if (IS_ERR(plane)) {
b079bd17
VS
15303 ret = PTR_ERR(plane);
15304 goto fail;
15305 }
a81d6fa0
VS
15306 }
15307
580503c7 15308 cursor = intel_cursor_plane_create(dev_priv, pipe);
d2b2cbce 15309 if (IS_ERR(cursor)) {
b079bd17 15310 ret = PTR_ERR(cursor);
3d7d6510 15311 goto fail;
b079bd17 15312 }
3d7d6510 15313
5ab0d85b 15314 ret = drm_crtc_init_with_planes(&dev_priv->drm, &intel_crtc->base,
b079bd17
VS
15315 &primary->base, &cursor->base,
15316 &intel_crtc_funcs,
4d5d72b7 15317 "pipe %c", pipe_name(pipe));
3d7d6510
MR
15318 if (ret)
15319 goto fail;
79e53945 15320
1f1c2e24
VS
15321 /*
15322 * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
8c0f92e1 15323 * is hooked to pipe B. Hence we want plane A feeding pipe B.
1f1c2e24 15324 */
80824003 15325 intel_crtc->pipe = pipe;
b079bd17 15326 intel_crtc->plane = (enum plane) pipe;
5ab0d85b 15327 if (HAS_FBC(dev_priv) && INTEL_GEN(dev_priv) < 4) {
28c97730 15328 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
e2e767ab 15329 intel_crtc->plane = !pipe;
80824003
JB
15330 }
15331
4b0e333e
CW
15332 intel_crtc->cursor_base = ~0;
15333 intel_crtc->cursor_cntl = ~0;
dc41c154 15334 intel_crtc->cursor_size = ~0;
8d7849db 15335
852eb00d
VS
15336 intel_crtc->wm.cxsr_allowed = true;
15337
22fd0fab
JB
15338 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
15339 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
e2af48c6
VS
15340 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = intel_crtc;
15341 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = intel_crtc;
22fd0fab 15342
79e53945 15343 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
87b6b101 15344
8563b1e8
LL
15345 intel_color_init(&intel_crtc->base);
15346
87b6b101 15347 WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
b079bd17
VS
15348
15349 return 0;
3d7d6510
MR
15350
15351fail:
b079bd17
VS
15352 /*
15353 * drm_mode_config_cleanup() will free up any
15354 * crtcs/planes already initialized.
15355 */
f5de6e07 15356 kfree(crtc_state);
3d7d6510 15357 kfree(intel_crtc);
b079bd17
VS
15358
15359 return ret;
79e53945
JB
15360}
15361
752aa88a
JB
15362enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
15363{
15364 struct drm_encoder *encoder = connector->base.encoder;
6e9f798d 15365 struct drm_device *dev = connector->base.dev;
752aa88a 15366
51fd371b 15367 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
752aa88a 15368
d3babd3f 15369 if (!encoder || WARN_ON(!encoder->crtc))
752aa88a
JB
15370 return INVALID_PIPE;
15371
15372 return to_intel_crtc(encoder->crtc)->pipe;
15373}
15374
08d7b3d1 15375int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
05394f39 15376 struct drm_file *file)
08d7b3d1 15377{
08d7b3d1 15378 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
7707e653 15379 struct drm_crtc *drmmode_crtc;
c05422d5 15380 struct intel_crtc *crtc;
08d7b3d1 15381
7707e653 15382 drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
71240ed2 15383 if (!drmmode_crtc)
3f2c2057 15384 return -ENOENT;
08d7b3d1 15385
7707e653 15386 crtc = to_intel_crtc(drmmode_crtc);
c05422d5 15387 pipe_from_crtc_id->pipe = crtc->pipe;
08d7b3d1 15388
c05422d5 15389 return 0;
08d7b3d1
CW
15390}
15391
66a9278e 15392static int intel_encoder_clones(struct intel_encoder *encoder)
79e53945 15393{
66a9278e
SV
15394 struct drm_device *dev = encoder->base.dev;
15395 struct intel_encoder *source_encoder;
79e53945 15396 int index_mask = 0;
79e53945
JB
15397 int entry = 0;
15398
b2784e15 15399 for_each_intel_encoder(dev, source_encoder) {
bc079e8b 15400 if (encoders_cloneable(encoder, source_encoder))
66a9278e
SV
15401 index_mask |= (1 << entry);
15402
79e53945
JB
15403 entry++;
15404 }
4ef69c7a 15405
79e53945
JB
15406 return index_mask;
15407}
15408
646d5772 15409static bool has_edp_a(struct drm_i915_private *dev_priv)
4d302442 15410{
646d5772 15411 if (!IS_MOBILE(dev_priv))
4d302442
CW
15412 return false;
15413
15414 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
15415 return false;
15416
5db94019 15417 if (IS_GEN5(dev_priv) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
4d302442
CW
15418 return false;
15419
15420 return true;
15421}
15422
84b4e042
JB
15423static bool intel_crt_present(struct drm_device *dev)
15424{
fac5e23e 15425 struct drm_i915_private *dev_priv = to_i915(dev);
84b4e042 15426
884497ed
DL
15427 if (INTEL_INFO(dev)->gen >= 9)
15428 return false;
15429
50a0bc90 15430 if (IS_HSW_ULT(dev_priv) || IS_BDW_ULT(dev_priv))
84b4e042
JB
15431 return false;
15432
920a14b2 15433 if (IS_CHERRYVIEW(dev_priv))
84b4e042
JB
15434 return false;
15435
4f8036a2
TU
15436 if (HAS_PCH_LPT_H(dev_priv) &&
15437 I915_READ(SFUSE_STRAP) & SFUSE_STRAP_CRT_DISABLED)
65e472e4
VS
15438 return false;
15439
70ac54d0 15440 /* DDI E can't be used if DDI A requires 4 lanes */
4f8036a2 15441 if (HAS_DDI(dev_priv) && I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_A_4_LANES)
70ac54d0
VS
15442 return false;
15443
e4abb733 15444 if (!dev_priv->vbt.int_crt_support)
84b4e042
JB
15445 return false;
15446
15447 return true;
15448}
15449
8090ba8c
ID
15450void intel_pps_unlock_regs_wa(struct drm_i915_private *dev_priv)
15451{
15452 int pps_num;
15453 int pps_idx;
15454
15455 if (HAS_DDI(dev_priv))
15456 return;
15457 /*
15458 * This w/a is needed at least on CPT/PPT, but to be sure apply it
15459 * everywhere where registers can be write protected.
15460 */
15461 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
15462 pps_num = 2;
15463 else
15464 pps_num = 1;
15465
15466 for (pps_idx = 0; pps_idx < pps_num; pps_idx++) {
15467 u32 val = I915_READ(PP_CONTROL(pps_idx));
15468
15469 val = (val & ~PANEL_UNLOCK_MASK) | PANEL_UNLOCK_REGS;
15470 I915_WRITE(PP_CONTROL(pps_idx), val);
15471 }
15472}
15473
44cb734c
ID
15474static void intel_pps_init(struct drm_i915_private *dev_priv)
15475{
15476 if (HAS_PCH_SPLIT(dev_priv) || IS_BROXTON(dev_priv))
15477 dev_priv->pps_mmio_base = PCH_PPS_BASE;
15478 else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
15479 dev_priv->pps_mmio_base = VLV_PPS_BASE;
15480 else
15481 dev_priv->pps_mmio_base = PPS_BASE;
8090ba8c
ID
15482
15483 intel_pps_unlock_regs_wa(dev_priv);
44cb734c
ID
15484}
15485
79e53945
JB
15486static void intel_setup_outputs(struct drm_device *dev)
15487{
fac5e23e 15488 struct drm_i915_private *dev_priv = to_i915(dev);
4ef69c7a 15489 struct intel_encoder *encoder;
cb0953d7 15490 bool dpd_is_edp = false;
79e53945 15491
44cb734c
ID
15492 intel_pps_init(dev_priv);
15493
97a824e1
ID
15494 /*
15495 * intel_edp_init_connector() depends on this completing first, to
15496 * prevent the registeration of both eDP and LVDS and the incorrect
15497 * sharing of the PPS.
15498 */
c9093354 15499 intel_lvds_init(dev);
79e53945 15500
84b4e042 15501 if (intel_crt_present(dev))
79935fca 15502 intel_crt_init(dev);
cb0953d7 15503
e2d214ae 15504 if (IS_BROXTON(dev_priv)) {
c776eb2e
VK
15505 /*
15506 * FIXME: Broxton doesn't support port detection via the
15507 * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
15508 * detect the ports.
15509 */
15510 intel_ddi_init(dev, PORT_A);
15511 intel_ddi_init(dev, PORT_B);
15512 intel_ddi_init(dev, PORT_C);
c6c794a2
SS
15513
15514 intel_dsi_init(dev);
4f8036a2 15515 } else if (HAS_DDI(dev_priv)) {
0e72a5b5
ED
15516 int found;
15517
de31facd
JB
15518 /*
15519 * Haswell uses DDI functions to detect digital outputs.
15520 * On SKL pre-D0 the strap isn't connected, so we assume
15521 * it's there.
15522 */
77179400 15523 found = I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_INIT_DISPLAY_DETECTED;
de31facd 15524 /* WaIgnoreDDIAStrap: skl */
0853723b 15525 if (found || IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv))
0e72a5b5
ED
15526 intel_ddi_init(dev, PORT_A);
15527
15528 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
15529 * register */
15530 found = I915_READ(SFUSE_STRAP);
15531
15532 if (found & SFUSE_STRAP_DDIB_DETECTED)
15533 intel_ddi_init(dev, PORT_B);
15534 if (found & SFUSE_STRAP_DDIC_DETECTED)
15535 intel_ddi_init(dev, PORT_C);
15536 if (found & SFUSE_STRAP_DDID_DETECTED)
15537 intel_ddi_init(dev, PORT_D);
2800e4c2
RV
15538 /*
15539 * On SKL we don't have a way to detect DDI-E so we rely on VBT.
15540 */
0853723b 15541 if ((IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv)) &&
2800e4c2
RV
15542 (dev_priv->vbt.ddi_port_info[PORT_E].supports_dp ||
15543 dev_priv->vbt.ddi_port_info[PORT_E].supports_dvi ||
15544 dev_priv->vbt.ddi_port_info[PORT_E].supports_hdmi))
15545 intel_ddi_init(dev, PORT_E);
15546
6e266956 15547 } else if (HAS_PCH_SPLIT(dev_priv)) {
cb0953d7 15548 int found;
5d8a7752 15549 dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
270b3042 15550
646d5772 15551 if (has_edp_a(dev_priv))
270b3042 15552 intel_dp_init(dev, DP_A, PORT_A);
cb0953d7 15553
dc0fa718 15554 if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
461ed3ca 15555 /* PCH SDVOB multiplex with HDMIB */
2a5c0832 15556 found = intel_sdvo_init(dev, PCH_SDVOB, PORT_B);
30ad48b7 15557 if (!found)
e2debe91 15558 intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
5eb08b69 15559 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
ab9d7c30 15560 intel_dp_init(dev, PCH_DP_B, PORT_B);
30ad48b7
ZW
15561 }
15562
dc0fa718 15563 if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
e2debe91 15564 intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
30ad48b7 15565
dc0fa718 15566 if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
e2debe91 15567 intel_hdmi_init(dev, PCH_HDMID, PORT_D);
30ad48b7 15568
5eb08b69 15569 if (I915_READ(PCH_DP_C) & DP_DETECTED)
ab9d7c30 15570 intel_dp_init(dev, PCH_DP_C, PORT_C);
5eb08b69 15571
270b3042 15572 if (I915_READ(PCH_DP_D) & DP_DETECTED)
ab9d7c30 15573 intel_dp_init(dev, PCH_DP_D, PORT_D);
920a14b2 15574 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
22f35042 15575 bool has_edp, has_port;
457c52d8 15576
e17ac6db
VS
15577 /*
15578 * The DP_DETECTED bit is the latched state of the DDC
15579 * SDA pin at boot. However since eDP doesn't require DDC
15580 * (no way to plug in a DP->HDMI dongle) the DDC pins for
15581 * eDP ports may have been muxed to an alternate function.
15582 * Thus we can't rely on the DP_DETECTED bit alone to detect
15583 * eDP ports. Consult the VBT as well as DP_DETECTED to
15584 * detect eDP ports.
22f35042
VS
15585 *
15586 * Sadly the straps seem to be missing sometimes even for HDMI
15587 * ports (eg. on Voyo V3 - CHT x7-Z8700), so check both strap
15588 * and VBT for the presence of the port. Additionally we can't
15589 * trust the port type the VBT declares as we've seen at least
15590 * HDMI ports that the VBT claim are DP or eDP.
e17ac6db 15591 */
457c52d8 15592 has_edp = intel_dp_is_edp(dev, PORT_B);
22f35042
VS
15593 has_port = intel_bios_is_port_present(dev_priv, PORT_B);
15594 if (I915_READ(VLV_DP_B) & DP_DETECTED || has_port)
457c52d8 15595 has_edp &= intel_dp_init(dev, VLV_DP_B, PORT_B);
22f35042 15596 if ((I915_READ(VLV_HDMIB) & SDVO_DETECTED || has_port) && !has_edp)
e66eb81d 15597 intel_hdmi_init(dev, VLV_HDMIB, PORT_B);
585a94b8 15598
457c52d8 15599 has_edp = intel_dp_is_edp(dev, PORT_C);
22f35042
VS
15600 has_port = intel_bios_is_port_present(dev_priv, PORT_C);
15601 if (I915_READ(VLV_DP_C) & DP_DETECTED || has_port)
457c52d8 15602 has_edp &= intel_dp_init(dev, VLV_DP_C, PORT_C);
22f35042 15603 if ((I915_READ(VLV_HDMIC) & SDVO_DETECTED || has_port) && !has_edp)
e66eb81d 15604 intel_hdmi_init(dev, VLV_HDMIC, PORT_C);
19c03924 15605
920a14b2 15606 if (IS_CHERRYVIEW(dev_priv)) {
22f35042
VS
15607 /*
15608 * eDP not supported on port D,
15609 * so no need to worry about it
15610 */
15611 has_port = intel_bios_is_port_present(dev_priv, PORT_D);
15612 if (I915_READ(CHV_DP_D) & DP_DETECTED || has_port)
e66eb81d 15613 intel_dp_init(dev, CHV_DP_D, PORT_D);
22f35042
VS
15614 if (I915_READ(CHV_HDMID) & SDVO_DETECTED || has_port)
15615 intel_hdmi_init(dev, CHV_HDMID, PORT_D);
9418c1f1
VS
15616 }
15617
3cfca973 15618 intel_dsi_init(dev);
5db94019 15619 } else if (!IS_GEN2(dev_priv) && !IS_PINEVIEW(dev_priv)) {
27185ae1 15620 bool found = false;
7d57382e 15621
e2debe91 15622 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
b01f2c3a 15623 DRM_DEBUG_KMS("probing SDVOB\n");
2a5c0832 15624 found = intel_sdvo_init(dev, GEN3_SDVOB, PORT_B);
9beb5fea 15625 if (!found && IS_G4X(dev_priv)) {
b01f2c3a 15626 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
e2debe91 15627 intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
b01f2c3a 15628 }
27185ae1 15629
9beb5fea 15630 if (!found && IS_G4X(dev_priv))
ab9d7c30 15631 intel_dp_init(dev, DP_B, PORT_B);
725e30ad 15632 }
13520b05
KH
15633
15634 /* Before G4X SDVOC doesn't have its own detect register */
13520b05 15635
e2debe91 15636 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
b01f2c3a 15637 DRM_DEBUG_KMS("probing SDVOC\n");
2a5c0832 15638 found = intel_sdvo_init(dev, GEN3_SDVOC, PORT_C);
b01f2c3a 15639 }
27185ae1 15640
e2debe91 15641 if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
27185ae1 15642
9beb5fea 15643 if (IS_G4X(dev_priv)) {
b01f2c3a 15644 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
e2debe91 15645 intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
b01f2c3a 15646 }
9beb5fea 15647 if (IS_G4X(dev_priv))
ab9d7c30 15648 intel_dp_init(dev, DP_C, PORT_C);
725e30ad 15649 }
27185ae1 15650
9beb5fea 15651 if (IS_G4X(dev_priv) && (I915_READ(DP_D) & DP_DETECTED))
ab9d7c30 15652 intel_dp_init(dev, DP_D, PORT_D);
5db94019 15653 } else if (IS_GEN2(dev_priv))
79e53945
JB
15654 intel_dvo_init(dev);
15655
56b857a5 15656 if (SUPPORTS_TV(dev_priv))
79e53945
JB
15657 intel_tv_init(dev);
15658
0bc12bcb 15659 intel_psr_init(dev);
7c8f8a70 15660
b2784e15 15661 for_each_intel_encoder(dev, encoder) {
4ef69c7a
CW
15662 encoder->base.possible_crtcs = encoder->crtc_mask;
15663 encoder->base.possible_clones =
66a9278e 15664 intel_encoder_clones(encoder);
79e53945 15665 }
47356eb6 15666
dde86e2d 15667 intel_init_pch_refclk(dev);
270b3042
SV
15668
15669 drm_helper_move_panel_connectors_to_head(dev);
79e53945
JB
15670}
15671
15672static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
15673{
60a5ca01 15674 struct drm_device *dev = fb->dev;
79e53945 15675 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
79e53945 15676
ef2d633e 15677 drm_framebuffer_cleanup(fb);
60a5ca01 15678 mutex_lock(&dev->struct_mutex);
ef2d633e 15679 WARN_ON(!intel_fb->obj->framebuffer_references--);
f8c417cd 15680 i915_gem_object_put(intel_fb->obj);
60a5ca01 15681 mutex_unlock(&dev->struct_mutex);
79e53945
JB
15682 kfree(intel_fb);
15683}
15684
15685static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
05394f39 15686 struct drm_file *file,
79e53945
JB
15687 unsigned int *handle)
15688{
15689 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
05394f39 15690 struct drm_i915_gem_object *obj = intel_fb->obj;
79e53945 15691
cc917ab4
CW
15692 if (obj->userptr.mm) {
15693 DRM_DEBUG("attempting to use a userptr for a framebuffer, denied\n");
15694 return -EINVAL;
15695 }
15696
05394f39 15697 return drm_gem_handle_create(file, &obj->base, handle);
79e53945
JB
15698}
15699
86c98588
RV
15700static int intel_user_framebuffer_dirty(struct drm_framebuffer *fb,
15701 struct drm_file *file,
15702 unsigned flags, unsigned color,
15703 struct drm_clip_rect *clips,
15704 unsigned num_clips)
15705{
15706 struct drm_device *dev = fb->dev;
15707 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
15708 struct drm_i915_gem_object *obj = intel_fb->obj;
15709
15710 mutex_lock(&dev->struct_mutex);
74b4ea1e 15711 intel_fb_obj_flush(obj, false, ORIGIN_DIRTYFB);
86c98588
RV
15712 mutex_unlock(&dev->struct_mutex);
15713
15714 return 0;
15715}
15716
79e53945
JB
15717static const struct drm_framebuffer_funcs intel_fb_funcs = {
15718 .destroy = intel_user_framebuffer_destroy,
15719 .create_handle = intel_user_framebuffer_create_handle,
86c98588 15720 .dirty = intel_user_framebuffer_dirty,
79e53945
JB
15721};
15722
b321803d 15723static
920a14b2
TU
15724u32 intel_fb_pitch_limit(struct drm_i915_private *dev_priv,
15725 uint64_t fb_modifier, uint32_t pixel_format)
b321803d 15726{
920a14b2 15727 u32 gen = INTEL_INFO(dev_priv)->gen;
b321803d
DL
15728
15729 if (gen >= 9) {
ac484963
VS
15730 int cpp = drm_format_plane_cpp(pixel_format, 0);
15731
b321803d
DL
15732 /* "The stride in bytes must not exceed the of the size of 8K
15733 * pixels and 32K bytes."
15734 */
ac484963 15735 return min(8192 * cpp, 32768);
920a14b2
TU
15736 } else if (gen >= 5 && !IS_VALLEYVIEW(dev_priv) &&
15737 !IS_CHERRYVIEW(dev_priv)) {
b321803d
DL
15738 return 32*1024;
15739 } else if (gen >= 4) {
15740 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
15741 return 16*1024;
15742 else
15743 return 32*1024;
15744 } else if (gen >= 3) {
15745 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
15746 return 8*1024;
15747 else
15748 return 16*1024;
15749 } else {
15750 /* XXX DSPC is limited to 4k tiled */
15751 return 8*1024;
15752 }
15753}
15754
b5ea642a
SV
15755static int intel_framebuffer_init(struct drm_device *dev,
15756 struct intel_framebuffer *intel_fb,
15757 struct drm_mode_fb_cmd2 *mode_cmd,
15758 struct drm_i915_gem_object *obj)
79e53945 15759{
7b49f948 15760 struct drm_i915_private *dev_priv = to_i915(dev);
c2ff7370 15761 unsigned int tiling = i915_gem_object_get_tiling(obj);
79e53945 15762 int ret;
b321803d 15763 u32 pitch_limit, stride_alignment;
d3828147 15764 char *format_name;
79e53945 15765
dd4916c5
SV
15766 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
15767
2a80eada 15768 if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
c2ff7370
VS
15769 /*
15770 * If there's a fence, enforce that
15771 * the fb modifier and tiling mode match.
15772 */
15773 if (tiling != I915_TILING_NONE &&
15774 tiling != intel_fb_modifier_to_tiling(mode_cmd->modifier[0])) {
2a80eada
SV
15775 DRM_DEBUG("tiling_mode doesn't match fb modifier\n");
15776 return -EINVAL;
15777 }
15778 } else {
c2ff7370 15779 if (tiling == I915_TILING_X) {
2a80eada 15780 mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
c2ff7370 15781 } else if (tiling == I915_TILING_Y) {
2a80eada
SV
15782 DRM_DEBUG("No Y tiling for legacy addfb\n");
15783 return -EINVAL;
15784 }
15785 }
15786
9a8f0a12
TU
15787 /* Passed in modifier sanity checking. */
15788 switch (mode_cmd->modifier[0]) {
15789 case I915_FORMAT_MOD_Y_TILED:
15790 case I915_FORMAT_MOD_Yf_TILED:
15791 if (INTEL_INFO(dev)->gen < 9) {
15792 DRM_DEBUG("Unsupported tiling 0x%llx!\n",
15793 mode_cmd->modifier[0]);
15794 return -EINVAL;
15795 }
15796 case DRM_FORMAT_MOD_NONE:
15797 case I915_FORMAT_MOD_X_TILED:
15798 break;
15799 default:
c0f40428
JB
15800 DRM_DEBUG("Unsupported fb modifier 0x%llx!\n",
15801 mode_cmd->modifier[0]);
57cd6508 15802 return -EINVAL;
c16ed4be 15803 }
57cd6508 15804
c2ff7370
VS
15805 /*
15806 * gen2/3 display engine uses the fence if present,
15807 * so the tiling mode must match the fb modifier exactly.
15808 */
15809 if (INTEL_INFO(dev_priv)->gen < 4 &&
15810 tiling != intel_fb_modifier_to_tiling(mode_cmd->modifier[0])) {
15811 DRM_DEBUG("tiling_mode must match fb modifier exactly on gen2/3\n");
15812 return -EINVAL;
15813 }
15814
7b49f948
VS
15815 stride_alignment = intel_fb_stride_alignment(dev_priv,
15816 mode_cmd->modifier[0],
b321803d
DL
15817 mode_cmd->pixel_format);
15818 if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
15819 DRM_DEBUG("pitch (%d) must be at least %u byte aligned\n",
15820 mode_cmd->pitches[0], stride_alignment);
57cd6508 15821 return -EINVAL;
c16ed4be 15822 }
57cd6508 15823
920a14b2 15824 pitch_limit = intel_fb_pitch_limit(dev_priv, mode_cmd->modifier[0],
b321803d 15825 mode_cmd->pixel_format);
a35cdaa0 15826 if (mode_cmd->pitches[0] > pitch_limit) {
b321803d
DL
15827 DRM_DEBUG("%s pitch (%u) must be at less than %d\n",
15828 mode_cmd->modifier[0] != DRM_FORMAT_MOD_NONE ?
2a80eada 15829 "tiled" : "linear",
a35cdaa0 15830 mode_cmd->pitches[0], pitch_limit);
5d7bd705 15831 return -EINVAL;
c16ed4be 15832 }
5d7bd705 15833
c2ff7370
VS
15834 /*
15835 * If there's a fence, enforce that
15836 * the fb pitch and fence stride match.
15837 */
15838 if (tiling != I915_TILING_NONE &&
3e510a8e 15839 mode_cmd->pitches[0] != i915_gem_object_get_stride(obj)) {
c16ed4be 15840 DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
3e510a8e
CW
15841 mode_cmd->pitches[0],
15842 i915_gem_object_get_stride(obj));
5d7bd705 15843 return -EINVAL;
c16ed4be 15844 }
5d7bd705 15845
57779d06 15846 /* Reject formats not supported by any plane early. */
308e5bcb 15847 switch (mode_cmd->pixel_format) {
57779d06 15848 case DRM_FORMAT_C8:
04b3924d
VS
15849 case DRM_FORMAT_RGB565:
15850 case DRM_FORMAT_XRGB8888:
15851 case DRM_FORMAT_ARGB8888:
57779d06
VS
15852 break;
15853 case DRM_FORMAT_XRGB1555:
c16ed4be 15854 if (INTEL_INFO(dev)->gen > 3) {
90844f00
EE
15855 format_name = drm_get_format_name(mode_cmd->pixel_format);
15856 DRM_DEBUG("unsupported pixel format: %s\n", format_name);
15857 kfree(format_name);
57779d06 15858 return -EINVAL;
c16ed4be 15859 }
57779d06 15860 break;
57779d06 15861 case DRM_FORMAT_ABGR8888:
920a14b2 15862 if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv) &&
666a4537 15863 INTEL_INFO(dev)->gen < 9) {
90844f00
EE
15864 format_name = drm_get_format_name(mode_cmd->pixel_format);
15865 DRM_DEBUG("unsupported pixel format: %s\n", format_name);
15866 kfree(format_name);
6c0fd451
DL
15867 return -EINVAL;
15868 }
15869 break;
15870 case DRM_FORMAT_XBGR8888:
04b3924d 15871 case DRM_FORMAT_XRGB2101010:
57779d06 15872 case DRM_FORMAT_XBGR2101010:
c16ed4be 15873 if (INTEL_INFO(dev)->gen < 4) {
90844f00
EE
15874 format_name = drm_get_format_name(mode_cmd->pixel_format);
15875 DRM_DEBUG("unsupported pixel format: %s\n", format_name);
15876 kfree(format_name);
57779d06 15877 return -EINVAL;
c16ed4be 15878 }
b5626747 15879 break;
7531208b 15880 case DRM_FORMAT_ABGR2101010:
920a14b2 15881 if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv)) {
90844f00
EE
15882 format_name = drm_get_format_name(mode_cmd->pixel_format);
15883 DRM_DEBUG("unsupported pixel format: %s\n", format_name);
15884 kfree(format_name);
7531208b
DL
15885 return -EINVAL;
15886 }
15887 break;
04b3924d
VS
15888 case DRM_FORMAT_YUYV:
15889 case DRM_FORMAT_UYVY:
15890 case DRM_FORMAT_YVYU:
15891 case DRM_FORMAT_VYUY:
c16ed4be 15892 if (INTEL_INFO(dev)->gen < 5) {
90844f00
EE
15893 format_name = drm_get_format_name(mode_cmd->pixel_format);
15894 DRM_DEBUG("unsupported pixel format: %s\n", format_name);
15895 kfree(format_name);
57779d06 15896 return -EINVAL;
c16ed4be 15897 }
57cd6508
CW
15898 break;
15899 default:
90844f00
EE
15900 format_name = drm_get_format_name(mode_cmd->pixel_format);
15901 DRM_DEBUG("unsupported pixel format: %s\n", format_name);
15902 kfree(format_name);
57cd6508
CW
15903 return -EINVAL;
15904 }
15905
90f9a336
VS
15906 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
15907 if (mode_cmd->offsets[0] != 0)
15908 return -EINVAL;
15909
c7d73f6a
SV
15910 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
15911 intel_fb->obj = obj;
15912
6687c906
VS
15913 ret = intel_fill_fb_info(dev_priv, &intel_fb->base);
15914 if (ret)
15915 return ret;
2d7a215f 15916
79e53945
JB
15917 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
15918 if (ret) {
15919 DRM_ERROR("framebuffer init failed %d\n", ret);
15920 return ret;
15921 }
15922
0b05e1e0
VS
15923 intel_fb->obj->framebuffer_references++;
15924
79e53945
JB
15925 return 0;
15926}
15927
79e53945
JB
15928static struct drm_framebuffer *
15929intel_user_framebuffer_create(struct drm_device *dev,
15930 struct drm_file *filp,
1eb83451 15931 const struct drm_mode_fb_cmd2 *user_mode_cmd)
79e53945 15932{
dcb1394e 15933 struct drm_framebuffer *fb;
05394f39 15934 struct drm_i915_gem_object *obj;
76dc3769 15935 struct drm_mode_fb_cmd2 mode_cmd = *user_mode_cmd;
79e53945 15936
03ac0642
CW
15937 obj = i915_gem_object_lookup(filp, mode_cmd.handles[0]);
15938 if (!obj)
cce13ff7 15939 return ERR_PTR(-ENOENT);
79e53945 15940
92907cbb 15941 fb = intel_framebuffer_create(dev, &mode_cmd, obj);
dcb1394e 15942 if (IS_ERR(fb))
f0cd5182 15943 i915_gem_object_put(obj);
dcb1394e
LW
15944
15945 return fb;
79e53945
JB
15946}
15947
79e53945 15948static const struct drm_mode_config_funcs intel_mode_funcs = {
79e53945 15949 .fb_create = intel_user_framebuffer_create,
0632fef6 15950 .output_poll_changed = intel_fbdev_output_poll_changed,
5ee67f1c
MR
15951 .atomic_check = intel_atomic_check,
15952 .atomic_commit = intel_atomic_commit,
de419ab6
ML
15953 .atomic_state_alloc = intel_atomic_state_alloc,
15954 .atomic_state_clear = intel_atomic_state_clear,
79e53945
JB
15955};
15956
88212941
ID
15957/**
15958 * intel_init_display_hooks - initialize the display modesetting hooks
15959 * @dev_priv: device private
15960 */
15961void intel_init_display_hooks(struct drm_i915_private *dev_priv)
e70236a8 15962{
88212941 15963 if (INTEL_INFO(dev_priv)->gen >= 9) {
bc8d7dff 15964 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
5724dbd1
DL
15965 dev_priv->display.get_initial_plane_config =
15966 skylake_get_initial_plane_config;
bc8d7dff
DL
15967 dev_priv->display.crtc_compute_clock =
15968 haswell_crtc_compute_clock;
15969 dev_priv->display.crtc_enable = haswell_crtc_enable;
15970 dev_priv->display.crtc_disable = haswell_crtc_disable;
88212941 15971 } else if (HAS_DDI(dev_priv)) {
0e8ffe1b 15972 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
5724dbd1
DL
15973 dev_priv->display.get_initial_plane_config =
15974 ironlake_get_initial_plane_config;
797d0259
ACO
15975 dev_priv->display.crtc_compute_clock =
15976 haswell_crtc_compute_clock;
4f771f10
PZ
15977 dev_priv->display.crtc_enable = haswell_crtc_enable;
15978 dev_priv->display.crtc_disable = haswell_crtc_disable;
88212941 15979 } else if (HAS_PCH_SPLIT(dev_priv)) {
0e8ffe1b 15980 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
5724dbd1
DL
15981 dev_priv->display.get_initial_plane_config =
15982 ironlake_get_initial_plane_config;
3fb37703
ACO
15983 dev_priv->display.crtc_compute_clock =
15984 ironlake_crtc_compute_clock;
76e5a89c
SV
15985 dev_priv->display.crtc_enable = ironlake_crtc_enable;
15986 dev_priv->display.crtc_disable = ironlake_crtc_disable;
65b3d6a9 15987 } else if (IS_CHERRYVIEW(dev_priv)) {
89b667f8 15988 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
5724dbd1
DL
15989 dev_priv->display.get_initial_plane_config =
15990 i9xx_get_initial_plane_config;
65b3d6a9
ACO
15991 dev_priv->display.crtc_compute_clock = chv_crtc_compute_clock;
15992 dev_priv->display.crtc_enable = valleyview_crtc_enable;
15993 dev_priv->display.crtc_disable = i9xx_crtc_disable;
15994 } else if (IS_VALLEYVIEW(dev_priv)) {
15995 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
15996 dev_priv->display.get_initial_plane_config =
15997 i9xx_get_initial_plane_config;
15998 dev_priv->display.crtc_compute_clock = vlv_crtc_compute_clock;
89b667f8
JB
15999 dev_priv->display.crtc_enable = valleyview_crtc_enable;
16000 dev_priv->display.crtc_disable = i9xx_crtc_disable;
19ec6693
ACO
16001 } else if (IS_G4X(dev_priv)) {
16002 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
16003 dev_priv->display.get_initial_plane_config =
16004 i9xx_get_initial_plane_config;
16005 dev_priv->display.crtc_compute_clock = g4x_crtc_compute_clock;
16006 dev_priv->display.crtc_enable = i9xx_crtc_enable;
16007 dev_priv->display.crtc_disable = i9xx_crtc_disable;
70e8aa21
ACO
16008 } else if (IS_PINEVIEW(dev_priv)) {
16009 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
16010 dev_priv->display.get_initial_plane_config =
16011 i9xx_get_initial_plane_config;
16012 dev_priv->display.crtc_compute_clock = pnv_crtc_compute_clock;
16013 dev_priv->display.crtc_enable = i9xx_crtc_enable;
16014 dev_priv->display.crtc_disable = i9xx_crtc_disable;
81c97f52 16015 } else if (!IS_GEN2(dev_priv)) {
0e8ffe1b 16016 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
5724dbd1
DL
16017 dev_priv->display.get_initial_plane_config =
16018 i9xx_get_initial_plane_config;
d6dfee7a 16019 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
76e5a89c
SV
16020 dev_priv->display.crtc_enable = i9xx_crtc_enable;
16021 dev_priv->display.crtc_disable = i9xx_crtc_disable;
81c97f52
ACO
16022 } else {
16023 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
16024 dev_priv->display.get_initial_plane_config =
16025 i9xx_get_initial_plane_config;
16026 dev_priv->display.crtc_compute_clock = i8xx_crtc_compute_clock;
16027 dev_priv->display.crtc_enable = i9xx_crtc_enable;
16028 dev_priv->display.crtc_disable = i9xx_crtc_disable;
f564048e 16029 }
e70236a8 16030
e70236a8 16031 /* Returns the core display clock speed */
88212941 16032 if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv))
1652d19e
VS
16033 dev_priv->display.get_display_clock_speed =
16034 skylake_get_display_clock_speed;
88212941 16035 else if (IS_BROXTON(dev_priv))
acd3f3d3
BP
16036 dev_priv->display.get_display_clock_speed =
16037 broxton_get_display_clock_speed;
88212941 16038 else if (IS_BROADWELL(dev_priv))
1652d19e
VS
16039 dev_priv->display.get_display_clock_speed =
16040 broadwell_get_display_clock_speed;
88212941 16041 else if (IS_HASWELL(dev_priv))
1652d19e
VS
16042 dev_priv->display.get_display_clock_speed =
16043 haswell_get_display_clock_speed;
88212941 16044 else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
25eb05fc
JB
16045 dev_priv->display.get_display_clock_speed =
16046 valleyview_get_display_clock_speed;
88212941 16047 else if (IS_GEN5(dev_priv))
b37a6434
VS
16048 dev_priv->display.get_display_clock_speed =
16049 ilk_get_display_clock_speed;
88212941
ID
16050 else if (IS_I945G(dev_priv) || IS_BROADWATER(dev_priv) ||
16051 IS_GEN6(dev_priv) || IS_IVYBRIDGE(dev_priv))
e70236a8
JB
16052 dev_priv->display.get_display_clock_speed =
16053 i945_get_display_clock_speed;
88212941 16054 else if (IS_GM45(dev_priv))
34edce2f
VS
16055 dev_priv->display.get_display_clock_speed =
16056 gm45_get_display_clock_speed;
88212941 16057 else if (IS_CRESTLINE(dev_priv))
34edce2f
VS
16058 dev_priv->display.get_display_clock_speed =
16059 i965gm_get_display_clock_speed;
88212941 16060 else if (IS_PINEVIEW(dev_priv))
34edce2f
VS
16061 dev_priv->display.get_display_clock_speed =
16062 pnv_get_display_clock_speed;
88212941 16063 else if (IS_G33(dev_priv) || IS_G4X(dev_priv))
34edce2f
VS
16064 dev_priv->display.get_display_clock_speed =
16065 g33_get_display_clock_speed;
88212941 16066 else if (IS_I915G(dev_priv))
e70236a8
JB
16067 dev_priv->display.get_display_clock_speed =
16068 i915_get_display_clock_speed;
88212941 16069 else if (IS_I945GM(dev_priv) || IS_845G(dev_priv))
e70236a8
JB
16070 dev_priv->display.get_display_clock_speed =
16071 i9xx_misc_get_display_clock_speed;
88212941 16072 else if (IS_I915GM(dev_priv))
e70236a8
JB
16073 dev_priv->display.get_display_clock_speed =
16074 i915gm_get_display_clock_speed;
88212941 16075 else if (IS_I865G(dev_priv))
e70236a8
JB
16076 dev_priv->display.get_display_clock_speed =
16077 i865_get_display_clock_speed;
88212941 16078 else if (IS_I85X(dev_priv))
e70236a8 16079 dev_priv->display.get_display_clock_speed =
1b1d2716 16080 i85x_get_display_clock_speed;
623e01e5 16081 else { /* 830 */
88212941 16082 WARN(!IS_I830(dev_priv), "Unknown platform. Assuming 133 MHz CDCLK\n");
e70236a8
JB
16083 dev_priv->display.get_display_clock_speed =
16084 i830_get_display_clock_speed;
623e01e5 16085 }
e70236a8 16086
88212941 16087 if (IS_GEN5(dev_priv)) {
3bb11b53 16088 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
88212941 16089 } else if (IS_GEN6(dev_priv)) {
3bb11b53 16090 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
88212941 16091 } else if (IS_IVYBRIDGE(dev_priv)) {
3bb11b53
SJ
16092 /* FIXME: detect B0+ stepping and use auto training */
16093 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
88212941 16094 } else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
3bb11b53 16095 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
445e780b
VS
16096 }
16097
16098 if (IS_BROADWELL(dev_priv)) {
16099 dev_priv->display.modeset_commit_cdclk =
16100 broadwell_modeset_commit_cdclk;
16101 dev_priv->display.modeset_calc_cdclk =
16102 broadwell_modeset_calc_cdclk;
88212941 16103 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
27c329ed
ML
16104 dev_priv->display.modeset_commit_cdclk =
16105 valleyview_modeset_commit_cdclk;
16106 dev_priv->display.modeset_calc_cdclk =
16107 valleyview_modeset_calc_cdclk;
88212941 16108 } else if (IS_BROXTON(dev_priv)) {
27c329ed 16109 dev_priv->display.modeset_commit_cdclk =
324513c0 16110 bxt_modeset_commit_cdclk;
27c329ed 16111 dev_priv->display.modeset_calc_cdclk =
324513c0 16112 bxt_modeset_calc_cdclk;
c89e39f3
CT
16113 } else if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv)) {
16114 dev_priv->display.modeset_commit_cdclk =
16115 skl_modeset_commit_cdclk;
16116 dev_priv->display.modeset_calc_cdclk =
16117 skl_modeset_calc_cdclk;
e70236a8 16118 }
5a21b665 16119
27082493
L
16120 if (dev_priv->info.gen >= 9)
16121 dev_priv->display.update_crtcs = skl_update_crtcs;
16122 else
16123 dev_priv->display.update_crtcs = intel_update_crtcs;
16124
5a21b665
SV
16125 switch (INTEL_INFO(dev_priv)->gen) {
16126 case 2:
16127 dev_priv->display.queue_flip = intel_gen2_queue_flip;
16128 break;
16129
16130 case 3:
16131 dev_priv->display.queue_flip = intel_gen3_queue_flip;
16132 break;
16133
16134 case 4:
16135 case 5:
16136 dev_priv->display.queue_flip = intel_gen4_queue_flip;
16137 break;
16138
16139 case 6:
16140 dev_priv->display.queue_flip = intel_gen6_queue_flip;
16141 break;
16142 case 7:
16143 case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
16144 dev_priv->display.queue_flip = intel_gen7_queue_flip;
16145 break;
16146 case 9:
16147 /* Drop through - unsupported since execlist only. */
16148 default:
16149 /* Default just returns -ENODEV to indicate unsupported */
16150 dev_priv->display.queue_flip = intel_default_queue_flip;
16151 }
e70236a8
JB
16152}
16153
b690e96c
JB
16154/*
16155 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
16156 * resume, or other times. This quirk makes sure that's the case for
16157 * affected systems.
16158 */
0206e353 16159static void quirk_pipea_force(struct drm_device *dev)
b690e96c 16160{
fac5e23e 16161 struct drm_i915_private *dev_priv = to_i915(dev);
b690e96c
JB
16162
16163 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
bc0daf48 16164 DRM_INFO("applying pipe a force quirk\n");
b690e96c
JB
16165}
16166
b6b5d049
VS
16167static void quirk_pipeb_force(struct drm_device *dev)
16168{
fac5e23e 16169 struct drm_i915_private *dev_priv = to_i915(dev);
b6b5d049
VS
16170
16171 dev_priv->quirks |= QUIRK_PIPEB_FORCE;
16172 DRM_INFO("applying pipe b force quirk\n");
16173}
16174
435793df
KP
16175/*
16176 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
16177 */
16178static void quirk_ssc_force_disable(struct drm_device *dev)
16179{
fac5e23e 16180 struct drm_i915_private *dev_priv = to_i915(dev);
435793df 16181 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
bc0daf48 16182 DRM_INFO("applying lvds SSC disable quirk\n");
435793df
KP
16183}
16184
4dca20ef 16185/*
5a15ab5b
CE
16186 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
16187 * brightness value
4dca20ef
CE
16188 */
16189static void quirk_invert_brightness(struct drm_device *dev)
16190{
fac5e23e 16191 struct drm_i915_private *dev_priv = to_i915(dev);
4dca20ef 16192 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
bc0daf48 16193 DRM_INFO("applying inverted panel brightness quirk\n");
435793df
KP
16194}
16195
9c72cc6f
SD
16196/* Some VBT's incorrectly indicate no backlight is present */
16197static void quirk_backlight_present(struct drm_device *dev)
16198{
fac5e23e 16199 struct drm_i915_private *dev_priv = to_i915(dev);
9c72cc6f
SD
16200 dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
16201 DRM_INFO("applying backlight present quirk\n");
16202}
16203
b690e96c
JB
16204struct intel_quirk {
16205 int device;
16206 int subsystem_vendor;
16207 int subsystem_device;
16208 void (*hook)(struct drm_device *dev);
16209};
16210
5f85f176
EE
16211/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
16212struct intel_dmi_quirk {
16213 void (*hook)(struct drm_device *dev);
16214 const struct dmi_system_id (*dmi_id_list)[];
16215};
16216
16217static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
16218{
16219 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
16220 return 1;
16221}
16222
16223static const struct intel_dmi_quirk intel_dmi_quirks[] = {
16224 {
16225 .dmi_id_list = &(const struct dmi_system_id[]) {
16226 {
16227 .callback = intel_dmi_reverse_brightness,
16228 .ident = "NCR Corporation",
16229 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
16230 DMI_MATCH(DMI_PRODUCT_NAME, ""),
16231 },
16232 },
16233 { } /* terminating entry */
16234 },
16235 .hook = quirk_invert_brightness,
16236 },
16237};
16238
c43b5634 16239static struct intel_quirk intel_quirks[] = {
b690e96c
JB
16240 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
16241 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
16242
b690e96c
JB
16243 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
16244 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
16245
5f080c0f
VS
16246 /* 830 needs to leave pipe A & dpll A up */
16247 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
16248
b6b5d049
VS
16249 /* 830 needs to leave pipe B & dpll B up */
16250 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
16251
435793df
KP
16252 /* Lenovo U160 cannot use SSC on LVDS */
16253 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
070d329a
MAS
16254
16255 /* Sony Vaio Y cannot use SSC on LVDS */
16256 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
5a15ab5b 16257
be505f64
AH
16258 /* Acer Aspire 5734Z must invert backlight brightness */
16259 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
16260
16261 /* Acer/eMachines G725 */
16262 { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
16263
16264 /* Acer/eMachines e725 */
16265 { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
16266
16267 /* Acer/Packard Bell NCL20 */
16268 { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
16269
16270 /* Acer Aspire 4736Z */
16271 { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
0f540c3a
JN
16272
16273 /* Acer Aspire 5336 */
16274 { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
2e93a1aa
SD
16275
16276 /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
16277 { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
d4967d8c 16278
dfb3d47b
SD
16279 /* Acer C720 Chromebook (Core i3 4005U) */
16280 { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
16281
b2a9601c 16282 /* Apple Macbook 2,1 (Core 2 T7400) */
16283 { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
16284
1b9448b0
JN
16285 /* Apple Macbook 4,1 */
16286 { 0x2a02, 0x106b, 0x00a1, quirk_backlight_present },
16287
d4967d8c
SD
16288 /* Toshiba CB35 Chromebook (Celeron 2955U) */
16289 { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
724cb06f
SD
16290
16291 /* HP Chromebook 14 (Celeron 2955U) */
16292 { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
cf6f0af9
JN
16293
16294 /* Dell Chromebook 11 */
16295 { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
9be64eee
JN
16296
16297 /* Dell Chromebook 11 (2015 version) */
16298 { 0x0a16, 0x1028, 0x0a35, quirk_backlight_present },
b690e96c
JB
16299};
16300
16301static void intel_init_quirks(struct drm_device *dev)
16302{
16303 struct pci_dev *d = dev->pdev;
16304 int i;
16305
16306 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
16307 struct intel_quirk *q = &intel_quirks[i];
16308
16309 if (d->device == q->device &&
16310 (d->subsystem_vendor == q->subsystem_vendor ||
16311 q->subsystem_vendor == PCI_ANY_ID) &&
16312 (d->subsystem_device == q->subsystem_device ||
16313 q->subsystem_device == PCI_ANY_ID))
16314 q->hook(dev);
16315 }
5f85f176
EE
16316 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
16317 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
16318 intel_dmi_quirks[i].hook(dev);
16319 }
b690e96c
JB
16320}
16321
9cce37f4
JB
16322/* Disable the VGA plane that we never use */
16323static void i915_disable_vga(struct drm_device *dev)
16324{
fac5e23e 16325 struct drm_i915_private *dev_priv = to_i915(dev);
52a05c30 16326 struct pci_dev *pdev = dev_priv->drm.pdev;
9cce37f4 16327 u8 sr1;
920a14b2 16328 i915_reg_t vga_reg = i915_vgacntrl_reg(dev_priv);
9cce37f4 16329
2b37c616 16330 /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
52a05c30 16331 vga_get_uninterruptible(pdev, VGA_RSRC_LEGACY_IO);
3fdcf431 16332 outb(SR01, VGA_SR_INDEX);
9cce37f4
JB
16333 sr1 = inb(VGA_SR_DATA);
16334 outb(sr1 | 1<<5, VGA_SR_DATA);
52a05c30 16335 vga_put(pdev, VGA_RSRC_LEGACY_IO);
9cce37f4
JB
16336 udelay(300);
16337
01f5a626 16338 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
9cce37f4
JB
16339 POSTING_READ(vga_reg);
16340}
16341
f817586c
SV
16342void intel_modeset_init_hw(struct drm_device *dev)
16343{
fac5e23e 16344 struct drm_i915_private *dev_priv = to_i915(dev);
1a617b77 16345
4c75b940 16346 intel_update_cdclk(dev_priv);
1a617b77
ML
16347
16348 dev_priv->atomic_cdclk_freq = dev_priv->cdclk_freq;
16349
46f16e63 16350 intel_init_clock_gating(dev_priv);
f817586c
SV
16351}
16352
d93c0372
MR
16353/*
16354 * Calculate what we think the watermarks should be for the state we've read
16355 * out of the hardware and then immediately program those watermarks so that
16356 * we ensure the hardware settings match our internal state.
16357 *
16358 * We can calculate what we think WM's should be by creating a duplicate of the
16359 * current state (which was constructed during hardware readout) and running it
16360 * through the atomic check code to calculate new watermark values in the
16361 * state object.
16362 */
16363static void sanitize_watermarks(struct drm_device *dev)
16364{
16365 struct drm_i915_private *dev_priv = to_i915(dev);
16366 struct drm_atomic_state *state;
16367 struct drm_crtc *crtc;
16368 struct drm_crtc_state *cstate;
16369 struct drm_modeset_acquire_ctx ctx;
16370 int ret;
16371 int i;
16372
16373 /* Only supported on platforms that use atomic watermark design */
ed4a6a7c 16374 if (!dev_priv->display.optimize_watermarks)
d93c0372
MR
16375 return;
16376
16377 /*
16378 * We need to hold connection_mutex before calling duplicate_state so
16379 * that the connector loop is protected.
16380 */
16381 drm_modeset_acquire_init(&ctx, 0);
16382retry:
0cd1262d 16383 ret = drm_modeset_lock_all_ctx(dev, &ctx);
d93c0372
MR
16384 if (ret == -EDEADLK) {
16385 drm_modeset_backoff(&ctx);
16386 goto retry;
16387 } else if (WARN_ON(ret)) {
0cd1262d 16388 goto fail;
d93c0372
MR
16389 }
16390
16391 state = drm_atomic_helper_duplicate_state(dev, &ctx);
16392 if (WARN_ON(IS_ERR(state)))
0cd1262d 16393 goto fail;
d93c0372 16394
ed4a6a7c
MR
16395 /*
16396 * Hardware readout is the only time we don't want to calculate
16397 * intermediate watermarks (since we don't trust the current
16398 * watermarks).
16399 */
16400 to_intel_atomic_state(state)->skip_intermediate_wm = true;
16401
d93c0372
MR
16402 ret = intel_atomic_check(dev, state);
16403 if (ret) {
16404 /*
16405 * If we fail here, it means that the hardware appears to be
16406 * programmed in a way that shouldn't be possible, given our
16407 * understanding of watermark requirements. This might mean a
16408 * mistake in the hardware readout code or a mistake in the
16409 * watermark calculations for a given platform. Raise a WARN
16410 * so that this is noticeable.
16411 *
16412 * If this actually happens, we'll have to just leave the
16413 * BIOS-programmed watermarks untouched and hope for the best.
16414 */
16415 WARN(true, "Could not determine valid watermarks for inherited state\n");
b9a1b717 16416 goto put_state;
d93c0372
MR
16417 }
16418
16419 /* Write calculated watermark values back */
d93c0372
MR
16420 for_each_crtc_in_state(state, crtc, cstate, i) {
16421 struct intel_crtc_state *cs = to_intel_crtc_state(cstate);
16422
ed4a6a7c
MR
16423 cs->wm.need_postvbl_update = true;
16424 dev_priv->display.optimize_watermarks(cs);
d93c0372
MR
16425 }
16426
b9a1b717 16427put_state:
0853695c 16428 drm_atomic_state_put(state);
0cd1262d 16429fail:
d93c0372
MR
16430 drm_modeset_drop_locks(&ctx);
16431 drm_modeset_acquire_fini(&ctx);
16432}
16433
b079bd17 16434int intel_modeset_init(struct drm_device *dev)
79e53945 16435{
72e96d64
JL
16436 struct drm_i915_private *dev_priv = to_i915(dev);
16437 struct i915_ggtt *ggtt = &dev_priv->ggtt;
8cc87b75 16438 enum pipe pipe;
46f297fb 16439 struct intel_crtc *crtc;
79e53945
JB
16440
16441 drm_mode_config_init(dev);
16442
16443 dev->mode_config.min_width = 0;
16444 dev->mode_config.min_height = 0;
16445
019d96cb
DA
16446 dev->mode_config.preferred_depth = 24;
16447 dev->mode_config.prefer_shadow = 1;
16448
25bab385
TU
16449 dev->mode_config.allow_fb_modifiers = true;
16450
e6ecefaa 16451 dev->mode_config.funcs = &intel_mode_funcs;
79e53945 16452
b690e96c
JB
16453 intel_init_quirks(dev);
16454
62d75df7 16455 intel_init_pm(dev_priv);
1fa61106 16456
b7f05d4a 16457 if (INTEL_INFO(dev_priv)->num_pipes == 0)
b079bd17 16458 return 0;
e3c74757 16459
69f92f67
LW
16460 /*
16461 * There may be no VBT; and if the BIOS enabled SSC we can
16462 * just keep using it to avoid unnecessary flicker. Whereas if the
16463 * BIOS isn't using it, don't assume it will work even if the VBT
16464 * indicates as much.
16465 */
6e266956 16466 if (HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv)) {
69f92f67
LW
16467 bool bios_lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
16468 DREF_SSC1_ENABLE);
16469
16470 if (dev_priv->vbt.lvds_use_ssc != bios_lvds_use_ssc) {
16471 DRM_DEBUG_KMS("SSC %sabled by BIOS, overriding VBT which says %sabled\n",
16472 bios_lvds_use_ssc ? "en" : "dis",
16473 dev_priv->vbt.lvds_use_ssc ? "en" : "dis");
16474 dev_priv->vbt.lvds_use_ssc = bios_lvds_use_ssc;
16475 }
16476 }
16477
5db94019 16478 if (IS_GEN2(dev_priv)) {
a6c45cf0
CW
16479 dev->mode_config.max_width = 2048;
16480 dev->mode_config.max_height = 2048;
5db94019 16481 } else if (IS_GEN3(dev_priv)) {
5e4d6fa7
KP
16482 dev->mode_config.max_width = 4096;
16483 dev->mode_config.max_height = 4096;
79e53945 16484 } else {
a6c45cf0
CW
16485 dev->mode_config.max_width = 8192;
16486 dev->mode_config.max_height = 8192;
79e53945 16487 }
068be561 16488
50a0bc90
TU
16489 if (IS_845G(dev_priv) || IS_I865G(dev_priv)) {
16490 dev->mode_config.cursor_width = IS_845G(dev_priv) ? 64 : 512;
dc41c154 16491 dev->mode_config.cursor_height = 1023;
5db94019 16492 } else if (IS_GEN2(dev_priv)) {
068be561
DL
16493 dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
16494 dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
16495 } else {
16496 dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
16497 dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
16498 }
16499
72e96d64 16500 dev->mode_config.fb_base = ggtt->mappable_base;
79e53945 16501
28c97730 16502 DRM_DEBUG_KMS("%d display pipe%s available.\n",
b7f05d4a
TU
16503 INTEL_INFO(dev_priv)->num_pipes,
16504 INTEL_INFO(dev_priv)->num_pipes > 1 ? "s" : "");
79e53945 16505
055e393f 16506 for_each_pipe(dev_priv, pipe) {
b079bd17
VS
16507 int ret;
16508
5ab0d85b 16509 ret = intel_crtc_init(dev_priv, pipe);
b079bd17
VS
16510 if (ret) {
16511 drm_mode_config_cleanup(dev);
16512 return ret;
16513 }
79e53945
JB
16514 }
16515
bfa7df01 16516 intel_update_czclk(dev_priv);
4c75b940 16517 intel_update_cdclk(dev_priv);
bfa7df01 16518
e72f9fbf 16519 intel_shared_dpll_init(dev);
ee7b9f93 16520
b2045352 16521 if (dev_priv->max_cdclk_freq == 0)
4c75b940 16522 intel_update_max_cdclk(dev_priv);
b2045352 16523
9cce37f4
JB
16524 /* Just disable it once at startup */
16525 i915_disable_vga(dev);
79e53945 16526 intel_setup_outputs(dev);
11be49eb 16527
6e9f798d 16528 drm_modeset_lock_all(dev);
043e9bda 16529 intel_modeset_setup_hw_state(dev);
6e9f798d 16530 drm_modeset_unlock_all(dev);
46f297fb 16531
d3fcc808 16532 for_each_intel_crtc(dev, crtc) {
eeebeac5
ML
16533 struct intel_initial_plane_config plane_config = {};
16534
46f297fb
JB
16535 if (!crtc->active)
16536 continue;
16537
46f297fb 16538 /*
46f297fb
JB
16539 * Note that reserving the BIOS fb up front prevents us
16540 * from stuffing other stolen allocations like the ring
16541 * on top. This prevents some ugliness at boot time, and
16542 * can even allow for smooth boot transitions if the BIOS
16543 * fb is large enough for the active pipe configuration.
16544 */
eeebeac5
ML
16545 dev_priv->display.get_initial_plane_config(crtc,
16546 &plane_config);
16547
16548 /*
16549 * If the fb is shared between multiple heads, we'll
16550 * just get the first one.
16551 */
16552 intel_find_initial_plane_obj(crtc, &plane_config);
46f297fb 16553 }
d93c0372
MR
16554
16555 /*
16556 * Make sure hardware watermarks really match the state we read out.
16557 * Note that we need to do this after reconstructing the BIOS fb's
16558 * since the watermark calculation done here will use pstate->fb.
16559 */
16560 sanitize_watermarks(dev);
b079bd17
VS
16561
16562 return 0;
2c7111db
CW
16563}
16564
7fad798e
SV
16565static void intel_enable_pipe_a(struct drm_device *dev)
16566{
16567 struct intel_connector *connector;
16568 struct drm_connector *crt = NULL;
16569 struct intel_load_detect_pipe load_detect_temp;
208bf9fd 16570 struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
7fad798e
SV
16571
16572 /* We can't just switch on the pipe A, we need to set things up with a
16573 * proper mode and output configuration. As a gross hack, enable pipe A
16574 * by enabling the load detect pipe once. */
3a3371ff 16575 for_each_intel_connector(dev, connector) {
7fad798e
SV
16576 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
16577 crt = &connector->base;
16578 break;
16579 }
16580 }
16581
16582 if (!crt)
16583 return;
16584
208bf9fd 16585 if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
49172fee 16586 intel_release_load_detect_pipe(crt, &load_detect_temp, ctx);
7fad798e
SV
16587}
16588
fa555837
SV
16589static bool
16590intel_check_plane_mapping(struct intel_crtc *crtc)
16591{
b7f05d4a 16592 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
649636ef 16593 u32 val;
fa555837 16594
b7f05d4a 16595 if (INTEL_INFO(dev_priv)->num_pipes == 1)
fa555837
SV
16596 return true;
16597
649636ef 16598 val = I915_READ(DSPCNTR(!crtc->plane));
fa555837
SV
16599
16600 if ((val & DISPLAY_PLANE_ENABLE) &&
16601 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
16602 return false;
16603
16604 return true;
16605}
16606
02e93c35
VS
16607static bool intel_crtc_has_encoders(struct intel_crtc *crtc)
16608{
16609 struct drm_device *dev = crtc->base.dev;
16610 struct intel_encoder *encoder;
16611
16612 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
16613 return true;
16614
16615 return false;
16616}
16617
496b0fc3
ML
16618static struct intel_connector *intel_encoder_find_connector(struct intel_encoder *encoder)
16619{
16620 struct drm_device *dev = encoder->base.dev;
16621 struct intel_connector *connector;
16622
16623 for_each_connector_on_encoder(dev, &encoder->base, connector)
16624 return connector;
16625
16626 return NULL;
16627}
16628
a168f5b3
VS
16629static bool has_pch_trancoder(struct drm_i915_private *dev_priv,
16630 enum transcoder pch_transcoder)
16631{
16632 return HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv) ||
16633 (HAS_PCH_LPT_H(dev_priv) && pch_transcoder == TRANSCODER_A);
16634}
16635
24929352
SV
16636static void intel_sanitize_crtc(struct intel_crtc *crtc)
16637{
16638 struct drm_device *dev = crtc->base.dev;
fac5e23e 16639 struct drm_i915_private *dev_priv = to_i915(dev);
4d1de975 16640 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
24929352 16641
24929352 16642 /* Clear any frame start delays used for debugging left by the BIOS */
4d1de975
JN
16643 if (!transcoder_is_dsi(cpu_transcoder)) {
16644 i915_reg_t reg = PIPECONF(cpu_transcoder);
16645
16646 I915_WRITE(reg,
16647 I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
16648 }
24929352 16649
d3eaf884 16650 /* restore vblank interrupts to correct state */
9625604c 16651 drm_crtc_vblank_reset(&crtc->base);
d297e103 16652 if (crtc->active) {
f9cd7b88
VS
16653 struct intel_plane *plane;
16654
9625604c 16655 drm_crtc_vblank_on(&crtc->base);
f9cd7b88
VS
16656
16657 /* Disable everything but the primary plane */
16658 for_each_intel_plane_on_crtc(dev, crtc, plane) {
16659 if (plane->base.type == DRM_PLANE_TYPE_PRIMARY)
16660 continue;
16661
16662 plane->disable_plane(&plane->base, &crtc->base);
16663 }
9625604c 16664 }
d3eaf884 16665
24929352 16666 /* We need to sanitize the plane -> pipe mapping first because this will
fa555837
SV
16667 * disable the crtc (and hence change the state) if it is wrong. Note
16668 * that gen4+ has a fixed plane -> pipe mapping. */
16669 if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
24929352
SV
16670 bool plane;
16671
78108b7c
VS
16672 DRM_DEBUG_KMS("[CRTC:%d:%s] wrong plane connection detected!\n",
16673 crtc->base.base.id, crtc->base.name);
24929352
SV
16674
16675 /* Pipe has the wrong plane attached and the plane is active.
16676 * Temporarily change the plane mapping and disable everything
16677 * ... */
16678 plane = crtc->plane;
936e71e3 16679 to_intel_plane_state(crtc->base.primary->state)->base.visible = true;
24929352 16680 crtc->plane = !plane;
b17d48e2 16681 intel_crtc_disable_noatomic(&crtc->base);
24929352 16682 crtc->plane = plane;
24929352 16683 }
24929352 16684
7fad798e
SV
16685 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
16686 crtc->pipe == PIPE_A && !crtc->active) {
16687 /* BIOS forgot to enable pipe A, this mostly happens after
16688 * resume. Force-enable the pipe to fix this, the update_dpms
16689 * call below we restore the pipe to the right state, but leave
16690 * the required bits on. */
16691 intel_enable_pipe_a(dev);
16692 }
16693
24929352
SV
16694 /* Adjust the state of the output pipe according to whether we
16695 * have active connectors/encoders. */
842e0307 16696 if (crtc->active && !intel_crtc_has_encoders(crtc))
b17d48e2 16697 intel_crtc_disable_noatomic(&crtc->base);
24929352 16698
49cff963 16699 if (crtc->active || HAS_GMCH_DISPLAY(dev_priv)) {
4cc31489
SV
16700 /*
16701 * We start out with underrun reporting disabled to avoid races.
16702 * For correct bookkeeping mark this on active crtcs.
16703 *
c5ab3bc0
SV
16704 * Also on gmch platforms we dont have any hardware bits to
16705 * disable the underrun reporting. Which means we need to start
16706 * out with underrun reporting disabled also on inactive pipes,
16707 * since otherwise we'll complain about the garbage we read when
16708 * e.g. coming up after runtime pm.
16709 *
4cc31489
SV
16710 * No protection against concurrent access is required - at
16711 * worst a fifo underrun happens which also sets this to false.
16712 */
16713 crtc->cpu_fifo_underrun_disabled = true;
a168f5b3
VS
16714 /*
16715 * We track the PCH trancoder underrun reporting state
16716 * within the crtc. With crtc for pipe A housing the underrun
16717 * reporting state for PCH transcoder A, crtc for pipe B housing
16718 * it for PCH transcoder B, etc. LPT-H has only PCH transcoder A,
16719 * and marking underrun reporting as disabled for the non-existing
16720 * PCH transcoders B and C would prevent enabling the south
16721 * error interrupt (see cpt_can_enable_serr_int()).
16722 */
16723 if (has_pch_trancoder(dev_priv, (enum transcoder)crtc->pipe))
16724 crtc->pch_fifo_underrun_disabled = true;
4cc31489 16725 }
24929352
SV
16726}
16727
16728static void intel_sanitize_encoder(struct intel_encoder *encoder)
16729{
16730 struct intel_connector *connector;
24929352
SV
16731
16732 /* We need to check both for a crtc link (meaning that the
16733 * encoder is active and trying to read from a pipe) and the
16734 * pipe itself being active. */
16735 bool has_active_crtc = encoder->base.crtc &&
16736 to_intel_crtc(encoder->base.crtc)->active;
16737
496b0fc3
ML
16738 connector = intel_encoder_find_connector(encoder);
16739 if (connector && !has_active_crtc) {
24929352
SV
16740 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
16741 encoder->base.base.id,
8e329a03 16742 encoder->base.name);
24929352
SV
16743
16744 /* Connector is active, but has no active pipe. This is
16745 * fallout from our resume register restoring. Disable
16746 * the encoder manually again. */
16747 if (encoder->base.crtc) {
fd6bbda9
ML
16748 struct drm_crtc_state *crtc_state = encoder->base.crtc->state;
16749
24929352
SV
16750 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
16751 encoder->base.base.id,
8e329a03 16752 encoder->base.name);
fd6bbda9 16753 encoder->disable(encoder, to_intel_crtc_state(crtc_state), connector->base.state);
a62d1497 16754 if (encoder->post_disable)
fd6bbda9 16755 encoder->post_disable(encoder, to_intel_crtc_state(crtc_state), connector->base.state);
24929352 16756 }
7f1950fb 16757 encoder->base.crtc = NULL;
24929352
SV
16758
16759 /* Inconsistent output/port/pipe state happens presumably due to
16760 * a bug in one of the get_hw_state functions. Or someplace else
16761 * in our code, like the register restore mess on resume. Clamp
16762 * things to off as a safer default. */
fd6bbda9
ML
16763
16764 connector->base.dpms = DRM_MODE_DPMS_OFF;
16765 connector->base.encoder = NULL;
24929352
SV
16766 }
16767 /* Enabled encoders without active connectors will be fixed in
16768 * the crtc fixup. */
16769}
16770
04098753 16771void i915_redisable_vga_power_on(struct drm_device *dev)
0fde901f 16772{
fac5e23e 16773 struct drm_i915_private *dev_priv = to_i915(dev);
920a14b2 16774 i915_reg_t vga_reg = i915_vgacntrl_reg(dev_priv);
0fde901f 16775
04098753
ID
16776 if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
16777 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
16778 i915_disable_vga(dev);
16779 }
16780}
16781
16782void i915_redisable_vga(struct drm_device *dev)
16783{
fac5e23e 16784 struct drm_i915_private *dev_priv = to_i915(dev);
04098753 16785
8dc8a27c
PZ
16786 /* This function can be called both from intel_modeset_setup_hw_state or
16787 * at a very early point in our resume sequence, where the power well
16788 * structures are not yet restored. Since this function is at a very
16789 * paranoid "someone might have enabled VGA while we were not looking"
16790 * level, just check if the power well is enabled instead of trying to
16791 * follow the "don't touch the power well if we don't need it" policy
16792 * the rest of the driver uses. */
6392f847 16793 if (!intel_display_power_get_if_enabled(dev_priv, POWER_DOMAIN_VGA))
8dc8a27c
PZ
16794 return;
16795
04098753 16796 i915_redisable_vga_power_on(dev);
6392f847
ID
16797
16798 intel_display_power_put(dev_priv, POWER_DOMAIN_VGA);
0fde901f
KM
16799}
16800
f9cd7b88 16801static bool primary_get_hw_state(struct intel_plane *plane)
98ec7739 16802{
f9cd7b88 16803 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
98ec7739 16804
f9cd7b88 16805 return I915_READ(DSPCNTR(plane->plane)) & DISPLAY_PLANE_ENABLE;
d032ffa0
ML
16806}
16807
f9cd7b88
VS
16808/* FIXME read out full plane state for all planes */
16809static void readout_plane_state(struct intel_crtc *crtc)
d032ffa0 16810{
b26d3ea3 16811 struct drm_plane *primary = crtc->base.primary;
f9cd7b88 16812 struct intel_plane_state *plane_state =
b26d3ea3 16813 to_intel_plane_state(primary->state);
d032ffa0 16814
936e71e3 16815 plane_state->base.visible = crtc->active &&
b26d3ea3
ML
16816 primary_get_hw_state(to_intel_plane(primary));
16817
936e71e3 16818 if (plane_state->base.visible)
b26d3ea3 16819 crtc->base.state->plane_mask |= 1 << drm_plane_index(primary);
98ec7739
VS
16820}
16821
30e984df 16822static void intel_modeset_readout_hw_state(struct drm_device *dev)
24929352 16823{
fac5e23e 16824 struct drm_i915_private *dev_priv = to_i915(dev);
24929352 16825 enum pipe pipe;
24929352
SV
16826 struct intel_crtc *crtc;
16827 struct intel_encoder *encoder;
16828 struct intel_connector *connector;
5358901f 16829 int i;
24929352 16830
565602d7
ML
16831 dev_priv->active_crtcs = 0;
16832
d3fcc808 16833 for_each_intel_crtc(dev, crtc) {
565602d7
ML
16834 struct intel_crtc_state *crtc_state = crtc->config;
16835 int pixclk = 0;
3b117c8f 16836
ec2dc6a0 16837 __drm_atomic_helper_crtc_destroy_state(&crtc_state->base);
565602d7
ML
16838 memset(crtc_state, 0, sizeof(*crtc_state));
16839 crtc_state->base.crtc = &crtc->base;
24929352 16840
565602d7
ML
16841 crtc_state->base.active = crtc_state->base.enable =
16842 dev_priv->display.get_pipe_config(crtc, crtc_state);
16843
16844 crtc->base.enabled = crtc_state->base.enable;
16845 crtc->active = crtc_state->base.active;
16846
16847 if (crtc_state->base.active) {
16848 dev_priv->active_crtcs |= 1 << crtc->pipe;
16849
c89e39f3 16850 if (INTEL_GEN(dev_priv) >= 9 || IS_BROADWELL(dev_priv))
565602d7 16851 pixclk = ilk_pipe_pixel_rate(crtc_state);
9558d15d 16852 else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
565602d7
ML
16853 pixclk = crtc_state->base.adjusted_mode.crtc_clock;
16854 else
16855 WARN_ON(dev_priv->display.modeset_calc_cdclk);
9558d15d
VS
16856
16857 /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
16858 if (IS_BROADWELL(dev_priv) && crtc_state->ips_enabled)
16859 pixclk = DIV_ROUND_UP(pixclk * 100, 95);
565602d7
ML
16860 }
16861
16862 dev_priv->min_pixclk[crtc->pipe] = pixclk;
b70709a6 16863
f9cd7b88 16864 readout_plane_state(crtc);
24929352 16865
78108b7c
VS
16866 DRM_DEBUG_KMS("[CRTC:%d:%s] hw state readout: %s\n",
16867 crtc->base.base.id, crtc->base.name,
24929352
SV
16868 crtc->active ? "enabled" : "disabled");
16869 }
16870
5358901f
SV
16871 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
16872 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
16873
2edd6443
ACO
16874 pll->on = pll->funcs.get_hw_state(dev_priv, pll,
16875 &pll->config.hw_state);
3e369b76 16876 pll->config.crtc_mask = 0;
d3fcc808 16877 for_each_intel_crtc(dev, crtc) {
2dd66ebd 16878 if (crtc->active && crtc->config->shared_dpll == pll)
3e369b76 16879 pll->config.crtc_mask |= 1 << crtc->pipe;
5358901f 16880 }
2dd66ebd 16881 pll->active_mask = pll->config.crtc_mask;
5358901f 16882
1e6f2ddc 16883 DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
3e369b76 16884 pll->name, pll->config.crtc_mask, pll->on);
5358901f
SV
16885 }
16886
b2784e15 16887 for_each_intel_encoder(dev, encoder) {
24929352
SV
16888 pipe = 0;
16889
16890 if (encoder->get_hw_state(encoder, &pipe)) {
98187836 16891 crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
e2af48c6 16892
045ac3b5 16893 encoder->base.crtc = &crtc->base;
253c84c8 16894 crtc->config->output_types |= 1 << encoder->type;
6e3c9717 16895 encoder->get_config(encoder, crtc->config);
24929352
SV
16896 } else {
16897 encoder->base.crtc = NULL;
16898 }
16899
6f2bcceb 16900 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
24929352 16901 encoder->base.base.id,
8e329a03 16902 encoder->base.name,
24929352 16903 encoder->base.crtc ? "enabled" : "disabled",
6f2bcceb 16904 pipe_name(pipe));
24929352
SV
16905 }
16906
3a3371ff 16907 for_each_intel_connector(dev, connector) {
24929352
SV
16908 if (connector->get_hw_state(connector)) {
16909 connector->base.dpms = DRM_MODE_DPMS_ON;
2aa974c9
ML
16910
16911 encoder = connector->encoder;
16912 connector->base.encoder = &encoder->base;
16913
16914 if (encoder->base.crtc &&
16915 encoder->base.crtc->state->active) {
16916 /*
16917 * This has to be done during hardware readout
16918 * because anything calling .crtc_disable may
16919 * rely on the connector_mask being accurate.
16920 */
16921 encoder->base.crtc->state->connector_mask |=
16922 1 << drm_connector_index(&connector->base);
e87a52b3
ML
16923 encoder->base.crtc->state->encoder_mask |=
16924 1 << drm_encoder_index(&encoder->base);
2aa974c9
ML
16925 }
16926
24929352
SV
16927 } else {
16928 connector->base.dpms = DRM_MODE_DPMS_OFF;
16929 connector->base.encoder = NULL;
16930 }
16931 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
16932 connector->base.base.id,
c23cc417 16933 connector->base.name,
24929352
SV
16934 connector->base.encoder ? "enabled" : "disabled");
16935 }
7f4c6284
VS
16936
16937 for_each_intel_crtc(dev, crtc) {
16938 crtc->base.hwmode = crtc->config->base.adjusted_mode;
16939
16940 memset(&crtc->base.mode, 0, sizeof(crtc->base.mode));
16941 if (crtc->base.state->active) {
16942 intel_mode_from_pipe_config(&crtc->base.mode, crtc->config);
16943 intel_mode_from_pipe_config(&crtc->base.state->adjusted_mode, crtc->config);
16944 WARN_ON(drm_atomic_set_mode_for_crtc(crtc->base.state, &crtc->base.mode));
16945
16946 /*
16947 * The initial mode needs to be set in order to keep
16948 * the atomic core happy. It wants a valid mode if the
16949 * crtc's enabled, so we do the above call.
16950 *
16951 * At this point some state updated by the connectors
16952 * in their ->detect() callback has not run yet, so
16953 * no recalculation can be done yet.
16954 *
16955 * Even if we could do a recalculation and modeset
16956 * right now it would cause a double modeset if
16957 * fbdev or userspace chooses a different initial mode.
16958 *
16959 * If that happens, someone indicated they wanted a
16960 * mode change, which means it's safe to do a full
16961 * recalculation.
16962 */
16963 crtc->base.state->mode.private_flags = I915_MODE_FLAG_INHERITED;
9eca6832
VS
16964
16965 drm_calc_timestamping_constants(&crtc->base, &crtc->base.hwmode);
16966 update_scanline_offset(crtc);
7f4c6284 16967 }
e3b247da
VS
16968
16969 intel_pipe_config_sanity_check(dev_priv, crtc->config);
7f4c6284 16970 }
30e984df
SV
16971}
16972
043e9bda
ML
16973/* Scan out the current hw modeset state,
16974 * and sanitizes it to the current state
16975 */
16976static void
16977intel_modeset_setup_hw_state(struct drm_device *dev)
30e984df 16978{
fac5e23e 16979 struct drm_i915_private *dev_priv = to_i915(dev);
30e984df 16980 enum pipe pipe;
30e984df
SV
16981 struct intel_crtc *crtc;
16982 struct intel_encoder *encoder;
35c95375 16983 int i;
30e984df
SV
16984
16985 intel_modeset_readout_hw_state(dev);
24929352
SV
16986
16987 /* HW state is read out, now we need to sanitize this mess. */
b2784e15 16988 for_each_intel_encoder(dev, encoder) {
24929352
SV
16989 intel_sanitize_encoder(encoder);
16990 }
16991
055e393f 16992 for_each_pipe(dev_priv, pipe) {
98187836 16993 crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
e2af48c6 16994
24929352 16995 intel_sanitize_crtc(crtc);
6e3c9717
ACO
16996 intel_dump_pipe_config(crtc, crtc->config,
16997 "[setup_hw_state]");
24929352 16998 }
9a935856 16999
d29b2f9d
ACO
17000 intel_modeset_update_connector_atomic_state(dev);
17001
35c95375
SV
17002 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
17003 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
17004
2dd66ebd 17005 if (!pll->on || pll->active_mask)
35c95375
SV
17006 continue;
17007
17008 DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
17009
2edd6443 17010 pll->funcs.disable(dev_priv, pll);
35c95375
SV
17011 pll->on = false;
17012 }
17013
920a14b2 17014 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
6eb1a681 17015 vlv_wm_get_hw_state(dev);
5db94019 17016 else if (IS_GEN9(dev_priv))
3078999f 17017 skl_wm_get_hw_state(dev);
6e266956 17018 else if (HAS_PCH_SPLIT(dev_priv))
243e6a44 17019 ilk_wm_get_hw_state(dev);
292b990e
ML
17020
17021 for_each_intel_crtc(dev, crtc) {
17022 unsigned long put_domains;
17023
74bff5f9 17024 put_domains = modeset_get_crtc_power_domains(&crtc->base, crtc->config);
292b990e
ML
17025 if (WARN_ON(put_domains))
17026 modeset_put_power_domains(dev_priv, put_domains);
17027 }
17028 intel_display_set_init_power(dev_priv, false);
010cf73d
PZ
17029
17030 intel_fbc_init_pipe_state(dev_priv);
043e9bda 17031}
7d0bc1ea 17032
043e9bda
ML
17033void intel_display_resume(struct drm_device *dev)
17034{
e2c8b870
ML
17035 struct drm_i915_private *dev_priv = to_i915(dev);
17036 struct drm_atomic_state *state = dev_priv->modeset_restore_state;
17037 struct drm_modeset_acquire_ctx ctx;
043e9bda 17038 int ret;
f30da187 17039
e2c8b870 17040 dev_priv->modeset_restore_state = NULL;
73974893
ML
17041 if (state)
17042 state->acquire_ctx = &ctx;
043e9bda 17043
ea49c9ac
ML
17044 /*
17045 * This is a cludge because with real atomic modeset mode_config.mutex
17046 * won't be taken. Unfortunately some probed state like
17047 * audio_codec_enable is still protected by mode_config.mutex, so lock
17048 * it here for now.
17049 */
17050 mutex_lock(&dev->mode_config.mutex);
e2c8b870 17051 drm_modeset_acquire_init(&ctx, 0);
043e9bda 17052
73974893
ML
17053 while (1) {
17054 ret = drm_modeset_lock_all_ctx(dev, &ctx);
17055 if (ret != -EDEADLK)
17056 break;
043e9bda 17057
e2c8b870 17058 drm_modeset_backoff(&ctx);
e2c8b870 17059 }
043e9bda 17060
73974893
ML
17061 if (!ret)
17062 ret = __intel_display_resume(dev, state);
17063
e2c8b870
ML
17064 drm_modeset_drop_locks(&ctx);
17065 drm_modeset_acquire_fini(&ctx);
ea49c9ac 17066 mutex_unlock(&dev->mode_config.mutex);
043e9bda 17067
0853695c 17068 if (ret)
e2c8b870 17069 DRM_ERROR("Restoring old state failed with %i\n", ret);
0853695c 17070 drm_atomic_state_put(state);
2c7111db
CW
17071}
17072
17073void intel_modeset_gem_init(struct drm_device *dev)
17074{
dc97997a 17075 struct drm_i915_private *dev_priv = to_i915(dev);
484b41dd 17076 struct drm_crtc *c;
2ff8fde1 17077 struct drm_i915_gem_object *obj;
484b41dd 17078
dc97997a 17079 intel_init_gt_powersave(dev_priv);
ae48434c 17080
1833b134 17081 intel_modeset_init_hw(dev);
02e792fb 17082
1ee8da6d 17083 intel_setup_overlay(dev_priv);
484b41dd
JB
17084
17085 /*
17086 * Make sure any fbs we allocated at startup are properly
17087 * pinned & fenced. When we do the allocation it's too early
17088 * for this.
17089 */
70e1e0ec 17090 for_each_crtc(dev, c) {
058d88c4
CW
17091 struct i915_vma *vma;
17092
2ff8fde1
MR
17093 obj = intel_fb_obj(c->primary->fb);
17094 if (obj == NULL)
484b41dd
JB
17095 continue;
17096
e0d6149b 17097 mutex_lock(&dev->struct_mutex);
058d88c4 17098 vma = intel_pin_and_fence_fb_obj(c->primary->fb,
3465c580 17099 c->primary->state->rotation);
e0d6149b 17100 mutex_unlock(&dev->struct_mutex);
058d88c4 17101 if (IS_ERR(vma)) {
484b41dd
JB
17102 DRM_ERROR("failed to pin boot fb on pipe %d\n",
17103 to_intel_crtc(c)->pipe);
66e514c1 17104 drm_framebuffer_unreference(c->primary->fb);
5a21b665 17105 c->primary->fb = NULL;
36750f28 17106 c->primary->crtc = c->primary->state->crtc = NULL;
5a21b665 17107 update_state_fb(c->primary);
36750f28 17108 c->state->plane_mask &= ~(1 << drm_plane_index(c->primary));
484b41dd
JB
17109 }
17110 }
1ebaa0b9
CW
17111}
17112
17113int intel_connector_register(struct drm_connector *connector)
17114{
17115 struct intel_connector *intel_connector = to_intel_connector(connector);
17116 int ret;
17117
17118 ret = intel_backlight_device_register(intel_connector);
17119 if (ret)
17120 goto err;
17121
17122 return 0;
0962c3c9 17123
1ebaa0b9
CW
17124err:
17125 return ret;
79e53945
JB
17126}
17127
c191eca1 17128void intel_connector_unregister(struct drm_connector *connector)
4932e2c3 17129{
e63d87c0 17130 struct intel_connector *intel_connector = to_intel_connector(connector);
4932e2c3 17131
e63d87c0 17132 intel_backlight_device_unregister(intel_connector);
4932e2c3 17133 intel_panel_destroy_backlight(connector);
4932e2c3
ID
17134}
17135
79e53945
JB
17136void intel_modeset_cleanup(struct drm_device *dev)
17137{
fac5e23e 17138 struct drm_i915_private *dev_priv = to_i915(dev);
652c393a 17139
dc97997a 17140 intel_disable_gt_powersave(dev_priv);
2eb5252e 17141
fd0c0642
SV
17142 /*
17143 * Interrupts and polling as the first thing to avoid creating havoc.
2eb5252e 17144 * Too much stuff here (turning of connectors, ...) would
fd0c0642
SV
17145 * experience fancy races otherwise.
17146 */
2aeb7d3a 17147 intel_irq_uninstall(dev_priv);
eb21b92b 17148
fd0c0642
SV
17149 /*
17150 * Due to the hpd irq storm handling the hotplug work can re-arm the
17151 * poll handlers. Hence disable polling after hpd handling is shut down.
17152 */
f87ea761 17153 drm_kms_helper_poll_fini(dev);
fd0c0642 17154
723bfd70
JB
17155 intel_unregister_dsm_handler();
17156
c937ab3e 17157 intel_fbc_global_disable(dev_priv);
69341a5e 17158
1630fe75
CW
17159 /* flush any delayed tasks or pending work */
17160 flush_scheduled_work();
17161
79e53945 17162 drm_mode_config_cleanup(dev);
4d7bb011 17163
1ee8da6d 17164 intel_cleanup_overlay(dev_priv);
ae48434c 17165
dc97997a 17166 intel_cleanup_gt_powersave(dev_priv);
f5949141
SV
17167
17168 intel_teardown_gmbus(dev);
79e53945
JB
17169}
17170
df0e9248
CW
17171void intel_connector_attach_encoder(struct intel_connector *connector,
17172 struct intel_encoder *encoder)
17173{
17174 connector->encoder = encoder;
17175 drm_mode_connector_attach_encoder(&connector->base,
17176 &encoder->base);
79e53945 17177}
28d52043
DA
17178
17179/*
17180 * set vga decode state - true == enable VGA decode
17181 */
17182int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
17183{
fac5e23e 17184 struct drm_i915_private *dev_priv = to_i915(dev);
a885b3cc 17185 unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
28d52043
DA
17186 u16 gmch_ctrl;
17187
75fa041d
CW
17188 if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
17189 DRM_ERROR("failed to read control word\n");
17190 return -EIO;
17191 }
17192
c0cc8a55
CW
17193 if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
17194 return 0;
17195
28d52043
DA
17196 if (state)
17197 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
17198 else
17199 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
75fa041d
CW
17200
17201 if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
17202 DRM_ERROR("failed to write control word\n");
17203 return -EIO;
17204 }
17205
28d52043
DA
17206 return 0;
17207}
c4a1d9e4 17208
98a2f411
CW
17209#if IS_ENABLED(CONFIG_DRM_I915_CAPTURE_ERROR)
17210
c4a1d9e4 17211struct intel_display_error_state {
ff57f1b0
PZ
17212
17213 u32 power_well_driver;
17214
63b66e5b
CW
17215 int num_transcoders;
17216
c4a1d9e4
CW
17217 struct intel_cursor_error_state {
17218 u32 control;
17219 u32 position;
17220 u32 base;
17221 u32 size;
52331309 17222 } cursor[I915_MAX_PIPES];
c4a1d9e4
CW
17223
17224 struct intel_pipe_error_state {
ddf9c536 17225 bool power_domain_on;
c4a1d9e4 17226 u32 source;
f301b1e1 17227 u32 stat;
52331309 17228 } pipe[I915_MAX_PIPES];
c4a1d9e4
CW
17229
17230 struct intel_plane_error_state {
17231 u32 control;
17232 u32 stride;
17233 u32 size;
17234 u32 pos;
17235 u32 addr;
17236 u32 surface;
17237 u32 tile_offset;
52331309 17238 } plane[I915_MAX_PIPES];
63b66e5b
CW
17239
17240 struct intel_transcoder_error_state {
ddf9c536 17241 bool power_domain_on;
63b66e5b
CW
17242 enum transcoder cpu_transcoder;
17243
17244 u32 conf;
17245
17246 u32 htotal;
17247 u32 hblank;
17248 u32 hsync;
17249 u32 vtotal;
17250 u32 vblank;
17251 u32 vsync;
17252 } transcoder[4];
c4a1d9e4
CW
17253};
17254
17255struct intel_display_error_state *
c033666a 17256intel_display_capture_error_state(struct drm_i915_private *dev_priv)
c4a1d9e4 17257{
c4a1d9e4 17258 struct intel_display_error_state *error;
63b66e5b
CW
17259 int transcoders[] = {
17260 TRANSCODER_A,
17261 TRANSCODER_B,
17262 TRANSCODER_C,
17263 TRANSCODER_EDP,
17264 };
c4a1d9e4
CW
17265 int i;
17266
c033666a 17267 if (INTEL_INFO(dev_priv)->num_pipes == 0)
63b66e5b
CW
17268 return NULL;
17269
9d1cb914 17270 error = kzalloc(sizeof(*error), GFP_ATOMIC);
c4a1d9e4
CW
17271 if (error == NULL)
17272 return NULL;
17273
c033666a 17274 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
ff57f1b0
PZ
17275 error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
17276
055e393f 17277 for_each_pipe(dev_priv, i) {
ddf9c536 17278 error->pipe[i].power_domain_on =
f458ebbc
SV
17279 __intel_display_power_is_enabled(dev_priv,
17280 POWER_DOMAIN_PIPE(i));
ddf9c536 17281 if (!error->pipe[i].power_domain_on)
9d1cb914
PZ
17282 continue;
17283
5efb3e28
VS
17284 error->cursor[i].control = I915_READ(CURCNTR(i));
17285 error->cursor[i].position = I915_READ(CURPOS(i));
17286 error->cursor[i].base = I915_READ(CURBASE(i));
c4a1d9e4
CW
17287
17288 error->plane[i].control = I915_READ(DSPCNTR(i));
17289 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
c033666a 17290 if (INTEL_GEN(dev_priv) <= 3) {
51889b35 17291 error->plane[i].size = I915_READ(DSPSIZE(i));
80ca378b
PZ
17292 error->plane[i].pos = I915_READ(DSPPOS(i));
17293 }
c033666a 17294 if (INTEL_GEN(dev_priv) <= 7 && !IS_HASWELL(dev_priv))
ca291363 17295 error->plane[i].addr = I915_READ(DSPADDR(i));
c033666a 17296 if (INTEL_GEN(dev_priv) >= 4) {
c4a1d9e4
CW
17297 error->plane[i].surface = I915_READ(DSPSURF(i));
17298 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
17299 }
17300
c4a1d9e4 17301 error->pipe[i].source = I915_READ(PIPESRC(i));
f301b1e1 17302
c033666a 17303 if (HAS_GMCH_DISPLAY(dev_priv))
f301b1e1 17304 error->pipe[i].stat = I915_READ(PIPESTAT(i));
63b66e5b
CW
17305 }
17306
4d1de975 17307 /* Note: this does not include DSI transcoders. */
c033666a 17308 error->num_transcoders = INTEL_INFO(dev_priv)->num_pipes;
2d1fe073 17309 if (HAS_DDI(dev_priv))
63b66e5b
CW
17310 error->num_transcoders++; /* Account for eDP. */
17311
17312 for (i = 0; i < error->num_transcoders; i++) {
17313 enum transcoder cpu_transcoder = transcoders[i];
17314
ddf9c536 17315 error->transcoder[i].power_domain_on =
f458ebbc 17316 __intel_display_power_is_enabled(dev_priv,
38cc1daf 17317 POWER_DOMAIN_TRANSCODER(cpu_transcoder));
ddf9c536 17318 if (!error->transcoder[i].power_domain_on)
9d1cb914
PZ
17319 continue;
17320
63b66e5b
CW
17321 error->transcoder[i].cpu_transcoder = cpu_transcoder;
17322
17323 error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
17324 error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
17325 error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
17326 error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
17327 error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
17328 error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
17329 error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
c4a1d9e4
CW
17330 }
17331
17332 return error;
17333}
17334
edc3d884
MK
17335#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
17336
c4a1d9e4 17337void
edc3d884 17338intel_display_print_error_state(struct drm_i915_error_state_buf *m,
c4a1d9e4
CW
17339 struct drm_device *dev,
17340 struct intel_display_error_state *error)
17341{
fac5e23e 17342 struct drm_i915_private *dev_priv = to_i915(dev);
c4a1d9e4
CW
17343 int i;
17344
63b66e5b
CW
17345 if (!error)
17346 return;
17347
b7f05d4a 17348 err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev_priv)->num_pipes);
8652744b 17349 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
edc3d884 17350 err_printf(m, "PWR_WELL_CTL2: %08x\n",
ff57f1b0 17351 error->power_well_driver);
055e393f 17352 for_each_pipe(dev_priv, i) {
edc3d884 17353 err_printf(m, "Pipe [%d]:\n", i);
ddf9c536 17354 err_printf(m, " Power: %s\n",
87ad3212 17355 onoff(error->pipe[i].power_domain_on));
edc3d884 17356 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
f301b1e1 17357 err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
edc3d884
MK
17358
17359 err_printf(m, "Plane [%d]:\n", i);
17360 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
17361 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
80ca378b 17362 if (INTEL_INFO(dev)->gen <= 3) {
edc3d884
MK
17363 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
17364 err_printf(m, " POS: %08x\n", error->plane[i].pos);
80ca378b 17365 }
772c2a51 17366 if (INTEL_GEN(dev_priv) <= 7 && !IS_HASWELL(dev_priv))
edc3d884 17367 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
c4a1d9e4 17368 if (INTEL_INFO(dev)->gen >= 4) {
edc3d884
MK
17369 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
17370 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
c4a1d9e4
CW
17371 }
17372
edc3d884
MK
17373 err_printf(m, "Cursor [%d]:\n", i);
17374 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
17375 err_printf(m, " POS: %08x\n", error->cursor[i].position);
17376 err_printf(m, " BASE: %08x\n", error->cursor[i].base);
c4a1d9e4 17377 }
63b66e5b
CW
17378
17379 for (i = 0; i < error->num_transcoders; i++) {
da205630 17380 err_printf(m, "CPU transcoder: %s\n",
63b66e5b 17381 transcoder_name(error->transcoder[i].cpu_transcoder));
ddf9c536 17382 err_printf(m, " Power: %s\n",
87ad3212 17383 onoff(error->transcoder[i].power_domain_on));
63b66e5b
CW
17384 err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
17385 err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
17386 err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
17387 err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
17388 err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
17389 err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
17390 err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
17391 }
c4a1d9e4 17392}
98a2f411
CW
17393
17394#endif
This page took 7.879323 seconds and 4 git commands to generate.