]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/gpu/drm/i915/intel_display.c
drm/i915: Make 308 and 671 MHz cdclks more accurate on SKL
[mirror_ubuntu-bionic-kernel.git] / drivers / gpu / drm / i915 / intel_display.c
CommitLineData
79e53945
JB
1/*
2 * Copyright © 2006-2007 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 *
23 * Authors:
24 * Eric Anholt <eric@anholt.net>
25 */
26
618563e3 27#include <linux/dmi.h>
c1c7af60
JB
28#include <linux/module.h>
29#include <linux/input.h>
79e53945 30#include <linux/i2c.h>
7662c8bd 31#include <linux/kernel.h>
5a0e3ad6 32#include <linux/slab.h>
9cce37f4 33#include <linux/vgaarb.h>
e0dac65e 34#include <drm/drm_edid.h>
760285e7 35#include <drm/drmP.h>
79e53945 36#include "intel_drv.h"
760285e7 37#include <drm/i915_drm.h>
79e53945 38#include "i915_drv.h"
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);
549e2bfb
CK
111static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
112 struct intel_crtc_state *crtc_state);
bfd16b2a
ML
113static void skylake_pfit_enable(struct intel_crtc *crtc);
114static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force);
115static void ironlake_pfit_enable(struct intel_crtc *crtc);
043e9bda 116static void intel_modeset_setup_hw_state(struct drm_device *dev);
2622a081 117static void intel_pre_disable_primary_noatomic(struct drm_crtc *crtc);
4e5ca60f 118static int ilk_max_pixel_rate(struct drm_atomic_state *state);
143f73b3
ML
119static void intel_modeset_verify_crtc(struct drm_crtc *crtc,
120 struct drm_crtc_state *old_state,
121 struct drm_crtc_state *new_state);
e7457a9a 122
d4906093 123struct intel_limit {
4c5def93
ACO
124 struct {
125 int min, max;
126 } dot, vco, n, m, m1, m2, p, p1;
127
128 struct {
129 int dot_limit;
130 int p2_slow, p2_fast;
131 } p2;
d4906093 132};
79e53945 133
bfa7df01
VS
134/* returns HPLL frequency in kHz */
135static int valleyview_get_vco(struct drm_i915_private *dev_priv)
136{
137 int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
138
139 /* Obtain SKU information */
140 mutex_lock(&dev_priv->sb_lock);
141 hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
142 CCK_FUSE_HPLL_FREQ_MASK;
143 mutex_unlock(&dev_priv->sb_lock);
144
145 return vco_freq[hpll_freq] * 1000;
146}
147
c30fec65
VS
148int vlv_get_cck_clock(struct drm_i915_private *dev_priv,
149 const char *name, u32 reg, int ref_freq)
bfa7df01
VS
150{
151 u32 val;
152 int divider;
153
bfa7df01
VS
154 mutex_lock(&dev_priv->sb_lock);
155 val = vlv_cck_read(dev_priv, reg);
156 mutex_unlock(&dev_priv->sb_lock);
157
158 divider = val & CCK_FREQUENCY_VALUES;
159
160 WARN((val & CCK_FREQUENCY_STATUS) !=
161 (divider << CCK_FREQUENCY_STATUS_SHIFT),
162 "%s change in progress\n", name);
163
c30fec65
VS
164 return DIV_ROUND_CLOSEST(ref_freq << 1, divider + 1);
165}
166
167static int vlv_get_cck_clock_hpll(struct drm_i915_private *dev_priv,
168 const char *name, u32 reg)
169{
170 if (dev_priv->hpll_freq == 0)
171 dev_priv->hpll_freq = valleyview_get_vco(dev_priv);
172
173 return vlv_get_cck_clock(dev_priv, name, reg,
174 dev_priv->hpll_freq);
bfa7df01
VS
175}
176
e7dc33f3
VS
177static int
178intel_pch_rawclk(struct drm_i915_private *dev_priv)
d2acd215 179{
e7dc33f3
VS
180 return (I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK) * 1000;
181}
d2acd215 182
e7dc33f3
VS
183static int
184intel_vlv_hrawclk(struct drm_i915_private *dev_priv)
185{
19ab4ed3 186 /* RAWCLK_FREQ_VLV register updated from power well code */
35d38d1f
VS
187 return vlv_get_cck_clock_hpll(dev_priv, "hrawclk",
188 CCK_DISPLAY_REF_CLOCK_CONTROL);
d2acd215
DV
189}
190
e7dc33f3
VS
191static int
192intel_g4x_hrawclk(struct drm_i915_private *dev_priv)
79e50a4f 193{
79e50a4f
JN
194 uint32_t clkcfg;
195
e7dc33f3 196 /* hrawclock is 1/4 the FSB frequency */
79e50a4f
JN
197 clkcfg = I915_READ(CLKCFG);
198 switch (clkcfg & CLKCFG_FSB_MASK) {
199 case CLKCFG_FSB_400:
e7dc33f3 200 return 100000;
79e50a4f 201 case CLKCFG_FSB_533:
e7dc33f3 202 return 133333;
79e50a4f 203 case CLKCFG_FSB_667:
e7dc33f3 204 return 166667;
79e50a4f 205 case CLKCFG_FSB_800:
e7dc33f3 206 return 200000;
79e50a4f 207 case CLKCFG_FSB_1067:
e7dc33f3 208 return 266667;
79e50a4f 209 case CLKCFG_FSB_1333:
e7dc33f3 210 return 333333;
79e50a4f
JN
211 /* these two are just a guess; one of them might be right */
212 case CLKCFG_FSB_1600:
213 case CLKCFG_FSB_1600_ALT:
e7dc33f3 214 return 400000;
79e50a4f 215 default:
e7dc33f3 216 return 133333;
79e50a4f
JN
217 }
218}
219
19ab4ed3 220void intel_update_rawclk(struct drm_i915_private *dev_priv)
e7dc33f3
VS
221{
222 if (HAS_PCH_SPLIT(dev_priv))
223 dev_priv->rawclk_freq = intel_pch_rawclk(dev_priv);
224 else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
225 dev_priv->rawclk_freq = intel_vlv_hrawclk(dev_priv);
226 else if (IS_G4X(dev_priv) || IS_PINEVIEW(dev_priv))
227 dev_priv->rawclk_freq = intel_g4x_hrawclk(dev_priv);
228 else
229 return; /* no rawclk on other platforms, or no need to know it */
230
231 DRM_DEBUG_DRIVER("rawclk rate: %d kHz\n", dev_priv->rawclk_freq);
232}
233
bfa7df01
VS
234static void intel_update_czclk(struct drm_i915_private *dev_priv)
235{
666a4537 236 if (!(IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)))
bfa7df01
VS
237 return;
238
239 dev_priv->czclk_freq = vlv_get_cck_clock_hpll(dev_priv, "czclk",
240 CCK_CZ_CLOCK_CONTROL);
241
242 DRM_DEBUG_DRIVER("CZ clock rate: %d kHz\n", dev_priv->czclk_freq);
243}
244
021357ac 245static inline u32 /* units of 100MHz */
21a727b3
VS
246intel_fdi_link_freq(struct drm_i915_private *dev_priv,
247 const struct intel_crtc_state *pipe_config)
021357ac 248{
21a727b3
VS
249 if (HAS_DDI(dev_priv))
250 return pipe_config->port_clock; /* SPLL */
251 else if (IS_GEN5(dev_priv))
252 return ((I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2) * 10000;
e3b247da 253 else
21a727b3 254 return 270000;
021357ac
CW
255}
256
1b6f4958 257static const struct intel_limit intel_limits_i8xx_dac = {
0206e353 258 .dot = { .min = 25000, .max = 350000 },
9c333719 259 .vco = { .min = 908000, .max = 1512000 },
91dbe5fb 260 .n = { .min = 2, .max = 16 },
0206e353
AJ
261 .m = { .min = 96, .max = 140 },
262 .m1 = { .min = 18, .max = 26 },
263 .m2 = { .min = 6, .max = 16 },
264 .p = { .min = 4, .max = 128 },
265 .p1 = { .min = 2, .max = 33 },
273e27ca
EA
266 .p2 = { .dot_limit = 165000,
267 .p2_slow = 4, .p2_fast = 2 },
e4b36699
KP
268};
269
1b6f4958 270static const struct intel_limit intel_limits_i8xx_dvo = {
5d536e28 271 .dot = { .min = 25000, .max = 350000 },
9c333719 272 .vco = { .min = 908000, .max = 1512000 },
91dbe5fb 273 .n = { .min = 2, .max = 16 },
5d536e28
DV
274 .m = { .min = 96, .max = 140 },
275 .m1 = { .min = 18, .max = 26 },
276 .m2 = { .min = 6, .max = 16 },
277 .p = { .min = 4, .max = 128 },
278 .p1 = { .min = 2, .max = 33 },
279 .p2 = { .dot_limit = 165000,
280 .p2_slow = 4, .p2_fast = 4 },
281};
282
1b6f4958 283static const struct intel_limit intel_limits_i8xx_lvds = {
0206e353 284 .dot = { .min = 25000, .max = 350000 },
9c333719 285 .vco = { .min = 908000, .max = 1512000 },
91dbe5fb 286 .n = { .min = 2, .max = 16 },
0206e353
AJ
287 .m = { .min = 96, .max = 140 },
288 .m1 = { .min = 18, .max = 26 },
289 .m2 = { .min = 6, .max = 16 },
290 .p = { .min = 4, .max = 128 },
291 .p1 = { .min = 1, .max = 6 },
273e27ca
EA
292 .p2 = { .dot_limit = 165000,
293 .p2_slow = 14, .p2_fast = 7 },
e4b36699 294};
273e27ca 295
1b6f4958 296static const struct intel_limit intel_limits_i9xx_sdvo = {
0206e353
AJ
297 .dot = { .min = 20000, .max = 400000 },
298 .vco = { .min = 1400000, .max = 2800000 },
299 .n = { .min = 1, .max = 6 },
300 .m = { .min = 70, .max = 120 },
4f7dfb67
PJ
301 .m1 = { .min = 8, .max = 18 },
302 .m2 = { .min = 3, .max = 7 },
0206e353
AJ
303 .p = { .min = 5, .max = 80 },
304 .p1 = { .min = 1, .max = 8 },
273e27ca
EA
305 .p2 = { .dot_limit = 200000,
306 .p2_slow = 10, .p2_fast = 5 },
e4b36699
KP
307};
308
1b6f4958 309static const struct intel_limit intel_limits_i9xx_lvds = {
0206e353
AJ
310 .dot = { .min = 20000, .max = 400000 },
311 .vco = { .min = 1400000, .max = 2800000 },
312 .n = { .min = 1, .max = 6 },
313 .m = { .min = 70, .max = 120 },
53a7d2d1
PJ
314 .m1 = { .min = 8, .max = 18 },
315 .m2 = { .min = 3, .max = 7 },
0206e353
AJ
316 .p = { .min = 7, .max = 98 },
317 .p1 = { .min = 1, .max = 8 },
273e27ca
EA
318 .p2 = { .dot_limit = 112000,
319 .p2_slow = 14, .p2_fast = 7 },
e4b36699
KP
320};
321
273e27ca 322
1b6f4958 323static const struct intel_limit intel_limits_g4x_sdvo = {
273e27ca
EA
324 .dot = { .min = 25000, .max = 270000 },
325 .vco = { .min = 1750000, .max = 3500000},
326 .n = { .min = 1, .max = 4 },
327 .m = { .min = 104, .max = 138 },
328 .m1 = { .min = 17, .max = 23 },
329 .m2 = { .min = 5, .max = 11 },
330 .p = { .min = 10, .max = 30 },
331 .p1 = { .min = 1, .max = 3},
332 .p2 = { .dot_limit = 270000,
333 .p2_slow = 10,
334 .p2_fast = 10
044c7c41 335 },
e4b36699
KP
336};
337
1b6f4958 338static const struct intel_limit intel_limits_g4x_hdmi = {
273e27ca
EA
339 .dot = { .min = 22000, .max = 400000 },
340 .vco = { .min = 1750000, .max = 3500000},
341 .n = { .min = 1, .max = 4 },
342 .m = { .min = 104, .max = 138 },
343 .m1 = { .min = 16, .max = 23 },
344 .m2 = { .min = 5, .max = 11 },
345 .p = { .min = 5, .max = 80 },
346 .p1 = { .min = 1, .max = 8},
347 .p2 = { .dot_limit = 165000,
348 .p2_slow = 10, .p2_fast = 5 },
e4b36699
KP
349};
350
1b6f4958 351static const struct intel_limit intel_limits_g4x_single_channel_lvds = {
273e27ca
EA
352 .dot = { .min = 20000, .max = 115000 },
353 .vco = { .min = 1750000, .max = 3500000 },
354 .n = { .min = 1, .max = 3 },
355 .m = { .min = 104, .max = 138 },
356 .m1 = { .min = 17, .max = 23 },
357 .m2 = { .min = 5, .max = 11 },
358 .p = { .min = 28, .max = 112 },
359 .p1 = { .min = 2, .max = 8 },
360 .p2 = { .dot_limit = 0,
361 .p2_slow = 14, .p2_fast = 14
044c7c41 362 },
e4b36699
KP
363};
364
1b6f4958 365static const struct intel_limit intel_limits_g4x_dual_channel_lvds = {
273e27ca
EA
366 .dot = { .min = 80000, .max = 224000 },
367 .vco = { .min = 1750000, .max = 3500000 },
368 .n = { .min = 1, .max = 3 },
369 .m = { .min = 104, .max = 138 },
370 .m1 = { .min = 17, .max = 23 },
371 .m2 = { .min = 5, .max = 11 },
372 .p = { .min = 14, .max = 42 },
373 .p1 = { .min = 2, .max = 6 },
374 .p2 = { .dot_limit = 0,
375 .p2_slow = 7, .p2_fast = 7
044c7c41 376 },
e4b36699
KP
377};
378
1b6f4958 379static const struct intel_limit intel_limits_pineview_sdvo = {
0206e353
AJ
380 .dot = { .min = 20000, .max = 400000},
381 .vco = { .min = 1700000, .max = 3500000 },
273e27ca 382 /* Pineview's Ncounter is a ring counter */
0206e353
AJ
383 .n = { .min = 3, .max = 6 },
384 .m = { .min = 2, .max = 256 },
273e27ca 385 /* Pineview only has one combined m divider, which we treat as m2. */
0206e353
AJ
386 .m1 = { .min = 0, .max = 0 },
387 .m2 = { .min = 0, .max = 254 },
388 .p = { .min = 5, .max = 80 },
389 .p1 = { .min = 1, .max = 8 },
273e27ca
EA
390 .p2 = { .dot_limit = 200000,
391 .p2_slow = 10, .p2_fast = 5 },
e4b36699
KP
392};
393
1b6f4958 394static const struct intel_limit intel_limits_pineview_lvds = {
0206e353
AJ
395 .dot = { .min = 20000, .max = 400000 },
396 .vco = { .min = 1700000, .max = 3500000 },
397 .n = { .min = 3, .max = 6 },
398 .m = { .min = 2, .max = 256 },
399 .m1 = { .min = 0, .max = 0 },
400 .m2 = { .min = 0, .max = 254 },
401 .p = { .min = 7, .max = 112 },
402 .p1 = { .min = 1, .max = 8 },
273e27ca
EA
403 .p2 = { .dot_limit = 112000,
404 .p2_slow = 14, .p2_fast = 14 },
e4b36699
KP
405};
406
273e27ca
EA
407/* Ironlake / Sandybridge
408 *
409 * We calculate clock using (register_value + 2) for N/M1/M2, so here
410 * the range value for them is (actual_value - 2).
411 */
1b6f4958 412static const struct intel_limit intel_limits_ironlake_dac = {
273e27ca
EA
413 .dot = { .min = 25000, .max = 350000 },
414 .vco = { .min = 1760000, .max = 3510000 },
415 .n = { .min = 1, .max = 5 },
416 .m = { .min = 79, .max = 127 },
417 .m1 = { .min = 12, .max = 22 },
418 .m2 = { .min = 5, .max = 9 },
419 .p = { .min = 5, .max = 80 },
420 .p1 = { .min = 1, .max = 8 },
421 .p2 = { .dot_limit = 225000,
422 .p2_slow = 10, .p2_fast = 5 },
e4b36699
KP
423};
424
1b6f4958 425static const struct intel_limit intel_limits_ironlake_single_lvds = {
273e27ca
EA
426 .dot = { .min = 25000, .max = 350000 },
427 .vco = { .min = 1760000, .max = 3510000 },
428 .n = { .min = 1, .max = 3 },
429 .m = { .min = 79, .max = 118 },
430 .m1 = { .min = 12, .max = 22 },
431 .m2 = { .min = 5, .max = 9 },
432 .p = { .min = 28, .max = 112 },
433 .p1 = { .min = 2, .max = 8 },
434 .p2 = { .dot_limit = 225000,
435 .p2_slow = 14, .p2_fast = 14 },
b91ad0ec
ZW
436};
437
1b6f4958 438static const struct intel_limit intel_limits_ironlake_dual_lvds = {
273e27ca
EA
439 .dot = { .min = 25000, .max = 350000 },
440 .vco = { .min = 1760000, .max = 3510000 },
441 .n = { .min = 1, .max = 3 },
442 .m = { .min = 79, .max = 127 },
443 .m1 = { .min = 12, .max = 22 },
444 .m2 = { .min = 5, .max = 9 },
445 .p = { .min = 14, .max = 56 },
446 .p1 = { .min = 2, .max = 8 },
447 .p2 = { .dot_limit = 225000,
448 .p2_slow = 7, .p2_fast = 7 },
b91ad0ec
ZW
449};
450
273e27ca 451/* LVDS 100mhz refclk limits. */
1b6f4958 452static const struct intel_limit intel_limits_ironlake_single_lvds_100m = {
273e27ca
EA
453 .dot = { .min = 25000, .max = 350000 },
454 .vco = { .min = 1760000, .max = 3510000 },
455 .n = { .min = 1, .max = 2 },
456 .m = { .min = 79, .max = 126 },
457 .m1 = { .min = 12, .max = 22 },
458 .m2 = { .min = 5, .max = 9 },
459 .p = { .min = 28, .max = 112 },
0206e353 460 .p1 = { .min = 2, .max = 8 },
273e27ca
EA
461 .p2 = { .dot_limit = 225000,
462 .p2_slow = 14, .p2_fast = 14 },
b91ad0ec
ZW
463};
464
1b6f4958 465static const struct intel_limit intel_limits_ironlake_dual_lvds_100m = {
273e27ca
EA
466 .dot = { .min = 25000, .max = 350000 },
467 .vco = { .min = 1760000, .max = 3510000 },
468 .n = { .min = 1, .max = 3 },
469 .m = { .min = 79, .max = 126 },
470 .m1 = { .min = 12, .max = 22 },
471 .m2 = { .min = 5, .max = 9 },
472 .p = { .min = 14, .max = 42 },
0206e353 473 .p1 = { .min = 2, .max = 6 },
273e27ca
EA
474 .p2 = { .dot_limit = 225000,
475 .p2_slow = 7, .p2_fast = 7 },
4547668a
ZY
476};
477
1b6f4958 478static const struct intel_limit intel_limits_vlv = {
f01b7962
VS
479 /*
480 * These are the data rate limits (measured in fast clocks)
481 * since those are the strictest limits we have. The fast
482 * clock and actual rate limits are more relaxed, so checking
483 * them would make no difference.
484 */
485 .dot = { .min = 25000 * 5, .max = 270000 * 5 },
75e53986 486 .vco = { .min = 4000000, .max = 6000000 },
a0c4da24 487 .n = { .min = 1, .max = 7 },
a0c4da24
JB
488 .m1 = { .min = 2, .max = 3 },
489 .m2 = { .min = 11, .max = 156 },
b99ab663 490 .p1 = { .min = 2, .max = 3 },
5fdc9c49 491 .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
a0c4da24
JB
492};
493
1b6f4958 494static const struct intel_limit intel_limits_chv = {
ef9348c8
CML
495 /*
496 * These are the data rate limits (measured in fast clocks)
497 * since those are the strictest limits we have. The fast
498 * clock and actual rate limits are more relaxed, so checking
499 * them would make no difference.
500 */
501 .dot = { .min = 25000 * 5, .max = 540000 * 5},
17fe1021 502 .vco = { .min = 4800000, .max = 6480000 },
ef9348c8
CML
503 .n = { .min = 1, .max = 1 },
504 .m1 = { .min = 2, .max = 2 },
505 .m2 = { .min = 24 << 22, .max = 175 << 22 },
506 .p1 = { .min = 2, .max = 4 },
507 .p2 = { .p2_slow = 1, .p2_fast = 14 },
508};
509
1b6f4958 510static const struct intel_limit intel_limits_bxt = {
5ab7b0b7
ID
511 /* FIXME: find real dot limits */
512 .dot = { .min = 0, .max = INT_MAX },
e6292556 513 .vco = { .min = 4800000, .max = 6700000 },
5ab7b0b7
ID
514 .n = { .min = 1, .max = 1 },
515 .m1 = { .min = 2, .max = 2 },
516 /* FIXME: find real m2 limits */
517 .m2 = { .min = 2 << 22, .max = 255 << 22 },
518 .p1 = { .min = 2, .max = 4 },
519 .p2 = { .p2_slow = 1, .p2_fast = 20 },
520};
521
cdba954e
ACO
522static bool
523needs_modeset(struct drm_crtc_state *state)
524{
fc596660 525 return drm_atomic_crtc_needs_modeset(state);
cdba954e
ACO
526}
527
e0638cdf
PZ
528/**
529 * Returns whether any output on the specified pipe is of the specified type
530 */
4093561b 531bool intel_pipe_has_type(struct intel_crtc *crtc, enum intel_output_type type)
e0638cdf 532{
409ee761 533 struct drm_device *dev = crtc->base.dev;
e0638cdf
PZ
534 struct intel_encoder *encoder;
535
409ee761 536 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
e0638cdf
PZ
537 if (encoder->type == type)
538 return true;
539
540 return false;
541}
542
d0737e1d
ACO
543/**
544 * Returns whether any output on the specified pipe will have the specified
545 * type after a staged modeset is complete, i.e., the same as
546 * intel_pipe_has_type() but looking at encoder->new_crtc instead of
547 * encoder->crtc.
548 */
a93e255f
ACO
549static bool intel_pipe_will_have_type(const struct intel_crtc_state *crtc_state,
550 int type)
d0737e1d 551{
a93e255f 552 struct drm_atomic_state *state = crtc_state->base.state;
da3ced29 553 struct drm_connector *connector;
a93e255f 554 struct drm_connector_state *connector_state;
d0737e1d 555 struct intel_encoder *encoder;
a93e255f
ACO
556 int i, num_connectors = 0;
557
da3ced29 558 for_each_connector_in_state(state, connector, connector_state, i) {
a93e255f
ACO
559 if (connector_state->crtc != crtc_state->base.crtc)
560 continue;
561
562 num_connectors++;
d0737e1d 563
a93e255f
ACO
564 encoder = to_intel_encoder(connector_state->best_encoder);
565 if (encoder->type == type)
d0737e1d 566 return true;
a93e255f
ACO
567 }
568
569 WARN_ON(num_connectors == 0);
d0737e1d
ACO
570
571 return false;
572}
573
dccbea3b
ID
574/*
575 * Platform specific helpers to calculate the port PLL loopback- (clock.m),
576 * and post-divider (clock.p) values, pre- (clock.vco) and post-divided fast
577 * (clock.dot) clock rates. This fast dot clock is fed to the port's IO logic.
578 * The helpers' return value is the rate of the clock that is fed to the
579 * display engine's pipe which can be the above fast dot clock rate or a
580 * divided-down version of it.
581 */
f2b115e6 582/* m1 is reserved as 0 in Pineview, n is a ring counter */
9e2c8475 583static int pnv_calc_dpll_params(int refclk, struct dpll *clock)
79e53945 584{
2177832f
SL
585 clock->m = clock->m2 + 2;
586 clock->p = clock->p1 * clock->p2;
ed5ca77e 587 if (WARN_ON(clock->n == 0 || clock->p == 0))
dccbea3b 588 return 0;
fb03ac01
VS
589 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
590 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
dccbea3b
ID
591
592 return clock->dot;
2177832f
SL
593}
594
7429e9d4
DV
595static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
596{
597 return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
598}
599
9e2c8475 600static int i9xx_calc_dpll_params(int refclk, struct dpll *clock)
2177832f 601{
7429e9d4 602 clock->m = i9xx_dpll_compute_m(clock);
79e53945 603 clock->p = clock->p1 * clock->p2;
ed5ca77e 604 if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
dccbea3b 605 return 0;
fb03ac01
VS
606 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
607 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
dccbea3b
ID
608
609 return clock->dot;
79e53945
JB
610}
611
9e2c8475 612static int vlv_calc_dpll_params(int refclk, struct dpll *clock)
589eca67
ID
613{
614 clock->m = clock->m1 * clock->m2;
615 clock->p = clock->p1 * clock->p2;
616 if (WARN_ON(clock->n == 0 || clock->p == 0))
dccbea3b 617 return 0;
589eca67
ID
618 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
619 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
dccbea3b
ID
620
621 return clock->dot / 5;
589eca67
ID
622}
623
9e2c8475 624int chv_calc_dpll_params(int refclk, struct dpll *clock)
ef9348c8
CML
625{
626 clock->m = clock->m1 * clock->m2;
627 clock->p = clock->p1 * clock->p2;
628 if (WARN_ON(clock->n == 0 || clock->p == 0))
dccbea3b 629 return 0;
ef9348c8
CML
630 clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
631 clock->n << 22);
632 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
dccbea3b
ID
633
634 return clock->dot / 5;
ef9348c8
CML
635}
636
7c04d1d9 637#define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
79e53945
JB
638/**
639 * Returns whether the given set of divisors are valid for a given refclk with
640 * the given connectors.
641 */
642
1b894b59 643static bool intel_PLL_is_valid(struct drm_device *dev,
1b6f4958 644 const struct intel_limit *limit,
9e2c8475 645 const struct dpll *clock)
79e53945 646{
f01b7962
VS
647 if (clock->n < limit->n.min || limit->n.max < clock->n)
648 INTELPllInvalid("n out of range\n");
79e53945 649 if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
0206e353 650 INTELPllInvalid("p1 out of range\n");
79e53945 651 if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
0206e353 652 INTELPllInvalid("m2 out of range\n");
79e53945 653 if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
0206e353 654 INTELPllInvalid("m1 out of range\n");
f01b7962 655
666a4537
WB
656 if (!IS_PINEVIEW(dev) && !IS_VALLEYVIEW(dev) &&
657 !IS_CHERRYVIEW(dev) && !IS_BROXTON(dev))
f01b7962
VS
658 if (clock->m1 <= clock->m2)
659 INTELPllInvalid("m1 <= m2\n");
660
666a4537 661 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev) && !IS_BROXTON(dev)) {
f01b7962
VS
662 if (clock->p < limit->p.min || limit->p.max < clock->p)
663 INTELPllInvalid("p out of range\n");
664 if (clock->m < limit->m.min || limit->m.max < clock->m)
665 INTELPllInvalid("m out of range\n");
666 }
667
79e53945 668 if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
0206e353 669 INTELPllInvalid("vco out of range\n");
79e53945
JB
670 /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
671 * connector, etc., rather than just a single range.
672 */
673 if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
0206e353 674 INTELPllInvalid("dot out of range\n");
79e53945
JB
675
676 return true;
677}
678
3b1429d9 679static int
1b6f4958 680i9xx_select_p2_div(const struct intel_limit *limit,
3b1429d9
VS
681 const struct intel_crtc_state *crtc_state,
682 int target)
79e53945 683{
3b1429d9 684 struct drm_device *dev = crtc_state->base.crtc->dev;
79e53945 685
a93e255f 686 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
79e53945 687 /*
a210b028
DV
688 * For LVDS just rely on its current settings for dual-channel.
689 * We haven't figured out how to reliably set up different
690 * single/dual channel state, if we even can.
79e53945 691 */
1974cad0 692 if (intel_is_dual_link_lvds(dev))
3b1429d9 693 return limit->p2.p2_fast;
79e53945 694 else
3b1429d9 695 return limit->p2.p2_slow;
79e53945
JB
696 } else {
697 if (target < limit->p2.dot_limit)
3b1429d9 698 return limit->p2.p2_slow;
79e53945 699 else
3b1429d9 700 return limit->p2.p2_fast;
79e53945 701 }
3b1429d9
VS
702}
703
70e8aa21
ACO
704/*
705 * Returns a set of divisors for the desired target clock with the given
706 * refclk, or FALSE. The returned values represent the clock equation:
707 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
708 *
709 * Target and reference clocks are specified in kHz.
710 *
711 * If match_clock is provided, then best_clock P divider must match the P
712 * divider from @match_clock used for LVDS downclocking.
713 */
3b1429d9 714static bool
1b6f4958 715i9xx_find_best_dpll(const struct intel_limit *limit,
3b1429d9 716 struct intel_crtc_state *crtc_state,
9e2c8475
ACO
717 int target, int refclk, struct dpll *match_clock,
718 struct dpll *best_clock)
3b1429d9
VS
719{
720 struct drm_device *dev = crtc_state->base.crtc->dev;
9e2c8475 721 struct dpll clock;
3b1429d9 722 int err = target;
79e53945 723
0206e353 724 memset(best_clock, 0, sizeof(*best_clock));
79e53945 725
3b1429d9
VS
726 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
727
42158660
ZY
728 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
729 clock.m1++) {
730 for (clock.m2 = limit->m2.min;
731 clock.m2 <= limit->m2.max; clock.m2++) {
c0efc387 732 if (clock.m2 >= clock.m1)
42158660
ZY
733 break;
734 for (clock.n = limit->n.min;
735 clock.n <= limit->n.max; clock.n++) {
736 for (clock.p1 = limit->p1.min;
737 clock.p1 <= limit->p1.max; clock.p1++) {
79e53945
JB
738 int this_err;
739
dccbea3b 740 i9xx_calc_dpll_params(refclk, &clock);
ac58c3f0
DV
741 if (!intel_PLL_is_valid(dev, limit,
742 &clock))
743 continue;
744 if (match_clock &&
745 clock.p != match_clock->p)
746 continue;
747
748 this_err = abs(clock.dot - target);
749 if (this_err < err) {
750 *best_clock = clock;
751 err = this_err;
752 }
753 }
754 }
755 }
756 }
757
758 return (err != target);
759}
760
70e8aa21
ACO
761/*
762 * Returns a set of divisors for the desired target clock with the given
763 * refclk, or FALSE. The returned values represent the clock equation:
764 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
765 *
766 * Target and reference clocks are specified in kHz.
767 *
768 * If match_clock is provided, then best_clock P divider must match the P
769 * divider from @match_clock used for LVDS downclocking.
770 */
ac58c3f0 771static bool
1b6f4958 772pnv_find_best_dpll(const struct intel_limit *limit,
a93e255f 773 struct intel_crtc_state *crtc_state,
9e2c8475
ACO
774 int target, int refclk, struct dpll *match_clock,
775 struct dpll *best_clock)
79e53945 776{
3b1429d9 777 struct drm_device *dev = crtc_state->base.crtc->dev;
9e2c8475 778 struct dpll clock;
79e53945
JB
779 int err = target;
780
0206e353 781 memset(best_clock, 0, sizeof(*best_clock));
79e53945 782
3b1429d9
VS
783 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
784
42158660
ZY
785 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
786 clock.m1++) {
787 for (clock.m2 = limit->m2.min;
788 clock.m2 <= limit->m2.max; clock.m2++) {
42158660
ZY
789 for (clock.n = limit->n.min;
790 clock.n <= limit->n.max; clock.n++) {
791 for (clock.p1 = limit->p1.min;
792 clock.p1 <= limit->p1.max; clock.p1++) {
79e53945
JB
793 int this_err;
794
dccbea3b 795 pnv_calc_dpll_params(refclk, &clock);
1b894b59
CW
796 if (!intel_PLL_is_valid(dev, limit,
797 &clock))
79e53945 798 continue;
cec2f356
SP
799 if (match_clock &&
800 clock.p != match_clock->p)
801 continue;
79e53945
JB
802
803 this_err = abs(clock.dot - target);
804 if (this_err < err) {
805 *best_clock = clock;
806 err = this_err;
807 }
808 }
809 }
810 }
811 }
812
813 return (err != target);
814}
815
997c030c
ACO
816/*
817 * Returns a set of divisors for the desired target clock with the given
818 * refclk, or FALSE. The returned values represent the clock equation:
819 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
70e8aa21
ACO
820 *
821 * Target and reference clocks are specified in kHz.
822 *
823 * If match_clock is provided, then best_clock P divider must match the P
824 * divider from @match_clock used for LVDS downclocking.
997c030c 825 */
d4906093 826static bool
1b6f4958 827g4x_find_best_dpll(const struct intel_limit *limit,
a93e255f 828 struct intel_crtc_state *crtc_state,
9e2c8475
ACO
829 int target, int refclk, struct dpll *match_clock,
830 struct dpll *best_clock)
d4906093 831{
3b1429d9 832 struct drm_device *dev = crtc_state->base.crtc->dev;
9e2c8475 833 struct dpll clock;
d4906093 834 int max_n;
3b1429d9 835 bool found = false;
6ba770dc
AJ
836 /* approximately equals target * 0.00585 */
837 int err_most = (target >> 8) + (target >> 9);
d4906093
ML
838
839 memset(best_clock, 0, sizeof(*best_clock));
3b1429d9
VS
840
841 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
842
d4906093 843 max_n = limit->n.max;
f77f13e2 844 /* based on hardware requirement, prefer smaller n to precision */
d4906093 845 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
f77f13e2 846 /* based on hardware requirement, prefere larger m1,m2 */
d4906093
ML
847 for (clock.m1 = limit->m1.max;
848 clock.m1 >= limit->m1.min; clock.m1--) {
849 for (clock.m2 = limit->m2.max;
850 clock.m2 >= limit->m2.min; clock.m2--) {
851 for (clock.p1 = limit->p1.max;
852 clock.p1 >= limit->p1.min; clock.p1--) {
853 int this_err;
854
dccbea3b 855 i9xx_calc_dpll_params(refclk, &clock);
1b894b59
CW
856 if (!intel_PLL_is_valid(dev, limit,
857 &clock))
d4906093 858 continue;
1b894b59
CW
859
860 this_err = abs(clock.dot - target);
d4906093
ML
861 if (this_err < err_most) {
862 *best_clock = clock;
863 err_most = this_err;
864 max_n = clock.n;
865 found = true;
866 }
867 }
868 }
869 }
870 }
2c07245f
ZW
871 return found;
872}
873
d5dd62bd
ID
874/*
875 * Check if the calculated PLL configuration is more optimal compared to the
876 * best configuration and error found so far. Return the calculated error.
877 */
878static bool vlv_PLL_is_optimal(struct drm_device *dev, int target_freq,
9e2c8475
ACO
879 const struct dpll *calculated_clock,
880 const struct dpll *best_clock,
d5dd62bd
ID
881 unsigned int best_error_ppm,
882 unsigned int *error_ppm)
883{
9ca3ba01
ID
884 /*
885 * For CHV ignore the error and consider only the P value.
886 * Prefer a bigger P value based on HW requirements.
887 */
888 if (IS_CHERRYVIEW(dev)) {
889 *error_ppm = 0;
890
891 return calculated_clock->p > best_clock->p;
892 }
893
24be4e46
ID
894 if (WARN_ON_ONCE(!target_freq))
895 return false;
896
d5dd62bd
ID
897 *error_ppm = div_u64(1000000ULL *
898 abs(target_freq - calculated_clock->dot),
899 target_freq);
900 /*
901 * Prefer a better P value over a better (smaller) error if the error
902 * is small. Ensure this preference for future configurations too by
903 * setting the error to 0.
904 */
905 if (*error_ppm < 100 && calculated_clock->p > best_clock->p) {
906 *error_ppm = 0;
907
908 return true;
909 }
910
911 return *error_ppm + 10 < best_error_ppm;
912}
913
65b3d6a9
ACO
914/*
915 * Returns a set of divisors for the desired target clock with the given
916 * refclk, or FALSE. The returned values represent the clock equation:
917 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
918 */
a0c4da24 919static bool
1b6f4958 920vlv_find_best_dpll(const struct intel_limit *limit,
a93e255f 921 struct intel_crtc_state *crtc_state,
9e2c8475
ACO
922 int target, int refclk, struct dpll *match_clock,
923 struct dpll *best_clock)
a0c4da24 924{
a93e255f 925 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
a919ff14 926 struct drm_device *dev = crtc->base.dev;
9e2c8475 927 struct dpll clock;
69e4f900 928 unsigned int bestppm = 1000000;
27e639bf
VS
929 /* min update 19.2 MHz */
930 int max_n = min(limit->n.max, refclk / 19200);
49e497ef 931 bool found = false;
a0c4da24 932
6b4bf1c4
VS
933 target *= 5; /* fast clock */
934
935 memset(best_clock, 0, sizeof(*best_clock));
a0c4da24
JB
936
937 /* based on hardware requirement, prefer smaller n to precision */
27e639bf 938 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
811bbf05 939 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
889059d8 940 for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
c1a9ae43 941 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
6b4bf1c4 942 clock.p = clock.p1 * clock.p2;
a0c4da24 943 /* based on hardware requirement, prefer bigger m1,m2 values */
6b4bf1c4 944 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
d5dd62bd 945 unsigned int ppm;
69e4f900 946
6b4bf1c4
VS
947 clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
948 refclk * clock.m1);
949
dccbea3b 950 vlv_calc_dpll_params(refclk, &clock);
43b0ac53 951
f01b7962
VS
952 if (!intel_PLL_is_valid(dev, limit,
953 &clock))
43b0ac53
VS
954 continue;
955
d5dd62bd
ID
956 if (!vlv_PLL_is_optimal(dev, target,
957 &clock,
958 best_clock,
959 bestppm, &ppm))
960 continue;
6b4bf1c4 961
d5dd62bd
ID
962 *best_clock = clock;
963 bestppm = ppm;
964 found = true;
a0c4da24
JB
965 }
966 }
967 }
968 }
a0c4da24 969
49e497ef 970 return found;
a0c4da24 971}
a4fc5ed6 972
65b3d6a9
ACO
973/*
974 * Returns a set of divisors for the desired target clock with the given
975 * refclk, or FALSE. The returned values represent the clock equation:
976 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
977 */
ef9348c8 978static bool
1b6f4958 979chv_find_best_dpll(const struct intel_limit *limit,
a93e255f 980 struct intel_crtc_state *crtc_state,
9e2c8475
ACO
981 int target, int refclk, struct dpll *match_clock,
982 struct dpll *best_clock)
ef9348c8 983{
a93e255f 984 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
a919ff14 985 struct drm_device *dev = crtc->base.dev;
9ca3ba01 986 unsigned int best_error_ppm;
9e2c8475 987 struct dpll clock;
ef9348c8
CML
988 uint64_t m2;
989 int found = false;
990
991 memset(best_clock, 0, sizeof(*best_clock));
9ca3ba01 992 best_error_ppm = 1000000;
ef9348c8
CML
993
994 /*
995 * Based on hardware doc, the n always set to 1, and m1 always
996 * set to 2. If requires to support 200Mhz refclk, we need to
997 * revisit this because n may not 1 anymore.
998 */
999 clock.n = 1, clock.m1 = 2;
1000 target *= 5; /* fast clock */
1001
1002 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
1003 for (clock.p2 = limit->p2.p2_fast;
1004 clock.p2 >= limit->p2.p2_slow;
1005 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
9ca3ba01 1006 unsigned int error_ppm;
ef9348c8
CML
1007
1008 clock.p = clock.p1 * clock.p2;
1009
1010 m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
1011 clock.n) << 22, refclk * clock.m1);
1012
1013 if (m2 > INT_MAX/clock.m1)
1014 continue;
1015
1016 clock.m2 = m2;
1017
dccbea3b 1018 chv_calc_dpll_params(refclk, &clock);
ef9348c8
CML
1019
1020 if (!intel_PLL_is_valid(dev, limit, &clock))
1021 continue;
1022
9ca3ba01
ID
1023 if (!vlv_PLL_is_optimal(dev, target, &clock, best_clock,
1024 best_error_ppm, &error_ppm))
1025 continue;
1026
1027 *best_clock = clock;
1028 best_error_ppm = error_ppm;
1029 found = true;
ef9348c8
CML
1030 }
1031 }
1032
1033 return found;
1034}
1035
5ab7b0b7 1036bool bxt_find_best_dpll(struct intel_crtc_state *crtc_state, int target_clock,
9e2c8475 1037 struct dpll *best_clock)
5ab7b0b7 1038{
65b3d6a9 1039 int refclk = 100000;
1b6f4958 1040 const struct intel_limit *limit = &intel_limits_bxt;
5ab7b0b7 1041
65b3d6a9 1042 return chv_find_best_dpll(limit, crtc_state,
5ab7b0b7
ID
1043 target_clock, refclk, NULL, best_clock);
1044}
1045
20ddf665
VS
1046bool intel_crtc_active(struct drm_crtc *crtc)
1047{
1048 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1049
1050 /* Be paranoid as we can arrive here with only partial
1051 * state retrieved from the hardware during setup.
1052 *
241bfc38 1053 * We can ditch the adjusted_mode.crtc_clock check as soon
20ddf665
VS
1054 * as Haswell has gained clock readout/fastboot support.
1055 *
66e514c1 1056 * We can ditch the crtc->primary->fb check as soon as we can
20ddf665 1057 * properly reconstruct framebuffers.
c3d1f436
MR
1058 *
1059 * FIXME: The intel_crtc->active here should be switched to
1060 * crtc->state->active once we have proper CRTC states wired up
1061 * for atomic.
20ddf665 1062 */
c3d1f436 1063 return intel_crtc->active && crtc->primary->state->fb &&
6e3c9717 1064 intel_crtc->config->base.adjusted_mode.crtc_clock;
20ddf665
VS
1065}
1066
a5c961d1
PZ
1067enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
1068 enum pipe pipe)
1069{
1070 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
1071 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1072
6e3c9717 1073 return intel_crtc->config->cpu_transcoder;
a5c961d1
PZ
1074}
1075
fbf49ea2
VS
1076static bool pipe_dsl_stopped(struct drm_device *dev, enum pipe pipe)
1077{
1078 struct drm_i915_private *dev_priv = dev->dev_private;
f0f59a00 1079 i915_reg_t reg = PIPEDSL(pipe);
fbf49ea2
VS
1080 u32 line1, line2;
1081 u32 line_mask;
1082
1083 if (IS_GEN2(dev))
1084 line_mask = DSL_LINEMASK_GEN2;
1085 else
1086 line_mask = DSL_LINEMASK_GEN3;
1087
1088 line1 = I915_READ(reg) & line_mask;
6adfb1ef 1089 msleep(5);
fbf49ea2
VS
1090 line2 = I915_READ(reg) & line_mask;
1091
1092 return line1 == line2;
1093}
1094
ab7ad7f6
KP
1095/*
1096 * intel_wait_for_pipe_off - wait for pipe to turn off
575f7ab7 1097 * @crtc: crtc whose pipe to wait for
9d0498a2
JB
1098 *
1099 * After disabling a pipe, we can't wait for vblank in the usual way,
1100 * spinning on the vblank interrupt status bit, since we won't actually
1101 * see an interrupt when the pipe is disabled.
1102 *
ab7ad7f6
KP
1103 * On Gen4 and above:
1104 * wait for the pipe register state bit to turn off
1105 *
1106 * Otherwise:
1107 * wait for the display line value to settle (it usually
1108 * ends up stopping at the start of the next frame).
58e10eb9 1109 *
9d0498a2 1110 */
575f7ab7 1111static void intel_wait_for_pipe_off(struct intel_crtc *crtc)
9d0498a2 1112{
575f7ab7 1113 struct drm_device *dev = crtc->base.dev;
9d0498a2 1114 struct drm_i915_private *dev_priv = dev->dev_private;
6e3c9717 1115 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
575f7ab7 1116 enum pipe pipe = crtc->pipe;
ab7ad7f6
KP
1117
1118 if (INTEL_INFO(dev)->gen >= 4) {
f0f59a00 1119 i915_reg_t reg = PIPECONF(cpu_transcoder);
ab7ad7f6
KP
1120
1121 /* Wait for the Pipe State to go off */
58e10eb9
CW
1122 if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
1123 100))
284637d9 1124 WARN(1, "pipe_off wait timed out\n");
ab7ad7f6 1125 } else {
ab7ad7f6 1126 /* Wait for the display line to settle */
fbf49ea2 1127 if (wait_for(pipe_dsl_stopped(dev, pipe), 100))
284637d9 1128 WARN(1, "pipe_off wait timed out\n");
ab7ad7f6 1129 }
79e53945
JB
1130}
1131
b24e7179 1132/* Only for pre-ILK configs */
55607e8a
DV
1133void assert_pll(struct drm_i915_private *dev_priv,
1134 enum pipe pipe, bool state)
b24e7179 1135{
b24e7179
JB
1136 u32 val;
1137 bool cur_state;
1138
649636ef 1139 val = I915_READ(DPLL(pipe));
b24e7179 1140 cur_state = !!(val & DPLL_VCO_ENABLE);
e2c719b7 1141 I915_STATE_WARN(cur_state != state,
b24e7179 1142 "PLL state assertion failure (expected %s, current %s)\n",
87ad3212 1143 onoff(state), onoff(cur_state));
b24e7179 1144}
b24e7179 1145
23538ef1 1146/* XXX: the dsi pll is shared between MIPI DSI ports */
8563b1e8 1147void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
23538ef1
JN
1148{
1149 u32 val;
1150 bool cur_state;
1151
a580516d 1152 mutex_lock(&dev_priv->sb_lock);
23538ef1 1153 val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
a580516d 1154 mutex_unlock(&dev_priv->sb_lock);
23538ef1
JN
1155
1156 cur_state = val & DSI_PLL_VCO_EN;
e2c719b7 1157 I915_STATE_WARN(cur_state != state,
23538ef1 1158 "DSI PLL state assertion failure (expected %s, current %s)\n",
87ad3212 1159 onoff(state), onoff(cur_state));
23538ef1 1160}
23538ef1 1161
040484af
JB
1162static void assert_fdi_tx(struct drm_i915_private *dev_priv,
1163 enum pipe pipe, bool state)
1164{
040484af 1165 bool cur_state;
ad80a810
PZ
1166 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1167 pipe);
040484af 1168
2d1fe073 1169 if (HAS_DDI(dev_priv)) {
affa9354 1170 /* DDI does not have a specific FDI_TX register */
649636ef 1171 u32 val = I915_READ(TRANS_DDI_FUNC_CTL(cpu_transcoder));
ad80a810 1172 cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
bf507ef7 1173 } else {
649636ef 1174 u32 val = I915_READ(FDI_TX_CTL(pipe));
bf507ef7
ED
1175 cur_state = !!(val & FDI_TX_ENABLE);
1176 }
e2c719b7 1177 I915_STATE_WARN(cur_state != state,
040484af 1178 "FDI TX state assertion failure (expected %s, current %s)\n",
87ad3212 1179 onoff(state), onoff(cur_state));
040484af
JB
1180}
1181#define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
1182#define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
1183
1184static void assert_fdi_rx(struct drm_i915_private *dev_priv,
1185 enum pipe pipe, bool state)
1186{
040484af
JB
1187 u32 val;
1188 bool cur_state;
1189
649636ef 1190 val = I915_READ(FDI_RX_CTL(pipe));
d63fa0dc 1191 cur_state = !!(val & FDI_RX_ENABLE);
e2c719b7 1192 I915_STATE_WARN(cur_state != state,
040484af 1193 "FDI RX state assertion failure (expected %s, current %s)\n",
87ad3212 1194 onoff(state), onoff(cur_state));
040484af
JB
1195}
1196#define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
1197#define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
1198
1199static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
1200 enum pipe pipe)
1201{
040484af
JB
1202 u32 val;
1203
1204 /* ILK FDI PLL is always enabled */
7e22dbbb 1205 if (IS_GEN5(dev_priv))
040484af
JB
1206 return;
1207
bf507ef7 1208 /* On Haswell, DDI ports are responsible for the FDI PLL setup */
2d1fe073 1209 if (HAS_DDI(dev_priv))
bf507ef7
ED
1210 return;
1211
649636ef 1212 val = I915_READ(FDI_TX_CTL(pipe));
e2c719b7 1213 I915_STATE_WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
040484af
JB
1214}
1215
55607e8a
DV
1216void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
1217 enum pipe pipe, bool state)
040484af 1218{
040484af 1219 u32 val;
55607e8a 1220 bool cur_state;
040484af 1221
649636ef 1222 val = I915_READ(FDI_RX_CTL(pipe));
55607e8a 1223 cur_state = !!(val & FDI_RX_PLL_ENABLE);
e2c719b7 1224 I915_STATE_WARN(cur_state != state,
55607e8a 1225 "FDI RX PLL assertion failure (expected %s, current %s)\n",
87ad3212 1226 onoff(state), onoff(cur_state));
040484af
JB
1227}
1228
b680c37a
DV
1229void assert_panel_unlocked(struct drm_i915_private *dev_priv,
1230 enum pipe pipe)
ea0760cf 1231{
bedd4dba 1232 struct drm_device *dev = dev_priv->dev;
f0f59a00 1233 i915_reg_t pp_reg;
ea0760cf
JB
1234 u32 val;
1235 enum pipe panel_pipe = PIPE_A;
0de3b485 1236 bool locked = true;
ea0760cf 1237
bedd4dba
JN
1238 if (WARN_ON(HAS_DDI(dev)))
1239 return;
1240
1241 if (HAS_PCH_SPLIT(dev)) {
1242 u32 port_sel;
1243
ea0760cf 1244 pp_reg = PCH_PP_CONTROL;
bedd4dba
JN
1245 port_sel = I915_READ(PCH_PP_ON_DELAYS) & PANEL_PORT_SELECT_MASK;
1246
1247 if (port_sel == PANEL_PORT_SELECT_LVDS &&
1248 I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
1249 panel_pipe = PIPE_B;
1250 /* XXX: else fix for eDP */
666a4537 1251 } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
bedd4dba
JN
1252 /* presumably write lock depends on pipe, not port select */
1253 pp_reg = VLV_PIPE_PP_CONTROL(pipe);
1254 panel_pipe = pipe;
ea0760cf
JB
1255 } else {
1256 pp_reg = PP_CONTROL;
bedd4dba
JN
1257 if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
1258 panel_pipe = PIPE_B;
ea0760cf
JB
1259 }
1260
1261 val = I915_READ(pp_reg);
1262 if (!(val & PANEL_POWER_ON) ||
ec49ba2d 1263 ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
ea0760cf
JB
1264 locked = false;
1265
e2c719b7 1266 I915_STATE_WARN(panel_pipe == pipe && locked,
ea0760cf 1267 "panel assertion failure, pipe %c regs locked\n",
9db4a9c7 1268 pipe_name(pipe));
ea0760cf
JB
1269}
1270
93ce0ba6
JN
1271static void assert_cursor(struct drm_i915_private *dev_priv,
1272 enum pipe pipe, bool state)
1273{
1274 struct drm_device *dev = dev_priv->dev;
1275 bool cur_state;
1276
d9d82081 1277 if (IS_845G(dev) || IS_I865G(dev))
0b87c24e 1278 cur_state = I915_READ(CURCNTR(PIPE_A)) & CURSOR_ENABLE;
d9d82081 1279 else
5efb3e28 1280 cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
93ce0ba6 1281
e2c719b7 1282 I915_STATE_WARN(cur_state != state,
93ce0ba6 1283 "cursor on pipe %c assertion failure (expected %s, current %s)\n",
87ad3212 1284 pipe_name(pipe), onoff(state), onoff(cur_state));
93ce0ba6
JN
1285}
1286#define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
1287#define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
1288
b840d907
JB
1289void assert_pipe(struct drm_i915_private *dev_priv,
1290 enum pipe pipe, bool state)
b24e7179 1291{
63d7bbe9 1292 bool cur_state;
702e7a56
PZ
1293 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1294 pipe);
4feed0eb 1295 enum intel_display_power_domain power_domain;
b24e7179 1296
b6b5d049
VS
1297 /* if we need the pipe quirk it must be always on */
1298 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1299 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
8e636784
DV
1300 state = true;
1301
4feed0eb
ID
1302 power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
1303 if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
649636ef 1304 u32 val = I915_READ(PIPECONF(cpu_transcoder));
69310161 1305 cur_state = !!(val & PIPECONF_ENABLE);
4feed0eb
ID
1306
1307 intel_display_power_put(dev_priv, power_domain);
1308 } else {
1309 cur_state = false;
69310161
PZ
1310 }
1311
e2c719b7 1312 I915_STATE_WARN(cur_state != state,
63d7bbe9 1313 "pipe %c assertion failure (expected %s, current %s)\n",
87ad3212 1314 pipe_name(pipe), onoff(state), onoff(cur_state));
b24e7179
JB
1315}
1316
931872fc
CW
1317static void assert_plane(struct drm_i915_private *dev_priv,
1318 enum plane plane, bool state)
b24e7179 1319{
b24e7179 1320 u32 val;
931872fc 1321 bool cur_state;
b24e7179 1322
649636ef 1323 val = I915_READ(DSPCNTR(plane));
931872fc 1324 cur_state = !!(val & DISPLAY_PLANE_ENABLE);
e2c719b7 1325 I915_STATE_WARN(cur_state != state,
931872fc 1326 "plane %c assertion failure (expected %s, current %s)\n",
87ad3212 1327 plane_name(plane), onoff(state), onoff(cur_state));
b24e7179
JB
1328}
1329
931872fc
CW
1330#define assert_plane_enabled(d, p) assert_plane(d, p, true)
1331#define assert_plane_disabled(d, p) assert_plane(d, p, false)
1332
b24e7179
JB
1333static void assert_planes_disabled(struct drm_i915_private *dev_priv,
1334 enum pipe pipe)
1335{
653e1026 1336 struct drm_device *dev = dev_priv->dev;
649636ef 1337 int i;
b24e7179 1338
653e1026
VS
1339 /* Primary planes are fixed to pipes on gen4+ */
1340 if (INTEL_INFO(dev)->gen >= 4) {
649636ef 1341 u32 val = I915_READ(DSPCNTR(pipe));
e2c719b7 1342 I915_STATE_WARN(val & DISPLAY_PLANE_ENABLE,
28c05794
AJ
1343 "plane %c assertion failure, should be disabled but not\n",
1344 plane_name(pipe));
19ec1358 1345 return;
28c05794 1346 }
19ec1358 1347
b24e7179 1348 /* Need to check both planes against the pipe */
055e393f 1349 for_each_pipe(dev_priv, i) {
649636ef
VS
1350 u32 val = I915_READ(DSPCNTR(i));
1351 enum pipe cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
b24e7179 1352 DISPPLANE_SEL_PIPE_SHIFT;
e2c719b7 1353 I915_STATE_WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
9db4a9c7
JB
1354 "plane %c assertion failure, should be off on pipe %c but is still active\n",
1355 plane_name(i), pipe_name(pipe));
b24e7179
JB
1356 }
1357}
1358
19332d7a
JB
1359static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
1360 enum pipe pipe)
1361{
20674eef 1362 struct drm_device *dev = dev_priv->dev;
649636ef 1363 int sprite;
19332d7a 1364
7feb8b88 1365 if (INTEL_INFO(dev)->gen >= 9) {
3bdcfc0c 1366 for_each_sprite(dev_priv, pipe, sprite) {
649636ef 1367 u32 val = I915_READ(PLANE_CTL(pipe, sprite));
e2c719b7 1368 I915_STATE_WARN(val & PLANE_CTL_ENABLE,
7feb8b88
DL
1369 "plane %d assertion failure, should be off on pipe %c but is still active\n",
1370 sprite, pipe_name(pipe));
1371 }
666a4537 1372 } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
3bdcfc0c 1373 for_each_sprite(dev_priv, pipe, sprite) {
649636ef 1374 u32 val = I915_READ(SPCNTR(pipe, sprite));
e2c719b7 1375 I915_STATE_WARN(val & SP_ENABLE,
20674eef 1376 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
1fe47785 1377 sprite_name(pipe, sprite), pipe_name(pipe));
20674eef
VS
1378 }
1379 } else if (INTEL_INFO(dev)->gen >= 7) {
649636ef 1380 u32 val = I915_READ(SPRCTL(pipe));
e2c719b7 1381 I915_STATE_WARN(val & SPRITE_ENABLE,
06da8da2 1382 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
20674eef
VS
1383 plane_name(pipe), pipe_name(pipe));
1384 } else if (INTEL_INFO(dev)->gen >= 5) {
649636ef 1385 u32 val = I915_READ(DVSCNTR(pipe));
e2c719b7 1386 I915_STATE_WARN(val & DVS_ENABLE,
06da8da2 1387 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
20674eef 1388 plane_name(pipe), pipe_name(pipe));
19332d7a
JB
1389 }
1390}
1391
08c71e5e
VS
1392static void assert_vblank_disabled(struct drm_crtc *crtc)
1393{
e2c719b7 1394 if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
08c71e5e
VS
1395 drm_crtc_vblank_put(crtc);
1396}
1397
7abd4b35
ACO
1398void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
1399 enum pipe pipe)
92f2584a 1400{
92f2584a
JB
1401 u32 val;
1402 bool enabled;
1403
649636ef 1404 val = I915_READ(PCH_TRANSCONF(pipe));
92f2584a 1405 enabled = !!(val & TRANS_ENABLE);
e2c719b7 1406 I915_STATE_WARN(enabled,
9db4a9c7
JB
1407 "transcoder assertion failed, should be off on pipe %c but is still active\n",
1408 pipe_name(pipe));
92f2584a
JB
1409}
1410
4e634389
KP
1411static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
1412 enum pipe pipe, u32 port_sel, u32 val)
f0575e92
KP
1413{
1414 if ((val & DP_PORT_EN) == 0)
1415 return false;
1416
2d1fe073 1417 if (HAS_PCH_CPT(dev_priv)) {
f0f59a00 1418 u32 trans_dp_ctl = I915_READ(TRANS_DP_CTL(pipe));
f0575e92
KP
1419 if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
1420 return false;
2d1fe073 1421 } else if (IS_CHERRYVIEW(dev_priv)) {
44f37d1f
CML
1422 if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
1423 return false;
f0575e92
KP
1424 } else {
1425 if ((val & DP_PIPE_MASK) != (pipe << 30))
1426 return false;
1427 }
1428 return true;
1429}
1430
1519b995
KP
1431static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
1432 enum pipe pipe, u32 val)
1433{
dc0fa718 1434 if ((val & SDVO_ENABLE) == 0)
1519b995
KP
1435 return false;
1436
2d1fe073 1437 if (HAS_PCH_CPT(dev_priv)) {
dc0fa718 1438 if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
1519b995 1439 return false;
2d1fe073 1440 } else if (IS_CHERRYVIEW(dev_priv)) {
44f37d1f
CML
1441 if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
1442 return false;
1519b995 1443 } else {
dc0fa718 1444 if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
1519b995
KP
1445 return false;
1446 }
1447 return true;
1448}
1449
1450static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
1451 enum pipe pipe, u32 val)
1452{
1453 if ((val & LVDS_PORT_EN) == 0)
1454 return false;
1455
2d1fe073 1456 if (HAS_PCH_CPT(dev_priv)) {
1519b995
KP
1457 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1458 return false;
1459 } else {
1460 if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
1461 return false;
1462 }
1463 return true;
1464}
1465
1466static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
1467 enum pipe pipe, u32 val)
1468{
1469 if ((val & ADPA_DAC_ENABLE) == 0)
1470 return false;
2d1fe073 1471 if (HAS_PCH_CPT(dev_priv)) {
1519b995
KP
1472 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1473 return false;
1474 } else {
1475 if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
1476 return false;
1477 }
1478 return true;
1479}
1480
291906f1 1481static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
f0f59a00
VS
1482 enum pipe pipe, i915_reg_t reg,
1483 u32 port_sel)
291906f1 1484{
47a05eca 1485 u32 val = I915_READ(reg);
e2c719b7 1486 I915_STATE_WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
291906f1 1487 "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
f0f59a00 1488 i915_mmio_reg_offset(reg), pipe_name(pipe));
de9a35ab 1489
2d1fe073 1490 I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && (val & DP_PORT_EN) == 0
75c5da27 1491 && (val & DP_PIPEB_SELECT),
de9a35ab 1492 "IBX PCH dp port still using transcoder B\n");
291906f1
JB
1493}
1494
1495static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
f0f59a00 1496 enum pipe pipe, i915_reg_t reg)
291906f1 1497{
47a05eca 1498 u32 val = I915_READ(reg);
e2c719b7 1499 I915_STATE_WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
23c99e77 1500 "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
f0f59a00 1501 i915_mmio_reg_offset(reg), pipe_name(pipe));
de9a35ab 1502
2d1fe073 1503 I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && (val & SDVO_ENABLE) == 0
75c5da27 1504 && (val & SDVO_PIPE_B_SELECT),
de9a35ab 1505 "IBX PCH hdmi port still using transcoder B\n");
291906f1
JB
1506}
1507
1508static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
1509 enum pipe pipe)
1510{
291906f1 1511 u32 val;
291906f1 1512
f0575e92
KP
1513 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
1514 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
1515 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
291906f1 1516
649636ef 1517 val = I915_READ(PCH_ADPA);
e2c719b7 1518 I915_STATE_WARN(adpa_pipe_enabled(dev_priv, pipe, val),
291906f1 1519 "PCH VGA enabled on transcoder %c, should be disabled\n",
9db4a9c7 1520 pipe_name(pipe));
291906f1 1521
649636ef 1522 val = I915_READ(PCH_LVDS);
e2c719b7 1523 I915_STATE_WARN(lvds_pipe_enabled(dev_priv, pipe, val),
291906f1 1524 "PCH LVDS enabled on transcoder %c, should be disabled\n",
9db4a9c7 1525 pipe_name(pipe));
291906f1 1526
e2debe91
PZ
1527 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
1528 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
1529 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
291906f1
JB
1530}
1531
cd2d34d9
VS
1532static void _vlv_enable_pll(struct intel_crtc *crtc,
1533 const struct intel_crtc_state *pipe_config)
1534{
1535 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1536 enum pipe pipe = crtc->pipe;
1537
1538 I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
1539 POSTING_READ(DPLL(pipe));
1540 udelay(150);
1541
1542 if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
1543 DRM_ERROR("DPLL %d failed to lock\n", pipe);
1544}
1545
d288f65f 1546static void vlv_enable_pll(struct intel_crtc *crtc,
5cec258b 1547 const struct intel_crtc_state *pipe_config)
87442f73 1548{
cd2d34d9 1549 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
8bd3f301 1550 enum pipe pipe = crtc->pipe;
87442f73 1551
8bd3f301 1552 assert_pipe_disabled(dev_priv, pipe);
87442f73 1553
87442f73 1554 /* PLL is protected by panel, make sure we can write it */
7d1a83cb 1555 assert_panel_unlocked(dev_priv, pipe);
87442f73 1556
cd2d34d9
VS
1557 if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
1558 _vlv_enable_pll(crtc, pipe_config);
426115cf 1559
8bd3f301
VS
1560 I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
1561 POSTING_READ(DPLL_MD(pipe));
87442f73
DV
1562}
1563
cd2d34d9
VS
1564
1565static void _chv_enable_pll(struct intel_crtc *crtc,
1566 const struct intel_crtc_state *pipe_config)
9d556c99 1567{
cd2d34d9 1568 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
8bd3f301 1569 enum pipe pipe = crtc->pipe;
9d556c99 1570 enum dpio_channel port = vlv_pipe_to_channel(pipe);
9d556c99
CML
1571 u32 tmp;
1572
a580516d 1573 mutex_lock(&dev_priv->sb_lock);
9d556c99
CML
1574
1575 /* Enable back the 10bit clock to display controller */
1576 tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1577 tmp |= DPIO_DCLKP_EN;
1578 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
1579
54433e91
VS
1580 mutex_unlock(&dev_priv->sb_lock);
1581
9d556c99
CML
1582 /*
1583 * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
1584 */
1585 udelay(1);
1586
1587 /* Enable PLL */
d288f65f 1588 I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
9d556c99
CML
1589
1590 /* Check PLL is locked */
a11b0703 1591 if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
9d556c99 1592 DRM_ERROR("PLL %d failed to lock\n", pipe);
cd2d34d9
VS
1593}
1594
1595static void chv_enable_pll(struct intel_crtc *crtc,
1596 const struct intel_crtc_state *pipe_config)
1597{
1598 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1599 enum pipe pipe = crtc->pipe;
1600
1601 assert_pipe_disabled(dev_priv, pipe);
1602
1603 /* PLL is protected by panel, make sure we can write it */
1604 assert_panel_unlocked(dev_priv, pipe);
1605
1606 if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
1607 _chv_enable_pll(crtc, pipe_config);
9d556c99 1608
c231775c
VS
1609 if (pipe != PIPE_A) {
1610 /*
1611 * WaPixelRepeatModeFixForC0:chv
1612 *
1613 * DPLLCMD is AWOL. Use chicken bits to propagate
1614 * the value from DPLLBMD to either pipe B or C.
1615 */
1616 I915_WRITE(CBR4_VLV, pipe == PIPE_B ? CBR_DPLLBMD_PIPE_B : CBR_DPLLBMD_PIPE_C);
1617 I915_WRITE(DPLL_MD(PIPE_B), pipe_config->dpll_hw_state.dpll_md);
1618 I915_WRITE(CBR4_VLV, 0);
1619 dev_priv->chv_dpll_md[pipe] = pipe_config->dpll_hw_state.dpll_md;
1620
1621 /*
1622 * DPLLB VGA mode also seems to cause problems.
1623 * We should always have it disabled.
1624 */
1625 WARN_ON((I915_READ(DPLL(PIPE_B)) & DPLL_VGA_MODE_DIS) == 0);
1626 } else {
1627 I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
1628 POSTING_READ(DPLL_MD(pipe));
1629 }
9d556c99
CML
1630}
1631
1c4e0274
VS
1632static int intel_num_dvo_pipes(struct drm_device *dev)
1633{
1634 struct intel_crtc *crtc;
1635 int count = 0;
1636
1637 for_each_intel_crtc(dev, crtc)
3538b9df 1638 count += crtc->base.state->active &&
409ee761 1639 intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO);
1c4e0274
VS
1640
1641 return count;
1642}
1643
66e3d5c0 1644static void i9xx_enable_pll(struct intel_crtc *crtc)
63d7bbe9 1645{
66e3d5c0
DV
1646 struct drm_device *dev = crtc->base.dev;
1647 struct drm_i915_private *dev_priv = dev->dev_private;
f0f59a00 1648 i915_reg_t reg = DPLL(crtc->pipe);
6e3c9717 1649 u32 dpll = crtc->config->dpll_hw_state.dpll;
63d7bbe9 1650
66e3d5c0 1651 assert_pipe_disabled(dev_priv, crtc->pipe);
58c6eaa2 1652
63d7bbe9 1653 /* PLL is protected by panel, make sure we can write it */
66e3d5c0
DV
1654 if (IS_MOBILE(dev) && !IS_I830(dev))
1655 assert_panel_unlocked(dev_priv, crtc->pipe);
63d7bbe9 1656
1c4e0274
VS
1657 /* Enable DVO 2x clock on both PLLs if necessary */
1658 if (IS_I830(dev) && intel_num_dvo_pipes(dev) > 0) {
1659 /*
1660 * It appears to be important that we don't enable this
1661 * for the current pipe before otherwise configuring the
1662 * PLL. No idea how this should be handled if multiple
1663 * DVO outputs are enabled simultaneosly.
1664 */
1665 dpll |= DPLL_DVO_2X_MODE;
1666 I915_WRITE(DPLL(!crtc->pipe),
1667 I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
1668 }
66e3d5c0 1669
c2b63374
VS
1670 /*
1671 * Apparently we need to have VGA mode enabled prior to changing
1672 * the P1/P2 dividers. Otherwise the DPLL will keep using the old
1673 * dividers, even though the register value does change.
1674 */
1675 I915_WRITE(reg, 0);
1676
8e7a65aa
VS
1677 I915_WRITE(reg, dpll);
1678
66e3d5c0
DV
1679 /* Wait for the clocks to stabilize. */
1680 POSTING_READ(reg);
1681 udelay(150);
1682
1683 if (INTEL_INFO(dev)->gen >= 4) {
1684 I915_WRITE(DPLL_MD(crtc->pipe),
6e3c9717 1685 crtc->config->dpll_hw_state.dpll_md);
66e3d5c0
DV
1686 } else {
1687 /* The pixel multiplier can only be updated once the
1688 * DPLL is enabled and the clocks are stable.
1689 *
1690 * So write it again.
1691 */
1692 I915_WRITE(reg, dpll);
1693 }
63d7bbe9
JB
1694
1695 /* We do this three times for luck */
66e3d5c0 1696 I915_WRITE(reg, dpll);
63d7bbe9
JB
1697 POSTING_READ(reg);
1698 udelay(150); /* wait for warmup */
66e3d5c0 1699 I915_WRITE(reg, dpll);
63d7bbe9
JB
1700 POSTING_READ(reg);
1701 udelay(150); /* wait for warmup */
66e3d5c0 1702 I915_WRITE(reg, dpll);
63d7bbe9
JB
1703 POSTING_READ(reg);
1704 udelay(150); /* wait for warmup */
1705}
1706
1707/**
50b44a44 1708 * i9xx_disable_pll - disable a PLL
63d7bbe9
JB
1709 * @dev_priv: i915 private structure
1710 * @pipe: pipe PLL to disable
1711 *
1712 * Disable the PLL for @pipe, making sure the pipe is off first.
1713 *
1714 * Note! This is for pre-ILK only.
1715 */
1c4e0274 1716static void i9xx_disable_pll(struct intel_crtc *crtc)
63d7bbe9 1717{
1c4e0274
VS
1718 struct drm_device *dev = crtc->base.dev;
1719 struct drm_i915_private *dev_priv = dev->dev_private;
1720 enum pipe pipe = crtc->pipe;
1721
1722 /* Disable DVO 2x clock on both PLLs if necessary */
1723 if (IS_I830(dev) &&
409ee761 1724 intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO) &&
3538b9df 1725 !intel_num_dvo_pipes(dev)) {
1c4e0274
VS
1726 I915_WRITE(DPLL(PIPE_B),
1727 I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
1728 I915_WRITE(DPLL(PIPE_A),
1729 I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
1730 }
1731
b6b5d049
VS
1732 /* Don't disable pipe or pipe PLLs if needed */
1733 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1734 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
63d7bbe9
JB
1735 return;
1736
1737 /* Make sure the pipe isn't still relying on us */
1738 assert_pipe_disabled(dev_priv, pipe);
1739
b8afb911 1740 I915_WRITE(DPLL(pipe), DPLL_VGA_MODE_DIS);
50b44a44 1741 POSTING_READ(DPLL(pipe));
63d7bbe9
JB
1742}
1743
f6071166
JB
1744static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1745{
b8afb911 1746 u32 val;
f6071166
JB
1747
1748 /* Make sure the pipe isn't still relying on us */
1749 assert_pipe_disabled(dev_priv, pipe);
1750
03ed5cbf
VS
1751 val = DPLL_INTEGRATED_REF_CLK_VLV |
1752 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
1753 if (pipe != PIPE_A)
1754 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
1755
f6071166
JB
1756 I915_WRITE(DPLL(pipe), val);
1757 POSTING_READ(DPLL(pipe));
076ed3b2
CML
1758}
1759
1760static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1761{
d752048d 1762 enum dpio_channel port = vlv_pipe_to_channel(pipe);
076ed3b2
CML
1763 u32 val;
1764
a11b0703
VS
1765 /* Make sure the pipe isn't still relying on us */
1766 assert_pipe_disabled(dev_priv, pipe);
076ed3b2 1767
60bfe44f
VS
1768 val = DPLL_SSC_REF_CLK_CHV |
1769 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
a11b0703
VS
1770 if (pipe != PIPE_A)
1771 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
03ed5cbf 1772
a11b0703
VS
1773 I915_WRITE(DPLL(pipe), val);
1774 POSTING_READ(DPLL(pipe));
d752048d 1775
a580516d 1776 mutex_lock(&dev_priv->sb_lock);
d752048d
VS
1777
1778 /* Disable 10bit clock to display controller */
1779 val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1780 val &= ~DPIO_DCLKP_EN;
1781 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
1782
a580516d 1783 mutex_unlock(&dev_priv->sb_lock);
f6071166
JB
1784}
1785
e4607fcf 1786void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
9b6de0a1
VS
1787 struct intel_digital_port *dport,
1788 unsigned int expected_mask)
89b667f8
JB
1789{
1790 u32 port_mask;
f0f59a00 1791 i915_reg_t dpll_reg;
89b667f8 1792
e4607fcf
CML
1793 switch (dport->port) {
1794 case PORT_B:
89b667f8 1795 port_mask = DPLL_PORTB_READY_MASK;
00fc31b7 1796 dpll_reg = DPLL(0);
e4607fcf
CML
1797 break;
1798 case PORT_C:
89b667f8 1799 port_mask = DPLL_PORTC_READY_MASK;
00fc31b7 1800 dpll_reg = DPLL(0);
9b6de0a1 1801 expected_mask <<= 4;
00fc31b7
CML
1802 break;
1803 case PORT_D:
1804 port_mask = DPLL_PORTD_READY_MASK;
1805 dpll_reg = DPIO_PHY_STATUS;
e4607fcf
CML
1806 break;
1807 default:
1808 BUG();
1809 }
89b667f8 1810
9b6de0a1
VS
1811 if (wait_for((I915_READ(dpll_reg) & port_mask) == expected_mask, 1000))
1812 WARN(1, "timed out waiting for port %c ready: got 0x%x, expected 0x%x\n",
1813 port_name(dport->port), I915_READ(dpll_reg) & port_mask, expected_mask);
89b667f8
JB
1814}
1815
b8a4f404
PZ
1816static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1817 enum pipe pipe)
040484af 1818{
23670b32 1819 struct drm_device *dev = dev_priv->dev;
7c26e5c6 1820 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
e2b78267 1821 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
f0f59a00
VS
1822 i915_reg_t reg;
1823 uint32_t val, pipeconf_val;
040484af 1824
040484af 1825 /* Make sure PCH DPLL is enabled */
8106ddbd 1826 assert_shared_dpll_enabled(dev_priv, intel_crtc->config->shared_dpll);
040484af
JB
1827
1828 /* FDI must be feeding us bits for PCH ports */
1829 assert_fdi_tx_enabled(dev_priv, pipe);
1830 assert_fdi_rx_enabled(dev_priv, pipe);
1831
23670b32
DV
1832 if (HAS_PCH_CPT(dev)) {
1833 /* Workaround: Set the timing override bit before enabling the
1834 * pch transcoder. */
1835 reg = TRANS_CHICKEN2(pipe);
1836 val = I915_READ(reg);
1837 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
1838 I915_WRITE(reg, val);
59c859d6 1839 }
23670b32 1840
ab9412ba 1841 reg = PCH_TRANSCONF(pipe);
040484af 1842 val = I915_READ(reg);
5f7f726d 1843 pipeconf_val = I915_READ(PIPECONF(pipe));
e9bcff5c 1844
2d1fe073 1845 if (HAS_PCH_IBX(dev_priv)) {
e9bcff5c 1846 /*
c5de7c6f
VS
1847 * Make the BPC in transcoder be consistent with
1848 * that in pipeconf reg. For HDMI we must use 8bpc
1849 * here for both 8bpc and 12bpc.
e9bcff5c 1850 */
dfd07d72 1851 val &= ~PIPECONF_BPC_MASK;
c5de7c6f
VS
1852 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_HDMI))
1853 val |= PIPECONF_8BPC;
1854 else
1855 val |= pipeconf_val & PIPECONF_BPC_MASK;
e9bcff5c 1856 }
5f7f726d
PZ
1857
1858 val &= ~TRANS_INTERLACE_MASK;
1859 if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
2d1fe073 1860 if (HAS_PCH_IBX(dev_priv) &&
409ee761 1861 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
7c26e5c6
PZ
1862 val |= TRANS_LEGACY_INTERLACED_ILK;
1863 else
1864 val |= TRANS_INTERLACED;
5f7f726d
PZ
1865 else
1866 val |= TRANS_PROGRESSIVE;
1867
040484af
JB
1868 I915_WRITE(reg, val | TRANS_ENABLE);
1869 if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
4bb6f1f3 1870 DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
040484af
JB
1871}
1872
8fb033d7 1873static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
937bb610 1874 enum transcoder cpu_transcoder)
040484af 1875{
8fb033d7 1876 u32 val, pipeconf_val;
8fb033d7 1877
8fb033d7 1878 /* FDI must be feeding us bits for PCH ports */
1a240d4d 1879 assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
937bb610 1880 assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
8fb033d7 1881
223a6fdf 1882 /* Workaround: set timing override bit. */
36c0d0cf 1883 val = I915_READ(TRANS_CHICKEN2(PIPE_A));
23670b32 1884 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
36c0d0cf 1885 I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
223a6fdf 1886
25f3ef11 1887 val = TRANS_ENABLE;
937bb610 1888 pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
8fb033d7 1889
9a76b1c6
PZ
1890 if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
1891 PIPECONF_INTERLACED_ILK)
a35f2679 1892 val |= TRANS_INTERLACED;
8fb033d7
PZ
1893 else
1894 val |= TRANS_PROGRESSIVE;
1895
ab9412ba
DV
1896 I915_WRITE(LPT_TRANSCONF, val);
1897 if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
937bb610 1898 DRM_ERROR("Failed to enable PCH transcoder\n");
8fb033d7
PZ
1899}
1900
b8a4f404
PZ
1901static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
1902 enum pipe pipe)
040484af 1903{
23670b32 1904 struct drm_device *dev = dev_priv->dev;
f0f59a00
VS
1905 i915_reg_t reg;
1906 uint32_t val;
040484af
JB
1907
1908 /* FDI relies on the transcoder */
1909 assert_fdi_tx_disabled(dev_priv, pipe);
1910 assert_fdi_rx_disabled(dev_priv, pipe);
1911
291906f1
JB
1912 /* Ports must be off as well */
1913 assert_pch_ports_disabled(dev_priv, pipe);
1914
ab9412ba 1915 reg = PCH_TRANSCONF(pipe);
040484af
JB
1916 val = I915_READ(reg);
1917 val &= ~TRANS_ENABLE;
1918 I915_WRITE(reg, val);
1919 /* wait for PCH transcoder off, transcoder state */
1920 if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
4bb6f1f3 1921 DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
23670b32 1922
c465613b 1923 if (HAS_PCH_CPT(dev)) {
23670b32
DV
1924 /* Workaround: Clear the timing override chicken bit again. */
1925 reg = TRANS_CHICKEN2(pipe);
1926 val = I915_READ(reg);
1927 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
1928 I915_WRITE(reg, val);
1929 }
040484af
JB
1930}
1931
ab4d966c 1932static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
8fb033d7 1933{
8fb033d7
PZ
1934 u32 val;
1935
ab9412ba 1936 val = I915_READ(LPT_TRANSCONF);
8fb033d7 1937 val &= ~TRANS_ENABLE;
ab9412ba 1938 I915_WRITE(LPT_TRANSCONF, val);
8fb033d7 1939 /* wait for PCH transcoder off, transcoder state */
ab9412ba 1940 if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
8a52fd9f 1941 DRM_ERROR("Failed to disable PCH transcoder\n");
223a6fdf
PZ
1942
1943 /* Workaround: clear timing override bit. */
36c0d0cf 1944 val = I915_READ(TRANS_CHICKEN2(PIPE_A));
23670b32 1945 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
36c0d0cf 1946 I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
040484af
JB
1947}
1948
b24e7179 1949/**
309cfea8 1950 * intel_enable_pipe - enable a pipe, asserting requirements
0372264a 1951 * @crtc: crtc responsible for the pipe
b24e7179 1952 *
0372264a 1953 * Enable @crtc's pipe, making sure that various hardware specific requirements
b24e7179 1954 * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
b24e7179 1955 */
e1fdc473 1956static void intel_enable_pipe(struct intel_crtc *crtc)
b24e7179 1957{
0372264a
PZ
1958 struct drm_device *dev = crtc->base.dev;
1959 struct drm_i915_private *dev_priv = dev->dev_private;
1960 enum pipe pipe = crtc->pipe;
1a70a728 1961 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
1a240d4d 1962 enum pipe pch_transcoder;
f0f59a00 1963 i915_reg_t reg;
b24e7179
JB
1964 u32 val;
1965
9e2ee2dd
VS
1966 DRM_DEBUG_KMS("enabling pipe %c\n", pipe_name(pipe));
1967
58c6eaa2 1968 assert_planes_disabled(dev_priv, pipe);
93ce0ba6 1969 assert_cursor_disabled(dev_priv, pipe);
58c6eaa2
DV
1970 assert_sprites_disabled(dev_priv, pipe);
1971
2d1fe073 1972 if (HAS_PCH_LPT(dev_priv))
cc391bbb
PZ
1973 pch_transcoder = TRANSCODER_A;
1974 else
1975 pch_transcoder = pipe;
1976
b24e7179
JB
1977 /*
1978 * A pipe without a PLL won't actually be able to drive bits from
1979 * a plane. On ILK+ the pipe PLLs are integrated, so we don't
1980 * need the check.
1981 */
2d1fe073 1982 if (HAS_GMCH_DISPLAY(dev_priv))
a65347ba 1983 if (crtc->config->has_dsi_encoder)
23538ef1
JN
1984 assert_dsi_pll_enabled(dev_priv);
1985 else
1986 assert_pll_enabled(dev_priv, pipe);
040484af 1987 else {
6e3c9717 1988 if (crtc->config->has_pch_encoder) {
040484af 1989 /* if driving the PCH, we need FDI enabled */
cc391bbb 1990 assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
1a240d4d
DV
1991 assert_fdi_tx_pll_enabled(dev_priv,
1992 (enum pipe) cpu_transcoder);
040484af
JB
1993 }
1994 /* FIXME: assert CPU port conditions for SNB+ */
1995 }
b24e7179 1996
702e7a56 1997 reg = PIPECONF(cpu_transcoder);
b24e7179 1998 val = I915_READ(reg);
7ad25d48 1999 if (val & PIPECONF_ENABLE) {
b6b5d049
VS
2000 WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
2001 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
00d70b15 2002 return;
7ad25d48 2003 }
00d70b15
CW
2004
2005 I915_WRITE(reg, val | PIPECONF_ENABLE);
851855d8 2006 POSTING_READ(reg);
b7792d8b
VS
2007
2008 /*
2009 * Until the pipe starts DSL will read as 0, which would cause
2010 * an apparent vblank timestamp jump, which messes up also the
2011 * frame count when it's derived from the timestamps. So let's
2012 * wait for the pipe to start properly before we call
2013 * drm_crtc_vblank_on()
2014 */
2015 if (dev->max_vblank_count == 0 &&
2016 wait_for(intel_get_crtc_scanline(crtc) != crtc->scanline_offset, 50))
2017 DRM_ERROR("pipe %c didn't start\n", pipe_name(pipe));
b24e7179
JB
2018}
2019
2020/**
309cfea8 2021 * intel_disable_pipe - disable a pipe, asserting requirements
575f7ab7 2022 * @crtc: crtc whose pipes is to be disabled
b24e7179 2023 *
575f7ab7
VS
2024 * Disable the pipe of @crtc, making sure that various hardware
2025 * specific requirements are met, if applicable, e.g. plane
2026 * disabled, panel fitter off, etc.
b24e7179
JB
2027 *
2028 * Will wait until the pipe has shut down before returning.
2029 */
575f7ab7 2030static void intel_disable_pipe(struct intel_crtc *crtc)
b24e7179 2031{
575f7ab7 2032 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
6e3c9717 2033 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
575f7ab7 2034 enum pipe pipe = crtc->pipe;
f0f59a00 2035 i915_reg_t reg;
b24e7179
JB
2036 u32 val;
2037
9e2ee2dd
VS
2038 DRM_DEBUG_KMS("disabling pipe %c\n", pipe_name(pipe));
2039
b24e7179
JB
2040 /*
2041 * Make sure planes won't keep trying to pump pixels to us,
2042 * or we might hang the display.
2043 */
2044 assert_planes_disabled(dev_priv, pipe);
93ce0ba6 2045 assert_cursor_disabled(dev_priv, pipe);
19332d7a 2046 assert_sprites_disabled(dev_priv, pipe);
b24e7179 2047
702e7a56 2048 reg = PIPECONF(cpu_transcoder);
b24e7179 2049 val = I915_READ(reg);
00d70b15
CW
2050 if ((val & PIPECONF_ENABLE) == 0)
2051 return;
2052
67adc644
VS
2053 /*
2054 * Double wide has implications for planes
2055 * so best keep it disabled when not needed.
2056 */
6e3c9717 2057 if (crtc->config->double_wide)
67adc644
VS
2058 val &= ~PIPECONF_DOUBLE_WIDE;
2059
2060 /* Don't disable pipe or pipe PLLs if needed */
b6b5d049
VS
2061 if (!(pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) &&
2062 !(pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
67adc644
VS
2063 val &= ~PIPECONF_ENABLE;
2064
2065 I915_WRITE(reg, val);
2066 if ((val & PIPECONF_ENABLE) == 0)
2067 intel_wait_for_pipe_off(crtc);
b24e7179
JB
2068}
2069
693db184
CW
2070static bool need_vtd_wa(struct drm_device *dev)
2071{
2072#ifdef CONFIG_INTEL_IOMMU
2073 if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
2074 return true;
2075#endif
2076 return false;
2077}
2078
832be82f
VS
2079static unsigned int intel_tile_size(const struct drm_i915_private *dev_priv)
2080{
2081 return IS_GEN2(dev_priv) ? 2048 : 4096;
2082}
2083
27ba3910
VS
2084static unsigned int intel_tile_width_bytes(const struct drm_i915_private *dev_priv,
2085 uint64_t fb_modifier, unsigned int cpp)
7b49f948
VS
2086{
2087 switch (fb_modifier) {
2088 case DRM_FORMAT_MOD_NONE:
2089 return cpp;
2090 case I915_FORMAT_MOD_X_TILED:
2091 if (IS_GEN2(dev_priv))
2092 return 128;
2093 else
2094 return 512;
2095 case I915_FORMAT_MOD_Y_TILED:
2096 if (IS_GEN2(dev_priv) || HAS_128_BYTE_Y_TILING(dev_priv))
2097 return 128;
2098 else
2099 return 512;
2100 case I915_FORMAT_MOD_Yf_TILED:
2101 switch (cpp) {
2102 case 1:
2103 return 64;
2104 case 2:
2105 case 4:
2106 return 128;
2107 case 8:
2108 case 16:
2109 return 256;
2110 default:
2111 MISSING_CASE(cpp);
2112 return cpp;
2113 }
2114 break;
2115 default:
2116 MISSING_CASE(fb_modifier);
2117 return cpp;
2118 }
2119}
2120
832be82f
VS
2121unsigned int intel_tile_height(const struct drm_i915_private *dev_priv,
2122 uint64_t fb_modifier, unsigned int cpp)
a57ce0b2 2123{
832be82f
VS
2124 if (fb_modifier == DRM_FORMAT_MOD_NONE)
2125 return 1;
2126 else
2127 return intel_tile_size(dev_priv) /
27ba3910 2128 intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
6761dd31
TU
2129}
2130
8d0deca8
VS
2131/* Return the tile dimensions in pixel units */
2132static void intel_tile_dims(const struct drm_i915_private *dev_priv,
2133 unsigned int *tile_width,
2134 unsigned int *tile_height,
2135 uint64_t fb_modifier,
2136 unsigned int cpp)
2137{
2138 unsigned int tile_width_bytes =
2139 intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
2140
2141 *tile_width = tile_width_bytes / cpp;
2142 *tile_height = intel_tile_size(dev_priv) / tile_width_bytes;
2143}
2144
6761dd31
TU
2145unsigned int
2146intel_fb_align_height(struct drm_device *dev, unsigned int height,
832be82f 2147 uint32_t pixel_format, uint64_t fb_modifier)
6761dd31 2148{
832be82f
VS
2149 unsigned int cpp = drm_format_plane_cpp(pixel_format, 0);
2150 unsigned int tile_height = intel_tile_height(to_i915(dev), fb_modifier, cpp);
2151
2152 return ALIGN(height, tile_height);
a57ce0b2
JB
2153}
2154
1663b9d6
VS
2155unsigned int intel_rotation_info_size(const struct intel_rotation_info *rot_info)
2156{
2157 unsigned int size = 0;
2158 int i;
2159
2160 for (i = 0 ; i < ARRAY_SIZE(rot_info->plane); i++)
2161 size += rot_info->plane[i].width * rot_info->plane[i].height;
2162
2163 return size;
2164}
2165
75c82a53 2166static void
3465c580
VS
2167intel_fill_fb_ggtt_view(struct i915_ggtt_view *view,
2168 const struct drm_framebuffer *fb,
2169 unsigned int rotation)
f64b98cd 2170{
2d7a215f
VS
2171 if (intel_rotation_90_or_270(rotation)) {
2172 *view = i915_ggtt_view_rotated;
2173 view->params.rotated = to_intel_framebuffer(fb)->rot_info;
2174 } else {
2175 *view = i915_ggtt_view_normal;
2176 }
2177}
50470bb0 2178
2d7a215f
VS
2179static void
2180intel_fill_fb_info(struct drm_i915_private *dev_priv,
2181 struct drm_framebuffer *fb)
2182{
2183 struct intel_rotation_info *info = &to_intel_framebuffer(fb)->rot_info;
2184 unsigned int tile_size, tile_width, tile_height, cpp;
50470bb0 2185
d9b3288e
VS
2186 tile_size = intel_tile_size(dev_priv);
2187
2188 cpp = drm_format_plane_cpp(fb->pixel_format, 0);
8d0deca8
VS
2189 intel_tile_dims(dev_priv, &tile_width, &tile_height,
2190 fb->modifier[0], cpp);
d9b3288e 2191
1663b9d6
VS
2192 info->plane[0].width = DIV_ROUND_UP(fb->pitches[0], tile_width * cpp);
2193 info->plane[0].height = DIV_ROUND_UP(fb->height, tile_height);
84fe03f7 2194
89e3e142 2195 if (info->pixel_format == DRM_FORMAT_NV12) {
832be82f 2196 cpp = drm_format_plane_cpp(fb->pixel_format, 1);
8d0deca8
VS
2197 intel_tile_dims(dev_priv, &tile_width, &tile_height,
2198 fb->modifier[1], cpp);
d9b3288e 2199
2d7a215f 2200 info->uv_offset = fb->offsets[1];
1663b9d6
VS
2201 info->plane[1].width = DIV_ROUND_UP(fb->pitches[1], tile_width * cpp);
2202 info->plane[1].height = DIV_ROUND_UP(fb->height / 2, tile_height);
89e3e142 2203 }
f64b98cd
TU
2204}
2205
603525d7 2206static unsigned int intel_linear_alignment(const struct drm_i915_private *dev_priv)
4e9a86b6
VS
2207{
2208 if (INTEL_INFO(dev_priv)->gen >= 9)
2209 return 256 * 1024;
985b8bb4 2210 else if (IS_BROADWATER(dev_priv) || IS_CRESTLINE(dev_priv) ||
666a4537 2211 IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
4e9a86b6
VS
2212 return 128 * 1024;
2213 else if (INTEL_INFO(dev_priv)->gen >= 4)
2214 return 4 * 1024;
2215 else
44c5905e 2216 return 0;
4e9a86b6
VS
2217}
2218
603525d7
VS
2219static unsigned int intel_surf_alignment(const struct drm_i915_private *dev_priv,
2220 uint64_t fb_modifier)
2221{
2222 switch (fb_modifier) {
2223 case DRM_FORMAT_MOD_NONE:
2224 return intel_linear_alignment(dev_priv);
2225 case I915_FORMAT_MOD_X_TILED:
2226 if (INTEL_INFO(dev_priv)->gen >= 9)
2227 return 256 * 1024;
2228 return 0;
2229 case I915_FORMAT_MOD_Y_TILED:
2230 case I915_FORMAT_MOD_Yf_TILED:
2231 return 1 * 1024 * 1024;
2232 default:
2233 MISSING_CASE(fb_modifier);
2234 return 0;
2235 }
2236}
2237
127bd2ac 2238int
3465c580
VS
2239intel_pin_and_fence_fb_obj(struct drm_framebuffer *fb,
2240 unsigned int rotation)
6b95a207 2241{
850c4cdc 2242 struct drm_device *dev = fb->dev;
ce453d81 2243 struct drm_i915_private *dev_priv = dev->dev_private;
850c4cdc 2244 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
f64b98cd 2245 struct i915_ggtt_view view;
6b95a207
KH
2246 u32 alignment;
2247 int ret;
2248
ebcdd39e
MR
2249 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
2250
603525d7 2251 alignment = intel_surf_alignment(dev_priv, fb->modifier[0]);
6b95a207 2252
3465c580 2253 intel_fill_fb_ggtt_view(&view, fb, rotation);
f64b98cd 2254
693db184
CW
2255 /* Note that the w/a also requires 64 PTE of padding following the
2256 * bo. We currently fill all unused PTE with the shadow page and so
2257 * we should always have valid PTE following the scanout preventing
2258 * the VT-d warning.
2259 */
2260 if (need_vtd_wa(dev) && alignment < 256 * 1024)
2261 alignment = 256 * 1024;
2262
d6dd6843
PZ
2263 /*
2264 * Global gtt pte registers are special registers which actually forward
2265 * writes to a chunk of system memory. Which means that there is no risk
2266 * that the register values disappear as soon as we call
2267 * intel_runtime_pm_put(), so it is correct to wrap only the
2268 * pin/unpin/fence and not more.
2269 */
2270 intel_runtime_pm_get(dev_priv);
2271
7580d774
ML
2272 ret = i915_gem_object_pin_to_display_plane(obj, alignment,
2273 &view);
48b956c5 2274 if (ret)
b26a6b35 2275 goto err_pm;
6b95a207
KH
2276
2277 /* Install a fence for tiled scan-out. Pre-i965 always needs a
2278 * fence, whereas 965+ only requires a fence if using
2279 * framebuffer compression. For simplicity, we always install
2280 * a fence as the cost is not that onerous.
2281 */
9807216f
VK
2282 if (view.type == I915_GGTT_VIEW_NORMAL) {
2283 ret = i915_gem_object_get_fence(obj);
2284 if (ret == -EDEADLK) {
2285 /*
2286 * -EDEADLK means there are no free fences
2287 * no pending flips.
2288 *
2289 * This is propagated to atomic, but it uses
2290 * -EDEADLK to force a locking recovery, so
2291 * change the returned error to -EBUSY.
2292 */
2293 ret = -EBUSY;
2294 goto err_unpin;
2295 } else if (ret)
2296 goto err_unpin;
1690e1eb 2297
9807216f
VK
2298 i915_gem_object_pin_fence(obj);
2299 }
6b95a207 2300
d6dd6843 2301 intel_runtime_pm_put(dev_priv);
6b95a207 2302 return 0;
48b956c5
CW
2303
2304err_unpin:
f64b98cd 2305 i915_gem_object_unpin_from_display_plane(obj, &view);
b26a6b35 2306err_pm:
d6dd6843 2307 intel_runtime_pm_put(dev_priv);
48b956c5 2308 return ret;
6b95a207
KH
2309}
2310
fb4b8ce1 2311void intel_unpin_fb_obj(struct drm_framebuffer *fb, unsigned int rotation)
1690e1eb 2312{
82bc3b2d 2313 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
f64b98cd 2314 struct i915_ggtt_view view;
82bc3b2d 2315
ebcdd39e
MR
2316 WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
2317
3465c580 2318 intel_fill_fb_ggtt_view(&view, fb, rotation);
f64b98cd 2319
9807216f
VK
2320 if (view.type == I915_GGTT_VIEW_NORMAL)
2321 i915_gem_object_unpin_fence(obj);
2322
f64b98cd 2323 i915_gem_object_unpin_from_display_plane(obj, &view);
1690e1eb
CW
2324}
2325
29cf9491
VS
2326/*
2327 * Adjust the tile offset by moving the difference into
2328 * the x/y offsets.
2329 *
2330 * Input tile dimensions and pitch must already be
2331 * rotated to match x and y, and in pixel units.
2332 */
2333static u32 intel_adjust_tile_offset(int *x, int *y,
2334 unsigned int tile_width,
2335 unsigned int tile_height,
2336 unsigned int tile_size,
2337 unsigned int pitch_tiles,
2338 u32 old_offset,
2339 u32 new_offset)
2340{
2341 unsigned int tiles;
2342
2343 WARN_ON(old_offset & (tile_size - 1));
2344 WARN_ON(new_offset & (tile_size - 1));
2345 WARN_ON(new_offset > old_offset);
2346
2347 tiles = (old_offset - new_offset) / tile_size;
2348
2349 *y += tiles / pitch_tiles * tile_height;
2350 *x += tiles % pitch_tiles * tile_width;
2351
2352 return new_offset;
2353}
2354
8d0deca8
VS
2355/*
2356 * Computes the linear offset to the base tile and adjusts
2357 * x, y. bytes per pixel is assumed to be a power-of-two.
2358 *
2359 * In the 90/270 rotated case, x and y are assumed
2360 * to be already rotated to match the rotated GTT view, and
2361 * pitch is the tile_height aligned framebuffer height.
2362 */
4f2d9934
VS
2363u32 intel_compute_tile_offset(int *x, int *y,
2364 const struct drm_framebuffer *fb, int plane,
8d0deca8
VS
2365 unsigned int pitch,
2366 unsigned int rotation)
c2c75131 2367{
4f2d9934
VS
2368 const struct drm_i915_private *dev_priv = to_i915(fb->dev);
2369 uint64_t fb_modifier = fb->modifier[plane];
2370 unsigned int cpp = drm_format_plane_cpp(fb->pixel_format, plane);
29cf9491
VS
2371 u32 offset, offset_aligned, alignment;
2372
2373 alignment = intel_surf_alignment(dev_priv, fb_modifier);
2374 if (alignment)
2375 alignment--;
2376
b5c65338 2377 if (fb_modifier != DRM_FORMAT_MOD_NONE) {
8d0deca8
VS
2378 unsigned int tile_size, tile_width, tile_height;
2379 unsigned int tile_rows, tiles, pitch_tiles;
c2c75131 2380
d843310d 2381 tile_size = intel_tile_size(dev_priv);
8d0deca8
VS
2382 intel_tile_dims(dev_priv, &tile_width, &tile_height,
2383 fb_modifier, cpp);
2384
2385 if (intel_rotation_90_or_270(rotation)) {
2386 pitch_tiles = pitch / tile_height;
2387 swap(tile_width, tile_height);
2388 } else {
2389 pitch_tiles = pitch / (tile_width * cpp);
2390 }
d843310d
VS
2391
2392 tile_rows = *y / tile_height;
2393 *y %= tile_height;
c2c75131 2394
8d0deca8
VS
2395 tiles = *x / tile_width;
2396 *x %= tile_width;
bc752862 2397
29cf9491
VS
2398 offset = (tile_rows * pitch_tiles + tiles) * tile_size;
2399 offset_aligned = offset & ~alignment;
bc752862 2400
29cf9491
VS
2401 intel_adjust_tile_offset(x, y, tile_width, tile_height,
2402 tile_size, pitch_tiles,
2403 offset, offset_aligned);
2404 } else {
bc752862 2405 offset = *y * pitch + *x * cpp;
29cf9491
VS
2406 offset_aligned = offset & ~alignment;
2407
4e9a86b6
VS
2408 *y = (offset & alignment) / pitch;
2409 *x = ((offset & alignment) - *y * pitch) / cpp;
bc752862 2410 }
29cf9491
VS
2411
2412 return offset_aligned;
c2c75131
DV
2413}
2414
b35d63fa 2415static int i9xx_format_to_fourcc(int format)
46f297fb
JB
2416{
2417 switch (format) {
2418 case DISPPLANE_8BPP:
2419 return DRM_FORMAT_C8;
2420 case DISPPLANE_BGRX555:
2421 return DRM_FORMAT_XRGB1555;
2422 case DISPPLANE_BGRX565:
2423 return DRM_FORMAT_RGB565;
2424 default:
2425 case DISPPLANE_BGRX888:
2426 return DRM_FORMAT_XRGB8888;
2427 case DISPPLANE_RGBX888:
2428 return DRM_FORMAT_XBGR8888;
2429 case DISPPLANE_BGRX101010:
2430 return DRM_FORMAT_XRGB2101010;
2431 case DISPPLANE_RGBX101010:
2432 return DRM_FORMAT_XBGR2101010;
2433 }
2434}
2435
bc8d7dff
DL
2436static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
2437{
2438 switch (format) {
2439 case PLANE_CTL_FORMAT_RGB_565:
2440 return DRM_FORMAT_RGB565;
2441 default:
2442 case PLANE_CTL_FORMAT_XRGB_8888:
2443 if (rgb_order) {
2444 if (alpha)
2445 return DRM_FORMAT_ABGR8888;
2446 else
2447 return DRM_FORMAT_XBGR8888;
2448 } else {
2449 if (alpha)
2450 return DRM_FORMAT_ARGB8888;
2451 else
2452 return DRM_FORMAT_XRGB8888;
2453 }
2454 case PLANE_CTL_FORMAT_XRGB_2101010:
2455 if (rgb_order)
2456 return DRM_FORMAT_XBGR2101010;
2457 else
2458 return DRM_FORMAT_XRGB2101010;
2459 }
2460}
2461
5724dbd1 2462static bool
f6936e29
DV
2463intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
2464 struct intel_initial_plane_config *plane_config)
46f297fb
JB
2465{
2466 struct drm_device *dev = crtc->base.dev;
3badb49f 2467 struct drm_i915_private *dev_priv = to_i915(dev);
72e96d64 2468 struct i915_ggtt *ggtt = &dev_priv->ggtt;
46f297fb
JB
2469 struct drm_i915_gem_object *obj = NULL;
2470 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
2d14030b 2471 struct drm_framebuffer *fb = &plane_config->fb->base;
f37b5c2b
DV
2472 u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
2473 u32 size_aligned = round_up(plane_config->base + plane_config->size,
2474 PAGE_SIZE);
2475
2476 size_aligned -= base_aligned;
46f297fb 2477
ff2652ea
CW
2478 if (plane_config->size == 0)
2479 return false;
2480
3badb49f
PZ
2481 /* If the FB is too big, just don't use it since fbdev is not very
2482 * important and we should probably use that space with FBC or other
2483 * features. */
72e96d64 2484 if (size_aligned * 2 > ggtt->stolen_usable_size)
3badb49f
PZ
2485 return false;
2486
12c83d99
TU
2487 mutex_lock(&dev->struct_mutex);
2488
f37b5c2b
DV
2489 obj = i915_gem_object_create_stolen_for_preallocated(dev,
2490 base_aligned,
2491 base_aligned,
2492 size_aligned);
12c83d99
TU
2493 if (!obj) {
2494 mutex_unlock(&dev->struct_mutex);
484b41dd 2495 return false;
12c83d99 2496 }
46f297fb 2497
49af449b
DL
2498 obj->tiling_mode = plane_config->tiling;
2499 if (obj->tiling_mode == I915_TILING_X)
6bf129df 2500 obj->stride = fb->pitches[0];
46f297fb 2501
6bf129df
DL
2502 mode_cmd.pixel_format = fb->pixel_format;
2503 mode_cmd.width = fb->width;
2504 mode_cmd.height = fb->height;
2505 mode_cmd.pitches[0] = fb->pitches[0];
18c5247e
DV
2506 mode_cmd.modifier[0] = fb->modifier[0];
2507 mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
46f297fb 2508
6bf129df 2509 if (intel_framebuffer_init(dev, to_intel_framebuffer(fb),
484b41dd 2510 &mode_cmd, obj)) {
46f297fb
JB
2511 DRM_DEBUG_KMS("intel fb init failed\n");
2512 goto out_unref_obj;
2513 }
12c83d99 2514
46f297fb 2515 mutex_unlock(&dev->struct_mutex);
484b41dd 2516
f6936e29 2517 DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
484b41dd 2518 return true;
46f297fb
JB
2519
2520out_unref_obj:
2521 drm_gem_object_unreference(&obj->base);
2522 mutex_unlock(&dev->struct_mutex);
484b41dd
JB
2523 return false;
2524}
2525
5724dbd1 2526static void
f6936e29
DV
2527intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
2528 struct intel_initial_plane_config *plane_config)
484b41dd
JB
2529{
2530 struct drm_device *dev = intel_crtc->base.dev;
d9ceb816 2531 struct drm_i915_private *dev_priv = dev->dev_private;
484b41dd
JB
2532 struct drm_crtc *c;
2533 struct intel_crtc *i;
2ff8fde1 2534 struct drm_i915_gem_object *obj;
88595ac9 2535 struct drm_plane *primary = intel_crtc->base.primary;
be5651f2 2536 struct drm_plane_state *plane_state = primary->state;
200757f5
MR
2537 struct drm_crtc_state *crtc_state = intel_crtc->base.state;
2538 struct intel_plane *intel_plane = to_intel_plane(primary);
0a8d8a86
MR
2539 struct intel_plane_state *intel_state =
2540 to_intel_plane_state(plane_state);
88595ac9 2541 struct drm_framebuffer *fb;
484b41dd 2542
2d14030b 2543 if (!plane_config->fb)
484b41dd
JB
2544 return;
2545
f6936e29 2546 if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
88595ac9
DV
2547 fb = &plane_config->fb->base;
2548 goto valid_fb;
f55548b5 2549 }
484b41dd 2550
2d14030b 2551 kfree(plane_config->fb);
484b41dd
JB
2552
2553 /*
2554 * Failed to alloc the obj, check to see if we should share
2555 * an fb with another CRTC instead
2556 */
70e1e0ec 2557 for_each_crtc(dev, c) {
484b41dd
JB
2558 i = to_intel_crtc(c);
2559
2560 if (c == &intel_crtc->base)
2561 continue;
2562
2ff8fde1
MR
2563 if (!i->active)
2564 continue;
2565
88595ac9
DV
2566 fb = c->primary->fb;
2567 if (!fb)
484b41dd
JB
2568 continue;
2569
88595ac9 2570 obj = intel_fb_obj(fb);
2ff8fde1 2571 if (i915_gem_obj_ggtt_offset(obj) == plane_config->base) {
88595ac9
DV
2572 drm_framebuffer_reference(fb);
2573 goto valid_fb;
484b41dd
JB
2574 }
2575 }
88595ac9 2576
200757f5
MR
2577 /*
2578 * We've failed to reconstruct the BIOS FB. Current display state
2579 * indicates that the primary plane is visible, but has a NULL FB,
2580 * which will lead to problems later if we don't fix it up. The
2581 * simplest solution is to just disable the primary plane now and
2582 * pretend the BIOS never had it enabled.
2583 */
2584 to_intel_plane_state(plane_state)->visible = false;
2585 crtc_state->plane_mask &= ~(1 << drm_plane_index(primary));
2622a081 2586 intel_pre_disable_primary_noatomic(&intel_crtc->base);
200757f5
MR
2587 intel_plane->disable_plane(primary, &intel_crtc->base);
2588
88595ac9
DV
2589 return;
2590
2591valid_fb:
f44e2659
VS
2592 plane_state->src_x = 0;
2593 plane_state->src_y = 0;
be5651f2
ML
2594 plane_state->src_w = fb->width << 16;
2595 plane_state->src_h = fb->height << 16;
2596
f44e2659
VS
2597 plane_state->crtc_x = 0;
2598 plane_state->crtc_y = 0;
be5651f2
ML
2599 plane_state->crtc_w = fb->width;
2600 plane_state->crtc_h = fb->height;
2601
0a8d8a86
MR
2602 intel_state->src.x1 = plane_state->src_x;
2603 intel_state->src.y1 = plane_state->src_y;
2604 intel_state->src.x2 = plane_state->src_x + plane_state->src_w;
2605 intel_state->src.y2 = plane_state->src_y + plane_state->src_h;
2606 intel_state->dst.x1 = plane_state->crtc_x;
2607 intel_state->dst.y1 = plane_state->crtc_y;
2608 intel_state->dst.x2 = plane_state->crtc_x + plane_state->crtc_w;
2609 intel_state->dst.y2 = plane_state->crtc_y + plane_state->crtc_h;
2610
88595ac9
DV
2611 obj = intel_fb_obj(fb);
2612 if (obj->tiling_mode != I915_TILING_NONE)
2613 dev_priv->preserve_bios_swizzle = true;
2614
be5651f2
ML
2615 drm_framebuffer_reference(fb);
2616 primary->fb = primary->state->fb = fb;
36750f28 2617 primary->crtc = primary->state->crtc = &intel_crtc->base;
36750f28 2618 intel_crtc->base.state->plane_mask |= (1 << drm_plane_index(primary));
a9ff8714 2619 obj->frontbuffer_bits |= to_intel_plane(primary)->frontbuffer_bit;
46f297fb
JB
2620}
2621
a8d201af
ML
2622static void i9xx_update_primary_plane(struct drm_plane *primary,
2623 const struct intel_crtc_state *crtc_state,
2624 const struct intel_plane_state *plane_state)
81255565 2625{
a8d201af 2626 struct drm_device *dev = primary->dev;
81255565 2627 struct drm_i915_private *dev_priv = dev->dev_private;
a8d201af
ML
2628 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
2629 struct drm_framebuffer *fb = plane_state->base.fb;
2630 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
81255565 2631 int plane = intel_crtc->plane;
54ea9da8 2632 u32 linear_offset;
81255565 2633 u32 dspcntr;
f0f59a00 2634 i915_reg_t reg = DSPCNTR(plane);
8d0deca8 2635 unsigned int rotation = plane_state->base.rotation;
ac484963 2636 int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
54ea9da8
VS
2637 int x = plane_state->src.x1 >> 16;
2638 int y = plane_state->src.y1 >> 16;
c9ba6fad 2639
f45651ba
VS
2640 dspcntr = DISPPLANE_GAMMA_ENABLE;
2641
fdd508a6 2642 dspcntr |= DISPLAY_PLANE_ENABLE;
f45651ba
VS
2643
2644 if (INTEL_INFO(dev)->gen < 4) {
2645 if (intel_crtc->pipe == PIPE_B)
2646 dspcntr |= DISPPLANE_SEL_PIPE_B;
2647
2648 /* pipesrc and dspsize control the size that is scaled from,
2649 * which should always be the user's requested size.
2650 */
2651 I915_WRITE(DSPSIZE(plane),
a8d201af
ML
2652 ((crtc_state->pipe_src_h - 1) << 16) |
2653 (crtc_state->pipe_src_w - 1));
f45651ba 2654 I915_WRITE(DSPPOS(plane), 0);
c14b0485
VS
2655 } else if (IS_CHERRYVIEW(dev) && plane == PLANE_B) {
2656 I915_WRITE(PRIMSIZE(plane),
a8d201af
ML
2657 ((crtc_state->pipe_src_h - 1) << 16) |
2658 (crtc_state->pipe_src_w - 1));
c14b0485
VS
2659 I915_WRITE(PRIMPOS(plane), 0);
2660 I915_WRITE(PRIMCNSTALPHA(plane), 0);
f45651ba 2661 }
81255565 2662
57779d06
VS
2663 switch (fb->pixel_format) {
2664 case DRM_FORMAT_C8:
81255565
JB
2665 dspcntr |= DISPPLANE_8BPP;
2666 break;
57779d06 2667 case DRM_FORMAT_XRGB1555:
57779d06 2668 dspcntr |= DISPPLANE_BGRX555;
81255565 2669 break;
57779d06
VS
2670 case DRM_FORMAT_RGB565:
2671 dspcntr |= DISPPLANE_BGRX565;
2672 break;
2673 case DRM_FORMAT_XRGB8888:
57779d06
VS
2674 dspcntr |= DISPPLANE_BGRX888;
2675 break;
2676 case DRM_FORMAT_XBGR8888:
57779d06
VS
2677 dspcntr |= DISPPLANE_RGBX888;
2678 break;
2679 case DRM_FORMAT_XRGB2101010:
57779d06
VS
2680 dspcntr |= DISPPLANE_BGRX101010;
2681 break;
2682 case DRM_FORMAT_XBGR2101010:
57779d06 2683 dspcntr |= DISPPLANE_RGBX101010;
81255565
JB
2684 break;
2685 default:
baba133a 2686 BUG();
81255565 2687 }
57779d06 2688
f45651ba
VS
2689 if (INTEL_INFO(dev)->gen >= 4 &&
2690 obj->tiling_mode != I915_TILING_NONE)
2691 dspcntr |= DISPPLANE_TILED;
81255565 2692
de1aa629
VS
2693 if (IS_G4X(dev))
2694 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
2695
ac484963 2696 linear_offset = y * fb->pitches[0] + x * cpp;
81255565 2697
c2c75131
DV
2698 if (INTEL_INFO(dev)->gen >= 4) {
2699 intel_crtc->dspaddr_offset =
4f2d9934 2700 intel_compute_tile_offset(&x, &y, fb, 0,
8d0deca8 2701 fb->pitches[0], rotation);
c2c75131
DV
2702 linear_offset -= intel_crtc->dspaddr_offset;
2703 } else {
e506a0c6 2704 intel_crtc->dspaddr_offset = linear_offset;
c2c75131 2705 }
e506a0c6 2706
8d0deca8 2707 if (rotation == BIT(DRM_ROTATE_180)) {
48404c1e
SJ
2708 dspcntr |= DISPPLANE_ROTATE_180;
2709
a8d201af
ML
2710 x += (crtc_state->pipe_src_w - 1);
2711 y += (crtc_state->pipe_src_h - 1);
48404c1e
SJ
2712
2713 /* Finding the last pixel of the last line of the display
2714 data and adding to linear_offset*/
2715 linear_offset +=
a8d201af 2716 (crtc_state->pipe_src_h - 1) * fb->pitches[0] +
ac484963 2717 (crtc_state->pipe_src_w - 1) * cpp;
48404c1e
SJ
2718 }
2719
2db3366b
PZ
2720 intel_crtc->adjusted_x = x;
2721 intel_crtc->adjusted_y = y;
2722
48404c1e
SJ
2723 I915_WRITE(reg, dspcntr);
2724
01f2c773 2725 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
a6c45cf0 2726 if (INTEL_INFO(dev)->gen >= 4) {
85ba7b7d
DV
2727 I915_WRITE(DSPSURF(plane),
2728 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
5eddb70b 2729 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
e506a0c6 2730 I915_WRITE(DSPLINOFF(plane), linear_offset);
5eddb70b 2731 } else
f343c5f6 2732 I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
5eddb70b 2733 POSTING_READ(reg);
17638cd6
JB
2734}
2735
a8d201af
ML
2736static void i9xx_disable_primary_plane(struct drm_plane *primary,
2737 struct drm_crtc *crtc)
17638cd6
JB
2738{
2739 struct drm_device *dev = crtc->dev;
2740 struct drm_i915_private *dev_priv = dev->dev_private;
2741 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
17638cd6 2742 int plane = intel_crtc->plane;
f45651ba 2743
a8d201af
ML
2744 I915_WRITE(DSPCNTR(plane), 0);
2745 if (INTEL_INFO(dev_priv)->gen >= 4)
fdd508a6 2746 I915_WRITE(DSPSURF(plane), 0);
a8d201af
ML
2747 else
2748 I915_WRITE(DSPADDR(plane), 0);
2749 POSTING_READ(DSPCNTR(plane));
2750}
c9ba6fad 2751
a8d201af
ML
2752static void ironlake_update_primary_plane(struct drm_plane *primary,
2753 const struct intel_crtc_state *crtc_state,
2754 const struct intel_plane_state *plane_state)
2755{
2756 struct drm_device *dev = primary->dev;
2757 struct drm_i915_private *dev_priv = dev->dev_private;
2758 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
2759 struct drm_framebuffer *fb = plane_state->base.fb;
2760 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
2761 int plane = intel_crtc->plane;
54ea9da8 2762 u32 linear_offset;
a8d201af
ML
2763 u32 dspcntr;
2764 i915_reg_t reg = DSPCNTR(plane);
8d0deca8 2765 unsigned int rotation = plane_state->base.rotation;
ac484963 2766 int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
a8d201af
ML
2767 int x = plane_state->src.x1 >> 16;
2768 int y = plane_state->src.y1 >> 16;
c9ba6fad 2769
f45651ba 2770 dspcntr = DISPPLANE_GAMMA_ENABLE;
fdd508a6 2771 dspcntr |= DISPLAY_PLANE_ENABLE;
f45651ba
VS
2772
2773 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
2774 dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
17638cd6 2775
57779d06
VS
2776 switch (fb->pixel_format) {
2777 case DRM_FORMAT_C8:
17638cd6
JB
2778 dspcntr |= DISPPLANE_8BPP;
2779 break;
57779d06
VS
2780 case DRM_FORMAT_RGB565:
2781 dspcntr |= DISPPLANE_BGRX565;
17638cd6 2782 break;
57779d06 2783 case DRM_FORMAT_XRGB8888:
57779d06
VS
2784 dspcntr |= DISPPLANE_BGRX888;
2785 break;
2786 case DRM_FORMAT_XBGR8888:
57779d06
VS
2787 dspcntr |= DISPPLANE_RGBX888;
2788 break;
2789 case DRM_FORMAT_XRGB2101010:
57779d06
VS
2790 dspcntr |= DISPPLANE_BGRX101010;
2791 break;
2792 case DRM_FORMAT_XBGR2101010:
57779d06 2793 dspcntr |= DISPPLANE_RGBX101010;
17638cd6
JB
2794 break;
2795 default:
baba133a 2796 BUG();
17638cd6
JB
2797 }
2798
2799 if (obj->tiling_mode != I915_TILING_NONE)
2800 dspcntr |= DISPPLANE_TILED;
17638cd6 2801
f45651ba 2802 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev))
1f5d76db 2803 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
17638cd6 2804
ac484963 2805 linear_offset = y * fb->pitches[0] + x * cpp;
c2c75131 2806 intel_crtc->dspaddr_offset =
4f2d9934 2807 intel_compute_tile_offset(&x, &y, fb, 0,
8d0deca8 2808 fb->pitches[0], rotation);
c2c75131 2809 linear_offset -= intel_crtc->dspaddr_offset;
8d0deca8 2810 if (rotation == BIT(DRM_ROTATE_180)) {
48404c1e
SJ
2811 dspcntr |= DISPPLANE_ROTATE_180;
2812
2813 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev)) {
a8d201af
ML
2814 x += (crtc_state->pipe_src_w - 1);
2815 y += (crtc_state->pipe_src_h - 1);
48404c1e
SJ
2816
2817 /* Finding the last pixel of the last line of the display
2818 data and adding to linear_offset*/
2819 linear_offset +=
a8d201af 2820 (crtc_state->pipe_src_h - 1) * fb->pitches[0] +
ac484963 2821 (crtc_state->pipe_src_w - 1) * cpp;
48404c1e
SJ
2822 }
2823 }
2824
2db3366b
PZ
2825 intel_crtc->adjusted_x = x;
2826 intel_crtc->adjusted_y = y;
2827
48404c1e 2828 I915_WRITE(reg, dspcntr);
17638cd6 2829
01f2c773 2830 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
85ba7b7d
DV
2831 I915_WRITE(DSPSURF(plane),
2832 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
b3dc685e 2833 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
bc1c91eb
DL
2834 I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
2835 } else {
2836 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2837 I915_WRITE(DSPLINOFF(plane), linear_offset);
2838 }
17638cd6 2839 POSTING_READ(reg);
17638cd6
JB
2840}
2841
7b49f948
VS
2842u32 intel_fb_stride_alignment(const struct drm_i915_private *dev_priv,
2843 uint64_t fb_modifier, uint32_t pixel_format)
b321803d 2844{
7b49f948 2845 if (fb_modifier == DRM_FORMAT_MOD_NONE) {
b321803d 2846 return 64;
7b49f948
VS
2847 } else {
2848 int cpp = drm_format_plane_cpp(pixel_format, 0);
2849
27ba3910 2850 return intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
b321803d
DL
2851 }
2852}
2853
44eb0cb9
MK
2854u32 intel_plane_obj_offset(struct intel_plane *intel_plane,
2855 struct drm_i915_gem_object *obj,
2856 unsigned int plane)
121920fa 2857{
ce7f1728 2858 struct i915_ggtt_view view;
dedf278c 2859 struct i915_vma *vma;
44eb0cb9 2860 u64 offset;
121920fa 2861
e7941294 2862 intel_fill_fb_ggtt_view(&view, intel_plane->base.state->fb,
3465c580 2863 intel_plane->base.state->rotation);
121920fa 2864
ce7f1728 2865 vma = i915_gem_obj_to_ggtt_view(obj, &view);
dedf278c 2866 if (WARN(!vma, "ggtt vma for display object not found! (view=%u)\n",
ce7f1728 2867 view.type))
dedf278c
TU
2868 return -1;
2869
44eb0cb9 2870 offset = vma->node.start;
dedf278c
TU
2871
2872 if (plane == 1) {
7723f47d 2873 offset += vma->ggtt_view.params.rotated.uv_start_page *
dedf278c
TU
2874 PAGE_SIZE;
2875 }
2876
44eb0cb9
MK
2877 WARN_ON(upper_32_bits(offset));
2878
2879 return lower_32_bits(offset);
121920fa
TU
2880}
2881
e435d6e5
ML
2882static void skl_detach_scaler(struct intel_crtc *intel_crtc, int id)
2883{
2884 struct drm_device *dev = intel_crtc->base.dev;
2885 struct drm_i915_private *dev_priv = dev->dev_private;
2886
2887 I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, id), 0);
2888 I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, id), 0);
2889 I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, id), 0);
e435d6e5
ML
2890}
2891
a1b2278e
CK
2892/*
2893 * This function detaches (aka. unbinds) unused scalers in hardware
2894 */
0583236e 2895static void skl_detach_scalers(struct intel_crtc *intel_crtc)
a1b2278e 2896{
a1b2278e
CK
2897 struct intel_crtc_scaler_state *scaler_state;
2898 int i;
2899
a1b2278e
CK
2900 scaler_state = &intel_crtc->config->scaler_state;
2901
2902 /* loop through and disable scalers that aren't in use */
2903 for (i = 0; i < intel_crtc->num_scalers; i++) {
e435d6e5
ML
2904 if (!scaler_state->scalers[i].in_use)
2905 skl_detach_scaler(intel_crtc, i);
a1b2278e
CK
2906 }
2907}
2908
6156a456 2909u32 skl_plane_ctl_format(uint32_t pixel_format)
70d21f0e 2910{
6156a456 2911 switch (pixel_format) {
d161cf7a 2912 case DRM_FORMAT_C8:
c34ce3d1 2913 return PLANE_CTL_FORMAT_INDEXED;
70d21f0e 2914 case DRM_FORMAT_RGB565:
c34ce3d1 2915 return PLANE_CTL_FORMAT_RGB_565;
70d21f0e 2916 case DRM_FORMAT_XBGR8888:
c34ce3d1 2917 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
6156a456 2918 case DRM_FORMAT_XRGB8888:
c34ce3d1 2919 return PLANE_CTL_FORMAT_XRGB_8888;
6156a456
CK
2920 /*
2921 * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
2922 * to be already pre-multiplied. We need to add a knob (or a different
2923 * DRM_FORMAT) for user-space to configure that.
2924 */
f75fb42a 2925 case DRM_FORMAT_ABGR8888:
c34ce3d1 2926 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX |
6156a456 2927 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
6156a456 2928 case DRM_FORMAT_ARGB8888:
c34ce3d1 2929 return PLANE_CTL_FORMAT_XRGB_8888 |
6156a456 2930 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
70d21f0e 2931 case DRM_FORMAT_XRGB2101010:
c34ce3d1 2932 return PLANE_CTL_FORMAT_XRGB_2101010;
70d21f0e 2933 case DRM_FORMAT_XBGR2101010:
c34ce3d1 2934 return PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
6156a456 2935 case DRM_FORMAT_YUYV:
c34ce3d1 2936 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
6156a456 2937 case DRM_FORMAT_YVYU:
c34ce3d1 2938 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
6156a456 2939 case DRM_FORMAT_UYVY:
c34ce3d1 2940 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
6156a456 2941 case DRM_FORMAT_VYUY:
c34ce3d1 2942 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
70d21f0e 2943 default:
4249eeef 2944 MISSING_CASE(pixel_format);
70d21f0e 2945 }
8cfcba41 2946
c34ce3d1 2947 return 0;
6156a456 2948}
70d21f0e 2949
6156a456
CK
2950u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
2951{
6156a456 2952 switch (fb_modifier) {
30af77c4 2953 case DRM_FORMAT_MOD_NONE:
70d21f0e 2954 break;
30af77c4 2955 case I915_FORMAT_MOD_X_TILED:
c34ce3d1 2956 return PLANE_CTL_TILED_X;
b321803d 2957 case I915_FORMAT_MOD_Y_TILED:
c34ce3d1 2958 return PLANE_CTL_TILED_Y;
b321803d 2959 case I915_FORMAT_MOD_Yf_TILED:
c34ce3d1 2960 return PLANE_CTL_TILED_YF;
70d21f0e 2961 default:
6156a456 2962 MISSING_CASE(fb_modifier);
70d21f0e 2963 }
8cfcba41 2964
c34ce3d1 2965 return 0;
6156a456 2966}
70d21f0e 2967
6156a456
CK
2968u32 skl_plane_ctl_rotation(unsigned int rotation)
2969{
3b7a5119 2970 switch (rotation) {
6156a456
CK
2971 case BIT(DRM_ROTATE_0):
2972 break;
1e8df167
SJ
2973 /*
2974 * DRM_ROTATE_ is counter clockwise to stay compatible with Xrandr
2975 * while i915 HW rotation is clockwise, thats why this swapping.
2976 */
3b7a5119 2977 case BIT(DRM_ROTATE_90):
1e8df167 2978 return PLANE_CTL_ROTATE_270;
3b7a5119 2979 case BIT(DRM_ROTATE_180):
c34ce3d1 2980 return PLANE_CTL_ROTATE_180;
3b7a5119 2981 case BIT(DRM_ROTATE_270):
1e8df167 2982 return PLANE_CTL_ROTATE_90;
6156a456
CK
2983 default:
2984 MISSING_CASE(rotation);
2985 }
2986
c34ce3d1 2987 return 0;
6156a456
CK
2988}
2989
a8d201af
ML
2990static void skylake_update_primary_plane(struct drm_plane *plane,
2991 const struct intel_crtc_state *crtc_state,
2992 const struct intel_plane_state *plane_state)
6156a456 2993{
a8d201af 2994 struct drm_device *dev = plane->dev;
6156a456 2995 struct drm_i915_private *dev_priv = dev->dev_private;
a8d201af
ML
2996 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
2997 struct drm_framebuffer *fb = plane_state->base.fb;
2998 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
6156a456
CK
2999 int pipe = intel_crtc->pipe;
3000 u32 plane_ctl, stride_div, stride;
3001 u32 tile_height, plane_offset, plane_size;
a8d201af 3002 unsigned int rotation = plane_state->base.rotation;
6156a456 3003 int x_offset, y_offset;
44eb0cb9 3004 u32 surf_addr;
a8d201af
ML
3005 int scaler_id = plane_state->scaler_id;
3006 int src_x = plane_state->src.x1 >> 16;
3007 int src_y = plane_state->src.y1 >> 16;
3008 int src_w = drm_rect_width(&plane_state->src) >> 16;
3009 int src_h = drm_rect_height(&plane_state->src) >> 16;
3010 int dst_x = plane_state->dst.x1;
3011 int dst_y = plane_state->dst.y1;
3012 int dst_w = drm_rect_width(&plane_state->dst);
3013 int dst_h = drm_rect_height(&plane_state->dst);
70d21f0e 3014
6156a456
CK
3015 plane_ctl = PLANE_CTL_ENABLE |
3016 PLANE_CTL_PIPE_GAMMA_ENABLE |
3017 PLANE_CTL_PIPE_CSC_ENABLE;
3018
3019 plane_ctl |= skl_plane_ctl_format(fb->pixel_format);
3020 plane_ctl |= skl_plane_ctl_tiling(fb->modifier[0]);
3021 plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
6156a456
CK
3022 plane_ctl |= skl_plane_ctl_rotation(rotation);
3023
7b49f948 3024 stride_div = intel_fb_stride_alignment(dev_priv, fb->modifier[0],
b321803d 3025 fb->pixel_format);
dedf278c 3026 surf_addr = intel_plane_obj_offset(to_intel_plane(plane), obj, 0);
3b7a5119 3027
a42e5a23
PZ
3028 WARN_ON(drm_rect_width(&plane_state->src) == 0);
3029
3b7a5119 3030 if (intel_rotation_90_or_270(rotation)) {
832be82f
VS
3031 int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
3032
3b7a5119 3033 /* stride = Surface height in tiles */
832be82f 3034 tile_height = intel_tile_height(dev_priv, fb->modifier[0], cpp);
3b7a5119 3035 stride = DIV_ROUND_UP(fb->height, tile_height);
a8d201af
ML
3036 x_offset = stride * tile_height - src_y - src_h;
3037 y_offset = src_x;
6156a456 3038 plane_size = (src_w - 1) << 16 | (src_h - 1);
3b7a5119
SJ
3039 } else {
3040 stride = fb->pitches[0] / stride_div;
a8d201af
ML
3041 x_offset = src_x;
3042 y_offset = src_y;
6156a456 3043 plane_size = (src_h - 1) << 16 | (src_w - 1);
3b7a5119
SJ
3044 }
3045 plane_offset = y_offset << 16 | x_offset;
b321803d 3046
2db3366b
PZ
3047 intel_crtc->adjusted_x = x_offset;
3048 intel_crtc->adjusted_y = y_offset;
3049
70d21f0e 3050 I915_WRITE(PLANE_CTL(pipe, 0), plane_ctl);
3b7a5119
SJ
3051 I915_WRITE(PLANE_OFFSET(pipe, 0), plane_offset);
3052 I915_WRITE(PLANE_SIZE(pipe, 0), plane_size);
3053 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
6156a456
CK
3054
3055 if (scaler_id >= 0) {
3056 uint32_t ps_ctrl = 0;
3057
3058 WARN_ON(!dst_w || !dst_h);
3059 ps_ctrl = PS_SCALER_EN | PS_PLANE_SEL(0) |
3060 crtc_state->scaler_state.scalers[scaler_id].mode;
3061 I915_WRITE(SKL_PS_CTRL(pipe, scaler_id), ps_ctrl);
3062 I915_WRITE(SKL_PS_PWR_GATE(pipe, scaler_id), 0);
3063 I915_WRITE(SKL_PS_WIN_POS(pipe, scaler_id), (dst_x << 16) | dst_y);
3064 I915_WRITE(SKL_PS_WIN_SZ(pipe, scaler_id), (dst_w << 16) | dst_h);
3065 I915_WRITE(PLANE_POS(pipe, 0), 0);
3066 } else {
3067 I915_WRITE(PLANE_POS(pipe, 0), (dst_y << 16) | dst_x);
3068 }
3069
121920fa 3070 I915_WRITE(PLANE_SURF(pipe, 0), surf_addr);
70d21f0e
DL
3071
3072 POSTING_READ(PLANE_SURF(pipe, 0));
3073}
3074
a8d201af
ML
3075static void skylake_disable_primary_plane(struct drm_plane *primary,
3076 struct drm_crtc *crtc)
17638cd6
JB
3077{
3078 struct drm_device *dev = crtc->dev;
3079 struct drm_i915_private *dev_priv = dev->dev_private;
a8d201af 3080 int pipe = to_intel_crtc(crtc)->pipe;
17638cd6 3081
a8d201af
ML
3082 I915_WRITE(PLANE_CTL(pipe, 0), 0);
3083 I915_WRITE(PLANE_SURF(pipe, 0), 0);
3084 POSTING_READ(PLANE_SURF(pipe, 0));
3085}
29b9bde6 3086
a8d201af
ML
3087/* Assume fb object is pinned & idle & fenced and just update base pointers */
3088static int
3089intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
3090 int x, int y, enum mode_set_atomic state)
3091{
3092 /* Support for kgdboc is disabled, this needs a major rework. */
3093 DRM_ERROR("legacy panic handler not supported any more.\n");
3094
3095 return -ENODEV;
81255565
JB
3096}
3097
7514747d
VS
3098static void intel_update_primary_planes(struct drm_device *dev)
3099{
7514747d 3100 struct drm_crtc *crtc;
96a02917 3101
70e1e0ec 3102 for_each_crtc(dev, crtc) {
11c22da6
ML
3103 struct intel_plane *plane = to_intel_plane(crtc->primary);
3104 struct intel_plane_state *plane_state;
96a02917 3105
11c22da6 3106 drm_modeset_lock_crtc(crtc, &plane->base);
11c22da6
ML
3107 plane_state = to_intel_plane_state(plane->base.state);
3108
a8d201af
ML
3109 if (plane_state->visible)
3110 plane->update_plane(&plane->base,
3111 to_intel_crtc_state(crtc->state),
3112 plane_state);
11c22da6
ML
3113
3114 drm_modeset_unlock_crtc(crtc);
96a02917
VS
3115 }
3116}
3117
c033666a 3118void intel_prepare_reset(struct drm_i915_private *dev_priv)
7514747d
VS
3119{
3120 /* no reset support for gen2 */
c033666a 3121 if (IS_GEN2(dev_priv))
7514747d
VS
3122 return;
3123
3124 /* reset doesn't touch the display */
c033666a 3125 if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
7514747d
VS
3126 return;
3127
c033666a 3128 drm_modeset_lock_all(dev_priv->dev);
f98ce92f
VS
3129 /*
3130 * Disabling the crtcs gracefully seems nicer. Also the
3131 * g33 docs say we should at least disable all the planes.
3132 */
c033666a 3133 intel_display_suspend(dev_priv->dev);
7514747d
VS
3134}
3135
c033666a 3136void intel_finish_reset(struct drm_i915_private *dev_priv)
7514747d 3137{
7514747d 3138 /* no reset support for gen2 */
c033666a 3139 if (IS_GEN2(dev_priv))
7514747d
VS
3140 return;
3141
3142 /* reset doesn't touch the display */
c033666a 3143 if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv)) {
7514747d
VS
3144 /*
3145 * Flips in the rings have been nuked by the reset,
3146 * so update the base address of all primary
3147 * planes to the the last fb to make sure we're
3148 * showing the correct fb after a reset.
11c22da6
ML
3149 *
3150 * FIXME: Atomic will make this obsolete since we won't schedule
3151 * CS-based flips (which might get lost in gpu resets) any more.
7514747d 3152 */
c033666a 3153 intel_update_primary_planes(dev_priv->dev);
7514747d
VS
3154 return;
3155 }
3156
3157 /*
3158 * The display has been reset as well,
3159 * so need a full re-initialization.
3160 */
3161 intel_runtime_pm_disable_interrupts(dev_priv);
3162 intel_runtime_pm_enable_interrupts(dev_priv);
3163
c033666a 3164 intel_modeset_init_hw(dev_priv->dev);
7514747d
VS
3165
3166 spin_lock_irq(&dev_priv->irq_lock);
3167 if (dev_priv->display.hpd_irq_setup)
91d14251 3168 dev_priv->display.hpd_irq_setup(dev_priv);
7514747d
VS
3169 spin_unlock_irq(&dev_priv->irq_lock);
3170
c033666a 3171 intel_display_resume(dev_priv->dev);
7514747d
VS
3172
3173 intel_hpd_init(dev_priv);
3174
c033666a 3175 drm_modeset_unlock_all(dev_priv->dev);
7514747d
VS
3176}
3177
7d5e3799
CW
3178static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
3179{
6885843a 3180 return !list_empty_careful(&to_intel_crtc(crtc)->flip_work);
7d5e3799
CW
3181}
3182
bfd16b2a
ML
3183static void intel_update_pipe_config(struct intel_crtc *crtc,
3184 struct intel_crtc_state *old_crtc_state)
e30e8f75
GP
3185{
3186 struct drm_device *dev = crtc->base.dev;
3187 struct drm_i915_private *dev_priv = dev->dev_private;
bfd16b2a
ML
3188 struct intel_crtc_state *pipe_config =
3189 to_intel_crtc_state(crtc->base.state);
e30e8f75 3190
bfd16b2a
ML
3191 /* drm_atomic_helper_update_legacy_modeset_state might not be called. */
3192 crtc->base.mode = crtc->base.state->mode;
3193
3194 DRM_DEBUG_KMS("Updating pipe size %ix%i -> %ix%i\n",
3195 old_crtc_state->pipe_src_w, old_crtc_state->pipe_src_h,
3196 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
e30e8f75
GP
3197
3198 /*
3199 * Update pipe size and adjust fitter if needed: the reason for this is
3200 * that in compute_mode_changes we check the native mode (not the pfit
3201 * mode) to see if we can flip rather than do a full mode set. In the
3202 * fastboot case, we'll flip, but if we don't update the pipesrc and
3203 * pfit state, we'll end up with a big fb scanned out into the wrong
3204 * sized surface.
e30e8f75
GP
3205 */
3206
e30e8f75 3207 I915_WRITE(PIPESRC(crtc->pipe),
bfd16b2a
ML
3208 ((pipe_config->pipe_src_w - 1) << 16) |
3209 (pipe_config->pipe_src_h - 1));
3210
3211 /* on skylake this is done by detaching scalers */
3212 if (INTEL_INFO(dev)->gen >= 9) {
3213 skl_detach_scalers(crtc);
3214
3215 if (pipe_config->pch_pfit.enabled)
3216 skylake_pfit_enable(crtc);
3217 } else if (HAS_PCH_SPLIT(dev)) {
3218 if (pipe_config->pch_pfit.enabled)
3219 ironlake_pfit_enable(crtc);
3220 else if (old_crtc_state->pch_pfit.enabled)
3221 ironlake_pfit_disable(crtc, true);
e30e8f75 3222 }
e30e8f75
GP
3223}
3224
5e84e1a4
ZW
3225static void intel_fdi_normal_train(struct drm_crtc *crtc)
3226{
3227 struct drm_device *dev = crtc->dev;
3228 struct drm_i915_private *dev_priv = dev->dev_private;
3229 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3230 int pipe = intel_crtc->pipe;
f0f59a00
VS
3231 i915_reg_t reg;
3232 u32 temp;
5e84e1a4
ZW
3233
3234 /* enable normal train */
3235 reg = FDI_TX_CTL(pipe);
3236 temp = I915_READ(reg);
61e499bf 3237 if (IS_IVYBRIDGE(dev)) {
357555c0
JB
3238 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3239 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
61e499bf
KP
3240 } else {
3241 temp &= ~FDI_LINK_TRAIN_NONE;
3242 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
357555c0 3243 }
5e84e1a4
ZW
3244 I915_WRITE(reg, temp);
3245
3246 reg = FDI_RX_CTL(pipe);
3247 temp = I915_READ(reg);
3248 if (HAS_PCH_CPT(dev)) {
3249 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3250 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
3251 } else {
3252 temp &= ~FDI_LINK_TRAIN_NONE;
3253 temp |= FDI_LINK_TRAIN_NONE;
3254 }
3255 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
3256
3257 /* wait one idle pattern time */
3258 POSTING_READ(reg);
3259 udelay(1000);
357555c0
JB
3260
3261 /* IVB wants error correction enabled */
3262 if (IS_IVYBRIDGE(dev))
3263 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
3264 FDI_FE_ERRC_ENABLE);
5e84e1a4
ZW
3265}
3266
8db9d77b
ZW
3267/* The FDI link training functions for ILK/Ibexpeak. */
3268static void ironlake_fdi_link_train(struct drm_crtc *crtc)
3269{
3270 struct drm_device *dev = crtc->dev;
3271 struct drm_i915_private *dev_priv = dev->dev_private;
3272 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3273 int pipe = intel_crtc->pipe;
f0f59a00
VS
3274 i915_reg_t reg;
3275 u32 temp, tries;
8db9d77b 3276
1c8562f6 3277 /* FDI needs bits from pipe first */
0fc932b8 3278 assert_pipe_enabled(dev_priv, pipe);
0fc932b8 3279
e1a44743
AJ
3280 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3281 for train result */
5eddb70b
CW
3282 reg = FDI_RX_IMR(pipe);
3283 temp = I915_READ(reg);
e1a44743
AJ
3284 temp &= ~FDI_RX_SYMBOL_LOCK;
3285 temp &= ~FDI_RX_BIT_LOCK;
5eddb70b
CW
3286 I915_WRITE(reg, temp);
3287 I915_READ(reg);
e1a44743
AJ
3288 udelay(150);
3289
8db9d77b 3290 /* enable CPU FDI TX and PCH FDI RX */
5eddb70b
CW
3291 reg = FDI_TX_CTL(pipe);
3292 temp = I915_READ(reg);
627eb5a3 3293 temp &= ~FDI_DP_PORT_WIDTH_MASK;
6e3c9717 3294 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
8db9d77b
ZW
3295 temp &= ~FDI_LINK_TRAIN_NONE;
3296 temp |= FDI_LINK_TRAIN_PATTERN_1;
5eddb70b 3297 I915_WRITE(reg, temp | FDI_TX_ENABLE);
8db9d77b 3298
5eddb70b
CW
3299 reg = FDI_RX_CTL(pipe);
3300 temp = I915_READ(reg);
8db9d77b
ZW
3301 temp &= ~FDI_LINK_TRAIN_NONE;
3302 temp |= FDI_LINK_TRAIN_PATTERN_1;
5eddb70b
CW
3303 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3304
3305 POSTING_READ(reg);
8db9d77b
ZW
3306 udelay(150);
3307
5b2adf89 3308 /* Ironlake workaround, enable clock pointer after FDI enable*/
8f5718a6
DV
3309 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
3310 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
3311 FDI_RX_PHASE_SYNC_POINTER_EN);
5b2adf89 3312
5eddb70b 3313 reg = FDI_RX_IIR(pipe);
e1a44743 3314 for (tries = 0; tries < 5; tries++) {
5eddb70b 3315 temp = I915_READ(reg);
8db9d77b
ZW
3316 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3317
3318 if ((temp & FDI_RX_BIT_LOCK)) {
3319 DRM_DEBUG_KMS("FDI train 1 done.\n");
5eddb70b 3320 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
8db9d77b
ZW
3321 break;
3322 }
8db9d77b 3323 }
e1a44743 3324 if (tries == 5)
5eddb70b 3325 DRM_ERROR("FDI train 1 fail!\n");
8db9d77b
ZW
3326
3327 /* Train 2 */
5eddb70b
CW
3328 reg = FDI_TX_CTL(pipe);
3329 temp = I915_READ(reg);
8db9d77b
ZW
3330 temp &= ~FDI_LINK_TRAIN_NONE;
3331 temp |= FDI_LINK_TRAIN_PATTERN_2;
5eddb70b 3332 I915_WRITE(reg, temp);
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_2;
5eddb70b 3338 I915_WRITE(reg, temp);
8db9d77b 3339
5eddb70b
CW
3340 POSTING_READ(reg);
3341 udelay(150);
8db9d77b 3342
5eddb70b 3343 reg = FDI_RX_IIR(pipe);
e1a44743 3344 for (tries = 0; tries < 5; tries++) {
5eddb70b 3345 temp = I915_READ(reg);
8db9d77b
ZW
3346 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3347
3348 if (temp & FDI_RX_SYMBOL_LOCK) {
5eddb70b 3349 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
8db9d77b
ZW
3350 DRM_DEBUG_KMS("FDI train 2 done.\n");
3351 break;
3352 }
8db9d77b 3353 }
e1a44743 3354 if (tries == 5)
5eddb70b 3355 DRM_ERROR("FDI train 2 fail!\n");
8db9d77b
ZW
3356
3357 DRM_DEBUG_KMS("FDI train done\n");
5c5313c8 3358
8db9d77b
ZW
3359}
3360
0206e353 3361static const int snb_b_fdi_train_param[] = {
8db9d77b
ZW
3362 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
3363 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
3364 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
3365 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
3366};
3367
3368/* The FDI link training functions for SNB/Cougarpoint. */
3369static void gen6_fdi_link_train(struct drm_crtc *crtc)
3370{
3371 struct drm_device *dev = crtc->dev;
3372 struct drm_i915_private *dev_priv = dev->dev_private;
3373 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3374 int pipe = intel_crtc->pipe;
f0f59a00
VS
3375 i915_reg_t reg;
3376 u32 temp, i, retry;
8db9d77b 3377
e1a44743
AJ
3378 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3379 for train result */
5eddb70b
CW
3380 reg = FDI_RX_IMR(pipe);
3381 temp = I915_READ(reg);
e1a44743
AJ
3382 temp &= ~FDI_RX_SYMBOL_LOCK;
3383 temp &= ~FDI_RX_BIT_LOCK;
5eddb70b
CW
3384 I915_WRITE(reg, temp);
3385
3386 POSTING_READ(reg);
e1a44743
AJ
3387 udelay(150);
3388
8db9d77b 3389 /* enable CPU FDI TX and PCH FDI RX */
5eddb70b
CW
3390 reg = FDI_TX_CTL(pipe);
3391 temp = I915_READ(reg);
627eb5a3 3392 temp &= ~FDI_DP_PORT_WIDTH_MASK;
6e3c9717 3393 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
8db9d77b
ZW
3394 temp &= ~FDI_LINK_TRAIN_NONE;
3395 temp |= FDI_LINK_TRAIN_PATTERN_1;
3396 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3397 /* SNB-B */
3398 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
5eddb70b 3399 I915_WRITE(reg, temp | FDI_TX_ENABLE);
8db9d77b 3400
d74cf324
DV
3401 I915_WRITE(FDI_RX_MISC(pipe),
3402 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3403
5eddb70b
CW
3404 reg = FDI_RX_CTL(pipe);
3405 temp = I915_READ(reg);
8db9d77b
ZW
3406 if (HAS_PCH_CPT(dev)) {
3407 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3408 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3409 } else {
3410 temp &= ~FDI_LINK_TRAIN_NONE;
3411 temp |= FDI_LINK_TRAIN_PATTERN_1;
3412 }
5eddb70b
CW
3413 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3414
3415 POSTING_READ(reg);
8db9d77b
ZW
3416 udelay(150);
3417
0206e353 3418 for (i = 0; i < 4; i++) {
5eddb70b
CW
3419 reg = FDI_TX_CTL(pipe);
3420 temp = I915_READ(reg);
8db9d77b
ZW
3421 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3422 temp |= snb_b_fdi_train_param[i];
5eddb70b
CW
3423 I915_WRITE(reg, temp);
3424
3425 POSTING_READ(reg);
8db9d77b
ZW
3426 udelay(500);
3427
fa37d39e
SP
3428 for (retry = 0; retry < 5; retry++) {
3429 reg = FDI_RX_IIR(pipe);
3430 temp = I915_READ(reg);
3431 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3432 if (temp & FDI_RX_BIT_LOCK) {
3433 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3434 DRM_DEBUG_KMS("FDI train 1 done.\n");
3435 break;
3436 }
3437 udelay(50);
8db9d77b 3438 }
fa37d39e
SP
3439 if (retry < 5)
3440 break;
8db9d77b
ZW
3441 }
3442 if (i == 4)
5eddb70b 3443 DRM_ERROR("FDI train 1 fail!\n");
8db9d77b
ZW
3444
3445 /* Train 2 */
5eddb70b
CW
3446 reg = FDI_TX_CTL(pipe);
3447 temp = I915_READ(reg);
8db9d77b
ZW
3448 temp &= ~FDI_LINK_TRAIN_NONE;
3449 temp |= FDI_LINK_TRAIN_PATTERN_2;
3450 if (IS_GEN6(dev)) {
3451 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3452 /* SNB-B */
3453 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
3454 }
5eddb70b 3455 I915_WRITE(reg, temp);
8db9d77b 3456
5eddb70b
CW
3457 reg = FDI_RX_CTL(pipe);
3458 temp = I915_READ(reg);
8db9d77b
ZW
3459 if (HAS_PCH_CPT(dev)) {
3460 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3461 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
3462 } else {
3463 temp &= ~FDI_LINK_TRAIN_NONE;
3464 temp |= FDI_LINK_TRAIN_PATTERN_2;
3465 }
5eddb70b
CW
3466 I915_WRITE(reg, temp);
3467
3468 POSTING_READ(reg);
8db9d77b
ZW
3469 udelay(150);
3470
0206e353 3471 for (i = 0; i < 4; i++) {
5eddb70b
CW
3472 reg = FDI_TX_CTL(pipe);
3473 temp = I915_READ(reg);
8db9d77b
ZW
3474 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3475 temp |= snb_b_fdi_train_param[i];
5eddb70b
CW
3476 I915_WRITE(reg, temp);
3477
3478 POSTING_READ(reg);
8db9d77b
ZW
3479 udelay(500);
3480
fa37d39e
SP
3481 for (retry = 0; retry < 5; retry++) {
3482 reg = FDI_RX_IIR(pipe);
3483 temp = I915_READ(reg);
3484 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3485 if (temp & FDI_RX_SYMBOL_LOCK) {
3486 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3487 DRM_DEBUG_KMS("FDI train 2 done.\n");
3488 break;
3489 }
3490 udelay(50);
8db9d77b 3491 }
fa37d39e
SP
3492 if (retry < 5)
3493 break;
8db9d77b
ZW
3494 }
3495 if (i == 4)
5eddb70b 3496 DRM_ERROR("FDI train 2 fail!\n");
8db9d77b
ZW
3497
3498 DRM_DEBUG_KMS("FDI train done.\n");
3499}
3500
357555c0
JB
3501/* Manual link training for Ivy Bridge A0 parts */
3502static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
3503{
3504 struct drm_device *dev = crtc->dev;
3505 struct drm_i915_private *dev_priv = dev->dev_private;
3506 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3507 int pipe = intel_crtc->pipe;
f0f59a00
VS
3508 i915_reg_t reg;
3509 u32 temp, i, j;
357555c0
JB
3510
3511 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3512 for train result */
3513 reg = FDI_RX_IMR(pipe);
3514 temp = I915_READ(reg);
3515 temp &= ~FDI_RX_SYMBOL_LOCK;
3516 temp &= ~FDI_RX_BIT_LOCK;
3517 I915_WRITE(reg, temp);
3518
3519 POSTING_READ(reg);
3520 udelay(150);
3521
01a415fd
DV
3522 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
3523 I915_READ(FDI_RX_IIR(pipe)));
3524
139ccd3f
JB
3525 /* Try each vswing and preemphasis setting twice before moving on */
3526 for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
3527 /* disable first in case we need to retry */
3528 reg = FDI_TX_CTL(pipe);
3529 temp = I915_READ(reg);
3530 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
3531 temp &= ~FDI_TX_ENABLE;
3532 I915_WRITE(reg, temp);
357555c0 3533
139ccd3f
JB
3534 reg = FDI_RX_CTL(pipe);
3535 temp = I915_READ(reg);
3536 temp &= ~FDI_LINK_TRAIN_AUTO;
3537 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3538 temp &= ~FDI_RX_ENABLE;
3539 I915_WRITE(reg, temp);
357555c0 3540
139ccd3f 3541 /* enable CPU FDI TX and PCH FDI RX */
357555c0
JB
3542 reg = FDI_TX_CTL(pipe);
3543 temp = I915_READ(reg);
139ccd3f 3544 temp &= ~FDI_DP_PORT_WIDTH_MASK;
6e3c9717 3545 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
139ccd3f 3546 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
357555c0 3547 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
139ccd3f
JB
3548 temp |= snb_b_fdi_train_param[j/2];
3549 temp |= FDI_COMPOSITE_SYNC;
3550 I915_WRITE(reg, temp | FDI_TX_ENABLE);
357555c0 3551
139ccd3f
JB
3552 I915_WRITE(FDI_RX_MISC(pipe),
3553 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
357555c0 3554
139ccd3f 3555 reg = FDI_RX_CTL(pipe);
357555c0 3556 temp = I915_READ(reg);
139ccd3f
JB
3557 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3558 temp |= FDI_COMPOSITE_SYNC;
3559 I915_WRITE(reg, temp | FDI_RX_ENABLE);
357555c0 3560
139ccd3f
JB
3561 POSTING_READ(reg);
3562 udelay(1); /* should be 0.5us */
357555c0 3563
139ccd3f
JB
3564 for (i = 0; i < 4; i++) {
3565 reg = FDI_RX_IIR(pipe);
3566 temp = I915_READ(reg);
3567 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
357555c0 3568
139ccd3f
JB
3569 if (temp & FDI_RX_BIT_LOCK ||
3570 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
3571 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3572 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
3573 i);
3574 break;
3575 }
3576 udelay(1); /* should be 0.5us */
3577 }
3578 if (i == 4) {
3579 DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
3580 continue;
3581 }
357555c0 3582
139ccd3f 3583 /* Train 2 */
357555c0
JB
3584 reg = FDI_TX_CTL(pipe);
3585 temp = I915_READ(reg);
139ccd3f
JB
3586 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3587 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
3588 I915_WRITE(reg, temp);
3589
3590 reg = FDI_RX_CTL(pipe);
3591 temp = I915_READ(reg);
3592 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3593 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
357555c0
JB
3594 I915_WRITE(reg, temp);
3595
3596 POSTING_READ(reg);
139ccd3f 3597 udelay(2); /* should be 1.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_SYMBOL_LOCK ||
3605 (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
3606 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3607 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
3608 i);
3609 goto train_done;
3610 }
3611 udelay(2); /* should be 1.5us */
357555c0 3612 }
139ccd3f
JB
3613 if (i == 4)
3614 DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
357555c0 3615 }
357555c0 3616
139ccd3f 3617train_done:
357555c0
JB
3618 DRM_DEBUG_KMS("FDI train done.\n");
3619}
3620
88cefb6c 3621static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
2c07245f 3622{
88cefb6c 3623 struct drm_device *dev = intel_crtc->base.dev;
2c07245f 3624 struct drm_i915_private *dev_priv = dev->dev_private;
2c07245f 3625 int pipe = intel_crtc->pipe;
f0f59a00
VS
3626 i915_reg_t reg;
3627 u32 temp;
c64e311e 3628
c98e9dcf 3629 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
5eddb70b
CW
3630 reg = FDI_RX_CTL(pipe);
3631 temp = I915_READ(reg);
627eb5a3 3632 temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
6e3c9717 3633 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
dfd07d72 3634 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
5eddb70b
CW
3635 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
3636
3637 POSTING_READ(reg);
c98e9dcf
JB
3638 udelay(200);
3639
3640 /* Switch from Rawclk to PCDclk */
5eddb70b
CW
3641 temp = I915_READ(reg);
3642 I915_WRITE(reg, temp | FDI_PCDCLK);
3643
3644 POSTING_READ(reg);
c98e9dcf
JB
3645 udelay(200);
3646
20749730
PZ
3647 /* Enable CPU FDI TX PLL, always on for Ironlake */
3648 reg = FDI_TX_CTL(pipe);
3649 temp = I915_READ(reg);
3650 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
3651 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
5eddb70b 3652
20749730
PZ
3653 POSTING_READ(reg);
3654 udelay(100);
6be4a607 3655 }
0e23b99d
JB
3656}
3657
88cefb6c
DV
3658static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
3659{
3660 struct drm_device *dev = intel_crtc->base.dev;
3661 struct drm_i915_private *dev_priv = dev->dev_private;
3662 int pipe = intel_crtc->pipe;
f0f59a00
VS
3663 i915_reg_t reg;
3664 u32 temp;
88cefb6c
DV
3665
3666 /* Switch from PCDclk to Rawclk */
3667 reg = FDI_RX_CTL(pipe);
3668 temp = I915_READ(reg);
3669 I915_WRITE(reg, temp & ~FDI_PCDCLK);
3670
3671 /* Disable CPU FDI TX PLL */
3672 reg = FDI_TX_CTL(pipe);
3673 temp = I915_READ(reg);
3674 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
3675
3676 POSTING_READ(reg);
3677 udelay(100);
3678
3679 reg = FDI_RX_CTL(pipe);
3680 temp = I915_READ(reg);
3681 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
3682
3683 /* Wait for the clocks to turn off. */
3684 POSTING_READ(reg);
3685 udelay(100);
3686}
3687
0fc932b8
JB
3688static void ironlake_fdi_disable(struct drm_crtc *crtc)
3689{
3690 struct drm_device *dev = crtc->dev;
3691 struct drm_i915_private *dev_priv = dev->dev_private;
3692 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3693 int pipe = intel_crtc->pipe;
f0f59a00
VS
3694 i915_reg_t reg;
3695 u32 temp;
0fc932b8
JB
3696
3697 /* disable CPU FDI tx and PCH FDI rx */
3698 reg = FDI_TX_CTL(pipe);
3699 temp = I915_READ(reg);
3700 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
3701 POSTING_READ(reg);
3702
3703 reg = FDI_RX_CTL(pipe);
3704 temp = I915_READ(reg);
3705 temp &= ~(0x7 << 16);
dfd07d72 3706 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
0fc932b8
JB
3707 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
3708
3709 POSTING_READ(reg);
3710 udelay(100);
3711
3712 /* Ironlake workaround, disable clock pointer after downing FDI */
eba905b2 3713 if (HAS_PCH_IBX(dev))
6f06ce18 3714 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
0fc932b8
JB
3715
3716 /* still set train pattern 1 */
3717 reg = FDI_TX_CTL(pipe);
3718 temp = I915_READ(reg);
3719 temp &= ~FDI_LINK_TRAIN_NONE;
3720 temp |= FDI_LINK_TRAIN_PATTERN_1;
3721 I915_WRITE(reg, temp);
3722
3723 reg = FDI_RX_CTL(pipe);
3724 temp = I915_READ(reg);
3725 if (HAS_PCH_CPT(dev)) {
3726 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3727 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3728 } else {
3729 temp &= ~FDI_LINK_TRAIN_NONE;
3730 temp |= FDI_LINK_TRAIN_PATTERN_1;
3731 }
3732 /* BPC in FDI rx is consistent with that in PIPECONF */
3733 temp &= ~(0x07 << 16);
dfd07d72 3734 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
0fc932b8
JB
3735 I915_WRITE(reg, temp);
3736
3737 POSTING_READ(reg);
3738 udelay(100);
3739}
3740
5dce5b93
CW
3741bool intel_has_pending_fb_unpin(struct drm_device *dev)
3742{
3743 struct intel_crtc *crtc;
3744
3745 /* Note that we don't need to be called with mode_config.lock here
3746 * as our list of CRTC objects is static for the lifetime of the
3747 * device and so cannot disappear as we iterate. Similarly, we can
3748 * happily treat the predicates as racy, atomic checks as userspace
3749 * cannot claim and pin a new fb without at least acquring the
3750 * struct_mutex and so serialising with us.
3751 */
d3fcc808 3752 for_each_intel_crtc(dev, crtc) {
5dce5b93
CW
3753 if (atomic_read(&crtc->unpin_work_count) == 0)
3754 continue;
3755
6885843a 3756 if (!list_empty_careful(&crtc->flip_work))
5dce5b93
CW
3757 intel_wait_for_vblank(dev, crtc->pipe);
3758
3759 return true;
3760 }
3761
3762 return false;
3763}
3764
6885843a 3765static void page_flip_completed(struct intel_crtc *intel_crtc, struct intel_flip_work *work)
d6bbafa1
CW
3766{
3767 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
143f73b3
ML
3768 struct drm_plane_state *new_plane_state;
3769 struct drm_plane *primary = intel_crtc->base.primary;
d6bbafa1
CW
3770
3771 if (work->event)
560ce1dc 3772 drm_crtc_send_vblank_event(&intel_crtc->base, work->event);
d6bbafa1
CW
3773
3774 drm_crtc_vblank_put(&intel_crtc->base);
3775
143f73b3
ML
3776 new_plane_state = &work->old_plane_state[0]->base;
3777 if (work->num_planes >= 1 &&
3778 new_plane_state->plane == primary &&
3779 new_plane_state->fb)
3780 trace_i915_flip_complete(intel_crtc->plane,
3781 intel_fb_obj(new_plane_state->fb));
d6bbafa1 3782
143f73b3
ML
3783 if (work->can_async_unpin) {
3784 list_del_init(&work->head);
3785 wake_up_all(&dev_priv->pending_flip_queue);
3786 }
3787
3788 queue_work(dev_priv->wq, &work->unpin_work);
d6bbafa1
CW
3789}
3790
5008e874 3791static int intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
e6c3a2a6 3792{
0f91128d 3793 struct drm_device *dev = crtc->dev;
5bb61643 3794 struct drm_i915_private *dev_priv = dev->dev_private;
5008e874 3795 long ret;
e6c3a2a6 3796
2c10d571 3797 WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
5008e874
ML
3798
3799 ret = wait_event_interruptible_timeout(
3800 dev_priv->pending_flip_queue,
3801 !intel_crtc_has_pending_flip(crtc),
3802 60*HZ);
3803
3804 if (ret < 0)
3805 return ret;
3806
8dd634d9 3807 WARN(ret == 0, "Stuck page flip\n");
5bb61643 3808
5008e874 3809 return 0;
e6c3a2a6
CW
3810}
3811
060f02d8
VS
3812static void lpt_disable_iclkip(struct drm_i915_private *dev_priv)
3813{
3814 u32 temp;
3815
3816 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
3817
3818 mutex_lock(&dev_priv->sb_lock);
3819
3820 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
3821 temp |= SBI_SSCCTL_DISABLE;
3822 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
3823
3824 mutex_unlock(&dev_priv->sb_lock);
3825}
3826
e615efe4
ED
3827/* Program iCLKIP clock to the desired frequency */
3828static void lpt_program_iclkip(struct drm_crtc *crtc)
3829{
64b46a06 3830 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
6e3c9717 3831 int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
e615efe4
ED
3832 u32 divsel, phaseinc, auxdiv, phasedir = 0;
3833 u32 temp;
3834
060f02d8 3835 lpt_disable_iclkip(dev_priv);
e615efe4 3836
64b46a06
VS
3837 /* The iCLK virtual clock root frequency is in MHz,
3838 * but the adjusted_mode->crtc_clock in in KHz. To get the
3839 * divisors, it is necessary to divide one by another, so we
3840 * convert the virtual clock precision to KHz here for higher
3841 * precision.
3842 */
3843 for (auxdiv = 0; auxdiv < 2; auxdiv++) {
e615efe4
ED
3844 u32 iclk_virtual_root_freq = 172800 * 1000;
3845 u32 iclk_pi_range = 64;
64b46a06 3846 u32 desired_divisor;
e615efe4 3847
64b46a06
VS
3848 desired_divisor = DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
3849 clock << auxdiv);
3850 divsel = (desired_divisor / iclk_pi_range) - 2;
3851 phaseinc = desired_divisor % iclk_pi_range;
e615efe4 3852
64b46a06
VS
3853 /*
3854 * Near 20MHz is a corner case which is
3855 * out of range for the 7-bit divisor
3856 */
3857 if (divsel <= 0x7f)
3858 break;
e615efe4
ED
3859 }
3860
3861 /* This should not happen with any sane values */
3862 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
3863 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
3864 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
3865 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
3866
3867 DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
12d7ceed 3868 clock,
e615efe4
ED
3869 auxdiv,
3870 divsel,
3871 phasedir,
3872 phaseinc);
3873
060f02d8
VS
3874 mutex_lock(&dev_priv->sb_lock);
3875
e615efe4 3876 /* Program SSCDIVINTPHASE6 */
988d6ee8 3877 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
e615efe4
ED
3878 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
3879 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
3880 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
3881 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
3882 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
3883 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
988d6ee8 3884 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
e615efe4
ED
3885
3886 /* Program SSCAUXDIV */
988d6ee8 3887 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
e615efe4
ED
3888 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
3889 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
988d6ee8 3890 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
e615efe4
ED
3891
3892 /* Enable modulator and associated divider */
988d6ee8 3893 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
e615efe4 3894 temp &= ~SBI_SSCCTL_DISABLE;
988d6ee8 3895 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
e615efe4 3896
060f02d8
VS
3897 mutex_unlock(&dev_priv->sb_lock);
3898
e615efe4
ED
3899 /* Wait for initialization time */
3900 udelay(24);
3901
3902 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
3903}
3904
8802e5b6
VS
3905int lpt_get_iclkip(struct drm_i915_private *dev_priv)
3906{
3907 u32 divsel, phaseinc, auxdiv;
3908 u32 iclk_virtual_root_freq = 172800 * 1000;
3909 u32 iclk_pi_range = 64;
3910 u32 desired_divisor;
3911 u32 temp;
3912
3913 if ((I915_READ(PIXCLK_GATE) & PIXCLK_GATE_UNGATE) == 0)
3914 return 0;
3915
3916 mutex_lock(&dev_priv->sb_lock);
3917
3918 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
3919 if (temp & SBI_SSCCTL_DISABLE) {
3920 mutex_unlock(&dev_priv->sb_lock);
3921 return 0;
3922 }
3923
3924 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
3925 divsel = (temp & SBI_SSCDIVINTPHASE_DIVSEL_MASK) >>
3926 SBI_SSCDIVINTPHASE_DIVSEL_SHIFT;
3927 phaseinc = (temp & SBI_SSCDIVINTPHASE_INCVAL_MASK) >>
3928 SBI_SSCDIVINTPHASE_INCVAL_SHIFT;
3929
3930 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
3931 auxdiv = (temp & SBI_SSCAUXDIV_FINALDIV2SEL_MASK) >>
3932 SBI_SSCAUXDIV_FINALDIV2SEL_SHIFT;
3933
3934 mutex_unlock(&dev_priv->sb_lock);
3935
3936 desired_divisor = (divsel + 2) * iclk_pi_range + phaseinc;
3937
3938 return DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
3939 desired_divisor << auxdiv);
3940}
3941
275f01b2
DV
3942static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
3943 enum pipe pch_transcoder)
3944{
3945 struct drm_device *dev = crtc->base.dev;
3946 struct drm_i915_private *dev_priv = dev->dev_private;
6e3c9717 3947 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
275f01b2
DV
3948
3949 I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
3950 I915_READ(HTOTAL(cpu_transcoder)));
3951 I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
3952 I915_READ(HBLANK(cpu_transcoder)));
3953 I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
3954 I915_READ(HSYNC(cpu_transcoder)));
3955
3956 I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
3957 I915_READ(VTOTAL(cpu_transcoder)));
3958 I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
3959 I915_READ(VBLANK(cpu_transcoder)));
3960 I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
3961 I915_READ(VSYNC(cpu_transcoder)));
3962 I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
3963 I915_READ(VSYNCSHIFT(cpu_transcoder)));
3964}
3965
003632d9 3966static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
1fbc0d78
DV
3967{
3968 struct drm_i915_private *dev_priv = dev->dev_private;
3969 uint32_t temp;
3970
3971 temp = I915_READ(SOUTH_CHICKEN1);
003632d9 3972 if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
1fbc0d78
DV
3973 return;
3974
3975 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
3976 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
3977
003632d9
ACO
3978 temp &= ~FDI_BC_BIFURCATION_SELECT;
3979 if (enable)
3980 temp |= FDI_BC_BIFURCATION_SELECT;
3981
3982 DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
1fbc0d78
DV
3983 I915_WRITE(SOUTH_CHICKEN1, temp);
3984 POSTING_READ(SOUTH_CHICKEN1);
3985}
3986
3987static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
3988{
3989 struct drm_device *dev = intel_crtc->base.dev;
1fbc0d78
DV
3990
3991 switch (intel_crtc->pipe) {
3992 case PIPE_A:
3993 break;
3994 case PIPE_B:
6e3c9717 3995 if (intel_crtc->config->fdi_lanes > 2)
003632d9 3996 cpt_set_fdi_bc_bifurcation(dev, false);
1fbc0d78 3997 else
003632d9 3998 cpt_set_fdi_bc_bifurcation(dev, true);
1fbc0d78
DV
3999
4000 break;
4001 case PIPE_C:
003632d9 4002 cpt_set_fdi_bc_bifurcation(dev, true);
1fbc0d78
DV
4003
4004 break;
4005 default:
4006 BUG();
4007 }
4008}
4009
c48b5305
VS
4010/* Return which DP Port should be selected for Transcoder DP control */
4011static enum port
4012intel_trans_dp_port_sel(struct drm_crtc *crtc)
4013{
4014 struct drm_device *dev = crtc->dev;
4015 struct intel_encoder *encoder;
4016
4017 for_each_encoder_on_crtc(dev, crtc, encoder) {
4018 if (encoder->type == INTEL_OUTPUT_DISPLAYPORT ||
4019 encoder->type == INTEL_OUTPUT_EDP)
4020 return enc_to_dig_port(&encoder->base)->port;
4021 }
4022
4023 return -1;
4024}
4025
f67a559d
JB
4026/*
4027 * Enable PCH resources required for PCH ports:
4028 * - PCH PLLs
4029 * - FDI training & RX/TX
4030 * - update transcoder timings
4031 * - DP transcoding bits
4032 * - transcoder
4033 */
4034static void ironlake_pch_enable(struct drm_crtc *crtc)
0e23b99d
JB
4035{
4036 struct drm_device *dev = crtc->dev;
4037 struct drm_i915_private *dev_priv = dev->dev_private;
4038 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4039 int pipe = intel_crtc->pipe;
f0f59a00 4040 u32 temp;
2c07245f 4041
ab9412ba 4042 assert_pch_transcoder_disabled(dev_priv, pipe);
e7e164db 4043
1fbc0d78
DV
4044 if (IS_IVYBRIDGE(dev))
4045 ivybridge_update_fdi_bc_bifurcation(intel_crtc);
4046
cd986abb
DV
4047 /* Write the TU size bits before fdi link training, so that error
4048 * detection works. */
4049 I915_WRITE(FDI_RX_TUSIZE1(pipe),
4050 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
4051
c98e9dcf 4052 /* For PCH output, training FDI link */
674cf967 4053 dev_priv->display.fdi_link_train(crtc);
2c07245f 4054
3ad8a208
DV
4055 /* We need to program the right clock selection before writing the pixel
4056 * mutliplier into the DPLL. */
303b81e0 4057 if (HAS_PCH_CPT(dev)) {
ee7b9f93 4058 u32 sel;
4b645f14 4059
c98e9dcf 4060 temp = I915_READ(PCH_DPLL_SEL);
11887397
DV
4061 temp |= TRANS_DPLL_ENABLE(pipe);
4062 sel = TRANS_DPLLB_SEL(pipe);
8106ddbd
ACO
4063 if (intel_crtc->config->shared_dpll ==
4064 intel_get_shared_dpll_by_id(dev_priv, DPLL_ID_PCH_PLL_B))
ee7b9f93
JB
4065 temp |= sel;
4066 else
4067 temp &= ~sel;
c98e9dcf 4068 I915_WRITE(PCH_DPLL_SEL, temp);
c98e9dcf 4069 }
5eddb70b 4070
3ad8a208
DV
4071 /* XXX: pch pll's can be enabled any time before we enable the PCH
4072 * transcoder, and we actually should do this to not upset any PCH
4073 * transcoder that already use the clock when we share it.
4074 *
4075 * Note that enable_shared_dpll tries to do the right thing, but
4076 * get_shared_dpll unconditionally resets the pll - we need that to have
4077 * the right LVDS enable sequence. */
85b3894f 4078 intel_enable_shared_dpll(intel_crtc);
3ad8a208 4079
d9b6cb56
JB
4080 /* set transcoder timing, panel must allow it */
4081 assert_panel_unlocked(dev_priv, pipe);
275f01b2 4082 ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
8db9d77b 4083
303b81e0 4084 intel_fdi_normal_train(crtc);
5e84e1a4 4085
c98e9dcf 4086 /* For PCH DP, enable TRANS_DP_CTL */
6e3c9717 4087 if (HAS_PCH_CPT(dev) && intel_crtc->config->has_dp_encoder) {
9c4edaee
VS
4088 const struct drm_display_mode *adjusted_mode =
4089 &intel_crtc->config->base.adjusted_mode;
dfd07d72 4090 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
f0f59a00 4091 i915_reg_t reg = TRANS_DP_CTL(pipe);
5eddb70b
CW
4092 temp = I915_READ(reg);
4093 temp &= ~(TRANS_DP_PORT_SEL_MASK |
220cad3c
EA
4094 TRANS_DP_SYNC_MASK |
4095 TRANS_DP_BPC_MASK);
e3ef4479 4096 temp |= TRANS_DP_OUTPUT_ENABLE;
9325c9f0 4097 temp |= bpc << 9; /* same format but at 11:9 */
c98e9dcf 4098
9c4edaee 4099 if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
5eddb70b 4100 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
9c4edaee 4101 if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
5eddb70b 4102 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
c98e9dcf
JB
4103
4104 switch (intel_trans_dp_port_sel(crtc)) {
c48b5305 4105 case PORT_B:
5eddb70b 4106 temp |= TRANS_DP_PORT_SEL_B;
c98e9dcf 4107 break;
c48b5305 4108 case PORT_C:
5eddb70b 4109 temp |= TRANS_DP_PORT_SEL_C;
c98e9dcf 4110 break;
c48b5305 4111 case PORT_D:
5eddb70b 4112 temp |= TRANS_DP_PORT_SEL_D;
c98e9dcf
JB
4113 break;
4114 default:
e95d41e1 4115 BUG();
32f9d658 4116 }
2c07245f 4117
5eddb70b 4118 I915_WRITE(reg, temp);
6be4a607 4119 }
b52eb4dc 4120
b8a4f404 4121 ironlake_enable_pch_transcoder(dev_priv, pipe);
f67a559d
JB
4122}
4123
1507e5bd
PZ
4124static void lpt_pch_enable(struct drm_crtc *crtc)
4125{
4126 struct drm_device *dev = crtc->dev;
4127 struct drm_i915_private *dev_priv = dev->dev_private;
4128 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6e3c9717 4129 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
1507e5bd 4130
ab9412ba 4131 assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
1507e5bd 4132
8c52b5e8 4133 lpt_program_iclkip(crtc);
1507e5bd 4134
0540e488 4135 /* Set transcoder timing. */
275f01b2 4136 ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
1507e5bd 4137
937bb610 4138 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
f67a559d
JB
4139}
4140
a1520318 4141static void cpt_verify_modeset(struct drm_device *dev, int pipe)
d4270e57
JB
4142{
4143 struct drm_i915_private *dev_priv = dev->dev_private;
f0f59a00 4144 i915_reg_t dslreg = PIPEDSL(pipe);
d4270e57
JB
4145 u32 temp;
4146
4147 temp = I915_READ(dslreg);
4148 udelay(500);
4149 if (wait_for(I915_READ(dslreg) != temp, 5)) {
d4270e57 4150 if (wait_for(I915_READ(dslreg) != temp, 5))
84f44ce7 4151 DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
d4270e57
JB
4152 }
4153}
4154
86adf9d7
ML
4155static int
4156skl_update_scaler(struct intel_crtc_state *crtc_state, bool force_detach,
4157 unsigned scaler_user, int *scaler_id, unsigned int rotation,
4158 int src_w, int src_h, int dst_w, int dst_h)
a1b2278e 4159{
86adf9d7
ML
4160 struct intel_crtc_scaler_state *scaler_state =
4161 &crtc_state->scaler_state;
4162 struct intel_crtc *intel_crtc =
4163 to_intel_crtc(crtc_state->base.crtc);
a1b2278e 4164 int need_scaling;
6156a456
CK
4165
4166 need_scaling = intel_rotation_90_or_270(rotation) ?
4167 (src_h != dst_w || src_w != dst_h):
4168 (src_w != dst_w || src_h != dst_h);
a1b2278e
CK
4169
4170 /*
4171 * if plane is being disabled or scaler is no more required or force detach
4172 * - free scaler binded to this plane/crtc
4173 * - in order to do this, update crtc->scaler_usage
4174 *
4175 * Here scaler state in crtc_state is set free so that
4176 * scaler can be assigned to other user. Actual register
4177 * update to free the scaler is done in plane/panel-fit programming.
4178 * For this purpose crtc/plane_state->scaler_id isn't reset here.
4179 */
86adf9d7 4180 if (force_detach || !need_scaling) {
a1b2278e 4181 if (*scaler_id >= 0) {
86adf9d7 4182 scaler_state->scaler_users &= ~(1 << scaler_user);
a1b2278e
CK
4183 scaler_state->scalers[*scaler_id].in_use = 0;
4184
86adf9d7
ML
4185 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4186 "Staged freeing scaler id %d scaler_users = 0x%x\n",
4187 intel_crtc->pipe, scaler_user, *scaler_id,
a1b2278e
CK
4188 scaler_state->scaler_users);
4189 *scaler_id = -1;
4190 }
4191 return 0;
4192 }
4193
4194 /* range checks */
4195 if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
4196 dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
4197
4198 src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
4199 dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
86adf9d7 4200 DRM_DEBUG_KMS("scaler_user index %u.%u: src %ux%u dst %ux%u "
a1b2278e 4201 "size is out of scaler range\n",
86adf9d7 4202 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h);
a1b2278e
CK
4203 return -EINVAL;
4204 }
4205
86adf9d7
ML
4206 /* mark this plane as a scaler user in crtc_state */
4207 scaler_state->scaler_users |= (1 << scaler_user);
4208 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4209 "staged scaling request for %ux%u->%ux%u scaler_users = 0x%x\n",
4210 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h,
4211 scaler_state->scaler_users);
4212
4213 return 0;
4214}
4215
4216/**
4217 * skl_update_scaler_crtc - Stages update to scaler state for a given crtc.
4218 *
4219 * @state: crtc's scaler state
86adf9d7
ML
4220 *
4221 * Return
4222 * 0 - scaler_usage updated successfully
4223 * error - requested scaling cannot be supported or other error condition
4224 */
e435d6e5 4225int skl_update_scaler_crtc(struct intel_crtc_state *state)
86adf9d7
ML
4226{
4227 struct intel_crtc *intel_crtc = to_intel_crtc(state->base.crtc);
7c5f93b0 4228 const struct drm_display_mode *adjusted_mode = &state->base.adjusted_mode;
86adf9d7
ML
4229
4230 DRM_DEBUG_KMS("Updating scaler for [CRTC:%i] scaler_user index %u.%u\n",
4231 intel_crtc->base.base.id, intel_crtc->pipe, SKL_CRTC_INDEX);
4232
e435d6e5 4233 return skl_update_scaler(state, !state->base.active, SKL_CRTC_INDEX,
fa5a7970 4234 &state->scaler_state.scaler_id, BIT(DRM_ROTATE_0),
86adf9d7 4235 state->pipe_src_w, state->pipe_src_h,
aad941d5 4236 adjusted_mode->crtc_hdisplay, adjusted_mode->crtc_vdisplay);
86adf9d7
ML
4237}
4238
4239/**
4240 * skl_update_scaler_plane - Stages update to scaler state for a given plane.
4241 *
4242 * @state: crtc's scaler state
86adf9d7
ML
4243 * @plane_state: atomic plane state to update
4244 *
4245 * Return
4246 * 0 - scaler_usage updated successfully
4247 * error - requested scaling cannot be supported or other error condition
4248 */
da20eabd
ML
4249static int skl_update_scaler_plane(struct intel_crtc_state *crtc_state,
4250 struct intel_plane_state *plane_state)
86adf9d7
ML
4251{
4252
4253 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
da20eabd
ML
4254 struct intel_plane *intel_plane =
4255 to_intel_plane(plane_state->base.plane);
86adf9d7
ML
4256 struct drm_framebuffer *fb = plane_state->base.fb;
4257 int ret;
4258
4259 bool force_detach = !fb || !plane_state->visible;
4260
4261 DRM_DEBUG_KMS("Updating scaler for [PLANE:%d] scaler_user index %u.%u\n",
4262 intel_plane->base.base.id, intel_crtc->pipe,
4263 drm_plane_index(&intel_plane->base));
4264
4265 ret = skl_update_scaler(crtc_state, force_detach,
4266 drm_plane_index(&intel_plane->base),
4267 &plane_state->scaler_id,
4268 plane_state->base.rotation,
4269 drm_rect_width(&plane_state->src) >> 16,
4270 drm_rect_height(&plane_state->src) >> 16,
4271 drm_rect_width(&plane_state->dst),
4272 drm_rect_height(&plane_state->dst));
4273
4274 if (ret || plane_state->scaler_id < 0)
4275 return ret;
4276
a1b2278e 4277 /* check colorkey */
818ed961 4278 if (plane_state->ckey.flags != I915_SET_COLORKEY_NONE) {
86adf9d7 4279 DRM_DEBUG_KMS("[PLANE:%d] scaling with color key not allowed",
818ed961 4280 intel_plane->base.base.id);
a1b2278e
CK
4281 return -EINVAL;
4282 }
4283
4284 /* Check src format */
86adf9d7
ML
4285 switch (fb->pixel_format) {
4286 case DRM_FORMAT_RGB565:
4287 case DRM_FORMAT_XBGR8888:
4288 case DRM_FORMAT_XRGB8888:
4289 case DRM_FORMAT_ABGR8888:
4290 case DRM_FORMAT_ARGB8888:
4291 case DRM_FORMAT_XRGB2101010:
4292 case DRM_FORMAT_XBGR2101010:
4293 case DRM_FORMAT_YUYV:
4294 case DRM_FORMAT_YVYU:
4295 case DRM_FORMAT_UYVY:
4296 case DRM_FORMAT_VYUY:
4297 break;
4298 default:
4299 DRM_DEBUG_KMS("[PLANE:%d] FB:%d unsupported scaling format 0x%x\n",
4300 intel_plane->base.base.id, fb->base.id, fb->pixel_format);
4301 return -EINVAL;
a1b2278e
CK
4302 }
4303
a1b2278e
CK
4304 return 0;
4305}
4306
e435d6e5
ML
4307static void skylake_scaler_disable(struct intel_crtc *crtc)
4308{
4309 int i;
4310
4311 for (i = 0; i < crtc->num_scalers; i++)
4312 skl_detach_scaler(crtc, i);
4313}
4314
4315static void skylake_pfit_enable(struct intel_crtc *crtc)
bd2e244f
JB
4316{
4317 struct drm_device *dev = crtc->base.dev;
4318 struct drm_i915_private *dev_priv = dev->dev_private;
4319 int pipe = crtc->pipe;
a1b2278e
CK
4320 struct intel_crtc_scaler_state *scaler_state =
4321 &crtc->config->scaler_state;
4322
4323 DRM_DEBUG_KMS("for crtc_state = %p\n", crtc->config);
4324
6e3c9717 4325 if (crtc->config->pch_pfit.enabled) {
a1b2278e
CK
4326 int id;
4327
4328 if (WARN_ON(crtc->config->scaler_state.scaler_id < 0)) {
4329 DRM_ERROR("Requesting pfit without getting a scaler first\n");
4330 return;
4331 }
4332
4333 id = scaler_state->scaler_id;
4334 I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
4335 PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
4336 I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
4337 I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
4338
4339 DRM_DEBUG_KMS("for crtc_state = %p scaler_id = %d\n", crtc->config, id);
bd2e244f
JB
4340 }
4341}
4342
b074cec8
JB
4343static void ironlake_pfit_enable(struct intel_crtc *crtc)
4344{
4345 struct drm_device *dev = crtc->base.dev;
4346 struct drm_i915_private *dev_priv = dev->dev_private;
4347 int pipe = crtc->pipe;
4348
6e3c9717 4349 if (crtc->config->pch_pfit.enabled) {
b074cec8
JB
4350 /* Force use of hard-coded filter coefficients
4351 * as some pre-programmed values are broken,
4352 * e.g. x201.
4353 */
4354 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
4355 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
4356 PF_PIPE_SEL_IVB(pipe));
4357 else
4358 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
6e3c9717
ACO
4359 I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
4360 I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
d4270e57
JB
4361 }
4362}
4363
20bc8673 4364void hsw_enable_ips(struct intel_crtc *crtc)
d77e4531 4365{
cea165c3
VS
4366 struct drm_device *dev = crtc->base.dev;
4367 struct drm_i915_private *dev_priv = dev->dev_private;
d77e4531 4368
6e3c9717 4369 if (!crtc->config->ips_enabled)
d77e4531
PZ
4370 return;
4371
307e4498
ML
4372 /*
4373 * We can only enable IPS after we enable a plane and wait for a vblank
4374 * This function is called from post_plane_update, which is run after
4375 * a vblank wait.
4376 */
cea165c3 4377
d77e4531 4378 assert_plane_enabled(dev_priv, crtc->plane);
cea165c3 4379 if (IS_BROADWELL(dev)) {
2a114cc1
BW
4380 mutex_lock(&dev_priv->rps.hw_lock);
4381 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
4382 mutex_unlock(&dev_priv->rps.hw_lock);
4383 /* Quoting Art Runyan: "its not safe to expect any particular
4384 * value in IPS_CTL bit 31 after enabling IPS through the
e59150dc
JB
4385 * mailbox." Moreover, the mailbox may return a bogus state,
4386 * so we need to just enable it and continue on.
2a114cc1
BW
4387 */
4388 } else {
4389 I915_WRITE(IPS_CTL, IPS_ENABLE);
4390 /* The bit only becomes 1 in the next vblank, so this wait here
4391 * is essentially intel_wait_for_vblank. If we don't have this
4392 * and don't wait for vblanks until the end of crtc_enable, then
4393 * the HW state readout code will complain that the expected
4394 * IPS_CTL value is not the one we read. */
4395 if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
4396 DRM_ERROR("Timed out waiting for IPS enable\n");
4397 }
d77e4531
PZ
4398}
4399
20bc8673 4400void hsw_disable_ips(struct intel_crtc *crtc)
d77e4531
PZ
4401{
4402 struct drm_device *dev = crtc->base.dev;
4403 struct drm_i915_private *dev_priv = dev->dev_private;
4404
6e3c9717 4405 if (!crtc->config->ips_enabled)
d77e4531
PZ
4406 return;
4407
4408 assert_plane_enabled(dev_priv, crtc->plane);
23d0b130 4409 if (IS_BROADWELL(dev)) {
2a114cc1
BW
4410 mutex_lock(&dev_priv->rps.hw_lock);
4411 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
4412 mutex_unlock(&dev_priv->rps.hw_lock);
23d0b130
BW
4413 /* wait for pcode to finish disabling IPS, which may take up to 42ms */
4414 if (wait_for((I915_READ(IPS_CTL) & IPS_ENABLE) == 0, 42))
4415 DRM_ERROR("Timed out waiting for IPS disable\n");
e59150dc 4416 } else {
2a114cc1 4417 I915_WRITE(IPS_CTL, 0);
e59150dc
JB
4418 POSTING_READ(IPS_CTL);
4419 }
d77e4531
PZ
4420
4421 /* We need to wait for a vblank before we can disable the plane. */
4422 intel_wait_for_vblank(dev, crtc->pipe);
4423}
4424
7cac945f 4425static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
d3eedb1a 4426{
7cac945f 4427 if (intel_crtc->overlay) {
d3eedb1a
VS
4428 struct drm_device *dev = intel_crtc->base.dev;
4429 struct drm_i915_private *dev_priv = dev->dev_private;
4430
4431 mutex_lock(&dev->struct_mutex);
4432 dev_priv->mm.interruptible = false;
4433 (void) intel_overlay_switch_off(intel_crtc->overlay);
4434 dev_priv->mm.interruptible = true;
4435 mutex_unlock(&dev->struct_mutex);
4436 }
4437
4438 /* Let userspace switch the overlay on again. In most cases userspace
4439 * has to recompute where to put it anyway.
4440 */
4441}
4442
87d4300a
ML
4443/**
4444 * intel_post_enable_primary - Perform operations after enabling primary plane
4445 * @crtc: the CRTC whose primary plane was just enabled
4446 *
4447 * Performs potentially sleeping operations that must be done after the primary
4448 * plane is enabled, such as updating FBC and IPS. Note that this may be
4449 * called due to an explicit primary plane update, or due to an implicit
4450 * re-enable that is caused when a sprite plane is updated to no longer
4451 * completely hide the primary plane.
4452 */
4453static void
4454intel_post_enable_primary(struct drm_crtc *crtc)
a5c4d7bc
VS
4455{
4456 struct drm_device *dev = crtc->dev;
87d4300a 4457 struct drm_i915_private *dev_priv = dev->dev_private;
a5c4d7bc
VS
4458 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4459 int pipe = intel_crtc->pipe;
a5c4d7bc 4460
87d4300a
ML
4461 /*
4462 * FIXME IPS should be fine as long as one plane is
4463 * enabled, but in practice it seems to have problems
4464 * when going from primary only to sprite only and vice
4465 * versa.
4466 */
a5c4d7bc
VS
4467 hsw_enable_ips(intel_crtc);
4468
f99d7069 4469 /*
87d4300a
ML
4470 * Gen2 reports pipe underruns whenever all planes are disabled.
4471 * So don't enable underrun reporting before at least some planes
4472 * are enabled.
4473 * FIXME: Need to fix the logic to work when we turn off all planes
4474 * but leave the pipe running.
f99d7069 4475 */
87d4300a
ML
4476 if (IS_GEN2(dev))
4477 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4478
aca7b684
VS
4479 /* Underruns don't always raise interrupts, so check manually. */
4480 intel_check_cpu_fifo_underruns(dev_priv);
4481 intel_check_pch_fifo_underruns(dev_priv);
a5c4d7bc
VS
4482}
4483
2622a081 4484/* FIXME move all this to pre_plane_update() with proper state tracking */
87d4300a
ML
4485static void
4486intel_pre_disable_primary(struct drm_crtc *crtc)
a5c4d7bc
VS
4487{
4488 struct drm_device *dev = crtc->dev;
4489 struct drm_i915_private *dev_priv = dev->dev_private;
4490 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4491 int pipe = intel_crtc->pipe;
a5c4d7bc 4492
87d4300a
ML
4493 /*
4494 * Gen2 reports pipe underruns whenever all planes are disabled.
4495 * So diasble underrun reporting before all the planes get disabled.
4496 * FIXME: Need to fix the logic to work when we turn off all planes
4497 * but leave the pipe running.
4498 */
4499 if (IS_GEN2(dev))
4500 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
a5c4d7bc 4501
2622a081
VS
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 */
4508 hsw_disable_ips(intel_crtc);
4509}
4510
4511/* FIXME get rid of this and use pre_plane_update */
4512static void
4513intel_pre_disable_primary_noatomic(struct drm_crtc *crtc)
4514{
4515 struct drm_device *dev = crtc->dev;
4516 struct drm_i915_private *dev_priv = dev->dev_private;
4517 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4518 int pipe = intel_crtc->pipe;
4519
4520 intel_pre_disable_primary(crtc);
4521
87d4300a
ML
4522 /*
4523 * Vblank time updates from the shadow to live plane control register
4524 * are blocked if the memory self-refresh mode is active at that
4525 * moment. So to make sure the plane gets truly disabled, disable
4526 * first the self-refresh mode. The self-refresh enable bit in turn
4527 * will be checked/applied by the HW only at the next frame start
4528 * event which is after the vblank start event, so we need to have a
4529 * wait-for-vblank between disabling the plane and the pipe.
4530 */
262cd2e1 4531 if (HAS_GMCH_DISPLAY(dev)) {
87d4300a 4532 intel_set_memory_cxsr(dev_priv, false);
262cd2e1
VS
4533 dev_priv->wm.vlv.cxsr = false;
4534 intel_wait_for_vblank(dev, pipe);
4535 }
87d4300a
ML
4536}
4537
5c74cd73 4538static void intel_pre_plane_update(struct intel_crtc_state *old_crtc_state)
ac21b225 4539{
5c74cd73 4540 struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
ac21b225 4541 struct drm_device *dev = crtc->base.dev;
eddfcbcd 4542 struct drm_i915_private *dev_priv = dev->dev_private;
ab1d3a0e
ML
4543 struct intel_crtc_state *pipe_config =
4544 to_intel_crtc_state(crtc->base.state);
5c74cd73
ML
4545 struct drm_atomic_state *old_state = old_crtc_state->base.state;
4546 struct drm_plane *primary = crtc->base.primary;
4547 struct drm_plane_state *old_pri_state =
4548 drm_atomic_get_existing_plane_state(old_state, primary);
4549 bool modeset = needs_modeset(&pipe_config->base);
ac21b225 4550
5c74cd73
ML
4551 if (old_pri_state) {
4552 struct intel_plane_state *primary_state =
4553 to_intel_plane_state(primary->state);
4554 struct intel_plane_state *old_primary_state =
4555 to_intel_plane_state(old_pri_state);
4556
2099deff 4557 intel_fbc_pre_update(crtc, pipe_config, primary_state);
31ae71fc 4558
5c74cd73
ML
4559 if (old_primary_state->visible &&
4560 (modeset || !primary_state->visible))
4561 intel_pre_disable_primary(&crtc->base);
4562 }
852eb00d 4563
ab1d3a0e 4564 if (pipe_config->disable_cxsr) {
852eb00d 4565 crtc->wm.cxsr_allowed = false;
2dfd178d 4566
2622a081
VS
4567 /*
4568 * Vblank time updates from the shadow to live plane control register
4569 * are blocked if the memory self-refresh mode is active at that
4570 * moment. So to make sure the plane gets truly disabled, disable
4571 * first the self-refresh mode. The self-refresh enable bit in turn
4572 * will be checked/applied by the HW only at the next frame start
4573 * event which is after the vblank start event, so we need to have a
4574 * wait-for-vblank between disabling the plane and the pipe.
4575 */
4576 if (old_crtc_state->base.active) {
2dfd178d 4577 intel_set_memory_cxsr(dev_priv, false);
2622a081
VS
4578 dev_priv->wm.vlv.cxsr = false;
4579 intel_wait_for_vblank(dev, crtc->pipe);
4580 }
852eb00d 4581 }
92826fcd 4582
ed4a6a7c
MR
4583 /*
4584 * IVB workaround: must disable low power watermarks for at least
4585 * one frame before enabling scaling. LP watermarks can be re-enabled
4586 * when scaling is disabled.
4587 *
4588 * WaCxSRDisabledForSpriteScaling:ivb
4589 */
4590 if (pipe_config->disable_lp_wm) {
4591 ilk_disable_lp_wm(dev);
4592 intel_wait_for_vblank(dev, crtc->pipe);
4593 }
4594
4595 /*
4596 * If we're doing a modeset, we're done. No need to do any pre-vblank
4597 * watermark programming here.
4598 */
4599 if (needs_modeset(&pipe_config->base))
4600 return;
4601
4602 /*
4603 * For platforms that support atomic watermarks, program the
4604 * 'intermediate' watermarks immediately. On pre-gen9 platforms, these
4605 * will be the intermediate values that are safe for both pre- and
4606 * post- vblank; when vblank happens, the 'active' values will be set
4607 * to the final 'target' values and we'll do this again to get the
4608 * optimal watermarks. For gen9+ platforms, the values we program here
4609 * will be the final target values which will get automatically latched
4610 * at vblank time; no further programming will be necessary.
4611 *
4612 * If a platform hasn't been transitioned to atomic watermarks yet,
4613 * we'll continue to update watermarks the old way, if flags tell
4614 * us to.
4615 */
4616 if (dev_priv->display.initial_watermarks != NULL)
4617 dev_priv->display.initial_watermarks(pipe_config);
caed361d 4618 else if (pipe_config->update_wm_pre)
92826fcd 4619 intel_update_watermarks(&crtc->base);
ac21b225
ML
4620}
4621
d032ffa0 4622static void intel_crtc_disable_planes(struct drm_crtc *crtc, unsigned plane_mask)
87d4300a
ML
4623{
4624 struct drm_device *dev = crtc->dev;
4625 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
d032ffa0 4626 struct drm_plane *p;
87d4300a
ML
4627 int pipe = intel_crtc->pipe;
4628
7cac945f 4629 intel_crtc_dpms_overlay_disable(intel_crtc);
27321ae8 4630
d032ffa0
ML
4631 drm_for_each_plane_mask(p, dev, plane_mask)
4632 to_intel_plane(p)->disable_plane(p, crtc);
f98551ae 4633
f99d7069
DV
4634 /*
4635 * FIXME: Once we grow proper nuclear flip support out of this we need
4636 * to compute the mask of flip planes precisely. For the time being
4637 * consider this a flip to a NULL plane.
4638 */
4639 intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
a5c4d7bc
VS
4640}
4641
f67a559d
JB
4642static void ironlake_crtc_enable(struct drm_crtc *crtc)
4643{
4644 struct drm_device *dev = crtc->dev;
4645 struct drm_i915_private *dev_priv = dev->dev_private;
4646 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 4647 struct intel_encoder *encoder;
f67a559d 4648 int pipe = intel_crtc->pipe;
b95c5321
ML
4649 struct intel_crtc_state *pipe_config =
4650 to_intel_crtc_state(crtc->state);
f67a559d 4651
53d9f4e9 4652 if (WARN_ON(intel_crtc->active))
f67a559d
JB
4653 return;
4654
b2c0593a
VS
4655 /*
4656 * Sometimes spurious CPU pipe underruns happen during FDI
4657 * training, at least with VGA+HDMI cloning. Suppress them.
4658 *
4659 * On ILK we get an occasional spurious CPU pipe underruns
4660 * between eDP port A enable and vdd enable. Also PCH port
4661 * enable seems to result in the occasional CPU pipe underrun.
4662 *
4663 * Spurious PCH underruns also occur during PCH enabling.
4664 */
4665 if (intel_crtc->config->has_pch_encoder || IS_GEN5(dev_priv))
4666 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
81b088ca
VS
4667 if (intel_crtc->config->has_pch_encoder)
4668 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
4669
6e3c9717 4670 if (intel_crtc->config->has_pch_encoder)
b14b1055
DV
4671 intel_prepare_shared_dpll(intel_crtc);
4672
6e3c9717 4673 if (intel_crtc->config->has_dp_encoder)
fe3cd48d 4674 intel_dp_set_m_n(intel_crtc, M1_N1);
29407aab
DV
4675
4676 intel_set_pipe_timings(intel_crtc);
bc58be60 4677 intel_set_pipe_src_size(intel_crtc);
29407aab 4678
6e3c9717 4679 if (intel_crtc->config->has_pch_encoder) {
29407aab 4680 intel_cpu_transcoder_set_m_n(intel_crtc,
6e3c9717 4681 &intel_crtc->config->fdi_m_n, NULL);
29407aab
DV
4682 }
4683
4684 ironlake_set_pipeconf(crtc);
4685
f67a559d 4686 intel_crtc->active = true;
8664281b 4687
f6736a1a 4688 for_each_encoder_on_crtc(dev, crtc, encoder)
952735ee
DV
4689 if (encoder->pre_enable)
4690 encoder->pre_enable(encoder);
f67a559d 4691
6e3c9717 4692 if (intel_crtc->config->has_pch_encoder) {
fff367c7
DV
4693 /* Note: FDI PLL enabling _must_ be done before we enable the
4694 * cpu pipes, hence this is separate from all the other fdi/pch
4695 * enabling. */
88cefb6c 4696 ironlake_fdi_pll_enable(intel_crtc);
46b6f814
DV
4697 } else {
4698 assert_fdi_tx_disabled(dev_priv, pipe);
4699 assert_fdi_rx_disabled(dev_priv, pipe);
4700 }
f67a559d 4701
b074cec8 4702 ironlake_pfit_enable(intel_crtc);
f67a559d 4703
9c54c0dd
JB
4704 /*
4705 * On ILK+ LUT must be loaded before the pipe is running but with
4706 * clocks enabled
4707 */
b95c5321 4708 intel_color_load_luts(&pipe_config->base);
9c54c0dd 4709
1d5bf5d9
ID
4710 if (dev_priv->display.initial_watermarks != NULL)
4711 dev_priv->display.initial_watermarks(intel_crtc->config);
e1fdc473 4712 intel_enable_pipe(intel_crtc);
f67a559d 4713
6e3c9717 4714 if (intel_crtc->config->has_pch_encoder)
f67a559d 4715 ironlake_pch_enable(crtc);
c98e9dcf 4716
f9b61ff6
DV
4717 assert_vblank_disabled(crtc);
4718 drm_crtc_vblank_on(crtc);
4719
fa5c73b1
DV
4720 for_each_encoder_on_crtc(dev, crtc, encoder)
4721 encoder->enable(encoder);
61b77ddd
DV
4722
4723 if (HAS_PCH_CPT(dev))
a1520318 4724 cpt_verify_modeset(dev, intel_crtc->pipe);
37ca8d4c
VS
4725
4726 /* Must wait for vblank to avoid spurious PCH FIFO underruns */
4727 if (intel_crtc->config->has_pch_encoder)
4728 intel_wait_for_vblank(dev, pipe);
b2c0593a 4729 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
37ca8d4c 4730 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
6be4a607
JB
4731}
4732
42db64ef
PZ
4733/* IPS only exists on ULT machines and is tied to pipe A. */
4734static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
4735{
f5adf94e 4736 return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
42db64ef
PZ
4737}
4738
4f771f10
PZ
4739static void haswell_crtc_enable(struct drm_crtc *crtc)
4740{
4741 struct drm_device *dev = crtc->dev;
4742 struct drm_i915_private *dev_priv = dev->dev_private;
4743 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4744 struct intel_encoder *encoder;
99d736a2 4745 int pipe = intel_crtc->pipe, hsw_workaround_pipe;
4d1de975 4746 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
99d736a2
ML
4747 struct intel_crtc_state *pipe_config =
4748 to_intel_crtc_state(crtc->state);
4f771f10 4749
53d9f4e9 4750 if (WARN_ON(intel_crtc->active))
4f771f10
PZ
4751 return;
4752
81b088ca
VS
4753 if (intel_crtc->config->has_pch_encoder)
4754 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
4755 false);
4756
8106ddbd 4757 if (intel_crtc->config->shared_dpll)
df8ad70c
DV
4758 intel_enable_shared_dpll(intel_crtc);
4759
6e3c9717 4760 if (intel_crtc->config->has_dp_encoder)
fe3cd48d 4761 intel_dp_set_m_n(intel_crtc, M1_N1);
229fca97 4762
4d1de975
JN
4763 if (!intel_crtc->config->has_dsi_encoder)
4764 intel_set_pipe_timings(intel_crtc);
4765
bc58be60 4766 intel_set_pipe_src_size(intel_crtc);
229fca97 4767
4d1de975
JN
4768 if (cpu_transcoder != TRANSCODER_EDP &&
4769 !transcoder_is_dsi(cpu_transcoder)) {
4770 I915_WRITE(PIPE_MULT(cpu_transcoder),
6e3c9717 4771 intel_crtc->config->pixel_multiplier - 1);
ebb69c95
CT
4772 }
4773
6e3c9717 4774 if (intel_crtc->config->has_pch_encoder) {
229fca97 4775 intel_cpu_transcoder_set_m_n(intel_crtc,
6e3c9717 4776 &intel_crtc->config->fdi_m_n, NULL);
229fca97
DV
4777 }
4778
4d1de975
JN
4779 if (!intel_crtc->config->has_dsi_encoder)
4780 haswell_set_pipeconf(crtc);
4781
391bf048 4782 haswell_set_pipemisc(crtc);
229fca97 4783
b95c5321 4784 intel_color_set_csc(&pipe_config->base);
229fca97 4785
4f771f10 4786 intel_crtc->active = true;
8664281b 4787
6b698516
DV
4788 if (intel_crtc->config->has_pch_encoder)
4789 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
4790 else
4791 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4792
7d4aefd0 4793 for_each_encoder_on_crtc(dev, crtc, encoder) {
4f771f10
PZ
4794 if (encoder->pre_enable)
4795 encoder->pre_enable(encoder);
7d4aefd0 4796 }
4f771f10 4797
d2d65408 4798 if (intel_crtc->config->has_pch_encoder)
4fe9467d 4799 dev_priv->display.fdi_link_train(crtc);
4fe9467d 4800
a65347ba 4801 if (!intel_crtc->config->has_dsi_encoder)
7d4aefd0 4802 intel_ddi_enable_pipe_clock(intel_crtc);
4f771f10 4803
1c132b44 4804 if (INTEL_INFO(dev)->gen >= 9)
e435d6e5 4805 skylake_pfit_enable(intel_crtc);
ff6d9f55 4806 else
1c132b44 4807 ironlake_pfit_enable(intel_crtc);
4f771f10
PZ
4808
4809 /*
4810 * On ILK+ LUT must be loaded before the pipe is running but with
4811 * clocks enabled
4812 */
b95c5321 4813 intel_color_load_luts(&pipe_config->base);
4f771f10 4814
1f544388 4815 intel_ddi_set_pipe_settings(crtc);
a65347ba 4816 if (!intel_crtc->config->has_dsi_encoder)
7d4aefd0 4817 intel_ddi_enable_transcoder_func(crtc);
4f771f10 4818
1d5bf5d9
ID
4819 if (dev_priv->display.initial_watermarks != NULL)
4820 dev_priv->display.initial_watermarks(pipe_config);
4821 else
4822 intel_update_watermarks(crtc);
4d1de975
JN
4823
4824 /* XXX: Do the pipe assertions at the right place for BXT DSI. */
4825 if (!intel_crtc->config->has_dsi_encoder)
4826 intel_enable_pipe(intel_crtc);
42db64ef 4827
6e3c9717 4828 if (intel_crtc->config->has_pch_encoder)
1507e5bd 4829 lpt_pch_enable(crtc);
4f771f10 4830
a65347ba 4831 if (intel_crtc->config->dp_encoder_is_mst)
0e32b39c
DA
4832 intel_ddi_set_vc_payload_alloc(crtc, true);
4833
f9b61ff6
DV
4834 assert_vblank_disabled(crtc);
4835 drm_crtc_vblank_on(crtc);
4836
8807e55b 4837 for_each_encoder_on_crtc(dev, crtc, encoder) {
4f771f10 4838 encoder->enable(encoder);
8807e55b
JN
4839 intel_opregion_notify_encoder(encoder, true);
4840 }
4f771f10 4841
6b698516
DV
4842 if (intel_crtc->config->has_pch_encoder) {
4843 intel_wait_for_vblank(dev, pipe);
4844 intel_wait_for_vblank(dev, pipe);
4845 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
d2d65408
VS
4846 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
4847 true);
6b698516 4848 }
d2d65408 4849
e4916946
PZ
4850 /* If we change the relative order between pipe/planes enabling, we need
4851 * to change the workaround. */
99d736a2
ML
4852 hsw_workaround_pipe = pipe_config->hsw_workaround_pipe;
4853 if (IS_HASWELL(dev) && hsw_workaround_pipe != INVALID_PIPE) {
4854 intel_wait_for_vblank(dev, hsw_workaround_pipe);
4855 intel_wait_for_vblank(dev, hsw_workaround_pipe);
4856 }
4f771f10
PZ
4857}
4858
bfd16b2a 4859static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force)
3f8dce3a
DV
4860{
4861 struct drm_device *dev = crtc->base.dev;
4862 struct drm_i915_private *dev_priv = dev->dev_private;
4863 int pipe = crtc->pipe;
4864
4865 /* To avoid upsetting the power well on haswell only disable the pfit if
4866 * it's in use. The hw state code will make sure we get this right. */
bfd16b2a 4867 if (force || crtc->config->pch_pfit.enabled) {
3f8dce3a
DV
4868 I915_WRITE(PF_CTL(pipe), 0);
4869 I915_WRITE(PF_WIN_POS(pipe), 0);
4870 I915_WRITE(PF_WIN_SZ(pipe), 0);
4871 }
4872}
4873
6be4a607
JB
4874static void ironlake_crtc_disable(struct drm_crtc *crtc)
4875{
4876 struct drm_device *dev = crtc->dev;
4877 struct drm_i915_private *dev_priv = dev->dev_private;
4878 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 4879 struct intel_encoder *encoder;
6be4a607 4880 int pipe = intel_crtc->pipe;
b52eb4dc 4881
b2c0593a
VS
4882 /*
4883 * Sometimes spurious CPU pipe underruns happen when the
4884 * pipe is already disabled, but FDI RX/TX is still enabled.
4885 * Happens at least with VGA+HDMI cloning. Suppress them.
4886 */
4887 if (intel_crtc->config->has_pch_encoder) {
4888 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
37ca8d4c 4889 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
b2c0593a 4890 }
37ca8d4c 4891
ea9d758d
DV
4892 for_each_encoder_on_crtc(dev, crtc, encoder)
4893 encoder->disable(encoder);
4894
f9b61ff6
DV
4895 drm_crtc_vblank_off(crtc);
4896 assert_vblank_disabled(crtc);
4897
575f7ab7 4898 intel_disable_pipe(intel_crtc);
32f9d658 4899
bfd16b2a 4900 ironlake_pfit_disable(intel_crtc, false);
2c07245f 4901
b2c0593a 4902 if (intel_crtc->config->has_pch_encoder)
5a74f70a
VS
4903 ironlake_fdi_disable(crtc);
4904
bf49ec8c
DV
4905 for_each_encoder_on_crtc(dev, crtc, encoder)
4906 if (encoder->post_disable)
4907 encoder->post_disable(encoder);
2c07245f 4908
6e3c9717 4909 if (intel_crtc->config->has_pch_encoder) {
d925c59a 4910 ironlake_disable_pch_transcoder(dev_priv, pipe);
6be4a607 4911
d925c59a 4912 if (HAS_PCH_CPT(dev)) {
f0f59a00
VS
4913 i915_reg_t reg;
4914 u32 temp;
4915
d925c59a
DV
4916 /* disable TRANS_DP_CTL */
4917 reg = TRANS_DP_CTL(pipe);
4918 temp = I915_READ(reg);
4919 temp &= ~(TRANS_DP_OUTPUT_ENABLE |
4920 TRANS_DP_PORT_SEL_MASK);
4921 temp |= TRANS_DP_PORT_SEL_NONE;
4922 I915_WRITE(reg, temp);
4923
4924 /* disable DPLL_SEL */
4925 temp = I915_READ(PCH_DPLL_SEL);
11887397 4926 temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
d925c59a 4927 I915_WRITE(PCH_DPLL_SEL, temp);
9db4a9c7 4928 }
e3421a18 4929
d925c59a
DV
4930 ironlake_fdi_pll_disable(intel_crtc);
4931 }
81b088ca 4932
b2c0593a 4933 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
81b088ca 4934 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
6be4a607 4935}
1b3c7a47 4936
4f771f10 4937static void haswell_crtc_disable(struct drm_crtc *crtc)
ee7b9f93 4938{
4f771f10
PZ
4939 struct drm_device *dev = crtc->dev;
4940 struct drm_i915_private *dev_priv = dev->dev_private;
ee7b9f93 4941 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4f771f10 4942 struct intel_encoder *encoder;
6e3c9717 4943 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
ee7b9f93 4944
d2d65408
VS
4945 if (intel_crtc->config->has_pch_encoder)
4946 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
4947 false);
4948
8807e55b
JN
4949 for_each_encoder_on_crtc(dev, crtc, encoder) {
4950 intel_opregion_notify_encoder(encoder, false);
4f771f10 4951 encoder->disable(encoder);
8807e55b 4952 }
4f771f10 4953
f9b61ff6
DV
4954 drm_crtc_vblank_off(crtc);
4955 assert_vblank_disabled(crtc);
4956
4d1de975
JN
4957 /* XXX: Do the pipe assertions at the right place for BXT DSI. */
4958 if (!intel_crtc->config->has_dsi_encoder)
4959 intel_disable_pipe(intel_crtc);
4f771f10 4960
6e3c9717 4961 if (intel_crtc->config->dp_encoder_is_mst)
a4bf214f
VS
4962 intel_ddi_set_vc_payload_alloc(crtc, false);
4963
a65347ba 4964 if (!intel_crtc->config->has_dsi_encoder)
7d4aefd0 4965 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
4f771f10 4966
1c132b44 4967 if (INTEL_INFO(dev)->gen >= 9)
e435d6e5 4968 skylake_scaler_disable(intel_crtc);
ff6d9f55 4969 else
bfd16b2a 4970 ironlake_pfit_disable(intel_crtc, false);
4f771f10 4971
a65347ba 4972 if (!intel_crtc->config->has_dsi_encoder)
7d4aefd0 4973 intel_ddi_disable_pipe_clock(intel_crtc);
4f771f10 4974
97b040aa
ID
4975 for_each_encoder_on_crtc(dev, crtc, encoder)
4976 if (encoder->post_disable)
4977 encoder->post_disable(encoder);
81b088ca 4978
92966a37
VS
4979 if (intel_crtc->config->has_pch_encoder) {
4980 lpt_disable_pch_transcoder(dev_priv);
503a74e9 4981 lpt_disable_iclkip(dev_priv);
92966a37
VS
4982 intel_ddi_fdi_disable(crtc);
4983
81b088ca
VS
4984 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
4985 true);
92966a37 4986 }
4f771f10
PZ
4987}
4988
2dd24552
JB
4989static void i9xx_pfit_enable(struct intel_crtc *crtc)
4990{
4991 struct drm_device *dev = crtc->base.dev;
4992 struct drm_i915_private *dev_priv = dev->dev_private;
6e3c9717 4993 struct intel_crtc_state *pipe_config = crtc->config;
2dd24552 4994
681a8504 4995 if (!pipe_config->gmch_pfit.control)
2dd24552
JB
4996 return;
4997
2dd24552 4998 /*
c0b03411
DV
4999 * The panel fitter should only be adjusted whilst the pipe is disabled,
5000 * according to register description and PRM.
2dd24552 5001 */
c0b03411
DV
5002 WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
5003 assert_pipe_disabled(dev_priv, crtc->pipe);
2dd24552 5004
b074cec8
JB
5005 I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
5006 I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
5a80c45c
DV
5007
5008 /* Border color in case we don't scale up to the full screen. Black by
5009 * default, change to something else for debugging. */
5010 I915_WRITE(BCLRPAT(crtc->pipe), 0);
2dd24552
JB
5011}
5012
d05410f9
DA
5013static enum intel_display_power_domain port_to_power_domain(enum port port)
5014{
5015 switch (port) {
5016 case PORT_A:
6331a704 5017 return POWER_DOMAIN_PORT_DDI_A_LANES;
d05410f9 5018 case PORT_B:
6331a704 5019 return POWER_DOMAIN_PORT_DDI_B_LANES;
d05410f9 5020 case PORT_C:
6331a704 5021 return POWER_DOMAIN_PORT_DDI_C_LANES;
d05410f9 5022 case PORT_D:
6331a704 5023 return POWER_DOMAIN_PORT_DDI_D_LANES;
d8e19f99 5024 case PORT_E:
6331a704 5025 return POWER_DOMAIN_PORT_DDI_E_LANES;
d05410f9 5026 default:
b9fec167 5027 MISSING_CASE(port);
d05410f9
DA
5028 return POWER_DOMAIN_PORT_OTHER;
5029 }
5030}
5031
25f78f58
VS
5032static enum intel_display_power_domain port_to_aux_power_domain(enum port port)
5033{
5034 switch (port) {
5035 case PORT_A:
5036 return POWER_DOMAIN_AUX_A;
5037 case PORT_B:
5038 return POWER_DOMAIN_AUX_B;
5039 case PORT_C:
5040 return POWER_DOMAIN_AUX_C;
5041 case PORT_D:
5042 return POWER_DOMAIN_AUX_D;
5043 case PORT_E:
5044 /* FIXME: Check VBT for actual wiring of PORT E */
5045 return POWER_DOMAIN_AUX_D;
5046 default:
b9fec167 5047 MISSING_CASE(port);
25f78f58
VS
5048 return POWER_DOMAIN_AUX_A;
5049 }
5050}
5051
319be8ae
ID
5052enum intel_display_power_domain
5053intel_display_port_power_domain(struct intel_encoder *intel_encoder)
5054{
5055 struct drm_device *dev = intel_encoder->base.dev;
5056 struct intel_digital_port *intel_dig_port;
5057
5058 switch (intel_encoder->type) {
5059 case INTEL_OUTPUT_UNKNOWN:
5060 /* Only DDI platforms should ever use this output type */
5061 WARN_ON_ONCE(!HAS_DDI(dev));
5062 case INTEL_OUTPUT_DISPLAYPORT:
5063 case INTEL_OUTPUT_HDMI:
5064 case INTEL_OUTPUT_EDP:
5065 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
d05410f9 5066 return port_to_power_domain(intel_dig_port->port);
0e32b39c
DA
5067 case INTEL_OUTPUT_DP_MST:
5068 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
5069 return port_to_power_domain(intel_dig_port->port);
319be8ae
ID
5070 case INTEL_OUTPUT_ANALOG:
5071 return POWER_DOMAIN_PORT_CRT;
5072 case INTEL_OUTPUT_DSI:
5073 return POWER_DOMAIN_PORT_DSI;
5074 default:
5075 return POWER_DOMAIN_PORT_OTHER;
5076 }
5077}
5078
25f78f58
VS
5079enum intel_display_power_domain
5080intel_display_port_aux_power_domain(struct intel_encoder *intel_encoder)
5081{
5082 struct drm_device *dev = intel_encoder->base.dev;
5083 struct intel_digital_port *intel_dig_port;
5084
5085 switch (intel_encoder->type) {
5086 case INTEL_OUTPUT_UNKNOWN:
651174a4
ID
5087 case INTEL_OUTPUT_HDMI:
5088 /*
5089 * Only DDI platforms should ever use these output types.
5090 * We can get here after the HDMI detect code has already set
5091 * the type of the shared encoder. Since we can't be sure
5092 * what's the status of the given connectors, play safe and
5093 * run the DP detection too.
5094 */
25f78f58
VS
5095 WARN_ON_ONCE(!HAS_DDI(dev));
5096 case INTEL_OUTPUT_DISPLAYPORT:
5097 case INTEL_OUTPUT_EDP:
5098 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
5099 return port_to_aux_power_domain(intel_dig_port->port);
5100 case INTEL_OUTPUT_DP_MST:
5101 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
5102 return port_to_aux_power_domain(intel_dig_port->port);
5103 default:
b9fec167 5104 MISSING_CASE(intel_encoder->type);
25f78f58
VS
5105 return POWER_DOMAIN_AUX_A;
5106 }
5107}
5108
74bff5f9
ML
5109static unsigned long get_crtc_power_domains(struct drm_crtc *crtc,
5110 struct intel_crtc_state *crtc_state)
77d22dca 5111{
319be8ae 5112 struct drm_device *dev = crtc->dev;
74bff5f9 5113 struct drm_encoder *encoder;
319be8ae
ID
5114 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5115 enum pipe pipe = intel_crtc->pipe;
77d22dca 5116 unsigned long mask;
74bff5f9 5117 enum transcoder transcoder = crtc_state->cpu_transcoder;
77d22dca 5118
74bff5f9 5119 if (!crtc_state->base.active)
292b990e
ML
5120 return 0;
5121
77d22dca
ID
5122 mask = BIT(POWER_DOMAIN_PIPE(pipe));
5123 mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
74bff5f9
ML
5124 if (crtc_state->pch_pfit.enabled ||
5125 crtc_state->pch_pfit.force_thru)
77d22dca
ID
5126 mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
5127
74bff5f9
ML
5128 drm_for_each_encoder_mask(encoder, dev, crtc_state->base.encoder_mask) {
5129 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
5130
319be8ae 5131 mask |= BIT(intel_display_port_power_domain(intel_encoder));
74bff5f9 5132 }
319be8ae 5133
15e7ec29
ML
5134 if (crtc_state->shared_dpll)
5135 mask |= BIT(POWER_DOMAIN_PLLS);
5136
77d22dca
ID
5137 return mask;
5138}
5139
74bff5f9
ML
5140static unsigned long
5141modeset_get_crtc_power_domains(struct drm_crtc *crtc,
5142 struct intel_crtc_state *crtc_state)
77d22dca 5143{
292b990e
ML
5144 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
5145 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5146 enum intel_display_power_domain domain;
a6747b73 5147 unsigned long domains, new_domains, old_domains, ms_domain = 0;
77d22dca 5148
292b990e 5149 old_domains = intel_crtc->enabled_power_domains;
74bff5f9
ML
5150 intel_crtc->enabled_power_domains = new_domains =
5151 get_crtc_power_domains(crtc, crtc_state);
77d22dca 5152
a6747b73
ML
5153 if (needs_modeset(&crtc_state->base))
5154 ms_domain = BIT(POWER_DOMAIN_MODESET);
5155
5156 domains = (new_domains & ~old_domains) | ms_domain;
292b990e
ML
5157
5158 for_each_power_domain(domain, domains)
5159 intel_display_power_get(dev_priv, domain);
5160
a6747b73 5161 return (old_domains & ~new_domains) | ms_domain;
292b990e
ML
5162}
5163
5164static void modeset_put_power_domains(struct drm_i915_private *dev_priv,
5165 unsigned long domains)
5166{
5167 enum intel_display_power_domain domain;
5168
5169 for_each_power_domain(domain, domains)
5170 intel_display_power_put(dev_priv, domain);
5171}
77d22dca 5172
adafdc6f
MK
5173static int intel_compute_max_dotclk(struct drm_i915_private *dev_priv)
5174{
5175 int max_cdclk_freq = dev_priv->max_cdclk_freq;
5176
5177 if (INTEL_INFO(dev_priv)->gen >= 9 ||
5178 IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
5179 return max_cdclk_freq;
5180 else if (IS_CHERRYVIEW(dev_priv))
5181 return max_cdclk_freq*95/100;
5182 else if (INTEL_INFO(dev_priv)->gen < 4)
5183 return 2*max_cdclk_freq*90/100;
5184 else
5185 return max_cdclk_freq*90/100;
5186}
5187
b2045352
VS
5188static int skl_calc_cdclk(int max_pixclk, int vco);
5189
560a7ae4
DL
5190static void intel_update_max_cdclk(struct drm_device *dev)
5191{
5192 struct drm_i915_private *dev_priv = dev->dev_private;
5193
ef11bdb3 5194 if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) {
560a7ae4 5195 u32 limit = I915_READ(SKL_DFSM) & SKL_DFSM_CDCLK_LIMIT_MASK;
b2045352
VS
5196 int max_cdclk, vco;
5197
5198 vco = dev_priv->skl_preferred_vco_freq;
5199 WARN_ON(vco != 8100 && vco != 8640);
560a7ae4 5200
b2045352
VS
5201 /*
5202 * Use the lower (vco 8640) cdclk values as a
5203 * first guess. skl_calc_cdclk() will correct it
5204 * if the preferred vco is 8100 instead.
5205 */
560a7ae4 5206 if (limit == SKL_DFSM_CDCLK_LIMIT_675)
487ed2e4 5207 max_cdclk = 617143;
560a7ae4 5208 else if (limit == SKL_DFSM_CDCLK_LIMIT_540)
b2045352 5209 max_cdclk = 540000;
560a7ae4 5210 else if (limit == SKL_DFSM_CDCLK_LIMIT_450)
b2045352 5211 max_cdclk = 432000;
560a7ae4 5212 else
487ed2e4 5213 max_cdclk = 308571;
b2045352
VS
5214
5215 dev_priv->max_cdclk_freq = skl_calc_cdclk(max_cdclk, vco);
281c114f
MR
5216 } else if (IS_BROXTON(dev)) {
5217 dev_priv->max_cdclk_freq = 624000;
560a7ae4
DL
5218 } else if (IS_BROADWELL(dev)) {
5219 /*
5220 * FIXME with extra cooling we can allow
5221 * 540 MHz for ULX and 675 Mhz for ULT.
5222 * How can we know if extra cooling is
5223 * available? PCI ID, VTB, something else?
5224 */
5225 if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
5226 dev_priv->max_cdclk_freq = 450000;
5227 else if (IS_BDW_ULX(dev))
5228 dev_priv->max_cdclk_freq = 450000;
5229 else if (IS_BDW_ULT(dev))
5230 dev_priv->max_cdclk_freq = 540000;
5231 else
5232 dev_priv->max_cdclk_freq = 675000;
0904deaf
MK
5233 } else if (IS_CHERRYVIEW(dev)) {
5234 dev_priv->max_cdclk_freq = 320000;
560a7ae4
DL
5235 } else if (IS_VALLEYVIEW(dev)) {
5236 dev_priv->max_cdclk_freq = 400000;
5237 } else {
5238 /* otherwise assume cdclk is fixed */
5239 dev_priv->max_cdclk_freq = dev_priv->cdclk_freq;
5240 }
5241
adafdc6f
MK
5242 dev_priv->max_dotclk_freq = intel_compute_max_dotclk(dev_priv);
5243
560a7ae4
DL
5244 DRM_DEBUG_DRIVER("Max CD clock rate: %d kHz\n",
5245 dev_priv->max_cdclk_freq);
adafdc6f
MK
5246
5247 DRM_DEBUG_DRIVER("Max dotclock rate: %d kHz\n",
5248 dev_priv->max_dotclk_freq);
560a7ae4
DL
5249}
5250
5251static void intel_update_cdclk(struct drm_device *dev)
5252{
5253 struct drm_i915_private *dev_priv = dev->dev_private;
5254
5255 dev_priv->cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
2f2a121a
VS
5256
5257 if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv))
5258 DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz, VCO: %d MHz\n",
5259 dev_priv->cdclk_freq, dev_priv->skl_vco_freq);
5260 else
5261 DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
5262 dev_priv->cdclk_freq);
560a7ae4
DL
5263
5264 /*
b5d99ff9
VS
5265 * 9:0 CMBUS [sic] CDCLK frequency (cdfreq):
5266 * Programmng [sic] note: bit[9:2] should be programmed to the number
5267 * of cdclk that generates 4MHz reference clock freq which is used to
5268 * generate GMBus clock. This will vary with the cdclk freq.
560a7ae4 5269 */
b5d99ff9 5270 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
560a7ae4 5271 I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->cdclk_freq, 1000));
560a7ae4
DL
5272}
5273
92891e45
VS
5274/* convert from kHz to .1 fixpoint MHz with -1MHz offset */
5275static int skl_cdclk_decimal(int cdclk)
5276{
5277 return DIV_ROUND_CLOSEST(cdclk - 1000, 500);
5278}
5279
9ef56154 5280static void broxton_set_cdclk(struct drm_i915_private *dev_priv, int cdclk)
f8437dd1 5281{
f8437dd1
VK
5282 uint32_t divider;
5283 uint32_t ratio;
9ef56154 5284 uint32_t current_cdclk;
f8437dd1
VK
5285 int ret;
5286
5287 /* frequency = 19.2MHz * ratio / 2 / div{1,1.5,2,4} */
9ef56154 5288 switch (cdclk) {
f8437dd1
VK
5289 case 144000:
5290 divider = BXT_CDCLK_CD2X_DIV_SEL_4;
5291 ratio = BXT_DE_PLL_RATIO(60);
5292 break;
5293 case 288000:
5294 divider = BXT_CDCLK_CD2X_DIV_SEL_2;
5295 ratio = BXT_DE_PLL_RATIO(60);
5296 break;
5297 case 384000:
5298 divider = BXT_CDCLK_CD2X_DIV_SEL_1_5;
5299 ratio = BXT_DE_PLL_RATIO(60);
5300 break;
5301 case 576000:
5302 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
5303 ratio = BXT_DE_PLL_RATIO(60);
5304 break;
5305 case 624000:
5306 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
5307 ratio = BXT_DE_PLL_RATIO(65);
5308 break;
5309 case 19200:
5310 /*
5311 * Bypass frequency with DE PLL disabled. Init ratio, divider
5312 * to suppress GCC warning.
5313 */
5314 ratio = 0;
5315 divider = 0;
5316 break;
5317 default:
9ef56154 5318 DRM_ERROR("unsupported CDCLK freq %d", cdclk);
f8437dd1
VK
5319
5320 return;
5321 }
5322
5323 mutex_lock(&dev_priv->rps.hw_lock);
5324 /* Inform power controller of upcoming frequency change */
5325 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
5326 0x80000000);
5327 mutex_unlock(&dev_priv->rps.hw_lock);
5328
5329 if (ret) {
5330 DRM_ERROR("PCode CDCLK freq change notify failed (err %d, freq %d)\n",
9ef56154 5331 ret, cdclk);
f8437dd1
VK
5332 return;
5333 }
5334
9ef56154 5335 current_cdclk = I915_READ(CDCLK_CTL) & CDCLK_FREQ_DECIMAL_MASK;
f8437dd1 5336 /* convert from .1 fixpoint MHz with -1MHz offset to kHz */
9ef56154 5337 current_cdclk = current_cdclk * 500 + 1000;
f8437dd1
VK
5338
5339 /*
5340 * DE PLL has to be disabled when
5341 * - setting to 19.2MHz (bypass, PLL isn't used)
5342 * - before setting to 624MHz (PLL needs toggling)
5343 * - before setting to any frequency from 624MHz (PLL needs toggling)
5344 */
9ef56154
VS
5345 if (cdclk == 19200 || cdclk == 624000 ||
5346 current_cdclk == 624000) {
f8437dd1
VK
5347 I915_WRITE(BXT_DE_PLL_ENABLE, ~BXT_DE_PLL_PLL_ENABLE);
5348 /* Timeout 200us */
5349 if (wait_for(!(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK),
5350 1))
5351 DRM_ERROR("timout waiting for DE PLL unlock\n");
5352 }
5353
9ef56154 5354 if (cdclk != 19200) {
f8437dd1
VK
5355 uint32_t val;
5356
5357 val = I915_READ(BXT_DE_PLL_CTL);
5358 val &= ~BXT_DE_PLL_RATIO_MASK;
5359 val |= ratio;
5360 I915_WRITE(BXT_DE_PLL_CTL, val);
5361
5362 I915_WRITE(BXT_DE_PLL_ENABLE, BXT_DE_PLL_PLL_ENABLE);
5363 /* Timeout 200us */
5364 if (wait_for(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK, 1))
5365 DRM_ERROR("timeout waiting for DE PLL lock\n");
5366
b8e75705 5367 val = divider | skl_cdclk_decimal(cdclk);
7fe62757
VS
5368 /*
5369 * FIXME if only the cd2x divider needs changing, it could be done
5370 * without shutting off the pipe (if only one pipe is active).
5371 */
5372 val |= BXT_CDCLK_CD2X_PIPE_NONE;
f8437dd1
VK
5373 /*
5374 * Disable SSA Precharge when CD clock frequency < 500 MHz,
5375 * enable otherwise.
5376 */
9ef56154 5377 if (cdclk >= 500000)
f8437dd1 5378 val |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
f8437dd1
VK
5379 I915_WRITE(CDCLK_CTL, val);
5380 }
5381
5382 mutex_lock(&dev_priv->rps.hw_lock);
5383 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
9ef56154 5384 DIV_ROUND_UP(cdclk, 25000));
f8437dd1
VK
5385 mutex_unlock(&dev_priv->rps.hw_lock);
5386
5387 if (ret) {
5388 DRM_ERROR("PCode CDCLK freq set failed, (err %d, freq %d)\n",
9ef56154 5389 ret, cdclk);
f8437dd1
VK
5390 return;
5391 }
5392
c6c4696f 5393 intel_update_cdclk(dev_priv->dev);
f8437dd1
VK
5394}
5395
c2e001ef
ID
5396static bool broxton_cdclk_is_enabled(struct drm_i915_private *dev_priv)
5397{
5398 if (!(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_PLL_ENABLE))
5399 return false;
5400
5401 /* TODO: Check for a valid CDCLK rate */
5402
c2e001ef
ID
5403 return true;
5404}
5405
adc7f04b
ID
5406bool broxton_cdclk_verify_state(struct drm_i915_private *dev_priv)
5407{
5408 return broxton_cdclk_is_enabled(dev_priv);
5409}
5410
c6c4696f 5411void broxton_init_cdclk(struct drm_i915_private *dev_priv)
f8437dd1 5412{
f8437dd1 5413 /* check if cd clock is enabled */
c2e001ef
ID
5414 if (broxton_cdclk_is_enabled(dev_priv)) {
5415 DRM_DEBUG_KMS("CDCLK already enabled, won't reprogram it\n");
f8437dd1
VK
5416 return;
5417 }
5418
c2e001ef
ID
5419 DRM_DEBUG_KMS("CDCLK not enabled, enabling it\n");
5420
f8437dd1
VK
5421 /*
5422 * FIXME:
5423 * - The initial CDCLK needs to be read from VBT.
5424 * Need to make this change after VBT has changes for BXT.
5425 * - check if setting the max (or any) cdclk freq is really necessary
5426 * here, it belongs to modeset time
5427 */
c6c4696f 5428 broxton_set_cdclk(dev_priv, 624000);
f8437dd1
VK
5429}
5430
c6c4696f 5431void broxton_uninit_cdclk(struct drm_i915_private *dev_priv)
f8437dd1 5432{
f8437dd1 5433 /* Set minimum (bypass) frequency, in effect turning off the DE PLL */
c6c4696f 5434 broxton_set_cdclk(dev_priv, 19200);
f8437dd1
VK
5435}
5436
a8ca4934
VS
5437static int skl_calc_cdclk(int max_pixclk, int vco)
5438{
5439 if (vco == 8640) {
5440 if (max_pixclk > 540000)
487ed2e4 5441 return 617143;
a8ca4934
VS
5442 else if (max_pixclk > 432000)
5443 return 540000;
487ed2e4 5444 else if (max_pixclk > 308571)
a8ca4934
VS
5445 return 432000;
5446 else
487ed2e4 5447 return 308571;
a8ca4934
VS
5448 } else {
5449 /* VCO 8100 */
5450 if (max_pixclk > 540000)
5451 return 675000;
5452 else if (max_pixclk > 450000)
5453 return 540000;
5454 else if (max_pixclk > 337500)
5455 return 450000;
5456 else
5457 return 337500;
5458 }
5459}
5460
ea61791e
VS
5461static void
5462skl_dpll0_update(struct drm_i915_private *dev_priv)
5d96d8af 5463{
ea61791e 5464 u32 val;
5d96d8af 5465
ea61791e
VS
5466 val = I915_READ(LCPLL1_CTL);
5467 if ((val & LCPLL_PLL_ENABLE) == 0) {
5468 dev_priv->skl_vco_freq = 0;
5469 return;
5d96d8af
DL
5470 }
5471
9f7eb31a
VS
5472 WARN_ON((val & LCPLL_PLL_LOCK) == 0);
5473
ea61791e
VS
5474 val = I915_READ(DPLL_CTRL1);
5475
9f7eb31a
VS
5476 WARN_ON((val & (DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) |
5477 DPLL_CTRL1_SSC(SKL_DPLL0) |
5478 DPLL_CTRL1_OVERRIDE(SKL_DPLL0))) !=
5479 DPLL_CTRL1_OVERRIDE(SKL_DPLL0));
5480
ea61791e
VS
5481 switch (val & DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0)) {
5482 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810, SKL_DPLL0):
5483 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1350, SKL_DPLL0):
5484 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1620, SKL_DPLL0):
5485 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_2700, SKL_DPLL0):
5486 dev_priv->skl_vco_freq = 8100;
5487 break;
5488 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080, SKL_DPLL0):
5489 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_2160, SKL_DPLL0):
5490 dev_priv->skl_vco_freq = 8640;
5491 break;
5492 default:
5493 MISSING_CASE(val & DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
5494 dev_priv->skl_vco_freq = 0;
5495 break;
5496 }
5d96d8af
DL
5497}
5498
b2045352
VS
5499void skl_set_preferred_cdclk_vco(struct drm_i915_private *dev_priv, int vco)
5500{
5501 bool changed = dev_priv->skl_preferred_vco_freq != vco;
5502
5503 dev_priv->skl_preferred_vco_freq = vco;
5504
5505 if (changed)
5506 intel_update_max_cdclk(dev_priv->dev);
5507}
5508
5d96d8af 5509static void
3861fc60 5510skl_dpll0_enable(struct drm_i915_private *dev_priv, int vco)
5d96d8af 5511{
a8ca4934 5512 int min_cdclk = skl_calc_cdclk(0, vco);
5d96d8af
DL
5513 u32 val;
5514
b2045352
VS
5515 WARN_ON(vco != 8100 && vco != 8640);
5516
5d96d8af 5517 /* select the minimum CDCLK before enabling DPLL 0 */
9ef56154 5518 val = CDCLK_FREQ_337_308 | skl_cdclk_decimal(min_cdclk);
5d96d8af
DL
5519 I915_WRITE(CDCLK_CTL, val);
5520 POSTING_READ(CDCLK_CTL);
5521
5522 /*
5523 * We always enable DPLL0 with the lowest link rate possible, but still
5524 * taking into account the VCO required to operate the eDP panel at the
5525 * desired frequency. The usual DP link rates operate with a VCO of
5526 * 8100 while the eDP 1.4 alternate link rates need a VCO of 8640.
5527 * The modeset code is responsible for the selection of the exact link
5528 * rate later on, with the constraint of choosing a frequency that
a8ca4934 5529 * works with vco.
5d96d8af
DL
5530 */
5531 val = I915_READ(DPLL_CTRL1);
5532
5533 val &= ~(DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) | DPLL_CTRL1_SSC(SKL_DPLL0) |
5534 DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
5535 val |= DPLL_CTRL1_OVERRIDE(SKL_DPLL0);
3861fc60 5536 if (vco == 8640)
5d96d8af
DL
5537 val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080,
5538 SKL_DPLL0);
5539 else
5540 val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810,
5541 SKL_DPLL0);
5542
5543 I915_WRITE(DPLL_CTRL1, val);
5544 POSTING_READ(DPLL_CTRL1);
5545
5546 I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) | LCPLL_PLL_ENABLE);
5547
5548 if (wait_for(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK, 5))
5549 DRM_ERROR("DPLL0 not locked\n");
1cd593e0
VS
5550
5551 dev_priv->skl_vco_freq = vco;
b2045352
VS
5552
5553 /* We'll want to keep using the current vco from now on. */
5554 skl_set_preferred_cdclk_vco(dev_priv, vco);
5d96d8af
DL
5555}
5556
430e05de
VS
5557static void
5558skl_dpll0_disable(struct drm_i915_private *dev_priv)
5559{
5560 I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) & ~LCPLL_PLL_ENABLE);
5561 if (wait_for(!(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK), 1))
5562 DRM_ERROR("Couldn't disable DPLL0\n");
1cd593e0
VS
5563
5564 dev_priv->skl_vco_freq = 0;
430e05de
VS
5565}
5566
5d96d8af
DL
5567static bool skl_cdclk_pcu_ready(struct drm_i915_private *dev_priv)
5568{
5569 int ret;
5570 u32 val;
5571
5572 /* inform PCU we want to change CDCLK */
5573 val = SKL_CDCLK_PREPARE_FOR_CHANGE;
5574 mutex_lock(&dev_priv->rps.hw_lock);
5575 ret = sandybridge_pcode_read(dev_priv, SKL_PCODE_CDCLK_CONTROL, &val);
5576 mutex_unlock(&dev_priv->rps.hw_lock);
5577
5578 return ret == 0 && (val & SKL_CDCLK_READY_FOR_CHANGE);
5579}
5580
5581static bool skl_cdclk_wait_for_pcu_ready(struct drm_i915_private *dev_priv)
5582{
5583 unsigned int i;
5584
5585 for (i = 0; i < 15; i++) {
5586 if (skl_cdclk_pcu_ready(dev_priv))
5587 return true;
5588 udelay(10);
5589 }
5590
5591 return false;
5592}
5593
1cd593e0 5594static void skl_set_cdclk(struct drm_i915_private *dev_priv, int cdclk, int vco)
5d96d8af 5595{
560a7ae4 5596 struct drm_device *dev = dev_priv->dev;
5d96d8af
DL
5597 u32 freq_select, pcu_ack;
5598
1cd593e0
VS
5599 WARN_ON((cdclk == 24000) != (vco == 0));
5600
5601 DRM_DEBUG_DRIVER("Changing CDCLK to %d kHz (VCO %d MHz)\n", cdclk, vco);
5d96d8af
DL
5602
5603 if (!skl_cdclk_wait_for_pcu_ready(dev_priv)) {
5604 DRM_ERROR("failed to inform PCU about cdclk change\n");
5605 return;
5606 }
5607
5608 /* set CDCLK_CTL */
9ef56154 5609 switch (cdclk) {
5d96d8af
DL
5610 case 450000:
5611 case 432000:
5612 freq_select = CDCLK_FREQ_450_432;
5613 pcu_ack = 1;
5614 break;
5615 case 540000:
5616 freq_select = CDCLK_FREQ_540;
5617 pcu_ack = 2;
5618 break;
487ed2e4 5619 case 308571:
5d96d8af
DL
5620 case 337500:
5621 default:
5622 freq_select = CDCLK_FREQ_337_308;
5623 pcu_ack = 0;
5624 break;
487ed2e4 5625 case 617143:
5d96d8af
DL
5626 case 675000:
5627 freq_select = CDCLK_FREQ_675_617;
5628 pcu_ack = 3;
5629 break;
5630 }
5631
1cd593e0
VS
5632 if (dev_priv->skl_vco_freq != 0 &&
5633 dev_priv->skl_vco_freq != vco)
5634 skl_dpll0_disable(dev_priv);
5635
5636 if (dev_priv->skl_vco_freq != vco)
5637 skl_dpll0_enable(dev_priv, vco);
5638
9ef56154 5639 I915_WRITE(CDCLK_CTL, freq_select | skl_cdclk_decimal(cdclk));
5d96d8af
DL
5640 POSTING_READ(CDCLK_CTL);
5641
5642 /* inform PCU of the change */
5643 mutex_lock(&dev_priv->rps.hw_lock);
5644 sandybridge_pcode_write(dev_priv, SKL_PCODE_CDCLK_CONTROL, pcu_ack);
5645 mutex_unlock(&dev_priv->rps.hw_lock);
560a7ae4
DL
5646
5647 intel_update_cdclk(dev);
5d96d8af
DL
5648}
5649
9f7eb31a
VS
5650static void skl_sanitize_cdclk(struct drm_i915_private *dev_priv);
5651
5d96d8af
DL
5652void skl_uninit_cdclk(struct drm_i915_private *dev_priv)
5653{
1cd593e0 5654 skl_set_cdclk(dev_priv, 24000, 0);
5d96d8af
DL
5655}
5656
5657void skl_init_cdclk(struct drm_i915_private *dev_priv)
5658{
9f7eb31a
VS
5659 int cdclk, vco;
5660
5661 skl_sanitize_cdclk(dev_priv);
5d96d8af 5662
9f7eb31a
VS
5663 if (dev_priv->cdclk_freq != 0 && dev_priv->skl_vco_freq != 0) {
5664 /*
5665 * Use the current vco as our initial
5666 * guess as to what the preferred vco is.
5667 */
5668 if (dev_priv->skl_preferred_vco_freq == 0)
5669 skl_set_preferred_cdclk_vco(dev_priv,
5670 dev_priv->skl_vco_freq);
70c2c184 5671 return;
1cd593e0 5672 }
5d96d8af 5673
70c2c184
VS
5674 vco = dev_priv->skl_preferred_vco_freq;
5675 if (vco == 0)
5676 vco = 8100;
5677 cdclk = skl_calc_cdclk(0, vco);
5d96d8af 5678
70c2c184 5679 skl_set_cdclk(dev_priv, cdclk, vco);
5d96d8af
DL
5680}
5681
9f7eb31a 5682static void skl_sanitize_cdclk(struct drm_i915_private *dev_priv)
c73666f3 5683{
09492498 5684 uint32_t cdctl, expected;
c73666f3 5685
f1b391a5
SK
5686 /*
5687 * check if the pre-os intialized the display
5688 * There is SWF18 scratchpad register defined which is set by the
5689 * pre-os which can be used by the OS drivers to check the status
5690 */
5691 if ((I915_READ(SWF_ILK(0x18)) & 0x00FFFFFF) == 0)
5692 goto sanitize;
5693
c73666f3 5694 /* Is PLL enabled and locked ? */
09492498
VS
5695 if ((I915_READ(LCPLL1_CTL) & (LCPLL_PLL_ENABLE | LCPLL_PLL_LOCK)) !=
5696 (LCPLL_PLL_ENABLE | LCPLL_PLL_LOCK))
5697 goto sanitize;
5698
5699 if ((I915_READ(DPLL_CTRL1) & (DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) |
5700 DPLL_CTRL1_SSC(SKL_DPLL0) |
5701 DPLL_CTRL1_OVERRIDE(SKL_DPLL0))) !=
5702 DPLL_CTRL1_OVERRIDE(SKL_DPLL0))
c73666f3
SK
5703 goto sanitize;
5704
9f7eb31a
VS
5705 intel_update_cdclk(dev_priv->dev);
5706
c73666f3
SK
5707 /* DPLL okay; verify the cdclock
5708 *
5709 * Noticed in some instances that the freq selection is correct but
5710 * decimal part is programmed wrong from BIOS where pre-os does not
5711 * enable display. Verify the same as well.
5712 */
09492498
VS
5713 cdctl = I915_READ(CDCLK_CTL);
5714 expected = (cdctl & CDCLK_FREQ_SEL_MASK) |
5715 skl_cdclk_decimal(dev_priv->cdclk_freq);
5716 if (cdctl == expected)
c73666f3 5717 /* All well; nothing to sanitize */
9f7eb31a 5718 return;
c89e39f3 5719
9f7eb31a
VS
5720sanitize:
5721 DRM_DEBUG_KMS("Sanitizing cdclk programmed by pre-os\n");
c73666f3 5722
9f7eb31a
VS
5723 /* force cdclk programming */
5724 dev_priv->cdclk_freq = 0;
5725 /* force full PLL disable + enable */
5726 dev_priv->skl_vco_freq = -1;
c73666f3
SK
5727}
5728
30a970c6
JB
5729/* Adjust CDclk dividers to allow high res or save power if possible */
5730static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
5731{
5732 struct drm_i915_private *dev_priv = dev->dev_private;
5733 u32 val, cmd;
5734
164dfd28
VK
5735 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5736 != dev_priv->cdclk_freq);
d60c4473 5737
dfcab17e 5738 if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
30a970c6 5739 cmd = 2;
dfcab17e 5740 else if (cdclk == 266667)
30a970c6
JB
5741 cmd = 1;
5742 else
5743 cmd = 0;
5744
5745 mutex_lock(&dev_priv->rps.hw_lock);
5746 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5747 val &= ~DSPFREQGUAR_MASK;
5748 val |= (cmd << DSPFREQGUAR_SHIFT);
5749 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5750 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5751 DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
5752 50)) {
5753 DRM_ERROR("timed out waiting for CDclk change\n");
5754 }
5755 mutex_unlock(&dev_priv->rps.hw_lock);
5756
54433e91
VS
5757 mutex_lock(&dev_priv->sb_lock);
5758
dfcab17e 5759 if (cdclk == 400000) {
6bcda4f0 5760 u32 divider;
30a970c6 5761
6bcda4f0 5762 divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
30a970c6 5763
30a970c6
JB
5764 /* adjust cdclk divider */
5765 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
87d5d259 5766 val &= ~CCK_FREQUENCY_VALUES;
30a970c6
JB
5767 val |= divider;
5768 vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
a877e801
VS
5769
5770 if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
87d5d259 5771 CCK_FREQUENCY_STATUS) == (divider << CCK_FREQUENCY_STATUS_SHIFT),
a877e801
VS
5772 50))
5773 DRM_ERROR("timed out waiting for CDclk change\n");
30a970c6
JB
5774 }
5775
30a970c6
JB
5776 /* adjust self-refresh exit latency value */
5777 val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
5778 val &= ~0x7f;
5779
5780 /*
5781 * For high bandwidth configs, we set a higher latency in the bunit
5782 * so that the core display fetch happens in time to avoid underruns.
5783 */
dfcab17e 5784 if (cdclk == 400000)
30a970c6
JB
5785 val |= 4500 / 250; /* 4.5 usec */
5786 else
5787 val |= 3000 / 250; /* 3.0 usec */
5788 vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
54433e91 5789
a580516d 5790 mutex_unlock(&dev_priv->sb_lock);
30a970c6 5791
b6283055 5792 intel_update_cdclk(dev);
30a970c6
JB
5793}
5794
383c5a6a
VS
5795static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
5796{
5797 struct drm_i915_private *dev_priv = dev->dev_private;
5798 u32 val, cmd;
5799
164dfd28
VK
5800 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5801 != dev_priv->cdclk_freq);
383c5a6a
VS
5802
5803 switch (cdclk) {
383c5a6a
VS
5804 case 333333:
5805 case 320000:
383c5a6a 5806 case 266667:
383c5a6a 5807 case 200000:
383c5a6a
VS
5808 break;
5809 default:
5f77eeb0 5810 MISSING_CASE(cdclk);
383c5a6a
VS
5811 return;
5812 }
5813
9d0d3fda
VS
5814 /*
5815 * Specs are full of misinformation, but testing on actual
5816 * hardware has shown that we just need to write the desired
5817 * CCK divider into the Punit register.
5818 */
5819 cmd = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
5820
383c5a6a
VS
5821 mutex_lock(&dev_priv->rps.hw_lock);
5822 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5823 val &= ~DSPFREQGUAR_MASK_CHV;
5824 val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
5825 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5826 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5827 DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
5828 50)) {
5829 DRM_ERROR("timed out waiting for CDclk change\n");
5830 }
5831 mutex_unlock(&dev_priv->rps.hw_lock);
5832
b6283055 5833 intel_update_cdclk(dev);
383c5a6a
VS
5834}
5835
30a970c6
JB
5836static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
5837 int max_pixclk)
5838{
6bcda4f0 5839 int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
6cca3195 5840 int limit = IS_CHERRYVIEW(dev_priv) ? 95 : 90;
29dc7ef3 5841
30a970c6
JB
5842 /*
5843 * Really only a few cases to deal with, as only 4 CDclks are supported:
5844 * 200MHz
5845 * 267MHz
29dc7ef3 5846 * 320/333MHz (depends on HPLL freq)
6cca3195
VS
5847 * 400MHz (VLV only)
5848 * So we check to see whether we're above 90% (VLV) or 95% (CHV)
5849 * of the lower bin and adjust if needed.
e37c67a1
VS
5850 *
5851 * We seem to get an unstable or solid color picture at 200MHz.
5852 * Not sure what's wrong. For now use 200MHz only when all pipes
5853 * are off.
30a970c6 5854 */
6cca3195
VS
5855 if (!IS_CHERRYVIEW(dev_priv) &&
5856 max_pixclk > freq_320*limit/100)
dfcab17e 5857 return 400000;
6cca3195 5858 else if (max_pixclk > 266667*limit/100)
29dc7ef3 5859 return freq_320;
e37c67a1 5860 else if (max_pixclk > 0)
dfcab17e 5861 return 266667;
e37c67a1
VS
5862 else
5863 return 200000;
30a970c6
JB
5864}
5865
c44deb6c 5866static int broxton_calc_cdclk(int max_pixclk)
f8437dd1
VK
5867{
5868 /*
5869 * FIXME:
f8437dd1
VK
5870 * - set 19.2MHz bypass frequency if there are no active pipes
5871 */
760e1477 5872 if (max_pixclk > 576000)
f8437dd1 5873 return 624000;
760e1477 5874 else if (max_pixclk > 384000)
f8437dd1 5875 return 576000;
760e1477 5876 else if (max_pixclk > 288000)
f8437dd1 5877 return 384000;
760e1477 5878 else if (max_pixclk > 144000)
f8437dd1
VK
5879 return 288000;
5880 else
5881 return 144000;
5882}
5883
e8788cbc 5884/* Compute the max pixel clock for new configuration. */
a821fc46
ACO
5885static int intel_mode_max_pixclk(struct drm_device *dev,
5886 struct drm_atomic_state *state)
30a970c6 5887{
565602d7
ML
5888 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
5889 struct drm_i915_private *dev_priv = dev->dev_private;
5890 struct drm_crtc *crtc;
5891 struct drm_crtc_state *crtc_state;
5892 unsigned max_pixclk = 0, i;
5893 enum pipe pipe;
30a970c6 5894
565602d7
ML
5895 memcpy(intel_state->min_pixclk, dev_priv->min_pixclk,
5896 sizeof(intel_state->min_pixclk));
304603f4 5897
565602d7
ML
5898 for_each_crtc_in_state(state, crtc, crtc_state, i) {
5899 int pixclk = 0;
5900
5901 if (crtc_state->enable)
5902 pixclk = crtc_state->adjusted_mode.crtc_clock;
304603f4 5903
565602d7 5904 intel_state->min_pixclk[i] = pixclk;
30a970c6
JB
5905 }
5906
565602d7
ML
5907 for_each_pipe(dev_priv, pipe)
5908 max_pixclk = max(intel_state->min_pixclk[pipe], max_pixclk);
5909
30a970c6
JB
5910 return max_pixclk;
5911}
5912
27c329ed 5913static int valleyview_modeset_calc_cdclk(struct drm_atomic_state *state)
30a970c6 5914{
27c329ed
ML
5915 struct drm_device *dev = state->dev;
5916 struct drm_i915_private *dev_priv = dev->dev_private;
5917 int max_pixclk = intel_mode_max_pixclk(dev, state);
1a617b77
ML
5918 struct intel_atomic_state *intel_state =
5919 to_intel_atomic_state(state);
30a970c6 5920
1a617b77 5921 intel_state->cdclk = intel_state->dev_cdclk =
27c329ed 5922 valleyview_calc_cdclk(dev_priv, max_pixclk);
0a9ab303 5923
1a617b77
ML
5924 if (!intel_state->active_crtcs)
5925 intel_state->dev_cdclk = valleyview_calc_cdclk(dev_priv, 0);
5926
27c329ed
ML
5927 return 0;
5928}
304603f4 5929
27c329ed
ML
5930static int broxton_modeset_calc_cdclk(struct drm_atomic_state *state)
5931{
4e5ca60f 5932 int max_pixclk = ilk_max_pixel_rate(state);
1a617b77
ML
5933 struct intel_atomic_state *intel_state =
5934 to_intel_atomic_state(state);
85a96e7a 5935
1a617b77 5936 intel_state->cdclk = intel_state->dev_cdclk =
c44deb6c 5937 broxton_calc_cdclk(max_pixclk);
85a96e7a 5938
1a617b77 5939 if (!intel_state->active_crtcs)
c44deb6c 5940 intel_state->dev_cdclk = broxton_calc_cdclk(0);
1a617b77 5941
27c329ed 5942 return 0;
30a970c6
JB
5943}
5944
1e69cd74
VS
5945static void vlv_program_pfi_credits(struct drm_i915_private *dev_priv)
5946{
5947 unsigned int credits, default_credits;
5948
5949 if (IS_CHERRYVIEW(dev_priv))
5950 default_credits = PFI_CREDIT(12);
5951 else
5952 default_credits = PFI_CREDIT(8);
5953
bfa7df01 5954 if (dev_priv->cdclk_freq >= dev_priv->czclk_freq) {
1e69cd74
VS
5955 /* CHV suggested value is 31 or 63 */
5956 if (IS_CHERRYVIEW(dev_priv))
fcc0008f 5957 credits = PFI_CREDIT_63;
1e69cd74
VS
5958 else
5959 credits = PFI_CREDIT(15);
5960 } else {
5961 credits = default_credits;
5962 }
5963
5964 /*
5965 * WA - write default credits before re-programming
5966 * FIXME: should we also set the resend bit here?
5967 */
5968 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
5969 default_credits);
5970
5971 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
5972 credits | PFI_CREDIT_RESEND);
5973
5974 /*
5975 * FIXME is this guaranteed to clear
5976 * immediately or should we poll for it?
5977 */
5978 WARN_ON(I915_READ(GCI_CONTROL) & PFI_CREDIT_RESEND);
5979}
5980
27c329ed 5981static void valleyview_modeset_commit_cdclk(struct drm_atomic_state *old_state)
30a970c6 5982{
a821fc46 5983 struct drm_device *dev = old_state->dev;
30a970c6 5984 struct drm_i915_private *dev_priv = dev->dev_private;
1a617b77
ML
5985 struct intel_atomic_state *old_intel_state =
5986 to_intel_atomic_state(old_state);
5987 unsigned req_cdclk = old_intel_state->dev_cdclk;
30a970c6 5988
27c329ed
ML
5989 /*
5990 * FIXME: We can end up here with all power domains off, yet
5991 * with a CDCLK frequency other than the minimum. To account
5992 * for this take the PIPE-A power domain, which covers the HW
5993 * blocks needed for the following programming. This can be
5994 * removed once it's guaranteed that we get here either with
5995 * the minimum CDCLK set, or the required power domains
5996 * enabled.
5997 */
5998 intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
738c05c0 5999
27c329ed
ML
6000 if (IS_CHERRYVIEW(dev))
6001 cherryview_set_cdclk(dev, req_cdclk);
6002 else
6003 valleyview_set_cdclk(dev, req_cdclk);
738c05c0 6004
27c329ed 6005 vlv_program_pfi_credits(dev_priv);
1e69cd74 6006
27c329ed 6007 intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
30a970c6
JB
6008}
6009
89b667f8
JB
6010static void valleyview_crtc_enable(struct drm_crtc *crtc)
6011{
6012 struct drm_device *dev = crtc->dev;
a72e4c9f 6013 struct drm_i915_private *dev_priv = to_i915(dev);
89b667f8
JB
6014 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6015 struct intel_encoder *encoder;
b95c5321
ML
6016 struct intel_crtc_state *pipe_config =
6017 to_intel_crtc_state(crtc->state);
89b667f8 6018 int pipe = intel_crtc->pipe;
89b667f8 6019
53d9f4e9 6020 if (WARN_ON(intel_crtc->active))
89b667f8
JB
6021 return;
6022
6e3c9717 6023 if (intel_crtc->config->has_dp_encoder)
fe3cd48d 6024 intel_dp_set_m_n(intel_crtc, M1_N1);
5b18e57c
DV
6025
6026 intel_set_pipe_timings(intel_crtc);
bc58be60 6027 intel_set_pipe_src_size(intel_crtc);
5b18e57c 6028
c14b0485
VS
6029 if (IS_CHERRYVIEW(dev) && pipe == PIPE_B) {
6030 struct drm_i915_private *dev_priv = dev->dev_private;
6031
6032 I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
6033 I915_WRITE(CHV_CANVAS(pipe), 0);
6034 }
6035
5b18e57c
DV
6036 i9xx_set_pipeconf(intel_crtc);
6037
89b667f8 6038 intel_crtc->active = true;
89b667f8 6039
a72e4c9f 6040 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4a3436e8 6041
89b667f8
JB
6042 for_each_encoder_on_crtc(dev, crtc, encoder)
6043 if (encoder->pre_pll_enable)
6044 encoder->pre_pll_enable(encoder);
6045
cd2d34d9
VS
6046 if (IS_CHERRYVIEW(dev)) {
6047 chv_prepare_pll(intel_crtc, intel_crtc->config);
6048 chv_enable_pll(intel_crtc, intel_crtc->config);
6049 } else {
6050 vlv_prepare_pll(intel_crtc, intel_crtc->config);
6051 vlv_enable_pll(intel_crtc, intel_crtc->config);
9d556c99 6052 }
89b667f8
JB
6053
6054 for_each_encoder_on_crtc(dev, crtc, encoder)
6055 if (encoder->pre_enable)
6056 encoder->pre_enable(encoder);
6057
2dd24552
JB
6058 i9xx_pfit_enable(intel_crtc);
6059
b95c5321 6060 intel_color_load_luts(&pipe_config->base);
63cbb074 6061
caed361d 6062 intel_update_watermarks(crtc);
e1fdc473 6063 intel_enable_pipe(intel_crtc);
be6a6f8e 6064
4b3a9526
VS
6065 assert_vblank_disabled(crtc);
6066 drm_crtc_vblank_on(crtc);
6067
f9b61ff6
DV
6068 for_each_encoder_on_crtc(dev, crtc, encoder)
6069 encoder->enable(encoder);
89b667f8
JB
6070}
6071
f13c2ef3
DV
6072static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
6073{
6074 struct drm_device *dev = crtc->base.dev;
6075 struct drm_i915_private *dev_priv = dev->dev_private;
6076
6e3c9717
ACO
6077 I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
6078 I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
f13c2ef3
DV
6079}
6080
0b8765c6 6081static void i9xx_crtc_enable(struct drm_crtc *crtc)
79e53945
JB
6082{
6083 struct drm_device *dev = crtc->dev;
a72e4c9f 6084 struct drm_i915_private *dev_priv = to_i915(dev);
79e53945 6085 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 6086 struct intel_encoder *encoder;
b95c5321
ML
6087 struct intel_crtc_state *pipe_config =
6088 to_intel_crtc_state(crtc->state);
cd2d34d9 6089 enum pipe pipe = intel_crtc->pipe;
79e53945 6090
53d9f4e9 6091 if (WARN_ON(intel_crtc->active))
f7abfe8b
CW
6092 return;
6093
f13c2ef3
DV
6094 i9xx_set_pll_dividers(intel_crtc);
6095
6e3c9717 6096 if (intel_crtc->config->has_dp_encoder)
fe3cd48d 6097 intel_dp_set_m_n(intel_crtc, M1_N1);
5b18e57c
DV
6098
6099 intel_set_pipe_timings(intel_crtc);
bc58be60 6100 intel_set_pipe_src_size(intel_crtc);
5b18e57c 6101
5b18e57c
DV
6102 i9xx_set_pipeconf(intel_crtc);
6103
f7abfe8b 6104 intel_crtc->active = true;
6b383a7f 6105
4a3436e8 6106 if (!IS_GEN2(dev))
a72e4c9f 6107 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4a3436e8 6108
9d6d9f19
MK
6109 for_each_encoder_on_crtc(dev, crtc, encoder)
6110 if (encoder->pre_enable)
6111 encoder->pre_enable(encoder);
6112
f6736a1a
DV
6113 i9xx_enable_pll(intel_crtc);
6114
2dd24552
JB
6115 i9xx_pfit_enable(intel_crtc);
6116
b95c5321 6117 intel_color_load_luts(&pipe_config->base);
63cbb074 6118
f37fcc2a 6119 intel_update_watermarks(crtc);
e1fdc473 6120 intel_enable_pipe(intel_crtc);
be6a6f8e 6121
4b3a9526
VS
6122 assert_vblank_disabled(crtc);
6123 drm_crtc_vblank_on(crtc);
6124
f9b61ff6
DV
6125 for_each_encoder_on_crtc(dev, crtc, encoder)
6126 encoder->enable(encoder);
0b8765c6 6127}
79e53945 6128
87476d63
DV
6129static void i9xx_pfit_disable(struct intel_crtc *crtc)
6130{
6131 struct drm_device *dev = crtc->base.dev;
6132 struct drm_i915_private *dev_priv = dev->dev_private;
87476d63 6133
6e3c9717 6134 if (!crtc->config->gmch_pfit.control)
328d8e82 6135 return;
87476d63 6136
328d8e82 6137 assert_pipe_disabled(dev_priv, crtc->pipe);
87476d63 6138
328d8e82
DV
6139 DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
6140 I915_READ(PFIT_CONTROL));
6141 I915_WRITE(PFIT_CONTROL, 0);
87476d63
DV
6142}
6143
0b8765c6
JB
6144static void i9xx_crtc_disable(struct drm_crtc *crtc)
6145{
6146 struct drm_device *dev = crtc->dev;
6147 struct drm_i915_private *dev_priv = dev->dev_private;
6148 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 6149 struct intel_encoder *encoder;
0b8765c6 6150 int pipe = intel_crtc->pipe;
ef9c3aee 6151
6304cd91
VS
6152 /*
6153 * On gen2 planes are double buffered but the pipe isn't, so we must
6154 * wait for planes to fully turn off before disabling the pipe.
6155 */
90e83e53
ACO
6156 if (IS_GEN2(dev))
6157 intel_wait_for_vblank(dev, pipe);
6304cd91 6158
4b3a9526
VS
6159 for_each_encoder_on_crtc(dev, crtc, encoder)
6160 encoder->disable(encoder);
6161
f9b61ff6
DV
6162 drm_crtc_vblank_off(crtc);
6163 assert_vblank_disabled(crtc);
6164
575f7ab7 6165 intel_disable_pipe(intel_crtc);
24a1f16d 6166
87476d63 6167 i9xx_pfit_disable(intel_crtc);
24a1f16d 6168
89b667f8
JB
6169 for_each_encoder_on_crtc(dev, crtc, encoder)
6170 if (encoder->post_disable)
6171 encoder->post_disable(encoder);
6172
a65347ba 6173 if (!intel_crtc->config->has_dsi_encoder) {
076ed3b2
CML
6174 if (IS_CHERRYVIEW(dev))
6175 chv_disable_pll(dev_priv, pipe);
6176 else if (IS_VALLEYVIEW(dev))
6177 vlv_disable_pll(dev_priv, pipe);
6178 else
1c4e0274 6179 i9xx_disable_pll(intel_crtc);
076ed3b2 6180 }
0b8765c6 6181
d6db995f
VS
6182 for_each_encoder_on_crtc(dev, crtc, encoder)
6183 if (encoder->post_pll_disable)
6184 encoder->post_pll_disable(encoder);
6185
4a3436e8 6186 if (!IS_GEN2(dev))
a72e4c9f 6187 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
0b8765c6
JB
6188}
6189
b17d48e2
ML
6190static void intel_crtc_disable_noatomic(struct drm_crtc *crtc)
6191{
842e0307 6192 struct intel_encoder *encoder;
b17d48e2
ML
6193 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6194 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
6195 enum intel_display_power_domain domain;
6196 unsigned long domains;
6197
6198 if (!intel_crtc->active)
6199 return;
6200
a539205a 6201 if (to_intel_plane_state(crtc->primary->state)->visible) {
6885843a 6202 WARN_ON(list_empty(&intel_crtc->flip_work));
fc32b1fd 6203
2622a081 6204 intel_pre_disable_primary_noatomic(crtc);
54a41961
ML
6205
6206 intel_crtc_disable_planes(crtc, 1 << drm_plane_index(crtc->primary));
6207 to_intel_plane_state(crtc->primary->state)->visible = false;
a539205a
ML
6208 }
6209
b17d48e2 6210 dev_priv->display.crtc_disable(crtc);
842e0307
ML
6211
6212 DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was enabled, now disabled\n",
6213 crtc->base.id);
6214
6215 WARN_ON(drm_atomic_set_mode_for_crtc(crtc->state, NULL) < 0);
6216 crtc->state->active = false;
37d9078b 6217 intel_crtc->active = false;
842e0307
ML
6218 crtc->enabled = false;
6219 crtc->state->connector_mask = 0;
6220 crtc->state->encoder_mask = 0;
6221
6222 for_each_encoder_on_crtc(crtc->dev, crtc, encoder)
6223 encoder->base.crtc = NULL;
6224
58f9c0bc 6225 intel_fbc_disable(intel_crtc);
37d9078b 6226 intel_update_watermarks(crtc);
1f7457b1 6227 intel_disable_shared_dpll(intel_crtc);
b17d48e2
ML
6228
6229 domains = intel_crtc->enabled_power_domains;
6230 for_each_power_domain(domain, domains)
6231 intel_display_power_put(dev_priv, domain);
6232 intel_crtc->enabled_power_domains = 0;
565602d7
ML
6233
6234 dev_priv->active_crtcs &= ~(1 << intel_crtc->pipe);
6235 dev_priv->min_pixclk[intel_crtc->pipe] = 0;
b17d48e2
ML
6236}
6237
6b72d486
ML
6238/*
6239 * turn all crtc's off, but do not adjust state
6240 * This has to be paired with a call to intel_modeset_setup_hw_state.
6241 */
70e0bd74 6242int intel_display_suspend(struct drm_device *dev)
ee7b9f93 6243{
e2c8b870 6244 struct drm_i915_private *dev_priv = to_i915(dev);
70e0bd74 6245 struct drm_atomic_state *state;
e2c8b870 6246 int ret;
70e0bd74 6247
e2c8b870
ML
6248 state = drm_atomic_helper_suspend(dev);
6249 ret = PTR_ERR_OR_ZERO(state);
70e0bd74
ML
6250 if (ret)
6251 DRM_ERROR("Suspending crtc's failed with %i\n", ret);
e2c8b870
ML
6252 else
6253 dev_priv->modeset_restore_state = state;
a6747b73
ML
6254
6255 /*
6256 * Make sure all unpin_work completes before returning.
6257 */
6258 flush_workqueue(dev_priv->wq);
6259
70e0bd74 6260 return ret;
ee7b9f93
JB
6261}
6262
ea5b213a 6263void intel_encoder_destroy(struct drm_encoder *encoder)
7e7d76c3 6264{
4ef69c7a 6265 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
ea5b213a 6266
ea5b213a
CW
6267 drm_encoder_cleanup(encoder);
6268 kfree(intel_encoder);
7e7d76c3
JB
6269}
6270
0a91ca29
DV
6271/* Cross check the actual hw state with our own modeset state tracking (and it's
6272 * internal consistency). */
03f476e1
ML
6273static void intel_connector_verify_state(struct intel_connector *connector,
6274 struct drm_connector_state *conn_state)
79e53945 6275{
03f476e1 6276 struct drm_crtc *crtc = conn_state->crtc;
35dd3c64
ML
6277
6278 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
6279 connector->base.base.id,
6280 connector->base.name);
6281
0a91ca29 6282 if (connector->get_hw_state(connector)) {
e85376cb 6283 struct intel_encoder *encoder = connector->encoder;
0a91ca29 6284
35dd3c64
ML
6285 I915_STATE_WARN(!crtc,
6286 "connector enabled without attached crtc\n");
0a91ca29 6287
35dd3c64
ML
6288 if (!crtc)
6289 return;
6290
6291 I915_STATE_WARN(!crtc->state->active,
6292 "connector is active, but attached crtc isn't\n");
6293
e85376cb 6294 if (!encoder || encoder->type == INTEL_OUTPUT_DP_MST)
35dd3c64
ML
6295 return;
6296
e85376cb 6297 I915_STATE_WARN(conn_state->best_encoder != &encoder->base,
35dd3c64
ML
6298 "atomic encoder doesn't match attached encoder\n");
6299
e85376cb 6300 I915_STATE_WARN(conn_state->crtc != encoder->base.crtc,
35dd3c64
ML
6301 "attached encoder crtc differs from connector crtc\n");
6302 } else {
4d688a2a
ML
6303 I915_STATE_WARN(crtc && crtc->state->active,
6304 "attached crtc is active, but connector isn't\n");
03f476e1 6305 I915_STATE_WARN(!crtc && conn_state->best_encoder,
35dd3c64 6306 "best encoder set without crtc!\n");
0a91ca29 6307 }
79e53945
JB
6308}
6309
08d9bc92
ACO
6310int intel_connector_init(struct intel_connector *connector)
6311{
5350a031 6312 drm_atomic_helper_connector_reset(&connector->base);
08d9bc92 6313
5350a031 6314 if (!connector->base.state)
08d9bc92
ACO
6315 return -ENOMEM;
6316
08d9bc92
ACO
6317 return 0;
6318}
6319
6320struct intel_connector *intel_connector_alloc(void)
6321{
6322 struct intel_connector *connector;
6323
6324 connector = kzalloc(sizeof *connector, GFP_KERNEL);
6325 if (!connector)
6326 return NULL;
6327
6328 if (intel_connector_init(connector) < 0) {
6329 kfree(connector);
6330 return NULL;
6331 }
6332
6333 return connector;
6334}
6335
f0947c37
DV
6336/* Simple connector->get_hw_state implementation for encoders that support only
6337 * one connector and no cloning and hence the encoder state determines the state
6338 * of the connector. */
6339bool intel_connector_get_hw_state(struct intel_connector *connector)
ea5b213a 6340{
24929352 6341 enum pipe pipe = 0;
f0947c37 6342 struct intel_encoder *encoder = connector->encoder;
ea5b213a 6343
f0947c37 6344 return encoder->get_hw_state(encoder, &pipe);
ea5b213a
CW
6345}
6346
6d293983 6347static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
d272ddfa 6348{
6d293983
ACO
6349 if (crtc_state->base.enable && crtc_state->has_pch_encoder)
6350 return crtc_state->fdi_lanes;
d272ddfa
VS
6351
6352 return 0;
6353}
6354
6d293983 6355static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
5cec258b 6356 struct intel_crtc_state *pipe_config)
1857e1da 6357{
6d293983
ACO
6358 struct drm_atomic_state *state = pipe_config->base.state;
6359 struct intel_crtc *other_crtc;
6360 struct intel_crtc_state *other_crtc_state;
6361
1857e1da
DV
6362 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
6363 pipe_name(pipe), pipe_config->fdi_lanes);
6364 if (pipe_config->fdi_lanes > 4) {
6365 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
6366 pipe_name(pipe), pipe_config->fdi_lanes);
6d293983 6367 return -EINVAL;
1857e1da
DV
6368 }
6369
bafb6553 6370 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
1857e1da
DV
6371 if (pipe_config->fdi_lanes > 2) {
6372 DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
6373 pipe_config->fdi_lanes);
6d293983 6374 return -EINVAL;
1857e1da 6375 } else {
6d293983 6376 return 0;
1857e1da
DV
6377 }
6378 }
6379
6380 if (INTEL_INFO(dev)->num_pipes == 2)
6d293983 6381 return 0;
1857e1da
DV
6382
6383 /* Ivybridge 3 pipe is really complicated */
6384 switch (pipe) {
6385 case PIPE_A:
6d293983 6386 return 0;
1857e1da 6387 case PIPE_B:
6d293983
ACO
6388 if (pipe_config->fdi_lanes <= 2)
6389 return 0;
6390
6391 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_C));
6392 other_crtc_state =
6393 intel_atomic_get_crtc_state(state, other_crtc);
6394 if (IS_ERR(other_crtc_state))
6395 return PTR_ERR(other_crtc_state);
6396
6397 if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
1857e1da
DV
6398 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
6399 pipe_name(pipe), pipe_config->fdi_lanes);
6d293983 6400 return -EINVAL;
1857e1da 6401 }
6d293983 6402 return 0;
1857e1da 6403 case PIPE_C:
251cc67c
VS
6404 if (pipe_config->fdi_lanes > 2) {
6405 DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
6406 pipe_name(pipe), pipe_config->fdi_lanes);
6d293983 6407 return -EINVAL;
251cc67c 6408 }
6d293983
ACO
6409
6410 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_B));
6411 other_crtc_state =
6412 intel_atomic_get_crtc_state(state, other_crtc);
6413 if (IS_ERR(other_crtc_state))
6414 return PTR_ERR(other_crtc_state);
6415
6416 if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
1857e1da 6417 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
6d293983 6418 return -EINVAL;
1857e1da 6419 }
6d293983 6420 return 0;
1857e1da
DV
6421 default:
6422 BUG();
6423 }
6424}
6425
e29c22c0
DV
6426#define RETRY 1
6427static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
5cec258b 6428 struct intel_crtc_state *pipe_config)
877d48d5 6429{
1857e1da 6430 struct drm_device *dev = intel_crtc->base.dev;
7c5f93b0 6431 const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
6d293983
ACO
6432 int lane, link_bw, fdi_dotclock, ret;
6433 bool needs_recompute = false;
877d48d5 6434
e29c22c0 6435retry:
877d48d5
DV
6436 /* FDI is a binary signal running at ~2.7GHz, encoding
6437 * each output octet as 10 bits. The actual frequency
6438 * is stored as a divider into a 100MHz clock, and the
6439 * mode pixel clock is stored in units of 1KHz.
6440 * Hence the bw of each lane in terms of the mode signal
6441 * is:
6442 */
21a727b3 6443 link_bw = intel_fdi_link_freq(to_i915(dev), pipe_config);
877d48d5 6444
241bfc38 6445 fdi_dotclock = adjusted_mode->crtc_clock;
877d48d5 6446
2bd89a07 6447 lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
877d48d5
DV
6448 pipe_config->pipe_bpp);
6449
6450 pipe_config->fdi_lanes = lane;
6451
2bd89a07 6452 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
877d48d5 6453 link_bw, &pipe_config->fdi_m_n);
1857e1da 6454
e3b247da 6455 ret = ironlake_check_fdi_lanes(dev, intel_crtc->pipe, pipe_config);
6d293983 6456 if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
e29c22c0
DV
6457 pipe_config->pipe_bpp -= 2*3;
6458 DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
6459 pipe_config->pipe_bpp);
6460 needs_recompute = true;
6461 pipe_config->bw_constrained = true;
6462
6463 goto retry;
6464 }
6465
6466 if (needs_recompute)
6467 return RETRY;
6468
6d293983 6469 return ret;
877d48d5
DV
6470}
6471
8cfb3407
VS
6472static bool pipe_config_supports_ips(struct drm_i915_private *dev_priv,
6473 struct intel_crtc_state *pipe_config)
6474{
6475 if (pipe_config->pipe_bpp > 24)
6476 return false;
6477
6478 /* HSW can handle pixel rate up to cdclk? */
2d1fe073 6479 if (IS_HASWELL(dev_priv))
8cfb3407
VS
6480 return true;
6481
6482 /*
b432e5cf
VS
6483 * We compare against max which means we must take
6484 * the increased cdclk requirement into account when
6485 * calculating the new cdclk.
6486 *
6487 * Should measure whether using a lower cdclk w/o IPS
8cfb3407
VS
6488 */
6489 return ilk_pipe_pixel_rate(pipe_config) <=
6490 dev_priv->max_cdclk_freq * 95 / 100;
6491}
6492
42db64ef 6493static void hsw_compute_ips_config(struct intel_crtc *crtc,
5cec258b 6494 struct intel_crtc_state *pipe_config)
42db64ef 6495{
8cfb3407
VS
6496 struct drm_device *dev = crtc->base.dev;
6497 struct drm_i915_private *dev_priv = dev->dev_private;
6498
d330a953 6499 pipe_config->ips_enabled = i915.enable_ips &&
8cfb3407
VS
6500 hsw_crtc_supports_ips(crtc) &&
6501 pipe_config_supports_ips(dev_priv, pipe_config);
42db64ef
PZ
6502}
6503
39acb4aa
VS
6504static bool intel_crtc_supports_double_wide(const struct intel_crtc *crtc)
6505{
6506 const struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6507
6508 /* GDG double wide on either pipe, otherwise pipe A only */
6509 return INTEL_INFO(dev_priv)->gen < 4 &&
6510 (crtc->pipe == PIPE_A || IS_I915G(dev_priv));
6511}
6512
a43f6e0f 6513static int intel_crtc_compute_config(struct intel_crtc *crtc,
5cec258b 6514 struct intel_crtc_state *pipe_config)
79e53945 6515{
a43f6e0f 6516 struct drm_device *dev = crtc->base.dev;
8bd31e67 6517 struct drm_i915_private *dev_priv = dev->dev_private;
7c5f93b0 6518 const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
89749350 6519
ad3a4479 6520 /* FIXME should check pixel clock limits on all platforms */
cf532bb2 6521 if (INTEL_INFO(dev)->gen < 4) {
39acb4aa 6522 int clock_limit = dev_priv->max_cdclk_freq * 9 / 10;
cf532bb2
VS
6523
6524 /*
39acb4aa 6525 * Enable double wide mode when the dot clock
cf532bb2 6526 * is > 90% of the (display) core speed.
cf532bb2 6527 */
39acb4aa
VS
6528 if (intel_crtc_supports_double_wide(crtc) &&
6529 adjusted_mode->crtc_clock > clock_limit) {
ad3a4479 6530 clock_limit *= 2;
cf532bb2 6531 pipe_config->double_wide = true;
ad3a4479
VS
6532 }
6533
39acb4aa
VS
6534 if (adjusted_mode->crtc_clock > clock_limit) {
6535 DRM_DEBUG_KMS("requested pixel clock (%d kHz) too high (max: %d kHz, double wide: %s)\n",
6536 adjusted_mode->crtc_clock, clock_limit,
6537 yesno(pipe_config->double_wide));
e29c22c0 6538 return -EINVAL;
39acb4aa 6539 }
2c07245f 6540 }
89749350 6541
1d1d0e27
VS
6542 /*
6543 * Pipe horizontal size must be even in:
6544 * - DVO ganged mode
6545 * - LVDS dual channel mode
6546 * - Double wide pipe
6547 */
a93e255f 6548 if ((intel_pipe_will_have_type(pipe_config, INTEL_OUTPUT_LVDS) &&
1d1d0e27
VS
6549 intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
6550 pipe_config->pipe_src_w &= ~1;
6551
8693a824
DL
6552 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
6553 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
44f46b42
CW
6554 */
6555 if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
aad941d5 6556 adjusted_mode->crtc_hsync_start == adjusted_mode->crtc_hdisplay)
e29c22c0 6557 return -EINVAL;
44f46b42 6558
f5adf94e 6559 if (HAS_IPS(dev))
a43f6e0f
DV
6560 hsw_compute_ips_config(crtc, pipe_config);
6561
877d48d5 6562 if (pipe_config->has_pch_encoder)
a43f6e0f 6563 return ironlake_fdi_compute_config(crtc, pipe_config);
877d48d5 6564
cf5a15be 6565 return 0;
79e53945
JB
6566}
6567
1652d19e
VS
6568static int skylake_get_display_clock_speed(struct drm_device *dev)
6569{
6570 struct drm_i915_private *dev_priv = to_i915(dev);
ea61791e 6571 uint32_t cdctl;
1652d19e 6572
ea61791e 6573 skl_dpll0_update(dev_priv);
1652d19e 6574
ea61791e
VS
6575 if (dev_priv->skl_vco_freq == 0)
6576 return 24000; /* 24MHz is the cd freq with NSSC ref */
1652d19e 6577
ea61791e 6578 cdctl = I915_READ(CDCLK_CTL);
1652d19e 6579
ea61791e 6580 if (dev_priv->skl_vco_freq == 8640) {
1652d19e
VS
6581 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6582 case CDCLK_FREQ_450_432:
6583 return 432000;
6584 case CDCLK_FREQ_337_308:
487ed2e4 6585 return 308571;
ea61791e
VS
6586 case CDCLK_FREQ_540:
6587 return 540000;
1652d19e 6588 case CDCLK_FREQ_675_617:
487ed2e4 6589 return 617143;
1652d19e 6590 default:
ea61791e 6591 MISSING_CASE(cdctl & CDCLK_FREQ_SEL_MASK);
1652d19e
VS
6592 }
6593 } else {
1652d19e
VS
6594 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6595 case CDCLK_FREQ_450_432:
6596 return 450000;
6597 case CDCLK_FREQ_337_308:
6598 return 337500;
ea61791e
VS
6599 case CDCLK_FREQ_540:
6600 return 540000;
1652d19e
VS
6601 case CDCLK_FREQ_675_617:
6602 return 675000;
6603 default:
ea61791e 6604 MISSING_CASE(cdctl & CDCLK_FREQ_SEL_MASK);
1652d19e
VS
6605 }
6606 }
6607
6608 /* error case, do as if DPLL0 isn't enabled */
6609 return 24000;
6610}
6611
acd3f3d3
BP
6612static int broxton_get_display_clock_speed(struct drm_device *dev)
6613{
6614 struct drm_i915_private *dev_priv = to_i915(dev);
6615 uint32_t cdctl = I915_READ(CDCLK_CTL);
6616 uint32_t pll_ratio = I915_READ(BXT_DE_PLL_CTL) & BXT_DE_PLL_RATIO_MASK;
6617 uint32_t pll_enab = I915_READ(BXT_DE_PLL_ENABLE);
6618 int cdclk;
6619
6620 if (!(pll_enab & BXT_DE_PLL_PLL_ENABLE))
6621 return 19200;
6622
6623 cdclk = 19200 * pll_ratio / 2;
6624
6625 switch (cdctl & BXT_CDCLK_CD2X_DIV_SEL_MASK) {
6626 case BXT_CDCLK_CD2X_DIV_SEL_1:
6627 return cdclk; /* 576MHz or 624MHz */
6628 case BXT_CDCLK_CD2X_DIV_SEL_1_5:
6629 return cdclk * 2 / 3; /* 384MHz */
6630 case BXT_CDCLK_CD2X_DIV_SEL_2:
6631 return cdclk / 2; /* 288MHz */
6632 case BXT_CDCLK_CD2X_DIV_SEL_4:
6633 return cdclk / 4; /* 144MHz */
6634 }
6635
6636 /* error case, do as if DE PLL isn't enabled */
6637 return 19200;
6638}
6639
1652d19e
VS
6640static int broadwell_get_display_clock_speed(struct drm_device *dev)
6641{
6642 struct drm_i915_private *dev_priv = dev->dev_private;
6643 uint32_t lcpll = I915_READ(LCPLL_CTL);
6644 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6645
6646 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6647 return 800000;
6648 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6649 return 450000;
6650 else if (freq == LCPLL_CLK_FREQ_450)
6651 return 450000;
6652 else if (freq == LCPLL_CLK_FREQ_54O_BDW)
6653 return 540000;
6654 else if (freq == LCPLL_CLK_FREQ_337_5_BDW)
6655 return 337500;
6656 else
6657 return 675000;
6658}
6659
6660static int haswell_get_display_clock_speed(struct drm_device *dev)
6661{
6662 struct drm_i915_private *dev_priv = dev->dev_private;
6663 uint32_t lcpll = I915_READ(LCPLL_CTL);
6664 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6665
6666 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6667 return 800000;
6668 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6669 return 450000;
6670 else if (freq == LCPLL_CLK_FREQ_450)
6671 return 450000;
6672 else if (IS_HSW_ULT(dev))
6673 return 337500;
6674 else
6675 return 540000;
79e53945
JB
6676}
6677
25eb05fc
JB
6678static int valleyview_get_display_clock_speed(struct drm_device *dev)
6679{
bfa7df01
VS
6680 return vlv_get_cck_clock_hpll(to_i915(dev), "cdclk",
6681 CCK_DISPLAY_CLOCK_CONTROL);
25eb05fc
JB
6682}
6683
b37a6434
VS
6684static int ilk_get_display_clock_speed(struct drm_device *dev)
6685{
6686 return 450000;
6687}
6688
e70236a8
JB
6689static int i945_get_display_clock_speed(struct drm_device *dev)
6690{
6691 return 400000;
6692}
79e53945 6693
e70236a8 6694static int i915_get_display_clock_speed(struct drm_device *dev)
79e53945 6695{
e907f170 6696 return 333333;
e70236a8 6697}
79e53945 6698
e70236a8
JB
6699static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
6700{
6701 return 200000;
6702}
79e53945 6703
257a7ffc
DV
6704static int pnv_get_display_clock_speed(struct drm_device *dev)
6705{
6706 u16 gcfgc = 0;
6707
6708 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6709
6710 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6711 case GC_DISPLAY_CLOCK_267_MHZ_PNV:
e907f170 6712 return 266667;
257a7ffc 6713 case GC_DISPLAY_CLOCK_333_MHZ_PNV:
e907f170 6714 return 333333;
257a7ffc 6715 case GC_DISPLAY_CLOCK_444_MHZ_PNV:
e907f170 6716 return 444444;
257a7ffc
DV
6717 case GC_DISPLAY_CLOCK_200_MHZ_PNV:
6718 return 200000;
6719 default:
6720 DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
6721 case GC_DISPLAY_CLOCK_133_MHZ_PNV:
e907f170 6722 return 133333;
257a7ffc 6723 case GC_DISPLAY_CLOCK_167_MHZ_PNV:
e907f170 6724 return 166667;
257a7ffc
DV
6725 }
6726}
6727
e70236a8
JB
6728static int i915gm_get_display_clock_speed(struct drm_device *dev)
6729{
6730 u16 gcfgc = 0;
79e53945 6731
e70236a8
JB
6732 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6733
6734 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
e907f170 6735 return 133333;
e70236a8
JB
6736 else {
6737 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6738 case GC_DISPLAY_CLOCK_333_MHZ:
e907f170 6739 return 333333;
e70236a8
JB
6740 default:
6741 case GC_DISPLAY_CLOCK_190_200_MHZ:
6742 return 190000;
79e53945 6743 }
e70236a8
JB
6744 }
6745}
6746
6747static int i865_get_display_clock_speed(struct drm_device *dev)
6748{
e907f170 6749 return 266667;
e70236a8
JB
6750}
6751
1b1d2716 6752static int i85x_get_display_clock_speed(struct drm_device *dev)
e70236a8
JB
6753{
6754 u16 hpllcc = 0;
1b1d2716 6755
65cd2b3f
VS
6756 /*
6757 * 852GM/852GMV only supports 133 MHz and the HPLLCC
6758 * encoding is different :(
6759 * FIXME is this the right way to detect 852GM/852GMV?
6760 */
6761 if (dev->pdev->revision == 0x1)
6762 return 133333;
6763
1b1d2716
VS
6764 pci_bus_read_config_word(dev->pdev->bus,
6765 PCI_DEVFN(0, 3), HPLLCC, &hpllcc);
6766
e70236a8
JB
6767 /* Assume that the hardware is in the high speed state. This
6768 * should be the default.
6769 */
6770 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
6771 case GC_CLOCK_133_200:
1b1d2716 6772 case GC_CLOCK_133_200_2:
e70236a8
JB
6773 case GC_CLOCK_100_200:
6774 return 200000;
6775 case GC_CLOCK_166_250:
6776 return 250000;
6777 case GC_CLOCK_100_133:
e907f170 6778 return 133333;
1b1d2716
VS
6779 case GC_CLOCK_133_266:
6780 case GC_CLOCK_133_266_2:
6781 case GC_CLOCK_166_266:
6782 return 266667;
e70236a8 6783 }
79e53945 6784
e70236a8
JB
6785 /* Shouldn't happen */
6786 return 0;
6787}
79e53945 6788
e70236a8
JB
6789static int i830_get_display_clock_speed(struct drm_device *dev)
6790{
e907f170 6791 return 133333;
79e53945
JB
6792}
6793
34edce2f
VS
6794static unsigned int intel_hpll_vco(struct drm_device *dev)
6795{
6796 struct drm_i915_private *dev_priv = dev->dev_private;
6797 static const unsigned int blb_vco[8] = {
6798 [0] = 3200000,
6799 [1] = 4000000,
6800 [2] = 5333333,
6801 [3] = 4800000,
6802 [4] = 6400000,
6803 };
6804 static const unsigned int pnv_vco[8] = {
6805 [0] = 3200000,
6806 [1] = 4000000,
6807 [2] = 5333333,
6808 [3] = 4800000,
6809 [4] = 2666667,
6810 };
6811 static const unsigned int cl_vco[8] = {
6812 [0] = 3200000,
6813 [1] = 4000000,
6814 [2] = 5333333,
6815 [3] = 6400000,
6816 [4] = 3333333,
6817 [5] = 3566667,
6818 [6] = 4266667,
6819 };
6820 static const unsigned int elk_vco[8] = {
6821 [0] = 3200000,
6822 [1] = 4000000,
6823 [2] = 5333333,
6824 [3] = 4800000,
6825 };
6826 static const unsigned int ctg_vco[8] = {
6827 [0] = 3200000,
6828 [1] = 4000000,
6829 [2] = 5333333,
6830 [3] = 6400000,
6831 [4] = 2666667,
6832 [5] = 4266667,
6833 };
6834 const unsigned int *vco_table;
6835 unsigned int vco;
6836 uint8_t tmp = 0;
6837
6838 /* FIXME other chipsets? */
6839 if (IS_GM45(dev))
6840 vco_table = ctg_vco;
6841 else if (IS_G4X(dev))
6842 vco_table = elk_vco;
6843 else if (IS_CRESTLINE(dev))
6844 vco_table = cl_vco;
6845 else if (IS_PINEVIEW(dev))
6846 vco_table = pnv_vco;
6847 else if (IS_G33(dev))
6848 vco_table = blb_vco;
6849 else
6850 return 0;
6851
6852 tmp = I915_READ(IS_MOBILE(dev) ? HPLLVCO_MOBILE : HPLLVCO);
6853
6854 vco = vco_table[tmp & 0x7];
6855 if (vco == 0)
6856 DRM_ERROR("Bad HPLL VCO (HPLLVCO=0x%02x)\n", tmp);
6857 else
6858 DRM_DEBUG_KMS("HPLL VCO %u kHz\n", vco);
6859
6860 return vco;
6861}
6862
6863static int gm45_get_display_clock_speed(struct drm_device *dev)
6864{
6865 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
6866 uint16_t tmp = 0;
6867
6868 pci_read_config_word(dev->pdev, GCFGC, &tmp);
6869
6870 cdclk_sel = (tmp >> 12) & 0x1;
6871
6872 switch (vco) {
6873 case 2666667:
6874 case 4000000:
6875 case 5333333:
6876 return cdclk_sel ? 333333 : 222222;
6877 case 3200000:
6878 return cdclk_sel ? 320000 : 228571;
6879 default:
6880 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u, CFGC=0x%04x\n", vco, tmp);
6881 return 222222;
6882 }
6883}
6884
6885static int i965gm_get_display_clock_speed(struct drm_device *dev)
6886{
6887 static const uint8_t div_3200[] = { 16, 10, 8 };
6888 static const uint8_t div_4000[] = { 20, 12, 10 };
6889 static const uint8_t div_5333[] = { 24, 16, 14 };
6890 const uint8_t *div_table;
6891 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
6892 uint16_t tmp = 0;
6893
6894 pci_read_config_word(dev->pdev, GCFGC, &tmp);
6895
6896 cdclk_sel = ((tmp >> 8) & 0x1f) - 1;
6897
6898 if (cdclk_sel >= ARRAY_SIZE(div_3200))
6899 goto fail;
6900
6901 switch (vco) {
6902 case 3200000:
6903 div_table = div_3200;
6904 break;
6905 case 4000000:
6906 div_table = div_4000;
6907 break;
6908 case 5333333:
6909 div_table = div_5333;
6910 break;
6911 default:
6912 goto fail;
6913 }
6914
6915 return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
6916
caf4e252 6917fail:
34edce2f
VS
6918 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%04x\n", vco, tmp);
6919 return 200000;
6920}
6921
6922static int g33_get_display_clock_speed(struct drm_device *dev)
6923{
6924 static const uint8_t div_3200[] = { 12, 10, 8, 7, 5, 16 };
6925 static const uint8_t div_4000[] = { 14, 12, 10, 8, 6, 20 };
6926 static const uint8_t div_4800[] = { 20, 14, 12, 10, 8, 24 };
6927 static const uint8_t div_5333[] = { 20, 16, 12, 12, 8, 28 };
6928 const uint8_t *div_table;
6929 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
6930 uint16_t tmp = 0;
6931
6932 pci_read_config_word(dev->pdev, GCFGC, &tmp);
6933
6934 cdclk_sel = (tmp >> 4) & 0x7;
6935
6936 if (cdclk_sel >= ARRAY_SIZE(div_3200))
6937 goto fail;
6938
6939 switch (vco) {
6940 case 3200000:
6941 div_table = div_3200;
6942 break;
6943 case 4000000:
6944 div_table = div_4000;
6945 break;
6946 case 4800000:
6947 div_table = div_4800;
6948 break;
6949 case 5333333:
6950 div_table = div_5333;
6951 break;
6952 default:
6953 goto fail;
6954 }
6955
6956 return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
6957
caf4e252 6958fail:
34edce2f
VS
6959 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%08x\n", vco, tmp);
6960 return 190476;
6961}
6962
2c07245f 6963static void
a65851af 6964intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
2c07245f 6965{
a65851af
VS
6966 while (*num > DATA_LINK_M_N_MASK ||
6967 *den > DATA_LINK_M_N_MASK) {
2c07245f
ZW
6968 *num >>= 1;
6969 *den >>= 1;
6970 }
6971}
6972
a65851af
VS
6973static void compute_m_n(unsigned int m, unsigned int n,
6974 uint32_t *ret_m, uint32_t *ret_n)
6975{
6976 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
6977 *ret_m = div_u64((uint64_t) m * *ret_n, n);
6978 intel_reduce_m_n_ratio(ret_m, ret_n);
6979}
6980
e69d0bc1
DV
6981void
6982intel_link_compute_m_n(int bits_per_pixel, int nlanes,
6983 int pixel_clock, int link_clock,
6984 struct intel_link_m_n *m_n)
2c07245f 6985{
e69d0bc1 6986 m_n->tu = 64;
a65851af
VS
6987
6988 compute_m_n(bits_per_pixel * pixel_clock,
6989 link_clock * nlanes * 8,
6990 &m_n->gmch_m, &m_n->gmch_n);
6991
6992 compute_m_n(pixel_clock, link_clock,
6993 &m_n->link_m, &m_n->link_n);
2c07245f
ZW
6994}
6995
a7615030
CW
6996static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
6997{
d330a953
JN
6998 if (i915.panel_use_ssc >= 0)
6999 return i915.panel_use_ssc != 0;
41aa3448 7000 return dev_priv->vbt.lvds_use_ssc
435793df 7001 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
a7615030
CW
7002}
7003
7429e9d4 7004static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
c65d77d8 7005{
7df00d7a 7006 return (1 << dpll->n) << 16 | dpll->m2;
7429e9d4 7007}
f47709a9 7008
7429e9d4
DV
7009static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
7010{
7011 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
c65d77d8
JB
7012}
7013
f47709a9 7014static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
190f68c5 7015 struct intel_crtc_state *crtc_state,
9e2c8475 7016 struct dpll *reduced_clock)
a7516a05 7017{
f47709a9 7018 struct drm_device *dev = crtc->base.dev;
a7516a05
JB
7019 u32 fp, fp2 = 0;
7020
7021 if (IS_PINEVIEW(dev)) {
190f68c5 7022 fp = pnv_dpll_compute_fp(&crtc_state->dpll);
a7516a05 7023 if (reduced_clock)
7429e9d4 7024 fp2 = pnv_dpll_compute_fp(reduced_clock);
a7516a05 7025 } else {
190f68c5 7026 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
a7516a05 7027 if (reduced_clock)
7429e9d4 7028 fp2 = i9xx_dpll_compute_fp(reduced_clock);
a7516a05
JB
7029 }
7030
190f68c5 7031 crtc_state->dpll_hw_state.fp0 = fp;
a7516a05 7032
f47709a9 7033 crtc->lowfreq_avail = false;
a93e255f 7034 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
ab585dea 7035 reduced_clock) {
190f68c5 7036 crtc_state->dpll_hw_state.fp1 = fp2;
f47709a9 7037 crtc->lowfreq_avail = true;
a7516a05 7038 } else {
190f68c5 7039 crtc_state->dpll_hw_state.fp1 = fp;
a7516a05
JB
7040 }
7041}
7042
5e69f97f
CML
7043static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
7044 pipe)
89b667f8
JB
7045{
7046 u32 reg_val;
7047
7048 /*
7049 * PLLB opamp always calibrates to max value of 0x3f, force enable it
7050 * and set it to a reasonable value instead.
7051 */
ab3c759a 7052 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
89b667f8
JB
7053 reg_val &= 0xffffff00;
7054 reg_val |= 0x00000030;
ab3c759a 7055 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
89b667f8 7056
ab3c759a 7057 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
89b667f8
JB
7058 reg_val &= 0x8cffffff;
7059 reg_val = 0x8c000000;
ab3c759a 7060 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
89b667f8 7061
ab3c759a 7062 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
89b667f8 7063 reg_val &= 0xffffff00;
ab3c759a 7064 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
89b667f8 7065
ab3c759a 7066 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
89b667f8
JB
7067 reg_val &= 0x00ffffff;
7068 reg_val |= 0xb0000000;
ab3c759a 7069 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
89b667f8
JB
7070}
7071
b551842d
DV
7072static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
7073 struct intel_link_m_n *m_n)
7074{
7075 struct drm_device *dev = crtc->base.dev;
7076 struct drm_i915_private *dev_priv = dev->dev_private;
7077 int pipe = crtc->pipe;
7078
e3b95f1e
DV
7079 I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7080 I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
7081 I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
7082 I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
b551842d
DV
7083}
7084
7085static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
f769cd24
VK
7086 struct intel_link_m_n *m_n,
7087 struct intel_link_m_n *m2_n2)
b551842d
DV
7088{
7089 struct drm_device *dev = crtc->base.dev;
7090 struct drm_i915_private *dev_priv = dev->dev_private;
7091 int pipe = crtc->pipe;
6e3c9717 7092 enum transcoder transcoder = crtc->config->cpu_transcoder;
b551842d
DV
7093
7094 if (INTEL_INFO(dev)->gen >= 5) {
7095 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
7096 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
7097 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
7098 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
f769cd24
VK
7099 /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
7100 * for gen < 8) and if DRRS is supported (to make sure the
7101 * registers are not unnecessarily accessed).
7102 */
44395bfe 7103 if (m2_n2 && (IS_CHERRYVIEW(dev) || INTEL_INFO(dev)->gen < 8) &&
6e3c9717 7104 crtc->config->has_drrs) {
f769cd24
VK
7105 I915_WRITE(PIPE_DATA_M2(transcoder),
7106 TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
7107 I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
7108 I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
7109 I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
7110 }
b551842d 7111 } else {
e3b95f1e
DV
7112 I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7113 I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
7114 I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
7115 I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
b551842d
DV
7116 }
7117}
7118
fe3cd48d 7119void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
03afc4a2 7120{
fe3cd48d
R
7121 struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
7122
7123 if (m_n == M1_N1) {
7124 dp_m_n = &crtc->config->dp_m_n;
7125 dp_m2_n2 = &crtc->config->dp_m2_n2;
7126 } else if (m_n == M2_N2) {
7127
7128 /*
7129 * M2_N2 registers are not supported. Hence m2_n2 divider value
7130 * needs to be programmed into M1_N1.
7131 */
7132 dp_m_n = &crtc->config->dp_m2_n2;
7133 } else {
7134 DRM_ERROR("Unsupported divider value\n");
7135 return;
7136 }
7137
6e3c9717
ACO
7138 if (crtc->config->has_pch_encoder)
7139 intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
03afc4a2 7140 else
fe3cd48d 7141 intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
03afc4a2
DV
7142}
7143
251ac862
DV
7144static void vlv_compute_dpll(struct intel_crtc *crtc,
7145 struct intel_crtc_state *pipe_config)
bdd4b6a6 7146{
03ed5cbf 7147 pipe_config->dpll_hw_state.dpll = DPLL_INTEGRATED_REF_CLK_VLV |
cd2d34d9 7148 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
03ed5cbf
VS
7149 if (crtc->pipe != PIPE_A)
7150 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
bdd4b6a6 7151
cd2d34d9 7152 /* DPLL not used with DSI, but still need the rest set up */
187a1c07 7153 if (!pipe_config->has_dsi_encoder)
cd2d34d9
VS
7154 pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE |
7155 DPLL_EXT_BUFFER_ENABLE_VLV;
7156
03ed5cbf
VS
7157 pipe_config->dpll_hw_state.dpll_md =
7158 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
7159}
bdd4b6a6 7160
03ed5cbf
VS
7161static void chv_compute_dpll(struct intel_crtc *crtc,
7162 struct intel_crtc_state *pipe_config)
7163{
7164 pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLK_CHV |
cd2d34d9 7165 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
03ed5cbf
VS
7166 if (crtc->pipe != PIPE_A)
7167 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
7168
cd2d34d9 7169 /* DPLL not used with DSI, but still need the rest set up */
187a1c07 7170 if (!pipe_config->has_dsi_encoder)
cd2d34d9
VS
7171 pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE;
7172
03ed5cbf
VS
7173 pipe_config->dpll_hw_state.dpll_md =
7174 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
bdd4b6a6
DV
7175}
7176
d288f65f 7177static void vlv_prepare_pll(struct intel_crtc *crtc,
5cec258b 7178 const struct intel_crtc_state *pipe_config)
a0c4da24 7179{
f47709a9 7180 struct drm_device *dev = crtc->base.dev;
a0c4da24 7181 struct drm_i915_private *dev_priv = dev->dev_private;
cd2d34d9 7182 enum pipe pipe = crtc->pipe;
bdd4b6a6 7183 u32 mdiv;
a0c4da24 7184 u32 bestn, bestm1, bestm2, bestp1, bestp2;
bdd4b6a6 7185 u32 coreclk, reg_val;
a0c4da24 7186
cd2d34d9
VS
7187 /* Enable Refclk */
7188 I915_WRITE(DPLL(pipe),
7189 pipe_config->dpll_hw_state.dpll &
7190 ~(DPLL_VCO_ENABLE | DPLL_EXT_BUFFER_ENABLE_VLV));
7191
7192 /* No need to actually set up the DPLL with DSI */
7193 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
7194 return;
7195
a580516d 7196 mutex_lock(&dev_priv->sb_lock);
09153000 7197
d288f65f
VS
7198 bestn = pipe_config->dpll.n;
7199 bestm1 = pipe_config->dpll.m1;
7200 bestm2 = pipe_config->dpll.m2;
7201 bestp1 = pipe_config->dpll.p1;
7202 bestp2 = pipe_config->dpll.p2;
a0c4da24 7203
89b667f8
JB
7204 /* See eDP HDMI DPIO driver vbios notes doc */
7205
7206 /* PLL B needs special handling */
bdd4b6a6 7207 if (pipe == PIPE_B)
5e69f97f 7208 vlv_pllb_recal_opamp(dev_priv, pipe);
89b667f8
JB
7209
7210 /* Set up Tx target for periodic Rcomp update */
ab3c759a 7211 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
89b667f8
JB
7212
7213 /* Disable target IRef on PLL */
ab3c759a 7214 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
89b667f8 7215 reg_val &= 0x00ffffff;
ab3c759a 7216 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
89b667f8
JB
7217
7218 /* Disable fast lock */
ab3c759a 7219 vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
89b667f8
JB
7220
7221 /* Set idtafcrecal before PLL is enabled */
a0c4da24
JB
7222 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
7223 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
7224 mdiv |= ((bestn << DPIO_N_SHIFT));
a0c4da24 7225 mdiv |= (1 << DPIO_K_SHIFT);
7df5080b
JB
7226
7227 /*
7228 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
7229 * but we don't support that).
7230 * Note: don't use the DAC post divider as it seems unstable.
7231 */
7232 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
ab3c759a 7233 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
a0c4da24 7234
a0c4da24 7235 mdiv |= DPIO_ENABLE_CALIBRATION;
ab3c759a 7236 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
a0c4da24 7237
89b667f8 7238 /* Set HBR and RBR LPF coefficients */
d288f65f 7239 if (pipe_config->port_clock == 162000 ||
409ee761
ACO
7240 intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG) ||
7241 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
ab3c759a 7242 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
885b0120 7243 0x009f0003);
89b667f8 7244 else
ab3c759a 7245 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
89b667f8
JB
7246 0x00d0000f);
7247
681a8504 7248 if (pipe_config->has_dp_encoder) {
89b667f8 7249 /* Use SSC source */
bdd4b6a6 7250 if (pipe == PIPE_A)
ab3c759a 7251 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
7252 0x0df40000);
7253 else
ab3c759a 7254 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
7255 0x0df70000);
7256 } else { /* HDMI or VGA */
7257 /* Use bend source */
bdd4b6a6 7258 if (pipe == PIPE_A)
ab3c759a 7259 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
7260 0x0df70000);
7261 else
ab3c759a 7262 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
7263 0x0df40000);
7264 }
a0c4da24 7265
ab3c759a 7266 coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
89b667f8 7267 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
409ee761
ACO
7268 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
7269 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
89b667f8 7270 coreclk |= 0x01000000;
ab3c759a 7271 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
a0c4da24 7272
ab3c759a 7273 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
a580516d 7274 mutex_unlock(&dev_priv->sb_lock);
a0c4da24
JB
7275}
7276
d288f65f 7277static void chv_prepare_pll(struct intel_crtc *crtc,
5cec258b 7278 const struct intel_crtc_state *pipe_config)
9d556c99
CML
7279{
7280 struct drm_device *dev = crtc->base.dev;
7281 struct drm_i915_private *dev_priv = dev->dev_private;
cd2d34d9 7282 enum pipe pipe = crtc->pipe;
9d556c99 7283 enum dpio_channel port = vlv_pipe_to_channel(pipe);
9cbe40c1 7284 u32 loopfilter, tribuf_calcntr;
9d556c99 7285 u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
a945ce7e 7286 u32 dpio_val;
9cbe40c1 7287 int vco;
9d556c99 7288
cd2d34d9
VS
7289 /* Enable Refclk and SSC */
7290 I915_WRITE(DPLL(pipe),
7291 pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
7292
7293 /* No need to actually set up the DPLL with DSI */
7294 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
7295 return;
7296
d288f65f
VS
7297 bestn = pipe_config->dpll.n;
7298 bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
7299 bestm1 = pipe_config->dpll.m1;
7300 bestm2 = pipe_config->dpll.m2 >> 22;
7301 bestp1 = pipe_config->dpll.p1;
7302 bestp2 = pipe_config->dpll.p2;
9cbe40c1 7303 vco = pipe_config->dpll.vco;
a945ce7e 7304 dpio_val = 0;
9cbe40c1 7305 loopfilter = 0;
9d556c99 7306
a580516d 7307 mutex_lock(&dev_priv->sb_lock);
9d556c99 7308
9d556c99
CML
7309 /* p1 and p2 divider */
7310 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
7311 5 << DPIO_CHV_S1_DIV_SHIFT |
7312 bestp1 << DPIO_CHV_P1_DIV_SHIFT |
7313 bestp2 << DPIO_CHV_P2_DIV_SHIFT |
7314 1 << DPIO_CHV_K_DIV_SHIFT);
7315
7316 /* Feedback post-divider - m2 */
7317 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
7318
7319 /* Feedback refclk divider - n and m1 */
7320 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
7321 DPIO_CHV_M1_DIV_BY_2 |
7322 1 << DPIO_CHV_N_DIV_SHIFT);
7323
7324 /* M2 fraction division */
25a25dfc 7325 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
9d556c99
CML
7326
7327 /* M2 fraction division enable */
a945ce7e
VP
7328 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
7329 dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
7330 dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
7331 if (bestm2_frac)
7332 dpio_val |= DPIO_CHV_FRAC_DIV_EN;
7333 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
9d556c99 7334
de3a0fde
VP
7335 /* Program digital lock detect threshold */
7336 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
7337 dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
7338 DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
7339 dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
7340 if (!bestm2_frac)
7341 dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
7342 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
7343
9d556c99 7344 /* Loop filter */
9cbe40c1
VP
7345 if (vco == 5400000) {
7346 loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
7347 loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
7348 loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
7349 tribuf_calcntr = 0x9;
7350 } else if (vco <= 6200000) {
7351 loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
7352 loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
7353 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7354 tribuf_calcntr = 0x9;
7355 } else if (vco <= 6480000) {
7356 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7357 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7358 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7359 tribuf_calcntr = 0x8;
7360 } else {
7361 /* Not supported. Apply the same limits as in the max case */
7362 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7363 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7364 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7365 tribuf_calcntr = 0;
7366 }
9d556c99
CML
7367 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
7368
968040b2 7369 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
9cbe40c1
VP
7370 dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
7371 dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
7372 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
7373
9d556c99
CML
7374 /* AFC Recal */
7375 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
7376 vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
7377 DPIO_AFC_RECAL);
7378
a580516d 7379 mutex_unlock(&dev_priv->sb_lock);
9d556c99
CML
7380}
7381
d288f65f
VS
7382/**
7383 * vlv_force_pll_on - forcibly enable just the PLL
7384 * @dev_priv: i915 private structure
7385 * @pipe: pipe PLL to enable
7386 * @dpll: PLL configuration
7387 *
7388 * Enable the PLL for @pipe using the supplied @dpll config. To be used
7389 * in cases where we need the PLL enabled even when @pipe is not going to
7390 * be enabled.
7391 */
3f36b937
TU
7392int vlv_force_pll_on(struct drm_device *dev, enum pipe pipe,
7393 const struct dpll *dpll)
d288f65f
VS
7394{
7395 struct intel_crtc *crtc =
7396 to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
3f36b937
TU
7397 struct intel_crtc_state *pipe_config;
7398
7399 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
7400 if (!pipe_config)
7401 return -ENOMEM;
7402
7403 pipe_config->base.crtc = &crtc->base;
7404 pipe_config->pixel_multiplier = 1;
7405 pipe_config->dpll = *dpll;
d288f65f
VS
7406
7407 if (IS_CHERRYVIEW(dev)) {
3f36b937
TU
7408 chv_compute_dpll(crtc, pipe_config);
7409 chv_prepare_pll(crtc, pipe_config);
7410 chv_enable_pll(crtc, pipe_config);
d288f65f 7411 } else {
3f36b937
TU
7412 vlv_compute_dpll(crtc, pipe_config);
7413 vlv_prepare_pll(crtc, pipe_config);
7414 vlv_enable_pll(crtc, pipe_config);
d288f65f 7415 }
3f36b937
TU
7416
7417 kfree(pipe_config);
7418
7419 return 0;
d288f65f
VS
7420}
7421
7422/**
7423 * vlv_force_pll_off - forcibly disable just the PLL
7424 * @dev_priv: i915 private structure
7425 * @pipe: pipe PLL to disable
7426 *
7427 * Disable the PLL for @pipe. To be used in cases where we need
7428 * the PLL enabled even when @pipe is not going to be enabled.
7429 */
7430void vlv_force_pll_off(struct drm_device *dev, enum pipe pipe)
7431{
7432 if (IS_CHERRYVIEW(dev))
7433 chv_disable_pll(to_i915(dev), pipe);
7434 else
7435 vlv_disable_pll(to_i915(dev), pipe);
7436}
7437
251ac862
DV
7438static void i9xx_compute_dpll(struct intel_crtc *crtc,
7439 struct intel_crtc_state *crtc_state,
9e2c8475 7440 struct dpll *reduced_clock)
eb1cbe48 7441{
f47709a9 7442 struct drm_device *dev = crtc->base.dev;
eb1cbe48 7443 struct drm_i915_private *dev_priv = dev->dev_private;
eb1cbe48
DV
7444 u32 dpll;
7445 bool is_sdvo;
190f68c5 7446 struct dpll *clock = &crtc_state->dpll;
eb1cbe48 7447
190f68c5 7448 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
2a8f64ca 7449
a93e255f
ACO
7450 is_sdvo = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO) ||
7451 intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI);
eb1cbe48
DV
7452
7453 dpll = DPLL_VGA_MODE_DIS;
7454
a93e255f 7455 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
eb1cbe48
DV
7456 dpll |= DPLLB_MODE_LVDS;
7457 else
7458 dpll |= DPLLB_MODE_DAC_SERIAL;
6cc5f341 7459
ef1b460d 7460 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
190f68c5 7461 dpll |= (crtc_state->pixel_multiplier - 1)
198a037f 7462 << SDVO_MULTIPLIER_SHIFT_HIRES;
eb1cbe48 7463 }
198a037f
DV
7464
7465 if (is_sdvo)
4a33e48d 7466 dpll |= DPLL_SDVO_HIGH_SPEED;
198a037f 7467
190f68c5 7468 if (crtc_state->has_dp_encoder)
4a33e48d 7469 dpll |= DPLL_SDVO_HIGH_SPEED;
eb1cbe48
DV
7470
7471 /* compute bitmask from p1 value */
7472 if (IS_PINEVIEW(dev))
7473 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
7474 else {
7475 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7476 if (IS_G4X(dev) && reduced_clock)
7477 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
7478 }
7479 switch (clock->p2) {
7480 case 5:
7481 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
7482 break;
7483 case 7:
7484 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
7485 break;
7486 case 10:
7487 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
7488 break;
7489 case 14:
7490 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
7491 break;
7492 }
7493 if (INTEL_INFO(dev)->gen >= 4)
7494 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
7495
190f68c5 7496 if (crtc_state->sdvo_tv_clock)
eb1cbe48 7497 dpll |= PLL_REF_INPUT_TVCLKINBC;
a93e255f 7498 else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
ceb41007 7499 intel_panel_use_ssc(dev_priv))
eb1cbe48
DV
7500 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7501 else
7502 dpll |= PLL_REF_INPUT_DREFCLK;
7503
7504 dpll |= DPLL_VCO_ENABLE;
190f68c5 7505 crtc_state->dpll_hw_state.dpll = dpll;
8bcc2795 7506
eb1cbe48 7507 if (INTEL_INFO(dev)->gen >= 4) {
190f68c5 7508 u32 dpll_md = (crtc_state->pixel_multiplier - 1)
ef1b460d 7509 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
190f68c5 7510 crtc_state->dpll_hw_state.dpll_md = dpll_md;
eb1cbe48
DV
7511 }
7512}
7513
251ac862
DV
7514static void i8xx_compute_dpll(struct intel_crtc *crtc,
7515 struct intel_crtc_state *crtc_state,
9e2c8475 7516 struct dpll *reduced_clock)
eb1cbe48 7517{
f47709a9 7518 struct drm_device *dev = crtc->base.dev;
eb1cbe48 7519 struct drm_i915_private *dev_priv = dev->dev_private;
eb1cbe48 7520 u32 dpll;
190f68c5 7521 struct dpll *clock = &crtc_state->dpll;
eb1cbe48 7522
190f68c5 7523 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
2a8f64ca 7524
eb1cbe48
DV
7525 dpll = DPLL_VGA_MODE_DIS;
7526
a93e255f 7527 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
eb1cbe48
DV
7528 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7529 } else {
7530 if (clock->p1 == 2)
7531 dpll |= PLL_P1_DIVIDE_BY_TWO;
7532 else
7533 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7534 if (clock->p2 == 4)
7535 dpll |= PLL_P2_DIVIDE_BY_4;
7536 }
7537
a93e255f 7538 if (!IS_I830(dev) && intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
4a33e48d
DV
7539 dpll |= DPLL_DVO_2X_MODE;
7540
a93e255f 7541 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
ceb41007 7542 intel_panel_use_ssc(dev_priv))
eb1cbe48
DV
7543 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7544 else
7545 dpll |= PLL_REF_INPUT_DREFCLK;
7546
7547 dpll |= DPLL_VCO_ENABLE;
190f68c5 7548 crtc_state->dpll_hw_state.dpll = dpll;
eb1cbe48
DV
7549}
7550
8a654f3b 7551static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
b0e77b9c
PZ
7552{
7553 struct drm_device *dev = intel_crtc->base.dev;
7554 struct drm_i915_private *dev_priv = dev->dev_private;
7555 enum pipe pipe = intel_crtc->pipe;
6e3c9717 7556 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
7c5f93b0 7557 const struct drm_display_mode *adjusted_mode = &intel_crtc->config->base.adjusted_mode;
1caea6e9
VS
7558 uint32_t crtc_vtotal, crtc_vblank_end;
7559 int vsyncshift = 0;
4d8a62ea
DV
7560
7561 /* We need to be careful not to changed the adjusted mode, for otherwise
7562 * the hw state checker will get angry at the mismatch. */
7563 crtc_vtotal = adjusted_mode->crtc_vtotal;
7564 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
b0e77b9c 7565
609aeaca 7566 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
b0e77b9c 7567 /* the chip adds 2 halflines automatically */
4d8a62ea
DV
7568 crtc_vtotal -= 1;
7569 crtc_vblank_end -= 1;
609aeaca 7570
409ee761 7571 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
609aeaca
VS
7572 vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
7573 else
7574 vsyncshift = adjusted_mode->crtc_hsync_start -
7575 adjusted_mode->crtc_htotal / 2;
1caea6e9
VS
7576 if (vsyncshift < 0)
7577 vsyncshift += adjusted_mode->crtc_htotal;
b0e77b9c
PZ
7578 }
7579
7580 if (INTEL_INFO(dev)->gen > 3)
fe2b8f9d 7581 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
b0e77b9c 7582
fe2b8f9d 7583 I915_WRITE(HTOTAL(cpu_transcoder),
b0e77b9c
PZ
7584 (adjusted_mode->crtc_hdisplay - 1) |
7585 ((adjusted_mode->crtc_htotal - 1) << 16));
fe2b8f9d 7586 I915_WRITE(HBLANK(cpu_transcoder),
b0e77b9c
PZ
7587 (adjusted_mode->crtc_hblank_start - 1) |
7588 ((adjusted_mode->crtc_hblank_end - 1) << 16));
fe2b8f9d 7589 I915_WRITE(HSYNC(cpu_transcoder),
b0e77b9c
PZ
7590 (adjusted_mode->crtc_hsync_start - 1) |
7591 ((adjusted_mode->crtc_hsync_end - 1) << 16));
7592
fe2b8f9d 7593 I915_WRITE(VTOTAL(cpu_transcoder),
b0e77b9c 7594 (adjusted_mode->crtc_vdisplay - 1) |
4d8a62ea 7595 ((crtc_vtotal - 1) << 16));
fe2b8f9d 7596 I915_WRITE(VBLANK(cpu_transcoder),
b0e77b9c 7597 (adjusted_mode->crtc_vblank_start - 1) |
4d8a62ea 7598 ((crtc_vblank_end - 1) << 16));
fe2b8f9d 7599 I915_WRITE(VSYNC(cpu_transcoder),
b0e77b9c
PZ
7600 (adjusted_mode->crtc_vsync_start - 1) |
7601 ((adjusted_mode->crtc_vsync_end - 1) << 16));
7602
b5e508d4
PZ
7603 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
7604 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
7605 * documented on the DDI_FUNC_CTL register description, EDP Input Select
7606 * bits. */
7607 if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
7608 (pipe == PIPE_B || pipe == PIPE_C))
7609 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
7610
bc58be60
JN
7611}
7612
7613static void intel_set_pipe_src_size(struct intel_crtc *intel_crtc)
7614{
7615 struct drm_device *dev = intel_crtc->base.dev;
7616 struct drm_i915_private *dev_priv = dev->dev_private;
7617 enum pipe pipe = intel_crtc->pipe;
7618
b0e77b9c
PZ
7619 /* pipesrc controls the size that is scaled from, which should
7620 * always be the user's requested size.
7621 */
7622 I915_WRITE(PIPESRC(pipe),
6e3c9717
ACO
7623 ((intel_crtc->config->pipe_src_w - 1) << 16) |
7624 (intel_crtc->config->pipe_src_h - 1));
b0e77b9c
PZ
7625}
7626
1bd1bd80 7627static void intel_get_pipe_timings(struct intel_crtc *crtc,
5cec258b 7628 struct intel_crtc_state *pipe_config)
1bd1bd80
DV
7629{
7630 struct drm_device *dev = crtc->base.dev;
7631 struct drm_i915_private *dev_priv = dev->dev_private;
7632 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
7633 uint32_t tmp;
7634
7635 tmp = I915_READ(HTOTAL(cpu_transcoder));
2d112de7
ACO
7636 pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
7637 pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 7638 tmp = I915_READ(HBLANK(cpu_transcoder));
2d112de7
ACO
7639 pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
7640 pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 7641 tmp = I915_READ(HSYNC(cpu_transcoder));
2d112de7
ACO
7642 pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
7643 pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80
DV
7644
7645 tmp = I915_READ(VTOTAL(cpu_transcoder));
2d112de7
ACO
7646 pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
7647 pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 7648 tmp = I915_READ(VBLANK(cpu_transcoder));
2d112de7
ACO
7649 pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
7650 pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 7651 tmp = I915_READ(VSYNC(cpu_transcoder));
2d112de7
ACO
7652 pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
7653 pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80
DV
7654
7655 if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
2d112de7
ACO
7656 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
7657 pipe_config->base.adjusted_mode.crtc_vtotal += 1;
7658 pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
1bd1bd80 7659 }
bc58be60
JN
7660}
7661
7662static void intel_get_pipe_src_size(struct intel_crtc *crtc,
7663 struct intel_crtc_state *pipe_config)
7664{
7665 struct drm_device *dev = crtc->base.dev;
7666 struct drm_i915_private *dev_priv = dev->dev_private;
7667 u32 tmp;
1bd1bd80
DV
7668
7669 tmp = I915_READ(PIPESRC(crtc->pipe));
37327abd
VS
7670 pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
7671 pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
7672
2d112de7
ACO
7673 pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
7674 pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
1bd1bd80
DV
7675}
7676
f6a83288 7677void intel_mode_from_pipe_config(struct drm_display_mode *mode,
5cec258b 7678 struct intel_crtc_state *pipe_config)
babea61d 7679{
2d112de7
ACO
7680 mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
7681 mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
7682 mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
7683 mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
babea61d 7684
2d112de7
ACO
7685 mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
7686 mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
7687 mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
7688 mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
babea61d 7689
2d112de7 7690 mode->flags = pipe_config->base.adjusted_mode.flags;
cd13f5ab 7691 mode->type = DRM_MODE_TYPE_DRIVER;
babea61d 7692
2d112de7
ACO
7693 mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
7694 mode->flags |= pipe_config->base.adjusted_mode.flags;
cd13f5ab
ML
7695
7696 mode->hsync = drm_mode_hsync(mode);
7697 mode->vrefresh = drm_mode_vrefresh(mode);
7698 drm_mode_set_name(mode);
babea61d
JB
7699}
7700
84b046f3
DV
7701static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
7702{
7703 struct drm_device *dev = intel_crtc->base.dev;
7704 struct drm_i915_private *dev_priv = dev->dev_private;
7705 uint32_t pipeconf;
7706
9f11a9e4 7707 pipeconf = 0;
84b046f3 7708
b6b5d049
VS
7709 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
7710 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
7711 pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
67c72a12 7712
6e3c9717 7713 if (intel_crtc->config->double_wide)
cf532bb2 7714 pipeconf |= PIPECONF_DOUBLE_WIDE;
84b046f3 7715
ff9ce46e 7716 /* only g4x and later have fancy bpc/dither controls */
666a4537 7717 if (IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
ff9ce46e 7718 /* Bspec claims that we can't use dithering for 30bpp pipes. */
6e3c9717 7719 if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
ff9ce46e 7720 pipeconf |= PIPECONF_DITHER_EN |
84b046f3 7721 PIPECONF_DITHER_TYPE_SP;
84b046f3 7722
6e3c9717 7723 switch (intel_crtc->config->pipe_bpp) {
ff9ce46e
DV
7724 case 18:
7725 pipeconf |= PIPECONF_6BPC;
7726 break;
7727 case 24:
7728 pipeconf |= PIPECONF_8BPC;
7729 break;
7730 case 30:
7731 pipeconf |= PIPECONF_10BPC;
7732 break;
7733 default:
7734 /* Case prevented by intel_choose_pipe_bpp_dither. */
7735 BUG();
84b046f3
DV
7736 }
7737 }
7738
7739 if (HAS_PIPE_CXSR(dev)) {
7740 if (intel_crtc->lowfreq_avail) {
7741 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
7742 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
7743 } else {
7744 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
84b046f3
DV
7745 }
7746 }
7747
6e3c9717 7748 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
efc2cfff 7749 if (INTEL_INFO(dev)->gen < 4 ||
409ee761 7750 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
efc2cfff
VS
7751 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
7752 else
7753 pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
7754 } else
84b046f3
DV
7755 pipeconf |= PIPECONF_PROGRESSIVE;
7756
666a4537
WB
7757 if ((IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) &&
7758 intel_crtc->config->limited_color_range)
9f11a9e4 7759 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
9c8e09b7 7760
84b046f3
DV
7761 I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
7762 POSTING_READ(PIPECONF(intel_crtc->pipe));
7763}
7764
81c97f52
ACO
7765static int i8xx_crtc_compute_clock(struct intel_crtc *crtc,
7766 struct intel_crtc_state *crtc_state)
7767{
7768 struct drm_device *dev = crtc->base.dev;
7769 struct drm_i915_private *dev_priv = dev->dev_private;
1b6f4958 7770 const struct intel_limit *limit;
81c97f52
ACO
7771 int refclk = 48000;
7772
7773 memset(&crtc_state->dpll_hw_state, 0,
7774 sizeof(crtc_state->dpll_hw_state));
7775
7776 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
7777 if (intel_panel_use_ssc(dev_priv)) {
7778 refclk = dev_priv->vbt.lvds_ssc_freq;
7779 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
7780 }
7781
7782 limit = &intel_limits_i8xx_lvds;
7783 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO)) {
7784 limit = &intel_limits_i8xx_dvo;
7785 } else {
7786 limit = &intel_limits_i8xx_dac;
7787 }
7788
7789 if (!crtc_state->clock_set &&
7790 !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7791 refclk, NULL, &crtc_state->dpll)) {
7792 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7793 return -EINVAL;
7794 }
7795
7796 i8xx_compute_dpll(crtc, crtc_state, NULL);
7797
7798 return 0;
7799}
7800
19ec6693
ACO
7801static int g4x_crtc_compute_clock(struct intel_crtc *crtc,
7802 struct intel_crtc_state *crtc_state)
7803{
7804 struct drm_device *dev = crtc->base.dev;
7805 struct drm_i915_private *dev_priv = dev->dev_private;
1b6f4958 7806 const struct intel_limit *limit;
19ec6693
ACO
7807 int refclk = 96000;
7808
7809 memset(&crtc_state->dpll_hw_state, 0,
7810 sizeof(crtc_state->dpll_hw_state));
7811
7812 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
7813 if (intel_panel_use_ssc(dev_priv)) {
7814 refclk = dev_priv->vbt.lvds_ssc_freq;
7815 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
7816 }
7817
7818 if (intel_is_dual_link_lvds(dev))
7819 limit = &intel_limits_g4x_dual_channel_lvds;
7820 else
7821 limit = &intel_limits_g4x_single_channel_lvds;
7822 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI) ||
7823 intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
7824 limit = &intel_limits_g4x_hdmi;
7825 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO)) {
7826 limit = &intel_limits_g4x_sdvo;
7827 } else {
7828 /* The option is for other outputs */
7829 limit = &intel_limits_i9xx_sdvo;
7830 }
7831
7832 if (!crtc_state->clock_set &&
7833 !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7834 refclk, NULL, &crtc_state->dpll)) {
7835 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7836 return -EINVAL;
7837 }
7838
7839 i9xx_compute_dpll(crtc, crtc_state, NULL);
7840
7841 return 0;
7842}
7843
70e8aa21
ACO
7844static int pnv_crtc_compute_clock(struct intel_crtc *crtc,
7845 struct intel_crtc_state *crtc_state)
7846{
7847 struct drm_device *dev = crtc->base.dev;
7848 struct drm_i915_private *dev_priv = dev->dev_private;
1b6f4958 7849 const struct intel_limit *limit;
70e8aa21
ACO
7850 int refclk = 96000;
7851
7852 memset(&crtc_state->dpll_hw_state, 0,
7853 sizeof(crtc_state->dpll_hw_state));
7854
7855 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
7856 if (intel_panel_use_ssc(dev_priv)) {
7857 refclk = dev_priv->vbt.lvds_ssc_freq;
7858 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
7859 }
7860
7861 limit = &intel_limits_pineview_lvds;
7862 } else {
7863 limit = &intel_limits_pineview_sdvo;
7864 }
7865
7866 if (!crtc_state->clock_set &&
7867 !pnv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7868 refclk, NULL, &crtc_state->dpll)) {
7869 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7870 return -EINVAL;
7871 }
7872
7873 i9xx_compute_dpll(crtc, crtc_state, NULL);
7874
7875 return 0;
7876}
7877
190f68c5
ACO
7878static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
7879 struct intel_crtc_state *crtc_state)
79e53945 7880{
c7653199 7881 struct drm_device *dev = crtc->base.dev;
79e53945 7882 struct drm_i915_private *dev_priv = dev->dev_private;
1b6f4958 7883 const struct intel_limit *limit;
81c97f52 7884 int refclk = 96000;
79e53945 7885
dd3cd74a
ACO
7886 memset(&crtc_state->dpll_hw_state, 0,
7887 sizeof(crtc_state->dpll_hw_state));
7888
70e8aa21
ACO
7889 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
7890 if (intel_panel_use_ssc(dev_priv)) {
7891 refclk = dev_priv->vbt.lvds_ssc_freq;
7892 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
7893 }
43565a06 7894
70e8aa21
ACO
7895 limit = &intel_limits_i9xx_lvds;
7896 } else {
7897 limit = &intel_limits_i9xx_sdvo;
81c97f52 7898 }
79e53945 7899
70e8aa21
ACO
7900 if (!crtc_state->clock_set &&
7901 !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7902 refclk, NULL, &crtc_state->dpll)) {
7903 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7904 return -EINVAL;
f47709a9 7905 }
7026d4ac 7906
81c97f52 7907 i9xx_compute_dpll(crtc, crtc_state, NULL);
79e53945 7908
c8f7a0db 7909 return 0;
f564048e
EA
7910}
7911
65b3d6a9
ACO
7912static int chv_crtc_compute_clock(struct intel_crtc *crtc,
7913 struct intel_crtc_state *crtc_state)
7914{
7915 int refclk = 100000;
1b6f4958 7916 const struct intel_limit *limit = &intel_limits_chv;
65b3d6a9
ACO
7917
7918 memset(&crtc_state->dpll_hw_state, 0,
7919 sizeof(crtc_state->dpll_hw_state));
7920
65b3d6a9
ACO
7921 if (!crtc_state->clock_set &&
7922 !chv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7923 refclk, NULL, &crtc_state->dpll)) {
7924 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7925 return -EINVAL;
7926 }
7927
7928 chv_compute_dpll(crtc, crtc_state);
7929
7930 return 0;
7931}
7932
7933static int vlv_crtc_compute_clock(struct intel_crtc *crtc,
7934 struct intel_crtc_state *crtc_state)
7935{
7936 int refclk = 100000;
1b6f4958 7937 const struct intel_limit *limit = &intel_limits_vlv;
65b3d6a9
ACO
7938
7939 memset(&crtc_state->dpll_hw_state, 0,
7940 sizeof(crtc_state->dpll_hw_state));
7941
65b3d6a9
ACO
7942 if (!crtc_state->clock_set &&
7943 !vlv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7944 refclk, NULL, &crtc_state->dpll)) {
7945 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7946 return -EINVAL;
7947 }
7948
7949 vlv_compute_dpll(crtc, crtc_state);
7950
7951 return 0;
7952}
7953
2fa2fe9a 7954static void i9xx_get_pfit_config(struct intel_crtc *crtc,
5cec258b 7955 struct intel_crtc_state *pipe_config)
2fa2fe9a
DV
7956{
7957 struct drm_device *dev = crtc->base.dev;
7958 struct drm_i915_private *dev_priv = dev->dev_private;
7959 uint32_t tmp;
7960
dc9e7dec
VS
7961 if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
7962 return;
7963
2fa2fe9a 7964 tmp = I915_READ(PFIT_CONTROL);
06922821
DV
7965 if (!(tmp & PFIT_ENABLE))
7966 return;
2fa2fe9a 7967
06922821 7968 /* Check whether the pfit is attached to our pipe. */
2fa2fe9a
DV
7969 if (INTEL_INFO(dev)->gen < 4) {
7970 if (crtc->pipe != PIPE_B)
7971 return;
2fa2fe9a
DV
7972 } else {
7973 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
7974 return;
7975 }
7976
06922821 7977 pipe_config->gmch_pfit.control = tmp;
2fa2fe9a 7978 pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
2fa2fe9a
DV
7979}
7980
acbec814 7981static void vlv_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 7982 struct intel_crtc_state *pipe_config)
acbec814
JB
7983{
7984 struct drm_device *dev = crtc->base.dev;
7985 struct drm_i915_private *dev_priv = dev->dev_private;
7986 int pipe = pipe_config->cpu_transcoder;
9e2c8475 7987 struct dpll clock;
acbec814 7988 u32 mdiv;
662c6ecb 7989 int refclk = 100000;
acbec814 7990
b521973b
VS
7991 /* In case of DSI, DPLL will not be used */
7992 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
f573de5a
SK
7993 return;
7994
a580516d 7995 mutex_lock(&dev_priv->sb_lock);
ab3c759a 7996 mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
a580516d 7997 mutex_unlock(&dev_priv->sb_lock);
acbec814
JB
7998
7999 clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
8000 clock.m2 = mdiv & DPIO_M2DIV_MASK;
8001 clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
8002 clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
8003 clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
8004
dccbea3b 8005 pipe_config->port_clock = vlv_calc_dpll_params(refclk, &clock);
acbec814
JB
8006}
8007
5724dbd1
DL
8008static void
8009i9xx_get_initial_plane_config(struct intel_crtc *crtc,
8010 struct intel_initial_plane_config *plane_config)
1ad292b5
JB
8011{
8012 struct drm_device *dev = crtc->base.dev;
8013 struct drm_i915_private *dev_priv = dev->dev_private;
8014 u32 val, base, offset;
8015 int pipe = crtc->pipe, plane = crtc->plane;
8016 int fourcc, pixel_format;
6761dd31 8017 unsigned int aligned_height;
b113d5ee 8018 struct drm_framebuffer *fb;
1b842c89 8019 struct intel_framebuffer *intel_fb;
1ad292b5 8020
42a7b088
DL
8021 val = I915_READ(DSPCNTR(plane));
8022 if (!(val & DISPLAY_PLANE_ENABLE))
8023 return;
8024
d9806c9f 8025 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
1b842c89 8026 if (!intel_fb) {
1ad292b5
JB
8027 DRM_DEBUG_KMS("failed to alloc fb\n");
8028 return;
8029 }
8030
1b842c89
DL
8031 fb = &intel_fb->base;
8032
18c5247e
DV
8033 if (INTEL_INFO(dev)->gen >= 4) {
8034 if (val & DISPPLANE_TILED) {
49af449b 8035 plane_config->tiling = I915_TILING_X;
18c5247e
DV
8036 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
8037 }
8038 }
1ad292b5
JB
8039
8040 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
b35d63fa 8041 fourcc = i9xx_format_to_fourcc(pixel_format);
b113d5ee
DL
8042 fb->pixel_format = fourcc;
8043 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
1ad292b5
JB
8044
8045 if (INTEL_INFO(dev)->gen >= 4) {
49af449b 8046 if (plane_config->tiling)
1ad292b5
JB
8047 offset = I915_READ(DSPTILEOFF(plane));
8048 else
8049 offset = I915_READ(DSPLINOFF(plane));
8050 base = I915_READ(DSPSURF(plane)) & 0xfffff000;
8051 } else {
8052 base = I915_READ(DSPADDR(plane));
8053 }
8054 plane_config->base = base;
8055
8056 val = I915_READ(PIPESRC(pipe));
b113d5ee
DL
8057 fb->width = ((val >> 16) & 0xfff) + 1;
8058 fb->height = ((val >> 0) & 0xfff) + 1;
1ad292b5
JB
8059
8060 val = I915_READ(DSPSTRIDE(pipe));
b113d5ee 8061 fb->pitches[0] = val & 0xffffffc0;
1ad292b5 8062
b113d5ee 8063 aligned_height = intel_fb_align_height(dev, fb->height,
091df6cb
DV
8064 fb->pixel_format,
8065 fb->modifier[0]);
1ad292b5 8066
f37b5c2b 8067 plane_config->size = fb->pitches[0] * aligned_height;
1ad292b5 8068
2844a921
DL
8069 DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
8070 pipe_name(pipe), plane, fb->width, fb->height,
8071 fb->bits_per_pixel, base, fb->pitches[0],
8072 plane_config->size);
1ad292b5 8073
2d14030b 8074 plane_config->fb = intel_fb;
1ad292b5
JB
8075}
8076
70b23a98 8077static void chv_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 8078 struct intel_crtc_state *pipe_config)
70b23a98
VS
8079{
8080 struct drm_device *dev = crtc->base.dev;
8081 struct drm_i915_private *dev_priv = dev->dev_private;
8082 int pipe = pipe_config->cpu_transcoder;
8083 enum dpio_channel port = vlv_pipe_to_channel(pipe);
9e2c8475 8084 struct dpll clock;
0d7b6b11 8085 u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2, pll_dw3;
70b23a98
VS
8086 int refclk = 100000;
8087
b521973b
VS
8088 /* In case of DSI, DPLL will not be used */
8089 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
8090 return;
8091
a580516d 8092 mutex_lock(&dev_priv->sb_lock);
70b23a98
VS
8093 cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
8094 pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
8095 pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
8096 pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
0d7b6b11 8097 pll_dw3 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
a580516d 8098 mutex_unlock(&dev_priv->sb_lock);
70b23a98
VS
8099
8100 clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
0d7b6b11
ID
8101 clock.m2 = (pll_dw0 & 0xff) << 22;
8102 if (pll_dw3 & DPIO_CHV_FRAC_DIV_EN)
8103 clock.m2 |= pll_dw2 & 0x3fffff;
70b23a98
VS
8104 clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
8105 clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
8106 clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
8107
dccbea3b 8108 pipe_config->port_clock = chv_calc_dpll_params(refclk, &clock);
70b23a98
VS
8109}
8110
0e8ffe1b 8111static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
5cec258b 8112 struct intel_crtc_state *pipe_config)
0e8ffe1b
DV
8113{
8114 struct drm_device *dev = crtc->base.dev;
8115 struct drm_i915_private *dev_priv = dev->dev_private;
1729050e 8116 enum intel_display_power_domain power_domain;
0e8ffe1b 8117 uint32_t tmp;
1729050e 8118 bool ret;
0e8ffe1b 8119
1729050e
ID
8120 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
8121 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
b5482bd0
ID
8122 return false;
8123
e143a21c 8124 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
8106ddbd 8125 pipe_config->shared_dpll = NULL;
eccb140b 8126
1729050e
ID
8127 ret = false;
8128
0e8ffe1b
DV
8129 tmp = I915_READ(PIPECONF(crtc->pipe));
8130 if (!(tmp & PIPECONF_ENABLE))
1729050e 8131 goto out;
0e8ffe1b 8132
666a4537 8133 if (IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
42571aef
VS
8134 switch (tmp & PIPECONF_BPC_MASK) {
8135 case PIPECONF_6BPC:
8136 pipe_config->pipe_bpp = 18;
8137 break;
8138 case PIPECONF_8BPC:
8139 pipe_config->pipe_bpp = 24;
8140 break;
8141 case PIPECONF_10BPC:
8142 pipe_config->pipe_bpp = 30;
8143 break;
8144 default:
8145 break;
8146 }
8147 }
8148
666a4537
WB
8149 if ((IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) &&
8150 (tmp & PIPECONF_COLOR_RANGE_SELECT))
b5a9fa09
DV
8151 pipe_config->limited_color_range = true;
8152
282740f7
VS
8153 if (INTEL_INFO(dev)->gen < 4)
8154 pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
8155
1bd1bd80 8156 intel_get_pipe_timings(crtc, pipe_config);
bc58be60 8157 intel_get_pipe_src_size(crtc, pipe_config);
1bd1bd80 8158
2fa2fe9a
DV
8159 i9xx_get_pfit_config(crtc, pipe_config);
8160
6c49f241 8161 if (INTEL_INFO(dev)->gen >= 4) {
c231775c
VS
8162 /* No way to read it out on pipes B and C */
8163 if (IS_CHERRYVIEW(dev) && crtc->pipe != PIPE_A)
8164 tmp = dev_priv->chv_dpll_md[crtc->pipe];
8165 else
8166 tmp = I915_READ(DPLL_MD(crtc->pipe));
6c49f241
DV
8167 pipe_config->pixel_multiplier =
8168 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
8169 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
8bcc2795 8170 pipe_config->dpll_hw_state.dpll_md = tmp;
6c49f241
DV
8171 } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
8172 tmp = I915_READ(DPLL(crtc->pipe));
8173 pipe_config->pixel_multiplier =
8174 ((tmp & SDVO_MULTIPLIER_MASK)
8175 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
8176 } else {
8177 /* Note that on i915G/GM the pixel multiplier is in the sdvo
8178 * port and will be fixed up in the encoder->get_config
8179 * function. */
8180 pipe_config->pixel_multiplier = 1;
8181 }
8bcc2795 8182 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
666a4537 8183 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
1c4e0274
VS
8184 /*
8185 * DPLL_DVO_2X_MODE must be enabled for both DPLLs
8186 * on 830. Filter it out here so that we don't
8187 * report errors due to that.
8188 */
8189 if (IS_I830(dev))
8190 pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
8191
8bcc2795
DV
8192 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
8193 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
165e901c
VS
8194 } else {
8195 /* Mask out read-only status bits. */
8196 pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
8197 DPLL_PORTC_READY_MASK |
8198 DPLL_PORTB_READY_MASK);
8bcc2795 8199 }
6c49f241 8200
70b23a98
VS
8201 if (IS_CHERRYVIEW(dev))
8202 chv_crtc_clock_get(crtc, pipe_config);
8203 else if (IS_VALLEYVIEW(dev))
acbec814
JB
8204 vlv_crtc_clock_get(crtc, pipe_config);
8205 else
8206 i9xx_crtc_clock_get(crtc, pipe_config);
18442d08 8207
0f64614d
VS
8208 /*
8209 * Normally the dotclock is filled in by the encoder .get_config()
8210 * but in case the pipe is enabled w/o any ports we need a sane
8211 * default.
8212 */
8213 pipe_config->base.adjusted_mode.crtc_clock =
8214 pipe_config->port_clock / pipe_config->pixel_multiplier;
8215
1729050e
ID
8216 ret = true;
8217
8218out:
8219 intel_display_power_put(dev_priv, power_domain);
8220
8221 return ret;
0e8ffe1b
DV
8222}
8223
dde86e2d 8224static void ironlake_init_pch_refclk(struct drm_device *dev)
13d83a67
JB
8225{
8226 struct drm_i915_private *dev_priv = dev->dev_private;
13d83a67 8227 struct intel_encoder *encoder;
74cfd7ac 8228 u32 val, final;
13d83a67 8229 bool has_lvds = false;
199e5d79 8230 bool has_cpu_edp = false;
199e5d79 8231 bool has_panel = false;
99eb6a01
KP
8232 bool has_ck505 = false;
8233 bool can_ssc = false;
13d83a67
JB
8234
8235 /* We need to take the global config into account */
b2784e15 8236 for_each_intel_encoder(dev, encoder) {
199e5d79
KP
8237 switch (encoder->type) {
8238 case INTEL_OUTPUT_LVDS:
8239 has_panel = true;
8240 has_lvds = true;
8241 break;
8242 case INTEL_OUTPUT_EDP:
8243 has_panel = true;
2de6905f 8244 if (enc_to_dig_port(&encoder->base)->port == PORT_A)
199e5d79
KP
8245 has_cpu_edp = true;
8246 break;
6847d71b
PZ
8247 default:
8248 break;
13d83a67
JB
8249 }
8250 }
8251
99eb6a01 8252 if (HAS_PCH_IBX(dev)) {
41aa3448 8253 has_ck505 = dev_priv->vbt.display_clock_mode;
99eb6a01
KP
8254 can_ssc = has_ck505;
8255 } else {
8256 has_ck505 = false;
8257 can_ssc = true;
8258 }
8259
2de6905f
ID
8260 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
8261 has_panel, has_lvds, has_ck505);
13d83a67
JB
8262
8263 /* Ironlake: try to setup display ref clock before DPLL
8264 * enabling. This is only under driver's control after
8265 * PCH B stepping, previous chipset stepping should be
8266 * ignoring this setting.
8267 */
74cfd7ac
CW
8268 val = I915_READ(PCH_DREF_CONTROL);
8269
8270 /* As we must carefully and slowly disable/enable each source in turn,
8271 * compute the final state we want first and check if we need to
8272 * make any changes at all.
8273 */
8274 final = val;
8275 final &= ~DREF_NONSPREAD_SOURCE_MASK;
8276 if (has_ck505)
8277 final |= DREF_NONSPREAD_CK505_ENABLE;
8278 else
8279 final |= DREF_NONSPREAD_SOURCE_ENABLE;
8280
8281 final &= ~DREF_SSC_SOURCE_MASK;
8282 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
8283 final &= ~DREF_SSC1_ENABLE;
8284
8285 if (has_panel) {
8286 final |= DREF_SSC_SOURCE_ENABLE;
8287
8288 if (intel_panel_use_ssc(dev_priv) && can_ssc)
8289 final |= DREF_SSC1_ENABLE;
8290
8291 if (has_cpu_edp) {
8292 if (intel_panel_use_ssc(dev_priv) && can_ssc)
8293 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
8294 else
8295 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
8296 } else
8297 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
8298 } else {
8299 final |= DREF_SSC_SOURCE_DISABLE;
8300 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
8301 }
8302
8303 if (final == val)
8304 return;
8305
13d83a67 8306 /* Always enable nonspread source */
74cfd7ac 8307 val &= ~DREF_NONSPREAD_SOURCE_MASK;
13d83a67 8308
99eb6a01 8309 if (has_ck505)
74cfd7ac 8310 val |= DREF_NONSPREAD_CK505_ENABLE;
99eb6a01 8311 else
74cfd7ac 8312 val |= DREF_NONSPREAD_SOURCE_ENABLE;
13d83a67 8313
199e5d79 8314 if (has_panel) {
74cfd7ac
CW
8315 val &= ~DREF_SSC_SOURCE_MASK;
8316 val |= DREF_SSC_SOURCE_ENABLE;
13d83a67 8317
199e5d79 8318 /* SSC must be turned on before enabling the CPU output */
99eb6a01 8319 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
199e5d79 8320 DRM_DEBUG_KMS("Using SSC on panel\n");
74cfd7ac 8321 val |= DREF_SSC1_ENABLE;
e77166b5 8322 } else
74cfd7ac 8323 val &= ~DREF_SSC1_ENABLE;
199e5d79
KP
8324
8325 /* Get SSC going before enabling the outputs */
74cfd7ac 8326 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
8327 POSTING_READ(PCH_DREF_CONTROL);
8328 udelay(200);
8329
74cfd7ac 8330 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
13d83a67
JB
8331
8332 /* Enable CPU source on CPU attached eDP */
199e5d79 8333 if (has_cpu_edp) {
99eb6a01 8334 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
199e5d79 8335 DRM_DEBUG_KMS("Using SSC on eDP\n");
74cfd7ac 8336 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
eba905b2 8337 } else
74cfd7ac 8338 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
199e5d79 8339 } else
74cfd7ac 8340 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
199e5d79 8341
74cfd7ac 8342 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
8343 POSTING_READ(PCH_DREF_CONTROL);
8344 udelay(200);
8345 } else {
8346 DRM_DEBUG_KMS("Disabling SSC entirely\n");
8347
74cfd7ac 8348 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
199e5d79
KP
8349
8350 /* Turn off CPU output */
74cfd7ac 8351 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
199e5d79 8352
74cfd7ac 8353 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
8354 POSTING_READ(PCH_DREF_CONTROL);
8355 udelay(200);
8356
8357 /* Turn off the SSC source */
74cfd7ac
CW
8358 val &= ~DREF_SSC_SOURCE_MASK;
8359 val |= DREF_SSC_SOURCE_DISABLE;
199e5d79
KP
8360
8361 /* Turn off SSC1 */
74cfd7ac 8362 val &= ~DREF_SSC1_ENABLE;
199e5d79 8363
74cfd7ac 8364 I915_WRITE(PCH_DREF_CONTROL, val);
13d83a67
JB
8365 POSTING_READ(PCH_DREF_CONTROL);
8366 udelay(200);
8367 }
74cfd7ac
CW
8368
8369 BUG_ON(val != final);
13d83a67
JB
8370}
8371
f31f2d55 8372static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
dde86e2d 8373{
f31f2d55 8374 uint32_t tmp;
dde86e2d 8375
0ff066a9
PZ
8376 tmp = I915_READ(SOUTH_CHICKEN2);
8377 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
8378 I915_WRITE(SOUTH_CHICKEN2, tmp);
dde86e2d 8379
0ff066a9
PZ
8380 if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
8381 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
8382 DRM_ERROR("FDI mPHY reset assert timeout\n");
dde86e2d 8383
0ff066a9
PZ
8384 tmp = I915_READ(SOUTH_CHICKEN2);
8385 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
8386 I915_WRITE(SOUTH_CHICKEN2, tmp);
dde86e2d 8387
0ff066a9
PZ
8388 if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
8389 FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
8390 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
f31f2d55
PZ
8391}
8392
8393/* WaMPhyProgramming:hsw */
8394static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
8395{
8396 uint32_t tmp;
dde86e2d
PZ
8397
8398 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
8399 tmp &= ~(0xFF << 24);
8400 tmp |= (0x12 << 24);
8401 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
8402
dde86e2d
PZ
8403 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
8404 tmp |= (1 << 11);
8405 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
8406
8407 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
8408 tmp |= (1 << 11);
8409 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
8410
dde86e2d
PZ
8411 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
8412 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
8413 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
8414
8415 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
8416 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
8417 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
8418
0ff066a9
PZ
8419 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
8420 tmp &= ~(7 << 13);
8421 tmp |= (5 << 13);
8422 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
dde86e2d 8423
0ff066a9
PZ
8424 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
8425 tmp &= ~(7 << 13);
8426 tmp |= (5 << 13);
8427 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
dde86e2d
PZ
8428
8429 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
8430 tmp &= ~0xFF;
8431 tmp |= 0x1C;
8432 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
8433
8434 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
8435 tmp &= ~0xFF;
8436 tmp |= 0x1C;
8437 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
8438
8439 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
8440 tmp &= ~(0xFF << 16);
8441 tmp |= (0x1C << 16);
8442 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
8443
8444 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
8445 tmp &= ~(0xFF << 16);
8446 tmp |= (0x1C << 16);
8447 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
8448
0ff066a9
PZ
8449 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
8450 tmp |= (1 << 27);
8451 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
dde86e2d 8452
0ff066a9
PZ
8453 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
8454 tmp |= (1 << 27);
8455 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
dde86e2d 8456
0ff066a9
PZ
8457 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
8458 tmp &= ~(0xF << 28);
8459 tmp |= (4 << 28);
8460 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
dde86e2d 8461
0ff066a9
PZ
8462 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
8463 tmp &= ~(0xF << 28);
8464 tmp |= (4 << 28);
8465 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
f31f2d55
PZ
8466}
8467
2fa86a1f
PZ
8468/* Implements 3 different sequences from BSpec chapter "Display iCLK
8469 * Programming" based on the parameters passed:
8470 * - Sequence to enable CLKOUT_DP
8471 * - Sequence to enable CLKOUT_DP without spread
8472 * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
8473 */
8474static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
8475 bool with_fdi)
f31f2d55
PZ
8476{
8477 struct drm_i915_private *dev_priv = dev->dev_private;
2fa86a1f
PZ
8478 uint32_t reg, tmp;
8479
8480 if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
8481 with_spread = true;
c2699524 8482 if (WARN(HAS_PCH_LPT_LP(dev) && with_fdi, "LP PCH doesn't have FDI\n"))
2fa86a1f 8483 with_fdi = false;
f31f2d55 8484
a580516d 8485 mutex_lock(&dev_priv->sb_lock);
f31f2d55
PZ
8486
8487 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8488 tmp &= ~SBI_SSCCTL_DISABLE;
8489 tmp |= SBI_SSCCTL_PATHALT;
8490 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8491
8492 udelay(24);
8493
2fa86a1f
PZ
8494 if (with_spread) {
8495 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8496 tmp &= ~SBI_SSCCTL_PATHALT;
8497 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
f31f2d55 8498
2fa86a1f
PZ
8499 if (with_fdi) {
8500 lpt_reset_fdi_mphy(dev_priv);
8501 lpt_program_fdi_mphy(dev_priv);
8502 }
8503 }
dde86e2d 8504
c2699524 8505 reg = HAS_PCH_LPT_LP(dev) ? SBI_GEN0 : SBI_DBUFF0;
2fa86a1f
PZ
8506 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8507 tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8508 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
c00db246 8509
a580516d 8510 mutex_unlock(&dev_priv->sb_lock);
dde86e2d
PZ
8511}
8512
47701c3b
PZ
8513/* Sequence to disable CLKOUT_DP */
8514static void lpt_disable_clkout_dp(struct drm_device *dev)
8515{
8516 struct drm_i915_private *dev_priv = dev->dev_private;
8517 uint32_t reg, tmp;
8518
a580516d 8519 mutex_lock(&dev_priv->sb_lock);
47701c3b 8520
c2699524 8521 reg = HAS_PCH_LPT_LP(dev) ? SBI_GEN0 : SBI_DBUFF0;
47701c3b
PZ
8522 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8523 tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8524 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
8525
8526 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8527 if (!(tmp & SBI_SSCCTL_DISABLE)) {
8528 if (!(tmp & SBI_SSCCTL_PATHALT)) {
8529 tmp |= SBI_SSCCTL_PATHALT;
8530 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8531 udelay(32);
8532 }
8533 tmp |= SBI_SSCCTL_DISABLE;
8534 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8535 }
8536
a580516d 8537 mutex_unlock(&dev_priv->sb_lock);
47701c3b
PZ
8538}
8539
f7be2c21
VS
8540#define BEND_IDX(steps) ((50 + (steps)) / 5)
8541
8542static const uint16_t sscdivintphase[] = {
8543 [BEND_IDX( 50)] = 0x3B23,
8544 [BEND_IDX( 45)] = 0x3B23,
8545 [BEND_IDX( 40)] = 0x3C23,
8546 [BEND_IDX( 35)] = 0x3C23,
8547 [BEND_IDX( 30)] = 0x3D23,
8548 [BEND_IDX( 25)] = 0x3D23,
8549 [BEND_IDX( 20)] = 0x3E23,
8550 [BEND_IDX( 15)] = 0x3E23,
8551 [BEND_IDX( 10)] = 0x3F23,
8552 [BEND_IDX( 5)] = 0x3F23,
8553 [BEND_IDX( 0)] = 0x0025,
8554 [BEND_IDX( -5)] = 0x0025,
8555 [BEND_IDX(-10)] = 0x0125,
8556 [BEND_IDX(-15)] = 0x0125,
8557 [BEND_IDX(-20)] = 0x0225,
8558 [BEND_IDX(-25)] = 0x0225,
8559 [BEND_IDX(-30)] = 0x0325,
8560 [BEND_IDX(-35)] = 0x0325,
8561 [BEND_IDX(-40)] = 0x0425,
8562 [BEND_IDX(-45)] = 0x0425,
8563 [BEND_IDX(-50)] = 0x0525,
8564};
8565
8566/*
8567 * Bend CLKOUT_DP
8568 * steps -50 to 50 inclusive, in steps of 5
8569 * < 0 slow down the clock, > 0 speed up the clock, 0 == no bend (135MHz)
8570 * change in clock period = -(steps / 10) * 5.787 ps
8571 */
8572static void lpt_bend_clkout_dp(struct drm_i915_private *dev_priv, int steps)
8573{
8574 uint32_t tmp;
8575 int idx = BEND_IDX(steps);
8576
8577 if (WARN_ON(steps % 5 != 0))
8578 return;
8579
8580 if (WARN_ON(idx >= ARRAY_SIZE(sscdivintphase)))
8581 return;
8582
8583 mutex_lock(&dev_priv->sb_lock);
8584
8585 if (steps % 10 != 0)
8586 tmp = 0xAAAAAAAB;
8587 else
8588 tmp = 0x00000000;
8589 intel_sbi_write(dev_priv, SBI_SSCDITHPHASE, tmp, SBI_ICLK);
8590
8591 tmp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE, SBI_ICLK);
8592 tmp &= 0xffff0000;
8593 tmp |= sscdivintphase[idx];
8594 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE, tmp, SBI_ICLK);
8595
8596 mutex_unlock(&dev_priv->sb_lock);
8597}
8598
8599#undef BEND_IDX
8600
bf8fa3d3
PZ
8601static void lpt_init_pch_refclk(struct drm_device *dev)
8602{
bf8fa3d3
PZ
8603 struct intel_encoder *encoder;
8604 bool has_vga = false;
8605
b2784e15 8606 for_each_intel_encoder(dev, encoder) {
bf8fa3d3
PZ
8607 switch (encoder->type) {
8608 case INTEL_OUTPUT_ANALOG:
8609 has_vga = true;
8610 break;
6847d71b
PZ
8611 default:
8612 break;
bf8fa3d3
PZ
8613 }
8614 }
8615
f7be2c21
VS
8616 if (has_vga) {
8617 lpt_bend_clkout_dp(to_i915(dev), 0);
47701c3b 8618 lpt_enable_clkout_dp(dev, true, true);
f7be2c21 8619 } else {
47701c3b 8620 lpt_disable_clkout_dp(dev);
f7be2c21 8621 }
bf8fa3d3
PZ
8622}
8623
dde86e2d
PZ
8624/*
8625 * Initialize reference clocks when the driver loads
8626 */
8627void intel_init_pch_refclk(struct drm_device *dev)
8628{
8629 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
8630 ironlake_init_pch_refclk(dev);
8631 else if (HAS_PCH_LPT(dev))
8632 lpt_init_pch_refclk(dev);
8633}
8634
6ff93609 8635static void ironlake_set_pipeconf(struct drm_crtc *crtc)
79e53945 8636{
c8203565 8637 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
79e53945
JB
8638 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8639 int pipe = intel_crtc->pipe;
c8203565
PZ
8640 uint32_t val;
8641
78114071 8642 val = 0;
c8203565 8643
6e3c9717 8644 switch (intel_crtc->config->pipe_bpp) {
c8203565 8645 case 18:
dfd07d72 8646 val |= PIPECONF_6BPC;
c8203565
PZ
8647 break;
8648 case 24:
dfd07d72 8649 val |= PIPECONF_8BPC;
c8203565
PZ
8650 break;
8651 case 30:
dfd07d72 8652 val |= PIPECONF_10BPC;
c8203565
PZ
8653 break;
8654 case 36:
dfd07d72 8655 val |= PIPECONF_12BPC;
c8203565
PZ
8656 break;
8657 default:
cc769b62
PZ
8658 /* Case prevented by intel_choose_pipe_bpp_dither. */
8659 BUG();
c8203565
PZ
8660 }
8661
6e3c9717 8662 if (intel_crtc->config->dither)
c8203565
PZ
8663 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8664
6e3c9717 8665 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
c8203565
PZ
8666 val |= PIPECONF_INTERLACED_ILK;
8667 else
8668 val |= PIPECONF_PROGRESSIVE;
8669
6e3c9717 8670 if (intel_crtc->config->limited_color_range)
3685a8f3 8671 val |= PIPECONF_COLOR_RANGE_SELECT;
3685a8f3 8672
c8203565
PZ
8673 I915_WRITE(PIPECONF(pipe), val);
8674 POSTING_READ(PIPECONF(pipe));
8675}
8676
6ff93609 8677static void haswell_set_pipeconf(struct drm_crtc *crtc)
ee2b0b38 8678{
391bf048 8679 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
ee2b0b38 8680 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6e3c9717 8681 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
391bf048 8682 u32 val = 0;
ee2b0b38 8683
391bf048 8684 if (IS_HASWELL(dev_priv) && intel_crtc->config->dither)
ee2b0b38
PZ
8685 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8686
6e3c9717 8687 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
ee2b0b38
PZ
8688 val |= PIPECONF_INTERLACED_ILK;
8689 else
8690 val |= PIPECONF_PROGRESSIVE;
8691
702e7a56
PZ
8692 I915_WRITE(PIPECONF(cpu_transcoder), val);
8693 POSTING_READ(PIPECONF(cpu_transcoder));
391bf048
JN
8694}
8695
391bf048
JN
8696static void haswell_set_pipemisc(struct drm_crtc *crtc)
8697{
8698 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
8699 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
756f85cf 8700
391bf048
JN
8701 if (IS_BROADWELL(dev_priv) || INTEL_INFO(dev_priv)->gen >= 9) {
8702 u32 val = 0;
756f85cf 8703
6e3c9717 8704 switch (intel_crtc->config->pipe_bpp) {
756f85cf
PZ
8705 case 18:
8706 val |= PIPEMISC_DITHER_6_BPC;
8707 break;
8708 case 24:
8709 val |= PIPEMISC_DITHER_8_BPC;
8710 break;
8711 case 30:
8712 val |= PIPEMISC_DITHER_10_BPC;
8713 break;
8714 case 36:
8715 val |= PIPEMISC_DITHER_12_BPC;
8716 break;
8717 default:
8718 /* Case prevented by pipe_config_set_bpp. */
8719 BUG();
8720 }
8721
6e3c9717 8722 if (intel_crtc->config->dither)
756f85cf
PZ
8723 val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
8724
391bf048 8725 I915_WRITE(PIPEMISC(intel_crtc->pipe), val);
756f85cf 8726 }
ee2b0b38
PZ
8727}
8728
d4b1931c
PZ
8729int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
8730{
8731 /*
8732 * Account for spread spectrum to avoid
8733 * oversubscribing the link. Max center spread
8734 * is 2.5%; use 5% for safety's sake.
8735 */
8736 u32 bps = target_clock * bpp * 21 / 20;
619d4d04 8737 return DIV_ROUND_UP(bps, link_bw * 8);
d4b1931c
PZ
8738}
8739
7429e9d4 8740static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
6cf86a5e 8741{
7429e9d4 8742 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
f48d8f23
PZ
8743}
8744
b75ca6f6
ACO
8745static void ironlake_compute_dpll(struct intel_crtc *intel_crtc,
8746 struct intel_crtc_state *crtc_state,
9e2c8475 8747 struct dpll *reduced_clock)
79e53945 8748{
de13a2e3 8749 struct drm_crtc *crtc = &intel_crtc->base;
79e53945
JB
8750 struct drm_device *dev = crtc->dev;
8751 struct drm_i915_private *dev_priv = dev->dev_private;
55bb9992 8752 struct drm_atomic_state *state = crtc_state->base.state;
da3ced29 8753 struct drm_connector *connector;
55bb9992
ACO
8754 struct drm_connector_state *connector_state;
8755 struct intel_encoder *encoder;
b75ca6f6 8756 u32 dpll, fp, fp2;
ceb41007 8757 int factor, i;
09ede541 8758 bool is_lvds = false, is_sdvo = false;
79e53945 8759
da3ced29 8760 for_each_connector_in_state(state, connector, connector_state, i) {
55bb9992
ACO
8761 if (connector_state->crtc != crtc_state->base.crtc)
8762 continue;
8763
8764 encoder = to_intel_encoder(connector_state->best_encoder);
8765
8766 switch (encoder->type) {
79e53945
JB
8767 case INTEL_OUTPUT_LVDS:
8768 is_lvds = true;
8769 break;
8770 case INTEL_OUTPUT_SDVO:
7d57382e 8771 case INTEL_OUTPUT_HDMI:
79e53945 8772 is_sdvo = true;
79e53945 8773 break;
6847d71b
PZ
8774 default:
8775 break;
79e53945
JB
8776 }
8777 }
79e53945 8778
c1858123 8779 /* Enable autotuning of the PLL clock (if permissible) */
8febb297
EA
8780 factor = 21;
8781 if (is_lvds) {
8782 if ((intel_panel_use_ssc(dev_priv) &&
e91e941b 8783 dev_priv->vbt.lvds_ssc_freq == 100000) ||
f0b44056 8784 (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
8febb297 8785 factor = 25;
190f68c5 8786 } else if (crtc_state->sdvo_tv_clock)
8febb297 8787 factor = 20;
c1858123 8788
b75ca6f6
ACO
8789 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
8790
190f68c5 8791 if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
b75ca6f6
ACO
8792 fp |= FP_CB_TUNE;
8793
8794 if (reduced_clock) {
8795 fp2 = i9xx_dpll_compute_fp(reduced_clock);
2c07245f 8796
b75ca6f6
ACO
8797 if (reduced_clock->m < factor * reduced_clock->n)
8798 fp2 |= FP_CB_TUNE;
8799 } else {
8800 fp2 = fp;
8801 }
9a7c7890 8802
5eddb70b 8803 dpll = 0;
2c07245f 8804
a07d6787
EA
8805 if (is_lvds)
8806 dpll |= DPLLB_MODE_LVDS;
8807 else
8808 dpll |= DPLLB_MODE_DAC_SERIAL;
198a037f 8809
190f68c5 8810 dpll |= (crtc_state->pixel_multiplier - 1)
ef1b460d 8811 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
198a037f
DV
8812
8813 if (is_sdvo)
4a33e48d 8814 dpll |= DPLL_SDVO_HIGH_SPEED;
190f68c5 8815 if (crtc_state->has_dp_encoder)
4a33e48d 8816 dpll |= DPLL_SDVO_HIGH_SPEED;
79e53945 8817
a07d6787 8818 /* compute bitmask from p1 value */
190f68c5 8819 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
a07d6787 8820 /* also FPA1 */
190f68c5 8821 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
a07d6787 8822
190f68c5 8823 switch (crtc_state->dpll.p2) {
a07d6787
EA
8824 case 5:
8825 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
8826 break;
8827 case 7:
8828 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
8829 break;
8830 case 10:
8831 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
8832 break;
8833 case 14:
8834 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
8835 break;
79e53945
JB
8836 }
8837
ceb41007 8838 if (is_lvds && intel_panel_use_ssc(dev_priv))
43565a06 8839 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
79e53945
JB
8840 else
8841 dpll |= PLL_REF_INPUT_DREFCLK;
8842
b75ca6f6
ACO
8843 dpll |= DPLL_VCO_ENABLE;
8844
8845 crtc_state->dpll_hw_state.dpll = dpll;
8846 crtc_state->dpll_hw_state.fp0 = fp;
8847 crtc_state->dpll_hw_state.fp1 = fp2;
de13a2e3
PZ
8848}
8849
190f68c5
ACO
8850static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
8851 struct intel_crtc_state *crtc_state)
de13a2e3 8852{
997c030c
ACO
8853 struct drm_device *dev = crtc->base.dev;
8854 struct drm_i915_private *dev_priv = dev->dev_private;
9e2c8475 8855 struct dpll reduced_clock;
7ed9f894 8856 bool has_reduced_clock = false;
e2b78267 8857 struct intel_shared_dpll *pll;
1b6f4958 8858 const struct intel_limit *limit;
997c030c 8859 int refclk = 120000;
de13a2e3 8860
dd3cd74a
ACO
8861 memset(&crtc_state->dpll_hw_state, 0,
8862 sizeof(crtc_state->dpll_hw_state));
8863
ded220e2
ACO
8864 crtc->lowfreq_avail = false;
8865
8866 /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
8867 if (!crtc_state->has_pch_encoder)
8868 return 0;
79e53945 8869
997c030c
ACO
8870 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
8871 if (intel_panel_use_ssc(dev_priv)) {
8872 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
8873 dev_priv->vbt.lvds_ssc_freq);
8874 refclk = dev_priv->vbt.lvds_ssc_freq;
8875 }
8876
8877 if (intel_is_dual_link_lvds(dev)) {
8878 if (refclk == 100000)
8879 limit = &intel_limits_ironlake_dual_lvds_100m;
8880 else
8881 limit = &intel_limits_ironlake_dual_lvds;
8882 } else {
8883 if (refclk == 100000)
8884 limit = &intel_limits_ironlake_single_lvds_100m;
8885 else
8886 limit = &intel_limits_ironlake_single_lvds;
8887 }
8888 } else {
8889 limit = &intel_limits_ironlake_dac;
8890 }
8891
364ee29d 8892 if (!crtc_state->clock_set &&
997c030c
ACO
8893 !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8894 refclk, NULL, &crtc_state->dpll)) {
364ee29d
ACO
8895 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8896 return -EINVAL;
f47709a9 8897 }
79e53945 8898
b75ca6f6
ACO
8899 ironlake_compute_dpll(crtc, crtc_state,
8900 has_reduced_clock ? &reduced_clock : NULL);
66e985c0 8901
ded220e2
ACO
8902 pll = intel_get_shared_dpll(crtc, crtc_state, NULL);
8903 if (pll == NULL) {
8904 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
8905 pipe_name(crtc->pipe));
8906 return -EINVAL;
3fb37703 8907 }
79e53945 8908
ded220e2
ACO
8909 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
8910 has_reduced_clock)
c7653199 8911 crtc->lowfreq_avail = true;
e2b78267 8912
c8f7a0db 8913 return 0;
79e53945
JB
8914}
8915
eb14cb74
VS
8916static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
8917 struct intel_link_m_n *m_n)
8918{
8919 struct drm_device *dev = crtc->base.dev;
8920 struct drm_i915_private *dev_priv = dev->dev_private;
8921 enum pipe pipe = crtc->pipe;
8922
8923 m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
8924 m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
8925 m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
8926 & ~TU_SIZE_MASK;
8927 m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
8928 m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
8929 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8930}
8931
8932static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
8933 enum transcoder transcoder,
b95af8be
VK
8934 struct intel_link_m_n *m_n,
8935 struct intel_link_m_n *m2_n2)
72419203
DV
8936{
8937 struct drm_device *dev = crtc->base.dev;
8938 struct drm_i915_private *dev_priv = dev->dev_private;
eb14cb74 8939 enum pipe pipe = crtc->pipe;
72419203 8940
eb14cb74
VS
8941 if (INTEL_INFO(dev)->gen >= 5) {
8942 m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
8943 m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
8944 m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
8945 & ~TU_SIZE_MASK;
8946 m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
8947 m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
8948 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
b95af8be
VK
8949 /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
8950 * gen < 8) and if DRRS is supported (to make sure the
8951 * registers are not unnecessarily read).
8952 */
8953 if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
6e3c9717 8954 crtc->config->has_drrs) {
b95af8be
VK
8955 m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
8956 m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
8957 m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
8958 & ~TU_SIZE_MASK;
8959 m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
8960 m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
8961 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8962 }
eb14cb74
VS
8963 } else {
8964 m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
8965 m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
8966 m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
8967 & ~TU_SIZE_MASK;
8968 m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
8969 m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
8970 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8971 }
8972}
8973
8974void intel_dp_get_m_n(struct intel_crtc *crtc,
5cec258b 8975 struct intel_crtc_state *pipe_config)
eb14cb74 8976{
681a8504 8977 if (pipe_config->has_pch_encoder)
eb14cb74
VS
8978 intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
8979 else
8980 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
b95af8be
VK
8981 &pipe_config->dp_m_n,
8982 &pipe_config->dp_m2_n2);
eb14cb74 8983}
72419203 8984
eb14cb74 8985static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
5cec258b 8986 struct intel_crtc_state *pipe_config)
eb14cb74
VS
8987{
8988 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
b95af8be 8989 &pipe_config->fdi_m_n, NULL);
72419203
DV
8990}
8991
bd2e244f 8992static void skylake_get_pfit_config(struct intel_crtc *crtc,
5cec258b 8993 struct intel_crtc_state *pipe_config)
bd2e244f
JB
8994{
8995 struct drm_device *dev = crtc->base.dev;
8996 struct drm_i915_private *dev_priv = dev->dev_private;
a1b2278e
CK
8997 struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
8998 uint32_t ps_ctrl = 0;
8999 int id = -1;
9000 int i;
bd2e244f 9001
a1b2278e
CK
9002 /* find scaler attached to this pipe */
9003 for (i = 0; i < crtc->num_scalers; i++) {
9004 ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
9005 if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
9006 id = i;
9007 pipe_config->pch_pfit.enabled = true;
9008 pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
9009 pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
9010 break;
9011 }
9012 }
bd2e244f 9013
a1b2278e
CK
9014 scaler_state->scaler_id = id;
9015 if (id >= 0) {
9016 scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
9017 } else {
9018 scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
bd2e244f
JB
9019 }
9020}
9021
5724dbd1
DL
9022static void
9023skylake_get_initial_plane_config(struct intel_crtc *crtc,
9024 struct intel_initial_plane_config *plane_config)
bc8d7dff
DL
9025{
9026 struct drm_device *dev = crtc->base.dev;
9027 struct drm_i915_private *dev_priv = dev->dev_private;
40f46283 9028 u32 val, base, offset, stride_mult, tiling;
bc8d7dff
DL
9029 int pipe = crtc->pipe;
9030 int fourcc, pixel_format;
6761dd31 9031 unsigned int aligned_height;
bc8d7dff 9032 struct drm_framebuffer *fb;
1b842c89 9033 struct intel_framebuffer *intel_fb;
bc8d7dff 9034
d9806c9f 9035 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
1b842c89 9036 if (!intel_fb) {
bc8d7dff
DL
9037 DRM_DEBUG_KMS("failed to alloc fb\n");
9038 return;
9039 }
9040
1b842c89
DL
9041 fb = &intel_fb->base;
9042
bc8d7dff 9043 val = I915_READ(PLANE_CTL(pipe, 0));
42a7b088
DL
9044 if (!(val & PLANE_CTL_ENABLE))
9045 goto error;
9046
bc8d7dff
DL
9047 pixel_format = val & PLANE_CTL_FORMAT_MASK;
9048 fourcc = skl_format_to_fourcc(pixel_format,
9049 val & PLANE_CTL_ORDER_RGBX,
9050 val & PLANE_CTL_ALPHA_MASK);
9051 fb->pixel_format = fourcc;
9052 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
9053
40f46283
DL
9054 tiling = val & PLANE_CTL_TILED_MASK;
9055 switch (tiling) {
9056 case PLANE_CTL_TILED_LINEAR:
9057 fb->modifier[0] = DRM_FORMAT_MOD_NONE;
9058 break;
9059 case PLANE_CTL_TILED_X:
9060 plane_config->tiling = I915_TILING_X;
9061 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
9062 break;
9063 case PLANE_CTL_TILED_Y:
9064 fb->modifier[0] = I915_FORMAT_MOD_Y_TILED;
9065 break;
9066 case PLANE_CTL_TILED_YF:
9067 fb->modifier[0] = I915_FORMAT_MOD_Yf_TILED;
9068 break;
9069 default:
9070 MISSING_CASE(tiling);
9071 goto error;
9072 }
9073
bc8d7dff
DL
9074 base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
9075 plane_config->base = base;
9076
9077 offset = I915_READ(PLANE_OFFSET(pipe, 0));
9078
9079 val = I915_READ(PLANE_SIZE(pipe, 0));
9080 fb->height = ((val >> 16) & 0xfff) + 1;
9081 fb->width = ((val >> 0) & 0x1fff) + 1;
9082
9083 val = I915_READ(PLANE_STRIDE(pipe, 0));
7b49f948 9084 stride_mult = intel_fb_stride_alignment(dev_priv, fb->modifier[0],
40f46283 9085 fb->pixel_format);
bc8d7dff
DL
9086 fb->pitches[0] = (val & 0x3ff) * stride_mult;
9087
9088 aligned_height = intel_fb_align_height(dev, fb->height,
091df6cb
DV
9089 fb->pixel_format,
9090 fb->modifier[0]);
bc8d7dff 9091
f37b5c2b 9092 plane_config->size = fb->pitches[0] * aligned_height;
bc8d7dff
DL
9093
9094 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9095 pipe_name(pipe), fb->width, fb->height,
9096 fb->bits_per_pixel, base, fb->pitches[0],
9097 plane_config->size);
9098
2d14030b 9099 plane_config->fb = intel_fb;
bc8d7dff
DL
9100 return;
9101
9102error:
9103 kfree(fb);
9104}
9105
2fa2fe9a 9106static void ironlake_get_pfit_config(struct intel_crtc *crtc,
5cec258b 9107 struct intel_crtc_state *pipe_config)
2fa2fe9a
DV
9108{
9109 struct drm_device *dev = crtc->base.dev;
9110 struct drm_i915_private *dev_priv = dev->dev_private;
9111 uint32_t tmp;
9112
9113 tmp = I915_READ(PF_CTL(crtc->pipe));
9114
9115 if (tmp & PF_ENABLE) {
fd4daa9c 9116 pipe_config->pch_pfit.enabled = true;
2fa2fe9a
DV
9117 pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
9118 pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
cb8b2a30
DV
9119
9120 /* We currently do not free assignements of panel fitters on
9121 * ivb/hsw (since we don't use the higher upscaling modes which
9122 * differentiates them) so just WARN about this case for now. */
9123 if (IS_GEN7(dev)) {
9124 WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
9125 PF_PIPE_SEL_IVB(crtc->pipe));
9126 }
2fa2fe9a 9127 }
79e53945
JB
9128}
9129
5724dbd1
DL
9130static void
9131ironlake_get_initial_plane_config(struct intel_crtc *crtc,
9132 struct intel_initial_plane_config *plane_config)
4c6baa59
JB
9133{
9134 struct drm_device *dev = crtc->base.dev;
9135 struct drm_i915_private *dev_priv = dev->dev_private;
9136 u32 val, base, offset;
aeee5a49 9137 int pipe = crtc->pipe;
4c6baa59 9138 int fourcc, pixel_format;
6761dd31 9139 unsigned int aligned_height;
b113d5ee 9140 struct drm_framebuffer *fb;
1b842c89 9141 struct intel_framebuffer *intel_fb;
4c6baa59 9142
42a7b088
DL
9143 val = I915_READ(DSPCNTR(pipe));
9144 if (!(val & DISPLAY_PLANE_ENABLE))
9145 return;
9146
d9806c9f 9147 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
1b842c89 9148 if (!intel_fb) {
4c6baa59
JB
9149 DRM_DEBUG_KMS("failed to alloc fb\n");
9150 return;
9151 }
9152
1b842c89
DL
9153 fb = &intel_fb->base;
9154
18c5247e
DV
9155 if (INTEL_INFO(dev)->gen >= 4) {
9156 if (val & DISPPLANE_TILED) {
49af449b 9157 plane_config->tiling = I915_TILING_X;
18c5247e
DV
9158 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
9159 }
9160 }
4c6baa59
JB
9161
9162 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
b35d63fa 9163 fourcc = i9xx_format_to_fourcc(pixel_format);
b113d5ee
DL
9164 fb->pixel_format = fourcc;
9165 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
4c6baa59 9166
aeee5a49 9167 base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
4c6baa59 9168 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
aeee5a49 9169 offset = I915_READ(DSPOFFSET(pipe));
4c6baa59 9170 } else {
49af449b 9171 if (plane_config->tiling)
aeee5a49 9172 offset = I915_READ(DSPTILEOFF(pipe));
4c6baa59 9173 else
aeee5a49 9174 offset = I915_READ(DSPLINOFF(pipe));
4c6baa59
JB
9175 }
9176 plane_config->base = base;
9177
9178 val = I915_READ(PIPESRC(pipe));
b113d5ee
DL
9179 fb->width = ((val >> 16) & 0xfff) + 1;
9180 fb->height = ((val >> 0) & 0xfff) + 1;
4c6baa59
JB
9181
9182 val = I915_READ(DSPSTRIDE(pipe));
b113d5ee 9183 fb->pitches[0] = val & 0xffffffc0;
4c6baa59 9184
b113d5ee 9185 aligned_height = intel_fb_align_height(dev, fb->height,
091df6cb
DV
9186 fb->pixel_format,
9187 fb->modifier[0]);
4c6baa59 9188
f37b5c2b 9189 plane_config->size = fb->pitches[0] * aligned_height;
4c6baa59 9190
2844a921
DL
9191 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9192 pipe_name(pipe), fb->width, fb->height,
9193 fb->bits_per_pixel, base, fb->pitches[0],
9194 plane_config->size);
b113d5ee 9195
2d14030b 9196 plane_config->fb = intel_fb;
4c6baa59
JB
9197}
9198
0e8ffe1b 9199static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
5cec258b 9200 struct intel_crtc_state *pipe_config)
0e8ffe1b
DV
9201{
9202 struct drm_device *dev = crtc->base.dev;
9203 struct drm_i915_private *dev_priv = dev->dev_private;
1729050e 9204 enum intel_display_power_domain power_domain;
0e8ffe1b 9205 uint32_t tmp;
1729050e 9206 bool ret;
0e8ffe1b 9207
1729050e
ID
9208 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
9209 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
930e8c9e
PZ
9210 return false;
9211
e143a21c 9212 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
8106ddbd 9213 pipe_config->shared_dpll = NULL;
eccb140b 9214
1729050e 9215 ret = false;
0e8ffe1b
DV
9216 tmp = I915_READ(PIPECONF(crtc->pipe));
9217 if (!(tmp & PIPECONF_ENABLE))
1729050e 9218 goto out;
0e8ffe1b 9219
42571aef
VS
9220 switch (tmp & PIPECONF_BPC_MASK) {
9221 case PIPECONF_6BPC:
9222 pipe_config->pipe_bpp = 18;
9223 break;
9224 case PIPECONF_8BPC:
9225 pipe_config->pipe_bpp = 24;
9226 break;
9227 case PIPECONF_10BPC:
9228 pipe_config->pipe_bpp = 30;
9229 break;
9230 case PIPECONF_12BPC:
9231 pipe_config->pipe_bpp = 36;
9232 break;
9233 default:
9234 break;
9235 }
9236
b5a9fa09
DV
9237 if (tmp & PIPECONF_COLOR_RANGE_SELECT)
9238 pipe_config->limited_color_range = true;
9239
ab9412ba 9240 if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
66e985c0 9241 struct intel_shared_dpll *pll;
8106ddbd 9242 enum intel_dpll_id pll_id;
66e985c0 9243
88adfff1
DV
9244 pipe_config->has_pch_encoder = true;
9245
627eb5a3
DV
9246 tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
9247 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
9248 FDI_DP_PORT_WIDTH_SHIFT) + 1;
72419203
DV
9249
9250 ironlake_get_fdi_m_n_config(crtc, pipe_config);
6c49f241 9251
2d1fe073 9252 if (HAS_PCH_IBX(dev_priv)) {
d9a7bc67
ID
9253 /*
9254 * The pipe->pch transcoder and pch transcoder->pll
9255 * mapping is fixed.
9256 */
8106ddbd 9257 pll_id = (enum intel_dpll_id) crtc->pipe;
c0d43d62
DV
9258 } else {
9259 tmp = I915_READ(PCH_DPLL_SEL);
9260 if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
8106ddbd 9261 pll_id = DPLL_ID_PCH_PLL_B;
c0d43d62 9262 else
8106ddbd 9263 pll_id= DPLL_ID_PCH_PLL_A;
c0d43d62 9264 }
66e985c0 9265
8106ddbd
ACO
9266 pipe_config->shared_dpll =
9267 intel_get_shared_dpll_by_id(dev_priv, pll_id);
9268 pll = pipe_config->shared_dpll;
66e985c0 9269
2edd6443
ACO
9270 WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
9271 &pipe_config->dpll_hw_state));
c93f54cf
DV
9272
9273 tmp = pipe_config->dpll_hw_state.dpll;
9274 pipe_config->pixel_multiplier =
9275 ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
9276 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
18442d08
VS
9277
9278 ironlake_pch_clock_get(crtc, pipe_config);
6c49f241
DV
9279 } else {
9280 pipe_config->pixel_multiplier = 1;
627eb5a3
DV
9281 }
9282
1bd1bd80 9283 intel_get_pipe_timings(crtc, pipe_config);
bc58be60 9284 intel_get_pipe_src_size(crtc, pipe_config);
1bd1bd80 9285
2fa2fe9a
DV
9286 ironlake_get_pfit_config(crtc, pipe_config);
9287
1729050e
ID
9288 ret = true;
9289
9290out:
9291 intel_display_power_put(dev_priv, power_domain);
9292
9293 return ret;
0e8ffe1b
DV
9294}
9295
be256dc7
PZ
9296static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
9297{
9298 struct drm_device *dev = dev_priv->dev;
be256dc7 9299 struct intel_crtc *crtc;
be256dc7 9300
d3fcc808 9301 for_each_intel_crtc(dev, crtc)
e2c719b7 9302 I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
be256dc7
PZ
9303 pipe_name(crtc->pipe));
9304
e2c719b7
RC
9305 I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
9306 I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
01403de3
VS
9307 I915_STATE_WARN(I915_READ(WRPLL_CTL(0)) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
9308 I915_STATE_WARN(I915_READ(WRPLL_CTL(1)) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
e2c719b7
RC
9309 I915_STATE_WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
9310 I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
be256dc7 9311 "CPU PWM1 enabled\n");
c5107b87 9312 if (IS_HASWELL(dev))
e2c719b7 9313 I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
c5107b87 9314 "CPU PWM2 enabled\n");
e2c719b7 9315 I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
be256dc7 9316 "PCH PWM1 enabled\n");
e2c719b7 9317 I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
be256dc7 9318 "Utility pin enabled\n");
e2c719b7 9319 I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
be256dc7 9320
9926ada1
PZ
9321 /*
9322 * In theory we can still leave IRQs enabled, as long as only the HPD
9323 * interrupts remain enabled. We used to check for that, but since it's
9324 * gen-specific and since we only disable LCPLL after we fully disable
9325 * the interrupts, the check below should be enough.
9326 */
e2c719b7 9327 I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
be256dc7
PZ
9328}
9329
9ccd5aeb
PZ
9330static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
9331{
9332 struct drm_device *dev = dev_priv->dev;
9333
9334 if (IS_HASWELL(dev))
9335 return I915_READ(D_COMP_HSW);
9336 else
9337 return I915_READ(D_COMP_BDW);
9338}
9339
3c4c9b81
PZ
9340static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
9341{
9342 struct drm_device *dev = dev_priv->dev;
9343
9344 if (IS_HASWELL(dev)) {
9345 mutex_lock(&dev_priv->rps.hw_lock);
9346 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
9347 val))
f475dadf 9348 DRM_ERROR("Failed to write to D_COMP\n");
3c4c9b81
PZ
9349 mutex_unlock(&dev_priv->rps.hw_lock);
9350 } else {
9ccd5aeb
PZ
9351 I915_WRITE(D_COMP_BDW, val);
9352 POSTING_READ(D_COMP_BDW);
3c4c9b81 9353 }
be256dc7
PZ
9354}
9355
9356/*
9357 * This function implements pieces of two sequences from BSpec:
9358 * - Sequence for display software to disable LCPLL
9359 * - Sequence for display software to allow package C8+
9360 * The steps implemented here are just the steps that actually touch the LCPLL
9361 * register. Callers should take care of disabling all the display engine
9362 * functions, doing the mode unset, fixing interrupts, etc.
9363 */
6ff58d53
PZ
9364static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
9365 bool switch_to_fclk, bool allow_power_down)
be256dc7
PZ
9366{
9367 uint32_t val;
9368
9369 assert_can_disable_lcpll(dev_priv);
9370
9371 val = I915_READ(LCPLL_CTL);
9372
9373 if (switch_to_fclk) {
9374 val |= LCPLL_CD_SOURCE_FCLK;
9375 I915_WRITE(LCPLL_CTL, val);
9376
9377 if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
9378 LCPLL_CD_SOURCE_FCLK_DONE, 1))
9379 DRM_ERROR("Switching to FCLK failed\n");
9380
9381 val = I915_READ(LCPLL_CTL);
9382 }
9383
9384 val |= LCPLL_PLL_DISABLE;
9385 I915_WRITE(LCPLL_CTL, val);
9386 POSTING_READ(LCPLL_CTL);
9387
9388 if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
9389 DRM_ERROR("LCPLL still locked\n");
9390
9ccd5aeb 9391 val = hsw_read_dcomp(dev_priv);
be256dc7 9392 val |= D_COMP_COMP_DISABLE;
3c4c9b81 9393 hsw_write_dcomp(dev_priv, val);
be256dc7
PZ
9394 ndelay(100);
9395
9ccd5aeb
PZ
9396 if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
9397 1))
be256dc7
PZ
9398 DRM_ERROR("D_COMP RCOMP still in progress\n");
9399
9400 if (allow_power_down) {
9401 val = I915_READ(LCPLL_CTL);
9402 val |= LCPLL_POWER_DOWN_ALLOW;
9403 I915_WRITE(LCPLL_CTL, val);
9404 POSTING_READ(LCPLL_CTL);
9405 }
9406}
9407
9408/*
9409 * Fully restores LCPLL, disallowing power down and switching back to LCPLL
9410 * source.
9411 */
6ff58d53 9412static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
be256dc7
PZ
9413{
9414 uint32_t val;
9415
9416 val = I915_READ(LCPLL_CTL);
9417
9418 if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
9419 LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
9420 return;
9421
a8a8bd54
PZ
9422 /*
9423 * Make sure we're not on PC8 state before disabling PC8, otherwise
9424 * we'll hang the machine. To prevent PC8 state, just enable force_wake.
a8a8bd54 9425 */
59bad947 9426 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
215733fa 9427
be256dc7
PZ
9428 if (val & LCPLL_POWER_DOWN_ALLOW) {
9429 val &= ~LCPLL_POWER_DOWN_ALLOW;
9430 I915_WRITE(LCPLL_CTL, val);
35d8f2eb 9431 POSTING_READ(LCPLL_CTL);
be256dc7
PZ
9432 }
9433
9ccd5aeb 9434 val = hsw_read_dcomp(dev_priv);
be256dc7
PZ
9435 val |= D_COMP_COMP_FORCE;
9436 val &= ~D_COMP_COMP_DISABLE;
3c4c9b81 9437 hsw_write_dcomp(dev_priv, val);
be256dc7
PZ
9438
9439 val = I915_READ(LCPLL_CTL);
9440 val &= ~LCPLL_PLL_DISABLE;
9441 I915_WRITE(LCPLL_CTL, val);
9442
9443 if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
9444 DRM_ERROR("LCPLL not locked yet\n");
9445
9446 if (val & LCPLL_CD_SOURCE_FCLK) {
9447 val = I915_READ(LCPLL_CTL);
9448 val &= ~LCPLL_CD_SOURCE_FCLK;
9449 I915_WRITE(LCPLL_CTL, val);
9450
9451 if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
9452 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
9453 DRM_ERROR("Switching back to LCPLL failed\n");
9454 }
215733fa 9455
59bad947 9456 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
b6283055 9457 intel_update_cdclk(dev_priv->dev);
be256dc7
PZ
9458}
9459
765dab67
PZ
9460/*
9461 * Package states C8 and deeper are really deep PC states that can only be
9462 * reached when all the devices on the system allow it, so even if the graphics
9463 * device allows PC8+, it doesn't mean the system will actually get to these
9464 * states. Our driver only allows PC8+ when going into runtime PM.
9465 *
9466 * The requirements for PC8+ are that all the outputs are disabled, the power
9467 * well is disabled and most interrupts are disabled, and these are also
9468 * requirements for runtime PM. When these conditions are met, we manually do
9469 * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
9470 * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
9471 * hang the machine.
9472 *
9473 * When we really reach PC8 or deeper states (not just when we allow it) we lose
9474 * the state of some registers, so when we come back from PC8+ we need to
9475 * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
9476 * need to take care of the registers kept by RC6. Notice that this happens even
9477 * if we don't put the device in PCI D3 state (which is what currently happens
9478 * because of the runtime PM support).
9479 *
9480 * For more, read "Display Sequences for Package C8" on the hardware
9481 * documentation.
9482 */
a14cb6fc 9483void hsw_enable_pc8(struct drm_i915_private *dev_priv)
c67a470b 9484{
c67a470b
PZ
9485 struct drm_device *dev = dev_priv->dev;
9486 uint32_t val;
9487
c67a470b
PZ
9488 DRM_DEBUG_KMS("Enabling package C8+\n");
9489
c2699524 9490 if (HAS_PCH_LPT_LP(dev)) {
c67a470b
PZ
9491 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9492 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
9493 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9494 }
9495
9496 lpt_disable_clkout_dp(dev);
c67a470b
PZ
9497 hsw_disable_lcpll(dev_priv, true, true);
9498}
9499
a14cb6fc 9500void hsw_disable_pc8(struct drm_i915_private *dev_priv)
c67a470b
PZ
9501{
9502 struct drm_device *dev = dev_priv->dev;
9503 uint32_t val;
9504
c67a470b
PZ
9505 DRM_DEBUG_KMS("Disabling package C8+\n");
9506
9507 hsw_restore_lcpll(dev_priv);
c67a470b
PZ
9508 lpt_init_pch_refclk(dev);
9509
c2699524 9510 if (HAS_PCH_LPT_LP(dev)) {
c67a470b
PZ
9511 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9512 val |= PCH_LP_PARTITION_LEVEL_DISABLE;
9513 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9514 }
c67a470b
PZ
9515}
9516
27c329ed 9517static void broxton_modeset_commit_cdclk(struct drm_atomic_state *old_state)
f8437dd1 9518{
a821fc46 9519 struct drm_device *dev = old_state->dev;
1a617b77
ML
9520 struct intel_atomic_state *old_intel_state =
9521 to_intel_atomic_state(old_state);
9522 unsigned int req_cdclk = old_intel_state->dev_cdclk;
f8437dd1 9523
c6c4696f 9524 broxton_set_cdclk(to_i915(dev), req_cdclk);
f8437dd1
VK
9525}
9526
b432e5cf 9527/* compute the max rate for new configuration */
27c329ed 9528static int ilk_max_pixel_rate(struct drm_atomic_state *state)
b432e5cf 9529{
565602d7
ML
9530 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
9531 struct drm_i915_private *dev_priv = state->dev->dev_private;
9532 struct drm_crtc *crtc;
9533 struct drm_crtc_state *cstate;
27c329ed 9534 struct intel_crtc_state *crtc_state;
565602d7
ML
9535 unsigned max_pixel_rate = 0, i;
9536 enum pipe pipe;
b432e5cf 9537
565602d7
ML
9538 memcpy(intel_state->min_pixclk, dev_priv->min_pixclk,
9539 sizeof(intel_state->min_pixclk));
27c329ed 9540
565602d7
ML
9541 for_each_crtc_in_state(state, crtc, cstate, i) {
9542 int pixel_rate;
27c329ed 9543
565602d7
ML
9544 crtc_state = to_intel_crtc_state(cstate);
9545 if (!crtc_state->base.enable) {
9546 intel_state->min_pixclk[i] = 0;
b432e5cf 9547 continue;
565602d7 9548 }
b432e5cf 9549
27c329ed 9550 pixel_rate = ilk_pipe_pixel_rate(crtc_state);
b432e5cf
VS
9551
9552 /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
565602d7 9553 if (IS_BROADWELL(dev_priv) && crtc_state->ips_enabled)
b432e5cf
VS
9554 pixel_rate = DIV_ROUND_UP(pixel_rate * 100, 95);
9555
565602d7 9556 intel_state->min_pixclk[i] = pixel_rate;
b432e5cf
VS
9557 }
9558
565602d7
ML
9559 for_each_pipe(dev_priv, pipe)
9560 max_pixel_rate = max(intel_state->min_pixclk[pipe], max_pixel_rate);
9561
b432e5cf
VS
9562 return max_pixel_rate;
9563}
9564
9565static void broadwell_set_cdclk(struct drm_device *dev, int cdclk)
9566{
9567 struct drm_i915_private *dev_priv = dev->dev_private;
9568 uint32_t val, data;
9569 int ret;
9570
9571 if (WARN((I915_READ(LCPLL_CTL) &
9572 (LCPLL_PLL_DISABLE | LCPLL_PLL_LOCK |
9573 LCPLL_CD_CLOCK_DISABLE | LCPLL_ROOT_CD_CLOCK_DISABLE |
9574 LCPLL_CD2X_CLOCK_DISABLE | LCPLL_POWER_DOWN_ALLOW |
9575 LCPLL_CD_SOURCE_FCLK)) != LCPLL_PLL_LOCK,
9576 "trying to change cdclk frequency with cdclk not enabled\n"))
9577 return;
9578
9579 mutex_lock(&dev_priv->rps.hw_lock);
9580 ret = sandybridge_pcode_write(dev_priv,
9581 BDW_PCODE_DISPLAY_FREQ_CHANGE_REQ, 0x0);
9582 mutex_unlock(&dev_priv->rps.hw_lock);
9583 if (ret) {
9584 DRM_ERROR("failed to inform pcode about cdclk change\n");
9585 return;
9586 }
9587
9588 val = I915_READ(LCPLL_CTL);
9589 val |= LCPLL_CD_SOURCE_FCLK;
9590 I915_WRITE(LCPLL_CTL, val);
9591
5ba00178
TU
9592 if (wait_for_us(I915_READ(LCPLL_CTL) &
9593 LCPLL_CD_SOURCE_FCLK_DONE, 1))
b432e5cf
VS
9594 DRM_ERROR("Switching to FCLK failed\n");
9595
9596 val = I915_READ(LCPLL_CTL);
9597 val &= ~LCPLL_CLK_FREQ_MASK;
9598
9599 switch (cdclk) {
9600 case 450000:
9601 val |= LCPLL_CLK_FREQ_450;
9602 data = 0;
9603 break;
9604 case 540000:
9605 val |= LCPLL_CLK_FREQ_54O_BDW;
9606 data = 1;
9607 break;
9608 case 337500:
9609 val |= LCPLL_CLK_FREQ_337_5_BDW;
9610 data = 2;
9611 break;
9612 case 675000:
9613 val |= LCPLL_CLK_FREQ_675_BDW;
9614 data = 3;
9615 break;
9616 default:
9617 WARN(1, "invalid cdclk frequency\n");
9618 return;
9619 }
9620
9621 I915_WRITE(LCPLL_CTL, val);
9622
9623 val = I915_READ(LCPLL_CTL);
9624 val &= ~LCPLL_CD_SOURCE_FCLK;
9625 I915_WRITE(LCPLL_CTL, val);
9626
5ba00178
TU
9627 if (wait_for_us((I915_READ(LCPLL_CTL) &
9628 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
b432e5cf
VS
9629 DRM_ERROR("Switching back to LCPLL failed\n");
9630
9631 mutex_lock(&dev_priv->rps.hw_lock);
9632 sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ, data);
9633 mutex_unlock(&dev_priv->rps.hw_lock);
9634
7f1052a8
VS
9635 I915_WRITE(CDCLK_FREQ, DIV_ROUND_CLOSEST(cdclk, 1000) - 1);
9636
b432e5cf
VS
9637 intel_update_cdclk(dev);
9638
9639 WARN(cdclk != dev_priv->cdclk_freq,
9640 "cdclk requested %d kHz but got %d kHz\n",
9641 cdclk, dev_priv->cdclk_freq);
9642}
9643
587c7914
VS
9644static int broadwell_calc_cdclk(int max_pixclk)
9645{
9646 if (max_pixclk > 540000)
9647 return 675000;
9648 else if (max_pixclk > 450000)
9649 return 540000;
9650 else if (max_pixclk > 337500)
9651 return 450000;
9652 else
9653 return 337500;
9654}
9655
27c329ed 9656static int broadwell_modeset_calc_cdclk(struct drm_atomic_state *state)
b432e5cf 9657{
27c329ed 9658 struct drm_i915_private *dev_priv = to_i915(state->dev);
1a617b77 9659 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
27c329ed 9660 int max_pixclk = ilk_max_pixel_rate(state);
b432e5cf
VS
9661 int cdclk;
9662
9663 /*
9664 * FIXME should also account for plane ratio
9665 * once 64bpp pixel formats are supported.
9666 */
587c7914 9667 cdclk = broadwell_calc_cdclk(max_pixclk);
b432e5cf 9668
b432e5cf 9669 if (cdclk > dev_priv->max_cdclk_freq) {
63ba534e
ML
9670 DRM_DEBUG_KMS("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
9671 cdclk, dev_priv->max_cdclk_freq);
9672 return -EINVAL;
b432e5cf
VS
9673 }
9674
1a617b77
ML
9675 intel_state->cdclk = intel_state->dev_cdclk = cdclk;
9676 if (!intel_state->active_crtcs)
587c7914 9677 intel_state->dev_cdclk = broadwell_calc_cdclk(0);
b432e5cf
VS
9678
9679 return 0;
9680}
9681
27c329ed 9682static void broadwell_modeset_commit_cdclk(struct drm_atomic_state *old_state)
b432e5cf 9683{
27c329ed 9684 struct drm_device *dev = old_state->dev;
1a617b77
ML
9685 struct intel_atomic_state *old_intel_state =
9686 to_intel_atomic_state(old_state);
9687 unsigned req_cdclk = old_intel_state->dev_cdclk;
b432e5cf 9688
27c329ed 9689 broadwell_set_cdclk(dev, req_cdclk);
b432e5cf
VS
9690}
9691
c89e39f3
CT
9692static int skl_modeset_calc_cdclk(struct drm_atomic_state *state)
9693{
9694 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
9695 struct drm_i915_private *dev_priv = to_i915(state->dev);
9696 const int max_pixclk = ilk_max_pixel_rate(state);
a8ca4934 9697 int vco = intel_state->cdclk_pll_vco;
c89e39f3
CT
9698 int cdclk;
9699
9700 /*
9701 * FIXME should also account for plane ratio
9702 * once 64bpp pixel formats are supported.
9703 */
a8ca4934 9704 cdclk = skl_calc_cdclk(max_pixclk, vco);
c89e39f3
CT
9705
9706 /*
9707 * FIXME move the cdclk caclulation to
9708 * compute_config() so we can fail gracegully.
9709 */
9710 if (cdclk > dev_priv->max_cdclk_freq) {
9711 DRM_ERROR("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
9712 cdclk, dev_priv->max_cdclk_freq);
9713 cdclk = dev_priv->max_cdclk_freq;
9714 }
9715
9716 intel_state->cdclk = intel_state->dev_cdclk = cdclk;
9717 if (!intel_state->active_crtcs)
a8ca4934 9718 intel_state->dev_cdclk = skl_calc_cdclk(0, vco);
c89e39f3
CT
9719
9720 return 0;
9721}
9722
9723static void skl_modeset_commit_cdclk(struct drm_atomic_state *old_state)
9724{
1cd593e0
VS
9725 struct drm_i915_private *dev_priv = to_i915(old_state->dev);
9726 struct intel_atomic_state *intel_state = to_intel_atomic_state(old_state);
9727 unsigned int req_cdclk = intel_state->dev_cdclk;
9728 unsigned int req_vco = intel_state->cdclk_pll_vco;
c89e39f3 9729
1cd593e0 9730 skl_set_cdclk(dev_priv, req_cdclk, req_vco);
c89e39f3
CT
9731}
9732
190f68c5
ACO
9733static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
9734 struct intel_crtc_state *crtc_state)
09b4ddf9 9735{
af3997b5
MK
9736 struct intel_encoder *intel_encoder =
9737 intel_ddi_get_crtc_new_encoder(crtc_state);
9738
9739 if (intel_encoder->type != INTEL_OUTPUT_DSI) {
9740 if (!intel_ddi_pll_select(crtc, crtc_state))
9741 return -EINVAL;
9742 }
716c2e55 9743
c7653199 9744 crtc->lowfreq_avail = false;
644cef34 9745
c8f7a0db 9746 return 0;
79e53945
JB
9747}
9748
3760b59c
S
9749static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
9750 enum port port,
9751 struct intel_crtc_state *pipe_config)
9752{
8106ddbd
ACO
9753 enum intel_dpll_id id;
9754
3760b59c
S
9755 switch (port) {
9756 case PORT_A:
9757 pipe_config->ddi_pll_sel = SKL_DPLL0;
08250c4b 9758 id = DPLL_ID_SKL_DPLL0;
3760b59c
S
9759 break;
9760 case PORT_B:
9761 pipe_config->ddi_pll_sel = SKL_DPLL1;
08250c4b 9762 id = DPLL_ID_SKL_DPLL1;
3760b59c
S
9763 break;
9764 case PORT_C:
9765 pipe_config->ddi_pll_sel = SKL_DPLL2;
08250c4b 9766 id = DPLL_ID_SKL_DPLL2;
3760b59c
S
9767 break;
9768 default:
9769 DRM_ERROR("Incorrect port type\n");
8106ddbd 9770 return;
3760b59c 9771 }
8106ddbd
ACO
9772
9773 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
3760b59c
S
9774}
9775
96b7dfb7
S
9776static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
9777 enum port port,
5cec258b 9778 struct intel_crtc_state *pipe_config)
96b7dfb7 9779{
8106ddbd 9780 enum intel_dpll_id id;
a3c988ea 9781 u32 temp;
96b7dfb7
S
9782
9783 temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
9784 pipe_config->ddi_pll_sel = temp >> (port * 3 + 1);
9785
9786 switch (pipe_config->ddi_pll_sel) {
3148ade7 9787 case SKL_DPLL0:
a3c988ea
ACO
9788 id = DPLL_ID_SKL_DPLL0;
9789 break;
96b7dfb7 9790 case SKL_DPLL1:
8106ddbd 9791 id = DPLL_ID_SKL_DPLL1;
96b7dfb7
S
9792 break;
9793 case SKL_DPLL2:
8106ddbd 9794 id = DPLL_ID_SKL_DPLL2;
96b7dfb7
S
9795 break;
9796 case SKL_DPLL3:
8106ddbd 9797 id = DPLL_ID_SKL_DPLL3;
96b7dfb7 9798 break;
8106ddbd
ACO
9799 default:
9800 MISSING_CASE(pipe_config->ddi_pll_sel);
9801 return;
96b7dfb7 9802 }
8106ddbd
ACO
9803
9804 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
96b7dfb7
S
9805}
9806
7d2c8175
DL
9807static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
9808 enum port port,
5cec258b 9809 struct intel_crtc_state *pipe_config)
7d2c8175 9810{
8106ddbd
ACO
9811 enum intel_dpll_id id;
9812
7d2c8175
DL
9813 pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
9814
9815 switch (pipe_config->ddi_pll_sel) {
9816 case PORT_CLK_SEL_WRPLL1:
8106ddbd 9817 id = DPLL_ID_WRPLL1;
7d2c8175
DL
9818 break;
9819 case PORT_CLK_SEL_WRPLL2:
8106ddbd 9820 id = DPLL_ID_WRPLL2;
7d2c8175 9821 break;
00490c22 9822 case PORT_CLK_SEL_SPLL:
8106ddbd 9823 id = DPLL_ID_SPLL;
79bd23da 9824 break;
9d16da65
ACO
9825 case PORT_CLK_SEL_LCPLL_810:
9826 id = DPLL_ID_LCPLL_810;
9827 break;
9828 case PORT_CLK_SEL_LCPLL_1350:
9829 id = DPLL_ID_LCPLL_1350;
9830 break;
9831 case PORT_CLK_SEL_LCPLL_2700:
9832 id = DPLL_ID_LCPLL_2700;
9833 break;
8106ddbd
ACO
9834 default:
9835 MISSING_CASE(pipe_config->ddi_pll_sel);
9836 /* fall through */
9837 case PORT_CLK_SEL_NONE:
8106ddbd 9838 return;
7d2c8175 9839 }
8106ddbd
ACO
9840
9841 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
7d2c8175
DL
9842}
9843
cf30429e
JN
9844static bool hsw_get_transcoder_state(struct intel_crtc *crtc,
9845 struct intel_crtc_state *pipe_config,
9846 unsigned long *power_domain_mask)
9847{
9848 struct drm_device *dev = crtc->base.dev;
9849 struct drm_i915_private *dev_priv = dev->dev_private;
9850 enum intel_display_power_domain power_domain;
9851 u32 tmp;
9852
d9a7bc67
ID
9853 /*
9854 * The pipe->transcoder mapping is fixed with the exception of the eDP
9855 * transcoder handled below.
9856 */
cf30429e
JN
9857 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
9858
9859 /*
9860 * XXX: Do intel_display_power_get_if_enabled before reading this (for
9861 * consistency and less surprising code; it's in always on power).
9862 */
9863 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
9864 if (tmp & TRANS_DDI_FUNC_ENABLE) {
9865 enum pipe trans_edp_pipe;
9866 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
9867 default:
9868 WARN(1, "unknown pipe linked to edp transcoder\n");
9869 case TRANS_DDI_EDP_INPUT_A_ONOFF:
9870 case TRANS_DDI_EDP_INPUT_A_ON:
9871 trans_edp_pipe = PIPE_A;
9872 break;
9873 case TRANS_DDI_EDP_INPUT_B_ONOFF:
9874 trans_edp_pipe = PIPE_B;
9875 break;
9876 case TRANS_DDI_EDP_INPUT_C_ONOFF:
9877 trans_edp_pipe = PIPE_C;
9878 break;
9879 }
9880
9881 if (trans_edp_pipe == crtc->pipe)
9882 pipe_config->cpu_transcoder = TRANSCODER_EDP;
9883 }
9884
9885 power_domain = POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder);
9886 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
9887 return false;
9888 *power_domain_mask |= BIT(power_domain);
9889
9890 tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
9891
9892 return tmp & PIPECONF_ENABLE;
9893}
9894
4d1de975
JN
9895static bool bxt_get_dsi_transcoder_state(struct intel_crtc *crtc,
9896 struct intel_crtc_state *pipe_config,
9897 unsigned long *power_domain_mask)
9898{
9899 struct drm_device *dev = crtc->base.dev;
9900 struct drm_i915_private *dev_priv = dev->dev_private;
9901 enum intel_display_power_domain power_domain;
9902 enum port port;
9903 enum transcoder cpu_transcoder;
9904 u32 tmp;
9905
9906 pipe_config->has_dsi_encoder = false;
9907
9908 for_each_port_masked(port, BIT(PORT_A) | BIT(PORT_C)) {
9909 if (port == PORT_A)
9910 cpu_transcoder = TRANSCODER_DSI_A;
9911 else
9912 cpu_transcoder = TRANSCODER_DSI_C;
9913
9914 power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
9915 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
9916 continue;
9917 *power_domain_mask |= BIT(power_domain);
9918
db18b6a6
ID
9919 /*
9920 * The PLL needs to be enabled with a valid divider
9921 * configuration, otherwise accessing DSI registers will hang
9922 * the machine. See BSpec North Display Engine
9923 * registers/MIPI[BXT]. We can break out here early, since we
9924 * need the same DSI PLL to be enabled for both DSI ports.
9925 */
9926 if (!intel_dsi_pll_is_enabled(dev_priv))
9927 break;
9928
4d1de975
JN
9929 /* XXX: this works for video mode only */
9930 tmp = I915_READ(BXT_MIPI_PORT_CTRL(port));
9931 if (!(tmp & DPI_ENABLE))
9932 continue;
9933
9934 tmp = I915_READ(MIPI_CTRL(port));
9935 if ((tmp & BXT_PIPE_SELECT_MASK) != BXT_PIPE_SELECT(crtc->pipe))
9936 continue;
9937
9938 pipe_config->cpu_transcoder = cpu_transcoder;
9939 pipe_config->has_dsi_encoder = true;
9940 break;
9941 }
9942
9943 return pipe_config->has_dsi_encoder;
9944}
9945
26804afd 9946static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
5cec258b 9947 struct intel_crtc_state *pipe_config)
26804afd
DV
9948{
9949 struct drm_device *dev = crtc->base.dev;
9950 struct drm_i915_private *dev_priv = dev->dev_private;
d452c5b6 9951 struct intel_shared_dpll *pll;
26804afd
DV
9952 enum port port;
9953 uint32_t tmp;
9954
9955 tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
9956
9957 port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
9958
ef11bdb3 9959 if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev))
96b7dfb7 9960 skylake_get_ddi_pll(dev_priv, port, pipe_config);
3760b59c
S
9961 else if (IS_BROXTON(dev))
9962 bxt_get_ddi_pll(dev_priv, port, pipe_config);
96b7dfb7
S
9963 else
9964 haswell_get_ddi_pll(dev_priv, port, pipe_config);
9cd86933 9965
8106ddbd
ACO
9966 pll = pipe_config->shared_dpll;
9967 if (pll) {
2edd6443
ACO
9968 WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
9969 &pipe_config->dpll_hw_state));
d452c5b6
DV
9970 }
9971
26804afd
DV
9972 /*
9973 * Haswell has only FDI/PCH transcoder A. It is which is connected to
9974 * DDI E. So just check whether this pipe is wired to DDI E and whether
9975 * the PCH transcoder is on.
9976 */
ca370455
DL
9977 if (INTEL_INFO(dev)->gen < 9 &&
9978 (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
26804afd
DV
9979 pipe_config->has_pch_encoder = true;
9980
9981 tmp = I915_READ(FDI_RX_CTL(PIPE_A));
9982 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
9983 FDI_DP_PORT_WIDTH_SHIFT) + 1;
9984
9985 ironlake_get_fdi_m_n_config(crtc, pipe_config);
9986 }
9987}
9988
0e8ffe1b 9989static bool haswell_get_pipe_config(struct intel_crtc *crtc,
5cec258b 9990 struct intel_crtc_state *pipe_config)
0e8ffe1b
DV
9991{
9992 struct drm_device *dev = crtc->base.dev;
9993 struct drm_i915_private *dev_priv = dev->dev_private;
1729050e
ID
9994 enum intel_display_power_domain power_domain;
9995 unsigned long power_domain_mask;
cf30429e 9996 bool active;
0e8ffe1b 9997
1729050e
ID
9998 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
9999 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
b5482bd0 10000 return false;
1729050e
ID
10001 power_domain_mask = BIT(power_domain);
10002
8106ddbd 10003 pipe_config->shared_dpll = NULL;
c0d43d62 10004
cf30429e 10005 active = hsw_get_transcoder_state(crtc, pipe_config, &power_domain_mask);
eccb140b 10006
4d1de975
JN
10007 if (IS_BROXTON(dev_priv)) {
10008 bxt_get_dsi_transcoder_state(crtc, pipe_config,
10009 &power_domain_mask);
10010 WARN_ON(active && pipe_config->has_dsi_encoder);
10011 if (pipe_config->has_dsi_encoder)
10012 active = true;
10013 }
10014
cf30429e 10015 if (!active)
1729050e 10016 goto out;
0e8ffe1b 10017
4d1de975
JN
10018 if (!pipe_config->has_dsi_encoder) {
10019 haswell_get_ddi_port_state(crtc, pipe_config);
10020 intel_get_pipe_timings(crtc, pipe_config);
10021 }
627eb5a3 10022
bc58be60 10023 intel_get_pipe_src_size(crtc, pipe_config);
1bd1bd80 10024
05dc698c
LL
10025 pipe_config->gamma_mode =
10026 I915_READ(GAMMA_MODE(crtc->pipe)) & GAMMA_MODE_MODE_MASK;
10027
a1b2278e
CK
10028 if (INTEL_INFO(dev)->gen >= 9) {
10029 skl_init_scalers(dev, crtc, pipe_config);
10030 }
10031
af99ceda
CK
10032 if (INTEL_INFO(dev)->gen >= 9) {
10033 pipe_config->scaler_state.scaler_id = -1;
10034 pipe_config->scaler_state.scaler_users &= ~(1 << SKL_CRTC_INDEX);
10035 }
10036
1729050e
ID
10037 power_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
10038 if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
10039 power_domain_mask |= BIT(power_domain);
1c132b44 10040 if (INTEL_INFO(dev)->gen >= 9)
bd2e244f 10041 skylake_get_pfit_config(crtc, pipe_config);
ff6d9f55 10042 else
1c132b44 10043 ironlake_get_pfit_config(crtc, pipe_config);
bd2e244f 10044 }
88adfff1 10045
e59150dc
JB
10046 if (IS_HASWELL(dev))
10047 pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
10048 (I915_READ(IPS_CTL) & IPS_ENABLE);
42db64ef 10049
4d1de975
JN
10050 if (pipe_config->cpu_transcoder != TRANSCODER_EDP &&
10051 !transcoder_is_dsi(pipe_config->cpu_transcoder)) {
ebb69c95
CT
10052 pipe_config->pixel_multiplier =
10053 I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
10054 } else {
10055 pipe_config->pixel_multiplier = 1;
10056 }
6c49f241 10057
1729050e
ID
10058out:
10059 for_each_power_domain(power_domain, power_domain_mask)
10060 intel_display_power_put(dev_priv, power_domain);
10061
cf30429e 10062 return active;
0e8ffe1b
DV
10063}
10064
55a08b3f
ML
10065static void i845_update_cursor(struct drm_crtc *crtc, u32 base,
10066 const struct intel_plane_state *plane_state)
560b85bb
CW
10067{
10068 struct drm_device *dev = crtc->dev;
10069 struct drm_i915_private *dev_priv = dev->dev_private;
10070 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
dc41c154 10071 uint32_t cntl = 0, size = 0;
560b85bb 10072
55a08b3f
ML
10073 if (plane_state && plane_state->visible) {
10074 unsigned int width = plane_state->base.crtc_w;
10075 unsigned int height = plane_state->base.crtc_h;
dc41c154
VS
10076 unsigned int stride = roundup_pow_of_two(width) * 4;
10077
10078 switch (stride) {
10079 default:
10080 WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
10081 width, stride);
10082 stride = 256;
10083 /* fallthrough */
10084 case 256:
10085 case 512:
10086 case 1024:
10087 case 2048:
10088 break;
4b0e333e
CW
10089 }
10090
dc41c154
VS
10091 cntl |= CURSOR_ENABLE |
10092 CURSOR_GAMMA_ENABLE |
10093 CURSOR_FORMAT_ARGB |
10094 CURSOR_STRIDE(stride);
10095
10096 size = (height << 12) | width;
4b0e333e 10097 }
560b85bb 10098
dc41c154
VS
10099 if (intel_crtc->cursor_cntl != 0 &&
10100 (intel_crtc->cursor_base != base ||
10101 intel_crtc->cursor_size != size ||
10102 intel_crtc->cursor_cntl != cntl)) {
10103 /* On these chipsets we can only modify the base/size/stride
10104 * whilst the cursor is disabled.
10105 */
0b87c24e
VS
10106 I915_WRITE(CURCNTR(PIPE_A), 0);
10107 POSTING_READ(CURCNTR(PIPE_A));
dc41c154 10108 intel_crtc->cursor_cntl = 0;
4b0e333e 10109 }
560b85bb 10110
99d1f387 10111 if (intel_crtc->cursor_base != base) {
0b87c24e 10112 I915_WRITE(CURBASE(PIPE_A), base);
99d1f387
VS
10113 intel_crtc->cursor_base = base;
10114 }
4726e0b0 10115
dc41c154
VS
10116 if (intel_crtc->cursor_size != size) {
10117 I915_WRITE(CURSIZE, size);
10118 intel_crtc->cursor_size = size;
4b0e333e 10119 }
560b85bb 10120
4b0e333e 10121 if (intel_crtc->cursor_cntl != cntl) {
0b87c24e
VS
10122 I915_WRITE(CURCNTR(PIPE_A), cntl);
10123 POSTING_READ(CURCNTR(PIPE_A));
4b0e333e 10124 intel_crtc->cursor_cntl = cntl;
560b85bb 10125 }
560b85bb
CW
10126}
10127
55a08b3f
ML
10128static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base,
10129 const struct intel_plane_state *plane_state)
65a21cd6
JB
10130{
10131 struct drm_device *dev = crtc->dev;
10132 struct drm_i915_private *dev_priv = dev->dev_private;
10133 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10134 int pipe = intel_crtc->pipe;
663f3122 10135 uint32_t cntl = 0;
4b0e333e 10136
55a08b3f 10137 if (plane_state && plane_state->visible) {
4b0e333e 10138 cntl = MCURSOR_GAMMA_ENABLE;
55a08b3f 10139 switch (plane_state->base.crtc_w) {
4726e0b0
SK
10140 case 64:
10141 cntl |= CURSOR_MODE_64_ARGB_AX;
10142 break;
10143 case 128:
10144 cntl |= CURSOR_MODE_128_ARGB_AX;
10145 break;
10146 case 256:
10147 cntl |= CURSOR_MODE_256_ARGB_AX;
10148 break;
10149 default:
55a08b3f 10150 MISSING_CASE(plane_state->base.crtc_w);
4726e0b0 10151 return;
65a21cd6 10152 }
4b0e333e 10153 cntl |= pipe << 28; /* Connect to correct pipe */
47bf17a7 10154
fc6f93bc 10155 if (HAS_DDI(dev))
47bf17a7 10156 cntl |= CURSOR_PIPE_CSC_ENABLE;
65a21cd6 10157
55a08b3f
ML
10158 if (plane_state->base.rotation == BIT(DRM_ROTATE_180))
10159 cntl |= CURSOR_ROTATE_180;
10160 }
4398ad45 10161
4b0e333e
CW
10162 if (intel_crtc->cursor_cntl != cntl) {
10163 I915_WRITE(CURCNTR(pipe), cntl);
10164 POSTING_READ(CURCNTR(pipe));
10165 intel_crtc->cursor_cntl = cntl;
65a21cd6 10166 }
4b0e333e 10167
65a21cd6 10168 /* and commit changes on next vblank */
5efb3e28
VS
10169 I915_WRITE(CURBASE(pipe), base);
10170 POSTING_READ(CURBASE(pipe));
99d1f387
VS
10171
10172 intel_crtc->cursor_base = base;
65a21cd6
JB
10173}
10174
cda4b7d3 10175/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
6b383a7f 10176static void intel_crtc_update_cursor(struct drm_crtc *crtc,
55a08b3f 10177 const struct intel_plane_state *plane_state)
cda4b7d3
CW
10178{
10179 struct drm_device *dev = crtc->dev;
10180 struct drm_i915_private *dev_priv = dev->dev_private;
10181 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10182 int pipe = intel_crtc->pipe;
55a08b3f
ML
10183 u32 base = intel_crtc->cursor_addr;
10184 u32 pos = 0;
cda4b7d3 10185
55a08b3f
ML
10186 if (plane_state) {
10187 int x = plane_state->base.crtc_x;
10188 int y = plane_state->base.crtc_y;
cda4b7d3 10189
55a08b3f
ML
10190 if (x < 0) {
10191 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
10192 x = -x;
10193 }
10194 pos |= x << CURSOR_X_SHIFT;
cda4b7d3 10195
55a08b3f
ML
10196 if (y < 0) {
10197 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
10198 y = -y;
10199 }
10200 pos |= y << CURSOR_Y_SHIFT;
10201
10202 /* ILK+ do this automagically */
10203 if (HAS_GMCH_DISPLAY(dev) &&
10204 plane_state->base.rotation == BIT(DRM_ROTATE_180)) {
10205 base += (plane_state->base.crtc_h *
10206 plane_state->base.crtc_w - 1) * 4;
10207 }
cda4b7d3 10208 }
cda4b7d3 10209
5efb3e28
VS
10210 I915_WRITE(CURPOS(pipe), pos);
10211
8ac54669 10212 if (IS_845G(dev) || IS_I865G(dev))
55a08b3f 10213 i845_update_cursor(crtc, base, plane_state);
5efb3e28 10214 else
55a08b3f 10215 i9xx_update_cursor(crtc, base, plane_state);
cda4b7d3
CW
10216}
10217
dc41c154
VS
10218static bool cursor_size_ok(struct drm_device *dev,
10219 uint32_t width, uint32_t height)
10220{
10221 if (width == 0 || height == 0)
10222 return false;
10223
10224 /*
10225 * 845g/865g are special in that they are only limited by
10226 * the width of their cursors, the height is arbitrary up to
10227 * the precision of the register. Everything else requires
10228 * square cursors, limited to a few power-of-two sizes.
10229 */
10230 if (IS_845G(dev) || IS_I865G(dev)) {
10231 if ((width & 63) != 0)
10232 return false;
10233
10234 if (width > (IS_845G(dev) ? 64 : 512))
10235 return false;
10236
10237 if (height > 1023)
10238 return false;
10239 } else {
10240 switch (width | height) {
10241 case 256:
10242 case 128:
10243 if (IS_GEN2(dev))
10244 return false;
10245 case 64:
10246 break;
10247 default:
10248 return false;
10249 }
10250 }
10251
10252 return true;
10253}
10254
79e53945
JB
10255/* VESA 640x480x72Hz mode to set on the pipe */
10256static struct drm_display_mode load_detect_mode = {
10257 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
10258 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
10259};
10260
a8bb6818
DV
10261struct drm_framebuffer *
10262__intel_framebuffer_create(struct drm_device *dev,
10263 struct drm_mode_fb_cmd2 *mode_cmd,
10264 struct drm_i915_gem_object *obj)
d2dff872
CW
10265{
10266 struct intel_framebuffer *intel_fb;
10267 int ret;
10268
10269 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
dcb1394e 10270 if (!intel_fb)
d2dff872 10271 return ERR_PTR(-ENOMEM);
d2dff872
CW
10272
10273 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
dd4916c5
DV
10274 if (ret)
10275 goto err;
d2dff872
CW
10276
10277 return &intel_fb->base;
dcb1394e 10278
dd4916c5 10279err:
dd4916c5 10280 kfree(intel_fb);
dd4916c5 10281 return ERR_PTR(ret);
d2dff872
CW
10282}
10283
b5ea642a 10284static struct drm_framebuffer *
a8bb6818
DV
10285intel_framebuffer_create(struct drm_device *dev,
10286 struct drm_mode_fb_cmd2 *mode_cmd,
10287 struct drm_i915_gem_object *obj)
10288{
10289 struct drm_framebuffer *fb;
10290 int ret;
10291
10292 ret = i915_mutex_lock_interruptible(dev);
10293 if (ret)
10294 return ERR_PTR(ret);
10295 fb = __intel_framebuffer_create(dev, mode_cmd, obj);
10296 mutex_unlock(&dev->struct_mutex);
10297
10298 return fb;
10299}
10300
d2dff872
CW
10301static u32
10302intel_framebuffer_pitch_for_width(int width, int bpp)
10303{
10304 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
10305 return ALIGN(pitch, 64);
10306}
10307
10308static u32
10309intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
10310{
10311 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
1267a26b 10312 return PAGE_ALIGN(pitch * mode->vdisplay);
d2dff872
CW
10313}
10314
10315static struct drm_framebuffer *
10316intel_framebuffer_create_for_mode(struct drm_device *dev,
10317 struct drm_display_mode *mode,
10318 int depth, int bpp)
10319{
dcb1394e 10320 struct drm_framebuffer *fb;
d2dff872 10321 struct drm_i915_gem_object *obj;
0fed39bd 10322 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
d2dff872 10323
d37cd8a8 10324 obj = i915_gem_object_create(dev,
d2dff872 10325 intel_framebuffer_size_for_mode(mode, bpp));
fe3db79b
CW
10326 if (IS_ERR(obj))
10327 return ERR_CAST(obj);
d2dff872
CW
10328
10329 mode_cmd.width = mode->hdisplay;
10330 mode_cmd.height = mode->vdisplay;
308e5bcb
JB
10331 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
10332 bpp);
5ca0c34a 10333 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
d2dff872 10334
dcb1394e
LW
10335 fb = intel_framebuffer_create(dev, &mode_cmd, obj);
10336 if (IS_ERR(fb))
10337 drm_gem_object_unreference_unlocked(&obj->base);
10338
10339 return fb;
d2dff872
CW
10340}
10341
10342static struct drm_framebuffer *
10343mode_fits_in_fbdev(struct drm_device *dev,
10344 struct drm_display_mode *mode)
10345{
0695726e 10346#ifdef CONFIG_DRM_FBDEV_EMULATION
d2dff872
CW
10347 struct drm_i915_private *dev_priv = dev->dev_private;
10348 struct drm_i915_gem_object *obj;
10349 struct drm_framebuffer *fb;
10350
4c0e5528 10351 if (!dev_priv->fbdev)
d2dff872
CW
10352 return NULL;
10353
4c0e5528 10354 if (!dev_priv->fbdev->fb)
d2dff872
CW
10355 return NULL;
10356
4c0e5528
DV
10357 obj = dev_priv->fbdev->fb->obj;
10358 BUG_ON(!obj);
10359
8bcd4553 10360 fb = &dev_priv->fbdev->fb->base;
01f2c773
VS
10361 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
10362 fb->bits_per_pixel))
d2dff872
CW
10363 return NULL;
10364
01f2c773 10365 if (obj->base.size < mode->vdisplay * fb->pitches[0])
d2dff872
CW
10366 return NULL;
10367
edde3617 10368 drm_framebuffer_reference(fb);
d2dff872 10369 return fb;
4520f53a
DV
10370#else
10371 return NULL;
10372#endif
d2dff872
CW
10373}
10374
d3a40d1b
ACO
10375static int intel_modeset_setup_plane_state(struct drm_atomic_state *state,
10376 struct drm_crtc *crtc,
10377 struct drm_display_mode *mode,
10378 struct drm_framebuffer *fb,
10379 int x, int y)
10380{
10381 struct drm_plane_state *plane_state;
10382 int hdisplay, vdisplay;
10383 int ret;
10384
10385 plane_state = drm_atomic_get_plane_state(state, crtc->primary);
10386 if (IS_ERR(plane_state))
10387 return PTR_ERR(plane_state);
10388
10389 if (mode)
10390 drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
10391 else
10392 hdisplay = vdisplay = 0;
10393
10394 ret = drm_atomic_set_crtc_for_plane(plane_state, fb ? crtc : NULL);
10395 if (ret)
10396 return ret;
10397 drm_atomic_set_fb_for_plane(plane_state, fb);
10398 plane_state->crtc_x = 0;
10399 plane_state->crtc_y = 0;
10400 plane_state->crtc_w = hdisplay;
10401 plane_state->crtc_h = vdisplay;
10402 plane_state->src_x = x << 16;
10403 plane_state->src_y = y << 16;
10404 plane_state->src_w = hdisplay << 16;
10405 plane_state->src_h = vdisplay << 16;
10406
10407 return 0;
10408}
10409
d2434ab7 10410bool intel_get_load_detect_pipe(struct drm_connector *connector,
7173188d 10411 struct drm_display_mode *mode,
51fd371b
RC
10412 struct intel_load_detect_pipe *old,
10413 struct drm_modeset_acquire_ctx *ctx)
79e53945
JB
10414{
10415 struct intel_crtc *intel_crtc;
d2434ab7
DV
10416 struct intel_encoder *intel_encoder =
10417 intel_attached_encoder(connector);
79e53945 10418 struct drm_crtc *possible_crtc;
4ef69c7a 10419 struct drm_encoder *encoder = &intel_encoder->base;
79e53945
JB
10420 struct drm_crtc *crtc = NULL;
10421 struct drm_device *dev = encoder->dev;
94352cf9 10422 struct drm_framebuffer *fb;
51fd371b 10423 struct drm_mode_config *config = &dev->mode_config;
edde3617 10424 struct drm_atomic_state *state = NULL, *restore_state = NULL;
944b0c76 10425 struct drm_connector_state *connector_state;
4be07317 10426 struct intel_crtc_state *crtc_state;
51fd371b 10427 int ret, i = -1;
79e53945 10428
d2dff872 10429 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
c23cc417 10430 connector->base.id, connector->name,
8e329a03 10431 encoder->base.id, encoder->name);
d2dff872 10432
edde3617
ML
10433 old->restore_state = NULL;
10434
51fd371b
RC
10435retry:
10436 ret = drm_modeset_lock(&config->connection_mutex, ctx);
10437 if (ret)
ad3c558f 10438 goto fail;
6e9f798d 10439
79e53945
JB
10440 /*
10441 * Algorithm gets a little messy:
7a5e4805 10442 *
79e53945
JB
10443 * - if the connector already has an assigned crtc, use it (but make
10444 * sure it's on first)
7a5e4805 10445 *
79e53945
JB
10446 * - try to find the first unused crtc that can drive this connector,
10447 * and use that if we find one
79e53945
JB
10448 */
10449
10450 /* See if we already have a CRTC for this connector */
edde3617
ML
10451 if (connector->state->crtc) {
10452 crtc = connector->state->crtc;
8261b191 10453
51fd371b 10454 ret = drm_modeset_lock(&crtc->mutex, ctx);
4d02e2de 10455 if (ret)
ad3c558f 10456 goto fail;
8261b191
CW
10457
10458 /* Make sure the crtc and connector are running */
edde3617 10459 goto found;
79e53945
JB
10460 }
10461
10462 /* Find an unused one (if possible) */
70e1e0ec 10463 for_each_crtc(dev, possible_crtc) {
79e53945
JB
10464 i++;
10465 if (!(encoder->possible_crtcs & (1 << i)))
10466 continue;
edde3617
ML
10467
10468 ret = drm_modeset_lock(&possible_crtc->mutex, ctx);
10469 if (ret)
10470 goto fail;
10471
10472 if (possible_crtc->state->enable) {
10473 drm_modeset_unlock(&possible_crtc->mutex);
a459249c 10474 continue;
edde3617 10475 }
a459249c
VS
10476
10477 crtc = possible_crtc;
10478 break;
79e53945
JB
10479 }
10480
10481 /*
10482 * If we didn't find an unused CRTC, don't use any.
10483 */
10484 if (!crtc) {
7173188d 10485 DRM_DEBUG_KMS("no pipe available for load-detect\n");
ad3c558f 10486 goto fail;
79e53945
JB
10487 }
10488
edde3617
ML
10489found:
10490 intel_crtc = to_intel_crtc(crtc);
10491
4d02e2de
DV
10492 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
10493 if (ret)
ad3c558f 10494 goto fail;
79e53945 10495
83a57153 10496 state = drm_atomic_state_alloc(dev);
edde3617
ML
10497 restore_state = drm_atomic_state_alloc(dev);
10498 if (!state || !restore_state) {
10499 ret = -ENOMEM;
10500 goto fail;
10501 }
83a57153
ACO
10502
10503 state->acquire_ctx = ctx;
edde3617 10504 restore_state->acquire_ctx = ctx;
83a57153 10505
944b0c76
ACO
10506 connector_state = drm_atomic_get_connector_state(state, connector);
10507 if (IS_ERR(connector_state)) {
10508 ret = PTR_ERR(connector_state);
10509 goto fail;
10510 }
10511
edde3617
ML
10512 ret = drm_atomic_set_crtc_for_connector(connector_state, crtc);
10513 if (ret)
10514 goto fail;
944b0c76 10515
4be07317
ACO
10516 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
10517 if (IS_ERR(crtc_state)) {
10518 ret = PTR_ERR(crtc_state);
10519 goto fail;
10520 }
10521
49d6fa21 10522 crtc_state->base.active = crtc_state->base.enable = true;
4be07317 10523
6492711d
CW
10524 if (!mode)
10525 mode = &load_detect_mode;
79e53945 10526
d2dff872
CW
10527 /* We need a framebuffer large enough to accommodate all accesses
10528 * that the plane may generate whilst we perform load detection.
10529 * We can not rely on the fbcon either being present (we get called
10530 * during its initialisation to detect all boot displays, or it may
10531 * not even exist) or that it is large enough to satisfy the
10532 * requested mode.
10533 */
94352cf9
DV
10534 fb = mode_fits_in_fbdev(dev, mode);
10535 if (fb == NULL) {
d2dff872 10536 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
94352cf9 10537 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
d2dff872
CW
10538 } else
10539 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
94352cf9 10540 if (IS_ERR(fb)) {
d2dff872 10541 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
412b61d8 10542 goto fail;
79e53945 10543 }
79e53945 10544
d3a40d1b
ACO
10545 ret = intel_modeset_setup_plane_state(state, crtc, mode, fb, 0, 0);
10546 if (ret)
10547 goto fail;
10548
edde3617
ML
10549 drm_framebuffer_unreference(fb);
10550
10551 ret = drm_atomic_set_mode_for_crtc(&crtc_state->base, mode);
10552 if (ret)
10553 goto fail;
10554
10555 ret = PTR_ERR_OR_ZERO(drm_atomic_get_connector_state(restore_state, connector));
10556 if (!ret)
10557 ret = PTR_ERR_OR_ZERO(drm_atomic_get_crtc_state(restore_state, crtc));
10558 if (!ret)
10559 ret = PTR_ERR_OR_ZERO(drm_atomic_get_plane_state(restore_state, crtc->primary));
10560 if (ret) {
10561 DRM_DEBUG_KMS("Failed to create a copy of old state to restore: %i\n", ret);
10562 goto fail;
10563 }
8c7b5ccb 10564
3ba86073
ML
10565 ret = drm_atomic_commit(state);
10566 if (ret) {
6492711d 10567 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
412b61d8 10568 goto fail;
79e53945 10569 }
edde3617
ML
10570
10571 old->restore_state = restore_state;
7173188d 10572
79e53945 10573 /* let the connector get through one full cycle before testing */
9d0498a2 10574 intel_wait_for_vblank(dev, intel_crtc->pipe);
7173188d 10575 return true;
412b61d8 10576
ad3c558f 10577fail:
e5d958ef 10578 drm_atomic_state_free(state);
edde3617
ML
10579 drm_atomic_state_free(restore_state);
10580 restore_state = state = NULL;
83a57153 10581
51fd371b
RC
10582 if (ret == -EDEADLK) {
10583 drm_modeset_backoff(ctx);
10584 goto retry;
10585 }
10586
412b61d8 10587 return false;
79e53945
JB
10588}
10589
d2434ab7 10590void intel_release_load_detect_pipe(struct drm_connector *connector,
49172fee
ACO
10591 struct intel_load_detect_pipe *old,
10592 struct drm_modeset_acquire_ctx *ctx)
79e53945 10593{
d2434ab7
DV
10594 struct intel_encoder *intel_encoder =
10595 intel_attached_encoder(connector);
4ef69c7a 10596 struct drm_encoder *encoder = &intel_encoder->base;
edde3617 10597 struct drm_atomic_state *state = old->restore_state;
d3a40d1b 10598 int ret;
79e53945 10599
d2dff872 10600 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
c23cc417 10601 connector->base.id, connector->name,
8e329a03 10602 encoder->base.id, encoder->name);
d2dff872 10603
edde3617 10604 if (!state)
0622a53c 10605 return;
79e53945 10606
edde3617
ML
10607 ret = drm_atomic_commit(state);
10608 if (ret) {
10609 DRM_DEBUG_KMS("Couldn't release load detect pipe: %i\n", ret);
10610 drm_atomic_state_free(state);
10611 }
79e53945
JB
10612}
10613
da4a1efa 10614static int i9xx_pll_refclk(struct drm_device *dev,
5cec258b 10615 const struct intel_crtc_state *pipe_config)
da4a1efa
VS
10616{
10617 struct drm_i915_private *dev_priv = dev->dev_private;
10618 u32 dpll = pipe_config->dpll_hw_state.dpll;
10619
10620 if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
e91e941b 10621 return dev_priv->vbt.lvds_ssc_freq;
da4a1efa
VS
10622 else if (HAS_PCH_SPLIT(dev))
10623 return 120000;
10624 else if (!IS_GEN2(dev))
10625 return 96000;
10626 else
10627 return 48000;
10628}
10629
79e53945 10630/* Returns the clock of the currently programmed mode of the given pipe. */
f1f644dc 10631static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 10632 struct intel_crtc_state *pipe_config)
79e53945 10633{
f1f644dc 10634 struct drm_device *dev = crtc->base.dev;
79e53945 10635 struct drm_i915_private *dev_priv = dev->dev_private;
f1f644dc 10636 int pipe = pipe_config->cpu_transcoder;
293623f7 10637 u32 dpll = pipe_config->dpll_hw_state.dpll;
79e53945 10638 u32 fp;
9e2c8475 10639 struct dpll clock;
dccbea3b 10640 int port_clock;
da4a1efa 10641 int refclk = i9xx_pll_refclk(dev, pipe_config);
79e53945
JB
10642
10643 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
293623f7 10644 fp = pipe_config->dpll_hw_state.fp0;
79e53945 10645 else
293623f7 10646 fp = pipe_config->dpll_hw_state.fp1;
79e53945
JB
10647
10648 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
f2b115e6
AJ
10649 if (IS_PINEVIEW(dev)) {
10650 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
10651 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
2177832f
SL
10652 } else {
10653 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
10654 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
10655 }
10656
a6c45cf0 10657 if (!IS_GEN2(dev)) {
f2b115e6
AJ
10658 if (IS_PINEVIEW(dev))
10659 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
10660 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
2177832f
SL
10661 else
10662 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
79e53945
JB
10663 DPLL_FPA01_P1_POST_DIV_SHIFT);
10664
10665 switch (dpll & DPLL_MODE_MASK) {
10666 case DPLLB_MODE_DAC_SERIAL:
10667 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
10668 5 : 10;
10669 break;
10670 case DPLLB_MODE_LVDS:
10671 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
10672 7 : 14;
10673 break;
10674 default:
28c97730 10675 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
79e53945 10676 "mode\n", (int)(dpll & DPLL_MODE_MASK));
f1f644dc 10677 return;
79e53945
JB
10678 }
10679
ac58c3f0 10680 if (IS_PINEVIEW(dev))
dccbea3b 10681 port_clock = pnv_calc_dpll_params(refclk, &clock);
ac58c3f0 10682 else
dccbea3b 10683 port_clock = i9xx_calc_dpll_params(refclk, &clock);
79e53945 10684 } else {
0fb58223 10685 u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
b1c560d1 10686 bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
79e53945
JB
10687
10688 if (is_lvds) {
10689 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
10690 DPLL_FPA01_P1_POST_DIV_SHIFT);
b1c560d1
VS
10691
10692 if (lvds & LVDS_CLKB_POWER_UP)
10693 clock.p2 = 7;
10694 else
10695 clock.p2 = 14;
79e53945
JB
10696 } else {
10697 if (dpll & PLL_P1_DIVIDE_BY_TWO)
10698 clock.p1 = 2;
10699 else {
10700 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
10701 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
10702 }
10703 if (dpll & PLL_P2_DIVIDE_BY_4)
10704 clock.p2 = 4;
10705 else
10706 clock.p2 = 2;
79e53945 10707 }
da4a1efa 10708
dccbea3b 10709 port_clock = i9xx_calc_dpll_params(refclk, &clock);
79e53945
JB
10710 }
10711
18442d08
VS
10712 /*
10713 * This value includes pixel_multiplier. We will use
241bfc38 10714 * port_clock to compute adjusted_mode.crtc_clock in the
18442d08
VS
10715 * encoder's get_config() function.
10716 */
dccbea3b 10717 pipe_config->port_clock = port_clock;
f1f644dc
JB
10718}
10719
6878da05
VS
10720int intel_dotclock_calculate(int link_freq,
10721 const struct intel_link_m_n *m_n)
f1f644dc 10722{
f1f644dc
JB
10723 /*
10724 * The calculation for the data clock is:
1041a02f 10725 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
f1f644dc 10726 * But we want to avoid losing precison if possible, so:
1041a02f 10727 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
f1f644dc
JB
10728 *
10729 * and the link clock is simpler:
1041a02f 10730 * link_clock = (m * link_clock) / n
f1f644dc
JB
10731 */
10732
6878da05
VS
10733 if (!m_n->link_n)
10734 return 0;
f1f644dc 10735
6878da05
VS
10736 return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
10737}
f1f644dc 10738
18442d08 10739static void ironlake_pch_clock_get(struct intel_crtc *crtc,
5cec258b 10740 struct intel_crtc_state *pipe_config)
6878da05 10741{
e3b247da 10742 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
79e53945 10743
18442d08
VS
10744 /* read out port_clock from the DPLL */
10745 i9xx_crtc_clock_get(crtc, pipe_config);
f1f644dc 10746
f1f644dc 10747 /*
e3b247da
VS
10748 * In case there is an active pipe without active ports,
10749 * we may need some idea for the dotclock anyway.
10750 * Calculate one based on the FDI configuration.
79e53945 10751 */
2d112de7 10752 pipe_config->base.adjusted_mode.crtc_clock =
21a727b3 10753 intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
18442d08 10754 &pipe_config->fdi_m_n);
79e53945
JB
10755}
10756
10757/** Returns the currently programmed mode of the given pipe. */
10758struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
10759 struct drm_crtc *crtc)
10760{
548f245b 10761 struct drm_i915_private *dev_priv = dev->dev_private;
79e53945 10762 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6e3c9717 10763 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
79e53945 10764 struct drm_display_mode *mode;
3f36b937 10765 struct intel_crtc_state *pipe_config;
fe2b8f9d
PZ
10766 int htot = I915_READ(HTOTAL(cpu_transcoder));
10767 int hsync = I915_READ(HSYNC(cpu_transcoder));
10768 int vtot = I915_READ(VTOTAL(cpu_transcoder));
10769 int vsync = I915_READ(VSYNC(cpu_transcoder));
293623f7 10770 enum pipe pipe = intel_crtc->pipe;
79e53945
JB
10771
10772 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
10773 if (!mode)
10774 return NULL;
10775
3f36b937
TU
10776 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
10777 if (!pipe_config) {
10778 kfree(mode);
10779 return NULL;
10780 }
10781
f1f644dc
JB
10782 /*
10783 * Construct a pipe_config sufficient for getting the clock info
10784 * back out of crtc_clock_get.
10785 *
10786 * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
10787 * to use a real value here instead.
10788 */
3f36b937
TU
10789 pipe_config->cpu_transcoder = (enum transcoder) pipe;
10790 pipe_config->pixel_multiplier = 1;
10791 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(pipe));
10792 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(pipe));
10793 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(pipe));
10794 i9xx_crtc_clock_get(intel_crtc, pipe_config);
10795
10796 mode->clock = pipe_config->port_clock / pipe_config->pixel_multiplier;
79e53945
JB
10797 mode->hdisplay = (htot & 0xffff) + 1;
10798 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
10799 mode->hsync_start = (hsync & 0xffff) + 1;
10800 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
10801 mode->vdisplay = (vtot & 0xffff) + 1;
10802 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
10803 mode->vsync_start = (vsync & 0xffff) + 1;
10804 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
10805
10806 drm_mode_set_name(mode);
79e53945 10807
3f36b937
TU
10808 kfree(pipe_config);
10809
79e53945
JB
10810 return mode;
10811}
10812
7d993739 10813void intel_mark_busy(struct drm_i915_private *dev_priv)
f047e395 10814{
f62a0076
CW
10815 if (dev_priv->mm.busy)
10816 return;
10817
43694d69 10818 intel_runtime_pm_get(dev_priv);
c67a470b 10819 i915_update_gfx_val(dev_priv);
7d993739 10820 if (INTEL_GEN(dev_priv) >= 6)
43cf3bf0 10821 gen6_rps_busy(dev_priv);
f62a0076 10822 dev_priv->mm.busy = true;
f047e395
CW
10823}
10824
7d993739 10825void intel_mark_idle(struct drm_i915_private *dev_priv)
652c393a 10826{
f62a0076
CW
10827 if (!dev_priv->mm.busy)
10828 return;
10829
10830 dev_priv->mm.busy = false;
10831
7d993739
TU
10832 if (INTEL_GEN(dev_priv) >= 6)
10833 gen6_rps_idle(dev_priv);
bb4cdd53 10834
43694d69 10835 intel_runtime_pm_put(dev_priv);
652c393a
JB
10836}
10837
a6747b73 10838void intel_free_flip_work(struct intel_flip_work *work)
03f476e1
ML
10839{
10840 kfree(work->old_connector_state);
10841 kfree(work->new_connector_state);
10842 kfree(work);
10843}
10844
79e53945
JB
10845static void intel_crtc_destroy(struct drm_crtc *crtc)
10846{
10847 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
67e77c5a 10848 struct drm_device *dev = crtc->dev;
51cbaf01 10849 struct intel_flip_work *work;
67e77c5a 10850
5e2d7afc 10851 spin_lock_irq(&dev->event_lock);
6885843a
ML
10852 while (!list_empty(&intel_crtc->flip_work)) {
10853 work = list_first_entry(&intel_crtc->flip_work,
10854 struct intel_flip_work, head);
10855 list_del_init(&work->head);
10856 spin_unlock_irq(&dev->event_lock);
67e77c5a 10857
51cbaf01
ML
10858 cancel_work_sync(&work->mmio_work);
10859 cancel_work_sync(&work->unpin_work);
03f476e1 10860 intel_free_flip_work(work);
6885843a
ML
10861
10862 spin_lock_irq(&dev->event_lock);
67e77c5a 10863 }
6885843a 10864 spin_unlock_irq(&dev->event_lock);
79e53945
JB
10865
10866 drm_crtc_cleanup(crtc);
67e77c5a 10867
79e53945
JB
10868 kfree(intel_crtc);
10869}
10870
143f73b3
ML
10871static void intel_crtc_post_flip_update(struct intel_flip_work *work,
10872 struct drm_crtc *crtc)
10873{
10874 struct intel_crtc_state *crtc_state = work->new_crtc_state;
10875 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10876
10877 if (crtc_state->disable_cxsr)
10878 intel_crtc->wm.cxsr_allowed = true;
10879
10880 if (crtc_state->update_wm_post && crtc_state->base.active)
10881 intel_update_watermarks(crtc);
10882
10883 if (work->num_planes > 0 &&
10884 work->old_plane_state[0]->base.plane == crtc->primary) {
10885 struct intel_plane_state *plane_state =
10886 work->new_plane_state[0];
10887
10888 if (plane_state->visible &&
10889 (needs_modeset(&crtc_state->base) ||
10890 !work->old_plane_state[0]->visible))
10891 intel_post_enable_primary(crtc);
10892 }
10893}
10894
6b95a207
KH
10895static void intel_unpin_work_fn(struct work_struct *__work)
10896{
51cbaf01
ML
10897 struct intel_flip_work *work =
10898 container_of(__work, struct intel_flip_work, unpin_work);
143f73b3
ML
10899 struct drm_crtc *crtc = work->old_crtc_state->base.crtc;
10900 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10901 struct drm_device *dev = crtc->dev;
10902 struct drm_i915_private *dev_priv = dev->dev_private;
10903 int i;
6b95a207 10904
143f73b3
ML
10905 if (work->fb_bits)
10906 intel_frontbuffer_flip_complete(dev, work->fb_bits);
51cbaf01 10907
143f73b3
ML
10908 /*
10909 * Unless work->can_async_unpin is false, there's no way to ensure
10910 * that work->new_crtc_state contains valid memory during unpin
10911 * because intel_atomic_commit may free it before this runs.
10912 */
a6747b73 10913 if (!work->can_async_unpin) {
143f73b3
ML
10914 intel_crtc_post_flip_update(work, crtc);
10915
a6747b73
ML
10916 if (dev_priv->display.optimize_watermarks)
10917 dev_priv->display.optimize_watermarks(work->new_crtc_state);
10918 }
10919
143f73b3
ML
10920 if (work->fb_bits & to_intel_plane(crtc->primary)->frontbuffer_bit)
10921 intel_fbc_post_update(intel_crtc);
10922
10923 if (work->put_power_domains)
10924 modeset_put_power_domains(dev_priv, work->put_power_domains);
10925
10926 /* Make sure mmio work is completely finished before freeing all state here. */
10927 flush_work(&work->mmio_work);
10928
03f476e1
ML
10929 if (!work->can_async_unpin &&
10930 (work->new_crtc_state->update_pipe ||
10931 needs_modeset(&work->new_crtc_state->base))) {
143f73b3
ML
10932 /* This must be called before work is unpinned for serialization. */
10933 intel_modeset_verify_crtc(crtc, &work->old_crtc_state->base,
10934 &work->new_crtc_state->base);
10935
03f476e1
ML
10936 for (i = 0; i < work->num_new_connectors; i++) {
10937 struct drm_connector_state *conn_state =
10938 work->new_connector_state[i];
10939 struct drm_connector *con = conn_state->connector;
10940
a6747b73
ML
10941 WARN_ON(!con);
10942
03f476e1
ML
10943 intel_connector_verify_state(to_intel_connector(con),
10944 conn_state);
10945 }
10946 }
10947
10948 for (i = 0; i < work->num_old_connectors; i++) {
10949 struct drm_connector_state *old_con_state =
10950 work->old_connector_state[i];
10951 struct drm_connector *con =
10952 old_con_state->connector;
10953
10954 con->funcs->atomic_destroy_state(con, old_con_state);
10955 }
10956
143f73b3
ML
10957 if (!work->can_async_unpin || !list_empty(&work->head)) {
10958 spin_lock_irq(&dev->event_lock);
10959 WARN(list_empty(&work->head) != work->can_async_unpin,
10960 "[CRTC:%i] Pin work %p async %i with %i planes, active %i -> %i ms %i\n",
10961 crtc->base.id, work, work->can_async_unpin, work->num_planes,
10962 work->old_crtc_state->base.active, work->new_crtc_state->base.active,
10963 needs_modeset(&work->new_crtc_state->base));
10964
10965 if (!list_empty(&work->head))
10966 list_del(&work->head);
10967
10968 wake_up_all(&dev_priv->pending_flip_queue);
10969 spin_unlock_irq(&dev->event_lock);
10970 }
10971
a6747b73
ML
10972 /* New crtc_state freed? */
10973 if (work->free_new_crtc_state)
10974 intel_crtc_destroy_state(crtc, &work->new_crtc_state->base);
10975
143f73b3 10976 intel_crtc_destroy_state(crtc, &work->old_crtc_state->base);
d9e86c0e 10977
143f73b3
ML
10978 for (i = 0; i < work->num_planes; i++) {
10979 struct intel_plane_state *old_plane_state =
10980 work->old_plane_state[i];
10981 struct drm_framebuffer *old_fb = old_plane_state->base.fb;
10982 struct drm_plane *plane = old_plane_state->base.plane;
10983 struct drm_i915_gem_request *req;
10984
10985 req = old_plane_state->wait_req;
10986 old_plane_state->wait_req = NULL;
a6747b73
ML
10987 if (req)
10988 i915_gem_request_unreference(req);
143f73b3
ML
10989
10990 fence_put(old_plane_state->base.fence);
10991 old_plane_state->base.fence = NULL;
10992
10993 if (old_fb &&
10994 (plane->type != DRM_PLANE_TYPE_CURSOR ||
10995 !INTEL_INFO(dev_priv)->cursor_needs_physical)) {
10996 mutex_lock(&dev->struct_mutex);
10997 intel_unpin_fb_obj(old_fb, old_plane_state->base.rotation);
10998 mutex_unlock(&dev->struct_mutex);
10999 }
b4a98e57 11000
143f73b3
ML
11001 intel_plane_destroy_state(plane, &old_plane_state->base);
11002 }
f99d7069 11003
143f73b3
ML
11004 if (!WARN_ON(atomic_read(&intel_crtc->unpin_work_count) == 0))
11005 atomic_dec(&intel_crtc->unpin_work_count);
b4a98e57 11006
03f476e1 11007 intel_free_flip_work(work);
6b95a207
KH
11008}
11009
51cbaf01
ML
11010
11011static bool pageflip_finished(struct intel_crtc *crtc,
11012 struct intel_flip_work *work)
11013{
11014 if (!atomic_read(&work->pending))
11015 return false;
11016
11017 smp_rmb();
11018
51cbaf01 11019 /*
8dd634d9
ML
11020 * MMIO work completes when vblank is different from
11021 * flip_queued_vblank.
51cbaf01 11022 */
8dd634d9 11023 return intel_crtc_get_vblank_counter(crtc) != work->flip_queued_vblank;
75f7f3ec
VS
11024}
11025
51cbaf01 11026void intel_finish_page_flip_mmio(struct drm_i915_private *dev_priv, int pipe)
6b95a207 11027{
91d14251 11028 struct drm_device *dev = dev_priv->dev;
5251f04e
ML
11029 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
11030 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
51cbaf01 11031 struct intel_flip_work *work;
6b95a207
KH
11032 unsigned long flags;
11033
5251f04e
ML
11034 /* Ignore early vblank irqs */
11035 if (!crtc)
11036 return;
f326038a
DV
11037
11038 /*
11039 * This is called both by irq handlers and the reset code (to complete
11040 * lost pageflips) so needs the full irqsave spinlocks.
e7d841ca 11041 */
6b95a207 11042 spin_lock_irqsave(&dev->event_lock, flags);
6885843a
ML
11043 while (!list_empty(&intel_crtc->flip_work)) {
11044 work = list_first_entry(&intel_crtc->flip_work,
11045 struct intel_flip_work,
11046 head);
5251f04e 11047
143f73b3
ML
11048 if (!pageflip_finished(intel_crtc, work) ||
11049 work_busy(&work->unpin_work))
6885843a 11050 break;
5251f04e 11051
6885843a
ML
11052 page_flip_completed(intel_crtc, work);
11053 }
6b95a207
KH
11054 spin_unlock_irqrestore(&dev->event_lock, flags);
11055}
11056
51cbaf01 11057static void intel_mmio_flip_work_func(struct work_struct *w)
84c33a64 11058{
51cbaf01
ML
11059 struct intel_flip_work *work =
11060 container_of(w, struct intel_flip_work, mmio_work);
143f73b3
ML
11061 struct drm_crtc *crtc = work->old_crtc_state->base.crtc;
11062 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11063 struct intel_crtc_state *crtc_state = work->new_crtc_state;
11064 struct drm_device *dev = crtc->dev;
aa420ddd 11065 struct drm_i915_private *dev_priv = dev->dev_private;
143f73b3 11066 struct drm_i915_gem_request *req;
d55dbd06 11067 int i, ret;
84c33a64 11068
a6747b73
ML
11069 if (!needs_modeset(&crtc_state->base) && crtc_state->update_pipe) {
11070 work->put_power_domains =
11071 modeset_get_crtc_power_domains(crtc, crtc_state);
11072 }
11073
143f73b3
ML
11074 for (i = 0; i < work->num_planes; i++) {
11075 struct intel_plane_state *old_plane_state = work->old_plane_state[i];
11076
11077 /* For framebuffer backed by dmabuf, wait for fence */
11078 if (old_plane_state->base.fence)
11079 WARN_ON(fence_wait(old_plane_state->base.fence, false) < 0);
11080
11081 req = old_plane_state->wait_req;
11082 if (!req)
11083 continue;
11084
11085 WARN_ON(__i915_wait_request(req, false, NULL,
51cbaf01 11086 &dev_priv->rps.mmioflips));
143f73b3 11087 }
84c33a64 11088
d55dbd06
ML
11089 ret = drm_crtc_vblank_get(crtc);
11090 I915_STATE_WARN(ret < 0, "enabling vblank failed with %i\n", ret);
11091
11092 if (work->num_planes &&
11093 work->old_plane_state[0]->base.plane == crtc->primary)
11094 intel_fbc_enable(intel_crtc, work->new_crtc_state, work->new_plane_state[0]);
11095
11096 intel_frontbuffer_flip_prepare(dev, work->fb_bits);
fd8e058a 11097
143f73b3
ML
11098 intel_pipe_update_start(intel_crtc);
11099 if (!needs_modeset(&crtc_state->base)) {
11100 if (crtc_state->base.color_mgmt_changed || crtc_state->update_pipe) {
11101 intel_color_set_csc(&crtc_state->base);
11102 intel_color_load_luts(&crtc_state->base);
11103 }
84c33a64 11104
143f73b3
ML
11105 if (crtc_state->update_pipe)
11106 intel_update_pipe_config(intel_crtc, work->old_crtc_state);
11107 else if (INTEL_INFO(dev)->gen >= 9)
11108 skl_detach_scalers(intel_crtc);
11109 }
11110
11111 for (i = 0; i < work->num_planes; i++) {
11112 struct intel_plane_state *new_plane_state = work->new_plane_state[i];
11113 struct intel_plane *plane = to_intel_plane(new_plane_state->base.plane);
11114
d55dbd06
ML
11115 if (new_plane_state->visible)
11116 plane->update_plane(&plane->base, crtc_state, new_plane_state);
11117 else
11118 plane->disable_plane(&plane->base, crtc);
143f73b3
ML
11119 }
11120
11121 intel_pipe_update_end(intel_crtc, work);
8c9f3aaf
JB
11122}
11123
da20eabd
ML
11124/**
11125 * intel_wm_need_update - Check whether watermarks need updating
11126 * @plane: drm plane
11127 * @state: new plane state
11128 *
11129 * Check current plane state versus the new one to determine whether
11130 * watermarks need to be recalculated.
11131 *
11132 * Returns true or false.
11133 */
11134static bool intel_wm_need_update(struct drm_plane *plane,
11135 struct drm_plane_state *state)
11136{
d21fbe87
MR
11137 struct intel_plane_state *new = to_intel_plane_state(state);
11138 struct intel_plane_state *cur = to_intel_plane_state(plane->state);
11139
11140 /* Update watermarks on tiling or size changes. */
92826fcd
ML
11141 if (new->visible != cur->visible)
11142 return true;
11143
11144 if (!cur->base.fb || !new->base.fb)
11145 return false;
11146
11147 if (cur->base.fb->modifier[0] != new->base.fb->modifier[0] ||
11148 cur->base.rotation != new->base.rotation ||
d21fbe87
MR
11149 drm_rect_width(&new->src) != drm_rect_width(&cur->src) ||
11150 drm_rect_height(&new->src) != drm_rect_height(&cur->src) ||
11151 drm_rect_width(&new->dst) != drm_rect_width(&cur->dst) ||
11152 drm_rect_height(&new->dst) != drm_rect_height(&cur->dst))
2791a16c 11153 return true;
7809e5ae 11154
2791a16c 11155 return false;
7809e5ae
MR
11156}
11157
d21fbe87
MR
11158static bool needs_scaling(struct intel_plane_state *state)
11159{
11160 int src_w = drm_rect_width(&state->src) >> 16;
11161 int src_h = drm_rect_height(&state->src) >> 16;
11162 int dst_w = drm_rect_width(&state->dst);
11163 int dst_h = drm_rect_height(&state->dst);
11164
11165 return (src_w != dst_w || src_h != dst_h);
11166}
11167
da20eabd
ML
11168int intel_plane_atomic_calc_changes(struct drm_crtc_state *crtc_state,
11169 struct drm_plane_state *plane_state)
11170{
ab1d3a0e 11171 struct intel_crtc_state *pipe_config = to_intel_crtc_state(crtc_state);
da20eabd
ML
11172 struct drm_crtc *crtc = crtc_state->crtc;
11173 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11174 struct drm_plane *plane = plane_state->plane;
11175 struct drm_device *dev = crtc->dev;
ed4a6a7c 11176 struct drm_i915_private *dev_priv = to_i915(dev);
da20eabd
ML
11177 struct intel_plane_state *old_plane_state =
11178 to_intel_plane_state(plane->state);
11179 int idx = intel_crtc->base.base.id, ret;
da20eabd
ML
11180 bool mode_changed = needs_modeset(crtc_state);
11181 bool was_crtc_enabled = crtc->state->active;
11182 bool is_crtc_enabled = crtc_state->active;
da20eabd
ML
11183 bool turn_off, turn_on, visible, was_visible;
11184 struct drm_framebuffer *fb = plane_state->fb;
11185
11186 if (crtc_state && INTEL_INFO(dev)->gen >= 9 &&
11187 plane->type != DRM_PLANE_TYPE_CURSOR) {
11188 ret = skl_update_scaler_plane(
11189 to_intel_crtc_state(crtc_state),
11190 to_intel_plane_state(plane_state));
11191 if (ret)
11192 return ret;
11193 }
11194
da20eabd
ML
11195 was_visible = old_plane_state->visible;
11196 visible = to_intel_plane_state(plane_state)->visible;
11197
11198 if (!was_crtc_enabled && WARN_ON(was_visible))
11199 was_visible = false;
11200
35c08f43
ML
11201 /*
11202 * Visibility is calculated as if the crtc was on, but
11203 * after scaler setup everything depends on it being off
11204 * when the crtc isn't active.
f818ffea
VS
11205 *
11206 * FIXME this is wrong for watermarks. Watermarks should also
11207 * be computed as if the pipe would be active. Perhaps move
11208 * per-plane wm computation to the .check_plane() hook, and
11209 * only combine the results from all planes in the current place?
35c08f43
ML
11210 */
11211 if (!is_crtc_enabled)
11212 to_intel_plane_state(plane_state)->visible = visible = false;
da20eabd
ML
11213
11214 if (!was_visible && !visible)
11215 return 0;
11216
e8861675
ML
11217 if (fb != old_plane_state->base.fb)
11218 pipe_config->fb_changed = true;
11219
da20eabd
ML
11220 turn_off = was_visible && (!visible || mode_changed);
11221 turn_on = visible && (!was_visible || mode_changed);
11222
11223 DRM_DEBUG_ATOMIC("[CRTC:%i] has [PLANE:%i] with fb %i\n", idx,
11224 plane->base.id, fb ? fb->base.id : -1);
11225
11226 DRM_DEBUG_ATOMIC("[PLANE:%i] visible %i -> %i, off %i, on %i, ms %i\n",
11227 plane->base.id, was_visible, visible,
11228 turn_off, turn_on, mode_changed);
11229
caed361d
VS
11230 if (turn_on) {
11231 pipe_config->update_wm_pre = true;
11232
11233 /* must disable cxsr around plane enable/disable */
11234 if (plane->type != DRM_PLANE_TYPE_CURSOR)
11235 pipe_config->disable_cxsr = true;
11236 } else if (turn_off) {
11237 pipe_config->update_wm_post = true;
92826fcd 11238
852eb00d 11239 /* must disable cxsr around plane enable/disable */
e8861675 11240 if (plane->type != DRM_PLANE_TYPE_CURSOR)
ab1d3a0e 11241 pipe_config->disable_cxsr = true;
852eb00d 11242 } else if (intel_wm_need_update(plane, plane_state)) {
caed361d
VS
11243 /* FIXME bollocks */
11244 pipe_config->update_wm_pre = true;
11245 pipe_config->update_wm_post = true;
852eb00d 11246 }
da20eabd 11247
ed4a6a7c 11248 /* Pre-gen9 platforms need two-step watermark updates */
caed361d
VS
11249 if ((pipe_config->update_wm_pre || pipe_config->update_wm_post) &&
11250 INTEL_INFO(dev)->gen < 9 && dev_priv->display.optimize_watermarks)
ed4a6a7c
MR
11251 to_intel_crtc_state(crtc_state)->wm.need_postvbl_update = true;
11252
8be6ca85 11253 if (visible || was_visible)
cd202f69 11254 pipe_config->fb_bits |= to_intel_plane(plane)->frontbuffer_bit;
a9ff8714 11255
31ae71fc
ML
11256 /*
11257 * WaCxSRDisabledForSpriteScaling:ivb
11258 *
11259 * cstate->update_wm was already set above, so this flag will
11260 * take effect when we commit and program watermarks.
11261 */
11262 if (plane->type == DRM_PLANE_TYPE_OVERLAY && IS_IVYBRIDGE(dev) &&
11263 needs_scaling(to_intel_plane_state(plane_state)) &&
11264 !needs_scaling(old_plane_state))
11265 pipe_config->disable_lp_wm = true;
d21fbe87 11266
da20eabd
ML
11267 return 0;
11268}
11269
6d3a1ce7
ML
11270static bool encoders_cloneable(const struct intel_encoder *a,
11271 const struct intel_encoder *b)
11272{
11273 /* masks could be asymmetric, so check both ways */
11274 return a == b || (a->cloneable & (1 << b->type) &&
11275 b->cloneable & (1 << a->type));
11276}
11277
11278static bool check_single_encoder_cloning(struct drm_atomic_state *state,
11279 struct intel_crtc *crtc,
11280 struct intel_encoder *encoder)
11281{
11282 struct intel_encoder *source_encoder;
11283 struct drm_connector *connector;
11284 struct drm_connector_state *connector_state;
11285 int i;
11286
11287 for_each_connector_in_state(state, connector, connector_state, i) {
11288 if (connector_state->crtc != &crtc->base)
11289 continue;
11290
11291 source_encoder =
11292 to_intel_encoder(connector_state->best_encoder);
11293 if (!encoders_cloneable(encoder, source_encoder))
11294 return false;
11295 }
11296
11297 return true;
11298}
11299
11300static bool check_encoder_cloning(struct drm_atomic_state *state,
11301 struct intel_crtc *crtc)
11302{
11303 struct intel_encoder *encoder;
11304 struct drm_connector *connector;
11305 struct drm_connector_state *connector_state;
11306 int i;
11307
11308 for_each_connector_in_state(state, connector, connector_state, i) {
11309 if (connector_state->crtc != &crtc->base)
11310 continue;
11311
11312 encoder = to_intel_encoder(connector_state->best_encoder);
11313 if (!check_single_encoder_cloning(state, crtc, encoder))
11314 return false;
11315 }
11316
11317 return true;
11318}
11319
11320static int intel_crtc_atomic_check(struct drm_crtc *crtc,
11321 struct drm_crtc_state *crtc_state)
11322{
cf5a15be 11323 struct drm_device *dev = crtc->dev;
ad421372 11324 struct drm_i915_private *dev_priv = dev->dev_private;
6d3a1ce7 11325 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
cf5a15be
ML
11326 struct intel_crtc_state *pipe_config =
11327 to_intel_crtc_state(crtc_state);
6d3a1ce7 11328 struct drm_atomic_state *state = crtc_state->state;
4d20cd86 11329 int ret;
6d3a1ce7
ML
11330 bool mode_changed = needs_modeset(crtc_state);
11331
11332 if (mode_changed && !check_encoder_cloning(state, intel_crtc)) {
11333 DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
11334 return -EINVAL;
11335 }
11336
852eb00d 11337 if (mode_changed && !crtc_state->active)
caed361d 11338 pipe_config->update_wm_post = true;
eddfcbcd 11339
ad421372
ML
11340 if (mode_changed && crtc_state->enable &&
11341 dev_priv->display.crtc_compute_clock &&
8106ddbd 11342 !WARN_ON(pipe_config->shared_dpll)) {
ad421372
ML
11343 ret = dev_priv->display.crtc_compute_clock(intel_crtc,
11344 pipe_config);
11345 if (ret)
11346 return ret;
11347 }
11348
82cf435b
LL
11349 if (crtc_state->color_mgmt_changed) {
11350 ret = intel_color_check(crtc, crtc_state);
11351 if (ret)
11352 return ret;
11353 }
11354
e435d6e5 11355 ret = 0;
86c8bbbe 11356 if (dev_priv->display.compute_pipe_wm) {
e3bddded 11357 ret = dev_priv->display.compute_pipe_wm(pipe_config);
ed4a6a7c
MR
11358 if (ret) {
11359 DRM_DEBUG_KMS("Target pipe watermarks are invalid\n");
11360 return ret;
11361 }
11362 }
11363
11364 if (dev_priv->display.compute_intermediate_wm &&
11365 !to_intel_atomic_state(state)->skip_intermediate_wm) {
11366 if (WARN_ON(!dev_priv->display.compute_pipe_wm))
11367 return 0;
11368
11369 /*
11370 * Calculate 'intermediate' watermarks that satisfy both the
11371 * old state and the new state. We can program these
11372 * immediately.
11373 */
11374 ret = dev_priv->display.compute_intermediate_wm(crtc->dev,
11375 intel_crtc,
11376 pipe_config);
11377 if (ret) {
11378 DRM_DEBUG_KMS("No valid intermediate pipe watermarks are possible\n");
86c8bbbe 11379 return ret;
ed4a6a7c 11380 }
e3d5457c
VS
11381 } else if (dev_priv->display.compute_intermediate_wm) {
11382 if (HAS_PCH_SPLIT(dev_priv) && INTEL_GEN(dev_priv) < 9)
11383 pipe_config->wm.ilk.intermediate = pipe_config->wm.ilk.optimal;
86c8bbbe
MR
11384 }
11385
e435d6e5
ML
11386 if (INTEL_INFO(dev)->gen >= 9) {
11387 if (mode_changed)
11388 ret = skl_update_scaler_crtc(pipe_config);
11389
11390 if (!ret)
11391 ret = intel_atomic_setup_scalers(dev, intel_crtc,
11392 pipe_config);
11393 }
11394
11395 return ret;
6d3a1ce7
ML
11396}
11397
65b38e0d 11398static const struct drm_crtc_helper_funcs intel_helper_funcs = {
f6e5b160 11399 .mode_set_base_atomic = intel_pipe_set_base_atomic,
6d3a1ce7 11400 .atomic_check = intel_crtc_atomic_check,
f6e5b160
CW
11401};
11402
d29b2f9d
ACO
11403static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
11404{
11405 struct intel_connector *connector;
11406
11407 for_each_intel_connector(dev, connector) {
8863dc7f
DV
11408 if (connector->base.state->crtc)
11409 drm_connector_unreference(&connector->base);
11410
d29b2f9d
ACO
11411 if (connector->base.encoder) {
11412 connector->base.state->best_encoder =
11413 connector->base.encoder;
11414 connector->base.state->crtc =
11415 connector->base.encoder->crtc;
8863dc7f
DV
11416
11417 drm_connector_reference(&connector->base);
d29b2f9d
ACO
11418 } else {
11419 connector->base.state->best_encoder = NULL;
11420 connector->base.state->crtc = NULL;
11421 }
11422 }
11423}
11424
050f7aeb 11425static void
eba905b2 11426connected_sink_compute_bpp(struct intel_connector *connector,
5cec258b 11427 struct intel_crtc_state *pipe_config)
050f7aeb
DV
11428{
11429 int bpp = pipe_config->pipe_bpp;
11430
11431 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
11432 connector->base.base.id,
c23cc417 11433 connector->base.name);
050f7aeb
DV
11434
11435 /* Don't use an invalid EDID bpc value */
11436 if (connector->base.display_info.bpc &&
11437 connector->base.display_info.bpc * 3 < bpp) {
11438 DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
11439 bpp, connector->base.display_info.bpc*3);
11440 pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
11441 }
11442
013dd9e0
JN
11443 /* Clamp bpp to default limit on screens without EDID 1.4 */
11444 if (connector->base.display_info.bpc == 0) {
11445 int type = connector->base.connector_type;
11446 int clamp_bpp = 24;
11447
11448 /* Fall back to 18 bpp when DP sink capability is unknown. */
11449 if (type == DRM_MODE_CONNECTOR_DisplayPort ||
11450 type == DRM_MODE_CONNECTOR_eDP)
11451 clamp_bpp = 18;
11452
11453 if (bpp > clamp_bpp) {
11454 DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of %d\n",
11455 bpp, clamp_bpp);
11456 pipe_config->pipe_bpp = clamp_bpp;
11457 }
050f7aeb
DV
11458 }
11459}
11460
4e53c2e0 11461static int
050f7aeb 11462compute_baseline_pipe_bpp(struct intel_crtc *crtc,
5cec258b 11463 struct intel_crtc_state *pipe_config)
4e53c2e0 11464{
050f7aeb 11465 struct drm_device *dev = crtc->base.dev;
1486017f 11466 struct drm_atomic_state *state;
da3ced29
ACO
11467 struct drm_connector *connector;
11468 struct drm_connector_state *connector_state;
1486017f 11469 int bpp, i;
4e53c2e0 11470
666a4537 11471 if ((IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)))
4e53c2e0 11472 bpp = 10*3;
d328c9d7
DV
11473 else if (INTEL_INFO(dev)->gen >= 5)
11474 bpp = 12*3;
11475 else
11476 bpp = 8*3;
11477
4e53c2e0 11478
4e53c2e0
DV
11479 pipe_config->pipe_bpp = bpp;
11480
1486017f
ACO
11481 state = pipe_config->base.state;
11482
4e53c2e0 11483 /* Clamp display bpp to EDID value */
da3ced29
ACO
11484 for_each_connector_in_state(state, connector, connector_state, i) {
11485 if (connector_state->crtc != &crtc->base)
4e53c2e0
DV
11486 continue;
11487
da3ced29
ACO
11488 connected_sink_compute_bpp(to_intel_connector(connector),
11489 pipe_config);
4e53c2e0
DV
11490 }
11491
11492 return bpp;
11493}
11494
644db711
DV
11495static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
11496{
11497 DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
11498 "type: 0x%x flags: 0x%x\n",
1342830c 11499 mode->crtc_clock,
644db711
DV
11500 mode->crtc_hdisplay, mode->crtc_hsync_start,
11501 mode->crtc_hsync_end, mode->crtc_htotal,
11502 mode->crtc_vdisplay, mode->crtc_vsync_start,
11503 mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
11504}
11505
c0b03411 11506static void intel_dump_pipe_config(struct intel_crtc *crtc,
5cec258b 11507 struct intel_crtc_state *pipe_config,
c0b03411
DV
11508 const char *context)
11509{
6a60cd87
CK
11510 struct drm_device *dev = crtc->base.dev;
11511 struct drm_plane *plane;
11512 struct intel_plane *intel_plane;
11513 struct intel_plane_state *state;
11514 struct drm_framebuffer *fb;
11515
11516 DRM_DEBUG_KMS("[CRTC:%d]%s config %p for pipe %c\n", crtc->base.base.id,
11517 context, pipe_config, pipe_name(crtc->pipe));
c0b03411 11518
da205630 11519 DRM_DEBUG_KMS("cpu_transcoder: %s\n", transcoder_name(pipe_config->cpu_transcoder));
c0b03411
DV
11520 DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
11521 pipe_config->pipe_bpp, pipe_config->dither);
11522 DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
11523 pipe_config->has_pch_encoder,
11524 pipe_config->fdi_lanes,
11525 pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
11526 pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
11527 pipe_config->fdi_m_n.tu);
90a6b7b0 11528 DRM_DEBUG_KMS("dp: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
eb14cb74 11529 pipe_config->has_dp_encoder,
90a6b7b0 11530 pipe_config->lane_count,
eb14cb74
VS
11531 pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
11532 pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
11533 pipe_config->dp_m_n.tu);
b95af8be 11534
90a6b7b0 11535 DRM_DEBUG_KMS("dp: %i, lanes: %i, gmch_m2: %u, gmch_n2: %u, link_m2: %u, link_n2: %u, tu2: %u\n",
b95af8be 11536 pipe_config->has_dp_encoder,
90a6b7b0 11537 pipe_config->lane_count,
b95af8be
VK
11538 pipe_config->dp_m2_n2.gmch_m,
11539 pipe_config->dp_m2_n2.gmch_n,
11540 pipe_config->dp_m2_n2.link_m,
11541 pipe_config->dp_m2_n2.link_n,
11542 pipe_config->dp_m2_n2.tu);
11543
55072d19
DV
11544 DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
11545 pipe_config->has_audio,
11546 pipe_config->has_infoframe);
11547
c0b03411 11548 DRM_DEBUG_KMS("requested mode:\n");
2d112de7 11549 drm_mode_debug_printmodeline(&pipe_config->base.mode);
c0b03411 11550 DRM_DEBUG_KMS("adjusted mode:\n");
2d112de7
ACO
11551 drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
11552 intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
d71b8d4a 11553 DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
37327abd
VS
11554 DRM_DEBUG_KMS("pipe src size: %dx%d\n",
11555 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
0ec463d3
TU
11556 DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
11557 crtc->num_scalers,
11558 pipe_config->scaler_state.scaler_users,
11559 pipe_config->scaler_state.scaler_id);
c0b03411
DV
11560 DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
11561 pipe_config->gmch_pfit.control,
11562 pipe_config->gmch_pfit.pgm_ratios,
11563 pipe_config->gmch_pfit.lvds_border_bits);
fd4daa9c 11564 DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
c0b03411 11565 pipe_config->pch_pfit.pos,
fd4daa9c
CW
11566 pipe_config->pch_pfit.size,
11567 pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
42db64ef 11568 DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
cf532bb2 11569 DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
6a60cd87 11570
415ff0f6 11571 if (IS_BROXTON(dev)) {
05712c15 11572 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: ebb0: 0x%x, ebb4: 0x%x,"
415ff0f6 11573 "pll0: 0x%x, pll1: 0x%x, pll2: 0x%x, pll3: 0x%x, "
c8453338 11574 "pll6: 0x%x, pll8: 0x%x, pll9: 0x%x, pll10: 0x%x, pcsdw12: 0x%x\n",
415ff0f6
TU
11575 pipe_config->ddi_pll_sel,
11576 pipe_config->dpll_hw_state.ebb0,
05712c15 11577 pipe_config->dpll_hw_state.ebb4,
415ff0f6
TU
11578 pipe_config->dpll_hw_state.pll0,
11579 pipe_config->dpll_hw_state.pll1,
11580 pipe_config->dpll_hw_state.pll2,
11581 pipe_config->dpll_hw_state.pll3,
11582 pipe_config->dpll_hw_state.pll6,
11583 pipe_config->dpll_hw_state.pll8,
05712c15 11584 pipe_config->dpll_hw_state.pll9,
c8453338 11585 pipe_config->dpll_hw_state.pll10,
415ff0f6 11586 pipe_config->dpll_hw_state.pcsdw12);
ef11bdb3 11587 } else if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) {
415ff0f6
TU
11588 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: "
11589 "ctrl1: 0x%x, cfgcr1: 0x%x, cfgcr2: 0x%x\n",
11590 pipe_config->ddi_pll_sel,
11591 pipe_config->dpll_hw_state.ctrl1,
11592 pipe_config->dpll_hw_state.cfgcr1,
11593 pipe_config->dpll_hw_state.cfgcr2);
11594 } else if (HAS_DDI(dev)) {
1260f07e 11595 DRM_DEBUG_KMS("ddi_pll_sel: 0x%x; dpll_hw_state: wrpll: 0x%x spll: 0x%x\n",
415ff0f6 11596 pipe_config->ddi_pll_sel,
00490c22
ML
11597 pipe_config->dpll_hw_state.wrpll,
11598 pipe_config->dpll_hw_state.spll);
415ff0f6
TU
11599 } else {
11600 DRM_DEBUG_KMS("dpll_hw_state: dpll: 0x%x, dpll_md: 0x%x, "
11601 "fp0: 0x%x, fp1: 0x%x\n",
11602 pipe_config->dpll_hw_state.dpll,
11603 pipe_config->dpll_hw_state.dpll_md,
11604 pipe_config->dpll_hw_state.fp0,
11605 pipe_config->dpll_hw_state.fp1);
11606 }
11607
6a60cd87
CK
11608 DRM_DEBUG_KMS("planes on this crtc\n");
11609 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
11610 intel_plane = to_intel_plane(plane);
11611 if (intel_plane->pipe != crtc->pipe)
11612 continue;
11613
11614 state = to_intel_plane_state(plane->state);
11615 fb = state->base.fb;
11616 if (!fb) {
11617 DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d "
11618 "disabled, scaler_id = %d\n",
11619 plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
11620 plane->base.id, intel_plane->pipe,
11621 (crtc->base.primary == plane) ? 0 : intel_plane->plane + 1,
11622 drm_plane_index(plane), state->scaler_id);
11623 continue;
11624 }
11625
11626 DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d enabled",
11627 plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
11628 plane->base.id, intel_plane->pipe,
11629 crtc->base.primary == plane ? 0 : intel_plane->plane + 1,
11630 drm_plane_index(plane));
11631 DRM_DEBUG_KMS("\tFB:%d, fb = %ux%u format = 0x%x",
11632 fb->base.id, fb->width, fb->height, fb->pixel_format);
11633 DRM_DEBUG_KMS("\tscaler:%d src (%u, %u) %ux%u dst (%u, %u) %ux%u\n",
11634 state->scaler_id,
11635 state->src.x1 >> 16, state->src.y1 >> 16,
11636 drm_rect_width(&state->src) >> 16,
11637 drm_rect_height(&state->src) >> 16,
11638 state->dst.x1, state->dst.y1,
11639 drm_rect_width(&state->dst), drm_rect_height(&state->dst));
11640 }
c0b03411
DV
11641}
11642
5448a00d 11643static bool check_digital_port_conflicts(struct drm_atomic_state *state)
00f0b378 11644{
5448a00d 11645 struct drm_device *dev = state->dev;
da3ced29 11646 struct drm_connector *connector;
00f0b378
VS
11647 unsigned int used_ports = 0;
11648
11649 /*
11650 * Walk the connector list instead of the encoder
11651 * list to detect the problem on ddi platforms
11652 * where there's just one encoder per digital port.
11653 */
0bff4858
VS
11654 drm_for_each_connector(connector, dev) {
11655 struct drm_connector_state *connector_state;
11656 struct intel_encoder *encoder;
11657
11658 connector_state = drm_atomic_get_existing_connector_state(state, connector);
11659 if (!connector_state)
11660 connector_state = connector->state;
11661
5448a00d 11662 if (!connector_state->best_encoder)
00f0b378
VS
11663 continue;
11664
5448a00d
ACO
11665 encoder = to_intel_encoder(connector_state->best_encoder);
11666
11667 WARN_ON(!connector_state->crtc);
00f0b378
VS
11668
11669 switch (encoder->type) {
11670 unsigned int port_mask;
11671 case INTEL_OUTPUT_UNKNOWN:
11672 if (WARN_ON(!HAS_DDI(dev)))
11673 break;
11674 case INTEL_OUTPUT_DISPLAYPORT:
11675 case INTEL_OUTPUT_HDMI:
11676 case INTEL_OUTPUT_EDP:
11677 port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
11678
11679 /* the same port mustn't appear more than once */
11680 if (used_ports & port_mask)
11681 return false;
11682
11683 used_ports |= port_mask;
11684 default:
11685 break;
11686 }
11687 }
11688
11689 return true;
11690}
11691
83a57153
ACO
11692static void
11693clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
11694{
11695 struct drm_crtc_state tmp_state;
663a3640 11696 struct intel_crtc_scaler_state scaler_state;
4978cc93 11697 struct intel_dpll_hw_state dpll_hw_state;
8106ddbd 11698 struct intel_shared_dpll *shared_dpll;
8504c74c 11699 uint32_t ddi_pll_sel;
c4e2d043 11700 bool force_thru;
83a57153 11701
7546a384
ACO
11702 /* FIXME: before the switch to atomic started, a new pipe_config was
11703 * kzalloc'd. Code that depends on any field being zero should be
11704 * fixed, so that the crtc_state can be safely duplicated. For now,
11705 * only fields that are know to not cause problems are preserved. */
11706
83a57153 11707 tmp_state = crtc_state->base;
663a3640 11708 scaler_state = crtc_state->scaler_state;
4978cc93
ACO
11709 shared_dpll = crtc_state->shared_dpll;
11710 dpll_hw_state = crtc_state->dpll_hw_state;
8504c74c 11711 ddi_pll_sel = crtc_state->ddi_pll_sel;
c4e2d043 11712 force_thru = crtc_state->pch_pfit.force_thru;
4978cc93 11713
83a57153 11714 memset(crtc_state, 0, sizeof *crtc_state);
4978cc93 11715
83a57153 11716 crtc_state->base = tmp_state;
663a3640 11717 crtc_state->scaler_state = scaler_state;
4978cc93
ACO
11718 crtc_state->shared_dpll = shared_dpll;
11719 crtc_state->dpll_hw_state = dpll_hw_state;
8504c74c 11720 crtc_state->ddi_pll_sel = ddi_pll_sel;
c4e2d043 11721 crtc_state->pch_pfit.force_thru = force_thru;
83a57153
ACO
11722}
11723
548ee15b 11724static int
b8cecdf5 11725intel_modeset_pipe_config(struct drm_crtc *crtc,
b359283a 11726 struct intel_crtc_state *pipe_config)
ee7b9f93 11727{
b359283a 11728 struct drm_atomic_state *state = pipe_config->base.state;
7758a113 11729 struct intel_encoder *encoder;
da3ced29 11730 struct drm_connector *connector;
0b901879 11731 struct drm_connector_state *connector_state;
d328c9d7 11732 int base_bpp, ret = -EINVAL;
0b901879 11733 int i;
e29c22c0 11734 bool retry = true;
ee7b9f93 11735
83a57153 11736 clear_intel_crtc_state(pipe_config);
7758a113 11737
e143a21c
DV
11738 pipe_config->cpu_transcoder =
11739 (enum transcoder) to_intel_crtc(crtc)->pipe;
b8cecdf5 11740
2960bc9c
ID
11741 /*
11742 * Sanitize sync polarity flags based on requested ones. If neither
11743 * positive or negative polarity is requested, treat this as meaning
11744 * negative polarity.
11745 */
2d112de7 11746 if (!(pipe_config->base.adjusted_mode.flags &
2960bc9c 11747 (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
2d112de7 11748 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
2960bc9c 11749
2d112de7 11750 if (!(pipe_config->base.adjusted_mode.flags &
2960bc9c 11751 (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
2d112de7 11752 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
2960bc9c 11753
d328c9d7
DV
11754 base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
11755 pipe_config);
11756 if (base_bpp < 0)
4e53c2e0
DV
11757 goto fail;
11758
e41a56be
VS
11759 /*
11760 * Determine the real pipe dimensions. Note that stereo modes can
11761 * increase the actual pipe size due to the frame doubling and
11762 * insertion of additional space for blanks between the frame. This
11763 * is stored in the crtc timings. We use the requested mode to do this
11764 * computation to clearly distinguish it from the adjusted mode, which
11765 * can be changed by the connectors in the below retry loop.
11766 */
2d112de7 11767 drm_crtc_get_hv_timing(&pipe_config->base.mode,
ecb7e16b
GP
11768 &pipe_config->pipe_src_w,
11769 &pipe_config->pipe_src_h);
e41a56be 11770
e29c22c0 11771encoder_retry:
ef1b460d 11772 /* Ensure the port clock defaults are reset when retrying. */
ff9a6750 11773 pipe_config->port_clock = 0;
ef1b460d 11774 pipe_config->pixel_multiplier = 1;
ff9a6750 11775
135c81b8 11776 /* Fill in default crtc timings, allow encoders to overwrite them. */
2d112de7
ACO
11777 drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
11778 CRTC_STEREO_DOUBLE);
135c81b8 11779
7758a113
DV
11780 /* Pass our mode to the connectors and the CRTC to give them a chance to
11781 * adjust it according to limitations or connector properties, and also
11782 * a chance to reject the mode entirely.
47f1c6c9 11783 */
da3ced29 11784 for_each_connector_in_state(state, connector, connector_state, i) {
0b901879 11785 if (connector_state->crtc != crtc)
7758a113 11786 continue;
7ae89233 11787
0b901879
ACO
11788 encoder = to_intel_encoder(connector_state->best_encoder);
11789
efea6e8e
DV
11790 if (!(encoder->compute_config(encoder, pipe_config))) {
11791 DRM_DEBUG_KMS("Encoder config failure\n");
7758a113
DV
11792 goto fail;
11793 }
ee7b9f93 11794 }
47f1c6c9 11795
ff9a6750
DV
11796 /* Set default port clock if not overwritten by the encoder. Needs to be
11797 * done afterwards in case the encoder adjusts the mode. */
11798 if (!pipe_config->port_clock)
2d112de7 11799 pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
241bfc38 11800 * pipe_config->pixel_multiplier;
ff9a6750 11801
a43f6e0f 11802 ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
e29c22c0 11803 if (ret < 0) {
7758a113
DV
11804 DRM_DEBUG_KMS("CRTC fixup failed\n");
11805 goto fail;
ee7b9f93 11806 }
e29c22c0
DV
11807
11808 if (ret == RETRY) {
11809 if (WARN(!retry, "loop in pipe configuration computation\n")) {
11810 ret = -EINVAL;
11811 goto fail;
11812 }
11813
11814 DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
11815 retry = false;
11816 goto encoder_retry;
11817 }
11818
e8fa4270
DV
11819 /* Dithering seems to not pass-through bits correctly when it should, so
11820 * only enable it on 6bpc panels. */
11821 pipe_config->dither = pipe_config->pipe_bpp == 6*3;
62f0ace5 11822 DRM_DEBUG_KMS("hw max bpp: %i, pipe bpp: %i, dithering: %i\n",
d328c9d7 11823 base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
4e53c2e0 11824
7758a113 11825fail:
548ee15b 11826 return ret;
ee7b9f93 11827}
47f1c6c9 11828
ea9d758d 11829static void
4740b0f2 11830intel_modeset_update_crtc_state(struct drm_atomic_state *state)
ea9d758d 11831{
0a9ab303
ACO
11832 struct drm_crtc *crtc;
11833 struct drm_crtc_state *crtc_state;
8a75d157 11834 int i;
ea9d758d 11835
7668851f 11836 /* Double check state. */
8a75d157 11837 for_each_crtc_in_state(state, crtc, crtc_state, i) {
3cb480bc 11838 to_intel_crtc(crtc)->config = to_intel_crtc_state(crtc->state);
fc467a22
ML
11839
11840 /* Update hwmode for vblank functions */
11841 if (crtc->state->active)
11842 crtc->hwmode = crtc->state->adjusted_mode;
11843 else
11844 crtc->hwmode.crtc_clock = 0;
61067a5e
ML
11845
11846 /*
11847 * Update legacy state to satisfy fbc code. This can
11848 * be removed when fbc uses the atomic state.
11849 */
11850 if (drm_atomic_get_existing_plane_state(state, crtc->primary)) {
11851 struct drm_plane_state *plane_state = crtc->primary->state;
11852
11853 crtc->primary->fb = plane_state->fb;
11854 crtc->x = plane_state->src_x >> 16;
11855 crtc->y = plane_state->src_y >> 16;
11856 }
ea9d758d 11857 }
ea9d758d
DV
11858}
11859
3bd26263 11860static bool intel_fuzzy_clock_check(int clock1, int clock2)
f1f644dc 11861{
3bd26263 11862 int diff;
f1f644dc
JB
11863
11864 if (clock1 == clock2)
11865 return true;
11866
11867 if (!clock1 || !clock2)
11868 return false;
11869
11870 diff = abs(clock1 - clock2);
11871
11872 if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
11873 return true;
11874
11875 return false;
11876}
11877
25c5b266
DV
11878#define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
11879 list_for_each_entry((intel_crtc), \
11880 &(dev)->mode_config.crtc_list, \
11881 base.head) \
95150bdf 11882 for_each_if (mask & (1 <<(intel_crtc)->pipe))
25c5b266 11883
cfb23ed6
ML
11884static bool
11885intel_compare_m_n(unsigned int m, unsigned int n,
11886 unsigned int m2, unsigned int n2,
11887 bool exact)
11888{
11889 if (m == m2 && n == n2)
11890 return true;
11891
11892 if (exact || !m || !n || !m2 || !n2)
11893 return false;
11894
11895 BUILD_BUG_ON(DATA_LINK_M_N_MASK > INT_MAX);
11896
31d10b57
ML
11897 if (n > n2) {
11898 while (n > n2) {
cfb23ed6
ML
11899 m2 <<= 1;
11900 n2 <<= 1;
11901 }
31d10b57
ML
11902 } else if (n < n2) {
11903 while (n < n2) {
cfb23ed6
ML
11904 m <<= 1;
11905 n <<= 1;
11906 }
11907 }
11908
31d10b57
ML
11909 if (n != n2)
11910 return false;
11911
11912 return intel_fuzzy_clock_check(m, m2);
cfb23ed6
ML
11913}
11914
11915static bool
11916intel_compare_link_m_n(const struct intel_link_m_n *m_n,
11917 struct intel_link_m_n *m2_n2,
11918 bool adjust)
11919{
11920 if (m_n->tu == m2_n2->tu &&
11921 intel_compare_m_n(m_n->gmch_m, m_n->gmch_n,
11922 m2_n2->gmch_m, m2_n2->gmch_n, !adjust) &&
11923 intel_compare_m_n(m_n->link_m, m_n->link_n,
11924 m2_n2->link_m, m2_n2->link_n, !adjust)) {
11925 if (adjust)
11926 *m2_n2 = *m_n;
11927
11928 return true;
11929 }
11930
11931 return false;
11932}
11933
0e8ffe1b 11934static bool
2fa2fe9a 11935intel_pipe_config_compare(struct drm_device *dev,
5cec258b 11936 struct intel_crtc_state *current_config,
cfb23ed6
ML
11937 struct intel_crtc_state *pipe_config,
11938 bool adjust)
0e8ffe1b 11939{
cfb23ed6
ML
11940 bool ret = true;
11941
11942#define INTEL_ERR_OR_DBG_KMS(fmt, ...) \
11943 do { \
11944 if (!adjust) \
11945 DRM_ERROR(fmt, ##__VA_ARGS__); \
11946 else \
11947 DRM_DEBUG_KMS(fmt, ##__VA_ARGS__); \
11948 } while (0)
11949
66e985c0
DV
11950#define PIPE_CONF_CHECK_X(name) \
11951 if (current_config->name != pipe_config->name) { \
cfb23ed6 11952 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
66e985c0
DV
11953 "(expected 0x%08x, found 0x%08x)\n", \
11954 current_config->name, \
11955 pipe_config->name); \
cfb23ed6 11956 ret = false; \
66e985c0
DV
11957 }
11958
08a24034
DV
11959#define PIPE_CONF_CHECK_I(name) \
11960 if (current_config->name != pipe_config->name) { \
cfb23ed6 11961 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
08a24034
DV
11962 "(expected %i, found %i)\n", \
11963 current_config->name, \
11964 pipe_config->name); \
cfb23ed6
ML
11965 ret = false; \
11966 }
11967
8106ddbd
ACO
11968#define PIPE_CONF_CHECK_P(name) \
11969 if (current_config->name != pipe_config->name) { \
11970 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
11971 "(expected %p, found %p)\n", \
11972 current_config->name, \
11973 pipe_config->name); \
11974 ret = false; \
11975 }
11976
cfb23ed6
ML
11977#define PIPE_CONF_CHECK_M_N(name) \
11978 if (!intel_compare_link_m_n(&current_config->name, \
11979 &pipe_config->name,\
11980 adjust)) { \
11981 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
11982 "(expected tu %i gmch %i/%i link %i/%i, " \
11983 "found tu %i, gmch %i/%i link %i/%i)\n", \
11984 current_config->name.tu, \
11985 current_config->name.gmch_m, \
11986 current_config->name.gmch_n, \
11987 current_config->name.link_m, \
11988 current_config->name.link_n, \
11989 pipe_config->name.tu, \
11990 pipe_config->name.gmch_m, \
11991 pipe_config->name.gmch_n, \
11992 pipe_config->name.link_m, \
11993 pipe_config->name.link_n); \
11994 ret = false; \
11995 }
11996
55c561a7
DV
11997/* This is required for BDW+ where there is only one set of registers for
11998 * switching between high and low RR.
11999 * This macro can be used whenever a comparison has to be made between one
12000 * hw state and multiple sw state variables.
12001 */
cfb23ed6
ML
12002#define PIPE_CONF_CHECK_M_N_ALT(name, alt_name) \
12003 if (!intel_compare_link_m_n(&current_config->name, \
12004 &pipe_config->name, adjust) && \
12005 !intel_compare_link_m_n(&current_config->alt_name, \
12006 &pipe_config->name, adjust)) { \
12007 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
12008 "(expected tu %i gmch %i/%i link %i/%i, " \
12009 "or tu %i gmch %i/%i link %i/%i, " \
12010 "found tu %i, gmch %i/%i link %i/%i)\n", \
12011 current_config->name.tu, \
12012 current_config->name.gmch_m, \
12013 current_config->name.gmch_n, \
12014 current_config->name.link_m, \
12015 current_config->name.link_n, \
12016 current_config->alt_name.tu, \
12017 current_config->alt_name.gmch_m, \
12018 current_config->alt_name.gmch_n, \
12019 current_config->alt_name.link_m, \
12020 current_config->alt_name.link_n, \
12021 pipe_config->name.tu, \
12022 pipe_config->name.gmch_m, \
12023 pipe_config->name.gmch_n, \
12024 pipe_config->name.link_m, \
12025 pipe_config->name.link_n); \
12026 ret = false; \
88adfff1
DV
12027 }
12028
1bd1bd80
DV
12029#define PIPE_CONF_CHECK_FLAGS(name, mask) \
12030 if ((current_config->name ^ pipe_config->name) & (mask)) { \
cfb23ed6 12031 INTEL_ERR_OR_DBG_KMS("mismatch in " #name "(" #mask ") " \
1bd1bd80
DV
12032 "(expected %i, found %i)\n", \
12033 current_config->name & (mask), \
12034 pipe_config->name & (mask)); \
cfb23ed6 12035 ret = false; \
1bd1bd80
DV
12036 }
12037
5e550656
VS
12038#define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
12039 if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
cfb23ed6 12040 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
5e550656
VS
12041 "(expected %i, found %i)\n", \
12042 current_config->name, \
12043 pipe_config->name); \
cfb23ed6 12044 ret = false; \
5e550656
VS
12045 }
12046
bb760063
DV
12047#define PIPE_CONF_QUIRK(quirk) \
12048 ((current_config->quirks | pipe_config->quirks) & (quirk))
12049
eccb140b
DV
12050 PIPE_CONF_CHECK_I(cpu_transcoder);
12051
08a24034
DV
12052 PIPE_CONF_CHECK_I(has_pch_encoder);
12053 PIPE_CONF_CHECK_I(fdi_lanes);
cfb23ed6 12054 PIPE_CONF_CHECK_M_N(fdi_m_n);
08a24034 12055
eb14cb74 12056 PIPE_CONF_CHECK_I(has_dp_encoder);
90a6b7b0 12057 PIPE_CONF_CHECK_I(lane_count);
b95af8be
VK
12058
12059 if (INTEL_INFO(dev)->gen < 8) {
cfb23ed6
ML
12060 PIPE_CONF_CHECK_M_N(dp_m_n);
12061
cfb23ed6
ML
12062 if (current_config->has_drrs)
12063 PIPE_CONF_CHECK_M_N(dp_m2_n2);
12064 } else
12065 PIPE_CONF_CHECK_M_N_ALT(dp_m_n, dp_m2_n2);
eb14cb74 12066
a65347ba
JN
12067 PIPE_CONF_CHECK_I(has_dsi_encoder);
12068
2d112de7
ACO
12069 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
12070 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
12071 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
12072 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
12073 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
12074 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
1bd1bd80 12075
2d112de7
ACO
12076 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
12077 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
12078 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
12079 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
12080 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
12081 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
1bd1bd80 12082
c93f54cf 12083 PIPE_CONF_CHECK_I(pixel_multiplier);
6897b4b5 12084 PIPE_CONF_CHECK_I(has_hdmi_sink);
b5a9fa09 12085 if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
666a4537 12086 IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev))
b5a9fa09 12087 PIPE_CONF_CHECK_I(limited_color_range);
e43823ec 12088 PIPE_CONF_CHECK_I(has_infoframe);
6c49f241 12089
9ed109a7
DV
12090 PIPE_CONF_CHECK_I(has_audio);
12091
2d112de7 12092 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
1bd1bd80
DV
12093 DRM_MODE_FLAG_INTERLACE);
12094
bb760063 12095 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
2d112de7 12096 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063 12097 DRM_MODE_FLAG_PHSYNC);
2d112de7 12098 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063 12099 DRM_MODE_FLAG_NHSYNC);
2d112de7 12100 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063 12101 DRM_MODE_FLAG_PVSYNC);
2d112de7 12102 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063
DV
12103 DRM_MODE_FLAG_NVSYNC);
12104 }
045ac3b5 12105
333b8ca8 12106 PIPE_CONF_CHECK_X(gmch_pfit.control);
e2ff2d4a
DV
12107 /* pfit ratios are autocomputed by the hw on gen4+ */
12108 if (INTEL_INFO(dev)->gen < 4)
7f7d8dd6 12109 PIPE_CONF_CHECK_X(gmch_pfit.pgm_ratios);
333b8ca8 12110 PIPE_CONF_CHECK_X(gmch_pfit.lvds_border_bits);
9953599b 12111
bfd16b2a
ML
12112 if (!adjust) {
12113 PIPE_CONF_CHECK_I(pipe_src_w);
12114 PIPE_CONF_CHECK_I(pipe_src_h);
12115
12116 PIPE_CONF_CHECK_I(pch_pfit.enabled);
12117 if (current_config->pch_pfit.enabled) {
12118 PIPE_CONF_CHECK_X(pch_pfit.pos);
12119 PIPE_CONF_CHECK_X(pch_pfit.size);
12120 }
2fa2fe9a 12121
7aefe2b5
ML
12122 PIPE_CONF_CHECK_I(scaler_state.scaler_id);
12123 }
a1b2278e 12124
e59150dc
JB
12125 /* BDW+ don't expose a synchronous way to read the state */
12126 if (IS_HASWELL(dev))
12127 PIPE_CONF_CHECK_I(ips_enabled);
42db64ef 12128
282740f7
VS
12129 PIPE_CONF_CHECK_I(double_wide);
12130
26804afd
DV
12131 PIPE_CONF_CHECK_X(ddi_pll_sel);
12132
8106ddbd 12133 PIPE_CONF_CHECK_P(shared_dpll);
66e985c0 12134 PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
8bcc2795 12135 PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
66e985c0
DV
12136 PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
12137 PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
d452c5b6 12138 PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
00490c22 12139 PIPE_CONF_CHECK_X(dpll_hw_state.spll);
3f4cd19f
DL
12140 PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
12141 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
12142 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
c0d43d62 12143
47eacbab
VS
12144 PIPE_CONF_CHECK_X(dsi_pll.ctrl);
12145 PIPE_CONF_CHECK_X(dsi_pll.div);
12146
42571aef
VS
12147 if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
12148 PIPE_CONF_CHECK_I(pipe_bpp);
12149
2d112de7 12150 PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
a9a7e98a 12151 PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
5e550656 12152
66e985c0 12153#undef PIPE_CONF_CHECK_X
08a24034 12154#undef PIPE_CONF_CHECK_I
8106ddbd 12155#undef PIPE_CONF_CHECK_P
1bd1bd80 12156#undef PIPE_CONF_CHECK_FLAGS
5e550656 12157#undef PIPE_CONF_CHECK_CLOCK_FUZZY
bb760063 12158#undef PIPE_CONF_QUIRK
cfb23ed6 12159#undef INTEL_ERR_OR_DBG_KMS
88adfff1 12160
cfb23ed6 12161 return ret;
0e8ffe1b
DV
12162}
12163
e3b247da
VS
12164static void intel_pipe_config_sanity_check(struct drm_i915_private *dev_priv,
12165 const struct intel_crtc_state *pipe_config)
12166{
12167 if (pipe_config->has_pch_encoder) {
21a727b3 12168 int fdi_dotclock = intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
e3b247da
VS
12169 &pipe_config->fdi_m_n);
12170 int dotclock = pipe_config->base.adjusted_mode.crtc_clock;
12171
12172 /*
12173 * FDI already provided one idea for the dotclock.
12174 * Yell if the encoder disagrees.
12175 */
12176 WARN(!intel_fuzzy_clock_check(fdi_dotclock, dotclock),
12177 "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
12178 fdi_dotclock, dotclock);
12179 }
12180}
12181
c0ead703
ML
12182static void verify_wm_state(struct drm_crtc *crtc,
12183 struct drm_crtc_state *new_state)
08db6652 12184{
e7c84544 12185 struct drm_device *dev = crtc->dev;
08db6652
DL
12186 struct drm_i915_private *dev_priv = dev->dev_private;
12187 struct skl_ddb_allocation hw_ddb, *sw_ddb;
e7c84544
ML
12188 struct skl_ddb_entry *hw_entry, *sw_entry;
12189 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
12190 const enum pipe pipe = intel_crtc->pipe;
08db6652
DL
12191 int plane;
12192
e7c84544 12193 if (INTEL_INFO(dev)->gen < 9 || !new_state->active)
08db6652
DL
12194 return;
12195
12196 skl_ddb_get_hw_state(dev_priv, &hw_ddb);
12197 sw_ddb = &dev_priv->wm.skl_hw.ddb;
12198
e7c84544
ML
12199 /* planes */
12200 for_each_plane(dev_priv, pipe, plane) {
12201 hw_entry = &hw_ddb.plane[pipe][plane];
12202 sw_entry = &sw_ddb->plane[pipe][plane];
08db6652 12203
e7c84544 12204 if (skl_ddb_entry_equal(hw_entry, sw_entry))
08db6652
DL
12205 continue;
12206
e7c84544
ML
12207 DRM_ERROR("mismatch in DDB state pipe %c plane %d "
12208 "(expected (%u,%u), found (%u,%u))\n",
12209 pipe_name(pipe), plane + 1,
12210 sw_entry->start, sw_entry->end,
12211 hw_entry->start, hw_entry->end);
12212 }
08db6652 12213
e7c84544
ML
12214 /* cursor */
12215 hw_entry = &hw_ddb.plane[pipe][PLANE_CURSOR];
12216 sw_entry = &sw_ddb->plane[pipe][PLANE_CURSOR];
08db6652 12217
e7c84544 12218 if (!skl_ddb_entry_equal(hw_entry, sw_entry)) {
08db6652
DL
12219 DRM_ERROR("mismatch in DDB state pipe %c cursor "
12220 "(expected (%u,%u), found (%u,%u))\n",
12221 pipe_name(pipe),
12222 sw_entry->start, sw_entry->end,
12223 hw_entry->start, hw_entry->end);
12224 }
12225}
12226
91d1b4bd 12227static void
c0ead703 12228verify_connector_state(struct drm_device *dev, struct drm_crtc *crtc)
8af6cf88 12229{
35dd3c64 12230 struct drm_connector *connector;
8af6cf88 12231
e7c84544 12232 drm_for_each_connector(connector, dev) {
35dd3c64
ML
12233 struct drm_encoder *encoder = connector->encoder;
12234 struct drm_connector_state *state = connector->state;
ad3c558f 12235
e7c84544
ML
12236 if (state->crtc != crtc)
12237 continue;
12238
03f476e1
ML
12239 intel_connector_verify_state(to_intel_connector(connector),
12240 connector->state);
8af6cf88 12241
ad3c558f 12242 I915_STATE_WARN(state->best_encoder != encoder,
35dd3c64 12243 "connector's atomic encoder doesn't match legacy encoder\n");
8af6cf88 12244 }
91d1b4bd
DV
12245}
12246
12247static void
c0ead703 12248verify_encoder_state(struct drm_device *dev)
91d1b4bd
DV
12249{
12250 struct intel_encoder *encoder;
12251 struct intel_connector *connector;
8af6cf88 12252
b2784e15 12253 for_each_intel_encoder(dev, encoder) {
8af6cf88 12254 bool enabled = false;
4d20cd86 12255 enum pipe pipe;
8af6cf88
DV
12256
12257 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
12258 encoder->base.base.id,
8e329a03 12259 encoder->base.name);
8af6cf88 12260
3a3371ff 12261 for_each_intel_connector(dev, connector) {
4d20cd86 12262 if (connector->base.state->best_encoder != &encoder->base)
8af6cf88
DV
12263 continue;
12264 enabled = true;
ad3c558f
ML
12265
12266 I915_STATE_WARN(connector->base.state->crtc !=
12267 encoder->base.crtc,
12268 "connector's crtc doesn't match encoder crtc\n");
8af6cf88 12269 }
0e32b39c 12270
e2c719b7 12271 I915_STATE_WARN(!!encoder->base.crtc != enabled,
8af6cf88
DV
12272 "encoder's enabled state mismatch "
12273 "(expected %i, found %i)\n",
12274 !!encoder->base.crtc, enabled);
7c60d198
ML
12275
12276 if (!encoder->base.crtc) {
4d20cd86 12277 bool active;
7c60d198 12278
4d20cd86
ML
12279 active = encoder->get_hw_state(encoder, &pipe);
12280 I915_STATE_WARN(active,
12281 "encoder detached but still enabled on pipe %c.\n",
12282 pipe_name(pipe));
7c60d198 12283 }
8af6cf88 12284 }
91d1b4bd
DV
12285}
12286
12287static void
c0ead703
ML
12288verify_crtc_state(struct drm_crtc *crtc,
12289 struct drm_crtc_state *old_crtc_state,
12290 struct drm_crtc_state *new_crtc_state)
91d1b4bd 12291{
e7c84544 12292 struct drm_device *dev = crtc->dev;
fbee40df 12293 struct drm_i915_private *dev_priv = dev->dev_private;
91d1b4bd 12294 struct intel_encoder *encoder;
e7c84544
ML
12295 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
12296 struct intel_crtc_state *pipe_config, *sw_config;
12297 struct drm_atomic_state *old_state;
12298 bool active;
045ac3b5 12299
e7c84544
ML
12300 old_state = old_crtc_state->state;
12301 __drm_atomic_helper_crtc_destroy_state(crtc, old_crtc_state);
12302 pipe_config = to_intel_crtc_state(old_crtc_state);
12303 memset(pipe_config, 0, sizeof(*pipe_config));
12304 pipe_config->base.crtc = crtc;
12305 pipe_config->base.state = old_state;
8af6cf88 12306
e7c84544 12307 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
8af6cf88 12308
e7c84544 12309 active = dev_priv->display.get_pipe_config(intel_crtc, pipe_config);
d62cf62a 12310
e7c84544
ML
12311 /* hw state is inconsistent with the pipe quirk */
12312 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
12313 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
12314 active = new_crtc_state->active;
6c49f241 12315
e7c84544
ML
12316 I915_STATE_WARN(new_crtc_state->active != active,
12317 "crtc active state doesn't match with hw state "
12318 "(expected %i, found %i)\n", new_crtc_state->active, active);
0e8ffe1b 12319
e7c84544
ML
12320 I915_STATE_WARN(intel_crtc->active != new_crtc_state->active,
12321 "transitional active state does not match atomic hw state "
12322 "(expected %i, found %i)\n", new_crtc_state->active, intel_crtc->active);
4d20cd86 12323
e7c84544
ML
12324 for_each_encoder_on_crtc(dev, crtc, encoder) {
12325 enum pipe pipe;
4d20cd86 12326
e7c84544
ML
12327 active = encoder->get_hw_state(encoder, &pipe);
12328 I915_STATE_WARN(active != new_crtc_state->active,
12329 "[ENCODER:%i] active %i with crtc active %i\n",
12330 encoder->base.base.id, active, new_crtc_state->active);
4d20cd86 12331
e7c84544
ML
12332 I915_STATE_WARN(active && intel_crtc->pipe != pipe,
12333 "Encoder connected to wrong pipe %c\n",
12334 pipe_name(pipe));
4d20cd86 12335
e7c84544
ML
12336 if (active)
12337 encoder->get_config(encoder, pipe_config);
12338 }
53d9f4e9 12339
e7c84544
ML
12340 if (!new_crtc_state->active)
12341 return;
cfb23ed6 12342
e7c84544 12343 intel_pipe_config_sanity_check(dev_priv, pipe_config);
e3b247da 12344
e7c84544
ML
12345 sw_config = to_intel_crtc_state(crtc->state);
12346 if (!intel_pipe_config_compare(dev, sw_config,
12347 pipe_config, false)) {
12348 I915_STATE_WARN(1, "pipe state doesn't match!\n");
12349 intel_dump_pipe_config(intel_crtc, pipe_config,
12350 "[hw state]");
12351 intel_dump_pipe_config(intel_crtc, sw_config,
12352 "[sw state]");
8af6cf88
DV
12353 }
12354}
12355
91d1b4bd 12356static void
c0ead703
ML
12357verify_single_dpll_state(struct drm_i915_private *dev_priv,
12358 struct intel_shared_dpll *pll,
12359 struct drm_crtc *crtc,
12360 struct drm_crtc_state *new_state)
91d1b4bd 12361{
91d1b4bd 12362 struct intel_dpll_hw_state dpll_hw_state;
e7c84544
ML
12363 unsigned crtc_mask;
12364 bool active;
5358901f 12365
e7c84544 12366 memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
5358901f 12367
e7c84544 12368 DRM_DEBUG_KMS("%s\n", pll->name);
5358901f 12369
e7c84544 12370 active = pll->funcs.get_hw_state(dev_priv, pll, &dpll_hw_state);
5358901f 12371
e7c84544
ML
12372 if (!(pll->flags & INTEL_DPLL_ALWAYS_ON)) {
12373 I915_STATE_WARN(!pll->on && pll->active_mask,
12374 "pll in active use but not on in sw tracking\n");
12375 I915_STATE_WARN(pll->on && !pll->active_mask,
12376 "pll is on but not used by any active crtc\n");
12377 I915_STATE_WARN(pll->on != active,
12378 "pll on state mismatch (expected %i, found %i)\n",
12379 pll->on, active);
12380 }
5358901f 12381
e7c84544 12382 if (!crtc) {
2dd66ebd 12383 I915_STATE_WARN(pll->active_mask & ~pll->config.crtc_mask,
e7c84544
ML
12384 "more active pll users than references: %x vs %x\n",
12385 pll->active_mask, pll->config.crtc_mask);
5358901f 12386
e7c84544
ML
12387 return;
12388 }
12389
12390 crtc_mask = 1 << drm_crtc_index(crtc);
12391
12392 if (new_state->active)
12393 I915_STATE_WARN(!(pll->active_mask & crtc_mask),
12394 "pll active mismatch (expected pipe %c in active mask 0x%02x)\n",
12395 pipe_name(drm_crtc_index(crtc)), pll->active_mask);
12396 else
12397 I915_STATE_WARN(pll->active_mask & crtc_mask,
12398 "pll active mismatch (didn't expect pipe %c in active mask 0x%02x)\n",
12399 pipe_name(drm_crtc_index(crtc)), pll->active_mask);
2dd66ebd 12400
e7c84544
ML
12401 I915_STATE_WARN(!(pll->config.crtc_mask & crtc_mask),
12402 "pll enabled crtcs mismatch (expected 0x%x in 0x%02x)\n",
12403 crtc_mask, pll->config.crtc_mask);
66e985c0 12404
e7c84544
ML
12405 I915_STATE_WARN(pll->on && memcmp(&pll->config.hw_state,
12406 &dpll_hw_state,
12407 sizeof(dpll_hw_state)),
12408 "pll hw state mismatch\n");
12409}
12410
12411static void
c0ead703
ML
12412verify_shared_dpll_state(struct drm_device *dev, struct drm_crtc *crtc,
12413 struct drm_crtc_state *old_crtc_state,
12414 struct drm_crtc_state *new_crtc_state)
e7c84544
ML
12415{
12416 struct drm_i915_private *dev_priv = dev->dev_private;
12417 struct intel_crtc_state *old_state = to_intel_crtc_state(old_crtc_state);
12418 struct intel_crtc_state *new_state = to_intel_crtc_state(new_crtc_state);
12419
12420 if (new_state->shared_dpll)
c0ead703 12421 verify_single_dpll_state(dev_priv, new_state->shared_dpll, crtc, new_crtc_state);
e7c84544
ML
12422
12423 if (old_state->shared_dpll &&
12424 old_state->shared_dpll != new_state->shared_dpll) {
12425 unsigned crtc_mask = 1 << drm_crtc_index(crtc);
12426 struct intel_shared_dpll *pll = old_state->shared_dpll;
12427
12428 I915_STATE_WARN(pll->active_mask & crtc_mask,
12429 "pll active mismatch (didn't expect pipe %c in active mask)\n",
12430 pipe_name(drm_crtc_index(crtc)));
12431 I915_STATE_WARN(pll->config.crtc_mask & crtc_mask,
12432 "pll enabled crtcs mismatch (found %x in enabled mask)\n",
12433 pipe_name(drm_crtc_index(crtc)));
5358901f 12434 }
8af6cf88
DV
12435}
12436
e7c84544 12437static void
c0ead703 12438intel_modeset_verify_crtc(struct drm_crtc *crtc,
e7c84544
ML
12439 struct drm_crtc_state *old_state,
12440 struct drm_crtc_state *new_state)
12441{
c0ead703 12442 verify_wm_state(crtc, new_state);
c0ead703
ML
12443 verify_crtc_state(crtc, old_state, new_state);
12444 verify_shared_dpll_state(crtc->dev, crtc, old_state, new_state);
e7c84544
ML
12445}
12446
12447static void
c0ead703 12448verify_disabled_dpll_state(struct drm_device *dev)
e7c84544
ML
12449{
12450 struct drm_i915_private *dev_priv = dev->dev_private;
12451 int i;
12452
12453 for (i = 0; i < dev_priv->num_shared_dpll; i++)
c0ead703 12454 verify_single_dpll_state(dev_priv, &dev_priv->shared_dplls[i], NULL, NULL);
e7c84544
ML
12455}
12456
12457static void
c0ead703 12458intel_modeset_verify_disabled(struct drm_device *dev)
e7c84544 12459{
c0ead703
ML
12460 verify_encoder_state(dev);
12461 verify_connector_state(dev, NULL);
12462 verify_disabled_dpll_state(dev);
e7c84544
ML
12463}
12464
80715b2f
VS
12465static void update_scanline_offset(struct intel_crtc *crtc)
12466{
12467 struct drm_device *dev = crtc->base.dev;
12468
12469 /*
12470 * The scanline counter increments at the leading edge of hsync.
12471 *
12472 * On most platforms it starts counting from vtotal-1 on the
12473 * first active line. That means the scanline counter value is
12474 * always one less than what we would expect. Ie. just after
12475 * start of vblank, which also occurs at start of hsync (on the
12476 * last active line), the scanline counter will read vblank_start-1.
12477 *
12478 * On gen2 the scanline counter starts counting from 1 instead
12479 * of vtotal-1, so we have to subtract one (or rather add vtotal-1
12480 * to keep the value positive), instead of adding one.
12481 *
12482 * On HSW+ the behaviour of the scanline counter depends on the output
12483 * type. For DP ports it behaves like most other platforms, but on HDMI
12484 * there's an extra 1 line difference. So we need to add two instead of
12485 * one to the value.
12486 */
12487 if (IS_GEN2(dev)) {
124abe07 12488 const struct drm_display_mode *adjusted_mode = &crtc->config->base.adjusted_mode;
80715b2f
VS
12489 int vtotal;
12490
124abe07
VS
12491 vtotal = adjusted_mode->crtc_vtotal;
12492 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
80715b2f
VS
12493 vtotal /= 2;
12494
12495 crtc->scanline_offset = vtotal - 1;
12496 } else if (HAS_DDI(dev) &&
409ee761 12497 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI)) {
80715b2f
VS
12498 crtc->scanline_offset = 2;
12499 } else
12500 crtc->scanline_offset = 1;
12501}
12502
ad421372 12503static void intel_modeset_clear_plls(struct drm_atomic_state *state)
ed6739ef 12504{
225da59b 12505 struct drm_device *dev = state->dev;
ed6739ef 12506 struct drm_i915_private *dev_priv = to_i915(dev);
ad421372 12507 struct intel_shared_dpll_config *shared_dpll = NULL;
0a9ab303
ACO
12508 struct drm_crtc *crtc;
12509 struct drm_crtc_state *crtc_state;
0a9ab303 12510 int i;
ed6739ef
ACO
12511
12512 if (!dev_priv->display.crtc_compute_clock)
ad421372 12513 return;
ed6739ef 12514
0a9ab303 12515 for_each_crtc_in_state(state, crtc, crtc_state, i) {
fb1a38a9 12516 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8106ddbd
ACO
12517 struct intel_shared_dpll *old_dpll =
12518 to_intel_crtc_state(crtc->state)->shared_dpll;
0a9ab303 12519
fb1a38a9 12520 if (!needs_modeset(crtc_state))
225da59b
ACO
12521 continue;
12522
8106ddbd 12523 to_intel_crtc_state(crtc_state)->shared_dpll = NULL;
fb1a38a9 12524
8106ddbd 12525 if (!old_dpll)
fb1a38a9 12526 continue;
0a9ab303 12527
ad421372
ML
12528 if (!shared_dpll)
12529 shared_dpll = intel_atomic_get_shared_dpll_state(state);
ed6739ef 12530
8106ddbd 12531 intel_shared_dpll_config_put(shared_dpll, old_dpll, intel_crtc);
ad421372 12532 }
ed6739ef
ACO
12533}
12534
99d736a2
ML
12535/*
12536 * This implements the workaround described in the "notes" section of the mode
12537 * set sequence documentation. When going from no pipes or single pipe to
12538 * multiple pipes, and planes are enabled after the pipe, we need to wait at
12539 * least 2 vblanks on the first pipe before enabling planes on the second pipe.
12540 */
12541static int haswell_mode_set_planes_workaround(struct drm_atomic_state *state)
12542{
12543 struct drm_crtc_state *crtc_state;
12544 struct intel_crtc *intel_crtc;
12545 struct drm_crtc *crtc;
12546 struct intel_crtc_state *first_crtc_state = NULL;
12547 struct intel_crtc_state *other_crtc_state = NULL;
12548 enum pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
12549 int i;
12550
12551 /* look at all crtc's that are going to be enabled in during modeset */
12552 for_each_crtc_in_state(state, crtc, crtc_state, i) {
12553 intel_crtc = to_intel_crtc(crtc);
12554
12555 if (!crtc_state->active || !needs_modeset(crtc_state))
12556 continue;
12557
12558 if (first_crtc_state) {
12559 other_crtc_state = to_intel_crtc_state(crtc_state);
12560 break;
12561 } else {
12562 first_crtc_state = to_intel_crtc_state(crtc_state);
12563 first_pipe = intel_crtc->pipe;
12564 }
12565 }
12566
12567 /* No workaround needed? */
12568 if (!first_crtc_state)
12569 return 0;
12570
12571 /* w/a possibly needed, check how many crtc's are already enabled. */
12572 for_each_intel_crtc(state->dev, intel_crtc) {
12573 struct intel_crtc_state *pipe_config;
12574
12575 pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
12576 if (IS_ERR(pipe_config))
12577 return PTR_ERR(pipe_config);
12578
12579 pipe_config->hsw_workaround_pipe = INVALID_PIPE;
12580
12581 if (!pipe_config->base.active ||
12582 needs_modeset(&pipe_config->base))
12583 continue;
12584
12585 /* 2 or more enabled crtcs means no need for w/a */
12586 if (enabled_pipe != INVALID_PIPE)
12587 return 0;
12588
12589 enabled_pipe = intel_crtc->pipe;
12590 }
12591
12592 if (enabled_pipe != INVALID_PIPE)
12593 first_crtc_state->hsw_workaround_pipe = enabled_pipe;
12594 else if (other_crtc_state)
12595 other_crtc_state->hsw_workaround_pipe = first_pipe;
12596
12597 return 0;
12598}
12599
27c329ed
ML
12600static int intel_modeset_all_pipes(struct drm_atomic_state *state)
12601{
12602 struct drm_crtc *crtc;
12603 struct drm_crtc_state *crtc_state;
12604 int ret = 0;
12605
12606 /* add all active pipes to the state */
12607 for_each_crtc(state->dev, crtc) {
12608 crtc_state = drm_atomic_get_crtc_state(state, crtc);
12609 if (IS_ERR(crtc_state))
12610 return PTR_ERR(crtc_state);
12611
12612 if (!crtc_state->active || needs_modeset(crtc_state))
12613 continue;
12614
12615 crtc_state->mode_changed = true;
12616
12617 ret = drm_atomic_add_affected_connectors(state, crtc);
12618 if (ret)
12619 break;
12620
12621 ret = drm_atomic_add_affected_planes(state, crtc);
12622 if (ret)
12623 break;
12624 }
12625
12626 return ret;
12627}
12628
c347a676 12629static int intel_modeset_checks(struct drm_atomic_state *state)
054518dd 12630{
565602d7
ML
12631 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
12632 struct drm_i915_private *dev_priv = state->dev->dev_private;
12633 struct drm_crtc *crtc;
12634 struct drm_crtc_state *crtc_state;
12635 int ret = 0, i;
054518dd 12636
b359283a
ML
12637 if (!check_digital_port_conflicts(state)) {
12638 DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
12639 return -EINVAL;
12640 }
12641
565602d7
ML
12642 intel_state->modeset = true;
12643 intel_state->active_crtcs = dev_priv->active_crtcs;
12644
12645 for_each_crtc_in_state(state, crtc, crtc_state, i) {
12646 if (crtc_state->active)
12647 intel_state->active_crtcs |= 1 << i;
12648 else
12649 intel_state->active_crtcs &= ~(1 << i);
8b4a7d05
MR
12650
12651 if (crtc_state->active != crtc->state->active)
12652 intel_state->active_pipe_changes |= drm_crtc_mask(crtc);
565602d7
ML
12653 }
12654
054518dd
ACO
12655 /*
12656 * See if the config requires any additional preparation, e.g.
12657 * to adjust global state with pipes off. We need to do this
12658 * here so we can get the modeset_pipe updated config for the new
12659 * mode set on this crtc. For other crtcs we need to use the
12660 * adjusted_mode bits in the crtc directly.
12661 */
27c329ed 12662 if (dev_priv->display.modeset_calc_cdclk) {
c89e39f3
CT
12663 if (!intel_state->cdclk_pll_vco)
12664 intel_state->cdclk_pll_vco = dev_priv->skl_vco_freq;
b2045352
VS
12665 if (!intel_state->cdclk_pll_vco)
12666 intel_state->cdclk_pll_vco = dev_priv->skl_preferred_vco_freq;
c89e39f3 12667
27c329ed 12668 ret = dev_priv->display.modeset_calc_cdclk(state);
c89e39f3
CT
12669 if (ret < 0)
12670 return ret;
27c329ed 12671
c89e39f3
CT
12672 if (intel_state->dev_cdclk != dev_priv->cdclk_freq ||
12673 intel_state->cdclk_pll_vco != dev_priv->skl_vco_freq)
27c329ed
ML
12674 ret = intel_modeset_all_pipes(state);
12675
12676 if (ret < 0)
054518dd 12677 return ret;
e8788cbc
ML
12678
12679 DRM_DEBUG_KMS("New cdclk calculated to be atomic %u, actual %u\n",
12680 intel_state->cdclk, intel_state->dev_cdclk);
27c329ed 12681 } else
1a617b77 12682 to_intel_atomic_state(state)->cdclk = dev_priv->atomic_cdclk_freq;
054518dd 12683
ad421372 12684 intel_modeset_clear_plls(state);
054518dd 12685
565602d7 12686 if (IS_HASWELL(dev_priv))
ad421372 12687 return haswell_mode_set_planes_workaround(state);
99d736a2 12688
ad421372 12689 return 0;
c347a676
ACO
12690}
12691
aa363136
MR
12692/*
12693 * Handle calculation of various watermark data at the end of the atomic check
12694 * phase. The code here should be run after the per-crtc and per-plane 'check'
12695 * handlers to ensure that all derived state has been updated.
12696 */
55994c2c 12697static int calc_watermark_data(struct drm_atomic_state *state)
aa363136
MR
12698{
12699 struct drm_device *dev = state->dev;
98d39494 12700 struct drm_i915_private *dev_priv = to_i915(dev);
98d39494
MR
12701
12702 /* Is there platform-specific watermark information to calculate? */
12703 if (dev_priv->display.compute_global_watermarks)
55994c2c
MR
12704 return dev_priv->display.compute_global_watermarks(state);
12705
12706 return 0;
aa363136
MR
12707}
12708
74c090b1
ML
12709/**
12710 * intel_atomic_check - validate state object
12711 * @dev: drm device
12712 * @state: state to validate
12713 */
12714static int intel_atomic_check(struct drm_device *dev,
12715 struct drm_atomic_state *state)
c347a676 12716{
dd8b3bdb 12717 struct drm_i915_private *dev_priv = to_i915(dev);
aa363136 12718 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
c347a676
ACO
12719 struct drm_crtc *crtc;
12720 struct drm_crtc_state *crtc_state;
12721 int ret, i;
61333b60 12722 bool any_ms = false;
c347a676 12723
74c090b1 12724 ret = drm_atomic_helper_check_modeset(dev, state);
054518dd
ACO
12725 if (ret)
12726 return ret;
12727
c347a676 12728 for_each_crtc_in_state(state, crtc, crtc_state, i) {
cfb23ed6
ML
12729 struct intel_crtc_state *pipe_config =
12730 to_intel_crtc_state(crtc_state);
1ed51de9
DV
12731
12732 /* Catch I915_MODE_FLAG_INHERITED */
12733 if (crtc_state->mode.private_flags != crtc->state->mode.private_flags)
12734 crtc_state->mode_changed = true;
cfb23ed6 12735
af4a879e 12736 if (!needs_modeset(crtc_state))
c347a676
ACO
12737 continue;
12738
af4a879e
DV
12739 if (!crtc_state->enable) {
12740 any_ms = true;
cfb23ed6 12741 continue;
af4a879e 12742 }
cfb23ed6 12743
26495481
DV
12744 /* FIXME: For only active_changed we shouldn't need to do any
12745 * state recomputation at all. */
12746
1ed51de9
DV
12747 ret = drm_atomic_add_affected_connectors(state, crtc);
12748 if (ret)
12749 return ret;
b359283a 12750
cfb23ed6 12751 ret = intel_modeset_pipe_config(crtc, pipe_config);
25aa1c39
ML
12752 if (ret) {
12753 intel_dump_pipe_config(to_intel_crtc(crtc),
12754 pipe_config, "[failed]");
c347a676 12755 return ret;
25aa1c39 12756 }
c347a676 12757
73831236 12758 if (i915.fastboot &&
dd8b3bdb 12759 intel_pipe_config_compare(dev,
cfb23ed6 12760 to_intel_crtc_state(crtc->state),
1ed51de9 12761 pipe_config, true)) {
26495481 12762 crtc_state->mode_changed = false;
bfd16b2a 12763 to_intel_crtc_state(crtc_state)->update_pipe = true;
26495481
DV
12764 }
12765
af4a879e 12766 if (needs_modeset(crtc_state))
26495481 12767 any_ms = true;
cfb23ed6 12768
af4a879e
DV
12769 ret = drm_atomic_add_affected_planes(state, crtc);
12770 if (ret)
12771 return ret;
61333b60 12772
26495481
DV
12773 intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
12774 needs_modeset(crtc_state) ?
12775 "[modeset]" : "[fastset]");
c347a676
ACO
12776 }
12777
61333b60
ML
12778 if (any_ms) {
12779 ret = intel_modeset_checks(state);
12780
12781 if (ret)
12782 return ret;
27c329ed 12783 } else
dd8b3bdb 12784 intel_state->cdclk = dev_priv->cdclk_freq;
76305b1a 12785
dd8b3bdb 12786 ret = drm_atomic_helper_check_planes(dev, state);
aa363136
MR
12787 if (ret)
12788 return ret;
12789
f51be2e0 12790 intel_fbc_choose_crtc(dev_priv, state);
55994c2c 12791 return calc_watermark_data(state);
054518dd
ACO
12792}
12793
a6747b73
ML
12794static bool needs_work(struct drm_crtc_state *crtc_state)
12795{
12796 /* hw state checker needs to run */
12797 if (needs_modeset(crtc_state))
12798 return true;
12799
12800 /* unpin old fb's, possibly vblank update */
12801 if (crtc_state->planes_changed)
12802 return true;
12803
12804 /* pipe parameters need to be updated, and hw state checker */
12805 if (to_intel_crtc_state(crtc_state)->update_pipe)
12806 return true;
12807
12808 /* vblank event requested? */
12809 if (crtc_state->event)
12810 return true;
12811
12812 return false;
12813}
12814
5008e874
ML
12815static int intel_atomic_prepare_commit(struct drm_device *dev,
12816 struct drm_atomic_state *state,
81072bfd 12817 bool nonblock)
5008e874 12818{
7580d774 12819 struct drm_i915_private *dev_priv = dev->dev_private;
a6747b73 12820 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
7580d774 12821 struct drm_plane_state *plane_state;
5008e874 12822 struct drm_crtc_state *crtc_state;
7580d774 12823 struct drm_plane *plane;
5008e874
ML
12824 struct drm_crtc *crtc;
12825 int i, ret;
12826
5008e874 12827 for_each_crtc_in_state(state, crtc, crtc_state, i) {
a6747b73
ML
12828 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
12829 struct intel_flip_work *work;
12830
95c2ccdc
ML
12831 if (!state->legacy_cursor_update) {
12832 ret = intel_crtc_wait_for_pending_flips(crtc);
12833 if (ret)
12834 return ret;
7580d774 12835
95c2ccdc
ML
12836 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
12837 flush_workqueue(dev_priv->wq);
12838 }
a6747b73
ML
12839
12840 /* test if we need to update something */
12841 if (!needs_work(crtc_state))
12842 continue;
12843
12844 intel_state->work[i] = work =
12845 kzalloc(sizeof(**intel_state->work), GFP_KERNEL);
12846
12847 if (!work)
12848 return -ENOMEM;
12849
12850 if (needs_modeset(crtc_state) ||
12851 to_intel_crtc_state(crtc_state)->update_pipe) {
12852 work->num_old_connectors = hweight32(crtc->state->connector_mask);
12853
12854 work->old_connector_state = kcalloc(work->num_old_connectors,
12855 sizeof(*work->old_connector_state),
12856 GFP_KERNEL);
12857
12858 work->num_new_connectors = hweight32(crtc_state->connector_mask);
12859 work->new_connector_state = kcalloc(work->num_new_connectors,
12860 sizeof(*work->new_connector_state),
12861 GFP_KERNEL);
12862
12863 if (!work->old_connector_state || !work->new_connector_state)
12864 return -ENOMEM;
12865 }
5008e874
ML
12866 }
12867
d55dbd06
ML
12868 if (intel_state->modeset && nonblock) {
12869 DRM_DEBUG_ATOMIC("Nonblock modesets are not yet supported!\n");
12870 return -EINVAL;
12871 }
12872
f935675f
ML
12873 ret = mutex_lock_interruptible(&dev->struct_mutex);
12874 if (ret)
12875 return ret;
12876
5008e874 12877 ret = drm_atomic_helper_prepare_planes(dev, state);
f7e5838b 12878 mutex_unlock(&dev->struct_mutex);
7580d774 12879
21daaeee 12880 if (!ret && !nonblock) {
7580d774
ML
12881 for_each_plane_in_state(state, plane, plane_state, i) {
12882 struct intel_plane_state *intel_plane_state =
12883 to_intel_plane_state(plane_state);
12884
84fc494b
ML
12885 if (plane_state->fence) {
12886 long lret = fence_wait(plane_state->fence, true);
12887
12888 if (lret < 0) {
12889 ret = lret;
12890 break;
12891 }
12892 }
12893
7580d774
ML
12894 if (!intel_plane_state->wait_req)
12895 continue;
12896
12897 ret = __i915_wait_request(intel_plane_state->wait_req,
299259a3 12898 true, NULL, NULL);
f7e5838b 12899 if (ret) {
f4457ae7
CW
12900 /* Any hang should be swallowed by the wait */
12901 WARN_ON(ret == -EIO);
f7e5838b
CW
12902 mutex_lock(&dev->struct_mutex);
12903 drm_atomic_helper_cleanup_planes(dev, state);
12904 mutex_unlock(&dev->struct_mutex);
7580d774 12905 break;
f7e5838b 12906 }
7580d774 12907 }
7580d774 12908 }
5008e874
ML
12909
12910 return ret;
12911}
12912
a2991414
ML
12913u32 intel_crtc_get_vblank_counter(struct intel_crtc *crtc)
12914{
12915 struct drm_device *dev = crtc->base.dev;
12916
12917 if (!dev->max_vblank_count)
12918 return drm_accurate_vblank_count(&crtc->base);
12919
12920 return dev->driver->get_vblank_counter(dev, crtc->pipe);
12921}
12922
a6747b73
ML
12923static void intel_prepare_work(struct drm_crtc *crtc,
12924 struct intel_flip_work *work,
12925 struct drm_atomic_state *state,
12926 struct drm_crtc_state *old_crtc_state)
e8861675 12927{
a6747b73
ML
12928 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
12929 struct drm_plane_state *old_plane_state;
12930 struct drm_plane *plane;
12931 int i, j = 0;
e8861675 12932
a6747b73
ML
12933 INIT_WORK(&work->unpin_work, intel_unpin_work_fn);
12934 INIT_WORK(&work->mmio_work, intel_mmio_flip_work_func);
12935 atomic_inc(&intel_crtc->unpin_work_count);
e8861675 12936
a6747b73
ML
12937 for_each_plane_in_state(state, plane, old_plane_state, i) {
12938 struct intel_plane_state *old_state = to_intel_plane_state(old_plane_state);
12939 struct intel_plane_state *new_state = to_intel_plane_state(plane->state);
e8861675 12940
a6747b73
ML
12941 if (old_state->base.crtc != crtc &&
12942 new_state->base.crtc != crtc)
e8861675
ML
12943 continue;
12944
a6747b73
ML
12945 if (plane->type == DRM_PLANE_TYPE_PRIMARY) {
12946 plane->fb = new_state->base.fb;
12947 crtc->x = new_state->base.src_x >> 16;
12948 crtc->y = new_state->base.src_y >> 16;
e8861675
ML
12949 }
12950
a6747b73
ML
12951 old_state->wait_req = new_state->wait_req;
12952 new_state->wait_req = NULL;
12953
12954 old_state->base.fence = new_state->base.fence;
12955 new_state->base.fence = NULL;
12956
12957 /* remove plane state from the atomic state and move it to work */
12958 old_plane_state->state = NULL;
12959 state->planes[i] = NULL;
12960 state->plane_states[i] = NULL;
12961
12962 work->old_plane_state[j] = old_state;
12963 work->new_plane_state[j++] = new_state;
e8861675
ML
12964 }
12965
a6747b73
ML
12966 old_crtc_state->state = NULL;
12967 state->crtcs[drm_crtc_index(crtc)] = NULL;
12968 state->crtc_states[drm_crtc_index(crtc)] = NULL;
e8861675 12969
a6747b73
ML
12970 work->old_crtc_state = to_intel_crtc_state(old_crtc_state);
12971 work->new_crtc_state = to_intel_crtc_state(crtc->state);
12972 work->num_planes = j;
e8861675 12973
a6747b73
ML
12974 work->event = crtc->state->event;
12975 crtc->state->event = NULL;
e8861675 12976
a6747b73
ML
12977 if (needs_modeset(crtc->state) || work->new_crtc_state->update_pipe) {
12978 struct drm_connector *conn;
12979 struct drm_connector_state *old_conn_state;
12980 int k = 0;
e8861675 12981
a6747b73
ML
12982 j = 0;
12983
12984 /*
12985 * intel_unpin_work_fn cannot depend on the connector list
12986 * because it may be freed from underneath it, so add
12987 * them all to the work struct while we're holding locks.
12988 */
12989 for_each_connector_in_state(state, conn, old_conn_state, i) {
12990 if (old_conn_state->crtc == crtc) {
12991 work->old_connector_state[j++] = old_conn_state;
12992
12993 state->connectors[i] = NULL;
12994 state->connector_states[i] = NULL;
12995 }
12996 }
12997
12998 /* If another crtc has stolen the connector from state,
12999 * then for_each_connector_in_state is no longer reliable,
13000 * so use drm_for_each_connector here.
13001 */
13002 drm_for_each_connector(conn, state->dev)
13003 if (conn->state->crtc == crtc)
13004 work->new_connector_state[k++] = conn->state;
13005
13006 WARN(j != work->num_old_connectors, "j = %i, expected %i\n", j, work->num_old_connectors);
13007 WARN(k != work->num_new_connectors, "k = %i, expected %i\n", k, work->num_new_connectors);
13008 } else if (!work->new_crtc_state->update_wm_post)
13009 work->can_async_unpin = true;
13010
13011 work->fb_bits = work->new_crtc_state->fb_bits;
e8861675
ML
13012}
13013
a6747b73
ML
13014static void intel_schedule_unpin(struct drm_crtc *crtc,
13015 struct intel_atomic_state *state,
13016 struct intel_flip_work *work)
e8861675 13017{
a6747b73
ML
13018 struct drm_device *dev = crtc->dev;
13019 struct drm_i915_private *dev_priv = dev->dev_private;
e8861675 13020
a6747b73 13021 to_intel_crtc(crtc)->config = work->new_crtc_state;
e8861675 13022
a6747b73
ML
13023 queue_work(dev_priv->wq, &work->unpin_work);
13024}
e8861675 13025
d55dbd06
ML
13026static void intel_schedule_flip(struct drm_crtc *crtc,
13027 struct intel_atomic_state *state,
13028 struct intel_flip_work *work,
13029 bool nonblock)
13030{
13031 struct intel_crtc_state *crtc_state = work->new_crtc_state;
13032
13033 if (crtc_state->base.planes_changed ||
13034 needs_modeset(&crtc_state->base) ||
13035 crtc_state->update_pipe) {
13036 if (nonblock)
13037 schedule_work(&work->mmio_work);
13038 else
13039 intel_mmio_flip_work_func(&work->mmio_work);
13040 } else {
13041 int ret;
13042
13043 ret = drm_crtc_vblank_get(crtc);
13044 I915_STATE_WARN(ret < 0, "enabling vblank failed with %i\n", ret);
13045
13046 work->flip_queued_vblank = intel_crtc_get_vblank_counter(to_intel_crtc(crtc));
13047 smp_mb__before_atomic();
13048 atomic_set(&work->pending, 1);
13049 }
13050}
13051
a6747b73
ML
13052static void intel_schedule_update(struct drm_crtc *crtc,
13053 struct intel_atomic_state *state,
d55dbd06
ML
13054 struct intel_flip_work *work,
13055 bool nonblock)
a6747b73
ML
13056{
13057 struct drm_device *dev = crtc->dev;
d55dbd06 13058 struct intel_crtc_state *pipe_config = work->new_crtc_state;
a6747b73 13059
d55dbd06 13060 if (!pipe_config->base.active && work->can_async_unpin) {
a6747b73
ML
13061 INIT_LIST_HEAD(&work->head);
13062 intel_schedule_unpin(crtc, state, work);
13063 return;
13064 }
13065
13066 spin_lock_irq(&dev->event_lock);
13067 list_add_tail(&work->head, &to_intel_crtc(crtc)->flip_work);
13068 spin_unlock_irq(&dev->event_lock);
13069
d55dbd06
ML
13070 if (!pipe_config->base.active)
13071 intel_schedule_unpin(crtc, state, work);
13072 else
13073 intel_schedule_flip(crtc, state, work, nonblock);
e8861675
ML
13074}
13075
74c090b1
ML
13076/**
13077 * intel_atomic_commit - commit validated state object
13078 * @dev: DRM device
13079 * @state: the top-level driver state object
81072bfd 13080 * @nonblock: nonblocking commit
74c090b1
ML
13081 *
13082 * This function commits a top-level state object that has been validated
13083 * with drm_atomic_helper_check().
13084 *
13085 * FIXME: Atomic modeset support for i915 is not yet complete. At the moment
13086 * we can only handle plane-related operations and do not yet support
81072bfd 13087 * nonblocking commit.
74c090b1
ML
13088 *
13089 * RETURNS
13090 * Zero for success or -errno.
13091 */
13092static int intel_atomic_commit(struct drm_device *dev,
13093 struct drm_atomic_state *state,
81072bfd 13094 bool nonblock)
a6778b3c 13095{
565602d7 13096 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
fbee40df 13097 struct drm_i915_private *dev_priv = dev->dev_private;
29ceb0e6 13098 struct drm_crtc_state *old_crtc_state;
7580d774 13099 struct drm_crtc *crtc;
565602d7 13100 int ret = 0, i;
a6778b3c 13101
81072bfd 13102 ret = intel_atomic_prepare_commit(dev, state, nonblock);
7580d774
ML
13103 if (ret) {
13104 DRM_DEBUG_ATOMIC("Preparing state failed with %i\n", ret);
d4afb8cc 13105 return ret;
7580d774 13106 }
d4afb8cc 13107
1c5e19f8 13108 drm_atomic_helper_swap_state(dev, state);
279e99d7 13109 dev_priv->wm.distrust_bios_wm = false;
734fa01f 13110 dev_priv->wm.skl_results = intel_state->wm_results;
a1475e77 13111 intel_shared_dpll_commit(state);
1c5e19f8 13112
565602d7
ML
13113 if (intel_state->modeset) {
13114 memcpy(dev_priv->min_pixclk, intel_state->min_pixclk,
13115 sizeof(intel_state->min_pixclk));
13116 dev_priv->active_crtcs = intel_state->active_crtcs;
1a617b77 13117 dev_priv->atomic_cdclk_freq = intel_state->cdclk;
565602d7
ML
13118 }
13119
29ceb0e6 13120 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
a539205a
ML
13121 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13122
61333b60
ML
13123 if (!needs_modeset(crtc->state))
13124 continue;
13125
29ceb0e6 13126 intel_pre_plane_update(to_intel_crtc_state(old_crtc_state));
460da916 13127
a6747b73
ML
13128 intel_state->work[i]->put_power_domains =
13129 modeset_get_crtc_power_domains(crtc,
13130 to_intel_crtc_state(crtc->state));
13131
29ceb0e6
VS
13132 if (old_crtc_state->active) {
13133 intel_crtc_disable_planes(crtc, old_crtc_state->plane_mask);
a539205a 13134 dev_priv->display.crtc_disable(crtc);
eddfcbcd 13135 intel_crtc->active = false;
58f9c0bc 13136 intel_fbc_disable(intel_crtc);
eddfcbcd 13137 intel_disable_shared_dpll(intel_crtc);
9bbc8258
VS
13138
13139 /*
13140 * Underruns don't always raise
13141 * interrupts, so check manually.
13142 */
13143 intel_check_cpu_fifo_underruns(dev_priv);
13144 intel_check_pch_fifo_underruns(dev_priv);
b9001114
ML
13145
13146 if (!crtc->state->active)
13147 intel_update_watermarks(crtc);
a539205a 13148 }
b8cecdf5 13149 }
7758a113 13150
ea9d758d
DV
13151 /* Only after disabling all output pipelines that will be changed can we
13152 * update the the output configuration. */
4740b0f2 13153 intel_modeset_update_crtc_state(state);
f6e5b160 13154
565602d7 13155 if (intel_state->modeset) {
4740b0f2 13156 drm_atomic_helper_update_legacy_modeset_state(state->dev, state);
33c8df89
ML
13157
13158 if (dev_priv->display.modeset_commit_cdclk &&
c89e39f3
CT
13159 (intel_state->dev_cdclk != dev_priv->cdclk_freq ||
13160 intel_state->cdclk_pll_vco != dev_priv->skl_vco_freq))
33c8df89 13161 dev_priv->display.modeset_commit_cdclk(state);
f6d1973d 13162
c0ead703 13163 intel_modeset_verify_disabled(dev);
4740b0f2 13164 }
47fab737 13165
a6778b3c 13166 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
29ceb0e6 13167 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
d55dbd06 13168 struct intel_flip_work *work = intel_state->work[i];
f6ac4b2a
ML
13169 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13170 bool modeset = needs_modeset(crtc->state);
9f836f90 13171
f6ac4b2a 13172 if (modeset && crtc->state->active) {
a539205a
ML
13173 update_scanline_offset(to_intel_crtc(crtc));
13174 dev_priv->display.crtc_enable(crtc);
13175 }
80715b2f 13176
f6ac4b2a 13177 if (!modeset)
29ceb0e6 13178 intel_pre_plane_update(to_intel_crtc_state(old_crtc_state));
f6ac4b2a 13179
a6747b73
ML
13180 if (!work) {
13181 if (!list_empty_careful(&intel_crtc->flip_work)) {
13182 spin_lock_irq(&dev->event_lock);
13183 if (!list_empty(&intel_crtc->flip_work))
13184 work = list_last_entry(&intel_crtc->flip_work,
13185 struct intel_flip_work, head);
13186
13187 if (work && work->new_crtc_state == to_intel_crtc_state(old_crtc_state)) {
13188 work->free_new_crtc_state = true;
13189 state->crtc_states[i] = NULL;
13190 state->crtcs[i] = NULL;
13191 }
13192 spin_unlock_irq(&dev->event_lock);
13193 }
13194 continue;
13195 }
f6d1973d 13196
a6747b73
ML
13197 intel_state->work[i] = NULL;
13198 intel_prepare_work(crtc, work, state, old_crtc_state);
d55dbd06 13199 intel_schedule_update(crtc, intel_state, work, nonblock);
177246a8
MR
13200 }
13201
d55dbd06
ML
13202 /* FIXME: add subpixel order */
13203
ee165b1a 13204 drm_atomic_state_free(state);
f30da187 13205
75714940
MK
13206 /* As one of the primary mmio accessors, KMS has a high likelihood
13207 * of triggering bugs in unclaimed access. After we finish
13208 * modesetting, see if an error has been flagged, and if so
13209 * enable debugging for the next modeset - and hope we catch
13210 * the culprit.
13211 *
13212 * XXX note that we assume display power is on at this point.
13213 * This might hold true now but we need to add pm helper to check
13214 * unclaimed only when the hardware is on, as atomic commits
13215 * can happen also when the device is completely off.
13216 */
13217 intel_uncore_arm_unclaimed_mmio_detection(dev_priv);
13218
74c090b1 13219 return 0;
7f27126e
JB
13220}
13221
c0c36b94
CW
13222void intel_crtc_restore_mode(struct drm_crtc *crtc)
13223{
83a57153
ACO
13224 struct drm_device *dev = crtc->dev;
13225 struct drm_atomic_state *state;
e694eb02 13226 struct drm_crtc_state *crtc_state;
2bfb4627 13227 int ret;
83a57153
ACO
13228
13229 state = drm_atomic_state_alloc(dev);
13230 if (!state) {
e694eb02 13231 DRM_DEBUG_KMS("[CRTC:%d] crtc restore failed, out of memory",
83a57153
ACO
13232 crtc->base.id);
13233 return;
13234 }
13235
e694eb02 13236 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
83a57153 13237
e694eb02
ML
13238retry:
13239 crtc_state = drm_atomic_get_crtc_state(state, crtc);
13240 ret = PTR_ERR_OR_ZERO(crtc_state);
13241 if (!ret) {
13242 if (!crtc_state->active)
13243 goto out;
83a57153 13244
e694eb02 13245 crtc_state->mode_changed = true;
74c090b1 13246 ret = drm_atomic_commit(state);
83a57153
ACO
13247 }
13248
e694eb02
ML
13249 if (ret == -EDEADLK) {
13250 drm_atomic_state_clear(state);
13251 drm_modeset_backoff(state->acquire_ctx);
13252 goto retry;
4ed9fb37 13253 }
4be07317 13254
2bfb4627 13255 if (ret)
e694eb02 13256out:
2bfb4627 13257 drm_atomic_state_free(state);
c0c36b94
CW
13258}
13259
25c5b266
DV
13260#undef for_each_intel_crtc_masked
13261
f6e5b160 13262static const struct drm_crtc_funcs intel_crtc_funcs = {
82cf435b 13263 .gamma_set = drm_atomic_helper_legacy_gamma_set,
74c090b1 13264 .set_config = drm_atomic_helper_set_config,
82cf435b 13265 .set_property = drm_atomic_helper_crtc_set_property,
f6e5b160 13266 .destroy = intel_crtc_destroy,
d55dbd06 13267 .page_flip = drm_atomic_helper_page_flip,
1356837e
MR
13268 .atomic_duplicate_state = intel_crtc_duplicate_state,
13269 .atomic_destroy_state = intel_crtc_destroy_state,
f6e5b160
CW
13270};
13271
d55dbd06
ML
13272static struct fence *intel_get_excl_fence(struct drm_i915_gem_object *obj)
13273{
13274 struct reservation_object *resv;
13275
13276
13277 if (!obj->base.dma_buf)
13278 return NULL;
13279
13280 resv = obj->base.dma_buf->resv;
13281
13282 /* For framebuffer backed by dmabuf, wait for fence */
13283 while (1) {
13284 struct fence *fence_excl, *ret = NULL;
13285
13286 rcu_read_lock();
13287
13288 fence_excl = rcu_dereference(resv->fence_excl);
13289 if (fence_excl)
13290 ret = fence_get_rcu(fence_excl);
13291
13292 rcu_read_unlock();
13293
13294 if (ret == fence_excl)
13295 return ret;
13296 }
13297}
13298
6beb8c23
MR
13299/**
13300 * intel_prepare_plane_fb - Prepare fb for usage on plane
13301 * @plane: drm plane to prepare for
13302 * @fb: framebuffer to prepare for presentation
13303 *
13304 * Prepares a framebuffer for usage on a display plane. Generally this
13305 * involves pinning the underlying object and updating the frontbuffer tracking
13306 * bits. Some older platforms need special physical address handling for
13307 * cursor planes.
13308 *
f935675f
ML
13309 * Must be called with struct_mutex held.
13310 *
6beb8c23
MR
13311 * Returns 0 on success, negative error code on failure.
13312 */
13313int
13314intel_prepare_plane_fb(struct drm_plane *plane,
d136dfee 13315 const struct drm_plane_state *new_state)
465c120c
MR
13316{
13317 struct drm_device *dev = plane->dev;
844f9111 13318 struct drm_framebuffer *fb = new_state->fb;
6beb8c23 13319 struct intel_plane *intel_plane = to_intel_plane(plane);
6beb8c23 13320 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
1ee49399 13321 struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->state->fb);
15c86bdb 13322 struct drm_crtc *crtc = new_state->crtc ?: plane->state->crtc;
6beb8c23 13323 int ret = 0;
465c120c 13324
1ee49399 13325 if (!obj && !old_obj)
465c120c
MR
13326 return 0;
13327
15c86bdb
ML
13328 if (WARN_ON(!new_state->state) || WARN_ON(!crtc) ||
13329 WARN_ON(!to_intel_atomic_state(new_state->state)->work[to_intel_crtc(crtc)->pipe])) {
13330 if (WARN_ON(old_obj != obj))
13331 return -EINVAL;
13332
13333 return 0;
13334 }
13335
5008e874
ML
13336 if (old_obj) {
13337 struct drm_crtc_state *crtc_state =
13338 drm_atomic_get_existing_crtc_state(new_state->state, plane->state->crtc);
13339
13340 /* Big Hammer, we also need to ensure that any pending
13341 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
13342 * current scanout is retired before unpinning the old
13343 * framebuffer. Note that we rely on userspace rendering
13344 * into the buffer attached to the pipe they are waiting
13345 * on. If not, userspace generates a GPU hang with IPEHR
13346 * point to the MI_WAIT_FOR_EVENT.
13347 *
13348 * This should only fail upon a hung GPU, in which case we
13349 * can safely continue.
13350 */
13351 if (needs_modeset(crtc_state))
13352 ret = i915_gem_object_wait_rendering(old_obj, true);
f4457ae7
CW
13353 if (ret) {
13354 /* GPU hangs should have been swallowed by the wait */
13355 WARN_ON(ret == -EIO);
f935675f 13356 return ret;
f4457ae7 13357 }
5008e874
ML
13358 }
13359
1ee49399
ML
13360 if (!obj) {
13361 ret = 0;
13362 } else if (plane->type == DRM_PLANE_TYPE_CURSOR &&
6beb8c23
MR
13363 INTEL_INFO(dev)->cursor_needs_physical) {
13364 int align = IS_I830(dev) ? 16 * 1024 : 256;
13365 ret = i915_gem_object_attach_phys(obj, align);
13366 if (ret)
13367 DRM_DEBUG_KMS("failed to attach phys object\n");
13368 } else {
3465c580 13369 ret = intel_pin_and_fence_fb_obj(fb, new_state->rotation);
6beb8c23 13370 }
465c120c 13371
7580d774
ML
13372 if (ret == 0) {
13373 if (obj) {
13374 struct intel_plane_state *plane_state =
13375 to_intel_plane_state(new_state);
13376
13377 i915_gem_request_assign(&plane_state->wait_req,
13378 obj->last_write_req);
84fc494b
ML
13379
13380 plane_state->base.fence = intel_get_excl_fence(obj);
7580d774
ML
13381 }
13382
a9ff8714 13383 i915_gem_track_fb(old_obj, obj, intel_plane->frontbuffer_bit);
7580d774 13384 }
fdd508a6 13385
6beb8c23
MR
13386 return ret;
13387}
13388
38f3ce3a
MR
13389/**
13390 * intel_cleanup_plane_fb - Cleans up an fb after plane use
13391 * @plane: drm plane to clean up for
13392 * @fb: old framebuffer that was on plane
13393 *
13394 * Cleans up a framebuffer that has just been removed from a plane.
f935675f
ML
13395 *
13396 * Must be called with struct_mutex held.
38f3ce3a
MR
13397 */
13398void
13399intel_cleanup_plane_fb(struct drm_plane *plane,
d136dfee 13400 const struct drm_plane_state *old_state)
38f3ce3a
MR
13401{
13402 struct drm_device *dev = plane->dev;
1ee49399 13403 struct intel_plane *intel_plane = to_intel_plane(plane);
7580d774 13404 struct intel_plane_state *old_intel_state;
1ee49399
ML
13405 struct drm_i915_gem_object *old_obj = intel_fb_obj(old_state->fb);
13406 struct drm_i915_gem_object *obj = intel_fb_obj(plane->state->fb);
38f3ce3a 13407
7580d774
ML
13408 old_intel_state = to_intel_plane_state(old_state);
13409
1ee49399 13410 if (!obj && !old_obj)
38f3ce3a
MR
13411 return;
13412
1ee49399
ML
13413 if (old_obj && (plane->type != DRM_PLANE_TYPE_CURSOR ||
13414 !INTEL_INFO(dev)->cursor_needs_physical))
3465c580 13415 intel_unpin_fb_obj(old_state->fb, old_state->rotation);
1ee49399
ML
13416
13417 /* prepare_fb aborted? */
13418 if ((old_obj && (old_obj->frontbuffer_bits & intel_plane->frontbuffer_bit)) ||
13419 (obj && !(obj->frontbuffer_bits & intel_plane->frontbuffer_bit)))
13420 i915_gem_track_fb(old_obj, obj, intel_plane->frontbuffer_bit);
7580d774
ML
13421
13422 i915_gem_request_assign(&old_intel_state->wait_req, NULL);
84fc494b
ML
13423
13424 fence_put(old_intel_state->base.fence);
13425 old_intel_state->base.fence = NULL;
465c120c
MR
13426}
13427
6156a456
CK
13428int
13429skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
13430{
13431 int max_scale;
13432 struct drm_device *dev;
13433 struct drm_i915_private *dev_priv;
13434 int crtc_clock, cdclk;
13435
bf8a0af0 13436 if (!intel_crtc || !crtc_state->base.enable)
6156a456
CK
13437 return DRM_PLANE_HELPER_NO_SCALING;
13438
13439 dev = intel_crtc->base.dev;
13440 dev_priv = dev->dev_private;
13441 crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
27c329ed 13442 cdclk = to_intel_atomic_state(crtc_state->base.state)->cdclk;
6156a456 13443
54bf1ce6 13444 if (WARN_ON_ONCE(!crtc_clock || cdclk < crtc_clock))
6156a456
CK
13445 return DRM_PLANE_HELPER_NO_SCALING;
13446
13447 /*
13448 * skl max scale is lower of:
13449 * close to 3 but not 3, -1 is for that purpose
13450 * or
13451 * cdclk/crtc_clock
13452 */
13453 max_scale = min((1 << 16) * 3 - 1, (1 << 8) * ((cdclk << 8) / crtc_clock));
13454
13455 return max_scale;
13456}
13457
465c120c 13458static int
3c692a41 13459intel_check_primary_plane(struct drm_plane *plane,
061e4b8d 13460 struct intel_crtc_state *crtc_state,
3c692a41
GP
13461 struct intel_plane_state *state)
13462{
2b875c22
MR
13463 struct drm_crtc *crtc = state->base.crtc;
13464 struct drm_framebuffer *fb = state->base.fb;
6156a456 13465 int min_scale = DRM_PLANE_HELPER_NO_SCALING;
061e4b8d
ML
13466 int max_scale = DRM_PLANE_HELPER_NO_SCALING;
13467 bool can_position = false;
465c120c 13468
693bdc28
VS
13469 if (INTEL_INFO(plane->dev)->gen >= 9) {
13470 /* use scaler when colorkey is not required */
13471 if (state->ckey.flags == I915_SET_COLORKEY_NONE) {
13472 min_scale = 1;
13473 max_scale = skl_max_scale(to_intel_crtc(crtc), crtc_state);
13474 }
d8106366 13475 can_position = true;
6156a456 13476 }
d8106366 13477
061e4b8d
ML
13478 return drm_plane_helper_check_update(plane, crtc, fb, &state->src,
13479 &state->dst, &state->clip,
da20eabd
ML
13480 min_scale, max_scale,
13481 can_position, true,
13482 &state->visible);
14af293f
GP
13483}
13484
cf4c7c12 13485/**
4a3b8769
MR
13486 * intel_plane_destroy - destroy a plane
13487 * @plane: plane to destroy
cf4c7c12 13488 *
4a3b8769
MR
13489 * Common destruction function for all types of planes (primary, cursor,
13490 * sprite).
cf4c7c12 13491 */
4a3b8769 13492void intel_plane_destroy(struct drm_plane *plane)
465c120c
MR
13493{
13494 struct intel_plane *intel_plane = to_intel_plane(plane);
13495 drm_plane_cleanup(plane);
13496 kfree(intel_plane);
13497}
13498
65a3fea0 13499const struct drm_plane_funcs intel_plane_funcs = {
70a101f8
MR
13500 .update_plane = drm_atomic_helper_update_plane,
13501 .disable_plane = drm_atomic_helper_disable_plane,
3d7d6510 13502 .destroy = intel_plane_destroy,
c196e1d6 13503 .set_property = drm_atomic_helper_plane_set_property,
a98b3431
MR
13504 .atomic_get_property = intel_plane_atomic_get_property,
13505 .atomic_set_property = intel_plane_atomic_set_property,
ea2c67bb
MR
13506 .atomic_duplicate_state = intel_plane_duplicate_state,
13507 .atomic_destroy_state = intel_plane_destroy_state,
13508
465c120c
MR
13509};
13510
13511static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
13512 int pipe)
13513{
fca0ce2a
VS
13514 struct intel_plane *primary = NULL;
13515 struct intel_plane_state *state = NULL;
465c120c 13516 const uint32_t *intel_primary_formats;
45e3743a 13517 unsigned int num_formats;
fca0ce2a 13518 int ret;
465c120c
MR
13519
13520 primary = kzalloc(sizeof(*primary), GFP_KERNEL);
fca0ce2a
VS
13521 if (!primary)
13522 goto fail;
465c120c 13523
8e7d688b 13524 state = intel_create_plane_state(&primary->base);
fca0ce2a
VS
13525 if (!state)
13526 goto fail;
8e7d688b 13527 primary->base.state = &state->base;
ea2c67bb 13528
465c120c
MR
13529 primary->can_scale = false;
13530 primary->max_downscale = 1;
6156a456
CK
13531 if (INTEL_INFO(dev)->gen >= 9) {
13532 primary->can_scale = true;
af99ceda 13533 state->scaler_id = -1;
6156a456 13534 }
465c120c
MR
13535 primary->pipe = pipe;
13536 primary->plane = pipe;
a9ff8714 13537 primary->frontbuffer_bit = INTEL_FRONTBUFFER_PRIMARY(pipe);
c59cb179 13538 primary->check_plane = intel_check_primary_plane;
465c120c
MR
13539 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
13540 primary->plane = !pipe;
13541
6c0fd451
DL
13542 if (INTEL_INFO(dev)->gen >= 9) {
13543 intel_primary_formats = skl_primary_formats;
13544 num_formats = ARRAY_SIZE(skl_primary_formats);
a8d201af
ML
13545
13546 primary->update_plane = skylake_update_primary_plane;
13547 primary->disable_plane = skylake_disable_primary_plane;
13548 } else if (HAS_PCH_SPLIT(dev)) {
13549 intel_primary_formats = i965_primary_formats;
13550 num_formats = ARRAY_SIZE(i965_primary_formats);
13551
13552 primary->update_plane = ironlake_update_primary_plane;
13553 primary->disable_plane = i9xx_disable_primary_plane;
6c0fd451 13554 } else if (INTEL_INFO(dev)->gen >= 4) {
568db4f2
DL
13555 intel_primary_formats = i965_primary_formats;
13556 num_formats = ARRAY_SIZE(i965_primary_formats);
a8d201af
ML
13557
13558 primary->update_plane = i9xx_update_primary_plane;
13559 primary->disable_plane = i9xx_disable_primary_plane;
6c0fd451
DL
13560 } else {
13561 intel_primary_formats = i8xx_primary_formats;
13562 num_formats = ARRAY_SIZE(i8xx_primary_formats);
a8d201af
ML
13563
13564 primary->update_plane = i9xx_update_primary_plane;
13565 primary->disable_plane = i9xx_disable_primary_plane;
465c120c
MR
13566 }
13567
fca0ce2a
VS
13568 ret = drm_universal_plane_init(dev, &primary->base, 0,
13569 &intel_plane_funcs,
13570 intel_primary_formats, num_formats,
13571 DRM_PLANE_TYPE_PRIMARY, NULL);
13572 if (ret)
13573 goto fail;
48404c1e 13574
3b7a5119
SJ
13575 if (INTEL_INFO(dev)->gen >= 4)
13576 intel_create_rotation_property(dev, primary);
48404c1e 13577
ea2c67bb
MR
13578 drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
13579
465c120c 13580 return &primary->base;
fca0ce2a
VS
13581
13582fail:
13583 kfree(state);
13584 kfree(primary);
13585
13586 return NULL;
465c120c
MR
13587}
13588
3b7a5119
SJ
13589void intel_create_rotation_property(struct drm_device *dev, struct intel_plane *plane)
13590{
13591 if (!dev->mode_config.rotation_property) {
13592 unsigned long flags = BIT(DRM_ROTATE_0) |
13593 BIT(DRM_ROTATE_180);
13594
13595 if (INTEL_INFO(dev)->gen >= 9)
13596 flags |= BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_270);
13597
13598 dev->mode_config.rotation_property =
13599 drm_mode_create_rotation_property(dev, flags);
13600 }
13601 if (dev->mode_config.rotation_property)
13602 drm_object_attach_property(&plane->base.base,
13603 dev->mode_config.rotation_property,
13604 plane->base.state->rotation);
13605}
13606
3d7d6510 13607static int
852e787c 13608intel_check_cursor_plane(struct drm_plane *plane,
061e4b8d 13609 struct intel_crtc_state *crtc_state,
852e787c 13610 struct intel_plane_state *state)
3d7d6510 13611{
061e4b8d 13612 struct drm_crtc *crtc = crtc_state->base.crtc;
2b875c22 13613 struct drm_framebuffer *fb = state->base.fb;
757f9a3e 13614 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
b29ec92c 13615 enum pipe pipe = to_intel_plane(plane)->pipe;
757f9a3e
GP
13616 unsigned stride;
13617 int ret;
3d7d6510 13618
061e4b8d
ML
13619 ret = drm_plane_helper_check_update(plane, crtc, fb, &state->src,
13620 &state->dst, &state->clip,
3d7d6510
MR
13621 DRM_PLANE_HELPER_NO_SCALING,
13622 DRM_PLANE_HELPER_NO_SCALING,
852e787c 13623 true, true, &state->visible);
757f9a3e
GP
13624 if (ret)
13625 return ret;
13626
757f9a3e
GP
13627 /* if we want to turn off the cursor ignore width and height */
13628 if (!obj)
da20eabd 13629 return 0;
757f9a3e 13630
757f9a3e 13631 /* Check for which cursor types we support */
061e4b8d 13632 if (!cursor_size_ok(plane->dev, state->base.crtc_w, state->base.crtc_h)) {
ea2c67bb
MR
13633 DRM_DEBUG("Cursor dimension %dx%d not supported\n",
13634 state->base.crtc_w, state->base.crtc_h);
757f9a3e
GP
13635 return -EINVAL;
13636 }
13637
ea2c67bb
MR
13638 stride = roundup_pow_of_two(state->base.crtc_w) * 4;
13639 if (obj->base.size < stride * state->base.crtc_h) {
757f9a3e
GP
13640 DRM_DEBUG_KMS("buffer is too small\n");
13641 return -ENOMEM;
13642 }
13643
3a656b54 13644 if (fb->modifier[0] != DRM_FORMAT_MOD_NONE) {
757f9a3e 13645 DRM_DEBUG_KMS("cursor cannot be tiled\n");
da20eabd 13646 return -EINVAL;
32b7eeec
MR
13647 }
13648
b29ec92c
VS
13649 /*
13650 * There's something wrong with the cursor on CHV pipe C.
13651 * If it straddles the left edge of the screen then
13652 * moving it away from the edge or disabling it often
13653 * results in a pipe underrun, and often that can lead to
13654 * dead pipe (constant underrun reported, and it scans
13655 * out just a solid color). To recover from that, the
13656 * display power well must be turned off and on again.
13657 * Refuse the put the cursor into that compromised position.
13658 */
13659 if (IS_CHERRYVIEW(plane->dev) && pipe == PIPE_C &&
13660 state->visible && state->base.crtc_x < 0) {
13661 DRM_DEBUG_KMS("CHV cursor C not allowed to straddle the left screen edge\n");
13662 return -EINVAL;
13663 }
13664
da20eabd 13665 return 0;
852e787c 13666}
3d7d6510 13667
a8ad0d8e
ML
13668static void
13669intel_disable_cursor_plane(struct drm_plane *plane,
7fabf5ef 13670 struct drm_crtc *crtc)
a8ad0d8e 13671{
f2858021
ML
13672 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13673
13674 intel_crtc->cursor_addr = 0;
55a08b3f 13675 intel_crtc_update_cursor(crtc, NULL);
a8ad0d8e
ML
13676}
13677
f4a2cf29 13678static void
55a08b3f
ML
13679intel_update_cursor_plane(struct drm_plane *plane,
13680 const struct intel_crtc_state *crtc_state,
13681 const struct intel_plane_state *state)
852e787c 13682{
55a08b3f
ML
13683 struct drm_crtc *crtc = crtc_state->base.crtc;
13684 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ea2c67bb 13685 struct drm_device *dev = plane->dev;
2b875c22 13686 struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
a912f12f 13687 uint32_t addr;
852e787c 13688
f4a2cf29 13689 if (!obj)
a912f12f 13690 addr = 0;
f4a2cf29 13691 else if (!INTEL_INFO(dev)->cursor_needs_physical)
a912f12f 13692 addr = i915_gem_obj_ggtt_offset(obj);
f4a2cf29 13693 else
a912f12f 13694 addr = obj->phys_handle->busaddr;
852e787c 13695
a912f12f 13696 intel_crtc->cursor_addr = addr;
55a08b3f 13697 intel_crtc_update_cursor(crtc, state);
852e787c
GP
13698}
13699
3d7d6510
MR
13700static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
13701 int pipe)
13702{
fca0ce2a
VS
13703 struct intel_plane *cursor = NULL;
13704 struct intel_plane_state *state = NULL;
13705 int ret;
3d7d6510
MR
13706
13707 cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
fca0ce2a
VS
13708 if (!cursor)
13709 goto fail;
3d7d6510 13710
8e7d688b 13711 state = intel_create_plane_state(&cursor->base);
fca0ce2a
VS
13712 if (!state)
13713 goto fail;
8e7d688b 13714 cursor->base.state = &state->base;
ea2c67bb 13715
3d7d6510
MR
13716 cursor->can_scale = false;
13717 cursor->max_downscale = 1;
13718 cursor->pipe = pipe;
13719 cursor->plane = pipe;
a9ff8714 13720 cursor->frontbuffer_bit = INTEL_FRONTBUFFER_CURSOR(pipe);
c59cb179 13721 cursor->check_plane = intel_check_cursor_plane;
55a08b3f 13722 cursor->update_plane = intel_update_cursor_plane;
a8ad0d8e 13723 cursor->disable_plane = intel_disable_cursor_plane;
3d7d6510 13724
fca0ce2a
VS
13725 ret = drm_universal_plane_init(dev, &cursor->base, 0,
13726 &intel_plane_funcs,
13727 intel_cursor_formats,
13728 ARRAY_SIZE(intel_cursor_formats),
13729 DRM_PLANE_TYPE_CURSOR, NULL);
13730 if (ret)
13731 goto fail;
4398ad45
VS
13732
13733 if (INTEL_INFO(dev)->gen >= 4) {
13734 if (!dev->mode_config.rotation_property)
13735 dev->mode_config.rotation_property =
13736 drm_mode_create_rotation_property(dev,
13737 BIT(DRM_ROTATE_0) |
13738 BIT(DRM_ROTATE_180));
13739 if (dev->mode_config.rotation_property)
13740 drm_object_attach_property(&cursor->base.base,
13741 dev->mode_config.rotation_property,
8e7d688b 13742 state->base.rotation);
4398ad45
VS
13743 }
13744
af99ceda
CK
13745 if (INTEL_INFO(dev)->gen >=9)
13746 state->scaler_id = -1;
13747
ea2c67bb
MR
13748 drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
13749
3d7d6510 13750 return &cursor->base;
fca0ce2a
VS
13751
13752fail:
13753 kfree(state);
13754 kfree(cursor);
13755
13756 return NULL;
3d7d6510
MR
13757}
13758
549e2bfb
CK
13759static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
13760 struct intel_crtc_state *crtc_state)
13761{
13762 int i;
13763 struct intel_scaler *intel_scaler;
13764 struct intel_crtc_scaler_state *scaler_state = &crtc_state->scaler_state;
13765
13766 for (i = 0; i < intel_crtc->num_scalers; i++) {
13767 intel_scaler = &scaler_state->scalers[i];
13768 intel_scaler->in_use = 0;
549e2bfb
CK
13769 intel_scaler->mode = PS_SCALER_MODE_DYN;
13770 }
13771
13772 scaler_state->scaler_id = -1;
13773}
13774
b358d0a6 13775static void intel_crtc_init(struct drm_device *dev, int pipe)
79e53945 13776{
fbee40df 13777 struct drm_i915_private *dev_priv = dev->dev_private;
79e53945 13778 struct intel_crtc *intel_crtc;
f5de6e07 13779 struct intel_crtc_state *crtc_state = NULL;
3d7d6510
MR
13780 struct drm_plane *primary = NULL;
13781 struct drm_plane *cursor = NULL;
8563b1e8 13782 int ret;
79e53945 13783
955382f3 13784 intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
79e53945
JB
13785 if (intel_crtc == NULL)
13786 return;
13787
f5de6e07
ACO
13788 crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
13789 if (!crtc_state)
13790 goto fail;
550acefd
ACO
13791 intel_crtc->config = crtc_state;
13792 intel_crtc->base.state = &crtc_state->base;
07878248 13793 crtc_state->base.crtc = &intel_crtc->base;
f5de6e07 13794
6885843a
ML
13795 INIT_LIST_HEAD(&intel_crtc->flip_work);
13796
549e2bfb
CK
13797 /* initialize shared scalers */
13798 if (INTEL_INFO(dev)->gen >= 9) {
13799 if (pipe == PIPE_C)
13800 intel_crtc->num_scalers = 1;
13801 else
13802 intel_crtc->num_scalers = SKL_NUM_SCALERS;
13803
13804 skl_init_scalers(dev, intel_crtc, crtc_state);
13805 }
13806
465c120c 13807 primary = intel_primary_plane_create(dev, pipe);
3d7d6510
MR
13808 if (!primary)
13809 goto fail;
13810
13811 cursor = intel_cursor_plane_create(dev, pipe);
13812 if (!cursor)
13813 goto fail;
13814
465c120c 13815 ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
f9882876 13816 cursor, &intel_crtc_funcs, NULL);
3d7d6510
MR
13817 if (ret)
13818 goto fail;
79e53945 13819
1f1c2e24
VS
13820 /*
13821 * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
8c0f92e1 13822 * is hooked to pipe B. Hence we want plane A feeding pipe B.
1f1c2e24 13823 */
80824003
JB
13824 intel_crtc->pipe = pipe;
13825 intel_crtc->plane = pipe;
3a77c4c4 13826 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
28c97730 13827 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
e2e767ab 13828 intel_crtc->plane = !pipe;
80824003
JB
13829 }
13830
4b0e333e
CW
13831 intel_crtc->cursor_base = ~0;
13832 intel_crtc->cursor_cntl = ~0;
dc41c154 13833 intel_crtc->cursor_size = ~0;
8d7849db 13834
852eb00d
VS
13835 intel_crtc->wm.cxsr_allowed = true;
13836
22fd0fab
JB
13837 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
13838 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
13839 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
13840 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
13841
79e53945 13842 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
87b6b101 13843
8563b1e8
LL
13844 intel_color_init(&intel_crtc->base);
13845
87b6b101 13846 WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
3d7d6510
MR
13847 return;
13848
13849fail:
13850 if (primary)
13851 drm_plane_cleanup(primary);
13852 if (cursor)
13853 drm_plane_cleanup(cursor);
f5de6e07 13854 kfree(crtc_state);
3d7d6510 13855 kfree(intel_crtc);
79e53945
JB
13856}
13857
752aa88a
JB
13858enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
13859{
13860 struct drm_encoder *encoder = connector->base.encoder;
6e9f798d 13861 struct drm_device *dev = connector->base.dev;
752aa88a 13862
51fd371b 13863 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
752aa88a 13864
d3babd3f 13865 if (!encoder || WARN_ON(!encoder->crtc))
752aa88a
JB
13866 return INVALID_PIPE;
13867
13868 return to_intel_crtc(encoder->crtc)->pipe;
13869}
13870
08d7b3d1 13871int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
05394f39 13872 struct drm_file *file)
08d7b3d1 13873{
08d7b3d1 13874 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
7707e653 13875 struct drm_crtc *drmmode_crtc;
c05422d5 13876 struct intel_crtc *crtc;
08d7b3d1 13877
7707e653 13878 drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
08d7b3d1 13879
7707e653 13880 if (!drmmode_crtc) {
08d7b3d1 13881 DRM_ERROR("no such CRTC id\n");
3f2c2057 13882 return -ENOENT;
08d7b3d1
CW
13883 }
13884
7707e653 13885 crtc = to_intel_crtc(drmmode_crtc);
c05422d5 13886 pipe_from_crtc_id->pipe = crtc->pipe;
08d7b3d1 13887
c05422d5 13888 return 0;
08d7b3d1
CW
13889}
13890
66a9278e 13891static int intel_encoder_clones(struct intel_encoder *encoder)
79e53945 13892{
66a9278e
DV
13893 struct drm_device *dev = encoder->base.dev;
13894 struct intel_encoder *source_encoder;
79e53945 13895 int index_mask = 0;
79e53945
JB
13896 int entry = 0;
13897
b2784e15 13898 for_each_intel_encoder(dev, source_encoder) {
bc079e8b 13899 if (encoders_cloneable(encoder, source_encoder))
66a9278e
DV
13900 index_mask |= (1 << entry);
13901
79e53945
JB
13902 entry++;
13903 }
4ef69c7a 13904
79e53945
JB
13905 return index_mask;
13906}
13907
4d302442
CW
13908static bool has_edp_a(struct drm_device *dev)
13909{
13910 struct drm_i915_private *dev_priv = dev->dev_private;
13911
13912 if (!IS_MOBILE(dev))
13913 return false;
13914
13915 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
13916 return false;
13917
e3589908 13918 if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
4d302442
CW
13919 return false;
13920
13921 return true;
13922}
13923
84b4e042
JB
13924static bool intel_crt_present(struct drm_device *dev)
13925{
13926 struct drm_i915_private *dev_priv = dev->dev_private;
13927
884497ed
DL
13928 if (INTEL_INFO(dev)->gen >= 9)
13929 return false;
13930
cf404ce4 13931 if (IS_HSW_ULT(dev) || IS_BDW_ULT(dev))
84b4e042
JB
13932 return false;
13933
13934 if (IS_CHERRYVIEW(dev))
13935 return false;
13936
65e472e4
VS
13937 if (HAS_PCH_LPT_H(dev) && I915_READ(SFUSE_STRAP) & SFUSE_STRAP_CRT_DISABLED)
13938 return false;
13939
70ac54d0
VS
13940 /* DDI E can't be used if DDI A requires 4 lanes */
13941 if (HAS_DDI(dev) && I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_A_4_LANES)
13942 return false;
13943
e4abb733 13944 if (!dev_priv->vbt.int_crt_support)
84b4e042
JB
13945 return false;
13946
13947 return true;
13948}
13949
79e53945
JB
13950static void intel_setup_outputs(struct drm_device *dev)
13951{
725e30ad 13952 struct drm_i915_private *dev_priv = dev->dev_private;
4ef69c7a 13953 struct intel_encoder *encoder;
cb0953d7 13954 bool dpd_is_edp = false;
79e53945 13955
c9093354 13956 intel_lvds_init(dev);
79e53945 13957
84b4e042 13958 if (intel_crt_present(dev))
79935fca 13959 intel_crt_init(dev);
cb0953d7 13960
c776eb2e
VK
13961 if (IS_BROXTON(dev)) {
13962 /*
13963 * FIXME: Broxton doesn't support port detection via the
13964 * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
13965 * detect the ports.
13966 */
13967 intel_ddi_init(dev, PORT_A);
13968 intel_ddi_init(dev, PORT_B);
13969 intel_ddi_init(dev, PORT_C);
c6c794a2
SS
13970
13971 intel_dsi_init(dev);
c776eb2e 13972 } else if (HAS_DDI(dev)) {
0e72a5b5
ED
13973 int found;
13974
de31facd
JB
13975 /*
13976 * Haswell uses DDI functions to detect digital outputs.
13977 * On SKL pre-D0 the strap isn't connected, so we assume
13978 * it's there.
13979 */
77179400 13980 found = I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_INIT_DISPLAY_DETECTED;
de31facd 13981 /* WaIgnoreDDIAStrap: skl */
ef11bdb3 13982 if (found || IS_SKYLAKE(dev) || IS_KABYLAKE(dev))
0e72a5b5
ED
13983 intel_ddi_init(dev, PORT_A);
13984
13985 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
13986 * register */
13987 found = I915_READ(SFUSE_STRAP);
13988
13989 if (found & SFUSE_STRAP_DDIB_DETECTED)
13990 intel_ddi_init(dev, PORT_B);
13991 if (found & SFUSE_STRAP_DDIC_DETECTED)
13992 intel_ddi_init(dev, PORT_C);
13993 if (found & SFUSE_STRAP_DDID_DETECTED)
13994 intel_ddi_init(dev, PORT_D);
2800e4c2
RV
13995 /*
13996 * On SKL we don't have a way to detect DDI-E so we rely on VBT.
13997 */
ef11bdb3 13998 if ((IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) &&
2800e4c2
RV
13999 (dev_priv->vbt.ddi_port_info[PORT_E].supports_dp ||
14000 dev_priv->vbt.ddi_port_info[PORT_E].supports_dvi ||
14001 dev_priv->vbt.ddi_port_info[PORT_E].supports_hdmi))
14002 intel_ddi_init(dev, PORT_E);
14003
0e72a5b5 14004 } else if (HAS_PCH_SPLIT(dev)) {
cb0953d7 14005 int found;
5d8a7752 14006 dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
270b3042
DV
14007
14008 if (has_edp_a(dev))
14009 intel_dp_init(dev, DP_A, PORT_A);
cb0953d7 14010
dc0fa718 14011 if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
461ed3ca 14012 /* PCH SDVOB multiplex with HDMIB */
2a5c0832 14013 found = intel_sdvo_init(dev, PCH_SDVOB, PORT_B);
30ad48b7 14014 if (!found)
e2debe91 14015 intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
5eb08b69 14016 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
ab9d7c30 14017 intel_dp_init(dev, PCH_DP_B, PORT_B);
30ad48b7
ZW
14018 }
14019
dc0fa718 14020 if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
e2debe91 14021 intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
30ad48b7 14022
dc0fa718 14023 if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
e2debe91 14024 intel_hdmi_init(dev, PCH_HDMID, PORT_D);
30ad48b7 14025
5eb08b69 14026 if (I915_READ(PCH_DP_C) & DP_DETECTED)
ab9d7c30 14027 intel_dp_init(dev, PCH_DP_C, PORT_C);
5eb08b69 14028
270b3042 14029 if (I915_READ(PCH_DP_D) & DP_DETECTED)
ab9d7c30 14030 intel_dp_init(dev, PCH_DP_D, PORT_D);
666a4537 14031 } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
e17ac6db
VS
14032 /*
14033 * The DP_DETECTED bit is the latched state of the DDC
14034 * SDA pin at boot. However since eDP doesn't require DDC
14035 * (no way to plug in a DP->HDMI dongle) the DDC pins for
14036 * eDP ports may have been muxed to an alternate function.
14037 * Thus we can't rely on the DP_DETECTED bit alone to detect
14038 * eDP ports. Consult the VBT as well as DP_DETECTED to
14039 * detect eDP ports.
14040 */
e66eb81d 14041 if (I915_READ(VLV_HDMIB) & SDVO_DETECTED &&
d2182a66 14042 !intel_dp_is_edp(dev, PORT_B))
e66eb81d
VS
14043 intel_hdmi_init(dev, VLV_HDMIB, PORT_B);
14044 if (I915_READ(VLV_DP_B) & DP_DETECTED ||
e17ac6db 14045 intel_dp_is_edp(dev, PORT_B))
e66eb81d 14046 intel_dp_init(dev, VLV_DP_B, PORT_B);
585a94b8 14047
e66eb81d 14048 if (I915_READ(VLV_HDMIC) & SDVO_DETECTED &&
d2182a66 14049 !intel_dp_is_edp(dev, PORT_C))
e66eb81d
VS
14050 intel_hdmi_init(dev, VLV_HDMIC, PORT_C);
14051 if (I915_READ(VLV_DP_C) & DP_DETECTED ||
e17ac6db 14052 intel_dp_is_edp(dev, PORT_C))
e66eb81d 14053 intel_dp_init(dev, VLV_DP_C, PORT_C);
19c03924 14054
9418c1f1 14055 if (IS_CHERRYVIEW(dev)) {
e17ac6db 14056 /* eDP not supported on port D, so don't check VBT */
e66eb81d
VS
14057 if (I915_READ(CHV_HDMID) & SDVO_DETECTED)
14058 intel_hdmi_init(dev, CHV_HDMID, PORT_D);
14059 if (I915_READ(CHV_DP_D) & DP_DETECTED)
14060 intel_dp_init(dev, CHV_DP_D, PORT_D);
9418c1f1
VS
14061 }
14062
3cfca973 14063 intel_dsi_init(dev);
09da55dc 14064 } else if (!IS_GEN2(dev) && !IS_PINEVIEW(dev)) {
27185ae1 14065 bool found = false;
7d57382e 14066
e2debe91 14067 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
b01f2c3a 14068 DRM_DEBUG_KMS("probing SDVOB\n");
2a5c0832 14069 found = intel_sdvo_init(dev, GEN3_SDVOB, PORT_B);
3fec3d2f 14070 if (!found && IS_G4X(dev)) {
b01f2c3a 14071 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
e2debe91 14072 intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
b01f2c3a 14073 }
27185ae1 14074
3fec3d2f 14075 if (!found && IS_G4X(dev))
ab9d7c30 14076 intel_dp_init(dev, DP_B, PORT_B);
725e30ad 14077 }
13520b05
KH
14078
14079 /* Before G4X SDVOC doesn't have its own detect register */
13520b05 14080
e2debe91 14081 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
b01f2c3a 14082 DRM_DEBUG_KMS("probing SDVOC\n");
2a5c0832 14083 found = intel_sdvo_init(dev, GEN3_SDVOC, PORT_C);
b01f2c3a 14084 }
27185ae1 14085
e2debe91 14086 if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
27185ae1 14087
3fec3d2f 14088 if (IS_G4X(dev)) {
b01f2c3a 14089 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
e2debe91 14090 intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
b01f2c3a 14091 }
3fec3d2f 14092 if (IS_G4X(dev))
ab9d7c30 14093 intel_dp_init(dev, DP_C, PORT_C);
725e30ad 14094 }
27185ae1 14095
3fec3d2f 14096 if (IS_G4X(dev) &&
e7281eab 14097 (I915_READ(DP_D) & DP_DETECTED))
ab9d7c30 14098 intel_dp_init(dev, DP_D, PORT_D);
bad720ff 14099 } else if (IS_GEN2(dev))
79e53945
JB
14100 intel_dvo_init(dev);
14101
103a196f 14102 if (SUPPORTS_TV(dev))
79e53945
JB
14103 intel_tv_init(dev);
14104
0bc12bcb 14105 intel_psr_init(dev);
7c8f8a70 14106
b2784e15 14107 for_each_intel_encoder(dev, encoder) {
4ef69c7a
CW
14108 encoder->base.possible_crtcs = encoder->crtc_mask;
14109 encoder->base.possible_clones =
66a9278e 14110 intel_encoder_clones(encoder);
79e53945 14111 }
47356eb6 14112
dde86e2d 14113 intel_init_pch_refclk(dev);
270b3042
DV
14114
14115 drm_helper_move_panel_connectors_to_head(dev);
79e53945
JB
14116}
14117
14118static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
14119{
60a5ca01 14120 struct drm_device *dev = fb->dev;
79e53945 14121 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
79e53945 14122
ef2d633e 14123 drm_framebuffer_cleanup(fb);
60a5ca01 14124 mutex_lock(&dev->struct_mutex);
ef2d633e 14125 WARN_ON(!intel_fb->obj->framebuffer_references--);
60a5ca01
VS
14126 drm_gem_object_unreference(&intel_fb->obj->base);
14127 mutex_unlock(&dev->struct_mutex);
79e53945
JB
14128 kfree(intel_fb);
14129}
14130
14131static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
05394f39 14132 struct drm_file *file,
79e53945
JB
14133 unsigned int *handle)
14134{
14135 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
05394f39 14136 struct drm_i915_gem_object *obj = intel_fb->obj;
79e53945 14137
cc917ab4
CW
14138 if (obj->userptr.mm) {
14139 DRM_DEBUG("attempting to use a userptr for a framebuffer, denied\n");
14140 return -EINVAL;
14141 }
14142
05394f39 14143 return drm_gem_handle_create(file, &obj->base, handle);
79e53945
JB
14144}
14145
86c98588
RV
14146static int intel_user_framebuffer_dirty(struct drm_framebuffer *fb,
14147 struct drm_file *file,
14148 unsigned flags, unsigned color,
14149 struct drm_clip_rect *clips,
14150 unsigned num_clips)
14151{
14152 struct drm_device *dev = fb->dev;
14153 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
14154 struct drm_i915_gem_object *obj = intel_fb->obj;
14155
14156 mutex_lock(&dev->struct_mutex);
74b4ea1e 14157 intel_fb_obj_flush(obj, false, ORIGIN_DIRTYFB);
86c98588
RV
14158 mutex_unlock(&dev->struct_mutex);
14159
14160 return 0;
14161}
14162
79e53945
JB
14163static const struct drm_framebuffer_funcs intel_fb_funcs = {
14164 .destroy = intel_user_framebuffer_destroy,
14165 .create_handle = intel_user_framebuffer_create_handle,
86c98588 14166 .dirty = intel_user_framebuffer_dirty,
79e53945
JB
14167};
14168
b321803d
DL
14169static
14170u32 intel_fb_pitch_limit(struct drm_device *dev, uint64_t fb_modifier,
14171 uint32_t pixel_format)
14172{
14173 u32 gen = INTEL_INFO(dev)->gen;
14174
14175 if (gen >= 9) {
ac484963
VS
14176 int cpp = drm_format_plane_cpp(pixel_format, 0);
14177
b321803d
DL
14178 /* "The stride in bytes must not exceed the of the size of 8K
14179 * pixels and 32K bytes."
14180 */
ac484963 14181 return min(8192 * cpp, 32768);
666a4537 14182 } else if (gen >= 5 && !IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
b321803d
DL
14183 return 32*1024;
14184 } else if (gen >= 4) {
14185 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14186 return 16*1024;
14187 else
14188 return 32*1024;
14189 } else if (gen >= 3) {
14190 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14191 return 8*1024;
14192 else
14193 return 16*1024;
14194 } else {
14195 /* XXX DSPC is limited to 4k tiled */
14196 return 8*1024;
14197 }
14198}
14199
b5ea642a
DV
14200static int intel_framebuffer_init(struct drm_device *dev,
14201 struct intel_framebuffer *intel_fb,
14202 struct drm_mode_fb_cmd2 *mode_cmd,
14203 struct drm_i915_gem_object *obj)
79e53945 14204{
7b49f948 14205 struct drm_i915_private *dev_priv = to_i915(dev);
6761dd31 14206 unsigned int aligned_height;
79e53945 14207 int ret;
b321803d 14208 u32 pitch_limit, stride_alignment;
79e53945 14209
dd4916c5
DV
14210 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
14211
2a80eada
DV
14212 if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
14213 /* Enforce that fb modifier and tiling mode match, but only for
14214 * X-tiled. This is needed for FBC. */
14215 if (!!(obj->tiling_mode == I915_TILING_X) !=
14216 !!(mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED)) {
14217 DRM_DEBUG("tiling_mode doesn't match fb modifier\n");
14218 return -EINVAL;
14219 }
14220 } else {
14221 if (obj->tiling_mode == I915_TILING_X)
14222 mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
14223 else if (obj->tiling_mode == I915_TILING_Y) {
14224 DRM_DEBUG("No Y tiling for legacy addfb\n");
14225 return -EINVAL;
14226 }
14227 }
14228
9a8f0a12
TU
14229 /* Passed in modifier sanity checking. */
14230 switch (mode_cmd->modifier[0]) {
14231 case I915_FORMAT_MOD_Y_TILED:
14232 case I915_FORMAT_MOD_Yf_TILED:
14233 if (INTEL_INFO(dev)->gen < 9) {
14234 DRM_DEBUG("Unsupported tiling 0x%llx!\n",
14235 mode_cmd->modifier[0]);
14236 return -EINVAL;
14237 }
14238 case DRM_FORMAT_MOD_NONE:
14239 case I915_FORMAT_MOD_X_TILED:
14240 break;
14241 default:
c0f40428
JB
14242 DRM_DEBUG("Unsupported fb modifier 0x%llx!\n",
14243 mode_cmd->modifier[0]);
57cd6508 14244 return -EINVAL;
c16ed4be 14245 }
57cd6508 14246
7b49f948
VS
14247 stride_alignment = intel_fb_stride_alignment(dev_priv,
14248 mode_cmd->modifier[0],
b321803d
DL
14249 mode_cmd->pixel_format);
14250 if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
14251 DRM_DEBUG("pitch (%d) must be at least %u byte aligned\n",
14252 mode_cmd->pitches[0], stride_alignment);
57cd6508 14253 return -EINVAL;
c16ed4be 14254 }
57cd6508 14255
b321803d
DL
14256 pitch_limit = intel_fb_pitch_limit(dev, mode_cmd->modifier[0],
14257 mode_cmd->pixel_format);
a35cdaa0 14258 if (mode_cmd->pitches[0] > pitch_limit) {
b321803d
DL
14259 DRM_DEBUG("%s pitch (%u) must be at less than %d\n",
14260 mode_cmd->modifier[0] != DRM_FORMAT_MOD_NONE ?
2a80eada 14261 "tiled" : "linear",
a35cdaa0 14262 mode_cmd->pitches[0], pitch_limit);
5d7bd705 14263 return -EINVAL;
c16ed4be 14264 }
5d7bd705 14265
2a80eada 14266 if (mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED &&
c16ed4be
CW
14267 mode_cmd->pitches[0] != obj->stride) {
14268 DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
14269 mode_cmd->pitches[0], obj->stride);
5d7bd705 14270 return -EINVAL;
c16ed4be 14271 }
5d7bd705 14272
57779d06 14273 /* Reject formats not supported by any plane early. */
308e5bcb 14274 switch (mode_cmd->pixel_format) {
57779d06 14275 case DRM_FORMAT_C8:
04b3924d
VS
14276 case DRM_FORMAT_RGB565:
14277 case DRM_FORMAT_XRGB8888:
14278 case DRM_FORMAT_ARGB8888:
57779d06
VS
14279 break;
14280 case DRM_FORMAT_XRGB1555:
c16ed4be 14281 if (INTEL_INFO(dev)->gen > 3) {
4ee62c76
VS
14282 DRM_DEBUG("unsupported pixel format: %s\n",
14283 drm_get_format_name(mode_cmd->pixel_format));
57779d06 14284 return -EINVAL;
c16ed4be 14285 }
57779d06 14286 break;
57779d06 14287 case DRM_FORMAT_ABGR8888:
666a4537
WB
14288 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev) &&
14289 INTEL_INFO(dev)->gen < 9) {
6c0fd451
DL
14290 DRM_DEBUG("unsupported pixel format: %s\n",
14291 drm_get_format_name(mode_cmd->pixel_format));
14292 return -EINVAL;
14293 }
14294 break;
14295 case DRM_FORMAT_XBGR8888:
04b3924d 14296 case DRM_FORMAT_XRGB2101010:
57779d06 14297 case DRM_FORMAT_XBGR2101010:
c16ed4be 14298 if (INTEL_INFO(dev)->gen < 4) {
4ee62c76
VS
14299 DRM_DEBUG("unsupported pixel format: %s\n",
14300 drm_get_format_name(mode_cmd->pixel_format));
57779d06 14301 return -EINVAL;
c16ed4be 14302 }
b5626747 14303 break;
7531208b 14304 case DRM_FORMAT_ABGR2101010:
666a4537 14305 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
7531208b
DL
14306 DRM_DEBUG("unsupported pixel format: %s\n",
14307 drm_get_format_name(mode_cmd->pixel_format));
14308 return -EINVAL;
14309 }
14310 break;
04b3924d
VS
14311 case DRM_FORMAT_YUYV:
14312 case DRM_FORMAT_UYVY:
14313 case DRM_FORMAT_YVYU:
14314 case DRM_FORMAT_VYUY:
c16ed4be 14315 if (INTEL_INFO(dev)->gen < 5) {
4ee62c76
VS
14316 DRM_DEBUG("unsupported pixel format: %s\n",
14317 drm_get_format_name(mode_cmd->pixel_format));
57779d06 14318 return -EINVAL;
c16ed4be 14319 }
57cd6508
CW
14320 break;
14321 default:
4ee62c76
VS
14322 DRM_DEBUG("unsupported pixel format: %s\n",
14323 drm_get_format_name(mode_cmd->pixel_format));
57cd6508
CW
14324 return -EINVAL;
14325 }
14326
90f9a336
VS
14327 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
14328 if (mode_cmd->offsets[0] != 0)
14329 return -EINVAL;
14330
ec2c981e 14331 aligned_height = intel_fb_align_height(dev, mode_cmd->height,
091df6cb
DV
14332 mode_cmd->pixel_format,
14333 mode_cmd->modifier[0]);
53155c0a
DV
14334 /* FIXME drm helper for size checks (especially planar formats)? */
14335 if (obj->base.size < aligned_height * mode_cmd->pitches[0])
14336 return -EINVAL;
14337
c7d73f6a
DV
14338 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
14339 intel_fb->obj = obj;
14340
2d7a215f
VS
14341 intel_fill_fb_info(dev_priv, &intel_fb->base);
14342
79e53945
JB
14343 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
14344 if (ret) {
14345 DRM_ERROR("framebuffer init failed %d\n", ret);
14346 return ret;
14347 }
14348
0b05e1e0
VS
14349 intel_fb->obj->framebuffer_references++;
14350
79e53945
JB
14351 return 0;
14352}
14353
79e53945
JB
14354static struct drm_framebuffer *
14355intel_user_framebuffer_create(struct drm_device *dev,
14356 struct drm_file *filp,
1eb83451 14357 const struct drm_mode_fb_cmd2 *user_mode_cmd)
79e53945 14358{
dcb1394e 14359 struct drm_framebuffer *fb;
05394f39 14360 struct drm_i915_gem_object *obj;
76dc3769 14361 struct drm_mode_fb_cmd2 mode_cmd = *user_mode_cmd;
79e53945 14362
308e5bcb 14363 obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
76dc3769 14364 mode_cmd.handles[0]));
c8725226 14365 if (&obj->base == NULL)
cce13ff7 14366 return ERR_PTR(-ENOENT);
79e53945 14367
92907cbb 14368 fb = intel_framebuffer_create(dev, &mode_cmd, obj);
dcb1394e
LW
14369 if (IS_ERR(fb))
14370 drm_gem_object_unreference_unlocked(&obj->base);
14371
14372 return fb;
79e53945
JB
14373}
14374
0695726e 14375#ifndef CONFIG_DRM_FBDEV_EMULATION
0632fef6 14376static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
4520f53a
DV
14377{
14378}
14379#endif
14380
79e53945 14381static const struct drm_mode_config_funcs intel_mode_funcs = {
79e53945 14382 .fb_create = intel_user_framebuffer_create,
0632fef6 14383 .output_poll_changed = intel_fbdev_output_poll_changed,
5ee67f1c
MR
14384 .atomic_check = intel_atomic_check,
14385 .atomic_commit = intel_atomic_commit,
de419ab6
ML
14386 .atomic_state_alloc = intel_atomic_state_alloc,
14387 .atomic_state_clear = intel_atomic_state_clear,
79e53945
JB
14388};
14389
88212941
ID
14390/**
14391 * intel_init_display_hooks - initialize the display modesetting hooks
14392 * @dev_priv: device private
14393 */
14394void intel_init_display_hooks(struct drm_i915_private *dev_priv)
e70236a8 14395{
88212941 14396 if (INTEL_INFO(dev_priv)->gen >= 9) {
bc8d7dff 14397 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
5724dbd1
DL
14398 dev_priv->display.get_initial_plane_config =
14399 skylake_get_initial_plane_config;
bc8d7dff
DL
14400 dev_priv->display.crtc_compute_clock =
14401 haswell_crtc_compute_clock;
14402 dev_priv->display.crtc_enable = haswell_crtc_enable;
14403 dev_priv->display.crtc_disable = haswell_crtc_disable;
88212941 14404 } else if (HAS_DDI(dev_priv)) {
0e8ffe1b 14405 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
5724dbd1
DL
14406 dev_priv->display.get_initial_plane_config =
14407 ironlake_get_initial_plane_config;
797d0259
ACO
14408 dev_priv->display.crtc_compute_clock =
14409 haswell_crtc_compute_clock;
4f771f10
PZ
14410 dev_priv->display.crtc_enable = haswell_crtc_enable;
14411 dev_priv->display.crtc_disable = haswell_crtc_disable;
88212941 14412 } else if (HAS_PCH_SPLIT(dev_priv)) {
0e8ffe1b 14413 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
5724dbd1
DL
14414 dev_priv->display.get_initial_plane_config =
14415 ironlake_get_initial_plane_config;
3fb37703
ACO
14416 dev_priv->display.crtc_compute_clock =
14417 ironlake_crtc_compute_clock;
76e5a89c
DV
14418 dev_priv->display.crtc_enable = ironlake_crtc_enable;
14419 dev_priv->display.crtc_disable = ironlake_crtc_disable;
65b3d6a9 14420 } else if (IS_CHERRYVIEW(dev_priv)) {
89b667f8 14421 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
5724dbd1
DL
14422 dev_priv->display.get_initial_plane_config =
14423 i9xx_get_initial_plane_config;
65b3d6a9
ACO
14424 dev_priv->display.crtc_compute_clock = chv_crtc_compute_clock;
14425 dev_priv->display.crtc_enable = valleyview_crtc_enable;
14426 dev_priv->display.crtc_disable = i9xx_crtc_disable;
14427 } else if (IS_VALLEYVIEW(dev_priv)) {
14428 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
14429 dev_priv->display.get_initial_plane_config =
14430 i9xx_get_initial_plane_config;
14431 dev_priv->display.crtc_compute_clock = vlv_crtc_compute_clock;
89b667f8
JB
14432 dev_priv->display.crtc_enable = valleyview_crtc_enable;
14433 dev_priv->display.crtc_disable = i9xx_crtc_disable;
19ec6693
ACO
14434 } else if (IS_G4X(dev_priv)) {
14435 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
14436 dev_priv->display.get_initial_plane_config =
14437 i9xx_get_initial_plane_config;
14438 dev_priv->display.crtc_compute_clock = g4x_crtc_compute_clock;
14439 dev_priv->display.crtc_enable = i9xx_crtc_enable;
14440 dev_priv->display.crtc_disable = i9xx_crtc_disable;
70e8aa21
ACO
14441 } else if (IS_PINEVIEW(dev_priv)) {
14442 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
14443 dev_priv->display.get_initial_plane_config =
14444 i9xx_get_initial_plane_config;
14445 dev_priv->display.crtc_compute_clock = pnv_crtc_compute_clock;
14446 dev_priv->display.crtc_enable = i9xx_crtc_enable;
14447 dev_priv->display.crtc_disable = i9xx_crtc_disable;
81c97f52 14448 } else if (!IS_GEN2(dev_priv)) {
0e8ffe1b 14449 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
5724dbd1
DL
14450 dev_priv->display.get_initial_plane_config =
14451 i9xx_get_initial_plane_config;
d6dfee7a 14452 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
76e5a89c
DV
14453 dev_priv->display.crtc_enable = i9xx_crtc_enable;
14454 dev_priv->display.crtc_disable = i9xx_crtc_disable;
81c97f52
ACO
14455 } else {
14456 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
14457 dev_priv->display.get_initial_plane_config =
14458 i9xx_get_initial_plane_config;
14459 dev_priv->display.crtc_compute_clock = i8xx_crtc_compute_clock;
14460 dev_priv->display.crtc_enable = i9xx_crtc_enable;
14461 dev_priv->display.crtc_disable = i9xx_crtc_disable;
f564048e 14462 }
e70236a8 14463
e70236a8 14464 /* Returns the core display clock speed */
88212941 14465 if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv))
1652d19e
VS
14466 dev_priv->display.get_display_clock_speed =
14467 skylake_get_display_clock_speed;
88212941 14468 else if (IS_BROXTON(dev_priv))
acd3f3d3
BP
14469 dev_priv->display.get_display_clock_speed =
14470 broxton_get_display_clock_speed;
88212941 14471 else if (IS_BROADWELL(dev_priv))
1652d19e
VS
14472 dev_priv->display.get_display_clock_speed =
14473 broadwell_get_display_clock_speed;
88212941 14474 else if (IS_HASWELL(dev_priv))
1652d19e
VS
14475 dev_priv->display.get_display_clock_speed =
14476 haswell_get_display_clock_speed;
88212941 14477 else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
25eb05fc
JB
14478 dev_priv->display.get_display_clock_speed =
14479 valleyview_get_display_clock_speed;
88212941 14480 else if (IS_GEN5(dev_priv))
b37a6434
VS
14481 dev_priv->display.get_display_clock_speed =
14482 ilk_get_display_clock_speed;
88212941
ID
14483 else if (IS_I945G(dev_priv) || IS_BROADWATER(dev_priv) ||
14484 IS_GEN6(dev_priv) || IS_IVYBRIDGE(dev_priv))
e70236a8
JB
14485 dev_priv->display.get_display_clock_speed =
14486 i945_get_display_clock_speed;
88212941 14487 else if (IS_GM45(dev_priv))
34edce2f
VS
14488 dev_priv->display.get_display_clock_speed =
14489 gm45_get_display_clock_speed;
88212941 14490 else if (IS_CRESTLINE(dev_priv))
34edce2f
VS
14491 dev_priv->display.get_display_clock_speed =
14492 i965gm_get_display_clock_speed;
88212941 14493 else if (IS_PINEVIEW(dev_priv))
34edce2f
VS
14494 dev_priv->display.get_display_clock_speed =
14495 pnv_get_display_clock_speed;
88212941 14496 else if (IS_G33(dev_priv) || IS_G4X(dev_priv))
34edce2f
VS
14497 dev_priv->display.get_display_clock_speed =
14498 g33_get_display_clock_speed;
88212941 14499 else if (IS_I915G(dev_priv))
e70236a8
JB
14500 dev_priv->display.get_display_clock_speed =
14501 i915_get_display_clock_speed;
88212941 14502 else if (IS_I945GM(dev_priv) || IS_845G(dev_priv))
e70236a8
JB
14503 dev_priv->display.get_display_clock_speed =
14504 i9xx_misc_get_display_clock_speed;
88212941 14505 else if (IS_I915GM(dev_priv))
e70236a8
JB
14506 dev_priv->display.get_display_clock_speed =
14507 i915gm_get_display_clock_speed;
88212941 14508 else if (IS_I865G(dev_priv))
e70236a8
JB
14509 dev_priv->display.get_display_clock_speed =
14510 i865_get_display_clock_speed;
88212941 14511 else if (IS_I85X(dev_priv))
e70236a8 14512 dev_priv->display.get_display_clock_speed =
1b1d2716 14513 i85x_get_display_clock_speed;
623e01e5 14514 else { /* 830 */
88212941 14515 WARN(!IS_I830(dev_priv), "Unknown platform. Assuming 133 MHz CDCLK\n");
e70236a8
JB
14516 dev_priv->display.get_display_clock_speed =
14517 i830_get_display_clock_speed;
623e01e5 14518 }
e70236a8 14519
88212941 14520 if (IS_GEN5(dev_priv)) {
3bb11b53 14521 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
88212941 14522 } else if (IS_GEN6(dev_priv)) {
3bb11b53 14523 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
88212941 14524 } else if (IS_IVYBRIDGE(dev_priv)) {
3bb11b53
SJ
14525 /* FIXME: detect B0+ stepping and use auto training */
14526 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
88212941 14527 } else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
3bb11b53 14528 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
445e780b
VS
14529 }
14530
14531 if (IS_BROADWELL(dev_priv)) {
14532 dev_priv->display.modeset_commit_cdclk =
14533 broadwell_modeset_commit_cdclk;
14534 dev_priv->display.modeset_calc_cdclk =
14535 broadwell_modeset_calc_cdclk;
88212941 14536 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
27c329ed
ML
14537 dev_priv->display.modeset_commit_cdclk =
14538 valleyview_modeset_commit_cdclk;
14539 dev_priv->display.modeset_calc_cdclk =
14540 valleyview_modeset_calc_cdclk;
88212941 14541 } else if (IS_BROXTON(dev_priv)) {
27c329ed
ML
14542 dev_priv->display.modeset_commit_cdclk =
14543 broxton_modeset_commit_cdclk;
14544 dev_priv->display.modeset_calc_cdclk =
14545 broxton_modeset_calc_cdclk;
c89e39f3
CT
14546 } else if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv)) {
14547 dev_priv->display.modeset_commit_cdclk =
14548 skl_modeset_commit_cdclk;
14549 dev_priv->display.modeset_calc_cdclk =
14550 skl_modeset_calc_cdclk;
e70236a8
JB
14551 }
14552}
14553
b690e96c
JB
14554/*
14555 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
14556 * resume, or other times. This quirk makes sure that's the case for
14557 * affected systems.
14558 */
0206e353 14559static void quirk_pipea_force(struct drm_device *dev)
b690e96c
JB
14560{
14561 struct drm_i915_private *dev_priv = dev->dev_private;
14562
14563 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
bc0daf48 14564 DRM_INFO("applying pipe a force quirk\n");
b690e96c
JB
14565}
14566
b6b5d049
VS
14567static void quirk_pipeb_force(struct drm_device *dev)
14568{
14569 struct drm_i915_private *dev_priv = dev->dev_private;
14570
14571 dev_priv->quirks |= QUIRK_PIPEB_FORCE;
14572 DRM_INFO("applying pipe b force quirk\n");
14573}
14574
435793df
KP
14575/*
14576 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
14577 */
14578static void quirk_ssc_force_disable(struct drm_device *dev)
14579{
14580 struct drm_i915_private *dev_priv = dev->dev_private;
14581 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
bc0daf48 14582 DRM_INFO("applying lvds SSC disable quirk\n");
435793df
KP
14583}
14584
4dca20ef 14585/*
5a15ab5b
CE
14586 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
14587 * brightness value
4dca20ef
CE
14588 */
14589static void quirk_invert_brightness(struct drm_device *dev)
14590{
14591 struct drm_i915_private *dev_priv = dev->dev_private;
14592 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
bc0daf48 14593 DRM_INFO("applying inverted panel brightness quirk\n");
435793df
KP
14594}
14595
9c72cc6f
SD
14596/* Some VBT's incorrectly indicate no backlight is present */
14597static void quirk_backlight_present(struct drm_device *dev)
14598{
14599 struct drm_i915_private *dev_priv = dev->dev_private;
14600 dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
14601 DRM_INFO("applying backlight present quirk\n");
14602}
14603
b690e96c
JB
14604struct intel_quirk {
14605 int device;
14606 int subsystem_vendor;
14607 int subsystem_device;
14608 void (*hook)(struct drm_device *dev);
14609};
14610
5f85f176
EE
14611/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
14612struct intel_dmi_quirk {
14613 void (*hook)(struct drm_device *dev);
14614 const struct dmi_system_id (*dmi_id_list)[];
14615};
14616
14617static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
14618{
14619 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
14620 return 1;
14621}
14622
14623static const struct intel_dmi_quirk intel_dmi_quirks[] = {
14624 {
14625 .dmi_id_list = &(const struct dmi_system_id[]) {
14626 {
14627 .callback = intel_dmi_reverse_brightness,
14628 .ident = "NCR Corporation",
14629 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
14630 DMI_MATCH(DMI_PRODUCT_NAME, ""),
14631 },
14632 },
14633 { } /* terminating entry */
14634 },
14635 .hook = quirk_invert_brightness,
14636 },
14637};
14638
c43b5634 14639static struct intel_quirk intel_quirks[] = {
b690e96c
JB
14640 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
14641 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
14642
b690e96c
JB
14643 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
14644 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
14645
5f080c0f
VS
14646 /* 830 needs to leave pipe A & dpll A up */
14647 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
14648
b6b5d049
VS
14649 /* 830 needs to leave pipe B & dpll B up */
14650 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
14651
435793df
KP
14652 /* Lenovo U160 cannot use SSC on LVDS */
14653 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
070d329a
MAS
14654
14655 /* Sony Vaio Y cannot use SSC on LVDS */
14656 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
5a15ab5b 14657
be505f64
AH
14658 /* Acer Aspire 5734Z must invert backlight brightness */
14659 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
14660
14661 /* Acer/eMachines G725 */
14662 { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
14663
14664 /* Acer/eMachines e725 */
14665 { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
14666
14667 /* Acer/Packard Bell NCL20 */
14668 { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
14669
14670 /* Acer Aspire 4736Z */
14671 { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
0f540c3a
JN
14672
14673 /* Acer Aspire 5336 */
14674 { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
2e93a1aa
SD
14675
14676 /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
14677 { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
d4967d8c 14678
dfb3d47b
SD
14679 /* Acer C720 Chromebook (Core i3 4005U) */
14680 { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
14681
b2a9601c 14682 /* Apple Macbook 2,1 (Core 2 T7400) */
14683 { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
14684
1b9448b0
JN
14685 /* Apple Macbook 4,1 */
14686 { 0x2a02, 0x106b, 0x00a1, quirk_backlight_present },
14687
d4967d8c
SD
14688 /* Toshiba CB35 Chromebook (Celeron 2955U) */
14689 { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
724cb06f
SD
14690
14691 /* HP Chromebook 14 (Celeron 2955U) */
14692 { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
cf6f0af9
JN
14693
14694 /* Dell Chromebook 11 */
14695 { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
9be64eee
JN
14696
14697 /* Dell Chromebook 11 (2015 version) */
14698 { 0x0a16, 0x1028, 0x0a35, quirk_backlight_present },
b690e96c
JB
14699};
14700
14701static void intel_init_quirks(struct drm_device *dev)
14702{
14703 struct pci_dev *d = dev->pdev;
14704 int i;
14705
14706 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
14707 struct intel_quirk *q = &intel_quirks[i];
14708
14709 if (d->device == q->device &&
14710 (d->subsystem_vendor == q->subsystem_vendor ||
14711 q->subsystem_vendor == PCI_ANY_ID) &&
14712 (d->subsystem_device == q->subsystem_device ||
14713 q->subsystem_device == PCI_ANY_ID))
14714 q->hook(dev);
14715 }
5f85f176
EE
14716 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
14717 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
14718 intel_dmi_quirks[i].hook(dev);
14719 }
b690e96c
JB
14720}
14721
9cce37f4
JB
14722/* Disable the VGA plane that we never use */
14723static void i915_disable_vga(struct drm_device *dev)
14724{
14725 struct drm_i915_private *dev_priv = dev->dev_private;
14726 u8 sr1;
f0f59a00 14727 i915_reg_t vga_reg = i915_vgacntrl_reg(dev);
9cce37f4 14728
2b37c616 14729 /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
9cce37f4 14730 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
3fdcf431 14731 outb(SR01, VGA_SR_INDEX);
9cce37f4
JB
14732 sr1 = inb(VGA_SR_DATA);
14733 outb(sr1 | 1<<5, VGA_SR_DATA);
14734 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
14735 udelay(300);
14736
01f5a626 14737 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
9cce37f4
JB
14738 POSTING_READ(vga_reg);
14739}
14740
f817586c
DV
14741void intel_modeset_init_hw(struct drm_device *dev)
14742{
1a617b77
ML
14743 struct drm_i915_private *dev_priv = dev->dev_private;
14744
b6283055 14745 intel_update_cdclk(dev);
1a617b77
ML
14746
14747 dev_priv->atomic_cdclk_freq = dev_priv->cdclk_freq;
14748
f817586c 14749 intel_init_clock_gating(dev);
dc97997a 14750 intel_enable_gt_powersave(dev_priv);
f817586c
DV
14751}
14752
d93c0372
MR
14753/*
14754 * Calculate what we think the watermarks should be for the state we've read
14755 * out of the hardware and then immediately program those watermarks so that
14756 * we ensure the hardware settings match our internal state.
14757 *
14758 * We can calculate what we think WM's should be by creating a duplicate of the
14759 * current state (which was constructed during hardware readout) and running it
14760 * through the atomic check code to calculate new watermark values in the
14761 * state object.
14762 */
14763static void sanitize_watermarks(struct drm_device *dev)
14764{
14765 struct drm_i915_private *dev_priv = to_i915(dev);
14766 struct drm_atomic_state *state;
14767 struct drm_crtc *crtc;
14768 struct drm_crtc_state *cstate;
14769 struct drm_modeset_acquire_ctx ctx;
14770 int ret;
14771 int i;
14772
14773 /* Only supported on platforms that use atomic watermark design */
ed4a6a7c 14774 if (!dev_priv->display.optimize_watermarks)
d93c0372
MR
14775 return;
14776
14777 /*
14778 * We need to hold connection_mutex before calling duplicate_state so
14779 * that the connector loop is protected.
14780 */
14781 drm_modeset_acquire_init(&ctx, 0);
14782retry:
0cd1262d 14783 ret = drm_modeset_lock_all_ctx(dev, &ctx);
d93c0372
MR
14784 if (ret == -EDEADLK) {
14785 drm_modeset_backoff(&ctx);
14786 goto retry;
14787 } else if (WARN_ON(ret)) {
0cd1262d 14788 goto fail;
d93c0372
MR
14789 }
14790
14791 state = drm_atomic_helper_duplicate_state(dev, &ctx);
14792 if (WARN_ON(IS_ERR(state)))
0cd1262d 14793 goto fail;
d93c0372 14794
ed4a6a7c
MR
14795 /*
14796 * Hardware readout is the only time we don't want to calculate
14797 * intermediate watermarks (since we don't trust the current
14798 * watermarks).
14799 */
14800 to_intel_atomic_state(state)->skip_intermediate_wm = true;
14801
d93c0372
MR
14802 ret = intel_atomic_check(dev, state);
14803 if (ret) {
14804 /*
14805 * If we fail here, it means that the hardware appears to be
14806 * programmed in a way that shouldn't be possible, given our
14807 * understanding of watermark requirements. This might mean a
14808 * mistake in the hardware readout code or a mistake in the
14809 * watermark calculations for a given platform. Raise a WARN
14810 * so that this is noticeable.
14811 *
14812 * If this actually happens, we'll have to just leave the
14813 * BIOS-programmed watermarks untouched and hope for the best.
14814 */
14815 WARN(true, "Could not determine valid watermarks for inherited state\n");
0cd1262d 14816 goto fail;
d93c0372
MR
14817 }
14818
14819 /* Write calculated watermark values back */
d93c0372
MR
14820 for_each_crtc_in_state(state, crtc, cstate, i) {
14821 struct intel_crtc_state *cs = to_intel_crtc_state(cstate);
14822
ed4a6a7c
MR
14823 cs->wm.need_postvbl_update = true;
14824 dev_priv->display.optimize_watermarks(cs);
d93c0372
MR
14825 }
14826
14827 drm_atomic_state_free(state);
0cd1262d 14828fail:
d93c0372
MR
14829 drm_modeset_drop_locks(&ctx);
14830 drm_modeset_acquire_fini(&ctx);
14831}
14832
79e53945
JB
14833void intel_modeset_init(struct drm_device *dev)
14834{
72e96d64
JL
14835 struct drm_i915_private *dev_priv = to_i915(dev);
14836 struct i915_ggtt *ggtt = &dev_priv->ggtt;
1fe47785 14837 int sprite, ret;
8cc87b75 14838 enum pipe pipe;
46f297fb 14839 struct intel_crtc *crtc;
79e53945
JB
14840
14841 drm_mode_config_init(dev);
14842
14843 dev->mode_config.min_width = 0;
14844 dev->mode_config.min_height = 0;
14845
019d96cb
DA
14846 dev->mode_config.preferred_depth = 24;
14847 dev->mode_config.prefer_shadow = 1;
14848
25bab385
TU
14849 dev->mode_config.allow_fb_modifiers = true;
14850
e6ecefaa 14851 dev->mode_config.funcs = &intel_mode_funcs;
79e53945 14852
b690e96c
JB
14853 intel_init_quirks(dev);
14854
1fa61106
ED
14855 intel_init_pm(dev);
14856
e3c74757
BW
14857 if (INTEL_INFO(dev)->num_pipes == 0)
14858 return;
14859
69f92f67
LW
14860 /*
14861 * There may be no VBT; and if the BIOS enabled SSC we can
14862 * just keep using it to avoid unnecessary flicker. Whereas if the
14863 * BIOS isn't using it, don't assume it will work even if the VBT
14864 * indicates as much.
14865 */
14866 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)) {
14867 bool bios_lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
14868 DREF_SSC1_ENABLE);
14869
14870 if (dev_priv->vbt.lvds_use_ssc != bios_lvds_use_ssc) {
14871 DRM_DEBUG_KMS("SSC %sabled by BIOS, overriding VBT which says %sabled\n",
14872 bios_lvds_use_ssc ? "en" : "dis",
14873 dev_priv->vbt.lvds_use_ssc ? "en" : "dis");
14874 dev_priv->vbt.lvds_use_ssc = bios_lvds_use_ssc;
14875 }
14876 }
14877
a6c45cf0
CW
14878 if (IS_GEN2(dev)) {
14879 dev->mode_config.max_width = 2048;
14880 dev->mode_config.max_height = 2048;
14881 } else if (IS_GEN3(dev)) {
5e4d6fa7
KP
14882 dev->mode_config.max_width = 4096;
14883 dev->mode_config.max_height = 4096;
79e53945 14884 } else {
a6c45cf0
CW
14885 dev->mode_config.max_width = 8192;
14886 dev->mode_config.max_height = 8192;
79e53945 14887 }
068be561 14888
dc41c154
VS
14889 if (IS_845G(dev) || IS_I865G(dev)) {
14890 dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
14891 dev->mode_config.cursor_height = 1023;
14892 } else if (IS_GEN2(dev)) {
068be561
DL
14893 dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
14894 dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
14895 } else {
14896 dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
14897 dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
14898 }
14899
72e96d64 14900 dev->mode_config.fb_base = ggtt->mappable_base;
79e53945 14901
28c97730 14902 DRM_DEBUG_KMS("%d display pipe%s available.\n",
7eb552ae
BW
14903 INTEL_INFO(dev)->num_pipes,
14904 INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
79e53945 14905
055e393f 14906 for_each_pipe(dev_priv, pipe) {
8cc87b75 14907 intel_crtc_init(dev, pipe);
3bdcfc0c 14908 for_each_sprite(dev_priv, pipe, sprite) {
1fe47785 14909 ret = intel_plane_init(dev, pipe, sprite);
7f1f3851 14910 if (ret)
06da8da2 14911 DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
1fe47785 14912 pipe_name(pipe), sprite_name(pipe, sprite), ret);
7f1f3851 14913 }
79e53945
JB
14914 }
14915
bfa7df01
VS
14916 intel_update_czclk(dev_priv);
14917 intel_update_cdclk(dev);
14918
e72f9fbf 14919 intel_shared_dpll_init(dev);
ee7b9f93 14920
b2045352
VS
14921 if (dev_priv->max_cdclk_freq == 0)
14922 intel_update_max_cdclk(dev);
14923
9cce37f4
JB
14924 /* Just disable it once at startup */
14925 i915_disable_vga(dev);
79e53945 14926 intel_setup_outputs(dev);
11be49eb 14927
6e9f798d 14928 drm_modeset_lock_all(dev);
043e9bda 14929 intel_modeset_setup_hw_state(dev);
6e9f798d 14930 drm_modeset_unlock_all(dev);
46f297fb 14931
d3fcc808 14932 for_each_intel_crtc(dev, crtc) {
eeebeac5
ML
14933 struct intel_initial_plane_config plane_config = {};
14934
46f297fb
JB
14935 if (!crtc->active)
14936 continue;
14937
46f297fb 14938 /*
46f297fb
JB
14939 * Note that reserving the BIOS fb up front prevents us
14940 * from stuffing other stolen allocations like the ring
14941 * on top. This prevents some ugliness at boot time, and
14942 * can even allow for smooth boot transitions if the BIOS
14943 * fb is large enough for the active pipe configuration.
14944 */
eeebeac5
ML
14945 dev_priv->display.get_initial_plane_config(crtc,
14946 &plane_config);
14947
14948 /*
14949 * If the fb is shared between multiple heads, we'll
14950 * just get the first one.
14951 */
14952 intel_find_initial_plane_obj(crtc, &plane_config);
46f297fb 14953 }
d93c0372
MR
14954
14955 /*
14956 * Make sure hardware watermarks really match the state we read out.
14957 * Note that we need to do this after reconstructing the BIOS fb's
14958 * since the watermark calculation done here will use pstate->fb.
14959 */
14960 sanitize_watermarks(dev);
2c7111db
CW
14961}
14962
7fad798e
DV
14963static void intel_enable_pipe_a(struct drm_device *dev)
14964{
14965 struct intel_connector *connector;
14966 struct drm_connector *crt = NULL;
14967 struct intel_load_detect_pipe load_detect_temp;
208bf9fd 14968 struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
7fad798e
DV
14969
14970 /* We can't just switch on the pipe A, we need to set things up with a
14971 * proper mode and output configuration. As a gross hack, enable pipe A
14972 * by enabling the load detect pipe once. */
3a3371ff 14973 for_each_intel_connector(dev, connector) {
7fad798e
DV
14974 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
14975 crt = &connector->base;
14976 break;
14977 }
14978 }
14979
14980 if (!crt)
14981 return;
14982
208bf9fd 14983 if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
49172fee 14984 intel_release_load_detect_pipe(crt, &load_detect_temp, ctx);
7fad798e
DV
14985}
14986
fa555837
DV
14987static bool
14988intel_check_plane_mapping(struct intel_crtc *crtc)
14989{
7eb552ae
BW
14990 struct drm_device *dev = crtc->base.dev;
14991 struct drm_i915_private *dev_priv = dev->dev_private;
649636ef 14992 u32 val;
fa555837 14993
7eb552ae 14994 if (INTEL_INFO(dev)->num_pipes == 1)
fa555837
DV
14995 return true;
14996
649636ef 14997 val = I915_READ(DSPCNTR(!crtc->plane));
fa555837
DV
14998
14999 if ((val & DISPLAY_PLANE_ENABLE) &&
15000 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
15001 return false;
15002
15003 return true;
15004}
15005
02e93c35
VS
15006static bool intel_crtc_has_encoders(struct intel_crtc *crtc)
15007{
15008 struct drm_device *dev = crtc->base.dev;
15009 struct intel_encoder *encoder;
15010
15011 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
15012 return true;
15013
15014 return false;
15015}
15016
dd756198
VS
15017static bool intel_encoder_has_connectors(struct intel_encoder *encoder)
15018{
15019 struct drm_device *dev = encoder->base.dev;
15020 struct intel_connector *connector;
15021
15022 for_each_connector_on_encoder(dev, &encoder->base, connector)
15023 return true;
15024
15025 return false;
15026}
15027
24929352
DV
15028static void intel_sanitize_crtc(struct intel_crtc *crtc)
15029{
15030 struct drm_device *dev = crtc->base.dev;
15031 struct drm_i915_private *dev_priv = dev->dev_private;
4d1de975 15032 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
24929352 15033
24929352 15034 /* Clear any frame start delays used for debugging left by the BIOS */
4d1de975
JN
15035 if (!transcoder_is_dsi(cpu_transcoder)) {
15036 i915_reg_t reg = PIPECONF(cpu_transcoder);
15037
15038 I915_WRITE(reg,
15039 I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
15040 }
24929352 15041
d3eaf884 15042 /* restore vblank interrupts to correct state */
9625604c 15043 drm_crtc_vblank_reset(&crtc->base);
d297e103 15044 if (crtc->active) {
f9cd7b88
VS
15045 struct intel_plane *plane;
15046
9625604c 15047 drm_crtc_vblank_on(&crtc->base);
f9cd7b88
VS
15048
15049 /* Disable everything but the primary plane */
15050 for_each_intel_plane_on_crtc(dev, crtc, plane) {
15051 if (plane->base.type == DRM_PLANE_TYPE_PRIMARY)
15052 continue;
15053
15054 plane->disable_plane(&plane->base, &crtc->base);
15055 }
9625604c 15056 }
d3eaf884 15057
24929352 15058 /* We need to sanitize the plane -> pipe mapping first because this will
fa555837
DV
15059 * disable the crtc (and hence change the state) if it is wrong. Note
15060 * that gen4+ has a fixed plane -> pipe mapping. */
15061 if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
24929352
DV
15062 bool plane;
15063
24929352
DV
15064 DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
15065 crtc->base.base.id);
15066
15067 /* Pipe has the wrong plane attached and the plane is active.
15068 * Temporarily change the plane mapping and disable everything
15069 * ... */
15070 plane = crtc->plane;
b70709a6 15071 to_intel_plane_state(crtc->base.primary->state)->visible = true;
24929352 15072 crtc->plane = !plane;
b17d48e2 15073 intel_crtc_disable_noatomic(&crtc->base);
24929352 15074 crtc->plane = plane;
24929352 15075 }
24929352 15076
7fad798e
DV
15077 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
15078 crtc->pipe == PIPE_A && !crtc->active) {
15079 /* BIOS forgot to enable pipe A, this mostly happens after
15080 * resume. Force-enable the pipe to fix this, the update_dpms
15081 * call below we restore the pipe to the right state, but leave
15082 * the required bits on. */
15083 intel_enable_pipe_a(dev);
15084 }
15085
24929352
DV
15086 /* Adjust the state of the output pipe according to whether we
15087 * have active connectors/encoders. */
842e0307 15088 if (crtc->active && !intel_crtc_has_encoders(crtc))
b17d48e2 15089 intel_crtc_disable_noatomic(&crtc->base);
24929352 15090
a3ed6aad 15091 if (crtc->active || HAS_GMCH_DISPLAY(dev)) {
4cc31489
DV
15092 /*
15093 * We start out with underrun reporting disabled to avoid races.
15094 * For correct bookkeeping mark this on active crtcs.
15095 *
c5ab3bc0
DV
15096 * Also on gmch platforms we dont have any hardware bits to
15097 * disable the underrun reporting. Which means we need to start
15098 * out with underrun reporting disabled also on inactive pipes,
15099 * since otherwise we'll complain about the garbage we read when
15100 * e.g. coming up after runtime pm.
15101 *
4cc31489
DV
15102 * No protection against concurrent access is required - at
15103 * worst a fifo underrun happens which also sets this to false.
15104 */
15105 crtc->cpu_fifo_underrun_disabled = true;
15106 crtc->pch_fifo_underrun_disabled = true;
15107 }
24929352
DV
15108}
15109
15110static void intel_sanitize_encoder(struct intel_encoder *encoder)
15111{
15112 struct intel_connector *connector;
15113 struct drm_device *dev = encoder->base.dev;
15114
15115 /* We need to check both for a crtc link (meaning that the
15116 * encoder is active and trying to read from a pipe) and the
15117 * pipe itself being active. */
15118 bool has_active_crtc = encoder->base.crtc &&
15119 to_intel_crtc(encoder->base.crtc)->active;
15120
dd756198 15121 if (intel_encoder_has_connectors(encoder) && !has_active_crtc) {
24929352
DV
15122 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
15123 encoder->base.base.id,
8e329a03 15124 encoder->base.name);
24929352
DV
15125
15126 /* Connector is active, but has no active pipe. This is
15127 * fallout from our resume register restoring. Disable
15128 * the encoder manually again. */
15129 if (encoder->base.crtc) {
15130 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
15131 encoder->base.base.id,
8e329a03 15132 encoder->base.name);
24929352 15133 encoder->disable(encoder);
a62d1497
VS
15134 if (encoder->post_disable)
15135 encoder->post_disable(encoder);
24929352 15136 }
7f1950fb 15137 encoder->base.crtc = NULL;
24929352
DV
15138
15139 /* Inconsistent output/port/pipe state happens presumably due to
15140 * a bug in one of the get_hw_state functions. Or someplace else
15141 * in our code, like the register restore mess on resume. Clamp
15142 * things to off as a safer default. */
3a3371ff 15143 for_each_intel_connector(dev, connector) {
24929352
DV
15144 if (connector->encoder != encoder)
15145 continue;
7f1950fb
EE
15146 connector->base.dpms = DRM_MODE_DPMS_OFF;
15147 connector->base.encoder = NULL;
24929352
DV
15148 }
15149 }
15150 /* Enabled encoders without active connectors will be fixed in
15151 * the crtc fixup. */
15152}
15153
04098753 15154void i915_redisable_vga_power_on(struct drm_device *dev)
0fde901f
KM
15155{
15156 struct drm_i915_private *dev_priv = dev->dev_private;
f0f59a00 15157 i915_reg_t vga_reg = i915_vgacntrl_reg(dev);
0fde901f 15158
04098753
ID
15159 if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
15160 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
15161 i915_disable_vga(dev);
15162 }
15163}
15164
15165void i915_redisable_vga(struct drm_device *dev)
15166{
15167 struct drm_i915_private *dev_priv = dev->dev_private;
15168
8dc8a27c
PZ
15169 /* This function can be called both from intel_modeset_setup_hw_state or
15170 * at a very early point in our resume sequence, where the power well
15171 * structures are not yet restored. Since this function is at a very
15172 * paranoid "someone might have enabled VGA while we were not looking"
15173 * level, just check if the power well is enabled instead of trying to
15174 * follow the "don't touch the power well if we don't need it" policy
15175 * the rest of the driver uses. */
6392f847 15176 if (!intel_display_power_get_if_enabled(dev_priv, POWER_DOMAIN_VGA))
8dc8a27c
PZ
15177 return;
15178
04098753 15179 i915_redisable_vga_power_on(dev);
6392f847
ID
15180
15181 intel_display_power_put(dev_priv, POWER_DOMAIN_VGA);
0fde901f
KM
15182}
15183
f9cd7b88 15184static bool primary_get_hw_state(struct intel_plane *plane)
98ec7739 15185{
f9cd7b88 15186 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
98ec7739 15187
f9cd7b88 15188 return I915_READ(DSPCNTR(plane->plane)) & DISPLAY_PLANE_ENABLE;
d032ffa0
ML
15189}
15190
f9cd7b88
VS
15191/* FIXME read out full plane state for all planes */
15192static void readout_plane_state(struct intel_crtc *crtc)
d032ffa0 15193{
b26d3ea3 15194 struct drm_plane *primary = crtc->base.primary;
f9cd7b88 15195 struct intel_plane_state *plane_state =
b26d3ea3 15196 to_intel_plane_state(primary->state);
d032ffa0 15197
19b8d387 15198 plane_state->visible = crtc->active &&
b26d3ea3
ML
15199 primary_get_hw_state(to_intel_plane(primary));
15200
15201 if (plane_state->visible)
15202 crtc->base.state->plane_mask |= 1 << drm_plane_index(primary);
98ec7739
VS
15203}
15204
30e984df 15205static void intel_modeset_readout_hw_state(struct drm_device *dev)
24929352
DV
15206{
15207 struct drm_i915_private *dev_priv = dev->dev_private;
15208 enum pipe pipe;
24929352
DV
15209 struct intel_crtc *crtc;
15210 struct intel_encoder *encoder;
15211 struct intel_connector *connector;
5358901f 15212 int i;
24929352 15213
565602d7
ML
15214 dev_priv->active_crtcs = 0;
15215
d3fcc808 15216 for_each_intel_crtc(dev, crtc) {
565602d7
ML
15217 struct intel_crtc_state *crtc_state = crtc->config;
15218 int pixclk = 0;
3b117c8f 15219
565602d7
ML
15220 __drm_atomic_helper_crtc_destroy_state(&crtc->base, &crtc_state->base);
15221 memset(crtc_state, 0, sizeof(*crtc_state));
15222 crtc_state->base.crtc = &crtc->base;
24929352 15223
565602d7
ML
15224 crtc_state->base.active = crtc_state->base.enable =
15225 dev_priv->display.get_pipe_config(crtc, crtc_state);
15226
15227 crtc->base.enabled = crtc_state->base.enable;
15228 crtc->active = crtc_state->base.active;
15229
15230 if (crtc_state->base.active) {
15231 dev_priv->active_crtcs |= 1 << crtc->pipe;
15232
c89e39f3 15233 if (INTEL_GEN(dev_priv) >= 9 || IS_BROADWELL(dev_priv))
565602d7 15234 pixclk = ilk_pipe_pixel_rate(crtc_state);
9558d15d 15235 else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
565602d7
ML
15236 pixclk = crtc_state->base.adjusted_mode.crtc_clock;
15237 else
15238 WARN_ON(dev_priv->display.modeset_calc_cdclk);
9558d15d
VS
15239
15240 /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
15241 if (IS_BROADWELL(dev_priv) && crtc_state->ips_enabled)
15242 pixclk = DIV_ROUND_UP(pixclk * 100, 95);
565602d7
ML
15243 }
15244
15245 dev_priv->min_pixclk[crtc->pipe] = pixclk;
b70709a6 15246
f9cd7b88 15247 readout_plane_state(crtc);
24929352
DV
15248
15249 DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
15250 crtc->base.base.id,
15251 crtc->active ? "enabled" : "disabled");
15252 }
15253
5358901f
DV
15254 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
15255 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
15256
2edd6443
ACO
15257 pll->on = pll->funcs.get_hw_state(dev_priv, pll,
15258 &pll->config.hw_state);
3e369b76 15259 pll->config.crtc_mask = 0;
d3fcc808 15260 for_each_intel_crtc(dev, crtc) {
2dd66ebd 15261 if (crtc->active && crtc->config->shared_dpll == pll)
3e369b76 15262 pll->config.crtc_mask |= 1 << crtc->pipe;
5358901f 15263 }
2dd66ebd 15264 pll->active_mask = pll->config.crtc_mask;
5358901f 15265
1e6f2ddc 15266 DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
3e369b76 15267 pll->name, pll->config.crtc_mask, pll->on);
5358901f
DV
15268 }
15269
b2784e15 15270 for_each_intel_encoder(dev, encoder) {
24929352
DV
15271 pipe = 0;
15272
15273 if (encoder->get_hw_state(encoder, &pipe)) {
045ac3b5
JB
15274 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
15275 encoder->base.crtc = &crtc->base;
6e3c9717 15276 encoder->get_config(encoder, crtc->config);
24929352
DV
15277 } else {
15278 encoder->base.crtc = NULL;
15279 }
15280
6f2bcceb 15281 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
24929352 15282 encoder->base.base.id,
8e329a03 15283 encoder->base.name,
24929352 15284 encoder->base.crtc ? "enabled" : "disabled",
6f2bcceb 15285 pipe_name(pipe));
24929352
DV
15286 }
15287
3a3371ff 15288 for_each_intel_connector(dev, connector) {
24929352
DV
15289 if (connector->get_hw_state(connector)) {
15290 connector->base.dpms = DRM_MODE_DPMS_ON;
2aa974c9
ML
15291
15292 encoder = connector->encoder;
15293 connector->base.encoder = &encoder->base;
15294
15295 if (encoder->base.crtc &&
15296 encoder->base.crtc->state->active) {
15297 /*
15298 * This has to be done during hardware readout
15299 * because anything calling .crtc_disable may
15300 * rely on the connector_mask being accurate.
15301 */
15302 encoder->base.crtc->state->connector_mask |=
15303 1 << drm_connector_index(&connector->base);
e87a52b3
ML
15304 encoder->base.crtc->state->encoder_mask |=
15305 1 << drm_encoder_index(&encoder->base);
2aa974c9
ML
15306 }
15307
24929352
DV
15308 } else {
15309 connector->base.dpms = DRM_MODE_DPMS_OFF;
15310 connector->base.encoder = NULL;
15311 }
15312 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
15313 connector->base.base.id,
c23cc417 15314 connector->base.name,
24929352
DV
15315 connector->base.encoder ? "enabled" : "disabled");
15316 }
7f4c6284
VS
15317
15318 for_each_intel_crtc(dev, crtc) {
15319 crtc->base.hwmode = crtc->config->base.adjusted_mode;
15320
15321 memset(&crtc->base.mode, 0, sizeof(crtc->base.mode));
15322 if (crtc->base.state->active) {
15323 intel_mode_from_pipe_config(&crtc->base.mode, crtc->config);
15324 intel_mode_from_pipe_config(&crtc->base.state->adjusted_mode, crtc->config);
15325 WARN_ON(drm_atomic_set_mode_for_crtc(crtc->base.state, &crtc->base.mode));
15326
15327 /*
15328 * The initial mode needs to be set in order to keep
15329 * the atomic core happy. It wants a valid mode if the
15330 * crtc's enabled, so we do the above call.
15331 *
15332 * At this point some state updated by the connectors
15333 * in their ->detect() callback has not run yet, so
15334 * no recalculation can be done yet.
15335 *
15336 * Even if we could do a recalculation and modeset
15337 * right now it would cause a double modeset if
15338 * fbdev or userspace chooses a different initial mode.
15339 *
15340 * If that happens, someone indicated they wanted a
15341 * mode change, which means it's safe to do a full
15342 * recalculation.
15343 */
15344 crtc->base.state->mode.private_flags = I915_MODE_FLAG_INHERITED;
9eca6832
VS
15345
15346 drm_calc_timestamping_constants(&crtc->base, &crtc->base.hwmode);
15347 update_scanline_offset(crtc);
7f4c6284 15348 }
e3b247da
VS
15349
15350 intel_pipe_config_sanity_check(dev_priv, crtc->config);
7f4c6284 15351 }
30e984df
DV
15352}
15353
043e9bda
ML
15354/* Scan out the current hw modeset state,
15355 * and sanitizes it to the current state
15356 */
15357static void
15358intel_modeset_setup_hw_state(struct drm_device *dev)
30e984df
DV
15359{
15360 struct drm_i915_private *dev_priv = dev->dev_private;
15361 enum pipe pipe;
30e984df
DV
15362 struct intel_crtc *crtc;
15363 struct intel_encoder *encoder;
35c95375 15364 int i;
30e984df
DV
15365
15366 intel_modeset_readout_hw_state(dev);
24929352
DV
15367
15368 /* HW state is read out, now we need to sanitize this mess. */
b2784e15 15369 for_each_intel_encoder(dev, encoder) {
24929352
DV
15370 intel_sanitize_encoder(encoder);
15371 }
15372
055e393f 15373 for_each_pipe(dev_priv, pipe) {
24929352
DV
15374 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
15375 intel_sanitize_crtc(crtc);
6e3c9717
ACO
15376 intel_dump_pipe_config(crtc, crtc->config,
15377 "[setup_hw_state]");
24929352 15378 }
9a935856 15379
d29b2f9d
ACO
15380 intel_modeset_update_connector_atomic_state(dev);
15381
35c95375
DV
15382 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
15383 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
15384
2dd66ebd 15385 if (!pll->on || pll->active_mask)
35c95375
DV
15386 continue;
15387
15388 DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
15389
2edd6443 15390 pll->funcs.disable(dev_priv, pll);
35c95375
DV
15391 pll->on = false;
15392 }
15393
666a4537 15394 if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev))
6eb1a681
VS
15395 vlv_wm_get_hw_state(dev);
15396 else if (IS_GEN9(dev))
3078999f
PB
15397 skl_wm_get_hw_state(dev);
15398 else if (HAS_PCH_SPLIT(dev))
243e6a44 15399 ilk_wm_get_hw_state(dev);
292b990e
ML
15400
15401 for_each_intel_crtc(dev, crtc) {
15402 unsigned long put_domains;
15403
74bff5f9 15404 put_domains = modeset_get_crtc_power_domains(&crtc->base, crtc->config);
292b990e
ML
15405 if (WARN_ON(put_domains))
15406 modeset_put_power_domains(dev_priv, put_domains);
15407 }
15408 intel_display_set_init_power(dev_priv, false);
010cf73d
PZ
15409
15410 intel_fbc_init_pipe_state(dev_priv);
043e9bda 15411}
7d0bc1ea 15412
043e9bda
ML
15413void intel_display_resume(struct drm_device *dev)
15414{
e2c8b870
ML
15415 struct drm_i915_private *dev_priv = to_i915(dev);
15416 struct drm_atomic_state *state = dev_priv->modeset_restore_state;
15417 struct drm_modeset_acquire_ctx ctx;
043e9bda 15418 int ret;
e2c8b870 15419 bool setup = false;
f30da187 15420
e2c8b870 15421 dev_priv->modeset_restore_state = NULL;
043e9bda 15422
ea49c9ac
ML
15423 /*
15424 * This is a cludge because with real atomic modeset mode_config.mutex
15425 * won't be taken. Unfortunately some probed state like
15426 * audio_codec_enable is still protected by mode_config.mutex, so lock
15427 * it here for now.
15428 */
15429 mutex_lock(&dev->mode_config.mutex);
e2c8b870 15430 drm_modeset_acquire_init(&ctx, 0);
043e9bda 15431
e2c8b870
ML
15432retry:
15433 ret = drm_modeset_lock_all_ctx(dev, &ctx);
043e9bda 15434
e2c8b870
ML
15435 if (ret == 0 && !setup) {
15436 setup = true;
043e9bda 15437
e2c8b870
ML
15438 intel_modeset_setup_hw_state(dev);
15439 i915_redisable_vga(dev);
45e2b5f6 15440 }
8af6cf88 15441
e2c8b870
ML
15442 if (ret == 0 && state) {
15443 struct drm_crtc_state *crtc_state;
15444 struct drm_crtc *crtc;
15445 int i;
043e9bda 15446
e2c8b870
ML
15447 state->acquire_ctx = &ctx;
15448
e3d5457c
VS
15449 /* ignore any reset values/BIOS leftovers in the WM registers */
15450 to_intel_atomic_state(state)->skip_intermediate_wm = true;
15451
e2c8b870
ML
15452 for_each_crtc_in_state(state, crtc, crtc_state, i) {
15453 /*
15454 * Force recalculation even if we restore
15455 * current state. With fast modeset this may not result
15456 * in a modeset when the state is compatible.
15457 */
15458 crtc_state->mode_changed = true;
15459 }
15460
15461 ret = drm_atomic_commit(state);
043e9bda
ML
15462 }
15463
e2c8b870
ML
15464 if (ret == -EDEADLK) {
15465 drm_modeset_backoff(&ctx);
15466 goto retry;
15467 }
043e9bda 15468
e2c8b870
ML
15469 drm_modeset_drop_locks(&ctx);
15470 drm_modeset_acquire_fini(&ctx);
ea49c9ac 15471 mutex_unlock(&dev->mode_config.mutex);
043e9bda 15472
e2c8b870
ML
15473 if (ret) {
15474 DRM_ERROR("Restoring old state failed with %i\n", ret);
15475 drm_atomic_state_free(state);
15476 }
2c7111db
CW
15477}
15478
15479void intel_modeset_gem_init(struct drm_device *dev)
15480{
dc97997a 15481 struct drm_i915_private *dev_priv = to_i915(dev);
484b41dd 15482 struct drm_crtc *c;
2ff8fde1 15483 struct drm_i915_gem_object *obj;
e0d6149b 15484 int ret;
484b41dd 15485
dc97997a 15486 intel_init_gt_powersave(dev_priv);
ae48434c 15487
1833b134 15488 intel_modeset_init_hw(dev);
02e792fb 15489
1ee8da6d 15490 intel_setup_overlay(dev_priv);
484b41dd
JB
15491
15492 /*
15493 * Make sure any fbs we allocated at startup are properly
15494 * pinned & fenced. When we do the allocation it's too early
15495 * for this.
15496 */
70e1e0ec 15497 for_each_crtc(dev, c) {
2ff8fde1
MR
15498 obj = intel_fb_obj(c->primary->fb);
15499 if (obj == NULL)
484b41dd
JB
15500 continue;
15501
e0d6149b 15502 mutex_lock(&dev->struct_mutex);
3465c580
VS
15503 ret = intel_pin_and_fence_fb_obj(c->primary->fb,
15504 c->primary->state->rotation);
e0d6149b
TU
15505 mutex_unlock(&dev->struct_mutex);
15506 if (ret) {
484b41dd
JB
15507 DRM_ERROR("failed to pin boot fb on pipe %d\n",
15508 to_intel_crtc(c)->pipe);
66e514c1 15509 drm_framebuffer_unreference(c->primary->fb);
143f73b3
ML
15510 drm_framebuffer_unreference(c->primary->state->fb);
15511 c->primary->fb = c->primary->state->fb = NULL;
36750f28 15512 c->primary->crtc = c->primary->state->crtc = NULL;
36750f28 15513 c->state->plane_mask &= ~(1 << drm_plane_index(c->primary));
484b41dd
JB
15514 }
15515 }
0962c3c9
VS
15516
15517 intel_backlight_register(dev);
79e53945
JB
15518}
15519
4932e2c3
ID
15520void intel_connector_unregister(struct intel_connector *intel_connector)
15521{
15522 struct drm_connector *connector = &intel_connector->base;
15523
15524 intel_panel_destroy_backlight(connector);
34ea3d38 15525 drm_connector_unregister(connector);
4932e2c3
ID
15526}
15527
79e53945
JB
15528void intel_modeset_cleanup(struct drm_device *dev)
15529{
652c393a 15530 struct drm_i915_private *dev_priv = dev->dev_private;
19c8054c 15531 struct intel_connector *connector;
652c393a 15532
dc97997a 15533 intel_disable_gt_powersave(dev_priv);
2eb5252e 15534
0962c3c9
VS
15535 intel_backlight_unregister(dev);
15536
fd0c0642
DV
15537 /*
15538 * Interrupts and polling as the first thing to avoid creating havoc.
2eb5252e 15539 * Too much stuff here (turning of connectors, ...) would
fd0c0642
DV
15540 * experience fancy races otherwise.
15541 */
2aeb7d3a 15542 intel_irq_uninstall(dev_priv);
eb21b92b 15543
fd0c0642
DV
15544 /*
15545 * Due to the hpd irq storm handling the hotplug work can re-arm the
15546 * poll handlers. Hence disable polling after hpd handling is shut down.
15547 */
f87ea761 15548 drm_kms_helper_poll_fini(dev);
fd0c0642 15549
723bfd70
JB
15550 intel_unregister_dsm_handler();
15551
c937ab3e 15552 intel_fbc_global_disable(dev_priv);
69341a5e 15553
1630fe75
CW
15554 /* flush any delayed tasks or pending work */
15555 flush_scheduled_work();
15556
db31af1d 15557 /* destroy the backlight and sysfs files before encoders/connectors */
19c8054c
JN
15558 for_each_intel_connector(dev, connector)
15559 connector->unregister(connector);
d9255d57 15560
79e53945 15561 drm_mode_config_cleanup(dev);
4d7bb011 15562
1ee8da6d 15563 intel_cleanup_overlay(dev_priv);
ae48434c 15564
dc97997a 15565 intel_cleanup_gt_powersave(dev_priv);
f5949141
DV
15566
15567 intel_teardown_gmbus(dev);
79e53945
JB
15568}
15569
f1c79df3
ZW
15570/*
15571 * Return which encoder is currently attached for connector.
15572 */
df0e9248 15573struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
79e53945 15574{
df0e9248
CW
15575 return &intel_attached_encoder(connector)->base;
15576}
f1c79df3 15577
df0e9248
CW
15578void intel_connector_attach_encoder(struct intel_connector *connector,
15579 struct intel_encoder *encoder)
15580{
15581 connector->encoder = encoder;
15582 drm_mode_connector_attach_encoder(&connector->base,
15583 &encoder->base);
79e53945 15584}
28d52043
DA
15585
15586/*
15587 * set vga decode state - true == enable VGA decode
15588 */
15589int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
15590{
15591 struct drm_i915_private *dev_priv = dev->dev_private;
a885b3cc 15592 unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
28d52043
DA
15593 u16 gmch_ctrl;
15594
75fa041d
CW
15595 if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
15596 DRM_ERROR("failed to read control word\n");
15597 return -EIO;
15598 }
15599
c0cc8a55
CW
15600 if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
15601 return 0;
15602
28d52043
DA
15603 if (state)
15604 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
15605 else
15606 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
75fa041d
CW
15607
15608 if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
15609 DRM_ERROR("failed to write control word\n");
15610 return -EIO;
15611 }
15612
28d52043
DA
15613 return 0;
15614}
c4a1d9e4 15615
c4a1d9e4 15616struct intel_display_error_state {
ff57f1b0
PZ
15617
15618 u32 power_well_driver;
15619
63b66e5b
CW
15620 int num_transcoders;
15621
c4a1d9e4
CW
15622 struct intel_cursor_error_state {
15623 u32 control;
15624 u32 position;
15625 u32 base;
15626 u32 size;
52331309 15627 } cursor[I915_MAX_PIPES];
c4a1d9e4
CW
15628
15629 struct intel_pipe_error_state {
ddf9c536 15630 bool power_domain_on;
c4a1d9e4 15631 u32 source;
f301b1e1 15632 u32 stat;
52331309 15633 } pipe[I915_MAX_PIPES];
c4a1d9e4
CW
15634
15635 struct intel_plane_error_state {
15636 u32 control;
15637 u32 stride;
15638 u32 size;
15639 u32 pos;
15640 u32 addr;
15641 u32 surface;
15642 u32 tile_offset;
52331309 15643 } plane[I915_MAX_PIPES];
63b66e5b
CW
15644
15645 struct intel_transcoder_error_state {
ddf9c536 15646 bool power_domain_on;
63b66e5b
CW
15647 enum transcoder cpu_transcoder;
15648
15649 u32 conf;
15650
15651 u32 htotal;
15652 u32 hblank;
15653 u32 hsync;
15654 u32 vtotal;
15655 u32 vblank;
15656 u32 vsync;
15657 } transcoder[4];
c4a1d9e4
CW
15658};
15659
15660struct intel_display_error_state *
c033666a 15661intel_display_capture_error_state(struct drm_i915_private *dev_priv)
c4a1d9e4 15662{
c4a1d9e4 15663 struct intel_display_error_state *error;
63b66e5b
CW
15664 int transcoders[] = {
15665 TRANSCODER_A,
15666 TRANSCODER_B,
15667 TRANSCODER_C,
15668 TRANSCODER_EDP,
15669 };
c4a1d9e4
CW
15670 int i;
15671
c033666a 15672 if (INTEL_INFO(dev_priv)->num_pipes == 0)
63b66e5b
CW
15673 return NULL;
15674
9d1cb914 15675 error = kzalloc(sizeof(*error), GFP_ATOMIC);
c4a1d9e4
CW
15676 if (error == NULL)
15677 return NULL;
15678
c033666a 15679 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
ff57f1b0
PZ
15680 error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
15681
055e393f 15682 for_each_pipe(dev_priv, i) {
ddf9c536 15683 error->pipe[i].power_domain_on =
f458ebbc
DV
15684 __intel_display_power_is_enabled(dev_priv,
15685 POWER_DOMAIN_PIPE(i));
ddf9c536 15686 if (!error->pipe[i].power_domain_on)
9d1cb914
PZ
15687 continue;
15688
5efb3e28
VS
15689 error->cursor[i].control = I915_READ(CURCNTR(i));
15690 error->cursor[i].position = I915_READ(CURPOS(i));
15691 error->cursor[i].base = I915_READ(CURBASE(i));
c4a1d9e4
CW
15692
15693 error->plane[i].control = I915_READ(DSPCNTR(i));
15694 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
c033666a 15695 if (INTEL_GEN(dev_priv) <= 3) {
51889b35 15696 error->plane[i].size = I915_READ(DSPSIZE(i));
80ca378b
PZ
15697 error->plane[i].pos = I915_READ(DSPPOS(i));
15698 }
c033666a 15699 if (INTEL_GEN(dev_priv) <= 7 && !IS_HASWELL(dev_priv))
ca291363 15700 error->plane[i].addr = I915_READ(DSPADDR(i));
c033666a 15701 if (INTEL_GEN(dev_priv) >= 4) {
c4a1d9e4
CW
15702 error->plane[i].surface = I915_READ(DSPSURF(i));
15703 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
15704 }
15705
c4a1d9e4 15706 error->pipe[i].source = I915_READ(PIPESRC(i));
f301b1e1 15707
c033666a 15708 if (HAS_GMCH_DISPLAY(dev_priv))
f301b1e1 15709 error->pipe[i].stat = I915_READ(PIPESTAT(i));
63b66e5b
CW
15710 }
15711
4d1de975 15712 /* Note: this does not include DSI transcoders. */
c033666a 15713 error->num_transcoders = INTEL_INFO(dev_priv)->num_pipes;
2d1fe073 15714 if (HAS_DDI(dev_priv))
63b66e5b
CW
15715 error->num_transcoders++; /* Account for eDP. */
15716
15717 for (i = 0; i < error->num_transcoders; i++) {
15718 enum transcoder cpu_transcoder = transcoders[i];
15719
ddf9c536 15720 error->transcoder[i].power_domain_on =
f458ebbc 15721 __intel_display_power_is_enabled(dev_priv,
38cc1daf 15722 POWER_DOMAIN_TRANSCODER(cpu_transcoder));
ddf9c536 15723 if (!error->transcoder[i].power_domain_on)
9d1cb914
PZ
15724 continue;
15725
63b66e5b
CW
15726 error->transcoder[i].cpu_transcoder = cpu_transcoder;
15727
15728 error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
15729 error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
15730 error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
15731 error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
15732 error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
15733 error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
15734 error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
c4a1d9e4
CW
15735 }
15736
15737 return error;
15738}
15739
edc3d884
MK
15740#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
15741
c4a1d9e4 15742void
edc3d884 15743intel_display_print_error_state(struct drm_i915_error_state_buf *m,
c4a1d9e4
CW
15744 struct drm_device *dev,
15745 struct intel_display_error_state *error)
15746{
055e393f 15747 struct drm_i915_private *dev_priv = dev->dev_private;
c4a1d9e4
CW
15748 int i;
15749
63b66e5b
CW
15750 if (!error)
15751 return;
15752
edc3d884 15753 err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
190be112 15754 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
edc3d884 15755 err_printf(m, "PWR_WELL_CTL2: %08x\n",
ff57f1b0 15756 error->power_well_driver);
055e393f 15757 for_each_pipe(dev_priv, i) {
edc3d884 15758 err_printf(m, "Pipe [%d]:\n", i);
ddf9c536 15759 err_printf(m, " Power: %s\n",
87ad3212 15760 onoff(error->pipe[i].power_domain_on));
edc3d884 15761 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
f301b1e1 15762 err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
edc3d884
MK
15763
15764 err_printf(m, "Plane [%d]:\n", i);
15765 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
15766 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
80ca378b 15767 if (INTEL_INFO(dev)->gen <= 3) {
edc3d884
MK
15768 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
15769 err_printf(m, " POS: %08x\n", error->plane[i].pos);
80ca378b 15770 }
4b71a570 15771 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
edc3d884 15772 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
c4a1d9e4 15773 if (INTEL_INFO(dev)->gen >= 4) {
edc3d884
MK
15774 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
15775 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
c4a1d9e4
CW
15776 }
15777
edc3d884
MK
15778 err_printf(m, "Cursor [%d]:\n", i);
15779 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
15780 err_printf(m, " POS: %08x\n", error->cursor[i].position);
15781 err_printf(m, " BASE: %08x\n", error->cursor[i].base);
c4a1d9e4 15782 }
63b66e5b
CW
15783
15784 for (i = 0; i < error->num_transcoders; i++) {
da205630 15785 err_printf(m, "CPU transcoder: %s\n",
63b66e5b 15786 transcoder_name(error->transcoder[i].cpu_transcoder));
ddf9c536 15787 err_printf(m, " Power: %s\n",
87ad3212 15788 onoff(error->transcoder[i].power_domain_on));
63b66e5b
CW
15789 err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
15790 err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
15791 err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
15792 err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
15793 err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
15794 err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
15795 err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
15796 }
c4a1d9e4 15797}