]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/gpu/drm/i915/intel_display.c
drm/i915: Do not overwrite the request with zero on reallocation
[mirror_ubuntu-bionic-kernel.git] / drivers / gpu / drm / i915 / intel_display.c
CommitLineData
79e53945
JB
1/*
2 * Copyright © 2006-2007 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 *
23 * Authors:
24 * Eric Anholt <eric@anholt.net>
25 */
26
618563e3 27#include <linux/dmi.h>
c1c7af60
JB
28#include <linux/module.h>
29#include <linux/input.h>
79e53945 30#include <linux/i2c.h>
7662c8bd 31#include <linux/kernel.h>
5a0e3ad6 32#include <linux/slab.h>
9cce37f4 33#include <linux/vgaarb.h>
e0dac65e 34#include <drm/drm_edid.h>
760285e7 35#include <drm/drmP.h>
79e53945 36#include "intel_drv.h"
5d723d7a 37#include "intel_frontbuffer.h"
760285e7 38#include <drm/i915_drm.h>
79e53945 39#include "i915_drv.h"
c37efb99 40#include "i915_gem_dmabuf.h"
db18b6a6 41#include "intel_dsi.h"
e5510fac 42#include "i915_trace.h"
319c1d42 43#include <drm/drm_atomic.h>
c196e1d6 44#include <drm/drm_atomic_helper.h>
760285e7
DH
45#include <drm/drm_dp_helper.h>
46#include <drm/drm_crtc_helper.h>
465c120c
MR
47#include <drm/drm_plane_helper.h>
48#include <drm/drm_rect.h>
c0f372b3 49#include <linux/dma_remapping.h>
fd8e058a 50#include <linux/reservation.h>
79e53945 51
5a21b665
DV
52static bool is_mmio_work(struct intel_flip_work *work)
53{
54 return work->mmio_work.func;
55}
56
465c120c 57/* Primary plane formats for gen <= 3 */
568db4f2 58static const uint32_t i8xx_primary_formats[] = {
67fe7dc5
DL
59 DRM_FORMAT_C8,
60 DRM_FORMAT_RGB565,
465c120c 61 DRM_FORMAT_XRGB1555,
67fe7dc5 62 DRM_FORMAT_XRGB8888,
465c120c
MR
63};
64
65/* Primary plane formats for gen >= 4 */
568db4f2 66static const uint32_t i965_primary_formats[] = {
6c0fd451
DL
67 DRM_FORMAT_C8,
68 DRM_FORMAT_RGB565,
69 DRM_FORMAT_XRGB8888,
70 DRM_FORMAT_XBGR8888,
71 DRM_FORMAT_XRGB2101010,
72 DRM_FORMAT_XBGR2101010,
73};
74
75static const uint32_t skl_primary_formats[] = {
67fe7dc5
DL
76 DRM_FORMAT_C8,
77 DRM_FORMAT_RGB565,
78 DRM_FORMAT_XRGB8888,
465c120c 79 DRM_FORMAT_XBGR8888,
67fe7dc5 80 DRM_FORMAT_ARGB8888,
465c120c
MR
81 DRM_FORMAT_ABGR8888,
82 DRM_FORMAT_XRGB2101010,
465c120c 83 DRM_FORMAT_XBGR2101010,
ea916ea0
KM
84 DRM_FORMAT_YUYV,
85 DRM_FORMAT_YVYU,
86 DRM_FORMAT_UYVY,
87 DRM_FORMAT_VYUY,
465c120c
MR
88};
89
3d7d6510
MR
90/* Cursor formats */
91static const uint32_t intel_cursor_formats[] = {
92 DRM_FORMAT_ARGB8888,
93};
94
f1f644dc 95static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 96 struct intel_crtc_state *pipe_config);
18442d08 97static void ironlake_pch_clock_get(struct intel_crtc *crtc,
5cec258b 98 struct intel_crtc_state *pipe_config);
f1f644dc 99
eb1bfe80
JB
100static int intel_framebuffer_init(struct drm_device *dev,
101 struct intel_framebuffer *ifb,
102 struct drm_mode_fb_cmd2 *mode_cmd,
103 struct drm_i915_gem_object *obj);
5b18e57c
DV
104static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc);
105static void intel_set_pipe_timings(struct intel_crtc *intel_crtc);
bc58be60 106static void intel_set_pipe_src_size(struct intel_crtc *intel_crtc);
29407aab 107static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
f769cd24
VK
108 struct intel_link_m_n *m_n,
109 struct intel_link_m_n *m2_n2);
29407aab 110static void ironlake_set_pipeconf(struct drm_crtc *crtc);
229fca97 111static void haswell_set_pipeconf(struct drm_crtc *crtc);
391bf048 112static void haswell_set_pipemisc(struct drm_crtc *crtc);
d288f65f 113static void vlv_prepare_pll(struct intel_crtc *crtc,
5cec258b 114 const struct intel_crtc_state *pipe_config);
d288f65f 115static void chv_prepare_pll(struct intel_crtc *crtc,
5cec258b 116 const struct intel_crtc_state *pipe_config);
5a21b665
DV
117static void intel_begin_crtc_commit(struct drm_crtc *, struct drm_crtc_state *);
118static void intel_finish_crtc_commit(struct drm_crtc *, struct drm_crtc_state *);
549e2bfb
CK
119static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
120 struct intel_crtc_state *crtc_state);
bfd16b2a
ML
121static void skylake_pfit_enable(struct intel_crtc *crtc);
122static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force);
123static void ironlake_pfit_enable(struct intel_crtc *crtc);
043e9bda 124static void intel_modeset_setup_hw_state(struct drm_device *dev);
2622a081 125static void intel_pre_disable_primary_noatomic(struct drm_crtc *crtc);
4e5ca60f 126static int ilk_max_pixel_rate(struct drm_atomic_state *state);
324513c0 127static int bxt_calc_cdclk(int max_pixclk);
e7457a9a 128
d4906093 129struct intel_limit {
4c5def93
ACO
130 struct {
131 int min, max;
132 } dot, vco, n, m, m1, m2, p, p1;
133
134 struct {
135 int dot_limit;
136 int p2_slow, p2_fast;
137 } p2;
d4906093 138};
79e53945 139
bfa7df01
VS
140/* returns HPLL frequency in kHz */
141static int valleyview_get_vco(struct drm_i915_private *dev_priv)
142{
143 int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
144
145 /* Obtain SKU information */
146 mutex_lock(&dev_priv->sb_lock);
147 hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
148 CCK_FUSE_HPLL_FREQ_MASK;
149 mutex_unlock(&dev_priv->sb_lock);
150
151 return vco_freq[hpll_freq] * 1000;
152}
153
c30fec65
VS
154int vlv_get_cck_clock(struct drm_i915_private *dev_priv,
155 const char *name, u32 reg, int ref_freq)
bfa7df01
VS
156{
157 u32 val;
158 int divider;
159
bfa7df01
VS
160 mutex_lock(&dev_priv->sb_lock);
161 val = vlv_cck_read(dev_priv, reg);
162 mutex_unlock(&dev_priv->sb_lock);
163
164 divider = val & CCK_FREQUENCY_VALUES;
165
166 WARN((val & CCK_FREQUENCY_STATUS) !=
167 (divider << CCK_FREQUENCY_STATUS_SHIFT),
168 "%s change in progress\n", name);
169
c30fec65
VS
170 return DIV_ROUND_CLOSEST(ref_freq << 1, divider + 1);
171}
172
173static int vlv_get_cck_clock_hpll(struct drm_i915_private *dev_priv,
174 const char *name, u32 reg)
175{
176 if (dev_priv->hpll_freq == 0)
177 dev_priv->hpll_freq = valleyview_get_vco(dev_priv);
178
179 return vlv_get_cck_clock(dev_priv, name, reg,
180 dev_priv->hpll_freq);
bfa7df01
VS
181}
182
e7dc33f3
VS
183static int
184intel_pch_rawclk(struct drm_i915_private *dev_priv)
d2acd215 185{
e7dc33f3
VS
186 return (I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK) * 1000;
187}
d2acd215 188
e7dc33f3
VS
189static int
190intel_vlv_hrawclk(struct drm_i915_private *dev_priv)
191{
19ab4ed3 192 /* RAWCLK_FREQ_VLV register updated from power well code */
35d38d1f
VS
193 return vlv_get_cck_clock_hpll(dev_priv, "hrawclk",
194 CCK_DISPLAY_REF_CLOCK_CONTROL);
d2acd215
DV
195}
196
e7dc33f3
VS
197static int
198intel_g4x_hrawclk(struct drm_i915_private *dev_priv)
79e50a4f 199{
79e50a4f
JN
200 uint32_t clkcfg;
201
e7dc33f3 202 /* hrawclock is 1/4 the FSB frequency */
79e50a4f
JN
203 clkcfg = I915_READ(CLKCFG);
204 switch (clkcfg & CLKCFG_FSB_MASK) {
205 case CLKCFG_FSB_400:
e7dc33f3 206 return 100000;
79e50a4f 207 case CLKCFG_FSB_533:
e7dc33f3 208 return 133333;
79e50a4f 209 case CLKCFG_FSB_667:
e7dc33f3 210 return 166667;
79e50a4f 211 case CLKCFG_FSB_800:
e7dc33f3 212 return 200000;
79e50a4f 213 case CLKCFG_FSB_1067:
e7dc33f3 214 return 266667;
79e50a4f 215 case CLKCFG_FSB_1333:
e7dc33f3 216 return 333333;
79e50a4f
JN
217 /* these two are just a guess; one of them might be right */
218 case CLKCFG_FSB_1600:
219 case CLKCFG_FSB_1600_ALT:
e7dc33f3 220 return 400000;
79e50a4f 221 default:
e7dc33f3 222 return 133333;
79e50a4f
JN
223 }
224}
225
19ab4ed3 226void intel_update_rawclk(struct drm_i915_private *dev_priv)
e7dc33f3
VS
227{
228 if (HAS_PCH_SPLIT(dev_priv))
229 dev_priv->rawclk_freq = intel_pch_rawclk(dev_priv);
230 else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
231 dev_priv->rawclk_freq = intel_vlv_hrawclk(dev_priv);
232 else if (IS_G4X(dev_priv) || IS_PINEVIEW(dev_priv))
233 dev_priv->rawclk_freq = intel_g4x_hrawclk(dev_priv);
234 else
235 return; /* no rawclk on other platforms, or no need to know it */
236
237 DRM_DEBUG_DRIVER("rawclk rate: %d kHz\n", dev_priv->rawclk_freq);
238}
239
bfa7df01
VS
240static void intel_update_czclk(struct drm_i915_private *dev_priv)
241{
666a4537 242 if (!(IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)))
bfa7df01
VS
243 return;
244
245 dev_priv->czclk_freq = vlv_get_cck_clock_hpll(dev_priv, "czclk",
246 CCK_CZ_CLOCK_CONTROL);
247
248 DRM_DEBUG_DRIVER("CZ clock rate: %d kHz\n", dev_priv->czclk_freq);
249}
250
021357ac 251static inline u32 /* units of 100MHz */
21a727b3
VS
252intel_fdi_link_freq(struct drm_i915_private *dev_priv,
253 const struct intel_crtc_state *pipe_config)
021357ac 254{
21a727b3
VS
255 if (HAS_DDI(dev_priv))
256 return pipe_config->port_clock; /* SPLL */
257 else if (IS_GEN5(dev_priv))
258 return ((I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2) * 10000;
e3b247da 259 else
21a727b3 260 return 270000;
021357ac
CW
261}
262
1b6f4958 263static const struct intel_limit intel_limits_i8xx_dac = {
0206e353 264 .dot = { .min = 25000, .max = 350000 },
9c333719 265 .vco = { .min = 908000, .max = 1512000 },
91dbe5fb 266 .n = { .min = 2, .max = 16 },
0206e353
AJ
267 .m = { .min = 96, .max = 140 },
268 .m1 = { .min = 18, .max = 26 },
269 .m2 = { .min = 6, .max = 16 },
270 .p = { .min = 4, .max = 128 },
271 .p1 = { .min = 2, .max = 33 },
273e27ca
EA
272 .p2 = { .dot_limit = 165000,
273 .p2_slow = 4, .p2_fast = 2 },
e4b36699
KP
274};
275
1b6f4958 276static const struct intel_limit intel_limits_i8xx_dvo = {
5d536e28 277 .dot = { .min = 25000, .max = 350000 },
9c333719 278 .vco = { .min = 908000, .max = 1512000 },
91dbe5fb 279 .n = { .min = 2, .max = 16 },
5d536e28
DV
280 .m = { .min = 96, .max = 140 },
281 .m1 = { .min = 18, .max = 26 },
282 .m2 = { .min = 6, .max = 16 },
283 .p = { .min = 4, .max = 128 },
284 .p1 = { .min = 2, .max = 33 },
285 .p2 = { .dot_limit = 165000,
286 .p2_slow = 4, .p2_fast = 4 },
287};
288
1b6f4958 289static const struct intel_limit intel_limits_i8xx_lvds = {
0206e353 290 .dot = { .min = 25000, .max = 350000 },
9c333719 291 .vco = { .min = 908000, .max = 1512000 },
91dbe5fb 292 .n = { .min = 2, .max = 16 },
0206e353
AJ
293 .m = { .min = 96, .max = 140 },
294 .m1 = { .min = 18, .max = 26 },
295 .m2 = { .min = 6, .max = 16 },
296 .p = { .min = 4, .max = 128 },
297 .p1 = { .min = 1, .max = 6 },
273e27ca
EA
298 .p2 = { .dot_limit = 165000,
299 .p2_slow = 14, .p2_fast = 7 },
e4b36699 300};
273e27ca 301
1b6f4958 302static const struct intel_limit intel_limits_i9xx_sdvo = {
0206e353
AJ
303 .dot = { .min = 20000, .max = 400000 },
304 .vco = { .min = 1400000, .max = 2800000 },
305 .n = { .min = 1, .max = 6 },
306 .m = { .min = 70, .max = 120 },
4f7dfb67
PJ
307 .m1 = { .min = 8, .max = 18 },
308 .m2 = { .min = 3, .max = 7 },
0206e353
AJ
309 .p = { .min = 5, .max = 80 },
310 .p1 = { .min = 1, .max = 8 },
273e27ca
EA
311 .p2 = { .dot_limit = 200000,
312 .p2_slow = 10, .p2_fast = 5 },
e4b36699
KP
313};
314
1b6f4958 315static const struct intel_limit intel_limits_i9xx_lvds = {
0206e353
AJ
316 .dot = { .min = 20000, .max = 400000 },
317 .vco = { .min = 1400000, .max = 2800000 },
318 .n = { .min = 1, .max = 6 },
319 .m = { .min = 70, .max = 120 },
53a7d2d1
PJ
320 .m1 = { .min = 8, .max = 18 },
321 .m2 = { .min = 3, .max = 7 },
0206e353
AJ
322 .p = { .min = 7, .max = 98 },
323 .p1 = { .min = 1, .max = 8 },
273e27ca
EA
324 .p2 = { .dot_limit = 112000,
325 .p2_slow = 14, .p2_fast = 7 },
e4b36699
KP
326};
327
273e27ca 328
1b6f4958 329static const struct intel_limit intel_limits_g4x_sdvo = {
273e27ca
EA
330 .dot = { .min = 25000, .max = 270000 },
331 .vco = { .min = 1750000, .max = 3500000},
332 .n = { .min = 1, .max = 4 },
333 .m = { .min = 104, .max = 138 },
334 .m1 = { .min = 17, .max = 23 },
335 .m2 = { .min = 5, .max = 11 },
336 .p = { .min = 10, .max = 30 },
337 .p1 = { .min = 1, .max = 3},
338 .p2 = { .dot_limit = 270000,
339 .p2_slow = 10,
340 .p2_fast = 10
044c7c41 341 },
e4b36699
KP
342};
343
1b6f4958 344static const struct intel_limit intel_limits_g4x_hdmi = {
273e27ca
EA
345 .dot = { .min = 22000, .max = 400000 },
346 .vco = { .min = 1750000, .max = 3500000},
347 .n = { .min = 1, .max = 4 },
348 .m = { .min = 104, .max = 138 },
349 .m1 = { .min = 16, .max = 23 },
350 .m2 = { .min = 5, .max = 11 },
351 .p = { .min = 5, .max = 80 },
352 .p1 = { .min = 1, .max = 8},
353 .p2 = { .dot_limit = 165000,
354 .p2_slow = 10, .p2_fast = 5 },
e4b36699
KP
355};
356
1b6f4958 357static const struct intel_limit intel_limits_g4x_single_channel_lvds = {
273e27ca
EA
358 .dot = { .min = 20000, .max = 115000 },
359 .vco = { .min = 1750000, .max = 3500000 },
360 .n = { .min = 1, .max = 3 },
361 .m = { .min = 104, .max = 138 },
362 .m1 = { .min = 17, .max = 23 },
363 .m2 = { .min = 5, .max = 11 },
364 .p = { .min = 28, .max = 112 },
365 .p1 = { .min = 2, .max = 8 },
366 .p2 = { .dot_limit = 0,
367 .p2_slow = 14, .p2_fast = 14
044c7c41 368 },
e4b36699
KP
369};
370
1b6f4958 371static const struct intel_limit intel_limits_g4x_dual_channel_lvds = {
273e27ca
EA
372 .dot = { .min = 80000, .max = 224000 },
373 .vco = { .min = 1750000, .max = 3500000 },
374 .n = { .min = 1, .max = 3 },
375 .m = { .min = 104, .max = 138 },
376 .m1 = { .min = 17, .max = 23 },
377 .m2 = { .min = 5, .max = 11 },
378 .p = { .min = 14, .max = 42 },
379 .p1 = { .min = 2, .max = 6 },
380 .p2 = { .dot_limit = 0,
381 .p2_slow = 7, .p2_fast = 7
044c7c41 382 },
e4b36699
KP
383};
384
1b6f4958 385static const struct intel_limit intel_limits_pineview_sdvo = {
0206e353
AJ
386 .dot = { .min = 20000, .max = 400000},
387 .vco = { .min = 1700000, .max = 3500000 },
273e27ca 388 /* Pineview's Ncounter is a ring counter */
0206e353
AJ
389 .n = { .min = 3, .max = 6 },
390 .m = { .min = 2, .max = 256 },
273e27ca 391 /* Pineview only has one combined m divider, which we treat as m2. */
0206e353
AJ
392 .m1 = { .min = 0, .max = 0 },
393 .m2 = { .min = 0, .max = 254 },
394 .p = { .min = 5, .max = 80 },
395 .p1 = { .min = 1, .max = 8 },
273e27ca
EA
396 .p2 = { .dot_limit = 200000,
397 .p2_slow = 10, .p2_fast = 5 },
e4b36699
KP
398};
399
1b6f4958 400static const struct intel_limit intel_limits_pineview_lvds = {
0206e353
AJ
401 .dot = { .min = 20000, .max = 400000 },
402 .vco = { .min = 1700000, .max = 3500000 },
403 .n = { .min = 3, .max = 6 },
404 .m = { .min = 2, .max = 256 },
405 .m1 = { .min = 0, .max = 0 },
406 .m2 = { .min = 0, .max = 254 },
407 .p = { .min = 7, .max = 112 },
408 .p1 = { .min = 1, .max = 8 },
273e27ca
EA
409 .p2 = { .dot_limit = 112000,
410 .p2_slow = 14, .p2_fast = 14 },
e4b36699
KP
411};
412
273e27ca
EA
413/* Ironlake / Sandybridge
414 *
415 * We calculate clock using (register_value + 2) for N/M1/M2, so here
416 * the range value for them is (actual_value - 2).
417 */
1b6f4958 418static const struct intel_limit intel_limits_ironlake_dac = {
273e27ca
EA
419 .dot = { .min = 25000, .max = 350000 },
420 .vco = { .min = 1760000, .max = 3510000 },
421 .n = { .min = 1, .max = 5 },
422 .m = { .min = 79, .max = 127 },
423 .m1 = { .min = 12, .max = 22 },
424 .m2 = { .min = 5, .max = 9 },
425 .p = { .min = 5, .max = 80 },
426 .p1 = { .min = 1, .max = 8 },
427 .p2 = { .dot_limit = 225000,
428 .p2_slow = 10, .p2_fast = 5 },
e4b36699
KP
429};
430
1b6f4958 431static const struct intel_limit intel_limits_ironlake_single_lvds = {
273e27ca
EA
432 .dot = { .min = 25000, .max = 350000 },
433 .vco = { .min = 1760000, .max = 3510000 },
434 .n = { .min = 1, .max = 3 },
435 .m = { .min = 79, .max = 118 },
436 .m1 = { .min = 12, .max = 22 },
437 .m2 = { .min = 5, .max = 9 },
438 .p = { .min = 28, .max = 112 },
439 .p1 = { .min = 2, .max = 8 },
440 .p2 = { .dot_limit = 225000,
441 .p2_slow = 14, .p2_fast = 14 },
b91ad0ec
ZW
442};
443
1b6f4958 444static const struct intel_limit intel_limits_ironlake_dual_lvds = {
273e27ca
EA
445 .dot = { .min = 25000, .max = 350000 },
446 .vco = { .min = 1760000, .max = 3510000 },
447 .n = { .min = 1, .max = 3 },
448 .m = { .min = 79, .max = 127 },
449 .m1 = { .min = 12, .max = 22 },
450 .m2 = { .min = 5, .max = 9 },
451 .p = { .min = 14, .max = 56 },
452 .p1 = { .min = 2, .max = 8 },
453 .p2 = { .dot_limit = 225000,
454 .p2_slow = 7, .p2_fast = 7 },
b91ad0ec
ZW
455};
456
273e27ca 457/* LVDS 100mhz refclk limits. */
1b6f4958 458static const struct intel_limit intel_limits_ironlake_single_lvds_100m = {
273e27ca
EA
459 .dot = { .min = 25000, .max = 350000 },
460 .vco = { .min = 1760000, .max = 3510000 },
461 .n = { .min = 1, .max = 2 },
462 .m = { .min = 79, .max = 126 },
463 .m1 = { .min = 12, .max = 22 },
464 .m2 = { .min = 5, .max = 9 },
465 .p = { .min = 28, .max = 112 },
0206e353 466 .p1 = { .min = 2, .max = 8 },
273e27ca
EA
467 .p2 = { .dot_limit = 225000,
468 .p2_slow = 14, .p2_fast = 14 },
b91ad0ec
ZW
469};
470
1b6f4958 471static const struct intel_limit intel_limits_ironlake_dual_lvds_100m = {
273e27ca
EA
472 .dot = { .min = 25000, .max = 350000 },
473 .vco = { .min = 1760000, .max = 3510000 },
474 .n = { .min = 1, .max = 3 },
475 .m = { .min = 79, .max = 126 },
476 .m1 = { .min = 12, .max = 22 },
477 .m2 = { .min = 5, .max = 9 },
478 .p = { .min = 14, .max = 42 },
0206e353 479 .p1 = { .min = 2, .max = 6 },
273e27ca
EA
480 .p2 = { .dot_limit = 225000,
481 .p2_slow = 7, .p2_fast = 7 },
4547668a
ZY
482};
483
1b6f4958 484static const struct intel_limit intel_limits_vlv = {
f01b7962
VS
485 /*
486 * These are the data rate limits (measured in fast clocks)
487 * since those are the strictest limits we have. The fast
488 * clock and actual rate limits are more relaxed, so checking
489 * them would make no difference.
490 */
491 .dot = { .min = 25000 * 5, .max = 270000 * 5 },
75e53986 492 .vco = { .min = 4000000, .max = 6000000 },
a0c4da24 493 .n = { .min = 1, .max = 7 },
a0c4da24
JB
494 .m1 = { .min = 2, .max = 3 },
495 .m2 = { .min = 11, .max = 156 },
b99ab663 496 .p1 = { .min = 2, .max = 3 },
5fdc9c49 497 .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
a0c4da24
JB
498};
499
1b6f4958 500static const struct intel_limit intel_limits_chv = {
ef9348c8
CML
501 /*
502 * These are the data rate limits (measured in fast clocks)
503 * since those are the strictest limits we have. The fast
504 * clock and actual rate limits are more relaxed, so checking
505 * them would make no difference.
506 */
507 .dot = { .min = 25000 * 5, .max = 540000 * 5},
17fe1021 508 .vco = { .min = 4800000, .max = 6480000 },
ef9348c8
CML
509 .n = { .min = 1, .max = 1 },
510 .m1 = { .min = 2, .max = 2 },
511 .m2 = { .min = 24 << 22, .max = 175 << 22 },
512 .p1 = { .min = 2, .max = 4 },
513 .p2 = { .p2_slow = 1, .p2_fast = 14 },
514};
515
1b6f4958 516static const struct intel_limit intel_limits_bxt = {
5ab7b0b7
ID
517 /* FIXME: find real dot limits */
518 .dot = { .min = 0, .max = INT_MAX },
e6292556 519 .vco = { .min = 4800000, .max = 6700000 },
5ab7b0b7
ID
520 .n = { .min = 1, .max = 1 },
521 .m1 = { .min = 2, .max = 2 },
522 /* FIXME: find real m2 limits */
523 .m2 = { .min = 2 << 22, .max = 255 << 22 },
524 .p1 = { .min = 2, .max = 4 },
525 .p2 = { .p2_slow = 1, .p2_fast = 20 },
526};
527
cdba954e
ACO
528static bool
529needs_modeset(struct drm_crtc_state *state)
530{
fc596660 531 return drm_atomic_crtc_needs_modeset(state);
cdba954e
ACO
532}
533
dccbea3b
ID
534/*
535 * Platform specific helpers to calculate the port PLL loopback- (clock.m),
536 * and post-divider (clock.p) values, pre- (clock.vco) and post-divided fast
537 * (clock.dot) clock rates. This fast dot clock is fed to the port's IO logic.
538 * The helpers' return value is the rate of the clock that is fed to the
539 * display engine's pipe which can be the above fast dot clock rate or a
540 * divided-down version of it.
541 */
f2b115e6 542/* m1 is reserved as 0 in Pineview, n is a ring counter */
9e2c8475 543static int pnv_calc_dpll_params(int refclk, struct dpll *clock)
79e53945 544{
2177832f
SL
545 clock->m = clock->m2 + 2;
546 clock->p = clock->p1 * clock->p2;
ed5ca77e 547 if (WARN_ON(clock->n == 0 || clock->p == 0))
dccbea3b 548 return 0;
fb03ac01
VS
549 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
550 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
dccbea3b
ID
551
552 return clock->dot;
2177832f
SL
553}
554
7429e9d4
DV
555static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
556{
557 return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
558}
559
9e2c8475 560static int i9xx_calc_dpll_params(int refclk, struct dpll *clock)
2177832f 561{
7429e9d4 562 clock->m = i9xx_dpll_compute_m(clock);
79e53945 563 clock->p = clock->p1 * clock->p2;
ed5ca77e 564 if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
dccbea3b 565 return 0;
fb03ac01
VS
566 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
567 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
dccbea3b
ID
568
569 return clock->dot;
79e53945
JB
570}
571
9e2c8475 572static int vlv_calc_dpll_params(int refclk, struct dpll *clock)
589eca67
ID
573{
574 clock->m = clock->m1 * clock->m2;
575 clock->p = clock->p1 * clock->p2;
576 if (WARN_ON(clock->n == 0 || clock->p == 0))
dccbea3b 577 return 0;
589eca67
ID
578 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
579 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
dccbea3b
ID
580
581 return clock->dot / 5;
589eca67
ID
582}
583
9e2c8475 584int chv_calc_dpll_params(int refclk, struct dpll *clock)
ef9348c8
CML
585{
586 clock->m = clock->m1 * clock->m2;
587 clock->p = clock->p1 * clock->p2;
588 if (WARN_ON(clock->n == 0 || clock->p == 0))
dccbea3b 589 return 0;
ef9348c8
CML
590 clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
591 clock->n << 22);
592 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
dccbea3b
ID
593
594 return clock->dot / 5;
ef9348c8
CML
595}
596
7c04d1d9 597#define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
79e53945
JB
598/**
599 * Returns whether the given set of divisors are valid for a given refclk with
600 * the given connectors.
601 */
602
1b894b59 603static bool intel_PLL_is_valid(struct drm_device *dev,
1b6f4958 604 const struct intel_limit *limit,
9e2c8475 605 const struct dpll *clock)
79e53945 606{
f01b7962
VS
607 if (clock->n < limit->n.min || limit->n.max < clock->n)
608 INTELPllInvalid("n out of range\n");
79e53945 609 if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
0206e353 610 INTELPllInvalid("p1 out of range\n");
79e53945 611 if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
0206e353 612 INTELPllInvalid("m2 out of range\n");
79e53945 613 if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
0206e353 614 INTELPllInvalid("m1 out of range\n");
f01b7962 615
666a4537
WB
616 if (!IS_PINEVIEW(dev) && !IS_VALLEYVIEW(dev) &&
617 !IS_CHERRYVIEW(dev) && !IS_BROXTON(dev))
f01b7962
VS
618 if (clock->m1 <= clock->m2)
619 INTELPllInvalid("m1 <= m2\n");
620
666a4537 621 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev) && !IS_BROXTON(dev)) {
f01b7962
VS
622 if (clock->p < limit->p.min || limit->p.max < clock->p)
623 INTELPllInvalid("p out of range\n");
624 if (clock->m < limit->m.min || limit->m.max < clock->m)
625 INTELPllInvalid("m out of range\n");
626 }
627
79e53945 628 if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
0206e353 629 INTELPllInvalid("vco out of range\n");
79e53945
JB
630 /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
631 * connector, etc., rather than just a single range.
632 */
633 if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
0206e353 634 INTELPllInvalid("dot out of range\n");
79e53945
JB
635
636 return true;
637}
638
3b1429d9 639static int
1b6f4958 640i9xx_select_p2_div(const struct intel_limit *limit,
3b1429d9
VS
641 const struct intel_crtc_state *crtc_state,
642 int target)
79e53945 643{
3b1429d9 644 struct drm_device *dev = crtc_state->base.crtc->dev;
79e53945 645
2d84d2b3 646 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
79e53945 647 /*
a210b028
DV
648 * For LVDS just rely on its current settings for dual-channel.
649 * We haven't figured out how to reliably set up different
650 * single/dual channel state, if we even can.
79e53945 651 */
1974cad0 652 if (intel_is_dual_link_lvds(dev))
3b1429d9 653 return limit->p2.p2_fast;
79e53945 654 else
3b1429d9 655 return limit->p2.p2_slow;
79e53945
JB
656 } else {
657 if (target < limit->p2.dot_limit)
3b1429d9 658 return limit->p2.p2_slow;
79e53945 659 else
3b1429d9 660 return limit->p2.p2_fast;
79e53945 661 }
3b1429d9
VS
662}
663
70e8aa21
ACO
664/*
665 * Returns a set of divisors for the desired target clock with the given
666 * refclk, or FALSE. The returned values represent the clock equation:
667 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
668 *
669 * Target and reference clocks are specified in kHz.
670 *
671 * If match_clock is provided, then best_clock P divider must match the P
672 * divider from @match_clock used for LVDS downclocking.
673 */
3b1429d9 674static bool
1b6f4958 675i9xx_find_best_dpll(const struct intel_limit *limit,
3b1429d9 676 struct intel_crtc_state *crtc_state,
9e2c8475
ACO
677 int target, int refclk, struct dpll *match_clock,
678 struct dpll *best_clock)
3b1429d9
VS
679{
680 struct drm_device *dev = crtc_state->base.crtc->dev;
9e2c8475 681 struct dpll clock;
3b1429d9 682 int err = target;
79e53945 683
0206e353 684 memset(best_clock, 0, sizeof(*best_clock));
79e53945 685
3b1429d9
VS
686 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
687
42158660
ZY
688 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
689 clock.m1++) {
690 for (clock.m2 = limit->m2.min;
691 clock.m2 <= limit->m2.max; clock.m2++) {
c0efc387 692 if (clock.m2 >= clock.m1)
42158660
ZY
693 break;
694 for (clock.n = limit->n.min;
695 clock.n <= limit->n.max; clock.n++) {
696 for (clock.p1 = limit->p1.min;
697 clock.p1 <= limit->p1.max; clock.p1++) {
79e53945
JB
698 int this_err;
699
dccbea3b 700 i9xx_calc_dpll_params(refclk, &clock);
ac58c3f0
DV
701 if (!intel_PLL_is_valid(dev, limit,
702 &clock))
703 continue;
704 if (match_clock &&
705 clock.p != match_clock->p)
706 continue;
707
708 this_err = abs(clock.dot - target);
709 if (this_err < err) {
710 *best_clock = clock;
711 err = this_err;
712 }
713 }
714 }
715 }
716 }
717
718 return (err != target);
719}
720
70e8aa21
ACO
721/*
722 * Returns a set of divisors for the desired target clock with the given
723 * refclk, or FALSE. The returned values represent the clock equation:
724 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
725 *
726 * Target and reference clocks are specified in kHz.
727 *
728 * If match_clock is provided, then best_clock P divider must match the P
729 * divider from @match_clock used for LVDS downclocking.
730 */
ac58c3f0 731static bool
1b6f4958 732pnv_find_best_dpll(const struct intel_limit *limit,
a93e255f 733 struct intel_crtc_state *crtc_state,
9e2c8475
ACO
734 int target, int refclk, struct dpll *match_clock,
735 struct dpll *best_clock)
79e53945 736{
3b1429d9 737 struct drm_device *dev = crtc_state->base.crtc->dev;
9e2c8475 738 struct dpll clock;
79e53945
JB
739 int err = target;
740
0206e353 741 memset(best_clock, 0, sizeof(*best_clock));
79e53945 742
3b1429d9
VS
743 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
744
42158660
ZY
745 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
746 clock.m1++) {
747 for (clock.m2 = limit->m2.min;
748 clock.m2 <= limit->m2.max; clock.m2++) {
42158660
ZY
749 for (clock.n = limit->n.min;
750 clock.n <= limit->n.max; clock.n++) {
751 for (clock.p1 = limit->p1.min;
752 clock.p1 <= limit->p1.max; clock.p1++) {
79e53945
JB
753 int this_err;
754
dccbea3b 755 pnv_calc_dpll_params(refclk, &clock);
1b894b59
CW
756 if (!intel_PLL_is_valid(dev, limit,
757 &clock))
79e53945 758 continue;
cec2f356
SP
759 if (match_clock &&
760 clock.p != match_clock->p)
761 continue;
79e53945
JB
762
763 this_err = abs(clock.dot - target);
764 if (this_err < err) {
765 *best_clock = clock;
766 err = this_err;
767 }
768 }
769 }
770 }
771 }
772
773 return (err != target);
774}
775
997c030c
ACO
776/*
777 * Returns a set of divisors for the desired target clock with the given
778 * refclk, or FALSE. The returned values represent the clock equation:
779 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
70e8aa21
ACO
780 *
781 * Target and reference clocks are specified in kHz.
782 *
783 * If match_clock is provided, then best_clock P divider must match the P
784 * divider from @match_clock used for LVDS downclocking.
997c030c 785 */
d4906093 786static bool
1b6f4958 787g4x_find_best_dpll(const struct intel_limit *limit,
a93e255f 788 struct intel_crtc_state *crtc_state,
9e2c8475
ACO
789 int target, int refclk, struct dpll *match_clock,
790 struct dpll *best_clock)
d4906093 791{
3b1429d9 792 struct drm_device *dev = crtc_state->base.crtc->dev;
9e2c8475 793 struct dpll clock;
d4906093 794 int max_n;
3b1429d9 795 bool found = false;
6ba770dc
AJ
796 /* approximately equals target * 0.00585 */
797 int err_most = (target >> 8) + (target >> 9);
d4906093
ML
798
799 memset(best_clock, 0, sizeof(*best_clock));
3b1429d9
VS
800
801 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
802
d4906093 803 max_n = limit->n.max;
f77f13e2 804 /* based on hardware requirement, prefer smaller n to precision */
d4906093 805 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
f77f13e2 806 /* based on hardware requirement, prefere larger m1,m2 */
d4906093
ML
807 for (clock.m1 = limit->m1.max;
808 clock.m1 >= limit->m1.min; clock.m1--) {
809 for (clock.m2 = limit->m2.max;
810 clock.m2 >= limit->m2.min; clock.m2--) {
811 for (clock.p1 = limit->p1.max;
812 clock.p1 >= limit->p1.min; clock.p1--) {
813 int this_err;
814
dccbea3b 815 i9xx_calc_dpll_params(refclk, &clock);
1b894b59
CW
816 if (!intel_PLL_is_valid(dev, limit,
817 &clock))
d4906093 818 continue;
1b894b59
CW
819
820 this_err = abs(clock.dot - target);
d4906093
ML
821 if (this_err < err_most) {
822 *best_clock = clock;
823 err_most = this_err;
824 max_n = clock.n;
825 found = true;
826 }
827 }
828 }
829 }
830 }
2c07245f
ZW
831 return found;
832}
833
d5dd62bd
ID
834/*
835 * Check if the calculated PLL configuration is more optimal compared to the
836 * best configuration and error found so far. Return the calculated error.
837 */
838static bool vlv_PLL_is_optimal(struct drm_device *dev, int target_freq,
9e2c8475
ACO
839 const struct dpll *calculated_clock,
840 const struct dpll *best_clock,
d5dd62bd
ID
841 unsigned int best_error_ppm,
842 unsigned int *error_ppm)
843{
9ca3ba01
ID
844 /*
845 * For CHV ignore the error and consider only the P value.
846 * Prefer a bigger P value based on HW requirements.
847 */
848 if (IS_CHERRYVIEW(dev)) {
849 *error_ppm = 0;
850
851 return calculated_clock->p > best_clock->p;
852 }
853
24be4e46
ID
854 if (WARN_ON_ONCE(!target_freq))
855 return false;
856
d5dd62bd
ID
857 *error_ppm = div_u64(1000000ULL *
858 abs(target_freq - calculated_clock->dot),
859 target_freq);
860 /*
861 * Prefer a better P value over a better (smaller) error if the error
862 * is small. Ensure this preference for future configurations too by
863 * setting the error to 0.
864 */
865 if (*error_ppm < 100 && calculated_clock->p > best_clock->p) {
866 *error_ppm = 0;
867
868 return true;
869 }
870
871 return *error_ppm + 10 < best_error_ppm;
872}
873
65b3d6a9
ACO
874/*
875 * Returns a set of divisors for the desired target clock with the given
876 * refclk, or FALSE. The returned values represent the clock equation:
877 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
878 */
a0c4da24 879static bool
1b6f4958 880vlv_find_best_dpll(const struct intel_limit *limit,
a93e255f 881 struct intel_crtc_state *crtc_state,
9e2c8475
ACO
882 int target, int refclk, struct dpll *match_clock,
883 struct dpll *best_clock)
a0c4da24 884{
a93e255f 885 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
a919ff14 886 struct drm_device *dev = crtc->base.dev;
9e2c8475 887 struct dpll clock;
69e4f900 888 unsigned int bestppm = 1000000;
27e639bf
VS
889 /* min update 19.2 MHz */
890 int max_n = min(limit->n.max, refclk / 19200);
49e497ef 891 bool found = false;
a0c4da24 892
6b4bf1c4
VS
893 target *= 5; /* fast clock */
894
895 memset(best_clock, 0, sizeof(*best_clock));
a0c4da24
JB
896
897 /* based on hardware requirement, prefer smaller n to precision */
27e639bf 898 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
811bbf05 899 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
889059d8 900 for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
c1a9ae43 901 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
6b4bf1c4 902 clock.p = clock.p1 * clock.p2;
a0c4da24 903 /* based on hardware requirement, prefer bigger m1,m2 values */
6b4bf1c4 904 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
d5dd62bd 905 unsigned int ppm;
69e4f900 906
6b4bf1c4
VS
907 clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
908 refclk * clock.m1);
909
dccbea3b 910 vlv_calc_dpll_params(refclk, &clock);
43b0ac53 911
f01b7962
VS
912 if (!intel_PLL_is_valid(dev, limit,
913 &clock))
43b0ac53
VS
914 continue;
915
d5dd62bd
ID
916 if (!vlv_PLL_is_optimal(dev, target,
917 &clock,
918 best_clock,
919 bestppm, &ppm))
920 continue;
6b4bf1c4 921
d5dd62bd
ID
922 *best_clock = clock;
923 bestppm = ppm;
924 found = true;
a0c4da24
JB
925 }
926 }
927 }
928 }
a0c4da24 929
49e497ef 930 return found;
a0c4da24 931}
a4fc5ed6 932
65b3d6a9
ACO
933/*
934 * Returns a set of divisors for the desired target clock with the given
935 * refclk, or FALSE. The returned values represent the clock equation:
936 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
937 */
ef9348c8 938static bool
1b6f4958 939chv_find_best_dpll(const struct intel_limit *limit,
a93e255f 940 struct intel_crtc_state *crtc_state,
9e2c8475
ACO
941 int target, int refclk, struct dpll *match_clock,
942 struct dpll *best_clock)
ef9348c8 943{
a93e255f 944 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
a919ff14 945 struct drm_device *dev = crtc->base.dev;
9ca3ba01 946 unsigned int best_error_ppm;
9e2c8475 947 struct dpll clock;
ef9348c8
CML
948 uint64_t m2;
949 int found = false;
950
951 memset(best_clock, 0, sizeof(*best_clock));
9ca3ba01 952 best_error_ppm = 1000000;
ef9348c8
CML
953
954 /*
955 * Based on hardware doc, the n always set to 1, and m1 always
956 * set to 2. If requires to support 200Mhz refclk, we need to
957 * revisit this because n may not 1 anymore.
958 */
959 clock.n = 1, clock.m1 = 2;
960 target *= 5; /* fast clock */
961
962 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
963 for (clock.p2 = limit->p2.p2_fast;
964 clock.p2 >= limit->p2.p2_slow;
965 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
9ca3ba01 966 unsigned int error_ppm;
ef9348c8
CML
967
968 clock.p = clock.p1 * clock.p2;
969
970 m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
971 clock.n) << 22, refclk * clock.m1);
972
973 if (m2 > INT_MAX/clock.m1)
974 continue;
975
976 clock.m2 = m2;
977
dccbea3b 978 chv_calc_dpll_params(refclk, &clock);
ef9348c8
CML
979
980 if (!intel_PLL_is_valid(dev, limit, &clock))
981 continue;
982
9ca3ba01
ID
983 if (!vlv_PLL_is_optimal(dev, target, &clock, best_clock,
984 best_error_ppm, &error_ppm))
985 continue;
986
987 *best_clock = clock;
988 best_error_ppm = error_ppm;
989 found = true;
ef9348c8
CML
990 }
991 }
992
993 return found;
994}
995
5ab7b0b7 996bool bxt_find_best_dpll(struct intel_crtc_state *crtc_state, int target_clock,
9e2c8475 997 struct dpll *best_clock)
5ab7b0b7 998{
65b3d6a9 999 int refclk = 100000;
1b6f4958 1000 const struct intel_limit *limit = &intel_limits_bxt;
5ab7b0b7 1001
65b3d6a9 1002 return chv_find_best_dpll(limit, crtc_state,
5ab7b0b7
ID
1003 target_clock, refclk, NULL, best_clock);
1004}
1005
20ddf665
VS
1006bool intel_crtc_active(struct drm_crtc *crtc)
1007{
1008 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1009
1010 /* Be paranoid as we can arrive here with only partial
1011 * state retrieved from the hardware during setup.
1012 *
241bfc38 1013 * We can ditch the adjusted_mode.crtc_clock check as soon
20ddf665
VS
1014 * as Haswell has gained clock readout/fastboot support.
1015 *
66e514c1 1016 * We can ditch the crtc->primary->fb check as soon as we can
20ddf665 1017 * properly reconstruct framebuffers.
c3d1f436
MR
1018 *
1019 * FIXME: The intel_crtc->active here should be switched to
1020 * crtc->state->active once we have proper CRTC states wired up
1021 * for atomic.
20ddf665 1022 */
c3d1f436 1023 return intel_crtc->active && crtc->primary->state->fb &&
6e3c9717 1024 intel_crtc->config->base.adjusted_mode.crtc_clock;
20ddf665
VS
1025}
1026
a5c961d1
PZ
1027enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
1028 enum pipe pipe)
1029{
1030 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
1031 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1032
6e3c9717 1033 return intel_crtc->config->cpu_transcoder;
a5c961d1
PZ
1034}
1035
fbf49ea2
VS
1036static bool pipe_dsl_stopped(struct drm_device *dev, enum pipe pipe)
1037{
fac5e23e 1038 struct drm_i915_private *dev_priv = to_i915(dev);
f0f59a00 1039 i915_reg_t reg = PIPEDSL(pipe);
fbf49ea2
VS
1040 u32 line1, line2;
1041 u32 line_mask;
1042
1043 if (IS_GEN2(dev))
1044 line_mask = DSL_LINEMASK_GEN2;
1045 else
1046 line_mask = DSL_LINEMASK_GEN3;
1047
1048 line1 = I915_READ(reg) & line_mask;
6adfb1ef 1049 msleep(5);
fbf49ea2
VS
1050 line2 = I915_READ(reg) & line_mask;
1051
1052 return line1 == line2;
1053}
1054
ab7ad7f6
KP
1055/*
1056 * intel_wait_for_pipe_off - wait for pipe to turn off
575f7ab7 1057 * @crtc: crtc whose pipe to wait for
9d0498a2
JB
1058 *
1059 * After disabling a pipe, we can't wait for vblank in the usual way,
1060 * spinning on the vblank interrupt status bit, since we won't actually
1061 * see an interrupt when the pipe is disabled.
1062 *
ab7ad7f6
KP
1063 * On Gen4 and above:
1064 * wait for the pipe register state bit to turn off
1065 *
1066 * Otherwise:
1067 * wait for the display line value to settle (it usually
1068 * ends up stopping at the start of the next frame).
58e10eb9 1069 *
9d0498a2 1070 */
575f7ab7 1071static void intel_wait_for_pipe_off(struct intel_crtc *crtc)
9d0498a2 1072{
575f7ab7 1073 struct drm_device *dev = crtc->base.dev;
fac5e23e 1074 struct drm_i915_private *dev_priv = to_i915(dev);
6e3c9717 1075 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
575f7ab7 1076 enum pipe pipe = crtc->pipe;
ab7ad7f6
KP
1077
1078 if (INTEL_INFO(dev)->gen >= 4) {
f0f59a00 1079 i915_reg_t reg = PIPECONF(cpu_transcoder);
ab7ad7f6
KP
1080
1081 /* Wait for the Pipe State to go off */
b8511f53
CW
1082 if (intel_wait_for_register(dev_priv,
1083 reg, I965_PIPECONF_ACTIVE, 0,
1084 100))
284637d9 1085 WARN(1, "pipe_off wait timed out\n");
ab7ad7f6 1086 } else {
ab7ad7f6 1087 /* Wait for the display line to settle */
fbf49ea2 1088 if (wait_for(pipe_dsl_stopped(dev, pipe), 100))
284637d9 1089 WARN(1, "pipe_off wait timed out\n");
ab7ad7f6 1090 }
79e53945
JB
1091}
1092
b24e7179 1093/* Only for pre-ILK configs */
55607e8a
DV
1094void assert_pll(struct drm_i915_private *dev_priv,
1095 enum pipe pipe, bool state)
b24e7179 1096{
b24e7179
JB
1097 u32 val;
1098 bool cur_state;
1099
649636ef 1100 val = I915_READ(DPLL(pipe));
b24e7179 1101 cur_state = !!(val & DPLL_VCO_ENABLE);
e2c719b7 1102 I915_STATE_WARN(cur_state != state,
b24e7179 1103 "PLL state assertion failure (expected %s, current %s)\n",
87ad3212 1104 onoff(state), onoff(cur_state));
b24e7179 1105}
b24e7179 1106
23538ef1 1107/* XXX: the dsi pll is shared between MIPI DSI ports */
8563b1e8 1108void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
23538ef1
JN
1109{
1110 u32 val;
1111 bool cur_state;
1112
a580516d 1113 mutex_lock(&dev_priv->sb_lock);
23538ef1 1114 val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
a580516d 1115 mutex_unlock(&dev_priv->sb_lock);
23538ef1
JN
1116
1117 cur_state = val & DSI_PLL_VCO_EN;
e2c719b7 1118 I915_STATE_WARN(cur_state != state,
23538ef1 1119 "DSI PLL state assertion failure (expected %s, current %s)\n",
87ad3212 1120 onoff(state), onoff(cur_state));
23538ef1 1121}
23538ef1 1122
040484af
JB
1123static void assert_fdi_tx(struct drm_i915_private *dev_priv,
1124 enum pipe pipe, bool state)
1125{
040484af 1126 bool cur_state;
ad80a810
PZ
1127 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1128 pipe);
040484af 1129
2d1fe073 1130 if (HAS_DDI(dev_priv)) {
affa9354 1131 /* DDI does not have a specific FDI_TX register */
649636ef 1132 u32 val = I915_READ(TRANS_DDI_FUNC_CTL(cpu_transcoder));
ad80a810 1133 cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
bf507ef7 1134 } else {
649636ef 1135 u32 val = I915_READ(FDI_TX_CTL(pipe));
bf507ef7
ED
1136 cur_state = !!(val & FDI_TX_ENABLE);
1137 }
e2c719b7 1138 I915_STATE_WARN(cur_state != state,
040484af 1139 "FDI TX state assertion failure (expected %s, current %s)\n",
87ad3212 1140 onoff(state), onoff(cur_state));
040484af
JB
1141}
1142#define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
1143#define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
1144
1145static void assert_fdi_rx(struct drm_i915_private *dev_priv,
1146 enum pipe pipe, bool state)
1147{
040484af
JB
1148 u32 val;
1149 bool cur_state;
1150
649636ef 1151 val = I915_READ(FDI_RX_CTL(pipe));
d63fa0dc 1152 cur_state = !!(val & FDI_RX_ENABLE);
e2c719b7 1153 I915_STATE_WARN(cur_state != state,
040484af 1154 "FDI RX state assertion failure (expected %s, current %s)\n",
87ad3212 1155 onoff(state), onoff(cur_state));
040484af
JB
1156}
1157#define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
1158#define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
1159
1160static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
1161 enum pipe pipe)
1162{
040484af
JB
1163 u32 val;
1164
1165 /* ILK FDI PLL is always enabled */
7e22dbbb 1166 if (IS_GEN5(dev_priv))
040484af
JB
1167 return;
1168
bf507ef7 1169 /* On Haswell, DDI ports are responsible for the FDI PLL setup */
2d1fe073 1170 if (HAS_DDI(dev_priv))
bf507ef7
ED
1171 return;
1172
649636ef 1173 val = I915_READ(FDI_TX_CTL(pipe));
e2c719b7 1174 I915_STATE_WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
040484af
JB
1175}
1176
55607e8a
DV
1177void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
1178 enum pipe pipe, bool state)
040484af 1179{
040484af 1180 u32 val;
55607e8a 1181 bool cur_state;
040484af 1182
649636ef 1183 val = I915_READ(FDI_RX_CTL(pipe));
55607e8a 1184 cur_state = !!(val & FDI_RX_PLL_ENABLE);
e2c719b7 1185 I915_STATE_WARN(cur_state != state,
55607e8a 1186 "FDI RX PLL assertion failure (expected %s, current %s)\n",
87ad3212 1187 onoff(state), onoff(cur_state));
040484af
JB
1188}
1189
b680c37a
DV
1190void assert_panel_unlocked(struct drm_i915_private *dev_priv,
1191 enum pipe pipe)
ea0760cf 1192{
91c8a326 1193 struct drm_device *dev = &dev_priv->drm;
f0f59a00 1194 i915_reg_t pp_reg;
ea0760cf
JB
1195 u32 val;
1196 enum pipe panel_pipe = PIPE_A;
0de3b485 1197 bool locked = true;
ea0760cf 1198
bedd4dba
JN
1199 if (WARN_ON(HAS_DDI(dev)))
1200 return;
1201
1202 if (HAS_PCH_SPLIT(dev)) {
1203 u32 port_sel;
1204
ea0760cf 1205 pp_reg = PCH_PP_CONTROL;
bedd4dba
JN
1206 port_sel = I915_READ(PCH_PP_ON_DELAYS) & PANEL_PORT_SELECT_MASK;
1207
1208 if (port_sel == PANEL_PORT_SELECT_LVDS &&
1209 I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
1210 panel_pipe = PIPE_B;
1211 /* XXX: else fix for eDP */
666a4537 1212 } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
bedd4dba
JN
1213 /* presumably write lock depends on pipe, not port select */
1214 pp_reg = VLV_PIPE_PP_CONTROL(pipe);
1215 panel_pipe = pipe;
ea0760cf
JB
1216 } else {
1217 pp_reg = PP_CONTROL;
bedd4dba
JN
1218 if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
1219 panel_pipe = PIPE_B;
ea0760cf
JB
1220 }
1221
1222 val = I915_READ(pp_reg);
1223 if (!(val & PANEL_POWER_ON) ||
ec49ba2d 1224 ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
ea0760cf
JB
1225 locked = false;
1226
e2c719b7 1227 I915_STATE_WARN(panel_pipe == pipe && locked,
ea0760cf 1228 "panel assertion failure, pipe %c regs locked\n",
9db4a9c7 1229 pipe_name(pipe));
ea0760cf
JB
1230}
1231
93ce0ba6
JN
1232static void assert_cursor(struct drm_i915_private *dev_priv,
1233 enum pipe pipe, bool state)
1234{
91c8a326 1235 struct drm_device *dev = &dev_priv->drm;
93ce0ba6
JN
1236 bool cur_state;
1237
d9d82081 1238 if (IS_845G(dev) || IS_I865G(dev))
0b87c24e 1239 cur_state = I915_READ(CURCNTR(PIPE_A)) & CURSOR_ENABLE;
d9d82081 1240 else
5efb3e28 1241 cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
93ce0ba6 1242
e2c719b7 1243 I915_STATE_WARN(cur_state != state,
93ce0ba6 1244 "cursor on pipe %c assertion failure (expected %s, current %s)\n",
87ad3212 1245 pipe_name(pipe), onoff(state), onoff(cur_state));
93ce0ba6
JN
1246}
1247#define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
1248#define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
1249
b840d907
JB
1250void assert_pipe(struct drm_i915_private *dev_priv,
1251 enum pipe pipe, bool state)
b24e7179 1252{
63d7bbe9 1253 bool cur_state;
702e7a56
PZ
1254 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1255 pipe);
4feed0eb 1256 enum intel_display_power_domain power_domain;
b24e7179 1257
b6b5d049
VS
1258 /* if we need the pipe quirk it must be always on */
1259 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1260 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
8e636784
DV
1261 state = true;
1262
4feed0eb
ID
1263 power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
1264 if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
649636ef 1265 u32 val = I915_READ(PIPECONF(cpu_transcoder));
69310161 1266 cur_state = !!(val & PIPECONF_ENABLE);
4feed0eb
ID
1267
1268 intel_display_power_put(dev_priv, power_domain);
1269 } else {
1270 cur_state = false;
69310161
PZ
1271 }
1272
e2c719b7 1273 I915_STATE_WARN(cur_state != state,
63d7bbe9 1274 "pipe %c assertion failure (expected %s, current %s)\n",
87ad3212 1275 pipe_name(pipe), onoff(state), onoff(cur_state));
b24e7179
JB
1276}
1277
931872fc
CW
1278static void assert_plane(struct drm_i915_private *dev_priv,
1279 enum plane plane, bool state)
b24e7179 1280{
b24e7179 1281 u32 val;
931872fc 1282 bool cur_state;
b24e7179 1283
649636ef 1284 val = I915_READ(DSPCNTR(plane));
931872fc 1285 cur_state = !!(val & DISPLAY_PLANE_ENABLE);
e2c719b7 1286 I915_STATE_WARN(cur_state != state,
931872fc 1287 "plane %c assertion failure (expected %s, current %s)\n",
87ad3212 1288 plane_name(plane), onoff(state), onoff(cur_state));
b24e7179
JB
1289}
1290
931872fc
CW
1291#define assert_plane_enabled(d, p) assert_plane(d, p, true)
1292#define assert_plane_disabled(d, p) assert_plane(d, p, false)
1293
b24e7179
JB
1294static void assert_planes_disabled(struct drm_i915_private *dev_priv,
1295 enum pipe pipe)
1296{
91c8a326 1297 struct drm_device *dev = &dev_priv->drm;
649636ef 1298 int i;
b24e7179 1299
653e1026
VS
1300 /* Primary planes are fixed to pipes on gen4+ */
1301 if (INTEL_INFO(dev)->gen >= 4) {
649636ef 1302 u32 val = I915_READ(DSPCNTR(pipe));
e2c719b7 1303 I915_STATE_WARN(val & DISPLAY_PLANE_ENABLE,
28c05794
AJ
1304 "plane %c assertion failure, should be disabled but not\n",
1305 plane_name(pipe));
19ec1358 1306 return;
28c05794 1307 }
19ec1358 1308
b24e7179 1309 /* Need to check both planes against the pipe */
055e393f 1310 for_each_pipe(dev_priv, i) {
649636ef
VS
1311 u32 val = I915_READ(DSPCNTR(i));
1312 enum pipe cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
b24e7179 1313 DISPPLANE_SEL_PIPE_SHIFT;
e2c719b7 1314 I915_STATE_WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
9db4a9c7
JB
1315 "plane %c assertion failure, should be off on pipe %c but is still active\n",
1316 plane_name(i), pipe_name(pipe));
b24e7179
JB
1317 }
1318}
1319
19332d7a
JB
1320static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
1321 enum pipe pipe)
1322{
91c8a326 1323 struct drm_device *dev = &dev_priv->drm;
649636ef 1324 int sprite;
19332d7a 1325
7feb8b88 1326 if (INTEL_INFO(dev)->gen >= 9) {
3bdcfc0c 1327 for_each_sprite(dev_priv, pipe, sprite) {
649636ef 1328 u32 val = I915_READ(PLANE_CTL(pipe, sprite));
e2c719b7 1329 I915_STATE_WARN(val & PLANE_CTL_ENABLE,
7feb8b88
DL
1330 "plane %d assertion failure, should be off on pipe %c but is still active\n",
1331 sprite, pipe_name(pipe));
1332 }
666a4537 1333 } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
3bdcfc0c 1334 for_each_sprite(dev_priv, pipe, sprite) {
649636ef 1335 u32 val = I915_READ(SPCNTR(pipe, sprite));
e2c719b7 1336 I915_STATE_WARN(val & SP_ENABLE,
20674eef 1337 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
1fe47785 1338 sprite_name(pipe, sprite), pipe_name(pipe));
20674eef
VS
1339 }
1340 } else if (INTEL_INFO(dev)->gen >= 7) {
649636ef 1341 u32 val = I915_READ(SPRCTL(pipe));
e2c719b7 1342 I915_STATE_WARN(val & SPRITE_ENABLE,
06da8da2 1343 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
20674eef
VS
1344 plane_name(pipe), pipe_name(pipe));
1345 } else if (INTEL_INFO(dev)->gen >= 5) {
649636ef 1346 u32 val = I915_READ(DVSCNTR(pipe));
e2c719b7 1347 I915_STATE_WARN(val & DVS_ENABLE,
06da8da2 1348 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
20674eef 1349 plane_name(pipe), pipe_name(pipe));
19332d7a
JB
1350 }
1351}
1352
08c71e5e
VS
1353static void assert_vblank_disabled(struct drm_crtc *crtc)
1354{
e2c719b7 1355 if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
08c71e5e
VS
1356 drm_crtc_vblank_put(crtc);
1357}
1358
7abd4b35
ACO
1359void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
1360 enum pipe pipe)
92f2584a 1361{
92f2584a
JB
1362 u32 val;
1363 bool enabled;
1364
649636ef 1365 val = I915_READ(PCH_TRANSCONF(pipe));
92f2584a 1366 enabled = !!(val & TRANS_ENABLE);
e2c719b7 1367 I915_STATE_WARN(enabled,
9db4a9c7
JB
1368 "transcoder assertion failed, should be off on pipe %c but is still active\n",
1369 pipe_name(pipe));
92f2584a
JB
1370}
1371
4e634389
KP
1372static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
1373 enum pipe pipe, u32 port_sel, u32 val)
f0575e92
KP
1374{
1375 if ((val & DP_PORT_EN) == 0)
1376 return false;
1377
2d1fe073 1378 if (HAS_PCH_CPT(dev_priv)) {
f0f59a00 1379 u32 trans_dp_ctl = I915_READ(TRANS_DP_CTL(pipe));
f0575e92
KP
1380 if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
1381 return false;
2d1fe073 1382 } else if (IS_CHERRYVIEW(dev_priv)) {
44f37d1f
CML
1383 if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
1384 return false;
f0575e92
KP
1385 } else {
1386 if ((val & DP_PIPE_MASK) != (pipe << 30))
1387 return false;
1388 }
1389 return true;
1390}
1391
1519b995
KP
1392static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
1393 enum pipe pipe, u32 val)
1394{
dc0fa718 1395 if ((val & SDVO_ENABLE) == 0)
1519b995
KP
1396 return false;
1397
2d1fe073 1398 if (HAS_PCH_CPT(dev_priv)) {
dc0fa718 1399 if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
1519b995 1400 return false;
2d1fe073 1401 } else if (IS_CHERRYVIEW(dev_priv)) {
44f37d1f
CML
1402 if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
1403 return false;
1519b995 1404 } else {
dc0fa718 1405 if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
1519b995
KP
1406 return false;
1407 }
1408 return true;
1409}
1410
1411static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
1412 enum pipe pipe, u32 val)
1413{
1414 if ((val & LVDS_PORT_EN) == 0)
1415 return false;
1416
2d1fe073 1417 if (HAS_PCH_CPT(dev_priv)) {
1519b995
KP
1418 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1419 return false;
1420 } else {
1421 if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
1422 return false;
1423 }
1424 return true;
1425}
1426
1427static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
1428 enum pipe pipe, u32 val)
1429{
1430 if ((val & ADPA_DAC_ENABLE) == 0)
1431 return false;
2d1fe073 1432 if (HAS_PCH_CPT(dev_priv)) {
1519b995
KP
1433 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1434 return false;
1435 } else {
1436 if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
1437 return false;
1438 }
1439 return true;
1440}
1441
291906f1 1442static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
f0f59a00
VS
1443 enum pipe pipe, i915_reg_t reg,
1444 u32 port_sel)
291906f1 1445{
47a05eca 1446 u32 val = I915_READ(reg);
e2c719b7 1447 I915_STATE_WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
291906f1 1448 "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
f0f59a00 1449 i915_mmio_reg_offset(reg), pipe_name(pipe));
de9a35ab 1450
2d1fe073 1451 I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && (val & DP_PORT_EN) == 0
75c5da27 1452 && (val & DP_PIPEB_SELECT),
de9a35ab 1453 "IBX PCH dp port still using transcoder B\n");
291906f1
JB
1454}
1455
1456static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
f0f59a00 1457 enum pipe pipe, i915_reg_t reg)
291906f1 1458{
47a05eca 1459 u32 val = I915_READ(reg);
e2c719b7 1460 I915_STATE_WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
23c99e77 1461 "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
f0f59a00 1462 i915_mmio_reg_offset(reg), pipe_name(pipe));
de9a35ab 1463
2d1fe073 1464 I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && (val & SDVO_ENABLE) == 0
75c5da27 1465 && (val & SDVO_PIPE_B_SELECT),
de9a35ab 1466 "IBX PCH hdmi port still using transcoder B\n");
291906f1
JB
1467}
1468
1469static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
1470 enum pipe pipe)
1471{
291906f1 1472 u32 val;
291906f1 1473
f0575e92
KP
1474 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
1475 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
1476 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
291906f1 1477
649636ef 1478 val = I915_READ(PCH_ADPA);
e2c719b7 1479 I915_STATE_WARN(adpa_pipe_enabled(dev_priv, pipe, val),
291906f1 1480 "PCH VGA enabled on transcoder %c, should be disabled\n",
9db4a9c7 1481 pipe_name(pipe));
291906f1 1482
649636ef 1483 val = I915_READ(PCH_LVDS);
e2c719b7 1484 I915_STATE_WARN(lvds_pipe_enabled(dev_priv, pipe, val),
291906f1 1485 "PCH LVDS enabled on transcoder %c, should be disabled\n",
9db4a9c7 1486 pipe_name(pipe));
291906f1 1487
e2debe91
PZ
1488 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
1489 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
1490 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
291906f1
JB
1491}
1492
cd2d34d9
VS
1493static void _vlv_enable_pll(struct intel_crtc *crtc,
1494 const struct intel_crtc_state *pipe_config)
1495{
1496 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1497 enum pipe pipe = crtc->pipe;
1498
1499 I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
1500 POSTING_READ(DPLL(pipe));
1501 udelay(150);
1502
2c30b43b
CW
1503 if (intel_wait_for_register(dev_priv,
1504 DPLL(pipe),
1505 DPLL_LOCK_VLV,
1506 DPLL_LOCK_VLV,
1507 1))
cd2d34d9
VS
1508 DRM_ERROR("DPLL %d failed to lock\n", pipe);
1509}
1510
d288f65f 1511static void vlv_enable_pll(struct intel_crtc *crtc,
5cec258b 1512 const struct intel_crtc_state *pipe_config)
87442f73 1513{
cd2d34d9 1514 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
8bd3f301 1515 enum pipe pipe = crtc->pipe;
87442f73 1516
8bd3f301 1517 assert_pipe_disabled(dev_priv, pipe);
87442f73 1518
87442f73 1519 /* PLL is protected by panel, make sure we can write it */
7d1a83cb 1520 assert_panel_unlocked(dev_priv, pipe);
87442f73 1521
cd2d34d9
VS
1522 if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
1523 _vlv_enable_pll(crtc, pipe_config);
426115cf 1524
8bd3f301
VS
1525 I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
1526 POSTING_READ(DPLL_MD(pipe));
87442f73
DV
1527}
1528
cd2d34d9
VS
1529
1530static void _chv_enable_pll(struct intel_crtc *crtc,
1531 const struct intel_crtc_state *pipe_config)
9d556c99 1532{
cd2d34d9 1533 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
8bd3f301 1534 enum pipe pipe = crtc->pipe;
9d556c99 1535 enum dpio_channel port = vlv_pipe_to_channel(pipe);
9d556c99
CML
1536 u32 tmp;
1537
a580516d 1538 mutex_lock(&dev_priv->sb_lock);
9d556c99
CML
1539
1540 /* Enable back the 10bit clock to display controller */
1541 tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1542 tmp |= DPIO_DCLKP_EN;
1543 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
1544
54433e91
VS
1545 mutex_unlock(&dev_priv->sb_lock);
1546
9d556c99
CML
1547 /*
1548 * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
1549 */
1550 udelay(1);
1551
1552 /* Enable PLL */
d288f65f 1553 I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
9d556c99
CML
1554
1555 /* Check PLL is locked */
6b18826a
CW
1556 if (intel_wait_for_register(dev_priv,
1557 DPLL(pipe), DPLL_LOCK_VLV, DPLL_LOCK_VLV,
1558 1))
9d556c99 1559 DRM_ERROR("PLL %d failed to lock\n", pipe);
cd2d34d9
VS
1560}
1561
1562static void chv_enable_pll(struct intel_crtc *crtc,
1563 const struct intel_crtc_state *pipe_config)
1564{
1565 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1566 enum pipe pipe = crtc->pipe;
1567
1568 assert_pipe_disabled(dev_priv, pipe);
1569
1570 /* PLL is protected by panel, make sure we can write it */
1571 assert_panel_unlocked(dev_priv, pipe);
1572
1573 if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
1574 _chv_enable_pll(crtc, pipe_config);
9d556c99 1575
c231775c
VS
1576 if (pipe != PIPE_A) {
1577 /*
1578 * WaPixelRepeatModeFixForC0:chv
1579 *
1580 * DPLLCMD is AWOL. Use chicken bits to propagate
1581 * the value from DPLLBMD to either pipe B or C.
1582 */
1583 I915_WRITE(CBR4_VLV, pipe == PIPE_B ? CBR_DPLLBMD_PIPE_B : CBR_DPLLBMD_PIPE_C);
1584 I915_WRITE(DPLL_MD(PIPE_B), pipe_config->dpll_hw_state.dpll_md);
1585 I915_WRITE(CBR4_VLV, 0);
1586 dev_priv->chv_dpll_md[pipe] = pipe_config->dpll_hw_state.dpll_md;
1587
1588 /*
1589 * DPLLB VGA mode also seems to cause problems.
1590 * We should always have it disabled.
1591 */
1592 WARN_ON((I915_READ(DPLL(PIPE_B)) & DPLL_VGA_MODE_DIS) == 0);
1593 } else {
1594 I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
1595 POSTING_READ(DPLL_MD(pipe));
1596 }
9d556c99
CML
1597}
1598
1c4e0274
VS
1599static int intel_num_dvo_pipes(struct drm_device *dev)
1600{
1601 struct intel_crtc *crtc;
1602 int count = 0;
1603
2d84d2b3 1604 for_each_intel_crtc(dev, crtc) {
3538b9df 1605 count += crtc->base.state->active &&
2d84d2b3
VS
1606 intel_crtc_has_type(crtc->config, INTEL_OUTPUT_DVO);
1607 }
1c4e0274
VS
1608
1609 return count;
1610}
1611
66e3d5c0 1612static void i9xx_enable_pll(struct intel_crtc *crtc)
63d7bbe9 1613{
66e3d5c0 1614 struct drm_device *dev = crtc->base.dev;
fac5e23e 1615 struct drm_i915_private *dev_priv = to_i915(dev);
f0f59a00 1616 i915_reg_t reg = DPLL(crtc->pipe);
6e3c9717 1617 u32 dpll = crtc->config->dpll_hw_state.dpll;
63d7bbe9 1618
66e3d5c0 1619 assert_pipe_disabled(dev_priv, crtc->pipe);
58c6eaa2 1620
63d7bbe9 1621 /* PLL is protected by panel, make sure we can write it */
66e3d5c0
DV
1622 if (IS_MOBILE(dev) && !IS_I830(dev))
1623 assert_panel_unlocked(dev_priv, crtc->pipe);
63d7bbe9 1624
1c4e0274
VS
1625 /* Enable DVO 2x clock on both PLLs if necessary */
1626 if (IS_I830(dev) && intel_num_dvo_pipes(dev) > 0) {
1627 /*
1628 * It appears to be important that we don't enable this
1629 * for the current pipe before otherwise configuring the
1630 * PLL. No idea how this should be handled if multiple
1631 * DVO outputs are enabled simultaneosly.
1632 */
1633 dpll |= DPLL_DVO_2X_MODE;
1634 I915_WRITE(DPLL(!crtc->pipe),
1635 I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
1636 }
66e3d5c0 1637
c2b63374
VS
1638 /*
1639 * Apparently we need to have VGA mode enabled prior to changing
1640 * the P1/P2 dividers. Otherwise the DPLL will keep using the old
1641 * dividers, even though the register value does change.
1642 */
1643 I915_WRITE(reg, 0);
1644
8e7a65aa
VS
1645 I915_WRITE(reg, dpll);
1646
66e3d5c0
DV
1647 /* Wait for the clocks to stabilize. */
1648 POSTING_READ(reg);
1649 udelay(150);
1650
1651 if (INTEL_INFO(dev)->gen >= 4) {
1652 I915_WRITE(DPLL_MD(crtc->pipe),
6e3c9717 1653 crtc->config->dpll_hw_state.dpll_md);
66e3d5c0
DV
1654 } else {
1655 /* The pixel multiplier can only be updated once the
1656 * DPLL is enabled and the clocks are stable.
1657 *
1658 * So write it again.
1659 */
1660 I915_WRITE(reg, dpll);
1661 }
63d7bbe9
JB
1662
1663 /* We do this three times for luck */
66e3d5c0 1664 I915_WRITE(reg, dpll);
63d7bbe9
JB
1665 POSTING_READ(reg);
1666 udelay(150); /* wait for warmup */
66e3d5c0 1667 I915_WRITE(reg, dpll);
63d7bbe9
JB
1668 POSTING_READ(reg);
1669 udelay(150); /* wait for warmup */
66e3d5c0 1670 I915_WRITE(reg, dpll);
63d7bbe9
JB
1671 POSTING_READ(reg);
1672 udelay(150); /* wait for warmup */
1673}
1674
1675/**
50b44a44 1676 * i9xx_disable_pll - disable a PLL
63d7bbe9
JB
1677 * @dev_priv: i915 private structure
1678 * @pipe: pipe PLL to disable
1679 *
1680 * Disable the PLL for @pipe, making sure the pipe is off first.
1681 *
1682 * Note! This is for pre-ILK only.
1683 */
1c4e0274 1684static void i9xx_disable_pll(struct intel_crtc *crtc)
63d7bbe9 1685{
1c4e0274 1686 struct drm_device *dev = crtc->base.dev;
fac5e23e 1687 struct drm_i915_private *dev_priv = to_i915(dev);
1c4e0274
VS
1688 enum pipe pipe = crtc->pipe;
1689
1690 /* Disable DVO 2x clock on both PLLs if necessary */
1691 if (IS_I830(dev) &&
2d84d2b3 1692 intel_crtc_has_type(crtc->config, INTEL_OUTPUT_DVO) &&
3538b9df 1693 !intel_num_dvo_pipes(dev)) {
1c4e0274
VS
1694 I915_WRITE(DPLL(PIPE_B),
1695 I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
1696 I915_WRITE(DPLL(PIPE_A),
1697 I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
1698 }
1699
b6b5d049
VS
1700 /* Don't disable pipe or pipe PLLs if needed */
1701 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1702 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
63d7bbe9
JB
1703 return;
1704
1705 /* Make sure the pipe isn't still relying on us */
1706 assert_pipe_disabled(dev_priv, pipe);
1707
b8afb911 1708 I915_WRITE(DPLL(pipe), DPLL_VGA_MODE_DIS);
50b44a44 1709 POSTING_READ(DPLL(pipe));
63d7bbe9
JB
1710}
1711
f6071166
JB
1712static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1713{
b8afb911 1714 u32 val;
f6071166
JB
1715
1716 /* Make sure the pipe isn't still relying on us */
1717 assert_pipe_disabled(dev_priv, pipe);
1718
03ed5cbf
VS
1719 val = DPLL_INTEGRATED_REF_CLK_VLV |
1720 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
1721 if (pipe != PIPE_A)
1722 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
1723
f6071166
JB
1724 I915_WRITE(DPLL(pipe), val);
1725 POSTING_READ(DPLL(pipe));
076ed3b2
CML
1726}
1727
1728static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1729{
d752048d 1730 enum dpio_channel port = vlv_pipe_to_channel(pipe);
076ed3b2
CML
1731 u32 val;
1732
a11b0703
VS
1733 /* Make sure the pipe isn't still relying on us */
1734 assert_pipe_disabled(dev_priv, pipe);
076ed3b2 1735
60bfe44f
VS
1736 val = DPLL_SSC_REF_CLK_CHV |
1737 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
a11b0703
VS
1738 if (pipe != PIPE_A)
1739 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
03ed5cbf 1740
a11b0703
VS
1741 I915_WRITE(DPLL(pipe), val);
1742 POSTING_READ(DPLL(pipe));
d752048d 1743
a580516d 1744 mutex_lock(&dev_priv->sb_lock);
d752048d
VS
1745
1746 /* Disable 10bit clock to display controller */
1747 val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1748 val &= ~DPIO_DCLKP_EN;
1749 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
1750
a580516d 1751 mutex_unlock(&dev_priv->sb_lock);
f6071166
JB
1752}
1753
e4607fcf 1754void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
9b6de0a1
VS
1755 struct intel_digital_port *dport,
1756 unsigned int expected_mask)
89b667f8
JB
1757{
1758 u32 port_mask;
f0f59a00 1759 i915_reg_t dpll_reg;
89b667f8 1760
e4607fcf
CML
1761 switch (dport->port) {
1762 case PORT_B:
89b667f8 1763 port_mask = DPLL_PORTB_READY_MASK;
00fc31b7 1764 dpll_reg = DPLL(0);
e4607fcf
CML
1765 break;
1766 case PORT_C:
89b667f8 1767 port_mask = DPLL_PORTC_READY_MASK;
00fc31b7 1768 dpll_reg = DPLL(0);
9b6de0a1 1769 expected_mask <<= 4;
00fc31b7
CML
1770 break;
1771 case PORT_D:
1772 port_mask = DPLL_PORTD_READY_MASK;
1773 dpll_reg = DPIO_PHY_STATUS;
e4607fcf
CML
1774 break;
1775 default:
1776 BUG();
1777 }
89b667f8 1778
370004d3
CW
1779 if (intel_wait_for_register(dev_priv,
1780 dpll_reg, port_mask, expected_mask,
1781 1000))
9b6de0a1
VS
1782 WARN(1, "timed out waiting for port %c ready: got 0x%x, expected 0x%x\n",
1783 port_name(dport->port), I915_READ(dpll_reg) & port_mask, expected_mask);
89b667f8
JB
1784}
1785
b8a4f404
PZ
1786static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1787 enum pipe pipe)
040484af 1788{
91c8a326 1789 struct drm_device *dev = &dev_priv->drm;
7c26e5c6 1790 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
e2b78267 1791 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
f0f59a00
VS
1792 i915_reg_t reg;
1793 uint32_t val, pipeconf_val;
040484af 1794
040484af 1795 /* Make sure PCH DPLL is enabled */
8106ddbd 1796 assert_shared_dpll_enabled(dev_priv, intel_crtc->config->shared_dpll);
040484af
JB
1797
1798 /* FDI must be feeding us bits for PCH ports */
1799 assert_fdi_tx_enabled(dev_priv, pipe);
1800 assert_fdi_rx_enabled(dev_priv, pipe);
1801
23670b32
DV
1802 if (HAS_PCH_CPT(dev)) {
1803 /* Workaround: Set the timing override bit before enabling the
1804 * pch transcoder. */
1805 reg = TRANS_CHICKEN2(pipe);
1806 val = I915_READ(reg);
1807 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
1808 I915_WRITE(reg, val);
59c859d6 1809 }
23670b32 1810
ab9412ba 1811 reg = PCH_TRANSCONF(pipe);
040484af 1812 val = I915_READ(reg);
5f7f726d 1813 pipeconf_val = I915_READ(PIPECONF(pipe));
e9bcff5c 1814
2d1fe073 1815 if (HAS_PCH_IBX(dev_priv)) {
e9bcff5c 1816 /*
c5de7c6f
VS
1817 * Make the BPC in transcoder be consistent with
1818 * that in pipeconf reg. For HDMI we must use 8bpc
1819 * here for both 8bpc and 12bpc.
e9bcff5c 1820 */
dfd07d72 1821 val &= ~PIPECONF_BPC_MASK;
2d84d2b3 1822 if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_HDMI))
c5de7c6f
VS
1823 val |= PIPECONF_8BPC;
1824 else
1825 val |= pipeconf_val & PIPECONF_BPC_MASK;
e9bcff5c 1826 }
5f7f726d
PZ
1827
1828 val &= ~TRANS_INTERLACE_MASK;
1829 if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
2d1fe073 1830 if (HAS_PCH_IBX(dev_priv) &&
2d84d2b3 1831 intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
7c26e5c6
PZ
1832 val |= TRANS_LEGACY_INTERLACED_ILK;
1833 else
1834 val |= TRANS_INTERLACED;
5f7f726d
PZ
1835 else
1836 val |= TRANS_PROGRESSIVE;
1837
040484af 1838 I915_WRITE(reg, val | TRANS_ENABLE);
650fbd84
CW
1839 if (intel_wait_for_register(dev_priv,
1840 reg, TRANS_STATE_ENABLE, TRANS_STATE_ENABLE,
1841 100))
4bb6f1f3 1842 DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
040484af
JB
1843}
1844
8fb033d7 1845static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
937bb610 1846 enum transcoder cpu_transcoder)
040484af 1847{
8fb033d7 1848 u32 val, pipeconf_val;
8fb033d7 1849
8fb033d7 1850 /* FDI must be feeding us bits for PCH ports */
1a240d4d 1851 assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
937bb610 1852 assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
8fb033d7 1853
223a6fdf 1854 /* Workaround: set timing override bit. */
36c0d0cf 1855 val = I915_READ(TRANS_CHICKEN2(PIPE_A));
23670b32 1856 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
36c0d0cf 1857 I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
223a6fdf 1858
25f3ef11 1859 val = TRANS_ENABLE;
937bb610 1860 pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
8fb033d7 1861
9a76b1c6
PZ
1862 if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
1863 PIPECONF_INTERLACED_ILK)
a35f2679 1864 val |= TRANS_INTERLACED;
8fb033d7
PZ
1865 else
1866 val |= TRANS_PROGRESSIVE;
1867
ab9412ba 1868 I915_WRITE(LPT_TRANSCONF, val);
d9f96244
CW
1869 if (intel_wait_for_register(dev_priv,
1870 LPT_TRANSCONF,
1871 TRANS_STATE_ENABLE,
1872 TRANS_STATE_ENABLE,
1873 100))
937bb610 1874 DRM_ERROR("Failed to enable PCH transcoder\n");
8fb033d7
PZ
1875}
1876
b8a4f404
PZ
1877static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
1878 enum pipe pipe)
040484af 1879{
91c8a326 1880 struct drm_device *dev = &dev_priv->drm;
f0f59a00
VS
1881 i915_reg_t reg;
1882 uint32_t val;
040484af
JB
1883
1884 /* FDI relies on the transcoder */
1885 assert_fdi_tx_disabled(dev_priv, pipe);
1886 assert_fdi_rx_disabled(dev_priv, pipe);
1887
291906f1
JB
1888 /* Ports must be off as well */
1889 assert_pch_ports_disabled(dev_priv, pipe);
1890
ab9412ba 1891 reg = PCH_TRANSCONF(pipe);
040484af
JB
1892 val = I915_READ(reg);
1893 val &= ~TRANS_ENABLE;
1894 I915_WRITE(reg, val);
1895 /* wait for PCH transcoder off, transcoder state */
a7d04662
CW
1896 if (intel_wait_for_register(dev_priv,
1897 reg, TRANS_STATE_ENABLE, 0,
1898 50))
4bb6f1f3 1899 DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
23670b32 1900
c465613b 1901 if (HAS_PCH_CPT(dev)) {
23670b32
DV
1902 /* Workaround: Clear the timing override chicken bit again. */
1903 reg = TRANS_CHICKEN2(pipe);
1904 val = I915_READ(reg);
1905 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
1906 I915_WRITE(reg, val);
1907 }
040484af
JB
1908}
1909
ab4d966c 1910static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
8fb033d7 1911{
8fb033d7
PZ
1912 u32 val;
1913
ab9412ba 1914 val = I915_READ(LPT_TRANSCONF);
8fb033d7 1915 val &= ~TRANS_ENABLE;
ab9412ba 1916 I915_WRITE(LPT_TRANSCONF, val);
8fb033d7 1917 /* wait for PCH transcoder off, transcoder state */
dfdb4749
CW
1918 if (intel_wait_for_register(dev_priv,
1919 LPT_TRANSCONF, TRANS_STATE_ENABLE, 0,
1920 50))
8a52fd9f 1921 DRM_ERROR("Failed to disable PCH transcoder\n");
223a6fdf
PZ
1922
1923 /* Workaround: clear timing override bit. */
36c0d0cf 1924 val = I915_READ(TRANS_CHICKEN2(PIPE_A));
23670b32 1925 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
36c0d0cf 1926 I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
040484af
JB
1927}
1928
b24e7179 1929/**
309cfea8 1930 * intel_enable_pipe - enable a pipe, asserting requirements
0372264a 1931 * @crtc: crtc responsible for the pipe
b24e7179 1932 *
0372264a 1933 * Enable @crtc's pipe, making sure that various hardware specific requirements
b24e7179 1934 * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
b24e7179 1935 */
e1fdc473 1936static void intel_enable_pipe(struct intel_crtc *crtc)
b24e7179 1937{
0372264a 1938 struct drm_device *dev = crtc->base.dev;
fac5e23e 1939 struct drm_i915_private *dev_priv = to_i915(dev);
0372264a 1940 enum pipe pipe = crtc->pipe;
1a70a728 1941 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
1a240d4d 1942 enum pipe pch_transcoder;
f0f59a00 1943 i915_reg_t reg;
b24e7179
JB
1944 u32 val;
1945
9e2ee2dd
VS
1946 DRM_DEBUG_KMS("enabling pipe %c\n", pipe_name(pipe));
1947
58c6eaa2 1948 assert_planes_disabled(dev_priv, pipe);
93ce0ba6 1949 assert_cursor_disabled(dev_priv, pipe);
58c6eaa2
DV
1950 assert_sprites_disabled(dev_priv, pipe);
1951
2d1fe073 1952 if (HAS_PCH_LPT(dev_priv))
cc391bbb
PZ
1953 pch_transcoder = TRANSCODER_A;
1954 else
1955 pch_transcoder = pipe;
1956
b24e7179
JB
1957 /*
1958 * A pipe without a PLL won't actually be able to drive bits from
1959 * a plane. On ILK+ the pipe PLLs are integrated, so we don't
1960 * need the check.
1961 */
2d1fe073 1962 if (HAS_GMCH_DISPLAY(dev_priv))
d7edc4e5 1963 if (intel_crtc_has_type(crtc->config, INTEL_OUTPUT_DSI))
23538ef1
JN
1964 assert_dsi_pll_enabled(dev_priv);
1965 else
1966 assert_pll_enabled(dev_priv, pipe);
040484af 1967 else {
6e3c9717 1968 if (crtc->config->has_pch_encoder) {
040484af 1969 /* if driving the PCH, we need FDI enabled */
cc391bbb 1970 assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
1a240d4d
DV
1971 assert_fdi_tx_pll_enabled(dev_priv,
1972 (enum pipe) cpu_transcoder);
040484af
JB
1973 }
1974 /* FIXME: assert CPU port conditions for SNB+ */
1975 }
b24e7179 1976
702e7a56 1977 reg = PIPECONF(cpu_transcoder);
b24e7179 1978 val = I915_READ(reg);
7ad25d48 1979 if (val & PIPECONF_ENABLE) {
b6b5d049
VS
1980 WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1981 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
00d70b15 1982 return;
7ad25d48 1983 }
00d70b15
CW
1984
1985 I915_WRITE(reg, val | PIPECONF_ENABLE);
851855d8 1986 POSTING_READ(reg);
b7792d8b
VS
1987
1988 /*
1989 * Until the pipe starts DSL will read as 0, which would cause
1990 * an apparent vblank timestamp jump, which messes up also the
1991 * frame count when it's derived from the timestamps. So let's
1992 * wait for the pipe to start properly before we call
1993 * drm_crtc_vblank_on()
1994 */
1995 if (dev->max_vblank_count == 0 &&
1996 wait_for(intel_get_crtc_scanline(crtc) != crtc->scanline_offset, 50))
1997 DRM_ERROR("pipe %c didn't start\n", pipe_name(pipe));
b24e7179
JB
1998}
1999
2000/**
309cfea8 2001 * intel_disable_pipe - disable a pipe, asserting requirements
575f7ab7 2002 * @crtc: crtc whose pipes is to be disabled
b24e7179 2003 *
575f7ab7
VS
2004 * Disable the pipe of @crtc, making sure that various hardware
2005 * specific requirements are met, if applicable, e.g. plane
2006 * disabled, panel fitter off, etc.
b24e7179
JB
2007 *
2008 * Will wait until the pipe has shut down before returning.
2009 */
575f7ab7 2010static void intel_disable_pipe(struct intel_crtc *crtc)
b24e7179 2011{
fac5e23e 2012 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6e3c9717 2013 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
575f7ab7 2014 enum pipe pipe = crtc->pipe;
f0f59a00 2015 i915_reg_t reg;
b24e7179
JB
2016 u32 val;
2017
9e2ee2dd
VS
2018 DRM_DEBUG_KMS("disabling pipe %c\n", pipe_name(pipe));
2019
b24e7179
JB
2020 /*
2021 * Make sure planes won't keep trying to pump pixels to us,
2022 * or we might hang the display.
2023 */
2024 assert_planes_disabled(dev_priv, pipe);
93ce0ba6 2025 assert_cursor_disabled(dev_priv, pipe);
19332d7a 2026 assert_sprites_disabled(dev_priv, pipe);
b24e7179 2027
702e7a56 2028 reg = PIPECONF(cpu_transcoder);
b24e7179 2029 val = I915_READ(reg);
00d70b15
CW
2030 if ((val & PIPECONF_ENABLE) == 0)
2031 return;
2032
67adc644
VS
2033 /*
2034 * Double wide has implications for planes
2035 * so best keep it disabled when not needed.
2036 */
6e3c9717 2037 if (crtc->config->double_wide)
67adc644
VS
2038 val &= ~PIPECONF_DOUBLE_WIDE;
2039
2040 /* Don't disable pipe or pipe PLLs if needed */
b6b5d049
VS
2041 if (!(pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) &&
2042 !(pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
67adc644
VS
2043 val &= ~PIPECONF_ENABLE;
2044
2045 I915_WRITE(reg, val);
2046 if ((val & PIPECONF_ENABLE) == 0)
2047 intel_wait_for_pipe_off(crtc);
b24e7179
JB
2048}
2049
832be82f
VS
2050static unsigned int intel_tile_size(const struct drm_i915_private *dev_priv)
2051{
2052 return IS_GEN2(dev_priv) ? 2048 : 4096;
2053}
2054
27ba3910
VS
2055static unsigned int intel_tile_width_bytes(const struct drm_i915_private *dev_priv,
2056 uint64_t fb_modifier, unsigned int cpp)
7b49f948
VS
2057{
2058 switch (fb_modifier) {
2059 case DRM_FORMAT_MOD_NONE:
2060 return cpp;
2061 case I915_FORMAT_MOD_X_TILED:
2062 if (IS_GEN2(dev_priv))
2063 return 128;
2064 else
2065 return 512;
2066 case I915_FORMAT_MOD_Y_TILED:
2067 if (IS_GEN2(dev_priv) || HAS_128_BYTE_Y_TILING(dev_priv))
2068 return 128;
2069 else
2070 return 512;
2071 case I915_FORMAT_MOD_Yf_TILED:
2072 switch (cpp) {
2073 case 1:
2074 return 64;
2075 case 2:
2076 case 4:
2077 return 128;
2078 case 8:
2079 case 16:
2080 return 256;
2081 default:
2082 MISSING_CASE(cpp);
2083 return cpp;
2084 }
2085 break;
2086 default:
2087 MISSING_CASE(fb_modifier);
2088 return cpp;
2089 }
2090}
2091
832be82f
VS
2092unsigned int intel_tile_height(const struct drm_i915_private *dev_priv,
2093 uint64_t fb_modifier, unsigned int cpp)
a57ce0b2 2094{
832be82f
VS
2095 if (fb_modifier == DRM_FORMAT_MOD_NONE)
2096 return 1;
2097 else
2098 return intel_tile_size(dev_priv) /
27ba3910 2099 intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
6761dd31
TU
2100}
2101
8d0deca8
VS
2102/* Return the tile dimensions in pixel units */
2103static void intel_tile_dims(const struct drm_i915_private *dev_priv,
2104 unsigned int *tile_width,
2105 unsigned int *tile_height,
2106 uint64_t fb_modifier,
2107 unsigned int cpp)
2108{
2109 unsigned int tile_width_bytes =
2110 intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
2111
2112 *tile_width = tile_width_bytes / cpp;
2113 *tile_height = intel_tile_size(dev_priv) / tile_width_bytes;
2114}
2115
6761dd31
TU
2116unsigned int
2117intel_fb_align_height(struct drm_device *dev, unsigned int height,
832be82f 2118 uint32_t pixel_format, uint64_t fb_modifier)
6761dd31 2119{
832be82f
VS
2120 unsigned int cpp = drm_format_plane_cpp(pixel_format, 0);
2121 unsigned int tile_height = intel_tile_height(to_i915(dev), fb_modifier, cpp);
2122
2123 return ALIGN(height, tile_height);
a57ce0b2
JB
2124}
2125
1663b9d6
VS
2126unsigned int intel_rotation_info_size(const struct intel_rotation_info *rot_info)
2127{
2128 unsigned int size = 0;
2129 int i;
2130
2131 for (i = 0 ; i < ARRAY_SIZE(rot_info->plane); i++)
2132 size += rot_info->plane[i].width * rot_info->plane[i].height;
2133
2134 return size;
2135}
2136
75c82a53 2137static void
3465c580
VS
2138intel_fill_fb_ggtt_view(struct i915_ggtt_view *view,
2139 const struct drm_framebuffer *fb,
2140 unsigned int rotation)
f64b98cd 2141{
2d7a215f
VS
2142 if (intel_rotation_90_or_270(rotation)) {
2143 *view = i915_ggtt_view_rotated;
2144 view->params.rotated = to_intel_framebuffer(fb)->rot_info;
2145 } else {
2146 *view = i915_ggtt_view_normal;
2147 }
2148}
50470bb0 2149
2d7a215f
VS
2150static void
2151intel_fill_fb_info(struct drm_i915_private *dev_priv,
2152 struct drm_framebuffer *fb)
2153{
2154 struct intel_rotation_info *info = &to_intel_framebuffer(fb)->rot_info;
2155 unsigned int tile_size, tile_width, tile_height, cpp;
50470bb0 2156
d9b3288e
VS
2157 tile_size = intel_tile_size(dev_priv);
2158
2159 cpp = drm_format_plane_cpp(fb->pixel_format, 0);
8d0deca8
VS
2160 intel_tile_dims(dev_priv, &tile_width, &tile_height,
2161 fb->modifier[0], cpp);
d9b3288e 2162
1663b9d6
VS
2163 info->plane[0].width = DIV_ROUND_UP(fb->pitches[0], tile_width * cpp);
2164 info->plane[0].height = DIV_ROUND_UP(fb->height, tile_height);
84fe03f7 2165
89e3e142 2166 if (info->pixel_format == DRM_FORMAT_NV12) {
832be82f 2167 cpp = drm_format_plane_cpp(fb->pixel_format, 1);
8d0deca8
VS
2168 intel_tile_dims(dev_priv, &tile_width, &tile_height,
2169 fb->modifier[1], cpp);
d9b3288e 2170
2d7a215f 2171 info->uv_offset = fb->offsets[1];
1663b9d6
VS
2172 info->plane[1].width = DIV_ROUND_UP(fb->pitches[1], tile_width * cpp);
2173 info->plane[1].height = DIV_ROUND_UP(fb->height / 2, tile_height);
89e3e142 2174 }
f64b98cd
TU
2175}
2176
603525d7 2177static unsigned int intel_linear_alignment(const struct drm_i915_private *dev_priv)
4e9a86b6
VS
2178{
2179 if (INTEL_INFO(dev_priv)->gen >= 9)
2180 return 256 * 1024;
985b8bb4 2181 else if (IS_BROADWATER(dev_priv) || IS_CRESTLINE(dev_priv) ||
666a4537 2182 IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
4e9a86b6
VS
2183 return 128 * 1024;
2184 else if (INTEL_INFO(dev_priv)->gen >= 4)
2185 return 4 * 1024;
2186 else
44c5905e 2187 return 0;
4e9a86b6
VS
2188}
2189
603525d7
VS
2190static unsigned int intel_surf_alignment(const struct drm_i915_private *dev_priv,
2191 uint64_t fb_modifier)
2192{
2193 switch (fb_modifier) {
2194 case DRM_FORMAT_MOD_NONE:
2195 return intel_linear_alignment(dev_priv);
2196 case I915_FORMAT_MOD_X_TILED:
2197 if (INTEL_INFO(dev_priv)->gen >= 9)
2198 return 256 * 1024;
2199 return 0;
2200 case I915_FORMAT_MOD_Y_TILED:
2201 case I915_FORMAT_MOD_Yf_TILED:
2202 return 1 * 1024 * 1024;
2203 default:
2204 MISSING_CASE(fb_modifier);
2205 return 0;
2206 }
2207}
2208
127bd2ac 2209int
3465c580
VS
2210intel_pin_and_fence_fb_obj(struct drm_framebuffer *fb,
2211 unsigned int rotation)
6b95a207 2212{
850c4cdc 2213 struct drm_device *dev = fb->dev;
fac5e23e 2214 struct drm_i915_private *dev_priv = to_i915(dev);
850c4cdc 2215 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
f64b98cd 2216 struct i915_ggtt_view view;
6b95a207
KH
2217 u32 alignment;
2218 int ret;
2219
ebcdd39e
MR
2220 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
2221
603525d7 2222 alignment = intel_surf_alignment(dev_priv, fb->modifier[0]);
6b95a207 2223
3465c580 2224 intel_fill_fb_ggtt_view(&view, fb, rotation);
f64b98cd 2225
693db184
CW
2226 /* Note that the w/a also requires 64 PTE of padding following the
2227 * bo. We currently fill all unused PTE with the shadow page and so
2228 * we should always have valid PTE following the scanout preventing
2229 * the VT-d warning.
2230 */
48f112fe 2231 if (intel_scanout_needs_vtd_wa(dev_priv) && alignment < 256 * 1024)
693db184
CW
2232 alignment = 256 * 1024;
2233
d6dd6843
PZ
2234 /*
2235 * Global gtt pte registers are special registers which actually forward
2236 * writes to a chunk of system memory. Which means that there is no risk
2237 * that the register values disappear as soon as we call
2238 * intel_runtime_pm_put(), so it is correct to wrap only the
2239 * pin/unpin/fence and not more.
2240 */
2241 intel_runtime_pm_get(dev_priv);
2242
7580d774
ML
2243 ret = i915_gem_object_pin_to_display_plane(obj, alignment,
2244 &view);
48b956c5 2245 if (ret)
b26a6b35 2246 goto err_pm;
6b95a207
KH
2247
2248 /* Install a fence for tiled scan-out. Pre-i965 always needs a
2249 * fence, whereas 965+ only requires a fence if using
2250 * framebuffer compression. For simplicity, we always install
2251 * a fence as the cost is not that onerous.
2252 */
9807216f
VK
2253 if (view.type == I915_GGTT_VIEW_NORMAL) {
2254 ret = i915_gem_object_get_fence(obj);
2255 if (ret == -EDEADLK) {
2256 /*
2257 * -EDEADLK means there are no free fences
2258 * no pending flips.
2259 *
2260 * This is propagated to atomic, but it uses
2261 * -EDEADLK to force a locking recovery, so
2262 * change the returned error to -EBUSY.
2263 */
2264 ret = -EBUSY;
2265 goto err_unpin;
2266 } else if (ret)
2267 goto err_unpin;
1690e1eb 2268
9807216f
VK
2269 i915_gem_object_pin_fence(obj);
2270 }
6b95a207 2271
d6dd6843 2272 intel_runtime_pm_put(dev_priv);
6b95a207 2273 return 0;
48b956c5
CW
2274
2275err_unpin:
f64b98cd 2276 i915_gem_object_unpin_from_display_plane(obj, &view);
b26a6b35 2277err_pm:
d6dd6843 2278 intel_runtime_pm_put(dev_priv);
48b956c5 2279 return ret;
6b95a207
KH
2280}
2281
fb4b8ce1 2282void intel_unpin_fb_obj(struct drm_framebuffer *fb, unsigned int rotation)
1690e1eb 2283{
82bc3b2d 2284 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
f64b98cd 2285 struct i915_ggtt_view view;
82bc3b2d 2286
ebcdd39e
MR
2287 WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
2288
3465c580 2289 intel_fill_fb_ggtt_view(&view, fb, rotation);
f64b98cd 2290
9807216f
VK
2291 if (view.type == I915_GGTT_VIEW_NORMAL)
2292 i915_gem_object_unpin_fence(obj);
2293
f64b98cd 2294 i915_gem_object_unpin_from_display_plane(obj, &view);
1690e1eb
CW
2295}
2296
29cf9491
VS
2297/*
2298 * Adjust the tile offset by moving the difference into
2299 * the x/y offsets.
2300 *
2301 * Input tile dimensions and pitch must already be
2302 * rotated to match x and y, and in pixel units.
2303 */
2304static u32 intel_adjust_tile_offset(int *x, int *y,
2305 unsigned int tile_width,
2306 unsigned int tile_height,
2307 unsigned int tile_size,
2308 unsigned int pitch_tiles,
2309 u32 old_offset,
2310 u32 new_offset)
2311{
2312 unsigned int tiles;
2313
2314 WARN_ON(old_offset & (tile_size - 1));
2315 WARN_ON(new_offset & (tile_size - 1));
2316 WARN_ON(new_offset > old_offset);
2317
2318 tiles = (old_offset - new_offset) / tile_size;
2319
2320 *y += tiles / pitch_tiles * tile_height;
2321 *x += tiles % pitch_tiles * tile_width;
2322
2323 return new_offset;
2324}
2325
8d0deca8
VS
2326/*
2327 * Computes the linear offset to the base tile and adjusts
2328 * x, y. bytes per pixel is assumed to be a power-of-two.
2329 *
2330 * In the 90/270 rotated case, x and y are assumed
2331 * to be already rotated to match the rotated GTT view, and
2332 * pitch is the tile_height aligned framebuffer height.
2333 */
4f2d9934
VS
2334u32 intel_compute_tile_offset(int *x, int *y,
2335 const struct drm_framebuffer *fb, int plane,
8d0deca8
VS
2336 unsigned int pitch,
2337 unsigned int rotation)
c2c75131 2338{
4f2d9934
VS
2339 const struct drm_i915_private *dev_priv = to_i915(fb->dev);
2340 uint64_t fb_modifier = fb->modifier[plane];
2341 unsigned int cpp = drm_format_plane_cpp(fb->pixel_format, plane);
29cf9491
VS
2342 u32 offset, offset_aligned, alignment;
2343
2344 alignment = intel_surf_alignment(dev_priv, fb_modifier);
2345 if (alignment)
2346 alignment--;
2347
b5c65338 2348 if (fb_modifier != DRM_FORMAT_MOD_NONE) {
8d0deca8
VS
2349 unsigned int tile_size, tile_width, tile_height;
2350 unsigned int tile_rows, tiles, pitch_tiles;
c2c75131 2351
d843310d 2352 tile_size = intel_tile_size(dev_priv);
8d0deca8
VS
2353 intel_tile_dims(dev_priv, &tile_width, &tile_height,
2354 fb_modifier, cpp);
2355
2356 if (intel_rotation_90_or_270(rotation)) {
2357 pitch_tiles = pitch / tile_height;
2358 swap(tile_width, tile_height);
2359 } else {
2360 pitch_tiles = pitch / (tile_width * cpp);
2361 }
d843310d
VS
2362
2363 tile_rows = *y / tile_height;
2364 *y %= tile_height;
c2c75131 2365
8d0deca8
VS
2366 tiles = *x / tile_width;
2367 *x %= tile_width;
bc752862 2368
29cf9491
VS
2369 offset = (tile_rows * pitch_tiles + tiles) * tile_size;
2370 offset_aligned = offset & ~alignment;
bc752862 2371
29cf9491
VS
2372 intel_adjust_tile_offset(x, y, tile_width, tile_height,
2373 tile_size, pitch_tiles,
2374 offset, offset_aligned);
2375 } else {
bc752862 2376 offset = *y * pitch + *x * cpp;
29cf9491
VS
2377 offset_aligned = offset & ~alignment;
2378
4e9a86b6
VS
2379 *y = (offset & alignment) / pitch;
2380 *x = ((offset & alignment) - *y * pitch) / cpp;
bc752862 2381 }
29cf9491
VS
2382
2383 return offset_aligned;
c2c75131
DV
2384}
2385
b35d63fa 2386static int i9xx_format_to_fourcc(int format)
46f297fb
JB
2387{
2388 switch (format) {
2389 case DISPPLANE_8BPP:
2390 return DRM_FORMAT_C8;
2391 case DISPPLANE_BGRX555:
2392 return DRM_FORMAT_XRGB1555;
2393 case DISPPLANE_BGRX565:
2394 return DRM_FORMAT_RGB565;
2395 default:
2396 case DISPPLANE_BGRX888:
2397 return DRM_FORMAT_XRGB8888;
2398 case DISPPLANE_RGBX888:
2399 return DRM_FORMAT_XBGR8888;
2400 case DISPPLANE_BGRX101010:
2401 return DRM_FORMAT_XRGB2101010;
2402 case DISPPLANE_RGBX101010:
2403 return DRM_FORMAT_XBGR2101010;
2404 }
2405}
2406
bc8d7dff
DL
2407static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
2408{
2409 switch (format) {
2410 case PLANE_CTL_FORMAT_RGB_565:
2411 return DRM_FORMAT_RGB565;
2412 default:
2413 case PLANE_CTL_FORMAT_XRGB_8888:
2414 if (rgb_order) {
2415 if (alpha)
2416 return DRM_FORMAT_ABGR8888;
2417 else
2418 return DRM_FORMAT_XBGR8888;
2419 } else {
2420 if (alpha)
2421 return DRM_FORMAT_ARGB8888;
2422 else
2423 return DRM_FORMAT_XRGB8888;
2424 }
2425 case PLANE_CTL_FORMAT_XRGB_2101010:
2426 if (rgb_order)
2427 return DRM_FORMAT_XBGR2101010;
2428 else
2429 return DRM_FORMAT_XRGB2101010;
2430 }
2431}
2432
5724dbd1 2433static bool
f6936e29
DV
2434intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
2435 struct intel_initial_plane_config *plane_config)
46f297fb
JB
2436{
2437 struct drm_device *dev = crtc->base.dev;
3badb49f 2438 struct drm_i915_private *dev_priv = to_i915(dev);
72e96d64 2439 struct i915_ggtt *ggtt = &dev_priv->ggtt;
46f297fb
JB
2440 struct drm_i915_gem_object *obj = NULL;
2441 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
2d14030b 2442 struct drm_framebuffer *fb = &plane_config->fb->base;
f37b5c2b
DV
2443 u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
2444 u32 size_aligned = round_up(plane_config->base + plane_config->size,
2445 PAGE_SIZE);
2446
2447 size_aligned -= base_aligned;
46f297fb 2448
ff2652ea
CW
2449 if (plane_config->size == 0)
2450 return false;
2451
3badb49f
PZ
2452 /* If the FB is too big, just don't use it since fbdev is not very
2453 * important and we should probably use that space with FBC or other
2454 * features. */
72e96d64 2455 if (size_aligned * 2 > ggtt->stolen_usable_size)
3badb49f
PZ
2456 return false;
2457
12c83d99
TU
2458 mutex_lock(&dev->struct_mutex);
2459
f37b5c2b
DV
2460 obj = i915_gem_object_create_stolen_for_preallocated(dev,
2461 base_aligned,
2462 base_aligned,
2463 size_aligned);
12c83d99
TU
2464 if (!obj) {
2465 mutex_unlock(&dev->struct_mutex);
484b41dd 2466 return false;
12c83d99 2467 }
46f297fb 2468
3e510a8e
CW
2469 if (plane_config->tiling == I915_TILING_X)
2470 obj->tiling_and_stride = fb->pitches[0] | I915_TILING_X;
46f297fb 2471
6bf129df
DL
2472 mode_cmd.pixel_format = fb->pixel_format;
2473 mode_cmd.width = fb->width;
2474 mode_cmd.height = fb->height;
2475 mode_cmd.pitches[0] = fb->pitches[0];
18c5247e
DV
2476 mode_cmd.modifier[0] = fb->modifier[0];
2477 mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
46f297fb 2478
6bf129df 2479 if (intel_framebuffer_init(dev, to_intel_framebuffer(fb),
484b41dd 2480 &mode_cmd, obj)) {
46f297fb
JB
2481 DRM_DEBUG_KMS("intel fb init failed\n");
2482 goto out_unref_obj;
2483 }
12c83d99 2484
46f297fb 2485 mutex_unlock(&dev->struct_mutex);
484b41dd 2486
f6936e29 2487 DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
484b41dd 2488 return true;
46f297fb
JB
2489
2490out_unref_obj:
f8c417cd 2491 i915_gem_object_put(obj);
46f297fb 2492 mutex_unlock(&dev->struct_mutex);
484b41dd
JB
2493 return false;
2494}
2495
5a21b665
DV
2496/* Update plane->state->fb to match plane->fb after driver-internal updates */
2497static void
2498update_state_fb(struct drm_plane *plane)
2499{
2500 if (plane->fb == plane->state->fb)
2501 return;
2502
2503 if (plane->state->fb)
2504 drm_framebuffer_unreference(plane->state->fb);
2505 plane->state->fb = plane->fb;
2506 if (plane->state->fb)
2507 drm_framebuffer_reference(plane->state->fb);
2508}
2509
5724dbd1 2510static void
f6936e29
DV
2511intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
2512 struct intel_initial_plane_config *plane_config)
484b41dd
JB
2513{
2514 struct drm_device *dev = intel_crtc->base.dev;
fac5e23e 2515 struct drm_i915_private *dev_priv = to_i915(dev);
484b41dd
JB
2516 struct drm_crtc *c;
2517 struct intel_crtc *i;
2ff8fde1 2518 struct drm_i915_gem_object *obj;
88595ac9 2519 struct drm_plane *primary = intel_crtc->base.primary;
be5651f2 2520 struct drm_plane_state *plane_state = primary->state;
200757f5
MR
2521 struct drm_crtc_state *crtc_state = intel_crtc->base.state;
2522 struct intel_plane *intel_plane = to_intel_plane(primary);
0a8d8a86
MR
2523 struct intel_plane_state *intel_state =
2524 to_intel_plane_state(plane_state);
88595ac9 2525 struct drm_framebuffer *fb;
484b41dd 2526
2d14030b 2527 if (!plane_config->fb)
484b41dd
JB
2528 return;
2529
f6936e29 2530 if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
88595ac9
DV
2531 fb = &plane_config->fb->base;
2532 goto valid_fb;
f55548b5 2533 }
484b41dd 2534
2d14030b 2535 kfree(plane_config->fb);
484b41dd
JB
2536
2537 /*
2538 * Failed to alloc the obj, check to see if we should share
2539 * an fb with another CRTC instead
2540 */
70e1e0ec 2541 for_each_crtc(dev, c) {
484b41dd
JB
2542 i = to_intel_crtc(c);
2543
2544 if (c == &intel_crtc->base)
2545 continue;
2546
2ff8fde1
MR
2547 if (!i->active)
2548 continue;
2549
88595ac9
DV
2550 fb = c->primary->fb;
2551 if (!fb)
484b41dd
JB
2552 continue;
2553
88595ac9 2554 obj = intel_fb_obj(fb);
2ff8fde1 2555 if (i915_gem_obj_ggtt_offset(obj) == plane_config->base) {
88595ac9
DV
2556 drm_framebuffer_reference(fb);
2557 goto valid_fb;
484b41dd
JB
2558 }
2559 }
88595ac9 2560
200757f5
MR
2561 /*
2562 * We've failed to reconstruct the BIOS FB. Current display state
2563 * indicates that the primary plane is visible, but has a NULL FB,
2564 * which will lead to problems later if we don't fix it up. The
2565 * simplest solution is to just disable the primary plane now and
2566 * pretend the BIOS never had it enabled.
2567 */
2568 to_intel_plane_state(plane_state)->visible = false;
2569 crtc_state->plane_mask &= ~(1 << drm_plane_index(primary));
2622a081 2570 intel_pre_disable_primary_noatomic(&intel_crtc->base);
200757f5
MR
2571 intel_plane->disable_plane(primary, &intel_crtc->base);
2572
88595ac9
DV
2573 return;
2574
2575valid_fb:
f44e2659
VS
2576 plane_state->src_x = 0;
2577 plane_state->src_y = 0;
be5651f2
ML
2578 plane_state->src_w = fb->width << 16;
2579 plane_state->src_h = fb->height << 16;
2580
f44e2659
VS
2581 plane_state->crtc_x = 0;
2582 plane_state->crtc_y = 0;
be5651f2
ML
2583 plane_state->crtc_w = fb->width;
2584 plane_state->crtc_h = fb->height;
2585
0a8d8a86
MR
2586 intel_state->src.x1 = plane_state->src_x;
2587 intel_state->src.y1 = plane_state->src_y;
2588 intel_state->src.x2 = plane_state->src_x + plane_state->src_w;
2589 intel_state->src.y2 = plane_state->src_y + plane_state->src_h;
2590 intel_state->dst.x1 = plane_state->crtc_x;
2591 intel_state->dst.y1 = plane_state->crtc_y;
2592 intel_state->dst.x2 = plane_state->crtc_x + plane_state->crtc_w;
2593 intel_state->dst.y2 = plane_state->crtc_y + plane_state->crtc_h;
2594
88595ac9 2595 obj = intel_fb_obj(fb);
3e510a8e 2596 if (i915_gem_object_is_tiled(obj))
88595ac9
DV
2597 dev_priv->preserve_bios_swizzle = true;
2598
be5651f2
ML
2599 drm_framebuffer_reference(fb);
2600 primary->fb = primary->state->fb = fb;
36750f28 2601 primary->crtc = primary->state->crtc = &intel_crtc->base;
36750f28 2602 intel_crtc->base.state->plane_mask |= (1 << drm_plane_index(primary));
faf5bf0a
CW
2603 atomic_or(to_intel_plane(primary)->frontbuffer_bit,
2604 &obj->frontbuffer_bits);
46f297fb
JB
2605}
2606
a8d201af
ML
2607static void i9xx_update_primary_plane(struct drm_plane *primary,
2608 const struct intel_crtc_state *crtc_state,
2609 const struct intel_plane_state *plane_state)
81255565 2610{
a8d201af 2611 struct drm_device *dev = primary->dev;
fac5e23e 2612 struct drm_i915_private *dev_priv = to_i915(dev);
a8d201af
ML
2613 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
2614 struct drm_framebuffer *fb = plane_state->base.fb;
2615 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
81255565 2616 int plane = intel_crtc->plane;
54ea9da8 2617 u32 linear_offset;
81255565 2618 u32 dspcntr;
f0f59a00 2619 i915_reg_t reg = DSPCNTR(plane);
8d0deca8 2620 unsigned int rotation = plane_state->base.rotation;
ac484963 2621 int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
54ea9da8
VS
2622 int x = plane_state->src.x1 >> 16;
2623 int y = plane_state->src.y1 >> 16;
c9ba6fad 2624
f45651ba
VS
2625 dspcntr = DISPPLANE_GAMMA_ENABLE;
2626
fdd508a6 2627 dspcntr |= DISPLAY_PLANE_ENABLE;
f45651ba
VS
2628
2629 if (INTEL_INFO(dev)->gen < 4) {
2630 if (intel_crtc->pipe == PIPE_B)
2631 dspcntr |= DISPPLANE_SEL_PIPE_B;
2632
2633 /* pipesrc and dspsize control the size that is scaled from,
2634 * which should always be the user's requested size.
2635 */
2636 I915_WRITE(DSPSIZE(plane),
a8d201af
ML
2637 ((crtc_state->pipe_src_h - 1) << 16) |
2638 (crtc_state->pipe_src_w - 1));
f45651ba 2639 I915_WRITE(DSPPOS(plane), 0);
c14b0485
VS
2640 } else if (IS_CHERRYVIEW(dev) && plane == PLANE_B) {
2641 I915_WRITE(PRIMSIZE(plane),
a8d201af
ML
2642 ((crtc_state->pipe_src_h - 1) << 16) |
2643 (crtc_state->pipe_src_w - 1));
c14b0485
VS
2644 I915_WRITE(PRIMPOS(plane), 0);
2645 I915_WRITE(PRIMCNSTALPHA(plane), 0);
f45651ba 2646 }
81255565 2647
57779d06
VS
2648 switch (fb->pixel_format) {
2649 case DRM_FORMAT_C8:
81255565
JB
2650 dspcntr |= DISPPLANE_8BPP;
2651 break;
57779d06 2652 case DRM_FORMAT_XRGB1555:
57779d06 2653 dspcntr |= DISPPLANE_BGRX555;
81255565 2654 break;
57779d06
VS
2655 case DRM_FORMAT_RGB565:
2656 dspcntr |= DISPPLANE_BGRX565;
2657 break;
2658 case DRM_FORMAT_XRGB8888:
57779d06
VS
2659 dspcntr |= DISPPLANE_BGRX888;
2660 break;
2661 case DRM_FORMAT_XBGR8888:
57779d06
VS
2662 dspcntr |= DISPPLANE_RGBX888;
2663 break;
2664 case DRM_FORMAT_XRGB2101010:
57779d06
VS
2665 dspcntr |= DISPPLANE_BGRX101010;
2666 break;
2667 case DRM_FORMAT_XBGR2101010:
57779d06 2668 dspcntr |= DISPPLANE_RGBX101010;
81255565
JB
2669 break;
2670 default:
baba133a 2671 BUG();
81255565 2672 }
57779d06 2673
3e510a8e 2674 if (INTEL_INFO(dev)->gen >= 4 && i915_gem_object_is_tiled(obj))
f45651ba 2675 dspcntr |= DISPPLANE_TILED;
81255565 2676
de1aa629
VS
2677 if (IS_G4X(dev))
2678 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
2679
ac484963 2680 linear_offset = y * fb->pitches[0] + x * cpp;
81255565 2681
c2c75131
DV
2682 if (INTEL_INFO(dev)->gen >= 4) {
2683 intel_crtc->dspaddr_offset =
4f2d9934 2684 intel_compute_tile_offset(&x, &y, fb, 0,
8d0deca8 2685 fb->pitches[0], rotation);
c2c75131
DV
2686 linear_offset -= intel_crtc->dspaddr_offset;
2687 } else {
e506a0c6 2688 intel_crtc->dspaddr_offset = linear_offset;
c2c75131 2689 }
e506a0c6 2690
8d0deca8 2691 if (rotation == BIT(DRM_ROTATE_180)) {
48404c1e
SJ
2692 dspcntr |= DISPPLANE_ROTATE_180;
2693
a8d201af
ML
2694 x += (crtc_state->pipe_src_w - 1);
2695 y += (crtc_state->pipe_src_h - 1);
48404c1e
SJ
2696
2697 /* Finding the last pixel of the last line of the display
2698 data and adding to linear_offset*/
2699 linear_offset +=
a8d201af 2700 (crtc_state->pipe_src_h - 1) * fb->pitches[0] +
ac484963 2701 (crtc_state->pipe_src_w - 1) * cpp;
48404c1e
SJ
2702 }
2703
2db3366b
PZ
2704 intel_crtc->adjusted_x = x;
2705 intel_crtc->adjusted_y = y;
2706
48404c1e
SJ
2707 I915_WRITE(reg, dspcntr);
2708
01f2c773 2709 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
a6c45cf0 2710 if (INTEL_INFO(dev)->gen >= 4) {
85ba7b7d
DV
2711 I915_WRITE(DSPSURF(plane),
2712 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
5eddb70b 2713 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
e506a0c6 2714 I915_WRITE(DSPLINOFF(plane), linear_offset);
5eddb70b 2715 } else
f343c5f6 2716 I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
5eddb70b 2717 POSTING_READ(reg);
17638cd6
JB
2718}
2719
a8d201af
ML
2720static void i9xx_disable_primary_plane(struct drm_plane *primary,
2721 struct drm_crtc *crtc)
17638cd6
JB
2722{
2723 struct drm_device *dev = crtc->dev;
fac5e23e 2724 struct drm_i915_private *dev_priv = to_i915(dev);
17638cd6 2725 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
17638cd6 2726 int plane = intel_crtc->plane;
f45651ba 2727
a8d201af
ML
2728 I915_WRITE(DSPCNTR(plane), 0);
2729 if (INTEL_INFO(dev_priv)->gen >= 4)
fdd508a6 2730 I915_WRITE(DSPSURF(plane), 0);
a8d201af
ML
2731 else
2732 I915_WRITE(DSPADDR(plane), 0);
2733 POSTING_READ(DSPCNTR(plane));
2734}
c9ba6fad 2735
a8d201af
ML
2736static void ironlake_update_primary_plane(struct drm_plane *primary,
2737 const struct intel_crtc_state *crtc_state,
2738 const struct intel_plane_state *plane_state)
2739{
2740 struct drm_device *dev = primary->dev;
fac5e23e 2741 struct drm_i915_private *dev_priv = to_i915(dev);
a8d201af
ML
2742 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
2743 struct drm_framebuffer *fb = plane_state->base.fb;
2744 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
2745 int plane = intel_crtc->plane;
54ea9da8 2746 u32 linear_offset;
a8d201af
ML
2747 u32 dspcntr;
2748 i915_reg_t reg = DSPCNTR(plane);
8d0deca8 2749 unsigned int rotation = plane_state->base.rotation;
ac484963 2750 int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
a8d201af
ML
2751 int x = plane_state->src.x1 >> 16;
2752 int y = plane_state->src.y1 >> 16;
c9ba6fad 2753
f45651ba 2754 dspcntr = DISPPLANE_GAMMA_ENABLE;
fdd508a6 2755 dspcntr |= DISPLAY_PLANE_ENABLE;
f45651ba
VS
2756
2757 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
2758 dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
17638cd6 2759
57779d06
VS
2760 switch (fb->pixel_format) {
2761 case DRM_FORMAT_C8:
17638cd6
JB
2762 dspcntr |= DISPPLANE_8BPP;
2763 break;
57779d06
VS
2764 case DRM_FORMAT_RGB565:
2765 dspcntr |= DISPPLANE_BGRX565;
17638cd6 2766 break;
57779d06 2767 case DRM_FORMAT_XRGB8888:
57779d06
VS
2768 dspcntr |= DISPPLANE_BGRX888;
2769 break;
2770 case DRM_FORMAT_XBGR8888:
57779d06
VS
2771 dspcntr |= DISPPLANE_RGBX888;
2772 break;
2773 case DRM_FORMAT_XRGB2101010:
57779d06
VS
2774 dspcntr |= DISPPLANE_BGRX101010;
2775 break;
2776 case DRM_FORMAT_XBGR2101010:
57779d06 2777 dspcntr |= DISPPLANE_RGBX101010;
17638cd6
JB
2778 break;
2779 default:
baba133a 2780 BUG();
17638cd6
JB
2781 }
2782
3e510a8e 2783 if (i915_gem_object_is_tiled(obj))
17638cd6 2784 dspcntr |= DISPPLANE_TILED;
17638cd6 2785
f45651ba 2786 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev))
1f5d76db 2787 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
17638cd6 2788
ac484963 2789 linear_offset = y * fb->pitches[0] + x * cpp;
c2c75131 2790 intel_crtc->dspaddr_offset =
4f2d9934 2791 intel_compute_tile_offset(&x, &y, fb, 0,
8d0deca8 2792 fb->pitches[0], rotation);
c2c75131 2793 linear_offset -= intel_crtc->dspaddr_offset;
8d0deca8 2794 if (rotation == BIT(DRM_ROTATE_180)) {
48404c1e
SJ
2795 dspcntr |= DISPPLANE_ROTATE_180;
2796
2797 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev)) {
a8d201af
ML
2798 x += (crtc_state->pipe_src_w - 1);
2799 y += (crtc_state->pipe_src_h - 1);
48404c1e
SJ
2800
2801 /* Finding the last pixel of the last line of the display
2802 data and adding to linear_offset*/
2803 linear_offset +=
a8d201af 2804 (crtc_state->pipe_src_h - 1) * fb->pitches[0] +
ac484963 2805 (crtc_state->pipe_src_w - 1) * cpp;
48404c1e
SJ
2806 }
2807 }
2808
2db3366b
PZ
2809 intel_crtc->adjusted_x = x;
2810 intel_crtc->adjusted_y = y;
2811
48404c1e 2812 I915_WRITE(reg, dspcntr);
17638cd6 2813
01f2c773 2814 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
85ba7b7d
DV
2815 I915_WRITE(DSPSURF(plane),
2816 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
b3dc685e 2817 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
bc1c91eb
DL
2818 I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
2819 } else {
2820 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2821 I915_WRITE(DSPLINOFF(plane), linear_offset);
2822 }
17638cd6 2823 POSTING_READ(reg);
17638cd6
JB
2824}
2825
7b49f948
VS
2826u32 intel_fb_stride_alignment(const struct drm_i915_private *dev_priv,
2827 uint64_t fb_modifier, uint32_t pixel_format)
b321803d 2828{
7b49f948 2829 if (fb_modifier == DRM_FORMAT_MOD_NONE) {
b321803d 2830 return 64;
7b49f948
VS
2831 } else {
2832 int cpp = drm_format_plane_cpp(pixel_format, 0);
2833
27ba3910 2834 return intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
b321803d
DL
2835 }
2836}
2837
44eb0cb9
MK
2838u32 intel_plane_obj_offset(struct intel_plane *intel_plane,
2839 struct drm_i915_gem_object *obj,
2840 unsigned int plane)
121920fa 2841{
ce7f1728 2842 struct i915_ggtt_view view;
dedf278c 2843 struct i915_vma *vma;
44eb0cb9 2844 u64 offset;
121920fa 2845
e7941294 2846 intel_fill_fb_ggtt_view(&view, intel_plane->base.state->fb,
3465c580 2847 intel_plane->base.state->rotation);
121920fa 2848
ce7f1728 2849 vma = i915_gem_obj_to_ggtt_view(obj, &view);
dedf278c 2850 if (WARN(!vma, "ggtt vma for display object not found! (view=%u)\n",
ce7f1728 2851 view.type))
dedf278c
TU
2852 return -1;
2853
44eb0cb9 2854 offset = vma->node.start;
dedf278c
TU
2855
2856 if (plane == 1) {
7723f47d 2857 offset += vma->ggtt_view.params.rotated.uv_start_page *
dedf278c
TU
2858 PAGE_SIZE;
2859 }
2860
44eb0cb9
MK
2861 WARN_ON(upper_32_bits(offset));
2862
2863 return lower_32_bits(offset);
121920fa
TU
2864}
2865
e435d6e5
ML
2866static void skl_detach_scaler(struct intel_crtc *intel_crtc, int id)
2867{
2868 struct drm_device *dev = intel_crtc->base.dev;
fac5e23e 2869 struct drm_i915_private *dev_priv = to_i915(dev);
e435d6e5
ML
2870
2871 I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, id), 0);
2872 I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, id), 0);
2873 I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, id), 0);
e435d6e5
ML
2874}
2875
a1b2278e
CK
2876/*
2877 * This function detaches (aka. unbinds) unused scalers in hardware
2878 */
0583236e 2879static void skl_detach_scalers(struct intel_crtc *intel_crtc)
a1b2278e 2880{
a1b2278e
CK
2881 struct intel_crtc_scaler_state *scaler_state;
2882 int i;
2883
a1b2278e
CK
2884 scaler_state = &intel_crtc->config->scaler_state;
2885
2886 /* loop through and disable scalers that aren't in use */
2887 for (i = 0; i < intel_crtc->num_scalers; i++) {
e435d6e5
ML
2888 if (!scaler_state->scalers[i].in_use)
2889 skl_detach_scaler(intel_crtc, i);
a1b2278e
CK
2890 }
2891}
2892
6156a456 2893u32 skl_plane_ctl_format(uint32_t pixel_format)
70d21f0e 2894{
6156a456 2895 switch (pixel_format) {
d161cf7a 2896 case DRM_FORMAT_C8:
c34ce3d1 2897 return PLANE_CTL_FORMAT_INDEXED;
70d21f0e 2898 case DRM_FORMAT_RGB565:
c34ce3d1 2899 return PLANE_CTL_FORMAT_RGB_565;
70d21f0e 2900 case DRM_FORMAT_XBGR8888:
c34ce3d1 2901 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
6156a456 2902 case DRM_FORMAT_XRGB8888:
c34ce3d1 2903 return PLANE_CTL_FORMAT_XRGB_8888;
6156a456
CK
2904 /*
2905 * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
2906 * to be already pre-multiplied. We need to add a knob (or a different
2907 * DRM_FORMAT) for user-space to configure that.
2908 */
f75fb42a 2909 case DRM_FORMAT_ABGR8888:
c34ce3d1 2910 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX |
6156a456 2911 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
6156a456 2912 case DRM_FORMAT_ARGB8888:
c34ce3d1 2913 return PLANE_CTL_FORMAT_XRGB_8888 |
6156a456 2914 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
70d21f0e 2915 case DRM_FORMAT_XRGB2101010:
c34ce3d1 2916 return PLANE_CTL_FORMAT_XRGB_2101010;
70d21f0e 2917 case DRM_FORMAT_XBGR2101010:
c34ce3d1 2918 return PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
6156a456 2919 case DRM_FORMAT_YUYV:
c34ce3d1 2920 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
6156a456 2921 case DRM_FORMAT_YVYU:
c34ce3d1 2922 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
6156a456 2923 case DRM_FORMAT_UYVY:
c34ce3d1 2924 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
6156a456 2925 case DRM_FORMAT_VYUY:
c34ce3d1 2926 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
70d21f0e 2927 default:
4249eeef 2928 MISSING_CASE(pixel_format);
70d21f0e 2929 }
8cfcba41 2930
c34ce3d1 2931 return 0;
6156a456 2932}
70d21f0e 2933
6156a456
CK
2934u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
2935{
6156a456 2936 switch (fb_modifier) {
30af77c4 2937 case DRM_FORMAT_MOD_NONE:
70d21f0e 2938 break;
30af77c4 2939 case I915_FORMAT_MOD_X_TILED:
c34ce3d1 2940 return PLANE_CTL_TILED_X;
b321803d 2941 case I915_FORMAT_MOD_Y_TILED:
c34ce3d1 2942 return PLANE_CTL_TILED_Y;
b321803d 2943 case I915_FORMAT_MOD_Yf_TILED:
c34ce3d1 2944 return PLANE_CTL_TILED_YF;
70d21f0e 2945 default:
6156a456 2946 MISSING_CASE(fb_modifier);
70d21f0e 2947 }
8cfcba41 2948
c34ce3d1 2949 return 0;
6156a456 2950}
70d21f0e 2951
6156a456
CK
2952u32 skl_plane_ctl_rotation(unsigned int rotation)
2953{
3b7a5119 2954 switch (rotation) {
6156a456
CK
2955 case BIT(DRM_ROTATE_0):
2956 break;
1e8df167
SJ
2957 /*
2958 * DRM_ROTATE_ is counter clockwise to stay compatible with Xrandr
2959 * while i915 HW rotation is clockwise, thats why this swapping.
2960 */
3b7a5119 2961 case BIT(DRM_ROTATE_90):
1e8df167 2962 return PLANE_CTL_ROTATE_270;
3b7a5119 2963 case BIT(DRM_ROTATE_180):
c34ce3d1 2964 return PLANE_CTL_ROTATE_180;
3b7a5119 2965 case BIT(DRM_ROTATE_270):
1e8df167 2966 return PLANE_CTL_ROTATE_90;
6156a456
CK
2967 default:
2968 MISSING_CASE(rotation);
2969 }
2970
c34ce3d1 2971 return 0;
6156a456
CK
2972}
2973
a8d201af
ML
2974static void skylake_update_primary_plane(struct drm_plane *plane,
2975 const struct intel_crtc_state *crtc_state,
2976 const struct intel_plane_state *plane_state)
6156a456 2977{
a8d201af 2978 struct drm_device *dev = plane->dev;
fac5e23e 2979 struct drm_i915_private *dev_priv = to_i915(dev);
a8d201af
ML
2980 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
2981 struct drm_framebuffer *fb = plane_state->base.fb;
2982 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
6156a456
CK
2983 int pipe = intel_crtc->pipe;
2984 u32 plane_ctl, stride_div, stride;
2985 u32 tile_height, plane_offset, plane_size;
a8d201af 2986 unsigned int rotation = plane_state->base.rotation;
6156a456 2987 int x_offset, y_offset;
44eb0cb9 2988 u32 surf_addr;
a8d201af
ML
2989 int scaler_id = plane_state->scaler_id;
2990 int src_x = plane_state->src.x1 >> 16;
2991 int src_y = plane_state->src.y1 >> 16;
2992 int src_w = drm_rect_width(&plane_state->src) >> 16;
2993 int src_h = drm_rect_height(&plane_state->src) >> 16;
2994 int dst_x = plane_state->dst.x1;
2995 int dst_y = plane_state->dst.y1;
2996 int dst_w = drm_rect_width(&plane_state->dst);
2997 int dst_h = drm_rect_height(&plane_state->dst);
70d21f0e 2998
6156a456
CK
2999 plane_ctl = PLANE_CTL_ENABLE |
3000 PLANE_CTL_PIPE_GAMMA_ENABLE |
3001 PLANE_CTL_PIPE_CSC_ENABLE;
3002
3003 plane_ctl |= skl_plane_ctl_format(fb->pixel_format);
3004 plane_ctl |= skl_plane_ctl_tiling(fb->modifier[0]);
3005 plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
6156a456
CK
3006 plane_ctl |= skl_plane_ctl_rotation(rotation);
3007
7b49f948 3008 stride_div = intel_fb_stride_alignment(dev_priv, fb->modifier[0],
b321803d 3009 fb->pixel_format);
dedf278c 3010 surf_addr = intel_plane_obj_offset(to_intel_plane(plane), obj, 0);
3b7a5119 3011
a42e5a23
PZ
3012 WARN_ON(drm_rect_width(&plane_state->src) == 0);
3013
3b7a5119 3014 if (intel_rotation_90_or_270(rotation)) {
832be82f
VS
3015 int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
3016
3b7a5119 3017 /* stride = Surface height in tiles */
832be82f 3018 tile_height = intel_tile_height(dev_priv, fb->modifier[0], cpp);
3b7a5119 3019 stride = DIV_ROUND_UP(fb->height, tile_height);
a8d201af
ML
3020 x_offset = stride * tile_height - src_y - src_h;
3021 y_offset = src_x;
6156a456 3022 plane_size = (src_w - 1) << 16 | (src_h - 1);
3b7a5119
SJ
3023 } else {
3024 stride = fb->pitches[0] / stride_div;
a8d201af
ML
3025 x_offset = src_x;
3026 y_offset = src_y;
6156a456 3027 plane_size = (src_h - 1) << 16 | (src_w - 1);
3b7a5119
SJ
3028 }
3029 plane_offset = y_offset << 16 | x_offset;
b321803d 3030
2db3366b
PZ
3031 intel_crtc->adjusted_x = x_offset;
3032 intel_crtc->adjusted_y = y_offset;
3033
70d21f0e 3034 I915_WRITE(PLANE_CTL(pipe, 0), plane_ctl);
3b7a5119
SJ
3035 I915_WRITE(PLANE_OFFSET(pipe, 0), plane_offset);
3036 I915_WRITE(PLANE_SIZE(pipe, 0), plane_size);
3037 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
6156a456
CK
3038
3039 if (scaler_id >= 0) {
3040 uint32_t ps_ctrl = 0;
3041
3042 WARN_ON(!dst_w || !dst_h);
3043 ps_ctrl = PS_SCALER_EN | PS_PLANE_SEL(0) |
3044 crtc_state->scaler_state.scalers[scaler_id].mode;
3045 I915_WRITE(SKL_PS_CTRL(pipe, scaler_id), ps_ctrl);
3046 I915_WRITE(SKL_PS_PWR_GATE(pipe, scaler_id), 0);
3047 I915_WRITE(SKL_PS_WIN_POS(pipe, scaler_id), (dst_x << 16) | dst_y);
3048 I915_WRITE(SKL_PS_WIN_SZ(pipe, scaler_id), (dst_w << 16) | dst_h);
3049 I915_WRITE(PLANE_POS(pipe, 0), 0);
3050 } else {
3051 I915_WRITE(PLANE_POS(pipe, 0), (dst_y << 16) | dst_x);
3052 }
3053
121920fa 3054 I915_WRITE(PLANE_SURF(pipe, 0), surf_addr);
70d21f0e
DL
3055
3056 POSTING_READ(PLANE_SURF(pipe, 0));
3057}
3058
a8d201af
ML
3059static void skylake_disable_primary_plane(struct drm_plane *primary,
3060 struct drm_crtc *crtc)
17638cd6
JB
3061{
3062 struct drm_device *dev = crtc->dev;
fac5e23e 3063 struct drm_i915_private *dev_priv = to_i915(dev);
a8d201af 3064 int pipe = to_intel_crtc(crtc)->pipe;
17638cd6 3065
a8d201af
ML
3066 I915_WRITE(PLANE_CTL(pipe, 0), 0);
3067 I915_WRITE(PLANE_SURF(pipe, 0), 0);
3068 POSTING_READ(PLANE_SURF(pipe, 0));
3069}
29b9bde6 3070
a8d201af
ML
3071/* Assume fb object is pinned & idle & fenced and just update base pointers */
3072static int
3073intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
3074 int x, int y, enum mode_set_atomic state)
3075{
3076 /* Support for kgdboc is disabled, this needs a major rework. */
3077 DRM_ERROR("legacy panic handler not supported any more.\n");
3078
3079 return -ENODEV;
81255565
JB
3080}
3081
5a21b665
DV
3082static void intel_complete_page_flips(struct drm_i915_private *dev_priv)
3083{
3084 struct intel_crtc *crtc;
3085
91c8a326 3086 for_each_intel_crtc(&dev_priv->drm, crtc)
5a21b665
DV
3087 intel_finish_page_flip_cs(dev_priv, crtc->pipe);
3088}
3089
7514747d
VS
3090static void intel_update_primary_planes(struct drm_device *dev)
3091{
7514747d 3092 struct drm_crtc *crtc;
96a02917 3093
70e1e0ec 3094 for_each_crtc(dev, crtc) {
11c22da6
ML
3095 struct intel_plane *plane = to_intel_plane(crtc->primary);
3096 struct intel_plane_state *plane_state;
96a02917 3097
11c22da6 3098 drm_modeset_lock_crtc(crtc, &plane->base);
11c22da6
ML
3099 plane_state = to_intel_plane_state(plane->base.state);
3100
a8d201af
ML
3101 if (plane_state->visible)
3102 plane->update_plane(&plane->base,
3103 to_intel_crtc_state(crtc->state),
3104 plane_state);
11c22da6
ML
3105
3106 drm_modeset_unlock_crtc(crtc);
96a02917
VS
3107 }
3108}
3109
c033666a 3110void intel_prepare_reset(struct drm_i915_private *dev_priv)
7514747d
VS
3111{
3112 /* no reset support for gen2 */
c033666a 3113 if (IS_GEN2(dev_priv))
7514747d
VS
3114 return;
3115
3116 /* reset doesn't touch the display */
c033666a 3117 if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
7514747d
VS
3118 return;
3119
91c8a326 3120 drm_modeset_lock_all(&dev_priv->drm);
f98ce92f
VS
3121 /*
3122 * Disabling the crtcs gracefully seems nicer. Also the
3123 * g33 docs say we should at least disable all the planes.
3124 */
91c8a326 3125 intel_display_suspend(&dev_priv->drm);
7514747d
VS
3126}
3127
c033666a 3128void intel_finish_reset(struct drm_i915_private *dev_priv)
7514747d 3129{
5a21b665
DV
3130 /*
3131 * Flips in the rings will be nuked by the reset,
3132 * so complete all pending flips so that user space
3133 * will get its events and not get stuck.
3134 */
3135 intel_complete_page_flips(dev_priv);
3136
7514747d 3137 /* no reset support for gen2 */
c033666a 3138 if (IS_GEN2(dev_priv))
7514747d
VS
3139 return;
3140
3141 /* reset doesn't touch the display */
c033666a 3142 if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv)) {
7514747d
VS
3143 /*
3144 * Flips in the rings have been nuked by the reset,
3145 * so update the base address of all primary
3146 * planes to the the last fb to make sure we're
3147 * showing the correct fb after a reset.
11c22da6
ML
3148 *
3149 * FIXME: Atomic will make this obsolete since we won't schedule
3150 * CS-based flips (which might get lost in gpu resets) any more.
7514747d 3151 */
91c8a326 3152 intel_update_primary_planes(&dev_priv->drm);
7514747d
VS
3153 return;
3154 }
3155
3156 /*
3157 * The display has been reset as well,
3158 * so need a full re-initialization.
3159 */
3160 intel_runtime_pm_disable_interrupts(dev_priv);
3161 intel_runtime_pm_enable_interrupts(dev_priv);
3162
91c8a326 3163 intel_modeset_init_hw(&dev_priv->drm);
7514747d
VS
3164
3165 spin_lock_irq(&dev_priv->irq_lock);
3166 if (dev_priv->display.hpd_irq_setup)
91d14251 3167 dev_priv->display.hpd_irq_setup(dev_priv);
7514747d
VS
3168 spin_unlock_irq(&dev_priv->irq_lock);
3169
91c8a326 3170 intel_display_resume(&dev_priv->drm);
7514747d
VS
3171
3172 intel_hpd_init(dev_priv);
3173
91c8a326 3174 drm_modeset_unlock_all(&dev_priv->drm);
7514747d
VS
3175}
3176
7d5e3799
CW
3177static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
3178{
5a21b665
DV
3179 struct drm_device *dev = crtc->dev;
3180 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3181 unsigned reset_counter;
3182 bool pending;
3183
3184 reset_counter = i915_reset_counter(&to_i915(dev)->gpu_error);
3185 if (intel_crtc->reset_counter != reset_counter)
3186 return false;
3187
3188 spin_lock_irq(&dev->event_lock);
3189 pending = to_intel_crtc(crtc)->flip_work != NULL;
3190 spin_unlock_irq(&dev->event_lock);
3191
3192 return pending;
7d5e3799
CW
3193}
3194
bfd16b2a
ML
3195static void intel_update_pipe_config(struct intel_crtc *crtc,
3196 struct intel_crtc_state *old_crtc_state)
e30e8f75
GP
3197{
3198 struct drm_device *dev = crtc->base.dev;
fac5e23e 3199 struct drm_i915_private *dev_priv = to_i915(dev);
bfd16b2a
ML
3200 struct intel_crtc_state *pipe_config =
3201 to_intel_crtc_state(crtc->base.state);
e30e8f75 3202
bfd16b2a
ML
3203 /* drm_atomic_helper_update_legacy_modeset_state might not be called. */
3204 crtc->base.mode = crtc->base.state->mode;
3205
3206 DRM_DEBUG_KMS("Updating pipe size %ix%i -> %ix%i\n",
3207 old_crtc_state->pipe_src_w, old_crtc_state->pipe_src_h,
3208 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
e30e8f75
GP
3209
3210 /*
3211 * Update pipe size and adjust fitter if needed: the reason for this is
3212 * that in compute_mode_changes we check the native mode (not the pfit
3213 * mode) to see if we can flip rather than do a full mode set. In the
3214 * fastboot case, we'll flip, but if we don't update the pipesrc and
3215 * pfit state, we'll end up with a big fb scanned out into the wrong
3216 * sized surface.
e30e8f75
GP
3217 */
3218
e30e8f75 3219 I915_WRITE(PIPESRC(crtc->pipe),
bfd16b2a
ML
3220 ((pipe_config->pipe_src_w - 1) << 16) |
3221 (pipe_config->pipe_src_h - 1));
3222
3223 /* on skylake this is done by detaching scalers */
3224 if (INTEL_INFO(dev)->gen >= 9) {
3225 skl_detach_scalers(crtc);
3226
3227 if (pipe_config->pch_pfit.enabled)
3228 skylake_pfit_enable(crtc);
3229 } else if (HAS_PCH_SPLIT(dev)) {
3230 if (pipe_config->pch_pfit.enabled)
3231 ironlake_pfit_enable(crtc);
3232 else if (old_crtc_state->pch_pfit.enabled)
3233 ironlake_pfit_disable(crtc, true);
e30e8f75 3234 }
e30e8f75
GP
3235}
3236
5e84e1a4
ZW
3237static void intel_fdi_normal_train(struct drm_crtc *crtc)
3238{
3239 struct drm_device *dev = crtc->dev;
fac5e23e 3240 struct drm_i915_private *dev_priv = to_i915(dev);
5e84e1a4
ZW
3241 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3242 int pipe = intel_crtc->pipe;
f0f59a00
VS
3243 i915_reg_t reg;
3244 u32 temp;
5e84e1a4
ZW
3245
3246 /* enable normal train */
3247 reg = FDI_TX_CTL(pipe);
3248 temp = I915_READ(reg);
61e499bf 3249 if (IS_IVYBRIDGE(dev)) {
357555c0
JB
3250 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3251 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
61e499bf
KP
3252 } else {
3253 temp &= ~FDI_LINK_TRAIN_NONE;
3254 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
357555c0 3255 }
5e84e1a4
ZW
3256 I915_WRITE(reg, temp);
3257
3258 reg = FDI_RX_CTL(pipe);
3259 temp = I915_READ(reg);
3260 if (HAS_PCH_CPT(dev)) {
3261 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3262 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
3263 } else {
3264 temp &= ~FDI_LINK_TRAIN_NONE;
3265 temp |= FDI_LINK_TRAIN_NONE;
3266 }
3267 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
3268
3269 /* wait one idle pattern time */
3270 POSTING_READ(reg);
3271 udelay(1000);
357555c0
JB
3272
3273 /* IVB wants error correction enabled */
3274 if (IS_IVYBRIDGE(dev))
3275 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
3276 FDI_FE_ERRC_ENABLE);
5e84e1a4
ZW
3277}
3278
8db9d77b
ZW
3279/* The FDI link training functions for ILK/Ibexpeak. */
3280static void ironlake_fdi_link_train(struct drm_crtc *crtc)
3281{
3282 struct drm_device *dev = crtc->dev;
fac5e23e 3283 struct drm_i915_private *dev_priv = to_i915(dev);
8db9d77b
ZW
3284 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3285 int pipe = intel_crtc->pipe;
f0f59a00
VS
3286 i915_reg_t reg;
3287 u32 temp, tries;
8db9d77b 3288
1c8562f6 3289 /* FDI needs bits from pipe first */
0fc932b8 3290 assert_pipe_enabled(dev_priv, pipe);
0fc932b8 3291
e1a44743
AJ
3292 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3293 for train result */
5eddb70b
CW
3294 reg = FDI_RX_IMR(pipe);
3295 temp = I915_READ(reg);
e1a44743
AJ
3296 temp &= ~FDI_RX_SYMBOL_LOCK;
3297 temp &= ~FDI_RX_BIT_LOCK;
5eddb70b
CW
3298 I915_WRITE(reg, temp);
3299 I915_READ(reg);
e1a44743
AJ
3300 udelay(150);
3301
8db9d77b 3302 /* enable CPU FDI TX and PCH FDI RX */
5eddb70b
CW
3303 reg = FDI_TX_CTL(pipe);
3304 temp = I915_READ(reg);
627eb5a3 3305 temp &= ~FDI_DP_PORT_WIDTH_MASK;
6e3c9717 3306 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
8db9d77b
ZW
3307 temp &= ~FDI_LINK_TRAIN_NONE;
3308 temp |= FDI_LINK_TRAIN_PATTERN_1;
5eddb70b 3309 I915_WRITE(reg, temp | FDI_TX_ENABLE);
8db9d77b 3310
5eddb70b
CW
3311 reg = FDI_RX_CTL(pipe);
3312 temp = I915_READ(reg);
8db9d77b
ZW
3313 temp &= ~FDI_LINK_TRAIN_NONE;
3314 temp |= FDI_LINK_TRAIN_PATTERN_1;
5eddb70b
CW
3315 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3316
3317 POSTING_READ(reg);
8db9d77b
ZW
3318 udelay(150);
3319
5b2adf89 3320 /* Ironlake workaround, enable clock pointer after FDI enable*/
8f5718a6
DV
3321 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
3322 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
3323 FDI_RX_PHASE_SYNC_POINTER_EN);
5b2adf89 3324
5eddb70b 3325 reg = FDI_RX_IIR(pipe);
e1a44743 3326 for (tries = 0; tries < 5; tries++) {
5eddb70b 3327 temp = I915_READ(reg);
8db9d77b
ZW
3328 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3329
3330 if ((temp & FDI_RX_BIT_LOCK)) {
3331 DRM_DEBUG_KMS("FDI train 1 done.\n");
5eddb70b 3332 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
8db9d77b
ZW
3333 break;
3334 }
8db9d77b 3335 }
e1a44743 3336 if (tries == 5)
5eddb70b 3337 DRM_ERROR("FDI train 1 fail!\n");
8db9d77b
ZW
3338
3339 /* Train 2 */
5eddb70b
CW
3340 reg = FDI_TX_CTL(pipe);
3341 temp = I915_READ(reg);
8db9d77b
ZW
3342 temp &= ~FDI_LINK_TRAIN_NONE;
3343 temp |= FDI_LINK_TRAIN_PATTERN_2;
5eddb70b 3344 I915_WRITE(reg, temp);
8db9d77b 3345
5eddb70b
CW
3346 reg = FDI_RX_CTL(pipe);
3347 temp = I915_READ(reg);
8db9d77b
ZW
3348 temp &= ~FDI_LINK_TRAIN_NONE;
3349 temp |= FDI_LINK_TRAIN_PATTERN_2;
5eddb70b 3350 I915_WRITE(reg, temp);
8db9d77b 3351
5eddb70b
CW
3352 POSTING_READ(reg);
3353 udelay(150);
8db9d77b 3354
5eddb70b 3355 reg = FDI_RX_IIR(pipe);
e1a44743 3356 for (tries = 0; tries < 5; tries++) {
5eddb70b 3357 temp = I915_READ(reg);
8db9d77b
ZW
3358 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3359
3360 if (temp & FDI_RX_SYMBOL_LOCK) {
5eddb70b 3361 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
8db9d77b
ZW
3362 DRM_DEBUG_KMS("FDI train 2 done.\n");
3363 break;
3364 }
8db9d77b 3365 }
e1a44743 3366 if (tries == 5)
5eddb70b 3367 DRM_ERROR("FDI train 2 fail!\n");
8db9d77b
ZW
3368
3369 DRM_DEBUG_KMS("FDI train done\n");
5c5313c8 3370
8db9d77b
ZW
3371}
3372
0206e353 3373static const int snb_b_fdi_train_param[] = {
8db9d77b
ZW
3374 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
3375 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
3376 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
3377 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
3378};
3379
3380/* The FDI link training functions for SNB/Cougarpoint. */
3381static void gen6_fdi_link_train(struct drm_crtc *crtc)
3382{
3383 struct drm_device *dev = crtc->dev;
fac5e23e 3384 struct drm_i915_private *dev_priv = to_i915(dev);
8db9d77b
ZW
3385 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3386 int pipe = intel_crtc->pipe;
f0f59a00
VS
3387 i915_reg_t reg;
3388 u32 temp, i, retry;
8db9d77b 3389
e1a44743
AJ
3390 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3391 for train result */
5eddb70b
CW
3392 reg = FDI_RX_IMR(pipe);
3393 temp = I915_READ(reg);
e1a44743
AJ
3394 temp &= ~FDI_RX_SYMBOL_LOCK;
3395 temp &= ~FDI_RX_BIT_LOCK;
5eddb70b
CW
3396 I915_WRITE(reg, temp);
3397
3398 POSTING_READ(reg);
e1a44743
AJ
3399 udelay(150);
3400
8db9d77b 3401 /* enable CPU FDI TX and PCH FDI RX */
5eddb70b
CW
3402 reg = FDI_TX_CTL(pipe);
3403 temp = I915_READ(reg);
627eb5a3 3404 temp &= ~FDI_DP_PORT_WIDTH_MASK;
6e3c9717 3405 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
8db9d77b
ZW
3406 temp &= ~FDI_LINK_TRAIN_NONE;
3407 temp |= FDI_LINK_TRAIN_PATTERN_1;
3408 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3409 /* SNB-B */
3410 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
5eddb70b 3411 I915_WRITE(reg, temp | FDI_TX_ENABLE);
8db9d77b 3412
d74cf324
DV
3413 I915_WRITE(FDI_RX_MISC(pipe),
3414 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3415
5eddb70b
CW
3416 reg = FDI_RX_CTL(pipe);
3417 temp = I915_READ(reg);
8db9d77b
ZW
3418 if (HAS_PCH_CPT(dev)) {
3419 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3420 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3421 } else {
3422 temp &= ~FDI_LINK_TRAIN_NONE;
3423 temp |= FDI_LINK_TRAIN_PATTERN_1;
3424 }
5eddb70b
CW
3425 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3426
3427 POSTING_READ(reg);
8db9d77b
ZW
3428 udelay(150);
3429
0206e353 3430 for (i = 0; i < 4; i++) {
5eddb70b
CW
3431 reg = FDI_TX_CTL(pipe);
3432 temp = I915_READ(reg);
8db9d77b
ZW
3433 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3434 temp |= snb_b_fdi_train_param[i];
5eddb70b
CW
3435 I915_WRITE(reg, temp);
3436
3437 POSTING_READ(reg);
8db9d77b
ZW
3438 udelay(500);
3439
fa37d39e
SP
3440 for (retry = 0; retry < 5; retry++) {
3441 reg = FDI_RX_IIR(pipe);
3442 temp = I915_READ(reg);
3443 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3444 if (temp & FDI_RX_BIT_LOCK) {
3445 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3446 DRM_DEBUG_KMS("FDI train 1 done.\n");
3447 break;
3448 }
3449 udelay(50);
8db9d77b 3450 }
fa37d39e
SP
3451 if (retry < 5)
3452 break;
8db9d77b
ZW
3453 }
3454 if (i == 4)
5eddb70b 3455 DRM_ERROR("FDI train 1 fail!\n");
8db9d77b
ZW
3456
3457 /* Train 2 */
5eddb70b
CW
3458 reg = FDI_TX_CTL(pipe);
3459 temp = I915_READ(reg);
8db9d77b
ZW
3460 temp &= ~FDI_LINK_TRAIN_NONE;
3461 temp |= FDI_LINK_TRAIN_PATTERN_2;
3462 if (IS_GEN6(dev)) {
3463 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3464 /* SNB-B */
3465 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
3466 }
5eddb70b 3467 I915_WRITE(reg, temp);
8db9d77b 3468
5eddb70b
CW
3469 reg = FDI_RX_CTL(pipe);
3470 temp = I915_READ(reg);
8db9d77b
ZW
3471 if (HAS_PCH_CPT(dev)) {
3472 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3473 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
3474 } else {
3475 temp &= ~FDI_LINK_TRAIN_NONE;
3476 temp |= FDI_LINK_TRAIN_PATTERN_2;
3477 }
5eddb70b
CW
3478 I915_WRITE(reg, temp);
3479
3480 POSTING_READ(reg);
8db9d77b
ZW
3481 udelay(150);
3482
0206e353 3483 for (i = 0; i < 4; i++) {
5eddb70b
CW
3484 reg = FDI_TX_CTL(pipe);
3485 temp = I915_READ(reg);
8db9d77b
ZW
3486 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3487 temp |= snb_b_fdi_train_param[i];
5eddb70b
CW
3488 I915_WRITE(reg, temp);
3489
3490 POSTING_READ(reg);
8db9d77b
ZW
3491 udelay(500);
3492
fa37d39e
SP
3493 for (retry = 0; retry < 5; retry++) {
3494 reg = FDI_RX_IIR(pipe);
3495 temp = I915_READ(reg);
3496 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3497 if (temp & FDI_RX_SYMBOL_LOCK) {
3498 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3499 DRM_DEBUG_KMS("FDI train 2 done.\n");
3500 break;
3501 }
3502 udelay(50);
8db9d77b 3503 }
fa37d39e
SP
3504 if (retry < 5)
3505 break;
8db9d77b
ZW
3506 }
3507 if (i == 4)
5eddb70b 3508 DRM_ERROR("FDI train 2 fail!\n");
8db9d77b
ZW
3509
3510 DRM_DEBUG_KMS("FDI train done.\n");
3511}
3512
357555c0
JB
3513/* Manual link training for Ivy Bridge A0 parts */
3514static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
3515{
3516 struct drm_device *dev = crtc->dev;
fac5e23e 3517 struct drm_i915_private *dev_priv = to_i915(dev);
357555c0
JB
3518 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3519 int pipe = intel_crtc->pipe;
f0f59a00
VS
3520 i915_reg_t reg;
3521 u32 temp, i, j;
357555c0
JB
3522
3523 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3524 for train result */
3525 reg = FDI_RX_IMR(pipe);
3526 temp = I915_READ(reg);
3527 temp &= ~FDI_RX_SYMBOL_LOCK;
3528 temp &= ~FDI_RX_BIT_LOCK;
3529 I915_WRITE(reg, temp);
3530
3531 POSTING_READ(reg);
3532 udelay(150);
3533
01a415fd
DV
3534 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
3535 I915_READ(FDI_RX_IIR(pipe)));
3536
139ccd3f
JB
3537 /* Try each vswing and preemphasis setting twice before moving on */
3538 for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
3539 /* disable first in case we need to retry */
3540 reg = FDI_TX_CTL(pipe);
3541 temp = I915_READ(reg);
3542 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
3543 temp &= ~FDI_TX_ENABLE;
3544 I915_WRITE(reg, temp);
357555c0 3545
139ccd3f
JB
3546 reg = FDI_RX_CTL(pipe);
3547 temp = I915_READ(reg);
3548 temp &= ~FDI_LINK_TRAIN_AUTO;
3549 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3550 temp &= ~FDI_RX_ENABLE;
3551 I915_WRITE(reg, temp);
357555c0 3552
139ccd3f 3553 /* enable CPU FDI TX and PCH FDI RX */
357555c0
JB
3554 reg = FDI_TX_CTL(pipe);
3555 temp = I915_READ(reg);
139ccd3f 3556 temp &= ~FDI_DP_PORT_WIDTH_MASK;
6e3c9717 3557 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
139ccd3f 3558 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
357555c0 3559 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
139ccd3f
JB
3560 temp |= snb_b_fdi_train_param[j/2];
3561 temp |= FDI_COMPOSITE_SYNC;
3562 I915_WRITE(reg, temp | FDI_TX_ENABLE);
357555c0 3563
139ccd3f
JB
3564 I915_WRITE(FDI_RX_MISC(pipe),
3565 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
357555c0 3566
139ccd3f 3567 reg = FDI_RX_CTL(pipe);
357555c0 3568 temp = I915_READ(reg);
139ccd3f
JB
3569 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3570 temp |= FDI_COMPOSITE_SYNC;
3571 I915_WRITE(reg, temp | FDI_RX_ENABLE);
357555c0 3572
139ccd3f
JB
3573 POSTING_READ(reg);
3574 udelay(1); /* should be 0.5us */
357555c0 3575
139ccd3f
JB
3576 for (i = 0; i < 4; i++) {
3577 reg = FDI_RX_IIR(pipe);
3578 temp = I915_READ(reg);
3579 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
357555c0 3580
139ccd3f
JB
3581 if (temp & FDI_RX_BIT_LOCK ||
3582 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
3583 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3584 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
3585 i);
3586 break;
3587 }
3588 udelay(1); /* should be 0.5us */
3589 }
3590 if (i == 4) {
3591 DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
3592 continue;
3593 }
357555c0 3594
139ccd3f 3595 /* Train 2 */
357555c0
JB
3596 reg = FDI_TX_CTL(pipe);
3597 temp = I915_READ(reg);
139ccd3f
JB
3598 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3599 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
3600 I915_WRITE(reg, temp);
3601
3602 reg = FDI_RX_CTL(pipe);
3603 temp = I915_READ(reg);
3604 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3605 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
357555c0
JB
3606 I915_WRITE(reg, temp);
3607
3608 POSTING_READ(reg);
139ccd3f 3609 udelay(2); /* should be 1.5us */
357555c0 3610
139ccd3f
JB
3611 for (i = 0; i < 4; i++) {
3612 reg = FDI_RX_IIR(pipe);
3613 temp = I915_READ(reg);
3614 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
357555c0 3615
139ccd3f
JB
3616 if (temp & FDI_RX_SYMBOL_LOCK ||
3617 (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
3618 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3619 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
3620 i);
3621 goto train_done;
3622 }
3623 udelay(2); /* should be 1.5us */
357555c0 3624 }
139ccd3f
JB
3625 if (i == 4)
3626 DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
357555c0 3627 }
357555c0 3628
139ccd3f 3629train_done:
357555c0
JB
3630 DRM_DEBUG_KMS("FDI train done.\n");
3631}
3632
88cefb6c 3633static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
2c07245f 3634{
88cefb6c 3635 struct drm_device *dev = intel_crtc->base.dev;
fac5e23e 3636 struct drm_i915_private *dev_priv = to_i915(dev);
2c07245f 3637 int pipe = intel_crtc->pipe;
f0f59a00
VS
3638 i915_reg_t reg;
3639 u32 temp;
c64e311e 3640
c98e9dcf 3641 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
5eddb70b
CW
3642 reg = FDI_RX_CTL(pipe);
3643 temp = I915_READ(reg);
627eb5a3 3644 temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
6e3c9717 3645 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
dfd07d72 3646 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
5eddb70b
CW
3647 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
3648
3649 POSTING_READ(reg);
c98e9dcf
JB
3650 udelay(200);
3651
3652 /* Switch from Rawclk to PCDclk */
5eddb70b
CW
3653 temp = I915_READ(reg);
3654 I915_WRITE(reg, temp | FDI_PCDCLK);
3655
3656 POSTING_READ(reg);
c98e9dcf
JB
3657 udelay(200);
3658
20749730
PZ
3659 /* Enable CPU FDI TX PLL, always on for Ironlake */
3660 reg = FDI_TX_CTL(pipe);
3661 temp = I915_READ(reg);
3662 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
3663 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
5eddb70b 3664
20749730
PZ
3665 POSTING_READ(reg);
3666 udelay(100);
6be4a607 3667 }
0e23b99d
JB
3668}
3669
88cefb6c
DV
3670static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
3671{
3672 struct drm_device *dev = intel_crtc->base.dev;
fac5e23e 3673 struct drm_i915_private *dev_priv = to_i915(dev);
88cefb6c 3674 int pipe = intel_crtc->pipe;
f0f59a00
VS
3675 i915_reg_t reg;
3676 u32 temp;
88cefb6c
DV
3677
3678 /* Switch from PCDclk to Rawclk */
3679 reg = FDI_RX_CTL(pipe);
3680 temp = I915_READ(reg);
3681 I915_WRITE(reg, temp & ~FDI_PCDCLK);
3682
3683 /* Disable CPU FDI TX PLL */
3684 reg = FDI_TX_CTL(pipe);
3685 temp = I915_READ(reg);
3686 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
3687
3688 POSTING_READ(reg);
3689 udelay(100);
3690
3691 reg = FDI_RX_CTL(pipe);
3692 temp = I915_READ(reg);
3693 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
3694
3695 /* Wait for the clocks to turn off. */
3696 POSTING_READ(reg);
3697 udelay(100);
3698}
3699
0fc932b8
JB
3700static void ironlake_fdi_disable(struct drm_crtc *crtc)
3701{
3702 struct drm_device *dev = crtc->dev;
fac5e23e 3703 struct drm_i915_private *dev_priv = to_i915(dev);
0fc932b8
JB
3704 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3705 int pipe = intel_crtc->pipe;
f0f59a00
VS
3706 i915_reg_t reg;
3707 u32 temp;
0fc932b8
JB
3708
3709 /* disable CPU FDI tx and PCH FDI rx */
3710 reg = FDI_TX_CTL(pipe);
3711 temp = I915_READ(reg);
3712 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
3713 POSTING_READ(reg);
3714
3715 reg = FDI_RX_CTL(pipe);
3716 temp = I915_READ(reg);
3717 temp &= ~(0x7 << 16);
dfd07d72 3718 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
0fc932b8
JB
3719 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
3720
3721 POSTING_READ(reg);
3722 udelay(100);
3723
3724 /* Ironlake workaround, disable clock pointer after downing FDI */
eba905b2 3725 if (HAS_PCH_IBX(dev))
6f06ce18 3726 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
0fc932b8
JB
3727
3728 /* still set train pattern 1 */
3729 reg = FDI_TX_CTL(pipe);
3730 temp = I915_READ(reg);
3731 temp &= ~FDI_LINK_TRAIN_NONE;
3732 temp |= FDI_LINK_TRAIN_PATTERN_1;
3733 I915_WRITE(reg, temp);
3734
3735 reg = FDI_RX_CTL(pipe);
3736 temp = I915_READ(reg);
3737 if (HAS_PCH_CPT(dev)) {
3738 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3739 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3740 } else {
3741 temp &= ~FDI_LINK_TRAIN_NONE;
3742 temp |= FDI_LINK_TRAIN_PATTERN_1;
3743 }
3744 /* BPC in FDI rx is consistent with that in PIPECONF */
3745 temp &= ~(0x07 << 16);
dfd07d72 3746 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
0fc932b8
JB
3747 I915_WRITE(reg, temp);
3748
3749 POSTING_READ(reg);
3750 udelay(100);
3751}
3752
5dce5b93
CW
3753bool intel_has_pending_fb_unpin(struct drm_device *dev)
3754{
3755 struct intel_crtc *crtc;
3756
3757 /* Note that we don't need to be called with mode_config.lock here
3758 * as our list of CRTC objects is static for the lifetime of the
3759 * device and so cannot disappear as we iterate. Similarly, we can
3760 * happily treat the predicates as racy, atomic checks as userspace
3761 * cannot claim and pin a new fb without at least acquring the
3762 * struct_mutex and so serialising with us.
3763 */
d3fcc808 3764 for_each_intel_crtc(dev, crtc) {
5dce5b93
CW
3765 if (atomic_read(&crtc->unpin_work_count) == 0)
3766 continue;
3767
5a21b665 3768 if (crtc->flip_work)
5dce5b93
CW
3769 intel_wait_for_vblank(dev, crtc->pipe);
3770
3771 return true;
3772 }
3773
3774 return false;
3775}
3776
5a21b665 3777static void page_flip_completed(struct intel_crtc *intel_crtc)
d6bbafa1
CW
3778{
3779 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
5a21b665
DV
3780 struct intel_flip_work *work = intel_crtc->flip_work;
3781
3782 intel_crtc->flip_work = NULL;
d6bbafa1
CW
3783
3784 if (work->event)
560ce1dc 3785 drm_crtc_send_vblank_event(&intel_crtc->base, work->event);
d6bbafa1
CW
3786
3787 drm_crtc_vblank_put(&intel_crtc->base);
3788
5a21b665 3789 wake_up_all(&dev_priv->pending_flip_queue);
143f73b3 3790 queue_work(dev_priv->wq, &work->unpin_work);
5a21b665
DV
3791
3792 trace_i915_flip_complete(intel_crtc->plane,
3793 work->pending_flip_obj);
d6bbafa1
CW
3794}
3795
5008e874 3796static int intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
e6c3a2a6 3797{
0f91128d 3798 struct drm_device *dev = crtc->dev;
fac5e23e 3799 struct drm_i915_private *dev_priv = to_i915(dev);
5008e874 3800 long ret;
e6c3a2a6 3801
2c10d571 3802 WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
5008e874
ML
3803
3804 ret = wait_event_interruptible_timeout(
3805 dev_priv->pending_flip_queue,
3806 !intel_crtc_has_pending_flip(crtc),
3807 60*HZ);
3808
3809 if (ret < 0)
3810 return ret;
3811
5a21b665
DV
3812 if (ret == 0) {
3813 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3814 struct intel_flip_work *work;
3815
3816 spin_lock_irq(&dev->event_lock);
3817 work = intel_crtc->flip_work;
3818 if (work && !is_mmio_work(work)) {
3819 WARN_ONCE(1, "Removing stuck page flip\n");
3820 page_flip_completed(intel_crtc);
3821 }
3822 spin_unlock_irq(&dev->event_lock);
3823 }
5bb61643 3824
5008e874 3825 return 0;
e6c3a2a6
CW
3826}
3827
060f02d8
VS
3828static void lpt_disable_iclkip(struct drm_i915_private *dev_priv)
3829{
3830 u32 temp;
3831
3832 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
3833
3834 mutex_lock(&dev_priv->sb_lock);
3835
3836 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
3837 temp |= SBI_SSCCTL_DISABLE;
3838 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
3839
3840 mutex_unlock(&dev_priv->sb_lock);
3841}
3842
e615efe4
ED
3843/* Program iCLKIP clock to the desired frequency */
3844static void lpt_program_iclkip(struct drm_crtc *crtc)
3845{
64b46a06 3846 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
6e3c9717 3847 int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
e615efe4
ED
3848 u32 divsel, phaseinc, auxdiv, phasedir = 0;
3849 u32 temp;
3850
060f02d8 3851 lpt_disable_iclkip(dev_priv);
e615efe4 3852
64b46a06
VS
3853 /* The iCLK virtual clock root frequency is in MHz,
3854 * but the adjusted_mode->crtc_clock in in KHz. To get the
3855 * divisors, it is necessary to divide one by another, so we
3856 * convert the virtual clock precision to KHz here for higher
3857 * precision.
3858 */
3859 for (auxdiv = 0; auxdiv < 2; auxdiv++) {
e615efe4
ED
3860 u32 iclk_virtual_root_freq = 172800 * 1000;
3861 u32 iclk_pi_range = 64;
64b46a06 3862 u32 desired_divisor;
e615efe4 3863
64b46a06
VS
3864 desired_divisor = DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
3865 clock << auxdiv);
3866 divsel = (desired_divisor / iclk_pi_range) - 2;
3867 phaseinc = desired_divisor % iclk_pi_range;
e615efe4 3868
64b46a06
VS
3869 /*
3870 * Near 20MHz is a corner case which is
3871 * out of range for the 7-bit divisor
3872 */
3873 if (divsel <= 0x7f)
3874 break;
e615efe4
ED
3875 }
3876
3877 /* This should not happen with any sane values */
3878 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
3879 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
3880 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
3881 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
3882
3883 DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
12d7ceed 3884 clock,
e615efe4
ED
3885 auxdiv,
3886 divsel,
3887 phasedir,
3888 phaseinc);
3889
060f02d8
VS
3890 mutex_lock(&dev_priv->sb_lock);
3891
e615efe4 3892 /* Program SSCDIVINTPHASE6 */
988d6ee8 3893 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
e615efe4
ED
3894 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
3895 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
3896 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
3897 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
3898 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
3899 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
988d6ee8 3900 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
e615efe4
ED
3901
3902 /* Program SSCAUXDIV */
988d6ee8 3903 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
e615efe4
ED
3904 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
3905 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
988d6ee8 3906 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
e615efe4
ED
3907
3908 /* Enable modulator and associated divider */
988d6ee8 3909 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
e615efe4 3910 temp &= ~SBI_SSCCTL_DISABLE;
988d6ee8 3911 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
e615efe4 3912
060f02d8
VS
3913 mutex_unlock(&dev_priv->sb_lock);
3914
e615efe4
ED
3915 /* Wait for initialization time */
3916 udelay(24);
3917
3918 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
3919}
3920
8802e5b6
VS
3921int lpt_get_iclkip(struct drm_i915_private *dev_priv)
3922{
3923 u32 divsel, phaseinc, auxdiv;
3924 u32 iclk_virtual_root_freq = 172800 * 1000;
3925 u32 iclk_pi_range = 64;
3926 u32 desired_divisor;
3927 u32 temp;
3928
3929 if ((I915_READ(PIXCLK_GATE) & PIXCLK_GATE_UNGATE) == 0)
3930 return 0;
3931
3932 mutex_lock(&dev_priv->sb_lock);
3933
3934 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
3935 if (temp & SBI_SSCCTL_DISABLE) {
3936 mutex_unlock(&dev_priv->sb_lock);
3937 return 0;
3938 }
3939
3940 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
3941 divsel = (temp & SBI_SSCDIVINTPHASE_DIVSEL_MASK) >>
3942 SBI_SSCDIVINTPHASE_DIVSEL_SHIFT;
3943 phaseinc = (temp & SBI_SSCDIVINTPHASE_INCVAL_MASK) >>
3944 SBI_SSCDIVINTPHASE_INCVAL_SHIFT;
3945
3946 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
3947 auxdiv = (temp & SBI_SSCAUXDIV_FINALDIV2SEL_MASK) >>
3948 SBI_SSCAUXDIV_FINALDIV2SEL_SHIFT;
3949
3950 mutex_unlock(&dev_priv->sb_lock);
3951
3952 desired_divisor = (divsel + 2) * iclk_pi_range + phaseinc;
3953
3954 return DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
3955 desired_divisor << auxdiv);
3956}
3957
275f01b2
DV
3958static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
3959 enum pipe pch_transcoder)
3960{
3961 struct drm_device *dev = crtc->base.dev;
fac5e23e 3962 struct drm_i915_private *dev_priv = to_i915(dev);
6e3c9717 3963 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
275f01b2
DV
3964
3965 I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
3966 I915_READ(HTOTAL(cpu_transcoder)));
3967 I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
3968 I915_READ(HBLANK(cpu_transcoder)));
3969 I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
3970 I915_READ(HSYNC(cpu_transcoder)));
3971
3972 I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
3973 I915_READ(VTOTAL(cpu_transcoder)));
3974 I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
3975 I915_READ(VBLANK(cpu_transcoder)));
3976 I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
3977 I915_READ(VSYNC(cpu_transcoder)));
3978 I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
3979 I915_READ(VSYNCSHIFT(cpu_transcoder)));
3980}
3981
003632d9 3982static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
1fbc0d78 3983{
fac5e23e 3984 struct drm_i915_private *dev_priv = to_i915(dev);
1fbc0d78
DV
3985 uint32_t temp;
3986
3987 temp = I915_READ(SOUTH_CHICKEN1);
003632d9 3988 if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
1fbc0d78
DV
3989 return;
3990
3991 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
3992 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
3993
003632d9
ACO
3994 temp &= ~FDI_BC_BIFURCATION_SELECT;
3995 if (enable)
3996 temp |= FDI_BC_BIFURCATION_SELECT;
3997
3998 DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
1fbc0d78
DV
3999 I915_WRITE(SOUTH_CHICKEN1, temp);
4000 POSTING_READ(SOUTH_CHICKEN1);
4001}
4002
4003static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
4004{
4005 struct drm_device *dev = intel_crtc->base.dev;
1fbc0d78
DV
4006
4007 switch (intel_crtc->pipe) {
4008 case PIPE_A:
4009 break;
4010 case PIPE_B:
6e3c9717 4011 if (intel_crtc->config->fdi_lanes > 2)
003632d9 4012 cpt_set_fdi_bc_bifurcation(dev, false);
1fbc0d78 4013 else
003632d9 4014 cpt_set_fdi_bc_bifurcation(dev, true);
1fbc0d78
DV
4015
4016 break;
4017 case PIPE_C:
003632d9 4018 cpt_set_fdi_bc_bifurcation(dev, true);
1fbc0d78
DV
4019
4020 break;
4021 default:
4022 BUG();
4023 }
4024}
4025
c48b5305
VS
4026/* Return which DP Port should be selected for Transcoder DP control */
4027static enum port
4028intel_trans_dp_port_sel(struct drm_crtc *crtc)
4029{
4030 struct drm_device *dev = crtc->dev;
4031 struct intel_encoder *encoder;
4032
4033 for_each_encoder_on_crtc(dev, crtc, encoder) {
cca0502b 4034 if (encoder->type == INTEL_OUTPUT_DP ||
c48b5305
VS
4035 encoder->type == INTEL_OUTPUT_EDP)
4036 return enc_to_dig_port(&encoder->base)->port;
4037 }
4038
4039 return -1;
4040}
4041
f67a559d
JB
4042/*
4043 * Enable PCH resources required for PCH ports:
4044 * - PCH PLLs
4045 * - FDI training & RX/TX
4046 * - update transcoder timings
4047 * - DP transcoding bits
4048 * - transcoder
4049 */
4050static void ironlake_pch_enable(struct drm_crtc *crtc)
0e23b99d
JB
4051{
4052 struct drm_device *dev = crtc->dev;
fac5e23e 4053 struct drm_i915_private *dev_priv = to_i915(dev);
0e23b99d
JB
4054 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4055 int pipe = intel_crtc->pipe;
f0f59a00 4056 u32 temp;
2c07245f 4057
ab9412ba 4058 assert_pch_transcoder_disabled(dev_priv, pipe);
e7e164db 4059
1fbc0d78
DV
4060 if (IS_IVYBRIDGE(dev))
4061 ivybridge_update_fdi_bc_bifurcation(intel_crtc);
4062
cd986abb
DV
4063 /* Write the TU size bits before fdi link training, so that error
4064 * detection works. */
4065 I915_WRITE(FDI_RX_TUSIZE1(pipe),
4066 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
4067
c98e9dcf 4068 /* For PCH output, training FDI link */
674cf967 4069 dev_priv->display.fdi_link_train(crtc);
2c07245f 4070
3ad8a208
DV
4071 /* We need to program the right clock selection before writing the pixel
4072 * mutliplier into the DPLL. */
303b81e0 4073 if (HAS_PCH_CPT(dev)) {
ee7b9f93 4074 u32 sel;
4b645f14 4075
c98e9dcf 4076 temp = I915_READ(PCH_DPLL_SEL);
11887397
DV
4077 temp |= TRANS_DPLL_ENABLE(pipe);
4078 sel = TRANS_DPLLB_SEL(pipe);
8106ddbd
ACO
4079 if (intel_crtc->config->shared_dpll ==
4080 intel_get_shared_dpll_by_id(dev_priv, DPLL_ID_PCH_PLL_B))
ee7b9f93
JB
4081 temp |= sel;
4082 else
4083 temp &= ~sel;
c98e9dcf 4084 I915_WRITE(PCH_DPLL_SEL, temp);
c98e9dcf 4085 }
5eddb70b 4086
3ad8a208
DV
4087 /* XXX: pch pll's can be enabled any time before we enable the PCH
4088 * transcoder, and we actually should do this to not upset any PCH
4089 * transcoder that already use the clock when we share it.
4090 *
4091 * Note that enable_shared_dpll tries to do the right thing, but
4092 * get_shared_dpll unconditionally resets the pll - we need that to have
4093 * the right LVDS enable sequence. */
85b3894f 4094 intel_enable_shared_dpll(intel_crtc);
3ad8a208 4095
d9b6cb56
JB
4096 /* set transcoder timing, panel must allow it */
4097 assert_panel_unlocked(dev_priv, pipe);
275f01b2 4098 ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
8db9d77b 4099
303b81e0 4100 intel_fdi_normal_train(crtc);
5e84e1a4 4101
c98e9dcf 4102 /* For PCH DP, enable TRANS_DP_CTL */
37a5650b 4103 if (HAS_PCH_CPT(dev) && intel_crtc_has_dp_encoder(intel_crtc->config)) {
9c4edaee
VS
4104 const struct drm_display_mode *adjusted_mode =
4105 &intel_crtc->config->base.adjusted_mode;
dfd07d72 4106 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
f0f59a00 4107 i915_reg_t reg = TRANS_DP_CTL(pipe);
5eddb70b
CW
4108 temp = I915_READ(reg);
4109 temp &= ~(TRANS_DP_PORT_SEL_MASK |
220cad3c
EA
4110 TRANS_DP_SYNC_MASK |
4111 TRANS_DP_BPC_MASK);
e3ef4479 4112 temp |= TRANS_DP_OUTPUT_ENABLE;
9325c9f0 4113 temp |= bpc << 9; /* same format but at 11:9 */
c98e9dcf 4114
9c4edaee 4115 if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
5eddb70b 4116 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
9c4edaee 4117 if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
5eddb70b 4118 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
c98e9dcf
JB
4119
4120 switch (intel_trans_dp_port_sel(crtc)) {
c48b5305 4121 case PORT_B:
5eddb70b 4122 temp |= TRANS_DP_PORT_SEL_B;
c98e9dcf 4123 break;
c48b5305 4124 case PORT_C:
5eddb70b 4125 temp |= TRANS_DP_PORT_SEL_C;
c98e9dcf 4126 break;
c48b5305 4127 case PORT_D:
5eddb70b 4128 temp |= TRANS_DP_PORT_SEL_D;
c98e9dcf
JB
4129 break;
4130 default:
e95d41e1 4131 BUG();
32f9d658 4132 }
2c07245f 4133
5eddb70b 4134 I915_WRITE(reg, temp);
6be4a607 4135 }
b52eb4dc 4136
b8a4f404 4137 ironlake_enable_pch_transcoder(dev_priv, pipe);
f67a559d
JB
4138}
4139
1507e5bd
PZ
4140static void lpt_pch_enable(struct drm_crtc *crtc)
4141{
4142 struct drm_device *dev = crtc->dev;
fac5e23e 4143 struct drm_i915_private *dev_priv = to_i915(dev);
1507e5bd 4144 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6e3c9717 4145 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
1507e5bd 4146
ab9412ba 4147 assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
1507e5bd 4148
8c52b5e8 4149 lpt_program_iclkip(crtc);
1507e5bd 4150
0540e488 4151 /* Set transcoder timing. */
275f01b2 4152 ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
1507e5bd 4153
937bb610 4154 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
f67a559d
JB
4155}
4156
a1520318 4157static void cpt_verify_modeset(struct drm_device *dev, int pipe)
d4270e57 4158{
fac5e23e 4159 struct drm_i915_private *dev_priv = to_i915(dev);
f0f59a00 4160 i915_reg_t dslreg = PIPEDSL(pipe);
d4270e57
JB
4161 u32 temp;
4162
4163 temp = I915_READ(dslreg);
4164 udelay(500);
4165 if (wait_for(I915_READ(dslreg) != temp, 5)) {
d4270e57 4166 if (wait_for(I915_READ(dslreg) != temp, 5))
84f44ce7 4167 DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
d4270e57
JB
4168 }
4169}
4170
86adf9d7
ML
4171static int
4172skl_update_scaler(struct intel_crtc_state *crtc_state, bool force_detach,
4173 unsigned scaler_user, int *scaler_id, unsigned int rotation,
4174 int src_w, int src_h, int dst_w, int dst_h)
a1b2278e 4175{
86adf9d7
ML
4176 struct intel_crtc_scaler_state *scaler_state =
4177 &crtc_state->scaler_state;
4178 struct intel_crtc *intel_crtc =
4179 to_intel_crtc(crtc_state->base.crtc);
a1b2278e 4180 int need_scaling;
6156a456
CK
4181
4182 need_scaling = intel_rotation_90_or_270(rotation) ?
4183 (src_h != dst_w || src_w != dst_h):
4184 (src_w != dst_w || src_h != dst_h);
a1b2278e
CK
4185
4186 /*
4187 * if plane is being disabled or scaler is no more required or force detach
4188 * - free scaler binded to this plane/crtc
4189 * - in order to do this, update crtc->scaler_usage
4190 *
4191 * Here scaler state in crtc_state is set free so that
4192 * scaler can be assigned to other user. Actual register
4193 * update to free the scaler is done in plane/panel-fit programming.
4194 * For this purpose crtc/plane_state->scaler_id isn't reset here.
4195 */
86adf9d7 4196 if (force_detach || !need_scaling) {
a1b2278e 4197 if (*scaler_id >= 0) {
86adf9d7 4198 scaler_state->scaler_users &= ~(1 << scaler_user);
a1b2278e
CK
4199 scaler_state->scalers[*scaler_id].in_use = 0;
4200
86adf9d7
ML
4201 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4202 "Staged freeing scaler id %d scaler_users = 0x%x\n",
4203 intel_crtc->pipe, scaler_user, *scaler_id,
a1b2278e
CK
4204 scaler_state->scaler_users);
4205 *scaler_id = -1;
4206 }
4207 return 0;
4208 }
4209
4210 /* range checks */
4211 if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
4212 dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
4213
4214 src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
4215 dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
86adf9d7 4216 DRM_DEBUG_KMS("scaler_user index %u.%u: src %ux%u dst %ux%u "
a1b2278e 4217 "size is out of scaler range\n",
86adf9d7 4218 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h);
a1b2278e
CK
4219 return -EINVAL;
4220 }
4221
86adf9d7
ML
4222 /* mark this plane as a scaler user in crtc_state */
4223 scaler_state->scaler_users |= (1 << scaler_user);
4224 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4225 "staged scaling request for %ux%u->%ux%u scaler_users = 0x%x\n",
4226 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h,
4227 scaler_state->scaler_users);
4228
4229 return 0;
4230}
4231
4232/**
4233 * skl_update_scaler_crtc - Stages update to scaler state for a given crtc.
4234 *
4235 * @state: crtc's scaler state
86adf9d7
ML
4236 *
4237 * Return
4238 * 0 - scaler_usage updated successfully
4239 * error - requested scaling cannot be supported or other error condition
4240 */
e435d6e5 4241int skl_update_scaler_crtc(struct intel_crtc_state *state)
86adf9d7
ML
4242{
4243 struct intel_crtc *intel_crtc = to_intel_crtc(state->base.crtc);
7c5f93b0 4244 const struct drm_display_mode *adjusted_mode = &state->base.adjusted_mode;
86adf9d7 4245
78108b7c
VS
4246 DRM_DEBUG_KMS("Updating scaler for [CRTC:%d:%s] scaler_user index %u.%u\n",
4247 intel_crtc->base.base.id, intel_crtc->base.name,
4248 intel_crtc->pipe, SKL_CRTC_INDEX);
86adf9d7 4249
e435d6e5 4250 return skl_update_scaler(state, !state->base.active, SKL_CRTC_INDEX,
fa5a7970 4251 &state->scaler_state.scaler_id, BIT(DRM_ROTATE_0),
86adf9d7 4252 state->pipe_src_w, state->pipe_src_h,
aad941d5 4253 adjusted_mode->crtc_hdisplay, adjusted_mode->crtc_vdisplay);
86adf9d7
ML
4254}
4255
4256/**
4257 * skl_update_scaler_plane - Stages update to scaler state for a given plane.
4258 *
4259 * @state: crtc's scaler state
86adf9d7
ML
4260 * @plane_state: atomic plane state to update
4261 *
4262 * Return
4263 * 0 - scaler_usage updated successfully
4264 * error - requested scaling cannot be supported or other error condition
4265 */
da20eabd
ML
4266static int skl_update_scaler_plane(struct intel_crtc_state *crtc_state,
4267 struct intel_plane_state *plane_state)
86adf9d7
ML
4268{
4269
4270 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
da20eabd
ML
4271 struct intel_plane *intel_plane =
4272 to_intel_plane(plane_state->base.plane);
86adf9d7
ML
4273 struct drm_framebuffer *fb = plane_state->base.fb;
4274 int ret;
4275
4276 bool force_detach = !fb || !plane_state->visible;
4277
72660ce0
VS
4278 DRM_DEBUG_KMS("Updating scaler for [PLANE:%d:%s] scaler_user index %u.%u\n",
4279 intel_plane->base.base.id, intel_plane->base.name,
4280 intel_crtc->pipe, drm_plane_index(&intel_plane->base));
86adf9d7
ML
4281
4282 ret = skl_update_scaler(crtc_state, force_detach,
4283 drm_plane_index(&intel_plane->base),
4284 &plane_state->scaler_id,
4285 plane_state->base.rotation,
4286 drm_rect_width(&plane_state->src) >> 16,
4287 drm_rect_height(&plane_state->src) >> 16,
4288 drm_rect_width(&plane_state->dst),
4289 drm_rect_height(&plane_state->dst));
4290
4291 if (ret || plane_state->scaler_id < 0)
4292 return ret;
4293
a1b2278e 4294 /* check colorkey */
818ed961 4295 if (plane_state->ckey.flags != I915_SET_COLORKEY_NONE) {
72660ce0
VS
4296 DRM_DEBUG_KMS("[PLANE:%d:%s] scaling with color key not allowed",
4297 intel_plane->base.base.id,
4298 intel_plane->base.name);
a1b2278e
CK
4299 return -EINVAL;
4300 }
4301
4302 /* Check src format */
86adf9d7
ML
4303 switch (fb->pixel_format) {
4304 case DRM_FORMAT_RGB565:
4305 case DRM_FORMAT_XBGR8888:
4306 case DRM_FORMAT_XRGB8888:
4307 case DRM_FORMAT_ABGR8888:
4308 case DRM_FORMAT_ARGB8888:
4309 case DRM_FORMAT_XRGB2101010:
4310 case DRM_FORMAT_XBGR2101010:
4311 case DRM_FORMAT_YUYV:
4312 case DRM_FORMAT_YVYU:
4313 case DRM_FORMAT_UYVY:
4314 case DRM_FORMAT_VYUY:
4315 break;
4316 default:
72660ce0
VS
4317 DRM_DEBUG_KMS("[PLANE:%d:%s] FB:%d unsupported scaling format 0x%x\n",
4318 intel_plane->base.base.id, intel_plane->base.name,
4319 fb->base.id, fb->pixel_format);
86adf9d7 4320 return -EINVAL;
a1b2278e
CK
4321 }
4322
a1b2278e
CK
4323 return 0;
4324}
4325
e435d6e5
ML
4326static void skylake_scaler_disable(struct intel_crtc *crtc)
4327{
4328 int i;
4329
4330 for (i = 0; i < crtc->num_scalers; i++)
4331 skl_detach_scaler(crtc, i);
4332}
4333
4334static void skylake_pfit_enable(struct intel_crtc *crtc)
bd2e244f
JB
4335{
4336 struct drm_device *dev = crtc->base.dev;
fac5e23e 4337 struct drm_i915_private *dev_priv = to_i915(dev);
bd2e244f 4338 int pipe = crtc->pipe;
a1b2278e
CK
4339 struct intel_crtc_scaler_state *scaler_state =
4340 &crtc->config->scaler_state;
4341
4342 DRM_DEBUG_KMS("for crtc_state = %p\n", crtc->config);
4343
6e3c9717 4344 if (crtc->config->pch_pfit.enabled) {
a1b2278e
CK
4345 int id;
4346
4347 if (WARN_ON(crtc->config->scaler_state.scaler_id < 0)) {
4348 DRM_ERROR("Requesting pfit without getting a scaler first\n");
4349 return;
4350 }
4351
4352 id = scaler_state->scaler_id;
4353 I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
4354 PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
4355 I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
4356 I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
4357
4358 DRM_DEBUG_KMS("for crtc_state = %p scaler_id = %d\n", crtc->config, id);
bd2e244f
JB
4359 }
4360}
4361
b074cec8
JB
4362static void ironlake_pfit_enable(struct intel_crtc *crtc)
4363{
4364 struct drm_device *dev = crtc->base.dev;
fac5e23e 4365 struct drm_i915_private *dev_priv = to_i915(dev);
b074cec8
JB
4366 int pipe = crtc->pipe;
4367
6e3c9717 4368 if (crtc->config->pch_pfit.enabled) {
b074cec8
JB
4369 /* Force use of hard-coded filter coefficients
4370 * as some pre-programmed values are broken,
4371 * e.g. x201.
4372 */
4373 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
4374 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
4375 PF_PIPE_SEL_IVB(pipe));
4376 else
4377 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
6e3c9717
ACO
4378 I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
4379 I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
d4270e57
JB
4380 }
4381}
4382
20bc8673 4383void hsw_enable_ips(struct intel_crtc *crtc)
d77e4531 4384{
cea165c3 4385 struct drm_device *dev = crtc->base.dev;
fac5e23e 4386 struct drm_i915_private *dev_priv = to_i915(dev);
d77e4531 4387
6e3c9717 4388 if (!crtc->config->ips_enabled)
d77e4531
PZ
4389 return;
4390
307e4498
ML
4391 /*
4392 * We can only enable IPS after we enable a plane and wait for a vblank
4393 * This function is called from post_plane_update, which is run after
4394 * a vblank wait.
4395 */
cea165c3 4396
d77e4531 4397 assert_plane_enabled(dev_priv, crtc->plane);
cea165c3 4398 if (IS_BROADWELL(dev)) {
2a114cc1
BW
4399 mutex_lock(&dev_priv->rps.hw_lock);
4400 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
4401 mutex_unlock(&dev_priv->rps.hw_lock);
4402 /* Quoting Art Runyan: "its not safe to expect any particular
4403 * value in IPS_CTL bit 31 after enabling IPS through the
e59150dc
JB
4404 * mailbox." Moreover, the mailbox may return a bogus state,
4405 * so we need to just enable it and continue on.
2a114cc1
BW
4406 */
4407 } else {
4408 I915_WRITE(IPS_CTL, IPS_ENABLE);
4409 /* The bit only becomes 1 in the next vblank, so this wait here
4410 * is essentially intel_wait_for_vblank. If we don't have this
4411 * and don't wait for vblanks until the end of crtc_enable, then
4412 * the HW state readout code will complain that the expected
4413 * IPS_CTL value is not the one we read. */
2ec9ba3c
CW
4414 if (intel_wait_for_register(dev_priv,
4415 IPS_CTL, IPS_ENABLE, IPS_ENABLE,
4416 50))
2a114cc1
BW
4417 DRM_ERROR("Timed out waiting for IPS enable\n");
4418 }
d77e4531
PZ
4419}
4420
20bc8673 4421void hsw_disable_ips(struct intel_crtc *crtc)
d77e4531
PZ
4422{
4423 struct drm_device *dev = crtc->base.dev;
fac5e23e 4424 struct drm_i915_private *dev_priv = to_i915(dev);
d77e4531 4425
6e3c9717 4426 if (!crtc->config->ips_enabled)
d77e4531
PZ
4427 return;
4428
4429 assert_plane_enabled(dev_priv, crtc->plane);
23d0b130 4430 if (IS_BROADWELL(dev)) {
2a114cc1
BW
4431 mutex_lock(&dev_priv->rps.hw_lock);
4432 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
4433 mutex_unlock(&dev_priv->rps.hw_lock);
23d0b130 4434 /* wait for pcode to finish disabling IPS, which may take up to 42ms */
b85c1ecf
CW
4435 if (intel_wait_for_register(dev_priv,
4436 IPS_CTL, IPS_ENABLE, 0,
4437 42))
23d0b130 4438 DRM_ERROR("Timed out waiting for IPS disable\n");
e59150dc 4439 } else {
2a114cc1 4440 I915_WRITE(IPS_CTL, 0);
e59150dc
JB
4441 POSTING_READ(IPS_CTL);
4442 }
d77e4531
PZ
4443
4444 /* We need to wait for a vblank before we can disable the plane. */
4445 intel_wait_for_vblank(dev, crtc->pipe);
4446}
4447
7cac945f 4448static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
d3eedb1a 4449{
7cac945f 4450 if (intel_crtc->overlay) {
d3eedb1a 4451 struct drm_device *dev = intel_crtc->base.dev;
fac5e23e 4452 struct drm_i915_private *dev_priv = to_i915(dev);
d3eedb1a
VS
4453
4454 mutex_lock(&dev->struct_mutex);
4455 dev_priv->mm.interruptible = false;
4456 (void) intel_overlay_switch_off(intel_crtc->overlay);
4457 dev_priv->mm.interruptible = true;
4458 mutex_unlock(&dev->struct_mutex);
4459 }
4460
4461 /* Let userspace switch the overlay on again. In most cases userspace
4462 * has to recompute where to put it anyway.
4463 */
4464}
4465
87d4300a
ML
4466/**
4467 * intel_post_enable_primary - Perform operations after enabling primary plane
4468 * @crtc: the CRTC whose primary plane was just enabled
4469 *
4470 * Performs potentially sleeping operations that must be done after the primary
4471 * plane is enabled, such as updating FBC and IPS. Note that this may be
4472 * called due to an explicit primary plane update, or due to an implicit
4473 * re-enable that is caused when a sprite plane is updated to no longer
4474 * completely hide the primary plane.
4475 */
4476static void
4477intel_post_enable_primary(struct drm_crtc *crtc)
a5c4d7bc
VS
4478{
4479 struct drm_device *dev = crtc->dev;
fac5e23e 4480 struct drm_i915_private *dev_priv = to_i915(dev);
a5c4d7bc
VS
4481 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4482 int pipe = intel_crtc->pipe;
a5c4d7bc 4483
87d4300a
ML
4484 /*
4485 * FIXME IPS should be fine as long as one plane is
4486 * enabled, but in practice it seems to have problems
4487 * when going from primary only to sprite only and vice
4488 * versa.
4489 */
a5c4d7bc
VS
4490 hsw_enable_ips(intel_crtc);
4491
f99d7069 4492 /*
87d4300a
ML
4493 * Gen2 reports pipe underruns whenever all planes are disabled.
4494 * So don't enable underrun reporting before at least some planes
4495 * are enabled.
4496 * FIXME: Need to fix the logic to work when we turn off all planes
4497 * but leave the pipe running.
f99d7069 4498 */
87d4300a
ML
4499 if (IS_GEN2(dev))
4500 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4501
aca7b684
VS
4502 /* Underruns don't always raise interrupts, so check manually. */
4503 intel_check_cpu_fifo_underruns(dev_priv);
4504 intel_check_pch_fifo_underruns(dev_priv);
a5c4d7bc
VS
4505}
4506
2622a081 4507/* FIXME move all this to pre_plane_update() with proper state tracking */
87d4300a
ML
4508static void
4509intel_pre_disable_primary(struct drm_crtc *crtc)
a5c4d7bc
VS
4510{
4511 struct drm_device *dev = crtc->dev;
fac5e23e 4512 struct drm_i915_private *dev_priv = to_i915(dev);
a5c4d7bc
VS
4513 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4514 int pipe = intel_crtc->pipe;
a5c4d7bc 4515
87d4300a
ML
4516 /*
4517 * Gen2 reports pipe underruns whenever all planes are disabled.
4518 * So diasble underrun reporting before all the planes get disabled.
4519 * FIXME: Need to fix the logic to work when we turn off all planes
4520 * but leave the pipe running.
4521 */
4522 if (IS_GEN2(dev))
4523 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
a5c4d7bc 4524
2622a081
VS
4525 /*
4526 * FIXME IPS should be fine as long as one plane is
4527 * enabled, but in practice it seems to have problems
4528 * when going from primary only to sprite only and vice
4529 * versa.
4530 */
4531 hsw_disable_ips(intel_crtc);
4532}
4533
4534/* FIXME get rid of this and use pre_plane_update */
4535static void
4536intel_pre_disable_primary_noatomic(struct drm_crtc *crtc)
4537{
4538 struct drm_device *dev = crtc->dev;
fac5e23e 4539 struct drm_i915_private *dev_priv = to_i915(dev);
2622a081
VS
4540 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4541 int pipe = intel_crtc->pipe;
4542
4543 intel_pre_disable_primary(crtc);
4544
87d4300a
ML
4545 /*
4546 * Vblank time updates from the shadow to live plane control register
4547 * are blocked if the memory self-refresh mode is active at that
4548 * moment. So to make sure the plane gets truly disabled, disable
4549 * first the self-refresh mode. The self-refresh enable bit in turn
4550 * will be checked/applied by the HW only at the next frame start
4551 * event which is after the vblank start event, so we need to have a
4552 * wait-for-vblank between disabling the plane and the pipe.
4553 */
262cd2e1 4554 if (HAS_GMCH_DISPLAY(dev)) {
87d4300a 4555 intel_set_memory_cxsr(dev_priv, false);
262cd2e1
VS
4556 dev_priv->wm.vlv.cxsr = false;
4557 intel_wait_for_vblank(dev, pipe);
4558 }
87d4300a
ML
4559}
4560
5a21b665
DV
4561static void intel_post_plane_update(struct intel_crtc_state *old_crtc_state)
4562{
4563 struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
4564 struct drm_atomic_state *old_state = old_crtc_state->base.state;
4565 struct intel_crtc_state *pipe_config =
4566 to_intel_crtc_state(crtc->base.state);
5a21b665
DV
4567 struct drm_plane *primary = crtc->base.primary;
4568 struct drm_plane_state *old_pri_state =
4569 drm_atomic_get_existing_plane_state(old_state, primary);
4570
5748b6a1 4571 intel_frontbuffer_flip(to_i915(crtc->base.dev), pipe_config->fb_bits);
5a21b665
DV
4572
4573 crtc->wm.cxsr_allowed = true;
4574
4575 if (pipe_config->update_wm_post && pipe_config->base.active)
4576 intel_update_watermarks(&crtc->base);
4577
4578 if (old_pri_state) {
4579 struct intel_plane_state *primary_state =
4580 to_intel_plane_state(primary->state);
4581 struct intel_plane_state *old_primary_state =
4582 to_intel_plane_state(old_pri_state);
4583
4584 intel_fbc_post_update(crtc);
4585
4586 if (primary_state->visible &&
4587 (needs_modeset(&pipe_config->base) ||
4588 !old_primary_state->visible))
4589 intel_post_enable_primary(&crtc->base);
4590 }
4591}
4592
5c74cd73 4593static void intel_pre_plane_update(struct intel_crtc_state *old_crtc_state)
ac21b225 4594{
5c74cd73 4595 struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
ac21b225 4596 struct drm_device *dev = crtc->base.dev;
fac5e23e 4597 struct drm_i915_private *dev_priv = to_i915(dev);
ab1d3a0e
ML
4598 struct intel_crtc_state *pipe_config =
4599 to_intel_crtc_state(crtc->base.state);
5c74cd73
ML
4600 struct drm_atomic_state *old_state = old_crtc_state->base.state;
4601 struct drm_plane *primary = crtc->base.primary;
4602 struct drm_plane_state *old_pri_state =
4603 drm_atomic_get_existing_plane_state(old_state, primary);
4604 bool modeset = needs_modeset(&pipe_config->base);
ac21b225 4605
5c74cd73
ML
4606 if (old_pri_state) {
4607 struct intel_plane_state *primary_state =
4608 to_intel_plane_state(primary->state);
4609 struct intel_plane_state *old_primary_state =
4610 to_intel_plane_state(old_pri_state);
4611
faf68d92 4612 intel_fbc_pre_update(crtc, pipe_config, primary_state);
31ae71fc 4613
5c74cd73
ML
4614 if (old_primary_state->visible &&
4615 (modeset || !primary_state->visible))
4616 intel_pre_disable_primary(&crtc->base);
4617 }
852eb00d 4618
a4015f9a 4619 if (pipe_config->disable_cxsr && HAS_GMCH_DISPLAY(dev)) {
852eb00d 4620 crtc->wm.cxsr_allowed = false;
2dfd178d 4621
2622a081
VS
4622 /*
4623 * Vblank time updates from the shadow to live plane control register
4624 * are blocked if the memory self-refresh mode is active at that
4625 * moment. So to make sure the plane gets truly disabled, disable
4626 * first the self-refresh mode. The self-refresh enable bit in turn
4627 * will be checked/applied by the HW only at the next frame start
4628 * event which is after the vblank start event, so we need to have a
4629 * wait-for-vblank between disabling the plane and the pipe.
4630 */
4631 if (old_crtc_state->base.active) {
2dfd178d 4632 intel_set_memory_cxsr(dev_priv, false);
2622a081
VS
4633 dev_priv->wm.vlv.cxsr = false;
4634 intel_wait_for_vblank(dev, crtc->pipe);
4635 }
852eb00d 4636 }
92826fcd 4637
ed4a6a7c
MR
4638 /*
4639 * IVB workaround: must disable low power watermarks for at least
4640 * one frame before enabling scaling. LP watermarks can be re-enabled
4641 * when scaling is disabled.
4642 *
4643 * WaCxSRDisabledForSpriteScaling:ivb
4644 */
4645 if (pipe_config->disable_lp_wm) {
4646 ilk_disable_lp_wm(dev);
4647 intel_wait_for_vblank(dev, crtc->pipe);
4648 }
4649
4650 /*
4651 * If we're doing a modeset, we're done. No need to do any pre-vblank
4652 * watermark programming here.
4653 */
4654 if (needs_modeset(&pipe_config->base))
4655 return;
4656
4657 /*
4658 * For platforms that support atomic watermarks, program the
4659 * 'intermediate' watermarks immediately. On pre-gen9 platforms, these
4660 * will be the intermediate values that are safe for both pre- and
4661 * post- vblank; when vblank happens, the 'active' values will be set
4662 * to the final 'target' values and we'll do this again to get the
4663 * optimal watermarks. For gen9+ platforms, the values we program here
4664 * will be the final target values which will get automatically latched
4665 * at vblank time; no further programming will be necessary.
4666 *
4667 * If a platform hasn't been transitioned to atomic watermarks yet,
4668 * we'll continue to update watermarks the old way, if flags tell
4669 * us to.
4670 */
4671 if (dev_priv->display.initial_watermarks != NULL)
4672 dev_priv->display.initial_watermarks(pipe_config);
caed361d 4673 else if (pipe_config->update_wm_pre)
92826fcd 4674 intel_update_watermarks(&crtc->base);
ac21b225
ML
4675}
4676
d032ffa0 4677static void intel_crtc_disable_planes(struct drm_crtc *crtc, unsigned plane_mask)
87d4300a
ML
4678{
4679 struct drm_device *dev = crtc->dev;
4680 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
d032ffa0 4681 struct drm_plane *p;
87d4300a
ML
4682 int pipe = intel_crtc->pipe;
4683
7cac945f 4684 intel_crtc_dpms_overlay_disable(intel_crtc);
27321ae8 4685
d032ffa0
ML
4686 drm_for_each_plane_mask(p, dev, plane_mask)
4687 to_intel_plane(p)->disable_plane(p, crtc);
f98551ae 4688
f99d7069
DV
4689 /*
4690 * FIXME: Once we grow proper nuclear flip support out of this we need
4691 * to compute the mask of flip planes precisely. For the time being
4692 * consider this a flip to a NULL plane.
4693 */
5748b6a1 4694 intel_frontbuffer_flip(to_i915(dev), INTEL_FRONTBUFFER_ALL_MASK(pipe));
a5c4d7bc
VS
4695}
4696
f67a559d
JB
4697static void ironlake_crtc_enable(struct drm_crtc *crtc)
4698{
4699 struct drm_device *dev = crtc->dev;
fac5e23e 4700 struct drm_i915_private *dev_priv = to_i915(dev);
f67a559d 4701 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 4702 struct intel_encoder *encoder;
f67a559d 4703 int pipe = intel_crtc->pipe;
b95c5321
ML
4704 struct intel_crtc_state *pipe_config =
4705 to_intel_crtc_state(crtc->state);
f67a559d 4706
53d9f4e9 4707 if (WARN_ON(intel_crtc->active))
f67a559d
JB
4708 return;
4709
b2c0593a
VS
4710 /*
4711 * Sometimes spurious CPU pipe underruns happen during FDI
4712 * training, at least with VGA+HDMI cloning. Suppress them.
4713 *
4714 * On ILK we get an occasional spurious CPU pipe underruns
4715 * between eDP port A enable and vdd enable. Also PCH port
4716 * enable seems to result in the occasional CPU pipe underrun.
4717 *
4718 * Spurious PCH underruns also occur during PCH enabling.
4719 */
4720 if (intel_crtc->config->has_pch_encoder || IS_GEN5(dev_priv))
4721 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
81b088ca
VS
4722 if (intel_crtc->config->has_pch_encoder)
4723 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
4724
6e3c9717 4725 if (intel_crtc->config->has_pch_encoder)
b14b1055
DV
4726 intel_prepare_shared_dpll(intel_crtc);
4727
37a5650b 4728 if (intel_crtc_has_dp_encoder(intel_crtc->config))
fe3cd48d 4729 intel_dp_set_m_n(intel_crtc, M1_N1);
29407aab
DV
4730
4731 intel_set_pipe_timings(intel_crtc);
bc58be60 4732 intel_set_pipe_src_size(intel_crtc);
29407aab 4733
6e3c9717 4734 if (intel_crtc->config->has_pch_encoder) {
29407aab 4735 intel_cpu_transcoder_set_m_n(intel_crtc,
6e3c9717 4736 &intel_crtc->config->fdi_m_n, NULL);
29407aab
DV
4737 }
4738
4739 ironlake_set_pipeconf(crtc);
4740
f67a559d 4741 intel_crtc->active = true;
8664281b 4742
f6736a1a 4743 for_each_encoder_on_crtc(dev, crtc, encoder)
952735ee
DV
4744 if (encoder->pre_enable)
4745 encoder->pre_enable(encoder);
f67a559d 4746
6e3c9717 4747 if (intel_crtc->config->has_pch_encoder) {
fff367c7
DV
4748 /* Note: FDI PLL enabling _must_ be done before we enable the
4749 * cpu pipes, hence this is separate from all the other fdi/pch
4750 * enabling. */
88cefb6c 4751 ironlake_fdi_pll_enable(intel_crtc);
46b6f814
DV
4752 } else {
4753 assert_fdi_tx_disabled(dev_priv, pipe);
4754 assert_fdi_rx_disabled(dev_priv, pipe);
4755 }
f67a559d 4756
b074cec8 4757 ironlake_pfit_enable(intel_crtc);
f67a559d 4758
9c54c0dd
JB
4759 /*
4760 * On ILK+ LUT must be loaded before the pipe is running but with
4761 * clocks enabled
4762 */
b95c5321 4763 intel_color_load_luts(&pipe_config->base);
9c54c0dd 4764
1d5bf5d9
ID
4765 if (dev_priv->display.initial_watermarks != NULL)
4766 dev_priv->display.initial_watermarks(intel_crtc->config);
e1fdc473 4767 intel_enable_pipe(intel_crtc);
f67a559d 4768
6e3c9717 4769 if (intel_crtc->config->has_pch_encoder)
f67a559d 4770 ironlake_pch_enable(crtc);
c98e9dcf 4771
f9b61ff6
DV
4772 assert_vblank_disabled(crtc);
4773 drm_crtc_vblank_on(crtc);
4774
fa5c73b1
DV
4775 for_each_encoder_on_crtc(dev, crtc, encoder)
4776 encoder->enable(encoder);
61b77ddd
DV
4777
4778 if (HAS_PCH_CPT(dev))
a1520318 4779 cpt_verify_modeset(dev, intel_crtc->pipe);
37ca8d4c
VS
4780
4781 /* Must wait for vblank to avoid spurious PCH FIFO underruns */
4782 if (intel_crtc->config->has_pch_encoder)
4783 intel_wait_for_vblank(dev, pipe);
b2c0593a 4784 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
37ca8d4c 4785 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
6be4a607
JB
4786}
4787
42db64ef
PZ
4788/* IPS only exists on ULT machines and is tied to pipe A. */
4789static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
4790{
f5adf94e 4791 return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
42db64ef
PZ
4792}
4793
4f771f10
PZ
4794static void haswell_crtc_enable(struct drm_crtc *crtc)
4795{
4796 struct drm_device *dev = crtc->dev;
fac5e23e 4797 struct drm_i915_private *dev_priv = to_i915(dev);
4f771f10
PZ
4798 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4799 struct intel_encoder *encoder;
99d736a2 4800 int pipe = intel_crtc->pipe, hsw_workaround_pipe;
4d1de975 4801 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
99d736a2
ML
4802 struct intel_crtc_state *pipe_config =
4803 to_intel_crtc_state(crtc->state);
4f771f10 4804
53d9f4e9 4805 if (WARN_ON(intel_crtc->active))
4f771f10
PZ
4806 return;
4807
81b088ca
VS
4808 if (intel_crtc->config->has_pch_encoder)
4809 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
4810 false);
4811
95a7a2ae
ID
4812 for_each_encoder_on_crtc(dev, crtc, encoder)
4813 if (encoder->pre_pll_enable)
4814 encoder->pre_pll_enable(encoder);
4815
8106ddbd 4816 if (intel_crtc->config->shared_dpll)
df8ad70c
DV
4817 intel_enable_shared_dpll(intel_crtc);
4818
37a5650b 4819 if (intel_crtc_has_dp_encoder(intel_crtc->config))
fe3cd48d 4820 intel_dp_set_m_n(intel_crtc, M1_N1);
229fca97 4821
d7edc4e5 4822 if (!transcoder_is_dsi(cpu_transcoder))
4d1de975
JN
4823 intel_set_pipe_timings(intel_crtc);
4824
bc58be60 4825 intel_set_pipe_src_size(intel_crtc);
229fca97 4826
4d1de975
JN
4827 if (cpu_transcoder != TRANSCODER_EDP &&
4828 !transcoder_is_dsi(cpu_transcoder)) {
4829 I915_WRITE(PIPE_MULT(cpu_transcoder),
6e3c9717 4830 intel_crtc->config->pixel_multiplier - 1);
ebb69c95
CT
4831 }
4832
6e3c9717 4833 if (intel_crtc->config->has_pch_encoder) {
229fca97 4834 intel_cpu_transcoder_set_m_n(intel_crtc,
6e3c9717 4835 &intel_crtc->config->fdi_m_n, NULL);
229fca97
DV
4836 }
4837
d7edc4e5 4838 if (!transcoder_is_dsi(cpu_transcoder))
4d1de975
JN
4839 haswell_set_pipeconf(crtc);
4840
391bf048 4841 haswell_set_pipemisc(crtc);
229fca97 4842
b95c5321 4843 intel_color_set_csc(&pipe_config->base);
229fca97 4844
4f771f10 4845 intel_crtc->active = true;
8664281b 4846
6b698516
DV
4847 if (intel_crtc->config->has_pch_encoder)
4848 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
4849 else
4850 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4851
7d4aefd0 4852 for_each_encoder_on_crtc(dev, crtc, encoder) {
4f771f10
PZ
4853 if (encoder->pre_enable)
4854 encoder->pre_enable(encoder);
7d4aefd0 4855 }
4f771f10 4856
d2d65408 4857 if (intel_crtc->config->has_pch_encoder)
4fe9467d 4858 dev_priv->display.fdi_link_train(crtc);
4fe9467d 4859
d7edc4e5 4860 if (!transcoder_is_dsi(cpu_transcoder))
7d4aefd0 4861 intel_ddi_enable_pipe_clock(intel_crtc);
4f771f10 4862
1c132b44 4863 if (INTEL_INFO(dev)->gen >= 9)
e435d6e5 4864 skylake_pfit_enable(intel_crtc);
ff6d9f55 4865 else
1c132b44 4866 ironlake_pfit_enable(intel_crtc);
4f771f10
PZ
4867
4868 /*
4869 * On ILK+ LUT must be loaded before the pipe is running but with
4870 * clocks enabled
4871 */
b95c5321 4872 intel_color_load_luts(&pipe_config->base);
4f771f10 4873
1f544388 4874 intel_ddi_set_pipe_settings(crtc);
d7edc4e5 4875 if (!transcoder_is_dsi(cpu_transcoder))
7d4aefd0 4876 intel_ddi_enable_transcoder_func(crtc);
4f771f10 4877
1d5bf5d9
ID
4878 if (dev_priv->display.initial_watermarks != NULL)
4879 dev_priv->display.initial_watermarks(pipe_config);
4880 else
4881 intel_update_watermarks(crtc);
4d1de975
JN
4882
4883 /* XXX: Do the pipe assertions at the right place for BXT DSI. */
d7edc4e5 4884 if (!transcoder_is_dsi(cpu_transcoder))
4d1de975 4885 intel_enable_pipe(intel_crtc);
42db64ef 4886
6e3c9717 4887 if (intel_crtc->config->has_pch_encoder)
1507e5bd 4888 lpt_pch_enable(crtc);
4f771f10 4889
a65347ba 4890 if (intel_crtc->config->dp_encoder_is_mst)
0e32b39c
DA
4891 intel_ddi_set_vc_payload_alloc(crtc, true);
4892
f9b61ff6
DV
4893 assert_vblank_disabled(crtc);
4894 drm_crtc_vblank_on(crtc);
4895
8807e55b 4896 for_each_encoder_on_crtc(dev, crtc, encoder) {
4f771f10 4897 encoder->enable(encoder);
8807e55b
JN
4898 intel_opregion_notify_encoder(encoder, true);
4899 }
4f771f10 4900
6b698516
DV
4901 if (intel_crtc->config->has_pch_encoder) {
4902 intel_wait_for_vblank(dev, pipe);
4903 intel_wait_for_vblank(dev, pipe);
4904 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
d2d65408
VS
4905 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
4906 true);
6b698516 4907 }
d2d65408 4908
e4916946
PZ
4909 /* If we change the relative order between pipe/planes enabling, we need
4910 * to change the workaround. */
99d736a2
ML
4911 hsw_workaround_pipe = pipe_config->hsw_workaround_pipe;
4912 if (IS_HASWELL(dev) && hsw_workaround_pipe != INVALID_PIPE) {
4913 intel_wait_for_vblank(dev, hsw_workaround_pipe);
4914 intel_wait_for_vblank(dev, hsw_workaround_pipe);
4915 }
4f771f10
PZ
4916}
4917
bfd16b2a 4918static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force)
3f8dce3a
DV
4919{
4920 struct drm_device *dev = crtc->base.dev;
fac5e23e 4921 struct drm_i915_private *dev_priv = to_i915(dev);
3f8dce3a
DV
4922 int pipe = crtc->pipe;
4923
4924 /* To avoid upsetting the power well on haswell only disable the pfit if
4925 * it's in use. The hw state code will make sure we get this right. */
bfd16b2a 4926 if (force || crtc->config->pch_pfit.enabled) {
3f8dce3a
DV
4927 I915_WRITE(PF_CTL(pipe), 0);
4928 I915_WRITE(PF_WIN_POS(pipe), 0);
4929 I915_WRITE(PF_WIN_SZ(pipe), 0);
4930 }
4931}
4932
6be4a607
JB
4933static void ironlake_crtc_disable(struct drm_crtc *crtc)
4934{
4935 struct drm_device *dev = crtc->dev;
fac5e23e 4936 struct drm_i915_private *dev_priv = to_i915(dev);
6be4a607 4937 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 4938 struct intel_encoder *encoder;
6be4a607 4939 int pipe = intel_crtc->pipe;
b52eb4dc 4940
b2c0593a
VS
4941 /*
4942 * Sometimes spurious CPU pipe underruns happen when the
4943 * pipe is already disabled, but FDI RX/TX is still enabled.
4944 * Happens at least with VGA+HDMI cloning. Suppress them.
4945 */
4946 if (intel_crtc->config->has_pch_encoder) {
4947 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
37ca8d4c 4948 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
b2c0593a 4949 }
37ca8d4c 4950
ea9d758d
DV
4951 for_each_encoder_on_crtc(dev, crtc, encoder)
4952 encoder->disable(encoder);
4953
f9b61ff6
DV
4954 drm_crtc_vblank_off(crtc);
4955 assert_vblank_disabled(crtc);
4956
575f7ab7 4957 intel_disable_pipe(intel_crtc);
32f9d658 4958
bfd16b2a 4959 ironlake_pfit_disable(intel_crtc, false);
2c07245f 4960
b2c0593a 4961 if (intel_crtc->config->has_pch_encoder)
5a74f70a
VS
4962 ironlake_fdi_disable(crtc);
4963
bf49ec8c
DV
4964 for_each_encoder_on_crtc(dev, crtc, encoder)
4965 if (encoder->post_disable)
4966 encoder->post_disable(encoder);
2c07245f 4967
6e3c9717 4968 if (intel_crtc->config->has_pch_encoder) {
d925c59a 4969 ironlake_disable_pch_transcoder(dev_priv, pipe);
6be4a607 4970
d925c59a 4971 if (HAS_PCH_CPT(dev)) {
f0f59a00
VS
4972 i915_reg_t reg;
4973 u32 temp;
4974
d925c59a
DV
4975 /* disable TRANS_DP_CTL */
4976 reg = TRANS_DP_CTL(pipe);
4977 temp = I915_READ(reg);
4978 temp &= ~(TRANS_DP_OUTPUT_ENABLE |
4979 TRANS_DP_PORT_SEL_MASK);
4980 temp |= TRANS_DP_PORT_SEL_NONE;
4981 I915_WRITE(reg, temp);
4982
4983 /* disable DPLL_SEL */
4984 temp = I915_READ(PCH_DPLL_SEL);
11887397 4985 temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
d925c59a 4986 I915_WRITE(PCH_DPLL_SEL, temp);
9db4a9c7 4987 }
e3421a18 4988
d925c59a
DV
4989 ironlake_fdi_pll_disable(intel_crtc);
4990 }
81b088ca 4991
b2c0593a 4992 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
81b088ca 4993 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
6be4a607 4994}
1b3c7a47 4995
4f771f10 4996static void haswell_crtc_disable(struct drm_crtc *crtc)
ee7b9f93 4997{
4f771f10 4998 struct drm_device *dev = crtc->dev;
fac5e23e 4999 struct drm_i915_private *dev_priv = to_i915(dev);
ee7b9f93 5000 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4f771f10 5001 struct intel_encoder *encoder;
6e3c9717 5002 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
ee7b9f93 5003
d2d65408
VS
5004 if (intel_crtc->config->has_pch_encoder)
5005 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5006 false);
5007
8807e55b
JN
5008 for_each_encoder_on_crtc(dev, crtc, encoder) {
5009 intel_opregion_notify_encoder(encoder, false);
4f771f10 5010 encoder->disable(encoder);
8807e55b 5011 }
4f771f10 5012
f9b61ff6
DV
5013 drm_crtc_vblank_off(crtc);
5014 assert_vblank_disabled(crtc);
5015
4d1de975 5016 /* XXX: Do the pipe assertions at the right place for BXT DSI. */
d7edc4e5 5017 if (!transcoder_is_dsi(cpu_transcoder))
4d1de975 5018 intel_disable_pipe(intel_crtc);
4f771f10 5019
6e3c9717 5020 if (intel_crtc->config->dp_encoder_is_mst)
a4bf214f
VS
5021 intel_ddi_set_vc_payload_alloc(crtc, false);
5022
d7edc4e5 5023 if (!transcoder_is_dsi(cpu_transcoder))
7d4aefd0 5024 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
4f771f10 5025
1c132b44 5026 if (INTEL_INFO(dev)->gen >= 9)
e435d6e5 5027 skylake_scaler_disable(intel_crtc);
ff6d9f55 5028 else
bfd16b2a 5029 ironlake_pfit_disable(intel_crtc, false);
4f771f10 5030
d7edc4e5 5031 if (!transcoder_is_dsi(cpu_transcoder))
7d4aefd0 5032 intel_ddi_disable_pipe_clock(intel_crtc);
4f771f10 5033
97b040aa
ID
5034 for_each_encoder_on_crtc(dev, crtc, encoder)
5035 if (encoder->post_disable)
5036 encoder->post_disable(encoder);
81b088ca 5037
92966a37
VS
5038 if (intel_crtc->config->has_pch_encoder) {
5039 lpt_disable_pch_transcoder(dev_priv);
503a74e9 5040 lpt_disable_iclkip(dev_priv);
92966a37
VS
5041 intel_ddi_fdi_disable(crtc);
5042
81b088ca
VS
5043 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5044 true);
92966a37 5045 }
4f771f10
PZ
5046}
5047
2dd24552
JB
5048static void i9xx_pfit_enable(struct intel_crtc *crtc)
5049{
5050 struct drm_device *dev = crtc->base.dev;
fac5e23e 5051 struct drm_i915_private *dev_priv = to_i915(dev);
6e3c9717 5052 struct intel_crtc_state *pipe_config = crtc->config;
2dd24552 5053
681a8504 5054 if (!pipe_config->gmch_pfit.control)
2dd24552
JB
5055 return;
5056
2dd24552 5057 /*
c0b03411
DV
5058 * The panel fitter should only be adjusted whilst the pipe is disabled,
5059 * according to register description and PRM.
2dd24552 5060 */
c0b03411
DV
5061 WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
5062 assert_pipe_disabled(dev_priv, crtc->pipe);
2dd24552 5063
b074cec8
JB
5064 I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
5065 I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
5a80c45c
DV
5066
5067 /* Border color in case we don't scale up to the full screen. Black by
5068 * default, change to something else for debugging. */
5069 I915_WRITE(BCLRPAT(crtc->pipe), 0);
2dd24552
JB
5070}
5071
d05410f9
DA
5072static enum intel_display_power_domain port_to_power_domain(enum port port)
5073{
5074 switch (port) {
5075 case PORT_A:
6331a704 5076 return POWER_DOMAIN_PORT_DDI_A_LANES;
d05410f9 5077 case PORT_B:
6331a704 5078 return POWER_DOMAIN_PORT_DDI_B_LANES;
d05410f9 5079 case PORT_C:
6331a704 5080 return POWER_DOMAIN_PORT_DDI_C_LANES;
d05410f9 5081 case PORT_D:
6331a704 5082 return POWER_DOMAIN_PORT_DDI_D_LANES;
d8e19f99 5083 case PORT_E:
6331a704 5084 return POWER_DOMAIN_PORT_DDI_E_LANES;
d05410f9 5085 default:
b9fec167 5086 MISSING_CASE(port);
d05410f9
DA
5087 return POWER_DOMAIN_PORT_OTHER;
5088 }
5089}
5090
25f78f58
VS
5091static enum intel_display_power_domain port_to_aux_power_domain(enum port port)
5092{
5093 switch (port) {
5094 case PORT_A:
5095 return POWER_DOMAIN_AUX_A;
5096 case PORT_B:
5097 return POWER_DOMAIN_AUX_B;
5098 case PORT_C:
5099 return POWER_DOMAIN_AUX_C;
5100 case PORT_D:
5101 return POWER_DOMAIN_AUX_D;
5102 case PORT_E:
5103 /* FIXME: Check VBT for actual wiring of PORT E */
5104 return POWER_DOMAIN_AUX_D;
5105 default:
b9fec167 5106 MISSING_CASE(port);
25f78f58
VS
5107 return POWER_DOMAIN_AUX_A;
5108 }
5109}
5110
319be8ae
ID
5111enum intel_display_power_domain
5112intel_display_port_power_domain(struct intel_encoder *intel_encoder)
5113{
5114 struct drm_device *dev = intel_encoder->base.dev;
5115 struct intel_digital_port *intel_dig_port;
5116
5117 switch (intel_encoder->type) {
5118 case INTEL_OUTPUT_UNKNOWN:
5119 /* Only DDI platforms should ever use this output type */
5120 WARN_ON_ONCE(!HAS_DDI(dev));
cca0502b 5121 case INTEL_OUTPUT_DP:
319be8ae
ID
5122 case INTEL_OUTPUT_HDMI:
5123 case INTEL_OUTPUT_EDP:
5124 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
d05410f9 5125 return port_to_power_domain(intel_dig_port->port);
0e32b39c
DA
5126 case INTEL_OUTPUT_DP_MST:
5127 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
5128 return port_to_power_domain(intel_dig_port->port);
319be8ae
ID
5129 case INTEL_OUTPUT_ANALOG:
5130 return POWER_DOMAIN_PORT_CRT;
5131 case INTEL_OUTPUT_DSI:
5132 return POWER_DOMAIN_PORT_DSI;
5133 default:
5134 return POWER_DOMAIN_PORT_OTHER;
5135 }
5136}
5137
25f78f58
VS
5138enum intel_display_power_domain
5139intel_display_port_aux_power_domain(struct intel_encoder *intel_encoder)
5140{
5141 struct drm_device *dev = intel_encoder->base.dev;
5142 struct intel_digital_port *intel_dig_port;
5143
5144 switch (intel_encoder->type) {
5145 case INTEL_OUTPUT_UNKNOWN:
651174a4
ID
5146 case INTEL_OUTPUT_HDMI:
5147 /*
5148 * Only DDI platforms should ever use these output types.
5149 * We can get here after the HDMI detect code has already set
5150 * the type of the shared encoder. Since we can't be sure
5151 * what's the status of the given connectors, play safe and
5152 * run the DP detection too.
5153 */
25f78f58 5154 WARN_ON_ONCE(!HAS_DDI(dev));
cca0502b 5155 case INTEL_OUTPUT_DP:
25f78f58
VS
5156 case INTEL_OUTPUT_EDP:
5157 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
5158 return port_to_aux_power_domain(intel_dig_port->port);
5159 case INTEL_OUTPUT_DP_MST:
5160 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
5161 return port_to_aux_power_domain(intel_dig_port->port);
5162 default:
b9fec167 5163 MISSING_CASE(intel_encoder->type);
25f78f58
VS
5164 return POWER_DOMAIN_AUX_A;
5165 }
5166}
5167
74bff5f9
ML
5168static unsigned long get_crtc_power_domains(struct drm_crtc *crtc,
5169 struct intel_crtc_state *crtc_state)
77d22dca 5170{
319be8ae 5171 struct drm_device *dev = crtc->dev;
74bff5f9 5172 struct drm_encoder *encoder;
319be8ae
ID
5173 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5174 enum pipe pipe = intel_crtc->pipe;
77d22dca 5175 unsigned long mask;
74bff5f9 5176 enum transcoder transcoder = crtc_state->cpu_transcoder;
77d22dca 5177
74bff5f9 5178 if (!crtc_state->base.active)
292b990e
ML
5179 return 0;
5180
77d22dca
ID
5181 mask = BIT(POWER_DOMAIN_PIPE(pipe));
5182 mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
74bff5f9
ML
5183 if (crtc_state->pch_pfit.enabled ||
5184 crtc_state->pch_pfit.force_thru)
77d22dca
ID
5185 mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
5186
74bff5f9
ML
5187 drm_for_each_encoder_mask(encoder, dev, crtc_state->base.encoder_mask) {
5188 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
5189
319be8ae 5190 mask |= BIT(intel_display_port_power_domain(intel_encoder));
74bff5f9 5191 }
319be8ae 5192
15e7ec29
ML
5193 if (crtc_state->shared_dpll)
5194 mask |= BIT(POWER_DOMAIN_PLLS);
5195
77d22dca
ID
5196 return mask;
5197}
5198
74bff5f9
ML
5199static unsigned long
5200modeset_get_crtc_power_domains(struct drm_crtc *crtc,
5201 struct intel_crtc_state *crtc_state)
77d22dca 5202{
fac5e23e 5203 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
292b990e
ML
5204 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5205 enum intel_display_power_domain domain;
5a21b665 5206 unsigned long domains, new_domains, old_domains;
77d22dca 5207
292b990e 5208 old_domains = intel_crtc->enabled_power_domains;
74bff5f9
ML
5209 intel_crtc->enabled_power_domains = new_domains =
5210 get_crtc_power_domains(crtc, crtc_state);
77d22dca 5211
5a21b665 5212 domains = new_domains & ~old_domains;
292b990e
ML
5213
5214 for_each_power_domain(domain, domains)
5215 intel_display_power_get(dev_priv, domain);
5216
5a21b665 5217 return old_domains & ~new_domains;
292b990e
ML
5218}
5219
5220static void modeset_put_power_domains(struct drm_i915_private *dev_priv,
5221 unsigned long domains)
5222{
5223 enum intel_display_power_domain domain;
5224
5225 for_each_power_domain(domain, domains)
5226 intel_display_power_put(dev_priv, domain);
5227}
77d22dca 5228
adafdc6f
MK
5229static int intel_compute_max_dotclk(struct drm_i915_private *dev_priv)
5230{
5231 int max_cdclk_freq = dev_priv->max_cdclk_freq;
5232
5233 if (INTEL_INFO(dev_priv)->gen >= 9 ||
5234 IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
5235 return max_cdclk_freq;
5236 else if (IS_CHERRYVIEW(dev_priv))
5237 return max_cdclk_freq*95/100;
5238 else if (INTEL_INFO(dev_priv)->gen < 4)
5239 return 2*max_cdclk_freq*90/100;
5240 else
5241 return max_cdclk_freq*90/100;
5242}
5243
b2045352
VS
5244static int skl_calc_cdclk(int max_pixclk, int vco);
5245
560a7ae4
DL
5246static void intel_update_max_cdclk(struct drm_device *dev)
5247{
fac5e23e 5248 struct drm_i915_private *dev_priv = to_i915(dev);
560a7ae4 5249
ef11bdb3 5250 if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) {
560a7ae4 5251 u32 limit = I915_READ(SKL_DFSM) & SKL_DFSM_CDCLK_LIMIT_MASK;
b2045352
VS
5252 int max_cdclk, vco;
5253
5254 vco = dev_priv->skl_preferred_vco_freq;
63911d72 5255 WARN_ON(vco != 8100000 && vco != 8640000);
560a7ae4 5256
b2045352
VS
5257 /*
5258 * Use the lower (vco 8640) cdclk values as a
5259 * first guess. skl_calc_cdclk() will correct it
5260 * if the preferred vco is 8100 instead.
5261 */
560a7ae4 5262 if (limit == SKL_DFSM_CDCLK_LIMIT_675)
487ed2e4 5263 max_cdclk = 617143;
560a7ae4 5264 else if (limit == SKL_DFSM_CDCLK_LIMIT_540)
b2045352 5265 max_cdclk = 540000;
560a7ae4 5266 else if (limit == SKL_DFSM_CDCLK_LIMIT_450)
b2045352 5267 max_cdclk = 432000;
560a7ae4 5268 else
487ed2e4 5269 max_cdclk = 308571;
b2045352
VS
5270
5271 dev_priv->max_cdclk_freq = skl_calc_cdclk(max_cdclk, vco);
281c114f
MR
5272 } else if (IS_BROXTON(dev)) {
5273 dev_priv->max_cdclk_freq = 624000;
560a7ae4
DL
5274 } else if (IS_BROADWELL(dev)) {
5275 /*
5276 * FIXME with extra cooling we can allow
5277 * 540 MHz for ULX and 675 Mhz for ULT.
5278 * How can we know if extra cooling is
5279 * available? PCI ID, VTB, something else?
5280 */
5281 if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
5282 dev_priv->max_cdclk_freq = 450000;
5283 else if (IS_BDW_ULX(dev))
5284 dev_priv->max_cdclk_freq = 450000;
5285 else if (IS_BDW_ULT(dev))
5286 dev_priv->max_cdclk_freq = 540000;
5287 else
5288 dev_priv->max_cdclk_freq = 675000;
0904deaf
MK
5289 } else if (IS_CHERRYVIEW(dev)) {
5290 dev_priv->max_cdclk_freq = 320000;
560a7ae4
DL
5291 } else if (IS_VALLEYVIEW(dev)) {
5292 dev_priv->max_cdclk_freq = 400000;
5293 } else {
5294 /* otherwise assume cdclk is fixed */
5295 dev_priv->max_cdclk_freq = dev_priv->cdclk_freq;
5296 }
5297
adafdc6f
MK
5298 dev_priv->max_dotclk_freq = intel_compute_max_dotclk(dev_priv);
5299
560a7ae4
DL
5300 DRM_DEBUG_DRIVER("Max CD clock rate: %d kHz\n",
5301 dev_priv->max_cdclk_freq);
adafdc6f
MK
5302
5303 DRM_DEBUG_DRIVER("Max dotclock rate: %d kHz\n",
5304 dev_priv->max_dotclk_freq);
560a7ae4
DL
5305}
5306
5307static void intel_update_cdclk(struct drm_device *dev)
5308{
fac5e23e 5309 struct drm_i915_private *dev_priv = to_i915(dev);
560a7ae4
DL
5310
5311 dev_priv->cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
2f2a121a 5312
83d7c81f 5313 if (INTEL_GEN(dev_priv) >= 9)
709e05c3
VS
5314 DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz, VCO: %d kHz, ref: %d kHz\n",
5315 dev_priv->cdclk_freq, dev_priv->cdclk_pll.vco,
5316 dev_priv->cdclk_pll.ref);
2f2a121a
VS
5317 else
5318 DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
5319 dev_priv->cdclk_freq);
560a7ae4
DL
5320
5321 /*
b5d99ff9
VS
5322 * 9:0 CMBUS [sic] CDCLK frequency (cdfreq):
5323 * Programmng [sic] note: bit[9:2] should be programmed to the number
5324 * of cdclk that generates 4MHz reference clock freq which is used to
5325 * generate GMBus clock. This will vary with the cdclk freq.
560a7ae4 5326 */
b5d99ff9 5327 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
560a7ae4 5328 I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->cdclk_freq, 1000));
560a7ae4
DL
5329}
5330
92891e45
VS
5331/* convert from kHz to .1 fixpoint MHz with -1MHz offset */
5332static int skl_cdclk_decimal(int cdclk)
5333{
5334 return DIV_ROUND_CLOSEST(cdclk - 1000, 500);
5335}
5336
5f199dfa
VS
5337static int bxt_de_pll_vco(struct drm_i915_private *dev_priv, int cdclk)
5338{
5339 int ratio;
5340
5341 if (cdclk == dev_priv->cdclk_pll.ref)
5342 return 0;
5343
5344 switch (cdclk) {
5345 default:
5346 MISSING_CASE(cdclk);
5347 case 144000:
5348 case 288000:
5349 case 384000:
5350 case 576000:
5351 ratio = 60;
5352 break;
5353 case 624000:
5354 ratio = 65;
5355 break;
5356 }
5357
5358 return dev_priv->cdclk_pll.ref * ratio;
5359}
5360
2b73001e
VS
5361static void bxt_de_pll_disable(struct drm_i915_private *dev_priv)
5362{
5363 I915_WRITE(BXT_DE_PLL_ENABLE, 0);
5364
5365 /* Timeout 200us */
95cac283
CW
5366 if (intel_wait_for_register(dev_priv,
5367 BXT_DE_PLL_ENABLE, BXT_DE_PLL_LOCK, 0,
5368 1))
2b73001e 5369 DRM_ERROR("timeout waiting for DE PLL unlock\n");
83d7c81f
VS
5370
5371 dev_priv->cdclk_pll.vco = 0;
2b73001e
VS
5372}
5373
5f199dfa 5374static void bxt_de_pll_enable(struct drm_i915_private *dev_priv, int vco)
2b73001e 5375{
5f199dfa 5376 int ratio = DIV_ROUND_CLOSEST(vco, dev_priv->cdclk_pll.ref);
2b73001e
VS
5377 u32 val;
5378
5379 val = I915_READ(BXT_DE_PLL_CTL);
5380 val &= ~BXT_DE_PLL_RATIO_MASK;
5f199dfa 5381 val |= BXT_DE_PLL_RATIO(ratio);
2b73001e
VS
5382 I915_WRITE(BXT_DE_PLL_CTL, val);
5383
5384 I915_WRITE(BXT_DE_PLL_ENABLE, BXT_DE_PLL_PLL_ENABLE);
5385
5386 /* Timeout 200us */
e084e1b9
CW
5387 if (intel_wait_for_register(dev_priv,
5388 BXT_DE_PLL_ENABLE,
5389 BXT_DE_PLL_LOCK,
5390 BXT_DE_PLL_LOCK,
5391 1))
2b73001e 5392 DRM_ERROR("timeout waiting for DE PLL lock\n");
83d7c81f 5393
5f199dfa 5394 dev_priv->cdclk_pll.vco = vco;
2b73001e
VS
5395}
5396
324513c0 5397static void bxt_set_cdclk(struct drm_i915_private *dev_priv, int cdclk)
f8437dd1 5398{
5f199dfa
VS
5399 u32 val, divider;
5400 int vco, ret;
f8437dd1 5401
5f199dfa
VS
5402 vco = bxt_de_pll_vco(dev_priv, cdclk);
5403
5404 DRM_DEBUG_DRIVER("Changing CDCLK to %d kHz (VCO %d kHz)\n", cdclk, vco);
5405
5406 /* cdclk = vco / 2 / div{1,1.5,2,4} */
5407 switch (DIV_ROUND_CLOSEST(vco, cdclk)) {
5408 case 8:
f8437dd1 5409 divider = BXT_CDCLK_CD2X_DIV_SEL_4;
f8437dd1 5410 break;
5f199dfa 5411 case 4:
f8437dd1 5412 divider = BXT_CDCLK_CD2X_DIV_SEL_2;
f8437dd1 5413 break;
5f199dfa 5414 case 3:
f8437dd1 5415 divider = BXT_CDCLK_CD2X_DIV_SEL_1_5;
f8437dd1 5416 break;
5f199dfa 5417 case 2:
f8437dd1 5418 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
f8437dd1
VK
5419 break;
5420 default:
5f199dfa
VS
5421 WARN_ON(cdclk != dev_priv->cdclk_pll.ref);
5422 WARN_ON(vco != 0);
f8437dd1 5423
5f199dfa
VS
5424 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
5425 break;
f8437dd1
VK
5426 }
5427
f8437dd1 5428 /* Inform power controller of upcoming frequency change */
5f199dfa 5429 mutex_lock(&dev_priv->rps.hw_lock);
f8437dd1
VK
5430 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
5431 0x80000000);
5432 mutex_unlock(&dev_priv->rps.hw_lock);
5433
5434 if (ret) {
5435 DRM_ERROR("PCode CDCLK freq change notify failed (err %d, freq %d)\n",
9ef56154 5436 ret, cdclk);
f8437dd1
VK
5437 return;
5438 }
5439
5f199dfa
VS
5440 if (dev_priv->cdclk_pll.vco != 0 &&
5441 dev_priv->cdclk_pll.vco != vco)
2b73001e 5442 bxt_de_pll_disable(dev_priv);
f8437dd1 5443
5f199dfa
VS
5444 if (dev_priv->cdclk_pll.vco != vco)
5445 bxt_de_pll_enable(dev_priv, vco);
f8437dd1 5446
5f199dfa
VS
5447 val = divider | skl_cdclk_decimal(cdclk);
5448 /*
5449 * FIXME if only the cd2x divider needs changing, it could be done
5450 * without shutting off the pipe (if only one pipe is active).
5451 */
5452 val |= BXT_CDCLK_CD2X_PIPE_NONE;
5453 /*
5454 * Disable SSA Precharge when CD clock frequency < 500 MHz,
5455 * enable otherwise.
5456 */
5457 if (cdclk >= 500000)
5458 val |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
5459 I915_WRITE(CDCLK_CTL, val);
f8437dd1
VK
5460
5461 mutex_lock(&dev_priv->rps.hw_lock);
5462 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
9ef56154 5463 DIV_ROUND_UP(cdclk, 25000));
f8437dd1
VK
5464 mutex_unlock(&dev_priv->rps.hw_lock);
5465
5466 if (ret) {
5467 DRM_ERROR("PCode CDCLK freq set failed, (err %d, freq %d)\n",
9ef56154 5468 ret, cdclk);
f8437dd1
VK
5469 return;
5470 }
5471
91c8a326 5472 intel_update_cdclk(&dev_priv->drm);
f8437dd1
VK
5473}
5474
d66a2194 5475static void bxt_sanitize_cdclk(struct drm_i915_private *dev_priv)
f8437dd1 5476{
d66a2194
ID
5477 u32 cdctl, expected;
5478
91c8a326 5479 intel_update_cdclk(&dev_priv->drm);
f8437dd1 5480
d66a2194
ID
5481 if (dev_priv->cdclk_pll.vco == 0 ||
5482 dev_priv->cdclk_freq == dev_priv->cdclk_pll.ref)
5483 goto sanitize;
5484
5485 /* DPLL okay; verify the cdclock
5486 *
5487 * Some BIOS versions leave an incorrect decimal frequency value and
5488 * set reserved MBZ bits in CDCLK_CTL at least during exiting from S4,
5489 * so sanitize this register.
5490 */
5491 cdctl = I915_READ(CDCLK_CTL);
5492 /*
5493 * Let's ignore the pipe field, since BIOS could have configured the
5494 * dividers both synching to an active pipe, or asynchronously
5495 * (PIPE_NONE).
5496 */
5497 cdctl &= ~BXT_CDCLK_CD2X_PIPE_NONE;
5498
5499 expected = (cdctl & BXT_CDCLK_CD2X_DIV_SEL_MASK) |
5500 skl_cdclk_decimal(dev_priv->cdclk_freq);
5501 /*
5502 * Disable SSA Precharge when CD clock frequency < 500 MHz,
5503 * enable otherwise.
5504 */
5505 if (dev_priv->cdclk_freq >= 500000)
5506 expected |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
5507
5508 if (cdctl == expected)
5509 /* All well; nothing to sanitize */
5510 return;
5511
5512sanitize:
5513 DRM_DEBUG_KMS("Sanitizing cdclk programmed by pre-os\n");
5514
5515 /* force cdclk programming */
5516 dev_priv->cdclk_freq = 0;
5517
5518 /* force full PLL disable + enable */
5519 dev_priv->cdclk_pll.vco = -1;
5520}
5521
324513c0 5522void bxt_init_cdclk(struct drm_i915_private *dev_priv)
d66a2194
ID
5523{
5524 bxt_sanitize_cdclk(dev_priv);
5525
5526 if (dev_priv->cdclk_freq != 0 && dev_priv->cdclk_pll.vco != 0)
089c6fd5 5527 return;
c2e001ef 5528
f8437dd1
VK
5529 /*
5530 * FIXME:
5531 * - The initial CDCLK needs to be read from VBT.
5532 * Need to make this change after VBT has changes for BXT.
f8437dd1 5533 */
324513c0 5534 bxt_set_cdclk(dev_priv, bxt_calc_cdclk(0));
f8437dd1
VK
5535}
5536
324513c0 5537void bxt_uninit_cdclk(struct drm_i915_private *dev_priv)
f8437dd1 5538{
324513c0 5539 bxt_set_cdclk(dev_priv, dev_priv->cdclk_pll.ref);
f8437dd1
VK
5540}
5541
a8ca4934
VS
5542static int skl_calc_cdclk(int max_pixclk, int vco)
5543{
63911d72 5544 if (vco == 8640000) {
a8ca4934 5545 if (max_pixclk > 540000)
487ed2e4 5546 return 617143;
a8ca4934
VS
5547 else if (max_pixclk > 432000)
5548 return 540000;
487ed2e4 5549 else if (max_pixclk > 308571)
a8ca4934
VS
5550 return 432000;
5551 else
487ed2e4 5552 return 308571;
a8ca4934 5553 } else {
a8ca4934
VS
5554 if (max_pixclk > 540000)
5555 return 675000;
5556 else if (max_pixclk > 450000)
5557 return 540000;
5558 else if (max_pixclk > 337500)
5559 return 450000;
5560 else
5561 return 337500;
5562 }
5563}
5564
ea61791e
VS
5565static void
5566skl_dpll0_update(struct drm_i915_private *dev_priv)
5d96d8af 5567{
ea61791e 5568 u32 val;
5d96d8af 5569
709e05c3 5570 dev_priv->cdclk_pll.ref = 24000;
1c3f7700 5571 dev_priv->cdclk_pll.vco = 0;
709e05c3 5572
ea61791e 5573 val = I915_READ(LCPLL1_CTL);
1c3f7700 5574 if ((val & LCPLL_PLL_ENABLE) == 0)
ea61791e 5575 return;
5d96d8af 5576
1c3f7700
ID
5577 if (WARN_ON((val & LCPLL_PLL_LOCK) == 0))
5578 return;
9f7eb31a 5579
ea61791e
VS
5580 val = I915_READ(DPLL_CTRL1);
5581
1c3f7700
ID
5582 if (WARN_ON((val & (DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) |
5583 DPLL_CTRL1_SSC(SKL_DPLL0) |
5584 DPLL_CTRL1_OVERRIDE(SKL_DPLL0))) !=
5585 DPLL_CTRL1_OVERRIDE(SKL_DPLL0)))
5586 return;
9f7eb31a 5587
ea61791e
VS
5588 switch (val & DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0)) {
5589 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810, SKL_DPLL0):
5590 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1350, SKL_DPLL0):
5591 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1620, SKL_DPLL0):
5592 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_2700, SKL_DPLL0):
63911d72 5593 dev_priv->cdclk_pll.vco = 8100000;
ea61791e
VS
5594 break;
5595 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080, SKL_DPLL0):
5596 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_2160, SKL_DPLL0):
63911d72 5597 dev_priv->cdclk_pll.vco = 8640000;
ea61791e
VS
5598 break;
5599 default:
5600 MISSING_CASE(val & DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
ea61791e
VS
5601 break;
5602 }
5d96d8af
DL
5603}
5604
b2045352
VS
5605void skl_set_preferred_cdclk_vco(struct drm_i915_private *dev_priv, int vco)
5606{
5607 bool changed = dev_priv->skl_preferred_vco_freq != vco;
5608
5609 dev_priv->skl_preferred_vco_freq = vco;
5610
5611 if (changed)
91c8a326 5612 intel_update_max_cdclk(&dev_priv->drm);
b2045352
VS
5613}
5614
5d96d8af 5615static void
3861fc60 5616skl_dpll0_enable(struct drm_i915_private *dev_priv, int vco)
5d96d8af 5617{
a8ca4934 5618 int min_cdclk = skl_calc_cdclk(0, vco);
5d96d8af
DL
5619 u32 val;
5620
63911d72 5621 WARN_ON(vco != 8100000 && vco != 8640000);
b2045352 5622
5d96d8af 5623 /* select the minimum CDCLK before enabling DPLL 0 */
9ef56154 5624 val = CDCLK_FREQ_337_308 | skl_cdclk_decimal(min_cdclk);
5d96d8af
DL
5625 I915_WRITE(CDCLK_CTL, val);
5626 POSTING_READ(CDCLK_CTL);
5627
5628 /*
5629 * We always enable DPLL0 with the lowest link rate possible, but still
5630 * taking into account the VCO required to operate the eDP panel at the
5631 * desired frequency. The usual DP link rates operate with a VCO of
5632 * 8100 while the eDP 1.4 alternate link rates need a VCO of 8640.
5633 * The modeset code is responsible for the selection of the exact link
5634 * rate later on, with the constraint of choosing a frequency that
a8ca4934 5635 * works with vco.
5d96d8af
DL
5636 */
5637 val = I915_READ(DPLL_CTRL1);
5638
5639 val &= ~(DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) | DPLL_CTRL1_SSC(SKL_DPLL0) |
5640 DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
5641 val |= DPLL_CTRL1_OVERRIDE(SKL_DPLL0);
63911d72 5642 if (vco == 8640000)
5d96d8af
DL
5643 val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080,
5644 SKL_DPLL0);
5645 else
5646 val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810,
5647 SKL_DPLL0);
5648
5649 I915_WRITE(DPLL_CTRL1, val);
5650 POSTING_READ(DPLL_CTRL1);
5651
5652 I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) | LCPLL_PLL_ENABLE);
5653
e24ca054
CW
5654 if (intel_wait_for_register(dev_priv,
5655 LCPLL1_CTL, LCPLL_PLL_LOCK, LCPLL_PLL_LOCK,
5656 5))
5d96d8af 5657 DRM_ERROR("DPLL0 not locked\n");
1cd593e0 5658
63911d72 5659 dev_priv->cdclk_pll.vco = vco;
b2045352
VS
5660
5661 /* We'll want to keep using the current vco from now on. */
5662 skl_set_preferred_cdclk_vco(dev_priv, vco);
5d96d8af
DL
5663}
5664
430e05de
VS
5665static void
5666skl_dpll0_disable(struct drm_i915_private *dev_priv)
5667{
5668 I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) & ~LCPLL_PLL_ENABLE);
8ad32a05
CW
5669 if (intel_wait_for_register(dev_priv,
5670 LCPLL1_CTL, LCPLL_PLL_LOCK, 0,
5671 1))
430e05de 5672 DRM_ERROR("Couldn't disable DPLL0\n");
1cd593e0 5673
63911d72 5674 dev_priv->cdclk_pll.vco = 0;
430e05de
VS
5675}
5676
5d96d8af
DL
5677static bool skl_cdclk_pcu_ready(struct drm_i915_private *dev_priv)
5678{
5679 int ret;
5680 u32 val;
5681
5682 /* inform PCU we want to change CDCLK */
5683 val = SKL_CDCLK_PREPARE_FOR_CHANGE;
5684 mutex_lock(&dev_priv->rps.hw_lock);
5685 ret = sandybridge_pcode_read(dev_priv, SKL_PCODE_CDCLK_CONTROL, &val);
5686 mutex_unlock(&dev_priv->rps.hw_lock);
5687
5688 return ret == 0 && (val & SKL_CDCLK_READY_FOR_CHANGE);
5689}
5690
5691static bool skl_cdclk_wait_for_pcu_ready(struct drm_i915_private *dev_priv)
5692{
848496e5 5693 return _wait_for(skl_cdclk_pcu_ready(dev_priv), 3000, 10) == 0;
5d96d8af
DL
5694}
5695
1cd593e0 5696static void skl_set_cdclk(struct drm_i915_private *dev_priv, int cdclk, int vco)
5d96d8af 5697{
91c8a326 5698 struct drm_device *dev = &dev_priv->drm;
5d96d8af
DL
5699 u32 freq_select, pcu_ack;
5700
1cd593e0
VS
5701 WARN_ON((cdclk == 24000) != (vco == 0));
5702
63911d72 5703 DRM_DEBUG_DRIVER("Changing CDCLK to %d kHz (VCO %d kHz)\n", cdclk, vco);
5d96d8af
DL
5704
5705 if (!skl_cdclk_wait_for_pcu_ready(dev_priv)) {
5706 DRM_ERROR("failed to inform PCU about cdclk change\n");
5707 return;
5708 }
5709
5710 /* set CDCLK_CTL */
9ef56154 5711 switch (cdclk) {
5d96d8af
DL
5712 case 450000:
5713 case 432000:
5714 freq_select = CDCLK_FREQ_450_432;
5715 pcu_ack = 1;
5716 break;
5717 case 540000:
5718 freq_select = CDCLK_FREQ_540;
5719 pcu_ack = 2;
5720 break;
487ed2e4 5721 case 308571:
5d96d8af
DL
5722 case 337500:
5723 default:
5724 freq_select = CDCLK_FREQ_337_308;
5725 pcu_ack = 0;
5726 break;
487ed2e4 5727 case 617143:
5d96d8af
DL
5728 case 675000:
5729 freq_select = CDCLK_FREQ_675_617;
5730 pcu_ack = 3;
5731 break;
5732 }
5733
63911d72
VS
5734 if (dev_priv->cdclk_pll.vco != 0 &&
5735 dev_priv->cdclk_pll.vco != vco)
1cd593e0
VS
5736 skl_dpll0_disable(dev_priv);
5737
63911d72 5738 if (dev_priv->cdclk_pll.vco != vco)
1cd593e0
VS
5739 skl_dpll0_enable(dev_priv, vco);
5740
9ef56154 5741 I915_WRITE(CDCLK_CTL, freq_select | skl_cdclk_decimal(cdclk));
5d96d8af
DL
5742 POSTING_READ(CDCLK_CTL);
5743
5744 /* inform PCU of the change */
5745 mutex_lock(&dev_priv->rps.hw_lock);
5746 sandybridge_pcode_write(dev_priv, SKL_PCODE_CDCLK_CONTROL, pcu_ack);
5747 mutex_unlock(&dev_priv->rps.hw_lock);
560a7ae4
DL
5748
5749 intel_update_cdclk(dev);
5d96d8af
DL
5750}
5751
9f7eb31a
VS
5752static void skl_sanitize_cdclk(struct drm_i915_private *dev_priv);
5753
5d96d8af
DL
5754void skl_uninit_cdclk(struct drm_i915_private *dev_priv)
5755{
709e05c3 5756 skl_set_cdclk(dev_priv, dev_priv->cdclk_pll.ref, 0);
5d96d8af
DL
5757}
5758
5759void skl_init_cdclk(struct drm_i915_private *dev_priv)
5760{
9f7eb31a
VS
5761 int cdclk, vco;
5762
5763 skl_sanitize_cdclk(dev_priv);
5d96d8af 5764
63911d72 5765 if (dev_priv->cdclk_freq != 0 && dev_priv->cdclk_pll.vco != 0) {
9f7eb31a
VS
5766 /*
5767 * Use the current vco as our initial
5768 * guess as to what the preferred vco is.
5769 */
5770 if (dev_priv->skl_preferred_vco_freq == 0)
5771 skl_set_preferred_cdclk_vco(dev_priv,
63911d72 5772 dev_priv->cdclk_pll.vco);
70c2c184 5773 return;
1cd593e0 5774 }
5d96d8af 5775
70c2c184
VS
5776 vco = dev_priv->skl_preferred_vco_freq;
5777 if (vco == 0)
63911d72 5778 vco = 8100000;
70c2c184 5779 cdclk = skl_calc_cdclk(0, vco);
5d96d8af 5780
70c2c184 5781 skl_set_cdclk(dev_priv, cdclk, vco);
5d96d8af
DL
5782}
5783
9f7eb31a 5784static void skl_sanitize_cdclk(struct drm_i915_private *dev_priv)
c73666f3 5785{
09492498 5786 uint32_t cdctl, expected;
c73666f3 5787
f1b391a5
SK
5788 /*
5789 * check if the pre-os intialized the display
5790 * There is SWF18 scratchpad register defined which is set by the
5791 * pre-os which can be used by the OS drivers to check the status
5792 */
5793 if ((I915_READ(SWF_ILK(0x18)) & 0x00FFFFFF) == 0)
5794 goto sanitize;
5795
91c8a326 5796 intel_update_cdclk(&dev_priv->drm);
c73666f3 5797 /* Is PLL enabled and locked ? */
1c3f7700
ID
5798 if (dev_priv->cdclk_pll.vco == 0 ||
5799 dev_priv->cdclk_freq == dev_priv->cdclk_pll.ref)
c73666f3
SK
5800 goto sanitize;
5801
5802 /* DPLL okay; verify the cdclock
5803 *
5804 * Noticed in some instances that the freq selection is correct but
5805 * decimal part is programmed wrong from BIOS where pre-os does not
5806 * enable display. Verify the same as well.
5807 */
09492498
VS
5808 cdctl = I915_READ(CDCLK_CTL);
5809 expected = (cdctl & CDCLK_FREQ_SEL_MASK) |
5810 skl_cdclk_decimal(dev_priv->cdclk_freq);
5811 if (cdctl == expected)
c73666f3 5812 /* All well; nothing to sanitize */
9f7eb31a 5813 return;
c89e39f3 5814
9f7eb31a
VS
5815sanitize:
5816 DRM_DEBUG_KMS("Sanitizing cdclk programmed by pre-os\n");
c73666f3 5817
9f7eb31a
VS
5818 /* force cdclk programming */
5819 dev_priv->cdclk_freq = 0;
5820 /* force full PLL disable + enable */
63911d72 5821 dev_priv->cdclk_pll.vco = -1;
c73666f3
SK
5822}
5823
30a970c6
JB
5824/* Adjust CDclk dividers to allow high res or save power if possible */
5825static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
5826{
fac5e23e 5827 struct drm_i915_private *dev_priv = to_i915(dev);
30a970c6
JB
5828 u32 val, cmd;
5829
164dfd28
VK
5830 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5831 != dev_priv->cdclk_freq);
d60c4473 5832
dfcab17e 5833 if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
30a970c6 5834 cmd = 2;
dfcab17e 5835 else if (cdclk == 266667)
30a970c6
JB
5836 cmd = 1;
5837 else
5838 cmd = 0;
5839
5840 mutex_lock(&dev_priv->rps.hw_lock);
5841 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5842 val &= ~DSPFREQGUAR_MASK;
5843 val |= (cmd << DSPFREQGUAR_SHIFT);
5844 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5845 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5846 DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
5847 50)) {
5848 DRM_ERROR("timed out waiting for CDclk change\n");
5849 }
5850 mutex_unlock(&dev_priv->rps.hw_lock);
5851
54433e91
VS
5852 mutex_lock(&dev_priv->sb_lock);
5853
dfcab17e 5854 if (cdclk == 400000) {
6bcda4f0 5855 u32 divider;
30a970c6 5856
6bcda4f0 5857 divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
30a970c6 5858
30a970c6
JB
5859 /* adjust cdclk divider */
5860 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
87d5d259 5861 val &= ~CCK_FREQUENCY_VALUES;
30a970c6
JB
5862 val |= divider;
5863 vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
a877e801
VS
5864
5865 if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
87d5d259 5866 CCK_FREQUENCY_STATUS) == (divider << CCK_FREQUENCY_STATUS_SHIFT),
a877e801
VS
5867 50))
5868 DRM_ERROR("timed out waiting for CDclk change\n");
30a970c6
JB
5869 }
5870
30a970c6
JB
5871 /* adjust self-refresh exit latency value */
5872 val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
5873 val &= ~0x7f;
5874
5875 /*
5876 * For high bandwidth configs, we set a higher latency in the bunit
5877 * so that the core display fetch happens in time to avoid underruns.
5878 */
dfcab17e 5879 if (cdclk == 400000)
30a970c6
JB
5880 val |= 4500 / 250; /* 4.5 usec */
5881 else
5882 val |= 3000 / 250; /* 3.0 usec */
5883 vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
54433e91 5884
a580516d 5885 mutex_unlock(&dev_priv->sb_lock);
30a970c6 5886
b6283055 5887 intel_update_cdclk(dev);
30a970c6
JB
5888}
5889
383c5a6a
VS
5890static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
5891{
fac5e23e 5892 struct drm_i915_private *dev_priv = to_i915(dev);
383c5a6a
VS
5893 u32 val, cmd;
5894
164dfd28
VK
5895 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5896 != dev_priv->cdclk_freq);
383c5a6a
VS
5897
5898 switch (cdclk) {
383c5a6a
VS
5899 case 333333:
5900 case 320000:
383c5a6a 5901 case 266667:
383c5a6a 5902 case 200000:
383c5a6a
VS
5903 break;
5904 default:
5f77eeb0 5905 MISSING_CASE(cdclk);
383c5a6a
VS
5906 return;
5907 }
5908
9d0d3fda
VS
5909 /*
5910 * Specs are full of misinformation, but testing on actual
5911 * hardware has shown that we just need to write the desired
5912 * CCK divider into the Punit register.
5913 */
5914 cmd = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
5915
383c5a6a
VS
5916 mutex_lock(&dev_priv->rps.hw_lock);
5917 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5918 val &= ~DSPFREQGUAR_MASK_CHV;
5919 val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
5920 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5921 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5922 DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
5923 50)) {
5924 DRM_ERROR("timed out waiting for CDclk change\n");
5925 }
5926 mutex_unlock(&dev_priv->rps.hw_lock);
5927
b6283055 5928 intel_update_cdclk(dev);
383c5a6a
VS
5929}
5930
30a970c6
JB
5931static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
5932 int max_pixclk)
5933{
6bcda4f0 5934 int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
6cca3195 5935 int limit = IS_CHERRYVIEW(dev_priv) ? 95 : 90;
29dc7ef3 5936
30a970c6
JB
5937 /*
5938 * Really only a few cases to deal with, as only 4 CDclks are supported:
5939 * 200MHz
5940 * 267MHz
29dc7ef3 5941 * 320/333MHz (depends on HPLL freq)
6cca3195
VS
5942 * 400MHz (VLV only)
5943 * So we check to see whether we're above 90% (VLV) or 95% (CHV)
5944 * of the lower bin and adjust if needed.
e37c67a1
VS
5945 *
5946 * We seem to get an unstable or solid color picture at 200MHz.
5947 * Not sure what's wrong. For now use 200MHz only when all pipes
5948 * are off.
30a970c6 5949 */
6cca3195
VS
5950 if (!IS_CHERRYVIEW(dev_priv) &&
5951 max_pixclk > freq_320*limit/100)
dfcab17e 5952 return 400000;
6cca3195 5953 else if (max_pixclk > 266667*limit/100)
29dc7ef3 5954 return freq_320;
e37c67a1 5955 else if (max_pixclk > 0)
dfcab17e 5956 return 266667;
e37c67a1
VS
5957 else
5958 return 200000;
30a970c6
JB
5959}
5960
324513c0 5961static int bxt_calc_cdclk(int max_pixclk)
f8437dd1 5962{
760e1477 5963 if (max_pixclk > 576000)
f8437dd1 5964 return 624000;
760e1477 5965 else if (max_pixclk > 384000)
f8437dd1 5966 return 576000;
760e1477 5967 else if (max_pixclk > 288000)
f8437dd1 5968 return 384000;
760e1477 5969 else if (max_pixclk > 144000)
f8437dd1
VK
5970 return 288000;
5971 else
5972 return 144000;
5973}
5974
e8788cbc 5975/* Compute the max pixel clock for new configuration. */
a821fc46
ACO
5976static int intel_mode_max_pixclk(struct drm_device *dev,
5977 struct drm_atomic_state *state)
30a970c6 5978{
565602d7 5979 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
fac5e23e 5980 struct drm_i915_private *dev_priv = to_i915(dev);
565602d7
ML
5981 struct drm_crtc *crtc;
5982 struct drm_crtc_state *crtc_state;
5983 unsigned max_pixclk = 0, i;
5984 enum pipe pipe;
30a970c6 5985
565602d7
ML
5986 memcpy(intel_state->min_pixclk, dev_priv->min_pixclk,
5987 sizeof(intel_state->min_pixclk));
304603f4 5988
565602d7
ML
5989 for_each_crtc_in_state(state, crtc, crtc_state, i) {
5990 int pixclk = 0;
5991
5992 if (crtc_state->enable)
5993 pixclk = crtc_state->adjusted_mode.crtc_clock;
304603f4 5994
565602d7 5995 intel_state->min_pixclk[i] = pixclk;
30a970c6
JB
5996 }
5997
565602d7
ML
5998 for_each_pipe(dev_priv, pipe)
5999 max_pixclk = max(intel_state->min_pixclk[pipe], max_pixclk);
6000
30a970c6
JB
6001 return max_pixclk;
6002}
6003
27c329ed 6004static int valleyview_modeset_calc_cdclk(struct drm_atomic_state *state)
30a970c6 6005{
27c329ed 6006 struct drm_device *dev = state->dev;
fac5e23e 6007 struct drm_i915_private *dev_priv = to_i915(dev);
27c329ed 6008 int max_pixclk = intel_mode_max_pixclk(dev, state);
1a617b77
ML
6009 struct intel_atomic_state *intel_state =
6010 to_intel_atomic_state(state);
30a970c6 6011
1a617b77 6012 intel_state->cdclk = intel_state->dev_cdclk =
27c329ed 6013 valleyview_calc_cdclk(dev_priv, max_pixclk);
0a9ab303 6014
1a617b77
ML
6015 if (!intel_state->active_crtcs)
6016 intel_state->dev_cdclk = valleyview_calc_cdclk(dev_priv, 0);
6017
27c329ed
ML
6018 return 0;
6019}
304603f4 6020
324513c0 6021static int bxt_modeset_calc_cdclk(struct drm_atomic_state *state)
27c329ed 6022{
4e5ca60f 6023 int max_pixclk = ilk_max_pixel_rate(state);
1a617b77
ML
6024 struct intel_atomic_state *intel_state =
6025 to_intel_atomic_state(state);
85a96e7a 6026
1a617b77 6027 intel_state->cdclk = intel_state->dev_cdclk =
324513c0 6028 bxt_calc_cdclk(max_pixclk);
85a96e7a 6029
1a617b77 6030 if (!intel_state->active_crtcs)
324513c0 6031 intel_state->dev_cdclk = bxt_calc_cdclk(0);
1a617b77 6032
27c329ed 6033 return 0;
30a970c6
JB
6034}
6035
1e69cd74
VS
6036static void vlv_program_pfi_credits(struct drm_i915_private *dev_priv)
6037{
6038 unsigned int credits, default_credits;
6039
6040 if (IS_CHERRYVIEW(dev_priv))
6041 default_credits = PFI_CREDIT(12);
6042 else
6043 default_credits = PFI_CREDIT(8);
6044
bfa7df01 6045 if (dev_priv->cdclk_freq >= dev_priv->czclk_freq) {
1e69cd74
VS
6046 /* CHV suggested value is 31 or 63 */
6047 if (IS_CHERRYVIEW(dev_priv))
fcc0008f 6048 credits = PFI_CREDIT_63;
1e69cd74
VS
6049 else
6050 credits = PFI_CREDIT(15);
6051 } else {
6052 credits = default_credits;
6053 }
6054
6055 /*
6056 * WA - write default credits before re-programming
6057 * FIXME: should we also set the resend bit here?
6058 */
6059 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
6060 default_credits);
6061
6062 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
6063 credits | PFI_CREDIT_RESEND);
6064
6065 /*
6066 * FIXME is this guaranteed to clear
6067 * immediately or should we poll for it?
6068 */
6069 WARN_ON(I915_READ(GCI_CONTROL) & PFI_CREDIT_RESEND);
6070}
6071
27c329ed 6072static void valleyview_modeset_commit_cdclk(struct drm_atomic_state *old_state)
30a970c6 6073{
a821fc46 6074 struct drm_device *dev = old_state->dev;
fac5e23e 6075 struct drm_i915_private *dev_priv = to_i915(dev);
1a617b77
ML
6076 struct intel_atomic_state *old_intel_state =
6077 to_intel_atomic_state(old_state);
6078 unsigned req_cdclk = old_intel_state->dev_cdclk;
30a970c6 6079
27c329ed
ML
6080 /*
6081 * FIXME: We can end up here with all power domains off, yet
6082 * with a CDCLK frequency other than the minimum. To account
6083 * for this take the PIPE-A power domain, which covers the HW
6084 * blocks needed for the following programming. This can be
6085 * removed once it's guaranteed that we get here either with
6086 * the minimum CDCLK set, or the required power domains
6087 * enabled.
6088 */
6089 intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
738c05c0 6090
27c329ed
ML
6091 if (IS_CHERRYVIEW(dev))
6092 cherryview_set_cdclk(dev, req_cdclk);
6093 else
6094 valleyview_set_cdclk(dev, req_cdclk);
738c05c0 6095
27c329ed 6096 vlv_program_pfi_credits(dev_priv);
1e69cd74 6097
27c329ed 6098 intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
30a970c6
JB
6099}
6100
89b667f8
JB
6101static void valleyview_crtc_enable(struct drm_crtc *crtc)
6102{
6103 struct drm_device *dev = crtc->dev;
a72e4c9f 6104 struct drm_i915_private *dev_priv = to_i915(dev);
89b667f8
JB
6105 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6106 struct intel_encoder *encoder;
b95c5321
ML
6107 struct intel_crtc_state *pipe_config =
6108 to_intel_crtc_state(crtc->state);
89b667f8 6109 int pipe = intel_crtc->pipe;
89b667f8 6110
53d9f4e9 6111 if (WARN_ON(intel_crtc->active))
89b667f8
JB
6112 return;
6113
37a5650b 6114 if (intel_crtc_has_dp_encoder(intel_crtc->config))
fe3cd48d 6115 intel_dp_set_m_n(intel_crtc, M1_N1);
5b18e57c
DV
6116
6117 intel_set_pipe_timings(intel_crtc);
bc58be60 6118 intel_set_pipe_src_size(intel_crtc);
5b18e57c 6119
c14b0485 6120 if (IS_CHERRYVIEW(dev) && pipe == PIPE_B) {
fac5e23e 6121 struct drm_i915_private *dev_priv = to_i915(dev);
c14b0485
VS
6122
6123 I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
6124 I915_WRITE(CHV_CANVAS(pipe), 0);
6125 }
6126
5b18e57c
DV
6127 i9xx_set_pipeconf(intel_crtc);
6128
89b667f8 6129 intel_crtc->active = true;
89b667f8 6130
a72e4c9f 6131 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4a3436e8 6132
89b667f8
JB
6133 for_each_encoder_on_crtc(dev, crtc, encoder)
6134 if (encoder->pre_pll_enable)
6135 encoder->pre_pll_enable(encoder);
6136
cd2d34d9
VS
6137 if (IS_CHERRYVIEW(dev)) {
6138 chv_prepare_pll(intel_crtc, intel_crtc->config);
6139 chv_enable_pll(intel_crtc, intel_crtc->config);
6140 } else {
6141 vlv_prepare_pll(intel_crtc, intel_crtc->config);
6142 vlv_enable_pll(intel_crtc, intel_crtc->config);
9d556c99 6143 }
89b667f8
JB
6144
6145 for_each_encoder_on_crtc(dev, crtc, encoder)
6146 if (encoder->pre_enable)
6147 encoder->pre_enable(encoder);
6148
2dd24552
JB
6149 i9xx_pfit_enable(intel_crtc);
6150
b95c5321 6151 intel_color_load_luts(&pipe_config->base);
63cbb074 6152
caed361d 6153 intel_update_watermarks(crtc);
e1fdc473 6154 intel_enable_pipe(intel_crtc);
be6a6f8e 6155
4b3a9526
VS
6156 assert_vblank_disabled(crtc);
6157 drm_crtc_vblank_on(crtc);
6158
f9b61ff6
DV
6159 for_each_encoder_on_crtc(dev, crtc, encoder)
6160 encoder->enable(encoder);
89b667f8
JB
6161}
6162
f13c2ef3
DV
6163static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
6164{
6165 struct drm_device *dev = crtc->base.dev;
fac5e23e 6166 struct drm_i915_private *dev_priv = to_i915(dev);
f13c2ef3 6167
6e3c9717
ACO
6168 I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
6169 I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
f13c2ef3
DV
6170}
6171
0b8765c6 6172static void i9xx_crtc_enable(struct drm_crtc *crtc)
79e53945
JB
6173{
6174 struct drm_device *dev = crtc->dev;
a72e4c9f 6175 struct drm_i915_private *dev_priv = to_i915(dev);
79e53945 6176 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 6177 struct intel_encoder *encoder;
b95c5321
ML
6178 struct intel_crtc_state *pipe_config =
6179 to_intel_crtc_state(crtc->state);
cd2d34d9 6180 enum pipe pipe = intel_crtc->pipe;
79e53945 6181
53d9f4e9 6182 if (WARN_ON(intel_crtc->active))
f7abfe8b
CW
6183 return;
6184
f13c2ef3
DV
6185 i9xx_set_pll_dividers(intel_crtc);
6186
37a5650b 6187 if (intel_crtc_has_dp_encoder(intel_crtc->config))
fe3cd48d 6188 intel_dp_set_m_n(intel_crtc, M1_N1);
5b18e57c
DV
6189
6190 intel_set_pipe_timings(intel_crtc);
bc58be60 6191 intel_set_pipe_src_size(intel_crtc);
5b18e57c 6192
5b18e57c
DV
6193 i9xx_set_pipeconf(intel_crtc);
6194
f7abfe8b 6195 intel_crtc->active = true;
6b383a7f 6196
4a3436e8 6197 if (!IS_GEN2(dev))
a72e4c9f 6198 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4a3436e8 6199
9d6d9f19
MK
6200 for_each_encoder_on_crtc(dev, crtc, encoder)
6201 if (encoder->pre_enable)
6202 encoder->pre_enable(encoder);
6203
f6736a1a
DV
6204 i9xx_enable_pll(intel_crtc);
6205
2dd24552
JB
6206 i9xx_pfit_enable(intel_crtc);
6207
b95c5321 6208 intel_color_load_luts(&pipe_config->base);
63cbb074 6209
f37fcc2a 6210 intel_update_watermarks(crtc);
e1fdc473 6211 intel_enable_pipe(intel_crtc);
be6a6f8e 6212
4b3a9526
VS
6213 assert_vblank_disabled(crtc);
6214 drm_crtc_vblank_on(crtc);
6215
f9b61ff6
DV
6216 for_each_encoder_on_crtc(dev, crtc, encoder)
6217 encoder->enable(encoder);
0b8765c6 6218}
79e53945 6219
87476d63
DV
6220static void i9xx_pfit_disable(struct intel_crtc *crtc)
6221{
6222 struct drm_device *dev = crtc->base.dev;
fac5e23e 6223 struct drm_i915_private *dev_priv = to_i915(dev);
87476d63 6224
6e3c9717 6225 if (!crtc->config->gmch_pfit.control)
328d8e82 6226 return;
87476d63 6227
328d8e82 6228 assert_pipe_disabled(dev_priv, crtc->pipe);
87476d63 6229
328d8e82
DV
6230 DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
6231 I915_READ(PFIT_CONTROL));
6232 I915_WRITE(PFIT_CONTROL, 0);
87476d63
DV
6233}
6234
0b8765c6
JB
6235static void i9xx_crtc_disable(struct drm_crtc *crtc)
6236{
6237 struct drm_device *dev = crtc->dev;
fac5e23e 6238 struct drm_i915_private *dev_priv = to_i915(dev);
0b8765c6 6239 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 6240 struct intel_encoder *encoder;
0b8765c6 6241 int pipe = intel_crtc->pipe;
ef9c3aee 6242
6304cd91
VS
6243 /*
6244 * On gen2 planes are double buffered but the pipe isn't, so we must
6245 * wait for planes to fully turn off before disabling the pipe.
6246 */
90e83e53
ACO
6247 if (IS_GEN2(dev))
6248 intel_wait_for_vblank(dev, pipe);
6304cd91 6249
4b3a9526
VS
6250 for_each_encoder_on_crtc(dev, crtc, encoder)
6251 encoder->disable(encoder);
6252
f9b61ff6
DV
6253 drm_crtc_vblank_off(crtc);
6254 assert_vblank_disabled(crtc);
6255
575f7ab7 6256 intel_disable_pipe(intel_crtc);
24a1f16d 6257
87476d63 6258 i9xx_pfit_disable(intel_crtc);
24a1f16d 6259
89b667f8
JB
6260 for_each_encoder_on_crtc(dev, crtc, encoder)
6261 if (encoder->post_disable)
6262 encoder->post_disable(encoder);
6263
d7edc4e5 6264 if (!intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_DSI)) {
076ed3b2
CML
6265 if (IS_CHERRYVIEW(dev))
6266 chv_disable_pll(dev_priv, pipe);
6267 else if (IS_VALLEYVIEW(dev))
6268 vlv_disable_pll(dev_priv, pipe);
6269 else
1c4e0274 6270 i9xx_disable_pll(intel_crtc);
076ed3b2 6271 }
0b8765c6 6272
d6db995f
VS
6273 for_each_encoder_on_crtc(dev, crtc, encoder)
6274 if (encoder->post_pll_disable)
6275 encoder->post_pll_disable(encoder);
6276
4a3436e8 6277 if (!IS_GEN2(dev))
a72e4c9f 6278 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
0b8765c6
JB
6279}
6280
b17d48e2
ML
6281static void intel_crtc_disable_noatomic(struct drm_crtc *crtc)
6282{
842e0307 6283 struct intel_encoder *encoder;
b17d48e2
ML
6284 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6285 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
6286 enum intel_display_power_domain domain;
6287 unsigned long domains;
6288
6289 if (!intel_crtc->active)
6290 return;
6291
a539205a 6292 if (to_intel_plane_state(crtc->primary->state)->visible) {
5a21b665 6293 WARN_ON(intel_crtc->flip_work);
fc32b1fd 6294
2622a081 6295 intel_pre_disable_primary_noatomic(crtc);
54a41961
ML
6296
6297 intel_crtc_disable_planes(crtc, 1 << drm_plane_index(crtc->primary));
6298 to_intel_plane_state(crtc->primary->state)->visible = false;
a539205a
ML
6299 }
6300
b17d48e2 6301 dev_priv->display.crtc_disable(crtc);
842e0307 6302
78108b7c
VS
6303 DRM_DEBUG_KMS("[CRTC:%d:%s] hw state adjusted, was enabled, now disabled\n",
6304 crtc->base.id, crtc->name);
842e0307
ML
6305
6306 WARN_ON(drm_atomic_set_mode_for_crtc(crtc->state, NULL) < 0);
6307 crtc->state->active = false;
37d9078b 6308 intel_crtc->active = false;
842e0307
ML
6309 crtc->enabled = false;
6310 crtc->state->connector_mask = 0;
6311 crtc->state->encoder_mask = 0;
6312
6313 for_each_encoder_on_crtc(crtc->dev, crtc, encoder)
6314 encoder->base.crtc = NULL;
6315
58f9c0bc 6316 intel_fbc_disable(intel_crtc);
37d9078b 6317 intel_update_watermarks(crtc);
1f7457b1 6318 intel_disable_shared_dpll(intel_crtc);
b17d48e2
ML
6319
6320 domains = intel_crtc->enabled_power_domains;
6321 for_each_power_domain(domain, domains)
6322 intel_display_power_put(dev_priv, domain);
6323 intel_crtc->enabled_power_domains = 0;
565602d7
ML
6324
6325 dev_priv->active_crtcs &= ~(1 << intel_crtc->pipe);
6326 dev_priv->min_pixclk[intel_crtc->pipe] = 0;
b17d48e2
ML
6327}
6328
6b72d486
ML
6329/*
6330 * turn all crtc's off, but do not adjust state
6331 * This has to be paired with a call to intel_modeset_setup_hw_state.
6332 */
70e0bd74 6333int intel_display_suspend(struct drm_device *dev)
ee7b9f93 6334{
e2c8b870 6335 struct drm_i915_private *dev_priv = to_i915(dev);
70e0bd74 6336 struct drm_atomic_state *state;
e2c8b870 6337 int ret;
70e0bd74 6338
e2c8b870
ML
6339 state = drm_atomic_helper_suspend(dev);
6340 ret = PTR_ERR_OR_ZERO(state);
70e0bd74
ML
6341 if (ret)
6342 DRM_ERROR("Suspending crtc's failed with %i\n", ret);
e2c8b870
ML
6343 else
6344 dev_priv->modeset_restore_state = state;
70e0bd74 6345 return ret;
ee7b9f93
JB
6346}
6347
ea5b213a 6348void intel_encoder_destroy(struct drm_encoder *encoder)
7e7d76c3 6349{
4ef69c7a 6350 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
ea5b213a 6351
ea5b213a
CW
6352 drm_encoder_cleanup(encoder);
6353 kfree(intel_encoder);
7e7d76c3
JB
6354}
6355
0a91ca29
DV
6356/* Cross check the actual hw state with our own modeset state tracking (and it's
6357 * internal consistency). */
5a21b665 6358static void intel_connector_verify_state(struct intel_connector *connector)
79e53945 6359{
5a21b665 6360 struct drm_crtc *crtc = connector->base.state->crtc;
35dd3c64
ML
6361
6362 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
6363 connector->base.base.id,
6364 connector->base.name);
6365
0a91ca29 6366 if (connector->get_hw_state(connector)) {
e85376cb 6367 struct intel_encoder *encoder = connector->encoder;
5a21b665 6368 struct drm_connector_state *conn_state = connector->base.state;
0a91ca29 6369
35dd3c64
ML
6370 I915_STATE_WARN(!crtc,
6371 "connector enabled without attached crtc\n");
0a91ca29 6372
35dd3c64
ML
6373 if (!crtc)
6374 return;
6375
6376 I915_STATE_WARN(!crtc->state->active,
6377 "connector is active, but attached crtc isn't\n");
6378
e85376cb 6379 if (!encoder || encoder->type == INTEL_OUTPUT_DP_MST)
35dd3c64
ML
6380 return;
6381
e85376cb 6382 I915_STATE_WARN(conn_state->best_encoder != &encoder->base,
35dd3c64
ML
6383 "atomic encoder doesn't match attached encoder\n");
6384
e85376cb 6385 I915_STATE_WARN(conn_state->crtc != encoder->base.crtc,
35dd3c64
ML
6386 "attached encoder crtc differs from connector crtc\n");
6387 } else {
4d688a2a
ML
6388 I915_STATE_WARN(crtc && crtc->state->active,
6389 "attached crtc is active, but connector isn't\n");
5a21b665 6390 I915_STATE_WARN(!crtc && connector->base.state->best_encoder,
35dd3c64 6391 "best encoder set without crtc!\n");
0a91ca29 6392 }
79e53945
JB
6393}
6394
08d9bc92
ACO
6395int intel_connector_init(struct intel_connector *connector)
6396{
5350a031 6397 drm_atomic_helper_connector_reset(&connector->base);
08d9bc92 6398
5350a031 6399 if (!connector->base.state)
08d9bc92
ACO
6400 return -ENOMEM;
6401
08d9bc92
ACO
6402 return 0;
6403}
6404
6405struct intel_connector *intel_connector_alloc(void)
6406{
6407 struct intel_connector *connector;
6408
6409 connector = kzalloc(sizeof *connector, GFP_KERNEL);
6410 if (!connector)
6411 return NULL;
6412
6413 if (intel_connector_init(connector) < 0) {
6414 kfree(connector);
6415 return NULL;
6416 }
6417
6418 return connector;
6419}
6420
f0947c37
DV
6421/* Simple connector->get_hw_state implementation for encoders that support only
6422 * one connector and no cloning and hence the encoder state determines the state
6423 * of the connector. */
6424bool intel_connector_get_hw_state(struct intel_connector *connector)
ea5b213a 6425{
24929352 6426 enum pipe pipe = 0;
f0947c37 6427 struct intel_encoder *encoder = connector->encoder;
ea5b213a 6428
f0947c37 6429 return encoder->get_hw_state(encoder, &pipe);
ea5b213a
CW
6430}
6431
6d293983 6432static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
d272ddfa 6433{
6d293983
ACO
6434 if (crtc_state->base.enable && crtc_state->has_pch_encoder)
6435 return crtc_state->fdi_lanes;
d272ddfa
VS
6436
6437 return 0;
6438}
6439
6d293983 6440static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
5cec258b 6441 struct intel_crtc_state *pipe_config)
1857e1da 6442{
6d293983
ACO
6443 struct drm_atomic_state *state = pipe_config->base.state;
6444 struct intel_crtc *other_crtc;
6445 struct intel_crtc_state *other_crtc_state;
6446
1857e1da
DV
6447 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
6448 pipe_name(pipe), pipe_config->fdi_lanes);
6449 if (pipe_config->fdi_lanes > 4) {
6450 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
6451 pipe_name(pipe), pipe_config->fdi_lanes);
6d293983 6452 return -EINVAL;
1857e1da
DV
6453 }
6454
bafb6553 6455 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
1857e1da
DV
6456 if (pipe_config->fdi_lanes > 2) {
6457 DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
6458 pipe_config->fdi_lanes);
6d293983 6459 return -EINVAL;
1857e1da 6460 } else {
6d293983 6461 return 0;
1857e1da
DV
6462 }
6463 }
6464
6465 if (INTEL_INFO(dev)->num_pipes == 2)
6d293983 6466 return 0;
1857e1da
DV
6467
6468 /* Ivybridge 3 pipe is really complicated */
6469 switch (pipe) {
6470 case PIPE_A:
6d293983 6471 return 0;
1857e1da 6472 case PIPE_B:
6d293983
ACO
6473 if (pipe_config->fdi_lanes <= 2)
6474 return 0;
6475
6476 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_C));
6477 other_crtc_state =
6478 intel_atomic_get_crtc_state(state, other_crtc);
6479 if (IS_ERR(other_crtc_state))
6480 return PTR_ERR(other_crtc_state);
6481
6482 if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
1857e1da
DV
6483 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
6484 pipe_name(pipe), pipe_config->fdi_lanes);
6d293983 6485 return -EINVAL;
1857e1da 6486 }
6d293983 6487 return 0;
1857e1da 6488 case PIPE_C:
251cc67c
VS
6489 if (pipe_config->fdi_lanes > 2) {
6490 DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
6491 pipe_name(pipe), pipe_config->fdi_lanes);
6d293983 6492 return -EINVAL;
251cc67c 6493 }
6d293983
ACO
6494
6495 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_B));
6496 other_crtc_state =
6497 intel_atomic_get_crtc_state(state, other_crtc);
6498 if (IS_ERR(other_crtc_state))
6499 return PTR_ERR(other_crtc_state);
6500
6501 if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
1857e1da 6502 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
6d293983 6503 return -EINVAL;
1857e1da 6504 }
6d293983 6505 return 0;
1857e1da
DV
6506 default:
6507 BUG();
6508 }
6509}
6510
e29c22c0
DV
6511#define RETRY 1
6512static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
5cec258b 6513 struct intel_crtc_state *pipe_config)
877d48d5 6514{
1857e1da 6515 struct drm_device *dev = intel_crtc->base.dev;
7c5f93b0 6516 const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
6d293983
ACO
6517 int lane, link_bw, fdi_dotclock, ret;
6518 bool needs_recompute = false;
877d48d5 6519
e29c22c0 6520retry:
877d48d5
DV
6521 /* FDI is a binary signal running at ~2.7GHz, encoding
6522 * each output octet as 10 bits. The actual frequency
6523 * is stored as a divider into a 100MHz clock, and the
6524 * mode pixel clock is stored in units of 1KHz.
6525 * Hence the bw of each lane in terms of the mode signal
6526 * is:
6527 */
21a727b3 6528 link_bw = intel_fdi_link_freq(to_i915(dev), pipe_config);
877d48d5 6529
241bfc38 6530 fdi_dotclock = adjusted_mode->crtc_clock;
877d48d5 6531
2bd89a07 6532 lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
877d48d5
DV
6533 pipe_config->pipe_bpp);
6534
6535 pipe_config->fdi_lanes = lane;
6536
2bd89a07 6537 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
877d48d5 6538 link_bw, &pipe_config->fdi_m_n);
1857e1da 6539
e3b247da 6540 ret = ironlake_check_fdi_lanes(dev, intel_crtc->pipe, pipe_config);
6d293983 6541 if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
e29c22c0
DV
6542 pipe_config->pipe_bpp -= 2*3;
6543 DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
6544 pipe_config->pipe_bpp);
6545 needs_recompute = true;
6546 pipe_config->bw_constrained = true;
6547
6548 goto retry;
6549 }
6550
6551 if (needs_recompute)
6552 return RETRY;
6553
6d293983 6554 return ret;
877d48d5
DV
6555}
6556
8cfb3407
VS
6557static bool pipe_config_supports_ips(struct drm_i915_private *dev_priv,
6558 struct intel_crtc_state *pipe_config)
6559{
6560 if (pipe_config->pipe_bpp > 24)
6561 return false;
6562
6563 /* HSW can handle pixel rate up to cdclk? */
2d1fe073 6564 if (IS_HASWELL(dev_priv))
8cfb3407
VS
6565 return true;
6566
6567 /*
b432e5cf
VS
6568 * We compare against max which means we must take
6569 * the increased cdclk requirement into account when
6570 * calculating the new cdclk.
6571 *
6572 * Should measure whether using a lower cdclk w/o IPS
8cfb3407
VS
6573 */
6574 return ilk_pipe_pixel_rate(pipe_config) <=
6575 dev_priv->max_cdclk_freq * 95 / 100;
6576}
6577
42db64ef 6578static void hsw_compute_ips_config(struct intel_crtc *crtc,
5cec258b 6579 struct intel_crtc_state *pipe_config)
42db64ef 6580{
8cfb3407 6581 struct drm_device *dev = crtc->base.dev;
fac5e23e 6582 struct drm_i915_private *dev_priv = to_i915(dev);
8cfb3407 6583
d330a953 6584 pipe_config->ips_enabled = i915.enable_ips &&
8cfb3407
VS
6585 hsw_crtc_supports_ips(crtc) &&
6586 pipe_config_supports_ips(dev_priv, pipe_config);
42db64ef
PZ
6587}
6588
39acb4aa
VS
6589static bool intel_crtc_supports_double_wide(const struct intel_crtc *crtc)
6590{
6591 const struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6592
6593 /* GDG double wide on either pipe, otherwise pipe A only */
6594 return INTEL_INFO(dev_priv)->gen < 4 &&
6595 (crtc->pipe == PIPE_A || IS_I915G(dev_priv));
6596}
6597
a43f6e0f 6598static int intel_crtc_compute_config(struct intel_crtc *crtc,
5cec258b 6599 struct intel_crtc_state *pipe_config)
79e53945 6600{
a43f6e0f 6601 struct drm_device *dev = crtc->base.dev;
fac5e23e 6602 struct drm_i915_private *dev_priv = to_i915(dev);
7c5f93b0 6603 const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
f3261156 6604 int clock_limit = dev_priv->max_dotclk_freq;
89749350 6605
cf532bb2 6606 if (INTEL_INFO(dev)->gen < 4) {
f3261156 6607 clock_limit = dev_priv->max_cdclk_freq * 9 / 10;
cf532bb2
VS
6608
6609 /*
39acb4aa 6610 * Enable double wide mode when the dot clock
cf532bb2 6611 * is > 90% of the (display) core speed.
cf532bb2 6612 */
39acb4aa
VS
6613 if (intel_crtc_supports_double_wide(crtc) &&
6614 adjusted_mode->crtc_clock > clock_limit) {
f3261156 6615 clock_limit = dev_priv->max_dotclk_freq;
cf532bb2 6616 pipe_config->double_wide = true;
ad3a4479 6617 }
f3261156 6618 }
ad3a4479 6619
f3261156
VS
6620 if (adjusted_mode->crtc_clock > clock_limit) {
6621 DRM_DEBUG_KMS("requested pixel clock (%d kHz) too high (max: %d kHz, double wide: %s)\n",
6622 adjusted_mode->crtc_clock, clock_limit,
6623 yesno(pipe_config->double_wide));
6624 return -EINVAL;
2c07245f 6625 }
89749350 6626
1d1d0e27
VS
6627 /*
6628 * Pipe horizontal size must be even in:
6629 * - DVO ganged mode
6630 * - LVDS dual channel mode
6631 * - Double wide pipe
6632 */
2d84d2b3 6633 if ((intel_crtc_has_type(pipe_config, INTEL_OUTPUT_LVDS) &&
1d1d0e27
VS
6634 intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
6635 pipe_config->pipe_src_w &= ~1;
6636
8693a824
DL
6637 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
6638 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
44f46b42
CW
6639 */
6640 if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
aad941d5 6641 adjusted_mode->crtc_hsync_start == adjusted_mode->crtc_hdisplay)
e29c22c0 6642 return -EINVAL;
44f46b42 6643
f5adf94e 6644 if (HAS_IPS(dev))
a43f6e0f
DV
6645 hsw_compute_ips_config(crtc, pipe_config);
6646
877d48d5 6647 if (pipe_config->has_pch_encoder)
a43f6e0f 6648 return ironlake_fdi_compute_config(crtc, pipe_config);
877d48d5 6649
cf5a15be 6650 return 0;
79e53945
JB
6651}
6652
1652d19e
VS
6653static int skylake_get_display_clock_speed(struct drm_device *dev)
6654{
6655 struct drm_i915_private *dev_priv = to_i915(dev);
ea61791e 6656 uint32_t cdctl;
1652d19e 6657
ea61791e 6658 skl_dpll0_update(dev_priv);
1652d19e 6659
63911d72 6660 if (dev_priv->cdclk_pll.vco == 0)
709e05c3 6661 return dev_priv->cdclk_pll.ref;
1652d19e 6662
ea61791e 6663 cdctl = I915_READ(CDCLK_CTL);
1652d19e 6664
63911d72 6665 if (dev_priv->cdclk_pll.vco == 8640000) {
1652d19e
VS
6666 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6667 case CDCLK_FREQ_450_432:
6668 return 432000;
6669 case CDCLK_FREQ_337_308:
487ed2e4 6670 return 308571;
ea61791e
VS
6671 case CDCLK_FREQ_540:
6672 return 540000;
1652d19e 6673 case CDCLK_FREQ_675_617:
487ed2e4 6674 return 617143;
1652d19e 6675 default:
ea61791e 6676 MISSING_CASE(cdctl & CDCLK_FREQ_SEL_MASK);
1652d19e
VS
6677 }
6678 } else {
1652d19e
VS
6679 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6680 case CDCLK_FREQ_450_432:
6681 return 450000;
6682 case CDCLK_FREQ_337_308:
6683 return 337500;
ea61791e
VS
6684 case CDCLK_FREQ_540:
6685 return 540000;
1652d19e
VS
6686 case CDCLK_FREQ_675_617:
6687 return 675000;
6688 default:
ea61791e 6689 MISSING_CASE(cdctl & CDCLK_FREQ_SEL_MASK);
1652d19e
VS
6690 }
6691 }
6692
709e05c3 6693 return dev_priv->cdclk_pll.ref;
1652d19e
VS
6694}
6695
83d7c81f
VS
6696static void bxt_de_pll_update(struct drm_i915_private *dev_priv)
6697{
6698 u32 val;
6699
6700 dev_priv->cdclk_pll.ref = 19200;
1c3f7700 6701 dev_priv->cdclk_pll.vco = 0;
83d7c81f
VS
6702
6703 val = I915_READ(BXT_DE_PLL_ENABLE);
1c3f7700 6704 if ((val & BXT_DE_PLL_PLL_ENABLE) == 0)
83d7c81f 6705 return;
83d7c81f 6706
1c3f7700
ID
6707 if (WARN_ON((val & BXT_DE_PLL_LOCK) == 0))
6708 return;
83d7c81f
VS
6709
6710 val = I915_READ(BXT_DE_PLL_CTL);
6711 dev_priv->cdclk_pll.vco = (val & BXT_DE_PLL_RATIO_MASK) *
6712 dev_priv->cdclk_pll.ref;
6713}
6714
acd3f3d3
BP
6715static int broxton_get_display_clock_speed(struct drm_device *dev)
6716{
6717 struct drm_i915_private *dev_priv = to_i915(dev);
f5986242
VS
6718 u32 divider;
6719 int div, vco;
acd3f3d3 6720
83d7c81f
VS
6721 bxt_de_pll_update(dev_priv);
6722
f5986242
VS
6723 vco = dev_priv->cdclk_pll.vco;
6724 if (vco == 0)
6725 return dev_priv->cdclk_pll.ref;
acd3f3d3 6726
f5986242 6727 divider = I915_READ(CDCLK_CTL) & BXT_CDCLK_CD2X_DIV_SEL_MASK;
acd3f3d3 6728
f5986242 6729 switch (divider) {
acd3f3d3 6730 case BXT_CDCLK_CD2X_DIV_SEL_1:
f5986242
VS
6731 div = 2;
6732 break;
acd3f3d3 6733 case BXT_CDCLK_CD2X_DIV_SEL_1_5:
f5986242
VS
6734 div = 3;
6735 break;
acd3f3d3 6736 case BXT_CDCLK_CD2X_DIV_SEL_2:
f5986242
VS
6737 div = 4;
6738 break;
acd3f3d3 6739 case BXT_CDCLK_CD2X_DIV_SEL_4:
f5986242
VS
6740 div = 8;
6741 break;
6742 default:
6743 MISSING_CASE(divider);
6744 return dev_priv->cdclk_pll.ref;
acd3f3d3
BP
6745 }
6746
f5986242 6747 return DIV_ROUND_CLOSEST(vco, div);
acd3f3d3
BP
6748}
6749
1652d19e
VS
6750static int broadwell_get_display_clock_speed(struct drm_device *dev)
6751{
fac5e23e 6752 struct drm_i915_private *dev_priv = to_i915(dev);
1652d19e
VS
6753 uint32_t lcpll = I915_READ(LCPLL_CTL);
6754 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6755
6756 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6757 return 800000;
6758 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6759 return 450000;
6760 else if (freq == LCPLL_CLK_FREQ_450)
6761 return 450000;
6762 else if (freq == LCPLL_CLK_FREQ_54O_BDW)
6763 return 540000;
6764 else if (freq == LCPLL_CLK_FREQ_337_5_BDW)
6765 return 337500;
6766 else
6767 return 675000;
6768}
6769
6770static int haswell_get_display_clock_speed(struct drm_device *dev)
6771{
fac5e23e 6772 struct drm_i915_private *dev_priv = to_i915(dev);
1652d19e
VS
6773 uint32_t lcpll = I915_READ(LCPLL_CTL);
6774 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6775
6776 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6777 return 800000;
6778 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6779 return 450000;
6780 else if (freq == LCPLL_CLK_FREQ_450)
6781 return 450000;
6782 else if (IS_HSW_ULT(dev))
6783 return 337500;
6784 else
6785 return 540000;
79e53945
JB
6786}
6787
25eb05fc
JB
6788static int valleyview_get_display_clock_speed(struct drm_device *dev)
6789{
bfa7df01
VS
6790 return vlv_get_cck_clock_hpll(to_i915(dev), "cdclk",
6791 CCK_DISPLAY_CLOCK_CONTROL);
25eb05fc
JB
6792}
6793
b37a6434
VS
6794static int ilk_get_display_clock_speed(struct drm_device *dev)
6795{
6796 return 450000;
6797}
6798
e70236a8
JB
6799static int i945_get_display_clock_speed(struct drm_device *dev)
6800{
6801 return 400000;
6802}
79e53945 6803
e70236a8 6804static int i915_get_display_clock_speed(struct drm_device *dev)
79e53945 6805{
e907f170 6806 return 333333;
e70236a8 6807}
79e53945 6808
e70236a8
JB
6809static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
6810{
6811 return 200000;
6812}
79e53945 6813
257a7ffc
DV
6814static int pnv_get_display_clock_speed(struct drm_device *dev)
6815{
6816 u16 gcfgc = 0;
6817
6818 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6819
6820 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6821 case GC_DISPLAY_CLOCK_267_MHZ_PNV:
e907f170 6822 return 266667;
257a7ffc 6823 case GC_DISPLAY_CLOCK_333_MHZ_PNV:
e907f170 6824 return 333333;
257a7ffc 6825 case GC_DISPLAY_CLOCK_444_MHZ_PNV:
e907f170 6826 return 444444;
257a7ffc
DV
6827 case GC_DISPLAY_CLOCK_200_MHZ_PNV:
6828 return 200000;
6829 default:
6830 DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
6831 case GC_DISPLAY_CLOCK_133_MHZ_PNV:
e907f170 6832 return 133333;
257a7ffc 6833 case GC_DISPLAY_CLOCK_167_MHZ_PNV:
e907f170 6834 return 166667;
257a7ffc
DV
6835 }
6836}
6837
e70236a8
JB
6838static int i915gm_get_display_clock_speed(struct drm_device *dev)
6839{
6840 u16 gcfgc = 0;
79e53945 6841
e70236a8
JB
6842 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6843
6844 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
e907f170 6845 return 133333;
e70236a8
JB
6846 else {
6847 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6848 case GC_DISPLAY_CLOCK_333_MHZ:
e907f170 6849 return 333333;
e70236a8
JB
6850 default:
6851 case GC_DISPLAY_CLOCK_190_200_MHZ:
6852 return 190000;
79e53945 6853 }
e70236a8
JB
6854 }
6855}
6856
6857static int i865_get_display_clock_speed(struct drm_device *dev)
6858{
e907f170 6859 return 266667;
e70236a8
JB
6860}
6861
1b1d2716 6862static int i85x_get_display_clock_speed(struct drm_device *dev)
e70236a8
JB
6863{
6864 u16 hpllcc = 0;
1b1d2716 6865
65cd2b3f
VS
6866 /*
6867 * 852GM/852GMV only supports 133 MHz and the HPLLCC
6868 * encoding is different :(
6869 * FIXME is this the right way to detect 852GM/852GMV?
6870 */
6871 if (dev->pdev->revision == 0x1)
6872 return 133333;
6873
1b1d2716
VS
6874 pci_bus_read_config_word(dev->pdev->bus,
6875 PCI_DEVFN(0, 3), HPLLCC, &hpllcc);
6876
e70236a8
JB
6877 /* Assume that the hardware is in the high speed state. This
6878 * should be the default.
6879 */
6880 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
6881 case GC_CLOCK_133_200:
1b1d2716 6882 case GC_CLOCK_133_200_2:
e70236a8
JB
6883 case GC_CLOCK_100_200:
6884 return 200000;
6885 case GC_CLOCK_166_250:
6886 return 250000;
6887 case GC_CLOCK_100_133:
e907f170 6888 return 133333;
1b1d2716
VS
6889 case GC_CLOCK_133_266:
6890 case GC_CLOCK_133_266_2:
6891 case GC_CLOCK_166_266:
6892 return 266667;
e70236a8 6893 }
79e53945 6894
e70236a8
JB
6895 /* Shouldn't happen */
6896 return 0;
6897}
79e53945 6898
e70236a8
JB
6899static int i830_get_display_clock_speed(struct drm_device *dev)
6900{
e907f170 6901 return 133333;
79e53945
JB
6902}
6903
34edce2f
VS
6904static unsigned int intel_hpll_vco(struct drm_device *dev)
6905{
fac5e23e 6906 struct drm_i915_private *dev_priv = to_i915(dev);
34edce2f
VS
6907 static const unsigned int blb_vco[8] = {
6908 [0] = 3200000,
6909 [1] = 4000000,
6910 [2] = 5333333,
6911 [3] = 4800000,
6912 [4] = 6400000,
6913 };
6914 static const unsigned int pnv_vco[8] = {
6915 [0] = 3200000,
6916 [1] = 4000000,
6917 [2] = 5333333,
6918 [3] = 4800000,
6919 [4] = 2666667,
6920 };
6921 static const unsigned int cl_vco[8] = {
6922 [0] = 3200000,
6923 [1] = 4000000,
6924 [2] = 5333333,
6925 [3] = 6400000,
6926 [4] = 3333333,
6927 [5] = 3566667,
6928 [6] = 4266667,
6929 };
6930 static const unsigned int elk_vco[8] = {
6931 [0] = 3200000,
6932 [1] = 4000000,
6933 [2] = 5333333,
6934 [3] = 4800000,
6935 };
6936 static const unsigned int ctg_vco[8] = {
6937 [0] = 3200000,
6938 [1] = 4000000,
6939 [2] = 5333333,
6940 [3] = 6400000,
6941 [4] = 2666667,
6942 [5] = 4266667,
6943 };
6944 const unsigned int *vco_table;
6945 unsigned int vco;
6946 uint8_t tmp = 0;
6947
6948 /* FIXME other chipsets? */
6949 if (IS_GM45(dev))
6950 vco_table = ctg_vco;
6951 else if (IS_G4X(dev))
6952 vco_table = elk_vco;
6953 else if (IS_CRESTLINE(dev))
6954 vco_table = cl_vco;
6955 else if (IS_PINEVIEW(dev))
6956 vco_table = pnv_vco;
6957 else if (IS_G33(dev))
6958 vco_table = blb_vco;
6959 else
6960 return 0;
6961
6962 tmp = I915_READ(IS_MOBILE(dev) ? HPLLVCO_MOBILE : HPLLVCO);
6963
6964 vco = vco_table[tmp & 0x7];
6965 if (vco == 0)
6966 DRM_ERROR("Bad HPLL VCO (HPLLVCO=0x%02x)\n", tmp);
6967 else
6968 DRM_DEBUG_KMS("HPLL VCO %u kHz\n", vco);
6969
6970 return vco;
6971}
6972
6973static int gm45_get_display_clock_speed(struct drm_device *dev)
6974{
6975 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
6976 uint16_t tmp = 0;
6977
6978 pci_read_config_word(dev->pdev, GCFGC, &tmp);
6979
6980 cdclk_sel = (tmp >> 12) & 0x1;
6981
6982 switch (vco) {
6983 case 2666667:
6984 case 4000000:
6985 case 5333333:
6986 return cdclk_sel ? 333333 : 222222;
6987 case 3200000:
6988 return cdclk_sel ? 320000 : 228571;
6989 default:
6990 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u, CFGC=0x%04x\n", vco, tmp);
6991 return 222222;
6992 }
6993}
6994
6995static int i965gm_get_display_clock_speed(struct drm_device *dev)
6996{
6997 static const uint8_t div_3200[] = { 16, 10, 8 };
6998 static const uint8_t div_4000[] = { 20, 12, 10 };
6999 static const uint8_t div_5333[] = { 24, 16, 14 };
7000 const uint8_t *div_table;
7001 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
7002 uint16_t tmp = 0;
7003
7004 pci_read_config_word(dev->pdev, GCFGC, &tmp);
7005
7006 cdclk_sel = ((tmp >> 8) & 0x1f) - 1;
7007
7008 if (cdclk_sel >= ARRAY_SIZE(div_3200))
7009 goto fail;
7010
7011 switch (vco) {
7012 case 3200000:
7013 div_table = div_3200;
7014 break;
7015 case 4000000:
7016 div_table = div_4000;
7017 break;
7018 case 5333333:
7019 div_table = div_5333;
7020 break;
7021 default:
7022 goto fail;
7023 }
7024
7025 return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
7026
caf4e252 7027fail:
34edce2f
VS
7028 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%04x\n", vco, tmp);
7029 return 200000;
7030}
7031
7032static int g33_get_display_clock_speed(struct drm_device *dev)
7033{
7034 static const uint8_t div_3200[] = { 12, 10, 8, 7, 5, 16 };
7035 static const uint8_t div_4000[] = { 14, 12, 10, 8, 6, 20 };
7036 static const uint8_t div_4800[] = { 20, 14, 12, 10, 8, 24 };
7037 static const uint8_t div_5333[] = { 20, 16, 12, 12, 8, 28 };
7038 const uint8_t *div_table;
7039 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
7040 uint16_t tmp = 0;
7041
7042 pci_read_config_word(dev->pdev, GCFGC, &tmp);
7043
7044 cdclk_sel = (tmp >> 4) & 0x7;
7045
7046 if (cdclk_sel >= ARRAY_SIZE(div_3200))
7047 goto fail;
7048
7049 switch (vco) {
7050 case 3200000:
7051 div_table = div_3200;
7052 break;
7053 case 4000000:
7054 div_table = div_4000;
7055 break;
7056 case 4800000:
7057 div_table = div_4800;
7058 break;
7059 case 5333333:
7060 div_table = div_5333;
7061 break;
7062 default:
7063 goto fail;
7064 }
7065
7066 return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
7067
caf4e252 7068fail:
34edce2f
VS
7069 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%08x\n", vco, tmp);
7070 return 190476;
7071}
7072
2c07245f 7073static void
a65851af 7074intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
2c07245f 7075{
a65851af
VS
7076 while (*num > DATA_LINK_M_N_MASK ||
7077 *den > DATA_LINK_M_N_MASK) {
2c07245f
ZW
7078 *num >>= 1;
7079 *den >>= 1;
7080 }
7081}
7082
a65851af
VS
7083static void compute_m_n(unsigned int m, unsigned int n,
7084 uint32_t *ret_m, uint32_t *ret_n)
7085{
7086 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
7087 *ret_m = div_u64((uint64_t) m * *ret_n, n);
7088 intel_reduce_m_n_ratio(ret_m, ret_n);
7089}
7090
e69d0bc1
DV
7091void
7092intel_link_compute_m_n(int bits_per_pixel, int nlanes,
7093 int pixel_clock, int link_clock,
7094 struct intel_link_m_n *m_n)
2c07245f 7095{
e69d0bc1 7096 m_n->tu = 64;
a65851af
VS
7097
7098 compute_m_n(bits_per_pixel * pixel_clock,
7099 link_clock * nlanes * 8,
7100 &m_n->gmch_m, &m_n->gmch_n);
7101
7102 compute_m_n(pixel_clock, link_clock,
7103 &m_n->link_m, &m_n->link_n);
2c07245f
ZW
7104}
7105
a7615030
CW
7106static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
7107{
d330a953
JN
7108 if (i915.panel_use_ssc >= 0)
7109 return i915.panel_use_ssc != 0;
41aa3448 7110 return dev_priv->vbt.lvds_use_ssc
435793df 7111 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
a7615030
CW
7112}
7113
7429e9d4 7114static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
c65d77d8 7115{
7df00d7a 7116 return (1 << dpll->n) << 16 | dpll->m2;
7429e9d4 7117}
f47709a9 7118
7429e9d4
DV
7119static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
7120{
7121 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
c65d77d8
JB
7122}
7123
f47709a9 7124static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
190f68c5 7125 struct intel_crtc_state *crtc_state,
9e2c8475 7126 struct dpll *reduced_clock)
a7516a05 7127{
f47709a9 7128 struct drm_device *dev = crtc->base.dev;
a7516a05
JB
7129 u32 fp, fp2 = 0;
7130
7131 if (IS_PINEVIEW(dev)) {
190f68c5 7132 fp = pnv_dpll_compute_fp(&crtc_state->dpll);
a7516a05 7133 if (reduced_clock)
7429e9d4 7134 fp2 = pnv_dpll_compute_fp(reduced_clock);
a7516a05 7135 } else {
190f68c5 7136 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
a7516a05 7137 if (reduced_clock)
7429e9d4 7138 fp2 = i9xx_dpll_compute_fp(reduced_clock);
a7516a05
JB
7139 }
7140
190f68c5 7141 crtc_state->dpll_hw_state.fp0 = fp;
a7516a05 7142
f47709a9 7143 crtc->lowfreq_avail = false;
2d84d2b3 7144 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
ab585dea 7145 reduced_clock) {
190f68c5 7146 crtc_state->dpll_hw_state.fp1 = fp2;
f47709a9 7147 crtc->lowfreq_avail = true;
a7516a05 7148 } else {
190f68c5 7149 crtc_state->dpll_hw_state.fp1 = fp;
a7516a05
JB
7150 }
7151}
7152
5e69f97f
CML
7153static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
7154 pipe)
89b667f8
JB
7155{
7156 u32 reg_val;
7157
7158 /*
7159 * PLLB opamp always calibrates to max value of 0x3f, force enable it
7160 * and set it to a reasonable value instead.
7161 */
ab3c759a 7162 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
89b667f8
JB
7163 reg_val &= 0xffffff00;
7164 reg_val |= 0x00000030;
ab3c759a 7165 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
89b667f8 7166
ab3c759a 7167 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
89b667f8
JB
7168 reg_val &= 0x8cffffff;
7169 reg_val = 0x8c000000;
ab3c759a 7170 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
89b667f8 7171
ab3c759a 7172 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
89b667f8 7173 reg_val &= 0xffffff00;
ab3c759a 7174 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
89b667f8 7175
ab3c759a 7176 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
89b667f8
JB
7177 reg_val &= 0x00ffffff;
7178 reg_val |= 0xb0000000;
ab3c759a 7179 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
89b667f8
JB
7180}
7181
b551842d
DV
7182static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
7183 struct intel_link_m_n *m_n)
7184{
7185 struct drm_device *dev = crtc->base.dev;
fac5e23e 7186 struct drm_i915_private *dev_priv = to_i915(dev);
b551842d
DV
7187 int pipe = crtc->pipe;
7188
e3b95f1e
DV
7189 I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7190 I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
7191 I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
7192 I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
b551842d
DV
7193}
7194
7195static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
f769cd24
VK
7196 struct intel_link_m_n *m_n,
7197 struct intel_link_m_n *m2_n2)
b551842d
DV
7198{
7199 struct drm_device *dev = crtc->base.dev;
fac5e23e 7200 struct drm_i915_private *dev_priv = to_i915(dev);
b551842d 7201 int pipe = crtc->pipe;
6e3c9717 7202 enum transcoder transcoder = crtc->config->cpu_transcoder;
b551842d
DV
7203
7204 if (INTEL_INFO(dev)->gen >= 5) {
7205 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
7206 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
7207 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
7208 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
f769cd24
VK
7209 /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
7210 * for gen < 8) and if DRRS is supported (to make sure the
7211 * registers are not unnecessarily accessed).
7212 */
44395bfe 7213 if (m2_n2 && (IS_CHERRYVIEW(dev) || INTEL_INFO(dev)->gen < 8) &&
6e3c9717 7214 crtc->config->has_drrs) {
f769cd24
VK
7215 I915_WRITE(PIPE_DATA_M2(transcoder),
7216 TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
7217 I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
7218 I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
7219 I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
7220 }
b551842d 7221 } else {
e3b95f1e
DV
7222 I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7223 I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
7224 I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
7225 I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
b551842d
DV
7226 }
7227}
7228
fe3cd48d 7229void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
03afc4a2 7230{
fe3cd48d
R
7231 struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
7232
7233 if (m_n == M1_N1) {
7234 dp_m_n = &crtc->config->dp_m_n;
7235 dp_m2_n2 = &crtc->config->dp_m2_n2;
7236 } else if (m_n == M2_N2) {
7237
7238 /*
7239 * M2_N2 registers are not supported. Hence m2_n2 divider value
7240 * needs to be programmed into M1_N1.
7241 */
7242 dp_m_n = &crtc->config->dp_m2_n2;
7243 } else {
7244 DRM_ERROR("Unsupported divider value\n");
7245 return;
7246 }
7247
6e3c9717
ACO
7248 if (crtc->config->has_pch_encoder)
7249 intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
03afc4a2 7250 else
fe3cd48d 7251 intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
03afc4a2
DV
7252}
7253
251ac862
DV
7254static void vlv_compute_dpll(struct intel_crtc *crtc,
7255 struct intel_crtc_state *pipe_config)
bdd4b6a6 7256{
03ed5cbf 7257 pipe_config->dpll_hw_state.dpll = DPLL_INTEGRATED_REF_CLK_VLV |
cd2d34d9 7258 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
03ed5cbf
VS
7259 if (crtc->pipe != PIPE_A)
7260 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
bdd4b6a6 7261
cd2d34d9 7262 /* DPLL not used with DSI, but still need the rest set up */
d7edc4e5 7263 if (!intel_crtc_has_type(pipe_config, INTEL_OUTPUT_DSI))
cd2d34d9
VS
7264 pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE |
7265 DPLL_EXT_BUFFER_ENABLE_VLV;
7266
03ed5cbf
VS
7267 pipe_config->dpll_hw_state.dpll_md =
7268 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
7269}
bdd4b6a6 7270
03ed5cbf
VS
7271static void chv_compute_dpll(struct intel_crtc *crtc,
7272 struct intel_crtc_state *pipe_config)
7273{
7274 pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLK_CHV |
cd2d34d9 7275 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
03ed5cbf
VS
7276 if (crtc->pipe != PIPE_A)
7277 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
7278
cd2d34d9 7279 /* DPLL not used with DSI, but still need the rest set up */
d7edc4e5 7280 if (!intel_crtc_has_type(pipe_config, INTEL_OUTPUT_DSI))
cd2d34d9
VS
7281 pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE;
7282
03ed5cbf
VS
7283 pipe_config->dpll_hw_state.dpll_md =
7284 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
bdd4b6a6
DV
7285}
7286
d288f65f 7287static void vlv_prepare_pll(struct intel_crtc *crtc,
5cec258b 7288 const struct intel_crtc_state *pipe_config)
a0c4da24 7289{
f47709a9 7290 struct drm_device *dev = crtc->base.dev;
fac5e23e 7291 struct drm_i915_private *dev_priv = to_i915(dev);
cd2d34d9 7292 enum pipe pipe = crtc->pipe;
bdd4b6a6 7293 u32 mdiv;
a0c4da24 7294 u32 bestn, bestm1, bestm2, bestp1, bestp2;
bdd4b6a6 7295 u32 coreclk, reg_val;
a0c4da24 7296
cd2d34d9
VS
7297 /* Enable Refclk */
7298 I915_WRITE(DPLL(pipe),
7299 pipe_config->dpll_hw_state.dpll &
7300 ~(DPLL_VCO_ENABLE | DPLL_EXT_BUFFER_ENABLE_VLV));
7301
7302 /* No need to actually set up the DPLL with DSI */
7303 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
7304 return;
7305
a580516d 7306 mutex_lock(&dev_priv->sb_lock);
09153000 7307
d288f65f
VS
7308 bestn = pipe_config->dpll.n;
7309 bestm1 = pipe_config->dpll.m1;
7310 bestm2 = pipe_config->dpll.m2;
7311 bestp1 = pipe_config->dpll.p1;
7312 bestp2 = pipe_config->dpll.p2;
a0c4da24 7313
89b667f8
JB
7314 /* See eDP HDMI DPIO driver vbios notes doc */
7315
7316 /* PLL B needs special handling */
bdd4b6a6 7317 if (pipe == PIPE_B)
5e69f97f 7318 vlv_pllb_recal_opamp(dev_priv, pipe);
89b667f8
JB
7319
7320 /* Set up Tx target for periodic Rcomp update */
ab3c759a 7321 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
89b667f8
JB
7322
7323 /* Disable target IRef on PLL */
ab3c759a 7324 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
89b667f8 7325 reg_val &= 0x00ffffff;
ab3c759a 7326 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
89b667f8
JB
7327
7328 /* Disable fast lock */
ab3c759a 7329 vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
89b667f8
JB
7330
7331 /* Set idtafcrecal before PLL is enabled */
a0c4da24
JB
7332 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
7333 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
7334 mdiv |= ((bestn << DPIO_N_SHIFT));
a0c4da24 7335 mdiv |= (1 << DPIO_K_SHIFT);
7df5080b
JB
7336
7337 /*
7338 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
7339 * but we don't support that).
7340 * Note: don't use the DAC post divider as it seems unstable.
7341 */
7342 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
ab3c759a 7343 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
a0c4da24 7344
a0c4da24 7345 mdiv |= DPIO_ENABLE_CALIBRATION;
ab3c759a 7346 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
a0c4da24 7347
89b667f8 7348 /* Set HBR and RBR LPF coefficients */
d288f65f 7349 if (pipe_config->port_clock == 162000 ||
2d84d2b3
VS
7350 intel_crtc_has_type(crtc->config, INTEL_OUTPUT_ANALOG) ||
7351 intel_crtc_has_type(crtc->config, INTEL_OUTPUT_HDMI))
ab3c759a 7352 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
885b0120 7353 0x009f0003);
89b667f8 7354 else
ab3c759a 7355 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
89b667f8
JB
7356 0x00d0000f);
7357
37a5650b 7358 if (intel_crtc_has_dp_encoder(pipe_config)) {
89b667f8 7359 /* Use SSC source */
bdd4b6a6 7360 if (pipe == PIPE_A)
ab3c759a 7361 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
7362 0x0df40000);
7363 else
ab3c759a 7364 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
7365 0x0df70000);
7366 } else { /* HDMI or VGA */
7367 /* Use bend source */
bdd4b6a6 7368 if (pipe == PIPE_A)
ab3c759a 7369 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
7370 0x0df70000);
7371 else
ab3c759a 7372 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
7373 0x0df40000);
7374 }
a0c4da24 7375
ab3c759a 7376 coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
89b667f8 7377 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
2210ce7f 7378 if (intel_crtc_has_dp_encoder(crtc->config))
89b667f8 7379 coreclk |= 0x01000000;
ab3c759a 7380 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
a0c4da24 7381
ab3c759a 7382 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
a580516d 7383 mutex_unlock(&dev_priv->sb_lock);
a0c4da24
JB
7384}
7385
d288f65f 7386static void chv_prepare_pll(struct intel_crtc *crtc,
5cec258b 7387 const struct intel_crtc_state *pipe_config)
9d556c99
CML
7388{
7389 struct drm_device *dev = crtc->base.dev;
fac5e23e 7390 struct drm_i915_private *dev_priv = to_i915(dev);
cd2d34d9 7391 enum pipe pipe = crtc->pipe;
9d556c99 7392 enum dpio_channel port = vlv_pipe_to_channel(pipe);
9cbe40c1 7393 u32 loopfilter, tribuf_calcntr;
9d556c99 7394 u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
a945ce7e 7395 u32 dpio_val;
9cbe40c1 7396 int vco;
9d556c99 7397
cd2d34d9
VS
7398 /* Enable Refclk and SSC */
7399 I915_WRITE(DPLL(pipe),
7400 pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
7401
7402 /* No need to actually set up the DPLL with DSI */
7403 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
7404 return;
7405
d288f65f
VS
7406 bestn = pipe_config->dpll.n;
7407 bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
7408 bestm1 = pipe_config->dpll.m1;
7409 bestm2 = pipe_config->dpll.m2 >> 22;
7410 bestp1 = pipe_config->dpll.p1;
7411 bestp2 = pipe_config->dpll.p2;
9cbe40c1 7412 vco = pipe_config->dpll.vco;
a945ce7e 7413 dpio_val = 0;
9cbe40c1 7414 loopfilter = 0;
9d556c99 7415
a580516d 7416 mutex_lock(&dev_priv->sb_lock);
9d556c99 7417
9d556c99
CML
7418 /* p1 and p2 divider */
7419 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
7420 5 << DPIO_CHV_S1_DIV_SHIFT |
7421 bestp1 << DPIO_CHV_P1_DIV_SHIFT |
7422 bestp2 << DPIO_CHV_P2_DIV_SHIFT |
7423 1 << DPIO_CHV_K_DIV_SHIFT);
7424
7425 /* Feedback post-divider - m2 */
7426 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
7427
7428 /* Feedback refclk divider - n and m1 */
7429 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
7430 DPIO_CHV_M1_DIV_BY_2 |
7431 1 << DPIO_CHV_N_DIV_SHIFT);
7432
7433 /* M2 fraction division */
25a25dfc 7434 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
9d556c99
CML
7435
7436 /* M2 fraction division enable */
a945ce7e
VP
7437 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
7438 dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
7439 dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
7440 if (bestm2_frac)
7441 dpio_val |= DPIO_CHV_FRAC_DIV_EN;
7442 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
9d556c99 7443
de3a0fde
VP
7444 /* Program digital lock detect threshold */
7445 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
7446 dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
7447 DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
7448 dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
7449 if (!bestm2_frac)
7450 dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
7451 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
7452
9d556c99 7453 /* Loop filter */
9cbe40c1
VP
7454 if (vco == 5400000) {
7455 loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
7456 loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
7457 loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
7458 tribuf_calcntr = 0x9;
7459 } else if (vco <= 6200000) {
7460 loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
7461 loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
7462 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7463 tribuf_calcntr = 0x9;
7464 } else if (vco <= 6480000) {
7465 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7466 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7467 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7468 tribuf_calcntr = 0x8;
7469 } else {
7470 /* Not supported. Apply the same limits as in the max case */
7471 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7472 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7473 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7474 tribuf_calcntr = 0;
7475 }
9d556c99
CML
7476 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
7477
968040b2 7478 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
9cbe40c1
VP
7479 dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
7480 dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
7481 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
7482
9d556c99
CML
7483 /* AFC Recal */
7484 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
7485 vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
7486 DPIO_AFC_RECAL);
7487
a580516d 7488 mutex_unlock(&dev_priv->sb_lock);
9d556c99
CML
7489}
7490
d288f65f
VS
7491/**
7492 * vlv_force_pll_on - forcibly enable just the PLL
7493 * @dev_priv: i915 private structure
7494 * @pipe: pipe PLL to enable
7495 * @dpll: PLL configuration
7496 *
7497 * Enable the PLL for @pipe using the supplied @dpll config. To be used
7498 * in cases where we need the PLL enabled even when @pipe is not going to
7499 * be enabled.
7500 */
3f36b937
TU
7501int vlv_force_pll_on(struct drm_device *dev, enum pipe pipe,
7502 const struct dpll *dpll)
d288f65f
VS
7503{
7504 struct intel_crtc *crtc =
7505 to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
3f36b937
TU
7506 struct intel_crtc_state *pipe_config;
7507
7508 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
7509 if (!pipe_config)
7510 return -ENOMEM;
7511
7512 pipe_config->base.crtc = &crtc->base;
7513 pipe_config->pixel_multiplier = 1;
7514 pipe_config->dpll = *dpll;
d288f65f
VS
7515
7516 if (IS_CHERRYVIEW(dev)) {
3f36b937
TU
7517 chv_compute_dpll(crtc, pipe_config);
7518 chv_prepare_pll(crtc, pipe_config);
7519 chv_enable_pll(crtc, pipe_config);
d288f65f 7520 } else {
3f36b937
TU
7521 vlv_compute_dpll(crtc, pipe_config);
7522 vlv_prepare_pll(crtc, pipe_config);
7523 vlv_enable_pll(crtc, pipe_config);
d288f65f 7524 }
3f36b937
TU
7525
7526 kfree(pipe_config);
7527
7528 return 0;
d288f65f
VS
7529}
7530
7531/**
7532 * vlv_force_pll_off - forcibly disable just the PLL
7533 * @dev_priv: i915 private structure
7534 * @pipe: pipe PLL to disable
7535 *
7536 * Disable the PLL for @pipe. To be used in cases where we need
7537 * the PLL enabled even when @pipe is not going to be enabled.
7538 */
7539void vlv_force_pll_off(struct drm_device *dev, enum pipe pipe)
7540{
7541 if (IS_CHERRYVIEW(dev))
7542 chv_disable_pll(to_i915(dev), pipe);
7543 else
7544 vlv_disable_pll(to_i915(dev), pipe);
7545}
7546
251ac862
DV
7547static void i9xx_compute_dpll(struct intel_crtc *crtc,
7548 struct intel_crtc_state *crtc_state,
9e2c8475 7549 struct dpll *reduced_clock)
eb1cbe48 7550{
f47709a9 7551 struct drm_device *dev = crtc->base.dev;
fac5e23e 7552 struct drm_i915_private *dev_priv = to_i915(dev);
eb1cbe48 7553 u32 dpll;
190f68c5 7554 struct dpll *clock = &crtc_state->dpll;
eb1cbe48 7555
190f68c5 7556 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
2a8f64ca 7557
eb1cbe48
DV
7558 dpll = DPLL_VGA_MODE_DIS;
7559
2d84d2b3 7560 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS))
eb1cbe48
DV
7561 dpll |= DPLLB_MODE_LVDS;
7562 else
7563 dpll |= DPLLB_MODE_DAC_SERIAL;
6cc5f341 7564
ef1b460d 7565 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
190f68c5 7566 dpll |= (crtc_state->pixel_multiplier - 1)
198a037f 7567 << SDVO_MULTIPLIER_SHIFT_HIRES;
eb1cbe48 7568 }
198a037f 7569
3d6e9ee0
VS
7570 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO) ||
7571 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
4a33e48d 7572 dpll |= DPLL_SDVO_HIGH_SPEED;
198a037f 7573
37a5650b 7574 if (intel_crtc_has_dp_encoder(crtc_state))
4a33e48d 7575 dpll |= DPLL_SDVO_HIGH_SPEED;
eb1cbe48
DV
7576
7577 /* compute bitmask from p1 value */
7578 if (IS_PINEVIEW(dev))
7579 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
7580 else {
7581 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7582 if (IS_G4X(dev) && reduced_clock)
7583 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
7584 }
7585 switch (clock->p2) {
7586 case 5:
7587 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
7588 break;
7589 case 7:
7590 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
7591 break;
7592 case 10:
7593 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
7594 break;
7595 case 14:
7596 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
7597 break;
7598 }
7599 if (INTEL_INFO(dev)->gen >= 4)
7600 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
7601
190f68c5 7602 if (crtc_state->sdvo_tv_clock)
eb1cbe48 7603 dpll |= PLL_REF_INPUT_TVCLKINBC;
2d84d2b3 7604 else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
ceb41007 7605 intel_panel_use_ssc(dev_priv))
eb1cbe48
DV
7606 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7607 else
7608 dpll |= PLL_REF_INPUT_DREFCLK;
7609
7610 dpll |= DPLL_VCO_ENABLE;
190f68c5 7611 crtc_state->dpll_hw_state.dpll = dpll;
8bcc2795 7612
eb1cbe48 7613 if (INTEL_INFO(dev)->gen >= 4) {
190f68c5 7614 u32 dpll_md = (crtc_state->pixel_multiplier - 1)
ef1b460d 7615 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
190f68c5 7616 crtc_state->dpll_hw_state.dpll_md = dpll_md;
eb1cbe48
DV
7617 }
7618}
7619
251ac862
DV
7620static void i8xx_compute_dpll(struct intel_crtc *crtc,
7621 struct intel_crtc_state *crtc_state,
9e2c8475 7622 struct dpll *reduced_clock)
eb1cbe48 7623{
f47709a9 7624 struct drm_device *dev = crtc->base.dev;
fac5e23e 7625 struct drm_i915_private *dev_priv = to_i915(dev);
eb1cbe48 7626 u32 dpll;
190f68c5 7627 struct dpll *clock = &crtc_state->dpll;
eb1cbe48 7628
190f68c5 7629 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
2a8f64ca 7630
eb1cbe48
DV
7631 dpll = DPLL_VGA_MODE_DIS;
7632
2d84d2b3 7633 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
eb1cbe48
DV
7634 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7635 } else {
7636 if (clock->p1 == 2)
7637 dpll |= PLL_P1_DIVIDE_BY_TWO;
7638 else
7639 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7640 if (clock->p2 == 4)
7641 dpll |= PLL_P2_DIVIDE_BY_4;
7642 }
7643
2d84d2b3 7644 if (!IS_I830(dev) && intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DVO))
4a33e48d
DV
7645 dpll |= DPLL_DVO_2X_MODE;
7646
2d84d2b3 7647 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
ceb41007 7648 intel_panel_use_ssc(dev_priv))
eb1cbe48
DV
7649 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7650 else
7651 dpll |= PLL_REF_INPUT_DREFCLK;
7652
7653 dpll |= DPLL_VCO_ENABLE;
190f68c5 7654 crtc_state->dpll_hw_state.dpll = dpll;
eb1cbe48
DV
7655}
7656
8a654f3b 7657static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
b0e77b9c
PZ
7658{
7659 struct drm_device *dev = intel_crtc->base.dev;
fac5e23e 7660 struct drm_i915_private *dev_priv = to_i915(dev);
b0e77b9c 7661 enum pipe pipe = intel_crtc->pipe;
6e3c9717 7662 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
7c5f93b0 7663 const struct drm_display_mode *adjusted_mode = &intel_crtc->config->base.adjusted_mode;
1caea6e9
VS
7664 uint32_t crtc_vtotal, crtc_vblank_end;
7665 int vsyncshift = 0;
4d8a62ea
DV
7666
7667 /* We need to be careful not to changed the adjusted mode, for otherwise
7668 * the hw state checker will get angry at the mismatch. */
7669 crtc_vtotal = adjusted_mode->crtc_vtotal;
7670 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
b0e77b9c 7671
609aeaca 7672 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
b0e77b9c 7673 /* the chip adds 2 halflines automatically */
4d8a62ea
DV
7674 crtc_vtotal -= 1;
7675 crtc_vblank_end -= 1;
609aeaca 7676
2d84d2b3 7677 if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
609aeaca
VS
7678 vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
7679 else
7680 vsyncshift = adjusted_mode->crtc_hsync_start -
7681 adjusted_mode->crtc_htotal / 2;
1caea6e9
VS
7682 if (vsyncshift < 0)
7683 vsyncshift += adjusted_mode->crtc_htotal;
b0e77b9c
PZ
7684 }
7685
7686 if (INTEL_INFO(dev)->gen > 3)
fe2b8f9d 7687 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
b0e77b9c 7688
fe2b8f9d 7689 I915_WRITE(HTOTAL(cpu_transcoder),
b0e77b9c
PZ
7690 (adjusted_mode->crtc_hdisplay - 1) |
7691 ((adjusted_mode->crtc_htotal - 1) << 16));
fe2b8f9d 7692 I915_WRITE(HBLANK(cpu_transcoder),
b0e77b9c
PZ
7693 (adjusted_mode->crtc_hblank_start - 1) |
7694 ((adjusted_mode->crtc_hblank_end - 1) << 16));
fe2b8f9d 7695 I915_WRITE(HSYNC(cpu_transcoder),
b0e77b9c
PZ
7696 (adjusted_mode->crtc_hsync_start - 1) |
7697 ((adjusted_mode->crtc_hsync_end - 1) << 16));
7698
fe2b8f9d 7699 I915_WRITE(VTOTAL(cpu_transcoder),
b0e77b9c 7700 (adjusted_mode->crtc_vdisplay - 1) |
4d8a62ea 7701 ((crtc_vtotal - 1) << 16));
fe2b8f9d 7702 I915_WRITE(VBLANK(cpu_transcoder),
b0e77b9c 7703 (adjusted_mode->crtc_vblank_start - 1) |
4d8a62ea 7704 ((crtc_vblank_end - 1) << 16));
fe2b8f9d 7705 I915_WRITE(VSYNC(cpu_transcoder),
b0e77b9c
PZ
7706 (adjusted_mode->crtc_vsync_start - 1) |
7707 ((adjusted_mode->crtc_vsync_end - 1) << 16));
7708
b5e508d4
PZ
7709 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
7710 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
7711 * documented on the DDI_FUNC_CTL register description, EDP Input Select
7712 * bits. */
7713 if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
7714 (pipe == PIPE_B || pipe == PIPE_C))
7715 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
7716
bc58be60
JN
7717}
7718
7719static void intel_set_pipe_src_size(struct intel_crtc *intel_crtc)
7720{
7721 struct drm_device *dev = intel_crtc->base.dev;
fac5e23e 7722 struct drm_i915_private *dev_priv = to_i915(dev);
bc58be60
JN
7723 enum pipe pipe = intel_crtc->pipe;
7724
b0e77b9c
PZ
7725 /* pipesrc controls the size that is scaled from, which should
7726 * always be the user's requested size.
7727 */
7728 I915_WRITE(PIPESRC(pipe),
6e3c9717
ACO
7729 ((intel_crtc->config->pipe_src_w - 1) << 16) |
7730 (intel_crtc->config->pipe_src_h - 1));
b0e77b9c
PZ
7731}
7732
1bd1bd80 7733static void intel_get_pipe_timings(struct intel_crtc *crtc,
5cec258b 7734 struct intel_crtc_state *pipe_config)
1bd1bd80
DV
7735{
7736 struct drm_device *dev = crtc->base.dev;
fac5e23e 7737 struct drm_i915_private *dev_priv = to_i915(dev);
1bd1bd80
DV
7738 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
7739 uint32_t tmp;
7740
7741 tmp = I915_READ(HTOTAL(cpu_transcoder));
2d112de7
ACO
7742 pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
7743 pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 7744 tmp = I915_READ(HBLANK(cpu_transcoder));
2d112de7
ACO
7745 pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
7746 pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 7747 tmp = I915_READ(HSYNC(cpu_transcoder));
2d112de7
ACO
7748 pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
7749 pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80
DV
7750
7751 tmp = I915_READ(VTOTAL(cpu_transcoder));
2d112de7
ACO
7752 pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
7753 pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 7754 tmp = I915_READ(VBLANK(cpu_transcoder));
2d112de7
ACO
7755 pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
7756 pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 7757 tmp = I915_READ(VSYNC(cpu_transcoder));
2d112de7
ACO
7758 pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
7759 pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80
DV
7760
7761 if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
2d112de7
ACO
7762 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
7763 pipe_config->base.adjusted_mode.crtc_vtotal += 1;
7764 pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
1bd1bd80 7765 }
bc58be60
JN
7766}
7767
7768static void intel_get_pipe_src_size(struct intel_crtc *crtc,
7769 struct intel_crtc_state *pipe_config)
7770{
7771 struct drm_device *dev = crtc->base.dev;
fac5e23e 7772 struct drm_i915_private *dev_priv = to_i915(dev);
bc58be60 7773 u32 tmp;
1bd1bd80
DV
7774
7775 tmp = I915_READ(PIPESRC(crtc->pipe));
37327abd
VS
7776 pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
7777 pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
7778
2d112de7
ACO
7779 pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
7780 pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
1bd1bd80
DV
7781}
7782
f6a83288 7783void intel_mode_from_pipe_config(struct drm_display_mode *mode,
5cec258b 7784 struct intel_crtc_state *pipe_config)
babea61d 7785{
2d112de7
ACO
7786 mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
7787 mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
7788 mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
7789 mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
babea61d 7790
2d112de7
ACO
7791 mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
7792 mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
7793 mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
7794 mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
babea61d 7795
2d112de7 7796 mode->flags = pipe_config->base.adjusted_mode.flags;
cd13f5ab 7797 mode->type = DRM_MODE_TYPE_DRIVER;
babea61d 7798
2d112de7
ACO
7799 mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
7800 mode->flags |= pipe_config->base.adjusted_mode.flags;
cd13f5ab
ML
7801
7802 mode->hsync = drm_mode_hsync(mode);
7803 mode->vrefresh = drm_mode_vrefresh(mode);
7804 drm_mode_set_name(mode);
babea61d
JB
7805}
7806
84b046f3
DV
7807static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
7808{
7809 struct drm_device *dev = intel_crtc->base.dev;
fac5e23e 7810 struct drm_i915_private *dev_priv = to_i915(dev);
84b046f3
DV
7811 uint32_t pipeconf;
7812
9f11a9e4 7813 pipeconf = 0;
84b046f3 7814
b6b5d049
VS
7815 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
7816 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
7817 pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
67c72a12 7818
6e3c9717 7819 if (intel_crtc->config->double_wide)
cf532bb2 7820 pipeconf |= PIPECONF_DOUBLE_WIDE;
84b046f3 7821
ff9ce46e 7822 /* only g4x and later have fancy bpc/dither controls */
666a4537 7823 if (IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
ff9ce46e 7824 /* Bspec claims that we can't use dithering for 30bpp pipes. */
6e3c9717 7825 if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
ff9ce46e 7826 pipeconf |= PIPECONF_DITHER_EN |
84b046f3 7827 PIPECONF_DITHER_TYPE_SP;
84b046f3 7828
6e3c9717 7829 switch (intel_crtc->config->pipe_bpp) {
ff9ce46e
DV
7830 case 18:
7831 pipeconf |= PIPECONF_6BPC;
7832 break;
7833 case 24:
7834 pipeconf |= PIPECONF_8BPC;
7835 break;
7836 case 30:
7837 pipeconf |= PIPECONF_10BPC;
7838 break;
7839 default:
7840 /* Case prevented by intel_choose_pipe_bpp_dither. */
7841 BUG();
84b046f3
DV
7842 }
7843 }
7844
7845 if (HAS_PIPE_CXSR(dev)) {
7846 if (intel_crtc->lowfreq_avail) {
7847 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
7848 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
7849 } else {
7850 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
84b046f3
DV
7851 }
7852 }
7853
6e3c9717 7854 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
efc2cfff 7855 if (INTEL_INFO(dev)->gen < 4 ||
2d84d2b3 7856 intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
efc2cfff
VS
7857 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
7858 else
7859 pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
7860 } else
84b046f3
DV
7861 pipeconf |= PIPECONF_PROGRESSIVE;
7862
666a4537
WB
7863 if ((IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) &&
7864 intel_crtc->config->limited_color_range)
9f11a9e4 7865 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
9c8e09b7 7866
84b046f3
DV
7867 I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
7868 POSTING_READ(PIPECONF(intel_crtc->pipe));
7869}
7870
81c97f52
ACO
7871static int i8xx_crtc_compute_clock(struct intel_crtc *crtc,
7872 struct intel_crtc_state *crtc_state)
7873{
7874 struct drm_device *dev = crtc->base.dev;
fac5e23e 7875 struct drm_i915_private *dev_priv = to_i915(dev);
1b6f4958 7876 const struct intel_limit *limit;
81c97f52
ACO
7877 int refclk = 48000;
7878
7879 memset(&crtc_state->dpll_hw_state, 0,
7880 sizeof(crtc_state->dpll_hw_state));
7881
2d84d2b3 7882 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
81c97f52
ACO
7883 if (intel_panel_use_ssc(dev_priv)) {
7884 refclk = dev_priv->vbt.lvds_ssc_freq;
7885 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
7886 }
7887
7888 limit = &intel_limits_i8xx_lvds;
2d84d2b3 7889 } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DVO)) {
81c97f52
ACO
7890 limit = &intel_limits_i8xx_dvo;
7891 } else {
7892 limit = &intel_limits_i8xx_dac;
7893 }
7894
7895 if (!crtc_state->clock_set &&
7896 !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7897 refclk, NULL, &crtc_state->dpll)) {
7898 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7899 return -EINVAL;
7900 }
7901
7902 i8xx_compute_dpll(crtc, crtc_state, NULL);
7903
7904 return 0;
7905}
7906
19ec6693
ACO
7907static int g4x_crtc_compute_clock(struct intel_crtc *crtc,
7908 struct intel_crtc_state *crtc_state)
7909{
7910 struct drm_device *dev = crtc->base.dev;
fac5e23e 7911 struct drm_i915_private *dev_priv = to_i915(dev);
1b6f4958 7912 const struct intel_limit *limit;
19ec6693
ACO
7913 int refclk = 96000;
7914
7915 memset(&crtc_state->dpll_hw_state, 0,
7916 sizeof(crtc_state->dpll_hw_state));
7917
2d84d2b3 7918 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
19ec6693
ACO
7919 if (intel_panel_use_ssc(dev_priv)) {
7920 refclk = dev_priv->vbt.lvds_ssc_freq;
7921 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
7922 }
7923
7924 if (intel_is_dual_link_lvds(dev))
7925 limit = &intel_limits_g4x_dual_channel_lvds;
7926 else
7927 limit = &intel_limits_g4x_single_channel_lvds;
2d84d2b3
VS
7928 } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI) ||
7929 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
19ec6693 7930 limit = &intel_limits_g4x_hdmi;
2d84d2b3 7931 } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO)) {
19ec6693
ACO
7932 limit = &intel_limits_g4x_sdvo;
7933 } else {
7934 /* The option is for other outputs */
7935 limit = &intel_limits_i9xx_sdvo;
7936 }
7937
7938 if (!crtc_state->clock_set &&
7939 !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7940 refclk, NULL, &crtc_state->dpll)) {
7941 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7942 return -EINVAL;
7943 }
7944
7945 i9xx_compute_dpll(crtc, crtc_state, NULL);
7946
7947 return 0;
7948}
7949
70e8aa21
ACO
7950static int pnv_crtc_compute_clock(struct intel_crtc *crtc,
7951 struct intel_crtc_state *crtc_state)
7952{
7953 struct drm_device *dev = crtc->base.dev;
fac5e23e 7954 struct drm_i915_private *dev_priv = to_i915(dev);
1b6f4958 7955 const struct intel_limit *limit;
70e8aa21
ACO
7956 int refclk = 96000;
7957
7958 memset(&crtc_state->dpll_hw_state, 0,
7959 sizeof(crtc_state->dpll_hw_state));
7960
2d84d2b3 7961 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
70e8aa21
ACO
7962 if (intel_panel_use_ssc(dev_priv)) {
7963 refclk = dev_priv->vbt.lvds_ssc_freq;
7964 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
7965 }
7966
7967 limit = &intel_limits_pineview_lvds;
7968 } else {
7969 limit = &intel_limits_pineview_sdvo;
7970 }
7971
7972 if (!crtc_state->clock_set &&
7973 !pnv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7974 refclk, NULL, &crtc_state->dpll)) {
7975 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7976 return -EINVAL;
7977 }
7978
7979 i9xx_compute_dpll(crtc, crtc_state, NULL);
7980
7981 return 0;
7982}
7983
190f68c5
ACO
7984static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
7985 struct intel_crtc_state *crtc_state)
79e53945 7986{
c7653199 7987 struct drm_device *dev = crtc->base.dev;
fac5e23e 7988 struct drm_i915_private *dev_priv = to_i915(dev);
1b6f4958 7989 const struct intel_limit *limit;
81c97f52 7990 int refclk = 96000;
79e53945 7991
dd3cd74a
ACO
7992 memset(&crtc_state->dpll_hw_state, 0,
7993 sizeof(crtc_state->dpll_hw_state));
7994
2d84d2b3 7995 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
70e8aa21
ACO
7996 if (intel_panel_use_ssc(dev_priv)) {
7997 refclk = dev_priv->vbt.lvds_ssc_freq;
7998 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
7999 }
43565a06 8000
70e8aa21
ACO
8001 limit = &intel_limits_i9xx_lvds;
8002 } else {
8003 limit = &intel_limits_i9xx_sdvo;
81c97f52 8004 }
79e53945 8005
70e8aa21
ACO
8006 if (!crtc_state->clock_set &&
8007 !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8008 refclk, NULL, &crtc_state->dpll)) {
8009 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8010 return -EINVAL;
f47709a9 8011 }
7026d4ac 8012
81c97f52 8013 i9xx_compute_dpll(crtc, crtc_state, NULL);
79e53945 8014
c8f7a0db 8015 return 0;
f564048e
EA
8016}
8017
65b3d6a9
ACO
8018static int chv_crtc_compute_clock(struct intel_crtc *crtc,
8019 struct intel_crtc_state *crtc_state)
8020{
8021 int refclk = 100000;
1b6f4958 8022 const struct intel_limit *limit = &intel_limits_chv;
65b3d6a9
ACO
8023
8024 memset(&crtc_state->dpll_hw_state, 0,
8025 sizeof(crtc_state->dpll_hw_state));
8026
65b3d6a9
ACO
8027 if (!crtc_state->clock_set &&
8028 !chv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8029 refclk, NULL, &crtc_state->dpll)) {
8030 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8031 return -EINVAL;
8032 }
8033
8034 chv_compute_dpll(crtc, crtc_state);
8035
8036 return 0;
8037}
8038
8039static int vlv_crtc_compute_clock(struct intel_crtc *crtc,
8040 struct intel_crtc_state *crtc_state)
8041{
8042 int refclk = 100000;
1b6f4958 8043 const struct intel_limit *limit = &intel_limits_vlv;
65b3d6a9
ACO
8044
8045 memset(&crtc_state->dpll_hw_state, 0,
8046 sizeof(crtc_state->dpll_hw_state));
8047
65b3d6a9
ACO
8048 if (!crtc_state->clock_set &&
8049 !vlv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8050 refclk, NULL, &crtc_state->dpll)) {
8051 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8052 return -EINVAL;
8053 }
8054
8055 vlv_compute_dpll(crtc, crtc_state);
8056
8057 return 0;
8058}
8059
2fa2fe9a 8060static void i9xx_get_pfit_config(struct intel_crtc *crtc,
5cec258b 8061 struct intel_crtc_state *pipe_config)
2fa2fe9a
DV
8062{
8063 struct drm_device *dev = crtc->base.dev;
fac5e23e 8064 struct drm_i915_private *dev_priv = to_i915(dev);
2fa2fe9a
DV
8065 uint32_t tmp;
8066
dc9e7dec
VS
8067 if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
8068 return;
8069
2fa2fe9a 8070 tmp = I915_READ(PFIT_CONTROL);
06922821
DV
8071 if (!(tmp & PFIT_ENABLE))
8072 return;
2fa2fe9a 8073
06922821 8074 /* Check whether the pfit is attached to our pipe. */
2fa2fe9a
DV
8075 if (INTEL_INFO(dev)->gen < 4) {
8076 if (crtc->pipe != PIPE_B)
8077 return;
2fa2fe9a
DV
8078 } else {
8079 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
8080 return;
8081 }
8082
06922821 8083 pipe_config->gmch_pfit.control = tmp;
2fa2fe9a 8084 pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
2fa2fe9a
DV
8085}
8086
acbec814 8087static void vlv_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 8088 struct intel_crtc_state *pipe_config)
acbec814
JB
8089{
8090 struct drm_device *dev = crtc->base.dev;
fac5e23e 8091 struct drm_i915_private *dev_priv = to_i915(dev);
acbec814 8092 int pipe = pipe_config->cpu_transcoder;
9e2c8475 8093 struct dpll clock;
acbec814 8094 u32 mdiv;
662c6ecb 8095 int refclk = 100000;
acbec814 8096
b521973b
VS
8097 /* In case of DSI, DPLL will not be used */
8098 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
f573de5a
SK
8099 return;
8100
a580516d 8101 mutex_lock(&dev_priv->sb_lock);
ab3c759a 8102 mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
a580516d 8103 mutex_unlock(&dev_priv->sb_lock);
acbec814
JB
8104
8105 clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
8106 clock.m2 = mdiv & DPIO_M2DIV_MASK;
8107 clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
8108 clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
8109 clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
8110
dccbea3b 8111 pipe_config->port_clock = vlv_calc_dpll_params(refclk, &clock);
acbec814
JB
8112}
8113
5724dbd1
DL
8114static void
8115i9xx_get_initial_plane_config(struct intel_crtc *crtc,
8116 struct intel_initial_plane_config *plane_config)
1ad292b5
JB
8117{
8118 struct drm_device *dev = crtc->base.dev;
fac5e23e 8119 struct drm_i915_private *dev_priv = to_i915(dev);
1ad292b5
JB
8120 u32 val, base, offset;
8121 int pipe = crtc->pipe, plane = crtc->plane;
8122 int fourcc, pixel_format;
6761dd31 8123 unsigned int aligned_height;
b113d5ee 8124 struct drm_framebuffer *fb;
1b842c89 8125 struct intel_framebuffer *intel_fb;
1ad292b5 8126
42a7b088
DL
8127 val = I915_READ(DSPCNTR(plane));
8128 if (!(val & DISPLAY_PLANE_ENABLE))
8129 return;
8130
d9806c9f 8131 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
1b842c89 8132 if (!intel_fb) {
1ad292b5
JB
8133 DRM_DEBUG_KMS("failed to alloc fb\n");
8134 return;
8135 }
8136
1b842c89
DL
8137 fb = &intel_fb->base;
8138
18c5247e
DV
8139 if (INTEL_INFO(dev)->gen >= 4) {
8140 if (val & DISPPLANE_TILED) {
49af449b 8141 plane_config->tiling = I915_TILING_X;
18c5247e
DV
8142 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
8143 }
8144 }
1ad292b5
JB
8145
8146 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
b35d63fa 8147 fourcc = i9xx_format_to_fourcc(pixel_format);
b113d5ee
DL
8148 fb->pixel_format = fourcc;
8149 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
1ad292b5
JB
8150
8151 if (INTEL_INFO(dev)->gen >= 4) {
49af449b 8152 if (plane_config->tiling)
1ad292b5
JB
8153 offset = I915_READ(DSPTILEOFF(plane));
8154 else
8155 offset = I915_READ(DSPLINOFF(plane));
8156 base = I915_READ(DSPSURF(plane)) & 0xfffff000;
8157 } else {
8158 base = I915_READ(DSPADDR(plane));
8159 }
8160 plane_config->base = base;
8161
8162 val = I915_READ(PIPESRC(pipe));
b113d5ee
DL
8163 fb->width = ((val >> 16) & 0xfff) + 1;
8164 fb->height = ((val >> 0) & 0xfff) + 1;
1ad292b5
JB
8165
8166 val = I915_READ(DSPSTRIDE(pipe));
b113d5ee 8167 fb->pitches[0] = val & 0xffffffc0;
1ad292b5 8168
b113d5ee 8169 aligned_height = intel_fb_align_height(dev, fb->height,
091df6cb
DV
8170 fb->pixel_format,
8171 fb->modifier[0]);
1ad292b5 8172
f37b5c2b 8173 plane_config->size = fb->pitches[0] * aligned_height;
1ad292b5 8174
2844a921
DL
8175 DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
8176 pipe_name(pipe), plane, fb->width, fb->height,
8177 fb->bits_per_pixel, base, fb->pitches[0],
8178 plane_config->size);
1ad292b5 8179
2d14030b 8180 plane_config->fb = intel_fb;
1ad292b5
JB
8181}
8182
70b23a98 8183static void chv_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 8184 struct intel_crtc_state *pipe_config)
70b23a98
VS
8185{
8186 struct drm_device *dev = crtc->base.dev;
fac5e23e 8187 struct drm_i915_private *dev_priv = to_i915(dev);
70b23a98
VS
8188 int pipe = pipe_config->cpu_transcoder;
8189 enum dpio_channel port = vlv_pipe_to_channel(pipe);
9e2c8475 8190 struct dpll clock;
0d7b6b11 8191 u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2, pll_dw3;
70b23a98
VS
8192 int refclk = 100000;
8193
b521973b
VS
8194 /* In case of DSI, DPLL will not be used */
8195 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
8196 return;
8197
a580516d 8198 mutex_lock(&dev_priv->sb_lock);
70b23a98
VS
8199 cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
8200 pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
8201 pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
8202 pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
0d7b6b11 8203 pll_dw3 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
a580516d 8204 mutex_unlock(&dev_priv->sb_lock);
70b23a98
VS
8205
8206 clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
0d7b6b11
ID
8207 clock.m2 = (pll_dw0 & 0xff) << 22;
8208 if (pll_dw3 & DPIO_CHV_FRAC_DIV_EN)
8209 clock.m2 |= pll_dw2 & 0x3fffff;
70b23a98
VS
8210 clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
8211 clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
8212 clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
8213
dccbea3b 8214 pipe_config->port_clock = chv_calc_dpll_params(refclk, &clock);
70b23a98
VS
8215}
8216
0e8ffe1b 8217static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
5cec258b 8218 struct intel_crtc_state *pipe_config)
0e8ffe1b
DV
8219{
8220 struct drm_device *dev = crtc->base.dev;
fac5e23e 8221 struct drm_i915_private *dev_priv = to_i915(dev);
1729050e 8222 enum intel_display_power_domain power_domain;
0e8ffe1b 8223 uint32_t tmp;
1729050e 8224 bool ret;
0e8ffe1b 8225
1729050e
ID
8226 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
8227 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
b5482bd0
ID
8228 return false;
8229
e143a21c 8230 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
8106ddbd 8231 pipe_config->shared_dpll = NULL;
eccb140b 8232
1729050e
ID
8233 ret = false;
8234
0e8ffe1b
DV
8235 tmp = I915_READ(PIPECONF(crtc->pipe));
8236 if (!(tmp & PIPECONF_ENABLE))
1729050e 8237 goto out;
0e8ffe1b 8238
666a4537 8239 if (IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
42571aef
VS
8240 switch (tmp & PIPECONF_BPC_MASK) {
8241 case PIPECONF_6BPC:
8242 pipe_config->pipe_bpp = 18;
8243 break;
8244 case PIPECONF_8BPC:
8245 pipe_config->pipe_bpp = 24;
8246 break;
8247 case PIPECONF_10BPC:
8248 pipe_config->pipe_bpp = 30;
8249 break;
8250 default:
8251 break;
8252 }
8253 }
8254
666a4537
WB
8255 if ((IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) &&
8256 (tmp & PIPECONF_COLOR_RANGE_SELECT))
b5a9fa09
DV
8257 pipe_config->limited_color_range = true;
8258
282740f7
VS
8259 if (INTEL_INFO(dev)->gen < 4)
8260 pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
8261
1bd1bd80 8262 intel_get_pipe_timings(crtc, pipe_config);
bc58be60 8263 intel_get_pipe_src_size(crtc, pipe_config);
1bd1bd80 8264
2fa2fe9a
DV
8265 i9xx_get_pfit_config(crtc, pipe_config);
8266
6c49f241 8267 if (INTEL_INFO(dev)->gen >= 4) {
c231775c
VS
8268 /* No way to read it out on pipes B and C */
8269 if (IS_CHERRYVIEW(dev) && crtc->pipe != PIPE_A)
8270 tmp = dev_priv->chv_dpll_md[crtc->pipe];
8271 else
8272 tmp = I915_READ(DPLL_MD(crtc->pipe));
6c49f241
DV
8273 pipe_config->pixel_multiplier =
8274 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
8275 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
8bcc2795 8276 pipe_config->dpll_hw_state.dpll_md = tmp;
6c49f241
DV
8277 } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
8278 tmp = I915_READ(DPLL(crtc->pipe));
8279 pipe_config->pixel_multiplier =
8280 ((tmp & SDVO_MULTIPLIER_MASK)
8281 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
8282 } else {
8283 /* Note that on i915G/GM the pixel multiplier is in the sdvo
8284 * port and will be fixed up in the encoder->get_config
8285 * function. */
8286 pipe_config->pixel_multiplier = 1;
8287 }
8bcc2795 8288 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
666a4537 8289 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
1c4e0274
VS
8290 /*
8291 * DPLL_DVO_2X_MODE must be enabled for both DPLLs
8292 * on 830. Filter it out here so that we don't
8293 * report errors due to that.
8294 */
8295 if (IS_I830(dev))
8296 pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
8297
8bcc2795
DV
8298 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
8299 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
165e901c
VS
8300 } else {
8301 /* Mask out read-only status bits. */
8302 pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
8303 DPLL_PORTC_READY_MASK |
8304 DPLL_PORTB_READY_MASK);
8bcc2795 8305 }
6c49f241 8306
70b23a98
VS
8307 if (IS_CHERRYVIEW(dev))
8308 chv_crtc_clock_get(crtc, pipe_config);
8309 else if (IS_VALLEYVIEW(dev))
acbec814
JB
8310 vlv_crtc_clock_get(crtc, pipe_config);
8311 else
8312 i9xx_crtc_clock_get(crtc, pipe_config);
18442d08 8313
0f64614d
VS
8314 /*
8315 * Normally the dotclock is filled in by the encoder .get_config()
8316 * but in case the pipe is enabled w/o any ports we need a sane
8317 * default.
8318 */
8319 pipe_config->base.adjusted_mode.crtc_clock =
8320 pipe_config->port_clock / pipe_config->pixel_multiplier;
8321
1729050e
ID
8322 ret = true;
8323
8324out:
8325 intel_display_power_put(dev_priv, power_domain);
8326
8327 return ret;
0e8ffe1b
DV
8328}
8329
dde86e2d 8330static void ironlake_init_pch_refclk(struct drm_device *dev)
13d83a67 8331{
fac5e23e 8332 struct drm_i915_private *dev_priv = to_i915(dev);
13d83a67 8333 struct intel_encoder *encoder;
1c1a24d2 8334 int i;
74cfd7ac 8335 u32 val, final;
13d83a67 8336 bool has_lvds = false;
199e5d79 8337 bool has_cpu_edp = false;
199e5d79 8338 bool has_panel = false;
99eb6a01
KP
8339 bool has_ck505 = false;
8340 bool can_ssc = false;
1c1a24d2 8341 bool using_ssc_source = false;
13d83a67
JB
8342
8343 /* We need to take the global config into account */
b2784e15 8344 for_each_intel_encoder(dev, encoder) {
199e5d79
KP
8345 switch (encoder->type) {
8346 case INTEL_OUTPUT_LVDS:
8347 has_panel = true;
8348 has_lvds = true;
8349 break;
8350 case INTEL_OUTPUT_EDP:
8351 has_panel = true;
2de6905f 8352 if (enc_to_dig_port(&encoder->base)->port == PORT_A)
199e5d79
KP
8353 has_cpu_edp = true;
8354 break;
6847d71b
PZ
8355 default:
8356 break;
13d83a67
JB
8357 }
8358 }
8359
99eb6a01 8360 if (HAS_PCH_IBX(dev)) {
41aa3448 8361 has_ck505 = dev_priv->vbt.display_clock_mode;
99eb6a01
KP
8362 can_ssc = has_ck505;
8363 } else {
8364 has_ck505 = false;
8365 can_ssc = true;
8366 }
8367
1c1a24d2
L
8368 /* Check if any DPLLs are using the SSC source */
8369 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
8370 u32 temp = I915_READ(PCH_DPLL(i));
8371
8372 if (!(temp & DPLL_VCO_ENABLE))
8373 continue;
8374
8375 if ((temp & PLL_REF_INPUT_MASK) ==
8376 PLLB_REF_INPUT_SPREADSPECTRUMIN) {
8377 using_ssc_source = true;
8378 break;
8379 }
8380 }
8381
8382 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d using_ssc_source %d\n",
8383 has_panel, has_lvds, has_ck505, using_ssc_source);
13d83a67
JB
8384
8385 /* Ironlake: try to setup display ref clock before DPLL
8386 * enabling. This is only under driver's control after
8387 * PCH B stepping, previous chipset stepping should be
8388 * ignoring this setting.
8389 */
74cfd7ac
CW
8390 val = I915_READ(PCH_DREF_CONTROL);
8391
8392 /* As we must carefully and slowly disable/enable each source in turn,
8393 * compute the final state we want first and check if we need to
8394 * make any changes at all.
8395 */
8396 final = val;
8397 final &= ~DREF_NONSPREAD_SOURCE_MASK;
8398 if (has_ck505)
8399 final |= DREF_NONSPREAD_CK505_ENABLE;
8400 else
8401 final |= DREF_NONSPREAD_SOURCE_ENABLE;
8402
8c07eb68 8403 final &= ~DREF_SSC_SOURCE_MASK;
74cfd7ac 8404 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
8c07eb68 8405 final &= ~DREF_SSC1_ENABLE;
74cfd7ac
CW
8406
8407 if (has_panel) {
8408 final |= DREF_SSC_SOURCE_ENABLE;
8409
8410 if (intel_panel_use_ssc(dev_priv) && can_ssc)
8411 final |= DREF_SSC1_ENABLE;
8412
8413 if (has_cpu_edp) {
8414 if (intel_panel_use_ssc(dev_priv) && can_ssc)
8415 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
8416 else
8417 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
8418 } else
8419 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
1c1a24d2
L
8420 } else if (using_ssc_source) {
8421 final |= DREF_SSC_SOURCE_ENABLE;
8422 final |= DREF_SSC1_ENABLE;
74cfd7ac
CW
8423 }
8424
8425 if (final == val)
8426 return;
8427
13d83a67 8428 /* Always enable nonspread source */
74cfd7ac 8429 val &= ~DREF_NONSPREAD_SOURCE_MASK;
13d83a67 8430
99eb6a01 8431 if (has_ck505)
74cfd7ac 8432 val |= DREF_NONSPREAD_CK505_ENABLE;
99eb6a01 8433 else
74cfd7ac 8434 val |= DREF_NONSPREAD_SOURCE_ENABLE;
13d83a67 8435
199e5d79 8436 if (has_panel) {
74cfd7ac
CW
8437 val &= ~DREF_SSC_SOURCE_MASK;
8438 val |= DREF_SSC_SOURCE_ENABLE;
13d83a67 8439
199e5d79 8440 /* SSC must be turned on before enabling the CPU output */
99eb6a01 8441 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
199e5d79 8442 DRM_DEBUG_KMS("Using SSC on panel\n");
74cfd7ac 8443 val |= DREF_SSC1_ENABLE;
e77166b5 8444 } else
74cfd7ac 8445 val &= ~DREF_SSC1_ENABLE;
199e5d79
KP
8446
8447 /* Get SSC going before enabling the outputs */
74cfd7ac 8448 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
8449 POSTING_READ(PCH_DREF_CONTROL);
8450 udelay(200);
8451
74cfd7ac 8452 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
13d83a67
JB
8453
8454 /* Enable CPU source on CPU attached eDP */
199e5d79 8455 if (has_cpu_edp) {
99eb6a01 8456 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
199e5d79 8457 DRM_DEBUG_KMS("Using SSC on eDP\n");
74cfd7ac 8458 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
eba905b2 8459 } else
74cfd7ac 8460 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
199e5d79 8461 } else
74cfd7ac 8462 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
199e5d79 8463
74cfd7ac 8464 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
8465 POSTING_READ(PCH_DREF_CONTROL);
8466 udelay(200);
8467 } else {
1c1a24d2 8468 DRM_DEBUG_KMS("Disabling CPU source output\n");
199e5d79 8469
74cfd7ac 8470 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
199e5d79
KP
8471
8472 /* Turn off CPU output */
74cfd7ac 8473 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
199e5d79 8474
74cfd7ac 8475 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
8476 POSTING_READ(PCH_DREF_CONTROL);
8477 udelay(200);
8478
1c1a24d2
L
8479 if (!using_ssc_source) {
8480 DRM_DEBUG_KMS("Disabling SSC source\n");
199e5d79 8481
1c1a24d2
L
8482 /* Turn off the SSC source */
8483 val &= ~DREF_SSC_SOURCE_MASK;
8484 val |= DREF_SSC_SOURCE_DISABLE;
f165d283 8485
1c1a24d2
L
8486 /* Turn off SSC1 */
8487 val &= ~DREF_SSC1_ENABLE;
8488
8489 I915_WRITE(PCH_DREF_CONTROL, val);
8490 POSTING_READ(PCH_DREF_CONTROL);
8491 udelay(200);
8492 }
13d83a67 8493 }
74cfd7ac
CW
8494
8495 BUG_ON(val != final);
13d83a67
JB
8496}
8497
f31f2d55 8498static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
dde86e2d 8499{
f31f2d55 8500 uint32_t tmp;
dde86e2d 8501
0ff066a9
PZ
8502 tmp = I915_READ(SOUTH_CHICKEN2);
8503 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
8504 I915_WRITE(SOUTH_CHICKEN2, tmp);
dde86e2d 8505
cf3598c2
ID
8506 if (wait_for_us(I915_READ(SOUTH_CHICKEN2) &
8507 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
0ff066a9 8508 DRM_ERROR("FDI mPHY reset assert timeout\n");
dde86e2d 8509
0ff066a9
PZ
8510 tmp = I915_READ(SOUTH_CHICKEN2);
8511 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
8512 I915_WRITE(SOUTH_CHICKEN2, tmp);
dde86e2d 8513
cf3598c2
ID
8514 if (wait_for_us((I915_READ(SOUTH_CHICKEN2) &
8515 FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
0ff066a9 8516 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
f31f2d55
PZ
8517}
8518
8519/* WaMPhyProgramming:hsw */
8520static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
8521{
8522 uint32_t tmp;
dde86e2d
PZ
8523
8524 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
8525 tmp &= ~(0xFF << 24);
8526 tmp |= (0x12 << 24);
8527 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
8528
dde86e2d
PZ
8529 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
8530 tmp |= (1 << 11);
8531 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
8532
8533 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
8534 tmp |= (1 << 11);
8535 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
8536
dde86e2d
PZ
8537 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
8538 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
8539 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
8540
8541 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
8542 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
8543 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
8544
0ff066a9
PZ
8545 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
8546 tmp &= ~(7 << 13);
8547 tmp |= (5 << 13);
8548 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
dde86e2d 8549
0ff066a9
PZ
8550 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
8551 tmp &= ~(7 << 13);
8552 tmp |= (5 << 13);
8553 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
dde86e2d
PZ
8554
8555 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
8556 tmp &= ~0xFF;
8557 tmp |= 0x1C;
8558 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
8559
8560 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
8561 tmp &= ~0xFF;
8562 tmp |= 0x1C;
8563 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
8564
8565 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
8566 tmp &= ~(0xFF << 16);
8567 tmp |= (0x1C << 16);
8568 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
8569
8570 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
8571 tmp &= ~(0xFF << 16);
8572 tmp |= (0x1C << 16);
8573 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
8574
0ff066a9
PZ
8575 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
8576 tmp |= (1 << 27);
8577 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
dde86e2d 8578
0ff066a9
PZ
8579 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
8580 tmp |= (1 << 27);
8581 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
dde86e2d 8582
0ff066a9
PZ
8583 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
8584 tmp &= ~(0xF << 28);
8585 tmp |= (4 << 28);
8586 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
dde86e2d 8587
0ff066a9
PZ
8588 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
8589 tmp &= ~(0xF << 28);
8590 tmp |= (4 << 28);
8591 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
f31f2d55
PZ
8592}
8593
2fa86a1f
PZ
8594/* Implements 3 different sequences from BSpec chapter "Display iCLK
8595 * Programming" based on the parameters passed:
8596 * - Sequence to enable CLKOUT_DP
8597 * - Sequence to enable CLKOUT_DP without spread
8598 * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
8599 */
8600static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
8601 bool with_fdi)
f31f2d55 8602{
fac5e23e 8603 struct drm_i915_private *dev_priv = to_i915(dev);
2fa86a1f
PZ
8604 uint32_t reg, tmp;
8605
8606 if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
8607 with_spread = true;
c2699524 8608 if (WARN(HAS_PCH_LPT_LP(dev) && with_fdi, "LP PCH doesn't have FDI\n"))
2fa86a1f 8609 with_fdi = false;
f31f2d55 8610
a580516d 8611 mutex_lock(&dev_priv->sb_lock);
f31f2d55
PZ
8612
8613 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8614 tmp &= ~SBI_SSCCTL_DISABLE;
8615 tmp |= SBI_SSCCTL_PATHALT;
8616 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8617
8618 udelay(24);
8619
2fa86a1f
PZ
8620 if (with_spread) {
8621 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8622 tmp &= ~SBI_SSCCTL_PATHALT;
8623 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
f31f2d55 8624
2fa86a1f
PZ
8625 if (with_fdi) {
8626 lpt_reset_fdi_mphy(dev_priv);
8627 lpt_program_fdi_mphy(dev_priv);
8628 }
8629 }
dde86e2d 8630
c2699524 8631 reg = HAS_PCH_LPT_LP(dev) ? SBI_GEN0 : SBI_DBUFF0;
2fa86a1f
PZ
8632 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8633 tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8634 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
c00db246 8635
a580516d 8636 mutex_unlock(&dev_priv->sb_lock);
dde86e2d
PZ
8637}
8638
47701c3b
PZ
8639/* Sequence to disable CLKOUT_DP */
8640static void lpt_disable_clkout_dp(struct drm_device *dev)
8641{
fac5e23e 8642 struct drm_i915_private *dev_priv = to_i915(dev);
47701c3b
PZ
8643 uint32_t reg, tmp;
8644
a580516d 8645 mutex_lock(&dev_priv->sb_lock);
47701c3b 8646
c2699524 8647 reg = HAS_PCH_LPT_LP(dev) ? SBI_GEN0 : SBI_DBUFF0;
47701c3b
PZ
8648 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8649 tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8650 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
8651
8652 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8653 if (!(tmp & SBI_SSCCTL_DISABLE)) {
8654 if (!(tmp & SBI_SSCCTL_PATHALT)) {
8655 tmp |= SBI_SSCCTL_PATHALT;
8656 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8657 udelay(32);
8658 }
8659 tmp |= SBI_SSCCTL_DISABLE;
8660 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8661 }
8662
a580516d 8663 mutex_unlock(&dev_priv->sb_lock);
47701c3b
PZ
8664}
8665
f7be2c21
VS
8666#define BEND_IDX(steps) ((50 + (steps)) / 5)
8667
8668static const uint16_t sscdivintphase[] = {
8669 [BEND_IDX( 50)] = 0x3B23,
8670 [BEND_IDX( 45)] = 0x3B23,
8671 [BEND_IDX( 40)] = 0x3C23,
8672 [BEND_IDX( 35)] = 0x3C23,
8673 [BEND_IDX( 30)] = 0x3D23,
8674 [BEND_IDX( 25)] = 0x3D23,
8675 [BEND_IDX( 20)] = 0x3E23,
8676 [BEND_IDX( 15)] = 0x3E23,
8677 [BEND_IDX( 10)] = 0x3F23,
8678 [BEND_IDX( 5)] = 0x3F23,
8679 [BEND_IDX( 0)] = 0x0025,
8680 [BEND_IDX( -5)] = 0x0025,
8681 [BEND_IDX(-10)] = 0x0125,
8682 [BEND_IDX(-15)] = 0x0125,
8683 [BEND_IDX(-20)] = 0x0225,
8684 [BEND_IDX(-25)] = 0x0225,
8685 [BEND_IDX(-30)] = 0x0325,
8686 [BEND_IDX(-35)] = 0x0325,
8687 [BEND_IDX(-40)] = 0x0425,
8688 [BEND_IDX(-45)] = 0x0425,
8689 [BEND_IDX(-50)] = 0x0525,
8690};
8691
8692/*
8693 * Bend CLKOUT_DP
8694 * steps -50 to 50 inclusive, in steps of 5
8695 * < 0 slow down the clock, > 0 speed up the clock, 0 == no bend (135MHz)
8696 * change in clock period = -(steps / 10) * 5.787 ps
8697 */
8698static void lpt_bend_clkout_dp(struct drm_i915_private *dev_priv, int steps)
8699{
8700 uint32_t tmp;
8701 int idx = BEND_IDX(steps);
8702
8703 if (WARN_ON(steps % 5 != 0))
8704 return;
8705
8706 if (WARN_ON(idx >= ARRAY_SIZE(sscdivintphase)))
8707 return;
8708
8709 mutex_lock(&dev_priv->sb_lock);
8710
8711 if (steps % 10 != 0)
8712 tmp = 0xAAAAAAAB;
8713 else
8714 tmp = 0x00000000;
8715 intel_sbi_write(dev_priv, SBI_SSCDITHPHASE, tmp, SBI_ICLK);
8716
8717 tmp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE, SBI_ICLK);
8718 tmp &= 0xffff0000;
8719 tmp |= sscdivintphase[idx];
8720 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE, tmp, SBI_ICLK);
8721
8722 mutex_unlock(&dev_priv->sb_lock);
8723}
8724
8725#undef BEND_IDX
8726
bf8fa3d3
PZ
8727static void lpt_init_pch_refclk(struct drm_device *dev)
8728{
bf8fa3d3
PZ
8729 struct intel_encoder *encoder;
8730 bool has_vga = false;
8731
b2784e15 8732 for_each_intel_encoder(dev, encoder) {
bf8fa3d3
PZ
8733 switch (encoder->type) {
8734 case INTEL_OUTPUT_ANALOG:
8735 has_vga = true;
8736 break;
6847d71b
PZ
8737 default:
8738 break;
bf8fa3d3
PZ
8739 }
8740 }
8741
f7be2c21
VS
8742 if (has_vga) {
8743 lpt_bend_clkout_dp(to_i915(dev), 0);
47701c3b 8744 lpt_enable_clkout_dp(dev, true, true);
f7be2c21 8745 } else {
47701c3b 8746 lpt_disable_clkout_dp(dev);
f7be2c21 8747 }
bf8fa3d3
PZ
8748}
8749
dde86e2d
PZ
8750/*
8751 * Initialize reference clocks when the driver loads
8752 */
8753void intel_init_pch_refclk(struct drm_device *dev)
8754{
8755 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
8756 ironlake_init_pch_refclk(dev);
8757 else if (HAS_PCH_LPT(dev))
8758 lpt_init_pch_refclk(dev);
8759}
8760
6ff93609 8761static void ironlake_set_pipeconf(struct drm_crtc *crtc)
79e53945 8762{
fac5e23e 8763 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
79e53945
JB
8764 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8765 int pipe = intel_crtc->pipe;
c8203565
PZ
8766 uint32_t val;
8767
78114071 8768 val = 0;
c8203565 8769
6e3c9717 8770 switch (intel_crtc->config->pipe_bpp) {
c8203565 8771 case 18:
dfd07d72 8772 val |= PIPECONF_6BPC;
c8203565
PZ
8773 break;
8774 case 24:
dfd07d72 8775 val |= PIPECONF_8BPC;
c8203565
PZ
8776 break;
8777 case 30:
dfd07d72 8778 val |= PIPECONF_10BPC;
c8203565
PZ
8779 break;
8780 case 36:
dfd07d72 8781 val |= PIPECONF_12BPC;
c8203565
PZ
8782 break;
8783 default:
cc769b62
PZ
8784 /* Case prevented by intel_choose_pipe_bpp_dither. */
8785 BUG();
c8203565
PZ
8786 }
8787
6e3c9717 8788 if (intel_crtc->config->dither)
c8203565
PZ
8789 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8790
6e3c9717 8791 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
c8203565
PZ
8792 val |= PIPECONF_INTERLACED_ILK;
8793 else
8794 val |= PIPECONF_PROGRESSIVE;
8795
6e3c9717 8796 if (intel_crtc->config->limited_color_range)
3685a8f3 8797 val |= PIPECONF_COLOR_RANGE_SELECT;
3685a8f3 8798
c8203565
PZ
8799 I915_WRITE(PIPECONF(pipe), val);
8800 POSTING_READ(PIPECONF(pipe));
8801}
8802
6ff93609 8803static void haswell_set_pipeconf(struct drm_crtc *crtc)
ee2b0b38 8804{
fac5e23e 8805 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
ee2b0b38 8806 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6e3c9717 8807 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
391bf048 8808 u32 val = 0;
ee2b0b38 8809
391bf048 8810 if (IS_HASWELL(dev_priv) && intel_crtc->config->dither)
ee2b0b38
PZ
8811 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8812
6e3c9717 8813 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
ee2b0b38
PZ
8814 val |= PIPECONF_INTERLACED_ILK;
8815 else
8816 val |= PIPECONF_PROGRESSIVE;
8817
702e7a56
PZ
8818 I915_WRITE(PIPECONF(cpu_transcoder), val);
8819 POSTING_READ(PIPECONF(cpu_transcoder));
391bf048
JN
8820}
8821
391bf048
JN
8822static void haswell_set_pipemisc(struct drm_crtc *crtc)
8823{
fac5e23e 8824 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
391bf048 8825 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
756f85cf 8826
391bf048
JN
8827 if (IS_BROADWELL(dev_priv) || INTEL_INFO(dev_priv)->gen >= 9) {
8828 u32 val = 0;
756f85cf 8829
6e3c9717 8830 switch (intel_crtc->config->pipe_bpp) {
756f85cf
PZ
8831 case 18:
8832 val |= PIPEMISC_DITHER_6_BPC;
8833 break;
8834 case 24:
8835 val |= PIPEMISC_DITHER_8_BPC;
8836 break;
8837 case 30:
8838 val |= PIPEMISC_DITHER_10_BPC;
8839 break;
8840 case 36:
8841 val |= PIPEMISC_DITHER_12_BPC;
8842 break;
8843 default:
8844 /* Case prevented by pipe_config_set_bpp. */
8845 BUG();
8846 }
8847
6e3c9717 8848 if (intel_crtc->config->dither)
756f85cf
PZ
8849 val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
8850
391bf048 8851 I915_WRITE(PIPEMISC(intel_crtc->pipe), val);
756f85cf 8852 }
ee2b0b38
PZ
8853}
8854
d4b1931c
PZ
8855int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
8856{
8857 /*
8858 * Account for spread spectrum to avoid
8859 * oversubscribing the link. Max center spread
8860 * is 2.5%; use 5% for safety's sake.
8861 */
8862 u32 bps = target_clock * bpp * 21 / 20;
619d4d04 8863 return DIV_ROUND_UP(bps, link_bw * 8);
d4b1931c
PZ
8864}
8865
7429e9d4 8866static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
6cf86a5e 8867{
7429e9d4 8868 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
f48d8f23
PZ
8869}
8870
b75ca6f6
ACO
8871static void ironlake_compute_dpll(struct intel_crtc *intel_crtc,
8872 struct intel_crtc_state *crtc_state,
9e2c8475 8873 struct dpll *reduced_clock)
79e53945 8874{
de13a2e3 8875 struct drm_crtc *crtc = &intel_crtc->base;
79e53945 8876 struct drm_device *dev = crtc->dev;
fac5e23e 8877 struct drm_i915_private *dev_priv = to_i915(dev);
b75ca6f6 8878 u32 dpll, fp, fp2;
3d6e9ee0 8879 int factor;
79e53945 8880
c1858123 8881 /* Enable autotuning of the PLL clock (if permissible) */
8febb297 8882 factor = 21;
3d6e9ee0 8883 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
8febb297 8884 if ((intel_panel_use_ssc(dev_priv) &&
e91e941b 8885 dev_priv->vbt.lvds_ssc_freq == 100000) ||
f0b44056 8886 (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
8febb297 8887 factor = 25;
190f68c5 8888 } else if (crtc_state->sdvo_tv_clock)
8febb297 8889 factor = 20;
c1858123 8890
b75ca6f6
ACO
8891 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
8892
190f68c5 8893 if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
b75ca6f6
ACO
8894 fp |= FP_CB_TUNE;
8895
8896 if (reduced_clock) {
8897 fp2 = i9xx_dpll_compute_fp(reduced_clock);
2c07245f 8898
b75ca6f6
ACO
8899 if (reduced_clock->m < factor * reduced_clock->n)
8900 fp2 |= FP_CB_TUNE;
8901 } else {
8902 fp2 = fp;
8903 }
9a7c7890 8904
5eddb70b 8905 dpll = 0;
2c07245f 8906
3d6e9ee0 8907 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS))
a07d6787
EA
8908 dpll |= DPLLB_MODE_LVDS;
8909 else
8910 dpll |= DPLLB_MODE_DAC_SERIAL;
198a037f 8911
190f68c5 8912 dpll |= (crtc_state->pixel_multiplier - 1)
ef1b460d 8913 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
198a037f 8914
3d6e9ee0
VS
8915 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO) ||
8916 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
4a33e48d 8917 dpll |= DPLL_SDVO_HIGH_SPEED;
3d6e9ee0 8918
37a5650b 8919 if (intel_crtc_has_dp_encoder(crtc_state))
4a33e48d 8920 dpll |= DPLL_SDVO_HIGH_SPEED;
79e53945 8921
a07d6787 8922 /* compute bitmask from p1 value */
190f68c5 8923 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
a07d6787 8924 /* also FPA1 */
190f68c5 8925 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
a07d6787 8926
190f68c5 8927 switch (crtc_state->dpll.p2) {
a07d6787
EA
8928 case 5:
8929 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
8930 break;
8931 case 7:
8932 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
8933 break;
8934 case 10:
8935 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
8936 break;
8937 case 14:
8938 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
8939 break;
79e53945
JB
8940 }
8941
3d6e9ee0
VS
8942 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
8943 intel_panel_use_ssc(dev_priv))
43565a06 8944 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
79e53945
JB
8945 else
8946 dpll |= PLL_REF_INPUT_DREFCLK;
8947
b75ca6f6
ACO
8948 dpll |= DPLL_VCO_ENABLE;
8949
8950 crtc_state->dpll_hw_state.dpll = dpll;
8951 crtc_state->dpll_hw_state.fp0 = fp;
8952 crtc_state->dpll_hw_state.fp1 = fp2;
de13a2e3
PZ
8953}
8954
190f68c5
ACO
8955static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
8956 struct intel_crtc_state *crtc_state)
de13a2e3 8957{
997c030c 8958 struct drm_device *dev = crtc->base.dev;
fac5e23e 8959 struct drm_i915_private *dev_priv = to_i915(dev);
9e2c8475 8960 struct dpll reduced_clock;
7ed9f894 8961 bool has_reduced_clock = false;
e2b78267 8962 struct intel_shared_dpll *pll;
1b6f4958 8963 const struct intel_limit *limit;
997c030c 8964 int refclk = 120000;
de13a2e3 8965
dd3cd74a
ACO
8966 memset(&crtc_state->dpll_hw_state, 0,
8967 sizeof(crtc_state->dpll_hw_state));
8968
ded220e2
ACO
8969 crtc->lowfreq_avail = false;
8970
8971 /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
8972 if (!crtc_state->has_pch_encoder)
8973 return 0;
79e53945 8974
2d84d2b3 8975 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
997c030c
ACO
8976 if (intel_panel_use_ssc(dev_priv)) {
8977 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
8978 dev_priv->vbt.lvds_ssc_freq);
8979 refclk = dev_priv->vbt.lvds_ssc_freq;
8980 }
8981
8982 if (intel_is_dual_link_lvds(dev)) {
8983 if (refclk == 100000)
8984 limit = &intel_limits_ironlake_dual_lvds_100m;
8985 else
8986 limit = &intel_limits_ironlake_dual_lvds;
8987 } else {
8988 if (refclk == 100000)
8989 limit = &intel_limits_ironlake_single_lvds_100m;
8990 else
8991 limit = &intel_limits_ironlake_single_lvds;
8992 }
8993 } else {
8994 limit = &intel_limits_ironlake_dac;
8995 }
8996
364ee29d 8997 if (!crtc_state->clock_set &&
997c030c
ACO
8998 !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8999 refclk, NULL, &crtc_state->dpll)) {
364ee29d
ACO
9000 DRM_ERROR("Couldn't find PLL settings for mode!\n");
9001 return -EINVAL;
f47709a9 9002 }
79e53945 9003
b75ca6f6
ACO
9004 ironlake_compute_dpll(crtc, crtc_state,
9005 has_reduced_clock ? &reduced_clock : NULL);
66e985c0 9006
ded220e2
ACO
9007 pll = intel_get_shared_dpll(crtc, crtc_state, NULL);
9008 if (pll == NULL) {
9009 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
9010 pipe_name(crtc->pipe));
9011 return -EINVAL;
3fb37703 9012 }
79e53945 9013
2d84d2b3 9014 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
ded220e2 9015 has_reduced_clock)
c7653199 9016 crtc->lowfreq_avail = true;
e2b78267 9017
c8f7a0db 9018 return 0;
79e53945
JB
9019}
9020
eb14cb74
VS
9021static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
9022 struct intel_link_m_n *m_n)
9023{
9024 struct drm_device *dev = crtc->base.dev;
fac5e23e 9025 struct drm_i915_private *dev_priv = to_i915(dev);
eb14cb74
VS
9026 enum pipe pipe = crtc->pipe;
9027
9028 m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
9029 m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
9030 m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
9031 & ~TU_SIZE_MASK;
9032 m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
9033 m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
9034 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
9035}
9036
9037static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
9038 enum transcoder transcoder,
b95af8be
VK
9039 struct intel_link_m_n *m_n,
9040 struct intel_link_m_n *m2_n2)
72419203
DV
9041{
9042 struct drm_device *dev = crtc->base.dev;
fac5e23e 9043 struct drm_i915_private *dev_priv = to_i915(dev);
eb14cb74 9044 enum pipe pipe = crtc->pipe;
72419203 9045
eb14cb74
VS
9046 if (INTEL_INFO(dev)->gen >= 5) {
9047 m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
9048 m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
9049 m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
9050 & ~TU_SIZE_MASK;
9051 m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
9052 m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
9053 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
b95af8be
VK
9054 /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
9055 * gen < 8) and if DRRS is supported (to make sure the
9056 * registers are not unnecessarily read).
9057 */
9058 if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
6e3c9717 9059 crtc->config->has_drrs) {
b95af8be
VK
9060 m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
9061 m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
9062 m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
9063 & ~TU_SIZE_MASK;
9064 m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
9065 m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
9066 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
9067 }
eb14cb74
VS
9068 } else {
9069 m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
9070 m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
9071 m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
9072 & ~TU_SIZE_MASK;
9073 m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
9074 m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
9075 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
9076 }
9077}
9078
9079void intel_dp_get_m_n(struct intel_crtc *crtc,
5cec258b 9080 struct intel_crtc_state *pipe_config)
eb14cb74 9081{
681a8504 9082 if (pipe_config->has_pch_encoder)
eb14cb74
VS
9083 intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
9084 else
9085 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
b95af8be
VK
9086 &pipe_config->dp_m_n,
9087 &pipe_config->dp_m2_n2);
eb14cb74 9088}
72419203 9089
eb14cb74 9090static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
5cec258b 9091 struct intel_crtc_state *pipe_config)
eb14cb74
VS
9092{
9093 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
b95af8be 9094 &pipe_config->fdi_m_n, NULL);
72419203
DV
9095}
9096
bd2e244f 9097static void skylake_get_pfit_config(struct intel_crtc *crtc,
5cec258b 9098 struct intel_crtc_state *pipe_config)
bd2e244f
JB
9099{
9100 struct drm_device *dev = crtc->base.dev;
fac5e23e 9101 struct drm_i915_private *dev_priv = to_i915(dev);
a1b2278e
CK
9102 struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
9103 uint32_t ps_ctrl = 0;
9104 int id = -1;
9105 int i;
bd2e244f 9106
a1b2278e
CK
9107 /* find scaler attached to this pipe */
9108 for (i = 0; i < crtc->num_scalers; i++) {
9109 ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
9110 if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
9111 id = i;
9112 pipe_config->pch_pfit.enabled = true;
9113 pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
9114 pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
9115 break;
9116 }
9117 }
bd2e244f 9118
a1b2278e
CK
9119 scaler_state->scaler_id = id;
9120 if (id >= 0) {
9121 scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
9122 } else {
9123 scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
bd2e244f
JB
9124 }
9125}
9126
5724dbd1
DL
9127static void
9128skylake_get_initial_plane_config(struct intel_crtc *crtc,
9129 struct intel_initial_plane_config *plane_config)
bc8d7dff
DL
9130{
9131 struct drm_device *dev = crtc->base.dev;
fac5e23e 9132 struct drm_i915_private *dev_priv = to_i915(dev);
40f46283 9133 u32 val, base, offset, stride_mult, tiling;
bc8d7dff
DL
9134 int pipe = crtc->pipe;
9135 int fourcc, pixel_format;
6761dd31 9136 unsigned int aligned_height;
bc8d7dff 9137 struct drm_framebuffer *fb;
1b842c89 9138 struct intel_framebuffer *intel_fb;
bc8d7dff 9139
d9806c9f 9140 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
1b842c89 9141 if (!intel_fb) {
bc8d7dff
DL
9142 DRM_DEBUG_KMS("failed to alloc fb\n");
9143 return;
9144 }
9145
1b842c89
DL
9146 fb = &intel_fb->base;
9147
bc8d7dff 9148 val = I915_READ(PLANE_CTL(pipe, 0));
42a7b088
DL
9149 if (!(val & PLANE_CTL_ENABLE))
9150 goto error;
9151
bc8d7dff
DL
9152 pixel_format = val & PLANE_CTL_FORMAT_MASK;
9153 fourcc = skl_format_to_fourcc(pixel_format,
9154 val & PLANE_CTL_ORDER_RGBX,
9155 val & PLANE_CTL_ALPHA_MASK);
9156 fb->pixel_format = fourcc;
9157 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
9158
40f46283
DL
9159 tiling = val & PLANE_CTL_TILED_MASK;
9160 switch (tiling) {
9161 case PLANE_CTL_TILED_LINEAR:
9162 fb->modifier[0] = DRM_FORMAT_MOD_NONE;
9163 break;
9164 case PLANE_CTL_TILED_X:
9165 plane_config->tiling = I915_TILING_X;
9166 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
9167 break;
9168 case PLANE_CTL_TILED_Y:
9169 fb->modifier[0] = I915_FORMAT_MOD_Y_TILED;
9170 break;
9171 case PLANE_CTL_TILED_YF:
9172 fb->modifier[0] = I915_FORMAT_MOD_Yf_TILED;
9173 break;
9174 default:
9175 MISSING_CASE(tiling);
9176 goto error;
9177 }
9178
bc8d7dff
DL
9179 base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
9180 plane_config->base = base;
9181
9182 offset = I915_READ(PLANE_OFFSET(pipe, 0));
9183
9184 val = I915_READ(PLANE_SIZE(pipe, 0));
9185 fb->height = ((val >> 16) & 0xfff) + 1;
9186 fb->width = ((val >> 0) & 0x1fff) + 1;
9187
9188 val = I915_READ(PLANE_STRIDE(pipe, 0));
7b49f948 9189 stride_mult = intel_fb_stride_alignment(dev_priv, fb->modifier[0],
40f46283 9190 fb->pixel_format);
bc8d7dff
DL
9191 fb->pitches[0] = (val & 0x3ff) * stride_mult;
9192
9193 aligned_height = intel_fb_align_height(dev, fb->height,
091df6cb
DV
9194 fb->pixel_format,
9195 fb->modifier[0]);
bc8d7dff 9196
f37b5c2b 9197 plane_config->size = fb->pitches[0] * aligned_height;
bc8d7dff
DL
9198
9199 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9200 pipe_name(pipe), fb->width, fb->height,
9201 fb->bits_per_pixel, base, fb->pitches[0],
9202 plane_config->size);
9203
2d14030b 9204 plane_config->fb = intel_fb;
bc8d7dff
DL
9205 return;
9206
9207error:
9208 kfree(fb);
9209}
9210
2fa2fe9a 9211static void ironlake_get_pfit_config(struct intel_crtc *crtc,
5cec258b 9212 struct intel_crtc_state *pipe_config)
2fa2fe9a
DV
9213{
9214 struct drm_device *dev = crtc->base.dev;
fac5e23e 9215 struct drm_i915_private *dev_priv = to_i915(dev);
2fa2fe9a
DV
9216 uint32_t tmp;
9217
9218 tmp = I915_READ(PF_CTL(crtc->pipe));
9219
9220 if (tmp & PF_ENABLE) {
fd4daa9c 9221 pipe_config->pch_pfit.enabled = true;
2fa2fe9a
DV
9222 pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
9223 pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
cb8b2a30
DV
9224
9225 /* We currently do not free assignements of panel fitters on
9226 * ivb/hsw (since we don't use the higher upscaling modes which
9227 * differentiates them) so just WARN about this case for now. */
9228 if (IS_GEN7(dev)) {
9229 WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
9230 PF_PIPE_SEL_IVB(crtc->pipe));
9231 }
2fa2fe9a 9232 }
79e53945
JB
9233}
9234
5724dbd1
DL
9235static void
9236ironlake_get_initial_plane_config(struct intel_crtc *crtc,
9237 struct intel_initial_plane_config *plane_config)
4c6baa59
JB
9238{
9239 struct drm_device *dev = crtc->base.dev;
fac5e23e 9240 struct drm_i915_private *dev_priv = to_i915(dev);
4c6baa59 9241 u32 val, base, offset;
aeee5a49 9242 int pipe = crtc->pipe;
4c6baa59 9243 int fourcc, pixel_format;
6761dd31 9244 unsigned int aligned_height;
b113d5ee 9245 struct drm_framebuffer *fb;
1b842c89 9246 struct intel_framebuffer *intel_fb;
4c6baa59 9247
42a7b088
DL
9248 val = I915_READ(DSPCNTR(pipe));
9249 if (!(val & DISPLAY_PLANE_ENABLE))
9250 return;
9251
d9806c9f 9252 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
1b842c89 9253 if (!intel_fb) {
4c6baa59
JB
9254 DRM_DEBUG_KMS("failed to alloc fb\n");
9255 return;
9256 }
9257
1b842c89
DL
9258 fb = &intel_fb->base;
9259
18c5247e
DV
9260 if (INTEL_INFO(dev)->gen >= 4) {
9261 if (val & DISPPLANE_TILED) {
49af449b 9262 plane_config->tiling = I915_TILING_X;
18c5247e
DV
9263 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
9264 }
9265 }
4c6baa59
JB
9266
9267 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
b35d63fa 9268 fourcc = i9xx_format_to_fourcc(pixel_format);
b113d5ee
DL
9269 fb->pixel_format = fourcc;
9270 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
4c6baa59 9271
aeee5a49 9272 base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
4c6baa59 9273 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
aeee5a49 9274 offset = I915_READ(DSPOFFSET(pipe));
4c6baa59 9275 } else {
49af449b 9276 if (plane_config->tiling)
aeee5a49 9277 offset = I915_READ(DSPTILEOFF(pipe));
4c6baa59 9278 else
aeee5a49 9279 offset = I915_READ(DSPLINOFF(pipe));
4c6baa59
JB
9280 }
9281 plane_config->base = base;
9282
9283 val = I915_READ(PIPESRC(pipe));
b113d5ee
DL
9284 fb->width = ((val >> 16) & 0xfff) + 1;
9285 fb->height = ((val >> 0) & 0xfff) + 1;
4c6baa59
JB
9286
9287 val = I915_READ(DSPSTRIDE(pipe));
b113d5ee 9288 fb->pitches[0] = val & 0xffffffc0;
4c6baa59 9289
b113d5ee 9290 aligned_height = intel_fb_align_height(dev, fb->height,
091df6cb
DV
9291 fb->pixel_format,
9292 fb->modifier[0]);
4c6baa59 9293
f37b5c2b 9294 plane_config->size = fb->pitches[0] * aligned_height;
4c6baa59 9295
2844a921
DL
9296 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9297 pipe_name(pipe), fb->width, fb->height,
9298 fb->bits_per_pixel, base, fb->pitches[0],
9299 plane_config->size);
b113d5ee 9300
2d14030b 9301 plane_config->fb = intel_fb;
4c6baa59
JB
9302}
9303
0e8ffe1b 9304static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
5cec258b 9305 struct intel_crtc_state *pipe_config)
0e8ffe1b
DV
9306{
9307 struct drm_device *dev = crtc->base.dev;
fac5e23e 9308 struct drm_i915_private *dev_priv = to_i915(dev);
1729050e 9309 enum intel_display_power_domain power_domain;
0e8ffe1b 9310 uint32_t tmp;
1729050e 9311 bool ret;
0e8ffe1b 9312
1729050e
ID
9313 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
9314 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
930e8c9e
PZ
9315 return false;
9316
e143a21c 9317 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
8106ddbd 9318 pipe_config->shared_dpll = NULL;
eccb140b 9319
1729050e 9320 ret = false;
0e8ffe1b
DV
9321 tmp = I915_READ(PIPECONF(crtc->pipe));
9322 if (!(tmp & PIPECONF_ENABLE))
1729050e 9323 goto out;
0e8ffe1b 9324
42571aef
VS
9325 switch (tmp & PIPECONF_BPC_MASK) {
9326 case PIPECONF_6BPC:
9327 pipe_config->pipe_bpp = 18;
9328 break;
9329 case PIPECONF_8BPC:
9330 pipe_config->pipe_bpp = 24;
9331 break;
9332 case PIPECONF_10BPC:
9333 pipe_config->pipe_bpp = 30;
9334 break;
9335 case PIPECONF_12BPC:
9336 pipe_config->pipe_bpp = 36;
9337 break;
9338 default:
9339 break;
9340 }
9341
b5a9fa09
DV
9342 if (tmp & PIPECONF_COLOR_RANGE_SELECT)
9343 pipe_config->limited_color_range = true;
9344
ab9412ba 9345 if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
66e985c0 9346 struct intel_shared_dpll *pll;
8106ddbd 9347 enum intel_dpll_id pll_id;
66e985c0 9348
88adfff1
DV
9349 pipe_config->has_pch_encoder = true;
9350
627eb5a3
DV
9351 tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
9352 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
9353 FDI_DP_PORT_WIDTH_SHIFT) + 1;
72419203
DV
9354
9355 ironlake_get_fdi_m_n_config(crtc, pipe_config);
6c49f241 9356
2d1fe073 9357 if (HAS_PCH_IBX(dev_priv)) {
d9a7bc67
ID
9358 /*
9359 * The pipe->pch transcoder and pch transcoder->pll
9360 * mapping is fixed.
9361 */
8106ddbd 9362 pll_id = (enum intel_dpll_id) crtc->pipe;
c0d43d62
DV
9363 } else {
9364 tmp = I915_READ(PCH_DPLL_SEL);
9365 if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
8106ddbd 9366 pll_id = DPLL_ID_PCH_PLL_B;
c0d43d62 9367 else
8106ddbd 9368 pll_id= DPLL_ID_PCH_PLL_A;
c0d43d62 9369 }
66e985c0 9370
8106ddbd
ACO
9371 pipe_config->shared_dpll =
9372 intel_get_shared_dpll_by_id(dev_priv, pll_id);
9373 pll = pipe_config->shared_dpll;
66e985c0 9374
2edd6443
ACO
9375 WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
9376 &pipe_config->dpll_hw_state));
c93f54cf
DV
9377
9378 tmp = pipe_config->dpll_hw_state.dpll;
9379 pipe_config->pixel_multiplier =
9380 ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
9381 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
18442d08
VS
9382
9383 ironlake_pch_clock_get(crtc, pipe_config);
6c49f241
DV
9384 } else {
9385 pipe_config->pixel_multiplier = 1;
627eb5a3
DV
9386 }
9387
1bd1bd80 9388 intel_get_pipe_timings(crtc, pipe_config);
bc58be60 9389 intel_get_pipe_src_size(crtc, pipe_config);
1bd1bd80 9390
2fa2fe9a
DV
9391 ironlake_get_pfit_config(crtc, pipe_config);
9392
1729050e
ID
9393 ret = true;
9394
9395out:
9396 intel_display_power_put(dev_priv, power_domain);
9397
9398 return ret;
0e8ffe1b
DV
9399}
9400
be256dc7
PZ
9401static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
9402{
91c8a326 9403 struct drm_device *dev = &dev_priv->drm;
be256dc7 9404 struct intel_crtc *crtc;
be256dc7 9405
d3fcc808 9406 for_each_intel_crtc(dev, crtc)
e2c719b7 9407 I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
be256dc7
PZ
9408 pipe_name(crtc->pipe));
9409
e2c719b7
RC
9410 I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
9411 I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
01403de3
VS
9412 I915_STATE_WARN(I915_READ(WRPLL_CTL(0)) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
9413 I915_STATE_WARN(I915_READ(WRPLL_CTL(1)) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
e2c719b7
RC
9414 I915_STATE_WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
9415 I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
be256dc7 9416 "CPU PWM1 enabled\n");
c5107b87 9417 if (IS_HASWELL(dev))
e2c719b7 9418 I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
c5107b87 9419 "CPU PWM2 enabled\n");
e2c719b7 9420 I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
be256dc7 9421 "PCH PWM1 enabled\n");
e2c719b7 9422 I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
be256dc7 9423 "Utility pin enabled\n");
e2c719b7 9424 I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
be256dc7 9425
9926ada1
PZ
9426 /*
9427 * In theory we can still leave IRQs enabled, as long as only the HPD
9428 * interrupts remain enabled. We used to check for that, but since it's
9429 * gen-specific and since we only disable LCPLL after we fully disable
9430 * the interrupts, the check below should be enough.
9431 */
e2c719b7 9432 I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
be256dc7
PZ
9433}
9434
9ccd5aeb
PZ
9435static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
9436{
91c8a326 9437 struct drm_device *dev = &dev_priv->drm;
9ccd5aeb
PZ
9438
9439 if (IS_HASWELL(dev))
9440 return I915_READ(D_COMP_HSW);
9441 else
9442 return I915_READ(D_COMP_BDW);
9443}
9444
3c4c9b81
PZ
9445static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
9446{
91c8a326 9447 struct drm_device *dev = &dev_priv->drm;
3c4c9b81
PZ
9448
9449 if (IS_HASWELL(dev)) {
9450 mutex_lock(&dev_priv->rps.hw_lock);
9451 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
9452 val))
f475dadf 9453 DRM_ERROR("Failed to write to D_COMP\n");
3c4c9b81
PZ
9454 mutex_unlock(&dev_priv->rps.hw_lock);
9455 } else {
9ccd5aeb
PZ
9456 I915_WRITE(D_COMP_BDW, val);
9457 POSTING_READ(D_COMP_BDW);
3c4c9b81 9458 }
be256dc7
PZ
9459}
9460
9461/*
9462 * This function implements pieces of two sequences from BSpec:
9463 * - Sequence for display software to disable LCPLL
9464 * - Sequence for display software to allow package C8+
9465 * The steps implemented here are just the steps that actually touch the LCPLL
9466 * register. Callers should take care of disabling all the display engine
9467 * functions, doing the mode unset, fixing interrupts, etc.
9468 */
6ff58d53
PZ
9469static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
9470 bool switch_to_fclk, bool allow_power_down)
be256dc7
PZ
9471{
9472 uint32_t val;
9473
9474 assert_can_disable_lcpll(dev_priv);
9475
9476 val = I915_READ(LCPLL_CTL);
9477
9478 if (switch_to_fclk) {
9479 val |= LCPLL_CD_SOURCE_FCLK;
9480 I915_WRITE(LCPLL_CTL, val);
9481
f53dd63f
ID
9482 if (wait_for_us(I915_READ(LCPLL_CTL) &
9483 LCPLL_CD_SOURCE_FCLK_DONE, 1))
be256dc7
PZ
9484 DRM_ERROR("Switching to FCLK failed\n");
9485
9486 val = I915_READ(LCPLL_CTL);
9487 }
9488
9489 val |= LCPLL_PLL_DISABLE;
9490 I915_WRITE(LCPLL_CTL, val);
9491 POSTING_READ(LCPLL_CTL);
9492
24d8441d 9493 if (intel_wait_for_register(dev_priv, LCPLL_CTL, LCPLL_PLL_LOCK, 0, 1))
be256dc7
PZ
9494 DRM_ERROR("LCPLL still locked\n");
9495
9ccd5aeb 9496 val = hsw_read_dcomp(dev_priv);
be256dc7 9497 val |= D_COMP_COMP_DISABLE;
3c4c9b81 9498 hsw_write_dcomp(dev_priv, val);
be256dc7
PZ
9499 ndelay(100);
9500
9ccd5aeb
PZ
9501 if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
9502 1))
be256dc7
PZ
9503 DRM_ERROR("D_COMP RCOMP still in progress\n");
9504
9505 if (allow_power_down) {
9506 val = I915_READ(LCPLL_CTL);
9507 val |= LCPLL_POWER_DOWN_ALLOW;
9508 I915_WRITE(LCPLL_CTL, val);
9509 POSTING_READ(LCPLL_CTL);
9510 }
9511}
9512
9513/*
9514 * Fully restores LCPLL, disallowing power down and switching back to LCPLL
9515 * source.
9516 */
6ff58d53 9517static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
be256dc7
PZ
9518{
9519 uint32_t val;
9520
9521 val = I915_READ(LCPLL_CTL);
9522
9523 if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
9524 LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
9525 return;
9526
a8a8bd54
PZ
9527 /*
9528 * Make sure we're not on PC8 state before disabling PC8, otherwise
9529 * we'll hang the machine. To prevent PC8 state, just enable force_wake.
a8a8bd54 9530 */
59bad947 9531 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
215733fa 9532
be256dc7
PZ
9533 if (val & LCPLL_POWER_DOWN_ALLOW) {
9534 val &= ~LCPLL_POWER_DOWN_ALLOW;
9535 I915_WRITE(LCPLL_CTL, val);
35d8f2eb 9536 POSTING_READ(LCPLL_CTL);
be256dc7
PZ
9537 }
9538
9ccd5aeb 9539 val = hsw_read_dcomp(dev_priv);
be256dc7
PZ
9540 val |= D_COMP_COMP_FORCE;
9541 val &= ~D_COMP_COMP_DISABLE;
3c4c9b81 9542 hsw_write_dcomp(dev_priv, val);
be256dc7
PZ
9543
9544 val = I915_READ(LCPLL_CTL);
9545 val &= ~LCPLL_PLL_DISABLE;
9546 I915_WRITE(LCPLL_CTL, val);
9547
93220c08
CW
9548 if (intel_wait_for_register(dev_priv,
9549 LCPLL_CTL, LCPLL_PLL_LOCK, LCPLL_PLL_LOCK,
9550 5))
be256dc7
PZ
9551 DRM_ERROR("LCPLL not locked yet\n");
9552
9553 if (val & LCPLL_CD_SOURCE_FCLK) {
9554 val = I915_READ(LCPLL_CTL);
9555 val &= ~LCPLL_CD_SOURCE_FCLK;
9556 I915_WRITE(LCPLL_CTL, val);
9557
f53dd63f
ID
9558 if (wait_for_us((I915_READ(LCPLL_CTL) &
9559 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
be256dc7
PZ
9560 DRM_ERROR("Switching back to LCPLL failed\n");
9561 }
215733fa 9562
59bad947 9563 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
91c8a326 9564 intel_update_cdclk(&dev_priv->drm);
be256dc7
PZ
9565}
9566
765dab67
PZ
9567/*
9568 * Package states C8 and deeper are really deep PC states that can only be
9569 * reached when all the devices on the system allow it, so even if the graphics
9570 * device allows PC8+, it doesn't mean the system will actually get to these
9571 * states. Our driver only allows PC8+ when going into runtime PM.
9572 *
9573 * The requirements for PC8+ are that all the outputs are disabled, the power
9574 * well is disabled and most interrupts are disabled, and these are also
9575 * requirements for runtime PM. When these conditions are met, we manually do
9576 * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
9577 * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
9578 * hang the machine.
9579 *
9580 * When we really reach PC8 or deeper states (not just when we allow it) we lose
9581 * the state of some registers, so when we come back from PC8+ we need to
9582 * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
9583 * need to take care of the registers kept by RC6. Notice that this happens even
9584 * if we don't put the device in PCI D3 state (which is what currently happens
9585 * because of the runtime PM support).
9586 *
9587 * For more, read "Display Sequences for Package C8" on the hardware
9588 * documentation.
9589 */
a14cb6fc 9590void hsw_enable_pc8(struct drm_i915_private *dev_priv)
c67a470b 9591{
91c8a326 9592 struct drm_device *dev = &dev_priv->drm;
c67a470b
PZ
9593 uint32_t val;
9594
c67a470b
PZ
9595 DRM_DEBUG_KMS("Enabling package C8+\n");
9596
c2699524 9597 if (HAS_PCH_LPT_LP(dev)) {
c67a470b
PZ
9598 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9599 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
9600 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9601 }
9602
9603 lpt_disable_clkout_dp(dev);
c67a470b
PZ
9604 hsw_disable_lcpll(dev_priv, true, true);
9605}
9606
a14cb6fc 9607void hsw_disable_pc8(struct drm_i915_private *dev_priv)
c67a470b 9608{
91c8a326 9609 struct drm_device *dev = &dev_priv->drm;
c67a470b
PZ
9610 uint32_t val;
9611
c67a470b
PZ
9612 DRM_DEBUG_KMS("Disabling package C8+\n");
9613
9614 hsw_restore_lcpll(dev_priv);
c67a470b
PZ
9615 lpt_init_pch_refclk(dev);
9616
c2699524 9617 if (HAS_PCH_LPT_LP(dev)) {
c67a470b
PZ
9618 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9619 val |= PCH_LP_PARTITION_LEVEL_DISABLE;
9620 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9621 }
c67a470b
PZ
9622}
9623
324513c0 9624static void bxt_modeset_commit_cdclk(struct drm_atomic_state *old_state)
f8437dd1 9625{
a821fc46 9626 struct drm_device *dev = old_state->dev;
1a617b77
ML
9627 struct intel_atomic_state *old_intel_state =
9628 to_intel_atomic_state(old_state);
9629 unsigned int req_cdclk = old_intel_state->dev_cdclk;
f8437dd1 9630
324513c0 9631 bxt_set_cdclk(to_i915(dev), req_cdclk);
f8437dd1
VK
9632}
9633
b432e5cf 9634/* compute the max rate for new configuration */
27c329ed 9635static int ilk_max_pixel_rate(struct drm_atomic_state *state)
b432e5cf 9636{
565602d7 9637 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
fac5e23e 9638 struct drm_i915_private *dev_priv = to_i915(state->dev);
565602d7
ML
9639 struct drm_crtc *crtc;
9640 struct drm_crtc_state *cstate;
27c329ed 9641 struct intel_crtc_state *crtc_state;
565602d7
ML
9642 unsigned max_pixel_rate = 0, i;
9643 enum pipe pipe;
b432e5cf 9644
565602d7
ML
9645 memcpy(intel_state->min_pixclk, dev_priv->min_pixclk,
9646 sizeof(intel_state->min_pixclk));
27c329ed 9647
565602d7
ML
9648 for_each_crtc_in_state(state, crtc, cstate, i) {
9649 int pixel_rate;
27c329ed 9650
565602d7
ML
9651 crtc_state = to_intel_crtc_state(cstate);
9652 if (!crtc_state->base.enable) {
9653 intel_state->min_pixclk[i] = 0;
b432e5cf 9654 continue;
565602d7 9655 }
b432e5cf 9656
27c329ed 9657 pixel_rate = ilk_pipe_pixel_rate(crtc_state);
b432e5cf
VS
9658
9659 /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
565602d7 9660 if (IS_BROADWELL(dev_priv) && crtc_state->ips_enabled)
b432e5cf
VS
9661 pixel_rate = DIV_ROUND_UP(pixel_rate * 100, 95);
9662
565602d7 9663 intel_state->min_pixclk[i] = pixel_rate;
b432e5cf
VS
9664 }
9665
565602d7
ML
9666 for_each_pipe(dev_priv, pipe)
9667 max_pixel_rate = max(intel_state->min_pixclk[pipe], max_pixel_rate);
9668
b432e5cf
VS
9669 return max_pixel_rate;
9670}
9671
9672static void broadwell_set_cdclk(struct drm_device *dev, int cdclk)
9673{
fac5e23e 9674 struct drm_i915_private *dev_priv = to_i915(dev);
b432e5cf
VS
9675 uint32_t val, data;
9676 int ret;
9677
9678 if (WARN((I915_READ(LCPLL_CTL) &
9679 (LCPLL_PLL_DISABLE | LCPLL_PLL_LOCK |
9680 LCPLL_CD_CLOCK_DISABLE | LCPLL_ROOT_CD_CLOCK_DISABLE |
9681 LCPLL_CD2X_CLOCK_DISABLE | LCPLL_POWER_DOWN_ALLOW |
9682 LCPLL_CD_SOURCE_FCLK)) != LCPLL_PLL_LOCK,
9683 "trying to change cdclk frequency with cdclk not enabled\n"))
9684 return;
9685
9686 mutex_lock(&dev_priv->rps.hw_lock);
9687 ret = sandybridge_pcode_write(dev_priv,
9688 BDW_PCODE_DISPLAY_FREQ_CHANGE_REQ, 0x0);
9689 mutex_unlock(&dev_priv->rps.hw_lock);
9690 if (ret) {
9691 DRM_ERROR("failed to inform pcode about cdclk change\n");
9692 return;
9693 }
9694
9695 val = I915_READ(LCPLL_CTL);
9696 val |= LCPLL_CD_SOURCE_FCLK;
9697 I915_WRITE(LCPLL_CTL, val);
9698
5ba00178
TU
9699 if (wait_for_us(I915_READ(LCPLL_CTL) &
9700 LCPLL_CD_SOURCE_FCLK_DONE, 1))
b432e5cf
VS
9701 DRM_ERROR("Switching to FCLK failed\n");
9702
9703 val = I915_READ(LCPLL_CTL);
9704 val &= ~LCPLL_CLK_FREQ_MASK;
9705
9706 switch (cdclk) {
9707 case 450000:
9708 val |= LCPLL_CLK_FREQ_450;
9709 data = 0;
9710 break;
9711 case 540000:
9712 val |= LCPLL_CLK_FREQ_54O_BDW;
9713 data = 1;
9714 break;
9715 case 337500:
9716 val |= LCPLL_CLK_FREQ_337_5_BDW;
9717 data = 2;
9718 break;
9719 case 675000:
9720 val |= LCPLL_CLK_FREQ_675_BDW;
9721 data = 3;
9722 break;
9723 default:
9724 WARN(1, "invalid cdclk frequency\n");
9725 return;
9726 }
9727
9728 I915_WRITE(LCPLL_CTL, val);
9729
9730 val = I915_READ(LCPLL_CTL);
9731 val &= ~LCPLL_CD_SOURCE_FCLK;
9732 I915_WRITE(LCPLL_CTL, val);
9733
5ba00178
TU
9734 if (wait_for_us((I915_READ(LCPLL_CTL) &
9735 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
b432e5cf
VS
9736 DRM_ERROR("Switching back to LCPLL failed\n");
9737
9738 mutex_lock(&dev_priv->rps.hw_lock);
9739 sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ, data);
9740 mutex_unlock(&dev_priv->rps.hw_lock);
9741
7f1052a8
VS
9742 I915_WRITE(CDCLK_FREQ, DIV_ROUND_CLOSEST(cdclk, 1000) - 1);
9743
b432e5cf
VS
9744 intel_update_cdclk(dev);
9745
9746 WARN(cdclk != dev_priv->cdclk_freq,
9747 "cdclk requested %d kHz but got %d kHz\n",
9748 cdclk, dev_priv->cdclk_freq);
9749}
9750
587c7914
VS
9751static int broadwell_calc_cdclk(int max_pixclk)
9752{
9753 if (max_pixclk > 540000)
9754 return 675000;
9755 else if (max_pixclk > 450000)
9756 return 540000;
9757 else if (max_pixclk > 337500)
9758 return 450000;
9759 else
9760 return 337500;
9761}
9762
27c329ed 9763static int broadwell_modeset_calc_cdclk(struct drm_atomic_state *state)
b432e5cf 9764{
27c329ed 9765 struct drm_i915_private *dev_priv = to_i915(state->dev);
1a617b77 9766 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
27c329ed 9767 int max_pixclk = ilk_max_pixel_rate(state);
b432e5cf
VS
9768 int cdclk;
9769
9770 /*
9771 * FIXME should also account for plane ratio
9772 * once 64bpp pixel formats are supported.
9773 */
587c7914 9774 cdclk = broadwell_calc_cdclk(max_pixclk);
b432e5cf 9775
b432e5cf 9776 if (cdclk > dev_priv->max_cdclk_freq) {
63ba534e
ML
9777 DRM_DEBUG_KMS("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
9778 cdclk, dev_priv->max_cdclk_freq);
9779 return -EINVAL;
b432e5cf
VS
9780 }
9781
1a617b77
ML
9782 intel_state->cdclk = intel_state->dev_cdclk = cdclk;
9783 if (!intel_state->active_crtcs)
587c7914 9784 intel_state->dev_cdclk = broadwell_calc_cdclk(0);
b432e5cf
VS
9785
9786 return 0;
9787}
9788
27c329ed 9789static void broadwell_modeset_commit_cdclk(struct drm_atomic_state *old_state)
b432e5cf 9790{
27c329ed 9791 struct drm_device *dev = old_state->dev;
1a617b77
ML
9792 struct intel_atomic_state *old_intel_state =
9793 to_intel_atomic_state(old_state);
9794 unsigned req_cdclk = old_intel_state->dev_cdclk;
b432e5cf 9795
27c329ed 9796 broadwell_set_cdclk(dev, req_cdclk);
b432e5cf
VS
9797}
9798
c89e39f3
CT
9799static int skl_modeset_calc_cdclk(struct drm_atomic_state *state)
9800{
9801 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
9802 struct drm_i915_private *dev_priv = to_i915(state->dev);
9803 const int max_pixclk = ilk_max_pixel_rate(state);
a8ca4934 9804 int vco = intel_state->cdclk_pll_vco;
c89e39f3
CT
9805 int cdclk;
9806
9807 /*
9808 * FIXME should also account for plane ratio
9809 * once 64bpp pixel formats are supported.
9810 */
a8ca4934 9811 cdclk = skl_calc_cdclk(max_pixclk, vco);
c89e39f3
CT
9812
9813 /*
9814 * FIXME move the cdclk caclulation to
9815 * compute_config() so we can fail gracegully.
9816 */
9817 if (cdclk > dev_priv->max_cdclk_freq) {
9818 DRM_ERROR("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
9819 cdclk, dev_priv->max_cdclk_freq);
9820 cdclk = dev_priv->max_cdclk_freq;
9821 }
9822
9823 intel_state->cdclk = intel_state->dev_cdclk = cdclk;
9824 if (!intel_state->active_crtcs)
a8ca4934 9825 intel_state->dev_cdclk = skl_calc_cdclk(0, vco);
c89e39f3
CT
9826
9827 return 0;
9828}
9829
9830static void skl_modeset_commit_cdclk(struct drm_atomic_state *old_state)
9831{
1cd593e0
VS
9832 struct drm_i915_private *dev_priv = to_i915(old_state->dev);
9833 struct intel_atomic_state *intel_state = to_intel_atomic_state(old_state);
9834 unsigned int req_cdclk = intel_state->dev_cdclk;
9835 unsigned int req_vco = intel_state->cdclk_pll_vco;
c89e39f3 9836
1cd593e0 9837 skl_set_cdclk(dev_priv, req_cdclk, req_vco);
c89e39f3
CT
9838}
9839
190f68c5
ACO
9840static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
9841 struct intel_crtc_state *crtc_state)
09b4ddf9 9842{
d7edc4e5 9843 if (!intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DSI)) {
af3997b5
MK
9844 if (!intel_ddi_pll_select(crtc, crtc_state))
9845 return -EINVAL;
9846 }
716c2e55 9847
c7653199 9848 crtc->lowfreq_avail = false;
644cef34 9849
c8f7a0db 9850 return 0;
79e53945
JB
9851}
9852
3760b59c
S
9853static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
9854 enum port port,
9855 struct intel_crtc_state *pipe_config)
9856{
8106ddbd
ACO
9857 enum intel_dpll_id id;
9858
3760b59c
S
9859 switch (port) {
9860 case PORT_A:
9861 pipe_config->ddi_pll_sel = SKL_DPLL0;
08250c4b 9862 id = DPLL_ID_SKL_DPLL0;
3760b59c
S
9863 break;
9864 case PORT_B:
9865 pipe_config->ddi_pll_sel = SKL_DPLL1;
08250c4b 9866 id = DPLL_ID_SKL_DPLL1;
3760b59c
S
9867 break;
9868 case PORT_C:
9869 pipe_config->ddi_pll_sel = SKL_DPLL2;
08250c4b 9870 id = DPLL_ID_SKL_DPLL2;
3760b59c
S
9871 break;
9872 default:
9873 DRM_ERROR("Incorrect port type\n");
8106ddbd 9874 return;
3760b59c 9875 }
8106ddbd
ACO
9876
9877 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
3760b59c
S
9878}
9879
96b7dfb7
S
9880static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
9881 enum port port,
5cec258b 9882 struct intel_crtc_state *pipe_config)
96b7dfb7 9883{
8106ddbd 9884 enum intel_dpll_id id;
a3c988ea 9885 u32 temp;
96b7dfb7
S
9886
9887 temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
9888 pipe_config->ddi_pll_sel = temp >> (port * 3 + 1);
9889
9890 switch (pipe_config->ddi_pll_sel) {
3148ade7 9891 case SKL_DPLL0:
a3c988ea
ACO
9892 id = DPLL_ID_SKL_DPLL0;
9893 break;
96b7dfb7 9894 case SKL_DPLL1:
8106ddbd 9895 id = DPLL_ID_SKL_DPLL1;
96b7dfb7
S
9896 break;
9897 case SKL_DPLL2:
8106ddbd 9898 id = DPLL_ID_SKL_DPLL2;
96b7dfb7
S
9899 break;
9900 case SKL_DPLL3:
8106ddbd 9901 id = DPLL_ID_SKL_DPLL3;
96b7dfb7 9902 break;
8106ddbd
ACO
9903 default:
9904 MISSING_CASE(pipe_config->ddi_pll_sel);
9905 return;
96b7dfb7 9906 }
8106ddbd
ACO
9907
9908 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
96b7dfb7
S
9909}
9910
7d2c8175
DL
9911static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
9912 enum port port,
5cec258b 9913 struct intel_crtc_state *pipe_config)
7d2c8175 9914{
8106ddbd
ACO
9915 enum intel_dpll_id id;
9916
7d2c8175
DL
9917 pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
9918
9919 switch (pipe_config->ddi_pll_sel) {
9920 case PORT_CLK_SEL_WRPLL1:
8106ddbd 9921 id = DPLL_ID_WRPLL1;
7d2c8175
DL
9922 break;
9923 case PORT_CLK_SEL_WRPLL2:
8106ddbd 9924 id = DPLL_ID_WRPLL2;
7d2c8175 9925 break;
00490c22 9926 case PORT_CLK_SEL_SPLL:
8106ddbd 9927 id = DPLL_ID_SPLL;
79bd23da 9928 break;
9d16da65
ACO
9929 case PORT_CLK_SEL_LCPLL_810:
9930 id = DPLL_ID_LCPLL_810;
9931 break;
9932 case PORT_CLK_SEL_LCPLL_1350:
9933 id = DPLL_ID_LCPLL_1350;
9934 break;
9935 case PORT_CLK_SEL_LCPLL_2700:
9936 id = DPLL_ID_LCPLL_2700;
9937 break;
8106ddbd
ACO
9938 default:
9939 MISSING_CASE(pipe_config->ddi_pll_sel);
9940 /* fall through */
9941 case PORT_CLK_SEL_NONE:
8106ddbd 9942 return;
7d2c8175 9943 }
8106ddbd
ACO
9944
9945 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
7d2c8175
DL
9946}
9947
cf30429e
JN
9948static bool hsw_get_transcoder_state(struct intel_crtc *crtc,
9949 struct intel_crtc_state *pipe_config,
9950 unsigned long *power_domain_mask)
9951{
9952 struct drm_device *dev = crtc->base.dev;
fac5e23e 9953 struct drm_i915_private *dev_priv = to_i915(dev);
cf30429e
JN
9954 enum intel_display_power_domain power_domain;
9955 u32 tmp;
9956
d9a7bc67
ID
9957 /*
9958 * The pipe->transcoder mapping is fixed with the exception of the eDP
9959 * transcoder handled below.
9960 */
cf30429e
JN
9961 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
9962
9963 /*
9964 * XXX: Do intel_display_power_get_if_enabled before reading this (for
9965 * consistency and less surprising code; it's in always on power).
9966 */
9967 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
9968 if (tmp & TRANS_DDI_FUNC_ENABLE) {
9969 enum pipe trans_edp_pipe;
9970 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
9971 default:
9972 WARN(1, "unknown pipe linked to edp transcoder\n");
9973 case TRANS_DDI_EDP_INPUT_A_ONOFF:
9974 case TRANS_DDI_EDP_INPUT_A_ON:
9975 trans_edp_pipe = PIPE_A;
9976 break;
9977 case TRANS_DDI_EDP_INPUT_B_ONOFF:
9978 trans_edp_pipe = PIPE_B;
9979 break;
9980 case TRANS_DDI_EDP_INPUT_C_ONOFF:
9981 trans_edp_pipe = PIPE_C;
9982 break;
9983 }
9984
9985 if (trans_edp_pipe == crtc->pipe)
9986 pipe_config->cpu_transcoder = TRANSCODER_EDP;
9987 }
9988
9989 power_domain = POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder);
9990 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
9991 return false;
9992 *power_domain_mask |= BIT(power_domain);
9993
9994 tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
9995
9996 return tmp & PIPECONF_ENABLE;
9997}
9998
4d1de975
JN
9999static bool bxt_get_dsi_transcoder_state(struct intel_crtc *crtc,
10000 struct intel_crtc_state *pipe_config,
10001 unsigned long *power_domain_mask)
10002{
10003 struct drm_device *dev = crtc->base.dev;
fac5e23e 10004 struct drm_i915_private *dev_priv = to_i915(dev);
4d1de975
JN
10005 enum intel_display_power_domain power_domain;
10006 enum port port;
10007 enum transcoder cpu_transcoder;
10008 u32 tmp;
10009
4d1de975
JN
10010 for_each_port_masked(port, BIT(PORT_A) | BIT(PORT_C)) {
10011 if (port == PORT_A)
10012 cpu_transcoder = TRANSCODER_DSI_A;
10013 else
10014 cpu_transcoder = TRANSCODER_DSI_C;
10015
10016 power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
10017 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
10018 continue;
10019 *power_domain_mask |= BIT(power_domain);
10020
db18b6a6
ID
10021 /*
10022 * The PLL needs to be enabled with a valid divider
10023 * configuration, otherwise accessing DSI registers will hang
10024 * the machine. See BSpec North Display Engine
10025 * registers/MIPI[BXT]. We can break out here early, since we
10026 * need the same DSI PLL to be enabled for both DSI ports.
10027 */
10028 if (!intel_dsi_pll_is_enabled(dev_priv))
10029 break;
10030
4d1de975
JN
10031 /* XXX: this works for video mode only */
10032 tmp = I915_READ(BXT_MIPI_PORT_CTRL(port));
10033 if (!(tmp & DPI_ENABLE))
10034 continue;
10035
10036 tmp = I915_READ(MIPI_CTRL(port));
10037 if ((tmp & BXT_PIPE_SELECT_MASK) != BXT_PIPE_SELECT(crtc->pipe))
10038 continue;
10039
10040 pipe_config->cpu_transcoder = cpu_transcoder;
4d1de975
JN
10041 break;
10042 }
10043
d7edc4e5 10044 return transcoder_is_dsi(pipe_config->cpu_transcoder);
4d1de975
JN
10045}
10046
26804afd 10047static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
5cec258b 10048 struct intel_crtc_state *pipe_config)
26804afd
DV
10049{
10050 struct drm_device *dev = crtc->base.dev;
fac5e23e 10051 struct drm_i915_private *dev_priv = to_i915(dev);
d452c5b6 10052 struct intel_shared_dpll *pll;
26804afd
DV
10053 enum port port;
10054 uint32_t tmp;
10055
10056 tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
10057
10058 port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
10059
ef11bdb3 10060 if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev))
96b7dfb7 10061 skylake_get_ddi_pll(dev_priv, port, pipe_config);
3760b59c
S
10062 else if (IS_BROXTON(dev))
10063 bxt_get_ddi_pll(dev_priv, port, pipe_config);
96b7dfb7
S
10064 else
10065 haswell_get_ddi_pll(dev_priv, port, pipe_config);
9cd86933 10066
8106ddbd
ACO
10067 pll = pipe_config->shared_dpll;
10068 if (pll) {
2edd6443
ACO
10069 WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
10070 &pipe_config->dpll_hw_state));
d452c5b6
DV
10071 }
10072
26804afd
DV
10073 /*
10074 * Haswell has only FDI/PCH transcoder A. It is which is connected to
10075 * DDI E. So just check whether this pipe is wired to DDI E and whether
10076 * the PCH transcoder is on.
10077 */
ca370455
DL
10078 if (INTEL_INFO(dev)->gen < 9 &&
10079 (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
26804afd
DV
10080 pipe_config->has_pch_encoder = true;
10081
10082 tmp = I915_READ(FDI_RX_CTL(PIPE_A));
10083 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
10084 FDI_DP_PORT_WIDTH_SHIFT) + 1;
10085
10086 ironlake_get_fdi_m_n_config(crtc, pipe_config);
10087 }
10088}
10089
0e8ffe1b 10090static bool haswell_get_pipe_config(struct intel_crtc *crtc,
5cec258b 10091 struct intel_crtc_state *pipe_config)
0e8ffe1b
DV
10092{
10093 struct drm_device *dev = crtc->base.dev;
fac5e23e 10094 struct drm_i915_private *dev_priv = to_i915(dev);
1729050e
ID
10095 enum intel_display_power_domain power_domain;
10096 unsigned long power_domain_mask;
cf30429e 10097 bool active;
0e8ffe1b 10098
1729050e
ID
10099 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
10100 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
b5482bd0 10101 return false;
1729050e
ID
10102 power_domain_mask = BIT(power_domain);
10103
8106ddbd 10104 pipe_config->shared_dpll = NULL;
c0d43d62 10105
cf30429e 10106 active = hsw_get_transcoder_state(crtc, pipe_config, &power_domain_mask);
eccb140b 10107
d7edc4e5
VS
10108 if (IS_BROXTON(dev_priv) &&
10109 bxt_get_dsi_transcoder_state(crtc, pipe_config, &power_domain_mask)) {
10110 WARN_ON(active);
10111 active = true;
4d1de975
JN
10112 }
10113
cf30429e 10114 if (!active)
1729050e 10115 goto out;
0e8ffe1b 10116
d7edc4e5 10117 if (!transcoder_is_dsi(pipe_config->cpu_transcoder)) {
4d1de975
JN
10118 haswell_get_ddi_port_state(crtc, pipe_config);
10119 intel_get_pipe_timings(crtc, pipe_config);
10120 }
627eb5a3 10121
bc58be60 10122 intel_get_pipe_src_size(crtc, pipe_config);
1bd1bd80 10123
05dc698c
LL
10124 pipe_config->gamma_mode =
10125 I915_READ(GAMMA_MODE(crtc->pipe)) & GAMMA_MODE_MODE_MASK;
10126
a1b2278e
CK
10127 if (INTEL_INFO(dev)->gen >= 9) {
10128 skl_init_scalers(dev, crtc, pipe_config);
10129 }
10130
af99ceda
CK
10131 if (INTEL_INFO(dev)->gen >= 9) {
10132 pipe_config->scaler_state.scaler_id = -1;
10133 pipe_config->scaler_state.scaler_users &= ~(1 << SKL_CRTC_INDEX);
10134 }
10135
1729050e
ID
10136 power_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
10137 if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
10138 power_domain_mask |= BIT(power_domain);
1c132b44 10139 if (INTEL_INFO(dev)->gen >= 9)
bd2e244f 10140 skylake_get_pfit_config(crtc, pipe_config);
ff6d9f55 10141 else
1c132b44 10142 ironlake_get_pfit_config(crtc, pipe_config);
bd2e244f 10143 }
88adfff1 10144
e59150dc
JB
10145 if (IS_HASWELL(dev))
10146 pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
10147 (I915_READ(IPS_CTL) & IPS_ENABLE);
42db64ef 10148
4d1de975
JN
10149 if (pipe_config->cpu_transcoder != TRANSCODER_EDP &&
10150 !transcoder_is_dsi(pipe_config->cpu_transcoder)) {
ebb69c95
CT
10151 pipe_config->pixel_multiplier =
10152 I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
10153 } else {
10154 pipe_config->pixel_multiplier = 1;
10155 }
6c49f241 10156
1729050e
ID
10157out:
10158 for_each_power_domain(power_domain, power_domain_mask)
10159 intel_display_power_put(dev_priv, power_domain);
10160
cf30429e 10161 return active;
0e8ffe1b
DV
10162}
10163
55a08b3f
ML
10164static void i845_update_cursor(struct drm_crtc *crtc, u32 base,
10165 const struct intel_plane_state *plane_state)
560b85bb
CW
10166{
10167 struct drm_device *dev = crtc->dev;
fac5e23e 10168 struct drm_i915_private *dev_priv = to_i915(dev);
560b85bb 10169 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
dc41c154 10170 uint32_t cntl = 0, size = 0;
560b85bb 10171
55a08b3f
ML
10172 if (plane_state && plane_state->visible) {
10173 unsigned int width = plane_state->base.crtc_w;
10174 unsigned int height = plane_state->base.crtc_h;
dc41c154
VS
10175 unsigned int stride = roundup_pow_of_two(width) * 4;
10176
10177 switch (stride) {
10178 default:
10179 WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
10180 width, stride);
10181 stride = 256;
10182 /* fallthrough */
10183 case 256:
10184 case 512:
10185 case 1024:
10186 case 2048:
10187 break;
4b0e333e
CW
10188 }
10189
dc41c154
VS
10190 cntl |= CURSOR_ENABLE |
10191 CURSOR_GAMMA_ENABLE |
10192 CURSOR_FORMAT_ARGB |
10193 CURSOR_STRIDE(stride);
10194
10195 size = (height << 12) | width;
4b0e333e 10196 }
560b85bb 10197
dc41c154
VS
10198 if (intel_crtc->cursor_cntl != 0 &&
10199 (intel_crtc->cursor_base != base ||
10200 intel_crtc->cursor_size != size ||
10201 intel_crtc->cursor_cntl != cntl)) {
10202 /* On these chipsets we can only modify the base/size/stride
10203 * whilst the cursor is disabled.
10204 */
0b87c24e
VS
10205 I915_WRITE(CURCNTR(PIPE_A), 0);
10206 POSTING_READ(CURCNTR(PIPE_A));
dc41c154 10207 intel_crtc->cursor_cntl = 0;
4b0e333e 10208 }
560b85bb 10209
99d1f387 10210 if (intel_crtc->cursor_base != base) {
0b87c24e 10211 I915_WRITE(CURBASE(PIPE_A), base);
99d1f387
VS
10212 intel_crtc->cursor_base = base;
10213 }
4726e0b0 10214
dc41c154
VS
10215 if (intel_crtc->cursor_size != size) {
10216 I915_WRITE(CURSIZE, size);
10217 intel_crtc->cursor_size = size;
4b0e333e 10218 }
560b85bb 10219
4b0e333e 10220 if (intel_crtc->cursor_cntl != cntl) {
0b87c24e
VS
10221 I915_WRITE(CURCNTR(PIPE_A), cntl);
10222 POSTING_READ(CURCNTR(PIPE_A));
4b0e333e 10223 intel_crtc->cursor_cntl = cntl;
560b85bb 10224 }
560b85bb
CW
10225}
10226
55a08b3f
ML
10227static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base,
10228 const struct intel_plane_state *plane_state)
65a21cd6
JB
10229{
10230 struct drm_device *dev = crtc->dev;
fac5e23e 10231 struct drm_i915_private *dev_priv = to_i915(dev);
65a21cd6
JB
10232 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10233 int pipe = intel_crtc->pipe;
663f3122 10234 uint32_t cntl = 0;
4b0e333e 10235
55a08b3f 10236 if (plane_state && plane_state->visible) {
4b0e333e 10237 cntl = MCURSOR_GAMMA_ENABLE;
55a08b3f 10238 switch (plane_state->base.crtc_w) {
4726e0b0
SK
10239 case 64:
10240 cntl |= CURSOR_MODE_64_ARGB_AX;
10241 break;
10242 case 128:
10243 cntl |= CURSOR_MODE_128_ARGB_AX;
10244 break;
10245 case 256:
10246 cntl |= CURSOR_MODE_256_ARGB_AX;
10247 break;
10248 default:
55a08b3f 10249 MISSING_CASE(plane_state->base.crtc_w);
4726e0b0 10250 return;
65a21cd6 10251 }
4b0e333e 10252 cntl |= pipe << 28; /* Connect to correct pipe */
47bf17a7 10253
fc6f93bc 10254 if (HAS_DDI(dev))
47bf17a7 10255 cntl |= CURSOR_PIPE_CSC_ENABLE;
65a21cd6 10256
55a08b3f
ML
10257 if (plane_state->base.rotation == BIT(DRM_ROTATE_180))
10258 cntl |= CURSOR_ROTATE_180;
10259 }
4398ad45 10260
4b0e333e
CW
10261 if (intel_crtc->cursor_cntl != cntl) {
10262 I915_WRITE(CURCNTR(pipe), cntl);
10263 POSTING_READ(CURCNTR(pipe));
10264 intel_crtc->cursor_cntl = cntl;
65a21cd6 10265 }
4b0e333e 10266
65a21cd6 10267 /* and commit changes on next vblank */
5efb3e28
VS
10268 I915_WRITE(CURBASE(pipe), base);
10269 POSTING_READ(CURBASE(pipe));
99d1f387
VS
10270
10271 intel_crtc->cursor_base = base;
65a21cd6
JB
10272}
10273
cda4b7d3 10274/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
6b383a7f 10275static void intel_crtc_update_cursor(struct drm_crtc *crtc,
55a08b3f 10276 const struct intel_plane_state *plane_state)
cda4b7d3
CW
10277{
10278 struct drm_device *dev = crtc->dev;
fac5e23e 10279 struct drm_i915_private *dev_priv = to_i915(dev);
cda4b7d3
CW
10280 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10281 int pipe = intel_crtc->pipe;
55a08b3f
ML
10282 u32 base = intel_crtc->cursor_addr;
10283 u32 pos = 0;
cda4b7d3 10284
55a08b3f
ML
10285 if (plane_state) {
10286 int x = plane_state->base.crtc_x;
10287 int y = plane_state->base.crtc_y;
cda4b7d3 10288
55a08b3f
ML
10289 if (x < 0) {
10290 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
10291 x = -x;
10292 }
10293 pos |= x << CURSOR_X_SHIFT;
cda4b7d3 10294
55a08b3f
ML
10295 if (y < 0) {
10296 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
10297 y = -y;
10298 }
10299 pos |= y << CURSOR_Y_SHIFT;
10300
10301 /* ILK+ do this automagically */
10302 if (HAS_GMCH_DISPLAY(dev) &&
10303 plane_state->base.rotation == BIT(DRM_ROTATE_180)) {
10304 base += (plane_state->base.crtc_h *
10305 plane_state->base.crtc_w - 1) * 4;
10306 }
cda4b7d3 10307 }
cda4b7d3 10308
5efb3e28
VS
10309 I915_WRITE(CURPOS(pipe), pos);
10310
8ac54669 10311 if (IS_845G(dev) || IS_I865G(dev))
55a08b3f 10312 i845_update_cursor(crtc, base, plane_state);
5efb3e28 10313 else
55a08b3f 10314 i9xx_update_cursor(crtc, base, plane_state);
cda4b7d3
CW
10315}
10316
dc41c154
VS
10317static bool cursor_size_ok(struct drm_device *dev,
10318 uint32_t width, uint32_t height)
10319{
10320 if (width == 0 || height == 0)
10321 return false;
10322
10323 /*
10324 * 845g/865g are special in that they are only limited by
10325 * the width of their cursors, the height is arbitrary up to
10326 * the precision of the register. Everything else requires
10327 * square cursors, limited to a few power-of-two sizes.
10328 */
10329 if (IS_845G(dev) || IS_I865G(dev)) {
10330 if ((width & 63) != 0)
10331 return false;
10332
10333 if (width > (IS_845G(dev) ? 64 : 512))
10334 return false;
10335
10336 if (height > 1023)
10337 return false;
10338 } else {
10339 switch (width | height) {
10340 case 256:
10341 case 128:
10342 if (IS_GEN2(dev))
10343 return false;
10344 case 64:
10345 break;
10346 default:
10347 return false;
10348 }
10349 }
10350
10351 return true;
10352}
10353
79e53945
JB
10354/* VESA 640x480x72Hz mode to set on the pipe */
10355static struct drm_display_mode load_detect_mode = {
10356 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
10357 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
10358};
10359
a8bb6818
DV
10360struct drm_framebuffer *
10361__intel_framebuffer_create(struct drm_device *dev,
10362 struct drm_mode_fb_cmd2 *mode_cmd,
10363 struct drm_i915_gem_object *obj)
d2dff872
CW
10364{
10365 struct intel_framebuffer *intel_fb;
10366 int ret;
10367
10368 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
dcb1394e 10369 if (!intel_fb)
d2dff872 10370 return ERR_PTR(-ENOMEM);
d2dff872
CW
10371
10372 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
dd4916c5
DV
10373 if (ret)
10374 goto err;
d2dff872
CW
10375
10376 return &intel_fb->base;
dcb1394e 10377
dd4916c5 10378err:
dd4916c5 10379 kfree(intel_fb);
dd4916c5 10380 return ERR_PTR(ret);
d2dff872
CW
10381}
10382
b5ea642a 10383static struct drm_framebuffer *
a8bb6818
DV
10384intel_framebuffer_create(struct drm_device *dev,
10385 struct drm_mode_fb_cmd2 *mode_cmd,
10386 struct drm_i915_gem_object *obj)
10387{
10388 struct drm_framebuffer *fb;
10389 int ret;
10390
10391 ret = i915_mutex_lock_interruptible(dev);
10392 if (ret)
10393 return ERR_PTR(ret);
10394 fb = __intel_framebuffer_create(dev, mode_cmd, obj);
10395 mutex_unlock(&dev->struct_mutex);
10396
10397 return fb;
10398}
10399
d2dff872
CW
10400static u32
10401intel_framebuffer_pitch_for_width(int width, int bpp)
10402{
10403 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
10404 return ALIGN(pitch, 64);
10405}
10406
10407static u32
10408intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
10409{
10410 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
1267a26b 10411 return PAGE_ALIGN(pitch * mode->vdisplay);
d2dff872
CW
10412}
10413
10414static struct drm_framebuffer *
10415intel_framebuffer_create_for_mode(struct drm_device *dev,
10416 struct drm_display_mode *mode,
10417 int depth, int bpp)
10418{
dcb1394e 10419 struct drm_framebuffer *fb;
d2dff872 10420 struct drm_i915_gem_object *obj;
0fed39bd 10421 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
d2dff872 10422
d37cd8a8 10423 obj = i915_gem_object_create(dev,
d2dff872 10424 intel_framebuffer_size_for_mode(mode, bpp));
fe3db79b
CW
10425 if (IS_ERR(obj))
10426 return ERR_CAST(obj);
d2dff872
CW
10427
10428 mode_cmd.width = mode->hdisplay;
10429 mode_cmd.height = mode->vdisplay;
308e5bcb
JB
10430 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
10431 bpp);
5ca0c34a 10432 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
d2dff872 10433
dcb1394e
LW
10434 fb = intel_framebuffer_create(dev, &mode_cmd, obj);
10435 if (IS_ERR(fb))
34911fd3 10436 i915_gem_object_put_unlocked(obj);
dcb1394e
LW
10437
10438 return fb;
d2dff872
CW
10439}
10440
10441static struct drm_framebuffer *
10442mode_fits_in_fbdev(struct drm_device *dev,
10443 struct drm_display_mode *mode)
10444{
0695726e 10445#ifdef CONFIG_DRM_FBDEV_EMULATION
fac5e23e 10446 struct drm_i915_private *dev_priv = to_i915(dev);
d2dff872
CW
10447 struct drm_i915_gem_object *obj;
10448 struct drm_framebuffer *fb;
10449
4c0e5528 10450 if (!dev_priv->fbdev)
d2dff872
CW
10451 return NULL;
10452
4c0e5528 10453 if (!dev_priv->fbdev->fb)
d2dff872
CW
10454 return NULL;
10455
4c0e5528
DV
10456 obj = dev_priv->fbdev->fb->obj;
10457 BUG_ON(!obj);
10458
8bcd4553 10459 fb = &dev_priv->fbdev->fb->base;
01f2c773
VS
10460 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
10461 fb->bits_per_pixel))
d2dff872
CW
10462 return NULL;
10463
01f2c773 10464 if (obj->base.size < mode->vdisplay * fb->pitches[0])
d2dff872
CW
10465 return NULL;
10466
edde3617 10467 drm_framebuffer_reference(fb);
d2dff872 10468 return fb;
4520f53a
DV
10469#else
10470 return NULL;
10471#endif
d2dff872
CW
10472}
10473
d3a40d1b
ACO
10474static int intel_modeset_setup_plane_state(struct drm_atomic_state *state,
10475 struct drm_crtc *crtc,
10476 struct drm_display_mode *mode,
10477 struct drm_framebuffer *fb,
10478 int x, int y)
10479{
10480 struct drm_plane_state *plane_state;
10481 int hdisplay, vdisplay;
10482 int ret;
10483
10484 plane_state = drm_atomic_get_plane_state(state, crtc->primary);
10485 if (IS_ERR(plane_state))
10486 return PTR_ERR(plane_state);
10487
10488 if (mode)
10489 drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
10490 else
10491 hdisplay = vdisplay = 0;
10492
10493 ret = drm_atomic_set_crtc_for_plane(plane_state, fb ? crtc : NULL);
10494 if (ret)
10495 return ret;
10496 drm_atomic_set_fb_for_plane(plane_state, fb);
10497 plane_state->crtc_x = 0;
10498 plane_state->crtc_y = 0;
10499 plane_state->crtc_w = hdisplay;
10500 plane_state->crtc_h = vdisplay;
10501 plane_state->src_x = x << 16;
10502 plane_state->src_y = y << 16;
10503 plane_state->src_w = hdisplay << 16;
10504 plane_state->src_h = vdisplay << 16;
10505
10506 return 0;
10507}
10508
d2434ab7 10509bool intel_get_load_detect_pipe(struct drm_connector *connector,
7173188d 10510 struct drm_display_mode *mode,
51fd371b
RC
10511 struct intel_load_detect_pipe *old,
10512 struct drm_modeset_acquire_ctx *ctx)
79e53945
JB
10513{
10514 struct intel_crtc *intel_crtc;
d2434ab7
DV
10515 struct intel_encoder *intel_encoder =
10516 intel_attached_encoder(connector);
79e53945 10517 struct drm_crtc *possible_crtc;
4ef69c7a 10518 struct drm_encoder *encoder = &intel_encoder->base;
79e53945
JB
10519 struct drm_crtc *crtc = NULL;
10520 struct drm_device *dev = encoder->dev;
94352cf9 10521 struct drm_framebuffer *fb;
51fd371b 10522 struct drm_mode_config *config = &dev->mode_config;
edde3617 10523 struct drm_atomic_state *state = NULL, *restore_state = NULL;
944b0c76 10524 struct drm_connector_state *connector_state;
4be07317 10525 struct intel_crtc_state *crtc_state;
51fd371b 10526 int ret, i = -1;
79e53945 10527
d2dff872 10528 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
c23cc417 10529 connector->base.id, connector->name,
8e329a03 10530 encoder->base.id, encoder->name);
d2dff872 10531
edde3617
ML
10532 old->restore_state = NULL;
10533
51fd371b
RC
10534retry:
10535 ret = drm_modeset_lock(&config->connection_mutex, ctx);
10536 if (ret)
ad3c558f 10537 goto fail;
6e9f798d 10538
79e53945
JB
10539 /*
10540 * Algorithm gets a little messy:
7a5e4805 10541 *
79e53945
JB
10542 * - if the connector already has an assigned crtc, use it (but make
10543 * sure it's on first)
7a5e4805 10544 *
79e53945
JB
10545 * - try to find the first unused crtc that can drive this connector,
10546 * and use that if we find one
79e53945
JB
10547 */
10548
10549 /* See if we already have a CRTC for this connector */
edde3617
ML
10550 if (connector->state->crtc) {
10551 crtc = connector->state->crtc;
8261b191 10552
51fd371b 10553 ret = drm_modeset_lock(&crtc->mutex, ctx);
4d02e2de 10554 if (ret)
ad3c558f 10555 goto fail;
8261b191
CW
10556
10557 /* Make sure the crtc and connector are running */
edde3617 10558 goto found;
79e53945
JB
10559 }
10560
10561 /* Find an unused one (if possible) */
70e1e0ec 10562 for_each_crtc(dev, possible_crtc) {
79e53945
JB
10563 i++;
10564 if (!(encoder->possible_crtcs & (1 << i)))
10565 continue;
edde3617
ML
10566
10567 ret = drm_modeset_lock(&possible_crtc->mutex, ctx);
10568 if (ret)
10569 goto fail;
10570
10571 if (possible_crtc->state->enable) {
10572 drm_modeset_unlock(&possible_crtc->mutex);
a459249c 10573 continue;
edde3617 10574 }
a459249c
VS
10575
10576 crtc = possible_crtc;
10577 break;
79e53945
JB
10578 }
10579
10580 /*
10581 * If we didn't find an unused CRTC, don't use any.
10582 */
10583 if (!crtc) {
7173188d 10584 DRM_DEBUG_KMS("no pipe available for load-detect\n");
ad3c558f 10585 goto fail;
79e53945
JB
10586 }
10587
edde3617
ML
10588found:
10589 intel_crtc = to_intel_crtc(crtc);
10590
4d02e2de
DV
10591 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
10592 if (ret)
ad3c558f 10593 goto fail;
79e53945 10594
83a57153 10595 state = drm_atomic_state_alloc(dev);
edde3617
ML
10596 restore_state = drm_atomic_state_alloc(dev);
10597 if (!state || !restore_state) {
10598 ret = -ENOMEM;
10599 goto fail;
10600 }
83a57153
ACO
10601
10602 state->acquire_ctx = ctx;
edde3617 10603 restore_state->acquire_ctx = ctx;
83a57153 10604
944b0c76
ACO
10605 connector_state = drm_atomic_get_connector_state(state, connector);
10606 if (IS_ERR(connector_state)) {
10607 ret = PTR_ERR(connector_state);
10608 goto fail;
10609 }
10610
edde3617
ML
10611 ret = drm_atomic_set_crtc_for_connector(connector_state, crtc);
10612 if (ret)
10613 goto fail;
944b0c76 10614
4be07317
ACO
10615 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
10616 if (IS_ERR(crtc_state)) {
10617 ret = PTR_ERR(crtc_state);
10618 goto fail;
10619 }
10620
49d6fa21 10621 crtc_state->base.active = crtc_state->base.enable = true;
4be07317 10622
6492711d
CW
10623 if (!mode)
10624 mode = &load_detect_mode;
79e53945 10625
d2dff872
CW
10626 /* We need a framebuffer large enough to accommodate all accesses
10627 * that the plane may generate whilst we perform load detection.
10628 * We can not rely on the fbcon either being present (we get called
10629 * during its initialisation to detect all boot displays, or it may
10630 * not even exist) or that it is large enough to satisfy the
10631 * requested mode.
10632 */
94352cf9
DV
10633 fb = mode_fits_in_fbdev(dev, mode);
10634 if (fb == NULL) {
d2dff872 10635 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
94352cf9 10636 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
d2dff872
CW
10637 } else
10638 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
94352cf9 10639 if (IS_ERR(fb)) {
d2dff872 10640 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
412b61d8 10641 goto fail;
79e53945 10642 }
79e53945 10643
d3a40d1b
ACO
10644 ret = intel_modeset_setup_plane_state(state, crtc, mode, fb, 0, 0);
10645 if (ret)
10646 goto fail;
10647
edde3617
ML
10648 drm_framebuffer_unreference(fb);
10649
10650 ret = drm_atomic_set_mode_for_crtc(&crtc_state->base, mode);
10651 if (ret)
10652 goto fail;
10653
10654 ret = PTR_ERR_OR_ZERO(drm_atomic_get_connector_state(restore_state, connector));
10655 if (!ret)
10656 ret = PTR_ERR_OR_ZERO(drm_atomic_get_crtc_state(restore_state, crtc));
10657 if (!ret)
10658 ret = PTR_ERR_OR_ZERO(drm_atomic_get_plane_state(restore_state, crtc->primary));
10659 if (ret) {
10660 DRM_DEBUG_KMS("Failed to create a copy of old state to restore: %i\n", ret);
10661 goto fail;
10662 }
8c7b5ccb 10663
3ba86073
ML
10664 ret = drm_atomic_commit(state);
10665 if (ret) {
6492711d 10666 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
412b61d8 10667 goto fail;
79e53945 10668 }
edde3617
ML
10669
10670 old->restore_state = restore_state;
7173188d 10671
79e53945 10672 /* let the connector get through one full cycle before testing */
9d0498a2 10673 intel_wait_for_vblank(dev, intel_crtc->pipe);
7173188d 10674 return true;
412b61d8 10675
ad3c558f 10676fail:
e5d958ef 10677 drm_atomic_state_free(state);
edde3617
ML
10678 drm_atomic_state_free(restore_state);
10679 restore_state = state = NULL;
83a57153 10680
51fd371b
RC
10681 if (ret == -EDEADLK) {
10682 drm_modeset_backoff(ctx);
10683 goto retry;
10684 }
10685
412b61d8 10686 return false;
79e53945
JB
10687}
10688
d2434ab7 10689void intel_release_load_detect_pipe(struct drm_connector *connector,
49172fee
ACO
10690 struct intel_load_detect_pipe *old,
10691 struct drm_modeset_acquire_ctx *ctx)
79e53945 10692{
d2434ab7
DV
10693 struct intel_encoder *intel_encoder =
10694 intel_attached_encoder(connector);
4ef69c7a 10695 struct drm_encoder *encoder = &intel_encoder->base;
edde3617 10696 struct drm_atomic_state *state = old->restore_state;
d3a40d1b 10697 int ret;
79e53945 10698
d2dff872 10699 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
c23cc417 10700 connector->base.id, connector->name,
8e329a03 10701 encoder->base.id, encoder->name);
d2dff872 10702
edde3617 10703 if (!state)
0622a53c 10704 return;
79e53945 10705
edde3617
ML
10706 ret = drm_atomic_commit(state);
10707 if (ret) {
10708 DRM_DEBUG_KMS("Couldn't release load detect pipe: %i\n", ret);
10709 drm_atomic_state_free(state);
10710 }
79e53945
JB
10711}
10712
da4a1efa 10713static int i9xx_pll_refclk(struct drm_device *dev,
5cec258b 10714 const struct intel_crtc_state *pipe_config)
da4a1efa 10715{
fac5e23e 10716 struct drm_i915_private *dev_priv = to_i915(dev);
da4a1efa
VS
10717 u32 dpll = pipe_config->dpll_hw_state.dpll;
10718
10719 if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
e91e941b 10720 return dev_priv->vbt.lvds_ssc_freq;
da4a1efa
VS
10721 else if (HAS_PCH_SPLIT(dev))
10722 return 120000;
10723 else if (!IS_GEN2(dev))
10724 return 96000;
10725 else
10726 return 48000;
10727}
10728
79e53945 10729/* Returns the clock of the currently programmed mode of the given pipe. */
f1f644dc 10730static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 10731 struct intel_crtc_state *pipe_config)
79e53945 10732{
f1f644dc 10733 struct drm_device *dev = crtc->base.dev;
fac5e23e 10734 struct drm_i915_private *dev_priv = to_i915(dev);
f1f644dc 10735 int pipe = pipe_config->cpu_transcoder;
293623f7 10736 u32 dpll = pipe_config->dpll_hw_state.dpll;
79e53945 10737 u32 fp;
9e2c8475 10738 struct dpll clock;
dccbea3b 10739 int port_clock;
da4a1efa 10740 int refclk = i9xx_pll_refclk(dev, pipe_config);
79e53945
JB
10741
10742 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
293623f7 10743 fp = pipe_config->dpll_hw_state.fp0;
79e53945 10744 else
293623f7 10745 fp = pipe_config->dpll_hw_state.fp1;
79e53945
JB
10746
10747 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
f2b115e6
AJ
10748 if (IS_PINEVIEW(dev)) {
10749 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
10750 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
2177832f
SL
10751 } else {
10752 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
10753 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
10754 }
10755
a6c45cf0 10756 if (!IS_GEN2(dev)) {
f2b115e6
AJ
10757 if (IS_PINEVIEW(dev))
10758 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
10759 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
2177832f
SL
10760 else
10761 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
79e53945
JB
10762 DPLL_FPA01_P1_POST_DIV_SHIFT);
10763
10764 switch (dpll & DPLL_MODE_MASK) {
10765 case DPLLB_MODE_DAC_SERIAL:
10766 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
10767 5 : 10;
10768 break;
10769 case DPLLB_MODE_LVDS:
10770 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
10771 7 : 14;
10772 break;
10773 default:
28c97730 10774 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
79e53945 10775 "mode\n", (int)(dpll & DPLL_MODE_MASK));
f1f644dc 10776 return;
79e53945
JB
10777 }
10778
ac58c3f0 10779 if (IS_PINEVIEW(dev))
dccbea3b 10780 port_clock = pnv_calc_dpll_params(refclk, &clock);
ac58c3f0 10781 else
dccbea3b 10782 port_clock = i9xx_calc_dpll_params(refclk, &clock);
79e53945 10783 } else {
0fb58223 10784 u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
b1c560d1 10785 bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
79e53945
JB
10786
10787 if (is_lvds) {
10788 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
10789 DPLL_FPA01_P1_POST_DIV_SHIFT);
b1c560d1
VS
10790
10791 if (lvds & LVDS_CLKB_POWER_UP)
10792 clock.p2 = 7;
10793 else
10794 clock.p2 = 14;
79e53945
JB
10795 } else {
10796 if (dpll & PLL_P1_DIVIDE_BY_TWO)
10797 clock.p1 = 2;
10798 else {
10799 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
10800 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
10801 }
10802 if (dpll & PLL_P2_DIVIDE_BY_4)
10803 clock.p2 = 4;
10804 else
10805 clock.p2 = 2;
79e53945 10806 }
da4a1efa 10807
dccbea3b 10808 port_clock = i9xx_calc_dpll_params(refclk, &clock);
79e53945
JB
10809 }
10810
18442d08
VS
10811 /*
10812 * This value includes pixel_multiplier. We will use
241bfc38 10813 * port_clock to compute adjusted_mode.crtc_clock in the
18442d08
VS
10814 * encoder's get_config() function.
10815 */
dccbea3b 10816 pipe_config->port_clock = port_clock;
f1f644dc
JB
10817}
10818
6878da05
VS
10819int intel_dotclock_calculate(int link_freq,
10820 const struct intel_link_m_n *m_n)
f1f644dc 10821{
f1f644dc
JB
10822 /*
10823 * The calculation for the data clock is:
1041a02f 10824 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
f1f644dc 10825 * But we want to avoid losing precison if possible, so:
1041a02f 10826 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
f1f644dc
JB
10827 *
10828 * and the link clock is simpler:
1041a02f 10829 * link_clock = (m * link_clock) / n
f1f644dc
JB
10830 */
10831
6878da05
VS
10832 if (!m_n->link_n)
10833 return 0;
f1f644dc 10834
6878da05
VS
10835 return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
10836}
f1f644dc 10837
18442d08 10838static void ironlake_pch_clock_get(struct intel_crtc *crtc,
5cec258b 10839 struct intel_crtc_state *pipe_config)
6878da05 10840{
e3b247da 10841 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
79e53945 10842
18442d08
VS
10843 /* read out port_clock from the DPLL */
10844 i9xx_crtc_clock_get(crtc, pipe_config);
f1f644dc 10845
f1f644dc 10846 /*
e3b247da
VS
10847 * In case there is an active pipe without active ports,
10848 * we may need some idea for the dotclock anyway.
10849 * Calculate one based on the FDI configuration.
79e53945 10850 */
2d112de7 10851 pipe_config->base.adjusted_mode.crtc_clock =
21a727b3 10852 intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
18442d08 10853 &pipe_config->fdi_m_n);
79e53945
JB
10854}
10855
10856/** Returns the currently programmed mode of the given pipe. */
10857struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
10858 struct drm_crtc *crtc)
10859{
fac5e23e 10860 struct drm_i915_private *dev_priv = to_i915(dev);
79e53945 10861 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6e3c9717 10862 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
79e53945 10863 struct drm_display_mode *mode;
3f36b937 10864 struct intel_crtc_state *pipe_config;
fe2b8f9d
PZ
10865 int htot = I915_READ(HTOTAL(cpu_transcoder));
10866 int hsync = I915_READ(HSYNC(cpu_transcoder));
10867 int vtot = I915_READ(VTOTAL(cpu_transcoder));
10868 int vsync = I915_READ(VSYNC(cpu_transcoder));
293623f7 10869 enum pipe pipe = intel_crtc->pipe;
79e53945
JB
10870
10871 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
10872 if (!mode)
10873 return NULL;
10874
3f36b937
TU
10875 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
10876 if (!pipe_config) {
10877 kfree(mode);
10878 return NULL;
10879 }
10880
f1f644dc
JB
10881 /*
10882 * Construct a pipe_config sufficient for getting the clock info
10883 * back out of crtc_clock_get.
10884 *
10885 * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
10886 * to use a real value here instead.
10887 */
3f36b937
TU
10888 pipe_config->cpu_transcoder = (enum transcoder) pipe;
10889 pipe_config->pixel_multiplier = 1;
10890 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(pipe));
10891 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(pipe));
10892 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(pipe));
10893 i9xx_crtc_clock_get(intel_crtc, pipe_config);
10894
10895 mode->clock = pipe_config->port_clock / pipe_config->pixel_multiplier;
79e53945
JB
10896 mode->hdisplay = (htot & 0xffff) + 1;
10897 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
10898 mode->hsync_start = (hsync & 0xffff) + 1;
10899 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
10900 mode->vdisplay = (vtot & 0xffff) + 1;
10901 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
10902 mode->vsync_start = (vsync & 0xffff) + 1;
10903 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
10904
10905 drm_mode_set_name(mode);
79e53945 10906
3f36b937
TU
10907 kfree(pipe_config);
10908
79e53945
JB
10909 return mode;
10910}
10911
10912static void intel_crtc_destroy(struct drm_crtc *crtc)
10913{
10914 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
67e77c5a 10915 struct drm_device *dev = crtc->dev;
51cbaf01 10916 struct intel_flip_work *work;
67e77c5a 10917
5e2d7afc 10918 spin_lock_irq(&dev->event_lock);
5a21b665
DV
10919 work = intel_crtc->flip_work;
10920 intel_crtc->flip_work = NULL;
10921 spin_unlock_irq(&dev->event_lock);
67e77c5a 10922
5a21b665 10923 if (work) {
51cbaf01
ML
10924 cancel_work_sync(&work->mmio_work);
10925 cancel_work_sync(&work->unpin_work);
5a21b665 10926 kfree(work);
67e77c5a 10927 }
79e53945
JB
10928
10929 drm_crtc_cleanup(crtc);
67e77c5a 10930
79e53945
JB
10931 kfree(intel_crtc);
10932}
10933
6b95a207
KH
10934static void intel_unpin_work_fn(struct work_struct *__work)
10935{
51cbaf01
ML
10936 struct intel_flip_work *work =
10937 container_of(__work, struct intel_flip_work, unpin_work);
5a21b665
DV
10938 struct intel_crtc *crtc = to_intel_crtc(work->crtc);
10939 struct drm_device *dev = crtc->base.dev;
10940 struct drm_plane *primary = crtc->base.primary;
03f476e1 10941
5a21b665
DV
10942 if (is_mmio_work(work))
10943 flush_work(&work->mmio_work);
03f476e1 10944
5a21b665
DV
10945 mutex_lock(&dev->struct_mutex);
10946 intel_unpin_fb_obj(work->old_fb, primary->state->rotation);
f8c417cd 10947 i915_gem_object_put(work->pending_flip_obj);
5a21b665 10948 mutex_unlock(&dev->struct_mutex);
143f73b3 10949
e8a261ea
CW
10950 i915_gem_request_put(work->flip_queued_req);
10951
5748b6a1
CW
10952 intel_frontbuffer_flip_complete(to_i915(dev),
10953 to_intel_plane(primary)->frontbuffer_bit);
5a21b665
DV
10954 intel_fbc_post_update(crtc);
10955 drm_framebuffer_unreference(work->old_fb);
143f73b3 10956
5a21b665
DV
10957 BUG_ON(atomic_read(&crtc->unpin_work_count) == 0);
10958 atomic_dec(&crtc->unpin_work_count);
a6747b73 10959
5a21b665
DV
10960 kfree(work);
10961}
d9e86c0e 10962
5a21b665
DV
10963/* Is 'a' after or equal to 'b'? */
10964static bool g4x_flip_count_after_eq(u32 a, u32 b)
10965{
10966 return !((a - b) & 0x80000000);
10967}
143f73b3 10968
5a21b665
DV
10969static bool __pageflip_finished_cs(struct intel_crtc *crtc,
10970 struct intel_flip_work *work)
10971{
10972 struct drm_device *dev = crtc->base.dev;
fac5e23e 10973 struct drm_i915_private *dev_priv = to_i915(dev);
5a21b665 10974 unsigned reset_counter;
143f73b3 10975
5a21b665
DV
10976 reset_counter = i915_reset_counter(&dev_priv->gpu_error);
10977 if (crtc->reset_counter != reset_counter)
10978 return true;
143f73b3 10979
5a21b665
DV
10980 /*
10981 * The relevant registers doen't exist on pre-ctg.
10982 * As the flip done interrupt doesn't trigger for mmio
10983 * flips on gmch platforms, a flip count check isn't
10984 * really needed there. But since ctg has the registers,
10985 * include it in the check anyway.
10986 */
10987 if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
10988 return true;
b4a98e57 10989
5a21b665
DV
10990 /*
10991 * BDW signals flip done immediately if the plane
10992 * is disabled, even if the plane enable is already
10993 * armed to occur at the next vblank :(
10994 */
f99d7069 10995
5a21b665
DV
10996 /*
10997 * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
10998 * used the same base address. In that case the mmio flip might
10999 * have completed, but the CS hasn't even executed the flip yet.
11000 *
11001 * A flip count check isn't enough as the CS might have updated
11002 * the base address just after start of vblank, but before we
11003 * managed to process the interrupt. This means we'd complete the
11004 * CS flip too soon.
11005 *
11006 * Combining both checks should get us a good enough result. It may
11007 * still happen that the CS flip has been executed, but has not
11008 * yet actually completed. But in case the base address is the same
11009 * anyway, we don't really care.
11010 */
11011 return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
11012 crtc->flip_work->gtt_offset &&
11013 g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_G4X(crtc->pipe)),
11014 crtc->flip_work->flip_count);
11015}
b4a98e57 11016
5a21b665
DV
11017static bool
11018__pageflip_finished_mmio(struct intel_crtc *crtc,
11019 struct intel_flip_work *work)
11020{
11021 /*
11022 * MMIO work completes when vblank is different from
11023 * flip_queued_vblank.
11024 *
11025 * Reset counter value doesn't matter, this is handled by
11026 * i915_wait_request finishing early, so no need to handle
11027 * reset here.
11028 */
11029 return intel_crtc_get_vblank_counter(crtc) != work->flip_queued_vblank;
6b95a207
KH
11030}
11031
51cbaf01
ML
11032
11033static bool pageflip_finished(struct intel_crtc *crtc,
11034 struct intel_flip_work *work)
11035{
11036 if (!atomic_read(&work->pending))
11037 return false;
11038
11039 smp_rmb();
11040
5a21b665
DV
11041 if (is_mmio_work(work))
11042 return __pageflip_finished_mmio(crtc, work);
11043 else
11044 return __pageflip_finished_cs(crtc, work);
11045}
11046
11047void intel_finish_page_flip_cs(struct drm_i915_private *dev_priv, int pipe)
11048{
91c8a326 11049 struct drm_device *dev = &dev_priv->drm;
5a21b665
DV
11050 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
11051 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11052 struct intel_flip_work *work;
11053 unsigned long flags;
11054
11055 /* Ignore early vblank irqs */
11056 if (!crtc)
11057 return;
11058
51cbaf01 11059 /*
5a21b665
DV
11060 * This is called both by irq handlers and the reset code (to complete
11061 * lost pageflips) so needs the full irqsave spinlocks.
51cbaf01 11062 */
5a21b665
DV
11063 spin_lock_irqsave(&dev->event_lock, flags);
11064 work = intel_crtc->flip_work;
11065
11066 if (work != NULL &&
11067 !is_mmio_work(work) &&
11068 pageflip_finished(intel_crtc, work))
11069 page_flip_completed(intel_crtc);
11070
11071 spin_unlock_irqrestore(&dev->event_lock, flags);
75f7f3ec
VS
11072}
11073
51cbaf01 11074void intel_finish_page_flip_mmio(struct drm_i915_private *dev_priv, int pipe)
6b95a207 11075{
91c8a326 11076 struct drm_device *dev = &dev_priv->drm;
5251f04e
ML
11077 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
11078 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
51cbaf01 11079 struct intel_flip_work *work;
6b95a207
KH
11080 unsigned long flags;
11081
5251f04e
ML
11082 /* Ignore early vblank irqs */
11083 if (!crtc)
11084 return;
f326038a
DV
11085
11086 /*
11087 * This is called both by irq handlers and the reset code (to complete
11088 * lost pageflips) so needs the full irqsave spinlocks.
e7d841ca 11089 */
6b95a207 11090 spin_lock_irqsave(&dev->event_lock, flags);
5a21b665 11091 work = intel_crtc->flip_work;
5251f04e 11092
5a21b665
DV
11093 if (work != NULL &&
11094 is_mmio_work(work) &&
11095 pageflip_finished(intel_crtc, work))
11096 page_flip_completed(intel_crtc);
5251f04e 11097
6b95a207
KH
11098 spin_unlock_irqrestore(&dev->event_lock, flags);
11099}
11100
5a21b665
DV
11101static inline void intel_mark_page_flip_active(struct intel_crtc *crtc,
11102 struct intel_flip_work *work)
84c33a64 11103{
5a21b665 11104 work->flip_queued_vblank = intel_crtc_get_vblank_counter(crtc);
84c33a64 11105
5a21b665
DV
11106 /* Ensure that the work item is consistent when activating it ... */
11107 smp_mb__before_atomic();
11108 atomic_set(&work->pending, 1);
11109}
a6747b73 11110
5a21b665
DV
11111static int intel_gen2_queue_flip(struct drm_device *dev,
11112 struct drm_crtc *crtc,
11113 struct drm_framebuffer *fb,
11114 struct drm_i915_gem_object *obj,
11115 struct drm_i915_gem_request *req,
11116 uint32_t flags)
11117{
7e37f889 11118 struct intel_ring *ring = req->ring;
5a21b665
DV
11119 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11120 u32 flip_mask;
11121 int ret;
143f73b3 11122
5a21b665
DV
11123 ret = intel_ring_begin(req, 6);
11124 if (ret)
11125 return ret;
143f73b3 11126
5a21b665
DV
11127 /* Can't queue multiple flips, so wait for the previous
11128 * one to finish before executing the next.
11129 */
11130 if (intel_crtc->plane)
11131 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
11132 else
11133 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
b5321f30
CW
11134 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
11135 intel_ring_emit(ring, MI_NOOP);
11136 intel_ring_emit(ring, MI_DISPLAY_FLIP |
5a21b665 11137 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
b5321f30
CW
11138 intel_ring_emit(ring, fb->pitches[0]);
11139 intel_ring_emit(ring, intel_crtc->flip_work->gtt_offset);
11140 intel_ring_emit(ring, 0); /* aux display base address, unused */
143f73b3 11141
5a21b665
DV
11142 return 0;
11143}
84c33a64 11144
5a21b665
DV
11145static int intel_gen3_queue_flip(struct drm_device *dev,
11146 struct drm_crtc *crtc,
11147 struct drm_framebuffer *fb,
11148 struct drm_i915_gem_object *obj,
11149 struct drm_i915_gem_request *req,
11150 uint32_t flags)
11151{
7e37f889 11152 struct intel_ring *ring = req->ring;
5a21b665
DV
11153 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11154 u32 flip_mask;
11155 int ret;
d55dbd06 11156
5a21b665
DV
11157 ret = intel_ring_begin(req, 6);
11158 if (ret)
11159 return ret;
d55dbd06 11160
5a21b665
DV
11161 if (intel_crtc->plane)
11162 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
11163 else
11164 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
b5321f30
CW
11165 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
11166 intel_ring_emit(ring, MI_NOOP);
11167 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
5a21b665 11168 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
b5321f30
CW
11169 intel_ring_emit(ring, fb->pitches[0]);
11170 intel_ring_emit(ring, intel_crtc->flip_work->gtt_offset);
11171 intel_ring_emit(ring, MI_NOOP);
fd8e058a 11172
5a21b665
DV
11173 return 0;
11174}
84c33a64 11175
5a21b665
DV
11176static int intel_gen4_queue_flip(struct drm_device *dev,
11177 struct drm_crtc *crtc,
11178 struct drm_framebuffer *fb,
11179 struct drm_i915_gem_object *obj,
11180 struct drm_i915_gem_request *req,
11181 uint32_t flags)
11182{
7e37f889 11183 struct intel_ring *ring = req->ring;
fac5e23e 11184 struct drm_i915_private *dev_priv = to_i915(dev);
5a21b665
DV
11185 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11186 uint32_t pf, pipesrc;
11187 int ret;
143f73b3 11188
5a21b665
DV
11189 ret = intel_ring_begin(req, 4);
11190 if (ret)
11191 return ret;
143f73b3 11192
5a21b665
DV
11193 /* i965+ uses the linear or tiled offsets from the
11194 * Display Registers (which do not change across a page-flip)
11195 * so we need only reprogram the base address.
11196 */
b5321f30 11197 intel_ring_emit(ring, MI_DISPLAY_FLIP |
5a21b665 11198 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
b5321f30
CW
11199 intel_ring_emit(ring, fb->pitches[0]);
11200 intel_ring_emit(ring, intel_crtc->flip_work->gtt_offset |
3e510a8e 11201 i915_gem_object_get_tiling(obj));
5a21b665
DV
11202
11203 /* XXX Enabling the panel-fitter across page-flip is so far
11204 * untested on non-native modes, so ignore it for now.
11205 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
11206 */
11207 pf = 0;
11208 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
b5321f30 11209 intel_ring_emit(ring, pf | pipesrc);
143f73b3 11210
5a21b665 11211 return 0;
8c9f3aaf
JB
11212}
11213
5a21b665
DV
11214static int intel_gen6_queue_flip(struct drm_device *dev,
11215 struct drm_crtc *crtc,
11216 struct drm_framebuffer *fb,
11217 struct drm_i915_gem_object *obj,
11218 struct drm_i915_gem_request *req,
11219 uint32_t flags)
da20eabd 11220{
7e37f889 11221 struct intel_ring *ring = req->ring;
fac5e23e 11222 struct drm_i915_private *dev_priv = to_i915(dev);
5a21b665
DV
11223 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11224 uint32_t pf, pipesrc;
11225 int ret;
d21fbe87 11226
5a21b665
DV
11227 ret = intel_ring_begin(req, 4);
11228 if (ret)
11229 return ret;
92826fcd 11230
b5321f30 11231 intel_ring_emit(ring, MI_DISPLAY_FLIP |
5a21b665 11232 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
3e510a8e 11233 intel_ring_emit(ring, fb->pitches[0] | i915_gem_object_get_tiling(obj));
b5321f30 11234 intel_ring_emit(ring, intel_crtc->flip_work->gtt_offset);
92826fcd 11235
5a21b665
DV
11236 /* Contrary to the suggestions in the documentation,
11237 * "Enable Panel Fitter" does not seem to be required when page
11238 * flipping with a non-native mode, and worse causes a normal
11239 * modeset to fail.
11240 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
11241 */
11242 pf = 0;
11243 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
b5321f30 11244 intel_ring_emit(ring, pf | pipesrc);
7809e5ae 11245
5a21b665 11246 return 0;
7809e5ae
MR
11247}
11248
5a21b665
DV
11249static int intel_gen7_queue_flip(struct drm_device *dev,
11250 struct drm_crtc *crtc,
11251 struct drm_framebuffer *fb,
11252 struct drm_i915_gem_object *obj,
11253 struct drm_i915_gem_request *req,
11254 uint32_t flags)
d21fbe87 11255{
7e37f889 11256 struct intel_ring *ring = req->ring;
5a21b665
DV
11257 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11258 uint32_t plane_bit = 0;
11259 int len, ret;
d21fbe87 11260
5a21b665
DV
11261 switch (intel_crtc->plane) {
11262 case PLANE_A:
11263 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
11264 break;
11265 case PLANE_B:
11266 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
11267 break;
11268 case PLANE_C:
11269 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
11270 break;
11271 default:
11272 WARN_ONCE(1, "unknown plane in flip command\n");
11273 return -ENODEV;
11274 }
11275
11276 len = 4;
b5321f30 11277 if (req->engine->id == RCS) {
5a21b665
DV
11278 len += 6;
11279 /*
11280 * On Gen 8, SRM is now taking an extra dword to accommodate
11281 * 48bits addresses, and we need a NOOP for the batch size to
11282 * stay even.
11283 */
11284 if (IS_GEN8(dev))
11285 len += 2;
11286 }
11287
11288 /*
11289 * BSpec MI_DISPLAY_FLIP for IVB:
11290 * "The full packet must be contained within the same cache line."
11291 *
11292 * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
11293 * cacheline, if we ever start emitting more commands before
11294 * the MI_DISPLAY_FLIP we may need to first emit everything else,
11295 * then do the cacheline alignment, and finally emit the
11296 * MI_DISPLAY_FLIP.
11297 */
11298 ret = intel_ring_cacheline_align(req);
11299 if (ret)
11300 return ret;
11301
11302 ret = intel_ring_begin(req, len);
11303 if (ret)
11304 return ret;
11305
11306 /* Unmask the flip-done completion message. Note that the bspec says that
11307 * we should do this for both the BCS and RCS, and that we must not unmask
11308 * more than one flip event at any time (or ensure that one flip message
11309 * can be sent by waiting for flip-done prior to queueing new flips).
11310 * Experimentation says that BCS works despite DERRMR masking all
11311 * flip-done completion events and that unmasking all planes at once
11312 * for the RCS also doesn't appear to drop events. Setting the DERRMR
11313 * to zero does lead to lockups within MI_DISPLAY_FLIP.
11314 */
b5321f30
CW
11315 if (req->engine->id == RCS) {
11316 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
11317 intel_ring_emit_reg(ring, DERRMR);
11318 intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
5a21b665
DV
11319 DERRMR_PIPEB_PRI_FLIP_DONE |
11320 DERRMR_PIPEC_PRI_FLIP_DONE));
11321 if (IS_GEN8(dev))
b5321f30 11322 intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8 |
5a21b665
DV
11323 MI_SRM_LRM_GLOBAL_GTT);
11324 else
b5321f30 11325 intel_ring_emit(ring, MI_STORE_REGISTER_MEM |
5a21b665 11326 MI_SRM_LRM_GLOBAL_GTT);
b5321f30
CW
11327 intel_ring_emit_reg(ring, DERRMR);
11328 intel_ring_emit(ring, req->engine->scratch.gtt_offset + 256);
5a21b665 11329 if (IS_GEN8(dev)) {
b5321f30
CW
11330 intel_ring_emit(ring, 0);
11331 intel_ring_emit(ring, MI_NOOP);
5a21b665
DV
11332 }
11333 }
11334
b5321f30 11335 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
3e510a8e 11336 intel_ring_emit(ring, fb->pitches[0] | i915_gem_object_get_tiling(obj));
b5321f30
CW
11337 intel_ring_emit(ring, intel_crtc->flip_work->gtt_offset);
11338 intel_ring_emit(ring, (MI_NOOP));
5a21b665
DV
11339
11340 return 0;
11341}
11342
11343static bool use_mmio_flip(struct intel_engine_cs *engine,
11344 struct drm_i915_gem_object *obj)
11345{
c37efb99
CW
11346 struct reservation_object *resv;
11347
5a21b665
DV
11348 /*
11349 * This is not being used for older platforms, because
11350 * non-availability of flip done interrupt forces us to use
11351 * CS flips. Older platforms derive flip done using some clever
11352 * tricks involving the flip_pending status bits and vblank irqs.
11353 * So using MMIO flips there would disrupt this mechanism.
11354 */
11355
11356 if (engine == NULL)
11357 return true;
11358
11359 if (INTEL_GEN(engine->i915) < 5)
11360 return false;
11361
11362 if (i915.use_mmio_flip < 0)
11363 return false;
11364 else if (i915.use_mmio_flip > 0)
11365 return true;
11366 else if (i915.enable_execlists)
11367 return true;
c37efb99
CW
11368
11369 resv = i915_gem_object_get_dmabuf_resv(obj);
11370 if (resv && !reservation_object_test_signaled_rcu(resv, false))
5a21b665 11371 return true;
c37efb99 11372
d72d908b
CW
11373 return engine != i915_gem_active_get_engine(&obj->last_write,
11374 &obj->base.dev->struct_mutex);
5a21b665
DV
11375}
11376
11377static void skl_do_mmio_flip(struct intel_crtc *intel_crtc,
11378 unsigned int rotation,
11379 struct intel_flip_work *work)
11380{
11381 struct drm_device *dev = intel_crtc->base.dev;
fac5e23e 11382 struct drm_i915_private *dev_priv = to_i915(dev);
5a21b665
DV
11383 struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
11384 const enum pipe pipe = intel_crtc->pipe;
11385 u32 ctl, stride, tile_height;
11386
11387 ctl = I915_READ(PLANE_CTL(pipe, 0));
11388 ctl &= ~PLANE_CTL_TILED_MASK;
11389 switch (fb->modifier[0]) {
11390 case DRM_FORMAT_MOD_NONE:
11391 break;
11392 case I915_FORMAT_MOD_X_TILED:
11393 ctl |= PLANE_CTL_TILED_X;
11394 break;
11395 case I915_FORMAT_MOD_Y_TILED:
11396 ctl |= PLANE_CTL_TILED_Y;
11397 break;
11398 case I915_FORMAT_MOD_Yf_TILED:
11399 ctl |= PLANE_CTL_TILED_YF;
11400 break;
11401 default:
11402 MISSING_CASE(fb->modifier[0]);
11403 }
11404
11405 /*
11406 * The stride is either expressed as a multiple of 64 bytes chunks for
11407 * linear buffers or in number of tiles for tiled buffers.
11408 */
11409 if (intel_rotation_90_or_270(rotation)) {
11410 /* stride = Surface height in tiles */
11411 tile_height = intel_tile_height(dev_priv, fb->modifier[0], 0);
11412 stride = DIV_ROUND_UP(fb->height, tile_height);
11413 } else {
11414 stride = fb->pitches[0] /
11415 intel_fb_stride_alignment(dev_priv, fb->modifier[0],
11416 fb->pixel_format);
11417 }
11418
11419 /*
11420 * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
11421 * PLANE_SURF updates, the update is then guaranteed to be atomic.
11422 */
11423 I915_WRITE(PLANE_CTL(pipe, 0), ctl);
11424 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
11425
11426 I915_WRITE(PLANE_SURF(pipe, 0), work->gtt_offset);
11427 POSTING_READ(PLANE_SURF(pipe, 0));
11428}
11429
11430static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc,
11431 struct intel_flip_work *work)
11432{
11433 struct drm_device *dev = intel_crtc->base.dev;
fac5e23e 11434 struct drm_i915_private *dev_priv = to_i915(dev);
5a21b665
DV
11435 struct intel_framebuffer *intel_fb =
11436 to_intel_framebuffer(intel_crtc->base.primary->fb);
11437 struct drm_i915_gem_object *obj = intel_fb->obj;
11438 i915_reg_t reg = DSPCNTR(intel_crtc->plane);
11439 u32 dspcntr;
11440
11441 dspcntr = I915_READ(reg);
11442
3e510a8e 11443 if (i915_gem_object_is_tiled(obj))
5a21b665
DV
11444 dspcntr |= DISPPLANE_TILED;
11445 else
11446 dspcntr &= ~DISPPLANE_TILED;
11447
11448 I915_WRITE(reg, dspcntr);
11449
11450 I915_WRITE(DSPSURF(intel_crtc->plane), work->gtt_offset);
11451 POSTING_READ(DSPSURF(intel_crtc->plane));
11452}
11453
11454static void intel_mmio_flip_work_func(struct work_struct *w)
11455{
11456 struct intel_flip_work *work =
11457 container_of(w, struct intel_flip_work, mmio_work);
11458 struct intel_crtc *crtc = to_intel_crtc(work->crtc);
11459 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
11460 struct intel_framebuffer *intel_fb =
11461 to_intel_framebuffer(crtc->base.primary->fb);
11462 struct drm_i915_gem_object *obj = intel_fb->obj;
c37efb99 11463 struct reservation_object *resv;
5a21b665
DV
11464
11465 if (work->flip_queued_req)
776f3236
CW
11466 WARN_ON(i915_wait_request(work->flip_queued_req,
11467 false, NULL,
11468 NO_WAITBOOST));
5a21b665
DV
11469
11470 /* For framebuffer backed by dmabuf, wait for fence */
c37efb99
CW
11471 resv = i915_gem_object_get_dmabuf_resv(obj);
11472 if (resv)
11473 WARN_ON(reservation_object_wait_timeout_rcu(resv, false, false,
5a21b665
DV
11474 MAX_SCHEDULE_TIMEOUT) < 0);
11475
11476 intel_pipe_update_start(crtc);
11477
11478 if (INTEL_GEN(dev_priv) >= 9)
11479 skl_do_mmio_flip(crtc, work->rotation, work);
11480 else
11481 /* use_mmio_flip() retricts MMIO flips to ilk+ */
11482 ilk_do_mmio_flip(crtc, work);
11483
11484 intel_pipe_update_end(crtc, work);
11485}
11486
11487static int intel_default_queue_flip(struct drm_device *dev,
11488 struct drm_crtc *crtc,
11489 struct drm_framebuffer *fb,
11490 struct drm_i915_gem_object *obj,
11491 struct drm_i915_gem_request *req,
11492 uint32_t flags)
11493{
11494 return -ENODEV;
11495}
11496
11497static bool __pageflip_stall_check_cs(struct drm_i915_private *dev_priv,
11498 struct intel_crtc *intel_crtc,
11499 struct intel_flip_work *work)
11500{
11501 u32 addr, vblank;
11502
11503 if (!atomic_read(&work->pending))
11504 return false;
11505
11506 smp_rmb();
11507
11508 vblank = intel_crtc_get_vblank_counter(intel_crtc);
11509 if (work->flip_ready_vblank == 0) {
11510 if (work->flip_queued_req &&
f69a02c9 11511 !i915_gem_request_completed(work->flip_queued_req))
5a21b665
DV
11512 return false;
11513
11514 work->flip_ready_vblank = vblank;
11515 }
11516
11517 if (vblank - work->flip_ready_vblank < 3)
11518 return false;
11519
11520 /* Potential stall - if we see that the flip has happened,
11521 * assume a missed interrupt. */
11522 if (INTEL_GEN(dev_priv) >= 4)
11523 addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
11524 else
11525 addr = I915_READ(DSPADDR(intel_crtc->plane));
11526
11527 /* There is a potential issue here with a false positive after a flip
11528 * to the same address. We could address this by checking for a
11529 * non-incrementing frame counter.
11530 */
11531 return addr == work->gtt_offset;
11532}
11533
11534void intel_check_page_flip(struct drm_i915_private *dev_priv, int pipe)
11535{
91c8a326 11536 struct drm_device *dev = &dev_priv->drm;
5a21b665
DV
11537 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
11538 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11539 struct intel_flip_work *work;
11540
11541 WARN_ON(!in_interrupt());
11542
11543 if (crtc == NULL)
11544 return;
11545
11546 spin_lock(&dev->event_lock);
11547 work = intel_crtc->flip_work;
11548
11549 if (work != NULL && !is_mmio_work(work) &&
11550 __pageflip_stall_check_cs(dev_priv, intel_crtc, work)) {
11551 WARN_ONCE(1,
11552 "Kicking stuck page flip: queued at %d, now %d\n",
11553 work->flip_queued_vblank, intel_crtc_get_vblank_counter(intel_crtc));
11554 page_flip_completed(intel_crtc);
11555 work = NULL;
11556 }
11557
11558 if (work != NULL && !is_mmio_work(work) &&
11559 intel_crtc_get_vblank_counter(intel_crtc) - work->flip_queued_vblank > 1)
11560 intel_queue_rps_boost_for_request(work->flip_queued_req);
11561 spin_unlock(&dev->event_lock);
11562}
11563
11564static int intel_crtc_page_flip(struct drm_crtc *crtc,
11565 struct drm_framebuffer *fb,
11566 struct drm_pending_vblank_event *event,
11567 uint32_t page_flip_flags)
11568{
11569 struct drm_device *dev = crtc->dev;
fac5e23e 11570 struct drm_i915_private *dev_priv = to_i915(dev);
5a21b665
DV
11571 struct drm_framebuffer *old_fb = crtc->primary->fb;
11572 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
11573 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11574 struct drm_plane *primary = crtc->primary;
11575 enum pipe pipe = intel_crtc->pipe;
11576 struct intel_flip_work *work;
11577 struct intel_engine_cs *engine;
11578 bool mmio_flip;
8e637178 11579 struct drm_i915_gem_request *request;
5a21b665
DV
11580 int ret;
11581
11582 /*
11583 * drm_mode_page_flip_ioctl() should already catch this, but double
11584 * check to be safe. In the future we may enable pageflipping from
11585 * a disabled primary plane.
11586 */
11587 if (WARN_ON(intel_fb_obj(old_fb) == NULL))
11588 return -EBUSY;
11589
11590 /* Can't change pixel format via MI display flips. */
11591 if (fb->pixel_format != crtc->primary->fb->pixel_format)
11592 return -EINVAL;
11593
11594 /*
11595 * TILEOFF/LINOFF registers can't be changed via MI display flips.
11596 * Note that pitch changes could also affect these register.
11597 */
11598 if (INTEL_INFO(dev)->gen > 3 &&
11599 (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
11600 fb->pitches[0] != crtc->primary->fb->pitches[0]))
11601 return -EINVAL;
11602
11603 if (i915_terminally_wedged(&dev_priv->gpu_error))
11604 goto out_hang;
11605
11606 work = kzalloc(sizeof(*work), GFP_KERNEL);
11607 if (work == NULL)
11608 return -ENOMEM;
11609
11610 work->event = event;
11611 work->crtc = crtc;
11612 work->old_fb = old_fb;
11613 INIT_WORK(&work->unpin_work, intel_unpin_work_fn);
11614
11615 ret = drm_crtc_vblank_get(crtc);
11616 if (ret)
11617 goto free_work;
11618
11619 /* We borrow the event spin lock for protecting flip_work */
11620 spin_lock_irq(&dev->event_lock);
11621 if (intel_crtc->flip_work) {
11622 /* Before declaring the flip queue wedged, check if
11623 * the hardware completed the operation behind our backs.
11624 */
11625 if (pageflip_finished(intel_crtc, intel_crtc->flip_work)) {
11626 DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
11627 page_flip_completed(intel_crtc);
11628 } else {
11629 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
11630 spin_unlock_irq(&dev->event_lock);
11631
11632 drm_crtc_vblank_put(crtc);
11633 kfree(work);
11634 return -EBUSY;
11635 }
11636 }
11637 intel_crtc->flip_work = work;
11638 spin_unlock_irq(&dev->event_lock);
11639
11640 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
11641 flush_workqueue(dev_priv->wq);
11642
11643 /* Reference the objects for the scheduled work. */
11644 drm_framebuffer_reference(work->old_fb);
5a21b665
DV
11645
11646 crtc->primary->fb = fb;
11647 update_state_fb(crtc->primary);
faf68d92
ML
11648
11649 intel_fbc_pre_update(intel_crtc, intel_crtc->config,
11650 to_intel_plane_state(primary->state));
5a21b665 11651
25dc556a 11652 work->pending_flip_obj = i915_gem_object_get(obj);
5a21b665
DV
11653
11654 ret = i915_mutex_lock_interruptible(dev);
11655 if (ret)
11656 goto cleanup;
11657
11658 intel_crtc->reset_counter = i915_reset_counter(&dev_priv->gpu_error);
11659 if (__i915_reset_in_progress_or_wedged(intel_crtc->reset_counter)) {
11660 ret = -EIO;
11661 goto cleanup;
11662 }
11663
11664 atomic_inc(&intel_crtc->unpin_work_count);
11665
11666 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
11667 work->flip_count = I915_READ(PIPE_FLIPCOUNT_G4X(pipe)) + 1;
11668
11669 if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
11670 engine = &dev_priv->engine[BCS];
3e510a8e
CW
11671 if (i915_gem_object_get_tiling(obj) !=
11672 i915_gem_object_get_tiling(intel_fb_obj(work->old_fb)))
5a21b665
DV
11673 /* vlv: DISPLAY_FLIP fails to change tiling */
11674 engine = NULL;
11675 } else if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
11676 engine = &dev_priv->engine[BCS];
11677 } else if (INTEL_INFO(dev)->gen >= 7) {
d72d908b
CW
11678 engine = i915_gem_active_get_engine(&obj->last_write,
11679 &obj->base.dev->struct_mutex);
5a21b665
DV
11680 if (engine == NULL || engine->id != RCS)
11681 engine = &dev_priv->engine[BCS];
11682 } else {
11683 engine = &dev_priv->engine[RCS];
11684 }
11685
11686 mmio_flip = use_mmio_flip(engine, obj);
11687
5a21b665
DV
11688 ret = intel_pin_and_fence_fb_obj(fb, primary->state->rotation);
11689 if (ret)
11690 goto cleanup_pending;
11691
11692 work->gtt_offset = intel_plane_obj_offset(to_intel_plane(primary),
11693 obj, 0);
11694 work->gtt_offset += intel_crtc->dspaddr_offset;
11695 work->rotation = crtc->primary->state->rotation;
11696
11697 if (mmio_flip) {
11698 INIT_WORK(&work->mmio_work, intel_mmio_flip_work_func);
11699
d72d908b
CW
11700 work->flip_queued_req = i915_gem_active_get(&obj->last_write,
11701 &obj->base.dev->struct_mutex);
5a21b665
DV
11702 schedule_work(&work->mmio_work);
11703 } else {
8e637178
CW
11704 request = i915_gem_request_alloc(engine, engine->last_context);
11705 if (IS_ERR(request)) {
11706 ret = PTR_ERR(request);
11707 goto cleanup_unpin;
11708 }
11709
11710 ret = i915_gem_object_sync(obj, request);
11711 if (ret)
11712 goto cleanup_request;
11713
5a21b665
DV
11714 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, request,
11715 page_flip_flags);
11716 if (ret)
8e637178 11717 goto cleanup_request;
5a21b665
DV
11718
11719 intel_mark_page_flip_active(intel_crtc, work);
11720
8e637178 11721 work->flip_queued_req = i915_gem_request_get(request);
5a21b665
DV
11722 i915_add_request_no_flush(request);
11723 }
11724
11725 i915_gem_track_fb(intel_fb_obj(old_fb), obj,
11726 to_intel_plane(primary)->frontbuffer_bit);
11727 mutex_unlock(&dev->struct_mutex);
11728
5748b6a1 11729 intel_frontbuffer_flip_prepare(to_i915(dev),
5a21b665
DV
11730 to_intel_plane(primary)->frontbuffer_bit);
11731
11732 trace_i915_flip_request(intel_crtc->plane, obj);
11733
11734 return 0;
11735
8e637178
CW
11736cleanup_request:
11737 i915_add_request_no_flush(request);
5a21b665
DV
11738cleanup_unpin:
11739 intel_unpin_fb_obj(fb, crtc->primary->state->rotation);
11740cleanup_pending:
5a21b665
DV
11741 atomic_dec(&intel_crtc->unpin_work_count);
11742 mutex_unlock(&dev->struct_mutex);
11743cleanup:
11744 crtc->primary->fb = old_fb;
11745 update_state_fb(crtc->primary);
11746
34911fd3 11747 i915_gem_object_put_unlocked(obj);
5a21b665
DV
11748 drm_framebuffer_unreference(work->old_fb);
11749
11750 spin_lock_irq(&dev->event_lock);
11751 intel_crtc->flip_work = NULL;
11752 spin_unlock_irq(&dev->event_lock);
11753
11754 drm_crtc_vblank_put(crtc);
11755free_work:
11756 kfree(work);
11757
11758 if (ret == -EIO) {
11759 struct drm_atomic_state *state;
11760 struct drm_plane_state *plane_state;
11761
11762out_hang:
11763 state = drm_atomic_state_alloc(dev);
11764 if (!state)
11765 return -ENOMEM;
11766 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
11767
11768retry:
11769 plane_state = drm_atomic_get_plane_state(state, primary);
11770 ret = PTR_ERR_OR_ZERO(plane_state);
11771 if (!ret) {
11772 drm_atomic_set_fb_for_plane(plane_state, fb);
11773
11774 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
11775 if (!ret)
11776 ret = drm_atomic_commit(state);
11777 }
11778
11779 if (ret == -EDEADLK) {
11780 drm_modeset_backoff(state->acquire_ctx);
11781 drm_atomic_state_clear(state);
11782 goto retry;
11783 }
11784
11785 if (ret)
11786 drm_atomic_state_free(state);
11787
11788 if (ret == 0 && event) {
11789 spin_lock_irq(&dev->event_lock);
11790 drm_crtc_send_vblank_event(crtc, event);
11791 spin_unlock_irq(&dev->event_lock);
11792 }
11793 }
11794 return ret;
11795}
11796
11797
11798/**
11799 * intel_wm_need_update - Check whether watermarks need updating
11800 * @plane: drm plane
11801 * @state: new plane state
11802 *
11803 * Check current plane state versus the new one to determine whether
11804 * watermarks need to be recalculated.
11805 *
11806 * Returns true or false.
11807 */
11808static bool intel_wm_need_update(struct drm_plane *plane,
11809 struct drm_plane_state *state)
11810{
11811 struct intel_plane_state *new = to_intel_plane_state(state);
11812 struct intel_plane_state *cur = to_intel_plane_state(plane->state);
11813
11814 /* Update watermarks on tiling or size changes. */
11815 if (new->visible != cur->visible)
11816 return true;
11817
11818 if (!cur->base.fb || !new->base.fb)
11819 return false;
11820
11821 if (cur->base.fb->modifier[0] != new->base.fb->modifier[0] ||
11822 cur->base.rotation != new->base.rotation ||
11823 drm_rect_width(&new->src) != drm_rect_width(&cur->src) ||
11824 drm_rect_height(&new->src) != drm_rect_height(&cur->src) ||
11825 drm_rect_width(&new->dst) != drm_rect_width(&cur->dst) ||
11826 drm_rect_height(&new->dst) != drm_rect_height(&cur->dst))
11827 return true;
11828
11829 return false;
11830}
11831
11832static bool needs_scaling(struct intel_plane_state *state)
11833{
11834 int src_w = drm_rect_width(&state->src) >> 16;
11835 int src_h = drm_rect_height(&state->src) >> 16;
11836 int dst_w = drm_rect_width(&state->dst);
11837 int dst_h = drm_rect_height(&state->dst);
11838
11839 return (src_w != dst_w || src_h != dst_h);
11840}
d21fbe87 11841
da20eabd
ML
11842int intel_plane_atomic_calc_changes(struct drm_crtc_state *crtc_state,
11843 struct drm_plane_state *plane_state)
11844{
ab1d3a0e 11845 struct intel_crtc_state *pipe_config = to_intel_crtc_state(crtc_state);
da20eabd
ML
11846 struct drm_crtc *crtc = crtc_state->crtc;
11847 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11848 struct drm_plane *plane = plane_state->plane;
11849 struct drm_device *dev = crtc->dev;
ed4a6a7c 11850 struct drm_i915_private *dev_priv = to_i915(dev);
da20eabd
ML
11851 struct intel_plane_state *old_plane_state =
11852 to_intel_plane_state(plane->state);
da20eabd
ML
11853 bool mode_changed = needs_modeset(crtc_state);
11854 bool was_crtc_enabled = crtc->state->active;
11855 bool is_crtc_enabled = crtc_state->active;
da20eabd
ML
11856 bool turn_off, turn_on, visible, was_visible;
11857 struct drm_framebuffer *fb = plane_state->fb;
78108b7c 11858 int ret;
da20eabd 11859
84114990 11860 if (INTEL_GEN(dev) >= 9 && plane->type != DRM_PLANE_TYPE_CURSOR) {
da20eabd
ML
11861 ret = skl_update_scaler_plane(
11862 to_intel_crtc_state(crtc_state),
11863 to_intel_plane_state(plane_state));
11864 if (ret)
11865 return ret;
11866 }
11867
da20eabd
ML
11868 was_visible = old_plane_state->visible;
11869 visible = to_intel_plane_state(plane_state)->visible;
11870
11871 if (!was_crtc_enabled && WARN_ON(was_visible))
11872 was_visible = false;
11873
35c08f43
ML
11874 /*
11875 * Visibility is calculated as if the crtc was on, but
11876 * after scaler setup everything depends on it being off
11877 * when the crtc isn't active.
f818ffea
VS
11878 *
11879 * FIXME this is wrong for watermarks. Watermarks should also
11880 * be computed as if the pipe would be active. Perhaps move
11881 * per-plane wm computation to the .check_plane() hook, and
11882 * only combine the results from all planes in the current place?
35c08f43
ML
11883 */
11884 if (!is_crtc_enabled)
11885 to_intel_plane_state(plane_state)->visible = visible = false;
da20eabd
ML
11886
11887 if (!was_visible && !visible)
11888 return 0;
11889
e8861675
ML
11890 if (fb != old_plane_state->base.fb)
11891 pipe_config->fb_changed = true;
11892
da20eabd
ML
11893 turn_off = was_visible && (!visible || mode_changed);
11894 turn_on = visible && (!was_visible || mode_changed);
11895
72660ce0 11896 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] has [PLANE:%d:%s] with fb %i\n",
78108b7c
VS
11897 intel_crtc->base.base.id,
11898 intel_crtc->base.name,
72660ce0
VS
11899 plane->base.id, plane->name,
11900 fb ? fb->base.id : -1);
da20eabd 11901
72660ce0
VS
11902 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] visible %i -> %i, off %i, on %i, ms %i\n",
11903 plane->base.id, plane->name,
11904 was_visible, visible,
da20eabd
ML
11905 turn_off, turn_on, mode_changed);
11906
caed361d
VS
11907 if (turn_on) {
11908 pipe_config->update_wm_pre = true;
11909
11910 /* must disable cxsr around plane enable/disable */
11911 if (plane->type != DRM_PLANE_TYPE_CURSOR)
11912 pipe_config->disable_cxsr = true;
11913 } else if (turn_off) {
11914 pipe_config->update_wm_post = true;
92826fcd 11915
852eb00d 11916 /* must disable cxsr around plane enable/disable */
e8861675 11917 if (plane->type != DRM_PLANE_TYPE_CURSOR)
ab1d3a0e 11918 pipe_config->disable_cxsr = true;
852eb00d 11919 } else if (intel_wm_need_update(plane, plane_state)) {
caed361d
VS
11920 /* FIXME bollocks */
11921 pipe_config->update_wm_pre = true;
11922 pipe_config->update_wm_post = true;
852eb00d 11923 }
da20eabd 11924
ed4a6a7c 11925 /* Pre-gen9 platforms need two-step watermark updates */
caed361d
VS
11926 if ((pipe_config->update_wm_pre || pipe_config->update_wm_post) &&
11927 INTEL_INFO(dev)->gen < 9 && dev_priv->display.optimize_watermarks)
ed4a6a7c
MR
11928 to_intel_crtc_state(crtc_state)->wm.need_postvbl_update = true;
11929
8be6ca85 11930 if (visible || was_visible)
cd202f69 11931 pipe_config->fb_bits |= to_intel_plane(plane)->frontbuffer_bit;
a9ff8714 11932
31ae71fc
ML
11933 /*
11934 * WaCxSRDisabledForSpriteScaling:ivb
11935 *
11936 * cstate->update_wm was already set above, so this flag will
11937 * take effect when we commit and program watermarks.
11938 */
11939 if (plane->type == DRM_PLANE_TYPE_OVERLAY && IS_IVYBRIDGE(dev) &&
11940 needs_scaling(to_intel_plane_state(plane_state)) &&
11941 !needs_scaling(old_plane_state))
11942 pipe_config->disable_lp_wm = true;
d21fbe87 11943
da20eabd
ML
11944 return 0;
11945}
11946
6d3a1ce7
ML
11947static bool encoders_cloneable(const struct intel_encoder *a,
11948 const struct intel_encoder *b)
11949{
11950 /* masks could be asymmetric, so check both ways */
11951 return a == b || (a->cloneable & (1 << b->type) &&
11952 b->cloneable & (1 << a->type));
11953}
11954
11955static bool check_single_encoder_cloning(struct drm_atomic_state *state,
11956 struct intel_crtc *crtc,
11957 struct intel_encoder *encoder)
11958{
11959 struct intel_encoder *source_encoder;
11960 struct drm_connector *connector;
11961 struct drm_connector_state *connector_state;
11962 int i;
11963
11964 for_each_connector_in_state(state, connector, connector_state, i) {
11965 if (connector_state->crtc != &crtc->base)
11966 continue;
11967
11968 source_encoder =
11969 to_intel_encoder(connector_state->best_encoder);
11970 if (!encoders_cloneable(encoder, source_encoder))
11971 return false;
11972 }
11973
11974 return true;
11975}
11976
6d3a1ce7
ML
11977static int intel_crtc_atomic_check(struct drm_crtc *crtc,
11978 struct drm_crtc_state *crtc_state)
11979{
cf5a15be 11980 struct drm_device *dev = crtc->dev;
fac5e23e 11981 struct drm_i915_private *dev_priv = to_i915(dev);
6d3a1ce7 11982 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
cf5a15be
ML
11983 struct intel_crtc_state *pipe_config =
11984 to_intel_crtc_state(crtc_state);
6d3a1ce7 11985 struct drm_atomic_state *state = crtc_state->state;
4d20cd86 11986 int ret;
6d3a1ce7
ML
11987 bool mode_changed = needs_modeset(crtc_state);
11988
852eb00d 11989 if (mode_changed && !crtc_state->active)
caed361d 11990 pipe_config->update_wm_post = true;
eddfcbcd 11991
ad421372
ML
11992 if (mode_changed && crtc_state->enable &&
11993 dev_priv->display.crtc_compute_clock &&
8106ddbd 11994 !WARN_ON(pipe_config->shared_dpll)) {
ad421372
ML
11995 ret = dev_priv->display.crtc_compute_clock(intel_crtc,
11996 pipe_config);
11997 if (ret)
11998 return ret;
11999 }
12000
82cf435b
LL
12001 if (crtc_state->color_mgmt_changed) {
12002 ret = intel_color_check(crtc, crtc_state);
12003 if (ret)
12004 return ret;
e7852a4b
LL
12005
12006 /*
12007 * Changing color management on Intel hardware is
12008 * handled as part of planes update.
12009 */
12010 crtc_state->planes_changed = true;
82cf435b
LL
12011 }
12012
e435d6e5 12013 ret = 0;
86c8bbbe 12014 if (dev_priv->display.compute_pipe_wm) {
e3bddded 12015 ret = dev_priv->display.compute_pipe_wm(pipe_config);
ed4a6a7c
MR
12016 if (ret) {
12017 DRM_DEBUG_KMS("Target pipe watermarks are invalid\n");
12018 return ret;
12019 }
12020 }
12021
12022 if (dev_priv->display.compute_intermediate_wm &&
12023 !to_intel_atomic_state(state)->skip_intermediate_wm) {
12024 if (WARN_ON(!dev_priv->display.compute_pipe_wm))
12025 return 0;
12026
12027 /*
12028 * Calculate 'intermediate' watermarks that satisfy both the
12029 * old state and the new state. We can program these
12030 * immediately.
12031 */
12032 ret = dev_priv->display.compute_intermediate_wm(crtc->dev,
12033 intel_crtc,
12034 pipe_config);
12035 if (ret) {
12036 DRM_DEBUG_KMS("No valid intermediate pipe watermarks are possible\n");
86c8bbbe 12037 return ret;
ed4a6a7c 12038 }
e3d5457c
VS
12039 } else if (dev_priv->display.compute_intermediate_wm) {
12040 if (HAS_PCH_SPLIT(dev_priv) && INTEL_GEN(dev_priv) < 9)
12041 pipe_config->wm.ilk.intermediate = pipe_config->wm.ilk.optimal;
86c8bbbe
MR
12042 }
12043
e435d6e5
ML
12044 if (INTEL_INFO(dev)->gen >= 9) {
12045 if (mode_changed)
12046 ret = skl_update_scaler_crtc(pipe_config);
12047
12048 if (!ret)
12049 ret = intel_atomic_setup_scalers(dev, intel_crtc,
12050 pipe_config);
12051 }
12052
12053 return ret;
6d3a1ce7
ML
12054}
12055
65b38e0d 12056static const struct drm_crtc_helper_funcs intel_helper_funcs = {
f6e5b160 12057 .mode_set_base_atomic = intel_pipe_set_base_atomic,
5a21b665
DV
12058 .atomic_begin = intel_begin_crtc_commit,
12059 .atomic_flush = intel_finish_crtc_commit,
6d3a1ce7 12060 .atomic_check = intel_crtc_atomic_check,
f6e5b160
CW
12061};
12062
d29b2f9d
ACO
12063static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
12064{
12065 struct intel_connector *connector;
12066
12067 for_each_intel_connector(dev, connector) {
8863dc7f
DV
12068 if (connector->base.state->crtc)
12069 drm_connector_unreference(&connector->base);
12070
d29b2f9d
ACO
12071 if (connector->base.encoder) {
12072 connector->base.state->best_encoder =
12073 connector->base.encoder;
12074 connector->base.state->crtc =
12075 connector->base.encoder->crtc;
8863dc7f
DV
12076
12077 drm_connector_reference(&connector->base);
d29b2f9d
ACO
12078 } else {
12079 connector->base.state->best_encoder = NULL;
12080 connector->base.state->crtc = NULL;
12081 }
12082 }
12083}
12084
050f7aeb 12085static void
eba905b2 12086connected_sink_compute_bpp(struct intel_connector *connector,
5cec258b 12087 struct intel_crtc_state *pipe_config)
050f7aeb
DV
12088{
12089 int bpp = pipe_config->pipe_bpp;
12090
12091 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
12092 connector->base.base.id,
c23cc417 12093 connector->base.name);
050f7aeb
DV
12094
12095 /* Don't use an invalid EDID bpc value */
12096 if (connector->base.display_info.bpc &&
12097 connector->base.display_info.bpc * 3 < bpp) {
12098 DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
12099 bpp, connector->base.display_info.bpc*3);
12100 pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
12101 }
12102
013dd9e0
JN
12103 /* Clamp bpp to default limit on screens without EDID 1.4 */
12104 if (connector->base.display_info.bpc == 0) {
12105 int type = connector->base.connector_type;
12106 int clamp_bpp = 24;
12107
12108 /* Fall back to 18 bpp when DP sink capability is unknown. */
12109 if (type == DRM_MODE_CONNECTOR_DisplayPort ||
12110 type == DRM_MODE_CONNECTOR_eDP)
12111 clamp_bpp = 18;
12112
12113 if (bpp > clamp_bpp) {
12114 DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of %d\n",
12115 bpp, clamp_bpp);
12116 pipe_config->pipe_bpp = clamp_bpp;
12117 }
050f7aeb
DV
12118 }
12119}
12120
4e53c2e0 12121static int
050f7aeb 12122compute_baseline_pipe_bpp(struct intel_crtc *crtc,
5cec258b 12123 struct intel_crtc_state *pipe_config)
4e53c2e0 12124{
050f7aeb 12125 struct drm_device *dev = crtc->base.dev;
1486017f 12126 struct drm_atomic_state *state;
da3ced29
ACO
12127 struct drm_connector *connector;
12128 struct drm_connector_state *connector_state;
1486017f 12129 int bpp, i;
4e53c2e0 12130
666a4537 12131 if ((IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)))
4e53c2e0 12132 bpp = 10*3;
d328c9d7
DV
12133 else if (INTEL_INFO(dev)->gen >= 5)
12134 bpp = 12*3;
12135 else
12136 bpp = 8*3;
12137
4e53c2e0 12138
4e53c2e0
DV
12139 pipe_config->pipe_bpp = bpp;
12140
1486017f
ACO
12141 state = pipe_config->base.state;
12142
4e53c2e0 12143 /* Clamp display bpp to EDID value */
da3ced29
ACO
12144 for_each_connector_in_state(state, connector, connector_state, i) {
12145 if (connector_state->crtc != &crtc->base)
4e53c2e0
DV
12146 continue;
12147
da3ced29
ACO
12148 connected_sink_compute_bpp(to_intel_connector(connector),
12149 pipe_config);
4e53c2e0
DV
12150 }
12151
12152 return bpp;
12153}
12154
644db711
DV
12155static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
12156{
12157 DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
12158 "type: 0x%x flags: 0x%x\n",
1342830c 12159 mode->crtc_clock,
644db711
DV
12160 mode->crtc_hdisplay, mode->crtc_hsync_start,
12161 mode->crtc_hsync_end, mode->crtc_htotal,
12162 mode->crtc_vdisplay, mode->crtc_vsync_start,
12163 mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
12164}
12165
c0b03411 12166static void intel_dump_pipe_config(struct intel_crtc *crtc,
5cec258b 12167 struct intel_crtc_state *pipe_config,
c0b03411
DV
12168 const char *context)
12169{
6a60cd87
CK
12170 struct drm_device *dev = crtc->base.dev;
12171 struct drm_plane *plane;
12172 struct intel_plane *intel_plane;
12173 struct intel_plane_state *state;
12174 struct drm_framebuffer *fb;
12175
78108b7c
VS
12176 DRM_DEBUG_KMS("[CRTC:%d:%s]%s config %p for pipe %c\n",
12177 crtc->base.base.id, crtc->base.name,
6a60cd87 12178 context, pipe_config, pipe_name(crtc->pipe));
c0b03411 12179
da205630 12180 DRM_DEBUG_KMS("cpu_transcoder: %s\n", transcoder_name(pipe_config->cpu_transcoder));
c0b03411
DV
12181 DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
12182 pipe_config->pipe_bpp, pipe_config->dither);
12183 DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
12184 pipe_config->has_pch_encoder,
12185 pipe_config->fdi_lanes,
12186 pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
12187 pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
12188 pipe_config->fdi_m_n.tu);
90a6b7b0 12189 DRM_DEBUG_KMS("dp: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
37a5650b 12190 intel_crtc_has_dp_encoder(pipe_config),
90a6b7b0 12191 pipe_config->lane_count,
eb14cb74
VS
12192 pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
12193 pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
12194 pipe_config->dp_m_n.tu);
b95af8be 12195
90a6b7b0 12196 DRM_DEBUG_KMS("dp: %i, lanes: %i, gmch_m2: %u, gmch_n2: %u, link_m2: %u, link_n2: %u, tu2: %u\n",
37a5650b 12197 intel_crtc_has_dp_encoder(pipe_config),
90a6b7b0 12198 pipe_config->lane_count,
b95af8be
VK
12199 pipe_config->dp_m2_n2.gmch_m,
12200 pipe_config->dp_m2_n2.gmch_n,
12201 pipe_config->dp_m2_n2.link_m,
12202 pipe_config->dp_m2_n2.link_n,
12203 pipe_config->dp_m2_n2.tu);
12204
55072d19
DV
12205 DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
12206 pipe_config->has_audio,
12207 pipe_config->has_infoframe);
12208
c0b03411 12209 DRM_DEBUG_KMS("requested mode:\n");
2d112de7 12210 drm_mode_debug_printmodeline(&pipe_config->base.mode);
c0b03411 12211 DRM_DEBUG_KMS("adjusted mode:\n");
2d112de7
ACO
12212 drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
12213 intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
d71b8d4a 12214 DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
37327abd
VS
12215 DRM_DEBUG_KMS("pipe src size: %dx%d\n",
12216 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
0ec463d3
TU
12217 DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
12218 crtc->num_scalers,
12219 pipe_config->scaler_state.scaler_users,
12220 pipe_config->scaler_state.scaler_id);
c0b03411
DV
12221 DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
12222 pipe_config->gmch_pfit.control,
12223 pipe_config->gmch_pfit.pgm_ratios,
12224 pipe_config->gmch_pfit.lvds_border_bits);
fd4daa9c 12225 DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
c0b03411 12226 pipe_config->pch_pfit.pos,
fd4daa9c
CW
12227 pipe_config->pch_pfit.size,
12228 pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
42db64ef 12229 DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
cf532bb2 12230 DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
6a60cd87 12231
415ff0f6 12232 if (IS_BROXTON(dev)) {
05712c15 12233 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: ebb0: 0x%x, ebb4: 0x%x,"
415ff0f6 12234 "pll0: 0x%x, pll1: 0x%x, pll2: 0x%x, pll3: 0x%x, "
c8453338 12235 "pll6: 0x%x, pll8: 0x%x, pll9: 0x%x, pll10: 0x%x, pcsdw12: 0x%x\n",
415ff0f6
TU
12236 pipe_config->ddi_pll_sel,
12237 pipe_config->dpll_hw_state.ebb0,
05712c15 12238 pipe_config->dpll_hw_state.ebb4,
415ff0f6
TU
12239 pipe_config->dpll_hw_state.pll0,
12240 pipe_config->dpll_hw_state.pll1,
12241 pipe_config->dpll_hw_state.pll2,
12242 pipe_config->dpll_hw_state.pll3,
12243 pipe_config->dpll_hw_state.pll6,
12244 pipe_config->dpll_hw_state.pll8,
05712c15 12245 pipe_config->dpll_hw_state.pll9,
c8453338 12246 pipe_config->dpll_hw_state.pll10,
415ff0f6 12247 pipe_config->dpll_hw_state.pcsdw12);
ef11bdb3 12248 } else if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) {
415ff0f6
TU
12249 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: "
12250 "ctrl1: 0x%x, cfgcr1: 0x%x, cfgcr2: 0x%x\n",
12251 pipe_config->ddi_pll_sel,
12252 pipe_config->dpll_hw_state.ctrl1,
12253 pipe_config->dpll_hw_state.cfgcr1,
12254 pipe_config->dpll_hw_state.cfgcr2);
12255 } else if (HAS_DDI(dev)) {
1260f07e 12256 DRM_DEBUG_KMS("ddi_pll_sel: 0x%x; dpll_hw_state: wrpll: 0x%x spll: 0x%x\n",
415ff0f6 12257 pipe_config->ddi_pll_sel,
00490c22
ML
12258 pipe_config->dpll_hw_state.wrpll,
12259 pipe_config->dpll_hw_state.spll);
415ff0f6
TU
12260 } else {
12261 DRM_DEBUG_KMS("dpll_hw_state: dpll: 0x%x, dpll_md: 0x%x, "
12262 "fp0: 0x%x, fp1: 0x%x\n",
12263 pipe_config->dpll_hw_state.dpll,
12264 pipe_config->dpll_hw_state.dpll_md,
12265 pipe_config->dpll_hw_state.fp0,
12266 pipe_config->dpll_hw_state.fp1);
12267 }
12268
6a60cd87
CK
12269 DRM_DEBUG_KMS("planes on this crtc\n");
12270 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
12271 intel_plane = to_intel_plane(plane);
12272 if (intel_plane->pipe != crtc->pipe)
12273 continue;
12274
12275 state = to_intel_plane_state(plane->state);
12276 fb = state->base.fb;
12277 if (!fb) {
1d577e02
VS
12278 DRM_DEBUG_KMS("[PLANE:%d:%s] disabled, scaler_id = %d\n",
12279 plane->base.id, plane->name, state->scaler_id);
6a60cd87
CK
12280 continue;
12281 }
12282
1d577e02
VS
12283 DRM_DEBUG_KMS("[PLANE:%d:%s] enabled",
12284 plane->base.id, plane->name);
12285 DRM_DEBUG_KMS("\tFB:%d, fb = %ux%u format = %s",
12286 fb->base.id, fb->width, fb->height,
12287 drm_get_format_name(fb->pixel_format));
12288 DRM_DEBUG_KMS("\tscaler:%d src %dx%d+%d+%d dst %dx%d+%d+%d\n",
12289 state->scaler_id,
12290 state->src.x1 >> 16, state->src.y1 >> 16,
12291 drm_rect_width(&state->src) >> 16,
12292 drm_rect_height(&state->src) >> 16,
12293 state->dst.x1, state->dst.y1,
12294 drm_rect_width(&state->dst),
12295 drm_rect_height(&state->dst));
6a60cd87 12296 }
c0b03411
DV
12297}
12298
5448a00d 12299static bool check_digital_port_conflicts(struct drm_atomic_state *state)
00f0b378 12300{
5448a00d 12301 struct drm_device *dev = state->dev;
da3ced29 12302 struct drm_connector *connector;
00f0b378 12303 unsigned int used_ports = 0;
477321e0 12304 unsigned int used_mst_ports = 0;
00f0b378
VS
12305
12306 /*
12307 * Walk the connector list instead of the encoder
12308 * list to detect the problem on ddi platforms
12309 * where there's just one encoder per digital port.
12310 */
0bff4858
VS
12311 drm_for_each_connector(connector, dev) {
12312 struct drm_connector_state *connector_state;
12313 struct intel_encoder *encoder;
12314
12315 connector_state = drm_atomic_get_existing_connector_state(state, connector);
12316 if (!connector_state)
12317 connector_state = connector->state;
12318
5448a00d 12319 if (!connector_state->best_encoder)
00f0b378
VS
12320 continue;
12321
5448a00d
ACO
12322 encoder = to_intel_encoder(connector_state->best_encoder);
12323
12324 WARN_ON(!connector_state->crtc);
00f0b378
VS
12325
12326 switch (encoder->type) {
12327 unsigned int port_mask;
12328 case INTEL_OUTPUT_UNKNOWN:
12329 if (WARN_ON(!HAS_DDI(dev)))
12330 break;
cca0502b 12331 case INTEL_OUTPUT_DP:
00f0b378
VS
12332 case INTEL_OUTPUT_HDMI:
12333 case INTEL_OUTPUT_EDP:
12334 port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
12335
12336 /* the same port mustn't appear more than once */
12337 if (used_ports & port_mask)
12338 return false;
12339
12340 used_ports |= port_mask;
477321e0
VS
12341 break;
12342 case INTEL_OUTPUT_DP_MST:
12343 used_mst_ports |=
12344 1 << enc_to_mst(&encoder->base)->primary->port;
12345 break;
00f0b378
VS
12346 default:
12347 break;
12348 }
12349 }
12350
477321e0
VS
12351 /* can't mix MST and SST/HDMI on the same port */
12352 if (used_ports & used_mst_ports)
12353 return false;
12354
00f0b378
VS
12355 return true;
12356}
12357
83a57153
ACO
12358static void
12359clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
12360{
12361 struct drm_crtc_state tmp_state;
663a3640 12362 struct intel_crtc_scaler_state scaler_state;
4978cc93 12363 struct intel_dpll_hw_state dpll_hw_state;
8106ddbd 12364 struct intel_shared_dpll *shared_dpll;
8504c74c 12365 uint32_t ddi_pll_sel;
c4e2d043 12366 bool force_thru;
83a57153 12367
7546a384
ACO
12368 /* FIXME: before the switch to atomic started, a new pipe_config was
12369 * kzalloc'd. Code that depends on any field being zero should be
12370 * fixed, so that the crtc_state can be safely duplicated. For now,
12371 * only fields that are know to not cause problems are preserved. */
12372
83a57153 12373 tmp_state = crtc_state->base;
663a3640 12374 scaler_state = crtc_state->scaler_state;
4978cc93
ACO
12375 shared_dpll = crtc_state->shared_dpll;
12376 dpll_hw_state = crtc_state->dpll_hw_state;
8504c74c 12377 ddi_pll_sel = crtc_state->ddi_pll_sel;
c4e2d043 12378 force_thru = crtc_state->pch_pfit.force_thru;
4978cc93 12379
83a57153 12380 memset(crtc_state, 0, sizeof *crtc_state);
4978cc93 12381
83a57153 12382 crtc_state->base = tmp_state;
663a3640 12383 crtc_state->scaler_state = scaler_state;
4978cc93
ACO
12384 crtc_state->shared_dpll = shared_dpll;
12385 crtc_state->dpll_hw_state = dpll_hw_state;
8504c74c 12386 crtc_state->ddi_pll_sel = ddi_pll_sel;
c4e2d043 12387 crtc_state->pch_pfit.force_thru = force_thru;
83a57153
ACO
12388}
12389
548ee15b 12390static int
b8cecdf5 12391intel_modeset_pipe_config(struct drm_crtc *crtc,
b359283a 12392 struct intel_crtc_state *pipe_config)
ee7b9f93 12393{
b359283a 12394 struct drm_atomic_state *state = pipe_config->base.state;
7758a113 12395 struct intel_encoder *encoder;
da3ced29 12396 struct drm_connector *connector;
0b901879 12397 struct drm_connector_state *connector_state;
d328c9d7 12398 int base_bpp, ret = -EINVAL;
0b901879 12399 int i;
e29c22c0 12400 bool retry = true;
ee7b9f93 12401
83a57153 12402 clear_intel_crtc_state(pipe_config);
7758a113 12403
e143a21c
DV
12404 pipe_config->cpu_transcoder =
12405 (enum transcoder) to_intel_crtc(crtc)->pipe;
b8cecdf5 12406
2960bc9c
ID
12407 /*
12408 * Sanitize sync polarity flags based on requested ones. If neither
12409 * positive or negative polarity is requested, treat this as meaning
12410 * negative polarity.
12411 */
2d112de7 12412 if (!(pipe_config->base.adjusted_mode.flags &
2960bc9c 12413 (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
2d112de7 12414 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
2960bc9c 12415
2d112de7 12416 if (!(pipe_config->base.adjusted_mode.flags &
2960bc9c 12417 (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
2d112de7 12418 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
2960bc9c 12419
d328c9d7
DV
12420 base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
12421 pipe_config);
12422 if (base_bpp < 0)
4e53c2e0
DV
12423 goto fail;
12424
e41a56be
VS
12425 /*
12426 * Determine the real pipe dimensions. Note that stereo modes can
12427 * increase the actual pipe size due to the frame doubling and
12428 * insertion of additional space for blanks between the frame. This
12429 * is stored in the crtc timings. We use the requested mode to do this
12430 * computation to clearly distinguish it from the adjusted mode, which
12431 * can be changed by the connectors in the below retry loop.
12432 */
2d112de7 12433 drm_crtc_get_hv_timing(&pipe_config->base.mode,
ecb7e16b
GP
12434 &pipe_config->pipe_src_w,
12435 &pipe_config->pipe_src_h);
e41a56be 12436
253c84c8
VS
12437 for_each_connector_in_state(state, connector, connector_state, i) {
12438 if (connector_state->crtc != crtc)
12439 continue;
12440
12441 encoder = to_intel_encoder(connector_state->best_encoder);
12442
e25148d0
VS
12443 if (!check_single_encoder_cloning(state, to_intel_crtc(crtc), encoder)) {
12444 DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
12445 goto fail;
12446 }
12447
253c84c8
VS
12448 /*
12449 * Determine output_types before calling the .compute_config()
12450 * hooks so that the hooks can use this information safely.
12451 */
12452 pipe_config->output_types |= 1 << encoder->type;
12453 }
12454
e29c22c0 12455encoder_retry:
ef1b460d 12456 /* Ensure the port clock defaults are reset when retrying. */
ff9a6750 12457 pipe_config->port_clock = 0;
ef1b460d 12458 pipe_config->pixel_multiplier = 1;
ff9a6750 12459
135c81b8 12460 /* Fill in default crtc timings, allow encoders to overwrite them. */
2d112de7
ACO
12461 drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
12462 CRTC_STEREO_DOUBLE);
135c81b8 12463
7758a113
DV
12464 /* Pass our mode to the connectors and the CRTC to give them a chance to
12465 * adjust it according to limitations or connector properties, and also
12466 * a chance to reject the mode entirely.
47f1c6c9 12467 */
da3ced29 12468 for_each_connector_in_state(state, connector, connector_state, i) {
0b901879 12469 if (connector_state->crtc != crtc)
7758a113 12470 continue;
7ae89233 12471
0b901879
ACO
12472 encoder = to_intel_encoder(connector_state->best_encoder);
12473
efea6e8e
DV
12474 if (!(encoder->compute_config(encoder, pipe_config))) {
12475 DRM_DEBUG_KMS("Encoder config failure\n");
7758a113
DV
12476 goto fail;
12477 }
ee7b9f93 12478 }
47f1c6c9 12479
ff9a6750
DV
12480 /* Set default port clock if not overwritten by the encoder. Needs to be
12481 * done afterwards in case the encoder adjusts the mode. */
12482 if (!pipe_config->port_clock)
2d112de7 12483 pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
241bfc38 12484 * pipe_config->pixel_multiplier;
ff9a6750 12485
a43f6e0f 12486 ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
e29c22c0 12487 if (ret < 0) {
7758a113
DV
12488 DRM_DEBUG_KMS("CRTC fixup failed\n");
12489 goto fail;
ee7b9f93 12490 }
e29c22c0
DV
12491
12492 if (ret == RETRY) {
12493 if (WARN(!retry, "loop in pipe configuration computation\n")) {
12494 ret = -EINVAL;
12495 goto fail;
12496 }
12497
12498 DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
12499 retry = false;
12500 goto encoder_retry;
12501 }
12502
e8fa4270
DV
12503 /* Dithering seems to not pass-through bits correctly when it should, so
12504 * only enable it on 6bpc panels. */
12505 pipe_config->dither = pipe_config->pipe_bpp == 6*3;
62f0ace5 12506 DRM_DEBUG_KMS("hw max bpp: %i, pipe bpp: %i, dithering: %i\n",
d328c9d7 12507 base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
4e53c2e0 12508
7758a113 12509fail:
548ee15b 12510 return ret;
ee7b9f93 12511}
47f1c6c9 12512
ea9d758d 12513static void
4740b0f2 12514intel_modeset_update_crtc_state(struct drm_atomic_state *state)
ea9d758d 12515{
0a9ab303
ACO
12516 struct drm_crtc *crtc;
12517 struct drm_crtc_state *crtc_state;
8a75d157 12518 int i;
ea9d758d 12519
7668851f 12520 /* Double check state. */
8a75d157 12521 for_each_crtc_in_state(state, crtc, crtc_state, i) {
3cb480bc 12522 to_intel_crtc(crtc)->config = to_intel_crtc_state(crtc->state);
fc467a22
ML
12523
12524 /* Update hwmode for vblank functions */
12525 if (crtc->state->active)
12526 crtc->hwmode = crtc->state->adjusted_mode;
12527 else
12528 crtc->hwmode.crtc_clock = 0;
61067a5e
ML
12529
12530 /*
12531 * Update legacy state to satisfy fbc code. This can
12532 * be removed when fbc uses the atomic state.
12533 */
12534 if (drm_atomic_get_existing_plane_state(state, crtc->primary)) {
12535 struct drm_plane_state *plane_state = crtc->primary->state;
12536
12537 crtc->primary->fb = plane_state->fb;
12538 crtc->x = plane_state->src_x >> 16;
12539 crtc->y = plane_state->src_y >> 16;
12540 }
ea9d758d 12541 }
ea9d758d
DV
12542}
12543
3bd26263 12544static bool intel_fuzzy_clock_check(int clock1, int clock2)
f1f644dc 12545{
3bd26263 12546 int diff;
f1f644dc
JB
12547
12548 if (clock1 == clock2)
12549 return true;
12550
12551 if (!clock1 || !clock2)
12552 return false;
12553
12554 diff = abs(clock1 - clock2);
12555
12556 if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
12557 return true;
12558
12559 return false;
12560}
12561
25c5b266
DV
12562#define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
12563 list_for_each_entry((intel_crtc), \
12564 &(dev)->mode_config.crtc_list, \
12565 base.head) \
95150bdf 12566 for_each_if (mask & (1 <<(intel_crtc)->pipe))
25c5b266 12567
cfb23ed6
ML
12568static bool
12569intel_compare_m_n(unsigned int m, unsigned int n,
12570 unsigned int m2, unsigned int n2,
12571 bool exact)
12572{
12573 if (m == m2 && n == n2)
12574 return true;
12575
12576 if (exact || !m || !n || !m2 || !n2)
12577 return false;
12578
12579 BUILD_BUG_ON(DATA_LINK_M_N_MASK > INT_MAX);
12580
31d10b57
ML
12581 if (n > n2) {
12582 while (n > n2) {
cfb23ed6
ML
12583 m2 <<= 1;
12584 n2 <<= 1;
12585 }
31d10b57
ML
12586 } else if (n < n2) {
12587 while (n < n2) {
cfb23ed6
ML
12588 m <<= 1;
12589 n <<= 1;
12590 }
12591 }
12592
31d10b57
ML
12593 if (n != n2)
12594 return false;
12595
12596 return intel_fuzzy_clock_check(m, m2);
cfb23ed6
ML
12597}
12598
12599static bool
12600intel_compare_link_m_n(const struct intel_link_m_n *m_n,
12601 struct intel_link_m_n *m2_n2,
12602 bool adjust)
12603{
12604 if (m_n->tu == m2_n2->tu &&
12605 intel_compare_m_n(m_n->gmch_m, m_n->gmch_n,
12606 m2_n2->gmch_m, m2_n2->gmch_n, !adjust) &&
12607 intel_compare_m_n(m_n->link_m, m_n->link_n,
12608 m2_n2->link_m, m2_n2->link_n, !adjust)) {
12609 if (adjust)
12610 *m2_n2 = *m_n;
12611
12612 return true;
12613 }
12614
12615 return false;
12616}
12617
0e8ffe1b 12618static bool
2fa2fe9a 12619intel_pipe_config_compare(struct drm_device *dev,
5cec258b 12620 struct intel_crtc_state *current_config,
cfb23ed6
ML
12621 struct intel_crtc_state *pipe_config,
12622 bool adjust)
0e8ffe1b 12623{
cfb23ed6
ML
12624 bool ret = true;
12625
12626#define INTEL_ERR_OR_DBG_KMS(fmt, ...) \
12627 do { \
12628 if (!adjust) \
12629 DRM_ERROR(fmt, ##__VA_ARGS__); \
12630 else \
12631 DRM_DEBUG_KMS(fmt, ##__VA_ARGS__); \
12632 } while (0)
12633
66e985c0
DV
12634#define PIPE_CONF_CHECK_X(name) \
12635 if (current_config->name != pipe_config->name) { \
cfb23ed6 12636 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
66e985c0
DV
12637 "(expected 0x%08x, found 0x%08x)\n", \
12638 current_config->name, \
12639 pipe_config->name); \
cfb23ed6 12640 ret = false; \
66e985c0
DV
12641 }
12642
08a24034
DV
12643#define PIPE_CONF_CHECK_I(name) \
12644 if (current_config->name != pipe_config->name) { \
cfb23ed6 12645 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
08a24034
DV
12646 "(expected %i, found %i)\n", \
12647 current_config->name, \
12648 pipe_config->name); \
cfb23ed6
ML
12649 ret = false; \
12650 }
12651
8106ddbd
ACO
12652#define PIPE_CONF_CHECK_P(name) \
12653 if (current_config->name != pipe_config->name) { \
12654 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
12655 "(expected %p, found %p)\n", \
12656 current_config->name, \
12657 pipe_config->name); \
12658 ret = false; \
12659 }
12660
cfb23ed6
ML
12661#define PIPE_CONF_CHECK_M_N(name) \
12662 if (!intel_compare_link_m_n(&current_config->name, \
12663 &pipe_config->name,\
12664 adjust)) { \
12665 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
12666 "(expected tu %i gmch %i/%i link %i/%i, " \
12667 "found tu %i, gmch %i/%i link %i/%i)\n", \
12668 current_config->name.tu, \
12669 current_config->name.gmch_m, \
12670 current_config->name.gmch_n, \
12671 current_config->name.link_m, \
12672 current_config->name.link_n, \
12673 pipe_config->name.tu, \
12674 pipe_config->name.gmch_m, \
12675 pipe_config->name.gmch_n, \
12676 pipe_config->name.link_m, \
12677 pipe_config->name.link_n); \
12678 ret = false; \
12679 }
12680
55c561a7
DV
12681/* This is required for BDW+ where there is only one set of registers for
12682 * switching between high and low RR.
12683 * This macro can be used whenever a comparison has to be made between one
12684 * hw state and multiple sw state variables.
12685 */
cfb23ed6
ML
12686#define PIPE_CONF_CHECK_M_N_ALT(name, alt_name) \
12687 if (!intel_compare_link_m_n(&current_config->name, \
12688 &pipe_config->name, adjust) && \
12689 !intel_compare_link_m_n(&current_config->alt_name, \
12690 &pipe_config->name, adjust)) { \
12691 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
12692 "(expected tu %i gmch %i/%i link %i/%i, " \
12693 "or tu %i gmch %i/%i link %i/%i, " \
12694 "found tu %i, gmch %i/%i link %i/%i)\n", \
12695 current_config->name.tu, \
12696 current_config->name.gmch_m, \
12697 current_config->name.gmch_n, \
12698 current_config->name.link_m, \
12699 current_config->name.link_n, \
12700 current_config->alt_name.tu, \
12701 current_config->alt_name.gmch_m, \
12702 current_config->alt_name.gmch_n, \
12703 current_config->alt_name.link_m, \
12704 current_config->alt_name.link_n, \
12705 pipe_config->name.tu, \
12706 pipe_config->name.gmch_m, \
12707 pipe_config->name.gmch_n, \
12708 pipe_config->name.link_m, \
12709 pipe_config->name.link_n); \
12710 ret = false; \
88adfff1
DV
12711 }
12712
1bd1bd80
DV
12713#define PIPE_CONF_CHECK_FLAGS(name, mask) \
12714 if ((current_config->name ^ pipe_config->name) & (mask)) { \
cfb23ed6 12715 INTEL_ERR_OR_DBG_KMS("mismatch in " #name "(" #mask ") " \
1bd1bd80
DV
12716 "(expected %i, found %i)\n", \
12717 current_config->name & (mask), \
12718 pipe_config->name & (mask)); \
cfb23ed6 12719 ret = false; \
1bd1bd80
DV
12720 }
12721
5e550656
VS
12722#define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
12723 if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
cfb23ed6 12724 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
5e550656
VS
12725 "(expected %i, found %i)\n", \
12726 current_config->name, \
12727 pipe_config->name); \
cfb23ed6 12728 ret = false; \
5e550656
VS
12729 }
12730
bb760063
DV
12731#define PIPE_CONF_QUIRK(quirk) \
12732 ((current_config->quirks | pipe_config->quirks) & (quirk))
12733
eccb140b
DV
12734 PIPE_CONF_CHECK_I(cpu_transcoder);
12735
08a24034
DV
12736 PIPE_CONF_CHECK_I(has_pch_encoder);
12737 PIPE_CONF_CHECK_I(fdi_lanes);
cfb23ed6 12738 PIPE_CONF_CHECK_M_N(fdi_m_n);
08a24034 12739
90a6b7b0 12740 PIPE_CONF_CHECK_I(lane_count);
95a7a2ae 12741 PIPE_CONF_CHECK_X(lane_lat_optim_mask);
b95af8be
VK
12742
12743 if (INTEL_INFO(dev)->gen < 8) {
cfb23ed6
ML
12744 PIPE_CONF_CHECK_M_N(dp_m_n);
12745
cfb23ed6
ML
12746 if (current_config->has_drrs)
12747 PIPE_CONF_CHECK_M_N(dp_m2_n2);
12748 } else
12749 PIPE_CONF_CHECK_M_N_ALT(dp_m_n, dp_m2_n2);
eb14cb74 12750
253c84c8 12751 PIPE_CONF_CHECK_X(output_types);
a65347ba 12752
2d112de7
ACO
12753 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
12754 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
12755 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
12756 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
12757 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
12758 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
1bd1bd80 12759
2d112de7
ACO
12760 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
12761 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
12762 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
12763 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
12764 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
12765 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
1bd1bd80 12766
c93f54cf 12767 PIPE_CONF_CHECK_I(pixel_multiplier);
6897b4b5 12768 PIPE_CONF_CHECK_I(has_hdmi_sink);
b5a9fa09 12769 if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
666a4537 12770 IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev))
b5a9fa09 12771 PIPE_CONF_CHECK_I(limited_color_range);
e43823ec 12772 PIPE_CONF_CHECK_I(has_infoframe);
6c49f241 12773
9ed109a7
DV
12774 PIPE_CONF_CHECK_I(has_audio);
12775
2d112de7 12776 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
1bd1bd80
DV
12777 DRM_MODE_FLAG_INTERLACE);
12778
bb760063 12779 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
2d112de7 12780 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063 12781 DRM_MODE_FLAG_PHSYNC);
2d112de7 12782 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063 12783 DRM_MODE_FLAG_NHSYNC);
2d112de7 12784 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063 12785 DRM_MODE_FLAG_PVSYNC);
2d112de7 12786 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063
DV
12787 DRM_MODE_FLAG_NVSYNC);
12788 }
045ac3b5 12789
333b8ca8 12790 PIPE_CONF_CHECK_X(gmch_pfit.control);
e2ff2d4a
DV
12791 /* pfit ratios are autocomputed by the hw on gen4+ */
12792 if (INTEL_INFO(dev)->gen < 4)
7f7d8dd6 12793 PIPE_CONF_CHECK_X(gmch_pfit.pgm_ratios);
333b8ca8 12794 PIPE_CONF_CHECK_X(gmch_pfit.lvds_border_bits);
9953599b 12795
bfd16b2a
ML
12796 if (!adjust) {
12797 PIPE_CONF_CHECK_I(pipe_src_w);
12798 PIPE_CONF_CHECK_I(pipe_src_h);
12799
12800 PIPE_CONF_CHECK_I(pch_pfit.enabled);
12801 if (current_config->pch_pfit.enabled) {
12802 PIPE_CONF_CHECK_X(pch_pfit.pos);
12803 PIPE_CONF_CHECK_X(pch_pfit.size);
12804 }
2fa2fe9a 12805
7aefe2b5
ML
12806 PIPE_CONF_CHECK_I(scaler_state.scaler_id);
12807 }
a1b2278e 12808
e59150dc
JB
12809 /* BDW+ don't expose a synchronous way to read the state */
12810 if (IS_HASWELL(dev))
12811 PIPE_CONF_CHECK_I(ips_enabled);
42db64ef 12812
282740f7
VS
12813 PIPE_CONF_CHECK_I(double_wide);
12814
26804afd
DV
12815 PIPE_CONF_CHECK_X(ddi_pll_sel);
12816
8106ddbd 12817 PIPE_CONF_CHECK_P(shared_dpll);
66e985c0 12818 PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
8bcc2795 12819 PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
66e985c0
DV
12820 PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
12821 PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
d452c5b6 12822 PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
00490c22 12823 PIPE_CONF_CHECK_X(dpll_hw_state.spll);
3f4cd19f
DL
12824 PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
12825 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
12826 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
c0d43d62 12827
47eacbab
VS
12828 PIPE_CONF_CHECK_X(dsi_pll.ctrl);
12829 PIPE_CONF_CHECK_X(dsi_pll.div);
12830
42571aef
VS
12831 if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
12832 PIPE_CONF_CHECK_I(pipe_bpp);
12833
2d112de7 12834 PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
a9a7e98a 12835 PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
5e550656 12836
66e985c0 12837#undef PIPE_CONF_CHECK_X
08a24034 12838#undef PIPE_CONF_CHECK_I
8106ddbd 12839#undef PIPE_CONF_CHECK_P
1bd1bd80 12840#undef PIPE_CONF_CHECK_FLAGS
5e550656 12841#undef PIPE_CONF_CHECK_CLOCK_FUZZY
bb760063 12842#undef PIPE_CONF_QUIRK
cfb23ed6 12843#undef INTEL_ERR_OR_DBG_KMS
88adfff1 12844
cfb23ed6 12845 return ret;
0e8ffe1b
DV
12846}
12847
e3b247da
VS
12848static void intel_pipe_config_sanity_check(struct drm_i915_private *dev_priv,
12849 const struct intel_crtc_state *pipe_config)
12850{
12851 if (pipe_config->has_pch_encoder) {
21a727b3 12852 int fdi_dotclock = intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
e3b247da
VS
12853 &pipe_config->fdi_m_n);
12854 int dotclock = pipe_config->base.adjusted_mode.crtc_clock;
12855
12856 /*
12857 * FDI already provided one idea for the dotclock.
12858 * Yell if the encoder disagrees.
12859 */
12860 WARN(!intel_fuzzy_clock_check(fdi_dotclock, dotclock),
12861 "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
12862 fdi_dotclock, dotclock);
12863 }
12864}
12865
c0ead703
ML
12866static void verify_wm_state(struct drm_crtc *crtc,
12867 struct drm_crtc_state *new_state)
08db6652 12868{
e7c84544 12869 struct drm_device *dev = crtc->dev;
fac5e23e 12870 struct drm_i915_private *dev_priv = to_i915(dev);
08db6652 12871 struct skl_ddb_allocation hw_ddb, *sw_ddb;
e7c84544
ML
12872 struct skl_ddb_entry *hw_entry, *sw_entry;
12873 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
12874 const enum pipe pipe = intel_crtc->pipe;
08db6652
DL
12875 int plane;
12876
e7c84544 12877 if (INTEL_INFO(dev)->gen < 9 || !new_state->active)
08db6652
DL
12878 return;
12879
12880 skl_ddb_get_hw_state(dev_priv, &hw_ddb);
12881 sw_ddb = &dev_priv->wm.skl_hw.ddb;
12882
e7c84544
ML
12883 /* planes */
12884 for_each_plane(dev_priv, pipe, plane) {
12885 hw_entry = &hw_ddb.plane[pipe][plane];
12886 sw_entry = &sw_ddb->plane[pipe][plane];
08db6652 12887
e7c84544 12888 if (skl_ddb_entry_equal(hw_entry, sw_entry))
08db6652
DL
12889 continue;
12890
e7c84544
ML
12891 DRM_ERROR("mismatch in DDB state pipe %c plane %d "
12892 "(expected (%u,%u), found (%u,%u))\n",
12893 pipe_name(pipe), plane + 1,
12894 sw_entry->start, sw_entry->end,
12895 hw_entry->start, hw_entry->end);
12896 }
08db6652 12897
e7c84544
ML
12898 /* cursor */
12899 hw_entry = &hw_ddb.plane[pipe][PLANE_CURSOR];
12900 sw_entry = &sw_ddb->plane[pipe][PLANE_CURSOR];
08db6652 12901
e7c84544 12902 if (!skl_ddb_entry_equal(hw_entry, sw_entry)) {
08db6652
DL
12903 DRM_ERROR("mismatch in DDB state pipe %c cursor "
12904 "(expected (%u,%u), found (%u,%u))\n",
12905 pipe_name(pipe),
12906 sw_entry->start, sw_entry->end,
12907 hw_entry->start, hw_entry->end);
12908 }
12909}
12910
91d1b4bd 12911static void
c0ead703 12912verify_connector_state(struct drm_device *dev, struct drm_crtc *crtc)
8af6cf88 12913{
35dd3c64 12914 struct drm_connector *connector;
8af6cf88 12915
e7c84544 12916 drm_for_each_connector(connector, dev) {
35dd3c64
ML
12917 struct drm_encoder *encoder = connector->encoder;
12918 struct drm_connector_state *state = connector->state;
ad3c558f 12919
e7c84544
ML
12920 if (state->crtc != crtc)
12921 continue;
12922
5a21b665 12923 intel_connector_verify_state(to_intel_connector(connector));
8af6cf88 12924
ad3c558f 12925 I915_STATE_WARN(state->best_encoder != encoder,
35dd3c64 12926 "connector's atomic encoder doesn't match legacy encoder\n");
8af6cf88 12927 }
91d1b4bd
DV
12928}
12929
12930static void
c0ead703 12931verify_encoder_state(struct drm_device *dev)
91d1b4bd
DV
12932{
12933 struct intel_encoder *encoder;
12934 struct intel_connector *connector;
8af6cf88 12935
b2784e15 12936 for_each_intel_encoder(dev, encoder) {
8af6cf88 12937 bool enabled = false;
4d20cd86 12938 enum pipe pipe;
8af6cf88
DV
12939
12940 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
12941 encoder->base.base.id,
8e329a03 12942 encoder->base.name);
8af6cf88 12943
3a3371ff 12944 for_each_intel_connector(dev, connector) {
4d20cd86 12945 if (connector->base.state->best_encoder != &encoder->base)
8af6cf88
DV
12946 continue;
12947 enabled = true;
ad3c558f
ML
12948
12949 I915_STATE_WARN(connector->base.state->crtc !=
12950 encoder->base.crtc,
12951 "connector's crtc doesn't match encoder crtc\n");
8af6cf88 12952 }
0e32b39c 12953
e2c719b7 12954 I915_STATE_WARN(!!encoder->base.crtc != enabled,
8af6cf88
DV
12955 "encoder's enabled state mismatch "
12956 "(expected %i, found %i)\n",
12957 !!encoder->base.crtc, enabled);
7c60d198
ML
12958
12959 if (!encoder->base.crtc) {
4d20cd86 12960 bool active;
7c60d198 12961
4d20cd86
ML
12962 active = encoder->get_hw_state(encoder, &pipe);
12963 I915_STATE_WARN(active,
12964 "encoder detached but still enabled on pipe %c.\n",
12965 pipe_name(pipe));
7c60d198 12966 }
8af6cf88 12967 }
91d1b4bd
DV
12968}
12969
12970static void
c0ead703
ML
12971verify_crtc_state(struct drm_crtc *crtc,
12972 struct drm_crtc_state *old_crtc_state,
12973 struct drm_crtc_state *new_crtc_state)
91d1b4bd 12974{
e7c84544 12975 struct drm_device *dev = crtc->dev;
fac5e23e 12976 struct drm_i915_private *dev_priv = to_i915(dev);
91d1b4bd 12977 struct intel_encoder *encoder;
e7c84544
ML
12978 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
12979 struct intel_crtc_state *pipe_config, *sw_config;
12980 struct drm_atomic_state *old_state;
12981 bool active;
045ac3b5 12982
e7c84544 12983 old_state = old_crtc_state->state;
ec2dc6a0 12984 __drm_atomic_helper_crtc_destroy_state(old_crtc_state);
e7c84544
ML
12985 pipe_config = to_intel_crtc_state(old_crtc_state);
12986 memset(pipe_config, 0, sizeof(*pipe_config));
12987 pipe_config->base.crtc = crtc;
12988 pipe_config->base.state = old_state;
8af6cf88 12989
78108b7c 12990 DRM_DEBUG_KMS("[CRTC:%d:%s]\n", crtc->base.id, crtc->name);
8af6cf88 12991
e7c84544 12992 active = dev_priv->display.get_pipe_config(intel_crtc, pipe_config);
d62cf62a 12993
e7c84544
ML
12994 /* hw state is inconsistent with the pipe quirk */
12995 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
12996 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
12997 active = new_crtc_state->active;
6c49f241 12998
e7c84544
ML
12999 I915_STATE_WARN(new_crtc_state->active != active,
13000 "crtc active state doesn't match with hw state "
13001 "(expected %i, found %i)\n", new_crtc_state->active, active);
0e8ffe1b 13002
e7c84544
ML
13003 I915_STATE_WARN(intel_crtc->active != new_crtc_state->active,
13004 "transitional active state does not match atomic hw state "
13005 "(expected %i, found %i)\n", new_crtc_state->active, intel_crtc->active);
4d20cd86 13006
e7c84544
ML
13007 for_each_encoder_on_crtc(dev, crtc, encoder) {
13008 enum pipe pipe;
4d20cd86 13009
e7c84544
ML
13010 active = encoder->get_hw_state(encoder, &pipe);
13011 I915_STATE_WARN(active != new_crtc_state->active,
13012 "[ENCODER:%i] active %i with crtc active %i\n",
13013 encoder->base.base.id, active, new_crtc_state->active);
4d20cd86 13014
e7c84544
ML
13015 I915_STATE_WARN(active && intel_crtc->pipe != pipe,
13016 "Encoder connected to wrong pipe %c\n",
13017 pipe_name(pipe));
4d20cd86 13018
253c84c8
VS
13019 if (active) {
13020 pipe_config->output_types |= 1 << encoder->type;
e7c84544 13021 encoder->get_config(encoder, pipe_config);
253c84c8 13022 }
e7c84544 13023 }
53d9f4e9 13024
e7c84544
ML
13025 if (!new_crtc_state->active)
13026 return;
cfb23ed6 13027
e7c84544 13028 intel_pipe_config_sanity_check(dev_priv, pipe_config);
e3b247da 13029
e7c84544
ML
13030 sw_config = to_intel_crtc_state(crtc->state);
13031 if (!intel_pipe_config_compare(dev, sw_config,
13032 pipe_config, false)) {
13033 I915_STATE_WARN(1, "pipe state doesn't match!\n");
13034 intel_dump_pipe_config(intel_crtc, pipe_config,
13035 "[hw state]");
13036 intel_dump_pipe_config(intel_crtc, sw_config,
13037 "[sw state]");
8af6cf88
DV
13038 }
13039}
13040
91d1b4bd 13041static void
c0ead703
ML
13042verify_single_dpll_state(struct drm_i915_private *dev_priv,
13043 struct intel_shared_dpll *pll,
13044 struct drm_crtc *crtc,
13045 struct drm_crtc_state *new_state)
91d1b4bd 13046{
91d1b4bd 13047 struct intel_dpll_hw_state dpll_hw_state;
e7c84544
ML
13048 unsigned crtc_mask;
13049 bool active;
5358901f 13050
e7c84544 13051 memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
5358901f 13052
e7c84544 13053 DRM_DEBUG_KMS("%s\n", pll->name);
5358901f 13054
e7c84544 13055 active = pll->funcs.get_hw_state(dev_priv, pll, &dpll_hw_state);
5358901f 13056
e7c84544
ML
13057 if (!(pll->flags & INTEL_DPLL_ALWAYS_ON)) {
13058 I915_STATE_WARN(!pll->on && pll->active_mask,
13059 "pll in active use but not on in sw tracking\n");
13060 I915_STATE_WARN(pll->on && !pll->active_mask,
13061 "pll is on but not used by any active crtc\n");
13062 I915_STATE_WARN(pll->on != active,
13063 "pll on state mismatch (expected %i, found %i)\n",
13064 pll->on, active);
13065 }
5358901f 13066
e7c84544 13067 if (!crtc) {
2dd66ebd 13068 I915_STATE_WARN(pll->active_mask & ~pll->config.crtc_mask,
e7c84544
ML
13069 "more active pll users than references: %x vs %x\n",
13070 pll->active_mask, pll->config.crtc_mask);
5358901f 13071
e7c84544
ML
13072 return;
13073 }
13074
13075 crtc_mask = 1 << drm_crtc_index(crtc);
13076
13077 if (new_state->active)
13078 I915_STATE_WARN(!(pll->active_mask & crtc_mask),
13079 "pll active mismatch (expected pipe %c in active mask 0x%02x)\n",
13080 pipe_name(drm_crtc_index(crtc)), pll->active_mask);
13081 else
13082 I915_STATE_WARN(pll->active_mask & crtc_mask,
13083 "pll active mismatch (didn't expect pipe %c in active mask 0x%02x)\n",
13084 pipe_name(drm_crtc_index(crtc)), pll->active_mask);
2dd66ebd 13085
e7c84544
ML
13086 I915_STATE_WARN(!(pll->config.crtc_mask & crtc_mask),
13087 "pll enabled crtcs mismatch (expected 0x%x in 0x%02x)\n",
13088 crtc_mask, pll->config.crtc_mask);
66e985c0 13089
e7c84544
ML
13090 I915_STATE_WARN(pll->on && memcmp(&pll->config.hw_state,
13091 &dpll_hw_state,
13092 sizeof(dpll_hw_state)),
13093 "pll hw state mismatch\n");
13094}
13095
13096static void
c0ead703
ML
13097verify_shared_dpll_state(struct drm_device *dev, struct drm_crtc *crtc,
13098 struct drm_crtc_state *old_crtc_state,
13099 struct drm_crtc_state *new_crtc_state)
e7c84544 13100{
fac5e23e 13101 struct drm_i915_private *dev_priv = to_i915(dev);
e7c84544
ML
13102 struct intel_crtc_state *old_state = to_intel_crtc_state(old_crtc_state);
13103 struct intel_crtc_state *new_state = to_intel_crtc_state(new_crtc_state);
13104
13105 if (new_state->shared_dpll)
c0ead703 13106 verify_single_dpll_state(dev_priv, new_state->shared_dpll, crtc, new_crtc_state);
e7c84544
ML
13107
13108 if (old_state->shared_dpll &&
13109 old_state->shared_dpll != new_state->shared_dpll) {
13110 unsigned crtc_mask = 1 << drm_crtc_index(crtc);
13111 struct intel_shared_dpll *pll = old_state->shared_dpll;
13112
13113 I915_STATE_WARN(pll->active_mask & crtc_mask,
13114 "pll active mismatch (didn't expect pipe %c in active mask)\n",
13115 pipe_name(drm_crtc_index(crtc)));
13116 I915_STATE_WARN(pll->config.crtc_mask & crtc_mask,
13117 "pll enabled crtcs mismatch (found %x in enabled mask)\n",
13118 pipe_name(drm_crtc_index(crtc)));
5358901f 13119 }
8af6cf88
DV
13120}
13121
e7c84544 13122static void
c0ead703 13123intel_modeset_verify_crtc(struct drm_crtc *crtc,
e7c84544
ML
13124 struct drm_crtc_state *old_state,
13125 struct drm_crtc_state *new_state)
13126{
5a21b665
DV
13127 if (!needs_modeset(new_state) &&
13128 !to_intel_crtc_state(new_state)->update_pipe)
13129 return;
13130
c0ead703 13131 verify_wm_state(crtc, new_state);
5a21b665 13132 verify_connector_state(crtc->dev, crtc);
c0ead703
ML
13133 verify_crtc_state(crtc, old_state, new_state);
13134 verify_shared_dpll_state(crtc->dev, crtc, old_state, new_state);
e7c84544
ML
13135}
13136
13137static void
c0ead703 13138verify_disabled_dpll_state(struct drm_device *dev)
e7c84544 13139{
fac5e23e 13140 struct drm_i915_private *dev_priv = to_i915(dev);
e7c84544
ML
13141 int i;
13142
13143 for (i = 0; i < dev_priv->num_shared_dpll; i++)
c0ead703 13144 verify_single_dpll_state(dev_priv, &dev_priv->shared_dplls[i], NULL, NULL);
e7c84544
ML
13145}
13146
13147static void
c0ead703 13148intel_modeset_verify_disabled(struct drm_device *dev)
e7c84544 13149{
c0ead703
ML
13150 verify_encoder_state(dev);
13151 verify_connector_state(dev, NULL);
13152 verify_disabled_dpll_state(dev);
e7c84544
ML
13153}
13154
80715b2f
VS
13155static void update_scanline_offset(struct intel_crtc *crtc)
13156{
13157 struct drm_device *dev = crtc->base.dev;
13158
13159 /*
13160 * The scanline counter increments at the leading edge of hsync.
13161 *
13162 * On most platforms it starts counting from vtotal-1 on the
13163 * first active line. That means the scanline counter value is
13164 * always one less than what we would expect. Ie. just after
13165 * start of vblank, which also occurs at start of hsync (on the
13166 * last active line), the scanline counter will read vblank_start-1.
13167 *
13168 * On gen2 the scanline counter starts counting from 1 instead
13169 * of vtotal-1, so we have to subtract one (or rather add vtotal-1
13170 * to keep the value positive), instead of adding one.
13171 *
13172 * On HSW+ the behaviour of the scanline counter depends on the output
13173 * type. For DP ports it behaves like most other platforms, but on HDMI
13174 * there's an extra 1 line difference. So we need to add two instead of
13175 * one to the value.
13176 */
13177 if (IS_GEN2(dev)) {
124abe07 13178 const struct drm_display_mode *adjusted_mode = &crtc->config->base.adjusted_mode;
80715b2f
VS
13179 int vtotal;
13180
124abe07
VS
13181 vtotal = adjusted_mode->crtc_vtotal;
13182 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
80715b2f
VS
13183 vtotal /= 2;
13184
13185 crtc->scanline_offset = vtotal - 1;
13186 } else if (HAS_DDI(dev) &&
2d84d2b3 13187 intel_crtc_has_type(crtc->config, INTEL_OUTPUT_HDMI)) {
80715b2f
VS
13188 crtc->scanline_offset = 2;
13189 } else
13190 crtc->scanline_offset = 1;
13191}
13192
ad421372 13193static void intel_modeset_clear_plls(struct drm_atomic_state *state)
ed6739ef 13194{
225da59b 13195 struct drm_device *dev = state->dev;
ed6739ef 13196 struct drm_i915_private *dev_priv = to_i915(dev);
ad421372 13197 struct intel_shared_dpll_config *shared_dpll = NULL;
0a9ab303
ACO
13198 struct drm_crtc *crtc;
13199 struct drm_crtc_state *crtc_state;
0a9ab303 13200 int i;
ed6739ef
ACO
13201
13202 if (!dev_priv->display.crtc_compute_clock)
ad421372 13203 return;
ed6739ef 13204
0a9ab303 13205 for_each_crtc_in_state(state, crtc, crtc_state, i) {
fb1a38a9 13206 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8106ddbd
ACO
13207 struct intel_shared_dpll *old_dpll =
13208 to_intel_crtc_state(crtc->state)->shared_dpll;
0a9ab303 13209
fb1a38a9 13210 if (!needs_modeset(crtc_state))
225da59b
ACO
13211 continue;
13212
8106ddbd 13213 to_intel_crtc_state(crtc_state)->shared_dpll = NULL;
fb1a38a9 13214
8106ddbd 13215 if (!old_dpll)
fb1a38a9 13216 continue;
0a9ab303 13217
ad421372
ML
13218 if (!shared_dpll)
13219 shared_dpll = intel_atomic_get_shared_dpll_state(state);
ed6739ef 13220
8106ddbd 13221 intel_shared_dpll_config_put(shared_dpll, old_dpll, intel_crtc);
ad421372 13222 }
ed6739ef
ACO
13223}
13224
99d736a2
ML
13225/*
13226 * This implements the workaround described in the "notes" section of the mode
13227 * set sequence documentation. When going from no pipes or single pipe to
13228 * multiple pipes, and planes are enabled after the pipe, we need to wait at
13229 * least 2 vblanks on the first pipe before enabling planes on the second pipe.
13230 */
13231static int haswell_mode_set_planes_workaround(struct drm_atomic_state *state)
13232{
13233 struct drm_crtc_state *crtc_state;
13234 struct intel_crtc *intel_crtc;
13235 struct drm_crtc *crtc;
13236 struct intel_crtc_state *first_crtc_state = NULL;
13237 struct intel_crtc_state *other_crtc_state = NULL;
13238 enum pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
13239 int i;
13240
13241 /* look at all crtc's that are going to be enabled in during modeset */
13242 for_each_crtc_in_state(state, crtc, crtc_state, i) {
13243 intel_crtc = to_intel_crtc(crtc);
13244
13245 if (!crtc_state->active || !needs_modeset(crtc_state))
13246 continue;
13247
13248 if (first_crtc_state) {
13249 other_crtc_state = to_intel_crtc_state(crtc_state);
13250 break;
13251 } else {
13252 first_crtc_state = to_intel_crtc_state(crtc_state);
13253 first_pipe = intel_crtc->pipe;
13254 }
13255 }
13256
13257 /* No workaround needed? */
13258 if (!first_crtc_state)
13259 return 0;
13260
13261 /* w/a possibly needed, check how many crtc's are already enabled. */
13262 for_each_intel_crtc(state->dev, intel_crtc) {
13263 struct intel_crtc_state *pipe_config;
13264
13265 pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
13266 if (IS_ERR(pipe_config))
13267 return PTR_ERR(pipe_config);
13268
13269 pipe_config->hsw_workaround_pipe = INVALID_PIPE;
13270
13271 if (!pipe_config->base.active ||
13272 needs_modeset(&pipe_config->base))
13273 continue;
13274
13275 /* 2 or more enabled crtcs means no need for w/a */
13276 if (enabled_pipe != INVALID_PIPE)
13277 return 0;
13278
13279 enabled_pipe = intel_crtc->pipe;
13280 }
13281
13282 if (enabled_pipe != INVALID_PIPE)
13283 first_crtc_state->hsw_workaround_pipe = enabled_pipe;
13284 else if (other_crtc_state)
13285 other_crtc_state->hsw_workaround_pipe = first_pipe;
13286
13287 return 0;
13288}
13289
27c329ed
ML
13290static int intel_modeset_all_pipes(struct drm_atomic_state *state)
13291{
13292 struct drm_crtc *crtc;
13293 struct drm_crtc_state *crtc_state;
13294 int ret = 0;
13295
13296 /* add all active pipes to the state */
13297 for_each_crtc(state->dev, crtc) {
13298 crtc_state = drm_atomic_get_crtc_state(state, crtc);
13299 if (IS_ERR(crtc_state))
13300 return PTR_ERR(crtc_state);
13301
13302 if (!crtc_state->active || needs_modeset(crtc_state))
13303 continue;
13304
13305 crtc_state->mode_changed = true;
13306
13307 ret = drm_atomic_add_affected_connectors(state, crtc);
13308 if (ret)
13309 break;
13310
13311 ret = drm_atomic_add_affected_planes(state, crtc);
13312 if (ret)
13313 break;
13314 }
13315
13316 return ret;
13317}
13318
c347a676 13319static int intel_modeset_checks(struct drm_atomic_state *state)
054518dd 13320{
565602d7 13321 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
fac5e23e 13322 struct drm_i915_private *dev_priv = to_i915(state->dev);
565602d7
ML
13323 struct drm_crtc *crtc;
13324 struct drm_crtc_state *crtc_state;
13325 int ret = 0, i;
054518dd 13326
b359283a
ML
13327 if (!check_digital_port_conflicts(state)) {
13328 DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
13329 return -EINVAL;
13330 }
13331
565602d7
ML
13332 intel_state->modeset = true;
13333 intel_state->active_crtcs = dev_priv->active_crtcs;
13334
13335 for_each_crtc_in_state(state, crtc, crtc_state, i) {
13336 if (crtc_state->active)
13337 intel_state->active_crtcs |= 1 << i;
13338 else
13339 intel_state->active_crtcs &= ~(1 << i);
8b4a7d05
MR
13340
13341 if (crtc_state->active != crtc->state->active)
13342 intel_state->active_pipe_changes |= drm_crtc_mask(crtc);
565602d7
ML
13343 }
13344
054518dd
ACO
13345 /*
13346 * See if the config requires any additional preparation, e.g.
13347 * to adjust global state with pipes off. We need to do this
13348 * here so we can get the modeset_pipe updated config for the new
13349 * mode set on this crtc. For other crtcs we need to use the
13350 * adjusted_mode bits in the crtc directly.
13351 */
27c329ed 13352 if (dev_priv->display.modeset_calc_cdclk) {
c89e39f3 13353 if (!intel_state->cdclk_pll_vco)
63911d72 13354 intel_state->cdclk_pll_vco = dev_priv->cdclk_pll.vco;
b2045352
VS
13355 if (!intel_state->cdclk_pll_vco)
13356 intel_state->cdclk_pll_vco = dev_priv->skl_preferred_vco_freq;
c89e39f3 13357
27c329ed 13358 ret = dev_priv->display.modeset_calc_cdclk(state);
c89e39f3
CT
13359 if (ret < 0)
13360 return ret;
27c329ed 13361
c89e39f3 13362 if (intel_state->dev_cdclk != dev_priv->cdclk_freq ||
63911d72 13363 intel_state->cdclk_pll_vco != dev_priv->cdclk_pll.vco)
27c329ed
ML
13364 ret = intel_modeset_all_pipes(state);
13365
13366 if (ret < 0)
054518dd 13367 return ret;
e8788cbc
ML
13368
13369 DRM_DEBUG_KMS("New cdclk calculated to be atomic %u, actual %u\n",
13370 intel_state->cdclk, intel_state->dev_cdclk);
27c329ed 13371 } else
1a617b77 13372 to_intel_atomic_state(state)->cdclk = dev_priv->atomic_cdclk_freq;
054518dd 13373
ad421372 13374 intel_modeset_clear_plls(state);
054518dd 13375
565602d7 13376 if (IS_HASWELL(dev_priv))
ad421372 13377 return haswell_mode_set_planes_workaround(state);
99d736a2 13378
ad421372 13379 return 0;
c347a676
ACO
13380}
13381
aa363136
MR
13382/*
13383 * Handle calculation of various watermark data at the end of the atomic check
13384 * phase. The code here should be run after the per-crtc and per-plane 'check'
13385 * handlers to ensure that all derived state has been updated.
13386 */
55994c2c 13387static int calc_watermark_data(struct drm_atomic_state *state)
aa363136
MR
13388{
13389 struct drm_device *dev = state->dev;
98d39494 13390 struct drm_i915_private *dev_priv = to_i915(dev);
98d39494
MR
13391
13392 /* Is there platform-specific watermark information to calculate? */
13393 if (dev_priv->display.compute_global_watermarks)
55994c2c
MR
13394 return dev_priv->display.compute_global_watermarks(state);
13395
13396 return 0;
aa363136
MR
13397}
13398
74c090b1
ML
13399/**
13400 * intel_atomic_check - validate state object
13401 * @dev: drm device
13402 * @state: state to validate
13403 */
13404static int intel_atomic_check(struct drm_device *dev,
13405 struct drm_atomic_state *state)
c347a676 13406{
dd8b3bdb 13407 struct drm_i915_private *dev_priv = to_i915(dev);
aa363136 13408 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
c347a676
ACO
13409 struct drm_crtc *crtc;
13410 struct drm_crtc_state *crtc_state;
13411 int ret, i;
61333b60 13412 bool any_ms = false;
c347a676 13413
74c090b1 13414 ret = drm_atomic_helper_check_modeset(dev, state);
054518dd
ACO
13415 if (ret)
13416 return ret;
13417
c347a676 13418 for_each_crtc_in_state(state, crtc, crtc_state, i) {
cfb23ed6
ML
13419 struct intel_crtc_state *pipe_config =
13420 to_intel_crtc_state(crtc_state);
1ed51de9
DV
13421
13422 /* Catch I915_MODE_FLAG_INHERITED */
13423 if (crtc_state->mode.private_flags != crtc->state->mode.private_flags)
13424 crtc_state->mode_changed = true;
cfb23ed6 13425
af4a879e 13426 if (!needs_modeset(crtc_state))
c347a676
ACO
13427 continue;
13428
af4a879e
DV
13429 if (!crtc_state->enable) {
13430 any_ms = true;
cfb23ed6 13431 continue;
af4a879e 13432 }
cfb23ed6 13433
26495481
DV
13434 /* FIXME: For only active_changed we shouldn't need to do any
13435 * state recomputation at all. */
13436
1ed51de9
DV
13437 ret = drm_atomic_add_affected_connectors(state, crtc);
13438 if (ret)
13439 return ret;
b359283a 13440
cfb23ed6 13441 ret = intel_modeset_pipe_config(crtc, pipe_config);
25aa1c39
ML
13442 if (ret) {
13443 intel_dump_pipe_config(to_intel_crtc(crtc),
13444 pipe_config, "[failed]");
c347a676 13445 return ret;
25aa1c39 13446 }
c347a676 13447
73831236 13448 if (i915.fastboot &&
dd8b3bdb 13449 intel_pipe_config_compare(dev,
cfb23ed6 13450 to_intel_crtc_state(crtc->state),
1ed51de9 13451 pipe_config, true)) {
26495481 13452 crtc_state->mode_changed = false;
bfd16b2a 13453 to_intel_crtc_state(crtc_state)->update_pipe = true;
26495481
DV
13454 }
13455
af4a879e 13456 if (needs_modeset(crtc_state))
26495481 13457 any_ms = true;
cfb23ed6 13458
af4a879e
DV
13459 ret = drm_atomic_add_affected_planes(state, crtc);
13460 if (ret)
13461 return ret;
61333b60 13462
26495481
DV
13463 intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
13464 needs_modeset(crtc_state) ?
13465 "[modeset]" : "[fastset]");
c347a676
ACO
13466 }
13467
61333b60
ML
13468 if (any_ms) {
13469 ret = intel_modeset_checks(state);
13470
13471 if (ret)
13472 return ret;
27c329ed 13473 } else
dd8b3bdb 13474 intel_state->cdclk = dev_priv->cdclk_freq;
76305b1a 13475
dd8b3bdb 13476 ret = drm_atomic_helper_check_planes(dev, state);
aa363136
MR
13477 if (ret)
13478 return ret;
13479
f51be2e0 13480 intel_fbc_choose_crtc(dev_priv, state);
55994c2c 13481 return calc_watermark_data(state);
054518dd
ACO
13482}
13483
5008e874
ML
13484static int intel_atomic_prepare_commit(struct drm_device *dev,
13485 struct drm_atomic_state *state,
81072bfd 13486 bool nonblock)
5008e874 13487{
fac5e23e 13488 struct drm_i915_private *dev_priv = to_i915(dev);
7580d774 13489 struct drm_plane_state *plane_state;
5008e874 13490 struct drm_crtc_state *crtc_state;
7580d774 13491 struct drm_plane *plane;
5008e874
ML
13492 struct drm_crtc *crtc;
13493 int i, ret;
13494
5a21b665
DV
13495 for_each_crtc_in_state(state, crtc, crtc_state, i) {
13496 if (state->legacy_cursor_update)
a6747b73
ML
13497 continue;
13498
5a21b665
DV
13499 ret = intel_crtc_wait_for_pending_flips(crtc);
13500 if (ret)
13501 return ret;
5008e874 13502
5a21b665
DV
13503 if (atomic_read(&to_intel_crtc(crtc)->unpin_work_count) >= 2)
13504 flush_workqueue(dev_priv->wq);
d55dbd06
ML
13505 }
13506
f935675f
ML
13507 ret = mutex_lock_interruptible(&dev->struct_mutex);
13508 if (ret)
13509 return ret;
13510
5008e874 13511 ret = drm_atomic_helper_prepare_planes(dev, state);
f7e5838b 13512 mutex_unlock(&dev->struct_mutex);
7580d774 13513
21daaeee 13514 if (!ret && !nonblock) {
7580d774
ML
13515 for_each_plane_in_state(state, plane, plane_state, i) {
13516 struct intel_plane_state *intel_plane_state =
13517 to_intel_plane_state(plane_state);
13518
13519 if (!intel_plane_state->wait_req)
13520 continue;
13521
776f3236
CW
13522 ret = i915_wait_request(intel_plane_state->wait_req,
13523 true, NULL, NULL);
f7e5838b 13524 if (ret) {
f4457ae7
CW
13525 /* Any hang should be swallowed by the wait */
13526 WARN_ON(ret == -EIO);
f7e5838b
CW
13527 mutex_lock(&dev->struct_mutex);
13528 drm_atomic_helper_cleanup_planes(dev, state);
13529 mutex_unlock(&dev->struct_mutex);
7580d774 13530 break;
f7e5838b 13531 }
7580d774 13532 }
7580d774 13533 }
5008e874
ML
13534
13535 return ret;
13536}
13537
a2991414
ML
13538u32 intel_crtc_get_vblank_counter(struct intel_crtc *crtc)
13539{
13540 struct drm_device *dev = crtc->base.dev;
13541
13542 if (!dev->max_vblank_count)
13543 return drm_accurate_vblank_count(&crtc->base);
13544
13545 return dev->driver->get_vblank_counter(dev, crtc->pipe);
13546}
13547
5a21b665
DV
13548static void intel_atomic_wait_for_vblanks(struct drm_device *dev,
13549 struct drm_i915_private *dev_priv,
13550 unsigned crtc_mask)
e8861675 13551{
5a21b665
DV
13552 unsigned last_vblank_count[I915_MAX_PIPES];
13553 enum pipe pipe;
13554 int ret;
e8861675 13555
5a21b665
DV
13556 if (!crtc_mask)
13557 return;
e8861675 13558
5a21b665
DV
13559 for_each_pipe(dev_priv, pipe) {
13560 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
e8861675 13561
5a21b665 13562 if (!((1 << pipe) & crtc_mask))
e8861675
ML
13563 continue;
13564
5a21b665
DV
13565 ret = drm_crtc_vblank_get(crtc);
13566 if (WARN_ON(ret != 0)) {
13567 crtc_mask &= ~(1 << pipe);
13568 continue;
e8861675
ML
13569 }
13570
5a21b665 13571 last_vblank_count[pipe] = drm_crtc_vblank_count(crtc);
e8861675
ML
13572 }
13573
5a21b665
DV
13574 for_each_pipe(dev_priv, pipe) {
13575 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
13576 long lret;
e8861675 13577
5a21b665
DV
13578 if (!((1 << pipe) & crtc_mask))
13579 continue;
d55dbd06 13580
5a21b665
DV
13581 lret = wait_event_timeout(dev->vblank[pipe].queue,
13582 last_vblank_count[pipe] !=
13583 drm_crtc_vblank_count(crtc),
13584 msecs_to_jiffies(50));
d55dbd06 13585
5a21b665 13586 WARN(!lret, "pipe %c vblank wait timed out\n", pipe_name(pipe));
d55dbd06 13587
5a21b665 13588 drm_crtc_vblank_put(crtc);
d55dbd06
ML
13589 }
13590}
13591
5a21b665 13592static bool needs_vblank_wait(struct intel_crtc_state *crtc_state)
a6747b73 13593{
5a21b665
DV
13594 /* fb updated, need to unpin old fb */
13595 if (crtc_state->fb_changed)
13596 return true;
a6747b73 13597
5a21b665
DV
13598 /* wm changes, need vblank before final wm's */
13599 if (crtc_state->update_wm_post)
13600 return true;
a6747b73 13601
5a21b665
DV
13602 /*
13603 * cxsr is re-enabled after vblank.
13604 * This is already handled by crtc_state->update_wm_post,
13605 * but added for clarity.
13606 */
13607 if (crtc_state->disable_cxsr)
13608 return true;
a6747b73 13609
5a21b665 13610 return false;
e8861675
ML
13611}
13612
94f05024 13613static void intel_atomic_commit_tail(struct drm_atomic_state *state)
a6778b3c 13614{
94f05024 13615 struct drm_device *dev = state->dev;
565602d7 13616 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
fac5e23e 13617 struct drm_i915_private *dev_priv = to_i915(dev);
29ceb0e6 13618 struct drm_crtc_state *old_crtc_state;
7580d774 13619 struct drm_crtc *crtc;
5a21b665 13620 struct intel_crtc_state *intel_cstate;
94f05024
DV
13621 struct drm_plane *plane;
13622 struct drm_plane_state *plane_state;
5a21b665
DV
13623 bool hw_check = intel_state->modeset;
13624 unsigned long put_domains[I915_MAX_PIPES] = {};
13625 unsigned crtc_vblank_mask = 0;
94f05024 13626 int i, ret;
a6778b3c 13627
94f05024
DV
13628 for_each_plane_in_state(state, plane, plane_state, i) {
13629 struct intel_plane_state *intel_plane_state =
13630 to_intel_plane_state(plane_state);
ea0000f0 13631
94f05024
DV
13632 if (!intel_plane_state->wait_req)
13633 continue;
d4afb8cc 13634
776f3236
CW
13635 ret = i915_wait_request(intel_plane_state->wait_req,
13636 true, NULL, NULL);
94f05024
DV
13637 /* EIO should be eaten, and we can't get interrupted in the
13638 * worker, and blocking commits have waited already. */
13639 WARN_ON(ret);
13640 }
1c5e19f8 13641
ea0000f0
DV
13642 drm_atomic_helper_wait_for_dependencies(state);
13643
565602d7
ML
13644 if (intel_state->modeset) {
13645 memcpy(dev_priv->min_pixclk, intel_state->min_pixclk,
13646 sizeof(intel_state->min_pixclk));
13647 dev_priv->active_crtcs = intel_state->active_crtcs;
1a617b77 13648 dev_priv->atomic_cdclk_freq = intel_state->cdclk;
5a21b665
DV
13649
13650 intel_display_power_get(dev_priv, POWER_DOMAIN_MODESET);
565602d7
ML
13651 }
13652
29ceb0e6 13653 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
a539205a
ML
13654 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13655
5a21b665
DV
13656 if (needs_modeset(crtc->state) ||
13657 to_intel_crtc_state(crtc->state)->update_pipe) {
13658 hw_check = true;
13659
13660 put_domains[to_intel_crtc(crtc)->pipe] =
13661 modeset_get_crtc_power_domains(crtc,
13662 to_intel_crtc_state(crtc->state));
13663 }
13664
61333b60
ML
13665 if (!needs_modeset(crtc->state))
13666 continue;
13667
29ceb0e6 13668 intel_pre_plane_update(to_intel_crtc_state(old_crtc_state));
460da916 13669
29ceb0e6
VS
13670 if (old_crtc_state->active) {
13671 intel_crtc_disable_planes(crtc, old_crtc_state->plane_mask);
a539205a 13672 dev_priv->display.crtc_disable(crtc);
eddfcbcd 13673 intel_crtc->active = false;
58f9c0bc 13674 intel_fbc_disable(intel_crtc);
eddfcbcd 13675 intel_disable_shared_dpll(intel_crtc);
9bbc8258
VS
13676
13677 /*
13678 * Underruns don't always raise
13679 * interrupts, so check manually.
13680 */
13681 intel_check_cpu_fifo_underruns(dev_priv);
13682 intel_check_pch_fifo_underruns(dev_priv);
b9001114
ML
13683
13684 if (!crtc->state->active)
13685 intel_update_watermarks(crtc);
a539205a 13686 }
b8cecdf5 13687 }
7758a113 13688
ea9d758d
DV
13689 /* Only after disabling all output pipelines that will be changed can we
13690 * update the the output configuration. */
4740b0f2 13691 intel_modeset_update_crtc_state(state);
f6e5b160 13692
565602d7 13693 if (intel_state->modeset) {
4740b0f2 13694 drm_atomic_helper_update_legacy_modeset_state(state->dev, state);
33c8df89
ML
13695
13696 if (dev_priv->display.modeset_commit_cdclk &&
c89e39f3 13697 (intel_state->dev_cdclk != dev_priv->cdclk_freq ||
63911d72 13698 intel_state->cdclk_pll_vco != dev_priv->cdclk_pll.vco))
33c8df89 13699 dev_priv->display.modeset_commit_cdclk(state);
f6d1973d 13700
c0ead703 13701 intel_modeset_verify_disabled(dev);
4740b0f2 13702 }
47fab737 13703
a6778b3c 13704 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
29ceb0e6 13705 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
f6ac4b2a
ML
13706 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13707 bool modeset = needs_modeset(crtc->state);
5a21b665
DV
13708 struct intel_crtc_state *pipe_config =
13709 to_intel_crtc_state(crtc->state);
9f836f90 13710
f6ac4b2a 13711 if (modeset && crtc->state->active) {
a539205a
ML
13712 update_scanline_offset(to_intel_crtc(crtc));
13713 dev_priv->display.crtc_enable(crtc);
13714 }
80715b2f 13715
1f7528c4
DV
13716 /* Complete events for now disable pipes here. */
13717 if (modeset && !crtc->state->active && crtc->state->event) {
13718 spin_lock_irq(&dev->event_lock);
13719 drm_crtc_send_vblank_event(crtc, crtc->state->event);
13720 spin_unlock_irq(&dev->event_lock);
13721
13722 crtc->state->event = NULL;
13723 }
13724
f6ac4b2a 13725 if (!modeset)
29ceb0e6 13726 intel_pre_plane_update(to_intel_crtc_state(old_crtc_state));
f6ac4b2a 13727
5a21b665
DV
13728 if (crtc->state->active &&
13729 drm_atomic_get_existing_plane_state(state, crtc->primary))
faf68d92 13730 intel_fbc_enable(intel_crtc, pipe_config, to_intel_plane_state(crtc->primary->state));
5a21b665 13731
1f7528c4 13732 if (crtc->state->active)
5a21b665 13733 drm_atomic_helper_commit_planes_on_crtc(old_crtc_state);
f6d1973d 13734
5a21b665
DV
13735 if (pipe_config->base.active && needs_vblank_wait(pipe_config))
13736 crtc_vblank_mask |= 1 << i;
177246a8
MR
13737 }
13738
94f05024
DV
13739 /* FIXME: We should call drm_atomic_helper_commit_hw_done() here
13740 * already, but still need the state for the delayed optimization. To
13741 * fix this:
13742 * - wrap the optimization/post_plane_update stuff into a per-crtc work.
13743 * - schedule that vblank worker _before_ calling hw_done
13744 * - at the start of commit_tail, cancel it _synchrously
13745 * - switch over to the vblank wait helper in the core after that since
13746 * we don't need out special handling any more.
13747 */
5a21b665
DV
13748 if (!state->legacy_cursor_update)
13749 intel_atomic_wait_for_vblanks(dev, dev_priv, crtc_vblank_mask);
13750
13751 /*
13752 * Now that the vblank has passed, we can go ahead and program the
13753 * optimal watermarks on platforms that need two-step watermark
13754 * programming.
13755 *
13756 * TODO: Move this (and other cleanup) to an async worker eventually.
13757 */
13758 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
13759 intel_cstate = to_intel_crtc_state(crtc->state);
13760
13761 if (dev_priv->display.optimize_watermarks)
13762 dev_priv->display.optimize_watermarks(intel_cstate);
13763 }
13764
13765 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
13766 intel_post_plane_update(to_intel_crtc_state(old_crtc_state));
13767
13768 if (put_domains[i])
13769 modeset_put_power_domains(dev_priv, put_domains[i]);
13770
13771 intel_modeset_verify_crtc(crtc, old_crtc_state, crtc->state);
13772 }
13773
94f05024
DV
13774 drm_atomic_helper_commit_hw_done(state);
13775
5a21b665
DV
13776 if (intel_state->modeset)
13777 intel_display_power_put(dev_priv, POWER_DOMAIN_MODESET);
13778
13779 mutex_lock(&dev->struct_mutex);
13780 drm_atomic_helper_cleanup_planes(dev, state);
13781 mutex_unlock(&dev->struct_mutex);
13782
ea0000f0
DV
13783 drm_atomic_helper_commit_cleanup_done(state);
13784
ee165b1a 13785 drm_atomic_state_free(state);
f30da187 13786
75714940
MK
13787 /* As one of the primary mmio accessors, KMS has a high likelihood
13788 * of triggering bugs in unclaimed access. After we finish
13789 * modesetting, see if an error has been flagged, and if so
13790 * enable debugging for the next modeset - and hope we catch
13791 * the culprit.
13792 *
13793 * XXX note that we assume display power is on at this point.
13794 * This might hold true now but we need to add pm helper to check
13795 * unclaimed only when the hardware is on, as atomic commits
13796 * can happen also when the device is completely off.
13797 */
13798 intel_uncore_arm_unclaimed_mmio_detection(dev_priv);
94f05024
DV
13799}
13800
13801static void intel_atomic_commit_work(struct work_struct *work)
13802{
13803 struct drm_atomic_state *state = container_of(work,
13804 struct drm_atomic_state,
13805 commit_work);
13806 intel_atomic_commit_tail(state);
13807}
13808
6c9c1b38
DV
13809static void intel_atomic_track_fbs(struct drm_atomic_state *state)
13810{
13811 struct drm_plane_state *old_plane_state;
13812 struct drm_plane *plane;
6c9c1b38
DV
13813 int i;
13814
faf5bf0a
CW
13815 for_each_plane_in_state(state, plane, old_plane_state, i)
13816 i915_gem_track_fb(intel_fb_obj(old_plane_state->fb),
13817 intel_fb_obj(plane->state->fb),
13818 to_intel_plane(plane)->frontbuffer_bit);
6c9c1b38
DV
13819}
13820
94f05024
DV
13821/**
13822 * intel_atomic_commit - commit validated state object
13823 * @dev: DRM device
13824 * @state: the top-level driver state object
13825 * @nonblock: nonblocking commit
13826 *
13827 * This function commits a top-level state object that has been validated
13828 * with drm_atomic_helper_check().
13829 *
13830 * FIXME: Atomic modeset support for i915 is not yet complete. At the moment
13831 * nonblocking commits are only safe for pure plane updates. Everything else
13832 * should work though.
13833 *
13834 * RETURNS
13835 * Zero for success or -errno.
13836 */
13837static int intel_atomic_commit(struct drm_device *dev,
13838 struct drm_atomic_state *state,
13839 bool nonblock)
13840{
13841 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
fac5e23e 13842 struct drm_i915_private *dev_priv = to_i915(dev);
94f05024
DV
13843 int ret = 0;
13844
13845 if (intel_state->modeset && nonblock) {
13846 DRM_DEBUG_KMS("nonblocking commit for modeset not yet implemented.\n");
13847 return -EINVAL;
13848 }
13849
13850 ret = drm_atomic_helper_setup_commit(state, nonblock);
13851 if (ret)
13852 return ret;
13853
13854 INIT_WORK(&state->commit_work, intel_atomic_commit_work);
13855
13856 ret = intel_atomic_prepare_commit(dev, state, nonblock);
13857 if (ret) {
13858 DRM_DEBUG_ATOMIC("Preparing state failed with %i\n", ret);
13859 return ret;
13860 }
13861
13862 drm_atomic_helper_swap_state(state, true);
13863 dev_priv->wm.distrust_bios_wm = false;
13864 dev_priv->wm.skl_results = intel_state->wm_results;
13865 intel_shared_dpll_commit(state);
6c9c1b38 13866 intel_atomic_track_fbs(state);
94f05024
DV
13867
13868 if (nonblock)
13869 queue_work(system_unbound_wq, &state->commit_work);
13870 else
13871 intel_atomic_commit_tail(state);
75714940 13872
74c090b1 13873 return 0;
7f27126e
JB
13874}
13875
c0c36b94
CW
13876void intel_crtc_restore_mode(struct drm_crtc *crtc)
13877{
83a57153
ACO
13878 struct drm_device *dev = crtc->dev;
13879 struct drm_atomic_state *state;
e694eb02 13880 struct drm_crtc_state *crtc_state;
2bfb4627 13881 int ret;
83a57153
ACO
13882
13883 state = drm_atomic_state_alloc(dev);
13884 if (!state) {
78108b7c
VS
13885 DRM_DEBUG_KMS("[CRTC:%d:%s] crtc restore failed, out of memory",
13886 crtc->base.id, crtc->name);
83a57153
ACO
13887 return;
13888 }
13889
e694eb02 13890 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
83a57153 13891
e694eb02
ML
13892retry:
13893 crtc_state = drm_atomic_get_crtc_state(state, crtc);
13894 ret = PTR_ERR_OR_ZERO(crtc_state);
13895 if (!ret) {
13896 if (!crtc_state->active)
13897 goto out;
83a57153 13898
e694eb02 13899 crtc_state->mode_changed = true;
74c090b1 13900 ret = drm_atomic_commit(state);
83a57153
ACO
13901 }
13902
e694eb02
ML
13903 if (ret == -EDEADLK) {
13904 drm_atomic_state_clear(state);
13905 drm_modeset_backoff(state->acquire_ctx);
13906 goto retry;
4ed9fb37 13907 }
4be07317 13908
2bfb4627 13909 if (ret)
e694eb02 13910out:
2bfb4627 13911 drm_atomic_state_free(state);
c0c36b94
CW
13912}
13913
25c5b266
DV
13914#undef for_each_intel_crtc_masked
13915
a8784875
BP
13916/*
13917 * FIXME: Remove this once i915 is fully DRIVER_ATOMIC by calling
13918 * drm_atomic_helper_legacy_gamma_set() directly.
13919 */
13920static int intel_atomic_legacy_gamma_set(struct drm_crtc *crtc,
13921 u16 *red, u16 *green, u16 *blue,
13922 uint32_t size)
13923{
13924 struct drm_device *dev = crtc->dev;
13925 struct drm_mode_config *config = &dev->mode_config;
13926 struct drm_crtc_state *state;
13927 int ret;
13928
13929 ret = drm_atomic_helper_legacy_gamma_set(crtc, red, green, blue, size);
13930 if (ret)
13931 return ret;
13932
13933 /*
13934 * Make sure we update the legacy properties so this works when
13935 * atomic is not enabled.
13936 */
13937
13938 state = crtc->state;
13939
13940 drm_object_property_set_value(&crtc->base,
13941 config->degamma_lut_property,
13942 (state->degamma_lut) ?
13943 state->degamma_lut->base.id : 0);
13944
13945 drm_object_property_set_value(&crtc->base,
13946 config->ctm_property,
13947 (state->ctm) ?
13948 state->ctm->base.id : 0);
13949
13950 drm_object_property_set_value(&crtc->base,
13951 config->gamma_lut_property,
13952 (state->gamma_lut) ?
13953 state->gamma_lut->base.id : 0);
13954
13955 return 0;
13956}
13957
f6e5b160 13958static const struct drm_crtc_funcs intel_crtc_funcs = {
a8784875 13959 .gamma_set = intel_atomic_legacy_gamma_set,
74c090b1 13960 .set_config = drm_atomic_helper_set_config,
82cf435b 13961 .set_property = drm_atomic_helper_crtc_set_property,
f6e5b160 13962 .destroy = intel_crtc_destroy,
527b6abe 13963 .page_flip = intel_crtc_page_flip,
1356837e
MR
13964 .atomic_duplicate_state = intel_crtc_duplicate_state,
13965 .atomic_destroy_state = intel_crtc_destroy_state,
f6e5b160
CW
13966};
13967
6beb8c23
MR
13968/**
13969 * intel_prepare_plane_fb - Prepare fb for usage on plane
13970 * @plane: drm plane to prepare for
13971 * @fb: framebuffer to prepare for presentation
13972 *
13973 * Prepares a framebuffer for usage on a display plane. Generally this
13974 * involves pinning the underlying object and updating the frontbuffer tracking
13975 * bits. Some older platforms need special physical address handling for
13976 * cursor planes.
13977 *
f935675f
ML
13978 * Must be called with struct_mutex held.
13979 *
6beb8c23
MR
13980 * Returns 0 on success, negative error code on failure.
13981 */
13982int
13983intel_prepare_plane_fb(struct drm_plane *plane,
d136dfee 13984 const struct drm_plane_state *new_state)
465c120c
MR
13985{
13986 struct drm_device *dev = plane->dev;
844f9111 13987 struct drm_framebuffer *fb = new_state->fb;
6beb8c23 13988 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
1ee49399 13989 struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->state->fb);
c37efb99 13990 struct reservation_object *resv;
6beb8c23 13991 int ret = 0;
465c120c 13992
1ee49399 13993 if (!obj && !old_obj)
465c120c
MR
13994 return 0;
13995
5008e874
ML
13996 if (old_obj) {
13997 struct drm_crtc_state *crtc_state =
13998 drm_atomic_get_existing_crtc_state(new_state->state, plane->state->crtc);
13999
14000 /* Big Hammer, we also need to ensure that any pending
14001 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
14002 * current scanout is retired before unpinning the old
14003 * framebuffer. Note that we rely on userspace rendering
14004 * into the buffer attached to the pipe they are waiting
14005 * on. If not, userspace generates a GPU hang with IPEHR
14006 * point to the MI_WAIT_FOR_EVENT.
14007 *
14008 * This should only fail upon a hung GPU, in which case we
14009 * can safely continue.
14010 */
14011 if (needs_modeset(crtc_state))
14012 ret = i915_gem_object_wait_rendering(old_obj, true);
f4457ae7
CW
14013 if (ret) {
14014 /* GPU hangs should have been swallowed by the wait */
14015 WARN_ON(ret == -EIO);
f935675f 14016 return ret;
f4457ae7 14017 }
5008e874
ML
14018 }
14019
c37efb99
CW
14020 if (!obj)
14021 return 0;
14022
5a21b665 14023 /* For framebuffer backed by dmabuf, wait for fence */
c37efb99
CW
14024 resv = i915_gem_object_get_dmabuf_resv(obj);
14025 if (resv) {
5a21b665
DV
14026 long lret;
14027
c37efb99 14028 lret = reservation_object_wait_timeout_rcu(resv, false, true,
5a21b665
DV
14029 MAX_SCHEDULE_TIMEOUT);
14030 if (lret == -ERESTARTSYS)
14031 return lret;
14032
14033 WARN(lret < 0, "waiting returns %li\n", lret);
14034 }
14035
c37efb99 14036 if (plane->type == DRM_PLANE_TYPE_CURSOR &&
6beb8c23
MR
14037 INTEL_INFO(dev)->cursor_needs_physical) {
14038 int align = IS_I830(dev) ? 16 * 1024 : 256;
14039 ret = i915_gem_object_attach_phys(obj, align);
14040 if (ret)
14041 DRM_DEBUG_KMS("failed to attach phys object\n");
14042 } else {
3465c580 14043 ret = intel_pin_and_fence_fb_obj(fb, new_state->rotation);
6beb8c23 14044 }
465c120c 14045
c37efb99 14046 if (ret == 0) {
27c01aae 14047 to_intel_plane_state(new_state)->wait_req =
d72d908b
CW
14048 i915_gem_active_get(&obj->last_write,
14049 &obj->base.dev->struct_mutex);
7580d774 14050 }
fdd508a6 14051
6beb8c23
MR
14052 return ret;
14053}
14054
38f3ce3a
MR
14055/**
14056 * intel_cleanup_plane_fb - Cleans up an fb after plane use
14057 * @plane: drm plane to clean up for
14058 * @fb: old framebuffer that was on plane
14059 *
14060 * Cleans up a framebuffer that has just been removed from a plane.
f935675f
ML
14061 *
14062 * Must be called with struct_mutex held.
38f3ce3a
MR
14063 */
14064void
14065intel_cleanup_plane_fb(struct drm_plane *plane,
d136dfee 14066 const struct drm_plane_state *old_state)
38f3ce3a
MR
14067{
14068 struct drm_device *dev = plane->dev;
7580d774 14069 struct intel_plane_state *old_intel_state;
84978257 14070 struct intel_plane_state *intel_state = to_intel_plane_state(plane->state);
1ee49399
ML
14071 struct drm_i915_gem_object *old_obj = intel_fb_obj(old_state->fb);
14072 struct drm_i915_gem_object *obj = intel_fb_obj(plane->state->fb);
38f3ce3a 14073
7580d774
ML
14074 old_intel_state = to_intel_plane_state(old_state);
14075
1ee49399 14076 if (!obj && !old_obj)
38f3ce3a
MR
14077 return;
14078
1ee49399
ML
14079 if (old_obj && (plane->type != DRM_PLANE_TYPE_CURSOR ||
14080 !INTEL_INFO(dev)->cursor_needs_physical))
3465c580 14081 intel_unpin_fb_obj(old_state->fb, old_state->rotation);
1ee49399 14082
84978257 14083 i915_gem_request_assign(&intel_state->wait_req, NULL);
7580d774 14084 i915_gem_request_assign(&old_intel_state->wait_req, NULL);
465c120c
MR
14085}
14086
6156a456
CK
14087int
14088skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
14089{
14090 int max_scale;
6156a456
CK
14091 int crtc_clock, cdclk;
14092
bf8a0af0 14093 if (!intel_crtc || !crtc_state->base.enable)
6156a456
CK
14094 return DRM_PLANE_HELPER_NO_SCALING;
14095
6156a456 14096 crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
27c329ed 14097 cdclk = to_intel_atomic_state(crtc_state->base.state)->cdclk;
6156a456 14098
54bf1ce6 14099 if (WARN_ON_ONCE(!crtc_clock || cdclk < crtc_clock))
6156a456
CK
14100 return DRM_PLANE_HELPER_NO_SCALING;
14101
14102 /*
14103 * skl max scale is lower of:
14104 * close to 3 but not 3, -1 is for that purpose
14105 * or
14106 * cdclk/crtc_clock
14107 */
14108 max_scale = min((1 << 16) * 3 - 1, (1 << 8) * ((cdclk << 8) / crtc_clock));
14109
14110 return max_scale;
14111}
14112
465c120c 14113static int
3c692a41 14114intel_check_primary_plane(struct drm_plane *plane,
061e4b8d 14115 struct intel_crtc_state *crtc_state,
3c692a41
GP
14116 struct intel_plane_state *state)
14117{
2b875c22
MR
14118 struct drm_crtc *crtc = state->base.crtc;
14119 struct drm_framebuffer *fb = state->base.fb;
6156a456 14120 int min_scale = DRM_PLANE_HELPER_NO_SCALING;
061e4b8d
ML
14121 int max_scale = DRM_PLANE_HELPER_NO_SCALING;
14122 bool can_position = false;
465c120c 14123
693bdc28
VS
14124 if (INTEL_INFO(plane->dev)->gen >= 9) {
14125 /* use scaler when colorkey is not required */
14126 if (state->ckey.flags == I915_SET_COLORKEY_NONE) {
14127 min_scale = 1;
14128 max_scale = skl_max_scale(to_intel_crtc(crtc), crtc_state);
14129 }
d8106366 14130 can_position = true;
6156a456 14131 }
d8106366 14132
061e4b8d
ML
14133 return drm_plane_helper_check_update(plane, crtc, fb, &state->src,
14134 &state->dst, &state->clip,
9b8b013d 14135 state->base.rotation,
da20eabd
ML
14136 min_scale, max_scale,
14137 can_position, true,
14138 &state->visible);
14af293f
GP
14139}
14140
5a21b665
DV
14141static void intel_begin_crtc_commit(struct drm_crtc *crtc,
14142 struct drm_crtc_state *old_crtc_state)
14143{
14144 struct drm_device *dev = crtc->dev;
14145 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
14146 struct intel_crtc_state *old_intel_state =
14147 to_intel_crtc_state(old_crtc_state);
14148 bool modeset = needs_modeset(crtc->state);
14149
14150 /* Perform vblank evasion around commit operation */
14151 intel_pipe_update_start(intel_crtc);
14152
14153 if (modeset)
14154 return;
14155
14156 if (crtc->state->color_mgmt_changed || to_intel_crtc_state(crtc->state)->update_pipe) {
14157 intel_color_set_csc(crtc->state);
14158 intel_color_load_luts(crtc->state);
14159 }
14160
14161 if (to_intel_crtc_state(crtc->state)->update_pipe)
14162 intel_update_pipe_config(intel_crtc, old_intel_state);
14163 else if (INTEL_INFO(dev)->gen >= 9)
14164 skl_detach_scalers(intel_crtc);
14165}
14166
14167static void intel_finish_crtc_commit(struct drm_crtc *crtc,
14168 struct drm_crtc_state *old_crtc_state)
14169{
14170 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
14171
14172 intel_pipe_update_end(intel_crtc, NULL);
14173}
14174
cf4c7c12 14175/**
4a3b8769
MR
14176 * intel_plane_destroy - destroy a plane
14177 * @plane: plane to destroy
cf4c7c12 14178 *
4a3b8769
MR
14179 * Common destruction function for all types of planes (primary, cursor,
14180 * sprite).
cf4c7c12 14181 */
4a3b8769 14182void intel_plane_destroy(struct drm_plane *plane)
465c120c 14183{
69ae561f
VS
14184 if (!plane)
14185 return;
14186
465c120c 14187 drm_plane_cleanup(plane);
69ae561f 14188 kfree(to_intel_plane(plane));
465c120c
MR
14189}
14190
65a3fea0 14191const struct drm_plane_funcs intel_plane_funcs = {
70a101f8
MR
14192 .update_plane = drm_atomic_helper_update_plane,
14193 .disable_plane = drm_atomic_helper_disable_plane,
3d7d6510 14194 .destroy = intel_plane_destroy,
c196e1d6 14195 .set_property = drm_atomic_helper_plane_set_property,
a98b3431
MR
14196 .atomic_get_property = intel_plane_atomic_get_property,
14197 .atomic_set_property = intel_plane_atomic_set_property,
ea2c67bb
MR
14198 .atomic_duplicate_state = intel_plane_duplicate_state,
14199 .atomic_destroy_state = intel_plane_destroy_state,
14200
465c120c
MR
14201};
14202
14203static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
14204 int pipe)
14205{
fca0ce2a
VS
14206 struct intel_plane *primary = NULL;
14207 struct intel_plane_state *state = NULL;
465c120c 14208 const uint32_t *intel_primary_formats;
45e3743a 14209 unsigned int num_formats;
fca0ce2a 14210 int ret;
465c120c
MR
14211
14212 primary = kzalloc(sizeof(*primary), GFP_KERNEL);
fca0ce2a
VS
14213 if (!primary)
14214 goto fail;
465c120c 14215
8e7d688b 14216 state = intel_create_plane_state(&primary->base);
fca0ce2a
VS
14217 if (!state)
14218 goto fail;
8e7d688b 14219 primary->base.state = &state->base;
ea2c67bb 14220
465c120c
MR
14221 primary->can_scale = false;
14222 primary->max_downscale = 1;
6156a456
CK
14223 if (INTEL_INFO(dev)->gen >= 9) {
14224 primary->can_scale = true;
af99ceda 14225 state->scaler_id = -1;
6156a456 14226 }
465c120c
MR
14227 primary->pipe = pipe;
14228 primary->plane = pipe;
a9ff8714 14229 primary->frontbuffer_bit = INTEL_FRONTBUFFER_PRIMARY(pipe);
c59cb179 14230 primary->check_plane = intel_check_primary_plane;
465c120c
MR
14231 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
14232 primary->plane = !pipe;
14233
6c0fd451
DL
14234 if (INTEL_INFO(dev)->gen >= 9) {
14235 intel_primary_formats = skl_primary_formats;
14236 num_formats = ARRAY_SIZE(skl_primary_formats);
a8d201af
ML
14237
14238 primary->update_plane = skylake_update_primary_plane;
14239 primary->disable_plane = skylake_disable_primary_plane;
14240 } else if (HAS_PCH_SPLIT(dev)) {
14241 intel_primary_formats = i965_primary_formats;
14242 num_formats = ARRAY_SIZE(i965_primary_formats);
14243
14244 primary->update_plane = ironlake_update_primary_plane;
14245 primary->disable_plane = i9xx_disable_primary_plane;
6c0fd451 14246 } else if (INTEL_INFO(dev)->gen >= 4) {
568db4f2
DL
14247 intel_primary_formats = i965_primary_formats;
14248 num_formats = ARRAY_SIZE(i965_primary_formats);
a8d201af
ML
14249
14250 primary->update_plane = i9xx_update_primary_plane;
14251 primary->disable_plane = i9xx_disable_primary_plane;
6c0fd451
DL
14252 } else {
14253 intel_primary_formats = i8xx_primary_formats;
14254 num_formats = ARRAY_SIZE(i8xx_primary_formats);
a8d201af
ML
14255
14256 primary->update_plane = i9xx_update_primary_plane;
14257 primary->disable_plane = i9xx_disable_primary_plane;
465c120c
MR
14258 }
14259
38573dc1
VS
14260 if (INTEL_INFO(dev)->gen >= 9)
14261 ret = drm_universal_plane_init(dev, &primary->base, 0,
14262 &intel_plane_funcs,
14263 intel_primary_formats, num_formats,
14264 DRM_PLANE_TYPE_PRIMARY,
14265 "plane 1%c", pipe_name(pipe));
14266 else if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
14267 ret = drm_universal_plane_init(dev, &primary->base, 0,
14268 &intel_plane_funcs,
14269 intel_primary_formats, num_formats,
14270 DRM_PLANE_TYPE_PRIMARY,
14271 "primary %c", pipe_name(pipe));
14272 else
14273 ret = drm_universal_plane_init(dev, &primary->base, 0,
14274 &intel_plane_funcs,
14275 intel_primary_formats, num_formats,
14276 DRM_PLANE_TYPE_PRIMARY,
14277 "plane %c", plane_name(primary->plane));
fca0ce2a
VS
14278 if (ret)
14279 goto fail;
48404c1e 14280
3b7a5119
SJ
14281 if (INTEL_INFO(dev)->gen >= 4)
14282 intel_create_rotation_property(dev, primary);
48404c1e 14283
ea2c67bb
MR
14284 drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
14285
465c120c 14286 return &primary->base;
fca0ce2a
VS
14287
14288fail:
14289 kfree(state);
14290 kfree(primary);
14291
14292 return NULL;
465c120c
MR
14293}
14294
3b7a5119
SJ
14295void intel_create_rotation_property(struct drm_device *dev, struct intel_plane *plane)
14296{
14297 if (!dev->mode_config.rotation_property) {
14298 unsigned long flags = BIT(DRM_ROTATE_0) |
14299 BIT(DRM_ROTATE_180);
14300
14301 if (INTEL_INFO(dev)->gen >= 9)
14302 flags |= BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_270);
14303
14304 dev->mode_config.rotation_property =
14305 drm_mode_create_rotation_property(dev, flags);
14306 }
14307 if (dev->mode_config.rotation_property)
14308 drm_object_attach_property(&plane->base.base,
14309 dev->mode_config.rotation_property,
14310 plane->base.state->rotation);
14311}
14312
3d7d6510 14313static int
852e787c 14314intel_check_cursor_plane(struct drm_plane *plane,
061e4b8d 14315 struct intel_crtc_state *crtc_state,
852e787c 14316 struct intel_plane_state *state)
3d7d6510 14317{
061e4b8d 14318 struct drm_crtc *crtc = crtc_state->base.crtc;
2b875c22 14319 struct drm_framebuffer *fb = state->base.fb;
757f9a3e 14320 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
b29ec92c 14321 enum pipe pipe = to_intel_plane(plane)->pipe;
757f9a3e
GP
14322 unsigned stride;
14323 int ret;
3d7d6510 14324
061e4b8d
ML
14325 ret = drm_plane_helper_check_update(plane, crtc, fb, &state->src,
14326 &state->dst, &state->clip,
9b8b013d 14327 state->base.rotation,
3d7d6510
MR
14328 DRM_PLANE_HELPER_NO_SCALING,
14329 DRM_PLANE_HELPER_NO_SCALING,
852e787c 14330 true, true, &state->visible);
757f9a3e
GP
14331 if (ret)
14332 return ret;
14333
757f9a3e
GP
14334 /* if we want to turn off the cursor ignore width and height */
14335 if (!obj)
da20eabd 14336 return 0;
757f9a3e 14337
757f9a3e 14338 /* Check for which cursor types we support */
061e4b8d 14339 if (!cursor_size_ok(plane->dev, state->base.crtc_w, state->base.crtc_h)) {
ea2c67bb
MR
14340 DRM_DEBUG("Cursor dimension %dx%d not supported\n",
14341 state->base.crtc_w, state->base.crtc_h);
757f9a3e
GP
14342 return -EINVAL;
14343 }
14344
ea2c67bb
MR
14345 stride = roundup_pow_of_two(state->base.crtc_w) * 4;
14346 if (obj->base.size < stride * state->base.crtc_h) {
757f9a3e
GP
14347 DRM_DEBUG_KMS("buffer is too small\n");
14348 return -ENOMEM;
14349 }
14350
3a656b54 14351 if (fb->modifier[0] != DRM_FORMAT_MOD_NONE) {
757f9a3e 14352 DRM_DEBUG_KMS("cursor cannot be tiled\n");
da20eabd 14353 return -EINVAL;
32b7eeec
MR
14354 }
14355
b29ec92c
VS
14356 /*
14357 * There's something wrong with the cursor on CHV pipe C.
14358 * If it straddles the left edge of the screen then
14359 * moving it away from the edge or disabling it often
14360 * results in a pipe underrun, and often that can lead to
14361 * dead pipe (constant underrun reported, and it scans
14362 * out just a solid color). To recover from that, the
14363 * display power well must be turned off and on again.
14364 * Refuse the put the cursor into that compromised position.
14365 */
14366 if (IS_CHERRYVIEW(plane->dev) && pipe == PIPE_C &&
14367 state->visible && state->base.crtc_x < 0) {
14368 DRM_DEBUG_KMS("CHV cursor C not allowed to straddle the left screen edge\n");
14369 return -EINVAL;
14370 }
14371
da20eabd 14372 return 0;
852e787c 14373}
3d7d6510 14374
a8ad0d8e
ML
14375static void
14376intel_disable_cursor_plane(struct drm_plane *plane,
7fabf5ef 14377 struct drm_crtc *crtc)
a8ad0d8e 14378{
f2858021
ML
14379 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
14380
14381 intel_crtc->cursor_addr = 0;
55a08b3f 14382 intel_crtc_update_cursor(crtc, NULL);
a8ad0d8e
ML
14383}
14384
f4a2cf29 14385static void
55a08b3f
ML
14386intel_update_cursor_plane(struct drm_plane *plane,
14387 const struct intel_crtc_state *crtc_state,
14388 const struct intel_plane_state *state)
852e787c 14389{
55a08b3f
ML
14390 struct drm_crtc *crtc = crtc_state->base.crtc;
14391 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ea2c67bb 14392 struct drm_device *dev = plane->dev;
2b875c22 14393 struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
a912f12f 14394 uint32_t addr;
852e787c 14395
f4a2cf29 14396 if (!obj)
a912f12f 14397 addr = 0;
f4a2cf29 14398 else if (!INTEL_INFO(dev)->cursor_needs_physical)
a912f12f 14399 addr = i915_gem_obj_ggtt_offset(obj);
f4a2cf29 14400 else
a912f12f 14401 addr = obj->phys_handle->busaddr;
852e787c 14402
a912f12f 14403 intel_crtc->cursor_addr = addr;
55a08b3f 14404 intel_crtc_update_cursor(crtc, state);
852e787c
GP
14405}
14406
3d7d6510
MR
14407static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
14408 int pipe)
14409{
fca0ce2a
VS
14410 struct intel_plane *cursor = NULL;
14411 struct intel_plane_state *state = NULL;
14412 int ret;
3d7d6510
MR
14413
14414 cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
fca0ce2a
VS
14415 if (!cursor)
14416 goto fail;
3d7d6510 14417
8e7d688b 14418 state = intel_create_plane_state(&cursor->base);
fca0ce2a
VS
14419 if (!state)
14420 goto fail;
8e7d688b 14421 cursor->base.state = &state->base;
ea2c67bb 14422
3d7d6510
MR
14423 cursor->can_scale = false;
14424 cursor->max_downscale = 1;
14425 cursor->pipe = pipe;
14426 cursor->plane = pipe;
a9ff8714 14427 cursor->frontbuffer_bit = INTEL_FRONTBUFFER_CURSOR(pipe);
c59cb179 14428 cursor->check_plane = intel_check_cursor_plane;
55a08b3f 14429 cursor->update_plane = intel_update_cursor_plane;
a8ad0d8e 14430 cursor->disable_plane = intel_disable_cursor_plane;
3d7d6510 14431
fca0ce2a
VS
14432 ret = drm_universal_plane_init(dev, &cursor->base, 0,
14433 &intel_plane_funcs,
14434 intel_cursor_formats,
14435 ARRAY_SIZE(intel_cursor_formats),
38573dc1
VS
14436 DRM_PLANE_TYPE_CURSOR,
14437 "cursor %c", pipe_name(pipe));
fca0ce2a
VS
14438 if (ret)
14439 goto fail;
4398ad45
VS
14440
14441 if (INTEL_INFO(dev)->gen >= 4) {
14442 if (!dev->mode_config.rotation_property)
14443 dev->mode_config.rotation_property =
14444 drm_mode_create_rotation_property(dev,
14445 BIT(DRM_ROTATE_0) |
14446 BIT(DRM_ROTATE_180));
14447 if (dev->mode_config.rotation_property)
14448 drm_object_attach_property(&cursor->base.base,
14449 dev->mode_config.rotation_property,
8e7d688b 14450 state->base.rotation);
4398ad45
VS
14451 }
14452
af99ceda
CK
14453 if (INTEL_INFO(dev)->gen >=9)
14454 state->scaler_id = -1;
14455
ea2c67bb
MR
14456 drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
14457
3d7d6510 14458 return &cursor->base;
fca0ce2a
VS
14459
14460fail:
14461 kfree(state);
14462 kfree(cursor);
14463
14464 return NULL;
3d7d6510
MR
14465}
14466
549e2bfb
CK
14467static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
14468 struct intel_crtc_state *crtc_state)
14469{
14470 int i;
14471 struct intel_scaler *intel_scaler;
14472 struct intel_crtc_scaler_state *scaler_state = &crtc_state->scaler_state;
14473
14474 for (i = 0; i < intel_crtc->num_scalers; i++) {
14475 intel_scaler = &scaler_state->scalers[i];
14476 intel_scaler->in_use = 0;
549e2bfb
CK
14477 intel_scaler->mode = PS_SCALER_MODE_DYN;
14478 }
14479
14480 scaler_state->scaler_id = -1;
14481}
14482
b358d0a6 14483static void intel_crtc_init(struct drm_device *dev, int pipe)
79e53945 14484{
fac5e23e 14485 struct drm_i915_private *dev_priv = to_i915(dev);
79e53945 14486 struct intel_crtc *intel_crtc;
f5de6e07 14487 struct intel_crtc_state *crtc_state = NULL;
3d7d6510
MR
14488 struct drm_plane *primary = NULL;
14489 struct drm_plane *cursor = NULL;
8563b1e8 14490 int ret;
79e53945 14491
955382f3 14492 intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
79e53945
JB
14493 if (intel_crtc == NULL)
14494 return;
14495
f5de6e07
ACO
14496 crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
14497 if (!crtc_state)
14498 goto fail;
550acefd
ACO
14499 intel_crtc->config = crtc_state;
14500 intel_crtc->base.state = &crtc_state->base;
07878248 14501 crtc_state->base.crtc = &intel_crtc->base;
f5de6e07 14502
549e2bfb
CK
14503 /* initialize shared scalers */
14504 if (INTEL_INFO(dev)->gen >= 9) {
14505 if (pipe == PIPE_C)
14506 intel_crtc->num_scalers = 1;
14507 else
14508 intel_crtc->num_scalers = SKL_NUM_SCALERS;
14509
14510 skl_init_scalers(dev, intel_crtc, crtc_state);
14511 }
14512
465c120c 14513 primary = intel_primary_plane_create(dev, pipe);
3d7d6510
MR
14514 if (!primary)
14515 goto fail;
14516
14517 cursor = intel_cursor_plane_create(dev, pipe);
14518 if (!cursor)
14519 goto fail;
14520
465c120c 14521 ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
4d5d72b7
VS
14522 cursor, &intel_crtc_funcs,
14523 "pipe %c", pipe_name(pipe));
3d7d6510
MR
14524 if (ret)
14525 goto fail;
79e53945 14526
1f1c2e24
VS
14527 /*
14528 * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
8c0f92e1 14529 * is hooked to pipe B. Hence we want plane A feeding pipe B.
1f1c2e24 14530 */
80824003
JB
14531 intel_crtc->pipe = pipe;
14532 intel_crtc->plane = pipe;
3a77c4c4 14533 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
28c97730 14534 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
e2e767ab 14535 intel_crtc->plane = !pipe;
80824003
JB
14536 }
14537
4b0e333e
CW
14538 intel_crtc->cursor_base = ~0;
14539 intel_crtc->cursor_cntl = ~0;
dc41c154 14540 intel_crtc->cursor_size = ~0;
8d7849db 14541
852eb00d
VS
14542 intel_crtc->wm.cxsr_allowed = true;
14543
22fd0fab
JB
14544 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
14545 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
14546 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
14547 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
14548
79e53945 14549 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
87b6b101 14550
8563b1e8
LL
14551 intel_color_init(&intel_crtc->base);
14552
87b6b101 14553 WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
3d7d6510
MR
14554 return;
14555
14556fail:
69ae561f
VS
14557 intel_plane_destroy(primary);
14558 intel_plane_destroy(cursor);
f5de6e07 14559 kfree(crtc_state);
3d7d6510 14560 kfree(intel_crtc);
79e53945
JB
14561}
14562
752aa88a
JB
14563enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
14564{
14565 struct drm_encoder *encoder = connector->base.encoder;
6e9f798d 14566 struct drm_device *dev = connector->base.dev;
752aa88a 14567
51fd371b 14568 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
752aa88a 14569
d3babd3f 14570 if (!encoder || WARN_ON(!encoder->crtc))
752aa88a
JB
14571 return INVALID_PIPE;
14572
14573 return to_intel_crtc(encoder->crtc)->pipe;
14574}
14575
08d7b3d1 14576int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
05394f39 14577 struct drm_file *file)
08d7b3d1 14578{
08d7b3d1 14579 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
7707e653 14580 struct drm_crtc *drmmode_crtc;
c05422d5 14581 struct intel_crtc *crtc;
08d7b3d1 14582
7707e653 14583 drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
71240ed2 14584 if (!drmmode_crtc)
3f2c2057 14585 return -ENOENT;
08d7b3d1 14586
7707e653 14587 crtc = to_intel_crtc(drmmode_crtc);
c05422d5 14588 pipe_from_crtc_id->pipe = crtc->pipe;
08d7b3d1 14589
c05422d5 14590 return 0;
08d7b3d1
CW
14591}
14592
66a9278e 14593static int intel_encoder_clones(struct intel_encoder *encoder)
79e53945 14594{
66a9278e
DV
14595 struct drm_device *dev = encoder->base.dev;
14596 struct intel_encoder *source_encoder;
79e53945 14597 int index_mask = 0;
79e53945
JB
14598 int entry = 0;
14599
b2784e15 14600 for_each_intel_encoder(dev, source_encoder) {
bc079e8b 14601 if (encoders_cloneable(encoder, source_encoder))
66a9278e
DV
14602 index_mask |= (1 << entry);
14603
79e53945
JB
14604 entry++;
14605 }
4ef69c7a 14606
79e53945
JB
14607 return index_mask;
14608}
14609
4d302442
CW
14610static bool has_edp_a(struct drm_device *dev)
14611{
fac5e23e 14612 struct drm_i915_private *dev_priv = to_i915(dev);
4d302442
CW
14613
14614 if (!IS_MOBILE(dev))
14615 return false;
14616
14617 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
14618 return false;
14619
e3589908 14620 if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
4d302442
CW
14621 return false;
14622
14623 return true;
14624}
14625
84b4e042
JB
14626static bool intel_crt_present(struct drm_device *dev)
14627{
fac5e23e 14628 struct drm_i915_private *dev_priv = to_i915(dev);
84b4e042 14629
884497ed
DL
14630 if (INTEL_INFO(dev)->gen >= 9)
14631 return false;
14632
cf404ce4 14633 if (IS_HSW_ULT(dev) || IS_BDW_ULT(dev))
84b4e042
JB
14634 return false;
14635
14636 if (IS_CHERRYVIEW(dev))
14637 return false;
14638
65e472e4
VS
14639 if (HAS_PCH_LPT_H(dev) && I915_READ(SFUSE_STRAP) & SFUSE_STRAP_CRT_DISABLED)
14640 return false;
14641
70ac54d0
VS
14642 /* DDI E can't be used if DDI A requires 4 lanes */
14643 if (HAS_DDI(dev) && I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_A_4_LANES)
14644 return false;
14645
e4abb733 14646 if (!dev_priv->vbt.int_crt_support)
84b4e042
JB
14647 return false;
14648
14649 return true;
14650}
14651
79e53945
JB
14652static void intel_setup_outputs(struct drm_device *dev)
14653{
fac5e23e 14654 struct drm_i915_private *dev_priv = to_i915(dev);
4ef69c7a 14655 struct intel_encoder *encoder;
cb0953d7 14656 bool dpd_is_edp = false;
79e53945 14657
97a824e1
ID
14658 /*
14659 * intel_edp_init_connector() depends on this completing first, to
14660 * prevent the registeration of both eDP and LVDS and the incorrect
14661 * sharing of the PPS.
14662 */
c9093354 14663 intel_lvds_init(dev);
79e53945 14664
84b4e042 14665 if (intel_crt_present(dev))
79935fca 14666 intel_crt_init(dev);
cb0953d7 14667
c776eb2e
VK
14668 if (IS_BROXTON(dev)) {
14669 /*
14670 * FIXME: Broxton doesn't support port detection via the
14671 * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
14672 * detect the ports.
14673 */
14674 intel_ddi_init(dev, PORT_A);
14675 intel_ddi_init(dev, PORT_B);
14676 intel_ddi_init(dev, PORT_C);
c6c794a2
SS
14677
14678 intel_dsi_init(dev);
c776eb2e 14679 } else if (HAS_DDI(dev)) {
0e72a5b5
ED
14680 int found;
14681
de31facd
JB
14682 /*
14683 * Haswell uses DDI functions to detect digital outputs.
14684 * On SKL pre-D0 the strap isn't connected, so we assume
14685 * it's there.
14686 */
77179400 14687 found = I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_INIT_DISPLAY_DETECTED;
de31facd 14688 /* WaIgnoreDDIAStrap: skl */
ef11bdb3 14689 if (found || IS_SKYLAKE(dev) || IS_KABYLAKE(dev))
0e72a5b5
ED
14690 intel_ddi_init(dev, PORT_A);
14691
14692 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
14693 * register */
14694 found = I915_READ(SFUSE_STRAP);
14695
14696 if (found & SFUSE_STRAP_DDIB_DETECTED)
14697 intel_ddi_init(dev, PORT_B);
14698 if (found & SFUSE_STRAP_DDIC_DETECTED)
14699 intel_ddi_init(dev, PORT_C);
14700 if (found & SFUSE_STRAP_DDID_DETECTED)
14701 intel_ddi_init(dev, PORT_D);
2800e4c2
RV
14702 /*
14703 * On SKL we don't have a way to detect DDI-E so we rely on VBT.
14704 */
ef11bdb3 14705 if ((IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) &&
2800e4c2
RV
14706 (dev_priv->vbt.ddi_port_info[PORT_E].supports_dp ||
14707 dev_priv->vbt.ddi_port_info[PORT_E].supports_dvi ||
14708 dev_priv->vbt.ddi_port_info[PORT_E].supports_hdmi))
14709 intel_ddi_init(dev, PORT_E);
14710
0e72a5b5 14711 } else if (HAS_PCH_SPLIT(dev)) {
cb0953d7 14712 int found;
5d8a7752 14713 dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
270b3042
DV
14714
14715 if (has_edp_a(dev))
14716 intel_dp_init(dev, DP_A, PORT_A);
cb0953d7 14717
dc0fa718 14718 if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
461ed3ca 14719 /* PCH SDVOB multiplex with HDMIB */
2a5c0832 14720 found = intel_sdvo_init(dev, PCH_SDVOB, PORT_B);
30ad48b7 14721 if (!found)
e2debe91 14722 intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
5eb08b69 14723 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
ab9d7c30 14724 intel_dp_init(dev, PCH_DP_B, PORT_B);
30ad48b7
ZW
14725 }
14726
dc0fa718 14727 if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
e2debe91 14728 intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
30ad48b7 14729
dc0fa718 14730 if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
e2debe91 14731 intel_hdmi_init(dev, PCH_HDMID, PORT_D);
30ad48b7 14732
5eb08b69 14733 if (I915_READ(PCH_DP_C) & DP_DETECTED)
ab9d7c30 14734 intel_dp_init(dev, PCH_DP_C, PORT_C);
5eb08b69 14735
270b3042 14736 if (I915_READ(PCH_DP_D) & DP_DETECTED)
ab9d7c30 14737 intel_dp_init(dev, PCH_DP_D, PORT_D);
666a4537 14738 } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
22f35042 14739 bool has_edp, has_port;
457c52d8 14740
e17ac6db
VS
14741 /*
14742 * The DP_DETECTED bit is the latched state of the DDC
14743 * SDA pin at boot. However since eDP doesn't require DDC
14744 * (no way to plug in a DP->HDMI dongle) the DDC pins for
14745 * eDP ports may have been muxed to an alternate function.
14746 * Thus we can't rely on the DP_DETECTED bit alone to detect
14747 * eDP ports. Consult the VBT as well as DP_DETECTED to
14748 * detect eDP ports.
22f35042
VS
14749 *
14750 * Sadly the straps seem to be missing sometimes even for HDMI
14751 * ports (eg. on Voyo V3 - CHT x7-Z8700), so check both strap
14752 * and VBT for the presence of the port. Additionally we can't
14753 * trust the port type the VBT declares as we've seen at least
14754 * HDMI ports that the VBT claim are DP or eDP.
e17ac6db 14755 */
457c52d8 14756 has_edp = intel_dp_is_edp(dev, PORT_B);
22f35042
VS
14757 has_port = intel_bios_is_port_present(dev_priv, PORT_B);
14758 if (I915_READ(VLV_DP_B) & DP_DETECTED || has_port)
457c52d8 14759 has_edp &= intel_dp_init(dev, VLV_DP_B, PORT_B);
22f35042 14760 if ((I915_READ(VLV_HDMIB) & SDVO_DETECTED || has_port) && !has_edp)
e66eb81d 14761 intel_hdmi_init(dev, VLV_HDMIB, PORT_B);
585a94b8 14762
457c52d8 14763 has_edp = intel_dp_is_edp(dev, PORT_C);
22f35042
VS
14764 has_port = intel_bios_is_port_present(dev_priv, PORT_C);
14765 if (I915_READ(VLV_DP_C) & DP_DETECTED || has_port)
457c52d8 14766 has_edp &= intel_dp_init(dev, VLV_DP_C, PORT_C);
22f35042 14767 if ((I915_READ(VLV_HDMIC) & SDVO_DETECTED || has_port) && !has_edp)
e66eb81d 14768 intel_hdmi_init(dev, VLV_HDMIC, PORT_C);
19c03924 14769
9418c1f1 14770 if (IS_CHERRYVIEW(dev)) {
22f35042
VS
14771 /*
14772 * eDP not supported on port D,
14773 * so no need to worry about it
14774 */
14775 has_port = intel_bios_is_port_present(dev_priv, PORT_D);
14776 if (I915_READ(CHV_DP_D) & DP_DETECTED || has_port)
e66eb81d 14777 intel_dp_init(dev, CHV_DP_D, PORT_D);
22f35042
VS
14778 if (I915_READ(CHV_HDMID) & SDVO_DETECTED || has_port)
14779 intel_hdmi_init(dev, CHV_HDMID, PORT_D);
9418c1f1
VS
14780 }
14781
3cfca973 14782 intel_dsi_init(dev);
09da55dc 14783 } else if (!IS_GEN2(dev) && !IS_PINEVIEW(dev)) {
27185ae1 14784 bool found = false;
7d57382e 14785
e2debe91 14786 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
b01f2c3a 14787 DRM_DEBUG_KMS("probing SDVOB\n");
2a5c0832 14788 found = intel_sdvo_init(dev, GEN3_SDVOB, PORT_B);
3fec3d2f 14789 if (!found && IS_G4X(dev)) {
b01f2c3a 14790 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
e2debe91 14791 intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
b01f2c3a 14792 }
27185ae1 14793
3fec3d2f 14794 if (!found && IS_G4X(dev))
ab9d7c30 14795 intel_dp_init(dev, DP_B, PORT_B);
725e30ad 14796 }
13520b05
KH
14797
14798 /* Before G4X SDVOC doesn't have its own detect register */
13520b05 14799
e2debe91 14800 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
b01f2c3a 14801 DRM_DEBUG_KMS("probing SDVOC\n");
2a5c0832 14802 found = intel_sdvo_init(dev, GEN3_SDVOC, PORT_C);
b01f2c3a 14803 }
27185ae1 14804
e2debe91 14805 if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
27185ae1 14806
3fec3d2f 14807 if (IS_G4X(dev)) {
b01f2c3a 14808 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
e2debe91 14809 intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
b01f2c3a 14810 }
3fec3d2f 14811 if (IS_G4X(dev))
ab9d7c30 14812 intel_dp_init(dev, DP_C, PORT_C);
725e30ad 14813 }
27185ae1 14814
3fec3d2f 14815 if (IS_G4X(dev) &&
e7281eab 14816 (I915_READ(DP_D) & DP_DETECTED))
ab9d7c30 14817 intel_dp_init(dev, DP_D, PORT_D);
bad720ff 14818 } else if (IS_GEN2(dev))
79e53945
JB
14819 intel_dvo_init(dev);
14820
103a196f 14821 if (SUPPORTS_TV(dev))
79e53945
JB
14822 intel_tv_init(dev);
14823
0bc12bcb 14824 intel_psr_init(dev);
7c8f8a70 14825
b2784e15 14826 for_each_intel_encoder(dev, encoder) {
4ef69c7a
CW
14827 encoder->base.possible_crtcs = encoder->crtc_mask;
14828 encoder->base.possible_clones =
66a9278e 14829 intel_encoder_clones(encoder);
79e53945 14830 }
47356eb6 14831
dde86e2d 14832 intel_init_pch_refclk(dev);
270b3042
DV
14833
14834 drm_helper_move_panel_connectors_to_head(dev);
79e53945
JB
14835}
14836
14837static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
14838{
60a5ca01 14839 struct drm_device *dev = fb->dev;
79e53945 14840 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
79e53945 14841
ef2d633e 14842 drm_framebuffer_cleanup(fb);
60a5ca01 14843 mutex_lock(&dev->struct_mutex);
ef2d633e 14844 WARN_ON(!intel_fb->obj->framebuffer_references--);
f8c417cd 14845 i915_gem_object_put(intel_fb->obj);
60a5ca01 14846 mutex_unlock(&dev->struct_mutex);
79e53945
JB
14847 kfree(intel_fb);
14848}
14849
14850static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
05394f39 14851 struct drm_file *file,
79e53945
JB
14852 unsigned int *handle)
14853{
14854 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
05394f39 14855 struct drm_i915_gem_object *obj = intel_fb->obj;
79e53945 14856
cc917ab4
CW
14857 if (obj->userptr.mm) {
14858 DRM_DEBUG("attempting to use a userptr for a framebuffer, denied\n");
14859 return -EINVAL;
14860 }
14861
05394f39 14862 return drm_gem_handle_create(file, &obj->base, handle);
79e53945
JB
14863}
14864
86c98588
RV
14865static int intel_user_framebuffer_dirty(struct drm_framebuffer *fb,
14866 struct drm_file *file,
14867 unsigned flags, unsigned color,
14868 struct drm_clip_rect *clips,
14869 unsigned num_clips)
14870{
14871 struct drm_device *dev = fb->dev;
14872 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
14873 struct drm_i915_gem_object *obj = intel_fb->obj;
14874
14875 mutex_lock(&dev->struct_mutex);
74b4ea1e 14876 intel_fb_obj_flush(obj, false, ORIGIN_DIRTYFB);
86c98588
RV
14877 mutex_unlock(&dev->struct_mutex);
14878
14879 return 0;
14880}
14881
79e53945
JB
14882static const struct drm_framebuffer_funcs intel_fb_funcs = {
14883 .destroy = intel_user_framebuffer_destroy,
14884 .create_handle = intel_user_framebuffer_create_handle,
86c98588 14885 .dirty = intel_user_framebuffer_dirty,
79e53945
JB
14886};
14887
b321803d
DL
14888static
14889u32 intel_fb_pitch_limit(struct drm_device *dev, uint64_t fb_modifier,
14890 uint32_t pixel_format)
14891{
14892 u32 gen = INTEL_INFO(dev)->gen;
14893
14894 if (gen >= 9) {
ac484963
VS
14895 int cpp = drm_format_plane_cpp(pixel_format, 0);
14896
b321803d
DL
14897 /* "The stride in bytes must not exceed the of the size of 8K
14898 * pixels and 32K bytes."
14899 */
ac484963 14900 return min(8192 * cpp, 32768);
666a4537 14901 } else if (gen >= 5 && !IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
b321803d
DL
14902 return 32*1024;
14903 } else if (gen >= 4) {
14904 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14905 return 16*1024;
14906 else
14907 return 32*1024;
14908 } else if (gen >= 3) {
14909 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14910 return 8*1024;
14911 else
14912 return 16*1024;
14913 } else {
14914 /* XXX DSPC is limited to 4k tiled */
14915 return 8*1024;
14916 }
14917}
14918
b5ea642a
DV
14919static int intel_framebuffer_init(struct drm_device *dev,
14920 struct intel_framebuffer *intel_fb,
14921 struct drm_mode_fb_cmd2 *mode_cmd,
14922 struct drm_i915_gem_object *obj)
79e53945 14923{
7b49f948 14924 struct drm_i915_private *dev_priv = to_i915(dev);
6761dd31 14925 unsigned int aligned_height;
79e53945 14926 int ret;
b321803d 14927 u32 pitch_limit, stride_alignment;
79e53945 14928
dd4916c5
DV
14929 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
14930
2a80eada
DV
14931 if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
14932 /* Enforce that fb modifier and tiling mode match, but only for
14933 * X-tiled. This is needed for FBC. */
3e510a8e 14934 if (!!(i915_gem_object_get_tiling(obj) == I915_TILING_X) !=
2a80eada
DV
14935 !!(mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED)) {
14936 DRM_DEBUG("tiling_mode doesn't match fb modifier\n");
14937 return -EINVAL;
14938 }
14939 } else {
3e510a8e 14940 if (i915_gem_object_get_tiling(obj) == I915_TILING_X)
2a80eada 14941 mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
3e510a8e 14942 else if (i915_gem_object_get_tiling(obj) == I915_TILING_Y) {
2a80eada
DV
14943 DRM_DEBUG("No Y tiling for legacy addfb\n");
14944 return -EINVAL;
14945 }
14946 }
14947
9a8f0a12
TU
14948 /* Passed in modifier sanity checking. */
14949 switch (mode_cmd->modifier[0]) {
14950 case I915_FORMAT_MOD_Y_TILED:
14951 case I915_FORMAT_MOD_Yf_TILED:
14952 if (INTEL_INFO(dev)->gen < 9) {
14953 DRM_DEBUG("Unsupported tiling 0x%llx!\n",
14954 mode_cmd->modifier[0]);
14955 return -EINVAL;
14956 }
14957 case DRM_FORMAT_MOD_NONE:
14958 case I915_FORMAT_MOD_X_TILED:
14959 break;
14960 default:
c0f40428
JB
14961 DRM_DEBUG("Unsupported fb modifier 0x%llx!\n",
14962 mode_cmd->modifier[0]);
57cd6508 14963 return -EINVAL;
c16ed4be 14964 }
57cd6508 14965
7b49f948
VS
14966 stride_alignment = intel_fb_stride_alignment(dev_priv,
14967 mode_cmd->modifier[0],
b321803d
DL
14968 mode_cmd->pixel_format);
14969 if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
14970 DRM_DEBUG("pitch (%d) must be at least %u byte aligned\n",
14971 mode_cmd->pitches[0], stride_alignment);
57cd6508 14972 return -EINVAL;
c16ed4be 14973 }
57cd6508 14974
b321803d
DL
14975 pitch_limit = intel_fb_pitch_limit(dev, mode_cmd->modifier[0],
14976 mode_cmd->pixel_format);
a35cdaa0 14977 if (mode_cmd->pitches[0] > pitch_limit) {
b321803d
DL
14978 DRM_DEBUG("%s pitch (%u) must be at less than %d\n",
14979 mode_cmd->modifier[0] != DRM_FORMAT_MOD_NONE ?
2a80eada 14980 "tiled" : "linear",
a35cdaa0 14981 mode_cmd->pitches[0], pitch_limit);
5d7bd705 14982 return -EINVAL;
c16ed4be 14983 }
5d7bd705 14984
2a80eada 14985 if (mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED &&
3e510a8e 14986 mode_cmd->pitches[0] != i915_gem_object_get_stride(obj)) {
c16ed4be 14987 DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
3e510a8e
CW
14988 mode_cmd->pitches[0],
14989 i915_gem_object_get_stride(obj));
5d7bd705 14990 return -EINVAL;
c16ed4be 14991 }
5d7bd705 14992
57779d06 14993 /* Reject formats not supported by any plane early. */
308e5bcb 14994 switch (mode_cmd->pixel_format) {
57779d06 14995 case DRM_FORMAT_C8:
04b3924d
VS
14996 case DRM_FORMAT_RGB565:
14997 case DRM_FORMAT_XRGB8888:
14998 case DRM_FORMAT_ARGB8888:
57779d06
VS
14999 break;
15000 case DRM_FORMAT_XRGB1555:
c16ed4be 15001 if (INTEL_INFO(dev)->gen > 3) {
4ee62c76
VS
15002 DRM_DEBUG("unsupported pixel format: %s\n",
15003 drm_get_format_name(mode_cmd->pixel_format));
57779d06 15004 return -EINVAL;
c16ed4be 15005 }
57779d06 15006 break;
57779d06 15007 case DRM_FORMAT_ABGR8888:
666a4537
WB
15008 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev) &&
15009 INTEL_INFO(dev)->gen < 9) {
6c0fd451
DL
15010 DRM_DEBUG("unsupported pixel format: %s\n",
15011 drm_get_format_name(mode_cmd->pixel_format));
15012 return -EINVAL;
15013 }
15014 break;
15015 case DRM_FORMAT_XBGR8888:
04b3924d 15016 case DRM_FORMAT_XRGB2101010:
57779d06 15017 case DRM_FORMAT_XBGR2101010:
c16ed4be 15018 if (INTEL_INFO(dev)->gen < 4) {
4ee62c76
VS
15019 DRM_DEBUG("unsupported pixel format: %s\n",
15020 drm_get_format_name(mode_cmd->pixel_format));
57779d06 15021 return -EINVAL;
c16ed4be 15022 }
b5626747 15023 break;
7531208b 15024 case DRM_FORMAT_ABGR2101010:
666a4537 15025 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
7531208b
DL
15026 DRM_DEBUG("unsupported pixel format: %s\n",
15027 drm_get_format_name(mode_cmd->pixel_format));
15028 return -EINVAL;
15029 }
15030 break;
04b3924d
VS
15031 case DRM_FORMAT_YUYV:
15032 case DRM_FORMAT_UYVY:
15033 case DRM_FORMAT_YVYU:
15034 case DRM_FORMAT_VYUY:
c16ed4be 15035 if (INTEL_INFO(dev)->gen < 5) {
4ee62c76
VS
15036 DRM_DEBUG("unsupported pixel format: %s\n",
15037 drm_get_format_name(mode_cmd->pixel_format));
57779d06 15038 return -EINVAL;
c16ed4be 15039 }
57cd6508
CW
15040 break;
15041 default:
4ee62c76
VS
15042 DRM_DEBUG("unsupported pixel format: %s\n",
15043 drm_get_format_name(mode_cmd->pixel_format));
57cd6508
CW
15044 return -EINVAL;
15045 }
15046
90f9a336
VS
15047 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
15048 if (mode_cmd->offsets[0] != 0)
15049 return -EINVAL;
15050
ec2c981e 15051 aligned_height = intel_fb_align_height(dev, mode_cmd->height,
091df6cb
DV
15052 mode_cmd->pixel_format,
15053 mode_cmd->modifier[0]);
53155c0a
DV
15054 /* FIXME drm helper for size checks (especially planar formats)? */
15055 if (obj->base.size < aligned_height * mode_cmd->pitches[0])
15056 return -EINVAL;
15057
c7d73f6a
DV
15058 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
15059 intel_fb->obj = obj;
15060
2d7a215f
VS
15061 intel_fill_fb_info(dev_priv, &intel_fb->base);
15062
79e53945
JB
15063 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
15064 if (ret) {
15065 DRM_ERROR("framebuffer init failed %d\n", ret);
15066 return ret;
15067 }
15068
0b05e1e0
VS
15069 intel_fb->obj->framebuffer_references++;
15070
79e53945
JB
15071 return 0;
15072}
15073
79e53945
JB
15074static struct drm_framebuffer *
15075intel_user_framebuffer_create(struct drm_device *dev,
15076 struct drm_file *filp,
1eb83451 15077 const struct drm_mode_fb_cmd2 *user_mode_cmd)
79e53945 15078{
dcb1394e 15079 struct drm_framebuffer *fb;
05394f39 15080 struct drm_i915_gem_object *obj;
76dc3769 15081 struct drm_mode_fb_cmd2 mode_cmd = *user_mode_cmd;
79e53945 15082
03ac0642
CW
15083 obj = i915_gem_object_lookup(filp, mode_cmd.handles[0]);
15084 if (!obj)
cce13ff7 15085 return ERR_PTR(-ENOENT);
79e53945 15086
92907cbb 15087 fb = intel_framebuffer_create(dev, &mode_cmd, obj);
dcb1394e 15088 if (IS_ERR(fb))
34911fd3 15089 i915_gem_object_put_unlocked(obj);
dcb1394e
LW
15090
15091 return fb;
79e53945
JB
15092}
15093
0695726e 15094#ifndef CONFIG_DRM_FBDEV_EMULATION
0632fef6 15095static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
4520f53a
DV
15096{
15097}
15098#endif
15099
79e53945 15100static const struct drm_mode_config_funcs intel_mode_funcs = {
79e53945 15101 .fb_create = intel_user_framebuffer_create,
0632fef6 15102 .output_poll_changed = intel_fbdev_output_poll_changed,
5ee67f1c
MR
15103 .atomic_check = intel_atomic_check,
15104 .atomic_commit = intel_atomic_commit,
de419ab6
ML
15105 .atomic_state_alloc = intel_atomic_state_alloc,
15106 .atomic_state_clear = intel_atomic_state_clear,
79e53945
JB
15107};
15108
88212941
ID
15109/**
15110 * intel_init_display_hooks - initialize the display modesetting hooks
15111 * @dev_priv: device private
15112 */
15113void intel_init_display_hooks(struct drm_i915_private *dev_priv)
e70236a8 15114{
88212941 15115 if (INTEL_INFO(dev_priv)->gen >= 9) {
bc8d7dff 15116 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
5724dbd1
DL
15117 dev_priv->display.get_initial_plane_config =
15118 skylake_get_initial_plane_config;
bc8d7dff
DL
15119 dev_priv->display.crtc_compute_clock =
15120 haswell_crtc_compute_clock;
15121 dev_priv->display.crtc_enable = haswell_crtc_enable;
15122 dev_priv->display.crtc_disable = haswell_crtc_disable;
88212941 15123 } else if (HAS_DDI(dev_priv)) {
0e8ffe1b 15124 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
5724dbd1
DL
15125 dev_priv->display.get_initial_plane_config =
15126 ironlake_get_initial_plane_config;
797d0259
ACO
15127 dev_priv->display.crtc_compute_clock =
15128 haswell_crtc_compute_clock;
4f771f10
PZ
15129 dev_priv->display.crtc_enable = haswell_crtc_enable;
15130 dev_priv->display.crtc_disable = haswell_crtc_disable;
88212941 15131 } else if (HAS_PCH_SPLIT(dev_priv)) {
0e8ffe1b 15132 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
5724dbd1
DL
15133 dev_priv->display.get_initial_plane_config =
15134 ironlake_get_initial_plane_config;
3fb37703
ACO
15135 dev_priv->display.crtc_compute_clock =
15136 ironlake_crtc_compute_clock;
76e5a89c
DV
15137 dev_priv->display.crtc_enable = ironlake_crtc_enable;
15138 dev_priv->display.crtc_disable = ironlake_crtc_disable;
65b3d6a9 15139 } else if (IS_CHERRYVIEW(dev_priv)) {
89b667f8 15140 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
5724dbd1
DL
15141 dev_priv->display.get_initial_plane_config =
15142 i9xx_get_initial_plane_config;
65b3d6a9
ACO
15143 dev_priv->display.crtc_compute_clock = chv_crtc_compute_clock;
15144 dev_priv->display.crtc_enable = valleyview_crtc_enable;
15145 dev_priv->display.crtc_disable = i9xx_crtc_disable;
15146 } else if (IS_VALLEYVIEW(dev_priv)) {
15147 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
15148 dev_priv->display.get_initial_plane_config =
15149 i9xx_get_initial_plane_config;
15150 dev_priv->display.crtc_compute_clock = vlv_crtc_compute_clock;
89b667f8
JB
15151 dev_priv->display.crtc_enable = valleyview_crtc_enable;
15152 dev_priv->display.crtc_disable = i9xx_crtc_disable;
19ec6693
ACO
15153 } else if (IS_G4X(dev_priv)) {
15154 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
15155 dev_priv->display.get_initial_plane_config =
15156 i9xx_get_initial_plane_config;
15157 dev_priv->display.crtc_compute_clock = g4x_crtc_compute_clock;
15158 dev_priv->display.crtc_enable = i9xx_crtc_enable;
15159 dev_priv->display.crtc_disable = i9xx_crtc_disable;
70e8aa21
ACO
15160 } else if (IS_PINEVIEW(dev_priv)) {
15161 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
15162 dev_priv->display.get_initial_plane_config =
15163 i9xx_get_initial_plane_config;
15164 dev_priv->display.crtc_compute_clock = pnv_crtc_compute_clock;
15165 dev_priv->display.crtc_enable = i9xx_crtc_enable;
15166 dev_priv->display.crtc_disable = i9xx_crtc_disable;
81c97f52 15167 } else if (!IS_GEN2(dev_priv)) {
0e8ffe1b 15168 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
5724dbd1
DL
15169 dev_priv->display.get_initial_plane_config =
15170 i9xx_get_initial_plane_config;
d6dfee7a 15171 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
76e5a89c
DV
15172 dev_priv->display.crtc_enable = i9xx_crtc_enable;
15173 dev_priv->display.crtc_disable = i9xx_crtc_disable;
81c97f52
ACO
15174 } else {
15175 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
15176 dev_priv->display.get_initial_plane_config =
15177 i9xx_get_initial_plane_config;
15178 dev_priv->display.crtc_compute_clock = i8xx_crtc_compute_clock;
15179 dev_priv->display.crtc_enable = i9xx_crtc_enable;
15180 dev_priv->display.crtc_disable = i9xx_crtc_disable;
f564048e 15181 }
e70236a8 15182
e70236a8 15183 /* Returns the core display clock speed */
88212941 15184 if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv))
1652d19e
VS
15185 dev_priv->display.get_display_clock_speed =
15186 skylake_get_display_clock_speed;
88212941 15187 else if (IS_BROXTON(dev_priv))
acd3f3d3
BP
15188 dev_priv->display.get_display_clock_speed =
15189 broxton_get_display_clock_speed;
88212941 15190 else if (IS_BROADWELL(dev_priv))
1652d19e
VS
15191 dev_priv->display.get_display_clock_speed =
15192 broadwell_get_display_clock_speed;
88212941 15193 else if (IS_HASWELL(dev_priv))
1652d19e
VS
15194 dev_priv->display.get_display_clock_speed =
15195 haswell_get_display_clock_speed;
88212941 15196 else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
25eb05fc
JB
15197 dev_priv->display.get_display_clock_speed =
15198 valleyview_get_display_clock_speed;
88212941 15199 else if (IS_GEN5(dev_priv))
b37a6434
VS
15200 dev_priv->display.get_display_clock_speed =
15201 ilk_get_display_clock_speed;
88212941
ID
15202 else if (IS_I945G(dev_priv) || IS_BROADWATER(dev_priv) ||
15203 IS_GEN6(dev_priv) || IS_IVYBRIDGE(dev_priv))
e70236a8
JB
15204 dev_priv->display.get_display_clock_speed =
15205 i945_get_display_clock_speed;
88212941 15206 else if (IS_GM45(dev_priv))
34edce2f
VS
15207 dev_priv->display.get_display_clock_speed =
15208 gm45_get_display_clock_speed;
88212941 15209 else if (IS_CRESTLINE(dev_priv))
34edce2f
VS
15210 dev_priv->display.get_display_clock_speed =
15211 i965gm_get_display_clock_speed;
88212941 15212 else if (IS_PINEVIEW(dev_priv))
34edce2f
VS
15213 dev_priv->display.get_display_clock_speed =
15214 pnv_get_display_clock_speed;
88212941 15215 else if (IS_G33(dev_priv) || IS_G4X(dev_priv))
34edce2f
VS
15216 dev_priv->display.get_display_clock_speed =
15217 g33_get_display_clock_speed;
88212941 15218 else if (IS_I915G(dev_priv))
e70236a8
JB
15219 dev_priv->display.get_display_clock_speed =
15220 i915_get_display_clock_speed;
88212941 15221 else if (IS_I945GM(dev_priv) || IS_845G(dev_priv))
e70236a8
JB
15222 dev_priv->display.get_display_clock_speed =
15223 i9xx_misc_get_display_clock_speed;
88212941 15224 else if (IS_I915GM(dev_priv))
e70236a8
JB
15225 dev_priv->display.get_display_clock_speed =
15226 i915gm_get_display_clock_speed;
88212941 15227 else if (IS_I865G(dev_priv))
e70236a8
JB
15228 dev_priv->display.get_display_clock_speed =
15229 i865_get_display_clock_speed;
88212941 15230 else if (IS_I85X(dev_priv))
e70236a8 15231 dev_priv->display.get_display_clock_speed =
1b1d2716 15232 i85x_get_display_clock_speed;
623e01e5 15233 else { /* 830 */
88212941 15234 WARN(!IS_I830(dev_priv), "Unknown platform. Assuming 133 MHz CDCLK\n");
e70236a8
JB
15235 dev_priv->display.get_display_clock_speed =
15236 i830_get_display_clock_speed;
623e01e5 15237 }
e70236a8 15238
88212941 15239 if (IS_GEN5(dev_priv)) {
3bb11b53 15240 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
88212941 15241 } else if (IS_GEN6(dev_priv)) {
3bb11b53 15242 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
88212941 15243 } else if (IS_IVYBRIDGE(dev_priv)) {
3bb11b53
SJ
15244 /* FIXME: detect B0+ stepping and use auto training */
15245 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
88212941 15246 } else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
3bb11b53 15247 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
445e780b
VS
15248 }
15249
15250 if (IS_BROADWELL(dev_priv)) {
15251 dev_priv->display.modeset_commit_cdclk =
15252 broadwell_modeset_commit_cdclk;
15253 dev_priv->display.modeset_calc_cdclk =
15254 broadwell_modeset_calc_cdclk;
88212941 15255 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
27c329ed
ML
15256 dev_priv->display.modeset_commit_cdclk =
15257 valleyview_modeset_commit_cdclk;
15258 dev_priv->display.modeset_calc_cdclk =
15259 valleyview_modeset_calc_cdclk;
88212941 15260 } else if (IS_BROXTON(dev_priv)) {
27c329ed 15261 dev_priv->display.modeset_commit_cdclk =
324513c0 15262 bxt_modeset_commit_cdclk;
27c329ed 15263 dev_priv->display.modeset_calc_cdclk =
324513c0 15264 bxt_modeset_calc_cdclk;
c89e39f3
CT
15265 } else if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv)) {
15266 dev_priv->display.modeset_commit_cdclk =
15267 skl_modeset_commit_cdclk;
15268 dev_priv->display.modeset_calc_cdclk =
15269 skl_modeset_calc_cdclk;
e70236a8 15270 }
5a21b665
DV
15271
15272 switch (INTEL_INFO(dev_priv)->gen) {
15273 case 2:
15274 dev_priv->display.queue_flip = intel_gen2_queue_flip;
15275 break;
15276
15277 case 3:
15278 dev_priv->display.queue_flip = intel_gen3_queue_flip;
15279 break;
15280
15281 case 4:
15282 case 5:
15283 dev_priv->display.queue_flip = intel_gen4_queue_flip;
15284 break;
15285
15286 case 6:
15287 dev_priv->display.queue_flip = intel_gen6_queue_flip;
15288 break;
15289 case 7:
15290 case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
15291 dev_priv->display.queue_flip = intel_gen7_queue_flip;
15292 break;
15293 case 9:
15294 /* Drop through - unsupported since execlist only. */
15295 default:
15296 /* Default just returns -ENODEV to indicate unsupported */
15297 dev_priv->display.queue_flip = intel_default_queue_flip;
15298 }
e70236a8
JB
15299}
15300
b690e96c
JB
15301/*
15302 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
15303 * resume, or other times. This quirk makes sure that's the case for
15304 * affected systems.
15305 */
0206e353 15306static void quirk_pipea_force(struct drm_device *dev)
b690e96c 15307{
fac5e23e 15308 struct drm_i915_private *dev_priv = to_i915(dev);
b690e96c
JB
15309
15310 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
bc0daf48 15311 DRM_INFO("applying pipe a force quirk\n");
b690e96c
JB
15312}
15313
b6b5d049
VS
15314static void quirk_pipeb_force(struct drm_device *dev)
15315{
fac5e23e 15316 struct drm_i915_private *dev_priv = to_i915(dev);
b6b5d049
VS
15317
15318 dev_priv->quirks |= QUIRK_PIPEB_FORCE;
15319 DRM_INFO("applying pipe b force quirk\n");
15320}
15321
435793df
KP
15322/*
15323 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
15324 */
15325static void quirk_ssc_force_disable(struct drm_device *dev)
15326{
fac5e23e 15327 struct drm_i915_private *dev_priv = to_i915(dev);
435793df 15328 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
bc0daf48 15329 DRM_INFO("applying lvds SSC disable quirk\n");
435793df
KP
15330}
15331
4dca20ef 15332/*
5a15ab5b
CE
15333 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
15334 * brightness value
4dca20ef
CE
15335 */
15336static void quirk_invert_brightness(struct drm_device *dev)
15337{
fac5e23e 15338 struct drm_i915_private *dev_priv = to_i915(dev);
4dca20ef 15339 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
bc0daf48 15340 DRM_INFO("applying inverted panel brightness quirk\n");
435793df
KP
15341}
15342
9c72cc6f
SD
15343/* Some VBT's incorrectly indicate no backlight is present */
15344static void quirk_backlight_present(struct drm_device *dev)
15345{
fac5e23e 15346 struct drm_i915_private *dev_priv = to_i915(dev);
9c72cc6f
SD
15347 dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
15348 DRM_INFO("applying backlight present quirk\n");
15349}
15350
b690e96c
JB
15351struct intel_quirk {
15352 int device;
15353 int subsystem_vendor;
15354 int subsystem_device;
15355 void (*hook)(struct drm_device *dev);
15356};
15357
5f85f176
EE
15358/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
15359struct intel_dmi_quirk {
15360 void (*hook)(struct drm_device *dev);
15361 const struct dmi_system_id (*dmi_id_list)[];
15362};
15363
15364static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
15365{
15366 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
15367 return 1;
15368}
15369
15370static const struct intel_dmi_quirk intel_dmi_quirks[] = {
15371 {
15372 .dmi_id_list = &(const struct dmi_system_id[]) {
15373 {
15374 .callback = intel_dmi_reverse_brightness,
15375 .ident = "NCR Corporation",
15376 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
15377 DMI_MATCH(DMI_PRODUCT_NAME, ""),
15378 },
15379 },
15380 { } /* terminating entry */
15381 },
15382 .hook = quirk_invert_brightness,
15383 },
15384};
15385
c43b5634 15386static struct intel_quirk intel_quirks[] = {
b690e96c
JB
15387 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
15388 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
15389
b690e96c
JB
15390 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
15391 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
15392
5f080c0f
VS
15393 /* 830 needs to leave pipe A & dpll A up */
15394 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
15395
b6b5d049
VS
15396 /* 830 needs to leave pipe B & dpll B up */
15397 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
15398
435793df
KP
15399 /* Lenovo U160 cannot use SSC on LVDS */
15400 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
070d329a
MAS
15401
15402 /* Sony Vaio Y cannot use SSC on LVDS */
15403 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
5a15ab5b 15404
be505f64
AH
15405 /* Acer Aspire 5734Z must invert backlight brightness */
15406 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
15407
15408 /* Acer/eMachines G725 */
15409 { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
15410
15411 /* Acer/eMachines e725 */
15412 { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
15413
15414 /* Acer/Packard Bell NCL20 */
15415 { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
15416
15417 /* Acer Aspire 4736Z */
15418 { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
0f540c3a
JN
15419
15420 /* Acer Aspire 5336 */
15421 { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
2e93a1aa
SD
15422
15423 /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
15424 { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
d4967d8c 15425
dfb3d47b
SD
15426 /* Acer C720 Chromebook (Core i3 4005U) */
15427 { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
15428
b2a9601c 15429 /* Apple Macbook 2,1 (Core 2 T7400) */
15430 { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
15431
1b9448b0
JN
15432 /* Apple Macbook 4,1 */
15433 { 0x2a02, 0x106b, 0x00a1, quirk_backlight_present },
15434
d4967d8c
SD
15435 /* Toshiba CB35 Chromebook (Celeron 2955U) */
15436 { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
724cb06f
SD
15437
15438 /* HP Chromebook 14 (Celeron 2955U) */
15439 { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
cf6f0af9
JN
15440
15441 /* Dell Chromebook 11 */
15442 { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
9be64eee
JN
15443
15444 /* Dell Chromebook 11 (2015 version) */
15445 { 0x0a16, 0x1028, 0x0a35, quirk_backlight_present },
b690e96c
JB
15446};
15447
15448static void intel_init_quirks(struct drm_device *dev)
15449{
15450 struct pci_dev *d = dev->pdev;
15451 int i;
15452
15453 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
15454 struct intel_quirk *q = &intel_quirks[i];
15455
15456 if (d->device == q->device &&
15457 (d->subsystem_vendor == q->subsystem_vendor ||
15458 q->subsystem_vendor == PCI_ANY_ID) &&
15459 (d->subsystem_device == q->subsystem_device ||
15460 q->subsystem_device == PCI_ANY_ID))
15461 q->hook(dev);
15462 }
5f85f176
EE
15463 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
15464 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
15465 intel_dmi_quirks[i].hook(dev);
15466 }
b690e96c
JB
15467}
15468
9cce37f4
JB
15469/* Disable the VGA plane that we never use */
15470static void i915_disable_vga(struct drm_device *dev)
15471{
fac5e23e 15472 struct drm_i915_private *dev_priv = to_i915(dev);
9cce37f4 15473 u8 sr1;
f0f59a00 15474 i915_reg_t vga_reg = i915_vgacntrl_reg(dev);
9cce37f4 15475
2b37c616 15476 /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
9cce37f4 15477 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
3fdcf431 15478 outb(SR01, VGA_SR_INDEX);
9cce37f4
JB
15479 sr1 = inb(VGA_SR_DATA);
15480 outb(sr1 | 1<<5, VGA_SR_DATA);
15481 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
15482 udelay(300);
15483
01f5a626 15484 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
9cce37f4
JB
15485 POSTING_READ(vga_reg);
15486}
15487
f817586c
DV
15488void intel_modeset_init_hw(struct drm_device *dev)
15489{
fac5e23e 15490 struct drm_i915_private *dev_priv = to_i915(dev);
1a617b77 15491
b6283055 15492 intel_update_cdclk(dev);
1a617b77
ML
15493
15494 dev_priv->atomic_cdclk_freq = dev_priv->cdclk_freq;
15495
f817586c 15496 intel_init_clock_gating(dev);
f817586c
DV
15497}
15498
d93c0372
MR
15499/*
15500 * Calculate what we think the watermarks should be for the state we've read
15501 * out of the hardware and then immediately program those watermarks so that
15502 * we ensure the hardware settings match our internal state.
15503 *
15504 * We can calculate what we think WM's should be by creating a duplicate of the
15505 * current state (which was constructed during hardware readout) and running it
15506 * through the atomic check code to calculate new watermark values in the
15507 * state object.
15508 */
15509static void sanitize_watermarks(struct drm_device *dev)
15510{
15511 struct drm_i915_private *dev_priv = to_i915(dev);
15512 struct drm_atomic_state *state;
15513 struct drm_crtc *crtc;
15514 struct drm_crtc_state *cstate;
15515 struct drm_modeset_acquire_ctx ctx;
15516 int ret;
15517 int i;
15518
15519 /* Only supported on platforms that use atomic watermark design */
ed4a6a7c 15520 if (!dev_priv->display.optimize_watermarks)
d93c0372
MR
15521 return;
15522
15523 /*
15524 * We need to hold connection_mutex before calling duplicate_state so
15525 * that the connector loop is protected.
15526 */
15527 drm_modeset_acquire_init(&ctx, 0);
15528retry:
0cd1262d 15529 ret = drm_modeset_lock_all_ctx(dev, &ctx);
d93c0372
MR
15530 if (ret == -EDEADLK) {
15531 drm_modeset_backoff(&ctx);
15532 goto retry;
15533 } else if (WARN_ON(ret)) {
0cd1262d 15534 goto fail;
d93c0372
MR
15535 }
15536
15537 state = drm_atomic_helper_duplicate_state(dev, &ctx);
15538 if (WARN_ON(IS_ERR(state)))
0cd1262d 15539 goto fail;
d93c0372 15540
ed4a6a7c
MR
15541 /*
15542 * Hardware readout is the only time we don't want to calculate
15543 * intermediate watermarks (since we don't trust the current
15544 * watermarks).
15545 */
15546 to_intel_atomic_state(state)->skip_intermediate_wm = true;
15547
d93c0372
MR
15548 ret = intel_atomic_check(dev, state);
15549 if (ret) {
15550 /*
15551 * If we fail here, it means that the hardware appears to be
15552 * programmed in a way that shouldn't be possible, given our
15553 * understanding of watermark requirements. This might mean a
15554 * mistake in the hardware readout code or a mistake in the
15555 * watermark calculations for a given platform. Raise a WARN
15556 * so that this is noticeable.
15557 *
15558 * If this actually happens, we'll have to just leave the
15559 * BIOS-programmed watermarks untouched and hope for the best.
15560 */
15561 WARN(true, "Could not determine valid watermarks for inherited state\n");
0cd1262d 15562 goto fail;
d93c0372
MR
15563 }
15564
15565 /* Write calculated watermark values back */
d93c0372
MR
15566 for_each_crtc_in_state(state, crtc, cstate, i) {
15567 struct intel_crtc_state *cs = to_intel_crtc_state(cstate);
15568
ed4a6a7c
MR
15569 cs->wm.need_postvbl_update = true;
15570 dev_priv->display.optimize_watermarks(cs);
d93c0372
MR
15571 }
15572
15573 drm_atomic_state_free(state);
0cd1262d 15574fail:
d93c0372
MR
15575 drm_modeset_drop_locks(&ctx);
15576 drm_modeset_acquire_fini(&ctx);
15577}
15578
79e53945
JB
15579void intel_modeset_init(struct drm_device *dev)
15580{
72e96d64
JL
15581 struct drm_i915_private *dev_priv = to_i915(dev);
15582 struct i915_ggtt *ggtt = &dev_priv->ggtt;
1fe47785 15583 int sprite, ret;
8cc87b75 15584 enum pipe pipe;
46f297fb 15585 struct intel_crtc *crtc;
79e53945
JB
15586
15587 drm_mode_config_init(dev);
15588
15589 dev->mode_config.min_width = 0;
15590 dev->mode_config.min_height = 0;
15591
019d96cb
DA
15592 dev->mode_config.preferred_depth = 24;
15593 dev->mode_config.prefer_shadow = 1;
15594
25bab385
TU
15595 dev->mode_config.allow_fb_modifiers = true;
15596
e6ecefaa 15597 dev->mode_config.funcs = &intel_mode_funcs;
79e53945 15598
b690e96c
JB
15599 intel_init_quirks(dev);
15600
1fa61106
ED
15601 intel_init_pm(dev);
15602
e3c74757
BW
15603 if (INTEL_INFO(dev)->num_pipes == 0)
15604 return;
15605
69f92f67
LW
15606 /*
15607 * There may be no VBT; and if the BIOS enabled SSC we can
15608 * just keep using it to avoid unnecessary flicker. Whereas if the
15609 * BIOS isn't using it, don't assume it will work even if the VBT
15610 * indicates as much.
15611 */
15612 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)) {
15613 bool bios_lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
15614 DREF_SSC1_ENABLE);
15615
15616 if (dev_priv->vbt.lvds_use_ssc != bios_lvds_use_ssc) {
15617 DRM_DEBUG_KMS("SSC %sabled by BIOS, overriding VBT which says %sabled\n",
15618 bios_lvds_use_ssc ? "en" : "dis",
15619 dev_priv->vbt.lvds_use_ssc ? "en" : "dis");
15620 dev_priv->vbt.lvds_use_ssc = bios_lvds_use_ssc;
15621 }
15622 }
15623
a6c45cf0
CW
15624 if (IS_GEN2(dev)) {
15625 dev->mode_config.max_width = 2048;
15626 dev->mode_config.max_height = 2048;
15627 } else if (IS_GEN3(dev)) {
5e4d6fa7
KP
15628 dev->mode_config.max_width = 4096;
15629 dev->mode_config.max_height = 4096;
79e53945 15630 } else {
a6c45cf0
CW
15631 dev->mode_config.max_width = 8192;
15632 dev->mode_config.max_height = 8192;
79e53945 15633 }
068be561 15634
dc41c154
VS
15635 if (IS_845G(dev) || IS_I865G(dev)) {
15636 dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
15637 dev->mode_config.cursor_height = 1023;
15638 } else if (IS_GEN2(dev)) {
068be561
DL
15639 dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
15640 dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
15641 } else {
15642 dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
15643 dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
15644 }
15645
72e96d64 15646 dev->mode_config.fb_base = ggtt->mappable_base;
79e53945 15647
28c97730 15648 DRM_DEBUG_KMS("%d display pipe%s available.\n",
7eb552ae
BW
15649 INTEL_INFO(dev)->num_pipes,
15650 INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
79e53945 15651
055e393f 15652 for_each_pipe(dev_priv, pipe) {
8cc87b75 15653 intel_crtc_init(dev, pipe);
3bdcfc0c 15654 for_each_sprite(dev_priv, pipe, sprite) {
1fe47785 15655 ret = intel_plane_init(dev, pipe, sprite);
7f1f3851 15656 if (ret)
06da8da2 15657 DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
1fe47785 15658 pipe_name(pipe), sprite_name(pipe, sprite), ret);
7f1f3851 15659 }
79e53945
JB
15660 }
15661
bfa7df01
VS
15662 intel_update_czclk(dev_priv);
15663 intel_update_cdclk(dev);
15664
e72f9fbf 15665 intel_shared_dpll_init(dev);
ee7b9f93 15666
b2045352
VS
15667 if (dev_priv->max_cdclk_freq == 0)
15668 intel_update_max_cdclk(dev);
15669
9cce37f4
JB
15670 /* Just disable it once at startup */
15671 i915_disable_vga(dev);
79e53945 15672 intel_setup_outputs(dev);
11be49eb 15673
6e9f798d 15674 drm_modeset_lock_all(dev);
043e9bda 15675 intel_modeset_setup_hw_state(dev);
6e9f798d 15676 drm_modeset_unlock_all(dev);
46f297fb 15677
d3fcc808 15678 for_each_intel_crtc(dev, crtc) {
eeebeac5
ML
15679 struct intel_initial_plane_config plane_config = {};
15680
46f297fb
JB
15681 if (!crtc->active)
15682 continue;
15683
46f297fb 15684 /*
46f297fb
JB
15685 * Note that reserving the BIOS fb up front prevents us
15686 * from stuffing other stolen allocations like the ring
15687 * on top. This prevents some ugliness at boot time, and
15688 * can even allow for smooth boot transitions if the BIOS
15689 * fb is large enough for the active pipe configuration.
15690 */
eeebeac5
ML
15691 dev_priv->display.get_initial_plane_config(crtc,
15692 &plane_config);
15693
15694 /*
15695 * If the fb is shared between multiple heads, we'll
15696 * just get the first one.
15697 */
15698 intel_find_initial_plane_obj(crtc, &plane_config);
46f297fb 15699 }
d93c0372
MR
15700
15701 /*
15702 * Make sure hardware watermarks really match the state we read out.
15703 * Note that we need to do this after reconstructing the BIOS fb's
15704 * since the watermark calculation done here will use pstate->fb.
15705 */
15706 sanitize_watermarks(dev);
2c7111db
CW
15707}
15708
7fad798e
DV
15709static void intel_enable_pipe_a(struct drm_device *dev)
15710{
15711 struct intel_connector *connector;
15712 struct drm_connector *crt = NULL;
15713 struct intel_load_detect_pipe load_detect_temp;
208bf9fd 15714 struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
7fad798e
DV
15715
15716 /* We can't just switch on the pipe A, we need to set things up with a
15717 * proper mode and output configuration. As a gross hack, enable pipe A
15718 * by enabling the load detect pipe once. */
3a3371ff 15719 for_each_intel_connector(dev, connector) {
7fad798e
DV
15720 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
15721 crt = &connector->base;
15722 break;
15723 }
15724 }
15725
15726 if (!crt)
15727 return;
15728
208bf9fd 15729 if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
49172fee 15730 intel_release_load_detect_pipe(crt, &load_detect_temp, ctx);
7fad798e
DV
15731}
15732
fa555837
DV
15733static bool
15734intel_check_plane_mapping(struct intel_crtc *crtc)
15735{
7eb552ae 15736 struct drm_device *dev = crtc->base.dev;
fac5e23e 15737 struct drm_i915_private *dev_priv = to_i915(dev);
649636ef 15738 u32 val;
fa555837 15739
7eb552ae 15740 if (INTEL_INFO(dev)->num_pipes == 1)
fa555837
DV
15741 return true;
15742
649636ef 15743 val = I915_READ(DSPCNTR(!crtc->plane));
fa555837
DV
15744
15745 if ((val & DISPLAY_PLANE_ENABLE) &&
15746 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
15747 return false;
15748
15749 return true;
15750}
15751
02e93c35
VS
15752static bool intel_crtc_has_encoders(struct intel_crtc *crtc)
15753{
15754 struct drm_device *dev = crtc->base.dev;
15755 struct intel_encoder *encoder;
15756
15757 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
15758 return true;
15759
15760 return false;
15761}
15762
dd756198
VS
15763static bool intel_encoder_has_connectors(struct intel_encoder *encoder)
15764{
15765 struct drm_device *dev = encoder->base.dev;
15766 struct intel_connector *connector;
15767
15768 for_each_connector_on_encoder(dev, &encoder->base, connector)
15769 return true;
15770
15771 return false;
15772}
15773
24929352
DV
15774static void intel_sanitize_crtc(struct intel_crtc *crtc)
15775{
15776 struct drm_device *dev = crtc->base.dev;
fac5e23e 15777 struct drm_i915_private *dev_priv = to_i915(dev);
4d1de975 15778 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
24929352 15779
24929352 15780 /* Clear any frame start delays used for debugging left by the BIOS */
4d1de975
JN
15781 if (!transcoder_is_dsi(cpu_transcoder)) {
15782 i915_reg_t reg = PIPECONF(cpu_transcoder);
15783
15784 I915_WRITE(reg,
15785 I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
15786 }
24929352 15787
d3eaf884 15788 /* restore vblank interrupts to correct state */
9625604c 15789 drm_crtc_vblank_reset(&crtc->base);
d297e103 15790 if (crtc->active) {
f9cd7b88
VS
15791 struct intel_plane *plane;
15792
9625604c 15793 drm_crtc_vblank_on(&crtc->base);
f9cd7b88
VS
15794
15795 /* Disable everything but the primary plane */
15796 for_each_intel_plane_on_crtc(dev, crtc, plane) {
15797 if (plane->base.type == DRM_PLANE_TYPE_PRIMARY)
15798 continue;
15799
15800 plane->disable_plane(&plane->base, &crtc->base);
15801 }
9625604c 15802 }
d3eaf884 15803
24929352 15804 /* We need to sanitize the plane -> pipe mapping first because this will
fa555837
DV
15805 * disable the crtc (and hence change the state) if it is wrong. Note
15806 * that gen4+ has a fixed plane -> pipe mapping. */
15807 if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
24929352
DV
15808 bool plane;
15809
78108b7c
VS
15810 DRM_DEBUG_KMS("[CRTC:%d:%s] wrong plane connection detected!\n",
15811 crtc->base.base.id, crtc->base.name);
24929352
DV
15812
15813 /* Pipe has the wrong plane attached and the plane is active.
15814 * Temporarily change the plane mapping and disable everything
15815 * ... */
15816 plane = crtc->plane;
b70709a6 15817 to_intel_plane_state(crtc->base.primary->state)->visible = true;
24929352 15818 crtc->plane = !plane;
b17d48e2 15819 intel_crtc_disable_noatomic(&crtc->base);
24929352 15820 crtc->plane = plane;
24929352 15821 }
24929352 15822
7fad798e
DV
15823 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
15824 crtc->pipe == PIPE_A && !crtc->active) {
15825 /* BIOS forgot to enable pipe A, this mostly happens after
15826 * resume. Force-enable the pipe to fix this, the update_dpms
15827 * call below we restore the pipe to the right state, but leave
15828 * the required bits on. */
15829 intel_enable_pipe_a(dev);
15830 }
15831
24929352
DV
15832 /* Adjust the state of the output pipe according to whether we
15833 * have active connectors/encoders. */
842e0307 15834 if (crtc->active && !intel_crtc_has_encoders(crtc))
b17d48e2 15835 intel_crtc_disable_noatomic(&crtc->base);
24929352 15836
a3ed6aad 15837 if (crtc->active || HAS_GMCH_DISPLAY(dev)) {
4cc31489
DV
15838 /*
15839 * We start out with underrun reporting disabled to avoid races.
15840 * For correct bookkeeping mark this on active crtcs.
15841 *
c5ab3bc0
DV
15842 * Also on gmch platforms we dont have any hardware bits to
15843 * disable the underrun reporting. Which means we need to start
15844 * out with underrun reporting disabled also on inactive pipes,
15845 * since otherwise we'll complain about the garbage we read when
15846 * e.g. coming up after runtime pm.
15847 *
4cc31489
DV
15848 * No protection against concurrent access is required - at
15849 * worst a fifo underrun happens which also sets this to false.
15850 */
15851 crtc->cpu_fifo_underrun_disabled = true;
15852 crtc->pch_fifo_underrun_disabled = true;
15853 }
24929352
DV
15854}
15855
15856static void intel_sanitize_encoder(struct intel_encoder *encoder)
15857{
15858 struct intel_connector *connector;
15859 struct drm_device *dev = encoder->base.dev;
15860
15861 /* We need to check both for a crtc link (meaning that the
15862 * encoder is active and trying to read from a pipe) and the
15863 * pipe itself being active. */
15864 bool has_active_crtc = encoder->base.crtc &&
15865 to_intel_crtc(encoder->base.crtc)->active;
15866
dd756198 15867 if (intel_encoder_has_connectors(encoder) && !has_active_crtc) {
24929352
DV
15868 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
15869 encoder->base.base.id,
8e329a03 15870 encoder->base.name);
24929352
DV
15871
15872 /* Connector is active, but has no active pipe. This is
15873 * fallout from our resume register restoring. Disable
15874 * the encoder manually again. */
15875 if (encoder->base.crtc) {
15876 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
15877 encoder->base.base.id,
8e329a03 15878 encoder->base.name);
24929352 15879 encoder->disable(encoder);
a62d1497
VS
15880 if (encoder->post_disable)
15881 encoder->post_disable(encoder);
24929352 15882 }
7f1950fb 15883 encoder->base.crtc = NULL;
24929352
DV
15884
15885 /* Inconsistent output/port/pipe state happens presumably due to
15886 * a bug in one of the get_hw_state functions. Or someplace else
15887 * in our code, like the register restore mess on resume. Clamp
15888 * things to off as a safer default. */
3a3371ff 15889 for_each_intel_connector(dev, connector) {
24929352
DV
15890 if (connector->encoder != encoder)
15891 continue;
7f1950fb
EE
15892 connector->base.dpms = DRM_MODE_DPMS_OFF;
15893 connector->base.encoder = NULL;
24929352
DV
15894 }
15895 }
15896 /* Enabled encoders without active connectors will be fixed in
15897 * the crtc fixup. */
15898}
15899
04098753 15900void i915_redisable_vga_power_on(struct drm_device *dev)
0fde901f 15901{
fac5e23e 15902 struct drm_i915_private *dev_priv = to_i915(dev);
f0f59a00 15903 i915_reg_t vga_reg = i915_vgacntrl_reg(dev);
0fde901f 15904
04098753
ID
15905 if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
15906 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
15907 i915_disable_vga(dev);
15908 }
15909}
15910
15911void i915_redisable_vga(struct drm_device *dev)
15912{
fac5e23e 15913 struct drm_i915_private *dev_priv = to_i915(dev);
04098753 15914
8dc8a27c
PZ
15915 /* This function can be called both from intel_modeset_setup_hw_state or
15916 * at a very early point in our resume sequence, where the power well
15917 * structures are not yet restored. Since this function is at a very
15918 * paranoid "someone might have enabled VGA while we were not looking"
15919 * level, just check if the power well is enabled instead of trying to
15920 * follow the "don't touch the power well if we don't need it" policy
15921 * the rest of the driver uses. */
6392f847 15922 if (!intel_display_power_get_if_enabled(dev_priv, POWER_DOMAIN_VGA))
8dc8a27c
PZ
15923 return;
15924
04098753 15925 i915_redisable_vga_power_on(dev);
6392f847
ID
15926
15927 intel_display_power_put(dev_priv, POWER_DOMAIN_VGA);
0fde901f
KM
15928}
15929
f9cd7b88 15930static bool primary_get_hw_state(struct intel_plane *plane)
98ec7739 15931{
f9cd7b88 15932 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
98ec7739 15933
f9cd7b88 15934 return I915_READ(DSPCNTR(plane->plane)) & DISPLAY_PLANE_ENABLE;
d032ffa0
ML
15935}
15936
f9cd7b88
VS
15937/* FIXME read out full plane state for all planes */
15938static void readout_plane_state(struct intel_crtc *crtc)
d032ffa0 15939{
b26d3ea3 15940 struct drm_plane *primary = crtc->base.primary;
f9cd7b88 15941 struct intel_plane_state *plane_state =
b26d3ea3 15942 to_intel_plane_state(primary->state);
d032ffa0 15943
19b8d387 15944 plane_state->visible = crtc->active &&
b26d3ea3
ML
15945 primary_get_hw_state(to_intel_plane(primary));
15946
15947 if (plane_state->visible)
15948 crtc->base.state->plane_mask |= 1 << drm_plane_index(primary);
98ec7739
VS
15949}
15950
30e984df 15951static void intel_modeset_readout_hw_state(struct drm_device *dev)
24929352 15952{
fac5e23e 15953 struct drm_i915_private *dev_priv = to_i915(dev);
24929352 15954 enum pipe pipe;
24929352
DV
15955 struct intel_crtc *crtc;
15956 struct intel_encoder *encoder;
15957 struct intel_connector *connector;
5358901f 15958 int i;
24929352 15959
565602d7
ML
15960 dev_priv->active_crtcs = 0;
15961
d3fcc808 15962 for_each_intel_crtc(dev, crtc) {
565602d7
ML
15963 struct intel_crtc_state *crtc_state = crtc->config;
15964 int pixclk = 0;
3b117c8f 15965
ec2dc6a0 15966 __drm_atomic_helper_crtc_destroy_state(&crtc_state->base);
565602d7
ML
15967 memset(crtc_state, 0, sizeof(*crtc_state));
15968 crtc_state->base.crtc = &crtc->base;
24929352 15969
565602d7
ML
15970 crtc_state->base.active = crtc_state->base.enable =
15971 dev_priv->display.get_pipe_config(crtc, crtc_state);
15972
15973 crtc->base.enabled = crtc_state->base.enable;
15974 crtc->active = crtc_state->base.active;
15975
15976 if (crtc_state->base.active) {
15977 dev_priv->active_crtcs |= 1 << crtc->pipe;
15978
c89e39f3 15979 if (INTEL_GEN(dev_priv) >= 9 || IS_BROADWELL(dev_priv))
565602d7 15980 pixclk = ilk_pipe_pixel_rate(crtc_state);
9558d15d 15981 else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
565602d7
ML
15982 pixclk = crtc_state->base.adjusted_mode.crtc_clock;
15983 else
15984 WARN_ON(dev_priv->display.modeset_calc_cdclk);
9558d15d
VS
15985
15986 /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
15987 if (IS_BROADWELL(dev_priv) && crtc_state->ips_enabled)
15988 pixclk = DIV_ROUND_UP(pixclk * 100, 95);
565602d7
ML
15989 }
15990
15991 dev_priv->min_pixclk[crtc->pipe] = pixclk;
b70709a6 15992
f9cd7b88 15993 readout_plane_state(crtc);
24929352 15994
78108b7c
VS
15995 DRM_DEBUG_KMS("[CRTC:%d:%s] hw state readout: %s\n",
15996 crtc->base.base.id, crtc->base.name,
24929352
DV
15997 crtc->active ? "enabled" : "disabled");
15998 }
15999
5358901f
DV
16000 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
16001 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
16002
2edd6443
ACO
16003 pll->on = pll->funcs.get_hw_state(dev_priv, pll,
16004 &pll->config.hw_state);
3e369b76 16005 pll->config.crtc_mask = 0;
d3fcc808 16006 for_each_intel_crtc(dev, crtc) {
2dd66ebd 16007 if (crtc->active && crtc->config->shared_dpll == pll)
3e369b76 16008 pll->config.crtc_mask |= 1 << crtc->pipe;
5358901f 16009 }
2dd66ebd 16010 pll->active_mask = pll->config.crtc_mask;
5358901f 16011
1e6f2ddc 16012 DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
3e369b76 16013 pll->name, pll->config.crtc_mask, pll->on);
5358901f
DV
16014 }
16015
b2784e15 16016 for_each_intel_encoder(dev, encoder) {
24929352
DV
16017 pipe = 0;
16018
16019 if (encoder->get_hw_state(encoder, &pipe)) {
045ac3b5
JB
16020 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
16021 encoder->base.crtc = &crtc->base;
253c84c8 16022 crtc->config->output_types |= 1 << encoder->type;
6e3c9717 16023 encoder->get_config(encoder, crtc->config);
24929352
DV
16024 } else {
16025 encoder->base.crtc = NULL;
16026 }
16027
6f2bcceb 16028 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
24929352 16029 encoder->base.base.id,
8e329a03 16030 encoder->base.name,
24929352 16031 encoder->base.crtc ? "enabled" : "disabled",
6f2bcceb 16032 pipe_name(pipe));
24929352
DV
16033 }
16034
3a3371ff 16035 for_each_intel_connector(dev, connector) {
24929352
DV
16036 if (connector->get_hw_state(connector)) {
16037 connector->base.dpms = DRM_MODE_DPMS_ON;
2aa974c9
ML
16038
16039 encoder = connector->encoder;
16040 connector->base.encoder = &encoder->base;
16041
16042 if (encoder->base.crtc &&
16043 encoder->base.crtc->state->active) {
16044 /*
16045 * This has to be done during hardware readout
16046 * because anything calling .crtc_disable may
16047 * rely on the connector_mask being accurate.
16048 */
16049 encoder->base.crtc->state->connector_mask |=
16050 1 << drm_connector_index(&connector->base);
e87a52b3
ML
16051 encoder->base.crtc->state->encoder_mask |=
16052 1 << drm_encoder_index(&encoder->base);
2aa974c9
ML
16053 }
16054
24929352
DV
16055 } else {
16056 connector->base.dpms = DRM_MODE_DPMS_OFF;
16057 connector->base.encoder = NULL;
16058 }
16059 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
16060 connector->base.base.id,
c23cc417 16061 connector->base.name,
24929352
DV
16062 connector->base.encoder ? "enabled" : "disabled");
16063 }
7f4c6284
VS
16064
16065 for_each_intel_crtc(dev, crtc) {
16066 crtc->base.hwmode = crtc->config->base.adjusted_mode;
16067
16068 memset(&crtc->base.mode, 0, sizeof(crtc->base.mode));
16069 if (crtc->base.state->active) {
16070 intel_mode_from_pipe_config(&crtc->base.mode, crtc->config);
16071 intel_mode_from_pipe_config(&crtc->base.state->adjusted_mode, crtc->config);
16072 WARN_ON(drm_atomic_set_mode_for_crtc(crtc->base.state, &crtc->base.mode));
16073
16074 /*
16075 * The initial mode needs to be set in order to keep
16076 * the atomic core happy. It wants a valid mode if the
16077 * crtc's enabled, so we do the above call.
16078 *
16079 * At this point some state updated by the connectors
16080 * in their ->detect() callback has not run yet, so
16081 * no recalculation can be done yet.
16082 *
16083 * Even if we could do a recalculation and modeset
16084 * right now it would cause a double modeset if
16085 * fbdev or userspace chooses a different initial mode.
16086 *
16087 * If that happens, someone indicated they wanted a
16088 * mode change, which means it's safe to do a full
16089 * recalculation.
16090 */
16091 crtc->base.state->mode.private_flags = I915_MODE_FLAG_INHERITED;
9eca6832
VS
16092
16093 drm_calc_timestamping_constants(&crtc->base, &crtc->base.hwmode);
16094 update_scanline_offset(crtc);
7f4c6284 16095 }
e3b247da
VS
16096
16097 intel_pipe_config_sanity_check(dev_priv, crtc->config);
7f4c6284 16098 }
30e984df
DV
16099}
16100
043e9bda
ML
16101/* Scan out the current hw modeset state,
16102 * and sanitizes it to the current state
16103 */
16104static void
16105intel_modeset_setup_hw_state(struct drm_device *dev)
30e984df 16106{
fac5e23e 16107 struct drm_i915_private *dev_priv = to_i915(dev);
30e984df 16108 enum pipe pipe;
30e984df
DV
16109 struct intel_crtc *crtc;
16110 struct intel_encoder *encoder;
35c95375 16111 int i;
30e984df
DV
16112
16113 intel_modeset_readout_hw_state(dev);
24929352
DV
16114
16115 /* HW state is read out, now we need to sanitize this mess. */
b2784e15 16116 for_each_intel_encoder(dev, encoder) {
24929352
DV
16117 intel_sanitize_encoder(encoder);
16118 }
16119
055e393f 16120 for_each_pipe(dev_priv, pipe) {
24929352
DV
16121 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
16122 intel_sanitize_crtc(crtc);
6e3c9717
ACO
16123 intel_dump_pipe_config(crtc, crtc->config,
16124 "[setup_hw_state]");
24929352 16125 }
9a935856 16126
d29b2f9d
ACO
16127 intel_modeset_update_connector_atomic_state(dev);
16128
35c95375
DV
16129 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
16130 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
16131
2dd66ebd 16132 if (!pll->on || pll->active_mask)
35c95375
DV
16133 continue;
16134
16135 DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
16136
2edd6443 16137 pll->funcs.disable(dev_priv, pll);
35c95375
DV
16138 pll->on = false;
16139 }
16140
666a4537 16141 if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev))
6eb1a681
VS
16142 vlv_wm_get_hw_state(dev);
16143 else if (IS_GEN9(dev))
3078999f
PB
16144 skl_wm_get_hw_state(dev);
16145 else if (HAS_PCH_SPLIT(dev))
243e6a44 16146 ilk_wm_get_hw_state(dev);
292b990e
ML
16147
16148 for_each_intel_crtc(dev, crtc) {
16149 unsigned long put_domains;
16150
74bff5f9 16151 put_domains = modeset_get_crtc_power_domains(&crtc->base, crtc->config);
292b990e
ML
16152 if (WARN_ON(put_domains))
16153 modeset_put_power_domains(dev_priv, put_domains);
16154 }
16155 intel_display_set_init_power(dev_priv, false);
010cf73d
PZ
16156
16157 intel_fbc_init_pipe_state(dev_priv);
043e9bda 16158}
7d0bc1ea 16159
043e9bda
ML
16160void intel_display_resume(struct drm_device *dev)
16161{
e2c8b870
ML
16162 struct drm_i915_private *dev_priv = to_i915(dev);
16163 struct drm_atomic_state *state = dev_priv->modeset_restore_state;
16164 struct drm_modeset_acquire_ctx ctx;
043e9bda 16165 int ret;
e2c8b870 16166 bool setup = false;
f30da187 16167
e2c8b870 16168 dev_priv->modeset_restore_state = NULL;
043e9bda 16169
ea49c9ac
ML
16170 /*
16171 * This is a cludge because with real atomic modeset mode_config.mutex
16172 * won't be taken. Unfortunately some probed state like
16173 * audio_codec_enable is still protected by mode_config.mutex, so lock
16174 * it here for now.
16175 */
16176 mutex_lock(&dev->mode_config.mutex);
e2c8b870 16177 drm_modeset_acquire_init(&ctx, 0);
043e9bda 16178
e2c8b870
ML
16179retry:
16180 ret = drm_modeset_lock_all_ctx(dev, &ctx);
043e9bda 16181
e2c8b870
ML
16182 if (ret == 0 && !setup) {
16183 setup = true;
043e9bda 16184
e2c8b870
ML
16185 intel_modeset_setup_hw_state(dev);
16186 i915_redisable_vga(dev);
45e2b5f6 16187 }
8af6cf88 16188
e2c8b870
ML
16189 if (ret == 0 && state) {
16190 struct drm_crtc_state *crtc_state;
16191 struct drm_crtc *crtc;
16192 int i;
043e9bda 16193
e2c8b870
ML
16194 state->acquire_ctx = &ctx;
16195
e3d5457c
VS
16196 /* ignore any reset values/BIOS leftovers in the WM registers */
16197 to_intel_atomic_state(state)->skip_intermediate_wm = true;
16198
e2c8b870
ML
16199 for_each_crtc_in_state(state, crtc, crtc_state, i) {
16200 /*
16201 * Force recalculation even if we restore
16202 * current state. With fast modeset this may not result
16203 * in a modeset when the state is compatible.
16204 */
16205 crtc_state->mode_changed = true;
16206 }
16207
16208 ret = drm_atomic_commit(state);
043e9bda
ML
16209 }
16210
e2c8b870
ML
16211 if (ret == -EDEADLK) {
16212 drm_modeset_backoff(&ctx);
16213 goto retry;
16214 }
043e9bda 16215
e2c8b870
ML
16216 drm_modeset_drop_locks(&ctx);
16217 drm_modeset_acquire_fini(&ctx);
ea49c9ac 16218 mutex_unlock(&dev->mode_config.mutex);
043e9bda 16219
e2c8b870
ML
16220 if (ret) {
16221 DRM_ERROR("Restoring old state failed with %i\n", ret);
16222 drm_atomic_state_free(state);
16223 }
2c7111db
CW
16224}
16225
16226void intel_modeset_gem_init(struct drm_device *dev)
16227{
dc97997a 16228 struct drm_i915_private *dev_priv = to_i915(dev);
484b41dd 16229 struct drm_crtc *c;
2ff8fde1 16230 struct drm_i915_gem_object *obj;
e0d6149b 16231 int ret;
484b41dd 16232
dc97997a 16233 intel_init_gt_powersave(dev_priv);
ae48434c 16234
1833b134 16235 intel_modeset_init_hw(dev);
02e792fb 16236
1ee8da6d 16237 intel_setup_overlay(dev_priv);
484b41dd
JB
16238
16239 /*
16240 * Make sure any fbs we allocated at startup are properly
16241 * pinned & fenced. When we do the allocation it's too early
16242 * for this.
16243 */
70e1e0ec 16244 for_each_crtc(dev, c) {
2ff8fde1
MR
16245 obj = intel_fb_obj(c->primary->fb);
16246 if (obj == NULL)
484b41dd
JB
16247 continue;
16248
e0d6149b 16249 mutex_lock(&dev->struct_mutex);
3465c580
VS
16250 ret = intel_pin_and_fence_fb_obj(c->primary->fb,
16251 c->primary->state->rotation);
e0d6149b
TU
16252 mutex_unlock(&dev->struct_mutex);
16253 if (ret) {
484b41dd
JB
16254 DRM_ERROR("failed to pin boot fb on pipe %d\n",
16255 to_intel_crtc(c)->pipe);
66e514c1 16256 drm_framebuffer_unreference(c->primary->fb);
5a21b665 16257 c->primary->fb = NULL;
36750f28 16258 c->primary->crtc = c->primary->state->crtc = NULL;
5a21b665 16259 update_state_fb(c->primary);
36750f28 16260 c->state->plane_mask &= ~(1 << drm_plane_index(c->primary));
484b41dd
JB
16261 }
16262 }
1ebaa0b9
CW
16263}
16264
16265int intel_connector_register(struct drm_connector *connector)
16266{
16267 struct intel_connector *intel_connector = to_intel_connector(connector);
16268 int ret;
16269
16270 ret = intel_backlight_device_register(intel_connector);
16271 if (ret)
16272 goto err;
16273
16274 return 0;
0962c3c9 16275
1ebaa0b9
CW
16276err:
16277 return ret;
79e53945
JB
16278}
16279
c191eca1 16280void intel_connector_unregister(struct drm_connector *connector)
4932e2c3 16281{
e63d87c0 16282 struct intel_connector *intel_connector = to_intel_connector(connector);
4932e2c3 16283
e63d87c0 16284 intel_backlight_device_unregister(intel_connector);
4932e2c3 16285 intel_panel_destroy_backlight(connector);
4932e2c3
ID
16286}
16287
79e53945
JB
16288void intel_modeset_cleanup(struct drm_device *dev)
16289{
fac5e23e 16290 struct drm_i915_private *dev_priv = to_i915(dev);
652c393a 16291
dc97997a 16292 intel_disable_gt_powersave(dev_priv);
2eb5252e 16293
fd0c0642
DV
16294 /*
16295 * Interrupts and polling as the first thing to avoid creating havoc.
2eb5252e 16296 * Too much stuff here (turning of connectors, ...) would
fd0c0642
DV
16297 * experience fancy races otherwise.
16298 */
2aeb7d3a 16299 intel_irq_uninstall(dev_priv);
eb21b92b 16300
fd0c0642
DV
16301 /*
16302 * Due to the hpd irq storm handling the hotplug work can re-arm the
16303 * poll handlers. Hence disable polling after hpd handling is shut down.
16304 */
f87ea761 16305 drm_kms_helper_poll_fini(dev);
fd0c0642 16306
723bfd70
JB
16307 intel_unregister_dsm_handler();
16308
c937ab3e 16309 intel_fbc_global_disable(dev_priv);
69341a5e 16310
1630fe75
CW
16311 /* flush any delayed tasks or pending work */
16312 flush_scheduled_work();
16313
79e53945 16314 drm_mode_config_cleanup(dev);
4d7bb011 16315
1ee8da6d 16316 intel_cleanup_overlay(dev_priv);
ae48434c 16317
dc97997a 16318 intel_cleanup_gt_powersave(dev_priv);
f5949141
DV
16319
16320 intel_teardown_gmbus(dev);
79e53945
JB
16321}
16322
df0e9248
CW
16323void intel_connector_attach_encoder(struct intel_connector *connector,
16324 struct intel_encoder *encoder)
16325{
16326 connector->encoder = encoder;
16327 drm_mode_connector_attach_encoder(&connector->base,
16328 &encoder->base);
79e53945 16329}
28d52043
DA
16330
16331/*
16332 * set vga decode state - true == enable VGA decode
16333 */
16334int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
16335{
fac5e23e 16336 struct drm_i915_private *dev_priv = to_i915(dev);
a885b3cc 16337 unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
28d52043
DA
16338 u16 gmch_ctrl;
16339
75fa041d
CW
16340 if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
16341 DRM_ERROR("failed to read control word\n");
16342 return -EIO;
16343 }
16344
c0cc8a55
CW
16345 if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
16346 return 0;
16347
28d52043
DA
16348 if (state)
16349 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
16350 else
16351 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
75fa041d
CW
16352
16353 if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
16354 DRM_ERROR("failed to write control word\n");
16355 return -EIO;
16356 }
16357
28d52043
DA
16358 return 0;
16359}
c4a1d9e4 16360
c4a1d9e4 16361struct intel_display_error_state {
ff57f1b0
PZ
16362
16363 u32 power_well_driver;
16364
63b66e5b
CW
16365 int num_transcoders;
16366
c4a1d9e4
CW
16367 struct intel_cursor_error_state {
16368 u32 control;
16369 u32 position;
16370 u32 base;
16371 u32 size;
52331309 16372 } cursor[I915_MAX_PIPES];
c4a1d9e4
CW
16373
16374 struct intel_pipe_error_state {
ddf9c536 16375 bool power_domain_on;
c4a1d9e4 16376 u32 source;
f301b1e1 16377 u32 stat;
52331309 16378 } pipe[I915_MAX_PIPES];
c4a1d9e4
CW
16379
16380 struct intel_plane_error_state {
16381 u32 control;
16382 u32 stride;
16383 u32 size;
16384 u32 pos;
16385 u32 addr;
16386 u32 surface;
16387 u32 tile_offset;
52331309 16388 } plane[I915_MAX_PIPES];
63b66e5b
CW
16389
16390 struct intel_transcoder_error_state {
ddf9c536 16391 bool power_domain_on;
63b66e5b
CW
16392 enum transcoder cpu_transcoder;
16393
16394 u32 conf;
16395
16396 u32 htotal;
16397 u32 hblank;
16398 u32 hsync;
16399 u32 vtotal;
16400 u32 vblank;
16401 u32 vsync;
16402 } transcoder[4];
c4a1d9e4
CW
16403};
16404
16405struct intel_display_error_state *
c033666a 16406intel_display_capture_error_state(struct drm_i915_private *dev_priv)
c4a1d9e4 16407{
c4a1d9e4 16408 struct intel_display_error_state *error;
63b66e5b
CW
16409 int transcoders[] = {
16410 TRANSCODER_A,
16411 TRANSCODER_B,
16412 TRANSCODER_C,
16413 TRANSCODER_EDP,
16414 };
c4a1d9e4
CW
16415 int i;
16416
c033666a 16417 if (INTEL_INFO(dev_priv)->num_pipes == 0)
63b66e5b
CW
16418 return NULL;
16419
9d1cb914 16420 error = kzalloc(sizeof(*error), GFP_ATOMIC);
c4a1d9e4
CW
16421 if (error == NULL)
16422 return NULL;
16423
c033666a 16424 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
ff57f1b0
PZ
16425 error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
16426
055e393f 16427 for_each_pipe(dev_priv, i) {
ddf9c536 16428 error->pipe[i].power_domain_on =
f458ebbc
DV
16429 __intel_display_power_is_enabled(dev_priv,
16430 POWER_DOMAIN_PIPE(i));
ddf9c536 16431 if (!error->pipe[i].power_domain_on)
9d1cb914
PZ
16432 continue;
16433
5efb3e28
VS
16434 error->cursor[i].control = I915_READ(CURCNTR(i));
16435 error->cursor[i].position = I915_READ(CURPOS(i));
16436 error->cursor[i].base = I915_READ(CURBASE(i));
c4a1d9e4
CW
16437
16438 error->plane[i].control = I915_READ(DSPCNTR(i));
16439 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
c033666a 16440 if (INTEL_GEN(dev_priv) <= 3) {
51889b35 16441 error->plane[i].size = I915_READ(DSPSIZE(i));
80ca378b
PZ
16442 error->plane[i].pos = I915_READ(DSPPOS(i));
16443 }
c033666a 16444 if (INTEL_GEN(dev_priv) <= 7 && !IS_HASWELL(dev_priv))
ca291363 16445 error->plane[i].addr = I915_READ(DSPADDR(i));
c033666a 16446 if (INTEL_GEN(dev_priv) >= 4) {
c4a1d9e4
CW
16447 error->plane[i].surface = I915_READ(DSPSURF(i));
16448 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
16449 }
16450
c4a1d9e4 16451 error->pipe[i].source = I915_READ(PIPESRC(i));
f301b1e1 16452
c033666a 16453 if (HAS_GMCH_DISPLAY(dev_priv))
f301b1e1 16454 error->pipe[i].stat = I915_READ(PIPESTAT(i));
63b66e5b
CW
16455 }
16456
4d1de975 16457 /* Note: this does not include DSI transcoders. */
c033666a 16458 error->num_transcoders = INTEL_INFO(dev_priv)->num_pipes;
2d1fe073 16459 if (HAS_DDI(dev_priv))
63b66e5b
CW
16460 error->num_transcoders++; /* Account for eDP. */
16461
16462 for (i = 0; i < error->num_transcoders; i++) {
16463 enum transcoder cpu_transcoder = transcoders[i];
16464
ddf9c536 16465 error->transcoder[i].power_domain_on =
f458ebbc 16466 __intel_display_power_is_enabled(dev_priv,
38cc1daf 16467 POWER_DOMAIN_TRANSCODER(cpu_transcoder));
ddf9c536 16468 if (!error->transcoder[i].power_domain_on)
9d1cb914
PZ
16469 continue;
16470
63b66e5b
CW
16471 error->transcoder[i].cpu_transcoder = cpu_transcoder;
16472
16473 error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
16474 error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
16475 error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
16476 error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
16477 error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
16478 error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
16479 error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
c4a1d9e4
CW
16480 }
16481
16482 return error;
16483}
16484
edc3d884
MK
16485#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
16486
c4a1d9e4 16487void
edc3d884 16488intel_display_print_error_state(struct drm_i915_error_state_buf *m,
c4a1d9e4
CW
16489 struct drm_device *dev,
16490 struct intel_display_error_state *error)
16491{
fac5e23e 16492 struct drm_i915_private *dev_priv = to_i915(dev);
c4a1d9e4
CW
16493 int i;
16494
63b66e5b
CW
16495 if (!error)
16496 return;
16497
edc3d884 16498 err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
190be112 16499 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
edc3d884 16500 err_printf(m, "PWR_WELL_CTL2: %08x\n",
ff57f1b0 16501 error->power_well_driver);
055e393f 16502 for_each_pipe(dev_priv, i) {
edc3d884 16503 err_printf(m, "Pipe [%d]:\n", i);
ddf9c536 16504 err_printf(m, " Power: %s\n",
87ad3212 16505 onoff(error->pipe[i].power_domain_on));
edc3d884 16506 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
f301b1e1 16507 err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
edc3d884
MK
16508
16509 err_printf(m, "Plane [%d]:\n", i);
16510 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
16511 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
80ca378b 16512 if (INTEL_INFO(dev)->gen <= 3) {
edc3d884
MK
16513 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
16514 err_printf(m, " POS: %08x\n", error->plane[i].pos);
80ca378b 16515 }
4b71a570 16516 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
edc3d884 16517 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
c4a1d9e4 16518 if (INTEL_INFO(dev)->gen >= 4) {
edc3d884
MK
16519 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
16520 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
c4a1d9e4
CW
16521 }
16522
edc3d884
MK
16523 err_printf(m, "Cursor [%d]:\n", i);
16524 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
16525 err_printf(m, " POS: %08x\n", error->cursor[i].position);
16526 err_printf(m, " BASE: %08x\n", error->cursor[i].base);
c4a1d9e4 16527 }
63b66e5b
CW
16528
16529 for (i = 0; i < error->num_transcoders; i++) {
da205630 16530 err_printf(m, "CPU transcoder: %s\n",
63b66e5b 16531 transcoder_name(error->transcoder[i].cpu_transcoder));
ddf9c536 16532 err_printf(m, " Power: %s\n",
87ad3212 16533 onoff(error->transcoder[i].power_domain_on));
63b66e5b
CW
16534 err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
16535 err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
16536 err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
16537 err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
16538 err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
16539 err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
16540 err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
16541 }
c4a1d9e4 16542}