drm/i915: Hide the atomic_read(reset_counter) behind a helper
[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"
db18b6a6 39#include "intel_dsi.h"
e5510fac 40#include "i915_trace.h"
319c1d42 41#include <drm/drm_atomic.h>
c196e1d6 42#include <drm/drm_atomic_helper.h>
760285e7
DH
43#include <drm/drm_dp_helper.h>
44#include <drm/drm_crtc_helper.h>
465c120c
MR
45#include <drm/drm_plane_helper.h>
46#include <drm/drm_rect.h>
c0f372b3 47#include <linux/dma_remapping.h>
fd8e058a
AG
48#include <linux/reservation.h>
49#include <linux/dma-buf.h>
79e53945 50
465c120c 51/* Primary plane formats for gen <= 3 */
568db4f2 52static const uint32_t i8xx_primary_formats[] = {
67fe7dc5
DL
53 DRM_FORMAT_C8,
54 DRM_FORMAT_RGB565,
465c120c 55 DRM_FORMAT_XRGB1555,
67fe7dc5 56 DRM_FORMAT_XRGB8888,
465c120c
MR
57};
58
59/* Primary plane formats for gen >= 4 */
568db4f2 60static const uint32_t i965_primary_formats[] = {
6c0fd451
DL
61 DRM_FORMAT_C8,
62 DRM_FORMAT_RGB565,
63 DRM_FORMAT_XRGB8888,
64 DRM_FORMAT_XBGR8888,
65 DRM_FORMAT_XRGB2101010,
66 DRM_FORMAT_XBGR2101010,
67};
68
69static const uint32_t skl_primary_formats[] = {
67fe7dc5
DL
70 DRM_FORMAT_C8,
71 DRM_FORMAT_RGB565,
72 DRM_FORMAT_XRGB8888,
465c120c 73 DRM_FORMAT_XBGR8888,
67fe7dc5 74 DRM_FORMAT_ARGB8888,
465c120c
MR
75 DRM_FORMAT_ABGR8888,
76 DRM_FORMAT_XRGB2101010,
465c120c 77 DRM_FORMAT_XBGR2101010,
ea916ea0
KM
78 DRM_FORMAT_YUYV,
79 DRM_FORMAT_YVYU,
80 DRM_FORMAT_UYVY,
81 DRM_FORMAT_VYUY,
465c120c
MR
82};
83
3d7d6510
MR
84/* Cursor formats */
85static const uint32_t intel_cursor_formats[] = {
86 DRM_FORMAT_ARGB8888,
87};
88
f1f644dc 89static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 90 struct intel_crtc_state *pipe_config);
18442d08 91static void ironlake_pch_clock_get(struct intel_crtc *crtc,
5cec258b 92 struct intel_crtc_state *pipe_config);
f1f644dc 93
eb1bfe80
JB
94static int intel_framebuffer_init(struct drm_device *dev,
95 struct intel_framebuffer *ifb,
96 struct drm_mode_fb_cmd2 *mode_cmd,
97 struct drm_i915_gem_object *obj);
5b18e57c
DV
98static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc);
99static void intel_set_pipe_timings(struct intel_crtc *intel_crtc);
bc58be60 100static void intel_set_pipe_src_size(struct intel_crtc *intel_crtc);
29407aab 101static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
f769cd24
VK
102 struct intel_link_m_n *m_n,
103 struct intel_link_m_n *m2_n2);
29407aab 104static void ironlake_set_pipeconf(struct drm_crtc *crtc);
229fca97 105static void haswell_set_pipeconf(struct drm_crtc *crtc);
391bf048 106static void haswell_set_pipemisc(struct drm_crtc *crtc);
d288f65f 107static void vlv_prepare_pll(struct intel_crtc *crtc,
5cec258b 108 const struct intel_crtc_state *pipe_config);
d288f65f 109static void chv_prepare_pll(struct intel_crtc *crtc,
5cec258b 110 const struct intel_crtc_state *pipe_config);
613d2b27
ML
111static void intel_begin_crtc_commit(struct drm_crtc *, struct drm_crtc_state *);
112static void intel_finish_crtc_commit(struct drm_crtc *, struct drm_crtc_state *);
549e2bfb
CK
113static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
114 struct intel_crtc_state *crtc_state);
bfd16b2a
ML
115static void skylake_pfit_enable(struct intel_crtc *crtc);
116static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force);
117static void ironlake_pfit_enable(struct intel_crtc *crtc);
043e9bda 118static void intel_modeset_setup_hw_state(struct drm_device *dev);
2622a081 119static void intel_pre_disable_primary_noatomic(struct drm_crtc *crtc);
e7457a9a 120
79e53945 121typedef struct {
0206e353 122 int min, max;
79e53945
JB
123} intel_range_t;
124
125typedef struct {
0206e353
AJ
126 int dot_limit;
127 int p2_slow, p2_fast;
79e53945
JB
128} intel_p2_t;
129
d4906093
ML
130typedef struct intel_limit intel_limit_t;
131struct intel_limit {
0206e353
AJ
132 intel_range_t dot, vco, n, m, m1, m2, p, p1;
133 intel_p2_t p2;
d4906093 134};
79e53945 135
bfa7df01
VS
136/* returns HPLL frequency in kHz */
137static int valleyview_get_vco(struct drm_i915_private *dev_priv)
138{
139 int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
140
141 /* Obtain SKU information */
142 mutex_lock(&dev_priv->sb_lock);
143 hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
144 CCK_FUSE_HPLL_FREQ_MASK;
145 mutex_unlock(&dev_priv->sb_lock);
146
147 return vco_freq[hpll_freq] * 1000;
148}
149
c30fec65
VS
150int vlv_get_cck_clock(struct drm_i915_private *dev_priv,
151 const char *name, u32 reg, int ref_freq)
bfa7df01
VS
152{
153 u32 val;
154 int divider;
155
bfa7df01
VS
156 mutex_lock(&dev_priv->sb_lock);
157 val = vlv_cck_read(dev_priv, reg);
158 mutex_unlock(&dev_priv->sb_lock);
159
160 divider = val & CCK_FREQUENCY_VALUES;
161
162 WARN((val & CCK_FREQUENCY_STATUS) !=
163 (divider << CCK_FREQUENCY_STATUS_SHIFT),
164 "%s change in progress\n", name);
165
c30fec65
VS
166 return DIV_ROUND_CLOSEST(ref_freq << 1, divider + 1);
167}
168
169static int vlv_get_cck_clock_hpll(struct drm_i915_private *dev_priv,
170 const char *name, u32 reg)
171{
172 if (dev_priv->hpll_freq == 0)
173 dev_priv->hpll_freq = valleyview_get_vco(dev_priv);
174
175 return vlv_get_cck_clock(dev_priv, name, reg,
176 dev_priv->hpll_freq);
bfa7df01
VS
177}
178
e7dc33f3
VS
179static int
180intel_pch_rawclk(struct drm_i915_private *dev_priv)
d2acd215 181{
e7dc33f3
VS
182 return (I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK) * 1000;
183}
d2acd215 184
e7dc33f3
VS
185static int
186intel_vlv_hrawclk(struct drm_i915_private *dev_priv)
187{
35d38d1f
VS
188 return vlv_get_cck_clock_hpll(dev_priv, "hrawclk",
189 CCK_DISPLAY_REF_CLOCK_CONTROL);
d2acd215
DV
190}
191
e7dc33f3
VS
192static int
193intel_g4x_hrawclk(struct drm_i915_private *dev_priv)
79e50a4f 194{
79e50a4f
JN
195 uint32_t clkcfg;
196
e7dc33f3 197 /* hrawclock is 1/4 the FSB frequency */
79e50a4f
JN
198 clkcfg = I915_READ(CLKCFG);
199 switch (clkcfg & CLKCFG_FSB_MASK) {
200 case CLKCFG_FSB_400:
e7dc33f3 201 return 100000;
79e50a4f 202 case CLKCFG_FSB_533:
e7dc33f3 203 return 133333;
79e50a4f 204 case CLKCFG_FSB_667:
e7dc33f3 205 return 166667;
79e50a4f 206 case CLKCFG_FSB_800:
e7dc33f3 207 return 200000;
79e50a4f 208 case CLKCFG_FSB_1067:
e7dc33f3 209 return 266667;
79e50a4f 210 case CLKCFG_FSB_1333:
e7dc33f3 211 return 333333;
79e50a4f
JN
212 /* these two are just a guess; one of them might be right */
213 case CLKCFG_FSB_1600:
214 case CLKCFG_FSB_1600_ALT:
e7dc33f3 215 return 400000;
79e50a4f 216 default:
e7dc33f3 217 return 133333;
79e50a4f
JN
218 }
219}
220
e7dc33f3
VS
221static void intel_update_rawclk(struct drm_i915_private *dev_priv)
222{
223 if (HAS_PCH_SPLIT(dev_priv))
224 dev_priv->rawclk_freq = intel_pch_rawclk(dev_priv);
225 else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
226 dev_priv->rawclk_freq = intel_vlv_hrawclk(dev_priv);
227 else if (IS_G4X(dev_priv) || IS_PINEVIEW(dev_priv))
228 dev_priv->rawclk_freq = intel_g4x_hrawclk(dev_priv);
229 else
230 return; /* no rawclk on other platforms, or no need to know it */
231
232 DRM_DEBUG_DRIVER("rawclk rate: %d kHz\n", dev_priv->rawclk_freq);
233}
234
bfa7df01
VS
235static void intel_update_czclk(struct drm_i915_private *dev_priv)
236{
666a4537 237 if (!(IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)))
bfa7df01
VS
238 return;
239
240 dev_priv->czclk_freq = vlv_get_cck_clock_hpll(dev_priv, "czclk",
241 CCK_CZ_CLOCK_CONTROL);
242
243 DRM_DEBUG_DRIVER("CZ clock rate: %d kHz\n", dev_priv->czclk_freq);
244}
245
021357ac 246static inline u32 /* units of 100MHz */
21a727b3
VS
247intel_fdi_link_freq(struct drm_i915_private *dev_priv,
248 const struct intel_crtc_state *pipe_config)
021357ac 249{
21a727b3
VS
250 if (HAS_DDI(dev_priv))
251 return pipe_config->port_clock; /* SPLL */
252 else if (IS_GEN5(dev_priv))
253 return ((I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2) * 10000;
e3b247da 254 else
21a727b3 255 return 270000;
021357ac
CW
256}
257
5d536e28 258static const intel_limit_t intel_limits_i8xx_dac = {
0206e353 259 .dot = { .min = 25000, .max = 350000 },
9c333719 260 .vco = { .min = 908000, .max = 1512000 },
91dbe5fb 261 .n = { .min = 2, .max = 16 },
0206e353
AJ
262 .m = { .min = 96, .max = 140 },
263 .m1 = { .min = 18, .max = 26 },
264 .m2 = { .min = 6, .max = 16 },
265 .p = { .min = 4, .max = 128 },
266 .p1 = { .min = 2, .max = 33 },
273e27ca
EA
267 .p2 = { .dot_limit = 165000,
268 .p2_slow = 4, .p2_fast = 2 },
e4b36699
KP
269};
270
5d536e28
DV
271static const intel_limit_t intel_limits_i8xx_dvo = {
272 .dot = { .min = 25000, .max = 350000 },
9c333719 273 .vco = { .min = 908000, .max = 1512000 },
91dbe5fb 274 .n = { .min = 2, .max = 16 },
5d536e28
DV
275 .m = { .min = 96, .max = 140 },
276 .m1 = { .min = 18, .max = 26 },
277 .m2 = { .min = 6, .max = 16 },
278 .p = { .min = 4, .max = 128 },
279 .p1 = { .min = 2, .max = 33 },
280 .p2 = { .dot_limit = 165000,
281 .p2_slow = 4, .p2_fast = 4 },
282};
283
e4b36699 284static const intel_limit_t intel_limits_i8xx_lvds = {
0206e353 285 .dot = { .min = 25000, .max = 350000 },
9c333719 286 .vco = { .min = 908000, .max = 1512000 },
91dbe5fb 287 .n = { .min = 2, .max = 16 },
0206e353
AJ
288 .m = { .min = 96, .max = 140 },
289 .m1 = { .min = 18, .max = 26 },
290 .m2 = { .min = 6, .max = 16 },
291 .p = { .min = 4, .max = 128 },
292 .p1 = { .min = 1, .max = 6 },
273e27ca
EA
293 .p2 = { .dot_limit = 165000,
294 .p2_slow = 14, .p2_fast = 7 },
e4b36699 295};
273e27ca 296
e4b36699 297static const intel_limit_t intel_limits_i9xx_sdvo = {
0206e353
AJ
298 .dot = { .min = 20000, .max = 400000 },
299 .vco = { .min = 1400000, .max = 2800000 },
300 .n = { .min = 1, .max = 6 },
301 .m = { .min = 70, .max = 120 },
4f7dfb67
PJ
302 .m1 = { .min = 8, .max = 18 },
303 .m2 = { .min = 3, .max = 7 },
0206e353
AJ
304 .p = { .min = 5, .max = 80 },
305 .p1 = { .min = 1, .max = 8 },
273e27ca
EA
306 .p2 = { .dot_limit = 200000,
307 .p2_slow = 10, .p2_fast = 5 },
e4b36699
KP
308};
309
310static const intel_limit_t intel_limits_i9xx_lvds = {
0206e353
AJ
311 .dot = { .min = 20000, .max = 400000 },
312 .vco = { .min = 1400000, .max = 2800000 },
313 .n = { .min = 1, .max = 6 },
314 .m = { .min = 70, .max = 120 },
53a7d2d1
PJ
315 .m1 = { .min = 8, .max = 18 },
316 .m2 = { .min = 3, .max = 7 },
0206e353
AJ
317 .p = { .min = 7, .max = 98 },
318 .p1 = { .min = 1, .max = 8 },
273e27ca
EA
319 .p2 = { .dot_limit = 112000,
320 .p2_slow = 14, .p2_fast = 7 },
e4b36699
KP
321};
322
273e27ca 323
e4b36699 324static const intel_limit_t intel_limits_g4x_sdvo = {
273e27ca
EA
325 .dot = { .min = 25000, .max = 270000 },
326 .vco = { .min = 1750000, .max = 3500000},
327 .n = { .min = 1, .max = 4 },
328 .m = { .min = 104, .max = 138 },
329 .m1 = { .min = 17, .max = 23 },
330 .m2 = { .min = 5, .max = 11 },
331 .p = { .min = 10, .max = 30 },
332 .p1 = { .min = 1, .max = 3},
333 .p2 = { .dot_limit = 270000,
334 .p2_slow = 10,
335 .p2_fast = 10
044c7c41 336 },
e4b36699
KP
337};
338
339static const intel_limit_t intel_limits_g4x_hdmi = {
273e27ca
EA
340 .dot = { .min = 22000, .max = 400000 },
341 .vco = { .min = 1750000, .max = 3500000},
342 .n = { .min = 1, .max = 4 },
343 .m = { .min = 104, .max = 138 },
344 .m1 = { .min = 16, .max = 23 },
345 .m2 = { .min = 5, .max = 11 },
346 .p = { .min = 5, .max = 80 },
347 .p1 = { .min = 1, .max = 8},
348 .p2 = { .dot_limit = 165000,
349 .p2_slow = 10, .p2_fast = 5 },
e4b36699
KP
350};
351
352static const intel_limit_t intel_limits_g4x_single_channel_lvds = {
273e27ca
EA
353 .dot = { .min = 20000, .max = 115000 },
354 .vco = { .min = 1750000, .max = 3500000 },
355 .n = { .min = 1, .max = 3 },
356 .m = { .min = 104, .max = 138 },
357 .m1 = { .min = 17, .max = 23 },
358 .m2 = { .min = 5, .max = 11 },
359 .p = { .min = 28, .max = 112 },
360 .p1 = { .min = 2, .max = 8 },
361 .p2 = { .dot_limit = 0,
362 .p2_slow = 14, .p2_fast = 14
044c7c41 363 },
e4b36699
KP
364};
365
366static const intel_limit_t intel_limits_g4x_dual_channel_lvds = {
273e27ca
EA
367 .dot = { .min = 80000, .max = 224000 },
368 .vco = { .min = 1750000, .max = 3500000 },
369 .n = { .min = 1, .max = 3 },
370 .m = { .min = 104, .max = 138 },
371 .m1 = { .min = 17, .max = 23 },
372 .m2 = { .min = 5, .max = 11 },
373 .p = { .min = 14, .max = 42 },
374 .p1 = { .min = 2, .max = 6 },
375 .p2 = { .dot_limit = 0,
376 .p2_slow = 7, .p2_fast = 7
044c7c41 377 },
e4b36699
KP
378};
379
f2b115e6 380static const intel_limit_t intel_limits_pineview_sdvo = {
0206e353
AJ
381 .dot = { .min = 20000, .max = 400000},
382 .vco = { .min = 1700000, .max = 3500000 },
273e27ca 383 /* Pineview's Ncounter is a ring counter */
0206e353
AJ
384 .n = { .min = 3, .max = 6 },
385 .m = { .min = 2, .max = 256 },
273e27ca 386 /* Pineview only has one combined m divider, which we treat as m2. */
0206e353
AJ
387 .m1 = { .min = 0, .max = 0 },
388 .m2 = { .min = 0, .max = 254 },
389 .p = { .min = 5, .max = 80 },
390 .p1 = { .min = 1, .max = 8 },
273e27ca
EA
391 .p2 = { .dot_limit = 200000,
392 .p2_slow = 10, .p2_fast = 5 },
e4b36699
KP
393};
394
f2b115e6 395static const intel_limit_t intel_limits_pineview_lvds = {
0206e353
AJ
396 .dot = { .min = 20000, .max = 400000 },
397 .vco = { .min = 1700000, .max = 3500000 },
398 .n = { .min = 3, .max = 6 },
399 .m = { .min = 2, .max = 256 },
400 .m1 = { .min = 0, .max = 0 },
401 .m2 = { .min = 0, .max = 254 },
402 .p = { .min = 7, .max = 112 },
403 .p1 = { .min = 1, .max = 8 },
273e27ca
EA
404 .p2 = { .dot_limit = 112000,
405 .p2_slow = 14, .p2_fast = 14 },
e4b36699
KP
406};
407
273e27ca
EA
408/* Ironlake / Sandybridge
409 *
410 * We calculate clock using (register_value + 2) for N/M1/M2, so here
411 * the range value for them is (actual_value - 2).
412 */
b91ad0ec 413static const intel_limit_t intel_limits_ironlake_dac = {
273e27ca
EA
414 .dot = { .min = 25000, .max = 350000 },
415 .vco = { .min = 1760000, .max = 3510000 },
416 .n = { .min = 1, .max = 5 },
417 .m = { .min = 79, .max = 127 },
418 .m1 = { .min = 12, .max = 22 },
419 .m2 = { .min = 5, .max = 9 },
420 .p = { .min = 5, .max = 80 },
421 .p1 = { .min = 1, .max = 8 },
422 .p2 = { .dot_limit = 225000,
423 .p2_slow = 10, .p2_fast = 5 },
e4b36699
KP
424};
425
b91ad0ec 426static const intel_limit_t intel_limits_ironlake_single_lvds = {
273e27ca
EA
427 .dot = { .min = 25000, .max = 350000 },
428 .vco = { .min = 1760000, .max = 3510000 },
429 .n = { .min = 1, .max = 3 },
430 .m = { .min = 79, .max = 118 },
431 .m1 = { .min = 12, .max = 22 },
432 .m2 = { .min = 5, .max = 9 },
433 .p = { .min = 28, .max = 112 },
434 .p1 = { .min = 2, .max = 8 },
435 .p2 = { .dot_limit = 225000,
436 .p2_slow = 14, .p2_fast = 14 },
b91ad0ec
ZW
437};
438
439static const intel_limit_t intel_limits_ironlake_dual_lvds = {
273e27ca
EA
440 .dot = { .min = 25000, .max = 350000 },
441 .vco = { .min = 1760000, .max = 3510000 },
442 .n = { .min = 1, .max = 3 },
443 .m = { .min = 79, .max = 127 },
444 .m1 = { .min = 12, .max = 22 },
445 .m2 = { .min = 5, .max = 9 },
446 .p = { .min = 14, .max = 56 },
447 .p1 = { .min = 2, .max = 8 },
448 .p2 = { .dot_limit = 225000,
449 .p2_slow = 7, .p2_fast = 7 },
b91ad0ec
ZW
450};
451
273e27ca 452/* LVDS 100mhz refclk limits. */
b91ad0ec 453static const intel_limit_t intel_limits_ironlake_single_lvds_100m = {
273e27ca
EA
454 .dot = { .min = 25000, .max = 350000 },
455 .vco = { .min = 1760000, .max = 3510000 },
456 .n = { .min = 1, .max = 2 },
457 .m = { .min = 79, .max = 126 },
458 .m1 = { .min = 12, .max = 22 },
459 .m2 = { .min = 5, .max = 9 },
460 .p = { .min = 28, .max = 112 },
0206e353 461 .p1 = { .min = 2, .max = 8 },
273e27ca
EA
462 .p2 = { .dot_limit = 225000,
463 .p2_slow = 14, .p2_fast = 14 },
b91ad0ec
ZW
464};
465
466static const intel_limit_t intel_limits_ironlake_dual_lvds_100m = {
273e27ca
EA
467 .dot = { .min = 25000, .max = 350000 },
468 .vco = { .min = 1760000, .max = 3510000 },
469 .n = { .min = 1, .max = 3 },
470 .m = { .min = 79, .max = 126 },
471 .m1 = { .min = 12, .max = 22 },
472 .m2 = { .min = 5, .max = 9 },
473 .p = { .min = 14, .max = 42 },
0206e353 474 .p1 = { .min = 2, .max = 6 },
273e27ca
EA
475 .p2 = { .dot_limit = 225000,
476 .p2_slow = 7, .p2_fast = 7 },
4547668a
ZY
477};
478
dc730512 479static const intel_limit_t intel_limits_vlv = {
f01b7962
VS
480 /*
481 * These are the data rate limits (measured in fast clocks)
482 * since those are the strictest limits we have. The fast
483 * clock and actual rate limits are more relaxed, so checking
484 * them would make no difference.
485 */
486 .dot = { .min = 25000 * 5, .max = 270000 * 5 },
75e53986 487 .vco = { .min = 4000000, .max = 6000000 },
a0c4da24 488 .n = { .min = 1, .max = 7 },
a0c4da24
JB
489 .m1 = { .min = 2, .max = 3 },
490 .m2 = { .min = 11, .max = 156 },
b99ab663 491 .p1 = { .min = 2, .max = 3 },
5fdc9c49 492 .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
a0c4da24
JB
493};
494
ef9348c8
CML
495static const intel_limit_t intel_limits_chv = {
496 /*
497 * These are the data rate limits (measured in fast clocks)
498 * since those are the strictest limits we have. The fast
499 * clock and actual rate limits are more relaxed, so checking
500 * them would make no difference.
501 */
502 .dot = { .min = 25000 * 5, .max = 540000 * 5},
17fe1021 503 .vco = { .min = 4800000, .max = 6480000 },
ef9348c8
CML
504 .n = { .min = 1, .max = 1 },
505 .m1 = { .min = 2, .max = 2 },
506 .m2 = { .min = 24 << 22, .max = 175 << 22 },
507 .p1 = { .min = 2, .max = 4 },
508 .p2 = { .p2_slow = 1, .p2_fast = 14 },
509};
510
5ab7b0b7
ID
511static const intel_limit_t intel_limits_bxt = {
512 /* FIXME: find real dot limits */
513 .dot = { .min = 0, .max = INT_MAX },
e6292556 514 .vco = { .min = 4800000, .max = 6700000 },
5ab7b0b7
ID
515 .n = { .min = 1, .max = 1 },
516 .m1 = { .min = 2, .max = 2 },
517 /* FIXME: find real m2 limits */
518 .m2 = { .min = 2 << 22, .max = 255 << 22 },
519 .p1 = { .min = 2, .max = 4 },
520 .p2 = { .p2_slow = 1, .p2_fast = 20 },
521};
522
cdba954e
ACO
523static bool
524needs_modeset(struct drm_crtc_state *state)
525{
fc596660 526 return drm_atomic_crtc_needs_modeset(state);
cdba954e
ACO
527}
528
e0638cdf
PZ
529/**
530 * Returns whether any output on the specified pipe is of the specified type
531 */
4093561b 532bool intel_pipe_has_type(struct intel_crtc *crtc, enum intel_output_type type)
e0638cdf 533{
409ee761 534 struct drm_device *dev = crtc->base.dev;
e0638cdf
PZ
535 struct intel_encoder *encoder;
536
409ee761 537 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
e0638cdf
PZ
538 if (encoder->type == type)
539 return true;
540
541 return false;
542}
543
d0737e1d
ACO
544/**
545 * Returns whether any output on the specified pipe will have the specified
546 * type after a staged modeset is complete, i.e., the same as
547 * intel_pipe_has_type() but looking at encoder->new_crtc instead of
548 * encoder->crtc.
549 */
a93e255f
ACO
550static bool intel_pipe_will_have_type(const struct intel_crtc_state *crtc_state,
551 int type)
d0737e1d 552{
a93e255f 553 struct drm_atomic_state *state = crtc_state->base.state;
da3ced29 554 struct drm_connector *connector;
a93e255f 555 struct drm_connector_state *connector_state;
d0737e1d 556 struct intel_encoder *encoder;
a93e255f
ACO
557 int i, num_connectors = 0;
558
da3ced29 559 for_each_connector_in_state(state, connector, connector_state, i) {
a93e255f
ACO
560 if (connector_state->crtc != crtc_state->base.crtc)
561 continue;
562
563 num_connectors++;
d0737e1d 564
a93e255f
ACO
565 encoder = to_intel_encoder(connector_state->best_encoder);
566 if (encoder->type == type)
d0737e1d 567 return true;
a93e255f
ACO
568 }
569
570 WARN_ON(num_connectors == 0);
d0737e1d
ACO
571
572 return false;
573}
574
dccbea3b
ID
575/*
576 * Platform specific helpers to calculate the port PLL loopback- (clock.m),
577 * and post-divider (clock.p) values, pre- (clock.vco) and post-divided fast
578 * (clock.dot) clock rates. This fast dot clock is fed to the port's IO logic.
579 * The helpers' return value is the rate of the clock that is fed to the
580 * display engine's pipe which can be the above fast dot clock rate or a
581 * divided-down version of it.
582 */
f2b115e6 583/* m1 is reserved as 0 in Pineview, n is a ring counter */
dccbea3b 584static int pnv_calc_dpll_params(int refclk, intel_clock_t *clock)
79e53945 585{
2177832f
SL
586 clock->m = clock->m2 + 2;
587 clock->p = clock->p1 * clock->p2;
ed5ca77e 588 if (WARN_ON(clock->n == 0 || clock->p == 0))
dccbea3b 589 return 0;
fb03ac01
VS
590 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
591 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
dccbea3b
ID
592
593 return clock->dot;
2177832f
SL
594}
595
7429e9d4
DV
596static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
597{
598 return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
599}
600
dccbea3b 601static int i9xx_calc_dpll_params(int refclk, intel_clock_t *clock)
2177832f 602{
7429e9d4 603 clock->m = i9xx_dpll_compute_m(clock);
79e53945 604 clock->p = clock->p1 * clock->p2;
ed5ca77e 605 if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
dccbea3b 606 return 0;
fb03ac01
VS
607 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
608 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
dccbea3b
ID
609
610 return clock->dot;
79e53945
JB
611}
612
dccbea3b 613static int vlv_calc_dpll_params(int refclk, intel_clock_t *clock)
589eca67
ID
614{
615 clock->m = clock->m1 * clock->m2;
616 clock->p = clock->p1 * clock->p2;
617 if (WARN_ON(clock->n == 0 || clock->p == 0))
dccbea3b 618 return 0;
589eca67
ID
619 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
620 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
dccbea3b
ID
621
622 return clock->dot / 5;
589eca67
ID
623}
624
dccbea3b 625int chv_calc_dpll_params(int refclk, intel_clock_t *clock)
ef9348c8
CML
626{
627 clock->m = clock->m1 * clock->m2;
628 clock->p = clock->p1 * clock->p2;
629 if (WARN_ON(clock->n == 0 || clock->p == 0))
dccbea3b 630 return 0;
ef9348c8
CML
631 clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
632 clock->n << 22);
633 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
dccbea3b
ID
634
635 return clock->dot / 5;
ef9348c8
CML
636}
637
7c04d1d9 638#define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
79e53945
JB
639/**
640 * Returns whether the given set of divisors are valid for a given refclk with
641 * the given connectors.
642 */
643
1b894b59
CW
644static bool intel_PLL_is_valid(struct drm_device *dev,
645 const intel_limit_t *limit,
646 const intel_clock_t *clock)
79e53945 647{
f01b7962
VS
648 if (clock->n < limit->n.min || limit->n.max < clock->n)
649 INTELPllInvalid("n out of range\n");
79e53945 650 if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
0206e353 651 INTELPllInvalid("p1 out of range\n");
79e53945 652 if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
0206e353 653 INTELPllInvalid("m2 out of range\n");
79e53945 654 if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
0206e353 655 INTELPllInvalid("m1 out of range\n");
f01b7962 656
666a4537
WB
657 if (!IS_PINEVIEW(dev) && !IS_VALLEYVIEW(dev) &&
658 !IS_CHERRYVIEW(dev) && !IS_BROXTON(dev))
f01b7962
VS
659 if (clock->m1 <= clock->m2)
660 INTELPllInvalid("m1 <= m2\n");
661
666a4537 662 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev) && !IS_BROXTON(dev)) {
f01b7962
VS
663 if (clock->p < limit->p.min || limit->p.max < clock->p)
664 INTELPllInvalid("p out of range\n");
665 if (clock->m < limit->m.min || limit->m.max < clock->m)
666 INTELPllInvalid("m out of range\n");
667 }
668
79e53945 669 if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
0206e353 670 INTELPllInvalid("vco out of range\n");
79e53945
JB
671 /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
672 * connector, etc., rather than just a single range.
673 */
674 if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
0206e353 675 INTELPllInvalid("dot out of range\n");
79e53945
JB
676
677 return true;
678}
679
3b1429d9
VS
680static int
681i9xx_select_p2_div(const intel_limit_t *limit,
682 const struct intel_crtc_state *crtc_state,
683 int target)
79e53945 684{
3b1429d9 685 struct drm_device *dev = crtc_state->base.crtc->dev;
79e53945 686
a93e255f 687 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
79e53945 688 /*
a210b028
DV
689 * For LVDS just rely on its current settings for dual-channel.
690 * We haven't figured out how to reliably set up different
691 * single/dual channel state, if we even can.
79e53945 692 */
1974cad0 693 if (intel_is_dual_link_lvds(dev))
3b1429d9 694 return limit->p2.p2_fast;
79e53945 695 else
3b1429d9 696 return limit->p2.p2_slow;
79e53945
JB
697 } else {
698 if (target < limit->p2.dot_limit)
3b1429d9 699 return limit->p2.p2_slow;
79e53945 700 else
3b1429d9 701 return limit->p2.p2_fast;
79e53945 702 }
3b1429d9
VS
703}
704
70e8aa21
ACO
705/*
706 * Returns a set of divisors for the desired target clock with the given
707 * refclk, or FALSE. The returned values represent the clock equation:
708 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
709 *
710 * Target and reference clocks are specified in kHz.
711 *
712 * If match_clock is provided, then best_clock P divider must match the P
713 * divider from @match_clock used for LVDS downclocking.
714 */
3b1429d9
VS
715static bool
716i9xx_find_best_dpll(const intel_limit_t *limit,
717 struct intel_crtc_state *crtc_state,
718 int target, int refclk, intel_clock_t *match_clock,
719 intel_clock_t *best_clock)
720{
721 struct drm_device *dev = crtc_state->base.crtc->dev;
722 intel_clock_t clock;
723 int err = target;
79e53945 724
0206e353 725 memset(best_clock, 0, sizeof(*best_clock));
79e53945 726
3b1429d9
VS
727 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
728
42158660
ZY
729 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
730 clock.m1++) {
731 for (clock.m2 = limit->m2.min;
732 clock.m2 <= limit->m2.max; clock.m2++) {
c0efc387 733 if (clock.m2 >= clock.m1)
42158660
ZY
734 break;
735 for (clock.n = limit->n.min;
736 clock.n <= limit->n.max; clock.n++) {
737 for (clock.p1 = limit->p1.min;
738 clock.p1 <= limit->p1.max; clock.p1++) {
79e53945
JB
739 int this_err;
740
dccbea3b 741 i9xx_calc_dpll_params(refclk, &clock);
ac58c3f0
DV
742 if (!intel_PLL_is_valid(dev, limit,
743 &clock))
744 continue;
745 if (match_clock &&
746 clock.p != match_clock->p)
747 continue;
748
749 this_err = abs(clock.dot - target);
750 if (this_err < err) {
751 *best_clock = clock;
752 err = this_err;
753 }
754 }
755 }
756 }
757 }
758
759 return (err != target);
760}
761
70e8aa21
ACO
762/*
763 * Returns a set of divisors for the desired target clock with the given
764 * refclk, or FALSE. The returned values represent the clock equation:
765 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
766 *
767 * Target and reference clocks are specified in kHz.
768 *
769 * If match_clock is provided, then best_clock P divider must match the P
770 * divider from @match_clock used for LVDS downclocking.
771 */
ac58c3f0 772static bool
a93e255f
ACO
773pnv_find_best_dpll(const intel_limit_t *limit,
774 struct intel_crtc_state *crtc_state,
ee9300bb
DV
775 int target, int refclk, intel_clock_t *match_clock,
776 intel_clock_t *best_clock)
79e53945 777{
3b1429d9 778 struct drm_device *dev = crtc_state->base.crtc->dev;
79e53945 779 intel_clock_t clock;
79e53945
JB
780 int err = target;
781
0206e353 782 memset(best_clock, 0, sizeof(*best_clock));
79e53945 783
3b1429d9
VS
784 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
785
42158660
ZY
786 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
787 clock.m1++) {
788 for (clock.m2 = limit->m2.min;
789 clock.m2 <= limit->m2.max; clock.m2++) {
42158660
ZY
790 for (clock.n = limit->n.min;
791 clock.n <= limit->n.max; clock.n++) {
792 for (clock.p1 = limit->p1.min;
793 clock.p1 <= limit->p1.max; clock.p1++) {
79e53945
JB
794 int this_err;
795
dccbea3b 796 pnv_calc_dpll_params(refclk, &clock);
1b894b59
CW
797 if (!intel_PLL_is_valid(dev, limit,
798 &clock))
79e53945 799 continue;
cec2f356
SP
800 if (match_clock &&
801 clock.p != match_clock->p)
802 continue;
79e53945
JB
803
804 this_err = abs(clock.dot - target);
805 if (this_err < err) {
806 *best_clock = clock;
807 err = this_err;
808 }
809 }
810 }
811 }
812 }
813
814 return (err != target);
815}
816
997c030c
ACO
817/*
818 * Returns a set of divisors for the desired target clock with the given
819 * refclk, or FALSE. The returned values represent the clock equation:
820 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
70e8aa21
ACO
821 *
822 * Target and reference clocks are specified in kHz.
823 *
824 * If match_clock is provided, then best_clock P divider must match the P
825 * divider from @match_clock used for LVDS downclocking.
997c030c 826 */
d4906093 827static bool
a93e255f
ACO
828g4x_find_best_dpll(const intel_limit_t *limit,
829 struct intel_crtc_state *crtc_state,
ee9300bb
DV
830 int target, int refclk, intel_clock_t *match_clock,
831 intel_clock_t *best_clock)
d4906093 832{
3b1429d9 833 struct drm_device *dev = crtc_state->base.crtc->dev;
d4906093
ML
834 intel_clock_t clock;
835 int max_n;
3b1429d9 836 bool found = false;
6ba770dc
AJ
837 /* approximately equals target * 0.00585 */
838 int err_most = (target >> 8) + (target >> 9);
d4906093
ML
839
840 memset(best_clock, 0, sizeof(*best_clock));
3b1429d9
VS
841
842 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
843
d4906093 844 max_n = limit->n.max;
f77f13e2 845 /* based on hardware requirement, prefer smaller n to precision */
d4906093 846 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
f77f13e2 847 /* based on hardware requirement, prefere larger m1,m2 */
d4906093
ML
848 for (clock.m1 = limit->m1.max;
849 clock.m1 >= limit->m1.min; clock.m1--) {
850 for (clock.m2 = limit->m2.max;
851 clock.m2 >= limit->m2.min; clock.m2--) {
852 for (clock.p1 = limit->p1.max;
853 clock.p1 >= limit->p1.min; clock.p1--) {
854 int this_err;
855
dccbea3b 856 i9xx_calc_dpll_params(refclk, &clock);
1b894b59
CW
857 if (!intel_PLL_is_valid(dev, limit,
858 &clock))
d4906093 859 continue;
1b894b59
CW
860
861 this_err = abs(clock.dot - target);
d4906093
ML
862 if (this_err < err_most) {
863 *best_clock = clock;
864 err_most = this_err;
865 max_n = clock.n;
866 found = true;
867 }
868 }
869 }
870 }
871 }
2c07245f
ZW
872 return found;
873}
874
d5dd62bd
ID
875/*
876 * Check if the calculated PLL configuration is more optimal compared to the
877 * best configuration and error found so far. Return the calculated error.
878 */
879static bool vlv_PLL_is_optimal(struct drm_device *dev, int target_freq,
880 const intel_clock_t *calculated_clock,
881 const intel_clock_t *best_clock,
882 unsigned int best_error_ppm,
883 unsigned int *error_ppm)
884{
9ca3ba01
ID
885 /*
886 * For CHV ignore the error and consider only the P value.
887 * Prefer a bigger P value based on HW requirements.
888 */
889 if (IS_CHERRYVIEW(dev)) {
890 *error_ppm = 0;
891
892 return calculated_clock->p > best_clock->p;
893 }
894
24be4e46
ID
895 if (WARN_ON_ONCE(!target_freq))
896 return false;
897
d5dd62bd
ID
898 *error_ppm = div_u64(1000000ULL *
899 abs(target_freq - calculated_clock->dot),
900 target_freq);
901 /*
902 * Prefer a better P value over a better (smaller) error if the error
903 * is small. Ensure this preference for future configurations too by
904 * setting the error to 0.
905 */
906 if (*error_ppm < 100 && calculated_clock->p > best_clock->p) {
907 *error_ppm = 0;
908
909 return true;
910 }
911
912 return *error_ppm + 10 < best_error_ppm;
913}
914
65b3d6a9
ACO
915/*
916 * Returns a set of divisors for the desired target clock with the given
917 * refclk, or FALSE. The returned values represent the clock equation:
918 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
919 */
a0c4da24 920static bool
a93e255f
ACO
921vlv_find_best_dpll(const intel_limit_t *limit,
922 struct intel_crtc_state *crtc_state,
ee9300bb
DV
923 int target, int refclk, intel_clock_t *match_clock,
924 intel_clock_t *best_clock)
a0c4da24 925{
a93e255f 926 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
a919ff14 927 struct drm_device *dev = crtc->base.dev;
6b4bf1c4 928 intel_clock_t clock;
69e4f900 929 unsigned int bestppm = 1000000;
27e639bf
VS
930 /* min update 19.2 MHz */
931 int max_n = min(limit->n.max, refclk / 19200);
49e497ef 932 bool found = false;
a0c4da24 933
6b4bf1c4
VS
934 target *= 5; /* fast clock */
935
936 memset(best_clock, 0, sizeof(*best_clock));
a0c4da24
JB
937
938 /* based on hardware requirement, prefer smaller n to precision */
27e639bf 939 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
811bbf05 940 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
889059d8 941 for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
c1a9ae43 942 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
6b4bf1c4 943 clock.p = clock.p1 * clock.p2;
a0c4da24 944 /* based on hardware requirement, prefer bigger m1,m2 values */
6b4bf1c4 945 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
d5dd62bd 946 unsigned int ppm;
69e4f900 947
6b4bf1c4
VS
948 clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
949 refclk * clock.m1);
950
dccbea3b 951 vlv_calc_dpll_params(refclk, &clock);
43b0ac53 952
f01b7962
VS
953 if (!intel_PLL_is_valid(dev, limit,
954 &clock))
43b0ac53
VS
955 continue;
956
d5dd62bd
ID
957 if (!vlv_PLL_is_optimal(dev, target,
958 &clock,
959 best_clock,
960 bestppm, &ppm))
961 continue;
6b4bf1c4 962
d5dd62bd
ID
963 *best_clock = clock;
964 bestppm = ppm;
965 found = true;
a0c4da24
JB
966 }
967 }
968 }
969 }
a0c4da24 970
49e497ef 971 return found;
a0c4da24 972}
a4fc5ed6 973
65b3d6a9
ACO
974/*
975 * Returns a set of divisors for the desired target clock with the given
976 * refclk, or FALSE. The returned values represent the clock equation:
977 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
978 */
ef9348c8 979static bool
a93e255f
ACO
980chv_find_best_dpll(const intel_limit_t *limit,
981 struct intel_crtc_state *crtc_state,
ef9348c8
CML
982 int target, int refclk, intel_clock_t *match_clock,
983 intel_clock_t *best_clock)
984{
a93e255f 985 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
a919ff14 986 struct drm_device *dev = crtc->base.dev;
9ca3ba01 987 unsigned int best_error_ppm;
ef9348c8
CML
988 intel_clock_t clock;
989 uint64_t m2;
990 int found = false;
991
992 memset(best_clock, 0, sizeof(*best_clock));
9ca3ba01 993 best_error_ppm = 1000000;
ef9348c8
CML
994
995 /*
996 * Based on hardware doc, the n always set to 1, and m1 always
997 * set to 2. If requires to support 200Mhz refclk, we need to
998 * revisit this because n may not 1 anymore.
999 */
1000 clock.n = 1, clock.m1 = 2;
1001 target *= 5; /* fast clock */
1002
1003 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
1004 for (clock.p2 = limit->p2.p2_fast;
1005 clock.p2 >= limit->p2.p2_slow;
1006 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
9ca3ba01 1007 unsigned int error_ppm;
ef9348c8
CML
1008
1009 clock.p = clock.p1 * clock.p2;
1010
1011 m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
1012 clock.n) << 22, refclk * clock.m1);
1013
1014 if (m2 > INT_MAX/clock.m1)
1015 continue;
1016
1017 clock.m2 = m2;
1018
dccbea3b 1019 chv_calc_dpll_params(refclk, &clock);
ef9348c8
CML
1020
1021 if (!intel_PLL_is_valid(dev, limit, &clock))
1022 continue;
1023
9ca3ba01
ID
1024 if (!vlv_PLL_is_optimal(dev, target, &clock, best_clock,
1025 best_error_ppm, &error_ppm))
1026 continue;
1027
1028 *best_clock = clock;
1029 best_error_ppm = error_ppm;
1030 found = true;
ef9348c8
CML
1031 }
1032 }
1033
1034 return found;
1035}
1036
5ab7b0b7
ID
1037bool bxt_find_best_dpll(struct intel_crtc_state *crtc_state, int target_clock,
1038 intel_clock_t *best_clock)
1039{
65b3d6a9
ACO
1040 int refclk = 100000;
1041 const intel_limit_t *limit = &intel_limits_bxt;
5ab7b0b7 1042
65b3d6a9 1043 return chv_find_best_dpll(limit, crtc_state,
5ab7b0b7
ID
1044 target_clock, refclk, NULL, best_clock);
1045}
1046
20ddf665
VS
1047bool intel_crtc_active(struct drm_crtc *crtc)
1048{
1049 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1050
1051 /* Be paranoid as we can arrive here with only partial
1052 * state retrieved from the hardware during setup.
1053 *
241bfc38 1054 * We can ditch the adjusted_mode.crtc_clock check as soon
20ddf665
VS
1055 * as Haswell has gained clock readout/fastboot support.
1056 *
66e514c1 1057 * We can ditch the crtc->primary->fb check as soon as we can
20ddf665 1058 * properly reconstruct framebuffers.
c3d1f436
MR
1059 *
1060 * FIXME: The intel_crtc->active here should be switched to
1061 * crtc->state->active once we have proper CRTC states wired up
1062 * for atomic.
20ddf665 1063 */
c3d1f436 1064 return intel_crtc->active && crtc->primary->state->fb &&
6e3c9717 1065 intel_crtc->config->base.adjusted_mode.crtc_clock;
20ddf665
VS
1066}
1067
a5c961d1
PZ
1068enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
1069 enum pipe pipe)
1070{
1071 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
1072 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1073
6e3c9717 1074 return intel_crtc->config->cpu_transcoder;
a5c961d1
PZ
1075}
1076
fbf49ea2
VS
1077static bool pipe_dsl_stopped(struct drm_device *dev, enum pipe pipe)
1078{
1079 struct drm_i915_private *dev_priv = dev->dev_private;
f0f59a00 1080 i915_reg_t reg = PIPEDSL(pipe);
fbf49ea2
VS
1081 u32 line1, line2;
1082 u32 line_mask;
1083
1084 if (IS_GEN2(dev))
1085 line_mask = DSL_LINEMASK_GEN2;
1086 else
1087 line_mask = DSL_LINEMASK_GEN3;
1088
1089 line1 = I915_READ(reg) & line_mask;
6adfb1ef 1090 msleep(5);
fbf49ea2
VS
1091 line2 = I915_READ(reg) & line_mask;
1092
1093 return line1 == line2;
1094}
1095
ab7ad7f6
KP
1096/*
1097 * intel_wait_for_pipe_off - wait for pipe to turn off
575f7ab7 1098 * @crtc: crtc whose pipe to wait for
9d0498a2
JB
1099 *
1100 * After disabling a pipe, we can't wait for vblank in the usual way,
1101 * spinning on the vblank interrupt status bit, since we won't actually
1102 * see an interrupt when the pipe is disabled.
1103 *
ab7ad7f6
KP
1104 * On Gen4 and above:
1105 * wait for the pipe register state bit to turn off
1106 *
1107 * Otherwise:
1108 * wait for the display line value to settle (it usually
1109 * ends up stopping at the start of the next frame).
58e10eb9 1110 *
9d0498a2 1111 */
575f7ab7 1112static void intel_wait_for_pipe_off(struct intel_crtc *crtc)
9d0498a2 1113{
575f7ab7 1114 struct drm_device *dev = crtc->base.dev;
9d0498a2 1115 struct drm_i915_private *dev_priv = dev->dev_private;
6e3c9717 1116 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
575f7ab7 1117 enum pipe pipe = crtc->pipe;
ab7ad7f6
KP
1118
1119 if (INTEL_INFO(dev)->gen >= 4) {
f0f59a00 1120 i915_reg_t reg = PIPECONF(cpu_transcoder);
ab7ad7f6
KP
1121
1122 /* Wait for the Pipe State to go off */
58e10eb9
CW
1123 if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
1124 100))
284637d9 1125 WARN(1, "pipe_off wait timed out\n");
ab7ad7f6 1126 } else {
ab7ad7f6 1127 /* Wait for the display line to settle */
fbf49ea2 1128 if (wait_for(pipe_dsl_stopped(dev, pipe), 100))
284637d9 1129 WARN(1, "pipe_off wait timed out\n");
ab7ad7f6 1130 }
79e53945
JB
1131}
1132
b24e7179 1133/* Only for pre-ILK configs */
55607e8a
DV
1134void assert_pll(struct drm_i915_private *dev_priv,
1135 enum pipe pipe, bool state)
b24e7179 1136{
b24e7179
JB
1137 u32 val;
1138 bool cur_state;
1139
649636ef 1140 val = I915_READ(DPLL(pipe));
b24e7179 1141 cur_state = !!(val & DPLL_VCO_ENABLE);
e2c719b7 1142 I915_STATE_WARN(cur_state != state,
b24e7179 1143 "PLL state assertion failure (expected %s, current %s)\n",
87ad3212 1144 onoff(state), onoff(cur_state));
b24e7179 1145}
b24e7179 1146
23538ef1 1147/* XXX: the dsi pll is shared between MIPI DSI ports */
8563b1e8 1148void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
23538ef1
JN
1149{
1150 u32 val;
1151 bool cur_state;
1152
a580516d 1153 mutex_lock(&dev_priv->sb_lock);
23538ef1 1154 val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
a580516d 1155 mutex_unlock(&dev_priv->sb_lock);
23538ef1
JN
1156
1157 cur_state = val & DSI_PLL_VCO_EN;
e2c719b7 1158 I915_STATE_WARN(cur_state != state,
23538ef1 1159 "DSI PLL state assertion failure (expected %s, current %s)\n",
87ad3212 1160 onoff(state), onoff(cur_state));
23538ef1 1161}
23538ef1 1162
040484af
JB
1163static void assert_fdi_tx(struct drm_i915_private *dev_priv,
1164 enum pipe pipe, bool state)
1165{
040484af 1166 bool cur_state;
ad80a810
PZ
1167 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1168 pipe);
040484af 1169
2d1fe073 1170 if (HAS_DDI(dev_priv)) {
affa9354 1171 /* DDI does not have a specific FDI_TX register */
649636ef 1172 u32 val = I915_READ(TRANS_DDI_FUNC_CTL(cpu_transcoder));
ad80a810 1173 cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
bf507ef7 1174 } else {
649636ef 1175 u32 val = I915_READ(FDI_TX_CTL(pipe));
bf507ef7
ED
1176 cur_state = !!(val & FDI_TX_ENABLE);
1177 }
e2c719b7 1178 I915_STATE_WARN(cur_state != state,
040484af 1179 "FDI TX state assertion failure (expected %s, current %s)\n",
87ad3212 1180 onoff(state), onoff(cur_state));
040484af
JB
1181}
1182#define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
1183#define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
1184
1185static void assert_fdi_rx(struct drm_i915_private *dev_priv,
1186 enum pipe pipe, bool state)
1187{
040484af
JB
1188 u32 val;
1189 bool cur_state;
1190
649636ef 1191 val = I915_READ(FDI_RX_CTL(pipe));
d63fa0dc 1192 cur_state = !!(val & FDI_RX_ENABLE);
e2c719b7 1193 I915_STATE_WARN(cur_state != state,
040484af 1194 "FDI RX state assertion failure (expected %s, current %s)\n",
87ad3212 1195 onoff(state), onoff(cur_state));
040484af
JB
1196}
1197#define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
1198#define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
1199
1200static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
1201 enum pipe pipe)
1202{
040484af
JB
1203 u32 val;
1204
1205 /* ILK FDI PLL is always enabled */
2d1fe073 1206 if (INTEL_INFO(dev_priv)->gen == 5)
040484af
JB
1207 return;
1208
bf507ef7 1209 /* On Haswell, DDI ports are responsible for the FDI PLL setup */
2d1fe073 1210 if (HAS_DDI(dev_priv))
bf507ef7
ED
1211 return;
1212
649636ef 1213 val = I915_READ(FDI_TX_CTL(pipe));
e2c719b7 1214 I915_STATE_WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
040484af
JB
1215}
1216
55607e8a
DV
1217void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
1218 enum pipe pipe, bool state)
040484af 1219{
040484af 1220 u32 val;
55607e8a 1221 bool cur_state;
040484af 1222
649636ef 1223 val = I915_READ(FDI_RX_CTL(pipe));
55607e8a 1224 cur_state = !!(val & FDI_RX_PLL_ENABLE);
e2c719b7 1225 I915_STATE_WARN(cur_state != state,
55607e8a 1226 "FDI RX PLL assertion failure (expected %s, current %s)\n",
87ad3212 1227 onoff(state), onoff(cur_state));
040484af
JB
1228}
1229
b680c37a
DV
1230void assert_panel_unlocked(struct drm_i915_private *dev_priv,
1231 enum pipe pipe)
ea0760cf 1232{
bedd4dba 1233 struct drm_device *dev = dev_priv->dev;
f0f59a00 1234 i915_reg_t pp_reg;
ea0760cf
JB
1235 u32 val;
1236 enum pipe panel_pipe = PIPE_A;
0de3b485 1237 bool locked = true;
ea0760cf 1238
bedd4dba
JN
1239 if (WARN_ON(HAS_DDI(dev)))
1240 return;
1241
1242 if (HAS_PCH_SPLIT(dev)) {
1243 u32 port_sel;
1244
ea0760cf 1245 pp_reg = PCH_PP_CONTROL;
bedd4dba
JN
1246 port_sel = I915_READ(PCH_PP_ON_DELAYS) & PANEL_PORT_SELECT_MASK;
1247
1248 if (port_sel == PANEL_PORT_SELECT_LVDS &&
1249 I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
1250 panel_pipe = PIPE_B;
1251 /* XXX: else fix for eDP */
666a4537 1252 } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
bedd4dba
JN
1253 /* presumably write lock depends on pipe, not port select */
1254 pp_reg = VLV_PIPE_PP_CONTROL(pipe);
1255 panel_pipe = pipe;
ea0760cf
JB
1256 } else {
1257 pp_reg = PP_CONTROL;
bedd4dba
JN
1258 if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
1259 panel_pipe = PIPE_B;
ea0760cf
JB
1260 }
1261
1262 val = I915_READ(pp_reg);
1263 if (!(val & PANEL_POWER_ON) ||
ec49ba2d 1264 ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
ea0760cf
JB
1265 locked = false;
1266
e2c719b7 1267 I915_STATE_WARN(panel_pipe == pipe && locked,
ea0760cf 1268 "panel assertion failure, pipe %c regs locked\n",
9db4a9c7 1269 pipe_name(pipe));
ea0760cf
JB
1270}
1271
93ce0ba6
JN
1272static void assert_cursor(struct drm_i915_private *dev_priv,
1273 enum pipe pipe, bool state)
1274{
1275 struct drm_device *dev = dev_priv->dev;
1276 bool cur_state;
1277
d9d82081 1278 if (IS_845G(dev) || IS_I865G(dev))
0b87c24e 1279 cur_state = I915_READ(CURCNTR(PIPE_A)) & CURSOR_ENABLE;
d9d82081 1280 else
5efb3e28 1281 cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
93ce0ba6 1282
e2c719b7 1283 I915_STATE_WARN(cur_state != state,
93ce0ba6 1284 "cursor on pipe %c assertion failure (expected %s, current %s)\n",
87ad3212 1285 pipe_name(pipe), onoff(state), onoff(cur_state));
93ce0ba6
JN
1286}
1287#define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
1288#define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
1289
b840d907
JB
1290void assert_pipe(struct drm_i915_private *dev_priv,
1291 enum pipe pipe, bool state)
b24e7179 1292{
63d7bbe9 1293 bool cur_state;
702e7a56
PZ
1294 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1295 pipe);
4feed0eb 1296 enum intel_display_power_domain power_domain;
b24e7179 1297
b6b5d049
VS
1298 /* if we need the pipe quirk it must be always on */
1299 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1300 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
8e636784
DV
1301 state = true;
1302
4feed0eb
ID
1303 power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
1304 if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
649636ef 1305 u32 val = I915_READ(PIPECONF(cpu_transcoder));
69310161 1306 cur_state = !!(val & PIPECONF_ENABLE);
4feed0eb
ID
1307
1308 intel_display_power_put(dev_priv, power_domain);
1309 } else {
1310 cur_state = false;
69310161
PZ
1311 }
1312
e2c719b7 1313 I915_STATE_WARN(cur_state != state,
63d7bbe9 1314 "pipe %c assertion failure (expected %s, current %s)\n",
87ad3212 1315 pipe_name(pipe), onoff(state), onoff(cur_state));
b24e7179
JB
1316}
1317
931872fc
CW
1318static void assert_plane(struct drm_i915_private *dev_priv,
1319 enum plane plane, bool state)
b24e7179 1320{
b24e7179 1321 u32 val;
931872fc 1322 bool cur_state;
b24e7179 1323
649636ef 1324 val = I915_READ(DSPCNTR(plane));
931872fc 1325 cur_state = !!(val & DISPLAY_PLANE_ENABLE);
e2c719b7 1326 I915_STATE_WARN(cur_state != state,
931872fc 1327 "plane %c assertion failure (expected %s, current %s)\n",
87ad3212 1328 plane_name(plane), onoff(state), onoff(cur_state));
b24e7179
JB
1329}
1330
931872fc
CW
1331#define assert_plane_enabled(d, p) assert_plane(d, p, true)
1332#define assert_plane_disabled(d, p) assert_plane(d, p, false)
1333
b24e7179
JB
1334static void assert_planes_disabled(struct drm_i915_private *dev_priv,
1335 enum pipe pipe)
1336{
653e1026 1337 struct drm_device *dev = dev_priv->dev;
649636ef 1338 int i;
b24e7179 1339
653e1026
VS
1340 /* Primary planes are fixed to pipes on gen4+ */
1341 if (INTEL_INFO(dev)->gen >= 4) {
649636ef 1342 u32 val = I915_READ(DSPCNTR(pipe));
e2c719b7 1343 I915_STATE_WARN(val & DISPLAY_PLANE_ENABLE,
28c05794
AJ
1344 "plane %c assertion failure, should be disabled but not\n",
1345 plane_name(pipe));
19ec1358 1346 return;
28c05794 1347 }
19ec1358 1348
b24e7179 1349 /* Need to check both planes against the pipe */
055e393f 1350 for_each_pipe(dev_priv, i) {
649636ef
VS
1351 u32 val = I915_READ(DSPCNTR(i));
1352 enum pipe cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
b24e7179 1353 DISPPLANE_SEL_PIPE_SHIFT;
e2c719b7 1354 I915_STATE_WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
9db4a9c7
JB
1355 "plane %c assertion failure, should be off on pipe %c but is still active\n",
1356 plane_name(i), pipe_name(pipe));
b24e7179
JB
1357 }
1358}
1359
19332d7a
JB
1360static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
1361 enum pipe pipe)
1362{
20674eef 1363 struct drm_device *dev = dev_priv->dev;
649636ef 1364 int sprite;
19332d7a 1365
7feb8b88 1366 if (INTEL_INFO(dev)->gen >= 9) {
3bdcfc0c 1367 for_each_sprite(dev_priv, pipe, sprite) {
649636ef 1368 u32 val = I915_READ(PLANE_CTL(pipe, sprite));
e2c719b7 1369 I915_STATE_WARN(val & PLANE_CTL_ENABLE,
7feb8b88
DL
1370 "plane %d assertion failure, should be off on pipe %c but is still active\n",
1371 sprite, pipe_name(pipe));
1372 }
666a4537 1373 } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
3bdcfc0c 1374 for_each_sprite(dev_priv, pipe, sprite) {
649636ef 1375 u32 val = I915_READ(SPCNTR(pipe, sprite));
e2c719b7 1376 I915_STATE_WARN(val & SP_ENABLE,
20674eef 1377 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
1fe47785 1378 sprite_name(pipe, sprite), pipe_name(pipe));
20674eef
VS
1379 }
1380 } else if (INTEL_INFO(dev)->gen >= 7) {
649636ef 1381 u32 val = I915_READ(SPRCTL(pipe));
e2c719b7 1382 I915_STATE_WARN(val & SPRITE_ENABLE,
06da8da2 1383 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
20674eef
VS
1384 plane_name(pipe), pipe_name(pipe));
1385 } else if (INTEL_INFO(dev)->gen >= 5) {
649636ef 1386 u32 val = I915_READ(DVSCNTR(pipe));
e2c719b7 1387 I915_STATE_WARN(val & DVS_ENABLE,
06da8da2 1388 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
20674eef 1389 plane_name(pipe), pipe_name(pipe));
19332d7a
JB
1390 }
1391}
1392
08c71e5e
VS
1393static void assert_vblank_disabled(struct drm_crtc *crtc)
1394{
e2c719b7 1395 if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
08c71e5e
VS
1396 drm_crtc_vblank_put(crtc);
1397}
1398
7abd4b35
ACO
1399void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
1400 enum pipe pipe)
92f2584a 1401{
92f2584a
JB
1402 u32 val;
1403 bool enabled;
1404
649636ef 1405 val = I915_READ(PCH_TRANSCONF(pipe));
92f2584a 1406 enabled = !!(val & TRANS_ENABLE);
e2c719b7 1407 I915_STATE_WARN(enabled,
9db4a9c7
JB
1408 "transcoder assertion failed, should be off on pipe %c but is still active\n",
1409 pipe_name(pipe));
92f2584a
JB
1410}
1411
4e634389
KP
1412static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
1413 enum pipe pipe, u32 port_sel, u32 val)
f0575e92
KP
1414{
1415 if ((val & DP_PORT_EN) == 0)
1416 return false;
1417
2d1fe073 1418 if (HAS_PCH_CPT(dev_priv)) {
f0f59a00 1419 u32 trans_dp_ctl = I915_READ(TRANS_DP_CTL(pipe));
f0575e92
KP
1420 if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
1421 return false;
2d1fe073 1422 } else if (IS_CHERRYVIEW(dev_priv)) {
44f37d1f
CML
1423 if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
1424 return false;
f0575e92
KP
1425 } else {
1426 if ((val & DP_PIPE_MASK) != (pipe << 30))
1427 return false;
1428 }
1429 return true;
1430}
1431
1519b995
KP
1432static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
1433 enum pipe pipe, u32 val)
1434{
dc0fa718 1435 if ((val & SDVO_ENABLE) == 0)
1519b995
KP
1436 return false;
1437
2d1fe073 1438 if (HAS_PCH_CPT(dev_priv)) {
dc0fa718 1439 if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
1519b995 1440 return false;
2d1fe073 1441 } else if (IS_CHERRYVIEW(dev_priv)) {
44f37d1f
CML
1442 if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
1443 return false;
1519b995 1444 } else {
dc0fa718 1445 if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
1519b995
KP
1446 return false;
1447 }
1448 return true;
1449}
1450
1451static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
1452 enum pipe pipe, u32 val)
1453{
1454 if ((val & LVDS_PORT_EN) == 0)
1455 return false;
1456
2d1fe073 1457 if (HAS_PCH_CPT(dev_priv)) {
1519b995
KP
1458 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1459 return false;
1460 } else {
1461 if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
1462 return false;
1463 }
1464 return true;
1465}
1466
1467static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
1468 enum pipe pipe, u32 val)
1469{
1470 if ((val & ADPA_DAC_ENABLE) == 0)
1471 return false;
2d1fe073 1472 if (HAS_PCH_CPT(dev_priv)) {
1519b995
KP
1473 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1474 return false;
1475 } else {
1476 if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
1477 return false;
1478 }
1479 return true;
1480}
1481
291906f1 1482static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
f0f59a00
VS
1483 enum pipe pipe, i915_reg_t reg,
1484 u32 port_sel)
291906f1 1485{
47a05eca 1486 u32 val = I915_READ(reg);
e2c719b7 1487 I915_STATE_WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
291906f1 1488 "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
f0f59a00 1489 i915_mmio_reg_offset(reg), pipe_name(pipe));
de9a35ab 1490
2d1fe073 1491 I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && (val & DP_PORT_EN) == 0
75c5da27 1492 && (val & DP_PIPEB_SELECT),
de9a35ab 1493 "IBX PCH dp port still using transcoder B\n");
291906f1
JB
1494}
1495
1496static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
f0f59a00 1497 enum pipe pipe, i915_reg_t reg)
291906f1 1498{
47a05eca 1499 u32 val = I915_READ(reg);
e2c719b7 1500 I915_STATE_WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
23c99e77 1501 "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
f0f59a00 1502 i915_mmio_reg_offset(reg), pipe_name(pipe));
de9a35ab 1503
2d1fe073 1504 I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && (val & SDVO_ENABLE) == 0
75c5da27 1505 && (val & SDVO_PIPE_B_SELECT),
de9a35ab 1506 "IBX PCH hdmi port still using transcoder B\n");
291906f1
JB
1507}
1508
1509static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
1510 enum pipe pipe)
1511{
291906f1 1512 u32 val;
291906f1 1513
f0575e92
KP
1514 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
1515 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
1516 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
291906f1 1517
649636ef 1518 val = I915_READ(PCH_ADPA);
e2c719b7 1519 I915_STATE_WARN(adpa_pipe_enabled(dev_priv, pipe, val),
291906f1 1520 "PCH VGA enabled on transcoder %c, should be disabled\n",
9db4a9c7 1521 pipe_name(pipe));
291906f1 1522
649636ef 1523 val = I915_READ(PCH_LVDS);
e2c719b7 1524 I915_STATE_WARN(lvds_pipe_enabled(dev_priv, pipe, val),
291906f1 1525 "PCH LVDS enabled on transcoder %c, should be disabled\n",
9db4a9c7 1526 pipe_name(pipe));
291906f1 1527
e2debe91
PZ
1528 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
1529 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
1530 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
291906f1
JB
1531}
1532
d288f65f 1533static void vlv_enable_pll(struct intel_crtc *crtc,
5cec258b 1534 const struct intel_crtc_state *pipe_config)
87442f73 1535{
426115cf
DV
1536 struct drm_device *dev = crtc->base.dev;
1537 struct drm_i915_private *dev_priv = dev->dev_private;
8bd3f301
VS
1538 enum pipe pipe = crtc->pipe;
1539 i915_reg_t reg = DPLL(pipe);
d288f65f 1540 u32 dpll = pipe_config->dpll_hw_state.dpll;
87442f73 1541
8bd3f301 1542 assert_pipe_disabled(dev_priv, pipe);
87442f73 1543
87442f73 1544 /* PLL is protected by panel, make sure we can write it */
7d1a83cb 1545 assert_panel_unlocked(dev_priv, pipe);
87442f73 1546
426115cf
DV
1547 I915_WRITE(reg, dpll);
1548 POSTING_READ(reg);
1549 udelay(150);
1550
1551 if (wait_for(((I915_READ(reg) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
8bd3f301 1552 DRM_ERROR("DPLL %d failed to lock\n", pipe);
426115cf 1553
8bd3f301
VS
1554 I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
1555 POSTING_READ(DPLL_MD(pipe));
87442f73
DV
1556}
1557
d288f65f 1558static void chv_enable_pll(struct intel_crtc *crtc,
5cec258b 1559 const struct intel_crtc_state *pipe_config)
9d556c99
CML
1560{
1561 struct drm_device *dev = crtc->base.dev;
1562 struct drm_i915_private *dev_priv = dev->dev_private;
8bd3f301 1563 enum pipe pipe = crtc->pipe;
9d556c99 1564 enum dpio_channel port = vlv_pipe_to_channel(pipe);
9d556c99
CML
1565 u32 tmp;
1566
8bd3f301 1567 assert_pipe_disabled(dev_priv, pipe);
9d556c99 1568
7d1a83cb
VS
1569 /* PLL is protected by panel, make sure we can write it */
1570 assert_panel_unlocked(dev_priv, pipe);
1571
a580516d 1572 mutex_lock(&dev_priv->sb_lock);
9d556c99
CML
1573
1574 /* Enable back the 10bit clock to display controller */
1575 tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1576 tmp |= DPIO_DCLKP_EN;
1577 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
1578
54433e91
VS
1579 mutex_unlock(&dev_priv->sb_lock);
1580
9d556c99
CML
1581 /*
1582 * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
1583 */
1584 udelay(1);
1585
1586 /* Enable PLL */
d288f65f 1587 I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
9d556c99
CML
1588
1589 /* Check PLL is locked */
a11b0703 1590 if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
9d556c99
CML
1591 DRM_ERROR("PLL %d failed to lock\n", pipe);
1592
c231775c
VS
1593 if (pipe != PIPE_A) {
1594 /*
1595 * WaPixelRepeatModeFixForC0:chv
1596 *
1597 * DPLLCMD is AWOL. Use chicken bits to propagate
1598 * the value from DPLLBMD to either pipe B or C.
1599 */
1600 I915_WRITE(CBR4_VLV, pipe == PIPE_B ? CBR_DPLLBMD_PIPE_B : CBR_DPLLBMD_PIPE_C);
1601 I915_WRITE(DPLL_MD(PIPE_B), pipe_config->dpll_hw_state.dpll_md);
1602 I915_WRITE(CBR4_VLV, 0);
1603 dev_priv->chv_dpll_md[pipe] = pipe_config->dpll_hw_state.dpll_md;
1604
1605 /*
1606 * DPLLB VGA mode also seems to cause problems.
1607 * We should always have it disabled.
1608 */
1609 WARN_ON((I915_READ(DPLL(PIPE_B)) & DPLL_VGA_MODE_DIS) == 0);
1610 } else {
1611 I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
1612 POSTING_READ(DPLL_MD(pipe));
1613 }
9d556c99
CML
1614}
1615
1c4e0274
VS
1616static int intel_num_dvo_pipes(struct drm_device *dev)
1617{
1618 struct intel_crtc *crtc;
1619 int count = 0;
1620
1621 for_each_intel_crtc(dev, crtc)
3538b9df 1622 count += crtc->base.state->active &&
409ee761 1623 intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO);
1c4e0274
VS
1624
1625 return count;
1626}
1627
66e3d5c0 1628static void i9xx_enable_pll(struct intel_crtc *crtc)
63d7bbe9 1629{
66e3d5c0
DV
1630 struct drm_device *dev = crtc->base.dev;
1631 struct drm_i915_private *dev_priv = dev->dev_private;
f0f59a00 1632 i915_reg_t reg = DPLL(crtc->pipe);
6e3c9717 1633 u32 dpll = crtc->config->dpll_hw_state.dpll;
63d7bbe9 1634
66e3d5c0 1635 assert_pipe_disabled(dev_priv, crtc->pipe);
58c6eaa2 1636
63d7bbe9 1637 /* PLL is protected by panel, make sure we can write it */
66e3d5c0
DV
1638 if (IS_MOBILE(dev) && !IS_I830(dev))
1639 assert_panel_unlocked(dev_priv, crtc->pipe);
63d7bbe9 1640
1c4e0274
VS
1641 /* Enable DVO 2x clock on both PLLs if necessary */
1642 if (IS_I830(dev) && intel_num_dvo_pipes(dev) > 0) {
1643 /*
1644 * It appears to be important that we don't enable this
1645 * for the current pipe before otherwise configuring the
1646 * PLL. No idea how this should be handled if multiple
1647 * DVO outputs are enabled simultaneosly.
1648 */
1649 dpll |= DPLL_DVO_2X_MODE;
1650 I915_WRITE(DPLL(!crtc->pipe),
1651 I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
1652 }
66e3d5c0 1653
c2b63374
VS
1654 /*
1655 * Apparently we need to have VGA mode enabled prior to changing
1656 * the P1/P2 dividers. Otherwise the DPLL will keep using the old
1657 * dividers, even though the register value does change.
1658 */
1659 I915_WRITE(reg, 0);
1660
8e7a65aa
VS
1661 I915_WRITE(reg, dpll);
1662
66e3d5c0
DV
1663 /* Wait for the clocks to stabilize. */
1664 POSTING_READ(reg);
1665 udelay(150);
1666
1667 if (INTEL_INFO(dev)->gen >= 4) {
1668 I915_WRITE(DPLL_MD(crtc->pipe),
6e3c9717 1669 crtc->config->dpll_hw_state.dpll_md);
66e3d5c0
DV
1670 } else {
1671 /* The pixel multiplier can only be updated once the
1672 * DPLL is enabled and the clocks are stable.
1673 *
1674 * So write it again.
1675 */
1676 I915_WRITE(reg, dpll);
1677 }
63d7bbe9
JB
1678
1679 /* We do this three times for luck */
66e3d5c0 1680 I915_WRITE(reg, dpll);
63d7bbe9
JB
1681 POSTING_READ(reg);
1682 udelay(150); /* wait for warmup */
66e3d5c0 1683 I915_WRITE(reg, dpll);
63d7bbe9
JB
1684 POSTING_READ(reg);
1685 udelay(150); /* wait for warmup */
66e3d5c0 1686 I915_WRITE(reg, dpll);
63d7bbe9
JB
1687 POSTING_READ(reg);
1688 udelay(150); /* wait for warmup */
1689}
1690
1691/**
50b44a44 1692 * i9xx_disable_pll - disable a PLL
63d7bbe9
JB
1693 * @dev_priv: i915 private structure
1694 * @pipe: pipe PLL to disable
1695 *
1696 * Disable the PLL for @pipe, making sure the pipe is off first.
1697 *
1698 * Note! This is for pre-ILK only.
1699 */
1c4e0274 1700static void i9xx_disable_pll(struct intel_crtc *crtc)
63d7bbe9 1701{
1c4e0274
VS
1702 struct drm_device *dev = crtc->base.dev;
1703 struct drm_i915_private *dev_priv = dev->dev_private;
1704 enum pipe pipe = crtc->pipe;
1705
1706 /* Disable DVO 2x clock on both PLLs if necessary */
1707 if (IS_I830(dev) &&
409ee761 1708 intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO) &&
3538b9df 1709 !intel_num_dvo_pipes(dev)) {
1c4e0274
VS
1710 I915_WRITE(DPLL(PIPE_B),
1711 I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
1712 I915_WRITE(DPLL(PIPE_A),
1713 I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
1714 }
1715
b6b5d049
VS
1716 /* Don't disable pipe or pipe PLLs if needed */
1717 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1718 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
63d7bbe9
JB
1719 return;
1720
1721 /* Make sure the pipe isn't still relying on us */
1722 assert_pipe_disabled(dev_priv, pipe);
1723
b8afb911 1724 I915_WRITE(DPLL(pipe), DPLL_VGA_MODE_DIS);
50b44a44 1725 POSTING_READ(DPLL(pipe));
63d7bbe9
JB
1726}
1727
f6071166
JB
1728static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1729{
b8afb911 1730 u32 val;
f6071166
JB
1731
1732 /* Make sure the pipe isn't still relying on us */
1733 assert_pipe_disabled(dev_priv, pipe);
1734
03ed5cbf
VS
1735 val = DPLL_INTEGRATED_REF_CLK_VLV |
1736 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
1737 if (pipe != PIPE_A)
1738 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
1739
f6071166
JB
1740 I915_WRITE(DPLL(pipe), val);
1741 POSTING_READ(DPLL(pipe));
076ed3b2
CML
1742}
1743
1744static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1745{
d752048d 1746 enum dpio_channel port = vlv_pipe_to_channel(pipe);
076ed3b2
CML
1747 u32 val;
1748
a11b0703
VS
1749 /* Make sure the pipe isn't still relying on us */
1750 assert_pipe_disabled(dev_priv, pipe);
076ed3b2 1751
60bfe44f
VS
1752 val = DPLL_SSC_REF_CLK_CHV |
1753 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
a11b0703
VS
1754 if (pipe != PIPE_A)
1755 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
03ed5cbf 1756
a11b0703
VS
1757 I915_WRITE(DPLL(pipe), val);
1758 POSTING_READ(DPLL(pipe));
d752048d 1759
a580516d 1760 mutex_lock(&dev_priv->sb_lock);
d752048d
VS
1761
1762 /* Disable 10bit clock to display controller */
1763 val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1764 val &= ~DPIO_DCLKP_EN;
1765 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
1766
a580516d 1767 mutex_unlock(&dev_priv->sb_lock);
f6071166
JB
1768}
1769
e4607fcf 1770void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
9b6de0a1
VS
1771 struct intel_digital_port *dport,
1772 unsigned int expected_mask)
89b667f8
JB
1773{
1774 u32 port_mask;
f0f59a00 1775 i915_reg_t dpll_reg;
89b667f8 1776
e4607fcf
CML
1777 switch (dport->port) {
1778 case PORT_B:
89b667f8 1779 port_mask = DPLL_PORTB_READY_MASK;
00fc31b7 1780 dpll_reg = DPLL(0);
e4607fcf
CML
1781 break;
1782 case PORT_C:
89b667f8 1783 port_mask = DPLL_PORTC_READY_MASK;
00fc31b7 1784 dpll_reg = DPLL(0);
9b6de0a1 1785 expected_mask <<= 4;
00fc31b7
CML
1786 break;
1787 case PORT_D:
1788 port_mask = DPLL_PORTD_READY_MASK;
1789 dpll_reg = DPIO_PHY_STATUS;
e4607fcf
CML
1790 break;
1791 default:
1792 BUG();
1793 }
89b667f8 1794
9b6de0a1
VS
1795 if (wait_for((I915_READ(dpll_reg) & port_mask) == expected_mask, 1000))
1796 WARN(1, "timed out waiting for port %c ready: got 0x%x, expected 0x%x\n",
1797 port_name(dport->port), I915_READ(dpll_reg) & port_mask, expected_mask);
89b667f8
JB
1798}
1799
b8a4f404
PZ
1800static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1801 enum pipe pipe)
040484af 1802{
23670b32 1803 struct drm_device *dev = dev_priv->dev;
7c26e5c6 1804 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
e2b78267 1805 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
f0f59a00
VS
1806 i915_reg_t reg;
1807 uint32_t val, pipeconf_val;
040484af 1808
040484af 1809 /* Make sure PCH DPLL is enabled */
8106ddbd 1810 assert_shared_dpll_enabled(dev_priv, intel_crtc->config->shared_dpll);
040484af
JB
1811
1812 /* FDI must be feeding us bits for PCH ports */
1813 assert_fdi_tx_enabled(dev_priv, pipe);
1814 assert_fdi_rx_enabled(dev_priv, pipe);
1815
23670b32
DV
1816 if (HAS_PCH_CPT(dev)) {
1817 /* Workaround: Set the timing override bit before enabling the
1818 * pch transcoder. */
1819 reg = TRANS_CHICKEN2(pipe);
1820 val = I915_READ(reg);
1821 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
1822 I915_WRITE(reg, val);
59c859d6 1823 }
23670b32 1824
ab9412ba 1825 reg = PCH_TRANSCONF(pipe);
040484af 1826 val = I915_READ(reg);
5f7f726d 1827 pipeconf_val = I915_READ(PIPECONF(pipe));
e9bcff5c 1828
2d1fe073 1829 if (HAS_PCH_IBX(dev_priv)) {
e9bcff5c 1830 /*
c5de7c6f
VS
1831 * Make the BPC in transcoder be consistent with
1832 * that in pipeconf reg. For HDMI we must use 8bpc
1833 * here for both 8bpc and 12bpc.
e9bcff5c 1834 */
dfd07d72 1835 val &= ~PIPECONF_BPC_MASK;
c5de7c6f
VS
1836 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_HDMI))
1837 val |= PIPECONF_8BPC;
1838 else
1839 val |= pipeconf_val & PIPECONF_BPC_MASK;
e9bcff5c 1840 }
5f7f726d
PZ
1841
1842 val &= ~TRANS_INTERLACE_MASK;
1843 if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
2d1fe073 1844 if (HAS_PCH_IBX(dev_priv) &&
409ee761 1845 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
7c26e5c6
PZ
1846 val |= TRANS_LEGACY_INTERLACED_ILK;
1847 else
1848 val |= TRANS_INTERLACED;
5f7f726d
PZ
1849 else
1850 val |= TRANS_PROGRESSIVE;
1851
040484af
JB
1852 I915_WRITE(reg, val | TRANS_ENABLE);
1853 if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
4bb6f1f3 1854 DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
040484af
JB
1855}
1856
8fb033d7 1857static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
937bb610 1858 enum transcoder cpu_transcoder)
040484af 1859{
8fb033d7 1860 u32 val, pipeconf_val;
8fb033d7 1861
8fb033d7 1862 /* FDI must be feeding us bits for PCH ports */
1a240d4d 1863 assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
937bb610 1864 assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
8fb033d7 1865
223a6fdf 1866 /* Workaround: set timing override bit. */
36c0d0cf 1867 val = I915_READ(TRANS_CHICKEN2(PIPE_A));
23670b32 1868 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
36c0d0cf 1869 I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
223a6fdf 1870
25f3ef11 1871 val = TRANS_ENABLE;
937bb610 1872 pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
8fb033d7 1873
9a76b1c6
PZ
1874 if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
1875 PIPECONF_INTERLACED_ILK)
a35f2679 1876 val |= TRANS_INTERLACED;
8fb033d7
PZ
1877 else
1878 val |= TRANS_PROGRESSIVE;
1879
ab9412ba
DV
1880 I915_WRITE(LPT_TRANSCONF, val);
1881 if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
937bb610 1882 DRM_ERROR("Failed to enable PCH transcoder\n");
8fb033d7
PZ
1883}
1884
b8a4f404
PZ
1885static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
1886 enum pipe pipe)
040484af 1887{
23670b32 1888 struct drm_device *dev = dev_priv->dev;
f0f59a00
VS
1889 i915_reg_t reg;
1890 uint32_t val;
040484af
JB
1891
1892 /* FDI relies on the transcoder */
1893 assert_fdi_tx_disabled(dev_priv, pipe);
1894 assert_fdi_rx_disabled(dev_priv, pipe);
1895
291906f1
JB
1896 /* Ports must be off as well */
1897 assert_pch_ports_disabled(dev_priv, pipe);
1898
ab9412ba 1899 reg = PCH_TRANSCONF(pipe);
040484af
JB
1900 val = I915_READ(reg);
1901 val &= ~TRANS_ENABLE;
1902 I915_WRITE(reg, val);
1903 /* wait for PCH transcoder off, transcoder state */
1904 if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
4bb6f1f3 1905 DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
23670b32 1906
c465613b 1907 if (HAS_PCH_CPT(dev)) {
23670b32
DV
1908 /* Workaround: Clear the timing override chicken bit again. */
1909 reg = TRANS_CHICKEN2(pipe);
1910 val = I915_READ(reg);
1911 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
1912 I915_WRITE(reg, val);
1913 }
040484af
JB
1914}
1915
ab4d966c 1916static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
8fb033d7 1917{
8fb033d7
PZ
1918 u32 val;
1919
ab9412ba 1920 val = I915_READ(LPT_TRANSCONF);
8fb033d7 1921 val &= ~TRANS_ENABLE;
ab9412ba 1922 I915_WRITE(LPT_TRANSCONF, val);
8fb033d7 1923 /* wait for PCH transcoder off, transcoder state */
ab9412ba 1924 if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
8a52fd9f 1925 DRM_ERROR("Failed to disable PCH transcoder\n");
223a6fdf
PZ
1926
1927 /* Workaround: clear timing override bit. */
36c0d0cf 1928 val = I915_READ(TRANS_CHICKEN2(PIPE_A));
23670b32 1929 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
36c0d0cf 1930 I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
040484af
JB
1931}
1932
b24e7179 1933/**
309cfea8 1934 * intel_enable_pipe - enable a pipe, asserting requirements
0372264a 1935 * @crtc: crtc responsible for the pipe
b24e7179 1936 *
0372264a 1937 * Enable @crtc's pipe, making sure that various hardware specific requirements
b24e7179 1938 * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
b24e7179 1939 */
e1fdc473 1940static void intel_enable_pipe(struct intel_crtc *crtc)
b24e7179 1941{
0372264a
PZ
1942 struct drm_device *dev = crtc->base.dev;
1943 struct drm_i915_private *dev_priv = dev->dev_private;
1944 enum pipe pipe = crtc->pipe;
1a70a728 1945 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
1a240d4d 1946 enum pipe pch_transcoder;
f0f59a00 1947 i915_reg_t reg;
b24e7179
JB
1948 u32 val;
1949
9e2ee2dd
VS
1950 DRM_DEBUG_KMS("enabling pipe %c\n", pipe_name(pipe));
1951
58c6eaa2 1952 assert_planes_disabled(dev_priv, pipe);
93ce0ba6 1953 assert_cursor_disabled(dev_priv, pipe);
58c6eaa2
DV
1954 assert_sprites_disabled(dev_priv, pipe);
1955
2d1fe073 1956 if (HAS_PCH_LPT(dev_priv))
cc391bbb
PZ
1957 pch_transcoder = TRANSCODER_A;
1958 else
1959 pch_transcoder = pipe;
1960
b24e7179
JB
1961 /*
1962 * A pipe without a PLL won't actually be able to drive bits from
1963 * a plane. On ILK+ the pipe PLLs are integrated, so we don't
1964 * need the check.
1965 */
2d1fe073 1966 if (HAS_GMCH_DISPLAY(dev_priv))
a65347ba 1967 if (crtc->config->has_dsi_encoder)
23538ef1
JN
1968 assert_dsi_pll_enabled(dev_priv);
1969 else
1970 assert_pll_enabled(dev_priv, pipe);
040484af 1971 else {
6e3c9717 1972 if (crtc->config->has_pch_encoder) {
040484af 1973 /* if driving the PCH, we need FDI enabled */
cc391bbb 1974 assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
1a240d4d
DV
1975 assert_fdi_tx_pll_enabled(dev_priv,
1976 (enum pipe) cpu_transcoder);
040484af
JB
1977 }
1978 /* FIXME: assert CPU port conditions for SNB+ */
1979 }
b24e7179 1980
702e7a56 1981 reg = PIPECONF(cpu_transcoder);
b24e7179 1982 val = I915_READ(reg);
7ad25d48 1983 if (val & PIPECONF_ENABLE) {
b6b5d049
VS
1984 WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1985 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
00d70b15 1986 return;
7ad25d48 1987 }
00d70b15
CW
1988
1989 I915_WRITE(reg, val | PIPECONF_ENABLE);
851855d8 1990 POSTING_READ(reg);
b7792d8b
VS
1991
1992 /*
1993 * Until the pipe starts DSL will read as 0, which would cause
1994 * an apparent vblank timestamp jump, which messes up also the
1995 * frame count when it's derived from the timestamps. So let's
1996 * wait for the pipe to start properly before we call
1997 * drm_crtc_vblank_on()
1998 */
1999 if (dev->max_vblank_count == 0 &&
2000 wait_for(intel_get_crtc_scanline(crtc) != crtc->scanline_offset, 50))
2001 DRM_ERROR("pipe %c didn't start\n", pipe_name(pipe));
b24e7179
JB
2002}
2003
2004/**
309cfea8 2005 * intel_disable_pipe - disable a pipe, asserting requirements
575f7ab7 2006 * @crtc: crtc whose pipes is to be disabled
b24e7179 2007 *
575f7ab7
VS
2008 * Disable the pipe of @crtc, making sure that various hardware
2009 * specific requirements are met, if applicable, e.g. plane
2010 * disabled, panel fitter off, etc.
b24e7179
JB
2011 *
2012 * Will wait until the pipe has shut down before returning.
2013 */
575f7ab7 2014static void intel_disable_pipe(struct intel_crtc *crtc)
b24e7179 2015{
575f7ab7 2016 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
6e3c9717 2017 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
575f7ab7 2018 enum pipe pipe = crtc->pipe;
f0f59a00 2019 i915_reg_t reg;
b24e7179
JB
2020 u32 val;
2021
9e2ee2dd
VS
2022 DRM_DEBUG_KMS("disabling pipe %c\n", pipe_name(pipe));
2023
b24e7179
JB
2024 /*
2025 * Make sure planes won't keep trying to pump pixels to us,
2026 * or we might hang the display.
2027 */
2028 assert_planes_disabled(dev_priv, pipe);
93ce0ba6 2029 assert_cursor_disabled(dev_priv, pipe);
19332d7a 2030 assert_sprites_disabled(dev_priv, pipe);
b24e7179 2031
702e7a56 2032 reg = PIPECONF(cpu_transcoder);
b24e7179 2033 val = I915_READ(reg);
00d70b15
CW
2034 if ((val & PIPECONF_ENABLE) == 0)
2035 return;
2036
67adc644
VS
2037 /*
2038 * Double wide has implications for planes
2039 * so best keep it disabled when not needed.
2040 */
6e3c9717 2041 if (crtc->config->double_wide)
67adc644
VS
2042 val &= ~PIPECONF_DOUBLE_WIDE;
2043
2044 /* Don't disable pipe or pipe PLLs if needed */
b6b5d049
VS
2045 if (!(pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) &&
2046 !(pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
67adc644
VS
2047 val &= ~PIPECONF_ENABLE;
2048
2049 I915_WRITE(reg, val);
2050 if ((val & PIPECONF_ENABLE) == 0)
2051 intel_wait_for_pipe_off(crtc);
b24e7179
JB
2052}
2053
693db184
CW
2054static bool need_vtd_wa(struct drm_device *dev)
2055{
2056#ifdef CONFIG_INTEL_IOMMU
2057 if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
2058 return true;
2059#endif
2060 return false;
2061}
2062
832be82f
VS
2063static unsigned int intel_tile_size(const struct drm_i915_private *dev_priv)
2064{
2065 return IS_GEN2(dev_priv) ? 2048 : 4096;
2066}
2067
27ba3910
VS
2068static unsigned int intel_tile_width_bytes(const struct drm_i915_private *dev_priv,
2069 uint64_t fb_modifier, unsigned int cpp)
7b49f948
VS
2070{
2071 switch (fb_modifier) {
2072 case DRM_FORMAT_MOD_NONE:
2073 return cpp;
2074 case I915_FORMAT_MOD_X_TILED:
2075 if (IS_GEN2(dev_priv))
2076 return 128;
2077 else
2078 return 512;
2079 case I915_FORMAT_MOD_Y_TILED:
2080 if (IS_GEN2(dev_priv) || HAS_128_BYTE_Y_TILING(dev_priv))
2081 return 128;
2082 else
2083 return 512;
2084 case I915_FORMAT_MOD_Yf_TILED:
2085 switch (cpp) {
2086 case 1:
2087 return 64;
2088 case 2:
2089 case 4:
2090 return 128;
2091 case 8:
2092 case 16:
2093 return 256;
2094 default:
2095 MISSING_CASE(cpp);
2096 return cpp;
2097 }
2098 break;
2099 default:
2100 MISSING_CASE(fb_modifier);
2101 return cpp;
2102 }
2103}
2104
832be82f
VS
2105unsigned int intel_tile_height(const struct drm_i915_private *dev_priv,
2106 uint64_t fb_modifier, unsigned int cpp)
a57ce0b2 2107{
832be82f
VS
2108 if (fb_modifier == DRM_FORMAT_MOD_NONE)
2109 return 1;
2110 else
2111 return intel_tile_size(dev_priv) /
27ba3910 2112 intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
6761dd31
TU
2113}
2114
8d0deca8
VS
2115/* Return the tile dimensions in pixel units */
2116static void intel_tile_dims(const struct drm_i915_private *dev_priv,
2117 unsigned int *tile_width,
2118 unsigned int *tile_height,
2119 uint64_t fb_modifier,
2120 unsigned int cpp)
2121{
2122 unsigned int tile_width_bytes =
2123 intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
2124
2125 *tile_width = tile_width_bytes / cpp;
2126 *tile_height = intel_tile_size(dev_priv) / tile_width_bytes;
2127}
2128
6761dd31
TU
2129unsigned int
2130intel_fb_align_height(struct drm_device *dev, unsigned int height,
832be82f 2131 uint32_t pixel_format, uint64_t fb_modifier)
6761dd31 2132{
832be82f
VS
2133 unsigned int cpp = drm_format_plane_cpp(pixel_format, 0);
2134 unsigned int tile_height = intel_tile_height(to_i915(dev), fb_modifier, cpp);
2135
2136 return ALIGN(height, tile_height);
a57ce0b2
JB
2137}
2138
1663b9d6
VS
2139unsigned int intel_rotation_info_size(const struct intel_rotation_info *rot_info)
2140{
2141 unsigned int size = 0;
2142 int i;
2143
2144 for (i = 0 ; i < ARRAY_SIZE(rot_info->plane); i++)
2145 size += rot_info->plane[i].width * rot_info->plane[i].height;
2146
2147 return size;
2148}
2149
75c82a53 2150static void
3465c580
VS
2151intel_fill_fb_ggtt_view(struct i915_ggtt_view *view,
2152 const struct drm_framebuffer *fb,
2153 unsigned int rotation)
f64b98cd 2154{
2d7a215f
VS
2155 if (intel_rotation_90_or_270(rotation)) {
2156 *view = i915_ggtt_view_rotated;
2157 view->params.rotated = to_intel_framebuffer(fb)->rot_info;
2158 } else {
2159 *view = i915_ggtt_view_normal;
2160 }
2161}
50470bb0 2162
2d7a215f
VS
2163static void
2164intel_fill_fb_info(struct drm_i915_private *dev_priv,
2165 struct drm_framebuffer *fb)
2166{
2167 struct intel_rotation_info *info = &to_intel_framebuffer(fb)->rot_info;
2168 unsigned int tile_size, tile_width, tile_height, cpp;
50470bb0 2169
d9b3288e
VS
2170 tile_size = intel_tile_size(dev_priv);
2171
2172 cpp = drm_format_plane_cpp(fb->pixel_format, 0);
8d0deca8
VS
2173 intel_tile_dims(dev_priv, &tile_width, &tile_height,
2174 fb->modifier[0], cpp);
d9b3288e 2175
1663b9d6
VS
2176 info->plane[0].width = DIV_ROUND_UP(fb->pitches[0], tile_width * cpp);
2177 info->plane[0].height = DIV_ROUND_UP(fb->height, tile_height);
84fe03f7 2178
89e3e142 2179 if (info->pixel_format == DRM_FORMAT_NV12) {
832be82f 2180 cpp = drm_format_plane_cpp(fb->pixel_format, 1);
8d0deca8
VS
2181 intel_tile_dims(dev_priv, &tile_width, &tile_height,
2182 fb->modifier[1], cpp);
d9b3288e 2183
2d7a215f 2184 info->uv_offset = fb->offsets[1];
1663b9d6
VS
2185 info->plane[1].width = DIV_ROUND_UP(fb->pitches[1], tile_width * cpp);
2186 info->plane[1].height = DIV_ROUND_UP(fb->height / 2, tile_height);
89e3e142 2187 }
f64b98cd
TU
2188}
2189
603525d7 2190static unsigned int intel_linear_alignment(const struct drm_i915_private *dev_priv)
4e9a86b6
VS
2191{
2192 if (INTEL_INFO(dev_priv)->gen >= 9)
2193 return 256 * 1024;
985b8bb4 2194 else if (IS_BROADWATER(dev_priv) || IS_CRESTLINE(dev_priv) ||
666a4537 2195 IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
4e9a86b6
VS
2196 return 128 * 1024;
2197 else if (INTEL_INFO(dev_priv)->gen >= 4)
2198 return 4 * 1024;
2199 else
44c5905e 2200 return 0;
4e9a86b6
VS
2201}
2202
603525d7
VS
2203static unsigned int intel_surf_alignment(const struct drm_i915_private *dev_priv,
2204 uint64_t fb_modifier)
2205{
2206 switch (fb_modifier) {
2207 case DRM_FORMAT_MOD_NONE:
2208 return intel_linear_alignment(dev_priv);
2209 case I915_FORMAT_MOD_X_TILED:
2210 if (INTEL_INFO(dev_priv)->gen >= 9)
2211 return 256 * 1024;
2212 return 0;
2213 case I915_FORMAT_MOD_Y_TILED:
2214 case I915_FORMAT_MOD_Yf_TILED:
2215 return 1 * 1024 * 1024;
2216 default:
2217 MISSING_CASE(fb_modifier);
2218 return 0;
2219 }
2220}
2221
127bd2ac 2222int
3465c580
VS
2223intel_pin_and_fence_fb_obj(struct drm_framebuffer *fb,
2224 unsigned int rotation)
6b95a207 2225{
850c4cdc 2226 struct drm_device *dev = fb->dev;
ce453d81 2227 struct drm_i915_private *dev_priv = dev->dev_private;
850c4cdc 2228 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
f64b98cd 2229 struct i915_ggtt_view view;
6b95a207
KH
2230 u32 alignment;
2231 int ret;
2232
ebcdd39e
MR
2233 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
2234
603525d7 2235 alignment = intel_surf_alignment(dev_priv, fb->modifier[0]);
6b95a207 2236
3465c580 2237 intel_fill_fb_ggtt_view(&view, fb, rotation);
f64b98cd 2238
693db184
CW
2239 /* Note that the w/a also requires 64 PTE of padding following the
2240 * bo. We currently fill all unused PTE with the shadow page and so
2241 * we should always have valid PTE following the scanout preventing
2242 * the VT-d warning.
2243 */
2244 if (need_vtd_wa(dev) && alignment < 256 * 1024)
2245 alignment = 256 * 1024;
2246
d6dd6843
PZ
2247 /*
2248 * Global gtt pte registers are special registers which actually forward
2249 * writes to a chunk of system memory. Which means that there is no risk
2250 * that the register values disappear as soon as we call
2251 * intel_runtime_pm_put(), so it is correct to wrap only the
2252 * pin/unpin/fence and not more.
2253 */
2254 intel_runtime_pm_get(dev_priv);
2255
7580d774
ML
2256 ret = i915_gem_object_pin_to_display_plane(obj, alignment,
2257 &view);
48b956c5 2258 if (ret)
b26a6b35 2259 goto err_pm;
6b95a207
KH
2260
2261 /* Install a fence for tiled scan-out. Pre-i965 always needs a
2262 * fence, whereas 965+ only requires a fence if using
2263 * framebuffer compression. For simplicity, we always install
2264 * a fence as the cost is not that onerous.
2265 */
9807216f
VK
2266 if (view.type == I915_GGTT_VIEW_NORMAL) {
2267 ret = i915_gem_object_get_fence(obj);
2268 if (ret == -EDEADLK) {
2269 /*
2270 * -EDEADLK means there are no free fences
2271 * no pending flips.
2272 *
2273 * This is propagated to atomic, but it uses
2274 * -EDEADLK to force a locking recovery, so
2275 * change the returned error to -EBUSY.
2276 */
2277 ret = -EBUSY;
2278 goto err_unpin;
2279 } else if (ret)
2280 goto err_unpin;
1690e1eb 2281
9807216f
VK
2282 i915_gem_object_pin_fence(obj);
2283 }
6b95a207 2284
d6dd6843 2285 intel_runtime_pm_put(dev_priv);
6b95a207 2286 return 0;
48b956c5
CW
2287
2288err_unpin:
f64b98cd 2289 i915_gem_object_unpin_from_display_plane(obj, &view);
b26a6b35 2290err_pm:
d6dd6843 2291 intel_runtime_pm_put(dev_priv);
48b956c5 2292 return ret;
6b95a207
KH
2293}
2294
3465c580 2295static void intel_unpin_fb_obj(struct drm_framebuffer *fb, unsigned int rotation)
1690e1eb 2296{
82bc3b2d 2297 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
f64b98cd 2298 struct i915_ggtt_view view;
82bc3b2d 2299
ebcdd39e
MR
2300 WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
2301
3465c580 2302 intel_fill_fb_ggtt_view(&view, fb, rotation);
f64b98cd 2303
9807216f
VK
2304 if (view.type == I915_GGTT_VIEW_NORMAL)
2305 i915_gem_object_unpin_fence(obj);
2306
f64b98cd 2307 i915_gem_object_unpin_from_display_plane(obj, &view);
1690e1eb
CW
2308}
2309
29cf9491
VS
2310/*
2311 * Adjust the tile offset by moving the difference into
2312 * the x/y offsets.
2313 *
2314 * Input tile dimensions and pitch must already be
2315 * rotated to match x and y, and in pixel units.
2316 */
2317static u32 intel_adjust_tile_offset(int *x, int *y,
2318 unsigned int tile_width,
2319 unsigned int tile_height,
2320 unsigned int tile_size,
2321 unsigned int pitch_tiles,
2322 u32 old_offset,
2323 u32 new_offset)
2324{
2325 unsigned int tiles;
2326
2327 WARN_ON(old_offset & (tile_size - 1));
2328 WARN_ON(new_offset & (tile_size - 1));
2329 WARN_ON(new_offset > old_offset);
2330
2331 tiles = (old_offset - new_offset) / tile_size;
2332
2333 *y += tiles / pitch_tiles * tile_height;
2334 *x += tiles % pitch_tiles * tile_width;
2335
2336 return new_offset;
2337}
2338
8d0deca8
VS
2339/*
2340 * Computes the linear offset to the base tile and adjusts
2341 * x, y. bytes per pixel is assumed to be a power-of-two.
2342 *
2343 * In the 90/270 rotated case, x and y are assumed
2344 * to be already rotated to match the rotated GTT view, and
2345 * pitch is the tile_height aligned framebuffer height.
2346 */
4f2d9934
VS
2347u32 intel_compute_tile_offset(int *x, int *y,
2348 const struct drm_framebuffer *fb, int plane,
8d0deca8
VS
2349 unsigned int pitch,
2350 unsigned int rotation)
c2c75131 2351{
4f2d9934
VS
2352 const struct drm_i915_private *dev_priv = to_i915(fb->dev);
2353 uint64_t fb_modifier = fb->modifier[plane];
2354 unsigned int cpp = drm_format_plane_cpp(fb->pixel_format, plane);
29cf9491
VS
2355 u32 offset, offset_aligned, alignment;
2356
2357 alignment = intel_surf_alignment(dev_priv, fb_modifier);
2358 if (alignment)
2359 alignment--;
2360
b5c65338 2361 if (fb_modifier != DRM_FORMAT_MOD_NONE) {
8d0deca8
VS
2362 unsigned int tile_size, tile_width, tile_height;
2363 unsigned int tile_rows, tiles, pitch_tiles;
c2c75131 2364
d843310d 2365 tile_size = intel_tile_size(dev_priv);
8d0deca8
VS
2366 intel_tile_dims(dev_priv, &tile_width, &tile_height,
2367 fb_modifier, cpp);
2368
2369 if (intel_rotation_90_or_270(rotation)) {
2370 pitch_tiles = pitch / tile_height;
2371 swap(tile_width, tile_height);
2372 } else {
2373 pitch_tiles = pitch / (tile_width * cpp);
2374 }
d843310d
VS
2375
2376 tile_rows = *y / tile_height;
2377 *y %= tile_height;
c2c75131 2378
8d0deca8
VS
2379 tiles = *x / tile_width;
2380 *x %= tile_width;
bc752862 2381
29cf9491
VS
2382 offset = (tile_rows * pitch_tiles + tiles) * tile_size;
2383 offset_aligned = offset & ~alignment;
bc752862 2384
29cf9491
VS
2385 intel_adjust_tile_offset(x, y, tile_width, tile_height,
2386 tile_size, pitch_tiles,
2387 offset, offset_aligned);
2388 } else {
bc752862 2389 offset = *y * pitch + *x * cpp;
29cf9491
VS
2390 offset_aligned = offset & ~alignment;
2391
4e9a86b6
VS
2392 *y = (offset & alignment) / pitch;
2393 *x = ((offset & alignment) - *y * pitch) / cpp;
bc752862 2394 }
29cf9491
VS
2395
2396 return offset_aligned;
c2c75131
DV
2397}
2398
b35d63fa 2399static int i9xx_format_to_fourcc(int format)
46f297fb
JB
2400{
2401 switch (format) {
2402 case DISPPLANE_8BPP:
2403 return DRM_FORMAT_C8;
2404 case DISPPLANE_BGRX555:
2405 return DRM_FORMAT_XRGB1555;
2406 case DISPPLANE_BGRX565:
2407 return DRM_FORMAT_RGB565;
2408 default:
2409 case DISPPLANE_BGRX888:
2410 return DRM_FORMAT_XRGB8888;
2411 case DISPPLANE_RGBX888:
2412 return DRM_FORMAT_XBGR8888;
2413 case DISPPLANE_BGRX101010:
2414 return DRM_FORMAT_XRGB2101010;
2415 case DISPPLANE_RGBX101010:
2416 return DRM_FORMAT_XBGR2101010;
2417 }
2418}
2419
bc8d7dff
DL
2420static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
2421{
2422 switch (format) {
2423 case PLANE_CTL_FORMAT_RGB_565:
2424 return DRM_FORMAT_RGB565;
2425 default:
2426 case PLANE_CTL_FORMAT_XRGB_8888:
2427 if (rgb_order) {
2428 if (alpha)
2429 return DRM_FORMAT_ABGR8888;
2430 else
2431 return DRM_FORMAT_XBGR8888;
2432 } else {
2433 if (alpha)
2434 return DRM_FORMAT_ARGB8888;
2435 else
2436 return DRM_FORMAT_XRGB8888;
2437 }
2438 case PLANE_CTL_FORMAT_XRGB_2101010:
2439 if (rgb_order)
2440 return DRM_FORMAT_XBGR2101010;
2441 else
2442 return DRM_FORMAT_XRGB2101010;
2443 }
2444}
2445
5724dbd1 2446static bool
f6936e29
DV
2447intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
2448 struct intel_initial_plane_config *plane_config)
46f297fb
JB
2449{
2450 struct drm_device *dev = crtc->base.dev;
3badb49f 2451 struct drm_i915_private *dev_priv = to_i915(dev);
72e96d64 2452 struct i915_ggtt *ggtt = &dev_priv->ggtt;
46f297fb
JB
2453 struct drm_i915_gem_object *obj = NULL;
2454 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
2d14030b 2455 struct drm_framebuffer *fb = &plane_config->fb->base;
f37b5c2b
DV
2456 u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
2457 u32 size_aligned = round_up(plane_config->base + plane_config->size,
2458 PAGE_SIZE);
2459
2460 size_aligned -= base_aligned;
46f297fb 2461
ff2652ea
CW
2462 if (plane_config->size == 0)
2463 return false;
2464
3badb49f
PZ
2465 /* If the FB is too big, just don't use it since fbdev is not very
2466 * important and we should probably use that space with FBC or other
2467 * features. */
72e96d64 2468 if (size_aligned * 2 > ggtt->stolen_usable_size)
3badb49f
PZ
2469 return false;
2470
12c83d99
TU
2471 mutex_lock(&dev->struct_mutex);
2472
f37b5c2b
DV
2473 obj = i915_gem_object_create_stolen_for_preallocated(dev,
2474 base_aligned,
2475 base_aligned,
2476 size_aligned);
12c83d99
TU
2477 if (!obj) {
2478 mutex_unlock(&dev->struct_mutex);
484b41dd 2479 return false;
12c83d99 2480 }
46f297fb 2481
49af449b
DL
2482 obj->tiling_mode = plane_config->tiling;
2483 if (obj->tiling_mode == I915_TILING_X)
6bf129df 2484 obj->stride = fb->pitches[0];
46f297fb 2485
6bf129df
DL
2486 mode_cmd.pixel_format = fb->pixel_format;
2487 mode_cmd.width = fb->width;
2488 mode_cmd.height = fb->height;
2489 mode_cmd.pitches[0] = fb->pitches[0];
18c5247e
DV
2490 mode_cmd.modifier[0] = fb->modifier[0];
2491 mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
46f297fb 2492
6bf129df 2493 if (intel_framebuffer_init(dev, to_intel_framebuffer(fb),
484b41dd 2494 &mode_cmd, obj)) {
46f297fb
JB
2495 DRM_DEBUG_KMS("intel fb init failed\n");
2496 goto out_unref_obj;
2497 }
12c83d99 2498
46f297fb 2499 mutex_unlock(&dev->struct_mutex);
484b41dd 2500
f6936e29 2501 DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
484b41dd 2502 return true;
46f297fb
JB
2503
2504out_unref_obj:
2505 drm_gem_object_unreference(&obj->base);
2506 mutex_unlock(&dev->struct_mutex);
484b41dd
JB
2507 return false;
2508}
2509
afd65eb4
MR
2510/* Update plane->state->fb to match plane->fb after driver-internal updates */
2511static void
2512update_state_fb(struct drm_plane *plane)
2513{
2514 if (plane->fb == plane->state->fb)
2515 return;
2516
2517 if (plane->state->fb)
2518 drm_framebuffer_unreference(plane->state->fb);
2519 plane->state->fb = plane->fb;
2520 if (plane->state->fb)
2521 drm_framebuffer_reference(plane->state->fb);
2522}
2523
5724dbd1 2524static void
f6936e29
DV
2525intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
2526 struct intel_initial_plane_config *plane_config)
484b41dd
JB
2527{
2528 struct drm_device *dev = intel_crtc->base.dev;
d9ceb816 2529 struct drm_i915_private *dev_priv = dev->dev_private;
484b41dd
JB
2530 struct drm_crtc *c;
2531 struct intel_crtc *i;
2ff8fde1 2532 struct drm_i915_gem_object *obj;
88595ac9 2533 struct drm_plane *primary = intel_crtc->base.primary;
be5651f2 2534 struct drm_plane_state *plane_state = primary->state;
200757f5
MR
2535 struct drm_crtc_state *crtc_state = intel_crtc->base.state;
2536 struct intel_plane *intel_plane = to_intel_plane(primary);
0a8d8a86
MR
2537 struct intel_plane_state *intel_state =
2538 to_intel_plane_state(plane_state);
88595ac9 2539 struct drm_framebuffer *fb;
484b41dd 2540
2d14030b 2541 if (!plane_config->fb)
484b41dd
JB
2542 return;
2543
f6936e29 2544 if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
88595ac9
DV
2545 fb = &plane_config->fb->base;
2546 goto valid_fb;
f55548b5 2547 }
484b41dd 2548
2d14030b 2549 kfree(plane_config->fb);
484b41dd
JB
2550
2551 /*
2552 * Failed to alloc the obj, check to see if we should share
2553 * an fb with another CRTC instead
2554 */
70e1e0ec 2555 for_each_crtc(dev, c) {
484b41dd
JB
2556 i = to_intel_crtc(c);
2557
2558 if (c == &intel_crtc->base)
2559 continue;
2560
2ff8fde1
MR
2561 if (!i->active)
2562 continue;
2563
88595ac9
DV
2564 fb = c->primary->fb;
2565 if (!fb)
484b41dd
JB
2566 continue;
2567
88595ac9 2568 obj = intel_fb_obj(fb);
2ff8fde1 2569 if (i915_gem_obj_ggtt_offset(obj) == plane_config->base) {
88595ac9
DV
2570 drm_framebuffer_reference(fb);
2571 goto valid_fb;
484b41dd
JB
2572 }
2573 }
88595ac9 2574
200757f5
MR
2575 /*
2576 * We've failed to reconstruct the BIOS FB. Current display state
2577 * indicates that the primary plane is visible, but has a NULL FB,
2578 * which will lead to problems later if we don't fix it up. The
2579 * simplest solution is to just disable the primary plane now and
2580 * pretend the BIOS never had it enabled.
2581 */
2582 to_intel_plane_state(plane_state)->visible = false;
2583 crtc_state->plane_mask &= ~(1 << drm_plane_index(primary));
2622a081 2584 intel_pre_disable_primary_noatomic(&intel_crtc->base);
200757f5
MR
2585 intel_plane->disable_plane(primary, &intel_crtc->base);
2586
88595ac9
DV
2587 return;
2588
2589valid_fb:
f44e2659
VS
2590 plane_state->src_x = 0;
2591 plane_state->src_y = 0;
be5651f2
ML
2592 plane_state->src_w = fb->width << 16;
2593 plane_state->src_h = fb->height << 16;
2594
f44e2659
VS
2595 plane_state->crtc_x = 0;
2596 plane_state->crtc_y = 0;
be5651f2
ML
2597 plane_state->crtc_w = fb->width;
2598 plane_state->crtc_h = fb->height;
2599
0a8d8a86
MR
2600 intel_state->src.x1 = plane_state->src_x;
2601 intel_state->src.y1 = plane_state->src_y;
2602 intel_state->src.x2 = plane_state->src_x + plane_state->src_w;
2603 intel_state->src.y2 = plane_state->src_y + plane_state->src_h;
2604 intel_state->dst.x1 = plane_state->crtc_x;
2605 intel_state->dst.y1 = plane_state->crtc_y;
2606 intel_state->dst.x2 = plane_state->crtc_x + plane_state->crtc_w;
2607 intel_state->dst.y2 = plane_state->crtc_y + plane_state->crtc_h;
2608
88595ac9
DV
2609 obj = intel_fb_obj(fb);
2610 if (obj->tiling_mode != I915_TILING_NONE)
2611 dev_priv->preserve_bios_swizzle = true;
2612
be5651f2
ML
2613 drm_framebuffer_reference(fb);
2614 primary->fb = primary->state->fb = fb;
36750f28 2615 primary->crtc = primary->state->crtc = &intel_crtc->base;
36750f28 2616 intel_crtc->base.state->plane_mask |= (1 << drm_plane_index(primary));
a9ff8714 2617 obj->frontbuffer_bits |= to_intel_plane(primary)->frontbuffer_bit;
46f297fb
JB
2618}
2619
a8d201af
ML
2620static void i9xx_update_primary_plane(struct drm_plane *primary,
2621 const struct intel_crtc_state *crtc_state,
2622 const struct intel_plane_state *plane_state)
81255565 2623{
a8d201af 2624 struct drm_device *dev = primary->dev;
81255565 2625 struct drm_i915_private *dev_priv = dev->dev_private;
a8d201af
ML
2626 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
2627 struct drm_framebuffer *fb = plane_state->base.fb;
2628 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
81255565 2629 int plane = intel_crtc->plane;
54ea9da8 2630 u32 linear_offset;
81255565 2631 u32 dspcntr;
f0f59a00 2632 i915_reg_t reg = DSPCNTR(plane);
8d0deca8 2633 unsigned int rotation = plane_state->base.rotation;
ac484963 2634 int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
54ea9da8
VS
2635 int x = plane_state->src.x1 >> 16;
2636 int y = plane_state->src.y1 >> 16;
c9ba6fad 2637
f45651ba
VS
2638 dspcntr = DISPPLANE_GAMMA_ENABLE;
2639
fdd508a6 2640 dspcntr |= DISPLAY_PLANE_ENABLE;
f45651ba
VS
2641
2642 if (INTEL_INFO(dev)->gen < 4) {
2643 if (intel_crtc->pipe == PIPE_B)
2644 dspcntr |= DISPPLANE_SEL_PIPE_B;
2645
2646 /* pipesrc and dspsize control the size that is scaled from,
2647 * which should always be the user's requested size.
2648 */
2649 I915_WRITE(DSPSIZE(plane),
a8d201af
ML
2650 ((crtc_state->pipe_src_h - 1) << 16) |
2651 (crtc_state->pipe_src_w - 1));
f45651ba 2652 I915_WRITE(DSPPOS(plane), 0);
c14b0485
VS
2653 } else if (IS_CHERRYVIEW(dev) && plane == PLANE_B) {
2654 I915_WRITE(PRIMSIZE(plane),
a8d201af
ML
2655 ((crtc_state->pipe_src_h - 1) << 16) |
2656 (crtc_state->pipe_src_w - 1));
c14b0485
VS
2657 I915_WRITE(PRIMPOS(plane), 0);
2658 I915_WRITE(PRIMCNSTALPHA(plane), 0);
f45651ba 2659 }
81255565 2660
57779d06
VS
2661 switch (fb->pixel_format) {
2662 case DRM_FORMAT_C8:
81255565
JB
2663 dspcntr |= DISPPLANE_8BPP;
2664 break;
57779d06 2665 case DRM_FORMAT_XRGB1555:
57779d06 2666 dspcntr |= DISPPLANE_BGRX555;
81255565 2667 break;
57779d06
VS
2668 case DRM_FORMAT_RGB565:
2669 dspcntr |= DISPPLANE_BGRX565;
2670 break;
2671 case DRM_FORMAT_XRGB8888:
57779d06
VS
2672 dspcntr |= DISPPLANE_BGRX888;
2673 break;
2674 case DRM_FORMAT_XBGR8888:
57779d06
VS
2675 dspcntr |= DISPPLANE_RGBX888;
2676 break;
2677 case DRM_FORMAT_XRGB2101010:
57779d06
VS
2678 dspcntr |= DISPPLANE_BGRX101010;
2679 break;
2680 case DRM_FORMAT_XBGR2101010:
57779d06 2681 dspcntr |= DISPPLANE_RGBX101010;
81255565
JB
2682 break;
2683 default:
baba133a 2684 BUG();
81255565 2685 }
57779d06 2686
f45651ba
VS
2687 if (INTEL_INFO(dev)->gen >= 4 &&
2688 obj->tiling_mode != I915_TILING_NONE)
2689 dspcntr |= DISPPLANE_TILED;
81255565 2690
de1aa629
VS
2691 if (IS_G4X(dev))
2692 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
2693
ac484963 2694 linear_offset = y * fb->pitches[0] + x * cpp;
81255565 2695
c2c75131
DV
2696 if (INTEL_INFO(dev)->gen >= 4) {
2697 intel_crtc->dspaddr_offset =
4f2d9934 2698 intel_compute_tile_offset(&x, &y, fb, 0,
8d0deca8 2699 fb->pitches[0], rotation);
c2c75131
DV
2700 linear_offset -= intel_crtc->dspaddr_offset;
2701 } else {
e506a0c6 2702 intel_crtc->dspaddr_offset = linear_offset;
c2c75131 2703 }
e506a0c6 2704
8d0deca8 2705 if (rotation == BIT(DRM_ROTATE_180)) {
48404c1e
SJ
2706 dspcntr |= DISPPLANE_ROTATE_180;
2707
a8d201af
ML
2708 x += (crtc_state->pipe_src_w - 1);
2709 y += (crtc_state->pipe_src_h - 1);
48404c1e
SJ
2710
2711 /* Finding the last pixel of the last line of the display
2712 data and adding to linear_offset*/
2713 linear_offset +=
a8d201af 2714 (crtc_state->pipe_src_h - 1) * fb->pitches[0] +
ac484963 2715 (crtc_state->pipe_src_w - 1) * cpp;
48404c1e
SJ
2716 }
2717
2db3366b
PZ
2718 intel_crtc->adjusted_x = x;
2719 intel_crtc->adjusted_y = y;
2720
48404c1e
SJ
2721 I915_WRITE(reg, dspcntr);
2722
01f2c773 2723 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
a6c45cf0 2724 if (INTEL_INFO(dev)->gen >= 4) {
85ba7b7d
DV
2725 I915_WRITE(DSPSURF(plane),
2726 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
5eddb70b 2727 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
e506a0c6 2728 I915_WRITE(DSPLINOFF(plane), linear_offset);
5eddb70b 2729 } else
f343c5f6 2730 I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
5eddb70b 2731 POSTING_READ(reg);
17638cd6
JB
2732}
2733
a8d201af
ML
2734static void i9xx_disable_primary_plane(struct drm_plane *primary,
2735 struct drm_crtc *crtc)
17638cd6
JB
2736{
2737 struct drm_device *dev = crtc->dev;
2738 struct drm_i915_private *dev_priv = dev->dev_private;
2739 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
17638cd6 2740 int plane = intel_crtc->plane;
f45651ba 2741
a8d201af
ML
2742 I915_WRITE(DSPCNTR(plane), 0);
2743 if (INTEL_INFO(dev_priv)->gen >= 4)
fdd508a6 2744 I915_WRITE(DSPSURF(plane), 0);
a8d201af
ML
2745 else
2746 I915_WRITE(DSPADDR(plane), 0);
2747 POSTING_READ(DSPCNTR(plane));
2748}
c9ba6fad 2749
a8d201af
ML
2750static void ironlake_update_primary_plane(struct drm_plane *primary,
2751 const struct intel_crtc_state *crtc_state,
2752 const struct intel_plane_state *plane_state)
2753{
2754 struct drm_device *dev = primary->dev;
2755 struct drm_i915_private *dev_priv = dev->dev_private;
2756 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
2757 struct drm_framebuffer *fb = plane_state->base.fb;
2758 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
2759 int plane = intel_crtc->plane;
54ea9da8 2760 u32 linear_offset;
a8d201af
ML
2761 u32 dspcntr;
2762 i915_reg_t reg = DSPCNTR(plane);
8d0deca8 2763 unsigned int rotation = plane_state->base.rotation;
ac484963 2764 int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
a8d201af
ML
2765 int x = plane_state->src.x1 >> 16;
2766 int y = plane_state->src.y1 >> 16;
c9ba6fad 2767
f45651ba 2768 dspcntr = DISPPLANE_GAMMA_ENABLE;
fdd508a6 2769 dspcntr |= DISPLAY_PLANE_ENABLE;
f45651ba
VS
2770
2771 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
2772 dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
17638cd6 2773
57779d06
VS
2774 switch (fb->pixel_format) {
2775 case DRM_FORMAT_C8:
17638cd6
JB
2776 dspcntr |= DISPPLANE_8BPP;
2777 break;
57779d06
VS
2778 case DRM_FORMAT_RGB565:
2779 dspcntr |= DISPPLANE_BGRX565;
17638cd6 2780 break;
57779d06 2781 case DRM_FORMAT_XRGB8888:
57779d06
VS
2782 dspcntr |= DISPPLANE_BGRX888;
2783 break;
2784 case DRM_FORMAT_XBGR8888:
57779d06
VS
2785 dspcntr |= DISPPLANE_RGBX888;
2786 break;
2787 case DRM_FORMAT_XRGB2101010:
57779d06
VS
2788 dspcntr |= DISPPLANE_BGRX101010;
2789 break;
2790 case DRM_FORMAT_XBGR2101010:
57779d06 2791 dspcntr |= DISPPLANE_RGBX101010;
17638cd6
JB
2792 break;
2793 default:
baba133a 2794 BUG();
17638cd6
JB
2795 }
2796
2797 if (obj->tiling_mode != I915_TILING_NONE)
2798 dspcntr |= DISPPLANE_TILED;
17638cd6 2799
f45651ba 2800 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev))
1f5d76db 2801 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
17638cd6 2802
ac484963 2803 linear_offset = y * fb->pitches[0] + x * cpp;
c2c75131 2804 intel_crtc->dspaddr_offset =
4f2d9934 2805 intel_compute_tile_offset(&x, &y, fb, 0,
8d0deca8 2806 fb->pitches[0], rotation);
c2c75131 2807 linear_offset -= intel_crtc->dspaddr_offset;
8d0deca8 2808 if (rotation == BIT(DRM_ROTATE_180)) {
48404c1e
SJ
2809 dspcntr |= DISPPLANE_ROTATE_180;
2810
2811 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev)) {
a8d201af
ML
2812 x += (crtc_state->pipe_src_w - 1);
2813 y += (crtc_state->pipe_src_h - 1);
48404c1e
SJ
2814
2815 /* Finding the last pixel of the last line of the display
2816 data and adding to linear_offset*/
2817 linear_offset +=
a8d201af 2818 (crtc_state->pipe_src_h - 1) * fb->pitches[0] +
ac484963 2819 (crtc_state->pipe_src_w - 1) * cpp;
48404c1e
SJ
2820 }
2821 }
2822
2db3366b
PZ
2823 intel_crtc->adjusted_x = x;
2824 intel_crtc->adjusted_y = y;
2825
48404c1e 2826 I915_WRITE(reg, dspcntr);
17638cd6 2827
01f2c773 2828 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
85ba7b7d
DV
2829 I915_WRITE(DSPSURF(plane),
2830 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
b3dc685e 2831 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
bc1c91eb
DL
2832 I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
2833 } else {
2834 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2835 I915_WRITE(DSPLINOFF(plane), linear_offset);
2836 }
17638cd6 2837 POSTING_READ(reg);
17638cd6
JB
2838}
2839
7b49f948
VS
2840u32 intel_fb_stride_alignment(const struct drm_i915_private *dev_priv,
2841 uint64_t fb_modifier, uint32_t pixel_format)
b321803d 2842{
7b49f948 2843 if (fb_modifier == DRM_FORMAT_MOD_NONE) {
b321803d 2844 return 64;
7b49f948
VS
2845 } else {
2846 int cpp = drm_format_plane_cpp(pixel_format, 0);
2847
27ba3910 2848 return intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
b321803d
DL
2849 }
2850}
2851
44eb0cb9
MK
2852u32 intel_plane_obj_offset(struct intel_plane *intel_plane,
2853 struct drm_i915_gem_object *obj,
2854 unsigned int plane)
121920fa 2855{
ce7f1728 2856 struct i915_ggtt_view view;
dedf278c 2857 struct i915_vma *vma;
44eb0cb9 2858 u64 offset;
121920fa 2859
e7941294 2860 intel_fill_fb_ggtt_view(&view, intel_plane->base.state->fb,
3465c580 2861 intel_plane->base.state->rotation);
121920fa 2862
ce7f1728 2863 vma = i915_gem_obj_to_ggtt_view(obj, &view);
dedf278c 2864 if (WARN(!vma, "ggtt vma for display object not found! (view=%u)\n",
ce7f1728 2865 view.type))
dedf278c
TU
2866 return -1;
2867
44eb0cb9 2868 offset = vma->node.start;
dedf278c
TU
2869
2870 if (plane == 1) {
7723f47d 2871 offset += vma->ggtt_view.params.rotated.uv_start_page *
dedf278c
TU
2872 PAGE_SIZE;
2873 }
2874
44eb0cb9
MK
2875 WARN_ON(upper_32_bits(offset));
2876
2877 return lower_32_bits(offset);
121920fa
TU
2878}
2879
e435d6e5
ML
2880static void skl_detach_scaler(struct intel_crtc *intel_crtc, int id)
2881{
2882 struct drm_device *dev = intel_crtc->base.dev;
2883 struct drm_i915_private *dev_priv = dev->dev_private;
2884
2885 I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, id), 0);
2886 I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, id), 0);
2887 I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, id), 0);
e435d6e5
ML
2888}
2889
a1b2278e
CK
2890/*
2891 * This function detaches (aka. unbinds) unused scalers in hardware
2892 */
0583236e 2893static void skl_detach_scalers(struct intel_crtc *intel_crtc)
a1b2278e 2894{
a1b2278e
CK
2895 struct intel_crtc_scaler_state *scaler_state;
2896 int i;
2897
a1b2278e
CK
2898 scaler_state = &intel_crtc->config->scaler_state;
2899
2900 /* loop through and disable scalers that aren't in use */
2901 for (i = 0; i < intel_crtc->num_scalers; i++) {
e435d6e5
ML
2902 if (!scaler_state->scalers[i].in_use)
2903 skl_detach_scaler(intel_crtc, i);
a1b2278e
CK
2904 }
2905}
2906
6156a456 2907u32 skl_plane_ctl_format(uint32_t pixel_format)
70d21f0e 2908{
6156a456 2909 switch (pixel_format) {
d161cf7a 2910 case DRM_FORMAT_C8:
c34ce3d1 2911 return PLANE_CTL_FORMAT_INDEXED;
70d21f0e 2912 case DRM_FORMAT_RGB565:
c34ce3d1 2913 return PLANE_CTL_FORMAT_RGB_565;
70d21f0e 2914 case DRM_FORMAT_XBGR8888:
c34ce3d1 2915 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
6156a456 2916 case DRM_FORMAT_XRGB8888:
c34ce3d1 2917 return PLANE_CTL_FORMAT_XRGB_8888;
6156a456
CK
2918 /*
2919 * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
2920 * to be already pre-multiplied. We need to add a knob (or a different
2921 * DRM_FORMAT) for user-space to configure that.
2922 */
f75fb42a 2923 case DRM_FORMAT_ABGR8888:
c34ce3d1 2924 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX |
6156a456 2925 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
6156a456 2926 case DRM_FORMAT_ARGB8888:
c34ce3d1 2927 return PLANE_CTL_FORMAT_XRGB_8888 |
6156a456 2928 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
70d21f0e 2929 case DRM_FORMAT_XRGB2101010:
c34ce3d1 2930 return PLANE_CTL_FORMAT_XRGB_2101010;
70d21f0e 2931 case DRM_FORMAT_XBGR2101010:
c34ce3d1 2932 return PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
6156a456 2933 case DRM_FORMAT_YUYV:
c34ce3d1 2934 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
6156a456 2935 case DRM_FORMAT_YVYU:
c34ce3d1 2936 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
6156a456 2937 case DRM_FORMAT_UYVY:
c34ce3d1 2938 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
6156a456 2939 case DRM_FORMAT_VYUY:
c34ce3d1 2940 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
70d21f0e 2941 default:
4249eeef 2942 MISSING_CASE(pixel_format);
70d21f0e 2943 }
8cfcba41 2944
c34ce3d1 2945 return 0;
6156a456 2946}
70d21f0e 2947
6156a456
CK
2948u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
2949{
6156a456 2950 switch (fb_modifier) {
30af77c4 2951 case DRM_FORMAT_MOD_NONE:
70d21f0e 2952 break;
30af77c4 2953 case I915_FORMAT_MOD_X_TILED:
c34ce3d1 2954 return PLANE_CTL_TILED_X;
b321803d 2955 case I915_FORMAT_MOD_Y_TILED:
c34ce3d1 2956 return PLANE_CTL_TILED_Y;
b321803d 2957 case I915_FORMAT_MOD_Yf_TILED:
c34ce3d1 2958 return PLANE_CTL_TILED_YF;
70d21f0e 2959 default:
6156a456 2960 MISSING_CASE(fb_modifier);
70d21f0e 2961 }
8cfcba41 2962
c34ce3d1 2963 return 0;
6156a456 2964}
70d21f0e 2965
6156a456
CK
2966u32 skl_plane_ctl_rotation(unsigned int rotation)
2967{
3b7a5119 2968 switch (rotation) {
6156a456
CK
2969 case BIT(DRM_ROTATE_0):
2970 break;
1e8df167
SJ
2971 /*
2972 * DRM_ROTATE_ is counter clockwise to stay compatible with Xrandr
2973 * while i915 HW rotation is clockwise, thats why this swapping.
2974 */
3b7a5119 2975 case BIT(DRM_ROTATE_90):
1e8df167 2976 return PLANE_CTL_ROTATE_270;
3b7a5119 2977 case BIT(DRM_ROTATE_180):
c34ce3d1 2978 return PLANE_CTL_ROTATE_180;
3b7a5119 2979 case BIT(DRM_ROTATE_270):
1e8df167 2980 return PLANE_CTL_ROTATE_90;
6156a456
CK
2981 default:
2982 MISSING_CASE(rotation);
2983 }
2984
c34ce3d1 2985 return 0;
6156a456
CK
2986}
2987
a8d201af
ML
2988static void skylake_update_primary_plane(struct drm_plane *plane,
2989 const struct intel_crtc_state *crtc_state,
2990 const struct intel_plane_state *plane_state)
6156a456 2991{
a8d201af 2992 struct drm_device *dev = plane->dev;
6156a456 2993 struct drm_i915_private *dev_priv = dev->dev_private;
a8d201af
ML
2994 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
2995 struct drm_framebuffer *fb = plane_state->base.fb;
2996 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
6156a456
CK
2997 int pipe = intel_crtc->pipe;
2998 u32 plane_ctl, stride_div, stride;
2999 u32 tile_height, plane_offset, plane_size;
a8d201af 3000 unsigned int rotation = plane_state->base.rotation;
6156a456 3001 int x_offset, y_offset;
44eb0cb9 3002 u32 surf_addr;
a8d201af
ML
3003 int scaler_id = plane_state->scaler_id;
3004 int src_x = plane_state->src.x1 >> 16;
3005 int src_y = plane_state->src.y1 >> 16;
3006 int src_w = drm_rect_width(&plane_state->src) >> 16;
3007 int src_h = drm_rect_height(&plane_state->src) >> 16;
3008 int dst_x = plane_state->dst.x1;
3009 int dst_y = plane_state->dst.y1;
3010 int dst_w = drm_rect_width(&plane_state->dst);
3011 int dst_h = drm_rect_height(&plane_state->dst);
70d21f0e 3012
6156a456
CK
3013 plane_ctl = PLANE_CTL_ENABLE |
3014 PLANE_CTL_PIPE_GAMMA_ENABLE |
3015 PLANE_CTL_PIPE_CSC_ENABLE;
3016
3017 plane_ctl |= skl_plane_ctl_format(fb->pixel_format);
3018 plane_ctl |= skl_plane_ctl_tiling(fb->modifier[0]);
3019 plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
6156a456
CK
3020 plane_ctl |= skl_plane_ctl_rotation(rotation);
3021
7b49f948 3022 stride_div = intel_fb_stride_alignment(dev_priv, fb->modifier[0],
b321803d 3023 fb->pixel_format);
dedf278c 3024 surf_addr = intel_plane_obj_offset(to_intel_plane(plane), obj, 0);
3b7a5119 3025
a42e5a23
PZ
3026 WARN_ON(drm_rect_width(&plane_state->src) == 0);
3027
3b7a5119 3028 if (intel_rotation_90_or_270(rotation)) {
832be82f
VS
3029 int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
3030
3b7a5119 3031 /* stride = Surface height in tiles */
832be82f 3032 tile_height = intel_tile_height(dev_priv, fb->modifier[0], cpp);
3b7a5119 3033 stride = DIV_ROUND_UP(fb->height, tile_height);
a8d201af
ML
3034 x_offset = stride * tile_height - src_y - src_h;
3035 y_offset = src_x;
6156a456 3036 plane_size = (src_w - 1) << 16 | (src_h - 1);
3b7a5119
SJ
3037 } else {
3038 stride = fb->pitches[0] / stride_div;
a8d201af
ML
3039 x_offset = src_x;
3040 y_offset = src_y;
6156a456 3041 plane_size = (src_h - 1) << 16 | (src_w - 1);
3b7a5119
SJ
3042 }
3043 plane_offset = y_offset << 16 | x_offset;
b321803d 3044
2db3366b
PZ
3045 intel_crtc->adjusted_x = x_offset;
3046 intel_crtc->adjusted_y = y_offset;
3047
70d21f0e 3048 I915_WRITE(PLANE_CTL(pipe, 0), plane_ctl);
3b7a5119
SJ
3049 I915_WRITE(PLANE_OFFSET(pipe, 0), plane_offset);
3050 I915_WRITE(PLANE_SIZE(pipe, 0), plane_size);
3051 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
6156a456
CK
3052
3053 if (scaler_id >= 0) {
3054 uint32_t ps_ctrl = 0;
3055
3056 WARN_ON(!dst_w || !dst_h);
3057 ps_ctrl = PS_SCALER_EN | PS_PLANE_SEL(0) |
3058 crtc_state->scaler_state.scalers[scaler_id].mode;
3059 I915_WRITE(SKL_PS_CTRL(pipe, scaler_id), ps_ctrl);
3060 I915_WRITE(SKL_PS_PWR_GATE(pipe, scaler_id), 0);
3061 I915_WRITE(SKL_PS_WIN_POS(pipe, scaler_id), (dst_x << 16) | dst_y);
3062 I915_WRITE(SKL_PS_WIN_SZ(pipe, scaler_id), (dst_w << 16) | dst_h);
3063 I915_WRITE(PLANE_POS(pipe, 0), 0);
3064 } else {
3065 I915_WRITE(PLANE_POS(pipe, 0), (dst_y << 16) | dst_x);
3066 }
3067
121920fa 3068 I915_WRITE(PLANE_SURF(pipe, 0), surf_addr);
70d21f0e
DL
3069
3070 POSTING_READ(PLANE_SURF(pipe, 0));
3071}
3072
a8d201af
ML
3073static void skylake_disable_primary_plane(struct drm_plane *primary,
3074 struct drm_crtc *crtc)
17638cd6
JB
3075{
3076 struct drm_device *dev = crtc->dev;
3077 struct drm_i915_private *dev_priv = dev->dev_private;
a8d201af 3078 int pipe = to_intel_crtc(crtc)->pipe;
17638cd6 3079
a8d201af
ML
3080 I915_WRITE(PLANE_CTL(pipe, 0), 0);
3081 I915_WRITE(PLANE_SURF(pipe, 0), 0);
3082 POSTING_READ(PLANE_SURF(pipe, 0));
3083}
29b9bde6 3084
a8d201af
ML
3085/* Assume fb object is pinned & idle & fenced and just update base pointers */
3086static int
3087intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
3088 int x, int y, enum mode_set_atomic state)
3089{
3090 /* Support for kgdboc is disabled, this needs a major rework. */
3091 DRM_ERROR("legacy panic handler not supported any more.\n");
3092
3093 return -ENODEV;
81255565
JB
3094}
3095
7514747d 3096static void intel_complete_page_flips(struct drm_device *dev)
96a02917 3097{
96a02917
VS
3098 struct drm_crtc *crtc;
3099
70e1e0ec 3100 for_each_crtc(dev, crtc) {
96a02917
VS
3101 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3102 enum plane plane = intel_crtc->plane;
3103
3104 intel_prepare_page_flip(dev, plane);
3105 intel_finish_page_flip_plane(dev, plane);
3106 }
7514747d
VS
3107}
3108
3109static void intel_update_primary_planes(struct drm_device *dev)
3110{
7514747d 3111 struct drm_crtc *crtc;
96a02917 3112
70e1e0ec 3113 for_each_crtc(dev, crtc) {
11c22da6
ML
3114 struct intel_plane *plane = to_intel_plane(crtc->primary);
3115 struct intel_plane_state *plane_state;
96a02917 3116
11c22da6 3117 drm_modeset_lock_crtc(crtc, &plane->base);
11c22da6
ML
3118 plane_state = to_intel_plane_state(plane->base.state);
3119
a8d201af
ML
3120 if (plane_state->visible)
3121 plane->update_plane(&plane->base,
3122 to_intel_crtc_state(crtc->state),
3123 plane_state);
11c22da6
ML
3124
3125 drm_modeset_unlock_crtc(crtc);
96a02917
VS
3126 }
3127}
3128
7514747d
VS
3129void intel_prepare_reset(struct drm_device *dev)
3130{
3131 /* no reset support for gen2 */
3132 if (IS_GEN2(dev))
3133 return;
3134
3135 /* reset doesn't touch the display */
3136 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
3137 return;
3138
3139 drm_modeset_lock_all(dev);
f98ce92f
VS
3140 /*
3141 * Disabling the crtcs gracefully seems nicer. Also the
3142 * g33 docs say we should at least disable all the planes.
3143 */
6b72d486 3144 intel_display_suspend(dev);
7514747d
VS
3145}
3146
3147void intel_finish_reset(struct drm_device *dev)
3148{
3149 struct drm_i915_private *dev_priv = to_i915(dev);
3150
3151 /*
3152 * Flips in the rings will be nuked by the reset,
3153 * so complete all pending flips so that user space
3154 * will get its events and not get stuck.
3155 */
3156 intel_complete_page_flips(dev);
3157
3158 /* no reset support for gen2 */
3159 if (IS_GEN2(dev))
3160 return;
3161
3162 /* reset doesn't touch the display */
3163 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev)) {
3164 /*
3165 * Flips in the rings have been nuked by the reset,
3166 * so update the base address of all primary
3167 * planes to the the last fb to make sure we're
3168 * showing the correct fb after a reset.
11c22da6
ML
3169 *
3170 * FIXME: Atomic will make this obsolete since we won't schedule
3171 * CS-based flips (which might get lost in gpu resets) any more.
7514747d
VS
3172 */
3173 intel_update_primary_planes(dev);
3174 return;
3175 }
3176
3177 /*
3178 * The display has been reset as well,
3179 * so need a full re-initialization.
3180 */
3181 intel_runtime_pm_disable_interrupts(dev_priv);
3182 intel_runtime_pm_enable_interrupts(dev_priv);
3183
3184 intel_modeset_init_hw(dev);
3185
3186 spin_lock_irq(&dev_priv->irq_lock);
3187 if (dev_priv->display.hpd_irq_setup)
3188 dev_priv->display.hpd_irq_setup(dev);
3189 spin_unlock_irq(&dev_priv->irq_lock);
3190
043e9bda 3191 intel_display_resume(dev);
7514747d
VS
3192
3193 intel_hpd_init(dev_priv);
3194
3195 drm_modeset_unlock_all(dev);
3196}
3197
7d5e3799
CW
3198static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
3199{
3200 struct drm_device *dev = crtc->dev;
3201 struct drm_i915_private *dev_priv = dev->dev_private;
3202 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
c19ae989 3203 unsigned reset_counter;
7d5e3799
CW
3204 bool pending;
3205
c19ae989
CW
3206 reset_counter = i915_reset_counter(&dev_priv->gpu_error);
3207 if (intel_crtc->reset_counter != reset_counter ||
3208 __i915_reset_in_progress_or_wedged(reset_counter))
7d5e3799
CW
3209 return false;
3210
5e2d7afc 3211 spin_lock_irq(&dev->event_lock);
7d5e3799 3212 pending = to_intel_crtc(crtc)->unpin_work != NULL;
5e2d7afc 3213 spin_unlock_irq(&dev->event_lock);
7d5e3799
CW
3214
3215 return pending;
3216}
3217
bfd16b2a
ML
3218static void intel_update_pipe_config(struct intel_crtc *crtc,
3219 struct intel_crtc_state *old_crtc_state)
e30e8f75
GP
3220{
3221 struct drm_device *dev = crtc->base.dev;
3222 struct drm_i915_private *dev_priv = dev->dev_private;
bfd16b2a
ML
3223 struct intel_crtc_state *pipe_config =
3224 to_intel_crtc_state(crtc->base.state);
e30e8f75 3225
bfd16b2a
ML
3226 /* drm_atomic_helper_update_legacy_modeset_state might not be called. */
3227 crtc->base.mode = crtc->base.state->mode;
3228
3229 DRM_DEBUG_KMS("Updating pipe size %ix%i -> %ix%i\n",
3230 old_crtc_state->pipe_src_w, old_crtc_state->pipe_src_h,
3231 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
e30e8f75
GP
3232
3233 /*
3234 * Update pipe size and adjust fitter if needed: the reason for this is
3235 * that in compute_mode_changes we check the native mode (not the pfit
3236 * mode) to see if we can flip rather than do a full mode set. In the
3237 * fastboot case, we'll flip, but if we don't update the pipesrc and
3238 * pfit state, we'll end up with a big fb scanned out into the wrong
3239 * sized surface.
e30e8f75
GP
3240 */
3241
e30e8f75 3242 I915_WRITE(PIPESRC(crtc->pipe),
bfd16b2a
ML
3243 ((pipe_config->pipe_src_w - 1) << 16) |
3244 (pipe_config->pipe_src_h - 1));
3245
3246 /* on skylake this is done by detaching scalers */
3247 if (INTEL_INFO(dev)->gen >= 9) {
3248 skl_detach_scalers(crtc);
3249
3250 if (pipe_config->pch_pfit.enabled)
3251 skylake_pfit_enable(crtc);
3252 } else if (HAS_PCH_SPLIT(dev)) {
3253 if (pipe_config->pch_pfit.enabled)
3254 ironlake_pfit_enable(crtc);
3255 else if (old_crtc_state->pch_pfit.enabled)
3256 ironlake_pfit_disable(crtc, true);
e30e8f75 3257 }
e30e8f75
GP
3258}
3259
5e84e1a4
ZW
3260static void intel_fdi_normal_train(struct drm_crtc *crtc)
3261{
3262 struct drm_device *dev = crtc->dev;
3263 struct drm_i915_private *dev_priv = dev->dev_private;
3264 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3265 int pipe = intel_crtc->pipe;
f0f59a00
VS
3266 i915_reg_t reg;
3267 u32 temp;
5e84e1a4
ZW
3268
3269 /* enable normal train */
3270 reg = FDI_TX_CTL(pipe);
3271 temp = I915_READ(reg);
61e499bf 3272 if (IS_IVYBRIDGE(dev)) {
357555c0
JB
3273 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3274 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
61e499bf
KP
3275 } else {
3276 temp &= ~FDI_LINK_TRAIN_NONE;
3277 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
357555c0 3278 }
5e84e1a4
ZW
3279 I915_WRITE(reg, temp);
3280
3281 reg = FDI_RX_CTL(pipe);
3282 temp = I915_READ(reg);
3283 if (HAS_PCH_CPT(dev)) {
3284 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3285 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
3286 } else {
3287 temp &= ~FDI_LINK_TRAIN_NONE;
3288 temp |= FDI_LINK_TRAIN_NONE;
3289 }
3290 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
3291
3292 /* wait one idle pattern time */
3293 POSTING_READ(reg);
3294 udelay(1000);
357555c0
JB
3295
3296 /* IVB wants error correction enabled */
3297 if (IS_IVYBRIDGE(dev))
3298 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
3299 FDI_FE_ERRC_ENABLE);
5e84e1a4
ZW
3300}
3301
8db9d77b
ZW
3302/* The FDI link training functions for ILK/Ibexpeak. */
3303static void ironlake_fdi_link_train(struct drm_crtc *crtc)
3304{
3305 struct drm_device *dev = crtc->dev;
3306 struct drm_i915_private *dev_priv = dev->dev_private;
3307 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3308 int pipe = intel_crtc->pipe;
f0f59a00
VS
3309 i915_reg_t reg;
3310 u32 temp, tries;
8db9d77b 3311
1c8562f6 3312 /* FDI needs bits from pipe first */
0fc932b8 3313 assert_pipe_enabled(dev_priv, pipe);
0fc932b8 3314
e1a44743
AJ
3315 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3316 for train result */
5eddb70b
CW
3317 reg = FDI_RX_IMR(pipe);
3318 temp = I915_READ(reg);
e1a44743
AJ
3319 temp &= ~FDI_RX_SYMBOL_LOCK;
3320 temp &= ~FDI_RX_BIT_LOCK;
5eddb70b
CW
3321 I915_WRITE(reg, temp);
3322 I915_READ(reg);
e1a44743
AJ
3323 udelay(150);
3324
8db9d77b 3325 /* enable CPU FDI TX and PCH FDI RX */
5eddb70b
CW
3326 reg = FDI_TX_CTL(pipe);
3327 temp = I915_READ(reg);
627eb5a3 3328 temp &= ~FDI_DP_PORT_WIDTH_MASK;
6e3c9717 3329 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
8db9d77b
ZW
3330 temp &= ~FDI_LINK_TRAIN_NONE;
3331 temp |= FDI_LINK_TRAIN_PATTERN_1;
5eddb70b 3332 I915_WRITE(reg, temp | FDI_TX_ENABLE);
8db9d77b 3333
5eddb70b
CW
3334 reg = FDI_RX_CTL(pipe);
3335 temp = I915_READ(reg);
8db9d77b
ZW
3336 temp &= ~FDI_LINK_TRAIN_NONE;
3337 temp |= FDI_LINK_TRAIN_PATTERN_1;
5eddb70b
CW
3338 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3339
3340 POSTING_READ(reg);
8db9d77b
ZW
3341 udelay(150);
3342
5b2adf89 3343 /* Ironlake workaround, enable clock pointer after FDI enable*/
8f5718a6
DV
3344 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
3345 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
3346 FDI_RX_PHASE_SYNC_POINTER_EN);
5b2adf89 3347
5eddb70b 3348 reg = FDI_RX_IIR(pipe);
e1a44743 3349 for (tries = 0; tries < 5; tries++) {
5eddb70b 3350 temp = I915_READ(reg);
8db9d77b
ZW
3351 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3352
3353 if ((temp & FDI_RX_BIT_LOCK)) {
3354 DRM_DEBUG_KMS("FDI train 1 done.\n");
5eddb70b 3355 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
8db9d77b
ZW
3356 break;
3357 }
8db9d77b 3358 }
e1a44743 3359 if (tries == 5)
5eddb70b 3360 DRM_ERROR("FDI train 1 fail!\n");
8db9d77b
ZW
3361
3362 /* Train 2 */
5eddb70b
CW
3363 reg = FDI_TX_CTL(pipe);
3364 temp = I915_READ(reg);
8db9d77b
ZW
3365 temp &= ~FDI_LINK_TRAIN_NONE;
3366 temp |= FDI_LINK_TRAIN_PATTERN_2;
5eddb70b 3367 I915_WRITE(reg, temp);
8db9d77b 3368
5eddb70b
CW
3369 reg = FDI_RX_CTL(pipe);
3370 temp = I915_READ(reg);
8db9d77b
ZW
3371 temp &= ~FDI_LINK_TRAIN_NONE;
3372 temp |= FDI_LINK_TRAIN_PATTERN_2;
5eddb70b 3373 I915_WRITE(reg, temp);
8db9d77b 3374
5eddb70b
CW
3375 POSTING_READ(reg);
3376 udelay(150);
8db9d77b 3377
5eddb70b 3378 reg = FDI_RX_IIR(pipe);
e1a44743 3379 for (tries = 0; tries < 5; tries++) {
5eddb70b 3380 temp = I915_READ(reg);
8db9d77b
ZW
3381 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3382
3383 if (temp & FDI_RX_SYMBOL_LOCK) {
5eddb70b 3384 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
8db9d77b
ZW
3385 DRM_DEBUG_KMS("FDI train 2 done.\n");
3386 break;
3387 }
8db9d77b 3388 }
e1a44743 3389 if (tries == 5)
5eddb70b 3390 DRM_ERROR("FDI train 2 fail!\n");
8db9d77b
ZW
3391
3392 DRM_DEBUG_KMS("FDI train done\n");
5c5313c8 3393
8db9d77b
ZW
3394}
3395
0206e353 3396static const int snb_b_fdi_train_param[] = {
8db9d77b
ZW
3397 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
3398 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
3399 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
3400 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
3401};
3402
3403/* The FDI link training functions for SNB/Cougarpoint. */
3404static void gen6_fdi_link_train(struct drm_crtc *crtc)
3405{
3406 struct drm_device *dev = crtc->dev;
3407 struct drm_i915_private *dev_priv = dev->dev_private;
3408 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3409 int pipe = intel_crtc->pipe;
f0f59a00
VS
3410 i915_reg_t reg;
3411 u32 temp, i, retry;
8db9d77b 3412
e1a44743
AJ
3413 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3414 for train result */
5eddb70b
CW
3415 reg = FDI_RX_IMR(pipe);
3416 temp = I915_READ(reg);
e1a44743
AJ
3417 temp &= ~FDI_RX_SYMBOL_LOCK;
3418 temp &= ~FDI_RX_BIT_LOCK;
5eddb70b
CW
3419 I915_WRITE(reg, temp);
3420
3421 POSTING_READ(reg);
e1a44743
AJ
3422 udelay(150);
3423
8db9d77b 3424 /* enable CPU FDI TX and PCH FDI RX */
5eddb70b
CW
3425 reg = FDI_TX_CTL(pipe);
3426 temp = I915_READ(reg);
627eb5a3 3427 temp &= ~FDI_DP_PORT_WIDTH_MASK;
6e3c9717 3428 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
8db9d77b
ZW
3429 temp &= ~FDI_LINK_TRAIN_NONE;
3430 temp |= FDI_LINK_TRAIN_PATTERN_1;
3431 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3432 /* SNB-B */
3433 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
5eddb70b 3434 I915_WRITE(reg, temp | FDI_TX_ENABLE);
8db9d77b 3435
d74cf324
DV
3436 I915_WRITE(FDI_RX_MISC(pipe),
3437 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3438
5eddb70b
CW
3439 reg = FDI_RX_CTL(pipe);
3440 temp = I915_READ(reg);
8db9d77b
ZW
3441 if (HAS_PCH_CPT(dev)) {
3442 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3443 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3444 } else {
3445 temp &= ~FDI_LINK_TRAIN_NONE;
3446 temp |= FDI_LINK_TRAIN_PATTERN_1;
3447 }
5eddb70b
CW
3448 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3449
3450 POSTING_READ(reg);
8db9d77b
ZW
3451 udelay(150);
3452
0206e353 3453 for (i = 0; i < 4; i++) {
5eddb70b
CW
3454 reg = FDI_TX_CTL(pipe);
3455 temp = I915_READ(reg);
8db9d77b
ZW
3456 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3457 temp |= snb_b_fdi_train_param[i];
5eddb70b
CW
3458 I915_WRITE(reg, temp);
3459
3460 POSTING_READ(reg);
8db9d77b
ZW
3461 udelay(500);
3462
fa37d39e
SP
3463 for (retry = 0; retry < 5; retry++) {
3464 reg = FDI_RX_IIR(pipe);
3465 temp = I915_READ(reg);
3466 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3467 if (temp & FDI_RX_BIT_LOCK) {
3468 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3469 DRM_DEBUG_KMS("FDI train 1 done.\n");
3470 break;
3471 }
3472 udelay(50);
8db9d77b 3473 }
fa37d39e
SP
3474 if (retry < 5)
3475 break;
8db9d77b
ZW
3476 }
3477 if (i == 4)
5eddb70b 3478 DRM_ERROR("FDI train 1 fail!\n");
8db9d77b
ZW
3479
3480 /* Train 2 */
5eddb70b
CW
3481 reg = FDI_TX_CTL(pipe);
3482 temp = I915_READ(reg);
8db9d77b
ZW
3483 temp &= ~FDI_LINK_TRAIN_NONE;
3484 temp |= FDI_LINK_TRAIN_PATTERN_2;
3485 if (IS_GEN6(dev)) {
3486 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3487 /* SNB-B */
3488 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
3489 }
5eddb70b 3490 I915_WRITE(reg, temp);
8db9d77b 3491
5eddb70b
CW
3492 reg = FDI_RX_CTL(pipe);
3493 temp = I915_READ(reg);
8db9d77b
ZW
3494 if (HAS_PCH_CPT(dev)) {
3495 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3496 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
3497 } else {
3498 temp &= ~FDI_LINK_TRAIN_NONE;
3499 temp |= FDI_LINK_TRAIN_PATTERN_2;
3500 }
5eddb70b
CW
3501 I915_WRITE(reg, temp);
3502
3503 POSTING_READ(reg);
8db9d77b
ZW
3504 udelay(150);
3505
0206e353 3506 for (i = 0; i < 4; i++) {
5eddb70b
CW
3507 reg = FDI_TX_CTL(pipe);
3508 temp = I915_READ(reg);
8db9d77b
ZW
3509 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3510 temp |= snb_b_fdi_train_param[i];
5eddb70b
CW
3511 I915_WRITE(reg, temp);
3512
3513 POSTING_READ(reg);
8db9d77b
ZW
3514 udelay(500);
3515
fa37d39e
SP
3516 for (retry = 0; retry < 5; retry++) {
3517 reg = FDI_RX_IIR(pipe);
3518 temp = I915_READ(reg);
3519 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3520 if (temp & FDI_RX_SYMBOL_LOCK) {
3521 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3522 DRM_DEBUG_KMS("FDI train 2 done.\n");
3523 break;
3524 }
3525 udelay(50);
8db9d77b 3526 }
fa37d39e
SP
3527 if (retry < 5)
3528 break;
8db9d77b
ZW
3529 }
3530 if (i == 4)
5eddb70b 3531 DRM_ERROR("FDI train 2 fail!\n");
8db9d77b
ZW
3532
3533 DRM_DEBUG_KMS("FDI train done.\n");
3534}
3535
357555c0
JB
3536/* Manual link training for Ivy Bridge A0 parts */
3537static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
3538{
3539 struct drm_device *dev = crtc->dev;
3540 struct drm_i915_private *dev_priv = dev->dev_private;
3541 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3542 int pipe = intel_crtc->pipe;
f0f59a00
VS
3543 i915_reg_t reg;
3544 u32 temp, i, j;
357555c0
JB
3545
3546 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3547 for train result */
3548 reg = FDI_RX_IMR(pipe);
3549 temp = I915_READ(reg);
3550 temp &= ~FDI_RX_SYMBOL_LOCK;
3551 temp &= ~FDI_RX_BIT_LOCK;
3552 I915_WRITE(reg, temp);
3553
3554 POSTING_READ(reg);
3555 udelay(150);
3556
01a415fd
DV
3557 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
3558 I915_READ(FDI_RX_IIR(pipe)));
3559
139ccd3f
JB
3560 /* Try each vswing and preemphasis setting twice before moving on */
3561 for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
3562 /* disable first in case we need to retry */
3563 reg = FDI_TX_CTL(pipe);
3564 temp = I915_READ(reg);
3565 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
3566 temp &= ~FDI_TX_ENABLE;
3567 I915_WRITE(reg, temp);
357555c0 3568
139ccd3f
JB
3569 reg = FDI_RX_CTL(pipe);
3570 temp = I915_READ(reg);
3571 temp &= ~FDI_LINK_TRAIN_AUTO;
3572 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3573 temp &= ~FDI_RX_ENABLE;
3574 I915_WRITE(reg, temp);
357555c0 3575
139ccd3f 3576 /* enable CPU FDI TX and PCH FDI RX */
357555c0
JB
3577 reg = FDI_TX_CTL(pipe);
3578 temp = I915_READ(reg);
139ccd3f 3579 temp &= ~FDI_DP_PORT_WIDTH_MASK;
6e3c9717 3580 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
139ccd3f 3581 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
357555c0 3582 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
139ccd3f
JB
3583 temp |= snb_b_fdi_train_param[j/2];
3584 temp |= FDI_COMPOSITE_SYNC;
3585 I915_WRITE(reg, temp | FDI_TX_ENABLE);
357555c0 3586
139ccd3f
JB
3587 I915_WRITE(FDI_RX_MISC(pipe),
3588 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
357555c0 3589
139ccd3f 3590 reg = FDI_RX_CTL(pipe);
357555c0 3591 temp = I915_READ(reg);
139ccd3f
JB
3592 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3593 temp |= FDI_COMPOSITE_SYNC;
3594 I915_WRITE(reg, temp | FDI_RX_ENABLE);
357555c0 3595
139ccd3f
JB
3596 POSTING_READ(reg);
3597 udelay(1); /* should be 0.5us */
357555c0 3598
139ccd3f
JB
3599 for (i = 0; i < 4; i++) {
3600 reg = FDI_RX_IIR(pipe);
3601 temp = I915_READ(reg);
3602 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
357555c0 3603
139ccd3f
JB
3604 if (temp & FDI_RX_BIT_LOCK ||
3605 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
3606 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3607 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
3608 i);
3609 break;
3610 }
3611 udelay(1); /* should be 0.5us */
3612 }
3613 if (i == 4) {
3614 DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
3615 continue;
3616 }
357555c0 3617
139ccd3f 3618 /* Train 2 */
357555c0
JB
3619 reg = FDI_TX_CTL(pipe);
3620 temp = I915_READ(reg);
139ccd3f
JB
3621 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3622 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
3623 I915_WRITE(reg, temp);
3624
3625 reg = FDI_RX_CTL(pipe);
3626 temp = I915_READ(reg);
3627 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3628 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
357555c0
JB
3629 I915_WRITE(reg, temp);
3630
3631 POSTING_READ(reg);
139ccd3f 3632 udelay(2); /* should be 1.5us */
357555c0 3633
139ccd3f
JB
3634 for (i = 0; i < 4; i++) {
3635 reg = FDI_RX_IIR(pipe);
3636 temp = I915_READ(reg);
3637 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
357555c0 3638
139ccd3f
JB
3639 if (temp & FDI_RX_SYMBOL_LOCK ||
3640 (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
3641 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3642 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
3643 i);
3644 goto train_done;
3645 }
3646 udelay(2); /* should be 1.5us */
357555c0 3647 }
139ccd3f
JB
3648 if (i == 4)
3649 DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
357555c0 3650 }
357555c0 3651
139ccd3f 3652train_done:
357555c0
JB
3653 DRM_DEBUG_KMS("FDI train done.\n");
3654}
3655
88cefb6c 3656static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
2c07245f 3657{
88cefb6c 3658 struct drm_device *dev = intel_crtc->base.dev;
2c07245f 3659 struct drm_i915_private *dev_priv = dev->dev_private;
2c07245f 3660 int pipe = intel_crtc->pipe;
f0f59a00
VS
3661 i915_reg_t reg;
3662 u32 temp;
c64e311e 3663
c98e9dcf 3664 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
5eddb70b
CW
3665 reg = FDI_RX_CTL(pipe);
3666 temp = I915_READ(reg);
627eb5a3 3667 temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
6e3c9717 3668 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
dfd07d72 3669 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
5eddb70b
CW
3670 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
3671
3672 POSTING_READ(reg);
c98e9dcf
JB
3673 udelay(200);
3674
3675 /* Switch from Rawclk to PCDclk */
5eddb70b
CW
3676 temp = I915_READ(reg);
3677 I915_WRITE(reg, temp | FDI_PCDCLK);
3678
3679 POSTING_READ(reg);
c98e9dcf
JB
3680 udelay(200);
3681
20749730
PZ
3682 /* Enable CPU FDI TX PLL, always on for Ironlake */
3683 reg = FDI_TX_CTL(pipe);
3684 temp = I915_READ(reg);
3685 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
3686 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
5eddb70b 3687
20749730
PZ
3688 POSTING_READ(reg);
3689 udelay(100);
6be4a607 3690 }
0e23b99d
JB
3691}
3692
88cefb6c
DV
3693static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
3694{
3695 struct drm_device *dev = intel_crtc->base.dev;
3696 struct drm_i915_private *dev_priv = dev->dev_private;
3697 int pipe = intel_crtc->pipe;
f0f59a00
VS
3698 i915_reg_t reg;
3699 u32 temp;
88cefb6c
DV
3700
3701 /* Switch from PCDclk to Rawclk */
3702 reg = FDI_RX_CTL(pipe);
3703 temp = I915_READ(reg);
3704 I915_WRITE(reg, temp & ~FDI_PCDCLK);
3705
3706 /* Disable CPU FDI TX PLL */
3707 reg = FDI_TX_CTL(pipe);
3708 temp = I915_READ(reg);
3709 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
3710
3711 POSTING_READ(reg);
3712 udelay(100);
3713
3714 reg = FDI_RX_CTL(pipe);
3715 temp = I915_READ(reg);
3716 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
3717
3718 /* Wait for the clocks to turn off. */
3719 POSTING_READ(reg);
3720 udelay(100);
3721}
3722
0fc932b8
JB
3723static void ironlake_fdi_disable(struct drm_crtc *crtc)
3724{
3725 struct drm_device *dev = crtc->dev;
3726 struct drm_i915_private *dev_priv = dev->dev_private;
3727 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3728 int pipe = intel_crtc->pipe;
f0f59a00
VS
3729 i915_reg_t reg;
3730 u32 temp;
0fc932b8
JB
3731
3732 /* disable CPU FDI tx and PCH FDI rx */
3733 reg = FDI_TX_CTL(pipe);
3734 temp = I915_READ(reg);
3735 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
3736 POSTING_READ(reg);
3737
3738 reg = FDI_RX_CTL(pipe);
3739 temp = I915_READ(reg);
3740 temp &= ~(0x7 << 16);
dfd07d72 3741 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
0fc932b8
JB
3742 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
3743
3744 POSTING_READ(reg);
3745 udelay(100);
3746
3747 /* Ironlake workaround, disable clock pointer after downing FDI */
eba905b2 3748 if (HAS_PCH_IBX(dev))
6f06ce18 3749 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
0fc932b8
JB
3750
3751 /* still set train pattern 1 */
3752 reg = FDI_TX_CTL(pipe);
3753 temp = I915_READ(reg);
3754 temp &= ~FDI_LINK_TRAIN_NONE;
3755 temp |= FDI_LINK_TRAIN_PATTERN_1;
3756 I915_WRITE(reg, temp);
3757
3758 reg = FDI_RX_CTL(pipe);
3759 temp = I915_READ(reg);
3760 if (HAS_PCH_CPT(dev)) {
3761 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3762 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3763 } else {
3764 temp &= ~FDI_LINK_TRAIN_NONE;
3765 temp |= FDI_LINK_TRAIN_PATTERN_1;
3766 }
3767 /* BPC in FDI rx is consistent with that in PIPECONF */
3768 temp &= ~(0x07 << 16);
dfd07d72 3769 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
0fc932b8
JB
3770 I915_WRITE(reg, temp);
3771
3772 POSTING_READ(reg);
3773 udelay(100);
3774}
3775
5dce5b93
CW
3776bool intel_has_pending_fb_unpin(struct drm_device *dev)
3777{
3778 struct intel_crtc *crtc;
3779
3780 /* Note that we don't need to be called with mode_config.lock here
3781 * as our list of CRTC objects is static for the lifetime of the
3782 * device and so cannot disappear as we iterate. Similarly, we can
3783 * happily treat the predicates as racy, atomic checks as userspace
3784 * cannot claim and pin a new fb without at least acquring the
3785 * struct_mutex and so serialising with us.
3786 */
d3fcc808 3787 for_each_intel_crtc(dev, crtc) {
5dce5b93
CW
3788 if (atomic_read(&crtc->unpin_work_count) == 0)
3789 continue;
3790
3791 if (crtc->unpin_work)
3792 intel_wait_for_vblank(dev, crtc->pipe);
3793
3794 return true;
3795 }
3796
3797 return false;
3798}
3799
d6bbafa1
CW
3800static void page_flip_completed(struct intel_crtc *intel_crtc)
3801{
3802 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
3803 struct intel_unpin_work *work = intel_crtc->unpin_work;
3804
3805 /* ensure that the unpin work is consistent wrt ->pending. */
3806 smp_rmb();
3807 intel_crtc->unpin_work = NULL;
3808
3809 if (work->event)
3810 drm_send_vblank_event(intel_crtc->base.dev,
3811 intel_crtc->pipe,
3812 work->event);
3813
3814 drm_crtc_vblank_put(&intel_crtc->base);
3815
3816 wake_up_all(&dev_priv->pending_flip_queue);
3817 queue_work(dev_priv->wq, &work->work);
3818
3819 trace_i915_flip_complete(intel_crtc->plane,
3820 work->pending_flip_obj);
3821}
3822
5008e874 3823static int intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
e6c3a2a6 3824{
0f91128d 3825 struct drm_device *dev = crtc->dev;
5bb61643 3826 struct drm_i915_private *dev_priv = dev->dev_private;
5008e874 3827 long ret;
e6c3a2a6 3828
2c10d571 3829 WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
5008e874
ML
3830
3831 ret = wait_event_interruptible_timeout(
3832 dev_priv->pending_flip_queue,
3833 !intel_crtc_has_pending_flip(crtc),
3834 60*HZ);
3835
3836 if (ret < 0)
3837 return ret;
3838
3839 if (ret == 0) {
9c787942 3840 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2c10d571 3841
5e2d7afc 3842 spin_lock_irq(&dev->event_lock);
9c787942
CW
3843 if (intel_crtc->unpin_work) {
3844 WARN_ONCE(1, "Removing stuck page flip\n");
3845 page_flip_completed(intel_crtc);
3846 }
5e2d7afc 3847 spin_unlock_irq(&dev->event_lock);
9c787942 3848 }
5bb61643 3849
5008e874 3850 return 0;
e6c3a2a6
CW
3851}
3852
060f02d8
VS
3853static void lpt_disable_iclkip(struct drm_i915_private *dev_priv)
3854{
3855 u32 temp;
3856
3857 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
3858
3859 mutex_lock(&dev_priv->sb_lock);
3860
3861 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
3862 temp |= SBI_SSCCTL_DISABLE;
3863 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
3864
3865 mutex_unlock(&dev_priv->sb_lock);
3866}
3867
e615efe4
ED
3868/* Program iCLKIP clock to the desired frequency */
3869static void lpt_program_iclkip(struct drm_crtc *crtc)
3870{
64b46a06 3871 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
6e3c9717 3872 int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
e615efe4
ED
3873 u32 divsel, phaseinc, auxdiv, phasedir = 0;
3874 u32 temp;
3875
060f02d8 3876 lpt_disable_iclkip(dev_priv);
e615efe4 3877
64b46a06
VS
3878 /* The iCLK virtual clock root frequency is in MHz,
3879 * but the adjusted_mode->crtc_clock in in KHz. To get the
3880 * divisors, it is necessary to divide one by another, so we
3881 * convert the virtual clock precision to KHz here for higher
3882 * precision.
3883 */
3884 for (auxdiv = 0; auxdiv < 2; auxdiv++) {
e615efe4
ED
3885 u32 iclk_virtual_root_freq = 172800 * 1000;
3886 u32 iclk_pi_range = 64;
64b46a06 3887 u32 desired_divisor;
e615efe4 3888
64b46a06
VS
3889 desired_divisor = DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
3890 clock << auxdiv);
3891 divsel = (desired_divisor / iclk_pi_range) - 2;
3892 phaseinc = desired_divisor % iclk_pi_range;
e615efe4 3893
64b46a06
VS
3894 /*
3895 * Near 20MHz is a corner case which is
3896 * out of range for the 7-bit divisor
3897 */
3898 if (divsel <= 0x7f)
3899 break;
e615efe4
ED
3900 }
3901
3902 /* This should not happen with any sane values */
3903 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
3904 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
3905 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
3906 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
3907
3908 DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
12d7ceed 3909 clock,
e615efe4
ED
3910 auxdiv,
3911 divsel,
3912 phasedir,
3913 phaseinc);
3914
060f02d8
VS
3915 mutex_lock(&dev_priv->sb_lock);
3916
e615efe4 3917 /* Program SSCDIVINTPHASE6 */
988d6ee8 3918 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
e615efe4
ED
3919 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
3920 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
3921 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
3922 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
3923 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
3924 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
988d6ee8 3925 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
e615efe4
ED
3926
3927 /* Program SSCAUXDIV */
988d6ee8 3928 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
e615efe4
ED
3929 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
3930 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
988d6ee8 3931 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
e615efe4
ED
3932
3933 /* Enable modulator and associated divider */
988d6ee8 3934 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
e615efe4 3935 temp &= ~SBI_SSCCTL_DISABLE;
988d6ee8 3936 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
e615efe4 3937
060f02d8
VS
3938 mutex_unlock(&dev_priv->sb_lock);
3939
e615efe4
ED
3940 /* Wait for initialization time */
3941 udelay(24);
3942
3943 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
3944}
3945
8802e5b6
VS
3946int lpt_get_iclkip(struct drm_i915_private *dev_priv)
3947{
3948 u32 divsel, phaseinc, auxdiv;
3949 u32 iclk_virtual_root_freq = 172800 * 1000;
3950 u32 iclk_pi_range = 64;
3951 u32 desired_divisor;
3952 u32 temp;
3953
3954 if ((I915_READ(PIXCLK_GATE) & PIXCLK_GATE_UNGATE) == 0)
3955 return 0;
3956
3957 mutex_lock(&dev_priv->sb_lock);
3958
3959 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
3960 if (temp & SBI_SSCCTL_DISABLE) {
3961 mutex_unlock(&dev_priv->sb_lock);
3962 return 0;
3963 }
3964
3965 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
3966 divsel = (temp & SBI_SSCDIVINTPHASE_DIVSEL_MASK) >>
3967 SBI_SSCDIVINTPHASE_DIVSEL_SHIFT;
3968 phaseinc = (temp & SBI_SSCDIVINTPHASE_INCVAL_MASK) >>
3969 SBI_SSCDIVINTPHASE_INCVAL_SHIFT;
3970
3971 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
3972 auxdiv = (temp & SBI_SSCAUXDIV_FINALDIV2SEL_MASK) >>
3973 SBI_SSCAUXDIV_FINALDIV2SEL_SHIFT;
3974
3975 mutex_unlock(&dev_priv->sb_lock);
3976
3977 desired_divisor = (divsel + 2) * iclk_pi_range + phaseinc;
3978
3979 return DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
3980 desired_divisor << auxdiv);
3981}
3982
275f01b2
DV
3983static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
3984 enum pipe pch_transcoder)
3985{
3986 struct drm_device *dev = crtc->base.dev;
3987 struct drm_i915_private *dev_priv = dev->dev_private;
6e3c9717 3988 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
275f01b2
DV
3989
3990 I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
3991 I915_READ(HTOTAL(cpu_transcoder)));
3992 I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
3993 I915_READ(HBLANK(cpu_transcoder)));
3994 I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
3995 I915_READ(HSYNC(cpu_transcoder)));
3996
3997 I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
3998 I915_READ(VTOTAL(cpu_transcoder)));
3999 I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
4000 I915_READ(VBLANK(cpu_transcoder)));
4001 I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
4002 I915_READ(VSYNC(cpu_transcoder)));
4003 I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
4004 I915_READ(VSYNCSHIFT(cpu_transcoder)));
4005}
4006
003632d9 4007static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
1fbc0d78
DV
4008{
4009 struct drm_i915_private *dev_priv = dev->dev_private;
4010 uint32_t temp;
4011
4012 temp = I915_READ(SOUTH_CHICKEN1);
003632d9 4013 if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
1fbc0d78
DV
4014 return;
4015
4016 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
4017 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
4018
003632d9
ACO
4019 temp &= ~FDI_BC_BIFURCATION_SELECT;
4020 if (enable)
4021 temp |= FDI_BC_BIFURCATION_SELECT;
4022
4023 DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
1fbc0d78
DV
4024 I915_WRITE(SOUTH_CHICKEN1, temp);
4025 POSTING_READ(SOUTH_CHICKEN1);
4026}
4027
4028static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
4029{
4030 struct drm_device *dev = intel_crtc->base.dev;
1fbc0d78
DV
4031
4032 switch (intel_crtc->pipe) {
4033 case PIPE_A:
4034 break;
4035 case PIPE_B:
6e3c9717 4036 if (intel_crtc->config->fdi_lanes > 2)
003632d9 4037 cpt_set_fdi_bc_bifurcation(dev, false);
1fbc0d78 4038 else
003632d9 4039 cpt_set_fdi_bc_bifurcation(dev, true);
1fbc0d78
DV
4040
4041 break;
4042 case PIPE_C:
003632d9 4043 cpt_set_fdi_bc_bifurcation(dev, true);
1fbc0d78
DV
4044
4045 break;
4046 default:
4047 BUG();
4048 }
4049}
4050
c48b5305
VS
4051/* Return which DP Port should be selected for Transcoder DP control */
4052static enum port
4053intel_trans_dp_port_sel(struct drm_crtc *crtc)
4054{
4055 struct drm_device *dev = crtc->dev;
4056 struct intel_encoder *encoder;
4057
4058 for_each_encoder_on_crtc(dev, crtc, encoder) {
4059 if (encoder->type == INTEL_OUTPUT_DISPLAYPORT ||
4060 encoder->type == INTEL_OUTPUT_EDP)
4061 return enc_to_dig_port(&encoder->base)->port;
4062 }
4063
4064 return -1;
4065}
4066
f67a559d
JB
4067/*
4068 * Enable PCH resources required for PCH ports:
4069 * - PCH PLLs
4070 * - FDI training & RX/TX
4071 * - update transcoder timings
4072 * - DP transcoding bits
4073 * - transcoder
4074 */
4075static void ironlake_pch_enable(struct drm_crtc *crtc)
0e23b99d
JB
4076{
4077 struct drm_device *dev = crtc->dev;
4078 struct drm_i915_private *dev_priv = dev->dev_private;
4079 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4080 int pipe = intel_crtc->pipe;
f0f59a00 4081 u32 temp;
2c07245f 4082
ab9412ba 4083 assert_pch_transcoder_disabled(dev_priv, pipe);
e7e164db 4084
1fbc0d78
DV
4085 if (IS_IVYBRIDGE(dev))
4086 ivybridge_update_fdi_bc_bifurcation(intel_crtc);
4087
cd986abb
DV
4088 /* Write the TU size bits before fdi link training, so that error
4089 * detection works. */
4090 I915_WRITE(FDI_RX_TUSIZE1(pipe),
4091 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
4092
c98e9dcf 4093 /* For PCH output, training FDI link */
674cf967 4094 dev_priv->display.fdi_link_train(crtc);
2c07245f 4095
3ad8a208
DV
4096 /* We need to program the right clock selection before writing the pixel
4097 * mutliplier into the DPLL. */
303b81e0 4098 if (HAS_PCH_CPT(dev)) {
ee7b9f93 4099 u32 sel;
4b645f14 4100
c98e9dcf 4101 temp = I915_READ(PCH_DPLL_SEL);
11887397
DV
4102 temp |= TRANS_DPLL_ENABLE(pipe);
4103 sel = TRANS_DPLLB_SEL(pipe);
8106ddbd
ACO
4104 if (intel_crtc->config->shared_dpll ==
4105 intel_get_shared_dpll_by_id(dev_priv, DPLL_ID_PCH_PLL_B))
ee7b9f93
JB
4106 temp |= sel;
4107 else
4108 temp &= ~sel;
c98e9dcf 4109 I915_WRITE(PCH_DPLL_SEL, temp);
c98e9dcf 4110 }
5eddb70b 4111
3ad8a208
DV
4112 /* XXX: pch pll's can be enabled any time before we enable the PCH
4113 * transcoder, and we actually should do this to not upset any PCH
4114 * transcoder that already use the clock when we share it.
4115 *
4116 * Note that enable_shared_dpll tries to do the right thing, but
4117 * get_shared_dpll unconditionally resets the pll - we need that to have
4118 * the right LVDS enable sequence. */
85b3894f 4119 intel_enable_shared_dpll(intel_crtc);
3ad8a208 4120
d9b6cb56
JB
4121 /* set transcoder timing, panel must allow it */
4122 assert_panel_unlocked(dev_priv, pipe);
275f01b2 4123 ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
8db9d77b 4124
303b81e0 4125 intel_fdi_normal_train(crtc);
5e84e1a4 4126
c98e9dcf 4127 /* For PCH DP, enable TRANS_DP_CTL */
6e3c9717 4128 if (HAS_PCH_CPT(dev) && intel_crtc->config->has_dp_encoder) {
9c4edaee
VS
4129 const struct drm_display_mode *adjusted_mode =
4130 &intel_crtc->config->base.adjusted_mode;
dfd07d72 4131 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
f0f59a00 4132 i915_reg_t reg = TRANS_DP_CTL(pipe);
5eddb70b
CW
4133 temp = I915_READ(reg);
4134 temp &= ~(TRANS_DP_PORT_SEL_MASK |
220cad3c
EA
4135 TRANS_DP_SYNC_MASK |
4136 TRANS_DP_BPC_MASK);
e3ef4479 4137 temp |= TRANS_DP_OUTPUT_ENABLE;
9325c9f0 4138 temp |= bpc << 9; /* same format but at 11:9 */
c98e9dcf 4139
9c4edaee 4140 if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
5eddb70b 4141 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
9c4edaee 4142 if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
5eddb70b 4143 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
c98e9dcf
JB
4144
4145 switch (intel_trans_dp_port_sel(crtc)) {
c48b5305 4146 case PORT_B:
5eddb70b 4147 temp |= TRANS_DP_PORT_SEL_B;
c98e9dcf 4148 break;
c48b5305 4149 case PORT_C:
5eddb70b 4150 temp |= TRANS_DP_PORT_SEL_C;
c98e9dcf 4151 break;
c48b5305 4152 case PORT_D:
5eddb70b 4153 temp |= TRANS_DP_PORT_SEL_D;
c98e9dcf
JB
4154 break;
4155 default:
e95d41e1 4156 BUG();
32f9d658 4157 }
2c07245f 4158
5eddb70b 4159 I915_WRITE(reg, temp);
6be4a607 4160 }
b52eb4dc 4161
b8a4f404 4162 ironlake_enable_pch_transcoder(dev_priv, pipe);
f67a559d
JB
4163}
4164
1507e5bd
PZ
4165static void lpt_pch_enable(struct drm_crtc *crtc)
4166{
4167 struct drm_device *dev = crtc->dev;
4168 struct drm_i915_private *dev_priv = dev->dev_private;
4169 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6e3c9717 4170 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
1507e5bd 4171
ab9412ba 4172 assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
1507e5bd 4173
8c52b5e8 4174 lpt_program_iclkip(crtc);
1507e5bd 4175
0540e488 4176 /* Set transcoder timing. */
275f01b2 4177 ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
1507e5bd 4178
937bb610 4179 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
f67a559d
JB
4180}
4181
a1520318 4182static void cpt_verify_modeset(struct drm_device *dev, int pipe)
d4270e57
JB
4183{
4184 struct drm_i915_private *dev_priv = dev->dev_private;
f0f59a00 4185 i915_reg_t dslreg = PIPEDSL(pipe);
d4270e57
JB
4186 u32 temp;
4187
4188 temp = I915_READ(dslreg);
4189 udelay(500);
4190 if (wait_for(I915_READ(dslreg) != temp, 5)) {
d4270e57 4191 if (wait_for(I915_READ(dslreg) != temp, 5))
84f44ce7 4192 DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
d4270e57
JB
4193 }
4194}
4195
86adf9d7
ML
4196static int
4197skl_update_scaler(struct intel_crtc_state *crtc_state, bool force_detach,
4198 unsigned scaler_user, int *scaler_id, unsigned int rotation,
4199 int src_w, int src_h, int dst_w, int dst_h)
a1b2278e 4200{
86adf9d7
ML
4201 struct intel_crtc_scaler_state *scaler_state =
4202 &crtc_state->scaler_state;
4203 struct intel_crtc *intel_crtc =
4204 to_intel_crtc(crtc_state->base.crtc);
a1b2278e 4205 int need_scaling;
6156a456
CK
4206
4207 need_scaling = intel_rotation_90_or_270(rotation) ?
4208 (src_h != dst_w || src_w != dst_h):
4209 (src_w != dst_w || src_h != dst_h);
a1b2278e
CK
4210
4211 /*
4212 * if plane is being disabled or scaler is no more required or force detach
4213 * - free scaler binded to this plane/crtc
4214 * - in order to do this, update crtc->scaler_usage
4215 *
4216 * Here scaler state in crtc_state is set free so that
4217 * scaler can be assigned to other user. Actual register
4218 * update to free the scaler is done in plane/panel-fit programming.
4219 * For this purpose crtc/plane_state->scaler_id isn't reset here.
4220 */
86adf9d7 4221 if (force_detach || !need_scaling) {
a1b2278e 4222 if (*scaler_id >= 0) {
86adf9d7 4223 scaler_state->scaler_users &= ~(1 << scaler_user);
a1b2278e
CK
4224 scaler_state->scalers[*scaler_id].in_use = 0;
4225
86adf9d7
ML
4226 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4227 "Staged freeing scaler id %d scaler_users = 0x%x\n",
4228 intel_crtc->pipe, scaler_user, *scaler_id,
a1b2278e
CK
4229 scaler_state->scaler_users);
4230 *scaler_id = -1;
4231 }
4232 return 0;
4233 }
4234
4235 /* range checks */
4236 if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
4237 dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
4238
4239 src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
4240 dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
86adf9d7 4241 DRM_DEBUG_KMS("scaler_user index %u.%u: src %ux%u dst %ux%u "
a1b2278e 4242 "size is out of scaler range\n",
86adf9d7 4243 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h);
a1b2278e
CK
4244 return -EINVAL;
4245 }
4246
86adf9d7
ML
4247 /* mark this plane as a scaler user in crtc_state */
4248 scaler_state->scaler_users |= (1 << scaler_user);
4249 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4250 "staged scaling request for %ux%u->%ux%u scaler_users = 0x%x\n",
4251 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h,
4252 scaler_state->scaler_users);
4253
4254 return 0;
4255}
4256
4257/**
4258 * skl_update_scaler_crtc - Stages update to scaler state for a given crtc.
4259 *
4260 * @state: crtc's scaler state
86adf9d7
ML
4261 *
4262 * Return
4263 * 0 - scaler_usage updated successfully
4264 * error - requested scaling cannot be supported or other error condition
4265 */
e435d6e5 4266int skl_update_scaler_crtc(struct intel_crtc_state *state)
86adf9d7
ML
4267{
4268 struct intel_crtc *intel_crtc = to_intel_crtc(state->base.crtc);
7c5f93b0 4269 const struct drm_display_mode *adjusted_mode = &state->base.adjusted_mode;
86adf9d7
ML
4270
4271 DRM_DEBUG_KMS("Updating scaler for [CRTC:%i] scaler_user index %u.%u\n",
4272 intel_crtc->base.base.id, intel_crtc->pipe, SKL_CRTC_INDEX);
4273
e435d6e5 4274 return skl_update_scaler(state, !state->base.active, SKL_CRTC_INDEX,
fa5a7970 4275 &state->scaler_state.scaler_id, BIT(DRM_ROTATE_0),
86adf9d7 4276 state->pipe_src_w, state->pipe_src_h,
aad941d5 4277 adjusted_mode->crtc_hdisplay, adjusted_mode->crtc_vdisplay);
86adf9d7
ML
4278}
4279
4280/**
4281 * skl_update_scaler_plane - Stages update to scaler state for a given plane.
4282 *
4283 * @state: crtc's scaler state
86adf9d7
ML
4284 * @plane_state: atomic plane state to update
4285 *
4286 * Return
4287 * 0 - scaler_usage updated successfully
4288 * error - requested scaling cannot be supported or other error condition
4289 */
da20eabd
ML
4290static int skl_update_scaler_plane(struct intel_crtc_state *crtc_state,
4291 struct intel_plane_state *plane_state)
86adf9d7
ML
4292{
4293
4294 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
da20eabd
ML
4295 struct intel_plane *intel_plane =
4296 to_intel_plane(plane_state->base.plane);
86adf9d7
ML
4297 struct drm_framebuffer *fb = plane_state->base.fb;
4298 int ret;
4299
4300 bool force_detach = !fb || !plane_state->visible;
4301
4302 DRM_DEBUG_KMS("Updating scaler for [PLANE:%d] scaler_user index %u.%u\n",
4303 intel_plane->base.base.id, intel_crtc->pipe,
4304 drm_plane_index(&intel_plane->base));
4305
4306 ret = skl_update_scaler(crtc_state, force_detach,
4307 drm_plane_index(&intel_plane->base),
4308 &plane_state->scaler_id,
4309 plane_state->base.rotation,
4310 drm_rect_width(&plane_state->src) >> 16,
4311 drm_rect_height(&plane_state->src) >> 16,
4312 drm_rect_width(&plane_state->dst),
4313 drm_rect_height(&plane_state->dst));
4314
4315 if (ret || plane_state->scaler_id < 0)
4316 return ret;
4317
a1b2278e 4318 /* check colorkey */
818ed961 4319 if (plane_state->ckey.flags != I915_SET_COLORKEY_NONE) {
86adf9d7 4320 DRM_DEBUG_KMS("[PLANE:%d] scaling with color key not allowed",
818ed961 4321 intel_plane->base.base.id);
a1b2278e
CK
4322 return -EINVAL;
4323 }
4324
4325 /* Check src format */
86adf9d7
ML
4326 switch (fb->pixel_format) {
4327 case DRM_FORMAT_RGB565:
4328 case DRM_FORMAT_XBGR8888:
4329 case DRM_FORMAT_XRGB8888:
4330 case DRM_FORMAT_ABGR8888:
4331 case DRM_FORMAT_ARGB8888:
4332 case DRM_FORMAT_XRGB2101010:
4333 case DRM_FORMAT_XBGR2101010:
4334 case DRM_FORMAT_YUYV:
4335 case DRM_FORMAT_YVYU:
4336 case DRM_FORMAT_UYVY:
4337 case DRM_FORMAT_VYUY:
4338 break;
4339 default:
4340 DRM_DEBUG_KMS("[PLANE:%d] FB:%d unsupported scaling format 0x%x\n",
4341 intel_plane->base.base.id, fb->base.id, fb->pixel_format);
4342 return -EINVAL;
a1b2278e
CK
4343 }
4344
a1b2278e
CK
4345 return 0;
4346}
4347
e435d6e5
ML
4348static void skylake_scaler_disable(struct intel_crtc *crtc)
4349{
4350 int i;
4351
4352 for (i = 0; i < crtc->num_scalers; i++)
4353 skl_detach_scaler(crtc, i);
4354}
4355
4356static void skylake_pfit_enable(struct intel_crtc *crtc)
bd2e244f
JB
4357{
4358 struct drm_device *dev = crtc->base.dev;
4359 struct drm_i915_private *dev_priv = dev->dev_private;
4360 int pipe = crtc->pipe;
a1b2278e
CK
4361 struct intel_crtc_scaler_state *scaler_state =
4362 &crtc->config->scaler_state;
4363
4364 DRM_DEBUG_KMS("for crtc_state = %p\n", crtc->config);
4365
6e3c9717 4366 if (crtc->config->pch_pfit.enabled) {
a1b2278e
CK
4367 int id;
4368
4369 if (WARN_ON(crtc->config->scaler_state.scaler_id < 0)) {
4370 DRM_ERROR("Requesting pfit without getting a scaler first\n");
4371 return;
4372 }
4373
4374 id = scaler_state->scaler_id;
4375 I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
4376 PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
4377 I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
4378 I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
4379
4380 DRM_DEBUG_KMS("for crtc_state = %p scaler_id = %d\n", crtc->config, id);
bd2e244f
JB
4381 }
4382}
4383
b074cec8
JB
4384static void ironlake_pfit_enable(struct intel_crtc *crtc)
4385{
4386 struct drm_device *dev = crtc->base.dev;
4387 struct drm_i915_private *dev_priv = dev->dev_private;
4388 int pipe = crtc->pipe;
4389
6e3c9717 4390 if (crtc->config->pch_pfit.enabled) {
b074cec8
JB
4391 /* Force use of hard-coded filter coefficients
4392 * as some pre-programmed values are broken,
4393 * e.g. x201.
4394 */
4395 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
4396 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
4397 PF_PIPE_SEL_IVB(pipe));
4398 else
4399 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
6e3c9717
ACO
4400 I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
4401 I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
d4270e57
JB
4402 }
4403}
4404
20bc8673 4405void hsw_enable_ips(struct intel_crtc *crtc)
d77e4531 4406{
cea165c3
VS
4407 struct drm_device *dev = crtc->base.dev;
4408 struct drm_i915_private *dev_priv = dev->dev_private;
d77e4531 4409
6e3c9717 4410 if (!crtc->config->ips_enabled)
d77e4531
PZ
4411 return;
4412
307e4498
ML
4413 /*
4414 * We can only enable IPS after we enable a plane and wait for a vblank
4415 * This function is called from post_plane_update, which is run after
4416 * a vblank wait.
4417 */
cea165c3 4418
d77e4531 4419 assert_plane_enabled(dev_priv, crtc->plane);
cea165c3 4420 if (IS_BROADWELL(dev)) {
2a114cc1
BW
4421 mutex_lock(&dev_priv->rps.hw_lock);
4422 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
4423 mutex_unlock(&dev_priv->rps.hw_lock);
4424 /* Quoting Art Runyan: "its not safe to expect any particular
4425 * value in IPS_CTL bit 31 after enabling IPS through the
e59150dc
JB
4426 * mailbox." Moreover, the mailbox may return a bogus state,
4427 * so we need to just enable it and continue on.
2a114cc1
BW
4428 */
4429 } else {
4430 I915_WRITE(IPS_CTL, IPS_ENABLE);
4431 /* The bit only becomes 1 in the next vblank, so this wait here
4432 * is essentially intel_wait_for_vblank. If we don't have this
4433 * and don't wait for vblanks until the end of crtc_enable, then
4434 * the HW state readout code will complain that the expected
4435 * IPS_CTL value is not the one we read. */
4436 if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
4437 DRM_ERROR("Timed out waiting for IPS enable\n");
4438 }
d77e4531
PZ
4439}
4440
20bc8673 4441void hsw_disable_ips(struct intel_crtc *crtc)
d77e4531
PZ
4442{
4443 struct drm_device *dev = crtc->base.dev;
4444 struct drm_i915_private *dev_priv = dev->dev_private;
4445
6e3c9717 4446 if (!crtc->config->ips_enabled)
d77e4531
PZ
4447 return;
4448
4449 assert_plane_enabled(dev_priv, crtc->plane);
23d0b130 4450 if (IS_BROADWELL(dev)) {
2a114cc1
BW
4451 mutex_lock(&dev_priv->rps.hw_lock);
4452 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
4453 mutex_unlock(&dev_priv->rps.hw_lock);
23d0b130
BW
4454 /* wait for pcode to finish disabling IPS, which may take up to 42ms */
4455 if (wait_for((I915_READ(IPS_CTL) & IPS_ENABLE) == 0, 42))
4456 DRM_ERROR("Timed out waiting for IPS disable\n");
e59150dc 4457 } else {
2a114cc1 4458 I915_WRITE(IPS_CTL, 0);
e59150dc
JB
4459 POSTING_READ(IPS_CTL);
4460 }
d77e4531
PZ
4461
4462 /* We need to wait for a vblank before we can disable the plane. */
4463 intel_wait_for_vblank(dev, crtc->pipe);
4464}
4465
7cac945f 4466static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
d3eedb1a 4467{
7cac945f 4468 if (intel_crtc->overlay) {
d3eedb1a
VS
4469 struct drm_device *dev = intel_crtc->base.dev;
4470 struct drm_i915_private *dev_priv = dev->dev_private;
4471
4472 mutex_lock(&dev->struct_mutex);
4473 dev_priv->mm.interruptible = false;
4474 (void) intel_overlay_switch_off(intel_crtc->overlay);
4475 dev_priv->mm.interruptible = true;
4476 mutex_unlock(&dev->struct_mutex);
4477 }
4478
4479 /* Let userspace switch the overlay on again. In most cases userspace
4480 * has to recompute where to put it anyway.
4481 */
4482}
4483
87d4300a
ML
4484/**
4485 * intel_post_enable_primary - Perform operations after enabling primary plane
4486 * @crtc: the CRTC whose primary plane was just enabled
4487 *
4488 * Performs potentially sleeping operations that must be done after the primary
4489 * plane is enabled, such as updating FBC and IPS. Note that this may be
4490 * called due to an explicit primary plane update, or due to an implicit
4491 * re-enable that is caused when a sprite plane is updated to no longer
4492 * completely hide the primary plane.
4493 */
4494static void
4495intel_post_enable_primary(struct drm_crtc *crtc)
a5c4d7bc
VS
4496{
4497 struct drm_device *dev = crtc->dev;
87d4300a 4498 struct drm_i915_private *dev_priv = dev->dev_private;
a5c4d7bc
VS
4499 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4500 int pipe = intel_crtc->pipe;
a5c4d7bc 4501
87d4300a
ML
4502 /*
4503 * FIXME IPS should be fine as long as one plane is
4504 * enabled, but in practice it seems to have problems
4505 * when going from primary only to sprite only and vice
4506 * versa.
4507 */
a5c4d7bc
VS
4508 hsw_enable_ips(intel_crtc);
4509
f99d7069 4510 /*
87d4300a
ML
4511 * Gen2 reports pipe underruns whenever all planes are disabled.
4512 * So don't enable underrun reporting before at least some planes
4513 * are enabled.
4514 * FIXME: Need to fix the logic to work when we turn off all planes
4515 * but leave the pipe running.
f99d7069 4516 */
87d4300a
ML
4517 if (IS_GEN2(dev))
4518 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4519
aca7b684
VS
4520 /* Underruns don't always raise interrupts, so check manually. */
4521 intel_check_cpu_fifo_underruns(dev_priv);
4522 intel_check_pch_fifo_underruns(dev_priv);
a5c4d7bc
VS
4523}
4524
2622a081 4525/* FIXME move all this to pre_plane_update() with proper state tracking */
87d4300a
ML
4526static void
4527intel_pre_disable_primary(struct drm_crtc *crtc)
a5c4d7bc
VS
4528{
4529 struct drm_device *dev = crtc->dev;
4530 struct drm_i915_private *dev_priv = dev->dev_private;
4531 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4532 int pipe = intel_crtc->pipe;
a5c4d7bc 4533
87d4300a
ML
4534 /*
4535 * Gen2 reports pipe underruns whenever all planes are disabled.
4536 * So diasble underrun reporting before all the planes get disabled.
4537 * FIXME: Need to fix the logic to work when we turn off all planes
4538 * but leave the pipe running.
4539 */
4540 if (IS_GEN2(dev))
4541 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
a5c4d7bc 4542
2622a081
VS
4543 /*
4544 * FIXME IPS should be fine as long as one plane is
4545 * enabled, but in practice it seems to have problems
4546 * when going from primary only to sprite only and vice
4547 * versa.
4548 */
4549 hsw_disable_ips(intel_crtc);
4550}
4551
4552/* FIXME get rid of this and use pre_plane_update */
4553static void
4554intel_pre_disable_primary_noatomic(struct drm_crtc *crtc)
4555{
4556 struct drm_device *dev = crtc->dev;
4557 struct drm_i915_private *dev_priv = dev->dev_private;
4558 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4559 int pipe = intel_crtc->pipe;
4560
4561 intel_pre_disable_primary(crtc);
4562
87d4300a
ML
4563 /*
4564 * Vblank time updates from the shadow to live plane control register
4565 * are blocked if the memory self-refresh mode is active at that
4566 * moment. So to make sure the plane gets truly disabled, disable
4567 * first the self-refresh mode. The self-refresh enable bit in turn
4568 * will be checked/applied by the HW only at the next frame start
4569 * event which is after the vblank start event, so we need to have a
4570 * wait-for-vblank between disabling the plane and the pipe.
4571 */
262cd2e1 4572 if (HAS_GMCH_DISPLAY(dev)) {
87d4300a 4573 intel_set_memory_cxsr(dev_priv, false);
262cd2e1
VS
4574 dev_priv->wm.vlv.cxsr = false;
4575 intel_wait_for_vblank(dev, pipe);
4576 }
87d4300a
ML
4577}
4578
cd202f69 4579static void intel_post_plane_update(struct intel_crtc_state *old_crtc_state)
ac21b225 4580{
cd202f69
ML
4581 struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
4582 struct drm_atomic_state *old_state = old_crtc_state->base.state;
92826fcd
ML
4583 struct intel_crtc_state *pipe_config =
4584 to_intel_crtc_state(crtc->base.state);
ac21b225 4585 struct drm_device *dev = crtc->base.dev;
cd202f69
ML
4586 struct drm_plane *primary = crtc->base.primary;
4587 struct drm_plane_state *old_pri_state =
4588 drm_atomic_get_existing_plane_state(old_state, primary);
ac21b225 4589
cd202f69 4590 intel_frontbuffer_flip(dev, pipe_config->fb_bits);
ac21b225 4591
ab1d3a0e 4592 crtc->wm.cxsr_allowed = true;
852eb00d 4593
caed361d 4594 if (pipe_config->update_wm_post && pipe_config->base.active)
f015c551
VS
4595 intel_update_watermarks(&crtc->base);
4596
cd202f69
ML
4597 if (old_pri_state) {
4598 struct intel_plane_state *primary_state =
4599 to_intel_plane_state(primary->state);
4600 struct intel_plane_state *old_primary_state =
4601 to_intel_plane_state(old_pri_state);
4602
31ae71fc
ML
4603 intel_fbc_post_update(crtc);
4604
cd202f69
ML
4605 if (primary_state->visible &&
4606 (needs_modeset(&pipe_config->base) ||
4607 !old_primary_state->visible))
4608 intel_post_enable_primary(&crtc->base);
4609 }
ac21b225
ML
4610}
4611
5c74cd73 4612static void intel_pre_plane_update(struct intel_crtc_state *old_crtc_state)
ac21b225 4613{
5c74cd73 4614 struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
ac21b225 4615 struct drm_device *dev = crtc->base.dev;
eddfcbcd 4616 struct drm_i915_private *dev_priv = dev->dev_private;
ab1d3a0e
ML
4617 struct intel_crtc_state *pipe_config =
4618 to_intel_crtc_state(crtc->base.state);
5c74cd73
ML
4619 struct drm_atomic_state *old_state = old_crtc_state->base.state;
4620 struct drm_plane *primary = crtc->base.primary;
4621 struct drm_plane_state *old_pri_state =
4622 drm_atomic_get_existing_plane_state(old_state, primary);
4623 bool modeset = needs_modeset(&pipe_config->base);
ac21b225 4624
5c74cd73
ML
4625 if (old_pri_state) {
4626 struct intel_plane_state *primary_state =
4627 to_intel_plane_state(primary->state);
4628 struct intel_plane_state *old_primary_state =
4629 to_intel_plane_state(old_pri_state);
4630
31ae71fc
ML
4631 intel_fbc_pre_update(crtc);
4632
5c74cd73
ML
4633 if (old_primary_state->visible &&
4634 (modeset || !primary_state->visible))
4635 intel_pre_disable_primary(&crtc->base);
4636 }
852eb00d 4637
ab1d3a0e 4638 if (pipe_config->disable_cxsr) {
852eb00d 4639 crtc->wm.cxsr_allowed = false;
2dfd178d 4640
2622a081
VS
4641 /*
4642 * Vblank time updates from the shadow to live plane control register
4643 * are blocked if the memory self-refresh mode is active at that
4644 * moment. So to make sure the plane gets truly disabled, disable
4645 * first the self-refresh mode. The self-refresh enable bit in turn
4646 * will be checked/applied by the HW only at the next frame start
4647 * event which is after the vblank start event, so we need to have a
4648 * wait-for-vblank between disabling the plane and the pipe.
4649 */
4650 if (old_crtc_state->base.active) {
2dfd178d 4651 intel_set_memory_cxsr(dev_priv, false);
2622a081
VS
4652 dev_priv->wm.vlv.cxsr = false;
4653 intel_wait_for_vblank(dev, crtc->pipe);
4654 }
852eb00d 4655 }
92826fcd 4656
ed4a6a7c
MR
4657 /*
4658 * IVB workaround: must disable low power watermarks for at least
4659 * one frame before enabling scaling. LP watermarks can be re-enabled
4660 * when scaling is disabled.
4661 *
4662 * WaCxSRDisabledForSpriteScaling:ivb
4663 */
4664 if (pipe_config->disable_lp_wm) {
4665 ilk_disable_lp_wm(dev);
4666 intel_wait_for_vblank(dev, crtc->pipe);
4667 }
4668
4669 /*
4670 * If we're doing a modeset, we're done. No need to do any pre-vblank
4671 * watermark programming here.
4672 */
4673 if (needs_modeset(&pipe_config->base))
4674 return;
4675
4676 /*
4677 * For platforms that support atomic watermarks, program the
4678 * 'intermediate' watermarks immediately. On pre-gen9 platforms, these
4679 * will be the intermediate values that are safe for both pre- and
4680 * post- vblank; when vblank happens, the 'active' values will be set
4681 * to the final 'target' values and we'll do this again to get the
4682 * optimal watermarks. For gen9+ platforms, the values we program here
4683 * will be the final target values which will get automatically latched
4684 * at vblank time; no further programming will be necessary.
4685 *
4686 * If a platform hasn't been transitioned to atomic watermarks yet,
4687 * we'll continue to update watermarks the old way, if flags tell
4688 * us to.
4689 */
4690 if (dev_priv->display.initial_watermarks != NULL)
4691 dev_priv->display.initial_watermarks(pipe_config);
caed361d 4692 else if (pipe_config->update_wm_pre)
92826fcd 4693 intel_update_watermarks(&crtc->base);
ac21b225
ML
4694}
4695
d032ffa0 4696static void intel_crtc_disable_planes(struct drm_crtc *crtc, unsigned plane_mask)
87d4300a
ML
4697{
4698 struct drm_device *dev = crtc->dev;
4699 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
d032ffa0 4700 struct drm_plane *p;
87d4300a
ML
4701 int pipe = intel_crtc->pipe;
4702
7cac945f 4703 intel_crtc_dpms_overlay_disable(intel_crtc);
27321ae8 4704
d032ffa0
ML
4705 drm_for_each_plane_mask(p, dev, plane_mask)
4706 to_intel_plane(p)->disable_plane(p, crtc);
f98551ae 4707
f99d7069
DV
4708 /*
4709 * FIXME: Once we grow proper nuclear flip support out of this we need
4710 * to compute the mask of flip planes precisely. For the time being
4711 * consider this a flip to a NULL plane.
4712 */
4713 intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
a5c4d7bc
VS
4714}
4715
f67a559d
JB
4716static void ironlake_crtc_enable(struct drm_crtc *crtc)
4717{
4718 struct drm_device *dev = crtc->dev;
4719 struct drm_i915_private *dev_priv = dev->dev_private;
4720 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 4721 struct intel_encoder *encoder;
f67a559d 4722 int pipe = intel_crtc->pipe;
b95c5321
ML
4723 struct intel_crtc_state *pipe_config =
4724 to_intel_crtc_state(crtc->state);
f67a559d 4725
53d9f4e9 4726 if (WARN_ON(intel_crtc->active))
f67a559d
JB
4727 return;
4728
b2c0593a
VS
4729 /*
4730 * Sometimes spurious CPU pipe underruns happen during FDI
4731 * training, at least with VGA+HDMI cloning. Suppress them.
4732 *
4733 * On ILK we get an occasional spurious CPU pipe underruns
4734 * between eDP port A enable and vdd enable. Also PCH port
4735 * enable seems to result in the occasional CPU pipe underrun.
4736 *
4737 * Spurious PCH underruns also occur during PCH enabling.
4738 */
4739 if (intel_crtc->config->has_pch_encoder || IS_GEN5(dev_priv))
4740 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
81b088ca
VS
4741 if (intel_crtc->config->has_pch_encoder)
4742 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
4743
6e3c9717 4744 if (intel_crtc->config->has_pch_encoder)
b14b1055
DV
4745 intel_prepare_shared_dpll(intel_crtc);
4746
6e3c9717 4747 if (intel_crtc->config->has_dp_encoder)
fe3cd48d 4748 intel_dp_set_m_n(intel_crtc, M1_N1);
29407aab
DV
4749
4750 intel_set_pipe_timings(intel_crtc);
bc58be60 4751 intel_set_pipe_src_size(intel_crtc);
29407aab 4752
6e3c9717 4753 if (intel_crtc->config->has_pch_encoder) {
29407aab 4754 intel_cpu_transcoder_set_m_n(intel_crtc,
6e3c9717 4755 &intel_crtc->config->fdi_m_n, NULL);
29407aab
DV
4756 }
4757
4758 ironlake_set_pipeconf(crtc);
4759
f67a559d 4760 intel_crtc->active = true;
8664281b 4761
f6736a1a 4762 for_each_encoder_on_crtc(dev, crtc, encoder)
952735ee
DV
4763 if (encoder->pre_enable)
4764 encoder->pre_enable(encoder);
f67a559d 4765
6e3c9717 4766 if (intel_crtc->config->has_pch_encoder) {
fff367c7
DV
4767 /* Note: FDI PLL enabling _must_ be done before we enable the
4768 * cpu pipes, hence this is separate from all the other fdi/pch
4769 * enabling. */
88cefb6c 4770 ironlake_fdi_pll_enable(intel_crtc);
46b6f814
DV
4771 } else {
4772 assert_fdi_tx_disabled(dev_priv, pipe);
4773 assert_fdi_rx_disabled(dev_priv, pipe);
4774 }
f67a559d 4775
b074cec8 4776 ironlake_pfit_enable(intel_crtc);
f67a559d 4777
9c54c0dd
JB
4778 /*
4779 * On ILK+ LUT must be loaded before the pipe is running but with
4780 * clocks enabled
4781 */
b95c5321 4782 intel_color_load_luts(&pipe_config->base);
9c54c0dd 4783
1d5bf5d9
ID
4784 if (dev_priv->display.initial_watermarks != NULL)
4785 dev_priv->display.initial_watermarks(intel_crtc->config);
e1fdc473 4786 intel_enable_pipe(intel_crtc);
f67a559d 4787
6e3c9717 4788 if (intel_crtc->config->has_pch_encoder)
f67a559d 4789 ironlake_pch_enable(crtc);
c98e9dcf 4790
f9b61ff6
DV
4791 assert_vblank_disabled(crtc);
4792 drm_crtc_vblank_on(crtc);
4793
fa5c73b1
DV
4794 for_each_encoder_on_crtc(dev, crtc, encoder)
4795 encoder->enable(encoder);
61b77ddd
DV
4796
4797 if (HAS_PCH_CPT(dev))
a1520318 4798 cpt_verify_modeset(dev, intel_crtc->pipe);
37ca8d4c
VS
4799
4800 /* Must wait for vblank to avoid spurious PCH FIFO underruns */
4801 if (intel_crtc->config->has_pch_encoder)
4802 intel_wait_for_vblank(dev, pipe);
b2c0593a 4803 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
37ca8d4c 4804 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
6be4a607
JB
4805}
4806
42db64ef
PZ
4807/* IPS only exists on ULT machines and is tied to pipe A. */
4808static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
4809{
f5adf94e 4810 return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
42db64ef
PZ
4811}
4812
4f771f10
PZ
4813static void haswell_crtc_enable(struct drm_crtc *crtc)
4814{
4815 struct drm_device *dev = crtc->dev;
4816 struct drm_i915_private *dev_priv = dev->dev_private;
4817 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4818 struct intel_encoder *encoder;
99d736a2 4819 int pipe = intel_crtc->pipe, hsw_workaround_pipe;
4d1de975 4820 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
99d736a2
ML
4821 struct intel_crtc_state *pipe_config =
4822 to_intel_crtc_state(crtc->state);
4f771f10 4823
53d9f4e9 4824 if (WARN_ON(intel_crtc->active))
4f771f10
PZ
4825 return;
4826
81b088ca
VS
4827 if (intel_crtc->config->has_pch_encoder)
4828 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
4829 false);
4830
8106ddbd 4831 if (intel_crtc->config->shared_dpll)
df8ad70c
DV
4832 intel_enable_shared_dpll(intel_crtc);
4833
6e3c9717 4834 if (intel_crtc->config->has_dp_encoder)
fe3cd48d 4835 intel_dp_set_m_n(intel_crtc, M1_N1);
229fca97 4836
4d1de975
JN
4837 if (!intel_crtc->config->has_dsi_encoder)
4838 intel_set_pipe_timings(intel_crtc);
4839
bc58be60 4840 intel_set_pipe_src_size(intel_crtc);
229fca97 4841
4d1de975
JN
4842 if (cpu_transcoder != TRANSCODER_EDP &&
4843 !transcoder_is_dsi(cpu_transcoder)) {
4844 I915_WRITE(PIPE_MULT(cpu_transcoder),
6e3c9717 4845 intel_crtc->config->pixel_multiplier - 1);
ebb69c95
CT
4846 }
4847
6e3c9717 4848 if (intel_crtc->config->has_pch_encoder) {
229fca97 4849 intel_cpu_transcoder_set_m_n(intel_crtc,
6e3c9717 4850 &intel_crtc->config->fdi_m_n, NULL);
229fca97
DV
4851 }
4852
4d1de975
JN
4853 if (!intel_crtc->config->has_dsi_encoder)
4854 haswell_set_pipeconf(crtc);
4855
391bf048 4856 haswell_set_pipemisc(crtc);
229fca97 4857
b95c5321 4858 intel_color_set_csc(&pipe_config->base);
229fca97 4859
4f771f10 4860 intel_crtc->active = true;
8664281b 4861
6b698516
DV
4862 if (intel_crtc->config->has_pch_encoder)
4863 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
4864 else
4865 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4866
7d4aefd0 4867 for_each_encoder_on_crtc(dev, crtc, encoder) {
4f771f10
PZ
4868 if (encoder->pre_enable)
4869 encoder->pre_enable(encoder);
7d4aefd0 4870 }
4f771f10 4871
d2d65408 4872 if (intel_crtc->config->has_pch_encoder)
4fe9467d 4873 dev_priv->display.fdi_link_train(crtc);
4fe9467d 4874
a65347ba 4875 if (!intel_crtc->config->has_dsi_encoder)
7d4aefd0 4876 intel_ddi_enable_pipe_clock(intel_crtc);
4f771f10 4877
1c132b44 4878 if (INTEL_INFO(dev)->gen >= 9)
e435d6e5 4879 skylake_pfit_enable(intel_crtc);
ff6d9f55 4880 else
1c132b44 4881 ironlake_pfit_enable(intel_crtc);
4f771f10
PZ
4882
4883 /*
4884 * On ILK+ LUT must be loaded before the pipe is running but with
4885 * clocks enabled
4886 */
b95c5321 4887 intel_color_load_luts(&pipe_config->base);
4f771f10 4888
1f544388 4889 intel_ddi_set_pipe_settings(crtc);
a65347ba 4890 if (!intel_crtc->config->has_dsi_encoder)
7d4aefd0 4891 intel_ddi_enable_transcoder_func(crtc);
4f771f10 4892
1d5bf5d9
ID
4893 if (dev_priv->display.initial_watermarks != NULL)
4894 dev_priv->display.initial_watermarks(pipe_config);
4895 else
4896 intel_update_watermarks(crtc);
4d1de975
JN
4897
4898 /* XXX: Do the pipe assertions at the right place for BXT DSI. */
4899 if (!intel_crtc->config->has_dsi_encoder)
4900 intel_enable_pipe(intel_crtc);
42db64ef 4901
6e3c9717 4902 if (intel_crtc->config->has_pch_encoder)
1507e5bd 4903 lpt_pch_enable(crtc);
4f771f10 4904
a65347ba 4905 if (intel_crtc->config->dp_encoder_is_mst)
0e32b39c
DA
4906 intel_ddi_set_vc_payload_alloc(crtc, true);
4907
f9b61ff6
DV
4908 assert_vblank_disabled(crtc);
4909 drm_crtc_vblank_on(crtc);
4910
8807e55b 4911 for_each_encoder_on_crtc(dev, crtc, encoder) {
4f771f10 4912 encoder->enable(encoder);
8807e55b
JN
4913 intel_opregion_notify_encoder(encoder, true);
4914 }
4f771f10 4915
6b698516
DV
4916 if (intel_crtc->config->has_pch_encoder) {
4917 intel_wait_for_vblank(dev, pipe);
4918 intel_wait_for_vblank(dev, pipe);
4919 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
d2d65408
VS
4920 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
4921 true);
6b698516 4922 }
d2d65408 4923
e4916946
PZ
4924 /* If we change the relative order between pipe/planes enabling, we need
4925 * to change the workaround. */
99d736a2
ML
4926 hsw_workaround_pipe = pipe_config->hsw_workaround_pipe;
4927 if (IS_HASWELL(dev) && hsw_workaround_pipe != INVALID_PIPE) {
4928 intel_wait_for_vblank(dev, hsw_workaround_pipe);
4929 intel_wait_for_vblank(dev, hsw_workaround_pipe);
4930 }
4f771f10
PZ
4931}
4932
bfd16b2a 4933static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force)
3f8dce3a
DV
4934{
4935 struct drm_device *dev = crtc->base.dev;
4936 struct drm_i915_private *dev_priv = dev->dev_private;
4937 int pipe = crtc->pipe;
4938
4939 /* To avoid upsetting the power well on haswell only disable the pfit if
4940 * it's in use. The hw state code will make sure we get this right. */
bfd16b2a 4941 if (force || crtc->config->pch_pfit.enabled) {
3f8dce3a
DV
4942 I915_WRITE(PF_CTL(pipe), 0);
4943 I915_WRITE(PF_WIN_POS(pipe), 0);
4944 I915_WRITE(PF_WIN_SZ(pipe), 0);
4945 }
4946}
4947
6be4a607
JB
4948static void ironlake_crtc_disable(struct drm_crtc *crtc)
4949{
4950 struct drm_device *dev = crtc->dev;
4951 struct drm_i915_private *dev_priv = dev->dev_private;
4952 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 4953 struct intel_encoder *encoder;
6be4a607 4954 int pipe = intel_crtc->pipe;
b52eb4dc 4955
b2c0593a
VS
4956 /*
4957 * Sometimes spurious CPU pipe underruns happen when the
4958 * pipe is already disabled, but FDI RX/TX is still enabled.
4959 * Happens at least with VGA+HDMI cloning. Suppress them.
4960 */
4961 if (intel_crtc->config->has_pch_encoder) {
4962 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
37ca8d4c 4963 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
b2c0593a 4964 }
37ca8d4c 4965
ea9d758d
DV
4966 for_each_encoder_on_crtc(dev, crtc, encoder)
4967 encoder->disable(encoder);
4968
f9b61ff6
DV
4969 drm_crtc_vblank_off(crtc);
4970 assert_vblank_disabled(crtc);
4971
575f7ab7 4972 intel_disable_pipe(intel_crtc);
32f9d658 4973
bfd16b2a 4974 ironlake_pfit_disable(intel_crtc, false);
2c07245f 4975
b2c0593a 4976 if (intel_crtc->config->has_pch_encoder)
5a74f70a
VS
4977 ironlake_fdi_disable(crtc);
4978
bf49ec8c
DV
4979 for_each_encoder_on_crtc(dev, crtc, encoder)
4980 if (encoder->post_disable)
4981 encoder->post_disable(encoder);
2c07245f 4982
6e3c9717 4983 if (intel_crtc->config->has_pch_encoder) {
d925c59a 4984 ironlake_disable_pch_transcoder(dev_priv, pipe);
6be4a607 4985
d925c59a 4986 if (HAS_PCH_CPT(dev)) {
f0f59a00
VS
4987 i915_reg_t reg;
4988 u32 temp;
4989
d925c59a
DV
4990 /* disable TRANS_DP_CTL */
4991 reg = TRANS_DP_CTL(pipe);
4992 temp = I915_READ(reg);
4993 temp &= ~(TRANS_DP_OUTPUT_ENABLE |
4994 TRANS_DP_PORT_SEL_MASK);
4995 temp |= TRANS_DP_PORT_SEL_NONE;
4996 I915_WRITE(reg, temp);
4997
4998 /* disable DPLL_SEL */
4999 temp = I915_READ(PCH_DPLL_SEL);
11887397 5000 temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
d925c59a 5001 I915_WRITE(PCH_DPLL_SEL, temp);
9db4a9c7 5002 }
e3421a18 5003
d925c59a
DV
5004 ironlake_fdi_pll_disable(intel_crtc);
5005 }
81b088ca 5006
b2c0593a 5007 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
81b088ca 5008 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
6be4a607 5009}
1b3c7a47 5010
4f771f10 5011static void haswell_crtc_disable(struct drm_crtc *crtc)
ee7b9f93 5012{
4f771f10
PZ
5013 struct drm_device *dev = crtc->dev;
5014 struct drm_i915_private *dev_priv = dev->dev_private;
ee7b9f93 5015 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4f771f10 5016 struct intel_encoder *encoder;
6e3c9717 5017 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
ee7b9f93 5018
d2d65408
VS
5019 if (intel_crtc->config->has_pch_encoder)
5020 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5021 false);
5022
8807e55b
JN
5023 for_each_encoder_on_crtc(dev, crtc, encoder) {
5024 intel_opregion_notify_encoder(encoder, false);
4f771f10 5025 encoder->disable(encoder);
8807e55b 5026 }
4f771f10 5027
f9b61ff6
DV
5028 drm_crtc_vblank_off(crtc);
5029 assert_vblank_disabled(crtc);
5030
4d1de975
JN
5031 /* XXX: Do the pipe assertions at the right place for BXT DSI. */
5032 if (!intel_crtc->config->has_dsi_encoder)
5033 intel_disable_pipe(intel_crtc);
4f771f10 5034
6e3c9717 5035 if (intel_crtc->config->dp_encoder_is_mst)
a4bf214f
VS
5036 intel_ddi_set_vc_payload_alloc(crtc, false);
5037
a65347ba 5038 if (!intel_crtc->config->has_dsi_encoder)
7d4aefd0 5039 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
4f771f10 5040
1c132b44 5041 if (INTEL_INFO(dev)->gen >= 9)
e435d6e5 5042 skylake_scaler_disable(intel_crtc);
ff6d9f55 5043 else
bfd16b2a 5044 ironlake_pfit_disable(intel_crtc, false);
4f771f10 5045
a65347ba 5046 if (!intel_crtc->config->has_dsi_encoder)
7d4aefd0 5047 intel_ddi_disable_pipe_clock(intel_crtc);
4f771f10 5048
97b040aa
ID
5049 for_each_encoder_on_crtc(dev, crtc, encoder)
5050 if (encoder->post_disable)
5051 encoder->post_disable(encoder);
81b088ca 5052
92966a37
VS
5053 if (intel_crtc->config->has_pch_encoder) {
5054 lpt_disable_pch_transcoder(dev_priv);
503a74e9 5055 lpt_disable_iclkip(dev_priv);
92966a37
VS
5056 intel_ddi_fdi_disable(crtc);
5057
81b088ca
VS
5058 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5059 true);
92966a37 5060 }
4f771f10
PZ
5061}
5062
2dd24552
JB
5063static void i9xx_pfit_enable(struct intel_crtc *crtc)
5064{
5065 struct drm_device *dev = crtc->base.dev;
5066 struct drm_i915_private *dev_priv = dev->dev_private;
6e3c9717 5067 struct intel_crtc_state *pipe_config = crtc->config;
2dd24552 5068
681a8504 5069 if (!pipe_config->gmch_pfit.control)
2dd24552
JB
5070 return;
5071
2dd24552 5072 /*
c0b03411
DV
5073 * The panel fitter should only be adjusted whilst the pipe is disabled,
5074 * according to register description and PRM.
2dd24552 5075 */
c0b03411
DV
5076 WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
5077 assert_pipe_disabled(dev_priv, crtc->pipe);
2dd24552 5078
b074cec8
JB
5079 I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
5080 I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
5a80c45c
DV
5081
5082 /* Border color in case we don't scale up to the full screen. Black by
5083 * default, change to something else for debugging. */
5084 I915_WRITE(BCLRPAT(crtc->pipe), 0);
2dd24552
JB
5085}
5086
d05410f9
DA
5087static enum intel_display_power_domain port_to_power_domain(enum port port)
5088{
5089 switch (port) {
5090 case PORT_A:
6331a704 5091 return POWER_DOMAIN_PORT_DDI_A_LANES;
d05410f9 5092 case PORT_B:
6331a704 5093 return POWER_DOMAIN_PORT_DDI_B_LANES;
d05410f9 5094 case PORT_C:
6331a704 5095 return POWER_DOMAIN_PORT_DDI_C_LANES;
d05410f9 5096 case PORT_D:
6331a704 5097 return POWER_DOMAIN_PORT_DDI_D_LANES;
d8e19f99 5098 case PORT_E:
6331a704 5099 return POWER_DOMAIN_PORT_DDI_E_LANES;
d05410f9 5100 default:
b9fec167 5101 MISSING_CASE(port);
d05410f9
DA
5102 return POWER_DOMAIN_PORT_OTHER;
5103 }
5104}
5105
25f78f58
VS
5106static enum intel_display_power_domain port_to_aux_power_domain(enum port port)
5107{
5108 switch (port) {
5109 case PORT_A:
5110 return POWER_DOMAIN_AUX_A;
5111 case PORT_B:
5112 return POWER_DOMAIN_AUX_B;
5113 case PORT_C:
5114 return POWER_DOMAIN_AUX_C;
5115 case PORT_D:
5116 return POWER_DOMAIN_AUX_D;
5117 case PORT_E:
5118 /* FIXME: Check VBT for actual wiring of PORT E */
5119 return POWER_DOMAIN_AUX_D;
5120 default:
b9fec167 5121 MISSING_CASE(port);
25f78f58
VS
5122 return POWER_DOMAIN_AUX_A;
5123 }
5124}
5125
319be8ae
ID
5126enum intel_display_power_domain
5127intel_display_port_power_domain(struct intel_encoder *intel_encoder)
5128{
5129 struct drm_device *dev = intel_encoder->base.dev;
5130 struct intel_digital_port *intel_dig_port;
5131
5132 switch (intel_encoder->type) {
5133 case INTEL_OUTPUT_UNKNOWN:
5134 /* Only DDI platforms should ever use this output type */
5135 WARN_ON_ONCE(!HAS_DDI(dev));
5136 case INTEL_OUTPUT_DISPLAYPORT:
5137 case INTEL_OUTPUT_HDMI:
5138 case INTEL_OUTPUT_EDP:
5139 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
d05410f9 5140 return port_to_power_domain(intel_dig_port->port);
0e32b39c
DA
5141 case INTEL_OUTPUT_DP_MST:
5142 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
5143 return port_to_power_domain(intel_dig_port->port);
319be8ae
ID
5144 case INTEL_OUTPUT_ANALOG:
5145 return POWER_DOMAIN_PORT_CRT;
5146 case INTEL_OUTPUT_DSI:
5147 return POWER_DOMAIN_PORT_DSI;
5148 default:
5149 return POWER_DOMAIN_PORT_OTHER;
5150 }
5151}
5152
25f78f58
VS
5153enum intel_display_power_domain
5154intel_display_port_aux_power_domain(struct intel_encoder *intel_encoder)
5155{
5156 struct drm_device *dev = intel_encoder->base.dev;
5157 struct intel_digital_port *intel_dig_port;
5158
5159 switch (intel_encoder->type) {
5160 case INTEL_OUTPUT_UNKNOWN:
651174a4
ID
5161 case INTEL_OUTPUT_HDMI:
5162 /*
5163 * Only DDI platforms should ever use these output types.
5164 * We can get here after the HDMI detect code has already set
5165 * the type of the shared encoder. Since we can't be sure
5166 * what's the status of the given connectors, play safe and
5167 * run the DP detection too.
5168 */
25f78f58
VS
5169 WARN_ON_ONCE(!HAS_DDI(dev));
5170 case INTEL_OUTPUT_DISPLAYPORT:
5171 case INTEL_OUTPUT_EDP:
5172 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
5173 return port_to_aux_power_domain(intel_dig_port->port);
5174 case INTEL_OUTPUT_DP_MST:
5175 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
5176 return port_to_aux_power_domain(intel_dig_port->port);
5177 default:
b9fec167 5178 MISSING_CASE(intel_encoder->type);
25f78f58
VS
5179 return POWER_DOMAIN_AUX_A;
5180 }
5181}
5182
74bff5f9
ML
5183static unsigned long get_crtc_power_domains(struct drm_crtc *crtc,
5184 struct intel_crtc_state *crtc_state)
77d22dca 5185{
319be8ae 5186 struct drm_device *dev = crtc->dev;
74bff5f9 5187 struct drm_encoder *encoder;
319be8ae
ID
5188 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5189 enum pipe pipe = intel_crtc->pipe;
77d22dca 5190 unsigned long mask;
74bff5f9 5191 enum transcoder transcoder = crtc_state->cpu_transcoder;
77d22dca 5192
74bff5f9 5193 if (!crtc_state->base.active)
292b990e
ML
5194 return 0;
5195
77d22dca
ID
5196 mask = BIT(POWER_DOMAIN_PIPE(pipe));
5197 mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
74bff5f9
ML
5198 if (crtc_state->pch_pfit.enabled ||
5199 crtc_state->pch_pfit.force_thru)
77d22dca
ID
5200 mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
5201
74bff5f9
ML
5202 drm_for_each_encoder_mask(encoder, dev, crtc_state->base.encoder_mask) {
5203 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
5204
319be8ae 5205 mask |= BIT(intel_display_port_power_domain(intel_encoder));
74bff5f9 5206 }
319be8ae 5207
15e7ec29
ML
5208 if (crtc_state->shared_dpll)
5209 mask |= BIT(POWER_DOMAIN_PLLS);
5210
77d22dca
ID
5211 return mask;
5212}
5213
74bff5f9
ML
5214static unsigned long
5215modeset_get_crtc_power_domains(struct drm_crtc *crtc,
5216 struct intel_crtc_state *crtc_state)
77d22dca 5217{
292b990e
ML
5218 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
5219 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5220 enum intel_display_power_domain domain;
5221 unsigned long domains, new_domains, old_domains;
77d22dca 5222
292b990e 5223 old_domains = intel_crtc->enabled_power_domains;
74bff5f9
ML
5224 intel_crtc->enabled_power_domains = new_domains =
5225 get_crtc_power_domains(crtc, crtc_state);
77d22dca 5226
292b990e
ML
5227 domains = new_domains & ~old_domains;
5228
5229 for_each_power_domain(domain, domains)
5230 intel_display_power_get(dev_priv, domain);
5231
5232 return old_domains & ~new_domains;
5233}
5234
5235static void modeset_put_power_domains(struct drm_i915_private *dev_priv,
5236 unsigned long domains)
5237{
5238 enum intel_display_power_domain domain;
5239
5240 for_each_power_domain(domain, domains)
5241 intel_display_power_put(dev_priv, domain);
5242}
77d22dca 5243
adafdc6f
MK
5244static int intel_compute_max_dotclk(struct drm_i915_private *dev_priv)
5245{
5246 int max_cdclk_freq = dev_priv->max_cdclk_freq;
5247
5248 if (INTEL_INFO(dev_priv)->gen >= 9 ||
5249 IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
5250 return max_cdclk_freq;
5251 else if (IS_CHERRYVIEW(dev_priv))
5252 return max_cdclk_freq*95/100;
5253 else if (INTEL_INFO(dev_priv)->gen < 4)
5254 return 2*max_cdclk_freq*90/100;
5255 else
5256 return max_cdclk_freq*90/100;
5257}
5258
560a7ae4
DL
5259static void intel_update_max_cdclk(struct drm_device *dev)
5260{
5261 struct drm_i915_private *dev_priv = dev->dev_private;
5262
ef11bdb3 5263 if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) {
560a7ae4
DL
5264 u32 limit = I915_READ(SKL_DFSM) & SKL_DFSM_CDCLK_LIMIT_MASK;
5265
5266 if (limit == SKL_DFSM_CDCLK_LIMIT_675)
5267 dev_priv->max_cdclk_freq = 675000;
5268 else if (limit == SKL_DFSM_CDCLK_LIMIT_540)
5269 dev_priv->max_cdclk_freq = 540000;
5270 else if (limit == SKL_DFSM_CDCLK_LIMIT_450)
5271 dev_priv->max_cdclk_freq = 450000;
5272 else
5273 dev_priv->max_cdclk_freq = 337500;
281c114f
MR
5274 } else if (IS_BROXTON(dev)) {
5275 dev_priv->max_cdclk_freq = 624000;
560a7ae4
DL
5276 } else if (IS_BROADWELL(dev)) {
5277 /*
5278 * FIXME with extra cooling we can allow
5279 * 540 MHz for ULX and 675 Mhz for ULT.
5280 * How can we know if extra cooling is
5281 * available? PCI ID, VTB, something else?
5282 */
5283 if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
5284 dev_priv->max_cdclk_freq = 450000;
5285 else if (IS_BDW_ULX(dev))
5286 dev_priv->max_cdclk_freq = 450000;
5287 else if (IS_BDW_ULT(dev))
5288 dev_priv->max_cdclk_freq = 540000;
5289 else
5290 dev_priv->max_cdclk_freq = 675000;
0904deaf
MK
5291 } else if (IS_CHERRYVIEW(dev)) {
5292 dev_priv->max_cdclk_freq = 320000;
560a7ae4
DL
5293 } else if (IS_VALLEYVIEW(dev)) {
5294 dev_priv->max_cdclk_freq = 400000;
5295 } else {
5296 /* otherwise assume cdclk is fixed */
5297 dev_priv->max_cdclk_freq = dev_priv->cdclk_freq;
5298 }
5299
adafdc6f
MK
5300 dev_priv->max_dotclk_freq = intel_compute_max_dotclk(dev_priv);
5301
560a7ae4
DL
5302 DRM_DEBUG_DRIVER("Max CD clock rate: %d kHz\n",
5303 dev_priv->max_cdclk_freq);
adafdc6f
MK
5304
5305 DRM_DEBUG_DRIVER("Max dotclock rate: %d kHz\n",
5306 dev_priv->max_dotclk_freq);
560a7ae4
DL
5307}
5308
5309static void intel_update_cdclk(struct drm_device *dev)
5310{
5311 struct drm_i915_private *dev_priv = dev->dev_private;
5312
5313 dev_priv->cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
5314 DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
5315 dev_priv->cdclk_freq);
5316
5317 /*
5318 * Program the gmbus_freq based on the cdclk frequency.
5319 * BSpec erroneously claims we should aim for 4MHz, but
5320 * in fact 1MHz is the correct frequency.
5321 */
666a4537 5322 if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
560a7ae4
DL
5323 /*
5324 * Program the gmbus_freq based on the cdclk frequency.
5325 * BSpec erroneously claims we should aim for 4MHz, but
5326 * in fact 1MHz is the correct frequency.
5327 */
5328 I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->cdclk_freq, 1000));
5329 }
5330
5331 if (dev_priv->max_cdclk_freq == 0)
5332 intel_update_max_cdclk(dev);
5333}
5334
70d0c574 5335static void broxton_set_cdclk(struct drm_device *dev, int frequency)
f8437dd1
VK
5336{
5337 struct drm_i915_private *dev_priv = dev->dev_private;
5338 uint32_t divider;
5339 uint32_t ratio;
5340 uint32_t current_freq;
5341 int ret;
5342
5343 /* frequency = 19.2MHz * ratio / 2 / div{1,1.5,2,4} */
5344 switch (frequency) {
5345 case 144000:
5346 divider = BXT_CDCLK_CD2X_DIV_SEL_4;
5347 ratio = BXT_DE_PLL_RATIO(60);
5348 break;
5349 case 288000:
5350 divider = BXT_CDCLK_CD2X_DIV_SEL_2;
5351 ratio = BXT_DE_PLL_RATIO(60);
5352 break;
5353 case 384000:
5354 divider = BXT_CDCLK_CD2X_DIV_SEL_1_5;
5355 ratio = BXT_DE_PLL_RATIO(60);
5356 break;
5357 case 576000:
5358 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
5359 ratio = BXT_DE_PLL_RATIO(60);
5360 break;
5361 case 624000:
5362 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
5363 ratio = BXT_DE_PLL_RATIO(65);
5364 break;
5365 case 19200:
5366 /*
5367 * Bypass frequency with DE PLL disabled. Init ratio, divider
5368 * to suppress GCC warning.
5369 */
5370 ratio = 0;
5371 divider = 0;
5372 break;
5373 default:
5374 DRM_ERROR("unsupported CDCLK freq %d", frequency);
5375
5376 return;
5377 }
5378
5379 mutex_lock(&dev_priv->rps.hw_lock);
5380 /* Inform power controller of upcoming frequency change */
5381 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
5382 0x80000000);
5383 mutex_unlock(&dev_priv->rps.hw_lock);
5384
5385 if (ret) {
5386 DRM_ERROR("PCode CDCLK freq change notify failed (err %d, freq %d)\n",
5387 ret, frequency);
5388 return;
5389 }
5390
5391 current_freq = I915_READ(CDCLK_CTL) & CDCLK_FREQ_DECIMAL_MASK;
5392 /* convert from .1 fixpoint MHz with -1MHz offset to kHz */
5393 current_freq = current_freq * 500 + 1000;
5394
5395 /*
5396 * DE PLL has to be disabled when
5397 * - setting to 19.2MHz (bypass, PLL isn't used)
5398 * - before setting to 624MHz (PLL needs toggling)
5399 * - before setting to any frequency from 624MHz (PLL needs toggling)
5400 */
5401 if (frequency == 19200 || frequency == 624000 ||
5402 current_freq == 624000) {
5403 I915_WRITE(BXT_DE_PLL_ENABLE, ~BXT_DE_PLL_PLL_ENABLE);
5404 /* Timeout 200us */
5405 if (wait_for(!(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK),
5406 1))
5407 DRM_ERROR("timout waiting for DE PLL unlock\n");
5408 }
5409
5410 if (frequency != 19200) {
5411 uint32_t val;
5412
5413 val = I915_READ(BXT_DE_PLL_CTL);
5414 val &= ~BXT_DE_PLL_RATIO_MASK;
5415 val |= ratio;
5416 I915_WRITE(BXT_DE_PLL_CTL, val);
5417
5418 I915_WRITE(BXT_DE_PLL_ENABLE, BXT_DE_PLL_PLL_ENABLE);
5419 /* Timeout 200us */
5420 if (wait_for(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK, 1))
5421 DRM_ERROR("timeout waiting for DE PLL lock\n");
5422
5423 val = I915_READ(CDCLK_CTL);
5424 val &= ~BXT_CDCLK_CD2X_DIV_SEL_MASK;
5425 val |= divider;
5426 /*
5427 * Disable SSA Precharge when CD clock frequency < 500 MHz,
5428 * enable otherwise.
5429 */
5430 val &= ~BXT_CDCLK_SSA_PRECHARGE_ENABLE;
5431 if (frequency >= 500000)
5432 val |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
5433
5434 val &= ~CDCLK_FREQ_DECIMAL_MASK;
5435 /* convert from kHz to .1 fixpoint MHz with -1MHz offset */
5436 val |= (frequency - 1000) / 500;
5437 I915_WRITE(CDCLK_CTL, val);
5438 }
5439
5440 mutex_lock(&dev_priv->rps.hw_lock);
5441 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
5442 DIV_ROUND_UP(frequency, 25000));
5443 mutex_unlock(&dev_priv->rps.hw_lock);
5444
5445 if (ret) {
5446 DRM_ERROR("PCode CDCLK freq set failed, (err %d, freq %d)\n",
5447 ret, frequency);
5448 return;
5449 }
5450
a47871bd 5451 intel_update_cdclk(dev);
f8437dd1
VK
5452}
5453
5454void broxton_init_cdclk(struct drm_device *dev)
5455{
5456 struct drm_i915_private *dev_priv = dev->dev_private;
5457 uint32_t val;
5458
5459 /*
5460 * NDE_RSTWRN_OPT RST PCH Handshake En must always be 0b on BXT
5461 * or else the reset will hang because there is no PCH to respond.
5462 * Move the handshake programming to initialization sequence.
5463 * Previously was left up to BIOS.
5464 */
5465 val = I915_READ(HSW_NDE_RSTWRN_OPT);
5466 val &= ~RESET_PCH_HANDSHAKE_ENABLE;
5467 I915_WRITE(HSW_NDE_RSTWRN_OPT, val);
5468
5469 /* Enable PG1 for cdclk */
5470 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
5471
5472 /* check if cd clock is enabled */
5473 if (I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_PLL_ENABLE) {
5474 DRM_DEBUG_KMS("Display already initialized\n");
5475 return;
5476 }
5477
5478 /*
5479 * FIXME:
5480 * - The initial CDCLK needs to be read from VBT.
5481 * Need to make this change after VBT has changes for BXT.
5482 * - check if setting the max (or any) cdclk freq is really necessary
5483 * here, it belongs to modeset time
5484 */
5485 broxton_set_cdclk(dev, 624000);
5486
5487 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
22e02c0b
VS
5488 POSTING_READ(DBUF_CTL);
5489
f8437dd1
VK
5490 udelay(10);
5491
5492 if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
5493 DRM_ERROR("DBuf power enable timeout!\n");
5494}
5495
5496void broxton_uninit_cdclk(struct drm_device *dev)
5497{
5498 struct drm_i915_private *dev_priv = dev->dev_private;
5499
5500 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
22e02c0b
VS
5501 POSTING_READ(DBUF_CTL);
5502
f8437dd1
VK
5503 udelay(10);
5504
5505 if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
5506 DRM_ERROR("DBuf power disable timeout!\n");
5507
5508 /* Set minimum (bypass) frequency, in effect turning off the DE PLL */
5509 broxton_set_cdclk(dev, 19200);
5510
5511 intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
5512}
5513
5d96d8af
DL
5514static const struct skl_cdclk_entry {
5515 unsigned int freq;
5516 unsigned int vco;
5517} skl_cdclk_frequencies[] = {
5518 { .freq = 308570, .vco = 8640 },
5519 { .freq = 337500, .vco = 8100 },
5520 { .freq = 432000, .vco = 8640 },
5521 { .freq = 450000, .vco = 8100 },
5522 { .freq = 540000, .vco = 8100 },
5523 { .freq = 617140, .vco = 8640 },
5524 { .freq = 675000, .vco = 8100 },
5525};
5526
5527static unsigned int skl_cdclk_decimal(unsigned int freq)
5528{
5529 return (freq - 1000) / 500;
5530}
5531
5532static unsigned int skl_cdclk_get_vco(unsigned int freq)
5533{
5534 unsigned int i;
5535
5536 for (i = 0; i < ARRAY_SIZE(skl_cdclk_frequencies); i++) {
5537 const struct skl_cdclk_entry *e = &skl_cdclk_frequencies[i];
5538
5539 if (e->freq == freq)
5540 return e->vco;
5541 }
5542
5543 return 8100;
5544}
5545
5546static void
5547skl_dpll0_enable(struct drm_i915_private *dev_priv, unsigned int required_vco)
5548{
5549 unsigned int min_freq;
5550 u32 val;
5551
5552 /* select the minimum CDCLK before enabling DPLL 0 */
5553 val = I915_READ(CDCLK_CTL);
5554 val &= ~CDCLK_FREQ_SEL_MASK | ~CDCLK_FREQ_DECIMAL_MASK;
5555 val |= CDCLK_FREQ_337_308;
5556
5557 if (required_vco == 8640)
5558 min_freq = 308570;
5559 else
5560 min_freq = 337500;
5561
5562 val = CDCLK_FREQ_337_308 | skl_cdclk_decimal(min_freq);
5563
5564 I915_WRITE(CDCLK_CTL, val);
5565 POSTING_READ(CDCLK_CTL);
5566
5567 /*
5568 * We always enable DPLL0 with the lowest link rate possible, but still
5569 * taking into account the VCO required to operate the eDP panel at the
5570 * desired frequency. The usual DP link rates operate with a VCO of
5571 * 8100 while the eDP 1.4 alternate link rates need a VCO of 8640.
5572 * The modeset code is responsible for the selection of the exact link
5573 * rate later on, with the constraint of choosing a frequency that
5574 * works with required_vco.
5575 */
5576 val = I915_READ(DPLL_CTRL1);
5577
5578 val &= ~(DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) | DPLL_CTRL1_SSC(SKL_DPLL0) |
5579 DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
5580 val |= DPLL_CTRL1_OVERRIDE(SKL_DPLL0);
5581 if (required_vco == 8640)
5582 val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080,
5583 SKL_DPLL0);
5584 else
5585 val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810,
5586 SKL_DPLL0);
5587
5588 I915_WRITE(DPLL_CTRL1, val);
5589 POSTING_READ(DPLL_CTRL1);
5590
5591 I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) | LCPLL_PLL_ENABLE);
5592
5593 if (wait_for(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK, 5))
5594 DRM_ERROR("DPLL0 not locked\n");
5595}
5596
5597static bool skl_cdclk_pcu_ready(struct drm_i915_private *dev_priv)
5598{
5599 int ret;
5600 u32 val;
5601
5602 /* inform PCU we want to change CDCLK */
5603 val = SKL_CDCLK_PREPARE_FOR_CHANGE;
5604 mutex_lock(&dev_priv->rps.hw_lock);
5605 ret = sandybridge_pcode_read(dev_priv, SKL_PCODE_CDCLK_CONTROL, &val);
5606 mutex_unlock(&dev_priv->rps.hw_lock);
5607
5608 return ret == 0 && (val & SKL_CDCLK_READY_FOR_CHANGE);
5609}
5610
5611static bool skl_cdclk_wait_for_pcu_ready(struct drm_i915_private *dev_priv)
5612{
5613 unsigned int i;
5614
5615 for (i = 0; i < 15; i++) {
5616 if (skl_cdclk_pcu_ready(dev_priv))
5617 return true;
5618 udelay(10);
5619 }
5620
5621 return false;
5622}
5623
5624static void skl_set_cdclk(struct drm_i915_private *dev_priv, unsigned int freq)
5625{
560a7ae4 5626 struct drm_device *dev = dev_priv->dev;
5d96d8af
DL
5627 u32 freq_select, pcu_ack;
5628
5629 DRM_DEBUG_DRIVER("Changing CDCLK to %dKHz\n", freq);
5630
5631 if (!skl_cdclk_wait_for_pcu_ready(dev_priv)) {
5632 DRM_ERROR("failed to inform PCU about cdclk change\n");
5633 return;
5634 }
5635
5636 /* set CDCLK_CTL */
5637 switch(freq) {
5638 case 450000:
5639 case 432000:
5640 freq_select = CDCLK_FREQ_450_432;
5641 pcu_ack = 1;
5642 break;
5643 case 540000:
5644 freq_select = CDCLK_FREQ_540;
5645 pcu_ack = 2;
5646 break;
5647 case 308570:
5648 case 337500:
5649 default:
5650 freq_select = CDCLK_FREQ_337_308;
5651 pcu_ack = 0;
5652 break;
5653 case 617140:
5654 case 675000:
5655 freq_select = CDCLK_FREQ_675_617;
5656 pcu_ack = 3;
5657 break;
5658 }
5659
5660 I915_WRITE(CDCLK_CTL, freq_select | skl_cdclk_decimal(freq));
5661 POSTING_READ(CDCLK_CTL);
5662
5663 /* inform PCU of the change */
5664 mutex_lock(&dev_priv->rps.hw_lock);
5665 sandybridge_pcode_write(dev_priv, SKL_PCODE_CDCLK_CONTROL, pcu_ack);
5666 mutex_unlock(&dev_priv->rps.hw_lock);
560a7ae4
DL
5667
5668 intel_update_cdclk(dev);
5d96d8af
DL
5669}
5670
5671void skl_uninit_cdclk(struct drm_i915_private *dev_priv)
5672{
5673 /* disable DBUF power */
5674 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
5675 POSTING_READ(DBUF_CTL);
5676
5677 udelay(10);
5678
5679 if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
5680 DRM_ERROR("DBuf power disable timeout\n");
5681
ab96c1ee
ID
5682 /* disable DPLL0 */
5683 I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) & ~LCPLL_PLL_ENABLE);
5684 if (wait_for(!(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK), 1))
5685 DRM_ERROR("Couldn't disable DPLL0\n");
5d96d8af
DL
5686}
5687
5688void skl_init_cdclk(struct drm_i915_private *dev_priv)
5689{
5d96d8af
DL
5690 unsigned int required_vco;
5691
39d9b85a
GW
5692 /* DPLL0 not enabled (happens on early BIOS versions) */
5693 if (!(I915_READ(LCPLL1_CTL) & LCPLL_PLL_ENABLE)) {
5694 /* enable DPLL0 */
5695 required_vco = skl_cdclk_get_vco(dev_priv->skl_boot_cdclk);
5696 skl_dpll0_enable(dev_priv, required_vco);
5d96d8af
DL
5697 }
5698
5d96d8af
DL
5699 /* set CDCLK to the frequency the BIOS chose */
5700 skl_set_cdclk(dev_priv, dev_priv->skl_boot_cdclk);
5701
5702 /* enable DBUF power */
5703 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
5704 POSTING_READ(DBUF_CTL);
5705
5706 udelay(10);
5707
5708 if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
5709 DRM_ERROR("DBuf power enable timeout\n");
5710}
5711
c73666f3
SK
5712int skl_sanitize_cdclk(struct drm_i915_private *dev_priv)
5713{
5714 uint32_t lcpll1 = I915_READ(LCPLL1_CTL);
5715 uint32_t cdctl = I915_READ(CDCLK_CTL);
5716 int freq = dev_priv->skl_boot_cdclk;
5717
f1b391a5
SK
5718 /*
5719 * check if the pre-os intialized the display
5720 * There is SWF18 scratchpad register defined which is set by the
5721 * pre-os which can be used by the OS drivers to check the status
5722 */
5723 if ((I915_READ(SWF_ILK(0x18)) & 0x00FFFFFF) == 0)
5724 goto sanitize;
5725
c73666f3
SK
5726 /* Is PLL enabled and locked ? */
5727 if (!((lcpll1 & LCPLL_PLL_ENABLE) && (lcpll1 & LCPLL_PLL_LOCK)))
5728 goto sanitize;
5729
5730 /* DPLL okay; verify the cdclock
5731 *
5732 * Noticed in some instances that the freq selection is correct but
5733 * decimal part is programmed wrong from BIOS where pre-os does not
5734 * enable display. Verify the same as well.
5735 */
5736 if (cdctl == ((cdctl & CDCLK_FREQ_SEL_MASK) | skl_cdclk_decimal(freq)))
5737 /* All well; nothing to sanitize */
5738 return false;
5739sanitize:
5740 /*
5741 * As of now initialize with max cdclk till
5742 * we get dynamic cdclk support
5743 * */
5744 dev_priv->skl_boot_cdclk = dev_priv->max_cdclk_freq;
5745 skl_init_cdclk(dev_priv);
5746
5747 /* we did have to sanitize */
5748 return true;
5749}
5750
30a970c6
JB
5751/* Adjust CDclk dividers to allow high res or save power if possible */
5752static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
5753{
5754 struct drm_i915_private *dev_priv = dev->dev_private;
5755 u32 val, cmd;
5756
164dfd28
VK
5757 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5758 != dev_priv->cdclk_freq);
d60c4473 5759
dfcab17e 5760 if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
30a970c6 5761 cmd = 2;
dfcab17e 5762 else if (cdclk == 266667)
30a970c6
JB
5763 cmd = 1;
5764 else
5765 cmd = 0;
5766
5767 mutex_lock(&dev_priv->rps.hw_lock);
5768 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5769 val &= ~DSPFREQGUAR_MASK;
5770 val |= (cmd << DSPFREQGUAR_SHIFT);
5771 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5772 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5773 DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
5774 50)) {
5775 DRM_ERROR("timed out waiting for CDclk change\n");
5776 }
5777 mutex_unlock(&dev_priv->rps.hw_lock);
5778
54433e91
VS
5779 mutex_lock(&dev_priv->sb_lock);
5780
dfcab17e 5781 if (cdclk == 400000) {
6bcda4f0 5782 u32 divider;
30a970c6 5783
6bcda4f0 5784 divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
30a970c6 5785
30a970c6
JB
5786 /* adjust cdclk divider */
5787 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
87d5d259 5788 val &= ~CCK_FREQUENCY_VALUES;
30a970c6
JB
5789 val |= divider;
5790 vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
a877e801
VS
5791
5792 if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
87d5d259 5793 CCK_FREQUENCY_STATUS) == (divider << CCK_FREQUENCY_STATUS_SHIFT),
a877e801
VS
5794 50))
5795 DRM_ERROR("timed out waiting for CDclk change\n");
30a970c6
JB
5796 }
5797
30a970c6
JB
5798 /* adjust self-refresh exit latency value */
5799 val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
5800 val &= ~0x7f;
5801
5802 /*
5803 * For high bandwidth configs, we set a higher latency in the bunit
5804 * so that the core display fetch happens in time to avoid underruns.
5805 */
dfcab17e 5806 if (cdclk == 400000)
30a970c6
JB
5807 val |= 4500 / 250; /* 4.5 usec */
5808 else
5809 val |= 3000 / 250; /* 3.0 usec */
5810 vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
54433e91 5811
a580516d 5812 mutex_unlock(&dev_priv->sb_lock);
30a970c6 5813
b6283055 5814 intel_update_cdclk(dev);
30a970c6
JB
5815}
5816
383c5a6a
VS
5817static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
5818{
5819 struct drm_i915_private *dev_priv = dev->dev_private;
5820 u32 val, cmd;
5821
164dfd28
VK
5822 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5823 != dev_priv->cdclk_freq);
383c5a6a
VS
5824
5825 switch (cdclk) {
383c5a6a
VS
5826 case 333333:
5827 case 320000:
383c5a6a 5828 case 266667:
383c5a6a 5829 case 200000:
383c5a6a
VS
5830 break;
5831 default:
5f77eeb0 5832 MISSING_CASE(cdclk);
383c5a6a
VS
5833 return;
5834 }
5835
9d0d3fda
VS
5836 /*
5837 * Specs are full of misinformation, but testing on actual
5838 * hardware has shown that we just need to write the desired
5839 * CCK divider into the Punit register.
5840 */
5841 cmd = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
5842
383c5a6a
VS
5843 mutex_lock(&dev_priv->rps.hw_lock);
5844 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5845 val &= ~DSPFREQGUAR_MASK_CHV;
5846 val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
5847 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5848 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5849 DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
5850 50)) {
5851 DRM_ERROR("timed out waiting for CDclk change\n");
5852 }
5853 mutex_unlock(&dev_priv->rps.hw_lock);
5854
b6283055 5855 intel_update_cdclk(dev);
383c5a6a
VS
5856}
5857
30a970c6
JB
5858static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
5859 int max_pixclk)
5860{
6bcda4f0 5861 int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
6cca3195 5862 int limit = IS_CHERRYVIEW(dev_priv) ? 95 : 90;
29dc7ef3 5863
30a970c6
JB
5864 /*
5865 * Really only a few cases to deal with, as only 4 CDclks are supported:
5866 * 200MHz
5867 * 267MHz
29dc7ef3 5868 * 320/333MHz (depends on HPLL freq)
6cca3195
VS
5869 * 400MHz (VLV only)
5870 * So we check to see whether we're above 90% (VLV) or 95% (CHV)
5871 * of the lower bin and adjust if needed.
e37c67a1
VS
5872 *
5873 * We seem to get an unstable or solid color picture at 200MHz.
5874 * Not sure what's wrong. For now use 200MHz only when all pipes
5875 * are off.
30a970c6 5876 */
6cca3195
VS
5877 if (!IS_CHERRYVIEW(dev_priv) &&
5878 max_pixclk > freq_320*limit/100)
dfcab17e 5879 return 400000;
6cca3195 5880 else if (max_pixclk > 266667*limit/100)
29dc7ef3 5881 return freq_320;
e37c67a1 5882 else if (max_pixclk > 0)
dfcab17e 5883 return 266667;
e37c67a1
VS
5884 else
5885 return 200000;
30a970c6
JB
5886}
5887
f8437dd1
VK
5888static int broxton_calc_cdclk(struct drm_i915_private *dev_priv,
5889 int max_pixclk)
5890{
5891 /*
5892 * FIXME:
5893 * - remove the guardband, it's not needed on BXT
5894 * - set 19.2MHz bypass frequency if there are no active pipes
5895 */
5896 if (max_pixclk > 576000*9/10)
5897 return 624000;
5898 else if (max_pixclk > 384000*9/10)
5899 return 576000;
5900 else if (max_pixclk > 288000*9/10)
5901 return 384000;
5902 else if (max_pixclk > 144000*9/10)
5903 return 288000;
5904 else
5905 return 144000;
5906}
5907
e8788cbc 5908/* Compute the max pixel clock for new configuration. */
a821fc46
ACO
5909static int intel_mode_max_pixclk(struct drm_device *dev,
5910 struct drm_atomic_state *state)
30a970c6 5911{
565602d7
ML
5912 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
5913 struct drm_i915_private *dev_priv = dev->dev_private;
5914 struct drm_crtc *crtc;
5915 struct drm_crtc_state *crtc_state;
5916 unsigned max_pixclk = 0, i;
5917 enum pipe pipe;
30a970c6 5918
565602d7
ML
5919 memcpy(intel_state->min_pixclk, dev_priv->min_pixclk,
5920 sizeof(intel_state->min_pixclk));
304603f4 5921
565602d7
ML
5922 for_each_crtc_in_state(state, crtc, crtc_state, i) {
5923 int pixclk = 0;
5924
5925 if (crtc_state->enable)
5926 pixclk = crtc_state->adjusted_mode.crtc_clock;
304603f4 5927
565602d7 5928 intel_state->min_pixclk[i] = pixclk;
30a970c6
JB
5929 }
5930
565602d7
ML
5931 for_each_pipe(dev_priv, pipe)
5932 max_pixclk = max(intel_state->min_pixclk[pipe], max_pixclk);
5933
30a970c6
JB
5934 return max_pixclk;
5935}
5936
27c329ed 5937static int valleyview_modeset_calc_cdclk(struct drm_atomic_state *state)
30a970c6 5938{
27c329ed
ML
5939 struct drm_device *dev = state->dev;
5940 struct drm_i915_private *dev_priv = dev->dev_private;
5941 int max_pixclk = intel_mode_max_pixclk(dev, state);
1a617b77
ML
5942 struct intel_atomic_state *intel_state =
5943 to_intel_atomic_state(state);
30a970c6 5944
304603f4
ACO
5945 if (max_pixclk < 0)
5946 return max_pixclk;
30a970c6 5947
1a617b77 5948 intel_state->cdclk = intel_state->dev_cdclk =
27c329ed 5949 valleyview_calc_cdclk(dev_priv, max_pixclk);
0a9ab303 5950
1a617b77
ML
5951 if (!intel_state->active_crtcs)
5952 intel_state->dev_cdclk = valleyview_calc_cdclk(dev_priv, 0);
5953
27c329ed
ML
5954 return 0;
5955}
304603f4 5956
27c329ed
ML
5957static int broxton_modeset_calc_cdclk(struct drm_atomic_state *state)
5958{
5959 struct drm_device *dev = state->dev;
5960 struct drm_i915_private *dev_priv = dev->dev_private;
5961 int max_pixclk = intel_mode_max_pixclk(dev, state);
1a617b77
ML
5962 struct intel_atomic_state *intel_state =
5963 to_intel_atomic_state(state);
85a96e7a 5964
27c329ed
ML
5965 if (max_pixclk < 0)
5966 return max_pixclk;
85a96e7a 5967
1a617b77 5968 intel_state->cdclk = intel_state->dev_cdclk =
27c329ed 5969 broxton_calc_cdclk(dev_priv, max_pixclk);
85a96e7a 5970
1a617b77
ML
5971 if (!intel_state->active_crtcs)
5972 intel_state->dev_cdclk = broxton_calc_cdclk(dev_priv, 0);
5973
27c329ed 5974 return 0;
30a970c6
JB
5975}
5976
1e69cd74
VS
5977static void vlv_program_pfi_credits(struct drm_i915_private *dev_priv)
5978{
5979 unsigned int credits, default_credits;
5980
5981 if (IS_CHERRYVIEW(dev_priv))
5982 default_credits = PFI_CREDIT(12);
5983 else
5984 default_credits = PFI_CREDIT(8);
5985
bfa7df01 5986 if (dev_priv->cdclk_freq >= dev_priv->czclk_freq) {
1e69cd74
VS
5987 /* CHV suggested value is 31 or 63 */
5988 if (IS_CHERRYVIEW(dev_priv))
fcc0008f 5989 credits = PFI_CREDIT_63;
1e69cd74
VS
5990 else
5991 credits = PFI_CREDIT(15);
5992 } else {
5993 credits = default_credits;
5994 }
5995
5996 /*
5997 * WA - write default credits before re-programming
5998 * FIXME: should we also set the resend bit here?
5999 */
6000 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
6001 default_credits);
6002
6003 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
6004 credits | PFI_CREDIT_RESEND);
6005
6006 /*
6007 * FIXME is this guaranteed to clear
6008 * immediately or should we poll for it?
6009 */
6010 WARN_ON(I915_READ(GCI_CONTROL) & PFI_CREDIT_RESEND);
6011}
6012
27c329ed 6013static void valleyview_modeset_commit_cdclk(struct drm_atomic_state *old_state)
30a970c6 6014{
a821fc46 6015 struct drm_device *dev = old_state->dev;
30a970c6 6016 struct drm_i915_private *dev_priv = dev->dev_private;
1a617b77
ML
6017 struct intel_atomic_state *old_intel_state =
6018 to_intel_atomic_state(old_state);
6019 unsigned req_cdclk = old_intel_state->dev_cdclk;
30a970c6 6020
27c329ed
ML
6021 /*
6022 * FIXME: We can end up here with all power domains off, yet
6023 * with a CDCLK frequency other than the minimum. To account
6024 * for this take the PIPE-A power domain, which covers the HW
6025 * blocks needed for the following programming. This can be
6026 * removed once it's guaranteed that we get here either with
6027 * the minimum CDCLK set, or the required power domains
6028 * enabled.
6029 */
6030 intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
738c05c0 6031
27c329ed
ML
6032 if (IS_CHERRYVIEW(dev))
6033 cherryview_set_cdclk(dev, req_cdclk);
6034 else
6035 valleyview_set_cdclk(dev, req_cdclk);
738c05c0 6036
27c329ed 6037 vlv_program_pfi_credits(dev_priv);
1e69cd74 6038
27c329ed 6039 intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
30a970c6
JB
6040}
6041
89b667f8
JB
6042static void valleyview_crtc_enable(struct drm_crtc *crtc)
6043{
6044 struct drm_device *dev = crtc->dev;
a72e4c9f 6045 struct drm_i915_private *dev_priv = to_i915(dev);
89b667f8
JB
6046 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6047 struct intel_encoder *encoder;
b95c5321
ML
6048 struct intel_crtc_state *pipe_config =
6049 to_intel_crtc_state(crtc->state);
89b667f8 6050 int pipe = intel_crtc->pipe;
89b667f8 6051
53d9f4e9 6052 if (WARN_ON(intel_crtc->active))
89b667f8
JB
6053 return;
6054
6e3c9717 6055 if (intel_crtc->config->has_dp_encoder)
fe3cd48d 6056 intel_dp_set_m_n(intel_crtc, M1_N1);
5b18e57c
DV
6057
6058 intel_set_pipe_timings(intel_crtc);
bc58be60 6059 intel_set_pipe_src_size(intel_crtc);
5b18e57c 6060
c14b0485
VS
6061 if (IS_CHERRYVIEW(dev) && pipe == PIPE_B) {
6062 struct drm_i915_private *dev_priv = dev->dev_private;
6063
6064 I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
6065 I915_WRITE(CHV_CANVAS(pipe), 0);
6066 }
6067
5b18e57c
DV
6068 i9xx_set_pipeconf(intel_crtc);
6069
89b667f8 6070 intel_crtc->active = true;
89b667f8 6071
a72e4c9f 6072 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4a3436e8 6073
89b667f8
JB
6074 for_each_encoder_on_crtc(dev, crtc, encoder)
6075 if (encoder->pre_pll_enable)
6076 encoder->pre_pll_enable(encoder);
6077
a65347ba 6078 if (!intel_crtc->config->has_dsi_encoder) {
c0b4c660
VS
6079 if (IS_CHERRYVIEW(dev)) {
6080 chv_prepare_pll(intel_crtc, intel_crtc->config);
6e3c9717 6081 chv_enable_pll(intel_crtc, intel_crtc->config);
c0b4c660
VS
6082 } else {
6083 vlv_prepare_pll(intel_crtc, intel_crtc->config);
6e3c9717 6084 vlv_enable_pll(intel_crtc, intel_crtc->config);
c0b4c660 6085 }
9d556c99 6086 }
89b667f8
JB
6087
6088 for_each_encoder_on_crtc(dev, crtc, encoder)
6089 if (encoder->pre_enable)
6090 encoder->pre_enable(encoder);
6091
2dd24552
JB
6092 i9xx_pfit_enable(intel_crtc);
6093
b95c5321 6094 intel_color_load_luts(&pipe_config->base);
63cbb074 6095
caed361d 6096 intel_update_watermarks(crtc);
e1fdc473 6097 intel_enable_pipe(intel_crtc);
be6a6f8e 6098
4b3a9526
VS
6099 assert_vblank_disabled(crtc);
6100 drm_crtc_vblank_on(crtc);
6101
f9b61ff6
DV
6102 for_each_encoder_on_crtc(dev, crtc, encoder)
6103 encoder->enable(encoder);
89b667f8
JB
6104}
6105
f13c2ef3
DV
6106static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
6107{
6108 struct drm_device *dev = crtc->base.dev;
6109 struct drm_i915_private *dev_priv = dev->dev_private;
6110
6e3c9717
ACO
6111 I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
6112 I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
f13c2ef3
DV
6113}
6114
0b8765c6 6115static void i9xx_crtc_enable(struct drm_crtc *crtc)
79e53945
JB
6116{
6117 struct drm_device *dev = crtc->dev;
a72e4c9f 6118 struct drm_i915_private *dev_priv = to_i915(dev);
79e53945 6119 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 6120 struct intel_encoder *encoder;
b95c5321
ML
6121 struct intel_crtc_state *pipe_config =
6122 to_intel_crtc_state(crtc->state);
79e53945 6123 int pipe = intel_crtc->pipe;
79e53945 6124
53d9f4e9 6125 if (WARN_ON(intel_crtc->active))
f7abfe8b
CW
6126 return;
6127
f13c2ef3
DV
6128 i9xx_set_pll_dividers(intel_crtc);
6129
6e3c9717 6130 if (intel_crtc->config->has_dp_encoder)
fe3cd48d 6131 intel_dp_set_m_n(intel_crtc, M1_N1);
5b18e57c
DV
6132
6133 intel_set_pipe_timings(intel_crtc);
bc58be60 6134 intel_set_pipe_src_size(intel_crtc);
5b18e57c 6135
5b18e57c
DV
6136 i9xx_set_pipeconf(intel_crtc);
6137
f7abfe8b 6138 intel_crtc->active = true;
6b383a7f 6139
4a3436e8 6140 if (!IS_GEN2(dev))
a72e4c9f 6141 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4a3436e8 6142
9d6d9f19
MK
6143 for_each_encoder_on_crtc(dev, crtc, encoder)
6144 if (encoder->pre_enable)
6145 encoder->pre_enable(encoder);
6146
f6736a1a
DV
6147 i9xx_enable_pll(intel_crtc);
6148
2dd24552
JB
6149 i9xx_pfit_enable(intel_crtc);
6150
b95c5321 6151 intel_color_load_luts(&pipe_config->base);
63cbb074 6152
f37fcc2a 6153 intel_update_watermarks(crtc);
e1fdc473 6154 intel_enable_pipe(intel_crtc);
be6a6f8e 6155
4b3a9526
VS
6156 assert_vblank_disabled(crtc);
6157 drm_crtc_vblank_on(crtc);
6158
f9b61ff6
DV
6159 for_each_encoder_on_crtc(dev, crtc, encoder)
6160 encoder->enable(encoder);
0b8765c6 6161}
79e53945 6162
87476d63
DV
6163static void i9xx_pfit_disable(struct intel_crtc *crtc)
6164{
6165 struct drm_device *dev = crtc->base.dev;
6166 struct drm_i915_private *dev_priv = dev->dev_private;
87476d63 6167
6e3c9717 6168 if (!crtc->config->gmch_pfit.control)
328d8e82 6169 return;
87476d63 6170
328d8e82 6171 assert_pipe_disabled(dev_priv, crtc->pipe);
87476d63 6172
328d8e82
DV
6173 DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
6174 I915_READ(PFIT_CONTROL));
6175 I915_WRITE(PFIT_CONTROL, 0);
87476d63
DV
6176}
6177
0b8765c6
JB
6178static void i9xx_crtc_disable(struct drm_crtc *crtc)
6179{
6180 struct drm_device *dev = crtc->dev;
6181 struct drm_i915_private *dev_priv = dev->dev_private;
6182 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 6183 struct intel_encoder *encoder;
0b8765c6 6184 int pipe = intel_crtc->pipe;
ef9c3aee 6185
6304cd91
VS
6186 /*
6187 * On gen2 planes are double buffered but the pipe isn't, so we must
6188 * wait for planes to fully turn off before disabling the pipe.
6189 */
90e83e53
ACO
6190 if (IS_GEN2(dev))
6191 intel_wait_for_vblank(dev, pipe);
6304cd91 6192
4b3a9526
VS
6193 for_each_encoder_on_crtc(dev, crtc, encoder)
6194 encoder->disable(encoder);
6195
f9b61ff6
DV
6196 drm_crtc_vblank_off(crtc);
6197 assert_vblank_disabled(crtc);
6198
575f7ab7 6199 intel_disable_pipe(intel_crtc);
24a1f16d 6200
87476d63 6201 i9xx_pfit_disable(intel_crtc);
24a1f16d 6202
89b667f8
JB
6203 for_each_encoder_on_crtc(dev, crtc, encoder)
6204 if (encoder->post_disable)
6205 encoder->post_disable(encoder);
6206
a65347ba 6207 if (!intel_crtc->config->has_dsi_encoder) {
076ed3b2
CML
6208 if (IS_CHERRYVIEW(dev))
6209 chv_disable_pll(dev_priv, pipe);
6210 else if (IS_VALLEYVIEW(dev))
6211 vlv_disable_pll(dev_priv, pipe);
6212 else
1c4e0274 6213 i9xx_disable_pll(intel_crtc);
076ed3b2 6214 }
0b8765c6 6215
d6db995f
VS
6216 for_each_encoder_on_crtc(dev, crtc, encoder)
6217 if (encoder->post_pll_disable)
6218 encoder->post_pll_disable(encoder);
6219
4a3436e8 6220 if (!IS_GEN2(dev))
a72e4c9f 6221 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
0b8765c6
JB
6222}
6223
b17d48e2
ML
6224static void intel_crtc_disable_noatomic(struct drm_crtc *crtc)
6225{
842e0307 6226 struct intel_encoder *encoder;
b17d48e2
ML
6227 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6228 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
6229 enum intel_display_power_domain domain;
6230 unsigned long domains;
6231
6232 if (!intel_crtc->active)
6233 return;
6234
a539205a 6235 if (to_intel_plane_state(crtc->primary->state)->visible) {
fc32b1fd
ML
6236 WARN_ON(intel_crtc->unpin_work);
6237
2622a081 6238 intel_pre_disable_primary_noatomic(crtc);
54a41961
ML
6239
6240 intel_crtc_disable_planes(crtc, 1 << drm_plane_index(crtc->primary));
6241 to_intel_plane_state(crtc->primary->state)->visible = false;
a539205a
ML
6242 }
6243
b17d48e2 6244 dev_priv->display.crtc_disable(crtc);
842e0307
ML
6245
6246 DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was enabled, now disabled\n",
6247 crtc->base.id);
6248
6249 WARN_ON(drm_atomic_set_mode_for_crtc(crtc->state, NULL) < 0);
6250 crtc->state->active = false;
37d9078b 6251 intel_crtc->active = false;
842e0307
ML
6252 crtc->enabled = false;
6253 crtc->state->connector_mask = 0;
6254 crtc->state->encoder_mask = 0;
6255
6256 for_each_encoder_on_crtc(crtc->dev, crtc, encoder)
6257 encoder->base.crtc = NULL;
6258
58f9c0bc 6259 intel_fbc_disable(intel_crtc);
37d9078b 6260 intel_update_watermarks(crtc);
1f7457b1 6261 intel_disable_shared_dpll(intel_crtc);
b17d48e2
ML
6262
6263 domains = intel_crtc->enabled_power_domains;
6264 for_each_power_domain(domain, domains)
6265 intel_display_power_put(dev_priv, domain);
6266 intel_crtc->enabled_power_domains = 0;
565602d7
ML
6267
6268 dev_priv->active_crtcs &= ~(1 << intel_crtc->pipe);
6269 dev_priv->min_pixclk[intel_crtc->pipe] = 0;
b17d48e2
ML
6270}
6271
6b72d486
ML
6272/*
6273 * turn all crtc's off, but do not adjust state
6274 * This has to be paired with a call to intel_modeset_setup_hw_state.
6275 */
70e0bd74 6276int intel_display_suspend(struct drm_device *dev)
ee7b9f93 6277{
e2c8b870 6278 struct drm_i915_private *dev_priv = to_i915(dev);
70e0bd74 6279 struct drm_atomic_state *state;
e2c8b870 6280 int ret;
70e0bd74 6281
e2c8b870
ML
6282 state = drm_atomic_helper_suspend(dev);
6283 ret = PTR_ERR_OR_ZERO(state);
70e0bd74
ML
6284 if (ret)
6285 DRM_ERROR("Suspending crtc's failed with %i\n", ret);
e2c8b870
ML
6286 else
6287 dev_priv->modeset_restore_state = state;
70e0bd74 6288 return ret;
ee7b9f93
JB
6289}
6290
ea5b213a 6291void intel_encoder_destroy(struct drm_encoder *encoder)
7e7d76c3 6292{
4ef69c7a 6293 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
ea5b213a 6294
ea5b213a
CW
6295 drm_encoder_cleanup(encoder);
6296 kfree(intel_encoder);
7e7d76c3
JB
6297}
6298
0a91ca29
DV
6299/* Cross check the actual hw state with our own modeset state tracking (and it's
6300 * internal consistency). */
c0ead703 6301static void intel_connector_verify_state(struct intel_connector *connector)
79e53945 6302{
35dd3c64
ML
6303 struct drm_crtc *crtc = connector->base.state->crtc;
6304
6305 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
6306 connector->base.base.id,
6307 connector->base.name);
6308
0a91ca29 6309 if (connector->get_hw_state(connector)) {
e85376cb 6310 struct intel_encoder *encoder = connector->encoder;
35dd3c64 6311 struct drm_connector_state *conn_state = connector->base.state;
0a91ca29 6312
35dd3c64
ML
6313 I915_STATE_WARN(!crtc,
6314 "connector enabled without attached crtc\n");
0a91ca29 6315
35dd3c64
ML
6316 if (!crtc)
6317 return;
6318
6319 I915_STATE_WARN(!crtc->state->active,
6320 "connector is active, but attached crtc isn't\n");
6321
e85376cb 6322 if (!encoder || encoder->type == INTEL_OUTPUT_DP_MST)
35dd3c64
ML
6323 return;
6324
e85376cb 6325 I915_STATE_WARN(conn_state->best_encoder != &encoder->base,
35dd3c64
ML
6326 "atomic encoder doesn't match attached encoder\n");
6327
e85376cb 6328 I915_STATE_WARN(conn_state->crtc != encoder->base.crtc,
35dd3c64
ML
6329 "attached encoder crtc differs from connector crtc\n");
6330 } else {
4d688a2a
ML
6331 I915_STATE_WARN(crtc && crtc->state->active,
6332 "attached crtc is active, but connector isn't\n");
35dd3c64
ML
6333 I915_STATE_WARN(!crtc && connector->base.state->best_encoder,
6334 "best encoder set without crtc!\n");
0a91ca29 6335 }
79e53945
JB
6336}
6337
08d9bc92
ACO
6338int intel_connector_init(struct intel_connector *connector)
6339{
5350a031 6340 drm_atomic_helper_connector_reset(&connector->base);
08d9bc92 6341
5350a031 6342 if (!connector->base.state)
08d9bc92
ACO
6343 return -ENOMEM;
6344
08d9bc92
ACO
6345 return 0;
6346}
6347
6348struct intel_connector *intel_connector_alloc(void)
6349{
6350 struct intel_connector *connector;
6351
6352 connector = kzalloc(sizeof *connector, GFP_KERNEL);
6353 if (!connector)
6354 return NULL;
6355
6356 if (intel_connector_init(connector) < 0) {
6357 kfree(connector);
6358 return NULL;
6359 }
6360
6361 return connector;
6362}
6363
f0947c37
DV
6364/* Simple connector->get_hw_state implementation for encoders that support only
6365 * one connector and no cloning and hence the encoder state determines the state
6366 * of the connector. */
6367bool intel_connector_get_hw_state(struct intel_connector *connector)
ea5b213a 6368{
24929352 6369 enum pipe pipe = 0;
f0947c37 6370 struct intel_encoder *encoder = connector->encoder;
ea5b213a 6371
f0947c37 6372 return encoder->get_hw_state(encoder, &pipe);
ea5b213a
CW
6373}
6374
6d293983 6375static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
d272ddfa 6376{
6d293983
ACO
6377 if (crtc_state->base.enable && crtc_state->has_pch_encoder)
6378 return crtc_state->fdi_lanes;
d272ddfa
VS
6379
6380 return 0;
6381}
6382
6d293983 6383static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
5cec258b 6384 struct intel_crtc_state *pipe_config)
1857e1da 6385{
6d293983
ACO
6386 struct drm_atomic_state *state = pipe_config->base.state;
6387 struct intel_crtc *other_crtc;
6388 struct intel_crtc_state *other_crtc_state;
6389
1857e1da
DV
6390 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
6391 pipe_name(pipe), pipe_config->fdi_lanes);
6392 if (pipe_config->fdi_lanes > 4) {
6393 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
6394 pipe_name(pipe), pipe_config->fdi_lanes);
6d293983 6395 return -EINVAL;
1857e1da
DV
6396 }
6397
bafb6553 6398 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
1857e1da
DV
6399 if (pipe_config->fdi_lanes > 2) {
6400 DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
6401 pipe_config->fdi_lanes);
6d293983 6402 return -EINVAL;
1857e1da 6403 } else {
6d293983 6404 return 0;
1857e1da
DV
6405 }
6406 }
6407
6408 if (INTEL_INFO(dev)->num_pipes == 2)
6d293983 6409 return 0;
1857e1da
DV
6410
6411 /* Ivybridge 3 pipe is really complicated */
6412 switch (pipe) {
6413 case PIPE_A:
6d293983 6414 return 0;
1857e1da 6415 case PIPE_B:
6d293983
ACO
6416 if (pipe_config->fdi_lanes <= 2)
6417 return 0;
6418
6419 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_C));
6420 other_crtc_state =
6421 intel_atomic_get_crtc_state(state, other_crtc);
6422 if (IS_ERR(other_crtc_state))
6423 return PTR_ERR(other_crtc_state);
6424
6425 if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
1857e1da
DV
6426 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
6427 pipe_name(pipe), pipe_config->fdi_lanes);
6d293983 6428 return -EINVAL;
1857e1da 6429 }
6d293983 6430 return 0;
1857e1da 6431 case PIPE_C:
251cc67c
VS
6432 if (pipe_config->fdi_lanes > 2) {
6433 DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
6434 pipe_name(pipe), pipe_config->fdi_lanes);
6d293983 6435 return -EINVAL;
251cc67c 6436 }
6d293983
ACO
6437
6438 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_B));
6439 other_crtc_state =
6440 intel_atomic_get_crtc_state(state, other_crtc);
6441 if (IS_ERR(other_crtc_state))
6442 return PTR_ERR(other_crtc_state);
6443
6444 if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
1857e1da 6445 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
6d293983 6446 return -EINVAL;
1857e1da 6447 }
6d293983 6448 return 0;
1857e1da
DV
6449 default:
6450 BUG();
6451 }
6452}
6453
e29c22c0
DV
6454#define RETRY 1
6455static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
5cec258b 6456 struct intel_crtc_state *pipe_config)
877d48d5 6457{
1857e1da 6458 struct drm_device *dev = intel_crtc->base.dev;
7c5f93b0 6459 const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
6d293983
ACO
6460 int lane, link_bw, fdi_dotclock, ret;
6461 bool needs_recompute = false;
877d48d5 6462
e29c22c0 6463retry:
877d48d5
DV
6464 /* FDI is a binary signal running at ~2.7GHz, encoding
6465 * each output octet as 10 bits. The actual frequency
6466 * is stored as a divider into a 100MHz clock, and the
6467 * mode pixel clock is stored in units of 1KHz.
6468 * Hence the bw of each lane in terms of the mode signal
6469 * is:
6470 */
21a727b3 6471 link_bw = intel_fdi_link_freq(to_i915(dev), pipe_config);
877d48d5 6472
241bfc38 6473 fdi_dotclock = adjusted_mode->crtc_clock;
877d48d5 6474
2bd89a07 6475 lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
877d48d5
DV
6476 pipe_config->pipe_bpp);
6477
6478 pipe_config->fdi_lanes = lane;
6479
2bd89a07 6480 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
877d48d5 6481 link_bw, &pipe_config->fdi_m_n);
1857e1da 6482
e3b247da 6483 ret = ironlake_check_fdi_lanes(dev, intel_crtc->pipe, pipe_config);
6d293983 6484 if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
e29c22c0
DV
6485 pipe_config->pipe_bpp -= 2*3;
6486 DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
6487 pipe_config->pipe_bpp);
6488 needs_recompute = true;
6489 pipe_config->bw_constrained = true;
6490
6491 goto retry;
6492 }
6493
6494 if (needs_recompute)
6495 return RETRY;
6496
6d293983 6497 return ret;
877d48d5
DV
6498}
6499
8cfb3407
VS
6500static bool pipe_config_supports_ips(struct drm_i915_private *dev_priv,
6501 struct intel_crtc_state *pipe_config)
6502{
6503 if (pipe_config->pipe_bpp > 24)
6504 return false;
6505
6506 /* HSW can handle pixel rate up to cdclk? */
2d1fe073 6507 if (IS_HASWELL(dev_priv))
8cfb3407
VS
6508 return true;
6509
6510 /*
b432e5cf
VS
6511 * We compare against max which means we must take
6512 * the increased cdclk requirement into account when
6513 * calculating the new cdclk.
6514 *
6515 * Should measure whether using a lower cdclk w/o IPS
8cfb3407
VS
6516 */
6517 return ilk_pipe_pixel_rate(pipe_config) <=
6518 dev_priv->max_cdclk_freq * 95 / 100;
6519}
6520
42db64ef 6521static void hsw_compute_ips_config(struct intel_crtc *crtc,
5cec258b 6522 struct intel_crtc_state *pipe_config)
42db64ef 6523{
8cfb3407
VS
6524 struct drm_device *dev = crtc->base.dev;
6525 struct drm_i915_private *dev_priv = dev->dev_private;
6526
d330a953 6527 pipe_config->ips_enabled = i915.enable_ips &&
8cfb3407
VS
6528 hsw_crtc_supports_ips(crtc) &&
6529 pipe_config_supports_ips(dev_priv, pipe_config);
42db64ef
PZ
6530}
6531
39acb4aa
VS
6532static bool intel_crtc_supports_double_wide(const struct intel_crtc *crtc)
6533{
6534 const struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6535
6536 /* GDG double wide on either pipe, otherwise pipe A only */
6537 return INTEL_INFO(dev_priv)->gen < 4 &&
6538 (crtc->pipe == PIPE_A || IS_I915G(dev_priv));
6539}
6540
a43f6e0f 6541static int intel_crtc_compute_config(struct intel_crtc *crtc,
5cec258b 6542 struct intel_crtc_state *pipe_config)
79e53945 6543{
a43f6e0f 6544 struct drm_device *dev = crtc->base.dev;
8bd31e67 6545 struct drm_i915_private *dev_priv = dev->dev_private;
7c5f93b0 6546 const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
89749350 6547
ad3a4479 6548 /* FIXME should check pixel clock limits on all platforms */
cf532bb2 6549 if (INTEL_INFO(dev)->gen < 4) {
39acb4aa 6550 int clock_limit = dev_priv->max_cdclk_freq * 9 / 10;
cf532bb2
VS
6551
6552 /*
39acb4aa 6553 * Enable double wide mode when the dot clock
cf532bb2 6554 * is > 90% of the (display) core speed.
cf532bb2 6555 */
39acb4aa
VS
6556 if (intel_crtc_supports_double_wide(crtc) &&
6557 adjusted_mode->crtc_clock > clock_limit) {
ad3a4479 6558 clock_limit *= 2;
cf532bb2 6559 pipe_config->double_wide = true;
ad3a4479
VS
6560 }
6561
39acb4aa
VS
6562 if (adjusted_mode->crtc_clock > clock_limit) {
6563 DRM_DEBUG_KMS("requested pixel clock (%d kHz) too high (max: %d kHz, double wide: %s)\n",
6564 adjusted_mode->crtc_clock, clock_limit,
6565 yesno(pipe_config->double_wide));
e29c22c0 6566 return -EINVAL;
39acb4aa 6567 }
2c07245f 6568 }
89749350 6569
1d1d0e27
VS
6570 /*
6571 * Pipe horizontal size must be even in:
6572 * - DVO ganged mode
6573 * - LVDS dual channel mode
6574 * - Double wide pipe
6575 */
a93e255f 6576 if ((intel_pipe_will_have_type(pipe_config, INTEL_OUTPUT_LVDS) &&
1d1d0e27
VS
6577 intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
6578 pipe_config->pipe_src_w &= ~1;
6579
8693a824
DL
6580 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
6581 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
44f46b42
CW
6582 */
6583 if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
aad941d5 6584 adjusted_mode->crtc_hsync_start == adjusted_mode->crtc_hdisplay)
e29c22c0 6585 return -EINVAL;
44f46b42 6586
f5adf94e 6587 if (HAS_IPS(dev))
a43f6e0f
DV
6588 hsw_compute_ips_config(crtc, pipe_config);
6589
877d48d5 6590 if (pipe_config->has_pch_encoder)
a43f6e0f 6591 return ironlake_fdi_compute_config(crtc, pipe_config);
877d48d5 6592
cf5a15be 6593 return 0;
79e53945
JB
6594}
6595
1652d19e
VS
6596static int skylake_get_display_clock_speed(struct drm_device *dev)
6597{
6598 struct drm_i915_private *dev_priv = to_i915(dev);
6599 uint32_t lcpll1 = I915_READ(LCPLL1_CTL);
6600 uint32_t cdctl = I915_READ(CDCLK_CTL);
6601 uint32_t linkrate;
6602
414355a7 6603 if (!(lcpll1 & LCPLL_PLL_ENABLE))
1652d19e 6604 return 24000; /* 24MHz is the cd freq with NSSC ref */
1652d19e
VS
6605
6606 if ((cdctl & CDCLK_FREQ_SEL_MASK) == CDCLK_FREQ_540)
6607 return 540000;
6608
6609 linkrate = (I915_READ(DPLL_CTRL1) &
71cd8423 6610 DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0)) >> 1;
1652d19e 6611
71cd8423
DL
6612 if (linkrate == DPLL_CTRL1_LINK_RATE_2160 ||
6613 linkrate == DPLL_CTRL1_LINK_RATE_1080) {
1652d19e
VS
6614 /* vco 8640 */
6615 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6616 case CDCLK_FREQ_450_432:
6617 return 432000;
6618 case CDCLK_FREQ_337_308:
6619 return 308570;
6620 case CDCLK_FREQ_675_617:
6621 return 617140;
6622 default:
6623 WARN(1, "Unknown cd freq selection\n");
6624 }
6625 } else {
6626 /* vco 8100 */
6627 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6628 case CDCLK_FREQ_450_432:
6629 return 450000;
6630 case CDCLK_FREQ_337_308:
6631 return 337500;
6632 case CDCLK_FREQ_675_617:
6633 return 675000;
6634 default:
6635 WARN(1, "Unknown cd freq selection\n");
6636 }
6637 }
6638
6639 /* error case, do as if DPLL0 isn't enabled */
6640 return 24000;
6641}
6642
acd3f3d3
BP
6643static int broxton_get_display_clock_speed(struct drm_device *dev)
6644{
6645 struct drm_i915_private *dev_priv = to_i915(dev);
6646 uint32_t cdctl = I915_READ(CDCLK_CTL);
6647 uint32_t pll_ratio = I915_READ(BXT_DE_PLL_CTL) & BXT_DE_PLL_RATIO_MASK;
6648 uint32_t pll_enab = I915_READ(BXT_DE_PLL_ENABLE);
6649 int cdclk;
6650
6651 if (!(pll_enab & BXT_DE_PLL_PLL_ENABLE))
6652 return 19200;
6653
6654 cdclk = 19200 * pll_ratio / 2;
6655
6656 switch (cdctl & BXT_CDCLK_CD2X_DIV_SEL_MASK) {
6657 case BXT_CDCLK_CD2X_DIV_SEL_1:
6658 return cdclk; /* 576MHz or 624MHz */
6659 case BXT_CDCLK_CD2X_DIV_SEL_1_5:
6660 return cdclk * 2 / 3; /* 384MHz */
6661 case BXT_CDCLK_CD2X_DIV_SEL_2:
6662 return cdclk / 2; /* 288MHz */
6663 case BXT_CDCLK_CD2X_DIV_SEL_4:
6664 return cdclk / 4; /* 144MHz */
6665 }
6666
6667 /* error case, do as if DE PLL isn't enabled */
6668 return 19200;
6669}
6670
1652d19e
VS
6671static int broadwell_get_display_clock_speed(struct drm_device *dev)
6672{
6673 struct drm_i915_private *dev_priv = dev->dev_private;
6674 uint32_t lcpll = I915_READ(LCPLL_CTL);
6675 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6676
6677 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6678 return 800000;
6679 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6680 return 450000;
6681 else if (freq == LCPLL_CLK_FREQ_450)
6682 return 450000;
6683 else if (freq == LCPLL_CLK_FREQ_54O_BDW)
6684 return 540000;
6685 else if (freq == LCPLL_CLK_FREQ_337_5_BDW)
6686 return 337500;
6687 else
6688 return 675000;
6689}
6690
6691static int haswell_get_display_clock_speed(struct drm_device *dev)
6692{
6693 struct drm_i915_private *dev_priv = dev->dev_private;
6694 uint32_t lcpll = I915_READ(LCPLL_CTL);
6695 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6696
6697 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6698 return 800000;
6699 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6700 return 450000;
6701 else if (freq == LCPLL_CLK_FREQ_450)
6702 return 450000;
6703 else if (IS_HSW_ULT(dev))
6704 return 337500;
6705 else
6706 return 540000;
79e53945
JB
6707}
6708
25eb05fc
JB
6709static int valleyview_get_display_clock_speed(struct drm_device *dev)
6710{
bfa7df01
VS
6711 return vlv_get_cck_clock_hpll(to_i915(dev), "cdclk",
6712 CCK_DISPLAY_CLOCK_CONTROL);
25eb05fc
JB
6713}
6714
b37a6434
VS
6715static int ilk_get_display_clock_speed(struct drm_device *dev)
6716{
6717 return 450000;
6718}
6719
e70236a8
JB
6720static int i945_get_display_clock_speed(struct drm_device *dev)
6721{
6722 return 400000;
6723}
79e53945 6724
e70236a8 6725static int i915_get_display_clock_speed(struct drm_device *dev)
79e53945 6726{
e907f170 6727 return 333333;
e70236a8 6728}
79e53945 6729
e70236a8
JB
6730static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
6731{
6732 return 200000;
6733}
79e53945 6734
257a7ffc
DV
6735static int pnv_get_display_clock_speed(struct drm_device *dev)
6736{
6737 u16 gcfgc = 0;
6738
6739 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6740
6741 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6742 case GC_DISPLAY_CLOCK_267_MHZ_PNV:
e907f170 6743 return 266667;
257a7ffc 6744 case GC_DISPLAY_CLOCK_333_MHZ_PNV:
e907f170 6745 return 333333;
257a7ffc 6746 case GC_DISPLAY_CLOCK_444_MHZ_PNV:
e907f170 6747 return 444444;
257a7ffc
DV
6748 case GC_DISPLAY_CLOCK_200_MHZ_PNV:
6749 return 200000;
6750 default:
6751 DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
6752 case GC_DISPLAY_CLOCK_133_MHZ_PNV:
e907f170 6753 return 133333;
257a7ffc 6754 case GC_DISPLAY_CLOCK_167_MHZ_PNV:
e907f170 6755 return 166667;
257a7ffc
DV
6756 }
6757}
6758
e70236a8
JB
6759static int i915gm_get_display_clock_speed(struct drm_device *dev)
6760{
6761 u16 gcfgc = 0;
79e53945 6762
e70236a8
JB
6763 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6764
6765 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
e907f170 6766 return 133333;
e70236a8
JB
6767 else {
6768 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6769 case GC_DISPLAY_CLOCK_333_MHZ:
e907f170 6770 return 333333;
e70236a8
JB
6771 default:
6772 case GC_DISPLAY_CLOCK_190_200_MHZ:
6773 return 190000;
79e53945 6774 }
e70236a8
JB
6775 }
6776}
6777
6778static int i865_get_display_clock_speed(struct drm_device *dev)
6779{
e907f170 6780 return 266667;
e70236a8
JB
6781}
6782
1b1d2716 6783static int i85x_get_display_clock_speed(struct drm_device *dev)
e70236a8
JB
6784{
6785 u16 hpllcc = 0;
1b1d2716 6786
65cd2b3f
VS
6787 /*
6788 * 852GM/852GMV only supports 133 MHz and the HPLLCC
6789 * encoding is different :(
6790 * FIXME is this the right way to detect 852GM/852GMV?
6791 */
6792 if (dev->pdev->revision == 0x1)
6793 return 133333;
6794
1b1d2716
VS
6795 pci_bus_read_config_word(dev->pdev->bus,
6796 PCI_DEVFN(0, 3), HPLLCC, &hpllcc);
6797
e70236a8
JB
6798 /* Assume that the hardware is in the high speed state. This
6799 * should be the default.
6800 */
6801 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
6802 case GC_CLOCK_133_200:
1b1d2716 6803 case GC_CLOCK_133_200_2:
e70236a8
JB
6804 case GC_CLOCK_100_200:
6805 return 200000;
6806 case GC_CLOCK_166_250:
6807 return 250000;
6808 case GC_CLOCK_100_133:
e907f170 6809 return 133333;
1b1d2716
VS
6810 case GC_CLOCK_133_266:
6811 case GC_CLOCK_133_266_2:
6812 case GC_CLOCK_166_266:
6813 return 266667;
e70236a8 6814 }
79e53945 6815
e70236a8
JB
6816 /* Shouldn't happen */
6817 return 0;
6818}
79e53945 6819
e70236a8
JB
6820static int i830_get_display_clock_speed(struct drm_device *dev)
6821{
e907f170 6822 return 133333;
79e53945
JB
6823}
6824
34edce2f
VS
6825static unsigned int intel_hpll_vco(struct drm_device *dev)
6826{
6827 struct drm_i915_private *dev_priv = dev->dev_private;
6828 static const unsigned int blb_vco[8] = {
6829 [0] = 3200000,
6830 [1] = 4000000,
6831 [2] = 5333333,
6832 [3] = 4800000,
6833 [4] = 6400000,
6834 };
6835 static const unsigned int pnv_vco[8] = {
6836 [0] = 3200000,
6837 [1] = 4000000,
6838 [2] = 5333333,
6839 [3] = 4800000,
6840 [4] = 2666667,
6841 };
6842 static const unsigned int cl_vco[8] = {
6843 [0] = 3200000,
6844 [1] = 4000000,
6845 [2] = 5333333,
6846 [3] = 6400000,
6847 [4] = 3333333,
6848 [5] = 3566667,
6849 [6] = 4266667,
6850 };
6851 static const unsigned int elk_vco[8] = {
6852 [0] = 3200000,
6853 [1] = 4000000,
6854 [2] = 5333333,
6855 [3] = 4800000,
6856 };
6857 static const unsigned int ctg_vco[8] = {
6858 [0] = 3200000,
6859 [1] = 4000000,
6860 [2] = 5333333,
6861 [3] = 6400000,
6862 [4] = 2666667,
6863 [5] = 4266667,
6864 };
6865 const unsigned int *vco_table;
6866 unsigned int vco;
6867 uint8_t tmp = 0;
6868
6869 /* FIXME other chipsets? */
6870 if (IS_GM45(dev))
6871 vco_table = ctg_vco;
6872 else if (IS_G4X(dev))
6873 vco_table = elk_vco;
6874 else if (IS_CRESTLINE(dev))
6875 vco_table = cl_vco;
6876 else if (IS_PINEVIEW(dev))
6877 vco_table = pnv_vco;
6878 else if (IS_G33(dev))
6879 vco_table = blb_vco;
6880 else
6881 return 0;
6882
6883 tmp = I915_READ(IS_MOBILE(dev) ? HPLLVCO_MOBILE : HPLLVCO);
6884
6885 vco = vco_table[tmp & 0x7];
6886 if (vco == 0)
6887 DRM_ERROR("Bad HPLL VCO (HPLLVCO=0x%02x)\n", tmp);
6888 else
6889 DRM_DEBUG_KMS("HPLL VCO %u kHz\n", vco);
6890
6891 return vco;
6892}
6893
6894static int gm45_get_display_clock_speed(struct drm_device *dev)
6895{
6896 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
6897 uint16_t tmp = 0;
6898
6899 pci_read_config_word(dev->pdev, GCFGC, &tmp);
6900
6901 cdclk_sel = (tmp >> 12) & 0x1;
6902
6903 switch (vco) {
6904 case 2666667:
6905 case 4000000:
6906 case 5333333:
6907 return cdclk_sel ? 333333 : 222222;
6908 case 3200000:
6909 return cdclk_sel ? 320000 : 228571;
6910 default:
6911 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u, CFGC=0x%04x\n", vco, tmp);
6912 return 222222;
6913 }
6914}
6915
6916static int i965gm_get_display_clock_speed(struct drm_device *dev)
6917{
6918 static const uint8_t div_3200[] = { 16, 10, 8 };
6919 static const uint8_t div_4000[] = { 20, 12, 10 };
6920 static const uint8_t div_5333[] = { 24, 16, 14 };
6921 const uint8_t *div_table;
6922 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
6923 uint16_t tmp = 0;
6924
6925 pci_read_config_word(dev->pdev, GCFGC, &tmp);
6926
6927 cdclk_sel = ((tmp >> 8) & 0x1f) - 1;
6928
6929 if (cdclk_sel >= ARRAY_SIZE(div_3200))
6930 goto fail;
6931
6932 switch (vco) {
6933 case 3200000:
6934 div_table = div_3200;
6935 break;
6936 case 4000000:
6937 div_table = div_4000;
6938 break;
6939 case 5333333:
6940 div_table = div_5333;
6941 break;
6942 default:
6943 goto fail;
6944 }
6945
6946 return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
6947
caf4e252 6948fail:
34edce2f
VS
6949 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%04x\n", vco, tmp);
6950 return 200000;
6951}
6952
6953static int g33_get_display_clock_speed(struct drm_device *dev)
6954{
6955 static const uint8_t div_3200[] = { 12, 10, 8, 7, 5, 16 };
6956 static const uint8_t div_4000[] = { 14, 12, 10, 8, 6, 20 };
6957 static const uint8_t div_4800[] = { 20, 14, 12, 10, 8, 24 };
6958 static const uint8_t div_5333[] = { 20, 16, 12, 12, 8, 28 };
6959 const uint8_t *div_table;
6960 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
6961 uint16_t tmp = 0;
6962
6963 pci_read_config_word(dev->pdev, GCFGC, &tmp);
6964
6965 cdclk_sel = (tmp >> 4) & 0x7;
6966
6967 if (cdclk_sel >= ARRAY_SIZE(div_3200))
6968 goto fail;
6969
6970 switch (vco) {
6971 case 3200000:
6972 div_table = div_3200;
6973 break;
6974 case 4000000:
6975 div_table = div_4000;
6976 break;
6977 case 4800000:
6978 div_table = div_4800;
6979 break;
6980 case 5333333:
6981 div_table = div_5333;
6982 break;
6983 default:
6984 goto fail;
6985 }
6986
6987 return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
6988
caf4e252 6989fail:
34edce2f
VS
6990 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%08x\n", vco, tmp);
6991 return 190476;
6992}
6993
2c07245f 6994static void
a65851af 6995intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
2c07245f 6996{
a65851af
VS
6997 while (*num > DATA_LINK_M_N_MASK ||
6998 *den > DATA_LINK_M_N_MASK) {
2c07245f
ZW
6999 *num >>= 1;
7000 *den >>= 1;
7001 }
7002}
7003
a65851af
VS
7004static void compute_m_n(unsigned int m, unsigned int n,
7005 uint32_t *ret_m, uint32_t *ret_n)
7006{
7007 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
7008 *ret_m = div_u64((uint64_t) m * *ret_n, n);
7009 intel_reduce_m_n_ratio(ret_m, ret_n);
7010}
7011
e69d0bc1
DV
7012void
7013intel_link_compute_m_n(int bits_per_pixel, int nlanes,
7014 int pixel_clock, int link_clock,
7015 struct intel_link_m_n *m_n)
2c07245f 7016{
e69d0bc1 7017 m_n->tu = 64;
a65851af
VS
7018
7019 compute_m_n(bits_per_pixel * pixel_clock,
7020 link_clock * nlanes * 8,
7021 &m_n->gmch_m, &m_n->gmch_n);
7022
7023 compute_m_n(pixel_clock, link_clock,
7024 &m_n->link_m, &m_n->link_n);
2c07245f
ZW
7025}
7026
a7615030
CW
7027static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
7028{
d330a953
JN
7029 if (i915.panel_use_ssc >= 0)
7030 return i915.panel_use_ssc != 0;
41aa3448 7031 return dev_priv->vbt.lvds_use_ssc
435793df 7032 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
a7615030
CW
7033}
7034
7429e9d4 7035static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
c65d77d8 7036{
7df00d7a 7037 return (1 << dpll->n) << 16 | dpll->m2;
7429e9d4 7038}
f47709a9 7039
7429e9d4
DV
7040static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
7041{
7042 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
c65d77d8
JB
7043}
7044
f47709a9 7045static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
190f68c5 7046 struct intel_crtc_state *crtc_state,
a7516a05
JB
7047 intel_clock_t *reduced_clock)
7048{
f47709a9 7049 struct drm_device *dev = crtc->base.dev;
a7516a05
JB
7050 u32 fp, fp2 = 0;
7051
7052 if (IS_PINEVIEW(dev)) {
190f68c5 7053 fp = pnv_dpll_compute_fp(&crtc_state->dpll);
a7516a05 7054 if (reduced_clock)
7429e9d4 7055 fp2 = pnv_dpll_compute_fp(reduced_clock);
a7516a05 7056 } else {
190f68c5 7057 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
a7516a05 7058 if (reduced_clock)
7429e9d4 7059 fp2 = i9xx_dpll_compute_fp(reduced_clock);
a7516a05
JB
7060 }
7061
190f68c5 7062 crtc_state->dpll_hw_state.fp0 = fp;
a7516a05 7063
f47709a9 7064 crtc->lowfreq_avail = false;
a93e255f 7065 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
ab585dea 7066 reduced_clock) {
190f68c5 7067 crtc_state->dpll_hw_state.fp1 = fp2;
f47709a9 7068 crtc->lowfreq_avail = true;
a7516a05 7069 } else {
190f68c5 7070 crtc_state->dpll_hw_state.fp1 = fp;
a7516a05
JB
7071 }
7072}
7073
5e69f97f
CML
7074static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
7075 pipe)
89b667f8
JB
7076{
7077 u32 reg_val;
7078
7079 /*
7080 * PLLB opamp always calibrates to max value of 0x3f, force enable it
7081 * and set it to a reasonable value instead.
7082 */
ab3c759a 7083 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
89b667f8
JB
7084 reg_val &= 0xffffff00;
7085 reg_val |= 0x00000030;
ab3c759a 7086 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
89b667f8 7087
ab3c759a 7088 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
89b667f8
JB
7089 reg_val &= 0x8cffffff;
7090 reg_val = 0x8c000000;
ab3c759a 7091 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
89b667f8 7092
ab3c759a 7093 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
89b667f8 7094 reg_val &= 0xffffff00;
ab3c759a 7095 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
89b667f8 7096
ab3c759a 7097 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
89b667f8
JB
7098 reg_val &= 0x00ffffff;
7099 reg_val |= 0xb0000000;
ab3c759a 7100 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
89b667f8
JB
7101}
7102
b551842d
DV
7103static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
7104 struct intel_link_m_n *m_n)
7105{
7106 struct drm_device *dev = crtc->base.dev;
7107 struct drm_i915_private *dev_priv = dev->dev_private;
7108 int pipe = crtc->pipe;
7109
e3b95f1e
DV
7110 I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7111 I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
7112 I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
7113 I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
b551842d
DV
7114}
7115
7116static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
f769cd24
VK
7117 struct intel_link_m_n *m_n,
7118 struct intel_link_m_n *m2_n2)
b551842d
DV
7119{
7120 struct drm_device *dev = crtc->base.dev;
7121 struct drm_i915_private *dev_priv = dev->dev_private;
7122 int pipe = crtc->pipe;
6e3c9717 7123 enum transcoder transcoder = crtc->config->cpu_transcoder;
b551842d
DV
7124
7125 if (INTEL_INFO(dev)->gen >= 5) {
7126 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
7127 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
7128 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
7129 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
f769cd24
VK
7130 /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
7131 * for gen < 8) and if DRRS is supported (to make sure the
7132 * registers are not unnecessarily accessed).
7133 */
44395bfe 7134 if (m2_n2 && (IS_CHERRYVIEW(dev) || INTEL_INFO(dev)->gen < 8) &&
6e3c9717 7135 crtc->config->has_drrs) {
f769cd24
VK
7136 I915_WRITE(PIPE_DATA_M2(transcoder),
7137 TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
7138 I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
7139 I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
7140 I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
7141 }
b551842d 7142 } else {
e3b95f1e
DV
7143 I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7144 I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
7145 I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
7146 I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
b551842d
DV
7147 }
7148}
7149
fe3cd48d 7150void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
03afc4a2 7151{
fe3cd48d
R
7152 struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
7153
7154 if (m_n == M1_N1) {
7155 dp_m_n = &crtc->config->dp_m_n;
7156 dp_m2_n2 = &crtc->config->dp_m2_n2;
7157 } else if (m_n == M2_N2) {
7158
7159 /*
7160 * M2_N2 registers are not supported. Hence m2_n2 divider value
7161 * needs to be programmed into M1_N1.
7162 */
7163 dp_m_n = &crtc->config->dp_m2_n2;
7164 } else {
7165 DRM_ERROR("Unsupported divider value\n");
7166 return;
7167 }
7168
6e3c9717
ACO
7169 if (crtc->config->has_pch_encoder)
7170 intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
03afc4a2 7171 else
fe3cd48d 7172 intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
03afc4a2
DV
7173}
7174
251ac862
DV
7175static void vlv_compute_dpll(struct intel_crtc *crtc,
7176 struct intel_crtc_state *pipe_config)
bdd4b6a6 7177{
03ed5cbf
VS
7178 pipe_config->dpll_hw_state.dpll = DPLL_INTEGRATED_REF_CLK_VLV |
7179 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS |
7180 DPLL_VCO_ENABLE | DPLL_EXT_BUFFER_ENABLE_VLV;
7181 if (crtc->pipe != PIPE_A)
7182 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
bdd4b6a6 7183
03ed5cbf
VS
7184 pipe_config->dpll_hw_state.dpll_md =
7185 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
7186}
bdd4b6a6 7187
03ed5cbf
VS
7188static void chv_compute_dpll(struct intel_crtc *crtc,
7189 struct intel_crtc_state *pipe_config)
7190{
7191 pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLK_CHV |
7192 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS |
7193 DPLL_VCO_ENABLE;
7194 if (crtc->pipe != PIPE_A)
7195 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
7196
7197 pipe_config->dpll_hw_state.dpll_md =
7198 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
bdd4b6a6
DV
7199}
7200
d288f65f 7201static void vlv_prepare_pll(struct intel_crtc *crtc,
5cec258b 7202 const struct intel_crtc_state *pipe_config)
a0c4da24 7203{
f47709a9 7204 struct drm_device *dev = crtc->base.dev;
a0c4da24 7205 struct drm_i915_private *dev_priv = dev->dev_private;
f47709a9 7206 int pipe = crtc->pipe;
bdd4b6a6 7207 u32 mdiv;
a0c4da24 7208 u32 bestn, bestm1, bestm2, bestp1, bestp2;
bdd4b6a6 7209 u32 coreclk, reg_val;
a0c4da24 7210
a580516d 7211 mutex_lock(&dev_priv->sb_lock);
09153000 7212
d288f65f
VS
7213 bestn = pipe_config->dpll.n;
7214 bestm1 = pipe_config->dpll.m1;
7215 bestm2 = pipe_config->dpll.m2;
7216 bestp1 = pipe_config->dpll.p1;
7217 bestp2 = pipe_config->dpll.p2;
a0c4da24 7218
89b667f8
JB
7219 /* See eDP HDMI DPIO driver vbios notes doc */
7220
7221 /* PLL B needs special handling */
bdd4b6a6 7222 if (pipe == PIPE_B)
5e69f97f 7223 vlv_pllb_recal_opamp(dev_priv, pipe);
89b667f8
JB
7224
7225 /* Set up Tx target for periodic Rcomp update */
ab3c759a 7226 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
89b667f8
JB
7227
7228 /* Disable target IRef on PLL */
ab3c759a 7229 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
89b667f8 7230 reg_val &= 0x00ffffff;
ab3c759a 7231 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
89b667f8
JB
7232
7233 /* Disable fast lock */
ab3c759a 7234 vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
89b667f8
JB
7235
7236 /* Set idtafcrecal before PLL is enabled */
a0c4da24
JB
7237 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
7238 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
7239 mdiv |= ((bestn << DPIO_N_SHIFT));
a0c4da24 7240 mdiv |= (1 << DPIO_K_SHIFT);
7df5080b
JB
7241
7242 /*
7243 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
7244 * but we don't support that).
7245 * Note: don't use the DAC post divider as it seems unstable.
7246 */
7247 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
ab3c759a 7248 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
a0c4da24 7249
a0c4da24 7250 mdiv |= DPIO_ENABLE_CALIBRATION;
ab3c759a 7251 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
a0c4da24 7252
89b667f8 7253 /* Set HBR and RBR LPF coefficients */
d288f65f 7254 if (pipe_config->port_clock == 162000 ||
409ee761
ACO
7255 intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG) ||
7256 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
ab3c759a 7257 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
885b0120 7258 0x009f0003);
89b667f8 7259 else
ab3c759a 7260 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
89b667f8
JB
7261 0x00d0000f);
7262
681a8504 7263 if (pipe_config->has_dp_encoder) {
89b667f8 7264 /* Use SSC source */
bdd4b6a6 7265 if (pipe == PIPE_A)
ab3c759a 7266 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
7267 0x0df40000);
7268 else
ab3c759a 7269 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
7270 0x0df70000);
7271 } else { /* HDMI or VGA */
7272 /* Use bend source */
bdd4b6a6 7273 if (pipe == PIPE_A)
ab3c759a 7274 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
7275 0x0df70000);
7276 else
ab3c759a 7277 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
7278 0x0df40000);
7279 }
a0c4da24 7280
ab3c759a 7281 coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
89b667f8 7282 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
409ee761
ACO
7283 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
7284 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
89b667f8 7285 coreclk |= 0x01000000;
ab3c759a 7286 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
a0c4da24 7287
ab3c759a 7288 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
a580516d 7289 mutex_unlock(&dev_priv->sb_lock);
a0c4da24
JB
7290}
7291
d288f65f 7292static void chv_prepare_pll(struct intel_crtc *crtc,
5cec258b 7293 const struct intel_crtc_state *pipe_config)
9d556c99
CML
7294{
7295 struct drm_device *dev = crtc->base.dev;
7296 struct drm_i915_private *dev_priv = dev->dev_private;
7297 int pipe = crtc->pipe;
f0f59a00 7298 i915_reg_t dpll_reg = DPLL(crtc->pipe);
9d556c99 7299 enum dpio_channel port = vlv_pipe_to_channel(pipe);
9cbe40c1 7300 u32 loopfilter, tribuf_calcntr;
9d556c99 7301 u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
a945ce7e 7302 u32 dpio_val;
9cbe40c1 7303 int vco;
9d556c99 7304
d288f65f
VS
7305 bestn = pipe_config->dpll.n;
7306 bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
7307 bestm1 = pipe_config->dpll.m1;
7308 bestm2 = pipe_config->dpll.m2 >> 22;
7309 bestp1 = pipe_config->dpll.p1;
7310 bestp2 = pipe_config->dpll.p2;
9cbe40c1 7311 vco = pipe_config->dpll.vco;
a945ce7e 7312 dpio_val = 0;
9cbe40c1 7313 loopfilter = 0;
9d556c99
CML
7314
7315 /*
7316 * Enable Refclk and SSC
7317 */
a11b0703 7318 I915_WRITE(dpll_reg,
d288f65f 7319 pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
a11b0703 7320
a580516d 7321 mutex_lock(&dev_priv->sb_lock);
9d556c99 7322
9d556c99
CML
7323 /* p1 and p2 divider */
7324 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
7325 5 << DPIO_CHV_S1_DIV_SHIFT |
7326 bestp1 << DPIO_CHV_P1_DIV_SHIFT |
7327 bestp2 << DPIO_CHV_P2_DIV_SHIFT |
7328 1 << DPIO_CHV_K_DIV_SHIFT);
7329
7330 /* Feedback post-divider - m2 */
7331 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
7332
7333 /* Feedback refclk divider - n and m1 */
7334 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
7335 DPIO_CHV_M1_DIV_BY_2 |
7336 1 << DPIO_CHV_N_DIV_SHIFT);
7337
7338 /* M2 fraction division */
25a25dfc 7339 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
9d556c99
CML
7340
7341 /* M2 fraction division enable */
a945ce7e
VP
7342 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
7343 dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
7344 dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
7345 if (bestm2_frac)
7346 dpio_val |= DPIO_CHV_FRAC_DIV_EN;
7347 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
9d556c99 7348
de3a0fde
VP
7349 /* Program digital lock detect threshold */
7350 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
7351 dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
7352 DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
7353 dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
7354 if (!bestm2_frac)
7355 dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
7356 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
7357
9d556c99 7358 /* Loop filter */
9cbe40c1
VP
7359 if (vco == 5400000) {
7360 loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
7361 loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
7362 loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
7363 tribuf_calcntr = 0x9;
7364 } else if (vco <= 6200000) {
7365 loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
7366 loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
7367 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7368 tribuf_calcntr = 0x9;
7369 } else if (vco <= 6480000) {
7370 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7371 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7372 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7373 tribuf_calcntr = 0x8;
7374 } else {
7375 /* Not supported. Apply the same limits as in the max case */
7376 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7377 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7378 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7379 tribuf_calcntr = 0;
7380 }
9d556c99
CML
7381 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
7382
968040b2 7383 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
9cbe40c1
VP
7384 dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
7385 dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
7386 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
7387
9d556c99
CML
7388 /* AFC Recal */
7389 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
7390 vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
7391 DPIO_AFC_RECAL);
7392
a580516d 7393 mutex_unlock(&dev_priv->sb_lock);
9d556c99
CML
7394}
7395
d288f65f
VS
7396/**
7397 * vlv_force_pll_on - forcibly enable just the PLL
7398 * @dev_priv: i915 private structure
7399 * @pipe: pipe PLL to enable
7400 * @dpll: PLL configuration
7401 *
7402 * Enable the PLL for @pipe using the supplied @dpll config. To be used
7403 * in cases where we need the PLL enabled even when @pipe is not going to
7404 * be enabled.
7405 */
3f36b937
TU
7406int vlv_force_pll_on(struct drm_device *dev, enum pipe pipe,
7407 const struct dpll *dpll)
d288f65f
VS
7408{
7409 struct intel_crtc *crtc =
7410 to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
3f36b937
TU
7411 struct intel_crtc_state *pipe_config;
7412
7413 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
7414 if (!pipe_config)
7415 return -ENOMEM;
7416
7417 pipe_config->base.crtc = &crtc->base;
7418 pipe_config->pixel_multiplier = 1;
7419 pipe_config->dpll = *dpll;
d288f65f
VS
7420
7421 if (IS_CHERRYVIEW(dev)) {
3f36b937
TU
7422 chv_compute_dpll(crtc, pipe_config);
7423 chv_prepare_pll(crtc, pipe_config);
7424 chv_enable_pll(crtc, pipe_config);
d288f65f 7425 } else {
3f36b937
TU
7426 vlv_compute_dpll(crtc, pipe_config);
7427 vlv_prepare_pll(crtc, pipe_config);
7428 vlv_enable_pll(crtc, pipe_config);
d288f65f 7429 }
3f36b937
TU
7430
7431 kfree(pipe_config);
7432
7433 return 0;
d288f65f
VS
7434}
7435
7436/**
7437 * vlv_force_pll_off - forcibly disable just the PLL
7438 * @dev_priv: i915 private structure
7439 * @pipe: pipe PLL to disable
7440 *
7441 * Disable the PLL for @pipe. To be used in cases where we need
7442 * the PLL enabled even when @pipe is not going to be enabled.
7443 */
7444void vlv_force_pll_off(struct drm_device *dev, enum pipe pipe)
7445{
7446 if (IS_CHERRYVIEW(dev))
7447 chv_disable_pll(to_i915(dev), pipe);
7448 else
7449 vlv_disable_pll(to_i915(dev), pipe);
7450}
7451
251ac862
DV
7452static void i9xx_compute_dpll(struct intel_crtc *crtc,
7453 struct intel_crtc_state *crtc_state,
ceb41007 7454 intel_clock_t *reduced_clock)
eb1cbe48 7455{
f47709a9 7456 struct drm_device *dev = crtc->base.dev;
eb1cbe48 7457 struct drm_i915_private *dev_priv = dev->dev_private;
eb1cbe48
DV
7458 u32 dpll;
7459 bool is_sdvo;
190f68c5 7460 struct dpll *clock = &crtc_state->dpll;
eb1cbe48 7461
190f68c5 7462 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
2a8f64ca 7463
a93e255f
ACO
7464 is_sdvo = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO) ||
7465 intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI);
eb1cbe48
DV
7466
7467 dpll = DPLL_VGA_MODE_DIS;
7468
a93e255f 7469 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
eb1cbe48
DV
7470 dpll |= DPLLB_MODE_LVDS;
7471 else
7472 dpll |= DPLLB_MODE_DAC_SERIAL;
6cc5f341 7473
ef1b460d 7474 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
190f68c5 7475 dpll |= (crtc_state->pixel_multiplier - 1)
198a037f 7476 << SDVO_MULTIPLIER_SHIFT_HIRES;
eb1cbe48 7477 }
198a037f
DV
7478
7479 if (is_sdvo)
4a33e48d 7480 dpll |= DPLL_SDVO_HIGH_SPEED;
198a037f 7481
190f68c5 7482 if (crtc_state->has_dp_encoder)
4a33e48d 7483 dpll |= DPLL_SDVO_HIGH_SPEED;
eb1cbe48
DV
7484
7485 /* compute bitmask from p1 value */
7486 if (IS_PINEVIEW(dev))
7487 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
7488 else {
7489 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7490 if (IS_G4X(dev) && reduced_clock)
7491 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
7492 }
7493 switch (clock->p2) {
7494 case 5:
7495 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
7496 break;
7497 case 7:
7498 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
7499 break;
7500 case 10:
7501 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
7502 break;
7503 case 14:
7504 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
7505 break;
7506 }
7507 if (INTEL_INFO(dev)->gen >= 4)
7508 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
7509
190f68c5 7510 if (crtc_state->sdvo_tv_clock)
eb1cbe48 7511 dpll |= PLL_REF_INPUT_TVCLKINBC;
a93e255f 7512 else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
ceb41007 7513 intel_panel_use_ssc(dev_priv))
eb1cbe48
DV
7514 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7515 else
7516 dpll |= PLL_REF_INPUT_DREFCLK;
7517
7518 dpll |= DPLL_VCO_ENABLE;
190f68c5 7519 crtc_state->dpll_hw_state.dpll = dpll;
8bcc2795 7520
eb1cbe48 7521 if (INTEL_INFO(dev)->gen >= 4) {
190f68c5 7522 u32 dpll_md = (crtc_state->pixel_multiplier - 1)
ef1b460d 7523 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
190f68c5 7524 crtc_state->dpll_hw_state.dpll_md = dpll_md;
eb1cbe48
DV
7525 }
7526}
7527
251ac862
DV
7528static void i8xx_compute_dpll(struct intel_crtc *crtc,
7529 struct intel_crtc_state *crtc_state,
ceb41007 7530 intel_clock_t *reduced_clock)
eb1cbe48 7531{
f47709a9 7532 struct drm_device *dev = crtc->base.dev;
eb1cbe48 7533 struct drm_i915_private *dev_priv = dev->dev_private;
eb1cbe48 7534 u32 dpll;
190f68c5 7535 struct dpll *clock = &crtc_state->dpll;
eb1cbe48 7536
190f68c5 7537 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
2a8f64ca 7538
eb1cbe48
DV
7539 dpll = DPLL_VGA_MODE_DIS;
7540
a93e255f 7541 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
eb1cbe48
DV
7542 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7543 } else {
7544 if (clock->p1 == 2)
7545 dpll |= PLL_P1_DIVIDE_BY_TWO;
7546 else
7547 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7548 if (clock->p2 == 4)
7549 dpll |= PLL_P2_DIVIDE_BY_4;
7550 }
7551
a93e255f 7552 if (!IS_I830(dev) && intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
4a33e48d
DV
7553 dpll |= DPLL_DVO_2X_MODE;
7554
a93e255f 7555 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
ceb41007 7556 intel_panel_use_ssc(dev_priv))
eb1cbe48
DV
7557 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7558 else
7559 dpll |= PLL_REF_INPUT_DREFCLK;
7560
7561 dpll |= DPLL_VCO_ENABLE;
190f68c5 7562 crtc_state->dpll_hw_state.dpll = dpll;
eb1cbe48
DV
7563}
7564
8a654f3b 7565static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
b0e77b9c
PZ
7566{
7567 struct drm_device *dev = intel_crtc->base.dev;
7568 struct drm_i915_private *dev_priv = dev->dev_private;
7569 enum pipe pipe = intel_crtc->pipe;
6e3c9717 7570 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
7c5f93b0 7571 const struct drm_display_mode *adjusted_mode = &intel_crtc->config->base.adjusted_mode;
1caea6e9
VS
7572 uint32_t crtc_vtotal, crtc_vblank_end;
7573 int vsyncshift = 0;
4d8a62ea
DV
7574
7575 /* We need to be careful not to changed the adjusted mode, for otherwise
7576 * the hw state checker will get angry at the mismatch. */
7577 crtc_vtotal = adjusted_mode->crtc_vtotal;
7578 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
b0e77b9c 7579
609aeaca 7580 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
b0e77b9c 7581 /* the chip adds 2 halflines automatically */
4d8a62ea
DV
7582 crtc_vtotal -= 1;
7583 crtc_vblank_end -= 1;
609aeaca 7584
409ee761 7585 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
609aeaca
VS
7586 vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
7587 else
7588 vsyncshift = adjusted_mode->crtc_hsync_start -
7589 adjusted_mode->crtc_htotal / 2;
1caea6e9
VS
7590 if (vsyncshift < 0)
7591 vsyncshift += adjusted_mode->crtc_htotal;
b0e77b9c
PZ
7592 }
7593
7594 if (INTEL_INFO(dev)->gen > 3)
fe2b8f9d 7595 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
b0e77b9c 7596
fe2b8f9d 7597 I915_WRITE(HTOTAL(cpu_transcoder),
b0e77b9c
PZ
7598 (adjusted_mode->crtc_hdisplay - 1) |
7599 ((adjusted_mode->crtc_htotal - 1) << 16));
fe2b8f9d 7600 I915_WRITE(HBLANK(cpu_transcoder),
b0e77b9c
PZ
7601 (adjusted_mode->crtc_hblank_start - 1) |
7602 ((adjusted_mode->crtc_hblank_end - 1) << 16));
fe2b8f9d 7603 I915_WRITE(HSYNC(cpu_transcoder),
b0e77b9c
PZ
7604 (adjusted_mode->crtc_hsync_start - 1) |
7605 ((adjusted_mode->crtc_hsync_end - 1) << 16));
7606
fe2b8f9d 7607 I915_WRITE(VTOTAL(cpu_transcoder),
b0e77b9c 7608 (adjusted_mode->crtc_vdisplay - 1) |
4d8a62ea 7609 ((crtc_vtotal - 1) << 16));
fe2b8f9d 7610 I915_WRITE(VBLANK(cpu_transcoder),
b0e77b9c 7611 (adjusted_mode->crtc_vblank_start - 1) |
4d8a62ea 7612 ((crtc_vblank_end - 1) << 16));
fe2b8f9d 7613 I915_WRITE(VSYNC(cpu_transcoder),
b0e77b9c
PZ
7614 (adjusted_mode->crtc_vsync_start - 1) |
7615 ((adjusted_mode->crtc_vsync_end - 1) << 16));
7616
b5e508d4
PZ
7617 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
7618 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
7619 * documented on the DDI_FUNC_CTL register description, EDP Input Select
7620 * bits. */
7621 if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
7622 (pipe == PIPE_B || pipe == PIPE_C))
7623 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
7624
bc58be60
JN
7625}
7626
7627static void intel_set_pipe_src_size(struct intel_crtc *intel_crtc)
7628{
7629 struct drm_device *dev = intel_crtc->base.dev;
7630 struct drm_i915_private *dev_priv = dev->dev_private;
7631 enum pipe pipe = intel_crtc->pipe;
7632
b0e77b9c
PZ
7633 /* pipesrc controls the size that is scaled from, which should
7634 * always be the user's requested size.
7635 */
7636 I915_WRITE(PIPESRC(pipe),
6e3c9717
ACO
7637 ((intel_crtc->config->pipe_src_w - 1) << 16) |
7638 (intel_crtc->config->pipe_src_h - 1));
b0e77b9c
PZ
7639}
7640
1bd1bd80 7641static void intel_get_pipe_timings(struct intel_crtc *crtc,
5cec258b 7642 struct intel_crtc_state *pipe_config)
1bd1bd80
DV
7643{
7644 struct drm_device *dev = crtc->base.dev;
7645 struct drm_i915_private *dev_priv = dev->dev_private;
7646 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
7647 uint32_t tmp;
7648
7649 tmp = I915_READ(HTOTAL(cpu_transcoder));
2d112de7
ACO
7650 pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
7651 pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 7652 tmp = I915_READ(HBLANK(cpu_transcoder));
2d112de7
ACO
7653 pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
7654 pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 7655 tmp = I915_READ(HSYNC(cpu_transcoder));
2d112de7
ACO
7656 pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
7657 pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80
DV
7658
7659 tmp = I915_READ(VTOTAL(cpu_transcoder));
2d112de7
ACO
7660 pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
7661 pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 7662 tmp = I915_READ(VBLANK(cpu_transcoder));
2d112de7
ACO
7663 pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
7664 pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 7665 tmp = I915_READ(VSYNC(cpu_transcoder));
2d112de7
ACO
7666 pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
7667 pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80
DV
7668
7669 if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
2d112de7
ACO
7670 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
7671 pipe_config->base.adjusted_mode.crtc_vtotal += 1;
7672 pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
1bd1bd80 7673 }
bc58be60
JN
7674}
7675
7676static void intel_get_pipe_src_size(struct intel_crtc *crtc,
7677 struct intel_crtc_state *pipe_config)
7678{
7679 struct drm_device *dev = crtc->base.dev;
7680 struct drm_i915_private *dev_priv = dev->dev_private;
7681 u32 tmp;
1bd1bd80
DV
7682
7683 tmp = I915_READ(PIPESRC(crtc->pipe));
37327abd
VS
7684 pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
7685 pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
7686
2d112de7
ACO
7687 pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
7688 pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
1bd1bd80
DV
7689}
7690
f6a83288 7691void intel_mode_from_pipe_config(struct drm_display_mode *mode,
5cec258b 7692 struct intel_crtc_state *pipe_config)
babea61d 7693{
2d112de7
ACO
7694 mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
7695 mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
7696 mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
7697 mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
babea61d 7698
2d112de7
ACO
7699 mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
7700 mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
7701 mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
7702 mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
babea61d 7703
2d112de7 7704 mode->flags = pipe_config->base.adjusted_mode.flags;
cd13f5ab 7705 mode->type = DRM_MODE_TYPE_DRIVER;
babea61d 7706
2d112de7
ACO
7707 mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
7708 mode->flags |= pipe_config->base.adjusted_mode.flags;
cd13f5ab
ML
7709
7710 mode->hsync = drm_mode_hsync(mode);
7711 mode->vrefresh = drm_mode_vrefresh(mode);
7712 drm_mode_set_name(mode);
babea61d
JB
7713}
7714
84b046f3
DV
7715static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
7716{
7717 struct drm_device *dev = intel_crtc->base.dev;
7718 struct drm_i915_private *dev_priv = dev->dev_private;
7719 uint32_t pipeconf;
7720
9f11a9e4 7721 pipeconf = 0;
84b046f3 7722
b6b5d049
VS
7723 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
7724 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
7725 pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
67c72a12 7726
6e3c9717 7727 if (intel_crtc->config->double_wide)
cf532bb2 7728 pipeconf |= PIPECONF_DOUBLE_WIDE;
84b046f3 7729
ff9ce46e 7730 /* only g4x and later have fancy bpc/dither controls */
666a4537 7731 if (IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
ff9ce46e 7732 /* Bspec claims that we can't use dithering for 30bpp pipes. */
6e3c9717 7733 if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
ff9ce46e 7734 pipeconf |= PIPECONF_DITHER_EN |
84b046f3 7735 PIPECONF_DITHER_TYPE_SP;
84b046f3 7736
6e3c9717 7737 switch (intel_crtc->config->pipe_bpp) {
ff9ce46e
DV
7738 case 18:
7739 pipeconf |= PIPECONF_6BPC;
7740 break;
7741 case 24:
7742 pipeconf |= PIPECONF_8BPC;
7743 break;
7744 case 30:
7745 pipeconf |= PIPECONF_10BPC;
7746 break;
7747 default:
7748 /* Case prevented by intel_choose_pipe_bpp_dither. */
7749 BUG();
84b046f3
DV
7750 }
7751 }
7752
7753 if (HAS_PIPE_CXSR(dev)) {
7754 if (intel_crtc->lowfreq_avail) {
7755 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
7756 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
7757 } else {
7758 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
84b046f3
DV
7759 }
7760 }
7761
6e3c9717 7762 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
efc2cfff 7763 if (INTEL_INFO(dev)->gen < 4 ||
409ee761 7764 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
efc2cfff
VS
7765 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
7766 else
7767 pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
7768 } else
84b046f3
DV
7769 pipeconf |= PIPECONF_PROGRESSIVE;
7770
666a4537
WB
7771 if ((IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) &&
7772 intel_crtc->config->limited_color_range)
9f11a9e4 7773 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
9c8e09b7 7774
84b046f3
DV
7775 I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
7776 POSTING_READ(PIPECONF(intel_crtc->pipe));
7777}
7778
81c97f52
ACO
7779static int i8xx_crtc_compute_clock(struct intel_crtc *crtc,
7780 struct intel_crtc_state *crtc_state)
7781{
7782 struct drm_device *dev = crtc->base.dev;
7783 struct drm_i915_private *dev_priv = dev->dev_private;
7784 const intel_limit_t *limit;
7785 int refclk = 48000;
7786
7787 memset(&crtc_state->dpll_hw_state, 0,
7788 sizeof(crtc_state->dpll_hw_state));
7789
7790 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
7791 if (intel_panel_use_ssc(dev_priv)) {
7792 refclk = dev_priv->vbt.lvds_ssc_freq;
7793 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
7794 }
7795
7796 limit = &intel_limits_i8xx_lvds;
7797 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO)) {
7798 limit = &intel_limits_i8xx_dvo;
7799 } else {
7800 limit = &intel_limits_i8xx_dac;
7801 }
7802
7803 if (!crtc_state->clock_set &&
7804 !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7805 refclk, NULL, &crtc_state->dpll)) {
7806 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7807 return -EINVAL;
7808 }
7809
7810 i8xx_compute_dpll(crtc, crtc_state, NULL);
7811
7812 return 0;
7813}
7814
19ec6693
ACO
7815static int g4x_crtc_compute_clock(struct intel_crtc *crtc,
7816 struct intel_crtc_state *crtc_state)
7817{
7818 struct drm_device *dev = crtc->base.dev;
7819 struct drm_i915_private *dev_priv = dev->dev_private;
7820 const intel_limit_t *limit;
7821 int refclk = 96000;
7822
7823 memset(&crtc_state->dpll_hw_state, 0,
7824 sizeof(crtc_state->dpll_hw_state));
7825
7826 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
7827 if (intel_panel_use_ssc(dev_priv)) {
7828 refclk = dev_priv->vbt.lvds_ssc_freq;
7829 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
7830 }
7831
7832 if (intel_is_dual_link_lvds(dev))
7833 limit = &intel_limits_g4x_dual_channel_lvds;
7834 else
7835 limit = &intel_limits_g4x_single_channel_lvds;
7836 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI) ||
7837 intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
7838 limit = &intel_limits_g4x_hdmi;
7839 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO)) {
7840 limit = &intel_limits_g4x_sdvo;
7841 } else {
7842 /* The option is for other outputs */
7843 limit = &intel_limits_i9xx_sdvo;
7844 }
7845
7846 if (!crtc_state->clock_set &&
7847 !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7848 refclk, NULL, &crtc_state->dpll)) {
7849 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7850 return -EINVAL;
7851 }
7852
7853 i9xx_compute_dpll(crtc, crtc_state, NULL);
7854
7855 return 0;
7856}
7857
70e8aa21
ACO
7858static int pnv_crtc_compute_clock(struct intel_crtc *crtc,
7859 struct intel_crtc_state *crtc_state)
7860{
7861 struct drm_device *dev = crtc->base.dev;
7862 struct drm_i915_private *dev_priv = dev->dev_private;
7863 const intel_limit_t *limit;
7864 int refclk = 96000;
7865
7866 memset(&crtc_state->dpll_hw_state, 0,
7867 sizeof(crtc_state->dpll_hw_state));
7868
7869 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
7870 if (intel_panel_use_ssc(dev_priv)) {
7871 refclk = dev_priv->vbt.lvds_ssc_freq;
7872 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
7873 }
7874
7875 limit = &intel_limits_pineview_lvds;
7876 } else {
7877 limit = &intel_limits_pineview_sdvo;
7878 }
7879
7880 if (!crtc_state->clock_set &&
7881 !pnv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7882 refclk, NULL, &crtc_state->dpll)) {
7883 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7884 return -EINVAL;
7885 }
7886
7887 i9xx_compute_dpll(crtc, crtc_state, NULL);
7888
7889 return 0;
7890}
7891
190f68c5
ACO
7892static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
7893 struct intel_crtc_state *crtc_state)
79e53945 7894{
c7653199 7895 struct drm_device *dev = crtc->base.dev;
79e53945 7896 struct drm_i915_private *dev_priv = dev->dev_private;
d4906093 7897 const intel_limit_t *limit;
81c97f52 7898 int refclk = 96000;
79e53945 7899
dd3cd74a
ACO
7900 memset(&crtc_state->dpll_hw_state, 0,
7901 sizeof(crtc_state->dpll_hw_state));
7902
70e8aa21
ACO
7903 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
7904 if (intel_panel_use_ssc(dev_priv)) {
7905 refclk = dev_priv->vbt.lvds_ssc_freq;
7906 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
7907 }
43565a06 7908
70e8aa21
ACO
7909 limit = &intel_limits_i9xx_lvds;
7910 } else {
7911 limit = &intel_limits_i9xx_sdvo;
81c97f52 7912 }
79e53945 7913
70e8aa21
ACO
7914 if (!crtc_state->clock_set &&
7915 !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7916 refclk, NULL, &crtc_state->dpll)) {
7917 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7918 return -EINVAL;
f47709a9 7919 }
7026d4ac 7920
81c97f52 7921 i9xx_compute_dpll(crtc, crtc_state, NULL);
79e53945 7922
c8f7a0db 7923 return 0;
f564048e
EA
7924}
7925
65b3d6a9
ACO
7926static int chv_crtc_compute_clock(struct intel_crtc *crtc,
7927 struct intel_crtc_state *crtc_state)
7928{
7929 int refclk = 100000;
7930 const intel_limit_t *limit = &intel_limits_chv;
7931
7932 memset(&crtc_state->dpll_hw_state, 0,
7933 sizeof(crtc_state->dpll_hw_state));
7934
7935 if (crtc_state->has_dsi_encoder)
7936 return 0;
7937
7938 if (!crtc_state->clock_set &&
7939 !chv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7940 refclk, NULL, &crtc_state->dpll)) {
7941 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7942 return -EINVAL;
7943 }
7944
7945 chv_compute_dpll(crtc, crtc_state);
7946
7947 return 0;
7948}
7949
7950static int vlv_crtc_compute_clock(struct intel_crtc *crtc,
7951 struct intel_crtc_state *crtc_state)
7952{
7953 int refclk = 100000;
7954 const intel_limit_t *limit = &intel_limits_vlv;
7955
7956 memset(&crtc_state->dpll_hw_state, 0,
7957 sizeof(crtc_state->dpll_hw_state));
7958
7959 if (crtc_state->has_dsi_encoder)
7960 return 0;
7961
7962 if (!crtc_state->clock_set &&
7963 !vlv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7964 refclk, NULL, &crtc_state->dpll)) {
7965 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7966 return -EINVAL;
7967 }
7968
7969 vlv_compute_dpll(crtc, crtc_state);
7970
7971 return 0;
7972}
7973
2fa2fe9a 7974static void i9xx_get_pfit_config(struct intel_crtc *crtc,
5cec258b 7975 struct intel_crtc_state *pipe_config)
2fa2fe9a
DV
7976{
7977 struct drm_device *dev = crtc->base.dev;
7978 struct drm_i915_private *dev_priv = dev->dev_private;
7979 uint32_t tmp;
7980
dc9e7dec
VS
7981 if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
7982 return;
7983
2fa2fe9a 7984 tmp = I915_READ(PFIT_CONTROL);
06922821
DV
7985 if (!(tmp & PFIT_ENABLE))
7986 return;
2fa2fe9a 7987
06922821 7988 /* Check whether the pfit is attached to our pipe. */
2fa2fe9a
DV
7989 if (INTEL_INFO(dev)->gen < 4) {
7990 if (crtc->pipe != PIPE_B)
7991 return;
2fa2fe9a
DV
7992 } else {
7993 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
7994 return;
7995 }
7996
06922821 7997 pipe_config->gmch_pfit.control = tmp;
2fa2fe9a
DV
7998 pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
7999 if (INTEL_INFO(dev)->gen < 5)
8000 pipe_config->gmch_pfit.lvds_border_bits =
8001 I915_READ(LVDS) & LVDS_BORDER_ENABLE;
8002}
8003
acbec814 8004static void vlv_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 8005 struct intel_crtc_state *pipe_config)
acbec814
JB
8006{
8007 struct drm_device *dev = crtc->base.dev;
8008 struct drm_i915_private *dev_priv = dev->dev_private;
8009 int pipe = pipe_config->cpu_transcoder;
8010 intel_clock_t clock;
8011 u32 mdiv;
662c6ecb 8012 int refclk = 100000;
acbec814 8013
b521973b
VS
8014 /* In case of DSI, DPLL will not be used */
8015 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
f573de5a
SK
8016 return;
8017
a580516d 8018 mutex_lock(&dev_priv->sb_lock);
ab3c759a 8019 mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
a580516d 8020 mutex_unlock(&dev_priv->sb_lock);
acbec814
JB
8021
8022 clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
8023 clock.m2 = mdiv & DPIO_M2DIV_MASK;
8024 clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
8025 clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
8026 clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
8027
dccbea3b 8028 pipe_config->port_clock = vlv_calc_dpll_params(refclk, &clock);
acbec814
JB
8029}
8030
5724dbd1
DL
8031static void
8032i9xx_get_initial_plane_config(struct intel_crtc *crtc,
8033 struct intel_initial_plane_config *plane_config)
1ad292b5
JB
8034{
8035 struct drm_device *dev = crtc->base.dev;
8036 struct drm_i915_private *dev_priv = dev->dev_private;
8037 u32 val, base, offset;
8038 int pipe = crtc->pipe, plane = crtc->plane;
8039 int fourcc, pixel_format;
6761dd31 8040 unsigned int aligned_height;
b113d5ee 8041 struct drm_framebuffer *fb;
1b842c89 8042 struct intel_framebuffer *intel_fb;
1ad292b5 8043
42a7b088
DL
8044 val = I915_READ(DSPCNTR(plane));
8045 if (!(val & DISPLAY_PLANE_ENABLE))
8046 return;
8047
d9806c9f 8048 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
1b842c89 8049 if (!intel_fb) {
1ad292b5
JB
8050 DRM_DEBUG_KMS("failed to alloc fb\n");
8051 return;
8052 }
8053
1b842c89
DL
8054 fb = &intel_fb->base;
8055
18c5247e
DV
8056 if (INTEL_INFO(dev)->gen >= 4) {
8057 if (val & DISPPLANE_TILED) {
49af449b 8058 plane_config->tiling = I915_TILING_X;
18c5247e
DV
8059 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
8060 }
8061 }
1ad292b5
JB
8062
8063 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
b35d63fa 8064 fourcc = i9xx_format_to_fourcc(pixel_format);
b113d5ee
DL
8065 fb->pixel_format = fourcc;
8066 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
1ad292b5
JB
8067
8068 if (INTEL_INFO(dev)->gen >= 4) {
49af449b 8069 if (plane_config->tiling)
1ad292b5
JB
8070 offset = I915_READ(DSPTILEOFF(plane));
8071 else
8072 offset = I915_READ(DSPLINOFF(plane));
8073 base = I915_READ(DSPSURF(plane)) & 0xfffff000;
8074 } else {
8075 base = I915_READ(DSPADDR(plane));
8076 }
8077 plane_config->base = base;
8078
8079 val = I915_READ(PIPESRC(pipe));
b113d5ee
DL
8080 fb->width = ((val >> 16) & 0xfff) + 1;
8081 fb->height = ((val >> 0) & 0xfff) + 1;
1ad292b5
JB
8082
8083 val = I915_READ(DSPSTRIDE(pipe));
b113d5ee 8084 fb->pitches[0] = val & 0xffffffc0;
1ad292b5 8085
b113d5ee 8086 aligned_height = intel_fb_align_height(dev, fb->height,
091df6cb
DV
8087 fb->pixel_format,
8088 fb->modifier[0]);
1ad292b5 8089
f37b5c2b 8090 plane_config->size = fb->pitches[0] * aligned_height;
1ad292b5 8091
2844a921
DL
8092 DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
8093 pipe_name(pipe), plane, fb->width, fb->height,
8094 fb->bits_per_pixel, base, fb->pitches[0],
8095 plane_config->size);
1ad292b5 8096
2d14030b 8097 plane_config->fb = intel_fb;
1ad292b5
JB
8098}
8099
70b23a98 8100static void chv_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 8101 struct intel_crtc_state *pipe_config)
70b23a98
VS
8102{
8103 struct drm_device *dev = crtc->base.dev;
8104 struct drm_i915_private *dev_priv = dev->dev_private;
8105 int pipe = pipe_config->cpu_transcoder;
8106 enum dpio_channel port = vlv_pipe_to_channel(pipe);
8107 intel_clock_t clock;
0d7b6b11 8108 u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2, pll_dw3;
70b23a98
VS
8109 int refclk = 100000;
8110
b521973b
VS
8111 /* In case of DSI, DPLL will not be used */
8112 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
8113 return;
8114
a580516d 8115 mutex_lock(&dev_priv->sb_lock);
70b23a98
VS
8116 cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
8117 pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
8118 pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
8119 pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
0d7b6b11 8120 pll_dw3 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
a580516d 8121 mutex_unlock(&dev_priv->sb_lock);
70b23a98
VS
8122
8123 clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
0d7b6b11
ID
8124 clock.m2 = (pll_dw0 & 0xff) << 22;
8125 if (pll_dw3 & DPIO_CHV_FRAC_DIV_EN)
8126 clock.m2 |= pll_dw2 & 0x3fffff;
70b23a98
VS
8127 clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
8128 clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
8129 clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
8130
dccbea3b 8131 pipe_config->port_clock = chv_calc_dpll_params(refclk, &clock);
70b23a98
VS
8132}
8133
0e8ffe1b 8134static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
5cec258b 8135 struct intel_crtc_state *pipe_config)
0e8ffe1b
DV
8136{
8137 struct drm_device *dev = crtc->base.dev;
8138 struct drm_i915_private *dev_priv = dev->dev_private;
1729050e 8139 enum intel_display_power_domain power_domain;
0e8ffe1b 8140 uint32_t tmp;
1729050e 8141 bool ret;
0e8ffe1b 8142
1729050e
ID
8143 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
8144 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
b5482bd0
ID
8145 return false;
8146
e143a21c 8147 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
8106ddbd 8148 pipe_config->shared_dpll = NULL;
eccb140b 8149
1729050e
ID
8150 ret = false;
8151
0e8ffe1b
DV
8152 tmp = I915_READ(PIPECONF(crtc->pipe));
8153 if (!(tmp & PIPECONF_ENABLE))
1729050e 8154 goto out;
0e8ffe1b 8155
666a4537 8156 if (IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
42571aef
VS
8157 switch (tmp & PIPECONF_BPC_MASK) {
8158 case PIPECONF_6BPC:
8159 pipe_config->pipe_bpp = 18;
8160 break;
8161 case PIPECONF_8BPC:
8162 pipe_config->pipe_bpp = 24;
8163 break;
8164 case PIPECONF_10BPC:
8165 pipe_config->pipe_bpp = 30;
8166 break;
8167 default:
8168 break;
8169 }
8170 }
8171
666a4537
WB
8172 if ((IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) &&
8173 (tmp & PIPECONF_COLOR_RANGE_SELECT))
b5a9fa09
DV
8174 pipe_config->limited_color_range = true;
8175
282740f7
VS
8176 if (INTEL_INFO(dev)->gen < 4)
8177 pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
8178
1bd1bd80 8179 intel_get_pipe_timings(crtc, pipe_config);
bc58be60 8180 intel_get_pipe_src_size(crtc, pipe_config);
1bd1bd80 8181
2fa2fe9a
DV
8182 i9xx_get_pfit_config(crtc, pipe_config);
8183
6c49f241 8184 if (INTEL_INFO(dev)->gen >= 4) {
c231775c
VS
8185 /* No way to read it out on pipes B and C */
8186 if (IS_CHERRYVIEW(dev) && crtc->pipe != PIPE_A)
8187 tmp = dev_priv->chv_dpll_md[crtc->pipe];
8188 else
8189 tmp = I915_READ(DPLL_MD(crtc->pipe));
6c49f241
DV
8190 pipe_config->pixel_multiplier =
8191 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
8192 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
8bcc2795 8193 pipe_config->dpll_hw_state.dpll_md = tmp;
6c49f241
DV
8194 } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
8195 tmp = I915_READ(DPLL(crtc->pipe));
8196 pipe_config->pixel_multiplier =
8197 ((tmp & SDVO_MULTIPLIER_MASK)
8198 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
8199 } else {
8200 /* Note that on i915G/GM the pixel multiplier is in the sdvo
8201 * port and will be fixed up in the encoder->get_config
8202 * function. */
8203 pipe_config->pixel_multiplier = 1;
8204 }
8bcc2795 8205 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
666a4537 8206 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
1c4e0274
VS
8207 /*
8208 * DPLL_DVO_2X_MODE must be enabled for both DPLLs
8209 * on 830. Filter it out here so that we don't
8210 * report errors due to that.
8211 */
8212 if (IS_I830(dev))
8213 pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
8214
8bcc2795
DV
8215 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
8216 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
165e901c
VS
8217 } else {
8218 /* Mask out read-only status bits. */
8219 pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
8220 DPLL_PORTC_READY_MASK |
8221 DPLL_PORTB_READY_MASK);
8bcc2795 8222 }
6c49f241 8223
70b23a98
VS
8224 if (IS_CHERRYVIEW(dev))
8225 chv_crtc_clock_get(crtc, pipe_config);
8226 else if (IS_VALLEYVIEW(dev))
acbec814
JB
8227 vlv_crtc_clock_get(crtc, pipe_config);
8228 else
8229 i9xx_crtc_clock_get(crtc, pipe_config);
18442d08 8230
0f64614d
VS
8231 /*
8232 * Normally the dotclock is filled in by the encoder .get_config()
8233 * but in case the pipe is enabled w/o any ports we need a sane
8234 * default.
8235 */
8236 pipe_config->base.adjusted_mode.crtc_clock =
8237 pipe_config->port_clock / pipe_config->pixel_multiplier;
8238
1729050e
ID
8239 ret = true;
8240
8241out:
8242 intel_display_power_put(dev_priv, power_domain);
8243
8244 return ret;
0e8ffe1b
DV
8245}
8246
dde86e2d 8247static void ironlake_init_pch_refclk(struct drm_device *dev)
13d83a67
JB
8248{
8249 struct drm_i915_private *dev_priv = dev->dev_private;
13d83a67 8250 struct intel_encoder *encoder;
74cfd7ac 8251 u32 val, final;
13d83a67 8252 bool has_lvds = false;
199e5d79 8253 bool has_cpu_edp = false;
199e5d79 8254 bool has_panel = false;
99eb6a01
KP
8255 bool has_ck505 = false;
8256 bool can_ssc = false;
13d83a67
JB
8257
8258 /* We need to take the global config into account */
b2784e15 8259 for_each_intel_encoder(dev, encoder) {
199e5d79
KP
8260 switch (encoder->type) {
8261 case INTEL_OUTPUT_LVDS:
8262 has_panel = true;
8263 has_lvds = true;
8264 break;
8265 case INTEL_OUTPUT_EDP:
8266 has_panel = true;
2de6905f 8267 if (enc_to_dig_port(&encoder->base)->port == PORT_A)
199e5d79
KP
8268 has_cpu_edp = true;
8269 break;
6847d71b
PZ
8270 default:
8271 break;
13d83a67
JB
8272 }
8273 }
8274
99eb6a01 8275 if (HAS_PCH_IBX(dev)) {
41aa3448 8276 has_ck505 = dev_priv->vbt.display_clock_mode;
99eb6a01
KP
8277 can_ssc = has_ck505;
8278 } else {
8279 has_ck505 = false;
8280 can_ssc = true;
8281 }
8282
2de6905f
ID
8283 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
8284 has_panel, has_lvds, has_ck505);
13d83a67
JB
8285
8286 /* Ironlake: try to setup display ref clock before DPLL
8287 * enabling. This is only under driver's control after
8288 * PCH B stepping, previous chipset stepping should be
8289 * ignoring this setting.
8290 */
74cfd7ac
CW
8291 val = I915_READ(PCH_DREF_CONTROL);
8292
8293 /* As we must carefully and slowly disable/enable each source in turn,
8294 * compute the final state we want first and check if we need to
8295 * make any changes at all.
8296 */
8297 final = val;
8298 final &= ~DREF_NONSPREAD_SOURCE_MASK;
8299 if (has_ck505)
8300 final |= DREF_NONSPREAD_CK505_ENABLE;
8301 else
8302 final |= DREF_NONSPREAD_SOURCE_ENABLE;
8303
8304 final &= ~DREF_SSC_SOURCE_MASK;
8305 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
8306 final &= ~DREF_SSC1_ENABLE;
8307
8308 if (has_panel) {
8309 final |= DREF_SSC_SOURCE_ENABLE;
8310
8311 if (intel_panel_use_ssc(dev_priv) && can_ssc)
8312 final |= DREF_SSC1_ENABLE;
8313
8314 if (has_cpu_edp) {
8315 if (intel_panel_use_ssc(dev_priv) && can_ssc)
8316 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
8317 else
8318 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
8319 } else
8320 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
8321 } else {
8322 final |= DREF_SSC_SOURCE_DISABLE;
8323 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
8324 }
8325
8326 if (final == val)
8327 return;
8328
13d83a67 8329 /* Always enable nonspread source */
74cfd7ac 8330 val &= ~DREF_NONSPREAD_SOURCE_MASK;
13d83a67 8331
99eb6a01 8332 if (has_ck505)
74cfd7ac 8333 val |= DREF_NONSPREAD_CK505_ENABLE;
99eb6a01 8334 else
74cfd7ac 8335 val |= DREF_NONSPREAD_SOURCE_ENABLE;
13d83a67 8336
199e5d79 8337 if (has_panel) {
74cfd7ac
CW
8338 val &= ~DREF_SSC_SOURCE_MASK;
8339 val |= DREF_SSC_SOURCE_ENABLE;
13d83a67 8340
199e5d79 8341 /* SSC must be turned on before enabling the CPU output */
99eb6a01 8342 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
199e5d79 8343 DRM_DEBUG_KMS("Using SSC on panel\n");
74cfd7ac 8344 val |= DREF_SSC1_ENABLE;
e77166b5 8345 } else
74cfd7ac 8346 val &= ~DREF_SSC1_ENABLE;
199e5d79
KP
8347
8348 /* Get SSC going before enabling the outputs */
74cfd7ac 8349 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
8350 POSTING_READ(PCH_DREF_CONTROL);
8351 udelay(200);
8352
74cfd7ac 8353 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
13d83a67
JB
8354
8355 /* Enable CPU source on CPU attached eDP */
199e5d79 8356 if (has_cpu_edp) {
99eb6a01 8357 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
199e5d79 8358 DRM_DEBUG_KMS("Using SSC on eDP\n");
74cfd7ac 8359 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
eba905b2 8360 } else
74cfd7ac 8361 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
199e5d79 8362 } else
74cfd7ac 8363 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
199e5d79 8364
74cfd7ac 8365 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
8366 POSTING_READ(PCH_DREF_CONTROL);
8367 udelay(200);
8368 } else {
8369 DRM_DEBUG_KMS("Disabling SSC entirely\n");
8370
74cfd7ac 8371 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
199e5d79
KP
8372
8373 /* Turn off CPU output */
74cfd7ac 8374 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
199e5d79 8375
74cfd7ac 8376 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
8377 POSTING_READ(PCH_DREF_CONTROL);
8378 udelay(200);
8379
8380 /* Turn off the SSC source */
74cfd7ac
CW
8381 val &= ~DREF_SSC_SOURCE_MASK;
8382 val |= DREF_SSC_SOURCE_DISABLE;
199e5d79
KP
8383
8384 /* Turn off SSC1 */
74cfd7ac 8385 val &= ~DREF_SSC1_ENABLE;
199e5d79 8386
74cfd7ac 8387 I915_WRITE(PCH_DREF_CONTROL, val);
13d83a67
JB
8388 POSTING_READ(PCH_DREF_CONTROL);
8389 udelay(200);
8390 }
74cfd7ac
CW
8391
8392 BUG_ON(val != final);
13d83a67
JB
8393}
8394
f31f2d55 8395static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
dde86e2d 8396{
f31f2d55 8397 uint32_t tmp;
dde86e2d 8398
0ff066a9
PZ
8399 tmp = I915_READ(SOUTH_CHICKEN2);
8400 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
8401 I915_WRITE(SOUTH_CHICKEN2, tmp);
dde86e2d 8402
0ff066a9
PZ
8403 if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
8404 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
8405 DRM_ERROR("FDI mPHY reset assert timeout\n");
dde86e2d 8406
0ff066a9
PZ
8407 tmp = I915_READ(SOUTH_CHICKEN2);
8408 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
8409 I915_WRITE(SOUTH_CHICKEN2, tmp);
dde86e2d 8410
0ff066a9
PZ
8411 if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
8412 FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
8413 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
f31f2d55
PZ
8414}
8415
8416/* WaMPhyProgramming:hsw */
8417static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
8418{
8419 uint32_t tmp;
dde86e2d
PZ
8420
8421 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
8422 tmp &= ~(0xFF << 24);
8423 tmp |= (0x12 << 24);
8424 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
8425
dde86e2d
PZ
8426 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
8427 tmp |= (1 << 11);
8428 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
8429
8430 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
8431 tmp |= (1 << 11);
8432 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
8433
dde86e2d
PZ
8434 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
8435 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
8436 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
8437
8438 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
8439 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
8440 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
8441
0ff066a9
PZ
8442 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
8443 tmp &= ~(7 << 13);
8444 tmp |= (5 << 13);
8445 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
dde86e2d 8446
0ff066a9
PZ
8447 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
8448 tmp &= ~(7 << 13);
8449 tmp |= (5 << 13);
8450 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
dde86e2d
PZ
8451
8452 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
8453 tmp &= ~0xFF;
8454 tmp |= 0x1C;
8455 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
8456
8457 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
8458 tmp &= ~0xFF;
8459 tmp |= 0x1C;
8460 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
8461
8462 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
8463 tmp &= ~(0xFF << 16);
8464 tmp |= (0x1C << 16);
8465 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
8466
8467 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
8468 tmp &= ~(0xFF << 16);
8469 tmp |= (0x1C << 16);
8470 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
8471
0ff066a9
PZ
8472 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
8473 tmp |= (1 << 27);
8474 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
dde86e2d 8475
0ff066a9
PZ
8476 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
8477 tmp |= (1 << 27);
8478 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
dde86e2d 8479
0ff066a9
PZ
8480 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
8481 tmp &= ~(0xF << 28);
8482 tmp |= (4 << 28);
8483 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
dde86e2d 8484
0ff066a9
PZ
8485 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
8486 tmp &= ~(0xF << 28);
8487 tmp |= (4 << 28);
8488 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
f31f2d55
PZ
8489}
8490
2fa86a1f
PZ
8491/* Implements 3 different sequences from BSpec chapter "Display iCLK
8492 * Programming" based on the parameters passed:
8493 * - Sequence to enable CLKOUT_DP
8494 * - Sequence to enable CLKOUT_DP without spread
8495 * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
8496 */
8497static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
8498 bool with_fdi)
f31f2d55
PZ
8499{
8500 struct drm_i915_private *dev_priv = dev->dev_private;
2fa86a1f
PZ
8501 uint32_t reg, tmp;
8502
8503 if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
8504 with_spread = true;
c2699524 8505 if (WARN(HAS_PCH_LPT_LP(dev) && with_fdi, "LP PCH doesn't have FDI\n"))
2fa86a1f 8506 with_fdi = false;
f31f2d55 8507
a580516d 8508 mutex_lock(&dev_priv->sb_lock);
f31f2d55
PZ
8509
8510 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8511 tmp &= ~SBI_SSCCTL_DISABLE;
8512 tmp |= SBI_SSCCTL_PATHALT;
8513 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8514
8515 udelay(24);
8516
2fa86a1f
PZ
8517 if (with_spread) {
8518 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8519 tmp &= ~SBI_SSCCTL_PATHALT;
8520 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
f31f2d55 8521
2fa86a1f
PZ
8522 if (with_fdi) {
8523 lpt_reset_fdi_mphy(dev_priv);
8524 lpt_program_fdi_mphy(dev_priv);
8525 }
8526 }
dde86e2d 8527
c2699524 8528 reg = HAS_PCH_LPT_LP(dev) ? SBI_GEN0 : SBI_DBUFF0;
2fa86a1f
PZ
8529 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8530 tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8531 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
c00db246 8532
a580516d 8533 mutex_unlock(&dev_priv->sb_lock);
dde86e2d
PZ
8534}
8535
47701c3b
PZ
8536/* Sequence to disable CLKOUT_DP */
8537static void lpt_disable_clkout_dp(struct drm_device *dev)
8538{
8539 struct drm_i915_private *dev_priv = dev->dev_private;
8540 uint32_t reg, tmp;
8541
a580516d 8542 mutex_lock(&dev_priv->sb_lock);
47701c3b 8543
c2699524 8544 reg = HAS_PCH_LPT_LP(dev) ? SBI_GEN0 : SBI_DBUFF0;
47701c3b
PZ
8545 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8546 tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8547 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
8548
8549 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8550 if (!(tmp & SBI_SSCCTL_DISABLE)) {
8551 if (!(tmp & SBI_SSCCTL_PATHALT)) {
8552 tmp |= SBI_SSCCTL_PATHALT;
8553 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8554 udelay(32);
8555 }
8556 tmp |= SBI_SSCCTL_DISABLE;
8557 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8558 }
8559
a580516d 8560 mutex_unlock(&dev_priv->sb_lock);
47701c3b
PZ
8561}
8562
f7be2c21
VS
8563#define BEND_IDX(steps) ((50 + (steps)) / 5)
8564
8565static const uint16_t sscdivintphase[] = {
8566 [BEND_IDX( 50)] = 0x3B23,
8567 [BEND_IDX( 45)] = 0x3B23,
8568 [BEND_IDX( 40)] = 0x3C23,
8569 [BEND_IDX( 35)] = 0x3C23,
8570 [BEND_IDX( 30)] = 0x3D23,
8571 [BEND_IDX( 25)] = 0x3D23,
8572 [BEND_IDX( 20)] = 0x3E23,
8573 [BEND_IDX( 15)] = 0x3E23,
8574 [BEND_IDX( 10)] = 0x3F23,
8575 [BEND_IDX( 5)] = 0x3F23,
8576 [BEND_IDX( 0)] = 0x0025,
8577 [BEND_IDX( -5)] = 0x0025,
8578 [BEND_IDX(-10)] = 0x0125,
8579 [BEND_IDX(-15)] = 0x0125,
8580 [BEND_IDX(-20)] = 0x0225,
8581 [BEND_IDX(-25)] = 0x0225,
8582 [BEND_IDX(-30)] = 0x0325,
8583 [BEND_IDX(-35)] = 0x0325,
8584 [BEND_IDX(-40)] = 0x0425,
8585 [BEND_IDX(-45)] = 0x0425,
8586 [BEND_IDX(-50)] = 0x0525,
8587};
8588
8589/*
8590 * Bend CLKOUT_DP
8591 * steps -50 to 50 inclusive, in steps of 5
8592 * < 0 slow down the clock, > 0 speed up the clock, 0 == no bend (135MHz)
8593 * change in clock period = -(steps / 10) * 5.787 ps
8594 */
8595static void lpt_bend_clkout_dp(struct drm_i915_private *dev_priv, int steps)
8596{
8597 uint32_t tmp;
8598 int idx = BEND_IDX(steps);
8599
8600 if (WARN_ON(steps % 5 != 0))
8601 return;
8602
8603 if (WARN_ON(idx >= ARRAY_SIZE(sscdivintphase)))
8604 return;
8605
8606 mutex_lock(&dev_priv->sb_lock);
8607
8608 if (steps % 10 != 0)
8609 tmp = 0xAAAAAAAB;
8610 else
8611 tmp = 0x00000000;
8612 intel_sbi_write(dev_priv, SBI_SSCDITHPHASE, tmp, SBI_ICLK);
8613
8614 tmp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE, SBI_ICLK);
8615 tmp &= 0xffff0000;
8616 tmp |= sscdivintphase[idx];
8617 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE, tmp, SBI_ICLK);
8618
8619 mutex_unlock(&dev_priv->sb_lock);
8620}
8621
8622#undef BEND_IDX
8623
bf8fa3d3
PZ
8624static void lpt_init_pch_refclk(struct drm_device *dev)
8625{
bf8fa3d3
PZ
8626 struct intel_encoder *encoder;
8627 bool has_vga = false;
8628
b2784e15 8629 for_each_intel_encoder(dev, encoder) {
bf8fa3d3
PZ
8630 switch (encoder->type) {
8631 case INTEL_OUTPUT_ANALOG:
8632 has_vga = true;
8633 break;
6847d71b
PZ
8634 default:
8635 break;
bf8fa3d3
PZ
8636 }
8637 }
8638
f7be2c21
VS
8639 if (has_vga) {
8640 lpt_bend_clkout_dp(to_i915(dev), 0);
47701c3b 8641 lpt_enable_clkout_dp(dev, true, true);
f7be2c21 8642 } else {
47701c3b 8643 lpt_disable_clkout_dp(dev);
f7be2c21 8644 }
bf8fa3d3
PZ
8645}
8646
dde86e2d
PZ
8647/*
8648 * Initialize reference clocks when the driver loads
8649 */
8650void intel_init_pch_refclk(struct drm_device *dev)
8651{
8652 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
8653 ironlake_init_pch_refclk(dev);
8654 else if (HAS_PCH_LPT(dev))
8655 lpt_init_pch_refclk(dev);
8656}
8657
6ff93609 8658static void ironlake_set_pipeconf(struct drm_crtc *crtc)
79e53945 8659{
c8203565 8660 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
79e53945
JB
8661 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8662 int pipe = intel_crtc->pipe;
c8203565
PZ
8663 uint32_t val;
8664
78114071 8665 val = 0;
c8203565 8666
6e3c9717 8667 switch (intel_crtc->config->pipe_bpp) {
c8203565 8668 case 18:
dfd07d72 8669 val |= PIPECONF_6BPC;
c8203565
PZ
8670 break;
8671 case 24:
dfd07d72 8672 val |= PIPECONF_8BPC;
c8203565
PZ
8673 break;
8674 case 30:
dfd07d72 8675 val |= PIPECONF_10BPC;
c8203565
PZ
8676 break;
8677 case 36:
dfd07d72 8678 val |= PIPECONF_12BPC;
c8203565
PZ
8679 break;
8680 default:
cc769b62
PZ
8681 /* Case prevented by intel_choose_pipe_bpp_dither. */
8682 BUG();
c8203565
PZ
8683 }
8684
6e3c9717 8685 if (intel_crtc->config->dither)
c8203565
PZ
8686 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8687
6e3c9717 8688 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
c8203565
PZ
8689 val |= PIPECONF_INTERLACED_ILK;
8690 else
8691 val |= PIPECONF_PROGRESSIVE;
8692
6e3c9717 8693 if (intel_crtc->config->limited_color_range)
3685a8f3 8694 val |= PIPECONF_COLOR_RANGE_SELECT;
3685a8f3 8695
c8203565
PZ
8696 I915_WRITE(PIPECONF(pipe), val);
8697 POSTING_READ(PIPECONF(pipe));
8698}
8699
6ff93609 8700static void haswell_set_pipeconf(struct drm_crtc *crtc)
ee2b0b38 8701{
391bf048 8702 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
ee2b0b38 8703 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6e3c9717 8704 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
391bf048 8705 u32 val = 0;
ee2b0b38 8706
391bf048 8707 if (IS_HASWELL(dev_priv) && intel_crtc->config->dither)
ee2b0b38
PZ
8708 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8709
6e3c9717 8710 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
ee2b0b38
PZ
8711 val |= PIPECONF_INTERLACED_ILK;
8712 else
8713 val |= PIPECONF_PROGRESSIVE;
8714
702e7a56
PZ
8715 I915_WRITE(PIPECONF(cpu_transcoder), val);
8716 POSTING_READ(PIPECONF(cpu_transcoder));
391bf048
JN
8717}
8718
391bf048
JN
8719static void haswell_set_pipemisc(struct drm_crtc *crtc)
8720{
8721 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
8722 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
756f85cf 8723
391bf048
JN
8724 if (IS_BROADWELL(dev_priv) || INTEL_INFO(dev_priv)->gen >= 9) {
8725 u32 val = 0;
756f85cf 8726
6e3c9717 8727 switch (intel_crtc->config->pipe_bpp) {
756f85cf
PZ
8728 case 18:
8729 val |= PIPEMISC_DITHER_6_BPC;
8730 break;
8731 case 24:
8732 val |= PIPEMISC_DITHER_8_BPC;
8733 break;
8734 case 30:
8735 val |= PIPEMISC_DITHER_10_BPC;
8736 break;
8737 case 36:
8738 val |= PIPEMISC_DITHER_12_BPC;
8739 break;
8740 default:
8741 /* Case prevented by pipe_config_set_bpp. */
8742 BUG();
8743 }
8744
6e3c9717 8745 if (intel_crtc->config->dither)
756f85cf
PZ
8746 val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
8747
391bf048 8748 I915_WRITE(PIPEMISC(intel_crtc->pipe), val);
756f85cf 8749 }
ee2b0b38
PZ
8750}
8751
d4b1931c
PZ
8752int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
8753{
8754 /*
8755 * Account for spread spectrum to avoid
8756 * oversubscribing the link. Max center spread
8757 * is 2.5%; use 5% for safety's sake.
8758 */
8759 u32 bps = target_clock * bpp * 21 / 20;
619d4d04 8760 return DIV_ROUND_UP(bps, link_bw * 8);
d4b1931c
PZ
8761}
8762
7429e9d4 8763static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
6cf86a5e 8764{
7429e9d4 8765 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
f48d8f23
PZ
8766}
8767
b75ca6f6
ACO
8768static void ironlake_compute_dpll(struct intel_crtc *intel_crtc,
8769 struct intel_crtc_state *crtc_state,
8770 intel_clock_t *reduced_clock)
79e53945 8771{
de13a2e3 8772 struct drm_crtc *crtc = &intel_crtc->base;
79e53945
JB
8773 struct drm_device *dev = crtc->dev;
8774 struct drm_i915_private *dev_priv = dev->dev_private;
55bb9992 8775 struct drm_atomic_state *state = crtc_state->base.state;
da3ced29 8776 struct drm_connector *connector;
55bb9992
ACO
8777 struct drm_connector_state *connector_state;
8778 struct intel_encoder *encoder;
b75ca6f6 8779 u32 dpll, fp, fp2;
ceb41007 8780 int factor, i;
09ede541 8781 bool is_lvds = false, is_sdvo = false;
79e53945 8782
da3ced29 8783 for_each_connector_in_state(state, connector, connector_state, i) {
55bb9992
ACO
8784 if (connector_state->crtc != crtc_state->base.crtc)
8785 continue;
8786
8787 encoder = to_intel_encoder(connector_state->best_encoder);
8788
8789 switch (encoder->type) {
79e53945
JB
8790 case INTEL_OUTPUT_LVDS:
8791 is_lvds = true;
8792 break;
8793 case INTEL_OUTPUT_SDVO:
7d57382e 8794 case INTEL_OUTPUT_HDMI:
79e53945 8795 is_sdvo = true;
79e53945 8796 break;
6847d71b
PZ
8797 default:
8798 break;
79e53945
JB
8799 }
8800 }
79e53945 8801
c1858123 8802 /* Enable autotuning of the PLL clock (if permissible) */
8febb297
EA
8803 factor = 21;
8804 if (is_lvds) {
8805 if ((intel_panel_use_ssc(dev_priv) &&
e91e941b 8806 dev_priv->vbt.lvds_ssc_freq == 100000) ||
f0b44056 8807 (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
8febb297 8808 factor = 25;
190f68c5 8809 } else if (crtc_state->sdvo_tv_clock)
8febb297 8810 factor = 20;
c1858123 8811
b75ca6f6
ACO
8812 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
8813
190f68c5 8814 if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
b75ca6f6
ACO
8815 fp |= FP_CB_TUNE;
8816
8817 if (reduced_clock) {
8818 fp2 = i9xx_dpll_compute_fp(reduced_clock);
2c07245f 8819
b75ca6f6
ACO
8820 if (reduced_clock->m < factor * reduced_clock->n)
8821 fp2 |= FP_CB_TUNE;
8822 } else {
8823 fp2 = fp;
8824 }
9a7c7890 8825
5eddb70b 8826 dpll = 0;
2c07245f 8827
a07d6787
EA
8828 if (is_lvds)
8829 dpll |= DPLLB_MODE_LVDS;
8830 else
8831 dpll |= DPLLB_MODE_DAC_SERIAL;
198a037f 8832
190f68c5 8833 dpll |= (crtc_state->pixel_multiplier - 1)
ef1b460d 8834 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
198a037f
DV
8835
8836 if (is_sdvo)
4a33e48d 8837 dpll |= DPLL_SDVO_HIGH_SPEED;
190f68c5 8838 if (crtc_state->has_dp_encoder)
4a33e48d 8839 dpll |= DPLL_SDVO_HIGH_SPEED;
79e53945 8840
a07d6787 8841 /* compute bitmask from p1 value */
190f68c5 8842 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
a07d6787 8843 /* also FPA1 */
190f68c5 8844 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
a07d6787 8845
190f68c5 8846 switch (crtc_state->dpll.p2) {
a07d6787
EA
8847 case 5:
8848 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
8849 break;
8850 case 7:
8851 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
8852 break;
8853 case 10:
8854 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
8855 break;
8856 case 14:
8857 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
8858 break;
79e53945
JB
8859 }
8860
ceb41007 8861 if (is_lvds && intel_panel_use_ssc(dev_priv))
43565a06 8862 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
79e53945
JB
8863 else
8864 dpll |= PLL_REF_INPUT_DREFCLK;
8865
b75ca6f6
ACO
8866 dpll |= DPLL_VCO_ENABLE;
8867
8868 crtc_state->dpll_hw_state.dpll = dpll;
8869 crtc_state->dpll_hw_state.fp0 = fp;
8870 crtc_state->dpll_hw_state.fp1 = fp2;
de13a2e3
PZ
8871}
8872
190f68c5
ACO
8873static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
8874 struct intel_crtc_state *crtc_state)
de13a2e3 8875{
997c030c
ACO
8876 struct drm_device *dev = crtc->base.dev;
8877 struct drm_i915_private *dev_priv = dev->dev_private;
364ee29d 8878 intel_clock_t reduced_clock;
7ed9f894 8879 bool has_reduced_clock = false;
e2b78267 8880 struct intel_shared_dpll *pll;
997c030c
ACO
8881 const intel_limit_t *limit;
8882 int refclk = 120000;
de13a2e3 8883
dd3cd74a
ACO
8884 memset(&crtc_state->dpll_hw_state, 0,
8885 sizeof(crtc_state->dpll_hw_state));
8886
ded220e2
ACO
8887 crtc->lowfreq_avail = false;
8888
8889 /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
8890 if (!crtc_state->has_pch_encoder)
8891 return 0;
79e53945 8892
997c030c
ACO
8893 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
8894 if (intel_panel_use_ssc(dev_priv)) {
8895 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
8896 dev_priv->vbt.lvds_ssc_freq);
8897 refclk = dev_priv->vbt.lvds_ssc_freq;
8898 }
8899
8900 if (intel_is_dual_link_lvds(dev)) {
8901 if (refclk == 100000)
8902 limit = &intel_limits_ironlake_dual_lvds_100m;
8903 else
8904 limit = &intel_limits_ironlake_dual_lvds;
8905 } else {
8906 if (refclk == 100000)
8907 limit = &intel_limits_ironlake_single_lvds_100m;
8908 else
8909 limit = &intel_limits_ironlake_single_lvds;
8910 }
8911 } else {
8912 limit = &intel_limits_ironlake_dac;
8913 }
8914
364ee29d 8915 if (!crtc_state->clock_set &&
997c030c
ACO
8916 !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8917 refclk, NULL, &crtc_state->dpll)) {
364ee29d
ACO
8918 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8919 return -EINVAL;
f47709a9 8920 }
79e53945 8921
b75ca6f6
ACO
8922 ironlake_compute_dpll(crtc, crtc_state,
8923 has_reduced_clock ? &reduced_clock : NULL);
66e985c0 8924
ded220e2
ACO
8925 pll = intel_get_shared_dpll(crtc, crtc_state, NULL);
8926 if (pll == NULL) {
8927 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
8928 pipe_name(crtc->pipe));
8929 return -EINVAL;
3fb37703 8930 }
79e53945 8931
ded220e2
ACO
8932 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
8933 has_reduced_clock)
c7653199 8934 crtc->lowfreq_avail = true;
e2b78267 8935
c8f7a0db 8936 return 0;
79e53945
JB
8937}
8938
eb14cb74
VS
8939static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
8940 struct intel_link_m_n *m_n)
8941{
8942 struct drm_device *dev = crtc->base.dev;
8943 struct drm_i915_private *dev_priv = dev->dev_private;
8944 enum pipe pipe = crtc->pipe;
8945
8946 m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
8947 m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
8948 m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
8949 & ~TU_SIZE_MASK;
8950 m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
8951 m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
8952 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8953}
8954
8955static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
8956 enum transcoder transcoder,
b95af8be
VK
8957 struct intel_link_m_n *m_n,
8958 struct intel_link_m_n *m2_n2)
72419203
DV
8959{
8960 struct drm_device *dev = crtc->base.dev;
8961 struct drm_i915_private *dev_priv = dev->dev_private;
eb14cb74 8962 enum pipe pipe = crtc->pipe;
72419203 8963
eb14cb74
VS
8964 if (INTEL_INFO(dev)->gen >= 5) {
8965 m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
8966 m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
8967 m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
8968 & ~TU_SIZE_MASK;
8969 m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
8970 m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
8971 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
b95af8be
VK
8972 /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
8973 * gen < 8) and if DRRS is supported (to make sure the
8974 * registers are not unnecessarily read).
8975 */
8976 if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
6e3c9717 8977 crtc->config->has_drrs) {
b95af8be
VK
8978 m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
8979 m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
8980 m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
8981 & ~TU_SIZE_MASK;
8982 m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
8983 m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
8984 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8985 }
eb14cb74
VS
8986 } else {
8987 m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
8988 m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
8989 m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
8990 & ~TU_SIZE_MASK;
8991 m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
8992 m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
8993 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8994 }
8995}
8996
8997void intel_dp_get_m_n(struct intel_crtc *crtc,
5cec258b 8998 struct intel_crtc_state *pipe_config)
eb14cb74 8999{
681a8504 9000 if (pipe_config->has_pch_encoder)
eb14cb74
VS
9001 intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
9002 else
9003 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
b95af8be
VK
9004 &pipe_config->dp_m_n,
9005 &pipe_config->dp_m2_n2);
eb14cb74 9006}
72419203 9007
eb14cb74 9008static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
5cec258b 9009 struct intel_crtc_state *pipe_config)
eb14cb74
VS
9010{
9011 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
b95af8be 9012 &pipe_config->fdi_m_n, NULL);
72419203
DV
9013}
9014
bd2e244f 9015static void skylake_get_pfit_config(struct intel_crtc *crtc,
5cec258b 9016 struct intel_crtc_state *pipe_config)
bd2e244f
JB
9017{
9018 struct drm_device *dev = crtc->base.dev;
9019 struct drm_i915_private *dev_priv = dev->dev_private;
a1b2278e
CK
9020 struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
9021 uint32_t ps_ctrl = 0;
9022 int id = -1;
9023 int i;
bd2e244f 9024
a1b2278e
CK
9025 /* find scaler attached to this pipe */
9026 for (i = 0; i < crtc->num_scalers; i++) {
9027 ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
9028 if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
9029 id = i;
9030 pipe_config->pch_pfit.enabled = true;
9031 pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
9032 pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
9033 break;
9034 }
9035 }
bd2e244f 9036
a1b2278e
CK
9037 scaler_state->scaler_id = id;
9038 if (id >= 0) {
9039 scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
9040 } else {
9041 scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
bd2e244f
JB
9042 }
9043}
9044
5724dbd1
DL
9045static void
9046skylake_get_initial_plane_config(struct intel_crtc *crtc,
9047 struct intel_initial_plane_config *plane_config)
bc8d7dff
DL
9048{
9049 struct drm_device *dev = crtc->base.dev;
9050 struct drm_i915_private *dev_priv = dev->dev_private;
40f46283 9051 u32 val, base, offset, stride_mult, tiling;
bc8d7dff
DL
9052 int pipe = crtc->pipe;
9053 int fourcc, pixel_format;
6761dd31 9054 unsigned int aligned_height;
bc8d7dff 9055 struct drm_framebuffer *fb;
1b842c89 9056 struct intel_framebuffer *intel_fb;
bc8d7dff 9057
d9806c9f 9058 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
1b842c89 9059 if (!intel_fb) {
bc8d7dff
DL
9060 DRM_DEBUG_KMS("failed to alloc fb\n");
9061 return;
9062 }
9063
1b842c89
DL
9064 fb = &intel_fb->base;
9065
bc8d7dff 9066 val = I915_READ(PLANE_CTL(pipe, 0));
42a7b088
DL
9067 if (!(val & PLANE_CTL_ENABLE))
9068 goto error;
9069
bc8d7dff
DL
9070 pixel_format = val & PLANE_CTL_FORMAT_MASK;
9071 fourcc = skl_format_to_fourcc(pixel_format,
9072 val & PLANE_CTL_ORDER_RGBX,
9073 val & PLANE_CTL_ALPHA_MASK);
9074 fb->pixel_format = fourcc;
9075 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
9076
40f46283
DL
9077 tiling = val & PLANE_CTL_TILED_MASK;
9078 switch (tiling) {
9079 case PLANE_CTL_TILED_LINEAR:
9080 fb->modifier[0] = DRM_FORMAT_MOD_NONE;
9081 break;
9082 case PLANE_CTL_TILED_X:
9083 plane_config->tiling = I915_TILING_X;
9084 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
9085 break;
9086 case PLANE_CTL_TILED_Y:
9087 fb->modifier[0] = I915_FORMAT_MOD_Y_TILED;
9088 break;
9089 case PLANE_CTL_TILED_YF:
9090 fb->modifier[0] = I915_FORMAT_MOD_Yf_TILED;
9091 break;
9092 default:
9093 MISSING_CASE(tiling);
9094 goto error;
9095 }
9096
bc8d7dff
DL
9097 base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
9098 plane_config->base = base;
9099
9100 offset = I915_READ(PLANE_OFFSET(pipe, 0));
9101
9102 val = I915_READ(PLANE_SIZE(pipe, 0));
9103 fb->height = ((val >> 16) & 0xfff) + 1;
9104 fb->width = ((val >> 0) & 0x1fff) + 1;
9105
9106 val = I915_READ(PLANE_STRIDE(pipe, 0));
7b49f948 9107 stride_mult = intel_fb_stride_alignment(dev_priv, fb->modifier[0],
40f46283 9108 fb->pixel_format);
bc8d7dff
DL
9109 fb->pitches[0] = (val & 0x3ff) * stride_mult;
9110
9111 aligned_height = intel_fb_align_height(dev, fb->height,
091df6cb
DV
9112 fb->pixel_format,
9113 fb->modifier[0]);
bc8d7dff 9114
f37b5c2b 9115 plane_config->size = fb->pitches[0] * aligned_height;
bc8d7dff
DL
9116
9117 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9118 pipe_name(pipe), fb->width, fb->height,
9119 fb->bits_per_pixel, base, fb->pitches[0],
9120 plane_config->size);
9121
2d14030b 9122 plane_config->fb = intel_fb;
bc8d7dff
DL
9123 return;
9124
9125error:
9126 kfree(fb);
9127}
9128
2fa2fe9a 9129static void ironlake_get_pfit_config(struct intel_crtc *crtc,
5cec258b 9130 struct intel_crtc_state *pipe_config)
2fa2fe9a
DV
9131{
9132 struct drm_device *dev = crtc->base.dev;
9133 struct drm_i915_private *dev_priv = dev->dev_private;
9134 uint32_t tmp;
9135
9136 tmp = I915_READ(PF_CTL(crtc->pipe));
9137
9138 if (tmp & PF_ENABLE) {
fd4daa9c 9139 pipe_config->pch_pfit.enabled = true;
2fa2fe9a
DV
9140 pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
9141 pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
cb8b2a30
DV
9142
9143 /* We currently do not free assignements of panel fitters on
9144 * ivb/hsw (since we don't use the higher upscaling modes which
9145 * differentiates them) so just WARN about this case for now. */
9146 if (IS_GEN7(dev)) {
9147 WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
9148 PF_PIPE_SEL_IVB(crtc->pipe));
9149 }
2fa2fe9a 9150 }
79e53945
JB
9151}
9152
5724dbd1
DL
9153static void
9154ironlake_get_initial_plane_config(struct intel_crtc *crtc,
9155 struct intel_initial_plane_config *plane_config)
4c6baa59
JB
9156{
9157 struct drm_device *dev = crtc->base.dev;
9158 struct drm_i915_private *dev_priv = dev->dev_private;
9159 u32 val, base, offset;
aeee5a49 9160 int pipe = crtc->pipe;
4c6baa59 9161 int fourcc, pixel_format;
6761dd31 9162 unsigned int aligned_height;
b113d5ee 9163 struct drm_framebuffer *fb;
1b842c89 9164 struct intel_framebuffer *intel_fb;
4c6baa59 9165
42a7b088
DL
9166 val = I915_READ(DSPCNTR(pipe));
9167 if (!(val & DISPLAY_PLANE_ENABLE))
9168 return;
9169
d9806c9f 9170 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
1b842c89 9171 if (!intel_fb) {
4c6baa59
JB
9172 DRM_DEBUG_KMS("failed to alloc fb\n");
9173 return;
9174 }
9175
1b842c89
DL
9176 fb = &intel_fb->base;
9177
18c5247e
DV
9178 if (INTEL_INFO(dev)->gen >= 4) {
9179 if (val & DISPPLANE_TILED) {
49af449b 9180 plane_config->tiling = I915_TILING_X;
18c5247e
DV
9181 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
9182 }
9183 }
4c6baa59
JB
9184
9185 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
b35d63fa 9186 fourcc = i9xx_format_to_fourcc(pixel_format);
b113d5ee
DL
9187 fb->pixel_format = fourcc;
9188 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
4c6baa59 9189
aeee5a49 9190 base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
4c6baa59 9191 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
aeee5a49 9192 offset = I915_READ(DSPOFFSET(pipe));
4c6baa59 9193 } else {
49af449b 9194 if (plane_config->tiling)
aeee5a49 9195 offset = I915_READ(DSPTILEOFF(pipe));
4c6baa59 9196 else
aeee5a49 9197 offset = I915_READ(DSPLINOFF(pipe));
4c6baa59
JB
9198 }
9199 plane_config->base = base;
9200
9201 val = I915_READ(PIPESRC(pipe));
b113d5ee
DL
9202 fb->width = ((val >> 16) & 0xfff) + 1;
9203 fb->height = ((val >> 0) & 0xfff) + 1;
4c6baa59
JB
9204
9205 val = I915_READ(DSPSTRIDE(pipe));
b113d5ee 9206 fb->pitches[0] = val & 0xffffffc0;
4c6baa59 9207
b113d5ee 9208 aligned_height = intel_fb_align_height(dev, fb->height,
091df6cb
DV
9209 fb->pixel_format,
9210 fb->modifier[0]);
4c6baa59 9211
f37b5c2b 9212 plane_config->size = fb->pitches[0] * aligned_height;
4c6baa59 9213
2844a921
DL
9214 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9215 pipe_name(pipe), fb->width, fb->height,
9216 fb->bits_per_pixel, base, fb->pitches[0],
9217 plane_config->size);
b113d5ee 9218
2d14030b 9219 plane_config->fb = intel_fb;
4c6baa59
JB
9220}
9221
0e8ffe1b 9222static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
5cec258b 9223 struct intel_crtc_state *pipe_config)
0e8ffe1b
DV
9224{
9225 struct drm_device *dev = crtc->base.dev;
9226 struct drm_i915_private *dev_priv = dev->dev_private;
1729050e 9227 enum intel_display_power_domain power_domain;
0e8ffe1b 9228 uint32_t tmp;
1729050e 9229 bool ret;
0e8ffe1b 9230
1729050e
ID
9231 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
9232 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
930e8c9e
PZ
9233 return false;
9234
e143a21c 9235 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
8106ddbd 9236 pipe_config->shared_dpll = NULL;
eccb140b 9237
1729050e 9238 ret = false;
0e8ffe1b
DV
9239 tmp = I915_READ(PIPECONF(crtc->pipe));
9240 if (!(tmp & PIPECONF_ENABLE))
1729050e 9241 goto out;
0e8ffe1b 9242
42571aef
VS
9243 switch (tmp & PIPECONF_BPC_MASK) {
9244 case PIPECONF_6BPC:
9245 pipe_config->pipe_bpp = 18;
9246 break;
9247 case PIPECONF_8BPC:
9248 pipe_config->pipe_bpp = 24;
9249 break;
9250 case PIPECONF_10BPC:
9251 pipe_config->pipe_bpp = 30;
9252 break;
9253 case PIPECONF_12BPC:
9254 pipe_config->pipe_bpp = 36;
9255 break;
9256 default:
9257 break;
9258 }
9259
b5a9fa09
DV
9260 if (tmp & PIPECONF_COLOR_RANGE_SELECT)
9261 pipe_config->limited_color_range = true;
9262
ab9412ba 9263 if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
66e985c0 9264 struct intel_shared_dpll *pll;
8106ddbd 9265 enum intel_dpll_id pll_id;
66e985c0 9266
88adfff1
DV
9267 pipe_config->has_pch_encoder = true;
9268
627eb5a3
DV
9269 tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
9270 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
9271 FDI_DP_PORT_WIDTH_SHIFT) + 1;
72419203
DV
9272
9273 ironlake_get_fdi_m_n_config(crtc, pipe_config);
6c49f241 9274
2d1fe073 9275 if (HAS_PCH_IBX(dev_priv)) {
8106ddbd 9276 pll_id = (enum intel_dpll_id) crtc->pipe;
c0d43d62
DV
9277 } else {
9278 tmp = I915_READ(PCH_DPLL_SEL);
9279 if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
8106ddbd 9280 pll_id = DPLL_ID_PCH_PLL_B;
c0d43d62 9281 else
8106ddbd 9282 pll_id= DPLL_ID_PCH_PLL_A;
c0d43d62 9283 }
66e985c0 9284
8106ddbd
ACO
9285 pipe_config->shared_dpll =
9286 intel_get_shared_dpll_by_id(dev_priv, pll_id);
9287 pll = pipe_config->shared_dpll;
66e985c0 9288
2edd6443
ACO
9289 WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
9290 &pipe_config->dpll_hw_state));
c93f54cf
DV
9291
9292 tmp = pipe_config->dpll_hw_state.dpll;
9293 pipe_config->pixel_multiplier =
9294 ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
9295 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
18442d08
VS
9296
9297 ironlake_pch_clock_get(crtc, pipe_config);
6c49f241
DV
9298 } else {
9299 pipe_config->pixel_multiplier = 1;
627eb5a3
DV
9300 }
9301
1bd1bd80 9302 intel_get_pipe_timings(crtc, pipe_config);
bc58be60 9303 intel_get_pipe_src_size(crtc, pipe_config);
1bd1bd80 9304
2fa2fe9a
DV
9305 ironlake_get_pfit_config(crtc, pipe_config);
9306
1729050e
ID
9307 ret = true;
9308
9309out:
9310 intel_display_power_put(dev_priv, power_domain);
9311
9312 return ret;
0e8ffe1b
DV
9313}
9314
be256dc7
PZ
9315static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
9316{
9317 struct drm_device *dev = dev_priv->dev;
be256dc7 9318 struct intel_crtc *crtc;
be256dc7 9319
d3fcc808 9320 for_each_intel_crtc(dev, crtc)
e2c719b7 9321 I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
be256dc7
PZ
9322 pipe_name(crtc->pipe));
9323
e2c719b7
RC
9324 I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
9325 I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
01403de3
VS
9326 I915_STATE_WARN(I915_READ(WRPLL_CTL(0)) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
9327 I915_STATE_WARN(I915_READ(WRPLL_CTL(1)) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
e2c719b7
RC
9328 I915_STATE_WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
9329 I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
be256dc7 9330 "CPU PWM1 enabled\n");
c5107b87 9331 if (IS_HASWELL(dev))
e2c719b7 9332 I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
c5107b87 9333 "CPU PWM2 enabled\n");
e2c719b7 9334 I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
be256dc7 9335 "PCH PWM1 enabled\n");
e2c719b7 9336 I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
be256dc7 9337 "Utility pin enabled\n");
e2c719b7 9338 I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
be256dc7 9339
9926ada1
PZ
9340 /*
9341 * In theory we can still leave IRQs enabled, as long as only the HPD
9342 * interrupts remain enabled. We used to check for that, but since it's
9343 * gen-specific and since we only disable LCPLL after we fully disable
9344 * the interrupts, the check below should be enough.
9345 */
e2c719b7 9346 I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
be256dc7
PZ
9347}
9348
9ccd5aeb
PZ
9349static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
9350{
9351 struct drm_device *dev = dev_priv->dev;
9352
9353 if (IS_HASWELL(dev))
9354 return I915_READ(D_COMP_HSW);
9355 else
9356 return I915_READ(D_COMP_BDW);
9357}
9358
3c4c9b81
PZ
9359static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
9360{
9361 struct drm_device *dev = dev_priv->dev;
9362
9363 if (IS_HASWELL(dev)) {
9364 mutex_lock(&dev_priv->rps.hw_lock);
9365 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
9366 val))
f475dadf 9367 DRM_ERROR("Failed to write to D_COMP\n");
3c4c9b81
PZ
9368 mutex_unlock(&dev_priv->rps.hw_lock);
9369 } else {
9ccd5aeb
PZ
9370 I915_WRITE(D_COMP_BDW, val);
9371 POSTING_READ(D_COMP_BDW);
3c4c9b81 9372 }
be256dc7
PZ
9373}
9374
9375/*
9376 * This function implements pieces of two sequences from BSpec:
9377 * - Sequence for display software to disable LCPLL
9378 * - Sequence for display software to allow package C8+
9379 * The steps implemented here are just the steps that actually touch the LCPLL
9380 * register. Callers should take care of disabling all the display engine
9381 * functions, doing the mode unset, fixing interrupts, etc.
9382 */
6ff58d53
PZ
9383static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
9384 bool switch_to_fclk, bool allow_power_down)
be256dc7
PZ
9385{
9386 uint32_t val;
9387
9388 assert_can_disable_lcpll(dev_priv);
9389
9390 val = I915_READ(LCPLL_CTL);
9391
9392 if (switch_to_fclk) {
9393 val |= LCPLL_CD_SOURCE_FCLK;
9394 I915_WRITE(LCPLL_CTL, val);
9395
9396 if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
9397 LCPLL_CD_SOURCE_FCLK_DONE, 1))
9398 DRM_ERROR("Switching to FCLK failed\n");
9399
9400 val = I915_READ(LCPLL_CTL);
9401 }
9402
9403 val |= LCPLL_PLL_DISABLE;
9404 I915_WRITE(LCPLL_CTL, val);
9405 POSTING_READ(LCPLL_CTL);
9406
9407 if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
9408 DRM_ERROR("LCPLL still locked\n");
9409
9ccd5aeb 9410 val = hsw_read_dcomp(dev_priv);
be256dc7 9411 val |= D_COMP_COMP_DISABLE;
3c4c9b81 9412 hsw_write_dcomp(dev_priv, val);
be256dc7
PZ
9413 ndelay(100);
9414
9ccd5aeb
PZ
9415 if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
9416 1))
be256dc7
PZ
9417 DRM_ERROR("D_COMP RCOMP still in progress\n");
9418
9419 if (allow_power_down) {
9420 val = I915_READ(LCPLL_CTL);
9421 val |= LCPLL_POWER_DOWN_ALLOW;
9422 I915_WRITE(LCPLL_CTL, val);
9423 POSTING_READ(LCPLL_CTL);
9424 }
9425}
9426
9427/*
9428 * Fully restores LCPLL, disallowing power down and switching back to LCPLL
9429 * source.
9430 */
6ff58d53 9431static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
be256dc7
PZ
9432{
9433 uint32_t val;
9434
9435 val = I915_READ(LCPLL_CTL);
9436
9437 if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
9438 LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
9439 return;
9440
a8a8bd54
PZ
9441 /*
9442 * Make sure we're not on PC8 state before disabling PC8, otherwise
9443 * we'll hang the machine. To prevent PC8 state, just enable force_wake.
a8a8bd54 9444 */
59bad947 9445 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
215733fa 9446
be256dc7
PZ
9447 if (val & LCPLL_POWER_DOWN_ALLOW) {
9448 val &= ~LCPLL_POWER_DOWN_ALLOW;
9449 I915_WRITE(LCPLL_CTL, val);
35d8f2eb 9450 POSTING_READ(LCPLL_CTL);
be256dc7
PZ
9451 }
9452
9ccd5aeb 9453 val = hsw_read_dcomp(dev_priv);
be256dc7
PZ
9454 val |= D_COMP_COMP_FORCE;
9455 val &= ~D_COMP_COMP_DISABLE;
3c4c9b81 9456 hsw_write_dcomp(dev_priv, val);
be256dc7
PZ
9457
9458 val = I915_READ(LCPLL_CTL);
9459 val &= ~LCPLL_PLL_DISABLE;
9460 I915_WRITE(LCPLL_CTL, val);
9461
9462 if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
9463 DRM_ERROR("LCPLL not locked yet\n");
9464
9465 if (val & LCPLL_CD_SOURCE_FCLK) {
9466 val = I915_READ(LCPLL_CTL);
9467 val &= ~LCPLL_CD_SOURCE_FCLK;
9468 I915_WRITE(LCPLL_CTL, val);
9469
9470 if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
9471 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
9472 DRM_ERROR("Switching back to LCPLL failed\n");
9473 }
215733fa 9474
59bad947 9475 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
b6283055 9476 intel_update_cdclk(dev_priv->dev);
be256dc7
PZ
9477}
9478
765dab67
PZ
9479/*
9480 * Package states C8 and deeper are really deep PC states that can only be
9481 * reached when all the devices on the system allow it, so even if the graphics
9482 * device allows PC8+, it doesn't mean the system will actually get to these
9483 * states. Our driver only allows PC8+ when going into runtime PM.
9484 *
9485 * The requirements for PC8+ are that all the outputs are disabled, the power
9486 * well is disabled and most interrupts are disabled, and these are also
9487 * requirements for runtime PM. When these conditions are met, we manually do
9488 * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
9489 * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
9490 * hang the machine.
9491 *
9492 * When we really reach PC8 or deeper states (not just when we allow it) we lose
9493 * the state of some registers, so when we come back from PC8+ we need to
9494 * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
9495 * need to take care of the registers kept by RC6. Notice that this happens even
9496 * if we don't put the device in PCI D3 state (which is what currently happens
9497 * because of the runtime PM support).
9498 *
9499 * For more, read "Display Sequences for Package C8" on the hardware
9500 * documentation.
9501 */
a14cb6fc 9502void hsw_enable_pc8(struct drm_i915_private *dev_priv)
c67a470b 9503{
c67a470b
PZ
9504 struct drm_device *dev = dev_priv->dev;
9505 uint32_t val;
9506
c67a470b
PZ
9507 DRM_DEBUG_KMS("Enabling package C8+\n");
9508
c2699524 9509 if (HAS_PCH_LPT_LP(dev)) {
c67a470b
PZ
9510 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9511 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
9512 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9513 }
9514
9515 lpt_disable_clkout_dp(dev);
c67a470b
PZ
9516 hsw_disable_lcpll(dev_priv, true, true);
9517}
9518
a14cb6fc 9519void hsw_disable_pc8(struct drm_i915_private *dev_priv)
c67a470b
PZ
9520{
9521 struct drm_device *dev = dev_priv->dev;
9522 uint32_t val;
9523
c67a470b
PZ
9524 DRM_DEBUG_KMS("Disabling package C8+\n");
9525
9526 hsw_restore_lcpll(dev_priv);
c67a470b
PZ
9527 lpt_init_pch_refclk(dev);
9528
c2699524 9529 if (HAS_PCH_LPT_LP(dev)) {
c67a470b
PZ
9530 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9531 val |= PCH_LP_PARTITION_LEVEL_DISABLE;
9532 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9533 }
c67a470b
PZ
9534}
9535
27c329ed 9536static void broxton_modeset_commit_cdclk(struct drm_atomic_state *old_state)
f8437dd1 9537{
a821fc46 9538 struct drm_device *dev = old_state->dev;
1a617b77
ML
9539 struct intel_atomic_state *old_intel_state =
9540 to_intel_atomic_state(old_state);
9541 unsigned int req_cdclk = old_intel_state->dev_cdclk;
f8437dd1 9542
27c329ed 9543 broxton_set_cdclk(dev, req_cdclk);
f8437dd1
VK
9544}
9545
b432e5cf 9546/* compute the max rate for new configuration */
27c329ed 9547static int ilk_max_pixel_rate(struct drm_atomic_state *state)
b432e5cf 9548{
565602d7
ML
9549 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
9550 struct drm_i915_private *dev_priv = state->dev->dev_private;
9551 struct drm_crtc *crtc;
9552 struct drm_crtc_state *cstate;
27c329ed 9553 struct intel_crtc_state *crtc_state;
565602d7
ML
9554 unsigned max_pixel_rate = 0, i;
9555 enum pipe pipe;
b432e5cf 9556
565602d7
ML
9557 memcpy(intel_state->min_pixclk, dev_priv->min_pixclk,
9558 sizeof(intel_state->min_pixclk));
27c329ed 9559
565602d7
ML
9560 for_each_crtc_in_state(state, crtc, cstate, i) {
9561 int pixel_rate;
27c329ed 9562
565602d7
ML
9563 crtc_state = to_intel_crtc_state(cstate);
9564 if (!crtc_state->base.enable) {
9565 intel_state->min_pixclk[i] = 0;
b432e5cf 9566 continue;
565602d7 9567 }
b432e5cf 9568
27c329ed 9569 pixel_rate = ilk_pipe_pixel_rate(crtc_state);
b432e5cf
VS
9570
9571 /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
565602d7 9572 if (IS_BROADWELL(dev_priv) && crtc_state->ips_enabled)
b432e5cf
VS
9573 pixel_rate = DIV_ROUND_UP(pixel_rate * 100, 95);
9574
565602d7 9575 intel_state->min_pixclk[i] = pixel_rate;
b432e5cf
VS
9576 }
9577
565602d7
ML
9578 for_each_pipe(dev_priv, pipe)
9579 max_pixel_rate = max(intel_state->min_pixclk[pipe], max_pixel_rate);
9580
b432e5cf
VS
9581 return max_pixel_rate;
9582}
9583
9584static void broadwell_set_cdclk(struct drm_device *dev, int cdclk)
9585{
9586 struct drm_i915_private *dev_priv = dev->dev_private;
9587 uint32_t val, data;
9588 int ret;
9589
9590 if (WARN((I915_READ(LCPLL_CTL) &
9591 (LCPLL_PLL_DISABLE | LCPLL_PLL_LOCK |
9592 LCPLL_CD_CLOCK_DISABLE | LCPLL_ROOT_CD_CLOCK_DISABLE |
9593 LCPLL_CD2X_CLOCK_DISABLE | LCPLL_POWER_DOWN_ALLOW |
9594 LCPLL_CD_SOURCE_FCLK)) != LCPLL_PLL_LOCK,
9595 "trying to change cdclk frequency with cdclk not enabled\n"))
9596 return;
9597
9598 mutex_lock(&dev_priv->rps.hw_lock);
9599 ret = sandybridge_pcode_write(dev_priv,
9600 BDW_PCODE_DISPLAY_FREQ_CHANGE_REQ, 0x0);
9601 mutex_unlock(&dev_priv->rps.hw_lock);
9602 if (ret) {
9603 DRM_ERROR("failed to inform pcode about cdclk change\n");
9604 return;
9605 }
9606
9607 val = I915_READ(LCPLL_CTL);
9608 val |= LCPLL_CD_SOURCE_FCLK;
9609 I915_WRITE(LCPLL_CTL, val);
9610
5ba00178
TU
9611 if (wait_for_us(I915_READ(LCPLL_CTL) &
9612 LCPLL_CD_SOURCE_FCLK_DONE, 1))
b432e5cf
VS
9613 DRM_ERROR("Switching to FCLK failed\n");
9614
9615 val = I915_READ(LCPLL_CTL);
9616 val &= ~LCPLL_CLK_FREQ_MASK;
9617
9618 switch (cdclk) {
9619 case 450000:
9620 val |= LCPLL_CLK_FREQ_450;
9621 data = 0;
9622 break;
9623 case 540000:
9624 val |= LCPLL_CLK_FREQ_54O_BDW;
9625 data = 1;
9626 break;
9627 case 337500:
9628 val |= LCPLL_CLK_FREQ_337_5_BDW;
9629 data = 2;
9630 break;
9631 case 675000:
9632 val |= LCPLL_CLK_FREQ_675_BDW;
9633 data = 3;
9634 break;
9635 default:
9636 WARN(1, "invalid cdclk frequency\n");
9637 return;
9638 }
9639
9640 I915_WRITE(LCPLL_CTL, val);
9641
9642 val = I915_READ(LCPLL_CTL);
9643 val &= ~LCPLL_CD_SOURCE_FCLK;
9644 I915_WRITE(LCPLL_CTL, val);
9645
5ba00178
TU
9646 if (wait_for_us((I915_READ(LCPLL_CTL) &
9647 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
b432e5cf
VS
9648 DRM_ERROR("Switching back to LCPLL failed\n");
9649
9650 mutex_lock(&dev_priv->rps.hw_lock);
9651 sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ, data);
9652 mutex_unlock(&dev_priv->rps.hw_lock);
9653
9654 intel_update_cdclk(dev);
9655
9656 WARN(cdclk != dev_priv->cdclk_freq,
9657 "cdclk requested %d kHz but got %d kHz\n",
9658 cdclk, dev_priv->cdclk_freq);
9659}
9660
27c329ed 9661static int broadwell_modeset_calc_cdclk(struct drm_atomic_state *state)
b432e5cf 9662{
27c329ed 9663 struct drm_i915_private *dev_priv = to_i915(state->dev);
1a617b77 9664 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
27c329ed 9665 int max_pixclk = ilk_max_pixel_rate(state);
b432e5cf
VS
9666 int cdclk;
9667
9668 /*
9669 * FIXME should also account for plane ratio
9670 * once 64bpp pixel formats are supported.
9671 */
27c329ed 9672 if (max_pixclk > 540000)
b432e5cf 9673 cdclk = 675000;
27c329ed 9674 else if (max_pixclk > 450000)
b432e5cf 9675 cdclk = 540000;
27c329ed 9676 else if (max_pixclk > 337500)
b432e5cf
VS
9677 cdclk = 450000;
9678 else
9679 cdclk = 337500;
9680
b432e5cf 9681 if (cdclk > dev_priv->max_cdclk_freq) {
63ba534e
ML
9682 DRM_DEBUG_KMS("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
9683 cdclk, dev_priv->max_cdclk_freq);
9684 return -EINVAL;
b432e5cf
VS
9685 }
9686
1a617b77
ML
9687 intel_state->cdclk = intel_state->dev_cdclk = cdclk;
9688 if (!intel_state->active_crtcs)
9689 intel_state->dev_cdclk = 337500;
b432e5cf
VS
9690
9691 return 0;
9692}
9693
27c329ed 9694static void broadwell_modeset_commit_cdclk(struct drm_atomic_state *old_state)
b432e5cf 9695{
27c329ed 9696 struct drm_device *dev = old_state->dev;
1a617b77
ML
9697 struct intel_atomic_state *old_intel_state =
9698 to_intel_atomic_state(old_state);
9699 unsigned req_cdclk = old_intel_state->dev_cdclk;
b432e5cf 9700
27c329ed 9701 broadwell_set_cdclk(dev, req_cdclk);
b432e5cf
VS
9702}
9703
190f68c5
ACO
9704static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
9705 struct intel_crtc_state *crtc_state)
09b4ddf9 9706{
af3997b5
MK
9707 struct intel_encoder *intel_encoder =
9708 intel_ddi_get_crtc_new_encoder(crtc_state);
9709
9710 if (intel_encoder->type != INTEL_OUTPUT_DSI) {
9711 if (!intel_ddi_pll_select(crtc, crtc_state))
9712 return -EINVAL;
9713 }
716c2e55 9714
c7653199 9715 crtc->lowfreq_avail = false;
644cef34 9716
c8f7a0db 9717 return 0;
79e53945
JB
9718}
9719
3760b59c
S
9720static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
9721 enum port port,
9722 struct intel_crtc_state *pipe_config)
9723{
8106ddbd
ACO
9724 enum intel_dpll_id id;
9725
3760b59c
S
9726 switch (port) {
9727 case PORT_A:
9728 pipe_config->ddi_pll_sel = SKL_DPLL0;
08250c4b 9729 id = DPLL_ID_SKL_DPLL0;
3760b59c
S
9730 break;
9731 case PORT_B:
9732 pipe_config->ddi_pll_sel = SKL_DPLL1;
08250c4b 9733 id = DPLL_ID_SKL_DPLL1;
3760b59c
S
9734 break;
9735 case PORT_C:
9736 pipe_config->ddi_pll_sel = SKL_DPLL2;
08250c4b 9737 id = DPLL_ID_SKL_DPLL2;
3760b59c
S
9738 break;
9739 default:
9740 DRM_ERROR("Incorrect port type\n");
8106ddbd 9741 return;
3760b59c 9742 }
8106ddbd
ACO
9743
9744 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
3760b59c
S
9745}
9746
96b7dfb7
S
9747static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
9748 enum port port,
5cec258b 9749 struct intel_crtc_state *pipe_config)
96b7dfb7 9750{
8106ddbd 9751 enum intel_dpll_id id;
a3c988ea 9752 u32 temp;
96b7dfb7
S
9753
9754 temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
9755 pipe_config->ddi_pll_sel = temp >> (port * 3 + 1);
9756
9757 switch (pipe_config->ddi_pll_sel) {
3148ade7 9758 case SKL_DPLL0:
a3c988ea
ACO
9759 id = DPLL_ID_SKL_DPLL0;
9760 break;
96b7dfb7 9761 case SKL_DPLL1:
8106ddbd 9762 id = DPLL_ID_SKL_DPLL1;
96b7dfb7
S
9763 break;
9764 case SKL_DPLL2:
8106ddbd 9765 id = DPLL_ID_SKL_DPLL2;
96b7dfb7
S
9766 break;
9767 case SKL_DPLL3:
8106ddbd 9768 id = DPLL_ID_SKL_DPLL3;
96b7dfb7 9769 break;
8106ddbd
ACO
9770 default:
9771 MISSING_CASE(pipe_config->ddi_pll_sel);
9772 return;
96b7dfb7 9773 }
8106ddbd
ACO
9774
9775 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
96b7dfb7
S
9776}
9777
7d2c8175
DL
9778static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
9779 enum port port,
5cec258b 9780 struct intel_crtc_state *pipe_config)
7d2c8175 9781{
8106ddbd
ACO
9782 enum intel_dpll_id id;
9783
7d2c8175
DL
9784 pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
9785
9786 switch (pipe_config->ddi_pll_sel) {
9787 case PORT_CLK_SEL_WRPLL1:
8106ddbd 9788 id = DPLL_ID_WRPLL1;
7d2c8175
DL
9789 break;
9790 case PORT_CLK_SEL_WRPLL2:
8106ddbd 9791 id = DPLL_ID_WRPLL2;
7d2c8175 9792 break;
00490c22 9793 case PORT_CLK_SEL_SPLL:
8106ddbd 9794 id = DPLL_ID_SPLL;
79bd23da 9795 break;
9d16da65
ACO
9796 case PORT_CLK_SEL_LCPLL_810:
9797 id = DPLL_ID_LCPLL_810;
9798 break;
9799 case PORT_CLK_SEL_LCPLL_1350:
9800 id = DPLL_ID_LCPLL_1350;
9801 break;
9802 case PORT_CLK_SEL_LCPLL_2700:
9803 id = DPLL_ID_LCPLL_2700;
9804 break;
8106ddbd
ACO
9805 default:
9806 MISSING_CASE(pipe_config->ddi_pll_sel);
9807 /* fall through */
9808 case PORT_CLK_SEL_NONE:
8106ddbd 9809 return;
7d2c8175 9810 }
8106ddbd
ACO
9811
9812 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
7d2c8175
DL
9813}
9814
cf30429e
JN
9815static bool hsw_get_transcoder_state(struct intel_crtc *crtc,
9816 struct intel_crtc_state *pipe_config,
9817 unsigned long *power_domain_mask)
9818{
9819 struct drm_device *dev = crtc->base.dev;
9820 struct drm_i915_private *dev_priv = dev->dev_private;
9821 enum intel_display_power_domain power_domain;
9822 u32 tmp;
9823
9824 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
9825
9826 /*
9827 * XXX: Do intel_display_power_get_if_enabled before reading this (for
9828 * consistency and less surprising code; it's in always on power).
9829 */
9830 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
9831 if (tmp & TRANS_DDI_FUNC_ENABLE) {
9832 enum pipe trans_edp_pipe;
9833 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
9834 default:
9835 WARN(1, "unknown pipe linked to edp transcoder\n");
9836 case TRANS_DDI_EDP_INPUT_A_ONOFF:
9837 case TRANS_DDI_EDP_INPUT_A_ON:
9838 trans_edp_pipe = PIPE_A;
9839 break;
9840 case TRANS_DDI_EDP_INPUT_B_ONOFF:
9841 trans_edp_pipe = PIPE_B;
9842 break;
9843 case TRANS_DDI_EDP_INPUT_C_ONOFF:
9844 trans_edp_pipe = PIPE_C;
9845 break;
9846 }
9847
9848 if (trans_edp_pipe == crtc->pipe)
9849 pipe_config->cpu_transcoder = TRANSCODER_EDP;
9850 }
9851
9852 power_domain = POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder);
9853 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
9854 return false;
9855 *power_domain_mask |= BIT(power_domain);
9856
9857 tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
9858
9859 return tmp & PIPECONF_ENABLE;
9860}
9861
4d1de975
JN
9862static bool bxt_get_dsi_transcoder_state(struct intel_crtc *crtc,
9863 struct intel_crtc_state *pipe_config,
9864 unsigned long *power_domain_mask)
9865{
9866 struct drm_device *dev = crtc->base.dev;
9867 struct drm_i915_private *dev_priv = dev->dev_private;
9868 enum intel_display_power_domain power_domain;
9869 enum port port;
9870 enum transcoder cpu_transcoder;
9871 u32 tmp;
9872
9873 pipe_config->has_dsi_encoder = false;
9874
9875 for_each_port_masked(port, BIT(PORT_A) | BIT(PORT_C)) {
9876 if (port == PORT_A)
9877 cpu_transcoder = TRANSCODER_DSI_A;
9878 else
9879 cpu_transcoder = TRANSCODER_DSI_C;
9880
9881 power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
9882 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
9883 continue;
9884 *power_domain_mask |= BIT(power_domain);
9885
db18b6a6
ID
9886 /*
9887 * The PLL needs to be enabled with a valid divider
9888 * configuration, otherwise accessing DSI registers will hang
9889 * the machine. See BSpec North Display Engine
9890 * registers/MIPI[BXT]. We can break out here early, since we
9891 * need the same DSI PLL to be enabled for both DSI ports.
9892 */
9893 if (!intel_dsi_pll_is_enabled(dev_priv))
9894 break;
9895
4d1de975
JN
9896 /* XXX: this works for video mode only */
9897 tmp = I915_READ(BXT_MIPI_PORT_CTRL(port));
9898 if (!(tmp & DPI_ENABLE))
9899 continue;
9900
9901 tmp = I915_READ(MIPI_CTRL(port));
9902 if ((tmp & BXT_PIPE_SELECT_MASK) != BXT_PIPE_SELECT(crtc->pipe))
9903 continue;
9904
9905 pipe_config->cpu_transcoder = cpu_transcoder;
9906 pipe_config->has_dsi_encoder = true;
9907 break;
9908 }
9909
9910 return pipe_config->has_dsi_encoder;
9911}
9912
26804afd 9913static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
5cec258b 9914 struct intel_crtc_state *pipe_config)
26804afd
DV
9915{
9916 struct drm_device *dev = crtc->base.dev;
9917 struct drm_i915_private *dev_priv = dev->dev_private;
d452c5b6 9918 struct intel_shared_dpll *pll;
26804afd
DV
9919 enum port port;
9920 uint32_t tmp;
9921
9922 tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
9923
9924 port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
9925
ef11bdb3 9926 if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev))
96b7dfb7 9927 skylake_get_ddi_pll(dev_priv, port, pipe_config);
3760b59c
S
9928 else if (IS_BROXTON(dev))
9929 bxt_get_ddi_pll(dev_priv, port, pipe_config);
96b7dfb7
S
9930 else
9931 haswell_get_ddi_pll(dev_priv, port, pipe_config);
9cd86933 9932
8106ddbd
ACO
9933 pll = pipe_config->shared_dpll;
9934 if (pll) {
2edd6443
ACO
9935 WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
9936 &pipe_config->dpll_hw_state));
d452c5b6
DV
9937 }
9938
26804afd
DV
9939 /*
9940 * Haswell has only FDI/PCH transcoder A. It is which is connected to
9941 * DDI E. So just check whether this pipe is wired to DDI E and whether
9942 * the PCH transcoder is on.
9943 */
ca370455
DL
9944 if (INTEL_INFO(dev)->gen < 9 &&
9945 (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
26804afd
DV
9946 pipe_config->has_pch_encoder = true;
9947
9948 tmp = I915_READ(FDI_RX_CTL(PIPE_A));
9949 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
9950 FDI_DP_PORT_WIDTH_SHIFT) + 1;
9951
9952 ironlake_get_fdi_m_n_config(crtc, pipe_config);
9953 }
9954}
9955
0e8ffe1b 9956static bool haswell_get_pipe_config(struct intel_crtc *crtc,
5cec258b 9957 struct intel_crtc_state *pipe_config)
0e8ffe1b
DV
9958{
9959 struct drm_device *dev = crtc->base.dev;
9960 struct drm_i915_private *dev_priv = dev->dev_private;
1729050e
ID
9961 enum intel_display_power_domain power_domain;
9962 unsigned long power_domain_mask;
cf30429e 9963 bool active;
0e8ffe1b 9964
1729050e
ID
9965 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
9966 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
b5482bd0 9967 return false;
1729050e
ID
9968 power_domain_mask = BIT(power_domain);
9969
8106ddbd 9970 pipe_config->shared_dpll = NULL;
c0d43d62 9971
cf30429e 9972 active = hsw_get_transcoder_state(crtc, pipe_config, &power_domain_mask);
eccb140b 9973
4d1de975
JN
9974 if (IS_BROXTON(dev_priv)) {
9975 bxt_get_dsi_transcoder_state(crtc, pipe_config,
9976 &power_domain_mask);
9977 WARN_ON(active && pipe_config->has_dsi_encoder);
9978 if (pipe_config->has_dsi_encoder)
9979 active = true;
9980 }
9981
cf30429e 9982 if (!active)
1729050e 9983 goto out;
0e8ffe1b 9984
4d1de975
JN
9985 if (!pipe_config->has_dsi_encoder) {
9986 haswell_get_ddi_port_state(crtc, pipe_config);
9987 intel_get_pipe_timings(crtc, pipe_config);
9988 }
627eb5a3 9989
bc58be60 9990 intel_get_pipe_src_size(crtc, pipe_config);
1bd1bd80 9991
05dc698c
LL
9992 pipe_config->gamma_mode =
9993 I915_READ(GAMMA_MODE(crtc->pipe)) & GAMMA_MODE_MODE_MASK;
9994
a1b2278e
CK
9995 if (INTEL_INFO(dev)->gen >= 9) {
9996 skl_init_scalers(dev, crtc, pipe_config);
9997 }
9998
af99ceda
CK
9999 if (INTEL_INFO(dev)->gen >= 9) {
10000 pipe_config->scaler_state.scaler_id = -1;
10001 pipe_config->scaler_state.scaler_users &= ~(1 << SKL_CRTC_INDEX);
10002 }
10003
1729050e
ID
10004 power_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
10005 if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
10006 power_domain_mask |= BIT(power_domain);
1c132b44 10007 if (INTEL_INFO(dev)->gen >= 9)
bd2e244f 10008 skylake_get_pfit_config(crtc, pipe_config);
ff6d9f55 10009 else
1c132b44 10010 ironlake_get_pfit_config(crtc, pipe_config);
bd2e244f 10011 }
88adfff1 10012
e59150dc
JB
10013 if (IS_HASWELL(dev))
10014 pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
10015 (I915_READ(IPS_CTL) & IPS_ENABLE);
42db64ef 10016
4d1de975
JN
10017 if (pipe_config->cpu_transcoder != TRANSCODER_EDP &&
10018 !transcoder_is_dsi(pipe_config->cpu_transcoder)) {
ebb69c95
CT
10019 pipe_config->pixel_multiplier =
10020 I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
10021 } else {
10022 pipe_config->pixel_multiplier = 1;
10023 }
6c49f241 10024
1729050e
ID
10025out:
10026 for_each_power_domain(power_domain, power_domain_mask)
10027 intel_display_power_put(dev_priv, power_domain);
10028
cf30429e 10029 return active;
0e8ffe1b
DV
10030}
10031
55a08b3f
ML
10032static void i845_update_cursor(struct drm_crtc *crtc, u32 base,
10033 const struct intel_plane_state *plane_state)
560b85bb
CW
10034{
10035 struct drm_device *dev = crtc->dev;
10036 struct drm_i915_private *dev_priv = dev->dev_private;
10037 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
dc41c154 10038 uint32_t cntl = 0, size = 0;
560b85bb 10039
55a08b3f
ML
10040 if (plane_state && plane_state->visible) {
10041 unsigned int width = plane_state->base.crtc_w;
10042 unsigned int height = plane_state->base.crtc_h;
dc41c154
VS
10043 unsigned int stride = roundup_pow_of_two(width) * 4;
10044
10045 switch (stride) {
10046 default:
10047 WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
10048 width, stride);
10049 stride = 256;
10050 /* fallthrough */
10051 case 256:
10052 case 512:
10053 case 1024:
10054 case 2048:
10055 break;
4b0e333e
CW
10056 }
10057
dc41c154
VS
10058 cntl |= CURSOR_ENABLE |
10059 CURSOR_GAMMA_ENABLE |
10060 CURSOR_FORMAT_ARGB |
10061 CURSOR_STRIDE(stride);
10062
10063 size = (height << 12) | width;
4b0e333e 10064 }
560b85bb 10065
dc41c154
VS
10066 if (intel_crtc->cursor_cntl != 0 &&
10067 (intel_crtc->cursor_base != base ||
10068 intel_crtc->cursor_size != size ||
10069 intel_crtc->cursor_cntl != cntl)) {
10070 /* On these chipsets we can only modify the base/size/stride
10071 * whilst the cursor is disabled.
10072 */
0b87c24e
VS
10073 I915_WRITE(CURCNTR(PIPE_A), 0);
10074 POSTING_READ(CURCNTR(PIPE_A));
dc41c154 10075 intel_crtc->cursor_cntl = 0;
4b0e333e 10076 }
560b85bb 10077
99d1f387 10078 if (intel_crtc->cursor_base != base) {
0b87c24e 10079 I915_WRITE(CURBASE(PIPE_A), base);
99d1f387
VS
10080 intel_crtc->cursor_base = base;
10081 }
4726e0b0 10082
dc41c154
VS
10083 if (intel_crtc->cursor_size != size) {
10084 I915_WRITE(CURSIZE, size);
10085 intel_crtc->cursor_size = size;
4b0e333e 10086 }
560b85bb 10087
4b0e333e 10088 if (intel_crtc->cursor_cntl != cntl) {
0b87c24e
VS
10089 I915_WRITE(CURCNTR(PIPE_A), cntl);
10090 POSTING_READ(CURCNTR(PIPE_A));
4b0e333e 10091 intel_crtc->cursor_cntl = cntl;
560b85bb 10092 }
560b85bb
CW
10093}
10094
55a08b3f
ML
10095static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base,
10096 const struct intel_plane_state *plane_state)
65a21cd6
JB
10097{
10098 struct drm_device *dev = crtc->dev;
10099 struct drm_i915_private *dev_priv = dev->dev_private;
10100 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10101 int pipe = intel_crtc->pipe;
663f3122 10102 uint32_t cntl = 0;
4b0e333e 10103
55a08b3f 10104 if (plane_state && plane_state->visible) {
4b0e333e 10105 cntl = MCURSOR_GAMMA_ENABLE;
55a08b3f 10106 switch (plane_state->base.crtc_w) {
4726e0b0
SK
10107 case 64:
10108 cntl |= CURSOR_MODE_64_ARGB_AX;
10109 break;
10110 case 128:
10111 cntl |= CURSOR_MODE_128_ARGB_AX;
10112 break;
10113 case 256:
10114 cntl |= CURSOR_MODE_256_ARGB_AX;
10115 break;
10116 default:
55a08b3f 10117 MISSING_CASE(plane_state->base.crtc_w);
4726e0b0 10118 return;
65a21cd6 10119 }
4b0e333e 10120 cntl |= pipe << 28; /* Connect to correct pipe */
47bf17a7 10121
fc6f93bc 10122 if (HAS_DDI(dev))
47bf17a7 10123 cntl |= CURSOR_PIPE_CSC_ENABLE;
65a21cd6 10124
55a08b3f
ML
10125 if (plane_state->base.rotation == BIT(DRM_ROTATE_180))
10126 cntl |= CURSOR_ROTATE_180;
10127 }
4398ad45 10128
4b0e333e
CW
10129 if (intel_crtc->cursor_cntl != cntl) {
10130 I915_WRITE(CURCNTR(pipe), cntl);
10131 POSTING_READ(CURCNTR(pipe));
10132 intel_crtc->cursor_cntl = cntl;
65a21cd6 10133 }
4b0e333e 10134
65a21cd6 10135 /* and commit changes on next vblank */
5efb3e28
VS
10136 I915_WRITE(CURBASE(pipe), base);
10137 POSTING_READ(CURBASE(pipe));
99d1f387
VS
10138
10139 intel_crtc->cursor_base = base;
65a21cd6
JB
10140}
10141
cda4b7d3 10142/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
6b383a7f 10143static void intel_crtc_update_cursor(struct drm_crtc *crtc,
55a08b3f 10144 const struct intel_plane_state *plane_state)
cda4b7d3
CW
10145{
10146 struct drm_device *dev = crtc->dev;
10147 struct drm_i915_private *dev_priv = dev->dev_private;
10148 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10149 int pipe = intel_crtc->pipe;
55a08b3f
ML
10150 u32 base = intel_crtc->cursor_addr;
10151 u32 pos = 0;
cda4b7d3 10152
55a08b3f
ML
10153 if (plane_state) {
10154 int x = plane_state->base.crtc_x;
10155 int y = plane_state->base.crtc_y;
cda4b7d3 10156
55a08b3f
ML
10157 if (x < 0) {
10158 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
10159 x = -x;
10160 }
10161 pos |= x << CURSOR_X_SHIFT;
cda4b7d3 10162
55a08b3f
ML
10163 if (y < 0) {
10164 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
10165 y = -y;
10166 }
10167 pos |= y << CURSOR_Y_SHIFT;
10168
10169 /* ILK+ do this automagically */
10170 if (HAS_GMCH_DISPLAY(dev) &&
10171 plane_state->base.rotation == BIT(DRM_ROTATE_180)) {
10172 base += (plane_state->base.crtc_h *
10173 plane_state->base.crtc_w - 1) * 4;
10174 }
cda4b7d3 10175 }
cda4b7d3 10176
5efb3e28
VS
10177 I915_WRITE(CURPOS(pipe), pos);
10178
8ac54669 10179 if (IS_845G(dev) || IS_I865G(dev))
55a08b3f 10180 i845_update_cursor(crtc, base, plane_state);
5efb3e28 10181 else
55a08b3f 10182 i9xx_update_cursor(crtc, base, plane_state);
cda4b7d3
CW
10183}
10184
dc41c154
VS
10185static bool cursor_size_ok(struct drm_device *dev,
10186 uint32_t width, uint32_t height)
10187{
10188 if (width == 0 || height == 0)
10189 return false;
10190
10191 /*
10192 * 845g/865g are special in that they are only limited by
10193 * the width of their cursors, the height is arbitrary up to
10194 * the precision of the register. Everything else requires
10195 * square cursors, limited to a few power-of-two sizes.
10196 */
10197 if (IS_845G(dev) || IS_I865G(dev)) {
10198 if ((width & 63) != 0)
10199 return false;
10200
10201 if (width > (IS_845G(dev) ? 64 : 512))
10202 return false;
10203
10204 if (height > 1023)
10205 return false;
10206 } else {
10207 switch (width | height) {
10208 case 256:
10209 case 128:
10210 if (IS_GEN2(dev))
10211 return false;
10212 case 64:
10213 break;
10214 default:
10215 return false;
10216 }
10217 }
10218
10219 return true;
10220}
10221
79e53945
JB
10222/* VESA 640x480x72Hz mode to set on the pipe */
10223static struct drm_display_mode load_detect_mode = {
10224 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
10225 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
10226};
10227
a8bb6818
DV
10228struct drm_framebuffer *
10229__intel_framebuffer_create(struct drm_device *dev,
10230 struct drm_mode_fb_cmd2 *mode_cmd,
10231 struct drm_i915_gem_object *obj)
d2dff872
CW
10232{
10233 struct intel_framebuffer *intel_fb;
10234 int ret;
10235
10236 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
dcb1394e 10237 if (!intel_fb)
d2dff872 10238 return ERR_PTR(-ENOMEM);
d2dff872
CW
10239
10240 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
dd4916c5
DV
10241 if (ret)
10242 goto err;
d2dff872
CW
10243
10244 return &intel_fb->base;
dcb1394e 10245
dd4916c5 10246err:
dd4916c5 10247 kfree(intel_fb);
dd4916c5 10248 return ERR_PTR(ret);
d2dff872
CW
10249}
10250
b5ea642a 10251static struct drm_framebuffer *
a8bb6818
DV
10252intel_framebuffer_create(struct drm_device *dev,
10253 struct drm_mode_fb_cmd2 *mode_cmd,
10254 struct drm_i915_gem_object *obj)
10255{
10256 struct drm_framebuffer *fb;
10257 int ret;
10258
10259 ret = i915_mutex_lock_interruptible(dev);
10260 if (ret)
10261 return ERR_PTR(ret);
10262 fb = __intel_framebuffer_create(dev, mode_cmd, obj);
10263 mutex_unlock(&dev->struct_mutex);
10264
10265 return fb;
10266}
10267
d2dff872
CW
10268static u32
10269intel_framebuffer_pitch_for_width(int width, int bpp)
10270{
10271 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
10272 return ALIGN(pitch, 64);
10273}
10274
10275static u32
10276intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
10277{
10278 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
1267a26b 10279 return PAGE_ALIGN(pitch * mode->vdisplay);
d2dff872
CW
10280}
10281
10282static struct drm_framebuffer *
10283intel_framebuffer_create_for_mode(struct drm_device *dev,
10284 struct drm_display_mode *mode,
10285 int depth, int bpp)
10286{
dcb1394e 10287 struct drm_framebuffer *fb;
d2dff872 10288 struct drm_i915_gem_object *obj;
0fed39bd 10289 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
d2dff872
CW
10290
10291 obj = i915_gem_alloc_object(dev,
10292 intel_framebuffer_size_for_mode(mode, bpp));
10293 if (obj == NULL)
10294 return ERR_PTR(-ENOMEM);
10295
10296 mode_cmd.width = mode->hdisplay;
10297 mode_cmd.height = mode->vdisplay;
308e5bcb
JB
10298 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
10299 bpp);
5ca0c34a 10300 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
d2dff872 10301
dcb1394e
LW
10302 fb = intel_framebuffer_create(dev, &mode_cmd, obj);
10303 if (IS_ERR(fb))
10304 drm_gem_object_unreference_unlocked(&obj->base);
10305
10306 return fb;
d2dff872
CW
10307}
10308
10309static struct drm_framebuffer *
10310mode_fits_in_fbdev(struct drm_device *dev,
10311 struct drm_display_mode *mode)
10312{
0695726e 10313#ifdef CONFIG_DRM_FBDEV_EMULATION
d2dff872
CW
10314 struct drm_i915_private *dev_priv = dev->dev_private;
10315 struct drm_i915_gem_object *obj;
10316 struct drm_framebuffer *fb;
10317
4c0e5528 10318 if (!dev_priv->fbdev)
d2dff872
CW
10319 return NULL;
10320
4c0e5528 10321 if (!dev_priv->fbdev->fb)
d2dff872
CW
10322 return NULL;
10323
4c0e5528
DV
10324 obj = dev_priv->fbdev->fb->obj;
10325 BUG_ON(!obj);
10326
8bcd4553 10327 fb = &dev_priv->fbdev->fb->base;
01f2c773
VS
10328 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
10329 fb->bits_per_pixel))
d2dff872
CW
10330 return NULL;
10331
01f2c773 10332 if (obj->base.size < mode->vdisplay * fb->pitches[0])
d2dff872
CW
10333 return NULL;
10334
edde3617 10335 drm_framebuffer_reference(fb);
d2dff872 10336 return fb;
4520f53a
DV
10337#else
10338 return NULL;
10339#endif
d2dff872
CW
10340}
10341
d3a40d1b
ACO
10342static int intel_modeset_setup_plane_state(struct drm_atomic_state *state,
10343 struct drm_crtc *crtc,
10344 struct drm_display_mode *mode,
10345 struct drm_framebuffer *fb,
10346 int x, int y)
10347{
10348 struct drm_plane_state *plane_state;
10349 int hdisplay, vdisplay;
10350 int ret;
10351
10352 plane_state = drm_atomic_get_plane_state(state, crtc->primary);
10353 if (IS_ERR(plane_state))
10354 return PTR_ERR(plane_state);
10355
10356 if (mode)
10357 drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
10358 else
10359 hdisplay = vdisplay = 0;
10360
10361 ret = drm_atomic_set_crtc_for_plane(plane_state, fb ? crtc : NULL);
10362 if (ret)
10363 return ret;
10364 drm_atomic_set_fb_for_plane(plane_state, fb);
10365 plane_state->crtc_x = 0;
10366 plane_state->crtc_y = 0;
10367 plane_state->crtc_w = hdisplay;
10368 plane_state->crtc_h = vdisplay;
10369 plane_state->src_x = x << 16;
10370 plane_state->src_y = y << 16;
10371 plane_state->src_w = hdisplay << 16;
10372 plane_state->src_h = vdisplay << 16;
10373
10374 return 0;
10375}
10376
d2434ab7 10377bool intel_get_load_detect_pipe(struct drm_connector *connector,
7173188d 10378 struct drm_display_mode *mode,
51fd371b
RC
10379 struct intel_load_detect_pipe *old,
10380 struct drm_modeset_acquire_ctx *ctx)
79e53945
JB
10381{
10382 struct intel_crtc *intel_crtc;
d2434ab7
DV
10383 struct intel_encoder *intel_encoder =
10384 intel_attached_encoder(connector);
79e53945 10385 struct drm_crtc *possible_crtc;
4ef69c7a 10386 struct drm_encoder *encoder = &intel_encoder->base;
79e53945
JB
10387 struct drm_crtc *crtc = NULL;
10388 struct drm_device *dev = encoder->dev;
94352cf9 10389 struct drm_framebuffer *fb;
51fd371b 10390 struct drm_mode_config *config = &dev->mode_config;
edde3617 10391 struct drm_atomic_state *state = NULL, *restore_state = NULL;
944b0c76 10392 struct drm_connector_state *connector_state;
4be07317 10393 struct intel_crtc_state *crtc_state;
51fd371b 10394 int ret, i = -1;
79e53945 10395
d2dff872 10396 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
c23cc417 10397 connector->base.id, connector->name,
8e329a03 10398 encoder->base.id, encoder->name);
d2dff872 10399
edde3617
ML
10400 old->restore_state = NULL;
10401
51fd371b
RC
10402retry:
10403 ret = drm_modeset_lock(&config->connection_mutex, ctx);
10404 if (ret)
ad3c558f 10405 goto fail;
6e9f798d 10406
79e53945
JB
10407 /*
10408 * Algorithm gets a little messy:
7a5e4805 10409 *
79e53945
JB
10410 * - if the connector already has an assigned crtc, use it (but make
10411 * sure it's on first)
7a5e4805 10412 *
79e53945
JB
10413 * - try to find the first unused crtc that can drive this connector,
10414 * and use that if we find one
79e53945
JB
10415 */
10416
10417 /* See if we already have a CRTC for this connector */
edde3617
ML
10418 if (connector->state->crtc) {
10419 crtc = connector->state->crtc;
8261b191 10420
51fd371b 10421 ret = drm_modeset_lock(&crtc->mutex, ctx);
4d02e2de 10422 if (ret)
ad3c558f 10423 goto fail;
8261b191
CW
10424
10425 /* Make sure the crtc and connector are running */
edde3617 10426 goto found;
79e53945
JB
10427 }
10428
10429 /* Find an unused one (if possible) */
70e1e0ec 10430 for_each_crtc(dev, possible_crtc) {
79e53945
JB
10431 i++;
10432 if (!(encoder->possible_crtcs & (1 << i)))
10433 continue;
edde3617
ML
10434
10435 ret = drm_modeset_lock(&possible_crtc->mutex, ctx);
10436 if (ret)
10437 goto fail;
10438
10439 if (possible_crtc->state->enable) {
10440 drm_modeset_unlock(&possible_crtc->mutex);
a459249c 10441 continue;
edde3617 10442 }
a459249c
VS
10443
10444 crtc = possible_crtc;
10445 break;
79e53945
JB
10446 }
10447
10448 /*
10449 * If we didn't find an unused CRTC, don't use any.
10450 */
10451 if (!crtc) {
7173188d 10452 DRM_DEBUG_KMS("no pipe available for load-detect\n");
ad3c558f 10453 goto fail;
79e53945
JB
10454 }
10455
edde3617
ML
10456found:
10457 intel_crtc = to_intel_crtc(crtc);
10458
4d02e2de
DV
10459 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
10460 if (ret)
ad3c558f 10461 goto fail;
79e53945 10462
83a57153 10463 state = drm_atomic_state_alloc(dev);
edde3617
ML
10464 restore_state = drm_atomic_state_alloc(dev);
10465 if (!state || !restore_state) {
10466 ret = -ENOMEM;
10467 goto fail;
10468 }
83a57153
ACO
10469
10470 state->acquire_ctx = ctx;
edde3617 10471 restore_state->acquire_ctx = ctx;
83a57153 10472
944b0c76
ACO
10473 connector_state = drm_atomic_get_connector_state(state, connector);
10474 if (IS_ERR(connector_state)) {
10475 ret = PTR_ERR(connector_state);
10476 goto fail;
10477 }
10478
edde3617
ML
10479 ret = drm_atomic_set_crtc_for_connector(connector_state, crtc);
10480 if (ret)
10481 goto fail;
944b0c76 10482
4be07317
ACO
10483 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
10484 if (IS_ERR(crtc_state)) {
10485 ret = PTR_ERR(crtc_state);
10486 goto fail;
10487 }
10488
49d6fa21 10489 crtc_state->base.active = crtc_state->base.enable = true;
4be07317 10490
6492711d
CW
10491 if (!mode)
10492 mode = &load_detect_mode;
79e53945 10493
d2dff872
CW
10494 /* We need a framebuffer large enough to accommodate all accesses
10495 * that the plane may generate whilst we perform load detection.
10496 * We can not rely on the fbcon either being present (we get called
10497 * during its initialisation to detect all boot displays, or it may
10498 * not even exist) or that it is large enough to satisfy the
10499 * requested mode.
10500 */
94352cf9
DV
10501 fb = mode_fits_in_fbdev(dev, mode);
10502 if (fb == NULL) {
d2dff872 10503 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
94352cf9 10504 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
d2dff872
CW
10505 } else
10506 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
94352cf9 10507 if (IS_ERR(fb)) {
d2dff872 10508 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
412b61d8 10509 goto fail;
79e53945 10510 }
79e53945 10511
d3a40d1b
ACO
10512 ret = intel_modeset_setup_plane_state(state, crtc, mode, fb, 0, 0);
10513 if (ret)
10514 goto fail;
10515
edde3617
ML
10516 drm_framebuffer_unreference(fb);
10517
10518 ret = drm_atomic_set_mode_for_crtc(&crtc_state->base, mode);
10519 if (ret)
10520 goto fail;
10521
10522 ret = PTR_ERR_OR_ZERO(drm_atomic_get_connector_state(restore_state, connector));
10523 if (!ret)
10524 ret = PTR_ERR_OR_ZERO(drm_atomic_get_crtc_state(restore_state, crtc));
10525 if (!ret)
10526 ret = PTR_ERR_OR_ZERO(drm_atomic_get_plane_state(restore_state, crtc->primary));
10527 if (ret) {
10528 DRM_DEBUG_KMS("Failed to create a copy of old state to restore: %i\n", ret);
10529 goto fail;
10530 }
8c7b5ccb 10531
3ba86073
ML
10532 ret = drm_atomic_commit(state);
10533 if (ret) {
6492711d 10534 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
412b61d8 10535 goto fail;
79e53945 10536 }
edde3617
ML
10537
10538 old->restore_state = restore_state;
7173188d 10539
79e53945 10540 /* let the connector get through one full cycle before testing */
9d0498a2 10541 intel_wait_for_vblank(dev, intel_crtc->pipe);
7173188d 10542 return true;
412b61d8 10543
ad3c558f 10544fail:
e5d958ef 10545 drm_atomic_state_free(state);
edde3617
ML
10546 drm_atomic_state_free(restore_state);
10547 restore_state = state = NULL;
83a57153 10548
51fd371b
RC
10549 if (ret == -EDEADLK) {
10550 drm_modeset_backoff(ctx);
10551 goto retry;
10552 }
10553
412b61d8 10554 return false;
79e53945
JB
10555}
10556
d2434ab7 10557void intel_release_load_detect_pipe(struct drm_connector *connector,
49172fee
ACO
10558 struct intel_load_detect_pipe *old,
10559 struct drm_modeset_acquire_ctx *ctx)
79e53945 10560{
d2434ab7
DV
10561 struct intel_encoder *intel_encoder =
10562 intel_attached_encoder(connector);
4ef69c7a 10563 struct drm_encoder *encoder = &intel_encoder->base;
edde3617 10564 struct drm_atomic_state *state = old->restore_state;
d3a40d1b 10565 int ret;
79e53945 10566
d2dff872 10567 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
c23cc417 10568 connector->base.id, connector->name,
8e329a03 10569 encoder->base.id, encoder->name);
d2dff872 10570
edde3617 10571 if (!state)
0622a53c 10572 return;
79e53945 10573
edde3617
ML
10574 ret = drm_atomic_commit(state);
10575 if (ret) {
10576 DRM_DEBUG_KMS("Couldn't release load detect pipe: %i\n", ret);
10577 drm_atomic_state_free(state);
10578 }
79e53945
JB
10579}
10580
da4a1efa 10581static int i9xx_pll_refclk(struct drm_device *dev,
5cec258b 10582 const struct intel_crtc_state *pipe_config)
da4a1efa
VS
10583{
10584 struct drm_i915_private *dev_priv = dev->dev_private;
10585 u32 dpll = pipe_config->dpll_hw_state.dpll;
10586
10587 if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
e91e941b 10588 return dev_priv->vbt.lvds_ssc_freq;
da4a1efa
VS
10589 else if (HAS_PCH_SPLIT(dev))
10590 return 120000;
10591 else if (!IS_GEN2(dev))
10592 return 96000;
10593 else
10594 return 48000;
10595}
10596
79e53945 10597/* Returns the clock of the currently programmed mode of the given pipe. */
f1f644dc 10598static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 10599 struct intel_crtc_state *pipe_config)
79e53945 10600{
f1f644dc 10601 struct drm_device *dev = crtc->base.dev;
79e53945 10602 struct drm_i915_private *dev_priv = dev->dev_private;
f1f644dc 10603 int pipe = pipe_config->cpu_transcoder;
293623f7 10604 u32 dpll = pipe_config->dpll_hw_state.dpll;
79e53945
JB
10605 u32 fp;
10606 intel_clock_t clock;
dccbea3b 10607 int port_clock;
da4a1efa 10608 int refclk = i9xx_pll_refclk(dev, pipe_config);
79e53945
JB
10609
10610 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
293623f7 10611 fp = pipe_config->dpll_hw_state.fp0;
79e53945 10612 else
293623f7 10613 fp = pipe_config->dpll_hw_state.fp1;
79e53945
JB
10614
10615 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
f2b115e6
AJ
10616 if (IS_PINEVIEW(dev)) {
10617 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
10618 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
2177832f
SL
10619 } else {
10620 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
10621 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
10622 }
10623
a6c45cf0 10624 if (!IS_GEN2(dev)) {
f2b115e6
AJ
10625 if (IS_PINEVIEW(dev))
10626 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
10627 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
2177832f
SL
10628 else
10629 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
79e53945
JB
10630 DPLL_FPA01_P1_POST_DIV_SHIFT);
10631
10632 switch (dpll & DPLL_MODE_MASK) {
10633 case DPLLB_MODE_DAC_SERIAL:
10634 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
10635 5 : 10;
10636 break;
10637 case DPLLB_MODE_LVDS:
10638 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
10639 7 : 14;
10640 break;
10641 default:
28c97730 10642 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
79e53945 10643 "mode\n", (int)(dpll & DPLL_MODE_MASK));
f1f644dc 10644 return;
79e53945
JB
10645 }
10646
ac58c3f0 10647 if (IS_PINEVIEW(dev))
dccbea3b 10648 port_clock = pnv_calc_dpll_params(refclk, &clock);
ac58c3f0 10649 else
dccbea3b 10650 port_clock = i9xx_calc_dpll_params(refclk, &clock);
79e53945 10651 } else {
0fb58223 10652 u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
b1c560d1 10653 bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
79e53945
JB
10654
10655 if (is_lvds) {
10656 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
10657 DPLL_FPA01_P1_POST_DIV_SHIFT);
b1c560d1
VS
10658
10659 if (lvds & LVDS_CLKB_POWER_UP)
10660 clock.p2 = 7;
10661 else
10662 clock.p2 = 14;
79e53945
JB
10663 } else {
10664 if (dpll & PLL_P1_DIVIDE_BY_TWO)
10665 clock.p1 = 2;
10666 else {
10667 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
10668 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
10669 }
10670 if (dpll & PLL_P2_DIVIDE_BY_4)
10671 clock.p2 = 4;
10672 else
10673 clock.p2 = 2;
79e53945 10674 }
da4a1efa 10675
dccbea3b 10676 port_clock = i9xx_calc_dpll_params(refclk, &clock);
79e53945
JB
10677 }
10678
18442d08
VS
10679 /*
10680 * This value includes pixel_multiplier. We will use
241bfc38 10681 * port_clock to compute adjusted_mode.crtc_clock in the
18442d08
VS
10682 * encoder's get_config() function.
10683 */
dccbea3b 10684 pipe_config->port_clock = port_clock;
f1f644dc
JB
10685}
10686
6878da05
VS
10687int intel_dotclock_calculate(int link_freq,
10688 const struct intel_link_m_n *m_n)
f1f644dc 10689{
f1f644dc
JB
10690 /*
10691 * The calculation for the data clock is:
1041a02f 10692 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
f1f644dc 10693 * But we want to avoid losing precison if possible, so:
1041a02f 10694 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
f1f644dc
JB
10695 *
10696 * and the link clock is simpler:
1041a02f 10697 * link_clock = (m * link_clock) / n
f1f644dc
JB
10698 */
10699
6878da05
VS
10700 if (!m_n->link_n)
10701 return 0;
f1f644dc 10702
6878da05
VS
10703 return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
10704}
f1f644dc 10705
18442d08 10706static void ironlake_pch_clock_get(struct intel_crtc *crtc,
5cec258b 10707 struct intel_crtc_state *pipe_config)
6878da05 10708{
e3b247da 10709 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
79e53945 10710
18442d08
VS
10711 /* read out port_clock from the DPLL */
10712 i9xx_crtc_clock_get(crtc, pipe_config);
f1f644dc 10713
f1f644dc 10714 /*
e3b247da
VS
10715 * In case there is an active pipe without active ports,
10716 * we may need some idea for the dotclock anyway.
10717 * Calculate one based on the FDI configuration.
79e53945 10718 */
2d112de7 10719 pipe_config->base.adjusted_mode.crtc_clock =
21a727b3 10720 intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
18442d08 10721 &pipe_config->fdi_m_n);
79e53945
JB
10722}
10723
10724/** Returns the currently programmed mode of the given pipe. */
10725struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
10726 struct drm_crtc *crtc)
10727{
548f245b 10728 struct drm_i915_private *dev_priv = dev->dev_private;
79e53945 10729 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6e3c9717 10730 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
79e53945 10731 struct drm_display_mode *mode;
3f36b937 10732 struct intel_crtc_state *pipe_config;
fe2b8f9d
PZ
10733 int htot = I915_READ(HTOTAL(cpu_transcoder));
10734 int hsync = I915_READ(HSYNC(cpu_transcoder));
10735 int vtot = I915_READ(VTOTAL(cpu_transcoder));
10736 int vsync = I915_READ(VSYNC(cpu_transcoder));
293623f7 10737 enum pipe pipe = intel_crtc->pipe;
79e53945
JB
10738
10739 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
10740 if (!mode)
10741 return NULL;
10742
3f36b937
TU
10743 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
10744 if (!pipe_config) {
10745 kfree(mode);
10746 return NULL;
10747 }
10748
f1f644dc
JB
10749 /*
10750 * Construct a pipe_config sufficient for getting the clock info
10751 * back out of crtc_clock_get.
10752 *
10753 * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
10754 * to use a real value here instead.
10755 */
3f36b937
TU
10756 pipe_config->cpu_transcoder = (enum transcoder) pipe;
10757 pipe_config->pixel_multiplier = 1;
10758 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(pipe));
10759 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(pipe));
10760 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(pipe));
10761 i9xx_crtc_clock_get(intel_crtc, pipe_config);
10762
10763 mode->clock = pipe_config->port_clock / pipe_config->pixel_multiplier;
79e53945
JB
10764 mode->hdisplay = (htot & 0xffff) + 1;
10765 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
10766 mode->hsync_start = (hsync & 0xffff) + 1;
10767 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
10768 mode->vdisplay = (vtot & 0xffff) + 1;
10769 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
10770 mode->vsync_start = (vsync & 0xffff) + 1;
10771 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
10772
10773 drm_mode_set_name(mode);
79e53945 10774
3f36b937
TU
10775 kfree(pipe_config);
10776
79e53945
JB
10777 return mode;
10778}
10779
f047e395
CW
10780void intel_mark_busy(struct drm_device *dev)
10781{
c67a470b
PZ
10782 struct drm_i915_private *dev_priv = dev->dev_private;
10783
f62a0076
CW
10784 if (dev_priv->mm.busy)
10785 return;
10786
43694d69 10787 intel_runtime_pm_get(dev_priv);
c67a470b 10788 i915_update_gfx_val(dev_priv);
43cf3bf0
CW
10789 if (INTEL_INFO(dev)->gen >= 6)
10790 gen6_rps_busy(dev_priv);
f62a0076 10791 dev_priv->mm.busy = true;
f047e395
CW
10792}
10793
10794void intel_mark_idle(struct drm_device *dev)
652c393a 10795{
c67a470b 10796 struct drm_i915_private *dev_priv = dev->dev_private;
652c393a 10797
f62a0076
CW
10798 if (!dev_priv->mm.busy)
10799 return;
10800
10801 dev_priv->mm.busy = false;
10802
3d13ef2e 10803 if (INTEL_INFO(dev)->gen >= 6)
b29c19b6 10804 gen6_rps_idle(dev->dev_private);
bb4cdd53 10805
43694d69 10806 intel_runtime_pm_put(dev_priv);
652c393a
JB
10807}
10808
79e53945
JB
10809static void intel_crtc_destroy(struct drm_crtc *crtc)
10810{
10811 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
67e77c5a
DV
10812 struct drm_device *dev = crtc->dev;
10813 struct intel_unpin_work *work;
67e77c5a 10814
5e2d7afc 10815 spin_lock_irq(&dev->event_lock);
67e77c5a
DV
10816 work = intel_crtc->unpin_work;
10817 intel_crtc->unpin_work = NULL;
5e2d7afc 10818 spin_unlock_irq(&dev->event_lock);
67e77c5a
DV
10819
10820 if (work) {
10821 cancel_work_sync(&work->work);
10822 kfree(work);
10823 }
79e53945
JB
10824
10825 drm_crtc_cleanup(crtc);
67e77c5a 10826
79e53945
JB
10827 kfree(intel_crtc);
10828}
10829
6b95a207
KH
10830static void intel_unpin_work_fn(struct work_struct *__work)
10831{
10832 struct intel_unpin_work *work =
10833 container_of(__work, struct intel_unpin_work, work);
a9ff8714
VS
10834 struct intel_crtc *crtc = to_intel_crtc(work->crtc);
10835 struct drm_device *dev = crtc->base.dev;
10836 struct drm_plane *primary = crtc->base.primary;
6b95a207 10837
b4a98e57 10838 mutex_lock(&dev->struct_mutex);
3465c580 10839 intel_unpin_fb_obj(work->old_fb, primary->state->rotation);
05394f39 10840 drm_gem_object_unreference(&work->pending_flip_obj->base);
d9e86c0e 10841
f06cc1b9 10842 if (work->flip_queued_req)
146d84f0 10843 i915_gem_request_assign(&work->flip_queued_req, NULL);
b4a98e57
CW
10844 mutex_unlock(&dev->struct_mutex);
10845
a9ff8714 10846 intel_frontbuffer_flip_complete(dev, to_intel_plane(primary)->frontbuffer_bit);
1eb52238 10847 intel_fbc_post_update(crtc);
89ed88ba 10848 drm_framebuffer_unreference(work->old_fb);
f99d7069 10849
a9ff8714
VS
10850 BUG_ON(atomic_read(&crtc->unpin_work_count) == 0);
10851 atomic_dec(&crtc->unpin_work_count);
b4a98e57 10852
6b95a207
KH
10853 kfree(work);
10854}
10855
1afe3e9d 10856static void do_intel_finish_page_flip(struct drm_device *dev,
49b14a5c 10857 struct drm_crtc *crtc)
6b95a207 10858{
6b95a207
KH
10859 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10860 struct intel_unpin_work *work;
6b95a207
KH
10861 unsigned long flags;
10862
10863 /* Ignore early vblank irqs */
10864 if (intel_crtc == NULL)
10865 return;
10866
f326038a
DV
10867 /*
10868 * This is called both by irq handlers and the reset code (to complete
10869 * lost pageflips) so needs the full irqsave spinlocks.
10870 */
6b95a207
KH
10871 spin_lock_irqsave(&dev->event_lock, flags);
10872 work = intel_crtc->unpin_work;
e7d841ca
CW
10873
10874 /* Ensure we don't miss a work->pending update ... */
10875 smp_rmb();
10876
10877 if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
6b95a207
KH
10878 spin_unlock_irqrestore(&dev->event_lock, flags);
10879 return;
10880 }
10881
d6bbafa1 10882 page_flip_completed(intel_crtc);
0af7e4df 10883
6b95a207 10884 spin_unlock_irqrestore(&dev->event_lock, flags);
6b95a207
KH
10885}
10886
1afe3e9d
JB
10887void intel_finish_page_flip(struct drm_device *dev, int pipe)
10888{
fbee40df 10889 struct drm_i915_private *dev_priv = dev->dev_private;
1afe3e9d
JB
10890 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
10891
49b14a5c 10892 do_intel_finish_page_flip(dev, crtc);
1afe3e9d
JB
10893}
10894
10895void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
10896{
fbee40df 10897 struct drm_i915_private *dev_priv = dev->dev_private;
1afe3e9d
JB
10898 struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
10899
49b14a5c 10900 do_intel_finish_page_flip(dev, crtc);
1afe3e9d
JB
10901}
10902
75f7f3ec
VS
10903/* Is 'a' after or equal to 'b'? */
10904static bool g4x_flip_count_after_eq(u32 a, u32 b)
10905{
10906 return !((a - b) & 0x80000000);
10907}
10908
10909static bool page_flip_finished(struct intel_crtc *crtc)
10910{
10911 struct drm_device *dev = crtc->base.dev;
10912 struct drm_i915_private *dev_priv = dev->dev_private;
c19ae989 10913 unsigned reset_counter;
75f7f3ec 10914
c19ae989
CW
10915 reset_counter = i915_reset_counter(&dev_priv->gpu_error);
10916 if (crtc->reset_counter != reset_counter ||
10917 __i915_reset_in_progress_or_wedged(reset_counter))
bdfa7542
VS
10918 return true;
10919
75f7f3ec
VS
10920 /*
10921 * The relevant registers doen't exist on pre-ctg.
10922 * As the flip done interrupt doesn't trigger for mmio
10923 * flips on gmch platforms, a flip count check isn't
10924 * really needed there. But since ctg has the registers,
10925 * include it in the check anyway.
10926 */
10927 if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
10928 return true;
10929
e8861675
ML
10930 /*
10931 * BDW signals flip done immediately if the plane
10932 * is disabled, even if the plane enable is already
10933 * armed to occur at the next vblank :(
10934 */
10935
75f7f3ec
VS
10936 /*
10937 * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
10938 * used the same base address. In that case the mmio flip might
10939 * have completed, but the CS hasn't even executed the flip yet.
10940 *
10941 * A flip count check isn't enough as the CS might have updated
10942 * the base address just after start of vblank, but before we
10943 * managed to process the interrupt. This means we'd complete the
10944 * CS flip too soon.
10945 *
10946 * Combining both checks should get us a good enough result. It may
10947 * still happen that the CS flip has been executed, but has not
10948 * yet actually completed. But in case the base address is the same
10949 * anyway, we don't really care.
10950 */
10951 return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
10952 crtc->unpin_work->gtt_offset &&
fd8f507c 10953 g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_G4X(crtc->pipe)),
75f7f3ec
VS
10954 crtc->unpin_work->flip_count);
10955}
10956
6b95a207
KH
10957void intel_prepare_page_flip(struct drm_device *dev, int plane)
10958{
fbee40df 10959 struct drm_i915_private *dev_priv = dev->dev_private;
6b95a207
KH
10960 struct intel_crtc *intel_crtc =
10961 to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
10962 unsigned long flags;
10963
f326038a
DV
10964
10965 /*
10966 * This is called both by irq handlers and the reset code (to complete
10967 * lost pageflips) so needs the full irqsave spinlocks.
10968 *
10969 * NB: An MMIO update of the plane base pointer will also
e7d841ca
CW
10970 * generate a page-flip completion irq, i.e. every modeset
10971 * is also accompanied by a spurious intel_prepare_page_flip().
10972 */
6b95a207 10973 spin_lock_irqsave(&dev->event_lock, flags);
75f7f3ec 10974 if (intel_crtc->unpin_work && page_flip_finished(intel_crtc))
e7d841ca 10975 atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
6b95a207
KH
10976 spin_unlock_irqrestore(&dev->event_lock, flags);
10977}
10978
6042639c 10979static inline void intel_mark_page_flip_active(struct intel_unpin_work *work)
e7d841ca
CW
10980{
10981 /* Ensure that the work item is consistent when activating it ... */
10982 smp_wmb();
6042639c 10983 atomic_set(&work->pending, INTEL_FLIP_PENDING);
e7d841ca
CW
10984 /* and that it is marked active as soon as the irq could fire. */
10985 smp_wmb();
10986}
10987
8c9f3aaf
JB
10988static int intel_gen2_queue_flip(struct drm_device *dev,
10989 struct drm_crtc *crtc,
10990 struct drm_framebuffer *fb,
ed8d1975 10991 struct drm_i915_gem_object *obj,
6258fbe2 10992 struct drm_i915_gem_request *req,
ed8d1975 10993 uint32_t flags)
8c9f3aaf 10994{
4a570db5 10995 struct intel_engine_cs *engine = req->engine;
8c9f3aaf 10996 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8c9f3aaf
JB
10997 u32 flip_mask;
10998 int ret;
10999
5fb9de1a 11000 ret = intel_ring_begin(req, 6);
8c9f3aaf 11001 if (ret)
4fa62c89 11002 return ret;
8c9f3aaf
JB
11003
11004 /* Can't queue multiple flips, so wait for the previous
11005 * one to finish before executing the next.
11006 */
11007 if (intel_crtc->plane)
11008 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
11009 else
11010 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
e2f80391
TU
11011 intel_ring_emit(engine, MI_WAIT_FOR_EVENT | flip_mask);
11012 intel_ring_emit(engine, MI_NOOP);
11013 intel_ring_emit(engine, MI_DISPLAY_FLIP |
6d90c952 11014 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
e2f80391
TU
11015 intel_ring_emit(engine, fb->pitches[0]);
11016 intel_ring_emit(engine, intel_crtc->unpin_work->gtt_offset);
11017 intel_ring_emit(engine, 0); /* aux display base address, unused */
e7d841ca 11018
6042639c 11019 intel_mark_page_flip_active(intel_crtc->unpin_work);
83d4092b 11020 return 0;
8c9f3aaf
JB
11021}
11022
11023static int intel_gen3_queue_flip(struct drm_device *dev,
11024 struct drm_crtc *crtc,
11025 struct drm_framebuffer *fb,
ed8d1975 11026 struct drm_i915_gem_object *obj,
6258fbe2 11027 struct drm_i915_gem_request *req,
ed8d1975 11028 uint32_t flags)
8c9f3aaf 11029{
4a570db5 11030 struct intel_engine_cs *engine = req->engine;
8c9f3aaf 11031 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8c9f3aaf
JB
11032 u32 flip_mask;
11033 int ret;
11034
5fb9de1a 11035 ret = intel_ring_begin(req, 6);
8c9f3aaf 11036 if (ret)
4fa62c89 11037 return ret;
8c9f3aaf
JB
11038
11039 if (intel_crtc->plane)
11040 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
11041 else
11042 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
e2f80391
TU
11043 intel_ring_emit(engine, MI_WAIT_FOR_EVENT | flip_mask);
11044 intel_ring_emit(engine, MI_NOOP);
11045 intel_ring_emit(engine, MI_DISPLAY_FLIP_I915 |
6d90c952 11046 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
e2f80391
TU
11047 intel_ring_emit(engine, fb->pitches[0]);
11048 intel_ring_emit(engine, intel_crtc->unpin_work->gtt_offset);
11049 intel_ring_emit(engine, MI_NOOP);
6d90c952 11050
6042639c 11051 intel_mark_page_flip_active(intel_crtc->unpin_work);
83d4092b 11052 return 0;
8c9f3aaf
JB
11053}
11054
11055static int intel_gen4_queue_flip(struct drm_device *dev,
11056 struct drm_crtc *crtc,
11057 struct drm_framebuffer *fb,
ed8d1975 11058 struct drm_i915_gem_object *obj,
6258fbe2 11059 struct drm_i915_gem_request *req,
ed8d1975 11060 uint32_t flags)
8c9f3aaf 11061{
4a570db5 11062 struct intel_engine_cs *engine = req->engine;
8c9f3aaf
JB
11063 struct drm_i915_private *dev_priv = dev->dev_private;
11064 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11065 uint32_t pf, pipesrc;
11066 int ret;
11067
5fb9de1a 11068 ret = intel_ring_begin(req, 4);
8c9f3aaf 11069 if (ret)
4fa62c89 11070 return ret;
8c9f3aaf
JB
11071
11072 /* i965+ uses the linear or tiled offsets from the
11073 * Display Registers (which do not change across a page-flip)
11074 * so we need only reprogram the base address.
11075 */
e2f80391 11076 intel_ring_emit(engine, MI_DISPLAY_FLIP |
6d90c952 11077 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
e2f80391
TU
11078 intel_ring_emit(engine, fb->pitches[0]);
11079 intel_ring_emit(engine, intel_crtc->unpin_work->gtt_offset |
c2c75131 11080 obj->tiling_mode);
8c9f3aaf
JB
11081
11082 /* XXX Enabling the panel-fitter across page-flip is so far
11083 * untested on non-native modes, so ignore it for now.
11084 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
11085 */
11086 pf = 0;
11087 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
e2f80391 11088 intel_ring_emit(engine, pf | pipesrc);
e7d841ca 11089
6042639c 11090 intel_mark_page_flip_active(intel_crtc->unpin_work);
83d4092b 11091 return 0;
8c9f3aaf
JB
11092}
11093
11094static int intel_gen6_queue_flip(struct drm_device *dev,
11095 struct drm_crtc *crtc,
11096 struct drm_framebuffer *fb,
ed8d1975 11097 struct drm_i915_gem_object *obj,
6258fbe2 11098 struct drm_i915_gem_request *req,
ed8d1975 11099 uint32_t flags)
8c9f3aaf 11100{
4a570db5 11101 struct intel_engine_cs *engine = req->engine;
8c9f3aaf
JB
11102 struct drm_i915_private *dev_priv = dev->dev_private;
11103 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11104 uint32_t pf, pipesrc;
11105 int ret;
11106
5fb9de1a 11107 ret = intel_ring_begin(req, 4);
8c9f3aaf 11108 if (ret)
4fa62c89 11109 return ret;
8c9f3aaf 11110
e2f80391 11111 intel_ring_emit(engine, MI_DISPLAY_FLIP |
6d90c952 11112 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
e2f80391
TU
11113 intel_ring_emit(engine, fb->pitches[0] | obj->tiling_mode);
11114 intel_ring_emit(engine, intel_crtc->unpin_work->gtt_offset);
8c9f3aaf 11115
dc257cf1
DV
11116 /* Contrary to the suggestions in the documentation,
11117 * "Enable Panel Fitter" does not seem to be required when page
11118 * flipping with a non-native mode, and worse causes a normal
11119 * modeset to fail.
11120 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
11121 */
11122 pf = 0;
8c9f3aaf 11123 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
e2f80391 11124 intel_ring_emit(engine, pf | pipesrc);
e7d841ca 11125
6042639c 11126 intel_mark_page_flip_active(intel_crtc->unpin_work);
83d4092b 11127 return 0;
8c9f3aaf
JB
11128}
11129
7c9017e5
JB
11130static int intel_gen7_queue_flip(struct drm_device *dev,
11131 struct drm_crtc *crtc,
11132 struct drm_framebuffer *fb,
ed8d1975 11133 struct drm_i915_gem_object *obj,
6258fbe2 11134 struct drm_i915_gem_request *req,
ed8d1975 11135 uint32_t flags)
7c9017e5 11136{
4a570db5 11137 struct intel_engine_cs *engine = req->engine;
7c9017e5 11138 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
cb05d8de 11139 uint32_t plane_bit = 0;
ffe74d75
CW
11140 int len, ret;
11141
eba905b2 11142 switch (intel_crtc->plane) {
cb05d8de
DV
11143 case PLANE_A:
11144 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
11145 break;
11146 case PLANE_B:
11147 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
11148 break;
11149 case PLANE_C:
11150 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
11151 break;
11152 default:
11153 WARN_ONCE(1, "unknown plane in flip command\n");
4fa62c89 11154 return -ENODEV;
cb05d8de
DV
11155 }
11156
ffe74d75 11157 len = 4;
e2f80391 11158 if (engine->id == RCS) {
ffe74d75 11159 len += 6;
f476828a
DL
11160 /*
11161 * On Gen 8, SRM is now taking an extra dword to accommodate
11162 * 48bits addresses, and we need a NOOP for the batch size to
11163 * stay even.
11164 */
11165 if (IS_GEN8(dev))
11166 len += 2;
11167 }
ffe74d75 11168
f66fab8e
VS
11169 /*
11170 * BSpec MI_DISPLAY_FLIP for IVB:
11171 * "The full packet must be contained within the same cache line."
11172 *
11173 * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
11174 * cacheline, if we ever start emitting more commands before
11175 * the MI_DISPLAY_FLIP we may need to first emit everything else,
11176 * then do the cacheline alignment, and finally emit the
11177 * MI_DISPLAY_FLIP.
11178 */
bba09b12 11179 ret = intel_ring_cacheline_align(req);
f66fab8e 11180 if (ret)
4fa62c89 11181 return ret;
f66fab8e 11182
5fb9de1a 11183 ret = intel_ring_begin(req, len);
7c9017e5 11184 if (ret)
4fa62c89 11185 return ret;
7c9017e5 11186
ffe74d75
CW
11187 /* Unmask the flip-done completion message. Note that the bspec says that
11188 * we should do this for both the BCS and RCS, and that we must not unmask
11189 * more than one flip event at any time (or ensure that one flip message
11190 * can be sent by waiting for flip-done prior to queueing new flips).
11191 * Experimentation says that BCS works despite DERRMR masking all
11192 * flip-done completion events and that unmasking all planes at once
11193 * for the RCS also doesn't appear to drop events. Setting the DERRMR
11194 * to zero does lead to lockups within MI_DISPLAY_FLIP.
11195 */
e2f80391
TU
11196 if (engine->id == RCS) {
11197 intel_ring_emit(engine, MI_LOAD_REGISTER_IMM(1));
11198 intel_ring_emit_reg(engine, DERRMR);
11199 intel_ring_emit(engine, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
11200 DERRMR_PIPEB_PRI_FLIP_DONE |
11201 DERRMR_PIPEC_PRI_FLIP_DONE));
f476828a 11202 if (IS_GEN8(dev))
e2f80391 11203 intel_ring_emit(engine, MI_STORE_REGISTER_MEM_GEN8 |
f476828a
DL
11204 MI_SRM_LRM_GLOBAL_GTT);
11205 else
e2f80391 11206 intel_ring_emit(engine, MI_STORE_REGISTER_MEM |
f476828a 11207 MI_SRM_LRM_GLOBAL_GTT);
e2f80391
TU
11208 intel_ring_emit_reg(engine, DERRMR);
11209 intel_ring_emit(engine, engine->scratch.gtt_offset + 256);
f476828a 11210 if (IS_GEN8(dev)) {
e2f80391
TU
11211 intel_ring_emit(engine, 0);
11212 intel_ring_emit(engine, MI_NOOP);
f476828a 11213 }
ffe74d75
CW
11214 }
11215
e2f80391
TU
11216 intel_ring_emit(engine, MI_DISPLAY_FLIP_I915 | plane_bit);
11217 intel_ring_emit(engine, (fb->pitches[0] | obj->tiling_mode));
11218 intel_ring_emit(engine, intel_crtc->unpin_work->gtt_offset);
11219 intel_ring_emit(engine, (MI_NOOP));
e7d841ca 11220
6042639c 11221 intel_mark_page_flip_active(intel_crtc->unpin_work);
83d4092b 11222 return 0;
7c9017e5
JB
11223}
11224
0bc40be8 11225static bool use_mmio_flip(struct intel_engine_cs *engine,
84c33a64
SG
11226 struct drm_i915_gem_object *obj)
11227{
11228 /*
11229 * This is not being used for older platforms, because
11230 * non-availability of flip done interrupt forces us to use
11231 * CS flips. Older platforms derive flip done using some clever
11232 * tricks involving the flip_pending status bits and vblank irqs.
11233 * So using MMIO flips there would disrupt this mechanism.
11234 */
11235
0bc40be8 11236 if (engine == NULL)
8e09bf83
CW
11237 return true;
11238
0bc40be8 11239 if (INTEL_INFO(engine->dev)->gen < 5)
84c33a64
SG
11240 return false;
11241
11242 if (i915.use_mmio_flip < 0)
11243 return false;
11244 else if (i915.use_mmio_flip > 0)
11245 return true;
14bf993e
OM
11246 else if (i915.enable_execlists)
11247 return true;
fd8e058a
AG
11248 else if (obj->base.dma_buf &&
11249 !reservation_object_test_signaled_rcu(obj->base.dma_buf->resv,
11250 false))
11251 return true;
84c33a64 11252 else
666796da 11253 return engine != i915_gem_request_get_engine(obj->last_write_req);
84c33a64
SG
11254}
11255
6042639c 11256static void skl_do_mmio_flip(struct intel_crtc *intel_crtc,
86efe24a 11257 unsigned int rotation,
6042639c 11258 struct intel_unpin_work *work)
ff944564
DL
11259{
11260 struct drm_device *dev = intel_crtc->base.dev;
11261 struct drm_i915_private *dev_priv = dev->dev_private;
11262 struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
ff944564 11263 const enum pipe pipe = intel_crtc->pipe;
86efe24a 11264 u32 ctl, stride, tile_height;
ff944564
DL
11265
11266 ctl = I915_READ(PLANE_CTL(pipe, 0));
11267 ctl &= ~PLANE_CTL_TILED_MASK;
2ebef630
TU
11268 switch (fb->modifier[0]) {
11269 case DRM_FORMAT_MOD_NONE:
11270 break;
11271 case I915_FORMAT_MOD_X_TILED:
ff944564 11272 ctl |= PLANE_CTL_TILED_X;
2ebef630
TU
11273 break;
11274 case I915_FORMAT_MOD_Y_TILED:
11275 ctl |= PLANE_CTL_TILED_Y;
11276 break;
11277 case I915_FORMAT_MOD_Yf_TILED:
11278 ctl |= PLANE_CTL_TILED_YF;
11279 break;
11280 default:
11281 MISSING_CASE(fb->modifier[0]);
11282 }
ff944564
DL
11283
11284 /*
11285 * The stride is either expressed as a multiple of 64 bytes chunks for
11286 * linear buffers or in number of tiles for tiled buffers.
11287 */
86efe24a
TU
11288 if (intel_rotation_90_or_270(rotation)) {
11289 /* stride = Surface height in tiles */
832be82f 11290 tile_height = intel_tile_height(dev_priv, fb->modifier[0], 0);
86efe24a
TU
11291 stride = DIV_ROUND_UP(fb->height, tile_height);
11292 } else {
11293 stride = fb->pitches[0] /
7b49f948
VS
11294 intel_fb_stride_alignment(dev_priv, fb->modifier[0],
11295 fb->pixel_format);
86efe24a 11296 }
ff944564
DL
11297
11298 /*
11299 * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
11300 * PLANE_SURF updates, the update is then guaranteed to be atomic.
11301 */
11302 I915_WRITE(PLANE_CTL(pipe, 0), ctl);
11303 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
11304
6042639c 11305 I915_WRITE(PLANE_SURF(pipe, 0), work->gtt_offset);
ff944564
DL
11306 POSTING_READ(PLANE_SURF(pipe, 0));
11307}
11308
6042639c
CW
11309static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc,
11310 struct intel_unpin_work *work)
84c33a64
SG
11311{
11312 struct drm_device *dev = intel_crtc->base.dev;
11313 struct drm_i915_private *dev_priv = dev->dev_private;
11314 struct intel_framebuffer *intel_fb =
11315 to_intel_framebuffer(intel_crtc->base.primary->fb);
11316 struct drm_i915_gem_object *obj = intel_fb->obj;
f0f59a00 11317 i915_reg_t reg = DSPCNTR(intel_crtc->plane);
84c33a64 11318 u32 dspcntr;
84c33a64 11319
84c33a64
SG
11320 dspcntr = I915_READ(reg);
11321
c5d97472
DL
11322 if (obj->tiling_mode != I915_TILING_NONE)
11323 dspcntr |= DISPPLANE_TILED;
11324 else
11325 dspcntr &= ~DISPPLANE_TILED;
11326
84c33a64
SG
11327 I915_WRITE(reg, dspcntr);
11328
6042639c 11329 I915_WRITE(DSPSURF(intel_crtc->plane), work->gtt_offset);
84c33a64 11330 POSTING_READ(DSPSURF(intel_crtc->plane));
ff944564
DL
11331}
11332
11333/*
11334 * XXX: This is the temporary way to update the plane registers until we get
11335 * around to using the usual plane update functions for MMIO flips
11336 */
6042639c 11337static void intel_do_mmio_flip(struct intel_mmio_flip *mmio_flip)
ff944564 11338{
6042639c
CW
11339 struct intel_crtc *crtc = mmio_flip->crtc;
11340 struct intel_unpin_work *work;
11341
11342 spin_lock_irq(&crtc->base.dev->event_lock);
11343 work = crtc->unpin_work;
11344 spin_unlock_irq(&crtc->base.dev->event_lock);
11345 if (work == NULL)
11346 return;
ff944564 11347
6042639c 11348 intel_mark_page_flip_active(work);
ff944564 11349
6042639c 11350 intel_pipe_update_start(crtc);
ff944564 11351
6042639c 11352 if (INTEL_INFO(mmio_flip->i915)->gen >= 9)
86efe24a 11353 skl_do_mmio_flip(crtc, mmio_flip->rotation, work);
ff944564
DL
11354 else
11355 /* use_mmio_flip() retricts MMIO flips to ilk+ */
6042639c 11356 ilk_do_mmio_flip(crtc, work);
ff944564 11357
6042639c 11358 intel_pipe_update_end(crtc);
84c33a64
SG
11359}
11360
9362c7c5 11361static void intel_mmio_flip_work_func(struct work_struct *work)
84c33a64 11362{
b2cfe0ab
CW
11363 struct intel_mmio_flip *mmio_flip =
11364 container_of(work, struct intel_mmio_flip, work);
fd8e058a
AG
11365 struct intel_framebuffer *intel_fb =
11366 to_intel_framebuffer(mmio_flip->crtc->base.primary->fb);
11367 struct drm_i915_gem_object *obj = intel_fb->obj;
84c33a64 11368
6042639c 11369 if (mmio_flip->req) {
eed29a5b 11370 WARN_ON(__i915_wait_request(mmio_flip->req,
b2cfe0ab 11371 mmio_flip->crtc->reset_counter,
bcafc4e3
CW
11372 false, NULL,
11373 &mmio_flip->i915->rps.mmioflips));
6042639c
CW
11374 i915_gem_request_unreference__unlocked(mmio_flip->req);
11375 }
84c33a64 11376
fd8e058a
AG
11377 /* For framebuffer backed by dmabuf, wait for fence */
11378 if (obj->base.dma_buf)
11379 WARN_ON(reservation_object_wait_timeout_rcu(obj->base.dma_buf->resv,
11380 false, false,
11381 MAX_SCHEDULE_TIMEOUT) < 0);
11382
6042639c 11383 intel_do_mmio_flip(mmio_flip);
b2cfe0ab 11384 kfree(mmio_flip);
84c33a64
SG
11385}
11386
11387static int intel_queue_mmio_flip(struct drm_device *dev,
11388 struct drm_crtc *crtc,
86efe24a 11389 struct drm_i915_gem_object *obj)
84c33a64 11390{
b2cfe0ab
CW
11391 struct intel_mmio_flip *mmio_flip;
11392
11393 mmio_flip = kmalloc(sizeof(*mmio_flip), GFP_KERNEL);
11394 if (mmio_flip == NULL)
11395 return -ENOMEM;
84c33a64 11396
bcafc4e3 11397 mmio_flip->i915 = to_i915(dev);
eed29a5b 11398 mmio_flip->req = i915_gem_request_reference(obj->last_write_req);
b2cfe0ab 11399 mmio_flip->crtc = to_intel_crtc(crtc);
86efe24a 11400 mmio_flip->rotation = crtc->primary->state->rotation;
536f5b5e 11401
b2cfe0ab
CW
11402 INIT_WORK(&mmio_flip->work, intel_mmio_flip_work_func);
11403 schedule_work(&mmio_flip->work);
84c33a64 11404
84c33a64
SG
11405 return 0;
11406}
11407
8c9f3aaf
JB
11408static int intel_default_queue_flip(struct drm_device *dev,
11409 struct drm_crtc *crtc,
11410 struct drm_framebuffer *fb,
ed8d1975 11411 struct drm_i915_gem_object *obj,
6258fbe2 11412 struct drm_i915_gem_request *req,
ed8d1975 11413 uint32_t flags)
8c9f3aaf
JB
11414{
11415 return -ENODEV;
11416}
11417
d6bbafa1
CW
11418static bool __intel_pageflip_stall_check(struct drm_device *dev,
11419 struct drm_crtc *crtc)
11420{
11421 struct drm_i915_private *dev_priv = dev->dev_private;
11422 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11423 struct intel_unpin_work *work = intel_crtc->unpin_work;
11424 u32 addr;
11425
11426 if (atomic_read(&work->pending) >= INTEL_FLIP_COMPLETE)
11427 return true;
11428
908565c2
CW
11429 if (atomic_read(&work->pending) < INTEL_FLIP_PENDING)
11430 return false;
11431
d6bbafa1
CW
11432 if (!work->enable_stall_check)
11433 return false;
11434
11435 if (work->flip_ready_vblank == 0) {
3a8a946e
DV
11436 if (work->flip_queued_req &&
11437 !i915_gem_request_completed(work->flip_queued_req, true))
d6bbafa1
CW
11438 return false;
11439
1e3feefd 11440 work->flip_ready_vblank = drm_crtc_vblank_count(crtc);
d6bbafa1
CW
11441 }
11442
1e3feefd 11443 if (drm_crtc_vblank_count(crtc) - work->flip_ready_vblank < 3)
d6bbafa1
CW
11444 return false;
11445
11446 /* Potential stall - if we see that the flip has happened,
11447 * assume a missed interrupt. */
11448 if (INTEL_INFO(dev)->gen >= 4)
11449 addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
11450 else
11451 addr = I915_READ(DSPADDR(intel_crtc->plane));
11452
11453 /* There is a potential issue here with a false positive after a flip
11454 * to the same address. We could address this by checking for a
11455 * non-incrementing frame counter.
11456 */
11457 return addr == work->gtt_offset;
11458}
11459
11460void intel_check_page_flip(struct drm_device *dev, int pipe)
11461{
11462 struct drm_i915_private *dev_priv = dev->dev_private;
11463 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
11464 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6ad790c0 11465 struct intel_unpin_work *work;
f326038a 11466
6c51d46f 11467 WARN_ON(!in_interrupt());
d6bbafa1
CW
11468
11469 if (crtc == NULL)
11470 return;
11471
f326038a 11472 spin_lock(&dev->event_lock);
6ad790c0
CW
11473 work = intel_crtc->unpin_work;
11474 if (work != NULL && __intel_pageflip_stall_check(dev, crtc)) {
d6bbafa1 11475 WARN_ONCE(1, "Kicking stuck page flip: queued at %d, now %d\n",
6ad790c0 11476 work->flip_queued_vblank, drm_vblank_count(dev, pipe));
d6bbafa1 11477 page_flip_completed(intel_crtc);
6ad790c0 11478 work = NULL;
d6bbafa1 11479 }
6ad790c0
CW
11480 if (work != NULL &&
11481 drm_vblank_count(dev, pipe) - work->flip_queued_vblank > 1)
11482 intel_queue_rps_boost_for_request(dev, work->flip_queued_req);
f326038a 11483 spin_unlock(&dev->event_lock);
d6bbafa1
CW
11484}
11485
6b95a207
KH
11486static int intel_crtc_page_flip(struct drm_crtc *crtc,
11487 struct drm_framebuffer *fb,
ed8d1975
KP
11488 struct drm_pending_vblank_event *event,
11489 uint32_t page_flip_flags)
6b95a207
KH
11490{
11491 struct drm_device *dev = crtc->dev;
11492 struct drm_i915_private *dev_priv = dev->dev_private;
f4510a27 11493 struct drm_framebuffer *old_fb = crtc->primary->fb;
2ff8fde1 11494 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
6b95a207 11495 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
455a6808 11496 struct drm_plane *primary = crtc->primary;
a071fa00 11497 enum pipe pipe = intel_crtc->pipe;
6b95a207 11498 struct intel_unpin_work *work;
e2f80391 11499 struct intel_engine_cs *engine;
cf5d8a46 11500 bool mmio_flip;
91af127f 11501 struct drm_i915_gem_request *request = NULL;
52e68630 11502 int ret;
6b95a207 11503
2ff8fde1
MR
11504 /*
11505 * drm_mode_page_flip_ioctl() should already catch this, but double
11506 * check to be safe. In the future we may enable pageflipping from
11507 * a disabled primary plane.
11508 */
11509 if (WARN_ON(intel_fb_obj(old_fb) == NULL))
11510 return -EBUSY;
11511
e6a595d2 11512 /* Can't change pixel format via MI display flips. */
f4510a27 11513 if (fb->pixel_format != crtc->primary->fb->pixel_format)
e6a595d2
VS
11514 return -EINVAL;
11515
11516 /*
11517 * TILEOFF/LINOFF registers can't be changed via MI display flips.
11518 * Note that pitch changes could also affect these register.
11519 */
11520 if (INTEL_INFO(dev)->gen > 3 &&
f4510a27
MR
11521 (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
11522 fb->pitches[0] != crtc->primary->fb->pitches[0]))
e6a595d2
VS
11523 return -EINVAL;
11524
f900db47
CW
11525 if (i915_terminally_wedged(&dev_priv->gpu_error))
11526 goto out_hang;
11527
b14c5679 11528 work = kzalloc(sizeof(*work), GFP_KERNEL);
6b95a207
KH
11529 if (work == NULL)
11530 return -ENOMEM;
11531
6b95a207 11532 work->event = event;
b4a98e57 11533 work->crtc = crtc;
ab8d6675 11534 work->old_fb = old_fb;
6b95a207
KH
11535 INIT_WORK(&work->work, intel_unpin_work_fn);
11536
87b6b101 11537 ret = drm_crtc_vblank_get(crtc);
7317c75e
JB
11538 if (ret)
11539 goto free_work;
11540
6b95a207 11541 /* We borrow the event spin lock for protecting unpin_work */
5e2d7afc 11542 spin_lock_irq(&dev->event_lock);
6b95a207 11543 if (intel_crtc->unpin_work) {
d6bbafa1
CW
11544 /* Before declaring the flip queue wedged, check if
11545 * the hardware completed the operation behind our backs.
11546 */
11547 if (__intel_pageflip_stall_check(dev, crtc)) {
11548 DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
11549 page_flip_completed(intel_crtc);
11550 } else {
11551 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
5e2d7afc 11552 spin_unlock_irq(&dev->event_lock);
468f0b44 11553
d6bbafa1
CW
11554 drm_crtc_vblank_put(crtc);
11555 kfree(work);
11556 return -EBUSY;
11557 }
6b95a207
KH
11558 }
11559 intel_crtc->unpin_work = work;
5e2d7afc 11560 spin_unlock_irq(&dev->event_lock);
6b95a207 11561
b4a98e57
CW
11562 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
11563 flush_workqueue(dev_priv->wq);
11564
75dfca80 11565 /* Reference the objects for the scheduled work. */
ab8d6675 11566 drm_framebuffer_reference(work->old_fb);
05394f39 11567 drm_gem_object_reference(&obj->base);
6b95a207 11568
f4510a27 11569 crtc->primary->fb = fb;
afd65eb4 11570 update_state_fb(crtc->primary);
e8216e50 11571 intel_fbc_pre_update(intel_crtc);
1ed1f968 11572
e1f99ce6 11573 work->pending_flip_obj = obj;
e1f99ce6 11574
89ed88ba
CW
11575 ret = i915_mutex_lock_interruptible(dev);
11576 if (ret)
11577 goto cleanup;
11578
b4a98e57 11579 atomic_inc(&intel_crtc->unpin_work_count);
c19ae989 11580 intel_crtc->reset_counter = i915_reset_counter(&dev_priv->gpu_error);
e1f99ce6 11581
75f7f3ec 11582 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
fd8f507c 11583 work->flip_count = I915_READ(PIPE_FLIPCOUNT_G4X(pipe)) + 1;
75f7f3ec 11584
666a4537 11585 if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
4a570db5 11586 engine = &dev_priv->engine[BCS];
ab8d6675 11587 if (obj->tiling_mode != intel_fb_obj(work->old_fb)->tiling_mode)
8e09bf83 11588 /* vlv: DISPLAY_FLIP fails to change tiling */
e2f80391 11589 engine = NULL;
48bf5b2d 11590 } else if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
4a570db5 11591 engine = &dev_priv->engine[BCS];
4fa62c89 11592 } else if (INTEL_INFO(dev)->gen >= 7) {
666796da 11593 engine = i915_gem_request_get_engine(obj->last_write_req);
e2f80391 11594 if (engine == NULL || engine->id != RCS)
4a570db5 11595 engine = &dev_priv->engine[BCS];
4fa62c89 11596 } else {
4a570db5 11597 engine = &dev_priv->engine[RCS];
4fa62c89
VS
11598 }
11599
e2f80391 11600 mmio_flip = use_mmio_flip(engine, obj);
cf5d8a46
CW
11601
11602 /* When using CS flips, we want to emit semaphores between rings.
11603 * However, when using mmio flips we will create a task to do the
11604 * synchronisation, so all we want here is to pin the framebuffer
11605 * into the display plane and skip any waits.
11606 */
7580d774 11607 if (!mmio_flip) {
e2f80391 11608 ret = i915_gem_object_sync(obj, engine, &request);
7580d774
ML
11609 if (ret)
11610 goto cleanup_pending;
11611 }
11612
3465c580 11613 ret = intel_pin_and_fence_fb_obj(fb, primary->state->rotation);
8c9f3aaf
JB
11614 if (ret)
11615 goto cleanup_pending;
6b95a207 11616
dedf278c
TU
11617 work->gtt_offset = intel_plane_obj_offset(to_intel_plane(primary),
11618 obj, 0);
11619 work->gtt_offset += intel_crtc->dspaddr_offset;
4fa62c89 11620
cf5d8a46 11621 if (mmio_flip) {
86efe24a 11622 ret = intel_queue_mmio_flip(dev, crtc, obj);
d6bbafa1
CW
11623 if (ret)
11624 goto cleanup_unpin;
11625
f06cc1b9
JH
11626 i915_gem_request_assign(&work->flip_queued_req,
11627 obj->last_write_req);
d6bbafa1 11628 } else {
6258fbe2 11629 if (!request) {
e2f80391 11630 request = i915_gem_request_alloc(engine, NULL);
26827088
DG
11631 if (IS_ERR(request)) {
11632 ret = PTR_ERR(request);
6258fbe2 11633 goto cleanup_unpin;
26827088 11634 }
6258fbe2
JH
11635 }
11636
11637 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, request,
d6bbafa1
CW
11638 page_flip_flags);
11639 if (ret)
11640 goto cleanup_unpin;
11641
6258fbe2 11642 i915_gem_request_assign(&work->flip_queued_req, request);
d6bbafa1
CW
11643 }
11644
91af127f 11645 if (request)
75289874 11646 i915_add_request_no_flush(request);
91af127f 11647
1e3feefd 11648 work->flip_queued_vblank = drm_crtc_vblank_count(crtc);
d6bbafa1 11649 work->enable_stall_check = true;
4fa62c89 11650
ab8d6675 11651 i915_gem_track_fb(intel_fb_obj(work->old_fb), obj,
a9ff8714 11652 to_intel_plane(primary)->frontbuffer_bit);
c80ac854 11653 mutex_unlock(&dev->struct_mutex);
a071fa00 11654
a9ff8714
VS
11655 intel_frontbuffer_flip_prepare(dev,
11656 to_intel_plane(primary)->frontbuffer_bit);
6b95a207 11657
e5510fac
JB
11658 trace_i915_flip_request(intel_crtc->plane, obj);
11659
6b95a207 11660 return 0;
96b099fd 11661
4fa62c89 11662cleanup_unpin:
3465c580 11663 intel_unpin_fb_obj(fb, crtc->primary->state->rotation);
8c9f3aaf 11664cleanup_pending:
0aa498d5 11665 if (!IS_ERR_OR_NULL(request))
91af127f 11666 i915_gem_request_cancel(request);
b4a98e57 11667 atomic_dec(&intel_crtc->unpin_work_count);
89ed88ba
CW
11668 mutex_unlock(&dev->struct_mutex);
11669cleanup:
f4510a27 11670 crtc->primary->fb = old_fb;
afd65eb4 11671 update_state_fb(crtc->primary);
89ed88ba
CW
11672
11673 drm_gem_object_unreference_unlocked(&obj->base);
ab8d6675 11674 drm_framebuffer_unreference(work->old_fb);
96b099fd 11675
5e2d7afc 11676 spin_lock_irq(&dev->event_lock);
96b099fd 11677 intel_crtc->unpin_work = NULL;
5e2d7afc 11678 spin_unlock_irq(&dev->event_lock);
96b099fd 11679
87b6b101 11680 drm_crtc_vblank_put(crtc);
7317c75e 11681free_work:
96b099fd
CW
11682 kfree(work);
11683
f900db47 11684 if (ret == -EIO) {
02e0efb5
ML
11685 struct drm_atomic_state *state;
11686 struct drm_plane_state *plane_state;
11687
f900db47 11688out_hang:
02e0efb5
ML
11689 state = drm_atomic_state_alloc(dev);
11690 if (!state)
11691 return -ENOMEM;
11692 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
11693
11694retry:
11695 plane_state = drm_atomic_get_plane_state(state, primary);
11696 ret = PTR_ERR_OR_ZERO(plane_state);
11697 if (!ret) {
11698 drm_atomic_set_fb_for_plane(plane_state, fb);
11699
11700 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
11701 if (!ret)
11702 ret = drm_atomic_commit(state);
11703 }
11704
11705 if (ret == -EDEADLK) {
11706 drm_modeset_backoff(state->acquire_ctx);
11707 drm_atomic_state_clear(state);
11708 goto retry;
11709 }
11710
11711 if (ret)
11712 drm_atomic_state_free(state);
11713
f0d3dad3 11714 if (ret == 0 && event) {
5e2d7afc 11715 spin_lock_irq(&dev->event_lock);
a071fa00 11716 drm_send_vblank_event(dev, pipe, event);
5e2d7afc 11717 spin_unlock_irq(&dev->event_lock);
f0d3dad3 11718 }
f900db47 11719 }
96b099fd 11720 return ret;
6b95a207
KH
11721}
11722
da20eabd
ML
11723
11724/**
11725 * intel_wm_need_update - Check whether watermarks need updating
11726 * @plane: drm plane
11727 * @state: new plane state
11728 *
11729 * Check current plane state versus the new one to determine whether
11730 * watermarks need to be recalculated.
11731 *
11732 * Returns true or false.
11733 */
11734static bool intel_wm_need_update(struct drm_plane *plane,
11735 struct drm_plane_state *state)
11736{
d21fbe87
MR
11737 struct intel_plane_state *new = to_intel_plane_state(state);
11738 struct intel_plane_state *cur = to_intel_plane_state(plane->state);
11739
11740 /* Update watermarks on tiling or size changes. */
92826fcd
ML
11741 if (new->visible != cur->visible)
11742 return true;
11743
11744 if (!cur->base.fb || !new->base.fb)
11745 return false;
11746
11747 if (cur->base.fb->modifier[0] != new->base.fb->modifier[0] ||
11748 cur->base.rotation != new->base.rotation ||
d21fbe87
MR
11749 drm_rect_width(&new->src) != drm_rect_width(&cur->src) ||
11750 drm_rect_height(&new->src) != drm_rect_height(&cur->src) ||
11751 drm_rect_width(&new->dst) != drm_rect_width(&cur->dst) ||
11752 drm_rect_height(&new->dst) != drm_rect_height(&cur->dst))
2791a16c 11753 return true;
7809e5ae 11754
2791a16c 11755 return false;
7809e5ae
MR
11756}
11757
d21fbe87
MR
11758static bool needs_scaling(struct intel_plane_state *state)
11759{
11760 int src_w = drm_rect_width(&state->src) >> 16;
11761 int src_h = drm_rect_height(&state->src) >> 16;
11762 int dst_w = drm_rect_width(&state->dst);
11763 int dst_h = drm_rect_height(&state->dst);
11764
11765 return (src_w != dst_w || src_h != dst_h);
11766}
11767
da20eabd
ML
11768int intel_plane_atomic_calc_changes(struct drm_crtc_state *crtc_state,
11769 struct drm_plane_state *plane_state)
11770{
ab1d3a0e 11771 struct intel_crtc_state *pipe_config = to_intel_crtc_state(crtc_state);
da20eabd
ML
11772 struct drm_crtc *crtc = crtc_state->crtc;
11773 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11774 struct drm_plane *plane = plane_state->plane;
11775 struct drm_device *dev = crtc->dev;
ed4a6a7c 11776 struct drm_i915_private *dev_priv = to_i915(dev);
da20eabd
ML
11777 struct intel_plane_state *old_plane_state =
11778 to_intel_plane_state(plane->state);
11779 int idx = intel_crtc->base.base.id, ret;
da20eabd
ML
11780 bool mode_changed = needs_modeset(crtc_state);
11781 bool was_crtc_enabled = crtc->state->active;
11782 bool is_crtc_enabled = crtc_state->active;
da20eabd
ML
11783 bool turn_off, turn_on, visible, was_visible;
11784 struct drm_framebuffer *fb = plane_state->fb;
11785
11786 if (crtc_state && INTEL_INFO(dev)->gen >= 9 &&
11787 plane->type != DRM_PLANE_TYPE_CURSOR) {
11788 ret = skl_update_scaler_plane(
11789 to_intel_crtc_state(crtc_state),
11790 to_intel_plane_state(plane_state));
11791 if (ret)
11792 return ret;
11793 }
11794
da20eabd
ML
11795 was_visible = old_plane_state->visible;
11796 visible = to_intel_plane_state(plane_state)->visible;
11797
11798 if (!was_crtc_enabled && WARN_ON(was_visible))
11799 was_visible = false;
11800
35c08f43
ML
11801 /*
11802 * Visibility is calculated as if the crtc was on, but
11803 * after scaler setup everything depends on it being off
11804 * when the crtc isn't active.
11805 */
11806 if (!is_crtc_enabled)
11807 to_intel_plane_state(plane_state)->visible = visible = false;
da20eabd
ML
11808
11809 if (!was_visible && !visible)
11810 return 0;
11811
e8861675
ML
11812 if (fb != old_plane_state->base.fb)
11813 pipe_config->fb_changed = true;
11814
da20eabd
ML
11815 turn_off = was_visible && (!visible || mode_changed);
11816 turn_on = visible && (!was_visible || mode_changed);
11817
11818 DRM_DEBUG_ATOMIC("[CRTC:%i] has [PLANE:%i] with fb %i\n", idx,
11819 plane->base.id, fb ? fb->base.id : -1);
11820
11821 DRM_DEBUG_ATOMIC("[PLANE:%i] visible %i -> %i, off %i, on %i, ms %i\n",
11822 plane->base.id, was_visible, visible,
11823 turn_off, turn_on, mode_changed);
11824
caed361d
VS
11825 if (turn_on) {
11826 pipe_config->update_wm_pre = true;
11827
11828 /* must disable cxsr around plane enable/disable */
11829 if (plane->type != DRM_PLANE_TYPE_CURSOR)
11830 pipe_config->disable_cxsr = true;
11831 } else if (turn_off) {
11832 pipe_config->update_wm_post = true;
92826fcd 11833
852eb00d 11834 /* must disable cxsr around plane enable/disable */
e8861675 11835 if (plane->type != DRM_PLANE_TYPE_CURSOR)
ab1d3a0e 11836 pipe_config->disable_cxsr = true;
852eb00d 11837 } else if (intel_wm_need_update(plane, plane_state)) {
caed361d
VS
11838 /* FIXME bollocks */
11839 pipe_config->update_wm_pre = true;
11840 pipe_config->update_wm_post = true;
852eb00d 11841 }
da20eabd 11842
ed4a6a7c 11843 /* Pre-gen9 platforms need two-step watermark updates */
caed361d
VS
11844 if ((pipe_config->update_wm_pre || pipe_config->update_wm_post) &&
11845 INTEL_INFO(dev)->gen < 9 && dev_priv->display.optimize_watermarks)
ed4a6a7c
MR
11846 to_intel_crtc_state(crtc_state)->wm.need_postvbl_update = true;
11847
8be6ca85 11848 if (visible || was_visible)
cd202f69 11849 pipe_config->fb_bits |= to_intel_plane(plane)->frontbuffer_bit;
a9ff8714 11850
31ae71fc
ML
11851 /*
11852 * WaCxSRDisabledForSpriteScaling:ivb
11853 *
11854 * cstate->update_wm was already set above, so this flag will
11855 * take effect when we commit and program watermarks.
11856 */
11857 if (plane->type == DRM_PLANE_TYPE_OVERLAY && IS_IVYBRIDGE(dev) &&
11858 needs_scaling(to_intel_plane_state(plane_state)) &&
11859 !needs_scaling(old_plane_state))
11860 pipe_config->disable_lp_wm = true;
d21fbe87 11861
da20eabd
ML
11862 return 0;
11863}
11864
6d3a1ce7
ML
11865static bool encoders_cloneable(const struct intel_encoder *a,
11866 const struct intel_encoder *b)
11867{
11868 /* masks could be asymmetric, so check both ways */
11869 return a == b || (a->cloneable & (1 << b->type) &&
11870 b->cloneable & (1 << a->type));
11871}
11872
11873static bool check_single_encoder_cloning(struct drm_atomic_state *state,
11874 struct intel_crtc *crtc,
11875 struct intel_encoder *encoder)
11876{
11877 struct intel_encoder *source_encoder;
11878 struct drm_connector *connector;
11879 struct drm_connector_state *connector_state;
11880 int i;
11881
11882 for_each_connector_in_state(state, connector, connector_state, i) {
11883 if (connector_state->crtc != &crtc->base)
11884 continue;
11885
11886 source_encoder =
11887 to_intel_encoder(connector_state->best_encoder);
11888 if (!encoders_cloneable(encoder, source_encoder))
11889 return false;
11890 }
11891
11892 return true;
11893}
11894
11895static bool check_encoder_cloning(struct drm_atomic_state *state,
11896 struct intel_crtc *crtc)
11897{
11898 struct intel_encoder *encoder;
11899 struct drm_connector *connector;
11900 struct drm_connector_state *connector_state;
11901 int i;
11902
11903 for_each_connector_in_state(state, connector, connector_state, i) {
11904 if (connector_state->crtc != &crtc->base)
11905 continue;
11906
11907 encoder = to_intel_encoder(connector_state->best_encoder);
11908 if (!check_single_encoder_cloning(state, crtc, encoder))
11909 return false;
11910 }
11911
11912 return true;
11913}
11914
11915static int intel_crtc_atomic_check(struct drm_crtc *crtc,
11916 struct drm_crtc_state *crtc_state)
11917{
cf5a15be 11918 struct drm_device *dev = crtc->dev;
ad421372 11919 struct drm_i915_private *dev_priv = dev->dev_private;
6d3a1ce7 11920 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
cf5a15be
ML
11921 struct intel_crtc_state *pipe_config =
11922 to_intel_crtc_state(crtc_state);
6d3a1ce7 11923 struct drm_atomic_state *state = crtc_state->state;
4d20cd86 11924 int ret;
6d3a1ce7
ML
11925 bool mode_changed = needs_modeset(crtc_state);
11926
11927 if (mode_changed && !check_encoder_cloning(state, intel_crtc)) {
11928 DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
11929 return -EINVAL;
11930 }
11931
852eb00d 11932 if (mode_changed && !crtc_state->active)
caed361d 11933 pipe_config->update_wm_post = true;
eddfcbcd 11934
ad421372
ML
11935 if (mode_changed && crtc_state->enable &&
11936 dev_priv->display.crtc_compute_clock &&
8106ddbd 11937 !WARN_ON(pipe_config->shared_dpll)) {
ad421372
ML
11938 ret = dev_priv->display.crtc_compute_clock(intel_crtc,
11939 pipe_config);
11940 if (ret)
11941 return ret;
11942 }
11943
82cf435b
LL
11944 if (crtc_state->color_mgmt_changed) {
11945 ret = intel_color_check(crtc, crtc_state);
11946 if (ret)
11947 return ret;
11948 }
11949
e435d6e5 11950 ret = 0;
86c8bbbe 11951 if (dev_priv->display.compute_pipe_wm) {
e3bddded 11952 ret = dev_priv->display.compute_pipe_wm(pipe_config);
ed4a6a7c
MR
11953 if (ret) {
11954 DRM_DEBUG_KMS("Target pipe watermarks are invalid\n");
11955 return ret;
11956 }
11957 }
11958
11959 if (dev_priv->display.compute_intermediate_wm &&
11960 !to_intel_atomic_state(state)->skip_intermediate_wm) {
11961 if (WARN_ON(!dev_priv->display.compute_pipe_wm))
11962 return 0;
11963
11964 /*
11965 * Calculate 'intermediate' watermarks that satisfy both the
11966 * old state and the new state. We can program these
11967 * immediately.
11968 */
11969 ret = dev_priv->display.compute_intermediate_wm(crtc->dev,
11970 intel_crtc,
11971 pipe_config);
11972 if (ret) {
11973 DRM_DEBUG_KMS("No valid intermediate pipe watermarks are possible\n");
86c8bbbe 11974 return ret;
ed4a6a7c 11975 }
86c8bbbe
MR
11976 }
11977
e435d6e5
ML
11978 if (INTEL_INFO(dev)->gen >= 9) {
11979 if (mode_changed)
11980 ret = skl_update_scaler_crtc(pipe_config);
11981
11982 if (!ret)
11983 ret = intel_atomic_setup_scalers(dev, intel_crtc,
11984 pipe_config);
11985 }
11986
11987 return ret;
6d3a1ce7
ML
11988}
11989
65b38e0d 11990static const struct drm_crtc_helper_funcs intel_helper_funcs = {
f6e5b160 11991 .mode_set_base_atomic = intel_pipe_set_base_atomic,
ea2c67bb
MR
11992 .atomic_begin = intel_begin_crtc_commit,
11993 .atomic_flush = intel_finish_crtc_commit,
6d3a1ce7 11994 .atomic_check = intel_crtc_atomic_check,
f6e5b160
CW
11995};
11996
d29b2f9d
ACO
11997static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
11998{
11999 struct intel_connector *connector;
12000
12001 for_each_intel_connector(dev, connector) {
12002 if (connector->base.encoder) {
12003 connector->base.state->best_encoder =
12004 connector->base.encoder;
12005 connector->base.state->crtc =
12006 connector->base.encoder->crtc;
12007 } else {
12008 connector->base.state->best_encoder = NULL;
12009 connector->base.state->crtc = NULL;
12010 }
12011 }
12012}
12013
050f7aeb 12014static void
eba905b2 12015connected_sink_compute_bpp(struct intel_connector *connector,
5cec258b 12016 struct intel_crtc_state *pipe_config)
050f7aeb
DV
12017{
12018 int bpp = pipe_config->pipe_bpp;
12019
12020 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
12021 connector->base.base.id,
c23cc417 12022 connector->base.name);
050f7aeb
DV
12023
12024 /* Don't use an invalid EDID bpc value */
12025 if (connector->base.display_info.bpc &&
12026 connector->base.display_info.bpc * 3 < bpp) {
12027 DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
12028 bpp, connector->base.display_info.bpc*3);
12029 pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
12030 }
12031
013dd9e0
JN
12032 /* Clamp bpp to default limit on screens without EDID 1.4 */
12033 if (connector->base.display_info.bpc == 0) {
12034 int type = connector->base.connector_type;
12035 int clamp_bpp = 24;
12036
12037 /* Fall back to 18 bpp when DP sink capability is unknown. */
12038 if (type == DRM_MODE_CONNECTOR_DisplayPort ||
12039 type == DRM_MODE_CONNECTOR_eDP)
12040 clamp_bpp = 18;
12041
12042 if (bpp > clamp_bpp) {
12043 DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of %d\n",
12044 bpp, clamp_bpp);
12045 pipe_config->pipe_bpp = clamp_bpp;
12046 }
050f7aeb
DV
12047 }
12048}
12049
4e53c2e0 12050static int
050f7aeb 12051compute_baseline_pipe_bpp(struct intel_crtc *crtc,
5cec258b 12052 struct intel_crtc_state *pipe_config)
4e53c2e0 12053{
050f7aeb 12054 struct drm_device *dev = crtc->base.dev;
1486017f 12055 struct drm_atomic_state *state;
da3ced29
ACO
12056 struct drm_connector *connector;
12057 struct drm_connector_state *connector_state;
1486017f 12058 int bpp, i;
4e53c2e0 12059
666a4537 12060 if ((IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)))
4e53c2e0 12061 bpp = 10*3;
d328c9d7
DV
12062 else if (INTEL_INFO(dev)->gen >= 5)
12063 bpp = 12*3;
12064 else
12065 bpp = 8*3;
12066
4e53c2e0 12067
4e53c2e0
DV
12068 pipe_config->pipe_bpp = bpp;
12069
1486017f
ACO
12070 state = pipe_config->base.state;
12071
4e53c2e0 12072 /* Clamp display bpp to EDID value */
da3ced29
ACO
12073 for_each_connector_in_state(state, connector, connector_state, i) {
12074 if (connector_state->crtc != &crtc->base)
4e53c2e0
DV
12075 continue;
12076
da3ced29
ACO
12077 connected_sink_compute_bpp(to_intel_connector(connector),
12078 pipe_config);
4e53c2e0
DV
12079 }
12080
12081 return bpp;
12082}
12083
644db711
DV
12084static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
12085{
12086 DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
12087 "type: 0x%x flags: 0x%x\n",
1342830c 12088 mode->crtc_clock,
644db711
DV
12089 mode->crtc_hdisplay, mode->crtc_hsync_start,
12090 mode->crtc_hsync_end, mode->crtc_htotal,
12091 mode->crtc_vdisplay, mode->crtc_vsync_start,
12092 mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
12093}
12094
c0b03411 12095static void intel_dump_pipe_config(struct intel_crtc *crtc,
5cec258b 12096 struct intel_crtc_state *pipe_config,
c0b03411
DV
12097 const char *context)
12098{
6a60cd87
CK
12099 struct drm_device *dev = crtc->base.dev;
12100 struct drm_plane *plane;
12101 struct intel_plane *intel_plane;
12102 struct intel_plane_state *state;
12103 struct drm_framebuffer *fb;
12104
12105 DRM_DEBUG_KMS("[CRTC:%d]%s config %p for pipe %c\n", crtc->base.base.id,
12106 context, pipe_config, pipe_name(crtc->pipe));
c0b03411 12107
da205630 12108 DRM_DEBUG_KMS("cpu_transcoder: %s\n", transcoder_name(pipe_config->cpu_transcoder));
c0b03411
DV
12109 DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
12110 pipe_config->pipe_bpp, pipe_config->dither);
12111 DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
12112 pipe_config->has_pch_encoder,
12113 pipe_config->fdi_lanes,
12114 pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
12115 pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
12116 pipe_config->fdi_m_n.tu);
90a6b7b0 12117 DRM_DEBUG_KMS("dp: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
eb14cb74 12118 pipe_config->has_dp_encoder,
90a6b7b0 12119 pipe_config->lane_count,
eb14cb74
VS
12120 pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
12121 pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
12122 pipe_config->dp_m_n.tu);
b95af8be 12123
90a6b7b0 12124 DRM_DEBUG_KMS("dp: %i, lanes: %i, gmch_m2: %u, gmch_n2: %u, link_m2: %u, link_n2: %u, tu2: %u\n",
b95af8be 12125 pipe_config->has_dp_encoder,
90a6b7b0 12126 pipe_config->lane_count,
b95af8be
VK
12127 pipe_config->dp_m2_n2.gmch_m,
12128 pipe_config->dp_m2_n2.gmch_n,
12129 pipe_config->dp_m2_n2.link_m,
12130 pipe_config->dp_m2_n2.link_n,
12131 pipe_config->dp_m2_n2.tu);
12132
55072d19
DV
12133 DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
12134 pipe_config->has_audio,
12135 pipe_config->has_infoframe);
12136
c0b03411 12137 DRM_DEBUG_KMS("requested mode:\n");
2d112de7 12138 drm_mode_debug_printmodeline(&pipe_config->base.mode);
c0b03411 12139 DRM_DEBUG_KMS("adjusted mode:\n");
2d112de7
ACO
12140 drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
12141 intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
d71b8d4a 12142 DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
37327abd
VS
12143 DRM_DEBUG_KMS("pipe src size: %dx%d\n",
12144 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
0ec463d3
TU
12145 DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
12146 crtc->num_scalers,
12147 pipe_config->scaler_state.scaler_users,
12148 pipe_config->scaler_state.scaler_id);
c0b03411
DV
12149 DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
12150 pipe_config->gmch_pfit.control,
12151 pipe_config->gmch_pfit.pgm_ratios,
12152 pipe_config->gmch_pfit.lvds_border_bits);
fd4daa9c 12153 DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
c0b03411 12154 pipe_config->pch_pfit.pos,
fd4daa9c
CW
12155 pipe_config->pch_pfit.size,
12156 pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
42db64ef 12157 DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
cf532bb2 12158 DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
6a60cd87 12159
415ff0f6 12160 if (IS_BROXTON(dev)) {
05712c15 12161 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: ebb0: 0x%x, ebb4: 0x%x,"
415ff0f6 12162 "pll0: 0x%x, pll1: 0x%x, pll2: 0x%x, pll3: 0x%x, "
c8453338 12163 "pll6: 0x%x, pll8: 0x%x, pll9: 0x%x, pll10: 0x%x, pcsdw12: 0x%x\n",
415ff0f6
TU
12164 pipe_config->ddi_pll_sel,
12165 pipe_config->dpll_hw_state.ebb0,
05712c15 12166 pipe_config->dpll_hw_state.ebb4,
415ff0f6
TU
12167 pipe_config->dpll_hw_state.pll0,
12168 pipe_config->dpll_hw_state.pll1,
12169 pipe_config->dpll_hw_state.pll2,
12170 pipe_config->dpll_hw_state.pll3,
12171 pipe_config->dpll_hw_state.pll6,
12172 pipe_config->dpll_hw_state.pll8,
05712c15 12173 pipe_config->dpll_hw_state.pll9,
c8453338 12174 pipe_config->dpll_hw_state.pll10,
415ff0f6 12175 pipe_config->dpll_hw_state.pcsdw12);
ef11bdb3 12176 } else if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) {
415ff0f6
TU
12177 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: "
12178 "ctrl1: 0x%x, cfgcr1: 0x%x, cfgcr2: 0x%x\n",
12179 pipe_config->ddi_pll_sel,
12180 pipe_config->dpll_hw_state.ctrl1,
12181 pipe_config->dpll_hw_state.cfgcr1,
12182 pipe_config->dpll_hw_state.cfgcr2);
12183 } else if (HAS_DDI(dev)) {
1260f07e 12184 DRM_DEBUG_KMS("ddi_pll_sel: 0x%x; dpll_hw_state: wrpll: 0x%x spll: 0x%x\n",
415ff0f6 12185 pipe_config->ddi_pll_sel,
00490c22
ML
12186 pipe_config->dpll_hw_state.wrpll,
12187 pipe_config->dpll_hw_state.spll);
415ff0f6
TU
12188 } else {
12189 DRM_DEBUG_KMS("dpll_hw_state: dpll: 0x%x, dpll_md: 0x%x, "
12190 "fp0: 0x%x, fp1: 0x%x\n",
12191 pipe_config->dpll_hw_state.dpll,
12192 pipe_config->dpll_hw_state.dpll_md,
12193 pipe_config->dpll_hw_state.fp0,
12194 pipe_config->dpll_hw_state.fp1);
12195 }
12196
6a60cd87
CK
12197 DRM_DEBUG_KMS("planes on this crtc\n");
12198 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
12199 intel_plane = to_intel_plane(plane);
12200 if (intel_plane->pipe != crtc->pipe)
12201 continue;
12202
12203 state = to_intel_plane_state(plane->state);
12204 fb = state->base.fb;
12205 if (!fb) {
12206 DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d "
12207 "disabled, scaler_id = %d\n",
12208 plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
12209 plane->base.id, intel_plane->pipe,
12210 (crtc->base.primary == plane) ? 0 : intel_plane->plane + 1,
12211 drm_plane_index(plane), state->scaler_id);
12212 continue;
12213 }
12214
12215 DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d enabled",
12216 plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
12217 plane->base.id, intel_plane->pipe,
12218 crtc->base.primary == plane ? 0 : intel_plane->plane + 1,
12219 drm_plane_index(plane));
12220 DRM_DEBUG_KMS("\tFB:%d, fb = %ux%u format = 0x%x",
12221 fb->base.id, fb->width, fb->height, fb->pixel_format);
12222 DRM_DEBUG_KMS("\tscaler:%d src (%u, %u) %ux%u dst (%u, %u) %ux%u\n",
12223 state->scaler_id,
12224 state->src.x1 >> 16, state->src.y1 >> 16,
12225 drm_rect_width(&state->src) >> 16,
12226 drm_rect_height(&state->src) >> 16,
12227 state->dst.x1, state->dst.y1,
12228 drm_rect_width(&state->dst), drm_rect_height(&state->dst));
12229 }
c0b03411
DV
12230}
12231
5448a00d 12232static bool check_digital_port_conflicts(struct drm_atomic_state *state)
00f0b378 12233{
5448a00d 12234 struct drm_device *dev = state->dev;
da3ced29 12235 struct drm_connector *connector;
00f0b378
VS
12236 unsigned int used_ports = 0;
12237
12238 /*
12239 * Walk the connector list instead of the encoder
12240 * list to detect the problem on ddi platforms
12241 * where there's just one encoder per digital port.
12242 */
0bff4858
VS
12243 drm_for_each_connector(connector, dev) {
12244 struct drm_connector_state *connector_state;
12245 struct intel_encoder *encoder;
12246
12247 connector_state = drm_atomic_get_existing_connector_state(state, connector);
12248 if (!connector_state)
12249 connector_state = connector->state;
12250
5448a00d 12251 if (!connector_state->best_encoder)
00f0b378
VS
12252 continue;
12253
5448a00d
ACO
12254 encoder = to_intel_encoder(connector_state->best_encoder);
12255
12256 WARN_ON(!connector_state->crtc);
00f0b378
VS
12257
12258 switch (encoder->type) {
12259 unsigned int port_mask;
12260 case INTEL_OUTPUT_UNKNOWN:
12261 if (WARN_ON(!HAS_DDI(dev)))
12262 break;
12263 case INTEL_OUTPUT_DISPLAYPORT:
12264 case INTEL_OUTPUT_HDMI:
12265 case INTEL_OUTPUT_EDP:
12266 port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
12267
12268 /* the same port mustn't appear more than once */
12269 if (used_ports & port_mask)
12270 return false;
12271
12272 used_ports |= port_mask;
12273 default:
12274 break;
12275 }
12276 }
12277
12278 return true;
12279}
12280
83a57153
ACO
12281static void
12282clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
12283{
12284 struct drm_crtc_state tmp_state;
663a3640 12285 struct intel_crtc_scaler_state scaler_state;
4978cc93 12286 struct intel_dpll_hw_state dpll_hw_state;
8106ddbd 12287 struct intel_shared_dpll *shared_dpll;
8504c74c 12288 uint32_t ddi_pll_sel;
c4e2d043 12289 bool force_thru;
83a57153 12290
7546a384
ACO
12291 /* FIXME: before the switch to atomic started, a new pipe_config was
12292 * kzalloc'd. Code that depends on any field being zero should be
12293 * fixed, so that the crtc_state can be safely duplicated. For now,
12294 * only fields that are know to not cause problems are preserved. */
12295
83a57153 12296 tmp_state = crtc_state->base;
663a3640 12297 scaler_state = crtc_state->scaler_state;
4978cc93
ACO
12298 shared_dpll = crtc_state->shared_dpll;
12299 dpll_hw_state = crtc_state->dpll_hw_state;
8504c74c 12300 ddi_pll_sel = crtc_state->ddi_pll_sel;
c4e2d043 12301 force_thru = crtc_state->pch_pfit.force_thru;
4978cc93 12302
83a57153 12303 memset(crtc_state, 0, sizeof *crtc_state);
4978cc93 12304
83a57153 12305 crtc_state->base = tmp_state;
663a3640 12306 crtc_state->scaler_state = scaler_state;
4978cc93
ACO
12307 crtc_state->shared_dpll = shared_dpll;
12308 crtc_state->dpll_hw_state = dpll_hw_state;
8504c74c 12309 crtc_state->ddi_pll_sel = ddi_pll_sel;
c4e2d043 12310 crtc_state->pch_pfit.force_thru = force_thru;
83a57153
ACO
12311}
12312
548ee15b 12313static int
b8cecdf5 12314intel_modeset_pipe_config(struct drm_crtc *crtc,
b359283a 12315 struct intel_crtc_state *pipe_config)
ee7b9f93 12316{
b359283a 12317 struct drm_atomic_state *state = pipe_config->base.state;
7758a113 12318 struct intel_encoder *encoder;
da3ced29 12319 struct drm_connector *connector;
0b901879 12320 struct drm_connector_state *connector_state;
d328c9d7 12321 int base_bpp, ret = -EINVAL;
0b901879 12322 int i;
e29c22c0 12323 bool retry = true;
ee7b9f93 12324
83a57153 12325 clear_intel_crtc_state(pipe_config);
7758a113 12326
e143a21c
DV
12327 pipe_config->cpu_transcoder =
12328 (enum transcoder) to_intel_crtc(crtc)->pipe;
b8cecdf5 12329
2960bc9c
ID
12330 /*
12331 * Sanitize sync polarity flags based on requested ones. If neither
12332 * positive or negative polarity is requested, treat this as meaning
12333 * negative polarity.
12334 */
2d112de7 12335 if (!(pipe_config->base.adjusted_mode.flags &
2960bc9c 12336 (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
2d112de7 12337 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
2960bc9c 12338
2d112de7 12339 if (!(pipe_config->base.adjusted_mode.flags &
2960bc9c 12340 (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
2d112de7 12341 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
2960bc9c 12342
d328c9d7
DV
12343 base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
12344 pipe_config);
12345 if (base_bpp < 0)
4e53c2e0
DV
12346 goto fail;
12347
e41a56be
VS
12348 /*
12349 * Determine the real pipe dimensions. Note that stereo modes can
12350 * increase the actual pipe size due to the frame doubling and
12351 * insertion of additional space for blanks between the frame. This
12352 * is stored in the crtc timings. We use the requested mode to do this
12353 * computation to clearly distinguish it from the adjusted mode, which
12354 * can be changed by the connectors in the below retry loop.
12355 */
2d112de7 12356 drm_crtc_get_hv_timing(&pipe_config->base.mode,
ecb7e16b
GP
12357 &pipe_config->pipe_src_w,
12358 &pipe_config->pipe_src_h);
e41a56be 12359
e29c22c0 12360encoder_retry:
ef1b460d 12361 /* Ensure the port clock defaults are reset when retrying. */
ff9a6750 12362 pipe_config->port_clock = 0;
ef1b460d 12363 pipe_config->pixel_multiplier = 1;
ff9a6750 12364
135c81b8 12365 /* Fill in default crtc timings, allow encoders to overwrite them. */
2d112de7
ACO
12366 drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
12367 CRTC_STEREO_DOUBLE);
135c81b8 12368
7758a113
DV
12369 /* Pass our mode to the connectors and the CRTC to give them a chance to
12370 * adjust it according to limitations or connector properties, and also
12371 * a chance to reject the mode entirely.
47f1c6c9 12372 */
da3ced29 12373 for_each_connector_in_state(state, connector, connector_state, i) {
0b901879 12374 if (connector_state->crtc != crtc)
7758a113 12375 continue;
7ae89233 12376
0b901879
ACO
12377 encoder = to_intel_encoder(connector_state->best_encoder);
12378
efea6e8e
DV
12379 if (!(encoder->compute_config(encoder, pipe_config))) {
12380 DRM_DEBUG_KMS("Encoder config failure\n");
7758a113
DV
12381 goto fail;
12382 }
ee7b9f93 12383 }
47f1c6c9 12384
ff9a6750
DV
12385 /* Set default port clock if not overwritten by the encoder. Needs to be
12386 * done afterwards in case the encoder adjusts the mode. */
12387 if (!pipe_config->port_clock)
2d112de7 12388 pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
241bfc38 12389 * pipe_config->pixel_multiplier;
ff9a6750 12390
a43f6e0f 12391 ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
e29c22c0 12392 if (ret < 0) {
7758a113
DV
12393 DRM_DEBUG_KMS("CRTC fixup failed\n");
12394 goto fail;
ee7b9f93 12395 }
e29c22c0
DV
12396
12397 if (ret == RETRY) {
12398 if (WARN(!retry, "loop in pipe configuration computation\n")) {
12399 ret = -EINVAL;
12400 goto fail;
12401 }
12402
12403 DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
12404 retry = false;
12405 goto encoder_retry;
12406 }
12407
e8fa4270
DV
12408 /* Dithering seems to not pass-through bits correctly when it should, so
12409 * only enable it on 6bpc panels. */
12410 pipe_config->dither = pipe_config->pipe_bpp == 6*3;
62f0ace5 12411 DRM_DEBUG_KMS("hw max bpp: %i, pipe bpp: %i, dithering: %i\n",
d328c9d7 12412 base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
4e53c2e0 12413
7758a113 12414fail:
548ee15b 12415 return ret;
ee7b9f93 12416}
47f1c6c9 12417
ea9d758d 12418static void
4740b0f2 12419intel_modeset_update_crtc_state(struct drm_atomic_state *state)
ea9d758d 12420{
0a9ab303
ACO
12421 struct drm_crtc *crtc;
12422 struct drm_crtc_state *crtc_state;
8a75d157 12423 int i;
ea9d758d 12424
7668851f 12425 /* Double check state. */
8a75d157 12426 for_each_crtc_in_state(state, crtc, crtc_state, i) {
3cb480bc 12427 to_intel_crtc(crtc)->config = to_intel_crtc_state(crtc->state);
fc467a22
ML
12428
12429 /* Update hwmode for vblank functions */
12430 if (crtc->state->active)
12431 crtc->hwmode = crtc->state->adjusted_mode;
12432 else
12433 crtc->hwmode.crtc_clock = 0;
61067a5e
ML
12434
12435 /*
12436 * Update legacy state to satisfy fbc code. This can
12437 * be removed when fbc uses the atomic state.
12438 */
12439 if (drm_atomic_get_existing_plane_state(state, crtc->primary)) {
12440 struct drm_plane_state *plane_state = crtc->primary->state;
12441
12442 crtc->primary->fb = plane_state->fb;
12443 crtc->x = plane_state->src_x >> 16;
12444 crtc->y = plane_state->src_y >> 16;
12445 }
ea9d758d 12446 }
ea9d758d
DV
12447}
12448
3bd26263 12449static bool intel_fuzzy_clock_check(int clock1, int clock2)
f1f644dc 12450{
3bd26263 12451 int diff;
f1f644dc
JB
12452
12453 if (clock1 == clock2)
12454 return true;
12455
12456 if (!clock1 || !clock2)
12457 return false;
12458
12459 diff = abs(clock1 - clock2);
12460
12461 if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
12462 return true;
12463
12464 return false;
12465}
12466
25c5b266
DV
12467#define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
12468 list_for_each_entry((intel_crtc), \
12469 &(dev)->mode_config.crtc_list, \
12470 base.head) \
95150bdf 12471 for_each_if (mask & (1 <<(intel_crtc)->pipe))
25c5b266 12472
cfb23ed6
ML
12473static bool
12474intel_compare_m_n(unsigned int m, unsigned int n,
12475 unsigned int m2, unsigned int n2,
12476 bool exact)
12477{
12478 if (m == m2 && n == n2)
12479 return true;
12480
12481 if (exact || !m || !n || !m2 || !n2)
12482 return false;
12483
12484 BUILD_BUG_ON(DATA_LINK_M_N_MASK > INT_MAX);
12485
31d10b57
ML
12486 if (n > n2) {
12487 while (n > n2) {
cfb23ed6
ML
12488 m2 <<= 1;
12489 n2 <<= 1;
12490 }
31d10b57
ML
12491 } else if (n < n2) {
12492 while (n < n2) {
cfb23ed6
ML
12493 m <<= 1;
12494 n <<= 1;
12495 }
12496 }
12497
31d10b57
ML
12498 if (n != n2)
12499 return false;
12500
12501 return intel_fuzzy_clock_check(m, m2);
cfb23ed6
ML
12502}
12503
12504static bool
12505intel_compare_link_m_n(const struct intel_link_m_n *m_n,
12506 struct intel_link_m_n *m2_n2,
12507 bool adjust)
12508{
12509 if (m_n->tu == m2_n2->tu &&
12510 intel_compare_m_n(m_n->gmch_m, m_n->gmch_n,
12511 m2_n2->gmch_m, m2_n2->gmch_n, !adjust) &&
12512 intel_compare_m_n(m_n->link_m, m_n->link_n,
12513 m2_n2->link_m, m2_n2->link_n, !adjust)) {
12514 if (adjust)
12515 *m2_n2 = *m_n;
12516
12517 return true;
12518 }
12519
12520 return false;
12521}
12522
0e8ffe1b 12523static bool
2fa2fe9a 12524intel_pipe_config_compare(struct drm_device *dev,
5cec258b 12525 struct intel_crtc_state *current_config,
cfb23ed6
ML
12526 struct intel_crtc_state *pipe_config,
12527 bool adjust)
0e8ffe1b 12528{
cfb23ed6
ML
12529 bool ret = true;
12530
12531#define INTEL_ERR_OR_DBG_KMS(fmt, ...) \
12532 do { \
12533 if (!adjust) \
12534 DRM_ERROR(fmt, ##__VA_ARGS__); \
12535 else \
12536 DRM_DEBUG_KMS(fmt, ##__VA_ARGS__); \
12537 } while (0)
12538
66e985c0
DV
12539#define PIPE_CONF_CHECK_X(name) \
12540 if (current_config->name != pipe_config->name) { \
cfb23ed6 12541 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
66e985c0
DV
12542 "(expected 0x%08x, found 0x%08x)\n", \
12543 current_config->name, \
12544 pipe_config->name); \
cfb23ed6 12545 ret = false; \
66e985c0
DV
12546 }
12547
08a24034
DV
12548#define PIPE_CONF_CHECK_I(name) \
12549 if (current_config->name != pipe_config->name) { \
cfb23ed6 12550 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
08a24034
DV
12551 "(expected %i, found %i)\n", \
12552 current_config->name, \
12553 pipe_config->name); \
cfb23ed6
ML
12554 ret = false; \
12555 }
12556
8106ddbd
ACO
12557#define PIPE_CONF_CHECK_P(name) \
12558 if (current_config->name != pipe_config->name) { \
12559 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
12560 "(expected %p, found %p)\n", \
12561 current_config->name, \
12562 pipe_config->name); \
12563 ret = false; \
12564 }
12565
cfb23ed6
ML
12566#define PIPE_CONF_CHECK_M_N(name) \
12567 if (!intel_compare_link_m_n(&current_config->name, \
12568 &pipe_config->name,\
12569 adjust)) { \
12570 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
12571 "(expected tu %i gmch %i/%i link %i/%i, " \
12572 "found tu %i, gmch %i/%i link %i/%i)\n", \
12573 current_config->name.tu, \
12574 current_config->name.gmch_m, \
12575 current_config->name.gmch_n, \
12576 current_config->name.link_m, \
12577 current_config->name.link_n, \
12578 pipe_config->name.tu, \
12579 pipe_config->name.gmch_m, \
12580 pipe_config->name.gmch_n, \
12581 pipe_config->name.link_m, \
12582 pipe_config->name.link_n); \
12583 ret = false; \
12584 }
12585
55c561a7
DV
12586/* This is required for BDW+ where there is only one set of registers for
12587 * switching between high and low RR.
12588 * This macro can be used whenever a comparison has to be made between one
12589 * hw state and multiple sw state variables.
12590 */
cfb23ed6
ML
12591#define PIPE_CONF_CHECK_M_N_ALT(name, alt_name) \
12592 if (!intel_compare_link_m_n(&current_config->name, \
12593 &pipe_config->name, adjust) && \
12594 !intel_compare_link_m_n(&current_config->alt_name, \
12595 &pipe_config->name, adjust)) { \
12596 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
12597 "(expected tu %i gmch %i/%i link %i/%i, " \
12598 "or tu %i gmch %i/%i link %i/%i, " \
12599 "found tu %i, gmch %i/%i link %i/%i)\n", \
12600 current_config->name.tu, \
12601 current_config->name.gmch_m, \
12602 current_config->name.gmch_n, \
12603 current_config->name.link_m, \
12604 current_config->name.link_n, \
12605 current_config->alt_name.tu, \
12606 current_config->alt_name.gmch_m, \
12607 current_config->alt_name.gmch_n, \
12608 current_config->alt_name.link_m, \
12609 current_config->alt_name.link_n, \
12610 pipe_config->name.tu, \
12611 pipe_config->name.gmch_m, \
12612 pipe_config->name.gmch_n, \
12613 pipe_config->name.link_m, \
12614 pipe_config->name.link_n); \
12615 ret = false; \
88adfff1
DV
12616 }
12617
1bd1bd80
DV
12618#define PIPE_CONF_CHECK_FLAGS(name, mask) \
12619 if ((current_config->name ^ pipe_config->name) & (mask)) { \
cfb23ed6 12620 INTEL_ERR_OR_DBG_KMS("mismatch in " #name "(" #mask ") " \
1bd1bd80
DV
12621 "(expected %i, found %i)\n", \
12622 current_config->name & (mask), \
12623 pipe_config->name & (mask)); \
cfb23ed6 12624 ret = false; \
1bd1bd80
DV
12625 }
12626
5e550656
VS
12627#define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
12628 if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
cfb23ed6 12629 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
5e550656
VS
12630 "(expected %i, found %i)\n", \
12631 current_config->name, \
12632 pipe_config->name); \
cfb23ed6 12633 ret = false; \
5e550656
VS
12634 }
12635
bb760063
DV
12636#define PIPE_CONF_QUIRK(quirk) \
12637 ((current_config->quirks | pipe_config->quirks) & (quirk))
12638
eccb140b
DV
12639 PIPE_CONF_CHECK_I(cpu_transcoder);
12640
08a24034
DV
12641 PIPE_CONF_CHECK_I(has_pch_encoder);
12642 PIPE_CONF_CHECK_I(fdi_lanes);
cfb23ed6 12643 PIPE_CONF_CHECK_M_N(fdi_m_n);
08a24034 12644
eb14cb74 12645 PIPE_CONF_CHECK_I(has_dp_encoder);
90a6b7b0 12646 PIPE_CONF_CHECK_I(lane_count);
b95af8be
VK
12647
12648 if (INTEL_INFO(dev)->gen < 8) {
cfb23ed6
ML
12649 PIPE_CONF_CHECK_M_N(dp_m_n);
12650
cfb23ed6
ML
12651 if (current_config->has_drrs)
12652 PIPE_CONF_CHECK_M_N(dp_m2_n2);
12653 } else
12654 PIPE_CONF_CHECK_M_N_ALT(dp_m_n, dp_m2_n2);
eb14cb74 12655
a65347ba
JN
12656 PIPE_CONF_CHECK_I(has_dsi_encoder);
12657
2d112de7
ACO
12658 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
12659 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
12660 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
12661 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
12662 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
12663 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
1bd1bd80 12664
2d112de7
ACO
12665 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
12666 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
12667 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
12668 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
12669 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
12670 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
1bd1bd80 12671
c93f54cf 12672 PIPE_CONF_CHECK_I(pixel_multiplier);
6897b4b5 12673 PIPE_CONF_CHECK_I(has_hdmi_sink);
b5a9fa09 12674 if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
666a4537 12675 IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev))
b5a9fa09 12676 PIPE_CONF_CHECK_I(limited_color_range);
e43823ec 12677 PIPE_CONF_CHECK_I(has_infoframe);
6c49f241 12678
9ed109a7
DV
12679 PIPE_CONF_CHECK_I(has_audio);
12680
2d112de7 12681 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
1bd1bd80
DV
12682 DRM_MODE_FLAG_INTERLACE);
12683
bb760063 12684 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
2d112de7 12685 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063 12686 DRM_MODE_FLAG_PHSYNC);
2d112de7 12687 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063 12688 DRM_MODE_FLAG_NHSYNC);
2d112de7 12689 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063 12690 DRM_MODE_FLAG_PVSYNC);
2d112de7 12691 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063
DV
12692 DRM_MODE_FLAG_NVSYNC);
12693 }
045ac3b5 12694
333b8ca8 12695 PIPE_CONF_CHECK_X(gmch_pfit.control);
e2ff2d4a
DV
12696 /* pfit ratios are autocomputed by the hw on gen4+ */
12697 if (INTEL_INFO(dev)->gen < 4)
7f7d8dd6 12698 PIPE_CONF_CHECK_X(gmch_pfit.pgm_ratios);
333b8ca8 12699 PIPE_CONF_CHECK_X(gmch_pfit.lvds_border_bits);
9953599b 12700
bfd16b2a
ML
12701 if (!adjust) {
12702 PIPE_CONF_CHECK_I(pipe_src_w);
12703 PIPE_CONF_CHECK_I(pipe_src_h);
12704
12705 PIPE_CONF_CHECK_I(pch_pfit.enabled);
12706 if (current_config->pch_pfit.enabled) {
12707 PIPE_CONF_CHECK_X(pch_pfit.pos);
12708 PIPE_CONF_CHECK_X(pch_pfit.size);
12709 }
2fa2fe9a 12710
7aefe2b5
ML
12711 PIPE_CONF_CHECK_I(scaler_state.scaler_id);
12712 }
a1b2278e 12713
e59150dc
JB
12714 /* BDW+ don't expose a synchronous way to read the state */
12715 if (IS_HASWELL(dev))
12716 PIPE_CONF_CHECK_I(ips_enabled);
42db64ef 12717
282740f7
VS
12718 PIPE_CONF_CHECK_I(double_wide);
12719
26804afd
DV
12720 PIPE_CONF_CHECK_X(ddi_pll_sel);
12721
8106ddbd 12722 PIPE_CONF_CHECK_P(shared_dpll);
66e985c0 12723 PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
8bcc2795 12724 PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
66e985c0
DV
12725 PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
12726 PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
d452c5b6 12727 PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
00490c22 12728 PIPE_CONF_CHECK_X(dpll_hw_state.spll);
3f4cd19f
DL
12729 PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
12730 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
12731 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
c0d43d62 12732
42571aef
VS
12733 if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
12734 PIPE_CONF_CHECK_I(pipe_bpp);
12735
2d112de7 12736 PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
a9a7e98a 12737 PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
5e550656 12738
66e985c0 12739#undef PIPE_CONF_CHECK_X
08a24034 12740#undef PIPE_CONF_CHECK_I
8106ddbd 12741#undef PIPE_CONF_CHECK_P
1bd1bd80 12742#undef PIPE_CONF_CHECK_FLAGS
5e550656 12743#undef PIPE_CONF_CHECK_CLOCK_FUZZY
bb760063 12744#undef PIPE_CONF_QUIRK
cfb23ed6 12745#undef INTEL_ERR_OR_DBG_KMS
88adfff1 12746
cfb23ed6 12747 return ret;
0e8ffe1b
DV
12748}
12749
e3b247da
VS
12750static void intel_pipe_config_sanity_check(struct drm_i915_private *dev_priv,
12751 const struct intel_crtc_state *pipe_config)
12752{
12753 if (pipe_config->has_pch_encoder) {
21a727b3 12754 int fdi_dotclock = intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
e3b247da
VS
12755 &pipe_config->fdi_m_n);
12756 int dotclock = pipe_config->base.adjusted_mode.crtc_clock;
12757
12758 /*
12759 * FDI already provided one idea for the dotclock.
12760 * Yell if the encoder disagrees.
12761 */
12762 WARN(!intel_fuzzy_clock_check(fdi_dotclock, dotclock),
12763 "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
12764 fdi_dotclock, dotclock);
12765 }
12766}
12767
c0ead703
ML
12768static void verify_wm_state(struct drm_crtc *crtc,
12769 struct drm_crtc_state *new_state)
08db6652 12770{
e7c84544 12771 struct drm_device *dev = crtc->dev;
08db6652
DL
12772 struct drm_i915_private *dev_priv = dev->dev_private;
12773 struct skl_ddb_allocation hw_ddb, *sw_ddb;
e7c84544
ML
12774 struct skl_ddb_entry *hw_entry, *sw_entry;
12775 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
12776 const enum pipe pipe = intel_crtc->pipe;
08db6652
DL
12777 int plane;
12778
e7c84544 12779 if (INTEL_INFO(dev)->gen < 9 || !new_state->active)
08db6652
DL
12780 return;
12781
12782 skl_ddb_get_hw_state(dev_priv, &hw_ddb);
12783 sw_ddb = &dev_priv->wm.skl_hw.ddb;
12784
e7c84544
ML
12785 /* planes */
12786 for_each_plane(dev_priv, pipe, plane) {
12787 hw_entry = &hw_ddb.plane[pipe][plane];
12788 sw_entry = &sw_ddb->plane[pipe][plane];
08db6652 12789
e7c84544 12790 if (skl_ddb_entry_equal(hw_entry, sw_entry))
08db6652
DL
12791 continue;
12792
e7c84544
ML
12793 DRM_ERROR("mismatch in DDB state pipe %c plane %d "
12794 "(expected (%u,%u), found (%u,%u))\n",
12795 pipe_name(pipe), plane + 1,
12796 sw_entry->start, sw_entry->end,
12797 hw_entry->start, hw_entry->end);
12798 }
08db6652 12799
e7c84544
ML
12800 /* cursor */
12801 hw_entry = &hw_ddb.plane[pipe][PLANE_CURSOR];
12802 sw_entry = &sw_ddb->plane[pipe][PLANE_CURSOR];
08db6652 12803
e7c84544 12804 if (!skl_ddb_entry_equal(hw_entry, sw_entry)) {
08db6652
DL
12805 DRM_ERROR("mismatch in DDB state pipe %c cursor "
12806 "(expected (%u,%u), found (%u,%u))\n",
12807 pipe_name(pipe),
12808 sw_entry->start, sw_entry->end,
12809 hw_entry->start, hw_entry->end);
12810 }
12811}
12812
91d1b4bd 12813static void
c0ead703 12814verify_connector_state(struct drm_device *dev, struct drm_crtc *crtc)
8af6cf88 12815{
35dd3c64 12816 struct drm_connector *connector;
8af6cf88 12817
e7c84544 12818 drm_for_each_connector(connector, dev) {
35dd3c64
ML
12819 struct drm_encoder *encoder = connector->encoder;
12820 struct drm_connector_state *state = connector->state;
ad3c558f 12821
e7c84544
ML
12822 if (state->crtc != crtc)
12823 continue;
12824
c0ead703 12825 intel_connector_verify_state(to_intel_connector(connector));
8af6cf88 12826
ad3c558f 12827 I915_STATE_WARN(state->best_encoder != encoder,
35dd3c64 12828 "connector's atomic encoder doesn't match legacy encoder\n");
8af6cf88 12829 }
91d1b4bd
DV
12830}
12831
12832static void
c0ead703 12833verify_encoder_state(struct drm_device *dev)
91d1b4bd
DV
12834{
12835 struct intel_encoder *encoder;
12836 struct intel_connector *connector;
8af6cf88 12837
b2784e15 12838 for_each_intel_encoder(dev, encoder) {
8af6cf88 12839 bool enabled = false;
4d20cd86 12840 enum pipe pipe;
8af6cf88
DV
12841
12842 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
12843 encoder->base.base.id,
8e329a03 12844 encoder->base.name);
8af6cf88 12845
3a3371ff 12846 for_each_intel_connector(dev, connector) {
4d20cd86 12847 if (connector->base.state->best_encoder != &encoder->base)
8af6cf88
DV
12848 continue;
12849 enabled = true;
ad3c558f
ML
12850
12851 I915_STATE_WARN(connector->base.state->crtc !=
12852 encoder->base.crtc,
12853 "connector's crtc doesn't match encoder crtc\n");
8af6cf88 12854 }
0e32b39c 12855
e2c719b7 12856 I915_STATE_WARN(!!encoder->base.crtc != enabled,
8af6cf88
DV
12857 "encoder's enabled state mismatch "
12858 "(expected %i, found %i)\n",
12859 !!encoder->base.crtc, enabled);
7c60d198
ML
12860
12861 if (!encoder->base.crtc) {
4d20cd86 12862 bool active;
7c60d198 12863
4d20cd86
ML
12864 active = encoder->get_hw_state(encoder, &pipe);
12865 I915_STATE_WARN(active,
12866 "encoder detached but still enabled on pipe %c.\n",
12867 pipe_name(pipe));
7c60d198 12868 }
8af6cf88 12869 }
91d1b4bd
DV
12870}
12871
12872static void
c0ead703
ML
12873verify_crtc_state(struct drm_crtc *crtc,
12874 struct drm_crtc_state *old_crtc_state,
12875 struct drm_crtc_state *new_crtc_state)
91d1b4bd 12876{
e7c84544 12877 struct drm_device *dev = crtc->dev;
fbee40df 12878 struct drm_i915_private *dev_priv = dev->dev_private;
91d1b4bd 12879 struct intel_encoder *encoder;
e7c84544
ML
12880 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
12881 struct intel_crtc_state *pipe_config, *sw_config;
12882 struct drm_atomic_state *old_state;
12883 bool active;
045ac3b5 12884
e7c84544
ML
12885 old_state = old_crtc_state->state;
12886 __drm_atomic_helper_crtc_destroy_state(crtc, old_crtc_state);
12887 pipe_config = to_intel_crtc_state(old_crtc_state);
12888 memset(pipe_config, 0, sizeof(*pipe_config));
12889 pipe_config->base.crtc = crtc;
12890 pipe_config->base.state = old_state;
8af6cf88 12891
e7c84544 12892 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
8af6cf88 12893
e7c84544 12894 active = dev_priv->display.get_pipe_config(intel_crtc, pipe_config);
d62cf62a 12895
e7c84544
ML
12896 /* hw state is inconsistent with the pipe quirk */
12897 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
12898 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
12899 active = new_crtc_state->active;
6c49f241 12900
e7c84544
ML
12901 I915_STATE_WARN(new_crtc_state->active != active,
12902 "crtc active state doesn't match with hw state "
12903 "(expected %i, found %i)\n", new_crtc_state->active, active);
0e8ffe1b 12904
e7c84544
ML
12905 I915_STATE_WARN(intel_crtc->active != new_crtc_state->active,
12906 "transitional active state does not match atomic hw state "
12907 "(expected %i, found %i)\n", new_crtc_state->active, intel_crtc->active);
4d20cd86 12908
e7c84544
ML
12909 for_each_encoder_on_crtc(dev, crtc, encoder) {
12910 enum pipe pipe;
4d20cd86 12911
e7c84544
ML
12912 active = encoder->get_hw_state(encoder, &pipe);
12913 I915_STATE_WARN(active != new_crtc_state->active,
12914 "[ENCODER:%i] active %i with crtc active %i\n",
12915 encoder->base.base.id, active, new_crtc_state->active);
4d20cd86 12916
e7c84544
ML
12917 I915_STATE_WARN(active && intel_crtc->pipe != pipe,
12918 "Encoder connected to wrong pipe %c\n",
12919 pipe_name(pipe));
4d20cd86 12920
e7c84544
ML
12921 if (active)
12922 encoder->get_config(encoder, pipe_config);
12923 }
53d9f4e9 12924
e7c84544
ML
12925 if (!new_crtc_state->active)
12926 return;
cfb23ed6 12927
e7c84544 12928 intel_pipe_config_sanity_check(dev_priv, pipe_config);
e3b247da 12929
e7c84544
ML
12930 sw_config = to_intel_crtc_state(crtc->state);
12931 if (!intel_pipe_config_compare(dev, sw_config,
12932 pipe_config, false)) {
12933 I915_STATE_WARN(1, "pipe state doesn't match!\n");
12934 intel_dump_pipe_config(intel_crtc, pipe_config,
12935 "[hw state]");
12936 intel_dump_pipe_config(intel_crtc, sw_config,
12937 "[sw state]");
8af6cf88
DV
12938 }
12939}
12940
91d1b4bd 12941static void
c0ead703
ML
12942verify_single_dpll_state(struct drm_i915_private *dev_priv,
12943 struct intel_shared_dpll *pll,
12944 struct drm_crtc *crtc,
12945 struct drm_crtc_state *new_state)
91d1b4bd 12946{
91d1b4bd 12947 struct intel_dpll_hw_state dpll_hw_state;
e7c84544
ML
12948 unsigned crtc_mask;
12949 bool active;
5358901f 12950
e7c84544 12951 memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
5358901f 12952
e7c84544 12953 DRM_DEBUG_KMS("%s\n", pll->name);
5358901f 12954
e7c84544 12955 active = pll->funcs.get_hw_state(dev_priv, pll, &dpll_hw_state);
5358901f 12956
e7c84544
ML
12957 if (!(pll->flags & INTEL_DPLL_ALWAYS_ON)) {
12958 I915_STATE_WARN(!pll->on && pll->active_mask,
12959 "pll in active use but not on in sw tracking\n");
12960 I915_STATE_WARN(pll->on && !pll->active_mask,
12961 "pll is on but not used by any active crtc\n");
12962 I915_STATE_WARN(pll->on != active,
12963 "pll on state mismatch (expected %i, found %i)\n",
12964 pll->on, active);
12965 }
5358901f 12966
e7c84544 12967 if (!crtc) {
2dd66ebd 12968 I915_STATE_WARN(pll->active_mask & ~pll->config.crtc_mask,
e7c84544
ML
12969 "more active pll users than references: %x vs %x\n",
12970 pll->active_mask, pll->config.crtc_mask);
5358901f 12971
e7c84544
ML
12972 return;
12973 }
12974
12975 crtc_mask = 1 << drm_crtc_index(crtc);
12976
12977 if (new_state->active)
12978 I915_STATE_WARN(!(pll->active_mask & crtc_mask),
12979 "pll active mismatch (expected pipe %c in active mask 0x%02x)\n",
12980 pipe_name(drm_crtc_index(crtc)), pll->active_mask);
12981 else
12982 I915_STATE_WARN(pll->active_mask & crtc_mask,
12983 "pll active mismatch (didn't expect pipe %c in active mask 0x%02x)\n",
12984 pipe_name(drm_crtc_index(crtc)), pll->active_mask);
2dd66ebd 12985
e7c84544
ML
12986 I915_STATE_WARN(!(pll->config.crtc_mask & crtc_mask),
12987 "pll enabled crtcs mismatch (expected 0x%x in 0x%02x)\n",
12988 crtc_mask, pll->config.crtc_mask);
66e985c0 12989
e7c84544
ML
12990 I915_STATE_WARN(pll->on && memcmp(&pll->config.hw_state,
12991 &dpll_hw_state,
12992 sizeof(dpll_hw_state)),
12993 "pll hw state mismatch\n");
12994}
12995
12996static void
c0ead703
ML
12997verify_shared_dpll_state(struct drm_device *dev, struct drm_crtc *crtc,
12998 struct drm_crtc_state *old_crtc_state,
12999 struct drm_crtc_state *new_crtc_state)
e7c84544
ML
13000{
13001 struct drm_i915_private *dev_priv = dev->dev_private;
13002 struct intel_crtc_state *old_state = to_intel_crtc_state(old_crtc_state);
13003 struct intel_crtc_state *new_state = to_intel_crtc_state(new_crtc_state);
13004
13005 if (new_state->shared_dpll)
c0ead703 13006 verify_single_dpll_state(dev_priv, new_state->shared_dpll, crtc, new_crtc_state);
e7c84544
ML
13007
13008 if (old_state->shared_dpll &&
13009 old_state->shared_dpll != new_state->shared_dpll) {
13010 unsigned crtc_mask = 1 << drm_crtc_index(crtc);
13011 struct intel_shared_dpll *pll = old_state->shared_dpll;
13012
13013 I915_STATE_WARN(pll->active_mask & crtc_mask,
13014 "pll active mismatch (didn't expect pipe %c in active mask)\n",
13015 pipe_name(drm_crtc_index(crtc)));
13016 I915_STATE_WARN(pll->config.crtc_mask & crtc_mask,
13017 "pll enabled crtcs mismatch (found %x in enabled mask)\n",
13018 pipe_name(drm_crtc_index(crtc)));
5358901f 13019 }
8af6cf88
DV
13020}
13021
e7c84544 13022static void
c0ead703 13023intel_modeset_verify_crtc(struct drm_crtc *crtc,
e7c84544
ML
13024 struct drm_crtc_state *old_state,
13025 struct drm_crtc_state *new_state)
13026{
13027 if (!needs_modeset(new_state) &&
13028 !to_intel_crtc_state(new_state)->update_pipe)
13029 return;
13030
c0ead703
ML
13031 verify_wm_state(crtc, new_state);
13032 verify_connector_state(crtc->dev, crtc);
13033 verify_crtc_state(crtc, old_state, new_state);
13034 verify_shared_dpll_state(crtc->dev, crtc, old_state, new_state);
e7c84544
ML
13035}
13036
13037static void
c0ead703 13038verify_disabled_dpll_state(struct drm_device *dev)
e7c84544
ML
13039{
13040 struct drm_i915_private *dev_priv = dev->dev_private;
13041 int i;
13042
13043 for (i = 0; i < dev_priv->num_shared_dpll; i++)
c0ead703 13044 verify_single_dpll_state(dev_priv, &dev_priv->shared_dplls[i], NULL, NULL);
e7c84544
ML
13045}
13046
13047static void
c0ead703 13048intel_modeset_verify_disabled(struct drm_device *dev)
e7c84544 13049{
c0ead703
ML
13050 verify_encoder_state(dev);
13051 verify_connector_state(dev, NULL);
13052 verify_disabled_dpll_state(dev);
e7c84544
ML
13053}
13054
80715b2f
VS
13055static void update_scanline_offset(struct intel_crtc *crtc)
13056{
13057 struct drm_device *dev = crtc->base.dev;
13058
13059 /*
13060 * The scanline counter increments at the leading edge of hsync.
13061 *
13062 * On most platforms it starts counting from vtotal-1 on the
13063 * first active line. That means the scanline counter value is
13064 * always one less than what we would expect. Ie. just after
13065 * start of vblank, which also occurs at start of hsync (on the
13066 * last active line), the scanline counter will read vblank_start-1.
13067 *
13068 * On gen2 the scanline counter starts counting from 1 instead
13069 * of vtotal-1, so we have to subtract one (or rather add vtotal-1
13070 * to keep the value positive), instead of adding one.
13071 *
13072 * On HSW+ the behaviour of the scanline counter depends on the output
13073 * type. For DP ports it behaves like most other platforms, but on HDMI
13074 * there's an extra 1 line difference. So we need to add two instead of
13075 * one to the value.
13076 */
13077 if (IS_GEN2(dev)) {
124abe07 13078 const struct drm_display_mode *adjusted_mode = &crtc->config->base.adjusted_mode;
80715b2f
VS
13079 int vtotal;
13080
124abe07
VS
13081 vtotal = adjusted_mode->crtc_vtotal;
13082 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
80715b2f
VS
13083 vtotal /= 2;
13084
13085 crtc->scanline_offset = vtotal - 1;
13086 } else if (HAS_DDI(dev) &&
409ee761 13087 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI)) {
80715b2f
VS
13088 crtc->scanline_offset = 2;
13089 } else
13090 crtc->scanline_offset = 1;
13091}
13092
ad421372 13093static void intel_modeset_clear_plls(struct drm_atomic_state *state)
ed6739ef 13094{
225da59b 13095 struct drm_device *dev = state->dev;
ed6739ef 13096 struct drm_i915_private *dev_priv = to_i915(dev);
ad421372 13097 struct intel_shared_dpll_config *shared_dpll = NULL;
0a9ab303
ACO
13098 struct drm_crtc *crtc;
13099 struct drm_crtc_state *crtc_state;
0a9ab303 13100 int i;
ed6739ef
ACO
13101
13102 if (!dev_priv->display.crtc_compute_clock)
ad421372 13103 return;
ed6739ef 13104
0a9ab303 13105 for_each_crtc_in_state(state, crtc, crtc_state, i) {
fb1a38a9 13106 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8106ddbd
ACO
13107 struct intel_shared_dpll *old_dpll =
13108 to_intel_crtc_state(crtc->state)->shared_dpll;
0a9ab303 13109
fb1a38a9 13110 if (!needs_modeset(crtc_state))
225da59b
ACO
13111 continue;
13112
8106ddbd 13113 to_intel_crtc_state(crtc_state)->shared_dpll = NULL;
fb1a38a9 13114
8106ddbd 13115 if (!old_dpll)
fb1a38a9 13116 continue;
0a9ab303 13117
ad421372
ML
13118 if (!shared_dpll)
13119 shared_dpll = intel_atomic_get_shared_dpll_state(state);
ed6739ef 13120
8106ddbd 13121 intel_shared_dpll_config_put(shared_dpll, old_dpll, intel_crtc);
ad421372 13122 }
ed6739ef
ACO
13123}
13124
99d736a2
ML
13125/*
13126 * This implements the workaround described in the "notes" section of the mode
13127 * set sequence documentation. When going from no pipes or single pipe to
13128 * multiple pipes, and planes are enabled after the pipe, we need to wait at
13129 * least 2 vblanks on the first pipe before enabling planes on the second pipe.
13130 */
13131static int haswell_mode_set_planes_workaround(struct drm_atomic_state *state)
13132{
13133 struct drm_crtc_state *crtc_state;
13134 struct intel_crtc *intel_crtc;
13135 struct drm_crtc *crtc;
13136 struct intel_crtc_state *first_crtc_state = NULL;
13137 struct intel_crtc_state *other_crtc_state = NULL;
13138 enum pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
13139 int i;
13140
13141 /* look at all crtc's that are going to be enabled in during modeset */
13142 for_each_crtc_in_state(state, crtc, crtc_state, i) {
13143 intel_crtc = to_intel_crtc(crtc);
13144
13145 if (!crtc_state->active || !needs_modeset(crtc_state))
13146 continue;
13147
13148 if (first_crtc_state) {
13149 other_crtc_state = to_intel_crtc_state(crtc_state);
13150 break;
13151 } else {
13152 first_crtc_state = to_intel_crtc_state(crtc_state);
13153 first_pipe = intel_crtc->pipe;
13154 }
13155 }
13156
13157 /* No workaround needed? */
13158 if (!first_crtc_state)
13159 return 0;
13160
13161 /* w/a possibly needed, check how many crtc's are already enabled. */
13162 for_each_intel_crtc(state->dev, intel_crtc) {
13163 struct intel_crtc_state *pipe_config;
13164
13165 pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
13166 if (IS_ERR(pipe_config))
13167 return PTR_ERR(pipe_config);
13168
13169 pipe_config->hsw_workaround_pipe = INVALID_PIPE;
13170
13171 if (!pipe_config->base.active ||
13172 needs_modeset(&pipe_config->base))
13173 continue;
13174
13175 /* 2 or more enabled crtcs means no need for w/a */
13176 if (enabled_pipe != INVALID_PIPE)
13177 return 0;
13178
13179 enabled_pipe = intel_crtc->pipe;
13180 }
13181
13182 if (enabled_pipe != INVALID_PIPE)
13183 first_crtc_state->hsw_workaround_pipe = enabled_pipe;
13184 else if (other_crtc_state)
13185 other_crtc_state->hsw_workaround_pipe = first_pipe;
13186
13187 return 0;
13188}
13189
27c329ed
ML
13190static int intel_modeset_all_pipes(struct drm_atomic_state *state)
13191{
13192 struct drm_crtc *crtc;
13193 struct drm_crtc_state *crtc_state;
13194 int ret = 0;
13195
13196 /* add all active pipes to the state */
13197 for_each_crtc(state->dev, crtc) {
13198 crtc_state = drm_atomic_get_crtc_state(state, crtc);
13199 if (IS_ERR(crtc_state))
13200 return PTR_ERR(crtc_state);
13201
13202 if (!crtc_state->active || needs_modeset(crtc_state))
13203 continue;
13204
13205 crtc_state->mode_changed = true;
13206
13207 ret = drm_atomic_add_affected_connectors(state, crtc);
13208 if (ret)
13209 break;
13210
13211 ret = drm_atomic_add_affected_planes(state, crtc);
13212 if (ret)
13213 break;
13214 }
13215
13216 return ret;
13217}
13218
c347a676 13219static int intel_modeset_checks(struct drm_atomic_state *state)
054518dd 13220{
565602d7
ML
13221 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
13222 struct drm_i915_private *dev_priv = state->dev->dev_private;
13223 struct drm_crtc *crtc;
13224 struct drm_crtc_state *crtc_state;
13225 int ret = 0, i;
054518dd 13226
b359283a
ML
13227 if (!check_digital_port_conflicts(state)) {
13228 DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
13229 return -EINVAL;
13230 }
13231
565602d7
ML
13232 intel_state->modeset = true;
13233 intel_state->active_crtcs = dev_priv->active_crtcs;
13234
13235 for_each_crtc_in_state(state, crtc, crtc_state, i) {
13236 if (crtc_state->active)
13237 intel_state->active_crtcs |= 1 << i;
13238 else
13239 intel_state->active_crtcs &= ~(1 << i);
13240 }
13241
054518dd
ACO
13242 /*
13243 * See if the config requires any additional preparation, e.g.
13244 * to adjust global state with pipes off. We need to do this
13245 * here so we can get the modeset_pipe updated config for the new
13246 * mode set on this crtc. For other crtcs we need to use the
13247 * adjusted_mode bits in the crtc directly.
13248 */
27c329ed 13249 if (dev_priv->display.modeset_calc_cdclk) {
27c329ed
ML
13250 ret = dev_priv->display.modeset_calc_cdclk(state);
13251
1a617b77 13252 if (!ret && intel_state->dev_cdclk != dev_priv->cdclk_freq)
27c329ed
ML
13253 ret = intel_modeset_all_pipes(state);
13254
13255 if (ret < 0)
054518dd 13256 return ret;
e8788cbc
ML
13257
13258 DRM_DEBUG_KMS("New cdclk calculated to be atomic %u, actual %u\n",
13259 intel_state->cdclk, intel_state->dev_cdclk);
27c329ed 13260 } else
1a617b77 13261 to_intel_atomic_state(state)->cdclk = dev_priv->atomic_cdclk_freq;
054518dd 13262
ad421372 13263 intel_modeset_clear_plls(state);
054518dd 13264
565602d7 13265 if (IS_HASWELL(dev_priv))
ad421372 13266 return haswell_mode_set_planes_workaround(state);
99d736a2 13267
ad421372 13268 return 0;
c347a676
ACO
13269}
13270
aa363136
MR
13271/*
13272 * Handle calculation of various watermark data at the end of the atomic check
13273 * phase. The code here should be run after the per-crtc and per-plane 'check'
13274 * handlers to ensure that all derived state has been updated.
13275 */
13276static void calc_watermark_data(struct drm_atomic_state *state)
13277{
13278 struct drm_device *dev = state->dev;
13279 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
13280 struct drm_crtc *crtc;
13281 struct drm_crtc_state *cstate;
13282 struct drm_plane *plane;
13283 struct drm_plane_state *pstate;
13284
13285 /*
13286 * Calculate watermark configuration details now that derived
13287 * plane/crtc state is all properly updated.
13288 */
13289 drm_for_each_crtc(crtc, dev) {
13290 cstate = drm_atomic_get_existing_crtc_state(state, crtc) ?:
13291 crtc->state;
13292
13293 if (cstate->active)
13294 intel_state->wm_config.num_pipes_active++;
13295 }
13296 drm_for_each_legacy_plane(plane, dev) {
13297 pstate = drm_atomic_get_existing_plane_state(state, plane) ?:
13298 plane->state;
13299
13300 if (!to_intel_plane_state(pstate)->visible)
13301 continue;
13302
13303 intel_state->wm_config.sprites_enabled = true;
13304 if (pstate->crtc_w != pstate->src_w >> 16 ||
13305 pstate->crtc_h != pstate->src_h >> 16)
13306 intel_state->wm_config.sprites_scaled = true;
13307 }
13308}
13309
74c090b1
ML
13310/**
13311 * intel_atomic_check - validate state object
13312 * @dev: drm device
13313 * @state: state to validate
13314 */
13315static int intel_atomic_check(struct drm_device *dev,
13316 struct drm_atomic_state *state)
c347a676 13317{
dd8b3bdb 13318 struct drm_i915_private *dev_priv = to_i915(dev);
aa363136 13319 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
c347a676
ACO
13320 struct drm_crtc *crtc;
13321 struct drm_crtc_state *crtc_state;
13322 int ret, i;
61333b60 13323 bool any_ms = false;
c347a676 13324
74c090b1 13325 ret = drm_atomic_helper_check_modeset(dev, state);
054518dd
ACO
13326 if (ret)
13327 return ret;
13328
c347a676 13329 for_each_crtc_in_state(state, crtc, crtc_state, i) {
cfb23ed6
ML
13330 struct intel_crtc_state *pipe_config =
13331 to_intel_crtc_state(crtc_state);
1ed51de9
DV
13332
13333 /* Catch I915_MODE_FLAG_INHERITED */
13334 if (crtc_state->mode.private_flags != crtc->state->mode.private_flags)
13335 crtc_state->mode_changed = true;
cfb23ed6 13336
61333b60
ML
13337 if (!crtc_state->enable) {
13338 if (needs_modeset(crtc_state))
13339 any_ms = true;
c347a676 13340 continue;
61333b60 13341 }
c347a676 13342
26495481 13343 if (!needs_modeset(crtc_state))
cfb23ed6
ML
13344 continue;
13345
26495481
DV
13346 /* FIXME: For only active_changed we shouldn't need to do any
13347 * state recomputation at all. */
13348
1ed51de9
DV
13349 ret = drm_atomic_add_affected_connectors(state, crtc);
13350 if (ret)
13351 return ret;
b359283a 13352
cfb23ed6 13353 ret = intel_modeset_pipe_config(crtc, pipe_config);
c347a676
ACO
13354 if (ret)
13355 return ret;
13356
73831236 13357 if (i915.fastboot &&
dd8b3bdb 13358 intel_pipe_config_compare(dev,
cfb23ed6 13359 to_intel_crtc_state(crtc->state),
1ed51de9 13360 pipe_config, true)) {
26495481 13361 crtc_state->mode_changed = false;
bfd16b2a 13362 to_intel_crtc_state(crtc_state)->update_pipe = true;
26495481
DV
13363 }
13364
13365 if (needs_modeset(crtc_state)) {
13366 any_ms = true;
cfb23ed6
ML
13367
13368 ret = drm_atomic_add_affected_planes(state, crtc);
13369 if (ret)
13370 return ret;
13371 }
61333b60 13372
26495481
DV
13373 intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
13374 needs_modeset(crtc_state) ?
13375 "[modeset]" : "[fastset]");
c347a676
ACO
13376 }
13377
61333b60
ML
13378 if (any_ms) {
13379 ret = intel_modeset_checks(state);
13380
13381 if (ret)
13382 return ret;
27c329ed 13383 } else
dd8b3bdb 13384 intel_state->cdclk = dev_priv->cdclk_freq;
76305b1a 13385
dd8b3bdb 13386 ret = drm_atomic_helper_check_planes(dev, state);
aa363136
MR
13387 if (ret)
13388 return ret;
13389
f51be2e0 13390 intel_fbc_choose_crtc(dev_priv, state);
aa363136
MR
13391 calc_watermark_data(state);
13392
13393 return 0;
054518dd
ACO
13394}
13395
5008e874
ML
13396static int intel_atomic_prepare_commit(struct drm_device *dev,
13397 struct drm_atomic_state *state,
13398 bool async)
13399{
7580d774
ML
13400 struct drm_i915_private *dev_priv = dev->dev_private;
13401 struct drm_plane_state *plane_state;
5008e874 13402 struct drm_crtc_state *crtc_state;
7580d774 13403 struct drm_plane *plane;
5008e874
ML
13404 struct drm_crtc *crtc;
13405 int i, ret;
13406
13407 if (async) {
13408 DRM_DEBUG_KMS("i915 does not yet support async commit\n");
13409 return -EINVAL;
13410 }
13411
13412 for_each_crtc_in_state(state, crtc, crtc_state, i) {
13413 ret = intel_crtc_wait_for_pending_flips(crtc);
13414 if (ret)
13415 return ret;
7580d774
ML
13416
13417 if (atomic_read(&to_intel_crtc(crtc)->unpin_work_count) >= 2)
13418 flush_workqueue(dev_priv->wq);
5008e874
ML
13419 }
13420
f935675f
ML
13421 ret = mutex_lock_interruptible(&dev->struct_mutex);
13422 if (ret)
13423 return ret;
13424
5008e874 13425 ret = drm_atomic_helper_prepare_planes(dev, state);
c19ae989 13426 if (!ret && !async && !i915_reset_in_progress_or_wedged(&dev_priv->gpu_error)) {
7580d774
ML
13427 u32 reset_counter;
13428
c19ae989 13429 reset_counter = i915_reset_counter(&dev_priv->gpu_error);
7580d774
ML
13430 mutex_unlock(&dev->struct_mutex);
13431
13432 for_each_plane_in_state(state, plane, plane_state, i) {
13433 struct intel_plane_state *intel_plane_state =
13434 to_intel_plane_state(plane_state);
13435
13436 if (!intel_plane_state->wait_req)
13437 continue;
13438
13439 ret = __i915_wait_request(intel_plane_state->wait_req,
13440 reset_counter, true,
13441 NULL, NULL);
13442
13443 /* Swallow -EIO errors to allow updates during hw lockup. */
13444 if (ret == -EIO)
13445 ret = 0;
13446
13447 if (ret)
13448 break;
13449 }
13450
13451 if (!ret)
13452 return 0;
13453
13454 mutex_lock(&dev->struct_mutex);
13455 drm_atomic_helper_cleanup_planes(dev, state);
13456 }
5008e874 13457
f935675f 13458 mutex_unlock(&dev->struct_mutex);
5008e874
ML
13459 return ret;
13460}
13461
e8861675
ML
13462static void intel_atomic_wait_for_vblanks(struct drm_device *dev,
13463 struct drm_i915_private *dev_priv,
13464 unsigned crtc_mask)
13465{
13466 unsigned last_vblank_count[I915_MAX_PIPES];
13467 enum pipe pipe;
13468 int ret;
13469
13470 if (!crtc_mask)
13471 return;
13472
13473 for_each_pipe(dev_priv, pipe) {
13474 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
13475
13476 if (!((1 << pipe) & crtc_mask))
13477 continue;
13478
13479 ret = drm_crtc_vblank_get(crtc);
13480 if (WARN_ON(ret != 0)) {
13481 crtc_mask &= ~(1 << pipe);
13482 continue;
13483 }
13484
13485 last_vblank_count[pipe] = drm_crtc_vblank_count(crtc);
13486 }
13487
13488 for_each_pipe(dev_priv, pipe) {
13489 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
13490 long lret;
13491
13492 if (!((1 << pipe) & crtc_mask))
13493 continue;
13494
13495 lret = wait_event_timeout(dev->vblank[pipe].queue,
13496 last_vblank_count[pipe] !=
13497 drm_crtc_vblank_count(crtc),
13498 msecs_to_jiffies(50));
13499
13500 WARN_ON(!lret);
13501
13502 drm_crtc_vblank_put(crtc);
13503 }
13504}
13505
13506static bool needs_vblank_wait(struct intel_crtc_state *crtc_state)
13507{
13508 /* fb updated, need to unpin old fb */
13509 if (crtc_state->fb_changed)
13510 return true;
13511
13512 /* wm changes, need vblank before final wm's */
caed361d 13513 if (crtc_state->update_wm_post)
e8861675
ML
13514 return true;
13515
13516 /*
13517 * cxsr is re-enabled after vblank.
caed361d 13518 * This is already handled by crtc_state->update_wm_post,
e8861675
ML
13519 * but added for clarity.
13520 */
13521 if (crtc_state->disable_cxsr)
13522 return true;
13523
13524 return false;
13525}
13526
74c090b1
ML
13527/**
13528 * intel_atomic_commit - commit validated state object
13529 * @dev: DRM device
13530 * @state: the top-level driver state object
13531 * @async: asynchronous commit
13532 *
13533 * This function commits a top-level state object that has been validated
13534 * with drm_atomic_helper_check().
13535 *
13536 * FIXME: Atomic modeset support for i915 is not yet complete. At the moment
13537 * we can only handle plane-related operations and do not yet support
13538 * asynchronous commit.
13539 *
13540 * RETURNS
13541 * Zero for success or -errno.
13542 */
13543static int intel_atomic_commit(struct drm_device *dev,
13544 struct drm_atomic_state *state,
13545 bool async)
a6778b3c 13546{
565602d7 13547 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
fbee40df 13548 struct drm_i915_private *dev_priv = dev->dev_private;
29ceb0e6 13549 struct drm_crtc_state *old_crtc_state;
7580d774 13550 struct drm_crtc *crtc;
ed4a6a7c 13551 struct intel_crtc_state *intel_cstate;
565602d7
ML
13552 int ret = 0, i;
13553 bool hw_check = intel_state->modeset;
33c8df89 13554 unsigned long put_domains[I915_MAX_PIPES] = {};
e8861675 13555 unsigned crtc_vblank_mask = 0;
a6778b3c 13556
5008e874 13557 ret = intel_atomic_prepare_commit(dev, state, async);
7580d774
ML
13558 if (ret) {
13559 DRM_DEBUG_ATOMIC("Preparing state failed with %i\n", ret);
d4afb8cc 13560 return ret;
7580d774 13561 }
d4afb8cc 13562
1c5e19f8 13563 drm_atomic_helper_swap_state(dev, state);
a1475e77
ML
13564 dev_priv->wm.config = intel_state->wm_config;
13565 intel_shared_dpll_commit(state);
1c5e19f8 13566
565602d7
ML
13567 if (intel_state->modeset) {
13568 memcpy(dev_priv->min_pixclk, intel_state->min_pixclk,
13569 sizeof(intel_state->min_pixclk));
13570 dev_priv->active_crtcs = intel_state->active_crtcs;
1a617b77 13571 dev_priv->atomic_cdclk_freq = intel_state->cdclk;
33c8df89
ML
13572
13573 intel_display_power_get(dev_priv, POWER_DOMAIN_MODESET);
565602d7
ML
13574 }
13575
29ceb0e6 13576 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
a539205a
ML
13577 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13578
33c8df89
ML
13579 if (needs_modeset(crtc->state) ||
13580 to_intel_crtc_state(crtc->state)->update_pipe) {
13581 hw_check = true;
13582
13583 put_domains[to_intel_crtc(crtc)->pipe] =
13584 modeset_get_crtc_power_domains(crtc,
13585 to_intel_crtc_state(crtc->state));
13586 }
13587
61333b60
ML
13588 if (!needs_modeset(crtc->state))
13589 continue;
13590
29ceb0e6 13591 intel_pre_plane_update(to_intel_crtc_state(old_crtc_state));
460da916 13592
29ceb0e6
VS
13593 if (old_crtc_state->active) {
13594 intel_crtc_disable_planes(crtc, old_crtc_state->plane_mask);
a539205a 13595 dev_priv->display.crtc_disable(crtc);
eddfcbcd 13596 intel_crtc->active = false;
58f9c0bc 13597 intel_fbc_disable(intel_crtc);
eddfcbcd 13598 intel_disable_shared_dpll(intel_crtc);
9bbc8258
VS
13599
13600 /*
13601 * Underruns don't always raise
13602 * interrupts, so check manually.
13603 */
13604 intel_check_cpu_fifo_underruns(dev_priv);
13605 intel_check_pch_fifo_underruns(dev_priv);
b9001114
ML
13606
13607 if (!crtc->state->active)
13608 intel_update_watermarks(crtc);
a539205a 13609 }
b8cecdf5 13610 }
7758a113 13611
ea9d758d
DV
13612 /* Only after disabling all output pipelines that will be changed can we
13613 * update the the output configuration. */
4740b0f2 13614 intel_modeset_update_crtc_state(state);
f6e5b160 13615
565602d7 13616 if (intel_state->modeset) {
4740b0f2 13617 drm_atomic_helper_update_legacy_modeset_state(state->dev, state);
33c8df89
ML
13618
13619 if (dev_priv->display.modeset_commit_cdclk &&
13620 intel_state->dev_cdclk != dev_priv->cdclk_freq)
13621 dev_priv->display.modeset_commit_cdclk(state);
f6d1973d 13622
c0ead703 13623 intel_modeset_verify_disabled(dev);
4740b0f2 13624 }
47fab737 13625
a6778b3c 13626 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
29ceb0e6 13627 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
f6ac4b2a
ML
13628 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13629 bool modeset = needs_modeset(crtc->state);
e8861675
ML
13630 struct intel_crtc_state *pipe_config =
13631 to_intel_crtc_state(crtc->state);
13632 bool update_pipe = !modeset && pipe_config->update_pipe;
9f836f90 13633
f6ac4b2a 13634 if (modeset && crtc->state->active) {
a539205a
ML
13635 update_scanline_offset(to_intel_crtc(crtc));
13636 dev_priv->display.crtc_enable(crtc);
13637 }
80715b2f 13638
f6ac4b2a 13639 if (!modeset)
29ceb0e6 13640 intel_pre_plane_update(to_intel_crtc_state(old_crtc_state));
f6ac4b2a 13641
31ae71fc
ML
13642 if (crtc->state->active &&
13643 drm_atomic_get_existing_plane_state(state, crtc->primary))
49227c4a
PZ
13644 intel_fbc_enable(intel_crtc);
13645
6173ee28
ML
13646 if (crtc->state->active &&
13647 (crtc->state->planes_changed || update_pipe))
29ceb0e6 13648 drm_atomic_helper_commit_planes_on_crtc(old_crtc_state);
bfd16b2a 13649
e8861675
ML
13650 if (pipe_config->base.active && needs_vblank_wait(pipe_config))
13651 crtc_vblank_mask |= 1 << i;
80715b2f 13652 }
a6778b3c 13653
a6778b3c 13654 /* FIXME: add subpixel order */
83a57153 13655
e8861675
ML
13656 if (!state->legacy_cursor_update)
13657 intel_atomic_wait_for_vblanks(dev, dev_priv, crtc_vblank_mask);
f935675f 13658
ed4a6a7c
MR
13659 /*
13660 * Now that the vblank has passed, we can go ahead and program the
13661 * optimal watermarks on platforms that need two-step watermark
13662 * programming.
13663 *
13664 * TODO: Move this (and other cleanup) to an async worker eventually.
13665 */
29ceb0e6 13666 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
ed4a6a7c
MR
13667 intel_cstate = to_intel_crtc_state(crtc->state);
13668
13669 if (dev_priv->display.optimize_watermarks)
13670 dev_priv->display.optimize_watermarks(intel_cstate);
13671 }
13672
177246a8
MR
13673 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
13674 intel_post_plane_update(to_intel_crtc_state(old_crtc_state));
13675
13676 if (put_domains[i])
13677 modeset_put_power_domains(dev_priv, put_domains[i]);
f6d1973d 13678
c0ead703 13679 intel_modeset_verify_crtc(crtc, old_crtc_state, crtc->state);
177246a8
MR
13680 }
13681
13682 if (intel_state->modeset)
13683 intel_display_power_put(dev_priv, POWER_DOMAIN_MODESET);
13684
f935675f 13685 mutex_lock(&dev->struct_mutex);
d4afb8cc 13686 drm_atomic_helper_cleanup_planes(dev, state);
f935675f 13687 mutex_unlock(&dev->struct_mutex);
2bfb4627 13688
ee165b1a 13689 drm_atomic_state_free(state);
f30da187 13690
75714940
MK
13691 /* As one of the primary mmio accessors, KMS has a high likelihood
13692 * of triggering bugs in unclaimed access. After we finish
13693 * modesetting, see if an error has been flagged, and if so
13694 * enable debugging for the next modeset - and hope we catch
13695 * the culprit.
13696 *
13697 * XXX note that we assume display power is on at this point.
13698 * This might hold true now but we need to add pm helper to check
13699 * unclaimed only when the hardware is on, as atomic commits
13700 * can happen also when the device is completely off.
13701 */
13702 intel_uncore_arm_unclaimed_mmio_detection(dev_priv);
13703
74c090b1 13704 return 0;
7f27126e
JB
13705}
13706
c0c36b94
CW
13707void intel_crtc_restore_mode(struct drm_crtc *crtc)
13708{
83a57153
ACO
13709 struct drm_device *dev = crtc->dev;
13710 struct drm_atomic_state *state;
e694eb02 13711 struct drm_crtc_state *crtc_state;
2bfb4627 13712 int ret;
83a57153
ACO
13713
13714 state = drm_atomic_state_alloc(dev);
13715 if (!state) {
e694eb02 13716 DRM_DEBUG_KMS("[CRTC:%d] crtc restore failed, out of memory",
83a57153
ACO
13717 crtc->base.id);
13718 return;
13719 }
13720
e694eb02 13721 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
83a57153 13722
e694eb02
ML
13723retry:
13724 crtc_state = drm_atomic_get_crtc_state(state, crtc);
13725 ret = PTR_ERR_OR_ZERO(crtc_state);
13726 if (!ret) {
13727 if (!crtc_state->active)
13728 goto out;
83a57153 13729
e694eb02 13730 crtc_state->mode_changed = true;
74c090b1 13731 ret = drm_atomic_commit(state);
83a57153
ACO
13732 }
13733
e694eb02
ML
13734 if (ret == -EDEADLK) {
13735 drm_atomic_state_clear(state);
13736 drm_modeset_backoff(state->acquire_ctx);
13737 goto retry;
4ed9fb37 13738 }
4be07317 13739
2bfb4627 13740 if (ret)
e694eb02 13741out:
2bfb4627 13742 drm_atomic_state_free(state);
c0c36b94
CW
13743}
13744
25c5b266
DV
13745#undef for_each_intel_crtc_masked
13746
f6e5b160 13747static const struct drm_crtc_funcs intel_crtc_funcs = {
82cf435b 13748 .gamma_set = drm_atomic_helper_legacy_gamma_set,
74c090b1 13749 .set_config = drm_atomic_helper_set_config,
82cf435b 13750 .set_property = drm_atomic_helper_crtc_set_property,
f6e5b160
CW
13751 .destroy = intel_crtc_destroy,
13752 .page_flip = intel_crtc_page_flip,
1356837e
MR
13753 .atomic_duplicate_state = intel_crtc_duplicate_state,
13754 .atomic_destroy_state = intel_crtc_destroy_state,
f6e5b160
CW
13755};
13756
6beb8c23
MR
13757/**
13758 * intel_prepare_plane_fb - Prepare fb for usage on plane
13759 * @plane: drm plane to prepare for
13760 * @fb: framebuffer to prepare for presentation
13761 *
13762 * Prepares a framebuffer for usage on a display plane. Generally this
13763 * involves pinning the underlying object and updating the frontbuffer tracking
13764 * bits. Some older platforms need special physical address handling for
13765 * cursor planes.
13766 *
f935675f
ML
13767 * Must be called with struct_mutex held.
13768 *
6beb8c23
MR
13769 * Returns 0 on success, negative error code on failure.
13770 */
13771int
13772intel_prepare_plane_fb(struct drm_plane *plane,
d136dfee 13773 const struct drm_plane_state *new_state)
465c120c
MR
13774{
13775 struct drm_device *dev = plane->dev;
844f9111 13776 struct drm_framebuffer *fb = new_state->fb;
6beb8c23 13777 struct intel_plane *intel_plane = to_intel_plane(plane);
6beb8c23 13778 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
1ee49399 13779 struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->state->fb);
6beb8c23 13780 int ret = 0;
465c120c 13781
1ee49399 13782 if (!obj && !old_obj)
465c120c
MR
13783 return 0;
13784
5008e874
ML
13785 if (old_obj) {
13786 struct drm_crtc_state *crtc_state =
13787 drm_atomic_get_existing_crtc_state(new_state->state, plane->state->crtc);
13788
13789 /* Big Hammer, we also need to ensure that any pending
13790 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
13791 * current scanout is retired before unpinning the old
13792 * framebuffer. Note that we rely on userspace rendering
13793 * into the buffer attached to the pipe they are waiting
13794 * on. If not, userspace generates a GPU hang with IPEHR
13795 * point to the MI_WAIT_FOR_EVENT.
13796 *
13797 * This should only fail upon a hung GPU, in which case we
13798 * can safely continue.
13799 */
13800 if (needs_modeset(crtc_state))
13801 ret = i915_gem_object_wait_rendering(old_obj, true);
13802
13803 /* Swallow -EIO errors to allow updates during hw lockup. */
13804 if (ret && ret != -EIO)
f935675f 13805 return ret;
5008e874
ML
13806 }
13807
3c28ff22
AG
13808 /* For framebuffer backed by dmabuf, wait for fence */
13809 if (obj && obj->base.dma_buf) {
bcf8be27
ML
13810 long lret;
13811
13812 lret = reservation_object_wait_timeout_rcu(obj->base.dma_buf->resv,
13813 false, true,
13814 MAX_SCHEDULE_TIMEOUT);
13815 if (lret == -ERESTARTSYS)
13816 return lret;
3c28ff22 13817
bcf8be27 13818 WARN(lret < 0, "waiting returns %li\n", lret);
3c28ff22
AG
13819 }
13820
1ee49399
ML
13821 if (!obj) {
13822 ret = 0;
13823 } else if (plane->type == DRM_PLANE_TYPE_CURSOR &&
6beb8c23
MR
13824 INTEL_INFO(dev)->cursor_needs_physical) {
13825 int align = IS_I830(dev) ? 16 * 1024 : 256;
13826 ret = i915_gem_object_attach_phys(obj, align);
13827 if (ret)
13828 DRM_DEBUG_KMS("failed to attach phys object\n");
13829 } else {
3465c580 13830 ret = intel_pin_and_fence_fb_obj(fb, new_state->rotation);
6beb8c23 13831 }
465c120c 13832
7580d774
ML
13833 if (ret == 0) {
13834 if (obj) {
13835 struct intel_plane_state *plane_state =
13836 to_intel_plane_state(new_state);
13837
13838 i915_gem_request_assign(&plane_state->wait_req,
13839 obj->last_write_req);
13840 }
13841
a9ff8714 13842 i915_gem_track_fb(old_obj, obj, intel_plane->frontbuffer_bit);
7580d774 13843 }
fdd508a6 13844
6beb8c23
MR
13845 return ret;
13846}
13847
38f3ce3a
MR
13848/**
13849 * intel_cleanup_plane_fb - Cleans up an fb after plane use
13850 * @plane: drm plane to clean up for
13851 * @fb: old framebuffer that was on plane
13852 *
13853 * Cleans up a framebuffer that has just been removed from a plane.
f935675f
ML
13854 *
13855 * Must be called with struct_mutex held.
38f3ce3a
MR
13856 */
13857void
13858intel_cleanup_plane_fb(struct drm_plane *plane,
d136dfee 13859 const struct drm_plane_state *old_state)
38f3ce3a
MR
13860{
13861 struct drm_device *dev = plane->dev;
1ee49399 13862 struct intel_plane *intel_plane = to_intel_plane(plane);
7580d774 13863 struct intel_plane_state *old_intel_state;
1ee49399
ML
13864 struct drm_i915_gem_object *old_obj = intel_fb_obj(old_state->fb);
13865 struct drm_i915_gem_object *obj = intel_fb_obj(plane->state->fb);
38f3ce3a 13866
7580d774
ML
13867 old_intel_state = to_intel_plane_state(old_state);
13868
1ee49399 13869 if (!obj && !old_obj)
38f3ce3a
MR
13870 return;
13871
1ee49399
ML
13872 if (old_obj && (plane->type != DRM_PLANE_TYPE_CURSOR ||
13873 !INTEL_INFO(dev)->cursor_needs_physical))
3465c580 13874 intel_unpin_fb_obj(old_state->fb, old_state->rotation);
1ee49399
ML
13875
13876 /* prepare_fb aborted? */
13877 if ((old_obj && (old_obj->frontbuffer_bits & intel_plane->frontbuffer_bit)) ||
13878 (obj && !(obj->frontbuffer_bits & intel_plane->frontbuffer_bit)))
13879 i915_gem_track_fb(old_obj, obj, intel_plane->frontbuffer_bit);
7580d774
ML
13880
13881 i915_gem_request_assign(&old_intel_state->wait_req, NULL);
465c120c
MR
13882}
13883
6156a456
CK
13884int
13885skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
13886{
13887 int max_scale;
13888 struct drm_device *dev;
13889 struct drm_i915_private *dev_priv;
13890 int crtc_clock, cdclk;
13891
bf8a0af0 13892 if (!intel_crtc || !crtc_state->base.enable)
6156a456
CK
13893 return DRM_PLANE_HELPER_NO_SCALING;
13894
13895 dev = intel_crtc->base.dev;
13896 dev_priv = dev->dev_private;
13897 crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
27c329ed 13898 cdclk = to_intel_atomic_state(crtc_state->base.state)->cdclk;
6156a456 13899
54bf1ce6 13900 if (WARN_ON_ONCE(!crtc_clock || cdclk < crtc_clock))
6156a456
CK
13901 return DRM_PLANE_HELPER_NO_SCALING;
13902
13903 /*
13904 * skl max scale is lower of:
13905 * close to 3 but not 3, -1 is for that purpose
13906 * or
13907 * cdclk/crtc_clock
13908 */
13909 max_scale = min((1 << 16) * 3 - 1, (1 << 8) * ((cdclk << 8) / crtc_clock));
13910
13911 return max_scale;
13912}
13913
465c120c 13914static int
3c692a41 13915intel_check_primary_plane(struct drm_plane *plane,
061e4b8d 13916 struct intel_crtc_state *crtc_state,
3c692a41
GP
13917 struct intel_plane_state *state)
13918{
2b875c22
MR
13919 struct drm_crtc *crtc = state->base.crtc;
13920 struct drm_framebuffer *fb = state->base.fb;
6156a456 13921 int min_scale = DRM_PLANE_HELPER_NO_SCALING;
061e4b8d
ML
13922 int max_scale = DRM_PLANE_HELPER_NO_SCALING;
13923 bool can_position = false;
465c120c 13924
693bdc28
VS
13925 if (INTEL_INFO(plane->dev)->gen >= 9) {
13926 /* use scaler when colorkey is not required */
13927 if (state->ckey.flags == I915_SET_COLORKEY_NONE) {
13928 min_scale = 1;
13929 max_scale = skl_max_scale(to_intel_crtc(crtc), crtc_state);
13930 }
d8106366 13931 can_position = true;
6156a456 13932 }
d8106366 13933
061e4b8d
ML
13934 return drm_plane_helper_check_update(plane, crtc, fb, &state->src,
13935 &state->dst, &state->clip,
da20eabd
ML
13936 min_scale, max_scale,
13937 can_position, true,
13938 &state->visible);
14af293f
GP
13939}
13940
613d2b27
ML
13941static void intel_begin_crtc_commit(struct drm_crtc *crtc,
13942 struct drm_crtc_state *old_crtc_state)
3c692a41 13943{
32b7eeec 13944 struct drm_device *dev = crtc->dev;
3c692a41 13945 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
bfd16b2a
ML
13946 struct intel_crtc_state *old_intel_state =
13947 to_intel_crtc_state(old_crtc_state);
13948 bool modeset = needs_modeset(crtc->state);
3c692a41 13949
c34c9ee4 13950 /* Perform vblank evasion around commit operation */
62852622 13951 intel_pipe_update_start(intel_crtc);
0583236e 13952
bfd16b2a
ML
13953 if (modeset)
13954 return;
13955
20a34e78
ML
13956 if (crtc->state->color_mgmt_changed || to_intel_crtc_state(crtc->state)->update_pipe) {
13957 intel_color_set_csc(crtc->state);
13958 intel_color_load_luts(crtc->state);
13959 }
13960
bfd16b2a
ML
13961 if (to_intel_crtc_state(crtc->state)->update_pipe)
13962 intel_update_pipe_config(intel_crtc, old_intel_state);
13963 else if (INTEL_INFO(dev)->gen >= 9)
0583236e 13964 skl_detach_scalers(intel_crtc);
32b7eeec
MR
13965}
13966
613d2b27
ML
13967static void intel_finish_crtc_commit(struct drm_crtc *crtc,
13968 struct drm_crtc_state *old_crtc_state)
32b7eeec 13969{
32b7eeec 13970 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
32b7eeec 13971
62852622 13972 intel_pipe_update_end(intel_crtc);
3c692a41
GP
13973}
13974
cf4c7c12 13975/**
4a3b8769
MR
13976 * intel_plane_destroy - destroy a plane
13977 * @plane: plane to destroy
cf4c7c12 13978 *
4a3b8769
MR
13979 * Common destruction function for all types of planes (primary, cursor,
13980 * sprite).
cf4c7c12 13981 */
4a3b8769 13982void intel_plane_destroy(struct drm_plane *plane)
465c120c
MR
13983{
13984 struct intel_plane *intel_plane = to_intel_plane(plane);
13985 drm_plane_cleanup(plane);
13986 kfree(intel_plane);
13987}
13988
65a3fea0 13989const struct drm_plane_funcs intel_plane_funcs = {
70a101f8
MR
13990 .update_plane = drm_atomic_helper_update_plane,
13991 .disable_plane = drm_atomic_helper_disable_plane,
3d7d6510 13992 .destroy = intel_plane_destroy,
c196e1d6 13993 .set_property = drm_atomic_helper_plane_set_property,
a98b3431
MR
13994 .atomic_get_property = intel_plane_atomic_get_property,
13995 .atomic_set_property = intel_plane_atomic_set_property,
ea2c67bb
MR
13996 .atomic_duplicate_state = intel_plane_duplicate_state,
13997 .atomic_destroy_state = intel_plane_destroy_state,
13998
465c120c
MR
13999};
14000
14001static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
14002 int pipe)
14003{
fca0ce2a
VS
14004 struct intel_plane *primary = NULL;
14005 struct intel_plane_state *state = NULL;
465c120c 14006 const uint32_t *intel_primary_formats;
45e3743a 14007 unsigned int num_formats;
fca0ce2a 14008 int ret;
465c120c
MR
14009
14010 primary = kzalloc(sizeof(*primary), GFP_KERNEL);
fca0ce2a
VS
14011 if (!primary)
14012 goto fail;
465c120c 14013
8e7d688b 14014 state = intel_create_plane_state(&primary->base);
fca0ce2a
VS
14015 if (!state)
14016 goto fail;
8e7d688b 14017 primary->base.state = &state->base;
ea2c67bb 14018
465c120c
MR
14019 primary->can_scale = false;
14020 primary->max_downscale = 1;
6156a456
CK
14021 if (INTEL_INFO(dev)->gen >= 9) {
14022 primary->can_scale = true;
af99ceda 14023 state->scaler_id = -1;
6156a456 14024 }
465c120c
MR
14025 primary->pipe = pipe;
14026 primary->plane = pipe;
a9ff8714 14027 primary->frontbuffer_bit = INTEL_FRONTBUFFER_PRIMARY(pipe);
c59cb179 14028 primary->check_plane = intel_check_primary_plane;
465c120c
MR
14029 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
14030 primary->plane = !pipe;
14031
6c0fd451
DL
14032 if (INTEL_INFO(dev)->gen >= 9) {
14033 intel_primary_formats = skl_primary_formats;
14034 num_formats = ARRAY_SIZE(skl_primary_formats);
a8d201af
ML
14035
14036 primary->update_plane = skylake_update_primary_plane;
14037 primary->disable_plane = skylake_disable_primary_plane;
14038 } else if (HAS_PCH_SPLIT(dev)) {
14039 intel_primary_formats = i965_primary_formats;
14040 num_formats = ARRAY_SIZE(i965_primary_formats);
14041
14042 primary->update_plane = ironlake_update_primary_plane;
14043 primary->disable_plane = i9xx_disable_primary_plane;
6c0fd451 14044 } else if (INTEL_INFO(dev)->gen >= 4) {
568db4f2
DL
14045 intel_primary_formats = i965_primary_formats;
14046 num_formats = ARRAY_SIZE(i965_primary_formats);
a8d201af
ML
14047
14048 primary->update_plane = i9xx_update_primary_plane;
14049 primary->disable_plane = i9xx_disable_primary_plane;
6c0fd451
DL
14050 } else {
14051 intel_primary_formats = i8xx_primary_formats;
14052 num_formats = ARRAY_SIZE(i8xx_primary_formats);
a8d201af
ML
14053
14054 primary->update_plane = i9xx_update_primary_plane;
14055 primary->disable_plane = i9xx_disable_primary_plane;
465c120c
MR
14056 }
14057
fca0ce2a
VS
14058 ret = drm_universal_plane_init(dev, &primary->base, 0,
14059 &intel_plane_funcs,
14060 intel_primary_formats, num_formats,
14061 DRM_PLANE_TYPE_PRIMARY, NULL);
14062 if (ret)
14063 goto fail;
48404c1e 14064
3b7a5119
SJ
14065 if (INTEL_INFO(dev)->gen >= 4)
14066 intel_create_rotation_property(dev, primary);
48404c1e 14067
ea2c67bb
MR
14068 drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
14069
465c120c 14070 return &primary->base;
fca0ce2a
VS
14071
14072fail:
14073 kfree(state);
14074 kfree(primary);
14075
14076 return NULL;
465c120c
MR
14077}
14078
3b7a5119
SJ
14079void intel_create_rotation_property(struct drm_device *dev, struct intel_plane *plane)
14080{
14081 if (!dev->mode_config.rotation_property) {
14082 unsigned long flags = BIT(DRM_ROTATE_0) |
14083 BIT(DRM_ROTATE_180);
14084
14085 if (INTEL_INFO(dev)->gen >= 9)
14086 flags |= BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_270);
14087
14088 dev->mode_config.rotation_property =
14089 drm_mode_create_rotation_property(dev, flags);
14090 }
14091 if (dev->mode_config.rotation_property)
14092 drm_object_attach_property(&plane->base.base,
14093 dev->mode_config.rotation_property,
14094 plane->base.state->rotation);
14095}
14096
3d7d6510 14097static int
852e787c 14098intel_check_cursor_plane(struct drm_plane *plane,
061e4b8d 14099 struct intel_crtc_state *crtc_state,
852e787c 14100 struct intel_plane_state *state)
3d7d6510 14101{
061e4b8d 14102 struct drm_crtc *crtc = crtc_state->base.crtc;
2b875c22 14103 struct drm_framebuffer *fb = state->base.fb;
757f9a3e 14104 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
b29ec92c 14105 enum pipe pipe = to_intel_plane(plane)->pipe;
757f9a3e
GP
14106 unsigned stride;
14107 int ret;
3d7d6510 14108
061e4b8d
ML
14109 ret = drm_plane_helper_check_update(plane, crtc, fb, &state->src,
14110 &state->dst, &state->clip,
3d7d6510
MR
14111 DRM_PLANE_HELPER_NO_SCALING,
14112 DRM_PLANE_HELPER_NO_SCALING,
852e787c 14113 true, true, &state->visible);
757f9a3e
GP
14114 if (ret)
14115 return ret;
14116
757f9a3e
GP
14117 /* if we want to turn off the cursor ignore width and height */
14118 if (!obj)
da20eabd 14119 return 0;
757f9a3e 14120
757f9a3e 14121 /* Check for which cursor types we support */
061e4b8d 14122 if (!cursor_size_ok(plane->dev, state->base.crtc_w, state->base.crtc_h)) {
ea2c67bb
MR
14123 DRM_DEBUG("Cursor dimension %dx%d not supported\n",
14124 state->base.crtc_w, state->base.crtc_h);
757f9a3e
GP
14125 return -EINVAL;
14126 }
14127
ea2c67bb
MR
14128 stride = roundup_pow_of_two(state->base.crtc_w) * 4;
14129 if (obj->base.size < stride * state->base.crtc_h) {
757f9a3e
GP
14130 DRM_DEBUG_KMS("buffer is too small\n");
14131 return -ENOMEM;
14132 }
14133
3a656b54 14134 if (fb->modifier[0] != DRM_FORMAT_MOD_NONE) {
757f9a3e 14135 DRM_DEBUG_KMS("cursor cannot be tiled\n");
da20eabd 14136 return -EINVAL;
32b7eeec
MR
14137 }
14138
b29ec92c
VS
14139 /*
14140 * There's something wrong with the cursor on CHV pipe C.
14141 * If it straddles the left edge of the screen then
14142 * moving it away from the edge or disabling it often
14143 * results in a pipe underrun, and often that can lead to
14144 * dead pipe (constant underrun reported, and it scans
14145 * out just a solid color). To recover from that, the
14146 * display power well must be turned off and on again.
14147 * Refuse the put the cursor into that compromised position.
14148 */
14149 if (IS_CHERRYVIEW(plane->dev) && pipe == PIPE_C &&
14150 state->visible && state->base.crtc_x < 0) {
14151 DRM_DEBUG_KMS("CHV cursor C not allowed to straddle the left screen edge\n");
14152 return -EINVAL;
14153 }
14154
da20eabd 14155 return 0;
852e787c 14156}
3d7d6510 14157
a8ad0d8e
ML
14158static void
14159intel_disable_cursor_plane(struct drm_plane *plane,
7fabf5ef 14160 struct drm_crtc *crtc)
a8ad0d8e 14161{
f2858021
ML
14162 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
14163
14164 intel_crtc->cursor_addr = 0;
55a08b3f 14165 intel_crtc_update_cursor(crtc, NULL);
a8ad0d8e
ML
14166}
14167
f4a2cf29 14168static void
55a08b3f
ML
14169intel_update_cursor_plane(struct drm_plane *plane,
14170 const struct intel_crtc_state *crtc_state,
14171 const struct intel_plane_state *state)
852e787c 14172{
55a08b3f
ML
14173 struct drm_crtc *crtc = crtc_state->base.crtc;
14174 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ea2c67bb 14175 struct drm_device *dev = plane->dev;
2b875c22 14176 struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
a912f12f 14177 uint32_t addr;
852e787c 14178
f4a2cf29 14179 if (!obj)
a912f12f 14180 addr = 0;
f4a2cf29 14181 else if (!INTEL_INFO(dev)->cursor_needs_physical)
a912f12f 14182 addr = i915_gem_obj_ggtt_offset(obj);
f4a2cf29 14183 else
a912f12f 14184 addr = obj->phys_handle->busaddr;
852e787c 14185
a912f12f 14186 intel_crtc->cursor_addr = addr;
55a08b3f 14187 intel_crtc_update_cursor(crtc, state);
852e787c
GP
14188}
14189
3d7d6510
MR
14190static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
14191 int pipe)
14192{
fca0ce2a
VS
14193 struct intel_plane *cursor = NULL;
14194 struct intel_plane_state *state = NULL;
14195 int ret;
3d7d6510
MR
14196
14197 cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
fca0ce2a
VS
14198 if (!cursor)
14199 goto fail;
3d7d6510 14200
8e7d688b 14201 state = intel_create_plane_state(&cursor->base);
fca0ce2a
VS
14202 if (!state)
14203 goto fail;
8e7d688b 14204 cursor->base.state = &state->base;
ea2c67bb 14205
3d7d6510
MR
14206 cursor->can_scale = false;
14207 cursor->max_downscale = 1;
14208 cursor->pipe = pipe;
14209 cursor->plane = pipe;
a9ff8714 14210 cursor->frontbuffer_bit = INTEL_FRONTBUFFER_CURSOR(pipe);
c59cb179 14211 cursor->check_plane = intel_check_cursor_plane;
55a08b3f 14212 cursor->update_plane = intel_update_cursor_plane;
a8ad0d8e 14213 cursor->disable_plane = intel_disable_cursor_plane;
3d7d6510 14214
fca0ce2a
VS
14215 ret = drm_universal_plane_init(dev, &cursor->base, 0,
14216 &intel_plane_funcs,
14217 intel_cursor_formats,
14218 ARRAY_SIZE(intel_cursor_formats),
14219 DRM_PLANE_TYPE_CURSOR, NULL);
14220 if (ret)
14221 goto fail;
4398ad45
VS
14222
14223 if (INTEL_INFO(dev)->gen >= 4) {
14224 if (!dev->mode_config.rotation_property)
14225 dev->mode_config.rotation_property =
14226 drm_mode_create_rotation_property(dev,
14227 BIT(DRM_ROTATE_0) |
14228 BIT(DRM_ROTATE_180));
14229 if (dev->mode_config.rotation_property)
14230 drm_object_attach_property(&cursor->base.base,
14231 dev->mode_config.rotation_property,
8e7d688b 14232 state->base.rotation);
4398ad45
VS
14233 }
14234
af99ceda
CK
14235 if (INTEL_INFO(dev)->gen >=9)
14236 state->scaler_id = -1;
14237
ea2c67bb
MR
14238 drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
14239
3d7d6510 14240 return &cursor->base;
fca0ce2a
VS
14241
14242fail:
14243 kfree(state);
14244 kfree(cursor);
14245
14246 return NULL;
3d7d6510
MR
14247}
14248
549e2bfb
CK
14249static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
14250 struct intel_crtc_state *crtc_state)
14251{
14252 int i;
14253 struct intel_scaler *intel_scaler;
14254 struct intel_crtc_scaler_state *scaler_state = &crtc_state->scaler_state;
14255
14256 for (i = 0; i < intel_crtc->num_scalers; i++) {
14257 intel_scaler = &scaler_state->scalers[i];
14258 intel_scaler->in_use = 0;
549e2bfb
CK
14259 intel_scaler->mode = PS_SCALER_MODE_DYN;
14260 }
14261
14262 scaler_state->scaler_id = -1;
14263}
14264
b358d0a6 14265static void intel_crtc_init(struct drm_device *dev, int pipe)
79e53945 14266{
fbee40df 14267 struct drm_i915_private *dev_priv = dev->dev_private;
79e53945 14268 struct intel_crtc *intel_crtc;
f5de6e07 14269 struct intel_crtc_state *crtc_state = NULL;
3d7d6510
MR
14270 struct drm_plane *primary = NULL;
14271 struct drm_plane *cursor = NULL;
8563b1e8 14272 int ret;
79e53945 14273
955382f3 14274 intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
79e53945
JB
14275 if (intel_crtc == NULL)
14276 return;
14277
f5de6e07
ACO
14278 crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
14279 if (!crtc_state)
14280 goto fail;
550acefd
ACO
14281 intel_crtc->config = crtc_state;
14282 intel_crtc->base.state = &crtc_state->base;
07878248 14283 crtc_state->base.crtc = &intel_crtc->base;
f5de6e07 14284
549e2bfb
CK
14285 /* initialize shared scalers */
14286 if (INTEL_INFO(dev)->gen >= 9) {
14287 if (pipe == PIPE_C)
14288 intel_crtc->num_scalers = 1;
14289 else
14290 intel_crtc->num_scalers = SKL_NUM_SCALERS;
14291
14292 skl_init_scalers(dev, intel_crtc, crtc_state);
14293 }
14294
465c120c 14295 primary = intel_primary_plane_create(dev, pipe);
3d7d6510
MR
14296 if (!primary)
14297 goto fail;
14298
14299 cursor = intel_cursor_plane_create(dev, pipe);
14300 if (!cursor)
14301 goto fail;
14302
465c120c 14303 ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
f9882876 14304 cursor, &intel_crtc_funcs, NULL);
3d7d6510
MR
14305 if (ret)
14306 goto fail;
79e53945 14307
1f1c2e24
VS
14308 /*
14309 * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
8c0f92e1 14310 * is hooked to pipe B. Hence we want plane A feeding pipe B.
1f1c2e24 14311 */
80824003
JB
14312 intel_crtc->pipe = pipe;
14313 intel_crtc->plane = pipe;
3a77c4c4 14314 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
28c97730 14315 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
e2e767ab 14316 intel_crtc->plane = !pipe;
80824003
JB
14317 }
14318
4b0e333e
CW
14319 intel_crtc->cursor_base = ~0;
14320 intel_crtc->cursor_cntl = ~0;
dc41c154 14321 intel_crtc->cursor_size = ~0;
8d7849db 14322
852eb00d
VS
14323 intel_crtc->wm.cxsr_allowed = true;
14324
22fd0fab
JB
14325 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
14326 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
14327 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
14328 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
14329
79e53945 14330 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
87b6b101 14331
8563b1e8
LL
14332 intel_color_init(&intel_crtc->base);
14333
87b6b101 14334 WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
3d7d6510
MR
14335 return;
14336
14337fail:
14338 if (primary)
14339 drm_plane_cleanup(primary);
14340 if (cursor)
14341 drm_plane_cleanup(cursor);
f5de6e07 14342 kfree(crtc_state);
3d7d6510 14343 kfree(intel_crtc);
79e53945
JB
14344}
14345
752aa88a
JB
14346enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
14347{
14348 struct drm_encoder *encoder = connector->base.encoder;
6e9f798d 14349 struct drm_device *dev = connector->base.dev;
752aa88a 14350
51fd371b 14351 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
752aa88a 14352
d3babd3f 14353 if (!encoder || WARN_ON(!encoder->crtc))
752aa88a
JB
14354 return INVALID_PIPE;
14355
14356 return to_intel_crtc(encoder->crtc)->pipe;
14357}
14358
08d7b3d1 14359int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
05394f39 14360 struct drm_file *file)
08d7b3d1 14361{
08d7b3d1 14362 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
7707e653 14363 struct drm_crtc *drmmode_crtc;
c05422d5 14364 struct intel_crtc *crtc;
08d7b3d1 14365
7707e653 14366 drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
08d7b3d1 14367
7707e653 14368 if (!drmmode_crtc) {
08d7b3d1 14369 DRM_ERROR("no such CRTC id\n");
3f2c2057 14370 return -ENOENT;
08d7b3d1
CW
14371 }
14372
7707e653 14373 crtc = to_intel_crtc(drmmode_crtc);
c05422d5 14374 pipe_from_crtc_id->pipe = crtc->pipe;
08d7b3d1 14375
c05422d5 14376 return 0;
08d7b3d1
CW
14377}
14378
66a9278e 14379static int intel_encoder_clones(struct intel_encoder *encoder)
79e53945 14380{
66a9278e
DV
14381 struct drm_device *dev = encoder->base.dev;
14382 struct intel_encoder *source_encoder;
79e53945 14383 int index_mask = 0;
79e53945
JB
14384 int entry = 0;
14385
b2784e15 14386 for_each_intel_encoder(dev, source_encoder) {
bc079e8b 14387 if (encoders_cloneable(encoder, source_encoder))
66a9278e
DV
14388 index_mask |= (1 << entry);
14389
79e53945
JB
14390 entry++;
14391 }
4ef69c7a 14392
79e53945
JB
14393 return index_mask;
14394}
14395
4d302442
CW
14396static bool has_edp_a(struct drm_device *dev)
14397{
14398 struct drm_i915_private *dev_priv = dev->dev_private;
14399
14400 if (!IS_MOBILE(dev))
14401 return false;
14402
14403 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
14404 return false;
14405
e3589908 14406 if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
4d302442
CW
14407 return false;
14408
14409 return true;
14410}
14411
84b4e042
JB
14412static bool intel_crt_present(struct drm_device *dev)
14413{
14414 struct drm_i915_private *dev_priv = dev->dev_private;
14415
884497ed
DL
14416 if (INTEL_INFO(dev)->gen >= 9)
14417 return false;
14418
cf404ce4 14419 if (IS_HSW_ULT(dev) || IS_BDW_ULT(dev))
84b4e042
JB
14420 return false;
14421
14422 if (IS_CHERRYVIEW(dev))
14423 return false;
14424
65e472e4
VS
14425 if (HAS_PCH_LPT_H(dev) && I915_READ(SFUSE_STRAP) & SFUSE_STRAP_CRT_DISABLED)
14426 return false;
14427
70ac54d0
VS
14428 /* DDI E can't be used if DDI A requires 4 lanes */
14429 if (HAS_DDI(dev) && I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_A_4_LANES)
14430 return false;
14431
e4abb733 14432 if (!dev_priv->vbt.int_crt_support)
84b4e042
JB
14433 return false;
14434
14435 return true;
14436}
14437
79e53945
JB
14438static void intel_setup_outputs(struct drm_device *dev)
14439{
725e30ad 14440 struct drm_i915_private *dev_priv = dev->dev_private;
4ef69c7a 14441 struct intel_encoder *encoder;
cb0953d7 14442 bool dpd_is_edp = false;
79e53945 14443
c9093354 14444 intel_lvds_init(dev);
79e53945 14445
84b4e042 14446 if (intel_crt_present(dev))
79935fca 14447 intel_crt_init(dev);
cb0953d7 14448
c776eb2e
VK
14449 if (IS_BROXTON(dev)) {
14450 /*
14451 * FIXME: Broxton doesn't support port detection via the
14452 * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
14453 * detect the ports.
14454 */
14455 intel_ddi_init(dev, PORT_A);
14456 intel_ddi_init(dev, PORT_B);
14457 intel_ddi_init(dev, PORT_C);
c6c794a2
SS
14458
14459 intel_dsi_init(dev);
c776eb2e 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 (INTEL_INFO(dev_priv)->gen >= 9) {
bc8d7dff 14885 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
5724dbd1
DL
14886 dev_priv->display.get_initial_plane_config =
14887 skylake_get_initial_plane_config;
bc8d7dff
DL
14888 dev_priv->display.crtc_compute_clock =
14889 haswell_crtc_compute_clock;
14890 dev_priv->display.crtc_enable = haswell_crtc_enable;
14891 dev_priv->display.crtc_disable = haswell_crtc_disable;
88212941 14892 } else if (HAS_DDI(dev_priv)) {
0e8ffe1b 14893 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
5724dbd1
DL
14894 dev_priv->display.get_initial_plane_config =
14895 ironlake_get_initial_plane_config;
797d0259
ACO
14896 dev_priv->display.crtc_compute_clock =
14897 haswell_crtc_compute_clock;
4f771f10
PZ
14898 dev_priv->display.crtc_enable = haswell_crtc_enable;
14899 dev_priv->display.crtc_disable = haswell_crtc_disable;
88212941 14900 } else if (HAS_PCH_SPLIT(dev_priv)) {
0e8ffe1b 14901 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
5724dbd1
DL
14902 dev_priv->display.get_initial_plane_config =
14903 ironlake_get_initial_plane_config;
3fb37703
ACO
14904 dev_priv->display.crtc_compute_clock =
14905 ironlake_crtc_compute_clock;
76e5a89c
DV
14906 dev_priv->display.crtc_enable = ironlake_crtc_enable;
14907 dev_priv->display.crtc_disable = ironlake_crtc_disable;
65b3d6a9 14908 } else if (IS_CHERRYVIEW(dev_priv)) {
89b667f8 14909 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
5724dbd1
DL
14910 dev_priv->display.get_initial_plane_config =
14911 i9xx_get_initial_plane_config;
65b3d6a9
ACO
14912 dev_priv->display.crtc_compute_clock = chv_crtc_compute_clock;
14913 dev_priv->display.crtc_enable = valleyview_crtc_enable;
14914 dev_priv->display.crtc_disable = i9xx_crtc_disable;
14915 } else if (IS_VALLEYVIEW(dev_priv)) {
14916 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
14917 dev_priv->display.get_initial_plane_config =
14918 i9xx_get_initial_plane_config;
14919 dev_priv->display.crtc_compute_clock = vlv_crtc_compute_clock;
89b667f8
JB
14920 dev_priv->display.crtc_enable = valleyview_crtc_enable;
14921 dev_priv->display.crtc_disable = i9xx_crtc_disable;
19ec6693
ACO
14922 } else if (IS_G4X(dev_priv)) {
14923 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
14924 dev_priv->display.get_initial_plane_config =
14925 i9xx_get_initial_plane_config;
14926 dev_priv->display.crtc_compute_clock = g4x_crtc_compute_clock;
14927 dev_priv->display.crtc_enable = i9xx_crtc_enable;
14928 dev_priv->display.crtc_disable = i9xx_crtc_disable;
70e8aa21
ACO
14929 } else if (IS_PINEVIEW(dev_priv)) {
14930 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
14931 dev_priv->display.get_initial_plane_config =
14932 i9xx_get_initial_plane_config;
14933 dev_priv->display.crtc_compute_clock = pnv_crtc_compute_clock;
14934 dev_priv->display.crtc_enable = i9xx_crtc_enable;
14935 dev_priv->display.crtc_disable = i9xx_crtc_disable;
81c97f52 14936 } else if (!IS_GEN2(dev_priv)) {
0e8ffe1b 14937 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
5724dbd1
DL
14938 dev_priv->display.get_initial_plane_config =
14939 i9xx_get_initial_plane_config;
d6dfee7a 14940 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
76e5a89c
DV
14941 dev_priv->display.crtc_enable = i9xx_crtc_enable;
14942 dev_priv->display.crtc_disable = i9xx_crtc_disable;
81c97f52
ACO
14943 } else {
14944 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
14945 dev_priv->display.get_initial_plane_config =
14946 i9xx_get_initial_plane_config;
14947 dev_priv->display.crtc_compute_clock = i8xx_crtc_compute_clock;
14948 dev_priv->display.crtc_enable = i9xx_crtc_enable;
14949 dev_priv->display.crtc_disable = i9xx_crtc_disable;
f564048e 14950 }
e70236a8 14951
e70236a8 14952 /* Returns the core display clock speed */
88212941 14953 if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv))
1652d19e
VS
14954 dev_priv->display.get_display_clock_speed =
14955 skylake_get_display_clock_speed;
88212941 14956 else if (IS_BROXTON(dev_priv))
acd3f3d3
BP
14957 dev_priv->display.get_display_clock_speed =
14958 broxton_get_display_clock_speed;
88212941 14959 else if (IS_BROADWELL(dev_priv))
1652d19e
VS
14960 dev_priv->display.get_display_clock_speed =
14961 broadwell_get_display_clock_speed;
88212941 14962 else if (IS_HASWELL(dev_priv))
1652d19e
VS
14963 dev_priv->display.get_display_clock_speed =
14964 haswell_get_display_clock_speed;
88212941 14965 else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
25eb05fc
JB
14966 dev_priv->display.get_display_clock_speed =
14967 valleyview_get_display_clock_speed;
88212941 14968 else if (IS_GEN5(dev_priv))
b37a6434
VS
14969 dev_priv->display.get_display_clock_speed =
14970 ilk_get_display_clock_speed;
88212941
ID
14971 else if (IS_I945G(dev_priv) || IS_BROADWATER(dev_priv) ||
14972 IS_GEN6(dev_priv) || IS_IVYBRIDGE(dev_priv))
e70236a8
JB
14973 dev_priv->display.get_display_clock_speed =
14974 i945_get_display_clock_speed;
88212941 14975 else if (IS_GM45(dev_priv))
34edce2f
VS
14976 dev_priv->display.get_display_clock_speed =
14977 gm45_get_display_clock_speed;
88212941 14978 else if (IS_CRESTLINE(dev_priv))
34edce2f
VS
14979 dev_priv->display.get_display_clock_speed =
14980 i965gm_get_display_clock_speed;
88212941 14981 else if (IS_PINEVIEW(dev_priv))
34edce2f
VS
14982 dev_priv->display.get_display_clock_speed =
14983 pnv_get_display_clock_speed;
88212941 14984 else if (IS_G33(dev_priv) || IS_G4X(dev_priv))
34edce2f
VS
14985 dev_priv->display.get_display_clock_speed =
14986 g33_get_display_clock_speed;
88212941 14987 else if (IS_I915G(dev_priv))
e70236a8
JB
14988 dev_priv->display.get_display_clock_speed =
14989 i915_get_display_clock_speed;
88212941 14990 else if (IS_I945GM(dev_priv) || IS_845G(dev_priv))
e70236a8
JB
14991 dev_priv->display.get_display_clock_speed =
14992 i9xx_misc_get_display_clock_speed;
88212941 14993 else if (IS_I915GM(dev_priv))
e70236a8
JB
14994 dev_priv->display.get_display_clock_speed =
14995 i915gm_get_display_clock_speed;
88212941 14996 else if (IS_I865G(dev_priv))
e70236a8
JB
14997 dev_priv->display.get_display_clock_speed =
14998 i865_get_display_clock_speed;
88212941 14999 else if (IS_I85X(dev_priv))
e70236a8 15000 dev_priv->display.get_display_clock_speed =
1b1d2716 15001 i85x_get_display_clock_speed;
623e01e5 15002 else { /* 830 */
88212941 15003 WARN(!IS_I830(dev_priv), "Unknown platform. Assuming 133 MHz CDCLK\n");
e70236a8
JB
15004 dev_priv->display.get_display_clock_speed =
15005 i830_get_display_clock_speed;
623e01e5 15006 }
e70236a8 15007
88212941 15008 if (IS_GEN5(dev_priv)) {
3bb11b53 15009 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
88212941 15010 } else if (IS_GEN6(dev_priv)) {
3bb11b53 15011 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
88212941 15012 } else if (IS_IVYBRIDGE(dev_priv)) {
3bb11b53
SJ
15013 /* FIXME: detect B0+ stepping and use auto training */
15014 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
88212941 15015 } else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
3bb11b53 15016 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
88212941 15017 if (IS_BROADWELL(dev_priv)) {
27c329ed
ML
15018 dev_priv->display.modeset_commit_cdclk =
15019 broadwell_modeset_commit_cdclk;
15020 dev_priv->display.modeset_calc_cdclk =
15021 broadwell_modeset_calc_cdclk;
15022 }
88212941 15023 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
27c329ed
ML
15024 dev_priv->display.modeset_commit_cdclk =
15025 valleyview_modeset_commit_cdclk;
15026 dev_priv->display.modeset_calc_cdclk =
15027 valleyview_modeset_calc_cdclk;
88212941 15028 } else if (IS_BROXTON(dev_priv)) {
27c329ed
ML
15029 dev_priv->display.modeset_commit_cdclk =
15030 broxton_modeset_commit_cdclk;
15031 dev_priv->display.modeset_calc_cdclk =
15032 broxton_modeset_calc_cdclk;
e70236a8 15033 }
8c9f3aaf 15034
88212941 15035 switch (INTEL_INFO(dev_priv)->gen) {
8c9f3aaf
JB
15036 case 2:
15037 dev_priv->display.queue_flip = intel_gen2_queue_flip;
15038 break;
15039
15040 case 3:
15041 dev_priv->display.queue_flip = intel_gen3_queue_flip;
15042 break;
15043
15044 case 4:
15045 case 5:
15046 dev_priv->display.queue_flip = intel_gen4_queue_flip;
15047 break;
15048
15049 case 6:
15050 dev_priv->display.queue_flip = intel_gen6_queue_flip;
15051 break;
7c9017e5 15052 case 7:
4e0bbc31 15053 case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
7c9017e5
JB
15054 dev_priv->display.queue_flip = intel_gen7_queue_flip;
15055 break;
830c81db 15056 case 9:
ba343e02
TU
15057 /* Drop through - unsupported since execlist only. */
15058 default:
15059 /* Default just returns -ENODEV to indicate unsupported */
15060 dev_priv->display.queue_flip = intel_default_queue_flip;
8c9f3aaf 15061 }
e70236a8
JB
15062}
15063
b690e96c
JB
15064/*
15065 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
15066 * resume, or other times. This quirk makes sure that's the case for
15067 * affected systems.
15068 */
0206e353 15069static void quirk_pipea_force(struct drm_device *dev)
b690e96c
JB
15070{
15071 struct drm_i915_private *dev_priv = dev->dev_private;
15072
15073 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
bc0daf48 15074 DRM_INFO("applying pipe a force quirk\n");
b690e96c
JB
15075}
15076
b6b5d049
VS
15077static void quirk_pipeb_force(struct drm_device *dev)
15078{
15079 struct drm_i915_private *dev_priv = dev->dev_private;
15080
15081 dev_priv->quirks |= QUIRK_PIPEB_FORCE;
15082 DRM_INFO("applying pipe b force quirk\n");
15083}
15084
435793df
KP
15085/*
15086 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
15087 */
15088static void quirk_ssc_force_disable(struct drm_device *dev)
15089{
15090 struct drm_i915_private *dev_priv = dev->dev_private;
15091 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
bc0daf48 15092 DRM_INFO("applying lvds SSC disable quirk\n");
435793df
KP
15093}
15094
4dca20ef 15095/*
5a15ab5b
CE
15096 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
15097 * brightness value
4dca20ef
CE
15098 */
15099static void quirk_invert_brightness(struct drm_device *dev)
15100{
15101 struct drm_i915_private *dev_priv = dev->dev_private;
15102 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
bc0daf48 15103 DRM_INFO("applying inverted panel brightness quirk\n");
435793df
KP
15104}
15105
9c72cc6f
SD
15106/* Some VBT's incorrectly indicate no backlight is present */
15107static void quirk_backlight_present(struct drm_device *dev)
15108{
15109 struct drm_i915_private *dev_priv = dev->dev_private;
15110 dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
15111 DRM_INFO("applying backlight present quirk\n");
15112}
15113
b690e96c
JB
15114struct intel_quirk {
15115 int device;
15116 int subsystem_vendor;
15117 int subsystem_device;
15118 void (*hook)(struct drm_device *dev);
15119};
15120
5f85f176
EE
15121/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
15122struct intel_dmi_quirk {
15123 void (*hook)(struct drm_device *dev);
15124 const struct dmi_system_id (*dmi_id_list)[];
15125};
15126
15127static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
15128{
15129 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
15130 return 1;
15131}
15132
15133static const struct intel_dmi_quirk intel_dmi_quirks[] = {
15134 {
15135 .dmi_id_list = &(const struct dmi_system_id[]) {
15136 {
15137 .callback = intel_dmi_reverse_brightness,
15138 .ident = "NCR Corporation",
15139 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
15140 DMI_MATCH(DMI_PRODUCT_NAME, ""),
15141 },
15142 },
15143 { } /* terminating entry */
15144 },
15145 .hook = quirk_invert_brightness,
15146 },
15147};
15148
c43b5634 15149static struct intel_quirk intel_quirks[] = {
b690e96c
JB
15150 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
15151 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
15152
b690e96c
JB
15153 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
15154 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
15155
5f080c0f
VS
15156 /* 830 needs to leave pipe A & dpll A up */
15157 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
15158
b6b5d049
VS
15159 /* 830 needs to leave pipe B & dpll B up */
15160 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
15161
435793df
KP
15162 /* Lenovo U160 cannot use SSC on LVDS */
15163 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
070d329a
MAS
15164
15165 /* Sony Vaio Y cannot use SSC on LVDS */
15166 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
5a15ab5b 15167
be505f64
AH
15168 /* Acer Aspire 5734Z must invert backlight brightness */
15169 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
15170
15171 /* Acer/eMachines G725 */
15172 { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
15173
15174 /* Acer/eMachines e725 */
15175 { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
15176
15177 /* Acer/Packard Bell NCL20 */
15178 { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
15179
15180 /* Acer Aspire 4736Z */
15181 { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
0f540c3a
JN
15182
15183 /* Acer Aspire 5336 */
15184 { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
2e93a1aa
SD
15185
15186 /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
15187 { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
d4967d8c 15188
dfb3d47b
SD
15189 /* Acer C720 Chromebook (Core i3 4005U) */
15190 { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
15191
b2a9601c 15192 /* Apple Macbook 2,1 (Core 2 T7400) */
15193 { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
15194
1b9448b0
JN
15195 /* Apple Macbook 4,1 */
15196 { 0x2a02, 0x106b, 0x00a1, quirk_backlight_present },
15197
d4967d8c
SD
15198 /* Toshiba CB35 Chromebook (Celeron 2955U) */
15199 { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
724cb06f
SD
15200
15201 /* HP Chromebook 14 (Celeron 2955U) */
15202 { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
cf6f0af9
JN
15203
15204 /* Dell Chromebook 11 */
15205 { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
9be64eee
JN
15206
15207 /* Dell Chromebook 11 (2015 version) */
15208 { 0x0a16, 0x1028, 0x0a35, quirk_backlight_present },
b690e96c
JB
15209};
15210
15211static void intel_init_quirks(struct drm_device *dev)
15212{
15213 struct pci_dev *d = dev->pdev;
15214 int i;
15215
15216 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
15217 struct intel_quirk *q = &intel_quirks[i];
15218
15219 if (d->device == q->device &&
15220 (d->subsystem_vendor == q->subsystem_vendor ||
15221 q->subsystem_vendor == PCI_ANY_ID) &&
15222 (d->subsystem_device == q->subsystem_device ||
15223 q->subsystem_device == PCI_ANY_ID))
15224 q->hook(dev);
15225 }
5f85f176
EE
15226 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
15227 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
15228 intel_dmi_quirks[i].hook(dev);
15229 }
b690e96c
JB
15230}
15231
9cce37f4
JB
15232/* Disable the VGA plane that we never use */
15233static void i915_disable_vga(struct drm_device *dev)
15234{
15235 struct drm_i915_private *dev_priv = dev->dev_private;
15236 u8 sr1;
f0f59a00 15237 i915_reg_t vga_reg = i915_vgacntrl_reg(dev);
9cce37f4 15238
2b37c616 15239 /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
9cce37f4 15240 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
3fdcf431 15241 outb(SR01, VGA_SR_INDEX);
9cce37f4
JB
15242 sr1 = inb(VGA_SR_DATA);
15243 outb(sr1 | 1<<5, VGA_SR_DATA);
15244 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
15245 udelay(300);
15246
01f5a626 15247 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
9cce37f4
JB
15248 POSTING_READ(vga_reg);
15249}
15250
f817586c
DV
15251void intel_modeset_init_hw(struct drm_device *dev)
15252{
1a617b77
ML
15253 struct drm_i915_private *dev_priv = dev->dev_private;
15254
b6283055 15255 intel_update_cdclk(dev);
1a617b77
ML
15256
15257 dev_priv->atomic_cdclk_freq = dev_priv->cdclk_freq;
15258
f817586c 15259 intel_init_clock_gating(dev);
8090c6b9 15260 intel_enable_gt_powersave(dev);
f817586c
DV
15261}
15262
d93c0372
MR
15263/*
15264 * Calculate what we think the watermarks should be for the state we've read
15265 * out of the hardware and then immediately program those watermarks so that
15266 * we ensure the hardware settings match our internal state.
15267 *
15268 * We can calculate what we think WM's should be by creating a duplicate of the
15269 * current state (which was constructed during hardware readout) and running it
15270 * through the atomic check code to calculate new watermark values in the
15271 * state object.
15272 */
15273static void sanitize_watermarks(struct drm_device *dev)
15274{
15275 struct drm_i915_private *dev_priv = to_i915(dev);
15276 struct drm_atomic_state *state;
15277 struct drm_crtc *crtc;
15278 struct drm_crtc_state *cstate;
15279 struct drm_modeset_acquire_ctx ctx;
15280 int ret;
15281 int i;
15282
15283 /* Only supported on platforms that use atomic watermark design */
ed4a6a7c 15284 if (!dev_priv->display.optimize_watermarks)
d93c0372
MR
15285 return;
15286
15287 /*
15288 * We need to hold connection_mutex before calling duplicate_state so
15289 * that the connector loop is protected.
15290 */
15291 drm_modeset_acquire_init(&ctx, 0);
15292retry:
0cd1262d 15293 ret = drm_modeset_lock_all_ctx(dev, &ctx);
d93c0372
MR
15294 if (ret == -EDEADLK) {
15295 drm_modeset_backoff(&ctx);
15296 goto retry;
15297 } else if (WARN_ON(ret)) {
0cd1262d 15298 goto fail;
d93c0372
MR
15299 }
15300
15301 state = drm_atomic_helper_duplicate_state(dev, &ctx);
15302 if (WARN_ON(IS_ERR(state)))
0cd1262d 15303 goto fail;
d93c0372 15304
ed4a6a7c
MR
15305 /*
15306 * Hardware readout is the only time we don't want to calculate
15307 * intermediate watermarks (since we don't trust the current
15308 * watermarks).
15309 */
15310 to_intel_atomic_state(state)->skip_intermediate_wm = true;
15311
d93c0372
MR
15312 ret = intel_atomic_check(dev, state);
15313 if (ret) {
15314 /*
15315 * If we fail here, it means that the hardware appears to be
15316 * programmed in a way that shouldn't be possible, given our
15317 * understanding of watermark requirements. This might mean a
15318 * mistake in the hardware readout code or a mistake in the
15319 * watermark calculations for a given platform. Raise a WARN
15320 * so that this is noticeable.
15321 *
15322 * If this actually happens, we'll have to just leave the
15323 * BIOS-programmed watermarks untouched and hope for the best.
15324 */
15325 WARN(true, "Could not determine valid watermarks for inherited state\n");
0cd1262d 15326 goto fail;
d93c0372
MR
15327 }
15328
15329 /* Write calculated watermark values back */
15330 to_i915(dev)->wm.config = to_intel_atomic_state(state)->wm_config;
15331 for_each_crtc_in_state(state, crtc, cstate, i) {
15332 struct intel_crtc_state *cs = to_intel_crtc_state(cstate);
15333
ed4a6a7c
MR
15334 cs->wm.need_postvbl_update = true;
15335 dev_priv->display.optimize_watermarks(cs);
d93c0372
MR
15336 }
15337
15338 drm_atomic_state_free(state);
0cd1262d 15339fail:
d93c0372
MR
15340 drm_modeset_drop_locks(&ctx);
15341 drm_modeset_acquire_fini(&ctx);
15342}
15343
79e53945
JB
15344void intel_modeset_init(struct drm_device *dev)
15345{
72e96d64
JL
15346 struct drm_i915_private *dev_priv = to_i915(dev);
15347 struct i915_ggtt *ggtt = &dev_priv->ggtt;
1fe47785 15348 int sprite, ret;
8cc87b75 15349 enum pipe pipe;
46f297fb 15350 struct intel_crtc *crtc;
79e53945
JB
15351
15352 drm_mode_config_init(dev);
15353
15354 dev->mode_config.min_width = 0;
15355 dev->mode_config.min_height = 0;
15356
019d96cb
DA
15357 dev->mode_config.preferred_depth = 24;
15358 dev->mode_config.prefer_shadow = 1;
15359
25bab385
TU
15360 dev->mode_config.allow_fb_modifiers = true;
15361
e6ecefaa 15362 dev->mode_config.funcs = &intel_mode_funcs;
79e53945 15363
b690e96c
JB
15364 intel_init_quirks(dev);
15365
1fa61106
ED
15366 intel_init_pm(dev);
15367
e3c74757
BW
15368 if (INTEL_INFO(dev)->num_pipes == 0)
15369 return;
15370
69f92f67
LW
15371 /*
15372 * There may be no VBT; and if the BIOS enabled SSC we can
15373 * just keep using it to avoid unnecessary flicker. Whereas if the
15374 * BIOS isn't using it, don't assume it will work even if the VBT
15375 * indicates as much.
15376 */
15377 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)) {
15378 bool bios_lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
15379 DREF_SSC1_ENABLE);
15380
15381 if (dev_priv->vbt.lvds_use_ssc != bios_lvds_use_ssc) {
15382 DRM_DEBUG_KMS("SSC %sabled by BIOS, overriding VBT which says %sabled\n",
15383 bios_lvds_use_ssc ? "en" : "dis",
15384 dev_priv->vbt.lvds_use_ssc ? "en" : "dis");
15385 dev_priv->vbt.lvds_use_ssc = bios_lvds_use_ssc;
15386 }
15387 }
15388
a6c45cf0
CW
15389 if (IS_GEN2(dev)) {
15390 dev->mode_config.max_width = 2048;
15391 dev->mode_config.max_height = 2048;
15392 } else if (IS_GEN3(dev)) {
5e4d6fa7
KP
15393 dev->mode_config.max_width = 4096;
15394 dev->mode_config.max_height = 4096;
79e53945 15395 } else {
a6c45cf0
CW
15396 dev->mode_config.max_width = 8192;
15397 dev->mode_config.max_height = 8192;
79e53945 15398 }
068be561 15399
dc41c154
VS
15400 if (IS_845G(dev) || IS_I865G(dev)) {
15401 dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
15402 dev->mode_config.cursor_height = 1023;
15403 } else if (IS_GEN2(dev)) {
068be561
DL
15404 dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
15405 dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
15406 } else {
15407 dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
15408 dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
15409 }
15410
72e96d64 15411 dev->mode_config.fb_base = ggtt->mappable_base;
79e53945 15412
28c97730 15413 DRM_DEBUG_KMS("%d display pipe%s available.\n",
7eb552ae
BW
15414 INTEL_INFO(dev)->num_pipes,
15415 INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
79e53945 15416
055e393f 15417 for_each_pipe(dev_priv, pipe) {
8cc87b75 15418 intel_crtc_init(dev, pipe);
3bdcfc0c 15419 for_each_sprite(dev_priv, pipe, sprite) {
1fe47785 15420 ret = intel_plane_init(dev, pipe, sprite);
7f1f3851 15421 if (ret)
06da8da2 15422 DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
1fe47785 15423 pipe_name(pipe), sprite_name(pipe, sprite), ret);
7f1f3851 15424 }
79e53945
JB
15425 }
15426
bfa7df01 15427 intel_update_czclk(dev_priv);
e7dc33f3 15428 intel_update_rawclk(dev_priv);
bfa7df01
VS
15429 intel_update_cdclk(dev);
15430
e72f9fbf 15431 intel_shared_dpll_init(dev);
ee7b9f93 15432
9cce37f4
JB
15433 /* Just disable it once at startup */
15434 i915_disable_vga(dev);
79e53945 15435 intel_setup_outputs(dev);
11be49eb 15436
6e9f798d 15437 drm_modeset_lock_all(dev);
043e9bda 15438 intel_modeset_setup_hw_state(dev);
6e9f798d 15439 drm_modeset_unlock_all(dev);
46f297fb 15440
d3fcc808 15441 for_each_intel_crtc(dev, crtc) {
eeebeac5
ML
15442 struct intel_initial_plane_config plane_config = {};
15443
46f297fb
JB
15444 if (!crtc->active)
15445 continue;
15446
46f297fb 15447 /*
46f297fb
JB
15448 * Note that reserving the BIOS fb up front prevents us
15449 * from stuffing other stolen allocations like the ring
15450 * on top. This prevents some ugliness at boot time, and
15451 * can even allow for smooth boot transitions if the BIOS
15452 * fb is large enough for the active pipe configuration.
15453 */
eeebeac5
ML
15454 dev_priv->display.get_initial_plane_config(crtc,
15455 &plane_config);
15456
15457 /*
15458 * If the fb is shared between multiple heads, we'll
15459 * just get the first one.
15460 */
15461 intel_find_initial_plane_obj(crtc, &plane_config);
46f297fb 15462 }
d93c0372
MR
15463
15464 /*
15465 * Make sure hardware watermarks really match the state we read out.
15466 * Note that we need to do this after reconstructing the BIOS fb's
15467 * since the watermark calculation done here will use pstate->fb.
15468 */
15469 sanitize_watermarks(dev);
2c7111db
CW
15470}
15471
7fad798e
DV
15472static void intel_enable_pipe_a(struct drm_device *dev)
15473{
15474 struct intel_connector *connector;
15475 struct drm_connector *crt = NULL;
15476 struct intel_load_detect_pipe load_detect_temp;
208bf9fd 15477 struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
7fad798e
DV
15478
15479 /* We can't just switch on the pipe A, we need to set things up with a
15480 * proper mode and output configuration. As a gross hack, enable pipe A
15481 * by enabling the load detect pipe once. */
3a3371ff 15482 for_each_intel_connector(dev, connector) {
7fad798e
DV
15483 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
15484 crt = &connector->base;
15485 break;
15486 }
15487 }
15488
15489 if (!crt)
15490 return;
15491
208bf9fd 15492 if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
49172fee 15493 intel_release_load_detect_pipe(crt, &load_detect_temp, ctx);
7fad798e
DV
15494}
15495
fa555837
DV
15496static bool
15497intel_check_plane_mapping(struct intel_crtc *crtc)
15498{
7eb552ae
BW
15499 struct drm_device *dev = crtc->base.dev;
15500 struct drm_i915_private *dev_priv = dev->dev_private;
649636ef 15501 u32 val;
fa555837 15502
7eb552ae 15503 if (INTEL_INFO(dev)->num_pipes == 1)
fa555837
DV
15504 return true;
15505
649636ef 15506 val = I915_READ(DSPCNTR(!crtc->plane));
fa555837
DV
15507
15508 if ((val & DISPLAY_PLANE_ENABLE) &&
15509 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
15510 return false;
15511
15512 return true;
15513}
15514
02e93c35
VS
15515static bool intel_crtc_has_encoders(struct intel_crtc *crtc)
15516{
15517 struct drm_device *dev = crtc->base.dev;
15518 struct intel_encoder *encoder;
15519
15520 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
15521 return true;
15522
15523 return false;
15524}
15525
dd756198
VS
15526static bool intel_encoder_has_connectors(struct intel_encoder *encoder)
15527{
15528 struct drm_device *dev = encoder->base.dev;
15529 struct intel_connector *connector;
15530
15531 for_each_connector_on_encoder(dev, &encoder->base, connector)
15532 return true;
15533
15534 return false;
15535}
15536
24929352
DV
15537static void intel_sanitize_crtc(struct intel_crtc *crtc)
15538{
15539 struct drm_device *dev = crtc->base.dev;
15540 struct drm_i915_private *dev_priv = dev->dev_private;
4d1de975 15541 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
24929352 15542
24929352 15543 /* Clear any frame start delays used for debugging left by the BIOS */
4d1de975
JN
15544 if (!transcoder_is_dsi(cpu_transcoder)) {
15545 i915_reg_t reg = PIPECONF(cpu_transcoder);
15546
15547 I915_WRITE(reg,
15548 I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
15549 }
24929352 15550
d3eaf884 15551 /* restore vblank interrupts to correct state */
9625604c 15552 drm_crtc_vblank_reset(&crtc->base);
d297e103 15553 if (crtc->active) {
f9cd7b88
VS
15554 struct intel_plane *plane;
15555
9625604c 15556 drm_crtc_vblank_on(&crtc->base);
f9cd7b88
VS
15557
15558 /* Disable everything but the primary plane */
15559 for_each_intel_plane_on_crtc(dev, crtc, plane) {
15560 if (plane->base.type == DRM_PLANE_TYPE_PRIMARY)
15561 continue;
15562
15563 plane->disable_plane(&plane->base, &crtc->base);
15564 }
9625604c 15565 }
d3eaf884 15566
24929352 15567 /* We need to sanitize the plane -> pipe mapping first because this will
fa555837
DV
15568 * disable the crtc (and hence change the state) if it is wrong. Note
15569 * that gen4+ has a fixed plane -> pipe mapping. */
15570 if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
24929352
DV
15571 bool plane;
15572
24929352
DV
15573 DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
15574 crtc->base.base.id);
15575
15576 /* Pipe has the wrong plane attached and the plane is active.
15577 * Temporarily change the plane mapping and disable everything
15578 * ... */
15579 plane = crtc->plane;
b70709a6 15580 to_intel_plane_state(crtc->base.primary->state)->visible = true;
24929352 15581 crtc->plane = !plane;
b17d48e2 15582 intel_crtc_disable_noatomic(&crtc->base);
24929352 15583 crtc->plane = plane;
24929352 15584 }
24929352 15585
7fad798e
DV
15586 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
15587 crtc->pipe == PIPE_A && !crtc->active) {
15588 /* BIOS forgot to enable pipe A, this mostly happens after
15589 * resume. Force-enable the pipe to fix this, the update_dpms
15590 * call below we restore the pipe to the right state, but leave
15591 * the required bits on. */
15592 intel_enable_pipe_a(dev);
15593 }
15594
24929352
DV
15595 /* Adjust the state of the output pipe according to whether we
15596 * have active connectors/encoders. */
842e0307 15597 if (crtc->active && !intel_crtc_has_encoders(crtc))
b17d48e2 15598 intel_crtc_disable_noatomic(&crtc->base);
24929352 15599
a3ed6aad 15600 if (crtc->active || HAS_GMCH_DISPLAY(dev)) {
4cc31489
DV
15601 /*
15602 * We start out with underrun reporting disabled to avoid races.
15603 * For correct bookkeeping mark this on active crtcs.
15604 *
c5ab3bc0
DV
15605 * Also on gmch platforms we dont have any hardware bits to
15606 * disable the underrun reporting. Which means we need to start
15607 * out with underrun reporting disabled also on inactive pipes,
15608 * since otherwise we'll complain about the garbage we read when
15609 * e.g. coming up after runtime pm.
15610 *
4cc31489
DV
15611 * No protection against concurrent access is required - at
15612 * worst a fifo underrun happens which also sets this to false.
15613 */
15614 crtc->cpu_fifo_underrun_disabled = true;
15615 crtc->pch_fifo_underrun_disabled = true;
15616 }
24929352
DV
15617}
15618
15619static void intel_sanitize_encoder(struct intel_encoder *encoder)
15620{
15621 struct intel_connector *connector;
15622 struct drm_device *dev = encoder->base.dev;
15623
15624 /* We need to check both for a crtc link (meaning that the
15625 * encoder is active and trying to read from a pipe) and the
15626 * pipe itself being active. */
15627 bool has_active_crtc = encoder->base.crtc &&
15628 to_intel_crtc(encoder->base.crtc)->active;
15629
dd756198 15630 if (intel_encoder_has_connectors(encoder) && !has_active_crtc) {
24929352
DV
15631 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
15632 encoder->base.base.id,
8e329a03 15633 encoder->base.name);
24929352
DV
15634
15635 /* Connector is active, but has no active pipe. This is
15636 * fallout from our resume register restoring. Disable
15637 * the encoder manually again. */
15638 if (encoder->base.crtc) {
15639 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
15640 encoder->base.base.id,
8e329a03 15641 encoder->base.name);
24929352 15642 encoder->disable(encoder);
a62d1497
VS
15643 if (encoder->post_disable)
15644 encoder->post_disable(encoder);
24929352 15645 }
7f1950fb 15646 encoder->base.crtc = NULL;
24929352
DV
15647
15648 /* Inconsistent output/port/pipe state happens presumably due to
15649 * a bug in one of the get_hw_state functions. Or someplace else
15650 * in our code, like the register restore mess on resume. Clamp
15651 * things to off as a safer default. */
3a3371ff 15652 for_each_intel_connector(dev, connector) {
24929352
DV
15653 if (connector->encoder != encoder)
15654 continue;
7f1950fb
EE
15655 connector->base.dpms = DRM_MODE_DPMS_OFF;
15656 connector->base.encoder = NULL;
24929352
DV
15657 }
15658 }
15659 /* Enabled encoders without active connectors will be fixed in
15660 * the crtc fixup. */
15661}
15662
04098753 15663void i915_redisable_vga_power_on(struct drm_device *dev)
0fde901f
KM
15664{
15665 struct drm_i915_private *dev_priv = dev->dev_private;
f0f59a00 15666 i915_reg_t vga_reg = i915_vgacntrl_reg(dev);
0fde901f 15667
04098753
ID
15668 if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
15669 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
15670 i915_disable_vga(dev);
15671 }
15672}
15673
15674void i915_redisable_vga(struct drm_device *dev)
15675{
15676 struct drm_i915_private *dev_priv = dev->dev_private;
15677
8dc8a27c
PZ
15678 /* This function can be called both from intel_modeset_setup_hw_state or
15679 * at a very early point in our resume sequence, where the power well
15680 * structures are not yet restored. Since this function is at a very
15681 * paranoid "someone might have enabled VGA while we were not looking"
15682 * level, just check if the power well is enabled instead of trying to
15683 * follow the "don't touch the power well if we don't need it" policy
15684 * the rest of the driver uses. */
6392f847 15685 if (!intel_display_power_get_if_enabled(dev_priv, POWER_DOMAIN_VGA))
8dc8a27c
PZ
15686 return;
15687
04098753 15688 i915_redisable_vga_power_on(dev);
6392f847
ID
15689
15690 intel_display_power_put(dev_priv, POWER_DOMAIN_VGA);
0fde901f
KM
15691}
15692
f9cd7b88 15693static bool primary_get_hw_state(struct intel_plane *plane)
98ec7739 15694{
f9cd7b88 15695 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
98ec7739 15696
f9cd7b88 15697 return I915_READ(DSPCNTR(plane->plane)) & DISPLAY_PLANE_ENABLE;
d032ffa0
ML
15698}
15699
f9cd7b88
VS
15700/* FIXME read out full plane state for all planes */
15701static void readout_plane_state(struct intel_crtc *crtc)
d032ffa0 15702{
b26d3ea3 15703 struct drm_plane *primary = crtc->base.primary;
f9cd7b88 15704 struct intel_plane_state *plane_state =
b26d3ea3 15705 to_intel_plane_state(primary->state);
d032ffa0 15706
19b8d387 15707 plane_state->visible = crtc->active &&
b26d3ea3
ML
15708 primary_get_hw_state(to_intel_plane(primary));
15709
15710 if (plane_state->visible)
15711 crtc->base.state->plane_mask |= 1 << drm_plane_index(primary);
98ec7739
VS
15712}
15713
30e984df 15714static void intel_modeset_readout_hw_state(struct drm_device *dev)
24929352
DV
15715{
15716 struct drm_i915_private *dev_priv = dev->dev_private;
15717 enum pipe pipe;
24929352
DV
15718 struct intel_crtc *crtc;
15719 struct intel_encoder *encoder;
15720 struct intel_connector *connector;
5358901f 15721 int i;
24929352 15722
565602d7
ML
15723 dev_priv->active_crtcs = 0;
15724
d3fcc808 15725 for_each_intel_crtc(dev, crtc) {
565602d7
ML
15726 struct intel_crtc_state *crtc_state = crtc->config;
15727 int pixclk = 0;
3b117c8f 15728
565602d7
ML
15729 __drm_atomic_helper_crtc_destroy_state(&crtc->base, &crtc_state->base);
15730 memset(crtc_state, 0, sizeof(*crtc_state));
15731 crtc_state->base.crtc = &crtc->base;
24929352 15732
565602d7
ML
15733 crtc_state->base.active = crtc_state->base.enable =
15734 dev_priv->display.get_pipe_config(crtc, crtc_state);
15735
15736 crtc->base.enabled = crtc_state->base.enable;
15737 crtc->active = crtc_state->base.active;
15738
15739 if (crtc_state->base.active) {
15740 dev_priv->active_crtcs |= 1 << crtc->pipe;
15741
15742 if (IS_BROADWELL(dev_priv)) {
15743 pixclk = ilk_pipe_pixel_rate(crtc_state);
15744
15745 /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
15746 if (crtc_state->ips_enabled)
15747 pixclk = DIV_ROUND_UP(pixclk * 100, 95);
15748 } else if (IS_VALLEYVIEW(dev_priv) ||
15749 IS_CHERRYVIEW(dev_priv) ||
15750 IS_BROXTON(dev_priv))
15751 pixclk = crtc_state->base.adjusted_mode.crtc_clock;
15752 else
15753 WARN_ON(dev_priv->display.modeset_calc_cdclk);
15754 }
15755
15756 dev_priv->min_pixclk[crtc->pipe] = pixclk;
b70709a6 15757
f9cd7b88 15758 readout_plane_state(crtc);
24929352
DV
15759
15760 DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
15761 crtc->base.base.id,
15762 crtc->active ? "enabled" : "disabled");
15763 }
15764
5358901f
DV
15765 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
15766 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
15767
2edd6443
ACO
15768 pll->on = pll->funcs.get_hw_state(dev_priv, pll,
15769 &pll->config.hw_state);
3e369b76 15770 pll->config.crtc_mask = 0;
d3fcc808 15771 for_each_intel_crtc(dev, crtc) {
2dd66ebd 15772 if (crtc->active && crtc->config->shared_dpll == pll)
3e369b76 15773 pll->config.crtc_mask |= 1 << crtc->pipe;
5358901f 15774 }
2dd66ebd 15775 pll->active_mask = pll->config.crtc_mask;
5358901f 15776
1e6f2ddc 15777 DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
3e369b76 15778 pll->name, pll->config.crtc_mask, pll->on);
5358901f
DV
15779 }
15780
b2784e15 15781 for_each_intel_encoder(dev, encoder) {
24929352
DV
15782 pipe = 0;
15783
15784 if (encoder->get_hw_state(encoder, &pipe)) {
045ac3b5
JB
15785 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
15786 encoder->base.crtc = &crtc->base;
6e3c9717 15787 encoder->get_config(encoder, crtc->config);
24929352
DV
15788 } else {
15789 encoder->base.crtc = NULL;
15790 }
15791
6f2bcceb 15792 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
24929352 15793 encoder->base.base.id,
8e329a03 15794 encoder->base.name,
24929352 15795 encoder->base.crtc ? "enabled" : "disabled",
6f2bcceb 15796 pipe_name(pipe));
24929352
DV
15797 }
15798
3a3371ff 15799 for_each_intel_connector(dev, connector) {
24929352
DV
15800 if (connector->get_hw_state(connector)) {
15801 connector->base.dpms = DRM_MODE_DPMS_ON;
2aa974c9
ML
15802
15803 encoder = connector->encoder;
15804 connector->base.encoder = &encoder->base;
15805
15806 if (encoder->base.crtc &&
15807 encoder->base.crtc->state->active) {
15808 /*
15809 * This has to be done during hardware readout
15810 * because anything calling .crtc_disable may
15811 * rely on the connector_mask being accurate.
15812 */
15813 encoder->base.crtc->state->connector_mask |=
15814 1 << drm_connector_index(&connector->base);
e87a52b3
ML
15815 encoder->base.crtc->state->encoder_mask |=
15816 1 << drm_encoder_index(&encoder->base);
2aa974c9
ML
15817 }
15818
24929352
DV
15819 } else {
15820 connector->base.dpms = DRM_MODE_DPMS_OFF;
15821 connector->base.encoder = NULL;
15822 }
15823 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
15824 connector->base.base.id,
c23cc417 15825 connector->base.name,
24929352
DV
15826 connector->base.encoder ? "enabled" : "disabled");
15827 }
7f4c6284
VS
15828
15829 for_each_intel_crtc(dev, crtc) {
15830 crtc->base.hwmode = crtc->config->base.adjusted_mode;
15831
15832 memset(&crtc->base.mode, 0, sizeof(crtc->base.mode));
15833 if (crtc->base.state->active) {
15834 intel_mode_from_pipe_config(&crtc->base.mode, crtc->config);
15835 intel_mode_from_pipe_config(&crtc->base.state->adjusted_mode, crtc->config);
15836 WARN_ON(drm_atomic_set_mode_for_crtc(crtc->base.state, &crtc->base.mode));
15837
15838 /*
15839 * The initial mode needs to be set in order to keep
15840 * the atomic core happy. It wants a valid mode if the
15841 * crtc's enabled, so we do the above call.
15842 *
15843 * At this point some state updated by the connectors
15844 * in their ->detect() callback has not run yet, so
15845 * no recalculation can be done yet.
15846 *
15847 * Even if we could do a recalculation and modeset
15848 * right now it would cause a double modeset if
15849 * fbdev or userspace chooses a different initial mode.
15850 *
15851 * If that happens, someone indicated they wanted a
15852 * mode change, which means it's safe to do a full
15853 * recalculation.
15854 */
15855 crtc->base.state->mode.private_flags = I915_MODE_FLAG_INHERITED;
9eca6832
VS
15856
15857 drm_calc_timestamping_constants(&crtc->base, &crtc->base.hwmode);
15858 update_scanline_offset(crtc);
7f4c6284 15859 }
e3b247da
VS
15860
15861 intel_pipe_config_sanity_check(dev_priv, crtc->config);
7f4c6284 15862 }
30e984df
DV
15863}
15864
043e9bda
ML
15865/* Scan out the current hw modeset state,
15866 * and sanitizes it to the current state
15867 */
15868static void
15869intel_modeset_setup_hw_state(struct drm_device *dev)
30e984df
DV
15870{
15871 struct drm_i915_private *dev_priv = dev->dev_private;
15872 enum pipe pipe;
30e984df
DV
15873 struct intel_crtc *crtc;
15874 struct intel_encoder *encoder;
35c95375 15875 int i;
30e984df
DV
15876
15877 intel_modeset_readout_hw_state(dev);
24929352
DV
15878
15879 /* HW state is read out, now we need to sanitize this mess. */
b2784e15 15880 for_each_intel_encoder(dev, encoder) {
24929352
DV
15881 intel_sanitize_encoder(encoder);
15882 }
15883
055e393f 15884 for_each_pipe(dev_priv, pipe) {
24929352
DV
15885 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
15886 intel_sanitize_crtc(crtc);
6e3c9717
ACO
15887 intel_dump_pipe_config(crtc, crtc->config,
15888 "[setup_hw_state]");
24929352 15889 }
9a935856 15890
d29b2f9d
ACO
15891 intel_modeset_update_connector_atomic_state(dev);
15892
35c95375
DV
15893 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
15894 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
15895
2dd66ebd 15896 if (!pll->on || pll->active_mask)
35c95375
DV
15897 continue;
15898
15899 DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
15900
2edd6443 15901 pll->funcs.disable(dev_priv, pll);
35c95375
DV
15902 pll->on = false;
15903 }
15904
666a4537 15905 if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev))
6eb1a681
VS
15906 vlv_wm_get_hw_state(dev);
15907 else if (IS_GEN9(dev))
3078999f
PB
15908 skl_wm_get_hw_state(dev);
15909 else if (HAS_PCH_SPLIT(dev))
243e6a44 15910 ilk_wm_get_hw_state(dev);
292b990e
ML
15911
15912 for_each_intel_crtc(dev, crtc) {
15913 unsigned long put_domains;
15914
74bff5f9 15915 put_domains = modeset_get_crtc_power_domains(&crtc->base, crtc->config);
292b990e
ML
15916 if (WARN_ON(put_domains))
15917 modeset_put_power_domains(dev_priv, put_domains);
15918 }
15919 intel_display_set_init_power(dev_priv, false);
010cf73d
PZ
15920
15921 intel_fbc_init_pipe_state(dev_priv);
043e9bda 15922}
7d0bc1ea 15923
043e9bda
ML
15924void intel_display_resume(struct drm_device *dev)
15925{
e2c8b870
ML
15926 struct drm_i915_private *dev_priv = to_i915(dev);
15927 struct drm_atomic_state *state = dev_priv->modeset_restore_state;
15928 struct drm_modeset_acquire_ctx ctx;
043e9bda 15929 int ret;
e2c8b870 15930 bool setup = false;
f30da187 15931
e2c8b870 15932 dev_priv->modeset_restore_state = NULL;
043e9bda 15933
ea49c9ac
ML
15934 /*
15935 * This is a cludge because with real atomic modeset mode_config.mutex
15936 * won't be taken. Unfortunately some probed state like
15937 * audio_codec_enable is still protected by mode_config.mutex, so lock
15938 * it here for now.
15939 */
15940 mutex_lock(&dev->mode_config.mutex);
e2c8b870 15941 drm_modeset_acquire_init(&ctx, 0);
043e9bda 15942
e2c8b870
ML
15943retry:
15944 ret = drm_modeset_lock_all_ctx(dev, &ctx);
043e9bda 15945
e2c8b870
ML
15946 if (ret == 0 && !setup) {
15947 setup = true;
043e9bda 15948
e2c8b870
ML
15949 intel_modeset_setup_hw_state(dev);
15950 i915_redisable_vga(dev);
45e2b5f6 15951 }
8af6cf88 15952
e2c8b870
ML
15953 if (ret == 0 && state) {
15954 struct drm_crtc_state *crtc_state;
15955 struct drm_crtc *crtc;
15956 int i;
043e9bda 15957
e2c8b870
ML
15958 state->acquire_ctx = &ctx;
15959
15960 for_each_crtc_in_state(state, crtc, crtc_state, i) {
15961 /*
15962 * Force recalculation even if we restore
15963 * current state. With fast modeset this may not result
15964 * in a modeset when the state is compatible.
15965 */
15966 crtc_state->mode_changed = true;
15967 }
15968
15969 ret = drm_atomic_commit(state);
043e9bda
ML
15970 }
15971
e2c8b870
ML
15972 if (ret == -EDEADLK) {
15973 drm_modeset_backoff(&ctx);
15974 goto retry;
15975 }
043e9bda 15976
e2c8b870
ML
15977 drm_modeset_drop_locks(&ctx);
15978 drm_modeset_acquire_fini(&ctx);
ea49c9ac 15979 mutex_unlock(&dev->mode_config.mutex);
043e9bda 15980
e2c8b870
ML
15981 if (ret) {
15982 DRM_ERROR("Restoring old state failed with %i\n", ret);
15983 drm_atomic_state_free(state);
15984 }
2c7111db
CW
15985}
15986
15987void intel_modeset_gem_init(struct drm_device *dev)
15988{
484b41dd 15989 struct drm_crtc *c;
2ff8fde1 15990 struct drm_i915_gem_object *obj;
e0d6149b 15991 int ret;
484b41dd 15992
ae48434c 15993 intel_init_gt_powersave(dev);
ae48434c 15994
1833b134 15995 intel_modeset_init_hw(dev);
02e792fb
DV
15996
15997 intel_setup_overlay(dev);
484b41dd
JB
15998
15999 /*
16000 * Make sure any fbs we allocated at startup are properly
16001 * pinned & fenced. When we do the allocation it's too early
16002 * for this.
16003 */
70e1e0ec 16004 for_each_crtc(dev, c) {
2ff8fde1
MR
16005 obj = intel_fb_obj(c->primary->fb);
16006 if (obj == NULL)
484b41dd
JB
16007 continue;
16008
e0d6149b 16009 mutex_lock(&dev->struct_mutex);
3465c580
VS
16010 ret = intel_pin_and_fence_fb_obj(c->primary->fb,
16011 c->primary->state->rotation);
e0d6149b
TU
16012 mutex_unlock(&dev->struct_mutex);
16013 if (ret) {
484b41dd
JB
16014 DRM_ERROR("failed to pin boot fb on pipe %d\n",
16015 to_intel_crtc(c)->pipe);
66e514c1
DA
16016 drm_framebuffer_unreference(c->primary->fb);
16017 c->primary->fb = NULL;
36750f28 16018 c->primary->crtc = c->primary->state->crtc = NULL;
afd65eb4 16019 update_state_fb(c->primary);
36750f28 16020 c->state->plane_mask &= ~(1 << drm_plane_index(c->primary));
484b41dd
JB
16021 }
16022 }
0962c3c9
VS
16023
16024 intel_backlight_register(dev);
79e53945
JB
16025}
16026
4932e2c3
ID
16027void intel_connector_unregister(struct intel_connector *intel_connector)
16028{
16029 struct drm_connector *connector = &intel_connector->base;
16030
16031 intel_panel_destroy_backlight(connector);
34ea3d38 16032 drm_connector_unregister(connector);
4932e2c3
ID
16033}
16034
79e53945
JB
16035void intel_modeset_cleanup(struct drm_device *dev)
16036{
652c393a 16037 struct drm_i915_private *dev_priv = dev->dev_private;
19c8054c 16038 struct intel_connector *connector;
652c393a 16039
2eb5252e
ID
16040 intel_disable_gt_powersave(dev);
16041
0962c3c9
VS
16042 intel_backlight_unregister(dev);
16043
fd0c0642
DV
16044 /*
16045 * Interrupts and polling as the first thing to avoid creating havoc.
2eb5252e 16046 * Too much stuff here (turning of connectors, ...) would
fd0c0642
DV
16047 * experience fancy races otherwise.
16048 */
2aeb7d3a 16049 intel_irq_uninstall(dev_priv);
eb21b92b 16050
fd0c0642
DV
16051 /*
16052 * Due to the hpd irq storm handling the hotplug work can re-arm the
16053 * poll handlers. Hence disable polling after hpd handling is shut down.
16054 */
f87ea761 16055 drm_kms_helper_poll_fini(dev);
fd0c0642 16056
723bfd70
JB
16057 intel_unregister_dsm_handler();
16058
c937ab3e 16059 intel_fbc_global_disable(dev_priv);
69341a5e 16060
1630fe75
CW
16061 /* flush any delayed tasks or pending work */
16062 flush_scheduled_work();
16063
db31af1d 16064 /* destroy the backlight and sysfs files before encoders/connectors */
19c8054c
JN
16065 for_each_intel_connector(dev, connector)
16066 connector->unregister(connector);
d9255d57 16067
79e53945 16068 drm_mode_config_cleanup(dev);
4d7bb011
DV
16069
16070 intel_cleanup_overlay(dev);
ae48434c 16071
ae48434c 16072 intel_cleanup_gt_powersave(dev);
f5949141
DV
16073
16074 intel_teardown_gmbus(dev);
79e53945
JB
16075}
16076
f1c79df3
ZW
16077/*
16078 * Return which encoder is currently attached for connector.
16079 */
df0e9248 16080struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
79e53945 16081{
df0e9248
CW
16082 return &intel_attached_encoder(connector)->base;
16083}
f1c79df3 16084
df0e9248
CW
16085void intel_connector_attach_encoder(struct intel_connector *connector,
16086 struct intel_encoder *encoder)
16087{
16088 connector->encoder = encoder;
16089 drm_mode_connector_attach_encoder(&connector->base,
16090 &encoder->base);
79e53945 16091}
28d52043
DA
16092
16093/*
16094 * set vga decode state - true == enable VGA decode
16095 */
16096int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
16097{
16098 struct drm_i915_private *dev_priv = dev->dev_private;
a885b3cc 16099 unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
28d52043
DA
16100 u16 gmch_ctrl;
16101
75fa041d
CW
16102 if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
16103 DRM_ERROR("failed to read control word\n");
16104 return -EIO;
16105 }
16106
c0cc8a55
CW
16107 if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
16108 return 0;
16109
28d52043
DA
16110 if (state)
16111 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
16112 else
16113 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
75fa041d
CW
16114
16115 if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
16116 DRM_ERROR("failed to write control word\n");
16117 return -EIO;
16118 }
16119
28d52043
DA
16120 return 0;
16121}
c4a1d9e4 16122
c4a1d9e4 16123struct intel_display_error_state {
ff57f1b0
PZ
16124
16125 u32 power_well_driver;
16126
63b66e5b
CW
16127 int num_transcoders;
16128
c4a1d9e4
CW
16129 struct intel_cursor_error_state {
16130 u32 control;
16131 u32 position;
16132 u32 base;
16133 u32 size;
52331309 16134 } cursor[I915_MAX_PIPES];
c4a1d9e4
CW
16135
16136 struct intel_pipe_error_state {
ddf9c536 16137 bool power_domain_on;
c4a1d9e4 16138 u32 source;
f301b1e1 16139 u32 stat;
52331309 16140 } pipe[I915_MAX_PIPES];
c4a1d9e4
CW
16141
16142 struct intel_plane_error_state {
16143 u32 control;
16144 u32 stride;
16145 u32 size;
16146 u32 pos;
16147 u32 addr;
16148 u32 surface;
16149 u32 tile_offset;
52331309 16150 } plane[I915_MAX_PIPES];
63b66e5b
CW
16151
16152 struct intel_transcoder_error_state {
ddf9c536 16153 bool power_domain_on;
63b66e5b
CW
16154 enum transcoder cpu_transcoder;
16155
16156 u32 conf;
16157
16158 u32 htotal;
16159 u32 hblank;
16160 u32 hsync;
16161 u32 vtotal;
16162 u32 vblank;
16163 u32 vsync;
16164 } transcoder[4];
c4a1d9e4
CW
16165};
16166
16167struct intel_display_error_state *
16168intel_display_capture_error_state(struct drm_device *dev)
16169{
fbee40df 16170 struct drm_i915_private *dev_priv = dev->dev_private;
c4a1d9e4 16171 struct intel_display_error_state *error;
63b66e5b
CW
16172 int transcoders[] = {
16173 TRANSCODER_A,
16174 TRANSCODER_B,
16175 TRANSCODER_C,
16176 TRANSCODER_EDP,
16177 };
c4a1d9e4
CW
16178 int i;
16179
63b66e5b
CW
16180 if (INTEL_INFO(dev)->num_pipes == 0)
16181 return NULL;
16182
9d1cb914 16183 error = kzalloc(sizeof(*error), GFP_ATOMIC);
c4a1d9e4
CW
16184 if (error == NULL)
16185 return NULL;
16186
190be112 16187 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
ff57f1b0
PZ
16188 error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
16189
055e393f 16190 for_each_pipe(dev_priv, i) {
ddf9c536 16191 error->pipe[i].power_domain_on =
f458ebbc
DV
16192 __intel_display_power_is_enabled(dev_priv,
16193 POWER_DOMAIN_PIPE(i));
ddf9c536 16194 if (!error->pipe[i].power_domain_on)
9d1cb914
PZ
16195 continue;
16196
5efb3e28
VS
16197 error->cursor[i].control = I915_READ(CURCNTR(i));
16198 error->cursor[i].position = I915_READ(CURPOS(i));
16199 error->cursor[i].base = I915_READ(CURBASE(i));
c4a1d9e4
CW
16200
16201 error->plane[i].control = I915_READ(DSPCNTR(i));
16202 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
80ca378b 16203 if (INTEL_INFO(dev)->gen <= 3) {
51889b35 16204 error->plane[i].size = I915_READ(DSPSIZE(i));
80ca378b
PZ
16205 error->plane[i].pos = I915_READ(DSPPOS(i));
16206 }
ca291363
PZ
16207 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
16208 error->plane[i].addr = I915_READ(DSPADDR(i));
c4a1d9e4
CW
16209 if (INTEL_INFO(dev)->gen >= 4) {
16210 error->plane[i].surface = I915_READ(DSPSURF(i));
16211 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
16212 }
16213
c4a1d9e4 16214 error->pipe[i].source = I915_READ(PIPESRC(i));
f301b1e1 16215
3abfce77 16216 if (HAS_GMCH_DISPLAY(dev))
f301b1e1 16217 error->pipe[i].stat = I915_READ(PIPESTAT(i));
63b66e5b
CW
16218 }
16219
4d1de975 16220 /* Note: this does not include DSI transcoders. */
63b66e5b 16221 error->num_transcoders = INTEL_INFO(dev)->num_pipes;
2d1fe073 16222 if (HAS_DDI(dev_priv))
63b66e5b
CW
16223 error->num_transcoders++; /* Account for eDP. */
16224
16225 for (i = 0; i < error->num_transcoders; i++) {
16226 enum transcoder cpu_transcoder = transcoders[i];
16227
ddf9c536 16228 error->transcoder[i].power_domain_on =
f458ebbc 16229 __intel_display_power_is_enabled(dev_priv,
38cc1daf 16230 POWER_DOMAIN_TRANSCODER(cpu_transcoder));
ddf9c536 16231 if (!error->transcoder[i].power_domain_on)
9d1cb914
PZ
16232 continue;
16233
63b66e5b
CW
16234 error->transcoder[i].cpu_transcoder = cpu_transcoder;
16235
16236 error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
16237 error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
16238 error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
16239 error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
16240 error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
16241 error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
16242 error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
c4a1d9e4
CW
16243 }
16244
16245 return error;
16246}
16247
edc3d884
MK
16248#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
16249
c4a1d9e4 16250void
edc3d884 16251intel_display_print_error_state(struct drm_i915_error_state_buf *m,
c4a1d9e4
CW
16252 struct drm_device *dev,
16253 struct intel_display_error_state *error)
16254{
055e393f 16255 struct drm_i915_private *dev_priv = dev->dev_private;
c4a1d9e4
CW
16256 int i;
16257
63b66e5b
CW
16258 if (!error)
16259 return;
16260
edc3d884 16261 err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
190be112 16262 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
edc3d884 16263 err_printf(m, "PWR_WELL_CTL2: %08x\n",
ff57f1b0 16264 error->power_well_driver);
055e393f 16265 for_each_pipe(dev_priv, i) {
edc3d884 16266 err_printf(m, "Pipe [%d]:\n", i);
ddf9c536 16267 err_printf(m, " Power: %s\n",
87ad3212 16268 onoff(error->pipe[i].power_domain_on));
edc3d884 16269 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
f301b1e1 16270 err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
edc3d884
MK
16271
16272 err_printf(m, "Plane [%d]:\n", i);
16273 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
16274 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
80ca378b 16275 if (INTEL_INFO(dev)->gen <= 3) {
edc3d884
MK
16276 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
16277 err_printf(m, " POS: %08x\n", error->plane[i].pos);
80ca378b 16278 }
4b71a570 16279 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
edc3d884 16280 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
c4a1d9e4 16281 if (INTEL_INFO(dev)->gen >= 4) {
edc3d884
MK
16282 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
16283 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
c4a1d9e4
CW
16284 }
16285
edc3d884
MK
16286 err_printf(m, "Cursor [%d]:\n", i);
16287 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
16288 err_printf(m, " POS: %08x\n", error->cursor[i].position);
16289 err_printf(m, " BASE: %08x\n", error->cursor[i].base);
c4a1d9e4 16290 }
63b66e5b
CW
16291
16292 for (i = 0; i < error->num_transcoders; i++) {
da205630 16293 err_printf(m, "CPU transcoder: %s\n",
63b66e5b 16294 transcoder_name(error->transcoder[i].cpu_transcoder));
ddf9c536 16295 err_printf(m, " Power: %s\n",
87ad3212 16296 onoff(error->transcoder[i].power_domain_on));
63b66e5b
CW
16297 err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
16298 err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
16299 err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
16300 err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
16301 err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
16302 err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
16303 err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
16304 }
c4a1d9e4 16305}
This page took 4.794343 seconds and 5 git commands to generate.