drm/i915: abstract get config for cpu transcoder
[deliverable/linux.git] / drivers / gpu / drm / i915 / intel_display.c
CommitLineData
79e53945
JB
1/*
2 * Copyright © 2006-2007 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 *
23 * Authors:
24 * Eric Anholt <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
ML
4902 int pipe = intel_crtc->pipe, hsw_workaround_pipe;
4903 struct intel_crtc_state *pipe_config =
4904 to_intel_crtc_state(crtc->state);
4f771f10 4905
53d9f4e9 4906 if (WARN_ON(intel_crtc->active))
4f771f10
PZ
4907 return;
4908
81b088ca
VS
4909 if (intel_crtc->config->has_pch_encoder)
4910 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
4911 false);
4912
8106ddbd 4913 if (intel_crtc->config->shared_dpll)
df8ad70c
DV
4914 intel_enable_shared_dpll(intel_crtc);
4915
6e3c9717 4916 if (intel_crtc->config->has_dp_encoder)
fe3cd48d 4917 intel_dp_set_m_n(intel_crtc, M1_N1);
229fca97
DV
4918
4919 intel_set_pipe_timings(intel_crtc);
bc58be60 4920 intel_set_pipe_src_size(intel_crtc);
229fca97 4921
6e3c9717
ACO
4922 if (intel_crtc->config->cpu_transcoder != TRANSCODER_EDP) {
4923 I915_WRITE(PIPE_MULT(intel_crtc->config->cpu_transcoder),
4924 intel_crtc->config->pixel_multiplier - 1);
ebb69c95
CT
4925 }
4926
6e3c9717 4927 if (intel_crtc->config->has_pch_encoder) {
229fca97 4928 intel_cpu_transcoder_set_m_n(intel_crtc,
6e3c9717 4929 &intel_crtc->config->fdi_m_n, NULL);
229fca97
DV
4930 }
4931
4932 haswell_set_pipeconf(crtc);
391bf048
JN
4933 haswell_set_pipe_gamma(crtc);
4934 haswell_set_pipemisc(crtc);
229fca97
DV
4935
4936 intel_set_pipe_csc(crtc);
4937
4f771f10 4938 intel_crtc->active = true;
8664281b 4939
6b698516
DV
4940 if (intel_crtc->config->has_pch_encoder)
4941 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
4942 else
4943 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4944
7d4aefd0 4945 for_each_encoder_on_crtc(dev, crtc, encoder) {
4f771f10
PZ
4946 if (encoder->pre_enable)
4947 encoder->pre_enable(encoder);
7d4aefd0 4948 }
4f771f10 4949
d2d65408 4950 if (intel_crtc->config->has_pch_encoder)
4fe9467d 4951 dev_priv->display.fdi_link_train(crtc);
4fe9467d 4952
a65347ba 4953 if (!intel_crtc->config->has_dsi_encoder)
7d4aefd0 4954 intel_ddi_enable_pipe_clock(intel_crtc);
4f771f10 4955
1c132b44 4956 if (INTEL_INFO(dev)->gen >= 9)
e435d6e5 4957 skylake_pfit_enable(intel_crtc);
ff6d9f55 4958 else
1c132b44 4959 ironlake_pfit_enable(intel_crtc);
4f771f10
PZ
4960
4961 /*
4962 * On ILK+ LUT must be loaded before the pipe is running but with
4963 * clocks enabled
4964 */
4965 intel_crtc_load_lut(crtc);
4966
1f544388 4967 intel_ddi_set_pipe_settings(crtc);
a65347ba 4968 if (!intel_crtc->config->has_dsi_encoder)
7d4aefd0 4969 intel_ddi_enable_transcoder_func(crtc);
4f771f10 4970
1d5bf5d9
ID
4971 if (dev_priv->display.initial_watermarks != NULL)
4972 dev_priv->display.initial_watermarks(pipe_config);
4973 else
4974 intel_update_watermarks(crtc);
e1fdc473 4975 intel_enable_pipe(intel_crtc);
42db64ef 4976
6e3c9717 4977 if (intel_crtc->config->has_pch_encoder)
1507e5bd 4978 lpt_pch_enable(crtc);
4f771f10 4979
a65347ba 4980 if (intel_crtc->config->dp_encoder_is_mst)
0e32b39c
DA
4981 intel_ddi_set_vc_payload_alloc(crtc, true);
4982
f9b61ff6
DV
4983 assert_vblank_disabled(crtc);
4984 drm_crtc_vblank_on(crtc);
4985
8807e55b 4986 for_each_encoder_on_crtc(dev, crtc, encoder) {
4f771f10 4987 encoder->enable(encoder);
8807e55b
JN
4988 intel_opregion_notify_encoder(encoder, true);
4989 }
4f771f10 4990
6b698516
DV
4991 if (intel_crtc->config->has_pch_encoder) {
4992 intel_wait_for_vblank(dev, pipe);
4993 intel_wait_for_vblank(dev, pipe);
4994 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
d2d65408
VS
4995 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
4996 true);
6b698516 4997 }
d2d65408 4998
e4916946
PZ
4999 /* If we change the relative order between pipe/planes enabling, we need
5000 * to change the workaround. */
99d736a2
ML
5001 hsw_workaround_pipe = pipe_config->hsw_workaround_pipe;
5002 if (IS_HASWELL(dev) && hsw_workaround_pipe != INVALID_PIPE) {
5003 intel_wait_for_vblank(dev, hsw_workaround_pipe);
5004 intel_wait_for_vblank(dev, hsw_workaround_pipe);
5005 }
4f771f10
PZ
5006}
5007
bfd16b2a 5008static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force)
3f8dce3a
DV
5009{
5010 struct drm_device *dev = crtc->base.dev;
5011 struct drm_i915_private *dev_priv = dev->dev_private;
5012 int pipe = crtc->pipe;
5013
5014 /* To avoid upsetting the power well on haswell only disable the pfit if
5015 * it's in use. The hw state code will make sure we get this right. */
bfd16b2a 5016 if (force || crtc->config->pch_pfit.enabled) {
3f8dce3a
DV
5017 I915_WRITE(PF_CTL(pipe), 0);
5018 I915_WRITE(PF_WIN_POS(pipe), 0);
5019 I915_WRITE(PF_WIN_SZ(pipe), 0);
5020 }
5021}
5022
6be4a607
JB
5023static void ironlake_crtc_disable(struct drm_crtc *crtc)
5024{
5025 struct drm_device *dev = crtc->dev;
5026 struct drm_i915_private *dev_priv = dev->dev_private;
5027 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 5028 struct intel_encoder *encoder;
6be4a607 5029 int pipe = intel_crtc->pipe;
b52eb4dc 5030
37ca8d4c
VS
5031 if (intel_crtc->config->has_pch_encoder)
5032 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
5033
ea9d758d
DV
5034 for_each_encoder_on_crtc(dev, crtc, encoder)
5035 encoder->disable(encoder);
5036
f9b61ff6
DV
5037 drm_crtc_vblank_off(crtc);
5038 assert_vblank_disabled(crtc);
5039
3860b2ec
VS
5040 /*
5041 * Sometimes spurious CPU pipe underruns happen when the
5042 * pipe is already disabled, but FDI RX/TX is still enabled.
5043 * Happens at least with VGA+HDMI cloning. Suppress them.
5044 */
5045 if (intel_crtc->config->has_pch_encoder)
5046 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
5047
575f7ab7 5048 intel_disable_pipe(intel_crtc);
32f9d658 5049
bfd16b2a 5050 ironlake_pfit_disable(intel_crtc, false);
2c07245f 5051
3860b2ec 5052 if (intel_crtc->config->has_pch_encoder) {
5a74f70a 5053 ironlake_fdi_disable(crtc);
3860b2ec
VS
5054 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
5055 }
5a74f70a 5056
bf49ec8c
DV
5057 for_each_encoder_on_crtc(dev, crtc, encoder)
5058 if (encoder->post_disable)
5059 encoder->post_disable(encoder);
2c07245f 5060
6e3c9717 5061 if (intel_crtc->config->has_pch_encoder) {
d925c59a 5062 ironlake_disable_pch_transcoder(dev_priv, pipe);
6be4a607 5063
d925c59a 5064 if (HAS_PCH_CPT(dev)) {
f0f59a00
VS
5065 i915_reg_t reg;
5066 u32 temp;
5067
d925c59a
DV
5068 /* disable TRANS_DP_CTL */
5069 reg = TRANS_DP_CTL(pipe);
5070 temp = I915_READ(reg);
5071 temp &= ~(TRANS_DP_OUTPUT_ENABLE |
5072 TRANS_DP_PORT_SEL_MASK);
5073 temp |= TRANS_DP_PORT_SEL_NONE;
5074 I915_WRITE(reg, temp);
5075
5076 /* disable DPLL_SEL */
5077 temp = I915_READ(PCH_DPLL_SEL);
11887397 5078 temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
d925c59a 5079 I915_WRITE(PCH_DPLL_SEL, temp);
9db4a9c7 5080 }
e3421a18 5081
d925c59a
DV
5082 ironlake_fdi_pll_disable(intel_crtc);
5083 }
81b088ca
VS
5084
5085 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
6be4a607 5086}
1b3c7a47 5087
4f771f10 5088static void haswell_crtc_disable(struct drm_crtc *crtc)
ee7b9f93 5089{
4f771f10
PZ
5090 struct drm_device *dev = crtc->dev;
5091 struct drm_i915_private *dev_priv = dev->dev_private;
ee7b9f93 5092 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4f771f10 5093 struct intel_encoder *encoder;
6e3c9717 5094 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
ee7b9f93 5095
d2d65408
VS
5096 if (intel_crtc->config->has_pch_encoder)
5097 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5098 false);
5099
8807e55b
JN
5100 for_each_encoder_on_crtc(dev, crtc, encoder) {
5101 intel_opregion_notify_encoder(encoder, false);
4f771f10 5102 encoder->disable(encoder);
8807e55b 5103 }
4f771f10 5104
f9b61ff6
DV
5105 drm_crtc_vblank_off(crtc);
5106 assert_vblank_disabled(crtc);
5107
575f7ab7 5108 intel_disable_pipe(intel_crtc);
4f771f10 5109
6e3c9717 5110 if (intel_crtc->config->dp_encoder_is_mst)
a4bf214f
VS
5111 intel_ddi_set_vc_payload_alloc(crtc, false);
5112
a65347ba 5113 if (!intel_crtc->config->has_dsi_encoder)
7d4aefd0 5114 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
4f771f10 5115
1c132b44 5116 if (INTEL_INFO(dev)->gen >= 9)
e435d6e5 5117 skylake_scaler_disable(intel_crtc);
ff6d9f55 5118 else
bfd16b2a 5119 ironlake_pfit_disable(intel_crtc, false);
4f771f10 5120
a65347ba 5121 if (!intel_crtc->config->has_dsi_encoder)
7d4aefd0 5122 intel_ddi_disable_pipe_clock(intel_crtc);
4f771f10 5123
97b040aa
ID
5124 for_each_encoder_on_crtc(dev, crtc, encoder)
5125 if (encoder->post_disable)
5126 encoder->post_disable(encoder);
81b088ca 5127
92966a37
VS
5128 if (intel_crtc->config->has_pch_encoder) {
5129 lpt_disable_pch_transcoder(dev_priv);
503a74e9 5130 lpt_disable_iclkip(dev_priv);
92966a37
VS
5131 intel_ddi_fdi_disable(crtc);
5132
81b088ca
VS
5133 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5134 true);
92966a37 5135 }
4f771f10
PZ
5136}
5137
2dd24552
JB
5138static void i9xx_pfit_enable(struct intel_crtc *crtc)
5139{
5140 struct drm_device *dev = crtc->base.dev;
5141 struct drm_i915_private *dev_priv = dev->dev_private;
6e3c9717 5142 struct intel_crtc_state *pipe_config = crtc->config;
2dd24552 5143
681a8504 5144 if (!pipe_config->gmch_pfit.control)
2dd24552
JB
5145 return;
5146
2dd24552 5147 /*
c0b03411
DV
5148 * The panel fitter should only be adjusted whilst the pipe is disabled,
5149 * according to register description and PRM.
2dd24552 5150 */
c0b03411
DV
5151 WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
5152 assert_pipe_disabled(dev_priv, crtc->pipe);
2dd24552 5153
b074cec8
JB
5154 I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
5155 I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
5a80c45c
DV
5156
5157 /* Border color in case we don't scale up to the full screen. Black by
5158 * default, change to something else for debugging. */
5159 I915_WRITE(BCLRPAT(crtc->pipe), 0);
2dd24552
JB
5160}
5161
d05410f9
DA
5162static enum intel_display_power_domain port_to_power_domain(enum port port)
5163{
5164 switch (port) {
5165 case PORT_A:
6331a704 5166 return POWER_DOMAIN_PORT_DDI_A_LANES;
d05410f9 5167 case PORT_B:
6331a704 5168 return POWER_DOMAIN_PORT_DDI_B_LANES;
d05410f9 5169 case PORT_C:
6331a704 5170 return POWER_DOMAIN_PORT_DDI_C_LANES;
d05410f9 5171 case PORT_D:
6331a704 5172 return POWER_DOMAIN_PORT_DDI_D_LANES;
d8e19f99 5173 case PORT_E:
6331a704 5174 return POWER_DOMAIN_PORT_DDI_E_LANES;
d05410f9 5175 default:
b9fec167 5176 MISSING_CASE(port);
d05410f9
DA
5177 return POWER_DOMAIN_PORT_OTHER;
5178 }
5179}
5180
25f78f58
VS
5181static enum intel_display_power_domain port_to_aux_power_domain(enum port port)
5182{
5183 switch (port) {
5184 case PORT_A:
5185 return POWER_DOMAIN_AUX_A;
5186 case PORT_B:
5187 return POWER_DOMAIN_AUX_B;
5188 case PORT_C:
5189 return POWER_DOMAIN_AUX_C;
5190 case PORT_D:
5191 return POWER_DOMAIN_AUX_D;
5192 case PORT_E:
5193 /* FIXME: Check VBT for actual wiring of PORT E */
5194 return POWER_DOMAIN_AUX_D;
5195 default:
b9fec167 5196 MISSING_CASE(port);
25f78f58
VS
5197 return POWER_DOMAIN_AUX_A;
5198 }
5199}
5200
319be8ae
ID
5201enum intel_display_power_domain
5202intel_display_port_power_domain(struct intel_encoder *intel_encoder)
5203{
5204 struct drm_device *dev = intel_encoder->base.dev;
5205 struct intel_digital_port *intel_dig_port;
5206
5207 switch (intel_encoder->type) {
5208 case INTEL_OUTPUT_UNKNOWN:
5209 /* Only DDI platforms should ever use this output type */
5210 WARN_ON_ONCE(!HAS_DDI(dev));
5211 case INTEL_OUTPUT_DISPLAYPORT:
5212 case INTEL_OUTPUT_HDMI:
5213 case INTEL_OUTPUT_EDP:
5214 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
d05410f9 5215 return port_to_power_domain(intel_dig_port->port);
0e32b39c
DA
5216 case INTEL_OUTPUT_DP_MST:
5217 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
5218 return port_to_power_domain(intel_dig_port->port);
319be8ae
ID
5219 case INTEL_OUTPUT_ANALOG:
5220 return POWER_DOMAIN_PORT_CRT;
5221 case INTEL_OUTPUT_DSI:
5222 return POWER_DOMAIN_PORT_DSI;
5223 default:
5224 return POWER_DOMAIN_PORT_OTHER;
5225 }
5226}
5227
25f78f58
VS
5228enum intel_display_power_domain
5229intel_display_port_aux_power_domain(struct intel_encoder *intel_encoder)
5230{
5231 struct drm_device *dev = intel_encoder->base.dev;
5232 struct intel_digital_port *intel_dig_port;
5233
5234 switch (intel_encoder->type) {
5235 case INTEL_OUTPUT_UNKNOWN:
651174a4
ID
5236 case INTEL_OUTPUT_HDMI:
5237 /*
5238 * Only DDI platforms should ever use these output types.
5239 * We can get here after the HDMI detect code has already set
5240 * the type of the shared encoder. Since we can't be sure
5241 * what's the status of the given connectors, play safe and
5242 * run the DP detection too.
5243 */
25f78f58
VS
5244 WARN_ON_ONCE(!HAS_DDI(dev));
5245 case INTEL_OUTPUT_DISPLAYPORT:
5246 case INTEL_OUTPUT_EDP:
5247 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
5248 return port_to_aux_power_domain(intel_dig_port->port);
5249 case INTEL_OUTPUT_DP_MST:
5250 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
5251 return port_to_aux_power_domain(intel_dig_port->port);
5252 default:
b9fec167 5253 MISSING_CASE(intel_encoder->type);
25f78f58
VS
5254 return POWER_DOMAIN_AUX_A;
5255 }
5256}
5257
74bff5f9
ML
5258static unsigned long get_crtc_power_domains(struct drm_crtc *crtc,
5259 struct intel_crtc_state *crtc_state)
77d22dca 5260{
319be8ae 5261 struct drm_device *dev = crtc->dev;
74bff5f9 5262 struct drm_encoder *encoder;
319be8ae
ID
5263 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5264 enum pipe pipe = intel_crtc->pipe;
77d22dca 5265 unsigned long mask;
74bff5f9 5266 enum transcoder transcoder = crtc_state->cpu_transcoder;
77d22dca 5267
74bff5f9 5268 if (!crtc_state->base.active)
292b990e
ML
5269 return 0;
5270
77d22dca
ID
5271 mask = BIT(POWER_DOMAIN_PIPE(pipe));
5272 mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
74bff5f9
ML
5273 if (crtc_state->pch_pfit.enabled ||
5274 crtc_state->pch_pfit.force_thru)
77d22dca
ID
5275 mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
5276
74bff5f9
ML
5277 drm_for_each_encoder_mask(encoder, dev, crtc_state->base.encoder_mask) {
5278 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
5279
319be8ae 5280 mask |= BIT(intel_display_port_power_domain(intel_encoder));
74bff5f9 5281 }
319be8ae 5282
15e7ec29
ML
5283 if (crtc_state->shared_dpll)
5284 mask |= BIT(POWER_DOMAIN_PLLS);
5285
77d22dca
ID
5286 return mask;
5287}
5288
74bff5f9
ML
5289static unsigned long
5290modeset_get_crtc_power_domains(struct drm_crtc *crtc,
5291 struct intel_crtc_state *crtc_state)
77d22dca 5292{
292b990e
ML
5293 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
5294 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5295 enum intel_display_power_domain domain;
5296 unsigned long domains, new_domains, old_domains;
77d22dca 5297
292b990e 5298 old_domains = intel_crtc->enabled_power_domains;
74bff5f9
ML
5299 intel_crtc->enabled_power_domains = new_domains =
5300 get_crtc_power_domains(crtc, crtc_state);
77d22dca 5301
292b990e
ML
5302 domains = new_domains & ~old_domains;
5303
5304 for_each_power_domain(domain, domains)
5305 intel_display_power_get(dev_priv, domain);
5306
5307 return old_domains & ~new_domains;
5308}
5309
5310static void modeset_put_power_domains(struct drm_i915_private *dev_priv,
5311 unsigned long domains)
5312{
5313 enum intel_display_power_domain domain;
5314
5315 for_each_power_domain(domain, domains)
5316 intel_display_power_put(dev_priv, domain);
5317}
77d22dca 5318
adafdc6f
MK
5319static int intel_compute_max_dotclk(struct drm_i915_private *dev_priv)
5320{
5321 int max_cdclk_freq = dev_priv->max_cdclk_freq;
5322
5323 if (INTEL_INFO(dev_priv)->gen >= 9 ||
5324 IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
5325 return max_cdclk_freq;
5326 else if (IS_CHERRYVIEW(dev_priv))
5327 return max_cdclk_freq*95/100;
5328 else if (INTEL_INFO(dev_priv)->gen < 4)
5329 return 2*max_cdclk_freq*90/100;
5330 else
5331 return max_cdclk_freq*90/100;
5332}
5333
560a7ae4
DL
5334static void intel_update_max_cdclk(struct drm_device *dev)
5335{
5336 struct drm_i915_private *dev_priv = dev->dev_private;
5337
ef11bdb3 5338 if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) {
560a7ae4
DL
5339 u32 limit = I915_READ(SKL_DFSM) & SKL_DFSM_CDCLK_LIMIT_MASK;
5340
5341 if (limit == SKL_DFSM_CDCLK_LIMIT_675)
5342 dev_priv->max_cdclk_freq = 675000;
5343 else if (limit == SKL_DFSM_CDCLK_LIMIT_540)
5344 dev_priv->max_cdclk_freq = 540000;
5345 else if (limit == SKL_DFSM_CDCLK_LIMIT_450)
5346 dev_priv->max_cdclk_freq = 450000;
5347 else
5348 dev_priv->max_cdclk_freq = 337500;
5349 } else if (IS_BROADWELL(dev)) {
5350 /*
5351 * FIXME with extra cooling we can allow
5352 * 540 MHz for ULX and 675 Mhz for ULT.
5353 * How can we know if extra cooling is
5354 * available? PCI ID, VTB, something else?
5355 */
5356 if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
5357 dev_priv->max_cdclk_freq = 450000;
5358 else if (IS_BDW_ULX(dev))
5359 dev_priv->max_cdclk_freq = 450000;
5360 else if (IS_BDW_ULT(dev))
5361 dev_priv->max_cdclk_freq = 540000;
5362 else
5363 dev_priv->max_cdclk_freq = 675000;
0904deaf
MK
5364 } else if (IS_CHERRYVIEW(dev)) {
5365 dev_priv->max_cdclk_freq = 320000;
560a7ae4
DL
5366 } else if (IS_VALLEYVIEW(dev)) {
5367 dev_priv->max_cdclk_freq = 400000;
5368 } else {
5369 /* otherwise assume cdclk is fixed */
5370 dev_priv->max_cdclk_freq = dev_priv->cdclk_freq;
5371 }
5372
adafdc6f
MK
5373 dev_priv->max_dotclk_freq = intel_compute_max_dotclk(dev_priv);
5374
560a7ae4
DL
5375 DRM_DEBUG_DRIVER("Max CD clock rate: %d kHz\n",
5376 dev_priv->max_cdclk_freq);
adafdc6f
MK
5377
5378 DRM_DEBUG_DRIVER("Max dotclock rate: %d kHz\n",
5379 dev_priv->max_dotclk_freq);
560a7ae4
DL
5380}
5381
5382static void intel_update_cdclk(struct drm_device *dev)
5383{
5384 struct drm_i915_private *dev_priv = dev->dev_private;
5385
5386 dev_priv->cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
5387 DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
5388 dev_priv->cdclk_freq);
5389
5390 /*
5391 * Program the gmbus_freq based on the cdclk frequency.
5392 * BSpec erroneously claims we should aim for 4MHz, but
5393 * in fact 1MHz is the correct frequency.
5394 */
666a4537 5395 if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
560a7ae4
DL
5396 /*
5397 * Program the gmbus_freq based on the cdclk frequency.
5398 * BSpec erroneously claims we should aim for 4MHz, but
5399 * in fact 1MHz is the correct frequency.
5400 */
5401 I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->cdclk_freq, 1000));
5402 }
5403
5404 if (dev_priv->max_cdclk_freq == 0)
5405 intel_update_max_cdclk(dev);
5406}
5407
70d0c574 5408static void broxton_set_cdclk(struct drm_device *dev, int frequency)
f8437dd1
VK
5409{
5410 struct drm_i915_private *dev_priv = dev->dev_private;
5411 uint32_t divider;
5412 uint32_t ratio;
5413 uint32_t current_freq;
5414 int ret;
5415
5416 /* frequency = 19.2MHz * ratio / 2 / div{1,1.5,2,4} */
5417 switch (frequency) {
5418 case 144000:
5419 divider = BXT_CDCLK_CD2X_DIV_SEL_4;
5420 ratio = BXT_DE_PLL_RATIO(60);
5421 break;
5422 case 288000:
5423 divider = BXT_CDCLK_CD2X_DIV_SEL_2;
5424 ratio = BXT_DE_PLL_RATIO(60);
5425 break;
5426 case 384000:
5427 divider = BXT_CDCLK_CD2X_DIV_SEL_1_5;
5428 ratio = BXT_DE_PLL_RATIO(60);
5429 break;
5430 case 576000:
5431 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
5432 ratio = BXT_DE_PLL_RATIO(60);
5433 break;
5434 case 624000:
5435 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
5436 ratio = BXT_DE_PLL_RATIO(65);
5437 break;
5438 case 19200:
5439 /*
5440 * Bypass frequency with DE PLL disabled. Init ratio, divider
5441 * to suppress GCC warning.
5442 */
5443 ratio = 0;
5444 divider = 0;
5445 break;
5446 default:
5447 DRM_ERROR("unsupported CDCLK freq %d", frequency);
5448
5449 return;
5450 }
5451
5452 mutex_lock(&dev_priv->rps.hw_lock);
5453 /* Inform power controller of upcoming frequency change */
5454 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
5455 0x80000000);
5456 mutex_unlock(&dev_priv->rps.hw_lock);
5457
5458 if (ret) {
5459 DRM_ERROR("PCode CDCLK freq change notify failed (err %d, freq %d)\n",
5460 ret, frequency);
5461 return;
5462 }
5463
5464 current_freq = I915_READ(CDCLK_CTL) & CDCLK_FREQ_DECIMAL_MASK;
5465 /* convert from .1 fixpoint MHz with -1MHz offset to kHz */
5466 current_freq = current_freq * 500 + 1000;
5467
5468 /*
5469 * DE PLL has to be disabled when
5470 * - setting to 19.2MHz (bypass, PLL isn't used)
5471 * - before setting to 624MHz (PLL needs toggling)
5472 * - before setting to any frequency from 624MHz (PLL needs toggling)
5473 */
5474 if (frequency == 19200 || frequency == 624000 ||
5475 current_freq == 624000) {
5476 I915_WRITE(BXT_DE_PLL_ENABLE, ~BXT_DE_PLL_PLL_ENABLE);
5477 /* Timeout 200us */
5478 if (wait_for(!(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK),
5479 1))
5480 DRM_ERROR("timout waiting for DE PLL unlock\n");
5481 }
5482
5483 if (frequency != 19200) {
5484 uint32_t val;
5485
5486 val = I915_READ(BXT_DE_PLL_CTL);
5487 val &= ~BXT_DE_PLL_RATIO_MASK;
5488 val |= ratio;
5489 I915_WRITE(BXT_DE_PLL_CTL, val);
5490
5491 I915_WRITE(BXT_DE_PLL_ENABLE, BXT_DE_PLL_PLL_ENABLE);
5492 /* Timeout 200us */
5493 if (wait_for(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK, 1))
5494 DRM_ERROR("timeout waiting for DE PLL lock\n");
5495
5496 val = I915_READ(CDCLK_CTL);
5497 val &= ~BXT_CDCLK_CD2X_DIV_SEL_MASK;
5498 val |= divider;
5499 /*
5500 * Disable SSA Precharge when CD clock frequency < 500 MHz,
5501 * enable otherwise.
5502 */
5503 val &= ~BXT_CDCLK_SSA_PRECHARGE_ENABLE;
5504 if (frequency >= 500000)
5505 val |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
5506
5507 val &= ~CDCLK_FREQ_DECIMAL_MASK;
5508 /* convert from kHz to .1 fixpoint MHz with -1MHz offset */
5509 val |= (frequency - 1000) / 500;
5510 I915_WRITE(CDCLK_CTL, val);
5511 }
5512
5513 mutex_lock(&dev_priv->rps.hw_lock);
5514 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
5515 DIV_ROUND_UP(frequency, 25000));
5516 mutex_unlock(&dev_priv->rps.hw_lock);
5517
5518 if (ret) {
5519 DRM_ERROR("PCode CDCLK freq set failed, (err %d, freq %d)\n",
5520 ret, frequency);
5521 return;
5522 }
5523
a47871bd 5524 intel_update_cdclk(dev);
f8437dd1
VK
5525}
5526
5527void broxton_init_cdclk(struct drm_device *dev)
5528{
5529 struct drm_i915_private *dev_priv = dev->dev_private;
5530 uint32_t val;
5531
5532 /*
5533 * NDE_RSTWRN_OPT RST PCH Handshake En must always be 0b on BXT
5534 * or else the reset will hang because there is no PCH to respond.
5535 * Move the handshake programming to initialization sequence.
5536 * Previously was left up to BIOS.
5537 */
5538 val = I915_READ(HSW_NDE_RSTWRN_OPT);
5539 val &= ~RESET_PCH_HANDSHAKE_ENABLE;
5540 I915_WRITE(HSW_NDE_RSTWRN_OPT, val);
5541
5542 /* Enable PG1 for cdclk */
5543 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
5544
5545 /* check if cd clock is enabled */
5546 if (I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_PLL_ENABLE) {
5547 DRM_DEBUG_KMS("Display already initialized\n");
5548 return;
5549 }
5550
5551 /*
5552 * FIXME:
5553 * - The initial CDCLK needs to be read from VBT.
5554 * Need to make this change after VBT has changes for BXT.
5555 * - check if setting the max (or any) cdclk freq is really necessary
5556 * here, it belongs to modeset time
5557 */
5558 broxton_set_cdclk(dev, 624000);
5559
5560 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
22e02c0b
VS
5561 POSTING_READ(DBUF_CTL);
5562
f8437dd1
VK
5563 udelay(10);
5564
5565 if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
5566 DRM_ERROR("DBuf power enable timeout!\n");
5567}
5568
5569void broxton_uninit_cdclk(struct drm_device *dev)
5570{
5571 struct drm_i915_private *dev_priv = dev->dev_private;
5572
5573 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
22e02c0b
VS
5574 POSTING_READ(DBUF_CTL);
5575
f8437dd1
VK
5576 udelay(10);
5577
5578 if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
5579 DRM_ERROR("DBuf power disable timeout!\n");
5580
5581 /* Set minimum (bypass) frequency, in effect turning off the DE PLL */
5582 broxton_set_cdclk(dev, 19200);
5583
5584 intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
5585}
5586
5d96d8af
DL
5587static const struct skl_cdclk_entry {
5588 unsigned int freq;
5589 unsigned int vco;
5590} skl_cdclk_frequencies[] = {
5591 { .freq = 308570, .vco = 8640 },
5592 { .freq = 337500, .vco = 8100 },
5593 { .freq = 432000, .vco = 8640 },
5594 { .freq = 450000, .vco = 8100 },
5595 { .freq = 540000, .vco = 8100 },
5596 { .freq = 617140, .vco = 8640 },
5597 { .freq = 675000, .vco = 8100 },
5598};
5599
5600static unsigned int skl_cdclk_decimal(unsigned int freq)
5601{
5602 return (freq - 1000) / 500;
5603}
5604
5605static unsigned int skl_cdclk_get_vco(unsigned int freq)
5606{
5607 unsigned int i;
5608
5609 for (i = 0; i < ARRAY_SIZE(skl_cdclk_frequencies); i++) {
5610 const struct skl_cdclk_entry *e = &skl_cdclk_frequencies[i];
5611
5612 if (e->freq == freq)
5613 return e->vco;
5614 }
5615
5616 return 8100;
5617}
5618
5619static void
5620skl_dpll0_enable(struct drm_i915_private *dev_priv, unsigned int required_vco)
5621{
5622 unsigned int min_freq;
5623 u32 val;
5624
5625 /* select the minimum CDCLK before enabling DPLL 0 */
5626 val = I915_READ(CDCLK_CTL);
5627 val &= ~CDCLK_FREQ_SEL_MASK | ~CDCLK_FREQ_DECIMAL_MASK;
5628 val |= CDCLK_FREQ_337_308;
5629
5630 if (required_vco == 8640)
5631 min_freq = 308570;
5632 else
5633 min_freq = 337500;
5634
5635 val = CDCLK_FREQ_337_308 | skl_cdclk_decimal(min_freq);
5636
5637 I915_WRITE(CDCLK_CTL, val);
5638 POSTING_READ(CDCLK_CTL);
5639
5640 /*
5641 * We always enable DPLL0 with the lowest link rate possible, but still
5642 * taking into account the VCO required to operate the eDP panel at the
5643 * desired frequency. The usual DP link rates operate with a VCO of
5644 * 8100 while the eDP 1.4 alternate link rates need a VCO of 8640.
5645 * The modeset code is responsible for the selection of the exact link
5646 * rate later on, with the constraint of choosing a frequency that
5647 * works with required_vco.
5648 */
5649 val = I915_READ(DPLL_CTRL1);
5650
5651 val &= ~(DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) | DPLL_CTRL1_SSC(SKL_DPLL0) |
5652 DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
5653 val |= DPLL_CTRL1_OVERRIDE(SKL_DPLL0);
5654 if (required_vco == 8640)
5655 val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080,
5656 SKL_DPLL0);
5657 else
5658 val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810,
5659 SKL_DPLL0);
5660
5661 I915_WRITE(DPLL_CTRL1, val);
5662 POSTING_READ(DPLL_CTRL1);
5663
5664 I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) | LCPLL_PLL_ENABLE);
5665
5666 if (wait_for(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK, 5))
5667 DRM_ERROR("DPLL0 not locked\n");
5668}
5669
5670static bool skl_cdclk_pcu_ready(struct drm_i915_private *dev_priv)
5671{
5672 int ret;
5673 u32 val;
5674
5675 /* inform PCU we want to change CDCLK */
5676 val = SKL_CDCLK_PREPARE_FOR_CHANGE;
5677 mutex_lock(&dev_priv->rps.hw_lock);
5678 ret = sandybridge_pcode_read(dev_priv, SKL_PCODE_CDCLK_CONTROL, &val);
5679 mutex_unlock(&dev_priv->rps.hw_lock);
5680
5681 return ret == 0 && (val & SKL_CDCLK_READY_FOR_CHANGE);
5682}
5683
5684static bool skl_cdclk_wait_for_pcu_ready(struct drm_i915_private *dev_priv)
5685{
5686 unsigned int i;
5687
5688 for (i = 0; i < 15; i++) {
5689 if (skl_cdclk_pcu_ready(dev_priv))
5690 return true;
5691 udelay(10);
5692 }
5693
5694 return false;
5695}
5696
5697static void skl_set_cdclk(struct drm_i915_private *dev_priv, unsigned int freq)
5698{
560a7ae4 5699 struct drm_device *dev = dev_priv->dev;
5d96d8af
DL
5700 u32 freq_select, pcu_ack;
5701
5702 DRM_DEBUG_DRIVER("Changing CDCLK to %dKHz\n", freq);
5703
5704 if (!skl_cdclk_wait_for_pcu_ready(dev_priv)) {
5705 DRM_ERROR("failed to inform PCU about cdclk change\n");
5706 return;
5707 }
5708
5709 /* set CDCLK_CTL */
5710 switch(freq) {
5711 case 450000:
5712 case 432000:
5713 freq_select = CDCLK_FREQ_450_432;
5714 pcu_ack = 1;
5715 break;
5716 case 540000:
5717 freq_select = CDCLK_FREQ_540;
5718 pcu_ack = 2;
5719 break;
5720 case 308570:
5721 case 337500:
5722 default:
5723 freq_select = CDCLK_FREQ_337_308;
5724 pcu_ack = 0;
5725 break;
5726 case 617140:
5727 case 675000:
5728 freq_select = CDCLK_FREQ_675_617;
5729 pcu_ack = 3;
5730 break;
5731 }
5732
5733 I915_WRITE(CDCLK_CTL, freq_select | skl_cdclk_decimal(freq));
5734 POSTING_READ(CDCLK_CTL);
5735
5736 /* inform PCU of the change */
5737 mutex_lock(&dev_priv->rps.hw_lock);
5738 sandybridge_pcode_write(dev_priv, SKL_PCODE_CDCLK_CONTROL, pcu_ack);
5739 mutex_unlock(&dev_priv->rps.hw_lock);
560a7ae4
DL
5740
5741 intel_update_cdclk(dev);
5d96d8af
DL
5742}
5743
5744void skl_uninit_cdclk(struct drm_i915_private *dev_priv)
5745{
5746 /* disable DBUF power */
5747 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
5748 POSTING_READ(DBUF_CTL);
5749
5750 udelay(10);
5751
5752 if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
5753 DRM_ERROR("DBuf power disable timeout\n");
5754
ab96c1ee
ID
5755 /* disable DPLL0 */
5756 I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) & ~LCPLL_PLL_ENABLE);
5757 if (wait_for(!(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK), 1))
5758 DRM_ERROR("Couldn't disable DPLL0\n");
5d96d8af
DL
5759}
5760
5761void skl_init_cdclk(struct drm_i915_private *dev_priv)
5762{
5d96d8af
DL
5763 unsigned int required_vco;
5764
39d9b85a
GW
5765 /* DPLL0 not enabled (happens on early BIOS versions) */
5766 if (!(I915_READ(LCPLL1_CTL) & LCPLL_PLL_ENABLE)) {
5767 /* enable DPLL0 */
5768 required_vco = skl_cdclk_get_vco(dev_priv->skl_boot_cdclk);
5769 skl_dpll0_enable(dev_priv, required_vco);
5d96d8af
DL
5770 }
5771
5d96d8af
DL
5772 /* set CDCLK to the frequency the BIOS chose */
5773 skl_set_cdclk(dev_priv, dev_priv->skl_boot_cdclk);
5774
5775 /* enable DBUF power */
5776 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
5777 POSTING_READ(DBUF_CTL);
5778
5779 udelay(10);
5780
5781 if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
5782 DRM_ERROR("DBuf power enable timeout\n");
5783}
5784
c73666f3
SK
5785int skl_sanitize_cdclk(struct drm_i915_private *dev_priv)
5786{
5787 uint32_t lcpll1 = I915_READ(LCPLL1_CTL);
5788 uint32_t cdctl = I915_READ(CDCLK_CTL);
5789 int freq = dev_priv->skl_boot_cdclk;
5790
f1b391a5
SK
5791 /*
5792 * check if the pre-os intialized the display
5793 * There is SWF18 scratchpad register defined which is set by the
5794 * pre-os which can be used by the OS drivers to check the status
5795 */
5796 if ((I915_READ(SWF_ILK(0x18)) & 0x00FFFFFF) == 0)
5797 goto sanitize;
5798
c73666f3
SK
5799 /* Is PLL enabled and locked ? */
5800 if (!((lcpll1 & LCPLL_PLL_ENABLE) && (lcpll1 & LCPLL_PLL_LOCK)))
5801 goto sanitize;
5802
5803 /* DPLL okay; verify the cdclock
5804 *
5805 * Noticed in some instances that the freq selection is correct but
5806 * decimal part is programmed wrong from BIOS where pre-os does not
5807 * enable display. Verify the same as well.
5808 */
5809 if (cdctl == ((cdctl & CDCLK_FREQ_SEL_MASK) | skl_cdclk_decimal(freq)))
5810 /* All well; nothing to sanitize */
5811 return false;
5812sanitize:
5813 /*
5814 * As of now initialize with max cdclk till
5815 * we get dynamic cdclk support
5816 * */
5817 dev_priv->skl_boot_cdclk = dev_priv->max_cdclk_freq;
5818 skl_init_cdclk(dev_priv);
5819
5820 /* we did have to sanitize */
5821 return true;
5822}
5823
30a970c6
JB
5824/* Adjust CDclk dividers to allow high res or save power if possible */
5825static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
5826{
5827 struct drm_i915_private *dev_priv = dev->dev_private;
5828 u32 val, cmd;
5829
164dfd28
VK
5830 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5831 != dev_priv->cdclk_freq);
d60c4473 5832
dfcab17e 5833 if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
30a970c6 5834 cmd = 2;
dfcab17e 5835 else if (cdclk == 266667)
30a970c6
JB
5836 cmd = 1;
5837 else
5838 cmd = 0;
5839
5840 mutex_lock(&dev_priv->rps.hw_lock);
5841 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5842 val &= ~DSPFREQGUAR_MASK;
5843 val |= (cmd << DSPFREQGUAR_SHIFT);
5844 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5845 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5846 DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
5847 50)) {
5848 DRM_ERROR("timed out waiting for CDclk change\n");
5849 }
5850 mutex_unlock(&dev_priv->rps.hw_lock);
5851
54433e91
VS
5852 mutex_lock(&dev_priv->sb_lock);
5853
dfcab17e 5854 if (cdclk == 400000) {
6bcda4f0 5855 u32 divider;
30a970c6 5856
6bcda4f0 5857 divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
30a970c6 5858
30a970c6
JB
5859 /* adjust cdclk divider */
5860 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
87d5d259 5861 val &= ~CCK_FREQUENCY_VALUES;
30a970c6
JB
5862 val |= divider;
5863 vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
a877e801
VS
5864
5865 if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
87d5d259 5866 CCK_FREQUENCY_STATUS) == (divider << CCK_FREQUENCY_STATUS_SHIFT),
a877e801
VS
5867 50))
5868 DRM_ERROR("timed out waiting for CDclk change\n");
30a970c6
JB
5869 }
5870
30a970c6
JB
5871 /* adjust self-refresh exit latency value */
5872 val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
5873 val &= ~0x7f;
5874
5875 /*
5876 * For high bandwidth configs, we set a higher latency in the bunit
5877 * so that the core display fetch happens in time to avoid underruns.
5878 */
dfcab17e 5879 if (cdclk == 400000)
30a970c6
JB
5880 val |= 4500 / 250; /* 4.5 usec */
5881 else
5882 val |= 3000 / 250; /* 3.0 usec */
5883 vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
54433e91 5884
a580516d 5885 mutex_unlock(&dev_priv->sb_lock);
30a970c6 5886
b6283055 5887 intel_update_cdclk(dev);
30a970c6
JB
5888}
5889
383c5a6a
VS
5890static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
5891{
5892 struct drm_i915_private *dev_priv = dev->dev_private;
5893 u32 val, cmd;
5894
164dfd28
VK
5895 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5896 != dev_priv->cdclk_freq);
383c5a6a
VS
5897
5898 switch (cdclk) {
383c5a6a
VS
5899 case 333333:
5900 case 320000:
383c5a6a 5901 case 266667:
383c5a6a 5902 case 200000:
383c5a6a
VS
5903 break;
5904 default:
5f77eeb0 5905 MISSING_CASE(cdclk);
383c5a6a
VS
5906 return;
5907 }
5908
9d0d3fda
VS
5909 /*
5910 * Specs are full of misinformation, but testing on actual
5911 * hardware has shown that we just need to write the desired
5912 * CCK divider into the Punit register.
5913 */
5914 cmd = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
5915
383c5a6a
VS
5916 mutex_lock(&dev_priv->rps.hw_lock);
5917 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5918 val &= ~DSPFREQGUAR_MASK_CHV;
5919 val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
5920 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5921 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5922 DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
5923 50)) {
5924 DRM_ERROR("timed out waiting for CDclk change\n");
5925 }
5926 mutex_unlock(&dev_priv->rps.hw_lock);
5927
b6283055 5928 intel_update_cdclk(dev);
383c5a6a
VS
5929}
5930
30a970c6
JB
5931static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
5932 int max_pixclk)
5933{
6bcda4f0 5934 int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
6cca3195 5935 int limit = IS_CHERRYVIEW(dev_priv) ? 95 : 90;
29dc7ef3 5936
30a970c6
JB
5937 /*
5938 * Really only a few cases to deal with, as only 4 CDclks are supported:
5939 * 200MHz
5940 * 267MHz
29dc7ef3 5941 * 320/333MHz (depends on HPLL freq)
6cca3195
VS
5942 * 400MHz (VLV only)
5943 * So we check to see whether we're above 90% (VLV) or 95% (CHV)
5944 * of the lower bin and adjust if needed.
e37c67a1
VS
5945 *
5946 * We seem to get an unstable or solid color picture at 200MHz.
5947 * Not sure what's wrong. For now use 200MHz only when all pipes
5948 * are off.
30a970c6 5949 */
6cca3195
VS
5950 if (!IS_CHERRYVIEW(dev_priv) &&
5951 max_pixclk > freq_320*limit/100)
dfcab17e 5952 return 400000;
6cca3195 5953 else if (max_pixclk > 266667*limit/100)
29dc7ef3 5954 return freq_320;
e37c67a1 5955 else if (max_pixclk > 0)
dfcab17e 5956 return 266667;
e37c67a1
VS
5957 else
5958 return 200000;
30a970c6
JB
5959}
5960
f8437dd1
VK
5961static int broxton_calc_cdclk(struct drm_i915_private *dev_priv,
5962 int max_pixclk)
5963{
5964 /*
5965 * FIXME:
5966 * - remove the guardband, it's not needed on BXT
5967 * - set 19.2MHz bypass frequency if there are no active pipes
5968 */
5969 if (max_pixclk > 576000*9/10)
5970 return 624000;
5971 else if (max_pixclk > 384000*9/10)
5972 return 576000;
5973 else if (max_pixclk > 288000*9/10)
5974 return 384000;
5975 else if (max_pixclk > 144000*9/10)
5976 return 288000;
5977 else
5978 return 144000;
5979}
5980
e8788cbc 5981/* Compute the max pixel clock for new configuration. */
a821fc46
ACO
5982static int intel_mode_max_pixclk(struct drm_device *dev,
5983 struct drm_atomic_state *state)
30a970c6 5984{
565602d7
ML
5985 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
5986 struct drm_i915_private *dev_priv = dev->dev_private;
5987 struct drm_crtc *crtc;
5988 struct drm_crtc_state *crtc_state;
5989 unsigned max_pixclk = 0, i;
5990 enum pipe pipe;
30a970c6 5991
565602d7
ML
5992 memcpy(intel_state->min_pixclk, dev_priv->min_pixclk,
5993 sizeof(intel_state->min_pixclk));
304603f4 5994
565602d7
ML
5995 for_each_crtc_in_state(state, crtc, crtc_state, i) {
5996 int pixclk = 0;
5997
5998 if (crtc_state->enable)
5999 pixclk = crtc_state->adjusted_mode.crtc_clock;
304603f4 6000
565602d7 6001 intel_state->min_pixclk[i] = pixclk;
30a970c6
JB
6002 }
6003
565602d7
ML
6004 for_each_pipe(dev_priv, pipe)
6005 max_pixclk = max(intel_state->min_pixclk[pipe], max_pixclk);
6006
30a970c6
JB
6007 return max_pixclk;
6008}
6009
27c329ed 6010static int valleyview_modeset_calc_cdclk(struct drm_atomic_state *state)
30a970c6 6011{
27c329ed
ML
6012 struct drm_device *dev = state->dev;
6013 struct drm_i915_private *dev_priv = dev->dev_private;
6014 int max_pixclk = intel_mode_max_pixclk(dev, state);
1a617b77
ML
6015 struct intel_atomic_state *intel_state =
6016 to_intel_atomic_state(state);
30a970c6 6017
304603f4
ACO
6018 if (max_pixclk < 0)
6019 return max_pixclk;
30a970c6 6020
1a617b77 6021 intel_state->cdclk = intel_state->dev_cdclk =
27c329ed 6022 valleyview_calc_cdclk(dev_priv, max_pixclk);
0a9ab303 6023
1a617b77
ML
6024 if (!intel_state->active_crtcs)
6025 intel_state->dev_cdclk = valleyview_calc_cdclk(dev_priv, 0);
6026
27c329ed
ML
6027 return 0;
6028}
304603f4 6029
27c329ed
ML
6030static int broxton_modeset_calc_cdclk(struct drm_atomic_state *state)
6031{
6032 struct drm_device *dev = state->dev;
6033 struct drm_i915_private *dev_priv = dev->dev_private;
6034 int max_pixclk = intel_mode_max_pixclk(dev, state);
1a617b77
ML
6035 struct intel_atomic_state *intel_state =
6036 to_intel_atomic_state(state);
85a96e7a 6037
27c329ed
ML
6038 if (max_pixclk < 0)
6039 return max_pixclk;
85a96e7a 6040
1a617b77 6041 intel_state->cdclk = intel_state->dev_cdclk =
27c329ed 6042 broxton_calc_cdclk(dev_priv, max_pixclk);
85a96e7a 6043
1a617b77
ML
6044 if (!intel_state->active_crtcs)
6045 intel_state->dev_cdclk = broxton_calc_cdclk(dev_priv, 0);
6046
27c329ed 6047 return 0;
30a970c6
JB
6048}
6049
1e69cd74
VS
6050static void vlv_program_pfi_credits(struct drm_i915_private *dev_priv)
6051{
6052 unsigned int credits, default_credits;
6053
6054 if (IS_CHERRYVIEW(dev_priv))
6055 default_credits = PFI_CREDIT(12);
6056 else
6057 default_credits = PFI_CREDIT(8);
6058
bfa7df01 6059 if (dev_priv->cdclk_freq >= dev_priv->czclk_freq) {
1e69cd74
VS
6060 /* CHV suggested value is 31 or 63 */
6061 if (IS_CHERRYVIEW(dev_priv))
fcc0008f 6062 credits = PFI_CREDIT_63;
1e69cd74
VS
6063 else
6064 credits = PFI_CREDIT(15);
6065 } else {
6066 credits = default_credits;
6067 }
6068
6069 /*
6070 * WA - write default credits before re-programming
6071 * FIXME: should we also set the resend bit here?
6072 */
6073 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
6074 default_credits);
6075
6076 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
6077 credits | PFI_CREDIT_RESEND);
6078
6079 /*
6080 * FIXME is this guaranteed to clear
6081 * immediately or should we poll for it?
6082 */
6083 WARN_ON(I915_READ(GCI_CONTROL) & PFI_CREDIT_RESEND);
6084}
6085
27c329ed 6086static void valleyview_modeset_commit_cdclk(struct drm_atomic_state *old_state)
30a970c6 6087{
a821fc46 6088 struct drm_device *dev = old_state->dev;
30a970c6 6089 struct drm_i915_private *dev_priv = dev->dev_private;
1a617b77
ML
6090 struct intel_atomic_state *old_intel_state =
6091 to_intel_atomic_state(old_state);
6092 unsigned req_cdclk = old_intel_state->dev_cdclk;
30a970c6 6093
27c329ed
ML
6094 /*
6095 * FIXME: We can end up here with all power domains off, yet
6096 * with a CDCLK frequency other than the minimum. To account
6097 * for this take the PIPE-A power domain, which covers the HW
6098 * blocks needed for the following programming. This can be
6099 * removed once it's guaranteed that we get here either with
6100 * the minimum CDCLK set, or the required power domains
6101 * enabled.
6102 */
6103 intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
738c05c0 6104
27c329ed
ML
6105 if (IS_CHERRYVIEW(dev))
6106 cherryview_set_cdclk(dev, req_cdclk);
6107 else
6108 valleyview_set_cdclk(dev, req_cdclk);
738c05c0 6109
27c329ed 6110 vlv_program_pfi_credits(dev_priv);
1e69cd74 6111
27c329ed 6112 intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
30a970c6
JB
6113}
6114
89b667f8
JB
6115static void valleyview_crtc_enable(struct drm_crtc *crtc)
6116{
6117 struct drm_device *dev = crtc->dev;
a72e4c9f 6118 struct drm_i915_private *dev_priv = to_i915(dev);
89b667f8
JB
6119 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6120 struct intel_encoder *encoder;
6121 int pipe = intel_crtc->pipe;
89b667f8 6122
53d9f4e9 6123 if (WARN_ON(intel_crtc->active))
89b667f8
JB
6124 return;
6125
6e3c9717 6126 if (intel_crtc->config->has_dp_encoder)
fe3cd48d 6127 intel_dp_set_m_n(intel_crtc, M1_N1);
5b18e57c
DV
6128
6129 intel_set_pipe_timings(intel_crtc);
bc58be60 6130 intel_set_pipe_src_size(intel_crtc);
5b18e57c 6131
c14b0485
VS
6132 if (IS_CHERRYVIEW(dev) && pipe == PIPE_B) {
6133 struct drm_i915_private *dev_priv = dev->dev_private;
6134
6135 I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
6136 I915_WRITE(CHV_CANVAS(pipe), 0);
6137 }
6138
5b18e57c
DV
6139 i9xx_set_pipeconf(intel_crtc);
6140
89b667f8 6141 intel_crtc->active = true;
89b667f8 6142
a72e4c9f 6143 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4a3436e8 6144
89b667f8
JB
6145 for_each_encoder_on_crtc(dev, crtc, encoder)
6146 if (encoder->pre_pll_enable)
6147 encoder->pre_pll_enable(encoder);
6148
a65347ba 6149 if (!intel_crtc->config->has_dsi_encoder) {
c0b4c660
VS
6150 if (IS_CHERRYVIEW(dev)) {
6151 chv_prepare_pll(intel_crtc, intel_crtc->config);
6e3c9717 6152 chv_enable_pll(intel_crtc, intel_crtc->config);
c0b4c660
VS
6153 } else {
6154 vlv_prepare_pll(intel_crtc, intel_crtc->config);
6e3c9717 6155 vlv_enable_pll(intel_crtc, intel_crtc->config);
c0b4c660 6156 }
9d556c99 6157 }
89b667f8
JB
6158
6159 for_each_encoder_on_crtc(dev, crtc, encoder)
6160 if (encoder->pre_enable)
6161 encoder->pre_enable(encoder);
6162
2dd24552
JB
6163 i9xx_pfit_enable(intel_crtc);
6164
63cbb074
VS
6165 intel_crtc_load_lut(crtc);
6166
caed361d 6167 intel_update_watermarks(crtc);
e1fdc473 6168 intel_enable_pipe(intel_crtc);
be6a6f8e 6169
4b3a9526
VS
6170 assert_vblank_disabled(crtc);
6171 drm_crtc_vblank_on(crtc);
6172
f9b61ff6
DV
6173 for_each_encoder_on_crtc(dev, crtc, encoder)
6174 encoder->enable(encoder);
89b667f8
JB
6175}
6176
f13c2ef3
DV
6177static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
6178{
6179 struct drm_device *dev = crtc->base.dev;
6180 struct drm_i915_private *dev_priv = dev->dev_private;
6181
6e3c9717
ACO
6182 I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
6183 I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
f13c2ef3
DV
6184}
6185
0b8765c6 6186static void i9xx_crtc_enable(struct drm_crtc *crtc)
79e53945
JB
6187{
6188 struct drm_device *dev = crtc->dev;
a72e4c9f 6189 struct drm_i915_private *dev_priv = to_i915(dev);
79e53945 6190 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 6191 struct intel_encoder *encoder;
79e53945 6192 int pipe = intel_crtc->pipe;
79e53945 6193
53d9f4e9 6194 if (WARN_ON(intel_crtc->active))
f7abfe8b
CW
6195 return;
6196
f13c2ef3
DV
6197 i9xx_set_pll_dividers(intel_crtc);
6198
6e3c9717 6199 if (intel_crtc->config->has_dp_encoder)
fe3cd48d 6200 intel_dp_set_m_n(intel_crtc, M1_N1);
5b18e57c
DV
6201
6202 intel_set_pipe_timings(intel_crtc);
bc58be60 6203 intel_set_pipe_src_size(intel_crtc);
5b18e57c 6204
5b18e57c
DV
6205 i9xx_set_pipeconf(intel_crtc);
6206
f7abfe8b 6207 intel_crtc->active = true;
6b383a7f 6208
4a3436e8 6209 if (!IS_GEN2(dev))
a72e4c9f 6210 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4a3436e8 6211
9d6d9f19
MK
6212 for_each_encoder_on_crtc(dev, crtc, encoder)
6213 if (encoder->pre_enable)
6214 encoder->pre_enable(encoder);
6215
f6736a1a
DV
6216 i9xx_enable_pll(intel_crtc);
6217
2dd24552
JB
6218 i9xx_pfit_enable(intel_crtc);
6219
63cbb074
VS
6220 intel_crtc_load_lut(crtc);
6221
f37fcc2a 6222 intel_update_watermarks(crtc);
e1fdc473 6223 intel_enable_pipe(intel_crtc);
be6a6f8e 6224
4b3a9526
VS
6225 assert_vblank_disabled(crtc);
6226 drm_crtc_vblank_on(crtc);
6227
f9b61ff6
DV
6228 for_each_encoder_on_crtc(dev, crtc, encoder)
6229 encoder->enable(encoder);
0b8765c6 6230}
79e53945 6231
87476d63
DV
6232static void i9xx_pfit_disable(struct intel_crtc *crtc)
6233{
6234 struct drm_device *dev = crtc->base.dev;
6235 struct drm_i915_private *dev_priv = dev->dev_private;
87476d63 6236
6e3c9717 6237 if (!crtc->config->gmch_pfit.control)
328d8e82 6238 return;
87476d63 6239
328d8e82 6240 assert_pipe_disabled(dev_priv, crtc->pipe);
87476d63 6241
328d8e82
DV
6242 DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
6243 I915_READ(PFIT_CONTROL));
6244 I915_WRITE(PFIT_CONTROL, 0);
87476d63
DV
6245}
6246
0b8765c6
JB
6247static void i9xx_crtc_disable(struct drm_crtc *crtc)
6248{
6249 struct drm_device *dev = crtc->dev;
6250 struct drm_i915_private *dev_priv = dev->dev_private;
6251 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 6252 struct intel_encoder *encoder;
0b8765c6 6253 int pipe = intel_crtc->pipe;
ef9c3aee 6254
6304cd91
VS
6255 /*
6256 * On gen2 planes are double buffered but the pipe isn't, so we must
6257 * wait for planes to fully turn off before disabling the pipe.
564ed191
ID
6258 * We also need to wait on all gmch platforms because of the
6259 * self-refresh mode constraint explained above.
6304cd91 6260 */
564ed191 6261 intel_wait_for_vblank(dev, pipe);
6304cd91 6262
4b3a9526
VS
6263 for_each_encoder_on_crtc(dev, crtc, encoder)
6264 encoder->disable(encoder);
6265
f9b61ff6
DV
6266 drm_crtc_vblank_off(crtc);
6267 assert_vblank_disabled(crtc);
6268
575f7ab7 6269 intel_disable_pipe(intel_crtc);
24a1f16d 6270
87476d63 6271 i9xx_pfit_disable(intel_crtc);
24a1f16d 6272
89b667f8
JB
6273 for_each_encoder_on_crtc(dev, crtc, encoder)
6274 if (encoder->post_disable)
6275 encoder->post_disable(encoder);
6276
a65347ba 6277 if (!intel_crtc->config->has_dsi_encoder) {
076ed3b2
CML
6278 if (IS_CHERRYVIEW(dev))
6279 chv_disable_pll(dev_priv, pipe);
6280 else if (IS_VALLEYVIEW(dev))
6281 vlv_disable_pll(dev_priv, pipe);
6282 else
1c4e0274 6283 i9xx_disable_pll(intel_crtc);
076ed3b2 6284 }
0b8765c6 6285
d6db995f
VS
6286 for_each_encoder_on_crtc(dev, crtc, encoder)
6287 if (encoder->post_pll_disable)
6288 encoder->post_pll_disable(encoder);
6289
4a3436e8 6290 if (!IS_GEN2(dev))
a72e4c9f 6291 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
0b8765c6
JB
6292}
6293
b17d48e2
ML
6294static void intel_crtc_disable_noatomic(struct drm_crtc *crtc)
6295{
842e0307 6296 struct intel_encoder *encoder;
b17d48e2
ML
6297 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6298 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
6299 enum intel_display_power_domain domain;
6300 unsigned long domains;
6301
6302 if (!intel_crtc->active)
6303 return;
6304
a539205a 6305 if (to_intel_plane_state(crtc->primary->state)->visible) {
fc32b1fd
ML
6306 WARN_ON(intel_crtc->unpin_work);
6307
2622a081 6308 intel_pre_disable_primary_noatomic(crtc);
54a41961
ML
6309
6310 intel_crtc_disable_planes(crtc, 1 << drm_plane_index(crtc->primary));
6311 to_intel_plane_state(crtc->primary->state)->visible = false;
a539205a
ML
6312 }
6313
b17d48e2 6314 dev_priv->display.crtc_disable(crtc);
842e0307
ML
6315
6316 DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was enabled, now disabled\n",
6317 crtc->base.id);
6318
6319 WARN_ON(drm_atomic_set_mode_for_crtc(crtc->state, NULL) < 0);
6320 crtc->state->active = false;
37d9078b 6321 intel_crtc->active = false;
842e0307
ML
6322 crtc->enabled = false;
6323 crtc->state->connector_mask = 0;
6324 crtc->state->encoder_mask = 0;
6325
6326 for_each_encoder_on_crtc(crtc->dev, crtc, encoder)
6327 encoder->base.crtc = NULL;
6328
58f9c0bc 6329 intel_fbc_disable(intel_crtc);
37d9078b 6330 intel_update_watermarks(crtc);
1f7457b1 6331 intel_disable_shared_dpll(intel_crtc);
b17d48e2
ML
6332
6333 domains = intel_crtc->enabled_power_domains;
6334 for_each_power_domain(domain, domains)
6335 intel_display_power_put(dev_priv, domain);
6336 intel_crtc->enabled_power_domains = 0;
565602d7
ML
6337
6338 dev_priv->active_crtcs &= ~(1 << intel_crtc->pipe);
6339 dev_priv->min_pixclk[intel_crtc->pipe] = 0;
b17d48e2
ML
6340}
6341
6b72d486
ML
6342/*
6343 * turn all crtc's off, but do not adjust state
6344 * This has to be paired with a call to intel_modeset_setup_hw_state.
6345 */
70e0bd74 6346int intel_display_suspend(struct drm_device *dev)
ee7b9f93 6347{
e2c8b870 6348 struct drm_i915_private *dev_priv = to_i915(dev);
70e0bd74 6349 struct drm_atomic_state *state;
e2c8b870 6350 int ret;
70e0bd74 6351
e2c8b870
ML
6352 state = drm_atomic_helper_suspend(dev);
6353 ret = PTR_ERR_OR_ZERO(state);
70e0bd74
ML
6354 if (ret)
6355 DRM_ERROR("Suspending crtc's failed with %i\n", ret);
e2c8b870
ML
6356 else
6357 dev_priv->modeset_restore_state = state;
70e0bd74 6358 return ret;
ee7b9f93
JB
6359}
6360
ea5b213a 6361void intel_encoder_destroy(struct drm_encoder *encoder)
7e7d76c3 6362{
4ef69c7a 6363 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
ea5b213a 6364
ea5b213a
CW
6365 drm_encoder_cleanup(encoder);
6366 kfree(intel_encoder);
7e7d76c3
JB
6367}
6368
0a91ca29
DV
6369/* Cross check the actual hw state with our own modeset state tracking (and it's
6370 * internal consistency). */
b980514c 6371static void intel_connector_check_state(struct intel_connector *connector)
79e53945 6372{
35dd3c64
ML
6373 struct drm_crtc *crtc = connector->base.state->crtc;
6374
6375 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
6376 connector->base.base.id,
6377 connector->base.name);
6378
0a91ca29 6379 if (connector->get_hw_state(connector)) {
e85376cb 6380 struct intel_encoder *encoder = connector->encoder;
35dd3c64 6381 struct drm_connector_state *conn_state = connector->base.state;
0a91ca29 6382
35dd3c64
ML
6383 I915_STATE_WARN(!crtc,
6384 "connector enabled without attached crtc\n");
0a91ca29 6385
35dd3c64
ML
6386 if (!crtc)
6387 return;
6388
6389 I915_STATE_WARN(!crtc->state->active,
6390 "connector is active, but attached crtc isn't\n");
6391
e85376cb 6392 if (!encoder || encoder->type == INTEL_OUTPUT_DP_MST)
35dd3c64
ML
6393 return;
6394
e85376cb 6395 I915_STATE_WARN(conn_state->best_encoder != &encoder->base,
35dd3c64
ML
6396 "atomic encoder doesn't match attached encoder\n");
6397
e85376cb 6398 I915_STATE_WARN(conn_state->crtc != encoder->base.crtc,
35dd3c64
ML
6399 "attached encoder crtc differs from connector crtc\n");
6400 } else {
4d688a2a
ML
6401 I915_STATE_WARN(crtc && crtc->state->active,
6402 "attached crtc is active, but connector isn't\n");
35dd3c64
ML
6403 I915_STATE_WARN(!crtc && connector->base.state->best_encoder,
6404 "best encoder set without crtc!\n");
0a91ca29 6405 }
79e53945
JB
6406}
6407
08d9bc92
ACO
6408int intel_connector_init(struct intel_connector *connector)
6409{
5350a031 6410 drm_atomic_helper_connector_reset(&connector->base);
08d9bc92 6411
5350a031 6412 if (!connector->base.state)
08d9bc92
ACO
6413 return -ENOMEM;
6414
08d9bc92
ACO
6415 return 0;
6416}
6417
6418struct intel_connector *intel_connector_alloc(void)
6419{
6420 struct intel_connector *connector;
6421
6422 connector = kzalloc(sizeof *connector, GFP_KERNEL);
6423 if (!connector)
6424 return NULL;
6425
6426 if (intel_connector_init(connector) < 0) {
6427 kfree(connector);
6428 return NULL;
6429 }
6430
6431 return connector;
6432}
6433
f0947c37
DV
6434/* Simple connector->get_hw_state implementation for encoders that support only
6435 * one connector and no cloning and hence the encoder state determines the state
6436 * of the connector. */
6437bool intel_connector_get_hw_state(struct intel_connector *connector)
ea5b213a 6438{
24929352 6439 enum pipe pipe = 0;
f0947c37 6440 struct intel_encoder *encoder = connector->encoder;
ea5b213a 6441
f0947c37 6442 return encoder->get_hw_state(encoder, &pipe);
ea5b213a
CW
6443}
6444
6d293983 6445static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
d272ddfa 6446{
6d293983
ACO
6447 if (crtc_state->base.enable && crtc_state->has_pch_encoder)
6448 return crtc_state->fdi_lanes;
d272ddfa
VS
6449
6450 return 0;
6451}
6452
6d293983 6453static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
5cec258b 6454 struct intel_crtc_state *pipe_config)
1857e1da 6455{
6d293983
ACO
6456 struct drm_atomic_state *state = pipe_config->base.state;
6457 struct intel_crtc *other_crtc;
6458 struct intel_crtc_state *other_crtc_state;
6459
1857e1da
DV
6460 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
6461 pipe_name(pipe), pipe_config->fdi_lanes);
6462 if (pipe_config->fdi_lanes > 4) {
6463 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
6464 pipe_name(pipe), pipe_config->fdi_lanes);
6d293983 6465 return -EINVAL;
1857e1da
DV
6466 }
6467
bafb6553 6468 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
1857e1da
DV
6469 if (pipe_config->fdi_lanes > 2) {
6470 DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
6471 pipe_config->fdi_lanes);
6d293983 6472 return -EINVAL;
1857e1da 6473 } else {
6d293983 6474 return 0;
1857e1da
DV
6475 }
6476 }
6477
6478 if (INTEL_INFO(dev)->num_pipes == 2)
6d293983 6479 return 0;
1857e1da
DV
6480
6481 /* Ivybridge 3 pipe is really complicated */
6482 switch (pipe) {
6483 case PIPE_A:
6d293983 6484 return 0;
1857e1da 6485 case PIPE_B:
6d293983
ACO
6486 if (pipe_config->fdi_lanes <= 2)
6487 return 0;
6488
6489 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_C));
6490 other_crtc_state =
6491 intel_atomic_get_crtc_state(state, other_crtc);
6492 if (IS_ERR(other_crtc_state))
6493 return PTR_ERR(other_crtc_state);
6494
6495 if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
1857e1da
DV
6496 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
6497 pipe_name(pipe), pipe_config->fdi_lanes);
6d293983 6498 return -EINVAL;
1857e1da 6499 }
6d293983 6500 return 0;
1857e1da 6501 case PIPE_C:
251cc67c
VS
6502 if (pipe_config->fdi_lanes > 2) {
6503 DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
6504 pipe_name(pipe), pipe_config->fdi_lanes);
6d293983 6505 return -EINVAL;
251cc67c 6506 }
6d293983
ACO
6507
6508 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_B));
6509 other_crtc_state =
6510 intel_atomic_get_crtc_state(state, other_crtc);
6511 if (IS_ERR(other_crtc_state))
6512 return PTR_ERR(other_crtc_state);
6513
6514 if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
1857e1da 6515 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
6d293983 6516 return -EINVAL;
1857e1da 6517 }
6d293983 6518 return 0;
1857e1da
DV
6519 default:
6520 BUG();
6521 }
6522}
6523
e29c22c0
DV
6524#define RETRY 1
6525static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
5cec258b 6526 struct intel_crtc_state *pipe_config)
877d48d5 6527{
1857e1da 6528 struct drm_device *dev = intel_crtc->base.dev;
7c5f93b0 6529 const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
6d293983
ACO
6530 int lane, link_bw, fdi_dotclock, ret;
6531 bool needs_recompute = false;
877d48d5 6532
e29c22c0 6533retry:
877d48d5
DV
6534 /* FDI is a binary signal running at ~2.7GHz, encoding
6535 * each output octet as 10 bits. The actual frequency
6536 * is stored as a divider into a 100MHz clock, and the
6537 * mode pixel clock is stored in units of 1KHz.
6538 * Hence the bw of each lane in terms of the mode signal
6539 * is:
6540 */
21a727b3 6541 link_bw = intel_fdi_link_freq(to_i915(dev), pipe_config);
877d48d5 6542
241bfc38 6543 fdi_dotclock = adjusted_mode->crtc_clock;
877d48d5 6544
2bd89a07 6545 lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
877d48d5
DV
6546 pipe_config->pipe_bpp);
6547
6548 pipe_config->fdi_lanes = lane;
6549
2bd89a07 6550 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
877d48d5 6551 link_bw, &pipe_config->fdi_m_n);
1857e1da 6552
e3b247da 6553 ret = ironlake_check_fdi_lanes(dev, intel_crtc->pipe, pipe_config);
6d293983 6554 if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
e29c22c0
DV
6555 pipe_config->pipe_bpp -= 2*3;
6556 DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
6557 pipe_config->pipe_bpp);
6558 needs_recompute = true;
6559 pipe_config->bw_constrained = true;
6560
6561 goto retry;
6562 }
6563
6564 if (needs_recompute)
6565 return RETRY;
6566
6d293983 6567 return ret;
877d48d5
DV
6568}
6569
8cfb3407
VS
6570static bool pipe_config_supports_ips(struct drm_i915_private *dev_priv,
6571 struct intel_crtc_state *pipe_config)
6572{
6573 if (pipe_config->pipe_bpp > 24)
6574 return false;
6575
6576 /* HSW can handle pixel rate up to cdclk? */
6577 if (IS_HASWELL(dev_priv->dev))
6578 return true;
6579
6580 /*
b432e5cf
VS
6581 * We compare against max which means we must take
6582 * the increased cdclk requirement into account when
6583 * calculating the new cdclk.
6584 *
6585 * Should measure whether using a lower cdclk w/o IPS
8cfb3407
VS
6586 */
6587 return ilk_pipe_pixel_rate(pipe_config) <=
6588 dev_priv->max_cdclk_freq * 95 / 100;
6589}
6590
42db64ef 6591static void hsw_compute_ips_config(struct intel_crtc *crtc,
5cec258b 6592 struct intel_crtc_state *pipe_config)
42db64ef 6593{
8cfb3407
VS
6594 struct drm_device *dev = crtc->base.dev;
6595 struct drm_i915_private *dev_priv = dev->dev_private;
6596
d330a953 6597 pipe_config->ips_enabled = i915.enable_ips &&
8cfb3407
VS
6598 hsw_crtc_supports_ips(crtc) &&
6599 pipe_config_supports_ips(dev_priv, pipe_config);
42db64ef
PZ
6600}
6601
39acb4aa
VS
6602static bool intel_crtc_supports_double_wide(const struct intel_crtc *crtc)
6603{
6604 const struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6605
6606 /* GDG double wide on either pipe, otherwise pipe A only */
6607 return INTEL_INFO(dev_priv)->gen < 4 &&
6608 (crtc->pipe == PIPE_A || IS_I915G(dev_priv));
6609}
6610
a43f6e0f 6611static int intel_crtc_compute_config(struct intel_crtc *crtc,
5cec258b 6612 struct intel_crtc_state *pipe_config)
79e53945 6613{
a43f6e0f 6614 struct drm_device *dev = crtc->base.dev;
8bd31e67 6615 struct drm_i915_private *dev_priv = dev->dev_private;
7c5f93b0 6616 const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
89749350 6617
ad3a4479 6618 /* FIXME should check pixel clock limits on all platforms */
cf532bb2 6619 if (INTEL_INFO(dev)->gen < 4) {
39acb4aa 6620 int clock_limit = dev_priv->max_cdclk_freq * 9 / 10;
cf532bb2
VS
6621
6622 /*
39acb4aa 6623 * Enable double wide mode when the dot clock
cf532bb2 6624 * is > 90% of the (display) core speed.
cf532bb2 6625 */
39acb4aa
VS
6626 if (intel_crtc_supports_double_wide(crtc) &&
6627 adjusted_mode->crtc_clock > clock_limit) {
ad3a4479 6628 clock_limit *= 2;
cf532bb2 6629 pipe_config->double_wide = true;
ad3a4479
VS
6630 }
6631
39acb4aa
VS
6632 if (adjusted_mode->crtc_clock > clock_limit) {
6633 DRM_DEBUG_KMS("requested pixel clock (%d kHz) too high (max: %d kHz, double wide: %s)\n",
6634 adjusted_mode->crtc_clock, clock_limit,
6635 yesno(pipe_config->double_wide));
e29c22c0 6636 return -EINVAL;
39acb4aa 6637 }
2c07245f 6638 }
89749350 6639
1d1d0e27
VS
6640 /*
6641 * Pipe horizontal size must be even in:
6642 * - DVO ganged mode
6643 * - LVDS dual channel mode
6644 * - Double wide pipe
6645 */
a93e255f 6646 if ((intel_pipe_will_have_type(pipe_config, INTEL_OUTPUT_LVDS) &&
1d1d0e27
VS
6647 intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
6648 pipe_config->pipe_src_w &= ~1;
6649
8693a824
DL
6650 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
6651 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
44f46b42
CW
6652 */
6653 if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
aad941d5 6654 adjusted_mode->crtc_hsync_start == adjusted_mode->crtc_hdisplay)
e29c22c0 6655 return -EINVAL;
44f46b42 6656
f5adf94e 6657 if (HAS_IPS(dev))
a43f6e0f
DV
6658 hsw_compute_ips_config(crtc, pipe_config);
6659
877d48d5 6660 if (pipe_config->has_pch_encoder)
a43f6e0f 6661 return ironlake_fdi_compute_config(crtc, pipe_config);
877d48d5 6662
cf5a15be 6663 return 0;
79e53945
JB
6664}
6665
1652d19e
VS
6666static int skylake_get_display_clock_speed(struct drm_device *dev)
6667{
6668 struct drm_i915_private *dev_priv = to_i915(dev);
6669 uint32_t lcpll1 = I915_READ(LCPLL1_CTL);
6670 uint32_t cdctl = I915_READ(CDCLK_CTL);
6671 uint32_t linkrate;
6672
414355a7 6673 if (!(lcpll1 & LCPLL_PLL_ENABLE))
1652d19e 6674 return 24000; /* 24MHz is the cd freq with NSSC ref */
1652d19e
VS
6675
6676 if ((cdctl & CDCLK_FREQ_SEL_MASK) == CDCLK_FREQ_540)
6677 return 540000;
6678
6679 linkrate = (I915_READ(DPLL_CTRL1) &
71cd8423 6680 DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0)) >> 1;
1652d19e 6681
71cd8423
DL
6682 if (linkrate == DPLL_CTRL1_LINK_RATE_2160 ||
6683 linkrate == DPLL_CTRL1_LINK_RATE_1080) {
1652d19e
VS
6684 /* vco 8640 */
6685 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6686 case CDCLK_FREQ_450_432:
6687 return 432000;
6688 case CDCLK_FREQ_337_308:
6689 return 308570;
6690 case CDCLK_FREQ_675_617:
6691 return 617140;
6692 default:
6693 WARN(1, "Unknown cd freq selection\n");
6694 }
6695 } else {
6696 /* vco 8100 */
6697 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6698 case CDCLK_FREQ_450_432:
6699 return 450000;
6700 case CDCLK_FREQ_337_308:
6701 return 337500;
6702 case CDCLK_FREQ_675_617:
6703 return 675000;
6704 default:
6705 WARN(1, "Unknown cd freq selection\n");
6706 }
6707 }
6708
6709 /* error case, do as if DPLL0 isn't enabled */
6710 return 24000;
6711}
6712
acd3f3d3
BP
6713static int broxton_get_display_clock_speed(struct drm_device *dev)
6714{
6715 struct drm_i915_private *dev_priv = to_i915(dev);
6716 uint32_t cdctl = I915_READ(CDCLK_CTL);
6717 uint32_t pll_ratio = I915_READ(BXT_DE_PLL_CTL) & BXT_DE_PLL_RATIO_MASK;
6718 uint32_t pll_enab = I915_READ(BXT_DE_PLL_ENABLE);
6719 int cdclk;
6720
6721 if (!(pll_enab & BXT_DE_PLL_PLL_ENABLE))
6722 return 19200;
6723
6724 cdclk = 19200 * pll_ratio / 2;
6725
6726 switch (cdctl & BXT_CDCLK_CD2X_DIV_SEL_MASK) {
6727 case BXT_CDCLK_CD2X_DIV_SEL_1:
6728 return cdclk; /* 576MHz or 624MHz */
6729 case BXT_CDCLK_CD2X_DIV_SEL_1_5:
6730 return cdclk * 2 / 3; /* 384MHz */
6731 case BXT_CDCLK_CD2X_DIV_SEL_2:
6732 return cdclk / 2; /* 288MHz */
6733 case BXT_CDCLK_CD2X_DIV_SEL_4:
6734 return cdclk / 4; /* 144MHz */
6735 }
6736
6737 /* error case, do as if DE PLL isn't enabled */
6738 return 19200;
6739}
6740
1652d19e
VS
6741static int broadwell_get_display_clock_speed(struct drm_device *dev)
6742{
6743 struct drm_i915_private *dev_priv = dev->dev_private;
6744 uint32_t lcpll = I915_READ(LCPLL_CTL);
6745 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6746
6747 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6748 return 800000;
6749 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6750 return 450000;
6751 else if (freq == LCPLL_CLK_FREQ_450)
6752 return 450000;
6753 else if (freq == LCPLL_CLK_FREQ_54O_BDW)
6754 return 540000;
6755 else if (freq == LCPLL_CLK_FREQ_337_5_BDW)
6756 return 337500;
6757 else
6758 return 675000;
6759}
6760
6761static int haswell_get_display_clock_speed(struct drm_device *dev)
6762{
6763 struct drm_i915_private *dev_priv = dev->dev_private;
6764 uint32_t lcpll = I915_READ(LCPLL_CTL);
6765 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6766
6767 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6768 return 800000;
6769 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6770 return 450000;
6771 else if (freq == LCPLL_CLK_FREQ_450)
6772 return 450000;
6773 else if (IS_HSW_ULT(dev))
6774 return 337500;
6775 else
6776 return 540000;
79e53945
JB
6777}
6778
25eb05fc
JB
6779static int valleyview_get_display_clock_speed(struct drm_device *dev)
6780{
bfa7df01
VS
6781 return vlv_get_cck_clock_hpll(to_i915(dev), "cdclk",
6782 CCK_DISPLAY_CLOCK_CONTROL);
25eb05fc
JB
6783}
6784
b37a6434
VS
6785static int ilk_get_display_clock_speed(struct drm_device *dev)
6786{
6787 return 450000;
6788}
6789
e70236a8
JB
6790static int i945_get_display_clock_speed(struct drm_device *dev)
6791{
6792 return 400000;
6793}
79e53945 6794
e70236a8 6795static int i915_get_display_clock_speed(struct drm_device *dev)
79e53945 6796{
e907f170 6797 return 333333;
e70236a8 6798}
79e53945 6799
e70236a8
JB
6800static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
6801{
6802 return 200000;
6803}
79e53945 6804
257a7ffc
DV
6805static int pnv_get_display_clock_speed(struct drm_device *dev)
6806{
6807 u16 gcfgc = 0;
6808
6809 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6810
6811 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6812 case GC_DISPLAY_CLOCK_267_MHZ_PNV:
e907f170 6813 return 266667;
257a7ffc 6814 case GC_DISPLAY_CLOCK_333_MHZ_PNV:
e907f170 6815 return 333333;
257a7ffc 6816 case GC_DISPLAY_CLOCK_444_MHZ_PNV:
e907f170 6817 return 444444;
257a7ffc
DV
6818 case GC_DISPLAY_CLOCK_200_MHZ_PNV:
6819 return 200000;
6820 default:
6821 DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
6822 case GC_DISPLAY_CLOCK_133_MHZ_PNV:
e907f170 6823 return 133333;
257a7ffc 6824 case GC_DISPLAY_CLOCK_167_MHZ_PNV:
e907f170 6825 return 166667;
257a7ffc
DV
6826 }
6827}
6828
e70236a8
JB
6829static int i915gm_get_display_clock_speed(struct drm_device *dev)
6830{
6831 u16 gcfgc = 0;
79e53945 6832
e70236a8
JB
6833 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6834
6835 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
e907f170 6836 return 133333;
e70236a8
JB
6837 else {
6838 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6839 case GC_DISPLAY_CLOCK_333_MHZ:
e907f170 6840 return 333333;
e70236a8
JB
6841 default:
6842 case GC_DISPLAY_CLOCK_190_200_MHZ:
6843 return 190000;
79e53945 6844 }
e70236a8
JB
6845 }
6846}
6847
6848static int i865_get_display_clock_speed(struct drm_device *dev)
6849{
e907f170 6850 return 266667;
e70236a8
JB
6851}
6852
1b1d2716 6853static int i85x_get_display_clock_speed(struct drm_device *dev)
e70236a8
JB
6854{
6855 u16 hpllcc = 0;
1b1d2716 6856
65cd2b3f
VS
6857 /*
6858 * 852GM/852GMV only supports 133 MHz and the HPLLCC
6859 * encoding is different :(
6860 * FIXME is this the right way to detect 852GM/852GMV?
6861 */
6862 if (dev->pdev->revision == 0x1)
6863 return 133333;
6864
1b1d2716
VS
6865 pci_bus_read_config_word(dev->pdev->bus,
6866 PCI_DEVFN(0, 3), HPLLCC, &hpllcc);
6867
e70236a8
JB
6868 /* Assume that the hardware is in the high speed state. This
6869 * should be the default.
6870 */
6871 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
6872 case GC_CLOCK_133_200:
1b1d2716 6873 case GC_CLOCK_133_200_2:
e70236a8
JB
6874 case GC_CLOCK_100_200:
6875 return 200000;
6876 case GC_CLOCK_166_250:
6877 return 250000;
6878 case GC_CLOCK_100_133:
e907f170 6879 return 133333;
1b1d2716
VS
6880 case GC_CLOCK_133_266:
6881 case GC_CLOCK_133_266_2:
6882 case GC_CLOCK_166_266:
6883 return 266667;
e70236a8 6884 }
79e53945 6885
e70236a8
JB
6886 /* Shouldn't happen */
6887 return 0;
6888}
79e53945 6889
e70236a8
JB
6890static int i830_get_display_clock_speed(struct drm_device *dev)
6891{
e907f170 6892 return 133333;
79e53945
JB
6893}
6894
34edce2f
VS
6895static unsigned int intel_hpll_vco(struct drm_device *dev)
6896{
6897 struct drm_i915_private *dev_priv = dev->dev_private;
6898 static const unsigned int blb_vco[8] = {
6899 [0] = 3200000,
6900 [1] = 4000000,
6901 [2] = 5333333,
6902 [3] = 4800000,
6903 [4] = 6400000,
6904 };
6905 static const unsigned int pnv_vco[8] = {
6906 [0] = 3200000,
6907 [1] = 4000000,
6908 [2] = 5333333,
6909 [3] = 4800000,
6910 [4] = 2666667,
6911 };
6912 static const unsigned int cl_vco[8] = {
6913 [0] = 3200000,
6914 [1] = 4000000,
6915 [2] = 5333333,
6916 [3] = 6400000,
6917 [4] = 3333333,
6918 [5] = 3566667,
6919 [6] = 4266667,
6920 };
6921 static const unsigned int elk_vco[8] = {
6922 [0] = 3200000,
6923 [1] = 4000000,
6924 [2] = 5333333,
6925 [3] = 4800000,
6926 };
6927 static const unsigned int ctg_vco[8] = {
6928 [0] = 3200000,
6929 [1] = 4000000,
6930 [2] = 5333333,
6931 [3] = 6400000,
6932 [4] = 2666667,
6933 [5] = 4266667,
6934 };
6935 const unsigned int *vco_table;
6936 unsigned int vco;
6937 uint8_t tmp = 0;
6938
6939 /* FIXME other chipsets? */
6940 if (IS_GM45(dev))
6941 vco_table = ctg_vco;
6942 else if (IS_G4X(dev))
6943 vco_table = elk_vco;
6944 else if (IS_CRESTLINE(dev))
6945 vco_table = cl_vco;
6946 else if (IS_PINEVIEW(dev))
6947 vco_table = pnv_vco;
6948 else if (IS_G33(dev))
6949 vco_table = blb_vco;
6950 else
6951 return 0;
6952
6953 tmp = I915_READ(IS_MOBILE(dev) ? HPLLVCO_MOBILE : HPLLVCO);
6954
6955 vco = vco_table[tmp & 0x7];
6956 if (vco == 0)
6957 DRM_ERROR("Bad HPLL VCO (HPLLVCO=0x%02x)\n", tmp);
6958 else
6959 DRM_DEBUG_KMS("HPLL VCO %u kHz\n", vco);
6960
6961 return vco;
6962}
6963
6964static int gm45_get_display_clock_speed(struct drm_device *dev)
6965{
6966 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
6967 uint16_t tmp = 0;
6968
6969 pci_read_config_word(dev->pdev, GCFGC, &tmp);
6970
6971 cdclk_sel = (tmp >> 12) & 0x1;
6972
6973 switch (vco) {
6974 case 2666667:
6975 case 4000000:
6976 case 5333333:
6977 return cdclk_sel ? 333333 : 222222;
6978 case 3200000:
6979 return cdclk_sel ? 320000 : 228571;
6980 default:
6981 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u, CFGC=0x%04x\n", vco, tmp);
6982 return 222222;
6983 }
6984}
6985
6986static int i965gm_get_display_clock_speed(struct drm_device *dev)
6987{
6988 static const uint8_t div_3200[] = { 16, 10, 8 };
6989 static const uint8_t div_4000[] = { 20, 12, 10 };
6990 static const uint8_t div_5333[] = { 24, 16, 14 };
6991 const uint8_t *div_table;
6992 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
6993 uint16_t tmp = 0;
6994
6995 pci_read_config_word(dev->pdev, GCFGC, &tmp);
6996
6997 cdclk_sel = ((tmp >> 8) & 0x1f) - 1;
6998
6999 if (cdclk_sel >= ARRAY_SIZE(div_3200))
7000 goto fail;
7001
7002 switch (vco) {
7003 case 3200000:
7004 div_table = div_3200;
7005 break;
7006 case 4000000:
7007 div_table = div_4000;
7008 break;
7009 case 5333333:
7010 div_table = div_5333;
7011 break;
7012 default:
7013 goto fail;
7014 }
7015
7016 return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
7017
caf4e252 7018fail:
34edce2f
VS
7019 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%04x\n", vco, tmp);
7020 return 200000;
7021}
7022
7023static int g33_get_display_clock_speed(struct drm_device *dev)
7024{
7025 static const uint8_t div_3200[] = { 12, 10, 8, 7, 5, 16 };
7026 static const uint8_t div_4000[] = { 14, 12, 10, 8, 6, 20 };
7027 static const uint8_t div_4800[] = { 20, 14, 12, 10, 8, 24 };
7028 static const uint8_t div_5333[] = { 20, 16, 12, 12, 8, 28 };
7029 const uint8_t *div_table;
7030 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
7031 uint16_t tmp = 0;
7032
7033 pci_read_config_word(dev->pdev, GCFGC, &tmp);
7034
7035 cdclk_sel = (tmp >> 4) & 0x7;
7036
7037 if (cdclk_sel >= ARRAY_SIZE(div_3200))
7038 goto fail;
7039
7040 switch (vco) {
7041 case 3200000:
7042 div_table = div_3200;
7043 break;
7044 case 4000000:
7045 div_table = div_4000;
7046 break;
7047 case 4800000:
7048 div_table = div_4800;
7049 break;
7050 case 5333333:
7051 div_table = div_5333;
7052 break;
7053 default:
7054 goto fail;
7055 }
7056
7057 return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
7058
caf4e252 7059fail:
34edce2f
VS
7060 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%08x\n", vco, tmp);
7061 return 190476;
7062}
7063
2c07245f 7064static void
a65851af 7065intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
2c07245f 7066{
a65851af
VS
7067 while (*num > DATA_LINK_M_N_MASK ||
7068 *den > DATA_LINK_M_N_MASK) {
2c07245f
ZW
7069 *num >>= 1;
7070 *den >>= 1;
7071 }
7072}
7073
a65851af
VS
7074static void compute_m_n(unsigned int m, unsigned int n,
7075 uint32_t *ret_m, uint32_t *ret_n)
7076{
7077 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
7078 *ret_m = div_u64((uint64_t) m * *ret_n, n);
7079 intel_reduce_m_n_ratio(ret_m, ret_n);
7080}
7081
e69d0bc1
DV
7082void
7083intel_link_compute_m_n(int bits_per_pixel, int nlanes,
7084 int pixel_clock, int link_clock,
7085 struct intel_link_m_n *m_n)
2c07245f 7086{
e69d0bc1 7087 m_n->tu = 64;
a65851af
VS
7088
7089 compute_m_n(bits_per_pixel * pixel_clock,
7090 link_clock * nlanes * 8,
7091 &m_n->gmch_m, &m_n->gmch_n);
7092
7093 compute_m_n(pixel_clock, link_clock,
7094 &m_n->link_m, &m_n->link_n);
2c07245f
ZW
7095}
7096
a7615030
CW
7097static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
7098{
d330a953
JN
7099 if (i915.panel_use_ssc >= 0)
7100 return i915.panel_use_ssc != 0;
41aa3448 7101 return dev_priv->vbt.lvds_use_ssc
435793df 7102 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
a7615030
CW
7103}
7104
a93e255f
ACO
7105static int i9xx_get_refclk(const struct intel_crtc_state *crtc_state,
7106 int num_connectors)
c65d77d8 7107{
a93e255f 7108 struct drm_device *dev = crtc_state->base.crtc->dev;
c65d77d8
JB
7109 struct drm_i915_private *dev_priv = dev->dev_private;
7110 int refclk;
7111
a93e255f
ACO
7112 WARN_ON(!crtc_state->base.state);
7113
666a4537 7114 if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev) || IS_BROXTON(dev)) {
9a0ea498 7115 refclk = 100000;
a93e255f 7116 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
c65d77d8 7117 intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
e91e941b
VS
7118 refclk = dev_priv->vbt.lvds_ssc_freq;
7119 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
c65d77d8
JB
7120 } else if (!IS_GEN2(dev)) {
7121 refclk = 96000;
7122 } else {
7123 refclk = 48000;
7124 }
7125
7126 return refclk;
7127}
7128
7429e9d4 7129static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
c65d77d8 7130{
7df00d7a 7131 return (1 << dpll->n) << 16 | dpll->m2;
7429e9d4 7132}
f47709a9 7133
7429e9d4
DV
7134static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
7135{
7136 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
c65d77d8
JB
7137}
7138
f47709a9 7139static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
190f68c5 7140 struct intel_crtc_state *crtc_state,
a7516a05
JB
7141 intel_clock_t *reduced_clock)
7142{
f47709a9 7143 struct drm_device *dev = crtc->base.dev;
a7516a05
JB
7144 u32 fp, fp2 = 0;
7145
7146 if (IS_PINEVIEW(dev)) {
190f68c5 7147 fp = pnv_dpll_compute_fp(&crtc_state->dpll);
a7516a05 7148 if (reduced_clock)
7429e9d4 7149 fp2 = pnv_dpll_compute_fp(reduced_clock);
a7516a05 7150 } else {
190f68c5 7151 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
a7516a05 7152 if (reduced_clock)
7429e9d4 7153 fp2 = i9xx_dpll_compute_fp(reduced_clock);
a7516a05
JB
7154 }
7155
190f68c5 7156 crtc_state->dpll_hw_state.fp0 = fp;
a7516a05 7157
f47709a9 7158 crtc->lowfreq_avail = false;
a93e255f 7159 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
ab585dea 7160 reduced_clock) {
190f68c5 7161 crtc_state->dpll_hw_state.fp1 = fp2;
f47709a9 7162 crtc->lowfreq_avail = true;
a7516a05 7163 } else {
190f68c5 7164 crtc_state->dpll_hw_state.fp1 = fp;
a7516a05
JB
7165 }
7166}
7167
5e69f97f
CML
7168static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
7169 pipe)
89b667f8
JB
7170{
7171 u32 reg_val;
7172
7173 /*
7174 * PLLB opamp always calibrates to max value of 0x3f, force enable it
7175 * and set it to a reasonable value instead.
7176 */
ab3c759a 7177 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
89b667f8
JB
7178 reg_val &= 0xffffff00;
7179 reg_val |= 0x00000030;
ab3c759a 7180 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
89b667f8 7181
ab3c759a 7182 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
89b667f8
JB
7183 reg_val &= 0x8cffffff;
7184 reg_val = 0x8c000000;
ab3c759a 7185 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
89b667f8 7186
ab3c759a 7187 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
89b667f8 7188 reg_val &= 0xffffff00;
ab3c759a 7189 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
89b667f8 7190
ab3c759a 7191 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
89b667f8
JB
7192 reg_val &= 0x00ffffff;
7193 reg_val |= 0xb0000000;
ab3c759a 7194 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
89b667f8
JB
7195}
7196
b551842d
DV
7197static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
7198 struct intel_link_m_n *m_n)
7199{
7200 struct drm_device *dev = crtc->base.dev;
7201 struct drm_i915_private *dev_priv = dev->dev_private;
7202 int pipe = crtc->pipe;
7203
e3b95f1e
DV
7204 I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7205 I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
7206 I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
7207 I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
b551842d
DV
7208}
7209
7210static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
f769cd24
VK
7211 struct intel_link_m_n *m_n,
7212 struct intel_link_m_n *m2_n2)
b551842d
DV
7213{
7214 struct drm_device *dev = crtc->base.dev;
7215 struct drm_i915_private *dev_priv = dev->dev_private;
7216 int pipe = crtc->pipe;
6e3c9717 7217 enum transcoder transcoder = crtc->config->cpu_transcoder;
b551842d
DV
7218
7219 if (INTEL_INFO(dev)->gen >= 5) {
7220 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
7221 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
7222 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
7223 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
f769cd24
VK
7224 /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
7225 * for gen < 8) and if DRRS is supported (to make sure the
7226 * registers are not unnecessarily accessed).
7227 */
44395bfe 7228 if (m2_n2 && (IS_CHERRYVIEW(dev) || INTEL_INFO(dev)->gen < 8) &&
6e3c9717 7229 crtc->config->has_drrs) {
f769cd24
VK
7230 I915_WRITE(PIPE_DATA_M2(transcoder),
7231 TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
7232 I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
7233 I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
7234 I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
7235 }
b551842d 7236 } else {
e3b95f1e
DV
7237 I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7238 I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
7239 I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
7240 I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
b551842d
DV
7241 }
7242}
7243
fe3cd48d 7244void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
03afc4a2 7245{
fe3cd48d
R
7246 struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
7247
7248 if (m_n == M1_N1) {
7249 dp_m_n = &crtc->config->dp_m_n;
7250 dp_m2_n2 = &crtc->config->dp_m2_n2;
7251 } else if (m_n == M2_N2) {
7252
7253 /*
7254 * M2_N2 registers are not supported. Hence m2_n2 divider value
7255 * needs to be programmed into M1_N1.
7256 */
7257 dp_m_n = &crtc->config->dp_m2_n2;
7258 } else {
7259 DRM_ERROR("Unsupported divider value\n");
7260 return;
7261 }
7262
6e3c9717
ACO
7263 if (crtc->config->has_pch_encoder)
7264 intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
03afc4a2 7265 else
fe3cd48d 7266 intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
03afc4a2
DV
7267}
7268
251ac862
DV
7269static void vlv_compute_dpll(struct intel_crtc *crtc,
7270 struct intel_crtc_state *pipe_config)
bdd4b6a6
DV
7271{
7272 u32 dpll, dpll_md;
7273
7274 /*
7275 * Enable DPIO clock input. We should never disable the reference
7276 * clock for pipe B, since VGA hotplug / manual detection depends
7277 * on it.
7278 */
60bfe44f
VS
7279 dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REF_CLK_ENABLE_VLV |
7280 DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_REF_CLK_VLV;
bdd4b6a6
DV
7281 /* We should never disable this, set it here for state tracking */
7282 if (crtc->pipe == PIPE_B)
7283 dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
7284 dpll |= DPLL_VCO_ENABLE;
d288f65f 7285 pipe_config->dpll_hw_state.dpll = dpll;
bdd4b6a6 7286
d288f65f 7287 dpll_md = (pipe_config->pixel_multiplier - 1)
bdd4b6a6 7288 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
d288f65f 7289 pipe_config->dpll_hw_state.dpll_md = dpll_md;
bdd4b6a6
DV
7290}
7291
d288f65f 7292static void vlv_prepare_pll(struct intel_crtc *crtc,
5cec258b 7293 const struct intel_crtc_state *pipe_config)
a0c4da24 7294{
f47709a9 7295 struct drm_device *dev = crtc->base.dev;
a0c4da24 7296 struct drm_i915_private *dev_priv = dev->dev_private;
f47709a9 7297 int pipe = crtc->pipe;
bdd4b6a6 7298 u32 mdiv;
a0c4da24 7299 u32 bestn, bestm1, bestm2, bestp1, bestp2;
bdd4b6a6 7300 u32 coreclk, reg_val;
a0c4da24 7301
a580516d 7302 mutex_lock(&dev_priv->sb_lock);
09153000 7303
d288f65f
VS
7304 bestn = pipe_config->dpll.n;
7305 bestm1 = pipe_config->dpll.m1;
7306 bestm2 = pipe_config->dpll.m2;
7307 bestp1 = pipe_config->dpll.p1;
7308 bestp2 = pipe_config->dpll.p2;
a0c4da24 7309
89b667f8
JB
7310 /* See eDP HDMI DPIO driver vbios notes doc */
7311
7312 /* PLL B needs special handling */
bdd4b6a6 7313 if (pipe == PIPE_B)
5e69f97f 7314 vlv_pllb_recal_opamp(dev_priv, pipe);
89b667f8
JB
7315
7316 /* Set up Tx target for periodic Rcomp update */
ab3c759a 7317 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
89b667f8
JB
7318
7319 /* Disable target IRef on PLL */
ab3c759a 7320 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
89b667f8 7321 reg_val &= 0x00ffffff;
ab3c759a 7322 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
89b667f8
JB
7323
7324 /* Disable fast lock */
ab3c759a 7325 vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
89b667f8
JB
7326
7327 /* Set idtafcrecal before PLL is enabled */
a0c4da24
JB
7328 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
7329 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
7330 mdiv |= ((bestn << DPIO_N_SHIFT));
a0c4da24 7331 mdiv |= (1 << DPIO_K_SHIFT);
7df5080b
JB
7332
7333 /*
7334 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
7335 * but we don't support that).
7336 * Note: don't use the DAC post divider as it seems unstable.
7337 */
7338 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
ab3c759a 7339 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
a0c4da24 7340
a0c4da24 7341 mdiv |= DPIO_ENABLE_CALIBRATION;
ab3c759a 7342 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
a0c4da24 7343
89b667f8 7344 /* Set HBR and RBR LPF coefficients */
d288f65f 7345 if (pipe_config->port_clock == 162000 ||
409ee761
ACO
7346 intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG) ||
7347 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
ab3c759a 7348 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
885b0120 7349 0x009f0003);
89b667f8 7350 else
ab3c759a 7351 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
89b667f8
JB
7352 0x00d0000f);
7353
681a8504 7354 if (pipe_config->has_dp_encoder) {
89b667f8 7355 /* Use SSC source */
bdd4b6a6 7356 if (pipe == PIPE_A)
ab3c759a 7357 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
7358 0x0df40000);
7359 else
ab3c759a 7360 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
7361 0x0df70000);
7362 } else { /* HDMI or VGA */
7363 /* Use bend source */
bdd4b6a6 7364 if (pipe == PIPE_A)
ab3c759a 7365 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
7366 0x0df70000);
7367 else
ab3c759a 7368 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
7369 0x0df40000);
7370 }
a0c4da24 7371
ab3c759a 7372 coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
89b667f8 7373 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
409ee761
ACO
7374 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
7375 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
89b667f8 7376 coreclk |= 0x01000000;
ab3c759a 7377 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
a0c4da24 7378
ab3c759a 7379 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
a580516d 7380 mutex_unlock(&dev_priv->sb_lock);
a0c4da24
JB
7381}
7382
251ac862
DV
7383static void chv_compute_dpll(struct intel_crtc *crtc,
7384 struct intel_crtc_state *pipe_config)
1ae0d137 7385{
60bfe44f
VS
7386 pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLK_CHV |
7387 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS |
1ae0d137
VS
7388 DPLL_VCO_ENABLE;
7389 if (crtc->pipe != PIPE_A)
d288f65f 7390 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
1ae0d137 7391
d288f65f
VS
7392 pipe_config->dpll_hw_state.dpll_md =
7393 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
1ae0d137
VS
7394}
7395
d288f65f 7396static void chv_prepare_pll(struct intel_crtc *crtc,
5cec258b 7397 const struct intel_crtc_state *pipe_config)
9d556c99
CML
7398{
7399 struct drm_device *dev = crtc->base.dev;
7400 struct drm_i915_private *dev_priv = dev->dev_private;
7401 int pipe = crtc->pipe;
f0f59a00 7402 i915_reg_t dpll_reg = DPLL(crtc->pipe);
9d556c99 7403 enum dpio_channel port = vlv_pipe_to_channel(pipe);
9cbe40c1 7404 u32 loopfilter, tribuf_calcntr;
9d556c99 7405 u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
a945ce7e 7406 u32 dpio_val;
9cbe40c1 7407 int vco;
9d556c99 7408
d288f65f
VS
7409 bestn = pipe_config->dpll.n;
7410 bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
7411 bestm1 = pipe_config->dpll.m1;
7412 bestm2 = pipe_config->dpll.m2 >> 22;
7413 bestp1 = pipe_config->dpll.p1;
7414 bestp2 = pipe_config->dpll.p2;
9cbe40c1 7415 vco = pipe_config->dpll.vco;
a945ce7e 7416 dpio_val = 0;
9cbe40c1 7417 loopfilter = 0;
9d556c99
CML
7418
7419 /*
7420 * Enable Refclk and SSC
7421 */
a11b0703 7422 I915_WRITE(dpll_reg,
d288f65f 7423 pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
a11b0703 7424
a580516d 7425 mutex_lock(&dev_priv->sb_lock);
9d556c99 7426
9d556c99
CML
7427 /* p1 and p2 divider */
7428 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
7429 5 << DPIO_CHV_S1_DIV_SHIFT |
7430 bestp1 << DPIO_CHV_P1_DIV_SHIFT |
7431 bestp2 << DPIO_CHV_P2_DIV_SHIFT |
7432 1 << DPIO_CHV_K_DIV_SHIFT);
7433
7434 /* Feedback post-divider - m2 */
7435 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
7436
7437 /* Feedback refclk divider - n and m1 */
7438 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
7439 DPIO_CHV_M1_DIV_BY_2 |
7440 1 << DPIO_CHV_N_DIV_SHIFT);
7441
7442 /* M2 fraction division */
25a25dfc 7443 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
9d556c99
CML
7444
7445 /* M2 fraction division enable */
a945ce7e
VP
7446 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
7447 dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
7448 dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
7449 if (bestm2_frac)
7450 dpio_val |= DPIO_CHV_FRAC_DIV_EN;
7451 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
9d556c99 7452
de3a0fde
VP
7453 /* Program digital lock detect threshold */
7454 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
7455 dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
7456 DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
7457 dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
7458 if (!bestm2_frac)
7459 dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
7460 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
7461
9d556c99 7462 /* Loop filter */
9cbe40c1
VP
7463 if (vco == 5400000) {
7464 loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
7465 loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
7466 loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
7467 tribuf_calcntr = 0x9;
7468 } else if (vco <= 6200000) {
7469 loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
7470 loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
7471 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7472 tribuf_calcntr = 0x9;
7473 } else if (vco <= 6480000) {
7474 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7475 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7476 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7477 tribuf_calcntr = 0x8;
7478 } else {
7479 /* Not supported. Apply the same limits as in the max case */
7480 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7481 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7482 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7483 tribuf_calcntr = 0;
7484 }
9d556c99
CML
7485 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
7486
968040b2 7487 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
9cbe40c1
VP
7488 dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
7489 dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
7490 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
7491
9d556c99
CML
7492 /* AFC Recal */
7493 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
7494 vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
7495 DPIO_AFC_RECAL);
7496
a580516d 7497 mutex_unlock(&dev_priv->sb_lock);
9d556c99
CML
7498}
7499
d288f65f
VS
7500/**
7501 * vlv_force_pll_on - forcibly enable just the PLL
7502 * @dev_priv: i915 private structure
7503 * @pipe: pipe PLL to enable
7504 * @dpll: PLL configuration
7505 *
7506 * Enable the PLL for @pipe using the supplied @dpll config. To be used
7507 * in cases where we need the PLL enabled even when @pipe is not going to
7508 * be enabled.
7509 */
3f36b937
TU
7510int vlv_force_pll_on(struct drm_device *dev, enum pipe pipe,
7511 const struct dpll *dpll)
d288f65f
VS
7512{
7513 struct intel_crtc *crtc =
7514 to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
3f36b937
TU
7515 struct intel_crtc_state *pipe_config;
7516
7517 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
7518 if (!pipe_config)
7519 return -ENOMEM;
7520
7521 pipe_config->base.crtc = &crtc->base;
7522 pipe_config->pixel_multiplier = 1;
7523 pipe_config->dpll = *dpll;
d288f65f
VS
7524
7525 if (IS_CHERRYVIEW(dev)) {
3f36b937
TU
7526 chv_compute_dpll(crtc, pipe_config);
7527 chv_prepare_pll(crtc, pipe_config);
7528 chv_enable_pll(crtc, pipe_config);
d288f65f 7529 } else {
3f36b937
TU
7530 vlv_compute_dpll(crtc, pipe_config);
7531 vlv_prepare_pll(crtc, pipe_config);
7532 vlv_enable_pll(crtc, pipe_config);
d288f65f 7533 }
3f36b937
TU
7534
7535 kfree(pipe_config);
7536
7537 return 0;
d288f65f
VS
7538}
7539
7540/**
7541 * vlv_force_pll_off - forcibly disable just the PLL
7542 * @dev_priv: i915 private structure
7543 * @pipe: pipe PLL to disable
7544 *
7545 * Disable the PLL for @pipe. To be used in cases where we need
7546 * the PLL enabled even when @pipe is not going to be enabled.
7547 */
7548void vlv_force_pll_off(struct drm_device *dev, enum pipe pipe)
7549{
7550 if (IS_CHERRYVIEW(dev))
7551 chv_disable_pll(to_i915(dev), pipe);
7552 else
7553 vlv_disable_pll(to_i915(dev), pipe);
7554}
7555
251ac862
DV
7556static void i9xx_compute_dpll(struct intel_crtc *crtc,
7557 struct intel_crtc_state *crtc_state,
7558 intel_clock_t *reduced_clock,
7559 int num_connectors)
eb1cbe48 7560{
f47709a9 7561 struct drm_device *dev = crtc->base.dev;
eb1cbe48 7562 struct drm_i915_private *dev_priv = dev->dev_private;
eb1cbe48
DV
7563 u32 dpll;
7564 bool is_sdvo;
190f68c5 7565 struct dpll *clock = &crtc_state->dpll;
eb1cbe48 7566
190f68c5 7567 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
2a8f64ca 7568
a93e255f
ACO
7569 is_sdvo = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO) ||
7570 intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI);
eb1cbe48
DV
7571
7572 dpll = DPLL_VGA_MODE_DIS;
7573
a93e255f 7574 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
eb1cbe48
DV
7575 dpll |= DPLLB_MODE_LVDS;
7576 else
7577 dpll |= DPLLB_MODE_DAC_SERIAL;
6cc5f341 7578
ef1b460d 7579 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
190f68c5 7580 dpll |= (crtc_state->pixel_multiplier - 1)
198a037f 7581 << SDVO_MULTIPLIER_SHIFT_HIRES;
eb1cbe48 7582 }
198a037f
DV
7583
7584 if (is_sdvo)
4a33e48d 7585 dpll |= DPLL_SDVO_HIGH_SPEED;
198a037f 7586
190f68c5 7587 if (crtc_state->has_dp_encoder)
4a33e48d 7588 dpll |= DPLL_SDVO_HIGH_SPEED;
eb1cbe48
DV
7589
7590 /* compute bitmask from p1 value */
7591 if (IS_PINEVIEW(dev))
7592 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
7593 else {
7594 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7595 if (IS_G4X(dev) && reduced_clock)
7596 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
7597 }
7598 switch (clock->p2) {
7599 case 5:
7600 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
7601 break;
7602 case 7:
7603 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
7604 break;
7605 case 10:
7606 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
7607 break;
7608 case 14:
7609 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
7610 break;
7611 }
7612 if (INTEL_INFO(dev)->gen >= 4)
7613 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
7614
190f68c5 7615 if (crtc_state->sdvo_tv_clock)
eb1cbe48 7616 dpll |= PLL_REF_INPUT_TVCLKINBC;
a93e255f 7617 else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
eb1cbe48
DV
7618 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
7619 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7620 else
7621 dpll |= PLL_REF_INPUT_DREFCLK;
7622
7623 dpll |= DPLL_VCO_ENABLE;
190f68c5 7624 crtc_state->dpll_hw_state.dpll = dpll;
8bcc2795 7625
eb1cbe48 7626 if (INTEL_INFO(dev)->gen >= 4) {
190f68c5 7627 u32 dpll_md = (crtc_state->pixel_multiplier - 1)
ef1b460d 7628 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
190f68c5 7629 crtc_state->dpll_hw_state.dpll_md = dpll_md;
eb1cbe48
DV
7630 }
7631}
7632
251ac862
DV
7633static void i8xx_compute_dpll(struct intel_crtc *crtc,
7634 struct intel_crtc_state *crtc_state,
7635 intel_clock_t *reduced_clock,
7636 int num_connectors)
eb1cbe48 7637{
f47709a9 7638 struct drm_device *dev = crtc->base.dev;
eb1cbe48 7639 struct drm_i915_private *dev_priv = dev->dev_private;
eb1cbe48 7640 u32 dpll;
190f68c5 7641 struct dpll *clock = &crtc_state->dpll;
eb1cbe48 7642
190f68c5 7643 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
2a8f64ca 7644
eb1cbe48
DV
7645 dpll = DPLL_VGA_MODE_DIS;
7646
a93e255f 7647 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
eb1cbe48
DV
7648 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7649 } else {
7650 if (clock->p1 == 2)
7651 dpll |= PLL_P1_DIVIDE_BY_TWO;
7652 else
7653 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7654 if (clock->p2 == 4)
7655 dpll |= PLL_P2_DIVIDE_BY_4;
7656 }
7657
a93e255f 7658 if (!IS_I830(dev) && intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
4a33e48d
DV
7659 dpll |= DPLL_DVO_2X_MODE;
7660
a93e255f 7661 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
eb1cbe48
DV
7662 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
7663 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7664 else
7665 dpll |= PLL_REF_INPUT_DREFCLK;
7666
7667 dpll |= DPLL_VCO_ENABLE;
190f68c5 7668 crtc_state->dpll_hw_state.dpll = dpll;
eb1cbe48
DV
7669}
7670
8a654f3b 7671static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
b0e77b9c
PZ
7672{
7673 struct drm_device *dev = intel_crtc->base.dev;
7674 struct drm_i915_private *dev_priv = dev->dev_private;
7675 enum pipe pipe = intel_crtc->pipe;
6e3c9717 7676 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
7c5f93b0 7677 const struct drm_display_mode *adjusted_mode = &intel_crtc->config->base.adjusted_mode;
1caea6e9
VS
7678 uint32_t crtc_vtotal, crtc_vblank_end;
7679 int vsyncshift = 0;
4d8a62ea
DV
7680
7681 /* We need to be careful not to changed the adjusted mode, for otherwise
7682 * the hw state checker will get angry at the mismatch. */
7683 crtc_vtotal = adjusted_mode->crtc_vtotal;
7684 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
b0e77b9c 7685
609aeaca 7686 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
b0e77b9c 7687 /* the chip adds 2 halflines automatically */
4d8a62ea
DV
7688 crtc_vtotal -= 1;
7689 crtc_vblank_end -= 1;
609aeaca 7690
409ee761 7691 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
609aeaca
VS
7692 vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
7693 else
7694 vsyncshift = adjusted_mode->crtc_hsync_start -
7695 adjusted_mode->crtc_htotal / 2;
1caea6e9
VS
7696 if (vsyncshift < 0)
7697 vsyncshift += adjusted_mode->crtc_htotal;
b0e77b9c
PZ
7698 }
7699
7700 if (INTEL_INFO(dev)->gen > 3)
fe2b8f9d 7701 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
b0e77b9c 7702
fe2b8f9d 7703 I915_WRITE(HTOTAL(cpu_transcoder),
b0e77b9c
PZ
7704 (adjusted_mode->crtc_hdisplay - 1) |
7705 ((adjusted_mode->crtc_htotal - 1) << 16));
fe2b8f9d 7706 I915_WRITE(HBLANK(cpu_transcoder),
b0e77b9c
PZ
7707 (adjusted_mode->crtc_hblank_start - 1) |
7708 ((adjusted_mode->crtc_hblank_end - 1) << 16));
fe2b8f9d 7709 I915_WRITE(HSYNC(cpu_transcoder),
b0e77b9c
PZ
7710 (adjusted_mode->crtc_hsync_start - 1) |
7711 ((adjusted_mode->crtc_hsync_end - 1) << 16));
7712
fe2b8f9d 7713 I915_WRITE(VTOTAL(cpu_transcoder),
b0e77b9c 7714 (adjusted_mode->crtc_vdisplay - 1) |
4d8a62ea 7715 ((crtc_vtotal - 1) << 16));
fe2b8f9d 7716 I915_WRITE(VBLANK(cpu_transcoder),
b0e77b9c 7717 (adjusted_mode->crtc_vblank_start - 1) |
4d8a62ea 7718 ((crtc_vblank_end - 1) << 16));
fe2b8f9d 7719 I915_WRITE(VSYNC(cpu_transcoder),
b0e77b9c
PZ
7720 (adjusted_mode->crtc_vsync_start - 1) |
7721 ((adjusted_mode->crtc_vsync_end - 1) << 16));
7722
b5e508d4
PZ
7723 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
7724 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
7725 * documented on the DDI_FUNC_CTL register description, EDP Input Select
7726 * bits. */
7727 if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
7728 (pipe == PIPE_B || pipe == PIPE_C))
7729 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
7730
bc58be60
JN
7731}
7732
7733static void intel_set_pipe_src_size(struct intel_crtc *intel_crtc)
7734{
7735 struct drm_device *dev = intel_crtc->base.dev;
7736 struct drm_i915_private *dev_priv = dev->dev_private;
7737 enum pipe pipe = intel_crtc->pipe;
7738
b0e77b9c
PZ
7739 /* pipesrc controls the size that is scaled from, which should
7740 * always be the user's requested size.
7741 */
7742 I915_WRITE(PIPESRC(pipe),
6e3c9717
ACO
7743 ((intel_crtc->config->pipe_src_w - 1) << 16) |
7744 (intel_crtc->config->pipe_src_h - 1));
b0e77b9c
PZ
7745}
7746
1bd1bd80 7747static void intel_get_pipe_timings(struct intel_crtc *crtc,
5cec258b 7748 struct intel_crtc_state *pipe_config)
1bd1bd80
DV
7749{
7750 struct drm_device *dev = crtc->base.dev;
7751 struct drm_i915_private *dev_priv = dev->dev_private;
7752 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
7753 uint32_t tmp;
7754
7755 tmp = I915_READ(HTOTAL(cpu_transcoder));
2d112de7
ACO
7756 pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
7757 pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 7758 tmp = I915_READ(HBLANK(cpu_transcoder));
2d112de7
ACO
7759 pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
7760 pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 7761 tmp = I915_READ(HSYNC(cpu_transcoder));
2d112de7
ACO
7762 pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
7763 pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80
DV
7764
7765 tmp = I915_READ(VTOTAL(cpu_transcoder));
2d112de7
ACO
7766 pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
7767 pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 7768 tmp = I915_READ(VBLANK(cpu_transcoder));
2d112de7
ACO
7769 pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
7770 pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 7771 tmp = I915_READ(VSYNC(cpu_transcoder));
2d112de7
ACO
7772 pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
7773 pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80
DV
7774
7775 if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
2d112de7
ACO
7776 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
7777 pipe_config->base.adjusted_mode.crtc_vtotal += 1;
7778 pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
1bd1bd80 7779 }
bc58be60
JN
7780}
7781
7782static void intel_get_pipe_src_size(struct intel_crtc *crtc,
7783 struct intel_crtc_state *pipe_config)
7784{
7785 struct drm_device *dev = crtc->base.dev;
7786 struct drm_i915_private *dev_priv = dev->dev_private;
7787 u32 tmp;
1bd1bd80
DV
7788
7789 tmp = I915_READ(PIPESRC(crtc->pipe));
37327abd
VS
7790 pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
7791 pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
7792
2d112de7
ACO
7793 pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
7794 pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
1bd1bd80
DV
7795}
7796
f6a83288 7797void intel_mode_from_pipe_config(struct drm_display_mode *mode,
5cec258b 7798 struct intel_crtc_state *pipe_config)
babea61d 7799{
2d112de7
ACO
7800 mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
7801 mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
7802 mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
7803 mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
babea61d 7804
2d112de7
ACO
7805 mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
7806 mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
7807 mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
7808 mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
babea61d 7809
2d112de7 7810 mode->flags = pipe_config->base.adjusted_mode.flags;
cd13f5ab 7811 mode->type = DRM_MODE_TYPE_DRIVER;
babea61d 7812
2d112de7
ACO
7813 mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
7814 mode->flags |= pipe_config->base.adjusted_mode.flags;
cd13f5ab
ML
7815
7816 mode->hsync = drm_mode_hsync(mode);
7817 mode->vrefresh = drm_mode_vrefresh(mode);
7818 drm_mode_set_name(mode);
babea61d
JB
7819}
7820
84b046f3
DV
7821static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
7822{
7823 struct drm_device *dev = intel_crtc->base.dev;
7824 struct drm_i915_private *dev_priv = dev->dev_private;
7825 uint32_t pipeconf;
7826
9f11a9e4 7827 pipeconf = 0;
84b046f3 7828
b6b5d049
VS
7829 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
7830 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
7831 pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
67c72a12 7832
6e3c9717 7833 if (intel_crtc->config->double_wide)
cf532bb2 7834 pipeconf |= PIPECONF_DOUBLE_WIDE;
84b046f3 7835
ff9ce46e 7836 /* only g4x and later have fancy bpc/dither controls */
666a4537 7837 if (IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
ff9ce46e 7838 /* Bspec claims that we can't use dithering for 30bpp pipes. */
6e3c9717 7839 if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
ff9ce46e 7840 pipeconf |= PIPECONF_DITHER_EN |
84b046f3 7841 PIPECONF_DITHER_TYPE_SP;
84b046f3 7842
6e3c9717 7843 switch (intel_crtc->config->pipe_bpp) {
ff9ce46e
DV
7844 case 18:
7845 pipeconf |= PIPECONF_6BPC;
7846 break;
7847 case 24:
7848 pipeconf |= PIPECONF_8BPC;
7849 break;
7850 case 30:
7851 pipeconf |= PIPECONF_10BPC;
7852 break;
7853 default:
7854 /* Case prevented by intel_choose_pipe_bpp_dither. */
7855 BUG();
84b046f3
DV
7856 }
7857 }
7858
7859 if (HAS_PIPE_CXSR(dev)) {
7860 if (intel_crtc->lowfreq_avail) {
7861 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
7862 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
7863 } else {
7864 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
84b046f3
DV
7865 }
7866 }
7867
6e3c9717 7868 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
efc2cfff 7869 if (INTEL_INFO(dev)->gen < 4 ||
409ee761 7870 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
efc2cfff
VS
7871 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
7872 else
7873 pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
7874 } else
84b046f3
DV
7875 pipeconf |= PIPECONF_PROGRESSIVE;
7876
666a4537
WB
7877 if ((IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) &&
7878 intel_crtc->config->limited_color_range)
9f11a9e4 7879 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
9c8e09b7 7880
84b046f3
DV
7881 I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
7882 POSTING_READ(PIPECONF(intel_crtc->pipe));
7883}
7884
190f68c5
ACO
7885static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
7886 struct intel_crtc_state *crtc_state)
79e53945 7887{
c7653199 7888 struct drm_device *dev = crtc->base.dev;
79e53945 7889 struct drm_i915_private *dev_priv = dev->dev_private;
c751ce4f 7890 int refclk, num_connectors = 0;
c329a4ec
DV
7891 intel_clock_t clock;
7892 bool ok;
d4906093 7893 const intel_limit_t *limit;
55bb9992 7894 struct drm_atomic_state *state = crtc_state->base.state;
da3ced29 7895 struct drm_connector *connector;
55bb9992
ACO
7896 struct drm_connector_state *connector_state;
7897 int i;
79e53945 7898
dd3cd74a
ACO
7899 memset(&crtc_state->dpll_hw_state, 0,
7900 sizeof(crtc_state->dpll_hw_state));
7901
a65347ba
JN
7902 if (crtc_state->has_dsi_encoder)
7903 return 0;
43565a06 7904
a65347ba
JN
7905 for_each_connector_in_state(state, connector, connector_state, i) {
7906 if (connector_state->crtc == &crtc->base)
7907 num_connectors++;
79e53945
JB
7908 }
7909
190f68c5 7910 if (!crtc_state->clock_set) {
a93e255f 7911 refclk = i9xx_get_refclk(crtc_state, num_connectors);
79e53945 7912
e9fd1c02
JN
7913 /*
7914 * Returns a set of divisors for the desired target clock with
7915 * the given refclk, or FALSE. The returned values represent
7916 * the clock equation: reflck * (5 * (m1 + 2) + (m2 + 2)) / (n +
7917 * 2) / p1 / p2.
7918 */
a93e255f
ACO
7919 limit = intel_limit(crtc_state, refclk);
7920 ok = dev_priv->display.find_dpll(limit, crtc_state,
190f68c5 7921 crtc_state->port_clock,
e9fd1c02 7922 refclk, NULL, &clock);
f2335330 7923 if (!ok) {
e9fd1c02
JN
7924 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7925 return -EINVAL;
7926 }
79e53945 7927
f2335330 7928 /* Compat-code for transition, will disappear. */
190f68c5
ACO
7929 crtc_state->dpll.n = clock.n;
7930 crtc_state->dpll.m1 = clock.m1;
7931 crtc_state->dpll.m2 = clock.m2;
7932 crtc_state->dpll.p1 = clock.p1;
7933 crtc_state->dpll.p2 = clock.p2;
f47709a9 7934 }
7026d4ac 7935
e9fd1c02 7936 if (IS_GEN2(dev)) {
c329a4ec 7937 i8xx_compute_dpll(crtc, crtc_state, NULL,
251ac862 7938 num_connectors);
9d556c99 7939 } else if (IS_CHERRYVIEW(dev)) {
251ac862 7940 chv_compute_dpll(crtc, crtc_state);
e9fd1c02 7941 } else if (IS_VALLEYVIEW(dev)) {
251ac862 7942 vlv_compute_dpll(crtc, crtc_state);
e9fd1c02 7943 } else {
c329a4ec 7944 i9xx_compute_dpll(crtc, crtc_state, NULL,
251ac862 7945 num_connectors);
e9fd1c02 7946 }
79e53945 7947
c8f7a0db 7948 return 0;
f564048e
EA
7949}
7950
2fa2fe9a 7951static void i9xx_get_pfit_config(struct intel_crtc *crtc,
5cec258b 7952 struct intel_crtc_state *pipe_config)
2fa2fe9a
DV
7953{
7954 struct drm_device *dev = crtc->base.dev;
7955 struct drm_i915_private *dev_priv = dev->dev_private;
7956 uint32_t tmp;
7957
dc9e7dec
VS
7958 if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
7959 return;
7960
2fa2fe9a 7961 tmp = I915_READ(PFIT_CONTROL);
06922821
DV
7962 if (!(tmp & PFIT_ENABLE))
7963 return;
2fa2fe9a 7964
06922821 7965 /* Check whether the pfit is attached to our pipe. */
2fa2fe9a
DV
7966 if (INTEL_INFO(dev)->gen < 4) {
7967 if (crtc->pipe != PIPE_B)
7968 return;
2fa2fe9a
DV
7969 } else {
7970 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
7971 return;
7972 }
7973
06922821 7974 pipe_config->gmch_pfit.control = tmp;
2fa2fe9a
DV
7975 pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
7976 if (INTEL_INFO(dev)->gen < 5)
7977 pipe_config->gmch_pfit.lvds_border_bits =
7978 I915_READ(LVDS) & LVDS_BORDER_ENABLE;
7979}
7980
acbec814 7981static void vlv_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 7982 struct intel_crtc_state *pipe_config)
acbec814
JB
7983{
7984 struct drm_device *dev = crtc->base.dev;
7985 struct drm_i915_private *dev_priv = dev->dev_private;
7986 int pipe = pipe_config->cpu_transcoder;
7987 intel_clock_t clock;
7988 u32 mdiv;
662c6ecb 7989 int refclk = 100000;
acbec814 7990
f573de5a
SK
7991 /* In case of MIPI DPLL will not even be used */
7992 if (!(pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE))
7993 return;
7994
a580516d 7995 mutex_lock(&dev_priv->sb_lock);
ab3c759a 7996 mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
a580516d 7997 mutex_unlock(&dev_priv->sb_lock);
acbec814
JB
7998
7999 clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
8000 clock.m2 = mdiv & DPIO_M2DIV_MASK;
8001 clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
8002 clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
8003 clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
8004
dccbea3b 8005 pipe_config->port_clock = vlv_calc_dpll_params(refclk, &clock);
acbec814
JB
8006}
8007
5724dbd1
DL
8008static void
8009i9xx_get_initial_plane_config(struct intel_crtc *crtc,
8010 struct intel_initial_plane_config *plane_config)
1ad292b5
JB
8011{
8012 struct drm_device *dev = crtc->base.dev;
8013 struct drm_i915_private *dev_priv = dev->dev_private;
8014 u32 val, base, offset;
8015 int pipe = crtc->pipe, plane = crtc->plane;
8016 int fourcc, pixel_format;
6761dd31 8017 unsigned int aligned_height;
b113d5ee 8018 struct drm_framebuffer *fb;
1b842c89 8019 struct intel_framebuffer *intel_fb;
1ad292b5 8020
42a7b088
DL
8021 val = I915_READ(DSPCNTR(plane));
8022 if (!(val & DISPLAY_PLANE_ENABLE))
8023 return;
8024
d9806c9f 8025 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
1b842c89 8026 if (!intel_fb) {
1ad292b5
JB
8027 DRM_DEBUG_KMS("failed to alloc fb\n");
8028 return;
8029 }
8030
1b842c89
DL
8031 fb = &intel_fb->base;
8032
18c5247e
DV
8033 if (INTEL_INFO(dev)->gen >= 4) {
8034 if (val & DISPPLANE_TILED) {
49af449b 8035 plane_config->tiling = I915_TILING_X;
18c5247e
DV
8036 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
8037 }
8038 }
1ad292b5
JB
8039
8040 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
b35d63fa 8041 fourcc = i9xx_format_to_fourcc(pixel_format);
b113d5ee
DL
8042 fb->pixel_format = fourcc;
8043 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
1ad292b5
JB
8044
8045 if (INTEL_INFO(dev)->gen >= 4) {
49af449b 8046 if (plane_config->tiling)
1ad292b5
JB
8047 offset = I915_READ(DSPTILEOFF(plane));
8048 else
8049 offset = I915_READ(DSPLINOFF(plane));
8050 base = I915_READ(DSPSURF(plane)) & 0xfffff000;
8051 } else {
8052 base = I915_READ(DSPADDR(plane));
8053 }
8054 plane_config->base = base;
8055
8056 val = I915_READ(PIPESRC(pipe));
b113d5ee
DL
8057 fb->width = ((val >> 16) & 0xfff) + 1;
8058 fb->height = ((val >> 0) & 0xfff) + 1;
1ad292b5
JB
8059
8060 val = I915_READ(DSPSTRIDE(pipe));
b113d5ee 8061 fb->pitches[0] = val & 0xffffffc0;
1ad292b5 8062
b113d5ee 8063 aligned_height = intel_fb_align_height(dev, fb->height,
091df6cb
DV
8064 fb->pixel_format,
8065 fb->modifier[0]);
1ad292b5 8066
f37b5c2b 8067 plane_config->size = fb->pitches[0] * aligned_height;
1ad292b5 8068
2844a921
DL
8069 DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
8070 pipe_name(pipe), plane, fb->width, fb->height,
8071 fb->bits_per_pixel, base, fb->pitches[0],
8072 plane_config->size);
1ad292b5 8073
2d14030b 8074 plane_config->fb = intel_fb;
1ad292b5
JB
8075}
8076
70b23a98 8077static void chv_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 8078 struct intel_crtc_state *pipe_config)
70b23a98
VS
8079{
8080 struct drm_device *dev = crtc->base.dev;
8081 struct drm_i915_private *dev_priv = dev->dev_private;
8082 int pipe = pipe_config->cpu_transcoder;
8083 enum dpio_channel port = vlv_pipe_to_channel(pipe);
8084 intel_clock_t clock;
0d7b6b11 8085 u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2, pll_dw3;
70b23a98
VS
8086 int refclk = 100000;
8087
a580516d 8088 mutex_lock(&dev_priv->sb_lock);
70b23a98
VS
8089 cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
8090 pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
8091 pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
8092 pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
0d7b6b11 8093 pll_dw3 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
a580516d 8094 mutex_unlock(&dev_priv->sb_lock);
70b23a98
VS
8095
8096 clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
0d7b6b11
ID
8097 clock.m2 = (pll_dw0 & 0xff) << 22;
8098 if (pll_dw3 & DPIO_CHV_FRAC_DIV_EN)
8099 clock.m2 |= pll_dw2 & 0x3fffff;
70b23a98
VS
8100 clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
8101 clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
8102 clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
8103
dccbea3b 8104 pipe_config->port_clock = chv_calc_dpll_params(refclk, &clock);
70b23a98
VS
8105}
8106
0e8ffe1b 8107static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
5cec258b 8108 struct intel_crtc_state *pipe_config)
0e8ffe1b
DV
8109{
8110 struct drm_device *dev = crtc->base.dev;
8111 struct drm_i915_private *dev_priv = dev->dev_private;
1729050e 8112 enum intel_display_power_domain power_domain;
0e8ffe1b 8113 uint32_t tmp;
1729050e 8114 bool ret;
0e8ffe1b 8115
1729050e
ID
8116 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
8117 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
b5482bd0
ID
8118 return false;
8119
e143a21c 8120 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
8106ddbd 8121 pipe_config->shared_dpll = NULL;
eccb140b 8122
1729050e
ID
8123 ret = false;
8124
0e8ffe1b
DV
8125 tmp = I915_READ(PIPECONF(crtc->pipe));
8126 if (!(tmp & PIPECONF_ENABLE))
1729050e 8127 goto out;
0e8ffe1b 8128
666a4537 8129 if (IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
42571aef
VS
8130 switch (tmp & PIPECONF_BPC_MASK) {
8131 case PIPECONF_6BPC:
8132 pipe_config->pipe_bpp = 18;
8133 break;
8134 case PIPECONF_8BPC:
8135 pipe_config->pipe_bpp = 24;
8136 break;
8137 case PIPECONF_10BPC:
8138 pipe_config->pipe_bpp = 30;
8139 break;
8140 default:
8141 break;
8142 }
8143 }
8144
666a4537
WB
8145 if ((IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) &&
8146 (tmp & PIPECONF_COLOR_RANGE_SELECT))
b5a9fa09
DV
8147 pipe_config->limited_color_range = true;
8148
282740f7
VS
8149 if (INTEL_INFO(dev)->gen < 4)
8150 pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
8151
1bd1bd80 8152 intel_get_pipe_timings(crtc, pipe_config);
bc58be60 8153 intel_get_pipe_src_size(crtc, pipe_config);
1bd1bd80 8154
2fa2fe9a
DV
8155 i9xx_get_pfit_config(crtc, pipe_config);
8156
6c49f241
DV
8157 if (INTEL_INFO(dev)->gen >= 4) {
8158 tmp = I915_READ(DPLL_MD(crtc->pipe));
8159 pipe_config->pixel_multiplier =
8160 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
8161 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
8bcc2795 8162 pipe_config->dpll_hw_state.dpll_md = tmp;
6c49f241
DV
8163 } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
8164 tmp = I915_READ(DPLL(crtc->pipe));
8165 pipe_config->pixel_multiplier =
8166 ((tmp & SDVO_MULTIPLIER_MASK)
8167 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
8168 } else {
8169 /* Note that on i915G/GM the pixel multiplier is in the sdvo
8170 * port and will be fixed up in the encoder->get_config
8171 * function. */
8172 pipe_config->pixel_multiplier = 1;
8173 }
8bcc2795 8174 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
666a4537 8175 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
1c4e0274
VS
8176 /*
8177 * DPLL_DVO_2X_MODE must be enabled for both DPLLs
8178 * on 830. Filter it out here so that we don't
8179 * report errors due to that.
8180 */
8181 if (IS_I830(dev))
8182 pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
8183
8bcc2795
DV
8184 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
8185 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
165e901c
VS
8186 } else {
8187 /* Mask out read-only status bits. */
8188 pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
8189 DPLL_PORTC_READY_MASK |
8190 DPLL_PORTB_READY_MASK);
8bcc2795 8191 }
6c49f241 8192
70b23a98
VS
8193 if (IS_CHERRYVIEW(dev))
8194 chv_crtc_clock_get(crtc, pipe_config);
8195 else if (IS_VALLEYVIEW(dev))
acbec814
JB
8196 vlv_crtc_clock_get(crtc, pipe_config);
8197 else
8198 i9xx_crtc_clock_get(crtc, pipe_config);
18442d08 8199
0f64614d
VS
8200 /*
8201 * Normally the dotclock is filled in by the encoder .get_config()
8202 * but in case the pipe is enabled w/o any ports we need a sane
8203 * default.
8204 */
8205 pipe_config->base.adjusted_mode.crtc_clock =
8206 pipe_config->port_clock / pipe_config->pixel_multiplier;
8207
1729050e
ID
8208 ret = true;
8209
8210out:
8211 intel_display_power_put(dev_priv, power_domain);
8212
8213 return ret;
0e8ffe1b
DV
8214}
8215
dde86e2d 8216static void ironlake_init_pch_refclk(struct drm_device *dev)
13d83a67
JB
8217{
8218 struct drm_i915_private *dev_priv = dev->dev_private;
13d83a67 8219 struct intel_encoder *encoder;
74cfd7ac 8220 u32 val, final;
13d83a67 8221 bool has_lvds = false;
199e5d79 8222 bool has_cpu_edp = false;
199e5d79 8223 bool has_panel = false;
99eb6a01
KP
8224 bool has_ck505 = false;
8225 bool can_ssc = false;
13d83a67
JB
8226
8227 /* We need to take the global config into account */
b2784e15 8228 for_each_intel_encoder(dev, encoder) {
199e5d79
KP
8229 switch (encoder->type) {
8230 case INTEL_OUTPUT_LVDS:
8231 has_panel = true;
8232 has_lvds = true;
8233 break;
8234 case INTEL_OUTPUT_EDP:
8235 has_panel = true;
2de6905f 8236 if (enc_to_dig_port(&encoder->base)->port == PORT_A)
199e5d79
KP
8237 has_cpu_edp = true;
8238 break;
6847d71b
PZ
8239 default:
8240 break;
13d83a67
JB
8241 }
8242 }
8243
99eb6a01 8244 if (HAS_PCH_IBX(dev)) {
41aa3448 8245 has_ck505 = dev_priv->vbt.display_clock_mode;
99eb6a01
KP
8246 can_ssc = has_ck505;
8247 } else {
8248 has_ck505 = false;
8249 can_ssc = true;
8250 }
8251
2de6905f
ID
8252 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
8253 has_panel, has_lvds, has_ck505);
13d83a67
JB
8254
8255 /* Ironlake: try to setup display ref clock before DPLL
8256 * enabling. This is only under driver's control after
8257 * PCH B stepping, previous chipset stepping should be
8258 * ignoring this setting.
8259 */
74cfd7ac
CW
8260 val = I915_READ(PCH_DREF_CONTROL);
8261
8262 /* As we must carefully and slowly disable/enable each source in turn,
8263 * compute the final state we want first and check if we need to
8264 * make any changes at all.
8265 */
8266 final = val;
8267 final &= ~DREF_NONSPREAD_SOURCE_MASK;
8268 if (has_ck505)
8269 final |= DREF_NONSPREAD_CK505_ENABLE;
8270 else
8271 final |= DREF_NONSPREAD_SOURCE_ENABLE;
8272
8273 final &= ~DREF_SSC_SOURCE_MASK;
8274 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
8275 final &= ~DREF_SSC1_ENABLE;
8276
8277 if (has_panel) {
8278 final |= DREF_SSC_SOURCE_ENABLE;
8279
8280 if (intel_panel_use_ssc(dev_priv) && can_ssc)
8281 final |= DREF_SSC1_ENABLE;
8282
8283 if (has_cpu_edp) {
8284 if (intel_panel_use_ssc(dev_priv) && can_ssc)
8285 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
8286 else
8287 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
8288 } else
8289 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
8290 } else {
8291 final |= DREF_SSC_SOURCE_DISABLE;
8292 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
8293 }
8294
8295 if (final == val)
8296 return;
8297
13d83a67 8298 /* Always enable nonspread source */
74cfd7ac 8299 val &= ~DREF_NONSPREAD_SOURCE_MASK;
13d83a67 8300
99eb6a01 8301 if (has_ck505)
74cfd7ac 8302 val |= DREF_NONSPREAD_CK505_ENABLE;
99eb6a01 8303 else
74cfd7ac 8304 val |= DREF_NONSPREAD_SOURCE_ENABLE;
13d83a67 8305
199e5d79 8306 if (has_panel) {
74cfd7ac
CW
8307 val &= ~DREF_SSC_SOURCE_MASK;
8308 val |= DREF_SSC_SOURCE_ENABLE;
13d83a67 8309
199e5d79 8310 /* SSC must be turned on before enabling the CPU output */
99eb6a01 8311 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
199e5d79 8312 DRM_DEBUG_KMS("Using SSC on panel\n");
74cfd7ac 8313 val |= DREF_SSC1_ENABLE;
e77166b5 8314 } else
74cfd7ac 8315 val &= ~DREF_SSC1_ENABLE;
199e5d79
KP
8316
8317 /* Get SSC going before enabling the outputs */
74cfd7ac 8318 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
8319 POSTING_READ(PCH_DREF_CONTROL);
8320 udelay(200);
8321
74cfd7ac 8322 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
13d83a67
JB
8323
8324 /* Enable CPU source on CPU attached eDP */
199e5d79 8325 if (has_cpu_edp) {
99eb6a01 8326 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
199e5d79 8327 DRM_DEBUG_KMS("Using SSC on eDP\n");
74cfd7ac 8328 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
eba905b2 8329 } else
74cfd7ac 8330 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
199e5d79 8331 } else
74cfd7ac 8332 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
199e5d79 8333
74cfd7ac 8334 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
8335 POSTING_READ(PCH_DREF_CONTROL);
8336 udelay(200);
8337 } else {
8338 DRM_DEBUG_KMS("Disabling SSC entirely\n");
8339
74cfd7ac 8340 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
199e5d79
KP
8341
8342 /* Turn off CPU output */
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
8349 /* Turn off the SSC source */
74cfd7ac
CW
8350 val &= ~DREF_SSC_SOURCE_MASK;
8351 val |= DREF_SSC_SOURCE_DISABLE;
199e5d79
KP
8352
8353 /* Turn off SSC1 */
74cfd7ac 8354 val &= ~DREF_SSC1_ENABLE;
199e5d79 8355
74cfd7ac 8356 I915_WRITE(PCH_DREF_CONTROL, val);
13d83a67
JB
8357 POSTING_READ(PCH_DREF_CONTROL);
8358 udelay(200);
8359 }
74cfd7ac
CW
8360
8361 BUG_ON(val != final);
13d83a67
JB
8362}
8363
f31f2d55 8364static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
dde86e2d 8365{
f31f2d55 8366 uint32_t tmp;
dde86e2d 8367
0ff066a9
PZ
8368 tmp = I915_READ(SOUTH_CHICKEN2);
8369 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
8370 I915_WRITE(SOUTH_CHICKEN2, tmp);
dde86e2d 8371
0ff066a9
PZ
8372 if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
8373 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
8374 DRM_ERROR("FDI mPHY reset assert timeout\n");
dde86e2d 8375
0ff066a9
PZ
8376 tmp = I915_READ(SOUTH_CHICKEN2);
8377 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
8378 I915_WRITE(SOUTH_CHICKEN2, tmp);
dde86e2d 8379
0ff066a9
PZ
8380 if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
8381 FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
8382 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
f31f2d55
PZ
8383}
8384
8385/* WaMPhyProgramming:hsw */
8386static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
8387{
8388 uint32_t tmp;
dde86e2d
PZ
8389
8390 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
8391 tmp &= ~(0xFF << 24);
8392 tmp |= (0x12 << 24);
8393 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
8394
dde86e2d
PZ
8395 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
8396 tmp |= (1 << 11);
8397 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
8398
8399 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
8400 tmp |= (1 << 11);
8401 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
8402
dde86e2d
PZ
8403 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
8404 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
8405 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
8406
8407 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
8408 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
8409 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
8410
0ff066a9
PZ
8411 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
8412 tmp &= ~(7 << 13);
8413 tmp |= (5 << 13);
8414 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
dde86e2d 8415
0ff066a9
PZ
8416 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
8417 tmp &= ~(7 << 13);
8418 tmp |= (5 << 13);
8419 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
dde86e2d
PZ
8420
8421 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
8422 tmp &= ~0xFF;
8423 tmp |= 0x1C;
8424 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
8425
8426 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
8427 tmp &= ~0xFF;
8428 tmp |= 0x1C;
8429 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
8430
8431 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
8432 tmp &= ~(0xFF << 16);
8433 tmp |= (0x1C << 16);
8434 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
8435
8436 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
8437 tmp &= ~(0xFF << 16);
8438 tmp |= (0x1C << 16);
8439 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
8440
0ff066a9
PZ
8441 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
8442 tmp |= (1 << 27);
8443 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
dde86e2d 8444
0ff066a9
PZ
8445 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
8446 tmp |= (1 << 27);
8447 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
dde86e2d 8448
0ff066a9
PZ
8449 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
8450 tmp &= ~(0xF << 28);
8451 tmp |= (4 << 28);
8452 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
dde86e2d 8453
0ff066a9
PZ
8454 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
8455 tmp &= ~(0xF << 28);
8456 tmp |= (4 << 28);
8457 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
f31f2d55
PZ
8458}
8459
2fa86a1f
PZ
8460/* Implements 3 different sequences from BSpec chapter "Display iCLK
8461 * Programming" based on the parameters passed:
8462 * - Sequence to enable CLKOUT_DP
8463 * - Sequence to enable CLKOUT_DP without spread
8464 * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
8465 */
8466static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
8467 bool with_fdi)
f31f2d55
PZ
8468{
8469 struct drm_i915_private *dev_priv = dev->dev_private;
2fa86a1f
PZ
8470 uint32_t reg, tmp;
8471
8472 if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
8473 with_spread = true;
c2699524 8474 if (WARN(HAS_PCH_LPT_LP(dev) && with_fdi, "LP PCH doesn't have FDI\n"))
2fa86a1f 8475 with_fdi = false;
f31f2d55 8476
a580516d 8477 mutex_lock(&dev_priv->sb_lock);
f31f2d55
PZ
8478
8479 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8480 tmp &= ~SBI_SSCCTL_DISABLE;
8481 tmp |= SBI_SSCCTL_PATHALT;
8482 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8483
8484 udelay(24);
8485
2fa86a1f
PZ
8486 if (with_spread) {
8487 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8488 tmp &= ~SBI_SSCCTL_PATHALT;
8489 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
f31f2d55 8490
2fa86a1f
PZ
8491 if (with_fdi) {
8492 lpt_reset_fdi_mphy(dev_priv);
8493 lpt_program_fdi_mphy(dev_priv);
8494 }
8495 }
dde86e2d 8496
c2699524 8497 reg = HAS_PCH_LPT_LP(dev) ? SBI_GEN0 : SBI_DBUFF0;
2fa86a1f
PZ
8498 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8499 tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8500 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
c00db246 8501
a580516d 8502 mutex_unlock(&dev_priv->sb_lock);
dde86e2d
PZ
8503}
8504
47701c3b
PZ
8505/* Sequence to disable CLKOUT_DP */
8506static void lpt_disable_clkout_dp(struct drm_device *dev)
8507{
8508 struct drm_i915_private *dev_priv = dev->dev_private;
8509 uint32_t reg, tmp;
8510
a580516d 8511 mutex_lock(&dev_priv->sb_lock);
47701c3b 8512
c2699524 8513 reg = HAS_PCH_LPT_LP(dev) ? SBI_GEN0 : SBI_DBUFF0;
47701c3b
PZ
8514 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8515 tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8516 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
8517
8518 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8519 if (!(tmp & SBI_SSCCTL_DISABLE)) {
8520 if (!(tmp & SBI_SSCCTL_PATHALT)) {
8521 tmp |= SBI_SSCCTL_PATHALT;
8522 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8523 udelay(32);
8524 }
8525 tmp |= SBI_SSCCTL_DISABLE;
8526 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8527 }
8528
a580516d 8529 mutex_unlock(&dev_priv->sb_lock);
47701c3b
PZ
8530}
8531
f7be2c21
VS
8532#define BEND_IDX(steps) ((50 + (steps)) / 5)
8533
8534static const uint16_t sscdivintphase[] = {
8535 [BEND_IDX( 50)] = 0x3B23,
8536 [BEND_IDX( 45)] = 0x3B23,
8537 [BEND_IDX( 40)] = 0x3C23,
8538 [BEND_IDX( 35)] = 0x3C23,
8539 [BEND_IDX( 30)] = 0x3D23,
8540 [BEND_IDX( 25)] = 0x3D23,
8541 [BEND_IDX( 20)] = 0x3E23,
8542 [BEND_IDX( 15)] = 0x3E23,
8543 [BEND_IDX( 10)] = 0x3F23,
8544 [BEND_IDX( 5)] = 0x3F23,
8545 [BEND_IDX( 0)] = 0x0025,
8546 [BEND_IDX( -5)] = 0x0025,
8547 [BEND_IDX(-10)] = 0x0125,
8548 [BEND_IDX(-15)] = 0x0125,
8549 [BEND_IDX(-20)] = 0x0225,
8550 [BEND_IDX(-25)] = 0x0225,
8551 [BEND_IDX(-30)] = 0x0325,
8552 [BEND_IDX(-35)] = 0x0325,
8553 [BEND_IDX(-40)] = 0x0425,
8554 [BEND_IDX(-45)] = 0x0425,
8555 [BEND_IDX(-50)] = 0x0525,
8556};
8557
8558/*
8559 * Bend CLKOUT_DP
8560 * steps -50 to 50 inclusive, in steps of 5
8561 * < 0 slow down the clock, > 0 speed up the clock, 0 == no bend (135MHz)
8562 * change in clock period = -(steps / 10) * 5.787 ps
8563 */
8564static void lpt_bend_clkout_dp(struct drm_i915_private *dev_priv, int steps)
8565{
8566 uint32_t tmp;
8567 int idx = BEND_IDX(steps);
8568
8569 if (WARN_ON(steps % 5 != 0))
8570 return;
8571
8572 if (WARN_ON(idx >= ARRAY_SIZE(sscdivintphase)))
8573 return;
8574
8575 mutex_lock(&dev_priv->sb_lock);
8576
8577 if (steps % 10 != 0)
8578 tmp = 0xAAAAAAAB;
8579 else
8580 tmp = 0x00000000;
8581 intel_sbi_write(dev_priv, SBI_SSCDITHPHASE, tmp, SBI_ICLK);
8582
8583 tmp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE, SBI_ICLK);
8584 tmp &= 0xffff0000;
8585 tmp |= sscdivintphase[idx];
8586 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE, tmp, SBI_ICLK);
8587
8588 mutex_unlock(&dev_priv->sb_lock);
8589}
8590
8591#undef BEND_IDX
8592
bf8fa3d3
PZ
8593static void lpt_init_pch_refclk(struct drm_device *dev)
8594{
bf8fa3d3
PZ
8595 struct intel_encoder *encoder;
8596 bool has_vga = false;
8597
b2784e15 8598 for_each_intel_encoder(dev, encoder) {
bf8fa3d3
PZ
8599 switch (encoder->type) {
8600 case INTEL_OUTPUT_ANALOG:
8601 has_vga = true;
8602 break;
6847d71b
PZ
8603 default:
8604 break;
bf8fa3d3
PZ
8605 }
8606 }
8607
f7be2c21
VS
8608 if (has_vga) {
8609 lpt_bend_clkout_dp(to_i915(dev), 0);
47701c3b 8610 lpt_enable_clkout_dp(dev, true, true);
f7be2c21 8611 } else {
47701c3b 8612 lpt_disable_clkout_dp(dev);
f7be2c21 8613 }
bf8fa3d3
PZ
8614}
8615
dde86e2d
PZ
8616/*
8617 * Initialize reference clocks when the driver loads
8618 */
8619void intel_init_pch_refclk(struct drm_device *dev)
8620{
8621 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
8622 ironlake_init_pch_refclk(dev);
8623 else if (HAS_PCH_LPT(dev))
8624 lpt_init_pch_refclk(dev);
8625}
8626
55bb9992 8627static int ironlake_get_refclk(struct intel_crtc_state *crtc_state)
d9d444cb 8628{
55bb9992 8629 struct drm_device *dev = crtc_state->base.crtc->dev;
d9d444cb 8630 struct drm_i915_private *dev_priv = dev->dev_private;
55bb9992 8631 struct drm_atomic_state *state = crtc_state->base.state;
da3ced29 8632 struct drm_connector *connector;
55bb9992 8633 struct drm_connector_state *connector_state;
d9d444cb 8634 struct intel_encoder *encoder;
55bb9992 8635 int num_connectors = 0, i;
d9d444cb
JB
8636 bool is_lvds = false;
8637
da3ced29 8638 for_each_connector_in_state(state, connector, connector_state, i) {
55bb9992
ACO
8639 if (connector_state->crtc != crtc_state->base.crtc)
8640 continue;
8641
8642 encoder = to_intel_encoder(connector_state->best_encoder);
8643
d9d444cb
JB
8644 switch (encoder->type) {
8645 case INTEL_OUTPUT_LVDS:
8646 is_lvds = true;
8647 break;
6847d71b
PZ
8648 default:
8649 break;
d9d444cb
JB
8650 }
8651 num_connectors++;
8652 }
8653
8654 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
e91e941b 8655 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
41aa3448 8656 dev_priv->vbt.lvds_ssc_freq);
e91e941b 8657 return dev_priv->vbt.lvds_ssc_freq;
d9d444cb
JB
8658 }
8659
8660 return 120000;
8661}
8662
6ff93609 8663static void ironlake_set_pipeconf(struct drm_crtc *crtc)
79e53945 8664{
c8203565 8665 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
79e53945
JB
8666 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8667 int pipe = intel_crtc->pipe;
c8203565
PZ
8668 uint32_t val;
8669
78114071 8670 val = 0;
c8203565 8671
6e3c9717 8672 switch (intel_crtc->config->pipe_bpp) {
c8203565 8673 case 18:
dfd07d72 8674 val |= PIPECONF_6BPC;
c8203565
PZ
8675 break;
8676 case 24:
dfd07d72 8677 val |= PIPECONF_8BPC;
c8203565
PZ
8678 break;
8679 case 30:
dfd07d72 8680 val |= PIPECONF_10BPC;
c8203565
PZ
8681 break;
8682 case 36:
dfd07d72 8683 val |= PIPECONF_12BPC;
c8203565
PZ
8684 break;
8685 default:
cc769b62
PZ
8686 /* Case prevented by intel_choose_pipe_bpp_dither. */
8687 BUG();
c8203565
PZ
8688 }
8689
6e3c9717 8690 if (intel_crtc->config->dither)
c8203565
PZ
8691 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8692
6e3c9717 8693 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
c8203565
PZ
8694 val |= PIPECONF_INTERLACED_ILK;
8695 else
8696 val |= PIPECONF_PROGRESSIVE;
8697
6e3c9717 8698 if (intel_crtc->config->limited_color_range)
3685a8f3 8699 val |= PIPECONF_COLOR_RANGE_SELECT;
3685a8f3 8700
c8203565
PZ
8701 I915_WRITE(PIPECONF(pipe), val);
8702 POSTING_READ(PIPECONF(pipe));
8703}
8704
86d3efce
VS
8705/*
8706 * Set up the pipe CSC unit.
8707 *
8708 * Currently only full range RGB to limited range RGB conversion
8709 * is supported, but eventually this should handle various
8710 * RGB<->YCbCr scenarios as well.
8711 */
50f3b016 8712static void intel_set_pipe_csc(struct drm_crtc *crtc)
86d3efce
VS
8713{
8714 struct drm_device *dev = crtc->dev;
8715 struct drm_i915_private *dev_priv = dev->dev_private;
8716 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8717 int pipe = intel_crtc->pipe;
8718 uint16_t coeff = 0x7800; /* 1.0 */
8719
8720 /*
8721 * TODO: Check what kind of values actually come out of the pipe
8722 * with these coeff/postoff values and adjust to get the best
8723 * accuracy. Perhaps we even need to take the bpc value into
8724 * consideration.
8725 */
8726
6e3c9717 8727 if (intel_crtc->config->limited_color_range)
86d3efce
VS
8728 coeff = ((235 - 16) * (1 << 12) / 255) & 0xff8; /* 0.xxx... */
8729
8730 /*
8731 * GY/GU and RY/RU should be the other way around according
8732 * to BSpec, but reality doesn't agree. Just set them up in
8733 * a way that results in the correct picture.
8734 */
8735 I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff << 16);
8736 I915_WRITE(PIPE_CSC_COEFF_BY(pipe), 0);
8737
8738 I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff);
8739 I915_WRITE(PIPE_CSC_COEFF_BU(pipe), 0);
8740
8741 I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), 0);
8742 I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff << 16);
8743
8744 I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), 0);
8745 I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), 0);
8746 I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), 0);
8747
8748 if (INTEL_INFO(dev)->gen > 6) {
8749 uint16_t postoff = 0;
8750
6e3c9717 8751 if (intel_crtc->config->limited_color_range)
32cf0cb0 8752 postoff = (16 * (1 << 12) / 255) & 0x1fff;
86d3efce
VS
8753
8754 I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff);
8755 I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff);
8756 I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff);
8757
8758 I915_WRITE(PIPE_CSC_MODE(pipe), 0);
8759 } else {
8760 uint32_t mode = CSC_MODE_YUV_TO_RGB;
8761
6e3c9717 8762 if (intel_crtc->config->limited_color_range)
86d3efce
VS
8763 mode |= CSC_BLACK_SCREEN_OFFSET;
8764
8765 I915_WRITE(PIPE_CSC_MODE(pipe), mode);
8766 }
8767}
8768
6ff93609 8769static void haswell_set_pipeconf(struct drm_crtc *crtc)
ee2b0b38 8770{
391bf048 8771 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
ee2b0b38 8772 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6e3c9717 8773 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
391bf048 8774 u32 val = 0;
ee2b0b38 8775
391bf048 8776 if (IS_HASWELL(dev_priv) && intel_crtc->config->dither)
ee2b0b38
PZ
8777 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8778
6e3c9717 8779 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
ee2b0b38
PZ
8780 val |= PIPECONF_INTERLACED_ILK;
8781 else
8782 val |= PIPECONF_PROGRESSIVE;
8783
702e7a56
PZ
8784 I915_WRITE(PIPECONF(cpu_transcoder), val);
8785 POSTING_READ(PIPECONF(cpu_transcoder));
391bf048
JN
8786}
8787
8788static void haswell_set_pipe_gamma(struct drm_crtc *crtc)
8789{
8790 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
8791 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3eff4faa
DV
8792
8793 I915_WRITE(GAMMA_MODE(intel_crtc->pipe), GAMMA_MODE_MODE_8BIT);
8794 POSTING_READ(GAMMA_MODE(intel_crtc->pipe));
391bf048
JN
8795}
8796
8797static void haswell_set_pipemisc(struct drm_crtc *crtc)
8798{
8799 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
8800 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
756f85cf 8801
391bf048
JN
8802 if (IS_BROADWELL(dev_priv) || INTEL_INFO(dev_priv)->gen >= 9) {
8803 u32 val = 0;
756f85cf 8804
6e3c9717 8805 switch (intel_crtc->config->pipe_bpp) {
756f85cf
PZ
8806 case 18:
8807 val |= PIPEMISC_DITHER_6_BPC;
8808 break;
8809 case 24:
8810 val |= PIPEMISC_DITHER_8_BPC;
8811 break;
8812 case 30:
8813 val |= PIPEMISC_DITHER_10_BPC;
8814 break;
8815 case 36:
8816 val |= PIPEMISC_DITHER_12_BPC;
8817 break;
8818 default:
8819 /* Case prevented by pipe_config_set_bpp. */
8820 BUG();
8821 }
8822
6e3c9717 8823 if (intel_crtc->config->dither)
756f85cf
PZ
8824 val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
8825
391bf048 8826 I915_WRITE(PIPEMISC(intel_crtc->pipe), val);
756f85cf 8827 }
ee2b0b38
PZ
8828}
8829
6591c6e4 8830static bool ironlake_compute_clocks(struct drm_crtc *crtc,
190f68c5 8831 struct intel_crtc_state *crtc_state,
6591c6e4
PZ
8832 intel_clock_t *clock,
8833 bool *has_reduced_clock,
8834 intel_clock_t *reduced_clock)
8835{
8836 struct drm_device *dev = crtc->dev;
8837 struct drm_i915_private *dev_priv = dev->dev_private;
6591c6e4 8838 int refclk;
d4906093 8839 const intel_limit_t *limit;
c329a4ec 8840 bool ret;
79e53945 8841
55bb9992 8842 refclk = ironlake_get_refclk(crtc_state);
79e53945 8843
d4906093
ML
8844 /*
8845 * Returns a set of divisors for the desired target clock with the given
8846 * refclk, or FALSE. The returned values represent the clock equation:
8847 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
8848 */
a93e255f
ACO
8849 limit = intel_limit(crtc_state, refclk);
8850 ret = dev_priv->display.find_dpll(limit, crtc_state,
190f68c5 8851 crtc_state->port_clock,
ee9300bb 8852 refclk, NULL, clock);
6591c6e4
PZ
8853 if (!ret)
8854 return false;
cda4b7d3 8855
6591c6e4
PZ
8856 return true;
8857}
8858
d4b1931c
PZ
8859int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
8860{
8861 /*
8862 * Account for spread spectrum to avoid
8863 * oversubscribing the link. Max center spread
8864 * is 2.5%; use 5% for safety's sake.
8865 */
8866 u32 bps = target_clock * bpp * 21 / 20;
619d4d04 8867 return DIV_ROUND_UP(bps, link_bw * 8);
d4b1931c
PZ
8868}
8869
7429e9d4 8870static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
6cf86a5e 8871{
7429e9d4 8872 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
f48d8f23
PZ
8873}
8874
de13a2e3 8875static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
190f68c5 8876 struct intel_crtc_state *crtc_state,
7429e9d4 8877 u32 *fp,
9a7c7890 8878 intel_clock_t *reduced_clock, u32 *fp2)
79e53945 8879{
de13a2e3 8880 struct drm_crtc *crtc = &intel_crtc->base;
79e53945
JB
8881 struct drm_device *dev = crtc->dev;
8882 struct drm_i915_private *dev_priv = dev->dev_private;
55bb9992 8883 struct drm_atomic_state *state = crtc_state->base.state;
da3ced29 8884 struct drm_connector *connector;
55bb9992
ACO
8885 struct drm_connector_state *connector_state;
8886 struct intel_encoder *encoder;
de13a2e3 8887 uint32_t dpll;
55bb9992 8888 int factor, num_connectors = 0, i;
09ede541 8889 bool is_lvds = false, is_sdvo = false;
79e53945 8890
da3ced29 8891 for_each_connector_in_state(state, connector, connector_state, i) {
55bb9992
ACO
8892 if (connector_state->crtc != crtc_state->base.crtc)
8893 continue;
8894
8895 encoder = to_intel_encoder(connector_state->best_encoder);
8896
8897 switch (encoder->type) {
79e53945
JB
8898 case INTEL_OUTPUT_LVDS:
8899 is_lvds = true;
8900 break;
8901 case INTEL_OUTPUT_SDVO:
7d57382e 8902 case INTEL_OUTPUT_HDMI:
79e53945 8903 is_sdvo = true;
79e53945 8904 break;
6847d71b
PZ
8905 default:
8906 break;
79e53945 8907 }
43565a06 8908
c751ce4f 8909 num_connectors++;
79e53945 8910 }
79e53945 8911
c1858123 8912 /* Enable autotuning of the PLL clock (if permissible) */
8febb297
EA
8913 factor = 21;
8914 if (is_lvds) {
8915 if ((intel_panel_use_ssc(dev_priv) &&
e91e941b 8916 dev_priv->vbt.lvds_ssc_freq == 100000) ||
f0b44056 8917 (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
8febb297 8918 factor = 25;
190f68c5 8919 } else if (crtc_state->sdvo_tv_clock)
8febb297 8920 factor = 20;
c1858123 8921
190f68c5 8922 if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
7d0ac5b7 8923 *fp |= FP_CB_TUNE;
2c07245f 8924
9a7c7890
DV
8925 if (fp2 && (reduced_clock->m < factor * reduced_clock->n))
8926 *fp2 |= FP_CB_TUNE;
8927
5eddb70b 8928 dpll = 0;
2c07245f 8929
a07d6787
EA
8930 if (is_lvds)
8931 dpll |= DPLLB_MODE_LVDS;
8932 else
8933 dpll |= DPLLB_MODE_DAC_SERIAL;
198a037f 8934
190f68c5 8935 dpll |= (crtc_state->pixel_multiplier - 1)
ef1b460d 8936 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
198a037f
DV
8937
8938 if (is_sdvo)
4a33e48d 8939 dpll |= DPLL_SDVO_HIGH_SPEED;
190f68c5 8940 if (crtc_state->has_dp_encoder)
4a33e48d 8941 dpll |= DPLL_SDVO_HIGH_SPEED;
79e53945 8942
a07d6787 8943 /* compute bitmask from p1 value */
190f68c5 8944 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
a07d6787 8945 /* also FPA1 */
190f68c5 8946 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
a07d6787 8947
190f68c5 8948 switch (crtc_state->dpll.p2) {
a07d6787
EA
8949 case 5:
8950 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
8951 break;
8952 case 7:
8953 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
8954 break;
8955 case 10:
8956 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
8957 break;
8958 case 14:
8959 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
8960 break;
79e53945
JB
8961 }
8962
b4c09f3b 8963 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
43565a06 8964 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
79e53945
JB
8965 else
8966 dpll |= PLL_REF_INPUT_DREFCLK;
8967
959e16d6 8968 return dpll | DPLL_VCO_ENABLE;
de13a2e3
PZ
8969}
8970
190f68c5
ACO
8971static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
8972 struct intel_crtc_state *crtc_state)
de13a2e3 8973{
c7653199 8974 struct drm_device *dev = crtc->base.dev;
de13a2e3 8975 intel_clock_t clock, reduced_clock;
cbbab5bd 8976 u32 dpll = 0, fp = 0, fp2 = 0;
e2f12b07 8977 bool ok, has_reduced_clock = false;
8b47047b 8978 bool is_lvds = false;
e2b78267 8979 struct intel_shared_dpll *pll;
de13a2e3 8980
dd3cd74a
ACO
8981 memset(&crtc_state->dpll_hw_state, 0,
8982 sizeof(crtc_state->dpll_hw_state));
8983
7905df29 8984 is_lvds = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS);
79e53945 8985
5dc5298b
PZ
8986 WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
8987 "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
a07d6787 8988
190f68c5 8989 ok = ironlake_compute_clocks(&crtc->base, crtc_state, &clock,
de13a2e3 8990 &has_reduced_clock, &reduced_clock);
190f68c5 8991 if (!ok && !crtc_state->clock_set) {
de13a2e3
PZ
8992 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8993 return -EINVAL;
79e53945 8994 }
f47709a9 8995 /* Compat-code for transition, will disappear. */
190f68c5
ACO
8996 if (!crtc_state->clock_set) {
8997 crtc_state->dpll.n = clock.n;
8998 crtc_state->dpll.m1 = clock.m1;
8999 crtc_state->dpll.m2 = clock.m2;
9000 crtc_state->dpll.p1 = clock.p1;
9001 crtc_state->dpll.p2 = clock.p2;
f47709a9 9002 }
79e53945 9003
5dc5298b 9004 /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
190f68c5
ACO
9005 if (crtc_state->has_pch_encoder) {
9006 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
cbbab5bd 9007 if (has_reduced_clock)
7429e9d4 9008 fp2 = i9xx_dpll_compute_fp(&reduced_clock);
cbbab5bd 9009
190f68c5 9010 dpll = ironlake_compute_dpll(crtc, crtc_state,
cbbab5bd
DV
9011 &fp, &reduced_clock,
9012 has_reduced_clock ? &fp2 : NULL);
9013
190f68c5
ACO
9014 crtc_state->dpll_hw_state.dpll = dpll;
9015 crtc_state->dpll_hw_state.fp0 = fp;
66e985c0 9016 if (has_reduced_clock)
190f68c5 9017 crtc_state->dpll_hw_state.fp1 = fp2;
66e985c0 9018 else
190f68c5 9019 crtc_state->dpll_hw_state.fp1 = fp;
66e985c0 9020
daedf20a 9021 pll = intel_get_shared_dpll(crtc, crtc_state, NULL);
ee7b9f93 9022 if (pll == NULL) {
84f44ce7 9023 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
c7653199 9024 pipe_name(crtc->pipe));
4b645f14
JB
9025 return -EINVAL;
9026 }
3fb37703 9027 }
79e53945 9028
ab585dea 9029 if (is_lvds && has_reduced_clock)
c7653199 9030 crtc->lowfreq_avail = true;
bcd644e0 9031 else
c7653199 9032 crtc->lowfreq_avail = false;
e2b78267 9033
c8f7a0db 9034 return 0;
79e53945
JB
9035}
9036
eb14cb74
VS
9037static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
9038 struct intel_link_m_n *m_n)
9039{
9040 struct drm_device *dev = crtc->base.dev;
9041 struct drm_i915_private *dev_priv = dev->dev_private;
9042 enum pipe pipe = crtc->pipe;
9043
9044 m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
9045 m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
9046 m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
9047 & ~TU_SIZE_MASK;
9048 m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
9049 m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
9050 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
9051}
9052
9053static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
9054 enum transcoder transcoder,
b95af8be
VK
9055 struct intel_link_m_n *m_n,
9056 struct intel_link_m_n *m2_n2)
72419203
DV
9057{
9058 struct drm_device *dev = crtc->base.dev;
9059 struct drm_i915_private *dev_priv = dev->dev_private;
eb14cb74 9060 enum pipe pipe = crtc->pipe;
72419203 9061
eb14cb74
VS
9062 if (INTEL_INFO(dev)->gen >= 5) {
9063 m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
9064 m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
9065 m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
9066 & ~TU_SIZE_MASK;
9067 m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
9068 m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
9069 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
b95af8be
VK
9070 /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
9071 * gen < 8) and if DRRS is supported (to make sure the
9072 * registers are not unnecessarily read).
9073 */
9074 if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
6e3c9717 9075 crtc->config->has_drrs) {
b95af8be
VK
9076 m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
9077 m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
9078 m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
9079 & ~TU_SIZE_MASK;
9080 m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
9081 m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
9082 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
9083 }
eb14cb74
VS
9084 } else {
9085 m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
9086 m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
9087 m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
9088 & ~TU_SIZE_MASK;
9089 m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
9090 m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
9091 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
9092 }
9093}
9094
9095void intel_dp_get_m_n(struct intel_crtc *crtc,
5cec258b 9096 struct intel_crtc_state *pipe_config)
eb14cb74 9097{
681a8504 9098 if (pipe_config->has_pch_encoder)
eb14cb74
VS
9099 intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
9100 else
9101 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
b95af8be
VK
9102 &pipe_config->dp_m_n,
9103 &pipe_config->dp_m2_n2);
eb14cb74 9104}
72419203 9105
eb14cb74 9106static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
5cec258b 9107 struct intel_crtc_state *pipe_config)
eb14cb74
VS
9108{
9109 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
b95af8be 9110 &pipe_config->fdi_m_n, NULL);
72419203
DV
9111}
9112
bd2e244f 9113static void skylake_get_pfit_config(struct intel_crtc *crtc,
5cec258b 9114 struct intel_crtc_state *pipe_config)
bd2e244f
JB
9115{
9116 struct drm_device *dev = crtc->base.dev;
9117 struct drm_i915_private *dev_priv = dev->dev_private;
a1b2278e
CK
9118 struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
9119 uint32_t ps_ctrl = 0;
9120 int id = -1;
9121 int i;
bd2e244f 9122
a1b2278e
CK
9123 /* find scaler attached to this pipe */
9124 for (i = 0; i < crtc->num_scalers; i++) {
9125 ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
9126 if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
9127 id = i;
9128 pipe_config->pch_pfit.enabled = true;
9129 pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
9130 pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
9131 break;
9132 }
9133 }
bd2e244f 9134
a1b2278e
CK
9135 scaler_state->scaler_id = id;
9136 if (id >= 0) {
9137 scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
9138 } else {
9139 scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
bd2e244f
JB
9140 }
9141}
9142
5724dbd1
DL
9143static void
9144skylake_get_initial_plane_config(struct intel_crtc *crtc,
9145 struct intel_initial_plane_config *plane_config)
bc8d7dff
DL
9146{
9147 struct drm_device *dev = crtc->base.dev;
9148 struct drm_i915_private *dev_priv = dev->dev_private;
40f46283 9149 u32 val, base, offset, stride_mult, tiling;
bc8d7dff
DL
9150 int pipe = crtc->pipe;
9151 int fourcc, pixel_format;
6761dd31 9152 unsigned int aligned_height;
bc8d7dff 9153 struct drm_framebuffer *fb;
1b842c89 9154 struct intel_framebuffer *intel_fb;
bc8d7dff 9155
d9806c9f 9156 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
1b842c89 9157 if (!intel_fb) {
bc8d7dff
DL
9158 DRM_DEBUG_KMS("failed to alloc fb\n");
9159 return;
9160 }
9161
1b842c89
DL
9162 fb = &intel_fb->base;
9163
bc8d7dff 9164 val = I915_READ(PLANE_CTL(pipe, 0));
42a7b088
DL
9165 if (!(val & PLANE_CTL_ENABLE))
9166 goto error;
9167
bc8d7dff
DL
9168 pixel_format = val & PLANE_CTL_FORMAT_MASK;
9169 fourcc = skl_format_to_fourcc(pixel_format,
9170 val & PLANE_CTL_ORDER_RGBX,
9171 val & PLANE_CTL_ALPHA_MASK);
9172 fb->pixel_format = fourcc;
9173 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
9174
40f46283
DL
9175 tiling = val & PLANE_CTL_TILED_MASK;
9176 switch (tiling) {
9177 case PLANE_CTL_TILED_LINEAR:
9178 fb->modifier[0] = DRM_FORMAT_MOD_NONE;
9179 break;
9180 case PLANE_CTL_TILED_X:
9181 plane_config->tiling = I915_TILING_X;
9182 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
9183 break;
9184 case PLANE_CTL_TILED_Y:
9185 fb->modifier[0] = I915_FORMAT_MOD_Y_TILED;
9186 break;
9187 case PLANE_CTL_TILED_YF:
9188 fb->modifier[0] = I915_FORMAT_MOD_Yf_TILED;
9189 break;
9190 default:
9191 MISSING_CASE(tiling);
9192 goto error;
9193 }
9194
bc8d7dff
DL
9195 base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
9196 plane_config->base = base;
9197
9198 offset = I915_READ(PLANE_OFFSET(pipe, 0));
9199
9200 val = I915_READ(PLANE_SIZE(pipe, 0));
9201 fb->height = ((val >> 16) & 0xfff) + 1;
9202 fb->width = ((val >> 0) & 0x1fff) + 1;
9203
9204 val = I915_READ(PLANE_STRIDE(pipe, 0));
7b49f948 9205 stride_mult = intel_fb_stride_alignment(dev_priv, fb->modifier[0],
40f46283 9206 fb->pixel_format);
bc8d7dff
DL
9207 fb->pitches[0] = (val & 0x3ff) * stride_mult;
9208
9209 aligned_height = intel_fb_align_height(dev, fb->height,
091df6cb
DV
9210 fb->pixel_format,
9211 fb->modifier[0]);
bc8d7dff 9212
f37b5c2b 9213 plane_config->size = fb->pitches[0] * aligned_height;
bc8d7dff
DL
9214
9215 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9216 pipe_name(pipe), fb->width, fb->height,
9217 fb->bits_per_pixel, base, fb->pitches[0],
9218 plane_config->size);
9219
2d14030b 9220 plane_config->fb = intel_fb;
bc8d7dff
DL
9221 return;
9222
9223error:
9224 kfree(fb);
9225}
9226
2fa2fe9a 9227static void ironlake_get_pfit_config(struct intel_crtc *crtc,
5cec258b 9228 struct intel_crtc_state *pipe_config)
2fa2fe9a
DV
9229{
9230 struct drm_device *dev = crtc->base.dev;
9231 struct drm_i915_private *dev_priv = dev->dev_private;
9232 uint32_t tmp;
9233
9234 tmp = I915_READ(PF_CTL(crtc->pipe));
9235
9236 if (tmp & PF_ENABLE) {
fd4daa9c 9237 pipe_config->pch_pfit.enabled = true;
2fa2fe9a
DV
9238 pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
9239 pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
cb8b2a30
DV
9240
9241 /* We currently do not free assignements of panel fitters on
9242 * ivb/hsw (since we don't use the higher upscaling modes which
9243 * differentiates them) so just WARN about this case for now. */
9244 if (IS_GEN7(dev)) {
9245 WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
9246 PF_PIPE_SEL_IVB(crtc->pipe));
9247 }
2fa2fe9a 9248 }
79e53945
JB
9249}
9250
5724dbd1
DL
9251static void
9252ironlake_get_initial_plane_config(struct intel_crtc *crtc,
9253 struct intel_initial_plane_config *plane_config)
4c6baa59
JB
9254{
9255 struct drm_device *dev = crtc->base.dev;
9256 struct drm_i915_private *dev_priv = dev->dev_private;
9257 u32 val, base, offset;
aeee5a49 9258 int pipe = crtc->pipe;
4c6baa59 9259 int fourcc, pixel_format;
6761dd31 9260 unsigned int aligned_height;
b113d5ee 9261 struct drm_framebuffer *fb;
1b842c89 9262 struct intel_framebuffer *intel_fb;
4c6baa59 9263
42a7b088
DL
9264 val = I915_READ(DSPCNTR(pipe));
9265 if (!(val & DISPLAY_PLANE_ENABLE))
9266 return;
9267
d9806c9f 9268 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
1b842c89 9269 if (!intel_fb) {
4c6baa59
JB
9270 DRM_DEBUG_KMS("failed to alloc fb\n");
9271 return;
9272 }
9273
1b842c89
DL
9274 fb = &intel_fb->base;
9275
18c5247e
DV
9276 if (INTEL_INFO(dev)->gen >= 4) {
9277 if (val & DISPPLANE_TILED) {
49af449b 9278 plane_config->tiling = I915_TILING_X;
18c5247e
DV
9279 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
9280 }
9281 }
4c6baa59
JB
9282
9283 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
b35d63fa 9284 fourcc = i9xx_format_to_fourcc(pixel_format);
b113d5ee
DL
9285 fb->pixel_format = fourcc;
9286 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
4c6baa59 9287
aeee5a49 9288 base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
4c6baa59 9289 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
aeee5a49 9290 offset = I915_READ(DSPOFFSET(pipe));
4c6baa59 9291 } else {
49af449b 9292 if (plane_config->tiling)
aeee5a49 9293 offset = I915_READ(DSPTILEOFF(pipe));
4c6baa59 9294 else
aeee5a49 9295 offset = I915_READ(DSPLINOFF(pipe));
4c6baa59
JB
9296 }
9297 plane_config->base = base;
9298
9299 val = I915_READ(PIPESRC(pipe));
b113d5ee
DL
9300 fb->width = ((val >> 16) & 0xfff) + 1;
9301 fb->height = ((val >> 0) & 0xfff) + 1;
4c6baa59
JB
9302
9303 val = I915_READ(DSPSTRIDE(pipe));
b113d5ee 9304 fb->pitches[0] = val & 0xffffffc0;
4c6baa59 9305
b113d5ee 9306 aligned_height = intel_fb_align_height(dev, fb->height,
091df6cb
DV
9307 fb->pixel_format,
9308 fb->modifier[0]);
4c6baa59 9309
f37b5c2b 9310 plane_config->size = fb->pitches[0] * aligned_height;
4c6baa59 9311
2844a921
DL
9312 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9313 pipe_name(pipe), fb->width, fb->height,
9314 fb->bits_per_pixel, base, fb->pitches[0],
9315 plane_config->size);
b113d5ee 9316
2d14030b 9317 plane_config->fb = intel_fb;
4c6baa59
JB
9318}
9319
0e8ffe1b 9320static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
5cec258b 9321 struct intel_crtc_state *pipe_config)
0e8ffe1b
DV
9322{
9323 struct drm_device *dev = crtc->base.dev;
9324 struct drm_i915_private *dev_priv = dev->dev_private;
1729050e 9325 enum intel_display_power_domain power_domain;
0e8ffe1b 9326 uint32_t tmp;
1729050e 9327 bool ret;
0e8ffe1b 9328
1729050e
ID
9329 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
9330 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
930e8c9e
PZ
9331 return false;
9332
e143a21c 9333 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
8106ddbd 9334 pipe_config->shared_dpll = NULL;
eccb140b 9335
1729050e 9336 ret = false;
0e8ffe1b
DV
9337 tmp = I915_READ(PIPECONF(crtc->pipe));
9338 if (!(tmp & PIPECONF_ENABLE))
1729050e 9339 goto out;
0e8ffe1b 9340
42571aef
VS
9341 switch (tmp & PIPECONF_BPC_MASK) {
9342 case PIPECONF_6BPC:
9343 pipe_config->pipe_bpp = 18;
9344 break;
9345 case PIPECONF_8BPC:
9346 pipe_config->pipe_bpp = 24;
9347 break;
9348 case PIPECONF_10BPC:
9349 pipe_config->pipe_bpp = 30;
9350 break;
9351 case PIPECONF_12BPC:
9352 pipe_config->pipe_bpp = 36;
9353 break;
9354 default:
9355 break;
9356 }
9357
b5a9fa09
DV
9358 if (tmp & PIPECONF_COLOR_RANGE_SELECT)
9359 pipe_config->limited_color_range = true;
9360
ab9412ba 9361 if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
66e985c0 9362 struct intel_shared_dpll *pll;
8106ddbd 9363 enum intel_dpll_id pll_id;
66e985c0 9364
88adfff1
DV
9365 pipe_config->has_pch_encoder = true;
9366
627eb5a3
DV
9367 tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
9368 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
9369 FDI_DP_PORT_WIDTH_SHIFT) + 1;
72419203
DV
9370
9371 ironlake_get_fdi_m_n_config(crtc, pipe_config);
6c49f241 9372
c0d43d62 9373 if (HAS_PCH_IBX(dev_priv->dev)) {
8106ddbd 9374 pll_id = (enum intel_dpll_id) crtc->pipe;
c0d43d62
DV
9375 } else {
9376 tmp = I915_READ(PCH_DPLL_SEL);
9377 if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
8106ddbd 9378 pll_id = DPLL_ID_PCH_PLL_B;
c0d43d62 9379 else
8106ddbd 9380 pll_id= DPLL_ID_PCH_PLL_A;
c0d43d62 9381 }
66e985c0 9382
8106ddbd
ACO
9383 pipe_config->shared_dpll =
9384 intel_get_shared_dpll_by_id(dev_priv, pll_id);
9385 pll = pipe_config->shared_dpll;
66e985c0 9386
2edd6443
ACO
9387 WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
9388 &pipe_config->dpll_hw_state));
c93f54cf
DV
9389
9390 tmp = pipe_config->dpll_hw_state.dpll;
9391 pipe_config->pixel_multiplier =
9392 ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
9393 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
18442d08
VS
9394
9395 ironlake_pch_clock_get(crtc, pipe_config);
6c49f241
DV
9396 } else {
9397 pipe_config->pixel_multiplier = 1;
627eb5a3
DV
9398 }
9399
1bd1bd80 9400 intel_get_pipe_timings(crtc, pipe_config);
bc58be60 9401 intel_get_pipe_src_size(crtc, pipe_config);
1bd1bd80 9402
2fa2fe9a
DV
9403 ironlake_get_pfit_config(crtc, pipe_config);
9404
1729050e
ID
9405 ret = true;
9406
9407out:
9408 intel_display_power_put(dev_priv, power_domain);
9409
9410 return ret;
0e8ffe1b
DV
9411}
9412
be256dc7
PZ
9413static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
9414{
9415 struct drm_device *dev = dev_priv->dev;
be256dc7 9416 struct intel_crtc *crtc;
be256dc7 9417
d3fcc808 9418 for_each_intel_crtc(dev, crtc)
e2c719b7 9419 I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
be256dc7
PZ
9420 pipe_name(crtc->pipe));
9421
e2c719b7
RC
9422 I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
9423 I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
01403de3
VS
9424 I915_STATE_WARN(I915_READ(WRPLL_CTL(0)) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
9425 I915_STATE_WARN(I915_READ(WRPLL_CTL(1)) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
e2c719b7
RC
9426 I915_STATE_WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
9427 I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
be256dc7 9428 "CPU PWM1 enabled\n");
c5107b87 9429 if (IS_HASWELL(dev))
e2c719b7 9430 I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
c5107b87 9431 "CPU PWM2 enabled\n");
e2c719b7 9432 I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
be256dc7 9433 "PCH PWM1 enabled\n");
e2c719b7 9434 I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
be256dc7 9435 "Utility pin enabled\n");
e2c719b7 9436 I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
be256dc7 9437
9926ada1
PZ
9438 /*
9439 * In theory we can still leave IRQs enabled, as long as only the HPD
9440 * interrupts remain enabled. We used to check for that, but since it's
9441 * gen-specific and since we only disable LCPLL after we fully disable
9442 * the interrupts, the check below should be enough.
9443 */
e2c719b7 9444 I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
be256dc7
PZ
9445}
9446
9ccd5aeb
PZ
9447static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
9448{
9449 struct drm_device *dev = dev_priv->dev;
9450
9451 if (IS_HASWELL(dev))
9452 return I915_READ(D_COMP_HSW);
9453 else
9454 return I915_READ(D_COMP_BDW);
9455}
9456
3c4c9b81
PZ
9457static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
9458{
9459 struct drm_device *dev = dev_priv->dev;
9460
9461 if (IS_HASWELL(dev)) {
9462 mutex_lock(&dev_priv->rps.hw_lock);
9463 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
9464 val))
f475dadf 9465 DRM_ERROR("Failed to write to D_COMP\n");
3c4c9b81
PZ
9466 mutex_unlock(&dev_priv->rps.hw_lock);
9467 } else {
9ccd5aeb
PZ
9468 I915_WRITE(D_COMP_BDW, val);
9469 POSTING_READ(D_COMP_BDW);
3c4c9b81 9470 }
be256dc7
PZ
9471}
9472
9473/*
9474 * This function implements pieces of two sequences from BSpec:
9475 * - Sequence for display software to disable LCPLL
9476 * - Sequence for display software to allow package C8+
9477 * The steps implemented here are just the steps that actually touch the LCPLL
9478 * register. Callers should take care of disabling all the display engine
9479 * functions, doing the mode unset, fixing interrupts, etc.
9480 */
6ff58d53
PZ
9481static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
9482 bool switch_to_fclk, bool allow_power_down)
be256dc7
PZ
9483{
9484 uint32_t val;
9485
9486 assert_can_disable_lcpll(dev_priv);
9487
9488 val = I915_READ(LCPLL_CTL);
9489
9490 if (switch_to_fclk) {
9491 val |= LCPLL_CD_SOURCE_FCLK;
9492 I915_WRITE(LCPLL_CTL, val);
9493
9494 if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
9495 LCPLL_CD_SOURCE_FCLK_DONE, 1))
9496 DRM_ERROR("Switching to FCLK failed\n");
9497
9498 val = I915_READ(LCPLL_CTL);
9499 }
9500
9501 val |= LCPLL_PLL_DISABLE;
9502 I915_WRITE(LCPLL_CTL, val);
9503 POSTING_READ(LCPLL_CTL);
9504
9505 if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
9506 DRM_ERROR("LCPLL still locked\n");
9507
9ccd5aeb 9508 val = hsw_read_dcomp(dev_priv);
be256dc7 9509 val |= D_COMP_COMP_DISABLE;
3c4c9b81 9510 hsw_write_dcomp(dev_priv, val);
be256dc7
PZ
9511 ndelay(100);
9512
9ccd5aeb
PZ
9513 if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
9514 1))
be256dc7
PZ
9515 DRM_ERROR("D_COMP RCOMP still in progress\n");
9516
9517 if (allow_power_down) {
9518 val = I915_READ(LCPLL_CTL);
9519 val |= LCPLL_POWER_DOWN_ALLOW;
9520 I915_WRITE(LCPLL_CTL, val);
9521 POSTING_READ(LCPLL_CTL);
9522 }
9523}
9524
9525/*
9526 * Fully restores LCPLL, disallowing power down and switching back to LCPLL
9527 * source.
9528 */
6ff58d53 9529static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
be256dc7
PZ
9530{
9531 uint32_t val;
9532
9533 val = I915_READ(LCPLL_CTL);
9534
9535 if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
9536 LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
9537 return;
9538
a8a8bd54
PZ
9539 /*
9540 * Make sure we're not on PC8 state before disabling PC8, otherwise
9541 * we'll hang the machine. To prevent PC8 state, just enable force_wake.
a8a8bd54 9542 */
59bad947 9543 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
215733fa 9544
be256dc7
PZ
9545 if (val & LCPLL_POWER_DOWN_ALLOW) {
9546 val &= ~LCPLL_POWER_DOWN_ALLOW;
9547 I915_WRITE(LCPLL_CTL, val);
35d8f2eb 9548 POSTING_READ(LCPLL_CTL);
be256dc7
PZ
9549 }
9550
9ccd5aeb 9551 val = hsw_read_dcomp(dev_priv);
be256dc7
PZ
9552 val |= D_COMP_COMP_FORCE;
9553 val &= ~D_COMP_COMP_DISABLE;
3c4c9b81 9554 hsw_write_dcomp(dev_priv, val);
be256dc7
PZ
9555
9556 val = I915_READ(LCPLL_CTL);
9557 val &= ~LCPLL_PLL_DISABLE;
9558 I915_WRITE(LCPLL_CTL, val);
9559
9560 if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
9561 DRM_ERROR("LCPLL not locked yet\n");
9562
9563 if (val & LCPLL_CD_SOURCE_FCLK) {
9564 val = I915_READ(LCPLL_CTL);
9565 val &= ~LCPLL_CD_SOURCE_FCLK;
9566 I915_WRITE(LCPLL_CTL, val);
9567
9568 if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
9569 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
9570 DRM_ERROR("Switching back to LCPLL failed\n");
9571 }
215733fa 9572
59bad947 9573 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
b6283055 9574 intel_update_cdclk(dev_priv->dev);
be256dc7
PZ
9575}
9576
765dab67
PZ
9577/*
9578 * Package states C8 and deeper are really deep PC states that can only be
9579 * reached when all the devices on the system allow it, so even if the graphics
9580 * device allows PC8+, it doesn't mean the system will actually get to these
9581 * states. Our driver only allows PC8+ when going into runtime PM.
9582 *
9583 * The requirements for PC8+ are that all the outputs are disabled, the power
9584 * well is disabled and most interrupts are disabled, and these are also
9585 * requirements for runtime PM. When these conditions are met, we manually do
9586 * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
9587 * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
9588 * hang the machine.
9589 *
9590 * When we really reach PC8 or deeper states (not just when we allow it) we lose
9591 * the state of some registers, so when we come back from PC8+ we need to
9592 * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
9593 * need to take care of the registers kept by RC6. Notice that this happens even
9594 * if we don't put the device in PCI D3 state (which is what currently happens
9595 * because of the runtime PM support).
9596 *
9597 * For more, read "Display Sequences for Package C8" on the hardware
9598 * documentation.
9599 */
a14cb6fc 9600void hsw_enable_pc8(struct drm_i915_private *dev_priv)
c67a470b 9601{
c67a470b
PZ
9602 struct drm_device *dev = dev_priv->dev;
9603 uint32_t val;
9604
c67a470b
PZ
9605 DRM_DEBUG_KMS("Enabling package C8+\n");
9606
c2699524 9607 if (HAS_PCH_LPT_LP(dev)) {
c67a470b
PZ
9608 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9609 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
9610 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9611 }
9612
9613 lpt_disable_clkout_dp(dev);
c67a470b
PZ
9614 hsw_disable_lcpll(dev_priv, true, true);
9615}
9616
a14cb6fc 9617void hsw_disable_pc8(struct drm_i915_private *dev_priv)
c67a470b
PZ
9618{
9619 struct drm_device *dev = dev_priv->dev;
9620 uint32_t val;
9621
c67a470b
PZ
9622 DRM_DEBUG_KMS("Disabling package C8+\n");
9623
9624 hsw_restore_lcpll(dev_priv);
c67a470b
PZ
9625 lpt_init_pch_refclk(dev);
9626
c2699524 9627 if (HAS_PCH_LPT_LP(dev)) {
c67a470b
PZ
9628 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9629 val |= PCH_LP_PARTITION_LEVEL_DISABLE;
9630 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9631 }
c67a470b
PZ
9632}
9633
27c329ed 9634static void broxton_modeset_commit_cdclk(struct drm_atomic_state *old_state)
f8437dd1 9635{
a821fc46 9636 struct drm_device *dev = old_state->dev;
1a617b77
ML
9637 struct intel_atomic_state *old_intel_state =
9638 to_intel_atomic_state(old_state);
9639 unsigned int req_cdclk = old_intel_state->dev_cdclk;
f8437dd1 9640
27c329ed 9641 broxton_set_cdclk(dev, req_cdclk);
f8437dd1
VK
9642}
9643
b432e5cf 9644/* compute the max rate for new configuration */
27c329ed 9645static int ilk_max_pixel_rate(struct drm_atomic_state *state)
b432e5cf 9646{
565602d7
ML
9647 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
9648 struct drm_i915_private *dev_priv = state->dev->dev_private;
9649 struct drm_crtc *crtc;
9650 struct drm_crtc_state *cstate;
27c329ed 9651 struct intel_crtc_state *crtc_state;
565602d7
ML
9652 unsigned max_pixel_rate = 0, i;
9653 enum pipe pipe;
b432e5cf 9654
565602d7
ML
9655 memcpy(intel_state->min_pixclk, dev_priv->min_pixclk,
9656 sizeof(intel_state->min_pixclk));
27c329ed 9657
565602d7
ML
9658 for_each_crtc_in_state(state, crtc, cstate, i) {
9659 int pixel_rate;
27c329ed 9660
565602d7
ML
9661 crtc_state = to_intel_crtc_state(cstate);
9662 if (!crtc_state->base.enable) {
9663 intel_state->min_pixclk[i] = 0;
b432e5cf 9664 continue;
565602d7 9665 }
b432e5cf 9666
27c329ed 9667 pixel_rate = ilk_pipe_pixel_rate(crtc_state);
b432e5cf
VS
9668
9669 /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
565602d7 9670 if (IS_BROADWELL(dev_priv) && crtc_state->ips_enabled)
b432e5cf
VS
9671 pixel_rate = DIV_ROUND_UP(pixel_rate * 100, 95);
9672
565602d7 9673 intel_state->min_pixclk[i] = pixel_rate;
b432e5cf
VS
9674 }
9675
565602d7
ML
9676 for_each_pipe(dev_priv, pipe)
9677 max_pixel_rate = max(intel_state->min_pixclk[pipe], max_pixel_rate);
9678
b432e5cf
VS
9679 return max_pixel_rate;
9680}
9681
9682static void broadwell_set_cdclk(struct drm_device *dev, int cdclk)
9683{
9684 struct drm_i915_private *dev_priv = dev->dev_private;
9685 uint32_t val, data;
9686 int ret;
9687
9688 if (WARN((I915_READ(LCPLL_CTL) &
9689 (LCPLL_PLL_DISABLE | LCPLL_PLL_LOCK |
9690 LCPLL_CD_CLOCK_DISABLE | LCPLL_ROOT_CD_CLOCK_DISABLE |
9691 LCPLL_CD2X_CLOCK_DISABLE | LCPLL_POWER_DOWN_ALLOW |
9692 LCPLL_CD_SOURCE_FCLK)) != LCPLL_PLL_LOCK,
9693 "trying to change cdclk frequency with cdclk not enabled\n"))
9694 return;
9695
9696 mutex_lock(&dev_priv->rps.hw_lock);
9697 ret = sandybridge_pcode_write(dev_priv,
9698 BDW_PCODE_DISPLAY_FREQ_CHANGE_REQ, 0x0);
9699 mutex_unlock(&dev_priv->rps.hw_lock);
9700 if (ret) {
9701 DRM_ERROR("failed to inform pcode about cdclk change\n");
9702 return;
9703 }
9704
9705 val = I915_READ(LCPLL_CTL);
9706 val |= LCPLL_CD_SOURCE_FCLK;
9707 I915_WRITE(LCPLL_CTL, val);
9708
5ba00178
TU
9709 if (wait_for_us(I915_READ(LCPLL_CTL) &
9710 LCPLL_CD_SOURCE_FCLK_DONE, 1))
b432e5cf
VS
9711 DRM_ERROR("Switching to FCLK failed\n");
9712
9713 val = I915_READ(LCPLL_CTL);
9714 val &= ~LCPLL_CLK_FREQ_MASK;
9715
9716 switch (cdclk) {
9717 case 450000:
9718 val |= LCPLL_CLK_FREQ_450;
9719 data = 0;
9720 break;
9721 case 540000:
9722 val |= LCPLL_CLK_FREQ_54O_BDW;
9723 data = 1;
9724 break;
9725 case 337500:
9726 val |= LCPLL_CLK_FREQ_337_5_BDW;
9727 data = 2;
9728 break;
9729 case 675000:
9730 val |= LCPLL_CLK_FREQ_675_BDW;
9731 data = 3;
9732 break;
9733 default:
9734 WARN(1, "invalid cdclk frequency\n");
9735 return;
9736 }
9737
9738 I915_WRITE(LCPLL_CTL, val);
9739
9740 val = I915_READ(LCPLL_CTL);
9741 val &= ~LCPLL_CD_SOURCE_FCLK;
9742 I915_WRITE(LCPLL_CTL, val);
9743
5ba00178
TU
9744 if (wait_for_us((I915_READ(LCPLL_CTL) &
9745 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
b432e5cf
VS
9746 DRM_ERROR("Switching back to LCPLL failed\n");
9747
9748 mutex_lock(&dev_priv->rps.hw_lock);
9749 sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ, data);
9750 mutex_unlock(&dev_priv->rps.hw_lock);
9751
9752 intel_update_cdclk(dev);
9753
9754 WARN(cdclk != dev_priv->cdclk_freq,
9755 "cdclk requested %d kHz but got %d kHz\n",
9756 cdclk, dev_priv->cdclk_freq);
9757}
9758
27c329ed 9759static int broadwell_modeset_calc_cdclk(struct drm_atomic_state *state)
b432e5cf 9760{
27c329ed 9761 struct drm_i915_private *dev_priv = to_i915(state->dev);
1a617b77 9762 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
27c329ed 9763 int max_pixclk = ilk_max_pixel_rate(state);
b432e5cf
VS
9764 int cdclk;
9765
9766 /*
9767 * FIXME should also account for plane ratio
9768 * once 64bpp pixel formats are supported.
9769 */
27c329ed 9770 if (max_pixclk > 540000)
b432e5cf 9771 cdclk = 675000;
27c329ed 9772 else if (max_pixclk > 450000)
b432e5cf 9773 cdclk = 540000;
27c329ed 9774 else if (max_pixclk > 337500)
b432e5cf
VS
9775 cdclk = 450000;
9776 else
9777 cdclk = 337500;
9778
b432e5cf 9779 if (cdclk > dev_priv->max_cdclk_freq) {
63ba534e
ML
9780 DRM_DEBUG_KMS("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
9781 cdclk, dev_priv->max_cdclk_freq);
9782 return -EINVAL;
b432e5cf
VS
9783 }
9784
1a617b77
ML
9785 intel_state->cdclk = intel_state->dev_cdclk = cdclk;
9786 if (!intel_state->active_crtcs)
9787 intel_state->dev_cdclk = 337500;
b432e5cf
VS
9788
9789 return 0;
9790}
9791
27c329ed 9792static void broadwell_modeset_commit_cdclk(struct drm_atomic_state *old_state)
b432e5cf 9793{
27c329ed 9794 struct drm_device *dev = old_state->dev;
1a617b77
ML
9795 struct intel_atomic_state *old_intel_state =
9796 to_intel_atomic_state(old_state);
9797 unsigned req_cdclk = old_intel_state->dev_cdclk;
b432e5cf 9798
27c329ed 9799 broadwell_set_cdclk(dev, req_cdclk);
b432e5cf
VS
9800}
9801
190f68c5
ACO
9802static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
9803 struct intel_crtc_state *crtc_state)
09b4ddf9 9804{
af3997b5
MK
9805 struct intel_encoder *intel_encoder =
9806 intel_ddi_get_crtc_new_encoder(crtc_state);
9807
9808 if (intel_encoder->type != INTEL_OUTPUT_DSI) {
9809 if (!intel_ddi_pll_select(crtc, crtc_state))
9810 return -EINVAL;
9811 }
716c2e55 9812
c7653199 9813 crtc->lowfreq_avail = false;
644cef34 9814
c8f7a0db 9815 return 0;
79e53945
JB
9816}
9817
3760b59c
S
9818static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
9819 enum port port,
9820 struct intel_crtc_state *pipe_config)
9821{
8106ddbd
ACO
9822 enum intel_dpll_id id;
9823
3760b59c
S
9824 switch (port) {
9825 case PORT_A:
9826 pipe_config->ddi_pll_sel = SKL_DPLL0;
08250c4b 9827 id = DPLL_ID_SKL_DPLL0;
3760b59c
S
9828 break;
9829 case PORT_B:
9830 pipe_config->ddi_pll_sel = SKL_DPLL1;
08250c4b 9831 id = DPLL_ID_SKL_DPLL1;
3760b59c
S
9832 break;
9833 case PORT_C:
9834 pipe_config->ddi_pll_sel = SKL_DPLL2;
08250c4b 9835 id = DPLL_ID_SKL_DPLL2;
3760b59c
S
9836 break;
9837 default:
9838 DRM_ERROR("Incorrect port type\n");
8106ddbd 9839 return;
3760b59c 9840 }
8106ddbd
ACO
9841
9842 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
3760b59c
S
9843}
9844
96b7dfb7
S
9845static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
9846 enum port port,
5cec258b 9847 struct intel_crtc_state *pipe_config)
96b7dfb7 9848{
8106ddbd 9849 enum intel_dpll_id id;
a3c988ea 9850 u32 temp;
96b7dfb7
S
9851
9852 temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
9853 pipe_config->ddi_pll_sel = temp >> (port * 3 + 1);
9854
9855 switch (pipe_config->ddi_pll_sel) {
3148ade7 9856 case SKL_DPLL0:
a3c988ea
ACO
9857 id = DPLL_ID_SKL_DPLL0;
9858 break;
96b7dfb7 9859 case SKL_DPLL1:
8106ddbd 9860 id = DPLL_ID_SKL_DPLL1;
96b7dfb7
S
9861 break;
9862 case SKL_DPLL2:
8106ddbd 9863 id = DPLL_ID_SKL_DPLL2;
96b7dfb7
S
9864 break;
9865 case SKL_DPLL3:
8106ddbd 9866 id = DPLL_ID_SKL_DPLL3;
96b7dfb7 9867 break;
8106ddbd
ACO
9868 default:
9869 MISSING_CASE(pipe_config->ddi_pll_sel);
9870 return;
96b7dfb7 9871 }
8106ddbd
ACO
9872
9873 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
96b7dfb7
S
9874}
9875
7d2c8175
DL
9876static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
9877 enum port port,
5cec258b 9878 struct intel_crtc_state *pipe_config)
7d2c8175 9879{
8106ddbd
ACO
9880 enum intel_dpll_id id;
9881
7d2c8175
DL
9882 pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
9883
9884 switch (pipe_config->ddi_pll_sel) {
9885 case PORT_CLK_SEL_WRPLL1:
8106ddbd 9886 id = DPLL_ID_WRPLL1;
7d2c8175
DL
9887 break;
9888 case PORT_CLK_SEL_WRPLL2:
8106ddbd 9889 id = DPLL_ID_WRPLL2;
7d2c8175 9890 break;
00490c22 9891 case PORT_CLK_SEL_SPLL:
8106ddbd 9892 id = DPLL_ID_SPLL;
79bd23da 9893 break;
9d16da65
ACO
9894 case PORT_CLK_SEL_LCPLL_810:
9895 id = DPLL_ID_LCPLL_810;
9896 break;
9897 case PORT_CLK_SEL_LCPLL_1350:
9898 id = DPLL_ID_LCPLL_1350;
9899 break;
9900 case PORT_CLK_SEL_LCPLL_2700:
9901 id = DPLL_ID_LCPLL_2700;
9902 break;
8106ddbd
ACO
9903 default:
9904 MISSING_CASE(pipe_config->ddi_pll_sel);
9905 /* fall through */
9906 case PORT_CLK_SEL_NONE:
8106ddbd 9907 return;
7d2c8175 9908 }
8106ddbd
ACO
9909
9910 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
7d2c8175
DL
9911}
9912
cf30429e
JN
9913static bool hsw_get_transcoder_state(struct intel_crtc *crtc,
9914 struct intel_crtc_state *pipe_config,
9915 unsigned long *power_domain_mask)
9916{
9917 struct drm_device *dev = crtc->base.dev;
9918 struct drm_i915_private *dev_priv = dev->dev_private;
9919 enum intel_display_power_domain power_domain;
9920 u32 tmp;
9921
9922 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
9923
9924 /*
9925 * XXX: Do intel_display_power_get_if_enabled before reading this (for
9926 * consistency and less surprising code; it's in always on power).
9927 */
9928 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
9929 if (tmp & TRANS_DDI_FUNC_ENABLE) {
9930 enum pipe trans_edp_pipe;
9931 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
9932 default:
9933 WARN(1, "unknown pipe linked to edp transcoder\n");
9934 case TRANS_DDI_EDP_INPUT_A_ONOFF:
9935 case TRANS_DDI_EDP_INPUT_A_ON:
9936 trans_edp_pipe = PIPE_A;
9937 break;
9938 case TRANS_DDI_EDP_INPUT_B_ONOFF:
9939 trans_edp_pipe = PIPE_B;
9940 break;
9941 case TRANS_DDI_EDP_INPUT_C_ONOFF:
9942 trans_edp_pipe = PIPE_C;
9943 break;
9944 }
9945
9946 if (trans_edp_pipe == crtc->pipe)
9947 pipe_config->cpu_transcoder = TRANSCODER_EDP;
9948 }
9949
9950 power_domain = POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder);
9951 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
9952 return false;
9953 *power_domain_mask |= BIT(power_domain);
9954
9955 tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
9956
9957 return tmp & PIPECONF_ENABLE;
9958}
9959
26804afd 9960static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
5cec258b 9961 struct intel_crtc_state *pipe_config)
26804afd
DV
9962{
9963 struct drm_device *dev = crtc->base.dev;
9964 struct drm_i915_private *dev_priv = dev->dev_private;
d452c5b6 9965 struct intel_shared_dpll *pll;
26804afd
DV
9966 enum port port;
9967 uint32_t tmp;
9968
9969 tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
9970
9971 port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
9972
ef11bdb3 9973 if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev))
96b7dfb7 9974 skylake_get_ddi_pll(dev_priv, port, pipe_config);
3760b59c
S
9975 else if (IS_BROXTON(dev))
9976 bxt_get_ddi_pll(dev_priv, port, pipe_config);
96b7dfb7
S
9977 else
9978 haswell_get_ddi_pll(dev_priv, port, pipe_config);
9cd86933 9979
8106ddbd
ACO
9980 pll = pipe_config->shared_dpll;
9981 if (pll) {
2edd6443
ACO
9982 WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
9983 &pipe_config->dpll_hw_state));
d452c5b6
DV
9984 }
9985
26804afd
DV
9986 /*
9987 * Haswell has only FDI/PCH transcoder A. It is which is connected to
9988 * DDI E. So just check whether this pipe is wired to DDI E and whether
9989 * the PCH transcoder is on.
9990 */
ca370455
DL
9991 if (INTEL_INFO(dev)->gen < 9 &&
9992 (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
26804afd
DV
9993 pipe_config->has_pch_encoder = true;
9994
9995 tmp = I915_READ(FDI_RX_CTL(PIPE_A));
9996 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
9997 FDI_DP_PORT_WIDTH_SHIFT) + 1;
9998
9999 ironlake_get_fdi_m_n_config(crtc, pipe_config);
10000 }
10001}
10002
0e8ffe1b 10003static bool haswell_get_pipe_config(struct intel_crtc *crtc,
5cec258b 10004 struct intel_crtc_state *pipe_config)
0e8ffe1b
DV
10005{
10006 struct drm_device *dev = crtc->base.dev;
10007 struct drm_i915_private *dev_priv = dev->dev_private;
1729050e
ID
10008 enum intel_display_power_domain power_domain;
10009 unsigned long power_domain_mask;
cf30429e 10010 bool active;
0e8ffe1b 10011
1729050e
ID
10012 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
10013 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
b5482bd0 10014 return false;
1729050e
ID
10015 power_domain_mask = BIT(power_domain);
10016
8106ddbd 10017 pipe_config->shared_dpll = NULL;
c0d43d62 10018
cf30429e 10019 active = hsw_get_transcoder_state(crtc, pipe_config, &power_domain_mask);
eccb140b 10020
cf30429e 10021 if (!active)
1729050e 10022 goto out;
0e8ffe1b 10023
26804afd 10024 haswell_get_ddi_port_state(crtc, pipe_config);
627eb5a3 10025
1bd1bd80 10026 intel_get_pipe_timings(crtc, pipe_config);
bc58be60 10027 intel_get_pipe_src_size(crtc, pipe_config);
1bd1bd80 10028
a1b2278e
CK
10029 if (INTEL_INFO(dev)->gen >= 9) {
10030 skl_init_scalers(dev, crtc, pipe_config);
10031 }
10032
af99ceda
CK
10033 if (INTEL_INFO(dev)->gen >= 9) {
10034 pipe_config->scaler_state.scaler_id = -1;
10035 pipe_config->scaler_state.scaler_users &= ~(1 << SKL_CRTC_INDEX);
10036 }
10037
1729050e
ID
10038 power_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
10039 if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
10040 power_domain_mask |= BIT(power_domain);
1c132b44 10041 if (INTEL_INFO(dev)->gen >= 9)
bd2e244f 10042 skylake_get_pfit_config(crtc, pipe_config);
ff6d9f55 10043 else
1c132b44 10044 ironlake_get_pfit_config(crtc, pipe_config);
bd2e244f 10045 }
88adfff1 10046
e59150dc
JB
10047 if (IS_HASWELL(dev))
10048 pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
10049 (I915_READ(IPS_CTL) & IPS_ENABLE);
42db64ef 10050
ebb69c95
CT
10051 if (pipe_config->cpu_transcoder != TRANSCODER_EDP) {
10052 pipe_config->pixel_multiplier =
10053 I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
10054 } else {
10055 pipe_config->pixel_multiplier = 1;
10056 }
6c49f241 10057
1729050e
ID
10058out:
10059 for_each_power_domain(power_domain, power_domain_mask)
10060 intel_display_power_put(dev_priv, power_domain);
10061
cf30429e 10062 return active;
0e8ffe1b
DV
10063}
10064
55a08b3f
ML
10065static void i845_update_cursor(struct drm_crtc *crtc, u32 base,
10066 const struct intel_plane_state *plane_state)
560b85bb
CW
10067{
10068 struct drm_device *dev = crtc->dev;
10069 struct drm_i915_private *dev_priv = dev->dev_private;
10070 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
dc41c154 10071 uint32_t cntl = 0, size = 0;
560b85bb 10072
55a08b3f
ML
10073 if (plane_state && plane_state->visible) {
10074 unsigned int width = plane_state->base.crtc_w;
10075 unsigned int height = plane_state->base.crtc_h;
dc41c154
VS
10076 unsigned int stride = roundup_pow_of_two(width) * 4;
10077
10078 switch (stride) {
10079 default:
10080 WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
10081 width, stride);
10082 stride = 256;
10083 /* fallthrough */
10084 case 256:
10085 case 512:
10086 case 1024:
10087 case 2048:
10088 break;
4b0e333e
CW
10089 }
10090
dc41c154
VS
10091 cntl |= CURSOR_ENABLE |
10092 CURSOR_GAMMA_ENABLE |
10093 CURSOR_FORMAT_ARGB |
10094 CURSOR_STRIDE(stride);
10095
10096 size = (height << 12) | width;
4b0e333e 10097 }
560b85bb 10098
dc41c154
VS
10099 if (intel_crtc->cursor_cntl != 0 &&
10100 (intel_crtc->cursor_base != base ||
10101 intel_crtc->cursor_size != size ||
10102 intel_crtc->cursor_cntl != cntl)) {
10103 /* On these chipsets we can only modify the base/size/stride
10104 * whilst the cursor is disabled.
10105 */
0b87c24e
VS
10106 I915_WRITE(CURCNTR(PIPE_A), 0);
10107 POSTING_READ(CURCNTR(PIPE_A));
dc41c154 10108 intel_crtc->cursor_cntl = 0;
4b0e333e 10109 }
560b85bb 10110
99d1f387 10111 if (intel_crtc->cursor_base != base) {
0b87c24e 10112 I915_WRITE(CURBASE(PIPE_A), base);
99d1f387
VS
10113 intel_crtc->cursor_base = base;
10114 }
4726e0b0 10115
dc41c154
VS
10116 if (intel_crtc->cursor_size != size) {
10117 I915_WRITE(CURSIZE, size);
10118 intel_crtc->cursor_size = size;
4b0e333e 10119 }
560b85bb 10120
4b0e333e 10121 if (intel_crtc->cursor_cntl != cntl) {
0b87c24e
VS
10122 I915_WRITE(CURCNTR(PIPE_A), cntl);
10123 POSTING_READ(CURCNTR(PIPE_A));
4b0e333e 10124 intel_crtc->cursor_cntl = cntl;
560b85bb 10125 }
560b85bb
CW
10126}
10127
55a08b3f
ML
10128static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base,
10129 const struct intel_plane_state *plane_state)
65a21cd6
JB
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);
10134 int pipe = intel_crtc->pipe;
663f3122 10135 uint32_t cntl = 0;
4b0e333e 10136
55a08b3f 10137 if (plane_state && plane_state->visible) {
4b0e333e 10138 cntl = MCURSOR_GAMMA_ENABLE;
55a08b3f 10139 switch (plane_state->base.crtc_w) {
4726e0b0
SK
10140 case 64:
10141 cntl |= CURSOR_MODE_64_ARGB_AX;
10142 break;
10143 case 128:
10144 cntl |= CURSOR_MODE_128_ARGB_AX;
10145 break;
10146 case 256:
10147 cntl |= CURSOR_MODE_256_ARGB_AX;
10148 break;
10149 default:
55a08b3f 10150 MISSING_CASE(plane_state->base.crtc_w);
4726e0b0 10151 return;
65a21cd6 10152 }
4b0e333e 10153 cntl |= pipe << 28; /* Connect to correct pipe */
47bf17a7 10154
fc6f93bc 10155 if (HAS_DDI(dev))
47bf17a7 10156 cntl |= CURSOR_PIPE_CSC_ENABLE;
65a21cd6 10157
55a08b3f
ML
10158 if (plane_state->base.rotation == BIT(DRM_ROTATE_180))
10159 cntl |= CURSOR_ROTATE_180;
10160 }
4398ad45 10161
4b0e333e
CW
10162 if (intel_crtc->cursor_cntl != cntl) {
10163 I915_WRITE(CURCNTR(pipe), cntl);
10164 POSTING_READ(CURCNTR(pipe));
10165 intel_crtc->cursor_cntl = cntl;
65a21cd6 10166 }
4b0e333e 10167
65a21cd6 10168 /* and commit changes on next vblank */
5efb3e28
VS
10169 I915_WRITE(CURBASE(pipe), base);
10170 POSTING_READ(CURBASE(pipe));
99d1f387
VS
10171
10172 intel_crtc->cursor_base = base;
65a21cd6
JB
10173}
10174
cda4b7d3 10175/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
6b383a7f 10176static void intel_crtc_update_cursor(struct drm_crtc *crtc,
55a08b3f 10177 const struct intel_plane_state *plane_state)
cda4b7d3
CW
10178{
10179 struct drm_device *dev = crtc->dev;
10180 struct drm_i915_private *dev_priv = dev->dev_private;
10181 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10182 int pipe = intel_crtc->pipe;
55a08b3f
ML
10183 u32 base = intel_crtc->cursor_addr;
10184 u32 pos = 0;
cda4b7d3 10185
55a08b3f
ML
10186 if (plane_state) {
10187 int x = plane_state->base.crtc_x;
10188 int y = plane_state->base.crtc_y;
cda4b7d3 10189
55a08b3f
ML
10190 if (x < 0) {
10191 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
10192 x = -x;
10193 }
10194 pos |= x << CURSOR_X_SHIFT;
cda4b7d3 10195
55a08b3f
ML
10196 if (y < 0) {
10197 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
10198 y = -y;
10199 }
10200 pos |= y << CURSOR_Y_SHIFT;
10201
10202 /* ILK+ do this automagically */
10203 if (HAS_GMCH_DISPLAY(dev) &&
10204 plane_state->base.rotation == BIT(DRM_ROTATE_180)) {
10205 base += (plane_state->base.crtc_h *
10206 plane_state->base.crtc_w - 1) * 4;
10207 }
cda4b7d3 10208 }
cda4b7d3 10209
5efb3e28
VS
10210 I915_WRITE(CURPOS(pipe), pos);
10211
8ac54669 10212 if (IS_845G(dev) || IS_I865G(dev))
55a08b3f 10213 i845_update_cursor(crtc, base, plane_state);
5efb3e28 10214 else
55a08b3f 10215 i9xx_update_cursor(crtc, base, plane_state);
cda4b7d3
CW
10216}
10217
dc41c154
VS
10218static bool cursor_size_ok(struct drm_device *dev,
10219 uint32_t width, uint32_t height)
10220{
10221 if (width == 0 || height == 0)
10222 return false;
10223
10224 /*
10225 * 845g/865g are special in that they are only limited by
10226 * the width of their cursors, the height is arbitrary up to
10227 * the precision of the register. Everything else requires
10228 * square cursors, limited to a few power-of-two sizes.
10229 */
10230 if (IS_845G(dev) || IS_I865G(dev)) {
10231 if ((width & 63) != 0)
10232 return false;
10233
10234 if (width > (IS_845G(dev) ? 64 : 512))
10235 return false;
10236
10237 if (height > 1023)
10238 return false;
10239 } else {
10240 switch (width | height) {
10241 case 256:
10242 case 128:
10243 if (IS_GEN2(dev))
10244 return false;
10245 case 64:
10246 break;
10247 default:
10248 return false;
10249 }
10250 }
10251
10252 return true;
10253}
10254
79e53945 10255static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
7203425a 10256 u16 *blue, uint32_t start, uint32_t size)
79e53945 10257{
7203425a 10258 int end = (start + size > 256) ? 256 : start + size, i;
79e53945 10259 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
79e53945 10260
7203425a 10261 for (i = start; i < end; i++) {
79e53945
JB
10262 intel_crtc->lut_r[i] = red[i] >> 8;
10263 intel_crtc->lut_g[i] = green[i] >> 8;
10264 intel_crtc->lut_b[i] = blue[i] >> 8;
10265 }
10266
10267 intel_crtc_load_lut(crtc);
10268}
10269
79e53945
JB
10270/* VESA 640x480x72Hz mode to set on the pipe */
10271static struct drm_display_mode load_detect_mode = {
10272 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
10273 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
10274};
10275
a8bb6818
DV
10276struct drm_framebuffer *
10277__intel_framebuffer_create(struct drm_device *dev,
10278 struct drm_mode_fb_cmd2 *mode_cmd,
10279 struct drm_i915_gem_object *obj)
d2dff872
CW
10280{
10281 struct intel_framebuffer *intel_fb;
10282 int ret;
10283
10284 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
dcb1394e 10285 if (!intel_fb)
d2dff872 10286 return ERR_PTR(-ENOMEM);
d2dff872
CW
10287
10288 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
dd4916c5
DV
10289 if (ret)
10290 goto err;
d2dff872
CW
10291
10292 return &intel_fb->base;
dcb1394e 10293
dd4916c5 10294err:
dd4916c5 10295 kfree(intel_fb);
dd4916c5 10296 return ERR_PTR(ret);
d2dff872
CW
10297}
10298
b5ea642a 10299static struct drm_framebuffer *
a8bb6818
DV
10300intel_framebuffer_create(struct drm_device *dev,
10301 struct drm_mode_fb_cmd2 *mode_cmd,
10302 struct drm_i915_gem_object *obj)
10303{
10304 struct drm_framebuffer *fb;
10305 int ret;
10306
10307 ret = i915_mutex_lock_interruptible(dev);
10308 if (ret)
10309 return ERR_PTR(ret);
10310 fb = __intel_framebuffer_create(dev, mode_cmd, obj);
10311 mutex_unlock(&dev->struct_mutex);
10312
10313 return fb;
10314}
10315
d2dff872
CW
10316static u32
10317intel_framebuffer_pitch_for_width(int width, int bpp)
10318{
10319 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
10320 return ALIGN(pitch, 64);
10321}
10322
10323static u32
10324intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
10325{
10326 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
1267a26b 10327 return PAGE_ALIGN(pitch * mode->vdisplay);
d2dff872
CW
10328}
10329
10330static struct drm_framebuffer *
10331intel_framebuffer_create_for_mode(struct drm_device *dev,
10332 struct drm_display_mode *mode,
10333 int depth, int bpp)
10334{
dcb1394e 10335 struct drm_framebuffer *fb;
d2dff872 10336 struct drm_i915_gem_object *obj;
0fed39bd 10337 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
d2dff872
CW
10338
10339 obj = i915_gem_alloc_object(dev,
10340 intel_framebuffer_size_for_mode(mode, bpp));
10341 if (obj == NULL)
10342 return ERR_PTR(-ENOMEM);
10343
10344 mode_cmd.width = mode->hdisplay;
10345 mode_cmd.height = mode->vdisplay;
308e5bcb
JB
10346 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
10347 bpp);
5ca0c34a 10348 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
d2dff872 10349
dcb1394e
LW
10350 fb = intel_framebuffer_create(dev, &mode_cmd, obj);
10351 if (IS_ERR(fb))
10352 drm_gem_object_unreference_unlocked(&obj->base);
10353
10354 return fb;
d2dff872
CW
10355}
10356
10357static struct drm_framebuffer *
10358mode_fits_in_fbdev(struct drm_device *dev,
10359 struct drm_display_mode *mode)
10360{
0695726e 10361#ifdef CONFIG_DRM_FBDEV_EMULATION
d2dff872
CW
10362 struct drm_i915_private *dev_priv = dev->dev_private;
10363 struct drm_i915_gem_object *obj;
10364 struct drm_framebuffer *fb;
10365
4c0e5528 10366 if (!dev_priv->fbdev)
d2dff872
CW
10367 return NULL;
10368
4c0e5528 10369 if (!dev_priv->fbdev->fb)
d2dff872
CW
10370 return NULL;
10371
4c0e5528
DV
10372 obj = dev_priv->fbdev->fb->obj;
10373 BUG_ON(!obj);
10374
8bcd4553 10375 fb = &dev_priv->fbdev->fb->base;
01f2c773
VS
10376 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
10377 fb->bits_per_pixel))
d2dff872
CW
10378 return NULL;
10379
01f2c773 10380 if (obj->base.size < mode->vdisplay * fb->pitches[0])
d2dff872
CW
10381 return NULL;
10382
edde3617 10383 drm_framebuffer_reference(fb);
d2dff872 10384 return fb;
4520f53a
DV
10385#else
10386 return NULL;
10387#endif
d2dff872
CW
10388}
10389
d3a40d1b
ACO
10390static int intel_modeset_setup_plane_state(struct drm_atomic_state *state,
10391 struct drm_crtc *crtc,
10392 struct drm_display_mode *mode,
10393 struct drm_framebuffer *fb,
10394 int x, int y)
10395{
10396 struct drm_plane_state *plane_state;
10397 int hdisplay, vdisplay;
10398 int ret;
10399
10400 plane_state = drm_atomic_get_plane_state(state, crtc->primary);
10401 if (IS_ERR(plane_state))
10402 return PTR_ERR(plane_state);
10403
10404 if (mode)
10405 drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
10406 else
10407 hdisplay = vdisplay = 0;
10408
10409 ret = drm_atomic_set_crtc_for_plane(plane_state, fb ? crtc : NULL);
10410 if (ret)
10411 return ret;
10412 drm_atomic_set_fb_for_plane(plane_state, fb);
10413 plane_state->crtc_x = 0;
10414 plane_state->crtc_y = 0;
10415 plane_state->crtc_w = hdisplay;
10416 plane_state->crtc_h = vdisplay;
10417 plane_state->src_x = x << 16;
10418 plane_state->src_y = y << 16;
10419 plane_state->src_w = hdisplay << 16;
10420 plane_state->src_h = vdisplay << 16;
10421
10422 return 0;
10423}
10424
d2434ab7 10425bool intel_get_load_detect_pipe(struct drm_connector *connector,
7173188d 10426 struct drm_display_mode *mode,
51fd371b
RC
10427 struct intel_load_detect_pipe *old,
10428 struct drm_modeset_acquire_ctx *ctx)
79e53945
JB
10429{
10430 struct intel_crtc *intel_crtc;
d2434ab7
DV
10431 struct intel_encoder *intel_encoder =
10432 intel_attached_encoder(connector);
79e53945 10433 struct drm_crtc *possible_crtc;
4ef69c7a 10434 struct drm_encoder *encoder = &intel_encoder->base;
79e53945
JB
10435 struct drm_crtc *crtc = NULL;
10436 struct drm_device *dev = encoder->dev;
94352cf9 10437 struct drm_framebuffer *fb;
51fd371b 10438 struct drm_mode_config *config = &dev->mode_config;
edde3617 10439 struct drm_atomic_state *state = NULL, *restore_state = NULL;
944b0c76 10440 struct drm_connector_state *connector_state;
4be07317 10441 struct intel_crtc_state *crtc_state;
51fd371b 10442 int ret, i = -1;
79e53945 10443
d2dff872 10444 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
c23cc417 10445 connector->base.id, connector->name,
8e329a03 10446 encoder->base.id, encoder->name);
d2dff872 10447
edde3617
ML
10448 old->restore_state = NULL;
10449
51fd371b
RC
10450retry:
10451 ret = drm_modeset_lock(&config->connection_mutex, ctx);
10452 if (ret)
ad3c558f 10453 goto fail;
6e9f798d 10454
79e53945
JB
10455 /*
10456 * Algorithm gets a little messy:
7a5e4805 10457 *
79e53945
JB
10458 * - if the connector already has an assigned crtc, use it (but make
10459 * sure it's on first)
7a5e4805 10460 *
79e53945
JB
10461 * - try to find the first unused crtc that can drive this connector,
10462 * and use that if we find one
79e53945
JB
10463 */
10464
10465 /* See if we already have a CRTC for this connector */
edde3617
ML
10466 if (connector->state->crtc) {
10467 crtc = connector->state->crtc;
8261b191 10468
51fd371b 10469 ret = drm_modeset_lock(&crtc->mutex, ctx);
4d02e2de 10470 if (ret)
ad3c558f 10471 goto fail;
8261b191
CW
10472
10473 /* Make sure the crtc and connector are running */
edde3617 10474 goto found;
79e53945
JB
10475 }
10476
10477 /* Find an unused one (if possible) */
70e1e0ec 10478 for_each_crtc(dev, possible_crtc) {
79e53945
JB
10479 i++;
10480 if (!(encoder->possible_crtcs & (1 << i)))
10481 continue;
edde3617
ML
10482
10483 ret = drm_modeset_lock(&possible_crtc->mutex, ctx);
10484 if (ret)
10485 goto fail;
10486
10487 if (possible_crtc->state->enable) {
10488 drm_modeset_unlock(&possible_crtc->mutex);
a459249c 10489 continue;
edde3617 10490 }
a459249c
VS
10491
10492 crtc = possible_crtc;
10493 break;
79e53945
JB
10494 }
10495
10496 /*
10497 * If we didn't find an unused CRTC, don't use any.
10498 */
10499 if (!crtc) {
7173188d 10500 DRM_DEBUG_KMS("no pipe available for load-detect\n");
ad3c558f 10501 goto fail;
79e53945
JB
10502 }
10503
edde3617
ML
10504found:
10505 intel_crtc = to_intel_crtc(crtc);
10506
4d02e2de
DV
10507 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
10508 if (ret)
ad3c558f 10509 goto fail;
79e53945 10510
83a57153 10511 state = drm_atomic_state_alloc(dev);
edde3617
ML
10512 restore_state = drm_atomic_state_alloc(dev);
10513 if (!state || !restore_state) {
10514 ret = -ENOMEM;
10515 goto fail;
10516 }
83a57153
ACO
10517
10518 state->acquire_ctx = ctx;
edde3617 10519 restore_state->acquire_ctx = ctx;
83a57153 10520
944b0c76
ACO
10521 connector_state = drm_atomic_get_connector_state(state, connector);
10522 if (IS_ERR(connector_state)) {
10523 ret = PTR_ERR(connector_state);
10524 goto fail;
10525 }
10526
edde3617
ML
10527 ret = drm_atomic_set_crtc_for_connector(connector_state, crtc);
10528 if (ret)
10529 goto fail;
944b0c76 10530
4be07317
ACO
10531 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
10532 if (IS_ERR(crtc_state)) {
10533 ret = PTR_ERR(crtc_state);
10534 goto fail;
10535 }
10536
49d6fa21 10537 crtc_state->base.active = crtc_state->base.enable = true;
4be07317 10538
6492711d
CW
10539 if (!mode)
10540 mode = &load_detect_mode;
79e53945 10541
d2dff872
CW
10542 /* We need a framebuffer large enough to accommodate all accesses
10543 * that the plane may generate whilst we perform load detection.
10544 * We can not rely on the fbcon either being present (we get called
10545 * during its initialisation to detect all boot displays, or it may
10546 * not even exist) or that it is large enough to satisfy the
10547 * requested mode.
10548 */
94352cf9
DV
10549 fb = mode_fits_in_fbdev(dev, mode);
10550 if (fb == NULL) {
d2dff872 10551 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
94352cf9 10552 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
d2dff872
CW
10553 } else
10554 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
94352cf9 10555 if (IS_ERR(fb)) {
d2dff872 10556 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
412b61d8 10557 goto fail;
79e53945 10558 }
79e53945 10559
d3a40d1b
ACO
10560 ret = intel_modeset_setup_plane_state(state, crtc, mode, fb, 0, 0);
10561 if (ret)
10562 goto fail;
10563
edde3617
ML
10564 drm_framebuffer_unreference(fb);
10565
10566 ret = drm_atomic_set_mode_for_crtc(&crtc_state->base, mode);
10567 if (ret)
10568 goto fail;
10569
10570 ret = PTR_ERR_OR_ZERO(drm_atomic_get_connector_state(restore_state, connector));
10571 if (!ret)
10572 ret = PTR_ERR_OR_ZERO(drm_atomic_get_crtc_state(restore_state, crtc));
10573 if (!ret)
10574 ret = PTR_ERR_OR_ZERO(drm_atomic_get_plane_state(restore_state, crtc->primary));
10575 if (ret) {
10576 DRM_DEBUG_KMS("Failed to create a copy of old state to restore: %i\n", ret);
10577 goto fail;
10578 }
8c7b5ccb 10579
3ba86073
ML
10580 ret = drm_atomic_commit(state);
10581 if (ret) {
6492711d 10582 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
412b61d8 10583 goto fail;
79e53945 10584 }
edde3617
ML
10585
10586 old->restore_state = restore_state;
7173188d 10587
79e53945 10588 /* let the connector get through one full cycle before testing */
9d0498a2 10589 intel_wait_for_vblank(dev, intel_crtc->pipe);
7173188d 10590 return true;
412b61d8 10591
ad3c558f 10592fail:
e5d958ef 10593 drm_atomic_state_free(state);
edde3617
ML
10594 drm_atomic_state_free(restore_state);
10595 restore_state = state = NULL;
83a57153 10596
51fd371b
RC
10597 if (ret == -EDEADLK) {
10598 drm_modeset_backoff(ctx);
10599 goto retry;
10600 }
10601
412b61d8 10602 return false;
79e53945
JB
10603}
10604
d2434ab7 10605void intel_release_load_detect_pipe(struct drm_connector *connector,
49172fee
ACO
10606 struct intel_load_detect_pipe *old,
10607 struct drm_modeset_acquire_ctx *ctx)
79e53945 10608{
d2434ab7
DV
10609 struct intel_encoder *intel_encoder =
10610 intel_attached_encoder(connector);
4ef69c7a 10611 struct drm_encoder *encoder = &intel_encoder->base;
edde3617 10612 struct drm_atomic_state *state = old->restore_state;
d3a40d1b 10613 int ret;
79e53945 10614
d2dff872 10615 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
c23cc417 10616 connector->base.id, connector->name,
8e329a03 10617 encoder->base.id, encoder->name);
d2dff872 10618
edde3617 10619 if (!state)
0622a53c 10620 return;
79e53945 10621
edde3617
ML
10622 ret = drm_atomic_commit(state);
10623 if (ret) {
10624 DRM_DEBUG_KMS("Couldn't release load detect pipe: %i\n", ret);
10625 drm_atomic_state_free(state);
10626 }
79e53945
JB
10627}
10628
da4a1efa 10629static int i9xx_pll_refclk(struct drm_device *dev,
5cec258b 10630 const struct intel_crtc_state *pipe_config)
da4a1efa
VS
10631{
10632 struct drm_i915_private *dev_priv = dev->dev_private;
10633 u32 dpll = pipe_config->dpll_hw_state.dpll;
10634
10635 if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
e91e941b 10636 return dev_priv->vbt.lvds_ssc_freq;
da4a1efa
VS
10637 else if (HAS_PCH_SPLIT(dev))
10638 return 120000;
10639 else if (!IS_GEN2(dev))
10640 return 96000;
10641 else
10642 return 48000;
10643}
10644
79e53945 10645/* Returns the clock of the currently programmed mode of the given pipe. */
f1f644dc 10646static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 10647 struct intel_crtc_state *pipe_config)
79e53945 10648{
f1f644dc 10649 struct drm_device *dev = crtc->base.dev;
79e53945 10650 struct drm_i915_private *dev_priv = dev->dev_private;
f1f644dc 10651 int pipe = pipe_config->cpu_transcoder;
293623f7 10652 u32 dpll = pipe_config->dpll_hw_state.dpll;
79e53945
JB
10653 u32 fp;
10654 intel_clock_t clock;
dccbea3b 10655 int port_clock;
da4a1efa 10656 int refclk = i9xx_pll_refclk(dev, pipe_config);
79e53945
JB
10657
10658 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
293623f7 10659 fp = pipe_config->dpll_hw_state.fp0;
79e53945 10660 else
293623f7 10661 fp = pipe_config->dpll_hw_state.fp1;
79e53945
JB
10662
10663 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
f2b115e6
AJ
10664 if (IS_PINEVIEW(dev)) {
10665 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
10666 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
2177832f
SL
10667 } else {
10668 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
10669 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
10670 }
10671
a6c45cf0 10672 if (!IS_GEN2(dev)) {
f2b115e6
AJ
10673 if (IS_PINEVIEW(dev))
10674 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
10675 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
2177832f
SL
10676 else
10677 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
79e53945
JB
10678 DPLL_FPA01_P1_POST_DIV_SHIFT);
10679
10680 switch (dpll & DPLL_MODE_MASK) {
10681 case DPLLB_MODE_DAC_SERIAL:
10682 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
10683 5 : 10;
10684 break;
10685 case DPLLB_MODE_LVDS:
10686 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
10687 7 : 14;
10688 break;
10689 default:
28c97730 10690 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
79e53945 10691 "mode\n", (int)(dpll & DPLL_MODE_MASK));
f1f644dc 10692 return;
79e53945
JB
10693 }
10694
ac58c3f0 10695 if (IS_PINEVIEW(dev))
dccbea3b 10696 port_clock = pnv_calc_dpll_params(refclk, &clock);
ac58c3f0 10697 else
dccbea3b 10698 port_clock = i9xx_calc_dpll_params(refclk, &clock);
79e53945 10699 } else {
0fb58223 10700 u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
b1c560d1 10701 bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
79e53945
JB
10702
10703 if (is_lvds) {
10704 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
10705 DPLL_FPA01_P1_POST_DIV_SHIFT);
b1c560d1
VS
10706
10707 if (lvds & LVDS_CLKB_POWER_UP)
10708 clock.p2 = 7;
10709 else
10710 clock.p2 = 14;
79e53945
JB
10711 } else {
10712 if (dpll & PLL_P1_DIVIDE_BY_TWO)
10713 clock.p1 = 2;
10714 else {
10715 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
10716 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
10717 }
10718 if (dpll & PLL_P2_DIVIDE_BY_4)
10719 clock.p2 = 4;
10720 else
10721 clock.p2 = 2;
79e53945 10722 }
da4a1efa 10723
dccbea3b 10724 port_clock = i9xx_calc_dpll_params(refclk, &clock);
79e53945
JB
10725 }
10726
18442d08
VS
10727 /*
10728 * This value includes pixel_multiplier. We will use
241bfc38 10729 * port_clock to compute adjusted_mode.crtc_clock in the
18442d08
VS
10730 * encoder's get_config() function.
10731 */
dccbea3b 10732 pipe_config->port_clock = port_clock;
f1f644dc
JB
10733}
10734
6878da05
VS
10735int intel_dotclock_calculate(int link_freq,
10736 const struct intel_link_m_n *m_n)
f1f644dc 10737{
f1f644dc
JB
10738 /*
10739 * The calculation for the data clock is:
1041a02f 10740 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
f1f644dc 10741 * But we want to avoid losing precison if possible, so:
1041a02f 10742 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
f1f644dc
JB
10743 *
10744 * and the link clock is simpler:
1041a02f 10745 * link_clock = (m * link_clock) / n
f1f644dc
JB
10746 */
10747
6878da05
VS
10748 if (!m_n->link_n)
10749 return 0;
f1f644dc 10750
6878da05
VS
10751 return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
10752}
f1f644dc 10753
18442d08 10754static void ironlake_pch_clock_get(struct intel_crtc *crtc,
5cec258b 10755 struct intel_crtc_state *pipe_config)
6878da05 10756{
e3b247da 10757 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
79e53945 10758
18442d08
VS
10759 /* read out port_clock from the DPLL */
10760 i9xx_crtc_clock_get(crtc, pipe_config);
f1f644dc 10761
f1f644dc 10762 /*
e3b247da
VS
10763 * In case there is an active pipe without active ports,
10764 * we may need some idea for the dotclock anyway.
10765 * Calculate one based on the FDI configuration.
79e53945 10766 */
2d112de7 10767 pipe_config->base.adjusted_mode.crtc_clock =
21a727b3 10768 intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
18442d08 10769 &pipe_config->fdi_m_n);
79e53945
JB
10770}
10771
10772/** Returns the currently programmed mode of the given pipe. */
10773struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
10774 struct drm_crtc *crtc)
10775{
548f245b 10776 struct drm_i915_private *dev_priv = dev->dev_private;
79e53945 10777 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6e3c9717 10778 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
79e53945 10779 struct drm_display_mode *mode;
3f36b937 10780 struct intel_crtc_state *pipe_config;
fe2b8f9d
PZ
10781 int htot = I915_READ(HTOTAL(cpu_transcoder));
10782 int hsync = I915_READ(HSYNC(cpu_transcoder));
10783 int vtot = I915_READ(VTOTAL(cpu_transcoder));
10784 int vsync = I915_READ(VSYNC(cpu_transcoder));
293623f7 10785 enum pipe pipe = intel_crtc->pipe;
79e53945
JB
10786
10787 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
10788 if (!mode)
10789 return NULL;
10790
3f36b937
TU
10791 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
10792 if (!pipe_config) {
10793 kfree(mode);
10794 return NULL;
10795 }
10796
f1f644dc
JB
10797 /*
10798 * Construct a pipe_config sufficient for getting the clock info
10799 * back out of crtc_clock_get.
10800 *
10801 * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
10802 * to use a real value here instead.
10803 */
3f36b937
TU
10804 pipe_config->cpu_transcoder = (enum transcoder) pipe;
10805 pipe_config->pixel_multiplier = 1;
10806 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(pipe));
10807 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(pipe));
10808 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(pipe));
10809 i9xx_crtc_clock_get(intel_crtc, pipe_config);
10810
10811 mode->clock = pipe_config->port_clock / pipe_config->pixel_multiplier;
79e53945
JB
10812 mode->hdisplay = (htot & 0xffff) + 1;
10813 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
10814 mode->hsync_start = (hsync & 0xffff) + 1;
10815 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
10816 mode->vdisplay = (vtot & 0xffff) + 1;
10817 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
10818 mode->vsync_start = (vsync & 0xffff) + 1;
10819 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
10820
10821 drm_mode_set_name(mode);
79e53945 10822
3f36b937
TU
10823 kfree(pipe_config);
10824
79e53945
JB
10825 return mode;
10826}
10827
f047e395
CW
10828void intel_mark_busy(struct drm_device *dev)
10829{
c67a470b
PZ
10830 struct drm_i915_private *dev_priv = dev->dev_private;
10831
f62a0076
CW
10832 if (dev_priv->mm.busy)
10833 return;
10834
43694d69 10835 intel_runtime_pm_get(dev_priv);
c67a470b 10836 i915_update_gfx_val(dev_priv);
43cf3bf0
CW
10837 if (INTEL_INFO(dev)->gen >= 6)
10838 gen6_rps_busy(dev_priv);
f62a0076 10839 dev_priv->mm.busy = true;
f047e395
CW
10840}
10841
10842void intel_mark_idle(struct drm_device *dev)
652c393a 10843{
c67a470b 10844 struct drm_i915_private *dev_priv = dev->dev_private;
652c393a 10845
f62a0076
CW
10846 if (!dev_priv->mm.busy)
10847 return;
10848
10849 dev_priv->mm.busy = false;
10850
3d13ef2e 10851 if (INTEL_INFO(dev)->gen >= 6)
b29c19b6 10852 gen6_rps_idle(dev->dev_private);
bb4cdd53 10853
43694d69 10854 intel_runtime_pm_put(dev_priv);
652c393a
JB
10855}
10856
79e53945
JB
10857static void intel_crtc_destroy(struct drm_crtc *crtc)
10858{
10859 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
67e77c5a
DV
10860 struct drm_device *dev = crtc->dev;
10861 struct intel_unpin_work *work;
67e77c5a 10862
5e2d7afc 10863 spin_lock_irq(&dev->event_lock);
67e77c5a
DV
10864 work = intel_crtc->unpin_work;
10865 intel_crtc->unpin_work = NULL;
5e2d7afc 10866 spin_unlock_irq(&dev->event_lock);
67e77c5a
DV
10867
10868 if (work) {
10869 cancel_work_sync(&work->work);
10870 kfree(work);
10871 }
79e53945
JB
10872
10873 drm_crtc_cleanup(crtc);
67e77c5a 10874
79e53945
JB
10875 kfree(intel_crtc);
10876}
10877
6b95a207
KH
10878static void intel_unpin_work_fn(struct work_struct *__work)
10879{
10880 struct intel_unpin_work *work =
10881 container_of(__work, struct intel_unpin_work, work);
a9ff8714
VS
10882 struct intel_crtc *crtc = to_intel_crtc(work->crtc);
10883 struct drm_device *dev = crtc->base.dev;
10884 struct drm_plane *primary = crtc->base.primary;
6b95a207 10885
b4a98e57 10886 mutex_lock(&dev->struct_mutex);
3465c580 10887 intel_unpin_fb_obj(work->old_fb, primary->state->rotation);
05394f39 10888 drm_gem_object_unreference(&work->pending_flip_obj->base);
d9e86c0e 10889
f06cc1b9 10890 if (work->flip_queued_req)
146d84f0 10891 i915_gem_request_assign(&work->flip_queued_req, NULL);
b4a98e57
CW
10892 mutex_unlock(&dev->struct_mutex);
10893
a9ff8714 10894 intel_frontbuffer_flip_complete(dev, to_intel_plane(primary)->frontbuffer_bit);
1eb52238 10895 intel_fbc_post_update(crtc);
89ed88ba 10896 drm_framebuffer_unreference(work->old_fb);
f99d7069 10897
a9ff8714
VS
10898 BUG_ON(atomic_read(&crtc->unpin_work_count) == 0);
10899 atomic_dec(&crtc->unpin_work_count);
b4a98e57 10900
6b95a207
KH
10901 kfree(work);
10902}
10903
1afe3e9d 10904static void do_intel_finish_page_flip(struct drm_device *dev,
49b14a5c 10905 struct drm_crtc *crtc)
6b95a207 10906{
6b95a207
KH
10907 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10908 struct intel_unpin_work *work;
6b95a207
KH
10909 unsigned long flags;
10910
10911 /* Ignore early vblank irqs */
10912 if (intel_crtc == NULL)
10913 return;
10914
f326038a
DV
10915 /*
10916 * This is called both by irq handlers and the reset code (to complete
10917 * lost pageflips) so needs the full irqsave spinlocks.
10918 */
6b95a207
KH
10919 spin_lock_irqsave(&dev->event_lock, flags);
10920 work = intel_crtc->unpin_work;
e7d841ca
CW
10921
10922 /* Ensure we don't miss a work->pending update ... */
10923 smp_rmb();
10924
10925 if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
6b95a207
KH
10926 spin_unlock_irqrestore(&dev->event_lock, flags);
10927 return;
10928 }
10929
d6bbafa1 10930 page_flip_completed(intel_crtc);
0af7e4df 10931
6b95a207 10932 spin_unlock_irqrestore(&dev->event_lock, flags);
6b95a207
KH
10933}
10934
1afe3e9d
JB
10935void intel_finish_page_flip(struct drm_device *dev, int pipe)
10936{
fbee40df 10937 struct drm_i915_private *dev_priv = dev->dev_private;
1afe3e9d
JB
10938 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
10939
49b14a5c 10940 do_intel_finish_page_flip(dev, crtc);
1afe3e9d
JB
10941}
10942
10943void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
10944{
fbee40df 10945 struct drm_i915_private *dev_priv = dev->dev_private;
1afe3e9d
JB
10946 struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
10947
49b14a5c 10948 do_intel_finish_page_flip(dev, crtc);
1afe3e9d
JB
10949}
10950
75f7f3ec
VS
10951/* Is 'a' after or equal to 'b'? */
10952static bool g4x_flip_count_after_eq(u32 a, u32 b)
10953{
10954 return !((a - b) & 0x80000000);
10955}
10956
10957static bool page_flip_finished(struct intel_crtc *crtc)
10958{
10959 struct drm_device *dev = crtc->base.dev;
10960 struct drm_i915_private *dev_priv = dev->dev_private;
10961
bdfa7542
VS
10962 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
10963 crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
10964 return true;
10965
75f7f3ec
VS
10966 /*
10967 * The relevant registers doen't exist on pre-ctg.
10968 * As the flip done interrupt doesn't trigger for mmio
10969 * flips on gmch platforms, a flip count check isn't
10970 * really needed there. But since ctg has the registers,
10971 * include it in the check anyway.
10972 */
10973 if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
10974 return true;
10975
e8861675
ML
10976 /*
10977 * BDW signals flip done immediately if the plane
10978 * is disabled, even if the plane enable is already
10979 * armed to occur at the next vblank :(
10980 */
10981
75f7f3ec
VS
10982 /*
10983 * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
10984 * used the same base address. In that case the mmio flip might
10985 * have completed, but the CS hasn't even executed the flip yet.
10986 *
10987 * A flip count check isn't enough as the CS might have updated
10988 * the base address just after start of vblank, but before we
10989 * managed to process the interrupt. This means we'd complete the
10990 * CS flip too soon.
10991 *
10992 * Combining both checks should get us a good enough result. It may
10993 * still happen that the CS flip has been executed, but has not
10994 * yet actually completed. But in case the base address is the same
10995 * anyway, we don't really care.
10996 */
10997 return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
10998 crtc->unpin_work->gtt_offset &&
fd8f507c 10999 g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_G4X(crtc->pipe)),
75f7f3ec
VS
11000 crtc->unpin_work->flip_count);
11001}
11002
6b95a207
KH
11003void intel_prepare_page_flip(struct drm_device *dev, int plane)
11004{
fbee40df 11005 struct drm_i915_private *dev_priv = dev->dev_private;
6b95a207
KH
11006 struct intel_crtc *intel_crtc =
11007 to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
11008 unsigned long flags;
11009
f326038a
DV
11010
11011 /*
11012 * This is called both by irq handlers and the reset code (to complete
11013 * lost pageflips) so needs the full irqsave spinlocks.
11014 *
11015 * NB: An MMIO update of the plane base pointer will also
e7d841ca
CW
11016 * generate a page-flip completion irq, i.e. every modeset
11017 * is also accompanied by a spurious intel_prepare_page_flip().
11018 */
6b95a207 11019 spin_lock_irqsave(&dev->event_lock, flags);
75f7f3ec 11020 if (intel_crtc->unpin_work && page_flip_finished(intel_crtc))
e7d841ca 11021 atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
6b95a207
KH
11022 spin_unlock_irqrestore(&dev->event_lock, flags);
11023}
11024
6042639c 11025static inline void intel_mark_page_flip_active(struct intel_unpin_work *work)
e7d841ca
CW
11026{
11027 /* Ensure that the work item is consistent when activating it ... */
11028 smp_wmb();
6042639c 11029 atomic_set(&work->pending, INTEL_FLIP_PENDING);
e7d841ca
CW
11030 /* and that it is marked active as soon as the irq could fire. */
11031 smp_wmb();
11032}
11033
8c9f3aaf
JB
11034static int intel_gen2_queue_flip(struct drm_device *dev,
11035 struct drm_crtc *crtc,
11036 struct drm_framebuffer *fb,
ed8d1975 11037 struct drm_i915_gem_object *obj,
6258fbe2 11038 struct drm_i915_gem_request *req,
ed8d1975 11039 uint32_t flags)
8c9f3aaf 11040{
4a570db5 11041 struct intel_engine_cs *engine = req->engine;
8c9f3aaf 11042 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8c9f3aaf
JB
11043 u32 flip_mask;
11044 int ret;
11045
5fb9de1a 11046 ret = intel_ring_begin(req, 6);
8c9f3aaf 11047 if (ret)
4fa62c89 11048 return ret;
8c9f3aaf
JB
11049
11050 /* Can't queue multiple flips, so wait for the previous
11051 * one to finish before executing the next.
11052 */
11053 if (intel_crtc->plane)
11054 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
11055 else
11056 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
e2f80391
TU
11057 intel_ring_emit(engine, MI_WAIT_FOR_EVENT | flip_mask);
11058 intel_ring_emit(engine, MI_NOOP);
11059 intel_ring_emit(engine, MI_DISPLAY_FLIP |
6d90c952 11060 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
e2f80391
TU
11061 intel_ring_emit(engine, fb->pitches[0]);
11062 intel_ring_emit(engine, intel_crtc->unpin_work->gtt_offset);
11063 intel_ring_emit(engine, 0); /* aux display base address, unused */
e7d841ca 11064
6042639c 11065 intel_mark_page_flip_active(intel_crtc->unpin_work);
83d4092b 11066 return 0;
8c9f3aaf
JB
11067}
11068
11069static int intel_gen3_queue_flip(struct drm_device *dev,
11070 struct drm_crtc *crtc,
11071 struct drm_framebuffer *fb,
ed8d1975 11072 struct drm_i915_gem_object *obj,
6258fbe2 11073 struct drm_i915_gem_request *req,
ed8d1975 11074 uint32_t flags)
8c9f3aaf 11075{
4a570db5 11076 struct intel_engine_cs *engine = req->engine;
8c9f3aaf 11077 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8c9f3aaf
JB
11078 u32 flip_mask;
11079 int ret;
11080
5fb9de1a 11081 ret = intel_ring_begin(req, 6);
8c9f3aaf 11082 if (ret)
4fa62c89 11083 return ret;
8c9f3aaf
JB
11084
11085 if (intel_crtc->plane)
11086 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
11087 else
11088 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
e2f80391
TU
11089 intel_ring_emit(engine, MI_WAIT_FOR_EVENT | flip_mask);
11090 intel_ring_emit(engine, MI_NOOP);
11091 intel_ring_emit(engine, MI_DISPLAY_FLIP_I915 |
6d90c952 11092 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
e2f80391
TU
11093 intel_ring_emit(engine, fb->pitches[0]);
11094 intel_ring_emit(engine, intel_crtc->unpin_work->gtt_offset);
11095 intel_ring_emit(engine, MI_NOOP);
6d90c952 11096
6042639c 11097 intel_mark_page_flip_active(intel_crtc->unpin_work);
83d4092b 11098 return 0;
8c9f3aaf
JB
11099}
11100
11101static int intel_gen4_queue_flip(struct drm_device *dev,
11102 struct drm_crtc *crtc,
11103 struct drm_framebuffer *fb,
ed8d1975 11104 struct drm_i915_gem_object *obj,
6258fbe2 11105 struct drm_i915_gem_request *req,
ed8d1975 11106 uint32_t flags)
8c9f3aaf 11107{
4a570db5 11108 struct intel_engine_cs *engine = req->engine;
8c9f3aaf
JB
11109 struct drm_i915_private *dev_priv = dev->dev_private;
11110 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11111 uint32_t pf, pipesrc;
11112 int ret;
11113
5fb9de1a 11114 ret = intel_ring_begin(req, 4);
8c9f3aaf 11115 if (ret)
4fa62c89 11116 return ret;
8c9f3aaf
JB
11117
11118 /* i965+ uses the linear or tiled offsets from the
11119 * Display Registers (which do not change across a page-flip)
11120 * so we need only reprogram the base address.
11121 */
e2f80391 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 |
c2c75131 11126 obj->tiling_mode);
8c9f3aaf
JB
11127
11128 /* XXX Enabling the panel-fitter across page-flip is so far
11129 * untested on non-native modes, so ignore it for now.
11130 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
11131 */
11132 pf = 0;
11133 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
e2f80391 11134 intel_ring_emit(engine, pf | pipesrc);
e7d841ca 11135
6042639c 11136 intel_mark_page_flip_active(intel_crtc->unpin_work);
83d4092b 11137 return 0;
8c9f3aaf
JB
11138}
11139
11140static int intel_gen6_queue_flip(struct drm_device *dev,
11141 struct drm_crtc *crtc,
11142 struct drm_framebuffer *fb,
ed8d1975 11143 struct drm_i915_gem_object *obj,
6258fbe2 11144 struct drm_i915_gem_request *req,
ed8d1975 11145 uint32_t flags)
8c9f3aaf 11146{
4a570db5 11147 struct intel_engine_cs *engine = req->engine;
8c9f3aaf
JB
11148 struct drm_i915_private *dev_priv = dev->dev_private;
11149 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11150 uint32_t pf, pipesrc;
11151 int ret;
11152
5fb9de1a 11153 ret = intel_ring_begin(req, 4);
8c9f3aaf 11154 if (ret)
4fa62c89 11155 return ret;
8c9f3aaf 11156
e2f80391 11157 intel_ring_emit(engine, MI_DISPLAY_FLIP |
6d90c952 11158 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
e2f80391
TU
11159 intel_ring_emit(engine, fb->pitches[0] | obj->tiling_mode);
11160 intel_ring_emit(engine, intel_crtc->unpin_work->gtt_offset);
8c9f3aaf 11161
dc257cf1
DV
11162 /* Contrary to the suggestions in the documentation,
11163 * "Enable Panel Fitter" does not seem to be required when page
11164 * flipping with a non-native mode, and worse causes a normal
11165 * modeset to fail.
11166 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
11167 */
11168 pf = 0;
8c9f3aaf 11169 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
e2f80391 11170 intel_ring_emit(engine, pf | pipesrc);
e7d841ca 11171
6042639c 11172 intel_mark_page_flip_active(intel_crtc->unpin_work);
83d4092b 11173 return 0;
8c9f3aaf
JB
11174}
11175
7c9017e5
JB
11176static int intel_gen7_queue_flip(struct drm_device *dev,
11177 struct drm_crtc *crtc,
11178 struct drm_framebuffer *fb,
ed8d1975 11179 struct drm_i915_gem_object *obj,
6258fbe2 11180 struct drm_i915_gem_request *req,
ed8d1975 11181 uint32_t flags)
7c9017e5 11182{
4a570db5 11183 struct intel_engine_cs *engine = req->engine;
7c9017e5 11184 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
cb05d8de 11185 uint32_t plane_bit = 0;
ffe74d75
CW
11186 int len, ret;
11187
eba905b2 11188 switch (intel_crtc->plane) {
cb05d8de
DV
11189 case PLANE_A:
11190 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
11191 break;
11192 case PLANE_B:
11193 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
11194 break;
11195 case PLANE_C:
11196 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
11197 break;
11198 default:
11199 WARN_ONCE(1, "unknown plane in flip command\n");
4fa62c89 11200 return -ENODEV;
cb05d8de
DV
11201 }
11202
ffe74d75 11203 len = 4;
e2f80391 11204 if (engine->id == RCS) {
ffe74d75 11205 len += 6;
f476828a
DL
11206 /*
11207 * On Gen 8, SRM is now taking an extra dword to accommodate
11208 * 48bits addresses, and we need a NOOP for the batch size to
11209 * stay even.
11210 */
11211 if (IS_GEN8(dev))
11212 len += 2;
11213 }
ffe74d75 11214
f66fab8e
VS
11215 /*
11216 * BSpec MI_DISPLAY_FLIP for IVB:
11217 * "The full packet must be contained within the same cache line."
11218 *
11219 * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
11220 * cacheline, if we ever start emitting more commands before
11221 * the MI_DISPLAY_FLIP we may need to first emit everything else,
11222 * then do the cacheline alignment, and finally emit the
11223 * MI_DISPLAY_FLIP.
11224 */
bba09b12 11225 ret = intel_ring_cacheline_align(req);
f66fab8e 11226 if (ret)
4fa62c89 11227 return ret;
f66fab8e 11228
5fb9de1a 11229 ret = intel_ring_begin(req, len);
7c9017e5 11230 if (ret)
4fa62c89 11231 return ret;
7c9017e5 11232
ffe74d75
CW
11233 /* Unmask the flip-done completion message. Note that the bspec says that
11234 * we should do this for both the BCS and RCS, and that we must not unmask
11235 * more than one flip event at any time (or ensure that one flip message
11236 * can be sent by waiting for flip-done prior to queueing new flips).
11237 * Experimentation says that BCS works despite DERRMR masking all
11238 * flip-done completion events and that unmasking all planes at once
11239 * for the RCS also doesn't appear to drop events. Setting the DERRMR
11240 * to zero does lead to lockups within MI_DISPLAY_FLIP.
11241 */
e2f80391
TU
11242 if (engine->id == RCS) {
11243 intel_ring_emit(engine, MI_LOAD_REGISTER_IMM(1));
11244 intel_ring_emit_reg(engine, DERRMR);
11245 intel_ring_emit(engine, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
11246 DERRMR_PIPEB_PRI_FLIP_DONE |
11247 DERRMR_PIPEC_PRI_FLIP_DONE));
f476828a 11248 if (IS_GEN8(dev))
e2f80391 11249 intel_ring_emit(engine, MI_STORE_REGISTER_MEM_GEN8 |
f476828a
DL
11250 MI_SRM_LRM_GLOBAL_GTT);
11251 else
e2f80391 11252 intel_ring_emit(engine, MI_STORE_REGISTER_MEM |
f476828a 11253 MI_SRM_LRM_GLOBAL_GTT);
e2f80391
TU
11254 intel_ring_emit_reg(engine, DERRMR);
11255 intel_ring_emit(engine, engine->scratch.gtt_offset + 256);
f476828a 11256 if (IS_GEN8(dev)) {
e2f80391
TU
11257 intel_ring_emit(engine, 0);
11258 intel_ring_emit(engine, MI_NOOP);
f476828a 11259 }
ffe74d75
CW
11260 }
11261
e2f80391
TU
11262 intel_ring_emit(engine, MI_DISPLAY_FLIP_I915 | plane_bit);
11263 intel_ring_emit(engine, (fb->pitches[0] | obj->tiling_mode));
11264 intel_ring_emit(engine, intel_crtc->unpin_work->gtt_offset);
11265 intel_ring_emit(engine, (MI_NOOP));
e7d841ca 11266
6042639c 11267 intel_mark_page_flip_active(intel_crtc->unpin_work);
83d4092b 11268 return 0;
7c9017e5
JB
11269}
11270
0bc40be8 11271static bool use_mmio_flip(struct intel_engine_cs *engine,
84c33a64
SG
11272 struct drm_i915_gem_object *obj)
11273{
11274 /*
11275 * This is not being used for older platforms, because
11276 * non-availability of flip done interrupt forces us to use
11277 * CS flips. Older platforms derive flip done using some clever
11278 * tricks involving the flip_pending status bits and vblank irqs.
11279 * So using MMIO flips there would disrupt this mechanism.
11280 */
11281
0bc40be8 11282 if (engine == NULL)
8e09bf83
CW
11283 return true;
11284
0bc40be8 11285 if (INTEL_INFO(engine->dev)->gen < 5)
84c33a64
SG
11286 return false;
11287
11288 if (i915.use_mmio_flip < 0)
11289 return false;
11290 else if (i915.use_mmio_flip > 0)
11291 return true;
14bf993e
OM
11292 else if (i915.enable_execlists)
11293 return true;
fd8e058a
AG
11294 else if (obj->base.dma_buf &&
11295 !reservation_object_test_signaled_rcu(obj->base.dma_buf->resv,
11296 false))
11297 return true;
84c33a64 11298 else
666796da 11299 return engine != i915_gem_request_get_engine(obj->last_write_req);
84c33a64
SG
11300}
11301
6042639c 11302static void skl_do_mmio_flip(struct intel_crtc *intel_crtc,
86efe24a 11303 unsigned int rotation,
6042639c 11304 struct intel_unpin_work *work)
ff944564
DL
11305{
11306 struct drm_device *dev = intel_crtc->base.dev;
11307 struct drm_i915_private *dev_priv = dev->dev_private;
11308 struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
ff944564 11309 const enum pipe pipe = intel_crtc->pipe;
86efe24a 11310 u32 ctl, stride, tile_height;
ff944564
DL
11311
11312 ctl = I915_READ(PLANE_CTL(pipe, 0));
11313 ctl &= ~PLANE_CTL_TILED_MASK;
2ebef630
TU
11314 switch (fb->modifier[0]) {
11315 case DRM_FORMAT_MOD_NONE:
11316 break;
11317 case I915_FORMAT_MOD_X_TILED:
ff944564 11318 ctl |= PLANE_CTL_TILED_X;
2ebef630
TU
11319 break;
11320 case I915_FORMAT_MOD_Y_TILED:
11321 ctl |= PLANE_CTL_TILED_Y;
11322 break;
11323 case I915_FORMAT_MOD_Yf_TILED:
11324 ctl |= PLANE_CTL_TILED_YF;
11325 break;
11326 default:
11327 MISSING_CASE(fb->modifier[0]);
11328 }
ff944564
DL
11329
11330 /*
11331 * The stride is either expressed as a multiple of 64 bytes chunks for
11332 * linear buffers or in number of tiles for tiled buffers.
11333 */
86efe24a
TU
11334 if (intel_rotation_90_or_270(rotation)) {
11335 /* stride = Surface height in tiles */
832be82f 11336 tile_height = intel_tile_height(dev_priv, fb->modifier[0], 0);
86efe24a
TU
11337 stride = DIV_ROUND_UP(fb->height, tile_height);
11338 } else {
11339 stride = fb->pitches[0] /
7b49f948
VS
11340 intel_fb_stride_alignment(dev_priv, fb->modifier[0],
11341 fb->pixel_format);
86efe24a 11342 }
ff944564
DL
11343
11344 /*
11345 * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
11346 * PLANE_SURF updates, the update is then guaranteed to be atomic.
11347 */
11348 I915_WRITE(PLANE_CTL(pipe, 0), ctl);
11349 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
11350
6042639c 11351 I915_WRITE(PLANE_SURF(pipe, 0), work->gtt_offset);
ff944564
DL
11352 POSTING_READ(PLANE_SURF(pipe, 0));
11353}
11354
6042639c
CW
11355static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc,
11356 struct intel_unpin_work *work)
84c33a64
SG
11357{
11358 struct drm_device *dev = intel_crtc->base.dev;
11359 struct drm_i915_private *dev_priv = dev->dev_private;
11360 struct intel_framebuffer *intel_fb =
11361 to_intel_framebuffer(intel_crtc->base.primary->fb);
11362 struct drm_i915_gem_object *obj = intel_fb->obj;
f0f59a00 11363 i915_reg_t reg = DSPCNTR(intel_crtc->plane);
84c33a64 11364 u32 dspcntr;
84c33a64 11365
84c33a64
SG
11366 dspcntr = I915_READ(reg);
11367
c5d97472
DL
11368 if (obj->tiling_mode != I915_TILING_NONE)
11369 dspcntr |= DISPPLANE_TILED;
11370 else
11371 dspcntr &= ~DISPPLANE_TILED;
11372
84c33a64
SG
11373 I915_WRITE(reg, dspcntr);
11374
6042639c 11375 I915_WRITE(DSPSURF(intel_crtc->plane), work->gtt_offset);
84c33a64 11376 POSTING_READ(DSPSURF(intel_crtc->plane));
ff944564
DL
11377}
11378
11379/*
11380 * XXX: This is the temporary way to update the plane registers until we get
11381 * around to using the usual plane update functions for MMIO flips
11382 */
6042639c 11383static void intel_do_mmio_flip(struct intel_mmio_flip *mmio_flip)
ff944564 11384{
6042639c
CW
11385 struct intel_crtc *crtc = mmio_flip->crtc;
11386 struct intel_unpin_work *work;
11387
11388 spin_lock_irq(&crtc->base.dev->event_lock);
11389 work = crtc->unpin_work;
11390 spin_unlock_irq(&crtc->base.dev->event_lock);
11391 if (work == NULL)
11392 return;
ff944564 11393
6042639c 11394 intel_mark_page_flip_active(work);
ff944564 11395
6042639c 11396 intel_pipe_update_start(crtc);
ff944564 11397
6042639c 11398 if (INTEL_INFO(mmio_flip->i915)->gen >= 9)
86efe24a 11399 skl_do_mmio_flip(crtc, mmio_flip->rotation, work);
ff944564
DL
11400 else
11401 /* use_mmio_flip() retricts MMIO flips to ilk+ */
6042639c 11402 ilk_do_mmio_flip(crtc, work);
ff944564 11403
6042639c 11404 intel_pipe_update_end(crtc);
84c33a64
SG
11405}
11406
9362c7c5 11407static void intel_mmio_flip_work_func(struct work_struct *work)
84c33a64 11408{
b2cfe0ab
CW
11409 struct intel_mmio_flip *mmio_flip =
11410 container_of(work, struct intel_mmio_flip, work);
fd8e058a
AG
11411 struct intel_framebuffer *intel_fb =
11412 to_intel_framebuffer(mmio_flip->crtc->base.primary->fb);
11413 struct drm_i915_gem_object *obj = intel_fb->obj;
84c33a64 11414
6042639c 11415 if (mmio_flip->req) {
eed29a5b 11416 WARN_ON(__i915_wait_request(mmio_flip->req,
b2cfe0ab 11417 mmio_flip->crtc->reset_counter,
bcafc4e3
CW
11418 false, NULL,
11419 &mmio_flip->i915->rps.mmioflips));
6042639c
CW
11420 i915_gem_request_unreference__unlocked(mmio_flip->req);
11421 }
84c33a64 11422
fd8e058a
AG
11423 /* For framebuffer backed by dmabuf, wait for fence */
11424 if (obj->base.dma_buf)
11425 WARN_ON(reservation_object_wait_timeout_rcu(obj->base.dma_buf->resv,
11426 false, false,
11427 MAX_SCHEDULE_TIMEOUT) < 0);
11428
6042639c 11429 intel_do_mmio_flip(mmio_flip);
b2cfe0ab 11430 kfree(mmio_flip);
84c33a64
SG
11431}
11432
11433static int intel_queue_mmio_flip(struct drm_device *dev,
11434 struct drm_crtc *crtc,
86efe24a 11435 struct drm_i915_gem_object *obj)
84c33a64 11436{
b2cfe0ab
CW
11437 struct intel_mmio_flip *mmio_flip;
11438
11439 mmio_flip = kmalloc(sizeof(*mmio_flip), GFP_KERNEL);
11440 if (mmio_flip == NULL)
11441 return -ENOMEM;
84c33a64 11442
bcafc4e3 11443 mmio_flip->i915 = to_i915(dev);
eed29a5b 11444 mmio_flip->req = i915_gem_request_reference(obj->last_write_req);
b2cfe0ab 11445 mmio_flip->crtc = to_intel_crtc(crtc);
86efe24a 11446 mmio_flip->rotation = crtc->primary->state->rotation;
536f5b5e 11447
b2cfe0ab
CW
11448 INIT_WORK(&mmio_flip->work, intel_mmio_flip_work_func);
11449 schedule_work(&mmio_flip->work);
84c33a64 11450
84c33a64
SG
11451 return 0;
11452}
11453
8c9f3aaf
JB
11454static int intel_default_queue_flip(struct drm_device *dev,
11455 struct drm_crtc *crtc,
11456 struct drm_framebuffer *fb,
ed8d1975 11457 struct drm_i915_gem_object *obj,
6258fbe2 11458 struct drm_i915_gem_request *req,
ed8d1975 11459 uint32_t flags)
8c9f3aaf
JB
11460{
11461 return -ENODEV;
11462}
11463
d6bbafa1
CW
11464static bool __intel_pageflip_stall_check(struct drm_device *dev,
11465 struct drm_crtc *crtc)
11466{
11467 struct drm_i915_private *dev_priv = dev->dev_private;
11468 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11469 struct intel_unpin_work *work = intel_crtc->unpin_work;
11470 u32 addr;
11471
11472 if (atomic_read(&work->pending) >= INTEL_FLIP_COMPLETE)
11473 return true;
11474
908565c2
CW
11475 if (atomic_read(&work->pending) < INTEL_FLIP_PENDING)
11476 return false;
11477
d6bbafa1
CW
11478 if (!work->enable_stall_check)
11479 return false;
11480
11481 if (work->flip_ready_vblank == 0) {
3a8a946e
DV
11482 if (work->flip_queued_req &&
11483 !i915_gem_request_completed(work->flip_queued_req, true))
d6bbafa1
CW
11484 return false;
11485
1e3feefd 11486 work->flip_ready_vblank = drm_crtc_vblank_count(crtc);
d6bbafa1
CW
11487 }
11488
1e3feefd 11489 if (drm_crtc_vblank_count(crtc) - work->flip_ready_vblank < 3)
d6bbafa1
CW
11490 return false;
11491
11492 /* Potential stall - if we see that the flip has happened,
11493 * assume a missed interrupt. */
11494 if (INTEL_INFO(dev)->gen >= 4)
11495 addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
11496 else
11497 addr = I915_READ(DSPADDR(intel_crtc->plane));
11498
11499 /* There is a potential issue here with a false positive after a flip
11500 * to the same address. We could address this by checking for a
11501 * non-incrementing frame counter.
11502 */
11503 return addr == work->gtt_offset;
11504}
11505
11506void intel_check_page_flip(struct drm_device *dev, int pipe)
11507{
11508 struct drm_i915_private *dev_priv = dev->dev_private;
11509 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
11510 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6ad790c0 11511 struct intel_unpin_work *work;
f326038a 11512
6c51d46f 11513 WARN_ON(!in_interrupt());
d6bbafa1
CW
11514
11515 if (crtc == NULL)
11516 return;
11517
f326038a 11518 spin_lock(&dev->event_lock);
6ad790c0
CW
11519 work = intel_crtc->unpin_work;
11520 if (work != NULL && __intel_pageflip_stall_check(dev, crtc)) {
d6bbafa1 11521 WARN_ONCE(1, "Kicking stuck page flip: queued at %d, now %d\n",
6ad790c0 11522 work->flip_queued_vblank, drm_vblank_count(dev, pipe));
d6bbafa1 11523 page_flip_completed(intel_crtc);
6ad790c0 11524 work = NULL;
d6bbafa1 11525 }
6ad790c0
CW
11526 if (work != NULL &&
11527 drm_vblank_count(dev, pipe) - work->flip_queued_vblank > 1)
11528 intel_queue_rps_boost_for_request(dev, work->flip_queued_req);
f326038a 11529 spin_unlock(&dev->event_lock);
d6bbafa1
CW
11530}
11531
6b95a207
KH
11532static int intel_crtc_page_flip(struct drm_crtc *crtc,
11533 struct drm_framebuffer *fb,
ed8d1975
KP
11534 struct drm_pending_vblank_event *event,
11535 uint32_t page_flip_flags)
6b95a207
KH
11536{
11537 struct drm_device *dev = crtc->dev;
11538 struct drm_i915_private *dev_priv = dev->dev_private;
f4510a27 11539 struct drm_framebuffer *old_fb = crtc->primary->fb;
2ff8fde1 11540 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
6b95a207 11541 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
455a6808 11542 struct drm_plane *primary = crtc->primary;
a071fa00 11543 enum pipe pipe = intel_crtc->pipe;
6b95a207 11544 struct intel_unpin_work *work;
e2f80391 11545 struct intel_engine_cs *engine;
cf5d8a46 11546 bool mmio_flip;
91af127f 11547 struct drm_i915_gem_request *request = NULL;
52e68630 11548 int ret;
6b95a207 11549
2ff8fde1
MR
11550 /*
11551 * drm_mode_page_flip_ioctl() should already catch this, but double
11552 * check to be safe. In the future we may enable pageflipping from
11553 * a disabled primary plane.
11554 */
11555 if (WARN_ON(intel_fb_obj(old_fb) == NULL))
11556 return -EBUSY;
11557
e6a595d2 11558 /* Can't change pixel format via MI display flips. */
f4510a27 11559 if (fb->pixel_format != crtc->primary->fb->pixel_format)
e6a595d2
VS
11560 return -EINVAL;
11561
11562 /*
11563 * TILEOFF/LINOFF registers can't be changed via MI display flips.
11564 * Note that pitch changes could also affect these register.
11565 */
11566 if (INTEL_INFO(dev)->gen > 3 &&
f4510a27
MR
11567 (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
11568 fb->pitches[0] != crtc->primary->fb->pitches[0]))
e6a595d2
VS
11569 return -EINVAL;
11570
f900db47
CW
11571 if (i915_terminally_wedged(&dev_priv->gpu_error))
11572 goto out_hang;
11573
b14c5679 11574 work = kzalloc(sizeof(*work), GFP_KERNEL);
6b95a207
KH
11575 if (work == NULL)
11576 return -ENOMEM;
11577
6b95a207 11578 work->event = event;
b4a98e57 11579 work->crtc = crtc;
ab8d6675 11580 work->old_fb = old_fb;
6b95a207
KH
11581 INIT_WORK(&work->work, intel_unpin_work_fn);
11582
87b6b101 11583 ret = drm_crtc_vblank_get(crtc);
7317c75e
JB
11584 if (ret)
11585 goto free_work;
11586
6b95a207 11587 /* We borrow the event spin lock for protecting unpin_work */
5e2d7afc 11588 spin_lock_irq(&dev->event_lock);
6b95a207 11589 if (intel_crtc->unpin_work) {
d6bbafa1
CW
11590 /* Before declaring the flip queue wedged, check if
11591 * the hardware completed the operation behind our backs.
11592 */
11593 if (__intel_pageflip_stall_check(dev, crtc)) {
11594 DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
11595 page_flip_completed(intel_crtc);
11596 } else {
11597 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
5e2d7afc 11598 spin_unlock_irq(&dev->event_lock);
468f0b44 11599
d6bbafa1
CW
11600 drm_crtc_vblank_put(crtc);
11601 kfree(work);
11602 return -EBUSY;
11603 }
6b95a207
KH
11604 }
11605 intel_crtc->unpin_work = work;
5e2d7afc 11606 spin_unlock_irq(&dev->event_lock);
6b95a207 11607
b4a98e57
CW
11608 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
11609 flush_workqueue(dev_priv->wq);
11610
75dfca80 11611 /* Reference the objects for the scheduled work. */
ab8d6675 11612 drm_framebuffer_reference(work->old_fb);
05394f39 11613 drm_gem_object_reference(&obj->base);
6b95a207 11614
f4510a27 11615 crtc->primary->fb = fb;
afd65eb4 11616 update_state_fb(crtc->primary);
e8216e50 11617 intel_fbc_pre_update(intel_crtc);
1ed1f968 11618
e1f99ce6 11619 work->pending_flip_obj = obj;
e1f99ce6 11620
89ed88ba
CW
11621 ret = i915_mutex_lock_interruptible(dev);
11622 if (ret)
11623 goto cleanup;
11624
b4a98e57 11625 atomic_inc(&intel_crtc->unpin_work_count);
10d83730 11626 intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
e1f99ce6 11627
75f7f3ec 11628 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
fd8f507c 11629 work->flip_count = I915_READ(PIPE_FLIPCOUNT_G4X(pipe)) + 1;
75f7f3ec 11630
666a4537 11631 if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
4a570db5 11632 engine = &dev_priv->engine[BCS];
ab8d6675 11633 if (obj->tiling_mode != intel_fb_obj(work->old_fb)->tiling_mode)
8e09bf83 11634 /* vlv: DISPLAY_FLIP fails to change tiling */
e2f80391 11635 engine = NULL;
48bf5b2d 11636 } else if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
4a570db5 11637 engine = &dev_priv->engine[BCS];
4fa62c89 11638 } else if (INTEL_INFO(dev)->gen >= 7) {
666796da 11639 engine = i915_gem_request_get_engine(obj->last_write_req);
e2f80391 11640 if (engine == NULL || engine->id != RCS)
4a570db5 11641 engine = &dev_priv->engine[BCS];
4fa62c89 11642 } else {
4a570db5 11643 engine = &dev_priv->engine[RCS];
4fa62c89
VS
11644 }
11645
e2f80391 11646 mmio_flip = use_mmio_flip(engine, obj);
cf5d8a46
CW
11647
11648 /* When using CS flips, we want to emit semaphores between rings.
11649 * However, when using mmio flips we will create a task to do the
11650 * synchronisation, so all we want here is to pin the framebuffer
11651 * into the display plane and skip any waits.
11652 */
7580d774 11653 if (!mmio_flip) {
e2f80391 11654 ret = i915_gem_object_sync(obj, engine, &request);
7580d774
ML
11655 if (ret)
11656 goto cleanup_pending;
11657 }
11658
3465c580 11659 ret = intel_pin_and_fence_fb_obj(fb, primary->state->rotation);
8c9f3aaf
JB
11660 if (ret)
11661 goto cleanup_pending;
6b95a207 11662
dedf278c
TU
11663 work->gtt_offset = intel_plane_obj_offset(to_intel_plane(primary),
11664 obj, 0);
11665 work->gtt_offset += intel_crtc->dspaddr_offset;
4fa62c89 11666
cf5d8a46 11667 if (mmio_flip) {
86efe24a 11668 ret = intel_queue_mmio_flip(dev, crtc, obj);
d6bbafa1
CW
11669 if (ret)
11670 goto cleanup_unpin;
11671
f06cc1b9
JH
11672 i915_gem_request_assign(&work->flip_queued_req,
11673 obj->last_write_req);
d6bbafa1 11674 } else {
6258fbe2 11675 if (!request) {
e2f80391 11676 request = i915_gem_request_alloc(engine, NULL);
26827088
DG
11677 if (IS_ERR(request)) {
11678 ret = PTR_ERR(request);
6258fbe2 11679 goto cleanup_unpin;
26827088 11680 }
6258fbe2
JH
11681 }
11682
11683 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, request,
d6bbafa1
CW
11684 page_flip_flags);
11685 if (ret)
11686 goto cleanup_unpin;
11687
6258fbe2 11688 i915_gem_request_assign(&work->flip_queued_req, request);
d6bbafa1
CW
11689 }
11690
91af127f 11691 if (request)
75289874 11692 i915_add_request_no_flush(request);
91af127f 11693
1e3feefd 11694 work->flip_queued_vblank = drm_crtc_vblank_count(crtc);
d6bbafa1 11695 work->enable_stall_check = true;
4fa62c89 11696
ab8d6675 11697 i915_gem_track_fb(intel_fb_obj(work->old_fb), obj,
a9ff8714 11698 to_intel_plane(primary)->frontbuffer_bit);
c80ac854 11699 mutex_unlock(&dev->struct_mutex);
a071fa00 11700
a9ff8714
VS
11701 intel_frontbuffer_flip_prepare(dev,
11702 to_intel_plane(primary)->frontbuffer_bit);
6b95a207 11703
e5510fac
JB
11704 trace_i915_flip_request(intel_crtc->plane, obj);
11705
6b95a207 11706 return 0;
96b099fd 11707
4fa62c89 11708cleanup_unpin:
3465c580 11709 intel_unpin_fb_obj(fb, crtc->primary->state->rotation);
8c9f3aaf 11710cleanup_pending:
0aa498d5 11711 if (!IS_ERR_OR_NULL(request))
91af127f 11712 i915_gem_request_cancel(request);
b4a98e57 11713 atomic_dec(&intel_crtc->unpin_work_count);
89ed88ba
CW
11714 mutex_unlock(&dev->struct_mutex);
11715cleanup:
f4510a27 11716 crtc->primary->fb = old_fb;
afd65eb4 11717 update_state_fb(crtc->primary);
89ed88ba
CW
11718
11719 drm_gem_object_unreference_unlocked(&obj->base);
ab8d6675 11720 drm_framebuffer_unreference(work->old_fb);
96b099fd 11721
5e2d7afc 11722 spin_lock_irq(&dev->event_lock);
96b099fd 11723 intel_crtc->unpin_work = NULL;
5e2d7afc 11724 spin_unlock_irq(&dev->event_lock);
96b099fd 11725
87b6b101 11726 drm_crtc_vblank_put(crtc);
7317c75e 11727free_work:
96b099fd
CW
11728 kfree(work);
11729
f900db47 11730 if (ret == -EIO) {
02e0efb5
ML
11731 struct drm_atomic_state *state;
11732 struct drm_plane_state *plane_state;
11733
f900db47 11734out_hang:
02e0efb5
ML
11735 state = drm_atomic_state_alloc(dev);
11736 if (!state)
11737 return -ENOMEM;
11738 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
11739
11740retry:
11741 plane_state = drm_atomic_get_plane_state(state, primary);
11742 ret = PTR_ERR_OR_ZERO(plane_state);
11743 if (!ret) {
11744 drm_atomic_set_fb_for_plane(plane_state, fb);
11745
11746 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
11747 if (!ret)
11748 ret = drm_atomic_commit(state);
11749 }
11750
11751 if (ret == -EDEADLK) {
11752 drm_modeset_backoff(state->acquire_ctx);
11753 drm_atomic_state_clear(state);
11754 goto retry;
11755 }
11756
11757 if (ret)
11758 drm_atomic_state_free(state);
11759
f0d3dad3 11760 if (ret == 0 && event) {
5e2d7afc 11761 spin_lock_irq(&dev->event_lock);
a071fa00 11762 drm_send_vblank_event(dev, pipe, event);
5e2d7afc 11763 spin_unlock_irq(&dev->event_lock);
f0d3dad3 11764 }
f900db47 11765 }
96b099fd 11766 return ret;
6b95a207
KH
11767}
11768
da20eabd
ML
11769
11770/**
11771 * intel_wm_need_update - Check whether watermarks need updating
11772 * @plane: drm plane
11773 * @state: new plane state
11774 *
11775 * Check current plane state versus the new one to determine whether
11776 * watermarks need to be recalculated.
11777 *
11778 * Returns true or false.
11779 */
11780static bool intel_wm_need_update(struct drm_plane *plane,
11781 struct drm_plane_state *state)
11782{
d21fbe87
MR
11783 struct intel_plane_state *new = to_intel_plane_state(state);
11784 struct intel_plane_state *cur = to_intel_plane_state(plane->state);
11785
11786 /* Update watermarks on tiling or size changes. */
92826fcd
ML
11787 if (new->visible != cur->visible)
11788 return true;
11789
11790 if (!cur->base.fb || !new->base.fb)
11791 return false;
11792
11793 if (cur->base.fb->modifier[0] != new->base.fb->modifier[0] ||
11794 cur->base.rotation != new->base.rotation ||
d21fbe87
MR
11795 drm_rect_width(&new->src) != drm_rect_width(&cur->src) ||
11796 drm_rect_height(&new->src) != drm_rect_height(&cur->src) ||
11797 drm_rect_width(&new->dst) != drm_rect_width(&cur->dst) ||
11798 drm_rect_height(&new->dst) != drm_rect_height(&cur->dst))
2791a16c 11799 return true;
7809e5ae 11800
2791a16c 11801 return false;
7809e5ae
MR
11802}
11803
d21fbe87
MR
11804static bool needs_scaling(struct intel_plane_state *state)
11805{
11806 int src_w = drm_rect_width(&state->src) >> 16;
11807 int src_h = drm_rect_height(&state->src) >> 16;
11808 int dst_w = drm_rect_width(&state->dst);
11809 int dst_h = drm_rect_height(&state->dst);
11810
11811 return (src_w != dst_w || src_h != dst_h);
11812}
11813
da20eabd
ML
11814int intel_plane_atomic_calc_changes(struct drm_crtc_state *crtc_state,
11815 struct drm_plane_state *plane_state)
11816{
ab1d3a0e 11817 struct intel_crtc_state *pipe_config = to_intel_crtc_state(crtc_state);
da20eabd
ML
11818 struct drm_crtc *crtc = crtc_state->crtc;
11819 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11820 struct drm_plane *plane = plane_state->plane;
11821 struct drm_device *dev = crtc->dev;
ed4a6a7c 11822 struct drm_i915_private *dev_priv = to_i915(dev);
da20eabd
ML
11823 struct intel_plane_state *old_plane_state =
11824 to_intel_plane_state(plane->state);
11825 int idx = intel_crtc->base.base.id, ret;
da20eabd
ML
11826 bool mode_changed = needs_modeset(crtc_state);
11827 bool was_crtc_enabled = crtc->state->active;
11828 bool is_crtc_enabled = crtc_state->active;
da20eabd
ML
11829 bool turn_off, turn_on, visible, was_visible;
11830 struct drm_framebuffer *fb = plane_state->fb;
11831
11832 if (crtc_state && INTEL_INFO(dev)->gen >= 9 &&
11833 plane->type != DRM_PLANE_TYPE_CURSOR) {
11834 ret = skl_update_scaler_plane(
11835 to_intel_crtc_state(crtc_state),
11836 to_intel_plane_state(plane_state));
11837 if (ret)
11838 return ret;
11839 }
11840
da20eabd
ML
11841 was_visible = old_plane_state->visible;
11842 visible = to_intel_plane_state(plane_state)->visible;
11843
11844 if (!was_crtc_enabled && WARN_ON(was_visible))
11845 was_visible = false;
11846
35c08f43
ML
11847 /*
11848 * Visibility is calculated as if the crtc was on, but
11849 * after scaler setup everything depends on it being off
11850 * when the crtc isn't active.
11851 */
11852 if (!is_crtc_enabled)
11853 to_intel_plane_state(plane_state)->visible = visible = false;
da20eabd
ML
11854
11855 if (!was_visible && !visible)
11856 return 0;
11857
e8861675
ML
11858 if (fb != old_plane_state->base.fb)
11859 pipe_config->fb_changed = true;
11860
da20eabd
ML
11861 turn_off = was_visible && (!visible || mode_changed);
11862 turn_on = visible && (!was_visible || mode_changed);
11863
11864 DRM_DEBUG_ATOMIC("[CRTC:%i] has [PLANE:%i] with fb %i\n", idx,
11865 plane->base.id, fb ? fb->base.id : -1);
11866
11867 DRM_DEBUG_ATOMIC("[PLANE:%i] visible %i -> %i, off %i, on %i, ms %i\n",
11868 plane->base.id, was_visible, visible,
11869 turn_off, turn_on, mode_changed);
11870
caed361d
VS
11871 if (turn_on) {
11872 pipe_config->update_wm_pre = true;
11873
11874 /* must disable cxsr around plane enable/disable */
11875 if (plane->type != DRM_PLANE_TYPE_CURSOR)
11876 pipe_config->disable_cxsr = true;
11877 } else if (turn_off) {
11878 pipe_config->update_wm_post = true;
92826fcd 11879
852eb00d 11880 /* must disable cxsr around plane enable/disable */
e8861675 11881 if (plane->type != DRM_PLANE_TYPE_CURSOR)
ab1d3a0e 11882 pipe_config->disable_cxsr = true;
852eb00d 11883 } else if (intel_wm_need_update(plane, plane_state)) {
caed361d
VS
11884 /* FIXME bollocks */
11885 pipe_config->update_wm_pre = true;
11886 pipe_config->update_wm_post = true;
852eb00d 11887 }
da20eabd 11888
ed4a6a7c 11889 /* Pre-gen9 platforms need two-step watermark updates */
caed361d
VS
11890 if ((pipe_config->update_wm_pre || pipe_config->update_wm_post) &&
11891 INTEL_INFO(dev)->gen < 9 && dev_priv->display.optimize_watermarks)
ed4a6a7c
MR
11892 to_intel_crtc_state(crtc_state)->wm.need_postvbl_update = true;
11893
8be6ca85 11894 if (visible || was_visible)
cd202f69 11895 pipe_config->fb_bits |= to_intel_plane(plane)->frontbuffer_bit;
a9ff8714 11896
31ae71fc
ML
11897 /*
11898 * WaCxSRDisabledForSpriteScaling:ivb
11899 *
11900 * cstate->update_wm was already set above, so this flag will
11901 * take effect when we commit and program watermarks.
11902 */
11903 if (plane->type == DRM_PLANE_TYPE_OVERLAY && IS_IVYBRIDGE(dev) &&
11904 needs_scaling(to_intel_plane_state(plane_state)) &&
11905 !needs_scaling(old_plane_state))
11906 pipe_config->disable_lp_wm = true;
d21fbe87 11907
da20eabd
ML
11908 return 0;
11909}
11910
6d3a1ce7
ML
11911static bool encoders_cloneable(const struct intel_encoder *a,
11912 const struct intel_encoder *b)
11913{
11914 /* masks could be asymmetric, so check both ways */
11915 return a == b || (a->cloneable & (1 << b->type) &&
11916 b->cloneable & (1 << a->type));
11917}
11918
11919static bool check_single_encoder_cloning(struct drm_atomic_state *state,
11920 struct intel_crtc *crtc,
11921 struct intel_encoder *encoder)
11922{
11923 struct intel_encoder *source_encoder;
11924 struct drm_connector *connector;
11925 struct drm_connector_state *connector_state;
11926 int i;
11927
11928 for_each_connector_in_state(state, connector, connector_state, i) {
11929 if (connector_state->crtc != &crtc->base)
11930 continue;
11931
11932 source_encoder =
11933 to_intel_encoder(connector_state->best_encoder);
11934 if (!encoders_cloneable(encoder, source_encoder))
11935 return false;
11936 }
11937
11938 return true;
11939}
11940
11941static bool check_encoder_cloning(struct drm_atomic_state *state,
11942 struct intel_crtc *crtc)
11943{
11944 struct intel_encoder *encoder;
11945 struct drm_connector *connector;
11946 struct drm_connector_state *connector_state;
11947 int i;
11948
11949 for_each_connector_in_state(state, connector, connector_state, i) {
11950 if (connector_state->crtc != &crtc->base)
11951 continue;
11952
11953 encoder = to_intel_encoder(connector_state->best_encoder);
11954 if (!check_single_encoder_cloning(state, crtc, encoder))
11955 return false;
11956 }
11957
11958 return true;
11959}
11960
11961static int intel_crtc_atomic_check(struct drm_crtc *crtc,
11962 struct drm_crtc_state *crtc_state)
11963{
cf5a15be 11964 struct drm_device *dev = crtc->dev;
ad421372 11965 struct drm_i915_private *dev_priv = dev->dev_private;
6d3a1ce7 11966 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
cf5a15be
ML
11967 struct intel_crtc_state *pipe_config =
11968 to_intel_crtc_state(crtc_state);
6d3a1ce7 11969 struct drm_atomic_state *state = crtc_state->state;
4d20cd86 11970 int ret;
6d3a1ce7
ML
11971 bool mode_changed = needs_modeset(crtc_state);
11972
11973 if (mode_changed && !check_encoder_cloning(state, intel_crtc)) {
11974 DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
11975 return -EINVAL;
11976 }
11977
852eb00d 11978 if (mode_changed && !crtc_state->active)
caed361d 11979 pipe_config->update_wm_post = true;
eddfcbcd 11980
ad421372
ML
11981 if (mode_changed && crtc_state->enable &&
11982 dev_priv->display.crtc_compute_clock &&
8106ddbd 11983 !WARN_ON(pipe_config->shared_dpll)) {
ad421372
ML
11984 ret = dev_priv->display.crtc_compute_clock(intel_crtc,
11985 pipe_config);
11986 if (ret)
11987 return ret;
11988 }
11989
e435d6e5 11990 ret = 0;
86c8bbbe 11991 if (dev_priv->display.compute_pipe_wm) {
e3bddded 11992 ret = dev_priv->display.compute_pipe_wm(pipe_config);
ed4a6a7c
MR
11993 if (ret) {
11994 DRM_DEBUG_KMS("Target pipe watermarks are invalid\n");
11995 return ret;
11996 }
11997 }
11998
11999 if (dev_priv->display.compute_intermediate_wm &&
12000 !to_intel_atomic_state(state)->skip_intermediate_wm) {
12001 if (WARN_ON(!dev_priv->display.compute_pipe_wm))
12002 return 0;
12003
12004 /*
12005 * Calculate 'intermediate' watermarks that satisfy both the
12006 * old state and the new state. We can program these
12007 * immediately.
12008 */
12009 ret = dev_priv->display.compute_intermediate_wm(crtc->dev,
12010 intel_crtc,
12011 pipe_config);
12012 if (ret) {
12013 DRM_DEBUG_KMS("No valid intermediate pipe watermarks are possible\n");
86c8bbbe 12014 return ret;
ed4a6a7c 12015 }
86c8bbbe
MR
12016 }
12017
e435d6e5
ML
12018 if (INTEL_INFO(dev)->gen >= 9) {
12019 if (mode_changed)
12020 ret = skl_update_scaler_crtc(pipe_config);
12021
12022 if (!ret)
12023 ret = intel_atomic_setup_scalers(dev, intel_crtc,
12024 pipe_config);
12025 }
12026
12027 return ret;
6d3a1ce7
ML
12028}
12029
65b38e0d 12030static const struct drm_crtc_helper_funcs intel_helper_funcs = {
f6e5b160
CW
12031 .mode_set_base_atomic = intel_pipe_set_base_atomic,
12032 .load_lut = intel_crtc_load_lut,
ea2c67bb
MR
12033 .atomic_begin = intel_begin_crtc_commit,
12034 .atomic_flush = intel_finish_crtc_commit,
6d3a1ce7 12035 .atomic_check = intel_crtc_atomic_check,
f6e5b160
CW
12036};
12037
d29b2f9d
ACO
12038static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
12039{
12040 struct intel_connector *connector;
12041
12042 for_each_intel_connector(dev, connector) {
12043 if (connector->base.encoder) {
12044 connector->base.state->best_encoder =
12045 connector->base.encoder;
12046 connector->base.state->crtc =
12047 connector->base.encoder->crtc;
12048 } else {
12049 connector->base.state->best_encoder = NULL;
12050 connector->base.state->crtc = NULL;
12051 }
12052 }
12053}
12054
050f7aeb 12055static void
eba905b2 12056connected_sink_compute_bpp(struct intel_connector *connector,
5cec258b 12057 struct intel_crtc_state *pipe_config)
050f7aeb
DV
12058{
12059 int bpp = pipe_config->pipe_bpp;
12060
12061 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
12062 connector->base.base.id,
c23cc417 12063 connector->base.name);
050f7aeb
DV
12064
12065 /* Don't use an invalid EDID bpc value */
12066 if (connector->base.display_info.bpc &&
12067 connector->base.display_info.bpc * 3 < bpp) {
12068 DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
12069 bpp, connector->base.display_info.bpc*3);
12070 pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
12071 }
12072
013dd9e0
JN
12073 /* Clamp bpp to default limit on screens without EDID 1.4 */
12074 if (connector->base.display_info.bpc == 0) {
12075 int type = connector->base.connector_type;
12076 int clamp_bpp = 24;
12077
12078 /* Fall back to 18 bpp when DP sink capability is unknown. */
12079 if (type == DRM_MODE_CONNECTOR_DisplayPort ||
12080 type == DRM_MODE_CONNECTOR_eDP)
12081 clamp_bpp = 18;
12082
12083 if (bpp > clamp_bpp) {
12084 DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of %d\n",
12085 bpp, clamp_bpp);
12086 pipe_config->pipe_bpp = clamp_bpp;
12087 }
050f7aeb
DV
12088 }
12089}
12090
4e53c2e0 12091static int
050f7aeb 12092compute_baseline_pipe_bpp(struct intel_crtc *crtc,
5cec258b 12093 struct intel_crtc_state *pipe_config)
4e53c2e0 12094{
050f7aeb 12095 struct drm_device *dev = crtc->base.dev;
1486017f 12096 struct drm_atomic_state *state;
da3ced29
ACO
12097 struct drm_connector *connector;
12098 struct drm_connector_state *connector_state;
1486017f 12099 int bpp, i;
4e53c2e0 12100
666a4537 12101 if ((IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)))
4e53c2e0 12102 bpp = 10*3;
d328c9d7
DV
12103 else if (INTEL_INFO(dev)->gen >= 5)
12104 bpp = 12*3;
12105 else
12106 bpp = 8*3;
12107
4e53c2e0 12108
4e53c2e0
DV
12109 pipe_config->pipe_bpp = bpp;
12110
1486017f
ACO
12111 state = pipe_config->base.state;
12112
4e53c2e0 12113 /* Clamp display bpp to EDID value */
da3ced29
ACO
12114 for_each_connector_in_state(state, connector, connector_state, i) {
12115 if (connector_state->crtc != &crtc->base)
4e53c2e0
DV
12116 continue;
12117
da3ced29
ACO
12118 connected_sink_compute_bpp(to_intel_connector(connector),
12119 pipe_config);
4e53c2e0
DV
12120 }
12121
12122 return bpp;
12123}
12124
644db711
DV
12125static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
12126{
12127 DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
12128 "type: 0x%x flags: 0x%x\n",
1342830c 12129 mode->crtc_clock,
644db711
DV
12130 mode->crtc_hdisplay, mode->crtc_hsync_start,
12131 mode->crtc_hsync_end, mode->crtc_htotal,
12132 mode->crtc_vdisplay, mode->crtc_vsync_start,
12133 mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
12134}
12135
c0b03411 12136static void intel_dump_pipe_config(struct intel_crtc *crtc,
5cec258b 12137 struct intel_crtc_state *pipe_config,
c0b03411
DV
12138 const char *context)
12139{
6a60cd87
CK
12140 struct drm_device *dev = crtc->base.dev;
12141 struct drm_plane *plane;
12142 struct intel_plane *intel_plane;
12143 struct intel_plane_state *state;
12144 struct drm_framebuffer *fb;
12145
12146 DRM_DEBUG_KMS("[CRTC:%d]%s config %p for pipe %c\n", crtc->base.base.id,
12147 context, pipe_config, pipe_name(crtc->pipe));
c0b03411 12148
da205630 12149 DRM_DEBUG_KMS("cpu_transcoder: %s\n", transcoder_name(pipe_config->cpu_transcoder));
c0b03411
DV
12150 DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
12151 pipe_config->pipe_bpp, pipe_config->dither);
12152 DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
12153 pipe_config->has_pch_encoder,
12154 pipe_config->fdi_lanes,
12155 pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
12156 pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
12157 pipe_config->fdi_m_n.tu);
90a6b7b0 12158 DRM_DEBUG_KMS("dp: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
eb14cb74 12159 pipe_config->has_dp_encoder,
90a6b7b0 12160 pipe_config->lane_count,
eb14cb74
VS
12161 pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
12162 pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
12163 pipe_config->dp_m_n.tu);
b95af8be 12164
90a6b7b0 12165 DRM_DEBUG_KMS("dp: %i, lanes: %i, gmch_m2: %u, gmch_n2: %u, link_m2: %u, link_n2: %u, tu2: %u\n",
b95af8be 12166 pipe_config->has_dp_encoder,
90a6b7b0 12167 pipe_config->lane_count,
b95af8be
VK
12168 pipe_config->dp_m2_n2.gmch_m,
12169 pipe_config->dp_m2_n2.gmch_n,
12170 pipe_config->dp_m2_n2.link_m,
12171 pipe_config->dp_m2_n2.link_n,
12172 pipe_config->dp_m2_n2.tu);
12173
55072d19
DV
12174 DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
12175 pipe_config->has_audio,
12176 pipe_config->has_infoframe);
12177
c0b03411 12178 DRM_DEBUG_KMS("requested mode:\n");
2d112de7 12179 drm_mode_debug_printmodeline(&pipe_config->base.mode);
c0b03411 12180 DRM_DEBUG_KMS("adjusted mode:\n");
2d112de7
ACO
12181 drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
12182 intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
d71b8d4a 12183 DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
37327abd
VS
12184 DRM_DEBUG_KMS("pipe src size: %dx%d\n",
12185 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
0ec463d3
TU
12186 DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
12187 crtc->num_scalers,
12188 pipe_config->scaler_state.scaler_users,
12189 pipe_config->scaler_state.scaler_id);
c0b03411
DV
12190 DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
12191 pipe_config->gmch_pfit.control,
12192 pipe_config->gmch_pfit.pgm_ratios,
12193 pipe_config->gmch_pfit.lvds_border_bits);
fd4daa9c 12194 DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
c0b03411 12195 pipe_config->pch_pfit.pos,
fd4daa9c
CW
12196 pipe_config->pch_pfit.size,
12197 pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
42db64ef 12198 DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
cf532bb2 12199 DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
6a60cd87 12200
415ff0f6 12201 if (IS_BROXTON(dev)) {
05712c15 12202 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: ebb0: 0x%x, ebb4: 0x%x,"
415ff0f6 12203 "pll0: 0x%x, pll1: 0x%x, pll2: 0x%x, pll3: 0x%x, "
c8453338 12204 "pll6: 0x%x, pll8: 0x%x, pll9: 0x%x, pll10: 0x%x, pcsdw12: 0x%x\n",
415ff0f6
TU
12205 pipe_config->ddi_pll_sel,
12206 pipe_config->dpll_hw_state.ebb0,
05712c15 12207 pipe_config->dpll_hw_state.ebb4,
415ff0f6
TU
12208 pipe_config->dpll_hw_state.pll0,
12209 pipe_config->dpll_hw_state.pll1,
12210 pipe_config->dpll_hw_state.pll2,
12211 pipe_config->dpll_hw_state.pll3,
12212 pipe_config->dpll_hw_state.pll6,
12213 pipe_config->dpll_hw_state.pll8,
05712c15 12214 pipe_config->dpll_hw_state.pll9,
c8453338 12215 pipe_config->dpll_hw_state.pll10,
415ff0f6 12216 pipe_config->dpll_hw_state.pcsdw12);
ef11bdb3 12217 } else if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) {
415ff0f6
TU
12218 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: "
12219 "ctrl1: 0x%x, cfgcr1: 0x%x, cfgcr2: 0x%x\n",
12220 pipe_config->ddi_pll_sel,
12221 pipe_config->dpll_hw_state.ctrl1,
12222 pipe_config->dpll_hw_state.cfgcr1,
12223 pipe_config->dpll_hw_state.cfgcr2);
12224 } else if (HAS_DDI(dev)) {
1260f07e 12225 DRM_DEBUG_KMS("ddi_pll_sel: 0x%x; dpll_hw_state: wrpll: 0x%x spll: 0x%x\n",
415ff0f6 12226 pipe_config->ddi_pll_sel,
00490c22
ML
12227 pipe_config->dpll_hw_state.wrpll,
12228 pipe_config->dpll_hw_state.spll);
415ff0f6
TU
12229 } else {
12230 DRM_DEBUG_KMS("dpll_hw_state: dpll: 0x%x, dpll_md: 0x%x, "
12231 "fp0: 0x%x, fp1: 0x%x\n",
12232 pipe_config->dpll_hw_state.dpll,
12233 pipe_config->dpll_hw_state.dpll_md,
12234 pipe_config->dpll_hw_state.fp0,
12235 pipe_config->dpll_hw_state.fp1);
12236 }
12237
6a60cd87
CK
12238 DRM_DEBUG_KMS("planes on this crtc\n");
12239 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
12240 intel_plane = to_intel_plane(plane);
12241 if (intel_plane->pipe != crtc->pipe)
12242 continue;
12243
12244 state = to_intel_plane_state(plane->state);
12245 fb = state->base.fb;
12246 if (!fb) {
12247 DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d "
12248 "disabled, scaler_id = %d\n",
12249 plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
12250 plane->base.id, intel_plane->pipe,
12251 (crtc->base.primary == plane) ? 0 : intel_plane->plane + 1,
12252 drm_plane_index(plane), state->scaler_id);
12253 continue;
12254 }
12255
12256 DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d enabled",
12257 plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
12258 plane->base.id, intel_plane->pipe,
12259 crtc->base.primary == plane ? 0 : intel_plane->plane + 1,
12260 drm_plane_index(plane));
12261 DRM_DEBUG_KMS("\tFB:%d, fb = %ux%u format = 0x%x",
12262 fb->base.id, fb->width, fb->height, fb->pixel_format);
12263 DRM_DEBUG_KMS("\tscaler:%d src (%u, %u) %ux%u dst (%u, %u) %ux%u\n",
12264 state->scaler_id,
12265 state->src.x1 >> 16, state->src.y1 >> 16,
12266 drm_rect_width(&state->src) >> 16,
12267 drm_rect_height(&state->src) >> 16,
12268 state->dst.x1, state->dst.y1,
12269 drm_rect_width(&state->dst), drm_rect_height(&state->dst));
12270 }
c0b03411
DV
12271}
12272
5448a00d 12273static bool check_digital_port_conflicts(struct drm_atomic_state *state)
00f0b378 12274{
5448a00d 12275 struct drm_device *dev = state->dev;
da3ced29 12276 struct drm_connector *connector;
00f0b378
VS
12277 unsigned int used_ports = 0;
12278
12279 /*
12280 * Walk the connector list instead of the encoder
12281 * list to detect the problem on ddi platforms
12282 * where there's just one encoder per digital port.
12283 */
0bff4858
VS
12284 drm_for_each_connector(connector, dev) {
12285 struct drm_connector_state *connector_state;
12286 struct intel_encoder *encoder;
12287
12288 connector_state = drm_atomic_get_existing_connector_state(state, connector);
12289 if (!connector_state)
12290 connector_state = connector->state;
12291
5448a00d 12292 if (!connector_state->best_encoder)
00f0b378
VS
12293 continue;
12294
5448a00d
ACO
12295 encoder = to_intel_encoder(connector_state->best_encoder);
12296
12297 WARN_ON(!connector_state->crtc);
00f0b378
VS
12298
12299 switch (encoder->type) {
12300 unsigned int port_mask;
12301 case INTEL_OUTPUT_UNKNOWN:
12302 if (WARN_ON(!HAS_DDI(dev)))
12303 break;
12304 case INTEL_OUTPUT_DISPLAYPORT:
12305 case INTEL_OUTPUT_HDMI:
12306 case INTEL_OUTPUT_EDP:
12307 port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
12308
12309 /* the same port mustn't appear more than once */
12310 if (used_ports & port_mask)
12311 return false;
12312
12313 used_ports |= port_mask;
12314 default:
12315 break;
12316 }
12317 }
12318
12319 return true;
12320}
12321
83a57153
ACO
12322static void
12323clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
12324{
12325 struct drm_crtc_state tmp_state;
663a3640 12326 struct intel_crtc_scaler_state scaler_state;
4978cc93 12327 struct intel_dpll_hw_state dpll_hw_state;
8106ddbd 12328 struct intel_shared_dpll *shared_dpll;
8504c74c 12329 uint32_t ddi_pll_sel;
c4e2d043 12330 bool force_thru;
83a57153 12331
7546a384
ACO
12332 /* FIXME: before the switch to atomic started, a new pipe_config was
12333 * kzalloc'd. Code that depends on any field being zero should be
12334 * fixed, so that the crtc_state can be safely duplicated. For now,
12335 * only fields that are know to not cause problems are preserved. */
12336
83a57153 12337 tmp_state = crtc_state->base;
663a3640 12338 scaler_state = crtc_state->scaler_state;
4978cc93
ACO
12339 shared_dpll = crtc_state->shared_dpll;
12340 dpll_hw_state = crtc_state->dpll_hw_state;
8504c74c 12341 ddi_pll_sel = crtc_state->ddi_pll_sel;
c4e2d043 12342 force_thru = crtc_state->pch_pfit.force_thru;
4978cc93 12343
83a57153 12344 memset(crtc_state, 0, sizeof *crtc_state);
4978cc93 12345
83a57153 12346 crtc_state->base = tmp_state;
663a3640 12347 crtc_state->scaler_state = scaler_state;
4978cc93
ACO
12348 crtc_state->shared_dpll = shared_dpll;
12349 crtc_state->dpll_hw_state = dpll_hw_state;
8504c74c 12350 crtc_state->ddi_pll_sel = ddi_pll_sel;
c4e2d043 12351 crtc_state->pch_pfit.force_thru = force_thru;
83a57153
ACO
12352}
12353
548ee15b 12354static int
b8cecdf5 12355intel_modeset_pipe_config(struct drm_crtc *crtc,
b359283a 12356 struct intel_crtc_state *pipe_config)
ee7b9f93 12357{
b359283a 12358 struct drm_atomic_state *state = pipe_config->base.state;
7758a113 12359 struct intel_encoder *encoder;
da3ced29 12360 struct drm_connector *connector;
0b901879 12361 struct drm_connector_state *connector_state;
d328c9d7 12362 int base_bpp, ret = -EINVAL;
0b901879 12363 int i;
e29c22c0 12364 bool retry = true;
ee7b9f93 12365
83a57153 12366 clear_intel_crtc_state(pipe_config);
7758a113 12367
e143a21c
DV
12368 pipe_config->cpu_transcoder =
12369 (enum transcoder) to_intel_crtc(crtc)->pipe;
b8cecdf5 12370
2960bc9c
ID
12371 /*
12372 * Sanitize sync polarity flags based on requested ones. If neither
12373 * positive or negative polarity is requested, treat this as meaning
12374 * negative polarity.
12375 */
2d112de7 12376 if (!(pipe_config->base.adjusted_mode.flags &
2960bc9c 12377 (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
2d112de7 12378 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
2960bc9c 12379
2d112de7 12380 if (!(pipe_config->base.adjusted_mode.flags &
2960bc9c 12381 (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
2d112de7 12382 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
2960bc9c 12383
d328c9d7
DV
12384 base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
12385 pipe_config);
12386 if (base_bpp < 0)
4e53c2e0
DV
12387 goto fail;
12388
e41a56be
VS
12389 /*
12390 * Determine the real pipe dimensions. Note that stereo modes can
12391 * increase the actual pipe size due to the frame doubling and
12392 * insertion of additional space for blanks between the frame. This
12393 * is stored in the crtc timings. We use the requested mode to do this
12394 * computation to clearly distinguish it from the adjusted mode, which
12395 * can be changed by the connectors in the below retry loop.
12396 */
2d112de7 12397 drm_crtc_get_hv_timing(&pipe_config->base.mode,
ecb7e16b
GP
12398 &pipe_config->pipe_src_w,
12399 &pipe_config->pipe_src_h);
e41a56be 12400
e29c22c0 12401encoder_retry:
ef1b460d 12402 /* Ensure the port clock defaults are reset when retrying. */
ff9a6750 12403 pipe_config->port_clock = 0;
ef1b460d 12404 pipe_config->pixel_multiplier = 1;
ff9a6750 12405
135c81b8 12406 /* Fill in default crtc timings, allow encoders to overwrite them. */
2d112de7
ACO
12407 drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
12408 CRTC_STEREO_DOUBLE);
135c81b8 12409
7758a113
DV
12410 /* Pass our mode to the connectors and the CRTC to give them a chance to
12411 * adjust it according to limitations or connector properties, and also
12412 * a chance to reject the mode entirely.
47f1c6c9 12413 */
da3ced29 12414 for_each_connector_in_state(state, connector, connector_state, i) {
0b901879 12415 if (connector_state->crtc != crtc)
7758a113 12416 continue;
7ae89233 12417
0b901879
ACO
12418 encoder = to_intel_encoder(connector_state->best_encoder);
12419
efea6e8e
DV
12420 if (!(encoder->compute_config(encoder, pipe_config))) {
12421 DRM_DEBUG_KMS("Encoder config failure\n");
7758a113
DV
12422 goto fail;
12423 }
ee7b9f93 12424 }
47f1c6c9 12425
ff9a6750
DV
12426 /* Set default port clock if not overwritten by the encoder. Needs to be
12427 * done afterwards in case the encoder adjusts the mode. */
12428 if (!pipe_config->port_clock)
2d112de7 12429 pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
241bfc38 12430 * pipe_config->pixel_multiplier;
ff9a6750 12431
a43f6e0f 12432 ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
e29c22c0 12433 if (ret < 0) {
7758a113
DV
12434 DRM_DEBUG_KMS("CRTC fixup failed\n");
12435 goto fail;
ee7b9f93 12436 }
e29c22c0
DV
12437
12438 if (ret == RETRY) {
12439 if (WARN(!retry, "loop in pipe configuration computation\n")) {
12440 ret = -EINVAL;
12441 goto fail;
12442 }
12443
12444 DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
12445 retry = false;
12446 goto encoder_retry;
12447 }
12448
e8fa4270
DV
12449 /* Dithering seems to not pass-through bits correctly when it should, so
12450 * only enable it on 6bpc panels. */
12451 pipe_config->dither = pipe_config->pipe_bpp == 6*3;
62f0ace5 12452 DRM_DEBUG_KMS("hw max bpp: %i, pipe bpp: %i, dithering: %i\n",
d328c9d7 12453 base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
4e53c2e0 12454
7758a113 12455fail:
548ee15b 12456 return ret;
ee7b9f93 12457}
47f1c6c9 12458
ea9d758d 12459static void
4740b0f2 12460intel_modeset_update_crtc_state(struct drm_atomic_state *state)
ea9d758d 12461{
0a9ab303
ACO
12462 struct drm_crtc *crtc;
12463 struct drm_crtc_state *crtc_state;
8a75d157 12464 int i;
ea9d758d 12465
7668851f 12466 /* Double check state. */
8a75d157 12467 for_each_crtc_in_state(state, crtc, crtc_state, i) {
3cb480bc 12468 to_intel_crtc(crtc)->config = to_intel_crtc_state(crtc->state);
fc467a22
ML
12469
12470 /* Update hwmode for vblank functions */
12471 if (crtc->state->active)
12472 crtc->hwmode = crtc->state->adjusted_mode;
12473 else
12474 crtc->hwmode.crtc_clock = 0;
61067a5e
ML
12475
12476 /*
12477 * Update legacy state to satisfy fbc code. This can
12478 * be removed when fbc uses the atomic state.
12479 */
12480 if (drm_atomic_get_existing_plane_state(state, crtc->primary)) {
12481 struct drm_plane_state *plane_state = crtc->primary->state;
12482
12483 crtc->primary->fb = plane_state->fb;
12484 crtc->x = plane_state->src_x >> 16;
12485 crtc->y = plane_state->src_y >> 16;
12486 }
ea9d758d 12487 }
ea9d758d
DV
12488}
12489
3bd26263 12490static bool intel_fuzzy_clock_check(int clock1, int clock2)
f1f644dc 12491{
3bd26263 12492 int diff;
f1f644dc
JB
12493
12494 if (clock1 == clock2)
12495 return true;
12496
12497 if (!clock1 || !clock2)
12498 return false;
12499
12500 diff = abs(clock1 - clock2);
12501
12502 if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
12503 return true;
12504
12505 return false;
12506}
12507
25c5b266
DV
12508#define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
12509 list_for_each_entry((intel_crtc), \
12510 &(dev)->mode_config.crtc_list, \
12511 base.head) \
95150bdf 12512 for_each_if (mask & (1 <<(intel_crtc)->pipe))
25c5b266 12513
cfb23ed6
ML
12514static bool
12515intel_compare_m_n(unsigned int m, unsigned int n,
12516 unsigned int m2, unsigned int n2,
12517 bool exact)
12518{
12519 if (m == m2 && n == n2)
12520 return true;
12521
12522 if (exact || !m || !n || !m2 || !n2)
12523 return false;
12524
12525 BUILD_BUG_ON(DATA_LINK_M_N_MASK > INT_MAX);
12526
31d10b57
ML
12527 if (n > n2) {
12528 while (n > n2) {
cfb23ed6
ML
12529 m2 <<= 1;
12530 n2 <<= 1;
12531 }
31d10b57
ML
12532 } else if (n < n2) {
12533 while (n < n2) {
cfb23ed6
ML
12534 m <<= 1;
12535 n <<= 1;
12536 }
12537 }
12538
31d10b57
ML
12539 if (n != n2)
12540 return false;
12541
12542 return intel_fuzzy_clock_check(m, m2);
cfb23ed6
ML
12543}
12544
12545static bool
12546intel_compare_link_m_n(const struct intel_link_m_n *m_n,
12547 struct intel_link_m_n *m2_n2,
12548 bool adjust)
12549{
12550 if (m_n->tu == m2_n2->tu &&
12551 intel_compare_m_n(m_n->gmch_m, m_n->gmch_n,
12552 m2_n2->gmch_m, m2_n2->gmch_n, !adjust) &&
12553 intel_compare_m_n(m_n->link_m, m_n->link_n,
12554 m2_n2->link_m, m2_n2->link_n, !adjust)) {
12555 if (adjust)
12556 *m2_n2 = *m_n;
12557
12558 return true;
12559 }
12560
12561 return false;
12562}
12563
0e8ffe1b 12564static bool
2fa2fe9a 12565intel_pipe_config_compare(struct drm_device *dev,
5cec258b 12566 struct intel_crtc_state *current_config,
cfb23ed6
ML
12567 struct intel_crtc_state *pipe_config,
12568 bool adjust)
0e8ffe1b 12569{
cfb23ed6
ML
12570 bool ret = true;
12571
12572#define INTEL_ERR_OR_DBG_KMS(fmt, ...) \
12573 do { \
12574 if (!adjust) \
12575 DRM_ERROR(fmt, ##__VA_ARGS__); \
12576 else \
12577 DRM_DEBUG_KMS(fmt, ##__VA_ARGS__); \
12578 } while (0)
12579
66e985c0
DV
12580#define PIPE_CONF_CHECK_X(name) \
12581 if (current_config->name != pipe_config->name) { \
cfb23ed6 12582 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
66e985c0
DV
12583 "(expected 0x%08x, found 0x%08x)\n", \
12584 current_config->name, \
12585 pipe_config->name); \
cfb23ed6 12586 ret = false; \
66e985c0
DV
12587 }
12588
08a24034
DV
12589#define PIPE_CONF_CHECK_I(name) \
12590 if (current_config->name != pipe_config->name) { \
cfb23ed6 12591 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
08a24034
DV
12592 "(expected %i, found %i)\n", \
12593 current_config->name, \
12594 pipe_config->name); \
cfb23ed6
ML
12595 ret = false; \
12596 }
12597
8106ddbd
ACO
12598#define PIPE_CONF_CHECK_P(name) \
12599 if (current_config->name != pipe_config->name) { \
12600 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
12601 "(expected %p, found %p)\n", \
12602 current_config->name, \
12603 pipe_config->name); \
12604 ret = false; \
12605 }
12606
cfb23ed6
ML
12607#define PIPE_CONF_CHECK_M_N(name) \
12608 if (!intel_compare_link_m_n(&current_config->name, \
12609 &pipe_config->name,\
12610 adjust)) { \
12611 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
12612 "(expected tu %i gmch %i/%i link %i/%i, " \
12613 "found tu %i, gmch %i/%i link %i/%i)\n", \
12614 current_config->name.tu, \
12615 current_config->name.gmch_m, \
12616 current_config->name.gmch_n, \
12617 current_config->name.link_m, \
12618 current_config->name.link_n, \
12619 pipe_config->name.tu, \
12620 pipe_config->name.gmch_m, \
12621 pipe_config->name.gmch_n, \
12622 pipe_config->name.link_m, \
12623 pipe_config->name.link_n); \
12624 ret = false; \
12625 }
12626
12627#define PIPE_CONF_CHECK_M_N_ALT(name, alt_name) \
12628 if (!intel_compare_link_m_n(&current_config->name, \
12629 &pipe_config->name, adjust) && \
12630 !intel_compare_link_m_n(&current_config->alt_name, \
12631 &pipe_config->name, adjust)) { \
12632 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
12633 "(expected tu %i gmch %i/%i link %i/%i, " \
12634 "or tu %i gmch %i/%i link %i/%i, " \
12635 "found tu %i, gmch %i/%i link %i/%i)\n", \
12636 current_config->name.tu, \
12637 current_config->name.gmch_m, \
12638 current_config->name.gmch_n, \
12639 current_config->name.link_m, \
12640 current_config->name.link_n, \
12641 current_config->alt_name.tu, \
12642 current_config->alt_name.gmch_m, \
12643 current_config->alt_name.gmch_n, \
12644 current_config->alt_name.link_m, \
12645 current_config->alt_name.link_n, \
12646 pipe_config->name.tu, \
12647 pipe_config->name.gmch_m, \
12648 pipe_config->name.gmch_n, \
12649 pipe_config->name.link_m, \
12650 pipe_config->name.link_n); \
12651 ret = false; \
88adfff1
DV
12652 }
12653
b95af8be
VK
12654/* This is required for BDW+ where there is only one set of registers for
12655 * switching between high and low RR.
12656 * This macro can be used whenever a comparison has to be made between one
12657 * hw state and multiple sw state variables.
12658 */
12659#define PIPE_CONF_CHECK_I_ALT(name, alt_name) \
12660 if ((current_config->name != pipe_config->name) && \
12661 (current_config->alt_name != pipe_config->name)) { \
cfb23ed6 12662 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
b95af8be
VK
12663 "(expected %i or %i, found %i)\n", \
12664 current_config->name, \
12665 current_config->alt_name, \
12666 pipe_config->name); \
cfb23ed6 12667 ret = false; \
b95af8be
VK
12668 }
12669
1bd1bd80
DV
12670#define PIPE_CONF_CHECK_FLAGS(name, mask) \
12671 if ((current_config->name ^ pipe_config->name) & (mask)) { \
cfb23ed6 12672 INTEL_ERR_OR_DBG_KMS("mismatch in " #name "(" #mask ") " \
1bd1bd80
DV
12673 "(expected %i, found %i)\n", \
12674 current_config->name & (mask), \
12675 pipe_config->name & (mask)); \
cfb23ed6 12676 ret = false; \
1bd1bd80
DV
12677 }
12678
5e550656
VS
12679#define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
12680 if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
cfb23ed6 12681 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
5e550656
VS
12682 "(expected %i, found %i)\n", \
12683 current_config->name, \
12684 pipe_config->name); \
cfb23ed6 12685 ret = false; \
5e550656
VS
12686 }
12687
bb760063
DV
12688#define PIPE_CONF_QUIRK(quirk) \
12689 ((current_config->quirks | pipe_config->quirks) & (quirk))
12690
eccb140b
DV
12691 PIPE_CONF_CHECK_I(cpu_transcoder);
12692
08a24034
DV
12693 PIPE_CONF_CHECK_I(has_pch_encoder);
12694 PIPE_CONF_CHECK_I(fdi_lanes);
cfb23ed6 12695 PIPE_CONF_CHECK_M_N(fdi_m_n);
08a24034 12696
eb14cb74 12697 PIPE_CONF_CHECK_I(has_dp_encoder);
90a6b7b0 12698 PIPE_CONF_CHECK_I(lane_count);
b95af8be
VK
12699
12700 if (INTEL_INFO(dev)->gen < 8) {
cfb23ed6
ML
12701 PIPE_CONF_CHECK_M_N(dp_m_n);
12702
cfb23ed6
ML
12703 if (current_config->has_drrs)
12704 PIPE_CONF_CHECK_M_N(dp_m2_n2);
12705 } else
12706 PIPE_CONF_CHECK_M_N_ALT(dp_m_n, dp_m2_n2);
eb14cb74 12707
a65347ba
JN
12708 PIPE_CONF_CHECK_I(has_dsi_encoder);
12709
2d112de7
ACO
12710 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
12711 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
12712 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
12713 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
12714 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
12715 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
1bd1bd80 12716
2d112de7
ACO
12717 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
12718 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
12719 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
12720 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
12721 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
12722 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
1bd1bd80 12723
c93f54cf 12724 PIPE_CONF_CHECK_I(pixel_multiplier);
6897b4b5 12725 PIPE_CONF_CHECK_I(has_hdmi_sink);
b5a9fa09 12726 if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
666a4537 12727 IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev))
b5a9fa09 12728 PIPE_CONF_CHECK_I(limited_color_range);
e43823ec 12729 PIPE_CONF_CHECK_I(has_infoframe);
6c49f241 12730
9ed109a7
DV
12731 PIPE_CONF_CHECK_I(has_audio);
12732
2d112de7 12733 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
1bd1bd80
DV
12734 DRM_MODE_FLAG_INTERLACE);
12735
bb760063 12736 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
2d112de7 12737 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063 12738 DRM_MODE_FLAG_PHSYNC);
2d112de7 12739 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063 12740 DRM_MODE_FLAG_NHSYNC);
2d112de7 12741 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063 12742 DRM_MODE_FLAG_PVSYNC);
2d112de7 12743 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063
DV
12744 DRM_MODE_FLAG_NVSYNC);
12745 }
045ac3b5 12746
333b8ca8 12747 PIPE_CONF_CHECK_X(gmch_pfit.control);
e2ff2d4a
DV
12748 /* pfit ratios are autocomputed by the hw on gen4+ */
12749 if (INTEL_INFO(dev)->gen < 4)
12750 PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
333b8ca8 12751 PIPE_CONF_CHECK_X(gmch_pfit.lvds_border_bits);
9953599b 12752
bfd16b2a
ML
12753 if (!adjust) {
12754 PIPE_CONF_CHECK_I(pipe_src_w);
12755 PIPE_CONF_CHECK_I(pipe_src_h);
12756
12757 PIPE_CONF_CHECK_I(pch_pfit.enabled);
12758 if (current_config->pch_pfit.enabled) {
12759 PIPE_CONF_CHECK_X(pch_pfit.pos);
12760 PIPE_CONF_CHECK_X(pch_pfit.size);
12761 }
2fa2fe9a 12762
7aefe2b5
ML
12763 PIPE_CONF_CHECK_I(scaler_state.scaler_id);
12764 }
a1b2278e 12765
e59150dc
JB
12766 /* BDW+ don't expose a synchronous way to read the state */
12767 if (IS_HASWELL(dev))
12768 PIPE_CONF_CHECK_I(ips_enabled);
42db64ef 12769
282740f7
VS
12770 PIPE_CONF_CHECK_I(double_wide);
12771
26804afd
DV
12772 PIPE_CONF_CHECK_X(ddi_pll_sel);
12773
8106ddbd 12774 PIPE_CONF_CHECK_P(shared_dpll);
66e985c0 12775 PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
8bcc2795 12776 PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
66e985c0
DV
12777 PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
12778 PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
d452c5b6 12779 PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
00490c22 12780 PIPE_CONF_CHECK_X(dpll_hw_state.spll);
3f4cd19f
DL
12781 PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
12782 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
12783 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
c0d43d62 12784
42571aef
VS
12785 if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
12786 PIPE_CONF_CHECK_I(pipe_bpp);
12787
2d112de7 12788 PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
a9a7e98a 12789 PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
5e550656 12790
66e985c0 12791#undef PIPE_CONF_CHECK_X
08a24034 12792#undef PIPE_CONF_CHECK_I
8106ddbd 12793#undef PIPE_CONF_CHECK_P
b95af8be 12794#undef PIPE_CONF_CHECK_I_ALT
1bd1bd80 12795#undef PIPE_CONF_CHECK_FLAGS
5e550656 12796#undef PIPE_CONF_CHECK_CLOCK_FUZZY
bb760063 12797#undef PIPE_CONF_QUIRK
cfb23ed6 12798#undef INTEL_ERR_OR_DBG_KMS
88adfff1 12799
cfb23ed6 12800 return ret;
0e8ffe1b
DV
12801}
12802
e3b247da
VS
12803static void intel_pipe_config_sanity_check(struct drm_i915_private *dev_priv,
12804 const struct intel_crtc_state *pipe_config)
12805{
12806 if (pipe_config->has_pch_encoder) {
21a727b3 12807 int fdi_dotclock = intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
e3b247da
VS
12808 &pipe_config->fdi_m_n);
12809 int dotclock = pipe_config->base.adjusted_mode.crtc_clock;
12810
12811 /*
12812 * FDI already provided one idea for the dotclock.
12813 * Yell if the encoder disagrees.
12814 */
12815 WARN(!intel_fuzzy_clock_check(fdi_dotclock, dotclock),
12816 "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
12817 fdi_dotclock, dotclock);
12818 }
12819}
12820
08db6652
DL
12821static void check_wm_state(struct drm_device *dev)
12822{
12823 struct drm_i915_private *dev_priv = dev->dev_private;
12824 struct skl_ddb_allocation hw_ddb, *sw_ddb;
12825 struct intel_crtc *intel_crtc;
12826 int plane;
12827
12828 if (INTEL_INFO(dev)->gen < 9)
12829 return;
12830
12831 skl_ddb_get_hw_state(dev_priv, &hw_ddb);
12832 sw_ddb = &dev_priv->wm.skl_hw.ddb;
12833
12834 for_each_intel_crtc(dev, intel_crtc) {
12835 struct skl_ddb_entry *hw_entry, *sw_entry;
12836 const enum pipe pipe = intel_crtc->pipe;
12837
12838 if (!intel_crtc->active)
12839 continue;
12840
12841 /* planes */
dd740780 12842 for_each_plane(dev_priv, pipe, plane) {
08db6652
DL
12843 hw_entry = &hw_ddb.plane[pipe][plane];
12844 sw_entry = &sw_ddb->plane[pipe][plane];
12845
12846 if (skl_ddb_entry_equal(hw_entry, sw_entry))
12847 continue;
12848
12849 DRM_ERROR("mismatch in DDB state pipe %c plane %d "
12850 "(expected (%u,%u), found (%u,%u))\n",
12851 pipe_name(pipe), plane + 1,
12852 sw_entry->start, sw_entry->end,
12853 hw_entry->start, hw_entry->end);
12854 }
12855
12856 /* cursor */
4969d33e
MR
12857 hw_entry = &hw_ddb.plane[pipe][PLANE_CURSOR];
12858 sw_entry = &sw_ddb->plane[pipe][PLANE_CURSOR];
08db6652
DL
12859
12860 if (skl_ddb_entry_equal(hw_entry, sw_entry))
12861 continue;
12862
12863 DRM_ERROR("mismatch in DDB state pipe %c cursor "
12864 "(expected (%u,%u), found (%u,%u))\n",
12865 pipe_name(pipe),
12866 sw_entry->start, sw_entry->end,
12867 hw_entry->start, hw_entry->end);
12868 }
12869}
12870
91d1b4bd 12871static void
35dd3c64
ML
12872check_connector_state(struct drm_device *dev,
12873 struct drm_atomic_state *old_state)
8af6cf88 12874{
35dd3c64
ML
12875 struct drm_connector_state *old_conn_state;
12876 struct drm_connector *connector;
12877 int i;
8af6cf88 12878
35dd3c64
ML
12879 for_each_connector_in_state(old_state, connector, old_conn_state, i) {
12880 struct drm_encoder *encoder = connector->encoder;
12881 struct drm_connector_state *state = connector->state;
ad3c558f 12882
8af6cf88
DV
12883 /* This also checks the encoder/connector hw state with the
12884 * ->get_hw_state callbacks. */
35dd3c64 12885 intel_connector_check_state(to_intel_connector(connector));
8af6cf88 12886
ad3c558f 12887 I915_STATE_WARN(state->best_encoder != encoder,
35dd3c64 12888 "connector's atomic encoder doesn't match legacy encoder\n");
8af6cf88 12889 }
91d1b4bd
DV
12890}
12891
12892static void
12893check_encoder_state(struct drm_device *dev)
12894{
12895 struct intel_encoder *encoder;
12896 struct intel_connector *connector;
8af6cf88 12897
b2784e15 12898 for_each_intel_encoder(dev, encoder) {
8af6cf88 12899 bool enabled = false;
4d20cd86 12900 enum pipe pipe;
8af6cf88
DV
12901
12902 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
12903 encoder->base.base.id,
8e329a03 12904 encoder->base.name);
8af6cf88 12905
3a3371ff 12906 for_each_intel_connector(dev, connector) {
4d20cd86 12907 if (connector->base.state->best_encoder != &encoder->base)
8af6cf88
DV
12908 continue;
12909 enabled = true;
ad3c558f
ML
12910
12911 I915_STATE_WARN(connector->base.state->crtc !=
12912 encoder->base.crtc,
12913 "connector's crtc doesn't match encoder crtc\n");
8af6cf88 12914 }
0e32b39c 12915
e2c719b7 12916 I915_STATE_WARN(!!encoder->base.crtc != enabled,
8af6cf88
DV
12917 "encoder's enabled state mismatch "
12918 "(expected %i, found %i)\n",
12919 !!encoder->base.crtc, enabled);
7c60d198
ML
12920
12921 if (!encoder->base.crtc) {
4d20cd86 12922 bool active;
7c60d198 12923
4d20cd86
ML
12924 active = encoder->get_hw_state(encoder, &pipe);
12925 I915_STATE_WARN(active,
12926 "encoder detached but still enabled on pipe %c.\n",
12927 pipe_name(pipe));
7c60d198 12928 }
8af6cf88 12929 }
91d1b4bd
DV
12930}
12931
12932static void
4d20cd86 12933check_crtc_state(struct drm_device *dev, struct drm_atomic_state *old_state)
91d1b4bd 12934{
fbee40df 12935 struct drm_i915_private *dev_priv = dev->dev_private;
91d1b4bd 12936 struct intel_encoder *encoder;
4d20cd86
ML
12937 struct drm_crtc_state *old_crtc_state;
12938 struct drm_crtc *crtc;
12939 int i;
8af6cf88 12940
4d20cd86
ML
12941 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
12942 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
12943 struct intel_crtc_state *pipe_config, *sw_config;
7b89b8de 12944 bool active;
8af6cf88 12945
bfd16b2a
ML
12946 if (!needs_modeset(crtc->state) &&
12947 !to_intel_crtc_state(crtc->state)->update_pipe)
4d20cd86 12948 continue;
045ac3b5 12949
4d20cd86
ML
12950 __drm_atomic_helper_crtc_destroy_state(crtc, old_crtc_state);
12951 pipe_config = to_intel_crtc_state(old_crtc_state);
12952 memset(pipe_config, 0, sizeof(*pipe_config));
12953 pipe_config->base.crtc = crtc;
12954 pipe_config->base.state = old_state;
8af6cf88 12955
4d20cd86
ML
12956 DRM_DEBUG_KMS("[CRTC:%d]\n",
12957 crtc->base.id);
8af6cf88 12958
4d20cd86
ML
12959 active = dev_priv->display.get_pipe_config(intel_crtc,
12960 pipe_config);
d62cf62a 12961
b6b5d049 12962 /* hw state is inconsistent with the pipe quirk */
4d20cd86
ML
12963 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
12964 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
12965 active = crtc->state->active;
6c49f241 12966
4d20cd86 12967 I915_STATE_WARN(crtc->state->active != active,
0e8ffe1b 12968 "crtc active state doesn't match with hw state "
4d20cd86 12969 "(expected %i, found %i)\n", crtc->state->active, active);
0e8ffe1b 12970
4d20cd86 12971 I915_STATE_WARN(intel_crtc->active != crtc->state->active,
53d9f4e9 12972 "transitional active state does not match atomic hw state "
4d20cd86
ML
12973 "(expected %i, found %i)\n", crtc->state->active, intel_crtc->active);
12974
12975 for_each_encoder_on_crtc(dev, crtc, encoder) {
12976 enum pipe pipe;
12977
12978 active = encoder->get_hw_state(encoder, &pipe);
12979 I915_STATE_WARN(active != crtc->state->active,
12980 "[ENCODER:%i] active %i with crtc active %i\n",
12981 encoder->base.base.id, active, crtc->state->active);
12982
12983 I915_STATE_WARN(active && intel_crtc->pipe != pipe,
12984 "Encoder connected to wrong pipe %c\n",
12985 pipe_name(pipe));
12986
12987 if (active)
12988 encoder->get_config(encoder, pipe_config);
12989 }
53d9f4e9 12990
4d20cd86 12991 if (!crtc->state->active)
cfb23ed6
ML
12992 continue;
12993
e3b247da
VS
12994 intel_pipe_config_sanity_check(dev_priv, pipe_config);
12995
4d20cd86
ML
12996 sw_config = to_intel_crtc_state(crtc->state);
12997 if (!intel_pipe_config_compare(dev, sw_config,
12998 pipe_config, false)) {
e2c719b7 12999 I915_STATE_WARN(1, "pipe state doesn't match!\n");
4d20cd86 13000 intel_dump_pipe_config(intel_crtc, pipe_config,
c0b03411 13001 "[hw state]");
4d20cd86 13002 intel_dump_pipe_config(intel_crtc, sw_config,
c0b03411
DV
13003 "[sw state]");
13004 }
8af6cf88
DV
13005 }
13006}
13007
91d1b4bd
DV
13008static void
13009check_shared_dpll_state(struct drm_device *dev)
13010{
fbee40df 13011 struct drm_i915_private *dev_priv = dev->dev_private;
91d1b4bd
DV
13012 struct intel_crtc *crtc;
13013 struct intel_dpll_hw_state dpll_hw_state;
13014 int i;
5358901f
DV
13015
13016 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
8106ddbd
ACO
13017 struct intel_shared_dpll *pll =
13018 intel_get_shared_dpll_by_id(dev_priv, i);
2dd66ebd 13019 unsigned enabled_crtcs = 0, active_crtcs = 0;
5358901f
DV
13020 bool active;
13021
13022 memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
13023
13024 DRM_DEBUG_KMS("%s\n", pll->name);
13025
2edd6443 13026 active = pll->funcs.get_hw_state(dev_priv, pll, &dpll_hw_state);
5358901f 13027
2dd66ebd
ML
13028 I915_STATE_WARN(pll->active_mask & ~pll->config.crtc_mask,
13029 "more active pll users than references: %x vs %x\n",
13030 pll->active_mask, pll->config.crtc_mask);
9d16da65
ACO
13031
13032 if (!(pll->flags & INTEL_DPLL_ALWAYS_ON)) {
2dd66ebd
ML
13033 I915_STATE_WARN(!pll->on && pll->active_mask,
13034 "pll in active use but not on in sw tracking\n");
13035 I915_STATE_WARN(pll->on && !pll->active_mask,
13036 "pll is on but not used by any active crtc\n");
9d16da65
ACO
13037 I915_STATE_WARN(pll->on != active,
13038 "pll on state mismatch (expected %i, found %i)\n",
13039 pll->on, active);
13040 }
5358901f 13041
d3fcc808 13042 for_each_intel_crtc(dev, crtc) {
8106ddbd 13043 if (crtc->base.state->enable && crtc->config->shared_dpll == pll)
2dd66ebd
ML
13044 enabled_crtcs |= 1 << drm_crtc_index(&crtc->base);
13045 if (crtc->base.state->active && crtc->config->shared_dpll == pll)
13046 active_crtcs |= 1 << drm_crtc_index(&crtc->base);
5358901f 13047 }
2dd66ebd
ML
13048
13049 I915_STATE_WARN(pll->active_mask != active_crtcs,
13050 "pll active crtcs mismatch (expected %x, found %x)\n",
13051 pll->active_mask, active_crtcs);
13052 I915_STATE_WARN(pll->config.crtc_mask != enabled_crtcs,
13053 "pll enabled crtcs mismatch (expected %x, found %x)\n",
13054 pll->config.crtc_mask, enabled_crtcs);
66e985c0 13055
e2c719b7 13056 I915_STATE_WARN(pll->on && memcmp(&pll->config.hw_state, &dpll_hw_state,
66e985c0
DV
13057 sizeof(dpll_hw_state)),
13058 "pll hw state mismatch\n");
5358901f 13059 }
8af6cf88
DV
13060}
13061
ee165b1a
ML
13062static void
13063intel_modeset_check_state(struct drm_device *dev,
13064 struct drm_atomic_state *old_state)
91d1b4bd 13065{
08db6652 13066 check_wm_state(dev);
35dd3c64 13067 check_connector_state(dev, old_state);
91d1b4bd 13068 check_encoder_state(dev);
4d20cd86 13069 check_crtc_state(dev, old_state);
91d1b4bd
DV
13070 check_shared_dpll_state(dev);
13071}
13072
80715b2f
VS
13073static void update_scanline_offset(struct intel_crtc *crtc)
13074{
13075 struct drm_device *dev = crtc->base.dev;
13076
13077 /*
13078 * The scanline counter increments at the leading edge of hsync.
13079 *
13080 * On most platforms it starts counting from vtotal-1 on the
13081 * first active line. That means the scanline counter value is
13082 * always one less than what we would expect. Ie. just after
13083 * start of vblank, which also occurs at start of hsync (on the
13084 * last active line), the scanline counter will read vblank_start-1.
13085 *
13086 * On gen2 the scanline counter starts counting from 1 instead
13087 * of vtotal-1, so we have to subtract one (or rather add vtotal-1
13088 * to keep the value positive), instead of adding one.
13089 *
13090 * On HSW+ the behaviour of the scanline counter depends on the output
13091 * type. For DP ports it behaves like most other platforms, but on HDMI
13092 * there's an extra 1 line difference. So we need to add two instead of
13093 * one to the value.
13094 */
13095 if (IS_GEN2(dev)) {
124abe07 13096 const struct drm_display_mode *adjusted_mode = &crtc->config->base.adjusted_mode;
80715b2f
VS
13097 int vtotal;
13098
124abe07
VS
13099 vtotal = adjusted_mode->crtc_vtotal;
13100 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
80715b2f
VS
13101 vtotal /= 2;
13102
13103 crtc->scanline_offset = vtotal - 1;
13104 } else if (HAS_DDI(dev) &&
409ee761 13105 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI)) {
80715b2f
VS
13106 crtc->scanline_offset = 2;
13107 } else
13108 crtc->scanline_offset = 1;
13109}
13110
ad421372 13111static void intel_modeset_clear_plls(struct drm_atomic_state *state)
ed6739ef 13112{
225da59b 13113 struct drm_device *dev = state->dev;
ed6739ef 13114 struct drm_i915_private *dev_priv = to_i915(dev);
ad421372 13115 struct intel_shared_dpll_config *shared_dpll = NULL;
0a9ab303
ACO
13116 struct drm_crtc *crtc;
13117 struct drm_crtc_state *crtc_state;
0a9ab303 13118 int i;
ed6739ef
ACO
13119
13120 if (!dev_priv->display.crtc_compute_clock)
ad421372 13121 return;
ed6739ef 13122
0a9ab303 13123 for_each_crtc_in_state(state, crtc, crtc_state, i) {
fb1a38a9 13124 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8106ddbd
ACO
13125 struct intel_shared_dpll *old_dpll =
13126 to_intel_crtc_state(crtc->state)->shared_dpll;
0a9ab303 13127
fb1a38a9 13128 if (!needs_modeset(crtc_state))
225da59b
ACO
13129 continue;
13130
8106ddbd 13131 to_intel_crtc_state(crtc_state)->shared_dpll = NULL;
fb1a38a9 13132
8106ddbd 13133 if (!old_dpll)
fb1a38a9 13134 continue;
0a9ab303 13135
ad421372
ML
13136 if (!shared_dpll)
13137 shared_dpll = intel_atomic_get_shared_dpll_state(state);
ed6739ef 13138
8106ddbd 13139 intel_shared_dpll_config_put(shared_dpll, old_dpll, intel_crtc);
ad421372 13140 }
ed6739ef
ACO
13141}
13142
99d736a2
ML
13143/*
13144 * This implements the workaround described in the "notes" section of the mode
13145 * set sequence documentation. When going from no pipes or single pipe to
13146 * multiple pipes, and planes are enabled after the pipe, we need to wait at
13147 * least 2 vblanks on the first pipe before enabling planes on the second pipe.
13148 */
13149static int haswell_mode_set_planes_workaround(struct drm_atomic_state *state)
13150{
13151 struct drm_crtc_state *crtc_state;
13152 struct intel_crtc *intel_crtc;
13153 struct drm_crtc *crtc;
13154 struct intel_crtc_state *first_crtc_state = NULL;
13155 struct intel_crtc_state *other_crtc_state = NULL;
13156 enum pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
13157 int i;
13158
13159 /* look at all crtc's that are going to be enabled in during modeset */
13160 for_each_crtc_in_state(state, crtc, crtc_state, i) {
13161 intel_crtc = to_intel_crtc(crtc);
13162
13163 if (!crtc_state->active || !needs_modeset(crtc_state))
13164 continue;
13165
13166 if (first_crtc_state) {
13167 other_crtc_state = to_intel_crtc_state(crtc_state);
13168 break;
13169 } else {
13170 first_crtc_state = to_intel_crtc_state(crtc_state);
13171 first_pipe = intel_crtc->pipe;
13172 }
13173 }
13174
13175 /* No workaround needed? */
13176 if (!first_crtc_state)
13177 return 0;
13178
13179 /* w/a possibly needed, check how many crtc's are already enabled. */
13180 for_each_intel_crtc(state->dev, intel_crtc) {
13181 struct intel_crtc_state *pipe_config;
13182
13183 pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
13184 if (IS_ERR(pipe_config))
13185 return PTR_ERR(pipe_config);
13186
13187 pipe_config->hsw_workaround_pipe = INVALID_PIPE;
13188
13189 if (!pipe_config->base.active ||
13190 needs_modeset(&pipe_config->base))
13191 continue;
13192
13193 /* 2 or more enabled crtcs means no need for w/a */
13194 if (enabled_pipe != INVALID_PIPE)
13195 return 0;
13196
13197 enabled_pipe = intel_crtc->pipe;
13198 }
13199
13200 if (enabled_pipe != INVALID_PIPE)
13201 first_crtc_state->hsw_workaround_pipe = enabled_pipe;
13202 else if (other_crtc_state)
13203 other_crtc_state->hsw_workaround_pipe = first_pipe;
13204
13205 return 0;
13206}
13207
27c329ed
ML
13208static int intel_modeset_all_pipes(struct drm_atomic_state *state)
13209{
13210 struct drm_crtc *crtc;
13211 struct drm_crtc_state *crtc_state;
13212 int ret = 0;
13213
13214 /* add all active pipes to the state */
13215 for_each_crtc(state->dev, crtc) {
13216 crtc_state = drm_atomic_get_crtc_state(state, crtc);
13217 if (IS_ERR(crtc_state))
13218 return PTR_ERR(crtc_state);
13219
13220 if (!crtc_state->active || needs_modeset(crtc_state))
13221 continue;
13222
13223 crtc_state->mode_changed = true;
13224
13225 ret = drm_atomic_add_affected_connectors(state, crtc);
13226 if (ret)
13227 break;
13228
13229 ret = drm_atomic_add_affected_planes(state, crtc);
13230 if (ret)
13231 break;
13232 }
13233
13234 return ret;
13235}
13236
c347a676 13237static int intel_modeset_checks(struct drm_atomic_state *state)
054518dd 13238{
565602d7
ML
13239 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
13240 struct drm_i915_private *dev_priv = state->dev->dev_private;
13241 struct drm_crtc *crtc;
13242 struct drm_crtc_state *crtc_state;
13243 int ret = 0, i;
054518dd 13244
b359283a
ML
13245 if (!check_digital_port_conflicts(state)) {
13246 DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
13247 return -EINVAL;
13248 }
13249
565602d7
ML
13250 intel_state->modeset = true;
13251 intel_state->active_crtcs = dev_priv->active_crtcs;
13252
13253 for_each_crtc_in_state(state, crtc, crtc_state, i) {
13254 if (crtc_state->active)
13255 intel_state->active_crtcs |= 1 << i;
13256 else
13257 intel_state->active_crtcs &= ~(1 << i);
13258 }
13259
054518dd
ACO
13260 /*
13261 * See if the config requires any additional preparation, e.g.
13262 * to adjust global state with pipes off. We need to do this
13263 * here so we can get the modeset_pipe updated config for the new
13264 * mode set on this crtc. For other crtcs we need to use the
13265 * adjusted_mode bits in the crtc directly.
13266 */
27c329ed 13267 if (dev_priv->display.modeset_calc_cdclk) {
27c329ed
ML
13268 ret = dev_priv->display.modeset_calc_cdclk(state);
13269
1a617b77 13270 if (!ret && intel_state->dev_cdclk != dev_priv->cdclk_freq)
27c329ed
ML
13271 ret = intel_modeset_all_pipes(state);
13272
13273 if (ret < 0)
054518dd 13274 return ret;
e8788cbc
ML
13275
13276 DRM_DEBUG_KMS("New cdclk calculated to be atomic %u, actual %u\n",
13277 intel_state->cdclk, intel_state->dev_cdclk);
27c329ed 13278 } else
1a617b77 13279 to_intel_atomic_state(state)->cdclk = dev_priv->atomic_cdclk_freq;
054518dd 13280
ad421372 13281 intel_modeset_clear_plls(state);
054518dd 13282
565602d7 13283 if (IS_HASWELL(dev_priv))
ad421372 13284 return haswell_mode_set_planes_workaround(state);
99d736a2 13285
ad421372 13286 return 0;
c347a676
ACO
13287}
13288
aa363136
MR
13289/*
13290 * Handle calculation of various watermark data at the end of the atomic check
13291 * phase. The code here should be run after the per-crtc and per-plane 'check'
13292 * handlers to ensure that all derived state has been updated.
13293 */
13294static void calc_watermark_data(struct drm_atomic_state *state)
13295{
13296 struct drm_device *dev = state->dev;
13297 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
13298 struct drm_crtc *crtc;
13299 struct drm_crtc_state *cstate;
13300 struct drm_plane *plane;
13301 struct drm_plane_state *pstate;
13302
13303 /*
13304 * Calculate watermark configuration details now that derived
13305 * plane/crtc state is all properly updated.
13306 */
13307 drm_for_each_crtc(crtc, dev) {
13308 cstate = drm_atomic_get_existing_crtc_state(state, crtc) ?:
13309 crtc->state;
13310
13311 if (cstate->active)
13312 intel_state->wm_config.num_pipes_active++;
13313 }
13314 drm_for_each_legacy_plane(plane, dev) {
13315 pstate = drm_atomic_get_existing_plane_state(state, plane) ?:
13316 plane->state;
13317
13318 if (!to_intel_plane_state(pstate)->visible)
13319 continue;
13320
13321 intel_state->wm_config.sprites_enabled = true;
13322 if (pstate->crtc_w != pstate->src_w >> 16 ||
13323 pstate->crtc_h != pstate->src_h >> 16)
13324 intel_state->wm_config.sprites_scaled = true;
13325 }
13326}
13327
74c090b1
ML
13328/**
13329 * intel_atomic_check - validate state object
13330 * @dev: drm device
13331 * @state: state to validate
13332 */
13333static int intel_atomic_check(struct drm_device *dev,
13334 struct drm_atomic_state *state)
c347a676 13335{
dd8b3bdb 13336 struct drm_i915_private *dev_priv = to_i915(dev);
aa363136 13337 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
c347a676
ACO
13338 struct drm_crtc *crtc;
13339 struct drm_crtc_state *crtc_state;
13340 int ret, i;
61333b60 13341 bool any_ms = false;
c347a676 13342
74c090b1 13343 ret = drm_atomic_helper_check_modeset(dev, state);
054518dd
ACO
13344 if (ret)
13345 return ret;
13346
c347a676 13347 for_each_crtc_in_state(state, crtc, crtc_state, i) {
cfb23ed6
ML
13348 struct intel_crtc_state *pipe_config =
13349 to_intel_crtc_state(crtc_state);
1ed51de9
DV
13350
13351 /* Catch I915_MODE_FLAG_INHERITED */
13352 if (crtc_state->mode.private_flags != crtc->state->mode.private_flags)
13353 crtc_state->mode_changed = true;
cfb23ed6 13354
61333b60
ML
13355 if (!crtc_state->enable) {
13356 if (needs_modeset(crtc_state))
13357 any_ms = true;
c347a676 13358 continue;
61333b60 13359 }
c347a676 13360
26495481 13361 if (!needs_modeset(crtc_state))
cfb23ed6
ML
13362 continue;
13363
26495481
DV
13364 /* FIXME: For only active_changed we shouldn't need to do any
13365 * state recomputation at all. */
13366
1ed51de9
DV
13367 ret = drm_atomic_add_affected_connectors(state, crtc);
13368 if (ret)
13369 return ret;
b359283a 13370
cfb23ed6 13371 ret = intel_modeset_pipe_config(crtc, pipe_config);
c347a676
ACO
13372 if (ret)
13373 return ret;
13374
73831236 13375 if (i915.fastboot &&
dd8b3bdb 13376 intel_pipe_config_compare(dev,
cfb23ed6 13377 to_intel_crtc_state(crtc->state),
1ed51de9 13378 pipe_config, true)) {
26495481 13379 crtc_state->mode_changed = false;
bfd16b2a 13380 to_intel_crtc_state(crtc_state)->update_pipe = true;
26495481
DV
13381 }
13382
13383 if (needs_modeset(crtc_state)) {
13384 any_ms = true;
cfb23ed6
ML
13385
13386 ret = drm_atomic_add_affected_planes(state, crtc);
13387 if (ret)
13388 return ret;
13389 }
61333b60 13390
26495481
DV
13391 intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
13392 needs_modeset(crtc_state) ?
13393 "[modeset]" : "[fastset]");
c347a676
ACO
13394 }
13395
61333b60
ML
13396 if (any_ms) {
13397 ret = intel_modeset_checks(state);
13398
13399 if (ret)
13400 return ret;
27c329ed 13401 } else
dd8b3bdb 13402 intel_state->cdclk = dev_priv->cdclk_freq;
76305b1a 13403
dd8b3bdb 13404 ret = drm_atomic_helper_check_planes(dev, state);
aa363136
MR
13405 if (ret)
13406 return ret;
13407
f51be2e0 13408 intel_fbc_choose_crtc(dev_priv, state);
aa363136
MR
13409 calc_watermark_data(state);
13410
13411 return 0;
054518dd
ACO
13412}
13413
5008e874
ML
13414static int intel_atomic_prepare_commit(struct drm_device *dev,
13415 struct drm_atomic_state *state,
13416 bool async)
13417{
7580d774
ML
13418 struct drm_i915_private *dev_priv = dev->dev_private;
13419 struct drm_plane_state *plane_state;
5008e874 13420 struct drm_crtc_state *crtc_state;
7580d774 13421 struct drm_plane *plane;
5008e874
ML
13422 struct drm_crtc *crtc;
13423 int i, ret;
13424
13425 if (async) {
13426 DRM_DEBUG_KMS("i915 does not yet support async commit\n");
13427 return -EINVAL;
13428 }
13429
13430 for_each_crtc_in_state(state, crtc, crtc_state, i) {
13431 ret = intel_crtc_wait_for_pending_flips(crtc);
13432 if (ret)
13433 return ret;
7580d774
ML
13434
13435 if (atomic_read(&to_intel_crtc(crtc)->unpin_work_count) >= 2)
13436 flush_workqueue(dev_priv->wq);
5008e874
ML
13437 }
13438
f935675f
ML
13439 ret = mutex_lock_interruptible(&dev->struct_mutex);
13440 if (ret)
13441 return ret;
13442
5008e874 13443 ret = drm_atomic_helper_prepare_planes(dev, state);
7580d774
ML
13444 if (!ret && !async && !i915_reset_in_progress(&dev_priv->gpu_error)) {
13445 u32 reset_counter;
13446
13447 reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
13448 mutex_unlock(&dev->struct_mutex);
13449
13450 for_each_plane_in_state(state, plane, plane_state, i) {
13451 struct intel_plane_state *intel_plane_state =
13452 to_intel_plane_state(plane_state);
13453
13454 if (!intel_plane_state->wait_req)
13455 continue;
13456
13457 ret = __i915_wait_request(intel_plane_state->wait_req,
13458 reset_counter, true,
13459 NULL, NULL);
13460
13461 /* Swallow -EIO errors to allow updates during hw lockup. */
13462 if (ret == -EIO)
13463 ret = 0;
13464
13465 if (ret)
13466 break;
13467 }
13468
13469 if (!ret)
13470 return 0;
13471
13472 mutex_lock(&dev->struct_mutex);
13473 drm_atomic_helper_cleanup_planes(dev, state);
13474 }
5008e874 13475
f935675f 13476 mutex_unlock(&dev->struct_mutex);
5008e874
ML
13477 return ret;
13478}
13479
e8861675
ML
13480static void intel_atomic_wait_for_vblanks(struct drm_device *dev,
13481 struct drm_i915_private *dev_priv,
13482 unsigned crtc_mask)
13483{
13484 unsigned last_vblank_count[I915_MAX_PIPES];
13485 enum pipe pipe;
13486 int ret;
13487
13488 if (!crtc_mask)
13489 return;
13490
13491 for_each_pipe(dev_priv, pipe) {
13492 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
13493
13494 if (!((1 << pipe) & crtc_mask))
13495 continue;
13496
13497 ret = drm_crtc_vblank_get(crtc);
13498 if (WARN_ON(ret != 0)) {
13499 crtc_mask &= ~(1 << pipe);
13500 continue;
13501 }
13502
13503 last_vblank_count[pipe] = drm_crtc_vblank_count(crtc);
13504 }
13505
13506 for_each_pipe(dev_priv, pipe) {
13507 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
13508 long lret;
13509
13510 if (!((1 << pipe) & crtc_mask))
13511 continue;
13512
13513 lret = wait_event_timeout(dev->vblank[pipe].queue,
13514 last_vblank_count[pipe] !=
13515 drm_crtc_vblank_count(crtc),
13516 msecs_to_jiffies(50));
13517
13518 WARN_ON(!lret);
13519
13520 drm_crtc_vblank_put(crtc);
13521 }
13522}
13523
13524static bool needs_vblank_wait(struct intel_crtc_state *crtc_state)
13525{
13526 /* fb updated, need to unpin old fb */
13527 if (crtc_state->fb_changed)
13528 return true;
13529
13530 /* wm changes, need vblank before final wm's */
caed361d 13531 if (crtc_state->update_wm_post)
e8861675
ML
13532 return true;
13533
13534 /*
13535 * cxsr is re-enabled after vblank.
caed361d 13536 * This is already handled by crtc_state->update_wm_post,
e8861675
ML
13537 * but added for clarity.
13538 */
13539 if (crtc_state->disable_cxsr)
13540 return true;
13541
13542 return false;
13543}
13544
74c090b1
ML
13545/**
13546 * intel_atomic_commit - commit validated state object
13547 * @dev: DRM device
13548 * @state: the top-level driver state object
13549 * @async: asynchronous commit
13550 *
13551 * This function commits a top-level state object that has been validated
13552 * with drm_atomic_helper_check().
13553 *
13554 * FIXME: Atomic modeset support for i915 is not yet complete. At the moment
13555 * we can only handle plane-related operations and do not yet support
13556 * asynchronous commit.
13557 *
13558 * RETURNS
13559 * Zero for success or -errno.
13560 */
13561static int intel_atomic_commit(struct drm_device *dev,
13562 struct drm_atomic_state *state,
13563 bool async)
a6778b3c 13564{
565602d7 13565 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
fbee40df 13566 struct drm_i915_private *dev_priv = dev->dev_private;
29ceb0e6 13567 struct drm_crtc_state *old_crtc_state;
7580d774 13568 struct drm_crtc *crtc;
ed4a6a7c 13569 struct intel_crtc_state *intel_cstate;
565602d7
ML
13570 int ret = 0, i;
13571 bool hw_check = intel_state->modeset;
33c8df89 13572 unsigned long put_domains[I915_MAX_PIPES] = {};
e8861675 13573 unsigned crtc_vblank_mask = 0;
a6778b3c 13574
5008e874 13575 ret = intel_atomic_prepare_commit(dev, state, async);
7580d774
ML
13576 if (ret) {
13577 DRM_DEBUG_ATOMIC("Preparing state failed with %i\n", ret);
d4afb8cc 13578 return ret;
7580d774 13579 }
d4afb8cc 13580
1c5e19f8 13581 drm_atomic_helper_swap_state(dev, state);
a1475e77
ML
13582 dev_priv->wm.config = intel_state->wm_config;
13583 intel_shared_dpll_commit(state);
1c5e19f8 13584
565602d7
ML
13585 if (intel_state->modeset) {
13586 memcpy(dev_priv->min_pixclk, intel_state->min_pixclk,
13587 sizeof(intel_state->min_pixclk));
13588 dev_priv->active_crtcs = intel_state->active_crtcs;
1a617b77 13589 dev_priv->atomic_cdclk_freq = intel_state->cdclk;
33c8df89
ML
13590
13591 intel_display_power_get(dev_priv, POWER_DOMAIN_MODESET);
565602d7
ML
13592 }
13593
29ceb0e6 13594 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
a539205a
ML
13595 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13596
33c8df89
ML
13597 if (needs_modeset(crtc->state) ||
13598 to_intel_crtc_state(crtc->state)->update_pipe) {
13599 hw_check = true;
13600
13601 put_domains[to_intel_crtc(crtc)->pipe] =
13602 modeset_get_crtc_power_domains(crtc,
13603 to_intel_crtc_state(crtc->state));
13604 }
13605
61333b60
ML
13606 if (!needs_modeset(crtc->state))
13607 continue;
13608
29ceb0e6 13609 intel_pre_plane_update(to_intel_crtc_state(old_crtc_state));
460da916 13610
29ceb0e6
VS
13611 if (old_crtc_state->active) {
13612 intel_crtc_disable_planes(crtc, old_crtc_state->plane_mask);
a539205a 13613 dev_priv->display.crtc_disable(crtc);
eddfcbcd 13614 intel_crtc->active = false;
58f9c0bc 13615 intel_fbc_disable(intel_crtc);
eddfcbcd 13616 intel_disable_shared_dpll(intel_crtc);
9bbc8258
VS
13617
13618 /*
13619 * Underruns don't always raise
13620 * interrupts, so check manually.
13621 */
13622 intel_check_cpu_fifo_underruns(dev_priv);
13623 intel_check_pch_fifo_underruns(dev_priv);
b9001114
ML
13624
13625 if (!crtc->state->active)
13626 intel_update_watermarks(crtc);
a539205a 13627 }
b8cecdf5 13628 }
7758a113 13629
ea9d758d
DV
13630 /* Only after disabling all output pipelines that will be changed can we
13631 * update the the output configuration. */
4740b0f2 13632 intel_modeset_update_crtc_state(state);
f6e5b160 13633
565602d7 13634 if (intel_state->modeset) {
4740b0f2 13635 drm_atomic_helper_update_legacy_modeset_state(state->dev, state);
33c8df89
ML
13636
13637 if (dev_priv->display.modeset_commit_cdclk &&
13638 intel_state->dev_cdclk != dev_priv->cdclk_freq)
13639 dev_priv->display.modeset_commit_cdclk(state);
4740b0f2 13640 }
47fab737 13641
a6778b3c 13642 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
29ceb0e6 13643 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
f6ac4b2a
ML
13644 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13645 bool modeset = needs_modeset(crtc->state);
e8861675
ML
13646 struct intel_crtc_state *pipe_config =
13647 to_intel_crtc_state(crtc->state);
13648 bool update_pipe = !modeset && pipe_config->update_pipe;
9f836f90 13649
f6ac4b2a 13650 if (modeset && crtc->state->active) {
a539205a
ML
13651 update_scanline_offset(to_intel_crtc(crtc));
13652 dev_priv->display.crtc_enable(crtc);
13653 }
80715b2f 13654
f6ac4b2a 13655 if (!modeset)
29ceb0e6 13656 intel_pre_plane_update(to_intel_crtc_state(old_crtc_state));
f6ac4b2a 13657
31ae71fc
ML
13658 if (crtc->state->active &&
13659 drm_atomic_get_existing_plane_state(state, crtc->primary))
49227c4a
PZ
13660 intel_fbc_enable(intel_crtc);
13661
6173ee28
ML
13662 if (crtc->state->active &&
13663 (crtc->state->planes_changed || update_pipe))
29ceb0e6 13664 drm_atomic_helper_commit_planes_on_crtc(old_crtc_state);
bfd16b2a 13665
e8861675
ML
13666 if (pipe_config->base.active && needs_vblank_wait(pipe_config))
13667 crtc_vblank_mask |= 1 << i;
80715b2f 13668 }
a6778b3c 13669
a6778b3c 13670 /* FIXME: add subpixel order */
83a57153 13671
e8861675
ML
13672 if (!state->legacy_cursor_update)
13673 intel_atomic_wait_for_vblanks(dev, dev_priv, crtc_vblank_mask);
f935675f 13674
29ceb0e6 13675 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
cd202f69 13676 intel_post_plane_update(to_intel_crtc_state(old_crtc_state));
e8861675 13677
33c8df89
ML
13678 if (put_domains[i])
13679 modeset_put_power_domains(dev_priv, put_domains[i]);
13680 }
13681
13682 if (intel_state->modeset)
13683 intel_display_power_put(dev_priv, POWER_DOMAIN_MODESET);
13684
ed4a6a7c
MR
13685 /*
13686 * Now that the vblank has passed, we can go ahead and program the
13687 * optimal watermarks on platforms that need two-step watermark
13688 * programming.
13689 *
13690 * TODO: Move this (and other cleanup) to an async worker eventually.
13691 */
29ceb0e6 13692 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
ed4a6a7c
MR
13693 intel_cstate = to_intel_crtc_state(crtc->state);
13694
13695 if (dev_priv->display.optimize_watermarks)
13696 dev_priv->display.optimize_watermarks(intel_cstate);
13697 }
13698
f935675f 13699 mutex_lock(&dev->struct_mutex);
d4afb8cc 13700 drm_atomic_helper_cleanup_planes(dev, state);
f935675f 13701 mutex_unlock(&dev->struct_mutex);
2bfb4627 13702
565602d7 13703 if (hw_check)
ee165b1a
ML
13704 intel_modeset_check_state(dev, state);
13705
13706 drm_atomic_state_free(state);
f30da187 13707
75714940
MK
13708 /* As one of the primary mmio accessors, KMS has a high likelihood
13709 * of triggering bugs in unclaimed access. After we finish
13710 * modesetting, see if an error has been flagged, and if so
13711 * enable debugging for the next modeset - and hope we catch
13712 * the culprit.
13713 *
13714 * XXX note that we assume display power is on at this point.
13715 * This might hold true now but we need to add pm helper to check
13716 * unclaimed only when the hardware is on, as atomic commits
13717 * can happen also when the device is completely off.
13718 */
13719 intel_uncore_arm_unclaimed_mmio_detection(dev_priv);
13720
74c090b1 13721 return 0;
7f27126e
JB
13722}
13723
c0c36b94
CW
13724void intel_crtc_restore_mode(struct drm_crtc *crtc)
13725{
83a57153
ACO
13726 struct drm_device *dev = crtc->dev;
13727 struct drm_atomic_state *state;
e694eb02 13728 struct drm_crtc_state *crtc_state;
2bfb4627 13729 int ret;
83a57153
ACO
13730
13731 state = drm_atomic_state_alloc(dev);
13732 if (!state) {
e694eb02 13733 DRM_DEBUG_KMS("[CRTC:%d] crtc restore failed, out of memory",
83a57153
ACO
13734 crtc->base.id);
13735 return;
13736 }
13737
e694eb02 13738 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
83a57153 13739
e694eb02
ML
13740retry:
13741 crtc_state = drm_atomic_get_crtc_state(state, crtc);
13742 ret = PTR_ERR_OR_ZERO(crtc_state);
13743 if (!ret) {
13744 if (!crtc_state->active)
13745 goto out;
83a57153 13746
e694eb02 13747 crtc_state->mode_changed = true;
74c090b1 13748 ret = drm_atomic_commit(state);
83a57153
ACO
13749 }
13750
e694eb02
ML
13751 if (ret == -EDEADLK) {
13752 drm_atomic_state_clear(state);
13753 drm_modeset_backoff(state->acquire_ctx);
13754 goto retry;
4ed9fb37 13755 }
4be07317 13756
2bfb4627 13757 if (ret)
e694eb02 13758out:
2bfb4627 13759 drm_atomic_state_free(state);
c0c36b94
CW
13760}
13761
25c5b266
DV
13762#undef for_each_intel_crtc_masked
13763
f6e5b160 13764static const struct drm_crtc_funcs intel_crtc_funcs = {
f6e5b160 13765 .gamma_set = intel_crtc_gamma_set,
74c090b1 13766 .set_config = drm_atomic_helper_set_config,
f6e5b160
CW
13767 .destroy = intel_crtc_destroy,
13768 .page_flip = intel_crtc_page_flip,
1356837e
MR
13769 .atomic_duplicate_state = intel_crtc_duplicate_state,
13770 .atomic_destroy_state = intel_crtc_destroy_state,
f6e5b160
CW
13771};
13772
6beb8c23
MR
13773/**
13774 * intel_prepare_plane_fb - Prepare fb for usage on plane
13775 * @plane: drm plane to prepare for
13776 * @fb: framebuffer to prepare for presentation
13777 *
13778 * Prepares a framebuffer for usage on a display plane. Generally this
13779 * involves pinning the underlying object and updating the frontbuffer tracking
13780 * bits. Some older platforms need special physical address handling for
13781 * cursor planes.
13782 *
f935675f
ML
13783 * Must be called with struct_mutex held.
13784 *
6beb8c23
MR
13785 * Returns 0 on success, negative error code on failure.
13786 */
13787int
13788intel_prepare_plane_fb(struct drm_plane *plane,
d136dfee 13789 const struct drm_plane_state *new_state)
465c120c
MR
13790{
13791 struct drm_device *dev = plane->dev;
844f9111 13792 struct drm_framebuffer *fb = new_state->fb;
6beb8c23 13793 struct intel_plane *intel_plane = to_intel_plane(plane);
6beb8c23 13794 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
1ee49399 13795 struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->state->fb);
6beb8c23 13796 int ret = 0;
465c120c 13797
1ee49399 13798 if (!obj && !old_obj)
465c120c
MR
13799 return 0;
13800
5008e874
ML
13801 if (old_obj) {
13802 struct drm_crtc_state *crtc_state =
13803 drm_atomic_get_existing_crtc_state(new_state->state, plane->state->crtc);
13804
13805 /* Big Hammer, we also need to ensure that any pending
13806 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
13807 * current scanout is retired before unpinning the old
13808 * framebuffer. Note that we rely on userspace rendering
13809 * into the buffer attached to the pipe they are waiting
13810 * on. If not, userspace generates a GPU hang with IPEHR
13811 * point to the MI_WAIT_FOR_EVENT.
13812 *
13813 * This should only fail upon a hung GPU, in which case we
13814 * can safely continue.
13815 */
13816 if (needs_modeset(crtc_state))
13817 ret = i915_gem_object_wait_rendering(old_obj, true);
13818
13819 /* Swallow -EIO errors to allow updates during hw lockup. */
13820 if (ret && ret != -EIO)
f935675f 13821 return ret;
5008e874
ML
13822 }
13823
3c28ff22
AG
13824 /* For framebuffer backed by dmabuf, wait for fence */
13825 if (obj && obj->base.dma_buf) {
bcf8be27
ML
13826 long lret;
13827
13828 lret = reservation_object_wait_timeout_rcu(obj->base.dma_buf->resv,
13829 false, true,
13830 MAX_SCHEDULE_TIMEOUT);
13831 if (lret == -ERESTARTSYS)
13832 return lret;
3c28ff22 13833
bcf8be27 13834 WARN(lret < 0, "waiting returns %li\n", lret);
3c28ff22
AG
13835 }
13836
1ee49399
ML
13837 if (!obj) {
13838 ret = 0;
13839 } else if (plane->type == DRM_PLANE_TYPE_CURSOR &&
6beb8c23
MR
13840 INTEL_INFO(dev)->cursor_needs_physical) {
13841 int align = IS_I830(dev) ? 16 * 1024 : 256;
13842 ret = i915_gem_object_attach_phys(obj, align);
13843 if (ret)
13844 DRM_DEBUG_KMS("failed to attach phys object\n");
13845 } else {
3465c580 13846 ret = intel_pin_and_fence_fb_obj(fb, new_state->rotation);
6beb8c23 13847 }
465c120c 13848
7580d774
ML
13849 if (ret == 0) {
13850 if (obj) {
13851 struct intel_plane_state *plane_state =
13852 to_intel_plane_state(new_state);
13853
13854 i915_gem_request_assign(&plane_state->wait_req,
13855 obj->last_write_req);
13856 }
13857
a9ff8714 13858 i915_gem_track_fb(old_obj, obj, intel_plane->frontbuffer_bit);
7580d774 13859 }
fdd508a6 13860
6beb8c23
MR
13861 return ret;
13862}
13863
38f3ce3a
MR
13864/**
13865 * intel_cleanup_plane_fb - Cleans up an fb after plane use
13866 * @plane: drm plane to clean up for
13867 * @fb: old framebuffer that was on plane
13868 *
13869 * Cleans up a framebuffer that has just been removed from a plane.
f935675f
ML
13870 *
13871 * Must be called with struct_mutex held.
38f3ce3a
MR
13872 */
13873void
13874intel_cleanup_plane_fb(struct drm_plane *plane,
d136dfee 13875 const struct drm_plane_state *old_state)
38f3ce3a
MR
13876{
13877 struct drm_device *dev = plane->dev;
1ee49399 13878 struct intel_plane *intel_plane = to_intel_plane(plane);
7580d774 13879 struct intel_plane_state *old_intel_state;
1ee49399
ML
13880 struct drm_i915_gem_object *old_obj = intel_fb_obj(old_state->fb);
13881 struct drm_i915_gem_object *obj = intel_fb_obj(plane->state->fb);
38f3ce3a 13882
7580d774
ML
13883 old_intel_state = to_intel_plane_state(old_state);
13884
1ee49399 13885 if (!obj && !old_obj)
38f3ce3a
MR
13886 return;
13887
1ee49399
ML
13888 if (old_obj && (plane->type != DRM_PLANE_TYPE_CURSOR ||
13889 !INTEL_INFO(dev)->cursor_needs_physical))
3465c580 13890 intel_unpin_fb_obj(old_state->fb, old_state->rotation);
1ee49399
ML
13891
13892 /* prepare_fb aborted? */
13893 if ((old_obj && (old_obj->frontbuffer_bits & intel_plane->frontbuffer_bit)) ||
13894 (obj && !(obj->frontbuffer_bits & intel_plane->frontbuffer_bit)))
13895 i915_gem_track_fb(old_obj, obj, intel_plane->frontbuffer_bit);
7580d774
ML
13896
13897 i915_gem_request_assign(&old_intel_state->wait_req, NULL);
465c120c
MR
13898}
13899
6156a456
CK
13900int
13901skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
13902{
13903 int max_scale;
13904 struct drm_device *dev;
13905 struct drm_i915_private *dev_priv;
13906 int crtc_clock, cdclk;
13907
bf8a0af0 13908 if (!intel_crtc || !crtc_state->base.enable)
6156a456
CK
13909 return DRM_PLANE_HELPER_NO_SCALING;
13910
13911 dev = intel_crtc->base.dev;
13912 dev_priv = dev->dev_private;
13913 crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
27c329ed 13914 cdclk = to_intel_atomic_state(crtc_state->base.state)->cdclk;
6156a456 13915
54bf1ce6 13916 if (WARN_ON_ONCE(!crtc_clock || cdclk < crtc_clock))
6156a456
CK
13917 return DRM_PLANE_HELPER_NO_SCALING;
13918
13919 /*
13920 * skl max scale is lower of:
13921 * close to 3 but not 3, -1 is for that purpose
13922 * or
13923 * cdclk/crtc_clock
13924 */
13925 max_scale = min((1 << 16) * 3 - 1, (1 << 8) * ((cdclk << 8) / crtc_clock));
13926
13927 return max_scale;
13928}
13929
465c120c 13930static int
3c692a41 13931intel_check_primary_plane(struct drm_plane *plane,
061e4b8d 13932 struct intel_crtc_state *crtc_state,
3c692a41
GP
13933 struct intel_plane_state *state)
13934{
2b875c22
MR
13935 struct drm_crtc *crtc = state->base.crtc;
13936 struct drm_framebuffer *fb = state->base.fb;
6156a456 13937 int min_scale = DRM_PLANE_HELPER_NO_SCALING;
061e4b8d
ML
13938 int max_scale = DRM_PLANE_HELPER_NO_SCALING;
13939 bool can_position = false;
465c120c 13940
693bdc28
VS
13941 if (INTEL_INFO(plane->dev)->gen >= 9) {
13942 /* use scaler when colorkey is not required */
13943 if (state->ckey.flags == I915_SET_COLORKEY_NONE) {
13944 min_scale = 1;
13945 max_scale = skl_max_scale(to_intel_crtc(crtc), crtc_state);
13946 }
d8106366 13947 can_position = true;
6156a456 13948 }
d8106366 13949
061e4b8d
ML
13950 return drm_plane_helper_check_update(plane, crtc, fb, &state->src,
13951 &state->dst, &state->clip,
da20eabd
ML
13952 min_scale, max_scale,
13953 can_position, true,
13954 &state->visible);
14af293f
GP
13955}
13956
613d2b27
ML
13957static void intel_begin_crtc_commit(struct drm_crtc *crtc,
13958 struct drm_crtc_state *old_crtc_state)
3c692a41 13959{
32b7eeec 13960 struct drm_device *dev = crtc->dev;
3c692a41 13961 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
bfd16b2a
ML
13962 struct intel_crtc_state *old_intel_state =
13963 to_intel_crtc_state(old_crtc_state);
13964 bool modeset = needs_modeset(crtc->state);
3c692a41 13965
c34c9ee4 13966 /* Perform vblank evasion around commit operation */
62852622 13967 intel_pipe_update_start(intel_crtc);
0583236e 13968
bfd16b2a
ML
13969 if (modeset)
13970 return;
13971
13972 if (to_intel_crtc_state(crtc->state)->update_pipe)
13973 intel_update_pipe_config(intel_crtc, old_intel_state);
13974 else if (INTEL_INFO(dev)->gen >= 9)
0583236e 13975 skl_detach_scalers(intel_crtc);
32b7eeec
MR
13976}
13977
613d2b27
ML
13978static void intel_finish_crtc_commit(struct drm_crtc *crtc,
13979 struct drm_crtc_state *old_crtc_state)
32b7eeec 13980{
32b7eeec 13981 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
32b7eeec 13982
62852622 13983 intel_pipe_update_end(intel_crtc);
3c692a41
GP
13984}
13985
cf4c7c12 13986/**
4a3b8769
MR
13987 * intel_plane_destroy - destroy a plane
13988 * @plane: plane to destroy
cf4c7c12 13989 *
4a3b8769
MR
13990 * Common destruction function for all types of planes (primary, cursor,
13991 * sprite).
cf4c7c12 13992 */
4a3b8769 13993void intel_plane_destroy(struct drm_plane *plane)
465c120c
MR
13994{
13995 struct intel_plane *intel_plane = to_intel_plane(plane);
13996 drm_plane_cleanup(plane);
13997 kfree(intel_plane);
13998}
13999
65a3fea0 14000const struct drm_plane_funcs intel_plane_funcs = {
70a101f8
MR
14001 .update_plane = drm_atomic_helper_update_plane,
14002 .disable_plane = drm_atomic_helper_disable_plane,
3d7d6510 14003 .destroy = intel_plane_destroy,
c196e1d6 14004 .set_property = drm_atomic_helper_plane_set_property,
a98b3431
MR
14005 .atomic_get_property = intel_plane_atomic_get_property,
14006 .atomic_set_property = intel_plane_atomic_set_property,
ea2c67bb
MR
14007 .atomic_duplicate_state = intel_plane_duplicate_state,
14008 .atomic_destroy_state = intel_plane_destroy_state,
14009
465c120c
MR
14010};
14011
14012static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
14013 int pipe)
14014{
14015 struct intel_plane *primary;
8e7d688b 14016 struct intel_plane_state *state;
465c120c 14017 const uint32_t *intel_primary_formats;
45e3743a 14018 unsigned int num_formats;
465c120c
MR
14019
14020 primary = kzalloc(sizeof(*primary), GFP_KERNEL);
14021 if (primary == NULL)
14022 return NULL;
14023
8e7d688b
MR
14024 state = intel_create_plane_state(&primary->base);
14025 if (!state) {
ea2c67bb
MR
14026 kfree(primary);
14027 return NULL;
14028 }
8e7d688b 14029 primary->base.state = &state->base;
ea2c67bb 14030
465c120c
MR
14031 primary->can_scale = false;
14032 primary->max_downscale = 1;
6156a456
CK
14033 if (INTEL_INFO(dev)->gen >= 9) {
14034 primary->can_scale = true;
af99ceda 14035 state->scaler_id = -1;
6156a456 14036 }
465c120c
MR
14037 primary->pipe = pipe;
14038 primary->plane = pipe;
a9ff8714 14039 primary->frontbuffer_bit = INTEL_FRONTBUFFER_PRIMARY(pipe);
c59cb179 14040 primary->check_plane = intel_check_primary_plane;
465c120c
MR
14041 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
14042 primary->plane = !pipe;
14043
6c0fd451
DL
14044 if (INTEL_INFO(dev)->gen >= 9) {
14045 intel_primary_formats = skl_primary_formats;
14046 num_formats = ARRAY_SIZE(skl_primary_formats);
a8d201af
ML
14047
14048 primary->update_plane = skylake_update_primary_plane;
14049 primary->disable_plane = skylake_disable_primary_plane;
14050 } else if (HAS_PCH_SPLIT(dev)) {
14051 intel_primary_formats = i965_primary_formats;
14052 num_formats = ARRAY_SIZE(i965_primary_formats);
14053
14054 primary->update_plane = ironlake_update_primary_plane;
14055 primary->disable_plane = i9xx_disable_primary_plane;
6c0fd451 14056 } else if (INTEL_INFO(dev)->gen >= 4) {
568db4f2
DL
14057 intel_primary_formats = i965_primary_formats;
14058 num_formats = ARRAY_SIZE(i965_primary_formats);
a8d201af
ML
14059
14060 primary->update_plane = i9xx_update_primary_plane;
14061 primary->disable_plane = i9xx_disable_primary_plane;
6c0fd451
DL
14062 } else {
14063 intel_primary_formats = i8xx_primary_formats;
14064 num_formats = ARRAY_SIZE(i8xx_primary_formats);
a8d201af
ML
14065
14066 primary->update_plane = i9xx_update_primary_plane;
14067 primary->disable_plane = i9xx_disable_primary_plane;
465c120c
MR
14068 }
14069
14070 drm_universal_plane_init(dev, &primary->base, 0,
65a3fea0 14071 &intel_plane_funcs,
465c120c 14072 intel_primary_formats, num_formats,
b0b3b795 14073 DRM_PLANE_TYPE_PRIMARY, NULL);
48404c1e 14074
3b7a5119
SJ
14075 if (INTEL_INFO(dev)->gen >= 4)
14076 intel_create_rotation_property(dev, primary);
48404c1e 14077
ea2c67bb
MR
14078 drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
14079
465c120c
MR
14080 return &primary->base;
14081}
14082
3b7a5119
SJ
14083void intel_create_rotation_property(struct drm_device *dev, struct intel_plane *plane)
14084{
14085 if (!dev->mode_config.rotation_property) {
14086 unsigned long flags = BIT(DRM_ROTATE_0) |
14087 BIT(DRM_ROTATE_180);
14088
14089 if (INTEL_INFO(dev)->gen >= 9)
14090 flags |= BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_270);
14091
14092 dev->mode_config.rotation_property =
14093 drm_mode_create_rotation_property(dev, flags);
14094 }
14095 if (dev->mode_config.rotation_property)
14096 drm_object_attach_property(&plane->base.base,
14097 dev->mode_config.rotation_property,
14098 plane->base.state->rotation);
14099}
14100
3d7d6510 14101static int
852e787c 14102intel_check_cursor_plane(struct drm_plane *plane,
061e4b8d 14103 struct intel_crtc_state *crtc_state,
852e787c 14104 struct intel_plane_state *state)
3d7d6510 14105{
061e4b8d 14106 struct drm_crtc *crtc = crtc_state->base.crtc;
2b875c22 14107 struct drm_framebuffer *fb = state->base.fb;
757f9a3e 14108 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
b29ec92c 14109 enum pipe pipe = to_intel_plane(plane)->pipe;
757f9a3e
GP
14110 unsigned stride;
14111 int ret;
3d7d6510 14112
061e4b8d
ML
14113 ret = drm_plane_helper_check_update(plane, crtc, fb, &state->src,
14114 &state->dst, &state->clip,
3d7d6510
MR
14115 DRM_PLANE_HELPER_NO_SCALING,
14116 DRM_PLANE_HELPER_NO_SCALING,
852e787c 14117 true, true, &state->visible);
757f9a3e
GP
14118 if (ret)
14119 return ret;
14120
757f9a3e
GP
14121 /* if we want to turn off the cursor ignore width and height */
14122 if (!obj)
da20eabd 14123 return 0;
757f9a3e 14124
757f9a3e 14125 /* Check for which cursor types we support */
061e4b8d 14126 if (!cursor_size_ok(plane->dev, state->base.crtc_w, state->base.crtc_h)) {
ea2c67bb
MR
14127 DRM_DEBUG("Cursor dimension %dx%d not supported\n",
14128 state->base.crtc_w, state->base.crtc_h);
757f9a3e
GP
14129 return -EINVAL;
14130 }
14131
ea2c67bb
MR
14132 stride = roundup_pow_of_two(state->base.crtc_w) * 4;
14133 if (obj->base.size < stride * state->base.crtc_h) {
757f9a3e
GP
14134 DRM_DEBUG_KMS("buffer is too small\n");
14135 return -ENOMEM;
14136 }
14137
3a656b54 14138 if (fb->modifier[0] != DRM_FORMAT_MOD_NONE) {
757f9a3e 14139 DRM_DEBUG_KMS("cursor cannot be tiled\n");
da20eabd 14140 return -EINVAL;
32b7eeec
MR
14141 }
14142
b29ec92c
VS
14143 /*
14144 * There's something wrong with the cursor on CHV pipe C.
14145 * If it straddles the left edge of the screen then
14146 * moving it away from the edge or disabling it often
14147 * results in a pipe underrun, and often that can lead to
14148 * dead pipe (constant underrun reported, and it scans
14149 * out just a solid color). To recover from that, the
14150 * display power well must be turned off and on again.
14151 * Refuse the put the cursor into that compromised position.
14152 */
14153 if (IS_CHERRYVIEW(plane->dev) && pipe == PIPE_C &&
14154 state->visible && state->base.crtc_x < 0) {
14155 DRM_DEBUG_KMS("CHV cursor C not allowed to straddle the left screen edge\n");
14156 return -EINVAL;
14157 }
14158
da20eabd 14159 return 0;
852e787c 14160}
3d7d6510 14161
a8ad0d8e
ML
14162static void
14163intel_disable_cursor_plane(struct drm_plane *plane,
7fabf5ef 14164 struct drm_crtc *crtc)
a8ad0d8e 14165{
f2858021
ML
14166 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
14167
14168 intel_crtc->cursor_addr = 0;
55a08b3f 14169 intel_crtc_update_cursor(crtc, NULL);
a8ad0d8e
ML
14170}
14171
f4a2cf29 14172static void
55a08b3f
ML
14173intel_update_cursor_plane(struct drm_plane *plane,
14174 const struct intel_crtc_state *crtc_state,
14175 const struct intel_plane_state *state)
852e787c 14176{
55a08b3f
ML
14177 struct drm_crtc *crtc = crtc_state->base.crtc;
14178 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ea2c67bb 14179 struct drm_device *dev = plane->dev;
2b875c22 14180 struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
a912f12f 14181 uint32_t addr;
852e787c 14182
f4a2cf29 14183 if (!obj)
a912f12f 14184 addr = 0;
f4a2cf29 14185 else if (!INTEL_INFO(dev)->cursor_needs_physical)
a912f12f 14186 addr = i915_gem_obj_ggtt_offset(obj);
f4a2cf29 14187 else
a912f12f 14188 addr = obj->phys_handle->busaddr;
852e787c 14189
a912f12f 14190 intel_crtc->cursor_addr = addr;
55a08b3f 14191 intel_crtc_update_cursor(crtc, state);
852e787c
GP
14192}
14193
3d7d6510
MR
14194static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
14195 int pipe)
14196{
14197 struct intel_plane *cursor;
8e7d688b 14198 struct intel_plane_state *state;
3d7d6510
MR
14199
14200 cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
14201 if (cursor == NULL)
14202 return NULL;
14203
8e7d688b
MR
14204 state = intel_create_plane_state(&cursor->base);
14205 if (!state) {
ea2c67bb
MR
14206 kfree(cursor);
14207 return NULL;
14208 }
8e7d688b 14209 cursor->base.state = &state->base;
ea2c67bb 14210
3d7d6510
MR
14211 cursor->can_scale = false;
14212 cursor->max_downscale = 1;
14213 cursor->pipe = pipe;
14214 cursor->plane = pipe;
a9ff8714 14215 cursor->frontbuffer_bit = INTEL_FRONTBUFFER_CURSOR(pipe);
c59cb179 14216 cursor->check_plane = intel_check_cursor_plane;
55a08b3f 14217 cursor->update_plane = intel_update_cursor_plane;
a8ad0d8e 14218 cursor->disable_plane = intel_disable_cursor_plane;
3d7d6510
MR
14219
14220 drm_universal_plane_init(dev, &cursor->base, 0,
65a3fea0 14221 &intel_plane_funcs,
3d7d6510
MR
14222 intel_cursor_formats,
14223 ARRAY_SIZE(intel_cursor_formats),
b0b3b795 14224 DRM_PLANE_TYPE_CURSOR, NULL);
4398ad45
VS
14225
14226 if (INTEL_INFO(dev)->gen >= 4) {
14227 if (!dev->mode_config.rotation_property)
14228 dev->mode_config.rotation_property =
14229 drm_mode_create_rotation_property(dev,
14230 BIT(DRM_ROTATE_0) |
14231 BIT(DRM_ROTATE_180));
14232 if (dev->mode_config.rotation_property)
14233 drm_object_attach_property(&cursor->base.base,
14234 dev->mode_config.rotation_property,
8e7d688b 14235 state->base.rotation);
4398ad45
VS
14236 }
14237
af99ceda
CK
14238 if (INTEL_INFO(dev)->gen >=9)
14239 state->scaler_id = -1;
14240
ea2c67bb
MR
14241 drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
14242
3d7d6510
MR
14243 return &cursor->base;
14244}
14245
549e2bfb
CK
14246static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
14247 struct intel_crtc_state *crtc_state)
14248{
14249 int i;
14250 struct intel_scaler *intel_scaler;
14251 struct intel_crtc_scaler_state *scaler_state = &crtc_state->scaler_state;
14252
14253 for (i = 0; i < intel_crtc->num_scalers; i++) {
14254 intel_scaler = &scaler_state->scalers[i];
14255 intel_scaler->in_use = 0;
549e2bfb
CK
14256 intel_scaler->mode = PS_SCALER_MODE_DYN;
14257 }
14258
14259 scaler_state->scaler_id = -1;
14260}
14261
b358d0a6 14262static void intel_crtc_init(struct drm_device *dev, int pipe)
79e53945 14263{
fbee40df 14264 struct drm_i915_private *dev_priv = dev->dev_private;
79e53945 14265 struct intel_crtc *intel_crtc;
f5de6e07 14266 struct intel_crtc_state *crtc_state = NULL;
3d7d6510
MR
14267 struct drm_plane *primary = NULL;
14268 struct drm_plane *cursor = NULL;
465c120c 14269 int i, ret;
79e53945 14270
955382f3 14271 intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
79e53945
JB
14272 if (intel_crtc == NULL)
14273 return;
14274
f5de6e07
ACO
14275 crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
14276 if (!crtc_state)
14277 goto fail;
550acefd
ACO
14278 intel_crtc->config = crtc_state;
14279 intel_crtc->base.state = &crtc_state->base;
07878248 14280 crtc_state->base.crtc = &intel_crtc->base;
f5de6e07 14281
549e2bfb
CK
14282 /* initialize shared scalers */
14283 if (INTEL_INFO(dev)->gen >= 9) {
14284 if (pipe == PIPE_C)
14285 intel_crtc->num_scalers = 1;
14286 else
14287 intel_crtc->num_scalers = SKL_NUM_SCALERS;
14288
14289 skl_init_scalers(dev, intel_crtc, crtc_state);
14290 }
14291
465c120c 14292 primary = intel_primary_plane_create(dev, pipe);
3d7d6510
MR
14293 if (!primary)
14294 goto fail;
14295
14296 cursor = intel_cursor_plane_create(dev, pipe);
14297 if (!cursor)
14298 goto fail;
14299
465c120c 14300 ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
f9882876 14301 cursor, &intel_crtc_funcs, NULL);
3d7d6510
MR
14302 if (ret)
14303 goto fail;
79e53945
JB
14304
14305 drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
79e53945
JB
14306 for (i = 0; i < 256; i++) {
14307 intel_crtc->lut_r[i] = i;
14308 intel_crtc->lut_g[i] = i;
14309 intel_crtc->lut_b[i] = i;
14310 }
14311
1f1c2e24
VS
14312 /*
14313 * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
8c0f92e1 14314 * is hooked to pipe B. Hence we want plane A feeding pipe B.
1f1c2e24 14315 */
80824003
JB
14316 intel_crtc->pipe = pipe;
14317 intel_crtc->plane = pipe;
3a77c4c4 14318 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
28c97730 14319 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
e2e767ab 14320 intel_crtc->plane = !pipe;
80824003
JB
14321 }
14322
4b0e333e
CW
14323 intel_crtc->cursor_base = ~0;
14324 intel_crtc->cursor_cntl = ~0;
dc41c154 14325 intel_crtc->cursor_size = ~0;
8d7849db 14326
852eb00d
VS
14327 intel_crtc->wm.cxsr_allowed = true;
14328
22fd0fab
JB
14329 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
14330 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
14331 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
14332 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
14333
79e53945 14334 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
87b6b101
DV
14335
14336 WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
3d7d6510
MR
14337 return;
14338
14339fail:
14340 if (primary)
14341 drm_plane_cleanup(primary);
14342 if (cursor)
14343 drm_plane_cleanup(cursor);
f5de6e07 14344 kfree(crtc_state);
3d7d6510 14345 kfree(intel_crtc);
79e53945
JB
14346}
14347
752aa88a
JB
14348enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
14349{
14350 struct drm_encoder *encoder = connector->base.encoder;
6e9f798d 14351 struct drm_device *dev = connector->base.dev;
752aa88a 14352
51fd371b 14353 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
752aa88a 14354
d3babd3f 14355 if (!encoder || WARN_ON(!encoder->crtc))
752aa88a
JB
14356 return INVALID_PIPE;
14357
14358 return to_intel_crtc(encoder->crtc)->pipe;
14359}
14360
08d7b3d1 14361int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
05394f39 14362 struct drm_file *file)
08d7b3d1 14363{
08d7b3d1 14364 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
7707e653 14365 struct drm_crtc *drmmode_crtc;
c05422d5 14366 struct intel_crtc *crtc;
08d7b3d1 14367
7707e653 14368 drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
08d7b3d1 14369
7707e653 14370 if (!drmmode_crtc) {
08d7b3d1 14371 DRM_ERROR("no such CRTC id\n");
3f2c2057 14372 return -ENOENT;
08d7b3d1
CW
14373 }
14374
7707e653 14375 crtc = to_intel_crtc(drmmode_crtc);
c05422d5 14376 pipe_from_crtc_id->pipe = crtc->pipe;
08d7b3d1 14377
c05422d5 14378 return 0;
08d7b3d1
CW
14379}
14380
66a9278e 14381static int intel_encoder_clones(struct intel_encoder *encoder)
79e53945 14382{
66a9278e
DV
14383 struct drm_device *dev = encoder->base.dev;
14384 struct intel_encoder *source_encoder;
79e53945 14385 int index_mask = 0;
79e53945
JB
14386 int entry = 0;
14387
b2784e15 14388 for_each_intel_encoder(dev, source_encoder) {
bc079e8b 14389 if (encoders_cloneable(encoder, source_encoder))
66a9278e
DV
14390 index_mask |= (1 << entry);
14391
79e53945
JB
14392 entry++;
14393 }
4ef69c7a 14394
79e53945
JB
14395 return index_mask;
14396}
14397
4d302442
CW
14398static bool has_edp_a(struct drm_device *dev)
14399{
14400 struct drm_i915_private *dev_priv = dev->dev_private;
14401
14402 if (!IS_MOBILE(dev))
14403 return false;
14404
14405 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
14406 return false;
14407
e3589908 14408 if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
4d302442
CW
14409 return false;
14410
14411 return true;
14412}
14413
84b4e042
JB
14414static bool intel_crt_present(struct drm_device *dev)
14415{
14416 struct drm_i915_private *dev_priv = dev->dev_private;
14417
884497ed
DL
14418 if (INTEL_INFO(dev)->gen >= 9)
14419 return false;
14420
cf404ce4 14421 if (IS_HSW_ULT(dev) || IS_BDW_ULT(dev))
84b4e042
JB
14422 return false;
14423
14424 if (IS_CHERRYVIEW(dev))
14425 return false;
14426
65e472e4
VS
14427 if (HAS_PCH_LPT_H(dev) && I915_READ(SFUSE_STRAP) & SFUSE_STRAP_CRT_DISABLED)
14428 return false;
14429
70ac54d0
VS
14430 /* DDI E can't be used if DDI A requires 4 lanes */
14431 if (HAS_DDI(dev) && I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_A_4_LANES)
14432 return false;
14433
e4abb733 14434 if (!dev_priv->vbt.int_crt_support)
84b4e042
JB
14435 return false;
14436
14437 return true;
14438}
14439
79e53945
JB
14440static void intel_setup_outputs(struct drm_device *dev)
14441{
725e30ad 14442 struct drm_i915_private *dev_priv = dev->dev_private;
4ef69c7a 14443 struct intel_encoder *encoder;
cb0953d7 14444 bool dpd_is_edp = false;
79e53945 14445
c9093354 14446 intel_lvds_init(dev);
79e53945 14447
84b4e042 14448 if (intel_crt_present(dev))
79935fca 14449 intel_crt_init(dev);
cb0953d7 14450
c776eb2e
VK
14451 if (IS_BROXTON(dev)) {
14452 /*
14453 * FIXME: Broxton doesn't support port detection via the
14454 * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
14455 * detect the ports.
14456 */
14457 intel_ddi_init(dev, PORT_A);
14458 intel_ddi_init(dev, PORT_B);
14459 intel_ddi_init(dev, PORT_C);
14460 } else if (HAS_DDI(dev)) {
0e72a5b5
ED
14461 int found;
14462
de31facd
JB
14463 /*
14464 * Haswell uses DDI functions to detect digital outputs.
14465 * On SKL pre-D0 the strap isn't connected, so we assume
14466 * it's there.
14467 */
77179400 14468 found = I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_INIT_DISPLAY_DETECTED;
de31facd 14469 /* WaIgnoreDDIAStrap: skl */
ef11bdb3 14470 if (found || IS_SKYLAKE(dev) || IS_KABYLAKE(dev))
0e72a5b5
ED
14471 intel_ddi_init(dev, PORT_A);
14472
14473 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
14474 * register */
14475 found = I915_READ(SFUSE_STRAP);
14476
14477 if (found & SFUSE_STRAP_DDIB_DETECTED)
14478 intel_ddi_init(dev, PORT_B);
14479 if (found & SFUSE_STRAP_DDIC_DETECTED)
14480 intel_ddi_init(dev, PORT_C);
14481 if (found & SFUSE_STRAP_DDID_DETECTED)
14482 intel_ddi_init(dev, PORT_D);
2800e4c2
RV
14483 /*
14484 * On SKL we don't have a way to detect DDI-E so we rely on VBT.
14485 */
ef11bdb3 14486 if ((IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) &&
2800e4c2
RV
14487 (dev_priv->vbt.ddi_port_info[PORT_E].supports_dp ||
14488 dev_priv->vbt.ddi_port_info[PORT_E].supports_dvi ||
14489 dev_priv->vbt.ddi_port_info[PORT_E].supports_hdmi))
14490 intel_ddi_init(dev, PORT_E);
14491
0e72a5b5 14492 } else if (HAS_PCH_SPLIT(dev)) {
cb0953d7 14493 int found;
5d8a7752 14494 dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
270b3042
DV
14495
14496 if (has_edp_a(dev))
14497 intel_dp_init(dev, DP_A, PORT_A);
cb0953d7 14498
dc0fa718 14499 if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
461ed3ca 14500 /* PCH SDVOB multiplex with HDMIB */
2a5c0832 14501 found = intel_sdvo_init(dev, PCH_SDVOB, PORT_B);
30ad48b7 14502 if (!found)
e2debe91 14503 intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
5eb08b69 14504 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
ab9d7c30 14505 intel_dp_init(dev, PCH_DP_B, PORT_B);
30ad48b7
ZW
14506 }
14507
dc0fa718 14508 if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
e2debe91 14509 intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
30ad48b7 14510
dc0fa718 14511 if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
e2debe91 14512 intel_hdmi_init(dev, PCH_HDMID, PORT_D);
30ad48b7 14513
5eb08b69 14514 if (I915_READ(PCH_DP_C) & DP_DETECTED)
ab9d7c30 14515 intel_dp_init(dev, PCH_DP_C, PORT_C);
5eb08b69 14516
270b3042 14517 if (I915_READ(PCH_DP_D) & DP_DETECTED)
ab9d7c30 14518 intel_dp_init(dev, PCH_DP_D, PORT_D);
666a4537 14519 } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
e17ac6db
VS
14520 /*
14521 * The DP_DETECTED bit is the latched state of the DDC
14522 * SDA pin at boot. However since eDP doesn't require DDC
14523 * (no way to plug in a DP->HDMI dongle) the DDC pins for
14524 * eDP ports may have been muxed to an alternate function.
14525 * Thus we can't rely on the DP_DETECTED bit alone to detect
14526 * eDP ports. Consult the VBT as well as DP_DETECTED to
14527 * detect eDP ports.
14528 */
e66eb81d 14529 if (I915_READ(VLV_HDMIB) & SDVO_DETECTED &&
d2182a66 14530 !intel_dp_is_edp(dev, PORT_B))
e66eb81d
VS
14531 intel_hdmi_init(dev, VLV_HDMIB, PORT_B);
14532 if (I915_READ(VLV_DP_B) & DP_DETECTED ||
e17ac6db 14533 intel_dp_is_edp(dev, PORT_B))
e66eb81d 14534 intel_dp_init(dev, VLV_DP_B, PORT_B);
585a94b8 14535
e66eb81d 14536 if (I915_READ(VLV_HDMIC) & SDVO_DETECTED &&
d2182a66 14537 !intel_dp_is_edp(dev, PORT_C))
e66eb81d
VS
14538 intel_hdmi_init(dev, VLV_HDMIC, PORT_C);
14539 if (I915_READ(VLV_DP_C) & DP_DETECTED ||
e17ac6db 14540 intel_dp_is_edp(dev, PORT_C))
e66eb81d 14541 intel_dp_init(dev, VLV_DP_C, PORT_C);
19c03924 14542
9418c1f1 14543 if (IS_CHERRYVIEW(dev)) {
e17ac6db 14544 /* eDP not supported on port D, so don't check VBT */
e66eb81d
VS
14545 if (I915_READ(CHV_HDMID) & SDVO_DETECTED)
14546 intel_hdmi_init(dev, CHV_HDMID, PORT_D);
14547 if (I915_READ(CHV_DP_D) & DP_DETECTED)
14548 intel_dp_init(dev, CHV_DP_D, PORT_D);
9418c1f1
VS
14549 }
14550
3cfca973 14551 intel_dsi_init(dev);
09da55dc 14552 } else if (!IS_GEN2(dev) && !IS_PINEVIEW(dev)) {
27185ae1 14553 bool found = false;
7d57382e 14554
e2debe91 14555 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
b01f2c3a 14556 DRM_DEBUG_KMS("probing SDVOB\n");
2a5c0832 14557 found = intel_sdvo_init(dev, GEN3_SDVOB, PORT_B);
3fec3d2f 14558 if (!found && IS_G4X(dev)) {
b01f2c3a 14559 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
e2debe91 14560 intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
b01f2c3a 14561 }
27185ae1 14562
3fec3d2f 14563 if (!found && IS_G4X(dev))
ab9d7c30 14564 intel_dp_init(dev, DP_B, PORT_B);
725e30ad 14565 }
13520b05
KH
14566
14567 /* Before G4X SDVOC doesn't have its own detect register */
13520b05 14568
e2debe91 14569 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
b01f2c3a 14570 DRM_DEBUG_KMS("probing SDVOC\n");
2a5c0832 14571 found = intel_sdvo_init(dev, GEN3_SDVOC, PORT_C);
b01f2c3a 14572 }
27185ae1 14573
e2debe91 14574 if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
27185ae1 14575
3fec3d2f 14576 if (IS_G4X(dev)) {
b01f2c3a 14577 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
e2debe91 14578 intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
b01f2c3a 14579 }
3fec3d2f 14580 if (IS_G4X(dev))
ab9d7c30 14581 intel_dp_init(dev, DP_C, PORT_C);
725e30ad 14582 }
27185ae1 14583
3fec3d2f 14584 if (IS_G4X(dev) &&
e7281eab 14585 (I915_READ(DP_D) & DP_DETECTED))
ab9d7c30 14586 intel_dp_init(dev, DP_D, PORT_D);
bad720ff 14587 } else if (IS_GEN2(dev))
79e53945
JB
14588 intel_dvo_init(dev);
14589
103a196f 14590 if (SUPPORTS_TV(dev))
79e53945
JB
14591 intel_tv_init(dev);
14592
0bc12bcb 14593 intel_psr_init(dev);
7c8f8a70 14594
b2784e15 14595 for_each_intel_encoder(dev, encoder) {
4ef69c7a
CW
14596 encoder->base.possible_crtcs = encoder->crtc_mask;
14597 encoder->base.possible_clones =
66a9278e 14598 intel_encoder_clones(encoder);
79e53945 14599 }
47356eb6 14600
dde86e2d 14601 intel_init_pch_refclk(dev);
270b3042
DV
14602
14603 drm_helper_move_panel_connectors_to_head(dev);
79e53945
JB
14604}
14605
14606static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
14607{
60a5ca01 14608 struct drm_device *dev = fb->dev;
79e53945 14609 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
79e53945 14610
ef2d633e 14611 drm_framebuffer_cleanup(fb);
60a5ca01 14612 mutex_lock(&dev->struct_mutex);
ef2d633e 14613 WARN_ON(!intel_fb->obj->framebuffer_references--);
60a5ca01
VS
14614 drm_gem_object_unreference(&intel_fb->obj->base);
14615 mutex_unlock(&dev->struct_mutex);
79e53945
JB
14616 kfree(intel_fb);
14617}
14618
14619static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
05394f39 14620 struct drm_file *file,
79e53945
JB
14621 unsigned int *handle)
14622{
14623 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
05394f39 14624 struct drm_i915_gem_object *obj = intel_fb->obj;
79e53945 14625
cc917ab4
CW
14626 if (obj->userptr.mm) {
14627 DRM_DEBUG("attempting to use a userptr for a framebuffer, denied\n");
14628 return -EINVAL;
14629 }
14630
05394f39 14631 return drm_gem_handle_create(file, &obj->base, handle);
79e53945
JB
14632}
14633
86c98588
RV
14634static int intel_user_framebuffer_dirty(struct drm_framebuffer *fb,
14635 struct drm_file *file,
14636 unsigned flags, unsigned color,
14637 struct drm_clip_rect *clips,
14638 unsigned num_clips)
14639{
14640 struct drm_device *dev = fb->dev;
14641 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
14642 struct drm_i915_gem_object *obj = intel_fb->obj;
14643
14644 mutex_lock(&dev->struct_mutex);
74b4ea1e 14645 intel_fb_obj_flush(obj, false, ORIGIN_DIRTYFB);
86c98588
RV
14646 mutex_unlock(&dev->struct_mutex);
14647
14648 return 0;
14649}
14650
79e53945
JB
14651static const struct drm_framebuffer_funcs intel_fb_funcs = {
14652 .destroy = intel_user_framebuffer_destroy,
14653 .create_handle = intel_user_framebuffer_create_handle,
86c98588 14654 .dirty = intel_user_framebuffer_dirty,
79e53945
JB
14655};
14656
b321803d
DL
14657static
14658u32 intel_fb_pitch_limit(struct drm_device *dev, uint64_t fb_modifier,
14659 uint32_t pixel_format)
14660{
14661 u32 gen = INTEL_INFO(dev)->gen;
14662
14663 if (gen >= 9) {
ac484963
VS
14664 int cpp = drm_format_plane_cpp(pixel_format, 0);
14665
b321803d
DL
14666 /* "The stride in bytes must not exceed the of the size of 8K
14667 * pixels and 32K bytes."
14668 */
ac484963 14669 return min(8192 * cpp, 32768);
666a4537 14670 } else if (gen >= 5 && !IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
b321803d
DL
14671 return 32*1024;
14672 } else if (gen >= 4) {
14673 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14674 return 16*1024;
14675 else
14676 return 32*1024;
14677 } else if (gen >= 3) {
14678 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14679 return 8*1024;
14680 else
14681 return 16*1024;
14682 } else {
14683 /* XXX DSPC is limited to 4k tiled */
14684 return 8*1024;
14685 }
14686}
14687
b5ea642a
DV
14688static int intel_framebuffer_init(struct drm_device *dev,
14689 struct intel_framebuffer *intel_fb,
14690 struct drm_mode_fb_cmd2 *mode_cmd,
14691 struct drm_i915_gem_object *obj)
79e53945 14692{
7b49f948 14693 struct drm_i915_private *dev_priv = to_i915(dev);
6761dd31 14694 unsigned int aligned_height;
79e53945 14695 int ret;
b321803d 14696 u32 pitch_limit, stride_alignment;
79e53945 14697
dd4916c5
DV
14698 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
14699
2a80eada
DV
14700 if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
14701 /* Enforce that fb modifier and tiling mode match, but only for
14702 * X-tiled. This is needed for FBC. */
14703 if (!!(obj->tiling_mode == I915_TILING_X) !=
14704 !!(mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED)) {
14705 DRM_DEBUG("tiling_mode doesn't match fb modifier\n");
14706 return -EINVAL;
14707 }
14708 } else {
14709 if (obj->tiling_mode == I915_TILING_X)
14710 mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
14711 else if (obj->tiling_mode == I915_TILING_Y) {
14712 DRM_DEBUG("No Y tiling for legacy addfb\n");
14713 return -EINVAL;
14714 }
14715 }
14716
9a8f0a12
TU
14717 /* Passed in modifier sanity checking. */
14718 switch (mode_cmd->modifier[0]) {
14719 case I915_FORMAT_MOD_Y_TILED:
14720 case I915_FORMAT_MOD_Yf_TILED:
14721 if (INTEL_INFO(dev)->gen < 9) {
14722 DRM_DEBUG("Unsupported tiling 0x%llx!\n",
14723 mode_cmd->modifier[0]);
14724 return -EINVAL;
14725 }
14726 case DRM_FORMAT_MOD_NONE:
14727 case I915_FORMAT_MOD_X_TILED:
14728 break;
14729 default:
c0f40428
JB
14730 DRM_DEBUG("Unsupported fb modifier 0x%llx!\n",
14731 mode_cmd->modifier[0]);
57cd6508 14732 return -EINVAL;
c16ed4be 14733 }
57cd6508 14734
7b49f948
VS
14735 stride_alignment = intel_fb_stride_alignment(dev_priv,
14736 mode_cmd->modifier[0],
b321803d
DL
14737 mode_cmd->pixel_format);
14738 if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
14739 DRM_DEBUG("pitch (%d) must be at least %u byte aligned\n",
14740 mode_cmd->pitches[0], stride_alignment);
57cd6508 14741 return -EINVAL;
c16ed4be 14742 }
57cd6508 14743
b321803d
DL
14744 pitch_limit = intel_fb_pitch_limit(dev, mode_cmd->modifier[0],
14745 mode_cmd->pixel_format);
a35cdaa0 14746 if (mode_cmd->pitches[0] > pitch_limit) {
b321803d
DL
14747 DRM_DEBUG("%s pitch (%u) must be at less than %d\n",
14748 mode_cmd->modifier[0] != DRM_FORMAT_MOD_NONE ?
2a80eada 14749 "tiled" : "linear",
a35cdaa0 14750 mode_cmd->pitches[0], pitch_limit);
5d7bd705 14751 return -EINVAL;
c16ed4be 14752 }
5d7bd705 14753
2a80eada 14754 if (mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED &&
c16ed4be
CW
14755 mode_cmd->pitches[0] != obj->stride) {
14756 DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
14757 mode_cmd->pitches[0], obj->stride);
5d7bd705 14758 return -EINVAL;
c16ed4be 14759 }
5d7bd705 14760
57779d06 14761 /* Reject formats not supported by any plane early. */
308e5bcb 14762 switch (mode_cmd->pixel_format) {
57779d06 14763 case DRM_FORMAT_C8:
04b3924d
VS
14764 case DRM_FORMAT_RGB565:
14765 case DRM_FORMAT_XRGB8888:
14766 case DRM_FORMAT_ARGB8888:
57779d06
VS
14767 break;
14768 case DRM_FORMAT_XRGB1555:
c16ed4be 14769 if (INTEL_INFO(dev)->gen > 3) {
4ee62c76
VS
14770 DRM_DEBUG("unsupported pixel format: %s\n",
14771 drm_get_format_name(mode_cmd->pixel_format));
57779d06 14772 return -EINVAL;
c16ed4be 14773 }
57779d06 14774 break;
57779d06 14775 case DRM_FORMAT_ABGR8888:
666a4537
WB
14776 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev) &&
14777 INTEL_INFO(dev)->gen < 9) {
6c0fd451
DL
14778 DRM_DEBUG("unsupported pixel format: %s\n",
14779 drm_get_format_name(mode_cmd->pixel_format));
14780 return -EINVAL;
14781 }
14782 break;
14783 case DRM_FORMAT_XBGR8888:
04b3924d 14784 case DRM_FORMAT_XRGB2101010:
57779d06 14785 case DRM_FORMAT_XBGR2101010:
c16ed4be 14786 if (INTEL_INFO(dev)->gen < 4) {
4ee62c76
VS
14787 DRM_DEBUG("unsupported pixel format: %s\n",
14788 drm_get_format_name(mode_cmd->pixel_format));
57779d06 14789 return -EINVAL;
c16ed4be 14790 }
b5626747 14791 break;
7531208b 14792 case DRM_FORMAT_ABGR2101010:
666a4537 14793 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
7531208b
DL
14794 DRM_DEBUG("unsupported pixel format: %s\n",
14795 drm_get_format_name(mode_cmd->pixel_format));
14796 return -EINVAL;
14797 }
14798 break;
04b3924d
VS
14799 case DRM_FORMAT_YUYV:
14800 case DRM_FORMAT_UYVY:
14801 case DRM_FORMAT_YVYU:
14802 case DRM_FORMAT_VYUY:
c16ed4be 14803 if (INTEL_INFO(dev)->gen < 5) {
4ee62c76
VS
14804 DRM_DEBUG("unsupported pixel format: %s\n",
14805 drm_get_format_name(mode_cmd->pixel_format));
57779d06 14806 return -EINVAL;
c16ed4be 14807 }
57cd6508
CW
14808 break;
14809 default:
4ee62c76
VS
14810 DRM_DEBUG("unsupported pixel format: %s\n",
14811 drm_get_format_name(mode_cmd->pixel_format));
57cd6508
CW
14812 return -EINVAL;
14813 }
14814
90f9a336
VS
14815 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
14816 if (mode_cmd->offsets[0] != 0)
14817 return -EINVAL;
14818
ec2c981e 14819 aligned_height = intel_fb_align_height(dev, mode_cmd->height,
091df6cb
DV
14820 mode_cmd->pixel_format,
14821 mode_cmd->modifier[0]);
53155c0a
DV
14822 /* FIXME drm helper for size checks (especially planar formats)? */
14823 if (obj->base.size < aligned_height * mode_cmd->pitches[0])
14824 return -EINVAL;
14825
c7d73f6a
DV
14826 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
14827 intel_fb->obj = obj;
14828
2d7a215f
VS
14829 intel_fill_fb_info(dev_priv, &intel_fb->base);
14830
79e53945
JB
14831 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
14832 if (ret) {
14833 DRM_ERROR("framebuffer init failed %d\n", ret);
14834 return ret;
14835 }
14836
0b05e1e0
VS
14837 intel_fb->obj->framebuffer_references++;
14838
79e53945
JB
14839 return 0;
14840}
14841
79e53945
JB
14842static struct drm_framebuffer *
14843intel_user_framebuffer_create(struct drm_device *dev,
14844 struct drm_file *filp,
1eb83451 14845 const struct drm_mode_fb_cmd2 *user_mode_cmd)
79e53945 14846{
dcb1394e 14847 struct drm_framebuffer *fb;
05394f39 14848 struct drm_i915_gem_object *obj;
76dc3769 14849 struct drm_mode_fb_cmd2 mode_cmd = *user_mode_cmd;
79e53945 14850
308e5bcb 14851 obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
76dc3769 14852 mode_cmd.handles[0]));
c8725226 14853 if (&obj->base == NULL)
cce13ff7 14854 return ERR_PTR(-ENOENT);
79e53945 14855
92907cbb 14856 fb = intel_framebuffer_create(dev, &mode_cmd, obj);
dcb1394e
LW
14857 if (IS_ERR(fb))
14858 drm_gem_object_unreference_unlocked(&obj->base);
14859
14860 return fb;
79e53945
JB
14861}
14862
0695726e 14863#ifndef CONFIG_DRM_FBDEV_EMULATION
0632fef6 14864static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
4520f53a
DV
14865{
14866}
14867#endif
14868
79e53945 14869static const struct drm_mode_config_funcs intel_mode_funcs = {
79e53945 14870 .fb_create = intel_user_framebuffer_create,
0632fef6 14871 .output_poll_changed = intel_fbdev_output_poll_changed,
5ee67f1c
MR
14872 .atomic_check = intel_atomic_check,
14873 .atomic_commit = intel_atomic_commit,
de419ab6
ML
14874 .atomic_state_alloc = intel_atomic_state_alloc,
14875 .atomic_state_clear = intel_atomic_state_clear,
79e53945
JB
14876};
14877
88212941
ID
14878/**
14879 * intel_init_display_hooks - initialize the display modesetting hooks
14880 * @dev_priv: device private
14881 */
14882void intel_init_display_hooks(struct drm_i915_private *dev_priv)
e70236a8 14883{
88212941 14884 if (HAS_PCH_SPLIT(dev_priv) || IS_G4X(dev_priv))
ee9300bb 14885 dev_priv->display.find_dpll = g4x_find_best_dpll;
88212941 14886 else if (IS_CHERRYVIEW(dev_priv))
ef9348c8 14887 dev_priv->display.find_dpll = chv_find_best_dpll;
88212941 14888 else if (IS_VALLEYVIEW(dev_priv))
ee9300bb 14889 dev_priv->display.find_dpll = vlv_find_best_dpll;
88212941 14890 else if (IS_PINEVIEW(dev_priv))
ee9300bb
DV
14891 dev_priv->display.find_dpll = pnv_find_best_dpll;
14892 else
14893 dev_priv->display.find_dpll = i9xx_find_best_dpll;
14894
88212941 14895 if (INTEL_INFO(dev_priv)->gen >= 9) {
bc8d7dff 14896 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
5724dbd1
DL
14897 dev_priv->display.get_initial_plane_config =
14898 skylake_get_initial_plane_config;
bc8d7dff
DL
14899 dev_priv->display.crtc_compute_clock =
14900 haswell_crtc_compute_clock;
14901 dev_priv->display.crtc_enable = haswell_crtc_enable;
14902 dev_priv->display.crtc_disable = haswell_crtc_disable;
88212941 14903 } else if (HAS_DDI(dev_priv)) {
0e8ffe1b 14904 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
5724dbd1
DL
14905 dev_priv->display.get_initial_plane_config =
14906 ironlake_get_initial_plane_config;
797d0259
ACO
14907 dev_priv->display.crtc_compute_clock =
14908 haswell_crtc_compute_clock;
4f771f10
PZ
14909 dev_priv->display.crtc_enable = haswell_crtc_enable;
14910 dev_priv->display.crtc_disable = haswell_crtc_disable;
88212941 14911 } else if (HAS_PCH_SPLIT(dev_priv)) {
0e8ffe1b 14912 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
5724dbd1
DL
14913 dev_priv->display.get_initial_plane_config =
14914 ironlake_get_initial_plane_config;
3fb37703
ACO
14915 dev_priv->display.crtc_compute_clock =
14916 ironlake_crtc_compute_clock;
76e5a89c
DV
14917 dev_priv->display.crtc_enable = ironlake_crtc_enable;
14918 dev_priv->display.crtc_disable = ironlake_crtc_disable;
88212941 14919 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
89b667f8 14920 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
5724dbd1
DL
14921 dev_priv->display.get_initial_plane_config =
14922 i9xx_get_initial_plane_config;
d6dfee7a 14923 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
89b667f8
JB
14924 dev_priv->display.crtc_enable = valleyview_crtc_enable;
14925 dev_priv->display.crtc_disable = i9xx_crtc_disable;
f564048e 14926 } else {
0e8ffe1b 14927 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
5724dbd1
DL
14928 dev_priv->display.get_initial_plane_config =
14929 i9xx_get_initial_plane_config;
d6dfee7a 14930 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
76e5a89c
DV
14931 dev_priv->display.crtc_enable = i9xx_crtc_enable;
14932 dev_priv->display.crtc_disable = i9xx_crtc_disable;
f564048e 14933 }
e70236a8 14934
e70236a8 14935 /* Returns the core display clock speed */
88212941 14936 if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv))
1652d19e
VS
14937 dev_priv->display.get_display_clock_speed =
14938 skylake_get_display_clock_speed;
88212941 14939 else if (IS_BROXTON(dev_priv))
acd3f3d3
BP
14940 dev_priv->display.get_display_clock_speed =
14941 broxton_get_display_clock_speed;
88212941 14942 else if (IS_BROADWELL(dev_priv))
1652d19e
VS
14943 dev_priv->display.get_display_clock_speed =
14944 broadwell_get_display_clock_speed;
88212941 14945 else if (IS_HASWELL(dev_priv))
1652d19e
VS
14946 dev_priv->display.get_display_clock_speed =
14947 haswell_get_display_clock_speed;
88212941 14948 else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
25eb05fc
JB
14949 dev_priv->display.get_display_clock_speed =
14950 valleyview_get_display_clock_speed;
88212941 14951 else if (IS_GEN5(dev_priv))
b37a6434
VS
14952 dev_priv->display.get_display_clock_speed =
14953 ilk_get_display_clock_speed;
88212941
ID
14954 else if (IS_I945G(dev_priv) || IS_BROADWATER(dev_priv) ||
14955 IS_GEN6(dev_priv) || IS_IVYBRIDGE(dev_priv))
e70236a8
JB
14956 dev_priv->display.get_display_clock_speed =
14957 i945_get_display_clock_speed;
88212941 14958 else if (IS_GM45(dev_priv))
34edce2f
VS
14959 dev_priv->display.get_display_clock_speed =
14960 gm45_get_display_clock_speed;
88212941 14961 else if (IS_CRESTLINE(dev_priv))
34edce2f
VS
14962 dev_priv->display.get_display_clock_speed =
14963 i965gm_get_display_clock_speed;
88212941 14964 else if (IS_PINEVIEW(dev_priv))
34edce2f
VS
14965 dev_priv->display.get_display_clock_speed =
14966 pnv_get_display_clock_speed;
88212941 14967 else if (IS_G33(dev_priv) || IS_G4X(dev_priv))
34edce2f
VS
14968 dev_priv->display.get_display_clock_speed =
14969 g33_get_display_clock_speed;
88212941 14970 else if (IS_I915G(dev_priv))
e70236a8
JB
14971 dev_priv->display.get_display_clock_speed =
14972 i915_get_display_clock_speed;
88212941 14973 else if (IS_I945GM(dev_priv) || IS_845G(dev_priv))
e70236a8
JB
14974 dev_priv->display.get_display_clock_speed =
14975 i9xx_misc_get_display_clock_speed;
88212941 14976 else if (IS_I915GM(dev_priv))
e70236a8
JB
14977 dev_priv->display.get_display_clock_speed =
14978 i915gm_get_display_clock_speed;
88212941 14979 else if (IS_I865G(dev_priv))
e70236a8
JB
14980 dev_priv->display.get_display_clock_speed =
14981 i865_get_display_clock_speed;
88212941 14982 else if (IS_I85X(dev_priv))
e70236a8 14983 dev_priv->display.get_display_clock_speed =
1b1d2716 14984 i85x_get_display_clock_speed;
623e01e5 14985 else { /* 830 */
88212941 14986 WARN(!IS_I830(dev_priv), "Unknown platform. Assuming 133 MHz CDCLK\n");
e70236a8
JB
14987 dev_priv->display.get_display_clock_speed =
14988 i830_get_display_clock_speed;
623e01e5 14989 }
e70236a8 14990
88212941 14991 if (IS_GEN5(dev_priv)) {
3bb11b53 14992 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
88212941 14993 } else if (IS_GEN6(dev_priv)) {
3bb11b53 14994 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
88212941 14995 } else if (IS_IVYBRIDGE(dev_priv)) {
3bb11b53
SJ
14996 /* FIXME: detect B0+ stepping and use auto training */
14997 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
88212941 14998 } else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
3bb11b53 14999 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
88212941 15000 if (IS_BROADWELL(dev_priv)) {
27c329ed
ML
15001 dev_priv->display.modeset_commit_cdclk =
15002 broadwell_modeset_commit_cdclk;
15003 dev_priv->display.modeset_calc_cdclk =
15004 broadwell_modeset_calc_cdclk;
15005 }
88212941 15006 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
27c329ed
ML
15007 dev_priv->display.modeset_commit_cdclk =
15008 valleyview_modeset_commit_cdclk;
15009 dev_priv->display.modeset_calc_cdclk =
15010 valleyview_modeset_calc_cdclk;
88212941 15011 } else if (IS_BROXTON(dev_priv)) {
27c329ed
ML
15012 dev_priv->display.modeset_commit_cdclk =
15013 broxton_modeset_commit_cdclk;
15014 dev_priv->display.modeset_calc_cdclk =
15015 broxton_modeset_calc_cdclk;
e70236a8 15016 }
8c9f3aaf 15017
88212941 15018 switch (INTEL_INFO(dev_priv)->gen) {
8c9f3aaf
JB
15019 case 2:
15020 dev_priv->display.queue_flip = intel_gen2_queue_flip;
15021 break;
15022
15023 case 3:
15024 dev_priv->display.queue_flip = intel_gen3_queue_flip;
15025 break;
15026
15027 case 4:
15028 case 5:
15029 dev_priv->display.queue_flip = intel_gen4_queue_flip;
15030 break;
15031
15032 case 6:
15033 dev_priv->display.queue_flip = intel_gen6_queue_flip;
15034 break;
7c9017e5 15035 case 7:
4e0bbc31 15036 case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
7c9017e5
JB
15037 dev_priv->display.queue_flip = intel_gen7_queue_flip;
15038 break;
830c81db 15039 case 9:
ba343e02
TU
15040 /* Drop through - unsupported since execlist only. */
15041 default:
15042 /* Default just returns -ENODEV to indicate unsupported */
15043 dev_priv->display.queue_flip = intel_default_queue_flip;
8c9f3aaf 15044 }
e70236a8
JB
15045}
15046
b690e96c
JB
15047/*
15048 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
15049 * resume, or other times. This quirk makes sure that's the case for
15050 * affected systems.
15051 */
0206e353 15052static void quirk_pipea_force(struct drm_device *dev)
b690e96c
JB
15053{
15054 struct drm_i915_private *dev_priv = dev->dev_private;
15055
15056 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
bc0daf48 15057 DRM_INFO("applying pipe a force quirk\n");
b690e96c
JB
15058}
15059
b6b5d049
VS
15060static void quirk_pipeb_force(struct drm_device *dev)
15061{
15062 struct drm_i915_private *dev_priv = dev->dev_private;
15063
15064 dev_priv->quirks |= QUIRK_PIPEB_FORCE;
15065 DRM_INFO("applying pipe b force quirk\n");
15066}
15067
435793df
KP
15068/*
15069 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
15070 */
15071static void quirk_ssc_force_disable(struct drm_device *dev)
15072{
15073 struct drm_i915_private *dev_priv = dev->dev_private;
15074 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
bc0daf48 15075 DRM_INFO("applying lvds SSC disable quirk\n");
435793df
KP
15076}
15077
4dca20ef 15078/*
5a15ab5b
CE
15079 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
15080 * brightness value
4dca20ef
CE
15081 */
15082static void quirk_invert_brightness(struct drm_device *dev)
15083{
15084 struct drm_i915_private *dev_priv = dev->dev_private;
15085 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
bc0daf48 15086 DRM_INFO("applying inverted panel brightness quirk\n");
435793df
KP
15087}
15088
9c72cc6f
SD
15089/* Some VBT's incorrectly indicate no backlight is present */
15090static void quirk_backlight_present(struct drm_device *dev)
15091{
15092 struct drm_i915_private *dev_priv = dev->dev_private;
15093 dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
15094 DRM_INFO("applying backlight present quirk\n");
15095}
15096
b690e96c
JB
15097struct intel_quirk {
15098 int device;
15099 int subsystem_vendor;
15100 int subsystem_device;
15101 void (*hook)(struct drm_device *dev);
15102};
15103
5f85f176
EE
15104/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
15105struct intel_dmi_quirk {
15106 void (*hook)(struct drm_device *dev);
15107 const struct dmi_system_id (*dmi_id_list)[];
15108};
15109
15110static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
15111{
15112 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
15113 return 1;
15114}
15115
15116static const struct intel_dmi_quirk intel_dmi_quirks[] = {
15117 {
15118 .dmi_id_list = &(const struct dmi_system_id[]) {
15119 {
15120 .callback = intel_dmi_reverse_brightness,
15121 .ident = "NCR Corporation",
15122 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
15123 DMI_MATCH(DMI_PRODUCT_NAME, ""),
15124 },
15125 },
15126 { } /* terminating entry */
15127 },
15128 .hook = quirk_invert_brightness,
15129 },
15130};
15131
c43b5634 15132static struct intel_quirk intel_quirks[] = {
b690e96c
JB
15133 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
15134 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
15135
b690e96c
JB
15136 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
15137 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
15138
5f080c0f
VS
15139 /* 830 needs to leave pipe A & dpll A up */
15140 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
15141
b6b5d049
VS
15142 /* 830 needs to leave pipe B & dpll B up */
15143 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
15144
435793df
KP
15145 /* Lenovo U160 cannot use SSC on LVDS */
15146 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
070d329a
MAS
15147
15148 /* Sony Vaio Y cannot use SSC on LVDS */
15149 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
5a15ab5b 15150
be505f64
AH
15151 /* Acer Aspire 5734Z must invert backlight brightness */
15152 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
15153
15154 /* Acer/eMachines G725 */
15155 { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
15156
15157 /* Acer/eMachines e725 */
15158 { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
15159
15160 /* Acer/Packard Bell NCL20 */
15161 { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
15162
15163 /* Acer Aspire 4736Z */
15164 { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
0f540c3a
JN
15165
15166 /* Acer Aspire 5336 */
15167 { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
2e93a1aa
SD
15168
15169 /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
15170 { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
d4967d8c 15171
dfb3d47b
SD
15172 /* Acer C720 Chromebook (Core i3 4005U) */
15173 { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
15174
b2a9601c 15175 /* Apple Macbook 2,1 (Core 2 T7400) */
15176 { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
15177
1b9448b0
JN
15178 /* Apple Macbook 4,1 */
15179 { 0x2a02, 0x106b, 0x00a1, quirk_backlight_present },
15180
d4967d8c
SD
15181 /* Toshiba CB35 Chromebook (Celeron 2955U) */
15182 { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
724cb06f
SD
15183
15184 /* HP Chromebook 14 (Celeron 2955U) */
15185 { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
cf6f0af9
JN
15186
15187 /* Dell Chromebook 11 */
15188 { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
9be64eee
JN
15189
15190 /* Dell Chromebook 11 (2015 version) */
15191 { 0x0a16, 0x1028, 0x0a35, quirk_backlight_present },
b690e96c
JB
15192};
15193
15194static void intel_init_quirks(struct drm_device *dev)
15195{
15196 struct pci_dev *d = dev->pdev;
15197 int i;
15198
15199 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
15200 struct intel_quirk *q = &intel_quirks[i];
15201
15202 if (d->device == q->device &&
15203 (d->subsystem_vendor == q->subsystem_vendor ||
15204 q->subsystem_vendor == PCI_ANY_ID) &&
15205 (d->subsystem_device == q->subsystem_device ||
15206 q->subsystem_device == PCI_ANY_ID))
15207 q->hook(dev);
15208 }
5f85f176
EE
15209 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
15210 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
15211 intel_dmi_quirks[i].hook(dev);
15212 }
b690e96c
JB
15213}
15214
9cce37f4
JB
15215/* Disable the VGA plane that we never use */
15216static void i915_disable_vga(struct drm_device *dev)
15217{
15218 struct drm_i915_private *dev_priv = dev->dev_private;
15219 u8 sr1;
f0f59a00 15220 i915_reg_t vga_reg = i915_vgacntrl_reg(dev);
9cce37f4 15221
2b37c616 15222 /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
9cce37f4 15223 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
3fdcf431 15224 outb(SR01, VGA_SR_INDEX);
9cce37f4
JB
15225 sr1 = inb(VGA_SR_DATA);
15226 outb(sr1 | 1<<5, VGA_SR_DATA);
15227 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
15228 udelay(300);
15229
01f5a626 15230 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
9cce37f4
JB
15231 POSTING_READ(vga_reg);
15232}
15233
f817586c
DV
15234void intel_modeset_init_hw(struct drm_device *dev)
15235{
1a617b77
ML
15236 struct drm_i915_private *dev_priv = dev->dev_private;
15237
b6283055 15238 intel_update_cdclk(dev);
1a617b77
ML
15239
15240 dev_priv->atomic_cdclk_freq = dev_priv->cdclk_freq;
15241
f817586c 15242 intel_init_clock_gating(dev);
8090c6b9 15243 intel_enable_gt_powersave(dev);
f817586c
DV
15244}
15245
d93c0372
MR
15246/*
15247 * Calculate what we think the watermarks should be for the state we've read
15248 * out of the hardware and then immediately program those watermarks so that
15249 * we ensure the hardware settings match our internal state.
15250 *
15251 * We can calculate what we think WM's should be by creating a duplicate of the
15252 * current state (which was constructed during hardware readout) and running it
15253 * through the atomic check code to calculate new watermark values in the
15254 * state object.
15255 */
15256static void sanitize_watermarks(struct drm_device *dev)
15257{
15258 struct drm_i915_private *dev_priv = to_i915(dev);
15259 struct drm_atomic_state *state;
15260 struct drm_crtc *crtc;
15261 struct drm_crtc_state *cstate;
15262 struct drm_modeset_acquire_ctx ctx;
15263 int ret;
15264 int i;
15265
15266 /* Only supported on platforms that use atomic watermark design */
ed4a6a7c 15267 if (!dev_priv->display.optimize_watermarks)
d93c0372
MR
15268 return;
15269
15270 /*
15271 * We need to hold connection_mutex before calling duplicate_state so
15272 * that the connector loop is protected.
15273 */
15274 drm_modeset_acquire_init(&ctx, 0);
15275retry:
0cd1262d 15276 ret = drm_modeset_lock_all_ctx(dev, &ctx);
d93c0372
MR
15277 if (ret == -EDEADLK) {
15278 drm_modeset_backoff(&ctx);
15279 goto retry;
15280 } else if (WARN_ON(ret)) {
0cd1262d 15281 goto fail;
d93c0372
MR
15282 }
15283
15284 state = drm_atomic_helper_duplicate_state(dev, &ctx);
15285 if (WARN_ON(IS_ERR(state)))
0cd1262d 15286 goto fail;
d93c0372 15287
ed4a6a7c
MR
15288 /*
15289 * Hardware readout is the only time we don't want to calculate
15290 * intermediate watermarks (since we don't trust the current
15291 * watermarks).
15292 */
15293 to_intel_atomic_state(state)->skip_intermediate_wm = true;
15294
d93c0372
MR
15295 ret = intel_atomic_check(dev, state);
15296 if (ret) {
15297 /*
15298 * If we fail here, it means that the hardware appears to be
15299 * programmed in a way that shouldn't be possible, given our
15300 * understanding of watermark requirements. This might mean a
15301 * mistake in the hardware readout code or a mistake in the
15302 * watermark calculations for a given platform. Raise a WARN
15303 * so that this is noticeable.
15304 *
15305 * If this actually happens, we'll have to just leave the
15306 * BIOS-programmed watermarks untouched and hope for the best.
15307 */
15308 WARN(true, "Could not determine valid watermarks for inherited state\n");
0cd1262d 15309 goto fail;
d93c0372
MR
15310 }
15311
15312 /* Write calculated watermark values back */
15313 to_i915(dev)->wm.config = to_intel_atomic_state(state)->wm_config;
15314 for_each_crtc_in_state(state, crtc, cstate, i) {
15315 struct intel_crtc_state *cs = to_intel_crtc_state(cstate);
15316
ed4a6a7c
MR
15317 cs->wm.need_postvbl_update = true;
15318 dev_priv->display.optimize_watermarks(cs);
d93c0372
MR
15319 }
15320
15321 drm_atomic_state_free(state);
0cd1262d 15322fail:
d93c0372
MR
15323 drm_modeset_drop_locks(&ctx);
15324 drm_modeset_acquire_fini(&ctx);
15325}
15326
79e53945
JB
15327void intel_modeset_init(struct drm_device *dev)
15328{
652c393a 15329 struct drm_i915_private *dev_priv = dev->dev_private;
1fe47785 15330 int sprite, ret;
8cc87b75 15331 enum pipe pipe;
46f297fb 15332 struct intel_crtc *crtc;
79e53945
JB
15333
15334 drm_mode_config_init(dev);
15335
15336 dev->mode_config.min_width = 0;
15337 dev->mode_config.min_height = 0;
15338
019d96cb
DA
15339 dev->mode_config.preferred_depth = 24;
15340 dev->mode_config.prefer_shadow = 1;
15341
25bab385
TU
15342 dev->mode_config.allow_fb_modifiers = true;
15343
e6ecefaa 15344 dev->mode_config.funcs = &intel_mode_funcs;
79e53945 15345
b690e96c
JB
15346 intel_init_quirks(dev);
15347
1fa61106
ED
15348 intel_init_pm(dev);
15349
e3c74757
BW
15350 if (INTEL_INFO(dev)->num_pipes == 0)
15351 return;
15352
69f92f67
LW
15353 /*
15354 * There may be no VBT; and if the BIOS enabled SSC we can
15355 * just keep using it to avoid unnecessary flicker. Whereas if the
15356 * BIOS isn't using it, don't assume it will work even if the VBT
15357 * indicates as much.
15358 */
15359 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)) {
15360 bool bios_lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
15361 DREF_SSC1_ENABLE);
15362
15363 if (dev_priv->vbt.lvds_use_ssc != bios_lvds_use_ssc) {
15364 DRM_DEBUG_KMS("SSC %sabled by BIOS, overriding VBT which says %sabled\n",
15365 bios_lvds_use_ssc ? "en" : "dis",
15366 dev_priv->vbt.lvds_use_ssc ? "en" : "dis");
15367 dev_priv->vbt.lvds_use_ssc = bios_lvds_use_ssc;
15368 }
15369 }
15370
a6c45cf0
CW
15371 if (IS_GEN2(dev)) {
15372 dev->mode_config.max_width = 2048;
15373 dev->mode_config.max_height = 2048;
15374 } else if (IS_GEN3(dev)) {
5e4d6fa7
KP
15375 dev->mode_config.max_width = 4096;
15376 dev->mode_config.max_height = 4096;
79e53945 15377 } else {
a6c45cf0
CW
15378 dev->mode_config.max_width = 8192;
15379 dev->mode_config.max_height = 8192;
79e53945 15380 }
068be561 15381
dc41c154
VS
15382 if (IS_845G(dev) || IS_I865G(dev)) {
15383 dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
15384 dev->mode_config.cursor_height = 1023;
15385 } else if (IS_GEN2(dev)) {
068be561
DL
15386 dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
15387 dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
15388 } else {
15389 dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
15390 dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
15391 }
15392
62106b4f 15393 dev->mode_config.fb_base = dev_priv->ggtt.mappable_base;
79e53945 15394
28c97730 15395 DRM_DEBUG_KMS("%d display pipe%s available.\n",
7eb552ae
BW
15396 INTEL_INFO(dev)->num_pipes,
15397 INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
79e53945 15398
055e393f 15399 for_each_pipe(dev_priv, pipe) {
8cc87b75 15400 intel_crtc_init(dev, pipe);
3bdcfc0c 15401 for_each_sprite(dev_priv, pipe, sprite) {
1fe47785 15402 ret = intel_plane_init(dev, pipe, sprite);
7f1f3851 15403 if (ret)
06da8da2 15404 DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
1fe47785 15405 pipe_name(pipe), sprite_name(pipe, sprite), ret);
7f1f3851 15406 }
79e53945
JB
15407 }
15408
bfa7df01 15409 intel_update_czclk(dev_priv);
e7dc33f3 15410 intel_update_rawclk(dev_priv);
bfa7df01
VS
15411 intel_update_cdclk(dev);
15412
e72f9fbf 15413 intel_shared_dpll_init(dev);
ee7b9f93 15414
9cce37f4
JB
15415 /* Just disable it once at startup */
15416 i915_disable_vga(dev);
79e53945 15417 intel_setup_outputs(dev);
11be49eb 15418
6e9f798d 15419 drm_modeset_lock_all(dev);
043e9bda 15420 intel_modeset_setup_hw_state(dev);
6e9f798d 15421 drm_modeset_unlock_all(dev);
46f297fb 15422
d3fcc808 15423 for_each_intel_crtc(dev, crtc) {
eeebeac5
ML
15424 struct intel_initial_plane_config plane_config = {};
15425
46f297fb
JB
15426 if (!crtc->active)
15427 continue;
15428
46f297fb 15429 /*
46f297fb
JB
15430 * Note that reserving the BIOS fb up front prevents us
15431 * from stuffing other stolen allocations like the ring
15432 * on top. This prevents some ugliness at boot time, and
15433 * can even allow for smooth boot transitions if the BIOS
15434 * fb is large enough for the active pipe configuration.
15435 */
eeebeac5
ML
15436 dev_priv->display.get_initial_plane_config(crtc,
15437 &plane_config);
15438
15439 /*
15440 * If the fb is shared between multiple heads, we'll
15441 * just get the first one.
15442 */
15443 intel_find_initial_plane_obj(crtc, &plane_config);
46f297fb 15444 }
d93c0372
MR
15445
15446 /*
15447 * Make sure hardware watermarks really match the state we read out.
15448 * Note that we need to do this after reconstructing the BIOS fb's
15449 * since the watermark calculation done here will use pstate->fb.
15450 */
15451 sanitize_watermarks(dev);
2c7111db
CW
15452}
15453
7fad798e
DV
15454static void intel_enable_pipe_a(struct drm_device *dev)
15455{
15456 struct intel_connector *connector;
15457 struct drm_connector *crt = NULL;
15458 struct intel_load_detect_pipe load_detect_temp;
208bf9fd 15459 struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
7fad798e
DV
15460
15461 /* We can't just switch on the pipe A, we need to set things up with a
15462 * proper mode and output configuration. As a gross hack, enable pipe A
15463 * by enabling the load detect pipe once. */
3a3371ff 15464 for_each_intel_connector(dev, connector) {
7fad798e
DV
15465 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
15466 crt = &connector->base;
15467 break;
15468 }
15469 }
15470
15471 if (!crt)
15472 return;
15473
208bf9fd 15474 if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
49172fee 15475 intel_release_load_detect_pipe(crt, &load_detect_temp, ctx);
7fad798e
DV
15476}
15477
fa555837
DV
15478static bool
15479intel_check_plane_mapping(struct intel_crtc *crtc)
15480{
7eb552ae
BW
15481 struct drm_device *dev = crtc->base.dev;
15482 struct drm_i915_private *dev_priv = dev->dev_private;
649636ef 15483 u32 val;
fa555837 15484
7eb552ae 15485 if (INTEL_INFO(dev)->num_pipes == 1)
fa555837
DV
15486 return true;
15487
649636ef 15488 val = I915_READ(DSPCNTR(!crtc->plane));
fa555837
DV
15489
15490 if ((val & DISPLAY_PLANE_ENABLE) &&
15491 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
15492 return false;
15493
15494 return true;
15495}
15496
02e93c35
VS
15497static bool intel_crtc_has_encoders(struct intel_crtc *crtc)
15498{
15499 struct drm_device *dev = crtc->base.dev;
15500 struct intel_encoder *encoder;
15501
15502 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
15503 return true;
15504
15505 return false;
15506}
15507
dd756198
VS
15508static bool intel_encoder_has_connectors(struct intel_encoder *encoder)
15509{
15510 struct drm_device *dev = encoder->base.dev;
15511 struct intel_connector *connector;
15512
15513 for_each_connector_on_encoder(dev, &encoder->base, connector)
15514 return true;
15515
15516 return false;
15517}
15518
24929352
DV
15519static void intel_sanitize_crtc(struct intel_crtc *crtc)
15520{
15521 struct drm_device *dev = crtc->base.dev;
15522 struct drm_i915_private *dev_priv = dev->dev_private;
f0f59a00 15523 i915_reg_t reg = PIPECONF(crtc->config->cpu_transcoder);
24929352 15524
24929352 15525 /* Clear any frame start delays used for debugging left by the BIOS */
24929352
DV
15526 I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
15527
d3eaf884 15528 /* restore vblank interrupts to correct state */
9625604c 15529 drm_crtc_vblank_reset(&crtc->base);
d297e103 15530 if (crtc->active) {
f9cd7b88
VS
15531 struct intel_plane *plane;
15532
9625604c 15533 drm_crtc_vblank_on(&crtc->base);
f9cd7b88
VS
15534
15535 /* Disable everything but the primary plane */
15536 for_each_intel_plane_on_crtc(dev, crtc, plane) {
15537 if (plane->base.type == DRM_PLANE_TYPE_PRIMARY)
15538 continue;
15539
15540 plane->disable_plane(&plane->base, &crtc->base);
15541 }
9625604c 15542 }
d3eaf884 15543
24929352 15544 /* We need to sanitize the plane -> pipe mapping first because this will
fa555837
DV
15545 * disable the crtc (and hence change the state) if it is wrong. Note
15546 * that gen4+ has a fixed plane -> pipe mapping. */
15547 if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
24929352
DV
15548 bool plane;
15549
24929352
DV
15550 DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
15551 crtc->base.base.id);
15552
15553 /* Pipe has the wrong plane attached and the plane is active.
15554 * Temporarily change the plane mapping and disable everything
15555 * ... */
15556 plane = crtc->plane;
b70709a6 15557 to_intel_plane_state(crtc->base.primary->state)->visible = true;
24929352 15558 crtc->plane = !plane;
b17d48e2 15559 intel_crtc_disable_noatomic(&crtc->base);
24929352 15560 crtc->plane = plane;
24929352 15561 }
24929352 15562
7fad798e
DV
15563 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
15564 crtc->pipe == PIPE_A && !crtc->active) {
15565 /* BIOS forgot to enable pipe A, this mostly happens after
15566 * resume. Force-enable the pipe to fix this, the update_dpms
15567 * call below we restore the pipe to the right state, but leave
15568 * the required bits on. */
15569 intel_enable_pipe_a(dev);
15570 }
15571
24929352
DV
15572 /* Adjust the state of the output pipe according to whether we
15573 * have active connectors/encoders. */
842e0307 15574 if (crtc->active && !intel_crtc_has_encoders(crtc))
b17d48e2 15575 intel_crtc_disable_noatomic(&crtc->base);
24929352 15576
a3ed6aad 15577 if (crtc->active || HAS_GMCH_DISPLAY(dev)) {
4cc31489
DV
15578 /*
15579 * We start out with underrun reporting disabled to avoid races.
15580 * For correct bookkeeping mark this on active crtcs.
15581 *
c5ab3bc0
DV
15582 * Also on gmch platforms we dont have any hardware bits to
15583 * disable the underrun reporting. Which means we need to start
15584 * out with underrun reporting disabled also on inactive pipes,
15585 * since otherwise we'll complain about the garbage we read when
15586 * e.g. coming up after runtime pm.
15587 *
4cc31489
DV
15588 * No protection against concurrent access is required - at
15589 * worst a fifo underrun happens which also sets this to false.
15590 */
15591 crtc->cpu_fifo_underrun_disabled = true;
15592 crtc->pch_fifo_underrun_disabled = true;
15593 }
24929352
DV
15594}
15595
15596static void intel_sanitize_encoder(struct intel_encoder *encoder)
15597{
15598 struct intel_connector *connector;
15599 struct drm_device *dev = encoder->base.dev;
15600
15601 /* We need to check both for a crtc link (meaning that the
15602 * encoder is active and trying to read from a pipe) and the
15603 * pipe itself being active. */
15604 bool has_active_crtc = encoder->base.crtc &&
15605 to_intel_crtc(encoder->base.crtc)->active;
15606
dd756198 15607 if (intel_encoder_has_connectors(encoder) && !has_active_crtc) {
24929352
DV
15608 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
15609 encoder->base.base.id,
8e329a03 15610 encoder->base.name);
24929352
DV
15611
15612 /* Connector is active, but has no active pipe. This is
15613 * fallout from our resume register restoring. Disable
15614 * the encoder manually again. */
15615 if (encoder->base.crtc) {
15616 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
15617 encoder->base.base.id,
8e329a03 15618 encoder->base.name);
24929352 15619 encoder->disable(encoder);
a62d1497
VS
15620 if (encoder->post_disable)
15621 encoder->post_disable(encoder);
24929352 15622 }
7f1950fb 15623 encoder->base.crtc = NULL;
24929352
DV
15624
15625 /* Inconsistent output/port/pipe state happens presumably due to
15626 * a bug in one of the get_hw_state functions. Or someplace else
15627 * in our code, like the register restore mess on resume. Clamp
15628 * things to off as a safer default. */
3a3371ff 15629 for_each_intel_connector(dev, connector) {
24929352
DV
15630 if (connector->encoder != encoder)
15631 continue;
7f1950fb
EE
15632 connector->base.dpms = DRM_MODE_DPMS_OFF;
15633 connector->base.encoder = NULL;
24929352
DV
15634 }
15635 }
15636 /* Enabled encoders without active connectors will be fixed in
15637 * the crtc fixup. */
15638}
15639
04098753 15640void i915_redisable_vga_power_on(struct drm_device *dev)
0fde901f
KM
15641{
15642 struct drm_i915_private *dev_priv = dev->dev_private;
f0f59a00 15643 i915_reg_t vga_reg = i915_vgacntrl_reg(dev);
0fde901f 15644
04098753
ID
15645 if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
15646 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
15647 i915_disable_vga(dev);
15648 }
15649}
15650
15651void i915_redisable_vga(struct drm_device *dev)
15652{
15653 struct drm_i915_private *dev_priv = dev->dev_private;
15654
8dc8a27c
PZ
15655 /* This function can be called both from intel_modeset_setup_hw_state or
15656 * at a very early point in our resume sequence, where the power well
15657 * structures are not yet restored. Since this function is at a very
15658 * paranoid "someone might have enabled VGA while we were not looking"
15659 * level, just check if the power well is enabled instead of trying to
15660 * follow the "don't touch the power well if we don't need it" policy
15661 * the rest of the driver uses. */
6392f847 15662 if (!intel_display_power_get_if_enabled(dev_priv, POWER_DOMAIN_VGA))
8dc8a27c
PZ
15663 return;
15664
04098753 15665 i915_redisable_vga_power_on(dev);
6392f847
ID
15666
15667 intel_display_power_put(dev_priv, POWER_DOMAIN_VGA);
0fde901f
KM
15668}
15669
f9cd7b88 15670static bool primary_get_hw_state(struct intel_plane *plane)
98ec7739 15671{
f9cd7b88 15672 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
98ec7739 15673
f9cd7b88 15674 return I915_READ(DSPCNTR(plane->plane)) & DISPLAY_PLANE_ENABLE;
d032ffa0
ML
15675}
15676
f9cd7b88
VS
15677/* FIXME read out full plane state for all planes */
15678static void readout_plane_state(struct intel_crtc *crtc)
d032ffa0 15679{
b26d3ea3 15680 struct drm_plane *primary = crtc->base.primary;
f9cd7b88 15681 struct intel_plane_state *plane_state =
b26d3ea3 15682 to_intel_plane_state(primary->state);
d032ffa0 15683
19b8d387 15684 plane_state->visible = crtc->active &&
b26d3ea3
ML
15685 primary_get_hw_state(to_intel_plane(primary));
15686
15687 if (plane_state->visible)
15688 crtc->base.state->plane_mask |= 1 << drm_plane_index(primary);
98ec7739
VS
15689}
15690
30e984df 15691static void intel_modeset_readout_hw_state(struct drm_device *dev)
24929352
DV
15692{
15693 struct drm_i915_private *dev_priv = dev->dev_private;
15694 enum pipe pipe;
24929352
DV
15695 struct intel_crtc *crtc;
15696 struct intel_encoder *encoder;
15697 struct intel_connector *connector;
5358901f 15698 int i;
24929352 15699
565602d7
ML
15700 dev_priv->active_crtcs = 0;
15701
d3fcc808 15702 for_each_intel_crtc(dev, crtc) {
565602d7
ML
15703 struct intel_crtc_state *crtc_state = crtc->config;
15704 int pixclk = 0;
3b117c8f 15705
565602d7
ML
15706 __drm_atomic_helper_crtc_destroy_state(&crtc->base, &crtc_state->base);
15707 memset(crtc_state, 0, sizeof(*crtc_state));
15708 crtc_state->base.crtc = &crtc->base;
24929352 15709
565602d7
ML
15710 crtc_state->base.active = crtc_state->base.enable =
15711 dev_priv->display.get_pipe_config(crtc, crtc_state);
15712
15713 crtc->base.enabled = crtc_state->base.enable;
15714 crtc->active = crtc_state->base.active;
15715
15716 if (crtc_state->base.active) {
15717 dev_priv->active_crtcs |= 1 << crtc->pipe;
15718
15719 if (IS_BROADWELL(dev_priv)) {
15720 pixclk = ilk_pipe_pixel_rate(crtc_state);
15721
15722 /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
15723 if (crtc_state->ips_enabled)
15724 pixclk = DIV_ROUND_UP(pixclk * 100, 95);
15725 } else if (IS_VALLEYVIEW(dev_priv) ||
15726 IS_CHERRYVIEW(dev_priv) ||
15727 IS_BROXTON(dev_priv))
15728 pixclk = crtc_state->base.adjusted_mode.crtc_clock;
15729 else
15730 WARN_ON(dev_priv->display.modeset_calc_cdclk);
15731 }
15732
15733 dev_priv->min_pixclk[crtc->pipe] = pixclk;
b70709a6 15734
f9cd7b88 15735 readout_plane_state(crtc);
24929352
DV
15736
15737 DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
15738 crtc->base.base.id,
15739 crtc->active ? "enabled" : "disabled");
15740 }
15741
5358901f
DV
15742 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
15743 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
15744
2edd6443
ACO
15745 pll->on = pll->funcs.get_hw_state(dev_priv, pll,
15746 &pll->config.hw_state);
3e369b76 15747 pll->config.crtc_mask = 0;
d3fcc808 15748 for_each_intel_crtc(dev, crtc) {
2dd66ebd 15749 if (crtc->active && crtc->config->shared_dpll == pll)
3e369b76 15750 pll->config.crtc_mask |= 1 << crtc->pipe;
5358901f 15751 }
2dd66ebd 15752 pll->active_mask = pll->config.crtc_mask;
5358901f 15753
1e6f2ddc 15754 DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
3e369b76 15755 pll->name, pll->config.crtc_mask, pll->on);
5358901f
DV
15756 }
15757
b2784e15 15758 for_each_intel_encoder(dev, encoder) {
24929352
DV
15759 pipe = 0;
15760
15761 if (encoder->get_hw_state(encoder, &pipe)) {
045ac3b5
JB
15762 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
15763 encoder->base.crtc = &crtc->base;
6e3c9717 15764 encoder->get_config(encoder, crtc->config);
24929352
DV
15765 } else {
15766 encoder->base.crtc = NULL;
15767 }
15768
6f2bcceb 15769 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
24929352 15770 encoder->base.base.id,
8e329a03 15771 encoder->base.name,
24929352 15772 encoder->base.crtc ? "enabled" : "disabled",
6f2bcceb 15773 pipe_name(pipe));
24929352
DV
15774 }
15775
3a3371ff 15776 for_each_intel_connector(dev, connector) {
24929352
DV
15777 if (connector->get_hw_state(connector)) {
15778 connector->base.dpms = DRM_MODE_DPMS_ON;
2aa974c9
ML
15779
15780 encoder = connector->encoder;
15781 connector->base.encoder = &encoder->base;
15782
15783 if (encoder->base.crtc &&
15784 encoder->base.crtc->state->active) {
15785 /*
15786 * This has to be done during hardware readout
15787 * because anything calling .crtc_disable may
15788 * rely on the connector_mask being accurate.
15789 */
15790 encoder->base.crtc->state->connector_mask |=
15791 1 << drm_connector_index(&connector->base);
e87a52b3
ML
15792 encoder->base.crtc->state->encoder_mask |=
15793 1 << drm_encoder_index(&encoder->base);
2aa974c9
ML
15794 }
15795
24929352
DV
15796 } else {
15797 connector->base.dpms = DRM_MODE_DPMS_OFF;
15798 connector->base.encoder = NULL;
15799 }
15800 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
15801 connector->base.base.id,
c23cc417 15802 connector->base.name,
24929352
DV
15803 connector->base.encoder ? "enabled" : "disabled");
15804 }
7f4c6284
VS
15805
15806 for_each_intel_crtc(dev, crtc) {
15807 crtc->base.hwmode = crtc->config->base.adjusted_mode;
15808
15809 memset(&crtc->base.mode, 0, sizeof(crtc->base.mode));
15810 if (crtc->base.state->active) {
15811 intel_mode_from_pipe_config(&crtc->base.mode, crtc->config);
15812 intel_mode_from_pipe_config(&crtc->base.state->adjusted_mode, crtc->config);
15813 WARN_ON(drm_atomic_set_mode_for_crtc(crtc->base.state, &crtc->base.mode));
15814
15815 /*
15816 * The initial mode needs to be set in order to keep
15817 * the atomic core happy. It wants a valid mode if the
15818 * crtc's enabled, so we do the above call.
15819 *
15820 * At this point some state updated by the connectors
15821 * in their ->detect() callback has not run yet, so
15822 * no recalculation can be done yet.
15823 *
15824 * Even if we could do a recalculation and modeset
15825 * right now it would cause a double modeset if
15826 * fbdev or userspace chooses a different initial mode.
15827 *
15828 * If that happens, someone indicated they wanted a
15829 * mode change, which means it's safe to do a full
15830 * recalculation.
15831 */
15832 crtc->base.state->mode.private_flags = I915_MODE_FLAG_INHERITED;
9eca6832
VS
15833
15834 drm_calc_timestamping_constants(&crtc->base, &crtc->base.hwmode);
15835 update_scanline_offset(crtc);
7f4c6284 15836 }
e3b247da
VS
15837
15838 intel_pipe_config_sanity_check(dev_priv, crtc->config);
7f4c6284 15839 }
30e984df
DV
15840}
15841
043e9bda
ML
15842/* Scan out the current hw modeset state,
15843 * and sanitizes it to the current state
15844 */
15845static void
15846intel_modeset_setup_hw_state(struct drm_device *dev)
30e984df
DV
15847{
15848 struct drm_i915_private *dev_priv = dev->dev_private;
15849 enum pipe pipe;
30e984df
DV
15850 struct intel_crtc *crtc;
15851 struct intel_encoder *encoder;
35c95375 15852 int i;
30e984df
DV
15853
15854 intel_modeset_readout_hw_state(dev);
24929352
DV
15855
15856 /* HW state is read out, now we need to sanitize this mess. */
b2784e15 15857 for_each_intel_encoder(dev, encoder) {
24929352
DV
15858 intel_sanitize_encoder(encoder);
15859 }
15860
055e393f 15861 for_each_pipe(dev_priv, pipe) {
24929352
DV
15862 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
15863 intel_sanitize_crtc(crtc);
6e3c9717
ACO
15864 intel_dump_pipe_config(crtc, crtc->config,
15865 "[setup_hw_state]");
24929352 15866 }
9a935856 15867
d29b2f9d
ACO
15868 intel_modeset_update_connector_atomic_state(dev);
15869
35c95375
DV
15870 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
15871 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
15872
2dd66ebd 15873 if (!pll->on || pll->active_mask)
35c95375
DV
15874 continue;
15875
15876 DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
15877
2edd6443 15878 pll->funcs.disable(dev_priv, pll);
35c95375
DV
15879 pll->on = false;
15880 }
15881
666a4537 15882 if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev))
6eb1a681
VS
15883 vlv_wm_get_hw_state(dev);
15884 else if (IS_GEN9(dev))
3078999f
PB
15885 skl_wm_get_hw_state(dev);
15886 else if (HAS_PCH_SPLIT(dev))
243e6a44 15887 ilk_wm_get_hw_state(dev);
292b990e
ML
15888
15889 for_each_intel_crtc(dev, crtc) {
15890 unsigned long put_domains;
15891
74bff5f9 15892 put_domains = modeset_get_crtc_power_domains(&crtc->base, crtc->config);
292b990e
ML
15893 if (WARN_ON(put_domains))
15894 modeset_put_power_domains(dev_priv, put_domains);
15895 }
15896 intel_display_set_init_power(dev_priv, false);
010cf73d
PZ
15897
15898 intel_fbc_init_pipe_state(dev_priv);
043e9bda 15899}
7d0bc1ea 15900
043e9bda
ML
15901void intel_display_resume(struct drm_device *dev)
15902{
e2c8b870
ML
15903 struct drm_i915_private *dev_priv = to_i915(dev);
15904 struct drm_atomic_state *state = dev_priv->modeset_restore_state;
15905 struct drm_modeset_acquire_ctx ctx;
043e9bda 15906 int ret;
e2c8b870 15907 bool setup = false;
f30da187 15908
e2c8b870 15909 dev_priv->modeset_restore_state = NULL;
043e9bda 15910
ea49c9ac
ML
15911 /*
15912 * This is a cludge because with real atomic modeset mode_config.mutex
15913 * won't be taken. Unfortunately some probed state like
15914 * audio_codec_enable is still protected by mode_config.mutex, so lock
15915 * it here for now.
15916 */
15917 mutex_lock(&dev->mode_config.mutex);
e2c8b870 15918 drm_modeset_acquire_init(&ctx, 0);
043e9bda 15919
e2c8b870
ML
15920retry:
15921 ret = drm_modeset_lock_all_ctx(dev, &ctx);
043e9bda 15922
e2c8b870
ML
15923 if (ret == 0 && !setup) {
15924 setup = true;
043e9bda 15925
e2c8b870
ML
15926 intel_modeset_setup_hw_state(dev);
15927 i915_redisable_vga(dev);
45e2b5f6 15928 }
8af6cf88 15929
e2c8b870
ML
15930 if (ret == 0 && state) {
15931 struct drm_crtc_state *crtc_state;
15932 struct drm_crtc *crtc;
15933 int i;
043e9bda 15934
e2c8b870
ML
15935 state->acquire_ctx = &ctx;
15936
15937 for_each_crtc_in_state(state, crtc, crtc_state, i) {
15938 /*
15939 * Force recalculation even if we restore
15940 * current state. With fast modeset this may not result
15941 * in a modeset when the state is compatible.
15942 */
15943 crtc_state->mode_changed = true;
15944 }
15945
15946 ret = drm_atomic_commit(state);
043e9bda
ML
15947 }
15948
e2c8b870
ML
15949 if (ret == -EDEADLK) {
15950 drm_modeset_backoff(&ctx);
15951 goto retry;
15952 }
043e9bda 15953
e2c8b870
ML
15954 drm_modeset_drop_locks(&ctx);
15955 drm_modeset_acquire_fini(&ctx);
ea49c9ac 15956 mutex_unlock(&dev->mode_config.mutex);
043e9bda 15957
e2c8b870
ML
15958 if (ret) {
15959 DRM_ERROR("Restoring old state failed with %i\n", ret);
15960 drm_atomic_state_free(state);
15961 }
2c7111db
CW
15962}
15963
15964void intel_modeset_gem_init(struct drm_device *dev)
15965{
484b41dd 15966 struct drm_crtc *c;
2ff8fde1 15967 struct drm_i915_gem_object *obj;
e0d6149b 15968 int ret;
484b41dd 15969
ae48434c 15970 intel_init_gt_powersave(dev);
ae48434c 15971
1833b134 15972 intel_modeset_init_hw(dev);
02e792fb
DV
15973
15974 intel_setup_overlay(dev);
484b41dd
JB
15975
15976 /*
15977 * Make sure any fbs we allocated at startup are properly
15978 * pinned & fenced. When we do the allocation it's too early
15979 * for this.
15980 */
70e1e0ec 15981 for_each_crtc(dev, c) {
2ff8fde1
MR
15982 obj = intel_fb_obj(c->primary->fb);
15983 if (obj == NULL)
484b41dd
JB
15984 continue;
15985
e0d6149b 15986 mutex_lock(&dev->struct_mutex);
3465c580
VS
15987 ret = intel_pin_and_fence_fb_obj(c->primary->fb,
15988 c->primary->state->rotation);
e0d6149b
TU
15989 mutex_unlock(&dev->struct_mutex);
15990 if (ret) {
484b41dd
JB
15991 DRM_ERROR("failed to pin boot fb on pipe %d\n",
15992 to_intel_crtc(c)->pipe);
66e514c1
DA
15993 drm_framebuffer_unreference(c->primary->fb);
15994 c->primary->fb = NULL;
36750f28 15995 c->primary->crtc = c->primary->state->crtc = NULL;
afd65eb4 15996 update_state_fb(c->primary);
36750f28 15997 c->state->plane_mask &= ~(1 << drm_plane_index(c->primary));
484b41dd
JB
15998 }
15999 }
0962c3c9
VS
16000
16001 intel_backlight_register(dev);
79e53945
JB
16002}
16003
4932e2c3
ID
16004void intel_connector_unregister(struct intel_connector *intel_connector)
16005{
16006 struct drm_connector *connector = &intel_connector->base;
16007
16008 intel_panel_destroy_backlight(connector);
34ea3d38 16009 drm_connector_unregister(connector);
4932e2c3
ID
16010}
16011
79e53945
JB
16012void intel_modeset_cleanup(struct drm_device *dev)
16013{
652c393a 16014 struct drm_i915_private *dev_priv = dev->dev_private;
19c8054c 16015 struct intel_connector *connector;
652c393a 16016
2eb5252e
ID
16017 intel_disable_gt_powersave(dev);
16018
0962c3c9
VS
16019 intel_backlight_unregister(dev);
16020
fd0c0642
DV
16021 /*
16022 * Interrupts and polling as the first thing to avoid creating havoc.
2eb5252e 16023 * Too much stuff here (turning of connectors, ...) would
fd0c0642
DV
16024 * experience fancy races otherwise.
16025 */
2aeb7d3a 16026 intel_irq_uninstall(dev_priv);
eb21b92b 16027
fd0c0642
DV
16028 /*
16029 * Due to the hpd irq storm handling the hotplug work can re-arm the
16030 * poll handlers. Hence disable polling after hpd handling is shut down.
16031 */
f87ea761 16032 drm_kms_helper_poll_fini(dev);
fd0c0642 16033
723bfd70
JB
16034 intel_unregister_dsm_handler();
16035
c937ab3e 16036 intel_fbc_global_disable(dev_priv);
69341a5e 16037
1630fe75
CW
16038 /* flush any delayed tasks or pending work */
16039 flush_scheduled_work();
16040
db31af1d 16041 /* destroy the backlight and sysfs files before encoders/connectors */
19c8054c
JN
16042 for_each_intel_connector(dev, connector)
16043 connector->unregister(connector);
d9255d57 16044
79e53945 16045 drm_mode_config_cleanup(dev);
4d7bb011
DV
16046
16047 intel_cleanup_overlay(dev);
ae48434c 16048
ae48434c 16049 intel_cleanup_gt_powersave(dev);
f5949141
DV
16050
16051 intel_teardown_gmbus(dev);
79e53945
JB
16052}
16053
f1c79df3
ZW
16054/*
16055 * Return which encoder is currently attached for connector.
16056 */
df0e9248 16057struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
79e53945 16058{
df0e9248
CW
16059 return &intel_attached_encoder(connector)->base;
16060}
f1c79df3 16061
df0e9248
CW
16062void intel_connector_attach_encoder(struct intel_connector *connector,
16063 struct intel_encoder *encoder)
16064{
16065 connector->encoder = encoder;
16066 drm_mode_connector_attach_encoder(&connector->base,
16067 &encoder->base);
79e53945 16068}
28d52043
DA
16069
16070/*
16071 * set vga decode state - true == enable VGA decode
16072 */
16073int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
16074{
16075 struct drm_i915_private *dev_priv = dev->dev_private;
a885b3cc 16076 unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
28d52043
DA
16077 u16 gmch_ctrl;
16078
75fa041d
CW
16079 if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
16080 DRM_ERROR("failed to read control word\n");
16081 return -EIO;
16082 }
16083
c0cc8a55
CW
16084 if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
16085 return 0;
16086
28d52043
DA
16087 if (state)
16088 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
16089 else
16090 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
75fa041d
CW
16091
16092 if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
16093 DRM_ERROR("failed to write control word\n");
16094 return -EIO;
16095 }
16096
28d52043
DA
16097 return 0;
16098}
c4a1d9e4 16099
c4a1d9e4 16100struct intel_display_error_state {
ff57f1b0
PZ
16101
16102 u32 power_well_driver;
16103
63b66e5b
CW
16104 int num_transcoders;
16105
c4a1d9e4
CW
16106 struct intel_cursor_error_state {
16107 u32 control;
16108 u32 position;
16109 u32 base;
16110 u32 size;
52331309 16111 } cursor[I915_MAX_PIPES];
c4a1d9e4
CW
16112
16113 struct intel_pipe_error_state {
ddf9c536 16114 bool power_domain_on;
c4a1d9e4 16115 u32 source;
f301b1e1 16116 u32 stat;
52331309 16117 } pipe[I915_MAX_PIPES];
c4a1d9e4
CW
16118
16119 struct intel_plane_error_state {
16120 u32 control;
16121 u32 stride;
16122 u32 size;
16123 u32 pos;
16124 u32 addr;
16125 u32 surface;
16126 u32 tile_offset;
52331309 16127 } plane[I915_MAX_PIPES];
63b66e5b
CW
16128
16129 struct intel_transcoder_error_state {
ddf9c536 16130 bool power_domain_on;
63b66e5b
CW
16131 enum transcoder cpu_transcoder;
16132
16133 u32 conf;
16134
16135 u32 htotal;
16136 u32 hblank;
16137 u32 hsync;
16138 u32 vtotal;
16139 u32 vblank;
16140 u32 vsync;
16141 } transcoder[4];
c4a1d9e4
CW
16142};
16143
16144struct intel_display_error_state *
16145intel_display_capture_error_state(struct drm_device *dev)
16146{
fbee40df 16147 struct drm_i915_private *dev_priv = dev->dev_private;
c4a1d9e4 16148 struct intel_display_error_state *error;
63b66e5b
CW
16149 int transcoders[] = {
16150 TRANSCODER_A,
16151 TRANSCODER_B,
16152 TRANSCODER_C,
16153 TRANSCODER_EDP,
16154 };
c4a1d9e4
CW
16155 int i;
16156
63b66e5b
CW
16157 if (INTEL_INFO(dev)->num_pipes == 0)
16158 return NULL;
16159
9d1cb914 16160 error = kzalloc(sizeof(*error), GFP_ATOMIC);
c4a1d9e4
CW
16161 if (error == NULL)
16162 return NULL;
16163
190be112 16164 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
ff57f1b0
PZ
16165 error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
16166
055e393f 16167 for_each_pipe(dev_priv, i) {
ddf9c536 16168 error->pipe[i].power_domain_on =
f458ebbc
DV
16169 __intel_display_power_is_enabled(dev_priv,
16170 POWER_DOMAIN_PIPE(i));
ddf9c536 16171 if (!error->pipe[i].power_domain_on)
9d1cb914
PZ
16172 continue;
16173
5efb3e28
VS
16174 error->cursor[i].control = I915_READ(CURCNTR(i));
16175 error->cursor[i].position = I915_READ(CURPOS(i));
16176 error->cursor[i].base = I915_READ(CURBASE(i));
c4a1d9e4
CW
16177
16178 error->plane[i].control = I915_READ(DSPCNTR(i));
16179 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
80ca378b 16180 if (INTEL_INFO(dev)->gen <= 3) {
51889b35 16181 error->plane[i].size = I915_READ(DSPSIZE(i));
80ca378b
PZ
16182 error->plane[i].pos = I915_READ(DSPPOS(i));
16183 }
ca291363
PZ
16184 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
16185 error->plane[i].addr = I915_READ(DSPADDR(i));
c4a1d9e4
CW
16186 if (INTEL_INFO(dev)->gen >= 4) {
16187 error->plane[i].surface = I915_READ(DSPSURF(i));
16188 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
16189 }
16190
c4a1d9e4 16191 error->pipe[i].source = I915_READ(PIPESRC(i));
f301b1e1 16192
3abfce77 16193 if (HAS_GMCH_DISPLAY(dev))
f301b1e1 16194 error->pipe[i].stat = I915_READ(PIPESTAT(i));
63b66e5b
CW
16195 }
16196
16197 error->num_transcoders = INTEL_INFO(dev)->num_pipes;
16198 if (HAS_DDI(dev_priv->dev))
16199 error->num_transcoders++; /* Account for eDP. */
16200
16201 for (i = 0; i < error->num_transcoders; i++) {
16202 enum transcoder cpu_transcoder = transcoders[i];
16203
ddf9c536 16204 error->transcoder[i].power_domain_on =
f458ebbc 16205 __intel_display_power_is_enabled(dev_priv,
38cc1daf 16206 POWER_DOMAIN_TRANSCODER(cpu_transcoder));
ddf9c536 16207 if (!error->transcoder[i].power_domain_on)
9d1cb914
PZ
16208 continue;
16209
63b66e5b
CW
16210 error->transcoder[i].cpu_transcoder = cpu_transcoder;
16211
16212 error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
16213 error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
16214 error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
16215 error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
16216 error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
16217 error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
16218 error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
c4a1d9e4
CW
16219 }
16220
16221 return error;
16222}
16223
edc3d884
MK
16224#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
16225
c4a1d9e4 16226void
edc3d884 16227intel_display_print_error_state(struct drm_i915_error_state_buf *m,
c4a1d9e4
CW
16228 struct drm_device *dev,
16229 struct intel_display_error_state *error)
16230{
055e393f 16231 struct drm_i915_private *dev_priv = dev->dev_private;
c4a1d9e4
CW
16232 int i;
16233
63b66e5b
CW
16234 if (!error)
16235 return;
16236
edc3d884 16237 err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
190be112 16238 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
edc3d884 16239 err_printf(m, "PWR_WELL_CTL2: %08x\n",
ff57f1b0 16240 error->power_well_driver);
055e393f 16241 for_each_pipe(dev_priv, i) {
edc3d884 16242 err_printf(m, "Pipe [%d]:\n", i);
ddf9c536 16243 err_printf(m, " Power: %s\n",
87ad3212 16244 onoff(error->pipe[i].power_domain_on));
edc3d884 16245 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
f301b1e1 16246 err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
edc3d884
MK
16247
16248 err_printf(m, "Plane [%d]:\n", i);
16249 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
16250 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
80ca378b 16251 if (INTEL_INFO(dev)->gen <= 3) {
edc3d884
MK
16252 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
16253 err_printf(m, " POS: %08x\n", error->plane[i].pos);
80ca378b 16254 }
4b71a570 16255 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
edc3d884 16256 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
c4a1d9e4 16257 if (INTEL_INFO(dev)->gen >= 4) {
edc3d884
MK
16258 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
16259 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
c4a1d9e4
CW
16260 }
16261
edc3d884
MK
16262 err_printf(m, "Cursor [%d]:\n", i);
16263 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
16264 err_printf(m, " POS: %08x\n", error->cursor[i].position);
16265 err_printf(m, " BASE: %08x\n", error->cursor[i].base);
c4a1d9e4 16266 }
63b66e5b
CW
16267
16268 for (i = 0; i < error->num_transcoders; i++) {
da205630 16269 err_printf(m, "CPU transcoder: %s\n",
63b66e5b 16270 transcoder_name(error->transcoder[i].cpu_transcoder));
ddf9c536 16271 err_printf(m, " Power: %s\n",
87ad3212 16272 onoff(error->transcoder[i].power_domain_on));
63b66e5b
CW
16273 err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
16274 err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
16275 err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
16276 err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
16277 err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
16278 err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
16279 err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
16280 }
c4a1d9e4 16281}
This page took 3.960055 seconds and 5 git commands to generate.