]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/gpu/drm/i915/intel_display.c
drm/i915: Pass around plane_state instead of fb+rotation
[mirror_ubuntu-bionic-kernel.git] / drivers / gpu / drm / i915 / intel_display.c
CommitLineData
79e53945
JB
1/*
2 * Copyright © 2006-2007 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 *
23 * Authors:
24 * Eric Anholt <eric@anholt.net>
25 */
26
618563e3 27#include <linux/dmi.h>
c1c7af60
JB
28#include <linux/module.h>
29#include <linux/input.h>
79e53945 30#include <linux/i2c.h>
7662c8bd 31#include <linux/kernel.h>
5a0e3ad6 32#include <linux/slab.h>
9cce37f4 33#include <linux/vgaarb.h>
e0dac65e 34#include <drm/drm_edid.h>
760285e7 35#include <drm/drmP.h>
79e53945 36#include "intel_drv.h"
5d723d7a 37#include "intel_frontbuffer.h"
760285e7 38#include <drm/i915_drm.h>
79e53945 39#include "i915_drv.h"
c37efb99 40#include "i915_gem_dmabuf.h"
db18b6a6 41#include "intel_dsi.h"
e5510fac 42#include "i915_trace.h"
319c1d42 43#include <drm/drm_atomic.h>
c196e1d6 44#include <drm/drm_atomic_helper.h>
760285e7
DH
45#include <drm/drm_dp_helper.h>
46#include <drm/drm_crtc_helper.h>
465c120c
MR
47#include <drm/drm_plane_helper.h>
48#include <drm/drm_rect.h>
c0f372b3 49#include <linux/dma_remapping.h>
fd8e058a 50#include <linux/reservation.h>
79e53945 51
5a21b665
DV
52static bool is_mmio_work(struct intel_flip_work *work)
53{
54 return work->mmio_work.func;
55}
56
465c120c 57/* Primary plane formats for gen <= 3 */
568db4f2 58static const uint32_t i8xx_primary_formats[] = {
67fe7dc5
DL
59 DRM_FORMAT_C8,
60 DRM_FORMAT_RGB565,
465c120c 61 DRM_FORMAT_XRGB1555,
67fe7dc5 62 DRM_FORMAT_XRGB8888,
465c120c
MR
63};
64
65/* Primary plane formats for gen >= 4 */
568db4f2 66static const uint32_t i965_primary_formats[] = {
6c0fd451
DL
67 DRM_FORMAT_C8,
68 DRM_FORMAT_RGB565,
69 DRM_FORMAT_XRGB8888,
70 DRM_FORMAT_XBGR8888,
71 DRM_FORMAT_XRGB2101010,
72 DRM_FORMAT_XBGR2101010,
73};
74
75static const uint32_t skl_primary_formats[] = {
67fe7dc5
DL
76 DRM_FORMAT_C8,
77 DRM_FORMAT_RGB565,
78 DRM_FORMAT_XRGB8888,
465c120c 79 DRM_FORMAT_XBGR8888,
67fe7dc5 80 DRM_FORMAT_ARGB8888,
465c120c
MR
81 DRM_FORMAT_ABGR8888,
82 DRM_FORMAT_XRGB2101010,
465c120c 83 DRM_FORMAT_XBGR2101010,
ea916ea0
KM
84 DRM_FORMAT_YUYV,
85 DRM_FORMAT_YVYU,
86 DRM_FORMAT_UYVY,
87 DRM_FORMAT_VYUY,
465c120c
MR
88};
89
3d7d6510
MR
90/* Cursor formats */
91static const uint32_t intel_cursor_formats[] = {
92 DRM_FORMAT_ARGB8888,
93};
94
f1f644dc 95static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 96 struct intel_crtc_state *pipe_config);
18442d08 97static void ironlake_pch_clock_get(struct intel_crtc *crtc,
5cec258b 98 struct intel_crtc_state *pipe_config);
f1f644dc 99
eb1bfe80
JB
100static int intel_framebuffer_init(struct drm_device *dev,
101 struct intel_framebuffer *ifb,
102 struct drm_mode_fb_cmd2 *mode_cmd,
103 struct drm_i915_gem_object *obj);
5b18e57c
DV
104static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc);
105static void intel_set_pipe_timings(struct intel_crtc *intel_crtc);
bc58be60 106static void intel_set_pipe_src_size(struct intel_crtc *intel_crtc);
29407aab 107static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
f769cd24
VK
108 struct intel_link_m_n *m_n,
109 struct intel_link_m_n *m2_n2);
29407aab 110static void ironlake_set_pipeconf(struct drm_crtc *crtc);
229fca97 111static void haswell_set_pipeconf(struct drm_crtc *crtc);
391bf048 112static void haswell_set_pipemisc(struct drm_crtc *crtc);
d288f65f 113static void vlv_prepare_pll(struct intel_crtc *crtc,
5cec258b 114 const struct intel_crtc_state *pipe_config);
d288f65f 115static void chv_prepare_pll(struct intel_crtc *crtc,
5cec258b 116 const struct intel_crtc_state *pipe_config);
5a21b665
DV
117static void intel_begin_crtc_commit(struct drm_crtc *, struct drm_crtc_state *);
118static void intel_finish_crtc_commit(struct drm_crtc *, struct drm_crtc_state *);
549e2bfb
CK
119static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
120 struct intel_crtc_state *crtc_state);
bfd16b2a
ML
121static void skylake_pfit_enable(struct intel_crtc *crtc);
122static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force);
123static void ironlake_pfit_enable(struct intel_crtc *crtc);
043e9bda 124static void intel_modeset_setup_hw_state(struct drm_device *dev);
2622a081 125static void intel_pre_disable_primary_noatomic(struct drm_crtc *crtc);
4e5ca60f 126static int ilk_max_pixel_rate(struct drm_atomic_state *state);
324513c0 127static int bxt_calc_cdclk(int max_pixclk);
e7457a9a 128
d4906093 129struct intel_limit {
4c5def93
ACO
130 struct {
131 int min, max;
132 } dot, vco, n, m, m1, m2, p, p1;
133
134 struct {
135 int dot_limit;
136 int p2_slow, p2_fast;
137 } p2;
d4906093 138};
79e53945 139
bfa7df01
VS
140/* returns HPLL frequency in kHz */
141static int valleyview_get_vco(struct drm_i915_private *dev_priv)
142{
143 int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
144
145 /* Obtain SKU information */
146 mutex_lock(&dev_priv->sb_lock);
147 hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
148 CCK_FUSE_HPLL_FREQ_MASK;
149 mutex_unlock(&dev_priv->sb_lock);
150
151 return vco_freq[hpll_freq] * 1000;
152}
153
c30fec65
VS
154int vlv_get_cck_clock(struct drm_i915_private *dev_priv,
155 const char *name, u32 reg, int ref_freq)
bfa7df01
VS
156{
157 u32 val;
158 int divider;
159
bfa7df01
VS
160 mutex_lock(&dev_priv->sb_lock);
161 val = vlv_cck_read(dev_priv, reg);
162 mutex_unlock(&dev_priv->sb_lock);
163
164 divider = val & CCK_FREQUENCY_VALUES;
165
166 WARN((val & CCK_FREQUENCY_STATUS) !=
167 (divider << CCK_FREQUENCY_STATUS_SHIFT),
168 "%s change in progress\n", name);
169
c30fec65
VS
170 return DIV_ROUND_CLOSEST(ref_freq << 1, divider + 1);
171}
172
173static int vlv_get_cck_clock_hpll(struct drm_i915_private *dev_priv,
174 const char *name, u32 reg)
175{
176 if (dev_priv->hpll_freq == 0)
177 dev_priv->hpll_freq = valleyview_get_vco(dev_priv);
178
179 return vlv_get_cck_clock(dev_priv, name, reg,
180 dev_priv->hpll_freq);
bfa7df01
VS
181}
182
e7dc33f3
VS
183static int
184intel_pch_rawclk(struct drm_i915_private *dev_priv)
d2acd215 185{
e7dc33f3
VS
186 return (I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK) * 1000;
187}
d2acd215 188
e7dc33f3
VS
189static int
190intel_vlv_hrawclk(struct drm_i915_private *dev_priv)
191{
19ab4ed3 192 /* RAWCLK_FREQ_VLV register updated from power well code */
35d38d1f
VS
193 return vlv_get_cck_clock_hpll(dev_priv, "hrawclk",
194 CCK_DISPLAY_REF_CLOCK_CONTROL);
d2acd215
DV
195}
196
e7dc33f3
VS
197static int
198intel_g4x_hrawclk(struct drm_i915_private *dev_priv)
79e50a4f 199{
79e50a4f
JN
200 uint32_t clkcfg;
201
e7dc33f3 202 /* hrawclock is 1/4 the FSB frequency */
79e50a4f
JN
203 clkcfg = I915_READ(CLKCFG);
204 switch (clkcfg & CLKCFG_FSB_MASK) {
205 case CLKCFG_FSB_400:
e7dc33f3 206 return 100000;
79e50a4f 207 case CLKCFG_FSB_533:
e7dc33f3 208 return 133333;
79e50a4f 209 case CLKCFG_FSB_667:
e7dc33f3 210 return 166667;
79e50a4f 211 case CLKCFG_FSB_800:
e7dc33f3 212 return 200000;
79e50a4f 213 case CLKCFG_FSB_1067:
e7dc33f3 214 return 266667;
79e50a4f 215 case CLKCFG_FSB_1333:
e7dc33f3 216 return 333333;
79e50a4f
JN
217 /* these two are just a guess; one of them might be right */
218 case CLKCFG_FSB_1600:
219 case CLKCFG_FSB_1600_ALT:
e7dc33f3 220 return 400000;
79e50a4f 221 default:
e7dc33f3 222 return 133333;
79e50a4f
JN
223 }
224}
225
19ab4ed3 226void intel_update_rawclk(struct drm_i915_private *dev_priv)
e7dc33f3
VS
227{
228 if (HAS_PCH_SPLIT(dev_priv))
229 dev_priv->rawclk_freq = intel_pch_rawclk(dev_priv);
230 else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
231 dev_priv->rawclk_freq = intel_vlv_hrawclk(dev_priv);
232 else if (IS_G4X(dev_priv) || IS_PINEVIEW(dev_priv))
233 dev_priv->rawclk_freq = intel_g4x_hrawclk(dev_priv);
234 else
235 return; /* no rawclk on other platforms, or no need to know it */
236
237 DRM_DEBUG_DRIVER("rawclk rate: %d kHz\n", dev_priv->rawclk_freq);
238}
239
bfa7df01
VS
240static void intel_update_czclk(struct drm_i915_private *dev_priv)
241{
666a4537 242 if (!(IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)))
bfa7df01
VS
243 return;
244
245 dev_priv->czclk_freq = vlv_get_cck_clock_hpll(dev_priv, "czclk",
246 CCK_CZ_CLOCK_CONTROL);
247
248 DRM_DEBUG_DRIVER("CZ clock rate: %d kHz\n", dev_priv->czclk_freq);
249}
250
021357ac 251static inline u32 /* units of 100MHz */
21a727b3
VS
252intel_fdi_link_freq(struct drm_i915_private *dev_priv,
253 const struct intel_crtc_state *pipe_config)
021357ac 254{
21a727b3
VS
255 if (HAS_DDI(dev_priv))
256 return pipe_config->port_clock; /* SPLL */
257 else if (IS_GEN5(dev_priv))
258 return ((I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2) * 10000;
e3b247da 259 else
21a727b3 260 return 270000;
021357ac
CW
261}
262
1b6f4958 263static const struct intel_limit intel_limits_i8xx_dac = {
0206e353 264 .dot = { .min = 25000, .max = 350000 },
9c333719 265 .vco = { .min = 908000, .max = 1512000 },
91dbe5fb 266 .n = { .min = 2, .max = 16 },
0206e353
AJ
267 .m = { .min = 96, .max = 140 },
268 .m1 = { .min = 18, .max = 26 },
269 .m2 = { .min = 6, .max = 16 },
270 .p = { .min = 4, .max = 128 },
271 .p1 = { .min = 2, .max = 33 },
273e27ca
EA
272 .p2 = { .dot_limit = 165000,
273 .p2_slow = 4, .p2_fast = 2 },
e4b36699
KP
274};
275
1b6f4958 276static const struct intel_limit intel_limits_i8xx_dvo = {
5d536e28 277 .dot = { .min = 25000, .max = 350000 },
9c333719 278 .vco = { .min = 908000, .max = 1512000 },
91dbe5fb 279 .n = { .min = 2, .max = 16 },
5d536e28
DV
280 .m = { .min = 96, .max = 140 },
281 .m1 = { .min = 18, .max = 26 },
282 .m2 = { .min = 6, .max = 16 },
283 .p = { .min = 4, .max = 128 },
284 .p1 = { .min = 2, .max = 33 },
285 .p2 = { .dot_limit = 165000,
286 .p2_slow = 4, .p2_fast = 4 },
287};
288
1b6f4958 289static const struct intel_limit intel_limits_i8xx_lvds = {
0206e353 290 .dot = { .min = 25000, .max = 350000 },
9c333719 291 .vco = { .min = 908000, .max = 1512000 },
91dbe5fb 292 .n = { .min = 2, .max = 16 },
0206e353
AJ
293 .m = { .min = 96, .max = 140 },
294 .m1 = { .min = 18, .max = 26 },
295 .m2 = { .min = 6, .max = 16 },
296 .p = { .min = 4, .max = 128 },
297 .p1 = { .min = 1, .max = 6 },
273e27ca
EA
298 .p2 = { .dot_limit = 165000,
299 .p2_slow = 14, .p2_fast = 7 },
e4b36699 300};
273e27ca 301
1b6f4958 302static const struct intel_limit intel_limits_i9xx_sdvo = {
0206e353
AJ
303 .dot = { .min = 20000, .max = 400000 },
304 .vco = { .min = 1400000, .max = 2800000 },
305 .n = { .min = 1, .max = 6 },
306 .m = { .min = 70, .max = 120 },
4f7dfb67
PJ
307 .m1 = { .min = 8, .max = 18 },
308 .m2 = { .min = 3, .max = 7 },
0206e353
AJ
309 .p = { .min = 5, .max = 80 },
310 .p1 = { .min = 1, .max = 8 },
273e27ca
EA
311 .p2 = { .dot_limit = 200000,
312 .p2_slow = 10, .p2_fast = 5 },
e4b36699
KP
313};
314
1b6f4958 315static const struct intel_limit intel_limits_i9xx_lvds = {
0206e353
AJ
316 .dot = { .min = 20000, .max = 400000 },
317 .vco = { .min = 1400000, .max = 2800000 },
318 .n = { .min = 1, .max = 6 },
319 .m = { .min = 70, .max = 120 },
53a7d2d1
PJ
320 .m1 = { .min = 8, .max = 18 },
321 .m2 = { .min = 3, .max = 7 },
0206e353
AJ
322 .p = { .min = 7, .max = 98 },
323 .p1 = { .min = 1, .max = 8 },
273e27ca
EA
324 .p2 = { .dot_limit = 112000,
325 .p2_slow = 14, .p2_fast = 7 },
e4b36699
KP
326};
327
273e27ca 328
1b6f4958 329static const struct intel_limit intel_limits_g4x_sdvo = {
273e27ca
EA
330 .dot = { .min = 25000, .max = 270000 },
331 .vco = { .min = 1750000, .max = 3500000},
332 .n = { .min = 1, .max = 4 },
333 .m = { .min = 104, .max = 138 },
334 .m1 = { .min = 17, .max = 23 },
335 .m2 = { .min = 5, .max = 11 },
336 .p = { .min = 10, .max = 30 },
337 .p1 = { .min = 1, .max = 3},
338 .p2 = { .dot_limit = 270000,
339 .p2_slow = 10,
340 .p2_fast = 10
044c7c41 341 },
e4b36699
KP
342};
343
1b6f4958 344static const struct intel_limit intel_limits_g4x_hdmi = {
273e27ca
EA
345 .dot = { .min = 22000, .max = 400000 },
346 .vco = { .min = 1750000, .max = 3500000},
347 .n = { .min = 1, .max = 4 },
348 .m = { .min = 104, .max = 138 },
349 .m1 = { .min = 16, .max = 23 },
350 .m2 = { .min = 5, .max = 11 },
351 .p = { .min = 5, .max = 80 },
352 .p1 = { .min = 1, .max = 8},
353 .p2 = { .dot_limit = 165000,
354 .p2_slow = 10, .p2_fast = 5 },
e4b36699
KP
355};
356
1b6f4958 357static const struct intel_limit intel_limits_g4x_single_channel_lvds = {
273e27ca
EA
358 .dot = { .min = 20000, .max = 115000 },
359 .vco = { .min = 1750000, .max = 3500000 },
360 .n = { .min = 1, .max = 3 },
361 .m = { .min = 104, .max = 138 },
362 .m1 = { .min = 17, .max = 23 },
363 .m2 = { .min = 5, .max = 11 },
364 .p = { .min = 28, .max = 112 },
365 .p1 = { .min = 2, .max = 8 },
366 .p2 = { .dot_limit = 0,
367 .p2_slow = 14, .p2_fast = 14
044c7c41 368 },
e4b36699
KP
369};
370
1b6f4958 371static const struct intel_limit intel_limits_g4x_dual_channel_lvds = {
273e27ca
EA
372 .dot = { .min = 80000, .max = 224000 },
373 .vco = { .min = 1750000, .max = 3500000 },
374 .n = { .min = 1, .max = 3 },
375 .m = { .min = 104, .max = 138 },
376 .m1 = { .min = 17, .max = 23 },
377 .m2 = { .min = 5, .max = 11 },
378 .p = { .min = 14, .max = 42 },
379 .p1 = { .min = 2, .max = 6 },
380 .p2 = { .dot_limit = 0,
381 .p2_slow = 7, .p2_fast = 7
044c7c41 382 },
e4b36699
KP
383};
384
1b6f4958 385static const struct intel_limit intel_limits_pineview_sdvo = {
0206e353
AJ
386 .dot = { .min = 20000, .max = 400000},
387 .vco = { .min = 1700000, .max = 3500000 },
273e27ca 388 /* Pineview's Ncounter is a ring counter */
0206e353
AJ
389 .n = { .min = 3, .max = 6 },
390 .m = { .min = 2, .max = 256 },
273e27ca 391 /* Pineview only has one combined m divider, which we treat as m2. */
0206e353
AJ
392 .m1 = { .min = 0, .max = 0 },
393 .m2 = { .min = 0, .max = 254 },
394 .p = { .min = 5, .max = 80 },
395 .p1 = { .min = 1, .max = 8 },
273e27ca
EA
396 .p2 = { .dot_limit = 200000,
397 .p2_slow = 10, .p2_fast = 5 },
e4b36699
KP
398};
399
1b6f4958 400static const struct intel_limit intel_limits_pineview_lvds = {
0206e353
AJ
401 .dot = { .min = 20000, .max = 400000 },
402 .vco = { .min = 1700000, .max = 3500000 },
403 .n = { .min = 3, .max = 6 },
404 .m = { .min = 2, .max = 256 },
405 .m1 = { .min = 0, .max = 0 },
406 .m2 = { .min = 0, .max = 254 },
407 .p = { .min = 7, .max = 112 },
408 .p1 = { .min = 1, .max = 8 },
273e27ca
EA
409 .p2 = { .dot_limit = 112000,
410 .p2_slow = 14, .p2_fast = 14 },
e4b36699
KP
411};
412
273e27ca
EA
413/* Ironlake / Sandybridge
414 *
415 * We calculate clock using (register_value + 2) for N/M1/M2, so here
416 * the range value for them is (actual_value - 2).
417 */
1b6f4958 418static const struct intel_limit intel_limits_ironlake_dac = {
273e27ca
EA
419 .dot = { .min = 25000, .max = 350000 },
420 .vco = { .min = 1760000, .max = 3510000 },
421 .n = { .min = 1, .max = 5 },
422 .m = { .min = 79, .max = 127 },
423 .m1 = { .min = 12, .max = 22 },
424 .m2 = { .min = 5, .max = 9 },
425 .p = { .min = 5, .max = 80 },
426 .p1 = { .min = 1, .max = 8 },
427 .p2 = { .dot_limit = 225000,
428 .p2_slow = 10, .p2_fast = 5 },
e4b36699
KP
429};
430
1b6f4958 431static const struct intel_limit intel_limits_ironlake_single_lvds = {
273e27ca
EA
432 .dot = { .min = 25000, .max = 350000 },
433 .vco = { .min = 1760000, .max = 3510000 },
434 .n = { .min = 1, .max = 3 },
435 .m = { .min = 79, .max = 118 },
436 .m1 = { .min = 12, .max = 22 },
437 .m2 = { .min = 5, .max = 9 },
438 .p = { .min = 28, .max = 112 },
439 .p1 = { .min = 2, .max = 8 },
440 .p2 = { .dot_limit = 225000,
441 .p2_slow = 14, .p2_fast = 14 },
b91ad0ec
ZW
442};
443
1b6f4958 444static const struct intel_limit intel_limits_ironlake_dual_lvds = {
273e27ca
EA
445 .dot = { .min = 25000, .max = 350000 },
446 .vco = { .min = 1760000, .max = 3510000 },
447 .n = { .min = 1, .max = 3 },
448 .m = { .min = 79, .max = 127 },
449 .m1 = { .min = 12, .max = 22 },
450 .m2 = { .min = 5, .max = 9 },
451 .p = { .min = 14, .max = 56 },
452 .p1 = { .min = 2, .max = 8 },
453 .p2 = { .dot_limit = 225000,
454 .p2_slow = 7, .p2_fast = 7 },
b91ad0ec
ZW
455};
456
273e27ca 457/* LVDS 100mhz refclk limits. */
1b6f4958 458static const struct intel_limit intel_limits_ironlake_single_lvds_100m = {
273e27ca
EA
459 .dot = { .min = 25000, .max = 350000 },
460 .vco = { .min = 1760000, .max = 3510000 },
461 .n = { .min = 1, .max = 2 },
462 .m = { .min = 79, .max = 126 },
463 .m1 = { .min = 12, .max = 22 },
464 .m2 = { .min = 5, .max = 9 },
465 .p = { .min = 28, .max = 112 },
0206e353 466 .p1 = { .min = 2, .max = 8 },
273e27ca
EA
467 .p2 = { .dot_limit = 225000,
468 .p2_slow = 14, .p2_fast = 14 },
b91ad0ec
ZW
469};
470
1b6f4958 471static const struct intel_limit intel_limits_ironlake_dual_lvds_100m = {
273e27ca
EA
472 .dot = { .min = 25000, .max = 350000 },
473 .vco = { .min = 1760000, .max = 3510000 },
474 .n = { .min = 1, .max = 3 },
475 .m = { .min = 79, .max = 126 },
476 .m1 = { .min = 12, .max = 22 },
477 .m2 = { .min = 5, .max = 9 },
478 .p = { .min = 14, .max = 42 },
0206e353 479 .p1 = { .min = 2, .max = 6 },
273e27ca
EA
480 .p2 = { .dot_limit = 225000,
481 .p2_slow = 7, .p2_fast = 7 },
4547668a
ZY
482};
483
1b6f4958 484static const struct intel_limit intel_limits_vlv = {
f01b7962
VS
485 /*
486 * These are the data rate limits (measured in fast clocks)
487 * since those are the strictest limits we have. The fast
488 * clock and actual rate limits are more relaxed, so checking
489 * them would make no difference.
490 */
491 .dot = { .min = 25000 * 5, .max = 270000 * 5 },
75e53986 492 .vco = { .min = 4000000, .max = 6000000 },
a0c4da24 493 .n = { .min = 1, .max = 7 },
a0c4da24
JB
494 .m1 = { .min = 2, .max = 3 },
495 .m2 = { .min = 11, .max = 156 },
b99ab663 496 .p1 = { .min = 2, .max = 3 },
5fdc9c49 497 .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
a0c4da24
JB
498};
499
1b6f4958 500static const struct intel_limit intel_limits_chv = {
ef9348c8
CML
501 /*
502 * These are the data rate limits (measured in fast clocks)
503 * since those are the strictest limits we have. The fast
504 * clock and actual rate limits are more relaxed, so checking
505 * them would make no difference.
506 */
507 .dot = { .min = 25000 * 5, .max = 540000 * 5},
17fe1021 508 .vco = { .min = 4800000, .max = 6480000 },
ef9348c8
CML
509 .n = { .min = 1, .max = 1 },
510 .m1 = { .min = 2, .max = 2 },
511 .m2 = { .min = 24 << 22, .max = 175 << 22 },
512 .p1 = { .min = 2, .max = 4 },
513 .p2 = { .p2_slow = 1, .p2_fast = 14 },
514};
515
1b6f4958 516static const struct intel_limit intel_limits_bxt = {
5ab7b0b7
ID
517 /* FIXME: find real dot limits */
518 .dot = { .min = 0, .max = INT_MAX },
e6292556 519 .vco = { .min = 4800000, .max = 6700000 },
5ab7b0b7
ID
520 .n = { .min = 1, .max = 1 },
521 .m1 = { .min = 2, .max = 2 },
522 /* FIXME: find real m2 limits */
523 .m2 = { .min = 2 << 22, .max = 255 << 22 },
524 .p1 = { .min = 2, .max = 4 },
525 .p2 = { .p2_slow = 1, .p2_fast = 20 },
526};
527
cdba954e
ACO
528static bool
529needs_modeset(struct drm_crtc_state *state)
530{
fc596660 531 return drm_atomic_crtc_needs_modeset(state);
cdba954e
ACO
532}
533
dccbea3b
ID
534/*
535 * Platform specific helpers to calculate the port PLL loopback- (clock.m),
536 * and post-divider (clock.p) values, pre- (clock.vco) and post-divided fast
537 * (clock.dot) clock rates. This fast dot clock is fed to the port's IO logic.
538 * The helpers' return value is the rate of the clock that is fed to the
539 * display engine's pipe which can be the above fast dot clock rate or a
540 * divided-down version of it.
541 */
f2b115e6 542/* m1 is reserved as 0 in Pineview, n is a ring counter */
9e2c8475 543static int pnv_calc_dpll_params(int refclk, struct dpll *clock)
79e53945 544{
2177832f
SL
545 clock->m = clock->m2 + 2;
546 clock->p = clock->p1 * clock->p2;
ed5ca77e 547 if (WARN_ON(clock->n == 0 || clock->p == 0))
dccbea3b 548 return 0;
fb03ac01
VS
549 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
550 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
dccbea3b
ID
551
552 return clock->dot;
2177832f
SL
553}
554
7429e9d4
DV
555static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
556{
557 return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
558}
559
9e2c8475 560static int i9xx_calc_dpll_params(int refclk, struct dpll *clock)
2177832f 561{
7429e9d4 562 clock->m = i9xx_dpll_compute_m(clock);
79e53945 563 clock->p = clock->p1 * clock->p2;
ed5ca77e 564 if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
dccbea3b 565 return 0;
fb03ac01
VS
566 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
567 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
dccbea3b
ID
568
569 return clock->dot;
79e53945
JB
570}
571
9e2c8475 572static int vlv_calc_dpll_params(int refclk, struct dpll *clock)
589eca67
ID
573{
574 clock->m = clock->m1 * clock->m2;
575 clock->p = clock->p1 * clock->p2;
576 if (WARN_ON(clock->n == 0 || clock->p == 0))
dccbea3b 577 return 0;
589eca67
ID
578 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
579 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
dccbea3b
ID
580
581 return clock->dot / 5;
589eca67
ID
582}
583
9e2c8475 584int chv_calc_dpll_params(int refclk, struct dpll *clock)
ef9348c8
CML
585{
586 clock->m = clock->m1 * clock->m2;
587 clock->p = clock->p1 * clock->p2;
588 if (WARN_ON(clock->n == 0 || clock->p == 0))
dccbea3b 589 return 0;
ef9348c8
CML
590 clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
591 clock->n << 22);
592 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
dccbea3b
ID
593
594 return clock->dot / 5;
ef9348c8
CML
595}
596
7c04d1d9 597#define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
79e53945
JB
598/**
599 * Returns whether the given set of divisors are valid for a given refclk with
600 * the given connectors.
601 */
602
1b894b59 603static bool intel_PLL_is_valid(struct drm_device *dev,
1b6f4958 604 const struct intel_limit *limit,
9e2c8475 605 const struct dpll *clock)
79e53945 606{
f01b7962
VS
607 if (clock->n < limit->n.min || limit->n.max < clock->n)
608 INTELPllInvalid("n out of range\n");
79e53945 609 if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
0206e353 610 INTELPllInvalid("p1 out of range\n");
79e53945 611 if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
0206e353 612 INTELPllInvalid("m2 out of range\n");
79e53945 613 if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
0206e353 614 INTELPllInvalid("m1 out of range\n");
f01b7962 615
666a4537
WB
616 if (!IS_PINEVIEW(dev) && !IS_VALLEYVIEW(dev) &&
617 !IS_CHERRYVIEW(dev) && !IS_BROXTON(dev))
f01b7962
VS
618 if (clock->m1 <= clock->m2)
619 INTELPllInvalid("m1 <= m2\n");
620
666a4537 621 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev) && !IS_BROXTON(dev)) {
f01b7962
VS
622 if (clock->p < limit->p.min || limit->p.max < clock->p)
623 INTELPllInvalid("p out of range\n");
624 if (clock->m < limit->m.min || limit->m.max < clock->m)
625 INTELPllInvalid("m out of range\n");
626 }
627
79e53945 628 if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
0206e353 629 INTELPllInvalid("vco out of range\n");
79e53945
JB
630 /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
631 * connector, etc., rather than just a single range.
632 */
633 if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
0206e353 634 INTELPllInvalid("dot out of range\n");
79e53945
JB
635
636 return true;
637}
638
3b1429d9 639static int
1b6f4958 640i9xx_select_p2_div(const struct intel_limit *limit,
3b1429d9
VS
641 const struct intel_crtc_state *crtc_state,
642 int target)
79e53945 643{
3b1429d9 644 struct drm_device *dev = crtc_state->base.crtc->dev;
79e53945 645
2d84d2b3 646 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
79e53945 647 /*
a210b028
DV
648 * For LVDS just rely on its current settings for dual-channel.
649 * We haven't figured out how to reliably set up different
650 * single/dual channel state, if we even can.
79e53945 651 */
1974cad0 652 if (intel_is_dual_link_lvds(dev))
3b1429d9 653 return limit->p2.p2_fast;
79e53945 654 else
3b1429d9 655 return limit->p2.p2_slow;
79e53945
JB
656 } else {
657 if (target < limit->p2.dot_limit)
3b1429d9 658 return limit->p2.p2_slow;
79e53945 659 else
3b1429d9 660 return limit->p2.p2_fast;
79e53945 661 }
3b1429d9
VS
662}
663
70e8aa21
ACO
664/*
665 * Returns a set of divisors for the desired target clock with the given
666 * refclk, or FALSE. The returned values represent the clock equation:
667 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
668 *
669 * Target and reference clocks are specified in kHz.
670 *
671 * If match_clock is provided, then best_clock P divider must match the P
672 * divider from @match_clock used for LVDS downclocking.
673 */
3b1429d9 674static bool
1b6f4958 675i9xx_find_best_dpll(const struct intel_limit *limit,
3b1429d9 676 struct intel_crtc_state *crtc_state,
9e2c8475
ACO
677 int target, int refclk, struct dpll *match_clock,
678 struct dpll *best_clock)
3b1429d9
VS
679{
680 struct drm_device *dev = crtc_state->base.crtc->dev;
9e2c8475 681 struct dpll clock;
3b1429d9 682 int err = target;
79e53945 683
0206e353 684 memset(best_clock, 0, sizeof(*best_clock));
79e53945 685
3b1429d9
VS
686 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
687
42158660
ZY
688 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
689 clock.m1++) {
690 for (clock.m2 = limit->m2.min;
691 clock.m2 <= limit->m2.max; clock.m2++) {
c0efc387 692 if (clock.m2 >= clock.m1)
42158660
ZY
693 break;
694 for (clock.n = limit->n.min;
695 clock.n <= limit->n.max; clock.n++) {
696 for (clock.p1 = limit->p1.min;
697 clock.p1 <= limit->p1.max; clock.p1++) {
79e53945
JB
698 int this_err;
699
dccbea3b 700 i9xx_calc_dpll_params(refclk, &clock);
ac58c3f0
DV
701 if (!intel_PLL_is_valid(dev, limit,
702 &clock))
703 continue;
704 if (match_clock &&
705 clock.p != match_clock->p)
706 continue;
707
708 this_err = abs(clock.dot - target);
709 if (this_err < err) {
710 *best_clock = clock;
711 err = this_err;
712 }
713 }
714 }
715 }
716 }
717
718 return (err != target);
719}
720
70e8aa21
ACO
721/*
722 * Returns a set of divisors for the desired target clock with the given
723 * refclk, or FALSE. The returned values represent the clock equation:
724 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
725 *
726 * Target and reference clocks are specified in kHz.
727 *
728 * If match_clock is provided, then best_clock P divider must match the P
729 * divider from @match_clock used for LVDS downclocking.
730 */
ac58c3f0 731static bool
1b6f4958 732pnv_find_best_dpll(const struct intel_limit *limit,
a93e255f 733 struct intel_crtc_state *crtc_state,
9e2c8475
ACO
734 int target, int refclk, struct dpll *match_clock,
735 struct dpll *best_clock)
79e53945 736{
3b1429d9 737 struct drm_device *dev = crtc_state->base.crtc->dev;
9e2c8475 738 struct dpll clock;
79e53945
JB
739 int err = target;
740
0206e353 741 memset(best_clock, 0, sizeof(*best_clock));
79e53945 742
3b1429d9
VS
743 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
744
42158660
ZY
745 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
746 clock.m1++) {
747 for (clock.m2 = limit->m2.min;
748 clock.m2 <= limit->m2.max; clock.m2++) {
42158660
ZY
749 for (clock.n = limit->n.min;
750 clock.n <= limit->n.max; clock.n++) {
751 for (clock.p1 = limit->p1.min;
752 clock.p1 <= limit->p1.max; clock.p1++) {
79e53945
JB
753 int this_err;
754
dccbea3b 755 pnv_calc_dpll_params(refclk, &clock);
1b894b59
CW
756 if (!intel_PLL_is_valid(dev, limit,
757 &clock))
79e53945 758 continue;
cec2f356
SP
759 if (match_clock &&
760 clock.p != match_clock->p)
761 continue;
79e53945
JB
762
763 this_err = abs(clock.dot - target);
764 if (this_err < err) {
765 *best_clock = clock;
766 err = this_err;
767 }
768 }
769 }
770 }
771 }
772
773 return (err != target);
774}
775
997c030c
ACO
776/*
777 * Returns a set of divisors for the desired target clock with the given
778 * refclk, or FALSE. The returned values represent the clock equation:
779 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
70e8aa21
ACO
780 *
781 * Target and reference clocks are specified in kHz.
782 *
783 * If match_clock is provided, then best_clock P divider must match the P
784 * divider from @match_clock used for LVDS downclocking.
997c030c 785 */
d4906093 786static bool
1b6f4958 787g4x_find_best_dpll(const struct intel_limit *limit,
a93e255f 788 struct intel_crtc_state *crtc_state,
9e2c8475
ACO
789 int target, int refclk, struct dpll *match_clock,
790 struct dpll *best_clock)
d4906093 791{
3b1429d9 792 struct drm_device *dev = crtc_state->base.crtc->dev;
9e2c8475 793 struct dpll clock;
d4906093 794 int max_n;
3b1429d9 795 bool found = false;
6ba770dc
AJ
796 /* approximately equals target * 0.00585 */
797 int err_most = (target >> 8) + (target >> 9);
d4906093
ML
798
799 memset(best_clock, 0, sizeof(*best_clock));
3b1429d9
VS
800
801 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
802
d4906093 803 max_n = limit->n.max;
f77f13e2 804 /* based on hardware requirement, prefer smaller n to precision */
d4906093 805 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
f77f13e2 806 /* based on hardware requirement, prefere larger m1,m2 */
d4906093
ML
807 for (clock.m1 = limit->m1.max;
808 clock.m1 >= limit->m1.min; clock.m1--) {
809 for (clock.m2 = limit->m2.max;
810 clock.m2 >= limit->m2.min; clock.m2--) {
811 for (clock.p1 = limit->p1.max;
812 clock.p1 >= limit->p1.min; clock.p1--) {
813 int this_err;
814
dccbea3b 815 i9xx_calc_dpll_params(refclk, &clock);
1b894b59
CW
816 if (!intel_PLL_is_valid(dev, limit,
817 &clock))
d4906093 818 continue;
1b894b59
CW
819
820 this_err = abs(clock.dot - target);
d4906093
ML
821 if (this_err < err_most) {
822 *best_clock = clock;
823 err_most = this_err;
824 max_n = clock.n;
825 found = true;
826 }
827 }
828 }
829 }
830 }
2c07245f
ZW
831 return found;
832}
833
d5dd62bd
ID
834/*
835 * Check if the calculated PLL configuration is more optimal compared to the
836 * best configuration and error found so far. Return the calculated error.
837 */
838static bool vlv_PLL_is_optimal(struct drm_device *dev, int target_freq,
9e2c8475
ACO
839 const struct dpll *calculated_clock,
840 const struct dpll *best_clock,
d5dd62bd
ID
841 unsigned int best_error_ppm,
842 unsigned int *error_ppm)
843{
9ca3ba01
ID
844 /*
845 * For CHV ignore the error and consider only the P value.
846 * Prefer a bigger P value based on HW requirements.
847 */
848 if (IS_CHERRYVIEW(dev)) {
849 *error_ppm = 0;
850
851 return calculated_clock->p > best_clock->p;
852 }
853
24be4e46
ID
854 if (WARN_ON_ONCE(!target_freq))
855 return false;
856
d5dd62bd
ID
857 *error_ppm = div_u64(1000000ULL *
858 abs(target_freq - calculated_clock->dot),
859 target_freq);
860 /*
861 * Prefer a better P value over a better (smaller) error if the error
862 * is small. Ensure this preference for future configurations too by
863 * setting the error to 0.
864 */
865 if (*error_ppm < 100 && calculated_clock->p > best_clock->p) {
866 *error_ppm = 0;
867
868 return true;
869 }
870
871 return *error_ppm + 10 < best_error_ppm;
872}
873
65b3d6a9
ACO
874/*
875 * Returns a set of divisors for the desired target clock with the given
876 * refclk, or FALSE. The returned values represent the clock equation:
877 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
878 */
a0c4da24 879static bool
1b6f4958 880vlv_find_best_dpll(const struct intel_limit *limit,
a93e255f 881 struct intel_crtc_state *crtc_state,
9e2c8475
ACO
882 int target, int refclk, struct dpll *match_clock,
883 struct dpll *best_clock)
a0c4da24 884{
a93e255f 885 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
a919ff14 886 struct drm_device *dev = crtc->base.dev;
9e2c8475 887 struct dpll clock;
69e4f900 888 unsigned int bestppm = 1000000;
27e639bf
VS
889 /* min update 19.2 MHz */
890 int max_n = min(limit->n.max, refclk / 19200);
49e497ef 891 bool found = false;
a0c4da24 892
6b4bf1c4
VS
893 target *= 5; /* fast clock */
894
895 memset(best_clock, 0, sizeof(*best_clock));
a0c4da24
JB
896
897 /* based on hardware requirement, prefer smaller n to precision */
27e639bf 898 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
811bbf05 899 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
889059d8 900 for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
c1a9ae43 901 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
6b4bf1c4 902 clock.p = clock.p1 * clock.p2;
a0c4da24 903 /* based on hardware requirement, prefer bigger m1,m2 values */
6b4bf1c4 904 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
d5dd62bd 905 unsigned int ppm;
69e4f900 906
6b4bf1c4
VS
907 clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
908 refclk * clock.m1);
909
dccbea3b 910 vlv_calc_dpll_params(refclk, &clock);
43b0ac53 911
f01b7962
VS
912 if (!intel_PLL_is_valid(dev, limit,
913 &clock))
43b0ac53
VS
914 continue;
915
d5dd62bd
ID
916 if (!vlv_PLL_is_optimal(dev, target,
917 &clock,
918 best_clock,
919 bestppm, &ppm))
920 continue;
6b4bf1c4 921
d5dd62bd
ID
922 *best_clock = clock;
923 bestppm = ppm;
924 found = true;
a0c4da24
JB
925 }
926 }
927 }
928 }
a0c4da24 929
49e497ef 930 return found;
a0c4da24 931}
a4fc5ed6 932
65b3d6a9
ACO
933/*
934 * Returns a set of divisors for the desired target clock with the given
935 * refclk, or FALSE. The returned values represent the clock equation:
936 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
937 */
ef9348c8 938static bool
1b6f4958 939chv_find_best_dpll(const struct intel_limit *limit,
a93e255f 940 struct intel_crtc_state *crtc_state,
9e2c8475
ACO
941 int target, int refclk, struct dpll *match_clock,
942 struct dpll *best_clock)
ef9348c8 943{
a93e255f 944 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
a919ff14 945 struct drm_device *dev = crtc->base.dev;
9ca3ba01 946 unsigned int best_error_ppm;
9e2c8475 947 struct dpll clock;
ef9348c8
CML
948 uint64_t m2;
949 int found = false;
950
951 memset(best_clock, 0, sizeof(*best_clock));
9ca3ba01 952 best_error_ppm = 1000000;
ef9348c8
CML
953
954 /*
955 * Based on hardware doc, the n always set to 1, and m1 always
956 * set to 2. If requires to support 200Mhz refclk, we need to
957 * revisit this because n may not 1 anymore.
958 */
959 clock.n = 1, clock.m1 = 2;
960 target *= 5; /* fast clock */
961
962 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
963 for (clock.p2 = limit->p2.p2_fast;
964 clock.p2 >= limit->p2.p2_slow;
965 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
9ca3ba01 966 unsigned int error_ppm;
ef9348c8
CML
967
968 clock.p = clock.p1 * clock.p2;
969
970 m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
971 clock.n) << 22, refclk * clock.m1);
972
973 if (m2 > INT_MAX/clock.m1)
974 continue;
975
976 clock.m2 = m2;
977
dccbea3b 978 chv_calc_dpll_params(refclk, &clock);
ef9348c8
CML
979
980 if (!intel_PLL_is_valid(dev, limit, &clock))
981 continue;
982
9ca3ba01
ID
983 if (!vlv_PLL_is_optimal(dev, target, &clock, best_clock,
984 best_error_ppm, &error_ppm))
985 continue;
986
987 *best_clock = clock;
988 best_error_ppm = error_ppm;
989 found = true;
ef9348c8
CML
990 }
991 }
992
993 return found;
994}
995
5ab7b0b7 996bool bxt_find_best_dpll(struct intel_crtc_state *crtc_state, int target_clock,
9e2c8475 997 struct dpll *best_clock)
5ab7b0b7 998{
65b3d6a9 999 int refclk = 100000;
1b6f4958 1000 const struct intel_limit *limit = &intel_limits_bxt;
5ab7b0b7 1001
65b3d6a9 1002 return chv_find_best_dpll(limit, crtc_state,
5ab7b0b7
ID
1003 target_clock, refclk, NULL, best_clock);
1004}
1005
20ddf665
VS
1006bool intel_crtc_active(struct drm_crtc *crtc)
1007{
1008 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1009
1010 /* Be paranoid as we can arrive here with only partial
1011 * state retrieved from the hardware during setup.
1012 *
241bfc38 1013 * We can ditch the adjusted_mode.crtc_clock check as soon
20ddf665
VS
1014 * as Haswell has gained clock readout/fastboot support.
1015 *
66e514c1 1016 * We can ditch the crtc->primary->fb check as soon as we can
20ddf665 1017 * properly reconstruct framebuffers.
c3d1f436
MR
1018 *
1019 * FIXME: The intel_crtc->active here should be switched to
1020 * crtc->state->active once we have proper CRTC states wired up
1021 * for atomic.
20ddf665 1022 */
c3d1f436 1023 return intel_crtc->active && crtc->primary->state->fb &&
6e3c9717 1024 intel_crtc->config->base.adjusted_mode.crtc_clock;
20ddf665
VS
1025}
1026
a5c961d1
PZ
1027enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
1028 enum pipe pipe)
1029{
1030 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
1031 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1032
6e3c9717 1033 return intel_crtc->config->cpu_transcoder;
a5c961d1
PZ
1034}
1035
fbf49ea2
VS
1036static bool pipe_dsl_stopped(struct drm_device *dev, enum pipe pipe)
1037{
fac5e23e 1038 struct drm_i915_private *dev_priv = to_i915(dev);
f0f59a00 1039 i915_reg_t reg = PIPEDSL(pipe);
fbf49ea2
VS
1040 u32 line1, line2;
1041 u32 line_mask;
1042
1043 if (IS_GEN2(dev))
1044 line_mask = DSL_LINEMASK_GEN2;
1045 else
1046 line_mask = DSL_LINEMASK_GEN3;
1047
1048 line1 = I915_READ(reg) & line_mask;
6adfb1ef 1049 msleep(5);
fbf49ea2
VS
1050 line2 = I915_READ(reg) & line_mask;
1051
1052 return line1 == line2;
1053}
1054
ab7ad7f6
KP
1055/*
1056 * intel_wait_for_pipe_off - wait for pipe to turn off
575f7ab7 1057 * @crtc: crtc whose pipe to wait for
9d0498a2
JB
1058 *
1059 * After disabling a pipe, we can't wait for vblank in the usual way,
1060 * spinning on the vblank interrupt status bit, since we won't actually
1061 * see an interrupt when the pipe is disabled.
1062 *
ab7ad7f6
KP
1063 * On Gen4 and above:
1064 * wait for the pipe register state bit to turn off
1065 *
1066 * Otherwise:
1067 * wait for the display line value to settle (it usually
1068 * ends up stopping at the start of the next frame).
58e10eb9 1069 *
9d0498a2 1070 */
575f7ab7 1071static void intel_wait_for_pipe_off(struct intel_crtc *crtc)
9d0498a2 1072{
575f7ab7 1073 struct drm_device *dev = crtc->base.dev;
fac5e23e 1074 struct drm_i915_private *dev_priv = to_i915(dev);
6e3c9717 1075 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
575f7ab7 1076 enum pipe pipe = crtc->pipe;
ab7ad7f6
KP
1077
1078 if (INTEL_INFO(dev)->gen >= 4) {
f0f59a00 1079 i915_reg_t reg = PIPECONF(cpu_transcoder);
ab7ad7f6
KP
1080
1081 /* Wait for the Pipe State to go off */
b8511f53
CW
1082 if (intel_wait_for_register(dev_priv,
1083 reg, I965_PIPECONF_ACTIVE, 0,
1084 100))
284637d9 1085 WARN(1, "pipe_off wait timed out\n");
ab7ad7f6 1086 } else {
ab7ad7f6 1087 /* Wait for the display line to settle */
fbf49ea2 1088 if (wait_for(pipe_dsl_stopped(dev, pipe), 100))
284637d9 1089 WARN(1, "pipe_off wait timed out\n");
ab7ad7f6 1090 }
79e53945
JB
1091}
1092
b24e7179 1093/* Only for pre-ILK configs */
55607e8a
DV
1094void assert_pll(struct drm_i915_private *dev_priv,
1095 enum pipe pipe, bool state)
b24e7179 1096{
b24e7179
JB
1097 u32 val;
1098 bool cur_state;
1099
649636ef 1100 val = I915_READ(DPLL(pipe));
b24e7179 1101 cur_state = !!(val & DPLL_VCO_ENABLE);
e2c719b7 1102 I915_STATE_WARN(cur_state != state,
b24e7179 1103 "PLL state assertion failure (expected %s, current %s)\n",
87ad3212 1104 onoff(state), onoff(cur_state));
b24e7179 1105}
b24e7179 1106
23538ef1 1107/* XXX: the dsi pll is shared between MIPI DSI ports */
8563b1e8 1108void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
23538ef1
JN
1109{
1110 u32 val;
1111 bool cur_state;
1112
a580516d 1113 mutex_lock(&dev_priv->sb_lock);
23538ef1 1114 val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
a580516d 1115 mutex_unlock(&dev_priv->sb_lock);
23538ef1
JN
1116
1117 cur_state = val & DSI_PLL_VCO_EN;
e2c719b7 1118 I915_STATE_WARN(cur_state != state,
23538ef1 1119 "DSI PLL state assertion failure (expected %s, current %s)\n",
87ad3212 1120 onoff(state), onoff(cur_state));
23538ef1 1121}
23538ef1 1122
040484af
JB
1123static void assert_fdi_tx(struct drm_i915_private *dev_priv,
1124 enum pipe pipe, bool state)
1125{
040484af 1126 bool cur_state;
ad80a810
PZ
1127 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1128 pipe);
040484af 1129
2d1fe073 1130 if (HAS_DDI(dev_priv)) {
affa9354 1131 /* DDI does not have a specific FDI_TX register */
649636ef 1132 u32 val = I915_READ(TRANS_DDI_FUNC_CTL(cpu_transcoder));
ad80a810 1133 cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
bf507ef7 1134 } else {
649636ef 1135 u32 val = I915_READ(FDI_TX_CTL(pipe));
bf507ef7
ED
1136 cur_state = !!(val & FDI_TX_ENABLE);
1137 }
e2c719b7 1138 I915_STATE_WARN(cur_state != state,
040484af 1139 "FDI TX state assertion failure (expected %s, current %s)\n",
87ad3212 1140 onoff(state), onoff(cur_state));
040484af
JB
1141}
1142#define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
1143#define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
1144
1145static void assert_fdi_rx(struct drm_i915_private *dev_priv,
1146 enum pipe pipe, bool state)
1147{
040484af
JB
1148 u32 val;
1149 bool cur_state;
1150
649636ef 1151 val = I915_READ(FDI_RX_CTL(pipe));
d63fa0dc 1152 cur_state = !!(val & FDI_RX_ENABLE);
e2c719b7 1153 I915_STATE_WARN(cur_state != state,
040484af 1154 "FDI RX state assertion failure (expected %s, current %s)\n",
87ad3212 1155 onoff(state), onoff(cur_state));
040484af
JB
1156}
1157#define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
1158#define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
1159
1160static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
1161 enum pipe pipe)
1162{
040484af
JB
1163 u32 val;
1164
1165 /* ILK FDI PLL is always enabled */
7e22dbbb 1166 if (IS_GEN5(dev_priv))
040484af
JB
1167 return;
1168
bf507ef7 1169 /* On Haswell, DDI ports are responsible for the FDI PLL setup */
2d1fe073 1170 if (HAS_DDI(dev_priv))
bf507ef7
ED
1171 return;
1172
649636ef 1173 val = I915_READ(FDI_TX_CTL(pipe));
e2c719b7 1174 I915_STATE_WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
040484af
JB
1175}
1176
55607e8a
DV
1177void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
1178 enum pipe pipe, bool state)
040484af 1179{
040484af 1180 u32 val;
55607e8a 1181 bool cur_state;
040484af 1182
649636ef 1183 val = I915_READ(FDI_RX_CTL(pipe));
55607e8a 1184 cur_state = !!(val & FDI_RX_PLL_ENABLE);
e2c719b7 1185 I915_STATE_WARN(cur_state != state,
55607e8a 1186 "FDI RX PLL assertion failure (expected %s, current %s)\n",
87ad3212 1187 onoff(state), onoff(cur_state));
040484af
JB
1188}
1189
b680c37a
DV
1190void assert_panel_unlocked(struct drm_i915_private *dev_priv,
1191 enum pipe pipe)
ea0760cf 1192{
91c8a326 1193 struct drm_device *dev = &dev_priv->drm;
f0f59a00 1194 i915_reg_t pp_reg;
ea0760cf
JB
1195 u32 val;
1196 enum pipe panel_pipe = PIPE_A;
0de3b485 1197 bool locked = true;
ea0760cf 1198
bedd4dba
JN
1199 if (WARN_ON(HAS_DDI(dev)))
1200 return;
1201
1202 if (HAS_PCH_SPLIT(dev)) {
1203 u32 port_sel;
1204
44cb734c
ID
1205 pp_reg = PP_CONTROL(0);
1206 port_sel = I915_READ(PP_ON_DELAYS(0)) & PANEL_PORT_SELECT_MASK;
bedd4dba
JN
1207
1208 if (port_sel == PANEL_PORT_SELECT_LVDS &&
1209 I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
1210 panel_pipe = PIPE_B;
1211 /* XXX: else fix for eDP */
666a4537 1212 } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
bedd4dba 1213 /* presumably write lock depends on pipe, not port select */
44cb734c 1214 pp_reg = PP_CONTROL(pipe);
bedd4dba 1215 panel_pipe = pipe;
ea0760cf 1216 } else {
44cb734c 1217 pp_reg = PP_CONTROL(0);
bedd4dba
JN
1218 if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
1219 panel_pipe = PIPE_B;
ea0760cf
JB
1220 }
1221
1222 val = I915_READ(pp_reg);
1223 if (!(val & PANEL_POWER_ON) ||
ec49ba2d 1224 ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
ea0760cf
JB
1225 locked = false;
1226
e2c719b7 1227 I915_STATE_WARN(panel_pipe == pipe && locked,
ea0760cf 1228 "panel assertion failure, pipe %c regs locked\n",
9db4a9c7 1229 pipe_name(pipe));
ea0760cf
JB
1230}
1231
93ce0ba6
JN
1232static void assert_cursor(struct drm_i915_private *dev_priv,
1233 enum pipe pipe, bool state)
1234{
91c8a326 1235 struct drm_device *dev = &dev_priv->drm;
93ce0ba6
JN
1236 bool cur_state;
1237
d9d82081 1238 if (IS_845G(dev) || IS_I865G(dev))
0b87c24e 1239 cur_state = I915_READ(CURCNTR(PIPE_A)) & CURSOR_ENABLE;
d9d82081 1240 else
5efb3e28 1241 cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
93ce0ba6 1242
e2c719b7 1243 I915_STATE_WARN(cur_state != state,
93ce0ba6 1244 "cursor on pipe %c assertion failure (expected %s, current %s)\n",
87ad3212 1245 pipe_name(pipe), onoff(state), onoff(cur_state));
93ce0ba6
JN
1246}
1247#define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
1248#define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
1249
b840d907
JB
1250void assert_pipe(struct drm_i915_private *dev_priv,
1251 enum pipe pipe, bool state)
b24e7179 1252{
63d7bbe9 1253 bool cur_state;
702e7a56
PZ
1254 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1255 pipe);
4feed0eb 1256 enum intel_display_power_domain power_domain;
b24e7179 1257
b6b5d049
VS
1258 /* if we need the pipe quirk it must be always on */
1259 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1260 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
8e636784
DV
1261 state = true;
1262
4feed0eb
ID
1263 power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
1264 if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
649636ef 1265 u32 val = I915_READ(PIPECONF(cpu_transcoder));
69310161 1266 cur_state = !!(val & PIPECONF_ENABLE);
4feed0eb
ID
1267
1268 intel_display_power_put(dev_priv, power_domain);
1269 } else {
1270 cur_state = false;
69310161
PZ
1271 }
1272
e2c719b7 1273 I915_STATE_WARN(cur_state != state,
63d7bbe9 1274 "pipe %c assertion failure (expected %s, current %s)\n",
87ad3212 1275 pipe_name(pipe), onoff(state), onoff(cur_state));
b24e7179
JB
1276}
1277
931872fc
CW
1278static void assert_plane(struct drm_i915_private *dev_priv,
1279 enum plane plane, bool state)
b24e7179 1280{
b24e7179 1281 u32 val;
931872fc 1282 bool cur_state;
b24e7179 1283
649636ef 1284 val = I915_READ(DSPCNTR(plane));
931872fc 1285 cur_state = !!(val & DISPLAY_PLANE_ENABLE);
e2c719b7 1286 I915_STATE_WARN(cur_state != state,
931872fc 1287 "plane %c assertion failure (expected %s, current %s)\n",
87ad3212 1288 plane_name(plane), onoff(state), onoff(cur_state));
b24e7179
JB
1289}
1290
931872fc
CW
1291#define assert_plane_enabled(d, p) assert_plane(d, p, true)
1292#define assert_plane_disabled(d, p) assert_plane(d, p, false)
1293
b24e7179
JB
1294static void assert_planes_disabled(struct drm_i915_private *dev_priv,
1295 enum pipe pipe)
1296{
91c8a326 1297 struct drm_device *dev = &dev_priv->drm;
649636ef 1298 int i;
b24e7179 1299
653e1026
VS
1300 /* Primary planes are fixed to pipes on gen4+ */
1301 if (INTEL_INFO(dev)->gen >= 4) {
649636ef 1302 u32 val = I915_READ(DSPCNTR(pipe));
e2c719b7 1303 I915_STATE_WARN(val & DISPLAY_PLANE_ENABLE,
28c05794
AJ
1304 "plane %c assertion failure, should be disabled but not\n",
1305 plane_name(pipe));
19ec1358 1306 return;
28c05794 1307 }
19ec1358 1308
b24e7179 1309 /* Need to check both planes against the pipe */
055e393f 1310 for_each_pipe(dev_priv, i) {
649636ef
VS
1311 u32 val = I915_READ(DSPCNTR(i));
1312 enum pipe cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
b24e7179 1313 DISPPLANE_SEL_PIPE_SHIFT;
e2c719b7 1314 I915_STATE_WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
9db4a9c7
JB
1315 "plane %c assertion failure, should be off on pipe %c but is still active\n",
1316 plane_name(i), pipe_name(pipe));
b24e7179
JB
1317 }
1318}
1319
19332d7a
JB
1320static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
1321 enum pipe pipe)
1322{
91c8a326 1323 struct drm_device *dev = &dev_priv->drm;
649636ef 1324 int sprite;
19332d7a 1325
7feb8b88 1326 if (INTEL_INFO(dev)->gen >= 9) {
3bdcfc0c 1327 for_each_sprite(dev_priv, pipe, sprite) {
649636ef 1328 u32 val = I915_READ(PLANE_CTL(pipe, sprite));
e2c719b7 1329 I915_STATE_WARN(val & PLANE_CTL_ENABLE,
7feb8b88
DL
1330 "plane %d assertion failure, should be off on pipe %c but is still active\n",
1331 sprite, pipe_name(pipe));
1332 }
666a4537 1333 } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
3bdcfc0c 1334 for_each_sprite(dev_priv, pipe, sprite) {
649636ef 1335 u32 val = I915_READ(SPCNTR(pipe, sprite));
e2c719b7 1336 I915_STATE_WARN(val & SP_ENABLE,
20674eef 1337 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
1fe47785 1338 sprite_name(pipe, sprite), pipe_name(pipe));
20674eef
VS
1339 }
1340 } else if (INTEL_INFO(dev)->gen >= 7) {
649636ef 1341 u32 val = I915_READ(SPRCTL(pipe));
e2c719b7 1342 I915_STATE_WARN(val & SPRITE_ENABLE,
06da8da2 1343 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
20674eef
VS
1344 plane_name(pipe), pipe_name(pipe));
1345 } else if (INTEL_INFO(dev)->gen >= 5) {
649636ef 1346 u32 val = I915_READ(DVSCNTR(pipe));
e2c719b7 1347 I915_STATE_WARN(val & DVS_ENABLE,
06da8da2 1348 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
20674eef 1349 plane_name(pipe), pipe_name(pipe));
19332d7a
JB
1350 }
1351}
1352
08c71e5e
VS
1353static void assert_vblank_disabled(struct drm_crtc *crtc)
1354{
e2c719b7 1355 if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
08c71e5e
VS
1356 drm_crtc_vblank_put(crtc);
1357}
1358
7abd4b35
ACO
1359void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
1360 enum pipe pipe)
92f2584a 1361{
92f2584a
JB
1362 u32 val;
1363 bool enabled;
1364
649636ef 1365 val = I915_READ(PCH_TRANSCONF(pipe));
92f2584a 1366 enabled = !!(val & TRANS_ENABLE);
e2c719b7 1367 I915_STATE_WARN(enabled,
9db4a9c7
JB
1368 "transcoder assertion failed, should be off on pipe %c but is still active\n",
1369 pipe_name(pipe));
92f2584a
JB
1370}
1371
4e634389
KP
1372static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
1373 enum pipe pipe, u32 port_sel, u32 val)
f0575e92
KP
1374{
1375 if ((val & DP_PORT_EN) == 0)
1376 return false;
1377
2d1fe073 1378 if (HAS_PCH_CPT(dev_priv)) {
f0f59a00 1379 u32 trans_dp_ctl = I915_READ(TRANS_DP_CTL(pipe));
f0575e92
KP
1380 if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
1381 return false;
2d1fe073 1382 } else if (IS_CHERRYVIEW(dev_priv)) {
44f37d1f
CML
1383 if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
1384 return false;
f0575e92
KP
1385 } else {
1386 if ((val & DP_PIPE_MASK) != (pipe << 30))
1387 return false;
1388 }
1389 return true;
1390}
1391
1519b995
KP
1392static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
1393 enum pipe pipe, u32 val)
1394{
dc0fa718 1395 if ((val & SDVO_ENABLE) == 0)
1519b995
KP
1396 return false;
1397
2d1fe073 1398 if (HAS_PCH_CPT(dev_priv)) {
dc0fa718 1399 if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
1519b995 1400 return false;
2d1fe073 1401 } else if (IS_CHERRYVIEW(dev_priv)) {
44f37d1f
CML
1402 if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
1403 return false;
1519b995 1404 } else {
dc0fa718 1405 if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
1519b995
KP
1406 return false;
1407 }
1408 return true;
1409}
1410
1411static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
1412 enum pipe pipe, u32 val)
1413{
1414 if ((val & LVDS_PORT_EN) == 0)
1415 return false;
1416
2d1fe073 1417 if (HAS_PCH_CPT(dev_priv)) {
1519b995
KP
1418 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1419 return false;
1420 } else {
1421 if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
1422 return false;
1423 }
1424 return true;
1425}
1426
1427static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
1428 enum pipe pipe, u32 val)
1429{
1430 if ((val & ADPA_DAC_ENABLE) == 0)
1431 return false;
2d1fe073 1432 if (HAS_PCH_CPT(dev_priv)) {
1519b995
KP
1433 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1434 return false;
1435 } else {
1436 if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
1437 return false;
1438 }
1439 return true;
1440}
1441
291906f1 1442static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
f0f59a00
VS
1443 enum pipe pipe, i915_reg_t reg,
1444 u32 port_sel)
291906f1 1445{
47a05eca 1446 u32 val = I915_READ(reg);
e2c719b7 1447 I915_STATE_WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
291906f1 1448 "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
f0f59a00 1449 i915_mmio_reg_offset(reg), pipe_name(pipe));
de9a35ab 1450
2d1fe073 1451 I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && (val & DP_PORT_EN) == 0
75c5da27 1452 && (val & DP_PIPEB_SELECT),
de9a35ab 1453 "IBX PCH dp port still using transcoder B\n");
291906f1
JB
1454}
1455
1456static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
f0f59a00 1457 enum pipe pipe, i915_reg_t reg)
291906f1 1458{
47a05eca 1459 u32 val = I915_READ(reg);
e2c719b7 1460 I915_STATE_WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
23c99e77 1461 "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
f0f59a00 1462 i915_mmio_reg_offset(reg), pipe_name(pipe));
de9a35ab 1463
2d1fe073 1464 I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && (val & SDVO_ENABLE) == 0
75c5da27 1465 && (val & SDVO_PIPE_B_SELECT),
de9a35ab 1466 "IBX PCH hdmi port still using transcoder B\n");
291906f1
JB
1467}
1468
1469static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
1470 enum pipe pipe)
1471{
291906f1 1472 u32 val;
291906f1 1473
f0575e92
KP
1474 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
1475 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
1476 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
291906f1 1477
649636ef 1478 val = I915_READ(PCH_ADPA);
e2c719b7 1479 I915_STATE_WARN(adpa_pipe_enabled(dev_priv, pipe, val),
291906f1 1480 "PCH VGA enabled on transcoder %c, should be disabled\n",
9db4a9c7 1481 pipe_name(pipe));
291906f1 1482
649636ef 1483 val = I915_READ(PCH_LVDS);
e2c719b7 1484 I915_STATE_WARN(lvds_pipe_enabled(dev_priv, pipe, val),
291906f1 1485 "PCH LVDS enabled on transcoder %c, should be disabled\n",
9db4a9c7 1486 pipe_name(pipe));
291906f1 1487
e2debe91
PZ
1488 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
1489 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
1490 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
291906f1
JB
1491}
1492
cd2d34d9
VS
1493static void _vlv_enable_pll(struct intel_crtc *crtc,
1494 const struct intel_crtc_state *pipe_config)
1495{
1496 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1497 enum pipe pipe = crtc->pipe;
1498
1499 I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
1500 POSTING_READ(DPLL(pipe));
1501 udelay(150);
1502
2c30b43b
CW
1503 if (intel_wait_for_register(dev_priv,
1504 DPLL(pipe),
1505 DPLL_LOCK_VLV,
1506 DPLL_LOCK_VLV,
1507 1))
cd2d34d9
VS
1508 DRM_ERROR("DPLL %d failed to lock\n", pipe);
1509}
1510
d288f65f 1511static void vlv_enable_pll(struct intel_crtc *crtc,
5cec258b 1512 const struct intel_crtc_state *pipe_config)
87442f73 1513{
cd2d34d9 1514 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
8bd3f301 1515 enum pipe pipe = crtc->pipe;
87442f73 1516
8bd3f301 1517 assert_pipe_disabled(dev_priv, pipe);
87442f73 1518
87442f73 1519 /* PLL is protected by panel, make sure we can write it */
7d1a83cb 1520 assert_panel_unlocked(dev_priv, pipe);
87442f73 1521
cd2d34d9
VS
1522 if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
1523 _vlv_enable_pll(crtc, pipe_config);
426115cf 1524
8bd3f301
VS
1525 I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
1526 POSTING_READ(DPLL_MD(pipe));
87442f73
DV
1527}
1528
cd2d34d9
VS
1529
1530static void _chv_enable_pll(struct intel_crtc *crtc,
1531 const struct intel_crtc_state *pipe_config)
9d556c99 1532{
cd2d34d9 1533 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
8bd3f301 1534 enum pipe pipe = crtc->pipe;
9d556c99 1535 enum dpio_channel port = vlv_pipe_to_channel(pipe);
9d556c99
CML
1536 u32 tmp;
1537
a580516d 1538 mutex_lock(&dev_priv->sb_lock);
9d556c99
CML
1539
1540 /* Enable back the 10bit clock to display controller */
1541 tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1542 tmp |= DPIO_DCLKP_EN;
1543 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
1544
54433e91
VS
1545 mutex_unlock(&dev_priv->sb_lock);
1546
9d556c99
CML
1547 /*
1548 * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
1549 */
1550 udelay(1);
1551
1552 /* Enable PLL */
d288f65f 1553 I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
9d556c99
CML
1554
1555 /* Check PLL is locked */
6b18826a
CW
1556 if (intel_wait_for_register(dev_priv,
1557 DPLL(pipe), DPLL_LOCK_VLV, DPLL_LOCK_VLV,
1558 1))
9d556c99 1559 DRM_ERROR("PLL %d failed to lock\n", pipe);
cd2d34d9
VS
1560}
1561
1562static void chv_enable_pll(struct intel_crtc *crtc,
1563 const struct intel_crtc_state *pipe_config)
1564{
1565 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1566 enum pipe pipe = crtc->pipe;
1567
1568 assert_pipe_disabled(dev_priv, pipe);
1569
1570 /* PLL is protected by panel, make sure we can write it */
1571 assert_panel_unlocked(dev_priv, pipe);
1572
1573 if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
1574 _chv_enable_pll(crtc, pipe_config);
9d556c99 1575
c231775c
VS
1576 if (pipe != PIPE_A) {
1577 /*
1578 * WaPixelRepeatModeFixForC0:chv
1579 *
1580 * DPLLCMD is AWOL. Use chicken bits to propagate
1581 * the value from DPLLBMD to either pipe B or C.
1582 */
1583 I915_WRITE(CBR4_VLV, pipe == PIPE_B ? CBR_DPLLBMD_PIPE_B : CBR_DPLLBMD_PIPE_C);
1584 I915_WRITE(DPLL_MD(PIPE_B), pipe_config->dpll_hw_state.dpll_md);
1585 I915_WRITE(CBR4_VLV, 0);
1586 dev_priv->chv_dpll_md[pipe] = pipe_config->dpll_hw_state.dpll_md;
1587
1588 /*
1589 * DPLLB VGA mode also seems to cause problems.
1590 * We should always have it disabled.
1591 */
1592 WARN_ON((I915_READ(DPLL(PIPE_B)) & DPLL_VGA_MODE_DIS) == 0);
1593 } else {
1594 I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
1595 POSTING_READ(DPLL_MD(pipe));
1596 }
9d556c99
CML
1597}
1598
1c4e0274
VS
1599static int intel_num_dvo_pipes(struct drm_device *dev)
1600{
1601 struct intel_crtc *crtc;
1602 int count = 0;
1603
2d84d2b3 1604 for_each_intel_crtc(dev, crtc) {
3538b9df 1605 count += crtc->base.state->active &&
2d84d2b3
VS
1606 intel_crtc_has_type(crtc->config, INTEL_OUTPUT_DVO);
1607 }
1c4e0274
VS
1608
1609 return count;
1610}
1611
66e3d5c0 1612static void i9xx_enable_pll(struct intel_crtc *crtc)
63d7bbe9 1613{
66e3d5c0 1614 struct drm_device *dev = crtc->base.dev;
fac5e23e 1615 struct drm_i915_private *dev_priv = to_i915(dev);
f0f59a00 1616 i915_reg_t reg = DPLL(crtc->pipe);
6e3c9717 1617 u32 dpll = crtc->config->dpll_hw_state.dpll;
63d7bbe9 1618
66e3d5c0 1619 assert_pipe_disabled(dev_priv, crtc->pipe);
58c6eaa2 1620
63d7bbe9 1621 /* PLL is protected by panel, make sure we can write it */
66e3d5c0
DV
1622 if (IS_MOBILE(dev) && !IS_I830(dev))
1623 assert_panel_unlocked(dev_priv, crtc->pipe);
63d7bbe9 1624
1c4e0274
VS
1625 /* Enable DVO 2x clock on both PLLs if necessary */
1626 if (IS_I830(dev) && intel_num_dvo_pipes(dev) > 0) {
1627 /*
1628 * It appears to be important that we don't enable this
1629 * for the current pipe before otherwise configuring the
1630 * PLL. No idea how this should be handled if multiple
1631 * DVO outputs are enabled simultaneosly.
1632 */
1633 dpll |= DPLL_DVO_2X_MODE;
1634 I915_WRITE(DPLL(!crtc->pipe),
1635 I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
1636 }
66e3d5c0 1637
c2b63374
VS
1638 /*
1639 * Apparently we need to have VGA mode enabled prior to changing
1640 * the P1/P2 dividers. Otherwise the DPLL will keep using the old
1641 * dividers, even though the register value does change.
1642 */
1643 I915_WRITE(reg, 0);
1644
8e7a65aa
VS
1645 I915_WRITE(reg, dpll);
1646
66e3d5c0
DV
1647 /* Wait for the clocks to stabilize. */
1648 POSTING_READ(reg);
1649 udelay(150);
1650
1651 if (INTEL_INFO(dev)->gen >= 4) {
1652 I915_WRITE(DPLL_MD(crtc->pipe),
6e3c9717 1653 crtc->config->dpll_hw_state.dpll_md);
66e3d5c0
DV
1654 } else {
1655 /* The pixel multiplier can only be updated once the
1656 * DPLL is enabled and the clocks are stable.
1657 *
1658 * So write it again.
1659 */
1660 I915_WRITE(reg, dpll);
1661 }
63d7bbe9
JB
1662
1663 /* We do this three times for luck */
66e3d5c0 1664 I915_WRITE(reg, dpll);
63d7bbe9
JB
1665 POSTING_READ(reg);
1666 udelay(150); /* wait for warmup */
66e3d5c0 1667 I915_WRITE(reg, dpll);
63d7bbe9
JB
1668 POSTING_READ(reg);
1669 udelay(150); /* wait for warmup */
66e3d5c0 1670 I915_WRITE(reg, dpll);
63d7bbe9
JB
1671 POSTING_READ(reg);
1672 udelay(150); /* wait for warmup */
1673}
1674
1675/**
50b44a44 1676 * i9xx_disable_pll - disable a PLL
63d7bbe9
JB
1677 * @dev_priv: i915 private structure
1678 * @pipe: pipe PLL to disable
1679 *
1680 * Disable the PLL for @pipe, making sure the pipe is off first.
1681 *
1682 * Note! This is for pre-ILK only.
1683 */
1c4e0274 1684static void i9xx_disable_pll(struct intel_crtc *crtc)
63d7bbe9 1685{
1c4e0274 1686 struct drm_device *dev = crtc->base.dev;
fac5e23e 1687 struct drm_i915_private *dev_priv = to_i915(dev);
1c4e0274
VS
1688 enum pipe pipe = crtc->pipe;
1689
1690 /* Disable DVO 2x clock on both PLLs if necessary */
1691 if (IS_I830(dev) &&
2d84d2b3 1692 intel_crtc_has_type(crtc->config, INTEL_OUTPUT_DVO) &&
3538b9df 1693 !intel_num_dvo_pipes(dev)) {
1c4e0274
VS
1694 I915_WRITE(DPLL(PIPE_B),
1695 I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
1696 I915_WRITE(DPLL(PIPE_A),
1697 I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
1698 }
1699
b6b5d049
VS
1700 /* Don't disable pipe or pipe PLLs if needed */
1701 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1702 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
63d7bbe9
JB
1703 return;
1704
1705 /* Make sure the pipe isn't still relying on us */
1706 assert_pipe_disabled(dev_priv, pipe);
1707
b8afb911 1708 I915_WRITE(DPLL(pipe), DPLL_VGA_MODE_DIS);
50b44a44 1709 POSTING_READ(DPLL(pipe));
63d7bbe9
JB
1710}
1711
f6071166
JB
1712static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1713{
b8afb911 1714 u32 val;
f6071166
JB
1715
1716 /* Make sure the pipe isn't still relying on us */
1717 assert_pipe_disabled(dev_priv, pipe);
1718
03ed5cbf
VS
1719 val = DPLL_INTEGRATED_REF_CLK_VLV |
1720 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
1721 if (pipe != PIPE_A)
1722 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
1723
f6071166
JB
1724 I915_WRITE(DPLL(pipe), val);
1725 POSTING_READ(DPLL(pipe));
076ed3b2
CML
1726}
1727
1728static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1729{
d752048d 1730 enum dpio_channel port = vlv_pipe_to_channel(pipe);
076ed3b2
CML
1731 u32 val;
1732
a11b0703
VS
1733 /* Make sure the pipe isn't still relying on us */
1734 assert_pipe_disabled(dev_priv, pipe);
076ed3b2 1735
60bfe44f
VS
1736 val = DPLL_SSC_REF_CLK_CHV |
1737 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
a11b0703
VS
1738 if (pipe != PIPE_A)
1739 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
03ed5cbf 1740
a11b0703
VS
1741 I915_WRITE(DPLL(pipe), val);
1742 POSTING_READ(DPLL(pipe));
d752048d 1743
a580516d 1744 mutex_lock(&dev_priv->sb_lock);
d752048d
VS
1745
1746 /* Disable 10bit clock to display controller */
1747 val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1748 val &= ~DPIO_DCLKP_EN;
1749 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
1750
a580516d 1751 mutex_unlock(&dev_priv->sb_lock);
f6071166
JB
1752}
1753
e4607fcf 1754void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
9b6de0a1
VS
1755 struct intel_digital_port *dport,
1756 unsigned int expected_mask)
89b667f8
JB
1757{
1758 u32 port_mask;
f0f59a00 1759 i915_reg_t dpll_reg;
89b667f8 1760
e4607fcf
CML
1761 switch (dport->port) {
1762 case PORT_B:
89b667f8 1763 port_mask = DPLL_PORTB_READY_MASK;
00fc31b7 1764 dpll_reg = DPLL(0);
e4607fcf
CML
1765 break;
1766 case PORT_C:
89b667f8 1767 port_mask = DPLL_PORTC_READY_MASK;
00fc31b7 1768 dpll_reg = DPLL(0);
9b6de0a1 1769 expected_mask <<= 4;
00fc31b7
CML
1770 break;
1771 case PORT_D:
1772 port_mask = DPLL_PORTD_READY_MASK;
1773 dpll_reg = DPIO_PHY_STATUS;
e4607fcf
CML
1774 break;
1775 default:
1776 BUG();
1777 }
89b667f8 1778
370004d3
CW
1779 if (intel_wait_for_register(dev_priv,
1780 dpll_reg, port_mask, expected_mask,
1781 1000))
9b6de0a1
VS
1782 WARN(1, "timed out waiting for port %c ready: got 0x%x, expected 0x%x\n",
1783 port_name(dport->port), I915_READ(dpll_reg) & port_mask, expected_mask);
89b667f8
JB
1784}
1785
b8a4f404
PZ
1786static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1787 enum pipe pipe)
040484af 1788{
91c8a326 1789 struct drm_device *dev = &dev_priv->drm;
7c26e5c6 1790 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
e2b78267 1791 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
f0f59a00
VS
1792 i915_reg_t reg;
1793 uint32_t val, pipeconf_val;
040484af 1794
040484af 1795 /* Make sure PCH DPLL is enabled */
8106ddbd 1796 assert_shared_dpll_enabled(dev_priv, intel_crtc->config->shared_dpll);
040484af
JB
1797
1798 /* FDI must be feeding us bits for PCH ports */
1799 assert_fdi_tx_enabled(dev_priv, pipe);
1800 assert_fdi_rx_enabled(dev_priv, pipe);
1801
23670b32
DV
1802 if (HAS_PCH_CPT(dev)) {
1803 /* Workaround: Set the timing override bit before enabling the
1804 * pch transcoder. */
1805 reg = TRANS_CHICKEN2(pipe);
1806 val = I915_READ(reg);
1807 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
1808 I915_WRITE(reg, val);
59c859d6 1809 }
23670b32 1810
ab9412ba 1811 reg = PCH_TRANSCONF(pipe);
040484af 1812 val = I915_READ(reg);
5f7f726d 1813 pipeconf_val = I915_READ(PIPECONF(pipe));
e9bcff5c 1814
2d1fe073 1815 if (HAS_PCH_IBX(dev_priv)) {
e9bcff5c 1816 /*
c5de7c6f
VS
1817 * Make the BPC in transcoder be consistent with
1818 * that in pipeconf reg. For HDMI we must use 8bpc
1819 * here for both 8bpc and 12bpc.
e9bcff5c 1820 */
dfd07d72 1821 val &= ~PIPECONF_BPC_MASK;
2d84d2b3 1822 if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_HDMI))
c5de7c6f
VS
1823 val |= PIPECONF_8BPC;
1824 else
1825 val |= pipeconf_val & PIPECONF_BPC_MASK;
e9bcff5c 1826 }
5f7f726d
PZ
1827
1828 val &= ~TRANS_INTERLACE_MASK;
1829 if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
2d1fe073 1830 if (HAS_PCH_IBX(dev_priv) &&
2d84d2b3 1831 intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
7c26e5c6
PZ
1832 val |= TRANS_LEGACY_INTERLACED_ILK;
1833 else
1834 val |= TRANS_INTERLACED;
5f7f726d
PZ
1835 else
1836 val |= TRANS_PROGRESSIVE;
1837
040484af 1838 I915_WRITE(reg, val | TRANS_ENABLE);
650fbd84
CW
1839 if (intel_wait_for_register(dev_priv,
1840 reg, TRANS_STATE_ENABLE, TRANS_STATE_ENABLE,
1841 100))
4bb6f1f3 1842 DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
040484af
JB
1843}
1844
8fb033d7 1845static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
937bb610 1846 enum transcoder cpu_transcoder)
040484af 1847{
8fb033d7 1848 u32 val, pipeconf_val;
8fb033d7 1849
8fb033d7 1850 /* FDI must be feeding us bits for PCH ports */
1a240d4d 1851 assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
937bb610 1852 assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
8fb033d7 1853
223a6fdf 1854 /* Workaround: set timing override bit. */
36c0d0cf 1855 val = I915_READ(TRANS_CHICKEN2(PIPE_A));
23670b32 1856 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
36c0d0cf 1857 I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
223a6fdf 1858
25f3ef11 1859 val = TRANS_ENABLE;
937bb610 1860 pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
8fb033d7 1861
9a76b1c6
PZ
1862 if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
1863 PIPECONF_INTERLACED_ILK)
a35f2679 1864 val |= TRANS_INTERLACED;
8fb033d7
PZ
1865 else
1866 val |= TRANS_PROGRESSIVE;
1867
ab9412ba 1868 I915_WRITE(LPT_TRANSCONF, val);
d9f96244
CW
1869 if (intel_wait_for_register(dev_priv,
1870 LPT_TRANSCONF,
1871 TRANS_STATE_ENABLE,
1872 TRANS_STATE_ENABLE,
1873 100))
937bb610 1874 DRM_ERROR("Failed to enable PCH transcoder\n");
8fb033d7
PZ
1875}
1876
b8a4f404
PZ
1877static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
1878 enum pipe pipe)
040484af 1879{
91c8a326 1880 struct drm_device *dev = &dev_priv->drm;
f0f59a00
VS
1881 i915_reg_t reg;
1882 uint32_t val;
040484af
JB
1883
1884 /* FDI relies on the transcoder */
1885 assert_fdi_tx_disabled(dev_priv, pipe);
1886 assert_fdi_rx_disabled(dev_priv, pipe);
1887
291906f1
JB
1888 /* Ports must be off as well */
1889 assert_pch_ports_disabled(dev_priv, pipe);
1890
ab9412ba 1891 reg = PCH_TRANSCONF(pipe);
040484af
JB
1892 val = I915_READ(reg);
1893 val &= ~TRANS_ENABLE;
1894 I915_WRITE(reg, val);
1895 /* wait for PCH transcoder off, transcoder state */
a7d04662
CW
1896 if (intel_wait_for_register(dev_priv,
1897 reg, TRANS_STATE_ENABLE, 0,
1898 50))
4bb6f1f3 1899 DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
23670b32 1900
c465613b 1901 if (HAS_PCH_CPT(dev)) {
23670b32
DV
1902 /* Workaround: Clear the timing override chicken bit again. */
1903 reg = TRANS_CHICKEN2(pipe);
1904 val = I915_READ(reg);
1905 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
1906 I915_WRITE(reg, val);
1907 }
040484af
JB
1908}
1909
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 */
09fa8bb9 1962 if (HAS_GMCH_DISPLAY(dev_priv)) {
d7edc4e5 1963 if (intel_crtc_has_type(crtc->config, INTEL_OUTPUT_DSI))
23538ef1
JN
1964 assert_dsi_pll_enabled(dev_priv);
1965 else
1966 assert_pll_enabled(dev_priv, pipe);
09fa8bb9 1967 } else {
6e3c9717 1968 if (crtc->config->has_pch_encoder) {
040484af 1969 /* if driving the PCH, we need FDI enabled */
cc391bbb 1970 assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
1a240d4d
DV
1971 assert_fdi_tx_pll_enabled(dev_priv,
1972 (enum pipe) cpu_transcoder);
040484af
JB
1973 }
1974 /* FIXME: assert CPU port conditions for SNB+ */
1975 }
b24e7179 1976
702e7a56 1977 reg = PIPECONF(cpu_transcoder);
b24e7179 1978 val = I915_READ(reg);
7ad25d48 1979 if (val & PIPECONF_ENABLE) {
b6b5d049
VS
1980 WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1981 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
00d70b15 1982 return;
7ad25d48 1983 }
00d70b15
CW
1984
1985 I915_WRITE(reg, val | PIPECONF_ENABLE);
851855d8 1986 POSTING_READ(reg);
b7792d8b
VS
1987
1988 /*
1989 * Until the pipe starts DSL will read as 0, which would cause
1990 * an apparent vblank timestamp jump, which messes up also the
1991 * frame count when it's derived from the timestamps. So let's
1992 * wait for the pipe to start properly before we call
1993 * drm_crtc_vblank_on()
1994 */
1995 if (dev->max_vblank_count == 0 &&
1996 wait_for(intel_get_crtc_scanline(crtc) != crtc->scanline_offset, 50))
1997 DRM_ERROR("pipe %c didn't start\n", pipe_name(pipe));
b24e7179
JB
1998}
1999
2000/**
309cfea8 2001 * intel_disable_pipe - disable a pipe, asserting requirements
575f7ab7 2002 * @crtc: crtc whose pipes is to be disabled
b24e7179 2003 *
575f7ab7
VS
2004 * Disable the pipe of @crtc, making sure that various hardware
2005 * specific requirements are met, if applicable, e.g. plane
2006 * disabled, panel fitter off, etc.
b24e7179
JB
2007 *
2008 * Will wait until the pipe has shut down before returning.
2009 */
575f7ab7 2010static void intel_disable_pipe(struct intel_crtc *crtc)
b24e7179 2011{
fac5e23e 2012 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6e3c9717 2013 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
575f7ab7 2014 enum pipe pipe = crtc->pipe;
f0f59a00 2015 i915_reg_t reg;
b24e7179
JB
2016 u32 val;
2017
9e2ee2dd
VS
2018 DRM_DEBUG_KMS("disabling pipe %c\n", pipe_name(pipe));
2019
b24e7179
JB
2020 /*
2021 * Make sure planes won't keep trying to pump pixels to us,
2022 * or we might hang the display.
2023 */
2024 assert_planes_disabled(dev_priv, pipe);
93ce0ba6 2025 assert_cursor_disabled(dev_priv, pipe);
19332d7a 2026 assert_sprites_disabled(dev_priv, pipe);
b24e7179 2027
702e7a56 2028 reg = PIPECONF(cpu_transcoder);
b24e7179 2029 val = I915_READ(reg);
00d70b15
CW
2030 if ((val & PIPECONF_ENABLE) == 0)
2031 return;
2032
67adc644
VS
2033 /*
2034 * Double wide has implications for planes
2035 * so best keep it disabled when not needed.
2036 */
6e3c9717 2037 if (crtc->config->double_wide)
67adc644
VS
2038 val &= ~PIPECONF_DOUBLE_WIDE;
2039
2040 /* Don't disable pipe or pipe PLLs if needed */
b6b5d049
VS
2041 if (!(pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) &&
2042 !(pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
67adc644
VS
2043 val &= ~PIPECONF_ENABLE;
2044
2045 I915_WRITE(reg, val);
2046 if ((val & PIPECONF_ENABLE) == 0)
2047 intel_wait_for_pipe_off(crtc);
b24e7179
JB
2048}
2049
832be82f
VS
2050static unsigned int intel_tile_size(const struct drm_i915_private *dev_priv)
2051{
2052 return IS_GEN2(dev_priv) ? 2048 : 4096;
2053}
2054
27ba3910
VS
2055static unsigned int intel_tile_width_bytes(const struct drm_i915_private *dev_priv,
2056 uint64_t fb_modifier, unsigned int cpp)
7b49f948
VS
2057{
2058 switch (fb_modifier) {
2059 case DRM_FORMAT_MOD_NONE:
2060 return cpp;
2061 case I915_FORMAT_MOD_X_TILED:
2062 if (IS_GEN2(dev_priv))
2063 return 128;
2064 else
2065 return 512;
2066 case I915_FORMAT_MOD_Y_TILED:
2067 if (IS_GEN2(dev_priv) || HAS_128_BYTE_Y_TILING(dev_priv))
2068 return 128;
2069 else
2070 return 512;
2071 case I915_FORMAT_MOD_Yf_TILED:
2072 switch (cpp) {
2073 case 1:
2074 return 64;
2075 case 2:
2076 case 4:
2077 return 128;
2078 case 8:
2079 case 16:
2080 return 256;
2081 default:
2082 MISSING_CASE(cpp);
2083 return cpp;
2084 }
2085 break;
2086 default:
2087 MISSING_CASE(fb_modifier);
2088 return cpp;
2089 }
2090}
2091
832be82f
VS
2092unsigned int intel_tile_height(const struct drm_i915_private *dev_priv,
2093 uint64_t fb_modifier, unsigned int cpp)
a57ce0b2 2094{
832be82f
VS
2095 if (fb_modifier == DRM_FORMAT_MOD_NONE)
2096 return 1;
2097 else
2098 return intel_tile_size(dev_priv) /
27ba3910 2099 intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
6761dd31
TU
2100}
2101
8d0deca8
VS
2102/* Return the tile dimensions in pixel units */
2103static void intel_tile_dims(const struct drm_i915_private *dev_priv,
2104 unsigned int *tile_width,
2105 unsigned int *tile_height,
2106 uint64_t fb_modifier,
2107 unsigned int cpp)
2108{
2109 unsigned int tile_width_bytes =
2110 intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
2111
2112 *tile_width = tile_width_bytes / cpp;
2113 *tile_height = intel_tile_size(dev_priv) / tile_width_bytes;
2114}
2115
6761dd31
TU
2116unsigned int
2117intel_fb_align_height(struct drm_device *dev, unsigned int height,
832be82f 2118 uint32_t pixel_format, uint64_t fb_modifier)
6761dd31 2119{
832be82f
VS
2120 unsigned int cpp = drm_format_plane_cpp(pixel_format, 0);
2121 unsigned int tile_height = intel_tile_height(to_i915(dev), fb_modifier, cpp);
2122
2123 return ALIGN(height, tile_height);
a57ce0b2
JB
2124}
2125
1663b9d6
VS
2126unsigned int intel_rotation_info_size(const struct intel_rotation_info *rot_info)
2127{
2128 unsigned int size = 0;
2129 int i;
2130
2131 for (i = 0 ; i < ARRAY_SIZE(rot_info->plane); i++)
2132 size += rot_info->plane[i].width * rot_info->plane[i].height;
2133
2134 return size;
2135}
2136
75c82a53 2137static void
3465c580
VS
2138intel_fill_fb_ggtt_view(struct i915_ggtt_view *view,
2139 const struct drm_framebuffer *fb,
2140 unsigned int rotation)
f64b98cd 2141{
2d7a215f
VS
2142 if (intel_rotation_90_or_270(rotation)) {
2143 *view = i915_ggtt_view_rotated;
2144 view->params.rotated = to_intel_framebuffer(fb)->rot_info;
2145 } else {
2146 *view = i915_ggtt_view_normal;
2147 }
2148}
50470bb0 2149
603525d7 2150static unsigned int intel_linear_alignment(const struct drm_i915_private *dev_priv)
4e9a86b6
VS
2151{
2152 if (INTEL_INFO(dev_priv)->gen >= 9)
2153 return 256 * 1024;
985b8bb4 2154 else if (IS_BROADWATER(dev_priv) || IS_CRESTLINE(dev_priv) ||
666a4537 2155 IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
4e9a86b6
VS
2156 return 128 * 1024;
2157 else if (INTEL_INFO(dev_priv)->gen >= 4)
2158 return 4 * 1024;
2159 else
44c5905e 2160 return 0;
4e9a86b6
VS
2161}
2162
603525d7
VS
2163static unsigned int intel_surf_alignment(const struct drm_i915_private *dev_priv,
2164 uint64_t fb_modifier)
2165{
2166 switch (fb_modifier) {
2167 case DRM_FORMAT_MOD_NONE:
2168 return intel_linear_alignment(dev_priv);
2169 case I915_FORMAT_MOD_X_TILED:
2170 if (INTEL_INFO(dev_priv)->gen >= 9)
2171 return 256 * 1024;
2172 return 0;
2173 case I915_FORMAT_MOD_Y_TILED:
2174 case I915_FORMAT_MOD_Yf_TILED:
2175 return 1 * 1024 * 1024;
2176 default:
2177 MISSING_CASE(fb_modifier);
2178 return 0;
2179 }
2180}
2181
127bd2ac 2182int
3465c580
VS
2183intel_pin_and_fence_fb_obj(struct drm_framebuffer *fb,
2184 unsigned int rotation)
6b95a207 2185{
850c4cdc 2186 struct drm_device *dev = fb->dev;
fac5e23e 2187 struct drm_i915_private *dev_priv = to_i915(dev);
850c4cdc 2188 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
f64b98cd 2189 struct i915_ggtt_view view;
6b95a207
KH
2190 u32 alignment;
2191 int ret;
2192
ebcdd39e
MR
2193 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
2194
603525d7 2195 alignment = intel_surf_alignment(dev_priv, fb->modifier[0]);
6b95a207 2196
3465c580 2197 intel_fill_fb_ggtt_view(&view, fb, rotation);
f64b98cd 2198
693db184
CW
2199 /* Note that the w/a also requires 64 PTE of padding following the
2200 * bo. We currently fill all unused PTE with the shadow page and so
2201 * we should always have valid PTE following the scanout preventing
2202 * the VT-d warning.
2203 */
48f112fe 2204 if (intel_scanout_needs_vtd_wa(dev_priv) && alignment < 256 * 1024)
693db184
CW
2205 alignment = 256 * 1024;
2206
d6dd6843
PZ
2207 /*
2208 * Global gtt pte registers are special registers which actually forward
2209 * writes to a chunk of system memory. Which means that there is no risk
2210 * that the register values disappear as soon as we call
2211 * intel_runtime_pm_put(), so it is correct to wrap only the
2212 * pin/unpin/fence and not more.
2213 */
2214 intel_runtime_pm_get(dev_priv);
2215
7580d774
ML
2216 ret = i915_gem_object_pin_to_display_plane(obj, alignment,
2217 &view);
48b956c5 2218 if (ret)
b26a6b35 2219 goto err_pm;
6b95a207
KH
2220
2221 /* Install a fence for tiled scan-out. Pre-i965 always needs a
2222 * fence, whereas 965+ only requires a fence if using
2223 * framebuffer compression. For simplicity, we always install
2224 * a fence as the cost is not that onerous.
2225 */
9807216f
VK
2226 if (view.type == I915_GGTT_VIEW_NORMAL) {
2227 ret = i915_gem_object_get_fence(obj);
2228 if (ret == -EDEADLK) {
2229 /*
2230 * -EDEADLK means there are no free fences
2231 * no pending flips.
2232 *
2233 * This is propagated to atomic, but it uses
2234 * -EDEADLK to force a locking recovery, so
2235 * change the returned error to -EBUSY.
2236 */
2237 ret = -EBUSY;
2238 goto err_unpin;
2239 } else if (ret)
2240 goto err_unpin;
1690e1eb 2241
9807216f
VK
2242 i915_gem_object_pin_fence(obj);
2243 }
6b95a207 2244
d6dd6843 2245 intel_runtime_pm_put(dev_priv);
6b95a207 2246 return 0;
48b956c5
CW
2247
2248err_unpin:
f64b98cd 2249 i915_gem_object_unpin_from_display_plane(obj, &view);
b26a6b35 2250err_pm:
d6dd6843 2251 intel_runtime_pm_put(dev_priv);
48b956c5 2252 return ret;
6b95a207
KH
2253}
2254
fb4b8ce1 2255void intel_unpin_fb_obj(struct drm_framebuffer *fb, unsigned int rotation)
1690e1eb 2256{
82bc3b2d 2257 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
f64b98cd 2258 struct i915_ggtt_view view;
82bc3b2d 2259
ebcdd39e
MR
2260 WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
2261
3465c580 2262 intel_fill_fb_ggtt_view(&view, fb, rotation);
f64b98cd 2263
9807216f
VK
2264 if (view.type == I915_GGTT_VIEW_NORMAL)
2265 i915_gem_object_unpin_fence(obj);
2266
f64b98cd 2267 i915_gem_object_unpin_from_display_plane(obj, &view);
1690e1eb
CW
2268}
2269
ef78ec94
VS
2270static int intel_fb_pitch(const struct drm_framebuffer *fb, int plane,
2271 unsigned int rotation)
2272{
2273 if (intel_rotation_90_or_270(rotation))
2274 return to_intel_framebuffer(fb)->rotated[plane].pitch;
2275 else
2276 return fb->pitches[plane];
2277}
2278
6687c906
VS
2279/*
2280 * Convert the x/y offsets into a linear offset.
2281 * Only valid with 0/180 degree rotation, which is fine since linear
2282 * offset is only used with linear buffers on pre-hsw and tiled buffers
2283 * with gen2/3, and 90/270 degree rotations isn't supported on any of them.
2284 */
2285u32 intel_fb_xy_to_linear(int x, int y,
2949056c
VS
2286 const struct intel_plane_state *state,
2287 int plane)
6687c906 2288{
2949056c 2289 const struct drm_framebuffer *fb = state->base.fb;
6687c906
VS
2290 unsigned int cpp = drm_format_plane_cpp(fb->pixel_format, plane);
2291 unsigned int pitch = fb->pitches[plane];
2292
2293 return y * pitch + x * cpp;
2294}
2295
2296/*
2297 * Add the x/y offsets derived from fb->offsets[] to the user
2298 * specified plane src x/y offsets. The resulting x/y offsets
2299 * specify the start of scanout from the beginning of the gtt mapping.
2300 */
2301void intel_add_fb_offsets(int *x, int *y,
2949056c
VS
2302 const struct intel_plane_state *state,
2303 int plane)
6687c906
VS
2304
2305{
2949056c
VS
2306 const struct intel_framebuffer *intel_fb = to_intel_framebuffer(state->base.fb);
2307 unsigned int rotation = state->base.rotation;
6687c906
VS
2308
2309 if (intel_rotation_90_or_270(rotation)) {
2310 *x += intel_fb->rotated[plane].x;
2311 *y += intel_fb->rotated[plane].y;
2312 } else {
2313 *x += intel_fb->normal[plane].x;
2314 *y += intel_fb->normal[plane].y;
2315 }
2316}
2317
29cf9491
VS
2318/*
2319 * Adjust the tile offset by moving the difference into
2320 * the x/y offsets.
2321 *
2322 * Input tile dimensions and pitch must already be
2323 * rotated to match x and y, and in pixel units.
2324 */
2325static u32 intel_adjust_tile_offset(int *x, int *y,
2326 unsigned int tile_width,
2327 unsigned int tile_height,
2328 unsigned int tile_size,
2329 unsigned int pitch_tiles,
2330 u32 old_offset,
2331 u32 new_offset)
2332{
2333 unsigned int tiles;
2334
2335 WARN_ON(old_offset & (tile_size - 1));
2336 WARN_ON(new_offset & (tile_size - 1));
2337 WARN_ON(new_offset > old_offset);
2338
2339 tiles = (old_offset - new_offset) / tile_size;
2340
2341 *y += tiles / pitch_tiles * tile_height;
2342 *x += tiles % pitch_tiles * tile_width;
2343
2344 return new_offset;
2345}
2346
8d0deca8
VS
2347/*
2348 * Computes the linear offset to the base tile and adjusts
2349 * x, y. bytes per pixel is assumed to be a power-of-two.
2350 *
2351 * In the 90/270 rotated case, x and y are assumed
2352 * to be already rotated to match the rotated GTT view, and
2353 * pitch is the tile_height aligned framebuffer height.
6687c906
VS
2354 *
2355 * This function is used when computing the derived information
2356 * under intel_framebuffer, so using any of that information
2357 * here is not allowed. Anything under drm_framebuffer can be
2358 * used. This is why the user has to pass in the pitch since it
2359 * is specified in the rotated orientation.
8d0deca8 2360 */
6687c906
VS
2361static u32 _intel_compute_tile_offset(const struct drm_i915_private *dev_priv,
2362 int *x, int *y,
2363 const struct drm_framebuffer *fb, int plane,
2364 unsigned int pitch,
2365 unsigned int rotation,
2366 u32 alignment)
c2c75131 2367{
4f2d9934
VS
2368 uint64_t fb_modifier = fb->modifier[plane];
2369 unsigned int cpp = drm_format_plane_cpp(fb->pixel_format, plane);
6687c906 2370 u32 offset, offset_aligned;
29cf9491 2371
29cf9491
VS
2372 if (alignment)
2373 alignment--;
2374
b5c65338 2375 if (fb_modifier != DRM_FORMAT_MOD_NONE) {
8d0deca8
VS
2376 unsigned int tile_size, tile_width, tile_height;
2377 unsigned int tile_rows, tiles, pitch_tiles;
c2c75131 2378
d843310d 2379 tile_size = intel_tile_size(dev_priv);
8d0deca8
VS
2380 intel_tile_dims(dev_priv, &tile_width, &tile_height,
2381 fb_modifier, cpp);
2382
2383 if (intel_rotation_90_or_270(rotation)) {
2384 pitch_tiles = pitch / tile_height;
2385 swap(tile_width, tile_height);
2386 } else {
2387 pitch_tiles = pitch / (tile_width * cpp);
2388 }
d843310d
VS
2389
2390 tile_rows = *y / tile_height;
2391 *y %= tile_height;
c2c75131 2392
8d0deca8
VS
2393 tiles = *x / tile_width;
2394 *x %= tile_width;
bc752862 2395
29cf9491
VS
2396 offset = (tile_rows * pitch_tiles + tiles) * tile_size;
2397 offset_aligned = offset & ~alignment;
bc752862 2398
29cf9491
VS
2399 intel_adjust_tile_offset(x, y, tile_width, tile_height,
2400 tile_size, pitch_tiles,
2401 offset, offset_aligned);
2402 } else {
bc752862 2403 offset = *y * pitch + *x * cpp;
29cf9491
VS
2404 offset_aligned = offset & ~alignment;
2405
4e9a86b6
VS
2406 *y = (offset & alignment) / pitch;
2407 *x = ((offset & alignment) - *y * pitch) / cpp;
bc752862 2408 }
29cf9491
VS
2409
2410 return offset_aligned;
c2c75131
DV
2411}
2412
6687c906 2413u32 intel_compute_tile_offset(int *x, int *y,
2949056c
VS
2414 const struct intel_plane_state *state,
2415 int plane)
6687c906 2416{
2949056c
VS
2417 const struct drm_i915_private *dev_priv = to_i915(state->base.plane->dev);
2418 const struct drm_framebuffer *fb = state->base.fb;
2419 unsigned int rotation = state->base.rotation;
6687c906 2420 u32 alignment = intel_surf_alignment(dev_priv, fb->modifier[plane]);
ef78ec94 2421 int pitch = intel_fb_pitch(fb, plane, rotation);
6687c906
VS
2422
2423 return _intel_compute_tile_offset(dev_priv, x, y, fb, plane, pitch,
2424 rotation, alignment);
2425}
2426
2427/* Convert the fb->offset[] linear offset into x/y offsets */
2428static void intel_fb_offset_to_xy(int *x, int *y,
2429 const struct drm_framebuffer *fb, int plane)
2430{
2431 unsigned int cpp = drm_format_plane_cpp(fb->pixel_format, plane);
2432 unsigned int pitch = fb->pitches[plane];
2433 u32 linear_offset = fb->offsets[plane];
2434
2435 *y = linear_offset / pitch;
2436 *x = linear_offset % pitch / cpp;
2437}
2438
2439static int
2440intel_fill_fb_info(struct drm_i915_private *dev_priv,
2441 struct drm_framebuffer *fb)
2442{
2443 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
2444 struct intel_rotation_info *rot_info = &intel_fb->rot_info;
2445 u32 gtt_offset_rotated = 0;
2446 unsigned int max_size = 0;
2447 uint32_t format = fb->pixel_format;
2448 int i, num_planes = drm_format_num_planes(format);
2449 unsigned int tile_size = intel_tile_size(dev_priv);
2450
2451 for (i = 0; i < num_planes; i++) {
2452 unsigned int width, height;
2453 unsigned int cpp, size;
2454 u32 offset;
2455 int x, y;
2456
2457 cpp = drm_format_plane_cpp(format, i);
2458 width = drm_format_plane_width(fb->width, format, i);
2459 height = drm_format_plane_height(fb->height, format, i);
2460
2461 intel_fb_offset_to_xy(&x, &y, fb, i);
2462
2463 /*
2464 * First pixel of the framebuffer from
2465 * the start of the normal gtt mapping.
2466 */
2467 intel_fb->normal[i].x = x;
2468 intel_fb->normal[i].y = y;
2469
2470 offset = _intel_compute_tile_offset(dev_priv, &x, &y,
2471 fb, 0, fb->pitches[i],
2472 BIT(DRM_ROTATE_0), tile_size);
2473 offset /= tile_size;
2474
2475 if (fb->modifier[i] != DRM_FORMAT_MOD_NONE) {
2476 unsigned int tile_width, tile_height;
2477 unsigned int pitch_tiles;
2478 struct drm_rect r;
2479
2480 intel_tile_dims(dev_priv, &tile_width, &tile_height,
2481 fb->modifier[i], cpp);
2482
2483 rot_info->plane[i].offset = offset;
2484 rot_info->plane[i].stride = DIV_ROUND_UP(fb->pitches[i], tile_width * cpp);
2485 rot_info->plane[i].width = DIV_ROUND_UP(x + width, tile_width);
2486 rot_info->plane[i].height = DIV_ROUND_UP(y + height, tile_height);
2487
2488 intel_fb->rotated[i].pitch =
2489 rot_info->plane[i].height * tile_height;
2490
2491 /* how many tiles does this plane need */
2492 size = rot_info->plane[i].stride * rot_info->plane[i].height;
2493 /*
2494 * If the plane isn't horizontally tile aligned,
2495 * we need one more tile.
2496 */
2497 if (x != 0)
2498 size++;
2499
2500 /* rotate the x/y offsets to match the GTT view */
2501 r.x1 = x;
2502 r.y1 = y;
2503 r.x2 = x + width;
2504 r.y2 = y + height;
2505 drm_rect_rotate(&r,
2506 rot_info->plane[i].width * tile_width,
2507 rot_info->plane[i].height * tile_height,
2508 BIT(DRM_ROTATE_270));
2509 x = r.x1;
2510 y = r.y1;
2511
2512 /* rotate the tile dimensions to match the GTT view */
2513 pitch_tiles = intel_fb->rotated[i].pitch / tile_height;
2514 swap(tile_width, tile_height);
2515
2516 /*
2517 * We only keep the x/y offsets, so push all of the
2518 * gtt offset into the x/y offsets.
2519 */
2520 intel_adjust_tile_offset(&x, &y, tile_size,
2521 tile_width, tile_height, pitch_tiles,
2522 gtt_offset_rotated * tile_size, 0);
2523
2524 gtt_offset_rotated += rot_info->plane[i].width * rot_info->plane[i].height;
2525
2526 /*
2527 * First pixel of the framebuffer from
2528 * the start of the rotated gtt mapping.
2529 */
2530 intel_fb->rotated[i].x = x;
2531 intel_fb->rotated[i].y = y;
2532 } else {
2533 size = DIV_ROUND_UP((y + height) * fb->pitches[i] +
2534 x * cpp, tile_size);
2535 }
2536
2537 /* how many tiles in total needed in the bo */
2538 max_size = max(max_size, offset + size);
2539 }
2540
2541 if (max_size * tile_size > to_intel_framebuffer(fb)->obj->base.size) {
2542 DRM_DEBUG("fb too big for bo (need %u bytes, have %zu bytes)\n",
2543 max_size * tile_size, to_intel_framebuffer(fb)->obj->base.size);
2544 return -EINVAL;
2545 }
2546
2547 return 0;
2548}
2549
b35d63fa 2550static int i9xx_format_to_fourcc(int format)
46f297fb
JB
2551{
2552 switch (format) {
2553 case DISPPLANE_8BPP:
2554 return DRM_FORMAT_C8;
2555 case DISPPLANE_BGRX555:
2556 return DRM_FORMAT_XRGB1555;
2557 case DISPPLANE_BGRX565:
2558 return DRM_FORMAT_RGB565;
2559 default:
2560 case DISPPLANE_BGRX888:
2561 return DRM_FORMAT_XRGB8888;
2562 case DISPPLANE_RGBX888:
2563 return DRM_FORMAT_XBGR8888;
2564 case DISPPLANE_BGRX101010:
2565 return DRM_FORMAT_XRGB2101010;
2566 case DISPPLANE_RGBX101010:
2567 return DRM_FORMAT_XBGR2101010;
2568 }
2569}
2570
bc8d7dff
DL
2571static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
2572{
2573 switch (format) {
2574 case PLANE_CTL_FORMAT_RGB_565:
2575 return DRM_FORMAT_RGB565;
2576 default:
2577 case PLANE_CTL_FORMAT_XRGB_8888:
2578 if (rgb_order) {
2579 if (alpha)
2580 return DRM_FORMAT_ABGR8888;
2581 else
2582 return DRM_FORMAT_XBGR8888;
2583 } else {
2584 if (alpha)
2585 return DRM_FORMAT_ARGB8888;
2586 else
2587 return DRM_FORMAT_XRGB8888;
2588 }
2589 case PLANE_CTL_FORMAT_XRGB_2101010:
2590 if (rgb_order)
2591 return DRM_FORMAT_XBGR2101010;
2592 else
2593 return DRM_FORMAT_XRGB2101010;
2594 }
2595}
2596
5724dbd1 2597static bool
f6936e29
DV
2598intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
2599 struct intel_initial_plane_config *plane_config)
46f297fb
JB
2600{
2601 struct drm_device *dev = crtc->base.dev;
3badb49f 2602 struct drm_i915_private *dev_priv = to_i915(dev);
72e96d64 2603 struct i915_ggtt *ggtt = &dev_priv->ggtt;
46f297fb
JB
2604 struct drm_i915_gem_object *obj = NULL;
2605 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
2d14030b 2606 struct drm_framebuffer *fb = &plane_config->fb->base;
f37b5c2b
DV
2607 u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
2608 u32 size_aligned = round_up(plane_config->base + plane_config->size,
2609 PAGE_SIZE);
2610
2611 size_aligned -= base_aligned;
46f297fb 2612
ff2652ea
CW
2613 if (plane_config->size == 0)
2614 return false;
2615
3badb49f
PZ
2616 /* If the FB is too big, just don't use it since fbdev is not very
2617 * important and we should probably use that space with FBC or other
2618 * features. */
72e96d64 2619 if (size_aligned * 2 > ggtt->stolen_usable_size)
3badb49f
PZ
2620 return false;
2621
12c83d99
TU
2622 mutex_lock(&dev->struct_mutex);
2623
f37b5c2b
DV
2624 obj = i915_gem_object_create_stolen_for_preallocated(dev,
2625 base_aligned,
2626 base_aligned,
2627 size_aligned);
12c83d99
TU
2628 if (!obj) {
2629 mutex_unlock(&dev->struct_mutex);
484b41dd 2630 return false;
12c83d99 2631 }
46f297fb 2632
3e510a8e
CW
2633 if (plane_config->tiling == I915_TILING_X)
2634 obj->tiling_and_stride = fb->pitches[0] | I915_TILING_X;
46f297fb 2635
6bf129df
DL
2636 mode_cmd.pixel_format = fb->pixel_format;
2637 mode_cmd.width = fb->width;
2638 mode_cmd.height = fb->height;
2639 mode_cmd.pitches[0] = fb->pitches[0];
18c5247e
DV
2640 mode_cmd.modifier[0] = fb->modifier[0];
2641 mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
46f297fb 2642
6bf129df 2643 if (intel_framebuffer_init(dev, to_intel_framebuffer(fb),
484b41dd 2644 &mode_cmd, obj)) {
46f297fb
JB
2645 DRM_DEBUG_KMS("intel fb init failed\n");
2646 goto out_unref_obj;
2647 }
12c83d99 2648
46f297fb 2649 mutex_unlock(&dev->struct_mutex);
484b41dd 2650
f6936e29 2651 DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
484b41dd 2652 return true;
46f297fb
JB
2653
2654out_unref_obj:
f8c417cd 2655 i915_gem_object_put(obj);
46f297fb 2656 mutex_unlock(&dev->struct_mutex);
484b41dd
JB
2657 return false;
2658}
2659
5a21b665
DV
2660/* Update plane->state->fb to match plane->fb after driver-internal updates */
2661static void
2662update_state_fb(struct drm_plane *plane)
2663{
2664 if (plane->fb == plane->state->fb)
2665 return;
2666
2667 if (plane->state->fb)
2668 drm_framebuffer_unreference(plane->state->fb);
2669 plane->state->fb = plane->fb;
2670 if (plane->state->fb)
2671 drm_framebuffer_reference(plane->state->fb);
2672}
2673
5724dbd1 2674static void
f6936e29
DV
2675intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
2676 struct intel_initial_plane_config *plane_config)
484b41dd
JB
2677{
2678 struct drm_device *dev = intel_crtc->base.dev;
fac5e23e 2679 struct drm_i915_private *dev_priv = to_i915(dev);
484b41dd
JB
2680 struct drm_crtc *c;
2681 struct intel_crtc *i;
2ff8fde1 2682 struct drm_i915_gem_object *obj;
88595ac9 2683 struct drm_plane *primary = intel_crtc->base.primary;
be5651f2 2684 struct drm_plane_state *plane_state = primary->state;
200757f5
MR
2685 struct drm_crtc_state *crtc_state = intel_crtc->base.state;
2686 struct intel_plane *intel_plane = to_intel_plane(primary);
0a8d8a86
MR
2687 struct intel_plane_state *intel_state =
2688 to_intel_plane_state(plane_state);
88595ac9 2689 struct drm_framebuffer *fb;
484b41dd 2690
2d14030b 2691 if (!plane_config->fb)
484b41dd
JB
2692 return;
2693
f6936e29 2694 if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
88595ac9
DV
2695 fb = &plane_config->fb->base;
2696 goto valid_fb;
f55548b5 2697 }
484b41dd 2698
2d14030b 2699 kfree(plane_config->fb);
484b41dd
JB
2700
2701 /*
2702 * Failed to alloc the obj, check to see if we should share
2703 * an fb with another CRTC instead
2704 */
70e1e0ec 2705 for_each_crtc(dev, c) {
484b41dd
JB
2706 i = to_intel_crtc(c);
2707
2708 if (c == &intel_crtc->base)
2709 continue;
2710
2ff8fde1
MR
2711 if (!i->active)
2712 continue;
2713
88595ac9
DV
2714 fb = c->primary->fb;
2715 if (!fb)
484b41dd
JB
2716 continue;
2717
88595ac9 2718 obj = intel_fb_obj(fb);
2ff8fde1 2719 if (i915_gem_obj_ggtt_offset(obj) == plane_config->base) {
88595ac9
DV
2720 drm_framebuffer_reference(fb);
2721 goto valid_fb;
484b41dd
JB
2722 }
2723 }
88595ac9 2724
200757f5
MR
2725 /*
2726 * We've failed to reconstruct the BIOS FB. Current display state
2727 * indicates that the primary plane is visible, but has a NULL FB,
2728 * which will lead to problems later if we don't fix it up. The
2729 * simplest solution is to just disable the primary plane now and
2730 * pretend the BIOS never had it enabled.
2731 */
2732 to_intel_plane_state(plane_state)->visible = false;
2733 crtc_state->plane_mask &= ~(1 << drm_plane_index(primary));
2622a081 2734 intel_pre_disable_primary_noatomic(&intel_crtc->base);
200757f5
MR
2735 intel_plane->disable_plane(primary, &intel_crtc->base);
2736
88595ac9
DV
2737 return;
2738
2739valid_fb:
f44e2659
VS
2740 plane_state->src_x = 0;
2741 plane_state->src_y = 0;
be5651f2
ML
2742 plane_state->src_w = fb->width << 16;
2743 plane_state->src_h = fb->height << 16;
2744
f44e2659
VS
2745 plane_state->crtc_x = 0;
2746 plane_state->crtc_y = 0;
be5651f2
ML
2747 plane_state->crtc_w = fb->width;
2748 plane_state->crtc_h = fb->height;
2749
0a8d8a86
MR
2750 intel_state->src.x1 = plane_state->src_x;
2751 intel_state->src.y1 = plane_state->src_y;
2752 intel_state->src.x2 = plane_state->src_x + plane_state->src_w;
2753 intel_state->src.y2 = plane_state->src_y + plane_state->src_h;
2754 intel_state->dst.x1 = plane_state->crtc_x;
2755 intel_state->dst.y1 = plane_state->crtc_y;
2756 intel_state->dst.x2 = plane_state->crtc_x + plane_state->crtc_w;
2757 intel_state->dst.y2 = plane_state->crtc_y + plane_state->crtc_h;
2758
88595ac9 2759 obj = intel_fb_obj(fb);
3e510a8e 2760 if (i915_gem_object_is_tiled(obj))
88595ac9
DV
2761 dev_priv->preserve_bios_swizzle = true;
2762
be5651f2
ML
2763 drm_framebuffer_reference(fb);
2764 primary->fb = primary->state->fb = fb;
36750f28 2765 primary->crtc = primary->state->crtc = &intel_crtc->base;
36750f28 2766 intel_crtc->base.state->plane_mask |= (1 << drm_plane_index(primary));
faf5bf0a
CW
2767 atomic_or(to_intel_plane(primary)->frontbuffer_bit,
2768 &obj->frontbuffer_bits);
46f297fb
JB
2769}
2770
a8d201af
ML
2771static void i9xx_update_primary_plane(struct drm_plane *primary,
2772 const struct intel_crtc_state *crtc_state,
2773 const struct intel_plane_state *plane_state)
81255565 2774{
a8d201af 2775 struct drm_device *dev = primary->dev;
fac5e23e 2776 struct drm_i915_private *dev_priv = to_i915(dev);
a8d201af
ML
2777 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
2778 struct drm_framebuffer *fb = plane_state->base.fb;
2779 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
81255565 2780 int plane = intel_crtc->plane;
54ea9da8 2781 u32 linear_offset;
81255565 2782 u32 dspcntr;
f0f59a00 2783 i915_reg_t reg = DSPCNTR(plane);
8d0deca8 2784 unsigned int rotation = plane_state->base.rotation;
54ea9da8
VS
2785 int x = plane_state->src.x1 >> 16;
2786 int y = plane_state->src.y1 >> 16;
c9ba6fad 2787
f45651ba
VS
2788 dspcntr = DISPPLANE_GAMMA_ENABLE;
2789
fdd508a6 2790 dspcntr |= DISPLAY_PLANE_ENABLE;
f45651ba
VS
2791
2792 if (INTEL_INFO(dev)->gen < 4) {
2793 if (intel_crtc->pipe == PIPE_B)
2794 dspcntr |= DISPPLANE_SEL_PIPE_B;
2795
2796 /* pipesrc and dspsize control the size that is scaled from,
2797 * which should always be the user's requested size.
2798 */
2799 I915_WRITE(DSPSIZE(plane),
a8d201af
ML
2800 ((crtc_state->pipe_src_h - 1) << 16) |
2801 (crtc_state->pipe_src_w - 1));
f45651ba 2802 I915_WRITE(DSPPOS(plane), 0);
c14b0485
VS
2803 } else if (IS_CHERRYVIEW(dev) && plane == PLANE_B) {
2804 I915_WRITE(PRIMSIZE(plane),
a8d201af
ML
2805 ((crtc_state->pipe_src_h - 1) << 16) |
2806 (crtc_state->pipe_src_w - 1));
c14b0485
VS
2807 I915_WRITE(PRIMPOS(plane), 0);
2808 I915_WRITE(PRIMCNSTALPHA(plane), 0);
f45651ba 2809 }
81255565 2810
57779d06
VS
2811 switch (fb->pixel_format) {
2812 case DRM_FORMAT_C8:
81255565
JB
2813 dspcntr |= DISPPLANE_8BPP;
2814 break;
57779d06 2815 case DRM_FORMAT_XRGB1555:
57779d06 2816 dspcntr |= DISPPLANE_BGRX555;
81255565 2817 break;
57779d06
VS
2818 case DRM_FORMAT_RGB565:
2819 dspcntr |= DISPPLANE_BGRX565;
2820 break;
2821 case DRM_FORMAT_XRGB8888:
57779d06
VS
2822 dspcntr |= DISPPLANE_BGRX888;
2823 break;
2824 case DRM_FORMAT_XBGR8888:
57779d06
VS
2825 dspcntr |= DISPPLANE_RGBX888;
2826 break;
2827 case DRM_FORMAT_XRGB2101010:
57779d06
VS
2828 dspcntr |= DISPPLANE_BGRX101010;
2829 break;
2830 case DRM_FORMAT_XBGR2101010:
57779d06 2831 dspcntr |= DISPPLANE_RGBX101010;
81255565
JB
2832 break;
2833 default:
baba133a 2834 BUG();
81255565 2835 }
57779d06 2836
3e510a8e 2837 if (INTEL_INFO(dev)->gen >= 4 && i915_gem_object_is_tiled(obj))
f45651ba 2838 dspcntr |= DISPPLANE_TILED;
81255565 2839
de1aa629
VS
2840 if (IS_G4X(dev))
2841 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
2842
2949056c 2843 intel_add_fb_offsets(&x, &y, plane_state, 0);
81255565 2844
6687c906 2845 if (INTEL_INFO(dev)->gen >= 4)
c2c75131 2846 intel_crtc->dspaddr_offset =
2949056c 2847 intel_compute_tile_offset(&x, &y, plane_state, 0);
e506a0c6 2848
8d0deca8 2849 if (rotation == BIT(DRM_ROTATE_180)) {
48404c1e
SJ
2850 dspcntr |= DISPPLANE_ROTATE_180;
2851
a8d201af
ML
2852 x += (crtc_state->pipe_src_w - 1);
2853 y += (crtc_state->pipe_src_h - 1);
48404c1e
SJ
2854 }
2855
2949056c 2856 linear_offset = intel_fb_xy_to_linear(x, y, plane_state, 0);
6687c906
VS
2857
2858 if (INTEL_INFO(dev)->gen < 4)
2859 intel_crtc->dspaddr_offset = linear_offset;
2860
2db3366b
PZ
2861 intel_crtc->adjusted_x = x;
2862 intel_crtc->adjusted_y = y;
2863
48404c1e
SJ
2864 I915_WRITE(reg, dspcntr);
2865
01f2c773 2866 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
a6c45cf0 2867 if (INTEL_INFO(dev)->gen >= 4) {
85ba7b7d 2868 I915_WRITE(DSPSURF(plane),
6687c906
VS
2869 intel_fb_gtt_offset(fb, rotation) +
2870 intel_crtc->dspaddr_offset);
5eddb70b 2871 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
e506a0c6 2872 I915_WRITE(DSPLINOFF(plane), linear_offset);
5eddb70b 2873 } else
f343c5f6 2874 I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
5eddb70b 2875 POSTING_READ(reg);
17638cd6
JB
2876}
2877
a8d201af
ML
2878static void i9xx_disable_primary_plane(struct drm_plane *primary,
2879 struct drm_crtc *crtc)
17638cd6
JB
2880{
2881 struct drm_device *dev = crtc->dev;
fac5e23e 2882 struct drm_i915_private *dev_priv = to_i915(dev);
17638cd6 2883 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
17638cd6 2884 int plane = intel_crtc->plane;
f45651ba 2885
a8d201af
ML
2886 I915_WRITE(DSPCNTR(plane), 0);
2887 if (INTEL_INFO(dev_priv)->gen >= 4)
fdd508a6 2888 I915_WRITE(DSPSURF(plane), 0);
a8d201af
ML
2889 else
2890 I915_WRITE(DSPADDR(plane), 0);
2891 POSTING_READ(DSPCNTR(plane));
2892}
c9ba6fad 2893
a8d201af
ML
2894static void ironlake_update_primary_plane(struct drm_plane *primary,
2895 const struct intel_crtc_state *crtc_state,
2896 const struct intel_plane_state *plane_state)
2897{
2898 struct drm_device *dev = primary->dev;
fac5e23e 2899 struct drm_i915_private *dev_priv = to_i915(dev);
a8d201af
ML
2900 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
2901 struct drm_framebuffer *fb = plane_state->base.fb;
2902 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
2903 int plane = intel_crtc->plane;
54ea9da8 2904 u32 linear_offset;
a8d201af
ML
2905 u32 dspcntr;
2906 i915_reg_t reg = DSPCNTR(plane);
8d0deca8 2907 unsigned int rotation = plane_state->base.rotation;
a8d201af
ML
2908 int x = plane_state->src.x1 >> 16;
2909 int y = plane_state->src.y1 >> 16;
c9ba6fad 2910
f45651ba 2911 dspcntr = DISPPLANE_GAMMA_ENABLE;
fdd508a6 2912 dspcntr |= DISPLAY_PLANE_ENABLE;
f45651ba
VS
2913
2914 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
2915 dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
17638cd6 2916
57779d06
VS
2917 switch (fb->pixel_format) {
2918 case DRM_FORMAT_C8:
17638cd6
JB
2919 dspcntr |= DISPPLANE_8BPP;
2920 break;
57779d06
VS
2921 case DRM_FORMAT_RGB565:
2922 dspcntr |= DISPPLANE_BGRX565;
17638cd6 2923 break;
57779d06 2924 case DRM_FORMAT_XRGB8888:
57779d06
VS
2925 dspcntr |= DISPPLANE_BGRX888;
2926 break;
2927 case DRM_FORMAT_XBGR8888:
57779d06
VS
2928 dspcntr |= DISPPLANE_RGBX888;
2929 break;
2930 case DRM_FORMAT_XRGB2101010:
57779d06
VS
2931 dspcntr |= DISPPLANE_BGRX101010;
2932 break;
2933 case DRM_FORMAT_XBGR2101010:
57779d06 2934 dspcntr |= DISPPLANE_RGBX101010;
17638cd6
JB
2935 break;
2936 default:
baba133a 2937 BUG();
17638cd6
JB
2938 }
2939
3e510a8e 2940 if (i915_gem_object_is_tiled(obj))
17638cd6 2941 dspcntr |= DISPPLANE_TILED;
17638cd6 2942
f45651ba 2943 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev))
1f5d76db 2944 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
17638cd6 2945
2949056c 2946 intel_add_fb_offsets(&x, &y, plane_state, 0);
6687c906 2947
c2c75131 2948 intel_crtc->dspaddr_offset =
2949056c 2949 intel_compute_tile_offset(&x, &y, plane_state, 0);
6687c906 2950
8d0deca8 2951 if (rotation == BIT(DRM_ROTATE_180)) {
48404c1e
SJ
2952 dspcntr |= DISPPLANE_ROTATE_180;
2953
2954 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev)) {
a8d201af
ML
2955 x += (crtc_state->pipe_src_w - 1);
2956 y += (crtc_state->pipe_src_h - 1);
48404c1e
SJ
2957 }
2958 }
2959
2949056c 2960 linear_offset = intel_fb_xy_to_linear(x, y, plane_state, 0);
6687c906 2961
2db3366b
PZ
2962 intel_crtc->adjusted_x = x;
2963 intel_crtc->adjusted_y = y;
2964
48404c1e 2965 I915_WRITE(reg, dspcntr);
17638cd6 2966
01f2c773 2967 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
85ba7b7d 2968 I915_WRITE(DSPSURF(plane),
6687c906
VS
2969 intel_fb_gtt_offset(fb, rotation) +
2970 intel_crtc->dspaddr_offset);
b3dc685e 2971 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
bc1c91eb
DL
2972 I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
2973 } else {
2974 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2975 I915_WRITE(DSPLINOFF(plane), linear_offset);
2976 }
17638cd6 2977 POSTING_READ(reg);
17638cd6
JB
2978}
2979
7b49f948
VS
2980u32 intel_fb_stride_alignment(const struct drm_i915_private *dev_priv,
2981 uint64_t fb_modifier, uint32_t pixel_format)
b321803d 2982{
7b49f948 2983 if (fb_modifier == DRM_FORMAT_MOD_NONE) {
b321803d 2984 return 64;
7b49f948
VS
2985 } else {
2986 int cpp = drm_format_plane_cpp(pixel_format, 0);
2987
27ba3910 2988 return intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
b321803d
DL
2989 }
2990}
2991
6687c906
VS
2992u32 intel_fb_gtt_offset(struct drm_framebuffer *fb,
2993 unsigned int rotation)
121920fa 2994{
6687c906 2995 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
ce7f1728 2996 struct i915_ggtt_view view;
44eb0cb9 2997 u64 offset;
121920fa 2998
6687c906 2999 intel_fill_fb_ggtt_view(&view, fb, rotation);
dedf278c 3000
6687c906 3001 offset = i915_gem_obj_ggtt_offset_view(obj, &view);
dedf278c 3002
44eb0cb9
MK
3003 WARN_ON(upper_32_bits(offset));
3004
3005 return lower_32_bits(offset);
121920fa
TU
3006}
3007
e435d6e5
ML
3008static void skl_detach_scaler(struct intel_crtc *intel_crtc, int id)
3009{
3010 struct drm_device *dev = intel_crtc->base.dev;
fac5e23e 3011 struct drm_i915_private *dev_priv = to_i915(dev);
e435d6e5
ML
3012
3013 I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, id), 0);
3014 I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, id), 0);
3015 I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, id), 0);
e435d6e5
ML
3016}
3017
a1b2278e
CK
3018/*
3019 * This function detaches (aka. unbinds) unused scalers in hardware
3020 */
0583236e 3021static void skl_detach_scalers(struct intel_crtc *intel_crtc)
a1b2278e 3022{
a1b2278e
CK
3023 struct intel_crtc_scaler_state *scaler_state;
3024 int i;
3025
a1b2278e
CK
3026 scaler_state = &intel_crtc->config->scaler_state;
3027
3028 /* loop through and disable scalers that aren't in use */
3029 for (i = 0; i < intel_crtc->num_scalers; i++) {
e435d6e5
ML
3030 if (!scaler_state->scalers[i].in_use)
3031 skl_detach_scaler(intel_crtc, i);
a1b2278e
CK
3032 }
3033}
3034
d2196774
VS
3035u32 skl_plane_stride(const struct drm_framebuffer *fb, int plane,
3036 unsigned int rotation)
3037{
3038 const struct drm_i915_private *dev_priv = to_i915(fb->dev);
3039 u32 stride = intel_fb_pitch(fb, plane, rotation);
3040
3041 /*
3042 * The stride is either expressed as a multiple of 64 bytes chunks for
3043 * linear buffers or in number of tiles for tiled buffers.
3044 */
3045 if (intel_rotation_90_or_270(rotation)) {
3046 int cpp = drm_format_plane_cpp(fb->pixel_format, plane);
3047
3048 stride /= intel_tile_height(dev_priv, fb->modifier[0], cpp);
3049 } else {
3050 stride /= intel_fb_stride_alignment(dev_priv, fb->modifier[0],
3051 fb->pixel_format);
3052 }
3053
3054 return stride;
3055}
3056
6156a456 3057u32 skl_plane_ctl_format(uint32_t pixel_format)
70d21f0e 3058{
6156a456 3059 switch (pixel_format) {
d161cf7a 3060 case DRM_FORMAT_C8:
c34ce3d1 3061 return PLANE_CTL_FORMAT_INDEXED;
70d21f0e 3062 case DRM_FORMAT_RGB565:
c34ce3d1 3063 return PLANE_CTL_FORMAT_RGB_565;
70d21f0e 3064 case DRM_FORMAT_XBGR8888:
c34ce3d1 3065 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
6156a456 3066 case DRM_FORMAT_XRGB8888:
c34ce3d1 3067 return PLANE_CTL_FORMAT_XRGB_8888;
6156a456
CK
3068 /*
3069 * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
3070 * to be already pre-multiplied. We need to add a knob (or a different
3071 * DRM_FORMAT) for user-space to configure that.
3072 */
f75fb42a 3073 case DRM_FORMAT_ABGR8888:
c34ce3d1 3074 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX |
6156a456 3075 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
6156a456 3076 case DRM_FORMAT_ARGB8888:
c34ce3d1 3077 return PLANE_CTL_FORMAT_XRGB_8888 |
6156a456 3078 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
70d21f0e 3079 case DRM_FORMAT_XRGB2101010:
c34ce3d1 3080 return PLANE_CTL_FORMAT_XRGB_2101010;
70d21f0e 3081 case DRM_FORMAT_XBGR2101010:
c34ce3d1 3082 return PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
6156a456 3083 case DRM_FORMAT_YUYV:
c34ce3d1 3084 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
6156a456 3085 case DRM_FORMAT_YVYU:
c34ce3d1 3086 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
6156a456 3087 case DRM_FORMAT_UYVY:
c34ce3d1 3088 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
6156a456 3089 case DRM_FORMAT_VYUY:
c34ce3d1 3090 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
70d21f0e 3091 default:
4249eeef 3092 MISSING_CASE(pixel_format);
70d21f0e 3093 }
8cfcba41 3094
c34ce3d1 3095 return 0;
6156a456 3096}
70d21f0e 3097
6156a456
CK
3098u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
3099{
6156a456 3100 switch (fb_modifier) {
30af77c4 3101 case DRM_FORMAT_MOD_NONE:
70d21f0e 3102 break;
30af77c4 3103 case I915_FORMAT_MOD_X_TILED:
c34ce3d1 3104 return PLANE_CTL_TILED_X;
b321803d 3105 case I915_FORMAT_MOD_Y_TILED:
c34ce3d1 3106 return PLANE_CTL_TILED_Y;
b321803d 3107 case I915_FORMAT_MOD_Yf_TILED:
c34ce3d1 3108 return PLANE_CTL_TILED_YF;
70d21f0e 3109 default:
6156a456 3110 MISSING_CASE(fb_modifier);
70d21f0e 3111 }
8cfcba41 3112
c34ce3d1 3113 return 0;
6156a456 3114}
70d21f0e 3115
6156a456
CK
3116u32 skl_plane_ctl_rotation(unsigned int rotation)
3117{
3b7a5119 3118 switch (rotation) {
6156a456
CK
3119 case BIT(DRM_ROTATE_0):
3120 break;
1e8df167
SJ
3121 /*
3122 * DRM_ROTATE_ is counter clockwise to stay compatible with Xrandr
3123 * while i915 HW rotation is clockwise, thats why this swapping.
3124 */
3b7a5119 3125 case BIT(DRM_ROTATE_90):
1e8df167 3126 return PLANE_CTL_ROTATE_270;
3b7a5119 3127 case BIT(DRM_ROTATE_180):
c34ce3d1 3128 return PLANE_CTL_ROTATE_180;
3b7a5119 3129 case BIT(DRM_ROTATE_270):
1e8df167 3130 return PLANE_CTL_ROTATE_90;
6156a456
CK
3131 default:
3132 MISSING_CASE(rotation);
3133 }
3134
c34ce3d1 3135 return 0;
6156a456
CK
3136}
3137
a8d201af
ML
3138static void skylake_update_primary_plane(struct drm_plane *plane,
3139 const struct intel_crtc_state *crtc_state,
3140 const struct intel_plane_state *plane_state)
6156a456 3141{
a8d201af 3142 struct drm_device *dev = plane->dev;
fac5e23e 3143 struct drm_i915_private *dev_priv = to_i915(dev);
a8d201af
ML
3144 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
3145 struct drm_framebuffer *fb = plane_state->base.fb;
6156a456 3146 int pipe = intel_crtc->pipe;
d2196774 3147 u32 plane_ctl;
a8d201af 3148 unsigned int rotation = plane_state->base.rotation;
d2196774 3149 u32 stride = skl_plane_stride(fb, 0, rotation);
44eb0cb9 3150 u32 surf_addr;
a8d201af
ML
3151 int scaler_id = plane_state->scaler_id;
3152 int src_x = plane_state->src.x1 >> 16;
3153 int src_y = plane_state->src.y1 >> 16;
3154 int src_w = drm_rect_width(&plane_state->src) >> 16;
3155 int src_h = drm_rect_height(&plane_state->src) >> 16;
3156 int dst_x = plane_state->dst.x1;
3157 int dst_y = plane_state->dst.y1;
3158 int dst_w = drm_rect_width(&plane_state->dst);
3159 int dst_h = drm_rect_height(&plane_state->dst);
70d21f0e 3160
6156a456
CK
3161 plane_ctl = PLANE_CTL_ENABLE |
3162 PLANE_CTL_PIPE_GAMMA_ENABLE |
3163 PLANE_CTL_PIPE_CSC_ENABLE;
3164
3165 plane_ctl |= skl_plane_ctl_format(fb->pixel_format);
3166 plane_ctl |= skl_plane_ctl_tiling(fb->modifier[0]);
3167 plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
6156a456
CK
3168 plane_ctl |= skl_plane_ctl_rotation(rotation);
3169
3b7a5119 3170 if (intel_rotation_90_or_270(rotation)) {
6687c906
VS
3171 struct drm_rect r = {
3172 .x1 = src_x,
3173 .x2 = src_x + src_w,
3174 .y1 = src_y,
3175 .y2 = src_y + src_h,
3176 };
6687c906
VS
3177
3178 /* Rotate src coordinates to match rotated GTT view */
3179 drm_rect_rotate(&r, fb->width, fb->height, BIT(DRM_ROTATE_270));
832be82f 3180
6687c906
VS
3181 src_x = r.x1;
3182 src_y = r.y1;
3183 src_w = drm_rect_width(&r);
3184 src_h = drm_rect_height(&r);
3b7a5119 3185 }
b321803d 3186
2949056c
VS
3187 intel_add_fb_offsets(&src_x, &src_y, plane_state, 0);
3188 surf_addr = intel_compute_tile_offset(&src_x, &src_y, plane_state, 0);
6687c906
VS
3189
3190 /* Sizes are 0 based */
3191 src_w--;
3192 src_h--;
3193 dst_w--;
3194 dst_h--;
3195
3196 intel_crtc->adjusted_x = src_x;
3197 intel_crtc->adjusted_y = src_y;
2db3366b 3198
70d21f0e 3199 I915_WRITE(PLANE_CTL(pipe, 0), plane_ctl);
6687c906 3200 I915_WRITE(PLANE_OFFSET(pipe, 0), (src_y << 16) | src_x);
ef78ec94 3201 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
6687c906 3202 I915_WRITE(PLANE_SIZE(pipe, 0), (src_h << 16) | src_w);
6156a456
CK
3203
3204 if (scaler_id >= 0) {
3205 uint32_t ps_ctrl = 0;
3206
3207 WARN_ON(!dst_w || !dst_h);
3208 ps_ctrl = PS_SCALER_EN | PS_PLANE_SEL(0) |
3209 crtc_state->scaler_state.scalers[scaler_id].mode;
3210 I915_WRITE(SKL_PS_CTRL(pipe, scaler_id), ps_ctrl);
3211 I915_WRITE(SKL_PS_PWR_GATE(pipe, scaler_id), 0);
3212 I915_WRITE(SKL_PS_WIN_POS(pipe, scaler_id), (dst_x << 16) | dst_y);
3213 I915_WRITE(SKL_PS_WIN_SZ(pipe, scaler_id), (dst_w << 16) | dst_h);
3214 I915_WRITE(PLANE_POS(pipe, 0), 0);
3215 } else {
3216 I915_WRITE(PLANE_POS(pipe, 0), (dst_y << 16) | dst_x);
3217 }
3218
6687c906
VS
3219 I915_WRITE(PLANE_SURF(pipe, 0),
3220 intel_fb_gtt_offset(fb, rotation) + surf_addr);
70d21f0e
DL
3221
3222 POSTING_READ(PLANE_SURF(pipe, 0));
3223}
3224
a8d201af
ML
3225static void skylake_disable_primary_plane(struct drm_plane *primary,
3226 struct drm_crtc *crtc)
17638cd6
JB
3227{
3228 struct drm_device *dev = crtc->dev;
fac5e23e 3229 struct drm_i915_private *dev_priv = to_i915(dev);
a8d201af 3230 int pipe = to_intel_crtc(crtc)->pipe;
17638cd6 3231
a8d201af
ML
3232 I915_WRITE(PLANE_CTL(pipe, 0), 0);
3233 I915_WRITE(PLANE_SURF(pipe, 0), 0);
3234 POSTING_READ(PLANE_SURF(pipe, 0));
3235}
29b9bde6 3236
a8d201af
ML
3237/* Assume fb object is pinned & idle & fenced and just update base pointers */
3238static int
3239intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
3240 int x, int y, enum mode_set_atomic state)
3241{
3242 /* Support for kgdboc is disabled, this needs a major rework. */
3243 DRM_ERROR("legacy panic handler not supported any more.\n");
3244
3245 return -ENODEV;
81255565
JB
3246}
3247
5a21b665
DV
3248static void intel_complete_page_flips(struct drm_i915_private *dev_priv)
3249{
3250 struct intel_crtc *crtc;
3251
91c8a326 3252 for_each_intel_crtc(&dev_priv->drm, crtc)
5a21b665
DV
3253 intel_finish_page_flip_cs(dev_priv, crtc->pipe);
3254}
3255
7514747d
VS
3256static void intel_update_primary_planes(struct drm_device *dev)
3257{
7514747d 3258 struct drm_crtc *crtc;
96a02917 3259
70e1e0ec 3260 for_each_crtc(dev, crtc) {
11c22da6 3261 struct intel_plane *plane = to_intel_plane(crtc->primary);
73974893
ML
3262 struct intel_plane_state *plane_state =
3263 to_intel_plane_state(plane->base.state);
11c22da6 3264
a8d201af
ML
3265 if (plane_state->visible)
3266 plane->update_plane(&plane->base,
3267 to_intel_crtc_state(crtc->state),
3268 plane_state);
73974893
ML
3269 }
3270}
3271
3272static int
3273__intel_display_resume(struct drm_device *dev,
3274 struct drm_atomic_state *state)
3275{
3276 struct drm_crtc_state *crtc_state;
3277 struct drm_crtc *crtc;
3278 int i, ret;
11c22da6 3279
73974893
ML
3280 intel_modeset_setup_hw_state(dev);
3281 i915_redisable_vga(dev);
3282
3283 if (!state)
3284 return 0;
3285
3286 for_each_crtc_in_state(state, crtc, crtc_state, i) {
3287 /*
3288 * Force recalculation even if we restore
3289 * current state. With fast modeset this may not result
3290 * in a modeset when the state is compatible.
3291 */
3292 crtc_state->mode_changed = true;
96a02917 3293 }
73974893
ML
3294
3295 /* ignore any reset values/BIOS leftovers in the WM registers */
3296 to_intel_atomic_state(state)->skip_intermediate_wm = true;
3297
3298 ret = drm_atomic_commit(state);
3299
3300 WARN_ON(ret == -EDEADLK);
3301 return ret;
96a02917
VS
3302}
3303
4ac2ba2f
VS
3304static bool gpu_reset_clobbers_display(struct drm_i915_private *dev_priv)
3305{
ae98104b
VS
3306 return intel_has_gpu_reset(dev_priv) &&
3307 INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv);
4ac2ba2f
VS
3308}
3309
c033666a 3310void intel_prepare_reset(struct drm_i915_private *dev_priv)
7514747d 3311{
73974893
ML
3312 struct drm_device *dev = &dev_priv->drm;
3313 struct drm_modeset_acquire_ctx *ctx = &dev_priv->reset_ctx;
3314 struct drm_atomic_state *state;
3315 int ret;
3316
73974893
ML
3317 /*
3318 * Need mode_config.mutex so that we don't
3319 * trample ongoing ->detect() and whatnot.
3320 */
3321 mutex_lock(&dev->mode_config.mutex);
3322 drm_modeset_acquire_init(ctx, 0);
3323 while (1) {
3324 ret = drm_modeset_lock_all_ctx(dev, ctx);
3325 if (ret != -EDEADLK)
3326 break;
3327
3328 drm_modeset_backoff(ctx);
3329 }
3330
3331 /* reset doesn't touch the display, but flips might get nuked anyway, */
522a63de 3332 if (!i915.force_reset_modeset_test &&
4ac2ba2f 3333 !gpu_reset_clobbers_display(dev_priv))
7514747d
VS
3334 return;
3335
f98ce92f
VS
3336 /*
3337 * Disabling the crtcs gracefully seems nicer. Also the
3338 * g33 docs say we should at least disable all the planes.
3339 */
73974893
ML
3340 state = drm_atomic_helper_duplicate_state(dev, ctx);
3341 if (IS_ERR(state)) {
3342 ret = PTR_ERR(state);
3343 state = NULL;
3344 DRM_ERROR("Duplicating state failed with %i\n", ret);
3345 goto err;
3346 }
3347
3348 ret = drm_atomic_helper_disable_all(dev, ctx);
3349 if (ret) {
3350 DRM_ERROR("Suspending crtc's failed with %i\n", ret);
3351 goto err;
3352 }
3353
3354 dev_priv->modeset_restore_state = state;
3355 state->acquire_ctx = ctx;
3356 return;
3357
3358err:
3359 drm_atomic_state_free(state);
7514747d
VS
3360}
3361
c033666a 3362void intel_finish_reset(struct drm_i915_private *dev_priv)
7514747d 3363{
73974893
ML
3364 struct drm_device *dev = &dev_priv->drm;
3365 struct drm_modeset_acquire_ctx *ctx = &dev_priv->reset_ctx;
3366 struct drm_atomic_state *state = dev_priv->modeset_restore_state;
3367 int ret;
3368
5a21b665
DV
3369 /*
3370 * Flips in the rings will be nuked by the reset,
3371 * so complete all pending flips so that user space
3372 * will get its events and not get stuck.
3373 */
3374 intel_complete_page_flips(dev_priv);
3375
73974893
ML
3376 dev_priv->modeset_restore_state = NULL;
3377
7514747d 3378 /* reset doesn't touch the display */
4ac2ba2f 3379 if (!gpu_reset_clobbers_display(dev_priv)) {
522a63de
ML
3380 if (!state) {
3381 /*
3382 * Flips in the rings have been nuked by the reset,
3383 * so update the base address of all primary
3384 * planes to the the last fb to make sure we're
3385 * showing the correct fb after a reset.
3386 *
3387 * FIXME: Atomic will make this obsolete since we won't schedule
3388 * CS-based flips (which might get lost in gpu resets) any more.
3389 */
3390 intel_update_primary_planes(dev);
3391 } else {
3392 ret = __intel_display_resume(dev, state);
3393 if (ret)
3394 DRM_ERROR("Restoring old state failed with %i\n", ret);
3395 }
73974893
ML
3396 } else {
3397 /*
3398 * The display has been reset as well,
3399 * so need a full re-initialization.
3400 */
3401 intel_runtime_pm_disable_interrupts(dev_priv);
3402 intel_runtime_pm_enable_interrupts(dev_priv);
7514747d 3403
73974893 3404 intel_modeset_init_hw(dev);
7514747d 3405
73974893
ML
3406 spin_lock_irq(&dev_priv->irq_lock);
3407 if (dev_priv->display.hpd_irq_setup)
3408 dev_priv->display.hpd_irq_setup(dev_priv);
3409 spin_unlock_irq(&dev_priv->irq_lock);
7514747d 3410
73974893
ML
3411 ret = __intel_display_resume(dev, state);
3412 if (ret)
3413 DRM_ERROR("Restoring old state failed with %i\n", ret);
7514747d 3414
73974893
ML
3415 intel_hpd_init(dev_priv);
3416 }
7514747d 3417
73974893
ML
3418 drm_modeset_drop_locks(ctx);
3419 drm_modeset_acquire_fini(ctx);
3420 mutex_unlock(&dev->mode_config.mutex);
7514747d
VS
3421}
3422
7d5e3799
CW
3423static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
3424{
5a21b665
DV
3425 struct drm_device *dev = crtc->dev;
3426 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3427 unsigned reset_counter;
3428 bool pending;
3429
3430 reset_counter = i915_reset_counter(&to_i915(dev)->gpu_error);
3431 if (intel_crtc->reset_counter != reset_counter)
3432 return false;
3433
3434 spin_lock_irq(&dev->event_lock);
3435 pending = to_intel_crtc(crtc)->flip_work != NULL;
3436 spin_unlock_irq(&dev->event_lock);
3437
3438 return pending;
7d5e3799
CW
3439}
3440
bfd16b2a
ML
3441static void intel_update_pipe_config(struct intel_crtc *crtc,
3442 struct intel_crtc_state *old_crtc_state)
e30e8f75
GP
3443{
3444 struct drm_device *dev = crtc->base.dev;
fac5e23e 3445 struct drm_i915_private *dev_priv = to_i915(dev);
bfd16b2a
ML
3446 struct intel_crtc_state *pipe_config =
3447 to_intel_crtc_state(crtc->base.state);
e30e8f75 3448
bfd16b2a
ML
3449 /* drm_atomic_helper_update_legacy_modeset_state might not be called. */
3450 crtc->base.mode = crtc->base.state->mode;
3451
3452 DRM_DEBUG_KMS("Updating pipe size %ix%i -> %ix%i\n",
3453 old_crtc_state->pipe_src_w, old_crtc_state->pipe_src_h,
3454 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
e30e8f75
GP
3455
3456 /*
3457 * Update pipe size and adjust fitter if needed: the reason for this is
3458 * that in compute_mode_changes we check the native mode (not the pfit
3459 * mode) to see if we can flip rather than do a full mode set. In the
3460 * fastboot case, we'll flip, but if we don't update the pipesrc and
3461 * pfit state, we'll end up with a big fb scanned out into the wrong
3462 * sized surface.
e30e8f75
GP
3463 */
3464
e30e8f75 3465 I915_WRITE(PIPESRC(crtc->pipe),
bfd16b2a
ML
3466 ((pipe_config->pipe_src_w - 1) << 16) |
3467 (pipe_config->pipe_src_h - 1));
3468
3469 /* on skylake this is done by detaching scalers */
3470 if (INTEL_INFO(dev)->gen >= 9) {
3471 skl_detach_scalers(crtc);
3472
3473 if (pipe_config->pch_pfit.enabled)
3474 skylake_pfit_enable(crtc);
3475 } else if (HAS_PCH_SPLIT(dev)) {
3476 if (pipe_config->pch_pfit.enabled)
3477 ironlake_pfit_enable(crtc);
3478 else if (old_crtc_state->pch_pfit.enabled)
3479 ironlake_pfit_disable(crtc, true);
e30e8f75 3480 }
e30e8f75
GP
3481}
3482
5e84e1a4
ZW
3483static void intel_fdi_normal_train(struct drm_crtc *crtc)
3484{
3485 struct drm_device *dev = crtc->dev;
fac5e23e 3486 struct drm_i915_private *dev_priv = to_i915(dev);
5e84e1a4
ZW
3487 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3488 int pipe = intel_crtc->pipe;
f0f59a00
VS
3489 i915_reg_t reg;
3490 u32 temp;
5e84e1a4
ZW
3491
3492 /* enable normal train */
3493 reg = FDI_TX_CTL(pipe);
3494 temp = I915_READ(reg);
61e499bf 3495 if (IS_IVYBRIDGE(dev)) {
357555c0
JB
3496 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3497 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
61e499bf
KP
3498 } else {
3499 temp &= ~FDI_LINK_TRAIN_NONE;
3500 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
357555c0 3501 }
5e84e1a4
ZW
3502 I915_WRITE(reg, temp);
3503
3504 reg = FDI_RX_CTL(pipe);
3505 temp = I915_READ(reg);
3506 if (HAS_PCH_CPT(dev)) {
3507 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3508 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
3509 } else {
3510 temp &= ~FDI_LINK_TRAIN_NONE;
3511 temp |= FDI_LINK_TRAIN_NONE;
3512 }
3513 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
3514
3515 /* wait one idle pattern time */
3516 POSTING_READ(reg);
3517 udelay(1000);
357555c0
JB
3518
3519 /* IVB wants error correction enabled */
3520 if (IS_IVYBRIDGE(dev))
3521 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
3522 FDI_FE_ERRC_ENABLE);
5e84e1a4
ZW
3523}
3524
8db9d77b
ZW
3525/* The FDI link training functions for ILK/Ibexpeak. */
3526static void ironlake_fdi_link_train(struct drm_crtc *crtc)
3527{
3528 struct drm_device *dev = crtc->dev;
fac5e23e 3529 struct drm_i915_private *dev_priv = to_i915(dev);
8db9d77b
ZW
3530 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3531 int pipe = intel_crtc->pipe;
f0f59a00
VS
3532 i915_reg_t reg;
3533 u32 temp, tries;
8db9d77b 3534
1c8562f6 3535 /* FDI needs bits from pipe first */
0fc932b8 3536 assert_pipe_enabled(dev_priv, pipe);
0fc932b8 3537
e1a44743
AJ
3538 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3539 for train result */
5eddb70b
CW
3540 reg = FDI_RX_IMR(pipe);
3541 temp = I915_READ(reg);
e1a44743
AJ
3542 temp &= ~FDI_RX_SYMBOL_LOCK;
3543 temp &= ~FDI_RX_BIT_LOCK;
5eddb70b
CW
3544 I915_WRITE(reg, temp);
3545 I915_READ(reg);
e1a44743
AJ
3546 udelay(150);
3547
8db9d77b 3548 /* enable CPU FDI TX and PCH FDI RX */
5eddb70b
CW
3549 reg = FDI_TX_CTL(pipe);
3550 temp = I915_READ(reg);
627eb5a3 3551 temp &= ~FDI_DP_PORT_WIDTH_MASK;
6e3c9717 3552 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
8db9d77b
ZW
3553 temp &= ~FDI_LINK_TRAIN_NONE;
3554 temp |= FDI_LINK_TRAIN_PATTERN_1;
5eddb70b 3555 I915_WRITE(reg, temp | FDI_TX_ENABLE);
8db9d77b 3556
5eddb70b
CW
3557 reg = FDI_RX_CTL(pipe);
3558 temp = I915_READ(reg);
8db9d77b
ZW
3559 temp &= ~FDI_LINK_TRAIN_NONE;
3560 temp |= FDI_LINK_TRAIN_PATTERN_1;
5eddb70b
CW
3561 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3562
3563 POSTING_READ(reg);
8db9d77b
ZW
3564 udelay(150);
3565
5b2adf89 3566 /* Ironlake workaround, enable clock pointer after FDI enable*/
8f5718a6
DV
3567 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
3568 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
3569 FDI_RX_PHASE_SYNC_POINTER_EN);
5b2adf89 3570
5eddb70b 3571 reg = FDI_RX_IIR(pipe);
e1a44743 3572 for (tries = 0; tries < 5; tries++) {
5eddb70b 3573 temp = I915_READ(reg);
8db9d77b
ZW
3574 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3575
3576 if ((temp & FDI_RX_BIT_LOCK)) {
3577 DRM_DEBUG_KMS("FDI train 1 done.\n");
5eddb70b 3578 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
8db9d77b
ZW
3579 break;
3580 }
8db9d77b 3581 }
e1a44743 3582 if (tries == 5)
5eddb70b 3583 DRM_ERROR("FDI train 1 fail!\n");
8db9d77b
ZW
3584
3585 /* Train 2 */
5eddb70b
CW
3586 reg = FDI_TX_CTL(pipe);
3587 temp = I915_READ(reg);
8db9d77b
ZW
3588 temp &= ~FDI_LINK_TRAIN_NONE;
3589 temp |= FDI_LINK_TRAIN_PATTERN_2;
5eddb70b 3590 I915_WRITE(reg, temp);
8db9d77b 3591
5eddb70b
CW
3592 reg = FDI_RX_CTL(pipe);
3593 temp = I915_READ(reg);
8db9d77b
ZW
3594 temp &= ~FDI_LINK_TRAIN_NONE;
3595 temp |= FDI_LINK_TRAIN_PATTERN_2;
5eddb70b 3596 I915_WRITE(reg, temp);
8db9d77b 3597
5eddb70b
CW
3598 POSTING_READ(reg);
3599 udelay(150);
8db9d77b 3600
5eddb70b 3601 reg = FDI_RX_IIR(pipe);
e1a44743 3602 for (tries = 0; tries < 5; tries++) {
5eddb70b 3603 temp = I915_READ(reg);
8db9d77b
ZW
3604 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3605
3606 if (temp & FDI_RX_SYMBOL_LOCK) {
5eddb70b 3607 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
8db9d77b
ZW
3608 DRM_DEBUG_KMS("FDI train 2 done.\n");
3609 break;
3610 }
8db9d77b 3611 }
e1a44743 3612 if (tries == 5)
5eddb70b 3613 DRM_ERROR("FDI train 2 fail!\n");
8db9d77b
ZW
3614
3615 DRM_DEBUG_KMS("FDI train done\n");
5c5313c8 3616
8db9d77b
ZW
3617}
3618
0206e353 3619static const int snb_b_fdi_train_param[] = {
8db9d77b
ZW
3620 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
3621 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
3622 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
3623 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
3624};
3625
3626/* The FDI link training functions for SNB/Cougarpoint. */
3627static void gen6_fdi_link_train(struct drm_crtc *crtc)
3628{
3629 struct drm_device *dev = crtc->dev;
fac5e23e 3630 struct drm_i915_private *dev_priv = to_i915(dev);
8db9d77b
ZW
3631 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3632 int pipe = intel_crtc->pipe;
f0f59a00
VS
3633 i915_reg_t reg;
3634 u32 temp, i, retry;
8db9d77b 3635
e1a44743
AJ
3636 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3637 for train result */
5eddb70b
CW
3638 reg = FDI_RX_IMR(pipe);
3639 temp = I915_READ(reg);
e1a44743
AJ
3640 temp &= ~FDI_RX_SYMBOL_LOCK;
3641 temp &= ~FDI_RX_BIT_LOCK;
5eddb70b
CW
3642 I915_WRITE(reg, temp);
3643
3644 POSTING_READ(reg);
e1a44743
AJ
3645 udelay(150);
3646
8db9d77b 3647 /* enable CPU FDI TX and PCH FDI RX */
5eddb70b
CW
3648 reg = FDI_TX_CTL(pipe);
3649 temp = I915_READ(reg);
627eb5a3 3650 temp &= ~FDI_DP_PORT_WIDTH_MASK;
6e3c9717 3651 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
8db9d77b
ZW
3652 temp &= ~FDI_LINK_TRAIN_NONE;
3653 temp |= FDI_LINK_TRAIN_PATTERN_1;
3654 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3655 /* SNB-B */
3656 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
5eddb70b 3657 I915_WRITE(reg, temp | FDI_TX_ENABLE);
8db9d77b 3658
d74cf324
DV
3659 I915_WRITE(FDI_RX_MISC(pipe),
3660 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3661
5eddb70b
CW
3662 reg = FDI_RX_CTL(pipe);
3663 temp = I915_READ(reg);
8db9d77b
ZW
3664 if (HAS_PCH_CPT(dev)) {
3665 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3666 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3667 } else {
3668 temp &= ~FDI_LINK_TRAIN_NONE;
3669 temp |= FDI_LINK_TRAIN_PATTERN_1;
3670 }
5eddb70b
CW
3671 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3672
3673 POSTING_READ(reg);
8db9d77b
ZW
3674 udelay(150);
3675
0206e353 3676 for (i = 0; i < 4; i++) {
5eddb70b
CW
3677 reg = FDI_TX_CTL(pipe);
3678 temp = I915_READ(reg);
8db9d77b
ZW
3679 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3680 temp |= snb_b_fdi_train_param[i];
5eddb70b
CW
3681 I915_WRITE(reg, temp);
3682
3683 POSTING_READ(reg);
8db9d77b
ZW
3684 udelay(500);
3685
fa37d39e
SP
3686 for (retry = 0; retry < 5; retry++) {
3687 reg = FDI_RX_IIR(pipe);
3688 temp = I915_READ(reg);
3689 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3690 if (temp & FDI_RX_BIT_LOCK) {
3691 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3692 DRM_DEBUG_KMS("FDI train 1 done.\n");
3693 break;
3694 }
3695 udelay(50);
8db9d77b 3696 }
fa37d39e
SP
3697 if (retry < 5)
3698 break;
8db9d77b
ZW
3699 }
3700 if (i == 4)
5eddb70b 3701 DRM_ERROR("FDI train 1 fail!\n");
8db9d77b
ZW
3702
3703 /* Train 2 */
5eddb70b
CW
3704 reg = FDI_TX_CTL(pipe);
3705 temp = I915_READ(reg);
8db9d77b
ZW
3706 temp &= ~FDI_LINK_TRAIN_NONE;
3707 temp |= FDI_LINK_TRAIN_PATTERN_2;
3708 if (IS_GEN6(dev)) {
3709 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3710 /* SNB-B */
3711 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
3712 }
5eddb70b 3713 I915_WRITE(reg, temp);
8db9d77b 3714
5eddb70b
CW
3715 reg = FDI_RX_CTL(pipe);
3716 temp = I915_READ(reg);
8db9d77b
ZW
3717 if (HAS_PCH_CPT(dev)) {
3718 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3719 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
3720 } else {
3721 temp &= ~FDI_LINK_TRAIN_NONE;
3722 temp |= FDI_LINK_TRAIN_PATTERN_2;
3723 }
5eddb70b
CW
3724 I915_WRITE(reg, temp);
3725
3726 POSTING_READ(reg);
8db9d77b
ZW
3727 udelay(150);
3728
0206e353 3729 for (i = 0; i < 4; i++) {
5eddb70b
CW
3730 reg = FDI_TX_CTL(pipe);
3731 temp = I915_READ(reg);
8db9d77b
ZW
3732 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3733 temp |= snb_b_fdi_train_param[i];
5eddb70b
CW
3734 I915_WRITE(reg, temp);
3735
3736 POSTING_READ(reg);
8db9d77b
ZW
3737 udelay(500);
3738
fa37d39e
SP
3739 for (retry = 0; retry < 5; retry++) {
3740 reg = FDI_RX_IIR(pipe);
3741 temp = I915_READ(reg);
3742 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3743 if (temp & FDI_RX_SYMBOL_LOCK) {
3744 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3745 DRM_DEBUG_KMS("FDI train 2 done.\n");
3746 break;
3747 }
3748 udelay(50);
8db9d77b 3749 }
fa37d39e
SP
3750 if (retry < 5)
3751 break;
8db9d77b
ZW
3752 }
3753 if (i == 4)
5eddb70b 3754 DRM_ERROR("FDI train 2 fail!\n");
8db9d77b
ZW
3755
3756 DRM_DEBUG_KMS("FDI train done.\n");
3757}
3758
357555c0
JB
3759/* Manual link training for Ivy Bridge A0 parts */
3760static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
3761{
3762 struct drm_device *dev = crtc->dev;
fac5e23e 3763 struct drm_i915_private *dev_priv = to_i915(dev);
357555c0
JB
3764 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3765 int pipe = intel_crtc->pipe;
f0f59a00
VS
3766 i915_reg_t reg;
3767 u32 temp, i, j;
357555c0
JB
3768
3769 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3770 for train result */
3771 reg = FDI_RX_IMR(pipe);
3772 temp = I915_READ(reg);
3773 temp &= ~FDI_RX_SYMBOL_LOCK;
3774 temp &= ~FDI_RX_BIT_LOCK;
3775 I915_WRITE(reg, temp);
3776
3777 POSTING_READ(reg);
3778 udelay(150);
3779
01a415fd
DV
3780 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
3781 I915_READ(FDI_RX_IIR(pipe)));
3782
139ccd3f
JB
3783 /* Try each vswing and preemphasis setting twice before moving on */
3784 for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
3785 /* disable first in case we need to retry */
3786 reg = FDI_TX_CTL(pipe);
3787 temp = I915_READ(reg);
3788 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
3789 temp &= ~FDI_TX_ENABLE;
3790 I915_WRITE(reg, temp);
357555c0 3791
139ccd3f
JB
3792 reg = FDI_RX_CTL(pipe);
3793 temp = I915_READ(reg);
3794 temp &= ~FDI_LINK_TRAIN_AUTO;
3795 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3796 temp &= ~FDI_RX_ENABLE;
3797 I915_WRITE(reg, temp);
357555c0 3798
139ccd3f 3799 /* enable CPU FDI TX and PCH FDI RX */
357555c0
JB
3800 reg = FDI_TX_CTL(pipe);
3801 temp = I915_READ(reg);
139ccd3f 3802 temp &= ~FDI_DP_PORT_WIDTH_MASK;
6e3c9717 3803 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
139ccd3f 3804 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
357555c0 3805 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
139ccd3f
JB
3806 temp |= snb_b_fdi_train_param[j/2];
3807 temp |= FDI_COMPOSITE_SYNC;
3808 I915_WRITE(reg, temp | FDI_TX_ENABLE);
357555c0 3809
139ccd3f
JB
3810 I915_WRITE(FDI_RX_MISC(pipe),
3811 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
357555c0 3812
139ccd3f 3813 reg = FDI_RX_CTL(pipe);
357555c0 3814 temp = I915_READ(reg);
139ccd3f
JB
3815 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3816 temp |= FDI_COMPOSITE_SYNC;
3817 I915_WRITE(reg, temp | FDI_RX_ENABLE);
357555c0 3818
139ccd3f
JB
3819 POSTING_READ(reg);
3820 udelay(1); /* should be 0.5us */
357555c0 3821
139ccd3f
JB
3822 for (i = 0; i < 4; i++) {
3823 reg = FDI_RX_IIR(pipe);
3824 temp = I915_READ(reg);
3825 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
357555c0 3826
139ccd3f
JB
3827 if (temp & FDI_RX_BIT_LOCK ||
3828 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
3829 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3830 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
3831 i);
3832 break;
3833 }
3834 udelay(1); /* should be 0.5us */
3835 }
3836 if (i == 4) {
3837 DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
3838 continue;
3839 }
357555c0 3840
139ccd3f 3841 /* Train 2 */
357555c0
JB
3842 reg = FDI_TX_CTL(pipe);
3843 temp = I915_READ(reg);
139ccd3f
JB
3844 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3845 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
3846 I915_WRITE(reg, temp);
3847
3848 reg = FDI_RX_CTL(pipe);
3849 temp = I915_READ(reg);
3850 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3851 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
357555c0
JB
3852 I915_WRITE(reg, temp);
3853
3854 POSTING_READ(reg);
139ccd3f 3855 udelay(2); /* should be 1.5us */
357555c0 3856
139ccd3f
JB
3857 for (i = 0; i < 4; i++) {
3858 reg = FDI_RX_IIR(pipe);
3859 temp = I915_READ(reg);
3860 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
357555c0 3861
139ccd3f
JB
3862 if (temp & FDI_RX_SYMBOL_LOCK ||
3863 (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
3864 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3865 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
3866 i);
3867 goto train_done;
3868 }
3869 udelay(2); /* should be 1.5us */
357555c0 3870 }
139ccd3f
JB
3871 if (i == 4)
3872 DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
357555c0 3873 }
357555c0 3874
139ccd3f 3875train_done:
357555c0
JB
3876 DRM_DEBUG_KMS("FDI train done.\n");
3877}
3878
88cefb6c 3879static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
2c07245f 3880{
88cefb6c 3881 struct drm_device *dev = intel_crtc->base.dev;
fac5e23e 3882 struct drm_i915_private *dev_priv = to_i915(dev);
2c07245f 3883 int pipe = intel_crtc->pipe;
f0f59a00
VS
3884 i915_reg_t reg;
3885 u32 temp;
c64e311e 3886
c98e9dcf 3887 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
5eddb70b
CW
3888 reg = FDI_RX_CTL(pipe);
3889 temp = I915_READ(reg);
627eb5a3 3890 temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
6e3c9717 3891 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
dfd07d72 3892 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
5eddb70b
CW
3893 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
3894
3895 POSTING_READ(reg);
c98e9dcf
JB
3896 udelay(200);
3897
3898 /* Switch from Rawclk to PCDclk */
5eddb70b
CW
3899 temp = I915_READ(reg);
3900 I915_WRITE(reg, temp | FDI_PCDCLK);
3901
3902 POSTING_READ(reg);
c98e9dcf
JB
3903 udelay(200);
3904
20749730
PZ
3905 /* Enable CPU FDI TX PLL, always on for Ironlake */
3906 reg = FDI_TX_CTL(pipe);
3907 temp = I915_READ(reg);
3908 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
3909 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
5eddb70b 3910
20749730
PZ
3911 POSTING_READ(reg);
3912 udelay(100);
6be4a607 3913 }
0e23b99d
JB
3914}
3915
88cefb6c
DV
3916static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
3917{
3918 struct drm_device *dev = intel_crtc->base.dev;
fac5e23e 3919 struct drm_i915_private *dev_priv = to_i915(dev);
88cefb6c 3920 int pipe = intel_crtc->pipe;
f0f59a00
VS
3921 i915_reg_t reg;
3922 u32 temp;
88cefb6c
DV
3923
3924 /* Switch from PCDclk to Rawclk */
3925 reg = FDI_RX_CTL(pipe);
3926 temp = I915_READ(reg);
3927 I915_WRITE(reg, temp & ~FDI_PCDCLK);
3928
3929 /* Disable CPU FDI TX PLL */
3930 reg = FDI_TX_CTL(pipe);
3931 temp = I915_READ(reg);
3932 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
3933
3934 POSTING_READ(reg);
3935 udelay(100);
3936
3937 reg = FDI_RX_CTL(pipe);
3938 temp = I915_READ(reg);
3939 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
3940
3941 /* Wait for the clocks to turn off. */
3942 POSTING_READ(reg);
3943 udelay(100);
3944}
3945
0fc932b8
JB
3946static void ironlake_fdi_disable(struct drm_crtc *crtc)
3947{
3948 struct drm_device *dev = crtc->dev;
fac5e23e 3949 struct drm_i915_private *dev_priv = to_i915(dev);
0fc932b8
JB
3950 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3951 int pipe = intel_crtc->pipe;
f0f59a00
VS
3952 i915_reg_t reg;
3953 u32 temp;
0fc932b8
JB
3954
3955 /* disable CPU FDI tx and PCH FDI rx */
3956 reg = FDI_TX_CTL(pipe);
3957 temp = I915_READ(reg);
3958 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
3959 POSTING_READ(reg);
3960
3961 reg = FDI_RX_CTL(pipe);
3962 temp = I915_READ(reg);
3963 temp &= ~(0x7 << 16);
dfd07d72 3964 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
0fc932b8
JB
3965 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
3966
3967 POSTING_READ(reg);
3968 udelay(100);
3969
3970 /* Ironlake workaround, disable clock pointer after downing FDI */
eba905b2 3971 if (HAS_PCH_IBX(dev))
6f06ce18 3972 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
0fc932b8
JB
3973
3974 /* still set train pattern 1 */
3975 reg = FDI_TX_CTL(pipe);
3976 temp = I915_READ(reg);
3977 temp &= ~FDI_LINK_TRAIN_NONE;
3978 temp |= FDI_LINK_TRAIN_PATTERN_1;
3979 I915_WRITE(reg, temp);
3980
3981 reg = FDI_RX_CTL(pipe);
3982 temp = I915_READ(reg);
3983 if (HAS_PCH_CPT(dev)) {
3984 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3985 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3986 } else {
3987 temp &= ~FDI_LINK_TRAIN_NONE;
3988 temp |= FDI_LINK_TRAIN_PATTERN_1;
3989 }
3990 /* BPC in FDI rx is consistent with that in PIPECONF */
3991 temp &= ~(0x07 << 16);
dfd07d72 3992 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
0fc932b8
JB
3993 I915_WRITE(reg, temp);
3994
3995 POSTING_READ(reg);
3996 udelay(100);
3997}
3998
5dce5b93
CW
3999bool intel_has_pending_fb_unpin(struct drm_device *dev)
4000{
4001 struct intel_crtc *crtc;
4002
4003 /* Note that we don't need to be called with mode_config.lock here
4004 * as our list of CRTC objects is static for the lifetime of the
4005 * device and so cannot disappear as we iterate. Similarly, we can
4006 * happily treat the predicates as racy, atomic checks as userspace
4007 * cannot claim and pin a new fb without at least acquring the
4008 * struct_mutex and so serialising with us.
4009 */
d3fcc808 4010 for_each_intel_crtc(dev, crtc) {
5dce5b93
CW
4011 if (atomic_read(&crtc->unpin_work_count) == 0)
4012 continue;
4013
5a21b665 4014 if (crtc->flip_work)
5dce5b93
CW
4015 intel_wait_for_vblank(dev, crtc->pipe);
4016
4017 return true;
4018 }
4019
4020 return false;
4021}
4022
5a21b665 4023static void page_flip_completed(struct intel_crtc *intel_crtc)
d6bbafa1
CW
4024{
4025 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
5a21b665
DV
4026 struct intel_flip_work *work = intel_crtc->flip_work;
4027
4028 intel_crtc->flip_work = NULL;
d6bbafa1
CW
4029
4030 if (work->event)
560ce1dc 4031 drm_crtc_send_vblank_event(&intel_crtc->base, work->event);
d6bbafa1
CW
4032
4033 drm_crtc_vblank_put(&intel_crtc->base);
4034
5a21b665 4035 wake_up_all(&dev_priv->pending_flip_queue);
143f73b3 4036 queue_work(dev_priv->wq, &work->unpin_work);
5a21b665
DV
4037
4038 trace_i915_flip_complete(intel_crtc->plane,
4039 work->pending_flip_obj);
d6bbafa1
CW
4040}
4041
5008e874 4042static int intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
e6c3a2a6 4043{
0f91128d 4044 struct drm_device *dev = crtc->dev;
fac5e23e 4045 struct drm_i915_private *dev_priv = to_i915(dev);
5008e874 4046 long ret;
e6c3a2a6 4047
2c10d571 4048 WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
5008e874
ML
4049
4050 ret = wait_event_interruptible_timeout(
4051 dev_priv->pending_flip_queue,
4052 !intel_crtc_has_pending_flip(crtc),
4053 60*HZ);
4054
4055 if (ret < 0)
4056 return ret;
4057
5a21b665
DV
4058 if (ret == 0) {
4059 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4060 struct intel_flip_work *work;
4061
4062 spin_lock_irq(&dev->event_lock);
4063 work = intel_crtc->flip_work;
4064 if (work && !is_mmio_work(work)) {
4065 WARN_ONCE(1, "Removing stuck page flip\n");
4066 page_flip_completed(intel_crtc);
4067 }
4068 spin_unlock_irq(&dev->event_lock);
4069 }
5bb61643 4070
5008e874 4071 return 0;
e6c3a2a6
CW
4072}
4073
060f02d8
VS
4074static void lpt_disable_iclkip(struct drm_i915_private *dev_priv)
4075{
4076 u32 temp;
4077
4078 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
4079
4080 mutex_lock(&dev_priv->sb_lock);
4081
4082 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
4083 temp |= SBI_SSCCTL_DISABLE;
4084 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
4085
4086 mutex_unlock(&dev_priv->sb_lock);
4087}
4088
e615efe4
ED
4089/* Program iCLKIP clock to the desired frequency */
4090static void lpt_program_iclkip(struct drm_crtc *crtc)
4091{
64b46a06 4092 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
6e3c9717 4093 int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
e615efe4
ED
4094 u32 divsel, phaseinc, auxdiv, phasedir = 0;
4095 u32 temp;
4096
060f02d8 4097 lpt_disable_iclkip(dev_priv);
e615efe4 4098
64b46a06
VS
4099 /* The iCLK virtual clock root frequency is in MHz,
4100 * but the adjusted_mode->crtc_clock in in KHz. To get the
4101 * divisors, it is necessary to divide one by another, so we
4102 * convert the virtual clock precision to KHz here for higher
4103 * precision.
4104 */
4105 for (auxdiv = 0; auxdiv < 2; auxdiv++) {
e615efe4
ED
4106 u32 iclk_virtual_root_freq = 172800 * 1000;
4107 u32 iclk_pi_range = 64;
64b46a06 4108 u32 desired_divisor;
e615efe4 4109
64b46a06
VS
4110 desired_divisor = DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
4111 clock << auxdiv);
4112 divsel = (desired_divisor / iclk_pi_range) - 2;
4113 phaseinc = desired_divisor % iclk_pi_range;
e615efe4 4114
64b46a06
VS
4115 /*
4116 * Near 20MHz is a corner case which is
4117 * out of range for the 7-bit divisor
4118 */
4119 if (divsel <= 0x7f)
4120 break;
e615efe4
ED
4121 }
4122
4123 /* This should not happen with any sane values */
4124 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
4125 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
4126 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
4127 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
4128
4129 DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
12d7ceed 4130 clock,
e615efe4
ED
4131 auxdiv,
4132 divsel,
4133 phasedir,
4134 phaseinc);
4135
060f02d8
VS
4136 mutex_lock(&dev_priv->sb_lock);
4137
e615efe4 4138 /* Program SSCDIVINTPHASE6 */
988d6ee8 4139 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
e615efe4
ED
4140 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
4141 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
4142 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
4143 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
4144 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
4145 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
988d6ee8 4146 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
e615efe4
ED
4147
4148 /* Program SSCAUXDIV */
988d6ee8 4149 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
e615efe4
ED
4150 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
4151 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
988d6ee8 4152 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
e615efe4
ED
4153
4154 /* Enable modulator and associated divider */
988d6ee8 4155 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
e615efe4 4156 temp &= ~SBI_SSCCTL_DISABLE;
988d6ee8 4157 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
e615efe4 4158
060f02d8
VS
4159 mutex_unlock(&dev_priv->sb_lock);
4160
e615efe4
ED
4161 /* Wait for initialization time */
4162 udelay(24);
4163
4164 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
4165}
4166
8802e5b6
VS
4167int lpt_get_iclkip(struct drm_i915_private *dev_priv)
4168{
4169 u32 divsel, phaseinc, auxdiv;
4170 u32 iclk_virtual_root_freq = 172800 * 1000;
4171 u32 iclk_pi_range = 64;
4172 u32 desired_divisor;
4173 u32 temp;
4174
4175 if ((I915_READ(PIXCLK_GATE) & PIXCLK_GATE_UNGATE) == 0)
4176 return 0;
4177
4178 mutex_lock(&dev_priv->sb_lock);
4179
4180 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
4181 if (temp & SBI_SSCCTL_DISABLE) {
4182 mutex_unlock(&dev_priv->sb_lock);
4183 return 0;
4184 }
4185
4186 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
4187 divsel = (temp & SBI_SSCDIVINTPHASE_DIVSEL_MASK) >>
4188 SBI_SSCDIVINTPHASE_DIVSEL_SHIFT;
4189 phaseinc = (temp & SBI_SSCDIVINTPHASE_INCVAL_MASK) >>
4190 SBI_SSCDIVINTPHASE_INCVAL_SHIFT;
4191
4192 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
4193 auxdiv = (temp & SBI_SSCAUXDIV_FINALDIV2SEL_MASK) >>
4194 SBI_SSCAUXDIV_FINALDIV2SEL_SHIFT;
4195
4196 mutex_unlock(&dev_priv->sb_lock);
4197
4198 desired_divisor = (divsel + 2) * iclk_pi_range + phaseinc;
4199
4200 return DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
4201 desired_divisor << auxdiv);
4202}
4203
275f01b2
DV
4204static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
4205 enum pipe pch_transcoder)
4206{
4207 struct drm_device *dev = crtc->base.dev;
fac5e23e 4208 struct drm_i915_private *dev_priv = to_i915(dev);
6e3c9717 4209 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
275f01b2
DV
4210
4211 I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
4212 I915_READ(HTOTAL(cpu_transcoder)));
4213 I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
4214 I915_READ(HBLANK(cpu_transcoder)));
4215 I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
4216 I915_READ(HSYNC(cpu_transcoder)));
4217
4218 I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
4219 I915_READ(VTOTAL(cpu_transcoder)));
4220 I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
4221 I915_READ(VBLANK(cpu_transcoder)));
4222 I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
4223 I915_READ(VSYNC(cpu_transcoder)));
4224 I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
4225 I915_READ(VSYNCSHIFT(cpu_transcoder)));
4226}
4227
003632d9 4228static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
1fbc0d78 4229{
fac5e23e 4230 struct drm_i915_private *dev_priv = to_i915(dev);
1fbc0d78
DV
4231 uint32_t temp;
4232
4233 temp = I915_READ(SOUTH_CHICKEN1);
003632d9 4234 if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
1fbc0d78
DV
4235 return;
4236
4237 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
4238 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
4239
003632d9
ACO
4240 temp &= ~FDI_BC_BIFURCATION_SELECT;
4241 if (enable)
4242 temp |= FDI_BC_BIFURCATION_SELECT;
4243
4244 DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
1fbc0d78
DV
4245 I915_WRITE(SOUTH_CHICKEN1, temp);
4246 POSTING_READ(SOUTH_CHICKEN1);
4247}
4248
4249static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
4250{
4251 struct drm_device *dev = intel_crtc->base.dev;
1fbc0d78
DV
4252
4253 switch (intel_crtc->pipe) {
4254 case PIPE_A:
4255 break;
4256 case PIPE_B:
6e3c9717 4257 if (intel_crtc->config->fdi_lanes > 2)
003632d9 4258 cpt_set_fdi_bc_bifurcation(dev, false);
1fbc0d78 4259 else
003632d9 4260 cpt_set_fdi_bc_bifurcation(dev, true);
1fbc0d78
DV
4261
4262 break;
4263 case PIPE_C:
003632d9 4264 cpt_set_fdi_bc_bifurcation(dev, true);
1fbc0d78
DV
4265
4266 break;
4267 default:
4268 BUG();
4269 }
4270}
4271
c48b5305
VS
4272/* Return which DP Port should be selected for Transcoder DP control */
4273static enum port
4274intel_trans_dp_port_sel(struct drm_crtc *crtc)
4275{
4276 struct drm_device *dev = crtc->dev;
4277 struct intel_encoder *encoder;
4278
4279 for_each_encoder_on_crtc(dev, crtc, encoder) {
cca0502b 4280 if (encoder->type == INTEL_OUTPUT_DP ||
c48b5305
VS
4281 encoder->type == INTEL_OUTPUT_EDP)
4282 return enc_to_dig_port(&encoder->base)->port;
4283 }
4284
4285 return -1;
4286}
4287
f67a559d
JB
4288/*
4289 * Enable PCH resources required for PCH ports:
4290 * - PCH PLLs
4291 * - FDI training & RX/TX
4292 * - update transcoder timings
4293 * - DP transcoding bits
4294 * - transcoder
4295 */
4296static void ironlake_pch_enable(struct drm_crtc *crtc)
0e23b99d
JB
4297{
4298 struct drm_device *dev = crtc->dev;
fac5e23e 4299 struct drm_i915_private *dev_priv = to_i915(dev);
0e23b99d
JB
4300 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4301 int pipe = intel_crtc->pipe;
f0f59a00 4302 u32 temp;
2c07245f 4303
ab9412ba 4304 assert_pch_transcoder_disabled(dev_priv, pipe);
e7e164db 4305
1fbc0d78
DV
4306 if (IS_IVYBRIDGE(dev))
4307 ivybridge_update_fdi_bc_bifurcation(intel_crtc);
4308
cd986abb
DV
4309 /* Write the TU size bits before fdi link training, so that error
4310 * detection works. */
4311 I915_WRITE(FDI_RX_TUSIZE1(pipe),
4312 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
4313
c98e9dcf 4314 /* For PCH output, training FDI link */
674cf967 4315 dev_priv->display.fdi_link_train(crtc);
2c07245f 4316
3ad8a208
DV
4317 /* We need to program the right clock selection before writing the pixel
4318 * mutliplier into the DPLL. */
303b81e0 4319 if (HAS_PCH_CPT(dev)) {
ee7b9f93 4320 u32 sel;
4b645f14 4321
c98e9dcf 4322 temp = I915_READ(PCH_DPLL_SEL);
11887397
DV
4323 temp |= TRANS_DPLL_ENABLE(pipe);
4324 sel = TRANS_DPLLB_SEL(pipe);
8106ddbd
ACO
4325 if (intel_crtc->config->shared_dpll ==
4326 intel_get_shared_dpll_by_id(dev_priv, DPLL_ID_PCH_PLL_B))
ee7b9f93
JB
4327 temp |= sel;
4328 else
4329 temp &= ~sel;
c98e9dcf 4330 I915_WRITE(PCH_DPLL_SEL, temp);
c98e9dcf 4331 }
5eddb70b 4332
3ad8a208
DV
4333 /* XXX: pch pll's can be enabled any time before we enable the PCH
4334 * transcoder, and we actually should do this to not upset any PCH
4335 * transcoder that already use the clock when we share it.
4336 *
4337 * Note that enable_shared_dpll tries to do the right thing, but
4338 * get_shared_dpll unconditionally resets the pll - we need that to have
4339 * the right LVDS enable sequence. */
85b3894f 4340 intel_enable_shared_dpll(intel_crtc);
3ad8a208 4341
d9b6cb56
JB
4342 /* set transcoder timing, panel must allow it */
4343 assert_panel_unlocked(dev_priv, pipe);
275f01b2 4344 ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
8db9d77b 4345
303b81e0 4346 intel_fdi_normal_train(crtc);
5e84e1a4 4347
c98e9dcf 4348 /* For PCH DP, enable TRANS_DP_CTL */
37a5650b 4349 if (HAS_PCH_CPT(dev) && intel_crtc_has_dp_encoder(intel_crtc->config)) {
9c4edaee
VS
4350 const struct drm_display_mode *adjusted_mode =
4351 &intel_crtc->config->base.adjusted_mode;
dfd07d72 4352 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
f0f59a00 4353 i915_reg_t reg = TRANS_DP_CTL(pipe);
5eddb70b
CW
4354 temp = I915_READ(reg);
4355 temp &= ~(TRANS_DP_PORT_SEL_MASK |
220cad3c
EA
4356 TRANS_DP_SYNC_MASK |
4357 TRANS_DP_BPC_MASK);
e3ef4479 4358 temp |= TRANS_DP_OUTPUT_ENABLE;
9325c9f0 4359 temp |= bpc << 9; /* same format but at 11:9 */
c98e9dcf 4360
9c4edaee 4361 if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
5eddb70b 4362 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
9c4edaee 4363 if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
5eddb70b 4364 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
c98e9dcf
JB
4365
4366 switch (intel_trans_dp_port_sel(crtc)) {
c48b5305 4367 case PORT_B:
5eddb70b 4368 temp |= TRANS_DP_PORT_SEL_B;
c98e9dcf 4369 break;
c48b5305 4370 case PORT_C:
5eddb70b 4371 temp |= TRANS_DP_PORT_SEL_C;
c98e9dcf 4372 break;
c48b5305 4373 case PORT_D:
5eddb70b 4374 temp |= TRANS_DP_PORT_SEL_D;
c98e9dcf
JB
4375 break;
4376 default:
e95d41e1 4377 BUG();
32f9d658 4378 }
2c07245f 4379
5eddb70b 4380 I915_WRITE(reg, temp);
6be4a607 4381 }
b52eb4dc 4382
b8a4f404 4383 ironlake_enable_pch_transcoder(dev_priv, pipe);
f67a559d
JB
4384}
4385
1507e5bd
PZ
4386static void lpt_pch_enable(struct drm_crtc *crtc)
4387{
4388 struct drm_device *dev = crtc->dev;
fac5e23e 4389 struct drm_i915_private *dev_priv = to_i915(dev);
1507e5bd 4390 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6e3c9717 4391 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
1507e5bd 4392
ab9412ba 4393 assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
1507e5bd 4394
8c52b5e8 4395 lpt_program_iclkip(crtc);
1507e5bd 4396
0540e488 4397 /* Set transcoder timing. */
275f01b2 4398 ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
1507e5bd 4399
937bb610 4400 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
f67a559d
JB
4401}
4402
a1520318 4403static void cpt_verify_modeset(struct drm_device *dev, int pipe)
d4270e57 4404{
fac5e23e 4405 struct drm_i915_private *dev_priv = to_i915(dev);
f0f59a00 4406 i915_reg_t dslreg = PIPEDSL(pipe);
d4270e57
JB
4407 u32 temp;
4408
4409 temp = I915_READ(dslreg);
4410 udelay(500);
4411 if (wait_for(I915_READ(dslreg) != temp, 5)) {
d4270e57 4412 if (wait_for(I915_READ(dslreg) != temp, 5))
84f44ce7 4413 DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
d4270e57
JB
4414 }
4415}
4416
86adf9d7
ML
4417static int
4418skl_update_scaler(struct intel_crtc_state *crtc_state, bool force_detach,
4419 unsigned scaler_user, int *scaler_id, unsigned int rotation,
4420 int src_w, int src_h, int dst_w, int dst_h)
a1b2278e 4421{
86adf9d7
ML
4422 struct intel_crtc_scaler_state *scaler_state =
4423 &crtc_state->scaler_state;
4424 struct intel_crtc *intel_crtc =
4425 to_intel_crtc(crtc_state->base.crtc);
a1b2278e 4426 int need_scaling;
6156a456
CK
4427
4428 need_scaling = intel_rotation_90_or_270(rotation) ?
4429 (src_h != dst_w || src_w != dst_h):
4430 (src_w != dst_w || src_h != dst_h);
a1b2278e
CK
4431
4432 /*
4433 * if plane is being disabled or scaler is no more required or force detach
4434 * - free scaler binded to this plane/crtc
4435 * - in order to do this, update crtc->scaler_usage
4436 *
4437 * Here scaler state in crtc_state is set free so that
4438 * scaler can be assigned to other user. Actual register
4439 * update to free the scaler is done in plane/panel-fit programming.
4440 * For this purpose crtc/plane_state->scaler_id isn't reset here.
4441 */
86adf9d7 4442 if (force_detach || !need_scaling) {
a1b2278e 4443 if (*scaler_id >= 0) {
86adf9d7 4444 scaler_state->scaler_users &= ~(1 << scaler_user);
a1b2278e
CK
4445 scaler_state->scalers[*scaler_id].in_use = 0;
4446
86adf9d7
ML
4447 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4448 "Staged freeing scaler id %d scaler_users = 0x%x\n",
4449 intel_crtc->pipe, scaler_user, *scaler_id,
a1b2278e
CK
4450 scaler_state->scaler_users);
4451 *scaler_id = -1;
4452 }
4453 return 0;
4454 }
4455
4456 /* range checks */
4457 if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
4458 dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
4459
4460 src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
4461 dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
86adf9d7 4462 DRM_DEBUG_KMS("scaler_user index %u.%u: src %ux%u dst %ux%u "
a1b2278e 4463 "size is out of scaler range\n",
86adf9d7 4464 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h);
a1b2278e
CK
4465 return -EINVAL;
4466 }
4467
86adf9d7
ML
4468 /* mark this plane as a scaler user in crtc_state */
4469 scaler_state->scaler_users |= (1 << scaler_user);
4470 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4471 "staged scaling request for %ux%u->%ux%u scaler_users = 0x%x\n",
4472 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h,
4473 scaler_state->scaler_users);
4474
4475 return 0;
4476}
4477
4478/**
4479 * skl_update_scaler_crtc - Stages update to scaler state for a given crtc.
4480 *
4481 * @state: crtc's scaler state
86adf9d7
ML
4482 *
4483 * Return
4484 * 0 - scaler_usage updated successfully
4485 * error - requested scaling cannot be supported or other error condition
4486 */
e435d6e5 4487int skl_update_scaler_crtc(struct intel_crtc_state *state)
86adf9d7
ML
4488{
4489 struct intel_crtc *intel_crtc = to_intel_crtc(state->base.crtc);
7c5f93b0 4490 const struct drm_display_mode *adjusted_mode = &state->base.adjusted_mode;
86adf9d7 4491
78108b7c
VS
4492 DRM_DEBUG_KMS("Updating scaler for [CRTC:%d:%s] scaler_user index %u.%u\n",
4493 intel_crtc->base.base.id, intel_crtc->base.name,
4494 intel_crtc->pipe, SKL_CRTC_INDEX);
86adf9d7 4495
e435d6e5 4496 return skl_update_scaler(state, !state->base.active, SKL_CRTC_INDEX,
fa5a7970 4497 &state->scaler_state.scaler_id, BIT(DRM_ROTATE_0),
86adf9d7 4498 state->pipe_src_w, state->pipe_src_h,
aad941d5 4499 adjusted_mode->crtc_hdisplay, adjusted_mode->crtc_vdisplay);
86adf9d7
ML
4500}
4501
4502/**
4503 * skl_update_scaler_plane - Stages update to scaler state for a given plane.
4504 *
4505 * @state: crtc's scaler state
86adf9d7
ML
4506 * @plane_state: atomic plane state to update
4507 *
4508 * Return
4509 * 0 - scaler_usage updated successfully
4510 * error - requested scaling cannot be supported or other error condition
4511 */
da20eabd
ML
4512static int skl_update_scaler_plane(struct intel_crtc_state *crtc_state,
4513 struct intel_plane_state *plane_state)
86adf9d7
ML
4514{
4515
4516 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
da20eabd
ML
4517 struct intel_plane *intel_plane =
4518 to_intel_plane(plane_state->base.plane);
86adf9d7
ML
4519 struct drm_framebuffer *fb = plane_state->base.fb;
4520 int ret;
4521
4522 bool force_detach = !fb || !plane_state->visible;
4523
72660ce0
VS
4524 DRM_DEBUG_KMS("Updating scaler for [PLANE:%d:%s] scaler_user index %u.%u\n",
4525 intel_plane->base.base.id, intel_plane->base.name,
4526 intel_crtc->pipe, drm_plane_index(&intel_plane->base));
86adf9d7
ML
4527
4528 ret = skl_update_scaler(crtc_state, force_detach,
4529 drm_plane_index(&intel_plane->base),
4530 &plane_state->scaler_id,
4531 plane_state->base.rotation,
4532 drm_rect_width(&plane_state->src) >> 16,
4533 drm_rect_height(&plane_state->src) >> 16,
4534 drm_rect_width(&plane_state->dst),
4535 drm_rect_height(&plane_state->dst));
4536
4537 if (ret || plane_state->scaler_id < 0)
4538 return ret;
4539
a1b2278e 4540 /* check colorkey */
818ed961 4541 if (plane_state->ckey.flags != I915_SET_COLORKEY_NONE) {
72660ce0
VS
4542 DRM_DEBUG_KMS("[PLANE:%d:%s] scaling with color key not allowed",
4543 intel_plane->base.base.id,
4544 intel_plane->base.name);
a1b2278e
CK
4545 return -EINVAL;
4546 }
4547
4548 /* Check src format */
86adf9d7
ML
4549 switch (fb->pixel_format) {
4550 case DRM_FORMAT_RGB565:
4551 case DRM_FORMAT_XBGR8888:
4552 case DRM_FORMAT_XRGB8888:
4553 case DRM_FORMAT_ABGR8888:
4554 case DRM_FORMAT_ARGB8888:
4555 case DRM_FORMAT_XRGB2101010:
4556 case DRM_FORMAT_XBGR2101010:
4557 case DRM_FORMAT_YUYV:
4558 case DRM_FORMAT_YVYU:
4559 case DRM_FORMAT_UYVY:
4560 case DRM_FORMAT_VYUY:
4561 break;
4562 default:
72660ce0
VS
4563 DRM_DEBUG_KMS("[PLANE:%d:%s] FB:%d unsupported scaling format 0x%x\n",
4564 intel_plane->base.base.id, intel_plane->base.name,
4565 fb->base.id, fb->pixel_format);
86adf9d7 4566 return -EINVAL;
a1b2278e
CK
4567 }
4568
a1b2278e
CK
4569 return 0;
4570}
4571
e435d6e5
ML
4572static void skylake_scaler_disable(struct intel_crtc *crtc)
4573{
4574 int i;
4575
4576 for (i = 0; i < crtc->num_scalers; i++)
4577 skl_detach_scaler(crtc, i);
4578}
4579
4580static void skylake_pfit_enable(struct intel_crtc *crtc)
bd2e244f
JB
4581{
4582 struct drm_device *dev = crtc->base.dev;
fac5e23e 4583 struct drm_i915_private *dev_priv = to_i915(dev);
bd2e244f 4584 int pipe = crtc->pipe;
a1b2278e
CK
4585 struct intel_crtc_scaler_state *scaler_state =
4586 &crtc->config->scaler_state;
4587
4588 DRM_DEBUG_KMS("for crtc_state = %p\n", crtc->config);
4589
6e3c9717 4590 if (crtc->config->pch_pfit.enabled) {
a1b2278e
CK
4591 int id;
4592
4593 if (WARN_ON(crtc->config->scaler_state.scaler_id < 0)) {
4594 DRM_ERROR("Requesting pfit without getting a scaler first\n");
4595 return;
4596 }
4597
4598 id = scaler_state->scaler_id;
4599 I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
4600 PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
4601 I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
4602 I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
4603
4604 DRM_DEBUG_KMS("for crtc_state = %p scaler_id = %d\n", crtc->config, id);
bd2e244f
JB
4605 }
4606}
4607
b074cec8
JB
4608static void ironlake_pfit_enable(struct intel_crtc *crtc)
4609{
4610 struct drm_device *dev = crtc->base.dev;
fac5e23e 4611 struct drm_i915_private *dev_priv = to_i915(dev);
b074cec8
JB
4612 int pipe = crtc->pipe;
4613
6e3c9717 4614 if (crtc->config->pch_pfit.enabled) {
b074cec8
JB
4615 /* Force use of hard-coded filter coefficients
4616 * as some pre-programmed values are broken,
4617 * e.g. x201.
4618 */
4619 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
4620 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
4621 PF_PIPE_SEL_IVB(pipe));
4622 else
4623 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
6e3c9717
ACO
4624 I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
4625 I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
d4270e57
JB
4626 }
4627}
4628
20bc8673 4629void hsw_enable_ips(struct intel_crtc *crtc)
d77e4531 4630{
cea165c3 4631 struct drm_device *dev = crtc->base.dev;
fac5e23e 4632 struct drm_i915_private *dev_priv = to_i915(dev);
d77e4531 4633
6e3c9717 4634 if (!crtc->config->ips_enabled)
d77e4531
PZ
4635 return;
4636
307e4498
ML
4637 /*
4638 * We can only enable IPS after we enable a plane and wait for a vblank
4639 * This function is called from post_plane_update, which is run after
4640 * a vblank wait.
4641 */
cea165c3 4642
d77e4531 4643 assert_plane_enabled(dev_priv, crtc->plane);
cea165c3 4644 if (IS_BROADWELL(dev)) {
2a114cc1
BW
4645 mutex_lock(&dev_priv->rps.hw_lock);
4646 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
4647 mutex_unlock(&dev_priv->rps.hw_lock);
4648 /* Quoting Art Runyan: "its not safe to expect any particular
4649 * value in IPS_CTL bit 31 after enabling IPS through the
e59150dc
JB
4650 * mailbox." Moreover, the mailbox may return a bogus state,
4651 * so we need to just enable it and continue on.
2a114cc1
BW
4652 */
4653 } else {
4654 I915_WRITE(IPS_CTL, IPS_ENABLE);
4655 /* The bit only becomes 1 in the next vblank, so this wait here
4656 * is essentially intel_wait_for_vblank. If we don't have this
4657 * and don't wait for vblanks until the end of crtc_enable, then
4658 * the HW state readout code will complain that the expected
4659 * IPS_CTL value is not the one we read. */
2ec9ba3c
CW
4660 if (intel_wait_for_register(dev_priv,
4661 IPS_CTL, IPS_ENABLE, IPS_ENABLE,
4662 50))
2a114cc1
BW
4663 DRM_ERROR("Timed out waiting for IPS enable\n");
4664 }
d77e4531
PZ
4665}
4666
20bc8673 4667void hsw_disable_ips(struct intel_crtc *crtc)
d77e4531
PZ
4668{
4669 struct drm_device *dev = crtc->base.dev;
fac5e23e 4670 struct drm_i915_private *dev_priv = to_i915(dev);
d77e4531 4671
6e3c9717 4672 if (!crtc->config->ips_enabled)
d77e4531
PZ
4673 return;
4674
4675 assert_plane_enabled(dev_priv, crtc->plane);
23d0b130 4676 if (IS_BROADWELL(dev)) {
2a114cc1
BW
4677 mutex_lock(&dev_priv->rps.hw_lock);
4678 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
4679 mutex_unlock(&dev_priv->rps.hw_lock);
23d0b130 4680 /* wait for pcode to finish disabling IPS, which may take up to 42ms */
b85c1ecf
CW
4681 if (intel_wait_for_register(dev_priv,
4682 IPS_CTL, IPS_ENABLE, 0,
4683 42))
23d0b130 4684 DRM_ERROR("Timed out waiting for IPS disable\n");
e59150dc 4685 } else {
2a114cc1 4686 I915_WRITE(IPS_CTL, 0);
e59150dc
JB
4687 POSTING_READ(IPS_CTL);
4688 }
d77e4531
PZ
4689
4690 /* We need to wait for a vblank before we can disable the plane. */
4691 intel_wait_for_vblank(dev, crtc->pipe);
4692}
4693
7cac945f 4694static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
d3eedb1a 4695{
7cac945f 4696 if (intel_crtc->overlay) {
d3eedb1a 4697 struct drm_device *dev = intel_crtc->base.dev;
fac5e23e 4698 struct drm_i915_private *dev_priv = to_i915(dev);
d3eedb1a
VS
4699
4700 mutex_lock(&dev->struct_mutex);
4701 dev_priv->mm.interruptible = false;
4702 (void) intel_overlay_switch_off(intel_crtc->overlay);
4703 dev_priv->mm.interruptible = true;
4704 mutex_unlock(&dev->struct_mutex);
4705 }
4706
4707 /* Let userspace switch the overlay on again. In most cases userspace
4708 * has to recompute where to put it anyway.
4709 */
4710}
4711
87d4300a
ML
4712/**
4713 * intel_post_enable_primary - Perform operations after enabling primary plane
4714 * @crtc: the CRTC whose primary plane was just enabled
4715 *
4716 * Performs potentially sleeping operations that must be done after the primary
4717 * plane is enabled, such as updating FBC and IPS. Note that this may be
4718 * called due to an explicit primary plane update, or due to an implicit
4719 * re-enable that is caused when a sprite plane is updated to no longer
4720 * completely hide the primary plane.
4721 */
4722static void
4723intel_post_enable_primary(struct drm_crtc *crtc)
a5c4d7bc
VS
4724{
4725 struct drm_device *dev = crtc->dev;
fac5e23e 4726 struct drm_i915_private *dev_priv = to_i915(dev);
a5c4d7bc
VS
4727 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4728 int pipe = intel_crtc->pipe;
a5c4d7bc 4729
87d4300a
ML
4730 /*
4731 * FIXME IPS should be fine as long as one plane is
4732 * enabled, but in practice it seems to have problems
4733 * when going from primary only to sprite only and vice
4734 * versa.
4735 */
a5c4d7bc
VS
4736 hsw_enable_ips(intel_crtc);
4737
f99d7069 4738 /*
87d4300a
ML
4739 * Gen2 reports pipe underruns whenever all planes are disabled.
4740 * So don't enable underrun reporting before at least some planes
4741 * are enabled.
4742 * FIXME: Need to fix the logic to work when we turn off all planes
4743 * but leave the pipe running.
f99d7069 4744 */
87d4300a
ML
4745 if (IS_GEN2(dev))
4746 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4747
aca7b684
VS
4748 /* Underruns don't always raise interrupts, so check manually. */
4749 intel_check_cpu_fifo_underruns(dev_priv);
4750 intel_check_pch_fifo_underruns(dev_priv);
a5c4d7bc
VS
4751}
4752
2622a081 4753/* FIXME move all this to pre_plane_update() with proper state tracking */
87d4300a
ML
4754static void
4755intel_pre_disable_primary(struct drm_crtc *crtc)
a5c4d7bc
VS
4756{
4757 struct drm_device *dev = crtc->dev;
fac5e23e 4758 struct drm_i915_private *dev_priv = to_i915(dev);
a5c4d7bc
VS
4759 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4760 int pipe = intel_crtc->pipe;
a5c4d7bc 4761
87d4300a
ML
4762 /*
4763 * Gen2 reports pipe underruns whenever all planes are disabled.
4764 * So diasble underrun reporting before all the planes get disabled.
4765 * FIXME: Need to fix the logic to work when we turn off all planes
4766 * but leave the pipe running.
4767 */
4768 if (IS_GEN2(dev))
4769 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
a5c4d7bc 4770
2622a081
VS
4771 /*
4772 * FIXME IPS should be fine as long as one plane is
4773 * enabled, but in practice it seems to have problems
4774 * when going from primary only to sprite only and vice
4775 * versa.
4776 */
4777 hsw_disable_ips(intel_crtc);
4778}
4779
4780/* FIXME get rid of this and use pre_plane_update */
4781static void
4782intel_pre_disable_primary_noatomic(struct drm_crtc *crtc)
4783{
4784 struct drm_device *dev = crtc->dev;
fac5e23e 4785 struct drm_i915_private *dev_priv = to_i915(dev);
2622a081
VS
4786 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4787 int pipe = intel_crtc->pipe;
4788
4789 intel_pre_disable_primary(crtc);
4790
87d4300a
ML
4791 /*
4792 * Vblank time updates from the shadow to live plane control register
4793 * are blocked if the memory self-refresh mode is active at that
4794 * moment. So to make sure the plane gets truly disabled, disable
4795 * first the self-refresh mode. The self-refresh enable bit in turn
4796 * will be checked/applied by the HW only at the next frame start
4797 * event which is after the vblank start event, so we need to have a
4798 * wait-for-vblank between disabling the plane and the pipe.
4799 */
262cd2e1 4800 if (HAS_GMCH_DISPLAY(dev)) {
87d4300a 4801 intel_set_memory_cxsr(dev_priv, false);
262cd2e1
VS
4802 dev_priv->wm.vlv.cxsr = false;
4803 intel_wait_for_vblank(dev, pipe);
4804 }
87d4300a
ML
4805}
4806
5a21b665
DV
4807static void intel_post_plane_update(struct intel_crtc_state *old_crtc_state)
4808{
4809 struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
4810 struct drm_atomic_state *old_state = old_crtc_state->base.state;
4811 struct intel_crtc_state *pipe_config =
4812 to_intel_crtc_state(crtc->base.state);
5a21b665
DV
4813 struct drm_plane *primary = crtc->base.primary;
4814 struct drm_plane_state *old_pri_state =
4815 drm_atomic_get_existing_plane_state(old_state, primary);
4816
5748b6a1 4817 intel_frontbuffer_flip(to_i915(crtc->base.dev), pipe_config->fb_bits);
5a21b665
DV
4818
4819 crtc->wm.cxsr_allowed = true;
4820
4821 if (pipe_config->update_wm_post && pipe_config->base.active)
4822 intel_update_watermarks(&crtc->base);
4823
4824 if (old_pri_state) {
4825 struct intel_plane_state *primary_state =
4826 to_intel_plane_state(primary->state);
4827 struct intel_plane_state *old_primary_state =
4828 to_intel_plane_state(old_pri_state);
4829
4830 intel_fbc_post_update(crtc);
4831
4832 if (primary_state->visible &&
4833 (needs_modeset(&pipe_config->base) ||
4834 !old_primary_state->visible))
4835 intel_post_enable_primary(&crtc->base);
4836 }
4837}
4838
5c74cd73 4839static void intel_pre_plane_update(struct intel_crtc_state *old_crtc_state)
ac21b225 4840{
5c74cd73 4841 struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
ac21b225 4842 struct drm_device *dev = crtc->base.dev;
fac5e23e 4843 struct drm_i915_private *dev_priv = to_i915(dev);
ab1d3a0e
ML
4844 struct intel_crtc_state *pipe_config =
4845 to_intel_crtc_state(crtc->base.state);
5c74cd73
ML
4846 struct drm_atomic_state *old_state = old_crtc_state->base.state;
4847 struct drm_plane *primary = crtc->base.primary;
4848 struct drm_plane_state *old_pri_state =
4849 drm_atomic_get_existing_plane_state(old_state, primary);
4850 bool modeset = needs_modeset(&pipe_config->base);
ac21b225 4851
5c74cd73
ML
4852 if (old_pri_state) {
4853 struct intel_plane_state *primary_state =
4854 to_intel_plane_state(primary->state);
4855 struct intel_plane_state *old_primary_state =
4856 to_intel_plane_state(old_pri_state);
4857
faf68d92 4858 intel_fbc_pre_update(crtc, pipe_config, primary_state);
31ae71fc 4859
5c74cd73
ML
4860 if (old_primary_state->visible &&
4861 (modeset || !primary_state->visible))
4862 intel_pre_disable_primary(&crtc->base);
4863 }
852eb00d 4864
a4015f9a 4865 if (pipe_config->disable_cxsr && HAS_GMCH_DISPLAY(dev)) {
852eb00d 4866 crtc->wm.cxsr_allowed = false;
2dfd178d 4867
2622a081
VS
4868 /*
4869 * Vblank time updates from the shadow to live plane control register
4870 * are blocked if the memory self-refresh mode is active at that
4871 * moment. So to make sure the plane gets truly disabled, disable
4872 * first the self-refresh mode. The self-refresh enable bit in turn
4873 * will be checked/applied by the HW only at the next frame start
4874 * event which is after the vblank start event, so we need to have a
4875 * wait-for-vblank between disabling the plane and the pipe.
4876 */
4877 if (old_crtc_state->base.active) {
2dfd178d 4878 intel_set_memory_cxsr(dev_priv, false);
2622a081
VS
4879 dev_priv->wm.vlv.cxsr = false;
4880 intel_wait_for_vblank(dev, crtc->pipe);
4881 }
852eb00d 4882 }
92826fcd 4883
ed4a6a7c
MR
4884 /*
4885 * IVB workaround: must disable low power watermarks for at least
4886 * one frame before enabling scaling. LP watermarks can be re-enabled
4887 * when scaling is disabled.
4888 *
4889 * WaCxSRDisabledForSpriteScaling:ivb
4890 */
4891 if (pipe_config->disable_lp_wm) {
4892 ilk_disable_lp_wm(dev);
4893 intel_wait_for_vblank(dev, crtc->pipe);
4894 }
4895
4896 /*
4897 * If we're doing a modeset, we're done. No need to do any pre-vblank
4898 * watermark programming here.
4899 */
4900 if (needs_modeset(&pipe_config->base))
4901 return;
4902
4903 /*
4904 * For platforms that support atomic watermarks, program the
4905 * 'intermediate' watermarks immediately. On pre-gen9 platforms, these
4906 * will be the intermediate values that are safe for both pre- and
4907 * post- vblank; when vblank happens, the 'active' values will be set
4908 * to the final 'target' values and we'll do this again to get the
4909 * optimal watermarks. For gen9+ platforms, the values we program here
4910 * will be the final target values which will get automatically latched
4911 * at vblank time; no further programming will be necessary.
4912 *
4913 * If a platform hasn't been transitioned to atomic watermarks yet,
4914 * we'll continue to update watermarks the old way, if flags tell
4915 * us to.
4916 */
4917 if (dev_priv->display.initial_watermarks != NULL)
4918 dev_priv->display.initial_watermarks(pipe_config);
caed361d 4919 else if (pipe_config->update_wm_pre)
92826fcd 4920 intel_update_watermarks(&crtc->base);
ac21b225
ML
4921}
4922
d032ffa0 4923static void intel_crtc_disable_planes(struct drm_crtc *crtc, unsigned plane_mask)
87d4300a
ML
4924{
4925 struct drm_device *dev = crtc->dev;
4926 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
d032ffa0 4927 struct drm_plane *p;
87d4300a
ML
4928 int pipe = intel_crtc->pipe;
4929
7cac945f 4930 intel_crtc_dpms_overlay_disable(intel_crtc);
27321ae8 4931
d032ffa0
ML
4932 drm_for_each_plane_mask(p, dev, plane_mask)
4933 to_intel_plane(p)->disable_plane(p, crtc);
f98551ae 4934
f99d7069
DV
4935 /*
4936 * FIXME: Once we grow proper nuclear flip support out of this we need
4937 * to compute the mask of flip planes precisely. For the time being
4938 * consider this a flip to a NULL plane.
4939 */
5748b6a1 4940 intel_frontbuffer_flip(to_i915(dev), INTEL_FRONTBUFFER_ALL_MASK(pipe));
a5c4d7bc
VS
4941}
4942
f67a559d
JB
4943static void ironlake_crtc_enable(struct drm_crtc *crtc)
4944{
4945 struct drm_device *dev = crtc->dev;
fac5e23e 4946 struct drm_i915_private *dev_priv = to_i915(dev);
f67a559d 4947 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 4948 struct intel_encoder *encoder;
f67a559d 4949 int pipe = intel_crtc->pipe;
b95c5321
ML
4950 struct intel_crtc_state *pipe_config =
4951 to_intel_crtc_state(crtc->state);
f67a559d 4952
53d9f4e9 4953 if (WARN_ON(intel_crtc->active))
f67a559d
JB
4954 return;
4955
b2c0593a
VS
4956 /*
4957 * Sometimes spurious CPU pipe underruns happen during FDI
4958 * training, at least with VGA+HDMI cloning. Suppress them.
4959 *
4960 * On ILK we get an occasional spurious CPU pipe underruns
4961 * between eDP port A enable and vdd enable. Also PCH port
4962 * enable seems to result in the occasional CPU pipe underrun.
4963 *
4964 * Spurious PCH underruns also occur during PCH enabling.
4965 */
4966 if (intel_crtc->config->has_pch_encoder || IS_GEN5(dev_priv))
4967 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
81b088ca
VS
4968 if (intel_crtc->config->has_pch_encoder)
4969 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
4970
6e3c9717 4971 if (intel_crtc->config->has_pch_encoder)
b14b1055
DV
4972 intel_prepare_shared_dpll(intel_crtc);
4973
37a5650b 4974 if (intel_crtc_has_dp_encoder(intel_crtc->config))
fe3cd48d 4975 intel_dp_set_m_n(intel_crtc, M1_N1);
29407aab
DV
4976
4977 intel_set_pipe_timings(intel_crtc);
bc58be60 4978 intel_set_pipe_src_size(intel_crtc);
29407aab 4979
6e3c9717 4980 if (intel_crtc->config->has_pch_encoder) {
29407aab 4981 intel_cpu_transcoder_set_m_n(intel_crtc,
6e3c9717 4982 &intel_crtc->config->fdi_m_n, NULL);
29407aab
DV
4983 }
4984
4985 ironlake_set_pipeconf(crtc);
4986
f67a559d 4987 intel_crtc->active = true;
8664281b 4988
f6736a1a 4989 for_each_encoder_on_crtc(dev, crtc, encoder)
952735ee
DV
4990 if (encoder->pre_enable)
4991 encoder->pre_enable(encoder);
f67a559d 4992
6e3c9717 4993 if (intel_crtc->config->has_pch_encoder) {
fff367c7
DV
4994 /* Note: FDI PLL enabling _must_ be done before we enable the
4995 * cpu pipes, hence this is separate from all the other fdi/pch
4996 * enabling. */
88cefb6c 4997 ironlake_fdi_pll_enable(intel_crtc);
46b6f814
DV
4998 } else {
4999 assert_fdi_tx_disabled(dev_priv, pipe);
5000 assert_fdi_rx_disabled(dev_priv, pipe);
5001 }
f67a559d 5002
b074cec8 5003 ironlake_pfit_enable(intel_crtc);
f67a559d 5004
9c54c0dd
JB
5005 /*
5006 * On ILK+ LUT must be loaded before the pipe is running but with
5007 * clocks enabled
5008 */
b95c5321 5009 intel_color_load_luts(&pipe_config->base);
9c54c0dd 5010
1d5bf5d9
ID
5011 if (dev_priv->display.initial_watermarks != NULL)
5012 dev_priv->display.initial_watermarks(intel_crtc->config);
e1fdc473 5013 intel_enable_pipe(intel_crtc);
f67a559d 5014
6e3c9717 5015 if (intel_crtc->config->has_pch_encoder)
f67a559d 5016 ironlake_pch_enable(crtc);
c98e9dcf 5017
f9b61ff6
DV
5018 assert_vblank_disabled(crtc);
5019 drm_crtc_vblank_on(crtc);
5020
fa5c73b1
DV
5021 for_each_encoder_on_crtc(dev, crtc, encoder)
5022 encoder->enable(encoder);
61b77ddd
DV
5023
5024 if (HAS_PCH_CPT(dev))
a1520318 5025 cpt_verify_modeset(dev, intel_crtc->pipe);
37ca8d4c
VS
5026
5027 /* Must wait for vblank to avoid spurious PCH FIFO underruns */
5028 if (intel_crtc->config->has_pch_encoder)
5029 intel_wait_for_vblank(dev, pipe);
b2c0593a 5030 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
37ca8d4c 5031 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
6be4a607
JB
5032}
5033
42db64ef
PZ
5034/* IPS only exists on ULT machines and is tied to pipe A. */
5035static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
5036{
f5adf94e 5037 return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
42db64ef
PZ
5038}
5039
4f771f10
PZ
5040static void haswell_crtc_enable(struct drm_crtc *crtc)
5041{
5042 struct drm_device *dev = crtc->dev;
fac5e23e 5043 struct drm_i915_private *dev_priv = to_i915(dev);
4f771f10
PZ
5044 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5045 struct intel_encoder *encoder;
99d736a2 5046 int pipe = intel_crtc->pipe, hsw_workaround_pipe;
4d1de975 5047 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
99d736a2
ML
5048 struct intel_crtc_state *pipe_config =
5049 to_intel_crtc_state(crtc->state);
4f771f10 5050
53d9f4e9 5051 if (WARN_ON(intel_crtc->active))
4f771f10
PZ
5052 return;
5053
81b088ca
VS
5054 if (intel_crtc->config->has_pch_encoder)
5055 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5056 false);
5057
95a7a2ae
ID
5058 for_each_encoder_on_crtc(dev, crtc, encoder)
5059 if (encoder->pre_pll_enable)
5060 encoder->pre_pll_enable(encoder);
5061
8106ddbd 5062 if (intel_crtc->config->shared_dpll)
df8ad70c
DV
5063 intel_enable_shared_dpll(intel_crtc);
5064
37a5650b 5065 if (intel_crtc_has_dp_encoder(intel_crtc->config))
fe3cd48d 5066 intel_dp_set_m_n(intel_crtc, M1_N1);
229fca97 5067
d7edc4e5 5068 if (!transcoder_is_dsi(cpu_transcoder))
4d1de975
JN
5069 intel_set_pipe_timings(intel_crtc);
5070
bc58be60 5071 intel_set_pipe_src_size(intel_crtc);
229fca97 5072
4d1de975
JN
5073 if (cpu_transcoder != TRANSCODER_EDP &&
5074 !transcoder_is_dsi(cpu_transcoder)) {
5075 I915_WRITE(PIPE_MULT(cpu_transcoder),
6e3c9717 5076 intel_crtc->config->pixel_multiplier - 1);
ebb69c95
CT
5077 }
5078
6e3c9717 5079 if (intel_crtc->config->has_pch_encoder) {
229fca97 5080 intel_cpu_transcoder_set_m_n(intel_crtc,
6e3c9717 5081 &intel_crtc->config->fdi_m_n, NULL);
229fca97
DV
5082 }
5083
d7edc4e5 5084 if (!transcoder_is_dsi(cpu_transcoder))
4d1de975
JN
5085 haswell_set_pipeconf(crtc);
5086
391bf048 5087 haswell_set_pipemisc(crtc);
229fca97 5088
b95c5321 5089 intel_color_set_csc(&pipe_config->base);
229fca97 5090
4f771f10 5091 intel_crtc->active = true;
8664281b 5092
6b698516
DV
5093 if (intel_crtc->config->has_pch_encoder)
5094 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
5095 else
5096 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
5097
7d4aefd0 5098 for_each_encoder_on_crtc(dev, crtc, encoder) {
4f771f10
PZ
5099 if (encoder->pre_enable)
5100 encoder->pre_enable(encoder);
7d4aefd0 5101 }
4f771f10 5102
d2d65408 5103 if (intel_crtc->config->has_pch_encoder)
4fe9467d 5104 dev_priv->display.fdi_link_train(crtc);
4fe9467d 5105
d7edc4e5 5106 if (!transcoder_is_dsi(cpu_transcoder))
7d4aefd0 5107 intel_ddi_enable_pipe_clock(intel_crtc);
4f771f10 5108
1c132b44 5109 if (INTEL_INFO(dev)->gen >= 9)
e435d6e5 5110 skylake_pfit_enable(intel_crtc);
ff6d9f55 5111 else
1c132b44 5112 ironlake_pfit_enable(intel_crtc);
4f771f10
PZ
5113
5114 /*
5115 * On ILK+ LUT must be loaded before the pipe is running but with
5116 * clocks enabled
5117 */
b95c5321 5118 intel_color_load_luts(&pipe_config->base);
4f771f10 5119
1f544388 5120 intel_ddi_set_pipe_settings(crtc);
d7edc4e5 5121 if (!transcoder_is_dsi(cpu_transcoder))
7d4aefd0 5122 intel_ddi_enable_transcoder_func(crtc);
4f771f10 5123
1d5bf5d9
ID
5124 if (dev_priv->display.initial_watermarks != NULL)
5125 dev_priv->display.initial_watermarks(pipe_config);
5126 else
5127 intel_update_watermarks(crtc);
4d1de975
JN
5128
5129 /* XXX: Do the pipe assertions at the right place for BXT DSI. */
d7edc4e5 5130 if (!transcoder_is_dsi(cpu_transcoder))
4d1de975 5131 intel_enable_pipe(intel_crtc);
42db64ef 5132
6e3c9717 5133 if (intel_crtc->config->has_pch_encoder)
1507e5bd 5134 lpt_pch_enable(crtc);
4f771f10 5135
a65347ba 5136 if (intel_crtc->config->dp_encoder_is_mst)
0e32b39c
DA
5137 intel_ddi_set_vc_payload_alloc(crtc, true);
5138
f9b61ff6
DV
5139 assert_vblank_disabled(crtc);
5140 drm_crtc_vblank_on(crtc);
5141
8807e55b 5142 for_each_encoder_on_crtc(dev, crtc, encoder) {
4f771f10 5143 encoder->enable(encoder);
8807e55b
JN
5144 intel_opregion_notify_encoder(encoder, true);
5145 }
4f771f10 5146
6b698516
DV
5147 if (intel_crtc->config->has_pch_encoder) {
5148 intel_wait_for_vblank(dev, pipe);
5149 intel_wait_for_vblank(dev, pipe);
5150 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
d2d65408
VS
5151 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5152 true);
6b698516 5153 }
d2d65408 5154
e4916946
PZ
5155 /* If we change the relative order between pipe/planes enabling, we need
5156 * to change the workaround. */
99d736a2
ML
5157 hsw_workaround_pipe = pipe_config->hsw_workaround_pipe;
5158 if (IS_HASWELL(dev) && hsw_workaround_pipe != INVALID_PIPE) {
5159 intel_wait_for_vblank(dev, hsw_workaround_pipe);
5160 intel_wait_for_vblank(dev, hsw_workaround_pipe);
5161 }
4f771f10
PZ
5162}
5163
bfd16b2a 5164static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force)
3f8dce3a
DV
5165{
5166 struct drm_device *dev = crtc->base.dev;
fac5e23e 5167 struct drm_i915_private *dev_priv = to_i915(dev);
3f8dce3a
DV
5168 int pipe = crtc->pipe;
5169
5170 /* To avoid upsetting the power well on haswell only disable the pfit if
5171 * it's in use. The hw state code will make sure we get this right. */
bfd16b2a 5172 if (force || crtc->config->pch_pfit.enabled) {
3f8dce3a
DV
5173 I915_WRITE(PF_CTL(pipe), 0);
5174 I915_WRITE(PF_WIN_POS(pipe), 0);
5175 I915_WRITE(PF_WIN_SZ(pipe), 0);
5176 }
5177}
5178
6be4a607
JB
5179static void ironlake_crtc_disable(struct drm_crtc *crtc)
5180{
5181 struct drm_device *dev = crtc->dev;
fac5e23e 5182 struct drm_i915_private *dev_priv = to_i915(dev);
6be4a607 5183 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 5184 struct intel_encoder *encoder;
6be4a607 5185 int pipe = intel_crtc->pipe;
b52eb4dc 5186
b2c0593a
VS
5187 /*
5188 * Sometimes spurious CPU pipe underruns happen when the
5189 * pipe is already disabled, but FDI RX/TX is still enabled.
5190 * Happens at least with VGA+HDMI cloning. Suppress them.
5191 */
5192 if (intel_crtc->config->has_pch_encoder) {
5193 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
37ca8d4c 5194 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
b2c0593a 5195 }
37ca8d4c 5196
ea9d758d
DV
5197 for_each_encoder_on_crtc(dev, crtc, encoder)
5198 encoder->disable(encoder);
5199
f9b61ff6
DV
5200 drm_crtc_vblank_off(crtc);
5201 assert_vblank_disabled(crtc);
5202
575f7ab7 5203 intel_disable_pipe(intel_crtc);
32f9d658 5204
bfd16b2a 5205 ironlake_pfit_disable(intel_crtc, false);
2c07245f 5206
b2c0593a 5207 if (intel_crtc->config->has_pch_encoder)
5a74f70a
VS
5208 ironlake_fdi_disable(crtc);
5209
bf49ec8c
DV
5210 for_each_encoder_on_crtc(dev, crtc, encoder)
5211 if (encoder->post_disable)
5212 encoder->post_disable(encoder);
2c07245f 5213
6e3c9717 5214 if (intel_crtc->config->has_pch_encoder) {
d925c59a 5215 ironlake_disable_pch_transcoder(dev_priv, pipe);
6be4a607 5216
d925c59a 5217 if (HAS_PCH_CPT(dev)) {
f0f59a00
VS
5218 i915_reg_t reg;
5219 u32 temp;
5220
d925c59a
DV
5221 /* disable TRANS_DP_CTL */
5222 reg = TRANS_DP_CTL(pipe);
5223 temp = I915_READ(reg);
5224 temp &= ~(TRANS_DP_OUTPUT_ENABLE |
5225 TRANS_DP_PORT_SEL_MASK);
5226 temp |= TRANS_DP_PORT_SEL_NONE;
5227 I915_WRITE(reg, temp);
5228
5229 /* disable DPLL_SEL */
5230 temp = I915_READ(PCH_DPLL_SEL);
11887397 5231 temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
d925c59a 5232 I915_WRITE(PCH_DPLL_SEL, temp);
9db4a9c7 5233 }
e3421a18 5234
d925c59a
DV
5235 ironlake_fdi_pll_disable(intel_crtc);
5236 }
81b088ca 5237
b2c0593a 5238 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
81b088ca 5239 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
6be4a607 5240}
1b3c7a47 5241
4f771f10 5242static void haswell_crtc_disable(struct drm_crtc *crtc)
ee7b9f93 5243{
4f771f10 5244 struct drm_device *dev = crtc->dev;
fac5e23e 5245 struct drm_i915_private *dev_priv = to_i915(dev);
ee7b9f93 5246 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4f771f10 5247 struct intel_encoder *encoder;
6e3c9717 5248 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
ee7b9f93 5249
d2d65408
VS
5250 if (intel_crtc->config->has_pch_encoder)
5251 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5252 false);
5253
8807e55b
JN
5254 for_each_encoder_on_crtc(dev, crtc, encoder) {
5255 intel_opregion_notify_encoder(encoder, false);
4f771f10 5256 encoder->disable(encoder);
8807e55b 5257 }
4f771f10 5258
f9b61ff6
DV
5259 drm_crtc_vblank_off(crtc);
5260 assert_vblank_disabled(crtc);
5261
4d1de975 5262 /* XXX: Do the pipe assertions at the right place for BXT DSI. */
d7edc4e5 5263 if (!transcoder_is_dsi(cpu_transcoder))
4d1de975 5264 intel_disable_pipe(intel_crtc);
4f771f10 5265
6e3c9717 5266 if (intel_crtc->config->dp_encoder_is_mst)
a4bf214f
VS
5267 intel_ddi_set_vc_payload_alloc(crtc, false);
5268
d7edc4e5 5269 if (!transcoder_is_dsi(cpu_transcoder))
7d4aefd0 5270 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
4f771f10 5271
1c132b44 5272 if (INTEL_INFO(dev)->gen >= 9)
e435d6e5 5273 skylake_scaler_disable(intel_crtc);
ff6d9f55 5274 else
bfd16b2a 5275 ironlake_pfit_disable(intel_crtc, false);
4f771f10 5276
d7edc4e5 5277 if (!transcoder_is_dsi(cpu_transcoder))
7d4aefd0 5278 intel_ddi_disable_pipe_clock(intel_crtc);
4f771f10 5279
97b040aa
ID
5280 for_each_encoder_on_crtc(dev, crtc, encoder)
5281 if (encoder->post_disable)
5282 encoder->post_disable(encoder);
81b088ca 5283
92966a37
VS
5284 if (intel_crtc->config->has_pch_encoder) {
5285 lpt_disable_pch_transcoder(dev_priv);
503a74e9 5286 lpt_disable_iclkip(dev_priv);
92966a37
VS
5287 intel_ddi_fdi_disable(crtc);
5288
81b088ca
VS
5289 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5290 true);
92966a37 5291 }
4f771f10
PZ
5292}
5293
2dd24552
JB
5294static void i9xx_pfit_enable(struct intel_crtc *crtc)
5295{
5296 struct drm_device *dev = crtc->base.dev;
fac5e23e 5297 struct drm_i915_private *dev_priv = to_i915(dev);
6e3c9717 5298 struct intel_crtc_state *pipe_config = crtc->config;
2dd24552 5299
681a8504 5300 if (!pipe_config->gmch_pfit.control)
2dd24552
JB
5301 return;
5302
2dd24552 5303 /*
c0b03411
DV
5304 * The panel fitter should only be adjusted whilst the pipe is disabled,
5305 * according to register description and PRM.
2dd24552 5306 */
c0b03411
DV
5307 WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
5308 assert_pipe_disabled(dev_priv, crtc->pipe);
2dd24552 5309
b074cec8
JB
5310 I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
5311 I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
5a80c45c
DV
5312
5313 /* Border color in case we don't scale up to the full screen. Black by
5314 * default, change to something else for debugging. */
5315 I915_WRITE(BCLRPAT(crtc->pipe), 0);
2dd24552
JB
5316}
5317
d05410f9
DA
5318static enum intel_display_power_domain port_to_power_domain(enum port port)
5319{
5320 switch (port) {
5321 case PORT_A:
6331a704 5322 return POWER_DOMAIN_PORT_DDI_A_LANES;
d05410f9 5323 case PORT_B:
6331a704 5324 return POWER_DOMAIN_PORT_DDI_B_LANES;
d05410f9 5325 case PORT_C:
6331a704 5326 return POWER_DOMAIN_PORT_DDI_C_LANES;
d05410f9 5327 case PORT_D:
6331a704 5328 return POWER_DOMAIN_PORT_DDI_D_LANES;
d8e19f99 5329 case PORT_E:
6331a704 5330 return POWER_DOMAIN_PORT_DDI_E_LANES;
d05410f9 5331 default:
b9fec167 5332 MISSING_CASE(port);
d05410f9
DA
5333 return POWER_DOMAIN_PORT_OTHER;
5334 }
5335}
5336
25f78f58
VS
5337static enum intel_display_power_domain port_to_aux_power_domain(enum port port)
5338{
5339 switch (port) {
5340 case PORT_A:
5341 return POWER_DOMAIN_AUX_A;
5342 case PORT_B:
5343 return POWER_DOMAIN_AUX_B;
5344 case PORT_C:
5345 return POWER_DOMAIN_AUX_C;
5346 case PORT_D:
5347 return POWER_DOMAIN_AUX_D;
5348 case PORT_E:
5349 /* FIXME: Check VBT for actual wiring of PORT E */
5350 return POWER_DOMAIN_AUX_D;
5351 default:
b9fec167 5352 MISSING_CASE(port);
25f78f58
VS
5353 return POWER_DOMAIN_AUX_A;
5354 }
5355}
5356
319be8ae
ID
5357enum intel_display_power_domain
5358intel_display_port_power_domain(struct intel_encoder *intel_encoder)
5359{
5360 struct drm_device *dev = intel_encoder->base.dev;
5361 struct intel_digital_port *intel_dig_port;
5362
5363 switch (intel_encoder->type) {
5364 case INTEL_OUTPUT_UNKNOWN:
5365 /* Only DDI platforms should ever use this output type */
5366 WARN_ON_ONCE(!HAS_DDI(dev));
cca0502b 5367 case INTEL_OUTPUT_DP:
319be8ae
ID
5368 case INTEL_OUTPUT_HDMI:
5369 case INTEL_OUTPUT_EDP:
5370 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
d05410f9 5371 return port_to_power_domain(intel_dig_port->port);
0e32b39c
DA
5372 case INTEL_OUTPUT_DP_MST:
5373 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
5374 return port_to_power_domain(intel_dig_port->port);
319be8ae
ID
5375 case INTEL_OUTPUT_ANALOG:
5376 return POWER_DOMAIN_PORT_CRT;
5377 case INTEL_OUTPUT_DSI:
5378 return POWER_DOMAIN_PORT_DSI;
5379 default:
5380 return POWER_DOMAIN_PORT_OTHER;
5381 }
5382}
5383
25f78f58
VS
5384enum intel_display_power_domain
5385intel_display_port_aux_power_domain(struct intel_encoder *intel_encoder)
5386{
5387 struct drm_device *dev = intel_encoder->base.dev;
5388 struct intel_digital_port *intel_dig_port;
5389
5390 switch (intel_encoder->type) {
5391 case INTEL_OUTPUT_UNKNOWN:
651174a4
ID
5392 case INTEL_OUTPUT_HDMI:
5393 /*
5394 * Only DDI platforms should ever use these output types.
5395 * We can get here after the HDMI detect code has already set
5396 * the type of the shared encoder. Since we can't be sure
5397 * what's the status of the given connectors, play safe and
5398 * run the DP detection too.
5399 */
25f78f58 5400 WARN_ON_ONCE(!HAS_DDI(dev));
cca0502b 5401 case INTEL_OUTPUT_DP:
25f78f58
VS
5402 case INTEL_OUTPUT_EDP:
5403 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
5404 return port_to_aux_power_domain(intel_dig_port->port);
5405 case INTEL_OUTPUT_DP_MST:
5406 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
5407 return port_to_aux_power_domain(intel_dig_port->port);
5408 default:
b9fec167 5409 MISSING_CASE(intel_encoder->type);
25f78f58
VS
5410 return POWER_DOMAIN_AUX_A;
5411 }
5412}
5413
74bff5f9
ML
5414static unsigned long get_crtc_power_domains(struct drm_crtc *crtc,
5415 struct intel_crtc_state *crtc_state)
77d22dca 5416{
319be8ae 5417 struct drm_device *dev = crtc->dev;
74bff5f9 5418 struct drm_encoder *encoder;
319be8ae
ID
5419 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5420 enum pipe pipe = intel_crtc->pipe;
77d22dca 5421 unsigned long mask;
74bff5f9 5422 enum transcoder transcoder = crtc_state->cpu_transcoder;
77d22dca 5423
74bff5f9 5424 if (!crtc_state->base.active)
292b990e
ML
5425 return 0;
5426
77d22dca
ID
5427 mask = BIT(POWER_DOMAIN_PIPE(pipe));
5428 mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
74bff5f9
ML
5429 if (crtc_state->pch_pfit.enabled ||
5430 crtc_state->pch_pfit.force_thru)
77d22dca
ID
5431 mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
5432
74bff5f9
ML
5433 drm_for_each_encoder_mask(encoder, dev, crtc_state->base.encoder_mask) {
5434 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
5435
319be8ae 5436 mask |= BIT(intel_display_port_power_domain(intel_encoder));
74bff5f9 5437 }
319be8ae 5438
15e7ec29
ML
5439 if (crtc_state->shared_dpll)
5440 mask |= BIT(POWER_DOMAIN_PLLS);
5441
77d22dca
ID
5442 return mask;
5443}
5444
74bff5f9
ML
5445static unsigned long
5446modeset_get_crtc_power_domains(struct drm_crtc *crtc,
5447 struct intel_crtc_state *crtc_state)
77d22dca 5448{
fac5e23e 5449 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
292b990e
ML
5450 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5451 enum intel_display_power_domain domain;
5a21b665 5452 unsigned long domains, new_domains, old_domains;
77d22dca 5453
292b990e 5454 old_domains = intel_crtc->enabled_power_domains;
74bff5f9
ML
5455 intel_crtc->enabled_power_domains = new_domains =
5456 get_crtc_power_domains(crtc, crtc_state);
77d22dca 5457
5a21b665 5458 domains = new_domains & ~old_domains;
292b990e
ML
5459
5460 for_each_power_domain(domain, domains)
5461 intel_display_power_get(dev_priv, domain);
5462
5a21b665 5463 return old_domains & ~new_domains;
292b990e
ML
5464}
5465
5466static void modeset_put_power_domains(struct drm_i915_private *dev_priv,
5467 unsigned long domains)
5468{
5469 enum intel_display_power_domain domain;
5470
5471 for_each_power_domain(domain, domains)
5472 intel_display_power_put(dev_priv, domain);
5473}
77d22dca 5474
adafdc6f
MK
5475static int intel_compute_max_dotclk(struct drm_i915_private *dev_priv)
5476{
5477 int max_cdclk_freq = dev_priv->max_cdclk_freq;
5478
5479 if (INTEL_INFO(dev_priv)->gen >= 9 ||
5480 IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
5481 return max_cdclk_freq;
5482 else if (IS_CHERRYVIEW(dev_priv))
5483 return max_cdclk_freq*95/100;
5484 else if (INTEL_INFO(dev_priv)->gen < 4)
5485 return 2*max_cdclk_freq*90/100;
5486 else
5487 return max_cdclk_freq*90/100;
5488}
5489
b2045352
VS
5490static int skl_calc_cdclk(int max_pixclk, int vco);
5491
560a7ae4
DL
5492static void intel_update_max_cdclk(struct drm_device *dev)
5493{
fac5e23e 5494 struct drm_i915_private *dev_priv = to_i915(dev);
560a7ae4 5495
ef11bdb3 5496 if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) {
560a7ae4 5497 u32 limit = I915_READ(SKL_DFSM) & SKL_DFSM_CDCLK_LIMIT_MASK;
b2045352
VS
5498 int max_cdclk, vco;
5499
5500 vco = dev_priv->skl_preferred_vco_freq;
63911d72 5501 WARN_ON(vco != 8100000 && vco != 8640000);
560a7ae4 5502
b2045352
VS
5503 /*
5504 * Use the lower (vco 8640) cdclk values as a
5505 * first guess. skl_calc_cdclk() will correct it
5506 * if the preferred vco is 8100 instead.
5507 */
560a7ae4 5508 if (limit == SKL_DFSM_CDCLK_LIMIT_675)
487ed2e4 5509 max_cdclk = 617143;
560a7ae4 5510 else if (limit == SKL_DFSM_CDCLK_LIMIT_540)
b2045352 5511 max_cdclk = 540000;
560a7ae4 5512 else if (limit == SKL_DFSM_CDCLK_LIMIT_450)
b2045352 5513 max_cdclk = 432000;
560a7ae4 5514 else
487ed2e4 5515 max_cdclk = 308571;
b2045352
VS
5516
5517 dev_priv->max_cdclk_freq = skl_calc_cdclk(max_cdclk, vco);
281c114f
MR
5518 } else if (IS_BROXTON(dev)) {
5519 dev_priv->max_cdclk_freq = 624000;
560a7ae4
DL
5520 } else if (IS_BROADWELL(dev)) {
5521 /*
5522 * FIXME with extra cooling we can allow
5523 * 540 MHz for ULX and 675 Mhz for ULT.
5524 * How can we know if extra cooling is
5525 * available? PCI ID, VTB, something else?
5526 */
5527 if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
5528 dev_priv->max_cdclk_freq = 450000;
5529 else if (IS_BDW_ULX(dev))
5530 dev_priv->max_cdclk_freq = 450000;
5531 else if (IS_BDW_ULT(dev))
5532 dev_priv->max_cdclk_freq = 540000;
5533 else
5534 dev_priv->max_cdclk_freq = 675000;
0904deaf
MK
5535 } else if (IS_CHERRYVIEW(dev)) {
5536 dev_priv->max_cdclk_freq = 320000;
560a7ae4
DL
5537 } else if (IS_VALLEYVIEW(dev)) {
5538 dev_priv->max_cdclk_freq = 400000;
5539 } else {
5540 /* otherwise assume cdclk is fixed */
5541 dev_priv->max_cdclk_freq = dev_priv->cdclk_freq;
5542 }
5543
adafdc6f
MK
5544 dev_priv->max_dotclk_freq = intel_compute_max_dotclk(dev_priv);
5545
560a7ae4
DL
5546 DRM_DEBUG_DRIVER("Max CD clock rate: %d kHz\n",
5547 dev_priv->max_cdclk_freq);
adafdc6f
MK
5548
5549 DRM_DEBUG_DRIVER("Max dotclock rate: %d kHz\n",
5550 dev_priv->max_dotclk_freq);
560a7ae4
DL
5551}
5552
5553static void intel_update_cdclk(struct drm_device *dev)
5554{
fac5e23e 5555 struct drm_i915_private *dev_priv = to_i915(dev);
560a7ae4
DL
5556
5557 dev_priv->cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
2f2a121a 5558
83d7c81f 5559 if (INTEL_GEN(dev_priv) >= 9)
709e05c3
VS
5560 DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz, VCO: %d kHz, ref: %d kHz\n",
5561 dev_priv->cdclk_freq, dev_priv->cdclk_pll.vco,
5562 dev_priv->cdclk_pll.ref);
2f2a121a
VS
5563 else
5564 DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
5565 dev_priv->cdclk_freq);
560a7ae4
DL
5566
5567 /*
b5d99ff9
VS
5568 * 9:0 CMBUS [sic] CDCLK frequency (cdfreq):
5569 * Programmng [sic] note: bit[9:2] should be programmed to the number
5570 * of cdclk that generates 4MHz reference clock freq which is used to
5571 * generate GMBus clock. This will vary with the cdclk freq.
560a7ae4 5572 */
b5d99ff9 5573 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
560a7ae4 5574 I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->cdclk_freq, 1000));
560a7ae4
DL
5575}
5576
92891e45
VS
5577/* convert from kHz to .1 fixpoint MHz with -1MHz offset */
5578static int skl_cdclk_decimal(int cdclk)
5579{
5580 return DIV_ROUND_CLOSEST(cdclk - 1000, 500);
5581}
5582
5f199dfa
VS
5583static int bxt_de_pll_vco(struct drm_i915_private *dev_priv, int cdclk)
5584{
5585 int ratio;
5586
5587 if (cdclk == dev_priv->cdclk_pll.ref)
5588 return 0;
5589
5590 switch (cdclk) {
5591 default:
5592 MISSING_CASE(cdclk);
5593 case 144000:
5594 case 288000:
5595 case 384000:
5596 case 576000:
5597 ratio = 60;
5598 break;
5599 case 624000:
5600 ratio = 65;
5601 break;
5602 }
5603
5604 return dev_priv->cdclk_pll.ref * ratio;
5605}
5606
2b73001e
VS
5607static void bxt_de_pll_disable(struct drm_i915_private *dev_priv)
5608{
5609 I915_WRITE(BXT_DE_PLL_ENABLE, 0);
5610
5611 /* Timeout 200us */
95cac283
CW
5612 if (intel_wait_for_register(dev_priv,
5613 BXT_DE_PLL_ENABLE, BXT_DE_PLL_LOCK, 0,
5614 1))
2b73001e 5615 DRM_ERROR("timeout waiting for DE PLL unlock\n");
83d7c81f
VS
5616
5617 dev_priv->cdclk_pll.vco = 0;
2b73001e
VS
5618}
5619
5f199dfa 5620static void bxt_de_pll_enable(struct drm_i915_private *dev_priv, int vco)
2b73001e 5621{
5f199dfa 5622 int ratio = DIV_ROUND_CLOSEST(vco, dev_priv->cdclk_pll.ref);
2b73001e
VS
5623 u32 val;
5624
5625 val = I915_READ(BXT_DE_PLL_CTL);
5626 val &= ~BXT_DE_PLL_RATIO_MASK;
5f199dfa 5627 val |= BXT_DE_PLL_RATIO(ratio);
2b73001e
VS
5628 I915_WRITE(BXT_DE_PLL_CTL, val);
5629
5630 I915_WRITE(BXT_DE_PLL_ENABLE, BXT_DE_PLL_PLL_ENABLE);
5631
5632 /* Timeout 200us */
e084e1b9
CW
5633 if (intel_wait_for_register(dev_priv,
5634 BXT_DE_PLL_ENABLE,
5635 BXT_DE_PLL_LOCK,
5636 BXT_DE_PLL_LOCK,
5637 1))
2b73001e 5638 DRM_ERROR("timeout waiting for DE PLL lock\n");
83d7c81f 5639
5f199dfa 5640 dev_priv->cdclk_pll.vco = vco;
2b73001e
VS
5641}
5642
324513c0 5643static void bxt_set_cdclk(struct drm_i915_private *dev_priv, int cdclk)
f8437dd1 5644{
5f199dfa
VS
5645 u32 val, divider;
5646 int vco, ret;
f8437dd1 5647
5f199dfa
VS
5648 vco = bxt_de_pll_vco(dev_priv, cdclk);
5649
5650 DRM_DEBUG_DRIVER("Changing CDCLK to %d kHz (VCO %d kHz)\n", cdclk, vco);
5651
5652 /* cdclk = vco / 2 / div{1,1.5,2,4} */
5653 switch (DIV_ROUND_CLOSEST(vco, cdclk)) {
5654 case 8:
f8437dd1 5655 divider = BXT_CDCLK_CD2X_DIV_SEL_4;
f8437dd1 5656 break;
5f199dfa 5657 case 4:
f8437dd1 5658 divider = BXT_CDCLK_CD2X_DIV_SEL_2;
f8437dd1 5659 break;
5f199dfa 5660 case 3:
f8437dd1 5661 divider = BXT_CDCLK_CD2X_DIV_SEL_1_5;
f8437dd1 5662 break;
5f199dfa 5663 case 2:
f8437dd1 5664 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
f8437dd1
VK
5665 break;
5666 default:
5f199dfa
VS
5667 WARN_ON(cdclk != dev_priv->cdclk_pll.ref);
5668 WARN_ON(vco != 0);
f8437dd1 5669
5f199dfa
VS
5670 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
5671 break;
f8437dd1
VK
5672 }
5673
f8437dd1 5674 /* Inform power controller of upcoming frequency change */
5f199dfa 5675 mutex_lock(&dev_priv->rps.hw_lock);
f8437dd1
VK
5676 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
5677 0x80000000);
5678 mutex_unlock(&dev_priv->rps.hw_lock);
5679
5680 if (ret) {
5681 DRM_ERROR("PCode CDCLK freq change notify failed (err %d, freq %d)\n",
9ef56154 5682 ret, cdclk);
f8437dd1
VK
5683 return;
5684 }
5685
5f199dfa
VS
5686 if (dev_priv->cdclk_pll.vco != 0 &&
5687 dev_priv->cdclk_pll.vco != vco)
2b73001e 5688 bxt_de_pll_disable(dev_priv);
f8437dd1 5689
5f199dfa
VS
5690 if (dev_priv->cdclk_pll.vco != vco)
5691 bxt_de_pll_enable(dev_priv, vco);
f8437dd1 5692
5f199dfa
VS
5693 val = divider | skl_cdclk_decimal(cdclk);
5694 /*
5695 * FIXME if only the cd2x divider needs changing, it could be done
5696 * without shutting off the pipe (if only one pipe is active).
5697 */
5698 val |= BXT_CDCLK_CD2X_PIPE_NONE;
5699 /*
5700 * Disable SSA Precharge when CD clock frequency < 500 MHz,
5701 * enable otherwise.
5702 */
5703 if (cdclk >= 500000)
5704 val |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
5705 I915_WRITE(CDCLK_CTL, val);
f8437dd1
VK
5706
5707 mutex_lock(&dev_priv->rps.hw_lock);
5708 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
9ef56154 5709 DIV_ROUND_UP(cdclk, 25000));
f8437dd1
VK
5710 mutex_unlock(&dev_priv->rps.hw_lock);
5711
5712 if (ret) {
5713 DRM_ERROR("PCode CDCLK freq set failed, (err %d, freq %d)\n",
9ef56154 5714 ret, cdclk);
f8437dd1
VK
5715 return;
5716 }
5717
91c8a326 5718 intel_update_cdclk(&dev_priv->drm);
f8437dd1
VK
5719}
5720
d66a2194 5721static void bxt_sanitize_cdclk(struct drm_i915_private *dev_priv)
f8437dd1 5722{
d66a2194
ID
5723 u32 cdctl, expected;
5724
91c8a326 5725 intel_update_cdclk(&dev_priv->drm);
f8437dd1 5726
d66a2194
ID
5727 if (dev_priv->cdclk_pll.vco == 0 ||
5728 dev_priv->cdclk_freq == dev_priv->cdclk_pll.ref)
5729 goto sanitize;
5730
5731 /* DPLL okay; verify the cdclock
5732 *
5733 * Some BIOS versions leave an incorrect decimal frequency value and
5734 * set reserved MBZ bits in CDCLK_CTL at least during exiting from S4,
5735 * so sanitize this register.
5736 */
5737 cdctl = I915_READ(CDCLK_CTL);
5738 /*
5739 * Let's ignore the pipe field, since BIOS could have configured the
5740 * dividers both synching to an active pipe, or asynchronously
5741 * (PIPE_NONE).
5742 */
5743 cdctl &= ~BXT_CDCLK_CD2X_PIPE_NONE;
5744
5745 expected = (cdctl & BXT_CDCLK_CD2X_DIV_SEL_MASK) |
5746 skl_cdclk_decimal(dev_priv->cdclk_freq);
5747 /*
5748 * Disable SSA Precharge when CD clock frequency < 500 MHz,
5749 * enable otherwise.
5750 */
5751 if (dev_priv->cdclk_freq >= 500000)
5752 expected |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
5753
5754 if (cdctl == expected)
5755 /* All well; nothing to sanitize */
5756 return;
5757
5758sanitize:
5759 DRM_DEBUG_KMS("Sanitizing cdclk programmed by pre-os\n");
5760
5761 /* force cdclk programming */
5762 dev_priv->cdclk_freq = 0;
5763
5764 /* force full PLL disable + enable */
5765 dev_priv->cdclk_pll.vco = -1;
5766}
5767
324513c0 5768void bxt_init_cdclk(struct drm_i915_private *dev_priv)
d66a2194
ID
5769{
5770 bxt_sanitize_cdclk(dev_priv);
5771
5772 if (dev_priv->cdclk_freq != 0 && dev_priv->cdclk_pll.vco != 0)
089c6fd5 5773 return;
c2e001ef 5774
f8437dd1
VK
5775 /*
5776 * FIXME:
5777 * - The initial CDCLK needs to be read from VBT.
5778 * Need to make this change after VBT has changes for BXT.
f8437dd1 5779 */
324513c0 5780 bxt_set_cdclk(dev_priv, bxt_calc_cdclk(0));
f8437dd1
VK
5781}
5782
324513c0 5783void bxt_uninit_cdclk(struct drm_i915_private *dev_priv)
f8437dd1 5784{
324513c0 5785 bxt_set_cdclk(dev_priv, dev_priv->cdclk_pll.ref);
f8437dd1
VK
5786}
5787
a8ca4934
VS
5788static int skl_calc_cdclk(int max_pixclk, int vco)
5789{
63911d72 5790 if (vco == 8640000) {
a8ca4934 5791 if (max_pixclk > 540000)
487ed2e4 5792 return 617143;
a8ca4934
VS
5793 else if (max_pixclk > 432000)
5794 return 540000;
487ed2e4 5795 else if (max_pixclk > 308571)
a8ca4934
VS
5796 return 432000;
5797 else
487ed2e4 5798 return 308571;
a8ca4934 5799 } else {
a8ca4934
VS
5800 if (max_pixclk > 540000)
5801 return 675000;
5802 else if (max_pixclk > 450000)
5803 return 540000;
5804 else if (max_pixclk > 337500)
5805 return 450000;
5806 else
5807 return 337500;
5808 }
5809}
5810
ea61791e
VS
5811static void
5812skl_dpll0_update(struct drm_i915_private *dev_priv)
5d96d8af 5813{
ea61791e 5814 u32 val;
5d96d8af 5815
709e05c3 5816 dev_priv->cdclk_pll.ref = 24000;
1c3f7700 5817 dev_priv->cdclk_pll.vco = 0;
709e05c3 5818
ea61791e 5819 val = I915_READ(LCPLL1_CTL);
1c3f7700 5820 if ((val & LCPLL_PLL_ENABLE) == 0)
ea61791e 5821 return;
5d96d8af 5822
1c3f7700
ID
5823 if (WARN_ON((val & LCPLL_PLL_LOCK) == 0))
5824 return;
9f7eb31a 5825
ea61791e
VS
5826 val = I915_READ(DPLL_CTRL1);
5827
1c3f7700
ID
5828 if (WARN_ON((val & (DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) |
5829 DPLL_CTRL1_SSC(SKL_DPLL0) |
5830 DPLL_CTRL1_OVERRIDE(SKL_DPLL0))) !=
5831 DPLL_CTRL1_OVERRIDE(SKL_DPLL0)))
5832 return;
9f7eb31a 5833
ea61791e
VS
5834 switch (val & DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0)) {
5835 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810, SKL_DPLL0):
5836 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1350, SKL_DPLL0):
5837 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1620, SKL_DPLL0):
5838 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_2700, SKL_DPLL0):
63911d72 5839 dev_priv->cdclk_pll.vco = 8100000;
ea61791e
VS
5840 break;
5841 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080, SKL_DPLL0):
5842 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_2160, SKL_DPLL0):
63911d72 5843 dev_priv->cdclk_pll.vco = 8640000;
ea61791e
VS
5844 break;
5845 default:
5846 MISSING_CASE(val & DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
ea61791e
VS
5847 break;
5848 }
5d96d8af
DL
5849}
5850
b2045352
VS
5851void skl_set_preferred_cdclk_vco(struct drm_i915_private *dev_priv, int vco)
5852{
5853 bool changed = dev_priv->skl_preferred_vco_freq != vco;
5854
5855 dev_priv->skl_preferred_vco_freq = vco;
5856
5857 if (changed)
91c8a326 5858 intel_update_max_cdclk(&dev_priv->drm);
b2045352
VS
5859}
5860
5d96d8af 5861static void
3861fc60 5862skl_dpll0_enable(struct drm_i915_private *dev_priv, int vco)
5d96d8af 5863{
a8ca4934 5864 int min_cdclk = skl_calc_cdclk(0, vco);
5d96d8af
DL
5865 u32 val;
5866
63911d72 5867 WARN_ON(vco != 8100000 && vco != 8640000);
b2045352 5868
5d96d8af 5869 /* select the minimum CDCLK before enabling DPLL 0 */
9ef56154 5870 val = CDCLK_FREQ_337_308 | skl_cdclk_decimal(min_cdclk);
5d96d8af
DL
5871 I915_WRITE(CDCLK_CTL, val);
5872 POSTING_READ(CDCLK_CTL);
5873
5874 /*
5875 * We always enable DPLL0 with the lowest link rate possible, but still
5876 * taking into account the VCO required to operate the eDP panel at the
5877 * desired frequency. The usual DP link rates operate with a VCO of
5878 * 8100 while the eDP 1.4 alternate link rates need a VCO of 8640.
5879 * The modeset code is responsible for the selection of the exact link
5880 * rate later on, with the constraint of choosing a frequency that
a8ca4934 5881 * works with vco.
5d96d8af
DL
5882 */
5883 val = I915_READ(DPLL_CTRL1);
5884
5885 val &= ~(DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) | DPLL_CTRL1_SSC(SKL_DPLL0) |
5886 DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
5887 val |= DPLL_CTRL1_OVERRIDE(SKL_DPLL0);
63911d72 5888 if (vco == 8640000)
5d96d8af
DL
5889 val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080,
5890 SKL_DPLL0);
5891 else
5892 val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810,
5893 SKL_DPLL0);
5894
5895 I915_WRITE(DPLL_CTRL1, val);
5896 POSTING_READ(DPLL_CTRL1);
5897
5898 I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) | LCPLL_PLL_ENABLE);
5899
e24ca054
CW
5900 if (intel_wait_for_register(dev_priv,
5901 LCPLL1_CTL, LCPLL_PLL_LOCK, LCPLL_PLL_LOCK,
5902 5))
5d96d8af 5903 DRM_ERROR("DPLL0 not locked\n");
1cd593e0 5904
63911d72 5905 dev_priv->cdclk_pll.vco = vco;
b2045352
VS
5906
5907 /* We'll want to keep using the current vco from now on. */
5908 skl_set_preferred_cdclk_vco(dev_priv, vco);
5d96d8af
DL
5909}
5910
430e05de
VS
5911static void
5912skl_dpll0_disable(struct drm_i915_private *dev_priv)
5913{
5914 I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) & ~LCPLL_PLL_ENABLE);
8ad32a05
CW
5915 if (intel_wait_for_register(dev_priv,
5916 LCPLL1_CTL, LCPLL_PLL_LOCK, 0,
5917 1))
430e05de 5918 DRM_ERROR("Couldn't disable DPLL0\n");
1cd593e0 5919
63911d72 5920 dev_priv->cdclk_pll.vco = 0;
430e05de
VS
5921}
5922
5d96d8af
DL
5923static bool skl_cdclk_pcu_ready(struct drm_i915_private *dev_priv)
5924{
5925 int ret;
5926 u32 val;
5927
5928 /* inform PCU we want to change CDCLK */
5929 val = SKL_CDCLK_PREPARE_FOR_CHANGE;
5930 mutex_lock(&dev_priv->rps.hw_lock);
5931 ret = sandybridge_pcode_read(dev_priv, SKL_PCODE_CDCLK_CONTROL, &val);
5932 mutex_unlock(&dev_priv->rps.hw_lock);
5933
5934 return ret == 0 && (val & SKL_CDCLK_READY_FOR_CHANGE);
5935}
5936
5937static bool skl_cdclk_wait_for_pcu_ready(struct drm_i915_private *dev_priv)
5938{
848496e5 5939 return _wait_for(skl_cdclk_pcu_ready(dev_priv), 3000, 10) == 0;
5d96d8af
DL
5940}
5941
1cd593e0 5942static void skl_set_cdclk(struct drm_i915_private *dev_priv, int cdclk, int vco)
5d96d8af 5943{
91c8a326 5944 struct drm_device *dev = &dev_priv->drm;
5d96d8af
DL
5945 u32 freq_select, pcu_ack;
5946
1cd593e0
VS
5947 WARN_ON((cdclk == 24000) != (vco == 0));
5948
63911d72 5949 DRM_DEBUG_DRIVER("Changing CDCLK to %d kHz (VCO %d kHz)\n", cdclk, vco);
5d96d8af
DL
5950
5951 if (!skl_cdclk_wait_for_pcu_ready(dev_priv)) {
5952 DRM_ERROR("failed to inform PCU about cdclk change\n");
5953 return;
5954 }
5955
5956 /* set CDCLK_CTL */
9ef56154 5957 switch (cdclk) {
5d96d8af
DL
5958 case 450000:
5959 case 432000:
5960 freq_select = CDCLK_FREQ_450_432;
5961 pcu_ack = 1;
5962 break;
5963 case 540000:
5964 freq_select = CDCLK_FREQ_540;
5965 pcu_ack = 2;
5966 break;
487ed2e4 5967 case 308571:
5d96d8af
DL
5968 case 337500:
5969 default:
5970 freq_select = CDCLK_FREQ_337_308;
5971 pcu_ack = 0;
5972 break;
487ed2e4 5973 case 617143:
5d96d8af
DL
5974 case 675000:
5975 freq_select = CDCLK_FREQ_675_617;
5976 pcu_ack = 3;
5977 break;
5978 }
5979
63911d72
VS
5980 if (dev_priv->cdclk_pll.vco != 0 &&
5981 dev_priv->cdclk_pll.vco != vco)
1cd593e0
VS
5982 skl_dpll0_disable(dev_priv);
5983
63911d72 5984 if (dev_priv->cdclk_pll.vco != vco)
1cd593e0
VS
5985 skl_dpll0_enable(dev_priv, vco);
5986
9ef56154 5987 I915_WRITE(CDCLK_CTL, freq_select | skl_cdclk_decimal(cdclk));
5d96d8af
DL
5988 POSTING_READ(CDCLK_CTL);
5989
5990 /* inform PCU of the change */
5991 mutex_lock(&dev_priv->rps.hw_lock);
5992 sandybridge_pcode_write(dev_priv, SKL_PCODE_CDCLK_CONTROL, pcu_ack);
5993 mutex_unlock(&dev_priv->rps.hw_lock);
560a7ae4
DL
5994
5995 intel_update_cdclk(dev);
5d96d8af
DL
5996}
5997
9f7eb31a
VS
5998static void skl_sanitize_cdclk(struct drm_i915_private *dev_priv);
5999
5d96d8af
DL
6000void skl_uninit_cdclk(struct drm_i915_private *dev_priv)
6001{
709e05c3 6002 skl_set_cdclk(dev_priv, dev_priv->cdclk_pll.ref, 0);
5d96d8af
DL
6003}
6004
6005void skl_init_cdclk(struct drm_i915_private *dev_priv)
6006{
9f7eb31a
VS
6007 int cdclk, vco;
6008
6009 skl_sanitize_cdclk(dev_priv);
5d96d8af 6010
63911d72 6011 if (dev_priv->cdclk_freq != 0 && dev_priv->cdclk_pll.vco != 0) {
9f7eb31a
VS
6012 /*
6013 * Use the current vco as our initial
6014 * guess as to what the preferred vco is.
6015 */
6016 if (dev_priv->skl_preferred_vco_freq == 0)
6017 skl_set_preferred_cdclk_vco(dev_priv,
63911d72 6018 dev_priv->cdclk_pll.vco);
70c2c184 6019 return;
1cd593e0 6020 }
5d96d8af 6021
70c2c184
VS
6022 vco = dev_priv->skl_preferred_vco_freq;
6023 if (vco == 0)
63911d72 6024 vco = 8100000;
70c2c184 6025 cdclk = skl_calc_cdclk(0, vco);
5d96d8af 6026
70c2c184 6027 skl_set_cdclk(dev_priv, cdclk, vco);
5d96d8af
DL
6028}
6029
9f7eb31a 6030static void skl_sanitize_cdclk(struct drm_i915_private *dev_priv)
c73666f3 6031{
09492498 6032 uint32_t cdctl, expected;
c73666f3 6033
f1b391a5
SK
6034 /*
6035 * check if the pre-os intialized the display
6036 * There is SWF18 scratchpad register defined which is set by the
6037 * pre-os which can be used by the OS drivers to check the status
6038 */
6039 if ((I915_READ(SWF_ILK(0x18)) & 0x00FFFFFF) == 0)
6040 goto sanitize;
6041
91c8a326 6042 intel_update_cdclk(&dev_priv->drm);
c73666f3 6043 /* Is PLL enabled and locked ? */
1c3f7700
ID
6044 if (dev_priv->cdclk_pll.vco == 0 ||
6045 dev_priv->cdclk_freq == dev_priv->cdclk_pll.ref)
c73666f3
SK
6046 goto sanitize;
6047
6048 /* DPLL okay; verify the cdclock
6049 *
6050 * Noticed in some instances that the freq selection is correct but
6051 * decimal part is programmed wrong from BIOS where pre-os does not
6052 * enable display. Verify the same as well.
6053 */
09492498
VS
6054 cdctl = I915_READ(CDCLK_CTL);
6055 expected = (cdctl & CDCLK_FREQ_SEL_MASK) |
6056 skl_cdclk_decimal(dev_priv->cdclk_freq);
6057 if (cdctl == expected)
c73666f3 6058 /* All well; nothing to sanitize */
9f7eb31a 6059 return;
c89e39f3 6060
9f7eb31a
VS
6061sanitize:
6062 DRM_DEBUG_KMS("Sanitizing cdclk programmed by pre-os\n");
c73666f3 6063
9f7eb31a
VS
6064 /* force cdclk programming */
6065 dev_priv->cdclk_freq = 0;
6066 /* force full PLL disable + enable */
63911d72 6067 dev_priv->cdclk_pll.vco = -1;
c73666f3
SK
6068}
6069
30a970c6
JB
6070/* Adjust CDclk dividers to allow high res or save power if possible */
6071static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
6072{
fac5e23e 6073 struct drm_i915_private *dev_priv = to_i915(dev);
30a970c6
JB
6074 u32 val, cmd;
6075
164dfd28
VK
6076 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
6077 != dev_priv->cdclk_freq);
d60c4473 6078
dfcab17e 6079 if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
30a970c6 6080 cmd = 2;
dfcab17e 6081 else if (cdclk == 266667)
30a970c6
JB
6082 cmd = 1;
6083 else
6084 cmd = 0;
6085
6086 mutex_lock(&dev_priv->rps.hw_lock);
6087 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
6088 val &= ~DSPFREQGUAR_MASK;
6089 val |= (cmd << DSPFREQGUAR_SHIFT);
6090 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
6091 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
6092 DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
6093 50)) {
6094 DRM_ERROR("timed out waiting for CDclk change\n");
6095 }
6096 mutex_unlock(&dev_priv->rps.hw_lock);
6097
54433e91
VS
6098 mutex_lock(&dev_priv->sb_lock);
6099
dfcab17e 6100 if (cdclk == 400000) {
6bcda4f0 6101 u32 divider;
30a970c6 6102
6bcda4f0 6103 divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
30a970c6 6104
30a970c6
JB
6105 /* adjust cdclk divider */
6106 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
87d5d259 6107 val &= ~CCK_FREQUENCY_VALUES;
30a970c6
JB
6108 val |= divider;
6109 vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
a877e801
VS
6110
6111 if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
87d5d259 6112 CCK_FREQUENCY_STATUS) == (divider << CCK_FREQUENCY_STATUS_SHIFT),
a877e801
VS
6113 50))
6114 DRM_ERROR("timed out waiting for CDclk change\n");
30a970c6
JB
6115 }
6116
30a970c6
JB
6117 /* adjust self-refresh exit latency value */
6118 val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
6119 val &= ~0x7f;
6120
6121 /*
6122 * For high bandwidth configs, we set a higher latency in the bunit
6123 * so that the core display fetch happens in time to avoid underruns.
6124 */
dfcab17e 6125 if (cdclk == 400000)
30a970c6
JB
6126 val |= 4500 / 250; /* 4.5 usec */
6127 else
6128 val |= 3000 / 250; /* 3.0 usec */
6129 vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
54433e91 6130
a580516d 6131 mutex_unlock(&dev_priv->sb_lock);
30a970c6 6132
b6283055 6133 intel_update_cdclk(dev);
30a970c6
JB
6134}
6135
383c5a6a
VS
6136static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
6137{
fac5e23e 6138 struct drm_i915_private *dev_priv = to_i915(dev);
383c5a6a
VS
6139 u32 val, cmd;
6140
164dfd28
VK
6141 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
6142 != dev_priv->cdclk_freq);
383c5a6a
VS
6143
6144 switch (cdclk) {
383c5a6a
VS
6145 case 333333:
6146 case 320000:
383c5a6a 6147 case 266667:
383c5a6a 6148 case 200000:
383c5a6a
VS
6149 break;
6150 default:
5f77eeb0 6151 MISSING_CASE(cdclk);
383c5a6a
VS
6152 return;
6153 }
6154
9d0d3fda
VS
6155 /*
6156 * Specs are full of misinformation, but testing on actual
6157 * hardware has shown that we just need to write the desired
6158 * CCK divider into the Punit register.
6159 */
6160 cmd = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
6161
383c5a6a
VS
6162 mutex_lock(&dev_priv->rps.hw_lock);
6163 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
6164 val &= ~DSPFREQGUAR_MASK_CHV;
6165 val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
6166 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
6167 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
6168 DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
6169 50)) {
6170 DRM_ERROR("timed out waiting for CDclk change\n");
6171 }
6172 mutex_unlock(&dev_priv->rps.hw_lock);
6173
b6283055 6174 intel_update_cdclk(dev);
383c5a6a
VS
6175}
6176
30a970c6
JB
6177static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
6178 int max_pixclk)
6179{
6bcda4f0 6180 int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
6cca3195 6181 int limit = IS_CHERRYVIEW(dev_priv) ? 95 : 90;
29dc7ef3 6182
30a970c6
JB
6183 /*
6184 * Really only a few cases to deal with, as only 4 CDclks are supported:
6185 * 200MHz
6186 * 267MHz
29dc7ef3 6187 * 320/333MHz (depends on HPLL freq)
6cca3195
VS
6188 * 400MHz (VLV only)
6189 * So we check to see whether we're above 90% (VLV) or 95% (CHV)
6190 * of the lower bin and adjust if needed.
e37c67a1
VS
6191 *
6192 * We seem to get an unstable or solid color picture at 200MHz.
6193 * Not sure what's wrong. For now use 200MHz only when all pipes
6194 * are off.
30a970c6 6195 */
6cca3195
VS
6196 if (!IS_CHERRYVIEW(dev_priv) &&
6197 max_pixclk > freq_320*limit/100)
dfcab17e 6198 return 400000;
6cca3195 6199 else if (max_pixclk > 266667*limit/100)
29dc7ef3 6200 return freq_320;
e37c67a1 6201 else if (max_pixclk > 0)
dfcab17e 6202 return 266667;
e37c67a1
VS
6203 else
6204 return 200000;
30a970c6
JB
6205}
6206
324513c0 6207static int bxt_calc_cdclk(int max_pixclk)
f8437dd1 6208{
760e1477 6209 if (max_pixclk > 576000)
f8437dd1 6210 return 624000;
760e1477 6211 else if (max_pixclk > 384000)
f8437dd1 6212 return 576000;
760e1477 6213 else if (max_pixclk > 288000)
f8437dd1 6214 return 384000;
760e1477 6215 else if (max_pixclk > 144000)
f8437dd1
VK
6216 return 288000;
6217 else
6218 return 144000;
6219}
6220
e8788cbc 6221/* Compute the max pixel clock for new configuration. */
a821fc46
ACO
6222static int intel_mode_max_pixclk(struct drm_device *dev,
6223 struct drm_atomic_state *state)
30a970c6 6224{
565602d7 6225 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
fac5e23e 6226 struct drm_i915_private *dev_priv = to_i915(dev);
565602d7
ML
6227 struct drm_crtc *crtc;
6228 struct drm_crtc_state *crtc_state;
6229 unsigned max_pixclk = 0, i;
6230 enum pipe pipe;
30a970c6 6231
565602d7
ML
6232 memcpy(intel_state->min_pixclk, dev_priv->min_pixclk,
6233 sizeof(intel_state->min_pixclk));
304603f4 6234
565602d7
ML
6235 for_each_crtc_in_state(state, crtc, crtc_state, i) {
6236 int pixclk = 0;
6237
6238 if (crtc_state->enable)
6239 pixclk = crtc_state->adjusted_mode.crtc_clock;
304603f4 6240
565602d7 6241 intel_state->min_pixclk[i] = pixclk;
30a970c6
JB
6242 }
6243
565602d7
ML
6244 for_each_pipe(dev_priv, pipe)
6245 max_pixclk = max(intel_state->min_pixclk[pipe], max_pixclk);
6246
30a970c6
JB
6247 return max_pixclk;
6248}
6249
27c329ed 6250static int valleyview_modeset_calc_cdclk(struct drm_atomic_state *state)
30a970c6 6251{
27c329ed 6252 struct drm_device *dev = state->dev;
fac5e23e 6253 struct drm_i915_private *dev_priv = to_i915(dev);
27c329ed 6254 int max_pixclk = intel_mode_max_pixclk(dev, state);
1a617b77
ML
6255 struct intel_atomic_state *intel_state =
6256 to_intel_atomic_state(state);
30a970c6 6257
1a617b77 6258 intel_state->cdclk = intel_state->dev_cdclk =
27c329ed 6259 valleyview_calc_cdclk(dev_priv, max_pixclk);
0a9ab303 6260
1a617b77
ML
6261 if (!intel_state->active_crtcs)
6262 intel_state->dev_cdclk = valleyview_calc_cdclk(dev_priv, 0);
6263
27c329ed
ML
6264 return 0;
6265}
304603f4 6266
324513c0 6267static int bxt_modeset_calc_cdclk(struct drm_atomic_state *state)
27c329ed 6268{
4e5ca60f 6269 int max_pixclk = ilk_max_pixel_rate(state);
1a617b77
ML
6270 struct intel_atomic_state *intel_state =
6271 to_intel_atomic_state(state);
85a96e7a 6272
1a617b77 6273 intel_state->cdclk = intel_state->dev_cdclk =
324513c0 6274 bxt_calc_cdclk(max_pixclk);
85a96e7a 6275
1a617b77 6276 if (!intel_state->active_crtcs)
324513c0 6277 intel_state->dev_cdclk = bxt_calc_cdclk(0);
1a617b77 6278
27c329ed 6279 return 0;
30a970c6
JB
6280}
6281
1e69cd74
VS
6282static void vlv_program_pfi_credits(struct drm_i915_private *dev_priv)
6283{
6284 unsigned int credits, default_credits;
6285
6286 if (IS_CHERRYVIEW(dev_priv))
6287 default_credits = PFI_CREDIT(12);
6288 else
6289 default_credits = PFI_CREDIT(8);
6290
bfa7df01 6291 if (dev_priv->cdclk_freq >= dev_priv->czclk_freq) {
1e69cd74
VS
6292 /* CHV suggested value is 31 or 63 */
6293 if (IS_CHERRYVIEW(dev_priv))
fcc0008f 6294 credits = PFI_CREDIT_63;
1e69cd74
VS
6295 else
6296 credits = PFI_CREDIT(15);
6297 } else {
6298 credits = default_credits;
6299 }
6300
6301 /*
6302 * WA - write default credits before re-programming
6303 * FIXME: should we also set the resend bit here?
6304 */
6305 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
6306 default_credits);
6307
6308 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
6309 credits | PFI_CREDIT_RESEND);
6310
6311 /*
6312 * FIXME is this guaranteed to clear
6313 * immediately or should we poll for it?
6314 */
6315 WARN_ON(I915_READ(GCI_CONTROL) & PFI_CREDIT_RESEND);
6316}
6317
27c329ed 6318static void valleyview_modeset_commit_cdclk(struct drm_atomic_state *old_state)
30a970c6 6319{
a821fc46 6320 struct drm_device *dev = old_state->dev;
fac5e23e 6321 struct drm_i915_private *dev_priv = to_i915(dev);
1a617b77
ML
6322 struct intel_atomic_state *old_intel_state =
6323 to_intel_atomic_state(old_state);
6324 unsigned req_cdclk = old_intel_state->dev_cdclk;
30a970c6 6325
27c329ed
ML
6326 /*
6327 * FIXME: We can end up here with all power domains off, yet
6328 * with a CDCLK frequency other than the minimum. To account
6329 * for this take the PIPE-A power domain, which covers the HW
6330 * blocks needed for the following programming. This can be
6331 * removed once it's guaranteed that we get here either with
6332 * the minimum CDCLK set, or the required power domains
6333 * enabled.
6334 */
6335 intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
738c05c0 6336
27c329ed
ML
6337 if (IS_CHERRYVIEW(dev))
6338 cherryview_set_cdclk(dev, req_cdclk);
6339 else
6340 valleyview_set_cdclk(dev, req_cdclk);
738c05c0 6341
27c329ed 6342 vlv_program_pfi_credits(dev_priv);
1e69cd74 6343
27c329ed 6344 intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
30a970c6
JB
6345}
6346
89b667f8
JB
6347static void valleyview_crtc_enable(struct drm_crtc *crtc)
6348{
6349 struct drm_device *dev = crtc->dev;
a72e4c9f 6350 struct drm_i915_private *dev_priv = to_i915(dev);
89b667f8
JB
6351 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6352 struct intel_encoder *encoder;
b95c5321
ML
6353 struct intel_crtc_state *pipe_config =
6354 to_intel_crtc_state(crtc->state);
89b667f8 6355 int pipe = intel_crtc->pipe;
89b667f8 6356
53d9f4e9 6357 if (WARN_ON(intel_crtc->active))
89b667f8
JB
6358 return;
6359
37a5650b 6360 if (intel_crtc_has_dp_encoder(intel_crtc->config))
fe3cd48d 6361 intel_dp_set_m_n(intel_crtc, M1_N1);
5b18e57c
DV
6362
6363 intel_set_pipe_timings(intel_crtc);
bc58be60 6364 intel_set_pipe_src_size(intel_crtc);
5b18e57c 6365
c14b0485 6366 if (IS_CHERRYVIEW(dev) && pipe == PIPE_B) {
fac5e23e 6367 struct drm_i915_private *dev_priv = to_i915(dev);
c14b0485
VS
6368
6369 I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
6370 I915_WRITE(CHV_CANVAS(pipe), 0);
6371 }
6372
5b18e57c
DV
6373 i9xx_set_pipeconf(intel_crtc);
6374
89b667f8 6375 intel_crtc->active = true;
89b667f8 6376
a72e4c9f 6377 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4a3436e8 6378
89b667f8
JB
6379 for_each_encoder_on_crtc(dev, crtc, encoder)
6380 if (encoder->pre_pll_enable)
6381 encoder->pre_pll_enable(encoder);
6382
cd2d34d9
VS
6383 if (IS_CHERRYVIEW(dev)) {
6384 chv_prepare_pll(intel_crtc, intel_crtc->config);
6385 chv_enable_pll(intel_crtc, intel_crtc->config);
6386 } else {
6387 vlv_prepare_pll(intel_crtc, intel_crtc->config);
6388 vlv_enable_pll(intel_crtc, intel_crtc->config);
9d556c99 6389 }
89b667f8
JB
6390
6391 for_each_encoder_on_crtc(dev, crtc, encoder)
6392 if (encoder->pre_enable)
6393 encoder->pre_enable(encoder);
6394
2dd24552
JB
6395 i9xx_pfit_enable(intel_crtc);
6396
b95c5321 6397 intel_color_load_luts(&pipe_config->base);
63cbb074 6398
caed361d 6399 intel_update_watermarks(crtc);
e1fdc473 6400 intel_enable_pipe(intel_crtc);
be6a6f8e 6401
4b3a9526
VS
6402 assert_vblank_disabled(crtc);
6403 drm_crtc_vblank_on(crtc);
6404
f9b61ff6
DV
6405 for_each_encoder_on_crtc(dev, crtc, encoder)
6406 encoder->enable(encoder);
89b667f8
JB
6407}
6408
f13c2ef3
DV
6409static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
6410{
6411 struct drm_device *dev = crtc->base.dev;
fac5e23e 6412 struct drm_i915_private *dev_priv = to_i915(dev);
f13c2ef3 6413
6e3c9717
ACO
6414 I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
6415 I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
f13c2ef3
DV
6416}
6417
0b8765c6 6418static void i9xx_crtc_enable(struct drm_crtc *crtc)
79e53945
JB
6419{
6420 struct drm_device *dev = crtc->dev;
a72e4c9f 6421 struct drm_i915_private *dev_priv = to_i915(dev);
79e53945 6422 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 6423 struct intel_encoder *encoder;
b95c5321
ML
6424 struct intel_crtc_state *pipe_config =
6425 to_intel_crtc_state(crtc->state);
cd2d34d9 6426 enum pipe pipe = intel_crtc->pipe;
79e53945 6427
53d9f4e9 6428 if (WARN_ON(intel_crtc->active))
f7abfe8b
CW
6429 return;
6430
f13c2ef3
DV
6431 i9xx_set_pll_dividers(intel_crtc);
6432
37a5650b 6433 if (intel_crtc_has_dp_encoder(intel_crtc->config))
fe3cd48d 6434 intel_dp_set_m_n(intel_crtc, M1_N1);
5b18e57c
DV
6435
6436 intel_set_pipe_timings(intel_crtc);
bc58be60 6437 intel_set_pipe_src_size(intel_crtc);
5b18e57c 6438
5b18e57c
DV
6439 i9xx_set_pipeconf(intel_crtc);
6440
f7abfe8b 6441 intel_crtc->active = true;
6b383a7f 6442
4a3436e8 6443 if (!IS_GEN2(dev))
a72e4c9f 6444 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4a3436e8 6445
9d6d9f19
MK
6446 for_each_encoder_on_crtc(dev, crtc, encoder)
6447 if (encoder->pre_enable)
6448 encoder->pre_enable(encoder);
6449
f6736a1a
DV
6450 i9xx_enable_pll(intel_crtc);
6451
2dd24552
JB
6452 i9xx_pfit_enable(intel_crtc);
6453
b95c5321 6454 intel_color_load_luts(&pipe_config->base);
63cbb074 6455
f37fcc2a 6456 intel_update_watermarks(crtc);
e1fdc473 6457 intel_enable_pipe(intel_crtc);
be6a6f8e 6458
4b3a9526
VS
6459 assert_vblank_disabled(crtc);
6460 drm_crtc_vblank_on(crtc);
6461
f9b61ff6
DV
6462 for_each_encoder_on_crtc(dev, crtc, encoder)
6463 encoder->enable(encoder);
0b8765c6 6464}
79e53945 6465
87476d63
DV
6466static void i9xx_pfit_disable(struct intel_crtc *crtc)
6467{
6468 struct drm_device *dev = crtc->base.dev;
fac5e23e 6469 struct drm_i915_private *dev_priv = to_i915(dev);
87476d63 6470
6e3c9717 6471 if (!crtc->config->gmch_pfit.control)
328d8e82 6472 return;
87476d63 6473
328d8e82 6474 assert_pipe_disabled(dev_priv, crtc->pipe);
87476d63 6475
328d8e82
DV
6476 DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
6477 I915_READ(PFIT_CONTROL));
6478 I915_WRITE(PFIT_CONTROL, 0);
87476d63
DV
6479}
6480
0b8765c6
JB
6481static void i9xx_crtc_disable(struct drm_crtc *crtc)
6482{
6483 struct drm_device *dev = crtc->dev;
fac5e23e 6484 struct drm_i915_private *dev_priv = to_i915(dev);
0b8765c6 6485 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 6486 struct intel_encoder *encoder;
0b8765c6 6487 int pipe = intel_crtc->pipe;
ef9c3aee 6488
6304cd91
VS
6489 /*
6490 * On gen2 planes are double buffered but the pipe isn't, so we must
6491 * wait for planes to fully turn off before disabling the pipe.
6492 */
90e83e53
ACO
6493 if (IS_GEN2(dev))
6494 intel_wait_for_vblank(dev, pipe);
6304cd91 6495
4b3a9526
VS
6496 for_each_encoder_on_crtc(dev, crtc, encoder)
6497 encoder->disable(encoder);
6498
f9b61ff6
DV
6499 drm_crtc_vblank_off(crtc);
6500 assert_vblank_disabled(crtc);
6501
575f7ab7 6502 intel_disable_pipe(intel_crtc);
24a1f16d 6503
87476d63 6504 i9xx_pfit_disable(intel_crtc);
24a1f16d 6505
89b667f8
JB
6506 for_each_encoder_on_crtc(dev, crtc, encoder)
6507 if (encoder->post_disable)
6508 encoder->post_disable(encoder);
6509
d7edc4e5 6510 if (!intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_DSI)) {
076ed3b2
CML
6511 if (IS_CHERRYVIEW(dev))
6512 chv_disable_pll(dev_priv, pipe);
6513 else if (IS_VALLEYVIEW(dev))
6514 vlv_disable_pll(dev_priv, pipe);
6515 else
1c4e0274 6516 i9xx_disable_pll(intel_crtc);
076ed3b2 6517 }
0b8765c6 6518
d6db995f
VS
6519 for_each_encoder_on_crtc(dev, crtc, encoder)
6520 if (encoder->post_pll_disable)
6521 encoder->post_pll_disable(encoder);
6522
4a3436e8 6523 if (!IS_GEN2(dev))
a72e4c9f 6524 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
0b8765c6
JB
6525}
6526
b17d48e2
ML
6527static void intel_crtc_disable_noatomic(struct drm_crtc *crtc)
6528{
842e0307 6529 struct intel_encoder *encoder;
b17d48e2
ML
6530 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6531 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
6532 enum intel_display_power_domain domain;
6533 unsigned long domains;
6534
6535 if (!intel_crtc->active)
6536 return;
6537
a539205a 6538 if (to_intel_plane_state(crtc->primary->state)->visible) {
5a21b665 6539 WARN_ON(intel_crtc->flip_work);
fc32b1fd 6540
2622a081 6541 intel_pre_disable_primary_noatomic(crtc);
54a41961
ML
6542
6543 intel_crtc_disable_planes(crtc, 1 << drm_plane_index(crtc->primary));
6544 to_intel_plane_state(crtc->primary->state)->visible = false;
a539205a
ML
6545 }
6546
b17d48e2 6547 dev_priv->display.crtc_disable(crtc);
842e0307 6548
78108b7c
VS
6549 DRM_DEBUG_KMS("[CRTC:%d:%s] hw state adjusted, was enabled, now disabled\n",
6550 crtc->base.id, crtc->name);
842e0307
ML
6551
6552 WARN_ON(drm_atomic_set_mode_for_crtc(crtc->state, NULL) < 0);
6553 crtc->state->active = false;
37d9078b 6554 intel_crtc->active = false;
842e0307
ML
6555 crtc->enabled = false;
6556 crtc->state->connector_mask = 0;
6557 crtc->state->encoder_mask = 0;
6558
6559 for_each_encoder_on_crtc(crtc->dev, crtc, encoder)
6560 encoder->base.crtc = NULL;
6561
58f9c0bc 6562 intel_fbc_disable(intel_crtc);
37d9078b 6563 intel_update_watermarks(crtc);
1f7457b1 6564 intel_disable_shared_dpll(intel_crtc);
b17d48e2
ML
6565
6566 domains = intel_crtc->enabled_power_domains;
6567 for_each_power_domain(domain, domains)
6568 intel_display_power_put(dev_priv, domain);
6569 intel_crtc->enabled_power_domains = 0;
565602d7
ML
6570
6571 dev_priv->active_crtcs &= ~(1 << intel_crtc->pipe);
6572 dev_priv->min_pixclk[intel_crtc->pipe] = 0;
b17d48e2
ML
6573}
6574
6b72d486
ML
6575/*
6576 * turn all crtc's off, but do not adjust state
6577 * This has to be paired with a call to intel_modeset_setup_hw_state.
6578 */
70e0bd74 6579int intel_display_suspend(struct drm_device *dev)
ee7b9f93 6580{
e2c8b870 6581 struct drm_i915_private *dev_priv = to_i915(dev);
70e0bd74 6582 struct drm_atomic_state *state;
e2c8b870 6583 int ret;
70e0bd74 6584
e2c8b870
ML
6585 state = drm_atomic_helper_suspend(dev);
6586 ret = PTR_ERR_OR_ZERO(state);
70e0bd74
ML
6587 if (ret)
6588 DRM_ERROR("Suspending crtc's failed with %i\n", ret);
e2c8b870
ML
6589 else
6590 dev_priv->modeset_restore_state = state;
70e0bd74 6591 return ret;
ee7b9f93
JB
6592}
6593
ea5b213a 6594void intel_encoder_destroy(struct drm_encoder *encoder)
7e7d76c3 6595{
4ef69c7a 6596 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
ea5b213a 6597
ea5b213a
CW
6598 drm_encoder_cleanup(encoder);
6599 kfree(intel_encoder);
7e7d76c3
JB
6600}
6601
0a91ca29
DV
6602/* Cross check the actual hw state with our own modeset state tracking (and it's
6603 * internal consistency). */
5a21b665 6604static void intel_connector_verify_state(struct intel_connector *connector)
79e53945 6605{
5a21b665 6606 struct drm_crtc *crtc = connector->base.state->crtc;
35dd3c64
ML
6607
6608 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
6609 connector->base.base.id,
6610 connector->base.name);
6611
0a91ca29 6612 if (connector->get_hw_state(connector)) {
e85376cb 6613 struct intel_encoder *encoder = connector->encoder;
5a21b665 6614 struct drm_connector_state *conn_state = connector->base.state;
0a91ca29 6615
35dd3c64
ML
6616 I915_STATE_WARN(!crtc,
6617 "connector enabled without attached crtc\n");
0a91ca29 6618
35dd3c64
ML
6619 if (!crtc)
6620 return;
6621
6622 I915_STATE_WARN(!crtc->state->active,
6623 "connector is active, but attached crtc isn't\n");
6624
e85376cb 6625 if (!encoder || encoder->type == INTEL_OUTPUT_DP_MST)
35dd3c64
ML
6626 return;
6627
e85376cb 6628 I915_STATE_WARN(conn_state->best_encoder != &encoder->base,
35dd3c64
ML
6629 "atomic encoder doesn't match attached encoder\n");
6630
e85376cb 6631 I915_STATE_WARN(conn_state->crtc != encoder->base.crtc,
35dd3c64
ML
6632 "attached encoder crtc differs from connector crtc\n");
6633 } else {
4d688a2a
ML
6634 I915_STATE_WARN(crtc && crtc->state->active,
6635 "attached crtc is active, but connector isn't\n");
5a21b665 6636 I915_STATE_WARN(!crtc && connector->base.state->best_encoder,
35dd3c64 6637 "best encoder set without crtc!\n");
0a91ca29 6638 }
79e53945
JB
6639}
6640
08d9bc92
ACO
6641int intel_connector_init(struct intel_connector *connector)
6642{
5350a031 6643 drm_atomic_helper_connector_reset(&connector->base);
08d9bc92 6644
5350a031 6645 if (!connector->base.state)
08d9bc92
ACO
6646 return -ENOMEM;
6647
08d9bc92
ACO
6648 return 0;
6649}
6650
6651struct intel_connector *intel_connector_alloc(void)
6652{
6653 struct intel_connector *connector;
6654
6655 connector = kzalloc(sizeof *connector, GFP_KERNEL);
6656 if (!connector)
6657 return NULL;
6658
6659 if (intel_connector_init(connector) < 0) {
6660 kfree(connector);
6661 return NULL;
6662 }
6663
6664 return connector;
6665}
6666
f0947c37
DV
6667/* Simple connector->get_hw_state implementation for encoders that support only
6668 * one connector and no cloning and hence the encoder state determines the state
6669 * of the connector. */
6670bool intel_connector_get_hw_state(struct intel_connector *connector)
ea5b213a 6671{
24929352 6672 enum pipe pipe = 0;
f0947c37 6673 struct intel_encoder *encoder = connector->encoder;
ea5b213a 6674
f0947c37 6675 return encoder->get_hw_state(encoder, &pipe);
ea5b213a
CW
6676}
6677
6d293983 6678static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
d272ddfa 6679{
6d293983
ACO
6680 if (crtc_state->base.enable && crtc_state->has_pch_encoder)
6681 return crtc_state->fdi_lanes;
d272ddfa
VS
6682
6683 return 0;
6684}
6685
6d293983 6686static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
5cec258b 6687 struct intel_crtc_state *pipe_config)
1857e1da 6688{
6d293983
ACO
6689 struct drm_atomic_state *state = pipe_config->base.state;
6690 struct intel_crtc *other_crtc;
6691 struct intel_crtc_state *other_crtc_state;
6692
1857e1da
DV
6693 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
6694 pipe_name(pipe), pipe_config->fdi_lanes);
6695 if (pipe_config->fdi_lanes > 4) {
6696 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
6697 pipe_name(pipe), pipe_config->fdi_lanes);
6d293983 6698 return -EINVAL;
1857e1da
DV
6699 }
6700
bafb6553 6701 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
1857e1da
DV
6702 if (pipe_config->fdi_lanes > 2) {
6703 DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
6704 pipe_config->fdi_lanes);
6d293983 6705 return -EINVAL;
1857e1da 6706 } else {
6d293983 6707 return 0;
1857e1da
DV
6708 }
6709 }
6710
6711 if (INTEL_INFO(dev)->num_pipes == 2)
6d293983 6712 return 0;
1857e1da
DV
6713
6714 /* Ivybridge 3 pipe is really complicated */
6715 switch (pipe) {
6716 case PIPE_A:
6d293983 6717 return 0;
1857e1da 6718 case PIPE_B:
6d293983
ACO
6719 if (pipe_config->fdi_lanes <= 2)
6720 return 0;
6721
6722 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_C));
6723 other_crtc_state =
6724 intel_atomic_get_crtc_state(state, other_crtc);
6725 if (IS_ERR(other_crtc_state))
6726 return PTR_ERR(other_crtc_state);
6727
6728 if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
1857e1da
DV
6729 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
6730 pipe_name(pipe), pipe_config->fdi_lanes);
6d293983 6731 return -EINVAL;
1857e1da 6732 }
6d293983 6733 return 0;
1857e1da 6734 case PIPE_C:
251cc67c
VS
6735 if (pipe_config->fdi_lanes > 2) {
6736 DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
6737 pipe_name(pipe), pipe_config->fdi_lanes);
6d293983 6738 return -EINVAL;
251cc67c 6739 }
6d293983
ACO
6740
6741 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_B));
6742 other_crtc_state =
6743 intel_atomic_get_crtc_state(state, other_crtc);
6744 if (IS_ERR(other_crtc_state))
6745 return PTR_ERR(other_crtc_state);
6746
6747 if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
1857e1da 6748 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
6d293983 6749 return -EINVAL;
1857e1da 6750 }
6d293983 6751 return 0;
1857e1da
DV
6752 default:
6753 BUG();
6754 }
6755}
6756
e29c22c0
DV
6757#define RETRY 1
6758static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
5cec258b 6759 struct intel_crtc_state *pipe_config)
877d48d5 6760{
1857e1da 6761 struct drm_device *dev = intel_crtc->base.dev;
7c5f93b0 6762 const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
6d293983
ACO
6763 int lane, link_bw, fdi_dotclock, ret;
6764 bool needs_recompute = false;
877d48d5 6765
e29c22c0 6766retry:
877d48d5
DV
6767 /* FDI is a binary signal running at ~2.7GHz, encoding
6768 * each output octet as 10 bits. The actual frequency
6769 * is stored as a divider into a 100MHz clock, and the
6770 * mode pixel clock is stored in units of 1KHz.
6771 * Hence the bw of each lane in terms of the mode signal
6772 * is:
6773 */
21a727b3 6774 link_bw = intel_fdi_link_freq(to_i915(dev), pipe_config);
877d48d5 6775
241bfc38 6776 fdi_dotclock = adjusted_mode->crtc_clock;
877d48d5 6777
2bd89a07 6778 lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
877d48d5
DV
6779 pipe_config->pipe_bpp);
6780
6781 pipe_config->fdi_lanes = lane;
6782
2bd89a07 6783 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
877d48d5 6784 link_bw, &pipe_config->fdi_m_n);
1857e1da 6785
e3b247da 6786 ret = ironlake_check_fdi_lanes(dev, intel_crtc->pipe, pipe_config);
6d293983 6787 if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
e29c22c0
DV
6788 pipe_config->pipe_bpp -= 2*3;
6789 DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
6790 pipe_config->pipe_bpp);
6791 needs_recompute = true;
6792 pipe_config->bw_constrained = true;
6793
6794 goto retry;
6795 }
6796
6797 if (needs_recompute)
6798 return RETRY;
6799
6d293983 6800 return ret;
877d48d5
DV
6801}
6802
8cfb3407
VS
6803static bool pipe_config_supports_ips(struct drm_i915_private *dev_priv,
6804 struct intel_crtc_state *pipe_config)
6805{
6806 if (pipe_config->pipe_bpp > 24)
6807 return false;
6808
6809 /* HSW can handle pixel rate up to cdclk? */
2d1fe073 6810 if (IS_HASWELL(dev_priv))
8cfb3407
VS
6811 return true;
6812
6813 /*
b432e5cf
VS
6814 * We compare against max which means we must take
6815 * the increased cdclk requirement into account when
6816 * calculating the new cdclk.
6817 *
6818 * Should measure whether using a lower cdclk w/o IPS
8cfb3407
VS
6819 */
6820 return ilk_pipe_pixel_rate(pipe_config) <=
6821 dev_priv->max_cdclk_freq * 95 / 100;
6822}
6823
42db64ef 6824static void hsw_compute_ips_config(struct intel_crtc *crtc,
5cec258b 6825 struct intel_crtc_state *pipe_config)
42db64ef 6826{
8cfb3407 6827 struct drm_device *dev = crtc->base.dev;
fac5e23e 6828 struct drm_i915_private *dev_priv = to_i915(dev);
8cfb3407 6829
d330a953 6830 pipe_config->ips_enabled = i915.enable_ips &&
8cfb3407
VS
6831 hsw_crtc_supports_ips(crtc) &&
6832 pipe_config_supports_ips(dev_priv, pipe_config);
42db64ef
PZ
6833}
6834
39acb4aa
VS
6835static bool intel_crtc_supports_double_wide(const struct intel_crtc *crtc)
6836{
6837 const struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6838
6839 /* GDG double wide on either pipe, otherwise pipe A only */
6840 return INTEL_INFO(dev_priv)->gen < 4 &&
6841 (crtc->pipe == PIPE_A || IS_I915G(dev_priv));
6842}
6843
a43f6e0f 6844static int intel_crtc_compute_config(struct intel_crtc *crtc,
5cec258b 6845 struct intel_crtc_state *pipe_config)
79e53945 6846{
a43f6e0f 6847 struct drm_device *dev = crtc->base.dev;
fac5e23e 6848 struct drm_i915_private *dev_priv = to_i915(dev);
7c5f93b0 6849 const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
f3261156 6850 int clock_limit = dev_priv->max_dotclk_freq;
89749350 6851
cf532bb2 6852 if (INTEL_INFO(dev)->gen < 4) {
f3261156 6853 clock_limit = dev_priv->max_cdclk_freq * 9 / 10;
cf532bb2
VS
6854
6855 /*
39acb4aa 6856 * Enable double wide mode when the dot clock
cf532bb2 6857 * is > 90% of the (display) core speed.
cf532bb2 6858 */
39acb4aa
VS
6859 if (intel_crtc_supports_double_wide(crtc) &&
6860 adjusted_mode->crtc_clock > clock_limit) {
f3261156 6861 clock_limit = dev_priv->max_dotclk_freq;
cf532bb2 6862 pipe_config->double_wide = true;
ad3a4479 6863 }
f3261156 6864 }
ad3a4479 6865
f3261156
VS
6866 if (adjusted_mode->crtc_clock > clock_limit) {
6867 DRM_DEBUG_KMS("requested pixel clock (%d kHz) too high (max: %d kHz, double wide: %s)\n",
6868 adjusted_mode->crtc_clock, clock_limit,
6869 yesno(pipe_config->double_wide));
6870 return -EINVAL;
2c07245f 6871 }
89749350 6872
1d1d0e27
VS
6873 /*
6874 * Pipe horizontal size must be even in:
6875 * - DVO ganged mode
6876 * - LVDS dual channel mode
6877 * - Double wide pipe
6878 */
2d84d2b3 6879 if ((intel_crtc_has_type(pipe_config, INTEL_OUTPUT_LVDS) &&
1d1d0e27
VS
6880 intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
6881 pipe_config->pipe_src_w &= ~1;
6882
8693a824
DL
6883 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
6884 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
44f46b42
CW
6885 */
6886 if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
aad941d5 6887 adjusted_mode->crtc_hsync_start == adjusted_mode->crtc_hdisplay)
e29c22c0 6888 return -EINVAL;
44f46b42 6889
f5adf94e 6890 if (HAS_IPS(dev))
a43f6e0f
DV
6891 hsw_compute_ips_config(crtc, pipe_config);
6892
877d48d5 6893 if (pipe_config->has_pch_encoder)
a43f6e0f 6894 return ironlake_fdi_compute_config(crtc, pipe_config);
877d48d5 6895
cf5a15be 6896 return 0;
79e53945
JB
6897}
6898
1652d19e
VS
6899static int skylake_get_display_clock_speed(struct drm_device *dev)
6900{
6901 struct drm_i915_private *dev_priv = to_i915(dev);
ea61791e 6902 uint32_t cdctl;
1652d19e 6903
ea61791e 6904 skl_dpll0_update(dev_priv);
1652d19e 6905
63911d72 6906 if (dev_priv->cdclk_pll.vco == 0)
709e05c3 6907 return dev_priv->cdclk_pll.ref;
1652d19e 6908
ea61791e 6909 cdctl = I915_READ(CDCLK_CTL);
1652d19e 6910
63911d72 6911 if (dev_priv->cdclk_pll.vco == 8640000) {
1652d19e
VS
6912 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6913 case CDCLK_FREQ_450_432:
6914 return 432000;
6915 case CDCLK_FREQ_337_308:
487ed2e4 6916 return 308571;
ea61791e
VS
6917 case CDCLK_FREQ_540:
6918 return 540000;
1652d19e 6919 case CDCLK_FREQ_675_617:
487ed2e4 6920 return 617143;
1652d19e 6921 default:
ea61791e 6922 MISSING_CASE(cdctl & CDCLK_FREQ_SEL_MASK);
1652d19e
VS
6923 }
6924 } else {
1652d19e
VS
6925 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6926 case CDCLK_FREQ_450_432:
6927 return 450000;
6928 case CDCLK_FREQ_337_308:
6929 return 337500;
ea61791e
VS
6930 case CDCLK_FREQ_540:
6931 return 540000;
1652d19e
VS
6932 case CDCLK_FREQ_675_617:
6933 return 675000;
6934 default:
ea61791e 6935 MISSING_CASE(cdctl & CDCLK_FREQ_SEL_MASK);
1652d19e
VS
6936 }
6937 }
6938
709e05c3 6939 return dev_priv->cdclk_pll.ref;
1652d19e
VS
6940}
6941
83d7c81f
VS
6942static void bxt_de_pll_update(struct drm_i915_private *dev_priv)
6943{
6944 u32 val;
6945
6946 dev_priv->cdclk_pll.ref = 19200;
1c3f7700 6947 dev_priv->cdclk_pll.vco = 0;
83d7c81f
VS
6948
6949 val = I915_READ(BXT_DE_PLL_ENABLE);
1c3f7700 6950 if ((val & BXT_DE_PLL_PLL_ENABLE) == 0)
83d7c81f 6951 return;
83d7c81f 6952
1c3f7700
ID
6953 if (WARN_ON((val & BXT_DE_PLL_LOCK) == 0))
6954 return;
83d7c81f
VS
6955
6956 val = I915_READ(BXT_DE_PLL_CTL);
6957 dev_priv->cdclk_pll.vco = (val & BXT_DE_PLL_RATIO_MASK) *
6958 dev_priv->cdclk_pll.ref;
6959}
6960
acd3f3d3
BP
6961static int broxton_get_display_clock_speed(struct drm_device *dev)
6962{
6963 struct drm_i915_private *dev_priv = to_i915(dev);
f5986242
VS
6964 u32 divider;
6965 int div, vco;
acd3f3d3 6966
83d7c81f
VS
6967 bxt_de_pll_update(dev_priv);
6968
f5986242
VS
6969 vco = dev_priv->cdclk_pll.vco;
6970 if (vco == 0)
6971 return dev_priv->cdclk_pll.ref;
acd3f3d3 6972
f5986242 6973 divider = I915_READ(CDCLK_CTL) & BXT_CDCLK_CD2X_DIV_SEL_MASK;
acd3f3d3 6974
f5986242 6975 switch (divider) {
acd3f3d3 6976 case BXT_CDCLK_CD2X_DIV_SEL_1:
f5986242
VS
6977 div = 2;
6978 break;
acd3f3d3 6979 case BXT_CDCLK_CD2X_DIV_SEL_1_5:
f5986242
VS
6980 div = 3;
6981 break;
acd3f3d3 6982 case BXT_CDCLK_CD2X_DIV_SEL_2:
f5986242
VS
6983 div = 4;
6984 break;
acd3f3d3 6985 case BXT_CDCLK_CD2X_DIV_SEL_4:
f5986242
VS
6986 div = 8;
6987 break;
6988 default:
6989 MISSING_CASE(divider);
6990 return dev_priv->cdclk_pll.ref;
acd3f3d3
BP
6991 }
6992
f5986242 6993 return DIV_ROUND_CLOSEST(vco, div);
acd3f3d3
BP
6994}
6995
1652d19e
VS
6996static int broadwell_get_display_clock_speed(struct drm_device *dev)
6997{
fac5e23e 6998 struct drm_i915_private *dev_priv = to_i915(dev);
1652d19e
VS
6999 uint32_t lcpll = I915_READ(LCPLL_CTL);
7000 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
7001
7002 if (lcpll & LCPLL_CD_SOURCE_FCLK)
7003 return 800000;
7004 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
7005 return 450000;
7006 else if (freq == LCPLL_CLK_FREQ_450)
7007 return 450000;
7008 else if (freq == LCPLL_CLK_FREQ_54O_BDW)
7009 return 540000;
7010 else if (freq == LCPLL_CLK_FREQ_337_5_BDW)
7011 return 337500;
7012 else
7013 return 675000;
7014}
7015
7016static int haswell_get_display_clock_speed(struct drm_device *dev)
7017{
fac5e23e 7018 struct drm_i915_private *dev_priv = to_i915(dev);
1652d19e
VS
7019 uint32_t lcpll = I915_READ(LCPLL_CTL);
7020 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
7021
7022 if (lcpll & LCPLL_CD_SOURCE_FCLK)
7023 return 800000;
7024 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
7025 return 450000;
7026 else if (freq == LCPLL_CLK_FREQ_450)
7027 return 450000;
7028 else if (IS_HSW_ULT(dev))
7029 return 337500;
7030 else
7031 return 540000;
79e53945
JB
7032}
7033
25eb05fc
JB
7034static int valleyview_get_display_clock_speed(struct drm_device *dev)
7035{
bfa7df01
VS
7036 return vlv_get_cck_clock_hpll(to_i915(dev), "cdclk",
7037 CCK_DISPLAY_CLOCK_CONTROL);
25eb05fc
JB
7038}
7039
b37a6434
VS
7040static int ilk_get_display_clock_speed(struct drm_device *dev)
7041{
7042 return 450000;
7043}
7044
e70236a8
JB
7045static int i945_get_display_clock_speed(struct drm_device *dev)
7046{
7047 return 400000;
7048}
79e53945 7049
e70236a8 7050static int i915_get_display_clock_speed(struct drm_device *dev)
79e53945 7051{
e907f170 7052 return 333333;
e70236a8 7053}
79e53945 7054
e70236a8
JB
7055static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
7056{
7057 return 200000;
7058}
79e53945 7059
257a7ffc
DV
7060static int pnv_get_display_clock_speed(struct drm_device *dev)
7061{
7062 u16 gcfgc = 0;
7063
7064 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
7065
7066 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
7067 case GC_DISPLAY_CLOCK_267_MHZ_PNV:
e907f170 7068 return 266667;
257a7ffc 7069 case GC_DISPLAY_CLOCK_333_MHZ_PNV:
e907f170 7070 return 333333;
257a7ffc 7071 case GC_DISPLAY_CLOCK_444_MHZ_PNV:
e907f170 7072 return 444444;
257a7ffc
DV
7073 case GC_DISPLAY_CLOCK_200_MHZ_PNV:
7074 return 200000;
7075 default:
7076 DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
7077 case GC_DISPLAY_CLOCK_133_MHZ_PNV:
e907f170 7078 return 133333;
257a7ffc 7079 case GC_DISPLAY_CLOCK_167_MHZ_PNV:
e907f170 7080 return 166667;
257a7ffc
DV
7081 }
7082}
7083
e70236a8
JB
7084static int i915gm_get_display_clock_speed(struct drm_device *dev)
7085{
7086 u16 gcfgc = 0;
79e53945 7087
e70236a8
JB
7088 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
7089
7090 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
e907f170 7091 return 133333;
e70236a8
JB
7092 else {
7093 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
7094 case GC_DISPLAY_CLOCK_333_MHZ:
e907f170 7095 return 333333;
e70236a8
JB
7096 default:
7097 case GC_DISPLAY_CLOCK_190_200_MHZ:
7098 return 190000;
79e53945 7099 }
e70236a8
JB
7100 }
7101}
7102
7103static int i865_get_display_clock_speed(struct drm_device *dev)
7104{
e907f170 7105 return 266667;
e70236a8
JB
7106}
7107
1b1d2716 7108static int i85x_get_display_clock_speed(struct drm_device *dev)
e70236a8
JB
7109{
7110 u16 hpllcc = 0;
1b1d2716 7111
65cd2b3f
VS
7112 /*
7113 * 852GM/852GMV only supports 133 MHz and the HPLLCC
7114 * encoding is different :(
7115 * FIXME is this the right way to detect 852GM/852GMV?
7116 */
7117 if (dev->pdev->revision == 0x1)
7118 return 133333;
7119
1b1d2716
VS
7120 pci_bus_read_config_word(dev->pdev->bus,
7121 PCI_DEVFN(0, 3), HPLLCC, &hpllcc);
7122
e70236a8
JB
7123 /* Assume that the hardware is in the high speed state. This
7124 * should be the default.
7125 */
7126 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
7127 case GC_CLOCK_133_200:
1b1d2716 7128 case GC_CLOCK_133_200_2:
e70236a8
JB
7129 case GC_CLOCK_100_200:
7130 return 200000;
7131 case GC_CLOCK_166_250:
7132 return 250000;
7133 case GC_CLOCK_100_133:
e907f170 7134 return 133333;
1b1d2716
VS
7135 case GC_CLOCK_133_266:
7136 case GC_CLOCK_133_266_2:
7137 case GC_CLOCK_166_266:
7138 return 266667;
e70236a8 7139 }
79e53945 7140
e70236a8
JB
7141 /* Shouldn't happen */
7142 return 0;
7143}
79e53945 7144
e70236a8
JB
7145static int i830_get_display_clock_speed(struct drm_device *dev)
7146{
e907f170 7147 return 133333;
79e53945
JB
7148}
7149
34edce2f
VS
7150static unsigned int intel_hpll_vco(struct drm_device *dev)
7151{
fac5e23e 7152 struct drm_i915_private *dev_priv = to_i915(dev);
34edce2f
VS
7153 static const unsigned int blb_vco[8] = {
7154 [0] = 3200000,
7155 [1] = 4000000,
7156 [2] = 5333333,
7157 [3] = 4800000,
7158 [4] = 6400000,
7159 };
7160 static const unsigned int pnv_vco[8] = {
7161 [0] = 3200000,
7162 [1] = 4000000,
7163 [2] = 5333333,
7164 [3] = 4800000,
7165 [4] = 2666667,
7166 };
7167 static const unsigned int cl_vco[8] = {
7168 [0] = 3200000,
7169 [1] = 4000000,
7170 [2] = 5333333,
7171 [3] = 6400000,
7172 [4] = 3333333,
7173 [5] = 3566667,
7174 [6] = 4266667,
7175 };
7176 static const unsigned int elk_vco[8] = {
7177 [0] = 3200000,
7178 [1] = 4000000,
7179 [2] = 5333333,
7180 [3] = 4800000,
7181 };
7182 static const unsigned int ctg_vco[8] = {
7183 [0] = 3200000,
7184 [1] = 4000000,
7185 [2] = 5333333,
7186 [3] = 6400000,
7187 [4] = 2666667,
7188 [5] = 4266667,
7189 };
7190 const unsigned int *vco_table;
7191 unsigned int vco;
7192 uint8_t tmp = 0;
7193
7194 /* FIXME other chipsets? */
7195 if (IS_GM45(dev))
7196 vco_table = ctg_vco;
7197 else if (IS_G4X(dev))
7198 vco_table = elk_vco;
7199 else if (IS_CRESTLINE(dev))
7200 vco_table = cl_vco;
7201 else if (IS_PINEVIEW(dev))
7202 vco_table = pnv_vco;
7203 else if (IS_G33(dev))
7204 vco_table = blb_vco;
7205 else
7206 return 0;
7207
7208 tmp = I915_READ(IS_MOBILE(dev) ? HPLLVCO_MOBILE : HPLLVCO);
7209
7210 vco = vco_table[tmp & 0x7];
7211 if (vco == 0)
7212 DRM_ERROR("Bad HPLL VCO (HPLLVCO=0x%02x)\n", tmp);
7213 else
7214 DRM_DEBUG_KMS("HPLL VCO %u kHz\n", vco);
7215
7216 return vco;
7217}
7218
7219static int gm45_get_display_clock_speed(struct drm_device *dev)
7220{
7221 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
7222 uint16_t tmp = 0;
7223
7224 pci_read_config_word(dev->pdev, GCFGC, &tmp);
7225
7226 cdclk_sel = (tmp >> 12) & 0x1;
7227
7228 switch (vco) {
7229 case 2666667:
7230 case 4000000:
7231 case 5333333:
7232 return cdclk_sel ? 333333 : 222222;
7233 case 3200000:
7234 return cdclk_sel ? 320000 : 228571;
7235 default:
7236 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u, CFGC=0x%04x\n", vco, tmp);
7237 return 222222;
7238 }
7239}
7240
7241static int i965gm_get_display_clock_speed(struct drm_device *dev)
7242{
7243 static const uint8_t div_3200[] = { 16, 10, 8 };
7244 static const uint8_t div_4000[] = { 20, 12, 10 };
7245 static const uint8_t div_5333[] = { 24, 16, 14 };
7246 const uint8_t *div_table;
7247 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
7248 uint16_t tmp = 0;
7249
7250 pci_read_config_word(dev->pdev, GCFGC, &tmp);
7251
7252 cdclk_sel = ((tmp >> 8) & 0x1f) - 1;
7253
7254 if (cdclk_sel >= ARRAY_SIZE(div_3200))
7255 goto fail;
7256
7257 switch (vco) {
7258 case 3200000:
7259 div_table = div_3200;
7260 break;
7261 case 4000000:
7262 div_table = div_4000;
7263 break;
7264 case 5333333:
7265 div_table = div_5333;
7266 break;
7267 default:
7268 goto fail;
7269 }
7270
7271 return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
7272
caf4e252 7273fail:
34edce2f
VS
7274 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%04x\n", vco, tmp);
7275 return 200000;
7276}
7277
7278static int g33_get_display_clock_speed(struct drm_device *dev)
7279{
7280 static const uint8_t div_3200[] = { 12, 10, 8, 7, 5, 16 };
7281 static const uint8_t div_4000[] = { 14, 12, 10, 8, 6, 20 };
7282 static const uint8_t div_4800[] = { 20, 14, 12, 10, 8, 24 };
7283 static const uint8_t div_5333[] = { 20, 16, 12, 12, 8, 28 };
7284 const uint8_t *div_table;
7285 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
7286 uint16_t tmp = 0;
7287
7288 pci_read_config_word(dev->pdev, GCFGC, &tmp);
7289
7290 cdclk_sel = (tmp >> 4) & 0x7;
7291
7292 if (cdclk_sel >= ARRAY_SIZE(div_3200))
7293 goto fail;
7294
7295 switch (vco) {
7296 case 3200000:
7297 div_table = div_3200;
7298 break;
7299 case 4000000:
7300 div_table = div_4000;
7301 break;
7302 case 4800000:
7303 div_table = div_4800;
7304 break;
7305 case 5333333:
7306 div_table = div_5333;
7307 break;
7308 default:
7309 goto fail;
7310 }
7311
7312 return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
7313
caf4e252 7314fail:
34edce2f
VS
7315 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%08x\n", vco, tmp);
7316 return 190476;
7317}
7318
2c07245f 7319static void
a65851af 7320intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
2c07245f 7321{
a65851af
VS
7322 while (*num > DATA_LINK_M_N_MASK ||
7323 *den > DATA_LINK_M_N_MASK) {
2c07245f
ZW
7324 *num >>= 1;
7325 *den >>= 1;
7326 }
7327}
7328
a65851af
VS
7329static void compute_m_n(unsigned int m, unsigned int n,
7330 uint32_t *ret_m, uint32_t *ret_n)
7331{
7332 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
7333 *ret_m = div_u64((uint64_t) m * *ret_n, n);
7334 intel_reduce_m_n_ratio(ret_m, ret_n);
7335}
7336
e69d0bc1
DV
7337void
7338intel_link_compute_m_n(int bits_per_pixel, int nlanes,
7339 int pixel_clock, int link_clock,
7340 struct intel_link_m_n *m_n)
2c07245f 7341{
e69d0bc1 7342 m_n->tu = 64;
a65851af
VS
7343
7344 compute_m_n(bits_per_pixel * pixel_clock,
7345 link_clock * nlanes * 8,
7346 &m_n->gmch_m, &m_n->gmch_n);
7347
7348 compute_m_n(pixel_clock, link_clock,
7349 &m_n->link_m, &m_n->link_n);
2c07245f
ZW
7350}
7351
a7615030
CW
7352static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
7353{
d330a953
JN
7354 if (i915.panel_use_ssc >= 0)
7355 return i915.panel_use_ssc != 0;
41aa3448 7356 return dev_priv->vbt.lvds_use_ssc
435793df 7357 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
a7615030
CW
7358}
7359
7429e9d4 7360static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
c65d77d8 7361{
7df00d7a 7362 return (1 << dpll->n) << 16 | dpll->m2;
7429e9d4 7363}
f47709a9 7364
7429e9d4
DV
7365static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
7366{
7367 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
c65d77d8
JB
7368}
7369
f47709a9 7370static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
190f68c5 7371 struct intel_crtc_state *crtc_state,
9e2c8475 7372 struct dpll *reduced_clock)
a7516a05 7373{
f47709a9 7374 struct drm_device *dev = crtc->base.dev;
a7516a05
JB
7375 u32 fp, fp2 = 0;
7376
7377 if (IS_PINEVIEW(dev)) {
190f68c5 7378 fp = pnv_dpll_compute_fp(&crtc_state->dpll);
a7516a05 7379 if (reduced_clock)
7429e9d4 7380 fp2 = pnv_dpll_compute_fp(reduced_clock);
a7516a05 7381 } else {
190f68c5 7382 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
a7516a05 7383 if (reduced_clock)
7429e9d4 7384 fp2 = i9xx_dpll_compute_fp(reduced_clock);
a7516a05
JB
7385 }
7386
190f68c5 7387 crtc_state->dpll_hw_state.fp0 = fp;
a7516a05 7388
f47709a9 7389 crtc->lowfreq_avail = false;
2d84d2b3 7390 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
ab585dea 7391 reduced_clock) {
190f68c5 7392 crtc_state->dpll_hw_state.fp1 = fp2;
f47709a9 7393 crtc->lowfreq_avail = true;
a7516a05 7394 } else {
190f68c5 7395 crtc_state->dpll_hw_state.fp1 = fp;
a7516a05
JB
7396 }
7397}
7398
5e69f97f
CML
7399static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
7400 pipe)
89b667f8
JB
7401{
7402 u32 reg_val;
7403
7404 /*
7405 * PLLB opamp always calibrates to max value of 0x3f, force enable it
7406 * and set it to a reasonable value instead.
7407 */
ab3c759a 7408 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
89b667f8
JB
7409 reg_val &= 0xffffff00;
7410 reg_val |= 0x00000030;
ab3c759a 7411 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
89b667f8 7412
ab3c759a 7413 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
89b667f8
JB
7414 reg_val &= 0x8cffffff;
7415 reg_val = 0x8c000000;
ab3c759a 7416 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
89b667f8 7417
ab3c759a 7418 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
89b667f8 7419 reg_val &= 0xffffff00;
ab3c759a 7420 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
89b667f8 7421
ab3c759a 7422 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
89b667f8
JB
7423 reg_val &= 0x00ffffff;
7424 reg_val |= 0xb0000000;
ab3c759a 7425 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
89b667f8
JB
7426}
7427
b551842d
DV
7428static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
7429 struct intel_link_m_n *m_n)
7430{
7431 struct drm_device *dev = crtc->base.dev;
fac5e23e 7432 struct drm_i915_private *dev_priv = to_i915(dev);
b551842d
DV
7433 int pipe = crtc->pipe;
7434
e3b95f1e
DV
7435 I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7436 I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
7437 I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
7438 I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
b551842d
DV
7439}
7440
7441static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
f769cd24
VK
7442 struct intel_link_m_n *m_n,
7443 struct intel_link_m_n *m2_n2)
b551842d
DV
7444{
7445 struct drm_device *dev = crtc->base.dev;
fac5e23e 7446 struct drm_i915_private *dev_priv = to_i915(dev);
b551842d 7447 int pipe = crtc->pipe;
6e3c9717 7448 enum transcoder transcoder = crtc->config->cpu_transcoder;
b551842d
DV
7449
7450 if (INTEL_INFO(dev)->gen >= 5) {
7451 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
7452 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
7453 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
7454 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
f769cd24
VK
7455 /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
7456 * for gen < 8) and if DRRS is supported (to make sure the
7457 * registers are not unnecessarily accessed).
7458 */
44395bfe 7459 if (m2_n2 && (IS_CHERRYVIEW(dev) || INTEL_INFO(dev)->gen < 8) &&
6e3c9717 7460 crtc->config->has_drrs) {
f769cd24
VK
7461 I915_WRITE(PIPE_DATA_M2(transcoder),
7462 TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
7463 I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
7464 I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
7465 I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
7466 }
b551842d 7467 } else {
e3b95f1e
DV
7468 I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7469 I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
7470 I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
7471 I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
b551842d
DV
7472 }
7473}
7474
fe3cd48d 7475void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
03afc4a2 7476{
fe3cd48d
R
7477 struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
7478
7479 if (m_n == M1_N1) {
7480 dp_m_n = &crtc->config->dp_m_n;
7481 dp_m2_n2 = &crtc->config->dp_m2_n2;
7482 } else if (m_n == M2_N2) {
7483
7484 /*
7485 * M2_N2 registers are not supported. Hence m2_n2 divider value
7486 * needs to be programmed into M1_N1.
7487 */
7488 dp_m_n = &crtc->config->dp_m2_n2;
7489 } else {
7490 DRM_ERROR("Unsupported divider value\n");
7491 return;
7492 }
7493
6e3c9717
ACO
7494 if (crtc->config->has_pch_encoder)
7495 intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
03afc4a2 7496 else
fe3cd48d 7497 intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
03afc4a2
DV
7498}
7499
251ac862
DV
7500static void vlv_compute_dpll(struct intel_crtc *crtc,
7501 struct intel_crtc_state *pipe_config)
bdd4b6a6 7502{
03ed5cbf 7503 pipe_config->dpll_hw_state.dpll = DPLL_INTEGRATED_REF_CLK_VLV |
cd2d34d9 7504 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
03ed5cbf
VS
7505 if (crtc->pipe != PIPE_A)
7506 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
bdd4b6a6 7507
cd2d34d9 7508 /* DPLL not used with DSI, but still need the rest set up */
d7edc4e5 7509 if (!intel_crtc_has_type(pipe_config, INTEL_OUTPUT_DSI))
cd2d34d9
VS
7510 pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE |
7511 DPLL_EXT_BUFFER_ENABLE_VLV;
7512
03ed5cbf
VS
7513 pipe_config->dpll_hw_state.dpll_md =
7514 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
7515}
bdd4b6a6 7516
03ed5cbf
VS
7517static void chv_compute_dpll(struct intel_crtc *crtc,
7518 struct intel_crtc_state *pipe_config)
7519{
7520 pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLK_CHV |
cd2d34d9 7521 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
03ed5cbf
VS
7522 if (crtc->pipe != PIPE_A)
7523 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
7524
cd2d34d9 7525 /* DPLL not used with DSI, but still need the rest set up */
d7edc4e5 7526 if (!intel_crtc_has_type(pipe_config, INTEL_OUTPUT_DSI))
cd2d34d9
VS
7527 pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE;
7528
03ed5cbf
VS
7529 pipe_config->dpll_hw_state.dpll_md =
7530 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
bdd4b6a6
DV
7531}
7532
d288f65f 7533static void vlv_prepare_pll(struct intel_crtc *crtc,
5cec258b 7534 const struct intel_crtc_state *pipe_config)
a0c4da24 7535{
f47709a9 7536 struct drm_device *dev = crtc->base.dev;
fac5e23e 7537 struct drm_i915_private *dev_priv = to_i915(dev);
cd2d34d9 7538 enum pipe pipe = crtc->pipe;
bdd4b6a6 7539 u32 mdiv;
a0c4da24 7540 u32 bestn, bestm1, bestm2, bestp1, bestp2;
bdd4b6a6 7541 u32 coreclk, reg_val;
a0c4da24 7542
cd2d34d9
VS
7543 /* Enable Refclk */
7544 I915_WRITE(DPLL(pipe),
7545 pipe_config->dpll_hw_state.dpll &
7546 ~(DPLL_VCO_ENABLE | DPLL_EXT_BUFFER_ENABLE_VLV));
7547
7548 /* No need to actually set up the DPLL with DSI */
7549 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
7550 return;
7551
a580516d 7552 mutex_lock(&dev_priv->sb_lock);
09153000 7553
d288f65f
VS
7554 bestn = pipe_config->dpll.n;
7555 bestm1 = pipe_config->dpll.m1;
7556 bestm2 = pipe_config->dpll.m2;
7557 bestp1 = pipe_config->dpll.p1;
7558 bestp2 = pipe_config->dpll.p2;
a0c4da24 7559
89b667f8
JB
7560 /* See eDP HDMI DPIO driver vbios notes doc */
7561
7562 /* PLL B needs special handling */
bdd4b6a6 7563 if (pipe == PIPE_B)
5e69f97f 7564 vlv_pllb_recal_opamp(dev_priv, pipe);
89b667f8
JB
7565
7566 /* Set up Tx target for periodic Rcomp update */
ab3c759a 7567 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
89b667f8
JB
7568
7569 /* Disable target IRef on PLL */
ab3c759a 7570 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
89b667f8 7571 reg_val &= 0x00ffffff;
ab3c759a 7572 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
89b667f8
JB
7573
7574 /* Disable fast lock */
ab3c759a 7575 vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
89b667f8
JB
7576
7577 /* Set idtafcrecal before PLL is enabled */
a0c4da24
JB
7578 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
7579 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
7580 mdiv |= ((bestn << DPIO_N_SHIFT));
a0c4da24 7581 mdiv |= (1 << DPIO_K_SHIFT);
7df5080b
JB
7582
7583 /*
7584 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
7585 * but we don't support that).
7586 * Note: don't use the DAC post divider as it seems unstable.
7587 */
7588 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
ab3c759a 7589 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
a0c4da24 7590
a0c4da24 7591 mdiv |= DPIO_ENABLE_CALIBRATION;
ab3c759a 7592 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
a0c4da24 7593
89b667f8 7594 /* Set HBR and RBR LPF coefficients */
d288f65f 7595 if (pipe_config->port_clock == 162000 ||
2d84d2b3
VS
7596 intel_crtc_has_type(crtc->config, INTEL_OUTPUT_ANALOG) ||
7597 intel_crtc_has_type(crtc->config, INTEL_OUTPUT_HDMI))
ab3c759a 7598 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
885b0120 7599 0x009f0003);
89b667f8 7600 else
ab3c759a 7601 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
89b667f8
JB
7602 0x00d0000f);
7603
37a5650b 7604 if (intel_crtc_has_dp_encoder(pipe_config)) {
89b667f8 7605 /* Use SSC source */
bdd4b6a6 7606 if (pipe == PIPE_A)
ab3c759a 7607 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
7608 0x0df40000);
7609 else
ab3c759a 7610 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
7611 0x0df70000);
7612 } else { /* HDMI or VGA */
7613 /* Use bend source */
bdd4b6a6 7614 if (pipe == PIPE_A)
ab3c759a 7615 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
7616 0x0df70000);
7617 else
ab3c759a 7618 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
7619 0x0df40000);
7620 }
a0c4da24 7621
ab3c759a 7622 coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
89b667f8 7623 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
2210ce7f 7624 if (intel_crtc_has_dp_encoder(crtc->config))
89b667f8 7625 coreclk |= 0x01000000;
ab3c759a 7626 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
a0c4da24 7627
ab3c759a 7628 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
a580516d 7629 mutex_unlock(&dev_priv->sb_lock);
a0c4da24
JB
7630}
7631
d288f65f 7632static void chv_prepare_pll(struct intel_crtc *crtc,
5cec258b 7633 const struct intel_crtc_state *pipe_config)
9d556c99
CML
7634{
7635 struct drm_device *dev = crtc->base.dev;
fac5e23e 7636 struct drm_i915_private *dev_priv = to_i915(dev);
cd2d34d9 7637 enum pipe pipe = crtc->pipe;
9d556c99 7638 enum dpio_channel port = vlv_pipe_to_channel(pipe);
9cbe40c1 7639 u32 loopfilter, tribuf_calcntr;
9d556c99 7640 u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
a945ce7e 7641 u32 dpio_val;
9cbe40c1 7642 int vco;
9d556c99 7643
cd2d34d9
VS
7644 /* Enable Refclk and SSC */
7645 I915_WRITE(DPLL(pipe),
7646 pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
7647
7648 /* No need to actually set up the DPLL with DSI */
7649 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
7650 return;
7651
d288f65f
VS
7652 bestn = pipe_config->dpll.n;
7653 bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
7654 bestm1 = pipe_config->dpll.m1;
7655 bestm2 = pipe_config->dpll.m2 >> 22;
7656 bestp1 = pipe_config->dpll.p1;
7657 bestp2 = pipe_config->dpll.p2;
9cbe40c1 7658 vco = pipe_config->dpll.vco;
a945ce7e 7659 dpio_val = 0;
9cbe40c1 7660 loopfilter = 0;
9d556c99 7661
a580516d 7662 mutex_lock(&dev_priv->sb_lock);
9d556c99 7663
9d556c99
CML
7664 /* p1 and p2 divider */
7665 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
7666 5 << DPIO_CHV_S1_DIV_SHIFT |
7667 bestp1 << DPIO_CHV_P1_DIV_SHIFT |
7668 bestp2 << DPIO_CHV_P2_DIV_SHIFT |
7669 1 << DPIO_CHV_K_DIV_SHIFT);
7670
7671 /* Feedback post-divider - m2 */
7672 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
7673
7674 /* Feedback refclk divider - n and m1 */
7675 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
7676 DPIO_CHV_M1_DIV_BY_2 |
7677 1 << DPIO_CHV_N_DIV_SHIFT);
7678
7679 /* M2 fraction division */
25a25dfc 7680 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
9d556c99
CML
7681
7682 /* M2 fraction division enable */
a945ce7e
VP
7683 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
7684 dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
7685 dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
7686 if (bestm2_frac)
7687 dpio_val |= DPIO_CHV_FRAC_DIV_EN;
7688 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
9d556c99 7689
de3a0fde
VP
7690 /* Program digital lock detect threshold */
7691 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
7692 dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
7693 DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
7694 dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
7695 if (!bestm2_frac)
7696 dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
7697 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
7698
9d556c99 7699 /* Loop filter */
9cbe40c1
VP
7700 if (vco == 5400000) {
7701 loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
7702 loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
7703 loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
7704 tribuf_calcntr = 0x9;
7705 } else if (vco <= 6200000) {
7706 loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
7707 loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
7708 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7709 tribuf_calcntr = 0x9;
7710 } else if (vco <= 6480000) {
7711 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7712 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7713 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7714 tribuf_calcntr = 0x8;
7715 } else {
7716 /* Not supported. Apply the same limits as in the max case */
7717 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7718 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7719 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7720 tribuf_calcntr = 0;
7721 }
9d556c99
CML
7722 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
7723
968040b2 7724 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
9cbe40c1
VP
7725 dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
7726 dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
7727 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
7728
9d556c99
CML
7729 /* AFC Recal */
7730 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
7731 vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
7732 DPIO_AFC_RECAL);
7733
a580516d 7734 mutex_unlock(&dev_priv->sb_lock);
9d556c99
CML
7735}
7736
d288f65f
VS
7737/**
7738 * vlv_force_pll_on - forcibly enable just the PLL
7739 * @dev_priv: i915 private structure
7740 * @pipe: pipe PLL to enable
7741 * @dpll: PLL configuration
7742 *
7743 * Enable the PLL for @pipe using the supplied @dpll config. To be used
7744 * in cases where we need the PLL enabled even when @pipe is not going to
7745 * be enabled.
7746 */
3f36b937
TU
7747int vlv_force_pll_on(struct drm_device *dev, enum pipe pipe,
7748 const struct dpll *dpll)
d288f65f
VS
7749{
7750 struct intel_crtc *crtc =
7751 to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
3f36b937
TU
7752 struct intel_crtc_state *pipe_config;
7753
7754 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
7755 if (!pipe_config)
7756 return -ENOMEM;
7757
7758 pipe_config->base.crtc = &crtc->base;
7759 pipe_config->pixel_multiplier = 1;
7760 pipe_config->dpll = *dpll;
d288f65f
VS
7761
7762 if (IS_CHERRYVIEW(dev)) {
3f36b937
TU
7763 chv_compute_dpll(crtc, pipe_config);
7764 chv_prepare_pll(crtc, pipe_config);
7765 chv_enable_pll(crtc, pipe_config);
d288f65f 7766 } else {
3f36b937
TU
7767 vlv_compute_dpll(crtc, pipe_config);
7768 vlv_prepare_pll(crtc, pipe_config);
7769 vlv_enable_pll(crtc, pipe_config);
d288f65f 7770 }
3f36b937
TU
7771
7772 kfree(pipe_config);
7773
7774 return 0;
d288f65f
VS
7775}
7776
7777/**
7778 * vlv_force_pll_off - forcibly disable just the PLL
7779 * @dev_priv: i915 private structure
7780 * @pipe: pipe PLL to disable
7781 *
7782 * Disable the PLL for @pipe. To be used in cases where we need
7783 * the PLL enabled even when @pipe is not going to be enabled.
7784 */
7785void vlv_force_pll_off(struct drm_device *dev, enum pipe pipe)
7786{
7787 if (IS_CHERRYVIEW(dev))
7788 chv_disable_pll(to_i915(dev), pipe);
7789 else
7790 vlv_disable_pll(to_i915(dev), pipe);
7791}
7792
251ac862
DV
7793static void i9xx_compute_dpll(struct intel_crtc *crtc,
7794 struct intel_crtc_state *crtc_state,
9e2c8475 7795 struct dpll *reduced_clock)
eb1cbe48 7796{
f47709a9 7797 struct drm_device *dev = crtc->base.dev;
fac5e23e 7798 struct drm_i915_private *dev_priv = to_i915(dev);
eb1cbe48 7799 u32 dpll;
190f68c5 7800 struct dpll *clock = &crtc_state->dpll;
eb1cbe48 7801
190f68c5 7802 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
2a8f64ca 7803
eb1cbe48
DV
7804 dpll = DPLL_VGA_MODE_DIS;
7805
2d84d2b3 7806 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS))
eb1cbe48
DV
7807 dpll |= DPLLB_MODE_LVDS;
7808 else
7809 dpll |= DPLLB_MODE_DAC_SERIAL;
6cc5f341 7810
ef1b460d 7811 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
190f68c5 7812 dpll |= (crtc_state->pixel_multiplier - 1)
198a037f 7813 << SDVO_MULTIPLIER_SHIFT_HIRES;
eb1cbe48 7814 }
198a037f 7815
3d6e9ee0
VS
7816 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO) ||
7817 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
4a33e48d 7818 dpll |= DPLL_SDVO_HIGH_SPEED;
198a037f 7819
37a5650b 7820 if (intel_crtc_has_dp_encoder(crtc_state))
4a33e48d 7821 dpll |= DPLL_SDVO_HIGH_SPEED;
eb1cbe48
DV
7822
7823 /* compute bitmask from p1 value */
7824 if (IS_PINEVIEW(dev))
7825 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
7826 else {
7827 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7828 if (IS_G4X(dev) && reduced_clock)
7829 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
7830 }
7831 switch (clock->p2) {
7832 case 5:
7833 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
7834 break;
7835 case 7:
7836 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
7837 break;
7838 case 10:
7839 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
7840 break;
7841 case 14:
7842 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
7843 break;
7844 }
7845 if (INTEL_INFO(dev)->gen >= 4)
7846 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
7847
190f68c5 7848 if (crtc_state->sdvo_tv_clock)
eb1cbe48 7849 dpll |= PLL_REF_INPUT_TVCLKINBC;
2d84d2b3 7850 else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
ceb41007 7851 intel_panel_use_ssc(dev_priv))
eb1cbe48
DV
7852 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7853 else
7854 dpll |= PLL_REF_INPUT_DREFCLK;
7855
7856 dpll |= DPLL_VCO_ENABLE;
190f68c5 7857 crtc_state->dpll_hw_state.dpll = dpll;
8bcc2795 7858
eb1cbe48 7859 if (INTEL_INFO(dev)->gen >= 4) {
190f68c5 7860 u32 dpll_md = (crtc_state->pixel_multiplier - 1)
ef1b460d 7861 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
190f68c5 7862 crtc_state->dpll_hw_state.dpll_md = dpll_md;
eb1cbe48
DV
7863 }
7864}
7865
251ac862
DV
7866static void i8xx_compute_dpll(struct intel_crtc *crtc,
7867 struct intel_crtc_state *crtc_state,
9e2c8475 7868 struct dpll *reduced_clock)
eb1cbe48 7869{
f47709a9 7870 struct drm_device *dev = crtc->base.dev;
fac5e23e 7871 struct drm_i915_private *dev_priv = to_i915(dev);
eb1cbe48 7872 u32 dpll;
190f68c5 7873 struct dpll *clock = &crtc_state->dpll;
eb1cbe48 7874
190f68c5 7875 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
2a8f64ca 7876
eb1cbe48
DV
7877 dpll = DPLL_VGA_MODE_DIS;
7878
2d84d2b3 7879 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
eb1cbe48
DV
7880 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7881 } else {
7882 if (clock->p1 == 2)
7883 dpll |= PLL_P1_DIVIDE_BY_TWO;
7884 else
7885 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7886 if (clock->p2 == 4)
7887 dpll |= PLL_P2_DIVIDE_BY_4;
7888 }
7889
2d84d2b3 7890 if (!IS_I830(dev) && intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DVO))
4a33e48d
DV
7891 dpll |= DPLL_DVO_2X_MODE;
7892
2d84d2b3 7893 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
ceb41007 7894 intel_panel_use_ssc(dev_priv))
eb1cbe48
DV
7895 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7896 else
7897 dpll |= PLL_REF_INPUT_DREFCLK;
7898
7899 dpll |= DPLL_VCO_ENABLE;
190f68c5 7900 crtc_state->dpll_hw_state.dpll = dpll;
eb1cbe48
DV
7901}
7902
8a654f3b 7903static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
b0e77b9c
PZ
7904{
7905 struct drm_device *dev = intel_crtc->base.dev;
fac5e23e 7906 struct drm_i915_private *dev_priv = to_i915(dev);
b0e77b9c 7907 enum pipe pipe = intel_crtc->pipe;
6e3c9717 7908 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
7c5f93b0 7909 const struct drm_display_mode *adjusted_mode = &intel_crtc->config->base.adjusted_mode;
1caea6e9
VS
7910 uint32_t crtc_vtotal, crtc_vblank_end;
7911 int vsyncshift = 0;
4d8a62ea
DV
7912
7913 /* We need to be careful not to changed the adjusted mode, for otherwise
7914 * the hw state checker will get angry at the mismatch. */
7915 crtc_vtotal = adjusted_mode->crtc_vtotal;
7916 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
b0e77b9c 7917
609aeaca 7918 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
b0e77b9c 7919 /* the chip adds 2 halflines automatically */
4d8a62ea
DV
7920 crtc_vtotal -= 1;
7921 crtc_vblank_end -= 1;
609aeaca 7922
2d84d2b3 7923 if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
609aeaca
VS
7924 vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
7925 else
7926 vsyncshift = adjusted_mode->crtc_hsync_start -
7927 adjusted_mode->crtc_htotal / 2;
1caea6e9
VS
7928 if (vsyncshift < 0)
7929 vsyncshift += adjusted_mode->crtc_htotal;
b0e77b9c
PZ
7930 }
7931
7932 if (INTEL_INFO(dev)->gen > 3)
fe2b8f9d 7933 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
b0e77b9c 7934
fe2b8f9d 7935 I915_WRITE(HTOTAL(cpu_transcoder),
b0e77b9c
PZ
7936 (adjusted_mode->crtc_hdisplay - 1) |
7937 ((adjusted_mode->crtc_htotal - 1) << 16));
fe2b8f9d 7938 I915_WRITE(HBLANK(cpu_transcoder),
b0e77b9c
PZ
7939 (adjusted_mode->crtc_hblank_start - 1) |
7940 ((adjusted_mode->crtc_hblank_end - 1) << 16));
fe2b8f9d 7941 I915_WRITE(HSYNC(cpu_transcoder),
b0e77b9c
PZ
7942 (adjusted_mode->crtc_hsync_start - 1) |
7943 ((adjusted_mode->crtc_hsync_end - 1) << 16));
7944
fe2b8f9d 7945 I915_WRITE(VTOTAL(cpu_transcoder),
b0e77b9c 7946 (adjusted_mode->crtc_vdisplay - 1) |
4d8a62ea 7947 ((crtc_vtotal - 1) << 16));
fe2b8f9d 7948 I915_WRITE(VBLANK(cpu_transcoder),
b0e77b9c 7949 (adjusted_mode->crtc_vblank_start - 1) |
4d8a62ea 7950 ((crtc_vblank_end - 1) << 16));
fe2b8f9d 7951 I915_WRITE(VSYNC(cpu_transcoder),
b0e77b9c
PZ
7952 (adjusted_mode->crtc_vsync_start - 1) |
7953 ((adjusted_mode->crtc_vsync_end - 1) << 16));
7954
b5e508d4
PZ
7955 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
7956 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
7957 * documented on the DDI_FUNC_CTL register description, EDP Input Select
7958 * bits. */
7959 if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
7960 (pipe == PIPE_B || pipe == PIPE_C))
7961 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
7962
bc58be60
JN
7963}
7964
7965static void intel_set_pipe_src_size(struct intel_crtc *intel_crtc)
7966{
7967 struct drm_device *dev = intel_crtc->base.dev;
fac5e23e 7968 struct drm_i915_private *dev_priv = to_i915(dev);
bc58be60
JN
7969 enum pipe pipe = intel_crtc->pipe;
7970
b0e77b9c
PZ
7971 /* pipesrc controls the size that is scaled from, which should
7972 * always be the user's requested size.
7973 */
7974 I915_WRITE(PIPESRC(pipe),
6e3c9717
ACO
7975 ((intel_crtc->config->pipe_src_w - 1) << 16) |
7976 (intel_crtc->config->pipe_src_h - 1));
b0e77b9c
PZ
7977}
7978
1bd1bd80 7979static void intel_get_pipe_timings(struct intel_crtc *crtc,
5cec258b 7980 struct intel_crtc_state *pipe_config)
1bd1bd80
DV
7981{
7982 struct drm_device *dev = crtc->base.dev;
fac5e23e 7983 struct drm_i915_private *dev_priv = to_i915(dev);
1bd1bd80
DV
7984 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
7985 uint32_t tmp;
7986
7987 tmp = I915_READ(HTOTAL(cpu_transcoder));
2d112de7
ACO
7988 pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
7989 pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 7990 tmp = I915_READ(HBLANK(cpu_transcoder));
2d112de7
ACO
7991 pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
7992 pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 7993 tmp = I915_READ(HSYNC(cpu_transcoder));
2d112de7
ACO
7994 pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
7995 pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80
DV
7996
7997 tmp = I915_READ(VTOTAL(cpu_transcoder));
2d112de7
ACO
7998 pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
7999 pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 8000 tmp = I915_READ(VBLANK(cpu_transcoder));
2d112de7
ACO
8001 pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
8002 pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 8003 tmp = I915_READ(VSYNC(cpu_transcoder));
2d112de7
ACO
8004 pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
8005 pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80
DV
8006
8007 if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
2d112de7
ACO
8008 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
8009 pipe_config->base.adjusted_mode.crtc_vtotal += 1;
8010 pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
1bd1bd80 8011 }
bc58be60
JN
8012}
8013
8014static void intel_get_pipe_src_size(struct intel_crtc *crtc,
8015 struct intel_crtc_state *pipe_config)
8016{
8017 struct drm_device *dev = crtc->base.dev;
fac5e23e 8018 struct drm_i915_private *dev_priv = to_i915(dev);
bc58be60 8019 u32 tmp;
1bd1bd80
DV
8020
8021 tmp = I915_READ(PIPESRC(crtc->pipe));
37327abd
VS
8022 pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
8023 pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
8024
2d112de7
ACO
8025 pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
8026 pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
1bd1bd80
DV
8027}
8028
f6a83288 8029void intel_mode_from_pipe_config(struct drm_display_mode *mode,
5cec258b 8030 struct intel_crtc_state *pipe_config)
babea61d 8031{
2d112de7
ACO
8032 mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
8033 mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
8034 mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
8035 mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
babea61d 8036
2d112de7
ACO
8037 mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
8038 mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
8039 mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
8040 mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
babea61d 8041
2d112de7 8042 mode->flags = pipe_config->base.adjusted_mode.flags;
cd13f5ab 8043 mode->type = DRM_MODE_TYPE_DRIVER;
babea61d 8044
2d112de7
ACO
8045 mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
8046 mode->flags |= pipe_config->base.adjusted_mode.flags;
cd13f5ab
ML
8047
8048 mode->hsync = drm_mode_hsync(mode);
8049 mode->vrefresh = drm_mode_vrefresh(mode);
8050 drm_mode_set_name(mode);
babea61d
JB
8051}
8052
84b046f3
DV
8053static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
8054{
8055 struct drm_device *dev = intel_crtc->base.dev;
fac5e23e 8056 struct drm_i915_private *dev_priv = to_i915(dev);
84b046f3
DV
8057 uint32_t pipeconf;
8058
9f11a9e4 8059 pipeconf = 0;
84b046f3 8060
b6b5d049
VS
8061 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
8062 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
8063 pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
67c72a12 8064
6e3c9717 8065 if (intel_crtc->config->double_wide)
cf532bb2 8066 pipeconf |= PIPECONF_DOUBLE_WIDE;
84b046f3 8067
ff9ce46e 8068 /* only g4x and later have fancy bpc/dither controls */
666a4537 8069 if (IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
ff9ce46e 8070 /* Bspec claims that we can't use dithering for 30bpp pipes. */
6e3c9717 8071 if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
ff9ce46e 8072 pipeconf |= PIPECONF_DITHER_EN |
84b046f3 8073 PIPECONF_DITHER_TYPE_SP;
84b046f3 8074
6e3c9717 8075 switch (intel_crtc->config->pipe_bpp) {
ff9ce46e
DV
8076 case 18:
8077 pipeconf |= PIPECONF_6BPC;
8078 break;
8079 case 24:
8080 pipeconf |= PIPECONF_8BPC;
8081 break;
8082 case 30:
8083 pipeconf |= PIPECONF_10BPC;
8084 break;
8085 default:
8086 /* Case prevented by intel_choose_pipe_bpp_dither. */
8087 BUG();
84b046f3
DV
8088 }
8089 }
8090
8091 if (HAS_PIPE_CXSR(dev)) {
8092 if (intel_crtc->lowfreq_avail) {
8093 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
8094 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
8095 } else {
8096 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
84b046f3
DV
8097 }
8098 }
8099
6e3c9717 8100 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
efc2cfff 8101 if (INTEL_INFO(dev)->gen < 4 ||
2d84d2b3 8102 intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
efc2cfff
VS
8103 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
8104 else
8105 pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
8106 } else
84b046f3
DV
8107 pipeconf |= PIPECONF_PROGRESSIVE;
8108
666a4537
WB
8109 if ((IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) &&
8110 intel_crtc->config->limited_color_range)
9f11a9e4 8111 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
9c8e09b7 8112
84b046f3
DV
8113 I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
8114 POSTING_READ(PIPECONF(intel_crtc->pipe));
8115}
8116
81c97f52
ACO
8117static int i8xx_crtc_compute_clock(struct intel_crtc *crtc,
8118 struct intel_crtc_state *crtc_state)
8119{
8120 struct drm_device *dev = crtc->base.dev;
fac5e23e 8121 struct drm_i915_private *dev_priv = to_i915(dev);
1b6f4958 8122 const struct intel_limit *limit;
81c97f52
ACO
8123 int refclk = 48000;
8124
8125 memset(&crtc_state->dpll_hw_state, 0,
8126 sizeof(crtc_state->dpll_hw_state));
8127
2d84d2b3 8128 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
81c97f52
ACO
8129 if (intel_panel_use_ssc(dev_priv)) {
8130 refclk = dev_priv->vbt.lvds_ssc_freq;
8131 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
8132 }
8133
8134 limit = &intel_limits_i8xx_lvds;
2d84d2b3 8135 } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DVO)) {
81c97f52
ACO
8136 limit = &intel_limits_i8xx_dvo;
8137 } else {
8138 limit = &intel_limits_i8xx_dac;
8139 }
8140
8141 if (!crtc_state->clock_set &&
8142 !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8143 refclk, NULL, &crtc_state->dpll)) {
8144 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8145 return -EINVAL;
8146 }
8147
8148 i8xx_compute_dpll(crtc, crtc_state, NULL);
8149
8150 return 0;
8151}
8152
19ec6693
ACO
8153static int g4x_crtc_compute_clock(struct intel_crtc *crtc,
8154 struct intel_crtc_state *crtc_state)
8155{
8156 struct drm_device *dev = crtc->base.dev;
fac5e23e 8157 struct drm_i915_private *dev_priv = to_i915(dev);
1b6f4958 8158 const struct intel_limit *limit;
19ec6693
ACO
8159 int refclk = 96000;
8160
8161 memset(&crtc_state->dpll_hw_state, 0,
8162 sizeof(crtc_state->dpll_hw_state));
8163
2d84d2b3 8164 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
19ec6693
ACO
8165 if (intel_panel_use_ssc(dev_priv)) {
8166 refclk = dev_priv->vbt.lvds_ssc_freq;
8167 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
8168 }
8169
8170 if (intel_is_dual_link_lvds(dev))
8171 limit = &intel_limits_g4x_dual_channel_lvds;
8172 else
8173 limit = &intel_limits_g4x_single_channel_lvds;
2d84d2b3
VS
8174 } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI) ||
8175 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
19ec6693 8176 limit = &intel_limits_g4x_hdmi;
2d84d2b3 8177 } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO)) {
19ec6693
ACO
8178 limit = &intel_limits_g4x_sdvo;
8179 } else {
8180 /* The option is for other outputs */
8181 limit = &intel_limits_i9xx_sdvo;
8182 }
8183
8184 if (!crtc_state->clock_set &&
8185 !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8186 refclk, NULL, &crtc_state->dpll)) {
8187 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8188 return -EINVAL;
8189 }
8190
8191 i9xx_compute_dpll(crtc, crtc_state, NULL);
8192
8193 return 0;
8194}
8195
70e8aa21
ACO
8196static int pnv_crtc_compute_clock(struct intel_crtc *crtc,
8197 struct intel_crtc_state *crtc_state)
8198{
8199 struct drm_device *dev = crtc->base.dev;
fac5e23e 8200 struct drm_i915_private *dev_priv = to_i915(dev);
1b6f4958 8201 const struct intel_limit *limit;
70e8aa21
ACO
8202 int refclk = 96000;
8203
8204 memset(&crtc_state->dpll_hw_state, 0,
8205 sizeof(crtc_state->dpll_hw_state));
8206
2d84d2b3 8207 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
70e8aa21
ACO
8208 if (intel_panel_use_ssc(dev_priv)) {
8209 refclk = dev_priv->vbt.lvds_ssc_freq;
8210 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
8211 }
8212
8213 limit = &intel_limits_pineview_lvds;
8214 } else {
8215 limit = &intel_limits_pineview_sdvo;
8216 }
8217
8218 if (!crtc_state->clock_set &&
8219 !pnv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8220 refclk, NULL, &crtc_state->dpll)) {
8221 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8222 return -EINVAL;
8223 }
8224
8225 i9xx_compute_dpll(crtc, crtc_state, NULL);
8226
8227 return 0;
8228}
8229
190f68c5
ACO
8230static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
8231 struct intel_crtc_state *crtc_state)
79e53945 8232{
c7653199 8233 struct drm_device *dev = crtc->base.dev;
fac5e23e 8234 struct drm_i915_private *dev_priv = to_i915(dev);
1b6f4958 8235 const struct intel_limit *limit;
81c97f52 8236 int refclk = 96000;
79e53945 8237
dd3cd74a
ACO
8238 memset(&crtc_state->dpll_hw_state, 0,
8239 sizeof(crtc_state->dpll_hw_state));
8240
2d84d2b3 8241 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
70e8aa21
ACO
8242 if (intel_panel_use_ssc(dev_priv)) {
8243 refclk = dev_priv->vbt.lvds_ssc_freq;
8244 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
8245 }
43565a06 8246
70e8aa21
ACO
8247 limit = &intel_limits_i9xx_lvds;
8248 } else {
8249 limit = &intel_limits_i9xx_sdvo;
81c97f52 8250 }
79e53945 8251
70e8aa21
ACO
8252 if (!crtc_state->clock_set &&
8253 !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8254 refclk, NULL, &crtc_state->dpll)) {
8255 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8256 return -EINVAL;
f47709a9 8257 }
7026d4ac 8258
81c97f52 8259 i9xx_compute_dpll(crtc, crtc_state, NULL);
79e53945 8260
c8f7a0db 8261 return 0;
f564048e
EA
8262}
8263
65b3d6a9
ACO
8264static int chv_crtc_compute_clock(struct intel_crtc *crtc,
8265 struct intel_crtc_state *crtc_state)
8266{
8267 int refclk = 100000;
1b6f4958 8268 const struct intel_limit *limit = &intel_limits_chv;
65b3d6a9
ACO
8269
8270 memset(&crtc_state->dpll_hw_state, 0,
8271 sizeof(crtc_state->dpll_hw_state));
8272
65b3d6a9
ACO
8273 if (!crtc_state->clock_set &&
8274 !chv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8275 refclk, NULL, &crtc_state->dpll)) {
8276 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8277 return -EINVAL;
8278 }
8279
8280 chv_compute_dpll(crtc, crtc_state);
8281
8282 return 0;
8283}
8284
8285static int vlv_crtc_compute_clock(struct intel_crtc *crtc,
8286 struct intel_crtc_state *crtc_state)
8287{
8288 int refclk = 100000;
1b6f4958 8289 const struct intel_limit *limit = &intel_limits_vlv;
65b3d6a9
ACO
8290
8291 memset(&crtc_state->dpll_hw_state, 0,
8292 sizeof(crtc_state->dpll_hw_state));
8293
65b3d6a9
ACO
8294 if (!crtc_state->clock_set &&
8295 !vlv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8296 refclk, NULL, &crtc_state->dpll)) {
8297 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8298 return -EINVAL;
8299 }
8300
8301 vlv_compute_dpll(crtc, crtc_state);
8302
8303 return 0;
8304}
8305
2fa2fe9a 8306static void i9xx_get_pfit_config(struct intel_crtc *crtc,
5cec258b 8307 struct intel_crtc_state *pipe_config)
2fa2fe9a
DV
8308{
8309 struct drm_device *dev = crtc->base.dev;
fac5e23e 8310 struct drm_i915_private *dev_priv = to_i915(dev);
2fa2fe9a
DV
8311 uint32_t tmp;
8312
dc9e7dec
VS
8313 if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
8314 return;
8315
2fa2fe9a 8316 tmp = I915_READ(PFIT_CONTROL);
06922821
DV
8317 if (!(tmp & PFIT_ENABLE))
8318 return;
2fa2fe9a 8319
06922821 8320 /* Check whether the pfit is attached to our pipe. */
2fa2fe9a
DV
8321 if (INTEL_INFO(dev)->gen < 4) {
8322 if (crtc->pipe != PIPE_B)
8323 return;
2fa2fe9a
DV
8324 } else {
8325 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
8326 return;
8327 }
8328
06922821 8329 pipe_config->gmch_pfit.control = tmp;
2fa2fe9a 8330 pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
2fa2fe9a
DV
8331}
8332
acbec814 8333static void vlv_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 8334 struct intel_crtc_state *pipe_config)
acbec814
JB
8335{
8336 struct drm_device *dev = crtc->base.dev;
fac5e23e 8337 struct drm_i915_private *dev_priv = to_i915(dev);
acbec814 8338 int pipe = pipe_config->cpu_transcoder;
9e2c8475 8339 struct dpll clock;
acbec814 8340 u32 mdiv;
662c6ecb 8341 int refclk = 100000;
acbec814 8342
b521973b
VS
8343 /* In case of DSI, DPLL will not be used */
8344 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
f573de5a
SK
8345 return;
8346
a580516d 8347 mutex_lock(&dev_priv->sb_lock);
ab3c759a 8348 mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
a580516d 8349 mutex_unlock(&dev_priv->sb_lock);
acbec814
JB
8350
8351 clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
8352 clock.m2 = mdiv & DPIO_M2DIV_MASK;
8353 clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
8354 clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
8355 clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
8356
dccbea3b 8357 pipe_config->port_clock = vlv_calc_dpll_params(refclk, &clock);
acbec814
JB
8358}
8359
5724dbd1
DL
8360static void
8361i9xx_get_initial_plane_config(struct intel_crtc *crtc,
8362 struct intel_initial_plane_config *plane_config)
1ad292b5
JB
8363{
8364 struct drm_device *dev = crtc->base.dev;
fac5e23e 8365 struct drm_i915_private *dev_priv = to_i915(dev);
1ad292b5
JB
8366 u32 val, base, offset;
8367 int pipe = crtc->pipe, plane = crtc->plane;
8368 int fourcc, pixel_format;
6761dd31 8369 unsigned int aligned_height;
b113d5ee 8370 struct drm_framebuffer *fb;
1b842c89 8371 struct intel_framebuffer *intel_fb;
1ad292b5 8372
42a7b088
DL
8373 val = I915_READ(DSPCNTR(plane));
8374 if (!(val & DISPLAY_PLANE_ENABLE))
8375 return;
8376
d9806c9f 8377 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
1b842c89 8378 if (!intel_fb) {
1ad292b5
JB
8379 DRM_DEBUG_KMS("failed to alloc fb\n");
8380 return;
8381 }
8382
1b842c89
DL
8383 fb = &intel_fb->base;
8384
18c5247e
DV
8385 if (INTEL_INFO(dev)->gen >= 4) {
8386 if (val & DISPPLANE_TILED) {
49af449b 8387 plane_config->tiling = I915_TILING_X;
18c5247e
DV
8388 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
8389 }
8390 }
1ad292b5
JB
8391
8392 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
b35d63fa 8393 fourcc = i9xx_format_to_fourcc(pixel_format);
b113d5ee
DL
8394 fb->pixel_format = fourcc;
8395 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
1ad292b5
JB
8396
8397 if (INTEL_INFO(dev)->gen >= 4) {
49af449b 8398 if (plane_config->tiling)
1ad292b5
JB
8399 offset = I915_READ(DSPTILEOFF(plane));
8400 else
8401 offset = I915_READ(DSPLINOFF(plane));
8402 base = I915_READ(DSPSURF(plane)) & 0xfffff000;
8403 } else {
8404 base = I915_READ(DSPADDR(plane));
8405 }
8406 plane_config->base = base;
8407
8408 val = I915_READ(PIPESRC(pipe));
b113d5ee
DL
8409 fb->width = ((val >> 16) & 0xfff) + 1;
8410 fb->height = ((val >> 0) & 0xfff) + 1;
1ad292b5
JB
8411
8412 val = I915_READ(DSPSTRIDE(pipe));
b113d5ee 8413 fb->pitches[0] = val & 0xffffffc0;
1ad292b5 8414
b113d5ee 8415 aligned_height = intel_fb_align_height(dev, fb->height,
091df6cb
DV
8416 fb->pixel_format,
8417 fb->modifier[0]);
1ad292b5 8418
f37b5c2b 8419 plane_config->size = fb->pitches[0] * aligned_height;
1ad292b5 8420
2844a921
DL
8421 DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
8422 pipe_name(pipe), plane, fb->width, fb->height,
8423 fb->bits_per_pixel, base, fb->pitches[0],
8424 plane_config->size);
1ad292b5 8425
2d14030b 8426 plane_config->fb = intel_fb;
1ad292b5
JB
8427}
8428
70b23a98 8429static void chv_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 8430 struct intel_crtc_state *pipe_config)
70b23a98
VS
8431{
8432 struct drm_device *dev = crtc->base.dev;
fac5e23e 8433 struct drm_i915_private *dev_priv = to_i915(dev);
70b23a98
VS
8434 int pipe = pipe_config->cpu_transcoder;
8435 enum dpio_channel port = vlv_pipe_to_channel(pipe);
9e2c8475 8436 struct dpll clock;
0d7b6b11 8437 u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2, pll_dw3;
70b23a98
VS
8438 int refclk = 100000;
8439
b521973b
VS
8440 /* In case of DSI, DPLL will not be used */
8441 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
8442 return;
8443
a580516d 8444 mutex_lock(&dev_priv->sb_lock);
70b23a98
VS
8445 cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
8446 pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
8447 pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
8448 pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
0d7b6b11 8449 pll_dw3 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
a580516d 8450 mutex_unlock(&dev_priv->sb_lock);
70b23a98
VS
8451
8452 clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
0d7b6b11
ID
8453 clock.m2 = (pll_dw0 & 0xff) << 22;
8454 if (pll_dw3 & DPIO_CHV_FRAC_DIV_EN)
8455 clock.m2 |= pll_dw2 & 0x3fffff;
70b23a98
VS
8456 clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
8457 clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
8458 clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
8459
dccbea3b 8460 pipe_config->port_clock = chv_calc_dpll_params(refclk, &clock);
70b23a98
VS
8461}
8462
0e8ffe1b 8463static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
5cec258b 8464 struct intel_crtc_state *pipe_config)
0e8ffe1b
DV
8465{
8466 struct drm_device *dev = crtc->base.dev;
fac5e23e 8467 struct drm_i915_private *dev_priv = to_i915(dev);
1729050e 8468 enum intel_display_power_domain power_domain;
0e8ffe1b 8469 uint32_t tmp;
1729050e 8470 bool ret;
0e8ffe1b 8471
1729050e
ID
8472 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
8473 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
b5482bd0
ID
8474 return false;
8475
e143a21c 8476 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
8106ddbd 8477 pipe_config->shared_dpll = NULL;
eccb140b 8478
1729050e
ID
8479 ret = false;
8480
0e8ffe1b
DV
8481 tmp = I915_READ(PIPECONF(crtc->pipe));
8482 if (!(tmp & PIPECONF_ENABLE))
1729050e 8483 goto out;
0e8ffe1b 8484
666a4537 8485 if (IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
42571aef
VS
8486 switch (tmp & PIPECONF_BPC_MASK) {
8487 case PIPECONF_6BPC:
8488 pipe_config->pipe_bpp = 18;
8489 break;
8490 case PIPECONF_8BPC:
8491 pipe_config->pipe_bpp = 24;
8492 break;
8493 case PIPECONF_10BPC:
8494 pipe_config->pipe_bpp = 30;
8495 break;
8496 default:
8497 break;
8498 }
8499 }
8500
666a4537
WB
8501 if ((IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) &&
8502 (tmp & PIPECONF_COLOR_RANGE_SELECT))
b5a9fa09
DV
8503 pipe_config->limited_color_range = true;
8504
282740f7
VS
8505 if (INTEL_INFO(dev)->gen < 4)
8506 pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
8507
1bd1bd80 8508 intel_get_pipe_timings(crtc, pipe_config);
bc58be60 8509 intel_get_pipe_src_size(crtc, pipe_config);
1bd1bd80 8510
2fa2fe9a
DV
8511 i9xx_get_pfit_config(crtc, pipe_config);
8512
6c49f241 8513 if (INTEL_INFO(dev)->gen >= 4) {
c231775c
VS
8514 /* No way to read it out on pipes B and C */
8515 if (IS_CHERRYVIEW(dev) && crtc->pipe != PIPE_A)
8516 tmp = dev_priv->chv_dpll_md[crtc->pipe];
8517 else
8518 tmp = I915_READ(DPLL_MD(crtc->pipe));
6c49f241
DV
8519 pipe_config->pixel_multiplier =
8520 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
8521 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
8bcc2795 8522 pipe_config->dpll_hw_state.dpll_md = tmp;
6c49f241
DV
8523 } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
8524 tmp = I915_READ(DPLL(crtc->pipe));
8525 pipe_config->pixel_multiplier =
8526 ((tmp & SDVO_MULTIPLIER_MASK)
8527 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
8528 } else {
8529 /* Note that on i915G/GM the pixel multiplier is in the sdvo
8530 * port and will be fixed up in the encoder->get_config
8531 * function. */
8532 pipe_config->pixel_multiplier = 1;
8533 }
8bcc2795 8534 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
666a4537 8535 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
1c4e0274
VS
8536 /*
8537 * DPLL_DVO_2X_MODE must be enabled for both DPLLs
8538 * on 830. Filter it out here so that we don't
8539 * report errors due to that.
8540 */
8541 if (IS_I830(dev))
8542 pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
8543
8bcc2795
DV
8544 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
8545 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
165e901c
VS
8546 } else {
8547 /* Mask out read-only status bits. */
8548 pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
8549 DPLL_PORTC_READY_MASK |
8550 DPLL_PORTB_READY_MASK);
8bcc2795 8551 }
6c49f241 8552
70b23a98
VS
8553 if (IS_CHERRYVIEW(dev))
8554 chv_crtc_clock_get(crtc, pipe_config);
8555 else if (IS_VALLEYVIEW(dev))
acbec814
JB
8556 vlv_crtc_clock_get(crtc, pipe_config);
8557 else
8558 i9xx_crtc_clock_get(crtc, pipe_config);
18442d08 8559
0f64614d
VS
8560 /*
8561 * Normally the dotclock is filled in by the encoder .get_config()
8562 * but in case the pipe is enabled w/o any ports we need a sane
8563 * default.
8564 */
8565 pipe_config->base.adjusted_mode.crtc_clock =
8566 pipe_config->port_clock / pipe_config->pixel_multiplier;
8567
1729050e
ID
8568 ret = true;
8569
8570out:
8571 intel_display_power_put(dev_priv, power_domain);
8572
8573 return ret;
0e8ffe1b
DV
8574}
8575
dde86e2d 8576static void ironlake_init_pch_refclk(struct drm_device *dev)
13d83a67 8577{
fac5e23e 8578 struct drm_i915_private *dev_priv = to_i915(dev);
13d83a67 8579 struct intel_encoder *encoder;
1c1a24d2 8580 int i;
74cfd7ac 8581 u32 val, final;
13d83a67 8582 bool has_lvds = false;
199e5d79 8583 bool has_cpu_edp = false;
199e5d79 8584 bool has_panel = false;
99eb6a01
KP
8585 bool has_ck505 = false;
8586 bool can_ssc = false;
1c1a24d2 8587 bool using_ssc_source = false;
13d83a67
JB
8588
8589 /* We need to take the global config into account */
b2784e15 8590 for_each_intel_encoder(dev, encoder) {
199e5d79
KP
8591 switch (encoder->type) {
8592 case INTEL_OUTPUT_LVDS:
8593 has_panel = true;
8594 has_lvds = true;
8595 break;
8596 case INTEL_OUTPUT_EDP:
8597 has_panel = true;
2de6905f 8598 if (enc_to_dig_port(&encoder->base)->port == PORT_A)
199e5d79
KP
8599 has_cpu_edp = true;
8600 break;
6847d71b
PZ
8601 default:
8602 break;
13d83a67
JB
8603 }
8604 }
8605
99eb6a01 8606 if (HAS_PCH_IBX(dev)) {
41aa3448 8607 has_ck505 = dev_priv->vbt.display_clock_mode;
99eb6a01
KP
8608 can_ssc = has_ck505;
8609 } else {
8610 has_ck505 = false;
8611 can_ssc = true;
8612 }
8613
1c1a24d2
L
8614 /* Check if any DPLLs are using the SSC source */
8615 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
8616 u32 temp = I915_READ(PCH_DPLL(i));
8617
8618 if (!(temp & DPLL_VCO_ENABLE))
8619 continue;
8620
8621 if ((temp & PLL_REF_INPUT_MASK) ==
8622 PLLB_REF_INPUT_SPREADSPECTRUMIN) {
8623 using_ssc_source = true;
8624 break;
8625 }
8626 }
8627
8628 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d using_ssc_source %d\n",
8629 has_panel, has_lvds, has_ck505, using_ssc_source);
13d83a67
JB
8630
8631 /* Ironlake: try to setup display ref clock before DPLL
8632 * enabling. This is only under driver's control after
8633 * PCH B stepping, previous chipset stepping should be
8634 * ignoring this setting.
8635 */
74cfd7ac
CW
8636 val = I915_READ(PCH_DREF_CONTROL);
8637
8638 /* As we must carefully and slowly disable/enable each source in turn,
8639 * compute the final state we want first and check if we need to
8640 * make any changes at all.
8641 */
8642 final = val;
8643 final &= ~DREF_NONSPREAD_SOURCE_MASK;
8644 if (has_ck505)
8645 final |= DREF_NONSPREAD_CK505_ENABLE;
8646 else
8647 final |= DREF_NONSPREAD_SOURCE_ENABLE;
8648
8c07eb68 8649 final &= ~DREF_SSC_SOURCE_MASK;
74cfd7ac 8650 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
8c07eb68 8651 final &= ~DREF_SSC1_ENABLE;
74cfd7ac
CW
8652
8653 if (has_panel) {
8654 final |= DREF_SSC_SOURCE_ENABLE;
8655
8656 if (intel_panel_use_ssc(dev_priv) && can_ssc)
8657 final |= DREF_SSC1_ENABLE;
8658
8659 if (has_cpu_edp) {
8660 if (intel_panel_use_ssc(dev_priv) && can_ssc)
8661 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
8662 else
8663 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
8664 } else
8665 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
1c1a24d2
L
8666 } else if (using_ssc_source) {
8667 final |= DREF_SSC_SOURCE_ENABLE;
8668 final |= DREF_SSC1_ENABLE;
74cfd7ac
CW
8669 }
8670
8671 if (final == val)
8672 return;
8673
13d83a67 8674 /* Always enable nonspread source */
74cfd7ac 8675 val &= ~DREF_NONSPREAD_SOURCE_MASK;
13d83a67 8676
99eb6a01 8677 if (has_ck505)
74cfd7ac 8678 val |= DREF_NONSPREAD_CK505_ENABLE;
99eb6a01 8679 else
74cfd7ac 8680 val |= DREF_NONSPREAD_SOURCE_ENABLE;
13d83a67 8681
199e5d79 8682 if (has_panel) {
74cfd7ac
CW
8683 val &= ~DREF_SSC_SOURCE_MASK;
8684 val |= DREF_SSC_SOURCE_ENABLE;
13d83a67 8685
199e5d79 8686 /* SSC must be turned on before enabling the CPU output */
99eb6a01 8687 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
199e5d79 8688 DRM_DEBUG_KMS("Using SSC on panel\n");
74cfd7ac 8689 val |= DREF_SSC1_ENABLE;
e77166b5 8690 } else
74cfd7ac 8691 val &= ~DREF_SSC1_ENABLE;
199e5d79
KP
8692
8693 /* Get SSC going before enabling the outputs */
74cfd7ac 8694 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
8695 POSTING_READ(PCH_DREF_CONTROL);
8696 udelay(200);
8697
74cfd7ac 8698 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
13d83a67
JB
8699
8700 /* Enable CPU source on CPU attached eDP */
199e5d79 8701 if (has_cpu_edp) {
99eb6a01 8702 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
199e5d79 8703 DRM_DEBUG_KMS("Using SSC on eDP\n");
74cfd7ac 8704 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
eba905b2 8705 } else
74cfd7ac 8706 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
199e5d79 8707 } else
74cfd7ac 8708 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
199e5d79 8709
74cfd7ac 8710 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
8711 POSTING_READ(PCH_DREF_CONTROL);
8712 udelay(200);
8713 } else {
1c1a24d2 8714 DRM_DEBUG_KMS("Disabling CPU source output\n");
199e5d79 8715
74cfd7ac 8716 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
199e5d79
KP
8717
8718 /* Turn off CPU output */
74cfd7ac 8719 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
199e5d79 8720
74cfd7ac 8721 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
8722 POSTING_READ(PCH_DREF_CONTROL);
8723 udelay(200);
8724
1c1a24d2
L
8725 if (!using_ssc_source) {
8726 DRM_DEBUG_KMS("Disabling SSC source\n");
199e5d79 8727
1c1a24d2
L
8728 /* Turn off the SSC source */
8729 val &= ~DREF_SSC_SOURCE_MASK;
8730 val |= DREF_SSC_SOURCE_DISABLE;
f165d283 8731
1c1a24d2
L
8732 /* Turn off SSC1 */
8733 val &= ~DREF_SSC1_ENABLE;
8734
8735 I915_WRITE(PCH_DREF_CONTROL, val);
8736 POSTING_READ(PCH_DREF_CONTROL);
8737 udelay(200);
8738 }
13d83a67 8739 }
74cfd7ac
CW
8740
8741 BUG_ON(val != final);
13d83a67
JB
8742}
8743
f31f2d55 8744static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
dde86e2d 8745{
f31f2d55 8746 uint32_t tmp;
dde86e2d 8747
0ff066a9
PZ
8748 tmp = I915_READ(SOUTH_CHICKEN2);
8749 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
8750 I915_WRITE(SOUTH_CHICKEN2, tmp);
dde86e2d 8751
cf3598c2
ID
8752 if (wait_for_us(I915_READ(SOUTH_CHICKEN2) &
8753 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
0ff066a9 8754 DRM_ERROR("FDI mPHY reset assert timeout\n");
dde86e2d 8755
0ff066a9
PZ
8756 tmp = I915_READ(SOUTH_CHICKEN2);
8757 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
8758 I915_WRITE(SOUTH_CHICKEN2, tmp);
dde86e2d 8759
cf3598c2
ID
8760 if (wait_for_us((I915_READ(SOUTH_CHICKEN2) &
8761 FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
0ff066a9 8762 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
f31f2d55
PZ
8763}
8764
8765/* WaMPhyProgramming:hsw */
8766static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
8767{
8768 uint32_t tmp;
dde86e2d
PZ
8769
8770 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
8771 tmp &= ~(0xFF << 24);
8772 tmp |= (0x12 << 24);
8773 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
8774
dde86e2d
PZ
8775 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
8776 tmp |= (1 << 11);
8777 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
8778
8779 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
8780 tmp |= (1 << 11);
8781 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
8782
dde86e2d
PZ
8783 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
8784 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
8785 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
8786
8787 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
8788 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
8789 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
8790
0ff066a9
PZ
8791 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
8792 tmp &= ~(7 << 13);
8793 tmp |= (5 << 13);
8794 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
dde86e2d 8795
0ff066a9
PZ
8796 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
8797 tmp &= ~(7 << 13);
8798 tmp |= (5 << 13);
8799 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
dde86e2d
PZ
8800
8801 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
8802 tmp &= ~0xFF;
8803 tmp |= 0x1C;
8804 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
8805
8806 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
8807 tmp &= ~0xFF;
8808 tmp |= 0x1C;
8809 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
8810
8811 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
8812 tmp &= ~(0xFF << 16);
8813 tmp |= (0x1C << 16);
8814 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
8815
8816 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
8817 tmp &= ~(0xFF << 16);
8818 tmp |= (0x1C << 16);
8819 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
8820
0ff066a9
PZ
8821 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
8822 tmp |= (1 << 27);
8823 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
dde86e2d 8824
0ff066a9
PZ
8825 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
8826 tmp |= (1 << 27);
8827 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
dde86e2d 8828
0ff066a9
PZ
8829 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
8830 tmp &= ~(0xF << 28);
8831 tmp |= (4 << 28);
8832 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
dde86e2d 8833
0ff066a9
PZ
8834 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
8835 tmp &= ~(0xF << 28);
8836 tmp |= (4 << 28);
8837 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
f31f2d55
PZ
8838}
8839
2fa86a1f
PZ
8840/* Implements 3 different sequences from BSpec chapter "Display iCLK
8841 * Programming" based on the parameters passed:
8842 * - Sequence to enable CLKOUT_DP
8843 * - Sequence to enable CLKOUT_DP without spread
8844 * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
8845 */
8846static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
8847 bool with_fdi)
f31f2d55 8848{
fac5e23e 8849 struct drm_i915_private *dev_priv = to_i915(dev);
2fa86a1f
PZ
8850 uint32_t reg, tmp;
8851
8852 if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
8853 with_spread = true;
c2699524 8854 if (WARN(HAS_PCH_LPT_LP(dev) && with_fdi, "LP PCH doesn't have FDI\n"))
2fa86a1f 8855 with_fdi = false;
f31f2d55 8856
a580516d 8857 mutex_lock(&dev_priv->sb_lock);
f31f2d55
PZ
8858
8859 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8860 tmp &= ~SBI_SSCCTL_DISABLE;
8861 tmp |= SBI_SSCCTL_PATHALT;
8862 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8863
8864 udelay(24);
8865
2fa86a1f
PZ
8866 if (with_spread) {
8867 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8868 tmp &= ~SBI_SSCCTL_PATHALT;
8869 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
f31f2d55 8870
2fa86a1f
PZ
8871 if (with_fdi) {
8872 lpt_reset_fdi_mphy(dev_priv);
8873 lpt_program_fdi_mphy(dev_priv);
8874 }
8875 }
dde86e2d 8876
c2699524 8877 reg = HAS_PCH_LPT_LP(dev) ? SBI_GEN0 : SBI_DBUFF0;
2fa86a1f
PZ
8878 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8879 tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8880 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
c00db246 8881
a580516d 8882 mutex_unlock(&dev_priv->sb_lock);
dde86e2d
PZ
8883}
8884
47701c3b
PZ
8885/* Sequence to disable CLKOUT_DP */
8886static void lpt_disable_clkout_dp(struct drm_device *dev)
8887{
fac5e23e 8888 struct drm_i915_private *dev_priv = to_i915(dev);
47701c3b
PZ
8889 uint32_t reg, tmp;
8890
a580516d 8891 mutex_lock(&dev_priv->sb_lock);
47701c3b 8892
c2699524 8893 reg = HAS_PCH_LPT_LP(dev) ? SBI_GEN0 : SBI_DBUFF0;
47701c3b
PZ
8894 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8895 tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8896 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
8897
8898 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8899 if (!(tmp & SBI_SSCCTL_DISABLE)) {
8900 if (!(tmp & SBI_SSCCTL_PATHALT)) {
8901 tmp |= SBI_SSCCTL_PATHALT;
8902 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8903 udelay(32);
8904 }
8905 tmp |= SBI_SSCCTL_DISABLE;
8906 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8907 }
8908
a580516d 8909 mutex_unlock(&dev_priv->sb_lock);
47701c3b
PZ
8910}
8911
f7be2c21
VS
8912#define BEND_IDX(steps) ((50 + (steps)) / 5)
8913
8914static const uint16_t sscdivintphase[] = {
8915 [BEND_IDX( 50)] = 0x3B23,
8916 [BEND_IDX( 45)] = 0x3B23,
8917 [BEND_IDX( 40)] = 0x3C23,
8918 [BEND_IDX( 35)] = 0x3C23,
8919 [BEND_IDX( 30)] = 0x3D23,
8920 [BEND_IDX( 25)] = 0x3D23,
8921 [BEND_IDX( 20)] = 0x3E23,
8922 [BEND_IDX( 15)] = 0x3E23,
8923 [BEND_IDX( 10)] = 0x3F23,
8924 [BEND_IDX( 5)] = 0x3F23,
8925 [BEND_IDX( 0)] = 0x0025,
8926 [BEND_IDX( -5)] = 0x0025,
8927 [BEND_IDX(-10)] = 0x0125,
8928 [BEND_IDX(-15)] = 0x0125,
8929 [BEND_IDX(-20)] = 0x0225,
8930 [BEND_IDX(-25)] = 0x0225,
8931 [BEND_IDX(-30)] = 0x0325,
8932 [BEND_IDX(-35)] = 0x0325,
8933 [BEND_IDX(-40)] = 0x0425,
8934 [BEND_IDX(-45)] = 0x0425,
8935 [BEND_IDX(-50)] = 0x0525,
8936};
8937
8938/*
8939 * Bend CLKOUT_DP
8940 * steps -50 to 50 inclusive, in steps of 5
8941 * < 0 slow down the clock, > 0 speed up the clock, 0 == no bend (135MHz)
8942 * change in clock period = -(steps / 10) * 5.787 ps
8943 */
8944static void lpt_bend_clkout_dp(struct drm_i915_private *dev_priv, int steps)
8945{
8946 uint32_t tmp;
8947 int idx = BEND_IDX(steps);
8948
8949 if (WARN_ON(steps % 5 != 0))
8950 return;
8951
8952 if (WARN_ON(idx >= ARRAY_SIZE(sscdivintphase)))
8953 return;
8954
8955 mutex_lock(&dev_priv->sb_lock);
8956
8957 if (steps % 10 != 0)
8958 tmp = 0xAAAAAAAB;
8959 else
8960 tmp = 0x00000000;
8961 intel_sbi_write(dev_priv, SBI_SSCDITHPHASE, tmp, SBI_ICLK);
8962
8963 tmp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE, SBI_ICLK);
8964 tmp &= 0xffff0000;
8965 tmp |= sscdivintphase[idx];
8966 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE, tmp, SBI_ICLK);
8967
8968 mutex_unlock(&dev_priv->sb_lock);
8969}
8970
8971#undef BEND_IDX
8972
bf8fa3d3
PZ
8973static void lpt_init_pch_refclk(struct drm_device *dev)
8974{
bf8fa3d3
PZ
8975 struct intel_encoder *encoder;
8976 bool has_vga = false;
8977
b2784e15 8978 for_each_intel_encoder(dev, encoder) {
bf8fa3d3
PZ
8979 switch (encoder->type) {
8980 case INTEL_OUTPUT_ANALOG:
8981 has_vga = true;
8982 break;
6847d71b
PZ
8983 default:
8984 break;
bf8fa3d3
PZ
8985 }
8986 }
8987
f7be2c21
VS
8988 if (has_vga) {
8989 lpt_bend_clkout_dp(to_i915(dev), 0);
47701c3b 8990 lpt_enable_clkout_dp(dev, true, true);
f7be2c21 8991 } else {
47701c3b 8992 lpt_disable_clkout_dp(dev);
f7be2c21 8993 }
bf8fa3d3
PZ
8994}
8995
dde86e2d
PZ
8996/*
8997 * Initialize reference clocks when the driver loads
8998 */
8999void intel_init_pch_refclk(struct drm_device *dev)
9000{
9001 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
9002 ironlake_init_pch_refclk(dev);
9003 else if (HAS_PCH_LPT(dev))
9004 lpt_init_pch_refclk(dev);
9005}
9006
6ff93609 9007static void ironlake_set_pipeconf(struct drm_crtc *crtc)
79e53945 9008{
fac5e23e 9009 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
79e53945
JB
9010 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9011 int pipe = intel_crtc->pipe;
c8203565
PZ
9012 uint32_t val;
9013
78114071 9014 val = 0;
c8203565 9015
6e3c9717 9016 switch (intel_crtc->config->pipe_bpp) {
c8203565 9017 case 18:
dfd07d72 9018 val |= PIPECONF_6BPC;
c8203565
PZ
9019 break;
9020 case 24:
dfd07d72 9021 val |= PIPECONF_8BPC;
c8203565
PZ
9022 break;
9023 case 30:
dfd07d72 9024 val |= PIPECONF_10BPC;
c8203565
PZ
9025 break;
9026 case 36:
dfd07d72 9027 val |= PIPECONF_12BPC;
c8203565
PZ
9028 break;
9029 default:
cc769b62
PZ
9030 /* Case prevented by intel_choose_pipe_bpp_dither. */
9031 BUG();
c8203565
PZ
9032 }
9033
6e3c9717 9034 if (intel_crtc->config->dither)
c8203565
PZ
9035 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
9036
6e3c9717 9037 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
c8203565
PZ
9038 val |= PIPECONF_INTERLACED_ILK;
9039 else
9040 val |= PIPECONF_PROGRESSIVE;
9041
6e3c9717 9042 if (intel_crtc->config->limited_color_range)
3685a8f3 9043 val |= PIPECONF_COLOR_RANGE_SELECT;
3685a8f3 9044
c8203565
PZ
9045 I915_WRITE(PIPECONF(pipe), val);
9046 POSTING_READ(PIPECONF(pipe));
9047}
9048
6ff93609 9049static void haswell_set_pipeconf(struct drm_crtc *crtc)
ee2b0b38 9050{
fac5e23e 9051 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
ee2b0b38 9052 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6e3c9717 9053 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
391bf048 9054 u32 val = 0;
ee2b0b38 9055
391bf048 9056 if (IS_HASWELL(dev_priv) && intel_crtc->config->dither)
ee2b0b38
PZ
9057 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
9058
6e3c9717 9059 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
ee2b0b38
PZ
9060 val |= PIPECONF_INTERLACED_ILK;
9061 else
9062 val |= PIPECONF_PROGRESSIVE;
9063
702e7a56
PZ
9064 I915_WRITE(PIPECONF(cpu_transcoder), val);
9065 POSTING_READ(PIPECONF(cpu_transcoder));
391bf048
JN
9066}
9067
391bf048
JN
9068static void haswell_set_pipemisc(struct drm_crtc *crtc)
9069{
fac5e23e 9070 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
391bf048 9071 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
756f85cf 9072
391bf048
JN
9073 if (IS_BROADWELL(dev_priv) || INTEL_INFO(dev_priv)->gen >= 9) {
9074 u32 val = 0;
756f85cf 9075
6e3c9717 9076 switch (intel_crtc->config->pipe_bpp) {
756f85cf
PZ
9077 case 18:
9078 val |= PIPEMISC_DITHER_6_BPC;
9079 break;
9080 case 24:
9081 val |= PIPEMISC_DITHER_8_BPC;
9082 break;
9083 case 30:
9084 val |= PIPEMISC_DITHER_10_BPC;
9085 break;
9086 case 36:
9087 val |= PIPEMISC_DITHER_12_BPC;
9088 break;
9089 default:
9090 /* Case prevented by pipe_config_set_bpp. */
9091 BUG();
9092 }
9093
6e3c9717 9094 if (intel_crtc->config->dither)
756f85cf
PZ
9095 val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
9096
391bf048 9097 I915_WRITE(PIPEMISC(intel_crtc->pipe), val);
756f85cf 9098 }
ee2b0b38
PZ
9099}
9100
d4b1931c
PZ
9101int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
9102{
9103 /*
9104 * Account for spread spectrum to avoid
9105 * oversubscribing the link. Max center spread
9106 * is 2.5%; use 5% for safety's sake.
9107 */
9108 u32 bps = target_clock * bpp * 21 / 20;
619d4d04 9109 return DIV_ROUND_UP(bps, link_bw * 8);
d4b1931c
PZ
9110}
9111
7429e9d4 9112static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
6cf86a5e 9113{
7429e9d4 9114 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
f48d8f23
PZ
9115}
9116
b75ca6f6
ACO
9117static void ironlake_compute_dpll(struct intel_crtc *intel_crtc,
9118 struct intel_crtc_state *crtc_state,
9e2c8475 9119 struct dpll *reduced_clock)
79e53945 9120{
de13a2e3 9121 struct drm_crtc *crtc = &intel_crtc->base;
79e53945 9122 struct drm_device *dev = crtc->dev;
fac5e23e 9123 struct drm_i915_private *dev_priv = to_i915(dev);
b75ca6f6 9124 u32 dpll, fp, fp2;
3d6e9ee0 9125 int factor;
79e53945 9126
c1858123 9127 /* Enable autotuning of the PLL clock (if permissible) */
8febb297 9128 factor = 21;
3d6e9ee0 9129 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
8febb297 9130 if ((intel_panel_use_ssc(dev_priv) &&
e91e941b 9131 dev_priv->vbt.lvds_ssc_freq == 100000) ||
f0b44056 9132 (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
8febb297 9133 factor = 25;
190f68c5 9134 } else if (crtc_state->sdvo_tv_clock)
8febb297 9135 factor = 20;
c1858123 9136
b75ca6f6
ACO
9137 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
9138
190f68c5 9139 if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
b75ca6f6
ACO
9140 fp |= FP_CB_TUNE;
9141
9142 if (reduced_clock) {
9143 fp2 = i9xx_dpll_compute_fp(reduced_clock);
2c07245f 9144
b75ca6f6
ACO
9145 if (reduced_clock->m < factor * reduced_clock->n)
9146 fp2 |= FP_CB_TUNE;
9147 } else {
9148 fp2 = fp;
9149 }
9a7c7890 9150
5eddb70b 9151 dpll = 0;
2c07245f 9152
3d6e9ee0 9153 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS))
a07d6787
EA
9154 dpll |= DPLLB_MODE_LVDS;
9155 else
9156 dpll |= DPLLB_MODE_DAC_SERIAL;
198a037f 9157
190f68c5 9158 dpll |= (crtc_state->pixel_multiplier - 1)
ef1b460d 9159 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
198a037f 9160
3d6e9ee0
VS
9161 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO) ||
9162 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
4a33e48d 9163 dpll |= DPLL_SDVO_HIGH_SPEED;
3d6e9ee0 9164
37a5650b 9165 if (intel_crtc_has_dp_encoder(crtc_state))
4a33e48d 9166 dpll |= DPLL_SDVO_HIGH_SPEED;
79e53945 9167
a07d6787 9168 /* compute bitmask from p1 value */
190f68c5 9169 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
a07d6787 9170 /* also FPA1 */
190f68c5 9171 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
a07d6787 9172
190f68c5 9173 switch (crtc_state->dpll.p2) {
a07d6787
EA
9174 case 5:
9175 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
9176 break;
9177 case 7:
9178 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
9179 break;
9180 case 10:
9181 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
9182 break;
9183 case 14:
9184 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
9185 break;
79e53945
JB
9186 }
9187
3d6e9ee0
VS
9188 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
9189 intel_panel_use_ssc(dev_priv))
43565a06 9190 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
79e53945
JB
9191 else
9192 dpll |= PLL_REF_INPUT_DREFCLK;
9193
b75ca6f6
ACO
9194 dpll |= DPLL_VCO_ENABLE;
9195
9196 crtc_state->dpll_hw_state.dpll = dpll;
9197 crtc_state->dpll_hw_state.fp0 = fp;
9198 crtc_state->dpll_hw_state.fp1 = fp2;
de13a2e3
PZ
9199}
9200
190f68c5
ACO
9201static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
9202 struct intel_crtc_state *crtc_state)
de13a2e3 9203{
997c030c 9204 struct drm_device *dev = crtc->base.dev;
fac5e23e 9205 struct drm_i915_private *dev_priv = to_i915(dev);
9e2c8475 9206 struct dpll reduced_clock;
7ed9f894 9207 bool has_reduced_clock = false;
e2b78267 9208 struct intel_shared_dpll *pll;
1b6f4958 9209 const struct intel_limit *limit;
997c030c 9210 int refclk = 120000;
de13a2e3 9211
dd3cd74a
ACO
9212 memset(&crtc_state->dpll_hw_state, 0,
9213 sizeof(crtc_state->dpll_hw_state));
9214
ded220e2
ACO
9215 crtc->lowfreq_avail = false;
9216
9217 /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
9218 if (!crtc_state->has_pch_encoder)
9219 return 0;
79e53945 9220
2d84d2b3 9221 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
997c030c
ACO
9222 if (intel_panel_use_ssc(dev_priv)) {
9223 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
9224 dev_priv->vbt.lvds_ssc_freq);
9225 refclk = dev_priv->vbt.lvds_ssc_freq;
9226 }
9227
9228 if (intel_is_dual_link_lvds(dev)) {
9229 if (refclk == 100000)
9230 limit = &intel_limits_ironlake_dual_lvds_100m;
9231 else
9232 limit = &intel_limits_ironlake_dual_lvds;
9233 } else {
9234 if (refclk == 100000)
9235 limit = &intel_limits_ironlake_single_lvds_100m;
9236 else
9237 limit = &intel_limits_ironlake_single_lvds;
9238 }
9239 } else {
9240 limit = &intel_limits_ironlake_dac;
9241 }
9242
364ee29d 9243 if (!crtc_state->clock_set &&
997c030c
ACO
9244 !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
9245 refclk, NULL, &crtc_state->dpll)) {
364ee29d
ACO
9246 DRM_ERROR("Couldn't find PLL settings for mode!\n");
9247 return -EINVAL;
f47709a9 9248 }
79e53945 9249
b75ca6f6
ACO
9250 ironlake_compute_dpll(crtc, crtc_state,
9251 has_reduced_clock ? &reduced_clock : NULL);
66e985c0 9252
ded220e2
ACO
9253 pll = intel_get_shared_dpll(crtc, crtc_state, NULL);
9254 if (pll == NULL) {
9255 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
9256 pipe_name(crtc->pipe));
9257 return -EINVAL;
3fb37703 9258 }
79e53945 9259
2d84d2b3 9260 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
ded220e2 9261 has_reduced_clock)
c7653199 9262 crtc->lowfreq_avail = true;
e2b78267 9263
c8f7a0db 9264 return 0;
79e53945
JB
9265}
9266
eb14cb74
VS
9267static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
9268 struct intel_link_m_n *m_n)
9269{
9270 struct drm_device *dev = crtc->base.dev;
fac5e23e 9271 struct drm_i915_private *dev_priv = to_i915(dev);
eb14cb74
VS
9272 enum pipe pipe = crtc->pipe;
9273
9274 m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
9275 m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
9276 m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
9277 & ~TU_SIZE_MASK;
9278 m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
9279 m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
9280 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
9281}
9282
9283static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
9284 enum transcoder transcoder,
b95af8be
VK
9285 struct intel_link_m_n *m_n,
9286 struct intel_link_m_n *m2_n2)
72419203
DV
9287{
9288 struct drm_device *dev = crtc->base.dev;
fac5e23e 9289 struct drm_i915_private *dev_priv = to_i915(dev);
eb14cb74 9290 enum pipe pipe = crtc->pipe;
72419203 9291
eb14cb74
VS
9292 if (INTEL_INFO(dev)->gen >= 5) {
9293 m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
9294 m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
9295 m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
9296 & ~TU_SIZE_MASK;
9297 m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
9298 m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
9299 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
b95af8be
VK
9300 /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
9301 * gen < 8) and if DRRS is supported (to make sure the
9302 * registers are not unnecessarily read).
9303 */
9304 if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
6e3c9717 9305 crtc->config->has_drrs) {
b95af8be
VK
9306 m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
9307 m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
9308 m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
9309 & ~TU_SIZE_MASK;
9310 m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
9311 m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
9312 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
9313 }
eb14cb74
VS
9314 } else {
9315 m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
9316 m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
9317 m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
9318 & ~TU_SIZE_MASK;
9319 m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
9320 m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
9321 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
9322 }
9323}
9324
9325void intel_dp_get_m_n(struct intel_crtc *crtc,
5cec258b 9326 struct intel_crtc_state *pipe_config)
eb14cb74 9327{
681a8504 9328 if (pipe_config->has_pch_encoder)
eb14cb74
VS
9329 intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
9330 else
9331 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
b95af8be
VK
9332 &pipe_config->dp_m_n,
9333 &pipe_config->dp_m2_n2);
eb14cb74 9334}
72419203 9335
eb14cb74 9336static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
5cec258b 9337 struct intel_crtc_state *pipe_config)
eb14cb74
VS
9338{
9339 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
b95af8be 9340 &pipe_config->fdi_m_n, NULL);
72419203
DV
9341}
9342
bd2e244f 9343static void skylake_get_pfit_config(struct intel_crtc *crtc,
5cec258b 9344 struct intel_crtc_state *pipe_config)
bd2e244f
JB
9345{
9346 struct drm_device *dev = crtc->base.dev;
fac5e23e 9347 struct drm_i915_private *dev_priv = to_i915(dev);
a1b2278e
CK
9348 struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
9349 uint32_t ps_ctrl = 0;
9350 int id = -1;
9351 int i;
bd2e244f 9352
a1b2278e
CK
9353 /* find scaler attached to this pipe */
9354 for (i = 0; i < crtc->num_scalers; i++) {
9355 ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
9356 if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
9357 id = i;
9358 pipe_config->pch_pfit.enabled = true;
9359 pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
9360 pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
9361 break;
9362 }
9363 }
bd2e244f 9364
a1b2278e
CK
9365 scaler_state->scaler_id = id;
9366 if (id >= 0) {
9367 scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
9368 } else {
9369 scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
bd2e244f
JB
9370 }
9371}
9372
5724dbd1
DL
9373static void
9374skylake_get_initial_plane_config(struct intel_crtc *crtc,
9375 struct intel_initial_plane_config *plane_config)
bc8d7dff
DL
9376{
9377 struct drm_device *dev = crtc->base.dev;
fac5e23e 9378 struct drm_i915_private *dev_priv = to_i915(dev);
40f46283 9379 u32 val, base, offset, stride_mult, tiling;
bc8d7dff
DL
9380 int pipe = crtc->pipe;
9381 int fourcc, pixel_format;
6761dd31 9382 unsigned int aligned_height;
bc8d7dff 9383 struct drm_framebuffer *fb;
1b842c89 9384 struct intel_framebuffer *intel_fb;
bc8d7dff 9385
d9806c9f 9386 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
1b842c89 9387 if (!intel_fb) {
bc8d7dff
DL
9388 DRM_DEBUG_KMS("failed to alloc fb\n");
9389 return;
9390 }
9391
1b842c89
DL
9392 fb = &intel_fb->base;
9393
bc8d7dff 9394 val = I915_READ(PLANE_CTL(pipe, 0));
42a7b088
DL
9395 if (!(val & PLANE_CTL_ENABLE))
9396 goto error;
9397
bc8d7dff
DL
9398 pixel_format = val & PLANE_CTL_FORMAT_MASK;
9399 fourcc = skl_format_to_fourcc(pixel_format,
9400 val & PLANE_CTL_ORDER_RGBX,
9401 val & PLANE_CTL_ALPHA_MASK);
9402 fb->pixel_format = fourcc;
9403 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
9404
40f46283
DL
9405 tiling = val & PLANE_CTL_TILED_MASK;
9406 switch (tiling) {
9407 case PLANE_CTL_TILED_LINEAR:
9408 fb->modifier[0] = DRM_FORMAT_MOD_NONE;
9409 break;
9410 case PLANE_CTL_TILED_X:
9411 plane_config->tiling = I915_TILING_X;
9412 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
9413 break;
9414 case PLANE_CTL_TILED_Y:
9415 fb->modifier[0] = I915_FORMAT_MOD_Y_TILED;
9416 break;
9417 case PLANE_CTL_TILED_YF:
9418 fb->modifier[0] = I915_FORMAT_MOD_Yf_TILED;
9419 break;
9420 default:
9421 MISSING_CASE(tiling);
9422 goto error;
9423 }
9424
bc8d7dff
DL
9425 base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
9426 plane_config->base = base;
9427
9428 offset = I915_READ(PLANE_OFFSET(pipe, 0));
9429
9430 val = I915_READ(PLANE_SIZE(pipe, 0));
9431 fb->height = ((val >> 16) & 0xfff) + 1;
9432 fb->width = ((val >> 0) & 0x1fff) + 1;
9433
9434 val = I915_READ(PLANE_STRIDE(pipe, 0));
7b49f948 9435 stride_mult = intel_fb_stride_alignment(dev_priv, fb->modifier[0],
40f46283 9436 fb->pixel_format);
bc8d7dff
DL
9437 fb->pitches[0] = (val & 0x3ff) * stride_mult;
9438
9439 aligned_height = intel_fb_align_height(dev, fb->height,
091df6cb
DV
9440 fb->pixel_format,
9441 fb->modifier[0]);
bc8d7dff 9442
f37b5c2b 9443 plane_config->size = fb->pitches[0] * aligned_height;
bc8d7dff
DL
9444
9445 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9446 pipe_name(pipe), fb->width, fb->height,
9447 fb->bits_per_pixel, base, fb->pitches[0],
9448 plane_config->size);
9449
2d14030b 9450 plane_config->fb = intel_fb;
bc8d7dff
DL
9451 return;
9452
9453error:
9454 kfree(fb);
9455}
9456
2fa2fe9a 9457static void ironlake_get_pfit_config(struct intel_crtc *crtc,
5cec258b 9458 struct intel_crtc_state *pipe_config)
2fa2fe9a
DV
9459{
9460 struct drm_device *dev = crtc->base.dev;
fac5e23e 9461 struct drm_i915_private *dev_priv = to_i915(dev);
2fa2fe9a
DV
9462 uint32_t tmp;
9463
9464 tmp = I915_READ(PF_CTL(crtc->pipe));
9465
9466 if (tmp & PF_ENABLE) {
fd4daa9c 9467 pipe_config->pch_pfit.enabled = true;
2fa2fe9a
DV
9468 pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
9469 pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
cb8b2a30
DV
9470
9471 /* We currently do not free assignements of panel fitters on
9472 * ivb/hsw (since we don't use the higher upscaling modes which
9473 * differentiates them) so just WARN about this case for now. */
9474 if (IS_GEN7(dev)) {
9475 WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
9476 PF_PIPE_SEL_IVB(crtc->pipe));
9477 }
2fa2fe9a 9478 }
79e53945
JB
9479}
9480
5724dbd1
DL
9481static void
9482ironlake_get_initial_plane_config(struct intel_crtc *crtc,
9483 struct intel_initial_plane_config *plane_config)
4c6baa59
JB
9484{
9485 struct drm_device *dev = crtc->base.dev;
fac5e23e 9486 struct drm_i915_private *dev_priv = to_i915(dev);
4c6baa59 9487 u32 val, base, offset;
aeee5a49 9488 int pipe = crtc->pipe;
4c6baa59 9489 int fourcc, pixel_format;
6761dd31 9490 unsigned int aligned_height;
b113d5ee 9491 struct drm_framebuffer *fb;
1b842c89 9492 struct intel_framebuffer *intel_fb;
4c6baa59 9493
42a7b088
DL
9494 val = I915_READ(DSPCNTR(pipe));
9495 if (!(val & DISPLAY_PLANE_ENABLE))
9496 return;
9497
d9806c9f 9498 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
1b842c89 9499 if (!intel_fb) {
4c6baa59
JB
9500 DRM_DEBUG_KMS("failed to alloc fb\n");
9501 return;
9502 }
9503
1b842c89
DL
9504 fb = &intel_fb->base;
9505
18c5247e
DV
9506 if (INTEL_INFO(dev)->gen >= 4) {
9507 if (val & DISPPLANE_TILED) {
49af449b 9508 plane_config->tiling = I915_TILING_X;
18c5247e
DV
9509 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
9510 }
9511 }
4c6baa59
JB
9512
9513 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
b35d63fa 9514 fourcc = i9xx_format_to_fourcc(pixel_format);
b113d5ee
DL
9515 fb->pixel_format = fourcc;
9516 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
4c6baa59 9517
aeee5a49 9518 base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
4c6baa59 9519 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
aeee5a49 9520 offset = I915_READ(DSPOFFSET(pipe));
4c6baa59 9521 } else {
49af449b 9522 if (plane_config->tiling)
aeee5a49 9523 offset = I915_READ(DSPTILEOFF(pipe));
4c6baa59 9524 else
aeee5a49 9525 offset = I915_READ(DSPLINOFF(pipe));
4c6baa59
JB
9526 }
9527 plane_config->base = base;
9528
9529 val = I915_READ(PIPESRC(pipe));
b113d5ee
DL
9530 fb->width = ((val >> 16) & 0xfff) + 1;
9531 fb->height = ((val >> 0) & 0xfff) + 1;
4c6baa59
JB
9532
9533 val = I915_READ(DSPSTRIDE(pipe));
b113d5ee 9534 fb->pitches[0] = val & 0xffffffc0;
4c6baa59 9535
b113d5ee 9536 aligned_height = intel_fb_align_height(dev, fb->height,
091df6cb
DV
9537 fb->pixel_format,
9538 fb->modifier[0]);
4c6baa59 9539
f37b5c2b 9540 plane_config->size = fb->pitches[0] * aligned_height;
4c6baa59 9541
2844a921
DL
9542 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9543 pipe_name(pipe), fb->width, fb->height,
9544 fb->bits_per_pixel, base, fb->pitches[0],
9545 plane_config->size);
b113d5ee 9546
2d14030b 9547 plane_config->fb = intel_fb;
4c6baa59
JB
9548}
9549
0e8ffe1b 9550static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
5cec258b 9551 struct intel_crtc_state *pipe_config)
0e8ffe1b
DV
9552{
9553 struct drm_device *dev = crtc->base.dev;
fac5e23e 9554 struct drm_i915_private *dev_priv = to_i915(dev);
1729050e 9555 enum intel_display_power_domain power_domain;
0e8ffe1b 9556 uint32_t tmp;
1729050e 9557 bool ret;
0e8ffe1b 9558
1729050e
ID
9559 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
9560 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
930e8c9e
PZ
9561 return false;
9562
e143a21c 9563 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
8106ddbd 9564 pipe_config->shared_dpll = NULL;
eccb140b 9565
1729050e 9566 ret = false;
0e8ffe1b
DV
9567 tmp = I915_READ(PIPECONF(crtc->pipe));
9568 if (!(tmp & PIPECONF_ENABLE))
1729050e 9569 goto out;
0e8ffe1b 9570
42571aef
VS
9571 switch (tmp & PIPECONF_BPC_MASK) {
9572 case PIPECONF_6BPC:
9573 pipe_config->pipe_bpp = 18;
9574 break;
9575 case PIPECONF_8BPC:
9576 pipe_config->pipe_bpp = 24;
9577 break;
9578 case PIPECONF_10BPC:
9579 pipe_config->pipe_bpp = 30;
9580 break;
9581 case PIPECONF_12BPC:
9582 pipe_config->pipe_bpp = 36;
9583 break;
9584 default:
9585 break;
9586 }
9587
b5a9fa09
DV
9588 if (tmp & PIPECONF_COLOR_RANGE_SELECT)
9589 pipe_config->limited_color_range = true;
9590
ab9412ba 9591 if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
66e985c0 9592 struct intel_shared_dpll *pll;
8106ddbd 9593 enum intel_dpll_id pll_id;
66e985c0 9594
88adfff1
DV
9595 pipe_config->has_pch_encoder = true;
9596
627eb5a3
DV
9597 tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
9598 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
9599 FDI_DP_PORT_WIDTH_SHIFT) + 1;
72419203
DV
9600
9601 ironlake_get_fdi_m_n_config(crtc, pipe_config);
6c49f241 9602
2d1fe073 9603 if (HAS_PCH_IBX(dev_priv)) {
d9a7bc67
ID
9604 /*
9605 * The pipe->pch transcoder and pch transcoder->pll
9606 * mapping is fixed.
9607 */
8106ddbd 9608 pll_id = (enum intel_dpll_id) crtc->pipe;
c0d43d62
DV
9609 } else {
9610 tmp = I915_READ(PCH_DPLL_SEL);
9611 if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
8106ddbd 9612 pll_id = DPLL_ID_PCH_PLL_B;
c0d43d62 9613 else
8106ddbd 9614 pll_id= DPLL_ID_PCH_PLL_A;
c0d43d62 9615 }
66e985c0 9616
8106ddbd
ACO
9617 pipe_config->shared_dpll =
9618 intel_get_shared_dpll_by_id(dev_priv, pll_id);
9619 pll = pipe_config->shared_dpll;
66e985c0 9620
2edd6443
ACO
9621 WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
9622 &pipe_config->dpll_hw_state));
c93f54cf
DV
9623
9624 tmp = pipe_config->dpll_hw_state.dpll;
9625 pipe_config->pixel_multiplier =
9626 ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
9627 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
18442d08
VS
9628
9629 ironlake_pch_clock_get(crtc, pipe_config);
6c49f241
DV
9630 } else {
9631 pipe_config->pixel_multiplier = 1;
627eb5a3
DV
9632 }
9633
1bd1bd80 9634 intel_get_pipe_timings(crtc, pipe_config);
bc58be60 9635 intel_get_pipe_src_size(crtc, pipe_config);
1bd1bd80 9636
2fa2fe9a
DV
9637 ironlake_get_pfit_config(crtc, pipe_config);
9638
1729050e
ID
9639 ret = true;
9640
9641out:
9642 intel_display_power_put(dev_priv, power_domain);
9643
9644 return ret;
0e8ffe1b
DV
9645}
9646
be256dc7
PZ
9647static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
9648{
91c8a326 9649 struct drm_device *dev = &dev_priv->drm;
be256dc7 9650 struct intel_crtc *crtc;
be256dc7 9651
d3fcc808 9652 for_each_intel_crtc(dev, crtc)
e2c719b7 9653 I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
be256dc7
PZ
9654 pipe_name(crtc->pipe));
9655
e2c719b7
RC
9656 I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
9657 I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
01403de3
VS
9658 I915_STATE_WARN(I915_READ(WRPLL_CTL(0)) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
9659 I915_STATE_WARN(I915_READ(WRPLL_CTL(1)) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
44cb734c 9660 I915_STATE_WARN(I915_READ(PP_STATUS(0)) & PP_ON, "Panel power on\n");
e2c719b7 9661 I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
be256dc7 9662 "CPU PWM1 enabled\n");
c5107b87 9663 if (IS_HASWELL(dev))
e2c719b7 9664 I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
c5107b87 9665 "CPU PWM2 enabled\n");
e2c719b7 9666 I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
be256dc7 9667 "PCH PWM1 enabled\n");
e2c719b7 9668 I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
be256dc7 9669 "Utility pin enabled\n");
e2c719b7 9670 I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
be256dc7 9671
9926ada1
PZ
9672 /*
9673 * In theory we can still leave IRQs enabled, as long as only the HPD
9674 * interrupts remain enabled. We used to check for that, but since it's
9675 * gen-specific and since we only disable LCPLL after we fully disable
9676 * the interrupts, the check below should be enough.
9677 */
e2c719b7 9678 I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
be256dc7
PZ
9679}
9680
9ccd5aeb
PZ
9681static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
9682{
91c8a326 9683 struct drm_device *dev = &dev_priv->drm;
9ccd5aeb
PZ
9684
9685 if (IS_HASWELL(dev))
9686 return I915_READ(D_COMP_HSW);
9687 else
9688 return I915_READ(D_COMP_BDW);
9689}
9690
3c4c9b81
PZ
9691static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
9692{
91c8a326 9693 struct drm_device *dev = &dev_priv->drm;
3c4c9b81
PZ
9694
9695 if (IS_HASWELL(dev)) {
9696 mutex_lock(&dev_priv->rps.hw_lock);
9697 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
9698 val))
f475dadf 9699 DRM_ERROR("Failed to write to D_COMP\n");
3c4c9b81
PZ
9700 mutex_unlock(&dev_priv->rps.hw_lock);
9701 } else {
9ccd5aeb
PZ
9702 I915_WRITE(D_COMP_BDW, val);
9703 POSTING_READ(D_COMP_BDW);
3c4c9b81 9704 }
be256dc7
PZ
9705}
9706
9707/*
9708 * This function implements pieces of two sequences from BSpec:
9709 * - Sequence for display software to disable LCPLL
9710 * - Sequence for display software to allow package C8+
9711 * The steps implemented here are just the steps that actually touch the LCPLL
9712 * register. Callers should take care of disabling all the display engine
9713 * functions, doing the mode unset, fixing interrupts, etc.
9714 */
6ff58d53
PZ
9715static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
9716 bool switch_to_fclk, bool allow_power_down)
be256dc7
PZ
9717{
9718 uint32_t val;
9719
9720 assert_can_disable_lcpll(dev_priv);
9721
9722 val = I915_READ(LCPLL_CTL);
9723
9724 if (switch_to_fclk) {
9725 val |= LCPLL_CD_SOURCE_FCLK;
9726 I915_WRITE(LCPLL_CTL, val);
9727
f53dd63f
ID
9728 if (wait_for_us(I915_READ(LCPLL_CTL) &
9729 LCPLL_CD_SOURCE_FCLK_DONE, 1))
be256dc7
PZ
9730 DRM_ERROR("Switching to FCLK failed\n");
9731
9732 val = I915_READ(LCPLL_CTL);
9733 }
9734
9735 val |= LCPLL_PLL_DISABLE;
9736 I915_WRITE(LCPLL_CTL, val);
9737 POSTING_READ(LCPLL_CTL);
9738
24d8441d 9739 if (intel_wait_for_register(dev_priv, LCPLL_CTL, LCPLL_PLL_LOCK, 0, 1))
be256dc7
PZ
9740 DRM_ERROR("LCPLL still locked\n");
9741
9ccd5aeb 9742 val = hsw_read_dcomp(dev_priv);
be256dc7 9743 val |= D_COMP_COMP_DISABLE;
3c4c9b81 9744 hsw_write_dcomp(dev_priv, val);
be256dc7
PZ
9745 ndelay(100);
9746
9ccd5aeb
PZ
9747 if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
9748 1))
be256dc7
PZ
9749 DRM_ERROR("D_COMP RCOMP still in progress\n");
9750
9751 if (allow_power_down) {
9752 val = I915_READ(LCPLL_CTL);
9753 val |= LCPLL_POWER_DOWN_ALLOW;
9754 I915_WRITE(LCPLL_CTL, val);
9755 POSTING_READ(LCPLL_CTL);
9756 }
9757}
9758
9759/*
9760 * Fully restores LCPLL, disallowing power down and switching back to LCPLL
9761 * source.
9762 */
6ff58d53 9763static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
be256dc7
PZ
9764{
9765 uint32_t val;
9766
9767 val = I915_READ(LCPLL_CTL);
9768
9769 if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
9770 LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
9771 return;
9772
a8a8bd54
PZ
9773 /*
9774 * Make sure we're not on PC8 state before disabling PC8, otherwise
9775 * we'll hang the machine. To prevent PC8 state, just enable force_wake.
a8a8bd54 9776 */
59bad947 9777 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
215733fa 9778
be256dc7
PZ
9779 if (val & LCPLL_POWER_DOWN_ALLOW) {
9780 val &= ~LCPLL_POWER_DOWN_ALLOW;
9781 I915_WRITE(LCPLL_CTL, val);
35d8f2eb 9782 POSTING_READ(LCPLL_CTL);
be256dc7
PZ
9783 }
9784
9ccd5aeb 9785 val = hsw_read_dcomp(dev_priv);
be256dc7
PZ
9786 val |= D_COMP_COMP_FORCE;
9787 val &= ~D_COMP_COMP_DISABLE;
3c4c9b81 9788 hsw_write_dcomp(dev_priv, val);
be256dc7
PZ
9789
9790 val = I915_READ(LCPLL_CTL);
9791 val &= ~LCPLL_PLL_DISABLE;
9792 I915_WRITE(LCPLL_CTL, val);
9793
93220c08
CW
9794 if (intel_wait_for_register(dev_priv,
9795 LCPLL_CTL, LCPLL_PLL_LOCK, LCPLL_PLL_LOCK,
9796 5))
be256dc7
PZ
9797 DRM_ERROR("LCPLL not locked yet\n");
9798
9799 if (val & LCPLL_CD_SOURCE_FCLK) {
9800 val = I915_READ(LCPLL_CTL);
9801 val &= ~LCPLL_CD_SOURCE_FCLK;
9802 I915_WRITE(LCPLL_CTL, val);
9803
f53dd63f
ID
9804 if (wait_for_us((I915_READ(LCPLL_CTL) &
9805 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
be256dc7
PZ
9806 DRM_ERROR("Switching back to LCPLL failed\n");
9807 }
215733fa 9808
59bad947 9809 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
91c8a326 9810 intel_update_cdclk(&dev_priv->drm);
be256dc7
PZ
9811}
9812
765dab67
PZ
9813/*
9814 * Package states C8 and deeper are really deep PC states that can only be
9815 * reached when all the devices on the system allow it, so even if the graphics
9816 * device allows PC8+, it doesn't mean the system will actually get to these
9817 * states. Our driver only allows PC8+ when going into runtime PM.
9818 *
9819 * The requirements for PC8+ are that all the outputs are disabled, the power
9820 * well is disabled and most interrupts are disabled, and these are also
9821 * requirements for runtime PM. When these conditions are met, we manually do
9822 * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
9823 * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
9824 * hang the machine.
9825 *
9826 * When we really reach PC8 or deeper states (not just when we allow it) we lose
9827 * the state of some registers, so when we come back from PC8+ we need to
9828 * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
9829 * need to take care of the registers kept by RC6. Notice that this happens even
9830 * if we don't put the device in PCI D3 state (which is what currently happens
9831 * because of the runtime PM support).
9832 *
9833 * For more, read "Display Sequences for Package C8" on the hardware
9834 * documentation.
9835 */
a14cb6fc 9836void hsw_enable_pc8(struct drm_i915_private *dev_priv)
c67a470b 9837{
91c8a326 9838 struct drm_device *dev = &dev_priv->drm;
c67a470b
PZ
9839 uint32_t val;
9840
c67a470b
PZ
9841 DRM_DEBUG_KMS("Enabling package C8+\n");
9842
c2699524 9843 if (HAS_PCH_LPT_LP(dev)) {
c67a470b
PZ
9844 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9845 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
9846 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9847 }
9848
9849 lpt_disable_clkout_dp(dev);
c67a470b
PZ
9850 hsw_disable_lcpll(dev_priv, true, true);
9851}
9852
a14cb6fc 9853void hsw_disable_pc8(struct drm_i915_private *dev_priv)
c67a470b 9854{
91c8a326 9855 struct drm_device *dev = &dev_priv->drm;
c67a470b
PZ
9856 uint32_t val;
9857
c67a470b
PZ
9858 DRM_DEBUG_KMS("Disabling package C8+\n");
9859
9860 hsw_restore_lcpll(dev_priv);
c67a470b
PZ
9861 lpt_init_pch_refclk(dev);
9862
c2699524 9863 if (HAS_PCH_LPT_LP(dev)) {
c67a470b
PZ
9864 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9865 val |= PCH_LP_PARTITION_LEVEL_DISABLE;
9866 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9867 }
c67a470b
PZ
9868}
9869
324513c0 9870static void bxt_modeset_commit_cdclk(struct drm_atomic_state *old_state)
f8437dd1 9871{
a821fc46 9872 struct drm_device *dev = old_state->dev;
1a617b77
ML
9873 struct intel_atomic_state *old_intel_state =
9874 to_intel_atomic_state(old_state);
9875 unsigned int req_cdclk = old_intel_state->dev_cdclk;
f8437dd1 9876
324513c0 9877 bxt_set_cdclk(to_i915(dev), req_cdclk);
f8437dd1
VK
9878}
9879
b432e5cf 9880/* compute the max rate for new configuration */
27c329ed 9881static int ilk_max_pixel_rate(struct drm_atomic_state *state)
b432e5cf 9882{
565602d7 9883 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
fac5e23e 9884 struct drm_i915_private *dev_priv = to_i915(state->dev);
565602d7
ML
9885 struct drm_crtc *crtc;
9886 struct drm_crtc_state *cstate;
27c329ed 9887 struct intel_crtc_state *crtc_state;
565602d7
ML
9888 unsigned max_pixel_rate = 0, i;
9889 enum pipe pipe;
b432e5cf 9890
565602d7
ML
9891 memcpy(intel_state->min_pixclk, dev_priv->min_pixclk,
9892 sizeof(intel_state->min_pixclk));
27c329ed 9893
565602d7
ML
9894 for_each_crtc_in_state(state, crtc, cstate, i) {
9895 int pixel_rate;
27c329ed 9896
565602d7
ML
9897 crtc_state = to_intel_crtc_state(cstate);
9898 if (!crtc_state->base.enable) {
9899 intel_state->min_pixclk[i] = 0;
b432e5cf 9900 continue;
565602d7 9901 }
b432e5cf 9902
27c329ed 9903 pixel_rate = ilk_pipe_pixel_rate(crtc_state);
b432e5cf
VS
9904
9905 /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
565602d7 9906 if (IS_BROADWELL(dev_priv) && crtc_state->ips_enabled)
b432e5cf
VS
9907 pixel_rate = DIV_ROUND_UP(pixel_rate * 100, 95);
9908
565602d7 9909 intel_state->min_pixclk[i] = pixel_rate;
b432e5cf
VS
9910 }
9911
565602d7
ML
9912 for_each_pipe(dev_priv, pipe)
9913 max_pixel_rate = max(intel_state->min_pixclk[pipe], max_pixel_rate);
9914
b432e5cf
VS
9915 return max_pixel_rate;
9916}
9917
9918static void broadwell_set_cdclk(struct drm_device *dev, int cdclk)
9919{
fac5e23e 9920 struct drm_i915_private *dev_priv = to_i915(dev);
b432e5cf
VS
9921 uint32_t val, data;
9922 int ret;
9923
9924 if (WARN((I915_READ(LCPLL_CTL) &
9925 (LCPLL_PLL_DISABLE | LCPLL_PLL_LOCK |
9926 LCPLL_CD_CLOCK_DISABLE | LCPLL_ROOT_CD_CLOCK_DISABLE |
9927 LCPLL_CD2X_CLOCK_DISABLE | LCPLL_POWER_DOWN_ALLOW |
9928 LCPLL_CD_SOURCE_FCLK)) != LCPLL_PLL_LOCK,
9929 "trying to change cdclk frequency with cdclk not enabled\n"))
9930 return;
9931
9932 mutex_lock(&dev_priv->rps.hw_lock);
9933 ret = sandybridge_pcode_write(dev_priv,
9934 BDW_PCODE_DISPLAY_FREQ_CHANGE_REQ, 0x0);
9935 mutex_unlock(&dev_priv->rps.hw_lock);
9936 if (ret) {
9937 DRM_ERROR("failed to inform pcode about cdclk change\n");
9938 return;
9939 }
9940
9941 val = I915_READ(LCPLL_CTL);
9942 val |= LCPLL_CD_SOURCE_FCLK;
9943 I915_WRITE(LCPLL_CTL, val);
9944
5ba00178
TU
9945 if (wait_for_us(I915_READ(LCPLL_CTL) &
9946 LCPLL_CD_SOURCE_FCLK_DONE, 1))
b432e5cf
VS
9947 DRM_ERROR("Switching to FCLK failed\n");
9948
9949 val = I915_READ(LCPLL_CTL);
9950 val &= ~LCPLL_CLK_FREQ_MASK;
9951
9952 switch (cdclk) {
9953 case 450000:
9954 val |= LCPLL_CLK_FREQ_450;
9955 data = 0;
9956 break;
9957 case 540000:
9958 val |= LCPLL_CLK_FREQ_54O_BDW;
9959 data = 1;
9960 break;
9961 case 337500:
9962 val |= LCPLL_CLK_FREQ_337_5_BDW;
9963 data = 2;
9964 break;
9965 case 675000:
9966 val |= LCPLL_CLK_FREQ_675_BDW;
9967 data = 3;
9968 break;
9969 default:
9970 WARN(1, "invalid cdclk frequency\n");
9971 return;
9972 }
9973
9974 I915_WRITE(LCPLL_CTL, val);
9975
9976 val = I915_READ(LCPLL_CTL);
9977 val &= ~LCPLL_CD_SOURCE_FCLK;
9978 I915_WRITE(LCPLL_CTL, val);
9979
5ba00178
TU
9980 if (wait_for_us((I915_READ(LCPLL_CTL) &
9981 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
b432e5cf
VS
9982 DRM_ERROR("Switching back to LCPLL failed\n");
9983
9984 mutex_lock(&dev_priv->rps.hw_lock);
9985 sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ, data);
9986 mutex_unlock(&dev_priv->rps.hw_lock);
9987
7f1052a8
VS
9988 I915_WRITE(CDCLK_FREQ, DIV_ROUND_CLOSEST(cdclk, 1000) - 1);
9989
b432e5cf
VS
9990 intel_update_cdclk(dev);
9991
9992 WARN(cdclk != dev_priv->cdclk_freq,
9993 "cdclk requested %d kHz but got %d kHz\n",
9994 cdclk, dev_priv->cdclk_freq);
9995}
9996
587c7914
VS
9997static int broadwell_calc_cdclk(int max_pixclk)
9998{
9999 if (max_pixclk > 540000)
10000 return 675000;
10001 else if (max_pixclk > 450000)
10002 return 540000;
10003 else if (max_pixclk > 337500)
10004 return 450000;
10005 else
10006 return 337500;
10007}
10008
27c329ed 10009static int broadwell_modeset_calc_cdclk(struct drm_atomic_state *state)
b432e5cf 10010{
27c329ed 10011 struct drm_i915_private *dev_priv = to_i915(state->dev);
1a617b77 10012 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
27c329ed 10013 int max_pixclk = ilk_max_pixel_rate(state);
b432e5cf
VS
10014 int cdclk;
10015
10016 /*
10017 * FIXME should also account for plane ratio
10018 * once 64bpp pixel formats are supported.
10019 */
587c7914 10020 cdclk = broadwell_calc_cdclk(max_pixclk);
b432e5cf 10021
b432e5cf 10022 if (cdclk > dev_priv->max_cdclk_freq) {
63ba534e
ML
10023 DRM_DEBUG_KMS("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
10024 cdclk, dev_priv->max_cdclk_freq);
10025 return -EINVAL;
b432e5cf
VS
10026 }
10027
1a617b77
ML
10028 intel_state->cdclk = intel_state->dev_cdclk = cdclk;
10029 if (!intel_state->active_crtcs)
587c7914 10030 intel_state->dev_cdclk = broadwell_calc_cdclk(0);
b432e5cf
VS
10031
10032 return 0;
10033}
10034
27c329ed 10035static void broadwell_modeset_commit_cdclk(struct drm_atomic_state *old_state)
b432e5cf 10036{
27c329ed 10037 struct drm_device *dev = old_state->dev;
1a617b77
ML
10038 struct intel_atomic_state *old_intel_state =
10039 to_intel_atomic_state(old_state);
10040 unsigned req_cdclk = old_intel_state->dev_cdclk;
b432e5cf 10041
27c329ed 10042 broadwell_set_cdclk(dev, req_cdclk);
b432e5cf
VS
10043}
10044
c89e39f3
CT
10045static int skl_modeset_calc_cdclk(struct drm_atomic_state *state)
10046{
10047 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
10048 struct drm_i915_private *dev_priv = to_i915(state->dev);
10049 const int max_pixclk = ilk_max_pixel_rate(state);
a8ca4934 10050 int vco = intel_state->cdclk_pll_vco;
c89e39f3
CT
10051 int cdclk;
10052
10053 /*
10054 * FIXME should also account for plane ratio
10055 * once 64bpp pixel formats are supported.
10056 */
a8ca4934 10057 cdclk = skl_calc_cdclk(max_pixclk, vco);
c89e39f3
CT
10058
10059 /*
10060 * FIXME move the cdclk caclulation to
10061 * compute_config() so we can fail gracegully.
10062 */
10063 if (cdclk > dev_priv->max_cdclk_freq) {
10064 DRM_ERROR("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
10065 cdclk, dev_priv->max_cdclk_freq);
10066 cdclk = dev_priv->max_cdclk_freq;
10067 }
10068
10069 intel_state->cdclk = intel_state->dev_cdclk = cdclk;
10070 if (!intel_state->active_crtcs)
a8ca4934 10071 intel_state->dev_cdclk = skl_calc_cdclk(0, vco);
c89e39f3
CT
10072
10073 return 0;
10074}
10075
10076static void skl_modeset_commit_cdclk(struct drm_atomic_state *old_state)
10077{
1cd593e0
VS
10078 struct drm_i915_private *dev_priv = to_i915(old_state->dev);
10079 struct intel_atomic_state *intel_state = to_intel_atomic_state(old_state);
10080 unsigned int req_cdclk = intel_state->dev_cdclk;
10081 unsigned int req_vco = intel_state->cdclk_pll_vco;
c89e39f3 10082
1cd593e0 10083 skl_set_cdclk(dev_priv, req_cdclk, req_vco);
c89e39f3
CT
10084}
10085
190f68c5
ACO
10086static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
10087 struct intel_crtc_state *crtc_state)
09b4ddf9 10088{
d7edc4e5 10089 if (!intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DSI)) {
af3997b5
MK
10090 if (!intel_ddi_pll_select(crtc, crtc_state))
10091 return -EINVAL;
10092 }
716c2e55 10093
c7653199 10094 crtc->lowfreq_avail = false;
644cef34 10095
c8f7a0db 10096 return 0;
79e53945
JB
10097}
10098
3760b59c
S
10099static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
10100 enum port port,
10101 struct intel_crtc_state *pipe_config)
10102{
8106ddbd
ACO
10103 enum intel_dpll_id id;
10104
3760b59c
S
10105 switch (port) {
10106 case PORT_A:
10107 pipe_config->ddi_pll_sel = SKL_DPLL0;
08250c4b 10108 id = DPLL_ID_SKL_DPLL0;
3760b59c
S
10109 break;
10110 case PORT_B:
10111 pipe_config->ddi_pll_sel = SKL_DPLL1;
08250c4b 10112 id = DPLL_ID_SKL_DPLL1;
3760b59c
S
10113 break;
10114 case PORT_C:
10115 pipe_config->ddi_pll_sel = SKL_DPLL2;
08250c4b 10116 id = DPLL_ID_SKL_DPLL2;
3760b59c
S
10117 break;
10118 default:
10119 DRM_ERROR("Incorrect port type\n");
8106ddbd 10120 return;
3760b59c 10121 }
8106ddbd
ACO
10122
10123 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
3760b59c
S
10124}
10125
96b7dfb7
S
10126static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
10127 enum port port,
5cec258b 10128 struct intel_crtc_state *pipe_config)
96b7dfb7 10129{
8106ddbd 10130 enum intel_dpll_id id;
a3c988ea 10131 u32 temp;
96b7dfb7
S
10132
10133 temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
10134 pipe_config->ddi_pll_sel = temp >> (port * 3 + 1);
10135
10136 switch (pipe_config->ddi_pll_sel) {
3148ade7 10137 case SKL_DPLL0:
a3c988ea
ACO
10138 id = DPLL_ID_SKL_DPLL0;
10139 break;
96b7dfb7 10140 case SKL_DPLL1:
8106ddbd 10141 id = DPLL_ID_SKL_DPLL1;
96b7dfb7
S
10142 break;
10143 case SKL_DPLL2:
8106ddbd 10144 id = DPLL_ID_SKL_DPLL2;
96b7dfb7
S
10145 break;
10146 case SKL_DPLL3:
8106ddbd 10147 id = DPLL_ID_SKL_DPLL3;
96b7dfb7 10148 break;
8106ddbd
ACO
10149 default:
10150 MISSING_CASE(pipe_config->ddi_pll_sel);
10151 return;
96b7dfb7 10152 }
8106ddbd
ACO
10153
10154 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
96b7dfb7
S
10155}
10156
7d2c8175
DL
10157static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
10158 enum port port,
5cec258b 10159 struct intel_crtc_state *pipe_config)
7d2c8175 10160{
8106ddbd
ACO
10161 enum intel_dpll_id id;
10162
7d2c8175
DL
10163 pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
10164
10165 switch (pipe_config->ddi_pll_sel) {
10166 case PORT_CLK_SEL_WRPLL1:
8106ddbd 10167 id = DPLL_ID_WRPLL1;
7d2c8175
DL
10168 break;
10169 case PORT_CLK_SEL_WRPLL2:
8106ddbd 10170 id = DPLL_ID_WRPLL2;
7d2c8175 10171 break;
00490c22 10172 case PORT_CLK_SEL_SPLL:
8106ddbd 10173 id = DPLL_ID_SPLL;
79bd23da 10174 break;
9d16da65
ACO
10175 case PORT_CLK_SEL_LCPLL_810:
10176 id = DPLL_ID_LCPLL_810;
10177 break;
10178 case PORT_CLK_SEL_LCPLL_1350:
10179 id = DPLL_ID_LCPLL_1350;
10180 break;
10181 case PORT_CLK_SEL_LCPLL_2700:
10182 id = DPLL_ID_LCPLL_2700;
10183 break;
8106ddbd
ACO
10184 default:
10185 MISSING_CASE(pipe_config->ddi_pll_sel);
10186 /* fall through */
10187 case PORT_CLK_SEL_NONE:
8106ddbd 10188 return;
7d2c8175 10189 }
8106ddbd
ACO
10190
10191 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
7d2c8175
DL
10192}
10193
cf30429e
JN
10194static bool hsw_get_transcoder_state(struct intel_crtc *crtc,
10195 struct intel_crtc_state *pipe_config,
10196 unsigned long *power_domain_mask)
10197{
10198 struct drm_device *dev = crtc->base.dev;
fac5e23e 10199 struct drm_i915_private *dev_priv = to_i915(dev);
cf30429e
JN
10200 enum intel_display_power_domain power_domain;
10201 u32 tmp;
10202
d9a7bc67
ID
10203 /*
10204 * The pipe->transcoder mapping is fixed with the exception of the eDP
10205 * transcoder handled below.
10206 */
cf30429e
JN
10207 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
10208
10209 /*
10210 * XXX: Do intel_display_power_get_if_enabled before reading this (for
10211 * consistency and less surprising code; it's in always on power).
10212 */
10213 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
10214 if (tmp & TRANS_DDI_FUNC_ENABLE) {
10215 enum pipe trans_edp_pipe;
10216 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
10217 default:
10218 WARN(1, "unknown pipe linked to edp transcoder\n");
10219 case TRANS_DDI_EDP_INPUT_A_ONOFF:
10220 case TRANS_DDI_EDP_INPUT_A_ON:
10221 trans_edp_pipe = PIPE_A;
10222 break;
10223 case TRANS_DDI_EDP_INPUT_B_ONOFF:
10224 trans_edp_pipe = PIPE_B;
10225 break;
10226 case TRANS_DDI_EDP_INPUT_C_ONOFF:
10227 trans_edp_pipe = PIPE_C;
10228 break;
10229 }
10230
10231 if (trans_edp_pipe == crtc->pipe)
10232 pipe_config->cpu_transcoder = TRANSCODER_EDP;
10233 }
10234
10235 power_domain = POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder);
10236 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
10237 return false;
10238 *power_domain_mask |= BIT(power_domain);
10239
10240 tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
10241
10242 return tmp & PIPECONF_ENABLE;
10243}
10244
4d1de975
JN
10245static bool bxt_get_dsi_transcoder_state(struct intel_crtc *crtc,
10246 struct intel_crtc_state *pipe_config,
10247 unsigned long *power_domain_mask)
10248{
10249 struct drm_device *dev = crtc->base.dev;
fac5e23e 10250 struct drm_i915_private *dev_priv = to_i915(dev);
4d1de975
JN
10251 enum intel_display_power_domain power_domain;
10252 enum port port;
10253 enum transcoder cpu_transcoder;
10254 u32 tmp;
10255
4d1de975
JN
10256 for_each_port_masked(port, BIT(PORT_A) | BIT(PORT_C)) {
10257 if (port == PORT_A)
10258 cpu_transcoder = TRANSCODER_DSI_A;
10259 else
10260 cpu_transcoder = TRANSCODER_DSI_C;
10261
10262 power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
10263 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
10264 continue;
10265 *power_domain_mask |= BIT(power_domain);
10266
db18b6a6
ID
10267 /*
10268 * The PLL needs to be enabled with a valid divider
10269 * configuration, otherwise accessing DSI registers will hang
10270 * the machine. See BSpec North Display Engine
10271 * registers/MIPI[BXT]. We can break out here early, since we
10272 * need the same DSI PLL to be enabled for both DSI ports.
10273 */
10274 if (!intel_dsi_pll_is_enabled(dev_priv))
10275 break;
10276
4d1de975
JN
10277 /* XXX: this works for video mode only */
10278 tmp = I915_READ(BXT_MIPI_PORT_CTRL(port));
10279 if (!(tmp & DPI_ENABLE))
10280 continue;
10281
10282 tmp = I915_READ(MIPI_CTRL(port));
10283 if ((tmp & BXT_PIPE_SELECT_MASK) != BXT_PIPE_SELECT(crtc->pipe))
10284 continue;
10285
10286 pipe_config->cpu_transcoder = cpu_transcoder;
4d1de975
JN
10287 break;
10288 }
10289
d7edc4e5 10290 return transcoder_is_dsi(pipe_config->cpu_transcoder);
4d1de975
JN
10291}
10292
26804afd 10293static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
5cec258b 10294 struct intel_crtc_state *pipe_config)
26804afd
DV
10295{
10296 struct drm_device *dev = crtc->base.dev;
fac5e23e 10297 struct drm_i915_private *dev_priv = to_i915(dev);
d452c5b6 10298 struct intel_shared_dpll *pll;
26804afd
DV
10299 enum port port;
10300 uint32_t tmp;
10301
10302 tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
10303
10304 port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
10305
ef11bdb3 10306 if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev))
96b7dfb7 10307 skylake_get_ddi_pll(dev_priv, port, pipe_config);
3760b59c
S
10308 else if (IS_BROXTON(dev))
10309 bxt_get_ddi_pll(dev_priv, port, pipe_config);
96b7dfb7
S
10310 else
10311 haswell_get_ddi_pll(dev_priv, port, pipe_config);
9cd86933 10312
8106ddbd
ACO
10313 pll = pipe_config->shared_dpll;
10314 if (pll) {
2edd6443
ACO
10315 WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
10316 &pipe_config->dpll_hw_state));
d452c5b6
DV
10317 }
10318
26804afd
DV
10319 /*
10320 * Haswell has only FDI/PCH transcoder A. It is which is connected to
10321 * DDI E. So just check whether this pipe is wired to DDI E and whether
10322 * the PCH transcoder is on.
10323 */
ca370455
DL
10324 if (INTEL_INFO(dev)->gen < 9 &&
10325 (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
26804afd
DV
10326 pipe_config->has_pch_encoder = true;
10327
10328 tmp = I915_READ(FDI_RX_CTL(PIPE_A));
10329 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
10330 FDI_DP_PORT_WIDTH_SHIFT) + 1;
10331
10332 ironlake_get_fdi_m_n_config(crtc, pipe_config);
10333 }
10334}
10335
0e8ffe1b 10336static bool haswell_get_pipe_config(struct intel_crtc *crtc,
5cec258b 10337 struct intel_crtc_state *pipe_config)
0e8ffe1b
DV
10338{
10339 struct drm_device *dev = crtc->base.dev;
fac5e23e 10340 struct drm_i915_private *dev_priv = to_i915(dev);
1729050e
ID
10341 enum intel_display_power_domain power_domain;
10342 unsigned long power_domain_mask;
cf30429e 10343 bool active;
0e8ffe1b 10344
1729050e
ID
10345 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
10346 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
b5482bd0 10347 return false;
1729050e
ID
10348 power_domain_mask = BIT(power_domain);
10349
8106ddbd 10350 pipe_config->shared_dpll = NULL;
c0d43d62 10351
cf30429e 10352 active = hsw_get_transcoder_state(crtc, pipe_config, &power_domain_mask);
eccb140b 10353
d7edc4e5
VS
10354 if (IS_BROXTON(dev_priv) &&
10355 bxt_get_dsi_transcoder_state(crtc, pipe_config, &power_domain_mask)) {
10356 WARN_ON(active);
10357 active = true;
4d1de975
JN
10358 }
10359
cf30429e 10360 if (!active)
1729050e 10361 goto out;
0e8ffe1b 10362
d7edc4e5 10363 if (!transcoder_is_dsi(pipe_config->cpu_transcoder)) {
4d1de975
JN
10364 haswell_get_ddi_port_state(crtc, pipe_config);
10365 intel_get_pipe_timings(crtc, pipe_config);
10366 }
627eb5a3 10367
bc58be60 10368 intel_get_pipe_src_size(crtc, pipe_config);
1bd1bd80 10369
05dc698c
LL
10370 pipe_config->gamma_mode =
10371 I915_READ(GAMMA_MODE(crtc->pipe)) & GAMMA_MODE_MODE_MASK;
10372
a1b2278e
CK
10373 if (INTEL_INFO(dev)->gen >= 9) {
10374 skl_init_scalers(dev, crtc, pipe_config);
10375 }
10376
af99ceda
CK
10377 if (INTEL_INFO(dev)->gen >= 9) {
10378 pipe_config->scaler_state.scaler_id = -1;
10379 pipe_config->scaler_state.scaler_users &= ~(1 << SKL_CRTC_INDEX);
10380 }
10381
1729050e
ID
10382 power_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
10383 if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
10384 power_domain_mask |= BIT(power_domain);
1c132b44 10385 if (INTEL_INFO(dev)->gen >= 9)
bd2e244f 10386 skylake_get_pfit_config(crtc, pipe_config);
ff6d9f55 10387 else
1c132b44 10388 ironlake_get_pfit_config(crtc, pipe_config);
bd2e244f 10389 }
88adfff1 10390
e59150dc
JB
10391 if (IS_HASWELL(dev))
10392 pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
10393 (I915_READ(IPS_CTL) & IPS_ENABLE);
42db64ef 10394
4d1de975
JN
10395 if (pipe_config->cpu_transcoder != TRANSCODER_EDP &&
10396 !transcoder_is_dsi(pipe_config->cpu_transcoder)) {
ebb69c95
CT
10397 pipe_config->pixel_multiplier =
10398 I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
10399 } else {
10400 pipe_config->pixel_multiplier = 1;
10401 }
6c49f241 10402
1729050e
ID
10403out:
10404 for_each_power_domain(power_domain, power_domain_mask)
10405 intel_display_power_put(dev_priv, power_domain);
10406
cf30429e 10407 return active;
0e8ffe1b
DV
10408}
10409
55a08b3f
ML
10410static void i845_update_cursor(struct drm_crtc *crtc, u32 base,
10411 const struct intel_plane_state *plane_state)
560b85bb
CW
10412{
10413 struct drm_device *dev = crtc->dev;
fac5e23e 10414 struct drm_i915_private *dev_priv = to_i915(dev);
560b85bb 10415 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
dc41c154 10416 uint32_t cntl = 0, size = 0;
560b85bb 10417
55a08b3f
ML
10418 if (plane_state && plane_state->visible) {
10419 unsigned int width = plane_state->base.crtc_w;
10420 unsigned int height = plane_state->base.crtc_h;
dc41c154
VS
10421 unsigned int stride = roundup_pow_of_two(width) * 4;
10422
10423 switch (stride) {
10424 default:
10425 WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
10426 width, stride);
10427 stride = 256;
10428 /* fallthrough */
10429 case 256:
10430 case 512:
10431 case 1024:
10432 case 2048:
10433 break;
4b0e333e
CW
10434 }
10435
dc41c154
VS
10436 cntl |= CURSOR_ENABLE |
10437 CURSOR_GAMMA_ENABLE |
10438 CURSOR_FORMAT_ARGB |
10439 CURSOR_STRIDE(stride);
10440
10441 size = (height << 12) | width;
4b0e333e 10442 }
560b85bb 10443
dc41c154
VS
10444 if (intel_crtc->cursor_cntl != 0 &&
10445 (intel_crtc->cursor_base != base ||
10446 intel_crtc->cursor_size != size ||
10447 intel_crtc->cursor_cntl != cntl)) {
10448 /* On these chipsets we can only modify the base/size/stride
10449 * whilst the cursor is disabled.
10450 */
0b87c24e
VS
10451 I915_WRITE(CURCNTR(PIPE_A), 0);
10452 POSTING_READ(CURCNTR(PIPE_A));
dc41c154 10453 intel_crtc->cursor_cntl = 0;
4b0e333e 10454 }
560b85bb 10455
99d1f387 10456 if (intel_crtc->cursor_base != base) {
0b87c24e 10457 I915_WRITE(CURBASE(PIPE_A), base);
99d1f387
VS
10458 intel_crtc->cursor_base = base;
10459 }
4726e0b0 10460
dc41c154
VS
10461 if (intel_crtc->cursor_size != size) {
10462 I915_WRITE(CURSIZE, size);
10463 intel_crtc->cursor_size = size;
4b0e333e 10464 }
560b85bb 10465
4b0e333e 10466 if (intel_crtc->cursor_cntl != cntl) {
0b87c24e
VS
10467 I915_WRITE(CURCNTR(PIPE_A), cntl);
10468 POSTING_READ(CURCNTR(PIPE_A));
4b0e333e 10469 intel_crtc->cursor_cntl = cntl;
560b85bb 10470 }
560b85bb
CW
10471}
10472
55a08b3f
ML
10473static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base,
10474 const struct intel_plane_state *plane_state)
65a21cd6
JB
10475{
10476 struct drm_device *dev = crtc->dev;
fac5e23e 10477 struct drm_i915_private *dev_priv = to_i915(dev);
65a21cd6
JB
10478 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10479 int pipe = intel_crtc->pipe;
663f3122 10480 uint32_t cntl = 0;
4b0e333e 10481
55a08b3f 10482 if (plane_state && plane_state->visible) {
4b0e333e 10483 cntl = MCURSOR_GAMMA_ENABLE;
55a08b3f 10484 switch (plane_state->base.crtc_w) {
4726e0b0
SK
10485 case 64:
10486 cntl |= CURSOR_MODE_64_ARGB_AX;
10487 break;
10488 case 128:
10489 cntl |= CURSOR_MODE_128_ARGB_AX;
10490 break;
10491 case 256:
10492 cntl |= CURSOR_MODE_256_ARGB_AX;
10493 break;
10494 default:
55a08b3f 10495 MISSING_CASE(plane_state->base.crtc_w);
4726e0b0 10496 return;
65a21cd6 10497 }
4b0e333e 10498 cntl |= pipe << 28; /* Connect to correct pipe */
47bf17a7 10499
fc6f93bc 10500 if (HAS_DDI(dev))
47bf17a7 10501 cntl |= CURSOR_PIPE_CSC_ENABLE;
65a21cd6 10502
55a08b3f
ML
10503 if (plane_state->base.rotation == BIT(DRM_ROTATE_180))
10504 cntl |= CURSOR_ROTATE_180;
10505 }
4398ad45 10506
4b0e333e
CW
10507 if (intel_crtc->cursor_cntl != cntl) {
10508 I915_WRITE(CURCNTR(pipe), cntl);
10509 POSTING_READ(CURCNTR(pipe));
10510 intel_crtc->cursor_cntl = cntl;
65a21cd6 10511 }
4b0e333e 10512
65a21cd6 10513 /* and commit changes on next vblank */
5efb3e28
VS
10514 I915_WRITE(CURBASE(pipe), base);
10515 POSTING_READ(CURBASE(pipe));
99d1f387
VS
10516
10517 intel_crtc->cursor_base = base;
65a21cd6
JB
10518}
10519
cda4b7d3 10520/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
6b383a7f 10521static void intel_crtc_update_cursor(struct drm_crtc *crtc,
55a08b3f 10522 const struct intel_plane_state *plane_state)
cda4b7d3
CW
10523{
10524 struct drm_device *dev = crtc->dev;
fac5e23e 10525 struct drm_i915_private *dev_priv = to_i915(dev);
cda4b7d3
CW
10526 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10527 int pipe = intel_crtc->pipe;
55a08b3f
ML
10528 u32 base = intel_crtc->cursor_addr;
10529 u32 pos = 0;
cda4b7d3 10530
55a08b3f
ML
10531 if (plane_state) {
10532 int x = plane_state->base.crtc_x;
10533 int y = plane_state->base.crtc_y;
cda4b7d3 10534
55a08b3f
ML
10535 if (x < 0) {
10536 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
10537 x = -x;
10538 }
10539 pos |= x << CURSOR_X_SHIFT;
cda4b7d3 10540
55a08b3f
ML
10541 if (y < 0) {
10542 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
10543 y = -y;
10544 }
10545 pos |= y << CURSOR_Y_SHIFT;
10546
10547 /* ILK+ do this automagically */
10548 if (HAS_GMCH_DISPLAY(dev) &&
10549 plane_state->base.rotation == BIT(DRM_ROTATE_180)) {
10550 base += (plane_state->base.crtc_h *
10551 plane_state->base.crtc_w - 1) * 4;
10552 }
cda4b7d3 10553 }
cda4b7d3 10554
5efb3e28
VS
10555 I915_WRITE(CURPOS(pipe), pos);
10556
8ac54669 10557 if (IS_845G(dev) || IS_I865G(dev))
55a08b3f 10558 i845_update_cursor(crtc, base, plane_state);
5efb3e28 10559 else
55a08b3f 10560 i9xx_update_cursor(crtc, base, plane_state);
cda4b7d3
CW
10561}
10562
dc41c154
VS
10563static bool cursor_size_ok(struct drm_device *dev,
10564 uint32_t width, uint32_t height)
10565{
10566 if (width == 0 || height == 0)
10567 return false;
10568
10569 /*
10570 * 845g/865g are special in that they are only limited by
10571 * the width of their cursors, the height is arbitrary up to
10572 * the precision of the register. Everything else requires
10573 * square cursors, limited to a few power-of-two sizes.
10574 */
10575 if (IS_845G(dev) || IS_I865G(dev)) {
10576 if ((width & 63) != 0)
10577 return false;
10578
10579 if (width > (IS_845G(dev) ? 64 : 512))
10580 return false;
10581
10582 if (height > 1023)
10583 return false;
10584 } else {
10585 switch (width | height) {
10586 case 256:
10587 case 128:
10588 if (IS_GEN2(dev))
10589 return false;
10590 case 64:
10591 break;
10592 default:
10593 return false;
10594 }
10595 }
10596
10597 return true;
10598}
10599
79e53945
JB
10600/* VESA 640x480x72Hz mode to set on the pipe */
10601static struct drm_display_mode load_detect_mode = {
10602 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
10603 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
10604};
10605
a8bb6818
DV
10606struct drm_framebuffer *
10607__intel_framebuffer_create(struct drm_device *dev,
10608 struct drm_mode_fb_cmd2 *mode_cmd,
10609 struct drm_i915_gem_object *obj)
d2dff872
CW
10610{
10611 struct intel_framebuffer *intel_fb;
10612 int ret;
10613
10614 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
dcb1394e 10615 if (!intel_fb)
d2dff872 10616 return ERR_PTR(-ENOMEM);
d2dff872
CW
10617
10618 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
dd4916c5
DV
10619 if (ret)
10620 goto err;
d2dff872
CW
10621
10622 return &intel_fb->base;
dcb1394e 10623
dd4916c5 10624err:
dd4916c5 10625 kfree(intel_fb);
dd4916c5 10626 return ERR_PTR(ret);
d2dff872
CW
10627}
10628
b5ea642a 10629static struct drm_framebuffer *
a8bb6818
DV
10630intel_framebuffer_create(struct drm_device *dev,
10631 struct drm_mode_fb_cmd2 *mode_cmd,
10632 struct drm_i915_gem_object *obj)
10633{
10634 struct drm_framebuffer *fb;
10635 int ret;
10636
10637 ret = i915_mutex_lock_interruptible(dev);
10638 if (ret)
10639 return ERR_PTR(ret);
10640 fb = __intel_framebuffer_create(dev, mode_cmd, obj);
10641 mutex_unlock(&dev->struct_mutex);
10642
10643 return fb;
10644}
10645
d2dff872
CW
10646static u32
10647intel_framebuffer_pitch_for_width(int width, int bpp)
10648{
10649 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
10650 return ALIGN(pitch, 64);
10651}
10652
10653static u32
10654intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
10655{
10656 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
1267a26b 10657 return PAGE_ALIGN(pitch * mode->vdisplay);
d2dff872
CW
10658}
10659
10660static struct drm_framebuffer *
10661intel_framebuffer_create_for_mode(struct drm_device *dev,
10662 struct drm_display_mode *mode,
10663 int depth, int bpp)
10664{
dcb1394e 10665 struct drm_framebuffer *fb;
d2dff872 10666 struct drm_i915_gem_object *obj;
0fed39bd 10667 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
d2dff872 10668
d37cd8a8 10669 obj = i915_gem_object_create(dev,
d2dff872 10670 intel_framebuffer_size_for_mode(mode, bpp));
fe3db79b
CW
10671 if (IS_ERR(obj))
10672 return ERR_CAST(obj);
d2dff872
CW
10673
10674 mode_cmd.width = mode->hdisplay;
10675 mode_cmd.height = mode->vdisplay;
308e5bcb
JB
10676 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
10677 bpp);
5ca0c34a 10678 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
d2dff872 10679
dcb1394e
LW
10680 fb = intel_framebuffer_create(dev, &mode_cmd, obj);
10681 if (IS_ERR(fb))
34911fd3 10682 i915_gem_object_put_unlocked(obj);
dcb1394e
LW
10683
10684 return fb;
d2dff872
CW
10685}
10686
10687static struct drm_framebuffer *
10688mode_fits_in_fbdev(struct drm_device *dev,
10689 struct drm_display_mode *mode)
10690{
0695726e 10691#ifdef CONFIG_DRM_FBDEV_EMULATION
fac5e23e 10692 struct drm_i915_private *dev_priv = to_i915(dev);
d2dff872
CW
10693 struct drm_i915_gem_object *obj;
10694 struct drm_framebuffer *fb;
10695
4c0e5528 10696 if (!dev_priv->fbdev)
d2dff872
CW
10697 return NULL;
10698
4c0e5528 10699 if (!dev_priv->fbdev->fb)
d2dff872
CW
10700 return NULL;
10701
4c0e5528
DV
10702 obj = dev_priv->fbdev->fb->obj;
10703 BUG_ON(!obj);
10704
8bcd4553 10705 fb = &dev_priv->fbdev->fb->base;
01f2c773
VS
10706 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
10707 fb->bits_per_pixel))
d2dff872
CW
10708 return NULL;
10709
01f2c773 10710 if (obj->base.size < mode->vdisplay * fb->pitches[0])
d2dff872
CW
10711 return NULL;
10712
edde3617 10713 drm_framebuffer_reference(fb);
d2dff872 10714 return fb;
4520f53a
DV
10715#else
10716 return NULL;
10717#endif
d2dff872
CW
10718}
10719
d3a40d1b
ACO
10720static int intel_modeset_setup_plane_state(struct drm_atomic_state *state,
10721 struct drm_crtc *crtc,
10722 struct drm_display_mode *mode,
10723 struct drm_framebuffer *fb,
10724 int x, int y)
10725{
10726 struct drm_plane_state *plane_state;
10727 int hdisplay, vdisplay;
10728 int ret;
10729
10730 plane_state = drm_atomic_get_plane_state(state, crtc->primary);
10731 if (IS_ERR(plane_state))
10732 return PTR_ERR(plane_state);
10733
10734 if (mode)
10735 drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
10736 else
10737 hdisplay = vdisplay = 0;
10738
10739 ret = drm_atomic_set_crtc_for_plane(plane_state, fb ? crtc : NULL);
10740 if (ret)
10741 return ret;
10742 drm_atomic_set_fb_for_plane(plane_state, fb);
10743 plane_state->crtc_x = 0;
10744 plane_state->crtc_y = 0;
10745 plane_state->crtc_w = hdisplay;
10746 plane_state->crtc_h = vdisplay;
10747 plane_state->src_x = x << 16;
10748 plane_state->src_y = y << 16;
10749 plane_state->src_w = hdisplay << 16;
10750 plane_state->src_h = vdisplay << 16;
10751
10752 return 0;
10753}
10754
d2434ab7 10755bool intel_get_load_detect_pipe(struct drm_connector *connector,
7173188d 10756 struct drm_display_mode *mode,
51fd371b
RC
10757 struct intel_load_detect_pipe *old,
10758 struct drm_modeset_acquire_ctx *ctx)
79e53945
JB
10759{
10760 struct intel_crtc *intel_crtc;
d2434ab7
DV
10761 struct intel_encoder *intel_encoder =
10762 intel_attached_encoder(connector);
79e53945 10763 struct drm_crtc *possible_crtc;
4ef69c7a 10764 struct drm_encoder *encoder = &intel_encoder->base;
79e53945
JB
10765 struct drm_crtc *crtc = NULL;
10766 struct drm_device *dev = encoder->dev;
94352cf9 10767 struct drm_framebuffer *fb;
51fd371b 10768 struct drm_mode_config *config = &dev->mode_config;
edde3617 10769 struct drm_atomic_state *state = NULL, *restore_state = NULL;
944b0c76 10770 struct drm_connector_state *connector_state;
4be07317 10771 struct intel_crtc_state *crtc_state;
51fd371b 10772 int ret, i = -1;
79e53945 10773
d2dff872 10774 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
c23cc417 10775 connector->base.id, connector->name,
8e329a03 10776 encoder->base.id, encoder->name);
d2dff872 10777
edde3617
ML
10778 old->restore_state = NULL;
10779
51fd371b
RC
10780retry:
10781 ret = drm_modeset_lock(&config->connection_mutex, ctx);
10782 if (ret)
ad3c558f 10783 goto fail;
6e9f798d 10784
79e53945
JB
10785 /*
10786 * Algorithm gets a little messy:
7a5e4805 10787 *
79e53945
JB
10788 * - if the connector already has an assigned crtc, use it (but make
10789 * sure it's on first)
7a5e4805 10790 *
79e53945
JB
10791 * - try to find the first unused crtc that can drive this connector,
10792 * and use that if we find one
79e53945
JB
10793 */
10794
10795 /* See if we already have a CRTC for this connector */
edde3617
ML
10796 if (connector->state->crtc) {
10797 crtc = connector->state->crtc;
8261b191 10798
51fd371b 10799 ret = drm_modeset_lock(&crtc->mutex, ctx);
4d02e2de 10800 if (ret)
ad3c558f 10801 goto fail;
8261b191
CW
10802
10803 /* Make sure the crtc and connector are running */
edde3617 10804 goto found;
79e53945
JB
10805 }
10806
10807 /* Find an unused one (if possible) */
70e1e0ec 10808 for_each_crtc(dev, possible_crtc) {
79e53945
JB
10809 i++;
10810 if (!(encoder->possible_crtcs & (1 << i)))
10811 continue;
edde3617
ML
10812
10813 ret = drm_modeset_lock(&possible_crtc->mutex, ctx);
10814 if (ret)
10815 goto fail;
10816
10817 if (possible_crtc->state->enable) {
10818 drm_modeset_unlock(&possible_crtc->mutex);
a459249c 10819 continue;
edde3617 10820 }
a459249c
VS
10821
10822 crtc = possible_crtc;
10823 break;
79e53945
JB
10824 }
10825
10826 /*
10827 * If we didn't find an unused CRTC, don't use any.
10828 */
10829 if (!crtc) {
7173188d 10830 DRM_DEBUG_KMS("no pipe available for load-detect\n");
ad3c558f 10831 goto fail;
79e53945
JB
10832 }
10833
edde3617
ML
10834found:
10835 intel_crtc = to_intel_crtc(crtc);
10836
4d02e2de
DV
10837 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
10838 if (ret)
ad3c558f 10839 goto fail;
79e53945 10840
83a57153 10841 state = drm_atomic_state_alloc(dev);
edde3617
ML
10842 restore_state = drm_atomic_state_alloc(dev);
10843 if (!state || !restore_state) {
10844 ret = -ENOMEM;
10845 goto fail;
10846 }
83a57153
ACO
10847
10848 state->acquire_ctx = ctx;
edde3617 10849 restore_state->acquire_ctx = ctx;
83a57153 10850
944b0c76
ACO
10851 connector_state = drm_atomic_get_connector_state(state, connector);
10852 if (IS_ERR(connector_state)) {
10853 ret = PTR_ERR(connector_state);
10854 goto fail;
10855 }
10856
edde3617
ML
10857 ret = drm_atomic_set_crtc_for_connector(connector_state, crtc);
10858 if (ret)
10859 goto fail;
944b0c76 10860
4be07317
ACO
10861 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
10862 if (IS_ERR(crtc_state)) {
10863 ret = PTR_ERR(crtc_state);
10864 goto fail;
10865 }
10866
49d6fa21 10867 crtc_state->base.active = crtc_state->base.enable = true;
4be07317 10868
6492711d
CW
10869 if (!mode)
10870 mode = &load_detect_mode;
79e53945 10871
d2dff872
CW
10872 /* We need a framebuffer large enough to accommodate all accesses
10873 * that the plane may generate whilst we perform load detection.
10874 * We can not rely on the fbcon either being present (we get called
10875 * during its initialisation to detect all boot displays, or it may
10876 * not even exist) or that it is large enough to satisfy the
10877 * requested mode.
10878 */
94352cf9
DV
10879 fb = mode_fits_in_fbdev(dev, mode);
10880 if (fb == NULL) {
d2dff872 10881 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
94352cf9 10882 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
d2dff872
CW
10883 } else
10884 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
94352cf9 10885 if (IS_ERR(fb)) {
d2dff872 10886 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
412b61d8 10887 goto fail;
79e53945 10888 }
79e53945 10889
d3a40d1b
ACO
10890 ret = intel_modeset_setup_plane_state(state, crtc, mode, fb, 0, 0);
10891 if (ret)
10892 goto fail;
10893
edde3617
ML
10894 drm_framebuffer_unreference(fb);
10895
10896 ret = drm_atomic_set_mode_for_crtc(&crtc_state->base, mode);
10897 if (ret)
10898 goto fail;
10899
10900 ret = PTR_ERR_OR_ZERO(drm_atomic_get_connector_state(restore_state, connector));
10901 if (!ret)
10902 ret = PTR_ERR_OR_ZERO(drm_atomic_get_crtc_state(restore_state, crtc));
10903 if (!ret)
10904 ret = PTR_ERR_OR_ZERO(drm_atomic_get_plane_state(restore_state, crtc->primary));
10905 if (ret) {
10906 DRM_DEBUG_KMS("Failed to create a copy of old state to restore: %i\n", ret);
10907 goto fail;
10908 }
8c7b5ccb 10909
3ba86073
ML
10910 ret = drm_atomic_commit(state);
10911 if (ret) {
6492711d 10912 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
412b61d8 10913 goto fail;
79e53945 10914 }
edde3617
ML
10915
10916 old->restore_state = restore_state;
7173188d 10917
79e53945 10918 /* let the connector get through one full cycle before testing */
9d0498a2 10919 intel_wait_for_vblank(dev, intel_crtc->pipe);
7173188d 10920 return true;
412b61d8 10921
ad3c558f 10922fail:
e5d958ef 10923 drm_atomic_state_free(state);
edde3617
ML
10924 drm_atomic_state_free(restore_state);
10925 restore_state = state = NULL;
83a57153 10926
51fd371b
RC
10927 if (ret == -EDEADLK) {
10928 drm_modeset_backoff(ctx);
10929 goto retry;
10930 }
10931
412b61d8 10932 return false;
79e53945
JB
10933}
10934
d2434ab7 10935void intel_release_load_detect_pipe(struct drm_connector *connector,
49172fee
ACO
10936 struct intel_load_detect_pipe *old,
10937 struct drm_modeset_acquire_ctx *ctx)
79e53945 10938{
d2434ab7
DV
10939 struct intel_encoder *intel_encoder =
10940 intel_attached_encoder(connector);
4ef69c7a 10941 struct drm_encoder *encoder = &intel_encoder->base;
edde3617 10942 struct drm_atomic_state *state = old->restore_state;
d3a40d1b 10943 int ret;
79e53945 10944
d2dff872 10945 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
c23cc417 10946 connector->base.id, connector->name,
8e329a03 10947 encoder->base.id, encoder->name);
d2dff872 10948
edde3617 10949 if (!state)
0622a53c 10950 return;
79e53945 10951
edde3617
ML
10952 ret = drm_atomic_commit(state);
10953 if (ret) {
10954 DRM_DEBUG_KMS("Couldn't release load detect pipe: %i\n", ret);
10955 drm_atomic_state_free(state);
10956 }
79e53945
JB
10957}
10958
da4a1efa 10959static int i9xx_pll_refclk(struct drm_device *dev,
5cec258b 10960 const struct intel_crtc_state *pipe_config)
da4a1efa 10961{
fac5e23e 10962 struct drm_i915_private *dev_priv = to_i915(dev);
da4a1efa
VS
10963 u32 dpll = pipe_config->dpll_hw_state.dpll;
10964
10965 if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
e91e941b 10966 return dev_priv->vbt.lvds_ssc_freq;
da4a1efa
VS
10967 else if (HAS_PCH_SPLIT(dev))
10968 return 120000;
10969 else if (!IS_GEN2(dev))
10970 return 96000;
10971 else
10972 return 48000;
10973}
10974
79e53945 10975/* Returns the clock of the currently programmed mode of the given pipe. */
f1f644dc 10976static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 10977 struct intel_crtc_state *pipe_config)
79e53945 10978{
f1f644dc 10979 struct drm_device *dev = crtc->base.dev;
fac5e23e 10980 struct drm_i915_private *dev_priv = to_i915(dev);
f1f644dc 10981 int pipe = pipe_config->cpu_transcoder;
293623f7 10982 u32 dpll = pipe_config->dpll_hw_state.dpll;
79e53945 10983 u32 fp;
9e2c8475 10984 struct dpll clock;
dccbea3b 10985 int port_clock;
da4a1efa 10986 int refclk = i9xx_pll_refclk(dev, pipe_config);
79e53945
JB
10987
10988 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
293623f7 10989 fp = pipe_config->dpll_hw_state.fp0;
79e53945 10990 else
293623f7 10991 fp = pipe_config->dpll_hw_state.fp1;
79e53945
JB
10992
10993 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
f2b115e6
AJ
10994 if (IS_PINEVIEW(dev)) {
10995 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
10996 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
2177832f
SL
10997 } else {
10998 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
10999 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
11000 }
11001
a6c45cf0 11002 if (!IS_GEN2(dev)) {
f2b115e6
AJ
11003 if (IS_PINEVIEW(dev))
11004 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
11005 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
2177832f
SL
11006 else
11007 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
79e53945
JB
11008 DPLL_FPA01_P1_POST_DIV_SHIFT);
11009
11010 switch (dpll & DPLL_MODE_MASK) {
11011 case DPLLB_MODE_DAC_SERIAL:
11012 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
11013 5 : 10;
11014 break;
11015 case DPLLB_MODE_LVDS:
11016 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
11017 7 : 14;
11018 break;
11019 default:
28c97730 11020 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
79e53945 11021 "mode\n", (int)(dpll & DPLL_MODE_MASK));
f1f644dc 11022 return;
79e53945
JB
11023 }
11024
ac58c3f0 11025 if (IS_PINEVIEW(dev))
dccbea3b 11026 port_clock = pnv_calc_dpll_params(refclk, &clock);
ac58c3f0 11027 else
dccbea3b 11028 port_clock = i9xx_calc_dpll_params(refclk, &clock);
79e53945 11029 } else {
0fb58223 11030 u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
b1c560d1 11031 bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
79e53945
JB
11032
11033 if (is_lvds) {
11034 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
11035 DPLL_FPA01_P1_POST_DIV_SHIFT);
b1c560d1
VS
11036
11037 if (lvds & LVDS_CLKB_POWER_UP)
11038 clock.p2 = 7;
11039 else
11040 clock.p2 = 14;
79e53945
JB
11041 } else {
11042 if (dpll & PLL_P1_DIVIDE_BY_TWO)
11043 clock.p1 = 2;
11044 else {
11045 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
11046 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
11047 }
11048 if (dpll & PLL_P2_DIVIDE_BY_4)
11049 clock.p2 = 4;
11050 else
11051 clock.p2 = 2;
79e53945 11052 }
da4a1efa 11053
dccbea3b 11054 port_clock = i9xx_calc_dpll_params(refclk, &clock);
79e53945
JB
11055 }
11056
18442d08
VS
11057 /*
11058 * This value includes pixel_multiplier. We will use
241bfc38 11059 * port_clock to compute adjusted_mode.crtc_clock in the
18442d08
VS
11060 * encoder's get_config() function.
11061 */
dccbea3b 11062 pipe_config->port_clock = port_clock;
f1f644dc
JB
11063}
11064
6878da05
VS
11065int intel_dotclock_calculate(int link_freq,
11066 const struct intel_link_m_n *m_n)
f1f644dc 11067{
f1f644dc
JB
11068 /*
11069 * The calculation for the data clock is:
1041a02f 11070 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
f1f644dc 11071 * But we want to avoid losing precison if possible, so:
1041a02f 11072 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
f1f644dc
JB
11073 *
11074 * and the link clock is simpler:
1041a02f 11075 * link_clock = (m * link_clock) / n
f1f644dc
JB
11076 */
11077
6878da05
VS
11078 if (!m_n->link_n)
11079 return 0;
f1f644dc 11080
6878da05
VS
11081 return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
11082}
f1f644dc 11083
18442d08 11084static void ironlake_pch_clock_get(struct intel_crtc *crtc,
5cec258b 11085 struct intel_crtc_state *pipe_config)
6878da05 11086{
e3b247da 11087 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
79e53945 11088
18442d08
VS
11089 /* read out port_clock from the DPLL */
11090 i9xx_crtc_clock_get(crtc, pipe_config);
f1f644dc 11091
f1f644dc 11092 /*
e3b247da
VS
11093 * In case there is an active pipe without active ports,
11094 * we may need some idea for the dotclock anyway.
11095 * Calculate one based on the FDI configuration.
79e53945 11096 */
2d112de7 11097 pipe_config->base.adjusted_mode.crtc_clock =
21a727b3 11098 intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
18442d08 11099 &pipe_config->fdi_m_n);
79e53945
JB
11100}
11101
11102/** Returns the currently programmed mode of the given pipe. */
11103struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
11104 struct drm_crtc *crtc)
11105{
fac5e23e 11106 struct drm_i915_private *dev_priv = to_i915(dev);
79e53945 11107 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6e3c9717 11108 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
79e53945 11109 struct drm_display_mode *mode;
3f36b937 11110 struct intel_crtc_state *pipe_config;
fe2b8f9d
PZ
11111 int htot = I915_READ(HTOTAL(cpu_transcoder));
11112 int hsync = I915_READ(HSYNC(cpu_transcoder));
11113 int vtot = I915_READ(VTOTAL(cpu_transcoder));
11114 int vsync = I915_READ(VSYNC(cpu_transcoder));
293623f7 11115 enum pipe pipe = intel_crtc->pipe;
79e53945
JB
11116
11117 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
11118 if (!mode)
11119 return NULL;
11120
3f36b937
TU
11121 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
11122 if (!pipe_config) {
11123 kfree(mode);
11124 return NULL;
11125 }
11126
f1f644dc
JB
11127 /*
11128 * Construct a pipe_config sufficient for getting the clock info
11129 * back out of crtc_clock_get.
11130 *
11131 * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
11132 * to use a real value here instead.
11133 */
3f36b937
TU
11134 pipe_config->cpu_transcoder = (enum transcoder) pipe;
11135 pipe_config->pixel_multiplier = 1;
11136 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(pipe));
11137 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(pipe));
11138 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(pipe));
11139 i9xx_crtc_clock_get(intel_crtc, pipe_config);
11140
11141 mode->clock = pipe_config->port_clock / pipe_config->pixel_multiplier;
79e53945
JB
11142 mode->hdisplay = (htot & 0xffff) + 1;
11143 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
11144 mode->hsync_start = (hsync & 0xffff) + 1;
11145 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
11146 mode->vdisplay = (vtot & 0xffff) + 1;
11147 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
11148 mode->vsync_start = (vsync & 0xffff) + 1;
11149 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
11150
11151 drm_mode_set_name(mode);
79e53945 11152
3f36b937
TU
11153 kfree(pipe_config);
11154
79e53945
JB
11155 return mode;
11156}
11157
11158static void intel_crtc_destroy(struct drm_crtc *crtc)
11159{
11160 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
67e77c5a 11161 struct drm_device *dev = crtc->dev;
51cbaf01 11162 struct intel_flip_work *work;
67e77c5a 11163
5e2d7afc 11164 spin_lock_irq(&dev->event_lock);
5a21b665
DV
11165 work = intel_crtc->flip_work;
11166 intel_crtc->flip_work = NULL;
11167 spin_unlock_irq(&dev->event_lock);
67e77c5a 11168
5a21b665 11169 if (work) {
51cbaf01
ML
11170 cancel_work_sync(&work->mmio_work);
11171 cancel_work_sync(&work->unpin_work);
5a21b665 11172 kfree(work);
67e77c5a 11173 }
79e53945
JB
11174
11175 drm_crtc_cleanup(crtc);
67e77c5a 11176
79e53945
JB
11177 kfree(intel_crtc);
11178}
11179
6b95a207
KH
11180static void intel_unpin_work_fn(struct work_struct *__work)
11181{
51cbaf01
ML
11182 struct intel_flip_work *work =
11183 container_of(__work, struct intel_flip_work, unpin_work);
5a21b665
DV
11184 struct intel_crtc *crtc = to_intel_crtc(work->crtc);
11185 struct drm_device *dev = crtc->base.dev;
11186 struct drm_plane *primary = crtc->base.primary;
03f476e1 11187
5a21b665
DV
11188 if (is_mmio_work(work))
11189 flush_work(&work->mmio_work);
03f476e1 11190
5a21b665
DV
11191 mutex_lock(&dev->struct_mutex);
11192 intel_unpin_fb_obj(work->old_fb, primary->state->rotation);
f8c417cd 11193 i915_gem_object_put(work->pending_flip_obj);
5a21b665 11194 mutex_unlock(&dev->struct_mutex);
143f73b3 11195
e8a261ea
CW
11196 i915_gem_request_put(work->flip_queued_req);
11197
5748b6a1
CW
11198 intel_frontbuffer_flip_complete(to_i915(dev),
11199 to_intel_plane(primary)->frontbuffer_bit);
5a21b665
DV
11200 intel_fbc_post_update(crtc);
11201 drm_framebuffer_unreference(work->old_fb);
143f73b3 11202
5a21b665
DV
11203 BUG_ON(atomic_read(&crtc->unpin_work_count) == 0);
11204 atomic_dec(&crtc->unpin_work_count);
a6747b73 11205
5a21b665
DV
11206 kfree(work);
11207}
d9e86c0e 11208
5a21b665
DV
11209/* Is 'a' after or equal to 'b'? */
11210static bool g4x_flip_count_after_eq(u32 a, u32 b)
11211{
11212 return !((a - b) & 0x80000000);
11213}
143f73b3 11214
5a21b665
DV
11215static bool __pageflip_finished_cs(struct intel_crtc *crtc,
11216 struct intel_flip_work *work)
11217{
11218 struct drm_device *dev = crtc->base.dev;
fac5e23e 11219 struct drm_i915_private *dev_priv = to_i915(dev);
5a21b665 11220 unsigned reset_counter;
143f73b3 11221
5a21b665
DV
11222 reset_counter = i915_reset_counter(&dev_priv->gpu_error);
11223 if (crtc->reset_counter != reset_counter)
11224 return true;
143f73b3 11225
5a21b665
DV
11226 /*
11227 * The relevant registers doen't exist on pre-ctg.
11228 * As the flip done interrupt doesn't trigger for mmio
11229 * flips on gmch platforms, a flip count check isn't
11230 * really needed there. But since ctg has the registers,
11231 * include it in the check anyway.
11232 */
11233 if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
11234 return true;
b4a98e57 11235
5a21b665
DV
11236 /*
11237 * BDW signals flip done immediately if the plane
11238 * is disabled, even if the plane enable is already
11239 * armed to occur at the next vblank :(
11240 */
f99d7069 11241
5a21b665
DV
11242 /*
11243 * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
11244 * used the same base address. In that case the mmio flip might
11245 * have completed, but the CS hasn't even executed the flip yet.
11246 *
11247 * A flip count check isn't enough as the CS might have updated
11248 * the base address just after start of vblank, but before we
11249 * managed to process the interrupt. This means we'd complete the
11250 * CS flip too soon.
11251 *
11252 * Combining both checks should get us a good enough result. It may
11253 * still happen that the CS flip has been executed, but has not
11254 * yet actually completed. But in case the base address is the same
11255 * anyway, we don't really care.
11256 */
11257 return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
11258 crtc->flip_work->gtt_offset &&
11259 g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_G4X(crtc->pipe)),
11260 crtc->flip_work->flip_count);
11261}
b4a98e57 11262
5a21b665
DV
11263static bool
11264__pageflip_finished_mmio(struct intel_crtc *crtc,
11265 struct intel_flip_work *work)
11266{
11267 /*
11268 * MMIO work completes when vblank is different from
11269 * flip_queued_vblank.
11270 *
11271 * Reset counter value doesn't matter, this is handled by
11272 * i915_wait_request finishing early, so no need to handle
11273 * reset here.
11274 */
11275 return intel_crtc_get_vblank_counter(crtc) != work->flip_queued_vblank;
6b95a207
KH
11276}
11277
51cbaf01
ML
11278
11279static bool pageflip_finished(struct intel_crtc *crtc,
11280 struct intel_flip_work *work)
11281{
11282 if (!atomic_read(&work->pending))
11283 return false;
11284
11285 smp_rmb();
11286
5a21b665
DV
11287 if (is_mmio_work(work))
11288 return __pageflip_finished_mmio(crtc, work);
11289 else
11290 return __pageflip_finished_cs(crtc, work);
11291}
11292
11293void intel_finish_page_flip_cs(struct drm_i915_private *dev_priv, int pipe)
11294{
91c8a326 11295 struct drm_device *dev = &dev_priv->drm;
5a21b665
DV
11296 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
11297 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11298 struct intel_flip_work *work;
11299 unsigned long flags;
11300
11301 /* Ignore early vblank irqs */
11302 if (!crtc)
11303 return;
11304
51cbaf01 11305 /*
5a21b665
DV
11306 * This is called both by irq handlers and the reset code (to complete
11307 * lost pageflips) so needs the full irqsave spinlocks.
51cbaf01 11308 */
5a21b665
DV
11309 spin_lock_irqsave(&dev->event_lock, flags);
11310 work = intel_crtc->flip_work;
11311
11312 if (work != NULL &&
11313 !is_mmio_work(work) &&
11314 pageflip_finished(intel_crtc, work))
11315 page_flip_completed(intel_crtc);
11316
11317 spin_unlock_irqrestore(&dev->event_lock, flags);
75f7f3ec
VS
11318}
11319
51cbaf01 11320void intel_finish_page_flip_mmio(struct drm_i915_private *dev_priv, int pipe)
6b95a207 11321{
91c8a326 11322 struct drm_device *dev = &dev_priv->drm;
5251f04e
ML
11323 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
11324 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
51cbaf01 11325 struct intel_flip_work *work;
6b95a207
KH
11326 unsigned long flags;
11327
5251f04e
ML
11328 /* Ignore early vblank irqs */
11329 if (!crtc)
11330 return;
f326038a
DV
11331
11332 /*
11333 * This is called both by irq handlers and the reset code (to complete
11334 * lost pageflips) so needs the full irqsave spinlocks.
e7d841ca 11335 */
6b95a207 11336 spin_lock_irqsave(&dev->event_lock, flags);
5a21b665 11337 work = intel_crtc->flip_work;
5251f04e 11338
5a21b665
DV
11339 if (work != NULL &&
11340 is_mmio_work(work) &&
11341 pageflip_finished(intel_crtc, work))
11342 page_flip_completed(intel_crtc);
5251f04e 11343
6b95a207
KH
11344 spin_unlock_irqrestore(&dev->event_lock, flags);
11345}
11346
5a21b665
DV
11347static inline void intel_mark_page_flip_active(struct intel_crtc *crtc,
11348 struct intel_flip_work *work)
84c33a64 11349{
5a21b665 11350 work->flip_queued_vblank = intel_crtc_get_vblank_counter(crtc);
84c33a64 11351
5a21b665
DV
11352 /* Ensure that the work item is consistent when activating it ... */
11353 smp_mb__before_atomic();
11354 atomic_set(&work->pending, 1);
11355}
a6747b73 11356
5a21b665
DV
11357static int intel_gen2_queue_flip(struct drm_device *dev,
11358 struct drm_crtc *crtc,
11359 struct drm_framebuffer *fb,
11360 struct drm_i915_gem_object *obj,
11361 struct drm_i915_gem_request *req,
11362 uint32_t flags)
11363{
7e37f889 11364 struct intel_ring *ring = req->ring;
5a21b665
DV
11365 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11366 u32 flip_mask;
11367 int ret;
143f73b3 11368
5a21b665
DV
11369 ret = intel_ring_begin(req, 6);
11370 if (ret)
11371 return ret;
143f73b3 11372
5a21b665
DV
11373 /* Can't queue multiple flips, so wait for the previous
11374 * one to finish before executing the next.
11375 */
11376 if (intel_crtc->plane)
11377 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
11378 else
11379 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
b5321f30
CW
11380 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
11381 intel_ring_emit(ring, MI_NOOP);
11382 intel_ring_emit(ring, MI_DISPLAY_FLIP |
5a21b665 11383 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
b5321f30
CW
11384 intel_ring_emit(ring, fb->pitches[0]);
11385 intel_ring_emit(ring, intel_crtc->flip_work->gtt_offset);
11386 intel_ring_emit(ring, 0); /* aux display base address, unused */
143f73b3 11387
5a21b665
DV
11388 return 0;
11389}
84c33a64 11390
5a21b665
DV
11391static int intel_gen3_queue_flip(struct drm_device *dev,
11392 struct drm_crtc *crtc,
11393 struct drm_framebuffer *fb,
11394 struct drm_i915_gem_object *obj,
11395 struct drm_i915_gem_request *req,
11396 uint32_t flags)
11397{
7e37f889 11398 struct intel_ring *ring = req->ring;
5a21b665
DV
11399 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11400 u32 flip_mask;
11401 int ret;
d55dbd06 11402
5a21b665
DV
11403 ret = intel_ring_begin(req, 6);
11404 if (ret)
11405 return ret;
d55dbd06 11406
5a21b665
DV
11407 if (intel_crtc->plane)
11408 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
11409 else
11410 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
b5321f30
CW
11411 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
11412 intel_ring_emit(ring, MI_NOOP);
11413 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
5a21b665 11414 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
b5321f30
CW
11415 intel_ring_emit(ring, fb->pitches[0]);
11416 intel_ring_emit(ring, intel_crtc->flip_work->gtt_offset);
11417 intel_ring_emit(ring, MI_NOOP);
fd8e058a 11418
5a21b665
DV
11419 return 0;
11420}
84c33a64 11421
5a21b665
DV
11422static int intel_gen4_queue_flip(struct drm_device *dev,
11423 struct drm_crtc *crtc,
11424 struct drm_framebuffer *fb,
11425 struct drm_i915_gem_object *obj,
11426 struct drm_i915_gem_request *req,
11427 uint32_t flags)
11428{
7e37f889 11429 struct intel_ring *ring = req->ring;
fac5e23e 11430 struct drm_i915_private *dev_priv = to_i915(dev);
5a21b665
DV
11431 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11432 uint32_t pf, pipesrc;
11433 int ret;
143f73b3 11434
5a21b665
DV
11435 ret = intel_ring_begin(req, 4);
11436 if (ret)
11437 return ret;
143f73b3 11438
5a21b665
DV
11439 /* i965+ uses the linear or tiled offsets from the
11440 * Display Registers (which do not change across a page-flip)
11441 * so we need only reprogram the base address.
11442 */
b5321f30 11443 intel_ring_emit(ring, MI_DISPLAY_FLIP |
5a21b665 11444 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
b5321f30
CW
11445 intel_ring_emit(ring, fb->pitches[0]);
11446 intel_ring_emit(ring, intel_crtc->flip_work->gtt_offset |
3e510a8e 11447 i915_gem_object_get_tiling(obj));
5a21b665
DV
11448
11449 /* XXX Enabling the panel-fitter across page-flip is so far
11450 * untested on non-native modes, so ignore it for now.
11451 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
11452 */
11453 pf = 0;
11454 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
b5321f30 11455 intel_ring_emit(ring, pf | pipesrc);
143f73b3 11456
5a21b665 11457 return 0;
8c9f3aaf
JB
11458}
11459
5a21b665
DV
11460static int intel_gen6_queue_flip(struct drm_device *dev,
11461 struct drm_crtc *crtc,
11462 struct drm_framebuffer *fb,
11463 struct drm_i915_gem_object *obj,
11464 struct drm_i915_gem_request *req,
11465 uint32_t flags)
da20eabd 11466{
7e37f889 11467 struct intel_ring *ring = req->ring;
fac5e23e 11468 struct drm_i915_private *dev_priv = to_i915(dev);
5a21b665
DV
11469 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11470 uint32_t pf, pipesrc;
11471 int ret;
d21fbe87 11472
5a21b665
DV
11473 ret = intel_ring_begin(req, 4);
11474 if (ret)
11475 return ret;
92826fcd 11476
b5321f30 11477 intel_ring_emit(ring, MI_DISPLAY_FLIP |
5a21b665 11478 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
3e510a8e 11479 intel_ring_emit(ring, fb->pitches[0] | i915_gem_object_get_tiling(obj));
b5321f30 11480 intel_ring_emit(ring, intel_crtc->flip_work->gtt_offset);
92826fcd 11481
5a21b665
DV
11482 /* Contrary to the suggestions in the documentation,
11483 * "Enable Panel Fitter" does not seem to be required when page
11484 * flipping with a non-native mode, and worse causes a normal
11485 * modeset to fail.
11486 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
11487 */
11488 pf = 0;
11489 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
b5321f30 11490 intel_ring_emit(ring, pf | pipesrc);
7809e5ae 11491
5a21b665 11492 return 0;
7809e5ae
MR
11493}
11494
5a21b665
DV
11495static int intel_gen7_queue_flip(struct drm_device *dev,
11496 struct drm_crtc *crtc,
11497 struct drm_framebuffer *fb,
11498 struct drm_i915_gem_object *obj,
11499 struct drm_i915_gem_request *req,
11500 uint32_t flags)
d21fbe87 11501{
7e37f889 11502 struct intel_ring *ring = req->ring;
5a21b665
DV
11503 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11504 uint32_t plane_bit = 0;
11505 int len, ret;
d21fbe87 11506
5a21b665
DV
11507 switch (intel_crtc->plane) {
11508 case PLANE_A:
11509 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
11510 break;
11511 case PLANE_B:
11512 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
11513 break;
11514 case PLANE_C:
11515 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
11516 break;
11517 default:
11518 WARN_ONCE(1, "unknown plane in flip command\n");
11519 return -ENODEV;
11520 }
11521
11522 len = 4;
b5321f30 11523 if (req->engine->id == RCS) {
5a21b665
DV
11524 len += 6;
11525 /*
11526 * On Gen 8, SRM is now taking an extra dword to accommodate
11527 * 48bits addresses, and we need a NOOP for the batch size to
11528 * stay even.
11529 */
11530 if (IS_GEN8(dev))
11531 len += 2;
11532 }
11533
11534 /*
11535 * BSpec MI_DISPLAY_FLIP for IVB:
11536 * "The full packet must be contained within the same cache line."
11537 *
11538 * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
11539 * cacheline, if we ever start emitting more commands before
11540 * the MI_DISPLAY_FLIP we may need to first emit everything else,
11541 * then do the cacheline alignment, and finally emit the
11542 * MI_DISPLAY_FLIP.
11543 */
11544 ret = intel_ring_cacheline_align(req);
11545 if (ret)
11546 return ret;
11547
11548 ret = intel_ring_begin(req, len);
11549 if (ret)
11550 return ret;
11551
11552 /* Unmask the flip-done completion message. Note that the bspec says that
11553 * we should do this for both the BCS and RCS, and that we must not unmask
11554 * more than one flip event at any time (or ensure that one flip message
11555 * can be sent by waiting for flip-done prior to queueing new flips).
11556 * Experimentation says that BCS works despite DERRMR masking all
11557 * flip-done completion events and that unmasking all planes at once
11558 * for the RCS also doesn't appear to drop events. Setting the DERRMR
11559 * to zero does lead to lockups within MI_DISPLAY_FLIP.
11560 */
b5321f30
CW
11561 if (req->engine->id == RCS) {
11562 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
11563 intel_ring_emit_reg(ring, DERRMR);
11564 intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
5a21b665
DV
11565 DERRMR_PIPEB_PRI_FLIP_DONE |
11566 DERRMR_PIPEC_PRI_FLIP_DONE));
11567 if (IS_GEN8(dev))
b5321f30 11568 intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8 |
5a21b665
DV
11569 MI_SRM_LRM_GLOBAL_GTT);
11570 else
b5321f30 11571 intel_ring_emit(ring, MI_STORE_REGISTER_MEM |
5a21b665 11572 MI_SRM_LRM_GLOBAL_GTT);
b5321f30
CW
11573 intel_ring_emit_reg(ring, DERRMR);
11574 intel_ring_emit(ring, req->engine->scratch.gtt_offset + 256);
5a21b665 11575 if (IS_GEN8(dev)) {
b5321f30
CW
11576 intel_ring_emit(ring, 0);
11577 intel_ring_emit(ring, MI_NOOP);
5a21b665
DV
11578 }
11579 }
11580
b5321f30 11581 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
3e510a8e 11582 intel_ring_emit(ring, fb->pitches[0] | i915_gem_object_get_tiling(obj));
b5321f30
CW
11583 intel_ring_emit(ring, intel_crtc->flip_work->gtt_offset);
11584 intel_ring_emit(ring, (MI_NOOP));
5a21b665
DV
11585
11586 return 0;
11587}
11588
11589static bool use_mmio_flip(struct intel_engine_cs *engine,
11590 struct drm_i915_gem_object *obj)
11591{
c37efb99
CW
11592 struct reservation_object *resv;
11593
5a21b665
DV
11594 /*
11595 * This is not being used for older platforms, because
11596 * non-availability of flip done interrupt forces us to use
11597 * CS flips. Older platforms derive flip done using some clever
11598 * tricks involving the flip_pending status bits and vblank irqs.
11599 * So using MMIO flips there would disrupt this mechanism.
11600 */
11601
11602 if (engine == NULL)
11603 return true;
11604
11605 if (INTEL_GEN(engine->i915) < 5)
11606 return false;
11607
11608 if (i915.use_mmio_flip < 0)
11609 return false;
11610 else if (i915.use_mmio_flip > 0)
11611 return true;
11612 else if (i915.enable_execlists)
11613 return true;
c37efb99
CW
11614
11615 resv = i915_gem_object_get_dmabuf_resv(obj);
11616 if (resv && !reservation_object_test_signaled_rcu(resv, false))
5a21b665 11617 return true;
c37efb99 11618
d72d908b
CW
11619 return engine != i915_gem_active_get_engine(&obj->last_write,
11620 &obj->base.dev->struct_mutex);
5a21b665
DV
11621}
11622
11623static void skl_do_mmio_flip(struct intel_crtc *intel_crtc,
11624 unsigned int rotation,
11625 struct intel_flip_work *work)
11626{
11627 struct drm_device *dev = intel_crtc->base.dev;
fac5e23e 11628 struct drm_i915_private *dev_priv = to_i915(dev);
5a21b665
DV
11629 struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
11630 const enum pipe pipe = intel_crtc->pipe;
d2196774 11631 u32 ctl, stride = skl_plane_stride(fb, 0, rotation);
5a21b665
DV
11632
11633 ctl = I915_READ(PLANE_CTL(pipe, 0));
11634 ctl &= ~PLANE_CTL_TILED_MASK;
11635 switch (fb->modifier[0]) {
11636 case DRM_FORMAT_MOD_NONE:
11637 break;
11638 case I915_FORMAT_MOD_X_TILED:
11639 ctl |= PLANE_CTL_TILED_X;
11640 break;
11641 case I915_FORMAT_MOD_Y_TILED:
11642 ctl |= PLANE_CTL_TILED_Y;
11643 break;
11644 case I915_FORMAT_MOD_Yf_TILED:
11645 ctl |= PLANE_CTL_TILED_YF;
11646 break;
11647 default:
11648 MISSING_CASE(fb->modifier[0]);
11649 }
11650
5a21b665
DV
11651 /*
11652 * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
11653 * PLANE_SURF updates, the update is then guaranteed to be atomic.
11654 */
11655 I915_WRITE(PLANE_CTL(pipe, 0), ctl);
11656 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
11657
11658 I915_WRITE(PLANE_SURF(pipe, 0), work->gtt_offset);
11659 POSTING_READ(PLANE_SURF(pipe, 0));
11660}
11661
11662static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc,
11663 struct intel_flip_work *work)
11664{
11665 struct drm_device *dev = intel_crtc->base.dev;
fac5e23e 11666 struct drm_i915_private *dev_priv = to_i915(dev);
5a21b665
DV
11667 struct intel_framebuffer *intel_fb =
11668 to_intel_framebuffer(intel_crtc->base.primary->fb);
11669 struct drm_i915_gem_object *obj = intel_fb->obj;
11670 i915_reg_t reg = DSPCNTR(intel_crtc->plane);
11671 u32 dspcntr;
11672
11673 dspcntr = I915_READ(reg);
11674
3e510a8e 11675 if (i915_gem_object_is_tiled(obj))
5a21b665
DV
11676 dspcntr |= DISPPLANE_TILED;
11677 else
11678 dspcntr &= ~DISPPLANE_TILED;
11679
11680 I915_WRITE(reg, dspcntr);
11681
11682 I915_WRITE(DSPSURF(intel_crtc->plane), work->gtt_offset);
11683 POSTING_READ(DSPSURF(intel_crtc->plane));
11684}
11685
11686static void intel_mmio_flip_work_func(struct work_struct *w)
11687{
11688 struct intel_flip_work *work =
11689 container_of(w, struct intel_flip_work, mmio_work);
11690 struct intel_crtc *crtc = to_intel_crtc(work->crtc);
11691 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
11692 struct intel_framebuffer *intel_fb =
11693 to_intel_framebuffer(crtc->base.primary->fb);
11694 struct drm_i915_gem_object *obj = intel_fb->obj;
c37efb99 11695 struct reservation_object *resv;
5a21b665
DV
11696
11697 if (work->flip_queued_req)
776f3236
CW
11698 WARN_ON(i915_wait_request(work->flip_queued_req,
11699 false, NULL,
11700 NO_WAITBOOST));
5a21b665
DV
11701
11702 /* For framebuffer backed by dmabuf, wait for fence */
c37efb99
CW
11703 resv = i915_gem_object_get_dmabuf_resv(obj);
11704 if (resv)
11705 WARN_ON(reservation_object_wait_timeout_rcu(resv, false, false,
5a21b665
DV
11706 MAX_SCHEDULE_TIMEOUT) < 0);
11707
11708 intel_pipe_update_start(crtc);
11709
11710 if (INTEL_GEN(dev_priv) >= 9)
11711 skl_do_mmio_flip(crtc, work->rotation, work);
11712 else
11713 /* use_mmio_flip() retricts MMIO flips to ilk+ */
11714 ilk_do_mmio_flip(crtc, work);
11715
11716 intel_pipe_update_end(crtc, work);
11717}
11718
11719static int intel_default_queue_flip(struct drm_device *dev,
11720 struct drm_crtc *crtc,
11721 struct drm_framebuffer *fb,
11722 struct drm_i915_gem_object *obj,
11723 struct drm_i915_gem_request *req,
11724 uint32_t flags)
11725{
11726 return -ENODEV;
11727}
11728
11729static bool __pageflip_stall_check_cs(struct drm_i915_private *dev_priv,
11730 struct intel_crtc *intel_crtc,
11731 struct intel_flip_work *work)
11732{
11733 u32 addr, vblank;
11734
11735 if (!atomic_read(&work->pending))
11736 return false;
11737
11738 smp_rmb();
11739
11740 vblank = intel_crtc_get_vblank_counter(intel_crtc);
11741 if (work->flip_ready_vblank == 0) {
11742 if (work->flip_queued_req &&
f69a02c9 11743 !i915_gem_request_completed(work->flip_queued_req))
5a21b665
DV
11744 return false;
11745
11746 work->flip_ready_vblank = vblank;
11747 }
11748
11749 if (vblank - work->flip_ready_vblank < 3)
11750 return false;
11751
11752 /* Potential stall - if we see that the flip has happened,
11753 * assume a missed interrupt. */
11754 if (INTEL_GEN(dev_priv) >= 4)
11755 addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
11756 else
11757 addr = I915_READ(DSPADDR(intel_crtc->plane));
11758
11759 /* There is a potential issue here with a false positive after a flip
11760 * to the same address. We could address this by checking for a
11761 * non-incrementing frame counter.
11762 */
11763 return addr == work->gtt_offset;
11764}
11765
11766void intel_check_page_flip(struct drm_i915_private *dev_priv, int pipe)
11767{
91c8a326 11768 struct drm_device *dev = &dev_priv->drm;
5a21b665
DV
11769 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
11770 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11771 struct intel_flip_work *work;
11772
11773 WARN_ON(!in_interrupt());
11774
11775 if (crtc == NULL)
11776 return;
11777
11778 spin_lock(&dev->event_lock);
11779 work = intel_crtc->flip_work;
11780
11781 if (work != NULL && !is_mmio_work(work) &&
11782 __pageflip_stall_check_cs(dev_priv, intel_crtc, work)) {
11783 WARN_ONCE(1,
11784 "Kicking stuck page flip: queued at %d, now %d\n",
11785 work->flip_queued_vblank, intel_crtc_get_vblank_counter(intel_crtc));
11786 page_flip_completed(intel_crtc);
11787 work = NULL;
11788 }
11789
11790 if (work != NULL && !is_mmio_work(work) &&
11791 intel_crtc_get_vblank_counter(intel_crtc) - work->flip_queued_vblank > 1)
11792 intel_queue_rps_boost_for_request(work->flip_queued_req);
11793 spin_unlock(&dev->event_lock);
11794}
11795
11796static int intel_crtc_page_flip(struct drm_crtc *crtc,
11797 struct drm_framebuffer *fb,
11798 struct drm_pending_vblank_event *event,
11799 uint32_t page_flip_flags)
11800{
11801 struct drm_device *dev = crtc->dev;
fac5e23e 11802 struct drm_i915_private *dev_priv = to_i915(dev);
5a21b665
DV
11803 struct drm_framebuffer *old_fb = crtc->primary->fb;
11804 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
11805 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11806 struct drm_plane *primary = crtc->primary;
11807 enum pipe pipe = intel_crtc->pipe;
11808 struct intel_flip_work *work;
11809 struct intel_engine_cs *engine;
11810 bool mmio_flip;
8e637178 11811 struct drm_i915_gem_request *request;
5a21b665
DV
11812 int ret;
11813
11814 /*
11815 * drm_mode_page_flip_ioctl() should already catch this, but double
11816 * check to be safe. In the future we may enable pageflipping from
11817 * a disabled primary plane.
11818 */
11819 if (WARN_ON(intel_fb_obj(old_fb) == NULL))
11820 return -EBUSY;
11821
11822 /* Can't change pixel format via MI display flips. */
11823 if (fb->pixel_format != crtc->primary->fb->pixel_format)
11824 return -EINVAL;
11825
11826 /*
11827 * TILEOFF/LINOFF registers can't be changed via MI display flips.
11828 * Note that pitch changes could also affect these register.
11829 */
11830 if (INTEL_INFO(dev)->gen > 3 &&
11831 (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
11832 fb->pitches[0] != crtc->primary->fb->pitches[0]))
11833 return -EINVAL;
11834
11835 if (i915_terminally_wedged(&dev_priv->gpu_error))
11836 goto out_hang;
11837
11838 work = kzalloc(sizeof(*work), GFP_KERNEL);
11839 if (work == NULL)
11840 return -ENOMEM;
11841
11842 work->event = event;
11843 work->crtc = crtc;
11844 work->old_fb = old_fb;
11845 INIT_WORK(&work->unpin_work, intel_unpin_work_fn);
11846
11847 ret = drm_crtc_vblank_get(crtc);
11848 if (ret)
11849 goto free_work;
11850
11851 /* We borrow the event spin lock for protecting flip_work */
11852 spin_lock_irq(&dev->event_lock);
11853 if (intel_crtc->flip_work) {
11854 /* Before declaring the flip queue wedged, check if
11855 * the hardware completed the operation behind our backs.
11856 */
11857 if (pageflip_finished(intel_crtc, intel_crtc->flip_work)) {
11858 DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
11859 page_flip_completed(intel_crtc);
11860 } else {
11861 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
11862 spin_unlock_irq(&dev->event_lock);
11863
11864 drm_crtc_vblank_put(crtc);
11865 kfree(work);
11866 return -EBUSY;
11867 }
11868 }
11869 intel_crtc->flip_work = work;
11870 spin_unlock_irq(&dev->event_lock);
11871
11872 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
11873 flush_workqueue(dev_priv->wq);
11874
11875 /* Reference the objects for the scheduled work. */
11876 drm_framebuffer_reference(work->old_fb);
5a21b665
DV
11877
11878 crtc->primary->fb = fb;
11879 update_state_fb(crtc->primary);
faf68d92
ML
11880
11881 intel_fbc_pre_update(intel_crtc, intel_crtc->config,
11882 to_intel_plane_state(primary->state));
5a21b665 11883
25dc556a 11884 work->pending_flip_obj = i915_gem_object_get(obj);
5a21b665
DV
11885
11886 ret = i915_mutex_lock_interruptible(dev);
11887 if (ret)
11888 goto cleanup;
11889
11890 intel_crtc->reset_counter = i915_reset_counter(&dev_priv->gpu_error);
11891 if (__i915_reset_in_progress_or_wedged(intel_crtc->reset_counter)) {
11892 ret = -EIO;
11893 goto cleanup;
11894 }
11895
11896 atomic_inc(&intel_crtc->unpin_work_count);
11897
11898 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
11899 work->flip_count = I915_READ(PIPE_FLIPCOUNT_G4X(pipe)) + 1;
11900
11901 if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
11902 engine = &dev_priv->engine[BCS];
3e510a8e
CW
11903 if (i915_gem_object_get_tiling(obj) !=
11904 i915_gem_object_get_tiling(intel_fb_obj(work->old_fb)))
5a21b665
DV
11905 /* vlv: DISPLAY_FLIP fails to change tiling */
11906 engine = NULL;
11907 } else if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
11908 engine = &dev_priv->engine[BCS];
11909 } else if (INTEL_INFO(dev)->gen >= 7) {
d72d908b
CW
11910 engine = i915_gem_active_get_engine(&obj->last_write,
11911 &obj->base.dev->struct_mutex);
5a21b665
DV
11912 if (engine == NULL || engine->id != RCS)
11913 engine = &dev_priv->engine[BCS];
11914 } else {
11915 engine = &dev_priv->engine[RCS];
11916 }
11917
11918 mmio_flip = use_mmio_flip(engine, obj);
11919
5a21b665
DV
11920 ret = intel_pin_and_fence_fb_obj(fb, primary->state->rotation);
11921 if (ret)
11922 goto cleanup_pending;
11923
6687c906 11924 work->gtt_offset = intel_fb_gtt_offset(fb, primary->state->rotation);
5a21b665
DV
11925 work->gtt_offset += intel_crtc->dspaddr_offset;
11926 work->rotation = crtc->primary->state->rotation;
11927
11928 if (mmio_flip) {
11929 INIT_WORK(&work->mmio_work, intel_mmio_flip_work_func);
11930
d72d908b
CW
11931 work->flip_queued_req = i915_gem_active_get(&obj->last_write,
11932 &obj->base.dev->struct_mutex);
5a21b665
DV
11933 schedule_work(&work->mmio_work);
11934 } else {
8e637178
CW
11935 request = i915_gem_request_alloc(engine, engine->last_context);
11936 if (IS_ERR(request)) {
11937 ret = PTR_ERR(request);
11938 goto cleanup_unpin;
11939 }
11940
11941 ret = i915_gem_object_sync(obj, request);
11942 if (ret)
11943 goto cleanup_request;
11944
5a21b665
DV
11945 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, request,
11946 page_flip_flags);
11947 if (ret)
8e637178 11948 goto cleanup_request;
5a21b665
DV
11949
11950 intel_mark_page_flip_active(intel_crtc, work);
11951
8e637178 11952 work->flip_queued_req = i915_gem_request_get(request);
5a21b665
DV
11953 i915_add_request_no_flush(request);
11954 }
11955
11956 i915_gem_track_fb(intel_fb_obj(old_fb), obj,
11957 to_intel_plane(primary)->frontbuffer_bit);
11958 mutex_unlock(&dev->struct_mutex);
11959
5748b6a1 11960 intel_frontbuffer_flip_prepare(to_i915(dev),
5a21b665
DV
11961 to_intel_plane(primary)->frontbuffer_bit);
11962
11963 trace_i915_flip_request(intel_crtc->plane, obj);
11964
11965 return 0;
11966
8e637178
CW
11967cleanup_request:
11968 i915_add_request_no_flush(request);
5a21b665
DV
11969cleanup_unpin:
11970 intel_unpin_fb_obj(fb, crtc->primary->state->rotation);
11971cleanup_pending:
5a21b665
DV
11972 atomic_dec(&intel_crtc->unpin_work_count);
11973 mutex_unlock(&dev->struct_mutex);
11974cleanup:
11975 crtc->primary->fb = old_fb;
11976 update_state_fb(crtc->primary);
11977
34911fd3 11978 i915_gem_object_put_unlocked(obj);
5a21b665
DV
11979 drm_framebuffer_unreference(work->old_fb);
11980
11981 spin_lock_irq(&dev->event_lock);
11982 intel_crtc->flip_work = NULL;
11983 spin_unlock_irq(&dev->event_lock);
11984
11985 drm_crtc_vblank_put(crtc);
11986free_work:
11987 kfree(work);
11988
11989 if (ret == -EIO) {
11990 struct drm_atomic_state *state;
11991 struct drm_plane_state *plane_state;
11992
11993out_hang:
11994 state = drm_atomic_state_alloc(dev);
11995 if (!state)
11996 return -ENOMEM;
11997 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
11998
11999retry:
12000 plane_state = drm_atomic_get_plane_state(state, primary);
12001 ret = PTR_ERR_OR_ZERO(plane_state);
12002 if (!ret) {
12003 drm_atomic_set_fb_for_plane(plane_state, fb);
12004
12005 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
12006 if (!ret)
12007 ret = drm_atomic_commit(state);
12008 }
12009
12010 if (ret == -EDEADLK) {
12011 drm_modeset_backoff(state->acquire_ctx);
12012 drm_atomic_state_clear(state);
12013 goto retry;
12014 }
12015
12016 if (ret)
12017 drm_atomic_state_free(state);
12018
12019 if (ret == 0 && event) {
12020 spin_lock_irq(&dev->event_lock);
12021 drm_crtc_send_vblank_event(crtc, event);
12022 spin_unlock_irq(&dev->event_lock);
12023 }
12024 }
12025 return ret;
12026}
12027
12028
12029/**
12030 * intel_wm_need_update - Check whether watermarks need updating
12031 * @plane: drm plane
12032 * @state: new plane state
12033 *
12034 * Check current plane state versus the new one to determine whether
12035 * watermarks need to be recalculated.
12036 *
12037 * Returns true or false.
12038 */
12039static bool intel_wm_need_update(struct drm_plane *plane,
12040 struct drm_plane_state *state)
12041{
12042 struct intel_plane_state *new = to_intel_plane_state(state);
12043 struct intel_plane_state *cur = to_intel_plane_state(plane->state);
12044
12045 /* Update watermarks on tiling or size changes. */
12046 if (new->visible != cur->visible)
12047 return true;
12048
12049 if (!cur->base.fb || !new->base.fb)
12050 return false;
12051
12052 if (cur->base.fb->modifier[0] != new->base.fb->modifier[0] ||
12053 cur->base.rotation != new->base.rotation ||
12054 drm_rect_width(&new->src) != drm_rect_width(&cur->src) ||
12055 drm_rect_height(&new->src) != drm_rect_height(&cur->src) ||
12056 drm_rect_width(&new->dst) != drm_rect_width(&cur->dst) ||
12057 drm_rect_height(&new->dst) != drm_rect_height(&cur->dst))
12058 return true;
12059
12060 return false;
12061}
12062
12063static bool needs_scaling(struct intel_plane_state *state)
12064{
12065 int src_w = drm_rect_width(&state->src) >> 16;
12066 int src_h = drm_rect_height(&state->src) >> 16;
12067 int dst_w = drm_rect_width(&state->dst);
12068 int dst_h = drm_rect_height(&state->dst);
12069
12070 return (src_w != dst_w || src_h != dst_h);
12071}
d21fbe87 12072
da20eabd
ML
12073int intel_plane_atomic_calc_changes(struct drm_crtc_state *crtc_state,
12074 struct drm_plane_state *plane_state)
12075{
ab1d3a0e 12076 struct intel_crtc_state *pipe_config = to_intel_crtc_state(crtc_state);
da20eabd
ML
12077 struct drm_crtc *crtc = crtc_state->crtc;
12078 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
12079 struct drm_plane *plane = plane_state->plane;
12080 struct drm_device *dev = crtc->dev;
ed4a6a7c 12081 struct drm_i915_private *dev_priv = to_i915(dev);
da20eabd
ML
12082 struct intel_plane_state *old_plane_state =
12083 to_intel_plane_state(plane->state);
da20eabd
ML
12084 bool mode_changed = needs_modeset(crtc_state);
12085 bool was_crtc_enabled = crtc->state->active;
12086 bool is_crtc_enabled = crtc_state->active;
da20eabd
ML
12087 bool turn_off, turn_on, visible, was_visible;
12088 struct drm_framebuffer *fb = plane_state->fb;
78108b7c 12089 int ret;
da20eabd 12090
84114990 12091 if (INTEL_GEN(dev) >= 9 && plane->type != DRM_PLANE_TYPE_CURSOR) {
da20eabd
ML
12092 ret = skl_update_scaler_plane(
12093 to_intel_crtc_state(crtc_state),
12094 to_intel_plane_state(plane_state));
12095 if (ret)
12096 return ret;
12097 }
12098
da20eabd
ML
12099 was_visible = old_plane_state->visible;
12100 visible = to_intel_plane_state(plane_state)->visible;
12101
12102 if (!was_crtc_enabled && WARN_ON(was_visible))
12103 was_visible = false;
12104
35c08f43
ML
12105 /*
12106 * Visibility is calculated as if the crtc was on, but
12107 * after scaler setup everything depends on it being off
12108 * when the crtc isn't active.
f818ffea
VS
12109 *
12110 * FIXME this is wrong for watermarks. Watermarks should also
12111 * be computed as if the pipe would be active. Perhaps move
12112 * per-plane wm computation to the .check_plane() hook, and
12113 * only combine the results from all planes in the current place?
35c08f43
ML
12114 */
12115 if (!is_crtc_enabled)
12116 to_intel_plane_state(plane_state)->visible = visible = false;
da20eabd
ML
12117
12118 if (!was_visible && !visible)
12119 return 0;
12120
e8861675
ML
12121 if (fb != old_plane_state->base.fb)
12122 pipe_config->fb_changed = true;
12123
da20eabd
ML
12124 turn_off = was_visible && (!visible || mode_changed);
12125 turn_on = visible && (!was_visible || mode_changed);
12126
72660ce0 12127 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] has [PLANE:%d:%s] with fb %i\n",
78108b7c
VS
12128 intel_crtc->base.base.id,
12129 intel_crtc->base.name,
72660ce0
VS
12130 plane->base.id, plane->name,
12131 fb ? fb->base.id : -1);
da20eabd 12132
72660ce0
VS
12133 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] visible %i -> %i, off %i, on %i, ms %i\n",
12134 plane->base.id, plane->name,
12135 was_visible, visible,
da20eabd
ML
12136 turn_off, turn_on, mode_changed);
12137
caed361d
VS
12138 if (turn_on) {
12139 pipe_config->update_wm_pre = true;
12140
12141 /* must disable cxsr around plane enable/disable */
12142 if (plane->type != DRM_PLANE_TYPE_CURSOR)
12143 pipe_config->disable_cxsr = true;
12144 } else if (turn_off) {
12145 pipe_config->update_wm_post = true;
92826fcd 12146
852eb00d 12147 /* must disable cxsr around plane enable/disable */
e8861675 12148 if (plane->type != DRM_PLANE_TYPE_CURSOR)
ab1d3a0e 12149 pipe_config->disable_cxsr = true;
852eb00d 12150 } else if (intel_wm_need_update(plane, plane_state)) {
caed361d
VS
12151 /* FIXME bollocks */
12152 pipe_config->update_wm_pre = true;
12153 pipe_config->update_wm_post = true;
852eb00d 12154 }
da20eabd 12155
ed4a6a7c 12156 /* Pre-gen9 platforms need two-step watermark updates */
caed361d
VS
12157 if ((pipe_config->update_wm_pre || pipe_config->update_wm_post) &&
12158 INTEL_INFO(dev)->gen < 9 && dev_priv->display.optimize_watermarks)
ed4a6a7c
MR
12159 to_intel_crtc_state(crtc_state)->wm.need_postvbl_update = true;
12160
8be6ca85 12161 if (visible || was_visible)
cd202f69 12162 pipe_config->fb_bits |= to_intel_plane(plane)->frontbuffer_bit;
a9ff8714 12163
31ae71fc
ML
12164 /*
12165 * WaCxSRDisabledForSpriteScaling:ivb
12166 *
12167 * cstate->update_wm was already set above, so this flag will
12168 * take effect when we commit and program watermarks.
12169 */
12170 if (plane->type == DRM_PLANE_TYPE_OVERLAY && IS_IVYBRIDGE(dev) &&
12171 needs_scaling(to_intel_plane_state(plane_state)) &&
12172 !needs_scaling(old_plane_state))
12173 pipe_config->disable_lp_wm = true;
d21fbe87 12174
da20eabd
ML
12175 return 0;
12176}
12177
6d3a1ce7
ML
12178static bool encoders_cloneable(const struct intel_encoder *a,
12179 const struct intel_encoder *b)
12180{
12181 /* masks could be asymmetric, so check both ways */
12182 return a == b || (a->cloneable & (1 << b->type) &&
12183 b->cloneable & (1 << a->type));
12184}
12185
12186static bool check_single_encoder_cloning(struct drm_atomic_state *state,
12187 struct intel_crtc *crtc,
12188 struct intel_encoder *encoder)
12189{
12190 struct intel_encoder *source_encoder;
12191 struct drm_connector *connector;
12192 struct drm_connector_state *connector_state;
12193 int i;
12194
12195 for_each_connector_in_state(state, connector, connector_state, i) {
12196 if (connector_state->crtc != &crtc->base)
12197 continue;
12198
12199 source_encoder =
12200 to_intel_encoder(connector_state->best_encoder);
12201 if (!encoders_cloneable(encoder, source_encoder))
12202 return false;
12203 }
12204
12205 return true;
12206}
12207
6d3a1ce7
ML
12208static int intel_crtc_atomic_check(struct drm_crtc *crtc,
12209 struct drm_crtc_state *crtc_state)
12210{
cf5a15be 12211 struct drm_device *dev = crtc->dev;
fac5e23e 12212 struct drm_i915_private *dev_priv = to_i915(dev);
6d3a1ce7 12213 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
cf5a15be
ML
12214 struct intel_crtc_state *pipe_config =
12215 to_intel_crtc_state(crtc_state);
6d3a1ce7 12216 struct drm_atomic_state *state = crtc_state->state;
4d20cd86 12217 int ret;
6d3a1ce7
ML
12218 bool mode_changed = needs_modeset(crtc_state);
12219
852eb00d 12220 if (mode_changed && !crtc_state->active)
caed361d 12221 pipe_config->update_wm_post = true;
eddfcbcd 12222
ad421372
ML
12223 if (mode_changed && crtc_state->enable &&
12224 dev_priv->display.crtc_compute_clock &&
8106ddbd 12225 !WARN_ON(pipe_config->shared_dpll)) {
ad421372
ML
12226 ret = dev_priv->display.crtc_compute_clock(intel_crtc,
12227 pipe_config);
12228 if (ret)
12229 return ret;
12230 }
12231
82cf435b
LL
12232 if (crtc_state->color_mgmt_changed) {
12233 ret = intel_color_check(crtc, crtc_state);
12234 if (ret)
12235 return ret;
e7852a4b
LL
12236
12237 /*
12238 * Changing color management on Intel hardware is
12239 * handled as part of planes update.
12240 */
12241 crtc_state->planes_changed = true;
82cf435b
LL
12242 }
12243
e435d6e5 12244 ret = 0;
86c8bbbe 12245 if (dev_priv->display.compute_pipe_wm) {
e3bddded 12246 ret = dev_priv->display.compute_pipe_wm(pipe_config);
ed4a6a7c
MR
12247 if (ret) {
12248 DRM_DEBUG_KMS("Target pipe watermarks are invalid\n");
12249 return ret;
12250 }
12251 }
12252
12253 if (dev_priv->display.compute_intermediate_wm &&
12254 !to_intel_atomic_state(state)->skip_intermediate_wm) {
12255 if (WARN_ON(!dev_priv->display.compute_pipe_wm))
12256 return 0;
12257
12258 /*
12259 * Calculate 'intermediate' watermarks that satisfy both the
12260 * old state and the new state. We can program these
12261 * immediately.
12262 */
12263 ret = dev_priv->display.compute_intermediate_wm(crtc->dev,
12264 intel_crtc,
12265 pipe_config);
12266 if (ret) {
12267 DRM_DEBUG_KMS("No valid intermediate pipe watermarks are possible\n");
86c8bbbe 12268 return ret;
ed4a6a7c 12269 }
e3d5457c
VS
12270 } else if (dev_priv->display.compute_intermediate_wm) {
12271 if (HAS_PCH_SPLIT(dev_priv) && INTEL_GEN(dev_priv) < 9)
12272 pipe_config->wm.ilk.intermediate = pipe_config->wm.ilk.optimal;
86c8bbbe
MR
12273 }
12274
e435d6e5
ML
12275 if (INTEL_INFO(dev)->gen >= 9) {
12276 if (mode_changed)
12277 ret = skl_update_scaler_crtc(pipe_config);
12278
12279 if (!ret)
12280 ret = intel_atomic_setup_scalers(dev, intel_crtc,
12281 pipe_config);
12282 }
12283
12284 return ret;
6d3a1ce7
ML
12285}
12286
65b38e0d 12287static const struct drm_crtc_helper_funcs intel_helper_funcs = {
f6e5b160 12288 .mode_set_base_atomic = intel_pipe_set_base_atomic,
5a21b665
DV
12289 .atomic_begin = intel_begin_crtc_commit,
12290 .atomic_flush = intel_finish_crtc_commit,
6d3a1ce7 12291 .atomic_check = intel_crtc_atomic_check,
f6e5b160
CW
12292};
12293
d29b2f9d
ACO
12294static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
12295{
12296 struct intel_connector *connector;
12297
12298 for_each_intel_connector(dev, connector) {
8863dc7f
DV
12299 if (connector->base.state->crtc)
12300 drm_connector_unreference(&connector->base);
12301
d29b2f9d
ACO
12302 if (connector->base.encoder) {
12303 connector->base.state->best_encoder =
12304 connector->base.encoder;
12305 connector->base.state->crtc =
12306 connector->base.encoder->crtc;
8863dc7f
DV
12307
12308 drm_connector_reference(&connector->base);
d29b2f9d
ACO
12309 } else {
12310 connector->base.state->best_encoder = NULL;
12311 connector->base.state->crtc = NULL;
12312 }
12313 }
12314}
12315
050f7aeb 12316static void
eba905b2 12317connected_sink_compute_bpp(struct intel_connector *connector,
5cec258b 12318 struct intel_crtc_state *pipe_config)
050f7aeb
DV
12319{
12320 int bpp = pipe_config->pipe_bpp;
12321
12322 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
12323 connector->base.base.id,
c23cc417 12324 connector->base.name);
050f7aeb
DV
12325
12326 /* Don't use an invalid EDID bpc value */
12327 if (connector->base.display_info.bpc &&
12328 connector->base.display_info.bpc * 3 < bpp) {
12329 DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
12330 bpp, connector->base.display_info.bpc*3);
12331 pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
12332 }
12333
013dd9e0
JN
12334 /* Clamp bpp to default limit on screens without EDID 1.4 */
12335 if (connector->base.display_info.bpc == 0) {
12336 int type = connector->base.connector_type;
12337 int clamp_bpp = 24;
12338
12339 /* Fall back to 18 bpp when DP sink capability is unknown. */
12340 if (type == DRM_MODE_CONNECTOR_DisplayPort ||
12341 type == DRM_MODE_CONNECTOR_eDP)
12342 clamp_bpp = 18;
12343
12344 if (bpp > clamp_bpp) {
12345 DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of %d\n",
12346 bpp, clamp_bpp);
12347 pipe_config->pipe_bpp = clamp_bpp;
12348 }
050f7aeb
DV
12349 }
12350}
12351
4e53c2e0 12352static int
050f7aeb 12353compute_baseline_pipe_bpp(struct intel_crtc *crtc,
5cec258b 12354 struct intel_crtc_state *pipe_config)
4e53c2e0 12355{
050f7aeb 12356 struct drm_device *dev = crtc->base.dev;
1486017f 12357 struct drm_atomic_state *state;
da3ced29
ACO
12358 struct drm_connector *connector;
12359 struct drm_connector_state *connector_state;
1486017f 12360 int bpp, i;
4e53c2e0 12361
666a4537 12362 if ((IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)))
4e53c2e0 12363 bpp = 10*3;
d328c9d7
DV
12364 else if (INTEL_INFO(dev)->gen >= 5)
12365 bpp = 12*3;
12366 else
12367 bpp = 8*3;
12368
4e53c2e0 12369
4e53c2e0
DV
12370 pipe_config->pipe_bpp = bpp;
12371
1486017f
ACO
12372 state = pipe_config->base.state;
12373
4e53c2e0 12374 /* Clamp display bpp to EDID value */
da3ced29
ACO
12375 for_each_connector_in_state(state, connector, connector_state, i) {
12376 if (connector_state->crtc != &crtc->base)
4e53c2e0
DV
12377 continue;
12378
da3ced29
ACO
12379 connected_sink_compute_bpp(to_intel_connector(connector),
12380 pipe_config);
4e53c2e0
DV
12381 }
12382
12383 return bpp;
12384}
12385
644db711
DV
12386static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
12387{
12388 DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
12389 "type: 0x%x flags: 0x%x\n",
1342830c 12390 mode->crtc_clock,
644db711
DV
12391 mode->crtc_hdisplay, mode->crtc_hsync_start,
12392 mode->crtc_hsync_end, mode->crtc_htotal,
12393 mode->crtc_vdisplay, mode->crtc_vsync_start,
12394 mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
12395}
12396
c0b03411 12397static void intel_dump_pipe_config(struct intel_crtc *crtc,
5cec258b 12398 struct intel_crtc_state *pipe_config,
c0b03411
DV
12399 const char *context)
12400{
6a60cd87
CK
12401 struct drm_device *dev = crtc->base.dev;
12402 struct drm_plane *plane;
12403 struct intel_plane *intel_plane;
12404 struct intel_plane_state *state;
12405 struct drm_framebuffer *fb;
12406
78108b7c
VS
12407 DRM_DEBUG_KMS("[CRTC:%d:%s]%s config %p for pipe %c\n",
12408 crtc->base.base.id, crtc->base.name,
6a60cd87 12409 context, pipe_config, pipe_name(crtc->pipe));
c0b03411 12410
da205630 12411 DRM_DEBUG_KMS("cpu_transcoder: %s\n", transcoder_name(pipe_config->cpu_transcoder));
c0b03411
DV
12412 DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
12413 pipe_config->pipe_bpp, pipe_config->dither);
12414 DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
12415 pipe_config->has_pch_encoder,
12416 pipe_config->fdi_lanes,
12417 pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
12418 pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
12419 pipe_config->fdi_m_n.tu);
90a6b7b0 12420 DRM_DEBUG_KMS("dp: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
37a5650b 12421 intel_crtc_has_dp_encoder(pipe_config),
90a6b7b0 12422 pipe_config->lane_count,
eb14cb74
VS
12423 pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
12424 pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
12425 pipe_config->dp_m_n.tu);
b95af8be 12426
90a6b7b0 12427 DRM_DEBUG_KMS("dp: %i, lanes: %i, gmch_m2: %u, gmch_n2: %u, link_m2: %u, link_n2: %u, tu2: %u\n",
37a5650b 12428 intel_crtc_has_dp_encoder(pipe_config),
90a6b7b0 12429 pipe_config->lane_count,
b95af8be
VK
12430 pipe_config->dp_m2_n2.gmch_m,
12431 pipe_config->dp_m2_n2.gmch_n,
12432 pipe_config->dp_m2_n2.link_m,
12433 pipe_config->dp_m2_n2.link_n,
12434 pipe_config->dp_m2_n2.tu);
12435
55072d19
DV
12436 DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
12437 pipe_config->has_audio,
12438 pipe_config->has_infoframe);
12439
c0b03411 12440 DRM_DEBUG_KMS("requested mode:\n");
2d112de7 12441 drm_mode_debug_printmodeline(&pipe_config->base.mode);
c0b03411 12442 DRM_DEBUG_KMS("adjusted mode:\n");
2d112de7
ACO
12443 drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
12444 intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
d71b8d4a 12445 DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
37327abd
VS
12446 DRM_DEBUG_KMS("pipe src size: %dx%d\n",
12447 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
0ec463d3
TU
12448 DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
12449 crtc->num_scalers,
12450 pipe_config->scaler_state.scaler_users,
12451 pipe_config->scaler_state.scaler_id);
c0b03411
DV
12452 DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
12453 pipe_config->gmch_pfit.control,
12454 pipe_config->gmch_pfit.pgm_ratios,
12455 pipe_config->gmch_pfit.lvds_border_bits);
fd4daa9c 12456 DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
c0b03411 12457 pipe_config->pch_pfit.pos,
fd4daa9c
CW
12458 pipe_config->pch_pfit.size,
12459 pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
42db64ef 12460 DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
cf532bb2 12461 DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
6a60cd87 12462
415ff0f6 12463 if (IS_BROXTON(dev)) {
05712c15 12464 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: ebb0: 0x%x, ebb4: 0x%x,"
415ff0f6 12465 "pll0: 0x%x, pll1: 0x%x, pll2: 0x%x, pll3: 0x%x, "
c8453338 12466 "pll6: 0x%x, pll8: 0x%x, pll9: 0x%x, pll10: 0x%x, pcsdw12: 0x%x\n",
415ff0f6
TU
12467 pipe_config->ddi_pll_sel,
12468 pipe_config->dpll_hw_state.ebb0,
05712c15 12469 pipe_config->dpll_hw_state.ebb4,
415ff0f6
TU
12470 pipe_config->dpll_hw_state.pll0,
12471 pipe_config->dpll_hw_state.pll1,
12472 pipe_config->dpll_hw_state.pll2,
12473 pipe_config->dpll_hw_state.pll3,
12474 pipe_config->dpll_hw_state.pll6,
12475 pipe_config->dpll_hw_state.pll8,
05712c15 12476 pipe_config->dpll_hw_state.pll9,
c8453338 12477 pipe_config->dpll_hw_state.pll10,
415ff0f6 12478 pipe_config->dpll_hw_state.pcsdw12);
ef11bdb3 12479 } else if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) {
415ff0f6
TU
12480 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: "
12481 "ctrl1: 0x%x, cfgcr1: 0x%x, cfgcr2: 0x%x\n",
12482 pipe_config->ddi_pll_sel,
12483 pipe_config->dpll_hw_state.ctrl1,
12484 pipe_config->dpll_hw_state.cfgcr1,
12485 pipe_config->dpll_hw_state.cfgcr2);
12486 } else if (HAS_DDI(dev)) {
1260f07e 12487 DRM_DEBUG_KMS("ddi_pll_sel: 0x%x; dpll_hw_state: wrpll: 0x%x spll: 0x%x\n",
415ff0f6 12488 pipe_config->ddi_pll_sel,
00490c22
ML
12489 pipe_config->dpll_hw_state.wrpll,
12490 pipe_config->dpll_hw_state.spll);
415ff0f6
TU
12491 } else {
12492 DRM_DEBUG_KMS("dpll_hw_state: dpll: 0x%x, dpll_md: 0x%x, "
12493 "fp0: 0x%x, fp1: 0x%x\n",
12494 pipe_config->dpll_hw_state.dpll,
12495 pipe_config->dpll_hw_state.dpll_md,
12496 pipe_config->dpll_hw_state.fp0,
12497 pipe_config->dpll_hw_state.fp1);
12498 }
12499
6a60cd87
CK
12500 DRM_DEBUG_KMS("planes on this crtc\n");
12501 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
12502 intel_plane = to_intel_plane(plane);
12503 if (intel_plane->pipe != crtc->pipe)
12504 continue;
12505
12506 state = to_intel_plane_state(plane->state);
12507 fb = state->base.fb;
12508 if (!fb) {
1d577e02
VS
12509 DRM_DEBUG_KMS("[PLANE:%d:%s] disabled, scaler_id = %d\n",
12510 plane->base.id, plane->name, state->scaler_id);
6a60cd87
CK
12511 continue;
12512 }
12513
1d577e02
VS
12514 DRM_DEBUG_KMS("[PLANE:%d:%s] enabled",
12515 plane->base.id, plane->name);
12516 DRM_DEBUG_KMS("\tFB:%d, fb = %ux%u format = %s",
12517 fb->base.id, fb->width, fb->height,
12518 drm_get_format_name(fb->pixel_format));
12519 DRM_DEBUG_KMS("\tscaler:%d src %dx%d+%d+%d dst %dx%d+%d+%d\n",
12520 state->scaler_id,
12521 state->src.x1 >> 16, state->src.y1 >> 16,
12522 drm_rect_width(&state->src) >> 16,
12523 drm_rect_height(&state->src) >> 16,
12524 state->dst.x1, state->dst.y1,
12525 drm_rect_width(&state->dst),
12526 drm_rect_height(&state->dst));
6a60cd87 12527 }
c0b03411
DV
12528}
12529
5448a00d 12530static bool check_digital_port_conflicts(struct drm_atomic_state *state)
00f0b378 12531{
5448a00d 12532 struct drm_device *dev = state->dev;
da3ced29 12533 struct drm_connector *connector;
00f0b378 12534 unsigned int used_ports = 0;
477321e0 12535 unsigned int used_mst_ports = 0;
00f0b378
VS
12536
12537 /*
12538 * Walk the connector list instead of the encoder
12539 * list to detect the problem on ddi platforms
12540 * where there's just one encoder per digital port.
12541 */
0bff4858
VS
12542 drm_for_each_connector(connector, dev) {
12543 struct drm_connector_state *connector_state;
12544 struct intel_encoder *encoder;
12545
12546 connector_state = drm_atomic_get_existing_connector_state(state, connector);
12547 if (!connector_state)
12548 connector_state = connector->state;
12549
5448a00d 12550 if (!connector_state->best_encoder)
00f0b378
VS
12551 continue;
12552
5448a00d
ACO
12553 encoder = to_intel_encoder(connector_state->best_encoder);
12554
12555 WARN_ON(!connector_state->crtc);
00f0b378
VS
12556
12557 switch (encoder->type) {
12558 unsigned int port_mask;
12559 case INTEL_OUTPUT_UNKNOWN:
12560 if (WARN_ON(!HAS_DDI(dev)))
12561 break;
cca0502b 12562 case INTEL_OUTPUT_DP:
00f0b378
VS
12563 case INTEL_OUTPUT_HDMI:
12564 case INTEL_OUTPUT_EDP:
12565 port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
12566
12567 /* the same port mustn't appear more than once */
12568 if (used_ports & port_mask)
12569 return false;
12570
12571 used_ports |= port_mask;
477321e0
VS
12572 break;
12573 case INTEL_OUTPUT_DP_MST:
12574 used_mst_ports |=
12575 1 << enc_to_mst(&encoder->base)->primary->port;
12576 break;
00f0b378
VS
12577 default:
12578 break;
12579 }
12580 }
12581
477321e0
VS
12582 /* can't mix MST and SST/HDMI on the same port */
12583 if (used_ports & used_mst_ports)
12584 return false;
12585
00f0b378
VS
12586 return true;
12587}
12588
83a57153
ACO
12589static void
12590clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
12591{
12592 struct drm_crtc_state tmp_state;
663a3640 12593 struct intel_crtc_scaler_state scaler_state;
4978cc93 12594 struct intel_dpll_hw_state dpll_hw_state;
8106ddbd 12595 struct intel_shared_dpll *shared_dpll;
8504c74c 12596 uint32_t ddi_pll_sel;
c4e2d043 12597 bool force_thru;
83a57153 12598
7546a384
ACO
12599 /* FIXME: before the switch to atomic started, a new pipe_config was
12600 * kzalloc'd. Code that depends on any field being zero should be
12601 * fixed, so that the crtc_state can be safely duplicated. For now,
12602 * only fields that are know to not cause problems are preserved. */
12603
83a57153 12604 tmp_state = crtc_state->base;
663a3640 12605 scaler_state = crtc_state->scaler_state;
4978cc93
ACO
12606 shared_dpll = crtc_state->shared_dpll;
12607 dpll_hw_state = crtc_state->dpll_hw_state;
8504c74c 12608 ddi_pll_sel = crtc_state->ddi_pll_sel;
c4e2d043 12609 force_thru = crtc_state->pch_pfit.force_thru;
4978cc93 12610
83a57153 12611 memset(crtc_state, 0, sizeof *crtc_state);
4978cc93 12612
83a57153 12613 crtc_state->base = tmp_state;
663a3640 12614 crtc_state->scaler_state = scaler_state;
4978cc93
ACO
12615 crtc_state->shared_dpll = shared_dpll;
12616 crtc_state->dpll_hw_state = dpll_hw_state;
8504c74c 12617 crtc_state->ddi_pll_sel = ddi_pll_sel;
c4e2d043 12618 crtc_state->pch_pfit.force_thru = force_thru;
83a57153
ACO
12619}
12620
548ee15b 12621static int
b8cecdf5 12622intel_modeset_pipe_config(struct drm_crtc *crtc,
b359283a 12623 struct intel_crtc_state *pipe_config)
ee7b9f93 12624{
b359283a 12625 struct drm_atomic_state *state = pipe_config->base.state;
7758a113 12626 struct intel_encoder *encoder;
da3ced29 12627 struct drm_connector *connector;
0b901879 12628 struct drm_connector_state *connector_state;
d328c9d7 12629 int base_bpp, ret = -EINVAL;
0b901879 12630 int i;
e29c22c0 12631 bool retry = true;
ee7b9f93 12632
83a57153 12633 clear_intel_crtc_state(pipe_config);
7758a113 12634
e143a21c
DV
12635 pipe_config->cpu_transcoder =
12636 (enum transcoder) to_intel_crtc(crtc)->pipe;
b8cecdf5 12637
2960bc9c
ID
12638 /*
12639 * Sanitize sync polarity flags based on requested ones. If neither
12640 * positive or negative polarity is requested, treat this as meaning
12641 * negative polarity.
12642 */
2d112de7 12643 if (!(pipe_config->base.adjusted_mode.flags &
2960bc9c 12644 (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
2d112de7 12645 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
2960bc9c 12646
2d112de7 12647 if (!(pipe_config->base.adjusted_mode.flags &
2960bc9c 12648 (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
2d112de7 12649 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
2960bc9c 12650
d328c9d7
DV
12651 base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
12652 pipe_config);
12653 if (base_bpp < 0)
4e53c2e0
DV
12654 goto fail;
12655
e41a56be
VS
12656 /*
12657 * Determine the real pipe dimensions. Note that stereo modes can
12658 * increase the actual pipe size due to the frame doubling and
12659 * insertion of additional space for blanks between the frame. This
12660 * is stored in the crtc timings. We use the requested mode to do this
12661 * computation to clearly distinguish it from the adjusted mode, which
12662 * can be changed by the connectors in the below retry loop.
12663 */
2d112de7 12664 drm_crtc_get_hv_timing(&pipe_config->base.mode,
ecb7e16b
GP
12665 &pipe_config->pipe_src_w,
12666 &pipe_config->pipe_src_h);
e41a56be 12667
253c84c8
VS
12668 for_each_connector_in_state(state, connector, connector_state, i) {
12669 if (connector_state->crtc != crtc)
12670 continue;
12671
12672 encoder = to_intel_encoder(connector_state->best_encoder);
12673
e25148d0
VS
12674 if (!check_single_encoder_cloning(state, to_intel_crtc(crtc), encoder)) {
12675 DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
12676 goto fail;
12677 }
12678
253c84c8
VS
12679 /*
12680 * Determine output_types before calling the .compute_config()
12681 * hooks so that the hooks can use this information safely.
12682 */
12683 pipe_config->output_types |= 1 << encoder->type;
12684 }
12685
e29c22c0 12686encoder_retry:
ef1b460d 12687 /* Ensure the port clock defaults are reset when retrying. */
ff9a6750 12688 pipe_config->port_clock = 0;
ef1b460d 12689 pipe_config->pixel_multiplier = 1;
ff9a6750 12690
135c81b8 12691 /* Fill in default crtc timings, allow encoders to overwrite them. */
2d112de7
ACO
12692 drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
12693 CRTC_STEREO_DOUBLE);
135c81b8 12694
7758a113
DV
12695 /* Pass our mode to the connectors and the CRTC to give them a chance to
12696 * adjust it according to limitations or connector properties, and also
12697 * a chance to reject the mode entirely.
47f1c6c9 12698 */
da3ced29 12699 for_each_connector_in_state(state, connector, connector_state, i) {
0b901879 12700 if (connector_state->crtc != crtc)
7758a113 12701 continue;
7ae89233 12702
0b901879
ACO
12703 encoder = to_intel_encoder(connector_state->best_encoder);
12704
efea6e8e
DV
12705 if (!(encoder->compute_config(encoder, pipe_config))) {
12706 DRM_DEBUG_KMS("Encoder config failure\n");
7758a113
DV
12707 goto fail;
12708 }
ee7b9f93 12709 }
47f1c6c9 12710
ff9a6750
DV
12711 /* Set default port clock if not overwritten by the encoder. Needs to be
12712 * done afterwards in case the encoder adjusts the mode. */
12713 if (!pipe_config->port_clock)
2d112de7 12714 pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
241bfc38 12715 * pipe_config->pixel_multiplier;
ff9a6750 12716
a43f6e0f 12717 ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
e29c22c0 12718 if (ret < 0) {
7758a113
DV
12719 DRM_DEBUG_KMS("CRTC fixup failed\n");
12720 goto fail;
ee7b9f93 12721 }
e29c22c0
DV
12722
12723 if (ret == RETRY) {
12724 if (WARN(!retry, "loop in pipe configuration computation\n")) {
12725 ret = -EINVAL;
12726 goto fail;
12727 }
12728
12729 DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
12730 retry = false;
12731 goto encoder_retry;
12732 }
12733
e8fa4270
DV
12734 /* Dithering seems to not pass-through bits correctly when it should, so
12735 * only enable it on 6bpc panels. */
12736 pipe_config->dither = pipe_config->pipe_bpp == 6*3;
62f0ace5 12737 DRM_DEBUG_KMS("hw max bpp: %i, pipe bpp: %i, dithering: %i\n",
d328c9d7 12738 base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
4e53c2e0 12739
7758a113 12740fail:
548ee15b 12741 return ret;
ee7b9f93 12742}
47f1c6c9 12743
ea9d758d 12744static void
4740b0f2 12745intel_modeset_update_crtc_state(struct drm_atomic_state *state)
ea9d758d 12746{
0a9ab303
ACO
12747 struct drm_crtc *crtc;
12748 struct drm_crtc_state *crtc_state;
8a75d157 12749 int i;
ea9d758d 12750
7668851f 12751 /* Double check state. */
8a75d157 12752 for_each_crtc_in_state(state, crtc, crtc_state, i) {
3cb480bc 12753 to_intel_crtc(crtc)->config = to_intel_crtc_state(crtc->state);
fc467a22
ML
12754
12755 /* Update hwmode for vblank functions */
12756 if (crtc->state->active)
12757 crtc->hwmode = crtc->state->adjusted_mode;
12758 else
12759 crtc->hwmode.crtc_clock = 0;
61067a5e
ML
12760
12761 /*
12762 * Update legacy state to satisfy fbc code. This can
12763 * be removed when fbc uses the atomic state.
12764 */
12765 if (drm_atomic_get_existing_plane_state(state, crtc->primary)) {
12766 struct drm_plane_state *plane_state = crtc->primary->state;
12767
12768 crtc->primary->fb = plane_state->fb;
12769 crtc->x = plane_state->src_x >> 16;
12770 crtc->y = plane_state->src_y >> 16;
12771 }
ea9d758d 12772 }
ea9d758d
DV
12773}
12774
3bd26263 12775static bool intel_fuzzy_clock_check(int clock1, int clock2)
f1f644dc 12776{
3bd26263 12777 int diff;
f1f644dc
JB
12778
12779 if (clock1 == clock2)
12780 return true;
12781
12782 if (!clock1 || !clock2)
12783 return false;
12784
12785 diff = abs(clock1 - clock2);
12786
12787 if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
12788 return true;
12789
12790 return false;
12791}
12792
25c5b266
DV
12793#define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
12794 list_for_each_entry((intel_crtc), \
12795 &(dev)->mode_config.crtc_list, \
12796 base.head) \
95150bdf 12797 for_each_if (mask & (1 <<(intel_crtc)->pipe))
25c5b266 12798
cfb23ed6
ML
12799static bool
12800intel_compare_m_n(unsigned int m, unsigned int n,
12801 unsigned int m2, unsigned int n2,
12802 bool exact)
12803{
12804 if (m == m2 && n == n2)
12805 return true;
12806
12807 if (exact || !m || !n || !m2 || !n2)
12808 return false;
12809
12810 BUILD_BUG_ON(DATA_LINK_M_N_MASK > INT_MAX);
12811
31d10b57
ML
12812 if (n > n2) {
12813 while (n > n2) {
cfb23ed6
ML
12814 m2 <<= 1;
12815 n2 <<= 1;
12816 }
31d10b57
ML
12817 } else if (n < n2) {
12818 while (n < n2) {
cfb23ed6
ML
12819 m <<= 1;
12820 n <<= 1;
12821 }
12822 }
12823
31d10b57
ML
12824 if (n != n2)
12825 return false;
12826
12827 return intel_fuzzy_clock_check(m, m2);
cfb23ed6
ML
12828}
12829
12830static bool
12831intel_compare_link_m_n(const struct intel_link_m_n *m_n,
12832 struct intel_link_m_n *m2_n2,
12833 bool adjust)
12834{
12835 if (m_n->tu == m2_n2->tu &&
12836 intel_compare_m_n(m_n->gmch_m, m_n->gmch_n,
12837 m2_n2->gmch_m, m2_n2->gmch_n, !adjust) &&
12838 intel_compare_m_n(m_n->link_m, m_n->link_n,
12839 m2_n2->link_m, m2_n2->link_n, !adjust)) {
12840 if (adjust)
12841 *m2_n2 = *m_n;
12842
12843 return true;
12844 }
12845
12846 return false;
12847}
12848
0e8ffe1b 12849static bool
2fa2fe9a 12850intel_pipe_config_compare(struct drm_device *dev,
5cec258b 12851 struct intel_crtc_state *current_config,
cfb23ed6
ML
12852 struct intel_crtc_state *pipe_config,
12853 bool adjust)
0e8ffe1b 12854{
cfb23ed6
ML
12855 bool ret = true;
12856
12857#define INTEL_ERR_OR_DBG_KMS(fmt, ...) \
12858 do { \
12859 if (!adjust) \
12860 DRM_ERROR(fmt, ##__VA_ARGS__); \
12861 else \
12862 DRM_DEBUG_KMS(fmt, ##__VA_ARGS__); \
12863 } while (0)
12864
66e985c0
DV
12865#define PIPE_CONF_CHECK_X(name) \
12866 if (current_config->name != pipe_config->name) { \
cfb23ed6 12867 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
66e985c0
DV
12868 "(expected 0x%08x, found 0x%08x)\n", \
12869 current_config->name, \
12870 pipe_config->name); \
cfb23ed6 12871 ret = false; \
66e985c0
DV
12872 }
12873
08a24034
DV
12874#define PIPE_CONF_CHECK_I(name) \
12875 if (current_config->name != pipe_config->name) { \
cfb23ed6 12876 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
08a24034
DV
12877 "(expected %i, found %i)\n", \
12878 current_config->name, \
12879 pipe_config->name); \
cfb23ed6
ML
12880 ret = false; \
12881 }
12882
8106ddbd
ACO
12883#define PIPE_CONF_CHECK_P(name) \
12884 if (current_config->name != pipe_config->name) { \
12885 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
12886 "(expected %p, found %p)\n", \
12887 current_config->name, \
12888 pipe_config->name); \
12889 ret = false; \
12890 }
12891
cfb23ed6
ML
12892#define PIPE_CONF_CHECK_M_N(name) \
12893 if (!intel_compare_link_m_n(&current_config->name, \
12894 &pipe_config->name,\
12895 adjust)) { \
12896 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
12897 "(expected tu %i gmch %i/%i link %i/%i, " \
12898 "found tu %i, gmch %i/%i link %i/%i)\n", \
12899 current_config->name.tu, \
12900 current_config->name.gmch_m, \
12901 current_config->name.gmch_n, \
12902 current_config->name.link_m, \
12903 current_config->name.link_n, \
12904 pipe_config->name.tu, \
12905 pipe_config->name.gmch_m, \
12906 pipe_config->name.gmch_n, \
12907 pipe_config->name.link_m, \
12908 pipe_config->name.link_n); \
12909 ret = false; \
12910 }
12911
55c561a7
DV
12912/* This is required for BDW+ where there is only one set of registers for
12913 * switching between high and low RR.
12914 * This macro can be used whenever a comparison has to be made between one
12915 * hw state and multiple sw state variables.
12916 */
cfb23ed6
ML
12917#define PIPE_CONF_CHECK_M_N_ALT(name, alt_name) \
12918 if (!intel_compare_link_m_n(&current_config->name, \
12919 &pipe_config->name, adjust) && \
12920 !intel_compare_link_m_n(&current_config->alt_name, \
12921 &pipe_config->name, adjust)) { \
12922 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
12923 "(expected tu %i gmch %i/%i link %i/%i, " \
12924 "or tu %i gmch %i/%i link %i/%i, " \
12925 "found tu %i, gmch %i/%i link %i/%i)\n", \
12926 current_config->name.tu, \
12927 current_config->name.gmch_m, \
12928 current_config->name.gmch_n, \
12929 current_config->name.link_m, \
12930 current_config->name.link_n, \
12931 current_config->alt_name.tu, \
12932 current_config->alt_name.gmch_m, \
12933 current_config->alt_name.gmch_n, \
12934 current_config->alt_name.link_m, \
12935 current_config->alt_name.link_n, \
12936 pipe_config->name.tu, \
12937 pipe_config->name.gmch_m, \
12938 pipe_config->name.gmch_n, \
12939 pipe_config->name.link_m, \
12940 pipe_config->name.link_n); \
12941 ret = false; \
88adfff1
DV
12942 }
12943
1bd1bd80
DV
12944#define PIPE_CONF_CHECK_FLAGS(name, mask) \
12945 if ((current_config->name ^ pipe_config->name) & (mask)) { \
cfb23ed6 12946 INTEL_ERR_OR_DBG_KMS("mismatch in " #name "(" #mask ") " \
1bd1bd80
DV
12947 "(expected %i, found %i)\n", \
12948 current_config->name & (mask), \
12949 pipe_config->name & (mask)); \
cfb23ed6 12950 ret = false; \
1bd1bd80
DV
12951 }
12952
5e550656
VS
12953#define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
12954 if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
cfb23ed6 12955 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
5e550656
VS
12956 "(expected %i, found %i)\n", \
12957 current_config->name, \
12958 pipe_config->name); \
cfb23ed6 12959 ret = false; \
5e550656
VS
12960 }
12961
bb760063
DV
12962#define PIPE_CONF_QUIRK(quirk) \
12963 ((current_config->quirks | pipe_config->quirks) & (quirk))
12964
eccb140b
DV
12965 PIPE_CONF_CHECK_I(cpu_transcoder);
12966
08a24034
DV
12967 PIPE_CONF_CHECK_I(has_pch_encoder);
12968 PIPE_CONF_CHECK_I(fdi_lanes);
cfb23ed6 12969 PIPE_CONF_CHECK_M_N(fdi_m_n);
08a24034 12970
90a6b7b0 12971 PIPE_CONF_CHECK_I(lane_count);
95a7a2ae 12972 PIPE_CONF_CHECK_X(lane_lat_optim_mask);
b95af8be
VK
12973
12974 if (INTEL_INFO(dev)->gen < 8) {
cfb23ed6
ML
12975 PIPE_CONF_CHECK_M_N(dp_m_n);
12976
cfb23ed6
ML
12977 if (current_config->has_drrs)
12978 PIPE_CONF_CHECK_M_N(dp_m2_n2);
12979 } else
12980 PIPE_CONF_CHECK_M_N_ALT(dp_m_n, dp_m2_n2);
eb14cb74 12981
253c84c8 12982 PIPE_CONF_CHECK_X(output_types);
a65347ba 12983
2d112de7
ACO
12984 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
12985 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
12986 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
12987 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
12988 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
12989 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
1bd1bd80 12990
2d112de7
ACO
12991 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
12992 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
12993 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
12994 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
12995 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
12996 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
1bd1bd80 12997
c93f54cf 12998 PIPE_CONF_CHECK_I(pixel_multiplier);
6897b4b5 12999 PIPE_CONF_CHECK_I(has_hdmi_sink);
b5a9fa09 13000 if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
666a4537 13001 IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev))
b5a9fa09 13002 PIPE_CONF_CHECK_I(limited_color_range);
e43823ec 13003 PIPE_CONF_CHECK_I(has_infoframe);
6c49f241 13004
9ed109a7
DV
13005 PIPE_CONF_CHECK_I(has_audio);
13006
2d112de7 13007 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
1bd1bd80
DV
13008 DRM_MODE_FLAG_INTERLACE);
13009
bb760063 13010 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
2d112de7 13011 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063 13012 DRM_MODE_FLAG_PHSYNC);
2d112de7 13013 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063 13014 DRM_MODE_FLAG_NHSYNC);
2d112de7 13015 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063 13016 DRM_MODE_FLAG_PVSYNC);
2d112de7 13017 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063
DV
13018 DRM_MODE_FLAG_NVSYNC);
13019 }
045ac3b5 13020
333b8ca8 13021 PIPE_CONF_CHECK_X(gmch_pfit.control);
e2ff2d4a
DV
13022 /* pfit ratios are autocomputed by the hw on gen4+ */
13023 if (INTEL_INFO(dev)->gen < 4)
7f7d8dd6 13024 PIPE_CONF_CHECK_X(gmch_pfit.pgm_ratios);
333b8ca8 13025 PIPE_CONF_CHECK_X(gmch_pfit.lvds_border_bits);
9953599b 13026
bfd16b2a
ML
13027 if (!adjust) {
13028 PIPE_CONF_CHECK_I(pipe_src_w);
13029 PIPE_CONF_CHECK_I(pipe_src_h);
13030
13031 PIPE_CONF_CHECK_I(pch_pfit.enabled);
13032 if (current_config->pch_pfit.enabled) {
13033 PIPE_CONF_CHECK_X(pch_pfit.pos);
13034 PIPE_CONF_CHECK_X(pch_pfit.size);
13035 }
2fa2fe9a 13036
7aefe2b5
ML
13037 PIPE_CONF_CHECK_I(scaler_state.scaler_id);
13038 }
a1b2278e 13039
e59150dc
JB
13040 /* BDW+ don't expose a synchronous way to read the state */
13041 if (IS_HASWELL(dev))
13042 PIPE_CONF_CHECK_I(ips_enabled);
42db64ef 13043
282740f7
VS
13044 PIPE_CONF_CHECK_I(double_wide);
13045
26804afd
DV
13046 PIPE_CONF_CHECK_X(ddi_pll_sel);
13047
8106ddbd 13048 PIPE_CONF_CHECK_P(shared_dpll);
66e985c0 13049 PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
8bcc2795 13050 PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
66e985c0
DV
13051 PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
13052 PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
d452c5b6 13053 PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
00490c22 13054 PIPE_CONF_CHECK_X(dpll_hw_state.spll);
3f4cd19f
DL
13055 PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
13056 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
13057 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
c0d43d62 13058
47eacbab
VS
13059 PIPE_CONF_CHECK_X(dsi_pll.ctrl);
13060 PIPE_CONF_CHECK_X(dsi_pll.div);
13061
42571aef
VS
13062 if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
13063 PIPE_CONF_CHECK_I(pipe_bpp);
13064
2d112de7 13065 PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
a9a7e98a 13066 PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
5e550656 13067
66e985c0 13068#undef PIPE_CONF_CHECK_X
08a24034 13069#undef PIPE_CONF_CHECK_I
8106ddbd 13070#undef PIPE_CONF_CHECK_P
1bd1bd80 13071#undef PIPE_CONF_CHECK_FLAGS
5e550656 13072#undef PIPE_CONF_CHECK_CLOCK_FUZZY
bb760063 13073#undef PIPE_CONF_QUIRK
cfb23ed6 13074#undef INTEL_ERR_OR_DBG_KMS
88adfff1 13075
cfb23ed6 13076 return ret;
0e8ffe1b
DV
13077}
13078
e3b247da
VS
13079static void intel_pipe_config_sanity_check(struct drm_i915_private *dev_priv,
13080 const struct intel_crtc_state *pipe_config)
13081{
13082 if (pipe_config->has_pch_encoder) {
21a727b3 13083 int fdi_dotclock = intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
e3b247da
VS
13084 &pipe_config->fdi_m_n);
13085 int dotclock = pipe_config->base.adjusted_mode.crtc_clock;
13086
13087 /*
13088 * FDI already provided one idea for the dotclock.
13089 * Yell if the encoder disagrees.
13090 */
13091 WARN(!intel_fuzzy_clock_check(fdi_dotclock, dotclock),
13092 "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
13093 fdi_dotclock, dotclock);
13094 }
13095}
13096
c0ead703
ML
13097static void verify_wm_state(struct drm_crtc *crtc,
13098 struct drm_crtc_state *new_state)
08db6652 13099{
e7c84544 13100 struct drm_device *dev = crtc->dev;
fac5e23e 13101 struct drm_i915_private *dev_priv = to_i915(dev);
08db6652 13102 struct skl_ddb_allocation hw_ddb, *sw_ddb;
e7c84544
ML
13103 struct skl_ddb_entry *hw_entry, *sw_entry;
13104 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13105 const enum pipe pipe = intel_crtc->pipe;
08db6652
DL
13106 int plane;
13107
e7c84544 13108 if (INTEL_INFO(dev)->gen < 9 || !new_state->active)
08db6652
DL
13109 return;
13110
13111 skl_ddb_get_hw_state(dev_priv, &hw_ddb);
13112 sw_ddb = &dev_priv->wm.skl_hw.ddb;
13113
e7c84544
ML
13114 /* planes */
13115 for_each_plane(dev_priv, pipe, plane) {
13116 hw_entry = &hw_ddb.plane[pipe][plane];
13117 sw_entry = &sw_ddb->plane[pipe][plane];
08db6652 13118
e7c84544 13119 if (skl_ddb_entry_equal(hw_entry, sw_entry))
08db6652
DL
13120 continue;
13121
e7c84544
ML
13122 DRM_ERROR("mismatch in DDB state pipe %c plane %d "
13123 "(expected (%u,%u), found (%u,%u))\n",
13124 pipe_name(pipe), plane + 1,
13125 sw_entry->start, sw_entry->end,
13126 hw_entry->start, hw_entry->end);
13127 }
08db6652 13128
e7c84544
ML
13129 /* cursor */
13130 hw_entry = &hw_ddb.plane[pipe][PLANE_CURSOR];
13131 sw_entry = &sw_ddb->plane[pipe][PLANE_CURSOR];
08db6652 13132
e7c84544 13133 if (!skl_ddb_entry_equal(hw_entry, sw_entry)) {
08db6652
DL
13134 DRM_ERROR("mismatch in DDB state pipe %c cursor "
13135 "(expected (%u,%u), found (%u,%u))\n",
13136 pipe_name(pipe),
13137 sw_entry->start, sw_entry->end,
13138 hw_entry->start, hw_entry->end);
13139 }
13140}
13141
91d1b4bd 13142static void
c0ead703 13143verify_connector_state(struct drm_device *dev, struct drm_crtc *crtc)
8af6cf88 13144{
35dd3c64 13145 struct drm_connector *connector;
8af6cf88 13146
e7c84544 13147 drm_for_each_connector(connector, dev) {
35dd3c64
ML
13148 struct drm_encoder *encoder = connector->encoder;
13149 struct drm_connector_state *state = connector->state;
ad3c558f 13150
e7c84544
ML
13151 if (state->crtc != crtc)
13152 continue;
13153
5a21b665 13154 intel_connector_verify_state(to_intel_connector(connector));
8af6cf88 13155
ad3c558f 13156 I915_STATE_WARN(state->best_encoder != encoder,
35dd3c64 13157 "connector's atomic encoder doesn't match legacy encoder\n");
8af6cf88 13158 }
91d1b4bd
DV
13159}
13160
13161static void
c0ead703 13162verify_encoder_state(struct drm_device *dev)
91d1b4bd
DV
13163{
13164 struct intel_encoder *encoder;
13165 struct intel_connector *connector;
8af6cf88 13166
b2784e15 13167 for_each_intel_encoder(dev, encoder) {
8af6cf88 13168 bool enabled = false;
4d20cd86 13169 enum pipe pipe;
8af6cf88
DV
13170
13171 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
13172 encoder->base.base.id,
8e329a03 13173 encoder->base.name);
8af6cf88 13174
3a3371ff 13175 for_each_intel_connector(dev, connector) {
4d20cd86 13176 if (connector->base.state->best_encoder != &encoder->base)
8af6cf88
DV
13177 continue;
13178 enabled = true;
ad3c558f
ML
13179
13180 I915_STATE_WARN(connector->base.state->crtc !=
13181 encoder->base.crtc,
13182 "connector's crtc doesn't match encoder crtc\n");
8af6cf88 13183 }
0e32b39c 13184
e2c719b7 13185 I915_STATE_WARN(!!encoder->base.crtc != enabled,
8af6cf88
DV
13186 "encoder's enabled state mismatch "
13187 "(expected %i, found %i)\n",
13188 !!encoder->base.crtc, enabled);
7c60d198
ML
13189
13190 if (!encoder->base.crtc) {
4d20cd86 13191 bool active;
7c60d198 13192
4d20cd86
ML
13193 active = encoder->get_hw_state(encoder, &pipe);
13194 I915_STATE_WARN(active,
13195 "encoder detached but still enabled on pipe %c.\n",
13196 pipe_name(pipe));
7c60d198 13197 }
8af6cf88 13198 }
91d1b4bd
DV
13199}
13200
13201static void
c0ead703
ML
13202verify_crtc_state(struct drm_crtc *crtc,
13203 struct drm_crtc_state *old_crtc_state,
13204 struct drm_crtc_state *new_crtc_state)
91d1b4bd 13205{
e7c84544 13206 struct drm_device *dev = crtc->dev;
fac5e23e 13207 struct drm_i915_private *dev_priv = to_i915(dev);
91d1b4bd 13208 struct intel_encoder *encoder;
e7c84544
ML
13209 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13210 struct intel_crtc_state *pipe_config, *sw_config;
13211 struct drm_atomic_state *old_state;
13212 bool active;
045ac3b5 13213
e7c84544 13214 old_state = old_crtc_state->state;
ec2dc6a0 13215 __drm_atomic_helper_crtc_destroy_state(old_crtc_state);
e7c84544
ML
13216 pipe_config = to_intel_crtc_state(old_crtc_state);
13217 memset(pipe_config, 0, sizeof(*pipe_config));
13218 pipe_config->base.crtc = crtc;
13219 pipe_config->base.state = old_state;
8af6cf88 13220
78108b7c 13221 DRM_DEBUG_KMS("[CRTC:%d:%s]\n", crtc->base.id, crtc->name);
8af6cf88 13222
e7c84544 13223 active = dev_priv->display.get_pipe_config(intel_crtc, pipe_config);
d62cf62a 13224
e7c84544
ML
13225 /* hw state is inconsistent with the pipe quirk */
13226 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
13227 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
13228 active = new_crtc_state->active;
6c49f241 13229
e7c84544
ML
13230 I915_STATE_WARN(new_crtc_state->active != active,
13231 "crtc active state doesn't match with hw state "
13232 "(expected %i, found %i)\n", new_crtc_state->active, active);
0e8ffe1b 13233
e7c84544
ML
13234 I915_STATE_WARN(intel_crtc->active != new_crtc_state->active,
13235 "transitional active state does not match atomic hw state "
13236 "(expected %i, found %i)\n", new_crtc_state->active, intel_crtc->active);
4d20cd86 13237
e7c84544
ML
13238 for_each_encoder_on_crtc(dev, crtc, encoder) {
13239 enum pipe pipe;
4d20cd86 13240
e7c84544
ML
13241 active = encoder->get_hw_state(encoder, &pipe);
13242 I915_STATE_WARN(active != new_crtc_state->active,
13243 "[ENCODER:%i] active %i with crtc active %i\n",
13244 encoder->base.base.id, active, new_crtc_state->active);
4d20cd86 13245
e7c84544
ML
13246 I915_STATE_WARN(active && intel_crtc->pipe != pipe,
13247 "Encoder connected to wrong pipe %c\n",
13248 pipe_name(pipe));
4d20cd86 13249
253c84c8
VS
13250 if (active) {
13251 pipe_config->output_types |= 1 << encoder->type;
e7c84544 13252 encoder->get_config(encoder, pipe_config);
253c84c8 13253 }
e7c84544 13254 }
53d9f4e9 13255
e7c84544
ML
13256 if (!new_crtc_state->active)
13257 return;
cfb23ed6 13258
e7c84544 13259 intel_pipe_config_sanity_check(dev_priv, pipe_config);
e3b247da 13260
e7c84544
ML
13261 sw_config = to_intel_crtc_state(crtc->state);
13262 if (!intel_pipe_config_compare(dev, sw_config,
13263 pipe_config, false)) {
13264 I915_STATE_WARN(1, "pipe state doesn't match!\n");
13265 intel_dump_pipe_config(intel_crtc, pipe_config,
13266 "[hw state]");
13267 intel_dump_pipe_config(intel_crtc, sw_config,
13268 "[sw state]");
8af6cf88
DV
13269 }
13270}
13271
91d1b4bd 13272static void
c0ead703
ML
13273verify_single_dpll_state(struct drm_i915_private *dev_priv,
13274 struct intel_shared_dpll *pll,
13275 struct drm_crtc *crtc,
13276 struct drm_crtc_state *new_state)
91d1b4bd 13277{
91d1b4bd 13278 struct intel_dpll_hw_state dpll_hw_state;
e7c84544
ML
13279 unsigned crtc_mask;
13280 bool active;
5358901f 13281
e7c84544 13282 memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
5358901f 13283
e7c84544 13284 DRM_DEBUG_KMS("%s\n", pll->name);
5358901f 13285
e7c84544 13286 active = pll->funcs.get_hw_state(dev_priv, pll, &dpll_hw_state);
5358901f 13287
e7c84544
ML
13288 if (!(pll->flags & INTEL_DPLL_ALWAYS_ON)) {
13289 I915_STATE_WARN(!pll->on && pll->active_mask,
13290 "pll in active use but not on in sw tracking\n");
13291 I915_STATE_WARN(pll->on && !pll->active_mask,
13292 "pll is on but not used by any active crtc\n");
13293 I915_STATE_WARN(pll->on != active,
13294 "pll on state mismatch (expected %i, found %i)\n",
13295 pll->on, active);
13296 }
5358901f 13297
e7c84544 13298 if (!crtc) {
2dd66ebd 13299 I915_STATE_WARN(pll->active_mask & ~pll->config.crtc_mask,
e7c84544
ML
13300 "more active pll users than references: %x vs %x\n",
13301 pll->active_mask, pll->config.crtc_mask);
5358901f 13302
e7c84544
ML
13303 return;
13304 }
13305
13306 crtc_mask = 1 << drm_crtc_index(crtc);
13307
13308 if (new_state->active)
13309 I915_STATE_WARN(!(pll->active_mask & crtc_mask),
13310 "pll active mismatch (expected pipe %c in active mask 0x%02x)\n",
13311 pipe_name(drm_crtc_index(crtc)), pll->active_mask);
13312 else
13313 I915_STATE_WARN(pll->active_mask & crtc_mask,
13314 "pll active mismatch (didn't expect pipe %c in active mask 0x%02x)\n",
13315 pipe_name(drm_crtc_index(crtc)), pll->active_mask);
2dd66ebd 13316
e7c84544
ML
13317 I915_STATE_WARN(!(pll->config.crtc_mask & crtc_mask),
13318 "pll enabled crtcs mismatch (expected 0x%x in 0x%02x)\n",
13319 crtc_mask, pll->config.crtc_mask);
66e985c0 13320
e7c84544
ML
13321 I915_STATE_WARN(pll->on && memcmp(&pll->config.hw_state,
13322 &dpll_hw_state,
13323 sizeof(dpll_hw_state)),
13324 "pll hw state mismatch\n");
13325}
13326
13327static void
c0ead703
ML
13328verify_shared_dpll_state(struct drm_device *dev, struct drm_crtc *crtc,
13329 struct drm_crtc_state *old_crtc_state,
13330 struct drm_crtc_state *new_crtc_state)
e7c84544 13331{
fac5e23e 13332 struct drm_i915_private *dev_priv = to_i915(dev);
e7c84544
ML
13333 struct intel_crtc_state *old_state = to_intel_crtc_state(old_crtc_state);
13334 struct intel_crtc_state *new_state = to_intel_crtc_state(new_crtc_state);
13335
13336 if (new_state->shared_dpll)
c0ead703 13337 verify_single_dpll_state(dev_priv, new_state->shared_dpll, crtc, new_crtc_state);
e7c84544
ML
13338
13339 if (old_state->shared_dpll &&
13340 old_state->shared_dpll != new_state->shared_dpll) {
13341 unsigned crtc_mask = 1 << drm_crtc_index(crtc);
13342 struct intel_shared_dpll *pll = old_state->shared_dpll;
13343
13344 I915_STATE_WARN(pll->active_mask & crtc_mask,
13345 "pll active mismatch (didn't expect pipe %c in active mask)\n",
13346 pipe_name(drm_crtc_index(crtc)));
13347 I915_STATE_WARN(pll->config.crtc_mask & crtc_mask,
13348 "pll enabled crtcs mismatch (found %x in enabled mask)\n",
13349 pipe_name(drm_crtc_index(crtc)));
5358901f 13350 }
8af6cf88
DV
13351}
13352
e7c84544 13353static void
c0ead703 13354intel_modeset_verify_crtc(struct drm_crtc *crtc,
e7c84544
ML
13355 struct drm_crtc_state *old_state,
13356 struct drm_crtc_state *new_state)
13357{
5a21b665
DV
13358 if (!needs_modeset(new_state) &&
13359 !to_intel_crtc_state(new_state)->update_pipe)
13360 return;
13361
c0ead703 13362 verify_wm_state(crtc, new_state);
5a21b665 13363 verify_connector_state(crtc->dev, crtc);
c0ead703
ML
13364 verify_crtc_state(crtc, old_state, new_state);
13365 verify_shared_dpll_state(crtc->dev, crtc, old_state, new_state);
e7c84544
ML
13366}
13367
13368static void
c0ead703 13369verify_disabled_dpll_state(struct drm_device *dev)
e7c84544 13370{
fac5e23e 13371 struct drm_i915_private *dev_priv = to_i915(dev);
e7c84544
ML
13372 int i;
13373
13374 for (i = 0; i < dev_priv->num_shared_dpll; i++)
c0ead703 13375 verify_single_dpll_state(dev_priv, &dev_priv->shared_dplls[i], NULL, NULL);
e7c84544
ML
13376}
13377
13378static void
c0ead703 13379intel_modeset_verify_disabled(struct drm_device *dev)
e7c84544 13380{
c0ead703
ML
13381 verify_encoder_state(dev);
13382 verify_connector_state(dev, NULL);
13383 verify_disabled_dpll_state(dev);
e7c84544
ML
13384}
13385
80715b2f
VS
13386static void update_scanline_offset(struct intel_crtc *crtc)
13387{
13388 struct drm_device *dev = crtc->base.dev;
13389
13390 /*
13391 * The scanline counter increments at the leading edge of hsync.
13392 *
13393 * On most platforms it starts counting from vtotal-1 on the
13394 * first active line. That means the scanline counter value is
13395 * always one less than what we would expect. Ie. just after
13396 * start of vblank, which also occurs at start of hsync (on the
13397 * last active line), the scanline counter will read vblank_start-1.
13398 *
13399 * On gen2 the scanline counter starts counting from 1 instead
13400 * of vtotal-1, so we have to subtract one (or rather add vtotal-1
13401 * to keep the value positive), instead of adding one.
13402 *
13403 * On HSW+ the behaviour of the scanline counter depends on the output
13404 * type. For DP ports it behaves like most other platforms, but on HDMI
13405 * there's an extra 1 line difference. So we need to add two instead of
13406 * one to the value.
13407 */
13408 if (IS_GEN2(dev)) {
124abe07 13409 const struct drm_display_mode *adjusted_mode = &crtc->config->base.adjusted_mode;
80715b2f
VS
13410 int vtotal;
13411
124abe07
VS
13412 vtotal = adjusted_mode->crtc_vtotal;
13413 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
80715b2f
VS
13414 vtotal /= 2;
13415
13416 crtc->scanline_offset = vtotal - 1;
13417 } else if (HAS_DDI(dev) &&
2d84d2b3 13418 intel_crtc_has_type(crtc->config, INTEL_OUTPUT_HDMI)) {
80715b2f
VS
13419 crtc->scanline_offset = 2;
13420 } else
13421 crtc->scanline_offset = 1;
13422}
13423
ad421372 13424static void intel_modeset_clear_plls(struct drm_atomic_state *state)
ed6739ef 13425{
225da59b 13426 struct drm_device *dev = state->dev;
ed6739ef 13427 struct drm_i915_private *dev_priv = to_i915(dev);
ad421372 13428 struct intel_shared_dpll_config *shared_dpll = NULL;
0a9ab303
ACO
13429 struct drm_crtc *crtc;
13430 struct drm_crtc_state *crtc_state;
0a9ab303 13431 int i;
ed6739ef
ACO
13432
13433 if (!dev_priv->display.crtc_compute_clock)
ad421372 13434 return;
ed6739ef 13435
0a9ab303 13436 for_each_crtc_in_state(state, crtc, crtc_state, i) {
fb1a38a9 13437 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8106ddbd
ACO
13438 struct intel_shared_dpll *old_dpll =
13439 to_intel_crtc_state(crtc->state)->shared_dpll;
0a9ab303 13440
fb1a38a9 13441 if (!needs_modeset(crtc_state))
225da59b
ACO
13442 continue;
13443
8106ddbd 13444 to_intel_crtc_state(crtc_state)->shared_dpll = NULL;
fb1a38a9 13445
8106ddbd 13446 if (!old_dpll)
fb1a38a9 13447 continue;
0a9ab303 13448
ad421372
ML
13449 if (!shared_dpll)
13450 shared_dpll = intel_atomic_get_shared_dpll_state(state);
ed6739ef 13451
8106ddbd 13452 intel_shared_dpll_config_put(shared_dpll, old_dpll, intel_crtc);
ad421372 13453 }
ed6739ef
ACO
13454}
13455
99d736a2
ML
13456/*
13457 * This implements the workaround described in the "notes" section of the mode
13458 * set sequence documentation. When going from no pipes or single pipe to
13459 * multiple pipes, and planes are enabled after the pipe, we need to wait at
13460 * least 2 vblanks on the first pipe before enabling planes on the second pipe.
13461 */
13462static int haswell_mode_set_planes_workaround(struct drm_atomic_state *state)
13463{
13464 struct drm_crtc_state *crtc_state;
13465 struct intel_crtc *intel_crtc;
13466 struct drm_crtc *crtc;
13467 struct intel_crtc_state *first_crtc_state = NULL;
13468 struct intel_crtc_state *other_crtc_state = NULL;
13469 enum pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
13470 int i;
13471
13472 /* look at all crtc's that are going to be enabled in during modeset */
13473 for_each_crtc_in_state(state, crtc, crtc_state, i) {
13474 intel_crtc = to_intel_crtc(crtc);
13475
13476 if (!crtc_state->active || !needs_modeset(crtc_state))
13477 continue;
13478
13479 if (first_crtc_state) {
13480 other_crtc_state = to_intel_crtc_state(crtc_state);
13481 break;
13482 } else {
13483 first_crtc_state = to_intel_crtc_state(crtc_state);
13484 first_pipe = intel_crtc->pipe;
13485 }
13486 }
13487
13488 /* No workaround needed? */
13489 if (!first_crtc_state)
13490 return 0;
13491
13492 /* w/a possibly needed, check how many crtc's are already enabled. */
13493 for_each_intel_crtc(state->dev, intel_crtc) {
13494 struct intel_crtc_state *pipe_config;
13495
13496 pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
13497 if (IS_ERR(pipe_config))
13498 return PTR_ERR(pipe_config);
13499
13500 pipe_config->hsw_workaround_pipe = INVALID_PIPE;
13501
13502 if (!pipe_config->base.active ||
13503 needs_modeset(&pipe_config->base))
13504 continue;
13505
13506 /* 2 or more enabled crtcs means no need for w/a */
13507 if (enabled_pipe != INVALID_PIPE)
13508 return 0;
13509
13510 enabled_pipe = intel_crtc->pipe;
13511 }
13512
13513 if (enabled_pipe != INVALID_PIPE)
13514 first_crtc_state->hsw_workaround_pipe = enabled_pipe;
13515 else if (other_crtc_state)
13516 other_crtc_state->hsw_workaround_pipe = first_pipe;
13517
13518 return 0;
13519}
13520
27c329ed
ML
13521static int intel_modeset_all_pipes(struct drm_atomic_state *state)
13522{
13523 struct drm_crtc *crtc;
13524 struct drm_crtc_state *crtc_state;
13525 int ret = 0;
13526
13527 /* add all active pipes to the state */
13528 for_each_crtc(state->dev, crtc) {
13529 crtc_state = drm_atomic_get_crtc_state(state, crtc);
13530 if (IS_ERR(crtc_state))
13531 return PTR_ERR(crtc_state);
13532
13533 if (!crtc_state->active || needs_modeset(crtc_state))
13534 continue;
13535
13536 crtc_state->mode_changed = true;
13537
13538 ret = drm_atomic_add_affected_connectors(state, crtc);
13539 if (ret)
13540 break;
13541
13542 ret = drm_atomic_add_affected_planes(state, crtc);
13543 if (ret)
13544 break;
13545 }
13546
13547 return ret;
13548}
13549
c347a676 13550static int intel_modeset_checks(struct drm_atomic_state *state)
054518dd 13551{
565602d7 13552 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
fac5e23e 13553 struct drm_i915_private *dev_priv = to_i915(state->dev);
565602d7
ML
13554 struct drm_crtc *crtc;
13555 struct drm_crtc_state *crtc_state;
13556 int ret = 0, i;
054518dd 13557
b359283a
ML
13558 if (!check_digital_port_conflicts(state)) {
13559 DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
13560 return -EINVAL;
13561 }
13562
565602d7
ML
13563 intel_state->modeset = true;
13564 intel_state->active_crtcs = dev_priv->active_crtcs;
13565
13566 for_each_crtc_in_state(state, crtc, crtc_state, i) {
13567 if (crtc_state->active)
13568 intel_state->active_crtcs |= 1 << i;
13569 else
13570 intel_state->active_crtcs &= ~(1 << i);
8b4a7d05
MR
13571
13572 if (crtc_state->active != crtc->state->active)
13573 intel_state->active_pipe_changes |= drm_crtc_mask(crtc);
565602d7
ML
13574 }
13575
054518dd
ACO
13576 /*
13577 * See if the config requires any additional preparation, e.g.
13578 * to adjust global state with pipes off. We need to do this
13579 * here so we can get the modeset_pipe updated config for the new
13580 * mode set on this crtc. For other crtcs we need to use the
13581 * adjusted_mode bits in the crtc directly.
13582 */
27c329ed 13583 if (dev_priv->display.modeset_calc_cdclk) {
c89e39f3 13584 if (!intel_state->cdclk_pll_vco)
63911d72 13585 intel_state->cdclk_pll_vco = dev_priv->cdclk_pll.vco;
b2045352
VS
13586 if (!intel_state->cdclk_pll_vco)
13587 intel_state->cdclk_pll_vco = dev_priv->skl_preferred_vco_freq;
c89e39f3 13588
27c329ed 13589 ret = dev_priv->display.modeset_calc_cdclk(state);
c89e39f3
CT
13590 if (ret < 0)
13591 return ret;
27c329ed 13592
c89e39f3 13593 if (intel_state->dev_cdclk != dev_priv->cdclk_freq ||
63911d72 13594 intel_state->cdclk_pll_vco != dev_priv->cdclk_pll.vco)
27c329ed
ML
13595 ret = intel_modeset_all_pipes(state);
13596
13597 if (ret < 0)
054518dd 13598 return ret;
e8788cbc
ML
13599
13600 DRM_DEBUG_KMS("New cdclk calculated to be atomic %u, actual %u\n",
13601 intel_state->cdclk, intel_state->dev_cdclk);
27c329ed 13602 } else
1a617b77 13603 to_intel_atomic_state(state)->cdclk = dev_priv->atomic_cdclk_freq;
054518dd 13604
ad421372 13605 intel_modeset_clear_plls(state);
054518dd 13606
565602d7 13607 if (IS_HASWELL(dev_priv))
ad421372 13608 return haswell_mode_set_planes_workaround(state);
99d736a2 13609
ad421372 13610 return 0;
c347a676
ACO
13611}
13612
aa363136
MR
13613/*
13614 * Handle calculation of various watermark data at the end of the atomic check
13615 * phase. The code here should be run after the per-crtc and per-plane 'check'
13616 * handlers to ensure that all derived state has been updated.
13617 */
55994c2c 13618static int calc_watermark_data(struct drm_atomic_state *state)
aa363136
MR
13619{
13620 struct drm_device *dev = state->dev;
98d39494 13621 struct drm_i915_private *dev_priv = to_i915(dev);
98d39494
MR
13622
13623 /* Is there platform-specific watermark information to calculate? */
13624 if (dev_priv->display.compute_global_watermarks)
55994c2c
MR
13625 return dev_priv->display.compute_global_watermarks(state);
13626
13627 return 0;
aa363136
MR
13628}
13629
74c090b1
ML
13630/**
13631 * intel_atomic_check - validate state object
13632 * @dev: drm device
13633 * @state: state to validate
13634 */
13635static int intel_atomic_check(struct drm_device *dev,
13636 struct drm_atomic_state *state)
c347a676 13637{
dd8b3bdb 13638 struct drm_i915_private *dev_priv = to_i915(dev);
aa363136 13639 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
c347a676
ACO
13640 struct drm_crtc *crtc;
13641 struct drm_crtc_state *crtc_state;
13642 int ret, i;
61333b60 13643 bool any_ms = false;
c347a676 13644
74c090b1 13645 ret = drm_atomic_helper_check_modeset(dev, state);
054518dd
ACO
13646 if (ret)
13647 return ret;
13648
c347a676 13649 for_each_crtc_in_state(state, crtc, crtc_state, i) {
cfb23ed6
ML
13650 struct intel_crtc_state *pipe_config =
13651 to_intel_crtc_state(crtc_state);
1ed51de9
DV
13652
13653 /* Catch I915_MODE_FLAG_INHERITED */
13654 if (crtc_state->mode.private_flags != crtc->state->mode.private_flags)
13655 crtc_state->mode_changed = true;
cfb23ed6 13656
af4a879e 13657 if (!needs_modeset(crtc_state))
c347a676
ACO
13658 continue;
13659
af4a879e
DV
13660 if (!crtc_state->enable) {
13661 any_ms = true;
cfb23ed6 13662 continue;
af4a879e 13663 }
cfb23ed6 13664
26495481
DV
13665 /* FIXME: For only active_changed we shouldn't need to do any
13666 * state recomputation at all. */
13667
1ed51de9
DV
13668 ret = drm_atomic_add_affected_connectors(state, crtc);
13669 if (ret)
13670 return ret;
b359283a 13671
cfb23ed6 13672 ret = intel_modeset_pipe_config(crtc, pipe_config);
25aa1c39
ML
13673 if (ret) {
13674 intel_dump_pipe_config(to_intel_crtc(crtc),
13675 pipe_config, "[failed]");
c347a676 13676 return ret;
25aa1c39 13677 }
c347a676 13678
73831236 13679 if (i915.fastboot &&
dd8b3bdb 13680 intel_pipe_config_compare(dev,
cfb23ed6 13681 to_intel_crtc_state(crtc->state),
1ed51de9 13682 pipe_config, true)) {
26495481 13683 crtc_state->mode_changed = false;
bfd16b2a 13684 to_intel_crtc_state(crtc_state)->update_pipe = true;
26495481
DV
13685 }
13686
af4a879e 13687 if (needs_modeset(crtc_state))
26495481 13688 any_ms = true;
cfb23ed6 13689
af4a879e
DV
13690 ret = drm_atomic_add_affected_planes(state, crtc);
13691 if (ret)
13692 return ret;
61333b60 13693
26495481
DV
13694 intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
13695 needs_modeset(crtc_state) ?
13696 "[modeset]" : "[fastset]");
c347a676
ACO
13697 }
13698
61333b60
ML
13699 if (any_ms) {
13700 ret = intel_modeset_checks(state);
13701
13702 if (ret)
13703 return ret;
27c329ed 13704 } else
dd8b3bdb 13705 intel_state->cdclk = dev_priv->cdclk_freq;
76305b1a 13706
dd8b3bdb 13707 ret = drm_atomic_helper_check_planes(dev, state);
aa363136
MR
13708 if (ret)
13709 return ret;
13710
f51be2e0 13711 intel_fbc_choose_crtc(dev_priv, state);
55994c2c 13712 return calc_watermark_data(state);
054518dd
ACO
13713}
13714
5008e874
ML
13715static int intel_atomic_prepare_commit(struct drm_device *dev,
13716 struct drm_atomic_state *state,
81072bfd 13717 bool nonblock)
5008e874 13718{
fac5e23e 13719 struct drm_i915_private *dev_priv = to_i915(dev);
7580d774 13720 struct drm_plane_state *plane_state;
5008e874 13721 struct drm_crtc_state *crtc_state;
7580d774 13722 struct drm_plane *plane;
5008e874
ML
13723 struct drm_crtc *crtc;
13724 int i, ret;
13725
5a21b665
DV
13726 for_each_crtc_in_state(state, crtc, crtc_state, i) {
13727 if (state->legacy_cursor_update)
a6747b73
ML
13728 continue;
13729
5a21b665
DV
13730 ret = intel_crtc_wait_for_pending_flips(crtc);
13731 if (ret)
13732 return ret;
5008e874 13733
5a21b665
DV
13734 if (atomic_read(&to_intel_crtc(crtc)->unpin_work_count) >= 2)
13735 flush_workqueue(dev_priv->wq);
d55dbd06
ML
13736 }
13737
f935675f
ML
13738 ret = mutex_lock_interruptible(&dev->struct_mutex);
13739 if (ret)
13740 return ret;
13741
5008e874 13742 ret = drm_atomic_helper_prepare_planes(dev, state);
f7e5838b 13743 mutex_unlock(&dev->struct_mutex);
7580d774 13744
21daaeee 13745 if (!ret && !nonblock) {
7580d774
ML
13746 for_each_plane_in_state(state, plane, plane_state, i) {
13747 struct intel_plane_state *intel_plane_state =
13748 to_intel_plane_state(plane_state);
13749
13750 if (!intel_plane_state->wait_req)
13751 continue;
13752
776f3236
CW
13753 ret = i915_wait_request(intel_plane_state->wait_req,
13754 true, NULL, NULL);
f7e5838b 13755 if (ret) {
f4457ae7
CW
13756 /* Any hang should be swallowed by the wait */
13757 WARN_ON(ret == -EIO);
f7e5838b
CW
13758 mutex_lock(&dev->struct_mutex);
13759 drm_atomic_helper_cleanup_planes(dev, state);
13760 mutex_unlock(&dev->struct_mutex);
7580d774 13761 break;
f7e5838b 13762 }
7580d774 13763 }
7580d774 13764 }
5008e874
ML
13765
13766 return ret;
13767}
13768
a2991414
ML
13769u32 intel_crtc_get_vblank_counter(struct intel_crtc *crtc)
13770{
13771 struct drm_device *dev = crtc->base.dev;
13772
13773 if (!dev->max_vblank_count)
13774 return drm_accurate_vblank_count(&crtc->base);
13775
13776 return dev->driver->get_vblank_counter(dev, crtc->pipe);
13777}
13778
5a21b665
DV
13779static void intel_atomic_wait_for_vblanks(struct drm_device *dev,
13780 struct drm_i915_private *dev_priv,
13781 unsigned crtc_mask)
e8861675 13782{
5a21b665
DV
13783 unsigned last_vblank_count[I915_MAX_PIPES];
13784 enum pipe pipe;
13785 int ret;
e8861675 13786
5a21b665
DV
13787 if (!crtc_mask)
13788 return;
e8861675 13789
5a21b665
DV
13790 for_each_pipe(dev_priv, pipe) {
13791 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
e8861675 13792
5a21b665 13793 if (!((1 << pipe) & crtc_mask))
e8861675
ML
13794 continue;
13795
5a21b665
DV
13796 ret = drm_crtc_vblank_get(crtc);
13797 if (WARN_ON(ret != 0)) {
13798 crtc_mask &= ~(1 << pipe);
13799 continue;
e8861675
ML
13800 }
13801
5a21b665 13802 last_vblank_count[pipe] = drm_crtc_vblank_count(crtc);
e8861675
ML
13803 }
13804
5a21b665
DV
13805 for_each_pipe(dev_priv, pipe) {
13806 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
13807 long lret;
e8861675 13808
5a21b665
DV
13809 if (!((1 << pipe) & crtc_mask))
13810 continue;
d55dbd06 13811
5a21b665
DV
13812 lret = wait_event_timeout(dev->vblank[pipe].queue,
13813 last_vblank_count[pipe] !=
13814 drm_crtc_vblank_count(crtc),
13815 msecs_to_jiffies(50));
d55dbd06 13816
5a21b665 13817 WARN(!lret, "pipe %c vblank wait timed out\n", pipe_name(pipe));
d55dbd06 13818
5a21b665 13819 drm_crtc_vblank_put(crtc);
d55dbd06
ML
13820 }
13821}
13822
5a21b665 13823static bool needs_vblank_wait(struct intel_crtc_state *crtc_state)
a6747b73 13824{
5a21b665
DV
13825 /* fb updated, need to unpin old fb */
13826 if (crtc_state->fb_changed)
13827 return true;
a6747b73 13828
5a21b665
DV
13829 /* wm changes, need vblank before final wm's */
13830 if (crtc_state->update_wm_post)
13831 return true;
a6747b73 13832
5a21b665
DV
13833 /*
13834 * cxsr is re-enabled after vblank.
13835 * This is already handled by crtc_state->update_wm_post,
13836 * but added for clarity.
13837 */
13838 if (crtc_state->disable_cxsr)
13839 return true;
a6747b73 13840
5a21b665 13841 return false;
e8861675
ML
13842}
13843
94f05024 13844static void intel_atomic_commit_tail(struct drm_atomic_state *state)
a6778b3c 13845{
94f05024 13846 struct drm_device *dev = state->dev;
565602d7 13847 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
fac5e23e 13848 struct drm_i915_private *dev_priv = to_i915(dev);
29ceb0e6 13849 struct drm_crtc_state *old_crtc_state;
7580d774 13850 struct drm_crtc *crtc;
5a21b665 13851 struct intel_crtc_state *intel_cstate;
94f05024
DV
13852 struct drm_plane *plane;
13853 struct drm_plane_state *plane_state;
5a21b665
DV
13854 bool hw_check = intel_state->modeset;
13855 unsigned long put_domains[I915_MAX_PIPES] = {};
13856 unsigned crtc_vblank_mask = 0;
94f05024 13857 int i, ret;
a6778b3c 13858
94f05024
DV
13859 for_each_plane_in_state(state, plane, plane_state, i) {
13860 struct intel_plane_state *intel_plane_state =
13861 to_intel_plane_state(plane_state);
ea0000f0 13862
94f05024
DV
13863 if (!intel_plane_state->wait_req)
13864 continue;
d4afb8cc 13865
776f3236
CW
13866 ret = i915_wait_request(intel_plane_state->wait_req,
13867 true, NULL, NULL);
94f05024
DV
13868 /* EIO should be eaten, and we can't get interrupted in the
13869 * worker, and blocking commits have waited already. */
13870 WARN_ON(ret);
13871 }
1c5e19f8 13872
ea0000f0
DV
13873 drm_atomic_helper_wait_for_dependencies(state);
13874
565602d7
ML
13875 if (intel_state->modeset) {
13876 memcpy(dev_priv->min_pixclk, intel_state->min_pixclk,
13877 sizeof(intel_state->min_pixclk));
13878 dev_priv->active_crtcs = intel_state->active_crtcs;
1a617b77 13879 dev_priv->atomic_cdclk_freq = intel_state->cdclk;
5a21b665
DV
13880
13881 intel_display_power_get(dev_priv, POWER_DOMAIN_MODESET);
565602d7
ML
13882 }
13883
29ceb0e6 13884 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
a539205a
ML
13885 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13886
5a21b665
DV
13887 if (needs_modeset(crtc->state) ||
13888 to_intel_crtc_state(crtc->state)->update_pipe) {
13889 hw_check = true;
13890
13891 put_domains[to_intel_crtc(crtc)->pipe] =
13892 modeset_get_crtc_power_domains(crtc,
13893 to_intel_crtc_state(crtc->state));
13894 }
13895
61333b60
ML
13896 if (!needs_modeset(crtc->state))
13897 continue;
13898
29ceb0e6 13899 intel_pre_plane_update(to_intel_crtc_state(old_crtc_state));
460da916 13900
29ceb0e6
VS
13901 if (old_crtc_state->active) {
13902 intel_crtc_disable_planes(crtc, old_crtc_state->plane_mask);
a539205a 13903 dev_priv->display.crtc_disable(crtc);
eddfcbcd 13904 intel_crtc->active = false;
58f9c0bc 13905 intel_fbc_disable(intel_crtc);
eddfcbcd 13906 intel_disable_shared_dpll(intel_crtc);
9bbc8258
VS
13907
13908 /*
13909 * Underruns don't always raise
13910 * interrupts, so check manually.
13911 */
13912 intel_check_cpu_fifo_underruns(dev_priv);
13913 intel_check_pch_fifo_underruns(dev_priv);
b9001114
ML
13914
13915 if (!crtc->state->active)
13916 intel_update_watermarks(crtc);
a539205a 13917 }
b8cecdf5 13918 }
7758a113 13919
ea9d758d
DV
13920 /* Only after disabling all output pipelines that will be changed can we
13921 * update the the output configuration. */
4740b0f2 13922 intel_modeset_update_crtc_state(state);
f6e5b160 13923
565602d7 13924 if (intel_state->modeset) {
4740b0f2 13925 drm_atomic_helper_update_legacy_modeset_state(state->dev, state);
33c8df89
ML
13926
13927 if (dev_priv->display.modeset_commit_cdclk &&
c89e39f3 13928 (intel_state->dev_cdclk != dev_priv->cdclk_freq ||
63911d72 13929 intel_state->cdclk_pll_vco != dev_priv->cdclk_pll.vco))
33c8df89 13930 dev_priv->display.modeset_commit_cdclk(state);
f6d1973d 13931
c0ead703 13932 intel_modeset_verify_disabled(dev);
4740b0f2 13933 }
47fab737 13934
a6778b3c 13935 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
29ceb0e6 13936 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
f6ac4b2a
ML
13937 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13938 bool modeset = needs_modeset(crtc->state);
5a21b665
DV
13939 struct intel_crtc_state *pipe_config =
13940 to_intel_crtc_state(crtc->state);
9f836f90 13941
f6ac4b2a 13942 if (modeset && crtc->state->active) {
a539205a
ML
13943 update_scanline_offset(to_intel_crtc(crtc));
13944 dev_priv->display.crtc_enable(crtc);
13945 }
80715b2f 13946
1f7528c4
DV
13947 /* Complete events for now disable pipes here. */
13948 if (modeset && !crtc->state->active && crtc->state->event) {
13949 spin_lock_irq(&dev->event_lock);
13950 drm_crtc_send_vblank_event(crtc, crtc->state->event);
13951 spin_unlock_irq(&dev->event_lock);
13952
13953 crtc->state->event = NULL;
13954 }
13955
f6ac4b2a 13956 if (!modeset)
29ceb0e6 13957 intel_pre_plane_update(to_intel_crtc_state(old_crtc_state));
f6ac4b2a 13958
5a21b665
DV
13959 if (crtc->state->active &&
13960 drm_atomic_get_existing_plane_state(state, crtc->primary))
faf68d92 13961 intel_fbc_enable(intel_crtc, pipe_config, to_intel_plane_state(crtc->primary->state));
5a21b665 13962
1f7528c4 13963 if (crtc->state->active)
5a21b665 13964 drm_atomic_helper_commit_planes_on_crtc(old_crtc_state);
f6d1973d 13965
5a21b665
DV
13966 if (pipe_config->base.active && needs_vblank_wait(pipe_config))
13967 crtc_vblank_mask |= 1 << i;
177246a8
MR
13968 }
13969
94f05024
DV
13970 /* FIXME: We should call drm_atomic_helper_commit_hw_done() here
13971 * already, but still need the state for the delayed optimization. To
13972 * fix this:
13973 * - wrap the optimization/post_plane_update stuff into a per-crtc work.
13974 * - schedule that vblank worker _before_ calling hw_done
13975 * - at the start of commit_tail, cancel it _synchrously
13976 * - switch over to the vblank wait helper in the core after that since
13977 * we don't need out special handling any more.
13978 */
5a21b665
DV
13979 if (!state->legacy_cursor_update)
13980 intel_atomic_wait_for_vblanks(dev, dev_priv, crtc_vblank_mask);
13981
13982 /*
13983 * Now that the vblank has passed, we can go ahead and program the
13984 * optimal watermarks on platforms that need two-step watermark
13985 * programming.
13986 *
13987 * TODO: Move this (and other cleanup) to an async worker eventually.
13988 */
13989 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
13990 intel_cstate = to_intel_crtc_state(crtc->state);
13991
13992 if (dev_priv->display.optimize_watermarks)
13993 dev_priv->display.optimize_watermarks(intel_cstate);
13994 }
13995
13996 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
13997 intel_post_plane_update(to_intel_crtc_state(old_crtc_state));
13998
13999 if (put_domains[i])
14000 modeset_put_power_domains(dev_priv, put_domains[i]);
14001
14002 intel_modeset_verify_crtc(crtc, old_crtc_state, crtc->state);
14003 }
14004
94f05024
DV
14005 drm_atomic_helper_commit_hw_done(state);
14006
5a21b665
DV
14007 if (intel_state->modeset)
14008 intel_display_power_put(dev_priv, POWER_DOMAIN_MODESET);
14009
14010 mutex_lock(&dev->struct_mutex);
14011 drm_atomic_helper_cleanup_planes(dev, state);
14012 mutex_unlock(&dev->struct_mutex);
14013
ea0000f0
DV
14014 drm_atomic_helper_commit_cleanup_done(state);
14015
ee165b1a 14016 drm_atomic_state_free(state);
f30da187 14017
75714940
MK
14018 /* As one of the primary mmio accessors, KMS has a high likelihood
14019 * of triggering bugs in unclaimed access. After we finish
14020 * modesetting, see if an error has been flagged, and if so
14021 * enable debugging for the next modeset - and hope we catch
14022 * the culprit.
14023 *
14024 * XXX note that we assume display power is on at this point.
14025 * This might hold true now but we need to add pm helper to check
14026 * unclaimed only when the hardware is on, as atomic commits
14027 * can happen also when the device is completely off.
14028 */
14029 intel_uncore_arm_unclaimed_mmio_detection(dev_priv);
94f05024
DV
14030}
14031
14032static void intel_atomic_commit_work(struct work_struct *work)
14033{
14034 struct drm_atomic_state *state = container_of(work,
14035 struct drm_atomic_state,
14036 commit_work);
14037 intel_atomic_commit_tail(state);
14038}
14039
6c9c1b38
DV
14040static void intel_atomic_track_fbs(struct drm_atomic_state *state)
14041{
14042 struct drm_plane_state *old_plane_state;
14043 struct drm_plane *plane;
6c9c1b38
DV
14044 int i;
14045
faf5bf0a
CW
14046 for_each_plane_in_state(state, plane, old_plane_state, i)
14047 i915_gem_track_fb(intel_fb_obj(old_plane_state->fb),
14048 intel_fb_obj(plane->state->fb),
14049 to_intel_plane(plane)->frontbuffer_bit);
6c9c1b38
DV
14050}
14051
94f05024
DV
14052/**
14053 * intel_atomic_commit - commit validated state object
14054 * @dev: DRM device
14055 * @state: the top-level driver state object
14056 * @nonblock: nonblocking commit
14057 *
14058 * This function commits a top-level state object that has been validated
14059 * with drm_atomic_helper_check().
14060 *
14061 * FIXME: Atomic modeset support for i915 is not yet complete. At the moment
14062 * nonblocking commits are only safe for pure plane updates. Everything else
14063 * should work though.
14064 *
14065 * RETURNS
14066 * Zero for success or -errno.
14067 */
14068static int intel_atomic_commit(struct drm_device *dev,
14069 struct drm_atomic_state *state,
14070 bool nonblock)
14071{
14072 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
fac5e23e 14073 struct drm_i915_private *dev_priv = to_i915(dev);
94f05024
DV
14074 int ret = 0;
14075
14076 if (intel_state->modeset && nonblock) {
14077 DRM_DEBUG_KMS("nonblocking commit for modeset not yet implemented.\n");
14078 return -EINVAL;
14079 }
14080
14081 ret = drm_atomic_helper_setup_commit(state, nonblock);
14082 if (ret)
14083 return ret;
14084
14085 INIT_WORK(&state->commit_work, intel_atomic_commit_work);
14086
14087 ret = intel_atomic_prepare_commit(dev, state, nonblock);
14088 if (ret) {
14089 DRM_DEBUG_ATOMIC("Preparing state failed with %i\n", ret);
14090 return ret;
14091 }
14092
14093 drm_atomic_helper_swap_state(state, true);
14094 dev_priv->wm.distrust_bios_wm = false;
14095 dev_priv->wm.skl_results = intel_state->wm_results;
14096 intel_shared_dpll_commit(state);
6c9c1b38 14097 intel_atomic_track_fbs(state);
94f05024
DV
14098
14099 if (nonblock)
14100 queue_work(system_unbound_wq, &state->commit_work);
14101 else
14102 intel_atomic_commit_tail(state);
75714940 14103
74c090b1 14104 return 0;
7f27126e
JB
14105}
14106
c0c36b94
CW
14107void intel_crtc_restore_mode(struct drm_crtc *crtc)
14108{
83a57153
ACO
14109 struct drm_device *dev = crtc->dev;
14110 struct drm_atomic_state *state;
e694eb02 14111 struct drm_crtc_state *crtc_state;
2bfb4627 14112 int ret;
83a57153
ACO
14113
14114 state = drm_atomic_state_alloc(dev);
14115 if (!state) {
78108b7c
VS
14116 DRM_DEBUG_KMS("[CRTC:%d:%s] crtc restore failed, out of memory",
14117 crtc->base.id, crtc->name);
83a57153
ACO
14118 return;
14119 }
14120
e694eb02 14121 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
83a57153 14122
e694eb02
ML
14123retry:
14124 crtc_state = drm_atomic_get_crtc_state(state, crtc);
14125 ret = PTR_ERR_OR_ZERO(crtc_state);
14126 if (!ret) {
14127 if (!crtc_state->active)
14128 goto out;
83a57153 14129
e694eb02 14130 crtc_state->mode_changed = true;
74c090b1 14131 ret = drm_atomic_commit(state);
83a57153
ACO
14132 }
14133
e694eb02
ML
14134 if (ret == -EDEADLK) {
14135 drm_atomic_state_clear(state);
14136 drm_modeset_backoff(state->acquire_ctx);
14137 goto retry;
4ed9fb37 14138 }
4be07317 14139
2bfb4627 14140 if (ret)
e694eb02 14141out:
2bfb4627 14142 drm_atomic_state_free(state);
c0c36b94
CW
14143}
14144
25c5b266
DV
14145#undef for_each_intel_crtc_masked
14146
a8784875
BP
14147/*
14148 * FIXME: Remove this once i915 is fully DRIVER_ATOMIC by calling
14149 * drm_atomic_helper_legacy_gamma_set() directly.
14150 */
14151static int intel_atomic_legacy_gamma_set(struct drm_crtc *crtc,
14152 u16 *red, u16 *green, u16 *blue,
14153 uint32_t size)
14154{
14155 struct drm_device *dev = crtc->dev;
14156 struct drm_mode_config *config = &dev->mode_config;
14157 struct drm_crtc_state *state;
14158 int ret;
14159
14160 ret = drm_atomic_helper_legacy_gamma_set(crtc, red, green, blue, size);
14161 if (ret)
14162 return ret;
14163
14164 /*
14165 * Make sure we update the legacy properties so this works when
14166 * atomic is not enabled.
14167 */
14168
14169 state = crtc->state;
14170
14171 drm_object_property_set_value(&crtc->base,
14172 config->degamma_lut_property,
14173 (state->degamma_lut) ?
14174 state->degamma_lut->base.id : 0);
14175
14176 drm_object_property_set_value(&crtc->base,
14177 config->ctm_property,
14178 (state->ctm) ?
14179 state->ctm->base.id : 0);
14180
14181 drm_object_property_set_value(&crtc->base,
14182 config->gamma_lut_property,
14183 (state->gamma_lut) ?
14184 state->gamma_lut->base.id : 0);
14185
14186 return 0;
14187}
14188
f6e5b160 14189static const struct drm_crtc_funcs intel_crtc_funcs = {
a8784875 14190 .gamma_set = intel_atomic_legacy_gamma_set,
74c090b1 14191 .set_config = drm_atomic_helper_set_config,
82cf435b 14192 .set_property = drm_atomic_helper_crtc_set_property,
f6e5b160 14193 .destroy = intel_crtc_destroy,
527b6abe 14194 .page_flip = intel_crtc_page_flip,
1356837e
MR
14195 .atomic_duplicate_state = intel_crtc_duplicate_state,
14196 .atomic_destroy_state = intel_crtc_destroy_state,
f6e5b160
CW
14197};
14198
6beb8c23
MR
14199/**
14200 * intel_prepare_plane_fb - Prepare fb for usage on plane
14201 * @plane: drm plane to prepare for
14202 * @fb: framebuffer to prepare for presentation
14203 *
14204 * Prepares a framebuffer for usage on a display plane. Generally this
14205 * involves pinning the underlying object and updating the frontbuffer tracking
14206 * bits. Some older platforms need special physical address handling for
14207 * cursor planes.
14208 *
f935675f
ML
14209 * Must be called with struct_mutex held.
14210 *
6beb8c23
MR
14211 * Returns 0 on success, negative error code on failure.
14212 */
14213int
14214intel_prepare_plane_fb(struct drm_plane *plane,
d136dfee 14215 const struct drm_plane_state *new_state)
465c120c
MR
14216{
14217 struct drm_device *dev = plane->dev;
844f9111 14218 struct drm_framebuffer *fb = new_state->fb;
6beb8c23 14219 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
1ee49399 14220 struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->state->fb);
c37efb99 14221 struct reservation_object *resv;
6beb8c23 14222 int ret = 0;
465c120c 14223
1ee49399 14224 if (!obj && !old_obj)
465c120c
MR
14225 return 0;
14226
5008e874
ML
14227 if (old_obj) {
14228 struct drm_crtc_state *crtc_state =
14229 drm_atomic_get_existing_crtc_state(new_state->state, plane->state->crtc);
14230
14231 /* Big Hammer, we also need to ensure that any pending
14232 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
14233 * current scanout is retired before unpinning the old
14234 * framebuffer. Note that we rely on userspace rendering
14235 * into the buffer attached to the pipe they are waiting
14236 * on. If not, userspace generates a GPU hang with IPEHR
14237 * point to the MI_WAIT_FOR_EVENT.
14238 *
14239 * This should only fail upon a hung GPU, in which case we
14240 * can safely continue.
14241 */
14242 if (needs_modeset(crtc_state))
14243 ret = i915_gem_object_wait_rendering(old_obj, true);
f4457ae7
CW
14244 if (ret) {
14245 /* GPU hangs should have been swallowed by the wait */
14246 WARN_ON(ret == -EIO);
f935675f 14247 return ret;
f4457ae7 14248 }
5008e874
ML
14249 }
14250
c37efb99
CW
14251 if (!obj)
14252 return 0;
14253
5a21b665 14254 /* For framebuffer backed by dmabuf, wait for fence */
c37efb99
CW
14255 resv = i915_gem_object_get_dmabuf_resv(obj);
14256 if (resv) {
5a21b665
DV
14257 long lret;
14258
c37efb99 14259 lret = reservation_object_wait_timeout_rcu(resv, false, true,
5a21b665
DV
14260 MAX_SCHEDULE_TIMEOUT);
14261 if (lret == -ERESTARTSYS)
14262 return lret;
14263
14264 WARN(lret < 0, "waiting returns %li\n", lret);
14265 }
14266
c37efb99 14267 if (plane->type == DRM_PLANE_TYPE_CURSOR &&
6beb8c23
MR
14268 INTEL_INFO(dev)->cursor_needs_physical) {
14269 int align = IS_I830(dev) ? 16 * 1024 : 256;
14270 ret = i915_gem_object_attach_phys(obj, align);
14271 if (ret)
14272 DRM_DEBUG_KMS("failed to attach phys object\n");
14273 } else {
3465c580 14274 ret = intel_pin_and_fence_fb_obj(fb, new_state->rotation);
6beb8c23 14275 }
465c120c 14276
c37efb99 14277 if (ret == 0) {
27c01aae 14278 to_intel_plane_state(new_state)->wait_req =
d72d908b
CW
14279 i915_gem_active_get(&obj->last_write,
14280 &obj->base.dev->struct_mutex);
7580d774 14281 }
fdd508a6 14282
6beb8c23
MR
14283 return ret;
14284}
14285
38f3ce3a
MR
14286/**
14287 * intel_cleanup_plane_fb - Cleans up an fb after plane use
14288 * @plane: drm plane to clean up for
14289 * @fb: old framebuffer that was on plane
14290 *
14291 * Cleans up a framebuffer that has just been removed from a plane.
f935675f
ML
14292 *
14293 * Must be called with struct_mutex held.
38f3ce3a
MR
14294 */
14295void
14296intel_cleanup_plane_fb(struct drm_plane *plane,
d136dfee 14297 const struct drm_plane_state *old_state)
38f3ce3a
MR
14298{
14299 struct drm_device *dev = plane->dev;
7580d774 14300 struct intel_plane_state *old_intel_state;
84978257 14301 struct intel_plane_state *intel_state = to_intel_plane_state(plane->state);
1ee49399
ML
14302 struct drm_i915_gem_object *old_obj = intel_fb_obj(old_state->fb);
14303 struct drm_i915_gem_object *obj = intel_fb_obj(plane->state->fb);
38f3ce3a 14304
7580d774
ML
14305 old_intel_state = to_intel_plane_state(old_state);
14306
1ee49399 14307 if (!obj && !old_obj)
38f3ce3a
MR
14308 return;
14309
1ee49399
ML
14310 if (old_obj && (plane->type != DRM_PLANE_TYPE_CURSOR ||
14311 !INTEL_INFO(dev)->cursor_needs_physical))
3465c580 14312 intel_unpin_fb_obj(old_state->fb, old_state->rotation);
1ee49399 14313
84978257 14314 i915_gem_request_assign(&intel_state->wait_req, NULL);
7580d774 14315 i915_gem_request_assign(&old_intel_state->wait_req, NULL);
465c120c
MR
14316}
14317
6156a456
CK
14318int
14319skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
14320{
14321 int max_scale;
6156a456
CK
14322 int crtc_clock, cdclk;
14323
bf8a0af0 14324 if (!intel_crtc || !crtc_state->base.enable)
6156a456
CK
14325 return DRM_PLANE_HELPER_NO_SCALING;
14326
6156a456 14327 crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
27c329ed 14328 cdclk = to_intel_atomic_state(crtc_state->base.state)->cdclk;
6156a456 14329
54bf1ce6 14330 if (WARN_ON_ONCE(!crtc_clock || cdclk < crtc_clock))
6156a456
CK
14331 return DRM_PLANE_HELPER_NO_SCALING;
14332
14333 /*
14334 * skl max scale is lower of:
14335 * close to 3 but not 3, -1 is for that purpose
14336 * or
14337 * cdclk/crtc_clock
14338 */
14339 max_scale = min((1 << 16) * 3 - 1, (1 << 8) * ((cdclk << 8) / crtc_clock));
14340
14341 return max_scale;
14342}
14343
465c120c 14344static int
3c692a41 14345intel_check_primary_plane(struct drm_plane *plane,
061e4b8d 14346 struct intel_crtc_state *crtc_state,
3c692a41
GP
14347 struct intel_plane_state *state)
14348{
2b875c22
MR
14349 struct drm_crtc *crtc = state->base.crtc;
14350 struct drm_framebuffer *fb = state->base.fb;
6156a456 14351 int min_scale = DRM_PLANE_HELPER_NO_SCALING;
061e4b8d
ML
14352 int max_scale = DRM_PLANE_HELPER_NO_SCALING;
14353 bool can_position = false;
465c120c 14354
693bdc28
VS
14355 if (INTEL_INFO(plane->dev)->gen >= 9) {
14356 /* use scaler when colorkey is not required */
14357 if (state->ckey.flags == I915_SET_COLORKEY_NONE) {
14358 min_scale = 1;
14359 max_scale = skl_max_scale(to_intel_crtc(crtc), crtc_state);
14360 }
d8106366 14361 can_position = true;
6156a456 14362 }
d8106366 14363
061e4b8d
ML
14364 return drm_plane_helper_check_update(plane, crtc, fb, &state->src,
14365 &state->dst, &state->clip,
9b8b013d 14366 state->base.rotation,
da20eabd
ML
14367 min_scale, max_scale,
14368 can_position, true,
14369 &state->visible);
14af293f
GP
14370}
14371
5a21b665
DV
14372static void intel_begin_crtc_commit(struct drm_crtc *crtc,
14373 struct drm_crtc_state *old_crtc_state)
14374{
14375 struct drm_device *dev = crtc->dev;
14376 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
14377 struct intel_crtc_state *old_intel_state =
14378 to_intel_crtc_state(old_crtc_state);
14379 bool modeset = needs_modeset(crtc->state);
14380
14381 /* Perform vblank evasion around commit operation */
14382 intel_pipe_update_start(intel_crtc);
14383
14384 if (modeset)
14385 return;
14386
14387 if (crtc->state->color_mgmt_changed || to_intel_crtc_state(crtc->state)->update_pipe) {
14388 intel_color_set_csc(crtc->state);
14389 intel_color_load_luts(crtc->state);
14390 }
14391
14392 if (to_intel_crtc_state(crtc->state)->update_pipe)
14393 intel_update_pipe_config(intel_crtc, old_intel_state);
14394 else if (INTEL_INFO(dev)->gen >= 9)
14395 skl_detach_scalers(intel_crtc);
14396}
14397
14398static void intel_finish_crtc_commit(struct drm_crtc *crtc,
14399 struct drm_crtc_state *old_crtc_state)
14400{
14401 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
14402
14403 intel_pipe_update_end(intel_crtc, NULL);
14404}
14405
cf4c7c12 14406/**
4a3b8769
MR
14407 * intel_plane_destroy - destroy a plane
14408 * @plane: plane to destroy
cf4c7c12 14409 *
4a3b8769
MR
14410 * Common destruction function for all types of planes (primary, cursor,
14411 * sprite).
cf4c7c12 14412 */
4a3b8769 14413void intel_plane_destroy(struct drm_plane *plane)
465c120c 14414{
69ae561f
VS
14415 if (!plane)
14416 return;
14417
465c120c 14418 drm_plane_cleanup(plane);
69ae561f 14419 kfree(to_intel_plane(plane));
465c120c
MR
14420}
14421
65a3fea0 14422const struct drm_plane_funcs intel_plane_funcs = {
70a101f8
MR
14423 .update_plane = drm_atomic_helper_update_plane,
14424 .disable_plane = drm_atomic_helper_disable_plane,
3d7d6510 14425 .destroy = intel_plane_destroy,
c196e1d6 14426 .set_property = drm_atomic_helper_plane_set_property,
a98b3431
MR
14427 .atomic_get_property = intel_plane_atomic_get_property,
14428 .atomic_set_property = intel_plane_atomic_set_property,
ea2c67bb
MR
14429 .atomic_duplicate_state = intel_plane_duplicate_state,
14430 .atomic_destroy_state = intel_plane_destroy_state,
14431
465c120c
MR
14432};
14433
14434static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
14435 int pipe)
14436{
fca0ce2a
VS
14437 struct intel_plane *primary = NULL;
14438 struct intel_plane_state *state = NULL;
465c120c 14439 const uint32_t *intel_primary_formats;
45e3743a 14440 unsigned int num_formats;
fca0ce2a 14441 int ret;
465c120c
MR
14442
14443 primary = kzalloc(sizeof(*primary), GFP_KERNEL);
fca0ce2a
VS
14444 if (!primary)
14445 goto fail;
465c120c 14446
8e7d688b 14447 state = intel_create_plane_state(&primary->base);
fca0ce2a
VS
14448 if (!state)
14449 goto fail;
8e7d688b 14450 primary->base.state = &state->base;
ea2c67bb 14451
465c120c
MR
14452 primary->can_scale = false;
14453 primary->max_downscale = 1;
6156a456
CK
14454 if (INTEL_INFO(dev)->gen >= 9) {
14455 primary->can_scale = true;
af99ceda 14456 state->scaler_id = -1;
6156a456 14457 }
465c120c
MR
14458 primary->pipe = pipe;
14459 primary->plane = pipe;
a9ff8714 14460 primary->frontbuffer_bit = INTEL_FRONTBUFFER_PRIMARY(pipe);
c59cb179 14461 primary->check_plane = intel_check_primary_plane;
465c120c
MR
14462 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
14463 primary->plane = !pipe;
14464
6c0fd451
DL
14465 if (INTEL_INFO(dev)->gen >= 9) {
14466 intel_primary_formats = skl_primary_formats;
14467 num_formats = ARRAY_SIZE(skl_primary_formats);
a8d201af
ML
14468
14469 primary->update_plane = skylake_update_primary_plane;
14470 primary->disable_plane = skylake_disable_primary_plane;
14471 } else if (HAS_PCH_SPLIT(dev)) {
14472 intel_primary_formats = i965_primary_formats;
14473 num_formats = ARRAY_SIZE(i965_primary_formats);
14474
14475 primary->update_plane = ironlake_update_primary_plane;
14476 primary->disable_plane = i9xx_disable_primary_plane;
6c0fd451 14477 } else if (INTEL_INFO(dev)->gen >= 4) {
568db4f2
DL
14478 intel_primary_formats = i965_primary_formats;
14479 num_formats = ARRAY_SIZE(i965_primary_formats);
a8d201af
ML
14480
14481 primary->update_plane = i9xx_update_primary_plane;
14482 primary->disable_plane = i9xx_disable_primary_plane;
6c0fd451
DL
14483 } else {
14484 intel_primary_formats = i8xx_primary_formats;
14485 num_formats = ARRAY_SIZE(i8xx_primary_formats);
a8d201af
ML
14486
14487 primary->update_plane = i9xx_update_primary_plane;
14488 primary->disable_plane = i9xx_disable_primary_plane;
465c120c
MR
14489 }
14490
38573dc1
VS
14491 if (INTEL_INFO(dev)->gen >= 9)
14492 ret = drm_universal_plane_init(dev, &primary->base, 0,
14493 &intel_plane_funcs,
14494 intel_primary_formats, num_formats,
14495 DRM_PLANE_TYPE_PRIMARY,
14496 "plane 1%c", pipe_name(pipe));
14497 else if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
14498 ret = drm_universal_plane_init(dev, &primary->base, 0,
14499 &intel_plane_funcs,
14500 intel_primary_formats, num_formats,
14501 DRM_PLANE_TYPE_PRIMARY,
14502 "primary %c", pipe_name(pipe));
14503 else
14504 ret = drm_universal_plane_init(dev, &primary->base, 0,
14505 &intel_plane_funcs,
14506 intel_primary_formats, num_formats,
14507 DRM_PLANE_TYPE_PRIMARY,
14508 "plane %c", plane_name(primary->plane));
fca0ce2a
VS
14509 if (ret)
14510 goto fail;
48404c1e 14511
3b7a5119
SJ
14512 if (INTEL_INFO(dev)->gen >= 4)
14513 intel_create_rotation_property(dev, primary);
48404c1e 14514
ea2c67bb
MR
14515 drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
14516
465c120c 14517 return &primary->base;
fca0ce2a
VS
14518
14519fail:
14520 kfree(state);
14521 kfree(primary);
14522
14523 return NULL;
465c120c
MR
14524}
14525
3b7a5119
SJ
14526void intel_create_rotation_property(struct drm_device *dev, struct intel_plane *plane)
14527{
14528 if (!dev->mode_config.rotation_property) {
14529 unsigned long flags = BIT(DRM_ROTATE_0) |
14530 BIT(DRM_ROTATE_180);
14531
14532 if (INTEL_INFO(dev)->gen >= 9)
14533 flags |= BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_270);
14534
14535 dev->mode_config.rotation_property =
14536 drm_mode_create_rotation_property(dev, flags);
14537 }
14538 if (dev->mode_config.rotation_property)
14539 drm_object_attach_property(&plane->base.base,
14540 dev->mode_config.rotation_property,
14541 plane->base.state->rotation);
14542}
14543
3d7d6510 14544static int
852e787c 14545intel_check_cursor_plane(struct drm_plane *plane,
061e4b8d 14546 struct intel_crtc_state *crtc_state,
852e787c 14547 struct intel_plane_state *state)
3d7d6510 14548{
061e4b8d 14549 struct drm_crtc *crtc = crtc_state->base.crtc;
2b875c22 14550 struct drm_framebuffer *fb = state->base.fb;
757f9a3e 14551 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
b29ec92c 14552 enum pipe pipe = to_intel_plane(plane)->pipe;
757f9a3e
GP
14553 unsigned stride;
14554 int ret;
3d7d6510 14555
061e4b8d
ML
14556 ret = drm_plane_helper_check_update(plane, crtc, fb, &state->src,
14557 &state->dst, &state->clip,
9b8b013d 14558 state->base.rotation,
3d7d6510
MR
14559 DRM_PLANE_HELPER_NO_SCALING,
14560 DRM_PLANE_HELPER_NO_SCALING,
852e787c 14561 true, true, &state->visible);
757f9a3e
GP
14562 if (ret)
14563 return ret;
14564
757f9a3e
GP
14565 /* if we want to turn off the cursor ignore width and height */
14566 if (!obj)
da20eabd 14567 return 0;
757f9a3e 14568
757f9a3e 14569 /* Check for which cursor types we support */
061e4b8d 14570 if (!cursor_size_ok(plane->dev, state->base.crtc_w, state->base.crtc_h)) {
ea2c67bb
MR
14571 DRM_DEBUG("Cursor dimension %dx%d not supported\n",
14572 state->base.crtc_w, state->base.crtc_h);
757f9a3e
GP
14573 return -EINVAL;
14574 }
14575
ea2c67bb
MR
14576 stride = roundup_pow_of_two(state->base.crtc_w) * 4;
14577 if (obj->base.size < stride * state->base.crtc_h) {
757f9a3e
GP
14578 DRM_DEBUG_KMS("buffer is too small\n");
14579 return -ENOMEM;
14580 }
14581
3a656b54 14582 if (fb->modifier[0] != DRM_FORMAT_MOD_NONE) {
757f9a3e 14583 DRM_DEBUG_KMS("cursor cannot be tiled\n");
da20eabd 14584 return -EINVAL;
32b7eeec
MR
14585 }
14586
b29ec92c
VS
14587 /*
14588 * There's something wrong with the cursor on CHV pipe C.
14589 * If it straddles the left edge of the screen then
14590 * moving it away from the edge or disabling it often
14591 * results in a pipe underrun, and often that can lead to
14592 * dead pipe (constant underrun reported, and it scans
14593 * out just a solid color). To recover from that, the
14594 * display power well must be turned off and on again.
14595 * Refuse the put the cursor into that compromised position.
14596 */
14597 if (IS_CHERRYVIEW(plane->dev) && pipe == PIPE_C &&
14598 state->visible && state->base.crtc_x < 0) {
14599 DRM_DEBUG_KMS("CHV cursor C not allowed to straddle the left screen edge\n");
14600 return -EINVAL;
14601 }
14602
da20eabd 14603 return 0;
852e787c 14604}
3d7d6510 14605
a8ad0d8e
ML
14606static void
14607intel_disable_cursor_plane(struct drm_plane *plane,
7fabf5ef 14608 struct drm_crtc *crtc)
a8ad0d8e 14609{
f2858021
ML
14610 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
14611
14612 intel_crtc->cursor_addr = 0;
55a08b3f 14613 intel_crtc_update_cursor(crtc, NULL);
a8ad0d8e
ML
14614}
14615
f4a2cf29 14616static void
55a08b3f
ML
14617intel_update_cursor_plane(struct drm_plane *plane,
14618 const struct intel_crtc_state *crtc_state,
14619 const struct intel_plane_state *state)
852e787c 14620{
55a08b3f
ML
14621 struct drm_crtc *crtc = crtc_state->base.crtc;
14622 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ea2c67bb 14623 struct drm_device *dev = plane->dev;
2b875c22 14624 struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
a912f12f 14625 uint32_t addr;
852e787c 14626
f4a2cf29 14627 if (!obj)
a912f12f 14628 addr = 0;
f4a2cf29 14629 else if (!INTEL_INFO(dev)->cursor_needs_physical)
a912f12f 14630 addr = i915_gem_obj_ggtt_offset(obj);
f4a2cf29 14631 else
a912f12f 14632 addr = obj->phys_handle->busaddr;
852e787c 14633
a912f12f 14634 intel_crtc->cursor_addr = addr;
55a08b3f 14635 intel_crtc_update_cursor(crtc, state);
852e787c
GP
14636}
14637
3d7d6510
MR
14638static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
14639 int pipe)
14640{
fca0ce2a
VS
14641 struct intel_plane *cursor = NULL;
14642 struct intel_plane_state *state = NULL;
14643 int ret;
3d7d6510
MR
14644
14645 cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
fca0ce2a
VS
14646 if (!cursor)
14647 goto fail;
3d7d6510 14648
8e7d688b 14649 state = intel_create_plane_state(&cursor->base);
fca0ce2a
VS
14650 if (!state)
14651 goto fail;
8e7d688b 14652 cursor->base.state = &state->base;
ea2c67bb 14653
3d7d6510
MR
14654 cursor->can_scale = false;
14655 cursor->max_downscale = 1;
14656 cursor->pipe = pipe;
14657 cursor->plane = pipe;
a9ff8714 14658 cursor->frontbuffer_bit = INTEL_FRONTBUFFER_CURSOR(pipe);
c59cb179 14659 cursor->check_plane = intel_check_cursor_plane;
55a08b3f 14660 cursor->update_plane = intel_update_cursor_plane;
a8ad0d8e 14661 cursor->disable_plane = intel_disable_cursor_plane;
3d7d6510 14662
fca0ce2a
VS
14663 ret = drm_universal_plane_init(dev, &cursor->base, 0,
14664 &intel_plane_funcs,
14665 intel_cursor_formats,
14666 ARRAY_SIZE(intel_cursor_formats),
38573dc1
VS
14667 DRM_PLANE_TYPE_CURSOR,
14668 "cursor %c", pipe_name(pipe));
fca0ce2a
VS
14669 if (ret)
14670 goto fail;
4398ad45
VS
14671
14672 if (INTEL_INFO(dev)->gen >= 4) {
14673 if (!dev->mode_config.rotation_property)
14674 dev->mode_config.rotation_property =
14675 drm_mode_create_rotation_property(dev,
14676 BIT(DRM_ROTATE_0) |
14677 BIT(DRM_ROTATE_180));
14678 if (dev->mode_config.rotation_property)
14679 drm_object_attach_property(&cursor->base.base,
14680 dev->mode_config.rotation_property,
8e7d688b 14681 state->base.rotation);
4398ad45
VS
14682 }
14683
af99ceda
CK
14684 if (INTEL_INFO(dev)->gen >=9)
14685 state->scaler_id = -1;
14686
ea2c67bb
MR
14687 drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
14688
3d7d6510 14689 return &cursor->base;
fca0ce2a
VS
14690
14691fail:
14692 kfree(state);
14693 kfree(cursor);
14694
14695 return NULL;
3d7d6510
MR
14696}
14697
549e2bfb
CK
14698static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
14699 struct intel_crtc_state *crtc_state)
14700{
14701 int i;
14702 struct intel_scaler *intel_scaler;
14703 struct intel_crtc_scaler_state *scaler_state = &crtc_state->scaler_state;
14704
14705 for (i = 0; i < intel_crtc->num_scalers; i++) {
14706 intel_scaler = &scaler_state->scalers[i];
14707 intel_scaler->in_use = 0;
549e2bfb
CK
14708 intel_scaler->mode = PS_SCALER_MODE_DYN;
14709 }
14710
14711 scaler_state->scaler_id = -1;
14712}
14713
b358d0a6 14714static void intel_crtc_init(struct drm_device *dev, int pipe)
79e53945 14715{
fac5e23e 14716 struct drm_i915_private *dev_priv = to_i915(dev);
79e53945 14717 struct intel_crtc *intel_crtc;
f5de6e07 14718 struct intel_crtc_state *crtc_state = NULL;
3d7d6510
MR
14719 struct drm_plane *primary = NULL;
14720 struct drm_plane *cursor = NULL;
8563b1e8 14721 int ret;
79e53945 14722
955382f3 14723 intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
79e53945
JB
14724 if (intel_crtc == NULL)
14725 return;
14726
f5de6e07
ACO
14727 crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
14728 if (!crtc_state)
14729 goto fail;
550acefd
ACO
14730 intel_crtc->config = crtc_state;
14731 intel_crtc->base.state = &crtc_state->base;
07878248 14732 crtc_state->base.crtc = &intel_crtc->base;
f5de6e07 14733
549e2bfb
CK
14734 /* initialize shared scalers */
14735 if (INTEL_INFO(dev)->gen >= 9) {
14736 if (pipe == PIPE_C)
14737 intel_crtc->num_scalers = 1;
14738 else
14739 intel_crtc->num_scalers = SKL_NUM_SCALERS;
14740
14741 skl_init_scalers(dev, intel_crtc, crtc_state);
14742 }
14743
465c120c 14744 primary = intel_primary_plane_create(dev, pipe);
3d7d6510
MR
14745 if (!primary)
14746 goto fail;
14747
14748 cursor = intel_cursor_plane_create(dev, pipe);
14749 if (!cursor)
14750 goto fail;
14751
465c120c 14752 ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
4d5d72b7
VS
14753 cursor, &intel_crtc_funcs,
14754 "pipe %c", pipe_name(pipe));
3d7d6510
MR
14755 if (ret)
14756 goto fail;
79e53945 14757
1f1c2e24
VS
14758 /*
14759 * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
8c0f92e1 14760 * is hooked to pipe B. Hence we want plane A feeding pipe B.
1f1c2e24 14761 */
80824003
JB
14762 intel_crtc->pipe = pipe;
14763 intel_crtc->plane = pipe;
3a77c4c4 14764 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
28c97730 14765 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
e2e767ab 14766 intel_crtc->plane = !pipe;
80824003
JB
14767 }
14768
4b0e333e
CW
14769 intel_crtc->cursor_base = ~0;
14770 intel_crtc->cursor_cntl = ~0;
dc41c154 14771 intel_crtc->cursor_size = ~0;
8d7849db 14772
852eb00d
VS
14773 intel_crtc->wm.cxsr_allowed = true;
14774
22fd0fab
JB
14775 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
14776 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
14777 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
14778 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
14779
79e53945 14780 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
87b6b101 14781
8563b1e8
LL
14782 intel_color_init(&intel_crtc->base);
14783
87b6b101 14784 WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
3d7d6510
MR
14785 return;
14786
14787fail:
69ae561f
VS
14788 intel_plane_destroy(primary);
14789 intel_plane_destroy(cursor);
f5de6e07 14790 kfree(crtc_state);
3d7d6510 14791 kfree(intel_crtc);
79e53945
JB
14792}
14793
752aa88a
JB
14794enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
14795{
14796 struct drm_encoder *encoder = connector->base.encoder;
6e9f798d 14797 struct drm_device *dev = connector->base.dev;
752aa88a 14798
51fd371b 14799 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
752aa88a 14800
d3babd3f 14801 if (!encoder || WARN_ON(!encoder->crtc))
752aa88a
JB
14802 return INVALID_PIPE;
14803
14804 return to_intel_crtc(encoder->crtc)->pipe;
14805}
14806
08d7b3d1 14807int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
05394f39 14808 struct drm_file *file)
08d7b3d1 14809{
08d7b3d1 14810 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
7707e653 14811 struct drm_crtc *drmmode_crtc;
c05422d5 14812 struct intel_crtc *crtc;
08d7b3d1 14813
7707e653 14814 drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
71240ed2 14815 if (!drmmode_crtc)
3f2c2057 14816 return -ENOENT;
08d7b3d1 14817
7707e653 14818 crtc = to_intel_crtc(drmmode_crtc);
c05422d5 14819 pipe_from_crtc_id->pipe = crtc->pipe;
08d7b3d1 14820
c05422d5 14821 return 0;
08d7b3d1
CW
14822}
14823
66a9278e 14824static int intel_encoder_clones(struct intel_encoder *encoder)
79e53945 14825{
66a9278e
DV
14826 struct drm_device *dev = encoder->base.dev;
14827 struct intel_encoder *source_encoder;
79e53945 14828 int index_mask = 0;
79e53945
JB
14829 int entry = 0;
14830
b2784e15 14831 for_each_intel_encoder(dev, source_encoder) {
bc079e8b 14832 if (encoders_cloneable(encoder, source_encoder))
66a9278e
DV
14833 index_mask |= (1 << entry);
14834
79e53945
JB
14835 entry++;
14836 }
4ef69c7a 14837
79e53945
JB
14838 return index_mask;
14839}
14840
4d302442
CW
14841static bool has_edp_a(struct drm_device *dev)
14842{
fac5e23e 14843 struct drm_i915_private *dev_priv = to_i915(dev);
4d302442
CW
14844
14845 if (!IS_MOBILE(dev))
14846 return false;
14847
14848 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
14849 return false;
14850
e3589908 14851 if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
4d302442
CW
14852 return false;
14853
14854 return true;
14855}
14856
84b4e042
JB
14857static bool intel_crt_present(struct drm_device *dev)
14858{
fac5e23e 14859 struct drm_i915_private *dev_priv = to_i915(dev);
84b4e042 14860
884497ed
DL
14861 if (INTEL_INFO(dev)->gen >= 9)
14862 return false;
14863
cf404ce4 14864 if (IS_HSW_ULT(dev) || IS_BDW_ULT(dev))
84b4e042
JB
14865 return false;
14866
14867 if (IS_CHERRYVIEW(dev))
14868 return false;
14869
65e472e4
VS
14870 if (HAS_PCH_LPT_H(dev) && I915_READ(SFUSE_STRAP) & SFUSE_STRAP_CRT_DISABLED)
14871 return false;
14872
70ac54d0
VS
14873 /* DDI E can't be used if DDI A requires 4 lanes */
14874 if (HAS_DDI(dev) && I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_A_4_LANES)
14875 return false;
14876
e4abb733 14877 if (!dev_priv->vbt.int_crt_support)
84b4e042
JB
14878 return false;
14879
14880 return true;
14881}
14882
8090ba8c
ID
14883void intel_pps_unlock_regs_wa(struct drm_i915_private *dev_priv)
14884{
14885 int pps_num;
14886 int pps_idx;
14887
14888 if (HAS_DDI(dev_priv))
14889 return;
14890 /*
14891 * This w/a is needed at least on CPT/PPT, but to be sure apply it
14892 * everywhere where registers can be write protected.
14893 */
14894 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
14895 pps_num = 2;
14896 else
14897 pps_num = 1;
14898
14899 for (pps_idx = 0; pps_idx < pps_num; pps_idx++) {
14900 u32 val = I915_READ(PP_CONTROL(pps_idx));
14901
14902 val = (val & ~PANEL_UNLOCK_MASK) | PANEL_UNLOCK_REGS;
14903 I915_WRITE(PP_CONTROL(pps_idx), val);
14904 }
14905}
14906
44cb734c
ID
14907static void intel_pps_init(struct drm_i915_private *dev_priv)
14908{
14909 if (HAS_PCH_SPLIT(dev_priv) || IS_BROXTON(dev_priv))
14910 dev_priv->pps_mmio_base = PCH_PPS_BASE;
14911 else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
14912 dev_priv->pps_mmio_base = VLV_PPS_BASE;
14913 else
14914 dev_priv->pps_mmio_base = PPS_BASE;
8090ba8c
ID
14915
14916 intel_pps_unlock_regs_wa(dev_priv);
44cb734c
ID
14917}
14918
79e53945
JB
14919static void intel_setup_outputs(struct drm_device *dev)
14920{
fac5e23e 14921 struct drm_i915_private *dev_priv = to_i915(dev);
4ef69c7a 14922 struct intel_encoder *encoder;
cb0953d7 14923 bool dpd_is_edp = false;
79e53945 14924
44cb734c
ID
14925 intel_pps_init(dev_priv);
14926
97a824e1
ID
14927 /*
14928 * intel_edp_init_connector() depends on this completing first, to
14929 * prevent the registeration of both eDP and LVDS and the incorrect
14930 * sharing of the PPS.
14931 */
c9093354 14932 intel_lvds_init(dev);
79e53945 14933
84b4e042 14934 if (intel_crt_present(dev))
79935fca 14935 intel_crt_init(dev);
cb0953d7 14936
c776eb2e
VK
14937 if (IS_BROXTON(dev)) {
14938 /*
14939 * FIXME: Broxton doesn't support port detection via the
14940 * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
14941 * detect the ports.
14942 */
14943 intel_ddi_init(dev, PORT_A);
14944 intel_ddi_init(dev, PORT_B);
14945 intel_ddi_init(dev, PORT_C);
c6c794a2
SS
14946
14947 intel_dsi_init(dev);
c776eb2e 14948 } else if (HAS_DDI(dev)) {
0e72a5b5
ED
14949 int found;
14950
de31facd
JB
14951 /*
14952 * Haswell uses DDI functions to detect digital outputs.
14953 * On SKL pre-D0 the strap isn't connected, so we assume
14954 * it's there.
14955 */
77179400 14956 found = I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_INIT_DISPLAY_DETECTED;
de31facd 14957 /* WaIgnoreDDIAStrap: skl */
ef11bdb3 14958 if (found || IS_SKYLAKE(dev) || IS_KABYLAKE(dev))
0e72a5b5
ED
14959 intel_ddi_init(dev, PORT_A);
14960
14961 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
14962 * register */
14963 found = I915_READ(SFUSE_STRAP);
14964
14965 if (found & SFUSE_STRAP_DDIB_DETECTED)
14966 intel_ddi_init(dev, PORT_B);
14967 if (found & SFUSE_STRAP_DDIC_DETECTED)
14968 intel_ddi_init(dev, PORT_C);
14969 if (found & SFUSE_STRAP_DDID_DETECTED)
14970 intel_ddi_init(dev, PORT_D);
2800e4c2
RV
14971 /*
14972 * On SKL we don't have a way to detect DDI-E so we rely on VBT.
14973 */
ef11bdb3 14974 if ((IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) &&
2800e4c2
RV
14975 (dev_priv->vbt.ddi_port_info[PORT_E].supports_dp ||
14976 dev_priv->vbt.ddi_port_info[PORT_E].supports_dvi ||
14977 dev_priv->vbt.ddi_port_info[PORT_E].supports_hdmi))
14978 intel_ddi_init(dev, PORT_E);
14979
0e72a5b5 14980 } else if (HAS_PCH_SPLIT(dev)) {
cb0953d7 14981 int found;
5d8a7752 14982 dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
270b3042
DV
14983
14984 if (has_edp_a(dev))
14985 intel_dp_init(dev, DP_A, PORT_A);
cb0953d7 14986
dc0fa718 14987 if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
461ed3ca 14988 /* PCH SDVOB multiplex with HDMIB */
2a5c0832 14989 found = intel_sdvo_init(dev, PCH_SDVOB, PORT_B);
30ad48b7 14990 if (!found)
e2debe91 14991 intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
5eb08b69 14992 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
ab9d7c30 14993 intel_dp_init(dev, PCH_DP_B, PORT_B);
30ad48b7
ZW
14994 }
14995
dc0fa718 14996 if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
e2debe91 14997 intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
30ad48b7 14998
dc0fa718 14999 if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
e2debe91 15000 intel_hdmi_init(dev, PCH_HDMID, PORT_D);
30ad48b7 15001
5eb08b69 15002 if (I915_READ(PCH_DP_C) & DP_DETECTED)
ab9d7c30 15003 intel_dp_init(dev, PCH_DP_C, PORT_C);
5eb08b69 15004
270b3042 15005 if (I915_READ(PCH_DP_D) & DP_DETECTED)
ab9d7c30 15006 intel_dp_init(dev, PCH_DP_D, PORT_D);
666a4537 15007 } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
22f35042 15008 bool has_edp, has_port;
457c52d8 15009
e17ac6db
VS
15010 /*
15011 * The DP_DETECTED bit is the latched state of the DDC
15012 * SDA pin at boot. However since eDP doesn't require DDC
15013 * (no way to plug in a DP->HDMI dongle) the DDC pins for
15014 * eDP ports may have been muxed to an alternate function.
15015 * Thus we can't rely on the DP_DETECTED bit alone to detect
15016 * eDP ports. Consult the VBT as well as DP_DETECTED to
15017 * detect eDP ports.
22f35042
VS
15018 *
15019 * Sadly the straps seem to be missing sometimes even for HDMI
15020 * ports (eg. on Voyo V3 - CHT x7-Z8700), so check both strap
15021 * and VBT for the presence of the port. Additionally we can't
15022 * trust the port type the VBT declares as we've seen at least
15023 * HDMI ports that the VBT claim are DP or eDP.
e17ac6db 15024 */
457c52d8 15025 has_edp = intel_dp_is_edp(dev, PORT_B);
22f35042
VS
15026 has_port = intel_bios_is_port_present(dev_priv, PORT_B);
15027 if (I915_READ(VLV_DP_B) & DP_DETECTED || has_port)
457c52d8 15028 has_edp &= intel_dp_init(dev, VLV_DP_B, PORT_B);
22f35042 15029 if ((I915_READ(VLV_HDMIB) & SDVO_DETECTED || has_port) && !has_edp)
e66eb81d 15030 intel_hdmi_init(dev, VLV_HDMIB, PORT_B);
585a94b8 15031
457c52d8 15032 has_edp = intel_dp_is_edp(dev, PORT_C);
22f35042
VS
15033 has_port = intel_bios_is_port_present(dev_priv, PORT_C);
15034 if (I915_READ(VLV_DP_C) & DP_DETECTED || has_port)
457c52d8 15035 has_edp &= intel_dp_init(dev, VLV_DP_C, PORT_C);
22f35042 15036 if ((I915_READ(VLV_HDMIC) & SDVO_DETECTED || has_port) && !has_edp)
e66eb81d 15037 intel_hdmi_init(dev, VLV_HDMIC, PORT_C);
19c03924 15038
9418c1f1 15039 if (IS_CHERRYVIEW(dev)) {
22f35042
VS
15040 /*
15041 * eDP not supported on port D,
15042 * so no need to worry about it
15043 */
15044 has_port = intel_bios_is_port_present(dev_priv, PORT_D);
15045 if (I915_READ(CHV_DP_D) & DP_DETECTED || has_port)
e66eb81d 15046 intel_dp_init(dev, CHV_DP_D, PORT_D);
22f35042
VS
15047 if (I915_READ(CHV_HDMID) & SDVO_DETECTED || has_port)
15048 intel_hdmi_init(dev, CHV_HDMID, PORT_D);
9418c1f1
VS
15049 }
15050
3cfca973 15051 intel_dsi_init(dev);
09da55dc 15052 } else if (!IS_GEN2(dev) && !IS_PINEVIEW(dev)) {
27185ae1 15053 bool found = false;
7d57382e 15054
e2debe91 15055 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
b01f2c3a 15056 DRM_DEBUG_KMS("probing SDVOB\n");
2a5c0832 15057 found = intel_sdvo_init(dev, GEN3_SDVOB, PORT_B);
3fec3d2f 15058 if (!found && IS_G4X(dev)) {
b01f2c3a 15059 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
e2debe91 15060 intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
b01f2c3a 15061 }
27185ae1 15062
3fec3d2f 15063 if (!found && IS_G4X(dev))
ab9d7c30 15064 intel_dp_init(dev, DP_B, PORT_B);
725e30ad 15065 }
13520b05
KH
15066
15067 /* Before G4X SDVOC doesn't have its own detect register */
13520b05 15068
e2debe91 15069 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
b01f2c3a 15070 DRM_DEBUG_KMS("probing SDVOC\n");
2a5c0832 15071 found = intel_sdvo_init(dev, GEN3_SDVOC, PORT_C);
b01f2c3a 15072 }
27185ae1 15073
e2debe91 15074 if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
27185ae1 15075
3fec3d2f 15076 if (IS_G4X(dev)) {
b01f2c3a 15077 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
e2debe91 15078 intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
b01f2c3a 15079 }
3fec3d2f 15080 if (IS_G4X(dev))
ab9d7c30 15081 intel_dp_init(dev, DP_C, PORT_C);
725e30ad 15082 }
27185ae1 15083
3fec3d2f 15084 if (IS_G4X(dev) &&
e7281eab 15085 (I915_READ(DP_D) & DP_DETECTED))
ab9d7c30 15086 intel_dp_init(dev, DP_D, PORT_D);
bad720ff 15087 } else if (IS_GEN2(dev))
79e53945
JB
15088 intel_dvo_init(dev);
15089
103a196f 15090 if (SUPPORTS_TV(dev))
79e53945
JB
15091 intel_tv_init(dev);
15092
0bc12bcb 15093 intel_psr_init(dev);
7c8f8a70 15094
b2784e15 15095 for_each_intel_encoder(dev, encoder) {
4ef69c7a
CW
15096 encoder->base.possible_crtcs = encoder->crtc_mask;
15097 encoder->base.possible_clones =
66a9278e 15098 intel_encoder_clones(encoder);
79e53945 15099 }
47356eb6 15100
dde86e2d 15101 intel_init_pch_refclk(dev);
270b3042
DV
15102
15103 drm_helper_move_panel_connectors_to_head(dev);
79e53945
JB
15104}
15105
15106static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
15107{
60a5ca01 15108 struct drm_device *dev = fb->dev;
79e53945 15109 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
79e53945 15110
ef2d633e 15111 drm_framebuffer_cleanup(fb);
60a5ca01 15112 mutex_lock(&dev->struct_mutex);
ef2d633e 15113 WARN_ON(!intel_fb->obj->framebuffer_references--);
f8c417cd 15114 i915_gem_object_put(intel_fb->obj);
60a5ca01 15115 mutex_unlock(&dev->struct_mutex);
79e53945
JB
15116 kfree(intel_fb);
15117}
15118
15119static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
05394f39 15120 struct drm_file *file,
79e53945
JB
15121 unsigned int *handle)
15122{
15123 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
05394f39 15124 struct drm_i915_gem_object *obj = intel_fb->obj;
79e53945 15125
cc917ab4
CW
15126 if (obj->userptr.mm) {
15127 DRM_DEBUG("attempting to use a userptr for a framebuffer, denied\n");
15128 return -EINVAL;
15129 }
15130
05394f39 15131 return drm_gem_handle_create(file, &obj->base, handle);
79e53945
JB
15132}
15133
86c98588
RV
15134static int intel_user_framebuffer_dirty(struct drm_framebuffer *fb,
15135 struct drm_file *file,
15136 unsigned flags, unsigned color,
15137 struct drm_clip_rect *clips,
15138 unsigned num_clips)
15139{
15140 struct drm_device *dev = fb->dev;
15141 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
15142 struct drm_i915_gem_object *obj = intel_fb->obj;
15143
15144 mutex_lock(&dev->struct_mutex);
74b4ea1e 15145 intel_fb_obj_flush(obj, false, ORIGIN_DIRTYFB);
86c98588
RV
15146 mutex_unlock(&dev->struct_mutex);
15147
15148 return 0;
15149}
15150
79e53945
JB
15151static const struct drm_framebuffer_funcs intel_fb_funcs = {
15152 .destroy = intel_user_framebuffer_destroy,
15153 .create_handle = intel_user_framebuffer_create_handle,
86c98588 15154 .dirty = intel_user_framebuffer_dirty,
79e53945
JB
15155};
15156
b321803d
DL
15157static
15158u32 intel_fb_pitch_limit(struct drm_device *dev, uint64_t fb_modifier,
15159 uint32_t pixel_format)
15160{
15161 u32 gen = INTEL_INFO(dev)->gen;
15162
15163 if (gen >= 9) {
ac484963
VS
15164 int cpp = drm_format_plane_cpp(pixel_format, 0);
15165
b321803d
DL
15166 /* "The stride in bytes must not exceed the of the size of 8K
15167 * pixels and 32K bytes."
15168 */
ac484963 15169 return min(8192 * cpp, 32768);
666a4537 15170 } else if (gen >= 5 && !IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
b321803d
DL
15171 return 32*1024;
15172 } else if (gen >= 4) {
15173 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
15174 return 16*1024;
15175 else
15176 return 32*1024;
15177 } else if (gen >= 3) {
15178 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
15179 return 8*1024;
15180 else
15181 return 16*1024;
15182 } else {
15183 /* XXX DSPC is limited to 4k tiled */
15184 return 8*1024;
15185 }
15186}
15187
b5ea642a
DV
15188static int intel_framebuffer_init(struct drm_device *dev,
15189 struct intel_framebuffer *intel_fb,
15190 struct drm_mode_fb_cmd2 *mode_cmd,
15191 struct drm_i915_gem_object *obj)
79e53945 15192{
7b49f948 15193 struct drm_i915_private *dev_priv = to_i915(dev);
79e53945 15194 int ret;
b321803d 15195 u32 pitch_limit, stride_alignment;
79e53945 15196
dd4916c5
DV
15197 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
15198
2a80eada
DV
15199 if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
15200 /* Enforce that fb modifier and tiling mode match, but only for
15201 * X-tiled. This is needed for FBC. */
3e510a8e 15202 if (!!(i915_gem_object_get_tiling(obj) == I915_TILING_X) !=
2a80eada
DV
15203 !!(mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED)) {
15204 DRM_DEBUG("tiling_mode doesn't match fb modifier\n");
15205 return -EINVAL;
15206 }
15207 } else {
3e510a8e 15208 if (i915_gem_object_get_tiling(obj) == I915_TILING_X)
2a80eada 15209 mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
3e510a8e 15210 else if (i915_gem_object_get_tiling(obj) == I915_TILING_Y) {
2a80eada
DV
15211 DRM_DEBUG("No Y tiling for legacy addfb\n");
15212 return -EINVAL;
15213 }
15214 }
15215
9a8f0a12
TU
15216 /* Passed in modifier sanity checking. */
15217 switch (mode_cmd->modifier[0]) {
15218 case I915_FORMAT_MOD_Y_TILED:
15219 case I915_FORMAT_MOD_Yf_TILED:
15220 if (INTEL_INFO(dev)->gen < 9) {
15221 DRM_DEBUG("Unsupported tiling 0x%llx!\n",
15222 mode_cmd->modifier[0]);
15223 return -EINVAL;
15224 }
15225 case DRM_FORMAT_MOD_NONE:
15226 case I915_FORMAT_MOD_X_TILED:
15227 break;
15228 default:
c0f40428
JB
15229 DRM_DEBUG("Unsupported fb modifier 0x%llx!\n",
15230 mode_cmd->modifier[0]);
57cd6508 15231 return -EINVAL;
c16ed4be 15232 }
57cd6508 15233
7b49f948
VS
15234 stride_alignment = intel_fb_stride_alignment(dev_priv,
15235 mode_cmd->modifier[0],
b321803d
DL
15236 mode_cmd->pixel_format);
15237 if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
15238 DRM_DEBUG("pitch (%d) must be at least %u byte aligned\n",
15239 mode_cmd->pitches[0], stride_alignment);
57cd6508 15240 return -EINVAL;
c16ed4be 15241 }
57cd6508 15242
b321803d
DL
15243 pitch_limit = intel_fb_pitch_limit(dev, mode_cmd->modifier[0],
15244 mode_cmd->pixel_format);
a35cdaa0 15245 if (mode_cmd->pitches[0] > pitch_limit) {
b321803d
DL
15246 DRM_DEBUG("%s pitch (%u) must be at less than %d\n",
15247 mode_cmd->modifier[0] != DRM_FORMAT_MOD_NONE ?
2a80eada 15248 "tiled" : "linear",
a35cdaa0 15249 mode_cmd->pitches[0], pitch_limit);
5d7bd705 15250 return -EINVAL;
c16ed4be 15251 }
5d7bd705 15252
2a80eada 15253 if (mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED &&
3e510a8e 15254 mode_cmd->pitches[0] != i915_gem_object_get_stride(obj)) {
c16ed4be 15255 DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
3e510a8e
CW
15256 mode_cmd->pitches[0],
15257 i915_gem_object_get_stride(obj));
5d7bd705 15258 return -EINVAL;
c16ed4be 15259 }
5d7bd705 15260
57779d06 15261 /* Reject formats not supported by any plane early. */
308e5bcb 15262 switch (mode_cmd->pixel_format) {
57779d06 15263 case DRM_FORMAT_C8:
04b3924d
VS
15264 case DRM_FORMAT_RGB565:
15265 case DRM_FORMAT_XRGB8888:
15266 case DRM_FORMAT_ARGB8888:
57779d06
VS
15267 break;
15268 case DRM_FORMAT_XRGB1555:
c16ed4be 15269 if (INTEL_INFO(dev)->gen > 3) {
4ee62c76
VS
15270 DRM_DEBUG("unsupported pixel format: %s\n",
15271 drm_get_format_name(mode_cmd->pixel_format));
57779d06 15272 return -EINVAL;
c16ed4be 15273 }
57779d06 15274 break;
57779d06 15275 case DRM_FORMAT_ABGR8888:
666a4537
WB
15276 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev) &&
15277 INTEL_INFO(dev)->gen < 9) {
6c0fd451
DL
15278 DRM_DEBUG("unsupported pixel format: %s\n",
15279 drm_get_format_name(mode_cmd->pixel_format));
15280 return -EINVAL;
15281 }
15282 break;
15283 case DRM_FORMAT_XBGR8888:
04b3924d 15284 case DRM_FORMAT_XRGB2101010:
57779d06 15285 case DRM_FORMAT_XBGR2101010:
c16ed4be 15286 if (INTEL_INFO(dev)->gen < 4) {
4ee62c76
VS
15287 DRM_DEBUG("unsupported pixel format: %s\n",
15288 drm_get_format_name(mode_cmd->pixel_format));
57779d06 15289 return -EINVAL;
c16ed4be 15290 }
b5626747 15291 break;
7531208b 15292 case DRM_FORMAT_ABGR2101010:
666a4537 15293 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
7531208b
DL
15294 DRM_DEBUG("unsupported pixel format: %s\n",
15295 drm_get_format_name(mode_cmd->pixel_format));
15296 return -EINVAL;
15297 }
15298 break;
04b3924d
VS
15299 case DRM_FORMAT_YUYV:
15300 case DRM_FORMAT_UYVY:
15301 case DRM_FORMAT_YVYU:
15302 case DRM_FORMAT_VYUY:
c16ed4be 15303 if (INTEL_INFO(dev)->gen < 5) {
4ee62c76
VS
15304 DRM_DEBUG("unsupported pixel format: %s\n",
15305 drm_get_format_name(mode_cmd->pixel_format));
57779d06 15306 return -EINVAL;
c16ed4be 15307 }
57cd6508
CW
15308 break;
15309 default:
4ee62c76
VS
15310 DRM_DEBUG("unsupported pixel format: %s\n",
15311 drm_get_format_name(mode_cmd->pixel_format));
57cd6508
CW
15312 return -EINVAL;
15313 }
15314
90f9a336
VS
15315 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
15316 if (mode_cmd->offsets[0] != 0)
15317 return -EINVAL;
15318
c7d73f6a
DV
15319 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
15320 intel_fb->obj = obj;
15321
6687c906
VS
15322 ret = intel_fill_fb_info(dev_priv, &intel_fb->base);
15323 if (ret)
15324 return ret;
2d7a215f 15325
79e53945
JB
15326 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
15327 if (ret) {
15328 DRM_ERROR("framebuffer init failed %d\n", ret);
15329 return ret;
15330 }
15331
0b05e1e0
VS
15332 intel_fb->obj->framebuffer_references++;
15333
79e53945
JB
15334 return 0;
15335}
15336
79e53945
JB
15337static struct drm_framebuffer *
15338intel_user_framebuffer_create(struct drm_device *dev,
15339 struct drm_file *filp,
1eb83451 15340 const struct drm_mode_fb_cmd2 *user_mode_cmd)
79e53945 15341{
dcb1394e 15342 struct drm_framebuffer *fb;
05394f39 15343 struct drm_i915_gem_object *obj;
76dc3769 15344 struct drm_mode_fb_cmd2 mode_cmd = *user_mode_cmd;
79e53945 15345
03ac0642
CW
15346 obj = i915_gem_object_lookup(filp, mode_cmd.handles[0]);
15347 if (!obj)
cce13ff7 15348 return ERR_PTR(-ENOENT);
79e53945 15349
92907cbb 15350 fb = intel_framebuffer_create(dev, &mode_cmd, obj);
dcb1394e 15351 if (IS_ERR(fb))
34911fd3 15352 i915_gem_object_put_unlocked(obj);
dcb1394e
LW
15353
15354 return fb;
79e53945
JB
15355}
15356
0695726e 15357#ifndef CONFIG_DRM_FBDEV_EMULATION
0632fef6 15358static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
4520f53a
DV
15359{
15360}
15361#endif
15362
79e53945 15363static const struct drm_mode_config_funcs intel_mode_funcs = {
79e53945 15364 .fb_create = intel_user_framebuffer_create,
0632fef6 15365 .output_poll_changed = intel_fbdev_output_poll_changed,
5ee67f1c
MR
15366 .atomic_check = intel_atomic_check,
15367 .atomic_commit = intel_atomic_commit,
de419ab6
ML
15368 .atomic_state_alloc = intel_atomic_state_alloc,
15369 .atomic_state_clear = intel_atomic_state_clear,
79e53945
JB
15370};
15371
88212941
ID
15372/**
15373 * intel_init_display_hooks - initialize the display modesetting hooks
15374 * @dev_priv: device private
15375 */
15376void intel_init_display_hooks(struct drm_i915_private *dev_priv)
e70236a8 15377{
88212941 15378 if (INTEL_INFO(dev_priv)->gen >= 9) {
bc8d7dff 15379 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
5724dbd1
DL
15380 dev_priv->display.get_initial_plane_config =
15381 skylake_get_initial_plane_config;
bc8d7dff
DL
15382 dev_priv->display.crtc_compute_clock =
15383 haswell_crtc_compute_clock;
15384 dev_priv->display.crtc_enable = haswell_crtc_enable;
15385 dev_priv->display.crtc_disable = haswell_crtc_disable;
88212941 15386 } else if (HAS_DDI(dev_priv)) {
0e8ffe1b 15387 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
5724dbd1
DL
15388 dev_priv->display.get_initial_plane_config =
15389 ironlake_get_initial_plane_config;
797d0259
ACO
15390 dev_priv->display.crtc_compute_clock =
15391 haswell_crtc_compute_clock;
4f771f10
PZ
15392 dev_priv->display.crtc_enable = haswell_crtc_enable;
15393 dev_priv->display.crtc_disable = haswell_crtc_disable;
88212941 15394 } else if (HAS_PCH_SPLIT(dev_priv)) {
0e8ffe1b 15395 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
5724dbd1
DL
15396 dev_priv->display.get_initial_plane_config =
15397 ironlake_get_initial_plane_config;
3fb37703
ACO
15398 dev_priv->display.crtc_compute_clock =
15399 ironlake_crtc_compute_clock;
76e5a89c
DV
15400 dev_priv->display.crtc_enable = ironlake_crtc_enable;
15401 dev_priv->display.crtc_disable = ironlake_crtc_disable;
65b3d6a9 15402 } else if (IS_CHERRYVIEW(dev_priv)) {
89b667f8 15403 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
5724dbd1
DL
15404 dev_priv->display.get_initial_plane_config =
15405 i9xx_get_initial_plane_config;
65b3d6a9
ACO
15406 dev_priv->display.crtc_compute_clock = chv_crtc_compute_clock;
15407 dev_priv->display.crtc_enable = valleyview_crtc_enable;
15408 dev_priv->display.crtc_disable = i9xx_crtc_disable;
15409 } else if (IS_VALLEYVIEW(dev_priv)) {
15410 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
15411 dev_priv->display.get_initial_plane_config =
15412 i9xx_get_initial_plane_config;
15413 dev_priv->display.crtc_compute_clock = vlv_crtc_compute_clock;
89b667f8
JB
15414 dev_priv->display.crtc_enable = valleyview_crtc_enable;
15415 dev_priv->display.crtc_disable = i9xx_crtc_disable;
19ec6693
ACO
15416 } else if (IS_G4X(dev_priv)) {
15417 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
15418 dev_priv->display.get_initial_plane_config =
15419 i9xx_get_initial_plane_config;
15420 dev_priv->display.crtc_compute_clock = g4x_crtc_compute_clock;
15421 dev_priv->display.crtc_enable = i9xx_crtc_enable;
15422 dev_priv->display.crtc_disable = i9xx_crtc_disable;
70e8aa21
ACO
15423 } else if (IS_PINEVIEW(dev_priv)) {
15424 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
15425 dev_priv->display.get_initial_plane_config =
15426 i9xx_get_initial_plane_config;
15427 dev_priv->display.crtc_compute_clock = pnv_crtc_compute_clock;
15428 dev_priv->display.crtc_enable = i9xx_crtc_enable;
15429 dev_priv->display.crtc_disable = i9xx_crtc_disable;
81c97f52 15430 } else if (!IS_GEN2(dev_priv)) {
0e8ffe1b 15431 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
5724dbd1
DL
15432 dev_priv->display.get_initial_plane_config =
15433 i9xx_get_initial_plane_config;
d6dfee7a 15434 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
76e5a89c
DV
15435 dev_priv->display.crtc_enable = i9xx_crtc_enable;
15436 dev_priv->display.crtc_disable = i9xx_crtc_disable;
81c97f52
ACO
15437 } else {
15438 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
15439 dev_priv->display.get_initial_plane_config =
15440 i9xx_get_initial_plane_config;
15441 dev_priv->display.crtc_compute_clock = i8xx_crtc_compute_clock;
15442 dev_priv->display.crtc_enable = i9xx_crtc_enable;
15443 dev_priv->display.crtc_disable = i9xx_crtc_disable;
f564048e 15444 }
e70236a8 15445
e70236a8 15446 /* Returns the core display clock speed */
88212941 15447 if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv))
1652d19e
VS
15448 dev_priv->display.get_display_clock_speed =
15449 skylake_get_display_clock_speed;
88212941 15450 else if (IS_BROXTON(dev_priv))
acd3f3d3
BP
15451 dev_priv->display.get_display_clock_speed =
15452 broxton_get_display_clock_speed;
88212941 15453 else if (IS_BROADWELL(dev_priv))
1652d19e
VS
15454 dev_priv->display.get_display_clock_speed =
15455 broadwell_get_display_clock_speed;
88212941 15456 else if (IS_HASWELL(dev_priv))
1652d19e
VS
15457 dev_priv->display.get_display_clock_speed =
15458 haswell_get_display_clock_speed;
88212941 15459 else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
25eb05fc
JB
15460 dev_priv->display.get_display_clock_speed =
15461 valleyview_get_display_clock_speed;
88212941 15462 else if (IS_GEN5(dev_priv))
b37a6434
VS
15463 dev_priv->display.get_display_clock_speed =
15464 ilk_get_display_clock_speed;
88212941
ID
15465 else if (IS_I945G(dev_priv) || IS_BROADWATER(dev_priv) ||
15466 IS_GEN6(dev_priv) || IS_IVYBRIDGE(dev_priv))
e70236a8
JB
15467 dev_priv->display.get_display_clock_speed =
15468 i945_get_display_clock_speed;
88212941 15469 else if (IS_GM45(dev_priv))
34edce2f
VS
15470 dev_priv->display.get_display_clock_speed =
15471 gm45_get_display_clock_speed;
88212941 15472 else if (IS_CRESTLINE(dev_priv))
34edce2f
VS
15473 dev_priv->display.get_display_clock_speed =
15474 i965gm_get_display_clock_speed;
88212941 15475 else if (IS_PINEVIEW(dev_priv))
34edce2f
VS
15476 dev_priv->display.get_display_clock_speed =
15477 pnv_get_display_clock_speed;
88212941 15478 else if (IS_G33(dev_priv) || IS_G4X(dev_priv))
34edce2f
VS
15479 dev_priv->display.get_display_clock_speed =
15480 g33_get_display_clock_speed;
88212941 15481 else if (IS_I915G(dev_priv))
e70236a8
JB
15482 dev_priv->display.get_display_clock_speed =
15483 i915_get_display_clock_speed;
88212941 15484 else if (IS_I945GM(dev_priv) || IS_845G(dev_priv))
e70236a8
JB
15485 dev_priv->display.get_display_clock_speed =
15486 i9xx_misc_get_display_clock_speed;
88212941 15487 else if (IS_I915GM(dev_priv))
e70236a8
JB
15488 dev_priv->display.get_display_clock_speed =
15489 i915gm_get_display_clock_speed;
88212941 15490 else if (IS_I865G(dev_priv))
e70236a8
JB
15491 dev_priv->display.get_display_clock_speed =
15492 i865_get_display_clock_speed;
88212941 15493 else if (IS_I85X(dev_priv))
e70236a8 15494 dev_priv->display.get_display_clock_speed =
1b1d2716 15495 i85x_get_display_clock_speed;
623e01e5 15496 else { /* 830 */
88212941 15497 WARN(!IS_I830(dev_priv), "Unknown platform. Assuming 133 MHz CDCLK\n");
e70236a8
JB
15498 dev_priv->display.get_display_clock_speed =
15499 i830_get_display_clock_speed;
623e01e5 15500 }
e70236a8 15501
88212941 15502 if (IS_GEN5(dev_priv)) {
3bb11b53 15503 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
88212941 15504 } else if (IS_GEN6(dev_priv)) {
3bb11b53 15505 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
88212941 15506 } else if (IS_IVYBRIDGE(dev_priv)) {
3bb11b53
SJ
15507 /* FIXME: detect B0+ stepping and use auto training */
15508 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
88212941 15509 } else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
3bb11b53 15510 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
445e780b
VS
15511 }
15512
15513 if (IS_BROADWELL(dev_priv)) {
15514 dev_priv->display.modeset_commit_cdclk =
15515 broadwell_modeset_commit_cdclk;
15516 dev_priv->display.modeset_calc_cdclk =
15517 broadwell_modeset_calc_cdclk;
88212941 15518 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
27c329ed
ML
15519 dev_priv->display.modeset_commit_cdclk =
15520 valleyview_modeset_commit_cdclk;
15521 dev_priv->display.modeset_calc_cdclk =
15522 valleyview_modeset_calc_cdclk;
88212941 15523 } else if (IS_BROXTON(dev_priv)) {
27c329ed 15524 dev_priv->display.modeset_commit_cdclk =
324513c0 15525 bxt_modeset_commit_cdclk;
27c329ed 15526 dev_priv->display.modeset_calc_cdclk =
324513c0 15527 bxt_modeset_calc_cdclk;
c89e39f3
CT
15528 } else if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv)) {
15529 dev_priv->display.modeset_commit_cdclk =
15530 skl_modeset_commit_cdclk;
15531 dev_priv->display.modeset_calc_cdclk =
15532 skl_modeset_calc_cdclk;
e70236a8 15533 }
5a21b665
DV
15534
15535 switch (INTEL_INFO(dev_priv)->gen) {
15536 case 2:
15537 dev_priv->display.queue_flip = intel_gen2_queue_flip;
15538 break;
15539
15540 case 3:
15541 dev_priv->display.queue_flip = intel_gen3_queue_flip;
15542 break;
15543
15544 case 4:
15545 case 5:
15546 dev_priv->display.queue_flip = intel_gen4_queue_flip;
15547 break;
15548
15549 case 6:
15550 dev_priv->display.queue_flip = intel_gen6_queue_flip;
15551 break;
15552 case 7:
15553 case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
15554 dev_priv->display.queue_flip = intel_gen7_queue_flip;
15555 break;
15556 case 9:
15557 /* Drop through - unsupported since execlist only. */
15558 default:
15559 /* Default just returns -ENODEV to indicate unsupported */
15560 dev_priv->display.queue_flip = intel_default_queue_flip;
15561 }
e70236a8
JB
15562}
15563
b690e96c
JB
15564/*
15565 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
15566 * resume, or other times. This quirk makes sure that's the case for
15567 * affected systems.
15568 */
0206e353 15569static void quirk_pipea_force(struct drm_device *dev)
b690e96c 15570{
fac5e23e 15571 struct drm_i915_private *dev_priv = to_i915(dev);
b690e96c
JB
15572
15573 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
bc0daf48 15574 DRM_INFO("applying pipe a force quirk\n");
b690e96c
JB
15575}
15576
b6b5d049
VS
15577static void quirk_pipeb_force(struct drm_device *dev)
15578{
fac5e23e 15579 struct drm_i915_private *dev_priv = to_i915(dev);
b6b5d049
VS
15580
15581 dev_priv->quirks |= QUIRK_PIPEB_FORCE;
15582 DRM_INFO("applying pipe b force quirk\n");
15583}
15584
435793df
KP
15585/*
15586 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
15587 */
15588static void quirk_ssc_force_disable(struct drm_device *dev)
15589{
fac5e23e 15590 struct drm_i915_private *dev_priv = to_i915(dev);
435793df 15591 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
bc0daf48 15592 DRM_INFO("applying lvds SSC disable quirk\n");
435793df
KP
15593}
15594
4dca20ef 15595/*
5a15ab5b
CE
15596 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
15597 * brightness value
4dca20ef
CE
15598 */
15599static void quirk_invert_brightness(struct drm_device *dev)
15600{
fac5e23e 15601 struct drm_i915_private *dev_priv = to_i915(dev);
4dca20ef 15602 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
bc0daf48 15603 DRM_INFO("applying inverted panel brightness quirk\n");
435793df
KP
15604}
15605
9c72cc6f
SD
15606/* Some VBT's incorrectly indicate no backlight is present */
15607static void quirk_backlight_present(struct drm_device *dev)
15608{
fac5e23e 15609 struct drm_i915_private *dev_priv = to_i915(dev);
9c72cc6f
SD
15610 dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
15611 DRM_INFO("applying backlight present quirk\n");
15612}
15613
b690e96c
JB
15614struct intel_quirk {
15615 int device;
15616 int subsystem_vendor;
15617 int subsystem_device;
15618 void (*hook)(struct drm_device *dev);
15619};
15620
5f85f176
EE
15621/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
15622struct intel_dmi_quirk {
15623 void (*hook)(struct drm_device *dev);
15624 const struct dmi_system_id (*dmi_id_list)[];
15625};
15626
15627static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
15628{
15629 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
15630 return 1;
15631}
15632
15633static const struct intel_dmi_quirk intel_dmi_quirks[] = {
15634 {
15635 .dmi_id_list = &(const struct dmi_system_id[]) {
15636 {
15637 .callback = intel_dmi_reverse_brightness,
15638 .ident = "NCR Corporation",
15639 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
15640 DMI_MATCH(DMI_PRODUCT_NAME, ""),
15641 },
15642 },
15643 { } /* terminating entry */
15644 },
15645 .hook = quirk_invert_brightness,
15646 },
15647};
15648
c43b5634 15649static struct intel_quirk intel_quirks[] = {
b690e96c
JB
15650 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
15651 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
15652
b690e96c
JB
15653 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
15654 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
15655
5f080c0f
VS
15656 /* 830 needs to leave pipe A & dpll A up */
15657 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
15658
b6b5d049
VS
15659 /* 830 needs to leave pipe B & dpll B up */
15660 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
15661
435793df
KP
15662 /* Lenovo U160 cannot use SSC on LVDS */
15663 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
070d329a
MAS
15664
15665 /* Sony Vaio Y cannot use SSC on LVDS */
15666 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
5a15ab5b 15667
be505f64
AH
15668 /* Acer Aspire 5734Z must invert backlight brightness */
15669 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
15670
15671 /* Acer/eMachines G725 */
15672 { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
15673
15674 /* Acer/eMachines e725 */
15675 { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
15676
15677 /* Acer/Packard Bell NCL20 */
15678 { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
15679
15680 /* Acer Aspire 4736Z */
15681 { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
0f540c3a
JN
15682
15683 /* Acer Aspire 5336 */
15684 { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
2e93a1aa
SD
15685
15686 /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
15687 { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
d4967d8c 15688
dfb3d47b
SD
15689 /* Acer C720 Chromebook (Core i3 4005U) */
15690 { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
15691
b2a9601c 15692 /* Apple Macbook 2,1 (Core 2 T7400) */
15693 { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
15694
1b9448b0
JN
15695 /* Apple Macbook 4,1 */
15696 { 0x2a02, 0x106b, 0x00a1, quirk_backlight_present },
15697
d4967d8c
SD
15698 /* Toshiba CB35 Chromebook (Celeron 2955U) */
15699 { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
724cb06f
SD
15700
15701 /* HP Chromebook 14 (Celeron 2955U) */
15702 { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
cf6f0af9
JN
15703
15704 /* Dell Chromebook 11 */
15705 { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
9be64eee
JN
15706
15707 /* Dell Chromebook 11 (2015 version) */
15708 { 0x0a16, 0x1028, 0x0a35, quirk_backlight_present },
b690e96c
JB
15709};
15710
15711static void intel_init_quirks(struct drm_device *dev)
15712{
15713 struct pci_dev *d = dev->pdev;
15714 int i;
15715
15716 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
15717 struct intel_quirk *q = &intel_quirks[i];
15718
15719 if (d->device == q->device &&
15720 (d->subsystem_vendor == q->subsystem_vendor ||
15721 q->subsystem_vendor == PCI_ANY_ID) &&
15722 (d->subsystem_device == q->subsystem_device ||
15723 q->subsystem_device == PCI_ANY_ID))
15724 q->hook(dev);
15725 }
5f85f176
EE
15726 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
15727 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
15728 intel_dmi_quirks[i].hook(dev);
15729 }
b690e96c
JB
15730}
15731
9cce37f4
JB
15732/* Disable the VGA plane that we never use */
15733static void i915_disable_vga(struct drm_device *dev)
15734{
fac5e23e 15735 struct drm_i915_private *dev_priv = to_i915(dev);
9cce37f4 15736 u8 sr1;
f0f59a00 15737 i915_reg_t vga_reg = i915_vgacntrl_reg(dev);
9cce37f4 15738
2b37c616 15739 /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
9cce37f4 15740 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
3fdcf431 15741 outb(SR01, VGA_SR_INDEX);
9cce37f4
JB
15742 sr1 = inb(VGA_SR_DATA);
15743 outb(sr1 | 1<<5, VGA_SR_DATA);
15744 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
15745 udelay(300);
15746
01f5a626 15747 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
9cce37f4
JB
15748 POSTING_READ(vga_reg);
15749}
15750
f817586c
DV
15751void intel_modeset_init_hw(struct drm_device *dev)
15752{
fac5e23e 15753 struct drm_i915_private *dev_priv = to_i915(dev);
1a617b77 15754
b6283055 15755 intel_update_cdclk(dev);
1a617b77
ML
15756
15757 dev_priv->atomic_cdclk_freq = dev_priv->cdclk_freq;
15758
f817586c 15759 intel_init_clock_gating(dev);
f817586c
DV
15760}
15761
d93c0372
MR
15762/*
15763 * Calculate what we think the watermarks should be for the state we've read
15764 * out of the hardware and then immediately program those watermarks so that
15765 * we ensure the hardware settings match our internal state.
15766 *
15767 * We can calculate what we think WM's should be by creating a duplicate of the
15768 * current state (which was constructed during hardware readout) and running it
15769 * through the atomic check code to calculate new watermark values in the
15770 * state object.
15771 */
15772static void sanitize_watermarks(struct drm_device *dev)
15773{
15774 struct drm_i915_private *dev_priv = to_i915(dev);
15775 struct drm_atomic_state *state;
15776 struct drm_crtc *crtc;
15777 struct drm_crtc_state *cstate;
15778 struct drm_modeset_acquire_ctx ctx;
15779 int ret;
15780 int i;
15781
15782 /* Only supported on platforms that use atomic watermark design */
ed4a6a7c 15783 if (!dev_priv->display.optimize_watermarks)
d93c0372
MR
15784 return;
15785
15786 /*
15787 * We need to hold connection_mutex before calling duplicate_state so
15788 * that the connector loop is protected.
15789 */
15790 drm_modeset_acquire_init(&ctx, 0);
15791retry:
0cd1262d 15792 ret = drm_modeset_lock_all_ctx(dev, &ctx);
d93c0372
MR
15793 if (ret == -EDEADLK) {
15794 drm_modeset_backoff(&ctx);
15795 goto retry;
15796 } else if (WARN_ON(ret)) {
0cd1262d 15797 goto fail;
d93c0372
MR
15798 }
15799
15800 state = drm_atomic_helper_duplicate_state(dev, &ctx);
15801 if (WARN_ON(IS_ERR(state)))
0cd1262d 15802 goto fail;
d93c0372 15803
ed4a6a7c
MR
15804 /*
15805 * Hardware readout is the only time we don't want to calculate
15806 * intermediate watermarks (since we don't trust the current
15807 * watermarks).
15808 */
15809 to_intel_atomic_state(state)->skip_intermediate_wm = true;
15810
d93c0372
MR
15811 ret = intel_atomic_check(dev, state);
15812 if (ret) {
15813 /*
15814 * If we fail here, it means that the hardware appears to be
15815 * programmed in a way that shouldn't be possible, given our
15816 * understanding of watermark requirements. This might mean a
15817 * mistake in the hardware readout code or a mistake in the
15818 * watermark calculations for a given platform. Raise a WARN
15819 * so that this is noticeable.
15820 *
15821 * If this actually happens, we'll have to just leave the
15822 * BIOS-programmed watermarks untouched and hope for the best.
15823 */
15824 WARN(true, "Could not determine valid watermarks for inherited state\n");
0cd1262d 15825 goto fail;
d93c0372
MR
15826 }
15827
15828 /* Write calculated watermark values back */
d93c0372
MR
15829 for_each_crtc_in_state(state, crtc, cstate, i) {
15830 struct intel_crtc_state *cs = to_intel_crtc_state(cstate);
15831
ed4a6a7c
MR
15832 cs->wm.need_postvbl_update = true;
15833 dev_priv->display.optimize_watermarks(cs);
d93c0372
MR
15834 }
15835
15836 drm_atomic_state_free(state);
0cd1262d 15837fail:
d93c0372
MR
15838 drm_modeset_drop_locks(&ctx);
15839 drm_modeset_acquire_fini(&ctx);
15840}
15841
79e53945
JB
15842void intel_modeset_init(struct drm_device *dev)
15843{
72e96d64
JL
15844 struct drm_i915_private *dev_priv = to_i915(dev);
15845 struct i915_ggtt *ggtt = &dev_priv->ggtt;
1fe47785 15846 int sprite, ret;
8cc87b75 15847 enum pipe pipe;
46f297fb 15848 struct intel_crtc *crtc;
79e53945
JB
15849
15850 drm_mode_config_init(dev);
15851
15852 dev->mode_config.min_width = 0;
15853 dev->mode_config.min_height = 0;
15854
019d96cb
DA
15855 dev->mode_config.preferred_depth = 24;
15856 dev->mode_config.prefer_shadow = 1;
15857
25bab385
TU
15858 dev->mode_config.allow_fb_modifiers = true;
15859
e6ecefaa 15860 dev->mode_config.funcs = &intel_mode_funcs;
79e53945 15861
b690e96c
JB
15862 intel_init_quirks(dev);
15863
1fa61106
ED
15864 intel_init_pm(dev);
15865
e3c74757
BW
15866 if (INTEL_INFO(dev)->num_pipes == 0)
15867 return;
15868
69f92f67
LW
15869 /*
15870 * There may be no VBT; and if the BIOS enabled SSC we can
15871 * just keep using it to avoid unnecessary flicker. Whereas if the
15872 * BIOS isn't using it, don't assume it will work even if the VBT
15873 * indicates as much.
15874 */
15875 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)) {
15876 bool bios_lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
15877 DREF_SSC1_ENABLE);
15878
15879 if (dev_priv->vbt.lvds_use_ssc != bios_lvds_use_ssc) {
15880 DRM_DEBUG_KMS("SSC %sabled by BIOS, overriding VBT which says %sabled\n",
15881 bios_lvds_use_ssc ? "en" : "dis",
15882 dev_priv->vbt.lvds_use_ssc ? "en" : "dis");
15883 dev_priv->vbt.lvds_use_ssc = bios_lvds_use_ssc;
15884 }
15885 }
15886
a6c45cf0
CW
15887 if (IS_GEN2(dev)) {
15888 dev->mode_config.max_width = 2048;
15889 dev->mode_config.max_height = 2048;
15890 } else if (IS_GEN3(dev)) {
5e4d6fa7
KP
15891 dev->mode_config.max_width = 4096;
15892 dev->mode_config.max_height = 4096;
79e53945 15893 } else {
a6c45cf0
CW
15894 dev->mode_config.max_width = 8192;
15895 dev->mode_config.max_height = 8192;
79e53945 15896 }
068be561 15897
dc41c154
VS
15898 if (IS_845G(dev) || IS_I865G(dev)) {
15899 dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
15900 dev->mode_config.cursor_height = 1023;
15901 } else if (IS_GEN2(dev)) {
068be561
DL
15902 dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
15903 dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
15904 } else {
15905 dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
15906 dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
15907 }
15908
72e96d64 15909 dev->mode_config.fb_base = ggtt->mappable_base;
79e53945 15910
28c97730 15911 DRM_DEBUG_KMS("%d display pipe%s available.\n",
7eb552ae
BW
15912 INTEL_INFO(dev)->num_pipes,
15913 INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
79e53945 15914
055e393f 15915 for_each_pipe(dev_priv, pipe) {
8cc87b75 15916 intel_crtc_init(dev, pipe);
3bdcfc0c 15917 for_each_sprite(dev_priv, pipe, sprite) {
1fe47785 15918 ret = intel_plane_init(dev, pipe, sprite);
7f1f3851 15919 if (ret)
06da8da2 15920 DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
1fe47785 15921 pipe_name(pipe), sprite_name(pipe, sprite), ret);
7f1f3851 15922 }
79e53945
JB
15923 }
15924
bfa7df01
VS
15925 intel_update_czclk(dev_priv);
15926 intel_update_cdclk(dev);
15927
e72f9fbf 15928 intel_shared_dpll_init(dev);
ee7b9f93 15929
b2045352
VS
15930 if (dev_priv->max_cdclk_freq == 0)
15931 intel_update_max_cdclk(dev);
15932
9cce37f4
JB
15933 /* Just disable it once at startup */
15934 i915_disable_vga(dev);
79e53945 15935 intel_setup_outputs(dev);
11be49eb 15936
6e9f798d 15937 drm_modeset_lock_all(dev);
043e9bda 15938 intel_modeset_setup_hw_state(dev);
6e9f798d 15939 drm_modeset_unlock_all(dev);
46f297fb 15940
d3fcc808 15941 for_each_intel_crtc(dev, crtc) {
eeebeac5
ML
15942 struct intel_initial_plane_config plane_config = {};
15943
46f297fb
JB
15944 if (!crtc->active)
15945 continue;
15946
46f297fb 15947 /*
46f297fb
JB
15948 * Note that reserving the BIOS fb up front prevents us
15949 * from stuffing other stolen allocations like the ring
15950 * on top. This prevents some ugliness at boot time, and
15951 * can even allow for smooth boot transitions if the BIOS
15952 * fb is large enough for the active pipe configuration.
15953 */
eeebeac5
ML
15954 dev_priv->display.get_initial_plane_config(crtc,
15955 &plane_config);
15956
15957 /*
15958 * If the fb is shared between multiple heads, we'll
15959 * just get the first one.
15960 */
15961 intel_find_initial_plane_obj(crtc, &plane_config);
46f297fb 15962 }
d93c0372
MR
15963
15964 /*
15965 * Make sure hardware watermarks really match the state we read out.
15966 * Note that we need to do this after reconstructing the BIOS fb's
15967 * since the watermark calculation done here will use pstate->fb.
15968 */
15969 sanitize_watermarks(dev);
2c7111db
CW
15970}
15971
7fad798e
DV
15972static void intel_enable_pipe_a(struct drm_device *dev)
15973{
15974 struct intel_connector *connector;
15975 struct drm_connector *crt = NULL;
15976 struct intel_load_detect_pipe load_detect_temp;
208bf9fd 15977 struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
7fad798e
DV
15978
15979 /* We can't just switch on the pipe A, we need to set things up with a
15980 * proper mode and output configuration. As a gross hack, enable pipe A
15981 * by enabling the load detect pipe once. */
3a3371ff 15982 for_each_intel_connector(dev, connector) {
7fad798e
DV
15983 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
15984 crt = &connector->base;
15985 break;
15986 }
15987 }
15988
15989 if (!crt)
15990 return;
15991
208bf9fd 15992 if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
49172fee 15993 intel_release_load_detect_pipe(crt, &load_detect_temp, ctx);
7fad798e
DV
15994}
15995
fa555837
DV
15996static bool
15997intel_check_plane_mapping(struct intel_crtc *crtc)
15998{
7eb552ae 15999 struct drm_device *dev = crtc->base.dev;
fac5e23e 16000 struct drm_i915_private *dev_priv = to_i915(dev);
649636ef 16001 u32 val;
fa555837 16002
7eb552ae 16003 if (INTEL_INFO(dev)->num_pipes == 1)
fa555837
DV
16004 return true;
16005
649636ef 16006 val = I915_READ(DSPCNTR(!crtc->plane));
fa555837
DV
16007
16008 if ((val & DISPLAY_PLANE_ENABLE) &&
16009 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
16010 return false;
16011
16012 return true;
16013}
16014
02e93c35
VS
16015static bool intel_crtc_has_encoders(struct intel_crtc *crtc)
16016{
16017 struct drm_device *dev = crtc->base.dev;
16018 struct intel_encoder *encoder;
16019
16020 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
16021 return true;
16022
16023 return false;
16024}
16025
dd756198
VS
16026static bool intel_encoder_has_connectors(struct intel_encoder *encoder)
16027{
16028 struct drm_device *dev = encoder->base.dev;
16029 struct intel_connector *connector;
16030
16031 for_each_connector_on_encoder(dev, &encoder->base, connector)
16032 return true;
16033
16034 return false;
16035}
16036
a168f5b3
VS
16037static bool has_pch_trancoder(struct drm_i915_private *dev_priv,
16038 enum transcoder pch_transcoder)
16039{
16040 return HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv) ||
16041 (HAS_PCH_LPT_H(dev_priv) && pch_transcoder == TRANSCODER_A);
16042}
16043
24929352
DV
16044static void intel_sanitize_crtc(struct intel_crtc *crtc)
16045{
16046 struct drm_device *dev = crtc->base.dev;
fac5e23e 16047 struct drm_i915_private *dev_priv = to_i915(dev);
4d1de975 16048 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
24929352 16049
24929352 16050 /* Clear any frame start delays used for debugging left by the BIOS */
4d1de975
JN
16051 if (!transcoder_is_dsi(cpu_transcoder)) {
16052 i915_reg_t reg = PIPECONF(cpu_transcoder);
16053
16054 I915_WRITE(reg,
16055 I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
16056 }
24929352 16057
d3eaf884 16058 /* restore vblank interrupts to correct state */
9625604c 16059 drm_crtc_vblank_reset(&crtc->base);
d297e103 16060 if (crtc->active) {
f9cd7b88
VS
16061 struct intel_plane *plane;
16062
9625604c 16063 drm_crtc_vblank_on(&crtc->base);
f9cd7b88
VS
16064
16065 /* Disable everything but the primary plane */
16066 for_each_intel_plane_on_crtc(dev, crtc, plane) {
16067 if (plane->base.type == DRM_PLANE_TYPE_PRIMARY)
16068 continue;
16069
16070 plane->disable_plane(&plane->base, &crtc->base);
16071 }
9625604c 16072 }
d3eaf884 16073
24929352 16074 /* We need to sanitize the plane -> pipe mapping first because this will
fa555837
DV
16075 * disable the crtc (and hence change the state) if it is wrong. Note
16076 * that gen4+ has a fixed plane -> pipe mapping. */
16077 if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
24929352
DV
16078 bool plane;
16079
78108b7c
VS
16080 DRM_DEBUG_KMS("[CRTC:%d:%s] wrong plane connection detected!\n",
16081 crtc->base.base.id, crtc->base.name);
24929352
DV
16082
16083 /* Pipe has the wrong plane attached and the plane is active.
16084 * Temporarily change the plane mapping and disable everything
16085 * ... */
16086 plane = crtc->plane;
b70709a6 16087 to_intel_plane_state(crtc->base.primary->state)->visible = true;
24929352 16088 crtc->plane = !plane;
b17d48e2 16089 intel_crtc_disable_noatomic(&crtc->base);
24929352 16090 crtc->plane = plane;
24929352 16091 }
24929352 16092
7fad798e
DV
16093 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
16094 crtc->pipe == PIPE_A && !crtc->active) {
16095 /* BIOS forgot to enable pipe A, this mostly happens after
16096 * resume. Force-enable the pipe to fix this, the update_dpms
16097 * call below we restore the pipe to the right state, but leave
16098 * the required bits on. */
16099 intel_enable_pipe_a(dev);
16100 }
16101
24929352
DV
16102 /* Adjust the state of the output pipe according to whether we
16103 * have active connectors/encoders. */
842e0307 16104 if (crtc->active && !intel_crtc_has_encoders(crtc))
b17d48e2 16105 intel_crtc_disable_noatomic(&crtc->base);
24929352 16106
a3ed6aad 16107 if (crtc->active || HAS_GMCH_DISPLAY(dev)) {
4cc31489
DV
16108 /*
16109 * We start out with underrun reporting disabled to avoid races.
16110 * For correct bookkeeping mark this on active crtcs.
16111 *
c5ab3bc0
DV
16112 * Also on gmch platforms we dont have any hardware bits to
16113 * disable the underrun reporting. Which means we need to start
16114 * out with underrun reporting disabled also on inactive pipes,
16115 * since otherwise we'll complain about the garbage we read when
16116 * e.g. coming up after runtime pm.
16117 *
4cc31489
DV
16118 * No protection against concurrent access is required - at
16119 * worst a fifo underrun happens which also sets this to false.
16120 */
16121 crtc->cpu_fifo_underrun_disabled = true;
a168f5b3
VS
16122 /*
16123 * We track the PCH trancoder underrun reporting state
16124 * within the crtc. With crtc for pipe A housing the underrun
16125 * reporting state for PCH transcoder A, crtc for pipe B housing
16126 * it for PCH transcoder B, etc. LPT-H has only PCH transcoder A,
16127 * and marking underrun reporting as disabled for the non-existing
16128 * PCH transcoders B and C would prevent enabling the south
16129 * error interrupt (see cpt_can_enable_serr_int()).
16130 */
16131 if (has_pch_trancoder(dev_priv, (enum transcoder)crtc->pipe))
16132 crtc->pch_fifo_underrun_disabled = true;
4cc31489 16133 }
24929352
DV
16134}
16135
16136static void intel_sanitize_encoder(struct intel_encoder *encoder)
16137{
16138 struct intel_connector *connector;
16139 struct drm_device *dev = encoder->base.dev;
16140
16141 /* We need to check both for a crtc link (meaning that the
16142 * encoder is active and trying to read from a pipe) and the
16143 * pipe itself being active. */
16144 bool has_active_crtc = encoder->base.crtc &&
16145 to_intel_crtc(encoder->base.crtc)->active;
16146
dd756198 16147 if (intel_encoder_has_connectors(encoder) && !has_active_crtc) {
24929352
DV
16148 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
16149 encoder->base.base.id,
8e329a03 16150 encoder->base.name);
24929352
DV
16151
16152 /* Connector is active, but has no active pipe. This is
16153 * fallout from our resume register restoring. Disable
16154 * the encoder manually again. */
16155 if (encoder->base.crtc) {
16156 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
16157 encoder->base.base.id,
8e329a03 16158 encoder->base.name);
24929352 16159 encoder->disable(encoder);
a62d1497
VS
16160 if (encoder->post_disable)
16161 encoder->post_disable(encoder);
24929352 16162 }
7f1950fb 16163 encoder->base.crtc = NULL;
24929352
DV
16164
16165 /* Inconsistent output/port/pipe state happens presumably due to
16166 * a bug in one of the get_hw_state functions. Or someplace else
16167 * in our code, like the register restore mess on resume. Clamp
16168 * things to off as a safer default. */
3a3371ff 16169 for_each_intel_connector(dev, connector) {
24929352
DV
16170 if (connector->encoder != encoder)
16171 continue;
7f1950fb
EE
16172 connector->base.dpms = DRM_MODE_DPMS_OFF;
16173 connector->base.encoder = NULL;
24929352
DV
16174 }
16175 }
16176 /* Enabled encoders without active connectors will be fixed in
16177 * the crtc fixup. */
16178}
16179
04098753 16180void i915_redisable_vga_power_on(struct drm_device *dev)
0fde901f 16181{
fac5e23e 16182 struct drm_i915_private *dev_priv = to_i915(dev);
f0f59a00 16183 i915_reg_t vga_reg = i915_vgacntrl_reg(dev);
0fde901f 16184
04098753
ID
16185 if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
16186 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
16187 i915_disable_vga(dev);
16188 }
16189}
16190
16191void i915_redisable_vga(struct drm_device *dev)
16192{
fac5e23e 16193 struct drm_i915_private *dev_priv = to_i915(dev);
04098753 16194
8dc8a27c
PZ
16195 /* This function can be called both from intel_modeset_setup_hw_state or
16196 * at a very early point in our resume sequence, where the power well
16197 * structures are not yet restored. Since this function is at a very
16198 * paranoid "someone might have enabled VGA while we were not looking"
16199 * level, just check if the power well is enabled instead of trying to
16200 * follow the "don't touch the power well if we don't need it" policy
16201 * the rest of the driver uses. */
6392f847 16202 if (!intel_display_power_get_if_enabled(dev_priv, POWER_DOMAIN_VGA))
8dc8a27c
PZ
16203 return;
16204
04098753 16205 i915_redisable_vga_power_on(dev);
6392f847
ID
16206
16207 intel_display_power_put(dev_priv, POWER_DOMAIN_VGA);
0fde901f
KM
16208}
16209
f9cd7b88 16210static bool primary_get_hw_state(struct intel_plane *plane)
98ec7739 16211{
f9cd7b88 16212 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
98ec7739 16213
f9cd7b88 16214 return I915_READ(DSPCNTR(plane->plane)) & DISPLAY_PLANE_ENABLE;
d032ffa0
ML
16215}
16216
f9cd7b88
VS
16217/* FIXME read out full plane state for all planes */
16218static void readout_plane_state(struct intel_crtc *crtc)
d032ffa0 16219{
b26d3ea3 16220 struct drm_plane *primary = crtc->base.primary;
f9cd7b88 16221 struct intel_plane_state *plane_state =
b26d3ea3 16222 to_intel_plane_state(primary->state);
d032ffa0 16223
19b8d387 16224 plane_state->visible = crtc->active &&
b26d3ea3
ML
16225 primary_get_hw_state(to_intel_plane(primary));
16226
16227 if (plane_state->visible)
16228 crtc->base.state->plane_mask |= 1 << drm_plane_index(primary);
98ec7739
VS
16229}
16230
30e984df 16231static void intel_modeset_readout_hw_state(struct drm_device *dev)
24929352 16232{
fac5e23e 16233 struct drm_i915_private *dev_priv = to_i915(dev);
24929352 16234 enum pipe pipe;
24929352
DV
16235 struct intel_crtc *crtc;
16236 struct intel_encoder *encoder;
16237 struct intel_connector *connector;
5358901f 16238 int i;
24929352 16239
565602d7
ML
16240 dev_priv->active_crtcs = 0;
16241
d3fcc808 16242 for_each_intel_crtc(dev, crtc) {
565602d7
ML
16243 struct intel_crtc_state *crtc_state = crtc->config;
16244 int pixclk = 0;
3b117c8f 16245
ec2dc6a0 16246 __drm_atomic_helper_crtc_destroy_state(&crtc_state->base);
565602d7
ML
16247 memset(crtc_state, 0, sizeof(*crtc_state));
16248 crtc_state->base.crtc = &crtc->base;
24929352 16249
565602d7
ML
16250 crtc_state->base.active = crtc_state->base.enable =
16251 dev_priv->display.get_pipe_config(crtc, crtc_state);
16252
16253 crtc->base.enabled = crtc_state->base.enable;
16254 crtc->active = crtc_state->base.active;
16255
16256 if (crtc_state->base.active) {
16257 dev_priv->active_crtcs |= 1 << crtc->pipe;
16258
c89e39f3 16259 if (INTEL_GEN(dev_priv) >= 9 || IS_BROADWELL(dev_priv))
565602d7 16260 pixclk = ilk_pipe_pixel_rate(crtc_state);
9558d15d 16261 else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
565602d7
ML
16262 pixclk = crtc_state->base.adjusted_mode.crtc_clock;
16263 else
16264 WARN_ON(dev_priv->display.modeset_calc_cdclk);
9558d15d
VS
16265
16266 /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
16267 if (IS_BROADWELL(dev_priv) && crtc_state->ips_enabled)
16268 pixclk = DIV_ROUND_UP(pixclk * 100, 95);
565602d7
ML
16269 }
16270
16271 dev_priv->min_pixclk[crtc->pipe] = pixclk;
b70709a6 16272
f9cd7b88 16273 readout_plane_state(crtc);
24929352 16274
78108b7c
VS
16275 DRM_DEBUG_KMS("[CRTC:%d:%s] hw state readout: %s\n",
16276 crtc->base.base.id, crtc->base.name,
24929352
DV
16277 crtc->active ? "enabled" : "disabled");
16278 }
16279
5358901f
DV
16280 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
16281 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
16282
2edd6443
ACO
16283 pll->on = pll->funcs.get_hw_state(dev_priv, pll,
16284 &pll->config.hw_state);
3e369b76 16285 pll->config.crtc_mask = 0;
d3fcc808 16286 for_each_intel_crtc(dev, crtc) {
2dd66ebd 16287 if (crtc->active && crtc->config->shared_dpll == pll)
3e369b76 16288 pll->config.crtc_mask |= 1 << crtc->pipe;
5358901f 16289 }
2dd66ebd 16290 pll->active_mask = pll->config.crtc_mask;
5358901f 16291
1e6f2ddc 16292 DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
3e369b76 16293 pll->name, pll->config.crtc_mask, pll->on);
5358901f
DV
16294 }
16295
b2784e15 16296 for_each_intel_encoder(dev, encoder) {
24929352
DV
16297 pipe = 0;
16298
16299 if (encoder->get_hw_state(encoder, &pipe)) {
045ac3b5
JB
16300 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
16301 encoder->base.crtc = &crtc->base;
253c84c8 16302 crtc->config->output_types |= 1 << encoder->type;
6e3c9717 16303 encoder->get_config(encoder, crtc->config);
24929352
DV
16304 } else {
16305 encoder->base.crtc = NULL;
16306 }
16307
6f2bcceb 16308 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
24929352 16309 encoder->base.base.id,
8e329a03 16310 encoder->base.name,
24929352 16311 encoder->base.crtc ? "enabled" : "disabled",
6f2bcceb 16312 pipe_name(pipe));
24929352
DV
16313 }
16314
3a3371ff 16315 for_each_intel_connector(dev, connector) {
24929352
DV
16316 if (connector->get_hw_state(connector)) {
16317 connector->base.dpms = DRM_MODE_DPMS_ON;
2aa974c9
ML
16318
16319 encoder = connector->encoder;
16320 connector->base.encoder = &encoder->base;
16321
16322 if (encoder->base.crtc &&
16323 encoder->base.crtc->state->active) {
16324 /*
16325 * This has to be done during hardware readout
16326 * because anything calling .crtc_disable may
16327 * rely on the connector_mask being accurate.
16328 */
16329 encoder->base.crtc->state->connector_mask |=
16330 1 << drm_connector_index(&connector->base);
e87a52b3
ML
16331 encoder->base.crtc->state->encoder_mask |=
16332 1 << drm_encoder_index(&encoder->base);
2aa974c9
ML
16333 }
16334
24929352
DV
16335 } else {
16336 connector->base.dpms = DRM_MODE_DPMS_OFF;
16337 connector->base.encoder = NULL;
16338 }
16339 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
16340 connector->base.base.id,
c23cc417 16341 connector->base.name,
24929352
DV
16342 connector->base.encoder ? "enabled" : "disabled");
16343 }
7f4c6284
VS
16344
16345 for_each_intel_crtc(dev, crtc) {
16346 crtc->base.hwmode = crtc->config->base.adjusted_mode;
16347
16348 memset(&crtc->base.mode, 0, sizeof(crtc->base.mode));
16349 if (crtc->base.state->active) {
16350 intel_mode_from_pipe_config(&crtc->base.mode, crtc->config);
16351 intel_mode_from_pipe_config(&crtc->base.state->adjusted_mode, crtc->config);
16352 WARN_ON(drm_atomic_set_mode_for_crtc(crtc->base.state, &crtc->base.mode));
16353
16354 /*
16355 * The initial mode needs to be set in order to keep
16356 * the atomic core happy. It wants a valid mode if the
16357 * crtc's enabled, so we do the above call.
16358 *
16359 * At this point some state updated by the connectors
16360 * in their ->detect() callback has not run yet, so
16361 * no recalculation can be done yet.
16362 *
16363 * Even if we could do a recalculation and modeset
16364 * right now it would cause a double modeset if
16365 * fbdev or userspace chooses a different initial mode.
16366 *
16367 * If that happens, someone indicated they wanted a
16368 * mode change, which means it's safe to do a full
16369 * recalculation.
16370 */
16371 crtc->base.state->mode.private_flags = I915_MODE_FLAG_INHERITED;
9eca6832
VS
16372
16373 drm_calc_timestamping_constants(&crtc->base, &crtc->base.hwmode);
16374 update_scanline_offset(crtc);
7f4c6284 16375 }
e3b247da
VS
16376
16377 intel_pipe_config_sanity_check(dev_priv, crtc->config);
7f4c6284 16378 }
30e984df
DV
16379}
16380
043e9bda
ML
16381/* Scan out the current hw modeset state,
16382 * and sanitizes it to the current state
16383 */
16384static void
16385intel_modeset_setup_hw_state(struct drm_device *dev)
30e984df 16386{
fac5e23e 16387 struct drm_i915_private *dev_priv = to_i915(dev);
30e984df 16388 enum pipe pipe;
30e984df
DV
16389 struct intel_crtc *crtc;
16390 struct intel_encoder *encoder;
35c95375 16391 int i;
30e984df
DV
16392
16393 intel_modeset_readout_hw_state(dev);
24929352
DV
16394
16395 /* HW state is read out, now we need to sanitize this mess. */
b2784e15 16396 for_each_intel_encoder(dev, encoder) {
24929352
DV
16397 intel_sanitize_encoder(encoder);
16398 }
16399
055e393f 16400 for_each_pipe(dev_priv, pipe) {
24929352
DV
16401 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
16402 intel_sanitize_crtc(crtc);
6e3c9717
ACO
16403 intel_dump_pipe_config(crtc, crtc->config,
16404 "[setup_hw_state]");
24929352 16405 }
9a935856 16406
d29b2f9d
ACO
16407 intel_modeset_update_connector_atomic_state(dev);
16408
35c95375
DV
16409 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
16410 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
16411
2dd66ebd 16412 if (!pll->on || pll->active_mask)
35c95375
DV
16413 continue;
16414
16415 DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
16416
2edd6443 16417 pll->funcs.disable(dev_priv, pll);
35c95375
DV
16418 pll->on = false;
16419 }
16420
666a4537 16421 if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev))
6eb1a681
VS
16422 vlv_wm_get_hw_state(dev);
16423 else if (IS_GEN9(dev))
3078999f
PB
16424 skl_wm_get_hw_state(dev);
16425 else if (HAS_PCH_SPLIT(dev))
243e6a44 16426 ilk_wm_get_hw_state(dev);
292b990e
ML
16427
16428 for_each_intel_crtc(dev, crtc) {
16429 unsigned long put_domains;
16430
74bff5f9 16431 put_domains = modeset_get_crtc_power_domains(&crtc->base, crtc->config);
292b990e
ML
16432 if (WARN_ON(put_domains))
16433 modeset_put_power_domains(dev_priv, put_domains);
16434 }
16435 intel_display_set_init_power(dev_priv, false);
010cf73d
PZ
16436
16437 intel_fbc_init_pipe_state(dev_priv);
043e9bda 16438}
7d0bc1ea 16439
043e9bda
ML
16440void intel_display_resume(struct drm_device *dev)
16441{
e2c8b870
ML
16442 struct drm_i915_private *dev_priv = to_i915(dev);
16443 struct drm_atomic_state *state = dev_priv->modeset_restore_state;
16444 struct drm_modeset_acquire_ctx ctx;
043e9bda 16445 int ret;
f30da187 16446
e2c8b870 16447 dev_priv->modeset_restore_state = NULL;
73974893
ML
16448 if (state)
16449 state->acquire_ctx = &ctx;
043e9bda 16450
ea49c9ac
ML
16451 /*
16452 * This is a cludge because with real atomic modeset mode_config.mutex
16453 * won't be taken. Unfortunately some probed state like
16454 * audio_codec_enable is still protected by mode_config.mutex, so lock
16455 * it here for now.
16456 */
16457 mutex_lock(&dev->mode_config.mutex);
e2c8b870 16458 drm_modeset_acquire_init(&ctx, 0);
043e9bda 16459
73974893
ML
16460 while (1) {
16461 ret = drm_modeset_lock_all_ctx(dev, &ctx);
16462 if (ret != -EDEADLK)
16463 break;
043e9bda 16464
e2c8b870 16465 drm_modeset_backoff(&ctx);
e2c8b870 16466 }
043e9bda 16467
73974893
ML
16468 if (!ret)
16469 ret = __intel_display_resume(dev, state);
16470
e2c8b870
ML
16471 drm_modeset_drop_locks(&ctx);
16472 drm_modeset_acquire_fini(&ctx);
ea49c9ac 16473 mutex_unlock(&dev->mode_config.mutex);
043e9bda 16474
e2c8b870
ML
16475 if (ret) {
16476 DRM_ERROR("Restoring old state failed with %i\n", ret);
16477 drm_atomic_state_free(state);
16478 }
2c7111db
CW
16479}
16480
16481void intel_modeset_gem_init(struct drm_device *dev)
16482{
dc97997a 16483 struct drm_i915_private *dev_priv = to_i915(dev);
484b41dd 16484 struct drm_crtc *c;
2ff8fde1 16485 struct drm_i915_gem_object *obj;
e0d6149b 16486 int ret;
484b41dd 16487
dc97997a 16488 intel_init_gt_powersave(dev_priv);
ae48434c 16489
1833b134 16490 intel_modeset_init_hw(dev);
02e792fb 16491
1ee8da6d 16492 intel_setup_overlay(dev_priv);
484b41dd
JB
16493
16494 /*
16495 * Make sure any fbs we allocated at startup are properly
16496 * pinned & fenced. When we do the allocation it's too early
16497 * for this.
16498 */
70e1e0ec 16499 for_each_crtc(dev, c) {
2ff8fde1
MR
16500 obj = intel_fb_obj(c->primary->fb);
16501 if (obj == NULL)
484b41dd
JB
16502 continue;
16503
e0d6149b 16504 mutex_lock(&dev->struct_mutex);
3465c580
VS
16505 ret = intel_pin_and_fence_fb_obj(c->primary->fb,
16506 c->primary->state->rotation);
e0d6149b
TU
16507 mutex_unlock(&dev->struct_mutex);
16508 if (ret) {
484b41dd
JB
16509 DRM_ERROR("failed to pin boot fb on pipe %d\n",
16510 to_intel_crtc(c)->pipe);
66e514c1 16511 drm_framebuffer_unreference(c->primary->fb);
5a21b665 16512 c->primary->fb = NULL;
36750f28 16513 c->primary->crtc = c->primary->state->crtc = NULL;
5a21b665 16514 update_state_fb(c->primary);
36750f28 16515 c->state->plane_mask &= ~(1 << drm_plane_index(c->primary));
484b41dd
JB
16516 }
16517 }
1ebaa0b9
CW
16518}
16519
16520int intel_connector_register(struct drm_connector *connector)
16521{
16522 struct intel_connector *intel_connector = to_intel_connector(connector);
16523 int ret;
16524
16525 ret = intel_backlight_device_register(intel_connector);
16526 if (ret)
16527 goto err;
16528
16529 return 0;
0962c3c9 16530
1ebaa0b9
CW
16531err:
16532 return ret;
79e53945
JB
16533}
16534
c191eca1 16535void intel_connector_unregister(struct drm_connector *connector)
4932e2c3 16536{
e63d87c0 16537 struct intel_connector *intel_connector = to_intel_connector(connector);
4932e2c3 16538
e63d87c0 16539 intel_backlight_device_unregister(intel_connector);
4932e2c3 16540 intel_panel_destroy_backlight(connector);
4932e2c3
ID
16541}
16542
79e53945
JB
16543void intel_modeset_cleanup(struct drm_device *dev)
16544{
fac5e23e 16545 struct drm_i915_private *dev_priv = to_i915(dev);
652c393a 16546
dc97997a 16547 intel_disable_gt_powersave(dev_priv);
2eb5252e 16548
fd0c0642
DV
16549 /*
16550 * Interrupts and polling as the first thing to avoid creating havoc.
2eb5252e 16551 * Too much stuff here (turning of connectors, ...) would
fd0c0642
DV
16552 * experience fancy races otherwise.
16553 */
2aeb7d3a 16554 intel_irq_uninstall(dev_priv);
eb21b92b 16555
fd0c0642
DV
16556 /*
16557 * Due to the hpd irq storm handling the hotplug work can re-arm the
16558 * poll handlers. Hence disable polling after hpd handling is shut down.
16559 */
f87ea761 16560 drm_kms_helper_poll_fini(dev);
fd0c0642 16561
723bfd70
JB
16562 intel_unregister_dsm_handler();
16563
c937ab3e 16564 intel_fbc_global_disable(dev_priv);
69341a5e 16565
1630fe75
CW
16566 /* flush any delayed tasks or pending work */
16567 flush_scheduled_work();
16568
79e53945 16569 drm_mode_config_cleanup(dev);
4d7bb011 16570
1ee8da6d 16571 intel_cleanup_overlay(dev_priv);
ae48434c 16572
dc97997a 16573 intel_cleanup_gt_powersave(dev_priv);
f5949141
DV
16574
16575 intel_teardown_gmbus(dev);
79e53945
JB
16576}
16577
df0e9248
CW
16578void intel_connector_attach_encoder(struct intel_connector *connector,
16579 struct intel_encoder *encoder)
16580{
16581 connector->encoder = encoder;
16582 drm_mode_connector_attach_encoder(&connector->base,
16583 &encoder->base);
79e53945 16584}
28d52043
DA
16585
16586/*
16587 * set vga decode state - true == enable VGA decode
16588 */
16589int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
16590{
fac5e23e 16591 struct drm_i915_private *dev_priv = to_i915(dev);
a885b3cc 16592 unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
28d52043
DA
16593 u16 gmch_ctrl;
16594
75fa041d
CW
16595 if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
16596 DRM_ERROR("failed to read control word\n");
16597 return -EIO;
16598 }
16599
c0cc8a55
CW
16600 if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
16601 return 0;
16602
28d52043
DA
16603 if (state)
16604 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
16605 else
16606 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
75fa041d
CW
16607
16608 if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
16609 DRM_ERROR("failed to write control word\n");
16610 return -EIO;
16611 }
16612
28d52043
DA
16613 return 0;
16614}
c4a1d9e4 16615
c4a1d9e4 16616struct intel_display_error_state {
ff57f1b0
PZ
16617
16618 u32 power_well_driver;
16619
63b66e5b
CW
16620 int num_transcoders;
16621
c4a1d9e4
CW
16622 struct intel_cursor_error_state {
16623 u32 control;
16624 u32 position;
16625 u32 base;
16626 u32 size;
52331309 16627 } cursor[I915_MAX_PIPES];
c4a1d9e4
CW
16628
16629 struct intel_pipe_error_state {
ddf9c536 16630 bool power_domain_on;
c4a1d9e4 16631 u32 source;
f301b1e1 16632 u32 stat;
52331309 16633 } pipe[I915_MAX_PIPES];
c4a1d9e4
CW
16634
16635 struct intel_plane_error_state {
16636 u32 control;
16637 u32 stride;
16638 u32 size;
16639 u32 pos;
16640 u32 addr;
16641 u32 surface;
16642 u32 tile_offset;
52331309 16643 } plane[I915_MAX_PIPES];
63b66e5b
CW
16644
16645 struct intel_transcoder_error_state {
ddf9c536 16646 bool power_domain_on;
63b66e5b
CW
16647 enum transcoder cpu_transcoder;
16648
16649 u32 conf;
16650
16651 u32 htotal;
16652 u32 hblank;
16653 u32 hsync;
16654 u32 vtotal;
16655 u32 vblank;
16656 u32 vsync;
16657 } transcoder[4];
c4a1d9e4
CW
16658};
16659
16660struct intel_display_error_state *
c033666a 16661intel_display_capture_error_state(struct drm_i915_private *dev_priv)
c4a1d9e4 16662{
c4a1d9e4 16663 struct intel_display_error_state *error;
63b66e5b
CW
16664 int transcoders[] = {
16665 TRANSCODER_A,
16666 TRANSCODER_B,
16667 TRANSCODER_C,
16668 TRANSCODER_EDP,
16669 };
c4a1d9e4
CW
16670 int i;
16671
c033666a 16672 if (INTEL_INFO(dev_priv)->num_pipes == 0)
63b66e5b
CW
16673 return NULL;
16674
9d1cb914 16675 error = kzalloc(sizeof(*error), GFP_ATOMIC);
c4a1d9e4
CW
16676 if (error == NULL)
16677 return NULL;
16678
c033666a 16679 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
ff57f1b0
PZ
16680 error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
16681
055e393f 16682 for_each_pipe(dev_priv, i) {
ddf9c536 16683 error->pipe[i].power_domain_on =
f458ebbc
DV
16684 __intel_display_power_is_enabled(dev_priv,
16685 POWER_DOMAIN_PIPE(i));
ddf9c536 16686 if (!error->pipe[i].power_domain_on)
9d1cb914
PZ
16687 continue;
16688
5efb3e28
VS
16689 error->cursor[i].control = I915_READ(CURCNTR(i));
16690 error->cursor[i].position = I915_READ(CURPOS(i));
16691 error->cursor[i].base = I915_READ(CURBASE(i));
c4a1d9e4
CW
16692
16693 error->plane[i].control = I915_READ(DSPCNTR(i));
16694 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
c033666a 16695 if (INTEL_GEN(dev_priv) <= 3) {
51889b35 16696 error->plane[i].size = I915_READ(DSPSIZE(i));
80ca378b
PZ
16697 error->plane[i].pos = I915_READ(DSPPOS(i));
16698 }
c033666a 16699 if (INTEL_GEN(dev_priv) <= 7 && !IS_HASWELL(dev_priv))
ca291363 16700 error->plane[i].addr = I915_READ(DSPADDR(i));
c033666a 16701 if (INTEL_GEN(dev_priv) >= 4) {
c4a1d9e4
CW
16702 error->plane[i].surface = I915_READ(DSPSURF(i));
16703 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
16704 }
16705
c4a1d9e4 16706 error->pipe[i].source = I915_READ(PIPESRC(i));
f301b1e1 16707
c033666a 16708 if (HAS_GMCH_DISPLAY(dev_priv))
f301b1e1 16709 error->pipe[i].stat = I915_READ(PIPESTAT(i));
63b66e5b
CW
16710 }
16711
4d1de975 16712 /* Note: this does not include DSI transcoders. */
c033666a 16713 error->num_transcoders = INTEL_INFO(dev_priv)->num_pipes;
2d1fe073 16714 if (HAS_DDI(dev_priv))
63b66e5b
CW
16715 error->num_transcoders++; /* Account for eDP. */
16716
16717 for (i = 0; i < error->num_transcoders; i++) {
16718 enum transcoder cpu_transcoder = transcoders[i];
16719
ddf9c536 16720 error->transcoder[i].power_domain_on =
f458ebbc 16721 __intel_display_power_is_enabled(dev_priv,
38cc1daf 16722 POWER_DOMAIN_TRANSCODER(cpu_transcoder));
ddf9c536 16723 if (!error->transcoder[i].power_domain_on)
9d1cb914
PZ
16724 continue;
16725
63b66e5b
CW
16726 error->transcoder[i].cpu_transcoder = cpu_transcoder;
16727
16728 error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
16729 error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
16730 error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
16731 error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
16732 error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
16733 error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
16734 error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
c4a1d9e4
CW
16735 }
16736
16737 return error;
16738}
16739
edc3d884
MK
16740#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
16741
c4a1d9e4 16742void
edc3d884 16743intel_display_print_error_state(struct drm_i915_error_state_buf *m,
c4a1d9e4
CW
16744 struct drm_device *dev,
16745 struct intel_display_error_state *error)
16746{
fac5e23e 16747 struct drm_i915_private *dev_priv = to_i915(dev);
c4a1d9e4
CW
16748 int i;
16749
63b66e5b
CW
16750 if (!error)
16751 return;
16752
edc3d884 16753 err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
190be112 16754 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
edc3d884 16755 err_printf(m, "PWR_WELL_CTL2: %08x\n",
ff57f1b0 16756 error->power_well_driver);
055e393f 16757 for_each_pipe(dev_priv, i) {
edc3d884 16758 err_printf(m, "Pipe [%d]:\n", i);
ddf9c536 16759 err_printf(m, " Power: %s\n",
87ad3212 16760 onoff(error->pipe[i].power_domain_on));
edc3d884 16761 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
f301b1e1 16762 err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
edc3d884
MK
16763
16764 err_printf(m, "Plane [%d]:\n", i);
16765 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
16766 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
80ca378b 16767 if (INTEL_INFO(dev)->gen <= 3) {
edc3d884
MK
16768 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
16769 err_printf(m, " POS: %08x\n", error->plane[i].pos);
80ca378b 16770 }
4b71a570 16771 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
edc3d884 16772 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
c4a1d9e4 16773 if (INTEL_INFO(dev)->gen >= 4) {
edc3d884
MK
16774 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
16775 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
c4a1d9e4
CW
16776 }
16777
edc3d884
MK
16778 err_printf(m, "Cursor [%d]:\n", i);
16779 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
16780 err_printf(m, " POS: %08x\n", error->cursor[i].position);
16781 err_printf(m, " BASE: %08x\n", error->cursor[i].base);
c4a1d9e4 16782 }
63b66e5b
CW
16783
16784 for (i = 0; i < error->num_transcoders; i++) {
da205630 16785 err_printf(m, "CPU transcoder: %s\n",
63b66e5b 16786 transcoder_name(error->transcoder[i].cpu_transcoder));
ddf9c536 16787 err_printf(m, " Power: %s\n",
87ad3212 16788 onoff(error->transcoder[i].power_domain_on));
63b66e5b
CW
16789 err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
16790 err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
16791 err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
16792 err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
16793 err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
16794 err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
16795 err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
16796 }
c4a1d9e4 16797}