]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/gpu/drm/i915/intel_display.c
drm/i915/bxt: allow dsi on any pipe
[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"
760285e7 37#include <drm/i915_drm.h>
79e53945 38#include "i915_drv.h"
e5510fac 39#include "i915_trace.h"
319c1d42 40#include <drm/drm_atomic.h>
c196e1d6 41#include <drm/drm_atomic_helper.h>
760285e7
DH
42#include <drm/drm_dp_helper.h>
43#include <drm/drm_crtc_helper.h>
465c120c
MR
44#include <drm/drm_plane_helper.h>
45#include <drm/drm_rect.h>
c0f372b3 46#include <linux/dma_remapping.h>
fd8e058a
AG
47#include <linux/reservation.h>
48#include <linux/dma-buf.h>
79e53945 49
465c120c 50/* Primary plane formats for gen <= 3 */
568db4f2 51static const uint32_t i8xx_primary_formats[] = {
67fe7dc5
DL
52 DRM_FORMAT_C8,
53 DRM_FORMAT_RGB565,
465c120c 54 DRM_FORMAT_XRGB1555,
67fe7dc5 55 DRM_FORMAT_XRGB8888,
465c120c
MR
56};
57
58/* Primary plane formats for gen >= 4 */
568db4f2 59static const uint32_t i965_primary_formats[] = {
6c0fd451
DL
60 DRM_FORMAT_C8,
61 DRM_FORMAT_RGB565,
62 DRM_FORMAT_XRGB8888,
63 DRM_FORMAT_XBGR8888,
64 DRM_FORMAT_XRGB2101010,
65 DRM_FORMAT_XBGR2101010,
66};
67
68static const uint32_t skl_primary_formats[] = {
67fe7dc5
DL
69 DRM_FORMAT_C8,
70 DRM_FORMAT_RGB565,
71 DRM_FORMAT_XRGB8888,
465c120c 72 DRM_FORMAT_XBGR8888,
67fe7dc5 73 DRM_FORMAT_ARGB8888,
465c120c
MR
74 DRM_FORMAT_ABGR8888,
75 DRM_FORMAT_XRGB2101010,
465c120c 76 DRM_FORMAT_XBGR2101010,
ea916ea0
KM
77 DRM_FORMAT_YUYV,
78 DRM_FORMAT_YVYU,
79 DRM_FORMAT_UYVY,
80 DRM_FORMAT_VYUY,
465c120c
MR
81};
82
3d7d6510
MR
83/* Cursor formats */
84static const uint32_t intel_cursor_formats[] = {
85 DRM_FORMAT_ARGB8888,
86};
87
f1f644dc 88static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 89 struct intel_crtc_state *pipe_config);
18442d08 90static void ironlake_pch_clock_get(struct intel_crtc *crtc,
5cec258b 91 struct intel_crtc_state *pipe_config);
f1f644dc 92
eb1bfe80
JB
93static int intel_framebuffer_init(struct drm_device *dev,
94 struct intel_framebuffer *ifb,
95 struct drm_mode_fb_cmd2 *mode_cmd,
96 struct drm_i915_gem_object *obj);
5b18e57c
DV
97static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc);
98static void intel_set_pipe_timings(struct intel_crtc *intel_crtc);
bc58be60 99static void intel_set_pipe_src_size(struct intel_crtc *intel_crtc);
29407aab 100static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
f769cd24
VK
101 struct intel_link_m_n *m_n,
102 struct intel_link_m_n *m2_n2);
29407aab 103static void ironlake_set_pipeconf(struct drm_crtc *crtc);
229fca97 104static void haswell_set_pipeconf(struct drm_crtc *crtc);
391bf048
JN
105static void haswell_set_pipe_gamma(struct drm_crtc *crtc);
106static void haswell_set_pipemisc(struct drm_crtc *crtc);
229fca97 107static void intel_set_pipe_csc(struct drm_crtc *crtc);
d288f65f 108static void vlv_prepare_pll(struct intel_crtc *crtc,
5cec258b 109 const struct intel_crtc_state *pipe_config);
d288f65f 110static void chv_prepare_pll(struct intel_crtc *crtc,
5cec258b 111 const struct intel_crtc_state *pipe_config);
613d2b27
ML
112static void intel_begin_crtc_commit(struct drm_crtc *, struct drm_crtc_state *);
113static void intel_finish_crtc_commit(struct drm_crtc *, struct drm_crtc_state *);
549e2bfb
CK
114static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
115 struct intel_crtc_state *crtc_state);
5ab7b0b7
ID
116static int i9xx_get_refclk(const struct intel_crtc_state *crtc_state,
117 int num_connectors);
bfd16b2a
ML
118static void skylake_pfit_enable(struct intel_crtc *crtc);
119static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force);
120static void ironlake_pfit_enable(struct intel_crtc *crtc);
043e9bda 121static void intel_modeset_setup_hw_state(struct drm_device *dev);
2622a081 122static void intel_pre_disable_primary_noatomic(struct drm_crtc *crtc);
e7457a9a 123
79e53945 124typedef struct {
0206e353 125 int min, max;
79e53945
JB
126} intel_range_t;
127
128typedef struct {
0206e353
AJ
129 int dot_limit;
130 int p2_slow, p2_fast;
79e53945
JB
131} intel_p2_t;
132
d4906093
ML
133typedef struct intel_limit intel_limit_t;
134struct intel_limit {
0206e353
AJ
135 intel_range_t dot, vco, n, m, m1, m2, p, p1;
136 intel_p2_t p2;
d4906093 137};
79e53945 138
bfa7df01
VS
139/* returns HPLL frequency in kHz */
140static int valleyview_get_vco(struct drm_i915_private *dev_priv)
141{
142 int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
143
144 /* Obtain SKU information */
145 mutex_lock(&dev_priv->sb_lock);
146 hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
147 CCK_FUSE_HPLL_FREQ_MASK;
148 mutex_unlock(&dev_priv->sb_lock);
149
150 return vco_freq[hpll_freq] * 1000;
151}
152
153static int vlv_get_cck_clock_hpll(struct drm_i915_private *dev_priv,
154 const char *name, u32 reg)
155{
156 u32 val;
157 int divider;
158
159 if (dev_priv->hpll_freq == 0)
160 dev_priv->hpll_freq = valleyview_get_vco(dev_priv);
161
162 mutex_lock(&dev_priv->sb_lock);
163 val = vlv_cck_read(dev_priv, reg);
164 mutex_unlock(&dev_priv->sb_lock);
165
166 divider = val & CCK_FREQUENCY_VALUES;
167
168 WARN((val & CCK_FREQUENCY_STATUS) !=
169 (divider << CCK_FREQUENCY_STATUS_SHIFT),
170 "%s change in progress\n", name);
171
172 return DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, divider + 1);
173}
174
e7dc33f3
VS
175static int
176intel_pch_rawclk(struct drm_i915_private *dev_priv)
d2acd215 177{
e7dc33f3
VS
178 return (I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK) * 1000;
179}
d2acd215 180
e7dc33f3
VS
181static int
182intel_vlv_hrawclk(struct drm_i915_private *dev_priv)
183{
35d38d1f
VS
184 return vlv_get_cck_clock_hpll(dev_priv, "hrawclk",
185 CCK_DISPLAY_REF_CLOCK_CONTROL);
d2acd215
DV
186}
187
e7dc33f3
VS
188static int
189intel_g4x_hrawclk(struct drm_i915_private *dev_priv)
79e50a4f 190{
79e50a4f
JN
191 uint32_t clkcfg;
192
e7dc33f3 193 /* hrawclock is 1/4 the FSB frequency */
79e50a4f
JN
194 clkcfg = I915_READ(CLKCFG);
195 switch (clkcfg & CLKCFG_FSB_MASK) {
196 case CLKCFG_FSB_400:
e7dc33f3 197 return 100000;
79e50a4f 198 case CLKCFG_FSB_533:
e7dc33f3 199 return 133333;
79e50a4f 200 case CLKCFG_FSB_667:
e7dc33f3 201 return 166667;
79e50a4f 202 case CLKCFG_FSB_800:
e7dc33f3 203 return 200000;
79e50a4f 204 case CLKCFG_FSB_1067:
e7dc33f3 205 return 266667;
79e50a4f 206 case CLKCFG_FSB_1333:
e7dc33f3 207 return 333333;
79e50a4f
JN
208 /* these two are just a guess; one of them might be right */
209 case CLKCFG_FSB_1600:
210 case CLKCFG_FSB_1600_ALT:
e7dc33f3 211 return 400000;
79e50a4f 212 default:
e7dc33f3 213 return 133333;
79e50a4f
JN
214 }
215}
216
e7dc33f3
VS
217static void intel_update_rawclk(struct drm_i915_private *dev_priv)
218{
219 if (HAS_PCH_SPLIT(dev_priv))
220 dev_priv->rawclk_freq = intel_pch_rawclk(dev_priv);
221 else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
222 dev_priv->rawclk_freq = intel_vlv_hrawclk(dev_priv);
223 else if (IS_G4X(dev_priv) || IS_PINEVIEW(dev_priv))
224 dev_priv->rawclk_freq = intel_g4x_hrawclk(dev_priv);
225 else
226 return; /* no rawclk on other platforms, or no need to know it */
227
228 DRM_DEBUG_DRIVER("rawclk rate: %d kHz\n", dev_priv->rawclk_freq);
229}
230
bfa7df01
VS
231static void intel_update_czclk(struct drm_i915_private *dev_priv)
232{
666a4537 233 if (!(IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)))
bfa7df01
VS
234 return;
235
236 dev_priv->czclk_freq = vlv_get_cck_clock_hpll(dev_priv, "czclk",
237 CCK_CZ_CLOCK_CONTROL);
238
239 DRM_DEBUG_DRIVER("CZ clock rate: %d kHz\n", dev_priv->czclk_freq);
240}
241
021357ac 242static inline u32 /* units of 100MHz */
21a727b3
VS
243intel_fdi_link_freq(struct drm_i915_private *dev_priv,
244 const struct intel_crtc_state *pipe_config)
021357ac 245{
21a727b3
VS
246 if (HAS_DDI(dev_priv))
247 return pipe_config->port_clock; /* SPLL */
248 else if (IS_GEN5(dev_priv))
249 return ((I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2) * 10000;
e3b247da 250 else
21a727b3 251 return 270000;
021357ac
CW
252}
253
5d536e28 254static const intel_limit_t intel_limits_i8xx_dac = {
0206e353 255 .dot = { .min = 25000, .max = 350000 },
9c333719 256 .vco = { .min = 908000, .max = 1512000 },
91dbe5fb 257 .n = { .min = 2, .max = 16 },
0206e353
AJ
258 .m = { .min = 96, .max = 140 },
259 .m1 = { .min = 18, .max = 26 },
260 .m2 = { .min = 6, .max = 16 },
261 .p = { .min = 4, .max = 128 },
262 .p1 = { .min = 2, .max = 33 },
273e27ca
EA
263 .p2 = { .dot_limit = 165000,
264 .p2_slow = 4, .p2_fast = 2 },
e4b36699
KP
265};
266
5d536e28
DV
267static const intel_limit_t intel_limits_i8xx_dvo = {
268 .dot = { .min = 25000, .max = 350000 },
9c333719 269 .vco = { .min = 908000, .max = 1512000 },
91dbe5fb 270 .n = { .min = 2, .max = 16 },
5d536e28
DV
271 .m = { .min = 96, .max = 140 },
272 .m1 = { .min = 18, .max = 26 },
273 .m2 = { .min = 6, .max = 16 },
274 .p = { .min = 4, .max = 128 },
275 .p1 = { .min = 2, .max = 33 },
276 .p2 = { .dot_limit = 165000,
277 .p2_slow = 4, .p2_fast = 4 },
278};
279
e4b36699 280static const intel_limit_t intel_limits_i8xx_lvds = {
0206e353 281 .dot = { .min = 25000, .max = 350000 },
9c333719 282 .vco = { .min = 908000, .max = 1512000 },
91dbe5fb 283 .n = { .min = 2, .max = 16 },
0206e353
AJ
284 .m = { .min = 96, .max = 140 },
285 .m1 = { .min = 18, .max = 26 },
286 .m2 = { .min = 6, .max = 16 },
287 .p = { .min = 4, .max = 128 },
288 .p1 = { .min = 1, .max = 6 },
273e27ca
EA
289 .p2 = { .dot_limit = 165000,
290 .p2_slow = 14, .p2_fast = 7 },
e4b36699 291};
273e27ca 292
e4b36699 293static const intel_limit_t intel_limits_i9xx_sdvo = {
0206e353
AJ
294 .dot = { .min = 20000, .max = 400000 },
295 .vco = { .min = 1400000, .max = 2800000 },
296 .n = { .min = 1, .max = 6 },
297 .m = { .min = 70, .max = 120 },
4f7dfb67
PJ
298 .m1 = { .min = 8, .max = 18 },
299 .m2 = { .min = 3, .max = 7 },
0206e353
AJ
300 .p = { .min = 5, .max = 80 },
301 .p1 = { .min = 1, .max = 8 },
273e27ca
EA
302 .p2 = { .dot_limit = 200000,
303 .p2_slow = 10, .p2_fast = 5 },
e4b36699
KP
304};
305
306static const intel_limit_t intel_limits_i9xx_lvds = {
0206e353
AJ
307 .dot = { .min = 20000, .max = 400000 },
308 .vco = { .min = 1400000, .max = 2800000 },
309 .n = { .min = 1, .max = 6 },
310 .m = { .min = 70, .max = 120 },
53a7d2d1
PJ
311 .m1 = { .min = 8, .max = 18 },
312 .m2 = { .min = 3, .max = 7 },
0206e353
AJ
313 .p = { .min = 7, .max = 98 },
314 .p1 = { .min = 1, .max = 8 },
273e27ca
EA
315 .p2 = { .dot_limit = 112000,
316 .p2_slow = 14, .p2_fast = 7 },
e4b36699
KP
317};
318
273e27ca 319
e4b36699 320static const intel_limit_t intel_limits_g4x_sdvo = {
273e27ca
EA
321 .dot = { .min = 25000, .max = 270000 },
322 .vco = { .min = 1750000, .max = 3500000},
323 .n = { .min = 1, .max = 4 },
324 .m = { .min = 104, .max = 138 },
325 .m1 = { .min = 17, .max = 23 },
326 .m2 = { .min = 5, .max = 11 },
327 .p = { .min = 10, .max = 30 },
328 .p1 = { .min = 1, .max = 3},
329 .p2 = { .dot_limit = 270000,
330 .p2_slow = 10,
331 .p2_fast = 10
044c7c41 332 },
e4b36699
KP
333};
334
335static const intel_limit_t intel_limits_g4x_hdmi = {
273e27ca
EA
336 .dot = { .min = 22000, .max = 400000 },
337 .vco = { .min = 1750000, .max = 3500000},
338 .n = { .min = 1, .max = 4 },
339 .m = { .min = 104, .max = 138 },
340 .m1 = { .min = 16, .max = 23 },
341 .m2 = { .min = 5, .max = 11 },
342 .p = { .min = 5, .max = 80 },
343 .p1 = { .min = 1, .max = 8},
344 .p2 = { .dot_limit = 165000,
345 .p2_slow = 10, .p2_fast = 5 },
e4b36699
KP
346};
347
348static const intel_limit_t intel_limits_g4x_single_channel_lvds = {
273e27ca
EA
349 .dot = { .min = 20000, .max = 115000 },
350 .vco = { .min = 1750000, .max = 3500000 },
351 .n = { .min = 1, .max = 3 },
352 .m = { .min = 104, .max = 138 },
353 .m1 = { .min = 17, .max = 23 },
354 .m2 = { .min = 5, .max = 11 },
355 .p = { .min = 28, .max = 112 },
356 .p1 = { .min = 2, .max = 8 },
357 .p2 = { .dot_limit = 0,
358 .p2_slow = 14, .p2_fast = 14
044c7c41 359 },
e4b36699
KP
360};
361
362static const intel_limit_t intel_limits_g4x_dual_channel_lvds = {
273e27ca
EA
363 .dot = { .min = 80000, .max = 224000 },
364 .vco = { .min = 1750000, .max = 3500000 },
365 .n = { .min = 1, .max = 3 },
366 .m = { .min = 104, .max = 138 },
367 .m1 = { .min = 17, .max = 23 },
368 .m2 = { .min = 5, .max = 11 },
369 .p = { .min = 14, .max = 42 },
370 .p1 = { .min = 2, .max = 6 },
371 .p2 = { .dot_limit = 0,
372 .p2_slow = 7, .p2_fast = 7
044c7c41 373 },
e4b36699
KP
374};
375
f2b115e6 376static const intel_limit_t intel_limits_pineview_sdvo = {
0206e353
AJ
377 .dot = { .min = 20000, .max = 400000},
378 .vco = { .min = 1700000, .max = 3500000 },
273e27ca 379 /* Pineview's Ncounter is a ring counter */
0206e353
AJ
380 .n = { .min = 3, .max = 6 },
381 .m = { .min = 2, .max = 256 },
273e27ca 382 /* Pineview only has one combined m divider, which we treat as m2. */
0206e353
AJ
383 .m1 = { .min = 0, .max = 0 },
384 .m2 = { .min = 0, .max = 254 },
385 .p = { .min = 5, .max = 80 },
386 .p1 = { .min = 1, .max = 8 },
273e27ca
EA
387 .p2 = { .dot_limit = 200000,
388 .p2_slow = 10, .p2_fast = 5 },
e4b36699
KP
389};
390
f2b115e6 391static const intel_limit_t intel_limits_pineview_lvds = {
0206e353
AJ
392 .dot = { .min = 20000, .max = 400000 },
393 .vco = { .min = 1700000, .max = 3500000 },
394 .n = { .min = 3, .max = 6 },
395 .m = { .min = 2, .max = 256 },
396 .m1 = { .min = 0, .max = 0 },
397 .m2 = { .min = 0, .max = 254 },
398 .p = { .min = 7, .max = 112 },
399 .p1 = { .min = 1, .max = 8 },
273e27ca
EA
400 .p2 = { .dot_limit = 112000,
401 .p2_slow = 14, .p2_fast = 14 },
e4b36699
KP
402};
403
273e27ca
EA
404/* Ironlake / Sandybridge
405 *
406 * We calculate clock using (register_value + 2) for N/M1/M2, so here
407 * the range value for them is (actual_value - 2).
408 */
b91ad0ec 409static const intel_limit_t intel_limits_ironlake_dac = {
273e27ca
EA
410 .dot = { .min = 25000, .max = 350000 },
411 .vco = { .min = 1760000, .max = 3510000 },
412 .n = { .min = 1, .max = 5 },
413 .m = { .min = 79, .max = 127 },
414 .m1 = { .min = 12, .max = 22 },
415 .m2 = { .min = 5, .max = 9 },
416 .p = { .min = 5, .max = 80 },
417 .p1 = { .min = 1, .max = 8 },
418 .p2 = { .dot_limit = 225000,
419 .p2_slow = 10, .p2_fast = 5 },
e4b36699
KP
420};
421
b91ad0ec 422static const intel_limit_t intel_limits_ironlake_single_lvds = {
273e27ca
EA
423 .dot = { .min = 25000, .max = 350000 },
424 .vco = { .min = 1760000, .max = 3510000 },
425 .n = { .min = 1, .max = 3 },
426 .m = { .min = 79, .max = 118 },
427 .m1 = { .min = 12, .max = 22 },
428 .m2 = { .min = 5, .max = 9 },
429 .p = { .min = 28, .max = 112 },
430 .p1 = { .min = 2, .max = 8 },
431 .p2 = { .dot_limit = 225000,
432 .p2_slow = 14, .p2_fast = 14 },
b91ad0ec
ZW
433};
434
435static const intel_limit_t intel_limits_ironlake_dual_lvds = {
273e27ca
EA
436 .dot = { .min = 25000, .max = 350000 },
437 .vco = { .min = 1760000, .max = 3510000 },
438 .n = { .min = 1, .max = 3 },
439 .m = { .min = 79, .max = 127 },
440 .m1 = { .min = 12, .max = 22 },
441 .m2 = { .min = 5, .max = 9 },
442 .p = { .min = 14, .max = 56 },
443 .p1 = { .min = 2, .max = 8 },
444 .p2 = { .dot_limit = 225000,
445 .p2_slow = 7, .p2_fast = 7 },
b91ad0ec
ZW
446};
447
273e27ca 448/* LVDS 100mhz refclk limits. */
b91ad0ec 449static const intel_limit_t intel_limits_ironlake_single_lvds_100m = {
273e27ca
EA
450 .dot = { .min = 25000, .max = 350000 },
451 .vco = { .min = 1760000, .max = 3510000 },
452 .n = { .min = 1, .max = 2 },
453 .m = { .min = 79, .max = 126 },
454 .m1 = { .min = 12, .max = 22 },
455 .m2 = { .min = 5, .max = 9 },
456 .p = { .min = 28, .max = 112 },
0206e353 457 .p1 = { .min = 2, .max = 8 },
273e27ca
EA
458 .p2 = { .dot_limit = 225000,
459 .p2_slow = 14, .p2_fast = 14 },
b91ad0ec
ZW
460};
461
462static const intel_limit_t intel_limits_ironlake_dual_lvds_100m = {
273e27ca
EA
463 .dot = { .min = 25000, .max = 350000 },
464 .vco = { .min = 1760000, .max = 3510000 },
465 .n = { .min = 1, .max = 3 },
466 .m = { .min = 79, .max = 126 },
467 .m1 = { .min = 12, .max = 22 },
468 .m2 = { .min = 5, .max = 9 },
469 .p = { .min = 14, .max = 42 },
0206e353 470 .p1 = { .min = 2, .max = 6 },
273e27ca
EA
471 .p2 = { .dot_limit = 225000,
472 .p2_slow = 7, .p2_fast = 7 },
4547668a
ZY
473};
474
dc730512 475static const intel_limit_t intel_limits_vlv = {
f01b7962
VS
476 /*
477 * These are the data rate limits (measured in fast clocks)
478 * since those are the strictest limits we have. The fast
479 * clock and actual rate limits are more relaxed, so checking
480 * them would make no difference.
481 */
482 .dot = { .min = 25000 * 5, .max = 270000 * 5 },
75e53986 483 .vco = { .min = 4000000, .max = 6000000 },
a0c4da24 484 .n = { .min = 1, .max = 7 },
a0c4da24
JB
485 .m1 = { .min = 2, .max = 3 },
486 .m2 = { .min = 11, .max = 156 },
b99ab663 487 .p1 = { .min = 2, .max = 3 },
5fdc9c49 488 .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
a0c4da24
JB
489};
490
ef9348c8
CML
491static const intel_limit_t intel_limits_chv = {
492 /*
493 * These are the data rate limits (measured in fast clocks)
494 * since those are the strictest limits we have. The fast
495 * clock and actual rate limits are more relaxed, so checking
496 * them would make no difference.
497 */
498 .dot = { .min = 25000 * 5, .max = 540000 * 5},
17fe1021 499 .vco = { .min = 4800000, .max = 6480000 },
ef9348c8
CML
500 .n = { .min = 1, .max = 1 },
501 .m1 = { .min = 2, .max = 2 },
502 .m2 = { .min = 24 << 22, .max = 175 << 22 },
503 .p1 = { .min = 2, .max = 4 },
504 .p2 = { .p2_slow = 1, .p2_fast = 14 },
505};
506
5ab7b0b7
ID
507static const intel_limit_t intel_limits_bxt = {
508 /* FIXME: find real dot limits */
509 .dot = { .min = 0, .max = INT_MAX },
e6292556 510 .vco = { .min = 4800000, .max = 6700000 },
5ab7b0b7
ID
511 .n = { .min = 1, .max = 1 },
512 .m1 = { .min = 2, .max = 2 },
513 /* FIXME: find real m2 limits */
514 .m2 = { .min = 2 << 22, .max = 255 << 22 },
515 .p1 = { .min = 2, .max = 4 },
516 .p2 = { .p2_slow = 1, .p2_fast = 20 },
517};
518
cdba954e
ACO
519static bool
520needs_modeset(struct drm_crtc_state *state)
521{
fc596660 522 return drm_atomic_crtc_needs_modeset(state);
cdba954e
ACO
523}
524
e0638cdf
PZ
525/**
526 * Returns whether any output on the specified pipe is of the specified type
527 */
4093561b 528bool intel_pipe_has_type(struct intel_crtc *crtc, enum intel_output_type type)
e0638cdf 529{
409ee761 530 struct drm_device *dev = crtc->base.dev;
e0638cdf
PZ
531 struct intel_encoder *encoder;
532
409ee761 533 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
e0638cdf
PZ
534 if (encoder->type == type)
535 return true;
536
537 return false;
538}
539
d0737e1d
ACO
540/**
541 * Returns whether any output on the specified pipe will have the specified
542 * type after a staged modeset is complete, i.e., the same as
543 * intel_pipe_has_type() but looking at encoder->new_crtc instead of
544 * encoder->crtc.
545 */
a93e255f
ACO
546static bool intel_pipe_will_have_type(const struct intel_crtc_state *crtc_state,
547 int type)
d0737e1d 548{
a93e255f 549 struct drm_atomic_state *state = crtc_state->base.state;
da3ced29 550 struct drm_connector *connector;
a93e255f 551 struct drm_connector_state *connector_state;
d0737e1d 552 struct intel_encoder *encoder;
a93e255f
ACO
553 int i, num_connectors = 0;
554
da3ced29 555 for_each_connector_in_state(state, connector, connector_state, i) {
a93e255f
ACO
556 if (connector_state->crtc != crtc_state->base.crtc)
557 continue;
558
559 num_connectors++;
d0737e1d 560
a93e255f
ACO
561 encoder = to_intel_encoder(connector_state->best_encoder);
562 if (encoder->type == type)
d0737e1d 563 return true;
a93e255f
ACO
564 }
565
566 WARN_ON(num_connectors == 0);
d0737e1d
ACO
567
568 return false;
569}
570
a93e255f
ACO
571static const intel_limit_t *
572intel_ironlake_limit(struct intel_crtc_state *crtc_state, int refclk)
2c07245f 573{
a93e255f 574 struct drm_device *dev = crtc_state->base.crtc->dev;
2c07245f 575 const intel_limit_t *limit;
b91ad0ec 576
a93e255f 577 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
1974cad0 578 if (intel_is_dual_link_lvds(dev)) {
1b894b59 579 if (refclk == 100000)
b91ad0ec
ZW
580 limit = &intel_limits_ironlake_dual_lvds_100m;
581 else
582 limit = &intel_limits_ironlake_dual_lvds;
583 } else {
1b894b59 584 if (refclk == 100000)
b91ad0ec
ZW
585 limit = &intel_limits_ironlake_single_lvds_100m;
586 else
587 limit = &intel_limits_ironlake_single_lvds;
588 }
c6bb3538 589 } else
b91ad0ec 590 limit = &intel_limits_ironlake_dac;
2c07245f
ZW
591
592 return limit;
593}
594
a93e255f
ACO
595static const intel_limit_t *
596intel_g4x_limit(struct intel_crtc_state *crtc_state)
044c7c41 597{
a93e255f 598 struct drm_device *dev = crtc_state->base.crtc->dev;
044c7c41
ML
599 const intel_limit_t *limit;
600
a93e255f 601 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
1974cad0 602 if (intel_is_dual_link_lvds(dev))
e4b36699 603 limit = &intel_limits_g4x_dual_channel_lvds;
044c7c41 604 else
e4b36699 605 limit = &intel_limits_g4x_single_channel_lvds;
a93e255f
ACO
606 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI) ||
607 intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
e4b36699 608 limit = &intel_limits_g4x_hdmi;
a93e255f 609 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO)) {
e4b36699 610 limit = &intel_limits_g4x_sdvo;
044c7c41 611 } else /* The option is for other outputs */
e4b36699 612 limit = &intel_limits_i9xx_sdvo;
044c7c41
ML
613
614 return limit;
615}
616
a93e255f
ACO
617static const intel_limit_t *
618intel_limit(struct intel_crtc_state *crtc_state, int refclk)
79e53945 619{
a93e255f 620 struct drm_device *dev = crtc_state->base.crtc->dev;
79e53945
JB
621 const intel_limit_t *limit;
622
5ab7b0b7
ID
623 if (IS_BROXTON(dev))
624 limit = &intel_limits_bxt;
625 else if (HAS_PCH_SPLIT(dev))
a93e255f 626 limit = intel_ironlake_limit(crtc_state, refclk);
2c07245f 627 else if (IS_G4X(dev)) {
a93e255f 628 limit = intel_g4x_limit(crtc_state);
f2b115e6 629 } else if (IS_PINEVIEW(dev)) {
a93e255f 630 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
f2b115e6 631 limit = &intel_limits_pineview_lvds;
2177832f 632 else
f2b115e6 633 limit = &intel_limits_pineview_sdvo;
ef9348c8
CML
634 } else if (IS_CHERRYVIEW(dev)) {
635 limit = &intel_limits_chv;
a0c4da24 636 } else if (IS_VALLEYVIEW(dev)) {
dc730512 637 limit = &intel_limits_vlv;
a6c45cf0 638 } else if (!IS_GEN2(dev)) {
a93e255f 639 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
a6c45cf0
CW
640 limit = &intel_limits_i9xx_lvds;
641 else
642 limit = &intel_limits_i9xx_sdvo;
79e53945 643 } else {
a93e255f 644 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
e4b36699 645 limit = &intel_limits_i8xx_lvds;
a93e255f 646 else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
e4b36699 647 limit = &intel_limits_i8xx_dvo;
5d536e28
DV
648 else
649 limit = &intel_limits_i8xx_dac;
79e53945
JB
650 }
651 return limit;
652}
653
dccbea3b
ID
654/*
655 * Platform specific helpers to calculate the port PLL loopback- (clock.m),
656 * and post-divider (clock.p) values, pre- (clock.vco) and post-divided fast
657 * (clock.dot) clock rates. This fast dot clock is fed to the port's IO logic.
658 * The helpers' return value is the rate of the clock that is fed to the
659 * display engine's pipe which can be the above fast dot clock rate or a
660 * divided-down version of it.
661 */
f2b115e6 662/* m1 is reserved as 0 in Pineview, n is a ring counter */
dccbea3b 663static int pnv_calc_dpll_params(int refclk, intel_clock_t *clock)
79e53945 664{
2177832f
SL
665 clock->m = clock->m2 + 2;
666 clock->p = clock->p1 * clock->p2;
ed5ca77e 667 if (WARN_ON(clock->n == 0 || clock->p == 0))
dccbea3b 668 return 0;
fb03ac01
VS
669 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
670 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
dccbea3b
ID
671
672 return clock->dot;
2177832f
SL
673}
674
7429e9d4
DV
675static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
676{
677 return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
678}
679
dccbea3b 680static int i9xx_calc_dpll_params(int refclk, intel_clock_t *clock)
2177832f 681{
7429e9d4 682 clock->m = i9xx_dpll_compute_m(clock);
79e53945 683 clock->p = clock->p1 * clock->p2;
ed5ca77e 684 if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
dccbea3b 685 return 0;
fb03ac01
VS
686 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
687 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
dccbea3b
ID
688
689 return clock->dot;
79e53945
JB
690}
691
dccbea3b 692static int vlv_calc_dpll_params(int refclk, intel_clock_t *clock)
589eca67
ID
693{
694 clock->m = clock->m1 * clock->m2;
695 clock->p = clock->p1 * clock->p2;
696 if (WARN_ON(clock->n == 0 || clock->p == 0))
dccbea3b 697 return 0;
589eca67
ID
698 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
699 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
dccbea3b
ID
700
701 return clock->dot / 5;
589eca67
ID
702}
703
dccbea3b 704int chv_calc_dpll_params(int refclk, intel_clock_t *clock)
ef9348c8
CML
705{
706 clock->m = clock->m1 * clock->m2;
707 clock->p = clock->p1 * clock->p2;
708 if (WARN_ON(clock->n == 0 || clock->p == 0))
dccbea3b 709 return 0;
ef9348c8
CML
710 clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
711 clock->n << 22);
712 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
dccbea3b
ID
713
714 return clock->dot / 5;
ef9348c8
CML
715}
716
7c04d1d9 717#define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
79e53945
JB
718/**
719 * Returns whether the given set of divisors are valid for a given refclk with
720 * the given connectors.
721 */
722
1b894b59
CW
723static bool intel_PLL_is_valid(struct drm_device *dev,
724 const intel_limit_t *limit,
725 const intel_clock_t *clock)
79e53945 726{
f01b7962
VS
727 if (clock->n < limit->n.min || limit->n.max < clock->n)
728 INTELPllInvalid("n out of range\n");
79e53945 729 if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
0206e353 730 INTELPllInvalid("p1 out of range\n");
79e53945 731 if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
0206e353 732 INTELPllInvalid("m2 out of range\n");
79e53945 733 if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
0206e353 734 INTELPllInvalid("m1 out of range\n");
f01b7962 735
666a4537
WB
736 if (!IS_PINEVIEW(dev) && !IS_VALLEYVIEW(dev) &&
737 !IS_CHERRYVIEW(dev) && !IS_BROXTON(dev))
f01b7962
VS
738 if (clock->m1 <= clock->m2)
739 INTELPllInvalid("m1 <= m2\n");
740
666a4537 741 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev) && !IS_BROXTON(dev)) {
f01b7962
VS
742 if (clock->p < limit->p.min || limit->p.max < clock->p)
743 INTELPllInvalid("p out of range\n");
744 if (clock->m < limit->m.min || limit->m.max < clock->m)
745 INTELPllInvalid("m out of range\n");
746 }
747
79e53945 748 if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
0206e353 749 INTELPllInvalid("vco out of range\n");
79e53945
JB
750 /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
751 * connector, etc., rather than just a single range.
752 */
753 if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
0206e353 754 INTELPllInvalid("dot out of range\n");
79e53945
JB
755
756 return true;
757}
758
3b1429d9
VS
759static int
760i9xx_select_p2_div(const intel_limit_t *limit,
761 const struct intel_crtc_state *crtc_state,
762 int target)
79e53945 763{
3b1429d9 764 struct drm_device *dev = crtc_state->base.crtc->dev;
79e53945 765
a93e255f 766 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
79e53945 767 /*
a210b028
DV
768 * For LVDS just rely on its current settings for dual-channel.
769 * We haven't figured out how to reliably set up different
770 * single/dual channel state, if we even can.
79e53945 771 */
1974cad0 772 if (intel_is_dual_link_lvds(dev))
3b1429d9 773 return limit->p2.p2_fast;
79e53945 774 else
3b1429d9 775 return limit->p2.p2_slow;
79e53945
JB
776 } else {
777 if (target < limit->p2.dot_limit)
3b1429d9 778 return limit->p2.p2_slow;
79e53945 779 else
3b1429d9 780 return limit->p2.p2_fast;
79e53945 781 }
3b1429d9
VS
782}
783
784static bool
785i9xx_find_best_dpll(const intel_limit_t *limit,
786 struct intel_crtc_state *crtc_state,
787 int target, int refclk, intel_clock_t *match_clock,
788 intel_clock_t *best_clock)
789{
790 struct drm_device *dev = crtc_state->base.crtc->dev;
791 intel_clock_t clock;
792 int err = target;
79e53945 793
0206e353 794 memset(best_clock, 0, sizeof(*best_clock));
79e53945 795
3b1429d9
VS
796 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
797
42158660
ZY
798 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
799 clock.m1++) {
800 for (clock.m2 = limit->m2.min;
801 clock.m2 <= limit->m2.max; clock.m2++) {
c0efc387 802 if (clock.m2 >= clock.m1)
42158660
ZY
803 break;
804 for (clock.n = limit->n.min;
805 clock.n <= limit->n.max; clock.n++) {
806 for (clock.p1 = limit->p1.min;
807 clock.p1 <= limit->p1.max; clock.p1++) {
79e53945
JB
808 int this_err;
809
dccbea3b 810 i9xx_calc_dpll_params(refclk, &clock);
ac58c3f0
DV
811 if (!intel_PLL_is_valid(dev, limit,
812 &clock))
813 continue;
814 if (match_clock &&
815 clock.p != match_clock->p)
816 continue;
817
818 this_err = abs(clock.dot - target);
819 if (this_err < err) {
820 *best_clock = clock;
821 err = this_err;
822 }
823 }
824 }
825 }
826 }
827
828 return (err != target);
829}
830
831static bool
a93e255f
ACO
832pnv_find_best_dpll(const intel_limit_t *limit,
833 struct intel_crtc_state *crtc_state,
ee9300bb
DV
834 int target, int refclk, intel_clock_t *match_clock,
835 intel_clock_t *best_clock)
79e53945 836{
3b1429d9 837 struct drm_device *dev = crtc_state->base.crtc->dev;
79e53945 838 intel_clock_t clock;
79e53945
JB
839 int err = target;
840
0206e353 841 memset(best_clock, 0, sizeof(*best_clock));
79e53945 842
3b1429d9
VS
843 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
844
42158660
ZY
845 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
846 clock.m1++) {
847 for (clock.m2 = limit->m2.min;
848 clock.m2 <= limit->m2.max; clock.m2++) {
42158660
ZY
849 for (clock.n = limit->n.min;
850 clock.n <= limit->n.max; clock.n++) {
851 for (clock.p1 = limit->p1.min;
852 clock.p1 <= limit->p1.max; clock.p1++) {
79e53945
JB
853 int this_err;
854
dccbea3b 855 pnv_calc_dpll_params(refclk, &clock);
1b894b59
CW
856 if (!intel_PLL_is_valid(dev, limit,
857 &clock))
79e53945 858 continue;
cec2f356
SP
859 if (match_clock &&
860 clock.p != match_clock->p)
861 continue;
79e53945
JB
862
863 this_err = abs(clock.dot - target);
864 if (this_err < err) {
865 *best_clock = clock;
866 err = this_err;
867 }
868 }
869 }
870 }
871 }
872
873 return (err != target);
874}
875
d4906093 876static bool
a93e255f
ACO
877g4x_find_best_dpll(const intel_limit_t *limit,
878 struct intel_crtc_state *crtc_state,
ee9300bb
DV
879 int target, int refclk, intel_clock_t *match_clock,
880 intel_clock_t *best_clock)
d4906093 881{
3b1429d9 882 struct drm_device *dev = crtc_state->base.crtc->dev;
d4906093
ML
883 intel_clock_t clock;
884 int max_n;
3b1429d9 885 bool found = false;
6ba770dc
AJ
886 /* approximately equals target * 0.00585 */
887 int err_most = (target >> 8) + (target >> 9);
d4906093
ML
888
889 memset(best_clock, 0, sizeof(*best_clock));
3b1429d9
VS
890
891 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
892
d4906093 893 max_n = limit->n.max;
f77f13e2 894 /* based on hardware requirement, prefer smaller n to precision */
d4906093 895 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
f77f13e2 896 /* based on hardware requirement, prefere larger m1,m2 */
d4906093
ML
897 for (clock.m1 = limit->m1.max;
898 clock.m1 >= limit->m1.min; clock.m1--) {
899 for (clock.m2 = limit->m2.max;
900 clock.m2 >= limit->m2.min; clock.m2--) {
901 for (clock.p1 = limit->p1.max;
902 clock.p1 >= limit->p1.min; clock.p1--) {
903 int this_err;
904
dccbea3b 905 i9xx_calc_dpll_params(refclk, &clock);
1b894b59
CW
906 if (!intel_PLL_is_valid(dev, limit,
907 &clock))
d4906093 908 continue;
1b894b59
CW
909
910 this_err = abs(clock.dot - target);
d4906093
ML
911 if (this_err < err_most) {
912 *best_clock = clock;
913 err_most = this_err;
914 max_n = clock.n;
915 found = true;
916 }
917 }
918 }
919 }
920 }
2c07245f
ZW
921 return found;
922}
923
d5dd62bd
ID
924/*
925 * Check if the calculated PLL configuration is more optimal compared to the
926 * best configuration and error found so far. Return the calculated error.
927 */
928static bool vlv_PLL_is_optimal(struct drm_device *dev, int target_freq,
929 const intel_clock_t *calculated_clock,
930 const intel_clock_t *best_clock,
931 unsigned int best_error_ppm,
932 unsigned int *error_ppm)
933{
9ca3ba01
ID
934 /*
935 * For CHV ignore the error and consider only the P value.
936 * Prefer a bigger P value based on HW requirements.
937 */
938 if (IS_CHERRYVIEW(dev)) {
939 *error_ppm = 0;
940
941 return calculated_clock->p > best_clock->p;
942 }
943
24be4e46
ID
944 if (WARN_ON_ONCE(!target_freq))
945 return false;
946
d5dd62bd
ID
947 *error_ppm = div_u64(1000000ULL *
948 abs(target_freq - calculated_clock->dot),
949 target_freq);
950 /*
951 * Prefer a better P value over a better (smaller) error if the error
952 * is small. Ensure this preference for future configurations too by
953 * setting the error to 0.
954 */
955 if (*error_ppm < 100 && calculated_clock->p > best_clock->p) {
956 *error_ppm = 0;
957
958 return true;
959 }
960
961 return *error_ppm + 10 < best_error_ppm;
962}
963
a0c4da24 964static bool
a93e255f
ACO
965vlv_find_best_dpll(const intel_limit_t *limit,
966 struct intel_crtc_state *crtc_state,
ee9300bb
DV
967 int target, int refclk, intel_clock_t *match_clock,
968 intel_clock_t *best_clock)
a0c4da24 969{
a93e255f 970 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
a919ff14 971 struct drm_device *dev = crtc->base.dev;
6b4bf1c4 972 intel_clock_t clock;
69e4f900 973 unsigned int bestppm = 1000000;
27e639bf
VS
974 /* min update 19.2 MHz */
975 int max_n = min(limit->n.max, refclk / 19200);
49e497ef 976 bool found = false;
a0c4da24 977
6b4bf1c4
VS
978 target *= 5; /* fast clock */
979
980 memset(best_clock, 0, sizeof(*best_clock));
a0c4da24
JB
981
982 /* based on hardware requirement, prefer smaller n to precision */
27e639bf 983 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
811bbf05 984 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
889059d8 985 for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
c1a9ae43 986 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
6b4bf1c4 987 clock.p = clock.p1 * clock.p2;
a0c4da24 988 /* based on hardware requirement, prefer bigger m1,m2 values */
6b4bf1c4 989 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
d5dd62bd 990 unsigned int ppm;
69e4f900 991
6b4bf1c4
VS
992 clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
993 refclk * clock.m1);
994
dccbea3b 995 vlv_calc_dpll_params(refclk, &clock);
43b0ac53 996
f01b7962
VS
997 if (!intel_PLL_is_valid(dev, limit,
998 &clock))
43b0ac53
VS
999 continue;
1000
d5dd62bd
ID
1001 if (!vlv_PLL_is_optimal(dev, target,
1002 &clock,
1003 best_clock,
1004 bestppm, &ppm))
1005 continue;
6b4bf1c4 1006
d5dd62bd
ID
1007 *best_clock = clock;
1008 bestppm = ppm;
1009 found = true;
a0c4da24
JB
1010 }
1011 }
1012 }
1013 }
a0c4da24 1014
49e497ef 1015 return found;
a0c4da24 1016}
a4fc5ed6 1017
ef9348c8 1018static bool
a93e255f
ACO
1019chv_find_best_dpll(const intel_limit_t *limit,
1020 struct intel_crtc_state *crtc_state,
ef9348c8
CML
1021 int target, int refclk, intel_clock_t *match_clock,
1022 intel_clock_t *best_clock)
1023{
a93e255f 1024 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
a919ff14 1025 struct drm_device *dev = crtc->base.dev;
9ca3ba01 1026 unsigned int best_error_ppm;
ef9348c8
CML
1027 intel_clock_t clock;
1028 uint64_t m2;
1029 int found = false;
1030
1031 memset(best_clock, 0, sizeof(*best_clock));
9ca3ba01 1032 best_error_ppm = 1000000;
ef9348c8
CML
1033
1034 /*
1035 * Based on hardware doc, the n always set to 1, and m1 always
1036 * set to 2. If requires to support 200Mhz refclk, we need to
1037 * revisit this because n may not 1 anymore.
1038 */
1039 clock.n = 1, clock.m1 = 2;
1040 target *= 5; /* fast clock */
1041
1042 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
1043 for (clock.p2 = limit->p2.p2_fast;
1044 clock.p2 >= limit->p2.p2_slow;
1045 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
9ca3ba01 1046 unsigned int error_ppm;
ef9348c8
CML
1047
1048 clock.p = clock.p1 * clock.p2;
1049
1050 m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
1051 clock.n) << 22, refclk * clock.m1);
1052
1053 if (m2 > INT_MAX/clock.m1)
1054 continue;
1055
1056 clock.m2 = m2;
1057
dccbea3b 1058 chv_calc_dpll_params(refclk, &clock);
ef9348c8
CML
1059
1060 if (!intel_PLL_is_valid(dev, limit, &clock))
1061 continue;
1062
9ca3ba01
ID
1063 if (!vlv_PLL_is_optimal(dev, target, &clock, best_clock,
1064 best_error_ppm, &error_ppm))
1065 continue;
1066
1067 *best_clock = clock;
1068 best_error_ppm = error_ppm;
1069 found = true;
ef9348c8
CML
1070 }
1071 }
1072
1073 return found;
1074}
1075
5ab7b0b7
ID
1076bool bxt_find_best_dpll(struct intel_crtc_state *crtc_state, int target_clock,
1077 intel_clock_t *best_clock)
1078{
1079 int refclk = i9xx_get_refclk(crtc_state, 0);
1080
1081 return chv_find_best_dpll(intel_limit(crtc_state, refclk), crtc_state,
1082 target_clock, refclk, NULL, best_clock);
1083}
1084
20ddf665
VS
1085bool intel_crtc_active(struct drm_crtc *crtc)
1086{
1087 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1088
1089 /* Be paranoid as we can arrive here with only partial
1090 * state retrieved from the hardware during setup.
1091 *
241bfc38 1092 * We can ditch the adjusted_mode.crtc_clock check as soon
20ddf665
VS
1093 * as Haswell has gained clock readout/fastboot support.
1094 *
66e514c1 1095 * We can ditch the crtc->primary->fb check as soon as we can
20ddf665 1096 * properly reconstruct framebuffers.
c3d1f436
MR
1097 *
1098 * FIXME: The intel_crtc->active here should be switched to
1099 * crtc->state->active once we have proper CRTC states wired up
1100 * for atomic.
20ddf665 1101 */
c3d1f436 1102 return intel_crtc->active && crtc->primary->state->fb &&
6e3c9717 1103 intel_crtc->config->base.adjusted_mode.crtc_clock;
20ddf665
VS
1104}
1105
a5c961d1
PZ
1106enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
1107 enum pipe pipe)
1108{
1109 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
1110 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1111
6e3c9717 1112 return intel_crtc->config->cpu_transcoder;
a5c961d1
PZ
1113}
1114
fbf49ea2
VS
1115static bool pipe_dsl_stopped(struct drm_device *dev, enum pipe pipe)
1116{
1117 struct drm_i915_private *dev_priv = dev->dev_private;
f0f59a00 1118 i915_reg_t reg = PIPEDSL(pipe);
fbf49ea2
VS
1119 u32 line1, line2;
1120 u32 line_mask;
1121
1122 if (IS_GEN2(dev))
1123 line_mask = DSL_LINEMASK_GEN2;
1124 else
1125 line_mask = DSL_LINEMASK_GEN3;
1126
1127 line1 = I915_READ(reg) & line_mask;
6adfb1ef 1128 msleep(5);
fbf49ea2
VS
1129 line2 = I915_READ(reg) & line_mask;
1130
1131 return line1 == line2;
1132}
1133
ab7ad7f6
KP
1134/*
1135 * intel_wait_for_pipe_off - wait for pipe to turn off
575f7ab7 1136 * @crtc: crtc whose pipe to wait for
9d0498a2
JB
1137 *
1138 * After disabling a pipe, we can't wait for vblank in the usual way,
1139 * spinning on the vblank interrupt status bit, since we won't actually
1140 * see an interrupt when the pipe is disabled.
1141 *
ab7ad7f6
KP
1142 * On Gen4 and above:
1143 * wait for the pipe register state bit to turn off
1144 *
1145 * Otherwise:
1146 * wait for the display line value to settle (it usually
1147 * ends up stopping at the start of the next frame).
58e10eb9 1148 *
9d0498a2 1149 */
575f7ab7 1150static void intel_wait_for_pipe_off(struct intel_crtc *crtc)
9d0498a2 1151{
575f7ab7 1152 struct drm_device *dev = crtc->base.dev;
9d0498a2 1153 struct drm_i915_private *dev_priv = dev->dev_private;
6e3c9717 1154 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
575f7ab7 1155 enum pipe pipe = crtc->pipe;
ab7ad7f6
KP
1156
1157 if (INTEL_INFO(dev)->gen >= 4) {
f0f59a00 1158 i915_reg_t reg = PIPECONF(cpu_transcoder);
ab7ad7f6
KP
1159
1160 /* Wait for the Pipe State to go off */
58e10eb9
CW
1161 if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
1162 100))
284637d9 1163 WARN(1, "pipe_off wait timed out\n");
ab7ad7f6 1164 } else {
ab7ad7f6 1165 /* Wait for the display line to settle */
fbf49ea2 1166 if (wait_for(pipe_dsl_stopped(dev, pipe), 100))
284637d9 1167 WARN(1, "pipe_off wait timed out\n");
ab7ad7f6 1168 }
79e53945
JB
1169}
1170
b24e7179 1171/* Only for pre-ILK configs */
55607e8a
DV
1172void assert_pll(struct drm_i915_private *dev_priv,
1173 enum pipe pipe, bool state)
b24e7179 1174{
b24e7179
JB
1175 u32 val;
1176 bool cur_state;
1177
649636ef 1178 val = I915_READ(DPLL(pipe));
b24e7179 1179 cur_state = !!(val & DPLL_VCO_ENABLE);
e2c719b7 1180 I915_STATE_WARN(cur_state != state,
b24e7179 1181 "PLL state assertion failure (expected %s, current %s)\n",
87ad3212 1182 onoff(state), onoff(cur_state));
b24e7179 1183}
b24e7179 1184
23538ef1
JN
1185/* XXX: the dsi pll is shared between MIPI DSI ports */
1186static void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
1187{
1188 u32 val;
1189 bool cur_state;
1190
a580516d 1191 mutex_lock(&dev_priv->sb_lock);
23538ef1 1192 val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
a580516d 1193 mutex_unlock(&dev_priv->sb_lock);
23538ef1
JN
1194
1195 cur_state = val & DSI_PLL_VCO_EN;
e2c719b7 1196 I915_STATE_WARN(cur_state != state,
23538ef1 1197 "DSI PLL state assertion failure (expected %s, current %s)\n",
87ad3212 1198 onoff(state), onoff(cur_state));
23538ef1
JN
1199}
1200#define assert_dsi_pll_enabled(d) assert_dsi_pll(d, true)
1201#define assert_dsi_pll_disabled(d) assert_dsi_pll(d, false)
1202
040484af
JB
1203static void assert_fdi_tx(struct drm_i915_private *dev_priv,
1204 enum pipe pipe, bool state)
1205{
040484af 1206 bool cur_state;
ad80a810
PZ
1207 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1208 pipe);
040484af 1209
affa9354
PZ
1210 if (HAS_DDI(dev_priv->dev)) {
1211 /* DDI does not have a specific FDI_TX register */
649636ef 1212 u32 val = I915_READ(TRANS_DDI_FUNC_CTL(cpu_transcoder));
ad80a810 1213 cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
bf507ef7 1214 } else {
649636ef 1215 u32 val = I915_READ(FDI_TX_CTL(pipe));
bf507ef7
ED
1216 cur_state = !!(val & FDI_TX_ENABLE);
1217 }
e2c719b7 1218 I915_STATE_WARN(cur_state != state,
040484af 1219 "FDI TX state assertion failure (expected %s, current %s)\n",
87ad3212 1220 onoff(state), onoff(cur_state));
040484af
JB
1221}
1222#define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
1223#define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
1224
1225static void assert_fdi_rx(struct drm_i915_private *dev_priv,
1226 enum pipe pipe, bool state)
1227{
040484af
JB
1228 u32 val;
1229 bool cur_state;
1230
649636ef 1231 val = I915_READ(FDI_RX_CTL(pipe));
d63fa0dc 1232 cur_state = !!(val & FDI_RX_ENABLE);
e2c719b7 1233 I915_STATE_WARN(cur_state != state,
040484af 1234 "FDI RX state assertion failure (expected %s, current %s)\n",
87ad3212 1235 onoff(state), onoff(cur_state));
040484af
JB
1236}
1237#define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
1238#define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
1239
1240static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
1241 enum pipe pipe)
1242{
040484af
JB
1243 u32 val;
1244
1245 /* ILK FDI PLL is always enabled */
3d13ef2e 1246 if (INTEL_INFO(dev_priv->dev)->gen == 5)
040484af
JB
1247 return;
1248
bf507ef7 1249 /* On Haswell, DDI ports are responsible for the FDI PLL setup */
affa9354 1250 if (HAS_DDI(dev_priv->dev))
bf507ef7
ED
1251 return;
1252
649636ef 1253 val = I915_READ(FDI_TX_CTL(pipe));
e2c719b7 1254 I915_STATE_WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
040484af
JB
1255}
1256
55607e8a
DV
1257void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
1258 enum pipe pipe, bool state)
040484af 1259{
040484af 1260 u32 val;
55607e8a 1261 bool cur_state;
040484af 1262
649636ef 1263 val = I915_READ(FDI_RX_CTL(pipe));
55607e8a 1264 cur_state = !!(val & FDI_RX_PLL_ENABLE);
e2c719b7 1265 I915_STATE_WARN(cur_state != state,
55607e8a 1266 "FDI RX PLL assertion failure (expected %s, current %s)\n",
87ad3212 1267 onoff(state), onoff(cur_state));
040484af
JB
1268}
1269
b680c37a
DV
1270void assert_panel_unlocked(struct drm_i915_private *dev_priv,
1271 enum pipe pipe)
ea0760cf 1272{
bedd4dba 1273 struct drm_device *dev = dev_priv->dev;
f0f59a00 1274 i915_reg_t pp_reg;
ea0760cf
JB
1275 u32 val;
1276 enum pipe panel_pipe = PIPE_A;
0de3b485 1277 bool locked = true;
ea0760cf 1278
bedd4dba
JN
1279 if (WARN_ON(HAS_DDI(dev)))
1280 return;
1281
1282 if (HAS_PCH_SPLIT(dev)) {
1283 u32 port_sel;
1284
ea0760cf 1285 pp_reg = PCH_PP_CONTROL;
bedd4dba
JN
1286 port_sel = I915_READ(PCH_PP_ON_DELAYS) & PANEL_PORT_SELECT_MASK;
1287
1288 if (port_sel == PANEL_PORT_SELECT_LVDS &&
1289 I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
1290 panel_pipe = PIPE_B;
1291 /* XXX: else fix for eDP */
666a4537 1292 } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
bedd4dba
JN
1293 /* presumably write lock depends on pipe, not port select */
1294 pp_reg = VLV_PIPE_PP_CONTROL(pipe);
1295 panel_pipe = pipe;
ea0760cf
JB
1296 } else {
1297 pp_reg = PP_CONTROL;
bedd4dba
JN
1298 if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
1299 panel_pipe = PIPE_B;
ea0760cf
JB
1300 }
1301
1302 val = I915_READ(pp_reg);
1303 if (!(val & PANEL_POWER_ON) ||
ec49ba2d 1304 ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
ea0760cf
JB
1305 locked = false;
1306
e2c719b7 1307 I915_STATE_WARN(panel_pipe == pipe && locked,
ea0760cf 1308 "panel assertion failure, pipe %c regs locked\n",
9db4a9c7 1309 pipe_name(pipe));
ea0760cf
JB
1310}
1311
93ce0ba6
JN
1312static void assert_cursor(struct drm_i915_private *dev_priv,
1313 enum pipe pipe, bool state)
1314{
1315 struct drm_device *dev = dev_priv->dev;
1316 bool cur_state;
1317
d9d82081 1318 if (IS_845G(dev) || IS_I865G(dev))
0b87c24e 1319 cur_state = I915_READ(CURCNTR(PIPE_A)) & CURSOR_ENABLE;
d9d82081 1320 else
5efb3e28 1321 cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
93ce0ba6 1322
e2c719b7 1323 I915_STATE_WARN(cur_state != state,
93ce0ba6 1324 "cursor on pipe %c assertion failure (expected %s, current %s)\n",
87ad3212 1325 pipe_name(pipe), onoff(state), onoff(cur_state));
93ce0ba6
JN
1326}
1327#define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
1328#define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
1329
b840d907
JB
1330void assert_pipe(struct drm_i915_private *dev_priv,
1331 enum pipe pipe, bool state)
b24e7179 1332{
63d7bbe9 1333 bool cur_state;
702e7a56
PZ
1334 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1335 pipe);
4feed0eb 1336 enum intel_display_power_domain power_domain;
b24e7179 1337
b6b5d049
VS
1338 /* if we need the pipe quirk it must be always on */
1339 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1340 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
8e636784
DV
1341 state = true;
1342
4feed0eb
ID
1343 power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
1344 if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
649636ef 1345 u32 val = I915_READ(PIPECONF(cpu_transcoder));
69310161 1346 cur_state = !!(val & PIPECONF_ENABLE);
4feed0eb
ID
1347
1348 intel_display_power_put(dev_priv, power_domain);
1349 } else {
1350 cur_state = false;
69310161
PZ
1351 }
1352
e2c719b7 1353 I915_STATE_WARN(cur_state != state,
63d7bbe9 1354 "pipe %c assertion failure (expected %s, current %s)\n",
87ad3212 1355 pipe_name(pipe), onoff(state), onoff(cur_state));
b24e7179
JB
1356}
1357
931872fc
CW
1358static void assert_plane(struct drm_i915_private *dev_priv,
1359 enum plane plane, bool state)
b24e7179 1360{
b24e7179 1361 u32 val;
931872fc 1362 bool cur_state;
b24e7179 1363
649636ef 1364 val = I915_READ(DSPCNTR(plane));
931872fc 1365 cur_state = !!(val & DISPLAY_PLANE_ENABLE);
e2c719b7 1366 I915_STATE_WARN(cur_state != state,
931872fc 1367 "plane %c assertion failure (expected %s, current %s)\n",
87ad3212 1368 plane_name(plane), onoff(state), onoff(cur_state));
b24e7179
JB
1369}
1370
931872fc
CW
1371#define assert_plane_enabled(d, p) assert_plane(d, p, true)
1372#define assert_plane_disabled(d, p) assert_plane(d, p, false)
1373
b24e7179
JB
1374static void assert_planes_disabled(struct drm_i915_private *dev_priv,
1375 enum pipe pipe)
1376{
653e1026 1377 struct drm_device *dev = dev_priv->dev;
649636ef 1378 int i;
b24e7179 1379
653e1026
VS
1380 /* Primary planes are fixed to pipes on gen4+ */
1381 if (INTEL_INFO(dev)->gen >= 4) {
649636ef 1382 u32 val = I915_READ(DSPCNTR(pipe));
e2c719b7 1383 I915_STATE_WARN(val & DISPLAY_PLANE_ENABLE,
28c05794
AJ
1384 "plane %c assertion failure, should be disabled but not\n",
1385 plane_name(pipe));
19ec1358 1386 return;
28c05794 1387 }
19ec1358 1388
b24e7179 1389 /* Need to check both planes against the pipe */
055e393f 1390 for_each_pipe(dev_priv, i) {
649636ef
VS
1391 u32 val = I915_READ(DSPCNTR(i));
1392 enum pipe cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
b24e7179 1393 DISPPLANE_SEL_PIPE_SHIFT;
e2c719b7 1394 I915_STATE_WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
9db4a9c7
JB
1395 "plane %c assertion failure, should be off on pipe %c but is still active\n",
1396 plane_name(i), pipe_name(pipe));
b24e7179
JB
1397 }
1398}
1399
19332d7a
JB
1400static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
1401 enum pipe pipe)
1402{
20674eef 1403 struct drm_device *dev = dev_priv->dev;
649636ef 1404 int sprite;
19332d7a 1405
7feb8b88 1406 if (INTEL_INFO(dev)->gen >= 9) {
3bdcfc0c 1407 for_each_sprite(dev_priv, pipe, sprite) {
649636ef 1408 u32 val = I915_READ(PLANE_CTL(pipe, sprite));
e2c719b7 1409 I915_STATE_WARN(val & PLANE_CTL_ENABLE,
7feb8b88
DL
1410 "plane %d assertion failure, should be off on pipe %c but is still active\n",
1411 sprite, pipe_name(pipe));
1412 }
666a4537 1413 } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
3bdcfc0c 1414 for_each_sprite(dev_priv, pipe, sprite) {
649636ef 1415 u32 val = I915_READ(SPCNTR(pipe, sprite));
e2c719b7 1416 I915_STATE_WARN(val & SP_ENABLE,
20674eef 1417 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
1fe47785 1418 sprite_name(pipe, sprite), pipe_name(pipe));
20674eef
VS
1419 }
1420 } else if (INTEL_INFO(dev)->gen >= 7) {
649636ef 1421 u32 val = I915_READ(SPRCTL(pipe));
e2c719b7 1422 I915_STATE_WARN(val & SPRITE_ENABLE,
06da8da2 1423 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
20674eef
VS
1424 plane_name(pipe), pipe_name(pipe));
1425 } else if (INTEL_INFO(dev)->gen >= 5) {
649636ef 1426 u32 val = I915_READ(DVSCNTR(pipe));
e2c719b7 1427 I915_STATE_WARN(val & DVS_ENABLE,
06da8da2 1428 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
20674eef 1429 plane_name(pipe), pipe_name(pipe));
19332d7a
JB
1430 }
1431}
1432
08c71e5e
VS
1433static void assert_vblank_disabled(struct drm_crtc *crtc)
1434{
e2c719b7 1435 if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
08c71e5e
VS
1436 drm_crtc_vblank_put(crtc);
1437}
1438
7abd4b35
ACO
1439void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
1440 enum pipe pipe)
92f2584a 1441{
92f2584a
JB
1442 u32 val;
1443 bool enabled;
1444
649636ef 1445 val = I915_READ(PCH_TRANSCONF(pipe));
92f2584a 1446 enabled = !!(val & TRANS_ENABLE);
e2c719b7 1447 I915_STATE_WARN(enabled,
9db4a9c7
JB
1448 "transcoder assertion failed, should be off on pipe %c but is still active\n",
1449 pipe_name(pipe));
92f2584a
JB
1450}
1451
4e634389
KP
1452static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
1453 enum pipe pipe, u32 port_sel, u32 val)
f0575e92
KP
1454{
1455 if ((val & DP_PORT_EN) == 0)
1456 return false;
1457
1458 if (HAS_PCH_CPT(dev_priv->dev)) {
f0f59a00 1459 u32 trans_dp_ctl = I915_READ(TRANS_DP_CTL(pipe));
f0575e92
KP
1460 if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
1461 return false;
44f37d1f
CML
1462 } else if (IS_CHERRYVIEW(dev_priv->dev)) {
1463 if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
1464 return false;
f0575e92
KP
1465 } else {
1466 if ((val & DP_PIPE_MASK) != (pipe << 30))
1467 return false;
1468 }
1469 return true;
1470}
1471
1519b995
KP
1472static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
1473 enum pipe pipe, u32 val)
1474{
dc0fa718 1475 if ((val & SDVO_ENABLE) == 0)
1519b995
KP
1476 return false;
1477
1478 if (HAS_PCH_CPT(dev_priv->dev)) {
dc0fa718 1479 if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
1519b995 1480 return false;
44f37d1f
CML
1481 } else if (IS_CHERRYVIEW(dev_priv->dev)) {
1482 if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
1483 return false;
1519b995 1484 } else {
dc0fa718 1485 if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
1519b995
KP
1486 return false;
1487 }
1488 return true;
1489}
1490
1491static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
1492 enum pipe pipe, u32 val)
1493{
1494 if ((val & LVDS_PORT_EN) == 0)
1495 return false;
1496
1497 if (HAS_PCH_CPT(dev_priv->dev)) {
1498 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1499 return false;
1500 } else {
1501 if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
1502 return false;
1503 }
1504 return true;
1505}
1506
1507static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
1508 enum pipe pipe, u32 val)
1509{
1510 if ((val & ADPA_DAC_ENABLE) == 0)
1511 return false;
1512 if (HAS_PCH_CPT(dev_priv->dev)) {
1513 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1514 return false;
1515 } else {
1516 if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
1517 return false;
1518 }
1519 return true;
1520}
1521
291906f1 1522static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
f0f59a00
VS
1523 enum pipe pipe, i915_reg_t reg,
1524 u32 port_sel)
291906f1 1525{
47a05eca 1526 u32 val = I915_READ(reg);
e2c719b7 1527 I915_STATE_WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
291906f1 1528 "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
f0f59a00 1529 i915_mmio_reg_offset(reg), pipe_name(pipe));
de9a35ab 1530
e2c719b7 1531 I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & DP_PORT_EN) == 0
75c5da27 1532 && (val & DP_PIPEB_SELECT),
de9a35ab 1533 "IBX PCH dp port still using transcoder B\n");
291906f1
JB
1534}
1535
1536static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
f0f59a00 1537 enum pipe pipe, i915_reg_t reg)
291906f1 1538{
47a05eca 1539 u32 val = I915_READ(reg);
e2c719b7 1540 I915_STATE_WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
23c99e77 1541 "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
f0f59a00 1542 i915_mmio_reg_offset(reg), pipe_name(pipe));
de9a35ab 1543
e2c719b7 1544 I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & SDVO_ENABLE) == 0
75c5da27 1545 && (val & SDVO_PIPE_B_SELECT),
de9a35ab 1546 "IBX PCH hdmi port still using transcoder B\n");
291906f1
JB
1547}
1548
1549static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
1550 enum pipe pipe)
1551{
291906f1 1552 u32 val;
291906f1 1553
f0575e92
KP
1554 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
1555 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
1556 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
291906f1 1557
649636ef 1558 val = I915_READ(PCH_ADPA);
e2c719b7 1559 I915_STATE_WARN(adpa_pipe_enabled(dev_priv, pipe, val),
291906f1 1560 "PCH VGA enabled on transcoder %c, should be disabled\n",
9db4a9c7 1561 pipe_name(pipe));
291906f1 1562
649636ef 1563 val = I915_READ(PCH_LVDS);
e2c719b7 1564 I915_STATE_WARN(lvds_pipe_enabled(dev_priv, pipe, val),
291906f1 1565 "PCH LVDS enabled on transcoder %c, should be disabled\n",
9db4a9c7 1566 pipe_name(pipe));
291906f1 1567
e2debe91
PZ
1568 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
1569 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
1570 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
291906f1
JB
1571}
1572
d288f65f 1573static void vlv_enable_pll(struct intel_crtc *crtc,
5cec258b 1574 const struct intel_crtc_state *pipe_config)
87442f73 1575{
426115cf
DV
1576 struct drm_device *dev = crtc->base.dev;
1577 struct drm_i915_private *dev_priv = dev->dev_private;
f0f59a00 1578 i915_reg_t reg = DPLL(crtc->pipe);
d288f65f 1579 u32 dpll = pipe_config->dpll_hw_state.dpll;
87442f73 1580
426115cf 1581 assert_pipe_disabled(dev_priv, crtc->pipe);
87442f73 1582
87442f73 1583 /* PLL is protected by panel, make sure we can write it */
6a9e7363 1584 if (IS_MOBILE(dev_priv->dev))
426115cf 1585 assert_panel_unlocked(dev_priv, crtc->pipe);
87442f73 1586
426115cf
DV
1587 I915_WRITE(reg, dpll);
1588 POSTING_READ(reg);
1589 udelay(150);
1590
1591 if (wait_for(((I915_READ(reg) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
1592 DRM_ERROR("DPLL %d failed to lock\n", crtc->pipe);
1593
d288f65f 1594 I915_WRITE(DPLL_MD(crtc->pipe), pipe_config->dpll_hw_state.dpll_md);
426115cf 1595 POSTING_READ(DPLL_MD(crtc->pipe));
87442f73
DV
1596
1597 /* We do this three times for luck */
426115cf 1598 I915_WRITE(reg, dpll);
87442f73
DV
1599 POSTING_READ(reg);
1600 udelay(150); /* wait for warmup */
426115cf 1601 I915_WRITE(reg, dpll);
87442f73
DV
1602 POSTING_READ(reg);
1603 udelay(150); /* wait for warmup */
426115cf 1604 I915_WRITE(reg, dpll);
87442f73
DV
1605 POSTING_READ(reg);
1606 udelay(150); /* wait for warmup */
1607}
1608
d288f65f 1609static void chv_enable_pll(struct intel_crtc *crtc,
5cec258b 1610 const struct intel_crtc_state *pipe_config)
9d556c99
CML
1611{
1612 struct drm_device *dev = crtc->base.dev;
1613 struct drm_i915_private *dev_priv = dev->dev_private;
1614 int pipe = crtc->pipe;
1615 enum dpio_channel port = vlv_pipe_to_channel(pipe);
9d556c99
CML
1616 u32 tmp;
1617
1618 assert_pipe_disabled(dev_priv, crtc->pipe);
1619
a580516d 1620 mutex_lock(&dev_priv->sb_lock);
9d556c99
CML
1621
1622 /* Enable back the 10bit clock to display controller */
1623 tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1624 tmp |= DPIO_DCLKP_EN;
1625 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
1626
54433e91
VS
1627 mutex_unlock(&dev_priv->sb_lock);
1628
9d556c99
CML
1629 /*
1630 * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
1631 */
1632 udelay(1);
1633
1634 /* Enable PLL */
d288f65f 1635 I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
9d556c99
CML
1636
1637 /* Check PLL is locked */
a11b0703 1638 if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
9d556c99
CML
1639 DRM_ERROR("PLL %d failed to lock\n", pipe);
1640
a11b0703 1641 /* not sure when this should be written */
d288f65f 1642 I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
a11b0703 1643 POSTING_READ(DPLL_MD(pipe));
9d556c99
CML
1644}
1645
1c4e0274
VS
1646static int intel_num_dvo_pipes(struct drm_device *dev)
1647{
1648 struct intel_crtc *crtc;
1649 int count = 0;
1650
1651 for_each_intel_crtc(dev, crtc)
3538b9df 1652 count += crtc->base.state->active &&
409ee761 1653 intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO);
1c4e0274
VS
1654
1655 return count;
1656}
1657
66e3d5c0 1658static void i9xx_enable_pll(struct intel_crtc *crtc)
63d7bbe9 1659{
66e3d5c0
DV
1660 struct drm_device *dev = crtc->base.dev;
1661 struct drm_i915_private *dev_priv = dev->dev_private;
f0f59a00 1662 i915_reg_t reg = DPLL(crtc->pipe);
6e3c9717 1663 u32 dpll = crtc->config->dpll_hw_state.dpll;
63d7bbe9 1664
66e3d5c0 1665 assert_pipe_disabled(dev_priv, crtc->pipe);
58c6eaa2 1666
63d7bbe9 1667 /* No really, not for ILK+ */
3d13ef2e 1668 BUG_ON(INTEL_INFO(dev)->gen >= 5);
63d7bbe9
JB
1669
1670 /* PLL is protected by panel, make sure we can write it */
66e3d5c0
DV
1671 if (IS_MOBILE(dev) && !IS_I830(dev))
1672 assert_panel_unlocked(dev_priv, crtc->pipe);
63d7bbe9 1673
1c4e0274
VS
1674 /* Enable DVO 2x clock on both PLLs if necessary */
1675 if (IS_I830(dev) && intel_num_dvo_pipes(dev) > 0) {
1676 /*
1677 * It appears to be important that we don't enable this
1678 * for the current pipe before otherwise configuring the
1679 * PLL. No idea how this should be handled if multiple
1680 * DVO outputs are enabled simultaneosly.
1681 */
1682 dpll |= DPLL_DVO_2X_MODE;
1683 I915_WRITE(DPLL(!crtc->pipe),
1684 I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
1685 }
66e3d5c0 1686
c2b63374
VS
1687 /*
1688 * Apparently we need to have VGA mode enabled prior to changing
1689 * the P1/P2 dividers. Otherwise the DPLL will keep using the old
1690 * dividers, even though the register value does change.
1691 */
1692 I915_WRITE(reg, 0);
1693
8e7a65aa
VS
1694 I915_WRITE(reg, dpll);
1695
66e3d5c0
DV
1696 /* Wait for the clocks to stabilize. */
1697 POSTING_READ(reg);
1698 udelay(150);
1699
1700 if (INTEL_INFO(dev)->gen >= 4) {
1701 I915_WRITE(DPLL_MD(crtc->pipe),
6e3c9717 1702 crtc->config->dpll_hw_state.dpll_md);
66e3d5c0
DV
1703 } else {
1704 /* The pixel multiplier can only be updated once the
1705 * DPLL is enabled and the clocks are stable.
1706 *
1707 * So write it again.
1708 */
1709 I915_WRITE(reg, dpll);
1710 }
63d7bbe9
JB
1711
1712 /* We do this three times for luck */
66e3d5c0 1713 I915_WRITE(reg, dpll);
63d7bbe9
JB
1714 POSTING_READ(reg);
1715 udelay(150); /* wait for warmup */
66e3d5c0 1716 I915_WRITE(reg, dpll);
63d7bbe9
JB
1717 POSTING_READ(reg);
1718 udelay(150); /* wait for warmup */
66e3d5c0 1719 I915_WRITE(reg, dpll);
63d7bbe9
JB
1720 POSTING_READ(reg);
1721 udelay(150); /* wait for warmup */
1722}
1723
1724/**
50b44a44 1725 * i9xx_disable_pll - disable a PLL
63d7bbe9
JB
1726 * @dev_priv: i915 private structure
1727 * @pipe: pipe PLL to disable
1728 *
1729 * Disable the PLL for @pipe, making sure the pipe is off first.
1730 *
1731 * Note! This is for pre-ILK only.
1732 */
1c4e0274 1733static void i9xx_disable_pll(struct intel_crtc *crtc)
63d7bbe9 1734{
1c4e0274
VS
1735 struct drm_device *dev = crtc->base.dev;
1736 struct drm_i915_private *dev_priv = dev->dev_private;
1737 enum pipe pipe = crtc->pipe;
1738
1739 /* Disable DVO 2x clock on both PLLs if necessary */
1740 if (IS_I830(dev) &&
409ee761 1741 intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO) &&
3538b9df 1742 !intel_num_dvo_pipes(dev)) {
1c4e0274
VS
1743 I915_WRITE(DPLL(PIPE_B),
1744 I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
1745 I915_WRITE(DPLL(PIPE_A),
1746 I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
1747 }
1748
b6b5d049
VS
1749 /* Don't disable pipe or pipe PLLs if needed */
1750 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1751 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
63d7bbe9
JB
1752 return;
1753
1754 /* Make sure the pipe isn't still relying on us */
1755 assert_pipe_disabled(dev_priv, pipe);
1756
b8afb911 1757 I915_WRITE(DPLL(pipe), DPLL_VGA_MODE_DIS);
50b44a44 1758 POSTING_READ(DPLL(pipe));
63d7bbe9
JB
1759}
1760
f6071166
JB
1761static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1762{
b8afb911 1763 u32 val;
f6071166
JB
1764
1765 /* Make sure the pipe isn't still relying on us */
1766 assert_pipe_disabled(dev_priv, pipe);
1767
e5cbfbfb
ID
1768 /*
1769 * Leave integrated clock source and reference clock enabled for pipe B.
1770 * The latter is needed for VGA hotplug / manual detection.
1771 */
b8afb911 1772 val = DPLL_VGA_MODE_DIS;
f6071166 1773 if (pipe == PIPE_B)
60bfe44f 1774 val = DPLL_INTEGRATED_CRI_CLK_VLV | DPLL_REF_CLK_ENABLE_VLV;
f6071166
JB
1775 I915_WRITE(DPLL(pipe), val);
1776 POSTING_READ(DPLL(pipe));
076ed3b2
CML
1777
1778}
1779
1780static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1781{
d752048d 1782 enum dpio_channel port = vlv_pipe_to_channel(pipe);
076ed3b2
CML
1783 u32 val;
1784
a11b0703
VS
1785 /* Make sure the pipe isn't still relying on us */
1786 assert_pipe_disabled(dev_priv, pipe);
076ed3b2 1787
a11b0703 1788 /* Set PLL en = 0 */
60bfe44f
VS
1789 val = DPLL_SSC_REF_CLK_CHV |
1790 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
a11b0703
VS
1791 if (pipe != PIPE_A)
1792 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
1793 I915_WRITE(DPLL(pipe), val);
1794 POSTING_READ(DPLL(pipe));
d752048d 1795
a580516d 1796 mutex_lock(&dev_priv->sb_lock);
d752048d
VS
1797
1798 /* Disable 10bit clock to display controller */
1799 val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1800 val &= ~DPIO_DCLKP_EN;
1801 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
1802
a580516d 1803 mutex_unlock(&dev_priv->sb_lock);
f6071166
JB
1804}
1805
e4607fcf 1806void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
9b6de0a1
VS
1807 struct intel_digital_port *dport,
1808 unsigned int expected_mask)
89b667f8
JB
1809{
1810 u32 port_mask;
f0f59a00 1811 i915_reg_t dpll_reg;
89b667f8 1812
e4607fcf
CML
1813 switch (dport->port) {
1814 case PORT_B:
89b667f8 1815 port_mask = DPLL_PORTB_READY_MASK;
00fc31b7 1816 dpll_reg = DPLL(0);
e4607fcf
CML
1817 break;
1818 case PORT_C:
89b667f8 1819 port_mask = DPLL_PORTC_READY_MASK;
00fc31b7 1820 dpll_reg = DPLL(0);
9b6de0a1 1821 expected_mask <<= 4;
00fc31b7
CML
1822 break;
1823 case PORT_D:
1824 port_mask = DPLL_PORTD_READY_MASK;
1825 dpll_reg = DPIO_PHY_STATUS;
e4607fcf
CML
1826 break;
1827 default:
1828 BUG();
1829 }
89b667f8 1830
9b6de0a1
VS
1831 if (wait_for((I915_READ(dpll_reg) & port_mask) == expected_mask, 1000))
1832 WARN(1, "timed out waiting for port %c ready: got 0x%x, expected 0x%x\n",
1833 port_name(dport->port), I915_READ(dpll_reg) & port_mask, expected_mask);
89b667f8
JB
1834}
1835
b8a4f404
PZ
1836static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1837 enum pipe pipe)
040484af 1838{
23670b32 1839 struct drm_device *dev = dev_priv->dev;
7c26e5c6 1840 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
e2b78267 1841 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
f0f59a00
VS
1842 i915_reg_t reg;
1843 uint32_t val, pipeconf_val;
040484af
JB
1844
1845 /* PCH only available on ILK+ */
55522f37 1846 BUG_ON(!HAS_PCH_SPLIT(dev));
040484af
JB
1847
1848 /* Make sure PCH DPLL is enabled */
8106ddbd 1849 assert_shared_dpll_enabled(dev_priv, intel_crtc->config->shared_dpll);
040484af
JB
1850
1851 /* FDI must be feeding us bits for PCH ports */
1852 assert_fdi_tx_enabled(dev_priv, pipe);
1853 assert_fdi_rx_enabled(dev_priv, pipe);
1854
23670b32
DV
1855 if (HAS_PCH_CPT(dev)) {
1856 /* Workaround: Set the timing override bit before enabling the
1857 * pch transcoder. */
1858 reg = TRANS_CHICKEN2(pipe);
1859 val = I915_READ(reg);
1860 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
1861 I915_WRITE(reg, val);
59c859d6 1862 }
23670b32 1863
ab9412ba 1864 reg = PCH_TRANSCONF(pipe);
040484af 1865 val = I915_READ(reg);
5f7f726d 1866 pipeconf_val = I915_READ(PIPECONF(pipe));
e9bcff5c
JB
1867
1868 if (HAS_PCH_IBX(dev_priv->dev)) {
1869 /*
c5de7c6f
VS
1870 * Make the BPC in transcoder be consistent with
1871 * that in pipeconf reg. For HDMI we must use 8bpc
1872 * here for both 8bpc and 12bpc.
e9bcff5c 1873 */
dfd07d72 1874 val &= ~PIPECONF_BPC_MASK;
c5de7c6f
VS
1875 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_HDMI))
1876 val |= PIPECONF_8BPC;
1877 else
1878 val |= pipeconf_val & PIPECONF_BPC_MASK;
e9bcff5c 1879 }
5f7f726d
PZ
1880
1881 val &= ~TRANS_INTERLACE_MASK;
1882 if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
7c26e5c6 1883 if (HAS_PCH_IBX(dev_priv->dev) &&
409ee761 1884 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
7c26e5c6
PZ
1885 val |= TRANS_LEGACY_INTERLACED_ILK;
1886 else
1887 val |= TRANS_INTERLACED;
5f7f726d
PZ
1888 else
1889 val |= TRANS_PROGRESSIVE;
1890
040484af
JB
1891 I915_WRITE(reg, val | TRANS_ENABLE);
1892 if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
4bb6f1f3 1893 DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
040484af
JB
1894}
1895
8fb033d7 1896static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
937bb610 1897 enum transcoder cpu_transcoder)
040484af 1898{
8fb033d7 1899 u32 val, pipeconf_val;
8fb033d7
PZ
1900
1901 /* PCH only available on ILK+ */
55522f37 1902 BUG_ON(!HAS_PCH_SPLIT(dev_priv->dev));
8fb033d7 1903
8fb033d7 1904 /* FDI must be feeding us bits for PCH ports */
1a240d4d 1905 assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
937bb610 1906 assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
8fb033d7 1907
223a6fdf 1908 /* Workaround: set timing override bit. */
36c0d0cf 1909 val = I915_READ(TRANS_CHICKEN2(PIPE_A));
23670b32 1910 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
36c0d0cf 1911 I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
223a6fdf 1912
25f3ef11 1913 val = TRANS_ENABLE;
937bb610 1914 pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
8fb033d7 1915
9a76b1c6
PZ
1916 if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
1917 PIPECONF_INTERLACED_ILK)
a35f2679 1918 val |= TRANS_INTERLACED;
8fb033d7
PZ
1919 else
1920 val |= TRANS_PROGRESSIVE;
1921
ab9412ba
DV
1922 I915_WRITE(LPT_TRANSCONF, val);
1923 if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
937bb610 1924 DRM_ERROR("Failed to enable PCH transcoder\n");
8fb033d7
PZ
1925}
1926
b8a4f404
PZ
1927static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
1928 enum pipe pipe)
040484af 1929{
23670b32 1930 struct drm_device *dev = dev_priv->dev;
f0f59a00
VS
1931 i915_reg_t reg;
1932 uint32_t val;
040484af
JB
1933
1934 /* FDI relies on the transcoder */
1935 assert_fdi_tx_disabled(dev_priv, pipe);
1936 assert_fdi_rx_disabled(dev_priv, pipe);
1937
291906f1
JB
1938 /* Ports must be off as well */
1939 assert_pch_ports_disabled(dev_priv, pipe);
1940
ab9412ba 1941 reg = PCH_TRANSCONF(pipe);
040484af
JB
1942 val = I915_READ(reg);
1943 val &= ~TRANS_ENABLE;
1944 I915_WRITE(reg, val);
1945 /* wait for PCH transcoder off, transcoder state */
1946 if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
4bb6f1f3 1947 DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
23670b32 1948
c465613b 1949 if (HAS_PCH_CPT(dev)) {
23670b32
DV
1950 /* Workaround: Clear the timing override chicken bit again. */
1951 reg = TRANS_CHICKEN2(pipe);
1952 val = I915_READ(reg);
1953 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
1954 I915_WRITE(reg, val);
1955 }
040484af
JB
1956}
1957
ab4d966c 1958static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
8fb033d7 1959{
8fb033d7
PZ
1960 u32 val;
1961
ab9412ba 1962 val = I915_READ(LPT_TRANSCONF);
8fb033d7 1963 val &= ~TRANS_ENABLE;
ab9412ba 1964 I915_WRITE(LPT_TRANSCONF, val);
8fb033d7 1965 /* wait for PCH transcoder off, transcoder state */
ab9412ba 1966 if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
8a52fd9f 1967 DRM_ERROR("Failed to disable PCH transcoder\n");
223a6fdf
PZ
1968
1969 /* Workaround: clear timing override bit. */
36c0d0cf 1970 val = I915_READ(TRANS_CHICKEN2(PIPE_A));
23670b32 1971 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
36c0d0cf 1972 I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
040484af
JB
1973}
1974
b24e7179 1975/**
309cfea8 1976 * intel_enable_pipe - enable a pipe, asserting requirements
0372264a 1977 * @crtc: crtc responsible for the pipe
b24e7179 1978 *
0372264a 1979 * Enable @crtc's pipe, making sure that various hardware specific requirements
b24e7179 1980 * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
b24e7179 1981 */
e1fdc473 1982static void intel_enable_pipe(struct intel_crtc *crtc)
b24e7179 1983{
0372264a
PZ
1984 struct drm_device *dev = crtc->base.dev;
1985 struct drm_i915_private *dev_priv = dev->dev_private;
1986 enum pipe pipe = crtc->pipe;
1a70a728 1987 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
1a240d4d 1988 enum pipe pch_transcoder;
f0f59a00 1989 i915_reg_t reg;
b24e7179
JB
1990 u32 val;
1991
9e2ee2dd
VS
1992 DRM_DEBUG_KMS("enabling pipe %c\n", pipe_name(pipe));
1993
58c6eaa2 1994 assert_planes_disabled(dev_priv, pipe);
93ce0ba6 1995 assert_cursor_disabled(dev_priv, pipe);
58c6eaa2
DV
1996 assert_sprites_disabled(dev_priv, pipe);
1997
681e5811 1998 if (HAS_PCH_LPT(dev_priv->dev))
cc391bbb
PZ
1999 pch_transcoder = TRANSCODER_A;
2000 else
2001 pch_transcoder = pipe;
2002
b24e7179
JB
2003 /*
2004 * A pipe without a PLL won't actually be able to drive bits from
2005 * a plane. On ILK+ the pipe PLLs are integrated, so we don't
2006 * need the check.
2007 */
50360403 2008 if (HAS_GMCH_DISPLAY(dev_priv->dev))
a65347ba 2009 if (crtc->config->has_dsi_encoder)
23538ef1
JN
2010 assert_dsi_pll_enabled(dev_priv);
2011 else
2012 assert_pll_enabled(dev_priv, pipe);
040484af 2013 else {
6e3c9717 2014 if (crtc->config->has_pch_encoder) {
040484af 2015 /* if driving the PCH, we need FDI enabled */
cc391bbb 2016 assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
1a240d4d
DV
2017 assert_fdi_tx_pll_enabled(dev_priv,
2018 (enum pipe) cpu_transcoder);
040484af
JB
2019 }
2020 /* FIXME: assert CPU port conditions for SNB+ */
2021 }
b24e7179 2022
702e7a56 2023 reg = PIPECONF(cpu_transcoder);
b24e7179 2024 val = I915_READ(reg);
7ad25d48 2025 if (val & PIPECONF_ENABLE) {
b6b5d049
VS
2026 WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
2027 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
00d70b15 2028 return;
7ad25d48 2029 }
00d70b15
CW
2030
2031 I915_WRITE(reg, val | PIPECONF_ENABLE);
851855d8 2032 POSTING_READ(reg);
b7792d8b
VS
2033
2034 /*
2035 * Until the pipe starts DSL will read as 0, which would cause
2036 * an apparent vblank timestamp jump, which messes up also the
2037 * frame count when it's derived from the timestamps. So let's
2038 * wait for the pipe to start properly before we call
2039 * drm_crtc_vblank_on()
2040 */
2041 if (dev->max_vblank_count == 0 &&
2042 wait_for(intel_get_crtc_scanline(crtc) != crtc->scanline_offset, 50))
2043 DRM_ERROR("pipe %c didn't start\n", pipe_name(pipe));
b24e7179
JB
2044}
2045
2046/**
309cfea8 2047 * intel_disable_pipe - disable a pipe, asserting requirements
575f7ab7 2048 * @crtc: crtc whose pipes is to be disabled
b24e7179 2049 *
575f7ab7
VS
2050 * Disable the pipe of @crtc, making sure that various hardware
2051 * specific requirements are met, if applicable, e.g. plane
2052 * disabled, panel fitter off, etc.
b24e7179
JB
2053 *
2054 * Will wait until the pipe has shut down before returning.
2055 */
575f7ab7 2056static void intel_disable_pipe(struct intel_crtc *crtc)
b24e7179 2057{
575f7ab7 2058 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
6e3c9717 2059 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
575f7ab7 2060 enum pipe pipe = crtc->pipe;
f0f59a00 2061 i915_reg_t reg;
b24e7179
JB
2062 u32 val;
2063
9e2ee2dd
VS
2064 DRM_DEBUG_KMS("disabling pipe %c\n", pipe_name(pipe));
2065
b24e7179
JB
2066 /*
2067 * Make sure planes won't keep trying to pump pixels to us,
2068 * or we might hang the display.
2069 */
2070 assert_planes_disabled(dev_priv, pipe);
93ce0ba6 2071 assert_cursor_disabled(dev_priv, pipe);
19332d7a 2072 assert_sprites_disabled(dev_priv, pipe);
b24e7179 2073
702e7a56 2074 reg = PIPECONF(cpu_transcoder);
b24e7179 2075 val = I915_READ(reg);
00d70b15
CW
2076 if ((val & PIPECONF_ENABLE) == 0)
2077 return;
2078
67adc644
VS
2079 /*
2080 * Double wide has implications for planes
2081 * so best keep it disabled when not needed.
2082 */
6e3c9717 2083 if (crtc->config->double_wide)
67adc644
VS
2084 val &= ~PIPECONF_DOUBLE_WIDE;
2085
2086 /* Don't disable pipe or pipe PLLs if needed */
b6b5d049
VS
2087 if (!(pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) &&
2088 !(pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
67adc644
VS
2089 val &= ~PIPECONF_ENABLE;
2090
2091 I915_WRITE(reg, val);
2092 if ((val & PIPECONF_ENABLE) == 0)
2093 intel_wait_for_pipe_off(crtc);
b24e7179
JB
2094}
2095
693db184
CW
2096static bool need_vtd_wa(struct drm_device *dev)
2097{
2098#ifdef CONFIG_INTEL_IOMMU
2099 if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
2100 return true;
2101#endif
2102 return false;
2103}
2104
832be82f
VS
2105static unsigned int intel_tile_size(const struct drm_i915_private *dev_priv)
2106{
2107 return IS_GEN2(dev_priv) ? 2048 : 4096;
2108}
2109
27ba3910
VS
2110static unsigned int intel_tile_width_bytes(const struct drm_i915_private *dev_priv,
2111 uint64_t fb_modifier, unsigned int cpp)
7b49f948
VS
2112{
2113 switch (fb_modifier) {
2114 case DRM_FORMAT_MOD_NONE:
2115 return cpp;
2116 case I915_FORMAT_MOD_X_TILED:
2117 if (IS_GEN2(dev_priv))
2118 return 128;
2119 else
2120 return 512;
2121 case I915_FORMAT_MOD_Y_TILED:
2122 if (IS_GEN2(dev_priv) || HAS_128_BYTE_Y_TILING(dev_priv))
2123 return 128;
2124 else
2125 return 512;
2126 case I915_FORMAT_MOD_Yf_TILED:
2127 switch (cpp) {
2128 case 1:
2129 return 64;
2130 case 2:
2131 case 4:
2132 return 128;
2133 case 8:
2134 case 16:
2135 return 256;
2136 default:
2137 MISSING_CASE(cpp);
2138 return cpp;
2139 }
2140 break;
2141 default:
2142 MISSING_CASE(fb_modifier);
2143 return cpp;
2144 }
2145}
2146
832be82f
VS
2147unsigned int intel_tile_height(const struct drm_i915_private *dev_priv,
2148 uint64_t fb_modifier, unsigned int cpp)
a57ce0b2 2149{
832be82f
VS
2150 if (fb_modifier == DRM_FORMAT_MOD_NONE)
2151 return 1;
2152 else
2153 return intel_tile_size(dev_priv) /
27ba3910 2154 intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
6761dd31
TU
2155}
2156
8d0deca8
VS
2157/* Return the tile dimensions in pixel units */
2158static void intel_tile_dims(const struct drm_i915_private *dev_priv,
2159 unsigned int *tile_width,
2160 unsigned int *tile_height,
2161 uint64_t fb_modifier,
2162 unsigned int cpp)
2163{
2164 unsigned int tile_width_bytes =
2165 intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
2166
2167 *tile_width = tile_width_bytes / cpp;
2168 *tile_height = intel_tile_size(dev_priv) / tile_width_bytes;
2169}
2170
6761dd31
TU
2171unsigned int
2172intel_fb_align_height(struct drm_device *dev, unsigned int height,
832be82f 2173 uint32_t pixel_format, uint64_t fb_modifier)
6761dd31 2174{
832be82f
VS
2175 unsigned int cpp = drm_format_plane_cpp(pixel_format, 0);
2176 unsigned int tile_height = intel_tile_height(to_i915(dev), fb_modifier, cpp);
2177
2178 return ALIGN(height, tile_height);
a57ce0b2
JB
2179}
2180
1663b9d6
VS
2181unsigned int intel_rotation_info_size(const struct intel_rotation_info *rot_info)
2182{
2183 unsigned int size = 0;
2184 int i;
2185
2186 for (i = 0 ; i < ARRAY_SIZE(rot_info->plane); i++)
2187 size += rot_info->plane[i].width * rot_info->plane[i].height;
2188
2189 return size;
2190}
2191
75c82a53 2192static void
3465c580
VS
2193intel_fill_fb_ggtt_view(struct i915_ggtt_view *view,
2194 const struct drm_framebuffer *fb,
2195 unsigned int rotation)
f64b98cd 2196{
2d7a215f
VS
2197 if (intel_rotation_90_or_270(rotation)) {
2198 *view = i915_ggtt_view_rotated;
2199 view->params.rotated = to_intel_framebuffer(fb)->rot_info;
2200 } else {
2201 *view = i915_ggtt_view_normal;
2202 }
2203}
50470bb0 2204
2d7a215f
VS
2205static void
2206intel_fill_fb_info(struct drm_i915_private *dev_priv,
2207 struct drm_framebuffer *fb)
2208{
2209 struct intel_rotation_info *info = &to_intel_framebuffer(fb)->rot_info;
2210 unsigned int tile_size, tile_width, tile_height, cpp;
50470bb0 2211
d9b3288e
VS
2212 tile_size = intel_tile_size(dev_priv);
2213
2214 cpp = drm_format_plane_cpp(fb->pixel_format, 0);
8d0deca8
VS
2215 intel_tile_dims(dev_priv, &tile_width, &tile_height,
2216 fb->modifier[0], cpp);
d9b3288e 2217
1663b9d6
VS
2218 info->plane[0].width = DIV_ROUND_UP(fb->pitches[0], tile_width * cpp);
2219 info->plane[0].height = DIV_ROUND_UP(fb->height, tile_height);
84fe03f7 2220
89e3e142 2221 if (info->pixel_format == DRM_FORMAT_NV12) {
832be82f 2222 cpp = drm_format_plane_cpp(fb->pixel_format, 1);
8d0deca8
VS
2223 intel_tile_dims(dev_priv, &tile_width, &tile_height,
2224 fb->modifier[1], cpp);
d9b3288e 2225
2d7a215f 2226 info->uv_offset = fb->offsets[1];
1663b9d6
VS
2227 info->plane[1].width = DIV_ROUND_UP(fb->pitches[1], tile_width * cpp);
2228 info->plane[1].height = DIV_ROUND_UP(fb->height / 2, tile_height);
89e3e142 2229 }
f64b98cd
TU
2230}
2231
603525d7 2232static unsigned int intel_linear_alignment(const struct drm_i915_private *dev_priv)
4e9a86b6
VS
2233{
2234 if (INTEL_INFO(dev_priv)->gen >= 9)
2235 return 256 * 1024;
985b8bb4 2236 else if (IS_BROADWATER(dev_priv) || IS_CRESTLINE(dev_priv) ||
666a4537 2237 IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
4e9a86b6
VS
2238 return 128 * 1024;
2239 else if (INTEL_INFO(dev_priv)->gen >= 4)
2240 return 4 * 1024;
2241 else
44c5905e 2242 return 0;
4e9a86b6
VS
2243}
2244
603525d7
VS
2245static unsigned int intel_surf_alignment(const struct drm_i915_private *dev_priv,
2246 uint64_t fb_modifier)
2247{
2248 switch (fb_modifier) {
2249 case DRM_FORMAT_MOD_NONE:
2250 return intel_linear_alignment(dev_priv);
2251 case I915_FORMAT_MOD_X_TILED:
2252 if (INTEL_INFO(dev_priv)->gen >= 9)
2253 return 256 * 1024;
2254 return 0;
2255 case I915_FORMAT_MOD_Y_TILED:
2256 case I915_FORMAT_MOD_Yf_TILED:
2257 return 1 * 1024 * 1024;
2258 default:
2259 MISSING_CASE(fb_modifier);
2260 return 0;
2261 }
2262}
2263
127bd2ac 2264int
3465c580
VS
2265intel_pin_and_fence_fb_obj(struct drm_framebuffer *fb,
2266 unsigned int rotation)
6b95a207 2267{
850c4cdc 2268 struct drm_device *dev = fb->dev;
ce453d81 2269 struct drm_i915_private *dev_priv = dev->dev_private;
850c4cdc 2270 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
f64b98cd 2271 struct i915_ggtt_view view;
6b95a207
KH
2272 u32 alignment;
2273 int ret;
2274
ebcdd39e
MR
2275 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
2276
603525d7 2277 alignment = intel_surf_alignment(dev_priv, fb->modifier[0]);
6b95a207 2278
3465c580 2279 intel_fill_fb_ggtt_view(&view, fb, rotation);
f64b98cd 2280
693db184
CW
2281 /* Note that the w/a also requires 64 PTE of padding following the
2282 * bo. We currently fill all unused PTE with the shadow page and so
2283 * we should always have valid PTE following the scanout preventing
2284 * the VT-d warning.
2285 */
2286 if (need_vtd_wa(dev) && alignment < 256 * 1024)
2287 alignment = 256 * 1024;
2288
d6dd6843
PZ
2289 /*
2290 * Global gtt pte registers are special registers which actually forward
2291 * writes to a chunk of system memory. Which means that there is no risk
2292 * that the register values disappear as soon as we call
2293 * intel_runtime_pm_put(), so it is correct to wrap only the
2294 * pin/unpin/fence and not more.
2295 */
2296 intel_runtime_pm_get(dev_priv);
2297
7580d774
ML
2298 ret = i915_gem_object_pin_to_display_plane(obj, alignment,
2299 &view);
48b956c5 2300 if (ret)
b26a6b35 2301 goto err_pm;
6b95a207
KH
2302
2303 /* Install a fence for tiled scan-out. Pre-i965 always needs a
2304 * fence, whereas 965+ only requires a fence if using
2305 * framebuffer compression. For simplicity, we always install
2306 * a fence as the cost is not that onerous.
2307 */
9807216f
VK
2308 if (view.type == I915_GGTT_VIEW_NORMAL) {
2309 ret = i915_gem_object_get_fence(obj);
2310 if (ret == -EDEADLK) {
2311 /*
2312 * -EDEADLK means there are no free fences
2313 * no pending flips.
2314 *
2315 * This is propagated to atomic, but it uses
2316 * -EDEADLK to force a locking recovery, so
2317 * change the returned error to -EBUSY.
2318 */
2319 ret = -EBUSY;
2320 goto err_unpin;
2321 } else if (ret)
2322 goto err_unpin;
1690e1eb 2323
9807216f
VK
2324 i915_gem_object_pin_fence(obj);
2325 }
6b95a207 2326
d6dd6843 2327 intel_runtime_pm_put(dev_priv);
6b95a207 2328 return 0;
48b956c5
CW
2329
2330err_unpin:
f64b98cd 2331 i915_gem_object_unpin_from_display_plane(obj, &view);
b26a6b35 2332err_pm:
d6dd6843 2333 intel_runtime_pm_put(dev_priv);
48b956c5 2334 return ret;
6b95a207
KH
2335}
2336
3465c580 2337static void intel_unpin_fb_obj(struct drm_framebuffer *fb, unsigned int rotation)
1690e1eb 2338{
82bc3b2d 2339 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
f64b98cd 2340 struct i915_ggtt_view view;
82bc3b2d 2341
ebcdd39e
MR
2342 WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
2343
3465c580 2344 intel_fill_fb_ggtt_view(&view, fb, rotation);
f64b98cd 2345
9807216f
VK
2346 if (view.type == I915_GGTT_VIEW_NORMAL)
2347 i915_gem_object_unpin_fence(obj);
2348
f64b98cd 2349 i915_gem_object_unpin_from_display_plane(obj, &view);
1690e1eb
CW
2350}
2351
29cf9491
VS
2352/*
2353 * Adjust the tile offset by moving the difference into
2354 * the x/y offsets.
2355 *
2356 * Input tile dimensions and pitch must already be
2357 * rotated to match x and y, and in pixel units.
2358 */
2359static u32 intel_adjust_tile_offset(int *x, int *y,
2360 unsigned int tile_width,
2361 unsigned int tile_height,
2362 unsigned int tile_size,
2363 unsigned int pitch_tiles,
2364 u32 old_offset,
2365 u32 new_offset)
2366{
2367 unsigned int tiles;
2368
2369 WARN_ON(old_offset & (tile_size - 1));
2370 WARN_ON(new_offset & (tile_size - 1));
2371 WARN_ON(new_offset > old_offset);
2372
2373 tiles = (old_offset - new_offset) / tile_size;
2374
2375 *y += tiles / pitch_tiles * tile_height;
2376 *x += tiles % pitch_tiles * tile_width;
2377
2378 return new_offset;
2379}
2380
8d0deca8
VS
2381/*
2382 * Computes the linear offset to the base tile and adjusts
2383 * x, y. bytes per pixel is assumed to be a power-of-two.
2384 *
2385 * In the 90/270 rotated case, x and y are assumed
2386 * to be already rotated to match the rotated GTT view, and
2387 * pitch is the tile_height aligned framebuffer height.
2388 */
4f2d9934
VS
2389u32 intel_compute_tile_offset(int *x, int *y,
2390 const struct drm_framebuffer *fb, int plane,
8d0deca8
VS
2391 unsigned int pitch,
2392 unsigned int rotation)
c2c75131 2393{
4f2d9934
VS
2394 const struct drm_i915_private *dev_priv = to_i915(fb->dev);
2395 uint64_t fb_modifier = fb->modifier[plane];
2396 unsigned int cpp = drm_format_plane_cpp(fb->pixel_format, plane);
29cf9491
VS
2397 u32 offset, offset_aligned, alignment;
2398
2399 alignment = intel_surf_alignment(dev_priv, fb_modifier);
2400 if (alignment)
2401 alignment--;
2402
b5c65338 2403 if (fb_modifier != DRM_FORMAT_MOD_NONE) {
8d0deca8
VS
2404 unsigned int tile_size, tile_width, tile_height;
2405 unsigned int tile_rows, tiles, pitch_tiles;
c2c75131 2406
d843310d 2407 tile_size = intel_tile_size(dev_priv);
8d0deca8
VS
2408 intel_tile_dims(dev_priv, &tile_width, &tile_height,
2409 fb_modifier, cpp);
2410
2411 if (intel_rotation_90_or_270(rotation)) {
2412 pitch_tiles = pitch / tile_height;
2413 swap(tile_width, tile_height);
2414 } else {
2415 pitch_tiles = pitch / (tile_width * cpp);
2416 }
d843310d
VS
2417
2418 tile_rows = *y / tile_height;
2419 *y %= tile_height;
c2c75131 2420
8d0deca8
VS
2421 tiles = *x / tile_width;
2422 *x %= tile_width;
bc752862 2423
29cf9491
VS
2424 offset = (tile_rows * pitch_tiles + tiles) * tile_size;
2425 offset_aligned = offset & ~alignment;
bc752862 2426
29cf9491
VS
2427 intel_adjust_tile_offset(x, y, tile_width, tile_height,
2428 tile_size, pitch_tiles,
2429 offset, offset_aligned);
2430 } else {
bc752862 2431 offset = *y * pitch + *x * cpp;
29cf9491
VS
2432 offset_aligned = offset & ~alignment;
2433
4e9a86b6
VS
2434 *y = (offset & alignment) / pitch;
2435 *x = ((offset & alignment) - *y * pitch) / cpp;
bc752862 2436 }
29cf9491
VS
2437
2438 return offset_aligned;
c2c75131
DV
2439}
2440
b35d63fa 2441static int i9xx_format_to_fourcc(int format)
46f297fb
JB
2442{
2443 switch (format) {
2444 case DISPPLANE_8BPP:
2445 return DRM_FORMAT_C8;
2446 case DISPPLANE_BGRX555:
2447 return DRM_FORMAT_XRGB1555;
2448 case DISPPLANE_BGRX565:
2449 return DRM_FORMAT_RGB565;
2450 default:
2451 case DISPPLANE_BGRX888:
2452 return DRM_FORMAT_XRGB8888;
2453 case DISPPLANE_RGBX888:
2454 return DRM_FORMAT_XBGR8888;
2455 case DISPPLANE_BGRX101010:
2456 return DRM_FORMAT_XRGB2101010;
2457 case DISPPLANE_RGBX101010:
2458 return DRM_FORMAT_XBGR2101010;
2459 }
2460}
2461
bc8d7dff
DL
2462static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
2463{
2464 switch (format) {
2465 case PLANE_CTL_FORMAT_RGB_565:
2466 return DRM_FORMAT_RGB565;
2467 default:
2468 case PLANE_CTL_FORMAT_XRGB_8888:
2469 if (rgb_order) {
2470 if (alpha)
2471 return DRM_FORMAT_ABGR8888;
2472 else
2473 return DRM_FORMAT_XBGR8888;
2474 } else {
2475 if (alpha)
2476 return DRM_FORMAT_ARGB8888;
2477 else
2478 return DRM_FORMAT_XRGB8888;
2479 }
2480 case PLANE_CTL_FORMAT_XRGB_2101010:
2481 if (rgb_order)
2482 return DRM_FORMAT_XBGR2101010;
2483 else
2484 return DRM_FORMAT_XRGB2101010;
2485 }
2486}
2487
5724dbd1 2488static bool
f6936e29
DV
2489intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
2490 struct intel_initial_plane_config *plane_config)
46f297fb
JB
2491{
2492 struct drm_device *dev = crtc->base.dev;
3badb49f 2493 struct drm_i915_private *dev_priv = to_i915(dev);
46f297fb
JB
2494 struct drm_i915_gem_object *obj = NULL;
2495 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
2d14030b 2496 struct drm_framebuffer *fb = &plane_config->fb->base;
f37b5c2b
DV
2497 u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
2498 u32 size_aligned = round_up(plane_config->base + plane_config->size,
2499 PAGE_SIZE);
2500
2501 size_aligned -= base_aligned;
46f297fb 2502
ff2652ea
CW
2503 if (plane_config->size == 0)
2504 return false;
2505
3badb49f
PZ
2506 /* If the FB is too big, just don't use it since fbdev is not very
2507 * important and we should probably use that space with FBC or other
2508 * features. */
62106b4f 2509 if (size_aligned * 2 > dev_priv->ggtt.stolen_usable_size)
3badb49f
PZ
2510 return false;
2511
12c83d99
TU
2512 mutex_lock(&dev->struct_mutex);
2513
f37b5c2b
DV
2514 obj = i915_gem_object_create_stolen_for_preallocated(dev,
2515 base_aligned,
2516 base_aligned,
2517 size_aligned);
12c83d99
TU
2518 if (!obj) {
2519 mutex_unlock(&dev->struct_mutex);
484b41dd 2520 return false;
12c83d99 2521 }
46f297fb 2522
49af449b
DL
2523 obj->tiling_mode = plane_config->tiling;
2524 if (obj->tiling_mode == I915_TILING_X)
6bf129df 2525 obj->stride = fb->pitches[0];
46f297fb 2526
6bf129df
DL
2527 mode_cmd.pixel_format = fb->pixel_format;
2528 mode_cmd.width = fb->width;
2529 mode_cmd.height = fb->height;
2530 mode_cmd.pitches[0] = fb->pitches[0];
18c5247e
DV
2531 mode_cmd.modifier[0] = fb->modifier[0];
2532 mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
46f297fb 2533
6bf129df 2534 if (intel_framebuffer_init(dev, to_intel_framebuffer(fb),
484b41dd 2535 &mode_cmd, obj)) {
46f297fb
JB
2536 DRM_DEBUG_KMS("intel fb init failed\n");
2537 goto out_unref_obj;
2538 }
12c83d99 2539
46f297fb 2540 mutex_unlock(&dev->struct_mutex);
484b41dd 2541
f6936e29 2542 DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
484b41dd 2543 return true;
46f297fb
JB
2544
2545out_unref_obj:
2546 drm_gem_object_unreference(&obj->base);
2547 mutex_unlock(&dev->struct_mutex);
484b41dd
JB
2548 return false;
2549}
2550
afd65eb4
MR
2551/* Update plane->state->fb to match plane->fb after driver-internal updates */
2552static void
2553update_state_fb(struct drm_plane *plane)
2554{
2555 if (plane->fb == plane->state->fb)
2556 return;
2557
2558 if (plane->state->fb)
2559 drm_framebuffer_unreference(plane->state->fb);
2560 plane->state->fb = plane->fb;
2561 if (plane->state->fb)
2562 drm_framebuffer_reference(plane->state->fb);
2563}
2564
5724dbd1 2565static void
f6936e29
DV
2566intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
2567 struct intel_initial_plane_config *plane_config)
484b41dd
JB
2568{
2569 struct drm_device *dev = intel_crtc->base.dev;
d9ceb816 2570 struct drm_i915_private *dev_priv = dev->dev_private;
484b41dd
JB
2571 struct drm_crtc *c;
2572 struct intel_crtc *i;
2ff8fde1 2573 struct drm_i915_gem_object *obj;
88595ac9 2574 struct drm_plane *primary = intel_crtc->base.primary;
be5651f2 2575 struct drm_plane_state *plane_state = primary->state;
200757f5
MR
2576 struct drm_crtc_state *crtc_state = intel_crtc->base.state;
2577 struct intel_plane *intel_plane = to_intel_plane(primary);
0a8d8a86
MR
2578 struct intel_plane_state *intel_state =
2579 to_intel_plane_state(plane_state);
88595ac9 2580 struct drm_framebuffer *fb;
484b41dd 2581
2d14030b 2582 if (!plane_config->fb)
484b41dd
JB
2583 return;
2584
f6936e29 2585 if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
88595ac9
DV
2586 fb = &plane_config->fb->base;
2587 goto valid_fb;
f55548b5 2588 }
484b41dd 2589
2d14030b 2590 kfree(plane_config->fb);
484b41dd
JB
2591
2592 /*
2593 * Failed to alloc the obj, check to see if we should share
2594 * an fb with another CRTC instead
2595 */
70e1e0ec 2596 for_each_crtc(dev, c) {
484b41dd
JB
2597 i = to_intel_crtc(c);
2598
2599 if (c == &intel_crtc->base)
2600 continue;
2601
2ff8fde1
MR
2602 if (!i->active)
2603 continue;
2604
88595ac9
DV
2605 fb = c->primary->fb;
2606 if (!fb)
484b41dd
JB
2607 continue;
2608
88595ac9 2609 obj = intel_fb_obj(fb);
2ff8fde1 2610 if (i915_gem_obj_ggtt_offset(obj) == plane_config->base) {
88595ac9
DV
2611 drm_framebuffer_reference(fb);
2612 goto valid_fb;
484b41dd
JB
2613 }
2614 }
88595ac9 2615
200757f5
MR
2616 /*
2617 * We've failed to reconstruct the BIOS FB. Current display state
2618 * indicates that the primary plane is visible, but has a NULL FB,
2619 * which will lead to problems later if we don't fix it up. The
2620 * simplest solution is to just disable the primary plane now and
2621 * pretend the BIOS never had it enabled.
2622 */
2623 to_intel_plane_state(plane_state)->visible = false;
2624 crtc_state->plane_mask &= ~(1 << drm_plane_index(primary));
2622a081 2625 intel_pre_disable_primary_noatomic(&intel_crtc->base);
200757f5
MR
2626 intel_plane->disable_plane(primary, &intel_crtc->base);
2627
88595ac9
DV
2628 return;
2629
2630valid_fb:
f44e2659
VS
2631 plane_state->src_x = 0;
2632 plane_state->src_y = 0;
be5651f2
ML
2633 plane_state->src_w = fb->width << 16;
2634 plane_state->src_h = fb->height << 16;
2635
f44e2659
VS
2636 plane_state->crtc_x = 0;
2637 plane_state->crtc_y = 0;
be5651f2
ML
2638 plane_state->crtc_w = fb->width;
2639 plane_state->crtc_h = fb->height;
2640
0a8d8a86
MR
2641 intel_state->src.x1 = plane_state->src_x;
2642 intel_state->src.y1 = plane_state->src_y;
2643 intel_state->src.x2 = plane_state->src_x + plane_state->src_w;
2644 intel_state->src.y2 = plane_state->src_y + plane_state->src_h;
2645 intel_state->dst.x1 = plane_state->crtc_x;
2646 intel_state->dst.y1 = plane_state->crtc_y;
2647 intel_state->dst.x2 = plane_state->crtc_x + plane_state->crtc_w;
2648 intel_state->dst.y2 = plane_state->crtc_y + plane_state->crtc_h;
2649
88595ac9
DV
2650 obj = intel_fb_obj(fb);
2651 if (obj->tiling_mode != I915_TILING_NONE)
2652 dev_priv->preserve_bios_swizzle = true;
2653
be5651f2
ML
2654 drm_framebuffer_reference(fb);
2655 primary->fb = primary->state->fb = fb;
36750f28 2656 primary->crtc = primary->state->crtc = &intel_crtc->base;
36750f28 2657 intel_crtc->base.state->plane_mask |= (1 << drm_plane_index(primary));
a9ff8714 2658 obj->frontbuffer_bits |= to_intel_plane(primary)->frontbuffer_bit;
46f297fb
JB
2659}
2660
a8d201af
ML
2661static void i9xx_update_primary_plane(struct drm_plane *primary,
2662 const struct intel_crtc_state *crtc_state,
2663 const struct intel_plane_state *plane_state)
81255565 2664{
a8d201af 2665 struct drm_device *dev = primary->dev;
81255565 2666 struct drm_i915_private *dev_priv = dev->dev_private;
a8d201af
ML
2667 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
2668 struct drm_framebuffer *fb = plane_state->base.fb;
2669 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
81255565 2670 int plane = intel_crtc->plane;
54ea9da8 2671 u32 linear_offset;
81255565 2672 u32 dspcntr;
f0f59a00 2673 i915_reg_t reg = DSPCNTR(plane);
8d0deca8 2674 unsigned int rotation = plane_state->base.rotation;
ac484963 2675 int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
54ea9da8
VS
2676 int x = plane_state->src.x1 >> 16;
2677 int y = plane_state->src.y1 >> 16;
c9ba6fad 2678
f45651ba
VS
2679 dspcntr = DISPPLANE_GAMMA_ENABLE;
2680
fdd508a6 2681 dspcntr |= DISPLAY_PLANE_ENABLE;
f45651ba
VS
2682
2683 if (INTEL_INFO(dev)->gen < 4) {
2684 if (intel_crtc->pipe == PIPE_B)
2685 dspcntr |= DISPPLANE_SEL_PIPE_B;
2686
2687 /* pipesrc and dspsize control the size that is scaled from,
2688 * which should always be the user's requested size.
2689 */
2690 I915_WRITE(DSPSIZE(plane),
a8d201af
ML
2691 ((crtc_state->pipe_src_h - 1) << 16) |
2692 (crtc_state->pipe_src_w - 1));
f45651ba 2693 I915_WRITE(DSPPOS(plane), 0);
c14b0485
VS
2694 } else if (IS_CHERRYVIEW(dev) && plane == PLANE_B) {
2695 I915_WRITE(PRIMSIZE(plane),
a8d201af
ML
2696 ((crtc_state->pipe_src_h - 1) << 16) |
2697 (crtc_state->pipe_src_w - 1));
c14b0485
VS
2698 I915_WRITE(PRIMPOS(plane), 0);
2699 I915_WRITE(PRIMCNSTALPHA(plane), 0);
f45651ba 2700 }
81255565 2701
57779d06
VS
2702 switch (fb->pixel_format) {
2703 case DRM_FORMAT_C8:
81255565
JB
2704 dspcntr |= DISPPLANE_8BPP;
2705 break;
57779d06 2706 case DRM_FORMAT_XRGB1555:
57779d06 2707 dspcntr |= DISPPLANE_BGRX555;
81255565 2708 break;
57779d06
VS
2709 case DRM_FORMAT_RGB565:
2710 dspcntr |= DISPPLANE_BGRX565;
2711 break;
2712 case DRM_FORMAT_XRGB8888:
57779d06
VS
2713 dspcntr |= DISPPLANE_BGRX888;
2714 break;
2715 case DRM_FORMAT_XBGR8888:
57779d06
VS
2716 dspcntr |= DISPPLANE_RGBX888;
2717 break;
2718 case DRM_FORMAT_XRGB2101010:
57779d06
VS
2719 dspcntr |= DISPPLANE_BGRX101010;
2720 break;
2721 case DRM_FORMAT_XBGR2101010:
57779d06 2722 dspcntr |= DISPPLANE_RGBX101010;
81255565
JB
2723 break;
2724 default:
baba133a 2725 BUG();
81255565 2726 }
57779d06 2727
f45651ba
VS
2728 if (INTEL_INFO(dev)->gen >= 4 &&
2729 obj->tiling_mode != I915_TILING_NONE)
2730 dspcntr |= DISPPLANE_TILED;
81255565 2731
de1aa629
VS
2732 if (IS_G4X(dev))
2733 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
2734
ac484963 2735 linear_offset = y * fb->pitches[0] + x * cpp;
81255565 2736
c2c75131
DV
2737 if (INTEL_INFO(dev)->gen >= 4) {
2738 intel_crtc->dspaddr_offset =
4f2d9934 2739 intel_compute_tile_offset(&x, &y, fb, 0,
8d0deca8 2740 fb->pitches[0], rotation);
c2c75131
DV
2741 linear_offset -= intel_crtc->dspaddr_offset;
2742 } else {
e506a0c6 2743 intel_crtc->dspaddr_offset = linear_offset;
c2c75131 2744 }
e506a0c6 2745
8d0deca8 2746 if (rotation == BIT(DRM_ROTATE_180)) {
48404c1e
SJ
2747 dspcntr |= DISPPLANE_ROTATE_180;
2748
a8d201af
ML
2749 x += (crtc_state->pipe_src_w - 1);
2750 y += (crtc_state->pipe_src_h - 1);
48404c1e
SJ
2751
2752 /* Finding the last pixel of the last line of the display
2753 data and adding to linear_offset*/
2754 linear_offset +=
a8d201af 2755 (crtc_state->pipe_src_h - 1) * fb->pitches[0] +
ac484963 2756 (crtc_state->pipe_src_w - 1) * cpp;
48404c1e
SJ
2757 }
2758
2db3366b
PZ
2759 intel_crtc->adjusted_x = x;
2760 intel_crtc->adjusted_y = y;
2761
48404c1e
SJ
2762 I915_WRITE(reg, dspcntr);
2763
01f2c773 2764 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
a6c45cf0 2765 if (INTEL_INFO(dev)->gen >= 4) {
85ba7b7d
DV
2766 I915_WRITE(DSPSURF(plane),
2767 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
5eddb70b 2768 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
e506a0c6 2769 I915_WRITE(DSPLINOFF(plane), linear_offset);
5eddb70b 2770 } else
f343c5f6 2771 I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
5eddb70b 2772 POSTING_READ(reg);
17638cd6
JB
2773}
2774
a8d201af
ML
2775static void i9xx_disable_primary_plane(struct drm_plane *primary,
2776 struct drm_crtc *crtc)
17638cd6
JB
2777{
2778 struct drm_device *dev = crtc->dev;
2779 struct drm_i915_private *dev_priv = dev->dev_private;
2780 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
17638cd6 2781 int plane = intel_crtc->plane;
f45651ba 2782
a8d201af
ML
2783 I915_WRITE(DSPCNTR(plane), 0);
2784 if (INTEL_INFO(dev_priv)->gen >= 4)
fdd508a6 2785 I915_WRITE(DSPSURF(plane), 0);
a8d201af
ML
2786 else
2787 I915_WRITE(DSPADDR(plane), 0);
2788 POSTING_READ(DSPCNTR(plane));
2789}
c9ba6fad 2790
a8d201af
ML
2791static void ironlake_update_primary_plane(struct drm_plane *primary,
2792 const struct intel_crtc_state *crtc_state,
2793 const struct intel_plane_state *plane_state)
2794{
2795 struct drm_device *dev = primary->dev;
2796 struct drm_i915_private *dev_priv = dev->dev_private;
2797 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
2798 struct drm_framebuffer *fb = plane_state->base.fb;
2799 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
2800 int plane = intel_crtc->plane;
54ea9da8 2801 u32 linear_offset;
a8d201af
ML
2802 u32 dspcntr;
2803 i915_reg_t reg = DSPCNTR(plane);
8d0deca8 2804 unsigned int rotation = plane_state->base.rotation;
ac484963 2805 int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
a8d201af
ML
2806 int x = plane_state->src.x1 >> 16;
2807 int y = plane_state->src.y1 >> 16;
c9ba6fad 2808
f45651ba 2809 dspcntr = DISPPLANE_GAMMA_ENABLE;
fdd508a6 2810 dspcntr |= DISPLAY_PLANE_ENABLE;
f45651ba
VS
2811
2812 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
2813 dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
17638cd6 2814
57779d06
VS
2815 switch (fb->pixel_format) {
2816 case DRM_FORMAT_C8:
17638cd6
JB
2817 dspcntr |= DISPPLANE_8BPP;
2818 break;
57779d06
VS
2819 case DRM_FORMAT_RGB565:
2820 dspcntr |= DISPPLANE_BGRX565;
17638cd6 2821 break;
57779d06 2822 case DRM_FORMAT_XRGB8888:
57779d06
VS
2823 dspcntr |= DISPPLANE_BGRX888;
2824 break;
2825 case DRM_FORMAT_XBGR8888:
57779d06
VS
2826 dspcntr |= DISPPLANE_RGBX888;
2827 break;
2828 case DRM_FORMAT_XRGB2101010:
57779d06
VS
2829 dspcntr |= DISPPLANE_BGRX101010;
2830 break;
2831 case DRM_FORMAT_XBGR2101010:
57779d06 2832 dspcntr |= DISPPLANE_RGBX101010;
17638cd6
JB
2833 break;
2834 default:
baba133a 2835 BUG();
17638cd6
JB
2836 }
2837
2838 if (obj->tiling_mode != I915_TILING_NONE)
2839 dspcntr |= DISPPLANE_TILED;
17638cd6 2840
f45651ba 2841 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev))
1f5d76db 2842 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
17638cd6 2843
ac484963 2844 linear_offset = y * fb->pitches[0] + x * cpp;
c2c75131 2845 intel_crtc->dspaddr_offset =
4f2d9934 2846 intel_compute_tile_offset(&x, &y, fb, 0,
8d0deca8 2847 fb->pitches[0], rotation);
c2c75131 2848 linear_offset -= intel_crtc->dspaddr_offset;
8d0deca8 2849 if (rotation == BIT(DRM_ROTATE_180)) {
48404c1e
SJ
2850 dspcntr |= DISPPLANE_ROTATE_180;
2851
2852 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev)) {
a8d201af
ML
2853 x += (crtc_state->pipe_src_w - 1);
2854 y += (crtc_state->pipe_src_h - 1);
48404c1e
SJ
2855
2856 /* Finding the last pixel of the last line of the display
2857 data and adding to linear_offset*/
2858 linear_offset +=
a8d201af 2859 (crtc_state->pipe_src_h - 1) * fb->pitches[0] +
ac484963 2860 (crtc_state->pipe_src_w - 1) * cpp;
48404c1e
SJ
2861 }
2862 }
2863
2db3366b
PZ
2864 intel_crtc->adjusted_x = x;
2865 intel_crtc->adjusted_y = y;
2866
48404c1e 2867 I915_WRITE(reg, dspcntr);
17638cd6 2868
01f2c773 2869 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
85ba7b7d
DV
2870 I915_WRITE(DSPSURF(plane),
2871 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
b3dc685e 2872 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
bc1c91eb
DL
2873 I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
2874 } else {
2875 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2876 I915_WRITE(DSPLINOFF(plane), linear_offset);
2877 }
17638cd6 2878 POSTING_READ(reg);
17638cd6
JB
2879}
2880
7b49f948
VS
2881u32 intel_fb_stride_alignment(const struct drm_i915_private *dev_priv,
2882 uint64_t fb_modifier, uint32_t pixel_format)
b321803d 2883{
7b49f948 2884 if (fb_modifier == DRM_FORMAT_MOD_NONE) {
b321803d 2885 return 64;
7b49f948
VS
2886 } else {
2887 int cpp = drm_format_plane_cpp(pixel_format, 0);
2888
27ba3910 2889 return intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
b321803d
DL
2890 }
2891}
2892
44eb0cb9
MK
2893u32 intel_plane_obj_offset(struct intel_plane *intel_plane,
2894 struct drm_i915_gem_object *obj,
2895 unsigned int plane)
121920fa 2896{
ce7f1728 2897 struct i915_ggtt_view view;
dedf278c 2898 struct i915_vma *vma;
44eb0cb9 2899 u64 offset;
121920fa 2900
e7941294 2901 intel_fill_fb_ggtt_view(&view, intel_plane->base.state->fb,
3465c580 2902 intel_plane->base.state->rotation);
121920fa 2903
ce7f1728 2904 vma = i915_gem_obj_to_ggtt_view(obj, &view);
dedf278c 2905 if (WARN(!vma, "ggtt vma for display object not found! (view=%u)\n",
ce7f1728 2906 view.type))
dedf278c
TU
2907 return -1;
2908
44eb0cb9 2909 offset = vma->node.start;
dedf278c
TU
2910
2911 if (plane == 1) {
7723f47d 2912 offset += vma->ggtt_view.params.rotated.uv_start_page *
dedf278c
TU
2913 PAGE_SIZE;
2914 }
2915
44eb0cb9
MK
2916 WARN_ON(upper_32_bits(offset));
2917
2918 return lower_32_bits(offset);
121920fa
TU
2919}
2920
e435d6e5
ML
2921static void skl_detach_scaler(struct intel_crtc *intel_crtc, int id)
2922{
2923 struct drm_device *dev = intel_crtc->base.dev;
2924 struct drm_i915_private *dev_priv = dev->dev_private;
2925
2926 I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, id), 0);
2927 I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, id), 0);
2928 I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, id), 0);
e435d6e5
ML
2929}
2930
a1b2278e
CK
2931/*
2932 * This function detaches (aka. unbinds) unused scalers in hardware
2933 */
0583236e 2934static void skl_detach_scalers(struct intel_crtc *intel_crtc)
a1b2278e 2935{
a1b2278e
CK
2936 struct intel_crtc_scaler_state *scaler_state;
2937 int i;
2938
a1b2278e
CK
2939 scaler_state = &intel_crtc->config->scaler_state;
2940
2941 /* loop through and disable scalers that aren't in use */
2942 for (i = 0; i < intel_crtc->num_scalers; i++) {
e435d6e5
ML
2943 if (!scaler_state->scalers[i].in_use)
2944 skl_detach_scaler(intel_crtc, i);
a1b2278e
CK
2945 }
2946}
2947
6156a456 2948u32 skl_plane_ctl_format(uint32_t pixel_format)
70d21f0e 2949{
6156a456 2950 switch (pixel_format) {
d161cf7a 2951 case DRM_FORMAT_C8:
c34ce3d1 2952 return PLANE_CTL_FORMAT_INDEXED;
70d21f0e 2953 case DRM_FORMAT_RGB565:
c34ce3d1 2954 return PLANE_CTL_FORMAT_RGB_565;
70d21f0e 2955 case DRM_FORMAT_XBGR8888:
c34ce3d1 2956 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
6156a456 2957 case DRM_FORMAT_XRGB8888:
c34ce3d1 2958 return PLANE_CTL_FORMAT_XRGB_8888;
6156a456
CK
2959 /*
2960 * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
2961 * to be already pre-multiplied. We need to add a knob (or a different
2962 * DRM_FORMAT) for user-space to configure that.
2963 */
f75fb42a 2964 case DRM_FORMAT_ABGR8888:
c34ce3d1 2965 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX |
6156a456 2966 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
6156a456 2967 case DRM_FORMAT_ARGB8888:
c34ce3d1 2968 return PLANE_CTL_FORMAT_XRGB_8888 |
6156a456 2969 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
70d21f0e 2970 case DRM_FORMAT_XRGB2101010:
c34ce3d1 2971 return PLANE_CTL_FORMAT_XRGB_2101010;
70d21f0e 2972 case DRM_FORMAT_XBGR2101010:
c34ce3d1 2973 return PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
6156a456 2974 case DRM_FORMAT_YUYV:
c34ce3d1 2975 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
6156a456 2976 case DRM_FORMAT_YVYU:
c34ce3d1 2977 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
6156a456 2978 case DRM_FORMAT_UYVY:
c34ce3d1 2979 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
6156a456 2980 case DRM_FORMAT_VYUY:
c34ce3d1 2981 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
70d21f0e 2982 default:
4249eeef 2983 MISSING_CASE(pixel_format);
70d21f0e 2984 }
8cfcba41 2985
c34ce3d1 2986 return 0;
6156a456 2987}
70d21f0e 2988
6156a456
CK
2989u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
2990{
6156a456 2991 switch (fb_modifier) {
30af77c4 2992 case DRM_FORMAT_MOD_NONE:
70d21f0e 2993 break;
30af77c4 2994 case I915_FORMAT_MOD_X_TILED:
c34ce3d1 2995 return PLANE_CTL_TILED_X;
b321803d 2996 case I915_FORMAT_MOD_Y_TILED:
c34ce3d1 2997 return PLANE_CTL_TILED_Y;
b321803d 2998 case I915_FORMAT_MOD_Yf_TILED:
c34ce3d1 2999 return PLANE_CTL_TILED_YF;
70d21f0e 3000 default:
6156a456 3001 MISSING_CASE(fb_modifier);
70d21f0e 3002 }
8cfcba41 3003
c34ce3d1 3004 return 0;
6156a456 3005}
70d21f0e 3006
6156a456
CK
3007u32 skl_plane_ctl_rotation(unsigned int rotation)
3008{
3b7a5119 3009 switch (rotation) {
6156a456
CK
3010 case BIT(DRM_ROTATE_0):
3011 break;
1e8df167
SJ
3012 /*
3013 * DRM_ROTATE_ is counter clockwise to stay compatible with Xrandr
3014 * while i915 HW rotation is clockwise, thats why this swapping.
3015 */
3b7a5119 3016 case BIT(DRM_ROTATE_90):
1e8df167 3017 return PLANE_CTL_ROTATE_270;
3b7a5119 3018 case BIT(DRM_ROTATE_180):
c34ce3d1 3019 return PLANE_CTL_ROTATE_180;
3b7a5119 3020 case BIT(DRM_ROTATE_270):
1e8df167 3021 return PLANE_CTL_ROTATE_90;
6156a456
CK
3022 default:
3023 MISSING_CASE(rotation);
3024 }
3025
c34ce3d1 3026 return 0;
6156a456
CK
3027}
3028
a8d201af
ML
3029static void skylake_update_primary_plane(struct drm_plane *plane,
3030 const struct intel_crtc_state *crtc_state,
3031 const struct intel_plane_state *plane_state)
6156a456 3032{
a8d201af 3033 struct drm_device *dev = plane->dev;
6156a456 3034 struct drm_i915_private *dev_priv = dev->dev_private;
a8d201af
ML
3035 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
3036 struct drm_framebuffer *fb = plane_state->base.fb;
3037 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
6156a456
CK
3038 int pipe = intel_crtc->pipe;
3039 u32 plane_ctl, stride_div, stride;
3040 u32 tile_height, plane_offset, plane_size;
a8d201af 3041 unsigned int rotation = plane_state->base.rotation;
6156a456 3042 int x_offset, y_offset;
44eb0cb9 3043 u32 surf_addr;
a8d201af
ML
3044 int scaler_id = plane_state->scaler_id;
3045 int src_x = plane_state->src.x1 >> 16;
3046 int src_y = plane_state->src.y1 >> 16;
3047 int src_w = drm_rect_width(&plane_state->src) >> 16;
3048 int src_h = drm_rect_height(&plane_state->src) >> 16;
3049 int dst_x = plane_state->dst.x1;
3050 int dst_y = plane_state->dst.y1;
3051 int dst_w = drm_rect_width(&plane_state->dst);
3052 int dst_h = drm_rect_height(&plane_state->dst);
70d21f0e 3053
6156a456
CK
3054 plane_ctl = PLANE_CTL_ENABLE |
3055 PLANE_CTL_PIPE_GAMMA_ENABLE |
3056 PLANE_CTL_PIPE_CSC_ENABLE;
3057
3058 plane_ctl |= skl_plane_ctl_format(fb->pixel_format);
3059 plane_ctl |= skl_plane_ctl_tiling(fb->modifier[0]);
3060 plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
6156a456
CK
3061 plane_ctl |= skl_plane_ctl_rotation(rotation);
3062
7b49f948 3063 stride_div = intel_fb_stride_alignment(dev_priv, fb->modifier[0],
b321803d 3064 fb->pixel_format);
dedf278c 3065 surf_addr = intel_plane_obj_offset(to_intel_plane(plane), obj, 0);
3b7a5119 3066
a42e5a23
PZ
3067 WARN_ON(drm_rect_width(&plane_state->src) == 0);
3068
3b7a5119 3069 if (intel_rotation_90_or_270(rotation)) {
832be82f
VS
3070 int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
3071
3b7a5119 3072 /* stride = Surface height in tiles */
832be82f 3073 tile_height = intel_tile_height(dev_priv, fb->modifier[0], cpp);
3b7a5119 3074 stride = DIV_ROUND_UP(fb->height, tile_height);
a8d201af
ML
3075 x_offset = stride * tile_height - src_y - src_h;
3076 y_offset = src_x;
6156a456 3077 plane_size = (src_w - 1) << 16 | (src_h - 1);
3b7a5119
SJ
3078 } else {
3079 stride = fb->pitches[0] / stride_div;
a8d201af
ML
3080 x_offset = src_x;
3081 y_offset = src_y;
6156a456 3082 plane_size = (src_h - 1) << 16 | (src_w - 1);
3b7a5119
SJ
3083 }
3084 plane_offset = y_offset << 16 | x_offset;
b321803d 3085
2db3366b
PZ
3086 intel_crtc->adjusted_x = x_offset;
3087 intel_crtc->adjusted_y = y_offset;
3088
70d21f0e 3089 I915_WRITE(PLANE_CTL(pipe, 0), plane_ctl);
3b7a5119
SJ
3090 I915_WRITE(PLANE_OFFSET(pipe, 0), plane_offset);
3091 I915_WRITE(PLANE_SIZE(pipe, 0), plane_size);
3092 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
6156a456
CK
3093
3094 if (scaler_id >= 0) {
3095 uint32_t ps_ctrl = 0;
3096
3097 WARN_ON(!dst_w || !dst_h);
3098 ps_ctrl = PS_SCALER_EN | PS_PLANE_SEL(0) |
3099 crtc_state->scaler_state.scalers[scaler_id].mode;
3100 I915_WRITE(SKL_PS_CTRL(pipe, scaler_id), ps_ctrl);
3101 I915_WRITE(SKL_PS_PWR_GATE(pipe, scaler_id), 0);
3102 I915_WRITE(SKL_PS_WIN_POS(pipe, scaler_id), (dst_x << 16) | dst_y);
3103 I915_WRITE(SKL_PS_WIN_SZ(pipe, scaler_id), (dst_w << 16) | dst_h);
3104 I915_WRITE(PLANE_POS(pipe, 0), 0);
3105 } else {
3106 I915_WRITE(PLANE_POS(pipe, 0), (dst_y << 16) | dst_x);
3107 }
3108
121920fa 3109 I915_WRITE(PLANE_SURF(pipe, 0), surf_addr);
70d21f0e
DL
3110
3111 POSTING_READ(PLANE_SURF(pipe, 0));
3112}
3113
a8d201af
ML
3114static void skylake_disable_primary_plane(struct drm_plane *primary,
3115 struct drm_crtc *crtc)
17638cd6
JB
3116{
3117 struct drm_device *dev = crtc->dev;
3118 struct drm_i915_private *dev_priv = dev->dev_private;
a8d201af 3119 int pipe = to_intel_crtc(crtc)->pipe;
17638cd6 3120
a8d201af
ML
3121 I915_WRITE(PLANE_CTL(pipe, 0), 0);
3122 I915_WRITE(PLANE_SURF(pipe, 0), 0);
3123 POSTING_READ(PLANE_SURF(pipe, 0));
3124}
29b9bde6 3125
a8d201af
ML
3126/* Assume fb object is pinned & idle & fenced and just update base pointers */
3127static int
3128intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
3129 int x, int y, enum mode_set_atomic state)
3130{
3131 /* Support for kgdboc is disabled, this needs a major rework. */
3132 DRM_ERROR("legacy panic handler not supported any more.\n");
3133
3134 return -ENODEV;
81255565
JB
3135}
3136
7514747d 3137static void intel_complete_page_flips(struct drm_device *dev)
96a02917 3138{
96a02917
VS
3139 struct drm_crtc *crtc;
3140
70e1e0ec 3141 for_each_crtc(dev, crtc) {
96a02917
VS
3142 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3143 enum plane plane = intel_crtc->plane;
3144
3145 intel_prepare_page_flip(dev, plane);
3146 intel_finish_page_flip_plane(dev, plane);
3147 }
7514747d
VS
3148}
3149
3150static void intel_update_primary_planes(struct drm_device *dev)
3151{
7514747d 3152 struct drm_crtc *crtc;
96a02917 3153
70e1e0ec 3154 for_each_crtc(dev, crtc) {
11c22da6
ML
3155 struct intel_plane *plane = to_intel_plane(crtc->primary);
3156 struct intel_plane_state *plane_state;
96a02917 3157
11c22da6 3158 drm_modeset_lock_crtc(crtc, &plane->base);
11c22da6
ML
3159 plane_state = to_intel_plane_state(plane->base.state);
3160
a8d201af
ML
3161 if (plane_state->visible)
3162 plane->update_plane(&plane->base,
3163 to_intel_crtc_state(crtc->state),
3164 plane_state);
11c22da6
ML
3165
3166 drm_modeset_unlock_crtc(crtc);
96a02917
VS
3167 }
3168}
3169
7514747d
VS
3170void intel_prepare_reset(struct drm_device *dev)
3171{
3172 /* no reset support for gen2 */
3173 if (IS_GEN2(dev))
3174 return;
3175
3176 /* reset doesn't touch the display */
3177 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
3178 return;
3179
3180 drm_modeset_lock_all(dev);
f98ce92f
VS
3181 /*
3182 * Disabling the crtcs gracefully seems nicer. Also the
3183 * g33 docs say we should at least disable all the planes.
3184 */
6b72d486 3185 intel_display_suspend(dev);
7514747d
VS
3186}
3187
3188void intel_finish_reset(struct drm_device *dev)
3189{
3190 struct drm_i915_private *dev_priv = to_i915(dev);
3191
3192 /*
3193 * Flips in the rings will be nuked by the reset,
3194 * so complete all pending flips so that user space
3195 * will get its events and not get stuck.
3196 */
3197 intel_complete_page_flips(dev);
3198
3199 /* no reset support for gen2 */
3200 if (IS_GEN2(dev))
3201 return;
3202
3203 /* reset doesn't touch the display */
3204 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev)) {
3205 /*
3206 * Flips in the rings have been nuked by the reset,
3207 * so update the base address of all primary
3208 * planes to the the last fb to make sure we're
3209 * showing the correct fb after a reset.
11c22da6
ML
3210 *
3211 * FIXME: Atomic will make this obsolete since we won't schedule
3212 * CS-based flips (which might get lost in gpu resets) any more.
7514747d
VS
3213 */
3214 intel_update_primary_planes(dev);
3215 return;
3216 }
3217
3218 /*
3219 * The display has been reset as well,
3220 * so need a full re-initialization.
3221 */
3222 intel_runtime_pm_disable_interrupts(dev_priv);
3223 intel_runtime_pm_enable_interrupts(dev_priv);
3224
3225 intel_modeset_init_hw(dev);
3226
3227 spin_lock_irq(&dev_priv->irq_lock);
3228 if (dev_priv->display.hpd_irq_setup)
3229 dev_priv->display.hpd_irq_setup(dev);
3230 spin_unlock_irq(&dev_priv->irq_lock);
3231
043e9bda 3232 intel_display_resume(dev);
7514747d
VS
3233
3234 intel_hpd_init(dev_priv);
3235
3236 drm_modeset_unlock_all(dev);
3237}
3238
7d5e3799
CW
3239static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
3240{
3241 struct drm_device *dev = crtc->dev;
3242 struct drm_i915_private *dev_priv = dev->dev_private;
3243 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7d5e3799
CW
3244 bool pending;
3245
3246 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
3247 intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
3248 return false;
3249
5e2d7afc 3250 spin_lock_irq(&dev->event_lock);
7d5e3799 3251 pending = to_intel_crtc(crtc)->unpin_work != NULL;
5e2d7afc 3252 spin_unlock_irq(&dev->event_lock);
7d5e3799
CW
3253
3254 return pending;
3255}
3256
bfd16b2a
ML
3257static void intel_update_pipe_config(struct intel_crtc *crtc,
3258 struct intel_crtc_state *old_crtc_state)
e30e8f75
GP
3259{
3260 struct drm_device *dev = crtc->base.dev;
3261 struct drm_i915_private *dev_priv = dev->dev_private;
bfd16b2a
ML
3262 struct intel_crtc_state *pipe_config =
3263 to_intel_crtc_state(crtc->base.state);
e30e8f75 3264
bfd16b2a
ML
3265 /* drm_atomic_helper_update_legacy_modeset_state might not be called. */
3266 crtc->base.mode = crtc->base.state->mode;
3267
3268 DRM_DEBUG_KMS("Updating pipe size %ix%i -> %ix%i\n",
3269 old_crtc_state->pipe_src_w, old_crtc_state->pipe_src_h,
3270 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
e30e8f75 3271
44522d85
ML
3272 if (HAS_DDI(dev))
3273 intel_set_pipe_csc(&crtc->base);
3274
e30e8f75
GP
3275 /*
3276 * Update pipe size and adjust fitter if needed: the reason for this is
3277 * that in compute_mode_changes we check the native mode (not the pfit
3278 * mode) to see if we can flip rather than do a full mode set. In the
3279 * fastboot case, we'll flip, but if we don't update the pipesrc and
3280 * pfit state, we'll end up with a big fb scanned out into the wrong
3281 * sized surface.
e30e8f75
GP
3282 */
3283
e30e8f75 3284 I915_WRITE(PIPESRC(crtc->pipe),
bfd16b2a
ML
3285 ((pipe_config->pipe_src_w - 1) << 16) |
3286 (pipe_config->pipe_src_h - 1));
3287
3288 /* on skylake this is done by detaching scalers */
3289 if (INTEL_INFO(dev)->gen >= 9) {
3290 skl_detach_scalers(crtc);
3291
3292 if (pipe_config->pch_pfit.enabled)
3293 skylake_pfit_enable(crtc);
3294 } else if (HAS_PCH_SPLIT(dev)) {
3295 if (pipe_config->pch_pfit.enabled)
3296 ironlake_pfit_enable(crtc);
3297 else if (old_crtc_state->pch_pfit.enabled)
3298 ironlake_pfit_disable(crtc, true);
e30e8f75 3299 }
e30e8f75
GP
3300}
3301
5e84e1a4
ZW
3302static void intel_fdi_normal_train(struct drm_crtc *crtc)
3303{
3304 struct drm_device *dev = crtc->dev;
3305 struct drm_i915_private *dev_priv = dev->dev_private;
3306 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3307 int pipe = intel_crtc->pipe;
f0f59a00
VS
3308 i915_reg_t reg;
3309 u32 temp;
5e84e1a4
ZW
3310
3311 /* enable normal train */
3312 reg = FDI_TX_CTL(pipe);
3313 temp = I915_READ(reg);
61e499bf 3314 if (IS_IVYBRIDGE(dev)) {
357555c0
JB
3315 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3316 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
61e499bf
KP
3317 } else {
3318 temp &= ~FDI_LINK_TRAIN_NONE;
3319 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
357555c0 3320 }
5e84e1a4
ZW
3321 I915_WRITE(reg, temp);
3322
3323 reg = FDI_RX_CTL(pipe);
3324 temp = I915_READ(reg);
3325 if (HAS_PCH_CPT(dev)) {
3326 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3327 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
3328 } else {
3329 temp &= ~FDI_LINK_TRAIN_NONE;
3330 temp |= FDI_LINK_TRAIN_NONE;
3331 }
3332 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
3333
3334 /* wait one idle pattern time */
3335 POSTING_READ(reg);
3336 udelay(1000);
357555c0
JB
3337
3338 /* IVB wants error correction enabled */
3339 if (IS_IVYBRIDGE(dev))
3340 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
3341 FDI_FE_ERRC_ENABLE);
5e84e1a4
ZW
3342}
3343
8db9d77b
ZW
3344/* The FDI link training functions for ILK/Ibexpeak. */
3345static void ironlake_fdi_link_train(struct drm_crtc *crtc)
3346{
3347 struct drm_device *dev = crtc->dev;
3348 struct drm_i915_private *dev_priv = dev->dev_private;
3349 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3350 int pipe = intel_crtc->pipe;
f0f59a00
VS
3351 i915_reg_t reg;
3352 u32 temp, tries;
8db9d77b 3353
1c8562f6 3354 /* FDI needs bits from pipe first */
0fc932b8 3355 assert_pipe_enabled(dev_priv, pipe);
0fc932b8 3356
e1a44743
AJ
3357 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3358 for train result */
5eddb70b
CW
3359 reg = FDI_RX_IMR(pipe);
3360 temp = I915_READ(reg);
e1a44743
AJ
3361 temp &= ~FDI_RX_SYMBOL_LOCK;
3362 temp &= ~FDI_RX_BIT_LOCK;
5eddb70b
CW
3363 I915_WRITE(reg, temp);
3364 I915_READ(reg);
e1a44743
AJ
3365 udelay(150);
3366
8db9d77b 3367 /* enable CPU FDI TX and PCH FDI RX */
5eddb70b
CW
3368 reg = FDI_TX_CTL(pipe);
3369 temp = I915_READ(reg);
627eb5a3 3370 temp &= ~FDI_DP_PORT_WIDTH_MASK;
6e3c9717 3371 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
8db9d77b
ZW
3372 temp &= ~FDI_LINK_TRAIN_NONE;
3373 temp |= FDI_LINK_TRAIN_PATTERN_1;
5eddb70b 3374 I915_WRITE(reg, temp | FDI_TX_ENABLE);
8db9d77b 3375
5eddb70b
CW
3376 reg = FDI_RX_CTL(pipe);
3377 temp = I915_READ(reg);
8db9d77b
ZW
3378 temp &= ~FDI_LINK_TRAIN_NONE;
3379 temp |= FDI_LINK_TRAIN_PATTERN_1;
5eddb70b
CW
3380 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3381
3382 POSTING_READ(reg);
8db9d77b
ZW
3383 udelay(150);
3384
5b2adf89 3385 /* Ironlake workaround, enable clock pointer after FDI enable*/
8f5718a6
DV
3386 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
3387 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
3388 FDI_RX_PHASE_SYNC_POINTER_EN);
5b2adf89 3389
5eddb70b 3390 reg = FDI_RX_IIR(pipe);
e1a44743 3391 for (tries = 0; tries < 5; tries++) {
5eddb70b 3392 temp = I915_READ(reg);
8db9d77b
ZW
3393 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3394
3395 if ((temp & FDI_RX_BIT_LOCK)) {
3396 DRM_DEBUG_KMS("FDI train 1 done.\n");
5eddb70b 3397 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
8db9d77b
ZW
3398 break;
3399 }
8db9d77b 3400 }
e1a44743 3401 if (tries == 5)
5eddb70b 3402 DRM_ERROR("FDI train 1 fail!\n");
8db9d77b
ZW
3403
3404 /* Train 2 */
5eddb70b
CW
3405 reg = FDI_TX_CTL(pipe);
3406 temp = I915_READ(reg);
8db9d77b
ZW
3407 temp &= ~FDI_LINK_TRAIN_NONE;
3408 temp |= FDI_LINK_TRAIN_PATTERN_2;
5eddb70b 3409 I915_WRITE(reg, temp);
8db9d77b 3410
5eddb70b
CW
3411 reg = FDI_RX_CTL(pipe);
3412 temp = I915_READ(reg);
8db9d77b
ZW
3413 temp &= ~FDI_LINK_TRAIN_NONE;
3414 temp |= FDI_LINK_TRAIN_PATTERN_2;
5eddb70b 3415 I915_WRITE(reg, temp);
8db9d77b 3416
5eddb70b
CW
3417 POSTING_READ(reg);
3418 udelay(150);
8db9d77b 3419
5eddb70b 3420 reg = FDI_RX_IIR(pipe);
e1a44743 3421 for (tries = 0; tries < 5; tries++) {
5eddb70b 3422 temp = I915_READ(reg);
8db9d77b
ZW
3423 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3424
3425 if (temp & FDI_RX_SYMBOL_LOCK) {
5eddb70b 3426 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
8db9d77b
ZW
3427 DRM_DEBUG_KMS("FDI train 2 done.\n");
3428 break;
3429 }
8db9d77b 3430 }
e1a44743 3431 if (tries == 5)
5eddb70b 3432 DRM_ERROR("FDI train 2 fail!\n");
8db9d77b
ZW
3433
3434 DRM_DEBUG_KMS("FDI train done\n");
5c5313c8 3435
8db9d77b
ZW
3436}
3437
0206e353 3438static const int snb_b_fdi_train_param[] = {
8db9d77b
ZW
3439 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
3440 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
3441 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
3442 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
3443};
3444
3445/* The FDI link training functions for SNB/Cougarpoint. */
3446static void gen6_fdi_link_train(struct drm_crtc *crtc)
3447{
3448 struct drm_device *dev = crtc->dev;
3449 struct drm_i915_private *dev_priv = dev->dev_private;
3450 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3451 int pipe = intel_crtc->pipe;
f0f59a00
VS
3452 i915_reg_t reg;
3453 u32 temp, i, retry;
8db9d77b 3454
e1a44743
AJ
3455 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3456 for train result */
5eddb70b
CW
3457 reg = FDI_RX_IMR(pipe);
3458 temp = I915_READ(reg);
e1a44743
AJ
3459 temp &= ~FDI_RX_SYMBOL_LOCK;
3460 temp &= ~FDI_RX_BIT_LOCK;
5eddb70b
CW
3461 I915_WRITE(reg, temp);
3462
3463 POSTING_READ(reg);
e1a44743
AJ
3464 udelay(150);
3465
8db9d77b 3466 /* enable CPU FDI TX and PCH FDI RX */
5eddb70b
CW
3467 reg = FDI_TX_CTL(pipe);
3468 temp = I915_READ(reg);
627eb5a3 3469 temp &= ~FDI_DP_PORT_WIDTH_MASK;
6e3c9717 3470 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
8db9d77b
ZW
3471 temp &= ~FDI_LINK_TRAIN_NONE;
3472 temp |= FDI_LINK_TRAIN_PATTERN_1;
3473 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3474 /* SNB-B */
3475 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
5eddb70b 3476 I915_WRITE(reg, temp | FDI_TX_ENABLE);
8db9d77b 3477
d74cf324
DV
3478 I915_WRITE(FDI_RX_MISC(pipe),
3479 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3480
5eddb70b
CW
3481 reg = FDI_RX_CTL(pipe);
3482 temp = I915_READ(reg);
8db9d77b
ZW
3483 if (HAS_PCH_CPT(dev)) {
3484 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3485 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3486 } else {
3487 temp &= ~FDI_LINK_TRAIN_NONE;
3488 temp |= FDI_LINK_TRAIN_PATTERN_1;
3489 }
5eddb70b
CW
3490 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3491
3492 POSTING_READ(reg);
8db9d77b
ZW
3493 udelay(150);
3494
0206e353 3495 for (i = 0; i < 4; i++) {
5eddb70b
CW
3496 reg = FDI_TX_CTL(pipe);
3497 temp = I915_READ(reg);
8db9d77b
ZW
3498 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3499 temp |= snb_b_fdi_train_param[i];
5eddb70b
CW
3500 I915_WRITE(reg, temp);
3501
3502 POSTING_READ(reg);
8db9d77b
ZW
3503 udelay(500);
3504
fa37d39e
SP
3505 for (retry = 0; retry < 5; retry++) {
3506 reg = FDI_RX_IIR(pipe);
3507 temp = I915_READ(reg);
3508 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3509 if (temp & FDI_RX_BIT_LOCK) {
3510 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3511 DRM_DEBUG_KMS("FDI train 1 done.\n");
3512 break;
3513 }
3514 udelay(50);
8db9d77b 3515 }
fa37d39e
SP
3516 if (retry < 5)
3517 break;
8db9d77b
ZW
3518 }
3519 if (i == 4)
5eddb70b 3520 DRM_ERROR("FDI train 1 fail!\n");
8db9d77b
ZW
3521
3522 /* Train 2 */
5eddb70b
CW
3523 reg = FDI_TX_CTL(pipe);
3524 temp = I915_READ(reg);
8db9d77b
ZW
3525 temp &= ~FDI_LINK_TRAIN_NONE;
3526 temp |= FDI_LINK_TRAIN_PATTERN_2;
3527 if (IS_GEN6(dev)) {
3528 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3529 /* SNB-B */
3530 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
3531 }
5eddb70b 3532 I915_WRITE(reg, temp);
8db9d77b 3533
5eddb70b
CW
3534 reg = FDI_RX_CTL(pipe);
3535 temp = I915_READ(reg);
8db9d77b
ZW
3536 if (HAS_PCH_CPT(dev)) {
3537 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3538 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
3539 } else {
3540 temp &= ~FDI_LINK_TRAIN_NONE;
3541 temp |= FDI_LINK_TRAIN_PATTERN_2;
3542 }
5eddb70b
CW
3543 I915_WRITE(reg, temp);
3544
3545 POSTING_READ(reg);
8db9d77b
ZW
3546 udelay(150);
3547
0206e353 3548 for (i = 0; i < 4; i++) {
5eddb70b
CW
3549 reg = FDI_TX_CTL(pipe);
3550 temp = I915_READ(reg);
8db9d77b
ZW
3551 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3552 temp |= snb_b_fdi_train_param[i];
5eddb70b
CW
3553 I915_WRITE(reg, temp);
3554
3555 POSTING_READ(reg);
8db9d77b
ZW
3556 udelay(500);
3557
fa37d39e
SP
3558 for (retry = 0; retry < 5; retry++) {
3559 reg = FDI_RX_IIR(pipe);
3560 temp = I915_READ(reg);
3561 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3562 if (temp & FDI_RX_SYMBOL_LOCK) {
3563 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3564 DRM_DEBUG_KMS("FDI train 2 done.\n");
3565 break;
3566 }
3567 udelay(50);
8db9d77b 3568 }
fa37d39e
SP
3569 if (retry < 5)
3570 break;
8db9d77b
ZW
3571 }
3572 if (i == 4)
5eddb70b 3573 DRM_ERROR("FDI train 2 fail!\n");
8db9d77b
ZW
3574
3575 DRM_DEBUG_KMS("FDI train done.\n");
3576}
3577
357555c0
JB
3578/* Manual link training for Ivy Bridge A0 parts */
3579static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
3580{
3581 struct drm_device *dev = crtc->dev;
3582 struct drm_i915_private *dev_priv = dev->dev_private;
3583 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3584 int pipe = intel_crtc->pipe;
f0f59a00
VS
3585 i915_reg_t reg;
3586 u32 temp, i, j;
357555c0
JB
3587
3588 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3589 for train result */
3590 reg = FDI_RX_IMR(pipe);
3591 temp = I915_READ(reg);
3592 temp &= ~FDI_RX_SYMBOL_LOCK;
3593 temp &= ~FDI_RX_BIT_LOCK;
3594 I915_WRITE(reg, temp);
3595
3596 POSTING_READ(reg);
3597 udelay(150);
3598
01a415fd
DV
3599 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
3600 I915_READ(FDI_RX_IIR(pipe)));
3601
139ccd3f
JB
3602 /* Try each vswing and preemphasis setting twice before moving on */
3603 for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
3604 /* disable first in case we need to retry */
3605 reg = FDI_TX_CTL(pipe);
3606 temp = I915_READ(reg);
3607 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
3608 temp &= ~FDI_TX_ENABLE;
3609 I915_WRITE(reg, temp);
357555c0 3610
139ccd3f
JB
3611 reg = FDI_RX_CTL(pipe);
3612 temp = I915_READ(reg);
3613 temp &= ~FDI_LINK_TRAIN_AUTO;
3614 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3615 temp &= ~FDI_RX_ENABLE;
3616 I915_WRITE(reg, temp);
357555c0 3617
139ccd3f 3618 /* enable CPU FDI TX and PCH FDI RX */
357555c0
JB
3619 reg = FDI_TX_CTL(pipe);
3620 temp = I915_READ(reg);
139ccd3f 3621 temp &= ~FDI_DP_PORT_WIDTH_MASK;
6e3c9717 3622 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
139ccd3f 3623 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
357555c0 3624 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
139ccd3f
JB
3625 temp |= snb_b_fdi_train_param[j/2];
3626 temp |= FDI_COMPOSITE_SYNC;
3627 I915_WRITE(reg, temp | FDI_TX_ENABLE);
357555c0 3628
139ccd3f
JB
3629 I915_WRITE(FDI_RX_MISC(pipe),
3630 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
357555c0 3631
139ccd3f 3632 reg = FDI_RX_CTL(pipe);
357555c0 3633 temp = I915_READ(reg);
139ccd3f
JB
3634 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3635 temp |= FDI_COMPOSITE_SYNC;
3636 I915_WRITE(reg, temp | FDI_RX_ENABLE);
357555c0 3637
139ccd3f
JB
3638 POSTING_READ(reg);
3639 udelay(1); /* should be 0.5us */
357555c0 3640
139ccd3f
JB
3641 for (i = 0; i < 4; i++) {
3642 reg = FDI_RX_IIR(pipe);
3643 temp = I915_READ(reg);
3644 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
357555c0 3645
139ccd3f
JB
3646 if (temp & FDI_RX_BIT_LOCK ||
3647 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
3648 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3649 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
3650 i);
3651 break;
3652 }
3653 udelay(1); /* should be 0.5us */
3654 }
3655 if (i == 4) {
3656 DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
3657 continue;
3658 }
357555c0 3659
139ccd3f 3660 /* Train 2 */
357555c0
JB
3661 reg = FDI_TX_CTL(pipe);
3662 temp = I915_READ(reg);
139ccd3f
JB
3663 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3664 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
3665 I915_WRITE(reg, temp);
3666
3667 reg = FDI_RX_CTL(pipe);
3668 temp = I915_READ(reg);
3669 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3670 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
357555c0
JB
3671 I915_WRITE(reg, temp);
3672
3673 POSTING_READ(reg);
139ccd3f 3674 udelay(2); /* should be 1.5us */
357555c0 3675
139ccd3f
JB
3676 for (i = 0; i < 4; i++) {
3677 reg = FDI_RX_IIR(pipe);
3678 temp = I915_READ(reg);
3679 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
357555c0 3680
139ccd3f
JB
3681 if (temp & FDI_RX_SYMBOL_LOCK ||
3682 (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
3683 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3684 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
3685 i);
3686 goto train_done;
3687 }
3688 udelay(2); /* should be 1.5us */
357555c0 3689 }
139ccd3f
JB
3690 if (i == 4)
3691 DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
357555c0 3692 }
357555c0 3693
139ccd3f 3694train_done:
357555c0
JB
3695 DRM_DEBUG_KMS("FDI train done.\n");
3696}
3697
88cefb6c 3698static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
2c07245f 3699{
88cefb6c 3700 struct drm_device *dev = intel_crtc->base.dev;
2c07245f 3701 struct drm_i915_private *dev_priv = dev->dev_private;
2c07245f 3702 int pipe = intel_crtc->pipe;
f0f59a00
VS
3703 i915_reg_t reg;
3704 u32 temp;
c64e311e 3705
c98e9dcf 3706 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
5eddb70b
CW
3707 reg = FDI_RX_CTL(pipe);
3708 temp = I915_READ(reg);
627eb5a3 3709 temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
6e3c9717 3710 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
dfd07d72 3711 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
5eddb70b
CW
3712 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
3713
3714 POSTING_READ(reg);
c98e9dcf
JB
3715 udelay(200);
3716
3717 /* Switch from Rawclk to PCDclk */
5eddb70b
CW
3718 temp = I915_READ(reg);
3719 I915_WRITE(reg, temp | FDI_PCDCLK);
3720
3721 POSTING_READ(reg);
c98e9dcf
JB
3722 udelay(200);
3723
20749730
PZ
3724 /* Enable CPU FDI TX PLL, always on for Ironlake */
3725 reg = FDI_TX_CTL(pipe);
3726 temp = I915_READ(reg);
3727 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
3728 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
5eddb70b 3729
20749730
PZ
3730 POSTING_READ(reg);
3731 udelay(100);
6be4a607 3732 }
0e23b99d
JB
3733}
3734
88cefb6c
DV
3735static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
3736{
3737 struct drm_device *dev = intel_crtc->base.dev;
3738 struct drm_i915_private *dev_priv = dev->dev_private;
3739 int pipe = intel_crtc->pipe;
f0f59a00
VS
3740 i915_reg_t reg;
3741 u32 temp;
88cefb6c
DV
3742
3743 /* Switch from PCDclk to Rawclk */
3744 reg = FDI_RX_CTL(pipe);
3745 temp = I915_READ(reg);
3746 I915_WRITE(reg, temp & ~FDI_PCDCLK);
3747
3748 /* Disable CPU FDI TX PLL */
3749 reg = FDI_TX_CTL(pipe);
3750 temp = I915_READ(reg);
3751 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
3752
3753 POSTING_READ(reg);
3754 udelay(100);
3755
3756 reg = FDI_RX_CTL(pipe);
3757 temp = I915_READ(reg);
3758 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
3759
3760 /* Wait for the clocks to turn off. */
3761 POSTING_READ(reg);
3762 udelay(100);
3763}
3764
0fc932b8
JB
3765static void ironlake_fdi_disable(struct drm_crtc *crtc)
3766{
3767 struct drm_device *dev = crtc->dev;
3768 struct drm_i915_private *dev_priv = dev->dev_private;
3769 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3770 int pipe = intel_crtc->pipe;
f0f59a00
VS
3771 i915_reg_t reg;
3772 u32 temp;
0fc932b8
JB
3773
3774 /* disable CPU FDI tx and PCH FDI rx */
3775 reg = FDI_TX_CTL(pipe);
3776 temp = I915_READ(reg);
3777 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
3778 POSTING_READ(reg);
3779
3780 reg = FDI_RX_CTL(pipe);
3781 temp = I915_READ(reg);
3782 temp &= ~(0x7 << 16);
dfd07d72 3783 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
0fc932b8
JB
3784 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
3785
3786 POSTING_READ(reg);
3787 udelay(100);
3788
3789 /* Ironlake workaround, disable clock pointer after downing FDI */
eba905b2 3790 if (HAS_PCH_IBX(dev))
6f06ce18 3791 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
0fc932b8
JB
3792
3793 /* still set train pattern 1 */
3794 reg = FDI_TX_CTL(pipe);
3795 temp = I915_READ(reg);
3796 temp &= ~FDI_LINK_TRAIN_NONE;
3797 temp |= FDI_LINK_TRAIN_PATTERN_1;
3798 I915_WRITE(reg, temp);
3799
3800 reg = FDI_RX_CTL(pipe);
3801 temp = I915_READ(reg);
3802 if (HAS_PCH_CPT(dev)) {
3803 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3804 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3805 } else {
3806 temp &= ~FDI_LINK_TRAIN_NONE;
3807 temp |= FDI_LINK_TRAIN_PATTERN_1;
3808 }
3809 /* BPC in FDI rx is consistent with that in PIPECONF */
3810 temp &= ~(0x07 << 16);
dfd07d72 3811 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
0fc932b8
JB
3812 I915_WRITE(reg, temp);
3813
3814 POSTING_READ(reg);
3815 udelay(100);
3816}
3817
5dce5b93
CW
3818bool intel_has_pending_fb_unpin(struct drm_device *dev)
3819{
3820 struct intel_crtc *crtc;
3821
3822 /* Note that we don't need to be called with mode_config.lock here
3823 * as our list of CRTC objects is static for the lifetime of the
3824 * device and so cannot disappear as we iterate. Similarly, we can
3825 * happily treat the predicates as racy, atomic checks as userspace
3826 * cannot claim and pin a new fb without at least acquring the
3827 * struct_mutex and so serialising with us.
3828 */
d3fcc808 3829 for_each_intel_crtc(dev, crtc) {
5dce5b93
CW
3830 if (atomic_read(&crtc->unpin_work_count) == 0)
3831 continue;
3832
3833 if (crtc->unpin_work)
3834 intel_wait_for_vblank(dev, crtc->pipe);
3835
3836 return true;
3837 }
3838
3839 return false;
3840}
3841
d6bbafa1
CW
3842static void page_flip_completed(struct intel_crtc *intel_crtc)
3843{
3844 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
3845 struct intel_unpin_work *work = intel_crtc->unpin_work;
3846
3847 /* ensure that the unpin work is consistent wrt ->pending. */
3848 smp_rmb();
3849 intel_crtc->unpin_work = NULL;
3850
3851 if (work->event)
3852 drm_send_vblank_event(intel_crtc->base.dev,
3853 intel_crtc->pipe,
3854 work->event);
3855
3856 drm_crtc_vblank_put(&intel_crtc->base);
3857
3858 wake_up_all(&dev_priv->pending_flip_queue);
3859 queue_work(dev_priv->wq, &work->work);
3860
3861 trace_i915_flip_complete(intel_crtc->plane,
3862 work->pending_flip_obj);
3863}
3864
5008e874 3865static int intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
e6c3a2a6 3866{
0f91128d 3867 struct drm_device *dev = crtc->dev;
5bb61643 3868 struct drm_i915_private *dev_priv = dev->dev_private;
5008e874 3869 long ret;
e6c3a2a6 3870
2c10d571 3871 WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
5008e874
ML
3872
3873 ret = wait_event_interruptible_timeout(
3874 dev_priv->pending_flip_queue,
3875 !intel_crtc_has_pending_flip(crtc),
3876 60*HZ);
3877
3878 if (ret < 0)
3879 return ret;
3880
3881 if (ret == 0) {
9c787942 3882 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2c10d571 3883
5e2d7afc 3884 spin_lock_irq(&dev->event_lock);
9c787942
CW
3885 if (intel_crtc->unpin_work) {
3886 WARN_ONCE(1, "Removing stuck page flip\n");
3887 page_flip_completed(intel_crtc);
3888 }
5e2d7afc 3889 spin_unlock_irq(&dev->event_lock);
9c787942 3890 }
5bb61643 3891
5008e874 3892 return 0;
e6c3a2a6
CW
3893}
3894
060f02d8
VS
3895static void lpt_disable_iclkip(struct drm_i915_private *dev_priv)
3896{
3897 u32 temp;
3898
3899 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
3900
3901 mutex_lock(&dev_priv->sb_lock);
3902
3903 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
3904 temp |= SBI_SSCCTL_DISABLE;
3905 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
3906
3907 mutex_unlock(&dev_priv->sb_lock);
3908}
3909
e615efe4
ED
3910/* Program iCLKIP clock to the desired frequency */
3911static void lpt_program_iclkip(struct drm_crtc *crtc)
3912{
64b46a06 3913 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
6e3c9717 3914 int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
e615efe4
ED
3915 u32 divsel, phaseinc, auxdiv, phasedir = 0;
3916 u32 temp;
3917
060f02d8 3918 lpt_disable_iclkip(dev_priv);
e615efe4 3919
64b46a06
VS
3920 /* The iCLK virtual clock root frequency is in MHz,
3921 * but the adjusted_mode->crtc_clock in in KHz. To get the
3922 * divisors, it is necessary to divide one by another, so we
3923 * convert the virtual clock precision to KHz here for higher
3924 * precision.
3925 */
3926 for (auxdiv = 0; auxdiv < 2; auxdiv++) {
e615efe4
ED
3927 u32 iclk_virtual_root_freq = 172800 * 1000;
3928 u32 iclk_pi_range = 64;
64b46a06 3929 u32 desired_divisor;
e615efe4 3930
64b46a06
VS
3931 desired_divisor = DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
3932 clock << auxdiv);
3933 divsel = (desired_divisor / iclk_pi_range) - 2;
3934 phaseinc = desired_divisor % iclk_pi_range;
e615efe4 3935
64b46a06
VS
3936 /*
3937 * Near 20MHz is a corner case which is
3938 * out of range for the 7-bit divisor
3939 */
3940 if (divsel <= 0x7f)
3941 break;
e615efe4
ED
3942 }
3943
3944 /* This should not happen with any sane values */
3945 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
3946 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
3947 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
3948 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
3949
3950 DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
12d7ceed 3951 clock,
e615efe4
ED
3952 auxdiv,
3953 divsel,
3954 phasedir,
3955 phaseinc);
3956
060f02d8
VS
3957 mutex_lock(&dev_priv->sb_lock);
3958
e615efe4 3959 /* Program SSCDIVINTPHASE6 */
988d6ee8 3960 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
e615efe4
ED
3961 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
3962 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
3963 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
3964 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
3965 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
3966 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
988d6ee8 3967 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
e615efe4
ED
3968
3969 /* Program SSCAUXDIV */
988d6ee8 3970 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
e615efe4
ED
3971 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
3972 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
988d6ee8 3973 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
e615efe4
ED
3974
3975 /* Enable modulator and associated divider */
988d6ee8 3976 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
e615efe4 3977 temp &= ~SBI_SSCCTL_DISABLE;
988d6ee8 3978 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
e615efe4 3979
060f02d8
VS
3980 mutex_unlock(&dev_priv->sb_lock);
3981
e615efe4
ED
3982 /* Wait for initialization time */
3983 udelay(24);
3984
3985 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
3986}
3987
8802e5b6
VS
3988int lpt_get_iclkip(struct drm_i915_private *dev_priv)
3989{
3990 u32 divsel, phaseinc, auxdiv;
3991 u32 iclk_virtual_root_freq = 172800 * 1000;
3992 u32 iclk_pi_range = 64;
3993 u32 desired_divisor;
3994 u32 temp;
3995
3996 if ((I915_READ(PIXCLK_GATE) & PIXCLK_GATE_UNGATE) == 0)
3997 return 0;
3998
3999 mutex_lock(&dev_priv->sb_lock);
4000
4001 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
4002 if (temp & SBI_SSCCTL_DISABLE) {
4003 mutex_unlock(&dev_priv->sb_lock);
4004 return 0;
4005 }
4006
4007 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
4008 divsel = (temp & SBI_SSCDIVINTPHASE_DIVSEL_MASK) >>
4009 SBI_SSCDIVINTPHASE_DIVSEL_SHIFT;
4010 phaseinc = (temp & SBI_SSCDIVINTPHASE_INCVAL_MASK) >>
4011 SBI_SSCDIVINTPHASE_INCVAL_SHIFT;
4012
4013 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
4014 auxdiv = (temp & SBI_SSCAUXDIV_FINALDIV2SEL_MASK) >>
4015 SBI_SSCAUXDIV_FINALDIV2SEL_SHIFT;
4016
4017 mutex_unlock(&dev_priv->sb_lock);
4018
4019 desired_divisor = (divsel + 2) * iclk_pi_range + phaseinc;
4020
4021 return DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
4022 desired_divisor << auxdiv);
4023}
4024
275f01b2
DV
4025static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
4026 enum pipe pch_transcoder)
4027{
4028 struct drm_device *dev = crtc->base.dev;
4029 struct drm_i915_private *dev_priv = dev->dev_private;
6e3c9717 4030 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
275f01b2
DV
4031
4032 I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
4033 I915_READ(HTOTAL(cpu_transcoder)));
4034 I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
4035 I915_READ(HBLANK(cpu_transcoder)));
4036 I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
4037 I915_READ(HSYNC(cpu_transcoder)));
4038
4039 I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
4040 I915_READ(VTOTAL(cpu_transcoder)));
4041 I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
4042 I915_READ(VBLANK(cpu_transcoder)));
4043 I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
4044 I915_READ(VSYNC(cpu_transcoder)));
4045 I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
4046 I915_READ(VSYNCSHIFT(cpu_transcoder)));
4047}
4048
003632d9 4049static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
1fbc0d78
DV
4050{
4051 struct drm_i915_private *dev_priv = dev->dev_private;
4052 uint32_t temp;
4053
4054 temp = I915_READ(SOUTH_CHICKEN1);
003632d9 4055 if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
1fbc0d78
DV
4056 return;
4057
4058 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
4059 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
4060
003632d9
ACO
4061 temp &= ~FDI_BC_BIFURCATION_SELECT;
4062 if (enable)
4063 temp |= FDI_BC_BIFURCATION_SELECT;
4064
4065 DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
1fbc0d78
DV
4066 I915_WRITE(SOUTH_CHICKEN1, temp);
4067 POSTING_READ(SOUTH_CHICKEN1);
4068}
4069
4070static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
4071{
4072 struct drm_device *dev = intel_crtc->base.dev;
1fbc0d78
DV
4073
4074 switch (intel_crtc->pipe) {
4075 case PIPE_A:
4076 break;
4077 case PIPE_B:
6e3c9717 4078 if (intel_crtc->config->fdi_lanes > 2)
003632d9 4079 cpt_set_fdi_bc_bifurcation(dev, false);
1fbc0d78 4080 else
003632d9 4081 cpt_set_fdi_bc_bifurcation(dev, true);
1fbc0d78
DV
4082
4083 break;
4084 case PIPE_C:
003632d9 4085 cpt_set_fdi_bc_bifurcation(dev, true);
1fbc0d78
DV
4086
4087 break;
4088 default:
4089 BUG();
4090 }
4091}
4092
c48b5305
VS
4093/* Return which DP Port should be selected for Transcoder DP control */
4094static enum port
4095intel_trans_dp_port_sel(struct drm_crtc *crtc)
4096{
4097 struct drm_device *dev = crtc->dev;
4098 struct intel_encoder *encoder;
4099
4100 for_each_encoder_on_crtc(dev, crtc, encoder) {
4101 if (encoder->type == INTEL_OUTPUT_DISPLAYPORT ||
4102 encoder->type == INTEL_OUTPUT_EDP)
4103 return enc_to_dig_port(&encoder->base)->port;
4104 }
4105
4106 return -1;
4107}
4108
f67a559d
JB
4109/*
4110 * Enable PCH resources required for PCH ports:
4111 * - PCH PLLs
4112 * - FDI training & RX/TX
4113 * - update transcoder timings
4114 * - DP transcoding bits
4115 * - transcoder
4116 */
4117static void ironlake_pch_enable(struct drm_crtc *crtc)
0e23b99d
JB
4118{
4119 struct drm_device *dev = crtc->dev;
4120 struct drm_i915_private *dev_priv = dev->dev_private;
4121 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4122 int pipe = intel_crtc->pipe;
f0f59a00 4123 u32 temp;
2c07245f 4124
ab9412ba 4125 assert_pch_transcoder_disabled(dev_priv, pipe);
e7e164db 4126
1fbc0d78
DV
4127 if (IS_IVYBRIDGE(dev))
4128 ivybridge_update_fdi_bc_bifurcation(intel_crtc);
4129
cd986abb
DV
4130 /* Write the TU size bits before fdi link training, so that error
4131 * detection works. */
4132 I915_WRITE(FDI_RX_TUSIZE1(pipe),
4133 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
4134
3860b2ec
VS
4135 /*
4136 * Sometimes spurious CPU pipe underruns happen during FDI
4137 * training, at least with VGA+HDMI cloning. Suppress them.
4138 */
4139 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
4140
c98e9dcf 4141 /* For PCH output, training FDI link */
674cf967 4142 dev_priv->display.fdi_link_train(crtc);
2c07245f 4143
3ad8a208
DV
4144 /* We need to program the right clock selection before writing the pixel
4145 * mutliplier into the DPLL. */
303b81e0 4146 if (HAS_PCH_CPT(dev)) {
ee7b9f93 4147 u32 sel;
4b645f14 4148
c98e9dcf 4149 temp = I915_READ(PCH_DPLL_SEL);
11887397
DV
4150 temp |= TRANS_DPLL_ENABLE(pipe);
4151 sel = TRANS_DPLLB_SEL(pipe);
8106ddbd
ACO
4152 if (intel_crtc->config->shared_dpll ==
4153 intel_get_shared_dpll_by_id(dev_priv, DPLL_ID_PCH_PLL_B))
ee7b9f93
JB
4154 temp |= sel;
4155 else
4156 temp &= ~sel;
c98e9dcf 4157 I915_WRITE(PCH_DPLL_SEL, temp);
c98e9dcf 4158 }
5eddb70b 4159
3ad8a208
DV
4160 /* XXX: pch pll's can be enabled any time before we enable the PCH
4161 * transcoder, and we actually should do this to not upset any PCH
4162 * transcoder that already use the clock when we share it.
4163 *
4164 * Note that enable_shared_dpll tries to do the right thing, but
4165 * get_shared_dpll unconditionally resets the pll - we need that to have
4166 * the right LVDS enable sequence. */
85b3894f 4167 intel_enable_shared_dpll(intel_crtc);
3ad8a208 4168
d9b6cb56
JB
4169 /* set transcoder timing, panel must allow it */
4170 assert_panel_unlocked(dev_priv, pipe);
275f01b2 4171 ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
8db9d77b 4172
303b81e0 4173 intel_fdi_normal_train(crtc);
5e84e1a4 4174
3860b2ec
VS
4175 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4176
c98e9dcf 4177 /* For PCH DP, enable TRANS_DP_CTL */
6e3c9717 4178 if (HAS_PCH_CPT(dev) && intel_crtc->config->has_dp_encoder) {
9c4edaee
VS
4179 const struct drm_display_mode *adjusted_mode =
4180 &intel_crtc->config->base.adjusted_mode;
dfd07d72 4181 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
f0f59a00 4182 i915_reg_t reg = TRANS_DP_CTL(pipe);
5eddb70b
CW
4183 temp = I915_READ(reg);
4184 temp &= ~(TRANS_DP_PORT_SEL_MASK |
220cad3c
EA
4185 TRANS_DP_SYNC_MASK |
4186 TRANS_DP_BPC_MASK);
e3ef4479 4187 temp |= TRANS_DP_OUTPUT_ENABLE;
9325c9f0 4188 temp |= bpc << 9; /* same format but at 11:9 */
c98e9dcf 4189
9c4edaee 4190 if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
5eddb70b 4191 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
9c4edaee 4192 if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
5eddb70b 4193 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
c98e9dcf
JB
4194
4195 switch (intel_trans_dp_port_sel(crtc)) {
c48b5305 4196 case PORT_B:
5eddb70b 4197 temp |= TRANS_DP_PORT_SEL_B;
c98e9dcf 4198 break;
c48b5305 4199 case PORT_C:
5eddb70b 4200 temp |= TRANS_DP_PORT_SEL_C;
c98e9dcf 4201 break;
c48b5305 4202 case PORT_D:
5eddb70b 4203 temp |= TRANS_DP_PORT_SEL_D;
c98e9dcf
JB
4204 break;
4205 default:
e95d41e1 4206 BUG();
32f9d658 4207 }
2c07245f 4208
5eddb70b 4209 I915_WRITE(reg, temp);
6be4a607 4210 }
b52eb4dc 4211
b8a4f404 4212 ironlake_enable_pch_transcoder(dev_priv, pipe);
f67a559d
JB
4213}
4214
1507e5bd
PZ
4215static void lpt_pch_enable(struct drm_crtc *crtc)
4216{
4217 struct drm_device *dev = crtc->dev;
4218 struct drm_i915_private *dev_priv = dev->dev_private;
4219 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6e3c9717 4220 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
1507e5bd 4221
ab9412ba 4222 assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
1507e5bd 4223
8c52b5e8 4224 lpt_program_iclkip(crtc);
1507e5bd 4225
0540e488 4226 /* Set transcoder timing. */
275f01b2 4227 ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
1507e5bd 4228
937bb610 4229 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
f67a559d
JB
4230}
4231
a1520318 4232static void cpt_verify_modeset(struct drm_device *dev, int pipe)
d4270e57
JB
4233{
4234 struct drm_i915_private *dev_priv = dev->dev_private;
f0f59a00 4235 i915_reg_t dslreg = PIPEDSL(pipe);
d4270e57
JB
4236 u32 temp;
4237
4238 temp = I915_READ(dslreg);
4239 udelay(500);
4240 if (wait_for(I915_READ(dslreg) != temp, 5)) {
d4270e57 4241 if (wait_for(I915_READ(dslreg) != temp, 5))
84f44ce7 4242 DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
d4270e57
JB
4243 }
4244}
4245
86adf9d7
ML
4246static int
4247skl_update_scaler(struct intel_crtc_state *crtc_state, bool force_detach,
4248 unsigned scaler_user, int *scaler_id, unsigned int rotation,
4249 int src_w, int src_h, int dst_w, int dst_h)
a1b2278e 4250{
86adf9d7
ML
4251 struct intel_crtc_scaler_state *scaler_state =
4252 &crtc_state->scaler_state;
4253 struct intel_crtc *intel_crtc =
4254 to_intel_crtc(crtc_state->base.crtc);
a1b2278e 4255 int need_scaling;
6156a456
CK
4256
4257 need_scaling = intel_rotation_90_or_270(rotation) ?
4258 (src_h != dst_w || src_w != dst_h):
4259 (src_w != dst_w || src_h != dst_h);
a1b2278e
CK
4260
4261 /*
4262 * if plane is being disabled or scaler is no more required or force detach
4263 * - free scaler binded to this plane/crtc
4264 * - in order to do this, update crtc->scaler_usage
4265 *
4266 * Here scaler state in crtc_state is set free so that
4267 * scaler can be assigned to other user. Actual register
4268 * update to free the scaler is done in plane/panel-fit programming.
4269 * For this purpose crtc/plane_state->scaler_id isn't reset here.
4270 */
86adf9d7 4271 if (force_detach || !need_scaling) {
a1b2278e 4272 if (*scaler_id >= 0) {
86adf9d7 4273 scaler_state->scaler_users &= ~(1 << scaler_user);
a1b2278e
CK
4274 scaler_state->scalers[*scaler_id].in_use = 0;
4275
86adf9d7
ML
4276 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4277 "Staged freeing scaler id %d scaler_users = 0x%x\n",
4278 intel_crtc->pipe, scaler_user, *scaler_id,
a1b2278e
CK
4279 scaler_state->scaler_users);
4280 *scaler_id = -1;
4281 }
4282 return 0;
4283 }
4284
4285 /* range checks */
4286 if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
4287 dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
4288
4289 src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
4290 dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
86adf9d7 4291 DRM_DEBUG_KMS("scaler_user index %u.%u: src %ux%u dst %ux%u "
a1b2278e 4292 "size is out of scaler range\n",
86adf9d7 4293 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h);
a1b2278e
CK
4294 return -EINVAL;
4295 }
4296
86adf9d7
ML
4297 /* mark this plane as a scaler user in crtc_state */
4298 scaler_state->scaler_users |= (1 << scaler_user);
4299 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4300 "staged scaling request for %ux%u->%ux%u scaler_users = 0x%x\n",
4301 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h,
4302 scaler_state->scaler_users);
4303
4304 return 0;
4305}
4306
4307/**
4308 * skl_update_scaler_crtc - Stages update to scaler state for a given crtc.
4309 *
4310 * @state: crtc's scaler state
86adf9d7
ML
4311 *
4312 * Return
4313 * 0 - scaler_usage updated successfully
4314 * error - requested scaling cannot be supported or other error condition
4315 */
e435d6e5 4316int skl_update_scaler_crtc(struct intel_crtc_state *state)
86adf9d7
ML
4317{
4318 struct intel_crtc *intel_crtc = to_intel_crtc(state->base.crtc);
7c5f93b0 4319 const struct drm_display_mode *adjusted_mode = &state->base.adjusted_mode;
86adf9d7
ML
4320
4321 DRM_DEBUG_KMS("Updating scaler for [CRTC:%i] scaler_user index %u.%u\n",
4322 intel_crtc->base.base.id, intel_crtc->pipe, SKL_CRTC_INDEX);
4323
e435d6e5 4324 return skl_update_scaler(state, !state->base.active, SKL_CRTC_INDEX,
fa5a7970 4325 &state->scaler_state.scaler_id, BIT(DRM_ROTATE_0),
86adf9d7 4326 state->pipe_src_w, state->pipe_src_h,
aad941d5 4327 adjusted_mode->crtc_hdisplay, adjusted_mode->crtc_vdisplay);
86adf9d7
ML
4328}
4329
4330/**
4331 * skl_update_scaler_plane - Stages update to scaler state for a given plane.
4332 *
4333 * @state: crtc's scaler state
86adf9d7
ML
4334 * @plane_state: atomic plane state to update
4335 *
4336 * Return
4337 * 0 - scaler_usage updated successfully
4338 * error - requested scaling cannot be supported or other error condition
4339 */
da20eabd
ML
4340static int skl_update_scaler_plane(struct intel_crtc_state *crtc_state,
4341 struct intel_plane_state *plane_state)
86adf9d7
ML
4342{
4343
4344 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
da20eabd
ML
4345 struct intel_plane *intel_plane =
4346 to_intel_plane(plane_state->base.plane);
86adf9d7
ML
4347 struct drm_framebuffer *fb = plane_state->base.fb;
4348 int ret;
4349
4350 bool force_detach = !fb || !plane_state->visible;
4351
4352 DRM_DEBUG_KMS("Updating scaler for [PLANE:%d] scaler_user index %u.%u\n",
4353 intel_plane->base.base.id, intel_crtc->pipe,
4354 drm_plane_index(&intel_plane->base));
4355
4356 ret = skl_update_scaler(crtc_state, force_detach,
4357 drm_plane_index(&intel_plane->base),
4358 &plane_state->scaler_id,
4359 plane_state->base.rotation,
4360 drm_rect_width(&plane_state->src) >> 16,
4361 drm_rect_height(&plane_state->src) >> 16,
4362 drm_rect_width(&plane_state->dst),
4363 drm_rect_height(&plane_state->dst));
4364
4365 if (ret || plane_state->scaler_id < 0)
4366 return ret;
4367
a1b2278e 4368 /* check colorkey */
818ed961 4369 if (plane_state->ckey.flags != I915_SET_COLORKEY_NONE) {
86adf9d7 4370 DRM_DEBUG_KMS("[PLANE:%d] scaling with color key not allowed",
818ed961 4371 intel_plane->base.base.id);
a1b2278e
CK
4372 return -EINVAL;
4373 }
4374
4375 /* Check src format */
86adf9d7
ML
4376 switch (fb->pixel_format) {
4377 case DRM_FORMAT_RGB565:
4378 case DRM_FORMAT_XBGR8888:
4379 case DRM_FORMAT_XRGB8888:
4380 case DRM_FORMAT_ABGR8888:
4381 case DRM_FORMAT_ARGB8888:
4382 case DRM_FORMAT_XRGB2101010:
4383 case DRM_FORMAT_XBGR2101010:
4384 case DRM_FORMAT_YUYV:
4385 case DRM_FORMAT_YVYU:
4386 case DRM_FORMAT_UYVY:
4387 case DRM_FORMAT_VYUY:
4388 break;
4389 default:
4390 DRM_DEBUG_KMS("[PLANE:%d] FB:%d unsupported scaling format 0x%x\n",
4391 intel_plane->base.base.id, fb->base.id, fb->pixel_format);
4392 return -EINVAL;
a1b2278e
CK
4393 }
4394
a1b2278e
CK
4395 return 0;
4396}
4397
e435d6e5
ML
4398static void skylake_scaler_disable(struct intel_crtc *crtc)
4399{
4400 int i;
4401
4402 for (i = 0; i < crtc->num_scalers; i++)
4403 skl_detach_scaler(crtc, i);
4404}
4405
4406static void skylake_pfit_enable(struct intel_crtc *crtc)
bd2e244f
JB
4407{
4408 struct drm_device *dev = crtc->base.dev;
4409 struct drm_i915_private *dev_priv = dev->dev_private;
4410 int pipe = crtc->pipe;
a1b2278e
CK
4411 struct intel_crtc_scaler_state *scaler_state =
4412 &crtc->config->scaler_state;
4413
4414 DRM_DEBUG_KMS("for crtc_state = %p\n", crtc->config);
4415
6e3c9717 4416 if (crtc->config->pch_pfit.enabled) {
a1b2278e
CK
4417 int id;
4418
4419 if (WARN_ON(crtc->config->scaler_state.scaler_id < 0)) {
4420 DRM_ERROR("Requesting pfit without getting a scaler first\n");
4421 return;
4422 }
4423
4424 id = scaler_state->scaler_id;
4425 I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
4426 PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
4427 I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
4428 I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
4429
4430 DRM_DEBUG_KMS("for crtc_state = %p scaler_id = %d\n", crtc->config, id);
bd2e244f
JB
4431 }
4432}
4433
b074cec8
JB
4434static void ironlake_pfit_enable(struct intel_crtc *crtc)
4435{
4436 struct drm_device *dev = crtc->base.dev;
4437 struct drm_i915_private *dev_priv = dev->dev_private;
4438 int pipe = crtc->pipe;
4439
6e3c9717 4440 if (crtc->config->pch_pfit.enabled) {
b074cec8
JB
4441 /* Force use of hard-coded filter coefficients
4442 * as some pre-programmed values are broken,
4443 * e.g. x201.
4444 */
4445 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
4446 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
4447 PF_PIPE_SEL_IVB(pipe));
4448 else
4449 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
6e3c9717
ACO
4450 I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
4451 I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
d4270e57
JB
4452 }
4453}
4454
20bc8673 4455void hsw_enable_ips(struct intel_crtc *crtc)
d77e4531 4456{
cea165c3
VS
4457 struct drm_device *dev = crtc->base.dev;
4458 struct drm_i915_private *dev_priv = dev->dev_private;
d77e4531 4459
6e3c9717 4460 if (!crtc->config->ips_enabled)
d77e4531
PZ
4461 return;
4462
cea165c3
VS
4463 /* We can only enable IPS after we enable a plane and wait for a vblank */
4464 intel_wait_for_vblank(dev, crtc->pipe);
4465
d77e4531 4466 assert_plane_enabled(dev_priv, crtc->plane);
cea165c3 4467 if (IS_BROADWELL(dev)) {
2a114cc1
BW
4468 mutex_lock(&dev_priv->rps.hw_lock);
4469 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
4470 mutex_unlock(&dev_priv->rps.hw_lock);
4471 /* Quoting Art Runyan: "its not safe to expect any particular
4472 * value in IPS_CTL bit 31 after enabling IPS through the
e59150dc
JB
4473 * mailbox." Moreover, the mailbox may return a bogus state,
4474 * so we need to just enable it and continue on.
2a114cc1
BW
4475 */
4476 } else {
4477 I915_WRITE(IPS_CTL, IPS_ENABLE);
4478 /* The bit only becomes 1 in the next vblank, so this wait here
4479 * is essentially intel_wait_for_vblank. If we don't have this
4480 * and don't wait for vblanks until the end of crtc_enable, then
4481 * the HW state readout code will complain that the expected
4482 * IPS_CTL value is not the one we read. */
4483 if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
4484 DRM_ERROR("Timed out waiting for IPS enable\n");
4485 }
d77e4531
PZ
4486}
4487
20bc8673 4488void hsw_disable_ips(struct intel_crtc *crtc)
d77e4531
PZ
4489{
4490 struct drm_device *dev = crtc->base.dev;
4491 struct drm_i915_private *dev_priv = dev->dev_private;
4492
6e3c9717 4493 if (!crtc->config->ips_enabled)
d77e4531
PZ
4494 return;
4495
4496 assert_plane_enabled(dev_priv, crtc->plane);
23d0b130 4497 if (IS_BROADWELL(dev)) {
2a114cc1
BW
4498 mutex_lock(&dev_priv->rps.hw_lock);
4499 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
4500 mutex_unlock(&dev_priv->rps.hw_lock);
23d0b130
BW
4501 /* wait for pcode to finish disabling IPS, which may take up to 42ms */
4502 if (wait_for((I915_READ(IPS_CTL) & IPS_ENABLE) == 0, 42))
4503 DRM_ERROR("Timed out waiting for IPS disable\n");
e59150dc 4504 } else {
2a114cc1 4505 I915_WRITE(IPS_CTL, 0);
e59150dc
JB
4506 POSTING_READ(IPS_CTL);
4507 }
d77e4531
PZ
4508
4509 /* We need to wait for a vblank before we can disable the plane. */
4510 intel_wait_for_vblank(dev, crtc->pipe);
4511}
4512
4513/** Loads the palette/gamma unit for the CRTC with the prepared values */
4514static void intel_crtc_load_lut(struct drm_crtc *crtc)
4515{
4516 struct drm_device *dev = crtc->dev;
4517 struct drm_i915_private *dev_priv = dev->dev_private;
4518 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4519 enum pipe pipe = intel_crtc->pipe;
d77e4531
PZ
4520 int i;
4521 bool reenable_ips = false;
4522
4523 /* The clocks have to be on to load the palette. */
53d9f4e9 4524 if (!crtc->state->active)
d77e4531
PZ
4525 return;
4526
50360403 4527 if (HAS_GMCH_DISPLAY(dev_priv->dev)) {
a65347ba 4528 if (intel_crtc->config->has_dsi_encoder)
d77e4531
PZ
4529 assert_dsi_pll_enabled(dev_priv);
4530 else
4531 assert_pll_enabled(dev_priv, pipe);
4532 }
4533
d77e4531
PZ
4534 /* Workaround : Do not read or write the pipe palette/gamma data while
4535 * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
4536 */
6e3c9717 4537 if (IS_HASWELL(dev) && intel_crtc->config->ips_enabled &&
d77e4531
PZ
4538 ((I915_READ(GAMMA_MODE(pipe)) & GAMMA_MODE_MODE_MASK) ==
4539 GAMMA_MODE_MODE_SPLIT)) {
4540 hsw_disable_ips(intel_crtc);
4541 reenable_ips = true;
4542 }
4543
4544 for (i = 0; i < 256; i++) {
f0f59a00 4545 i915_reg_t palreg;
f65a9c5b
VS
4546
4547 if (HAS_GMCH_DISPLAY(dev))
4548 palreg = PALETTE(pipe, i);
4549 else
4550 palreg = LGC_PALETTE(pipe, i);
4551
4552 I915_WRITE(palreg,
d77e4531
PZ
4553 (intel_crtc->lut_r[i] << 16) |
4554 (intel_crtc->lut_g[i] << 8) |
4555 intel_crtc->lut_b[i]);
4556 }
4557
4558 if (reenable_ips)
4559 hsw_enable_ips(intel_crtc);
4560}
4561
7cac945f 4562static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
d3eedb1a 4563{
7cac945f 4564 if (intel_crtc->overlay) {
d3eedb1a
VS
4565 struct drm_device *dev = intel_crtc->base.dev;
4566 struct drm_i915_private *dev_priv = dev->dev_private;
4567
4568 mutex_lock(&dev->struct_mutex);
4569 dev_priv->mm.interruptible = false;
4570 (void) intel_overlay_switch_off(intel_crtc->overlay);
4571 dev_priv->mm.interruptible = true;
4572 mutex_unlock(&dev->struct_mutex);
4573 }
4574
4575 /* Let userspace switch the overlay on again. In most cases userspace
4576 * has to recompute where to put it anyway.
4577 */
4578}
4579
87d4300a
ML
4580/**
4581 * intel_post_enable_primary - Perform operations after enabling primary plane
4582 * @crtc: the CRTC whose primary plane was just enabled
4583 *
4584 * Performs potentially sleeping operations that must be done after the primary
4585 * plane is enabled, such as updating FBC and IPS. Note that this may be
4586 * called due to an explicit primary plane update, or due to an implicit
4587 * re-enable that is caused when a sprite plane is updated to no longer
4588 * completely hide the primary plane.
4589 */
4590static void
4591intel_post_enable_primary(struct drm_crtc *crtc)
a5c4d7bc
VS
4592{
4593 struct drm_device *dev = crtc->dev;
87d4300a 4594 struct drm_i915_private *dev_priv = dev->dev_private;
a5c4d7bc
VS
4595 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4596 int pipe = intel_crtc->pipe;
a5c4d7bc 4597
87d4300a
ML
4598 /*
4599 * FIXME IPS should be fine as long as one plane is
4600 * enabled, but in practice it seems to have problems
4601 * when going from primary only to sprite only and vice
4602 * versa.
4603 */
a5c4d7bc
VS
4604 hsw_enable_ips(intel_crtc);
4605
f99d7069 4606 /*
87d4300a
ML
4607 * Gen2 reports pipe underruns whenever all planes are disabled.
4608 * So don't enable underrun reporting before at least some planes
4609 * are enabled.
4610 * FIXME: Need to fix the logic to work when we turn off all planes
4611 * but leave the pipe running.
f99d7069 4612 */
87d4300a
ML
4613 if (IS_GEN2(dev))
4614 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4615
aca7b684
VS
4616 /* Underruns don't always raise interrupts, so check manually. */
4617 intel_check_cpu_fifo_underruns(dev_priv);
4618 intel_check_pch_fifo_underruns(dev_priv);
a5c4d7bc
VS
4619}
4620
2622a081 4621/* FIXME move all this to pre_plane_update() with proper state tracking */
87d4300a
ML
4622static void
4623intel_pre_disable_primary(struct drm_crtc *crtc)
a5c4d7bc
VS
4624{
4625 struct drm_device *dev = crtc->dev;
4626 struct drm_i915_private *dev_priv = dev->dev_private;
4627 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4628 int pipe = intel_crtc->pipe;
a5c4d7bc 4629
87d4300a
ML
4630 /*
4631 * Gen2 reports pipe underruns whenever all planes are disabled.
4632 * So diasble underrun reporting before all the planes get disabled.
4633 * FIXME: Need to fix the logic to work when we turn off all planes
4634 * but leave the pipe running.
4635 */
4636 if (IS_GEN2(dev))
4637 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
a5c4d7bc 4638
2622a081
VS
4639 /*
4640 * FIXME IPS should be fine as long as one plane is
4641 * enabled, but in practice it seems to have problems
4642 * when going from primary only to sprite only and vice
4643 * versa.
4644 */
4645 hsw_disable_ips(intel_crtc);
4646}
4647
4648/* FIXME get rid of this and use pre_plane_update */
4649static void
4650intel_pre_disable_primary_noatomic(struct drm_crtc *crtc)
4651{
4652 struct drm_device *dev = crtc->dev;
4653 struct drm_i915_private *dev_priv = dev->dev_private;
4654 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4655 int pipe = intel_crtc->pipe;
4656
4657 intel_pre_disable_primary(crtc);
4658
87d4300a
ML
4659 /*
4660 * Vblank time updates from the shadow to live plane control register
4661 * are blocked if the memory self-refresh mode is active at that
4662 * moment. So to make sure the plane gets truly disabled, disable
4663 * first the self-refresh mode. The self-refresh enable bit in turn
4664 * will be checked/applied by the HW only at the next frame start
4665 * event which is after the vblank start event, so we need to have a
4666 * wait-for-vblank between disabling the plane and the pipe.
4667 */
262cd2e1 4668 if (HAS_GMCH_DISPLAY(dev)) {
87d4300a 4669 intel_set_memory_cxsr(dev_priv, false);
262cd2e1
VS
4670 dev_priv->wm.vlv.cxsr = false;
4671 intel_wait_for_vblank(dev, pipe);
4672 }
87d4300a
ML
4673}
4674
cd202f69 4675static void intel_post_plane_update(struct intel_crtc_state *old_crtc_state)
ac21b225 4676{
cd202f69
ML
4677 struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
4678 struct drm_atomic_state *old_state = old_crtc_state->base.state;
92826fcd
ML
4679 struct intel_crtc_state *pipe_config =
4680 to_intel_crtc_state(crtc->base.state);
ac21b225 4681 struct drm_device *dev = crtc->base.dev;
cd202f69
ML
4682 struct drm_plane *primary = crtc->base.primary;
4683 struct drm_plane_state *old_pri_state =
4684 drm_atomic_get_existing_plane_state(old_state, primary);
ac21b225 4685
cd202f69 4686 intel_frontbuffer_flip(dev, pipe_config->fb_bits);
ac21b225 4687
ab1d3a0e 4688 crtc->wm.cxsr_allowed = true;
852eb00d 4689
caed361d 4690 if (pipe_config->update_wm_post && pipe_config->base.active)
f015c551
VS
4691 intel_update_watermarks(&crtc->base);
4692
cd202f69
ML
4693 if (old_pri_state) {
4694 struct intel_plane_state *primary_state =
4695 to_intel_plane_state(primary->state);
4696 struct intel_plane_state *old_primary_state =
4697 to_intel_plane_state(old_pri_state);
4698
31ae71fc
ML
4699 intel_fbc_post_update(crtc);
4700
cd202f69
ML
4701 if (primary_state->visible &&
4702 (needs_modeset(&pipe_config->base) ||
4703 !old_primary_state->visible))
4704 intel_post_enable_primary(&crtc->base);
4705 }
ac21b225
ML
4706}
4707
5c74cd73 4708static void intel_pre_plane_update(struct intel_crtc_state *old_crtc_state)
ac21b225 4709{
5c74cd73 4710 struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
ac21b225 4711 struct drm_device *dev = crtc->base.dev;
eddfcbcd 4712 struct drm_i915_private *dev_priv = dev->dev_private;
ab1d3a0e
ML
4713 struct intel_crtc_state *pipe_config =
4714 to_intel_crtc_state(crtc->base.state);
5c74cd73
ML
4715 struct drm_atomic_state *old_state = old_crtc_state->base.state;
4716 struct drm_plane *primary = crtc->base.primary;
4717 struct drm_plane_state *old_pri_state =
4718 drm_atomic_get_existing_plane_state(old_state, primary);
4719 bool modeset = needs_modeset(&pipe_config->base);
ac21b225 4720
5c74cd73
ML
4721 if (old_pri_state) {
4722 struct intel_plane_state *primary_state =
4723 to_intel_plane_state(primary->state);
4724 struct intel_plane_state *old_primary_state =
4725 to_intel_plane_state(old_pri_state);
4726
31ae71fc
ML
4727 intel_fbc_pre_update(crtc);
4728
5c74cd73
ML
4729 if (old_primary_state->visible &&
4730 (modeset || !primary_state->visible))
4731 intel_pre_disable_primary(&crtc->base);
4732 }
852eb00d 4733
ab1d3a0e 4734 if (pipe_config->disable_cxsr) {
852eb00d 4735 crtc->wm.cxsr_allowed = false;
2dfd178d 4736
2622a081
VS
4737 /*
4738 * Vblank time updates from the shadow to live plane control register
4739 * are blocked if the memory self-refresh mode is active at that
4740 * moment. So to make sure the plane gets truly disabled, disable
4741 * first the self-refresh mode. The self-refresh enable bit in turn
4742 * will be checked/applied by the HW only at the next frame start
4743 * event which is after the vblank start event, so we need to have a
4744 * wait-for-vblank between disabling the plane and the pipe.
4745 */
4746 if (old_crtc_state->base.active) {
2dfd178d 4747 intel_set_memory_cxsr(dev_priv, false);
2622a081
VS
4748 dev_priv->wm.vlv.cxsr = false;
4749 intel_wait_for_vblank(dev, crtc->pipe);
4750 }
852eb00d 4751 }
92826fcd 4752
ed4a6a7c
MR
4753 /*
4754 * IVB workaround: must disable low power watermarks for at least
4755 * one frame before enabling scaling. LP watermarks can be re-enabled
4756 * when scaling is disabled.
4757 *
4758 * WaCxSRDisabledForSpriteScaling:ivb
4759 */
4760 if (pipe_config->disable_lp_wm) {
4761 ilk_disable_lp_wm(dev);
4762 intel_wait_for_vblank(dev, crtc->pipe);
4763 }
4764
4765 /*
4766 * If we're doing a modeset, we're done. No need to do any pre-vblank
4767 * watermark programming here.
4768 */
4769 if (needs_modeset(&pipe_config->base))
4770 return;
4771
4772 /*
4773 * For platforms that support atomic watermarks, program the
4774 * 'intermediate' watermarks immediately. On pre-gen9 platforms, these
4775 * will be the intermediate values that are safe for both pre- and
4776 * post- vblank; when vblank happens, the 'active' values will be set
4777 * to the final 'target' values and we'll do this again to get the
4778 * optimal watermarks. For gen9+ platforms, the values we program here
4779 * will be the final target values which will get automatically latched
4780 * at vblank time; no further programming will be necessary.
4781 *
4782 * If a platform hasn't been transitioned to atomic watermarks yet,
4783 * we'll continue to update watermarks the old way, if flags tell
4784 * us to.
4785 */
4786 if (dev_priv->display.initial_watermarks != NULL)
4787 dev_priv->display.initial_watermarks(pipe_config);
caed361d 4788 else if (pipe_config->update_wm_pre)
92826fcd 4789 intel_update_watermarks(&crtc->base);
ac21b225
ML
4790}
4791
d032ffa0 4792static void intel_crtc_disable_planes(struct drm_crtc *crtc, unsigned plane_mask)
87d4300a
ML
4793{
4794 struct drm_device *dev = crtc->dev;
4795 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
d032ffa0 4796 struct drm_plane *p;
87d4300a
ML
4797 int pipe = intel_crtc->pipe;
4798
7cac945f 4799 intel_crtc_dpms_overlay_disable(intel_crtc);
27321ae8 4800
d032ffa0
ML
4801 drm_for_each_plane_mask(p, dev, plane_mask)
4802 to_intel_plane(p)->disable_plane(p, crtc);
f98551ae 4803
f99d7069
DV
4804 /*
4805 * FIXME: Once we grow proper nuclear flip support out of this we need
4806 * to compute the mask of flip planes precisely. For the time being
4807 * consider this a flip to a NULL plane.
4808 */
4809 intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
a5c4d7bc
VS
4810}
4811
f67a559d
JB
4812static void ironlake_crtc_enable(struct drm_crtc *crtc)
4813{
4814 struct drm_device *dev = crtc->dev;
4815 struct drm_i915_private *dev_priv = dev->dev_private;
4816 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 4817 struct intel_encoder *encoder;
f67a559d 4818 int pipe = intel_crtc->pipe;
f67a559d 4819
53d9f4e9 4820 if (WARN_ON(intel_crtc->active))
f67a559d
JB
4821 return;
4822
81b088ca
VS
4823 if (intel_crtc->config->has_pch_encoder)
4824 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
4825
6e3c9717 4826 if (intel_crtc->config->has_pch_encoder)
b14b1055
DV
4827 intel_prepare_shared_dpll(intel_crtc);
4828
6e3c9717 4829 if (intel_crtc->config->has_dp_encoder)
fe3cd48d 4830 intel_dp_set_m_n(intel_crtc, M1_N1);
29407aab
DV
4831
4832 intel_set_pipe_timings(intel_crtc);
bc58be60 4833 intel_set_pipe_src_size(intel_crtc);
29407aab 4834
6e3c9717 4835 if (intel_crtc->config->has_pch_encoder) {
29407aab 4836 intel_cpu_transcoder_set_m_n(intel_crtc,
6e3c9717 4837 &intel_crtc->config->fdi_m_n, NULL);
29407aab
DV
4838 }
4839
4840 ironlake_set_pipeconf(crtc);
4841
f67a559d 4842 intel_crtc->active = true;
8664281b 4843
a72e4c9f 4844 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
8664281b 4845
f6736a1a 4846 for_each_encoder_on_crtc(dev, crtc, encoder)
952735ee
DV
4847 if (encoder->pre_enable)
4848 encoder->pre_enable(encoder);
f67a559d 4849
6e3c9717 4850 if (intel_crtc->config->has_pch_encoder) {
fff367c7
DV
4851 /* Note: FDI PLL enabling _must_ be done before we enable the
4852 * cpu pipes, hence this is separate from all the other fdi/pch
4853 * enabling. */
88cefb6c 4854 ironlake_fdi_pll_enable(intel_crtc);
46b6f814
DV
4855 } else {
4856 assert_fdi_tx_disabled(dev_priv, pipe);
4857 assert_fdi_rx_disabled(dev_priv, pipe);
4858 }
f67a559d 4859
b074cec8 4860 ironlake_pfit_enable(intel_crtc);
f67a559d 4861
9c54c0dd
JB
4862 /*
4863 * On ILK+ LUT must be loaded before the pipe is running but with
4864 * clocks enabled
4865 */
4866 intel_crtc_load_lut(crtc);
4867
1d5bf5d9
ID
4868 if (dev_priv->display.initial_watermarks != NULL)
4869 dev_priv->display.initial_watermarks(intel_crtc->config);
e1fdc473 4870 intel_enable_pipe(intel_crtc);
f67a559d 4871
6e3c9717 4872 if (intel_crtc->config->has_pch_encoder)
f67a559d 4873 ironlake_pch_enable(crtc);
c98e9dcf 4874
f9b61ff6
DV
4875 assert_vblank_disabled(crtc);
4876 drm_crtc_vblank_on(crtc);
4877
fa5c73b1
DV
4878 for_each_encoder_on_crtc(dev, crtc, encoder)
4879 encoder->enable(encoder);
61b77ddd
DV
4880
4881 if (HAS_PCH_CPT(dev))
a1520318 4882 cpt_verify_modeset(dev, intel_crtc->pipe);
37ca8d4c
VS
4883
4884 /* Must wait for vblank to avoid spurious PCH FIFO underruns */
4885 if (intel_crtc->config->has_pch_encoder)
4886 intel_wait_for_vblank(dev, pipe);
4887 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
6be4a607
JB
4888}
4889
42db64ef
PZ
4890/* IPS only exists on ULT machines and is tied to pipe A. */
4891static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
4892{
f5adf94e 4893 return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
42db64ef
PZ
4894}
4895
4f771f10
PZ
4896static void haswell_crtc_enable(struct drm_crtc *crtc)
4897{
4898 struct drm_device *dev = crtc->dev;
4899 struct drm_i915_private *dev_priv = dev->dev_private;
4900 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4901 struct intel_encoder *encoder;
99d736a2 4902 int pipe = intel_crtc->pipe, hsw_workaround_pipe;
4d1de975 4903 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
99d736a2
ML
4904 struct intel_crtc_state *pipe_config =
4905 to_intel_crtc_state(crtc->state);
4f771f10 4906
53d9f4e9 4907 if (WARN_ON(intel_crtc->active))
4f771f10
PZ
4908 return;
4909
81b088ca
VS
4910 if (intel_crtc->config->has_pch_encoder)
4911 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
4912 false);
4913
8106ddbd 4914 if (intel_crtc->config->shared_dpll)
df8ad70c
DV
4915 intel_enable_shared_dpll(intel_crtc);
4916
6e3c9717 4917 if (intel_crtc->config->has_dp_encoder)
fe3cd48d 4918 intel_dp_set_m_n(intel_crtc, M1_N1);
229fca97 4919
4d1de975
JN
4920 if (!intel_crtc->config->has_dsi_encoder)
4921 intel_set_pipe_timings(intel_crtc);
4922
bc58be60 4923 intel_set_pipe_src_size(intel_crtc);
229fca97 4924
4d1de975
JN
4925 if (cpu_transcoder != TRANSCODER_EDP &&
4926 !transcoder_is_dsi(cpu_transcoder)) {
4927 I915_WRITE(PIPE_MULT(cpu_transcoder),
6e3c9717 4928 intel_crtc->config->pixel_multiplier - 1);
ebb69c95
CT
4929 }
4930
6e3c9717 4931 if (intel_crtc->config->has_pch_encoder) {
229fca97 4932 intel_cpu_transcoder_set_m_n(intel_crtc,
6e3c9717 4933 &intel_crtc->config->fdi_m_n, NULL);
229fca97
DV
4934 }
4935
4d1de975
JN
4936 if (!intel_crtc->config->has_dsi_encoder)
4937 haswell_set_pipeconf(crtc);
4938
391bf048
JN
4939 haswell_set_pipe_gamma(crtc);
4940 haswell_set_pipemisc(crtc);
229fca97
DV
4941
4942 intel_set_pipe_csc(crtc);
4943
4f771f10 4944 intel_crtc->active = true;
8664281b 4945
6b698516
DV
4946 if (intel_crtc->config->has_pch_encoder)
4947 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
4948 else
4949 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4950
7d4aefd0 4951 for_each_encoder_on_crtc(dev, crtc, encoder) {
4f771f10
PZ
4952 if (encoder->pre_enable)
4953 encoder->pre_enable(encoder);
7d4aefd0 4954 }
4f771f10 4955
d2d65408 4956 if (intel_crtc->config->has_pch_encoder)
4fe9467d 4957 dev_priv->display.fdi_link_train(crtc);
4fe9467d 4958
a65347ba 4959 if (!intel_crtc->config->has_dsi_encoder)
7d4aefd0 4960 intel_ddi_enable_pipe_clock(intel_crtc);
4f771f10 4961
1c132b44 4962 if (INTEL_INFO(dev)->gen >= 9)
e435d6e5 4963 skylake_pfit_enable(intel_crtc);
ff6d9f55 4964 else
1c132b44 4965 ironlake_pfit_enable(intel_crtc);
4f771f10
PZ
4966
4967 /*
4968 * On ILK+ LUT must be loaded before the pipe is running but with
4969 * clocks enabled
4970 */
4971 intel_crtc_load_lut(crtc);
4972
1f544388 4973 intel_ddi_set_pipe_settings(crtc);
a65347ba 4974 if (!intel_crtc->config->has_dsi_encoder)
7d4aefd0 4975 intel_ddi_enable_transcoder_func(crtc);
4f771f10 4976
1d5bf5d9
ID
4977 if (dev_priv->display.initial_watermarks != NULL)
4978 dev_priv->display.initial_watermarks(pipe_config);
4979 else
4980 intel_update_watermarks(crtc);
4d1de975
JN
4981
4982 /* XXX: Do the pipe assertions at the right place for BXT DSI. */
4983 if (!intel_crtc->config->has_dsi_encoder)
4984 intel_enable_pipe(intel_crtc);
42db64ef 4985
6e3c9717 4986 if (intel_crtc->config->has_pch_encoder)
1507e5bd 4987 lpt_pch_enable(crtc);
4f771f10 4988
a65347ba 4989 if (intel_crtc->config->dp_encoder_is_mst)
0e32b39c
DA
4990 intel_ddi_set_vc_payload_alloc(crtc, true);
4991
f9b61ff6
DV
4992 assert_vblank_disabled(crtc);
4993 drm_crtc_vblank_on(crtc);
4994
8807e55b 4995 for_each_encoder_on_crtc(dev, crtc, encoder) {
4f771f10 4996 encoder->enable(encoder);
8807e55b
JN
4997 intel_opregion_notify_encoder(encoder, true);
4998 }
4f771f10 4999
6b698516
DV
5000 if (intel_crtc->config->has_pch_encoder) {
5001 intel_wait_for_vblank(dev, pipe);
5002 intel_wait_for_vblank(dev, pipe);
5003 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
d2d65408
VS
5004 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5005 true);
6b698516 5006 }
d2d65408 5007
e4916946
PZ
5008 /* If we change the relative order between pipe/planes enabling, we need
5009 * to change the workaround. */
99d736a2
ML
5010 hsw_workaround_pipe = pipe_config->hsw_workaround_pipe;
5011 if (IS_HASWELL(dev) && hsw_workaround_pipe != INVALID_PIPE) {
5012 intel_wait_for_vblank(dev, hsw_workaround_pipe);
5013 intel_wait_for_vblank(dev, hsw_workaround_pipe);
5014 }
4f771f10
PZ
5015}
5016
bfd16b2a 5017static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force)
3f8dce3a
DV
5018{
5019 struct drm_device *dev = crtc->base.dev;
5020 struct drm_i915_private *dev_priv = dev->dev_private;
5021 int pipe = crtc->pipe;
5022
5023 /* To avoid upsetting the power well on haswell only disable the pfit if
5024 * it's in use. The hw state code will make sure we get this right. */
bfd16b2a 5025 if (force || crtc->config->pch_pfit.enabled) {
3f8dce3a
DV
5026 I915_WRITE(PF_CTL(pipe), 0);
5027 I915_WRITE(PF_WIN_POS(pipe), 0);
5028 I915_WRITE(PF_WIN_SZ(pipe), 0);
5029 }
5030}
5031
6be4a607
JB
5032static void ironlake_crtc_disable(struct drm_crtc *crtc)
5033{
5034 struct drm_device *dev = crtc->dev;
5035 struct drm_i915_private *dev_priv = dev->dev_private;
5036 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 5037 struct intel_encoder *encoder;
6be4a607 5038 int pipe = intel_crtc->pipe;
b52eb4dc 5039
37ca8d4c
VS
5040 if (intel_crtc->config->has_pch_encoder)
5041 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
5042
ea9d758d
DV
5043 for_each_encoder_on_crtc(dev, crtc, encoder)
5044 encoder->disable(encoder);
5045
f9b61ff6
DV
5046 drm_crtc_vblank_off(crtc);
5047 assert_vblank_disabled(crtc);
5048
3860b2ec
VS
5049 /*
5050 * Sometimes spurious CPU pipe underruns happen when the
5051 * pipe is already disabled, but FDI RX/TX is still enabled.
5052 * Happens at least with VGA+HDMI cloning. Suppress them.
5053 */
5054 if (intel_crtc->config->has_pch_encoder)
5055 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
5056
575f7ab7 5057 intel_disable_pipe(intel_crtc);
32f9d658 5058
bfd16b2a 5059 ironlake_pfit_disable(intel_crtc, false);
2c07245f 5060
3860b2ec 5061 if (intel_crtc->config->has_pch_encoder) {
5a74f70a 5062 ironlake_fdi_disable(crtc);
3860b2ec
VS
5063 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
5064 }
5a74f70a 5065
bf49ec8c
DV
5066 for_each_encoder_on_crtc(dev, crtc, encoder)
5067 if (encoder->post_disable)
5068 encoder->post_disable(encoder);
2c07245f 5069
6e3c9717 5070 if (intel_crtc->config->has_pch_encoder) {
d925c59a 5071 ironlake_disable_pch_transcoder(dev_priv, pipe);
6be4a607 5072
d925c59a 5073 if (HAS_PCH_CPT(dev)) {
f0f59a00
VS
5074 i915_reg_t reg;
5075 u32 temp;
5076
d925c59a
DV
5077 /* disable TRANS_DP_CTL */
5078 reg = TRANS_DP_CTL(pipe);
5079 temp = I915_READ(reg);
5080 temp &= ~(TRANS_DP_OUTPUT_ENABLE |
5081 TRANS_DP_PORT_SEL_MASK);
5082 temp |= TRANS_DP_PORT_SEL_NONE;
5083 I915_WRITE(reg, temp);
5084
5085 /* disable DPLL_SEL */
5086 temp = I915_READ(PCH_DPLL_SEL);
11887397 5087 temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
d925c59a 5088 I915_WRITE(PCH_DPLL_SEL, temp);
9db4a9c7 5089 }
e3421a18 5090
d925c59a
DV
5091 ironlake_fdi_pll_disable(intel_crtc);
5092 }
81b088ca
VS
5093
5094 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
6be4a607 5095}
1b3c7a47 5096
4f771f10 5097static void haswell_crtc_disable(struct drm_crtc *crtc)
ee7b9f93 5098{
4f771f10
PZ
5099 struct drm_device *dev = crtc->dev;
5100 struct drm_i915_private *dev_priv = dev->dev_private;
ee7b9f93 5101 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4f771f10 5102 struct intel_encoder *encoder;
6e3c9717 5103 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
ee7b9f93 5104
d2d65408
VS
5105 if (intel_crtc->config->has_pch_encoder)
5106 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5107 false);
5108
8807e55b
JN
5109 for_each_encoder_on_crtc(dev, crtc, encoder) {
5110 intel_opregion_notify_encoder(encoder, false);
4f771f10 5111 encoder->disable(encoder);
8807e55b 5112 }
4f771f10 5113
f9b61ff6
DV
5114 drm_crtc_vblank_off(crtc);
5115 assert_vblank_disabled(crtc);
5116
4d1de975
JN
5117 /* XXX: Do the pipe assertions at the right place for BXT DSI. */
5118 if (!intel_crtc->config->has_dsi_encoder)
5119 intel_disable_pipe(intel_crtc);
4f771f10 5120
6e3c9717 5121 if (intel_crtc->config->dp_encoder_is_mst)
a4bf214f
VS
5122 intel_ddi_set_vc_payload_alloc(crtc, false);
5123
a65347ba 5124 if (!intel_crtc->config->has_dsi_encoder)
7d4aefd0 5125 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
4f771f10 5126
1c132b44 5127 if (INTEL_INFO(dev)->gen >= 9)
e435d6e5 5128 skylake_scaler_disable(intel_crtc);
ff6d9f55 5129 else
bfd16b2a 5130 ironlake_pfit_disable(intel_crtc, false);
4f771f10 5131
a65347ba 5132 if (!intel_crtc->config->has_dsi_encoder)
7d4aefd0 5133 intel_ddi_disable_pipe_clock(intel_crtc);
4f771f10 5134
97b040aa
ID
5135 for_each_encoder_on_crtc(dev, crtc, encoder)
5136 if (encoder->post_disable)
5137 encoder->post_disable(encoder);
81b088ca 5138
92966a37
VS
5139 if (intel_crtc->config->has_pch_encoder) {
5140 lpt_disable_pch_transcoder(dev_priv);
503a74e9 5141 lpt_disable_iclkip(dev_priv);
92966a37
VS
5142 intel_ddi_fdi_disable(crtc);
5143
81b088ca
VS
5144 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5145 true);
92966a37 5146 }
4f771f10
PZ
5147}
5148
2dd24552
JB
5149static void i9xx_pfit_enable(struct intel_crtc *crtc)
5150{
5151 struct drm_device *dev = crtc->base.dev;
5152 struct drm_i915_private *dev_priv = dev->dev_private;
6e3c9717 5153 struct intel_crtc_state *pipe_config = crtc->config;
2dd24552 5154
681a8504 5155 if (!pipe_config->gmch_pfit.control)
2dd24552
JB
5156 return;
5157
2dd24552 5158 /*
c0b03411
DV
5159 * The panel fitter should only be adjusted whilst the pipe is disabled,
5160 * according to register description and PRM.
2dd24552 5161 */
c0b03411
DV
5162 WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
5163 assert_pipe_disabled(dev_priv, crtc->pipe);
2dd24552 5164
b074cec8
JB
5165 I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
5166 I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
5a80c45c
DV
5167
5168 /* Border color in case we don't scale up to the full screen. Black by
5169 * default, change to something else for debugging. */
5170 I915_WRITE(BCLRPAT(crtc->pipe), 0);
2dd24552
JB
5171}
5172
d05410f9
DA
5173static enum intel_display_power_domain port_to_power_domain(enum port port)
5174{
5175 switch (port) {
5176 case PORT_A:
6331a704 5177 return POWER_DOMAIN_PORT_DDI_A_LANES;
d05410f9 5178 case PORT_B:
6331a704 5179 return POWER_DOMAIN_PORT_DDI_B_LANES;
d05410f9 5180 case PORT_C:
6331a704 5181 return POWER_DOMAIN_PORT_DDI_C_LANES;
d05410f9 5182 case PORT_D:
6331a704 5183 return POWER_DOMAIN_PORT_DDI_D_LANES;
d8e19f99 5184 case PORT_E:
6331a704 5185 return POWER_DOMAIN_PORT_DDI_E_LANES;
d05410f9 5186 default:
b9fec167 5187 MISSING_CASE(port);
d05410f9
DA
5188 return POWER_DOMAIN_PORT_OTHER;
5189 }
5190}
5191
25f78f58
VS
5192static enum intel_display_power_domain port_to_aux_power_domain(enum port port)
5193{
5194 switch (port) {
5195 case PORT_A:
5196 return POWER_DOMAIN_AUX_A;
5197 case PORT_B:
5198 return POWER_DOMAIN_AUX_B;
5199 case PORT_C:
5200 return POWER_DOMAIN_AUX_C;
5201 case PORT_D:
5202 return POWER_DOMAIN_AUX_D;
5203 case PORT_E:
5204 /* FIXME: Check VBT for actual wiring of PORT E */
5205 return POWER_DOMAIN_AUX_D;
5206 default:
b9fec167 5207 MISSING_CASE(port);
25f78f58
VS
5208 return POWER_DOMAIN_AUX_A;
5209 }
5210}
5211
319be8ae
ID
5212enum intel_display_power_domain
5213intel_display_port_power_domain(struct intel_encoder *intel_encoder)
5214{
5215 struct drm_device *dev = intel_encoder->base.dev;
5216 struct intel_digital_port *intel_dig_port;
5217
5218 switch (intel_encoder->type) {
5219 case INTEL_OUTPUT_UNKNOWN:
5220 /* Only DDI platforms should ever use this output type */
5221 WARN_ON_ONCE(!HAS_DDI(dev));
5222 case INTEL_OUTPUT_DISPLAYPORT:
5223 case INTEL_OUTPUT_HDMI:
5224 case INTEL_OUTPUT_EDP:
5225 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
d05410f9 5226 return port_to_power_domain(intel_dig_port->port);
0e32b39c
DA
5227 case INTEL_OUTPUT_DP_MST:
5228 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
5229 return port_to_power_domain(intel_dig_port->port);
319be8ae
ID
5230 case INTEL_OUTPUT_ANALOG:
5231 return POWER_DOMAIN_PORT_CRT;
5232 case INTEL_OUTPUT_DSI:
5233 return POWER_DOMAIN_PORT_DSI;
5234 default:
5235 return POWER_DOMAIN_PORT_OTHER;
5236 }
5237}
5238
25f78f58
VS
5239enum intel_display_power_domain
5240intel_display_port_aux_power_domain(struct intel_encoder *intel_encoder)
5241{
5242 struct drm_device *dev = intel_encoder->base.dev;
5243 struct intel_digital_port *intel_dig_port;
5244
5245 switch (intel_encoder->type) {
5246 case INTEL_OUTPUT_UNKNOWN:
651174a4
ID
5247 case INTEL_OUTPUT_HDMI:
5248 /*
5249 * Only DDI platforms should ever use these output types.
5250 * We can get here after the HDMI detect code has already set
5251 * the type of the shared encoder. Since we can't be sure
5252 * what's the status of the given connectors, play safe and
5253 * run the DP detection too.
5254 */
25f78f58
VS
5255 WARN_ON_ONCE(!HAS_DDI(dev));
5256 case INTEL_OUTPUT_DISPLAYPORT:
5257 case INTEL_OUTPUT_EDP:
5258 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
5259 return port_to_aux_power_domain(intel_dig_port->port);
5260 case INTEL_OUTPUT_DP_MST:
5261 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
5262 return port_to_aux_power_domain(intel_dig_port->port);
5263 default:
b9fec167 5264 MISSING_CASE(intel_encoder->type);
25f78f58
VS
5265 return POWER_DOMAIN_AUX_A;
5266 }
5267}
5268
74bff5f9
ML
5269static unsigned long get_crtc_power_domains(struct drm_crtc *crtc,
5270 struct intel_crtc_state *crtc_state)
77d22dca 5271{
319be8ae 5272 struct drm_device *dev = crtc->dev;
74bff5f9 5273 struct drm_encoder *encoder;
319be8ae
ID
5274 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5275 enum pipe pipe = intel_crtc->pipe;
77d22dca 5276 unsigned long mask;
74bff5f9 5277 enum transcoder transcoder = crtc_state->cpu_transcoder;
77d22dca 5278
74bff5f9 5279 if (!crtc_state->base.active)
292b990e
ML
5280 return 0;
5281
77d22dca
ID
5282 mask = BIT(POWER_DOMAIN_PIPE(pipe));
5283 mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
74bff5f9
ML
5284 if (crtc_state->pch_pfit.enabled ||
5285 crtc_state->pch_pfit.force_thru)
77d22dca
ID
5286 mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
5287
74bff5f9
ML
5288 drm_for_each_encoder_mask(encoder, dev, crtc_state->base.encoder_mask) {
5289 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
5290
319be8ae 5291 mask |= BIT(intel_display_port_power_domain(intel_encoder));
74bff5f9 5292 }
319be8ae 5293
15e7ec29
ML
5294 if (crtc_state->shared_dpll)
5295 mask |= BIT(POWER_DOMAIN_PLLS);
5296
77d22dca
ID
5297 return mask;
5298}
5299
74bff5f9
ML
5300static unsigned long
5301modeset_get_crtc_power_domains(struct drm_crtc *crtc,
5302 struct intel_crtc_state *crtc_state)
77d22dca 5303{
292b990e
ML
5304 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
5305 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5306 enum intel_display_power_domain domain;
5307 unsigned long domains, new_domains, old_domains;
77d22dca 5308
292b990e 5309 old_domains = intel_crtc->enabled_power_domains;
74bff5f9
ML
5310 intel_crtc->enabled_power_domains = new_domains =
5311 get_crtc_power_domains(crtc, crtc_state);
77d22dca 5312
292b990e
ML
5313 domains = new_domains & ~old_domains;
5314
5315 for_each_power_domain(domain, domains)
5316 intel_display_power_get(dev_priv, domain);
5317
5318 return old_domains & ~new_domains;
5319}
5320
5321static void modeset_put_power_domains(struct drm_i915_private *dev_priv,
5322 unsigned long domains)
5323{
5324 enum intel_display_power_domain domain;
5325
5326 for_each_power_domain(domain, domains)
5327 intel_display_power_put(dev_priv, domain);
5328}
77d22dca 5329
adafdc6f
MK
5330static int intel_compute_max_dotclk(struct drm_i915_private *dev_priv)
5331{
5332 int max_cdclk_freq = dev_priv->max_cdclk_freq;
5333
5334 if (INTEL_INFO(dev_priv)->gen >= 9 ||
5335 IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
5336 return max_cdclk_freq;
5337 else if (IS_CHERRYVIEW(dev_priv))
5338 return max_cdclk_freq*95/100;
5339 else if (INTEL_INFO(dev_priv)->gen < 4)
5340 return 2*max_cdclk_freq*90/100;
5341 else
5342 return max_cdclk_freq*90/100;
5343}
5344
560a7ae4
DL
5345static void intel_update_max_cdclk(struct drm_device *dev)
5346{
5347 struct drm_i915_private *dev_priv = dev->dev_private;
5348
ef11bdb3 5349 if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) {
560a7ae4
DL
5350 u32 limit = I915_READ(SKL_DFSM) & SKL_DFSM_CDCLK_LIMIT_MASK;
5351
5352 if (limit == SKL_DFSM_CDCLK_LIMIT_675)
5353 dev_priv->max_cdclk_freq = 675000;
5354 else if (limit == SKL_DFSM_CDCLK_LIMIT_540)
5355 dev_priv->max_cdclk_freq = 540000;
5356 else if (limit == SKL_DFSM_CDCLK_LIMIT_450)
5357 dev_priv->max_cdclk_freq = 450000;
5358 else
5359 dev_priv->max_cdclk_freq = 337500;
5360 } else if (IS_BROADWELL(dev)) {
5361 /*
5362 * FIXME with extra cooling we can allow
5363 * 540 MHz for ULX and 675 Mhz for ULT.
5364 * How can we know if extra cooling is
5365 * available? PCI ID, VTB, something else?
5366 */
5367 if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
5368 dev_priv->max_cdclk_freq = 450000;
5369 else if (IS_BDW_ULX(dev))
5370 dev_priv->max_cdclk_freq = 450000;
5371 else if (IS_BDW_ULT(dev))
5372 dev_priv->max_cdclk_freq = 540000;
5373 else
5374 dev_priv->max_cdclk_freq = 675000;
0904deaf
MK
5375 } else if (IS_CHERRYVIEW(dev)) {
5376 dev_priv->max_cdclk_freq = 320000;
560a7ae4
DL
5377 } else if (IS_VALLEYVIEW(dev)) {
5378 dev_priv->max_cdclk_freq = 400000;
5379 } else {
5380 /* otherwise assume cdclk is fixed */
5381 dev_priv->max_cdclk_freq = dev_priv->cdclk_freq;
5382 }
5383
adafdc6f
MK
5384 dev_priv->max_dotclk_freq = intel_compute_max_dotclk(dev_priv);
5385
560a7ae4
DL
5386 DRM_DEBUG_DRIVER("Max CD clock rate: %d kHz\n",
5387 dev_priv->max_cdclk_freq);
adafdc6f
MK
5388
5389 DRM_DEBUG_DRIVER("Max dotclock rate: %d kHz\n",
5390 dev_priv->max_dotclk_freq);
560a7ae4
DL
5391}
5392
5393static void intel_update_cdclk(struct drm_device *dev)
5394{
5395 struct drm_i915_private *dev_priv = dev->dev_private;
5396
5397 dev_priv->cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
5398 DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
5399 dev_priv->cdclk_freq);
5400
5401 /*
5402 * Program the gmbus_freq based on the cdclk frequency.
5403 * BSpec erroneously claims we should aim for 4MHz, but
5404 * in fact 1MHz is the correct frequency.
5405 */
666a4537 5406 if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
560a7ae4
DL
5407 /*
5408 * Program the gmbus_freq based on the cdclk frequency.
5409 * BSpec erroneously claims we should aim for 4MHz, but
5410 * in fact 1MHz is the correct frequency.
5411 */
5412 I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->cdclk_freq, 1000));
5413 }
5414
5415 if (dev_priv->max_cdclk_freq == 0)
5416 intel_update_max_cdclk(dev);
5417}
5418
70d0c574 5419static void broxton_set_cdclk(struct drm_device *dev, int frequency)
f8437dd1
VK
5420{
5421 struct drm_i915_private *dev_priv = dev->dev_private;
5422 uint32_t divider;
5423 uint32_t ratio;
5424 uint32_t current_freq;
5425 int ret;
5426
5427 /* frequency = 19.2MHz * ratio / 2 / div{1,1.5,2,4} */
5428 switch (frequency) {
5429 case 144000:
5430 divider = BXT_CDCLK_CD2X_DIV_SEL_4;
5431 ratio = BXT_DE_PLL_RATIO(60);
5432 break;
5433 case 288000:
5434 divider = BXT_CDCLK_CD2X_DIV_SEL_2;
5435 ratio = BXT_DE_PLL_RATIO(60);
5436 break;
5437 case 384000:
5438 divider = BXT_CDCLK_CD2X_DIV_SEL_1_5;
5439 ratio = BXT_DE_PLL_RATIO(60);
5440 break;
5441 case 576000:
5442 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
5443 ratio = BXT_DE_PLL_RATIO(60);
5444 break;
5445 case 624000:
5446 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
5447 ratio = BXT_DE_PLL_RATIO(65);
5448 break;
5449 case 19200:
5450 /*
5451 * Bypass frequency with DE PLL disabled. Init ratio, divider
5452 * to suppress GCC warning.
5453 */
5454 ratio = 0;
5455 divider = 0;
5456 break;
5457 default:
5458 DRM_ERROR("unsupported CDCLK freq %d", frequency);
5459
5460 return;
5461 }
5462
5463 mutex_lock(&dev_priv->rps.hw_lock);
5464 /* Inform power controller of upcoming frequency change */
5465 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
5466 0x80000000);
5467 mutex_unlock(&dev_priv->rps.hw_lock);
5468
5469 if (ret) {
5470 DRM_ERROR("PCode CDCLK freq change notify failed (err %d, freq %d)\n",
5471 ret, frequency);
5472 return;
5473 }
5474
5475 current_freq = I915_READ(CDCLK_CTL) & CDCLK_FREQ_DECIMAL_MASK;
5476 /* convert from .1 fixpoint MHz with -1MHz offset to kHz */
5477 current_freq = current_freq * 500 + 1000;
5478
5479 /*
5480 * DE PLL has to be disabled when
5481 * - setting to 19.2MHz (bypass, PLL isn't used)
5482 * - before setting to 624MHz (PLL needs toggling)
5483 * - before setting to any frequency from 624MHz (PLL needs toggling)
5484 */
5485 if (frequency == 19200 || frequency == 624000 ||
5486 current_freq == 624000) {
5487 I915_WRITE(BXT_DE_PLL_ENABLE, ~BXT_DE_PLL_PLL_ENABLE);
5488 /* Timeout 200us */
5489 if (wait_for(!(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK),
5490 1))
5491 DRM_ERROR("timout waiting for DE PLL unlock\n");
5492 }
5493
5494 if (frequency != 19200) {
5495 uint32_t val;
5496
5497 val = I915_READ(BXT_DE_PLL_CTL);
5498 val &= ~BXT_DE_PLL_RATIO_MASK;
5499 val |= ratio;
5500 I915_WRITE(BXT_DE_PLL_CTL, val);
5501
5502 I915_WRITE(BXT_DE_PLL_ENABLE, BXT_DE_PLL_PLL_ENABLE);
5503 /* Timeout 200us */
5504 if (wait_for(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK, 1))
5505 DRM_ERROR("timeout waiting for DE PLL lock\n");
5506
5507 val = I915_READ(CDCLK_CTL);
5508 val &= ~BXT_CDCLK_CD2X_DIV_SEL_MASK;
5509 val |= divider;
5510 /*
5511 * Disable SSA Precharge when CD clock frequency < 500 MHz,
5512 * enable otherwise.
5513 */
5514 val &= ~BXT_CDCLK_SSA_PRECHARGE_ENABLE;
5515 if (frequency >= 500000)
5516 val |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
5517
5518 val &= ~CDCLK_FREQ_DECIMAL_MASK;
5519 /* convert from kHz to .1 fixpoint MHz with -1MHz offset */
5520 val |= (frequency - 1000) / 500;
5521 I915_WRITE(CDCLK_CTL, val);
5522 }
5523
5524 mutex_lock(&dev_priv->rps.hw_lock);
5525 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
5526 DIV_ROUND_UP(frequency, 25000));
5527 mutex_unlock(&dev_priv->rps.hw_lock);
5528
5529 if (ret) {
5530 DRM_ERROR("PCode CDCLK freq set failed, (err %d, freq %d)\n",
5531 ret, frequency);
5532 return;
5533 }
5534
a47871bd 5535 intel_update_cdclk(dev);
f8437dd1
VK
5536}
5537
5538void broxton_init_cdclk(struct drm_device *dev)
5539{
5540 struct drm_i915_private *dev_priv = dev->dev_private;
5541 uint32_t val;
5542
5543 /*
5544 * NDE_RSTWRN_OPT RST PCH Handshake En must always be 0b on BXT
5545 * or else the reset will hang because there is no PCH to respond.
5546 * Move the handshake programming to initialization sequence.
5547 * Previously was left up to BIOS.
5548 */
5549 val = I915_READ(HSW_NDE_RSTWRN_OPT);
5550 val &= ~RESET_PCH_HANDSHAKE_ENABLE;
5551 I915_WRITE(HSW_NDE_RSTWRN_OPT, val);
5552
5553 /* Enable PG1 for cdclk */
5554 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
5555
5556 /* check if cd clock is enabled */
5557 if (I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_PLL_ENABLE) {
5558 DRM_DEBUG_KMS("Display already initialized\n");
5559 return;
5560 }
5561
5562 /*
5563 * FIXME:
5564 * - The initial CDCLK needs to be read from VBT.
5565 * Need to make this change after VBT has changes for BXT.
5566 * - check if setting the max (or any) cdclk freq is really necessary
5567 * here, it belongs to modeset time
5568 */
5569 broxton_set_cdclk(dev, 624000);
5570
5571 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
22e02c0b
VS
5572 POSTING_READ(DBUF_CTL);
5573
f8437dd1
VK
5574 udelay(10);
5575
5576 if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
5577 DRM_ERROR("DBuf power enable timeout!\n");
5578}
5579
5580void broxton_uninit_cdclk(struct drm_device *dev)
5581{
5582 struct drm_i915_private *dev_priv = dev->dev_private;
5583
5584 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
22e02c0b
VS
5585 POSTING_READ(DBUF_CTL);
5586
f8437dd1
VK
5587 udelay(10);
5588
5589 if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
5590 DRM_ERROR("DBuf power disable timeout!\n");
5591
5592 /* Set minimum (bypass) frequency, in effect turning off the DE PLL */
5593 broxton_set_cdclk(dev, 19200);
5594
5595 intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
5596}
5597
5d96d8af
DL
5598static const struct skl_cdclk_entry {
5599 unsigned int freq;
5600 unsigned int vco;
5601} skl_cdclk_frequencies[] = {
5602 { .freq = 308570, .vco = 8640 },
5603 { .freq = 337500, .vco = 8100 },
5604 { .freq = 432000, .vco = 8640 },
5605 { .freq = 450000, .vco = 8100 },
5606 { .freq = 540000, .vco = 8100 },
5607 { .freq = 617140, .vco = 8640 },
5608 { .freq = 675000, .vco = 8100 },
5609};
5610
5611static unsigned int skl_cdclk_decimal(unsigned int freq)
5612{
5613 return (freq - 1000) / 500;
5614}
5615
5616static unsigned int skl_cdclk_get_vco(unsigned int freq)
5617{
5618 unsigned int i;
5619
5620 for (i = 0; i < ARRAY_SIZE(skl_cdclk_frequencies); i++) {
5621 const struct skl_cdclk_entry *e = &skl_cdclk_frequencies[i];
5622
5623 if (e->freq == freq)
5624 return e->vco;
5625 }
5626
5627 return 8100;
5628}
5629
5630static void
5631skl_dpll0_enable(struct drm_i915_private *dev_priv, unsigned int required_vco)
5632{
5633 unsigned int min_freq;
5634 u32 val;
5635
5636 /* select the minimum CDCLK before enabling DPLL 0 */
5637 val = I915_READ(CDCLK_CTL);
5638 val &= ~CDCLK_FREQ_SEL_MASK | ~CDCLK_FREQ_DECIMAL_MASK;
5639 val |= CDCLK_FREQ_337_308;
5640
5641 if (required_vco == 8640)
5642 min_freq = 308570;
5643 else
5644 min_freq = 337500;
5645
5646 val = CDCLK_FREQ_337_308 | skl_cdclk_decimal(min_freq);
5647
5648 I915_WRITE(CDCLK_CTL, val);
5649 POSTING_READ(CDCLK_CTL);
5650
5651 /*
5652 * We always enable DPLL0 with the lowest link rate possible, but still
5653 * taking into account the VCO required to operate the eDP panel at the
5654 * desired frequency. The usual DP link rates operate with a VCO of
5655 * 8100 while the eDP 1.4 alternate link rates need a VCO of 8640.
5656 * The modeset code is responsible for the selection of the exact link
5657 * rate later on, with the constraint of choosing a frequency that
5658 * works with required_vco.
5659 */
5660 val = I915_READ(DPLL_CTRL1);
5661
5662 val &= ~(DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) | DPLL_CTRL1_SSC(SKL_DPLL0) |
5663 DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
5664 val |= DPLL_CTRL1_OVERRIDE(SKL_DPLL0);
5665 if (required_vco == 8640)
5666 val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080,
5667 SKL_DPLL0);
5668 else
5669 val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810,
5670 SKL_DPLL0);
5671
5672 I915_WRITE(DPLL_CTRL1, val);
5673 POSTING_READ(DPLL_CTRL1);
5674
5675 I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) | LCPLL_PLL_ENABLE);
5676
5677 if (wait_for(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK, 5))
5678 DRM_ERROR("DPLL0 not locked\n");
5679}
5680
5681static bool skl_cdclk_pcu_ready(struct drm_i915_private *dev_priv)
5682{
5683 int ret;
5684 u32 val;
5685
5686 /* inform PCU we want to change CDCLK */
5687 val = SKL_CDCLK_PREPARE_FOR_CHANGE;
5688 mutex_lock(&dev_priv->rps.hw_lock);
5689 ret = sandybridge_pcode_read(dev_priv, SKL_PCODE_CDCLK_CONTROL, &val);
5690 mutex_unlock(&dev_priv->rps.hw_lock);
5691
5692 return ret == 0 && (val & SKL_CDCLK_READY_FOR_CHANGE);
5693}
5694
5695static bool skl_cdclk_wait_for_pcu_ready(struct drm_i915_private *dev_priv)
5696{
5697 unsigned int i;
5698
5699 for (i = 0; i < 15; i++) {
5700 if (skl_cdclk_pcu_ready(dev_priv))
5701 return true;
5702 udelay(10);
5703 }
5704
5705 return false;
5706}
5707
5708static void skl_set_cdclk(struct drm_i915_private *dev_priv, unsigned int freq)
5709{
560a7ae4 5710 struct drm_device *dev = dev_priv->dev;
5d96d8af
DL
5711 u32 freq_select, pcu_ack;
5712
5713 DRM_DEBUG_DRIVER("Changing CDCLK to %dKHz\n", freq);
5714
5715 if (!skl_cdclk_wait_for_pcu_ready(dev_priv)) {
5716 DRM_ERROR("failed to inform PCU about cdclk change\n");
5717 return;
5718 }
5719
5720 /* set CDCLK_CTL */
5721 switch(freq) {
5722 case 450000:
5723 case 432000:
5724 freq_select = CDCLK_FREQ_450_432;
5725 pcu_ack = 1;
5726 break;
5727 case 540000:
5728 freq_select = CDCLK_FREQ_540;
5729 pcu_ack = 2;
5730 break;
5731 case 308570:
5732 case 337500:
5733 default:
5734 freq_select = CDCLK_FREQ_337_308;
5735 pcu_ack = 0;
5736 break;
5737 case 617140:
5738 case 675000:
5739 freq_select = CDCLK_FREQ_675_617;
5740 pcu_ack = 3;
5741 break;
5742 }
5743
5744 I915_WRITE(CDCLK_CTL, freq_select | skl_cdclk_decimal(freq));
5745 POSTING_READ(CDCLK_CTL);
5746
5747 /* inform PCU of the change */
5748 mutex_lock(&dev_priv->rps.hw_lock);
5749 sandybridge_pcode_write(dev_priv, SKL_PCODE_CDCLK_CONTROL, pcu_ack);
5750 mutex_unlock(&dev_priv->rps.hw_lock);
560a7ae4
DL
5751
5752 intel_update_cdclk(dev);
5d96d8af
DL
5753}
5754
5755void skl_uninit_cdclk(struct drm_i915_private *dev_priv)
5756{
5757 /* disable DBUF power */
5758 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
5759 POSTING_READ(DBUF_CTL);
5760
5761 udelay(10);
5762
5763 if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
5764 DRM_ERROR("DBuf power disable timeout\n");
5765
ab96c1ee
ID
5766 /* disable DPLL0 */
5767 I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) & ~LCPLL_PLL_ENABLE);
5768 if (wait_for(!(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK), 1))
5769 DRM_ERROR("Couldn't disable DPLL0\n");
5d96d8af
DL
5770}
5771
5772void skl_init_cdclk(struct drm_i915_private *dev_priv)
5773{
5d96d8af
DL
5774 unsigned int required_vco;
5775
39d9b85a
GW
5776 /* DPLL0 not enabled (happens on early BIOS versions) */
5777 if (!(I915_READ(LCPLL1_CTL) & LCPLL_PLL_ENABLE)) {
5778 /* enable DPLL0 */
5779 required_vco = skl_cdclk_get_vco(dev_priv->skl_boot_cdclk);
5780 skl_dpll0_enable(dev_priv, required_vco);
5d96d8af
DL
5781 }
5782
5d96d8af
DL
5783 /* set CDCLK to the frequency the BIOS chose */
5784 skl_set_cdclk(dev_priv, dev_priv->skl_boot_cdclk);
5785
5786 /* enable DBUF power */
5787 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
5788 POSTING_READ(DBUF_CTL);
5789
5790 udelay(10);
5791
5792 if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
5793 DRM_ERROR("DBuf power enable timeout\n");
5794}
5795
c73666f3
SK
5796int skl_sanitize_cdclk(struct drm_i915_private *dev_priv)
5797{
5798 uint32_t lcpll1 = I915_READ(LCPLL1_CTL);
5799 uint32_t cdctl = I915_READ(CDCLK_CTL);
5800 int freq = dev_priv->skl_boot_cdclk;
5801
f1b391a5
SK
5802 /*
5803 * check if the pre-os intialized the display
5804 * There is SWF18 scratchpad register defined which is set by the
5805 * pre-os which can be used by the OS drivers to check the status
5806 */
5807 if ((I915_READ(SWF_ILK(0x18)) & 0x00FFFFFF) == 0)
5808 goto sanitize;
5809
c73666f3
SK
5810 /* Is PLL enabled and locked ? */
5811 if (!((lcpll1 & LCPLL_PLL_ENABLE) && (lcpll1 & LCPLL_PLL_LOCK)))
5812 goto sanitize;
5813
5814 /* DPLL okay; verify the cdclock
5815 *
5816 * Noticed in some instances that the freq selection is correct but
5817 * decimal part is programmed wrong from BIOS where pre-os does not
5818 * enable display. Verify the same as well.
5819 */
5820 if (cdctl == ((cdctl & CDCLK_FREQ_SEL_MASK) | skl_cdclk_decimal(freq)))
5821 /* All well; nothing to sanitize */
5822 return false;
5823sanitize:
5824 /*
5825 * As of now initialize with max cdclk till
5826 * we get dynamic cdclk support
5827 * */
5828 dev_priv->skl_boot_cdclk = dev_priv->max_cdclk_freq;
5829 skl_init_cdclk(dev_priv);
5830
5831 /* we did have to sanitize */
5832 return true;
5833}
5834
30a970c6
JB
5835/* Adjust CDclk dividers to allow high res or save power if possible */
5836static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
5837{
5838 struct drm_i915_private *dev_priv = dev->dev_private;
5839 u32 val, cmd;
5840
164dfd28
VK
5841 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5842 != dev_priv->cdclk_freq);
d60c4473 5843
dfcab17e 5844 if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
30a970c6 5845 cmd = 2;
dfcab17e 5846 else if (cdclk == 266667)
30a970c6
JB
5847 cmd = 1;
5848 else
5849 cmd = 0;
5850
5851 mutex_lock(&dev_priv->rps.hw_lock);
5852 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5853 val &= ~DSPFREQGUAR_MASK;
5854 val |= (cmd << DSPFREQGUAR_SHIFT);
5855 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5856 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5857 DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
5858 50)) {
5859 DRM_ERROR("timed out waiting for CDclk change\n");
5860 }
5861 mutex_unlock(&dev_priv->rps.hw_lock);
5862
54433e91
VS
5863 mutex_lock(&dev_priv->sb_lock);
5864
dfcab17e 5865 if (cdclk == 400000) {
6bcda4f0 5866 u32 divider;
30a970c6 5867
6bcda4f0 5868 divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
30a970c6 5869
30a970c6
JB
5870 /* adjust cdclk divider */
5871 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
87d5d259 5872 val &= ~CCK_FREQUENCY_VALUES;
30a970c6
JB
5873 val |= divider;
5874 vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
a877e801
VS
5875
5876 if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
87d5d259 5877 CCK_FREQUENCY_STATUS) == (divider << CCK_FREQUENCY_STATUS_SHIFT),
a877e801
VS
5878 50))
5879 DRM_ERROR("timed out waiting for CDclk change\n");
30a970c6
JB
5880 }
5881
30a970c6
JB
5882 /* adjust self-refresh exit latency value */
5883 val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
5884 val &= ~0x7f;
5885
5886 /*
5887 * For high bandwidth configs, we set a higher latency in the bunit
5888 * so that the core display fetch happens in time to avoid underruns.
5889 */
dfcab17e 5890 if (cdclk == 400000)
30a970c6
JB
5891 val |= 4500 / 250; /* 4.5 usec */
5892 else
5893 val |= 3000 / 250; /* 3.0 usec */
5894 vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
54433e91 5895
a580516d 5896 mutex_unlock(&dev_priv->sb_lock);
30a970c6 5897
b6283055 5898 intel_update_cdclk(dev);
30a970c6
JB
5899}
5900
383c5a6a
VS
5901static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
5902{
5903 struct drm_i915_private *dev_priv = dev->dev_private;
5904 u32 val, cmd;
5905
164dfd28
VK
5906 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5907 != dev_priv->cdclk_freq);
383c5a6a
VS
5908
5909 switch (cdclk) {
383c5a6a
VS
5910 case 333333:
5911 case 320000:
383c5a6a 5912 case 266667:
383c5a6a 5913 case 200000:
383c5a6a
VS
5914 break;
5915 default:
5f77eeb0 5916 MISSING_CASE(cdclk);
383c5a6a
VS
5917 return;
5918 }
5919
9d0d3fda
VS
5920 /*
5921 * Specs are full of misinformation, but testing on actual
5922 * hardware has shown that we just need to write the desired
5923 * CCK divider into the Punit register.
5924 */
5925 cmd = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
5926
383c5a6a
VS
5927 mutex_lock(&dev_priv->rps.hw_lock);
5928 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5929 val &= ~DSPFREQGUAR_MASK_CHV;
5930 val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
5931 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5932 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5933 DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
5934 50)) {
5935 DRM_ERROR("timed out waiting for CDclk change\n");
5936 }
5937 mutex_unlock(&dev_priv->rps.hw_lock);
5938
b6283055 5939 intel_update_cdclk(dev);
383c5a6a
VS
5940}
5941
30a970c6
JB
5942static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
5943 int max_pixclk)
5944{
6bcda4f0 5945 int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
6cca3195 5946 int limit = IS_CHERRYVIEW(dev_priv) ? 95 : 90;
29dc7ef3 5947
30a970c6
JB
5948 /*
5949 * Really only a few cases to deal with, as only 4 CDclks are supported:
5950 * 200MHz
5951 * 267MHz
29dc7ef3 5952 * 320/333MHz (depends on HPLL freq)
6cca3195
VS
5953 * 400MHz (VLV only)
5954 * So we check to see whether we're above 90% (VLV) or 95% (CHV)
5955 * of the lower bin and adjust if needed.
e37c67a1
VS
5956 *
5957 * We seem to get an unstable or solid color picture at 200MHz.
5958 * Not sure what's wrong. For now use 200MHz only when all pipes
5959 * are off.
30a970c6 5960 */
6cca3195
VS
5961 if (!IS_CHERRYVIEW(dev_priv) &&
5962 max_pixclk > freq_320*limit/100)
dfcab17e 5963 return 400000;
6cca3195 5964 else if (max_pixclk > 266667*limit/100)
29dc7ef3 5965 return freq_320;
e37c67a1 5966 else if (max_pixclk > 0)
dfcab17e 5967 return 266667;
e37c67a1
VS
5968 else
5969 return 200000;
30a970c6
JB
5970}
5971
f8437dd1
VK
5972static int broxton_calc_cdclk(struct drm_i915_private *dev_priv,
5973 int max_pixclk)
5974{
5975 /*
5976 * FIXME:
5977 * - remove the guardband, it's not needed on BXT
5978 * - set 19.2MHz bypass frequency if there are no active pipes
5979 */
5980 if (max_pixclk > 576000*9/10)
5981 return 624000;
5982 else if (max_pixclk > 384000*9/10)
5983 return 576000;
5984 else if (max_pixclk > 288000*9/10)
5985 return 384000;
5986 else if (max_pixclk > 144000*9/10)
5987 return 288000;
5988 else
5989 return 144000;
5990}
5991
e8788cbc 5992/* Compute the max pixel clock for new configuration. */
a821fc46
ACO
5993static int intel_mode_max_pixclk(struct drm_device *dev,
5994 struct drm_atomic_state *state)
30a970c6 5995{
565602d7
ML
5996 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
5997 struct drm_i915_private *dev_priv = dev->dev_private;
5998 struct drm_crtc *crtc;
5999 struct drm_crtc_state *crtc_state;
6000 unsigned max_pixclk = 0, i;
6001 enum pipe pipe;
30a970c6 6002
565602d7
ML
6003 memcpy(intel_state->min_pixclk, dev_priv->min_pixclk,
6004 sizeof(intel_state->min_pixclk));
304603f4 6005
565602d7
ML
6006 for_each_crtc_in_state(state, crtc, crtc_state, i) {
6007 int pixclk = 0;
6008
6009 if (crtc_state->enable)
6010 pixclk = crtc_state->adjusted_mode.crtc_clock;
304603f4 6011
565602d7 6012 intel_state->min_pixclk[i] = pixclk;
30a970c6
JB
6013 }
6014
565602d7
ML
6015 for_each_pipe(dev_priv, pipe)
6016 max_pixclk = max(intel_state->min_pixclk[pipe], max_pixclk);
6017
30a970c6
JB
6018 return max_pixclk;
6019}
6020
27c329ed 6021static int valleyview_modeset_calc_cdclk(struct drm_atomic_state *state)
30a970c6 6022{
27c329ed
ML
6023 struct drm_device *dev = state->dev;
6024 struct drm_i915_private *dev_priv = dev->dev_private;
6025 int max_pixclk = intel_mode_max_pixclk(dev, state);
1a617b77
ML
6026 struct intel_atomic_state *intel_state =
6027 to_intel_atomic_state(state);
30a970c6 6028
304603f4
ACO
6029 if (max_pixclk < 0)
6030 return max_pixclk;
30a970c6 6031
1a617b77 6032 intel_state->cdclk = intel_state->dev_cdclk =
27c329ed 6033 valleyview_calc_cdclk(dev_priv, max_pixclk);
0a9ab303 6034
1a617b77
ML
6035 if (!intel_state->active_crtcs)
6036 intel_state->dev_cdclk = valleyview_calc_cdclk(dev_priv, 0);
6037
27c329ed
ML
6038 return 0;
6039}
304603f4 6040
27c329ed
ML
6041static int broxton_modeset_calc_cdclk(struct drm_atomic_state *state)
6042{
6043 struct drm_device *dev = state->dev;
6044 struct drm_i915_private *dev_priv = dev->dev_private;
6045 int max_pixclk = intel_mode_max_pixclk(dev, state);
1a617b77
ML
6046 struct intel_atomic_state *intel_state =
6047 to_intel_atomic_state(state);
85a96e7a 6048
27c329ed
ML
6049 if (max_pixclk < 0)
6050 return max_pixclk;
85a96e7a 6051
1a617b77 6052 intel_state->cdclk = intel_state->dev_cdclk =
27c329ed 6053 broxton_calc_cdclk(dev_priv, max_pixclk);
85a96e7a 6054
1a617b77
ML
6055 if (!intel_state->active_crtcs)
6056 intel_state->dev_cdclk = broxton_calc_cdclk(dev_priv, 0);
6057
27c329ed 6058 return 0;
30a970c6
JB
6059}
6060
1e69cd74
VS
6061static void vlv_program_pfi_credits(struct drm_i915_private *dev_priv)
6062{
6063 unsigned int credits, default_credits;
6064
6065 if (IS_CHERRYVIEW(dev_priv))
6066 default_credits = PFI_CREDIT(12);
6067 else
6068 default_credits = PFI_CREDIT(8);
6069
bfa7df01 6070 if (dev_priv->cdclk_freq >= dev_priv->czclk_freq) {
1e69cd74
VS
6071 /* CHV suggested value is 31 or 63 */
6072 if (IS_CHERRYVIEW(dev_priv))
fcc0008f 6073 credits = PFI_CREDIT_63;
1e69cd74
VS
6074 else
6075 credits = PFI_CREDIT(15);
6076 } else {
6077 credits = default_credits;
6078 }
6079
6080 /*
6081 * WA - write default credits before re-programming
6082 * FIXME: should we also set the resend bit here?
6083 */
6084 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
6085 default_credits);
6086
6087 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
6088 credits | PFI_CREDIT_RESEND);
6089
6090 /*
6091 * FIXME is this guaranteed to clear
6092 * immediately or should we poll for it?
6093 */
6094 WARN_ON(I915_READ(GCI_CONTROL) & PFI_CREDIT_RESEND);
6095}
6096
27c329ed 6097static void valleyview_modeset_commit_cdclk(struct drm_atomic_state *old_state)
30a970c6 6098{
a821fc46 6099 struct drm_device *dev = old_state->dev;
30a970c6 6100 struct drm_i915_private *dev_priv = dev->dev_private;
1a617b77
ML
6101 struct intel_atomic_state *old_intel_state =
6102 to_intel_atomic_state(old_state);
6103 unsigned req_cdclk = old_intel_state->dev_cdclk;
30a970c6 6104
27c329ed
ML
6105 /*
6106 * FIXME: We can end up here with all power domains off, yet
6107 * with a CDCLK frequency other than the minimum. To account
6108 * for this take the PIPE-A power domain, which covers the HW
6109 * blocks needed for the following programming. This can be
6110 * removed once it's guaranteed that we get here either with
6111 * the minimum CDCLK set, or the required power domains
6112 * enabled.
6113 */
6114 intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
738c05c0 6115
27c329ed
ML
6116 if (IS_CHERRYVIEW(dev))
6117 cherryview_set_cdclk(dev, req_cdclk);
6118 else
6119 valleyview_set_cdclk(dev, req_cdclk);
738c05c0 6120
27c329ed 6121 vlv_program_pfi_credits(dev_priv);
1e69cd74 6122
27c329ed 6123 intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
30a970c6
JB
6124}
6125
89b667f8
JB
6126static void valleyview_crtc_enable(struct drm_crtc *crtc)
6127{
6128 struct drm_device *dev = crtc->dev;
a72e4c9f 6129 struct drm_i915_private *dev_priv = to_i915(dev);
89b667f8
JB
6130 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6131 struct intel_encoder *encoder;
6132 int pipe = intel_crtc->pipe;
89b667f8 6133
53d9f4e9 6134 if (WARN_ON(intel_crtc->active))
89b667f8
JB
6135 return;
6136
6e3c9717 6137 if (intel_crtc->config->has_dp_encoder)
fe3cd48d 6138 intel_dp_set_m_n(intel_crtc, M1_N1);
5b18e57c
DV
6139
6140 intel_set_pipe_timings(intel_crtc);
bc58be60 6141 intel_set_pipe_src_size(intel_crtc);
5b18e57c 6142
c14b0485
VS
6143 if (IS_CHERRYVIEW(dev) && pipe == PIPE_B) {
6144 struct drm_i915_private *dev_priv = dev->dev_private;
6145
6146 I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
6147 I915_WRITE(CHV_CANVAS(pipe), 0);
6148 }
6149
5b18e57c
DV
6150 i9xx_set_pipeconf(intel_crtc);
6151
89b667f8 6152 intel_crtc->active = true;
89b667f8 6153
a72e4c9f 6154 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4a3436e8 6155
89b667f8
JB
6156 for_each_encoder_on_crtc(dev, crtc, encoder)
6157 if (encoder->pre_pll_enable)
6158 encoder->pre_pll_enable(encoder);
6159
a65347ba 6160 if (!intel_crtc->config->has_dsi_encoder) {
c0b4c660
VS
6161 if (IS_CHERRYVIEW(dev)) {
6162 chv_prepare_pll(intel_crtc, intel_crtc->config);
6e3c9717 6163 chv_enable_pll(intel_crtc, intel_crtc->config);
c0b4c660
VS
6164 } else {
6165 vlv_prepare_pll(intel_crtc, intel_crtc->config);
6e3c9717 6166 vlv_enable_pll(intel_crtc, intel_crtc->config);
c0b4c660 6167 }
9d556c99 6168 }
89b667f8
JB
6169
6170 for_each_encoder_on_crtc(dev, crtc, encoder)
6171 if (encoder->pre_enable)
6172 encoder->pre_enable(encoder);
6173
2dd24552
JB
6174 i9xx_pfit_enable(intel_crtc);
6175
63cbb074
VS
6176 intel_crtc_load_lut(crtc);
6177
caed361d 6178 intel_update_watermarks(crtc);
e1fdc473 6179 intel_enable_pipe(intel_crtc);
be6a6f8e 6180
4b3a9526
VS
6181 assert_vblank_disabled(crtc);
6182 drm_crtc_vblank_on(crtc);
6183
f9b61ff6
DV
6184 for_each_encoder_on_crtc(dev, crtc, encoder)
6185 encoder->enable(encoder);
89b667f8
JB
6186}
6187
f13c2ef3
DV
6188static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
6189{
6190 struct drm_device *dev = crtc->base.dev;
6191 struct drm_i915_private *dev_priv = dev->dev_private;
6192
6e3c9717
ACO
6193 I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
6194 I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
f13c2ef3
DV
6195}
6196
0b8765c6 6197static void i9xx_crtc_enable(struct drm_crtc *crtc)
79e53945
JB
6198{
6199 struct drm_device *dev = crtc->dev;
a72e4c9f 6200 struct drm_i915_private *dev_priv = to_i915(dev);
79e53945 6201 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 6202 struct intel_encoder *encoder;
79e53945 6203 int pipe = intel_crtc->pipe;
79e53945 6204
53d9f4e9 6205 if (WARN_ON(intel_crtc->active))
f7abfe8b
CW
6206 return;
6207
f13c2ef3
DV
6208 i9xx_set_pll_dividers(intel_crtc);
6209
6e3c9717 6210 if (intel_crtc->config->has_dp_encoder)
fe3cd48d 6211 intel_dp_set_m_n(intel_crtc, M1_N1);
5b18e57c
DV
6212
6213 intel_set_pipe_timings(intel_crtc);
bc58be60 6214 intel_set_pipe_src_size(intel_crtc);
5b18e57c 6215
5b18e57c
DV
6216 i9xx_set_pipeconf(intel_crtc);
6217
f7abfe8b 6218 intel_crtc->active = true;
6b383a7f 6219
4a3436e8 6220 if (!IS_GEN2(dev))
a72e4c9f 6221 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4a3436e8 6222
9d6d9f19
MK
6223 for_each_encoder_on_crtc(dev, crtc, encoder)
6224 if (encoder->pre_enable)
6225 encoder->pre_enable(encoder);
6226
f6736a1a
DV
6227 i9xx_enable_pll(intel_crtc);
6228
2dd24552
JB
6229 i9xx_pfit_enable(intel_crtc);
6230
63cbb074
VS
6231 intel_crtc_load_lut(crtc);
6232
f37fcc2a 6233 intel_update_watermarks(crtc);
e1fdc473 6234 intel_enable_pipe(intel_crtc);
be6a6f8e 6235
4b3a9526
VS
6236 assert_vblank_disabled(crtc);
6237 drm_crtc_vblank_on(crtc);
6238
f9b61ff6
DV
6239 for_each_encoder_on_crtc(dev, crtc, encoder)
6240 encoder->enable(encoder);
0b8765c6 6241}
79e53945 6242
87476d63
DV
6243static void i9xx_pfit_disable(struct intel_crtc *crtc)
6244{
6245 struct drm_device *dev = crtc->base.dev;
6246 struct drm_i915_private *dev_priv = dev->dev_private;
87476d63 6247
6e3c9717 6248 if (!crtc->config->gmch_pfit.control)
328d8e82 6249 return;
87476d63 6250
328d8e82 6251 assert_pipe_disabled(dev_priv, crtc->pipe);
87476d63 6252
328d8e82
DV
6253 DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
6254 I915_READ(PFIT_CONTROL));
6255 I915_WRITE(PFIT_CONTROL, 0);
87476d63
DV
6256}
6257
0b8765c6
JB
6258static void i9xx_crtc_disable(struct drm_crtc *crtc)
6259{
6260 struct drm_device *dev = crtc->dev;
6261 struct drm_i915_private *dev_priv = dev->dev_private;
6262 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 6263 struct intel_encoder *encoder;
0b8765c6 6264 int pipe = intel_crtc->pipe;
ef9c3aee 6265
6304cd91
VS
6266 /*
6267 * On gen2 planes are double buffered but the pipe isn't, so we must
6268 * wait for planes to fully turn off before disabling the pipe.
564ed191
ID
6269 * We also need to wait on all gmch platforms because of the
6270 * self-refresh mode constraint explained above.
6304cd91 6271 */
564ed191 6272 intel_wait_for_vblank(dev, pipe);
6304cd91 6273
4b3a9526
VS
6274 for_each_encoder_on_crtc(dev, crtc, encoder)
6275 encoder->disable(encoder);
6276
f9b61ff6
DV
6277 drm_crtc_vblank_off(crtc);
6278 assert_vblank_disabled(crtc);
6279
575f7ab7 6280 intel_disable_pipe(intel_crtc);
24a1f16d 6281
87476d63 6282 i9xx_pfit_disable(intel_crtc);
24a1f16d 6283
89b667f8
JB
6284 for_each_encoder_on_crtc(dev, crtc, encoder)
6285 if (encoder->post_disable)
6286 encoder->post_disable(encoder);
6287
a65347ba 6288 if (!intel_crtc->config->has_dsi_encoder) {
076ed3b2
CML
6289 if (IS_CHERRYVIEW(dev))
6290 chv_disable_pll(dev_priv, pipe);
6291 else if (IS_VALLEYVIEW(dev))
6292 vlv_disable_pll(dev_priv, pipe);
6293 else
1c4e0274 6294 i9xx_disable_pll(intel_crtc);
076ed3b2 6295 }
0b8765c6 6296
d6db995f
VS
6297 for_each_encoder_on_crtc(dev, crtc, encoder)
6298 if (encoder->post_pll_disable)
6299 encoder->post_pll_disable(encoder);
6300
4a3436e8 6301 if (!IS_GEN2(dev))
a72e4c9f 6302 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
0b8765c6
JB
6303}
6304
b17d48e2
ML
6305static void intel_crtc_disable_noatomic(struct drm_crtc *crtc)
6306{
842e0307 6307 struct intel_encoder *encoder;
b17d48e2
ML
6308 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6309 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
6310 enum intel_display_power_domain domain;
6311 unsigned long domains;
6312
6313 if (!intel_crtc->active)
6314 return;
6315
a539205a 6316 if (to_intel_plane_state(crtc->primary->state)->visible) {
fc32b1fd
ML
6317 WARN_ON(intel_crtc->unpin_work);
6318
2622a081 6319 intel_pre_disable_primary_noatomic(crtc);
54a41961
ML
6320
6321 intel_crtc_disable_planes(crtc, 1 << drm_plane_index(crtc->primary));
6322 to_intel_plane_state(crtc->primary->state)->visible = false;
a539205a
ML
6323 }
6324
b17d48e2 6325 dev_priv->display.crtc_disable(crtc);
842e0307
ML
6326
6327 DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was enabled, now disabled\n",
6328 crtc->base.id);
6329
6330 WARN_ON(drm_atomic_set_mode_for_crtc(crtc->state, NULL) < 0);
6331 crtc->state->active = false;
37d9078b 6332 intel_crtc->active = false;
842e0307
ML
6333 crtc->enabled = false;
6334 crtc->state->connector_mask = 0;
6335 crtc->state->encoder_mask = 0;
6336
6337 for_each_encoder_on_crtc(crtc->dev, crtc, encoder)
6338 encoder->base.crtc = NULL;
6339
58f9c0bc 6340 intel_fbc_disable(intel_crtc);
37d9078b 6341 intel_update_watermarks(crtc);
1f7457b1 6342 intel_disable_shared_dpll(intel_crtc);
b17d48e2
ML
6343
6344 domains = intel_crtc->enabled_power_domains;
6345 for_each_power_domain(domain, domains)
6346 intel_display_power_put(dev_priv, domain);
6347 intel_crtc->enabled_power_domains = 0;
565602d7
ML
6348
6349 dev_priv->active_crtcs &= ~(1 << intel_crtc->pipe);
6350 dev_priv->min_pixclk[intel_crtc->pipe] = 0;
b17d48e2
ML
6351}
6352
6b72d486
ML
6353/*
6354 * turn all crtc's off, but do not adjust state
6355 * This has to be paired with a call to intel_modeset_setup_hw_state.
6356 */
70e0bd74 6357int intel_display_suspend(struct drm_device *dev)
ee7b9f93 6358{
e2c8b870 6359 struct drm_i915_private *dev_priv = to_i915(dev);
70e0bd74 6360 struct drm_atomic_state *state;
e2c8b870 6361 int ret;
70e0bd74 6362
e2c8b870
ML
6363 state = drm_atomic_helper_suspend(dev);
6364 ret = PTR_ERR_OR_ZERO(state);
70e0bd74
ML
6365 if (ret)
6366 DRM_ERROR("Suspending crtc's failed with %i\n", ret);
e2c8b870
ML
6367 else
6368 dev_priv->modeset_restore_state = state;
70e0bd74 6369 return ret;
ee7b9f93
JB
6370}
6371
ea5b213a 6372void intel_encoder_destroy(struct drm_encoder *encoder)
7e7d76c3 6373{
4ef69c7a 6374 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
ea5b213a 6375
ea5b213a
CW
6376 drm_encoder_cleanup(encoder);
6377 kfree(intel_encoder);
7e7d76c3
JB
6378}
6379
0a91ca29
DV
6380/* Cross check the actual hw state with our own modeset state tracking (and it's
6381 * internal consistency). */
b980514c 6382static void intel_connector_check_state(struct intel_connector *connector)
79e53945 6383{
35dd3c64
ML
6384 struct drm_crtc *crtc = connector->base.state->crtc;
6385
6386 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
6387 connector->base.base.id,
6388 connector->base.name);
6389
0a91ca29 6390 if (connector->get_hw_state(connector)) {
e85376cb 6391 struct intel_encoder *encoder = connector->encoder;
35dd3c64 6392 struct drm_connector_state *conn_state = connector->base.state;
0a91ca29 6393
35dd3c64
ML
6394 I915_STATE_WARN(!crtc,
6395 "connector enabled without attached crtc\n");
0a91ca29 6396
35dd3c64
ML
6397 if (!crtc)
6398 return;
6399
6400 I915_STATE_WARN(!crtc->state->active,
6401 "connector is active, but attached crtc isn't\n");
6402
e85376cb 6403 if (!encoder || encoder->type == INTEL_OUTPUT_DP_MST)
35dd3c64
ML
6404 return;
6405
e85376cb 6406 I915_STATE_WARN(conn_state->best_encoder != &encoder->base,
35dd3c64
ML
6407 "atomic encoder doesn't match attached encoder\n");
6408
e85376cb 6409 I915_STATE_WARN(conn_state->crtc != encoder->base.crtc,
35dd3c64
ML
6410 "attached encoder crtc differs from connector crtc\n");
6411 } else {
4d688a2a
ML
6412 I915_STATE_WARN(crtc && crtc->state->active,
6413 "attached crtc is active, but connector isn't\n");
35dd3c64
ML
6414 I915_STATE_WARN(!crtc && connector->base.state->best_encoder,
6415 "best encoder set without crtc!\n");
0a91ca29 6416 }
79e53945
JB
6417}
6418
08d9bc92
ACO
6419int intel_connector_init(struct intel_connector *connector)
6420{
5350a031 6421 drm_atomic_helper_connector_reset(&connector->base);
08d9bc92 6422
5350a031 6423 if (!connector->base.state)
08d9bc92
ACO
6424 return -ENOMEM;
6425
08d9bc92
ACO
6426 return 0;
6427}
6428
6429struct intel_connector *intel_connector_alloc(void)
6430{
6431 struct intel_connector *connector;
6432
6433 connector = kzalloc(sizeof *connector, GFP_KERNEL);
6434 if (!connector)
6435 return NULL;
6436
6437 if (intel_connector_init(connector) < 0) {
6438 kfree(connector);
6439 return NULL;
6440 }
6441
6442 return connector;
6443}
6444
f0947c37
DV
6445/* Simple connector->get_hw_state implementation for encoders that support only
6446 * one connector and no cloning and hence the encoder state determines the state
6447 * of the connector. */
6448bool intel_connector_get_hw_state(struct intel_connector *connector)
ea5b213a 6449{
24929352 6450 enum pipe pipe = 0;
f0947c37 6451 struct intel_encoder *encoder = connector->encoder;
ea5b213a 6452
f0947c37 6453 return encoder->get_hw_state(encoder, &pipe);
ea5b213a
CW
6454}
6455
6d293983 6456static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
d272ddfa 6457{
6d293983
ACO
6458 if (crtc_state->base.enable && crtc_state->has_pch_encoder)
6459 return crtc_state->fdi_lanes;
d272ddfa
VS
6460
6461 return 0;
6462}
6463
6d293983 6464static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
5cec258b 6465 struct intel_crtc_state *pipe_config)
1857e1da 6466{
6d293983
ACO
6467 struct drm_atomic_state *state = pipe_config->base.state;
6468 struct intel_crtc *other_crtc;
6469 struct intel_crtc_state *other_crtc_state;
6470
1857e1da
DV
6471 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
6472 pipe_name(pipe), pipe_config->fdi_lanes);
6473 if (pipe_config->fdi_lanes > 4) {
6474 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
6475 pipe_name(pipe), pipe_config->fdi_lanes);
6d293983 6476 return -EINVAL;
1857e1da
DV
6477 }
6478
bafb6553 6479 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
1857e1da
DV
6480 if (pipe_config->fdi_lanes > 2) {
6481 DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
6482 pipe_config->fdi_lanes);
6d293983 6483 return -EINVAL;
1857e1da 6484 } else {
6d293983 6485 return 0;
1857e1da
DV
6486 }
6487 }
6488
6489 if (INTEL_INFO(dev)->num_pipes == 2)
6d293983 6490 return 0;
1857e1da
DV
6491
6492 /* Ivybridge 3 pipe is really complicated */
6493 switch (pipe) {
6494 case PIPE_A:
6d293983 6495 return 0;
1857e1da 6496 case PIPE_B:
6d293983
ACO
6497 if (pipe_config->fdi_lanes <= 2)
6498 return 0;
6499
6500 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_C));
6501 other_crtc_state =
6502 intel_atomic_get_crtc_state(state, other_crtc);
6503 if (IS_ERR(other_crtc_state))
6504 return PTR_ERR(other_crtc_state);
6505
6506 if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
1857e1da
DV
6507 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
6508 pipe_name(pipe), pipe_config->fdi_lanes);
6d293983 6509 return -EINVAL;
1857e1da 6510 }
6d293983 6511 return 0;
1857e1da 6512 case PIPE_C:
251cc67c
VS
6513 if (pipe_config->fdi_lanes > 2) {
6514 DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
6515 pipe_name(pipe), pipe_config->fdi_lanes);
6d293983 6516 return -EINVAL;
251cc67c 6517 }
6d293983
ACO
6518
6519 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_B));
6520 other_crtc_state =
6521 intel_atomic_get_crtc_state(state, other_crtc);
6522 if (IS_ERR(other_crtc_state))
6523 return PTR_ERR(other_crtc_state);
6524
6525 if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
1857e1da 6526 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
6d293983 6527 return -EINVAL;
1857e1da 6528 }
6d293983 6529 return 0;
1857e1da
DV
6530 default:
6531 BUG();
6532 }
6533}
6534
e29c22c0
DV
6535#define RETRY 1
6536static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
5cec258b 6537 struct intel_crtc_state *pipe_config)
877d48d5 6538{
1857e1da 6539 struct drm_device *dev = intel_crtc->base.dev;
7c5f93b0 6540 const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
6d293983
ACO
6541 int lane, link_bw, fdi_dotclock, ret;
6542 bool needs_recompute = false;
877d48d5 6543
e29c22c0 6544retry:
877d48d5
DV
6545 /* FDI is a binary signal running at ~2.7GHz, encoding
6546 * each output octet as 10 bits. The actual frequency
6547 * is stored as a divider into a 100MHz clock, and the
6548 * mode pixel clock is stored in units of 1KHz.
6549 * Hence the bw of each lane in terms of the mode signal
6550 * is:
6551 */
21a727b3 6552 link_bw = intel_fdi_link_freq(to_i915(dev), pipe_config);
877d48d5 6553
241bfc38 6554 fdi_dotclock = adjusted_mode->crtc_clock;
877d48d5 6555
2bd89a07 6556 lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
877d48d5
DV
6557 pipe_config->pipe_bpp);
6558
6559 pipe_config->fdi_lanes = lane;
6560
2bd89a07 6561 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
877d48d5 6562 link_bw, &pipe_config->fdi_m_n);
1857e1da 6563
e3b247da 6564 ret = ironlake_check_fdi_lanes(dev, intel_crtc->pipe, pipe_config);
6d293983 6565 if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
e29c22c0
DV
6566 pipe_config->pipe_bpp -= 2*3;
6567 DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
6568 pipe_config->pipe_bpp);
6569 needs_recompute = true;
6570 pipe_config->bw_constrained = true;
6571
6572 goto retry;
6573 }
6574
6575 if (needs_recompute)
6576 return RETRY;
6577
6d293983 6578 return ret;
877d48d5
DV
6579}
6580
8cfb3407
VS
6581static bool pipe_config_supports_ips(struct drm_i915_private *dev_priv,
6582 struct intel_crtc_state *pipe_config)
6583{
6584 if (pipe_config->pipe_bpp > 24)
6585 return false;
6586
6587 /* HSW can handle pixel rate up to cdclk? */
6588 if (IS_HASWELL(dev_priv->dev))
6589 return true;
6590
6591 /*
b432e5cf
VS
6592 * We compare against max which means we must take
6593 * the increased cdclk requirement into account when
6594 * calculating the new cdclk.
6595 *
6596 * Should measure whether using a lower cdclk w/o IPS
8cfb3407
VS
6597 */
6598 return ilk_pipe_pixel_rate(pipe_config) <=
6599 dev_priv->max_cdclk_freq * 95 / 100;
6600}
6601
42db64ef 6602static void hsw_compute_ips_config(struct intel_crtc *crtc,
5cec258b 6603 struct intel_crtc_state *pipe_config)
42db64ef 6604{
8cfb3407
VS
6605 struct drm_device *dev = crtc->base.dev;
6606 struct drm_i915_private *dev_priv = dev->dev_private;
6607
d330a953 6608 pipe_config->ips_enabled = i915.enable_ips &&
8cfb3407
VS
6609 hsw_crtc_supports_ips(crtc) &&
6610 pipe_config_supports_ips(dev_priv, pipe_config);
42db64ef
PZ
6611}
6612
39acb4aa
VS
6613static bool intel_crtc_supports_double_wide(const struct intel_crtc *crtc)
6614{
6615 const struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6616
6617 /* GDG double wide on either pipe, otherwise pipe A only */
6618 return INTEL_INFO(dev_priv)->gen < 4 &&
6619 (crtc->pipe == PIPE_A || IS_I915G(dev_priv));
6620}
6621
a43f6e0f 6622static int intel_crtc_compute_config(struct intel_crtc *crtc,
5cec258b 6623 struct intel_crtc_state *pipe_config)
79e53945 6624{
a43f6e0f 6625 struct drm_device *dev = crtc->base.dev;
8bd31e67 6626 struct drm_i915_private *dev_priv = dev->dev_private;
7c5f93b0 6627 const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
89749350 6628
ad3a4479 6629 /* FIXME should check pixel clock limits on all platforms */
cf532bb2 6630 if (INTEL_INFO(dev)->gen < 4) {
39acb4aa 6631 int clock_limit = dev_priv->max_cdclk_freq * 9 / 10;
cf532bb2
VS
6632
6633 /*
39acb4aa 6634 * Enable double wide mode when the dot clock
cf532bb2 6635 * is > 90% of the (display) core speed.
cf532bb2 6636 */
39acb4aa
VS
6637 if (intel_crtc_supports_double_wide(crtc) &&
6638 adjusted_mode->crtc_clock > clock_limit) {
ad3a4479 6639 clock_limit *= 2;
cf532bb2 6640 pipe_config->double_wide = true;
ad3a4479
VS
6641 }
6642
39acb4aa
VS
6643 if (adjusted_mode->crtc_clock > clock_limit) {
6644 DRM_DEBUG_KMS("requested pixel clock (%d kHz) too high (max: %d kHz, double wide: %s)\n",
6645 adjusted_mode->crtc_clock, clock_limit,
6646 yesno(pipe_config->double_wide));
e29c22c0 6647 return -EINVAL;
39acb4aa 6648 }
2c07245f 6649 }
89749350 6650
1d1d0e27
VS
6651 /*
6652 * Pipe horizontal size must be even in:
6653 * - DVO ganged mode
6654 * - LVDS dual channel mode
6655 * - Double wide pipe
6656 */
a93e255f 6657 if ((intel_pipe_will_have_type(pipe_config, INTEL_OUTPUT_LVDS) &&
1d1d0e27
VS
6658 intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
6659 pipe_config->pipe_src_w &= ~1;
6660
8693a824
DL
6661 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
6662 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
44f46b42
CW
6663 */
6664 if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
aad941d5 6665 adjusted_mode->crtc_hsync_start == adjusted_mode->crtc_hdisplay)
e29c22c0 6666 return -EINVAL;
44f46b42 6667
f5adf94e 6668 if (HAS_IPS(dev))
a43f6e0f
DV
6669 hsw_compute_ips_config(crtc, pipe_config);
6670
877d48d5 6671 if (pipe_config->has_pch_encoder)
a43f6e0f 6672 return ironlake_fdi_compute_config(crtc, pipe_config);
877d48d5 6673
cf5a15be 6674 return 0;
79e53945
JB
6675}
6676
1652d19e
VS
6677static int skylake_get_display_clock_speed(struct drm_device *dev)
6678{
6679 struct drm_i915_private *dev_priv = to_i915(dev);
6680 uint32_t lcpll1 = I915_READ(LCPLL1_CTL);
6681 uint32_t cdctl = I915_READ(CDCLK_CTL);
6682 uint32_t linkrate;
6683
414355a7 6684 if (!(lcpll1 & LCPLL_PLL_ENABLE))
1652d19e 6685 return 24000; /* 24MHz is the cd freq with NSSC ref */
1652d19e
VS
6686
6687 if ((cdctl & CDCLK_FREQ_SEL_MASK) == CDCLK_FREQ_540)
6688 return 540000;
6689
6690 linkrate = (I915_READ(DPLL_CTRL1) &
71cd8423 6691 DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0)) >> 1;
1652d19e 6692
71cd8423
DL
6693 if (linkrate == DPLL_CTRL1_LINK_RATE_2160 ||
6694 linkrate == DPLL_CTRL1_LINK_RATE_1080) {
1652d19e
VS
6695 /* vco 8640 */
6696 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6697 case CDCLK_FREQ_450_432:
6698 return 432000;
6699 case CDCLK_FREQ_337_308:
6700 return 308570;
6701 case CDCLK_FREQ_675_617:
6702 return 617140;
6703 default:
6704 WARN(1, "Unknown cd freq selection\n");
6705 }
6706 } else {
6707 /* vco 8100 */
6708 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6709 case CDCLK_FREQ_450_432:
6710 return 450000;
6711 case CDCLK_FREQ_337_308:
6712 return 337500;
6713 case CDCLK_FREQ_675_617:
6714 return 675000;
6715 default:
6716 WARN(1, "Unknown cd freq selection\n");
6717 }
6718 }
6719
6720 /* error case, do as if DPLL0 isn't enabled */
6721 return 24000;
6722}
6723
acd3f3d3
BP
6724static int broxton_get_display_clock_speed(struct drm_device *dev)
6725{
6726 struct drm_i915_private *dev_priv = to_i915(dev);
6727 uint32_t cdctl = I915_READ(CDCLK_CTL);
6728 uint32_t pll_ratio = I915_READ(BXT_DE_PLL_CTL) & BXT_DE_PLL_RATIO_MASK;
6729 uint32_t pll_enab = I915_READ(BXT_DE_PLL_ENABLE);
6730 int cdclk;
6731
6732 if (!(pll_enab & BXT_DE_PLL_PLL_ENABLE))
6733 return 19200;
6734
6735 cdclk = 19200 * pll_ratio / 2;
6736
6737 switch (cdctl & BXT_CDCLK_CD2X_DIV_SEL_MASK) {
6738 case BXT_CDCLK_CD2X_DIV_SEL_1:
6739 return cdclk; /* 576MHz or 624MHz */
6740 case BXT_CDCLK_CD2X_DIV_SEL_1_5:
6741 return cdclk * 2 / 3; /* 384MHz */
6742 case BXT_CDCLK_CD2X_DIV_SEL_2:
6743 return cdclk / 2; /* 288MHz */
6744 case BXT_CDCLK_CD2X_DIV_SEL_4:
6745 return cdclk / 4; /* 144MHz */
6746 }
6747
6748 /* error case, do as if DE PLL isn't enabled */
6749 return 19200;
6750}
6751
1652d19e
VS
6752static int broadwell_get_display_clock_speed(struct drm_device *dev)
6753{
6754 struct drm_i915_private *dev_priv = dev->dev_private;
6755 uint32_t lcpll = I915_READ(LCPLL_CTL);
6756 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6757
6758 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6759 return 800000;
6760 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6761 return 450000;
6762 else if (freq == LCPLL_CLK_FREQ_450)
6763 return 450000;
6764 else if (freq == LCPLL_CLK_FREQ_54O_BDW)
6765 return 540000;
6766 else if (freq == LCPLL_CLK_FREQ_337_5_BDW)
6767 return 337500;
6768 else
6769 return 675000;
6770}
6771
6772static int haswell_get_display_clock_speed(struct drm_device *dev)
6773{
6774 struct drm_i915_private *dev_priv = dev->dev_private;
6775 uint32_t lcpll = I915_READ(LCPLL_CTL);
6776 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6777
6778 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6779 return 800000;
6780 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6781 return 450000;
6782 else if (freq == LCPLL_CLK_FREQ_450)
6783 return 450000;
6784 else if (IS_HSW_ULT(dev))
6785 return 337500;
6786 else
6787 return 540000;
79e53945
JB
6788}
6789
25eb05fc
JB
6790static int valleyview_get_display_clock_speed(struct drm_device *dev)
6791{
bfa7df01
VS
6792 return vlv_get_cck_clock_hpll(to_i915(dev), "cdclk",
6793 CCK_DISPLAY_CLOCK_CONTROL);
25eb05fc
JB
6794}
6795
b37a6434
VS
6796static int ilk_get_display_clock_speed(struct drm_device *dev)
6797{
6798 return 450000;
6799}
6800
e70236a8
JB
6801static int i945_get_display_clock_speed(struct drm_device *dev)
6802{
6803 return 400000;
6804}
79e53945 6805
e70236a8 6806static int i915_get_display_clock_speed(struct drm_device *dev)
79e53945 6807{
e907f170 6808 return 333333;
e70236a8 6809}
79e53945 6810
e70236a8
JB
6811static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
6812{
6813 return 200000;
6814}
79e53945 6815
257a7ffc
DV
6816static int pnv_get_display_clock_speed(struct drm_device *dev)
6817{
6818 u16 gcfgc = 0;
6819
6820 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6821
6822 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6823 case GC_DISPLAY_CLOCK_267_MHZ_PNV:
e907f170 6824 return 266667;
257a7ffc 6825 case GC_DISPLAY_CLOCK_333_MHZ_PNV:
e907f170 6826 return 333333;
257a7ffc 6827 case GC_DISPLAY_CLOCK_444_MHZ_PNV:
e907f170 6828 return 444444;
257a7ffc
DV
6829 case GC_DISPLAY_CLOCK_200_MHZ_PNV:
6830 return 200000;
6831 default:
6832 DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
6833 case GC_DISPLAY_CLOCK_133_MHZ_PNV:
e907f170 6834 return 133333;
257a7ffc 6835 case GC_DISPLAY_CLOCK_167_MHZ_PNV:
e907f170 6836 return 166667;
257a7ffc
DV
6837 }
6838}
6839
e70236a8
JB
6840static int i915gm_get_display_clock_speed(struct drm_device *dev)
6841{
6842 u16 gcfgc = 0;
79e53945 6843
e70236a8
JB
6844 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6845
6846 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
e907f170 6847 return 133333;
e70236a8
JB
6848 else {
6849 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6850 case GC_DISPLAY_CLOCK_333_MHZ:
e907f170 6851 return 333333;
e70236a8
JB
6852 default:
6853 case GC_DISPLAY_CLOCK_190_200_MHZ:
6854 return 190000;
79e53945 6855 }
e70236a8
JB
6856 }
6857}
6858
6859static int i865_get_display_clock_speed(struct drm_device *dev)
6860{
e907f170 6861 return 266667;
e70236a8
JB
6862}
6863
1b1d2716 6864static int i85x_get_display_clock_speed(struct drm_device *dev)
e70236a8
JB
6865{
6866 u16 hpllcc = 0;
1b1d2716 6867
65cd2b3f
VS
6868 /*
6869 * 852GM/852GMV only supports 133 MHz and the HPLLCC
6870 * encoding is different :(
6871 * FIXME is this the right way to detect 852GM/852GMV?
6872 */
6873 if (dev->pdev->revision == 0x1)
6874 return 133333;
6875
1b1d2716
VS
6876 pci_bus_read_config_word(dev->pdev->bus,
6877 PCI_DEVFN(0, 3), HPLLCC, &hpllcc);
6878
e70236a8
JB
6879 /* Assume that the hardware is in the high speed state. This
6880 * should be the default.
6881 */
6882 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
6883 case GC_CLOCK_133_200:
1b1d2716 6884 case GC_CLOCK_133_200_2:
e70236a8
JB
6885 case GC_CLOCK_100_200:
6886 return 200000;
6887 case GC_CLOCK_166_250:
6888 return 250000;
6889 case GC_CLOCK_100_133:
e907f170 6890 return 133333;
1b1d2716
VS
6891 case GC_CLOCK_133_266:
6892 case GC_CLOCK_133_266_2:
6893 case GC_CLOCK_166_266:
6894 return 266667;
e70236a8 6895 }
79e53945 6896
e70236a8
JB
6897 /* Shouldn't happen */
6898 return 0;
6899}
79e53945 6900
e70236a8
JB
6901static int i830_get_display_clock_speed(struct drm_device *dev)
6902{
e907f170 6903 return 133333;
79e53945
JB
6904}
6905
34edce2f
VS
6906static unsigned int intel_hpll_vco(struct drm_device *dev)
6907{
6908 struct drm_i915_private *dev_priv = dev->dev_private;
6909 static const unsigned int blb_vco[8] = {
6910 [0] = 3200000,
6911 [1] = 4000000,
6912 [2] = 5333333,
6913 [3] = 4800000,
6914 [4] = 6400000,
6915 };
6916 static const unsigned int pnv_vco[8] = {
6917 [0] = 3200000,
6918 [1] = 4000000,
6919 [2] = 5333333,
6920 [3] = 4800000,
6921 [4] = 2666667,
6922 };
6923 static const unsigned int cl_vco[8] = {
6924 [0] = 3200000,
6925 [1] = 4000000,
6926 [2] = 5333333,
6927 [3] = 6400000,
6928 [4] = 3333333,
6929 [5] = 3566667,
6930 [6] = 4266667,
6931 };
6932 static const unsigned int elk_vco[8] = {
6933 [0] = 3200000,
6934 [1] = 4000000,
6935 [2] = 5333333,
6936 [3] = 4800000,
6937 };
6938 static const unsigned int ctg_vco[8] = {
6939 [0] = 3200000,
6940 [1] = 4000000,
6941 [2] = 5333333,
6942 [3] = 6400000,
6943 [4] = 2666667,
6944 [5] = 4266667,
6945 };
6946 const unsigned int *vco_table;
6947 unsigned int vco;
6948 uint8_t tmp = 0;
6949
6950 /* FIXME other chipsets? */
6951 if (IS_GM45(dev))
6952 vco_table = ctg_vco;
6953 else if (IS_G4X(dev))
6954 vco_table = elk_vco;
6955 else if (IS_CRESTLINE(dev))
6956 vco_table = cl_vco;
6957 else if (IS_PINEVIEW(dev))
6958 vco_table = pnv_vco;
6959 else if (IS_G33(dev))
6960 vco_table = blb_vco;
6961 else
6962 return 0;
6963
6964 tmp = I915_READ(IS_MOBILE(dev) ? HPLLVCO_MOBILE : HPLLVCO);
6965
6966 vco = vco_table[tmp & 0x7];
6967 if (vco == 0)
6968 DRM_ERROR("Bad HPLL VCO (HPLLVCO=0x%02x)\n", tmp);
6969 else
6970 DRM_DEBUG_KMS("HPLL VCO %u kHz\n", vco);
6971
6972 return vco;
6973}
6974
6975static int gm45_get_display_clock_speed(struct drm_device *dev)
6976{
6977 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
6978 uint16_t tmp = 0;
6979
6980 pci_read_config_word(dev->pdev, GCFGC, &tmp);
6981
6982 cdclk_sel = (tmp >> 12) & 0x1;
6983
6984 switch (vco) {
6985 case 2666667:
6986 case 4000000:
6987 case 5333333:
6988 return cdclk_sel ? 333333 : 222222;
6989 case 3200000:
6990 return cdclk_sel ? 320000 : 228571;
6991 default:
6992 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u, CFGC=0x%04x\n", vco, tmp);
6993 return 222222;
6994 }
6995}
6996
6997static int i965gm_get_display_clock_speed(struct drm_device *dev)
6998{
6999 static const uint8_t div_3200[] = { 16, 10, 8 };
7000 static const uint8_t div_4000[] = { 20, 12, 10 };
7001 static const uint8_t div_5333[] = { 24, 16, 14 };
7002 const uint8_t *div_table;
7003 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
7004 uint16_t tmp = 0;
7005
7006 pci_read_config_word(dev->pdev, GCFGC, &tmp);
7007
7008 cdclk_sel = ((tmp >> 8) & 0x1f) - 1;
7009
7010 if (cdclk_sel >= ARRAY_SIZE(div_3200))
7011 goto fail;
7012
7013 switch (vco) {
7014 case 3200000:
7015 div_table = div_3200;
7016 break;
7017 case 4000000:
7018 div_table = div_4000;
7019 break;
7020 case 5333333:
7021 div_table = div_5333;
7022 break;
7023 default:
7024 goto fail;
7025 }
7026
7027 return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
7028
caf4e252 7029fail:
34edce2f
VS
7030 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%04x\n", vco, tmp);
7031 return 200000;
7032}
7033
7034static int g33_get_display_clock_speed(struct drm_device *dev)
7035{
7036 static const uint8_t div_3200[] = { 12, 10, 8, 7, 5, 16 };
7037 static const uint8_t div_4000[] = { 14, 12, 10, 8, 6, 20 };
7038 static const uint8_t div_4800[] = { 20, 14, 12, 10, 8, 24 };
7039 static const uint8_t div_5333[] = { 20, 16, 12, 12, 8, 28 };
7040 const uint8_t *div_table;
7041 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
7042 uint16_t tmp = 0;
7043
7044 pci_read_config_word(dev->pdev, GCFGC, &tmp);
7045
7046 cdclk_sel = (tmp >> 4) & 0x7;
7047
7048 if (cdclk_sel >= ARRAY_SIZE(div_3200))
7049 goto fail;
7050
7051 switch (vco) {
7052 case 3200000:
7053 div_table = div_3200;
7054 break;
7055 case 4000000:
7056 div_table = div_4000;
7057 break;
7058 case 4800000:
7059 div_table = div_4800;
7060 break;
7061 case 5333333:
7062 div_table = div_5333;
7063 break;
7064 default:
7065 goto fail;
7066 }
7067
7068 return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
7069
caf4e252 7070fail:
34edce2f
VS
7071 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%08x\n", vco, tmp);
7072 return 190476;
7073}
7074
2c07245f 7075static void
a65851af 7076intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
2c07245f 7077{
a65851af
VS
7078 while (*num > DATA_LINK_M_N_MASK ||
7079 *den > DATA_LINK_M_N_MASK) {
2c07245f
ZW
7080 *num >>= 1;
7081 *den >>= 1;
7082 }
7083}
7084
a65851af
VS
7085static void compute_m_n(unsigned int m, unsigned int n,
7086 uint32_t *ret_m, uint32_t *ret_n)
7087{
7088 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
7089 *ret_m = div_u64((uint64_t) m * *ret_n, n);
7090 intel_reduce_m_n_ratio(ret_m, ret_n);
7091}
7092
e69d0bc1
DV
7093void
7094intel_link_compute_m_n(int bits_per_pixel, int nlanes,
7095 int pixel_clock, int link_clock,
7096 struct intel_link_m_n *m_n)
2c07245f 7097{
e69d0bc1 7098 m_n->tu = 64;
a65851af
VS
7099
7100 compute_m_n(bits_per_pixel * pixel_clock,
7101 link_clock * nlanes * 8,
7102 &m_n->gmch_m, &m_n->gmch_n);
7103
7104 compute_m_n(pixel_clock, link_clock,
7105 &m_n->link_m, &m_n->link_n);
2c07245f
ZW
7106}
7107
a7615030
CW
7108static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
7109{
d330a953
JN
7110 if (i915.panel_use_ssc >= 0)
7111 return i915.panel_use_ssc != 0;
41aa3448 7112 return dev_priv->vbt.lvds_use_ssc
435793df 7113 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
a7615030
CW
7114}
7115
a93e255f
ACO
7116static int i9xx_get_refclk(const struct intel_crtc_state *crtc_state,
7117 int num_connectors)
c65d77d8 7118{
a93e255f 7119 struct drm_device *dev = crtc_state->base.crtc->dev;
c65d77d8
JB
7120 struct drm_i915_private *dev_priv = dev->dev_private;
7121 int refclk;
7122
a93e255f
ACO
7123 WARN_ON(!crtc_state->base.state);
7124
666a4537 7125 if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev) || IS_BROXTON(dev)) {
9a0ea498 7126 refclk = 100000;
a93e255f 7127 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
c65d77d8 7128 intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
e91e941b
VS
7129 refclk = dev_priv->vbt.lvds_ssc_freq;
7130 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
c65d77d8
JB
7131 } else if (!IS_GEN2(dev)) {
7132 refclk = 96000;
7133 } else {
7134 refclk = 48000;
7135 }
7136
7137 return refclk;
7138}
7139
7429e9d4 7140static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
c65d77d8 7141{
7df00d7a 7142 return (1 << dpll->n) << 16 | dpll->m2;
7429e9d4 7143}
f47709a9 7144
7429e9d4
DV
7145static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
7146{
7147 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
c65d77d8
JB
7148}
7149
f47709a9 7150static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
190f68c5 7151 struct intel_crtc_state *crtc_state,
a7516a05
JB
7152 intel_clock_t *reduced_clock)
7153{
f47709a9 7154 struct drm_device *dev = crtc->base.dev;
a7516a05
JB
7155 u32 fp, fp2 = 0;
7156
7157 if (IS_PINEVIEW(dev)) {
190f68c5 7158 fp = pnv_dpll_compute_fp(&crtc_state->dpll);
a7516a05 7159 if (reduced_clock)
7429e9d4 7160 fp2 = pnv_dpll_compute_fp(reduced_clock);
a7516a05 7161 } else {
190f68c5 7162 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
a7516a05 7163 if (reduced_clock)
7429e9d4 7164 fp2 = i9xx_dpll_compute_fp(reduced_clock);
a7516a05
JB
7165 }
7166
190f68c5 7167 crtc_state->dpll_hw_state.fp0 = fp;
a7516a05 7168
f47709a9 7169 crtc->lowfreq_avail = false;
a93e255f 7170 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
ab585dea 7171 reduced_clock) {
190f68c5 7172 crtc_state->dpll_hw_state.fp1 = fp2;
f47709a9 7173 crtc->lowfreq_avail = true;
a7516a05 7174 } else {
190f68c5 7175 crtc_state->dpll_hw_state.fp1 = fp;
a7516a05
JB
7176 }
7177}
7178
5e69f97f
CML
7179static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
7180 pipe)
89b667f8
JB
7181{
7182 u32 reg_val;
7183
7184 /*
7185 * PLLB opamp always calibrates to max value of 0x3f, force enable it
7186 * and set it to a reasonable value instead.
7187 */
ab3c759a 7188 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
89b667f8
JB
7189 reg_val &= 0xffffff00;
7190 reg_val |= 0x00000030;
ab3c759a 7191 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
89b667f8 7192
ab3c759a 7193 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
89b667f8
JB
7194 reg_val &= 0x8cffffff;
7195 reg_val = 0x8c000000;
ab3c759a 7196 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
89b667f8 7197
ab3c759a 7198 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
89b667f8 7199 reg_val &= 0xffffff00;
ab3c759a 7200 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
89b667f8 7201
ab3c759a 7202 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
89b667f8
JB
7203 reg_val &= 0x00ffffff;
7204 reg_val |= 0xb0000000;
ab3c759a 7205 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
89b667f8
JB
7206}
7207
b551842d
DV
7208static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
7209 struct intel_link_m_n *m_n)
7210{
7211 struct drm_device *dev = crtc->base.dev;
7212 struct drm_i915_private *dev_priv = dev->dev_private;
7213 int pipe = crtc->pipe;
7214
e3b95f1e
DV
7215 I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7216 I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
7217 I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
7218 I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
b551842d
DV
7219}
7220
7221static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
f769cd24
VK
7222 struct intel_link_m_n *m_n,
7223 struct intel_link_m_n *m2_n2)
b551842d
DV
7224{
7225 struct drm_device *dev = crtc->base.dev;
7226 struct drm_i915_private *dev_priv = dev->dev_private;
7227 int pipe = crtc->pipe;
6e3c9717 7228 enum transcoder transcoder = crtc->config->cpu_transcoder;
b551842d
DV
7229
7230 if (INTEL_INFO(dev)->gen >= 5) {
7231 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
7232 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
7233 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
7234 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
f769cd24
VK
7235 /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
7236 * for gen < 8) and if DRRS is supported (to make sure the
7237 * registers are not unnecessarily accessed).
7238 */
44395bfe 7239 if (m2_n2 && (IS_CHERRYVIEW(dev) || INTEL_INFO(dev)->gen < 8) &&
6e3c9717 7240 crtc->config->has_drrs) {
f769cd24
VK
7241 I915_WRITE(PIPE_DATA_M2(transcoder),
7242 TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
7243 I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
7244 I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
7245 I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
7246 }
b551842d 7247 } else {
e3b95f1e
DV
7248 I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7249 I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
7250 I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
7251 I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
b551842d
DV
7252 }
7253}
7254
fe3cd48d 7255void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
03afc4a2 7256{
fe3cd48d
R
7257 struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
7258
7259 if (m_n == M1_N1) {
7260 dp_m_n = &crtc->config->dp_m_n;
7261 dp_m2_n2 = &crtc->config->dp_m2_n2;
7262 } else if (m_n == M2_N2) {
7263
7264 /*
7265 * M2_N2 registers are not supported. Hence m2_n2 divider value
7266 * needs to be programmed into M1_N1.
7267 */
7268 dp_m_n = &crtc->config->dp_m2_n2;
7269 } else {
7270 DRM_ERROR("Unsupported divider value\n");
7271 return;
7272 }
7273
6e3c9717
ACO
7274 if (crtc->config->has_pch_encoder)
7275 intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
03afc4a2 7276 else
fe3cd48d 7277 intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
03afc4a2
DV
7278}
7279
251ac862
DV
7280static void vlv_compute_dpll(struct intel_crtc *crtc,
7281 struct intel_crtc_state *pipe_config)
bdd4b6a6
DV
7282{
7283 u32 dpll, dpll_md;
7284
7285 /*
7286 * Enable DPIO clock input. We should never disable the reference
7287 * clock for pipe B, since VGA hotplug / manual detection depends
7288 * on it.
7289 */
60bfe44f
VS
7290 dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REF_CLK_ENABLE_VLV |
7291 DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_REF_CLK_VLV;
bdd4b6a6
DV
7292 /* We should never disable this, set it here for state tracking */
7293 if (crtc->pipe == PIPE_B)
7294 dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
7295 dpll |= DPLL_VCO_ENABLE;
d288f65f 7296 pipe_config->dpll_hw_state.dpll = dpll;
bdd4b6a6 7297
d288f65f 7298 dpll_md = (pipe_config->pixel_multiplier - 1)
bdd4b6a6 7299 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
d288f65f 7300 pipe_config->dpll_hw_state.dpll_md = dpll_md;
bdd4b6a6
DV
7301}
7302
d288f65f 7303static void vlv_prepare_pll(struct intel_crtc *crtc,
5cec258b 7304 const struct intel_crtc_state *pipe_config)
a0c4da24 7305{
f47709a9 7306 struct drm_device *dev = crtc->base.dev;
a0c4da24 7307 struct drm_i915_private *dev_priv = dev->dev_private;
f47709a9 7308 int pipe = crtc->pipe;
bdd4b6a6 7309 u32 mdiv;
a0c4da24 7310 u32 bestn, bestm1, bestm2, bestp1, bestp2;
bdd4b6a6 7311 u32 coreclk, reg_val;
a0c4da24 7312
a580516d 7313 mutex_lock(&dev_priv->sb_lock);
09153000 7314
d288f65f
VS
7315 bestn = pipe_config->dpll.n;
7316 bestm1 = pipe_config->dpll.m1;
7317 bestm2 = pipe_config->dpll.m2;
7318 bestp1 = pipe_config->dpll.p1;
7319 bestp2 = pipe_config->dpll.p2;
a0c4da24 7320
89b667f8
JB
7321 /* See eDP HDMI DPIO driver vbios notes doc */
7322
7323 /* PLL B needs special handling */
bdd4b6a6 7324 if (pipe == PIPE_B)
5e69f97f 7325 vlv_pllb_recal_opamp(dev_priv, pipe);
89b667f8
JB
7326
7327 /* Set up Tx target for periodic Rcomp update */
ab3c759a 7328 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
89b667f8
JB
7329
7330 /* Disable target IRef on PLL */
ab3c759a 7331 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
89b667f8 7332 reg_val &= 0x00ffffff;
ab3c759a 7333 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
89b667f8
JB
7334
7335 /* Disable fast lock */
ab3c759a 7336 vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
89b667f8
JB
7337
7338 /* Set idtafcrecal before PLL is enabled */
a0c4da24
JB
7339 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
7340 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
7341 mdiv |= ((bestn << DPIO_N_SHIFT));
a0c4da24 7342 mdiv |= (1 << DPIO_K_SHIFT);
7df5080b
JB
7343
7344 /*
7345 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
7346 * but we don't support that).
7347 * Note: don't use the DAC post divider as it seems unstable.
7348 */
7349 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
ab3c759a 7350 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
a0c4da24 7351
a0c4da24 7352 mdiv |= DPIO_ENABLE_CALIBRATION;
ab3c759a 7353 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
a0c4da24 7354
89b667f8 7355 /* Set HBR and RBR LPF coefficients */
d288f65f 7356 if (pipe_config->port_clock == 162000 ||
409ee761
ACO
7357 intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG) ||
7358 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
ab3c759a 7359 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
885b0120 7360 0x009f0003);
89b667f8 7361 else
ab3c759a 7362 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
89b667f8
JB
7363 0x00d0000f);
7364
681a8504 7365 if (pipe_config->has_dp_encoder) {
89b667f8 7366 /* Use SSC source */
bdd4b6a6 7367 if (pipe == PIPE_A)
ab3c759a 7368 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
7369 0x0df40000);
7370 else
ab3c759a 7371 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
7372 0x0df70000);
7373 } else { /* HDMI or VGA */
7374 /* Use bend source */
bdd4b6a6 7375 if (pipe == PIPE_A)
ab3c759a 7376 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
7377 0x0df70000);
7378 else
ab3c759a 7379 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
7380 0x0df40000);
7381 }
a0c4da24 7382
ab3c759a 7383 coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
89b667f8 7384 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
409ee761
ACO
7385 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
7386 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
89b667f8 7387 coreclk |= 0x01000000;
ab3c759a 7388 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
a0c4da24 7389
ab3c759a 7390 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
a580516d 7391 mutex_unlock(&dev_priv->sb_lock);
a0c4da24
JB
7392}
7393
251ac862
DV
7394static void chv_compute_dpll(struct intel_crtc *crtc,
7395 struct intel_crtc_state *pipe_config)
1ae0d137 7396{
60bfe44f
VS
7397 pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLK_CHV |
7398 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS |
1ae0d137
VS
7399 DPLL_VCO_ENABLE;
7400 if (crtc->pipe != PIPE_A)
d288f65f 7401 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
1ae0d137 7402
d288f65f
VS
7403 pipe_config->dpll_hw_state.dpll_md =
7404 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
1ae0d137
VS
7405}
7406
d288f65f 7407static void chv_prepare_pll(struct intel_crtc *crtc,
5cec258b 7408 const struct intel_crtc_state *pipe_config)
9d556c99
CML
7409{
7410 struct drm_device *dev = crtc->base.dev;
7411 struct drm_i915_private *dev_priv = dev->dev_private;
7412 int pipe = crtc->pipe;
f0f59a00 7413 i915_reg_t dpll_reg = DPLL(crtc->pipe);
9d556c99 7414 enum dpio_channel port = vlv_pipe_to_channel(pipe);
9cbe40c1 7415 u32 loopfilter, tribuf_calcntr;
9d556c99 7416 u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
a945ce7e 7417 u32 dpio_val;
9cbe40c1 7418 int vco;
9d556c99 7419
d288f65f
VS
7420 bestn = pipe_config->dpll.n;
7421 bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
7422 bestm1 = pipe_config->dpll.m1;
7423 bestm2 = pipe_config->dpll.m2 >> 22;
7424 bestp1 = pipe_config->dpll.p1;
7425 bestp2 = pipe_config->dpll.p2;
9cbe40c1 7426 vco = pipe_config->dpll.vco;
a945ce7e 7427 dpio_val = 0;
9cbe40c1 7428 loopfilter = 0;
9d556c99
CML
7429
7430 /*
7431 * Enable Refclk and SSC
7432 */
a11b0703 7433 I915_WRITE(dpll_reg,
d288f65f 7434 pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
a11b0703 7435
a580516d 7436 mutex_lock(&dev_priv->sb_lock);
9d556c99 7437
9d556c99
CML
7438 /* p1 and p2 divider */
7439 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
7440 5 << DPIO_CHV_S1_DIV_SHIFT |
7441 bestp1 << DPIO_CHV_P1_DIV_SHIFT |
7442 bestp2 << DPIO_CHV_P2_DIV_SHIFT |
7443 1 << DPIO_CHV_K_DIV_SHIFT);
7444
7445 /* Feedback post-divider - m2 */
7446 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
7447
7448 /* Feedback refclk divider - n and m1 */
7449 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
7450 DPIO_CHV_M1_DIV_BY_2 |
7451 1 << DPIO_CHV_N_DIV_SHIFT);
7452
7453 /* M2 fraction division */
25a25dfc 7454 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
9d556c99
CML
7455
7456 /* M2 fraction division enable */
a945ce7e
VP
7457 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
7458 dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
7459 dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
7460 if (bestm2_frac)
7461 dpio_val |= DPIO_CHV_FRAC_DIV_EN;
7462 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
9d556c99 7463
de3a0fde
VP
7464 /* Program digital lock detect threshold */
7465 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
7466 dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
7467 DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
7468 dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
7469 if (!bestm2_frac)
7470 dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
7471 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
7472
9d556c99 7473 /* Loop filter */
9cbe40c1
VP
7474 if (vco == 5400000) {
7475 loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
7476 loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
7477 loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
7478 tribuf_calcntr = 0x9;
7479 } else if (vco <= 6200000) {
7480 loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
7481 loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
7482 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7483 tribuf_calcntr = 0x9;
7484 } else if (vco <= 6480000) {
7485 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7486 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7487 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7488 tribuf_calcntr = 0x8;
7489 } else {
7490 /* Not supported. Apply the same limits as in the max case */
7491 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7492 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7493 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7494 tribuf_calcntr = 0;
7495 }
9d556c99
CML
7496 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
7497
968040b2 7498 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
9cbe40c1
VP
7499 dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
7500 dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
7501 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
7502
9d556c99
CML
7503 /* AFC Recal */
7504 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
7505 vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
7506 DPIO_AFC_RECAL);
7507
a580516d 7508 mutex_unlock(&dev_priv->sb_lock);
9d556c99
CML
7509}
7510
d288f65f
VS
7511/**
7512 * vlv_force_pll_on - forcibly enable just the PLL
7513 * @dev_priv: i915 private structure
7514 * @pipe: pipe PLL to enable
7515 * @dpll: PLL configuration
7516 *
7517 * Enable the PLL for @pipe using the supplied @dpll config. To be used
7518 * in cases where we need the PLL enabled even when @pipe is not going to
7519 * be enabled.
7520 */
3f36b937
TU
7521int vlv_force_pll_on(struct drm_device *dev, enum pipe pipe,
7522 const struct dpll *dpll)
d288f65f
VS
7523{
7524 struct intel_crtc *crtc =
7525 to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
3f36b937
TU
7526 struct intel_crtc_state *pipe_config;
7527
7528 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
7529 if (!pipe_config)
7530 return -ENOMEM;
7531
7532 pipe_config->base.crtc = &crtc->base;
7533 pipe_config->pixel_multiplier = 1;
7534 pipe_config->dpll = *dpll;
d288f65f
VS
7535
7536 if (IS_CHERRYVIEW(dev)) {
3f36b937
TU
7537 chv_compute_dpll(crtc, pipe_config);
7538 chv_prepare_pll(crtc, pipe_config);
7539 chv_enable_pll(crtc, pipe_config);
d288f65f 7540 } else {
3f36b937
TU
7541 vlv_compute_dpll(crtc, pipe_config);
7542 vlv_prepare_pll(crtc, pipe_config);
7543 vlv_enable_pll(crtc, pipe_config);
d288f65f 7544 }
3f36b937
TU
7545
7546 kfree(pipe_config);
7547
7548 return 0;
d288f65f
VS
7549}
7550
7551/**
7552 * vlv_force_pll_off - forcibly disable just the PLL
7553 * @dev_priv: i915 private structure
7554 * @pipe: pipe PLL to disable
7555 *
7556 * Disable the PLL for @pipe. To be used in cases where we need
7557 * the PLL enabled even when @pipe is not going to be enabled.
7558 */
7559void vlv_force_pll_off(struct drm_device *dev, enum pipe pipe)
7560{
7561 if (IS_CHERRYVIEW(dev))
7562 chv_disable_pll(to_i915(dev), pipe);
7563 else
7564 vlv_disable_pll(to_i915(dev), pipe);
7565}
7566
251ac862
DV
7567static void i9xx_compute_dpll(struct intel_crtc *crtc,
7568 struct intel_crtc_state *crtc_state,
7569 intel_clock_t *reduced_clock,
7570 int num_connectors)
eb1cbe48 7571{
f47709a9 7572 struct drm_device *dev = crtc->base.dev;
eb1cbe48 7573 struct drm_i915_private *dev_priv = dev->dev_private;
eb1cbe48
DV
7574 u32 dpll;
7575 bool is_sdvo;
190f68c5 7576 struct dpll *clock = &crtc_state->dpll;
eb1cbe48 7577
190f68c5 7578 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
2a8f64ca 7579
a93e255f
ACO
7580 is_sdvo = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO) ||
7581 intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI);
eb1cbe48
DV
7582
7583 dpll = DPLL_VGA_MODE_DIS;
7584
a93e255f 7585 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
eb1cbe48
DV
7586 dpll |= DPLLB_MODE_LVDS;
7587 else
7588 dpll |= DPLLB_MODE_DAC_SERIAL;
6cc5f341 7589
ef1b460d 7590 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
190f68c5 7591 dpll |= (crtc_state->pixel_multiplier - 1)
198a037f 7592 << SDVO_MULTIPLIER_SHIFT_HIRES;
eb1cbe48 7593 }
198a037f
DV
7594
7595 if (is_sdvo)
4a33e48d 7596 dpll |= DPLL_SDVO_HIGH_SPEED;
198a037f 7597
190f68c5 7598 if (crtc_state->has_dp_encoder)
4a33e48d 7599 dpll |= DPLL_SDVO_HIGH_SPEED;
eb1cbe48
DV
7600
7601 /* compute bitmask from p1 value */
7602 if (IS_PINEVIEW(dev))
7603 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
7604 else {
7605 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7606 if (IS_G4X(dev) && reduced_clock)
7607 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
7608 }
7609 switch (clock->p2) {
7610 case 5:
7611 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
7612 break;
7613 case 7:
7614 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
7615 break;
7616 case 10:
7617 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
7618 break;
7619 case 14:
7620 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
7621 break;
7622 }
7623 if (INTEL_INFO(dev)->gen >= 4)
7624 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
7625
190f68c5 7626 if (crtc_state->sdvo_tv_clock)
eb1cbe48 7627 dpll |= PLL_REF_INPUT_TVCLKINBC;
a93e255f 7628 else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
eb1cbe48
DV
7629 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
7630 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7631 else
7632 dpll |= PLL_REF_INPUT_DREFCLK;
7633
7634 dpll |= DPLL_VCO_ENABLE;
190f68c5 7635 crtc_state->dpll_hw_state.dpll = dpll;
8bcc2795 7636
eb1cbe48 7637 if (INTEL_INFO(dev)->gen >= 4) {
190f68c5 7638 u32 dpll_md = (crtc_state->pixel_multiplier - 1)
ef1b460d 7639 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
190f68c5 7640 crtc_state->dpll_hw_state.dpll_md = dpll_md;
eb1cbe48
DV
7641 }
7642}
7643
251ac862
DV
7644static void i8xx_compute_dpll(struct intel_crtc *crtc,
7645 struct intel_crtc_state *crtc_state,
7646 intel_clock_t *reduced_clock,
7647 int num_connectors)
eb1cbe48 7648{
f47709a9 7649 struct drm_device *dev = crtc->base.dev;
eb1cbe48 7650 struct drm_i915_private *dev_priv = dev->dev_private;
eb1cbe48 7651 u32 dpll;
190f68c5 7652 struct dpll *clock = &crtc_state->dpll;
eb1cbe48 7653
190f68c5 7654 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
2a8f64ca 7655
eb1cbe48
DV
7656 dpll = DPLL_VGA_MODE_DIS;
7657
a93e255f 7658 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
eb1cbe48
DV
7659 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7660 } else {
7661 if (clock->p1 == 2)
7662 dpll |= PLL_P1_DIVIDE_BY_TWO;
7663 else
7664 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7665 if (clock->p2 == 4)
7666 dpll |= PLL_P2_DIVIDE_BY_4;
7667 }
7668
a93e255f 7669 if (!IS_I830(dev) && intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
4a33e48d
DV
7670 dpll |= DPLL_DVO_2X_MODE;
7671
a93e255f 7672 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
eb1cbe48
DV
7673 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
7674 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7675 else
7676 dpll |= PLL_REF_INPUT_DREFCLK;
7677
7678 dpll |= DPLL_VCO_ENABLE;
190f68c5 7679 crtc_state->dpll_hw_state.dpll = dpll;
eb1cbe48
DV
7680}
7681
8a654f3b 7682static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
b0e77b9c
PZ
7683{
7684 struct drm_device *dev = intel_crtc->base.dev;
7685 struct drm_i915_private *dev_priv = dev->dev_private;
7686 enum pipe pipe = intel_crtc->pipe;
6e3c9717 7687 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
7c5f93b0 7688 const struct drm_display_mode *adjusted_mode = &intel_crtc->config->base.adjusted_mode;
1caea6e9
VS
7689 uint32_t crtc_vtotal, crtc_vblank_end;
7690 int vsyncshift = 0;
4d8a62ea
DV
7691
7692 /* We need to be careful not to changed the adjusted mode, for otherwise
7693 * the hw state checker will get angry at the mismatch. */
7694 crtc_vtotal = adjusted_mode->crtc_vtotal;
7695 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
b0e77b9c 7696
609aeaca 7697 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
b0e77b9c 7698 /* the chip adds 2 halflines automatically */
4d8a62ea
DV
7699 crtc_vtotal -= 1;
7700 crtc_vblank_end -= 1;
609aeaca 7701
409ee761 7702 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
609aeaca
VS
7703 vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
7704 else
7705 vsyncshift = adjusted_mode->crtc_hsync_start -
7706 adjusted_mode->crtc_htotal / 2;
1caea6e9
VS
7707 if (vsyncshift < 0)
7708 vsyncshift += adjusted_mode->crtc_htotal;
b0e77b9c
PZ
7709 }
7710
7711 if (INTEL_INFO(dev)->gen > 3)
fe2b8f9d 7712 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
b0e77b9c 7713
fe2b8f9d 7714 I915_WRITE(HTOTAL(cpu_transcoder),
b0e77b9c
PZ
7715 (adjusted_mode->crtc_hdisplay - 1) |
7716 ((adjusted_mode->crtc_htotal - 1) << 16));
fe2b8f9d 7717 I915_WRITE(HBLANK(cpu_transcoder),
b0e77b9c
PZ
7718 (adjusted_mode->crtc_hblank_start - 1) |
7719 ((adjusted_mode->crtc_hblank_end - 1) << 16));
fe2b8f9d 7720 I915_WRITE(HSYNC(cpu_transcoder),
b0e77b9c
PZ
7721 (adjusted_mode->crtc_hsync_start - 1) |
7722 ((adjusted_mode->crtc_hsync_end - 1) << 16));
7723
fe2b8f9d 7724 I915_WRITE(VTOTAL(cpu_transcoder),
b0e77b9c 7725 (adjusted_mode->crtc_vdisplay - 1) |
4d8a62ea 7726 ((crtc_vtotal - 1) << 16));
fe2b8f9d 7727 I915_WRITE(VBLANK(cpu_transcoder),
b0e77b9c 7728 (adjusted_mode->crtc_vblank_start - 1) |
4d8a62ea 7729 ((crtc_vblank_end - 1) << 16));
fe2b8f9d 7730 I915_WRITE(VSYNC(cpu_transcoder),
b0e77b9c
PZ
7731 (adjusted_mode->crtc_vsync_start - 1) |
7732 ((adjusted_mode->crtc_vsync_end - 1) << 16));
7733
b5e508d4
PZ
7734 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
7735 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
7736 * documented on the DDI_FUNC_CTL register description, EDP Input Select
7737 * bits. */
7738 if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
7739 (pipe == PIPE_B || pipe == PIPE_C))
7740 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
7741
bc58be60
JN
7742}
7743
7744static void intel_set_pipe_src_size(struct intel_crtc *intel_crtc)
7745{
7746 struct drm_device *dev = intel_crtc->base.dev;
7747 struct drm_i915_private *dev_priv = dev->dev_private;
7748 enum pipe pipe = intel_crtc->pipe;
7749
b0e77b9c
PZ
7750 /* pipesrc controls the size that is scaled from, which should
7751 * always be the user's requested size.
7752 */
7753 I915_WRITE(PIPESRC(pipe),
6e3c9717
ACO
7754 ((intel_crtc->config->pipe_src_w - 1) << 16) |
7755 (intel_crtc->config->pipe_src_h - 1));
b0e77b9c
PZ
7756}
7757
1bd1bd80 7758static void intel_get_pipe_timings(struct intel_crtc *crtc,
5cec258b 7759 struct intel_crtc_state *pipe_config)
1bd1bd80
DV
7760{
7761 struct drm_device *dev = crtc->base.dev;
7762 struct drm_i915_private *dev_priv = dev->dev_private;
7763 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
7764 uint32_t tmp;
7765
7766 tmp = I915_READ(HTOTAL(cpu_transcoder));
2d112de7
ACO
7767 pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
7768 pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 7769 tmp = I915_READ(HBLANK(cpu_transcoder));
2d112de7
ACO
7770 pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
7771 pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 7772 tmp = I915_READ(HSYNC(cpu_transcoder));
2d112de7
ACO
7773 pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
7774 pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80
DV
7775
7776 tmp = I915_READ(VTOTAL(cpu_transcoder));
2d112de7
ACO
7777 pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
7778 pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 7779 tmp = I915_READ(VBLANK(cpu_transcoder));
2d112de7
ACO
7780 pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
7781 pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 7782 tmp = I915_READ(VSYNC(cpu_transcoder));
2d112de7
ACO
7783 pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
7784 pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80
DV
7785
7786 if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
2d112de7
ACO
7787 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
7788 pipe_config->base.adjusted_mode.crtc_vtotal += 1;
7789 pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
1bd1bd80 7790 }
bc58be60
JN
7791}
7792
7793static void intel_get_pipe_src_size(struct intel_crtc *crtc,
7794 struct intel_crtc_state *pipe_config)
7795{
7796 struct drm_device *dev = crtc->base.dev;
7797 struct drm_i915_private *dev_priv = dev->dev_private;
7798 u32 tmp;
1bd1bd80
DV
7799
7800 tmp = I915_READ(PIPESRC(crtc->pipe));
37327abd
VS
7801 pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
7802 pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
7803
2d112de7
ACO
7804 pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
7805 pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
1bd1bd80
DV
7806}
7807
f6a83288 7808void intel_mode_from_pipe_config(struct drm_display_mode *mode,
5cec258b 7809 struct intel_crtc_state *pipe_config)
babea61d 7810{
2d112de7
ACO
7811 mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
7812 mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
7813 mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
7814 mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
babea61d 7815
2d112de7
ACO
7816 mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
7817 mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
7818 mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
7819 mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
babea61d 7820
2d112de7 7821 mode->flags = pipe_config->base.adjusted_mode.flags;
cd13f5ab 7822 mode->type = DRM_MODE_TYPE_DRIVER;
babea61d 7823
2d112de7
ACO
7824 mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
7825 mode->flags |= pipe_config->base.adjusted_mode.flags;
cd13f5ab
ML
7826
7827 mode->hsync = drm_mode_hsync(mode);
7828 mode->vrefresh = drm_mode_vrefresh(mode);
7829 drm_mode_set_name(mode);
babea61d
JB
7830}
7831
84b046f3
DV
7832static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
7833{
7834 struct drm_device *dev = intel_crtc->base.dev;
7835 struct drm_i915_private *dev_priv = dev->dev_private;
7836 uint32_t pipeconf;
7837
9f11a9e4 7838 pipeconf = 0;
84b046f3 7839
b6b5d049
VS
7840 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
7841 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
7842 pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
67c72a12 7843
6e3c9717 7844 if (intel_crtc->config->double_wide)
cf532bb2 7845 pipeconf |= PIPECONF_DOUBLE_WIDE;
84b046f3 7846
ff9ce46e 7847 /* only g4x and later have fancy bpc/dither controls */
666a4537 7848 if (IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
ff9ce46e 7849 /* Bspec claims that we can't use dithering for 30bpp pipes. */
6e3c9717 7850 if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
ff9ce46e 7851 pipeconf |= PIPECONF_DITHER_EN |
84b046f3 7852 PIPECONF_DITHER_TYPE_SP;
84b046f3 7853
6e3c9717 7854 switch (intel_crtc->config->pipe_bpp) {
ff9ce46e
DV
7855 case 18:
7856 pipeconf |= PIPECONF_6BPC;
7857 break;
7858 case 24:
7859 pipeconf |= PIPECONF_8BPC;
7860 break;
7861 case 30:
7862 pipeconf |= PIPECONF_10BPC;
7863 break;
7864 default:
7865 /* Case prevented by intel_choose_pipe_bpp_dither. */
7866 BUG();
84b046f3
DV
7867 }
7868 }
7869
7870 if (HAS_PIPE_CXSR(dev)) {
7871 if (intel_crtc->lowfreq_avail) {
7872 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
7873 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
7874 } else {
7875 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
84b046f3
DV
7876 }
7877 }
7878
6e3c9717 7879 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
efc2cfff 7880 if (INTEL_INFO(dev)->gen < 4 ||
409ee761 7881 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
efc2cfff
VS
7882 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
7883 else
7884 pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
7885 } else
84b046f3
DV
7886 pipeconf |= PIPECONF_PROGRESSIVE;
7887
666a4537
WB
7888 if ((IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) &&
7889 intel_crtc->config->limited_color_range)
9f11a9e4 7890 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
9c8e09b7 7891
84b046f3
DV
7892 I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
7893 POSTING_READ(PIPECONF(intel_crtc->pipe));
7894}
7895
190f68c5
ACO
7896static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
7897 struct intel_crtc_state *crtc_state)
79e53945 7898{
c7653199 7899 struct drm_device *dev = crtc->base.dev;
79e53945 7900 struct drm_i915_private *dev_priv = dev->dev_private;
c751ce4f 7901 int refclk, num_connectors = 0;
c329a4ec
DV
7902 intel_clock_t clock;
7903 bool ok;
d4906093 7904 const intel_limit_t *limit;
55bb9992 7905 struct drm_atomic_state *state = crtc_state->base.state;
da3ced29 7906 struct drm_connector *connector;
55bb9992
ACO
7907 struct drm_connector_state *connector_state;
7908 int i;
79e53945 7909
dd3cd74a
ACO
7910 memset(&crtc_state->dpll_hw_state, 0,
7911 sizeof(crtc_state->dpll_hw_state));
7912
a65347ba
JN
7913 if (crtc_state->has_dsi_encoder)
7914 return 0;
43565a06 7915
a65347ba
JN
7916 for_each_connector_in_state(state, connector, connector_state, i) {
7917 if (connector_state->crtc == &crtc->base)
7918 num_connectors++;
79e53945
JB
7919 }
7920
190f68c5 7921 if (!crtc_state->clock_set) {
a93e255f 7922 refclk = i9xx_get_refclk(crtc_state, num_connectors);
79e53945 7923
e9fd1c02
JN
7924 /*
7925 * Returns a set of divisors for the desired target clock with
7926 * the given refclk, or FALSE. The returned values represent
7927 * the clock equation: reflck * (5 * (m1 + 2) + (m2 + 2)) / (n +
7928 * 2) / p1 / p2.
7929 */
a93e255f
ACO
7930 limit = intel_limit(crtc_state, refclk);
7931 ok = dev_priv->display.find_dpll(limit, crtc_state,
190f68c5 7932 crtc_state->port_clock,
e9fd1c02 7933 refclk, NULL, &clock);
f2335330 7934 if (!ok) {
e9fd1c02
JN
7935 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7936 return -EINVAL;
7937 }
79e53945 7938
f2335330 7939 /* Compat-code for transition, will disappear. */
190f68c5
ACO
7940 crtc_state->dpll.n = clock.n;
7941 crtc_state->dpll.m1 = clock.m1;
7942 crtc_state->dpll.m2 = clock.m2;
7943 crtc_state->dpll.p1 = clock.p1;
7944 crtc_state->dpll.p2 = clock.p2;
f47709a9 7945 }
7026d4ac 7946
e9fd1c02 7947 if (IS_GEN2(dev)) {
c329a4ec 7948 i8xx_compute_dpll(crtc, crtc_state, NULL,
251ac862 7949 num_connectors);
9d556c99 7950 } else if (IS_CHERRYVIEW(dev)) {
251ac862 7951 chv_compute_dpll(crtc, crtc_state);
e9fd1c02 7952 } else if (IS_VALLEYVIEW(dev)) {
251ac862 7953 vlv_compute_dpll(crtc, crtc_state);
e9fd1c02 7954 } else {
c329a4ec 7955 i9xx_compute_dpll(crtc, crtc_state, NULL,
251ac862 7956 num_connectors);
e9fd1c02 7957 }
79e53945 7958
c8f7a0db 7959 return 0;
f564048e
EA
7960}
7961
2fa2fe9a 7962static void i9xx_get_pfit_config(struct intel_crtc *crtc,
5cec258b 7963 struct intel_crtc_state *pipe_config)
2fa2fe9a
DV
7964{
7965 struct drm_device *dev = crtc->base.dev;
7966 struct drm_i915_private *dev_priv = dev->dev_private;
7967 uint32_t tmp;
7968
dc9e7dec
VS
7969 if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
7970 return;
7971
2fa2fe9a 7972 tmp = I915_READ(PFIT_CONTROL);
06922821
DV
7973 if (!(tmp & PFIT_ENABLE))
7974 return;
2fa2fe9a 7975
06922821 7976 /* Check whether the pfit is attached to our pipe. */
2fa2fe9a
DV
7977 if (INTEL_INFO(dev)->gen < 4) {
7978 if (crtc->pipe != PIPE_B)
7979 return;
2fa2fe9a
DV
7980 } else {
7981 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
7982 return;
7983 }
7984
06922821 7985 pipe_config->gmch_pfit.control = tmp;
2fa2fe9a
DV
7986 pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
7987 if (INTEL_INFO(dev)->gen < 5)
7988 pipe_config->gmch_pfit.lvds_border_bits =
7989 I915_READ(LVDS) & LVDS_BORDER_ENABLE;
7990}
7991
acbec814 7992static void vlv_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 7993 struct intel_crtc_state *pipe_config)
acbec814
JB
7994{
7995 struct drm_device *dev = crtc->base.dev;
7996 struct drm_i915_private *dev_priv = dev->dev_private;
7997 int pipe = pipe_config->cpu_transcoder;
7998 intel_clock_t clock;
7999 u32 mdiv;
662c6ecb 8000 int refclk = 100000;
acbec814 8001
f573de5a
SK
8002 /* In case of MIPI DPLL will not even be used */
8003 if (!(pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE))
8004 return;
8005
a580516d 8006 mutex_lock(&dev_priv->sb_lock);
ab3c759a 8007 mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
a580516d 8008 mutex_unlock(&dev_priv->sb_lock);
acbec814
JB
8009
8010 clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
8011 clock.m2 = mdiv & DPIO_M2DIV_MASK;
8012 clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
8013 clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
8014 clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
8015
dccbea3b 8016 pipe_config->port_clock = vlv_calc_dpll_params(refclk, &clock);
acbec814
JB
8017}
8018
5724dbd1
DL
8019static void
8020i9xx_get_initial_plane_config(struct intel_crtc *crtc,
8021 struct intel_initial_plane_config *plane_config)
1ad292b5
JB
8022{
8023 struct drm_device *dev = crtc->base.dev;
8024 struct drm_i915_private *dev_priv = dev->dev_private;
8025 u32 val, base, offset;
8026 int pipe = crtc->pipe, plane = crtc->plane;
8027 int fourcc, pixel_format;
6761dd31 8028 unsigned int aligned_height;
b113d5ee 8029 struct drm_framebuffer *fb;
1b842c89 8030 struct intel_framebuffer *intel_fb;
1ad292b5 8031
42a7b088
DL
8032 val = I915_READ(DSPCNTR(plane));
8033 if (!(val & DISPLAY_PLANE_ENABLE))
8034 return;
8035
d9806c9f 8036 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
1b842c89 8037 if (!intel_fb) {
1ad292b5
JB
8038 DRM_DEBUG_KMS("failed to alloc fb\n");
8039 return;
8040 }
8041
1b842c89
DL
8042 fb = &intel_fb->base;
8043
18c5247e
DV
8044 if (INTEL_INFO(dev)->gen >= 4) {
8045 if (val & DISPPLANE_TILED) {
49af449b 8046 plane_config->tiling = I915_TILING_X;
18c5247e
DV
8047 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
8048 }
8049 }
1ad292b5
JB
8050
8051 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
b35d63fa 8052 fourcc = i9xx_format_to_fourcc(pixel_format);
b113d5ee
DL
8053 fb->pixel_format = fourcc;
8054 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
1ad292b5
JB
8055
8056 if (INTEL_INFO(dev)->gen >= 4) {
49af449b 8057 if (plane_config->tiling)
1ad292b5
JB
8058 offset = I915_READ(DSPTILEOFF(plane));
8059 else
8060 offset = I915_READ(DSPLINOFF(plane));
8061 base = I915_READ(DSPSURF(plane)) & 0xfffff000;
8062 } else {
8063 base = I915_READ(DSPADDR(plane));
8064 }
8065 plane_config->base = base;
8066
8067 val = I915_READ(PIPESRC(pipe));
b113d5ee
DL
8068 fb->width = ((val >> 16) & 0xfff) + 1;
8069 fb->height = ((val >> 0) & 0xfff) + 1;
1ad292b5
JB
8070
8071 val = I915_READ(DSPSTRIDE(pipe));
b113d5ee 8072 fb->pitches[0] = val & 0xffffffc0;
1ad292b5 8073
b113d5ee 8074 aligned_height = intel_fb_align_height(dev, fb->height,
091df6cb
DV
8075 fb->pixel_format,
8076 fb->modifier[0]);
1ad292b5 8077
f37b5c2b 8078 plane_config->size = fb->pitches[0] * aligned_height;
1ad292b5 8079
2844a921
DL
8080 DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
8081 pipe_name(pipe), plane, fb->width, fb->height,
8082 fb->bits_per_pixel, base, fb->pitches[0],
8083 plane_config->size);
1ad292b5 8084
2d14030b 8085 plane_config->fb = intel_fb;
1ad292b5
JB
8086}
8087
70b23a98 8088static void chv_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 8089 struct intel_crtc_state *pipe_config)
70b23a98
VS
8090{
8091 struct drm_device *dev = crtc->base.dev;
8092 struct drm_i915_private *dev_priv = dev->dev_private;
8093 int pipe = pipe_config->cpu_transcoder;
8094 enum dpio_channel port = vlv_pipe_to_channel(pipe);
8095 intel_clock_t clock;
0d7b6b11 8096 u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2, pll_dw3;
70b23a98
VS
8097 int refclk = 100000;
8098
a580516d 8099 mutex_lock(&dev_priv->sb_lock);
70b23a98
VS
8100 cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
8101 pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
8102 pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
8103 pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
0d7b6b11 8104 pll_dw3 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
a580516d 8105 mutex_unlock(&dev_priv->sb_lock);
70b23a98
VS
8106
8107 clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
0d7b6b11
ID
8108 clock.m2 = (pll_dw0 & 0xff) << 22;
8109 if (pll_dw3 & DPIO_CHV_FRAC_DIV_EN)
8110 clock.m2 |= pll_dw2 & 0x3fffff;
70b23a98
VS
8111 clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
8112 clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
8113 clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
8114
dccbea3b 8115 pipe_config->port_clock = chv_calc_dpll_params(refclk, &clock);
70b23a98
VS
8116}
8117
0e8ffe1b 8118static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
5cec258b 8119 struct intel_crtc_state *pipe_config)
0e8ffe1b
DV
8120{
8121 struct drm_device *dev = crtc->base.dev;
8122 struct drm_i915_private *dev_priv = dev->dev_private;
1729050e 8123 enum intel_display_power_domain power_domain;
0e8ffe1b 8124 uint32_t tmp;
1729050e 8125 bool ret;
0e8ffe1b 8126
1729050e
ID
8127 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
8128 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
b5482bd0
ID
8129 return false;
8130
e143a21c 8131 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
8106ddbd 8132 pipe_config->shared_dpll = NULL;
eccb140b 8133
1729050e
ID
8134 ret = false;
8135
0e8ffe1b
DV
8136 tmp = I915_READ(PIPECONF(crtc->pipe));
8137 if (!(tmp & PIPECONF_ENABLE))
1729050e 8138 goto out;
0e8ffe1b 8139
666a4537 8140 if (IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
42571aef
VS
8141 switch (tmp & PIPECONF_BPC_MASK) {
8142 case PIPECONF_6BPC:
8143 pipe_config->pipe_bpp = 18;
8144 break;
8145 case PIPECONF_8BPC:
8146 pipe_config->pipe_bpp = 24;
8147 break;
8148 case PIPECONF_10BPC:
8149 pipe_config->pipe_bpp = 30;
8150 break;
8151 default:
8152 break;
8153 }
8154 }
8155
666a4537
WB
8156 if ((IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) &&
8157 (tmp & PIPECONF_COLOR_RANGE_SELECT))
b5a9fa09
DV
8158 pipe_config->limited_color_range = true;
8159
282740f7
VS
8160 if (INTEL_INFO(dev)->gen < 4)
8161 pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
8162
1bd1bd80 8163 intel_get_pipe_timings(crtc, pipe_config);
bc58be60 8164 intel_get_pipe_src_size(crtc, pipe_config);
1bd1bd80 8165
2fa2fe9a
DV
8166 i9xx_get_pfit_config(crtc, pipe_config);
8167
6c49f241
DV
8168 if (INTEL_INFO(dev)->gen >= 4) {
8169 tmp = I915_READ(DPLL_MD(crtc->pipe));
8170 pipe_config->pixel_multiplier =
8171 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
8172 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
8bcc2795 8173 pipe_config->dpll_hw_state.dpll_md = tmp;
6c49f241
DV
8174 } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
8175 tmp = I915_READ(DPLL(crtc->pipe));
8176 pipe_config->pixel_multiplier =
8177 ((tmp & SDVO_MULTIPLIER_MASK)
8178 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
8179 } else {
8180 /* Note that on i915G/GM the pixel multiplier is in the sdvo
8181 * port and will be fixed up in the encoder->get_config
8182 * function. */
8183 pipe_config->pixel_multiplier = 1;
8184 }
8bcc2795 8185 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
666a4537 8186 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
1c4e0274
VS
8187 /*
8188 * DPLL_DVO_2X_MODE must be enabled for both DPLLs
8189 * on 830. Filter it out here so that we don't
8190 * report errors due to that.
8191 */
8192 if (IS_I830(dev))
8193 pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
8194
8bcc2795
DV
8195 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
8196 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
165e901c
VS
8197 } else {
8198 /* Mask out read-only status bits. */
8199 pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
8200 DPLL_PORTC_READY_MASK |
8201 DPLL_PORTB_READY_MASK);
8bcc2795 8202 }
6c49f241 8203
70b23a98
VS
8204 if (IS_CHERRYVIEW(dev))
8205 chv_crtc_clock_get(crtc, pipe_config);
8206 else if (IS_VALLEYVIEW(dev))
acbec814
JB
8207 vlv_crtc_clock_get(crtc, pipe_config);
8208 else
8209 i9xx_crtc_clock_get(crtc, pipe_config);
18442d08 8210
0f64614d
VS
8211 /*
8212 * Normally the dotclock is filled in by the encoder .get_config()
8213 * but in case the pipe is enabled w/o any ports we need a sane
8214 * default.
8215 */
8216 pipe_config->base.adjusted_mode.crtc_clock =
8217 pipe_config->port_clock / pipe_config->pixel_multiplier;
8218
1729050e
ID
8219 ret = true;
8220
8221out:
8222 intel_display_power_put(dev_priv, power_domain);
8223
8224 return ret;
0e8ffe1b
DV
8225}
8226
dde86e2d 8227static void ironlake_init_pch_refclk(struct drm_device *dev)
13d83a67
JB
8228{
8229 struct drm_i915_private *dev_priv = dev->dev_private;
13d83a67 8230 struct intel_encoder *encoder;
74cfd7ac 8231 u32 val, final;
13d83a67 8232 bool has_lvds = false;
199e5d79 8233 bool has_cpu_edp = false;
199e5d79 8234 bool has_panel = false;
99eb6a01
KP
8235 bool has_ck505 = false;
8236 bool can_ssc = false;
13d83a67
JB
8237
8238 /* We need to take the global config into account */
b2784e15 8239 for_each_intel_encoder(dev, encoder) {
199e5d79
KP
8240 switch (encoder->type) {
8241 case INTEL_OUTPUT_LVDS:
8242 has_panel = true;
8243 has_lvds = true;
8244 break;
8245 case INTEL_OUTPUT_EDP:
8246 has_panel = true;
2de6905f 8247 if (enc_to_dig_port(&encoder->base)->port == PORT_A)
199e5d79
KP
8248 has_cpu_edp = true;
8249 break;
6847d71b
PZ
8250 default:
8251 break;
13d83a67
JB
8252 }
8253 }
8254
99eb6a01 8255 if (HAS_PCH_IBX(dev)) {
41aa3448 8256 has_ck505 = dev_priv->vbt.display_clock_mode;
99eb6a01
KP
8257 can_ssc = has_ck505;
8258 } else {
8259 has_ck505 = false;
8260 can_ssc = true;
8261 }
8262
2de6905f
ID
8263 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
8264 has_panel, has_lvds, has_ck505);
13d83a67
JB
8265
8266 /* Ironlake: try to setup display ref clock before DPLL
8267 * enabling. This is only under driver's control after
8268 * PCH B stepping, previous chipset stepping should be
8269 * ignoring this setting.
8270 */
74cfd7ac
CW
8271 val = I915_READ(PCH_DREF_CONTROL);
8272
8273 /* As we must carefully and slowly disable/enable each source in turn,
8274 * compute the final state we want first and check if we need to
8275 * make any changes at all.
8276 */
8277 final = val;
8278 final &= ~DREF_NONSPREAD_SOURCE_MASK;
8279 if (has_ck505)
8280 final |= DREF_NONSPREAD_CK505_ENABLE;
8281 else
8282 final |= DREF_NONSPREAD_SOURCE_ENABLE;
8283
8284 final &= ~DREF_SSC_SOURCE_MASK;
8285 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
8286 final &= ~DREF_SSC1_ENABLE;
8287
8288 if (has_panel) {
8289 final |= DREF_SSC_SOURCE_ENABLE;
8290
8291 if (intel_panel_use_ssc(dev_priv) && can_ssc)
8292 final |= DREF_SSC1_ENABLE;
8293
8294 if (has_cpu_edp) {
8295 if (intel_panel_use_ssc(dev_priv) && can_ssc)
8296 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
8297 else
8298 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
8299 } else
8300 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
8301 } else {
8302 final |= DREF_SSC_SOURCE_DISABLE;
8303 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
8304 }
8305
8306 if (final == val)
8307 return;
8308
13d83a67 8309 /* Always enable nonspread source */
74cfd7ac 8310 val &= ~DREF_NONSPREAD_SOURCE_MASK;
13d83a67 8311
99eb6a01 8312 if (has_ck505)
74cfd7ac 8313 val |= DREF_NONSPREAD_CK505_ENABLE;
99eb6a01 8314 else
74cfd7ac 8315 val |= DREF_NONSPREAD_SOURCE_ENABLE;
13d83a67 8316
199e5d79 8317 if (has_panel) {
74cfd7ac
CW
8318 val &= ~DREF_SSC_SOURCE_MASK;
8319 val |= DREF_SSC_SOURCE_ENABLE;
13d83a67 8320
199e5d79 8321 /* SSC must be turned on before enabling the CPU output */
99eb6a01 8322 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
199e5d79 8323 DRM_DEBUG_KMS("Using SSC on panel\n");
74cfd7ac 8324 val |= DREF_SSC1_ENABLE;
e77166b5 8325 } else
74cfd7ac 8326 val &= ~DREF_SSC1_ENABLE;
199e5d79
KP
8327
8328 /* Get SSC going before enabling the outputs */
74cfd7ac 8329 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
8330 POSTING_READ(PCH_DREF_CONTROL);
8331 udelay(200);
8332
74cfd7ac 8333 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
13d83a67
JB
8334
8335 /* Enable CPU source on CPU attached eDP */
199e5d79 8336 if (has_cpu_edp) {
99eb6a01 8337 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
199e5d79 8338 DRM_DEBUG_KMS("Using SSC on eDP\n");
74cfd7ac 8339 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
eba905b2 8340 } else
74cfd7ac 8341 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
199e5d79 8342 } else
74cfd7ac 8343 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
199e5d79 8344
74cfd7ac 8345 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
8346 POSTING_READ(PCH_DREF_CONTROL);
8347 udelay(200);
8348 } else {
8349 DRM_DEBUG_KMS("Disabling SSC entirely\n");
8350
74cfd7ac 8351 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
199e5d79
KP
8352
8353 /* Turn off CPU output */
74cfd7ac 8354 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
199e5d79 8355
74cfd7ac 8356 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
8357 POSTING_READ(PCH_DREF_CONTROL);
8358 udelay(200);
8359
8360 /* Turn off the SSC source */
74cfd7ac
CW
8361 val &= ~DREF_SSC_SOURCE_MASK;
8362 val |= DREF_SSC_SOURCE_DISABLE;
199e5d79
KP
8363
8364 /* Turn off SSC1 */
74cfd7ac 8365 val &= ~DREF_SSC1_ENABLE;
199e5d79 8366
74cfd7ac 8367 I915_WRITE(PCH_DREF_CONTROL, val);
13d83a67
JB
8368 POSTING_READ(PCH_DREF_CONTROL);
8369 udelay(200);
8370 }
74cfd7ac
CW
8371
8372 BUG_ON(val != final);
13d83a67
JB
8373}
8374
f31f2d55 8375static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
dde86e2d 8376{
f31f2d55 8377 uint32_t tmp;
dde86e2d 8378
0ff066a9
PZ
8379 tmp = I915_READ(SOUTH_CHICKEN2);
8380 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
8381 I915_WRITE(SOUTH_CHICKEN2, tmp);
dde86e2d 8382
0ff066a9
PZ
8383 if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
8384 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
8385 DRM_ERROR("FDI mPHY reset assert timeout\n");
dde86e2d 8386
0ff066a9
PZ
8387 tmp = I915_READ(SOUTH_CHICKEN2);
8388 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
8389 I915_WRITE(SOUTH_CHICKEN2, tmp);
dde86e2d 8390
0ff066a9
PZ
8391 if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
8392 FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
8393 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
f31f2d55
PZ
8394}
8395
8396/* WaMPhyProgramming:hsw */
8397static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
8398{
8399 uint32_t tmp;
dde86e2d
PZ
8400
8401 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
8402 tmp &= ~(0xFF << 24);
8403 tmp |= (0x12 << 24);
8404 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
8405
dde86e2d
PZ
8406 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
8407 tmp |= (1 << 11);
8408 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
8409
8410 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
8411 tmp |= (1 << 11);
8412 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
8413
dde86e2d
PZ
8414 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
8415 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
8416 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
8417
8418 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
8419 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
8420 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
8421
0ff066a9
PZ
8422 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
8423 tmp &= ~(7 << 13);
8424 tmp |= (5 << 13);
8425 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
dde86e2d 8426
0ff066a9
PZ
8427 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
8428 tmp &= ~(7 << 13);
8429 tmp |= (5 << 13);
8430 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
dde86e2d
PZ
8431
8432 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
8433 tmp &= ~0xFF;
8434 tmp |= 0x1C;
8435 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
8436
8437 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
8438 tmp &= ~0xFF;
8439 tmp |= 0x1C;
8440 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
8441
8442 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
8443 tmp &= ~(0xFF << 16);
8444 tmp |= (0x1C << 16);
8445 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
8446
8447 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
8448 tmp &= ~(0xFF << 16);
8449 tmp |= (0x1C << 16);
8450 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
8451
0ff066a9
PZ
8452 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
8453 tmp |= (1 << 27);
8454 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
dde86e2d 8455
0ff066a9
PZ
8456 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
8457 tmp |= (1 << 27);
8458 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
dde86e2d 8459
0ff066a9
PZ
8460 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
8461 tmp &= ~(0xF << 28);
8462 tmp |= (4 << 28);
8463 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
dde86e2d 8464
0ff066a9
PZ
8465 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
8466 tmp &= ~(0xF << 28);
8467 tmp |= (4 << 28);
8468 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
f31f2d55
PZ
8469}
8470
2fa86a1f
PZ
8471/* Implements 3 different sequences from BSpec chapter "Display iCLK
8472 * Programming" based on the parameters passed:
8473 * - Sequence to enable CLKOUT_DP
8474 * - Sequence to enable CLKOUT_DP without spread
8475 * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
8476 */
8477static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
8478 bool with_fdi)
f31f2d55
PZ
8479{
8480 struct drm_i915_private *dev_priv = dev->dev_private;
2fa86a1f
PZ
8481 uint32_t reg, tmp;
8482
8483 if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
8484 with_spread = true;
c2699524 8485 if (WARN(HAS_PCH_LPT_LP(dev) && with_fdi, "LP PCH doesn't have FDI\n"))
2fa86a1f 8486 with_fdi = false;
f31f2d55 8487
a580516d 8488 mutex_lock(&dev_priv->sb_lock);
f31f2d55
PZ
8489
8490 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8491 tmp &= ~SBI_SSCCTL_DISABLE;
8492 tmp |= SBI_SSCCTL_PATHALT;
8493 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8494
8495 udelay(24);
8496
2fa86a1f
PZ
8497 if (with_spread) {
8498 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8499 tmp &= ~SBI_SSCCTL_PATHALT;
8500 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
f31f2d55 8501
2fa86a1f
PZ
8502 if (with_fdi) {
8503 lpt_reset_fdi_mphy(dev_priv);
8504 lpt_program_fdi_mphy(dev_priv);
8505 }
8506 }
dde86e2d 8507
c2699524 8508 reg = HAS_PCH_LPT_LP(dev) ? SBI_GEN0 : SBI_DBUFF0;
2fa86a1f
PZ
8509 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8510 tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8511 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
c00db246 8512
a580516d 8513 mutex_unlock(&dev_priv->sb_lock);
dde86e2d
PZ
8514}
8515
47701c3b
PZ
8516/* Sequence to disable CLKOUT_DP */
8517static void lpt_disable_clkout_dp(struct drm_device *dev)
8518{
8519 struct drm_i915_private *dev_priv = dev->dev_private;
8520 uint32_t reg, tmp;
8521
a580516d 8522 mutex_lock(&dev_priv->sb_lock);
47701c3b 8523
c2699524 8524 reg = HAS_PCH_LPT_LP(dev) ? SBI_GEN0 : SBI_DBUFF0;
47701c3b
PZ
8525 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8526 tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8527 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
8528
8529 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8530 if (!(tmp & SBI_SSCCTL_DISABLE)) {
8531 if (!(tmp & SBI_SSCCTL_PATHALT)) {
8532 tmp |= SBI_SSCCTL_PATHALT;
8533 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8534 udelay(32);
8535 }
8536 tmp |= SBI_SSCCTL_DISABLE;
8537 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8538 }
8539
a580516d 8540 mutex_unlock(&dev_priv->sb_lock);
47701c3b
PZ
8541}
8542
f7be2c21
VS
8543#define BEND_IDX(steps) ((50 + (steps)) / 5)
8544
8545static const uint16_t sscdivintphase[] = {
8546 [BEND_IDX( 50)] = 0x3B23,
8547 [BEND_IDX( 45)] = 0x3B23,
8548 [BEND_IDX( 40)] = 0x3C23,
8549 [BEND_IDX( 35)] = 0x3C23,
8550 [BEND_IDX( 30)] = 0x3D23,
8551 [BEND_IDX( 25)] = 0x3D23,
8552 [BEND_IDX( 20)] = 0x3E23,
8553 [BEND_IDX( 15)] = 0x3E23,
8554 [BEND_IDX( 10)] = 0x3F23,
8555 [BEND_IDX( 5)] = 0x3F23,
8556 [BEND_IDX( 0)] = 0x0025,
8557 [BEND_IDX( -5)] = 0x0025,
8558 [BEND_IDX(-10)] = 0x0125,
8559 [BEND_IDX(-15)] = 0x0125,
8560 [BEND_IDX(-20)] = 0x0225,
8561 [BEND_IDX(-25)] = 0x0225,
8562 [BEND_IDX(-30)] = 0x0325,
8563 [BEND_IDX(-35)] = 0x0325,
8564 [BEND_IDX(-40)] = 0x0425,
8565 [BEND_IDX(-45)] = 0x0425,
8566 [BEND_IDX(-50)] = 0x0525,
8567};
8568
8569/*
8570 * Bend CLKOUT_DP
8571 * steps -50 to 50 inclusive, in steps of 5
8572 * < 0 slow down the clock, > 0 speed up the clock, 0 == no bend (135MHz)
8573 * change in clock period = -(steps / 10) * 5.787 ps
8574 */
8575static void lpt_bend_clkout_dp(struct drm_i915_private *dev_priv, int steps)
8576{
8577 uint32_t tmp;
8578 int idx = BEND_IDX(steps);
8579
8580 if (WARN_ON(steps % 5 != 0))
8581 return;
8582
8583 if (WARN_ON(idx >= ARRAY_SIZE(sscdivintphase)))
8584 return;
8585
8586 mutex_lock(&dev_priv->sb_lock);
8587
8588 if (steps % 10 != 0)
8589 tmp = 0xAAAAAAAB;
8590 else
8591 tmp = 0x00000000;
8592 intel_sbi_write(dev_priv, SBI_SSCDITHPHASE, tmp, SBI_ICLK);
8593
8594 tmp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE, SBI_ICLK);
8595 tmp &= 0xffff0000;
8596 tmp |= sscdivintphase[idx];
8597 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE, tmp, SBI_ICLK);
8598
8599 mutex_unlock(&dev_priv->sb_lock);
8600}
8601
8602#undef BEND_IDX
8603
bf8fa3d3
PZ
8604static void lpt_init_pch_refclk(struct drm_device *dev)
8605{
bf8fa3d3
PZ
8606 struct intel_encoder *encoder;
8607 bool has_vga = false;
8608
b2784e15 8609 for_each_intel_encoder(dev, encoder) {
bf8fa3d3
PZ
8610 switch (encoder->type) {
8611 case INTEL_OUTPUT_ANALOG:
8612 has_vga = true;
8613 break;
6847d71b
PZ
8614 default:
8615 break;
bf8fa3d3
PZ
8616 }
8617 }
8618
f7be2c21
VS
8619 if (has_vga) {
8620 lpt_bend_clkout_dp(to_i915(dev), 0);
47701c3b 8621 lpt_enable_clkout_dp(dev, true, true);
f7be2c21 8622 } else {
47701c3b 8623 lpt_disable_clkout_dp(dev);
f7be2c21 8624 }
bf8fa3d3
PZ
8625}
8626
dde86e2d
PZ
8627/*
8628 * Initialize reference clocks when the driver loads
8629 */
8630void intel_init_pch_refclk(struct drm_device *dev)
8631{
8632 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
8633 ironlake_init_pch_refclk(dev);
8634 else if (HAS_PCH_LPT(dev))
8635 lpt_init_pch_refclk(dev);
8636}
8637
55bb9992 8638static int ironlake_get_refclk(struct intel_crtc_state *crtc_state)
d9d444cb 8639{
55bb9992 8640 struct drm_device *dev = crtc_state->base.crtc->dev;
d9d444cb 8641 struct drm_i915_private *dev_priv = dev->dev_private;
55bb9992 8642 struct drm_atomic_state *state = crtc_state->base.state;
da3ced29 8643 struct drm_connector *connector;
55bb9992 8644 struct drm_connector_state *connector_state;
d9d444cb 8645 struct intel_encoder *encoder;
55bb9992 8646 int num_connectors = 0, i;
d9d444cb
JB
8647 bool is_lvds = false;
8648
da3ced29 8649 for_each_connector_in_state(state, connector, connector_state, i) {
55bb9992
ACO
8650 if (connector_state->crtc != crtc_state->base.crtc)
8651 continue;
8652
8653 encoder = to_intel_encoder(connector_state->best_encoder);
8654
d9d444cb
JB
8655 switch (encoder->type) {
8656 case INTEL_OUTPUT_LVDS:
8657 is_lvds = true;
8658 break;
6847d71b
PZ
8659 default:
8660 break;
d9d444cb
JB
8661 }
8662 num_connectors++;
8663 }
8664
8665 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
e91e941b 8666 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
41aa3448 8667 dev_priv->vbt.lvds_ssc_freq);
e91e941b 8668 return dev_priv->vbt.lvds_ssc_freq;
d9d444cb
JB
8669 }
8670
8671 return 120000;
8672}
8673
6ff93609 8674static void ironlake_set_pipeconf(struct drm_crtc *crtc)
79e53945 8675{
c8203565 8676 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
79e53945
JB
8677 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8678 int pipe = intel_crtc->pipe;
c8203565
PZ
8679 uint32_t val;
8680
78114071 8681 val = 0;
c8203565 8682
6e3c9717 8683 switch (intel_crtc->config->pipe_bpp) {
c8203565 8684 case 18:
dfd07d72 8685 val |= PIPECONF_6BPC;
c8203565
PZ
8686 break;
8687 case 24:
dfd07d72 8688 val |= PIPECONF_8BPC;
c8203565
PZ
8689 break;
8690 case 30:
dfd07d72 8691 val |= PIPECONF_10BPC;
c8203565
PZ
8692 break;
8693 case 36:
dfd07d72 8694 val |= PIPECONF_12BPC;
c8203565
PZ
8695 break;
8696 default:
cc769b62
PZ
8697 /* Case prevented by intel_choose_pipe_bpp_dither. */
8698 BUG();
c8203565
PZ
8699 }
8700
6e3c9717 8701 if (intel_crtc->config->dither)
c8203565
PZ
8702 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8703
6e3c9717 8704 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
c8203565
PZ
8705 val |= PIPECONF_INTERLACED_ILK;
8706 else
8707 val |= PIPECONF_PROGRESSIVE;
8708
6e3c9717 8709 if (intel_crtc->config->limited_color_range)
3685a8f3 8710 val |= PIPECONF_COLOR_RANGE_SELECT;
3685a8f3 8711
c8203565
PZ
8712 I915_WRITE(PIPECONF(pipe), val);
8713 POSTING_READ(PIPECONF(pipe));
8714}
8715
86d3efce
VS
8716/*
8717 * Set up the pipe CSC unit.
8718 *
8719 * Currently only full range RGB to limited range RGB conversion
8720 * is supported, but eventually this should handle various
8721 * RGB<->YCbCr scenarios as well.
8722 */
50f3b016 8723static void intel_set_pipe_csc(struct drm_crtc *crtc)
86d3efce
VS
8724{
8725 struct drm_device *dev = crtc->dev;
8726 struct drm_i915_private *dev_priv = dev->dev_private;
8727 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8728 int pipe = intel_crtc->pipe;
8729 uint16_t coeff = 0x7800; /* 1.0 */
8730
8731 /*
8732 * TODO: Check what kind of values actually come out of the pipe
8733 * with these coeff/postoff values and adjust to get the best
8734 * accuracy. Perhaps we even need to take the bpc value into
8735 * consideration.
8736 */
8737
6e3c9717 8738 if (intel_crtc->config->limited_color_range)
86d3efce
VS
8739 coeff = ((235 - 16) * (1 << 12) / 255) & 0xff8; /* 0.xxx... */
8740
8741 /*
8742 * GY/GU and RY/RU should be the other way around according
8743 * to BSpec, but reality doesn't agree. Just set them up in
8744 * a way that results in the correct picture.
8745 */
8746 I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff << 16);
8747 I915_WRITE(PIPE_CSC_COEFF_BY(pipe), 0);
8748
8749 I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff);
8750 I915_WRITE(PIPE_CSC_COEFF_BU(pipe), 0);
8751
8752 I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), 0);
8753 I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff << 16);
8754
8755 I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), 0);
8756 I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), 0);
8757 I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), 0);
8758
8759 if (INTEL_INFO(dev)->gen > 6) {
8760 uint16_t postoff = 0;
8761
6e3c9717 8762 if (intel_crtc->config->limited_color_range)
32cf0cb0 8763 postoff = (16 * (1 << 12) / 255) & 0x1fff;
86d3efce
VS
8764
8765 I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff);
8766 I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff);
8767 I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff);
8768
8769 I915_WRITE(PIPE_CSC_MODE(pipe), 0);
8770 } else {
8771 uint32_t mode = CSC_MODE_YUV_TO_RGB;
8772
6e3c9717 8773 if (intel_crtc->config->limited_color_range)
86d3efce
VS
8774 mode |= CSC_BLACK_SCREEN_OFFSET;
8775
8776 I915_WRITE(PIPE_CSC_MODE(pipe), mode);
8777 }
8778}
8779
6ff93609 8780static void haswell_set_pipeconf(struct drm_crtc *crtc)
ee2b0b38 8781{
391bf048 8782 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
ee2b0b38 8783 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6e3c9717 8784 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
391bf048 8785 u32 val = 0;
ee2b0b38 8786
391bf048 8787 if (IS_HASWELL(dev_priv) && intel_crtc->config->dither)
ee2b0b38
PZ
8788 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8789
6e3c9717 8790 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
ee2b0b38
PZ
8791 val |= PIPECONF_INTERLACED_ILK;
8792 else
8793 val |= PIPECONF_PROGRESSIVE;
8794
702e7a56
PZ
8795 I915_WRITE(PIPECONF(cpu_transcoder), val);
8796 POSTING_READ(PIPECONF(cpu_transcoder));
391bf048
JN
8797}
8798
8799static void haswell_set_pipe_gamma(struct drm_crtc *crtc)
8800{
8801 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
8802 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3eff4faa
DV
8803
8804 I915_WRITE(GAMMA_MODE(intel_crtc->pipe), GAMMA_MODE_MODE_8BIT);
8805 POSTING_READ(GAMMA_MODE(intel_crtc->pipe));
391bf048
JN
8806}
8807
8808static void haswell_set_pipemisc(struct drm_crtc *crtc)
8809{
8810 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
8811 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
756f85cf 8812
391bf048
JN
8813 if (IS_BROADWELL(dev_priv) || INTEL_INFO(dev_priv)->gen >= 9) {
8814 u32 val = 0;
756f85cf 8815
6e3c9717 8816 switch (intel_crtc->config->pipe_bpp) {
756f85cf
PZ
8817 case 18:
8818 val |= PIPEMISC_DITHER_6_BPC;
8819 break;
8820 case 24:
8821 val |= PIPEMISC_DITHER_8_BPC;
8822 break;
8823 case 30:
8824 val |= PIPEMISC_DITHER_10_BPC;
8825 break;
8826 case 36:
8827 val |= PIPEMISC_DITHER_12_BPC;
8828 break;
8829 default:
8830 /* Case prevented by pipe_config_set_bpp. */
8831 BUG();
8832 }
8833
6e3c9717 8834 if (intel_crtc->config->dither)
756f85cf
PZ
8835 val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
8836
391bf048 8837 I915_WRITE(PIPEMISC(intel_crtc->pipe), val);
756f85cf 8838 }
ee2b0b38
PZ
8839}
8840
6591c6e4 8841static bool ironlake_compute_clocks(struct drm_crtc *crtc,
190f68c5 8842 struct intel_crtc_state *crtc_state,
6591c6e4
PZ
8843 intel_clock_t *clock,
8844 bool *has_reduced_clock,
8845 intel_clock_t *reduced_clock)
8846{
8847 struct drm_device *dev = crtc->dev;
8848 struct drm_i915_private *dev_priv = dev->dev_private;
6591c6e4 8849 int refclk;
d4906093 8850 const intel_limit_t *limit;
c329a4ec 8851 bool ret;
79e53945 8852
55bb9992 8853 refclk = ironlake_get_refclk(crtc_state);
79e53945 8854
d4906093
ML
8855 /*
8856 * Returns a set of divisors for the desired target clock with the given
8857 * refclk, or FALSE. The returned values represent the clock equation:
8858 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
8859 */
a93e255f
ACO
8860 limit = intel_limit(crtc_state, refclk);
8861 ret = dev_priv->display.find_dpll(limit, crtc_state,
190f68c5 8862 crtc_state->port_clock,
ee9300bb 8863 refclk, NULL, clock);
6591c6e4
PZ
8864 if (!ret)
8865 return false;
cda4b7d3 8866
6591c6e4
PZ
8867 return true;
8868}
8869
d4b1931c
PZ
8870int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
8871{
8872 /*
8873 * Account for spread spectrum to avoid
8874 * oversubscribing the link. Max center spread
8875 * is 2.5%; use 5% for safety's sake.
8876 */
8877 u32 bps = target_clock * bpp * 21 / 20;
619d4d04 8878 return DIV_ROUND_UP(bps, link_bw * 8);
d4b1931c
PZ
8879}
8880
7429e9d4 8881static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
6cf86a5e 8882{
7429e9d4 8883 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
f48d8f23
PZ
8884}
8885
de13a2e3 8886static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
190f68c5 8887 struct intel_crtc_state *crtc_state,
7429e9d4 8888 u32 *fp,
9a7c7890 8889 intel_clock_t *reduced_clock, u32 *fp2)
79e53945 8890{
de13a2e3 8891 struct drm_crtc *crtc = &intel_crtc->base;
79e53945
JB
8892 struct drm_device *dev = crtc->dev;
8893 struct drm_i915_private *dev_priv = dev->dev_private;
55bb9992 8894 struct drm_atomic_state *state = crtc_state->base.state;
da3ced29 8895 struct drm_connector *connector;
55bb9992
ACO
8896 struct drm_connector_state *connector_state;
8897 struct intel_encoder *encoder;
de13a2e3 8898 uint32_t dpll;
55bb9992 8899 int factor, num_connectors = 0, i;
09ede541 8900 bool is_lvds = false, is_sdvo = false;
79e53945 8901
da3ced29 8902 for_each_connector_in_state(state, connector, connector_state, i) {
55bb9992
ACO
8903 if (connector_state->crtc != crtc_state->base.crtc)
8904 continue;
8905
8906 encoder = to_intel_encoder(connector_state->best_encoder);
8907
8908 switch (encoder->type) {
79e53945
JB
8909 case INTEL_OUTPUT_LVDS:
8910 is_lvds = true;
8911 break;
8912 case INTEL_OUTPUT_SDVO:
7d57382e 8913 case INTEL_OUTPUT_HDMI:
79e53945 8914 is_sdvo = true;
79e53945 8915 break;
6847d71b
PZ
8916 default:
8917 break;
79e53945 8918 }
43565a06 8919
c751ce4f 8920 num_connectors++;
79e53945 8921 }
79e53945 8922
c1858123 8923 /* Enable autotuning of the PLL clock (if permissible) */
8febb297
EA
8924 factor = 21;
8925 if (is_lvds) {
8926 if ((intel_panel_use_ssc(dev_priv) &&
e91e941b 8927 dev_priv->vbt.lvds_ssc_freq == 100000) ||
f0b44056 8928 (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
8febb297 8929 factor = 25;
190f68c5 8930 } else if (crtc_state->sdvo_tv_clock)
8febb297 8931 factor = 20;
c1858123 8932
190f68c5 8933 if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
7d0ac5b7 8934 *fp |= FP_CB_TUNE;
2c07245f 8935
9a7c7890
DV
8936 if (fp2 && (reduced_clock->m < factor * reduced_clock->n))
8937 *fp2 |= FP_CB_TUNE;
8938
5eddb70b 8939 dpll = 0;
2c07245f 8940
a07d6787
EA
8941 if (is_lvds)
8942 dpll |= DPLLB_MODE_LVDS;
8943 else
8944 dpll |= DPLLB_MODE_DAC_SERIAL;
198a037f 8945
190f68c5 8946 dpll |= (crtc_state->pixel_multiplier - 1)
ef1b460d 8947 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
198a037f
DV
8948
8949 if (is_sdvo)
4a33e48d 8950 dpll |= DPLL_SDVO_HIGH_SPEED;
190f68c5 8951 if (crtc_state->has_dp_encoder)
4a33e48d 8952 dpll |= DPLL_SDVO_HIGH_SPEED;
79e53945 8953
a07d6787 8954 /* compute bitmask from p1 value */
190f68c5 8955 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
a07d6787 8956 /* also FPA1 */
190f68c5 8957 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
a07d6787 8958
190f68c5 8959 switch (crtc_state->dpll.p2) {
a07d6787
EA
8960 case 5:
8961 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
8962 break;
8963 case 7:
8964 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
8965 break;
8966 case 10:
8967 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
8968 break;
8969 case 14:
8970 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
8971 break;
79e53945
JB
8972 }
8973
b4c09f3b 8974 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
43565a06 8975 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
79e53945
JB
8976 else
8977 dpll |= PLL_REF_INPUT_DREFCLK;
8978
959e16d6 8979 return dpll | DPLL_VCO_ENABLE;
de13a2e3
PZ
8980}
8981
190f68c5
ACO
8982static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
8983 struct intel_crtc_state *crtc_state)
de13a2e3 8984{
c7653199 8985 struct drm_device *dev = crtc->base.dev;
de13a2e3 8986 intel_clock_t clock, reduced_clock;
cbbab5bd 8987 u32 dpll = 0, fp = 0, fp2 = 0;
e2f12b07 8988 bool ok, has_reduced_clock = false;
8b47047b 8989 bool is_lvds = false;
e2b78267 8990 struct intel_shared_dpll *pll;
de13a2e3 8991
dd3cd74a
ACO
8992 memset(&crtc_state->dpll_hw_state, 0,
8993 sizeof(crtc_state->dpll_hw_state));
8994
7905df29 8995 is_lvds = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS);
79e53945 8996
5dc5298b
PZ
8997 WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
8998 "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
a07d6787 8999
190f68c5 9000 ok = ironlake_compute_clocks(&crtc->base, crtc_state, &clock,
de13a2e3 9001 &has_reduced_clock, &reduced_clock);
190f68c5 9002 if (!ok && !crtc_state->clock_set) {
de13a2e3
PZ
9003 DRM_ERROR("Couldn't find PLL settings for mode!\n");
9004 return -EINVAL;
79e53945 9005 }
f47709a9 9006 /* Compat-code for transition, will disappear. */
190f68c5
ACO
9007 if (!crtc_state->clock_set) {
9008 crtc_state->dpll.n = clock.n;
9009 crtc_state->dpll.m1 = clock.m1;
9010 crtc_state->dpll.m2 = clock.m2;
9011 crtc_state->dpll.p1 = clock.p1;
9012 crtc_state->dpll.p2 = clock.p2;
f47709a9 9013 }
79e53945 9014
5dc5298b 9015 /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
190f68c5
ACO
9016 if (crtc_state->has_pch_encoder) {
9017 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
cbbab5bd 9018 if (has_reduced_clock)
7429e9d4 9019 fp2 = i9xx_dpll_compute_fp(&reduced_clock);
cbbab5bd 9020
190f68c5 9021 dpll = ironlake_compute_dpll(crtc, crtc_state,
cbbab5bd
DV
9022 &fp, &reduced_clock,
9023 has_reduced_clock ? &fp2 : NULL);
9024
190f68c5
ACO
9025 crtc_state->dpll_hw_state.dpll = dpll;
9026 crtc_state->dpll_hw_state.fp0 = fp;
66e985c0 9027 if (has_reduced_clock)
190f68c5 9028 crtc_state->dpll_hw_state.fp1 = fp2;
66e985c0 9029 else
190f68c5 9030 crtc_state->dpll_hw_state.fp1 = fp;
66e985c0 9031
daedf20a 9032 pll = intel_get_shared_dpll(crtc, crtc_state, NULL);
ee7b9f93 9033 if (pll == NULL) {
84f44ce7 9034 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
c7653199 9035 pipe_name(crtc->pipe));
4b645f14
JB
9036 return -EINVAL;
9037 }
3fb37703 9038 }
79e53945 9039
ab585dea 9040 if (is_lvds && has_reduced_clock)
c7653199 9041 crtc->lowfreq_avail = true;
bcd644e0 9042 else
c7653199 9043 crtc->lowfreq_avail = false;
e2b78267 9044
c8f7a0db 9045 return 0;
79e53945
JB
9046}
9047
eb14cb74
VS
9048static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
9049 struct intel_link_m_n *m_n)
9050{
9051 struct drm_device *dev = crtc->base.dev;
9052 struct drm_i915_private *dev_priv = dev->dev_private;
9053 enum pipe pipe = crtc->pipe;
9054
9055 m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
9056 m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
9057 m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
9058 & ~TU_SIZE_MASK;
9059 m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
9060 m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
9061 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
9062}
9063
9064static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
9065 enum transcoder transcoder,
b95af8be
VK
9066 struct intel_link_m_n *m_n,
9067 struct intel_link_m_n *m2_n2)
72419203
DV
9068{
9069 struct drm_device *dev = crtc->base.dev;
9070 struct drm_i915_private *dev_priv = dev->dev_private;
eb14cb74 9071 enum pipe pipe = crtc->pipe;
72419203 9072
eb14cb74
VS
9073 if (INTEL_INFO(dev)->gen >= 5) {
9074 m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
9075 m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
9076 m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
9077 & ~TU_SIZE_MASK;
9078 m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
9079 m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
9080 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
b95af8be
VK
9081 /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
9082 * gen < 8) and if DRRS is supported (to make sure the
9083 * registers are not unnecessarily read).
9084 */
9085 if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
6e3c9717 9086 crtc->config->has_drrs) {
b95af8be
VK
9087 m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
9088 m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
9089 m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
9090 & ~TU_SIZE_MASK;
9091 m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
9092 m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
9093 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
9094 }
eb14cb74
VS
9095 } else {
9096 m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
9097 m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
9098 m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
9099 & ~TU_SIZE_MASK;
9100 m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
9101 m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
9102 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
9103 }
9104}
9105
9106void intel_dp_get_m_n(struct intel_crtc *crtc,
5cec258b 9107 struct intel_crtc_state *pipe_config)
eb14cb74 9108{
681a8504 9109 if (pipe_config->has_pch_encoder)
eb14cb74
VS
9110 intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
9111 else
9112 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
b95af8be
VK
9113 &pipe_config->dp_m_n,
9114 &pipe_config->dp_m2_n2);
eb14cb74 9115}
72419203 9116
eb14cb74 9117static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
5cec258b 9118 struct intel_crtc_state *pipe_config)
eb14cb74
VS
9119{
9120 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
b95af8be 9121 &pipe_config->fdi_m_n, NULL);
72419203
DV
9122}
9123
bd2e244f 9124static void skylake_get_pfit_config(struct intel_crtc *crtc,
5cec258b 9125 struct intel_crtc_state *pipe_config)
bd2e244f
JB
9126{
9127 struct drm_device *dev = crtc->base.dev;
9128 struct drm_i915_private *dev_priv = dev->dev_private;
a1b2278e
CK
9129 struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
9130 uint32_t ps_ctrl = 0;
9131 int id = -1;
9132 int i;
bd2e244f 9133
a1b2278e
CK
9134 /* find scaler attached to this pipe */
9135 for (i = 0; i < crtc->num_scalers; i++) {
9136 ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
9137 if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
9138 id = i;
9139 pipe_config->pch_pfit.enabled = true;
9140 pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
9141 pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
9142 break;
9143 }
9144 }
bd2e244f 9145
a1b2278e
CK
9146 scaler_state->scaler_id = id;
9147 if (id >= 0) {
9148 scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
9149 } else {
9150 scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
bd2e244f
JB
9151 }
9152}
9153
5724dbd1
DL
9154static void
9155skylake_get_initial_plane_config(struct intel_crtc *crtc,
9156 struct intel_initial_plane_config *plane_config)
bc8d7dff
DL
9157{
9158 struct drm_device *dev = crtc->base.dev;
9159 struct drm_i915_private *dev_priv = dev->dev_private;
40f46283 9160 u32 val, base, offset, stride_mult, tiling;
bc8d7dff
DL
9161 int pipe = crtc->pipe;
9162 int fourcc, pixel_format;
6761dd31 9163 unsigned int aligned_height;
bc8d7dff 9164 struct drm_framebuffer *fb;
1b842c89 9165 struct intel_framebuffer *intel_fb;
bc8d7dff 9166
d9806c9f 9167 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
1b842c89 9168 if (!intel_fb) {
bc8d7dff
DL
9169 DRM_DEBUG_KMS("failed to alloc fb\n");
9170 return;
9171 }
9172
1b842c89
DL
9173 fb = &intel_fb->base;
9174
bc8d7dff 9175 val = I915_READ(PLANE_CTL(pipe, 0));
42a7b088
DL
9176 if (!(val & PLANE_CTL_ENABLE))
9177 goto error;
9178
bc8d7dff
DL
9179 pixel_format = val & PLANE_CTL_FORMAT_MASK;
9180 fourcc = skl_format_to_fourcc(pixel_format,
9181 val & PLANE_CTL_ORDER_RGBX,
9182 val & PLANE_CTL_ALPHA_MASK);
9183 fb->pixel_format = fourcc;
9184 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
9185
40f46283
DL
9186 tiling = val & PLANE_CTL_TILED_MASK;
9187 switch (tiling) {
9188 case PLANE_CTL_TILED_LINEAR:
9189 fb->modifier[0] = DRM_FORMAT_MOD_NONE;
9190 break;
9191 case PLANE_CTL_TILED_X:
9192 plane_config->tiling = I915_TILING_X;
9193 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
9194 break;
9195 case PLANE_CTL_TILED_Y:
9196 fb->modifier[0] = I915_FORMAT_MOD_Y_TILED;
9197 break;
9198 case PLANE_CTL_TILED_YF:
9199 fb->modifier[0] = I915_FORMAT_MOD_Yf_TILED;
9200 break;
9201 default:
9202 MISSING_CASE(tiling);
9203 goto error;
9204 }
9205
bc8d7dff
DL
9206 base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
9207 plane_config->base = base;
9208
9209 offset = I915_READ(PLANE_OFFSET(pipe, 0));
9210
9211 val = I915_READ(PLANE_SIZE(pipe, 0));
9212 fb->height = ((val >> 16) & 0xfff) + 1;
9213 fb->width = ((val >> 0) & 0x1fff) + 1;
9214
9215 val = I915_READ(PLANE_STRIDE(pipe, 0));
7b49f948 9216 stride_mult = intel_fb_stride_alignment(dev_priv, fb->modifier[0],
40f46283 9217 fb->pixel_format);
bc8d7dff
DL
9218 fb->pitches[0] = (val & 0x3ff) * stride_mult;
9219
9220 aligned_height = intel_fb_align_height(dev, fb->height,
091df6cb
DV
9221 fb->pixel_format,
9222 fb->modifier[0]);
bc8d7dff 9223
f37b5c2b 9224 plane_config->size = fb->pitches[0] * aligned_height;
bc8d7dff
DL
9225
9226 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9227 pipe_name(pipe), fb->width, fb->height,
9228 fb->bits_per_pixel, base, fb->pitches[0],
9229 plane_config->size);
9230
2d14030b 9231 plane_config->fb = intel_fb;
bc8d7dff
DL
9232 return;
9233
9234error:
9235 kfree(fb);
9236}
9237
2fa2fe9a 9238static void ironlake_get_pfit_config(struct intel_crtc *crtc,
5cec258b 9239 struct intel_crtc_state *pipe_config)
2fa2fe9a
DV
9240{
9241 struct drm_device *dev = crtc->base.dev;
9242 struct drm_i915_private *dev_priv = dev->dev_private;
9243 uint32_t tmp;
9244
9245 tmp = I915_READ(PF_CTL(crtc->pipe));
9246
9247 if (tmp & PF_ENABLE) {
fd4daa9c 9248 pipe_config->pch_pfit.enabled = true;
2fa2fe9a
DV
9249 pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
9250 pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
cb8b2a30
DV
9251
9252 /* We currently do not free assignements of panel fitters on
9253 * ivb/hsw (since we don't use the higher upscaling modes which
9254 * differentiates them) so just WARN about this case for now. */
9255 if (IS_GEN7(dev)) {
9256 WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
9257 PF_PIPE_SEL_IVB(crtc->pipe));
9258 }
2fa2fe9a 9259 }
79e53945
JB
9260}
9261
5724dbd1
DL
9262static void
9263ironlake_get_initial_plane_config(struct intel_crtc *crtc,
9264 struct intel_initial_plane_config *plane_config)
4c6baa59
JB
9265{
9266 struct drm_device *dev = crtc->base.dev;
9267 struct drm_i915_private *dev_priv = dev->dev_private;
9268 u32 val, base, offset;
aeee5a49 9269 int pipe = crtc->pipe;
4c6baa59 9270 int fourcc, pixel_format;
6761dd31 9271 unsigned int aligned_height;
b113d5ee 9272 struct drm_framebuffer *fb;
1b842c89 9273 struct intel_framebuffer *intel_fb;
4c6baa59 9274
42a7b088
DL
9275 val = I915_READ(DSPCNTR(pipe));
9276 if (!(val & DISPLAY_PLANE_ENABLE))
9277 return;
9278
d9806c9f 9279 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
1b842c89 9280 if (!intel_fb) {
4c6baa59
JB
9281 DRM_DEBUG_KMS("failed to alloc fb\n");
9282 return;
9283 }
9284
1b842c89
DL
9285 fb = &intel_fb->base;
9286
18c5247e
DV
9287 if (INTEL_INFO(dev)->gen >= 4) {
9288 if (val & DISPPLANE_TILED) {
49af449b 9289 plane_config->tiling = I915_TILING_X;
18c5247e
DV
9290 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
9291 }
9292 }
4c6baa59
JB
9293
9294 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
b35d63fa 9295 fourcc = i9xx_format_to_fourcc(pixel_format);
b113d5ee
DL
9296 fb->pixel_format = fourcc;
9297 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
4c6baa59 9298
aeee5a49 9299 base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
4c6baa59 9300 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
aeee5a49 9301 offset = I915_READ(DSPOFFSET(pipe));
4c6baa59 9302 } else {
49af449b 9303 if (plane_config->tiling)
aeee5a49 9304 offset = I915_READ(DSPTILEOFF(pipe));
4c6baa59 9305 else
aeee5a49 9306 offset = I915_READ(DSPLINOFF(pipe));
4c6baa59
JB
9307 }
9308 plane_config->base = base;
9309
9310 val = I915_READ(PIPESRC(pipe));
b113d5ee
DL
9311 fb->width = ((val >> 16) & 0xfff) + 1;
9312 fb->height = ((val >> 0) & 0xfff) + 1;
4c6baa59
JB
9313
9314 val = I915_READ(DSPSTRIDE(pipe));
b113d5ee 9315 fb->pitches[0] = val & 0xffffffc0;
4c6baa59 9316
b113d5ee 9317 aligned_height = intel_fb_align_height(dev, fb->height,
091df6cb
DV
9318 fb->pixel_format,
9319 fb->modifier[0]);
4c6baa59 9320
f37b5c2b 9321 plane_config->size = fb->pitches[0] * aligned_height;
4c6baa59 9322
2844a921
DL
9323 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9324 pipe_name(pipe), fb->width, fb->height,
9325 fb->bits_per_pixel, base, fb->pitches[0],
9326 plane_config->size);
b113d5ee 9327
2d14030b 9328 plane_config->fb = intel_fb;
4c6baa59
JB
9329}
9330
0e8ffe1b 9331static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
5cec258b 9332 struct intel_crtc_state *pipe_config)
0e8ffe1b
DV
9333{
9334 struct drm_device *dev = crtc->base.dev;
9335 struct drm_i915_private *dev_priv = dev->dev_private;
1729050e 9336 enum intel_display_power_domain power_domain;
0e8ffe1b 9337 uint32_t tmp;
1729050e 9338 bool ret;
0e8ffe1b 9339
1729050e
ID
9340 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
9341 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
930e8c9e
PZ
9342 return false;
9343
e143a21c 9344 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
8106ddbd 9345 pipe_config->shared_dpll = NULL;
eccb140b 9346
1729050e 9347 ret = false;
0e8ffe1b
DV
9348 tmp = I915_READ(PIPECONF(crtc->pipe));
9349 if (!(tmp & PIPECONF_ENABLE))
1729050e 9350 goto out;
0e8ffe1b 9351
42571aef
VS
9352 switch (tmp & PIPECONF_BPC_MASK) {
9353 case PIPECONF_6BPC:
9354 pipe_config->pipe_bpp = 18;
9355 break;
9356 case PIPECONF_8BPC:
9357 pipe_config->pipe_bpp = 24;
9358 break;
9359 case PIPECONF_10BPC:
9360 pipe_config->pipe_bpp = 30;
9361 break;
9362 case PIPECONF_12BPC:
9363 pipe_config->pipe_bpp = 36;
9364 break;
9365 default:
9366 break;
9367 }
9368
b5a9fa09
DV
9369 if (tmp & PIPECONF_COLOR_RANGE_SELECT)
9370 pipe_config->limited_color_range = true;
9371
ab9412ba 9372 if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
66e985c0 9373 struct intel_shared_dpll *pll;
8106ddbd 9374 enum intel_dpll_id pll_id;
66e985c0 9375
88adfff1
DV
9376 pipe_config->has_pch_encoder = true;
9377
627eb5a3
DV
9378 tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
9379 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
9380 FDI_DP_PORT_WIDTH_SHIFT) + 1;
72419203
DV
9381
9382 ironlake_get_fdi_m_n_config(crtc, pipe_config);
6c49f241 9383
c0d43d62 9384 if (HAS_PCH_IBX(dev_priv->dev)) {
8106ddbd 9385 pll_id = (enum intel_dpll_id) crtc->pipe;
c0d43d62
DV
9386 } else {
9387 tmp = I915_READ(PCH_DPLL_SEL);
9388 if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
8106ddbd 9389 pll_id = DPLL_ID_PCH_PLL_B;
c0d43d62 9390 else
8106ddbd 9391 pll_id= DPLL_ID_PCH_PLL_A;
c0d43d62 9392 }
66e985c0 9393
8106ddbd
ACO
9394 pipe_config->shared_dpll =
9395 intel_get_shared_dpll_by_id(dev_priv, pll_id);
9396 pll = pipe_config->shared_dpll;
66e985c0 9397
2edd6443
ACO
9398 WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
9399 &pipe_config->dpll_hw_state));
c93f54cf
DV
9400
9401 tmp = pipe_config->dpll_hw_state.dpll;
9402 pipe_config->pixel_multiplier =
9403 ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
9404 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
18442d08
VS
9405
9406 ironlake_pch_clock_get(crtc, pipe_config);
6c49f241
DV
9407 } else {
9408 pipe_config->pixel_multiplier = 1;
627eb5a3
DV
9409 }
9410
1bd1bd80 9411 intel_get_pipe_timings(crtc, pipe_config);
bc58be60 9412 intel_get_pipe_src_size(crtc, pipe_config);
1bd1bd80 9413
2fa2fe9a
DV
9414 ironlake_get_pfit_config(crtc, pipe_config);
9415
1729050e
ID
9416 ret = true;
9417
9418out:
9419 intel_display_power_put(dev_priv, power_domain);
9420
9421 return ret;
0e8ffe1b
DV
9422}
9423
be256dc7
PZ
9424static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
9425{
9426 struct drm_device *dev = dev_priv->dev;
be256dc7 9427 struct intel_crtc *crtc;
be256dc7 9428
d3fcc808 9429 for_each_intel_crtc(dev, crtc)
e2c719b7 9430 I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
be256dc7
PZ
9431 pipe_name(crtc->pipe));
9432
e2c719b7
RC
9433 I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
9434 I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
01403de3
VS
9435 I915_STATE_WARN(I915_READ(WRPLL_CTL(0)) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
9436 I915_STATE_WARN(I915_READ(WRPLL_CTL(1)) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
e2c719b7
RC
9437 I915_STATE_WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
9438 I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
be256dc7 9439 "CPU PWM1 enabled\n");
c5107b87 9440 if (IS_HASWELL(dev))
e2c719b7 9441 I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
c5107b87 9442 "CPU PWM2 enabled\n");
e2c719b7 9443 I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
be256dc7 9444 "PCH PWM1 enabled\n");
e2c719b7 9445 I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
be256dc7 9446 "Utility pin enabled\n");
e2c719b7 9447 I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
be256dc7 9448
9926ada1
PZ
9449 /*
9450 * In theory we can still leave IRQs enabled, as long as only the HPD
9451 * interrupts remain enabled. We used to check for that, but since it's
9452 * gen-specific and since we only disable LCPLL after we fully disable
9453 * the interrupts, the check below should be enough.
9454 */
e2c719b7 9455 I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
be256dc7
PZ
9456}
9457
9ccd5aeb
PZ
9458static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
9459{
9460 struct drm_device *dev = dev_priv->dev;
9461
9462 if (IS_HASWELL(dev))
9463 return I915_READ(D_COMP_HSW);
9464 else
9465 return I915_READ(D_COMP_BDW);
9466}
9467
3c4c9b81
PZ
9468static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
9469{
9470 struct drm_device *dev = dev_priv->dev;
9471
9472 if (IS_HASWELL(dev)) {
9473 mutex_lock(&dev_priv->rps.hw_lock);
9474 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
9475 val))
f475dadf 9476 DRM_ERROR("Failed to write to D_COMP\n");
3c4c9b81
PZ
9477 mutex_unlock(&dev_priv->rps.hw_lock);
9478 } else {
9ccd5aeb
PZ
9479 I915_WRITE(D_COMP_BDW, val);
9480 POSTING_READ(D_COMP_BDW);
3c4c9b81 9481 }
be256dc7
PZ
9482}
9483
9484/*
9485 * This function implements pieces of two sequences from BSpec:
9486 * - Sequence for display software to disable LCPLL
9487 * - Sequence for display software to allow package C8+
9488 * The steps implemented here are just the steps that actually touch the LCPLL
9489 * register. Callers should take care of disabling all the display engine
9490 * functions, doing the mode unset, fixing interrupts, etc.
9491 */
6ff58d53
PZ
9492static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
9493 bool switch_to_fclk, bool allow_power_down)
be256dc7
PZ
9494{
9495 uint32_t val;
9496
9497 assert_can_disable_lcpll(dev_priv);
9498
9499 val = I915_READ(LCPLL_CTL);
9500
9501 if (switch_to_fclk) {
9502 val |= LCPLL_CD_SOURCE_FCLK;
9503 I915_WRITE(LCPLL_CTL, val);
9504
9505 if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
9506 LCPLL_CD_SOURCE_FCLK_DONE, 1))
9507 DRM_ERROR("Switching to FCLK failed\n");
9508
9509 val = I915_READ(LCPLL_CTL);
9510 }
9511
9512 val |= LCPLL_PLL_DISABLE;
9513 I915_WRITE(LCPLL_CTL, val);
9514 POSTING_READ(LCPLL_CTL);
9515
9516 if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
9517 DRM_ERROR("LCPLL still locked\n");
9518
9ccd5aeb 9519 val = hsw_read_dcomp(dev_priv);
be256dc7 9520 val |= D_COMP_COMP_DISABLE;
3c4c9b81 9521 hsw_write_dcomp(dev_priv, val);
be256dc7
PZ
9522 ndelay(100);
9523
9ccd5aeb
PZ
9524 if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
9525 1))
be256dc7
PZ
9526 DRM_ERROR("D_COMP RCOMP still in progress\n");
9527
9528 if (allow_power_down) {
9529 val = I915_READ(LCPLL_CTL);
9530 val |= LCPLL_POWER_DOWN_ALLOW;
9531 I915_WRITE(LCPLL_CTL, val);
9532 POSTING_READ(LCPLL_CTL);
9533 }
9534}
9535
9536/*
9537 * Fully restores LCPLL, disallowing power down and switching back to LCPLL
9538 * source.
9539 */
6ff58d53 9540static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
be256dc7
PZ
9541{
9542 uint32_t val;
9543
9544 val = I915_READ(LCPLL_CTL);
9545
9546 if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
9547 LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
9548 return;
9549
a8a8bd54
PZ
9550 /*
9551 * Make sure we're not on PC8 state before disabling PC8, otherwise
9552 * we'll hang the machine. To prevent PC8 state, just enable force_wake.
a8a8bd54 9553 */
59bad947 9554 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
215733fa 9555
be256dc7
PZ
9556 if (val & LCPLL_POWER_DOWN_ALLOW) {
9557 val &= ~LCPLL_POWER_DOWN_ALLOW;
9558 I915_WRITE(LCPLL_CTL, val);
35d8f2eb 9559 POSTING_READ(LCPLL_CTL);
be256dc7
PZ
9560 }
9561
9ccd5aeb 9562 val = hsw_read_dcomp(dev_priv);
be256dc7
PZ
9563 val |= D_COMP_COMP_FORCE;
9564 val &= ~D_COMP_COMP_DISABLE;
3c4c9b81 9565 hsw_write_dcomp(dev_priv, val);
be256dc7
PZ
9566
9567 val = I915_READ(LCPLL_CTL);
9568 val &= ~LCPLL_PLL_DISABLE;
9569 I915_WRITE(LCPLL_CTL, val);
9570
9571 if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
9572 DRM_ERROR("LCPLL not locked yet\n");
9573
9574 if (val & LCPLL_CD_SOURCE_FCLK) {
9575 val = I915_READ(LCPLL_CTL);
9576 val &= ~LCPLL_CD_SOURCE_FCLK;
9577 I915_WRITE(LCPLL_CTL, val);
9578
9579 if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
9580 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
9581 DRM_ERROR("Switching back to LCPLL failed\n");
9582 }
215733fa 9583
59bad947 9584 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
b6283055 9585 intel_update_cdclk(dev_priv->dev);
be256dc7
PZ
9586}
9587
765dab67
PZ
9588/*
9589 * Package states C8 and deeper are really deep PC states that can only be
9590 * reached when all the devices on the system allow it, so even if the graphics
9591 * device allows PC8+, it doesn't mean the system will actually get to these
9592 * states. Our driver only allows PC8+ when going into runtime PM.
9593 *
9594 * The requirements for PC8+ are that all the outputs are disabled, the power
9595 * well is disabled and most interrupts are disabled, and these are also
9596 * requirements for runtime PM. When these conditions are met, we manually do
9597 * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
9598 * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
9599 * hang the machine.
9600 *
9601 * When we really reach PC8 or deeper states (not just when we allow it) we lose
9602 * the state of some registers, so when we come back from PC8+ we need to
9603 * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
9604 * need to take care of the registers kept by RC6. Notice that this happens even
9605 * if we don't put the device in PCI D3 state (which is what currently happens
9606 * because of the runtime PM support).
9607 *
9608 * For more, read "Display Sequences for Package C8" on the hardware
9609 * documentation.
9610 */
a14cb6fc 9611void hsw_enable_pc8(struct drm_i915_private *dev_priv)
c67a470b 9612{
c67a470b
PZ
9613 struct drm_device *dev = dev_priv->dev;
9614 uint32_t val;
9615
c67a470b
PZ
9616 DRM_DEBUG_KMS("Enabling package C8+\n");
9617
c2699524 9618 if (HAS_PCH_LPT_LP(dev)) {
c67a470b
PZ
9619 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9620 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
9621 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9622 }
9623
9624 lpt_disable_clkout_dp(dev);
c67a470b
PZ
9625 hsw_disable_lcpll(dev_priv, true, true);
9626}
9627
a14cb6fc 9628void hsw_disable_pc8(struct drm_i915_private *dev_priv)
c67a470b
PZ
9629{
9630 struct drm_device *dev = dev_priv->dev;
9631 uint32_t val;
9632
c67a470b
PZ
9633 DRM_DEBUG_KMS("Disabling package C8+\n");
9634
9635 hsw_restore_lcpll(dev_priv);
c67a470b
PZ
9636 lpt_init_pch_refclk(dev);
9637
c2699524 9638 if (HAS_PCH_LPT_LP(dev)) {
c67a470b
PZ
9639 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9640 val |= PCH_LP_PARTITION_LEVEL_DISABLE;
9641 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9642 }
c67a470b
PZ
9643}
9644
27c329ed 9645static void broxton_modeset_commit_cdclk(struct drm_atomic_state *old_state)
f8437dd1 9646{
a821fc46 9647 struct drm_device *dev = old_state->dev;
1a617b77
ML
9648 struct intel_atomic_state *old_intel_state =
9649 to_intel_atomic_state(old_state);
9650 unsigned int req_cdclk = old_intel_state->dev_cdclk;
f8437dd1 9651
27c329ed 9652 broxton_set_cdclk(dev, req_cdclk);
f8437dd1
VK
9653}
9654
b432e5cf 9655/* compute the max rate for new configuration */
27c329ed 9656static int ilk_max_pixel_rate(struct drm_atomic_state *state)
b432e5cf 9657{
565602d7
ML
9658 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
9659 struct drm_i915_private *dev_priv = state->dev->dev_private;
9660 struct drm_crtc *crtc;
9661 struct drm_crtc_state *cstate;
27c329ed 9662 struct intel_crtc_state *crtc_state;
565602d7
ML
9663 unsigned max_pixel_rate = 0, i;
9664 enum pipe pipe;
b432e5cf 9665
565602d7
ML
9666 memcpy(intel_state->min_pixclk, dev_priv->min_pixclk,
9667 sizeof(intel_state->min_pixclk));
27c329ed 9668
565602d7
ML
9669 for_each_crtc_in_state(state, crtc, cstate, i) {
9670 int pixel_rate;
27c329ed 9671
565602d7
ML
9672 crtc_state = to_intel_crtc_state(cstate);
9673 if (!crtc_state->base.enable) {
9674 intel_state->min_pixclk[i] = 0;
b432e5cf 9675 continue;
565602d7 9676 }
b432e5cf 9677
27c329ed 9678 pixel_rate = ilk_pipe_pixel_rate(crtc_state);
b432e5cf
VS
9679
9680 /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
565602d7 9681 if (IS_BROADWELL(dev_priv) && crtc_state->ips_enabled)
b432e5cf
VS
9682 pixel_rate = DIV_ROUND_UP(pixel_rate * 100, 95);
9683
565602d7 9684 intel_state->min_pixclk[i] = pixel_rate;
b432e5cf
VS
9685 }
9686
565602d7
ML
9687 for_each_pipe(dev_priv, pipe)
9688 max_pixel_rate = max(intel_state->min_pixclk[pipe], max_pixel_rate);
9689
b432e5cf
VS
9690 return max_pixel_rate;
9691}
9692
9693static void broadwell_set_cdclk(struct drm_device *dev, int cdclk)
9694{
9695 struct drm_i915_private *dev_priv = dev->dev_private;
9696 uint32_t val, data;
9697 int ret;
9698
9699 if (WARN((I915_READ(LCPLL_CTL) &
9700 (LCPLL_PLL_DISABLE | LCPLL_PLL_LOCK |
9701 LCPLL_CD_CLOCK_DISABLE | LCPLL_ROOT_CD_CLOCK_DISABLE |
9702 LCPLL_CD2X_CLOCK_DISABLE | LCPLL_POWER_DOWN_ALLOW |
9703 LCPLL_CD_SOURCE_FCLK)) != LCPLL_PLL_LOCK,
9704 "trying to change cdclk frequency with cdclk not enabled\n"))
9705 return;
9706
9707 mutex_lock(&dev_priv->rps.hw_lock);
9708 ret = sandybridge_pcode_write(dev_priv,
9709 BDW_PCODE_DISPLAY_FREQ_CHANGE_REQ, 0x0);
9710 mutex_unlock(&dev_priv->rps.hw_lock);
9711 if (ret) {
9712 DRM_ERROR("failed to inform pcode about cdclk change\n");
9713 return;
9714 }
9715
9716 val = I915_READ(LCPLL_CTL);
9717 val |= LCPLL_CD_SOURCE_FCLK;
9718 I915_WRITE(LCPLL_CTL, val);
9719
5ba00178
TU
9720 if (wait_for_us(I915_READ(LCPLL_CTL) &
9721 LCPLL_CD_SOURCE_FCLK_DONE, 1))
b432e5cf
VS
9722 DRM_ERROR("Switching to FCLK failed\n");
9723
9724 val = I915_READ(LCPLL_CTL);
9725 val &= ~LCPLL_CLK_FREQ_MASK;
9726
9727 switch (cdclk) {
9728 case 450000:
9729 val |= LCPLL_CLK_FREQ_450;
9730 data = 0;
9731 break;
9732 case 540000:
9733 val |= LCPLL_CLK_FREQ_54O_BDW;
9734 data = 1;
9735 break;
9736 case 337500:
9737 val |= LCPLL_CLK_FREQ_337_5_BDW;
9738 data = 2;
9739 break;
9740 case 675000:
9741 val |= LCPLL_CLK_FREQ_675_BDW;
9742 data = 3;
9743 break;
9744 default:
9745 WARN(1, "invalid cdclk frequency\n");
9746 return;
9747 }
9748
9749 I915_WRITE(LCPLL_CTL, val);
9750
9751 val = I915_READ(LCPLL_CTL);
9752 val &= ~LCPLL_CD_SOURCE_FCLK;
9753 I915_WRITE(LCPLL_CTL, val);
9754
5ba00178
TU
9755 if (wait_for_us((I915_READ(LCPLL_CTL) &
9756 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
b432e5cf
VS
9757 DRM_ERROR("Switching back to LCPLL failed\n");
9758
9759 mutex_lock(&dev_priv->rps.hw_lock);
9760 sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ, data);
9761 mutex_unlock(&dev_priv->rps.hw_lock);
9762
9763 intel_update_cdclk(dev);
9764
9765 WARN(cdclk != dev_priv->cdclk_freq,
9766 "cdclk requested %d kHz but got %d kHz\n",
9767 cdclk, dev_priv->cdclk_freq);
9768}
9769
27c329ed 9770static int broadwell_modeset_calc_cdclk(struct drm_atomic_state *state)
b432e5cf 9771{
27c329ed 9772 struct drm_i915_private *dev_priv = to_i915(state->dev);
1a617b77 9773 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
27c329ed 9774 int max_pixclk = ilk_max_pixel_rate(state);
b432e5cf
VS
9775 int cdclk;
9776
9777 /*
9778 * FIXME should also account for plane ratio
9779 * once 64bpp pixel formats are supported.
9780 */
27c329ed 9781 if (max_pixclk > 540000)
b432e5cf 9782 cdclk = 675000;
27c329ed 9783 else if (max_pixclk > 450000)
b432e5cf 9784 cdclk = 540000;
27c329ed 9785 else if (max_pixclk > 337500)
b432e5cf
VS
9786 cdclk = 450000;
9787 else
9788 cdclk = 337500;
9789
b432e5cf 9790 if (cdclk > dev_priv->max_cdclk_freq) {
63ba534e
ML
9791 DRM_DEBUG_KMS("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
9792 cdclk, dev_priv->max_cdclk_freq);
9793 return -EINVAL;
b432e5cf
VS
9794 }
9795
1a617b77
ML
9796 intel_state->cdclk = intel_state->dev_cdclk = cdclk;
9797 if (!intel_state->active_crtcs)
9798 intel_state->dev_cdclk = 337500;
b432e5cf
VS
9799
9800 return 0;
9801}
9802
27c329ed 9803static void broadwell_modeset_commit_cdclk(struct drm_atomic_state *old_state)
b432e5cf 9804{
27c329ed 9805 struct drm_device *dev = old_state->dev;
1a617b77
ML
9806 struct intel_atomic_state *old_intel_state =
9807 to_intel_atomic_state(old_state);
9808 unsigned req_cdclk = old_intel_state->dev_cdclk;
b432e5cf 9809
27c329ed 9810 broadwell_set_cdclk(dev, req_cdclk);
b432e5cf
VS
9811}
9812
190f68c5
ACO
9813static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
9814 struct intel_crtc_state *crtc_state)
09b4ddf9 9815{
af3997b5
MK
9816 struct intel_encoder *intel_encoder =
9817 intel_ddi_get_crtc_new_encoder(crtc_state);
9818
9819 if (intel_encoder->type != INTEL_OUTPUT_DSI) {
9820 if (!intel_ddi_pll_select(crtc, crtc_state))
9821 return -EINVAL;
9822 }
716c2e55 9823
c7653199 9824 crtc->lowfreq_avail = false;
644cef34 9825
c8f7a0db 9826 return 0;
79e53945
JB
9827}
9828
3760b59c
S
9829static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
9830 enum port port,
9831 struct intel_crtc_state *pipe_config)
9832{
8106ddbd
ACO
9833 enum intel_dpll_id id;
9834
3760b59c
S
9835 switch (port) {
9836 case PORT_A:
9837 pipe_config->ddi_pll_sel = SKL_DPLL0;
08250c4b 9838 id = DPLL_ID_SKL_DPLL0;
3760b59c
S
9839 break;
9840 case PORT_B:
9841 pipe_config->ddi_pll_sel = SKL_DPLL1;
08250c4b 9842 id = DPLL_ID_SKL_DPLL1;
3760b59c
S
9843 break;
9844 case PORT_C:
9845 pipe_config->ddi_pll_sel = SKL_DPLL2;
08250c4b 9846 id = DPLL_ID_SKL_DPLL2;
3760b59c
S
9847 break;
9848 default:
9849 DRM_ERROR("Incorrect port type\n");
8106ddbd 9850 return;
3760b59c 9851 }
8106ddbd
ACO
9852
9853 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
3760b59c
S
9854}
9855
96b7dfb7
S
9856static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
9857 enum port port,
5cec258b 9858 struct intel_crtc_state *pipe_config)
96b7dfb7 9859{
8106ddbd 9860 enum intel_dpll_id id;
a3c988ea 9861 u32 temp;
96b7dfb7
S
9862
9863 temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
9864 pipe_config->ddi_pll_sel = temp >> (port * 3 + 1);
9865
9866 switch (pipe_config->ddi_pll_sel) {
3148ade7 9867 case SKL_DPLL0:
a3c988ea
ACO
9868 id = DPLL_ID_SKL_DPLL0;
9869 break;
96b7dfb7 9870 case SKL_DPLL1:
8106ddbd 9871 id = DPLL_ID_SKL_DPLL1;
96b7dfb7
S
9872 break;
9873 case SKL_DPLL2:
8106ddbd 9874 id = DPLL_ID_SKL_DPLL2;
96b7dfb7
S
9875 break;
9876 case SKL_DPLL3:
8106ddbd 9877 id = DPLL_ID_SKL_DPLL3;
96b7dfb7 9878 break;
8106ddbd
ACO
9879 default:
9880 MISSING_CASE(pipe_config->ddi_pll_sel);
9881 return;
96b7dfb7 9882 }
8106ddbd
ACO
9883
9884 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
96b7dfb7
S
9885}
9886
7d2c8175
DL
9887static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
9888 enum port port,
5cec258b 9889 struct intel_crtc_state *pipe_config)
7d2c8175 9890{
8106ddbd
ACO
9891 enum intel_dpll_id id;
9892
7d2c8175
DL
9893 pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
9894
9895 switch (pipe_config->ddi_pll_sel) {
9896 case PORT_CLK_SEL_WRPLL1:
8106ddbd 9897 id = DPLL_ID_WRPLL1;
7d2c8175
DL
9898 break;
9899 case PORT_CLK_SEL_WRPLL2:
8106ddbd 9900 id = DPLL_ID_WRPLL2;
7d2c8175 9901 break;
00490c22 9902 case PORT_CLK_SEL_SPLL:
8106ddbd 9903 id = DPLL_ID_SPLL;
79bd23da 9904 break;
9d16da65
ACO
9905 case PORT_CLK_SEL_LCPLL_810:
9906 id = DPLL_ID_LCPLL_810;
9907 break;
9908 case PORT_CLK_SEL_LCPLL_1350:
9909 id = DPLL_ID_LCPLL_1350;
9910 break;
9911 case PORT_CLK_SEL_LCPLL_2700:
9912 id = DPLL_ID_LCPLL_2700;
9913 break;
8106ddbd
ACO
9914 default:
9915 MISSING_CASE(pipe_config->ddi_pll_sel);
9916 /* fall through */
9917 case PORT_CLK_SEL_NONE:
8106ddbd 9918 return;
7d2c8175 9919 }
8106ddbd
ACO
9920
9921 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
7d2c8175
DL
9922}
9923
cf30429e
JN
9924static bool hsw_get_transcoder_state(struct intel_crtc *crtc,
9925 struct intel_crtc_state *pipe_config,
9926 unsigned long *power_domain_mask)
9927{
9928 struct drm_device *dev = crtc->base.dev;
9929 struct drm_i915_private *dev_priv = dev->dev_private;
9930 enum intel_display_power_domain power_domain;
9931 u32 tmp;
9932
9933 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
9934
9935 /*
9936 * XXX: Do intel_display_power_get_if_enabled before reading this (for
9937 * consistency and less surprising code; it's in always on power).
9938 */
9939 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
9940 if (tmp & TRANS_DDI_FUNC_ENABLE) {
9941 enum pipe trans_edp_pipe;
9942 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
9943 default:
9944 WARN(1, "unknown pipe linked to edp transcoder\n");
9945 case TRANS_DDI_EDP_INPUT_A_ONOFF:
9946 case TRANS_DDI_EDP_INPUT_A_ON:
9947 trans_edp_pipe = PIPE_A;
9948 break;
9949 case TRANS_DDI_EDP_INPUT_B_ONOFF:
9950 trans_edp_pipe = PIPE_B;
9951 break;
9952 case TRANS_DDI_EDP_INPUT_C_ONOFF:
9953 trans_edp_pipe = PIPE_C;
9954 break;
9955 }
9956
9957 if (trans_edp_pipe == crtc->pipe)
9958 pipe_config->cpu_transcoder = TRANSCODER_EDP;
9959 }
9960
9961 power_domain = POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder);
9962 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
9963 return false;
9964 *power_domain_mask |= BIT(power_domain);
9965
9966 tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
9967
9968 return tmp & PIPECONF_ENABLE;
9969}
9970
4d1de975
JN
9971static bool bxt_get_dsi_transcoder_state(struct intel_crtc *crtc,
9972 struct intel_crtc_state *pipe_config,
9973 unsigned long *power_domain_mask)
9974{
9975 struct drm_device *dev = crtc->base.dev;
9976 struct drm_i915_private *dev_priv = dev->dev_private;
9977 enum intel_display_power_domain power_domain;
9978 enum port port;
9979 enum transcoder cpu_transcoder;
9980 u32 tmp;
9981
9982 pipe_config->has_dsi_encoder = false;
9983
9984 for_each_port_masked(port, BIT(PORT_A) | BIT(PORT_C)) {
9985 if (port == PORT_A)
9986 cpu_transcoder = TRANSCODER_DSI_A;
9987 else
9988 cpu_transcoder = TRANSCODER_DSI_C;
9989
9990 power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
9991 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
9992 continue;
9993 *power_domain_mask |= BIT(power_domain);
9994
9995 /* XXX: this works for video mode only */
9996 tmp = I915_READ(BXT_MIPI_PORT_CTRL(port));
9997 if (!(tmp & DPI_ENABLE))
9998 continue;
9999
10000 tmp = I915_READ(MIPI_CTRL(port));
10001 if ((tmp & BXT_PIPE_SELECT_MASK) != BXT_PIPE_SELECT(crtc->pipe))
10002 continue;
10003
10004 pipe_config->cpu_transcoder = cpu_transcoder;
10005 pipe_config->has_dsi_encoder = true;
10006 break;
10007 }
10008
10009 return pipe_config->has_dsi_encoder;
10010}
10011
26804afd 10012static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
5cec258b 10013 struct intel_crtc_state *pipe_config)
26804afd
DV
10014{
10015 struct drm_device *dev = crtc->base.dev;
10016 struct drm_i915_private *dev_priv = dev->dev_private;
d452c5b6 10017 struct intel_shared_dpll *pll;
26804afd
DV
10018 enum port port;
10019 uint32_t tmp;
10020
10021 tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
10022
10023 port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
10024
ef11bdb3 10025 if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev))
96b7dfb7 10026 skylake_get_ddi_pll(dev_priv, port, pipe_config);
3760b59c
S
10027 else if (IS_BROXTON(dev))
10028 bxt_get_ddi_pll(dev_priv, port, pipe_config);
96b7dfb7
S
10029 else
10030 haswell_get_ddi_pll(dev_priv, port, pipe_config);
9cd86933 10031
8106ddbd
ACO
10032 pll = pipe_config->shared_dpll;
10033 if (pll) {
2edd6443
ACO
10034 WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
10035 &pipe_config->dpll_hw_state));
d452c5b6
DV
10036 }
10037
26804afd
DV
10038 /*
10039 * Haswell has only FDI/PCH transcoder A. It is which is connected to
10040 * DDI E. So just check whether this pipe is wired to DDI E and whether
10041 * the PCH transcoder is on.
10042 */
ca370455
DL
10043 if (INTEL_INFO(dev)->gen < 9 &&
10044 (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
26804afd
DV
10045 pipe_config->has_pch_encoder = true;
10046
10047 tmp = I915_READ(FDI_RX_CTL(PIPE_A));
10048 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
10049 FDI_DP_PORT_WIDTH_SHIFT) + 1;
10050
10051 ironlake_get_fdi_m_n_config(crtc, pipe_config);
10052 }
10053}
10054
0e8ffe1b 10055static bool haswell_get_pipe_config(struct intel_crtc *crtc,
5cec258b 10056 struct intel_crtc_state *pipe_config)
0e8ffe1b
DV
10057{
10058 struct drm_device *dev = crtc->base.dev;
10059 struct drm_i915_private *dev_priv = dev->dev_private;
1729050e
ID
10060 enum intel_display_power_domain power_domain;
10061 unsigned long power_domain_mask;
cf30429e 10062 bool active;
0e8ffe1b 10063
1729050e
ID
10064 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
10065 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
b5482bd0 10066 return false;
1729050e
ID
10067 power_domain_mask = BIT(power_domain);
10068
8106ddbd 10069 pipe_config->shared_dpll = NULL;
c0d43d62 10070
cf30429e 10071 active = hsw_get_transcoder_state(crtc, pipe_config, &power_domain_mask);
eccb140b 10072
4d1de975
JN
10073 if (IS_BROXTON(dev_priv)) {
10074 bxt_get_dsi_transcoder_state(crtc, pipe_config,
10075 &power_domain_mask);
10076 WARN_ON(active && pipe_config->has_dsi_encoder);
10077 if (pipe_config->has_dsi_encoder)
10078 active = true;
10079 }
10080
cf30429e 10081 if (!active)
1729050e 10082 goto out;
0e8ffe1b 10083
4d1de975
JN
10084 if (!pipe_config->has_dsi_encoder) {
10085 haswell_get_ddi_port_state(crtc, pipe_config);
10086 intel_get_pipe_timings(crtc, pipe_config);
10087 }
627eb5a3 10088
bc58be60 10089 intel_get_pipe_src_size(crtc, pipe_config);
1bd1bd80 10090
a1b2278e
CK
10091 if (INTEL_INFO(dev)->gen >= 9) {
10092 skl_init_scalers(dev, crtc, pipe_config);
10093 }
10094
af99ceda
CK
10095 if (INTEL_INFO(dev)->gen >= 9) {
10096 pipe_config->scaler_state.scaler_id = -1;
10097 pipe_config->scaler_state.scaler_users &= ~(1 << SKL_CRTC_INDEX);
10098 }
10099
1729050e
ID
10100 power_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
10101 if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
10102 power_domain_mask |= BIT(power_domain);
1c132b44 10103 if (INTEL_INFO(dev)->gen >= 9)
bd2e244f 10104 skylake_get_pfit_config(crtc, pipe_config);
ff6d9f55 10105 else
1c132b44 10106 ironlake_get_pfit_config(crtc, pipe_config);
bd2e244f 10107 }
88adfff1 10108
e59150dc
JB
10109 if (IS_HASWELL(dev))
10110 pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
10111 (I915_READ(IPS_CTL) & IPS_ENABLE);
42db64ef 10112
4d1de975
JN
10113 if (pipe_config->cpu_transcoder != TRANSCODER_EDP &&
10114 !transcoder_is_dsi(pipe_config->cpu_transcoder)) {
ebb69c95
CT
10115 pipe_config->pixel_multiplier =
10116 I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
10117 } else {
10118 pipe_config->pixel_multiplier = 1;
10119 }
6c49f241 10120
1729050e
ID
10121out:
10122 for_each_power_domain(power_domain, power_domain_mask)
10123 intel_display_power_put(dev_priv, power_domain);
10124
cf30429e 10125 return active;
0e8ffe1b
DV
10126}
10127
55a08b3f
ML
10128static void i845_update_cursor(struct drm_crtc *crtc, u32 base,
10129 const struct intel_plane_state *plane_state)
560b85bb
CW
10130{
10131 struct drm_device *dev = crtc->dev;
10132 struct drm_i915_private *dev_priv = dev->dev_private;
10133 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
dc41c154 10134 uint32_t cntl = 0, size = 0;
560b85bb 10135
55a08b3f
ML
10136 if (plane_state && plane_state->visible) {
10137 unsigned int width = plane_state->base.crtc_w;
10138 unsigned int height = plane_state->base.crtc_h;
dc41c154
VS
10139 unsigned int stride = roundup_pow_of_two(width) * 4;
10140
10141 switch (stride) {
10142 default:
10143 WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
10144 width, stride);
10145 stride = 256;
10146 /* fallthrough */
10147 case 256:
10148 case 512:
10149 case 1024:
10150 case 2048:
10151 break;
4b0e333e
CW
10152 }
10153
dc41c154
VS
10154 cntl |= CURSOR_ENABLE |
10155 CURSOR_GAMMA_ENABLE |
10156 CURSOR_FORMAT_ARGB |
10157 CURSOR_STRIDE(stride);
10158
10159 size = (height << 12) | width;
4b0e333e 10160 }
560b85bb 10161
dc41c154
VS
10162 if (intel_crtc->cursor_cntl != 0 &&
10163 (intel_crtc->cursor_base != base ||
10164 intel_crtc->cursor_size != size ||
10165 intel_crtc->cursor_cntl != cntl)) {
10166 /* On these chipsets we can only modify the base/size/stride
10167 * whilst the cursor is disabled.
10168 */
0b87c24e
VS
10169 I915_WRITE(CURCNTR(PIPE_A), 0);
10170 POSTING_READ(CURCNTR(PIPE_A));
dc41c154 10171 intel_crtc->cursor_cntl = 0;
4b0e333e 10172 }
560b85bb 10173
99d1f387 10174 if (intel_crtc->cursor_base != base) {
0b87c24e 10175 I915_WRITE(CURBASE(PIPE_A), base);
99d1f387
VS
10176 intel_crtc->cursor_base = base;
10177 }
4726e0b0 10178
dc41c154
VS
10179 if (intel_crtc->cursor_size != size) {
10180 I915_WRITE(CURSIZE, size);
10181 intel_crtc->cursor_size = size;
4b0e333e 10182 }
560b85bb 10183
4b0e333e 10184 if (intel_crtc->cursor_cntl != cntl) {
0b87c24e
VS
10185 I915_WRITE(CURCNTR(PIPE_A), cntl);
10186 POSTING_READ(CURCNTR(PIPE_A));
4b0e333e 10187 intel_crtc->cursor_cntl = cntl;
560b85bb 10188 }
560b85bb
CW
10189}
10190
55a08b3f
ML
10191static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base,
10192 const struct intel_plane_state *plane_state)
65a21cd6
JB
10193{
10194 struct drm_device *dev = crtc->dev;
10195 struct drm_i915_private *dev_priv = dev->dev_private;
10196 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10197 int pipe = intel_crtc->pipe;
663f3122 10198 uint32_t cntl = 0;
4b0e333e 10199
55a08b3f 10200 if (plane_state && plane_state->visible) {
4b0e333e 10201 cntl = MCURSOR_GAMMA_ENABLE;
55a08b3f 10202 switch (plane_state->base.crtc_w) {
4726e0b0
SK
10203 case 64:
10204 cntl |= CURSOR_MODE_64_ARGB_AX;
10205 break;
10206 case 128:
10207 cntl |= CURSOR_MODE_128_ARGB_AX;
10208 break;
10209 case 256:
10210 cntl |= CURSOR_MODE_256_ARGB_AX;
10211 break;
10212 default:
55a08b3f 10213 MISSING_CASE(plane_state->base.crtc_w);
4726e0b0 10214 return;
65a21cd6 10215 }
4b0e333e 10216 cntl |= pipe << 28; /* Connect to correct pipe */
47bf17a7 10217
fc6f93bc 10218 if (HAS_DDI(dev))
47bf17a7 10219 cntl |= CURSOR_PIPE_CSC_ENABLE;
65a21cd6 10220
55a08b3f
ML
10221 if (plane_state->base.rotation == BIT(DRM_ROTATE_180))
10222 cntl |= CURSOR_ROTATE_180;
10223 }
4398ad45 10224
4b0e333e
CW
10225 if (intel_crtc->cursor_cntl != cntl) {
10226 I915_WRITE(CURCNTR(pipe), cntl);
10227 POSTING_READ(CURCNTR(pipe));
10228 intel_crtc->cursor_cntl = cntl;
65a21cd6 10229 }
4b0e333e 10230
65a21cd6 10231 /* and commit changes on next vblank */
5efb3e28
VS
10232 I915_WRITE(CURBASE(pipe), base);
10233 POSTING_READ(CURBASE(pipe));
99d1f387
VS
10234
10235 intel_crtc->cursor_base = base;
65a21cd6
JB
10236}
10237
cda4b7d3 10238/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
6b383a7f 10239static void intel_crtc_update_cursor(struct drm_crtc *crtc,
55a08b3f 10240 const struct intel_plane_state *plane_state)
cda4b7d3
CW
10241{
10242 struct drm_device *dev = crtc->dev;
10243 struct drm_i915_private *dev_priv = dev->dev_private;
10244 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10245 int pipe = intel_crtc->pipe;
55a08b3f
ML
10246 u32 base = intel_crtc->cursor_addr;
10247 u32 pos = 0;
cda4b7d3 10248
55a08b3f
ML
10249 if (plane_state) {
10250 int x = plane_state->base.crtc_x;
10251 int y = plane_state->base.crtc_y;
cda4b7d3 10252
55a08b3f
ML
10253 if (x < 0) {
10254 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
10255 x = -x;
10256 }
10257 pos |= x << CURSOR_X_SHIFT;
cda4b7d3 10258
55a08b3f
ML
10259 if (y < 0) {
10260 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
10261 y = -y;
10262 }
10263 pos |= y << CURSOR_Y_SHIFT;
10264
10265 /* ILK+ do this automagically */
10266 if (HAS_GMCH_DISPLAY(dev) &&
10267 plane_state->base.rotation == BIT(DRM_ROTATE_180)) {
10268 base += (plane_state->base.crtc_h *
10269 plane_state->base.crtc_w - 1) * 4;
10270 }
cda4b7d3 10271 }
cda4b7d3 10272
5efb3e28
VS
10273 I915_WRITE(CURPOS(pipe), pos);
10274
8ac54669 10275 if (IS_845G(dev) || IS_I865G(dev))
55a08b3f 10276 i845_update_cursor(crtc, base, plane_state);
5efb3e28 10277 else
55a08b3f 10278 i9xx_update_cursor(crtc, base, plane_state);
cda4b7d3
CW
10279}
10280
dc41c154
VS
10281static bool cursor_size_ok(struct drm_device *dev,
10282 uint32_t width, uint32_t height)
10283{
10284 if (width == 0 || height == 0)
10285 return false;
10286
10287 /*
10288 * 845g/865g are special in that they are only limited by
10289 * the width of their cursors, the height is arbitrary up to
10290 * the precision of the register. Everything else requires
10291 * square cursors, limited to a few power-of-two sizes.
10292 */
10293 if (IS_845G(dev) || IS_I865G(dev)) {
10294 if ((width & 63) != 0)
10295 return false;
10296
10297 if (width > (IS_845G(dev) ? 64 : 512))
10298 return false;
10299
10300 if (height > 1023)
10301 return false;
10302 } else {
10303 switch (width | height) {
10304 case 256:
10305 case 128:
10306 if (IS_GEN2(dev))
10307 return false;
10308 case 64:
10309 break;
10310 default:
10311 return false;
10312 }
10313 }
10314
10315 return true;
10316}
10317
79e53945 10318static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
7203425a 10319 u16 *blue, uint32_t start, uint32_t size)
79e53945 10320{
7203425a 10321 int end = (start + size > 256) ? 256 : start + size, i;
79e53945 10322 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
79e53945 10323
7203425a 10324 for (i = start; i < end; i++) {
79e53945
JB
10325 intel_crtc->lut_r[i] = red[i] >> 8;
10326 intel_crtc->lut_g[i] = green[i] >> 8;
10327 intel_crtc->lut_b[i] = blue[i] >> 8;
10328 }
10329
10330 intel_crtc_load_lut(crtc);
10331}
10332
79e53945
JB
10333/* VESA 640x480x72Hz mode to set on the pipe */
10334static struct drm_display_mode load_detect_mode = {
10335 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
10336 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
10337};
10338
a8bb6818
DV
10339struct drm_framebuffer *
10340__intel_framebuffer_create(struct drm_device *dev,
10341 struct drm_mode_fb_cmd2 *mode_cmd,
10342 struct drm_i915_gem_object *obj)
d2dff872
CW
10343{
10344 struct intel_framebuffer *intel_fb;
10345 int ret;
10346
10347 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
dcb1394e 10348 if (!intel_fb)
d2dff872 10349 return ERR_PTR(-ENOMEM);
d2dff872
CW
10350
10351 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
dd4916c5
DV
10352 if (ret)
10353 goto err;
d2dff872
CW
10354
10355 return &intel_fb->base;
dcb1394e 10356
dd4916c5 10357err:
dd4916c5 10358 kfree(intel_fb);
dd4916c5 10359 return ERR_PTR(ret);
d2dff872
CW
10360}
10361
b5ea642a 10362static struct drm_framebuffer *
a8bb6818
DV
10363intel_framebuffer_create(struct drm_device *dev,
10364 struct drm_mode_fb_cmd2 *mode_cmd,
10365 struct drm_i915_gem_object *obj)
10366{
10367 struct drm_framebuffer *fb;
10368 int ret;
10369
10370 ret = i915_mutex_lock_interruptible(dev);
10371 if (ret)
10372 return ERR_PTR(ret);
10373 fb = __intel_framebuffer_create(dev, mode_cmd, obj);
10374 mutex_unlock(&dev->struct_mutex);
10375
10376 return fb;
10377}
10378
d2dff872
CW
10379static u32
10380intel_framebuffer_pitch_for_width(int width, int bpp)
10381{
10382 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
10383 return ALIGN(pitch, 64);
10384}
10385
10386static u32
10387intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
10388{
10389 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
1267a26b 10390 return PAGE_ALIGN(pitch * mode->vdisplay);
d2dff872
CW
10391}
10392
10393static struct drm_framebuffer *
10394intel_framebuffer_create_for_mode(struct drm_device *dev,
10395 struct drm_display_mode *mode,
10396 int depth, int bpp)
10397{
dcb1394e 10398 struct drm_framebuffer *fb;
d2dff872 10399 struct drm_i915_gem_object *obj;
0fed39bd 10400 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
d2dff872
CW
10401
10402 obj = i915_gem_alloc_object(dev,
10403 intel_framebuffer_size_for_mode(mode, bpp));
10404 if (obj == NULL)
10405 return ERR_PTR(-ENOMEM);
10406
10407 mode_cmd.width = mode->hdisplay;
10408 mode_cmd.height = mode->vdisplay;
308e5bcb
JB
10409 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
10410 bpp);
5ca0c34a 10411 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
d2dff872 10412
dcb1394e
LW
10413 fb = intel_framebuffer_create(dev, &mode_cmd, obj);
10414 if (IS_ERR(fb))
10415 drm_gem_object_unreference_unlocked(&obj->base);
10416
10417 return fb;
d2dff872
CW
10418}
10419
10420static struct drm_framebuffer *
10421mode_fits_in_fbdev(struct drm_device *dev,
10422 struct drm_display_mode *mode)
10423{
0695726e 10424#ifdef CONFIG_DRM_FBDEV_EMULATION
d2dff872
CW
10425 struct drm_i915_private *dev_priv = dev->dev_private;
10426 struct drm_i915_gem_object *obj;
10427 struct drm_framebuffer *fb;
10428
4c0e5528 10429 if (!dev_priv->fbdev)
d2dff872
CW
10430 return NULL;
10431
4c0e5528 10432 if (!dev_priv->fbdev->fb)
d2dff872
CW
10433 return NULL;
10434
4c0e5528
DV
10435 obj = dev_priv->fbdev->fb->obj;
10436 BUG_ON(!obj);
10437
8bcd4553 10438 fb = &dev_priv->fbdev->fb->base;
01f2c773
VS
10439 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
10440 fb->bits_per_pixel))
d2dff872
CW
10441 return NULL;
10442
01f2c773 10443 if (obj->base.size < mode->vdisplay * fb->pitches[0])
d2dff872
CW
10444 return NULL;
10445
edde3617 10446 drm_framebuffer_reference(fb);
d2dff872 10447 return fb;
4520f53a
DV
10448#else
10449 return NULL;
10450#endif
d2dff872
CW
10451}
10452
d3a40d1b
ACO
10453static int intel_modeset_setup_plane_state(struct drm_atomic_state *state,
10454 struct drm_crtc *crtc,
10455 struct drm_display_mode *mode,
10456 struct drm_framebuffer *fb,
10457 int x, int y)
10458{
10459 struct drm_plane_state *plane_state;
10460 int hdisplay, vdisplay;
10461 int ret;
10462
10463 plane_state = drm_atomic_get_plane_state(state, crtc->primary);
10464 if (IS_ERR(plane_state))
10465 return PTR_ERR(plane_state);
10466
10467 if (mode)
10468 drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
10469 else
10470 hdisplay = vdisplay = 0;
10471
10472 ret = drm_atomic_set_crtc_for_plane(plane_state, fb ? crtc : NULL);
10473 if (ret)
10474 return ret;
10475 drm_atomic_set_fb_for_plane(plane_state, fb);
10476 plane_state->crtc_x = 0;
10477 plane_state->crtc_y = 0;
10478 plane_state->crtc_w = hdisplay;
10479 plane_state->crtc_h = vdisplay;
10480 plane_state->src_x = x << 16;
10481 plane_state->src_y = y << 16;
10482 plane_state->src_w = hdisplay << 16;
10483 plane_state->src_h = vdisplay << 16;
10484
10485 return 0;
10486}
10487
d2434ab7 10488bool intel_get_load_detect_pipe(struct drm_connector *connector,
7173188d 10489 struct drm_display_mode *mode,
51fd371b
RC
10490 struct intel_load_detect_pipe *old,
10491 struct drm_modeset_acquire_ctx *ctx)
79e53945
JB
10492{
10493 struct intel_crtc *intel_crtc;
d2434ab7
DV
10494 struct intel_encoder *intel_encoder =
10495 intel_attached_encoder(connector);
79e53945 10496 struct drm_crtc *possible_crtc;
4ef69c7a 10497 struct drm_encoder *encoder = &intel_encoder->base;
79e53945
JB
10498 struct drm_crtc *crtc = NULL;
10499 struct drm_device *dev = encoder->dev;
94352cf9 10500 struct drm_framebuffer *fb;
51fd371b 10501 struct drm_mode_config *config = &dev->mode_config;
edde3617 10502 struct drm_atomic_state *state = NULL, *restore_state = NULL;
944b0c76 10503 struct drm_connector_state *connector_state;
4be07317 10504 struct intel_crtc_state *crtc_state;
51fd371b 10505 int ret, i = -1;
79e53945 10506
d2dff872 10507 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
c23cc417 10508 connector->base.id, connector->name,
8e329a03 10509 encoder->base.id, encoder->name);
d2dff872 10510
edde3617
ML
10511 old->restore_state = NULL;
10512
51fd371b
RC
10513retry:
10514 ret = drm_modeset_lock(&config->connection_mutex, ctx);
10515 if (ret)
ad3c558f 10516 goto fail;
6e9f798d 10517
79e53945
JB
10518 /*
10519 * Algorithm gets a little messy:
7a5e4805 10520 *
79e53945
JB
10521 * - if the connector already has an assigned crtc, use it (but make
10522 * sure it's on first)
7a5e4805 10523 *
79e53945
JB
10524 * - try to find the first unused crtc that can drive this connector,
10525 * and use that if we find one
79e53945
JB
10526 */
10527
10528 /* See if we already have a CRTC for this connector */
edde3617
ML
10529 if (connector->state->crtc) {
10530 crtc = connector->state->crtc;
8261b191 10531
51fd371b 10532 ret = drm_modeset_lock(&crtc->mutex, ctx);
4d02e2de 10533 if (ret)
ad3c558f 10534 goto fail;
8261b191
CW
10535
10536 /* Make sure the crtc and connector are running */
edde3617 10537 goto found;
79e53945
JB
10538 }
10539
10540 /* Find an unused one (if possible) */
70e1e0ec 10541 for_each_crtc(dev, possible_crtc) {
79e53945
JB
10542 i++;
10543 if (!(encoder->possible_crtcs & (1 << i)))
10544 continue;
edde3617
ML
10545
10546 ret = drm_modeset_lock(&possible_crtc->mutex, ctx);
10547 if (ret)
10548 goto fail;
10549
10550 if (possible_crtc->state->enable) {
10551 drm_modeset_unlock(&possible_crtc->mutex);
a459249c 10552 continue;
edde3617 10553 }
a459249c
VS
10554
10555 crtc = possible_crtc;
10556 break;
79e53945
JB
10557 }
10558
10559 /*
10560 * If we didn't find an unused CRTC, don't use any.
10561 */
10562 if (!crtc) {
7173188d 10563 DRM_DEBUG_KMS("no pipe available for load-detect\n");
ad3c558f 10564 goto fail;
79e53945
JB
10565 }
10566
edde3617
ML
10567found:
10568 intel_crtc = to_intel_crtc(crtc);
10569
4d02e2de
DV
10570 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
10571 if (ret)
ad3c558f 10572 goto fail;
79e53945 10573
83a57153 10574 state = drm_atomic_state_alloc(dev);
edde3617
ML
10575 restore_state = drm_atomic_state_alloc(dev);
10576 if (!state || !restore_state) {
10577 ret = -ENOMEM;
10578 goto fail;
10579 }
83a57153
ACO
10580
10581 state->acquire_ctx = ctx;
edde3617 10582 restore_state->acquire_ctx = ctx;
83a57153 10583
944b0c76
ACO
10584 connector_state = drm_atomic_get_connector_state(state, connector);
10585 if (IS_ERR(connector_state)) {
10586 ret = PTR_ERR(connector_state);
10587 goto fail;
10588 }
10589
edde3617
ML
10590 ret = drm_atomic_set_crtc_for_connector(connector_state, crtc);
10591 if (ret)
10592 goto fail;
944b0c76 10593
4be07317
ACO
10594 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
10595 if (IS_ERR(crtc_state)) {
10596 ret = PTR_ERR(crtc_state);
10597 goto fail;
10598 }
10599
49d6fa21 10600 crtc_state->base.active = crtc_state->base.enable = true;
4be07317 10601
6492711d
CW
10602 if (!mode)
10603 mode = &load_detect_mode;
79e53945 10604
d2dff872
CW
10605 /* We need a framebuffer large enough to accommodate all accesses
10606 * that the plane may generate whilst we perform load detection.
10607 * We can not rely on the fbcon either being present (we get called
10608 * during its initialisation to detect all boot displays, or it may
10609 * not even exist) or that it is large enough to satisfy the
10610 * requested mode.
10611 */
94352cf9
DV
10612 fb = mode_fits_in_fbdev(dev, mode);
10613 if (fb == NULL) {
d2dff872 10614 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
94352cf9 10615 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
d2dff872
CW
10616 } else
10617 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
94352cf9 10618 if (IS_ERR(fb)) {
d2dff872 10619 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
412b61d8 10620 goto fail;
79e53945 10621 }
79e53945 10622
d3a40d1b
ACO
10623 ret = intel_modeset_setup_plane_state(state, crtc, mode, fb, 0, 0);
10624 if (ret)
10625 goto fail;
10626
edde3617
ML
10627 drm_framebuffer_unreference(fb);
10628
10629 ret = drm_atomic_set_mode_for_crtc(&crtc_state->base, mode);
10630 if (ret)
10631 goto fail;
10632
10633 ret = PTR_ERR_OR_ZERO(drm_atomic_get_connector_state(restore_state, connector));
10634 if (!ret)
10635 ret = PTR_ERR_OR_ZERO(drm_atomic_get_crtc_state(restore_state, crtc));
10636 if (!ret)
10637 ret = PTR_ERR_OR_ZERO(drm_atomic_get_plane_state(restore_state, crtc->primary));
10638 if (ret) {
10639 DRM_DEBUG_KMS("Failed to create a copy of old state to restore: %i\n", ret);
10640 goto fail;
10641 }
8c7b5ccb 10642
3ba86073
ML
10643 ret = drm_atomic_commit(state);
10644 if (ret) {
6492711d 10645 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
412b61d8 10646 goto fail;
79e53945 10647 }
edde3617
ML
10648
10649 old->restore_state = restore_state;
7173188d 10650
79e53945 10651 /* let the connector get through one full cycle before testing */
9d0498a2 10652 intel_wait_for_vblank(dev, intel_crtc->pipe);
7173188d 10653 return true;
412b61d8 10654
ad3c558f 10655fail:
e5d958ef 10656 drm_atomic_state_free(state);
edde3617
ML
10657 drm_atomic_state_free(restore_state);
10658 restore_state = state = NULL;
83a57153 10659
51fd371b
RC
10660 if (ret == -EDEADLK) {
10661 drm_modeset_backoff(ctx);
10662 goto retry;
10663 }
10664
412b61d8 10665 return false;
79e53945
JB
10666}
10667
d2434ab7 10668void intel_release_load_detect_pipe(struct drm_connector *connector,
49172fee
ACO
10669 struct intel_load_detect_pipe *old,
10670 struct drm_modeset_acquire_ctx *ctx)
79e53945 10671{
d2434ab7
DV
10672 struct intel_encoder *intel_encoder =
10673 intel_attached_encoder(connector);
4ef69c7a 10674 struct drm_encoder *encoder = &intel_encoder->base;
edde3617 10675 struct drm_atomic_state *state = old->restore_state;
d3a40d1b 10676 int ret;
79e53945 10677
d2dff872 10678 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
c23cc417 10679 connector->base.id, connector->name,
8e329a03 10680 encoder->base.id, encoder->name);
d2dff872 10681
edde3617 10682 if (!state)
0622a53c 10683 return;
79e53945 10684
edde3617
ML
10685 ret = drm_atomic_commit(state);
10686 if (ret) {
10687 DRM_DEBUG_KMS("Couldn't release load detect pipe: %i\n", ret);
10688 drm_atomic_state_free(state);
10689 }
79e53945
JB
10690}
10691
da4a1efa 10692static int i9xx_pll_refclk(struct drm_device *dev,
5cec258b 10693 const struct intel_crtc_state *pipe_config)
da4a1efa
VS
10694{
10695 struct drm_i915_private *dev_priv = dev->dev_private;
10696 u32 dpll = pipe_config->dpll_hw_state.dpll;
10697
10698 if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
e91e941b 10699 return dev_priv->vbt.lvds_ssc_freq;
da4a1efa
VS
10700 else if (HAS_PCH_SPLIT(dev))
10701 return 120000;
10702 else if (!IS_GEN2(dev))
10703 return 96000;
10704 else
10705 return 48000;
10706}
10707
79e53945 10708/* Returns the clock of the currently programmed mode of the given pipe. */
f1f644dc 10709static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 10710 struct intel_crtc_state *pipe_config)
79e53945 10711{
f1f644dc 10712 struct drm_device *dev = crtc->base.dev;
79e53945 10713 struct drm_i915_private *dev_priv = dev->dev_private;
f1f644dc 10714 int pipe = pipe_config->cpu_transcoder;
293623f7 10715 u32 dpll = pipe_config->dpll_hw_state.dpll;
79e53945
JB
10716 u32 fp;
10717 intel_clock_t clock;
dccbea3b 10718 int port_clock;
da4a1efa 10719 int refclk = i9xx_pll_refclk(dev, pipe_config);
79e53945
JB
10720
10721 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
293623f7 10722 fp = pipe_config->dpll_hw_state.fp0;
79e53945 10723 else
293623f7 10724 fp = pipe_config->dpll_hw_state.fp1;
79e53945
JB
10725
10726 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
f2b115e6
AJ
10727 if (IS_PINEVIEW(dev)) {
10728 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
10729 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
2177832f
SL
10730 } else {
10731 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
10732 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
10733 }
10734
a6c45cf0 10735 if (!IS_GEN2(dev)) {
f2b115e6
AJ
10736 if (IS_PINEVIEW(dev))
10737 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
10738 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
2177832f
SL
10739 else
10740 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
79e53945
JB
10741 DPLL_FPA01_P1_POST_DIV_SHIFT);
10742
10743 switch (dpll & DPLL_MODE_MASK) {
10744 case DPLLB_MODE_DAC_SERIAL:
10745 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
10746 5 : 10;
10747 break;
10748 case DPLLB_MODE_LVDS:
10749 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
10750 7 : 14;
10751 break;
10752 default:
28c97730 10753 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
79e53945 10754 "mode\n", (int)(dpll & DPLL_MODE_MASK));
f1f644dc 10755 return;
79e53945
JB
10756 }
10757
ac58c3f0 10758 if (IS_PINEVIEW(dev))
dccbea3b 10759 port_clock = pnv_calc_dpll_params(refclk, &clock);
ac58c3f0 10760 else
dccbea3b 10761 port_clock = i9xx_calc_dpll_params(refclk, &clock);
79e53945 10762 } else {
0fb58223 10763 u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
b1c560d1 10764 bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
79e53945
JB
10765
10766 if (is_lvds) {
10767 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
10768 DPLL_FPA01_P1_POST_DIV_SHIFT);
b1c560d1
VS
10769
10770 if (lvds & LVDS_CLKB_POWER_UP)
10771 clock.p2 = 7;
10772 else
10773 clock.p2 = 14;
79e53945
JB
10774 } else {
10775 if (dpll & PLL_P1_DIVIDE_BY_TWO)
10776 clock.p1 = 2;
10777 else {
10778 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
10779 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
10780 }
10781 if (dpll & PLL_P2_DIVIDE_BY_4)
10782 clock.p2 = 4;
10783 else
10784 clock.p2 = 2;
79e53945 10785 }
da4a1efa 10786
dccbea3b 10787 port_clock = i9xx_calc_dpll_params(refclk, &clock);
79e53945
JB
10788 }
10789
18442d08
VS
10790 /*
10791 * This value includes pixel_multiplier. We will use
241bfc38 10792 * port_clock to compute adjusted_mode.crtc_clock in the
18442d08
VS
10793 * encoder's get_config() function.
10794 */
dccbea3b 10795 pipe_config->port_clock = port_clock;
f1f644dc
JB
10796}
10797
6878da05
VS
10798int intel_dotclock_calculate(int link_freq,
10799 const struct intel_link_m_n *m_n)
f1f644dc 10800{
f1f644dc
JB
10801 /*
10802 * The calculation for the data clock is:
1041a02f 10803 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
f1f644dc 10804 * But we want to avoid losing precison if possible, so:
1041a02f 10805 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
f1f644dc
JB
10806 *
10807 * and the link clock is simpler:
1041a02f 10808 * link_clock = (m * link_clock) / n
f1f644dc
JB
10809 */
10810
6878da05
VS
10811 if (!m_n->link_n)
10812 return 0;
f1f644dc 10813
6878da05
VS
10814 return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
10815}
f1f644dc 10816
18442d08 10817static void ironlake_pch_clock_get(struct intel_crtc *crtc,
5cec258b 10818 struct intel_crtc_state *pipe_config)
6878da05 10819{
e3b247da 10820 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
79e53945 10821
18442d08
VS
10822 /* read out port_clock from the DPLL */
10823 i9xx_crtc_clock_get(crtc, pipe_config);
f1f644dc 10824
f1f644dc 10825 /*
e3b247da
VS
10826 * In case there is an active pipe without active ports,
10827 * we may need some idea for the dotclock anyway.
10828 * Calculate one based on the FDI configuration.
79e53945 10829 */
2d112de7 10830 pipe_config->base.adjusted_mode.crtc_clock =
21a727b3 10831 intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
18442d08 10832 &pipe_config->fdi_m_n);
79e53945
JB
10833}
10834
10835/** Returns the currently programmed mode of the given pipe. */
10836struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
10837 struct drm_crtc *crtc)
10838{
548f245b 10839 struct drm_i915_private *dev_priv = dev->dev_private;
79e53945 10840 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6e3c9717 10841 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
79e53945 10842 struct drm_display_mode *mode;
3f36b937 10843 struct intel_crtc_state *pipe_config;
fe2b8f9d
PZ
10844 int htot = I915_READ(HTOTAL(cpu_transcoder));
10845 int hsync = I915_READ(HSYNC(cpu_transcoder));
10846 int vtot = I915_READ(VTOTAL(cpu_transcoder));
10847 int vsync = I915_READ(VSYNC(cpu_transcoder));
293623f7 10848 enum pipe pipe = intel_crtc->pipe;
79e53945
JB
10849
10850 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
10851 if (!mode)
10852 return NULL;
10853
3f36b937
TU
10854 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
10855 if (!pipe_config) {
10856 kfree(mode);
10857 return NULL;
10858 }
10859
f1f644dc
JB
10860 /*
10861 * Construct a pipe_config sufficient for getting the clock info
10862 * back out of crtc_clock_get.
10863 *
10864 * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
10865 * to use a real value here instead.
10866 */
3f36b937
TU
10867 pipe_config->cpu_transcoder = (enum transcoder) pipe;
10868 pipe_config->pixel_multiplier = 1;
10869 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(pipe));
10870 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(pipe));
10871 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(pipe));
10872 i9xx_crtc_clock_get(intel_crtc, pipe_config);
10873
10874 mode->clock = pipe_config->port_clock / pipe_config->pixel_multiplier;
79e53945
JB
10875 mode->hdisplay = (htot & 0xffff) + 1;
10876 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
10877 mode->hsync_start = (hsync & 0xffff) + 1;
10878 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
10879 mode->vdisplay = (vtot & 0xffff) + 1;
10880 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
10881 mode->vsync_start = (vsync & 0xffff) + 1;
10882 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
10883
10884 drm_mode_set_name(mode);
79e53945 10885
3f36b937
TU
10886 kfree(pipe_config);
10887
79e53945
JB
10888 return mode;
10889}
10890
f047e395
CW
10891void intel_mark_busy(struct drm_device *dev)
10892{
c67a470b
PZ
10893 struct drm_i915_private *dev_priv = dev->dev_private;
10894
f62a0076
CW
10895 if (dev_priv->mm.busy)
10896 return;
10897
43694d69 10898 intel_runtime_pm_get(dev_priv);
c67a470b 10899 i915_update_gfx_val(dev_priv);
43cf3bf0
CW
10900 if (INTEL_INFO(dev)->gen >= 6)
10901 gen6_rps_busy(dev_priv);
f62a0076 10902 dev_priv->mm.busy = true;
f047e395
CW
10903}
10904
10905void intel_mark_idle(struct drm_device *dev)
652c393a 10906{
c67a470b 10907 struct drm_i915_private *dev_priv = dev->dev_private;
652c393a 10908
f62a0076
CW
10909 if (!dev_priv->mm.busy)
10910 return;
10911
10912 dev_priv->mm.busy = false;
10913
3d13ef2e 10914 if (INTEL_INFO(dev)->gen >= 6)
b29c19b6 10915 gen6_rps_idle(dev->dev_private);
bb4cdd53 10916
43694d69 10917 intel_runtime_pm_put(dev_priv);
652c393a
JB
10918}
10919
79e53945
JB
10920static void intel_crtc_destroy(struct drm_crtc *crtc)
10921{
10922 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
67e77c5a
DV
10923 struct drm_device *dev = crtc->dev;
10924 struct intel_unpin_work *work;
67e77c5a 10925
5e2d7afc 10926 spin_lock_irq(&dev->event_lock);
67e77c5a
DV
10927 work = intel_crtc->unpin_work;
10928 intel_crtc->unpin_work = NULL;
5e2d7afc 10929 spin_unlock_irq(&dev->event_lock);
67e77c5a
DV
10930
10931 if (work) {
10932 cancel_work_sync(&work->work);
10933 kfree(work);
10934 }
79e53945
JB
10935
10936 drm_crtc_cleanup(crtc);
67e77c5a 10937
79e53945
JB
10938 kfree(intel_crtc);
10939}
10940
6b95a207
KH
10941static void intel_unpin_work_fn(struct work_struct *__work)
10942{
10943 struct intel_unpin_work *work =
10944 container_of(__work, struct intel_unpin_work, work);
a9ff8714
VS
10945 struct intel_crtc *crtc = to_intel_crtc(work->crtc);
10946 struct drm_device *dev = crtc->base.dev;
10947 struct drm_plane *primary = crtc->base.primary;
6b95a207 10948
b4a98e57 10949 mutex_lock(&dev->struct_mutex);
3465c580 10950 intel_unpin_fb_obj(work->old_fb, primary->state->rotation);
05394f39 10951 drm_gem_object_unreference(&work->pending_flip_obj->base);
d9e86c0e 10952
f06cc1b9 10953 if (work->flip_queued_req)
146d84f0 10954 i915_gem_request_assign(&work->flip_queued_req, NULL);
b4a98e57
CW
10955 mutex_unlock(&dev->struct_mutex);
10956
a9ff8714 10957 intel_frontbuffer_flip_complete(dev, to_intel_plane(primary)->frontbuffer_bit);
1eb52238 10958 intel_fbc_post_update(crtc);
89ed88ba 10959 drm_framebuffer_unreference(work->old_fb);
f99d7069 10960
a9ff8714
VS
10961 BUG_ON(atomic_read(&crtc->unpin_work_count) == 0);
10962 atomic_dec(&crtc->unpin_work_count);
b4a98e57 10963
6b95a207
KH
10964 kfree(work);
10965}
10966
1afe3e9d 10967static void do_intel_finish_page_flip(struct drm_device *dev,
49b14a5c 10968 struct drm_crtc *crtc)
6b95a207 10969{
6b95a207
KH
10970 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10971 struct intel_unpin_work *work;
6b95a207
KH
10972 unsigned long flags;
10973
10974 /* Ignore early vblank irqs */
10975 if (intel_crtc == NULL)
10976 return;
10977
f326038a
DV
10978 /*
10979 * This is called both by irq handlers and the reset code (to complete
10980 * lost pageflips) so needs the full irqsave spinlocks.
10981 */
6b95a207
KH
10982 spin_lock_irqsave(&dev->event_lock, flags);
10983 work = intel_crtc->unpin_work;
e7d841ca
CW
10984
10985 /* Ensure we don't miss a work->pending update ... */
10986 smp_rmb();
10987
10988 if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
6b95a207
KH
10989 spin_unlock_irqrestore(&dev->event_lock, flags);
10990 return;
10991 }
10992
d6bbafa1 10993 page_flip_completed(intel_crtc);
0af7e4df 10994
6b95a207 10995 spin_unlock_irqrestore(&dev->event_lock, flags);
6b95a207
KH
10996}
10997
1afe3e9d
JB
10998void intel_finish_page_flip(struct drm_device *dev, int pipe)
10999{
fbee40df 11000 struct drm_i915_private *dev_priv = dev->dev_private;
1afe3e9d
JB
11001 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
11002
49b14a5c 11003 do_intel_finish_page_flip(dev, crtc);
1afe3e9d
JB
11004}
11005
11006void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
11007{
fbee40df 11008 struct drm_i915_private *dev_priv = dev->dev_private;
1afe3e9d
JB
11009 struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
11010
49b14a5c 11011 do_intel_finish_page_flip(dev, crtc);
1afe3e9d
JB
11012}
11013
75f7f3ec
VS
11014/* Is 'a' after or equal to 'b'? */
11015static bool g4x_flip_count_after_eq(u32 a, u32 b)
11016{
11017 return !((a - b) & 0x80000000);
11018}
11019
11020static bool page_flip_finished(struct intel_crtc *crtc)
11021{
11022 struct drm_device *dev = crtc->base.dev;
11023 struct drm_i915_private *dev_priv = dev->dev_private;
11024
bdfa7542
VS
11025 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
11026 crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
11027 return true;
11028
75f7f3ec
VS
11029 /*
11030 * The relevant registers doen't exist on pre-ctg.
11031 * As the flip done interrupt doesn't trigger for mmio
11032 * flips on gmch platforms, a flip count check isn't
11033 * really needed there. But since ctg has the registers,
11034 * include it in the check anyway.
11035 */
11036 if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
11037 return true;
11038
e8861675
ML
11039 /*
11040 * BDW signals flip done immediately if the plane
11041 * is disabled, even if the plane enable is already
11042 * armed to occur at the next vblank :(
11043 */
11044
75f7f3ec
VS
11045 /*
11046 * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
11047 * used the same base address. In that case the mmio flip might
11048 * have completed, but the CS hasn't even executed the flip yet.
11049 *
11050 * A flip count check isn't enough as the CS might have updated
11051 * the base address just after start of vblank, but before we
11052 * managed to process the interrupt. This means we'd complete the
11053 * CS flip too soon.
11054 *
11055 * Combining both checks should get us a good enough result. It may
11056 * still happen that the CS flip has been executed, but has not
11057 * yet actually completed. But in case the base address is the same
11058 * anyway, we don't really care.
11059 */
11060 return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
11061 crtc->unpin_work->gtt_offset &&
fd8f507c 11062 g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_G4X(crtc->pipe)),
75f7f3ec
VS
11063 crtc->unpin_work->flip_count);
11064}
11065
6b95a207
KH
11066void intel_prepare_page_flip(struct drm_device *dev, int plane)
11067{
fbee40df 11068 struct drm_i915_private *dev_priv = dev->dev_private;
6b95a207
KH
11069 struct intel_crtc *intel_crtc =
11070 to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
11071 unsigned long flags;
11072
f326038a
DV
11073
11074 /*
11075 * This is called both by irq handlers and the reset code (to complete
11076 * lost pageflips) so needs the full irqsave spinlocks.
11077 *
11078 * NB: An MMIO update of the plane base pointer will also
e7d841ca
CW
11079 * generate a page-flip completion irq, i.e. every modeset
11080 * is also accompanied by a spurious intel_prepare_page_flip().
11081 */
6b95a207 11082 spin_lock_irqsave(&dev->event_lock, flags);
75f7f3ec 11083 if (intel_crtc->unpin_work && page_flip_finished(intel_crtc))
e7d841ca 11084 atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
6b95a207
KH
11085 spin_unlock_irqrestore(&dev->event_lock, flags);
11086}
11087
6042639c 11088static inline void intel_mark_page_flip_active(struct intel_unpin_work *work)
e7d841ca
CW
11089{
11090 /* Ensure that the work item is consistent when activating it ... */
11091 smp_wmb();
6042639c 11092 atomic_set(&work->pending, INTEL_FLIP_PENDING);
e7d841ca
CW
11093 /* and that it is marked active as soon as the irq could fire. */
11094 smp_wmb();
11095}
11096
8c9f3aaf
JB
11097static int intel_gen2_queue_flip(struct drm_device *dev,
11098 struct drm_crtc *crtc,
11099 struct drm_framebuffer *fb,
ed8d1975 11100 struct drm_i915_gem_object *obj,
6258fbe2 11101 struct drm_i915_gem_request *req,
ed8d1975 11102 uint32_t flags)
8c9f3aaf 11103{
4a570db5 11104 struct intel_engine_cs *engine = req->engine;
8c9f3aaf 11105 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8c9f3aaf
JB
11106 u32 flip_mask;
11107 int ret;
11108
5fb9de1a 11109 ret = intel_ring_begin(req, 6);
8c9f3aaf 11110 if (ret)
4fa62c89 11111 return ret;
8c9f3aaf
JB
11112
11113 /* Can't queue multiple flips, so wait for the previous
11114 * one to finish before executing the next.
11115 */
11116 if (intel_crtc->plane)
11117 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
11118 else
11119 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
e2f80391
TU
11120 intel_ring_emit(engine, MI_WAIT_FOR_EVENT | flip_mask);
11121 intel_ring_emit(engine, MI_NOOP);
11122 intel_ring_emit(engine, MI_DISPLAY_FLIP |
6d90c952 11123 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
e2f80391
TU
11124 intel_ring_emit(engine, fb->pitches[0]);
11125 intel_ring_emit(engine, intel_crtc->unpin_work->gtt_offset);
11126 intel_ring_emit(engine, 0); /* aux display base address, unused */
e7d841ca 11127
6042639c 11128 intel_mark_page_flip_active(intel_crtc->unpin_work);
83d4092b 11129 return 0;
8c9f3aaf
JB
11130}
11131
11132static int intel_gen3_queue_flip(struct drm_device *dev,
11133 struct drm_crtc *crtc,
11134 struct drm_framebuffer *fb,
ed8d1975 11135 struct drm_i915_gem_object *obj,
6258fbe2 11136 struct drm_i915_gem_request *req,
ed8d1975 11137 uint32_t flags)
8c9f3aaf 11138{
4a570db5 11139 struct intel_engine_cs *engine = req->engine;
8c9f3aaf 11140 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8c9f3aaf
JB
11141 u32 flip_mask;
11142 int ret;
11143
5fb9de1a 11144 ret = intel_ring_begin(req, 6);
8c9f3aaf 11145 if (ret)
4fa62c89 11146 return ret;
8c9f3aaf
JB
11147
11148 if (intel_crtc->plane)
11149 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
11150 else
11151 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
e2f80391
TU
11152 intel_ring_emit(engine, MI_WAIT_FOR_EVENT | flip_mask);
11153 intel_ring_emit(engine, MI_NOOP);
11154 intel_ring_emit(engine, MI_DISPLAY_FLIP_I915 |
6d90c952 11155 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
e2f80391
TU
11156 intel_ring_emit(engine, fb->pitches[0]);
11157 intel_ring_emit(engine, intel_crtc->unpin_work->gtt_offset);
11158 intel_ring_emit(engine, MI_NOOP);
6d90c952 11159
6042639c 11160 intel_mark_page_flip_active(intel_crtc->unpin_work);
83d4092b 11161 return 0;
8c9f3aaf
JB
11162}
11163
11164static int intel_gen4_queue_flip(struct drm_device *dev,
11165 struct drm_crtc *crtc,
11166 struct drm_framebuffer *fb,
ed8d1975 11167 struct drm_i915_gem_object *obj,
6258fbe2 11168 struct drm_i915_gem_request *req,
ed8d1975 11169 uint32_t flags)
8c9f3aaf 11170{
4a570db5 11171 struct intel_engine_cs *engine = req->engine;
8c9f3aaf
JB
11172 struct drm_i915_private *dev_priv = dev->dev_private;
11173 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11174 uint32_t pf, pipesrc;
11175 int ret;
11176
5fb9de1a 11177 ret = intel_ring_begin(req, 4);
8c9f3aaf 11178 if (ret)
4fa62c89 11179 return ret;
8c9f3aaf
JB
11180
11181 /* i965+ uses the linear or tiled offsets from the
11182 * Display Registers (which do not change across a page-flip)
11183 * so we need only reprogram the base address.
11184 */
e2f80391 11185 intel_ring_emit(engine, MI_DISPLAY_FLIP |
6d90c952 11186 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
e2f80391
TU
11187 intel_ring_emit(engine, fb->pitches[0]);
11188 intel_ring_emit(engine, intel_crtc->unpin_work->gtt_offset |
c2c75131 11189 obj->tiling_mode);
8c9f3aaf
JB
11190
11191 /* XXX Enabling the panel-fitter across page-flip is so far
11192 * untested on non-native modes, so ignore it for now.
11193 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
11194 */
11195 pf = 0;
11196 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
e2f80391 11197 intel_ring_emit(engine, pf | pipesrc);
e7d841ca 11198
6042639c 11199 intel_mark_page_flip_active(intel_crtc->unpin_work);
83d4092b 11200 return 0;
8c9f3aaf
JB
11201}
11202
11203static int intel_gen6_queue_flip(struct drm_device *dev,
11204 struct drm_crtc *crtc,
11205 struct drm_framebuffer *fb,
ed8d1975 11206 struct drm_i915_gem_object *obj,
6258fbe2 11207 struct drm_i915_gem_request *req,
ed8d1975 11208 uint32_t flags)
8c9f3aaf 11209{
4a570db5 11210 struct intel_engine_cs *engine = req->engine;
8c9f3aaf
JB
11211 struct drm_i915_private *dev_priv = dev->dev_private;
11212 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11213 uint32_t pf, pipesrc;
11214 int ret;
11215
5fb9de1a 11216 ret = intel_ring_begin(req, 4);
8c9f3aaf 11217 if (ret)
4fa62c89 11218 return ret;
8c9f3aaf 11219
e2f80391 11220 intel_ring_emit(engine, MI_DISPLAY_FLIP |
6d90c952 11221 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
e2f80391
TU
11222 intel_ring_emit(engine, fb->pitches[0] | obj->tiling_mode);
11223 intel_ring_emit(engine, intel_crtc->unpin_work->gtt_offset);
8c9f3aaf 11224
dc257cf1
DV
11225 /* Contrary to the suggestions in the documentation,
11226 * "Enable Panel Fitter" does not seem to be required when page
11227 * flipping with a non-native mode, and worse causes a normal
11228 * modeset to fail.
11229 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
11230 */
11231 pf = 0;
8c9f3aaf 11232 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
e2f80391 11233 intel_ring_emit(engine, pf | pipesrc);
e7d841ca 11234
6042639c 11235 intel_mark_page_flip_active(intel_crtc->unpin_work);
83d4092b 11236 return 0;
8c9f3aaf
JB
11237}
11238
7c9017e5
JB
11239static int intel_gen7_queue_flip(struct drm_device *dev,
11240 struct drm_crtc *crtc,
11241 struct drm_framebuffer *fb,
ed8d1975 11242 struct drm_i915_gem_object *obj,
6258fbe2 11243 struct drm_i915_gem_request *req,
ed8d1975 11244 uint32_t flags)
7c9017e5 11245{
4a570db5 11246 struct intel_engine_cs *engine = req->engine;
7c9017e5 11247 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
cb05d8de 11248 uint32_t plane_bit = 0;
ffe74d75
CW
11249 int len, ret;
11250
eba905b2 11251 switch (intel_crtc->plane) {
cb05d8de
DV
11252 case PLANE_A:
11253 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
11254 break;
11255 case PLANE_B:
11256 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
11257 break;
11258 case PLANE_C:
11259 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
11260 break;
11261 default:
11262 WARN_ONCE(1, "unknown plane in flip command\n");
4fa62c89 11263 return -ENODEV;
cb05d8de
DV
11264 }
11265
ffe74d75 11266 len = 4;
e2f80391 11267 if (engine->id == RCS) {
ffe74d75 11268 len += 6;
f476828a
DL
11269 /*
11270 * On Gen 8, SRM is now taking an extra dword to accommodate
11271 * 48bits addresses, and we need a NOOP for the batch size to
11272 * stay even.
11273 */
11274 if (IS_GEN8(dev))
11275 len += 2;
11276 }
ffe74d75 11277
f66fab8e
VS
11278 /*
11279 * BSpec MI_DISPLAY_FLIP for IVB:
11280 * "The full packet must be contained within the same cache line."
11281 *
11282 * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
11283 * cacheline, if we ever start emitting more commands before
11284 * the MI_DISPLAY_FLIP we may need to first emit everything else,
11285 * then do the cacheline alignment, and finally emit the
11286 * MI_DISPLAY_FLIP.
11287 */
bba09b12 11288 ret = intel_ring_cacheline_align(req);
f66fab8e 11289 if (ret)
4fa62c89 11290 return ret;
f66fab8e 11291
5fb9de1a 11292 ret = intel_ring_begin(req, len);
7c9017e5 11293 if (ret)
4fa62c89 11294 return ret;
7c9017e5 11295
ffe74d75
CW
11296 /* Unmask the flip-done completion message. Note that the bspec says that
11297 * we should do this for both the BCS and RCS, and that we must not unmask
11298 * more than one flip event at any time (or ensure that one flip message
11299 * can be sent by waiting for flip-done prior to queueing new flips).
11300 * Experimentation says that BCS works despite DERRMR masking all
11301 * flip-done completion events and that unmasking all planes at once
11302 * for the RCS also doesn't appear to drop events. Setting the DERRMR
11303 * to zero does lead to lockups within MI_DISPLAY_FLIP.
11304 */
e2f80391
TU
11305 if (engine->id == RCS) {
11306 intel_ring_emit(engine, MI_LOAD_REGISTER_IMM(1));
11307 intel_ring_emit_reg(engine, DERRMR);
11308 intel_ring_emit(engine, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
11309 DERRMR_PIPEB_PRI_FLIP_DONE |
11310 DERRMR_PIPEC_PRI_FLIP_DONE));
f476828a 11311 if (IS_GEN8(dev))
e2f80391 11312 intel_ring_emit(engine, MI_STORE_REGISTER_MEM_GEN8 |
f476828a
DL
11313 MI_SRM_LRM_GLOBAL_GTT);
11314 else
e2f80391 11315 intel_ring_emit(engine, MI_STORE_REGISTER_MEM |
f476828a 11316 MI_SRM_LRM_GLOBAL_GTT);
e2f80391
TU
11317 intel_ring_emit_reg(engine, DERRMR);
11318 intel_ring_emit(engine, engine->scratch.gtt_offset + 256);
f476828a 11319 if (IS_GEN8(dev)) {
e2f80391
TU
11320 intel_ring_emit(engine, 0);
11321 intel_ring_emit(engine, MI_NOOP);
f476828a 11322 }
ffe74d75
CW
11323 }
11324
e2f80391
TU
11325 intel_ring_emit(engine, MI_DISPLAY_FLIP_I915 | plane_bit);
11326 intel_ring_emit(engine, (fb->pitches[0] | obj->tiling_mode));
11327 intel_ring_emit(engine, intel_crtc->unpin_work->gtt_offset);
11328 intel_ring_emit(engine, (MI_NOOP));
e7d841ca 11329
6042639c 11330 intel_mark_page_flip_active(intel_crtc->unpin_work);
83d4092b 11331 return 0;
7c9017e5
JB
11332}
11333
0bc40be8 11334static bool use_mmio_flip(struct intel_engine_cs *engine,
84c33a64
SG
11335 struct drm_i915_gem_object *obj)
11336{
11337 /*
11338 * This is not being used for older platforms, because
11339 * non-availability of flip done interrupt forces us to use
11340 * CS flips. Older platforms derive flip done using some clever
11341 * tricks involving the flip_pending status bits and vblank irqs.
11342 * So using MMIO flips there would disrupt this mechanism.
11343 */
11344
0bc40be8 11345 if (engine == NULL)
8e09bf83
CW
11346 return true;
11347
0bc40be8 11348 if (INTEL_INFO(engine->dev)->gen < 5)
84c33a64
SG
11349 return false;
11350
11351 if (i915.use_mmio_flip < 0)
11352 return false;
11353 else if (i915.use_mmio_flip > 0)
11354 return true;
14bf993e
OM
11355 else if (i915.enable_execlists)
11356 return true;
fd8e058a
AG
11357 else if (obj->base.dma_buf &&
11358 !reservation_object_test_signaled_rcu(obj->base.dma_buf->resv,
11359 false))
11360 return true;
84c33a64 11361 else
666796da 11362 return engine != i915_gem_request_get_engine(obj->last_write_req);
84c33a64
SG
11363}
11364
6042639c 11365static void skl_do_mmio_flip(struct intel_crtc *intel_crtc,
86efe24a 11366 unsigned int rotation,
6042639c 11367 struct intel_unpin_work *work)
ff944564
DL
11368{
11369 struct drm_device *dev = intel_crtc->base.dev;
11370 struct drm_i915_private *dev_priv = dev->dev_private;
11371 struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
ff944564 11372 const enum pipe pipe = intel_crtc->pipe;
86efe24a 11373 u32 ctl, stride, tile_height;
ff944564
DL
11374
11375 ctl = I915_READ(PLANE_CTL(pipe, 0));
11376 ctl &= ~PLANE_CTL_TILED_MASK;
2ebef630
TU
11377 switch (fb->modifier[0]) {
11378 case DRM_FORMAT_MOD_NONE:
11379 break;
11380 case I915_FORMAT_MOD_X_TILED:
ff944564 11381 ctl |= PLANE_CTL_TILED_X;
2ebef630
TU
11382 break;
11383 case I915_FORMAT_MOD_Y_TILED:
11384 ctl |= PLANE_CTL_TILED_Y;
11385 break;
11386 case I915_FORMAT_MOD_Yf_TILED:
11387 ctl |= PLANE_CTL_TILED_YF;
11388 break;
11389 default:
11390 MISSING_CASE(fb->modifier[0]);
11391 }
ff944564
DL
11392
11393 /*
11394 * The stride is either expressed as a multiple of 64 bytes chunks for
11395 * linear buffers or in number of tiles for tiled buffers.
11396 */
86efe24a
TU
11397 if (intel_rotation_90_or_270(rotation)) {
11398 /* stride = Surface height in tiles */
832be82f 11399 tile_height = intel_tile_height(dev_priv, fb->modifier[0], 0);
86efe24a
TU
11400 stride = DIV_ROUND_UP(fb->height, tile_height);
11401 } else {
11402 stride = fb->pitches[0] /
7b49f948
VS
11403 intel_fb_stride_alignment(dev_priv, fb->modifier[0],
11404 fb->pixel_format);
86efe24a 11405 }
ff944564
DL
11406
11407 /*
11408 * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
11409 * PLANE_SURF updates, the update is then guaranteed to be atomic.
11410 */
11411 I915_WRITE(PLANE_CTL(pipe, 0), ctl);
11412 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
11413
6042639c 11414 I915_WRITE(PLANE_SURF(pipe, 0), work->gtt_offset);
ff944564
DL
11415 POSTING_READ(PLANE_SURF(pipe, 0));
11416}
11417
6042639c
CW
11418static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc,
11419 struct intel_unpin_work *work)
84c33a64
SG
11420{
11421 struct drm_device *dev = intel_crtc->base.dev;
11422 struct drm_i915_private *dev_priv = dev->dev_private;
11423 struct intel_framebuffer *intel_fb =
11424 to_intel_framebuffer(intel_crtc->base.primary->fb);
11425 struct drm_i915_gem_object *obj = intel_fb->obj;
f0f59a00 11426 i915_reg_t reg = DSPCNTR(intel_crtc->plane);
84c33a64 11427 u32 dspcntr;
84c33a64 11428
84c33a64
SG
11429 dspcntr = I915_READ(reg);
11430
c5d97472
DL
11431 if (obj->tiling_mode != I915_TILING_NONE)
11432 dspcntr |= DISPPLANE_TILED;
11433 else
11434 dspcntr &= ~DISPPLANE_TILED;
11435
84c33a64
SG
11436 I915_WRITE(reg, dspcntr);
11437
6042639c 11438 I915_WRITE(DSPSURF(intel_crtc->plane), work->gtt_offset);
84c33a64 11439 POSTING_READ(DSPSURF(intel_crtc->plane));
ff944564
DL
11440}
11441
11442/*
11443 * XXX: This is the temporary way to update the plane registers until we get
11444 * around to using the usual plane update functions for MMIO flips
11445 */
6042639c 11446static void intel_do_mmio_flip(struct intel_mmio_flip *mmio_flip)
ff944564 11447{
6042639c
CW
11448 struct intel_crtc *crtc = mmio_flip->crtc;
11449 struct intel_unpin_work *work;
11450
11451 spin_lock_irq(&crtc->base.dev->event_lock);
11452 work = crtc->unpin_work;
11453 spin_unlock_irq(&crtc->base.dev->event_lock);
11454 if (work == NULL)
11455 return;
ff944564 11456
6042639c 11457 intel_mark_page_flip_active(work);
ff944564 11458
6042639c 11459 intel_pipe_update_start(crtc);
ff944564 11460
6042639c 11461 if (INTEL_INFO(mmio_flip->i915)->gen >= 9)
86efe24a 11462 skl_do_mmio_flip(crtc, mmio_flip->rotation, work);
ff944564
DL
11463 else
11464 /* use_mmio_flip() retricts MMIO flips to ilk+ */
6042639c 11465 ilk_do_mmio_flip(crtc, work);
ff944564 11466
6042639c 11467 intel_pipe_update_end(crtc);
84c33a64
SG
11468}
11469
9362c7c5 11470static void intel_mmio_flip_work_func(struct work_struct *work)
84c33a64 11471{
b2cfe0ab
CW
11472 struct intel_mmio_flip *mmio_flip =
11473 container_of(work, struct intel_mmio_flip, work);
fd8e058a
AG
11474 struct intel_framebuffer *intel_fb =
11475 to_intel_framebuffer(mmio_flip->crtc->base.primary->fb);
11476 struct drm_i915_gem_object *obj = intel_fb->obj;
84c33a64 11477
6042639c 11478 if (mmio_flip->req) {
eed29a5b 11479 WARN_ON(__i915_wait_request(mmio_flip->req,
b2cfe0ab 11480 mmio_flip->crtc->reset_counter,
bcafc4e3
CW
11481 false, NULL,
11482 &mmio_flip->i915->rps.mmioflips));
6042639c
CW
11483 i915_gem_request_unreference__unlocked(mmio_flip->req);
11484 }
84c33a64 11485
fd8e058a
AG
11486 /* For framebuffer backed by dmabuf, wait for fence */
11487 if (obj->base.dma_buf)
11488 WARN_ON(reservation_object_wait_timeout_rcu(obj->base.dma_buf->resv,
11489 false, false,
11490 MAX_SCHEDULE_TIMEOUT) < 0);
11491
6042639c 11492 intel_do_mmio_flip(mmio_flip);
b2cfe0ab 11493 kfree(mmio_flip);
84c33a64
SG
11494}
11495
11496static int intel_queue_mmio_flip(struct drm_device *dev,
11497 struct drm_crtc *crtc,
86efe24a 11498 struct drm_i915_gem_object *obj)
84c33a64 11499{
b2cfe0ab
CW
11500 struct intel_mmio_flip *mmio_flip;
11501
11502 mmio_flip = kmalloc(sizeof(*mmio_flip), GFP_KERNEL);
11503 if (mmio_flip == NULL)
11504 return -ENOMEM;
84c33a64 11505
bcafc4e3 11506 mmio_flip->i915 = to_i915(dev);
eed29a5b 11507 mmio_flip->req = i915_gem_request_reference(obj->last_write_req);
b2cfe0ab 11508 mmio_flip->crtc = to_intel_crtc(crtc);
86efe24a 11509 mmio_flip->rotation = crtc->primary->state->rotation;
536f5b5e 11510
b2cfe0ab
CW
11511 INIT_WORK(&mmio_flip->work, intel_mmio_flip_work_func);
11512 schedule_work(&mmio_flip->work);
84c33a64 11513
84c33a64
SG
11514 return 0;
11515}
11516
8c9f3aaf
JB
11517static int intel_default_queue_flip(struct drm_device *dev,
11518 struct drm_crtc *crtc,
11519 struct drm_framebuffer *fb,
ed8d1975 11520 struct drm_i915_gem_object *obj,
6258fbe2 11521 struct drm_i915_gem_request *req,
ed8d1975 11522 uint32_t flags)
8c9f3aaf
JB
11523{
11524 return -ENODEV;
11525}
11526
d6bbafa1
CW
11527static bool __intel_pageflip_stall_check(struct drm_device *dev,
11528 struct drm_crtc *crtc)
11529{
11530 struct drm_i915_private *dev_priv = dev->dev_private;
11531 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11532 struct intel_unpin_work *work = intel_crtc->unpin_work;
11533 u32 addr;
11534
11535 if (atomic_read(&work->pending) >= INTEL_FLIP_COMPLETE)
11536 return true;
11537
908565c2
CW
11538 if (atomic_read(&work->pending) < INTEL_FLIP_PENDING)
11539 return false;
11540
d6bbafa1
CW
11541 if (!work->enable_stall_check)
11542 return false;
11543
11544 if (work->flip_ready_vblank == 0) {
3a8a946e
DV
11545 if (work->flip_queued_req &&
11546 !i915_gem_request_completed(work->flip_queued_req, true))
d6bbafa1
CW
11547 return false;
11548
1e3feefd 11549 work->flip_ready_vblank = drm_crtc_vblank_count(crtc);
d6bbafa1
CW
11550 }
11551
1e3feefd 11552 if (drm_crtc_vblank_count(crtc) - work->flip_ready_vblank < 3)
d6bbafa1
CW
11553 return false;
11554
11555 /* Potential stall - if we see that the flip has happened,
11556 * assume a missed interrupt. */
11557 if (INTEL_INFO(dev)->gen >= 4)
11558 addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
11559 else
11560 addr = I915_READ(DSPADDR(intel_crtc->plane));
11561
11562 /* There is a potential issue here with a false positive after a flip
11563 * to the same address. We could address this by checking for a
11564 * non-incrementing frame counter.
11565 */
11566 return addr == work->gtt_offset;
11567}
11568
11569void intel_check_page_flip(struct drm_device *dev, int pipe)
11570{
11571 struct drm_i915_private *dev_priv = dev->dev_private;
11572 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
11573 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6ad790c0 11574 struct intel_unpin_work *work;
f326038a 11575
6c51d46f 11576 WARN_ON(!in_interrupt());
d6bbafa1
CW
11577
11578 if (crtc == NULL)
11579 return;
11580
f326038a 11581 spin_lock(&dev->event_lock);
6ad790c0
CW
11582 work = intel_crtc->unpin_work;
11583 if (work != NULL && __intel_pageflip_stall_check(dev, crtc)) {
d6bbafa1 11584 WARN_ONCE(1, "Kicking stuck page flip: queued at %d, now %d\n",
6ad790c0 11585 work->flip_queued_vblank, drm_vblank_count(dev, pipe));
d6bbafa1 11586 page_flip_completed(intel_crtc);
6ad790c0 11587 work = NULL;
d6bbafa1 11588 }
6ad790c0
CW
11589 if (work != NULL &&
11590 drm_vblank_count(dev, pipe) - work->flip_queued_vblank > 1)
11591 intel_queue_rps_boost_for_request(dev, work->flip_queued_req);
f326038a 11592 spin_unlock(&dev->event_lock);
d6bbafa1
CW
11593}
11594
6b95a207
KH
11595static int intel_crtc_page_flip(struct drm_crtc *crtc,
11596 struct drm_framebuffer *fb,
ed8d1975
KP
11597 struct drm_pending_vblank_event *event,
11598 uint32_t page_flip_flags)
6b95a207
KH
11599{
11600 struct drm_device *dev = crtc->dev;
11601 struct drm_i915_private *dev_priv = dev->dev_private;
f4510a27 11602 struct drm_framebuffer *old_fb = crtc->primary->fb;
2ff8fde1 11603 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
6b95a207 11604 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
455a6808 11605 struct drm_plane *primary = crtc->primary;
a071fa00 11606 enum pipe pipe = intel_crtc->pipe;
6b95a207 11607 struct intel_unpin_work *work;
e2f80391 11608 struct intel_engine_cs *engine;
cf5d8a46 11609 bool mmio_flip;
91af127f 11610 struct drm_i915_gem_request *request = NULL;
52e68630 11611 int ret;
6b95a207 11612
2ff8fde1
MR
11613 /*
11614 * drm_mode_page_flip_ioctl() should already catch this, but double
11615 * check to be safe. In the future we may enable pageflipping from
11616 * a disabled primary plane.
11617 */
11618 if (WARN_ON(intel_fb_obj(old_fb) == NULL))
11619 return -EBUSY;
11620
e6a595d2 11621 /* Can't change pixel format via MI display flips. */
f4510a27 11622 if (fb->pixel_format != crtc->primary->fb->pixel_format)
e6a595d2
VS
11623 return -EINVAL;
11624
11625 /*
11626 * TILEOFF/LINOFF registers can't be changed via MI display flips.
11627 * Note that pitch changes could also affect these register.
11628 */
11629 if (INTEL_INFO(dev)->gen > 3 &&
f4510a27
MR
11630 (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
11631 fb->pitches[0] != crtc->primary->fb->pitches[0]))
e6a595d2
VS
11632 return -EINVAL;
11633
f900db47
CW
11634 if (i915_terminally_wedged(&dev_priv->gpu_error))
11635 goto out_hang;
11636
b14c5679 11637 work = kzalloc(sizeof(*work), GFP_KERNEL);
6b95a207
KH
11638 if (work == NULL)
11639 return -ENOMEM;
11640
6b95a207 11641 work->event = event;
b4a98e57 11642 work->crtc = crtc;
ab8d6675 11643 work->old_fb = old_fb;
6b95a207
KH
11644 INIT_WORK(&work->work, intel_unpin_work_fn);
11645
87b6b101 11646 ret = drm_crtc_vblank_get(crtc);
7317c75e
JB
11647 if (ret)
11648 goto free_work;
11649
6b95a207 11650 /* We borrow the event spin lock for protecting unpin_work */
5e2d7afc 11651 spin_lock_irq(&dev->event_lock);
6b95a207 11652 if (intel_crtc->unpin_work) {
d6bbafa1
CW
11653 /* Before declaring the flip queue wedged, check if
11654 * the hardware completed the operation behind our backs.
11655 */
11656 if (__intel_pageflip_stall_check(dev, crtc)) {
11657 DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
11658 page_flip_completed(intel_crtc);
11659 } else {
11660 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
5e2d7afc 11661 spin_unlock_irq(&dev->event_lock);
468f0b44 11662
d6bbafa1
CW
11663 drm_crtc_vblank_put(crtc);
11664 kfree(work);
11665 return -EBUSY;
11666 }
6b95a207
KH
11667 }
11668 intel_crtc->unpin_work = work;
5e2d7afc 11669 spin_unlock_irq(&dev->event_lock);
6b95a207 11670
b4a98e57
CW
11671 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
11672 flush_workqueue(dev_priv->wq);
11673
75dfca80 11674 /* Reference the objects for the scheduled work. */
ab8d6675 11675 drm_framebuffer_reference(work->old_fb);
05394f39 11676 drm_gem_object_reference(&obj->base);
6b95a207 11677
f4510a27 11678 crtc->primary->fb = fb;
afd65eb4 11679 update_state_fb(crtc->primary);
e8216e50 11680 intel_fbc_pre_update(intel_crtc);
1ed1f968 11681
e1f99ce6 11682 work->pending_flip_obj = obj;
e1f99ce6 11683
89ed88ba
CW
11684 ret = i915_mutex_lock_interruptible(dev);
11685 if (ret)
11686 goto cleanup;
11687
b4a98e57 11688 atomic_inc(&intel_crtc->unpin_work_count);
10d83730 11689 intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
e1f99ce6 11690
75f7f3ec 11691 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
fd8f507c 11692 work->flip_count = I915_READ(PIPE_FLIPCOUNT_G4X(pipe)) + 1;
75f7f3ec 11693
666a4537 11694 if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
4a570db5 11695 engine = &dev_priv->engine[BCS];
ab8d6675 11696 if (obj->tiling_mode != intel_fb_obj(work->old_fb)->tiling_mode)
8e09bf83 11697 /* vlv: DISPLAY_FLIP fails to change tiling */
e2f80391 11698 engine = NULL;
48bf5b2d 11699 } else if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
4a570db5 11700 engine = &dev_priv->engine[BCS];
4fa62c89 11701 } else if (INTEL_INFO(dev)->gen >= 7) {
666796da 11702 engine = i915_gem_request_get_engine(obj->last_write_req);
e2f80391 11703 if (engine == NULL || engine->id != RCS)
4a570db5 11704 engine = &dev_priv->engine[BCS];
4fa62c89 11705 } else {
4a570db5 11706 engine = &dev_priv->engine[RCS];
4fa62c89
VS
11707 }
11708
e2f80391 11709 mmio_flip = use_mmio_flip(engine, obj);
cf5d8a46
CW
11710
11711 /* When using CS flips, we want to emit semaphores between rings.
11712 * However, when using mmio flips we will create a task to do the
11713 * synchronisation, so all we want here is to pin the framebuffer
11714 * into the display plane and skip any waits.
11715 */
7580d774 11716 if (!mmio_flip) {
e2f80391 11717 ret = i915_gem_object_sync(obj, engine, &request);
7580d774
ML
11718 if (ret)
11719 goto cleanup_pending;
11720 }
11721
3465c580 11722 ret = intel_pin_and_fence_fb_obj(fb, primary->state->rotation);
8c9f3aaf
JB
11723 if (ret)
11724 goto cleanup_pending;
6b95a207 11725
dedf278c
TU
11726 work->gtt_offset = intel_plane_obj_offset(to_intel_plane(primary),
11727 obj, 0);
11728 work->gtt_offset += intel_crtc->dspaddr_offset;
4fa62c89 11729
cf5d8a46 11730 if (mmio_flip) {
86efe24a 11731 ret = intel_queue_mmio_flip(dev, crtc, obj);
d6bbafa1
CW
11732 if (ret)
11733 goto cleanup_unpin;
11734
f06cc1b9
JH
11735 i915_gem_request_assign(&work->flip_queued_req,
11736 obj->last_write_req);
d6bbafa1 11737 } else {
6258fbe2 11738 if (!request) {
e2f80391 11739 request = i915_gem_request_alloc(engine, NULL);
26827088
DG
11740 if (IS_ERR(request)) {
11741 ret = PTR_ERR(request);
6258fbe2 11742 goto cleanup_unpin;
26827088 11743 }
6258fbe2
JH
11744 }
11745
11746 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, request,
d6bbafa1
CW
11747 page_flip_flags);
11748 if (ret)
11749 goto cleanup_unpin;
11750
6258fbe2 11751 i915_gem_request_assign(&work->flip_queued_req, request);
d6bbafa1
CW
11752 }
11753
91af127f 11754 if (request)
75289874 11755 i915_add_request_no_flush(request);
91af127f 11756
1e3feefd 11757 work->flip_queued_vblank = drm_crtc_vblank_count(crtc);
d6bbafa1 11758 work->enable_stall_check = true;
4fa62c89 11759
ab8d6675 11760 i915_gem_track_fb(intel_fb_obj(work->old_fb), obj,
a9ff8714 11761 to_intel_plane(primary)->frontbuffer_bit);
c80ac854 11762 mutex_unlock(&dev->struct_mutex);
a071fa00 11763
a9ff8714
VS
11764 intel_frontbuffer_flip_prepare(dev,
11765 to_intel_plane(primary)->frontbuffer_bit);
6b95a207 11766
e5510fac
JB
11767 trace_i915_flip_request(intel_crtc->plane, obj);
11768
6b95a207 11769 return 0;
96b099fd 11770
4fa62c89 11771cleanup_unpin:
3465c580 11772 intel_unpin_fb_obj(fb, crtc->primary->state->rotation);
8c9f3aaf 11773cleanup_pending:
0aa498d5 11774 if (!IS_ERR_OR_NULL(request))
91af127f 11775 i915_gem_request_cancel(request);
b4a98e57 11776 atomic_dec(&intel_crtc->unpin_work_count);
89ed88ba
CW
11777 mutex_unlock(&dev->struct_mutex);
11778cleanup:
f4510a27 11779 crtc->primary->fb = old_fb;
afd65eb4 11780 update_state_fb(crtc->primary);
89ed88ba
CW
11781
11782 drm_gem_object_unreference_unlocked(&obj->base);
ab8d6675 11783 drm_framebuffer_unreference(work->old_fb);
96b099fd 11784
5e2d7afc 11785 spin_lock_irq(&dev->event_lock);
96b099fd 11786 intel_crtc->unpin_work = NULL;
5e2d7afc 11787 spin_unlock_irq(&dev->event_lock);
96b099fd 11788
87b6b101 11789 drm_crtc_vblank_put(crtc);
7317c75e 11790free_work:
96b099fd
CW
11791 kfree(work);
11792
f900db47 11793 if (ret == -EIO) {
02e0efb5
ML
11794 struct drm_atomic_state *state;
11795 struct drm_plane_state *plane_state;
11796
f900db47 11797out_hang:
02e0efb5
ML
11798 state = drm_atomic_state_alloc(dev);
11799 if (!state)
11800 return -ENOMEM;
11801 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
11802
11803retry:
11804 plane_state = drm_atomic_get_plane_state(state, primary);
11805 ret = PTR_ERR_OR_ZERO(plane_state);
11806 if (!ret) {
11807 drm_atomic_set_fb_for_plane(plane_state, fb);
11808
11809 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
11810 if (!ret)
11811 ret = drm_atomic_commit(state);
11812 }
11813
11814 if (ret == -EDEADLK) {
11815 drm_modeset_backoff(state->acquire_ctx);
11816 drm_atomic_state_clear(state);
11817 goto retry;
11818 }
11819
11820 if (ret)
11821 drm_atomic_state_free(state);
11822
f0d3dad3 11823 if (ret == 0 && event) {
5e2d7afc 11824 spin_lock_irq(&dev->event_lock);
a071fa00 11825 drm_send_vblank_event(dev, pipe, event);
5e2d7afc 11826 spin_unlock_irq(&dev->event_lock);
f0d3dad3 11827 }
f900db47 11828 }
96b099fd 11829 return ret;
6b95a207
KH
11830}
11831
da20eabd
ML
11832
11833/**
11834 * intel_wm_need_update - Check whether watermarks need updating
11835 * @plane: drm plane
11836 * @state: new plane state
11837 *
11838 * Check current plane state versus the new one to determine whether
11839 * watermarks need to be recalculated.
11840 *
11841 * Returns true or false.
11842 */
11843static bool intel_wm_need_update(struct drm_plane *plane,
11844 struct drm_plane_state *state)
11845{
d21fbe87
MR
11846 struct intel_plane_state *new = to_intel_plane_state(state);
11847 struct intel_plane_state *cur = to_intel_plane_state(plane->state);
11848
11849 /* Update watermarks on tiling or size changes. */
92826fcd
ML
11850 if (new->visible != cur->visible)
11851 return true;
11852
11853 if (!cur->base.fb || !new->base.fb)
11854 return false;
11855
11856 if (cur->base.fb->modifier[0] != new->base.fb->modifier[0] ||
11857 cur->base.rotation != new->base.rotation ||
d21fbe87
MR
11858 drm_rect_width(&new->src) != drm_rect_width(&cur->src) ||
11859 drm_rect_height(&new->src) != drm_rect_height(&cur->src) ||
11860 drm_rect_width(&new->dst) != drm_rect_width(&cur->dst) ||
11861 drm_rect_height(&new->dst) != drm_rect_height(&cur->dst))
2791a16c 11862 return true;
7809e5ae 11863
2791a16c 11864 return false;
7809e5ae
MR
11865}
11866
d21fbe87
MR
11867static bool needs_scaling(struct intel_plane_state *state)
11868{
11869 int src_w = drm_rect_width(&state->src) >> 16;
11870 int src_h = drm_rect_height(&state->src) >> 16;
11871 int dst_w = drm_rect_width(&state->dst);
11872 int dst_h = drm_rect_height(&state->dst);
11873
11874 return (src_w != dst_w || src_h != dst_h);
11875}
11876
da20eabd
ML
11877int intel_plane_atomic_calc_changes(struct drm_crtc_state *crtc_state,
11878 struct drm_plane_state *plane_state)
11879{
ab1d3a0e 11880 struct intel_crtc_state *pipe_config = to_intel_crtc_state(crtc_state);
da20eabd
ML
11881 struct drm_crtc *crtc = crtc_state->crtc;
11882 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11883 struct drm_plane *plane = plane_state->plane;
11884 struct drm_device *dev = crtc->dev;
ed4a6a7c 11885 struct drm_i915_private *dev_priv = to_i915(dev);
da20eabd
ML
11886 struct intel_plane_state *old_plane_state =
11887 to_intel_plane_state(plane->state);
11888 int idx = intel_crtc->base.base.id, ret;
da20eabd
ML
11889 bool mode_changed = needs_modeset(crtc_state);
11890 bool was_crtc_enabled = crtc->state->active;
11891 bool is_crtc_enabled = crtc_state->active;
da20eabd
ML
11892 bool turn_off, turn_on, visible, was_visible;
11893 struct drm_framebuffer *fb = plane_state->fb;
11894
11895 if (crtc_state && INTEL_INFO(dev)->gen >= 9 &&
11896 plane->type != DRM_PLANE_TYPE_CURSOR) {
11897 ret = skl_update_scaler_plane(
11898 to_intel_crtc_state(crtc_state),
11899 to_intel_plane_state(plane_state));
11900 if (ret)
11901 return ret;
11902 }
11903
da20eabd
ML
11904 was_visible = old_plane_state->visible;
11905 visible = to_intel_plane_state(plane_state)->visible;
11906
11907 if (!was_crtc_enabled && WARN_ON(was_visible))
11908 was_visible = false;
11909
35c08f43
ML
11910 /*
11911 * Visibility is calculated as if the crtc was on, but
11912 * after scaler setup everything depends on it being off
11913 * when the crtc isn't active.
11914 */
11915 if (!is_crtc_enabled)
11916 to_intel_plane_state(plane_state)->visible = visible = false;
da20eabd
ML
11917
11918 if (!was_visible && !visible)
11919 return 0;
11920
e8861675
ML
11921 if (fb != old_plane_state->base.fb)
11922 pipe_config->fb_changed = true;
11923
da20eabd
ML
11924 turn_off = was_visible && (!visible || mode_changed);
11925 turn_on = visible && (!was_visible || mode_changed);
11926
11927 DRM_DEBUG_ATOMIC("[CRTC:%i] has [PLANE:%i] with fb %i\n", idx,
11928 plane->base.id, fb ? fb->base.id : -1);
11929
11930 DRM_DEBUG_ATOMIC("[PLANE:%i] visible %i -> %i, off %i, on %i, ms %i\n",
11931 plane->base.id, was_visible, visible,
11932 turn_off, turn_on, mode_changed);
11933
caed361d
VS
11934 if (turn_on) {
11935 pipe_config->update_wm_pre = true;
11936
11937 /* must disable cxsr around plane enable/disable */
11938 if (plane->type != DRM_PLANE_TYPE_CURSOR)
11939 pipe_config->disable_cxsr = true;
11940 } else if (turn_off) {
11941 pipe_config->update_wm_post = true;
92826fcd 11942
852eb00d 11943 /* must disable cxsr around plane enable/disable */
e8861675 11944 if (plane->type != DRM_PLANE_TYPE_CURSOR)
ab1d3a0e 11945 pipe_config->disable_cxsr = true;
852eb00d 11946 } else if (intel_wm_need_update(plane, plane_state)) {
caed361d
VS
11947 /* FIXME bollocks */
11948 pipe_config->update_wm_pre = true;
11949 pipe_config->update_wm_post = true;
852eb00d 11950 }
da20eabd 11951
ed4a6a7c 11952 /* Pre-gen9 platforms need two-step watermark updates */
caed361d
VS
11953 if ((pipe_config->update_wm_pre || pipe_config->update_wm_post) &&
11954 INTEL_INFO(dev)->gen < 9 && dev_priv->display.optimize_watermarks)
ed4a6a7c
MR
11955 to_intel_crtc_state(crtc_state)->wm.need_postvbl_update = true;
11956
8be6ca85 11957 if (visible || was_visible)
cd202f69 11958 pipe_config->fb_bits |= to_intel_plane(plane)->frontbuffer_bit;
a9ff8714 11959
31ae71fc
ML
11960 /*
11961 * WaCxSRDisabledForSpriteScaling:ivb
11962 *
11963 * cstate->update_wm was already set above, so this flag will
11964 * take effect when we commit and program watermarks.
11965 */
11966 if (plane->type == DRM_PLANE_TYPE_OVERLAY && IS_IVYBRIDGE(dev) &&
11967 needs_scaling(to_intel_plane_state(plane_state)) &&
11968 !needs_scaling(old_plane_state))
11969 pipe_config->disable_lp_wm = true;
d21fbe87 11970
da20eabd
ML
11971 return 0;
11972}
11973
6d3a1ce7
ML
11974static bool encoders_cloneable(const struct intel_encoder *a,
11975 const struct intel_encoder *b)
11976{
11977 /* masks could be asymmetric, so check both ways */
11978 return a == b || (a->cloneable & (1 << b->type) &&
11979 b->cloneable & (1 << a->type));
11980}
11981
11982static bool check_single_encoder_cloning(struct drm_atomic_state *state,
11983 struct intel_crtc *crtc,
11984 struct intel_encoder *encoder)
11985{
11986 struct intel_encoder *source_encoder;
11987 struct drm_connector *connector;
11988 struct drm_connector_state *connector_state;
11989 int i;
11990
11991 for_each_connector_in_state(state, connector, connector_state, i) {
11992 if (connector_state->crtc != &crtc->base)
11993 continue;
11994
11995 source_encoder =
11996 to_intel_encoder(connector_state->best_encoder);
11997 if (!encoders_cloneable(encoder, source_encoder))
11998 return false;
11999 }
12000
12001 return true;
12002}
12003
12004static bool check_encoder_cloning(struct drm_atomic_state *state,
12005 struct intel_crtc *crtc)
12006{
12007 struct intel_encoder *encoder;
12008 struct drm_connector *connector;
12009 struct drm_connector_state *connector_state;
12010 int i;
12011
12012 for_each_connector_in_state(state, connector, connector_state, i) {
12013 if (connector_state->crtc != &crtc->base)
12014 continue;
12015
12016 encoder = to_intel_encoder(connector_state->best_encoder);
12017 if (!check_single_encoder_cloning(state, crtc, encoder))
12018 return false;
12019 }
12020
12021 return true;
12022}
12023
12024static int intel_crtc_atomic_check(struct drm_crtc *crtc,
12025 struct drm_crtc_state *crtc_state)
12026{
cf5a15be 12027 struct drm_device *dev = crtc->dev;
ad421372 12028 struct drm_i915_private *dev_priv = dev->dev_private;
6d3a1ce7 12029 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
cf5a15be
ML
12030 struct intel_crtc_state *pipe_config =
12031 to_intel_crtc_state(crtc_state);
6d3a1ce7 12032 struct drm_atomic_state *state = crtc_state->state;
4d20cd86 12033 int ret;
6d3a1ce7
ML
12034 bool mode_changed = needs_modeset(crtc_state);
12035
12036 if (mode_changed && !check_encoder_cloning(state, intel_crtc)) {
12037 DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
12038 return -EINVAL;
12039 }
12040
852eb00d 12041 if (mode_changed && !crtc_state->active)
caed361d 12042 pipe_config->update_wm_post = true;
eddfcbcd 12043
ad421372
ML
12044 if (mode_changed && crtc_state->enable &&
12045 dev_priv->display.crtc_compute_clock &&
8106ddbd 12046 !WARN_ON(pipe_config->shared_dpll)) {
ad421372
ML
12047 ret = dev_priv->display.crtc_compute_clock(intel_crtc,
12048 pipe_config);
12049 if (ret)
12050 return ret;
12051 }
12052
e435d6e5 12053 ret = 0;
86c8bbbe 12054 if (dev_priv->display.compute_pipe_wm) {
e3bddded 12055 ret = dev_priv->display.compute_pipe_wm(pipe_config);
ed4a6a7c
MR
12056 if (ret) {
12057 DRM_DEBUG_KMS("Target pipe watermarks are invalid\n");
12058 return ret;
12059 }
12060 }
12061
12062 if (dev_priv->display.compute_intermediate_wm &&
12063 !to_intel_atomic_state(state)->skip_intermediate_wm) {
12064 if (WARN_ON(!dev_priv->display.compute_pipe_wm))
12065 return 0;
12066
12067 /*
12068 * Calculate 'intermediate' watermarks that satisfy both the
12069 * old state and the new state. We can program these
12070 * immediately.
12071 */
12072 ret = dev_priv->display.compute_intermediate_wm(crtc->dev,
12073 intel_crtc,
12074 pipe_config);
12075 if (ret) {
12076 DRM_DEBUG_KMS("No valid intermediate pipe watermarks are possible\n");
86c8bbbe 12077 return ret;
ed4a6a7c 12078 }
86c8bbbe
MR
12079 }
12080
e435d6e5
ML
12081 if (INTEL_INFO(dev)->gen >= 9) {
12082 if (mode_changed)
12083 ret = skl_update_scaler_crtc(pipe_config);
12084
12085 if (!ret)
12086 ret = intel_atomic_setup_scalers(dev, intel_crtc,
12087 pipe_config);
12088 }
12089
12090 return ret;
6d3a1ce7
ML
12091}
12092
65b38e0d 12093static const struct drm_crtc_helper_funcs intel_helper_funcs = {
f6e5b160
CW
12094 .mode_set_base_atomic = intel_pipe_set_base_atomic,
12095 .load_lut = intel_crtc_load_lut,
ea2c67bb
MR
12096 .atomic_begin = intel_begin_crtc_commit,
12097 .atomic_flush = intel_finish_crtc_commit,
6d3a1ce7 12098 .atomic_check = intel_crtc_atomic_check,
f6e5b160
CW
12099};
12100
d29b2f9d
ACO
12101static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
12102{
12103 struct intel_connector *connector;
12104
12105 for_each_intel_connector(dev, connector) {
12106 if (connector->base.encoder) {
12107 connector->base.state->best_encoder =
12108 connector->base.encoder;
12109 connector->base.state->crtc =
12110 connector->base.encoder->crtc;
12111 } else {
12112 connector->base.state->best_encoder = NULL;
12113 connector->base.state->crtc = NULL;
12114 }
12115 }
12116}
12117
050f7aeb 12118static void
eba905b2 12119connected_sink_compute_bpp(struct intel_connector *connector,
5cec258b 12120 struct intel_crtc_state *pipe_config)
050f7aeb
DV
12121{
12122 int bpp = pipe_config->pipe_bpp;
12123
12124 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
12125 connector->base.base.id,
c23cc417 12126 connector->base.name);
050f7aeb
DV
12127
12128 /* Don't use an invalid EDID bpc value */
12129 if (connector->base.display_info.bpc &&
12130 connector->base.display_info.bpc * 3 < bpp) {
12131 DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
12132 bpp, connector->base.display_info.bpc*3);
12133 pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
12134 }
12135
013dd9e0
JN
12136 /* Clamp bpp to default limit on screens without EDID 1.4 */
12137 if (connector->base.display_info.bpc == 0) {
12138 int type = connector->base.connector_type;
12139 int clamp_bpp = 24;
12140
12141 /* Fall back to 18 bpp when DP sink capability is unknown. */
12142 if (type == DRM_MODE_CONNECTOR_DisplayPort ||
12143 type == DRM_MODE_CONNECTOR_eDP)
12144 clamp_bpp = 18;
12145
12146 if (bpp > clamp_bpp) {
12147 DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of %d\n",
12148 bpp, clamp_bpp);
12149 pipe_config->pipe_bpp = clamp_bpp;
12150 }
050f7aeb
DV
12151 }
12152}
12153
4e53c2e0 12154static int
050f7aeb 12155compute_baseline_pipe_bpp(struct intel_crtc *crtc,
5cec258b 12156 struct intel_crtc_state *pipe_config)
4e53c2e0 12157{
050f7aeb 12158 struct drm_device *dev = crtc->base.dev;
1486017f 12159 struct drm_atomic_state *state;
da3ced29
ACO
12160 struct drm_connector *connector;
12161 struct drm_connector_state *connector_state;
1486017f 12162 int bpp, i;
4e53c2e0 12163
666a4537 12164 if ((IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)))
4e53c2e0 12165 bpp = 10*3;
d328c9d7
DV
12166 else if (INTEL_INFO(dev)->gen >= 5)
12167 bpp = 12*3;
12168 else
12169 bpp = 8*3;
12170
4e53c2e0 12171
4e53c2e0
DV
12172 pipe_config->pipe_bpp = bpp;
12173
1486017f
ACO
12174 state = pipe_config->base.state;
12175
4e53c2e0 12176 /* Clamp display bpp to EDID value */
da3ced29
ACO
12177 for_each_connector_in_state(state, connector, connector_state, i) {
12178 if (connector_state->crtc != &crtc->base)
4e53c2e0
DV
12179 continue;
12180
da3ced29
ACO
12181 connected_sink_compute_bpp(to_intel_connector(connector),
12182 pipe_config);
4e53c2e0
DV
12183 }
12184
12185 return bpp;
12186}
12187
644db711
DV
12188static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
12189{
12190 DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
12191 "type: 0x%x flags: 0x%x\n",
1342830c 12192 mode->crtc_clock,
644db711
DV
12193 mode->crtc_hdisplay, mode->crtc_hsync_start,
12194 mode->crtc_hsync_end, mode->crtc_htotal,
12195 mode->crtc_vdisplay, mode->crtc_vsync_start,
12196 mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
12197}
12198
c0b03411 12199static void intel_dump_pipe_config(struct intel_crtc *crtc,
5cec258b 12200 struct intel_crtc_state *pipe_config,
c0b03411
DV
12201 const char *context)
12202{
6a60cd87
CK
12203 struct drm_device *dev = crtc->base.dev;
12204 struct drm_plane *plane;
12205 struct intel_plane *intel_plane;
12206 struct intel_plane_state *state;
12207 struct drm_framebuffer *fb;
12208
12209 DRM_DEBUG_KMS("[CRTC:%d]%s config %p for pipe %c\n", crtc->base.base.id,
12210 context, pipe_config, pipe_name(crtc->pipe));
c0b03411 12211
da205630 12212 DRM_DEBUG_KMS("cpu_transcoder: %s\n", transcoder_name(pipe_config->cpu_transcoder));
c0b03411
DV
12213 DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
12214 pipe_config->pipe_bpp, pipe_config->dither);
12215 DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
12216 pipe_config->has_pch_encoder,
12217 pipe_config->fdi_lanes,
12218 pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
12219 pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
12220 pipe_config->fdi_m_n.tu);
90a6b7b0 12221 DRM_DEBUG_KMS("dp: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
eb14cb74 12222 pipe_config->has_dp_encoder,
90a6b7b0 12223 pipe_config->lane_count,
eb14cb74
VS
12224 pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
12225 pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
12226 pipe_config->dp_m_n.tu);
b95af8be 12227
90a6b7b0 12228 DRM_DEBUG_KMS("dp: %i, lanes: %i, gmch_m2: %u, gmch_n2: %u, link_m2: %u, link_n2: %u, tu2: %u\n",
b95af8be 12229 pipe_config->has_dp_encoder,
90a6b7b0 12230 pipe_config->lane_count,
b95af8be
VK
12231 pipe_config->dp_m2_n2.gmch_m,
12232 pipe_config->dp_m2_n2.gmch_n,
12233 pipe_config->dp_m2_n2.link_m,
12234 pipe_config->dp_m2_n2.link_n,
12235 pipe_config->dp_m2_n2.tu);
12236
55072d19
DV
12237 DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
12238 pipe_config->has_audio,
12239 pipe_config->has_infoframe);
12240
c0b03411 12241 DRM_DEBUG_KMS("requested mode:\n");
2d112de7 12242 drm_mode_debug_printmodeline(&pipe_config->base.mode);
c0b03411 12243 DRM_DEBUG_KMS("adjusted mode:\n");
2d112de7
ACO
12244 drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
12245 intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
d71b8d4a 12246 DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
37327abd
VS
12247 DRM_DEBUG_KMS("pipe src size: %dx%d\n",
12248 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
0ec463d3
TU
12249 DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
12250 crtc->num_scalers,
12251 pipe_config->scaler_state.scaler_users,
12252 pipe_config->scaler_state.scaler_id);
c0b03411
DV
12253 DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
12254 pipe_config->gmch_pfit.control,
12255 pipe_config->gmch_pfit.pgm_ratios,
12256 pipe_config->gmch_pfit.lvds_border_bits);
fd4daa9c 12257 DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
c0b03411 12258 pipe_config->pch_pfit.pos,
fd4daa9c
CW
12259 pipe_config->pch_pfit.size,
12260 pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
42db64ef 12261 DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
cf532bb2 12262 DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
6a60cd87 12263
415ff0f6 12264 if (IS_BROXTON(dev)) {
05712c15 12265 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: ebb0: 0x%x, ebb4: 0x%x,"
415ff0f6 12266 "pll0: 0x%x, pll1: 0x%x, pll2: 0x%x, pll3: 0x%x, "
c8453338 12267 "pll6: 0x%x, pll8: 0x%x, pll9: 0x%x, pll10: 0x%x, pcsdw12: 0x%x\n",
415ff0f6
TU
12268 pipe_config->ddi_pll_sel,
12269 pipe_config->dpll_hw_state.ebb0,
05712c15 12270 pipe_config->dpll_hw_state.ebb4,
415ff0f6
TU
12271 pipe_config->dpll_hw_state.pll0,
12272 pipe_config->dpll_hw_state.pll1,
12273 pipe_config->dpll_hw_state.pll2,
12274 pipe_config->dpll_hw_state.pll3,
12275 pipe_config->dpll_hw_state.pll6,
12276 pipe_config->dpll_hw_state.pll8,
05712c15 12277 pipe_config->dpll_hw_state.pll9,
c8453338 12278 pipe_config->dpll_hw_state.pll10,
415ff0f6 12279 pipe_config->dpll_hw_state.pcsdw12);
ef11bdb3 12280 } else if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) {
415ff0f6
TU
12281 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: "
12282 "ctrl1: 0x%x, cfgcr1: 0x%x, cfgcr2: 0x%x\n",
12283 pipe_config->ddi_pll_sel,
12284 pipe_config->dpll_hw_state.ctrl1,
12285 pipe_config->dpll_hw_state.cfgcr1,
12286 pipe_config->dpll_hw_state.cfgcr2);
12287 } else if (HAS_DDI(dev)) {
1260f07e 12288 DRM_DEBUG_KMS("ddi_pll_sel: 0x%x; dpll_hw_state: wrpll: 0x%x spll: 0x%x\n",
415ff0f6 12289 pipe_config->ddi_pll_sel,
00490c22
ML
12290 pipe_config->dpll_hw_state.wrpll,
12291 pipe_config->dpll_hw_state.spll);
415ff0f6
TU
12292 } else {
12293 DRM_DEBUG_KMS("dpll_hw_state: dpll: 0x%x, dpll_md: 0x%x, "
12294 "fp0: 0x%x, fp1: 0x%x\n",
12295 pipe_config->dpll_hw_state.dpll,
12296 pipe_config->dpll_hw_state.dpll_md,
12297 pipe_config->dpll_hw_state.fp0,
12298 pipe_config->dpll_hw_state.fp1);
12299 }
12300
6a60cd87
CK
12301 DRM_DEBUG_KMS("planes on this crtc\n");
12302 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
12303 intel_plane = to_intel_plane(plane);
12304 if (intel_plane->pipe != crtc->pipe)
12305 continue;
12306
12307 state = to_intel_plane_state(plane->state);
12308 fb = state->base.fb;
12309 if (!fb) {
12310 DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d "
12311 "disabled, scaler_id = %d\n",
12312 plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
12313 plane->base.id, intel_plane->pipe,
12314 (crtc->base.primary == plane) ? 0 : intel_plane->plane + 1,
12315 drm_plane_index(plane), state->scaler_id);
12316 continue;
12317 }
12318
12319 DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d enabled",
12320 plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
12321 plane->base.id, intel_plane->pipe,
12322 crtc->base.primary == plane ? 0 : intel_plane->plane + 1,
12323 drm_plane_index(plane));
12324 DRM_DEBUG_KMS("\tFB:%d, fb = %ux%u format = 0x%x",
12325 fb->base.id, fb->width, fb->height, fb->pixel_format);
12326 DRM_DEBUG_KMS("\tscaler:%d src (%u, %u) %ux%u dst (%u, %u) %ux%u\n",
12327 state->scaler_id,
12328 state->src.x1 >> 16, state->src.y1 >> 16,
12329 drm_rect_width(&state->src) >> 16,
12330 drm_rect_height(&state->src) >> 16,
12331 state->dst.x1, state->dst.y1,
12332 drm_rect_width(&state->dst), drm_rect_height(&state->dst));
12333 }
c0b03411
DV
12334}
12335
5448a00d 12336static bool check_digital_port_conflicts(struct drm_atomic_state *state)
00f0b378 12337{
5448a00d 12338 struct drm_device *dev = state->dev;
da3ced29 12339 struct drm_connector *connector;
00f0b378
VS
12340 unsigned int used_ports = 0;
12341
12342 /*
12343 * Walk the connector list instead of the encoder
12344 * list to detect the problem on ddi platforms
12345 * where there's just one encoder per digital port.
12346 */
0bff4858
VS
12347 drm_for_each_connector(connector, dev) {
12348 struct drm_connector_state *connector_state;
12349 struct intel_encoder *encoder;
12350
12351 connector_state = drm_atomic_get_existing_connector_state(state, connector);
12352 if (!connector_state)
12353 connector_state = connector->state;
12354
5448a00d 12355 if (!connector_state->best_encoder)
00f0b378
VS
12356 continue;
12357
5448a00d
ACO
12358 encoder = to_intel_encoder(connector_state->best_encoder);
12359
12360 WARN_ON(!connector_state->crtc);
00f0b378
VS
12361
12362 switch (encoder->type) {
12363 unsigned int port_mask;
12364 case INTEL_OUTPUT_UNKNOWN:
12365 if (WARN_ON(!HAS_DDI(dev)))
12366 break;
12367 case INTEL_OUTPUT_DISPLAYPORT:
12368 case INTEL_OUTPUT_HDMI:
12369 case INTEL_OUTPUT_EDP:
12370 port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
12371
12372 /* the same port mustn't appear more than once */
12373 if (used_ports & port_mask)
12374 return false;
12375
12376 used_ports |= port_mask;
12377 default:
12378 break;
12379 }
12380 }
12381
12382 return true;
12383}
12384
83a57153
ACO
12385static void
12386clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
12387{
12388 struct drm_crtc_state tmp_state;
663a3640 12389 struct intel_crtc_scaler_state scaler_state;
4978cc93 12390 struct intel_dpll_hw_state dpll_hw_state;
8106ddbd 12391 struct intel_shared_dpll *shared_dpll;
8504c74c 12392 uint32_t ddi_pll_sel;
c4e2d043 12393 bool force_thru;
83a57153 12394
7546a384
ACO
12395 /* FIXME: before the switch to atomic started, a new pipe_config was
12396 * kzalloc'd. Code that depends on any field being zero should be
12397 * fixed, so that the crtc_state can be safely duplicated. For now,
12398 * only fields that are know to not cause problems are preserved. */
12399
83a57153 12400 tmp_state = crtc_state->base;
663a3640 12401 scaler_state = crtc_state->scaler_state;
4978cc93
ACO
12402 shared_dpll = crtc_state->shared_dpll;
12403 dpll_hw_state = crtc_state->dpll_hw_state;
8504c74c 12404 ddi_pll_sel = crtc_state->ddi_pll_sel;
c4e2d043 12405 force_thru = crtc_state->pch_pfit.force_thru;
4978cc93 12406
83a57153 12407 memset(crtc_state, 0, sizeof *crtc_state);
4978cc93 12408
83a57153 12409 crtc_state->base = tmp_state;
663a3640 12410 crtc_state->scaler_state = scaler_state;
4978cc93
ACO
12411 crtc_state->shared_dpll = shared_dpll;
12412 crtc_state->dpll_hw_state = dpll_hw_state;
8504c74c 12413 crtc_state->ddi_pll_sel = ddi_pll_sel;
c4e2d043 12414 crtc_state->pch_pfit.force_thru = force_thru;
83a57153
ACO
12415}
12416
548ee15b 12417static int
b8cecdf5 12418intel_modeset_pipe_config(struct drm_crtc *crtc,
b359283a 12419 struct intel_crtc_state *pipe_config)
ee7b9f93 12420{
b359283a 12421 struct drm_atomic_state *state = pipe_config->base.state;
7758a113 12422 struct intel_encoder *encoder;
da3ced29 12423 struct drm_connector *connector;
0b901879 12424 struct drm_connector_state *connector_state;
d328c9d7 12425 int base_bpp, ret = -EINVAL;
0b901879 12426 int i;
e29c22c0 12427 bool retry = true;
ee7b9f93 12428
83a57153 12429 clear_intel_crtc_state(pipe_config);
7758a113 12430
e143a21c
DV
12431 pipe_config->cpu_transcoder =
12432 (enum transcoder) to_intel_crtc(crtc)->pipe;
b8cecdf5 12433
2960bc9c
ID
12434 /*
12435 * Sanitize sync polarity flags based on requested ones. If neither
12436 * positive or negative polarity is requested, treat this as meaning
12437 * negative polarity.
12438 */
2d112de7 12439 if (!(pipe_config->base.adjusted_mode.flags &
2960bc9c 12440 (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
2d112de7 12441 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
2960bc9c 12442
2d112de7 12443 if (!(pipe_config->base.adjusted_mode.flags &
2960bc9c 12444 (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
2d112de7 12445 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
2960bc9c 12446
d328c9d7
DV
12447 base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
12448 pipe_config);
12449 if (base_bpp < 0)
4e53c2e0
DV
12450 goto fail;
12451
e41a56be
VS
12452 /*
12453 * Determine the real pipe dimensions. Note that stereo modes can
12454 * increase the actual pipe size due to the frame doubling and
12455 * insertion of additional space for blanks between the frame. This
12456 * is stored in the crtc timings. We use the requested mode to do this
12457 * computation to clearly distinguish it from the adjusted mode, which
12458 * can be changed by the connectors in the below retry loop.
12459 */
2d112de7 12460 drm_crtc_get_hv_timing(&pipe_config->base.mode,
ecb7e16b
GP
12461 &pipe_config->pipe_src_w,
12462 &pipe_config->pipe_src_h);
e41a56be 12463
e29c22c0 12464encoder_retry:
ef1b460d 12465 /* Ensure the port clock defaults are reset when retrying. */
ff9a6750 12466 pipe_config->port_clock = 0;
ef1b460d 12467 pipe_config->pixel_multiplier = 1;
ff9a6750 12468
135c81b8 12469 /* Fill in default crtc timings, allow encoders to overwrite them. */
2d112de7
ACO
12470 drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
12471 CRTC_STEREO_DOUBLE);
135c81b8 12472
7758a113
DV
12473 /* Pass our mode to the connectors and the CRTC to give them a chance to
12474 * adjust it according to limitations or connector properties, and also
12475 * a chance to reject the mode entirely.
47f1c6c9 12476 */
da3ced29 12477 for_each_connector_in_state(state, connector, connector_state, i) {
0b901879 12478 if (connector_state->crtc != crtc)
7758a113 12479 continue;
7ae89233 12480
0b901879
ACO
12481 encoder = to_intel_encoder(connector_state->best_encoder);
12482
efea6e8e
DV
12483 if (!(encoder->compute_config(encoder, pipe_config))) {
12484 DRM_DEBUG_KMS("Encoder config failure\n");
7758a113
DV
12485 goto fail;
12486 }
ee7b9f93 12487 }
47f1c6c9 12488
ff9a6750
DV
12489 /* Set default port clock if not overwritten by the encoder. Needs to be
12490 * done afterwards in case the encoder adjusts the mode. */
12491 if (!pipe_config->port_clock)
2d112de7 12492 pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
241bfc38 12493 * pipe_config->pixel_multiplier;
ff9a6750 12494
a43f6e0f 12495 ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
e29c22c0 12496 if (ret < 0) {
7758a113
DV
12497 DRM_DEBUG_KMS("CRTC fixup failed\n");
12498 goto fail;
ee7b9f93 12499 }
e29c22c0
DV
12500
12501 if (ret == RETRY) {
12502 if (WARN(!retry, "loop in pipe configuration computation\n")) {
12503 ret = -EINVAL;
12504 goto fail;
12505 }
12506
12507 DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
12508 retry = false;
12509 goto encoder_retry;
12510 }
12511
e8fa4270
DV
12512 /* Dithering seems to not pass-through bits correctly when it should, so
12513 * only enable it on 6bpc panels. */
12514 pipe_config->dither = pipe_config->pipe_bpp == 6*3;
62f0ace5 12515 DRM_DEBUG_KMS("hw max bpp: %i, pipe bpp: %i, dithering: %i\n",
d328c9d7 12516 base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
4e53c2e0 12517
7758a113 12518fail:
548ee15b 12519 return ret;
ee7b9f93 12520}
47f1c6c9 12521
ea9d758d 12522static void
4740b0f2 12523intel_modeset_update_crtc_state(struct drm_atomic_state *state)
ea9d758d 12524{
0a9ab303
ACO
12525 struct drm_crtc *crtc;
12526 struct drm_crtc_state *crtc_state;
8a75d157 12527 int i;
ea9d758d 12528
7668851f 12529 /* Double check state. */
8a75d157 12530 for_each_crtc_in_state(state, crtc, crtc_state, i) {
3cb480bc 12531 to_intel_crtc(crtc)->config = to_intel_crtc_state(crtc->state);
fc467a22
ML
12532
12533 /* Update hwmode for vblank functions */
12534 if (crtc->state->active)
12535 crtc->hwmode = crtc->state->adjusted_mode;
12536 else
12537 crtc->hwmode.crtc_clock = 0;
61067a5e
ML
12538
12539 /*
12540 * Update legacy state to satisfy fbc code. This can
12541 * be removed when fbc uses the atomic state.
12542 */
12543 if (drm_atomic_get_existing_plane_state(state, crtc->primary)) {
12544 struct drm_plane_state *plane_state = crtc->primary->state;
12545
12546 crtc->primary->fb = plane_state->fb;
12547 crtc->x = plane_state->src_x >> 16;
12548 crtc->y = plane_state->src_y >> 16;
12549 }
ea9d758d 12550 }
ea9d758d
DV
12551}
12552
3bd26263 12553static bool intel_fuzzy_clock_check(int clock1, int clock2)
f1f644dc 12554{
3bd26263 12555 int diff;
f1f644dc
JB
12556
12557 if (clock1 == clock2)
12558 return true;
12559
12560 if (!clock1 || !clock2)
12561 return false;
12562
12563 diff = abs(clock1 - clock2);
12564
12565 if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
12566 return true;
12567
12568 return false;
12569}
12570
25c5b266
DV
12571#define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
12572 list_for_each_entry((intel_crtc), \
12573 &(dev)->mode_config.crtc_list, \
12574 base.head) \
95150bdf 12575 for_each_if (mask & (1 <<(intel_crtc)->pipe))
25c5b266 12576
cfb23ed6
ML
12577static bool
12578intel_compare_m_n(unsigned int m, unsigned int n,
12579 unsigned int m2, unsigned int n2,
12580 bool exact)
12581{
12582 if (m == m2 && n == n2)
12583 return true;
12584
12585 if (exact || !m || !n || !m2 || !n2)
12586 return false;
12587
12588 BUILD_BUG_ON(DATA_LINK_M_N_MASK > INT_MAX);
12589
31d10b57
ML
12590 if (n > n2) {
12591 while (n > n2) {
cfb23ed6
ML
12592 m2 <<= 1;
12593 n2 <<= 1;
12594 }
31d10b57
ML
12595 } else if (n < n2) {
12596 while (n < n2) {
cfb23ed6
ML
12597 m <<= 1;
12598 n <<= 1;
12599 }
12600 }
12601
31d10b57
ML
12602 if (n != n2)
12603 return false;
12604
12605 return intel_fuzzy_clock_check(m, m2);
cfb23ed6
ML
12606}
12607
12608static bool
12609intel_compare_link_m_n(const struct intel_link_m_n *m_n,
12610 struct intel_link_m_n *m2_n2,
12611 bool adjust)
12612{
12613 if (m_n->tu == m2_n2->tu &&
12614 intel_compare_m_n(m_n->gmch_m, m_n->gmch_n,
12615 m2_n2->gmch_m, m2_n2->gmch_n, !adjust) &&
12616 intel_compare_m_n(m_n->link_m, m_n->link_n,
12617 m2_n2->link_m, m2_n2->link_n, !adjust)) {
12618 if (adjust)
12619 *m2_n2 = *m_n;
12620
12621 return true;
12622 }
12623
12624 return false;
12625}
12626
0e8ffe1b 12627static bool
2fa2fe9a 12628intel_pipe_config_compare(struct drm_device *dev,
5cec258b 12629 struct intel_crtc_state *current_config,
cfb23ed6
ML
12630 struct intel_crtc_state *pipe_config,
12631 bool adjust)
0e8ffe1b 12632{
cfb23ed6
ML
12633 bool ret = true;
12634
12635#define INTEL_ERR_OR_DBG_KMS(fmt, ...) \
12636 do { \
12637 if (!adjust) \
12638 DRM_ERROR(fmt, ##__VA_ARGS__); \
12639 else \
12640 DRM_DEBUG_KMS(fmt, ##__VA_ARGS__); \
12641 } while (0)
12642
66e985c0
DV
12643#define PIPE_CONF_CHECK_X(name) \
12644 if (current_config->name != pipe_config->name) { \
cfb23ed6 12645 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
66e985c0
DV
12646 "(expected 0x%08x, found 0x%08x)\n", \
12647 current_config->name, \
12648 pipe_config->name); \
cfb23ed6 12649 ret = false; \
66e985c0
DV
12650 }
12651
08a24034
DV
12652#define PIPE_CONF_CHECK_I(name) \
12653 if (current_config->name != pipe_config->name) { \
cfb23ed6 12654 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
08a24034
DV
12655 "(expected %i, found %i)\n", \
12656 current_config->name, \
12657 pipe_config->name); \
cfb23ed6
ML
12658 ret = false; \
12659 }
12660
8106ddbd
ACO
12661#define PIPE_CONF_CHECK_P(name) \
12662 if (current_config->name != pipe_config->name) { \
12663 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
12664 "(expected %p, found %p)\n", \
12665 current_config->name, \
12666 pipe_config->name); \
12667 ret = false; \
12668 }
12669
cfb23ed6
ML
12670#define PIPE_CONF_CHECK_M_N(name) \
12671 if (!intel_compare_link_m_n(&current_config->name, \
12672 &pipe_config->name,\
12673 adjust)) { \
12674 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
12675 "(expected tu %i gmch %i/%i link %i/%i, " \
12676 "found tu %i, gmch %i/%i link %i/%i)\n", \
12677 current_config->name.tu, \
12678 current_config->name.gmch_m, \
12679 current_config->name.gmch_n, \
12680 current_config->name.link_m, \
12681 current_config->name.link_n, \
12682 pipe_config->name.tu, \
12683 pipe_config->name.gmch_m, \
12684 pipe_config->name.gmch_n, \
12685 pipe_config->name.link_m, \
12686 pipe_config->name.link_n); \
12687 ret = false; \
12688 }
12689
12690#define PIPE_CONF_CHECK_M_N_ALT(name, alt_name) \
12691 if (!intel_compare_link_m_n(&current_config->name, \
12692 &pipe_config->name, adjust) && \
12693 !intel_compare_link_m_n(&current_config->alt_name, \
12694 &pipe_config->name, adjust)) { \
12695 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
12696 "(expected tu %i gmch %i/%i link %i/%i, " \
12697 "or tu %i gmch %i/%i link %i/%i, " \
12698 "found tu %i, gmch %i/%i link %i/%i)\n", \
12699 current_config->name.tu, \
12700 current_config->name.gmch_m, \
12701 current_config->name.gmch_n, \
12702 current_config->name.link_m, \
12703 current_config->name.link_n, \
12704 current_config->alt_name.tu, \
12705 current_config->alt_name.gmch_m, \
12706 current_config->alt_name.gmch_n, \
12707 current_config->alt_name.link_m, \
12708 current_config->alt_name.link_n, \
12709 pipe_config->name.tu, \
12710 pipe_config->name.gmch_m, \
12711 pipe_config->name.gmch_n, \
12712 pipe_config->name.link_m, \
12713 pipe_config->name.link_n); \
12714 ret = false; \
88adfff1
DV
12715 }
12716
b95af8be
VK
12717/* This is required for BDW+ where there is only one set of registers for
12718 * switching between high and low RR.
12719 * This macro can be used whenever a comparison has to be made between one
12720 * hw state and multiple sw state variables.
12721 */
12722#define PIPE_CONF_CHECK_I_ALT(name, alt_name) \
12723 if ((current_config->name != pipe_config->name) && \
12724 (current_config->alt_name != pipe_config->name)) { \
cfb23ed6 12725 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
b95af8be
VK
12726 "(expected %i or %i, found %i)\n", \
12727 current_config->name, \
12728 current_config->alt_name, \
12729 pipe_config->name); \
cfb23ed6 12730 ret = false; \
b95af8be
VK
12731 }
12732
1bd1bd80
DV
12733#define PIPE_CONF_CHECK_FLAGS(name, mask) \
12734 if ((current_config->name ^ pipe_config->name) & (mask)) { \
cfb23ed6 12735 INTEL_ERR_OR_DBG_KMS("mismatch in " #name "(" #mask ") " \
1bd1bd80
DV
12736 "(expected %i, found %i)\n", \
12737 current_config->name & (mask), \
12738 pipe_config->name & (mask)); \
cfb23ed6 12739 ret = false; \
1bd1bd80
DV
12740 }
12741
5e550656
VS
12742#define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
12743 if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
cfb23ed6 12744 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
5e550656
VS
12745 "(expected %i, found %i)\n", \
12746 current_config->name, \
12747 pipe_config->name); \
cfb23ed6 12748 ret = false; \
5e550656
VS
12749 }
12750
bb760063
DV
12751#define PIPE_CONF_QUIRK(quirk) \
12752 ((current_config->quirks | pipe_config->quirks) & (quirk))
12753
eccb140b
DV
12754 PIPE_CONF_CHECK_I(cpu_transcoder);
12755
08a24034
DV
12756 PIPE_CONF_CHECK_I(has_pch_encoder);
12757 PIPE_CONF_CHECK_I(fdi_lanes);
cfb23ed6 12758 PIPE_CONF_CHECK_M_N(fdi_m_n);
08a24034 12759
eb14cb74 12760 PIPE_CONF_CHECK_I(has_dp_encoder);
90a6b7b0 12761 PIPE_CONF_CHECK_I(lane_count);
b95af8be
VK
12762
12763 if (INTEL_INFO(dev)->gen < 8) {
cfb23ed6
ML
12764 PIPE_CONF_CHECK_M_N(dp_m_n);
12765
cfb23ed6
ML
12766 if (current_config->has_drrs)
12767 PIPE_CONF_CHECK_M_N(dp_m2_n2);
12768 } else
12769 PIPE_CONF_CHECK_M_N_ALT(dp_m_n, dp_m2_n2);
eb14cb74 12770
a65347ba
JN
12771 PIPE_CONF_CHECK_I(has_dsi_encoder);
12772
2d112de7
ACO
12773 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
12774 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
12775 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
12776 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
12777 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
12778 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
1bd1bd80 12779
2d112de7
ACO
12780 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
12781 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
12782 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
12783 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
12784 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
12785 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
1bd1bd80 12786
c93f54cf 12787 PIPE_CONF_CHECK_I(pixel_multiplier);
6897b4b5 12788 PIPE_CONF_CHECK_I(has_hdmi_sink);
b5a9fa09 12789 if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
666a4537 12790 IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev))
b5a9fa09 12791 PIPE_CONF_CHECK_I(limited_color_range);
e43823ec 12792 PIPE_CONF_CHECK_I(has_infoframe);
6c49f241 12793
9ed109a7
DV
12794 PIPE_CONF_CHECK_I(has_audio);
12795
2d112de7 12796 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
1bd1bd80
DV
12797 DRM_MODE_FLAG_INTERLACE);
12798
bb760063 12799 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
2d112de7 12800 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063 12801 DRM_MODE_FLAG_PHSYNC);
2d112de7 12802 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063 12803 DRM_MODE_FLAG_NHSYNC);
2d112de7 12804 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063 12805 DRM_MODE_FLAG_PVSYNC);
2d112de7 12806 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063
DV
12807 DRM_MODE_FLAG_NVSYNC);
12808 }
045ac3b5 12809
333b8ca8 12810 PIPE_CONF_CHECK_X(gmch_pfit.control);
e2ff2d4a
DV
12811 /* pfit ratios are autocomputed by the hw on gen4+ */
12812 if (INTEL_INFO(dev)->gen < 4)
12813 PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
333b8ca8 12814 PIPE_CONF_CHECK_X(gmch_pfit.lvds_border_bits);
9953599b 12815
bfd16b2a
ML
12816 if (!adjust) {
12817 PIPE_CONF_CHECK_I(pipe_src_w);
12818 PIPE_CONF_CHECK_I(pipe_src_h);
12819
12820 PIPE_CONF_CHECK_I(pch_pfit.enabled);
12821 if (current_config->pch_pfit.enabled) {
12822 PIPE_CONF_CHECK_X(pch_pfit.pos);
12823 PIPE_CONF_CHECK_X(pch_pfit.size);
12824 }
2fa2fe9a 12825
7aefe2b5
ML
12826 PIPE_CONF_CHECK_I(scaler_state.scaler_id);
12827 }
a1b2278e 12828
e59150dc
JB
12829 /* BDW+ don't expose a synchronous way to read the state */
12830 if (IS_HASWELL(dev))
12831 PIPE_CONF_CHECK_I(ips_enabled);
42db64ef 12832
282740f7
VS
12833 PIPE_CONF_CHECK_I(double_wide);
12834
26804afd
DV
12835 PIPE_CONF_CHECK_X(ddi_pll_sel);
12836
8106ddbd 12837 PIPE_CONF_CHECK_P(shared_dpll);
66e985c0 12838 PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
8bcc2795 12839 PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
66e985c0
DV
12840 PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
12841 PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
d452c5b6 12842 PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
00490c22 12843 PIPE_CONF_CHECK_X(dpll_hw_state.spll);
3f4cd19f
DL
12844 PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
12845 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
12846 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
c0d43d62 12847
42571aef
VS
12848 if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
12849 PIPE_CONF_CHECK_I(pipe_bpp);
12850
2d112de7 12851 PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
a9a7e98a 12852 PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
5e550656 12853
66e985c0 12854#undef PIPE_CONF_CHECK_X
08a24034 12855#undef PIPE_CONF_CHECK_I
8106ddbd 12856#undef PIPE_CONF_CHECK_P
b95af8be 12857#undef PIPE_CONF_CHECK_I_ALT
1bd1bd80 12858#undef PIPE_CONF_CHECK_FLAGS
5e550656 12859#undef PIPE_CONF_CHECK_CLOCK_FUZZY
bb760063 12860#undef PIPE_CONF_QUIRK
cfb23ed6 12861#undef INTEL_ERR_OR_DBG_KMS
88adfff1 12862
cfb23ed6 12863 return ret;
0e8ffe1b
DV
12864}
12865
e3b247da
VS
12866static void intel_pipe_config_sanity_check(struct drm_i915_private *dev_priv,
12867 const struct intel_crtc_state *pipe_config)
12868{
12869 if (pipe_config->has_pch_encoder) {
21a727b3 12870 int fdi_dotclock = intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
e3b247da
VS
12871 &pipe_config->fdi_m_n);
12872 int dotclock = pipe_config->base.adjusted_mode.crtc_clock;
12873
12874 /*
12875 * FDI already provided one idea for the dotclock.
12876 * Yell if the encoder disagrees.
12877 */
12878 WARN(!intel_fuzzy_clock_check(fdi_dotclock, dotclock),
12879 "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
12880 fdi_dotclock, dotclock);
12881 }
12882}
12883
08db6652
DL
12884static void check_wm_state(struct drm_device *dev)
12885{
12886 struct drm_i915_private *dev_priv = dev->dev_private;
12887 struct skl_ddb_allocation hw_ddb, *sw_ddb;
12888 struct intel_crtc *intel_crtc;
12889 int plane;
12890
12891 if (INTEL_INFO(dev)->gen < 9)
12892 return;
12893
12894 skl_ddb_get_hw_state(dev_priv, &hw_ddb);
12895 sw_ddb = &dev_priv->wm.skl_hw.ddb;
12896
12897 for_each_intel_crtc(dev, intel_crtc) {
12898 struct skl_ddb_entry *hw_entry, *sw_entry;
12899 const enum pipe pipe = intel_crtc->pipe;
12900
12901 if (!intel_crtc->active)
12902 continue;
12903
12904 /* planes */
dd740780 12905 for_each_plane(dev_priv, pipe, plane) {
08db6652
DL
12906 hw_entry = &hw_ddb.plane[pipe][plane];
12907 sw_entry = &sw_ddb->plane[pipe][plane];
12908
12909 if (skl_ddb_entry_equal(hw_entry, sw_entry))
12910 continue;
12911
12912 DRM_ERROR("mismatch in DDB state pipe %c plane %d "
12913 "(expected (%u,%u), found (%u,%u))\n",
12914 pipe_name(pipe), plane + 1,
12915 sw_entry->start, sw_entry->end,
12916 hw_entry->start, hw_entry->end);
12917 }
12918
12919 /* cursor */
4969d33e
MR
12920 hw_entry = &hw_ddb.plane[pipe][PLANE_CURSOR];
12921 sw_entry = &sw_ddb->plane[pipe][PLANE_CURSOR];
08db6652
DL
12922
12923 if (skl_ddb_entry_equal(hw_entry, sw_entry))
12924 continue;
12925
12926 DRM_ERROR("mismatch in DDB state pipe %c cursor "
12927 "(expected (%u,%u), found (%u,%u))\n",
12928 pipe_name(pipe),
12929 sw_entry->start, sw_entry->end,
12930 hw_entry->start, hw_entry->end);
12931 }
12932}
12933
91d1b4bd 12934static void
35dd3c64
ML
12935check_connector_state(struct drm_device *dev,
12936 struct drm_atomic_state *old_state)
8af6cf88 12937{
35dd3c64
ML
12938 struct drm_connector_state *old_conn_state;
12939 struct drm_connector *connector;
12940 int i;
8af6cf88 12941
35dd3c64
ML
12942 for_each_connector_in_state(old_state, connector, old_conn_state, i) {
12943 struct drm_encoder *encoder = connector->encoder;
12944 struct drm_connector_state *state = connector->state;
ad3c558f 12945
8af6cf88
DV
12946 /* This also checks the encoder/connector hw state with the
12947 * ->get_hw_state callbacks. */
35dd3c64 12948 intel_connector_check_state(to_intel_connector(connector));
8af6cf88 12949
ad3c558f 12950 I915_STATE_WARN(state->best_encoder != encoder,
35dd3c64 12951 "connector's atomic encoder doesn't match legacy encoder\n");
8af6cf88 12952 }
91d1b4bd
DV
12953}
12954
12955static void
12956check_encoder_state(struct drm_device *dev)
12957{
12958 struct intel_encoder *encoder;
12959 struct intel_connector *connector;
8af6cf88 12960
b2784e15 12961 for_each_intel_encoder(dev, encoder) {
8af6cf88 12962 bool enabled = false;
4d20cd86 12963 enum pipe pipe;
8af6cf88
DV
12964
12965 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
12966 encoder->base.base.id,
8e329a03 12967 encoder->base.name);
8af6cf88 12968
3a3371ff 12969 for_each_intel_connector(dev, connector) {
4d20cd86 12970 if (connector->base.state->best_encoder != &encoder->base)
8af6cf88
DV
12971 continue;
12972 enabled = true;
ad3c558f
ML
12973
12974 I915_STATE_WARN(connector->base.state->crtc !=
12975 encoder->base.crtc,
12976 "connector's crtc doesn't match encoder crtc\n");
8af6cf88 12977 }
0e32b39c 12978
e2c719b7 12979 I915_STATE_WARN(!!encoder->base.crtc != enabled,
8af6cf88
DV
12980 "encoder's enabled state mismatch "
12981 "(expected %i, found %i)\n",
12982 !!encoder->base.crtc, enabled);
7c60d198
ML
12983
12984 if (!encoder->base.crtc) {
4d20cd86 12985 bool active;
7c60d198 12986
4d20cd86
ML
12987 active = encoder->get_hw_state(encoder, &pipe);
12988 I915_STATE_WARN(active,
12989 "encoder detached but still enabled on pipe %c.\n",
12990 pipe_name(pipe));
7c60d198 12991 }
8af6cf88 12992 }
91d1b4bd
DV
12993}
12994
12995static void
4d20cd86 12996check_crtc_state(struct drm_device *dev, struct drm_atomic_state *old_state)
91d1b4bd 12997{
fbee40df 12998 struct drm_i915_private *dev_priv = dev->dev_private;
91d1b4bd 12999 struct intel_encoder *encoder;
4d20cd86
ML
13000 struct drm_crtc_state *old_crtc_state;
13001 struct drm_crtc *crtc;
13002 int i;
8af6cf88 13003
4d20cd86
ML
13004 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
13005 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13006 struct intel_crtc_state *pipe_config, *sw_config;
7b89b8de 13007 bool active;
8af6cf88 13008
bfd16b2a
ML
13009 if (!needs_modeset(crtc->state) &&
13010 !to_intel_crtc_state(crtc->state)->update_pipe)
4d20cd86 13011 continue;
045ac3b5 13012
4d20cd86
ML
13013 __drm_atomic_helper_crtc_destroy_state(crtc, old_crtc_state);
13014 pipe_config = to_intel_crtc_state(old_crtc_state);
13015 memset(pipe_config, 0, sizeof(*pipe_config));
13016 pipe_config->base.crtc = crtc;
13017 pipe_config->base.state = old_state;
8af6cf88 13018
4d20cd86
ML
13019 DRM_DEBUG_KMS("[CRTC:%d]\n",
13020 crtc->base.id);
8af6cf88 13021
4d20cd86
ML
13022 active = dev_priv->display.get_pipe_config(intel_crtc,
13023 pipe_config);
d62cf62a 13024
b6b5d049 13025 /* hw state is inconsistent with the pipe quirk */
4d20cd86
ML
13026 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
13027 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
13028 active = crtc->state->active;
6c49f241 13029
4d20cd86 13030 I915_STATE_WARN(crtc->state->active != active,
0e8ffe1b 13031 "crtc active state doesn't match with hw state "
4d20cd86 13032 "(expected %i, found %i)\n", crtc->state->active, active);
0e8ffe1b 13033
4d20cd86 13034 I915_STATE_WARN(intel_crtc->active != crtc->state->active,
53d9f4e9 13035 "transitional active state does not match atomic hw state "
4d20cd86
ML
13036 "(expected %i, found %i)\n", crtc->state->active, intel_crtc->active);
13037
13038 for_each_encoder_on_crtc(dev, crtc, encoder) {
13039 enum pipe pipe;
13040
13041 active = encoder->get_hw_state(encoder, &pipe);
13042 I915_STATE_WARN(active != crtc->state->active,
13043 "[ENCODER:%i] active %i with crtc active %i\n",
13044 encoder->base.base.id, active, crtc->state->active);
13045
13046 I915_STATE_WARN(active && intel_crtc->pipe != pipe,
13047 "Encoder connected to wrong pipe %c\n",
13048 pipe_name(pipe));
13049
13050 if (active)
13051 encoder->get_config(encoder, pipe_config);
13052 }
53d9f4e9 13053
4d20cd86 13054 if (!crtc->state->active)
cfb23ed6
ML
13055 continue;
13056
e3b247da
VS
13057 intel_pipe_config_sanity_check(dev_priv, pipe_config);
13058
4d20cd86
ML
13059 sw_config = to_intel_crtc_state(crtc->state);
13060 if (!intel_pipe_config_compare(dev, sw_config,
13061 pipe_config, false)) {
e2c719b7 13062 I915_STATE_WARN(1, "pipe state doesn't match!\n");
4d20cd86 13063 intel_dump_pipe_config(intel_crtc, pipe_config,
c0b03411 13064 "[hw state]");
4d20cd86 13065 intel_dump_pipe_config(intel_crtc, sw_config,
c0b03411
DV
13066 "[sw state]");
13067 }
8af6cf88
DV
13068 }
13069}
13070
91d1b4bd
DV
13071static void
13072check_shared_dpll_state(struct drm_device *dev)
13073{
fbee40df 13074 struct drm_i915_private *dev_priv = dev->dev_private;
91d1b4bd
DV
13075 struct intel_crtc *crtc;
13076 struct intel_dpll_hw_state dpll_hw_state;
13077 int i;
5358901f
DV
13078
13079 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
8106ddbd
ACO
13080 struct intel_shared_dpll *pll =
13081 intel_get_shared_dpll_by_id(dev_priv, i);
2dd66ebd 13082 unsigned enabled_crtcs = 0, active_crtcs = 0;
5358901f
DV
13083 bool active;
13084
13085 memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
13086
13087 DRM_DEBUG_KMS("%s\n", pll->name);
13088
2edd6443 13089 active = pll->funcs.get_hw_state(dev_priv, pll, &dpll_hw_state);
5358901f 13090
2dd66ebd
ML
13091 I915_STATE_WARN(pll->active_mask & ~pll->config.crtc_mask,
13092 "more active pll users than references: %x vs %x\n",
13093 pll->active_mask, pll->config.crtc_mask);
9d16da65
ACO
13094
13095 if (!(pll->flags & INTEL_DPLL_ALWAYS_ON)) {
2dd66ebd
ML
13096 I915_STATE_WARN(!pll->on && pll->active_mask,
13097 "pll in active use but not on in sw tracking\n");
13098 I915_STATE_WARN(pll->on && !pll->active_mask,
13099 "pll is on but not used by any active crtc\n");
9d16da65
ACO
13100 I915_STATE_WARN(pll->on != active,
13101 "pll on state mismatch (expected %i, found %i)\n",
13102 pll->on, active);
13103 }
5358901f 13104
d3fcc808 13105 for_each_intel_crtc(dev, crtc) {
8106ddbd 13106 if (crtc->base.state->enable && crtc->config->shared_dpll == pll)
2dd66ebd
ML
13107 enabled_crtcs |= 1 << drm_crtc_index(&crtc->base);
13108 if (crtc->base.state->active && crtc->config->shared_dpll == pll)
13109 active_crtcs |= 1 << drm_crtc_index(&crtc->base);
5358901f 13110 }
2dd66ebd
ML
13111
13112 I915_STATE_WARN(pll->active_mask != active_crtcs,
13113 "pll active crtcs mismatch (expected %x, found %x)\n",
13114 pll->active_mask, active_crtcs);
13115 I915_STATE_WARN(pll->config.crtc_mask != enabled_crtcs,
13116 "pll enabled crtcs mismatch (expected %x, found %x)\n",
13117 pll->config.crtc_mask, enabled_crtcs);
66e985c0 13118
e2c719b7 13119 I915_STATE_WARN(pll->on && memcmp(&pll->config.hw_state, &dpll_hw_state,
66e985c0
DV
13120 sizeof(dpll_hw_state)),
13121 "pll hw state mismatch\n");
5358901f 13122 }
8af6cf88
DV
13123}
13124
ee165b1a
ML
13125static void
13126intel_modeset_check_state(struct drm_device *dev,
13127 struct drm_atomic_state *old_state)
91d1b4bd 13128{
08db6652 13129 check_wm_state(dev);
35dd3c64 13130 check_connector_state(dev, old_state);
91d1b4bd 13131 check_encoder_state(dev);
4d20cd86 13132 check_crtc_state(dev, old_state);
91d1b4bd
DV
13133 check_shared_dpll_state(dev);
13134}
13135
80715b2f
VS
13136static void update_scanline_offset(struct intel_crtc *crtc)
13137{
13138 struct drm_device *dev = crtc->base.dev;
13139
13140 /*
13141 * The scanline counter increments at the leading edge of hsync.
13142 *
13143 * On most platforms it starts counting from vtotal-1 on the
13144 * first active line. That means the scanline counter value is
13145 * always one less than what we would expect. Ie. just after
13146 * start of vblank, which also occurs at start of hsync (on the
13147 * last active line), the scanline counter will read vblank_start-1.
13148 *
13149 * On gen2 the scanline counter starts counting from 1 instead
13150 * of vtotal-1, so we have to subtract one (or rather add vtotal-1
13151 * to keep the value positive), instead of adding one.
13152 *
13153 * On HSW+ the behaviour of the scanline counter depends on the output
13154 * type. For DP ports it behaves like most other platforms, but on HDMI
13155 * there's an extra 1 line difference. So we need to add two instead of
13156 * one to the value.
13157 */
13158 if (IS_GEN2(dev)) {
124abe07 13159 const struct drm_display_mode *adjusted_mode = &crtc->config->base.adjusted_mode;
80715b2f
VS
13160 int vtotal;
13161
124abe07
VS
13162 vtotal = adjusted_mode->crtc_vtotal;
13163 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
80715b2f
VS
13164 vtotal /= 2;
13165
13166 crtc->scanline_offset = vtotal - 1;
13167 } else if (HAS_DDI(dev) &&
409ee761 13168 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI)) {
80715b2f
VS
13169 crtc->scanline_offset = 2;
13170 } else
13171 crtc->scanline_offset = 1;
13172}
13173
ad421372 13174static void intel_modeset_clear_plls(struct drm_atomic_state *state)
ed6739ef 13175{
225da59b 13176 struct drm_device *dev = state->dev;
ed6739ef 13177 struct drm_i915_private *dev_priv = to_i915(dev);
ad421372 13178 struct intel_shared_dpll_config *shared_dpll = NULL;
0a9ab303
ACO
13179 struct drm_crtc *crtc;
13180 struct drm_crtc_state *crtc_state;
0a9ab303 13181 int i;
ed6739ef
ACO
13182
13183 if (!dev_priv->display.crtc_compute_clock)
ad421372 13184 return;
ed6739ef 13185
0a9ab303 13186 for_each_crtc_in_state(state, crtc, crtc_state, i) {
fb1a38a9 13187 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8106ddbd
ACO
13188 struct intel_shared_dpll *old_dpll =
13189 to_intel_crtc_state(crtc->state)->shared_dpll;
0a9ab303 13190
fb1a38a9 13191 if (!needs_modeset(crtc_state))
225da59b
ACO
13192 continue;
13193
8106ddbd 13194 to_intel_crtc_state(crtc_state)->shared_dpll = NULL;
fb1a38a9 13195
8106ddbd 13196 if (!old_dpll)
fb1a38a9 13197 continue;
0a9ab303 13198
ad421372
ML
13199 if (!shared_dpll)
13200 shared_dpll = intel_atomic_get_shared_dpll_state(state);
ed6739ef 13201
8106ddbd 13202 intel_shared_dpll_config_put(shared_dpll, old_dpll, intel_crtc);
ad421372 13203 }
ed6739ef
ACO
13204}
13205
99d736a2
ML
13206/*
13207 * This implements the workaround described in the "notes" section of the mode
13208 * set sequence documentation. When going from no pipes or single pipe to
13209 * multiple pipes, and planes are enabled after the pipe, we need to wait at
13210 * least 2 vblanks on the first pipe before enabling planes on the second pipe.
13211 */
13212static int haswell_mode_set_planes_workaround(struct drm_atomic_state *state)
13213{
13214 struct drm_crtc_state *crtc_state;
13215 struct intel_crtc *intel_crtc;
13216 struct drm_crtc *crtc;
13217 struct intel_crtc_state *first_crtc_state = NULL;
13218 struct intel_crtc_state *other_crtc_state = NULL;
13219 enum pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
13220 int i;
13221
13222 /* look at all crtc's that are going to be enabled in during modeset */
13223 for_each_crtc_in_state(state, crtc, crtc_state, i) {
13224 intel_crtc = to_intel_crtc(crtc);
13225
13226 if (!crtc_state->active || !needs_modeset(crtc_state))
13227 continue;
13228
13229 if (first_crtc_state) {
13230 other_crtc_state = to_intel_crtc_state(crtc_state);
13231 break;
13232 } else {
13233 first_crtc_state = to_intel_crtc_state(crtc_state);
13234 first_pipe = intel_crtc->pipe;
13235 }
13236 }
13237
13238 /* No workaround needed? */
13239 if (!first_crtc_state)
13240 return 0;
13241
13242 /* w/a possibly needed, check how many crtc's are already enabled. */
13243 for_each_intel_crtc(state->dev, intel_crtc) {
13244 struct intel_crtc_state *pipe_config;
13245
13246 pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
13247 if (IS_ERR(pipe_config))
13248 return PTR_ERR(pipe_config);
13249
13250 pipe_config->hsw_workaround_pipe = INVALID_PIPE;
13251
13252 if (!pipe_config->base.active ||
13253 needs_modeset(&pipe_config->base))
13254 continue;
13255
13256 /* 2 or more enabled crtcs means no need for w/a */
13257 if (enabled_pipe != INVALID_PIPE)
13258 return 0;
13259
13260 enabled_pipe = intel_crtc->pipe;
13261 }
13262
13263 if (enabled_pipe != INVALID_PIPE)
13264 first_crtc_state->hsw_workaround_pipe = enabled_pipe;
13265 else if (other_crtc_state)
13266 other_crtc_state->hsw_workaround_pipe = first_pipe;
13267
13268 return 0;
13269}
13270
27c329ed
ML
13271static int intel_modeset_all_pipes(struct drm_atomic_state *state)
13272{
13273 struct drm_crtc *crtc;
13274 struct drm_crtc_state *crtc_state;
13275 int ret = 0;
13276
13277 /* add all active pipes to the state */
13278 for_each_crtc(state->dev, crtc) {
13279 crtc_state = drm_atomic_get_crtc_state(state, crtc);
13280 if (IS_ERR(crtc_state))
13281 return PTR_ERR(crtc_state);
13282
13283 if (!crtc_state->active || needs_modeset(crtc_state))
13284 continue;
13285
13286 crtc_state->mode_changed = true;
13287
13288 ret = drm_atomic_add_affected_connectors(state, crtc);
13289 if (ret)
13290 break;
13291
13292 ret = drm_atomic_add_affected_planes(state, crtc);
13293 if (ret)
13294 break;
13295 }
13296
13297 return ret;
13298}
13299
c347a676 13300static int intel_modeset_checks(struct drm_atomic_state *state)
054518dd 13301{
565602d7
ML
13302 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
13303 struct drm_i915_private *dev_priv = state->dev->dev_private;
13304 struct drm_crtc *crtc;
13305 struct drm_crtc_state *crtc_state;
13306 int ret = 0, i;
054518dd 13307
b359283a
ML
13308 if (!check_digital_port_conflicts(state)) {
13309 DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
13310 return -EINVAL;
13311 }
13312
565602d7
ML
13313 intel_state->modeset = true;
13314 intel_state->active_crtcs = dev_priv->active_crtcs;
13315
13316 for_each_crtc_in_state(state, crtc, crtc_state, i) {
13317 if (crtc_state->active)
13318 intel_state->active_crtcs |= 1 << i;
13319 else
13320 intel_state->active_crtcs &= ~(1 << i);
13321 }
13322
054518dd
ACO
13323 /*
13324 * See if the config requires any additional preparation, e.g.
13325 * to adjust global state with pipes off. We need to do this
13326 * here so we can get the modeset_pipe updated config for the new
13327 * mode set on this crtc. For other crtcs we need to use the
13328 * adjusted_mode bits in the crtc directly.
13329 */
27c329ed 13330 if (dev_priv->display.modeset_calc_cdclk) {
27c329ed
ML
13331 ret = dev_priv->display.modeset_calc_cdclk(state);
13332
1a617b77 13333 if (!ret && intel_state->dev_cdclk != dev_priv->cdclk_freq)
27c329ed
ML
13334 ret = intel_modeset_all_pipes(state);
13335
13336 if (ret < 0)
054518dd 13337 return ret;
e8788cbc
ML
13338
13339 DRM_DEBUG_KMS("New cdclk calculated to be atomic %u, actual %u\n",
13340 intel_state->cdclk, intel_state->dev_cdclk);
27c329ed 13341 } else
1a617b77 13342 to_intel_atomic_state(state)->cdclk = dev_priv->atomic_cdclk_freq;
054518dd 13343
ad421372 13344 intel_modeset_clear_plls(state);
054518dd 13345
565602d7 13346 if (IS_HASWELL(dev_priv))
ad421372 13347 return haswell_mode_set_planes_workaround(state);
99d736a2 13348
ad421372 13349 return 0;
c347a676
ACO
13350}
13351
aa363136
MR
13352/*
13353 * Handle calculation of various watermark data at the end of the atomic check
13354 * phase. The code here should be run after the per-crtc and per-plane 'check'
13355 * handlers to ensure that all derived state has been updated.
13356 */
13357static void calc_watermark_data(struct drm_atomic_state *state)
13358{
13359 struct drm_device *dev = state->dev;
13360 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
13361 struct drm_crtc *crtc;
13362 struct drm_crtc_state *cstate;
13363 struct drm_plane *plane;
13364 struct drm_plane_state *pstate;
13365
13366 /*
13367 * Calculate watermark configuration details now that derived
13368 * plane/crtc state is all properly updated.
13369 */
13370 drm_for_each_crtc(crtc, dev) {
13371 cstate = drm_atomic_get_existing_crtc_state(state, crtc) ?:
13372 crtc->state;
13373
13374 if (cstate->active)
13375 intel_state->wm_config.num_pipes_active++;
13376 }
13377 drm_for_each_legacy_plane(plane, dev) {
13378 pstate = drm_atomic_get_existing_plane_state(state, plane) ?:
13379 plane->state;
13380
13381 if (!to_intel_plane_state(pstate)->visible)
13382 continue;
13383
13384 intel_state->wm_config.sprites_enabled = true;
13385 if (pstate->crtc_w != pstate->src_w >> 16 ||
13386 pstate->crtc_h != pstate->src_h >> 16)
13387 intel_state->wm_config.sprites_scaled = true;
13388 }
13389}
13390
74c090b1
ML
13391/**
13392 * intel_atomic_check - validate state object
13393 * @dev: drm device
13394 * @state: state to validate
13395 */
13396static int intel_atomic_check(struct drm_device *dev,
13397 struct drm_atomic_state *state)
c347a676 13398{
dd8b3bdb 13399 struct drm_i915_private *dev_priv = to_i915(dev);
aa363136 13400 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
c347a676
ACO
13401 struct drm_crtc *crtc;
13402 struct drm_crtc_state *crtc_state;
13403 int ret, i;
61333b60 13404 bool any_ms = false;
c347a676 13405
74c090b1 13406 ret = drm_atomic_helper_check_modeset(dev, state);
054518dd
ACO
13407 if (ret)
13408 return ret;
13409
c347a676 13410 for_each_crtc_in_state(state, crtc, crtc_state, i) {
cfb23ed6
ML
13411 struct intel_crtc_state *pipe_config =
13412 to_intel_crtc_state(crtc_state);
1ed51de9
DV
13413
13414 /* Catch I915_MODE_FLAG_INHERITED */
13415 if (crtc_state->mode.private_flags != crtc->state->mode.private_flags)
13416 crtc_state->mode_changed = true;
cfb23ed6 13417
61333b60
ML
13418 if (!crtc_state->enable) {
13419 if (needs_modeset(crtc_state))
13420 any_ms = true;
c347a676 13421 continue;
61333b60 13422 }
c347a676 13423
26495481 13424 if (!needs_modeset(crtc_state))
cfb23ed6
ML
13425 continue;
13426
26495481
DV
13427 /* FIXME: For only active_changed we shouldn't need to do any
13428 * state recomputation at all. */
13429
1ed51de9
DV
13430 ret = drm_atomic_add_affected_connectors(state, crtc);
13431 if (ret)
13432 return ret;
b359283a 13433
cfb23ed6 13434 ret = intel_modeset_pipe_config(crtc, pipe_config);
c347a676
ACO
13435 if (ret)
13436 return ret;
13437
73831236 13438 if (i915.fastboot &&
dd8b3bdb 13439 intel_pipe_config_compare(dev,
cfb23ed6 13440 to_intel_crtc_state(crtc->state),
1ed51de9 13441 pipe_config, true)) {
26495481 13442 crtc_state->mode_changed = false;
bfd16b2a 13443 to_intel_crtc_state(crtc_state)->update_pipe = true;
26495481
DV
13444 }
13445
13446 if (needs_modeset(crtc_state)) {
13447 any_ms = true;
cfb23ed6
ML
13448
13449 ret = drm_atomic_add_affected_planes(state, crtc);
13450 if (ret)
13451 return ret;
13452 }
61333b60 13453
26495481
DV
13454 intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
13455 needs_modeset(crtc_state) ?
13456 "[modeset]" : "[fastset]");
c347a676
ACO
13457 }
13458
61333b60
ML
13459 if (any_ms) {
13460 ret = intel_modeset_checks(state);
13461
13462 if (ret)
13463 return ret;
27c329ed 13464 } else
dd8b3bdb 13465 intel_state->cdclk = dev_priv->cdclk_freq;
76305b1a 13466
dd8b3bdb 13467 ret = drm_atomic_helper_check_planes(dev, state);
aa363136
MR
13468 if (ret)
13469 return ret;
13470
f51be2e0 13471 intel_fbc_choose_crtc(dev_priv, state);
aa363136
MR
13472 calc_watermark_data(state);
13473
13474 return 0;
054518dd
ACO
13475}
13476
5008e874
ML
13477static int intel_atomic_prepare_commit(struct drm_device *dev,
13478 struct drm_atomic_state *state,
13479 bool async)
13480{
7580d774
ML
13481 struct drm_i915_private *dev_priv = dev->dev_private;
13482 struct drm_plane_state *plane_state;
5008e874 13483 struct drm_crtc_state *crtc_state;
7580d774 13484 struct drm_plane *plane;
5008e874
ML
13485 struct drm_crtc *crtc;
13486 int i, ret;
13487
13488 if (async) {
13489 DRM_DEBUG_KMS("i915 does not yet support async commit\n");
13490 return -EINVAL;
13491 }
13492
13493 for_each_crtc_in_state(state, crtc, crtc_state, i) {
13494 ret = intel_crtc_wait_for_pending_flips(crtc);
13495 if (ret)
13496 return ret;
7580d774
ML
13497
13498 if (atomic_read(&to_intel_crtc(crtc)->unpin_work_count) >= 2)
13499 flush_workqueue(dev_priv->wq);
5008e874
ML
13500 }
13501
f935675f
ML
13502 ret = mutex_lock_interruptible(&dev->struct_mutex);
13503 if (ret)
13504 return ret;
13505
5008e874 13506 ret = drm_atomic_helper_prepare_planes(dev, state);
7580d774
ML
13507 if (!ret && !async && !i915_reset_in_progress(&dev_priv->gpu_error)) {
13508 u32 reset_counter;
13509
13510 reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
13511 mutex_unlock(&dev->struct_mutex);
13512
13513 for_each_plane_in_state(state, plane, plane_state, i) {
13514 struct intel_plane_state *intel_plane_state =
13515 to_intel_plane_state(plane_state);
13516
13517 if (!intel_plane_state->wait_req)
13518 continue;
13519
13520 ret = __i915_wait_request(intel_plane_state->wait_req,
13521 reset_counter, true,
13522 NULL, NULL);
13523
13524 /* Swallow -EIO errors to allow updates during hw lockup. */
13525 if (ret == -EIO)
13526 ret = 0;
13527
13528 if (ret)
13529 break;
13530 }
13531
13532 if (!ret)
13533 return 0;
13534
13535 mutex_lock(&dev->struct_mutex);
13536 drm_atomic_helper_cleanup_planes(dev, state);
13537 }
5008e874 13538
f935675f 13539 mutex_unlock(&dev->struct_mutex);
5008e874
ML
13540 return ret;
13541}
13542
e8861675
ML
13543static void intel_atomic_wait_for_vblanks(struct drm_device *dev,
13544 struct drm_i915_private *dev_priv,
13545 unsigned crtc_mask)
13546{
13547 unsigned last_vblank_count[I915_MAX_PIPES];
13548 enum pipe pipe;
13549 int ret;
13550
13551 if (!crtc_mask)
13552 return;
13553
13554 for_each_pipe(dev_priv, pipe) {
13555 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
13556
13557 if (!((1 << pipe) & crtc_mask))
13558 continue;
13559
13560 ret = drm_crtc_vblank_get(crtc);
13561 if (WARN_ON(ret != 0)) {
13562 crtc_mask &= ~(1 << pipe);
13563 continue;
13564 }
13565
13566 last_vblank_count[pipe] = drm_crtc_vblank_count(crtc);
13567 }
13568
13569 for_each_pipe(dev_priv, pipe) {
13570 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
13571 long lret;
13572
13573 if (!((1 << pipe) & crtc_mask))
13574 continue;
13575
13576 lret = wait_event_timeout(dev->vblank[pipe].queue,
13577 last_vblank_count[pipe] !=
13578 drm_crtc_vblank_count(crtc),
13579 msecs_to_jiffies(50));
13580
13581 WARN_ON(!lret);
13582
13583 drm_crtc_vblank_put(crtc);
13584 }
13585}
13586
13587static bool needs_vblank_wait(struct intel_crtc_state *crtc_state)
13588{
13589 /* fb updated, need to unpin old fb */
13590 if (crtc_state->fb_changed)
13591 return true;
13592
13593 /* wm changes, need vblank before final wm's */
caed361d 13594 if (crtc_state->update_wm_post)
e8861675
ML
13595 return true;
13596
13597 /*
13598 * cxsr is re-enabled after vblank.
caed361d 13599 * This is already handled by crtc_state->update_wm_post,
e8861675
ML
13600 * but added for clarity.
13601 */
13602 if (crtc_state->disable_cxsr)
13603 return true;
13604
13605 return false;
13606}
13607
74c090b1
ML
13608/**
13609 * intel_atomic_commit - commit validated state object
13610 * @dev: DRM device
13611 * @state: the top-level driver state object
13612 * @async: asynchronous commit
13613 *
13614 * This function commits a top-level state object that has been validated
13615 * with drm_atomic_helper_check().
13616 *
13617 * FIXME: Atomic modeset support for i915 is not yet complete. At the moment
13618 * we can only handle plane-related operations and do not yet support
13619 * asynchronous commit.
13620 *
13621 * RETURNS
13622 * Zero for success or -errno.
13623 */
13624static int intel_atomic_commit(struct drm_device *dev,
13625 struct drm_atomic_state *state,
13626 bool async)
a6778b3c 13627{
565602d7 13628 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
fbee40df 13629 struct drm_i915_private *dev_priv = dev->dev_private;
29ceb0e6 13630 struct drm_crtc_state *old_crtc_state;
7580d774 13631 struct drm_crtc *crtc;
ed4a6a7c 13632 struct intel_crtc_state *intel_cstate;
565602d7
ML
13633 int ret = 0, i;
13634 bool hw_check = intel_state->modeset;
33c8df89 13635 unsigned long put_domains[I915_MAX_PIPES] = {};
e8861675 13636 unsigned crtc_vblank_mask = 0;
a6778b3c 13637
5008e874 13638 ret = intel_atomic_prepare_commit(dev, state, async);
7580d774
ML
13639 if (ret) {
13640 DRM_DEBUG_ATOMIC("Preparing state failed with %i\n", ret);
d4afb8cc 13641 return ret;
7580d774 13642 }
d4afb8cc 13643
1c5e19f8 13644 drm_atomic_helper_swap_state(dev, state);
a1475e77
ML
13645 dev_priv->wm.config = intel_state->wm_config;
13646 intel_shared_dpll_commit(state);
1c5e19f8 13647
565602d7
ML
13648 if (intel_state->modeset) {
13649 memcpy(dev_priv->min_pixclk, intel_state->min_pixclk,
13650 sizeof(intel_state->min_pixclk));
13651 dev_priv->active_crtcs = intel_state->active_crtcs;
1a617b77 13652 dev_priv->atomic_cdclk_freq = intel_state->cdclk;
33c8df89
ML
13653
13654 intel_display_power_get(dev_priv, POWER_DOMAIN_MODESET);
565602d7
ML
13655 }
13656
29ceb0e6 13657 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
a539205a
ML
13658 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13659
33c8df89
ML
13660 if (needs_modeset(crtc->state) ||
13661 to_intel_crtc_state(crtc->state)->update_pipe) {
13662 hw_check = true;
13663
13664 put_domains[to_intel_crtc(crtc)->pipe] =
13665 modeset_get_crtc_power_domains(crtc,
13666 to_intel_crtc_state(crtc->state));
13667 }
13668
61333b60
ML
13669 if (!needs_modeset(crtc->state))
13670 continue;
13671
29ceb0e6 13672 intel_pre_plane_update(to_intel_crtc_state(old_crtc_state));
460da916 13673
29ceb0e6
VS
13674 if (old_crtc_state->active) {
13675 intel_crtc_disable_planes(crtc, old_crtc_state->plane_mask);
a539205a 13676 dev_priv->display.crtc_disable(crtc);
eddfcbcd 13677 intel_crtc->active = false;
58f9c0bc 13678 intel_fbc_disable(intel_crtc);
eddfcbcd 13679 intel_disable_shared_dpll(intel_crtc);
9bbc8258
VS
13680
13681 /*
13682 * Underruns don't always raise
13683 * interrupts, so check manually.
13684 */
13685 intel_check_cpu_fifo_underruns(dev_priv);
13686 intel_check_pch_fifo_underruns(dev_priv);
b9001114
ML
13687
13688 if (!crtc->state->active)
13689 intel_update_watermarks(crtc);
a539205a 13690 }
b8cecdf5 13691 }
7758a113 13692
ea9d758d
DV
13693 /* Only after disabling all output pipelines that will be changed can we
13694 * update the the output configuration. */
4740b0f2 13695 intel_modeset_update_crtc_state(state);
f6e5b160 13696
565602d7 13697 if (intel_state->modeset) {
4740b0f2 13698 drm_atomic_helper_update_legacy_modeset_state(state->dev, state);
33c8df89
ML
13699
13700 if (dev_priv->display.modeset_commit_cdclk &&
13701 intel_state->dev_cdclk != dev_priv->cdclk_freq)
13702 dev_priv->display.modeset_commit_cdclk(state);
4740b0f2 13703 }
47fab737 13704
a6778b3c 13705 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
29ceb0e6 13706 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
f6ac4b2a
ML
13707 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13708 bool modeset = needs_modeset(crtc->state);
e8861675
ML
13709 struct intel_crtc_state *pipe_config =
13710 to_intel_crtc_state(crtc->state);
13711 bool update_pipe = !modeset && pipe_config->update_pipe;
9f836f90 13712
f6ac4b2a 13713 if (modeset && crtc->state->active) {
a539205a
ML
13714 update_scanline_offset(to_intel_crtc(crtc));
13715 dev_priv->display.crtc_enable(crtc);
13716 }
80715b2f 13717
f6ac4b2a 13718 if (!modeset)
29ceb0e6 13719 intel_pre_plane_update(to_intel_crtc_state(old_crtc_state));
f6ac4b2a 13720
31ae71fc
ML
13721 if (crtc->state->active &&
13722 drm_atomic_get_existing_plane_state(state, crtc->primary))
49227c4a
PZ
13723 intel_fbc_enable(intel_crtc);
13724
6173ee28
ML
13725 if (crtc->state->active &&
13726 (crtc->state->planes_changed || update_pipe))
29ceb0e6 13727 drm_atomic_helper_commit_planes_on_crtc(old_crtc_state);
bfd16b2a 13728
e8861675
ML
13729 if (pipe_config->base.active && needs_vblank_wait(pipe_config))
13730 crtc_vblank_mask |= 1 << i;
80715b2f 13731 }
a6778b3c 13732
a6778b3c 13733 /* FIXME: add subpixel order */
83a57153 13734
e8861675
ML
13735 if (!state->legacy_cursor_update)
13736 intel_atomic_wait_for_vblanks(dev, dev_priv, crtc_vblank_mask);
f935675f 13737
29ceb0e6 13738 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
cd202f69 13739 intel_post_plane_update(to_intel_crtc_state(old_crtc_state));
e8861675 13740
33c8df89
ML
13741 if (put_domains[i])
13742 modeset_put_power_domains(dev_priv, put_domains[i]);
13743 }
13744
13745 if (intel_state->modeset)
13746 intel_display_power_put(dev_priv, POWER_DOMAIN_MODESET);
13747
ed4a6a7c
MR
13748 /*
13749 * Now that the vblank has passed, we can go ahead and program the
13750 * optimal watermarks on platforms that need two-step watermark
13751 * programming.
13752 *
13753 * TODO: Move this (and other cleanup) to an async worker eventually.
13754 */
29ceb0e6 13755 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
ed4a6a7c
MR
13756 intel_cstate = to_intel_crtc_state(crtc->state);
13757
13758 if (dev_priv->display.optimize_watermarks)
13759 dev_priv->display.optimize_watermarks(intel_cstate);
13760 }
13761
f935675f 13762 mutex_lock(&dev->struct_mutex);
d4afb8cc 13763 drm_atomic_helper_cleanup_planes(dev, state);
f935675f 13764 mutex_unlock(&dev->struct_mutex);
2bfb4627 13765
565602d7 13766 if (hw_check)
ee165b1a
ML
13767 intel_modeset_check_state(dev, state);
13768
13769 drm_atomic_state_free(state);
f30da187 13770
75714940
MK
13771 /* As one of the primary mmio accessors, KMS has a high likelihood
13772 * of triggering bugs in unclaimed access. After we finish
13773 * modesetting, see if an error has been flagged, and if so
13774 * enable debugging for the next modeset - and hope we catch
13775 * the culprit.
13776 *
13777 * XXX note that we assume display power is on at this point.
13778 * This might hold true now but we need to add pm helper to check
13779 * unclaimed only when the hardware is on, as atomic commits
13780 * can happen also when the device is completely off.
13781 */
13782 intel_uncore_arm_unclaimed_mmio_detection(dev_priv);
13783
74c090b1 13784 return 0;
7f27126e
JB
13785}
13786
c0c36b94
CW
13787void intel_crtc_restore_mode(struct drm_crtc *crtc)
13788{
83a57153
ACO
13789 struct drm_device *dev = crtc->dev;
13790 struct drm_atomic_state *state;
e694eb02 13791 struct drm_crtc_state *crtc_state;
2bfb4627 13792 int ret;
83a57153
ACO
13793
13794 state = drm_atomic_state_alloc(dev);
13795 if (!state) {
e694eb02 13796 DRM_DEBUG_KMS("[CRTC:%d] crtc restore failed, out of memory",
83a57153
ACO
13797 crtc->base.id);
13798 return;
13799 }
13800
e694eb02 13801 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
83a57153 13802
e694eb02
ML
13803retry:
13804 crtc_state = drm_atomic_get_crtc_state(state, crtc);
13805 ret = PTR_ERR_OR_ZERO(crtc_state);
13806 if (!ret) {
13807 if (!crtc_state->active)
13808 goto out;
83a57153 13809
e694eb02 13810 crtc_state->mode_changed = true;
74c090b1 13811 ret = drm_atomic_commit(state);
83a57153
ACO
13812 }
13813
e694eb02
ML
13814 if (ret == -EDEADLK) {
13815 drm_atomic_state_clear(state);
13816 drm_modeset_backoff(state->acquire_ctx);
13817 goto retry;
4ed9fb37 13818 }
4be07317 13819
2bfb4627 13820 if (ret)
e694eb02 13821out:
2bfb4627 13822 drm_atomic_state_free(state);
c0c36b94
CW
13823}
13824
25c5b266
DV
13825#undef for_each_intel_crtc_masked
13826
f6e5b160 13827static const struct drm_crtc_funcs intel_crtc_funcs = {
f6e5b160 13828 .gamma_set = intel_crtc_gamma_set,
74c090b1 13829 .set_config = drm_atomic_helper_set_config,
f6e5b160
CW
13830 .destroy = intel_crtc_destroy,
13831 .page_flip = intel_crtc_page_flip,
1356837e
MR
13832 .atomic_duplicate_state = intel_crtc_duplicate_state,
13833 .atomic_destroy_state = intel_crtc_destroy_state,
f6e5b160
CW
13834};
13835
6beb8c23
MR
13836/**
13837 * intel_prepare_plane_fb - Prepare fb for usage on plane
13838 * @plane: drm plane to prepare for
13839 * @fb: framebuffer to prepare for presentation
13840 *
13841 * Prepares a framebuffer for usage on a display plane. Generally this
13842 * involves pinning the underlying object and updating the frontbuffer tracking
13843 * bits. Some older platforms need special physical address handling for
13844 * cursor planes.
13845 *
f935675f
ML
13846 * Must be called with struct_mutex held.
13847 *
6beb8c23
MR
13848 * Returns 0 on success, negative error code on failure.
13849 */
13850int
13851intel_prepare_plane_fb(struct drm_plane *plane,
d136dfee 13852 const struct drm_plane_state *new_state)
465c120c
MR
13853{
13854 struct drm_device *dev = plane->dev;
844f9111 13855 struct drm_framebuffer *fb = new_state->fb;
6beb8c23 13856 struct intel_plane *intel_plane = to_intel_plane(plane);
6beb8c23 13857 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
1ee49399 13858 struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->state->fb);
6beb8c23 13859 int ret = 0;
465c120c 13860
1ee49399 13861 if (!obj && !old_obj)
465c120c
MR
13862 return 0;
13863
5008e874
ML
13864 if (old_obj) {
13865 struct drm_crtc_state *crtc_state =
13866 drm_atomic_get_existing_crtc_state(new_state->state, plane->state->crtc);
13867
13868 /* Big Hammer, we also need to ensure that any pending
13869 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
13870 * current scanout is retired before unpinning the old
13871 * framebuffer. Note that we rely on userspace rendering
13872 * into the buffer attached to the pipe they are waiting
13873 * on. If not, userspace generates a GPU hang with IPEHR
13874 * point to the MI_WAIT_FOR_EVENT.
13875 *
13876 * This should only fail upon a hung GPU, in which case we
13877 * can safely continue.
13878 */
13879 if (needs_modeset(crtc_state))
13880 ret = i915_gem_object_wait_rendering(old_obj, true);
13881
13882 /* Swallow -EIO errors to allow updates during hw lockup. */
13883 if (ret && ret != -EIO)
f935675f 13884 return ret;
5008e874
ML
13885 }
13886
3c28ff22
AG
13887 /* For framebuffer backed by dmabuf, wait for fence */
13888 if (obj && obj->base.dma_buf) {
bcf8be27
ML
13889 long lret;
13890
13891 lret = reservation_object_wait_timeout_rcu(obj->base.dma_buf->resv,
13892 false, true,
13893 MAX_SCHEDULE_TIMEOUT);
13894 if (lret == -ERESTARTSYS)
13895 return lret;
3c28ff22 13896
bcf8be27 13897 WARN(lret < 0, "waiting returns %li\n", lret);
3c28ff22
AG
13898 }
13899
1ee49399
ML
13900 if (!obj) {
13901 ret = 0;
13902 } else if (plane->type == DRM_PLANE_TYPE_CURSOR &&
6beb8c23
MR
13903 INTEL_INFO(dev)->cursor_needs_physical) {
13904 int align = IS_I830(dev) ? 16 * 1024 : 256;
13905 ret = i915_gem_object_attach_phys(obj, align);
13906 if (ret)
13907 DRM_DEBUG_KMS("failed to attach phys object\n");
13908 } else {
3465c580 13909 ret = intel_pin_and_fence_fb_obj(fb, new_state->rotation);
6beb8c23 13910 }
465c120c 13911
7580d774
ML
13912 if (ret == 0) {
13913 if (obj) {
13914 struct intel_plane_state *plane_state =
13915 to_intel_plane_state(new_state);
13916
13917 i915_gem_request_assign(&plane_state->wait_req,
13918 obj->last_write_req);
13919 }
13920
a9ff8714 13921 i915_gem_track_fb(old_obj, obj, intel_plane->frontbuffer_bit);
7580d774 13922 }
fdd508a6 13923
6beb8c23
MR
13924 return ret;
13925}
13926
38f3ce3a
MR
13927/**
13928 * intel_cleanup_plane_fb - Cleans up an fb after plane use
13929 * @plane: drm plane to clean up for
13930 * @fb: old framebuffer that was on plane
13931 *
13932 * Cleans up a framebuffer that has just been removed from a plane.
f935675f
ML
13933 *
13934 * Must be called with struct_mutex held.
38f3ce3a
MR
13935 */
13936void
13937intel_cleanup_plane_fb(struct drm_plane *plane,
d136dfee 13938 const struct drm_plane_state *old_state)
38f3ce3a
MR
13939{
13940 struct drm_device *dev = plane->dev;
1ee49399 13941 struct intel_plane *intel_plane = to_intel_plane(plane);
7580d774 13942 struct intel_plane_state *old_intel_state;
1ee49399
ML
13943 struct drm_i915_gem_object *old_obj = intel_fb_obj(old_state->fb);
13944 struct drm_i915_gem_object *obj = intel_fb_obj(plane->state->fb);
38f3ce3a 13945
7580d774
ML
13946 old_intel_state = to_intel_plane_state(old_state);
13947
1ee49399 13948 if (!obj && !old_obj)
38f3ce3a
MR
13949 return;
13950
1ee49399
ML
13951 if (old_obj && (plane->type != DRM_PLANE_TYPE_CURSOR ||
13952 !INTEL_INFO(dev)->cursor_needs_physical))
3465c580 13953 intel_unpin_fb_obj(old_state->fb, old_state->rotation);
1ee49399
ML
13954
13955 /* prepare_fb aborted? */
13956 if ((old_obj && (old_obj->frontbuffer_bits & intel_plane->frontbuffer_bit)) ||
13957 (obj && !(obj->frontbuffer_bits & intel_plane->frontbuffer_bit)))
13958 i915_gem_track_fb(old_obj, obj, intel_plane->frontbuffer_bit);
7580d774
ML
13959
13960 i915_gem_request_assign(&old_intel_state->wait_req, NULL);
465c120c
MR
13961}
13962
6156a456
CK
13963int
13964skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
13965{
13966 int max_scale;
13967 struct drm_device *dev;
13968 struct drm_i915_private *dev_priv;
13969 int crtc_clock, cdclk;
13970
bf8a0af0 13971 if (!intel_crtc || !crtc_state->base.enable)
6156a456
CK
13972 return DRM_PLANE_HELPER_NO_SCALING;
13973
13974 dev = intel_crtc->base.dev;
13975 dev_priv = dev->dev_private;
13976 crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
27c329ed 13977 cdclk = to_intel_atomic_state(crtc_state->base.state)->cdclk;
6156a456 13978
54bf1ce6 13979 if (WARN_ON_ONCE(!crtc_clock || cdclk < crtc_clock))
6156a456
CK
13980 return DRM_PLANE_HELPER_NO_SCALING;
13981
13982 /*
13983 * skl max scale is lower of:
13984 * close to 3 but not 3, -1 is for that purpose
13985 * or
13986 * cdclk/crtc_clock
13987 */
13988 max_scale = min((1 << 16) * 3 - 1, (1 << 8) * ((cdclk << 8) / crtc_clock));
13989
13990 return max_scale;
13991}
13992
465c120c 13993static int
3c692a41 13994intel_check_primary_plane(struct drm_plane *plane,
061e4b8d 13995 struct intel_crtc_state *crtc_state,
3c692a41
GP
13996 struct intel_plane_state *state)
13997{
2b875c22
MR
13998 struct drm_crtc *crtc = state->base.crtc;
13999 struct drm_framebuffer *fb = state->base.fb;
6156a456 14000 int min_scale = DRM_PLANE_HELPER_NO_SCALING;
061e4b8d
ML
14001 int max_scale = DRM_PLANE_HELPER_NO_SCALING;
14002 bool can_position = false;
465c120c 14003
693bdc28
VS
14004 if (INTEL_INFO(plane->dev)->gen >= 9) {
14005 /* use scaler when colorkey is not required */
14006 if (state->ckey.flags == I915_SET_COLORKEY_NONE) {
14007 min_scale = 1;
14008 max_scale = skl_max_scale(to_intel_crtc(crtc), crtc_state);
14009 }
d8106366 14010 can_position = true;
6156a456 14011 }
d8106366 14012
061e4b8d
ML
14013 return drm_plane_helper_check_update(plane, crtc, fb, &state->src,
14014 &state->dst, &state->clip,
da20eabd
ML
14015 min_scale, max_scale,
14016 can_position, true,
14017 &state->visible);
14af293f
GP
14018}
14019
613d2b27
ML
14020static void intel_begin_crtc_commit(struct drm_crtc *crtc,
14021 struct drm_crtc_state *old_crtc_state)
3c692a41 14022{
32b7eeec 14023 struct drm_device *dev = crtc->dev;
3c692a41 14024 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
bfd16b2a
ML
14025 struct intel_crtc_state *old_intel_state =
14026 to_intel_crtc_state(old_crtc_state);
14027 bool modeset = needs_modeset(crtc->state);
3c692a41 14028
c34c9ee4 14029 /* Perform vblank evasion around commit operation */
62852622 14030 intel_pipe_update_start(intel_crtc);
0583236e 14031
bfd16b2a
ML
14032 if (modeset)
14033 return;
14034
14035 if (to_intel_crtc_state(crtc->state)->update_pipe)
14036 intel_update_pipe_config(intel_crtc, old_intel_state);
14037 else if (INTEL_INFO(dev)->gen >= 9)
0583236e 14038 skl_detach_scalers(intel_crtc);
32b7eeec
MR
14039}
14040
613d2b27
ML
14041static void intel_finish_crtc_commit(struct drm_crtc *crtc,
14042 struct drm_crtc_state *old_crtc_state)
32b7eeec 14043{
32b7eeec 14044 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
32b7eeec 14045
62852622 14046 intel_pipe_update_end(intel_crtc);
3c692a41
GP
14047}
14048
cf4c7c12 14049/**
4a3b8769
MR
14050 * intel_plane_destroy - destroy a plane
14051 * @plane: plane to destroy
cf4c7c12 14052 *
4a3b8769
MR
14053 * Common destruction function for all types of planes (primary, cursor,
14054 * sprite).
cf4c7c12 14055 */
4a3b8769 14056void intel_plane_destroy(struct drm_plane *plane)
465c120c
MR
14057{
14058 struct intel_plane *intel_plane = to_intel_plane(plane);
14059 drm_plane_cleanup(plane);
14060 kfree(intel_plane);
14061}
14062
65a3fea0 14063const struct drm_plane_funcs intel_plane_funcs = {
70a101f8
MR
14064 .update_plane = drm_atomic_helper_update_plane,
14065 .disable_plane = drm_atomic_helper_disable_plane,
3d7d6510 14066 .destroy = intel_plane_destroy,
c196e1d6 14067 .set_property = drm_atomic_helper_plane_set_property,
a98b3431
MR
14068 .atomic_get_property = intel_plane_atomic_get_property,
14069 .atomic_set_property = intel_plane_atomic_set_property,
ea2c67bb
MR
14070 .atomic_duplicate_state = intel_plane_duplicate_state,
14071 .atomic_destroy_state = intel_plane_destroy_state,
14072
465c120c
MR
14073};
14074
14075static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
14076 int pipe)
14077{
14078 struct intel_plane *primary;
8e7d688b 14079 struct intel_plane_state *state;
465c120c 14080 const uint32_t *intel_primary_formats;
45e3743a 14081 unsigned int num_formats;
465c120c
MR
14082
14083 primary = kzalloc(sizeof(*primary), GFP_KERNEL);
14084 if (primary == NULL)
14085 return NULL;
14086
8e7d688b
MR
14087 state = intel_create_plane_state(&primary->base);
14088 if (!state) {
ea2c67bb
MR
14089 kfree(primary);
14090 return NULL;
14091 }
8e7d688b 14092 primary->base.state = &state->base;
ea2c67bb 14093
465c120c
MR
14094 primary->can_scale = false;
14095 primary->max_downscale = 1;
6156a456
CK
14096 if (INTEL_INFO(dev)->gen >= 9) {
14097 primary->can_scale = true;
af99ceda 14098 state->scaler_id = -1;
6156a456 14099 }
465c120c
MR
14100 primary->pipe = pipe;
14101 primary->plane = pipe;
a9ff8714 14102 primary->frontbuffer_bit = INTEL_FRONTBUFFER_PRIMARY(pipe);
c59cb179 14103 primary->check_plane = intel_check_primary_plane;
465c120c
MR
14104 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
14105 primary->plane = !pipe;
14106
6c0fd451
DL
14107 if (INTEL_INFO(dev)->gen >= 9) {
14108 intel_primary_formats = skl_primary_formats;
14109 num_formats = ARRAY_SIZE(skl_primary_formats);
a8d201af
ML
14110
14111 primary->update_plane = skylake_update_primary_plane;
14112 primary->disable_plane = skylake_disable_primary_plane;
14113 } else if (HAS_PCH_SPLIT(dev)) {
14114 intel_primary_formats = i965_primary_formats;
14115 num_formats = ARRAY_SIZE(i965_primary_formats);
14116
14117 primary->update_plane = ironlake_update_primary_plane;
14118 primary->disable_plane = i9xx_disable_primary_plane;
6c0fd451 14119 } else if (INTEL_INFO(dev)->gen >= 4) {
568db4f2
DL
14120 intel_primary_formats = i965_primary_formats;
14121 num_formats = ARRAY_SIZE(i965_primary_formats);
a8d201af
ML
14122
14123 primary->update_plane = i9xx_update_primary_plane;
14124 primary->disable_plane = i9xx_disable_primary_plane;
6c0fd451
DL
14125 } else {
14126 intel_primary_formats = i8xx_primary_formats;
14127 num_formats = ARRAY_SIZE(i8xx_primary_formats);
a8d201af
ML
14128
14129 primary->update_plane = i9xx_update_primary_plane;
14130 primary->disable_plane = i9xx_disable_primary_plane;
465c120c
MR
14131 }
14132
14133 drm_universal_plane_init(dev, &primary->base, 0,
65a3fea0 14134 &intel_plane_funcs,
465c120c 14135 intel_primary_formats, num_formats,
b0b3b795 14136 DRM_PLANE_TYPE_PRIMARY, NULL);
48404c1e 14137
3b7a5119
SJ
14138 if (INTEL_INFO(dev)->gen >= 4)
14139 intel_create_rotation_property(dev, primary);
48404c1e 14140
ea2c67bb
MR
14141 drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
14142
465c120c
MR
14143 return &primary->base;
14144}
14145
3b7a5119
SJ
14146void intel_create_rotation_property(struct drm_device *dev, struct intel_plane *plane)
14147{
14148 if (!dev->mode_config.rotation_property) {
14149 unsigned long flags = BIT(DRM_ROTATE_0) |
14150 BIT(DRM_ROTATE_180);
14151
14152 if (INTEL_INFO(dev)->gen >= 9)
14153 flags |= BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_270);
14154
14155 dev->mode_config.rotation_property =
14156 drm_mode_create_rotation_property(dev, flags);
14157 }
14158 if (dev->mode_config.rotation_property)
14159 drm_object_attach_property(&plane->base.base,
14160 dev->mode_config.rotation_property,
14161 plane->base.state->rotation);
14162}
14163
3d7d6510 14164static int
852e787c 14165intel_check_cursor_plane(struct drm_plane *plane,
061e4b8d 14166 struct intel_crtc_state *crtc_state,
852e787c 14167 struct intel_plane_state *state)
3d7d6510 14168{
061e4b8d 14169 struct drm_crtc *crtc = crtc_state->base.crtc;
2b875c22 14170 struct drm_framebuffer *fb = state->base.fb;
757f9a3e 14171 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
b29ec92c 14172 enum pipe pipe = to_intel_plane(plane)->pipe;
757f9a3e
GP
14173 unsigned stride;
14174 int ret;
3d7d6510 14175
061e4b8d
ML
14176 ret = drm_plane_helper_check_update(plane, crtc, fb, &state->src,
14177 &state->dst, &state->clip,
3d7d6510
MR
14178 DRM_PLANE_HELPER_NO_SCALING,
14179 DRM_PLANE_HELPER_NO_SCALING,
852e787c 14180 true, true, &state->visible);
757f9a3e
GP
14181 if (ret)
14182 return ret;
14183
757f9a3e
GP
14184 /* if we want to turn off the cursor ignore width and height */
14185 if (!obj)
da20eabd 14186 return 0;
757f9a3e 14187
757f9a3e 14188 /* Check for which cursor types we support */
061e4b8d 14189 if (!cursor_size_ok(plane->dev, state->base.crtc_w, state->base.crtc_h)) {
ea2c67bb
MR
14190 DRM_DEBUG("Cursor dimension %dx%d not supported\n",
14191 state->base.crtc_w, state->base.crtc_h);
757f9a3e
GP
14192 return -EINVAL;
14193 }
14194
ea2c67bb
MR
14195 stride = roundup_pow_of_two(state->base.crtc_w) * 4;
14196 if (obj->base.size < stride * state->base.crtc_h) {
757f9a3e
GP
14197 DRM_DEBUG_KMS("buffer is too small\n");
14198 return -ENOMEM;
14199 }
14200
3a656b54 14201 if (fb->modifier[0] != DRM_FORMAT_MOD_NONE) {
757f9a3e 14202 DRM_DEBUG_KMS("cursor cannot be tiled\n");
da20eabd 14203 return -EINVAL;
32b7eeec
MR
14204 }
14205
b29ec92c
VS
14206 /*
14207 * There's something wrong with the cursor on CHV pipe C.
14208 * If it straddles the left edge of the screen then
14209 * moving it away from the edge or disabling it often
14210 * results in a pipe underrun, and often that can lead to
14211 * dead pipe (constant underrun reported, and it scans
14212 * out just a solid color). To recover from that, the
14213 * display power well must be turned off and on again.
14214 * Refuse the put the cursor into that compromised position.
14215 */
14216 if (IS_CHERRYVIEW(plane->dev) && pipe == PIPE_C &&
14217 state->visible && state->base.crtc_x < 0) {
14218 DRM_DEBUG_KMS("CHV cursor C not allowed to straddle the left screen edge\n");
14219 return -EINVAL;
14220 }
14221
da20eabd 14222 return 0;
852e787c 14223}
3d7d6510 14224
a8ad0d8e
ML
14225static void
14226intel_disable_cursor_plane(struct drm_plane *plane,
7fabf5ef 14227 struct drm_crtc *crtc)
a8ad0d8e 14228{
f2858021
ML
14229 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
14230
14231 intel_crtc->cursor_addr = 0;
55a08b3f 14232 intel_crtc_update_cursor(crtc, NULL);
a8ad0d8e
ML
14233}
14234
f4a2cf29 14235static void
55a08b3f
ML
14236intel_update_cursor_plane(struct drm_plane *plane,
14237 const struct intel_crtc_state *crtc_state,
14238 const struct intel_plane_state *state)
852e787c 14239{
55a08b3f
ML
14240 struct drm_crtc *crtc = crtc_state->base.crtc;
14241 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ea2c67bb 14242 struct drm_device *dev = plane->dev;
2b875c22 14243 struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
a912f12f 14244 uint32_t addr;
852e787c 14245
f4a2cf29 14246 if (!obj)
a912f12f 14247 addr = 0;
f4a2cf29 14248 else if (!INTEL_INFO(dev)->cursor_needs_physical)
a912f12f 14249 addr = i915_gem_obj_ggtt_offset(obj);
f4a2cf29 14250 else
a912f12f 14251 addr = obj->phys_handle->busaddr;
852e787c 14252
a912f12f 14253 intel_crtc->cursor_addr = addr;
55a08b3f 14254 intel_crtc_update_cursor(crtc, state);
852e787c
GP
14255}
14256
3d7d6510
MR
14257static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
14258 int pipe)
14259{
14260 struct intel_plane *cursor;
8e7d688b 14261 struct intel_plane_state *state;
3d7d6510
MR
14262
14263 cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
14264 if (cursor == NULL)
14265 return NULL;
14266
8e7d688b
MR
14267 state = intel_create_plane_state(&cursor->base);
14268 if (!state) {
ea2c67bb
MR
14269 kfree(cursor);
14270 return NULL;
14271 }
8e7d688b 14272 cursor->base.state = &state->base;
ea2c67bb 14273
3d7d6510
MR
14274 cursor->can_scale = false;
14275 cursor->max_downscale = 1;
14276 cursor->pipe = pipe;
14277 cursor->plane = pipe;
a9ff8714 14278 cursor->frontbuffer_bit = INTEL_FRONTBUFFER_CURSOR(pipe);
c59cb179 14279 cursor->check_plane = intel_check_cursor_plane;
55a08b3f 14280 cursor->update_plane = intel_update_cursor_plane;
a8ad0d8e 14281 cursor->disable_plane = intel_disable_cursor_plane;
3d7d6510
MR
14282
14283 drm_universal_plane_init(dev, &cursor->base, 0,
65a3fea0 14284 &intel_plane_funcs,
3d7d6510
MR
14285 intel_cursor_formats,
14286 ARRAY_SIZE(intel_cursor_formats),
b0b3b795 14287 DRM_PLANE_TYPE_CURSOR, NULL);
4398ad45
VS
14288
14289 if (INTEL_INFO(dev)->gen >= 4) {
14290 if (!dev->mode_config.rotation_property)
14291 dev->mode_config.rotation_property =
14292 drm_mode_create_rotation_property(dev,
14293 BIT(DRM_ROTATE_0) |
14294 BIT(DRM_ROTATE_180));
14295 if (dev->mode_config.rotation_property)
14296 drm_object_attach_property(&cursor->base.base,
14297 dev->mode_config.rotation_property,
8e7d688b 14298 state->base.rotation);
4398ad45
VS
14299 }
14300
af99ceda
CK
14301 if (INTEL_INFO(dev)->gen >=9)
14302 state->scaler_id = -1;
14303
ea2c67bb
MR
14304 drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
14305
3d7d6510
MR
14306 return &cursor->base;
14307}
14308
549e2bfb
CK
14309static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
14310 struct intel_crtc_state *crtc_state)
14311{
14312 int i;
14313 struct intel_scaler *intel_scaler;
14314 struct intel_crtc_scaler_state *scaler_state = &crtc_state->scaler_state;
14315
14316 for (i = 0; i < intel_crtc->num_scalers; i++) {
14317 intel_scaler = &scaler_state->scalers[i];
14318 intel_scaler->in_use = 0;
549e2bfb
CK
14319 intel_scaler->mode = PS_SCALER_MODE_DYN;
14320 }
14321
14322 scaler_state->scaler_id = -1;
14323}
14324
b358d0a6 14325static void intel_crtc_init(struct drm_device *dev, int pipe)
79e53945 14326{
fbee40df 14327 struct drm_i915_private *dev_priv = dev->dev_private;
79e53945 14328 struct intel_crtc *intel_crtc;
f5de6e07 14329 struct intel_crtc_state *crtc_state = NULL;
3d7d6510
MR
14330 struct drm_plane *primary = NULL;
14331 struct drm_plane *cursor = NULL;
465c120c 14332 int i, ret;
79e53945 14333
955382f3 14334 intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
79e53945
JB
14335 if (intel_crtc == NULL)
14336 return;
14337
f5de6e07
ACO
14338 crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
14339 if (!crtc_state)
14340 goto fail;
550acefd
ACO
14341 intel_crtc->config = crtc_state;
14342 intel_crtc->base.state = &crtc_state->base;
07878248 14343 crtc_state->base.crtc = &intel_crtc->base;
f5de6e07 14344
549e2bfb
CK
14345 /* initialize shared scalers */
14346 if (INTEL_INFO(dev)->gen >= 9) {
14347 if (pipe == PIPE_C)
14348 intel_crtc->num_scalers = 1;
14349 else
14350 intel_crtc->num_scalers = SKL_NUM_SCALERS;
14351
14352 skl_init_scalers(dev, intel_crtc, crtc_state);
14353 }
14354
465c120c 14355 primary = intel_primary_plane_create(dev, pipe);
3d7d6510
MR
14356 if (!primary)
14357 goto fail;
14358
14359 cursor = intel_cursor_plane_create(dev, pipe);
14360 if (!cursor)
14361 goto fail;
14362
465c120c 14363 ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
f9882876 14364 cursor, &intel_crtc_funcs, NULL);
3d7d6510
MR
14365 if (ret)
14366 goto fail;
79e53945
JB
14367
14368 drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
79e53945
JB
14369 for (i = 0; i < 256; i++) {
14370 intel_crtc->lut_r[i] = i;
14371 intel_crtc->lut_g[i] = i;
14372 intel_crtc->lut_b[i] = i;
14373 }
14374
1f1c2e24
VS
14375 /*
14376 * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
8c0f92e1 14377 * is hooked to pipe B. Hence we want plane A feeding pipe B.
1f1c2e24 14378 */
80824003
JB
14379 intel_crtc->pipe = pipe;
14380 intel_crtc->plane = pipe;
3a77c4c4 14381 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
28c97730 14382 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
e2e767ab 14383 intel_crtc->plane = !pipe;
80824003
JB
14384 }
14385
4b0e333e
CW
14386 intel_crtc->cursor_base = ~0;
14387 intel_crtc->cursor_cntl = ~0;
dc41c154 14388 intel_crtc->cursor_size = ~0;
8d7849db 14389
852eb00d
VS
14390 intel_crtc->wm.cxsr_allowed = true;
14391
22fd0fab
JB
14392 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
14393 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
14394 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
14395 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
14396
79e53945 14397 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
87b6b101
DV
14398
14399 WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
3d7d6510
MR
14400 return;
14401
14402fail:
14403 if (primary)
14404 drm_plane_cleanup(primary);
14405 if (cursor)
14406 drm_plane_cleanup(cursor);
f5de6e07 14407 kfree(crtc_state);
3d7d6510 14408 kfree(intel_crtc);
79e53945
JB
14409}
14410
752aa88a
JB
14411enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
14412{
14413 struct drm_encoder *encoder = connector->base.encoder;
6e9f798d 14414 struct drm_device *dev = connector->base.dev;
752aa88a 14415
51fd371b 14416 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
752aa88a 14417
d3babd3f 14418 if (!encoder || WARN_ON(!encoder->crtc))
752aa88a
JB
14419 return INVALID_PIPE;
14420
14421 return to_intel_crtc(encoder->crtc)->pipe;
14422}
14423
08d7b3d1 14424int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
05394f39 14425 struct drm_file *file)
08d7b3d1 14426{
08d7b3d1 14427 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
7707e653 14428 struct drm_crtc *drmmode_crtc;
c05422d5 14429 struct intel_crtc *crtc;
08d7b3d1 14430
7707e653 14431 drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
08d7b3d1 14432
7707e653 14433 if (!drmmode_crtc) {
08d7b3d1 14434 DRM_ERROR("no such CRTC id\n");
3f2c2057 14435 return -ENOENT;
08d7b3d1
CW
14436 }
14437
7707e653 14438 crtc = to_intel_crtc(drmmode_crtc);
c05422d5 14439 pipe_from_crtc_id->pipe = crtc->pipe;
08d7b3d1 14440
c05422d5 14441 return 0;
08d7b3d1
CW
14442}
14443
66a9278e 14444static int intel_encoder_clones(struct intel_encoder *encoder)
79e53945 14445{
66a9278e
DV
14446 struct drm_device *dev = encoder->base.dev;
14447 struct intel_encoder *source_encoder;
79e53945 14448 int index_mask = 0;
79e53945
JB
14449 int entry = 0;
14450
b2784e15 14451 for_each_intel_encoder(dev, source_encoder) {
bc079e8b 14452 if (encoders_cloneable(encoder, source_encoder))
66a9278e
DV
14453 index_mask |= (1 << entry);
14454
79e53945
JB
14455 entry++;
14456 }
4ef69c7a 14457
79e53945
JB
14458 return index_mask;
14459}
14460
4d302442
CW
14461static bool has_edp_a(struct drm_device *dev)
14462{
14463 struct drm_i915_private *dev_priv = dev->dev_private;
14464
14465 if (!IS_MOBILE(dev))
14466 return false;
14467
14468 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
14469 return false;
14470
e3589908 14471 if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
4d302442
CW
14472 return false;
14473
14474 return true;
14475}
14476
84b4e042
JB
14477static bool intel_crt_present(struct drm_device *dev)
14478{
14479 struct drm_i915_private *dev_priv = dev->dev_private;
14480
884497ed
DL
14481 if (INTEL_INFO(dev)->gen >= 9)
14482 return false;
14483
cf404ce4 14484 if (IS_HSW_ULT(dev) || IS_BDW_ULT(dev))
84b4e042
JB
14485 return false;
14486
14487 if (IS_CHERRYVIEW(dev))
14488 return false;
14489
65e472e4
VS
14490 if (HAS_PCH_LPT_H(dev) && I915_READ(SFUSE_STRAP) & SFUSE_STRAP_CRT_DISABLED)
14491 return false;
14492
70ac54d0
VS
14493 /* DDI E can't be used if DDI A requires 4 lanes */
14494 if (HAS_DDI(dev) && I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_A_4_LANES)
14495 return false;
14496
e4abb733 14497 if (!dev_priv->vbt.int_crt_support)
84b4e042
JB
14498 return false;
14499
14500 return true;
14501}
14502
79e53945
JB
14503static void intel_setup_outputs(struct drm_device *dev)
14504{
725e30ad 14505 struct drm_i915_private *dev_priv = dev->dev_private;
4ef69c7a 14506 struct intel_encoder *encoder;
cb0953d7 14507 bool dpd_is_edp = false;
79e53945 14508
c9093354 14509 intel_lvds_init(dev);
79e53945 14510
84b4e042 14511 if (intel_crt_present(dev))
79935fca 14512 intel_crt_init(dev);
cb0953d7 14513
c776eb2e
VK
14514 if (IS_BROXTON(dev)) {
14515 /*
14516 * FIXME: Broxton doesn't support port detection via the
14517 * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
14518 * detect the ports.
14519 */
14520 intel_ddi_init(dev, PORT_A);
14521 intel_ddi_init(dev, PORT_B);
14522 intel_ddi_init(dev, PORT_C);
14523 } else if (HAS_DDI(dev)) {
0e72a5b5
ED
14524 int found;
14525
de31facd
JB
14526 /*
14527 * Haswell uses DDI functions to detect digital outputs.
14528 * On SKL pre-D0 the strap isn't connected, so we assume
14529 * it's there.
14530 */
77179400 14531 found = I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_INIT_DISPLAY_DETECTED;
de31facd 14532 /* WaIgnoreDDIAStrap: skl */
ef11bdb3 14533 if (found || IS_SKYLAKE(dev) || IS_KABYLAKE(dev))
0e72a5b5
ED
14534 intel_ddi_init(dev, PORT_A);
14535
14536 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
14537 * register */
14538 found = I915_READ(SFUSE_STRAP);
14539
14540 if (found & SFUSE_STRAP_DDIB_DETECTED)
14541 intel_ddi_init(dev, PORT_B);
14542 if (found & SFUSE_STRAP_DDIC_DETECTED)
14543 intel_ddi_init(dev, PORT_C);
14544 if (found & SFUSE_STRAP_DDID_DETECTED)
14545 intel_ddi_init(dev, PORT_D);
2800e4c2
RV
14546 /*
14547 * On SKL we don't have a way to detect DDI-E so we rely on VBT.
14548 */
ef11bdb3 14549 if ((IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) &&
2800e4c2
RV
14550 (dev_priv->vbt.ddi_port_info[PORT_E].supports_dp ||
14551 dev_priv->vbt.ddi_port_info[PORT_E].supports_dvi ||
14552 dev_priv->vbt.ddi_port_info[PORT_E].supports_hdmi))
14553 intel_ddi_init(dev, PORT_E);
14554
0e72a5b5 14555 } else if (HAS_PCH_SPLIT(dev)) {
cb0953d7 14556 int found;
5d8a7752 14557 dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
270b3042
DV
14558
14559 if (has_edp_a(dev))
14560 intel_dp_init(dev, DP_A, PORT_A);
cb0953d7 14561
dc0fa718 14562 if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
461ed3ca 14563 /* PCH SDVOB multiplex with HDMIB */
2a5c0832 14564 found = intel_sdvo_init(dev, PCH_SDVOB, PORT_B);
30ad48b7 14565 if (!found)
e2debe91 14566 intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
5eb08b69 14567 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
ab9d7c30 14568 intel_dp_init(dev, PCH_DP_B, PORT_B);
30ad48b7
ZW
14569 }
14570
dc0fa718 14571 if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
e2debe91 14572 intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
30ad48b7 14573
dc0fa718 14574 if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
e2debe91 14575 intel_hdmi_init(dev, PCH_HDMID, PORT_D);
30ad48b7 14576
5eb08b69 14577 if (I915_READ(PCH_DP_C) & DP_DETECTED)
ab9d7c30 14578 intel_dp_init(dev, PCH_DP_C, PORT_C);
5eb08b69 14579
270b3042 14580 if (I915_READ(PCH_DP_D) & DP_DETECTED)
ab9d7c30 14581 intel_dp_init(dev, PCH_DP_D, PORT_D);
666a4537 14582 } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
e17ac6db
VS
14583 /*
14584 * The DP_DETECTED bit is the latched state of the DDC
14585 * SDA pin at boot. However since eDP doesn't require DDC
14586 * (no way to plug in a DP->HDMI dongle) the DDC pins for
14587 * eDP ports may have been muxed to an alternate function.
14588 * Thus we can't rely on the DP_DETECTED bit alone to detect
14589 * eDP ports. Consult the VBT as well as DP_DETECTED to
14590 * detect eDP ports.
14591 */
e66eb81d 14592 if (I915_READ(VLV_HDMIB) & SDVO_DETECTED &&
d2182a66 14593 !intel_dp_is_edp(dev, PORT_B))
e66eb81d
VS
14594 intel_hdmi_init(dev, VLV_HDMIB, PORT_B);
14595 if (I915_READ(VLV_DP_B) & DP_DETECTED ||
e17ac6db 14596 intel_dp_is_edp(dev, PORT_B))
e66eb81d 14597 intel_dp_init(dev, VLV_DP_B, PORT_B);
585a94b8 14598
e66eb81d 14599 if (I915_READ(VLV_HDMIC) & SDVO_DETECTED &&
d2182a66 14600 !intel_dp_is_edp(dev, PORT_C))
e66eb81d
VS
14601 intel_hdmi_init(dev, VLV_HDMIC, PORT_C);
14602 if (I915_READ(VLV_DP_C) & DP_DETECTED ||
e17ac6db 14603 intel_dp_is_edp(dev, PORT_C))
e66eb81d 14604 intel_dp_init(dev, VLV_DP_C, PORT_C);
19c03924 14605
9418c1f1 14606 if (IS_CHERRYVIEW(dev)) {
e17ac6db 14607 /* eDP not supported on port D, so don't check VBT */
e66eb81d
VS
14608 if (I915_READ(CHV_HDMID) & SDVO_DETECTED)
14609 intel_hdmi_init(dev, CHV_HDMID, PORT_D);
14610 if (I915_READ(CHV_DP_D) & DP_DETECTED)
14611 intel_dp_init(dev, CHV_DP_D, PORT_D);
9418c1f1
VS
14612 }
14613
3cfca973 14614 intel_dsi_init(dev);
09da55dc 14615 } else if (!IS_GEN2(dev) && !IS_PINEVIEW(dev)) {
27185ae1 14616 bool found = false;
7d57382e 14617
e2debe91 14618 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
b01f2c3a 14619 DRM_DEBUG_KMS("probing SDVOB\n");
2a5c0832 14620 found = intel_sdvo_init(dev, GEN3_SDVOB, PORT_B);
3fec3d2f 14621 if (!found && IS_G4X(dev)) {
b01f2c3a 14622 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
e2debe91 14623 intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
b01f2c3a 14624 }
27185ae1 14625
3fec3d2f 14626 if (!found && IS_G4X(dev))
ab9d7c30 14627 intel_dp_init(dev, DP_B, PORT_B);
725e30ad 14628 }
13520b05
KH
14629
14630 /* Before G4X SDVOC doesn't have its own detect register */
13520b05 14631
e2debe91 14632 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
b01f2c3a 14633 DRM_DEBUG_KMS("probing SDVOC\n");
2a5c0832 14634 found = intel_sdvo_init(dev, GEN3_SDVOC, PORT_C);
b01f2c3a 14635 }
27185ae1 14636
e2debe91 14637 if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
27185ae1 14638
3fec3d2f 14639 if (IS_G4X(dev)) {
b01f2c3a 14640 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
e2debe91 14641 intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
b01f2c3a 14642 }
3fec3d2f 14643 if (IS_G4X(dev))
ab9d7c30 14644 intel_dp_init(dev, DP_C, PORT_C);
725e30ad 14645 }
27185ae1 14646
3fec3d2f 14647 if (IS_G4X(dev) &&
e7281eab 14648 (I915_READ(DP_D) & DP_DETECTED))
ab9d7c30 14649 intel_dp_init(dev, DP_D, PORT_D);
bad720ff 14650 } else if (IS_GEN2(dev))
79e53945
JB
14651 intel_dvo_init(dev);
14652
103a196f 14653 if (SUPPORTS_TV(dev))
79e53945
JB
14654 intel_tv_init(dev);
14655
0bc12bcb 14656 intel_psr_init(dev);
7c8f8a70 14657
b2784e15 14658 for_each_intel_encoder(dev, encoder) {
4ef69c7a
CW
14659 encoder->base.possible_crtcs = encoder->crtc_mask;
14660 encoder->base.possible_clones =
66a9278e 14661 intel_encoder_clones(encoder);
79e53945 14662 }
47356eb6 14663
dde86e2d 14664 intel_init_pch_refclk(dev);
270b3042
DV
14665
14666 drm_helper_move_panel_connectors_to_head(dev);
79e53945
JB
14667}
14668
14669static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
14670{
60a5ca01 14671 struct drm_device *dev = fb->dev;
79e53945 14672 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
79e53945 14673
ef2d633e 14674 drm_framebuffer_cleanup(fb);
60a5ca01 14675 mutex_lock(&dev->struct_mutex);
ef2d633e 14676 WARN_ON(!intel_fb->obj->framebuffer_references--);
60a5ca01
VS
14677 drm_gem_object_unreference(&intel_fb->obj->base);
14678 mutex_unlock(&dev->struct_mutex);
79e53945
JB
14679 kfree(intel_fb);
14680}
14681
14682static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
05394f39 14683 struct drm_file *file,
79e53945
JB
14684 unsigned int *handle)
14685{
14686 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
05394f39 14687 struct drm_i915_gem_object *obj = intel_fb->obj;
79e53945 14688
cc917ab4
CW
14689 if (obj->userptr.mm) {
14690 DRM_DEBUG("attempting to use a userptr for a framebuffer, denied\n");
14691 return -EINVAL;
14692 }
14693
05394f39 14694 return drm_gem_handle_create(file, &obj->base, handle);
79e53945
JB
14695}
14696
86c98588
RV
14697static int intel_user_framebuffer_dirty(struct drm_framebuffer *fb,
14698 struct drm_file *file,
14699 unsigned flags, unsigned color,
14700 struct drm_clip_rect *clips,
14701 unsigned num_clips)
14702{
14703 struct drm_device *dev = fb->dev;
14704 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
14705 struct drm_i915_gem_object *obj = intel_fb->obj;
14706
14707 mutex_lock(&dev->struct_mutex);
74b4ea1e 14708 intel_fb_obj_flush(obj, false, ORIGIN_DIRTYFB);
86c98588
RV
14709 mutex_unlock(&dev->struct_mutex);
14710
14711 return 0;
14712}
14713
79e53945
JB
14714static const struct drm_framebuffer_funcs intel_fb_funcs = {
14715 .destroy = intel_user_framebuffer_destroy,
14716 .create_handle = intel_user_framebuffer_create_handle,
86c98588 14717 .dirty = intel_user_framebuffer_dirty,
79e53945
JB
14718};
14719
b321803d
DL
14720static
14721u32 intel_fb_pitch_limit(struct drm_device *dev, uint64_t fb_modifier,
14722 uint32_t pixel_format)
14723{
14724 u32 gen = INTEL_INFO(dev)->gen;
14725
14726 if (gen >= 9) {
ac484963
VS
14727 int cpp = drm_format_plane_cpp(pixel_format, 0);
14728
b321803d
DL
14729 /* "The stride in bytes must not exceed the of the size of 8K
14730 * pixels and 32K bytes."
14731 */
ac484963 14732 return min(8192 * cpp, 32768);
666a4537 14733 } else if (gen >= 5 && !IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
b321803d
DL
14734 return 32*1024;
14735 } else if (gen >= 4) {
14736 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14737 return 16*1024;
14738 else
14739 return 32*1024;
14740 } else if (gen >= 3) {
14741 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14742 return 8*1024;
14743 else
14744 return 16*1024;
14745 } else {
14746 /* XXX DSPC is limited to 4k tiled */
14747 return 8*1024;
14748 }
14749}
14750
b5ea642a
DV
14751static int intel_framebuffer_init(struct drm_device *dev,
14752 struct intel_framebuffer *intel_fb,
14753 struct drm_mode_fb_cmd2 *mode_cmd,
14754 struct drm_i915_gem_object *obj)
79e53945 14755{
7b49f948 14756 struct drm_i915_private *dev_priv = to_i915(dev);
6761dd31 14757 unsigned int aligned_height;
79e53945 14758 int ret;
b321803d 14759 u32 pitch_limit, stride_alignment;
79e53945 14760
dd4916c5
DV
14761 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
14762
2a80eada
DV
14763 if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
14764 /* Enforce that fb modifier and tiling mode match, but only for
14765 * X-tiled. This is needed for FBC. */
14766 if (!!(obj->tiling_mode == I915_TILING_X) !=
14767 !!(mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED)) {
14768 DRM_DEBUG("tiling_mode doesn't match fb modifier\n");
14769 return -EINVAL;
14770 }
14771 } else {
14772 if (obj->tiling_mode == I915_TILING_X)
14773 mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
14774 else if (obj->tiling_mode == I915_TILING_Y) {
14775 DRM_DEBUG("No Y tiling for legacy addfb\n");
14776 return -EINVAL;
14777 }
14778 }
14779
9a8f0a12
TU
14780 /* Passed in modifier sanity checking. */
14781 switch (mode_cmd->modifier[0]) {
14782 case I915_FORMAT_MOD_Y_TILED:
14783 case I915_FORMAT_MOD_Yf_TILED:
14784 if (INTEL_INFO(dev)->gen < 9) {
14785 DRM_DEBUG("Unsupported tiling 0x%llx!\n",
14786 mode_cmd->modifier[0]);
14787 return -EINVAL;
14788 }
14789 case DRM_FORMAT_MOD_NONE:
14790 case I915_FORMAT_MOD_X_TILED:
14791 break;
14792 default:
c0f40428
JB
14793 DRM_DEBUG("Unsupported fb modifier 0x%llx!\n",
14794 mode_cmd->modifier[0]);
57cd6508 14795 return -EINVAL;
c16ed4be 14796 }
57cd6508 14797
7b49f948
VS
14798 stride_alignment = intel_fb_stride_alignment(dev_priv,
14799 mode_cmd->modifier[0],
b321803d
DL
14800 mode_cmd->pixel_format);
14801 if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
14802 DRM_DEBUG("pitch (%d) must be at least %u byte aligned\n",
14803 mode_cmd->pitches[0], stride_alignment);
57cd6508 14804 return -EINVAL;
c16ed4be 14805 }
57cd6508 14806
b321803d
DL
14807 pitch_limit = intel_fb_pitch_limit(dev, mode_cmd->modifier[0],
14808 mode_cmd->pixel_format);
a35cdaa0 14809 if (mode_cmd->pitches[0] > pitch_limit) {
b321803d
DL
14810 DRM_DEBUG("%s pitch (%u) must be at less than %d\n",
14811 mode_cmd->modifier[0] != DRM_FORMAT_MOD_NONE ?
2a80eada 14812 "tiled" : "linear",
a35cdaa0 14813 mode_cmd->pitches[0], pitch_limit);
5d7bd705 14814 return -EINVAL;
c16ed4be 14815 }
5d7bd705 14816
2a80eada 14817 if (mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED &&
c16ed4be
CW
14818 mode_cmd->pitches[0] != obj->stride) {
14819 DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
14820 mode_cmd->pitches[0], obj->stride);
5d7bd705 14821 return -EINVAL;
c16ed4be 14822 }
5d7bd705 14823
57779d06 14824 /* Reject formats not supported by any plane early. */
308e5bcb 14825 switch (mode_cmd->pixel_format) {
57779d06 14826 case DRM_FORMAT_C8:
04b3924d
VS
14827 case DRM_FORMAT_RGB565:
14828 case DRM_FORMAT_XRGB8888:
14829 case DRM_FORMAT_ARGB8888:
57779d06
VS
14830 break;
14831 case DRM_FORMAT_XRGB1555:
c16ed4be 14832 if (INTEL_INFO(dev)->gen > 3) {
4ee62c76
VS
14833 DRM_DEBUG("unsupported pixel format: %s\n",
14834 drm_get_format_name(mode_cmd->pixel_format));
57779d06 14835 return -EINVAL;
c16ed4be 14836 }
57779d06 14837 break;
57779d06 14838 case DRM_FORMAT_ABGR8888:
666a4537
WB
14839 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev) &&
14840 INTEL_INFO(dev)->gen < 9) {
6c0fd451
DL
14841 DRM_DEBUG("unsupported pixel format: %s\n",
14842 drm_get_format_name(mode_cmd->pixel_format));
14843 return -EINVAL;
14844 }
14845 break;
14846 case DRM_FORMAT_XBGR8888:
04b3924d 14847 case DRM_FORMAT_XRGB2101010:
57779d06 14848 case DRM_FORMAT_XBGR2101010:
c16ed4be 14849 if (INTEL_INFO(dev)->gen < 4) {
4ee62c76
VS
14850 DRM_DEBUG("unsupported pixel format: %s\n",
14851 drm_get_format_name(mode_cmd->pixel_format));
57779d06 14852 return -EINVAL;
c16ed4be 14853 }
b5626747 14854 break;
7531208b 14855 case DRM_FORMAT_ABGR2101010:
666a4537 14856 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
7531208b
DL
14857 DRM_DEBUG("unsupported pixel format: %s\n",
14858 drm_get_format_name(mode_cmd->pixel_format));
14859 return -EINVAL;
14860 }
14861 break;
04b3924d
VS
14862 case DRM_FORMAT_YUYV:
14863 case DRM_FORMAT_UYVY:
14864 case DRM_FORMAT_YVYU:
14865 case DRM_FORMAT_VYUY:
c16ed4be 14866 if (INTEL_INFO(dev)->gen < 5) {
4ee62c76
VS
14867 DRM_DEBUG("unsupported pixel format: %s\n",
14868 drm_get_format_name(mode_cmd->pixel_format));
57779d06 14869 return -EINVAL;
c16ed4be 14870 }
57cd6508
CW
14871 break;
14872 default:
4ee62c76
VS
14873 DRM_DEBUG("unsupported pixel format: %s\n",
14874 drm_get_format_name(mode_cmd->pixel_format));
57cd6508
CW
14875 return -EINVAL;
14876 }
14877
90f9a336
VS
14878 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
14879 if (mode_cmd->offsets[0] != 0)
14880 return -EINVAL;
14881
ec2c981e 14882 aligned_height = intel_fb_align_height(dev, mode_cmd->height,
091df6cb
DV
14883 mode_cmd->pixel_format,
14884 mode_cmd->modifier[0]);
53155c0a
DV
14885 /* FIXME drm helper for size checks (especially planar formats)? */
14886 if (obj->base.size < aligned_height * mode_cmd->pitches[0])
14887 return -EINVAL;
14888
c7d73f6a
DV
14889 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
14890 intel_fb->obj = obj;
14891
2d7a215f
VS
14892 intel_fill_fb_info(dev_priv, &intel_fb->base);
14893
79e53945
JB
14894 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
14895 if (ret) {
14896 DRM_ERROR("framebuffer init failed %d\n", ret);
14897 return ret;
14898 }
14899
0b05e1e0
VS
14900 intel_fb->obj->framebuffer_references++;
14901
79e53945
JB
14902 return 0;
14903}
14904
79e53945
JB
14905static struct drm_framebuffer *
14906intel_user_framebuffer_create(struct drm_device *dev,
14907 struct drm_file *filp,
1eb83451 14908 const struct drm_mode_fb_cmd2 *user_mode_cmd)
79e53945 14909{
dcb1394e 14910 struct drm_framebuffer *fb;
05394f39 14911 struct drm_i915_gem_object *obj;
76dc3769 14912 struct drm_mode_fb_cmd2 mode_cmd = *user_mode_cmd;
79e53945 14913
308e5bcb 14914 obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
76dc3769 14915 mode_cmd.handles[0]));
c8725226 14916 if (&obj->base == NULL)
cce13ff7 14917 return ERR_PTR(-ENOENT);
79e53945 14918
92907cbb 14919 fb = intel_framebuffer_create(dev, &mode_cmd, obj);
dcb1394e
LW
14920 if (IS_ERR(fb))
14921 drm_gem_object_unreference_unlocked(&obj->base);
14922
14923 return fb;
79e53945
JB
14924}
14925
0695726e 14926#ifndef CONFIG_DRM_FBDEV_EMULATION
0632fef6 14927static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
4520f53a
DV
14928{
14929}
14930#endif
14931
79e53945 14932static const struct drm_mode_config_funcs intel_mode_funcs = {
79e53945 14933 .fb_create = intel_user_framebuffer_create,
0632fef6 14934 .output_poll_changed = intel_fbdev_output_poll_changed,
5ee67f1c
MR
14935 .atomic_check = intel_atomic_check,
14936 .atomic_commit = intel_atomic_commit,
de419ab6
ML
14937 .atomic_state_alloc = intel_atomic_state_alloc,
14938 .atomic_state_clear = intel_atomic_state_clear,
79e53945
JB
14939};
14940
88212941
ID
14941/**
14942 * intel_init_display_hooks - initialize the display modesetting hooks
14943 * @dev_priv: device private
14944 */
14945void intel_init_display_hooks(struct drm_i915_private *dev_priv)
e70236a8 14946{
88212941 14947 if (HAS_PCH_SPLIT(dev_priv) || IS_G4X(dev_priv))
ee9300bb 14948 dev_priv->display.find_dpll = g4x_find_best_dpll;
88212941 14949 else if (IS_CHERRYVIEW(dev_priv))
ef9348c8 14950 dev_priv->display.find_dpll = chv_find_best_dpll;
88212941 14951 else if (IS_VALLEYVIEW(dev_priv))
ee9300bb 14952 dev_priv->display.find_dpll = vlv_find_best_dpll;
88212941 14953 else if (IS_PINEVIEW(dev_priv))
ee9300bb
DV
14954 dev_priv->display.find_dpll = pnv_find_best_dpll;
14955 else
14956 dev_priv->display.find_dpll = i9xx_find_best_dpll;
14957
88212941 14958 if (INTEL_INFO(dev_priv)->gen >= 9) {
bc8d7dff 14959 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
5724dbd1
DL
14960 dev_priv->display.get_initial_plane_config =
14961 skylake_get_initial_plane_config;
bc8d7dff
DL
14962 dev_priv->display.crtc_compute_clock =
14963 haswell_crtc_compute_clock;
14964 dev_priv->display.crtc_enable = haswell_crtc_enable;
14965 dev_priv->display.crtc_disable = haswell_crtc_disable;
88212941 14966 } else if (HAS_DDI(dev_priv)) {
0e8ffe1b 14967 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
5724dbd1
DL
14968 dev_priv->display.get_initial_plane_config =
14969 ironlake_get_initial_plane_config;
797d0259
ACO
14970 dev_priv->display.crtc_compute_clock =
14971 haswell_crtc_compute_clock;
4f771f10
PZ
14972 dev_priv->display.crtc_enable = haswell_crtc_enable;
14973 dev_priv->display.crtc_disable = haswell_crtc_disable;
88212941 14974 } else if (HAS_PCH_SPLIT(dev_priv)) {
0e8ffe1b 14975 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
5724dbd1
DL
14976 dev_priv->display.get_initial_plane_config =
14977 ironlake_get_initial_plane_config;
3fb37703
ACO
14978 dev_priv->display.crtc_compute_clock =
14979 ironlake_crtc_compute_clock;
76e5a89c
DV
14980 dev_priv->display.crtc_enable = ironlake_crtc_enable;
14981 dev_priv->display.crtc_disable = ironlake_crtc_disable;
88212941 14982 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
89b667f8 14983 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
5724dbd1
DL
14984 dev_priv->display.get_initial_plane_config =
14985 i9xx_get_initial_plane_config;
d6dfee7a 14986 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
89b667f8
JB
14987 dev_priv->display.crtc_enable = valleyview_crtc_enable;
14988 dev_priv->display.crtc_disable = i9xx_crtc_disable;
f564048e 14989 } else {
0e8ffe1b 14990 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
5724dbd1
DL
14991 dev_priv->display.get_initial_plane_config =
14992 i9xx_get_initial_plane_config;
d6dfee7a 14993 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
76e5a89c
DV
14994 dev_priv->display.crtc_enable = i9xx_crtc_enable;
14995 dev_priv->display.crtc_disable = i9xx_crtc_disable;
f564048e 14996 }
e70236a8 14997
e70236a8 14998 /* Returns the core display clock speed */
88212941 14999 if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv))
1652d19e
VS
15000 dev_priv->display.get_display_clock_speed =
15001 skylake_get_display_clock_speed;
88212941 15002 else if (IS_BROXTON(dev_priv))
acd3f3d3
BP
15003 dev_priv->display.get_display_clock_speed =
15004 broxton_get_display_clock_speed;
88212941 15005 else if (IS_BROADWELL(dev_priv))
1652d19e
VS
15006 dev_priv->display.get_display_clock_speed =
15007 broadwell_get_display_clock_speed;
88212941 15008 else if (IS_HASWELL(dev_priv))
1652d19e
VS
15009 dev_priv->display.get_display_clock_speed =
15010 haswell_get_display_clock_speed;
88212941 15011 else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
25eb05fc
JB
15012 dev_priv->display.get_display_clock_speed =
15013 valleyview_get_display_clock_speed;
88212941 15014 else if (IS_GEN5(dev_priv))
b37a6434
VS
15015 dev_priv->display.get_display_clock_speed =
15016 ilk_get_display_clock_speed;
88212941
ID
15017 else if (IS_I945G(dev_priv) || IS_BROADWATER(dev_priv) ||
15018 IS_GEN6(dev_priv) || IS_IVYBRIDGE(dev_priv))
e70236a8
JB
15019 dev_priv->display.get_display_clock_speed =
15020 i945_get_display_clock_speed;
88212941 15021 else if (IS_GM45(dev_priv))
34edce2f
VS
15022 dev_priv->display.get_display_clock_speed =
15023 gm45_get_display_clock_speed;
88212941 15024 else if (IS_CRESTLINE(dev_priv))
34edce2f
VS
15025 dev_priv->display.get_display_clock_speed =
15026 i965gm_get_display_clock_speed;
88212941 15027 else if (IS_PINEVIEW(dev_priv))
34edce2f
VS
15028 dev_priv->display.get_display_clock_speed =
15029 pnv_get_display_clock_speed;
88212941 15030 else if (IS_G33(dev_priv) || IS_G4X(dev_priv))
34edce2f
VS
15031 dev_priv->display.get_display_clock_speed =
15032 g33_get_display_clock_speed;
88212941 15033 else if (IS_I915G(dev_priv))
e70236a8
JB
15034 dev_priv->display.get_display_clock_speed =
15035 i915_get_display_clock_speed;
88212941 15036 else if (IS_I945GM(dev_priv) || IS_845G(dev_priv))
e70236a8
JB
15037 dev_priv->display.get_display_clock_speed =
15038 i9xx_misc_get_display_clock_speed;
88212941 15039 else if (IS_I915GM(dev_priv))
e70236a8
JB
15040 dev_priv->display.get_display_clock_speed =
15041 i915gm_get_display_clock_speed;
88212941 15042 else if (IS_I865G(dev_priv))
e70236a8
JB
15043 dev_priv->display.get_display_clock_speed =
15044 i865_get_display_clock_speed;
88212941 15045 else if (IS_I85X(dev_priv))
e70236a8 15046 dev_priv->display.get_display_clock_speed =
1b1d2716 15047 i85x_get_display_clock_speed;
623e01e5 15048 else { /* 830 */
88212941 15049 WARN(!IS_I830(dev_priv), "Unknown platform. Assuming 133 MHz CDCLK\n");
e70236a8
JB
15050 dev_priv->display.get_display_clock_speed =
15051 i830_get_display_clock_speed;
623e01e5 15052 }
e70236a8 15053
88212941 15054 if (IS_GEN5(dev_priv)) {
3bb11b53 15055 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
88212941 15056 } else if (IS_GEN6(dev_priv)) {
3bb11b53 15057 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
88212941 15058 } else if (IS_IVYBRIDGE(dev_priv)) {
3bb11b53
SJ
15059 /* FIXME: detect B0+ stepping and use auto training */
15060 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
88212941 15061 } else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
3bb11b53 15062 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
88212941 15063 if (IS_BROADWELL(dev_priv)) {
27c329ed
ML
15064 dev_priv->display.modeset_commit_cdclk =
15065 broadwell_modeset_commit_cdclk;
15066 dev_priv->display.modeset_calc_cdclk =
15067 broadwell_modeset_calc_cdclk;
15068 }
88212941 15069 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
27c329ed
ML
15070 dev_priv->display.modeset_commit_cdclk =
15071 valleyview_modeset_commit_cdclk;
15072 dev_priv->display.modeset_calc_cdclk =
15073 valleyview_modeset_calc_cdclk;
88212941 15074 } else if (IS_BROXTON(dev_priv)) {
27c329ed
ML
15075 dev_priv->display.modeset_commit_cdclk =
15076 broxton_modeset_commit_cdclk;
15077 dev_priv->display.modeset_calc_cdclk =
15078 broxton_modeset_calc_cdclk;
e70236a8 15079 }
8c9f3aaf 15080
88212941 15081 switch (INTEL_INFO(dev_priv)->gen) {
8c9f3aaf
JB
15082 case 2:
15083 dev_priv->display.queue_flip = intel_gen2_queue_flip;
15084 break;
15085
15086 case 3:
15087 dev_priv->display.queue_flip = intel_gen3_queue_flip;
15088 break;
15089
15090 case 4:
15091 case 5:
15092 dev_priv->display.queue_flip = intel_gen4_queue_flip;
15093 break;
15094
15095 case 6:
15096 dev_priv->display.queue_flip = intel_gen6_queue_flip;
15097 break;
7c9017e5 15098 case 7:
4e0bbc31 15099 case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
7c9017e5
JB
15100 dev_priv->display.queue_flip = intel_gen7_queue_flip;
15101 break;
830c81db 15102 case 9:
ba343e02
TU
15103 /* Drop through - unsupported since execlist only. */
15104 default:
15105 /* Default just returns -ENODEV to indicate unsupported */
15106 dev_priv->display.queue_flip = intel_default_queue_flip;
8c9f3aaf 15107 }
e70236a8
JB
15108}
15109
b690e96c
JB
15110/*
15111 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
15112 * resume, or other times. This quirk makes sure that's the case for
15113 * affected systems.
15114 */
0206e353 15115static void quirk_pipea_force(struct drm_device *dev)
b690e96c
JB
15116{
15117 struct drm_i915_private *dev_priv = dev->dev_private;
15118
15119 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
bc0daf48 15120 DRM_INFO("applying pipe a force quirk\n");
b690e96c
JB
15121}
15122
b6b5d049
VS
15123static void quirk_pipeb_force(struct drm_device *dev)
15124{
15125 struct drm_i915_private *dev_priv = dev->dev_private;
15126
15127 dev_priv->quirks |= QUIRK_PIPEB_FORCE;
15128 DRM_INFO("applying pipe b force quirk\n");
15129}
15130
435793df
KP
15131/*
15132 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
15133 */
15134static void quirk_ssc_force_disable(struct drm_device *dev)
15135{
15136 struct drm_i915_private *dev_priv = dev->dev_private;
15137 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
bc0daf48 15138 DRM_INFO("applying lvds SSC disable quirk\n");
435793df
KP
15139}
15140
4dca20ef 15141/*
5a15ab5b
CE
15142 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
15143 * brightness value
4dca20ef
CE
15144 */
15145static void quirk_invert_brightness(struct drm_device *dev)
15146{
15147 struct drm_i915_private *dev_priv = dev->dev_private;
15148 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
bc0daf48 15149 DRM_INFO("applying inverted panel brightness quirk\n");
435793df
KP
15150}
15151
9c72cc6f
SD
15152/* Some VBT's incorrectly indicate no backlight is present */
15153static void quirk_backlight_present(struct drm_device *dev)
15154{
15155 struct drm_i915_private *dev_priv = dev->dev_private;
15156 dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
15157 DRM_INFO("applying backlight present quirk\n");
15158}
15159
b690e96c
JB
15160struct intel_quirk {
15161 int device;
15162 int subsystem_vendor;
15163 int subsystem_device;
15164 void (*hook)(struct drm_device *dev);
15165};
15166
5f85f176
EE
15167/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
15168struct intel_dmi_quirk {
15169 void (*hook)(struct drm_device *dev);
15170 const struct dmi_system_id (*dmi_id_list)[];
15171};
15172
15173static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
15174{
15175 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
15176 return 1;
15177}
15178
15179static const struct intel_dmi_quirk intel_dmi_quirks[] = {
15180 {
15181 .dmi_id_list = &(const struct dmi_system_id[]) {
15182 {
15183 .callback = intel_dmi_reverse_brightness,
15184 .ident = "NCR Corporation",
15185 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
15186 DMI_MATCH(DMI_PRODUCT_NAME, ""),
15187 },
15188 },
15189 { } /* terminating entry */
15190 },
15191 .hook = quirk_invert_brightness,
15192 },
15193};
15194
c43b5634 15195static struct intel_quirk intel_quirks[] = {
b690e96c
JB
15196 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
15197 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
15198
b690e96c
JB
15199 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
15200 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
15201
5f080c0f
VS
15202 /* 830 needs to leave pipe A & dpll A up */
15203 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
15204
b6b5d049
VS
15205 /* 830 needs to leave pipe B & dpll B up */
15206 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
15207
435793df
KP
15208 /* Lenovo U160 cannot use SSC on LVDS */
15209 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
070d329a
MAS
15210
15211 /* Sony Vaio Y cannot use SSC on LVDS */
15212 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
5a15ab5b 15213
be505f64
AH
15214 /* Acer Aspire 5734Z must invert backlight brightness */
15215 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
15216
15217 /* Acer/eMachines G725 */
15218 { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
15219
15220 /* Acer/eMachines e725 */
15221 { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
15222
15223 /* Acer/Packard Bell NCL20 */
15224 { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
15225
15226 /* Acer Aspire 4736Z */
15227 { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
0f540c3a
JN
15228
15229 /* Acer Aspire 5336 */
15230 { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
2e93a1aa
SD
15231
15232 /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
15233 { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
d4967d8c 15234
dfb3d47b
SD
15235 /* Acer C720 Chromebook (Core i3 4005U) */
15236 { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
15237
b2a9601c 15238 /* Apple Macbook 2,1 (Core 2 T7400) */
15239 { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
15240
1b9448b0
JN
15241 /* Apple Macbook 4,1 */
15242 { 0x2a02, 0x106b, 0x00a1, quirk_backlight_present },
15243
d4967d8c
SD
15244 /* Toshiba CB35 Chromebook (Celeron 2955U) */
15245 { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
724cb06f
SD
15246
15247 /* HP Chromebook 14 (Celeron 2955U) */
15248 { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
cf6f0af9
JN
15249
15250 /* Dell Chromebook 11 */
15251 { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
9be64eee
JN
15252
15253 /* Dell Chromebook 11 (2015 version) */
15254 { 0x0a16, 0x1028, 0x0a35, quirk_backlight_present },
b690e96c
JB
15255};
15256
15257static void intel_init_quirks(struct drm_device *dev)
15258{
15259 struct pci_dev *d = dev->pdev;
15260 int i;
15261
15262 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
15263 struct intel_quirk *q = &intel_quirks[i];
15264
15265 if (d->device == q->device &&
15266 (d->subsystem_vendor == q->subsystem_vendor ||
15267 q->subsystem_vendor == PCI_ANY_ID) &&
15268 (d->subsystem_device == q->subsystem_device ||
15269 q->subsystem_device == PCI_ANY_ID))
15270 q->hook(dev);
15271 }
5f85f176
EE
15272 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
15273 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
15274 intel_dmi_quirks[i].hook(dev);
15275 }
b690e96c
JB
15276}
15277
9cce37f4
JB
15278/* Disable the VGA plane that we never use */
15279static void i915_disable_vga(struct drm_device *dev)
15280{
15281 struct drm_i915_private *dev_priv = dev->dev_private;
15282 u8 sr1;
f0f59a00 15283 i915_reg_t vga_reg = i915_vgacntrl_reg(dev);
9cce37f4 15284
2b37c616 15285 /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
9cce37f4 15286 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
3fdcf431 15287 outb(SR01, VGA_SR_INDEX);
9cce37f4
JB
15288 sr1 = inb(VGA_SR_DATA);
15289 outb(sr1 | 1<<5, VGA_SR_DATA);
15290 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
15291 udelay(300);
15292
01f5a626 15293 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
9cce37f4
JB
15294 POSTING_READ(vga_reg);
15295}
15296
f817586c
DV
15297void intel_modeset_init_hw(struct drm_device *dev)
15298{
1a617b77
ML
15299 struct drm_i915_private *dev_priv = dev->dev_private;
15300
b6283055 15301 intel_update_cdclk(dev);
1a617b77
ML
15302
15303 dev_priv->atomic_cdclk_freq = dev_priv->cdclk_freq;
15304
f817586c 15305 intel_init_clock_gating(dev);
8090c6b9 15306 intel_enable_gt_powersave(dev);
f817586c
DV
15307}
15308
d93c0372
MR
15309/*
15310 * Calculate what we think the watermarks should be for the state we've read
15311 * out of the hardware and then immediately program those watermarks so that
15312 * we ensure the hardware settings match our internal state.
15313 *
15314 * We can calculate what we think WM's should be by creating a duplicate of the
15315 * current state (which was constructed during hardware readout) and running it
15316 * through the atomic check code to calculate new watermark values in the
15317 * state object.
15318 */
15319static void sanitize_watermarks(struct drm_device *dev)
15320{
15321 struct drm_i915_private *dev_priv = to_i915(dev);
15322 struct drm_atomic_state *state;
15323 struct drm_crtc *crtc;
15324 struct drm_crtc_state *cstate;
15325 struct drm_modeset_acquire_ctx ctx;
15326 int ret;
15327 int i;
15328
15329 /* Only supported on platforms that use atomic watermark design */
ed4a6a7c 15330 if (!dev_priv->display.optimize_watermarks)
d93c0372
MR
15331 return;
15332
15333 /*
15334 * We need to hold connection_mutex before calling duplicate_state so
15335 * that the connector loop is protected.
15336 */
15337 drm_modeset_acquire_init(&ctx, 0);
15338retry:
0cd1262d 15339 ret = drm_modeset_lock_all_ctx(dev, &ctx);
d93c0372
MR
15340 if (ret == -EDEADLK) {
15341 drm_modeset_backoff(&ctx);
15342 goto retry;
15343 } else if (WARN_ON(ret)) {
0cd1262d 15344 goto fail;
d93c0372
MR
15345 }
15346
15347 state = drm_atomic_helper_duplicate_state(dev, &ctx);
15348 if (WARN_ON(IS_ERR(state)))
0cd1262d 15349 goto fail;
d93c0372 15350
ed4a6a7c
MR
15351 /*
15352 * Hardware readout is the only time we don't want to calculate
15353 * intermediate watermarks (since we don't trust the current
15354 * watermarks).
15355 */
15356 to_intel_atomic_state(state)->skip_intermediate_wm = true;
15357
d93c0372
MR
15358 ret = intel_atomic_check(dev, state);
15359 if (ret) {
15360 /*
15361 * If we fail here, it means that the hardware appears to be
15362 * programmed in a way that shouldn't be possible, given our
15363 * understanding of watermark requirements. This might mean a
15364 * mistake in the hardware readout code or a mistake in the
15365 * watermark calculations for a given platform. Raise a WARN
15366 * so that this is noticeable.
15367 *
15368 * If this actually happens, we'll have to just leave the
15369 * BIOS-programmed watermarks untouched and hope for the best.
15370 */
15371 WARN(true, "Could not determine valid watermarks for inherited state\n");
0cd1262d 15372 goto fail;
d93c0372
MR
15373 }
15374
15375 /* Write calculated watermark values back */
15376 to_i915(dev)->wm.config = to_intel_atomic_state(state)->wm_config;
15377 for_each_crtc_in_state(state, crtc, cstate, i) {
15378 struct intel_crtc_state *cs = to_intel_crtc_state(cstate);
15379
ed4a6a7c
MR
15380 cs->wm.need_postvbl_update = true;
15381 dev_priv->display.optimize_watermarks(cs);
d93c0372
MR
15382 }
15383
15384 drm_atomic_state_free(state);
0cd1262d 15385fail:
d93c0372
MR
15386 drm_modeset_drop_locks(&ctx);
15387 drm_modeset_acquire_fini(&ctx);
15388}
15389
79e53945
JB
15390void intel_modeset_init(struct drm_device *dev)
15391{
652c393a 15392 struct drm_i915_private *dev_priv = dev->dev_private;
1fe47785 15393 int sprite, ret;
8cc87b75 15394 enum pipe pipe;
46f297fb 15395 struct intel_crtc *crtc;
79e53945
JB
15396
15397 drm_mode_config_init(dev);
15398
15399 dev->mode_config.min_width = 0;
15400 dev->mode_config.min_height = 0;
15401
019d96cb
DA
15402 dev->mode_config.preferred_depth = 24;
15403 dev->mode_config.prefer_shadow = 1;
15404
25bab385
TU
15405 dev->mode_config.allow_fb_modifiers = true;
15406
e6ecefaa 15407 dev->mode_config.funcs = &intel_mode_funcs;
79e53945 15408
b690e96c
JB
15409 intel_init_quirks(dev);
15410
1fa61106
ED
15411 intel_init_pm(dev);
15412
e3c74757
BW
15413 if (INTEL_INFO(dev)->num_pipes == 0)
15414 return;
15415
69f92f67
LW
15416 /*
15417 * There may be no VBT; and if the BIOS enabled SSC we can
15418 * just keep using it to avoid unnecessary flicker. Whereas if the
15419 * BIOS isn't using it, don't assume it will work even if the VBT
15420 * indicates as much.
15421 */
15422 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)) {
15423 bool bios_lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
15424 DREF_SSC1_ENABLE);
15425
15426 if (dev_priv->vbt.lvds_use_ssc != bios_lvds_use_ssc) {
15427 DRM_DEBUG_KMS("SSC %sabled by BIOS, overriding VBT which says %sabled\n",
15428 bios_lvds_use_ssc ? "en" : "dis",
15429 dev_priv->vbt.lvds_use_ssc ? "en" : "dis");
15430 dev_priv->vbt.lvds_use_ssc = bios_lvds_use_ssc;
15431 }
15432 }
15433
a6c45cf0
CW
15434 if (IS_GEN2(dev)) {
15435 dev->mode_config.max_width = 2048;
15436 dev->mode_config.max_height = 2048;
15437 } else if (IS_GEN3(dev)) {
5e4d6fa7
KP
15438 dev->mode_config.max_width = 4096;
15439 dev->mode_config.max_height = 4096;
79e53945 15440 } else {
a6c45cf0
CW
15441 dev->mode_config.max_width = 8192;
15442 dev->mode_config.max_height = 8192;
79e53945 15443 }
068be561 15444
dc41c154
VS
15445 if (IS_845G(dev) || IS_I865G(dev)) {
15446 dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
15447 dev->mode_config.cursor_height = 1023;
15448 } else if (IS_GEN2(dev)) {
068be561
DL
15449 dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
15450 dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
15451 } else {
15452 dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
15453 dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
15454 }
15455
62106b4f 15456 dev->mode_config.fb_base = dev_priv->ggtt.mappable_base;
79e53945 15457
28c97730 15458 DRM_DEBUG_KMS("%d display pipe%s available.\n",
7eb552ae
BW
15459 INTEL_INFO(dev)->num_pipes,
15460 INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
79e53945 15461
055e393f 15462 for_each_pipe(dev_priv, pipe) {
8cc87b75 15463 intel_crtc_init(dev, pipe);
3bdcfc0c 15464 for_each_sprite(dev_priv, pipe, sprite) {
1fe47785 15465 ret = intel_plane_init(dev, pipe, sprite);
7f1f3851 15466 if (ret)
06da8da2 15467 DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
1fe47785 15468 pipe_name(pipe), sprite_name(pipe, sprite), ret);
7f1f3851 15469 }
79e53945
JB
15470 }
15471
bfa7df01 15472 intel_update_czclk(dev_priv);
e7dc33f3 15473 intel_update_rawclk(dev_priv);
bfa7df01
VS
15474 intel_update_cdclk(dev);
15475
e72f9fbf 15476 intel_shared_dpll_init(dev);
ee7b9f93 15477
9cce37f4
JB
15478 /* Just disable it once at startup */
15479 i915_disable_vga(dev);
79e53945 15480 intel_setup_outputs(dev);
11be49eb 15481
6e9f798d 15482 drm_modeset_lock_all(dev);
043e9bda 15483 intel_modeset_setup_hw_state(dev);
6e9f798d 15484 drm_modeset_unlock_all(dev);
46f297fb 15485
d3fcc808 15486 for_each_intel_crtc(dev, crtc) {
eeebeac5
ML
15487 struct intel_initial_plane_config plane_config = {};
15488
46f297fb
JB
15489 if (!crtc->active)
15490 continue;
15491
46f297fb 15492 /*
46f297fb
JB
15493 * Note that reserving the BIOS fb up front prevents us
15494 * from stuffing other stolen allocations like the ring
15495 * on top. This prevents some ugliness at boot time, and
15496 * can even allow for smooth boot transitions if the BIOS
15497 * fb is large enough for the active pipe configuration.
15498 */
eeebeac5
ML
15499 dev_priv->display.get_initial_plane_config(crtc,
15500 &plane_config);
15501
15502 /*
15503 * If the fb is shared between multiple heads, we'll
15504 * just get the first one.
15505 */
15506 intel_find_initial_plane_obj(crtc, &plane_config);
46f297fb 15507 }
d93c0372
MR
15508
15509 /*
15510 * Make sure hardware watermarks really match the state we read out.
15511 * Note that we need to do this after reconstructing the BIOS fb's
15512 * since the watermark calculation done here will use pstate->fb.
15513 */
15514 sanitize_watermarks(dev);
2c7111db
CW
15515}
15516
7fad798e
DV
15517static void intel_enable_pipe_a(struct drm_device *dev)
15518{
15519 struct intel_connector *connector;
15520 struct drm_connector *crt = NULL;
15521 struct intel_load_detect_pipe load_detect_temp;
208bf9fd 15522 struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
7fad798e
DV
15523
15524 /* We can't just switch on the pipe A, we need to set things up with a
15525 * proper mode and output configuration. As a gross hack, enable pipe A
15526 * by enabling the load detect pipe once. */
3a3371ff 15527 for_each_intel_connector(dev, connector) {
7fad798e
DV
15528 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
15529 crt = &connector->base;
15530 break;
15531 }
15532 }
15533
15534 if (!crt)
15535 return;
15536
208bf9fd 15537 if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
49172fee 15538 intel_release_load_detect_pipe(crt, &load_detect_temp, ctx);
7fad798e
DV
15539}
15540
fa555837
DV
15541static bool
15542intel_check_plane_mapping(struct intel_crtc *crtc)
15543{
7eb552ae
BW
15544 struct drm_device *dev = crtc->base.dev;
15545 struct drm_i915_private *dev_priv = dev->dev_private;
649636ef 15546 u32 val;
fa555837 15547
7eb552ae 15548 if (INTEL_INFO(dev)->num_pipes == 1)
fa555837
DV
15549 return true;
15550
649636ef 15551 val = I915_READ(DSPCNTR(!crtc->plane));
fa555837
DV
15552
15553 if ((val & DISPLAY_PLANE_ENABLE) &&
15554 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
15555 return false;
15556
15557 return true;
15558}
15559
02e93c35
VS
15560static bool intel_crtc_has_encoders(struct intel_crtc *crtc)
15561{
15562 struct drm_device *dev = crtc->base.dev;
15563 struct intel_encoder *encoder;
15564
15565 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
15566 return true;
15567
15568 return false;
15569}
15570
dd756198
VS
15571static bool intel_encoder_has_connectors(struct intel_encoder *encoder)
15572{
15573 struct drm_device *dev = encoder->base.dev;
15574 struct intel_connector *connector;
15575
15576 for_each_connector_on_encoder(dev, &encoder->base, connector)
15577 return true;
15578
15579 return false;
15580}
15581
24929352
DV
15582static void intel_sanitize_crtc(struct intel_crtc *crtc)
15583{
15584 struct drm_device *dev = crtc->base.dev;
15585 struct drm_i915_private *dev_priv = dev->dev_private;
4d1de975 15586 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
24929352 15587
24929352 15588 /* Clear any frame start delays used for debugging left by the BIOS */
4d1de975
JN
15589 if (!transcoder_is_dsi(cpu_transcoder)) {
15590 i915_reg_t reg = PIPECONF(cpu_transcoder);
15591
15592 I915_WRITE(reg,
15593 I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
15594 }
24929352 15595
d3eaf884 15596 /* restore vblank interrupts to correct state */
9625604c 15597 drm_crtc_vblank_reset(&crtc->base);
d297e103 15598 if (crtc->active) {
f9cd7b88
VS
15599 struct intel_plane *plane;
15600
9625604c 15601 drm_crtc_vblank_on(&crtc->base);
f9cd7b88
VS
15602
15603 /* Disable everything but the primary plane */
15604 for_each_intel_plane_on_crtc(dev, crtc, plane) {
15605 if (plane->base.type == DRM_PLANE_TYPE_PRIMARY)
15606 continue;
15607
15608 plane->disable_plane(&plane->base, &crtc->base);
15609 }
9625604c 15610 }
d3eaf884 15611
24929352 15612 /* We need to sanitize the plane -> pipe mapping first because this will
fa555837
DV
15613 * disable the crtc (and hence change the state) if it is wrong. Note
15614 * that gen4+ has a fixed plane -> pipe mapping. */
15615 if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
24929352
DV
15616 bool plane;
15617
24929352
DV
15618 DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
15619 crtc->base.base.id);
15620
15621 /* Pipe has the wrong plane attached and the plane is active.
15622 * Temporarily change the plane mapping and disable everything
15623 * ... */
15624 plane = crtc->plane;
b70709a6 15625 to_intel_plane_state(crtc->base.primary->state)->visible = true;
24929352 15626 crtc->plane = !plane;
b17d48e2 15627 intel_crtc_disable_noatomic(&crtc->base);
24929352 15628 crtc->plane = plane;
24929352 15629 }
24929352 15630
7fad798e
DV
15631 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
15632 crtc->pipe == PIPE_A && !crtc->active) {
15633 /* BIOS forgot to enable pipe A, this mostly happens after
15634 * resume. Force-enable the pipe to fix this, the update_dpms
15635 * call below we restore the pipe to the right state, but leave
15636 * the required bits on. */
15637 intel_enable_pipe_a(dev);
15638 }
15639
24929352
DV
15640 /* Adjust the state of the output pipe according to whether we
15641 * have active connectors/encoders. */
842e0307 15642 if (crtc->active && !intel_crtc_has_encoders(crtc))
b17d48e2 15643 intel_crtc_disable_noatomic(&crtc->base);
24929352 15644
a3ed6aad 15645 if (crtc->active || HAS_GMCH_DISPLAY(dev)) {
4cc31489
DV
15646 /*
15647 * We start out with underrun reporting disabled to avoid races.
15648 * For correct bookkeeping mark this on active crtcs.
15649 *
c5ab3bc0
DV
15650 * Also on gmch platforms we dont have any hardware bits to
15651 * disable the underrun reporting. Which means we need to start
15652 * out with underrun reporting disabled also on inactive pipes,
15653 * since otherwise we'll complain about the garbage we read when
15654 * e.g. coming up after runtime pm.
15655 *
4cc31489
DV
15656 * No protection against concurrent access is required - at
15657 * worst a fifo underrun happens which also sets this to false.
15658 */
15659 crtc->cpu_fifo_underrun_disabled = true;
15660 crtc->pch_fifo_underrun_disabled = true;
15661 }
24929352
DV
15662}
15663
15664static void intel_sanitize_encoder(struct intel_encoder *encoder)
15665{
15666 struct intel_connector *connector;
15667 struct drm_device *dev = encoder->base.dev;
15668
15669 /* We need to check both for a crtc link (meaning that the
15670 * encoder is active and trying to read from a pipe) and the
15671 * pipe itself being active. */
15672 bool has_active_crtc = encoder->base.crtc &&
15673 to_intel_crtc(encoder->base.crtc)->active;
15674
dd756198 15675 if (intel_encoder_has_connectors(encoder) && !has_active_crtc) {
24929352
DV
15676 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
15677 encoder->base.base.id,
8e329a03 15678 encoder->base.name);
24929352
DV
15679
15680 /* Connector is active, but has no active pipe. This is
15681 * fallout from our resume register restoring. Disable
15682 * the encoder manually again. */
15683 if (encoder->base.crtc) {
15684 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
15685 encoder->base.base.id,
8e329a03 15686 encoder->base.name);
24929352 15687 encoder->disable(encoder);
a62d1497
VS
15688 if (encoder->post_disable)
15689 encoder->post_disable(encoder);
24929352 15690 }
7f1950fb 15691 encoder->base.crtc = NULL;
24929352
DV
15692
15693 /* Inconsistent output/port/pipe state happens presumably due to
15694 * a bug in one of the get_hw_state functions. Or someplace else
15695 * in our code, like the register restore mess on resume. Clamp
15696 * things to off as a safer default. */
3a3371ff 15697 for_each_intel_connector(dev, connector) {
24929352
DV
15698 if (connector->encoder != encoder)
15699 continue;
7f1950fb
EE
15700 connector->base.dpms = DRM_MODE_DPMS_OFF;
15701 connector->base.encoder = NULL;
24929352
DV
15702 }
15703 }
15704 /* Enabled encoders without active connectors will be fixed in
15705 * the crtc fixup. */
15706}
15707
04098753 15708void i915_redisable_vga_power_on(struct drm_device *dev)
0fde901f
KM
15709{
15710 struct drm_i915_private *dev_priv = dev->dev_private;
f0f59a00 15711 i915_reg_t vga_reg = i915_vgacntrl_reg(dev);
0fde901f 15712
04098753
ID
15713 if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
15714 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
15715 i915_disable_vga(dev);
15716 }
15717}
15718
15719void i915_redisable_vga(struct drm_device *dev)
15720{
15721 struct drm_i915_private *dev_priv = dev->dev_private;
15722
8dc8a27c
PZ
15723 /* This function can be called both from intel_modeset_setup_hw_state or
15724 * at a very early point in our resume sequence, where the power well
15725 * structures are not yet restored. Since this function is at a very
15726 * paranoid "someone might have enabled VGA while we were not looking"
15727 * level, just check if the power well is enabled instead of trying to
15728 * follow the "don't touch the power well if we don't need it" policy
15729 * the rest of the driver uses. */
6392f847 15730 if (!intel_display_power_get_if_enabled(dev_priv, POWER_DOMAIN_VGA))
8dc8a27c
PZ
15731 return;
15732
04098753 15733 i915_redisable_vga_power_on(dev);
6392f847
ID
15734
15735 intel_display_power_put(dev_priv, POWER_DOMAIN_VGA);
0fde901f
KM
15736}
15737
f9cd7b88 15738static bool primary_get_hw_state(struct intel_plane *plane)
98ec7739 15739{
f9cd7b88 15740 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
98ec7739 15741
f9cd7b88 15742 return I915_READ(DSPCNTR(plane->plane)) & DISPLAY_PLANE_ENABLE;
d032ffa0
ML
15743}
15744
f9cd7b88
VS
15745/* FIXME read out full plane state for all planes */
15746static void readout_plane_state(struct intel_crtc *crtc)
d032ffa0 15747{
b26d3ea3 15748 struct drm_plane *primary = crtc->base.primary;
f9cd7b88 15749 struct intel_plane_state *plane_state =
b26d3ea3 15750 to_intel_plane_state(primary->state);
d032ffa0 15751
19b8d387 15752 plane_state->visible = crtc->active &&
b26d3ea3
ML
15753 primary_get_hw_state(to_intel_plane(primary));
15754
15755 if (plane_state->visible)
15756 crtc->base.state->plane_mask |= 1 << drm_plane_index(primary);
98ec7739
VS
15757}
15758
30e984df 15759static void intel_modeset_readout_hw_state(struct drm_device *dev)
24929352
DV
15760{
15761 struct drm_i915_private *dev_priv = dev->dev_private;
15762 enum pipe pipe;
24929352
DV
15763 struct intel_crtc *crtc;
15764 struct intel_encoder *encoder;
15765 struct intel_connector *connector;
5358901f 15766 int i;
24929352 15767
565602d7
ML
15768 dev_priv->active_crtcs = 0;
15769
d3fcc808 15770 for_each_intel_crtc(dev, crtc) {
565602d7
ML
15771 struct intel_crtc_state *crtc_state = crtc->config;
15772 int pixclk = 0;
3b117c8f 15773
565602d7
ML
15774 __drm_atomic_helper_crtc_destroy_state(&crtc->base, &crtc_state->base);
15775 memset(crtc_state, 0, sizeof(*crtc_state));
15776 crtc_state->base.crtc = &crtc->base;
24929352 15777
565602d7
ML
15778 crtc_state->base.active = crtc_state->base.enable =
15779 dev_priv->display.get_pipe_config(crtc, crtc_state);
15780
15781 crtc->base.enabled = crtc_state->base.enable;
15782 crtc->active = crtc_state->base.active;
15783
15784 if (crtc_state->base.active) {
15785 dev_priv->active_crtcs |= 1 << crtc->pipe;
15786
15787 if (IS_BROADWELL(dev_priv)) {
15788 pixclk = ilk_pipe_pixel_rate(crtc_state);
15789
15790 /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
15791 if (crtc_state->ips_enabled)
15792 pixclk = DIV_ROUND_UP(pixclk * 100, 95);
15793 } else if (IS_VALLEYVIEW(dev_priv) ||
15794 IS_CHERRYVIEW(dev_priv) ||
15795 IS_BROXTON(dev_priv))
15796 pixclk = crtc_state->base.adjusted_mode.crtc_clock;
15797 else
15798 WARN_ON(dev_priv->display.modeset_calc_cdclk);
15799 }
15800
15801 dev_priv->min_pixclk[crtc->pipe] = pixclk;
b70709a6 15802
f9cd7b88 15803 readout_plane_state(crtc);
24929352
DV
15804
15805 DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
15806 crtc->base.base.id,
15807 crtc->active ? "enabled" : "disabled");
15808 }
15809
5358901f
DV
15810 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
15811 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
15812
2edd6443
ACO
15813 pll->on = pll->funcs.get_hw_state(dev_priv, pll,
15814 &pll->config.hw_state);
3e369b76 15815 pll->config.crtc_mask = 0;
d3fcc808 15816 for_each_intel_crtc(dev, crtc) {
2dd66ebd 15817 if (crtc->active && crtc->config->shared_dpll == pll)
3e369b76 15818 pll->config.crtc_mask |= 1 << crtc->pipe;
5358901f 15819 }
2dd66ebd 15820 pll->active_mask = pll->config.crtc_mask;
5358901f 15821
1e6f2ddc 15822 DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
3e369b76 15823 pll->name, pll->config.crtc_mask, pll->on);
5358901f
DV
15824 }
15825
b2784e15 15826 for_each_intel_encoder(dev, encoder) {
24929352
DV
15827 pipe = 0;
15828
15829 if (encoder->get_hw_state(encoder, &pipe)) {
045ac3b5
JB
15830 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
15831 encoder->base.crtc = &crtc->base;
6e3c9717 15832 encoder->get_config(encoder, crtc->config);
24929352
DV
15833 } else {
15834 encoder->base.crtc = NULL;
15835 }
15836
6f2bcceb 15837 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
24929352 15838 encoder->base.base.id,
8e329a03 15839 encoder->base.name,
24929352 15840 encoder->base.crtc ? "enabled" : "disabled",
6f2bcceb 15841 pipe_name(pipe));
24929352
DV
15842 }
15843
3a3371ff 15844 for_each_intel_connector(dev, connector) {
24929352
DV
15845 if (connector->get_hw_state(connector)) {
15846 connector->base.dpms = DRM_MODE_DPMS_ON;
2aa974c9
ML
15847
15848 encoder = connector->encoder;
15849 connector->base.encoder = &encoder->base;
15850
15851 if (encoder->base.crtc &&
15852 encoder->base.crtc->state->active) {
15853 /*
15854 * This has to be done during hardware readout
15855 * because anything calling .crtc_disable may
15856 * rely on the connector_mask being accurate.
15857 */
15858 encoder->base.crtc->state->connector_mask |=
15859 1 << drm_connector_index(&connector->base);
e87a52b3
ML
15860 encoder->base.crtc->state->encoder_mask |=
15861 1 << drm_encoder_index(&encoder->base);
2aa974c9
ML
15862 }
15863
24929352
DV
15864 } else {
15865 connector->base.dpms = DRM_MODE_DPMS_OFF;
15866 connector->base.encoder = NULL;
15867 }
15868 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
15869 connector->base.base.id,
c23cc417 15870 connector->base.name,
24929352
DV
15871 connector->base.encoder ? "enabled" : "disabled");
15872 }
7f4c6284
VS
15873
15874 for_each_intel_crtc(dev, crtc) {
15875 crtc->base.hwmode = crtc->config->base.adjusted_mode;
15876
15877 memset(&crtc->base.mode, 0, sizeof(crtc->base.mode));
15878 if (crtc->base.state->active) {
15879 intel_mode_from_pipe_config(&crtc->base.mode, crtc->config);
15880 intel_mode_from_pipe_config(&crtc->base.state->adjusted_mode, crtc->config);
15881 WARN_ON(drm_atomic_set_mode_for_crtc(crtc->base.state, &crtc->base.mode));
15882
15883 /*
15884 * The initial mode needs to be set in order to keep
15885 * the atomic core happy. It wants a valid mode if the
15886 * crtc's enabled, so we do the above call.
15887 *
15888 * At this point some state updated by the connectors
15889 * in their ->detect() callback has not run yet, so
15890 * no recalculation can be done yet.
15891 *
15892 * Even if we could do a recalculation and modeset
15893 * right now it would cause a double modeset if
15894 * fbdev or userspace chooses a different initial mode.
15895 *
15896 * If that happens, someone indicated they wanted a
15897 * mode change, which means it's safe to do a full
15898 * recalculation.
15899 */
15900 crtc->base.state->mode.private_flags = I915_MODE_FLAG_INHERITED;
9eca6832
VS
15901
15902 drm_calc_timestamping_constants(&crtc->base, &crtc->base.hwmode);
15903 update_scanline_offset(crtc);
7f4c6284 15904 }
e3b247da
VS
15905
15906 intel_pipe_config_sanity_check(dev_priv, crtc->config);
7f4c6284 15907 }
30e984df
DV
15908}
15909
043e9bda
ML
15910/* Scan out the current hw modeset state,
15911 * and sanitizes it to the current state
15912 */
15913static void
15914intel_modeset_setup_hw_state(struct drm_device *dev)
30e984df
DV
15915{
15916 struct drm_i915_private *dev_priv = dev->dev_private;
15917 enum pipe pipe;
30e984df
DV
15918 struct intel_crtc *crtc;
15919 struct intel_encoder *encoder;
35c95375 15920 int i;
30e984df
DV
15921
15922 intel_modeset_readout_hw_state(dev);
24929352
DV
15923
15924 /* HW state is read out, now we need to sanitize this mess. */
b2784e15 15925 for_each_intel_encoder(dev, encoder) {
24929352
DV
15926 intel_sanitize_encoder(encoder);
15927 }
15928
055e393f 15929 for_each_pipe(dev_priv, pipe) {
24929352
DV
15930 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
15931 intel_sanitize_crtc(crtc);
6e3c9717
ACO
15932 intel_dump_pipe_config(crtc, crtc->config,
15933 "[setup_hw_state]");
24929352 15934 }
9a935856 15935
d29b2f9d
ACO
15936 intel_modeset_update_connector_atomic_state(dev);
15937
35c95375
DV
15938 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
15939 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
15940
2dd66ebd 15941 if (!pll->on || pll->active_mask)
35c95375
DV
15942 continue;
15943
15944 DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
15945
2edd6443 15946 pll->funcs.disable(dev_priv, pll);
35c95375
DV
15947 pll->on = false;
15948 }
15949
666a4537 15950 if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev))
6eb1a681
VS
15951 vlv_wm_get_hw_state(dev);
15952 else if (IS_GEN9(dev))
3078999f
PB
15953 skl_wm_get_hw_state(dev);
15954 else if (HAS_PCH_SPLIT(dev))
243e6a44 15955 ilk_wm_get_hw_state(dev);
292b990e
ML
15956
15957 for_each_intel_crtc(dev, crtc) {
15958 unsigned long put_domains;
15959
74bff5f9 15960 put_domains = modeset_get_crtc_power_domains(&crtc->base, crtc->config);
292b990e
ML
15961 if (WARN_ON(put_domains))
15962 modeset_put_power_domains(dev_priv, put_domains);
15963 }
15964 intel_display_set_init_power(dev_priv, false);
010cf73d
PZ
15965
15966 intel_fbc_init_pipe_state(dev_priv);
043e9bda 15967}
7d0bc1ea 15968
043e9bda
ML
15969void intel_display_resume(struct drm_device *dev)
15970{
e2c8b870
ML
15971 struct drm_i915_private *dev_priv = to_i915(dev);
15972 struct drm_atomic_state *state = dev_priv->modeset_restore_state;
15973 struct drm_modeset_acquire_ctx ctx;
043e9bda 15974 int ret;
e2c8b870 15975 bool setup = false;
f30da187 15976
e2c8b870 15977 dev_priv->modeset_restore_state = NULL;
043e9bda 15978
ea49c9ac
ML
15979 /*
15980 * This is a cludge because with real atomic modeset mode_config.mutex
15981 * won't be taken. Unfortunately some probed state like
15982 * audio_codec_enable is still protected by mode_config.mutex, so lock
15983 * it here for now.
15984 */
15985 mutex_lock(&dev->mode_config.mutex);
e2c8b870 15986 drm_modeset_acquire_init(&ctx, 0);
043e9bda 15987
e2c8b870
ML
15988retry:
15989 ret = drm_modeset_lock_all_ctx(dev, &ctx);
043e9bda 15990
e2c8b870
ML
15991 if (ret == 0 && !setup) {
15992 setup = true;
043e9bda 15993
e2c8b870
ML
15994 intel_modeset_setup_hw_state(dev);
15995 i915_redisable_vga(dev);
45e2b5f6 15996 }
8af6cf88 15997
e2c8b870
ML
15998 if (ret == 0 && state) {
15999 struct drm_crtc_state *crtc_state;
16000 struct drm_crtc *crtc;
16001 int i;
043e9bda 16002
e2c8b870
ML
16003 state->acquire_ctx = &ctx;
16004
16005 for_each_crtc_in_state(state, crtc, crtc_state, i) {
16006 /*
16007 * Force recalculation even if we restore
16008 * current state. With fast modeset this may not result
16009 * in a modeset when the state is compatible.
16010 */
16011 crtc_state->mode_changed = true;
16012 }
16013
16014 ret = drm_atomic_commit(state);
043e9bda
ML
16015 }
16016
e2c8b870
ML
16017 if (ret == -EDEADLK) {
16018 drm_modeset_backoff(&ctx);
16019 goto retry;
16020 }
043e9bda 16021
e2c8b870
ML
16022 drm_modeset_drop_locks(&ctx);
16023 drm_modeset_acquire_fini(&ctx);
ea49c9ac 16024 mutex_unlock(&dev->mode_config.mutex);
043e9bda 16025
e2c8b870
ML
16026 if (ret) {
16027 DRM_ERROR("Restoring old state failed with %i\n", ret);
16028 drm_atomic_state_free(state);
16029 }
2c7111db
CW
16030}
16031
16032void intel_modeset_gem_init(struct drm_device *dev)
16033{
484b41dd 16034 struct drm_crtc *c;
2ff8fde1 16035 struct drm_i915_gem_object *obj;
e0d6149b 16036 int ret;
484b41dd 16037
ae48434c 16038 intel_init_gt_powersave(dev);
ae48434c 16039
1833b134 16040 intel_modeset_init_hw(dev);
02e792fb
DV
16041
16042 intel_setup_overlay(dev);
484b41dd
JB
16043
16044 /*
16045 * Make sure any fbs we allocated at startup are properly
16046 * pinned & fenced. When we do the allocation it's too early
16047 * for this.
16048 */
70e1e0ec 16049 for_each_crtc(dev, c) {
2ff8fde1
MR
16050 obj = intel_fb_obj(c->primary->fb);
16051 if (obj == NULL)
484b41dd
JB
16052 continue;
16053
e0d6149b 16054 mutex_lock(&dev->struct_mutex);
3465c580
VS
16055 ret = intel_pin_and_fence_fb_obj(c->primary->fb,
16056 c->primary->state->rotation);
e0d6149b
TU
16057 mutex_unlock(&dev->struct_mutex);
16058 if (ret) {
484b41dd
JB
16059 DRM_ERROR("failed to pin boot fb on pipe %d\n",
16060 to_intel_crtc(c)->pipe);
66e514c1
DA
16061 drm_framebuffer_unreference(c->primary->fb);
16062 c->primary->fb = NULL;
36750f28 16063 c->primary->crtc = c->primary->state->crtc = NULL;
afd65eb4 16064 update_state_fb(c->primary);
36750f28 16065 c->state->plane_mask &= ~(1 << drm_plane_index(c->primary));
484b41dd
JB
16066 }
16067 }
0962c3c9
VS
16068
16069 intel_backlight_register(dev);
79e53945
JB
16070}
16071
4932e2c3
ID
16072void intel_connector_unregister(struct intel_connector *intel_connector)
16073{
16074 struct drm_connector *connector = &intel_connector->base;
16075
16076 intel_panel_destroy_backlight(connector);
34ea3d38 16077 drm_connector_unregister(connector);
4932e2c3
ID
16078}
16079
79e53945
JB
16080void intel_modeset_cleanup(struct drm_device *dev)
16081{
652c393a 16082 struct drm_i915_private *dev_priv = dev->dev_private;
19c8054c 16083 struct intel_connector *connector;
652c393a 16084
2eb5252e
ID
16085 intel_disable_gt_powersave(dev);
16086
0962c3c9
VS
16087 intel_backlight_unregister(dev);
16088
fd0c0642
DV
16089 /*
16090 * Interrupts and polling as the first thing to avoid creating havoc.
2eb5252e 16091 * Too much stuff here (turning of connectors, ...) would
fd0c0642
DV
16092 * experience fancy races otherwise.
16093 */
2aeb7d3a 16094 intel_irq_uninstall(dev_priv);
eb21b92b 16095
fd0c0642
DV
16096 /*
16097 * Due to the hpd irq storm handling the hotplug work can re-arm the
16098 * poll handlers. Hence disable polling after hpd handling is shut down.
16099 */
f87ea761 16100 drm_kms_helper_poll_fini(dev);
fd0c0642 16101
723bfd70
JB
16102 intel_unregister_dsm_handler();
16103
c937ab3e 16104 intel_fbc_global_disable(dev_priv);
69341a5e 16105
1630fe75
CW
16106 /* flush any delayed tasks or pending work */
16107 flush_scheduled_work();
16108
db31af1d 16109 /* destroy the backlight and sysfs files before encoders/connectors */
19c8054c
JN
16110 for_each_intel_connector(dev, connector)
16111 connector->unregister(connector);
d9255d57 16112
79e53945 16113 drm_mode_config_cleanup(dev);
4d7bb011
DV
16114
16115 intel_cleanup_overlay(dev);
ae48434c 16116
ae48434c 16117 intel_cleanup_gt_powersave(dev);
f5949141
DV
16118
16119 intel_teardown_gmbus(dev);
79e53945
JB
16120}
16121
f1c79df3
ZW
16122/*
16123 * Return which encoder is currently attached for connector.
16124 */
df0e9248 16125struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
79e53945 16126{
df0e9248
CW
16127 return &intel_attached_encoder(connector)->base;
16128}
f1c79df3 16129
df0e9248
CW
16130void intel_connector_attach_encoder(struct intel_connector *connector,
16131 struct intel_encoder *encoder)
16132{
16133 connector->encoder = encoder;
16134 drm_mode_connector_attach_encoder(&connector->base,
16135 &encoder->base);
79e53945 16136}
28d52043
DA
16137
16138/*
16139 * set vga decode state - true == enable VGA decode
16140 */
16141int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
16142{
16143 struct drm_i915_private *dev_priv = dev->dev_private;
a885b3cc 16144 unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
28d52043
DA
16145 u16 gmch_ctrl;
16146
75fa041d
CW
16147 if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
16148 DRM_ERROR("failed to read control word\n");
16149 return -EIO;
16150 }
16151
c0cc8a55
CW
16152 if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
16153 return 0;
16154
28d52043
DA
16155 if (state)
16156 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
16157 else
16158 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
75fa041d
CW
16159
16160 if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
16161 DRM_ERROR("failed to write control word\n");
16162 return -EIO;
16163 }
16164
28d52043
DA
16165 return 0;
16166}
c4a1d9e4 16167
c4a1d9e4 16168struct intel_display_error_state {
ff57f1b0
PZ
16169
16170 u32 power_well_driver;
16171
63b66e5b
CW
16172 int num_transcoders;
16173
c4a1d9e4
CW
16174 struct intel_cursor_error_state {
16175 u32 control;
16176 u32 position;
16177 u32 base;
16178 u32 size;
52331309 16179 } cursor[I915_MAX_PIPES];
c4a1d9e4
CW
16180
16181 struct intel_pipe_error_state {
ddf9c536 16182 bool power_domain_on;
c4a1d9e4 16183 u32 source;
f301b1e1 16184 u32 stat;
52331309 16185 } pipe[I915_MAX_PIPES];
c4a1d9e4
CW
16186
16187 struct intel_plane_error_state {
16188 u32 control;
16189 u32 stride;
16190 u32 size;
16191 u32 pos;
16192 u32 addr;
16193 u32 surface;
16194 u32 tile_offset;
52331309 16195 } plane[I915_MAX_PIPES];
63b66e5b
CW
16196
16197 struct intel_transcoder_error_state {
ddf9c536 16198 bool power_domain_on;
63b66e5b
CW
16199 enum transcoder cpu_transcoder;
16200
16201 u32 conf;
16202
16203 u32 htotal;
16204 u32 hblank;
16205 u32 hsync;
16206 u32 vtotal;
16207 u32 vblank;
16208 u32 vsync;
16209 } transcoder[4];
c4a1d9e4
CW
16210};
16211
16212struct intel_display_error_state *
16213intel_display_capture_error_state(struct drm_device *dev)
16214{
fbee40df 16215 struct drm_i915_private *dev_priv = dev->dev_private;
c4a1d9e4 16216 struct intel_display_error_state *error;
63b66e5b
CW
16217 int transcoders[] = {
16218 TRANSCODER_A,
16219 TRANSCODER_B,
16220 TRANSCODER_C,
16221 TRANSCODER_EDP,
16222 };
c4a1d9e4
CW
16223 int i;
16224
63b66e5b
CW
16225 if (INTEL_INFO(dev)->num_pipes == 0)
16226 return NULL;
16227
9d1cb914 16228 error = kzalloc(sizeof(*error), GFP_ATOMIC);
c4a1d9e4
CW
16229 if (error == NULL)
16230 return NULL;
16231
190be112 16232 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
ff57f1b0
PZ
16233 error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
16234
055e393f 16235 for_each_pipe(dev_priv, i) {
ddf9c536 16236 error->pipe[i].power_domain_on =
f458ebbc
DV
16237 __intel_display_power_is_enabled(dev_priv,
16238 POWER_DOMAIN_PIPE(i));
ddf9c536 16239 if (!error->pipe[i].power_domain_on)
9d1cb914
PZ
16240 continue;
16241
5efb3e28
VS
16242 error->cursor[i].control = I915_READ(CURCNTR(i));
16243 error->cursor[i].position = I915_READ(CURPOS(i));
16244 error->cursor[i].base = I915_READ(CURBASE(i));
c4a1d9e4
CW
16245
16246 error->plane[i].control = I915_READ(DSPCNTR(i));
16247 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
80ca378b 16248 if (INTEL_INFO(dev)->gen <= 3) {
51889b35 16249 error->plane[i].size = I915_READ(DSPSIZE(i));
80ca378b
PZ
16250 error->plane[i].pos = I915_READ(DSPPOS(i));
16251 }
ca291363
PZ
16252 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
16253 error->plane[i].addr = I915_READ(DSPADDR(i));
c4a1d9e4
CW
16254 if (INTEL_INFO(dev)->gen >= 4) {
16255 error->plane[i].surface = I915_READ(DSPSURF(i));
16256 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
16257 }
16258
c4a1d9e4 16259 error->pipe[i].source = I915_READ(PIPESRC(i));
f301b1e1 16260
3abfce77 16261 if (HAS_GMCH_DISPLAY(dev))
f301b1e1 16262 error->pipe[i].stat = I915_READ(PIPESTAT(i));
63b66e5b
CW
16263 }
16264
4d1de975 16265 /* Note: this does not include DSI transcoders. */
63b66e5b
CW
16266 error->num_transcoders = INTEL_INFO(dev)->num_pipes;
16267 if (HAS_DDI(dev_priv->dev))
16268 error->num_transcoders++; /* Account for eDP. */
16269
16270 for (i = 0; i < error->num_transcoders; i++) {
16271 enum transcoder cpu_transcoder = transcoders[i];
16272
ddf9c536 16273 error->transcoder[i].power_domain_on =
f458ebbc 16274 __intel_display_power_is_enabled(dev_priv,
38cc1daf 16275 POWER_DOMAIN_TRANSCODER(cpu_transcoder));
ddf9c536 16276 if (!error->transcoder[i].power_domain_on)
9d1cb914
PZ
16277 continue;
16278
63b66e5b
CW
16279 error->transcoder[i].cpu_transcoder = cpu_transcoder;
16280
16281 error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
16282 error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
16283 error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
16284 error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
16285 error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
16286 error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
16287 error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
c4a1d9e4
CW
16288 }
16289
16290 return error;
16291}
16292
edc3d884
MK
16293#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
16294
c4a1d9e4 16295void
edc3d884 16296intel_display_print_error_state(struct drm_i915_error_state_buf *m,
c4a1d9e4
CW
16297 struct drm_device *dev,
16298 struct intel_display_error_state *error)
16299{
055e393f 16300 struct drm_i915_private *dev_priv = dev->dev_private;
c4a1d9e4
CW
16301 int i;
16302
63b66e5b
CW
16303 if (!error)
16304 return;
16305
edc3d884 16306 err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
190be112 16307 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
edc3d884 16308 err_printf(m, "PWR_WELL_CTL2: %08x\n",
ff57f1b0 16309 error->power_well_driver);
055e393f 16310 for_each_pipe(dev_priv, i) {
edc3d884 16311 err_printf(m, "Pipe [%d]:\n", i);
ddf9c536 16312 err_printf(m, " Power: %s\n",
87ad3212 16313 onoff(error->pipe[i].power_domain_on));
edc3d884 16314 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
f301b1e1 16315 err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
edc3d884
MK
16316
16317 err_printf(m, "Plane [%d]:\n", i);
16318 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
16319 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
80ca378b 16320 if (INTEL_INFO(dev)->gen <= 3) {
edc3d884
MK
16321 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
16322 err_printf(m, " POS: %08x\n", error->plane[i].pos);
80ca378b 16323 }
4b71a570 16324 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
edc3d884 16325 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
c4a1d9e4 16326 if (INTEL_INFO(dev)->gen >= 4) {
edc3d884
MK
16327 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
16328 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
c4a1d9e4
CW
16329 }
16330
edc3d884
MK
16331 err_printf(m, "Cursor [%d]:\n", i);
16332 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
16333 err_printf(m, " POS: %08x\n", error->cursor[i].position);
16334 err_printf(m, " BASE: %08x\n", error->cursor[i].base);
c4a1d9e4 16335 }
63b66e5b
CW
16336
16337 for (i = 0; i < error->num_transcoders; i++) {
da205630 16338 err_printf(m, "CPU transcoder: %s\n",
63b66e5b 16339 transcoder_name(error->transcoder[i].cpu_transcoder));
ddf9c536 16340 err_printf(m, " Power: %s\n",
87ad3212 16341 onoff(error->transcoder[i].power_domain_on));
63b66e5b
CW
16342 err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
16343 err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
16344 err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
16345 err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
16346 err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
16347 err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
16348 err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
16349 }
c4a1d9e4 16350}